Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1494291

[PATCH v3 10/13] futex: Inform FUTEX_LOCK callers on how the lock is acquired

From Waiman Long <Waiman.Long@hpe.com>
Newsgroups linux.kernel
Subject [PATCH v3 10/13] futex: Inform FUTEX_LOCK callers on how the lock is acquired
Date 2016-09-30 23:40 +0200
Message-ID <sndHc-7ss-13@gated-at.bofh.it> (permalink)
References <sndxv-7oR-7@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


As there are three different ways for a TP futex waiter in the kernel
to acquire the lock. It may be useful to pass this information out so
that user space has a better view of what is happening in the kernel.
With this change, different non-negative values will now be returned
depending on how the lock is acquired in the kernel.

Signed-off-by: Waiman Long <Waiman.Long@hpe.com>
---
 kernel/futex.c |   30 ++++++++++++++++++++++--------
 1 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/kernel/futex.c b/kernel/futex.c
index d260410..1219f32 100644
--- a/kernel/futex.c
+++ b/kernel/futex.c
@@ -3337,7 +3337,17 @@ void exit_robust_list(struct task_struct *curr)
 #define TP_SPIN_THRESHOLD       (1 << 14)
 #define TP_SLEEP_DECREMENT      (TP_SPIN_THRESHOLD/64)
 
-/**
+/*
+ * futex_lock() returned values to identify how the lock is acquired:
+ * 0 - steals the lock
+ * 1 - top waiter (mutex owner) acquires the lock
+ * 2 - handed off the lock
+ */
+#define TP_LOCK_STOLEN		0
+#define TP_LOCK_ACQUIRED	1
+#define TP_LOCK_HANDOFF		2
+
+ /**
  * lookup_futex_state - Looking up the futex state structure.
  * @hb:		 hash bucket
  * @key:	 futex key
@@ -3420,9 +3430,11 @@ static inline int put_futex_state_unlocked(struct futex_state *state)
  *   preserve the flag bits
  * endif
  *
- * Return: 1 if lock acquired;
+ * Return: TP_LOCK_ACQUIRED if lock acquired;
+ *	   TP_LOCK_HANDOFF if lock was handed off;
  *	   0 if lock acquisition failed;
  *	   -EFAULT if an error happened.
+ *	   *puval will contain the latest futex value when trylock fails.
  */
 static inline int futex_trylock(u32 __user *uaddr, u32 vpid, u32 *puval,
 				const bool waiter)
@@ -3435,7 +3447,7 @@ static inline int futex_trylock(u32 __user *uaddr, u32 vpid, u32 *puval,
 	*puval = uval;
 
 	if (waiter && (uval & FUTEX_TID_MASK) == vpid)
-		return 1;
+		return TP_LOCK_HANDOFF;
 
 	if (uval & FUTEX_TID_MASK)
 		return 0;	/* Trylock fails */
@@ -3446,7 +3458,7 @@ static inline int futex_trylock(u32 __user *uaddr, u32 vpid, u32 *puval,
 	if (unlikely(cmpxchg_futex_value(puval, uaddr, uval, vpid|flags)))
 		return -EFAULT;
 
-	return *puval == uval;
+	return (*puval == uval) ? TP_LOCK_ACQUIRED : 0;
 }
 
 /**
@@ -3659,8 +3671,10 @@ efault:
  * This function is not inlined so that it can show up separately in perf
  * profile for performance analysis purpose.
  *
- * Return: 0   - lock acquired
- *	   < 0 - an error happens
+ * Return: TP_LOCK_ACQUIRED - lock acquired normally
+ *	   TP_LOCK_HANDOFF  - lock handed off directly from unlocker
+ *	   TP_LOCK_STOLEN   - lock stolen
+ *	   < 0		    - an error happens
  */
 static noinline int futex_lock(u32 __user *uaddr, unsigned int flags)
 {
@@ -3676,7 +3690,7 @@ static noinline int futex_lock(u32 __user *uaddr, unsigned int flags)
 	 */
 	ret = futex_trylock(uaddr, vpid, &uval, false);
 	if (ret)
-		goto out;
+		return (ret < 0) ? ret : TP_LOCK_STOLEN;
 
 	/*
 	 * Detect deadlocks.
@@ -3750,7 +3764,7 @@ out_put_state_key:
 	put_futex_key(&key);
 
 out:
-	return (ret < 0) ? ret : 0;
+	return ret;
 }
 
 /*
-- 
1.7.1

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH v3 00/13] futex: Introducing throughput-optimized futexes Waiman Long <Waiman.Long@hpe.com> - 2016-09-30 23:30 +0200
  [PATCH v3 02/13] futex: Rename futex_pi_state to futex_state Waiman Long <Waiman.Long@hpe.com> - 2016-09-30 23:30 +0200
  [PATCH v3 13/13] perf bench: New microbenchmark for userspace mutex performance Waiman Long <Waiman.Long@hpe.com> - 2016-09-30 23:30 +0200
  [PATCH v3 04/13] futex: Consolidate pure pi_state_list add & delete codes to helpers Waiman Long <Waiman.Long@hpe.com> - 2016-09-30 23:40 +0200
  [PATCH v3 06/13] futex: Allow direct attachment of futex_state objects to hash bucket Waiman Long <Waiman.Long@hpe.com> - 2016-09-30 23:40 +0200
  [PATCH v3 07/13] futex: Throughput-optimized (TP) futexes Waiman Long <Waiman.Long@hpe.com> - 2016-09-30 23:40 +0200
    Re: [PATCH v3 07/13] futex: Throughput-optimized (TP) futexes Thomas Gleixner <tglx@linutronix.de> - 2016-10-01 09:00 +0200
  [PATCH v3 10/13] futex: Inform FUTEX_LOCK callers on how the lock is acquired Waiman Long <Waiman.Long@hpe.com> - 2016-09-30 23:40 +0200
  [PATCH v3 11/13] futex: Add timeout support to TP futexes Waiman Long <Waiman.Long@hpe.com> - 2016-09-30 23:40 +0200
  [PATCH v3 05/13] futex: Add a new futex type field into futex_state Waiman Long <Waiman.Long@hpe.com> - 2016-09-30 23:40 +0200
  [PATCH v3 08/13] futex: Enable robust handling of TP futexes Waiman Long <Waiman.Long@hpe.com> - 2016-09-30 23:40 +0200

csiph-web