From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oi1-x230.google.com (mail-oi1-x230.google.com [IPv6:2607:f8b0:4864:20::230]) by sourceware.org (Postfix) with ESMTPS id 56A0038560B3 for ; Tue, 31 May 2022 17:53:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 56A0038560B3 Received: by mail-oi1-x230.google.com with SMTP id r206so11181638oib.8 for ; Tue, 31 May 2022 10:53:12 -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=JwB8OcyhdmxDhPw3A1rKhszwr43N5h8sVg+KMkP+EQI=; b=EtRAOWiI6zzJQ6CDbytljphUaecLUxE0QDV/TNjus3R+kTO3Hz6oG3qGOqWa5zpN6W Mt5LpnHpotJR9M0rAD1yTcCzAYQIp0nr+Yg25x7EIFPUHvRBKFvcNFMd4TauDUOGC0ki F0GBI+fYk7tB0LdEoscVsJXQ1SIc0vA/AU5fg2mh99pemohxxMGzPgzLfe1IYvNOlyw+ 3+N7NoTTJ6DKAtcnf8qoOEYIJUrlkxmtV5vtOJkHYigCYNJqyR0eyXA+32EAyvJvJl7w 8yvRAiOlWI5N90tSDNZe4DjOwZTg3cRcoYZJ7WIdYtugKuIe7l/6r2U3sNTOVcJuRdu4 P+/A== X-Gm-Message-State: AOAM530BY2CXsmluhG3CcCfUf6IvF+rVw2u3qpPmG3wc4r3BiFHxPYH4 yTa2rearcSS6YHt0hT2aUISxhWjTcxVZIg== X-Google-Smtp-Source: ABdhPJwmtVPPNuCA2rbq3+4RVz/gypq/VU7UzVV72vO37F+qbZBW7RdW2J+CIufBAHagttNJriUg9A== X-Received: by 2002:a05:6808:3010:b0:2f7:3e71:88b2 with SMTP id ay16-20020a056808301000b002f73e7188b2mr12821493oib.102.1654019591910; Tue, 31 May 2022 10:53:11 -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.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 May 2022 10:53:11 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH v3 11/12] nptl: Use exit_lock when accessing TID on pthread_setschedprio Date: Tue, 31 May 2022 14:52:54 -0300 Message-Id: <20220531175255.1513396-12-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 Content-Transfer-Encoding: 8bit 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-List-Received-Date: Tue, 31 May 2022 17:53:13 -0000 Also return EINVAL if the thread is already terminated at the time of the call. Checked on x86_64-linux-gnu --- nptl/pthread_setschedprio.c | 45 +++++++++++++++------------- sysdeps/pthread/tst-pthread-exited.c | 5 ++++ 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/nptl/pthread_setschedprio.c b/nptl/pthread_setschedprio.c index 85f68d6f2d..8603624d80 100644 --- a/nptl/pthread_setschedprio.c +++ b/nptl/pthread_setschedprio.c @@ -15,25 +15,13 @@ License along with the GNU C Library; if not, see . */ -#include -#include -#include -#include -#include "pthreadP.h" -#include +#include +#include #include -int -__pthread_setschedprio (pthread_t threadid, int prio) +static int +setschedprio (struct pthread *pd, int prio) { - 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; struct sched_param param; param.sched_priority = prio; @@ -42,13 +30,12 @@ __pthread_setschedprio (pthread_t threadid, int prio) /* 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 > prio) + if (pd->tpp != NULL && pd->tpp->priomax > prio) param.sched_priority = pd->tpp->priomax; /* Try to set the scheduler information. */ - if (__glibc_unlikely (__sched_setparam (pd->tid, ¶m) == -1)) - result = errno; - else + int r = INTERNAL_SYSCALL_CALL (sched_setparam, pd->tid, ¶m); + if (r == 0) { /* We succeeded changing the kernel information. Reflect this change in the thread descriptor. */ @@ -59,6 +46,24 @@ __pthread_setschedprio (pthread_t threadid, int prio) lll_unlock (pd->lock, LLL_PRIVATE); + return -r; +} + +int +__pthread_setschedprio (pthread_t threadid, int prio) +{ + 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 ? setschedprio (pd, prio) : EINVAL; + + __libc_lock_unlock (pd->exit_lock); + __libc_signal_restore_set (&old_mask); + return result; } versioned_symbol (libc, __pthread_setschedprio, pthread_setschedprio, diff --git a/sysdeps/pthread/tst-pthread-exited.c b/sysdeps/pthread/tst-pthread-exited.c index c4d8492f8c..ed4006c829 100644 --- a/sysdeps/pthread/tst-pthread-exited.c +++ b/sysdeps/pthread/tst-pthread-exited.c @@ -81,6 +81,11 @@ do_test (void) TEST_COMPARE (r, EINVAL); } + { + int r = pthread_setschedprio (thr, 0); + TEST_COMPARE (r, EINVAL); + } + xpthread_join (thr); return 0; -- 2.34.1