]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
nptl: Fix PTHREAD_PRIO_PROTECT timed lock
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Wed, 25 Nov 2020 20:15:51 +0000 (17:15 -0300)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Fri, 27 Nov 2020 12:59:13 +0000 (09:59 -0300)
The 878fe624d4 changed lll_futex_timed_wait, which expects a relative
timeout, with a __futex_abstimed_wait64, which expects an absolute
timeout.  However the code still passes a relative timeout.

Also, the PTHREAD_PRIO_PROTECT support for clocks different than
CLOCK_REALTIME was broken since the inclusion of
pthread_mutex_clocklock (9d20e22e46) since lll_futex_timed_wait
always use CLOCK_REALTIME.

This patch fixes by removing the relative time calculation.  It
also adds some xtests that tests both thread and inter-process
usage.

Checked on x86_64-linux-gnu.

nptl/Makefile
nptl/pthread_mutex_timedlock.c
nptl/tst-mutexpp5.c [new file with mode: 0644]
nptl/tst-mutexpp9.c [new file with mode: 0644]
sysdeps/pthread/tst-mutex5.c
sysdeps/pthread/tst-mutex9.c

index a48426a39677f1c6a825c857e427f7cde3df9fde..94d805f0d49c00146b23a72614c4977bd5021ef7 100644 (file)
@@ -309,7 +309,8 @@ tests-internal := tst-robustpi8 tst-rwlock19 tst-rwlock20 \
                  tst-setgetname \
 
 xtests = tst-setuid1 tst-setuid1-static tst-setuid2 \
-       tst-mutexpp1 tst-mutexpp6 tst-mutexpp10 tst-setgroups
+       tst-mutexpp1 tst-mutexpp6 tst-mutexpp10 tst-setgroups \
+       tst-mutexpp5 tst-mutexpp9
 
 # This test can run into task limits because of a linux kernel bug
 # and then cause the make process to fail too, see bug 24537.
index aaaafa21ceff80c63f69cc2f98105aaf7d600d4a..74adffe790dad4c10902e36ecfdd73b0c641ee70 100644 (file)
@@ -547,30 +547,11 @@ __pthread_mutex_clocklock_common (pthread_mutex_t *mutex,
                        goto failpp;
                      }
 
-                   struct __timespec64 rt;
-
-                   /* Get the current time.  */
-                   __clock_gettime64 (CLOCK_REALTIME, &rt);
-
-                   /* Compute relative timeout.  */
-                   rt.tv_sec = abstime->tv_sec - rt.tv_sec;
-                   rt.tv_nsec = abstime->tv_nsec - rt.tv_nsec;
-                   if (rt.tv_nsec < 0)
-                     {
-                       rt.tv_nsec += 1000000000;
-                       --rt.tv_sec;
-                     }
-
-                   /* Already timed out?  */
-                   if (rt.tv_sec < 0)
-                     {
-                       result = ETIMEDOUT;
-                       goto failpp;
-                     }
-
-                   __futex_abstimed_wait64 (
-                     (unsigned int *) &mutex->__data.__lock, clockid,
-                     ceilval | 2, &rt, PTHREAD_MUTEX_PSHARED (mutex));
+                   int e = __futex_abstimed_wait64 (
+                     (unsigned int *) &mutex->__data.__lock, ceilval | 2,
+                     clockid, abstime, PTHREAD_MUTEX_PSHARED (mutex));
+                   if (e == ETIMEDOUT)
+                     return ETIMEDOUT;
                  }
              }
            while (atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
diff --git a/nptl/tst-mutexpp5.c b/nptl/tst-mutexpp5.c
new file mode 100644 (file)
index 0000000..a864a39
--- /dev/null
@@ -0,0 +1,2 @@
+#define ENABLE_PP 1
+#include "tst-mutex5.c"
diff --git a/nptl/tst-mutexpp9.c b/nptl/tst-mutexpp9.c
new file mode 100644 (file)
index 0000000..c848c74
--- /dev/null
@@ -0,0 +1,2 @@
+#define ENABLE_PP 1
+#include "tst-mutex9.c"
index bfe1a79fa4f064bfe2ed4fed77c8191b688379de..dbd2c3c15fbfe14ae298c485fb4e714888085489 100644 (file)
@@ -27,6 +27,9 @@
 #include <support/check.h>
 #include <support/timespec.h>
 
+#ifdef ENABLE_PP
+#include "tst-tpp.h"
+#endif
 
 #ifndef TYPE
 # define TYPE PTHREAD_MUTEX_NORMAL
@@ -47,8 +50,11 @@ do_test_clock (clockid_t clockid, const char *fnname)
   TEST_COMPARE (pthread_mutexattr_init (&a), 0);
   TEST_COMPARE (pthread_mutexattr_settype (&a, TYPE), 0);
 
-#ifdef ENABLE_PI
+#if defined ENABLE_PI
   TEST_COMPARE (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT), 0);
+#elif defined ENABLE_PP
+  TEST_COMPARE (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_PROTECT), 0);
+  TEST_COMPARE (pthread_mutexattr_setprioceiling (&a, 6), 0);
 #endif
 
   int err = pthread_mutex_init (&m, &a);
@@ -110,6 +116,10 @@ do_test_clock (clockid_t clockid, const char *fnname)
 
 static int do_test (void)
 {
+#ifdef ENABLE_PP
+  init_tpp_test ();
+#endif
+
   do_test_clock (CLOCK_USE_TIMEDLOCK, "timedlock");
   do_test_clock (CLOCK_REALTIME, "clocklock(realtime)");
 #ifndef ENABLE_PI
index bfc01f8c75b8f14210e6e6d519237aa61467cf14..081aeff0f6bd618ea5ea91b31e26b9db530b5745 100644 (file)
 #include <support/timespec.h>
 #include <support/xunistd.h>
 
+#ifdef ENABLE_PP
+#include "tst-tpp.h"
+#endif
+
 
 /* A bogus clock value that tells run_test to use pthread_mutex_timedlock
    rather than pthread_mutex_clocklock.  */
@@ -73,8 +77,11 @@ do_test_clock (clockid_t clockid)
 
   TEST_COMPARE (pthread_mutexattr_settype (&a, PTHREAD_MUTEX_RECURSIVE), 0);
 
-#ifdef ENABLE_PI
+#if defined ENABLE_PI
   TEST_COMPARE (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT), 0);
+#elif defined ENABLE_PP
+  TEST_COMPARE (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_PROTECT), 0);
+  TEST_COMPARE (pthread_mutexattr_setprioceiling (&a, 6), 0);
 #endif
 
   int e;
@@ -131,6 +138,10 @@ do_test_clock (clockid_t clockid)
 static int
 do_test (void)
 {
+#ifdef ENABLE_PP
+  init_tpp_test ();
+#endif
+
   do_test_clock (CLOCK_USE_TIMEDLOCK);
   do_test_clock (CLOCK_REALTIME);
 #ifndef ENABLE_PI