From patchwork Thu Sep 30 20:00:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 45627 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 182C63857C75 for ; Thu, 30 Sep 2021 20:04:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 182C63857C75 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1633032295; bh=7FD/LHY7tLIIADimH+sKik/92rZBRYBTQ5jwr0Q8uXM=; 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=jEy2aUhpN/+IDMvDlmhB2R8CpEAIMK8hgU1KWMga5H/5WVNypVEAkigQJH7XZYtJQ Td+EDBTX7Pz/sAjW+VbNr9ikCgSsHkfRcLgqy1uSp6mS2zdfjZs5HuDCuwJFkmA7gm 9zvVo6BHFVvX1ZgfTO1BgZGPAxvmj9ayDNzDQ9ac= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qt1-x835.google.com (mail-qt1-x835.google.com [IPv6:2607:f8b0:4864:20::835]) by sourceware.org (Postfix) with ESMTPS id A50143857C56 for ; Thu, 30 Sep 2021 20:01:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A50143857C56 Received: by mail-qt1-x835.google.com with SMTP id f15so6882819qtv.9 for ; Thu, 30 Sep 2021 13:01:03 -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=7FD/LHY7tLIIADimH+sKik/92rZBRYBTQ5jwr0Q8uXM=; b=Ja6diRXkm73RHVAbKY84QmDs4hY2H+7N7j1j9iSCbJ5F6gKKsgRIRZUN1TtB5S5Og+ XFwVxvTtx1/6LguM4HRsHsHaK6GrdxRunYsdxm/m5z8yknMLkBu3/DquVuQ5lEA3/YpI zJN99Ah/lw8BYji1kxo4JHCc+OJ9zYpzSzEwXwW+RlBKdl8mgT9rbY7qEr0XJ3XpiKeC vm1j4fDNbRShVHfRAYfMBdZEWDerU7SRClqoopbdVN3nPjh09pvoBHwFB49n4CByMRIZ VN0Wp8fuq6Cda+rvHWeuHFuHyxWf0xsq9VgzaDIMeX7euAkbI0sgGQ3iDhBnSqQUoAX9 fnnA== X-Gm-Message-State: AOAM531NiTpbht9ZRM4ttbJY/SmAERkQrqsSiEfqwVO5gInz9IWGEStu SObsHxhyXmrqXVL0lgQEBX6x+bc72Mf3cQ== X-Google-Smtp-Source: ABdhPJwEFwg3iYCnqZTgD7GZ9ysfmDGQci0X8c82m7sY+JhOGyo4mQLW6DmmX5joJyqoVb9Gx3G/UQ== X-Received: by 2002:ac8:4819:: with SMTP id g25mr8575188qtq.364.1633032063044; Thu, 30 Sep 2021 13:01:03 -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.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Sep 2021 13:01:02 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 06/15] nptl: Use exit_lock when accessing TID on pthread_getaffinity_np Date: Thu, 30 Sep 2021 17:00:42 -0300 Message-Id: <20210930200051.1017457-7-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.4 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 returning the calling thread affinity (current behaviour), since the thread lifetime is defined. Checked on x86_64-linux-gnu. --- nptl/pthread_getaffinity.c | 28 +++++++++------ sysdeps/pthread/Makefile | 1 + sysdeps/pthread/tst-pthread-exited.c | 51 ++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 11 deletions(-) create mode 100644 sysdeps/pthread/tst-pthread-exited.c diff --git a/nptl/pthread_getaffinity.c b/nptl/pthread_getaffinity.c index 3b23a4b50d..7cf239c335 100644 --- a/nptl/pthread_getaffinity.c +++ b/nptl/pthread_getaffinity.c @@ -15,25 +15,31 @@ License along with the GNU C Library; if not, see . */ -#include -#include +#include #include -#include -#include -#include -#include #include int __pthread_getaffinity_np (pthread_t th, size_t cpusetsize, cpu_set_t *cpuset) { - const struct pthread *pd = (const struct pthread *) th; + struct pthread *pd = (struct pthread *) th; - int res = INTERNAL_SYSCALL_CALL (sched_getaffinity, pd->tid, - MIN (INT_MAX, cpusetsize), cpuset); - if (INTERNAL_SYSCALL_ERROR_P (res)) - return INTERNAL_SYSCALL_ERRNO (res); + /* 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); + + int res = pd->tid == 0 + ? -ESRCH + : INTERNAL_SYSCALL_CALL (sched_getaffinity, pd->tid, + MIN (INT_MAX, cpusetsize), cpuset); + + __libc_lock_unlock (pd->exit_lock); + __libc_signal_restore_set (&old_mask); + + if (res < 0) + return -res; /* Clean the rest of the memory the kernel didn't do. */ memset ((char *) cpuset + res, '\0', cpusetsize - res); diff --git a/sysdeps/pthread/Makefile b/sysdeps/pthread/Makefile index d4bd2d4e3e..e4c32f9d65 100644 --- a/sysdeps/pthread/Makefile +++ b/sysdeps/pthread/Makefile @@ -123,6 +123,7 @@ tests += tst-cnd-basic tst-mtx-trylock tst-cnd-broadcast \ tst-pthread_cancel-select-loop \ tst-pthread_kill-exited \ tst-pthread_kill-exiting \ + tst-pthread-exited # tests tests-time64 := \ diff --git a/sysdeps/pthread/tst-pthread-exited.c b/sysdeps/pthread/tst-pthread-exited.c new file mode 100644 index 0000000000..c27e856dea --- /dev/null +++ b/sysdeps/pthread/tst-pthread-exited.c @@ -0,0 +1,51 @@ +/* Test pthread interface which should return ESRCH when issues along + with a terminated pthread_t. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include +#include + +static void * +noop_thread (void *closure) +{ + return NULL; +} + +static int +do_test (void) +{ + pthread_t thr = xpthread_create (NULL, noop_thread, NULL); + + support_wait_for_thread_exit (); + + { + cpu_set_t cpuset; + int r = pthread_getaffinity_np (thr, sizeof (cpuset), &cpuset); + TEST_COMPARE (r, ESRCH); + } + + xpthread_join (thr); + + return 0; +} + +#include