[09/15] nptl: Use exit_lock when accessing TID on pthread_setschedparam

Message ID 20210930200051.1017457-10-adhemerval.zanella@linaro.org
State New
Headers show
Series
  • Fix various NPTL synchronization issues
Related show

Commit Message

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(-)

-- 
2.30.2

Patch

diff --git a/nptl/pthread_setschedparam.c b/nptl/pthread_setschedparam.c
index f38bd788af..d6027824db 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
    <https://www.gnu.org/licenses/>.  */
 
-#include <errno.h>
-#include <sched.h>
-#include <string.h>
-#include "pthreadP.h"
-#include <lowlevellock.h>
+#include <libc-lock.h>
+#include <kernel-posix-cpu-timers.h>
+#include <pthreadP.h>
 
 
-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) : ESRCH;
+
+  __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 d33e6b28fe..208546a546 100644
--- a/sysdeps/pthread/tst-pthread-exited.c
+++ b/sysdeps/pthread/tst-pthread-exited.c
@@ -57,6 +57,12 @@  do_test (void)
     TEST_COMPARE (r, ESRCH);
   }
 
+  {
+    struct sched_param sch = { 0 };
+    int r = pthread_setschedparam (thr, SCHED_FIFO, &sch);
+    TEST_COMPARE (r, ESRCH);
+  }
+
   xpthread_join (thr);
 
   return 0;