From patchwork Thu Sep 30 20:00:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 45629 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 E76E73857C7E for ; Thu, 30 Sep 2021 20:06:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E76E73857C7E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1633032385; bh=wdUHfvojZerbtV3uTZp6KR2m5pF9ja1T/m0DkKdH2gU=; 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=l+D2R6iQYwdFJajNHDz0x69+YvHKRKOeuy7Aj2N3w643DclRV/OeY1aPEvvpHiCam /uNWZW6G4GhIaJVvvvKU6zPcjSspeqVqGyGZSvuSuhuQAEHwYR84H2XAqsFJEK3Cc9 j6eIlXnZUFPM857yO2tmUHNN3ryCWw3qmJ2SxX4g= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x72d.google.com (mail-qk1-x72d.google.com [IPv6:2607:f8b0:4864:20::72d]) by sourceware.org (Postfix) with ESMTPS id AFA283857C53 for ; Thu, 30 Sep 2021 20:01:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AFA283857C53 Received: by mail-qk1-x72d.google.com with SMTP id 72so7048153qkk.7 for ; Thu, 30 Sep 2021 13:01:04 -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=wdUHfvojZerbtV3uTZp6KR2m5pF9ja1T/m0DkKdH2gU=; b=nWJDUuCCVWEJ6lOam2BbnO0eTMoRKLzL2RqlaGJsBqKyUAdlG9sLw9tjmxuMiHbczZ be1zYeF7axyKefkCHjlLT6YKaKgZ53PgYv9BXw9hb69L49Q7wq0Dmqfb38ILPwr0us53 mbUgiS8V/gdixM9ebsYZMCHzoRzZRH8LpmBh80S8VBDenmRV9NQaJyw553/75+3FcF91 sTLtAGEwOfpOc676vk1uM8zt81EUQWZExake9KMxK5Ndk7Blh1rKuRDIh5ag6NNM87V+ rMGOPKeopf9+qi3upR2PXA2EREhhmK6QqR7k1F61f5smKqj0G6ZX/CHF25vwEOw3DSdj dDHw== X-Gm-Message-State: AOAM5337GYZsfjBYheOglZXYZFRT0Su8rBhfRm9TbAyzXiU6CUN7epPr 8k7inJT9KWXR9gVAVfmyDKPDKsW0UStPqg== X-Google-Smtp-Source: ABdhPJxCVjLiNIvvM4+vPsk0tQWciqcb0Am9c2SbI166x8XxC2wA/qLyZ9hJcYKQelLBFEvp6WjwtA== X-Received: by 2002:a37:a60f:: with SMTP id p15mr6548581qke.114.1633032064103; Thu, 30 Sep 2021 13:01:04 -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.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Sep 2021 13:01:03 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 07/15] nptl: Use exit_lock when accessing TID on pthread_setaffinity Date: Thu, 30 Sep 2021 17:00:43 -0300 Message-Id: <20210930200051.1017457-8-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.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, 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 Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Also return ESRCH if the thread is already terminated at the time of the call. This is slight better than setting the calling thread affinity (current behaviour). Checked on x86_64-linux-gnu. --- nptl/pthread_setaffinity.c | 25 +++++++++++++++---------- sysdeps/pthread/tst-pthread-exited.c | 7 +++++++ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/nptl/pthread_setaffinity.c b/nptl/pthread_setaffinity.c index 2ae6ae694c..cefc48e7c5 100644 --- a/nptl/pthread_setaffinity.c +++ b/nptl/pthread_setaffinity.c @@ -15,10 +15,8 @@ License along with the GNU C Library; if not, see . */ -#include +#include #include -#include -#include #include @@ -26,15 +24,22 @@ int __pthread_setaffinity_new (pthread_t th, size_t cpusetsize, const cpu_set_t *cpuset) { - const struct pthread *pd = (const struct pthread *) th; - int res; + struct pthread *pd = (struct pthread *) th; - res = INTERNAL_SYSCALL_CALL (sched_setaffinity, pd->tid, cpusetsize, - cpuset); + /* Block all signals, as required by pd->exit_lock. */ + sigset_t old_mask; + __libc_signal_block_all (&old_mask); + __libc_lock_lock (pd->exit_lock); - return (INTERNAL_SYSCALL_ERROR_P (res) - ? INTERNAL_SYSCALL_ERRNO (res) - : 0); + int res = pd->tid == 0 + ? ESRCH + : -INTERNAL_SYSCALL_CALL (sched_setaffinity, pd->tid, cpusetsize, + cpuset); + + __libc_lock_unlock (pd->exit_lock); + __libc_signal_restore_set (&old_mask); + + return res; } versioned_symbol (libc, __pthread_setaffinity_new, pthread_setaffinity_np, GLIBC_2_34); diff --git a/sysdeps/pthread/tst-pthread-exited.c b/sysdeps/pthread/tst-pthread-exited.c index c27e856dea..567d6b9b49 100644 --- a/sysdeps/pthread/tst-pthread-exited.c +++ b/sysdeps/pthread/tst-pthread-exited.c @@ -43,6 +43,13 @@ do_test (void) TEST_COMPARE (r, ESRCH); } + { + cpu_set_t cpuset; + CPU_ZERO (&cpuset); + int r = pthread_setaffinity_np (thr, sizeof (cpuset), &cpuset); + TEST_COMPARE (r, ESRCH); + } + xpthread_join (thr); return 0;