Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.kernel > #1548161
| From | Waiman Long <longman@redhat.com> |
|---|---|
| Newsgroups | linux.kernel |
| Subject | [PATCH v4 11/20] TP-futex: Add timeout support |
| Date | 2016-12-29 17:20 +0100 |
| Message-ID | <sTLAS-pI-55@gated-at.bofh.it> (permalink) |
| References | <sTLAR-pI-3@gated-at.bofh.it> |
| Organization | linux.* mail to news gateway |
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@redhat.com>
---
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
Back to linux.kernel | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
[PATCH v4 00/20] futex: Introducing throughput-optimized (TP) futexes Waiman Long <longman@redhat.com> - 2016-12-29 17:20 +0100
[PATCH v4 07/20] futex: Introduce throughput-optimized (TP) futexes Waiman Long <longman@redhat.com> - 2016-12-29 17:20 +0100
Re: [PATCH v4 07/20] futex: Introduce throughput-optimized (TP) futexes kbuild test robot <lkp@intel.com> - 2016-12-29 22:20 +0100
[PATCH v4 08/20] TP-futex: Enable robust handling Waiman Long <longman@redhat.com> - 2016-12-29 17:20 +0100
[PATCH v4 06/20] futex: Allow direct attachment of futex_state objects to hash bucket Waiman Long <longman@redhat.com> - 2016-12-29 17:20 +0100
[PATCH v4 20/20] futex: Dump internal futex state via debugfs Waiman Long <longman@redhat.com> - 2016-12-29 17:20 +0100
Re: [PATCH v4 20/20] futex: Dump internal futex state via debugfs kbuild test robot <lkp@intel.com> - 2016-12-29 20:00 +0100
[PATCH v4 16/20] TP-futex: Group readers together in wait queue Waiman Long <longman@redhat.com> - 2016-12-29 17:20 +0100
Re: [PATCH v4 16/20] TP-futex: Group readers together in wait queue kbuild test robot <lkp@intel.com> - 2016-12-29 21:00 +0100
Re: [PATCH v4 16/20] TP-futex: Group readers together in wait queue kbuild test robot <lkp@intel.com> - 2016-12-29 21:20 +0100
[PATCH v4 15/20] TP-futex: Enable kernel reader lock stealing Waiman Long <longman@redhat.com> - 2016-12-29 17:20 +0100
[PATCH v4 01/20] futex: Consolidate duplicated timer setup code Waiman Long <longman@redhat.com> - 2016-12-29 17:20 +0100
[PATCH v4 11/20] TP-futex: Add timeout support Waiman Long <longman@redhat.com> - 2016-12-29 17:20 +0100
[PATCH v4 04/20] futex: Consolidate pure pi_state_list add & delete codes to helpers Waiman Long <longman@redhat.com> - 2016-12-29 17:20 +0100
[PATCH v4 14/20] TP-futex: Support userspace reader/writer locks Waiman Long <longman@redhat.com> - 2016-12-29 17:20 +0100
Re: [PATCH v4 14/20] TP-futex: Support userspace reader/writer locks kbuild test robot <lkp@intel.com> - 2016-12-29 19:40 +0100
[PATCH v4 09/20] TP-futex: Implement lock handoff to prevent lock starvation Waiman Long <longman@redhat.com> - 2016-12-29 17:20 +0100
[PATCH v4 18/20] perf bench: New microbenchmark for userspace rwlock performance Waiman Long <longman@redhat.com> - 2016-12-29 17:20 +0100
csiph-web