From patchwork Tue May 31 17:52:50 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: 54586 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 020F1395B455 for ; Tue, 31 May 2022 17:58:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 020F1395B455 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1654019904; bh=jpzzlkMiG6nbY+ncdMPpxk9xsE9wbJHOWwoMDtxC8To=; 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=J45egk9ObdfcGlkByhX4AFVBXy3UbLP7OqooKxU4Sj9Xf2RI1nL4IMrpRKRjM+uvJ aKM6nr/7CoRhIf4gd/r6CQm/bQQYWhQrY2b7bsqkbg+O1skzqXe0oeJP1dyIN8TWRL asBesvnQKxokVLLjc8iO/A7ZTyncLaxGLwza1r6I= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ot1-x32c.google.com (mail-ot1-x32c.google.com [IPv6:2607:f8b0:4864:20::32c]) by sourceware.org (Postfix) with ESMTPS id 32FC038376F8 for ; Tue, 31 May 2022 17:53:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 32FC038376F8 Received: by mail-ot1-x32c.google.com with SMTP id 61-20020a9d0bc3000000b0060b9bfcfe76so2680131oth.9 for ; Tue, 31 May 2022 10:53:08 -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=jpzzlkMiG6nbY+ncdMPpxk9xsE9wbJHOWwoMDtxC8To=; b=V0nIIwq8SF1U2BswHtxkmcaKUyQEOQelt+2Botv+dVykAgGuN2Fzj4tCF7O8pugbl8 OtPBbZiBuCoaJ/4rpu0xKxf8Zhs/bEFdedbetHwWSBZzYeqr/lDIvLbcON2aVQpQPzAE 6DeSqxF/IwSOoCD/20pdQ4t1SJU/oY8v9Lyn1ClKGivM8sNm5Pvzwu33ShQT4cT1YimZ R7FtMTfoSy4DG/CeSzcgV7DO/CAezTX/CKfWeF+UMzf4+phqn4kVm7j9hYOoOqgoC8Hm Bx1iBsM7UZZWkqYXSVh1YkFLOEIH6OQPxPAYk3Fqs5DUzUhjtT0Pn1u++mahodvFSF0J wfWw== X-Gm-Message-State: AOAM533k5QsEEiEbdPYw+JnDJno2uaTsT3TFRfS52McBf5Fwmk+G3Yt/ P5PGm+4EhQhYOtrkfcsDWkts2vwyB3x9fA== X-Google-Smtp-Source: ABdhPJzhWi//aCrws4yIYst78Loi4H2iHSz8iNoYTkeoEhEn2ZbK1TsBXa31FLoXobloN9o7UiwYzQ== X-Received: by 2002:a05:6830:1dd3:b0:60b:1e56:f1f5 with SMTP id a19-20020a0568301dd300b0060b1e56f1f5mr15171141otj.280.1654019587279; Tue, 31 May 2022 10:53:07 -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.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 May 2022 10:53:07 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v3 07/12] nptl: Use exit_lock when accessing TID on pthread_getschedparam Date: Tue, 31 May 2022 14:52:50 -0300 Message-Id: <20220531175255.1513396-8-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.7 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" The sched_getparam call is also replaced with a INTERNAL_SYSCALL_CALL to avoid clobbering errno. Also return EINVAL if the thread is already terminated at the time of the call. Checked on x86_64-linux-gnu. --- nptl/pthread_getschedparam.c | 55 +++++++++++++++------------- sysdeps/pthread/tst-pthread-exited.c | 7 ++++ 2 files changed, 37 insertions(+), 25 deletions(-) diff --git a/nptl/pthread_getschedparam.c b/nptl/pthread_getschedparam.c index 11a34e4af7..bd0264f0fa 100644 --- a/nptl/pthread_getschedparam.c +++ b/nptl/pthread_getschedparam.c @@ -15,24 +15,14 @@ License along with the GNU C Library; if not, see . */ -#include -#include -#include "pthreadP.h" -#include +#include +#include +#include - -int -__pthread_getschedparam (pthread_t threadid, int *policy, - struct sched_param *param) +static int +getschedparam (struct pthread *pd, int *policy, struct sched_param *param) { - struct pthread *pd = (struct pthread *) threadid; - - /* Make sure the descriptor is valid. */ - if (INVALID_TD_P (pd)) - /* Not a valid thread handle. */ - return ESRCH; - - int result = 0; + int r = 0; /* See CREATE THREAD NOTES in nptl/pthread_create.c. */ lll_lock (pd->lock, LLL_PRIVATE); @@ -44,22 +34,18 @@ __pthread_getschedparam (pthread_t threadid, int *policy, not yet been retrieved do it now. */ if ((pd->flags & ATTR_FLAG_SCHED_SET) == 0) { - if (__sched_getparam (pd->tid, &pd->schedparam) != 0) - result = 1; - else + r = INTERNAL_SYSCALL_CALL (sched_getparam, pd->tid, &pd->schedparam); + if (r == 0) pd->flags |= ATTR_FLAG_SCHED_SET; } - if ((pd->flags & ATTR_FLAG_POLICY_SET) == 0) { - pd->schedpolicy = __sched_getscheduler (pd->tid); - if (pd->schedpolicy == -1) - result = 1; - else + r = pd->schedpolicy = INTERNAL_SYSCALL_CALL (sched_getscheduler, pd->tid); + if (r >= 0) pd->flags |= ATTR_FLAG_POLICY_SET; } - if (result == 0) + if (r >= 0) { *policy = pd->schedpolicy; memcpy (param, &pd->schedparam, sizeof (struct sched_param)); @@ -67,6 +53,25 @@ __pthread_getschedparam (pthread_t threadid, int *policy, lll_unlock (pd->lock, LLL_PRIVATE); + return -r; +} + +int +__pthread_getschedparam (pthread_t threadid, int *policy, + struct sched_param *param) +{ + struct pthread *pd = (struct pthread *) threadid; + + /* 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 result = pd->tid != 0 ? getschedparam (pd, policy, param) : EINVAL; + + __libc_lock_unlock (pd->exit_lock); + __libc_signal_restore_set (&old_mask); + return result; } libc_hidden_def (__pthread_getschedparam) diff --git a/sysdeps/pthread/tst-pthread-exited.c b/sysdeps/pthread/tst-pthread-exited.c index 94c5bc2bea..9fdbdfddc7 100644 --- a/sysdeps/pthread/tst-pthread-exited.c +++ b/sysdeps/pthread/tst-pthread-exited.c @@ -56,6 +56,13 @@ do_test (void) TEST_COMPARE (r, EINVAL); } + { + struct sched_param sch; + int policy; + int r = pthread_getschedparam (thr, &policy, &sch); + TEST_COMPARE (r, EINVAL); + } + xpthread_join (thr); return 0;