From patchwork Tue May 31 17:52:49 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: 54585 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 028B9395A438 for ; Tue, 31 May 2022 17:57:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 028B9395A438 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1654019862; bh=fTffz/nOYoxij8w6mYEVd/+Yi7j2lHxs12f/agOLzic=; 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=x0E90jAYm0fkz3fPcGirj/VkAaVbkuewLWm+yLBLEHDooU2m7bRieqtb44+88w6Jg OSfuMJ7JhwSmRHSfOCAU58HJrA+pyMtzvxtoLUBuwBOo++2AAn0bsECYjq6Gdo9WLh BdKY2aYbmlTxjkAYps1P4BWQxEPo3esHgCa85URg= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ot1-x335.google.com (mail-ot1-x335.google.com [IPv6:2607:f8b0:4864:20::335]) by sourceware.org (Postfix) with ESMTPS id F05F1396D837 for ; Tue, 31 May 2022 17:53:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org F05F1396D837 Received: by mail-ot1-x335.google.com with SMTP id l9-20020a056830268900b006054381dd35so10080334otu.4 for ; Tue, 31 May 2022 10:53:06 -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=fTffz/nOYoxij8w6mYEVd/+Yi7j2lHxs12f/agOLzic=; b=KuXl7lz4aqBBsRX57KKbOOcspYaNaTRqAczt3dghfpMNpYOeWfO52LibaX+Uwg68fO dpezgFl25SQ50igYZVVgejqV3wwDbve/xDGhVNny0MJwAx+KWQQB3YVqCfRFrObUWCA0 NGUkMlA3g+8Ij+AqlhubmzAAr1Jou1jfYaCK5DTsEi2r5PMTqCHtzRCGxwGvXA2jxfpx UmDYSyWisIgebHsyI39PPULR6G93ncMwlgVD7PTw7qrJa9nrj2MBEUScfNCeAwZqBiBw MpehAFTgHu1X2n1vQnFhnFyFo+bX8ECsqN+Qn3pWA0oe0qTcVO3DLPTPelFftaSPlb5U S3fg== X-Gm-Message-State: AOAM530RYnAG6U/RNpnJfeHzBC7CIEghhCP0SejVY8hrhLqWGpVFPr+0 i2Vo0mrzyztydAJMrjg6sOQGYjJRpBQwRQ== X-Google-Smtp-Source: ABdhPJxluL95MIUgVWE/U/woEbXW2bn4p9HMSiGBlEC+MVCOSOi0EMcp1mLrsiVYxOnHJqXT/txwMg== X-Received: by 2002:a05:6830:2051:b0:60b:2111:6bb3 with SMTP id f17-20020a056830205100b0060b21116bb3mr14151819otp.93.1654019586096; Tue, 31 May 2022 10:53:06 -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.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 May 2022 10:53:05 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v3 06/12] nptl: Use exit_lock when accessing TID on pthread_setschedparam Date: Tue, 31 May 2022 14:52:49 -0300 Message-Id: <20220531175255.1513396-7-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" Also return EINVAL if the thread is already terminated at the time of the call. The sched_getparam call is also replaced with a INTERNAL_SYSCALL_CALL to avoid clobbering errno. Checked on x86_64-linux-gnu. --- nptl/pthread_setschedparam.c | 51 +++++++++++++++------------- sysdeps/pthread/tst-pthread-exited.c | 6 ++++ 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/nptl/pthread_setschedparam.c b/nptl/pthread_setschedparam.c index 5568ff140c..58511fcf94 100644 --- a/nptl/pthread_setschedparam.c +++ b/nptl/pthread_setschedparam.c @@ -15,26 +15,15 @@ License along with the GNU C Library; if not, see . */ -#include -#include -#include -#include "pthreadP.h" -#include +#include +#include +#include -int -__pthread_setschedparam (pthread_t threadid, int policy, - const struct sched_param *param) +static int +setschedparam (struct pthread *pd, int policy, + const 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; - /* See CREATE THREAD NOTES in nptl/pthread_create.c. */ lll_lock (pd->lock, LLL_PRIVATE); @@ -43,8 +32,7 @@ __pthread_setschedparam (pthread_t threadid, int policy, /* If the thread should have higher priority because of some PTHREAD_PRIO_PROTECT mutexes it holds, adjust the priority. */ - if (__builtin_expect (pd->tpp != NULL, 0) - && pd->tpp->priomax > param->sched_priority) + if (pd->tpp != NULL && pd->tpp->priomax > param->sched_priority) { p = *param; p.sched_priority = pd->tpp->priomax; @@ -52,10 +40,8 @@ __pthread_setschedparam (pthread_t threadid, int policy, } /* Try to set the scheduler information. */ - if (__builtin_expect (__sched_setscheduler (pd->tid, policy, - param) == -1, 0)) - result = errno; - else + int r = INTERNAL_SYSCALL_CALL (sched_setscheduler, pd->tid, policy, param); + if (r == 0) { /* We succeeded changing the kernel information. Reflect this change in the thread descriptor. */ @@ -66,6 +52,25 @@ __pthread_setschedparam (pthread_t threadid, int policy, lll_unlock (pd->lock, LLL_PRIVATE); + return -r; +} + +int +__pthread_setschedparam (pthread_t threadid, int policy, + const 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 ? setschedparam (pd, policy, param) : EINVAL; + + __libc_lock_unlock (pd->exit_lock); + __libc_signal_restore_set (&old_mask); + return result; } strong_alias (__pthread_setschedparam, pthread_setschedparam) diff --git a/sysdeps/pthread/tst-pthread-exited.c b/sysdeps/pthread/tst-pthread-exited.c index d350369563..94c5bc2bea 100644 --- a/sysdeps/pthread/tst-pthread-exited.c +++ b/sysdeps/pthread/tst-pthread-exited.c @@ -50,6 +50,12 @@ do_test (void) TEST_COMPARE (r, EINVAL); } + { + struct sched_param sch = { 0 }; + int r = pthread_setschedparam (thr, SCHED_FIFO, &sch); + TEST_COMPARE (r, EINVAL); + } + xpthread_join (thr); return 0;