xref: /openbmc/linux/kernel/futex/waitwake.c (revision a046f1a0d3e320cfee6bdac336416a537f49e7c6)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 #include <linux/sched/task.h>
4 #include <linux/sched/signal.h>
5 #include <linux/freezer.h>
6 
7 #include "futex.h"
8 
9 /*
10  * READ this before attempting to hack on futexes!
11  *
12  * Basic futex operation and ordering guarantees
13  * =============================================
14  *
15  * The waiter reads the futex value in user space and calls
16  * futex_wait(). This function computes the hash bucket and acquires
17  * the hash bucket lock. After that it reads the futex user space value
18  * again and verifies that the data has not changed. If it has not changed
19  * it enqueues itself into the hash bucket, releases the hash bucket lock
20  * and schedules.
21  *
22  * The waker side modifies the user space value of the futex and calls
23  * futex_wake(). This function computes the hash bucket and acquires the
24  * hash bucket lock. Then it looks for waiters on that futex in the hash
25  * bucket and wakes them.
26  *
27  * In futex wake up scenarios where no tasks are blocked on a futex, taking
28  * the hb spinlock can be avoided and simply return. In order for this
29  * optimization to work, ordering guarantees must exist so that the waiter
30  * being added to the list is acknowledged when the list is concurrently being
31  * checked by the waker, avoiding scenarios like the following:
32  *
33  * CPU 0                               CPU 1
34  * val = *futex;
35  * sys_futex(WAIT, futex, val);
36  *   futex_wait(futex, val);
37  *   uval = *futex;
38  *                                     *futex = newval;
39  *                                     sys_futex(WAKE, futex);
40  *                                       futex_wake(futex);
41  *                                       if (queue_empty())
42  *                                         return;
43  *   if (uval == val)
44  *      lock(hash_bucket(futex));
45  *      queue();
46  *     unlock(hash_bucket(futex));
47  *     schedule();
48  *
49  * This would cause the waiter on CPU 0 to wait forever because it
50  * missed the transition of the user space value from val to newval
51  * and the waker did not find the waiter in the hash bucket queue.
52  *
53  * The correct serialization ensures that a waiter either observes
54  * the changed user space value before blocking or is woken by a
55  * concurrent waker:
56  *
57  * CPU 0                                 CPU 1
58  * val = *futex;
59  * sys_futex(WAIT, futex, val);
60  *   futex_wait(futex, val);
61  *
62  *   waiters++; (a)
63  *   smp_mb(); (A) <-- paired with -.
64  *                                  |
65  *   lock(hash_bucket(futex));      |
66  *                                  |
67  *   uval = *futex;                 |
68  *                                  |        *futex = newval;
69  *                                  |        sys_futex(WAKE, futex);
70  *                                  |          futex_wake(futex);
71  *                                  |
72  *                                  `--------> smp_mb(); (B)
73  *   if (uval == val)
74  *     queue();
75  *     unlock(hash_bucket(futex));
76  *     schedule();                         if (waiters)
77  *                                           lock(hash_bucket(futex));
78  *   else                                    wake_waiters(futex);
79  *     waiters--; (b)                        unlock(hash_bucket(futex));
80  *
81  * Where (A) orders the waiters increment and the futex value read through
82  * atomic operations (see futex_hb_waiters_inc) and where (B) orders the write
83  * to futex and the waiters read (see futex_hb_waiters_pending()).
84  *
85  * This yields the following case (where X:=waiters, Y:=futex):
86  *
87  *	X = Y = 0
88  *
89  *	w[X]=1		w[Y]=1
90  *	MB		MB
91  *	r[Y]=y		r[X]=x
92  *
93  * Which guarantees that x==0 && y==0 is impossible; which translates back into
94  * the guarantee that we cannot both miss the futex variable change and the
95  * enqueue.
96  *
97  * Note that a new waiter is accounted for in (a) even when it is possible that
98  * the wait call can return error, in which case we backtrack from it in (b).
99  * Refer to the comment in futex_q_lock().
100  *
101  * Similarly, in order to account for waiters being requeued on another
102  * address we always increment the waiters for the destination bucket before
103  * acquiring the lock. It then decrements them again  after releasing it -
104  * the code that actually moves the futex(es) between hash buckets (requeue_futex)
105  * will do the additional required waiter count housekeeping. This is done for
106  * double_lock_hb() and double_unlock_hb(), respectively.
107  */
108 
109 /*
110  * The hash bucket lock must be held when this is called.
111  * Afterwards, the futex_q must not be accessed. Callers
112  * must ensure to later call wake_up_q() for the actual
113  * wakeups to occur.
114  */
115 void futex_wake_mark(struct wake_q_head *wake_q, struct futex_q *q)
116 {
117 	struct task_struct *p = q->task;
118 
119 	if (WARN(q->pi_state || q->rt_waiter, "refusing to wake PI futex\n"))
120 		return;
121 
122 	get_task_struct(p);
123 	__futex_unqueue(q);
124 	/*
125 	 * The waiting task can free the futex_q as soon as q->lock_ptr = NULL
126 	 * is written, without taking any locks. This is possible in the event
127 	 * of a spurious wakeup, for example. A memory barrier is required here
128 	 * to prevent the following store to lock_ptr from getting ahead of the
129 	 * plist_del in __futex_unqueue().
130 	 */
131 	smp_store_release(&q->lock_ptr, NULL);
132 
133 	/*
134 	 * Queue the task for later wakeup for after we've released
135 	 * the hb->lock.
136 	 */
137 	wake_q_add_safe(wake_q, p);
138 }
139 
140 /*
141  * Wake up waiters matching bitset queued on this futex (uaddr).
142  */
143 int futex_wake(u32 __user *uaddr, unsigned int flags, int nr_wake, u32 bitset)
144 {
145 	struct futex_hash_bucket *hb;
146 	struct futex_q *this, *next;
147 	union futex_key key = FUTEX_KEY_INIT;
148 	int ret;
149 	DEFINE_WAKE_Q(wake_q);
150 
151 	if (!bitset)
152 		return -EINVAL;
153 
154 	ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, FUTEX_READ);
155 	if (unlikely(ret != 0))
156 		return ret;
157 
158 	hb = futex_hash(&key);
159 
160 	/* Make sure we really have tasks to wakeup */
161 	if (!futex_hb_waiters_pending(hb))
162 		return ret;
163 
164 	spin_lock(&hb->lock);
165 
166 	plist_for_each_entry_safe(this, next, &hb->chain, list) {
167 		if (futex_match (&this->key, &key)) {
168 			if (this->pi_state || this->rt_waiter) {
169 				ret = -EINVAL;
170 				break;
171 			}
172 
173 			/* Check if one of the bits is set in both bitsets */
174 			if (!(this->bitset & bitset))
175 				continue;
176 
177 			futex_wake_mark(&wake_q, this);
178 			if (++ret >= nr_wake)
179 				break;
180 		}
181 	}
182 
183 	spin_unlock(&hb->lock);
184 	wake_up_q(&wake_q);
185 	return ret;
186 }
187 
188 static int futex_atomic_op_inuser(unsigned int encoded_op, u32 __user *uaddr)
189 {
190 	unsigned int op =	  (encoded_op & 0x70000000) >> 28;
191 	unsigned int cmp =	  (encoded_op & 0x0f000000) >> 24;
192 	int oparg = sign_extend32((encoded_op & 0x00fff000) >> 12, 11);
193 	int cmparg = sign_extend32(encoded_op & 0x00000fff, 11);
194 	int oldval, ret;
195 
196 	if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28)) {
197 		if (oparg < 0 || oparg > 31) {
198 			char comm[sizeof(current->comm)];
199 			/*
200 			 * kill this print and return -EINVAL when userspace
201 			 * is sane again
202 			 */
203 			pr_info_ratelimited("futex_wake_op: %s tries to shift op by %d; fix this program\n",
204 					get_task_comm(comm, current), oparg);
205 			oparg &= 31;
206 		}
207 		oparg = 1 << oparg;
208 	}
209 
210 	pagefault_disable();
211 	ret = arch_futex_atomic_op_inuser(op, oparg, &oldval, uaddr);
212 	pagefault_enable();
213 	if (ret)
214 		return ret;
215 
216 	switch (cmp) {
217 	case FUTEX_OP_CMP_EQ:
218 		return oldval == cmparg;
219 	case FUTEX_OP_CMP_NE:
220 		return oldval != cmparg;
221 	case FUTEX_OP_CMP_LT:
222 		return oldval < cmparg;
223 	case FUTEX_OP_CMP_GE:
224 		return oldval >= cmparg;
225 	case FUTEX_OP_CMP_LE:
226 		return oldval <= cmparg;
227 	case FUTEX_OP_CMP_GT:
228 		return oldval > cmparg;
229 	default:
230 		return -ENOSYS;
231 	}
232 }
233 
234 /*
235  * Wake up all waiters hashed on the physical page that is mapped
236  * to this virtual address:
237  */
238 int futex_wake_op(u32 __user *uaddr1, unsigned int flags, u32 __user *uaddr2,
239 		  int nr_wake, int nr_wake2, int op)
240 {
241 	union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
242 	struct futex_hash_bucket *hb1, *hb2;
243 	struct futex_q *this, *next;
244 	int ret, op_ret;
245 	DEFINE_WAKE_Q(wake_q);
246 
247 retry:
248 	ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, FUTEX_READ);
249 	if (unlikely(ret != 0))
250 		return ret;
251 	ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, FUTEX_WRITE);
252 	if (unlikely(ret != 0))
253 		return ret;
254 
255 	hb1 = futex_hash(&key1);
256 	hb2 = futex_hash(&key2);
257 
258 retry_private:
259 	double_lock_hb(hb1, hb2);
260 	op_ret = futex_atomic_op_inuser(op, uaddr2);
261 	if (unlikely(op_ret < 0)) {
262 		double_unlock_hb(hb1, hb2);
263 
264 		if (!IS_ENABLED(CONFIG_MMU) ||
265 		    unlikely(op_ret != -EFAULT && op_ret != -EAGAIN)) {
266 			/*
267 			 * we don't get EFAULT from MMU faults if we don't have
268 			 * an MMU, but we might get them from range checking
269 			 */
270 			ret = op_ret;
271 			return ret;
272 		}
273 
274 		if (op_ret == -EFAULT) {
275 			ret = fault_in_user_writeable(uaddr2);
276 			if (ret)
277 				return ret;
278 		}
279 
280 		cond_resched();
281 		if (!(flags & FLAGS_SHARED))
282 			goto retry_private;
283 		goto retry;
284 	}
285 
286 	plist_for_each_entry_safe(this, next, &hb1->chain, list) {
287 		if (futex_match (&this->key, &key1)) {
288 			if (this->pi_state || this->rt_waiter) {
289 				ret = -EINVAL;
290 				goto out_unlock;
291 			}
292 			futex_wake_mark(&wake_q, this);
293 			if (++ret >= nr_wake)
294 				break;
295 		}
296 	}
297 
298 	if (op_ret > 0) {
299 		op_ret = 0;
300 		plist_for_each_entry_safe(this, next, &hb2->chain, list) {
301 			if (futex_match (&this->key, &key2)) {
302 				if (this->pi_state || this->rt_waiter) {
303 					ret = -EINVAL;
304 					goto out_unlock;
305 				}
306 				futex_wake_mark(&wake_q, this);
307 				if (++op_ret >= nr_wake2)
308 					break;
309 			}
310 		}
311 		ret += op_ret;
312 	}
313 
314 out_unlock:
315 	double_unlock_hb(hb1, hb2);
316 	wake_up_q(&wake_q);
317 	return ret;
318 }
319 
320 static long futex_wait_restart(struct restart_block *restart);
321 
322 /**
323  * futex_wait_queue() - futex_queue() and wait for wakeup, timeout, or signal
324  * @hb:		the futex hash bucket, must be locked by the caller
325  * @q:		the futex_q to queue up on
326  * @timeout:	the prepared hrtimer_sleeper, or null for no timeout
327  */
328 void futex_wait_queue(struct futex_hash_bucket *hb, struct futex_q *q,
329 			    struct hrtimer_sleeper *timeout)
330 {
331 	/*
332 	 * The task state is guaranteed to be set before another task can
333 	 * wake it. set_current_state() is implemented using smp_store_mb() and
334 	 * futex_queue() calls spin_unlock() upon completion, both serializing
335 	 * access to the hash list and forcing another memory barrier.
336 	 */
337 	set_current_state(TASK_INTERRUPTIBLE);
338 	futex_queue(q, hb);
339 
340 	/* Arm the timer */
341 	if (timeout)
342 		hrtimer_sleeper_start_expires(timeout, HRTIMER_MODE_ABS);
343 
344 	/*
345 	 * If we have been removed from the hash list, then another task
346 	 * has tried to wake us, and we can skip the call to schedule().
347 	 */
348 	if (likely(!plist_node_empty(&q->list))) {
349 		/*
350 		 * If the timer has already expired, current will already be
351 		 * flagged for rescheduling. Only call schedule if there
352 		 * is no timeout, or if it has yet to expire.
353 		 */
354 		if (!timeout || timeout->task)
355 			freezable_schedule();
356 	}
357 	__set_current_state(TASK_RUNNING);
358 }
359 
360 /**
361  * futex_wait_setup() - Prepare to wait on a futex
362  * @uaddr:	the futex userspace address
363  * @val:	the expected value
364  * @flags:	futex flags (FLAGS_SHARED, etc.)
365  * @q:		the associated futex_q
366  * @hb:		storage for hash_bucket pointer to be returned to caller
367  *
368  * Setup the futex_q and locate the hash_bucket.  Get the futex value and
369  * compare it with the expected value.  Handle atomic faults internally.
370  * Return with the hb lock held on success, and unlocked on failure.
371  *
372  * Return:
373  *  -  0 - uaddr contains val and hb has been locked;
374  *  - <1 - -EFAULT or -EWOULDBLOCK (uaddr does not contain val) and hb is unlocked
375  */
376 int futex_wait_setup(u32 __user *uaddr, u32 val, unsigned int flags,
377 		     struct futex_q *q, struct futex_hash_bucket **hb)
378 {
379 	u32 uval;
380 	int ret;
381 
382 	/*
383 	 * Access the page AFTER the hash-bucket is locked.
384 	 * Order is important:
385 	 *
386 	 *   Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val);
387 	 *   Userspace waker:  if (cond(var)) { var = new; futex_wake(&var); }
388 	 *
389 	 * The basic logical guarantee of a futex is that it blocks ONLY
390 	 * if cond(var) is known to be true at the time of blocking, for
391 	 * any cond.  If we locked the hash-bucket after testing *uaddr, that
392 	 * would open a race condition where we could block indefinitely with
393 	 * cond(var) false, which would violate the guarantee.
394 	 *
395 	 * On the other hand, we insert q and release the hash-bucket only
396 	 * after testing *uaddr.  This guarantees that futex_wait() will NOT
397 	 * absorb a wakeup if *uaddr does not match the desired values
398 	 * while the syscall executes.
399 	 */
400 retry:
401 	ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q->key, FUTEX_READ);
402 	if (unlikely(ret != 0))
403 		return ret;
404 
405 retry_private:
406 	*hb = futex_q_lock(q);
407 
408 	ret = futex_get_value_locked(&uval, uaddr);
409 
410 	if (ret) {
411 		futex_q_unlock(*hb);
412 
413 		ret = get_user(uval, uaddr);
414 		if (ret)
415 			return ret;
416 
417 		if (!(flags & FLAGS_SHARED))
418 			goto retry_private;
419 
420 		goto retry;
421 	}
422 
423 	if (uval != val) {
424 		futex_q_unlock(*hb);
425 		ret = -EWOULDBLOCK;
426 	}
427 
428 	return ret;
429 }
430 
431 int futex_wait(u32 __user *uaddr, unsigned int flags, u32 val, ktime_t *abs_time, u32 bitset)
432 {
433 	struct hrtimer_sleeper timeout, *to;
434 	struct restart_block *restart;
435 	struct futex_hash_bucket *hb;
436 	struct futex_q q = futex_q_init;
437 	int ret;
438 
439 	if (!bitset)
440 		return -EINVAL;
441 	q.bitset = bitset;
442 
443 	to = futex_setup_timer(abs_time, &timeout, flags,
444 			       current->timer_slack_ns);
445 retry:
446 	/*
447 	 * Prepare to wait on uaddr. On success, it holds hb->lock and q
448 	 * is initialized.
449 	 */
450 	ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
451 	if (ret)
452 		goto out;
453 
454 	/* futex_queue and wait for wakeup, timeout, or a signal. */
455 	futex_wait_queue(hb, &q, to);
456 
457 	/* If we were woken (and unqueued), we succeeded, whatever. */
458 	ret = 0;
459 	if (!futex_unqueue(&q))
460 		goto out;
461 	ret = -ETIMEDOUT;
462 	if (to && !to->task)
463 		goto out;
464 
465 	/*
466 	 * We expect signal_pending(current), but we might be the
467 	 * victim of a spurious wakeup as well.
468 	 */
469 	if (!signal_pending(current))
470 		goto retry;
471 
472 	ret = -ERESTARTSYS;
473 	if (!abs_time)
474 		goto out;
475 
476 	restart = &current->restart_block;
477 	restart->futex.uaddr = uaddr;
478 	restart->futex.val = val;
479 	restart->futex.time = *abs_time;
480 	restart->futex.bitset = bitset;
481 	restart->futex.flags = flags | FLAGS_HAS_TIMEOUT;
482 
483 	ret = set_restart_fn(restart, futex_wait_restart);
484 
485 out:
486 	if (to) {
487 		hrtimer_cancel(&to->timer);
488 		destroy_hrtimer_on_stack(&to->timer);
489 	}
490 	return ret;
491 }
492 
493 static long futex_wait_restart(struct restart_block *restart)
494 {
495 	u32 __user *uaddr = restart->futex.uaddr;
496 	ktime_t t, *tp = NULL;
497 
498 	if (restart->futex.flags & FLAGS_HAS_TIMEOUT) {
499 		t = restart->futex.time;
500 		tp = &t;
501 	}
502 	restart->fn = do_no_restart_syscall;
503 
504 	return (long)futex_wait(uaddr, restart->futex.flags,
505 				restart->futex.val, tp, restart->futex.bitset);
506 }
507 
508