From patchwork Thu Sep 30 20:00:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 45625 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 602963857C6E for ; Thu, 30 Sep 2021 20:03:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 602963857C6E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1633032204; bh=xpE2xJCCUTCjyBp4AleIVzQFlOHw0PRqI2CrDKm5JS4=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=u1glPUAeL9dPDU+EF1/MdkS2rDt0gwTZ+FfGTFTME6O+9KF0+vmwFI/buIjN9kxyj w+zjcA5paT642WPiqr9cDWbyMXvKEKaEuh0gM8HZZnJgGIVO1WlPYMH/jSPZVqBT62 cRG1Veqahu4x9tbeO36Pzb5JOKL6LNyr7+okC1/U= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qv1-xf31.google.com (mail-qv1-xf31.google.com [IPv6:2607:f8b0:4864:20::f31]) by sourceware.org (Postfix) with ESMTPS id 79FE63858422 for ; Thu, 30 Sep 2021 20:01:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 79FE63858422 Received: by mail-qv1-xf31.google.com with SMTP id jo30so4326116qvb.3 for ; Thu, 30 Sep 2021 13:01:01 -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:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=xpE2xJCCUTCjyBp4AleIVzQFlOHw0PRqI2CrDKm5JS4=; b=fkqsQhAJDiiYZj0nBD2Md1tWfDhVInGhD1Lvt6nqUlhM8bbX0KPKIz2wQm0yQ5pWJp gfSCw14PXsrrl6Yp5n1MCnECbJpW1qvcnP9b6CVCx8frp5LPhNrMI5yGLMJDBsY6zfp3 Bf+sEbvGbxowXkCxO1ZpWM7D9PG000ecWhAwqFDwDyeece6p4TI0hIZ37chjW5Rb+VHY X7Qj9/GoYJpqVDscndiBArYDHmy/srf5RnOSEXnapZMwwsmLP3ZfDEudhYGPNFb7V14s eOhi3UGyP7x1+td8KNqwsjPVTepYlgPqbO31ZVrMwuUVDR1oXy/qOIk7nIHso7shQbb+ 3YcA== X-Gm-Message-State: AOAM532B1bJLmlCxANoGp4FyaROVQtPFPoKSnjKq4XdBCZGnkW46/wvp 7yudfsjzWtnGsTuel8AmnJwWvXdRB1D0tQ== X-Google-Smtp-Source: ABdhPJzgmnywSqIqqkzDQx02AEKRhp0CDMYMfwI28lnwEytgcF9CgfpHcC5veP0CS6uMNa4LrT5aTw== X-Received: by 2002:ad4:44e4:: with SMTP id p4mr5694958qvt.40.1633032060852; Thu, 30 Sep 2021 13:01:00 -0700 (PDT) Received: from birita.. ([2804:431:c7cb:b338:b20e:53f8:8b5:1bb2]) by smtp.gmail.com with ESMTPSA id r196sm1910203qka.119.2021.09.30.13.01.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Sep 2021 13:01:00 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 04/15] nptl: Move setxid flag out of cancelhandling Date: Thu, 30 Sep 2021 17:00:40 -0300 Message-Id: <20210930200051.1017457-5-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210930200051.1017457-1-adhemerval.zanella@linaro.org> References: <20210930200051.1017457-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Now that the thread state is tracked by 'joinstate' field, there is no need to keep the setxid flag within the 'cancelhandling'. It simplifies the atomic code to set and reset it (since there is no need to handle the thread state concurrent update). Checked on x86_64-linux-gnu and aarch64-linux-gnu. --- nptl/allocatestack.c | 3 +++ nptl/descr.h | 6 ++--- nptl/nptl_setxid.c | 55 ++++++++++++++----------------------------- nptl/pthread_create.c | 4 ++-- 4 files changed, 26 insertions(+), 42 deletions(-) diff --git a/nptl/allocatestack.c b/nptl/allocatestack.c index fa8100719f..74a8bbc19c 100644 --- a/nptl/allocatestack.c +++ b/nptl/allocatestack.c @@ -99,6 +99,7 @@ get_cached_stack (size_t *sizep, void **memp) } /* Don't allow setxid until cloned. */ + result->setxid_flag = 0; result->setxid_futex = -1; /* Dequeue the entry. */ @@ -303,6 +304,7 @@ allocate_stack (const struct pthread_attr *attr, struct pthread **pdp, #endif /* Don't allow setxid until cloned. */ + pd->setxid_flag = 0; pd->setxid_futex = -1; /* Allocate the DTV for this thread. */ @@ -424,6 +426,7 @@ allocate_stack (const struct pthread_attr *attr, struct pthread **pdp, #endif /* Don't allow setxid until cloned. */ + pd->setxid_flag = 0; pd->setxid_futex = -1; /* Allocate the DTV for this thread. */ diff --git a/nptl/descr.h b/nptl/descr.h index 76522b85b3..db011af500 100644 --- a/nptl/descr.h +++ b/nptl/descr.h @@ -296,9 +296,6 @@ struct pthread /* Bit set if thread terminated and TCB is freed. */ #define TERMINATED_BIT 5 #define TERMINATED_BITMASK (0x01 << TERMINATED_BIT) - /* Bit set if thread is supposed to change XID. */ -#define SETXID_BIT 6 -#define SETXID_BITMASK (0x01 << SETXID_BIT) /* Flags. Including those copied from the thread attribute. */ int flags; @@ -338,6 +335,9 @@ struct pthread /* Lock to synchronize access to the descriptor. */ int lock; + /* Indicate whether thread is supposed to change XID, it acts a boolean + variable (0 means no operation pending). */ + int setxid_flag; /* Lock for synchronizing setxid calls. */ unsigned int setxid_futex; diff --git a/nptl/nptl_setxid.c b/nptl/nptl_setxid.c index 1bd7b5f803..244536260d 100644 --- a/nptl/nptl_setxid.c +++ b/nptl/nptl_setxid.c @@ -75,14 +75,7 @@ __nptl_setxid_sighandler (int sig, siginfo_t *si, void *ctx) /* Reset the SETXID flag. */ struct pthread *self = THREAD_SELF; - int flags, newval; - do - { - flags = THREAD_GETMEM (self, cancelhandling); - newval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling, - flags & ~SETXID_BITMASK, flags); - } - while (flags != newval); + atomic_store_release (&self->setxid_flag, 0); /* And release the futex. */ self->setxid_futex = 1; @@ -96,8 +89,6 @@ libc_hidden_def (__nptl_setxid_sighandler) static void setxid_mark_thread (struct xid_command *cmdp, struct pthread *t) { - int ch; - /* Wait until this thread is cloned. */ if (t->setxid_futex == -1 && ! atomic_compare_and_exchange_bool_acq (&t->setxid_futex, -2, -1)) @@ -108,41 +99,31 @@ setxid_mark_thread (struct xid_command *cmdp, struct pthread *t) /* Don't let the thread exit before the setxid handler runs. */ t->setxid_futex = 0; - do + /* If thread is exiting right now, ignore it. */ + if (atomic_load_acquire (&t->joinstate) == THREAD_STATE_EXITING) { - ch = t->cancelhandling; - - /* If the thread is exiting right now, ignore it. */ - if ((ch & EXITING_BITMASK) != 0) - { - /* Release the futex if there is no other setxid in - progress. */ - if ((ch & SETXID_BITMASK) == 0) - { - t->setxid_futex = 1; - futex_wake (&t->setxid_futex, 1, FUTEX_PRIVATE); - } - return; + /* Release the futex if there is no other setxid in progress. */ + if (atomic_load_relaxed (&t->setxid_flag) == 0) + { + t->setxid_futex = 1; + futex_wake (&t->setxid_futex, 1, FUTEX_PRIVATE); } + return; + } + + /* Release the futex if there is no other setxid in progress. */ + if (atomic_exchange_acquire (&t->setxid_flag, 1) == 0) + { + t->setxid_futex = 1; + futex_wake (&t->setxid_futex, 1, FUTEX_PRIVATE); } - while (atomic_compare_and_exchange_bool_acq (&t->cancelhandling, - ch | SETXID_BITMASK, ch)); } static void setxid_unmark_thread (struct xid_command *cmdp, struct pthread *t) { - int ch; - - do - { - ch = t->cancelhandling; - if ((ch & SETXID_BITMASK) == 0) - return; - } - while (atomic_compare_and_exchange_bool_acq (&t->cancelhandling, - ch & ~SETXID_BITMASK, ch)); + atomic_store_release (&t->setxid_flag, 0); /* Release the futex just in case. */ t->setxid_futex = 1; @@ -153,7 +134,7 @@ setxid_unmark_thread (struct xid_command *cmdp, struct pthread *t) static int setxid_signal_thread (struct xid_command *cmdp, struct pthread *t) { - if ((t->cancelhandling & SETXID_BITMASK) == 0) + if (atomic_load_relaxed (&t->setxid_flag) == 0) return 0; int val; diff --git a/nptl/pthread_create.c b/nptl/pthread_create.c index c7d53edbb8..3659f51c46 100644 --- a/nptl/pthread_create.c +++ b/nptl/pthread_create.c @@ -562,7 +562,7 @@ start_thread (void *arg) advise_stack_range (pd->stackblock, pd->stackblock_size, (uintptr_t) pd, pd->guardsize); - if (__glibc_unlikely (pd->cancelhandling & SETXID_BITMASK)) + if (__glibc_unlikely (atomic_load_relaxed (&pd->setxid_flag) == 1)) { /* Some other thread might call any of the setXid functions and expect us to reply. In this case wait until we did that. */ @@ -572,7 +572,7 @@ start_thread (void *arg) condition used in the surrounding loop (cancelhandling). We need to check and document why this is correct. */ futex_wait_simple (&pd->setxid_futex, 0, FUTEX_PRIVATE); - while (pd->cancelhandling & SETXID_BITMASK); + while (atomic_load_relaxed (&pd->setxid_flag) == 1); /* Reset the value so that the stack can be reused. */ pd->setxid_futex = 0;