From patchwork Wed Jul 13 18:22:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Noah Goldstein X-Patchwork-Id: 56045 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D21EE382C164 for ; Wed, 13 Jul 2022 18:23:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D21EE382C164 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1657736619; bh=E7qgF5OjWHFewSl8Mpp+YNCZvscLXhdA8lBvBXsBeRU=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=T4utfWaKo8Si0BztHD7LYZixvEf8JWY/3SS+hSa0ycbSTqq1mmupIGSFOzQvGbj4m fzjoJUytDFCWH5xHtc0WciW78vGpEZY/XYKl/Vl1JlsheJg9vmOpFSFPSc+IgnkOQs Mj6mgNEgxfU1bgmGJufMWTmq96Xg9pHIdxdMZy3I= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by sourceware.org (Postfix) with ESMTPS id AD5F8388553D for ; Wed, 13 Jul 2022 18:23:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AD5F8388553D Received: by mail-pf1-x434.google.com with SMTP id b9so10929051pfp.10 for ; Wed, 13 Jul 2022 11:23:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=E7qgF5OjWHFewSl8Mpp+YNCZvscLXhdA8lBvBXsBeRU=; b=0eIIrnm5DBE4j2yGUrns0x+dQUTnVBmwa/vvzGr1QbC53YVQngX6p/LqQuuhb5Ajmo 4d6YWwKpt5hXSLminXcFt3mLLq2GcIKW0QMJC/LtsQQ8UsosIwLlcW5dEzh7SK/MvoaY r2PqxO1TPWQ2nQaW/AyryHIX/N/Cn9ewaifyO5/UWYjN/qnGBoG3i7Yfrx0j7a/U7z2K XmYQSPW4XEzv/h31Mz+AIl5kYk8YNwKO9QwW84PQCKBRrFylmkRtSbRhgSa59FhNFdof Q6JPYHwm5kjmxmATCkwqLzhFRKTNHNnS3JIa7om9JCdZQ3pKYc1e4ifeYn91rfwLM7TB H9Sw== X-Gm-Message-State: AJIora/8+UUFZMHBjy0BbEGdf9kRb9tbxWTK79KFDsuhd3zmBOLbnaE0 9Pe/o02XRImtPWhTHInY4/0rBhNsAoU= X-Google-Smtp-Source: AGRyM1sN8fTdXiL077C6mri5ooW1oeN2AIi0jO033vCulBI1o5hvMHfdf7UDAYeBPUicREZnKE/l1g== X-Received: by 2002:a63:e5c:0:b0:416:8db:4f5f with SMTP id 28-20020a630e5c000000b0041608db4f5fmr3939435pgo.620.1657736596450; Wed, 13 Jul 2022 11:23:16 -0700 (PDT) Received: from noah-tgl.. ([192.55.60.37]) by smtp.gmail.com with ESMTPSA id x89-20020a17090a6c6200b001e2f892b352sm1916912pjj.45.2022.07.13.11.23.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Jul 2022 11:23:16 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v1] nptl: Replace CAS with cheaper atomics for pthread_cancel logic Date: Wed, 13 Jul 2022 11:22:55 -0700 Message-Id: <20220713182254.3592361-1-goldstein.w.n@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Noah Goldstein via Libc-alpha From: Noah Goldstein Reply-To: Noah Goldstein Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" The logic for setting/unsetting/testing CANCELTYPE_BITMASK can often be done with `atomic_fetch_{or|and}` instead of a CAS. On some arch (x86 for example) this can be optimized to more efficient instructions (`bt{s|r}`) instead of a CAS loop. Otherwise compilers will just optimize it to the CAS equivilent. Full check passed on x86-64 linux. --- The replacements of CAS with atomic_fetch_and in: - ___pthread_register_cancel_defer - __libc_cleanup_push_defer is logically equivilent AFAICT. The replacement of CAS with atomic_fetch_or in: - ___pthread_unregister_cancel_restore - __libc_cleanup_pop_restore not not logically equivilent and may have an issue. In `___pthread_unregister_cancel_restore` and `__libc_cleanup_pop_restore` since the CAS loop would reload `cancelhandling` on failed CAS there may now be a race condition on the later check of `cancel_enabled_and_canceled`. nptl/cleanup_defer.c | 28 ++++++++-------------------- nptl/libc-cleanup.c | 28 ++++++++-------------------- 2 files changed, 16 insertions(+), 40 deletions(-) diff --git a/nptl/cleanup_defer.c b/nptl/cleanup_defer.c index 4e864ead32..000b9d1553 100644 --- a/nptl/cleanup_defer.c +++ b/nptl/cleanup_defer.c @@ -33,19 +33,14 @@ ___pthread_register_cancel_defer (__pthread_unwind_buf_t *buf) int cancelhandling = atomic_load_relaxed (&self->cancelhandling); if (__glibc_unlikely (cancelhandling & CANCELTYPE_BITMASK)) { - int newval; - do - { - newval = cancelhandling & ~CANCELTYPE_BITMASK; - } - while (!atomic_compare_exchange_weak_acquire (&self->cancelhandling, - &cancelhandling, - newval)); + if (atomic_fetch_and_acquire (&self->cancelhandling, ~CANCELTYPE_BITMASK) + & CANCELTYPE_BITMASK) + ibuf->priv.data.canceltype = PTHREAD_CANCEL_ASYNCHRONOUS; + else + ibuf->priv.data.canceltype = PTHREAD_CANCEL_DEFERRED; } - - ibuf->priv.data.canceltype = (cancelhandling & CANCELTYPE_BITMASK - ? PTHREAD_CANCEL_ASYNCHRONOUS - : PTHREAD_CANCEL_DEFERRED); + else + ibuf->priv.data.canceltype = PTHREAD_CANCEL_DEFERRED; /* Store the new cleanup handler info. */ THREAD_SETMEM (self, cleanup_jmp_buf, (struct pthread_unwind_buf *) buf); @@ -73,14 +68,7 @@ ___pthread_unregister_cancel_restore (__pthread_unwind_buf_t *buf) int cancelhandling = atomic_load_relaxed (&self->cancelhandling); if ((cancelhandling & CANCELTYPE_BITMASK) == 0) { - int newval; - do - { - newval = cancelhandling | CANCELTYPE_BITMASK; - } - while (!atomic_compare_exchange_weak_acquire (&self->cancelhandling, - &cancelhandling, newval)); - + atomic_fetch_or_acquire (&self->cancelhandling, CANCELTYPE_BITMASK); if (cancel_enabled_and_canceled (cancelhandling)) { self->result = PTHREAD_CANCELED; diff --git a/nptl/libc-cleanup.c b/nptl/libc-cleanup.c index 2ce59388d4..8c6b3d79b4 100644 --- a/nptl/libc-cleanup.c +++ b/nptl/libc-cleanup.c @@ -31,19 +31,14 @@ __libc_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer) /* Disable asynchronous cancellation for now. */ if (__glibc_unlikely (cancelhandling & CANCELTYPE_BITMASK)) { - int newval; - do - { - newval = cancelhandling & ~CANCELTYPE_BITMASK; - } - while (!atomic_compare_exchange_weak_acquire (&self->cancelhandling, - &cancelhandling, - newval)); + if (atomic_fetch_and_acquire (&self->cancelhandling, ~CANCELTYPE_BITMASK) + & CANCELTYPE_BITMASK) + buffer->__canceltype = PTHREAD_CANCEL_ASYNCHRONOUS; + else + buffer->__canceltype = PTHREAD_CANCEL_DEFERRED; } - - buffer->__canceltype = (cancelhandling & CANCELTYPE_BITMASK - ? PTHREAD_CANCEL_ASYNCHRONOUS - : PTHREAD_CANCEL_DEFERRED); + else + buffer->__canceltype = PTHREAD_CANCEL_DEFERRED; THREAD_SETMEM (self, cleanup, buffer); } @@ -60,14 +55,7 @@ __libc_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer) if (buffer->__canceltype != PTHREAD_CANCEL_DEFERRED && (cancelhandling & CANCELTYPE_BITMASK) == 0) { - int newval; - do - { - newval = cancelhandling | CANCELTYPE_BITMASK; - } - while (!atomic_compare_exchange_weak_acquire (&self->cancelhandling, - &cancelhandling, newval)); - + atomic_fetch_or_acquire (&self->cancelhandling, CANCELTYPE_BITMASK); if (cancel_enabled_and_canceled (cancelhandling)) { self->result = PTHREAD_CANCELED;