[PATCH v4 11/20] TP-futex: Add timeout support

From: Waiman Long
Date: Thu Dec 29 2016 - 11:16:46 EST


Unlike other futexes, TP futexes do not accept the specification of
a timeout value. To align with the other futexes, timeout support is
now added. However, the timeout isn't as precise as the other futex
types due to the fact that timer expiration can't be detected when
the thread is waiting in the serialization mutex.

Signed-off-by: Waiman Long <longman@xxxxxxxxxx>
---
kernel/futex.c | 50 ++++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 42 insertions(+), 8 deletions(-)

diff --git a/kernel/futex.c b/kernel/futex.c
index 3308cc3..429d85f 100644
--- a/kernel/futex.c
+++ b/kernel/futex.c
@@ -3556,9 +3556,10 @@ static inline int futex_set_waiters_bit(u32 __user *uaddr, u32 *puval)

/**
* futex_spin_on_owner - Optimistically spin on futex owner
- * @uaddr: futex address
- * @vpid: PID of current task
- * @state: futex state object
+ * @uaddr: futex address
+ * @vpid: PID of current task
+ * @state: futex state object
+ * @timeout: hrtimer_sleeper structure
*
* Spin on the futex word while the futex owner is active. Otherwise, set
* the FUTEX_WAITERS bit and go to sleep.
@@ -3574,7 +3575,8 @@ static inline int futex_set_waiters_bit(u32 __user *uaddr, u32 *puval)
* Return: TP status code if lock acquired, < 0 if an error happens.
*/
static int futex_spin_on_owner(u32 __user *uaddr, const u32 vpid,
- struct futex_state *state)
+ struct futex_state *state,
+ struct hrtimer_sleeper *timeout)
{
#define OWNER_DEAD_MESSAGE \
"futex: owner pid %d of TP futex 0x%lx was %s.\n" \
@@ -3646,6 +3648,12 @@ static int futex_spin_on_owner(u32 __user *uaddr, const u32 vpid,
continue;
}

+
+ if (timeout && !timeout->task) {
+ ret = -ETIMEDOUT;
+ break;
+ }
+
if (signal_pending(current)) {
ret = -EINTR;
break;
@@ -3753,10 +3761,18 @@ static int futex_spin_on_owner(u32 __user *uaddr, const u32 vpid,
* This function is not inlined so that it can show up separately in perf
* profile for performance analysis purpose.
*
+ * The timeout functionality isn't as precise as other other futex types
+ * as timer expiration will not be detected while waiting in the
+ * serialization mutex. One possible solution is to modify the expiration
+ * function to send out a signal as well so as to break the thread out of
+ * the mutex code if we really want more precise timeout.
+ *
* Return: TP status code if lock acquired, < 0 if an error happens.
*/
-static noinline int futex_lock(u32 __user *uaddr, unsigned int flags)
+static noinline int
+futex_lock(u32 __user *uaddr, unsigned int flags, ktime_t *time)
{
+ struct hrtimer_sleeper timeout, *to;
struct futex_hash_bucket *hb;
union futex_key key = FUTEX_KEY_INIT;
struct futex_state *state;
@@ -3780,6 +3796,8 @@ static noinline int futex_lock(u32 __user *uaddr, unsigned int flags)
if (refill_futex_state_cache())
return -ENOMEM;

+ to = futex_setup_timer(time, &timeout, flags, current->timer_slack_ns);
+
ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, VERIFY_WRITE);
if (unlikely(ret))
goto out;
@@ -3805,6 +3823,9 @@ static noinline int futex_lock(u32 __user *uaddr, unsigned int flags)
goto out_put_state_key;
}

+ if (to)
+ hrtimer_start_expires(&to->timer, HRTIMER_MODE_ABS);
+
/*
* Acquiring the serialization mutex.
*
@@ -3816,10 +3837,19 @@ static noinline int futex_lock(u32 __user *uaddr, unsigned int flags)
goto out_put_state_key;

/*
+ * Check for timeout.
+ */
+ if (to && !timeout.task) {
+ ret = -ETIMEDOUT;
+ mutex_unlock(&state->mutex);
+ goto out_put_state_key;
+ }
+
+ /*
* As the mutex owner, we can now spin on the futex word as well as
* the active-ness of the futex owner.
*/
- ret = futex_spin_on_owner(uaddr, vpid, state);
+ ret = futex_spin_on_owner(uaddr, vpid, state, to);

mutex_unlock(&state->mutex);

@@ -3836,6 +3866,10 @@ static noinline int futex_lock(u32 __user *uaddr, unsigned int flags)
put_futex_key(&key);

out:
+ if (to) {
+ hrtimer_cancel(&to->timer);
+ destroy_hrtimer_on_stack(&to->timer);
+ }
return ret;
}

@@ -3989,7 +4023,7 @@ long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 1);
#ifdef CONFIG_SMP
case FUTEX_LOCK:
- return futex_lock(uaddr, flags);
+ return futex_lock(uaddr, flags, timeout);
case FUTEX_UNLOCK:
return futex_unlock(uaddr, flags);
#endif
@@ -4008,7 +4042,7 @@ long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
int cmd = op & FUTEX_CMD_MASK;

if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI ||
- cmd == FUTEX_WAIT_BITSET ||
+ cmd == FUTEX_WAIT_BITSET || cmd == FUTEX_LOCK ||
cmd == FUTEX_WAIT_REQUEUE_PI)) {
if (unlikely(should_fail_futex(!(op & FUTEX_PRIVATE_FLAG))))
return -EFAULT;
--
1.8.3.1