unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v3 0/4] Add CLOCK_MONOTONIC support for PI mutexes
@ 2021-09-06 20:47 Adhemerval Zanella via Libc-alpha
  2021-09-06 20:47 ` [PATCH v3 1/4] Linux: Add FUTEX_LOCK_PI2 Adhemerval Zanella via Libc-alpha
                   ` (4 more replies)
  0 siblings, 5 replies; 7+ messages in thread
From: Adhemerval Zanella via Libc-alpha @ 2021-09-06 20:47 UTC (permalink / raw)
  To: libc-alpha

It adds support for CLOCK_MONOTONIC for PI mutexes by using the new
Linux v5.14 FUTEX_LOCK_PI2 operation.  Similar to current approach,
pthread_mutex_clocklock might return EINVAL if the underlying kernel
has no support for the futex operation.

Changes since v2:

  * Renamed xpthread_mutex_pi_support_monotonic to
    support_mutex_pi_monotonic.

Changes since v1:
  
  * Refactor futex_lock_pi64 and avoid adding futex_lock_pi2.  The
    FUTEX_LOCK_PI2 logic is done at only one place, so the code change
    on pthread routines are minimal.  Also, FUTEX_LOCK_PI2 is only
    used if it is really required, i.e, when a timeout against
    CLOCK_MONOTONIC is used.
  * Do not remove nptl/tst-mutexpi10.c, but instead adjust it to new
    FUTEX_LOCK_PI2 support.
  * Add pthread_mutex_pi_support_monotonic() to check if monotonic
    clock with PI mutexes are supported and use on test to check for
    the expected result.

Adhemerval Zanella (3):
  nptl: Use FUTEX_LOCK_PI2 when available
  support: Add support_mutex_pi_monotonic
  nptl: Add CLOCK_MONOTONIC support for PI mutexes

Kurt Kanzenbach (1):
  Linux: Add FUTEX_LOCK_PI2

 nptl/futex-internal.c                     | 63 +++++++++++++++++++++++
 nptl/pthread_mutex_lock.c                 |  3 +-
 nptl/pthread_mutex_timedlock.c            | 10 +---
 nptl/tst-mutexpi10.c                      | 42 +++++++++------
 support/Makefile                          |  1 +
 support/support_mutex_pi_monotonic.c      | 33 ++++++++++++
 support/xthread.h                         |  4 ++
 sysdeps/nptl/futex-internal.h             | 58 ++-------------------
 sysdeps/nptl/lowlevellock-futex.h         |  1 +
 sysdeps/pthread/tst-mutex5.c              | 23 ++++++---
 sysdeps/pthread/tst-mutex9.c              | 20 ++++---
 sysdeps/unix/sysv/linux/kernel-features.h |  8 +++
 12 files changed, 172 insertions(+), 94 deletions(-)
 create mode 100644 support/support_mutex_pi_monotonic.c

-- 
2.30.2


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v3 1/4] Linux: Add FUTEX_LOCK_PI2
  2021-09-06 20:47 [PATCH v3 0/4] Add CLOCK_MONOTONIC support for PI mutexes Adhemerval Zanella via Libc-alpha
@ 2021-09-06 20:47 ` Adhemerval Zanella via Libc-alpha
  2021-09-06 20:47 ` [PATCH v3 2/4] nptl: Use FUTEX_LOCK_PI2 when available Adhemerval Zanella via Libc-alpha
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Adhemerval Zanella via Libc-alpha @ 2021-09-06 20:47 UTC (permalink / raw)
  To: libc-alpha; +Cc: Kurt Kanzenbach

From: Kurt Kanzenbach <kurt@linutronix.de>

Linux v5.14.0 introduced a new futex operation called FUTEX_LOCK_PI2.

This kernel feature can be used to implement
pthread_mutex_clocklock(MONOTONIC)/PI.

Signed-off-by: Kurt Kanzenbach <kurt@linutronix.de>
Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
---
 sysdeps/unix/sysv/linux/kernel-features.h | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/sysdeps/unix/sysv/linux/kernel-features.h b/sysdeps/unix/sysv/linux/kernel-features.h
index 3930e14322..ffb6af196b 100644
--- a/sysdeps/unix/sysv/linux/kernel-features.h
+++ b/sysdeps/unix/sysv/linux/kernel-features.h
@@ -220,4 +220,12 @@
 # define __ASSUME_FACCESSAT2 0
 #endif
 
+/* The FUTEX_LOCK_PI2 operation was introduced across all architectures in Linux
+   5.14.  */
+#if __LINUX_KERNEL_VERSION >= 0x050e00
+# define __ASSUME_FUTEX_LOCK_PI2 1
+#else
+# define __ASSUME_FUTEX_LOCK_PI2 0
+#endif
+
 #endif /* kernel-features.h */
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v3 2/4] nptl: Use FUTEX_LOCK_PI2 when available
  2021-09-06 20:47 [PATCH v3 0/4] Add CLOCK_MONOTONIC support for PI mutexes Adhemerval Zanella via Libc-alpha
  2021-09-06 20:47 ` [PATCH v3 1/4] Linux: Add FUTEX_LOCK_PI2 Adhemerval Zanella via Libc-alpha
@ 2021-09-06 20:47 ` Adhemerval Zanella via Libc-alpha
  2021-09-06 20:47 ` [PATCH v3 3/4] support: Add support_mutex_pi_monotonic Adhemerval Zanella via Libc-alpha
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Adhemerval Zanella via Libc-alpha @ 2021-09-06 20:47 UTC (permalink / raw)
  To: libc-alpha; +Cc: Kurt Kanzenbach

This patch uses the new futex PI operation provided by Linux v5.14
when it is required.

The futex_lock_pi64() is moved to futex-internal.c (since it used on
two different places and its code size might be large depending of the
kernel configuration) and clockid is added as an argument.

Co-authored-by: Kurt Kanzenbach <kurt@linutronix.de>
---
 nptl/futex-internal.c             | 63 +++++++++++++++++++++++++++++++
 nptl/pthread_mutex_lock.c         |  3 +-
 nptl/pthread_mutex_timedlock.c    |  3 +-
 sysdeps/nptl/futex-internal.h     | 58 ++--------------------------
 sysdeps/nptl/lowlevellock-futex.h |  1 +
 5 files changed, 72 insertions(+), 56 deletions(-)

diff --git a/nptl/futex-internal.c b/nptl/futex-internal.c
index e74647a9d4..58605b2fca 100644
--- a/nptl/futex-internal.c
+++ b/nptl/futex-internal.c
@@ -140,3 +140,66 @@ __futex_abstimed_wait_cancelable64 (unsigned int* futex_word,
                                        abstime, private, true);
 }
 libc_hidden_def (__futex_abstimed_wait_cancelable64)
+
+int
+__futex_lock_pi64 (int *futex_word, clockid_t clockid,
+		   const struct __timespec64 *abstime, int private)
+{
+  int err;
+
+  unsigned int clockbit = clockid == CLOCK_REALTIME
+			  ? FUTEX_CLOCK_REALTIME : 0;
+  int op_pi2 = __lll_private_flag (FUTEX_LOCK_PI2 | clockbit, private);
+#if __ASSUME_FUTEX_LOCK_PI2
+  /* Assume __ASSUME_TIME64_SYSCALLS since FUTEX_LOCK_PI2 was added later.  */
+  err = INTERNAL_SYSCALL_CALL (futex_time64, futex_word, op_pi2, 0, abstime);
+#else
+  /* FUTEX_LOCK_PI does not support clock selection, so for CLOCK_MONOTONIC
+     the only option is to use FUTEX_LOCK_PI2.  */
+  int op_pi1 = __lll_private_flag (FUTEX_LOCK_PI, private);
+  int op_pi = abstime != NULL && clockid != CLOCK_REALTIME ? op_pi2 : op_pi1;
+
+# ifdef __ASSUME_TIME64_SYSCALLS
+  err = INTERNAL_SYSCALL_CALL (futex_time64, futex_word, op_pi, 0, abstime);
+# else
+  bool need_time64 = abstime != NULL && !in_time_t_range (abstime->tv_sec);
+  if (need_time64)
+    err = INTERNAL_SYSCALL_CALL (futex_time64, futex_word, op_pi, 0, abstime);
+  else
+    {
+      struct timespec ts32, *pts32 = NULL;
+      if (abstime != NULL)
+	{
+	  ts32 = valid_timespec64_to_timespec (*abstime);
+	  pts32 = &ts32;
+	}
+      err = INTERNAL_SYSCALL_CALL (futex, futex_word, op_pi, 0, pts32);
+    }
+# endif	 /* __ASSUME_TIME64_SYSCALLS */
+   /* FUTEX_LOCK_PI2 is not available on this kernel.  */
+   if (err == -ENOSYS)
+     err = -EINVAL;
+#endif /* __ASSUME_FUTEX_LOCK_PI2  */
+
+  switch (err)
+    {
+    case 0:
+    case -EAGAIN:
+    case -EINTR:
+    case -ETIMEDOUT:
+    case -ESRCH:
+    case -EDEADLK:
+    case -EINVAL: /* This indicates either state corruption or that the kernel
+                     found a waiter on futex address which is waiting via
+                     FUTEX_WAIT or FUTEX_WAIT_BITSET.  This is reported on
+                     some futex_lock_pi usage (pthread_mutex_timedlock for
+                     instance).  */
+      return -err;
+
+    case -EFAULT: /* Must have been caused by a glibc or application bug.  */
+    case -ENOSYS: /* Must have been caused by a glibc bug.  */
+    /* No other errors are documented at this time.  */
+    default:
+      futex_fatal_error ();
+    }
+}
diff --git a/nptl/pthread_mutex_lock.c b/nptl/pthread_mutex_lock.c
index fbe8a3cd21..2bd41767e0 100644
--- a/nptl/pthread_mutex_lock.c
+++ b/nptl/pthread_mutex_lock.c
@@ -421,7 +421,8 @@ __pthread_mutex_lock_full (pthread_mutex_t *mutex)
 	    int private = (robust
 			   ? PTHREAD_ROBUST_MUTEX_PSHARED (mutex)
 			   : PTHREAD_MUTEX_PSHARED (mutex));
-	    int e = futex_lock_pi64 (&mutex->__data.__lock, NULL, private);
+	    int e = __futex_lock_pi64 (&mutex->__data.__lock, 0 /* ununsed  */,
+				       NULL, private);
 	    if (e == ESRCH || e == EDEADLK)
 	      {
 		assert (e != EDEADLK
diff --git a/nptl/pthread_mutex_timedlock.c b/nptl/pthread_mutex_timedlock.c
index bf2af6b048..a695faeb7e 100644
--- a/nptl/pthread_mutex_timedlock.c
+++ b/nptl/pthread_mutex_timedlock.c
@@ -369,7 +369,8 @@ __pthread_mutex_clocklock_common (pthread_mutex_t *mutex,
 	    int private = (robust
 			   ? PTHREAD_ROBUST_MUTEX_PSHARED (mutex)
 			   : PTHREAD_MUTEX_PSHARED (mutex));
-	    int e = futex_lock_pi64 (&mutex->__data.__lock, abstime, private);
+	    int e = __futex_lock_pi64 (&mutex->__data.__lock, clockid, abstime,
+				       private);
 	    if (e == ETIMEDOUT)
 	      return ETIMEDOUT;
 	    else if (e == ESRCH || e == EDEADLK)
diff --git a/sysdeps/nptl/futex-internal.h b/sysdeps/nptl/futex-internal.h
index 79a366604d..a46730cad6 100644
--- a/sysdeps/nptl/futex-internal.h
+++ b/sysdeps/nptl/futex-internal.h
@@ -236,8 +236,8 @@ futex_wake (unsigned int* futex_word, int processes_to_wake, int private)
    are done in descending priority order.
 
    The ABSTIME arguments provides an absolute timeout (measured against the
-   CLOCK_REALTIME clock).  If TIMEOUT is NULL, the operation will block
-   indefinitely.
+   CLOCK_REALTIME or CLOCK_MONOTONIC clock).  If TIMEOUT is NULL, the operation
+   will block indefinitely.
 
    Returns:
 
@@ -250,58 +250,8 @@ futex_wake (unsigned int* futex_word, int processes_to_wake, int private)
        futex.
      - ETIMEDOUT if the ABSTIME expires.
 */
-static __always_inline int
-futex_lock_pi64 (int *futex_word, const struct __timespec64 *abstime,
-                 int private)
-{
-  int err;
-#ifdef __ASSUME_TIME64_SYSCALLS
-  err = INTERNAL_SYSCALL_CALL (futex_time64, futex_word,
-			       __lll_private_flag (FUTEX_LOCK_PI, private), 0,
-			       abstime);
-#else
-  bool need_time64 = abstime != NULL && !in_time_t_range (abstime->tv_sec);
-  if (need_time64)
-    {
-      err = INTERNAL_SYSCALL_CALL (futex_time64, futex_word,
-				   __lll_private_flag (FUTEX_LOCK_PI, private),
-				   0, abstime);
-      if (err == -ENOSYS)
-	err = -EOVERFLOW;
-    }
-  else
-    {
-      struct timespec ts32;
-      if (abstime != NULL)
-        ts32 = valid_timespec64_to_timespec (*abstime);
-
-      err = INTERNAL_SYSCALL_CALL (futex, futex_word, __lll_private_flag
-                                   (FUTEX_LOCK_PI, private), 0,
-                                   abstime != NULL ? &ts32 : NULL);
-    }
-#endif
-  switch (err)
-    {
-    case 0:
-    case -EAGAIN:
-    case -EINTR:
-    case -ETIMEDOUT:
-    case -ESRCH:
-    case -EDEADLK:
-    case -EINVAL: /* This indicates either state corruption or that the kernel
-		     found a waiter on futex address which is waiting via
-		     FUTEX_WAIT or FUTEX_WAIT_BITSET.  This is reported on
-		     some futex_lock_pi usage (pthread_mutex_timedlock for
-		     instance).  */
-      return -err;
-
-    case -EFAULT: /* Must have been caused by a glibc or application bug.  */
-    case -ENOSYS: /* Must have been caused by a glibc bug.  */
-    /* No other errors are documented at this time.  */
-    default:
-      futex_fatal_error ();
-    }
-}
+int __futex_lock_pi64 (int *futex_word, clockid_t clockid,
+		       const struct __timespec64 *abstime, int private);
 
 /* Wakes the top priority waiter that called a futex_lock_pi operation on
    the futex.
diff --git a/sysdeps/nptl/lowlevellock-futex.h b/sysdeps/nptl/lowlevellock-futex.h
index 66ebfe50f4..abda179e0d 100644
--- a/sysdeps/nptl/lowlevellock-futex.h
+++ b/sysdeps/nptl/lowlevellock-futex.h
@@ -38,6 +38,7 @@
 #define FUTEX_WAKE_BITSET	10
 #define FUTEX_WAIT_REQUEUE_PI   11
 #define FUTEX_CMP_REQUEUE_PI    12
+#define FUTEX_LOCK_PI2		13
 #define FUTEX_PRIVATE_FLAG	128
 #define FUTEX_CLOCK_REALTIME	256
 
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v3 3/4] support: Add support_mutex_pi_monotonic
  2021-09-06 20:47 [PATCH v3 0/4] Add CLOCK_MONOTONIC support for PI mutexes Adhemerval Zanella via Libc-alpha
  2021-09-06 20:47 ` [PATCH v3 1/4] Linux: Add FUTEX_LOCK_PI2 Adhemerval Zanella via Libc-alpha
  2021-09-06 20:47 ` [PATCH v3 2/4] nptl: Use FUTEX_LOCK_PI2 when available Adhemerval Zanella via Libc-alpha
@ 2021-09-06 20:47 ` Adhemerval Zanella via Libc-alpha
  2021-09-06 20:47 ` [PATCH v3 4/4] nptl: Add CLOCK_MONOTONIC support for PI mutexes Adhemerval Zanella via Libc-alpha
  2021-09-27 13:48 ` [PATCH v3 0/4] " Adhemerval Zanella via Libc-alpha
  4 siblings, 0 replies; 7+ messages in thread
From: Adhemerval Zanella via Libc-alpha @ 2021-09-06 20:47 UTC (permalink / raw)
  To: libc-alpha

Returns true if Priority Inheritance support CLOCK_MONOTONIC.
---
 support/Makefile                     |  1 +
 support/support_mutex_pi_monotonic.c | 33 ++++++++++++++++++++++++++++
 support/xthread.h                    |  4 ++++
 3 files changed, 38 insertions(+)
 create mode 100644 support/support_mutex_pi_monotonic.c

diff --git a/support/Makefile b/support/Makefile
index 2a0731796f..fd27c8451e 100644
--- a/support/Makefile
+++ b/support/Makefile
@@ -63,6 +63,7 @@ libsupport-routines = \
   support_format_hostent \
   support_format_netent \
   support_isolate_in_subprocess \
+  support_mutex_pi_monotonic \
   support_path_support_time64 \
   support_process_state \
   support_ptrace \
diff --git a/support/support_mutex_pi_monotonic.c b/support/support_mutex_pi_monotonic.c
new file mode 100644
index 0000000000..5535837f92
--- /dev/null
+++ b/support/support_mutex_pi_monotonic.c
@@ -0,0 +1,33 @@
+/* Returns whether Priority Inheritance support CLOCK_MONOTONIC.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <support/xthread.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+bool
+support_mutex_pi_monotonic (void)
+{
+#ifdef __linux__
+  int r = syscall (__NR_futex, &(unsigned int){0}, 13 /* FUTEX_LOCK_PI2  */,
+		   0, NULL);
+  return r == 0;
+#else
+  return false;
+#endif
+}
diff --git a/support/xthread.h b/support/xthread.h
index a4a4ec5b1e..30501f5772 100644
--- a/support/xthread.h
+++ b/support/xthread.h
@@ -21,6 +21,7 @@
 
 #include <pthread.h>
 #include <sys/cdefs.h>
+#include <stdbool.h>
 
 __BEGIN_DECLS
 
@@ -29,6 +30,9 @@ __BEGIN_DECLS
    exit function, so atexit handlers are executed.  */
 void delayed_exit (int seconds);
 
+/* Returns true if Priority Inheritance support CLOCK_MONOTONIC.  */
+bool support_mutex_pi_monotonic (void);
+
 /* Terminate the process (with exit status 1) if VALUE is not zero.
    In that case, print a failure message to standard output mentioning
    FUNCTION.  The process is terminated with the exit function, so
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v3 4/4] nptl: Add CLOCK_MONOTONIC support for PI mutexes
  2021-09-06 20:47 [PATCH v3 0/4] Add CLOCK_MONOTONIC support for PI mutexes Adhemerval Zanella via Libc-alpha
                   ` (2 preceding siblings ...)
  2021-09-06 20:47 ` [PATCH v3 3/4] support: Add support_mutex_pi_monotonic Adhemerval Zanella via Libc-alpha
@ 2021-09-06 20:47 ` Adhemerval Zanella via Libc-alpha
  2021-09-27 13:48 ` [PATCH v3 0/4] " Adhemerval Zanella via Libc-alpha
  4 siblings, 0 replies; 7+ messages in thread
From: Adhemerval Zanella via Libc-alpha @ 2021-09-06 20:47 UTC (permalink / raw)
  To: libc-alpha

Linux added FUTEX_LOCK_PI2 to support clock selection
(commit bf22a6976897977b0a3f1aeba6823c959fc4fdae).  With the new
flag we can now proper support CLOCK_MONOTONIC for
pthread_mutex_clocklock with Priority Inheritance.  If kernel
does not support, EINVAL is returned instead.

The difference is the futex operation will be issued and the kernel
will advertise the missing support (instead of hard-code error
return).

Checked on x86_64-linux-gnu and i686-linux-gnu on Linux 5.14, 5.11,
and 4.15.
---
 nptl/pthread_mutex_timedlock.c |  7 ------
 nptl/tst-mutexpi10.c           | 42 +++++++++++++++++++++-------------
 sysdeps/pthread/tst-mutex5.c   | 23 ++++++++++++-------
 sysdeps/pthread/tst-mutex9.c   | 20 ++++++++++------
 4 files changed, 54 insertions(+), 38 deletions(-)

diff --git a/nptl/pthread_mutex_timedlock.c b/nptl/pthread_mutex_timedlock.c
index a695faeb7e..57f3f28869 100644
--- a/nptl/pthread_mutex_timedlock.c
+++ b/nptl/pthread_mutex_timedlock.c
@@ -299,13 +299,6 @@ __pthread_mutex_clocklock_common (pthread_mutex_t *mutex,
     case PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP:
     case PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP:
       {
-	/* Currently futex FUTEX_LOCK_PI operation only provides support for
-	   CLOCK_REALTIME and trying to emulate by converting a
-	   CLOCK_MONOTONIC to CLOCK_REALTIME will take in account possible
-	   changes to the wall clock.  */
-	if (__glibc_unlikely (clockid != CLOCK_REALTIME))
-	  return EINVAL;
-
 	int kind, robust;
 	{
 	  /* See concurrency notes regarding __kind in struct __pthread_mutex_s
diff --git a/nptl/tst-mutexpi10.c b/nptl/tst-mutexpi10.c
index da781d0d7a..34a7bea21b 100644
--- a/nptl/tst-mutexpi10.c
+++ b/nptl/tst-mutexpi10.c
@@ -38,29 +38,39 @@ do_test (void)
     PTHREAD_MUTEX_STALLED,
     PTHREAD_MUTEX_ROBUST
   };
-
+  const struct {
+    int clk;
+    int r;
+  } clocks[] = {
+    { CLOCK_REALTIME,         0 },
+    { CLOCK_MONOTONIC,        0 },
+    { CLOCK_REALTIME_COARSE,  EINVAL }
+  };
 
   for (int t = 0; t < array_length (types); t++)
     for (int r = 0; r < array_length (robust); r++)
-      {
-	pthread_mutexattr_t attr;
-
-	xpthread_mutexattr_init (&attr);
-	xpthread_mutexattr_setprotocol (&attr, PTHREAD_PRIO_INHERIT);
-	xpthread_mutexattr_settype (&attr, types[t]);
-	xpthread_mutexattr_setrobust (&attr, robust[r]);
+      for (int c = 0; c < array_length (clocks); c++)
+	{
+	  pthread_mutexattr_t attr;
+	  xpthread_mutexattr_init (&attr);
+	  xpthread_mutexattr_setprotocol (&attr, PTHREAD_PRIO_INHERIT);
+	  xpthread_mutexattr_settype (&attr, types[t]);
+	  xpthread_mutexattr_setrobust (&attr, robust[r]);
 
-	pthread_mutex_t mtx;
-	xpthread_mutex_init (&mtx, &attr);
+	  pthread_mutex_t mtx;
+	  xpthread_mutex_init (&mtx, &attr);
 
-	struct timespec tmo = timespec_add (xclock_now (CLOCK_MONOTONIC),
-					    make_timespec (0, 100000000));
+	  /* Uncontended case does not trigger any futex call.  */
+	  struct timespec tmo = timespec_add (xclock_now (clocks[c].clk),
+					      make_timespec (0, 100000000));
 
-	TEST_COMPARE (pthread_mutex_clocklock (&mtx, CLOCK_MONOTONIC, &tmo),
-		      EINVAL);
+	  TEST_COMPARE (pthread_mutex_clocklock (&mtx, clocks[c].clk, &tmo),
+			clocks[c].r);
+	  if (clocks[c].r == 0)
+	    TEST_COMPARE (pthread_mutex_unlock (&mtx), 0);
 
-	xpthread_mutex_destroy (&mtx);
-      }
+	  xpthread_mutex_destroy (&mtx);
+	}
 
   return 0;
 }
diff --git a/sysdeps/pthread/tst-mutex5.c b/sysdeps/pthread/tst-mutex5.c
index b2a06b3650..89ad03fd8f 100644
--- a/sysdeps/pthread/tst-mutex5.c
+++ b/sysdeps/pthread/tst-mutex5.c
@@ -25,6 +25,7 @@
 #include <config.h>
 #include <support/check.h>
 #include <support/timespec.h>
+#include <support/xthread.h>
 
 #ifdef ENABLE_PP
 #include "tst-tpp.h"
@@ -39,7 +40,7 @@
 #define CLOCK_USE_TIMEDLOCK (-1)
 
 static int
-do_test_clock (clockid_t clockid, const char *fnname)
+do_test_clock (clockid_t clockid, const char *fnname, int tmo_result)
 {
   pthread_mutex_t m;
   pthread_mutexattr_t a;
@@ -76,11 +77,12 @@ do_test_clock (clockid_t clockid, const char *fnname)
                                              make_timespec (2, 0));
 
   if (clockid == CLOCK_USE_TIMEDLOCK)
-    TEST_COMPARE (pthread_mutex_timedlock (&m, &ts_timeout), ETIMEDOUT);
+    TEST_COMPARE (pthread_mutex_timedlock (&m, &ts_timeout), tmo_result);
   else
     TEST_COMPARE (pthread_mutex_clocklock (&m, clockid, &ts_timeout),
-		  ETIMEDOUT);
-  TEST_TIMESPEC_BEFORE_NOW (ts_timeout, clockid_for_get);
+		  tmo_result);
+  if (tmo_result == ETIMEDOUT)
+    TEST_TIMESPEC_BEFORE_NOW (ts_timeout, clockid_for_get);
 
   /* The following makes the ts value invalid.  */
   ts_timeout.tv_nsec += 1000000000;
@@ -119,11 +121,16 @@ static int do_test (void)
   init_tpp_test ();
 #endif
 
-  do_test_clock (CLOCK_USE_TIMEDLOCK, "timedlock");
-  do_test_clock (CLOCK_REALTIME, "clocklock(realtime)");
-#ifndef ENABLE_PI
-  do_test_clock (CLOCK_MONOTONIC, "clocklock(monotonic)");
+  int monotonic_result =
+#ifdef ENABLE_PI
+    support_mutex_pi_monotonic () ? ETIMEDOUT : EINVAL;
+#else
+    ETIMEDOUT;
 #endif
+
+  do_test_clock (CLOCK_USE_TIMEDLOCK, "timedlock", ETIMEDOUT);
+  do_test_clock (CLOCK_REALTIME, "clocklock(realtime)", ETIMEDOUT);
+  do_test_clock (CLOCK_MONOTONIC, "clocklock(monotonic)", monotonic_result);
   return 0;
 }
 
diff --git a/sysdeps/pthread/tst-mutex9.c b/sysdeps/pthread/tst-mutex9.c
index 63bac69b6d..b29d6efcc8 100644
--- a/sysdeps/pthread/tst-mutex9.c
+++ b/sysdeps/pthread/tst-mutex9.c
@@ -28,6 +28,7 @@
 #include <support/check.h>
 #include <support/timespec.h>
 #include <support/xunistd.h>
+#include <support/xthread.h>
 
 #ifdef ENABLE_PP
 #include "tst-tpp.h"
@@ -39,7 +40,7 @@
 #define CLOCK_USE_TIMEDLOCK (-1)
 
 static void
-do_test_clock (clockid_t clockid)
+do_test_clock (clockid_t clockid, int tmo_result)
 {
   const clockid_t clockid_for_get =
     (clockid == CLOCK_USE_TIMEDLOCK) ? CLOCK_REALTIME : clockid;
@@ -111,9 +112,9 @@ do_test_clock (clockid_t clockid)
                                                make_timespec (0, 500000000));
 
       if (clockid == CLOCK_USE_TIMEDLOCK)
-        TEST_COMPARE (pthread_mutex_timedlock (m, &ts), ETIMEDOUT);
+        TEST_COMPARE (pthread_mutex_timedlock (m, &ts), tmo_result);
       else
-        TEST_COMPARE (pthread_mutex_clocklock (m, clockid, &ts), ETIMEDOUT);
+        TEST_COMPARE (pthread_mutex_clocklock (m, clockid, &ts), tmo_result);
 
       alarm (1);
 
@@ -141,11 +142,16 @@ do_test (void)
   init_tpp_test ();
 #endif
 
-  do_test_clock (CLOCK_USE_TIMEDLOCK);
-  do_test_clock (CLOCK_REALTIME);
-#ifndef ENABLE_PI
-  do_test_clock (CLOCK_MONOTONIC);
+  int monotonic_result =
+#ifdef ENABLE_PI
+    support_mutex_pi_monotonic () ? ETIMEDOUT : EINVAL;
+#else
+    ETIMEDOUT;
 #endif
+
+  do_test_clock (CLOCK_USE_TIMEDLOCK, ETIMEDOUT);
+  do_test_clock (CLOCK_REALTIME, ETIMEDOUT);
+  do_test_clock (CLOCK_MONOTONIC, monotonic_result);
   return 0;
 }
 
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v3 0/4] Add CLOCK_MONOTONIC support for PI mutexes
  2021-09-06 20:47 [PATCH v3 0/4] Add CLOCK_MONOTONIC support for PI mutexes Adhemerval Zanella via Libc-alpha
                   ` (3 preceding siblings ...)
  2021-09-06 20:47 ` [PATCH v3 4/4] nptl: Add CLOCK_MONOTONIC support for PI mutexes Adhemerval Zanella via Libc-alpha
@ 2021-09-27 13:48 ` Adhemerval Zanella via Libc-alpha
  2021-10-01 11:42   ` Adhemerval Zanella via Libc-alpha
  4 siblings, 1 reply; 7+ messages in thread
From: Adhemerval Zanella via Libc-alpha @ 2021-09-27 13:48 UTC (permalink / raw)
  To: libc-alpha

Ping on the set.

On 06/09/2021 17:47, Adhemerval Zanella wrote:
> It adds support for CLOCK_MONOTONIC for PI mutexes by using the new
> Linux v5.14 FUTEX_LOCK_PI2 operation.  Similar to current approach,
> pthread_mutex_clocklock might return EINVAL if the underlying kernel
> has no support for the futex operation.
> 
> Changes since v2:
> 
>   * Renamed xpthread_mutex_pi_support_monotonic to
>     support_mutex_pi_monotonic.
> 
> Changes since v1:
>   
>   * Refactor futex_lock_pi64 and avoid adding futex_lock_pi2.  The
>     FUTEX_LOCK_PI2 logic is done at only one place, so the code change
>     on pthread routines are minimal.  Also, FUTEX_LOCK_PI2 is only
>     used if it is really required, i.e, when a timeout against
>     CLOCK_MONOTONIC is used.
>   * Do not remove nptl/tst-mutexpi10.c, but instead adjust it to new
>     FUTEX_LOCK_PI2 support.
>   * Add pthread_mutex_pi_support_monotonic() to check if monotonic
>     clock with PI mutexes are supported and use on test to check for
>     the expected result.
> 
> Adhemerval Zanella (3):
>   nptl: Use FUTEX_LOCK_PI2 when available
>   support: Add support_mutex_pi_monotonic
>   nptl: Add CLOCK_MONOTONIC support for PI mutexes
> 
> Kurt Kanzenbach (1):
>   Linux: Add FUTEX_LOCK_PI2
> 
>  nptl/futex-internal.c                     | 63 +++++++++++++++++++++++
>  nptl/pthread_mutex_lock.c                 |  3 +-
>  nptl/pthread_mutex_timedlock.c            | 10 +---
>  nptl/tst-mutexpi10.c                      | 42 +++++++++------
>  support/Makefile                          |  1 +
>  support/support_mutex_pi_monotonic.c      | 33 ++++++++++++
>  support/xthread.h                         |  4 ++
>  sysdeps/nptl/futex-internal.h             | 58 ++-------------------
>  sysdeps/nptl/lowlevellock-futex.h         |  1 +
>  sysdeps/pthread/tst-mutex5.c              | 23 ++++++---
>  sysdeps/pthread/tst-mutex9.c              | 20 ++++---
>  sysdeps/unix/sysv/linux/kernel-features.h |  8 +++
>  12 files changed, 172 insertions(+), 94 deletions(-)
>  create mode 100644 support/support_mutex_pi_monotonic.c
> 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v3 0/4] Add CLOCK_MONOTONIC support for PI mutexes
  2021-09-27 13:48 ` [PATCH v3 0/4] " Adhemerval Zanella via Libc-alpha
@ 2021-10-01 11:42   ` Adhemerval Zanella via Libc-alpha
  0 siblings, 0 replies; 7+ messages in thread
From: Adhemerval Zanella via Libc-alpha @ 2021-10-01 11:42 UTC (permalink / raw)
  To: libc-alpha

I will push this set shortly if no one opposes it.

On 27/09/2021 10:48, Adhemerval Zanella wrote:
> Ping on the set.
> 
> On 06/09/2021 17:47, Adhemerval Zanella wrote:
>> It adds support for CLOCK_MONOTONIC for PI mutexes by using the new
>> Linux v5.14 FUTEX_LOCK_PI2 operation.  Similar to current approach,
>> pthread_mutex_clocklock might return EINVAL if the underlying kernel
>> has no support for the futex operation.
>>
>> Changes since v2:
>>
>>   * Renamed xpthread_mutex_pi_support_monotonic to
>>     support_mutex_pi_monotonic.
>>
>> Changes since v1:
>>   
>>   * Refactor futex_lock_pi64 and avoid adding futex_lock_pi2.  The
>>     FUTEX_LOCK_PI2 logic is done at only one place, so the code change
>>     on pthread routines are minimal.  Also, FUTEX_LOCK_PI2 is only
>>     used if it is really required, i.e, when a timeout against
>>     CLOCK_MONOTONIC is used.
>>   * Do not remove nptl/tst-mutexpi10.c, but instead adjust it to new
>>     FUTEX_LOCK_PI2 support.
>>   * Add pthread_mutex_pi_support_monotonic() to check if monotonic
>>     clock with PI mutexes are supported and use on test to check for
>>     the expected result.
>>
>> Adhemerval Zanella (3):
>>   nptl: Use FUTEX_LOCK_PI2 when available
>>   support: Add support_mutex_pi_monotonic
>>   nptl: Add CLOCK_MONOTONIC support for PI mutexes
>>
>> Kurt Kanzenbach (1):
>>   Linux: Add FUTEX_LOCK_PI2
>>
>>  nptl/futex-internal.c                     | 63 +++++++++++++++++++++++
>>  nptl/pthread_mutex_lock.c                 |  3 +-
>>  nptl/pthread_mutex_timedlock.c            | 10 +---
>>  nptl/tst-mutexpi10.c                      | 42 +++++++++------
>>  support/Makefile                          |  1 +
>>  support/support_mutex_pi_monotonic.c      | 33 ++++++++++++
>>  support/xthread.h                         |  4 ++
>>  sysdeps/nptl/futex-internal.h             | 58 ++-------------------
>>  sysdeps/nptl/lowlevellock-futex.h         |  1 +
>>  sysdeps/pthread/tst-mutex5.c              | 23 ++++++---
>>  sysdeps/pthread/tst-mutex9.c              | 20 ++++---
>>  sysdeps/unix/sysv/linux/kernel-features.h |  8 +++
>>  12 files changed, 172 insertions(+), 94 deletions(-)
>>  create mode 100644 support/support_mutex_pi_monotonic.c
>>

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2021-10-01 11:42 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-06 20:47 [PATCH v3 0/4] Add CLOCK_MONOTONIC support for PI mutexes Adhemerval Zanella via Libc-alpha
2021-09-06 20:47 ` [PATCH v3 1/4] Linux: Add FUTEX_LOCK_PI2 Adhemerval Zanella via Libc-alpha
2021-09-06 20:47 ` [PATCH v3 2/4] nptl: Use FUTEX_LOCK_PI2 when available Adhemerval Zanella via Libc-alpha
2021-09-06 20:47 ` [PATCH v3 3/4] support: Add support_mutex_pi_monotonic Adhemerval Zanella via Libc-alpha
2021-09-06 20:47 ` [PATCH v3 4/4] nptl: Add CLOCK_MONOTONIC support for PI mutexes Adhemerval Zanella via Libc-alpha
2021-09-27 13:48 ` [PATCH v3 0/4] " Adhemerval Zanella via Libc-alpha
2021-10-01 11:42   ` Adhemerval Zanella via Libc-alpha

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).