Commit f6f4ec00 authored by Thomas Gleixner's avatar Thomas Gleixner Committed by Ingo Molnar

futex: Clarify futex_requeue() PI handling

When requeuing to a PI futex, then the requeue code tries to trylock the PI
futex on behalf of the topmost waiter on the inner 'waitqueue' futex. If
that succeeds, then PI state has to be allocated in order to requeue further
waiters to the PI futex.

The comment and the code are confusing, as the PI state allocation uses
lookup_pi_state(), which either attaches to an existing waiter or to the
owner. As the PI futex was just acquired, there cannot be a waiter on the
PI futex because the hash bucket lock is held.

Clarify the comment and use attach_to_pi_owner() directly. As the task on
which behalf the PI futex has been acquired is guaranteed to be alive and
not exiting, this call must succeed. Add a WARN_ON() in case that fails.
Signed-off-by: default avatarThomas Gleixner <tglx@linutronix.de>
Signed-off-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: default avatarIngo Molnar <mingo@kernel.org>
Link: https://lore.kernel.org/r/20210815211305.305142462@linutronix.de
parent c363b7ed
...@@ -1299,27 +1299,6 @@ static int attach_to_pi_owner(u32 __user *uaddr, u32 uval, union futex_key *key, ...@@ -1299,27 +1299,6 @@ static int attach_to_pi_owner(u32 __user *uaddr, u32 uval, union futex_key *key,
return 0; return 0;
} }
static int lookup_pi_state(u32 __user *uaddr, u32 uval,
struct futex_hash_bucket *hb,
union futex_key *key, struct futex_pi_state **ps,
struct task_struct **exiting)
{
struct futex_q *top_waiter = futex_top_waiter(hb, key);
/*
* If there is a waiter on that futex, validate it and
* attach to the pi_state when the validation succeeds.
*/
if (top_waiter)
return attach_to_pi_state(uaddr, uval, top_waiter->pi_state, ps);
/*
* We are the first waiter - try to look up the owner based on
* @uval and attach to it.
*/
return attach_to_pi_owner(uaddr, uval, key, ps, exiting);
}
static int lock_pi_update_atomic(u32 __user *uaddr, u32 uval, u32 newval) static int lock_pi_update_atomic(u32 __user *uaddr, u32 uval, u32 newval)
{ {
int err; int err;
...@@ -2038,8 +2017,8 @@ static int futex_requeue(u32 __user *uaddr1, unsigned int flags, ...@@ -2038,8 +2017,8 @@ static int futex_requeue(u32 __user *uaddr1, unsigned int flags,
* At this point the top_waiter has either taken uaddr2 or is * At this point the top_waiter has either taken uaddr2 or is
* waiting on it. If the former, then the pi_state will not * waiting on it. If the former, then the pi_state will not
* exist yet, look it up one more time to ensure we have a * exist yet, look it up one more time to ensure we have a
* reference to it. If the lock was taken, ret contains the * reference to it. If the lock was taken, @ret contains the
* vpid of the top waiter task. * VPID of the top waiter task.
* If the lock was not taken, we have pi_state and an initial * If the lock was not taken, we have pi_state and an initial
* refcount on it. In case of an error we have nothing. * refcount on it. In case of an error we have nothing.
*/ */
...@@ -2047,19 +2026,25 @@ static int futex_requeue(u32 __user *uaddr1, unsigned int flags, ...@@ -2047,19 +2026,25 @@ static int futex_requeue(u32 __user *uaddr1, unsigned int flags,
WARN_ON(pi_state); WARN_ON(pi_state);
task_count++; task_count++;
/* /*
* If we acquired the lock, then the user space value * If futex_proxy_trylock_atomic() acquired the
* of uaddr2 should be vpid. It cannot be changed by * user space futex, then the user space value
* the top waiter as it is blocked on hb2 lock if it * @uaddr2 has been set to the @hb1's top waiter
* tries to do so. If something fiddled with it behind * task VPID. This task is guaranteed to be alive
* our back the pi state lookup might unearth it. So * and cannot be exiting because it is either
* we rather use the known value than rereading and * sleeping or blocked on @hb2 lock.
* handing potential crap to lookup_pi_state. *
* The @uaddr2 futex cannot have waiters either as
* otherwise futex_proxy_trylock_atomic() would not
* have succeeded.
* *
* If that call succeeds then we have pi_state and an * In order to requeue waiters to @hb2, pi state is
* initial refcount on it. * required. Hand in the VPID value (@ret) and
* allocate PI state with an initial refcount on
* it.
*/ */
ret = lookup_pi_state(uaddr2, ret, hb2, &key2, ret = attach_to_pi_owner(uaddr2, ret, &key2, &pi_state,
&pi_state, &exiting); &exiting);
WARN_ON(ret);
} }
switch (ret) { switch (ret) {
...@@ -2183,9 +2168,9 @@ static int futex_requeue(u32 __user *uaddr1, unsigned int flags, ...@@ -2183,9 +2168,9 @@ static int futex_requeue(u32 __user *uaddr1, unsigned int flags,
} }
/* /*
* We took an extra initial reference to the pi_state either * We took an extra initial reference to the pi_state either in
* in futex_proxy_trylock_atomic() or in lookup_pi_state(). We * futex_proxy_trylock_atomic() or in attach_to_pi_owner(). We need
* need to drop it here again. * to drop it here again.
*/ */
put_pi_state(pi_state); put_pi_state(pi_state);
...@@ -2364,7 +2349,7 @@ static int __fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q, ...@@ -2364,7 +2349,7 @@ static int __fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
* Modifying pi_state _before_ the user space value would leave the * Modifying pi_state _before_ the user space value would leave the
* pi_state in an inconsistent state when we fault here, because we * pi_state in an inconsistent state when we fault here, because we
* need to drop the locks to handle the fault. This might be observed * need to drop the locks to handle the fault. This might be observed
* in the PID check in lookup_pi_state. * in the PID checks when attaching to PI state .
*/ */
retry: retry:
if (!argowner) { if (!argowner) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment