From: Adhemerval Zanella Date: Mon, 23 Aug 2021 17:48:30 +0000 (-0300) Subject: nptl: Use exit_lock when accessing TID on pthread_setschedparam X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f303d285c87a8bd85d857cae065e5b57449ec74b;p=thirdparty%2Fglibc.git nptl: Use exit_lock when accessing TID on pthread_setschedparam 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. --- diff --git a/nptl/pthread_setschedparam.c b/nptl/pthread_setschedparam.c index 1b43eb1f71..02933438de 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. */ + internal_sigset_t old_mask; + internal_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); + internal_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 c544ebc6be..b768685771 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;