History

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.

Thomas Gleixner committed 5 years ago

Commit f6f4ec01 parent c363b7e1 file · +23 −38Browse files

Unified · Split

kernel/futex.c+23−38
@@ -1299,27 +1299,6 @@ static int attach_to_pi_owner(u32 __user *uaddr, u32 uval, union futex_key *key, · 1298 unmodified lines
12991299 return 0;
13001300 }
13011301
1302−static int lookup_pi_state(u32 __user *uaddr, u32 uval,
1303− struct futex_hash_bucket *hb,
1304− union futex_key *key, struct futex_pi_state **ps,
1305− struct task_struct **exiting)
1306−{
1307− struct futex_q *top_waiter = futex_top_waiter(hb, key);
1308−
1309− /*
1310− * If there is a waiter on that futex, validate it and
1311− * attach to the pi_state when the validation succeeds.
1312− */
1313− if (top_waiter)
1314− return attach_to_pi_state(uaddr, uval, top_waiter->pi_state, ps);
1315−
1316− /*
1317− * We are the first waiter - try to look up the owner based on
1318− * @uval and attach to it.
1319− */
1320− return attach_to_pi_owner(uaddr, uval, key, ps, exiting);
1321−}
1322−
13231302 static int lock_pi_update_atomic(u32 __user *uaddr, u32 uval, u32 newval)
13241303 {
13251304 int err;
@@ -2038,8 +2017,8 @@ retry_private: · 712 unmodified lines
20382017 * At this point the top_waiter has either taken uaddr2 or is
20392018 * waiting on it. If the former, then the pi_state will not
20402019 * exist yet, look it up one more time to ensure we have a
2041− * reference to it. If the lock was taken, ret contains the
2042− * vpid of the top waiter task.
2020+ * reference to it. If the lock was taken, @ret contains the
2021+ * VPID of the top waiter task.
20432022 * If the lock was not taken, we have pi_state and an initial
20442023 * refcount on it. In case of an error we have nothing.
20452024 */
@@ -2047,19 +2026,25 @@ retry_private: · 1 unmodified line
20472026 WARN_ON(pi_state);
20482027 task_count++;
20492028 /*
2050− * If we acquired the lock, then the user space value
2051− * of uaddr2 should be vpid. It cannot be changed by
2052− * the top waiter as it is blocked on hb2 lock if it
2053− * tries to do so. If something fiddled with it behind
2054− * our back the pi state lookup might unearth it. So
2055− * we rather use the known value than rereading and
2056− * handing potential crap to lookup_pi_state.
2029+ * If futex_proxy_trylock_atomic() acquired the
2030+ * user space futex, then the user space value
2031+ * @uaddr2 has been set to the @hb1's top waiter
2032+ * task VPID. This task is guaranteed to be alive
2033+ * and cannot be exiting because it is either
2034+ * sleeping or blocked on @hb2 lock.
2035+ *
2036+ * The @uaddr2 futex cannot have waiters either as
2037+ * otherwise futex_proxy_trylock_atomic() would not
2038+ * have succeeded.
20572039 *
2058− * If that call succeeds then we have pi_state and an
2059− * initial refcount on it.
2040+ * In order to requeue waiters to @hb2, pi state is
2041+ * required. Hand in the VPID value (@ret) and
2042+ * allocate PI state with an initial refcount on
2043+ * it.
20602044 */
2061− ret = lookup_pi_state(uaddr2, ret, hb2, &key2,
2062− &pi_state, &exiting);
2045+ ret = attach_to_pi_owner(uaddr2, ret, &key2, &pi_state,
2046+ &exiting);
2047+ WARN_ON(ret);
20632048 }
20642049
20652050 switch (ret) {
@@ -2183,9 +2168,9 @@ retry_private: · 117 unmodified lines
21832168 }
21842169
21852170 /*
2186− * We took an extra initial reference to the pi_state either
2187− * in futex_proxy_trylock_atomic() or in lookup_pi_state(). We
2188− * need to drop it here again.
2171+ * We took an extra initial reference to the pi_state either in
2172+ * futex_proxy_trylock_atomic() or in attach_to_pi_owner(). We need
2173+ * to drop it here again.
21892174 */
21902175 put_pi_state(pi_state);
21912176
@@ -2364,7 +2349,7 @@ static int __fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q, · 172 unmodified lines
23642349 * Modifying pi_state _before_ the user space value would leave the
23652350 * pi_state in an inconsistent state when we fault here, because we
23662351 * need to drop the locks to handle the fault. This might be observed
2367− * in the PID check in lookup_pi_state.
2352+ * in the PID checks when attaching to PI state .
23682353 */
23692354 retry:
23702355 if (!argowner) {