From patchwork Tue May 31 17:52:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 54582 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 8C693396D827 for ; Tue, 31 May 2022 17:55:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8C693396D827 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1654019726; bh=Q0aF7/209msWysQc+CCAYLMrwoMOgA4R5y89ZTAdNQU=; 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=ZCFXo37YrnzYhDmdJl11dvhuv1ajpqTCedzyJNh2SZScsAjgzRkDIjvtruUBlEODi IIWYI8wW79VL/o65OdxFAPVahLY1jCNKcxSlCTArfoGVyp2ZMvAJzIN2zPrDO7IVBM AzFyJZBqwdOHDlIVuUJl0LrFg61efbH7pomDrz/M= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oi1-x22a.google.com (mail-oi1-x22a.google.com [IPv6:2607:f8b0:4864:20::22a]) by sourceware.org (Postfix) with ESMTPS id 93E25396D828 for ; Tue, 31 May 2022 17:53:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 93E25396D828 Received: by mail-oi1-x22a.google.com with SMTP id s188so18394989oie.4 for ; Tue, 31 May 2022 10:53: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=Q0aF7/209msWysQc+CCAYLMrwoMOgA4R5y89ZTAdNQU=; b=aZy9TFCIe77jhTifttGoHVKFzqqjnEl9++UBAr6fXRPnv2uwJmyav6RwHiyzNgwnS+ w7YTRjgM6AIKZyonquXL9dtnK/QMX4i/cwnXZFfFKh9IU7hiCrng2XTiVZlvxOyVUFl9 zof47Q+1C9ErcM/17VIGUbuahTEUzHWZk1oyft6E66/f859VEkgHfHMhOFZtRYFx4EHU ozMyvA8O5GmkSRWWaJQwsV8Ba2ScQH3m5U3Lsm1f10eG7X0TH5fedJ9gp0oBYGB3eUYS 9x4Reh2KKXKXnIEOIYq2mAE2aD8aXZGsaFLCW1L78qL6HsIYFtiJLBjacf3ojZlN6vLi oaFg== X-Gm-Message-State: AOAM530inOFH1jEW+diZuHB2LaTYTgCpGBdARS4JEBXYUlI7M/YGfXdC A5MPJ9TVn3NcREfSf0cEEjYrMbk9xUpUcw== X-Google-Smtp-Source: ABdhPJwWPdi6OeKXSmKMFykVMvjDUVA8GgxUCUp9Z0Y36ZFOHGqGH1BKPRoVc6e1NSGMia5HVVE33A== X-Received: by 2002:a05:6808:11c4:b0:2d9:c395:f15e with SMTP id p4-20020a05680811c400b002d9c395f15emr12855746oiv.47.1654019583724; Tue, 31 May 2022 10:53:03 -0700 (PDT) Received: from birita.. ([2804:431:c7ca:e39c:980f:eaf1:7a30:10ef]) by smtp.gmail.com with ESMTPSA id f30-20020a4a8921000000b0035f627ac898sm6402626ooi.10.2022.05.31.10.53.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 May 2022 10:53:03 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v3 04/12] nptl: Use exit_lock when accessing TID on pthread_getaffinity_np Date: Tue, 31 May 2022 14:52:47 -0300 Message-Id: <20220531175255.1513396-5-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220531175255.1513396-1-adhemerval.zanella@linaro.org> References: <20220531175255.1513396-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.6 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, 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: 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" Also return EINVAL 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 | 30 ++++++++++------ sysdeps/pthread/Makefile | 1 + sysdeps/pthread/tst-pthread-exited.c | 51 ++++++++++++++++++++++++++++ 3 files changed, 71 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 c91fb37343..79f346326d 100644 --- a/nptl/pthread_getaffinity.c +++ b/nptl/pthread_getaffinity.c @@ -15,25 +15,33 @@ 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= 0; + if (pd->tid > 0) + res = INTERNAL_SYSCALL_CALL (sched_getaffinity, pd->tid, + MIN (INT_MAX, cpusetsize), cpuset); + else + res = -EINVAL; + + __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 8cebe7a784..6a1debaf30 100644 --- a/sysdeps/pthread/Makefile +++ b/sysdeps/pthread/Makefile @@ -126,6 +126,7 @@ tests += tst-cnd-basic tst-mtx-trylock tst-cnd-broadcast \ tst-pthread-raise-blocked-self \ 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..6fca37d7c2 --- /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, EINVAL); + } + + xpthread_join (thr); + + return 0; +} + +#include