[PATCH-tip v6 13/22] TP-futex: Add timeout support

From: Waiman Long
Date: Wed Mar 22 2017 - 13:42:11 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. Like FUTEX_WAIT, the timeout value is relative to current
time, not an absolute time. 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 | 52 +++++++++++++++++++++++++++++++++++++++++++---------
1 file changed, 43 insertions(+), 9 deletions(-)

diff --git a/kernel/futex.c b/kernel/futex.c
index c0ce1e1..b54f429 100644
--- a/kernel/futex.c
+++ b/kernel/futex.c
@@ -3580,9 +3580,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.
@@ -3598,7 +3599,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" \
@@ -3670,6 +3672,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;
@@ -3777,10 +3785,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;
@@ -3804,6 +3820,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;
@@ -3829,6 +3847,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.
*
@@ -3840,10 +3861,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);

@@ -3860,6 +3890,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;
}

@@ -4013,7 +4047,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
@@ -4032,7 +4066,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;
@@ -4042,7 +4076,7 @@ long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
return -EINVAL;

t = timespec_to_ktime(ts);
- if (cmd == FUTEX_WAIT)
+ if (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK)
t = ktime_add_safe(ktime_get(), t);
tp = &t;
}
--
1.8.3.1