From: Adhemerval Zanella Date: Thu, 9 Sep 2021 20:48:56 +0000 (-0300) Subject: nptl: Use exit_lock when accessing TID on pthread_setschedprio X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=846c2b23e2f0fa1315d008927c9dd6e5946f3798;p=thirdparty%2Fglibc.git nptl: Use exit_lock when accessing TID on pthread_setschedprio Also return EINVAL if the thread is already terminated at the time of the call. Checked on x86_64-linux-gnu --- diff --git a/nptl/pthread_setschedprio.c b/nptl/pthread_setschedprio.c index c355716593..da16a63395 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. */ + internal_sigset_t old_mask; + internal_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); + internal_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 ec8403eeb7..1faea86796 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;