1e5c68284SPeter Zijlstra // SPDX-License-Identifier: GPL-2.0-or-later
2e5c68284SPeter Zijlstra
3*8b7787a5SKent Overstreet #include <linux/plist.h>
4e5c68284SPeter Zijlstra #include <linux/sched/signal.h>
5e5c68284SPeter Zijlstra
6e5c68284SPeter Zijlstra #include "futex.h"
7e5c68284SPeter Zijlstra #include "../locking/rtmutex_common.h"
8e5c68284SPeter Zijlstra
9e5c68284SPeter Zijlstra /*
10e5c68284SPeter Zijlstra * On PREEMPT_RT, the hash bucket lock is a 'sleeping' spinlock with an
11e5c68284SPeter Zijlstra * underlying rtmutex. The task which is about to be requeued could have
12e5c68284SPeter Zijlstra * just woken up (timeout, signal). After the wake up the task has to
13e5c68284SPeter Zijlstra * acquire hash bucket lock, which is held by the requeue code. As a task
14e5c68284SPeter Zijlstra * can only be blocked on _ONE_ rtmutex at a time, the proxy lock blocking
15e5c68284SPeter Zijlstra * and the hash bucket lock blocking would collide and corrupt state.
16e5c68284SPeter Zijlstra *
17e5c68284SPeter Zijlstra * On !PREEMPT_RT this is not a problem and everything could be serialized
18e5c68284SPeter Zijlstra * on hash bucket lock, but aside of having the benefit of common code,
19e5c68284SPeter Zijlstra * this allows to avoid doing the requeue when the task is already on the
20e5c68284SPeter Zijlstra * way out and taking the hash bucket lock of the original uaddr1 when the
21e5c68284SPeter Zijlstra * requeue has been completed.
22e5c68284SPeter Zijlstra *
23e5c68284SPeter Zijlstra * The following state transitions are valid:
24e5c68284SPeter Zijlstra *
25e5c68284SPeter Zijlstra * On the waiter side:
26e5c68284SPeter Zijlstra * Q_REQUEUE_PI_NONE -> Q_REQUEUE_PI_IGNORE
27e5c68284SPeter Zijlstra * Q_REQUEUE_PI_IN_PROGRESS -> Q_REQUEUE_PI_WAIT
28e5c68284SPeter Zijlstra *
29e5c68284SPeter Zijlstra * On the requeue side:
30e5c68284SPeter Zijlstra * Q_REQUEUE_PI_NONE -> Q_REQUEUE_PI_INPROGRESS
31e5c68284SPeter Zijlstra * Q_REQUEUE_PI_IN_PROGRESS -> Q_REQUEUE_PI_DONE/LOCKED
32e5c68284SPeter Zijlstra * Q_REQUEUE_PI_IN_PROGRESS -> Q_REQUEUE_PI_NONE (requeue failed)
33e5c68284SPeter Zijlstra * Q_REQUEUE_PI_WAIT -> Q_REQUEUE_PI_DONE/LOCKED
34e5c68284SPeter Zijlstra * Q_REQUEUE_PI_WAIT -> Q_REQUEUE_PI_IGNORE (requeue failed)
35e5c68284SPeter Zijlstra *
36e5c68284SPeter Zijlstra * The requeue side ignores a waiter with state Q_REQUEUE_PI_IGNORE as this
37e5c68284SPeter Zijlstra * signals that the waiter is already on the way out. It also means that
38e5c68284SPeter Zijlstra * the waiter is still on the 'wait' futex, i.e. uaddr1.
39e5c68284SPeter Zijlstra *
40e5c68284SPeter Zijlstra * The waiter side signals early wakeup to the requeue side either through
41e5c68284SPeter Zijlstra * setting state to Q_REQUEUE_PI_IGNORE or to Q_REQUEUE_PI_WAIT depending
42e5c68284SPeter Zijlstra * on the current state. In case of Q_REQUEUE_PI_IGNORE it can immediately
43e5c68284SPeter Zijlstra * proceed to take the hash bucket lock of uaddr1. If it set state to WAIT,
44e5c68284SPeter Zijlstra * which means the wakeup is interleaving with a requeue in progress it has
45e5c68284SPeter Zijlstra * to wait for the requeue side to change the state. Either to DONE/LOCKED
46e5c68284SPeter Zijlstra * or to IGNORE. DONE/LOCKED means the waiter q is now on the uaddr2 futex
47e5c68284SPeter Zijlstra * and either blocked (DONE) or has acquired it (LOCKED). IGNORE is set by
48e5c68284SPeter Zijlstra * the requeue side when the requeue attempt failed via deadlock detection
49e5c68284SPeter Zijlstra * and therefore the waiter q is still on the uaddr1 futex.
50e5c68284SPeter Zijlstra */
51e5c68284SPeter Zijlstra enum {
52e5c68284SPeter Zijlstra Q_REQUEUE_PI_NONE = 0,
53e5c68284SPeter Zijlstra Q_REQUEUE_PI_IGNORE,
54e5c68284SPeter Zijlstra Q_REQUEUE_PI_IN_PROGRESS,
55e5c68284SPeter Zijlstra Q_REQUEUE_PI_WAIT,
56e5c68284SPeter Zijlstra Q_REQUEUE_PI_DONE,
57e5c68284SPeter Zijlstra Q_REQUEUE_PI_LOCKED,
58e5c68284SPeter Zijlstra };
59e5c68284SPeter Zijlstra
60e5c68284SPeter Zijlstra const struct futex_q futex_q_init = {
61e5c68284SPeter Zijlstra /* list gets initialized in futex_queue()*/
6212a4be50SJens Axboe .wake = futex_wake_mark,
63e5c68284SPeter Zijlstra .key = FUTEX_KEY_INIT,
64e5c68284SPeter Zijlstra .bitset = FUTEX_BITSET_MATCH_ANY,
65e5c68284SPeter Zijlstra .requeue_state = ATOMIC_INIT(Q_REQUEUE_PI_NONE),
66e5c68284SPeter Zijlstra };
67e5c68284SPeter Zijlstra
68e5c68284SPeter Zijlstra /**
69e5c68284SPeter Zijlstra * requeue_futex() - Requeue a futex_q from one hb to another
70e5c68284SPeter Zijlstra * @q: the futex_q to requeue
71e5c68284SPeter Zijlstra * @hb1: the source hash_bucket
72e5c68284SPeter Zijlstra * @hb2: the target hash_bucket
73e5c68284SPeter Zijlstra * @key2: the new key for the requeued futex_q
74e5c68284SPeter Zijlstra */
75e5c68284SPeter Zijlstra static inline
requeue_futex(struct futex_q * q,struct futex_hash_bucket * hb1,struct futex_hash_bucket * hb2,union futex_key * key2)76e5c68284SPeter Zijlstra void requeue_futex(struct futex_q *q, struct futex_hash_bucket *hb1,
77e5c68284SPeter Zijlstra struct futex_hash_bucket *hb2, union futex_key *key2)
78e5c68284SPeter Zijlstra {
79e5c68284SPeter Zijlstra
80e5c68284SPeter Zijlstra /*
81e5c68284SPeter Zijlstra * If key1 and key2 hash to the same bucket, no need to
82e5c68284SPeter Zijlstra * requeue.
83e5c68284SPeter Zijlstra */
84e5c68284SPeter Zijlstra if (likely(&hb1->chain != &hb2->chain)) {
85e5c68284SPeter Zijlstra plist_del(&q->list, &hb1->chain);
86e5c68284SPeter Zijlstra futex_hb_waiters_dec(hb1);
87e5c68284SPeter Zijlstra futex_hb_waiters_inc(hb2);
88e5c68284SPeter Zijlstra plist_add(&q->list, &hb2->chain);
89e5c68284SPeter Zijlstra q->lock_ptr = &hb2->lock;
90e5c68284SPeter Zijlstra }
91e5c68284SPeter Zijlstra q->key = *key2;
92e5c68284SPeter Zijlstra }
93e5c68284SPeter Zijlstra
futex_requeue_pi_prepare(struct futex_q * q,struct futex_pi_state * pi_state)94e5c68284SPeter Zijlstra static inline bool futex_requeue_pi_prepare(struct futex_q *q,
95e5c68284SPeter Zijlstra struct futex_pi_state *pi_state)
96e5c68284SPeter Zijlstra {
97e5c68284SPeter Zijlstra int old, new;
98e5c68284SPeter Zijlstra
99e5c68284SPeter Zijlstra /*
100e5c68284SPeter Zijlstra * Set state to Q_REQUEUE_PI_IN_PROGRESS unless an early wakeup has
101e5c68284SPeter Zijlstra * already set Q_REQUEUE_PI_IGNORE to signal that requeue should
102e5c68284SPeter Zijlstra * ignore the waiter.
103e5c68284SPeter Zijlstra */
104e5c68284SPeter Zijlstra old = atomic_read_acquire(&q->requeue_state);
105e5c68284SPeter Zijlstra do {
106e5c68284SPeter Zijlstra if (old == Q_REQUEUE_PI_IGNORE)
107e5c68284SPeter Zijlstra return false;
108e5c68284SPeter Zijlstra
109e5c68284SPeter Zijlstra /*
110e5c68284SPeter Zijlstra * futex_proxy_trylock_atomic() might have set it to
111e5c68284SPeter Zijlstra * IN_PROGRESS and a interleaved early wake to WAIT.
112e5c68284SPeter Zijlstra *
113e5c68284SPeter Zijlstra * It was considered to have an extra state for that
114e5c68284SPeter Zijlstra * trylock, but that would just add more conditionals
115e5c68284SPeter Zijlstra * all over the place for a dubious value.
116e5c68284SPeter Zijlstra */
117e5c68284SPeter Zijlstra if (old != Q_REQUEUE_PI_NONE)
118e5c68284SPeter Zijlstra break;
119e5c68284SPeter Zijlstra
120e5c68284SPeter Zijlstra new = Q_REQUEUE_PI_IN_PROGRESS;
121e5c68284SPeter Zijlstra } while (!atomic_try_cmpxchg(&q->requeue_state, &old, new));
122e5c68284SPeter Zijlstra
123e5c68284SPeter Zijlstra q->pi_state = pi_state;
124e5c68284SPeter Zijlstra return true;
125e5c68284SPeter Zijlstra }
126e5c68284SPeter Zijlstra
futex_requeue_pi_complete(struct futex_q * q,int locked)127e5c68284SPeter Zijlstra static inline void futex_requeue_pi_complete(struct futex_q *q, int locked)
128e5c68284SPeter Zijlstra {
129e5c68284SPeter Zijlstra int old, new;
130e5c68284SPeter Zijlstra
131e5c68284SPeter Zijlstra old = atomic_read_acquire(&q->requeue_state);
132e5c68284SPeter Zijlstra do {
133e5c68284SPeter Zijlstra if (old == Q_REQUEUE_PI_IGNORE)
134e5c68284SPeter Zijlstra return;
135e5c68284SPeter Zijlstra
136e5c68284SPeter Zijlstra if (locked >= 0) {
137e5c68284SPeter Zijlstra /* Requeue succeeded. Set DONE or LOCKED */
138e5c68284SPeter Zijlstra WARN_ON_ONCE(old != Q_REQUEUE_PI_IN_PROGRESS &&
139e5c68284SPeter Zijlstra old != Q_REQUEUE_PI_WAIT);
140e5c68284SPeter Zijlstra new = Q_REQUEUE_PI_DONE + locked;
141e5c68284SPeter Zijlstra } else if (old == Q_REQUEUE_PI_IN_PROGRESS) {
142e5c68284SPeter Zijlstra /* Deadlock, no early wakeup interleave */
143e5c68284SPeter Zijlstra new = Q_REQUEUE_PI_NONE;
144e5c68284SPeter Zijlstra } else {
145e5c68284SPeter Zijlstra /* Deadlock, early wakeup interleave. */
146e5c68284SPeter Zijlstra WARN_ON_ONCE(old != Q_REQUEUE_PI_WAIT);
147e5c68284SPeter Zijlstra new = Q_REQUEUE_PI_IGNORE;
148e5c68284SPeter Zijlstra }
149e5c68284SPeter Zijlstra } while (!atomic_try_cmpxchg(&q->requeue_state, &old, new));
150e5c68284SPeter Zijlstra
151e5c68284SPeter Zijlstra #ifdef CONFIG_PREEMPT_RT
152e5c68284SPeter Zijlstra /* If the waiter interleaved with the requeue let it know */
153e5c68284SPeter Zijlstra if (unlikely(old == Q_REQUEUE_PI_WAIT))
154e5c68284SPeter Zijlstra rcuwait_wake_up(&q->requeue_wait);
155e5c68284SPeter Zijlstra #endif
156e5c68284SPeter Zijlstra }
157e5c68284SPeter Zijlstra
futex_requeue_pi_wakeup_sync(struct futex_q * q)158e5c68284SPeter Zijlstra static inline int futex_requeue_pi_wakeup_sync(struct futex_q *q)
159e5c68284SPeter Zijlstra {
160e5c68284SPeter Zijlstra int old, new;
161e5c68284SPeter Zijlstra
162e5c68284SPeter Zijlstra old = atomic_read_acquire(&q->requeue_state);
163e5c68284SPeter Zijlstra do {
164e5c68284SPeter Zijlstra /* Is requeue done already? */
165e5c68284SPeter Zijlstra if (old >= Q_REQUEUE_PI_DONE)
166e5c68284SPeter Zijlstra return old;
167e5c68284SPeter Zijlstra
168e5c68284SPeter Zijlstra /*
169e5c68284SPeter Zijlstra * If not done, then tell the requeue code to either ignore
170e5c68284SPeter Zijlstra * the waiter or to wake it up once the requeue is done.
171e5c68284SPeter Zijlstra */
172e5c68284SPeter Zijlstra new = Q_REQUEUE_PI_WAIT;
173e5c68284SPeter Zijlstra if (old == Q_REQUEUE_PI_NONE)
174e5c68284SPeter Zijlstra new = Q_REQUEUE_PI_IGNORE;
175e5c68284SPeter Zijlstra } while (!atomic_try_cmpxchg(&q->requeue_state, &old, new));
176e5c68284SPeter Zijlstra
177e5c68284SPeter Zijlstra /* If the requeue was in progress, wait for it to complete */
178e5c68284SPeter Zijlstra if (old == Q_REQUEUE_PI_IN_PROGRESS) {
179e5c68284SPeter Zijlstra #ifdef CONFIG_PREEMPT_RT
180e5c68284SPeter Zijlstra rcuwait_wait_event(&q->requeue_wait,
181e5c68284SPeter Zijlstra atomic_read(&q->requeue_state) != Q_REQUEUE_PI_WAIT,
182e5c68284SPeter Zijlstra TASK_UNINTERRUPTIBLE);
183e5c68284SPeter Zijlstra #else
184e5c68284SPeter Zijlstra (void)atomic_cond_read_relaxed(&q->requeue_state, VAL != Q_REQUEUE_PI_WAIT);
185e5c68284SPeter Zijlstra #endif
186e5c68284SPeter Zijlstra }
187e5c68284SPeter Zijlstra
188e5c68284SPeter Zijlstra /*
189e5c68284SPeter Zijlstra * Requeue is now either prohibited or complete. Reread state
190e5c68284SPeter Zijlstra * because during the wait above it might have changed. Nothing
191e5c68284SPeter Zijlstra * will modify q->requeue_state after this point.
192e5c68284SPeter Zijlstra */
193e5c68284SPeter Zijlstra return atomic_read(&q->requeue_state);
194e5c68284SPeter Zijlstra }
195e5c68284SPeter Zijlstra
196e5c68284SPeter Zijlstra /**
197e5c68284SPeter Zijlstra * requeue_pi_wake_futex() - Wake a task that acquired the lock during requeue
198e5c68284SPeter Zijlstra * @q: the futex_q
199e5c68284SPeter Zijlstra * @key: the key of the requeue target futex
200e5c68284SPeter Zijlstra * @hb: the hash_bucket of the requeue target futex
201e5c68284SPeter Zijlstra *
202e5c68284SPeter Zijlstra * During futex_requeue, with requeue_pi=1, it is possible to acquire the
203e5c68284SPeter Zijlstra * target futex if it is uncontended or via a lock steal.
204e5c68284SPeter Zijlstra *
205e5c68284SPeter Zijlstra * 1) Set @q::key to the requeue target futex key so the waiter can detect
206e5c68284SPeter Zijlstra * the wakeup on the right futex.
207e5c68284SPeter Zijlstra *
208e5c68284SPeter Zijlstra * 2) Dequeue @q from the hash bucket.
209e5c68284SPeter Zijlstra *
210e5c68284SPeter Zijlstra * 3) Set @q::rt_waiter to NULL so the woken up task can detect atomic lock
211e5c68284SPeter Zijlstra * acquisition.
212e5c68284SPeter Zijlstra *
213e5c68284SPeter Zijlstra * 4) Set the q->lock_ptr to the requeue target hb->lock for the case that
214e5c68284SPeter Zijlstra * the waiter has to fixup the pi state.
215e5c68284SPeter Zijlstra *
216e5c68284SPeter Zijlstra * 5) Complete the requeue state so the waiter can make progress. After
217e5c68284SPeter Zijlstra * this point the waiter task can return from the syscall immediately in
218e5c68284SPeter Zijlstra * case that the pi state does not have to be fixed up.
219e5c68284SPeter Zijlstra *
220e5c68284SPeter Zijlstra * 6) Wake the waiter task.
221e5c68284SPeter Zijlstra *
222e5c68284SPeter Zijlstra * Must be called with both q->lock_ptr and hb->lock held.
223e5c68284SPeter Zijlstra */
224e5c68284SPeter Zijlstra static inline
requeue_pi_wake_futex(struct futex_q * q,union futex_key * key,struct futex_hash_bucket * hb)225e5c68284SPeter Zijlstra void requeue_pi_wake_futex(struct futex_q *q, union futex_key *key,
226e5c68284SPeter Zijlstra struct futex_hash_bucket *hb)
227e5c68284SPeter Zijlstra {
228e5c68284SPeter Zijlstra q->key = *key;
229e5c68284SPeter Zijlstra
230e5c68284SPeter Zijlstra __futex_unqueue(q);
231e5c68284SPeter Zijlstra
232e5c68284SPeter Zijlstra WARN_ON(!q->rt_waiter);
233e5c68284SPeter Zijlstra q->rt_waiter = NULL;
234e5c68284SPeter Zijlstra
235e5c68284SPeter Zijlstra q->lock_ptr = &hb->lock;
236e5c68284SPeter Zijlstra
237e5c68284SPeter Zijlstra /* Signal locked state to the waiter */
238e5c68284SPeter Zijlstra futex_requeue_pi_complete(q, 1);
239e5c68284SPeter Zijlstra wake_up_state(q->task, TASK_NORMAL);
240e5c68284SPeter Zijlstra }
241e5c68284SPeter Zijlstra
242e5c68284SPeter Zijlstra /**
243e5c68284SPeter Zijlstra * futex_proxy_trylock_atomic() - Attempt an atomic lock for the top waiter
244e5c68284SPeter Zijlstra * @pifutex: the user address of the to futex
245e5c68284SPeter Zijlstra * @hb1: the from futex hash bucket, must be locked by the caller
246e5c68284SPeter Zijlstra * @hb2: the to futex hash bucket, must be locked by the caller
247e5c68284SPeter Zijlstra * @key1: the from futex key
248e5c68284SPeter Zijlstra * @key2: the to futex key
249e5c68284SPeter Zijlstra * @ps: address to store the pi_state pointer
250e5c68284SPeter Zijlstra * @exiting: Pointer to store the task pointer of the owner task
251e5c68284SPeter Zijlstra * which is in the middle of exiting
252e5c68284SPeter Zijlstra * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0)
253e5c68284SPeter Zijlstra *
254e5c68284SPeter Zijlstra * Try and get the lock on behalf of the top waiter if we can do it atomically.
255e5c68284SPeter Zijlstra * Wake the top waiter if we succeed. If the caller specified set_waiters,
256e5c68284SPeter Zijlstra * then direct futex_lock_pi_atomic() to force setting the FUTEX_WAITERS bit.
257e5c68284SPeter Zijlstra * hb1 and hb2 must be held by the caller.
258e5c68284SPeter Zijlstra *
259e5c68284SPeter Zijlstra * @exiting is only set when the return value is -EBUSY. If so, this holds
260e5c68284SPeter Zijlstra * a refcount on the exiting task on return and the caller needs to drop it
261e5c68284SPeter Zijlstra * after waiting for the exit to complete.
262e5c68284SPeter Zijlstra *
263e5c68284SPeter Zijlstra * Return:
264e5c68284SPeter Zijlstra * - 0 - failed to acquire the lock atomically;
265e5c68284SPeter Zijlstra * - >0 - acquired the lock, return value is vpid of the top_waiter
266e5c68284SPeter Zijlstra * - <0 - error
267e5c68284SPeter Zijlstra */
268e5c68284SPeter Zijlstra static int
futex_proxy_trylock_atomic(u32 __user * pifutex,struct futex_hash_bucket * hb1,struct futex_hash_bucket * hb2,union futex_key * key1,union futex_key * key2,struct futex_pi_state ** ps,struct task_struct ** exiting,int set_waiters)269e5c68284SPeter Zijlstra futex_proxy_trylock_atomic(u32 __user *pifutex, struct futex_hash_bucket *hb1,
270e5c68284SPeter Zijlstra struct futex_hash_bucket *hb2, union futex_key *key1,
271e5c68284SPeter Zijlstra union futex_key *key2, struct futex_pi_state **ps,
272e5c68284SPeter Zijlstra struct task_struct **exiting, int set_waiters)
273e5c68284SPeter Zijlstra {
27401a99a75SLi zeming struct futex_q *top_waiter;
275e5c68284SPeter Zijlstra u32 curval;
276e5c68284SPeter Zijlstra int ret;
277e5c68284SPeter Zijlstra
278e5c68284SPeter Zijlstra if (futex_get_value_locked(&curval, pifutex))
279e5c68284SPeter Zijlstra return -EFAULT;
280e5c68284SPeter Zijlstra
281e5c68284SPeter Zijlstra if (unlikely(should_fail_futex(true)))
282e5c68284SPeter Zijlstra return -EFAULT;
283e5c68284SPeter Zijlstra
284e5c68284SPeter Zijlstra /*
285e5c68284SPeter Zijlstra * Find the top_waiter and determine if there are additional waiters.
286e5c68284SPeter Zijlstra * If the caller intends to requeue more than 1 waiter to pifutex,
287e5c68284SPeter Zijlstra * force futex_lock_pi_atomic() to set the FUTEX_WAITERS bit now,
288e5c68284SPeter Zijlstra * as we have means to handle the possible fault. If not, don't set
289e5c68284SPeter Zijlstra * the bit unnecessarily as it will force the subsequent unlock to enter
290e5c68284SPeter Zijlstra * the kernel.
291e5c68284SPeter Zijlstra */
292e5c68284SPeter Zijlstra top_waiter = futex_top_waiter(hb1, key1);
293e5c68284SPeter Zijlstra
294e5c68284SPeter Zijlstra /* There are no waiters, nothing for us to do. */
295e5c68284SPeter Zijlstra if (!top_waiter)
296e5c68284SPeter Zijlstra return 0;
297e5c68284SPeter Zijlstra
298e5c68284SPeter Zijlstra /*
299e5c68284SPeter Zijlstra * Ensure that this is a waiter sitting in futex_wait_requeue_pi()
300e5c68284SPeter Zijlstra * and waiting on the 'waitqueue' futex which is always !PI.
301e5c68284SPeter Zijlstra */
302e5c68284SPeter Zijlstra if (!top_waiter->rt_waiter || top_waiter->pi_state)
303e5c68284SPeter Zijlstra return -EINVAL;
304e5c68284SPeter Zijlstra
305e5c68284SPeter Zijlstra /* Ensure we requeue to the expected futex. */
306e5c68284SPeter Zijlstra if (!futex_match(top_waiter->requeue_pi_key, key2))
307e5c68284SPeter Zijlstra return -EINVAL;
308e5c68284SPeter Zijlstra
309e5c68284SPeter Zijlstra /* Ensure that this does not race against an early wakeup */
310e5c68284SPeter Zijlstra if (!futex_requeue_pi_prepare(top_waiter, NULL))
311e5c68284SPeter Zijlstra return -EAGAIN;
312e5c68284SPeter Zijlstra
313e5c68284SPeter Zijlstra /*
314e5c68284SPeter Zijlstra * Try to take the lock for top_waiter and set the FUTEX_WAITERS bit
315e5c68284SPeter Zijlstra * in the contended case or if @set_waiters is true.
316e5c68284SPeter Zijlstra *
317e5c68284SPeter Zijlstra * In the contended case PI state is attached to the lock owner. If
318e5c68284SPeter Zijlstra * the user space lock can be acquired then PI state is attached to
319e5c68284SPeter Zijlstra * the new owner (@top_waiter->task) when @set_waiters is true.
320e5c68284SPeter Zijlstra */
321e5c68284SPeter Zijlstra ret = futex_lock_pi_atomic(pifutex, hb2, key2, ps, top_waiter->task,
322e5c68284SPeter Zijlstra exiting, set_waiters);
323e5c68284SPeter Zijlstra if (ret == 1) {
324e5c68284SPeter Zijlstra /*
325e5c68284SPeter Zijlstra * Lock was acquired in user space and PI state was
326e5c68284SPeter Zijlstra * attached to @top_waiter->task. That means state is fully
327e5c68284SPeter Zijlstra * consistent and the waiter can return to user space
328e5c68284SPeter Zijlstra * immediately after the wakeup.
329e5c68284SPeter Zijlstra */
330e5c68284SPeter Zijlstra requeue_pi_wake_futex(top_waiter, key2, hb2);
331e5c68284SPeter Zijlstra } else if (ret < 0) {
332e5c68284SPeter Zijlstra /* Rewind top_waiter::requeue_state */
333e5c68284SPeter Zijlstra futex_requeue_pi_complete(top_waiter, ret);
334e5c68284SPeter Zijlstra } else {
335e5c68284SPeter Zijlstra /*
336e5c68284SPeter Zijlstra * futex_lock_pi_atomic() did not acquire the user space
337e5c68284SPeter Zijlstra * futex, but managed to establish the proxy lock and pi
338e5c68284SPeter Zijlstra * state. top_waiter::requeue_state cannot be fixed up here
339e5c68284SPeter Zijlstra * because the waiter is not enqueued on the rtmutex
340e5c68284SPeter Zijlstra * yet. This is handled at the callsite depending on the
341e5c68284SPeter Zijlstra * result of rt_mutex_start_proxy_lock() which is
342e5c68284SPeter Zijlstra * guaranteed to be reached with this function returning 0.
343e5c68284SPeter Zijlstra */
344e5c68284SPeter Zijlstra }
345e5c68284SPeter Zijlstra return ret;
346e5c68284SPeter Zijlstra }
347e5c68284SPeter Zijlstra
348e5c68284SPeter Zijlstra /**
349e5c68284SPeter Zijlstra * futex_requeue() - Requeue waiters from uaddr1 to uaddr2
350e5c68284SPeter Zijlstra * @uaddr1: source futex user address
35127b88f35S[email protected] * @flags1: futex flags (FLAGS_SHARED, etc.)
352e5c68284SPeter Zijlstra * @uaddr2: target futex user address
35327b88f35S[email protected] * @flags2: futex flags (FLAGS_SHARED, etc.)
354e5c68284SPeter Zijlstra * @nr_wake: number of waiters to wake (must be 1 for requeue_pi)
355e5c68284SPeter Zijlstra * @nr_requeue: number of waiters to requeue (0-INT_MAX)
356e5c68284SPeter Zijlstra * @cmpval: @uaddr1 expected value (or %NULL)
357e5c68284SPeter Zijlstra * @requeue_pi: if we are attempting to requeue from a non-pi futex to a
358e5c68284SPeter Zijlstra * pi futex (pi to pi requeue is not supported)
359e5c68284SPeter Zijlstra *
360e5c68284SPeter Zijlstra * Requeue waiters on uaddr1 to uaddr2. In the requeue_pi case, try to acquire
361e5c68284SPeter Zijlstra * uaddr2 atomically on behalf of the top waiter.
362e5c68284SPeter Zijlstra *
363e5c68284SPeter Zijlstra * Return:
364e5c68284SPeter Zijlstra * - >=0 - on success, the number of tasks requeued or woken;
365e5c68284SPeter Zijlstra * - <0 - on error
366e5c68284SPeter Zijlstra */
futex_requeue(u32 __user * uaddr1,unsigned int flags1,u32 __user * uaddr2,unsigned int flags2,int nr_wake,int nr_requeue,u32 * cmpval,int requeue_pi)36727b88f35S[email protected] int futex_requeue(u32 __user *uaddr1, unsigned int flags1,
36827b88f35S[email protected] u32 __user *uaddr2, unsigned int flags2,
369e5c68284SPeter Zijlstra int nr_wake, int nr_requeue, u32 *cmpval, int requeue_pi)
370e5c68284SPeter Zijlstra {
371e5c68284SPeter Zijlstra union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
372e5c68284SPeter Zijlstra int task_count = 0, ret;
373e5c68284SPeter Zijlstra struct futex_pi_state *pi_state = NULL;
374e5c68284SPeter Zijlstra struct futex_hash_bucket *hb1, *hb2;
375e5c68284SPeter Zijlstra struct futex_q *this, *next;
376e5c68284SPeter Zijlstra DEFINE_WAKE_Q(wake_q);
377e5c68284SPeter Zijlstra
378e5c68284SPeter Zijlstra if (nr_wake < 0 || nr_requeue < 0)
379e5c68284SPeter Zijlstra return -EINVAL;
380e5c68284SPeter Zijlstra
381e5c68284SPeter Zijlstra /*
382e5c68284SPeter Zijlstra * When PI not supported: return -ENOSYS if requeue_pi is true,
383e5c68284SPeter Zijlstra * consequently the compiler knows requeue_pi is always false past
384e5c68284SPeter Zijlstra * this point which will optimize away all the conditional code
385e5c68284SPeter Zijlstra * further down.
386e5c68284SPeter Zijlstra */
387e5c68284SPeter Zijlstra if (!IS_ENABLED(CONFIG_FUTEX_PI) && requeue_pi)
388e5c68284SPeter Zijlstra return -ENOSYS;
389e5c68284SPeter Zijlstra
390e5c68284SPeter Zijlstra if (requeue_pi) {
391e5c68284SPeter Zijlstra /*
392e5c68284SPeter Zijlstra * Requeue PI only works on two distinct uaddrs. This
393e5c68284SPeter Zijlstra * check is only valid for private futexes. See below.
394e5c68284SPeter Zijlstra */
395e5c68284SPeter Zijlstra if (uaddr1 == uaddr2)
396e5c68284SPeter Zijlstra return -EINVAL;
397e5c68284SPeter Zijlstra
398e5c68284SPeter Zijlstra /*
399e5c68284SPeter Zijlstra * futex_requeue() allows the caller to define the number
400e5c68284SPeter Zijlstra * of waiters to wake up via the @nr_wake argument. With
401e5c68284SPeter Zijlstra * REQUEUE_PI, waking up more than one waiter is creating
402e5c68284SPeter Zijlstra * more problems than it solves. Waking up a waiter makes
403e5c68284SPeter Zijlstra * only sense if the PI futex @uaddr2 is uncontended as
404e5c68284SPeter Zijlstra * this allows the requeue code to acquire the futex
405e5c68284SPeter Zijlstra * @uaddr2 before waking the waiter. The waiter can then
406e5c68284SPeter Zijlstra * return to user space without further action. A secondary
407e5c68284SPeter Zijlstra * wakeup would just make the futex_wait_requeue_pi()
408e5c68284SPeter Zijlstra * handling more complex, because that code would have to
409e5c68284SPeter Zijlstra * look up pi_state and do more or less all the handling
410e5c68284SPeter Zijlstra * which the requeue code has to do for the to be requeued
411e5c68284SPeter Zijlstra * waiters. So restrict the number of waiters to wake to
412e5c68284SPeter Zijlstra * one, and only wake it up when the PI futex is
413e5c68284SPeter Zijlstra * uncontended. Otherwise requeue it and let the unlock of
414e5c68284SPeter Zijlstra * the PI futex handle the wakeup.
415e5c68284SPeter Zijlstra *
416e5c68284SPeter Zijlstra * All REQUEUE_PI users, e.g. pthread_cond_signal() and
417e5c68284SPeter Zijlstra * pthread_cond_broadcast() must use nr_wake=1.
418e5c68284SPeter Zijlstra */
419e5c68284SPeter Zijlstra if (nr_wake != 1)
420e5c68284SPeter Zijlstra return -EINVAL;
421e5c68284SPeter Zijlstra
422e5c68284SPeter Zijlstra /*
423e5c68284SPeter Zijlstra * requeue_pi requires a pi_state, try to allocate it now
424e5c68284SPeter Zijlstra * without any locks in case it fails.
425e5c68284SPeter Zijlstra */
426e5c68284SPeter Zijlstra if (refill_pi_state_cache())
427e5c68284SPeter Zijlstra return -ENOMEM;
428e5c68284SPeter Zijlstra }
429e5c68284SPeter Zijlstra
430e5c68284SPeter Zijlstra retry:
43127b88f35S[email protected] ret = get_futex_key(uaddr1, flags1, &key1, FUTEX_READ);
432e5c68284SPeter Zijlstra if (unlikely(ret != 0))
433e5c68284SPeter Zijlstra return ret;
43427b88f35S[email protected] ret = get_futex_key(uaddr2, flags2, &key2,
435e5c68284SPeter Zijlstra requeue_pi ? FUTEX_WRITE : FUTEX_READ);
436e5c68284SPeter Zijlstra if (unlikely(ret != 0))
437e5c68284SPeter Zijlstra return ret;
438e5c68284SPeter Zijlstra
439e5c68284SPeter Zijlstra /*
440e5c68284SPeter Zijlstra * The check above which compares uaddrs is not sufficient for
441e5c68284SPeter Zijlstra * shared futexes. We need to compare the keys:
442e5c68284SPeter Zijlstra */
443e5c68284SPeter Zijlstra if (requeue_pi && futex_match(&key1, &key2))
444e5c68284SPeter Zijlstra return -EINVAL;
445e5c68284SPeter Zijlstra
446e5c68284SPeter Zijlstra hb1 = futex_hash(&key1);
447e5c68284SPeter Zijlstra hb2 = futex_hash(&key2);
448e5c68284SPeter Zijlstra
449e5c68284SPeter Zijlstra retry_private:
450e5c68284SPeter Zijlstra futex_hb_waiters_inc(hb2);
451e5c68284SPeter Zijlstra double_lock_hb(hb1, hb2);
452e5c68284SPeter Zijlstra
453e5c68284SPeter Zijlstra if (likely(cmpval != NULL)) {
454e5c68284SPeter Zijlstra u32 curval;
455e5c68284SPeter Zijlstra
456e5c68284SPeter Zijlstra ret = futex_get_value_locked(&curval, uaddr1);
457e5c68284SPeter Zijlstra
458e5c68284SPeter Zijlstra if (unlikely(ret)) {
459e5c68284SPeter Zijlstra double_unlock_hb(hb1, hb2);
460e5c68284SPeter Zijlstra futex_hb_waiters_dec(hb2);
461e5c68284SPeter Zijlstra
462e5c68284SPeter Zijlstra ret = get_user(curval, uaddr1);
463e5c68284SPeter Zijlstra if (ret)
464e5c68284SPeter Zijlstra return ret;
465e5c68284SPeter Zijlstra
46627b88f35S[email protected] if (!(flags1 & FLAGS_SHARED))
467e5c68284SPeter Zijlstra goto retry_private;
468e5c68284SPeter Zijlstra
469e5c68284SPeter Zijlstra goto retry;
470e5c68284SPeter Zijlstra }
471e5c68284SPeter Zijlstra if (curval != *cmpval) {
472e5c68284SPeter Zijlstra ret = -EAGAIN;
473e5c68284SPeter Zijlstra goto out_unlock;
474e5c68284SPeter Zijlstra }
475e5c68284SPeter Zijlstra }
476e5c68284SPeter Zijlstra
477e5c68284SPeter Zijlstra if (requeue_pi) {
478e5c68284SPeter Zijlstra struct task_struct *exiting = NULL;
479e5c68284SPeter Zijlstra
480e5c68284SPeter Zijlstra /*
481e5c68284SPeter Zijlstra * Attempt to acquire uaddr2 and wake the top waiter. If we
482e5c68284SPeter Zijlstra * intend to requeue waiters, force setting the FUTEX_WAITERS
483e5c68284SPeter Zijlstra * bit. We force this here where we are able to easily handle
484e5c68284SPeter Zijlstra * faults rather in the requeue loop below.
485e5c68284SPeter Zijlstra *
486e5c68284SPeter Zijlstra * Updates topwaiter::requeue_state if a top waiter exists.
487e5c68284SPeter Zijlstra */
488e5c68284SPeter Zijlstra ret = futex_proxy_trylock_atomic(uaddr2, hb1, hb2, &key1,
489e5c68284SPeter Zijlstra &key2, &pi_state,
490e5c68284SPeter Zijlstra &exiting, nr_requeue);
491e5c68284SPeter Zijlstra
492e5c68284SPeter Zijlstra /*
493e5c68284SPeter Zijlstra * At this point the top_waiter has either taken uaddr2 or
494e5c68284SPeter Zijlstra * is waiting on it. In both cases pi_state has been
495e5c68284SPeter Zijlstra * established and an initial refcount on it. In case of an
496e5c68284SPeter Zijlstra * error there's nothing.
497e5c68284SPeter Zijlstra *
498e5c68284SPeter Zijlstra * The top waiter's requeue_state is up to date:
499e5c68284SPeter Zijlstra *
500e5c68284SPeter Zijlstra * - If the lock was acquired atomically (ret == 1), then
501e5c68284SPeter Zijlstra * the state is Q_REQUEUE_PI_LOCKED.
502e5c68284SPeter Zijlstra *
503e5c68284SPeter Zijlstra * The top waiter has been dequeued and woken up and can
504e5c68284SPeter Zijlstra * return to user space immediately. The kernel/user
505e5c68284SPeter Zijlstra * space state is consistent. In case that there must be
506e5c68284SPeter Zijlstra * more waiters requeued the WAITERS bit in the user
507e5c68284SPeter Zijlstra * space futex is set so the top waiter task has to go
508e5c68284SPeter Zijlstra * into the syscall slowpath to unlock the futex. This
509e5c68284SPeter Zijlstra * will block until this requeue operation has been
510e5c68284SPeter Zijlstra * completed and the hash bucket locks have been
511e5c68284SPeter Zijlstra * dropped.
512e5c68284SPeter Zijlstra *
513e5c68284SPeter Zijlstra * - If the trylock failed with an error (ret < 0) then
514e5c68284SPeter Zijlstra * the state is either Q_REQUEUE_PI_NONE, i.e. "nothing
515e5c68284SPeter Zijlstra * happened", or Q_REQUEUE_PI_IGNORE when there was an
516e5c68284SPeter Zijlstra * interleaved early wakeup.
517e5c68284SPeter Zijlstra *
518e5c68284SPeter Zijlstra * - If the trylock did not succeed (ret == 0) then the
519e5c68284SPeter Zijlstra * state is either Q_REQUEUE_PI_IN_PROGRESS or
520e5c68284SPeter Zijlstra * Q_REQUEUE_PI_WAIT if an early wakeup interleaved.
521e5c68284SPeter Zijlstra * This will be cleaned up in the loop below, which
522e5c68284SPeter Zijlstra * cannot fail because futex_proxy_trylock_atomic() did
523e5c68284SPeter Zijlstra * the same sanity checks for requeue_pi as the loop
524e5c68284SPeter Zijlstra * below does.
525e5c68284SPeter Zijlstra */
526e5c68284SPeter Zijlstra switch (ret) {
527e5c68284SPeter Zijlstra case 0:
528e5c68284SPeter Zijlstra /* We hold a reference on the pi state. */
529e5c68284SPeter Zijlstra break;
530e5c68284SPeter Zijlstra
531e5c68284SPeter Zijlstra case 1:
532e5c68284SPeter Zijlstra /*
533e5c68284SPeter Zijlstra * futex_proxy_trylock_atomic() acquired the user space
534e5c68284SPeter Zijlstra * futex. Adjust task_count.
535e5c68284SPeter Zijlstra */
536e5c68284SPeter Zijlstra task_count++;
537e5c68284SPeter Zijlstra ret = 0;
538e5c68284SPeter Zijlstra break;
539e5c68284SPeter Zijlstra
540e5c68284SPeter Zijlstra /*
541e5c68284SPeter Zijlstra * If the above failed, then pi_state is NULL and
542e5c68284SPeter Zijlstra * waiter::requeue_state is correct.
543e5c68284SPeter Zijlstra */
544e5c68284SPeter Zijlstra case -EFAULT:
545e5c68284SPeter Zijlstra double_unlock_hb(hb1, hb2);
546e5c68284SPeter Zijlstra futex_hb_waiters_dec(hb2);
547e5c68284SPeter Zijlstra ret = fault_in_user_writeable(uaddr2);
548e5c68284SPeter Zijlstra if (!ret)
549e5c68284SPeter Zijlstra goto retry;
550e5c68284SPeter Zijlstra return ret;
551e5c68284SPeter Zijlstra case -EBUSY:
552e5c68284SPeter Zijlstra case -EAGAIN:
553e5c68284SPeter Zijlstra /*
554e5c68284SPeter Zijlstra * Two reasons for this:
555e5c68284SPeter Zijlstra * - EBUSY: Owner is exiting and we just wait for the
556e5c68284SPeter Zijlstra * exit to complete.
557e5c68284SPeter Zijlstra * - EAGAIN: The user space value changed.
558e5c68284SPeter Zijlstra */
559e5c68284SPeter Zijlstra double_unlock_hb(hb1, hb2);
560e5c68284SPeter Zijlstra futex_hb_waiters_dec(hb2);
561e5c68284SPeter Zijlstra /*
562e5c68284SPeter Zijlstra * Handle the case where the owner is in the middle of
563e5c68284SPeter Zijlstra * exiting. Wait for the exit to complete otherwise
564e5c68284SPeter Zijlstra * this task might loop forever, aka. live lock.
565e5c68284SPeter Zijlstra */
566e5c68284SPeter Zijlstra wait_for_owner_exiting(ret, exiting);
567e5c68284SPeter Zijlstra cond_resched();
568e5c68284SPeter Zijlstra goto retry;
569e5c68284SPeter Zijlstra default:
570e5c68284SPeter Zijlstra goto out_unlock;
571e5c68284SPeter Zijlstra }
572e5c68284SPeter Zijlstra }
573e5c68284SPeter Zijlstra
574e5c68284SPeter Zijlstra plist_for_each_entry_safe(this, next, &hb1->chain, list) {
575e5c68284SPeter Zijlstra if (task_count - nr_wake >= nr_requeue)
576e5c68284SPeter Zijlstra break;
577e5c68284SPeter Zijlstra
578e5c68284SPeter Zijlstra if (!futex_match(&this->key, &key1))
579e5c68284SPeter Zijlstra continue;
580e5c68284SPeter Zijlstra
581e5c68284SPeter Zijlstra /*
582e5c68284SPeter Zijlstra * FUTEX_WAIT_REQUEUE_PI and FUTEX_CMP_REQUEUE_PI should always
583e5c68284SPeter Zijlstra * be paired with each other and no other futex ops.
584e5c68284SPeter Zijlstra *
585e5c68284SPeter Zijlstra * We should never be requeueing a futex_q with a pi_state,
586e5c68284SPeter Zijlstra * which is awaiting a futex_unlock_pi().
587e5c68284SPeter Zijlstra */
588e5c68284SPeter Zijlstra if ((requeue_pi && !this->rt_waiter) ||
589e5c68284SPeter Zijlstra (!requeue_pi && this->rt_waiter) ||
590e5c68284SPeter Zijlstra this->pi_state) {
591e5c68284SPeter Zijlstra ret = -EINVAL;
592e5c68284SPeter Zijlstra break;
593e5c68284SPeter Zijlstra }
594e5c68284SPeter Zijlstra
595e5c68284SPeter Zijlstra /* Plain futexes just wake or requeue and are done */
596e5c68284SPeter Zijlstra if (!requeue_pi) {
597e5c68284SPeter Zijlstra if (++task_count <= nr_wake)
59812a4be50SJens Axboe this->wake(&wake_q, this);
599e5c68284SPeter Zijlstra else
600e5c68284SPeter Zijlstra requeue_futex(this, hb1, hb2, &key2);
601e5c68284SPeter Zijlstra continue;
602e5c68284SPeter Zijlstra }
603e5c68284SPeter Zijlstra
604e5c68284SPeter Zijlstra /* Ensure we requeue to the expected futex for requeue_pi. */
605e5c68284SPeter Zijlstra if (!futex_match(this->requeue_pi_key, &key2)) {
606e5c68284SPeter Zijlstra ret = -EINVAL;
607e5c68284SPeter Zijlstra break;
608e5c68284SPeter Zijlstra }
609e5c68284SPeter Zijlstra
610e5c68284SPeter Zijlstra /*
611e5c68284SPeter Zijlstra * Requeue nr_requeue waiters and possibly one more in the case
612e5c68284SPeter Zijlstra * of requeue_pi if we couldn't acquire the lock atomically.
613e5c68284SPeter Zijlstra *
614e5c68284SPeter Zijlstra * Prepare the waiter to take the rt_mutex. Take a refcount
615e5c68284SPeter Zijlstra * on the pi_state and store the pointer in the futex_q
616e5c68284SPeter Zijlstra * object of the waiter.
617e5c68284SPeter Zijlstra */
618e5c68284SPeter Zijlstra get_pi_state(pi_state);
619e5c68284SPeter Zijlstra
620e5c68284SPeter Zijlstra /* Don't requeue when the waiter is already on the way out. */
621e5c68284SPeter Zijlstra if (!futex_requeue_pi_prepare(this, pi_state)) {
622e5c68284SPeter Zijlstra /*
623e5c68284SPeter Zijlstra * Early woken waiter signaled that it is on the
624e5c68284SPeter Zijlstra * way out. Drop the pi_state reference and try the
625e5c68284SPeter Zijlstra * next waiter. @this->pi_state is still NULL.
626e5c68284SPeter Zijlstra */
627e5c68284SPeter Zijlstra put_pi_state(pi_state);
628e5c68284SPeter Zijlstra continue;
629e5c68284SPeter Zijlstra }
630e5c68284SPeter Zijlstra
631e5c68284SPeter Zijlstra ret = rt_mutex_start_proxy_lock(&pi_state->pi_mutex,
632e5c68284SPeter Zijlstra this->rt_waiter,
633e5c68284SPeter Zijlstra this->task);
634e5c68284SPeter Zijlstra
635e5c68284SPeter Zijlstra if (ret == 1) {
636e5c68284SPeter Zijlstra /*
637e5c68284SPeter Zijlstra * We got the lock. We do neither drop the refcount
638e5c68284SPeter Zijlstra * on pi_state nor clear this->pi_state because the
639e5c68284SPeter Zijlstra * waiter needs the pi_state for cleaning up the
640e5c68284SPeter Zijlstra * user space value. It will drop the refcount
641e5c68284SPeter Zijlstra * after doing so. this::requeue_state is updated
642e5c68284SPeter Zijlstra * in the wakeup as well.
643e5c68284SPeter Zijlstra */
644e5c68284SPeter Zijlstra requeue_pi_wake_futex(this, &key2, hb2);
645e5c68284SPeter Zijlstra task_count++;
646e5c68284SPeter Zijlstra } else if (!ret) {
647e5c68284SPeter Zijlstra /* Waiter is queued, move it to hb2 */
648e5c68284SPeter Zijlstra requeue_futex(this, hb1, hb2, &key2);
649e5c68284SPeter Zijlstra futex_requeue_pi_complete(this, 0);
650e5c68284SPeter Zijlstra task_count++;
651e5c68284SPeter Zijlstra } else {
652e5c68284SPeter Zijlstra /*
653e5c68284SPeter Zijlstra * rt_mutex_start_proxy_lock() detected a potential
654e5c68284SPeter Zijlstra * deadlock when we tried to queue that waiter.
655e5c68284SPeter Zijlstra * Drop the pi_state reference which we took above
656e5c68284SPeter Zijlstra * and remove the pointer to the state from the
657e5c68284SPeter Zijlstra * waiters futex_q object.
658e5c68284SPeter Zijlstra */
659e5c68284SPeter Zijlstra this->pi_state = NULL;
660e5c68284SPeter Zijlstra put_pi_state(pi_state);
661e5c68284SPeter Zijlstra futex_requeue_pi_complete(this, ret);
662e5c68284SPeter Zijlstra /*
663e5c68284SPeter Zijlstra * We stop queueing more waiters and let user space
664e5c68284SPeter Zijlstra * deal with the mess.
665e5c68284SPeter Zijlstra */
666e5c68284SPeter Zijlstra break;
667e5c68284SPeter Zijlstra }
668e5c68284SPeter Zijlstra }
669e5c68284SPeter Zijlstra
670e5c68284SPeter Zijlstra /*
671e5c68284SPeter Zijlstra * We took an extra initial reference to the pi_state in
672e5c68284SPeter Zijlstra * futex_proxy_trylock_atomic(). We need to drop it here again.
673e5c68284SPeter Zijlstra */
674e5c68284SPeter Zijlstra put_pi_state(pi_state);
675e5c68284SPeter Zijlstra
676e5c68284SPeter Zijlstra out_unlock:
677e5c68284SPeter Zijlstra double_unlock_hb(hb1, hb2);
678e5c68284SPeter Zijlstra wake_up_q(&wake_q);
679e5c68284SPeter Zijlstra futex_hb_waiters_dec(hb2);
680e5c68284SPeter Zijlstra return ret ? ret : task_count;
681e5c68284SPeter Zijlstra }
682e5c68284SPeter Zijlstra
683e5c68284SPeter Zijlstra /**
684e5c68284SPeter Zijlstra * handle_early_requeue_pi_wakeup() - Handle early wakeup on the initial futex
685e5c68284SPeter Zijlstra * @hb: the hash_bucket futex_q was original enqueued on
686e5c68284SPeter Zijlstra * @q: the futex_q woken while waiting to be requeued
687e5c68284SPeter Zijlstra * @timeout: the timeout associated with the wait (NULL if none)
688e5c68284SPeter Zijlstra *
689e5c68284SPeter Zijlstra * Determine the cause for the early wakeup.
690e5c68284SPeter Zijlstra *
691e5c68284SPeter Zijlstra * Return:
692e5c68284SPeter Zijlstra * -EWOULDBLOCK or -ETIMEDOUT or -ERESTARTNOINTR
693e5c68284SPeter Zijlstra */
694e5c68284SPeter Zijlstra static inline
handle_early_requeue_pi_wakeup(struct futex_hash_bucket * hb,struct futex_q * q,struct hrtimer_sleeper * timeout)695e5c68284SPeter Zijlstra int handle_early_requeue_pi_wakeup(struct futex_hash_bucket *hb,
696e5c68284SPeter Zijlstra struct futex_q *q,
697e5c68284SPeter Zijlstra struct hrtimer_sleeper *timeout)
698e5c68284SPeter Zijlstra {
699e5c68284SPeter Zijlstra int ret;
700e5c68284SPeter Zijlstra
701e5c68284SPeter Zijlstra /*
702e5c68284SPeter Zijlstra * With the hb lock held, we avoid races while we process the wakeup.
703e5c68284SPeter Zijlstra * We only need to hold hb (and not hb2) to ensure atomicity as the
704e5c68284SPeter Zijlstra * wakeup code can't change q.key from uaddr to uaddr2 if we hold hb.
705e5c68284SPeter Zijlstra * It can't be requeued from uaddr2 to something else since we don't
706e5c68284SPeter Zijlstra * support a PI aware source futex for requeue.
707e5c68284SPeter Zijlstra */
708e5c68284SPeter Zijlstra WARN_ON_ONCE(&hb->lock != q->lock_ptr);
709e5c68284SPeter Zijlstra
710e5c68284SPeter Zijlstra /*
711e5c68284SPeter Zijlstra * We were woken prior to requeue by a timeout or a signal.
712e5c68284SPeter Zijlstra * Unqueue the futex_q and determine which it was.
713e5c68284SPeter Zijlstra */
714e5c68284SPeter Zijlstra plist_del(&q->list, &hb->chain);
715e5c68284SPeter Zijlstra futex_hb_waiters_dec(hb);
716e5c68284SPeter Zijlstra
717e5c68284SPeter Zijlstra /* Handle spurious wakeups gracefully */
718e5c68284SPeter Zijlstra ret = -EWOULDBLOCK;
719e5c68284SPeter Zijlstra if (timeout && !timeout->task)
720e5c68284SPeter Zijlstra ret = -ETIMEDOUT;
721e5c68284SPeter Zijlstra else if (signal_pending(current))
722e5c68284SPeter Zijlstra ret = -ERESTARTNOINTR;
723e5c68284SPeter Zijlstra return ret;
724e5c68284SPeter Zijlstra }
725e5c68284SPeter Zijlstra
726e5c68284SPeter Zijlstra /**
727e5c68284SPeter Zijlstra * futex_wait_requeue_pi() - Wait on uaddr and take uaddr2
728e5c68284SPeter Zijlstra * @uaddr: the futex we initially wait on (non-pi)
729e5c68284SPeter Zijlstra * @flags: futex flags (FLAGS_SHARED, FLAGS_CLOCKRT, etc.), they must be
730e5c68284SPeter Zijlstra * the same type, no requeueing from private to shared, etc.
731e5c68284SPeter Zijlstra * @val: the expected value of uaddr
732e5c68284SPeter Zijlstra * @abs_time: absolute timeout
733e5c68284SPeter Zijlstra * @bitset: 32 bit wakeup bitset set by userspace, defaults to all
734e5c68284SPeter Zijlstra * @uaddr2: the pi futex we will take prior to returning to user-space
735e5c68284SPeter Zijlstra *
736e5c68284SPeter Zijlstra * The caller will wait on uaddr and will be requeued by futex_requeue() to
737e5c68284SPeter Zijlstra * uaddr2 which must be PI aware and unique from uaddr. Normal wakeup will wake
738e5c68284SPeter Zijlstra * on uaddr2 and complete the acquisition of the rt_mutex prior to returning to
739e5c68284SPeter Zijlstra * userspace. This ensures the rt_mutex maintains an owner when it has waiters;
740e5c68284SPeter Zijlstra * without one, the pi logic would not know which task to boost/deboost, if
741e5c68284SPeter Zijlstra * there was a need to.
742e5c68284SPeter Zijlstra *
743e5c68284SPeter Zijlstra * We call schedule in futex_wait_queue() when we enqueue and return there
744e5c68284SPeter Zijlstra * via the following--
745e5c68284SPeter Zijlstra * 1) wakeup on uaddr2 after an atomic lock acquisition by futex_requeue()
746e5c68284SPeter Zijlstra * 2) wakeup on uaddr2 after a requeue
747e5c68284SPeter Zijlstra * 3) signal
748e5c68284SPeter Zijlstra * 4) timeout
749e5c68284SPeter Zijlstra *
750e5c68284SPeter Zijlstra * If 3, cleanup and return -ERESTARTNOINTR.
751e5c68284SPeter Zijlstra *
752e5c68284SPeter Zijlstra * If 2, we may then block on trying to take the rt_mutex and return via:
753e5c68284SPeter Zijlstra * 5) successful lock
754e5c68284SPeter Zijlstra * 6) signal
755e5c68284SPeter Zijlstra * 7) timeout
756e5c68284SPeter Zijlstra * 8) other lock acquisition failure
757e5c68284SPeter Zijlstra *
758e5c68284SPeter Zijlstra * If 6, return -EWOULDBLOCK (restarting the syscall would do the same).
759e5c68284SPeter Zijlstra *
760e5c68284SPeter Zijlstra * If 4 or 7, we cleanup and return with -ETIMEDOUT.
761e5c68284SPeter Zijlstra *
762e5c68284SPeter Zijlstra * Return:
763e5c68284SPeter Zijlstra * - 0 - On success;
764e5c68284SPeter Zijlstra * - <0 - On error
765e5c68284SPeter Zijlstra */
futex_wait_requeue_pi(u32 __user * uaddr,unsigned int flags,u32 val,ktime_t * abs_time,u32 bitset,u32 __user * uaddr2)766e5c68284SPeter Zijlstra int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
767e5c68284SPeter Zijlstra u32 val, ktime_t *abs_time, u32 bitset,
768e5c68284SPeter Zijlstra u32 __user *uaddr2)
769e5c68284SPeter Zijlstra {
770e5c68284SPeter Zijlstra struct hrtimer_sleeper timeout, *to;
771e5c68284SPeter Zijlstra struct rt_mutex_waiter rt_waiter;
772e5c68284SPeter Zijlstra struct futex_hash_bucket *hb;
773e5c68284SPeter Zijlstra union futex_key key2 = FUTEX_KEY_INIT;
774e5c68284SPeter Zijlstra struct futex_q q = futex_q_init;
775e5c68284SPeter Zijlstra struct rt_mutex_base *pi_mutex;
776e5c68284SPeter Zijlstra int res, ret;
777e5c68284SPeter Zijlstra
778e5c68284SPeter Zijlstra if (!IS_ENABLED(CONFIG_FUTEX_PI))
779e5c68284SPeter Zijlstra return -ENOSYS;
780e5c68284SPeter Zijlstra
781e5c68284SPeter Zijlstra if (uaddr == uaddr2)
782e5c68284SPeter Zijlstra return -EINVAL;
783e5c68284SPeter Zijlstra
784e5c68284SPeter Zijlstra if (!bitset)
785e5c68284SPeter Zijlstra return -EINVAL;
786e5c68284SPeter Zijlstra
787e5c68284SPeter Zijlstra to = futex_setup_timer(abs_time, &timeout, flags,
788e5c68284SPeter Zijlstra current->timer_slack_ns);
789e5c68284SPeter Zijlstra
790e5c68284SPeter Zijlstra /*
791e5c68284SPeter Zijlstra * The waiter is allocated on our stack, manipulated by the requeue
792e5c68284SPeter Zijlstra * code while we sleep on uaddr.
793e5c68284SPeter Zijlstra */
794e5c68284SPeter Zijlstra rt_mutex_init_waiter(&rt_waiter);
795e5c68284SPeter Zijlstra
7963b63a55fS[email protected] ret = get_futex_key(uaddr2, flags, &key2, FUTEX_WRITE);
797e5c68284SPeter Zijlstra if (unlikely(ret != 0))
798e5c68284SPeter Zijlstra goto out;
799e5c68284SPeter Zijlstra
800e5c68284SPeter Zijlstra q.bitset = bitset;
801e5c68284SPeter Zijlstra q.rt_waiter = &rt_waiter;
802e5c68284SPeter Zijlstra q.requeue_pi_key = &key2;
803e5c68284SPeter Zijlstra
804e5c68284SPeter Zijlstra /*
805e5c68284SPeter Zijlstra * Prepare to wait on uaddr. On success, it holds hb->lock and q
806e5c68284SPeter Zijlstra * is initialized.
807e5c68284SPeter Zijlstra */
808e5c68284SPeter Zijlstra ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
809e5c68284SPeter Zijlstra if (ret)
810e5c68284SPeter Zijlstra goto out;
811e5c68284SPeter Zijlstra
812e5c68284SPeter Zijlstra /*
813e5c68284SPeter Zijlstra * The check above which compares uaddrs is not sufficient for
814e5c68284SPeter Zijlstra * shared futexes. We need to compare the keys:
815e5c68284SPeter Zijlstra */
816e5c68284SPeter Zijlstra if (futex_match(&q.key, &key2)) {
817e5c68284SPeter Zijlstra futex_q_unlock(hb);
818e5c68284SPeter Zijlstra ret = -EINVAL;
819e5c68284SPeter Zijlstra goto out;
820e5c68284SPeter Zijlstra }
821e5c68284SPeter Zijlstra
822e5c68284SPeter Zijlstra /* Queue the futex_q, drop the hb lock, wait for wakeup. */
823e5c68284SPeter Zijlstra futex_wait_queue(hb, &q, to);
824e5c68284SPeter Zijlstra
825e5c68284SPeter Zijlstra switch (futex_requeue_pi_wakeup_sync(&q)) {
826e5c68284SPeter Zijlstra case Q_REQUEUE_PI_IGNORE:
827e5c68284SPeter Zijlstra /* The waiter is still on uaddr1 */
828e5c68284SPeter Zijlstra spin_lock(&hb->lock);
829e5c68284SPeter Zijlstra ret = handle_early_requeue_pi_wakeup(hb, &q, to);
830e5c68284SPeter Zijlstra spin_unlock(&hb->lock);
831e5c68284SPeter Zijlstra break;
832e5c68284SPeter Zijlstra
833e5c68284SPeter Zijlstra case Q_REQUEUE_PI_LOCKED:
834e5c68284SPeter Zijlstra /* The requeue acquired the lock */
835e5c68284SPeter Zijlstra if (q.pi_state && (q.pi_state->owner != current)) {
836e5c68284SPeter Zijlstra spin_lock(q.lock_ptr);
837e5c68284SPeter Zijlstra ret = fixup_pi_owner(uaddr2, &q, true);
838e5c68284SPeter Zijlstra /*
839e5c68284SPeter Zijlstra * Drop the reference to the pi state which the
840e5c68284SPeter Zijlstra * requeue_pi() code acquired for us.
841e5c68284SPeter Zijlstra */
842e5c68284SPeter Zijlstra put_pi_state(q.pi_state);
843e5c68284SPeter Zijlstra spin_unlock(q.lock_ptr);
844e5c68284SPeter Zijlstra /*
845e5c68284SPeter Zijlstra * Adjust the return value. It's either -EFAULT or
846e5c68284SPeter Zijlstra * success (1) but the caller expects 0 for success.
847e5c68284SPeter Zijlstra */
848e5c68284SPeter Zijlstra ret = ret < 0 ? ret : 0;
849e5c68284SPeter Zijlstra }
850e5c68284SPeter Zijlstra break;
851e5c68284SPeter Zijlstra
852e5c68284SPeter Zijlstra case Q_REQUEUE_PI_DONE:
853e5c68284SPeter Zijlstra /* Requeue completed. Current is 'pi_blocked_on' the rtmutex */
854e5c68284SPeter Zijlstra pi_mutex = &q.pi_state->pi_mutex;
855e5c68284SPeter Zijlstra ret = rt_mutex_wait_proxy_lock(pi_mutex, to, &rt_waiter);
856e5c68284SPeter Zijlstra
857fbeb558bSPeter Zijlstra /*
858fbeb558bSPeter Zijlstra * See futex_unlock_pi()'s cleanup: comment.
859fbeb558bSPeter Zijlstra */
860e5c68284SPeter Zijlstra if (ret && !rt_mutex_cleanup_proxy_lock(pi_mutex, &rt_waiter))
861e5c68284SPeter Zijlstra ret = 0;
862e5c68284SPeter Zijlstra
863fbeb558bSPeter Zijlstra spin_lock(q.lock_ptr);
864e5c68284SPeter Zijlstra debug_rt_mutex_free_waiter(&rt_waiter);
865e5c68284SPeter Zijlstra /*
866e5c68284SPeter Zijlstra * Fixup the pi_state owner and possibly acquire the lock if we
867e5c68284SPeter Zijlstra * haven't already.
868e5c68284SPeter Zijlstra */
869e5c68284SPeter Zijlstra res = fixup_pi_owner(uaddr2, &q, !ret);
870e5c68284SPeter Zijlstra /*
871e5c68284SPeter Zijlstra * If fixup_pi_owner() returned an error, propagate that. If it
872e5c68284SPeter Zijlstra * acquired the lock, clear -ETIMEDOUT or -EINTR.
873e5c68284SPeter Zijlstra */
874e5c68284SPeter Zijlstra if (res)
875e5c68284SPeter Zijlstra ret = (res < 0) ? res : 0;
876e5c68284SPeter Zijlstra
877e5c68284SPeter Zijlstra futex_unqueue_pi(&q);
878e5c68284SPeter Zijlstra spin_unlock(q.lock_ptr);
879e5c68284SPeter Zijlstra
880e5c68284SPeter Zijlstra if (ret == -EINTR) {
881e5c68284SPeter Zijlstra /*
882e5c68284SPeter Zijlstra * We've already been requeued, but cannot restart
883e5c68284SPeter Zijlstra * by calling futex_lock_pi() directly. We could
884e5c68284SPeter Zijlstra * restart this syscall, but it would detect that
885e5c68284SPeter Zijlstra * the user space "val" changed and return
886e5c68284SPeter Zijlstra * -EWOULDBLOCK. Save the overhead of the restart
887e5c68284SPeter Zijlstra * and return -EWOULDBLOCK directly.
888e5c68284SPeter Zijlstra */
889e5c68284SPeter Zijlstra ret = -EWOULDBLOCK;
890e5c68284SPeter Zijlstra }
891e5c68284SPeter Zijlstra break;
892e5c68284SPeter Zijlstra default:
893e5c68284SPeter Zijlstra BUG();
894e5c68284SPeter Zijlstra }
895e5c68284SPeter Zijlstra
896e5c68284SPeter Zijlstra out:
897e5c68284SPeter Zijlstra if (to) {
898e5c68284SPeter Zijlstra hrtimer_cancel(&to->timer);
899e5c68284SPeter Zijlstra destroy_hrtimer_on_stack(&to->timer);
900e5c68284SPeter Zijlstra }
901e5c68284SPeter Zijlstra return ret;
902e5c68284SPeter Zijlstra }
903e5c68284SPeter Zijlstra
904