blob: 82cd270f25a0cb3d0fe3a2e31e651e583961e742 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Fast Userspace Mutexes (which I call "Futexes!").
4 * (C) Rusty Russell, IBM 2002
5 *
6 * Generalized futexes, futex requeueing, misc fixes by Ingo Molnar
7 * (C) Copyright 2003 Red Hat Inc, All Rights Reserved
8 *
9 * Removed page pinning, fix privately mapped COW pages and other cleanups
10 * (C) Copyright 2003, 2004 Jamie Lokier
11 *
Ingo Molnar0771dfe2006-03-27 01:16:22 -080012 * Robust futex support started by Ingo Molnar
13 * (C) Copyright 2006 Red Hat Inc, All Rights Reserved
14 * Thanks to Thomas Gleixner for suggestions, analysis and fixes.
15 *
Ingo Molnarc87e2832006-06-27 02:54:58 -070016 * PI-futex support started by Ingo Molnar and Thomas Gleixner
17 * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
18 * Copyright (C) 2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
19 *
Eric Dumazet34f01cc2007-05-09 02:35:04 -070020 * PRIVATE futexes by Eric Dumazet
21 * Copyright (C) 2007 Eric Dumazet <dada1@cosmosbay.com>
22 *
Darren Hart52400ba2009-04-03 13:40:49 -070023 * Requeue-PI support by Darren Hart <dvhltc@us.ibm.com>
24 * Copyright (C) IBM Corporation, 2009
25 * Thanks to Thomas Gleixner for conceptual design and careful reviews.
26 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 * Thanks to Ben LaHaise for yelling "hashed waitqueues" loudly
28 * enough at me, Linus for the original (flawed) idea, Matthew
29 * Kirkwood for proof-of-concept implementation.
30 *
31 * "The futexes are also cursed."
32 * "But they come in a choice of three flavours!"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
Arnd Bergmann04e77122018-04-17 16:31:07 +020034#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/jhash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/pagemap.h>
37#include <linux/syscalls.h>
Colin Cross88c80042013-05-01 18:35:05 -070038#include <linux/freezer.h>
Mike Rapoport57c8a662018-10-30 15:09:49 -070039#include <linux/memblock.h>
Davidlohr Buesoab51fba2015-06-29 23:26:02 -070040#include <linux/fault-inject.h>
Andrei Vaginc2f7d082020-10-15 09:00:19 -070041#include <linux/time_namespace.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070042
Jakub Jelinek4732efbe2005-09-06 15:16:25 -070043#include <asm/futex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Peter Zijlstra1696a8b2013-10-31 18:18:19 +010045#include "locking/rtmutex_common.h"
Ingo Molnarc87e2832006-06-27 02:54:58 -070046
Thomas Gleixner99b60ce2014-01-12 15:31:24 -080047/*
Davidlohr Buesod7e8af12014-04-09 11:55:07 -070048 * READ this before attempting to hack on futexes!
49 *
50 * Basic futex operation and ordering guarantees
51 * =============================================
Thomas Gleixner99b60ce2014-01-12 15:31:24 -080052 *
53 * The waiter reads the futex value in user space and calls
54 * futex_wait(). This function computes the hash bucket and acquires
55 * the hash bucket lock. After that it reads the futex user space value
Davidlohr Buesob0c29f72014-01-12 15:31:25 -080056 * again and verifies that the data has not changed. If it has not changed
57 * it enqueues itself into the hash bucket, releases the hash bucket lock
58 * and schedules.
Thomas Gleixner99b60ce2014-01-12 15:31:24 -080059 *
60 * The waker side modifies the user space value of the futex and calls
Davidlohr Buesob0c29f72014-01-12 15:31:25 -080061 * futex_wake(). This function computes the hash bucket and acquires the
62 * hash bucket lock. Then it looks for waiters on that futex in the hash
63 * bucket and wakes them.
Thomas Gleixner99b60ce2014-01-12 15:31:24 -080064 *
Davidlohr Buesob0c29f72014-01-12 15:31:25 -080065 * In futex wake up scenarios where no tasks are blocked on a futex, taking
66 * the hb spinlock can be avoided and simply return. In order for this
67 * optimization to work, ordering guarantees must exist so that the waiter
68 * being added to the list is acknowledged when the list is concurrently being
69 * checked by the waker, avoiding scenarios like the following:
Thomas Gleixner99b60ce2014-01-12 15:31:24 -080070 *
71 * CPU 0 CPU 1
72 * val = *futex;
73 * sys_futex(WAIT, futex, val);
74 * futex_wait(futex, val);
75 * uval = *futex;
76 * *futex = newval;
77 * sys_futex(WAKE, futex);
78 * futex_wake(futex);
79 * if (queue_empty())
80 * return;
81 * if (uval == val)
82 * lock(hash_bucket(futex));
83 * queue();
84 * unlock(hash_bucket(futex));
85 * schedule();
86 *
87 * This would cause the waiter on CPU 0 to wait forever because it
88 * missed the transition of the user space value from val to newval
89 * and the waker did not find the waiter in the hash bucket queue.
Thomas Gleixner99b60ce2014-01-12 15:31:24 -080090 *
Davidlohr Buesob0c29f72014-01-12 15:31:25 -080091 * The correct serialization ensures that a waiter either observes
92 * the changed user space value before blocking or is woken by a
93 * concurrent waker:
94 *
95 * CPU 0 CPU 1
Thomas Gleixner99b60ce2014-01-12 15:31:24 -080096 * val = *futex;
97 * sys_futex(WAIT, futex, val);
98 * futex_wait(futex, val);
Davidlohr Buesob0c29f72014-01-12 15:31:25 -080099 *
Davidlohr Buesod7e8af12014-04-09 11:55:07 -0700100 * waiters++; (a)
Davidlohr Bueso8ad7b372016-02-09 11:15:13 -0800101 * smp_mb(); (A) <-- paired with -.
102 * |
103 * lock(hash_bucket(futex)); |
104 * |
105 * uval = *futex; |
106 * | *futex = newval;
107 * | sys_futex(WAKE, futex);
108 * | futex_wake(futex);
109 * |
110 * `--------> smp_mb(); (B)
Thomas Gleixner99b60ce2014-01-12 15:31:24 -0800111 * if (uval == val)
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800112 * queue();
Thomas Gleixner99b60ce2014-01-12 15:31:24 -0800113 * unlock(hash_bucket(futex));
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800114 * schedule(); if (waiters)
115 * lock(hash_bucket(futex));
Davidlohr Buesod7e8af12014-04-09 11:55:07 -0700116 * else wake_waiters(futex);
117 * waiters--; (b) unlock(hash_bucket(futex));
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800118 *
Davidlohr Buesod7e8af12014-04-09 11:55:07 -0700119 * Where (A) orders the waiters increment and the futex value read through
120 * atomic operations (see hb_waiters_inc) and where (B) orders the write
Peter Zijlstra4b39f992020-03-04 13:24:24 +0100121 * to futex and the waiters read (see hb_waiters_pending()).
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800122 *
123 * This yields the following case (where X:=waiters, Y:=futex):
124 *
125 * X = Y = 0
126 *
127 * w[X]=1 w[Y]=1
128 * MB MB
129 * r[Y]=y r[X]=x
130 *
131 * Which guarantees that x==0 && y==0 is impossible; which translates back into
132 * the guarantee that we cannot both miss the futex variable change and the
133 * enqueue.
Davidlohr Buesod7e8af12014-04-09 11:55:07 -0700134 *
135 * Note that a new waiter is accounted for in (a) even when it is possible that
136 * the wait call can return error, in which case we backtrack from it in (b).
137 * Refer to the comment in queue_lock().
138 *
139 * Similarly, in order to account for waiters being requeued on another
140 * address we always increment the waiters for the destination bucket before
141 * acquiring the lock. It then decrements them again after releasing it -
142 * the code that actually moves the futex(es) between hash buckets (requeue_futex)
143 * will do the additional required waiter count housekeeping. This is done for
144 * double_lock_hb() and double_unlock_hb(), respectively.
Thomas Gleixner99b60ce2014-01-12 15:31:24 -0800145 */
146
Arnd Bergmann04e77122018-04-17 16:31:07 +0200147#ifdef CONFIG_HAVE_FUTEX_CMPXCHG
148#define futex_cmpxchg_enabled 1
149#else
150static int __read_mostly futex_cmpxchg_enabled;
Heiko Carstens03b8c7b2014-03-02 13:09:47 +0100151#endif
Thomas Gleixnera0c1e902008-02-23 15:23:57 -0800152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/*
Darren Hartb41277d2010-11-08 13:10:09 -0800154 * Futex flags used to encode options to functions and preserve them across
155 * restarts.
156 */
Thomas Gleixner784bdf32016-07-29 16:32:30 +0200157#ifdef CONFIG_MMU
158# define FLAGS_SHARED 0x01
159#else
160/*
161 * NOMMU does not have per process address space. Let the compiler optimize
162 * code away.
163 */
164# define FLAGS_SHARED 0x00
165#endif
Darren Hartb41277d2010-11-08 13:10:09 -0800166#define FLAGS_CLOCKRT 0x02
167#define FLAGS_HAS_TIMEOUT 0x04
168
169/*
Ingo Molnarc87e2832006-06-27 02:54:58 -0700170 * Priority Inheritance state:
171 */
172struct futex_pi_state {
173 /*
174 * list of 'owned' pi_state instances - these have to be
175 * cleaned up in do_exit() if the task exits prematurely:
176 */
177 struct list_head list;
178
179 /*
180 * The PI object:
181 */
Peter Zijlstra830e6ac2021-08-15 23:27:58 +0200182 struct rt_mutex_base pi_mutex;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700183
184 struct task_struct *owner;
Elena Reshetova49262de2019-02-05 14:24:27 +0200185 refcount_t refcount;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700186
187 union futex_key key;
Kees Cook3859a272016-10-28 01:22:25 -0700188} __randomize_layout;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700189
Darren Hartd8d88fb2009-09-21 22:30:30 -0700190/**
191 * struct futex_q - The hashed futex queue entry, one per waiting task
Randy Dunlapfb62db22010-10-13 11:02:34 -0700192 * @list: priority-sorted list of tasks waiting on this futex
Darren Hartd8d88fb2009-09-21 22:30:30 -0700193 * @task: the task waiting on the futex
194 * @lock_ptr: the hash bucket lock
195 * @key: the key the futex is hashed on
196 * @pi_state: optional priority inheritance state
197 * @rt_waiter: rt_waiter storage for use with requeue_pi
198 * @requeue_pi_key: the requeue_pi target futex key
199 * @bitset: bitset for the optional bitmasked wakeup
Thomas Gleixner07d91ef52021-08-15 23:29:18 +0200200 * @requeue_state: State field for futex_requeue_pi()
201 * @requeue_wait: RCU wait for futex_requeue_pi() (RT only)
Darren Hartd8d88fb2009-09-21 22:30:30 -0700202 *
Ingo Molnarac6424b2017-06-20 12:06:13 +0200203 * We use this hashed waitqueue, instead of a normal wait_queue_entry_t, so
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 * we can wake only the relevant ones (hashed queues may be shared).
205 *
206 * A futex_q has a woken state, just like tasks have TASK_RUNNING.
Pierre Peifferec92d082007-05-09 02:35:00 -0700207 * It is considered woken when plist_node_empty(&q->list) || q->lock_ptr == 0.
Randy Dunlapfb62db22010-10-13 11:02:34 -0700208 * The order of wakeup is always to make the first condition true, then
Darren Hartd8d88fb2009-09-21 22:30:30 -0700209 * the second.
210 *
211 * PI futexes are typically woken before they are removed from the hash list via
212 * the rt_mutex code. See unqueue_me_pi().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 */
214struct futex_q {
Pierre Peifferec92d082007-05-09 02:35:00 -0700215 struct plist_node list;
Darren Hartd8d88fb2009-09-21 22:30:30 -0700216
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +0200217 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 spinlock_t *lock_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 union futex_key key;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700220 struct futex_pi_state *pi_state;
Darren Hart52400ba2009-04-03 13:40:49 -0700221 struct rt_mutex_waiter *rt_waiter;
Darren Hart84bc4af2009-08-13 17:36:53 -0700222 union futex_key *requeue_pi_key;
Thomas Gleixnercd689982008-02-01 17:45:14 +0100223 u32 bitset;
Thomas Gleixner07d91ef52021-08-15 23:29:18 +0200224 atomic_t requeue_state;
225#ifdef CONFIG_PREEMPT_RT
226 struct rcuwait requeue_wait;
227#endif
Kees Cook3859a272016-10-28 01:22:25 -0700228} __randomize_layout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Thomas Gleixner07d91ef52021-08-15 23:29:18 +0200230/*
231 * On PREEMPT_RT, the hash bucket lock is a 'sleeping' spinlock with an
232 * underlying rtmutex. The task which is about to be requeued could have
233 * just woken up (timeout, signal). After the wake up the task has to
234 * acquire hash bucket lock, which is held by the requeue code. As a task
235 * can only be blocked on _ONE_ rtmutex at a time, the proxy lock blocking
236 * and the hash bucket lock blocking would collide and corrupt state.
237 *
238 * On !PREEMPT_RT this is not a problem and everything could be serialized
239 * on hash bucket lock, but aside of having the benefit of common code,
240 * this allows to avoid doing the requeue when the task is already on the
241 * way out and taking the hash bucket lock of the original uaddr1 when the
242 * requeue has been completed.
243 *
244 * The following state transitions are valid:
245 *
246 * On the waiter side:
247 * Q_REQUEUE_PI_NONE -> Q_REQUEUE_PI_IGNORE
248 * Q_REQUEUE_PI_IN_PROGRESS -> Q_REQUEUE_PI_WAIT
249 *
250 * On the requeue side:
251 * Q_REQUEUE_PI_NONE -> Q_REQUEUE_PI_INPROGRESS
252 * Q_REQUEUE_PI_IN_PROGRESS -> Q_REQUEUE_PI_DONE/LOCKED
253 * Q_REQUEUE_PI_IN_PROGRESS -> Q_REQUEUE_PI_NONE (requeue failed)
254 * Q_REQUEUE_PI_WAIT -> Q_REQUEUE_PI_DONE/LOCKED
255 * Q_REQUEUE_PI_WAIT -> Q_REQUEUE_PI_IGNORE (requeue failed)
256 *
257 * The requeue side ignores a waiter with state Q_REQUEUE_PI_IGNORE as this
258 * signals that the waiter is already on the way out. It also means that
259 * the waiter is still on the 'wait' futex, i.e. uaddr1.
260 *
261 * The waiter side signals early wakeup to the requeue side either through
262 * setting state to Q_REQUEUE_PI_IGNORE or to Q_REQUEUE_PI_WAIT depending
263 * on the current state. In case of Q_REQUEUE_PI_IGNORE it can immediately
264 * proceed to take the hash bucket lock of uaddr1. If it set state to WAIT,
265 * which means the wakeup is interleaving with a requeue in progress it has
266 * to wait for the requeue side to change the state. Either to DONE/LOCKED
267 * or to IGNORE. DONE/LOCKED means the waiter q is now on the uaddr2 futex
268 * and either blocked (DONE) or has acquired it (LOCKED). IGNORE is set by
269 * the requeue side when the requeue attempt failed via deadlock detection
270 * and therefore the waiter q is still on the uaddr1 futex.
271 */
272enum {
273 Q_REQUEUE_PI_NONE = 0,
274 Q_REQUEUE_PI_IGNORE,
275 Q_REQUEUE_PI_IN_PROGRESS,
276 Q_REQUEUE_PI_WAIT,
277 Q_REQUEUE_PI_DONE,
278 Q_REQUEUE_PI_LOCKED,
279};
280
Darren Hart5bdb05f2010-11-08 13:40:28 -0800281static const struct futex_q futex_q_init = {
282 /* list gets initialized in queue_me()*/
Thomas Gleixner07d91ef52021-08-15 23:29:18 +0200283 .key = FUTEX_KEY_INIT,
284 .bitset = FUTEX_BITSET_MATCH_ANY,
285 .requeue_state = ATOMIC_INIT(Q_REQUEUE_PI_NONE),
Darren Hart5bdb05f2010-11-08 13:40:28 -0800286};
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288/*
Darren Hartb2d09942009-03-12 00:55:37 -0700289 * Hash buckets are shared by all the futex_keys that hash to the same
290 * location. Each key may have multiple futex_q structures, one for each task
291 * waiting on a futex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 */
293struct futex_hash_bucket {
Linus Torvalds11d46162014-03-20 22:11:17 -0700294 atomic_t waiters;
Pierre Peifferec92d082007-05-09 02:35:00 -0700295 spinlock_t lock;
296 struct plist_head chain;
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -0800297} ____cacheline_aligned_in_smp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Rasmus Villemoesac742d32015-09-09 23:36:40 +0200299/*
300 * The base of the bucket array and its size are always used together
301 * (after initialization only in hash_futex()), so ensure that they
302 * reside in the same cacheline.
303 */
304static struct {
305 struct futex_hash_bucket *queues;
306 unsigned long hashsize;
307} __futex_data __read_mostly __aligned(2*sizeof(long));
308#define futex_queues (__futex_data.queues)
309#define futex_hashsize (__futex_data.hashsize)
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -0800310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Davidlohr Buesoab51fba2015-06-29 23:26:02 -0700312/*
313 * Fault injections for futexes.
314 */
315#ifdef CONFIG_FAIL_FUTEX
316
317static struct {
318 struct fault_attr attr;
319
Viresh Kumar621a5f72015-09-26 15:04:07 -0700320 bool ignore_private;
Davidlohr Buesoab51fba2015-06-29 23:26:02 -0700321} fail_futex = {
322 .attr = FAULT_ATTR_INITIALIZER,
Viresh Kumar621a5f72015-09-26 15:04:07 -0700323 .ignore_private = false,
Davidlohr Buesoab51fba2015-06-29 23:26:02 -0700324};
325
326static int __init setup_fail_futex(char *str)
327{
328 return setup_fault_attr(&fail_futex.attr, str);
329}
330__setup("fail_futex=", setup_fail_futex);
331
kbuild test robot5d285a72015-07-21 01:40:45 +0800332static bool should_fail_futex(bool fshared)
Davidlohr Buesoab51fba2015-06-29 23:26:02 -0700333{
334 if (fail_futex.ignore_private && !fshared)
335 return false;
336
337 return should_fail(&fail_futex.attr, 1);
338}
339
340#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
341
342static int __init fail_futex_debugfs(void)
343{
344 umode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
345 struct dentry *dir;
346
347 dir = fault_create_debugfs_attr("fail_futex", NULL,
348 &fail_futex.attr);
349 if (IS_ERR(dir))
350 return PTR_ERR(dir);
351
Greg Kroah-Hartman0365aeb2019-01-22 16:21:39 +0100352 debugfs_create_bool("ignore-private", mode, dir,
353 &fail_futex.ignore_private);
Davidlohr Buesoab51fba2015-06-29 23:26:02 -0700354 return 0;
355}
356
357late_initcall(fail_futex_debugfs);
358
359#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
360
361#else
362static inline bool should_fail_futex(bool fshared)
363{
364 return false;
365}
366#endif /* CONFIG_FAIL_FUTEX */
367
Thomas Gleixnerba31c1a42019-11-06 22:55:36 +0100368#ifdef CONFIG_COMPAT
369static void compat_exit_robust_list(struct task_struct *curr);
Thomas Gleixnerba31c1a42019-11-06 22:55:36 +0100370#endif
371
Linus Torvalds11d46162014-03-20 22:11:17 -0700372/*
373 * Reflects a new waiter being added to the waitqueue.
374 */
375static inline void hb_waiters_inc(struct futex_hash_bucket *hb)
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800376{
377#ifdef CONFIG_SMP
Linus Torvalds11d46162014-03-20 22:11:17 -0700378 atomic_inc(&hb->waiters);
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800379 /*
Linus Torvalds11d46162014-03-20 22:11:17 -0700380 * Full barrier (A), see the ordering comment above.
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800381 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100382 smp_mb__after_atomic();
Linus Torvalds11d46162014-03-20 22:11:17 -0700383#endif
384}
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800385
Linus Torvalds11d46162014-03-20 22:11:17 -0700386/*
387 * Reflects a waiter being removed from the waitqueue by wakeup
388 * paths.
389 */
390static inline void hb_waiters_dec(struct futex_hash_bucket *hb)
391{
392#ifdef CONFIG_SMP
393 atomic_dec(&hb->waiters);
394#endif
395}
396
397static inline int hb_waiters_pending(struct futex_hash_bucket *hb)
398{
399#ifdef CONFIG_SMP
Peter Zijlstra4b39f992020-03-04 13:24:24 +0100400 /*
401 * Full barrier (B), see the ordering comment above.
402 */
403 smp_mb();
Linus Torvalds11d46162014-03-20 22:11:17 -0700404 return atomic_read(&hb->waiters);
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800405#else
Linus Torvalds11d46162014-03-20 22:11:17 -0700406 return 1;
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800407#endif
408}
409
Thomas Gleixnere8b61b32016-06-01 10:43:29 +0200410/**
411 * hash_futex - Return the hash bucket in the global hash
412 * @key: Pointer to the futex key for which the hash is calculated
413 *
414 * We hash on the keys returned from get_futex_key (see below) and return the
415 * corresponding hash bucket in the global hash.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 */
417static struct futex_hash_bucket *hash_futex(union futex_key *key)
418{
Thomas Gleixner8d677432020-03-08 19:07:17 +0100419 u32 hash = jhash2((u32 *)key, offsetof(typeof(*key), both.offset) / 4,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 key->both.offset);
Thomas Gleixner8d677432020-03-08 19:07:17 +0100421
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -0800422 return &futex_queues[hash & (futex_hashsize - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
Thomas Gleixnere8b61b32016-06-01 10:43:29 +0200425
426/**
427 * match_futex - Check whether two futex keys are equal
428 * @key1: Pointer to key1
429 * @key2: Pointer to key2
430 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 * Return 1 if two futex_keys are equal, 0 otherwise.
432 */
433static inline int match_futex(union futex_key *key1, union futex_key *key2)
434{
Darren Hart2bc87202009-10-14 10:12:39 -0700435 return (key1 && key2
436 && key1->both.word == key2->both.word
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 && key1->both.ptr == key2->both.ptr
438 && key1->both.offset == key2->both.offset);
439}
440
Linus Torvalds96d4f262019-01-03 18:57:57 -0800441enum futex_access {
442 FUTEX_READ,
443 FUTEX_WRITE
444};
445
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700446/**
Waiman Long5ca584d2019-05-28 12:03:45 -0400447 * futex_setup_timer - set up the sleeping hrtimer.
448 * @time: ptr to the given timeout value
449 * @timeout: the hrtimer_sleeper structure to be set up
450 * @flags: futex flags
451 * @range_ns: optional range in ns
452 *
453 * Return: Initialized hrtimer_sleeper structure or NULL if no timeout
454 * value given
455 */
456static inline struct hrtimer_sleeper *
457futex_setup_timer(ktime_t *time, struct hrtimer_sleeper *timeout,
458 int flags, u64 range_ns)
459{
460 if (!time)
461 return NULL;
462
Sebastian Andrzej Siewiordbc16252019-07-26 20:30:50 +0200463 hrtimer_init_sleeper_on_stack(timeout, (flags & FLAGS_CLOCKRT) ?
464 CLOCK_REALTIME : CLOCK_MONOTONIC,
465 HRTIMER_MODE_ABS);
Waiman Long5ca584d2019-05-28 12:03:45 -0400466 /*
467 * If range_ns is 0, calling hrtimer_set_expires_range_ns() is
468 * effectively the same as calling hrtimer_set_expires().
469 */
470 hrtimer_set_expires_range_ns(&timeout->timer, *time, range_ns);
471
472 return timeout;
473}
474
Peter Zijlstra8019ad12020-03-04 11:28:31 +0100475/*
476 * Generate a machine wide unique identifier for this inode.
477 *
478 * This relies on u64 not wrapping in the life-time of the machine; which with
479 * 1ns resolution means almost 585 years.
480 *
481 * This further relies on the fact that a well formed program will not unmap
482 * the file while it has a (shared) futex waiting on it. This mapping will have
483 * a file reference which pins the mount and inode.
484 *
485 * If for some reason an inode gets evicted and read back in again, it will get
486 * a new sequence number and will _NOT_ match, even though it is the exact same
487 * file.
488 *
489 * It is important that match_futex() will never have a false-positive, esp.
490 * for PI futexes that can mess up the state. The above argues that false-negatives
491 * are only possible for malformed programs.
492 */
493static u64 get_inode_sequence_number(struct inode *inode)
494{
495 static atomic64_t i_seq;
496 u64 old;
497
498 /* Does the inode already have a sequence number? */
499 old = atomic64_read(&inode->i_sequence);
500 if (likely(old))
501 return old;
502
503 for (;;) {
504 u64 new = atomic64_add_return(1, &i_seq);
505 if (WARN_ON_ONCE(!new))
506 continue;
507
508 old = atomic64_cmpxchg_relaxed(&inode->i_sequence, 0, new);
509 if (old)
510 return old;
511 return new;
512 }
513}
514
Waiman Long5ca584d2019-05-28 12:03:45 -0400515/**
Darren Hartd96ee562009-09-21 22:30:22 -0700516 * get_futex_key() - Get parameters which are the keys for a futex
517 * @uaddr: virtual address of the futex
André Almeida92613082020-07-02 17:28:43 -0300518 * @fshared: false for a PROCESS_PRIVATE futex, true for PROCESS_SHARED
Darren Hartd96ee562009-09-21 22:30:22 -0700519 * @key: address where result is stored.
Linus Torvalds96d4f262019-01-03 18:57:57 -0800520 * @rw: mapping needs to be read/write (values: FUTEX_READ,
521 * FUTEX_WRITE)
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700522 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -0800523 * Return: a negative error code or 0
524 *
Mauro Carvalho Chehab7b4ff1a2017-05-11 10:17:45 -0300525 * The key words are stored in @key on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 *
Peter Zijlstra8019ad12020-03-04 11:28:31 +0100527 * For shared mappings (when @fshared), the key is:
Mauro Carvalho Chehab03c109d2020-04-14 18:48:58 +0200528 *
Peter Zijlstra8019ad12020-03-04 11:28:31 +0100529 * ( inode->i_sequence, page->index, offset_within_page )
Mauro Carvalho Chehab03c109d2020-04-14 18:48:58 +0200530 *
Peter Zijlstra8019ad12020-03-04 11:28:31 +0100531 * [ also see get_inode_sequence_number() ]
532 *
533 * For private mappings (or when !@fshared), the key is:
Mauro Carvalho Chehab03c109d2020-04-14 18:48:58 +0200534 *
Peter Zijlstra8019ad12020-03-04 11:28:31 +0100535 * ( current->mm, address, 0 )
536 *
537 * This allows (cross process, where applicable) identification of the futex
538 * without keeping the page pinned for the duration of the FUTEX_WAIT.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 *
Darren Hartb2d09942009-03-12 00:55:37 -0700540 * lock_page() might sleep, the caller should not hold a spinlock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 */
André Almeida92613082020-07-02 17:28:43 -0300542static int get_futex_key(u32 __user *uaddr, bool fshared, union futex_key *key,
543 enum futex_access rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Ingo Molnare2970f22006-06-27 02:54:47 -0700545 unsigned long address = (unsigned long)uaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 struct mm_struct *mm = current->mm;
Mel Gorman077fa7a2016-06-08 14:25:22 +0100547 struct page *page, *tail;
Kirill A. Shutemov14d27ab2016-01-15 16:53:00 -0800548 struct address_space *mapping;
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500549 int err, ro = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 /*
552 * The futex address must be "naturally" aligned.
553 */
Ingo Molnare2970f22006-06-27 02:54:47 -0700554 key->both.offset = address % PAGE_SIZE;
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700555 if (unlikely((address % sizeof(u32)) != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return -EINVAL;
Ingo Molnare2970f22006-06-27 02:54:47 -0700557 address -= key->both.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Linus Torvalds96d4f262019-01-03 18:57:57 -0800559 if (unlikely(!access_ok(uaddr, sizeof(u32))))
Linus Torvalds5cdec2d2013-12-12 09:53:51 -0800560 return -EFAULT;
561
Davidlohr Buesoab51fba2015-06-29 23:26:02 -0700562 if (unlikely(should_fail_futex(fshared)))
563 return -EFAULT;
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 /*
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700566 * PROCESS_PRIVATE futexes are fast.
567 * As the mm cannot disappear under us and the 'key' only needs
568 * virtual address, we dont even have to find the underlying vma.
569 * Note : We do have to check 'uaddr' is a valid user address,
570 * but access_ok() should be faster than find_vma()
571 */
572 if (!fshared) {
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700573 key->private.mm = mm;
574 key->private.address = address;
575 return 0;
576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200578again:
Davidlohr Buesoab51fba2015-06-29 23:26:02 -0700579 /* Ignore any VERIFY_READ mapping (futex common case) */
André Almeida92613082020-07-02 17:28:43 -0300580 if (unlikely(should_fail_futex(true)))
Davidlohr Buesoab51fba2015-06-29 23:26:02 -0700581 return -EFAULT;
582
Ira Weiny73b01402019-05-13 17:17:11 -0700583 err = get_user_pages_fast(address, 1, FOLL_WRITE, &page);
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500584 /*
585 * If write access is not required (eg. FUTEX_WAIT), try
586 * and get read-only access.
587 */
Linus Torvalds96d4f262019-01-03 18:57:57 -0800588 if (err == -EFAULT && rw == FUTEX_READ) {
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500589 err = get_user_pages_fast(address, 1, 0, &page);
590 ro = 1;
591 }
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200592 if (err < 0)
593 return err;
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500594 else
595 err = 0;
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200596
Mel Gorman65d8fc72016-02-09 11:15:14 -0800597 /*
598 * The treatment of mapping from this point on is critical. The page
599 * lock protects many things but in this context the page lock
600 * stabilizes mapping, prevents inode freeing in the shared
601 * file-backed region case and guards against movement to swap cache.
602 *
603 * Strictly speaking the page lock is not needed in all cases being
604 * considered here and page lock forces unnecessarily serialization
605 * From this point on, mapping will be re-verified if necessary and
606 * page lock will be acquired only if it is unavoidable
Mel Gorman077fa7a2016-06-08 14:25:22 +0100607 *
608 * Mapping checks require the head page for any compound page so the
609 * head page and mapping is looked up now. For anonymous pages, it
610 * does not matter if the page splits in the future as the key is
611 * based on the address. For filesystem-backed pages, the tail is
612 * required as the index of the page determines the key. For
613 * base pages, there is no tail page and tail == page.
Mel Gorman65d8fc72016-02-09 11:15:14 -0800614 */
Mel Gorman077fa7a2016-06-08 14:25:22 +0100615 tail = page;
Mel Gorman65d8fc72016-02-09 11:15:14 -0800616 page = compound_head(page);
617 mapping = READ_ONCE(page->mapping);
618
Hugh Dickinse6780f72011-12-31 11:44:01 -0800619 /*
Kirill A. Shutemov14d27ab2016-01-15 16:53:00 -0800620 * If page->mapping is NULL, then it cannot be a PageAnon
Hugh Dickinse6780f72011-12-31 11:44:01 -0800621 * page; but it might be the ZERO_PAGE or in the gate area or
622 * in a special mapping (all cases which we are happy to fail);
623 * or it may have been a good file page when get_user_pages_fast
624 * found it, but truncated or holepunched or subjected to
625 * invalidate_complete_page2 before we got the page lock (also
626 * cases which we are happy to fail). And we hold a reference,
627 * so refcount care in invalidate_complete_page's remove_mapping
628 * prevents drop_caches from setting mapping to NULL beneath us.
629 *
630 * The case we do have to guard against is when memory pressure made
631 * shmem_writepage move it from filecache to swapcache beneath us:
Kirill A. Shutemov14d27ab2016-01-15 16:53:00 -0800632 * an unlikely race, but we do need to retry for page->mapping.
Hugh Dickinse6780f72011-12-31 11:44:01 -0800633 */
Mel Gorman65d8fc72016-02-09 11:15:14 -0800634 if (unlikely(!mapping)) {
635 int shmem_swizzled;
636
637 /*
638 * Page lock is required to identify which special case above
639 * applies. If this is really a shmem page then the page lock
640 * will prevent unexpected transitions.
641 */
642 lock_page(page);
643 shmem_swizzled = PageSwapCache(page) || page->mapping;
Kirill A. Shutemov14d27ab2016-01-15 16:53:00 -0800644 unlock_page(page);
645 put_page(page);
Mel Gorman65d8fc72016-02-09 11:15:14 -0800646
Hugh Dickinse6780f72011-12-31 11:44:01 -0800647 if (shmem_swizzled)
648 goto again;
Mel Gorman65d8fc72016-02-09 11:15:14 -0800649
Hugh Dickinse6780f72011-12-31 11:44:01 -0800650 return -EFAULT;
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 /*
654 * Private mappings are handled in a simple way.
655 *
Mel Gorman65d8fc72016-02-09 11:15:14 -0800656 * If the futex key is stored on an anonymous page, then the associated
657 * object is the mm which is implicitly pinned by the calling process.
658 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 * NOTE: When userspace waits on a MAP_SHARED mapping, even if
660 * it's a read-only handle, it's expected that futexes attach to
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200661 * the object not the particular process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 */
Kirill A. Shutemov14d27ab2016-01-15 16:53:00 -0800663 if (PageAnon(page)) {
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500664 /*
665 * A RO anonymous page will never change and thus doesn't make
666 * sense for futex operations.
667 */
André Almeida92613082020-07-02 17:28:43 -0300668 if (unlikely(should_fail_futex(true)) || ro) {
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500669 err = -EFAULT;
670 goto out;
671 }
672
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200673 key->both.offset |= FUT_OFF_MMSHARED; /* ref taken on mm */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 key->private.mm = mm;
Ingo Molnare2970f22006-06-27 02:54:47 -0700675 key->private.address = address;
Mel Gorman65d8fc72016-02-09 11:15:14 -0800676
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200677 } else {
Mel Gorman65d8fc72016-02-09 11:15:14 -0800678 struct inode *inode;
679
680 /*
681 * The associated futex object in this case is the inode and
682 * the page->mapping must be traversed. Ordinarily this should
683 * be stabilised under page lock but it's not strictly
684 * necessary in this case as we just want to pin the inode, not
685 * update the radix tree or anything like that.
686 *
687 * The RCU read lock is taken as the inode is finally freed
688 * under RCU. If the mapping still matches expectations then the
689 * mapping->host can be safely accessed as being a valid inode.
690 */
691 rcu_read_lock();
692
693 if (READ_ONCE(page->mapping) != mapping) {
694 rcu_read_unlock();
695 put_page(page);
696
697 goto again;
698 }
699
700 inode = READ_ONCE(mapping->host);
701 if (!inode) {
702 rcu_read_unlock();
703 put_page(page);
704
705 goto again;
706 }
707
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200708 key->both.offset |= FUT_OFF_INODE; /* inode-based key */
Peter Zijlstra8019ad12020-03-04 11:28:31 +0100709 key->shared.i_seq = get_inode_sequence_number(inode);
Hugh Dickinsfe19bd32021-06-24 18:39:52 -0700710 key->shared.pgoff = page_to_pgoff(tail);
Mel Gorman65d8fc72016-02-09 11:15:14 -0800711 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
713
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500714out:
Kirill A. Shutemov14d27ab2016-01-15 16:53:00 -0800715 put_page(page);
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500716 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
Darren Hartd96ee562009-09-21 22:30:22 -0700719/**
720 * fault_in_user_writeable() - Fault in user address and verify RW access
Thomas Gleixnerd0725992009-06-11 23:15:43 +0200721 * @uaddr: pointer to faulting user space address
722 *
723 * Slow path to fixup the fault we just took in the atomic write
724 * access to @uaddr.
725 *
Randy Dunlapfb62db22010-10-13 11:02:34 -0700726 * We have no generic implementation of a non-destructive write to the
Thomas Gleixnerd0725992009-06-11 23:15:43 +0200727 * user address. We know that we faulted in the atomic pagefault
728 * disabled section so we can as well avoid the #PF overhead by
729 * calling get_user_pages() right away.
730 */
731static int fault_in_user_writeable(u32 __user *uaddr)
732{
Andi Kleen722d0172009-12-08 13:19:42 +0100733 struct mm_struct *mm = current->mm;
734 int ret;
735
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700736 mmap_read_lock(mm);
Peter Xu64019a22020-08-11 18:39:01 -0700737 ret = fixup_user_fault(mm, (unsigned long)uaddr,
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800738 FAULT_FLAG_WRITE, NULL);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700739 mmap_read_unlock(mm);
Andi Kleen722d0172009-12-08 13:19:42 +0100740
Thomas Gleixnerd0725992009-06-11 23:15:43 +0200741 return ret < 0 ? ret : 0;
742}
743
Darren Hart4b1c4862009-04-03 13:39:42 -0700744/**
745 * futex_top_waiter() - Return the highest priority waiter on a futex
Darren Hartd96ee562009-09-21 22:30:22 -0700746 * @hb: the hash bucket the futex_q's reside in
747 * @key: the futex key (to distinguish it from other futex futex_q's)
Darren Hart4b1c4862009-04-03 13:39:42 -0700748 *
749 * Must be called with the hb lock held.
750 */
751static struct futex_q *futex_top_waiter(struct futex_hash_bucket *hb,
752 union futex_key *key)
753{
754 struct futex_q *this;
755
756 plist_for_each_entry(this, &hb->chain, list) {
757 if (match_futex(&this->key, key))
758 return this;
759 }
760 return NULL;
761}
762
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800763static int cmpxchg_futex_value_locked(u32 *curval, u32 __user *uaddr,
764 u32 uval, u32 newval)
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700765{
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800766 int ret;
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700767
768 pagefault_disable();
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800769 ret = futex_atomic_cmpxchg_inatomic(curval, uaddr, uval, newval);
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700770 pagefault_enable();
771
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800772 return ret;
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700773}
774
775static int get_futex_value_locked(u32 *dest, u32 __user *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
777 int ret;
778
Peter Zijlstraa8663742006-12-06 20:32:20 -0800779 pagefault_disable();
Linus Torvaldsbd28b142016-05-22 17:21:27 -0700780 ret = __get_user(*dest, from);
Peter Zijlstraa8663742006-12-06 20:32:20 -0800781 pagefault_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 return ret ? -EFAULT : 0;
784}
785
Ingo Molnarc87e2832006-06-27 02:54:58 -0700786
787/*
788 * PI code:
789 */
790static int refill_pi_state_cache(void)
791{
792 struct futex_pi_state *pi_state;
793
794 if (likely(current->pi_state_cache))
795 return 0;
796
Burman Yan4668edc2006-12-06 20:38:51 -0800797 pi_state = kzalloc(sizeof(*pi_state), GFP_KERNEL);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700798
799 if (!pi_state)
800 return -ENOMEM;
801
Ingo Molnarc87e2832006-06-27 02:54:58 -0700802 INIT_LIST_HEAD(&pi_state->list);
803 /* pi_mutex gets initialized later */
804 pi_state->owner = NULL;
Elena Reshetova49262de2019-02-05 14:24:27 +0200805 refcount_set(&pi_state->refcount, 1);
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200806 pi_state->key = FUTEX_KEY_INIT;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700807
808 current->pi_state_cache = pi_state;
809
810 return 0;
811}
812
Peter Zijlstrabf92cf32017-03-22 11:35:53 +0100813static struct futex_pi_state *alloc_pi_state(void)
Ingo Molnarc87e2832006-06-27 02:54:58 -0700814{
815 struct futex_pi_state *pi_state = current->pi_state_cache;
816
817 WARN_ON(!pi_state);
818 current->pi_state_cache = NULL;
819
820 return pi_state;
821}
822
Thomas Gleixnerc5cade22021-01-19 15:21:35 +0100823static void pi_state_update_owner(struct futex_pi_state *pi_state,
824 struct task_struct *new_owner)
825{
826 struct task_struct *old_owner = pi_state->owner;
827
828 lockdep_assert_held(&pi_state->pi_mutex.wait_lock);
829
830 if (old_owner) {
831 raw_spin_lock(&old_owner->pi_lock);
832 WARN_ON(list_empty(&pi_state->list));
833 list_del_init(&pi_state->list);
834 raw_spin_unlock(&old_owner->pi_lock);
835 }
836
837 if (new_owner) {
838 raw_spin_lock(&new_owner->pi_lock);
839 WARN_ON(!list_empty(&pi_state->list));
840 list_add(&pi_state->list, &new_owner->pi_state_list);
841 pi_state->owner = new_owner;
842 raw_spin_unlock(&new_owner->pi_lock);
843 }
844}
845
Peter Zijlstrabf92cf32017-03-22 11:35:53 +0100846static void get_pi_state(struct futex_pi_state *pi_state)
847{
Elena Reshetova49262de2019-02-05 14:24:27 +0200848 WARN_ON_ONCE(!refcount_inc_not_zero(&pi_state->refcount));
Peter Zijlstrabf92cf32017-03-22 11:35:53 +0100849}
850
Brian Silverman30a6b802014-10-25 20:20:37 -0400851/*
Thomas Gleixner29e9ee52015-12-19 20:07:39 +0000852 * Drops a reference to the pi_state object and frees or caches it
853 * when the last reference is gone.
Brian Silverman30a6b802014-10-25 20:20:37 -0400854 */
Thomas Gleixner29e9ee52015-12-19 20:07:39 +0000855static void put_pi_state(struct futex_pi_state *pi_state)
Ingo Molnarc87e2832006-06-27 02:54:58 -0700856{
Brian Silverman30a6b802014-10-25 20:20:37 -0400857 if (!pi_state)
858 return;
859
Elena Reshetova49262de2019-02-05 14:24:27 +0200860 if (!refcount_dec_and_test(&pi_state->refcount))
Ingo Molnarc87e2832006-06-27 02:54:58 -0700861 return;
862
863 /*
864 * If pi_state->owner is NULL, the owner is most probably dying
865 * and has cleaned up the pi_state already
866 */
867 if (pi_state->owner) {
Dan Carpenter1e106aa2020-11-06 11:52:05 +0300868 unsigned long flags;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700869
Dan Carpenter1e106aa2020-11-06 11:52:05 +0300870 raw_spin_lock_irqsave(&pi_state->pi_mutex.wait_lock, flags);
Thomas Gleixner6ccc84f2021-01-20 11:35:19 +0100871 pi_state_update_owner(pi_state, NULL);
Thomas Gleixner2156ac12021-01-20 11:32:07 +0100872 rt_mutex_proxy_unlock(&pi_state->pi_mutex);
Dan Carpenter1e106aa2020-11-06 11:52:05 +0300873 raw_spin_unlock_irqrestore(&pi_state->pi_mutex.wait_lock, flags);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700874 }
875
Peter Zijlstrac74aef22017-09-22 17:48:06 +0200876 if (current->pi_state_cache) {
Ingo Molnarc87e2832006-06-27 02:54:58 -0700877 kfree(pi_state);
Peter Zijlstrac74aef22017-09-22 17:48:06 +0200878 } else {
Ingo Molnarc87e2832006-06-27 02:54:58 -0700879 /*
880 * pi_state->list is already empty.
881 * clear pi_state->owner.
882 * refcount is at 0 - put it back to 1.
883 */
884 pi_state->owner = NULL;
Elena Reshetova49262de2019-02-05 14:24:27 +0200885 refcount_set(&pi_state->refcount, 1);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700886 current->pi_state_cache = pi_state;
887 }
888}
889
Nicolas Pitrebc2eecd2017-08-01 00:31:32 -0400890#ifdef CONFIG_FUTEX_PI
891
Ingo Molnarc87e2832006-06-27 02:54:58 -0700892/*
893 * This task is holding PI mutexes at exit time => bad.
894 * Kernel cleans up PI-state, but userspace is likely hosed.
895 * (Robust-futex cleanup is separate and might save the day for userspace.)
896 */
Thomas Gleixnerba31c1a42019-11-06 22:55:36 +0100897static void exit_pi_state_list(struct task_struct *curr)
Ingo Molnarc87e2832006-06-27 02:54:58 -0700898{
Ingo Molnarc87e2832006-06-27 02:54:58 -0700899 struct list_head *next, *head = &curr->pi_state_list;
900 struct futex_pi_state *pi_state;
Ingo Molnar627371d2006-07-29 05:16:20 +0200901 struct futex_hash_bucket *hb;
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200902 union futex_key key = FUTEX_KEY_INIT;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700903
Thomas Gleixnera0c1e902008-02-23 15:23:57 -0800904 if (!futex_cmpxchg_enabled)
905 return;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700906 /*
907 * We are a ZOMBIE and nobody can enqueue itself on
908 * pi_state_list anymore, but we have to be careful
Ingo Molnar627371d2006-07-29 05:16:20 +0200909 * versus waiters unqueueing themselves:
Ingo Molnarc87e2832006-06-27 02:54:58 -0700910 */
Thomas Gleixner1d615482009-11-17 14:54:03 +0100911 raw_spin_lock_irq(&curr->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700912 while (!list_empty(head)) {
Ingo Molnarc87e2832006-06-27 02:54:58 -0700913 next = head->next;
914 pi_state = list_entry(next, struct futex_pi_state, list);
915 key = pi_state->key;
Ingo Molnar627371d2006-07-29 05:16:20 +0200916 hb = hash_futex(&key);
Peter Zijlstra153fbd12017-10-31 11:18:53 +0100917
918 /*
919 * We can race against put_pi_state() removing itself from the
920 * list (a waiter going away). put_pi_state() will first
921 * decrement the reference count and then modify the list, so
922 * its possible to see the list entry but fail this reference
923 * acquire.
924 *
925 * In that case; drop the locks to let put_pi_state() make
926 * progress and retry the loop.
927 */
Elena Reshetova49262de2019-02-05 14:24:27 +0200928 if (!refcount_inc_not_zero(&pi_state->refcount)) {
Peter Zijlstra153fbd12017-10-31 11:18:53 +0100929 raw_spin_unlock_irq(&curr->pi_lock);
930 cpu_relax();
931 raw_spin_lock_irq(&curr->pi_lock);
932 continue;
933 }
Thomas Gleixner1d615482009-11-17 14:54:03 +0100934 raw_spin_unlock_irq(&curr->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700935
Ingo Molnarc87e2832006-06-27 02:54:58 -0700936 spin_lock(&hb->lock);
Peter Zijlstrac74aef22017-09-22 17:48:06 +0200937 raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
938 raw_spin_lock(&curr->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +0200939 /*
940 * We dropped the pi-lock, so re-check whether this
941 * task still owns the PI-state:
942 */
Ingo Molnarc87e2832006-06-27 02:54:58 -0700943 if (head->next != next) {
Peter Zijlstra153fbd12017-10-31 11:18:53 +0100944 /* retain curr->pi_lock for the loop invariant */
Peter Zijlstrac74aef22017-09-22 17:48:06 +0200945 raw_spin_unlock(&pi_state->pi_mutex.wait_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700946 spin_unlock(&hb->lock);
Peter Zijlstra153fbd12017-10-31 11:18:53 +0100947 put_pi_state(pi_state);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700948 continue;
949 }
950
Ingo Molnarc87e2832006-06-27 02:54:58 -0700951 WARN_ON(pi_state->owner != curr);
Ingo Molnar627371d2006-07-29 05:16:20 +0200952 WARN_ON(list_empty(&pi_state->list));
953 list_del_init(&pi_state->list);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700954 pi_state->owner = NULL;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700955
Peter Zijlstra153fbd12017-10-31 11:18:53 +0100956 raw_spin_unlock(&curr->pi_lock);
Peter Zijlstrac74aef22017-09-22 17:48:06 +0200957 raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700958 spin_unlock(&hb->lock);
959
Peter Zijlstra16ffa122017-03-22 11:35:55 +0100960 rt_mutex_futex_unlock(&pi_state->pi_mutex);
961 put_pi_state(pi_state);
962
Thomas Gleixner1d615482009-11-17 14:54:03 +0100963 raw_spin_lock_irq(&curr->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700964 }
Thomas Gleixner1d615482009-11-17 14:54:03 +0100965 raw_spin_unlock_irq(&curr->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700966}
Thomas Gleixnerba31c1a42019-11-06 22:55:36 +0100967#else
968static inline void exit_pi_state_list(struct task_struct *curr) { }
Nicolas Pitrebc2eecd2017-08-01 00:31:32 -0400969#endif
970
Thomas Gleixner54a21782014-06-03 12:27:08 +0000971/*
972 * We need to check the following states:
973 *
974 * Waiter | pi_state | pi->owner | uTID | uODIED | ?
975 *
976 * [1] NULL | --- | --- | 0 | 0/1 | Valid
977 * [2] NULL | --- | --- | >0 | 0/1 | Valid
978 *
979 * [3] Found | NULL | -- | Any | 0/1 | Invalid
980 *
981 * [4] Found | Found | NULL | 0 | 1 | Valid
982 * [5] Found | Found | NULL | >0 | 1 | Invalid
983 *
984 * [6] Found | Found | task | 0 | 1 | Valid
985 *
986 * [7] Found | Found | NULL | Any | 0 | Invalid
987 *
988 * [8] Found | Found | task | ==taskTID | 0/1 | Valid
989 * [9] Found | Found | task | 0 | 0 | Invalid
990 * [10] Found | Found | task | !=taskTID | 0/1 | Invalid
991 *
992 * [1] Indicates that the kernel can acquire the futex atomically. We
Randy Dunlap7b7b8a22020-10-15 20:10:28 -0700993 * came here due to a stale FUTEX_WAITERS/FUTEX_OWNER_DIED bit.
Thomas Gleixner54a21782014-06-03 12:27:08 +0000994 *
995 * [2] Valid, if TID does not belong to a kernel thread. If no matching
996 * thread is found then it indicates that the owner TID has died.
997 *
998 * [3] Invalid. The waiter is queued on a non PI futex
999 *
1000 * [4] Valid state after exit_robust_list(), which sets the user space
1001 * value to FUTEX_WAITERS | FUTEX_OWNER_DIED.
1002 *
1003 * [5] The user space value got manipulated between exit_robust_list()
1004 * and exit_pi_state_list()
1005 *
1006 * [6] Valid state after exit_pi_state_list() which sets the new owner in
1007 * the pi_state but cannot access the user space value.
1008 *
1009 * [7] pi_state->owner can only be NULL when the OWNER_DIED bit is set.
1010 *
1011 * [8] Owner and user space value match
1012 *
1013 * [9] There is no transient state which sets the user space TID to 0
1014 * except exit_robust_list(), but this is indicated by the
1015 * FUTEX_OWNER_DIED bit. See [4]
1016 *
1017 * [10] There is no transient state which leaves owner and user space
Thomas Gleixner34b1a1c2021-01-18 19:01:21 +01001018 * TID out of sync. Except one error case where the kernel is denied
1019 * write access to the user address, see fixup_pi_state_owner().
Peter Zijlstra734009e2017-03-22 11:35:52 +01001020 *
1021 *
1022 * Serialization and lifetime rules:
1023 *
1024 * hb->lock:
1025 *
1026 * hb -> futex_q, relation
1027 * futex_q -> pi_state, relation
1028 *
1029 * (cannot be raw because hb can contain arbitrary amount
1030 * of futex_q's)
1031 *
1032 * pi_mutex->wait_lock:
1033 *
1034 * {uval, pi_state}
1035 *
1036 * (and pi_mutex 'obviously')
1037 *
1038 * p->pi_lock:
1039 *
1040 * p->pi_state_list -> pi_state->list, relation
Davidlohr Buesoc2e4bfe2021-02-26 09:50:29 -08001041 * pi_mutex->owner -> pi_state->owner, relation
Peter Zijlstra734009e2017-03-22 11:35:52 +01001042 *
1043 * pi_state->refcount:
1044 *
1045 * pi_state lifetime
1046 *
1047 *
1048 * Lock order:
1049 *
1050 * hb->lock
1051 * pi_mutex->wait_lock
1052 * p->pi_lock
1053 *
Thomas Gleixner54a21782014-06-03 12:27:08 +00001054 */
Thomas Gleixnere60cbc52014-06-11 20:45:39 +00001055
1056/*
1057 * Validate that the existing waiter has a pi_state and sanity check
1058 * the pi_state against the user space value. If correct, attach to
1059 * it.
1060 */
Peter Zijlstra734009e2017-03-22 11:35:52 +01001061static int attach_to_pi_state(u32 __user *uaddr, u32 uval,
1062 struct futex_pi_state *pi_state,
Thomas Gleixnere60cbc52014-06-11 20:45:39 +00001063 struct futex_pi_state **ps)
1064{
1065 pid_t pid = uval & FUTEX_TID_MASK;
Peter Zijlstra94ffac52017-04-07 09:04:07 +02001066 u32 uval2;
1067 int ret;
Thomas Gleixnere60cbc52014-06-11 20:45:39 +00001068
1069 /*
1070 * Userspace might have messed up non-PI and PI futexes [3]
1071 */
1072 if (unlikely(!pi_state))
1073 return -EINVAL;
1074
Peter Zijlstra734009e2017-03-22 11:35:52 +01001075 /*
1076 * We get here with hb->lock held, and having found a
1077 * futex_top_waiter(). This means that futex_lock_pi() of said futex_q
1078 * has dropped the hb->lock in between queue_me() and unqueue_me_pi(),
1079 * which in turn means that futex_lock_pi() still has a reference on
1080 * our pi_state.
Peter Zijlstra16ffa122017-03-22 11:35:55 +01001081 *
1082 * The waiter holding a reference on @pi_state also protects against
1083 * the unlocked put_pi_state() in futex_unlock_pi(), futex_lock_pi()
1084 * and futex_wait_requeue_pi() as it cannot go to 0 and consequently
1085 * free pi_state before we can take a reference ourselves.
Peter Zijlstra734009e2017-03-22 11:35:52 +01001086 */
Elena Reshetova49262de2019-02-05 14:24:27 +02001087 WARN_ON(!refcount_read(&pi_state->refcount));
Thomas Gleixnere60cbc52014-06-11 20:45:39 +00001088
1089 /*
Peter Zijlstra734009e2017-03-22 11:35:52 +01001090 * Now that we have a pi_state, we can acquire wait_lock
1091 * and do the state validation.
1092 */
1093 raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
1094
1095 /*
1096 * Since {uval, pi_state} is serialized by wait_lock, and our current
1097 * uval was read without holding it, it can have changed. Verify it
1098 * still is what we expect it to be, otherwise retry the entire
1099 * operation.
1100 */
1101 if (get_futex_value_locked(&uval2, uaddr))
1102 goto out_efault;
1103
1104 if (uval != uval2)
1105 goto out_eagain;
1106
1107 /*
Thomas Gleixnere60cbc52014-06-11 20:45:39 +00001108 * Handle the owner died case:
1109 */
1110 if (uval & FUTEX_OWNER_DIED) {
1111 /*
1112 * exit_pi_state_list sets owner to NULL and wakes the
1113 * topmost waiter. The task which acquires the
1114 * pi_state->rt_mutex will fixup owner.
1115 */
1116 if (!pi_state->owner) {
1117 /*
1118 * No pi state owner, but the user space TID
1119 * is not 0. Inconsistent state. [5]
1120 */
1121 if (pid)
Peter Zijlstra734009e2017-03-22 11:35:52 +01001122 goto out_einval;
Thomas Gleixnere60cbc52014-06-11 20:45:39 +00001123 /*
1124 * Take a ref on the state and return success. [4]
1125 */
Peter Zijlstra734009e2017-03-22 11:35:52 +01001126 goto out_attach;
Thomas Gleixnere60cbc52014-06-11 20:45:39 +00001127 }
1128
1129 /*
1130 * If TID is 0, then either the dying owner has not
1131 * yet executed exit_pi_state_list() or some waiter
1132 * acquired the rtmutex in the pi state, but did not
1133 * yet fixup the TID in user space.
1134 *
1135 * Take a ref on the state and return success. [6]
1136 */
1137 if (!pid)
Peter Zijlstra734009e2017-03-22 11:35:52 +01001138 goto out_attach;
Thomas Gleixnere60cbc52014-06-11 20:45:39 +00001139 } else {
1140 /*
1141 * If the owner died bit is not set, then the pi_state
1142 * must have an owner. [7]
1143 */
1144 if (!pi_state->owner)
Peter Zijlstra734009e2017-03-22 11:35:52 +01001145 goto out_einval;
Thomas Gleixnere60cbc52014-06-11 20:45:39 +00001146 }
1147
1148 /*
1149 * Bail out if user space manipulated the futex value. If pi
1150 * state exists then the owner TID must be the same as the
1151 * user space TID. [9/10]
1152 */
1153 if (pid != task_pid_vnr(pi_state->owner))
Peter Zijlstra734009e2017-03-22 11:35:52 +01001154 goto out_einval;
1155
1156out_attach:
Peter Zijlstrabf92cf32017-03-22 11:35:53 +01001157 get_pi_state(pi_state);
Peter Zijlstra734009e2017-03-22 11:35:52 +01001158 raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
Thomas Gleixnere60cbc52014-06-11 20:45:39 +00001159 *ps = pi_state;
1160 return 0;
Peter Zijlstra734009e2017-03-22 11:35:52 +01001161
1162out_einval:
1163 ret = -EINVAL;
1164 goto out_error;
1165
1166out_eagain:
1167 ret = -EAGAIN;
1168 goto out_error;
1169
1170out_efault:
1171 ret = -EFAULT;
1172 goto out_error;
1173
1174out_error:
1175 raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
1176 return ret;
Thomas Gleixnere60cbc52014-06-11 20:45:39 +00001177}
1178
Thomas Gleixner3ef240e2019-11-06 22:55:46 +01001179/**
1180 * wait_for_owner_exiting - Block until the owner has exited
Randy Dunlap51bfb1d2019-12-08 20:26:55 -08001181 * @ret: owner's current futex lock status
Thomas Gleixner3ef240e2019-11-06 22:55:46 +01001182 * @exiting: Pointer to the exiting task
1183 *
1184 * Caller must hold a refcount on @exiting.
1185 */
1186static void wait_for_owner_exiting(int ret, struct task_struct *exiting)
1187{
1188 if (ret != -EBUSY) {
1189 WARN_ON_ONCE(exiting);
1190 return;
1191 }
1192
1193 if (WARN_ON_ONCE(ret == -EBUSY && !exiting))
1194 return;
1195
1196 mutex_lock(&exiting->futex_exit_mutex);
1197 /*
1198 * No point in doing state checking here. If the waiter got here
1199 * while the task was in exec()->exec_futex_release() then it can
1200 * have any FUTEX_STATE_* value when the waiter has acquired the
1201 * mutex. OK, if running, EXITING or DEAD if it reached exit()
1202 * already. Highly unlikely and not a problem. Just one more round
1203 * through the futex maze.
1204 */
1205 mutex_unlock(&exiting->futex_exit_mutex);
1206
1207 put_task_struct(exiting);
1208}
1209
Thomas Gleixnerda791a62018-12-10 14:35:14 +01001210static int handle_exit_race(u32 __user *uaddr, u32 uval,
1211 struct task_struct *tsk)
1212{
1213 u32 uval2;
1214
1215 /*
Thomas Gleixnerac31c7f2019-11-06 22:55:45 +01001216 * If the futex exit state is not yet FUTEX_STATE_DEAD, tell the
1217 * caller that the alleged owner is busy.
Thomas Gleixnerda791a62018-12-10 14:35:14 +01001218 */
Thomas Gleixner3d4775d2019-11-06 22:55:37 +01001219 if (tsk && tsk->futex_state != FUTEX_STATE_DEAD)
Thomas Gleixnerac31c7f2019-11-06 22:55:45 +01001220 return -EBUSY;
Thomas Gleixnerda791a62018-12-10 14:35:14 +01001221
1222 /*
1223 * Reread the user space value to handle the following situation:
1224 *
1225 * CPU0 CPU1
1226 *
1227 * sys_exit() sys_futex()
1228 * do_exit() futex_lock_pi()
1229 * futex_lock_pi_atomic()
1230 * exit_signals(tsk) No waiters:
1231 * tsk->flags |= PF_EXITING; *uaddr == 0x00000PID
1232 * mm_release(tsk) Set waiter bit
1233 * exit_robust_list(tsk) { *uaddr = 0x80000PID;
1234 * Set owner died attach_to_pi_owner() {
1235 * *uaddr = 0xC0000000; tsk = get_task(PID);
1236 * } if (!tsk->flags & PF_EXITING) {
1237 * ... attach();
Thomas Gleixner3d4775d2019-11-06 22:55:37 +01001238 * tsk->futex_state = } else {
1239 * FUTEX_STATE_DEAD; if (tsk->futex_state !=
1240 * FUTEX_STATE_DEAD)
Thomas Gleixnerda791a62018-12-10 14:35:14 +01001241 * return -EAGAIN;
1242 * return -ESRCH; <--- FAIL
1243 * }
1244 *
1245 * Returning ESRCH unconditionally is wrong here because the
1246 * user space value has been changed by the exiting task.
1247 *
1248 * The same logic applies to the case where the exiting task is
1249 * already gone.
1250 */
1251 if (get_futex_value_locked(&uval2, uaddr))
1252 return -EFAULT;
1253
1254 /* If the user space value has changed, try again. */
1255 if (uval2 != uval)
1256 return -EAGAIN;
1257
1258 /*
1259 * The exiting task did not have a robust list, the robust list was
1260 * corrupted or the user space value in *uaddr is simply bogus.
1261 * Give up and tell user space.
1262 */
1263 return -ESRCH;
1264}
1265
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001266/*
1267 * Lookup the task for the TID provided from user space and attach to
1268 * it after doing proper sanity checks.
1269 */
Thomas Gleixnerda791a62018-12-10 14:35:14 +01001270static int attach_to_pi_owner(u32 __user *uaddr, u32 uval, union futex_key *key,
Thomas Gleixner3ef240e2019-11-06 22:55:46 +01001271 struct futex_pi_state **ps,
1272 struct task_struct **exiting)
Ingo Molnarc87e2832006-06-27 02:54:58 -07001273{
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001274 pid_t pid = uval & FUTEX_TID_MASK;
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001275 struct futex_pi_state *pi_state;
1276 struct task_struct *p;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001277
1278 /*
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001279 * We are the first waiter - try to look up the real owner and attach
Thomas Gleixner54a21782014-06-03 12:27:08 +00001280 * the new pi_state to it, but bail out when TID = 0 [1]
Thomas Gleixnerda791a62018-12-10 14:35:14 +01001281 *
1282 * The !pid check is paranoid. None of the call sites should end up
1283 * with pid == 0, but better safe than sorry. Let the caller retry
Ingo Molnarc87e2832006-06-27 02:54:58 -07001284 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001285 if (!pid)
Thomas Gleixnerda791a62018-12-10 14:35:14 +01001286 return -EAGAIN;
Mike Rapoport2ee08262018-02-06 15:40:17 -08001287 p = find_get_task_by_vpid(pid);
Michal Hocko7a0ea092010-06-30 09:51:19 +02001288 if (!p)
Thomas Gleixnerda791a62018-12-10 14:35:14 +01001289 return handle_exit_race(uaddr, uval, NULL);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001290
Oleg Nesterova2129462015-02-02 15:05:36 +01001291 if (unlikely(p->flags & PF_KTHREAD)) {
Thomas Gleixnerf0d71b32014-05-12 20:45:35 +00001292 put_task_struct(p);
1293 return -EPERM;
1294 }
1295
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001296 /*
Thomas Gleixner3d4775d2019-11-06 22:55:37 +01001297 * We need to look at the task state to figure out, whether the
1298 * task is exiting. To protect against the change of the task state
1299 * in futex_exit_release(), we do this protected by p->pi_lock:
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001300 */
Thomas Gleixner1d615482009-11-17 14:54:03 +01001301 raw_spin_lock_irq(&p->pi_lock);
Thomas Gleixner3d4775d2019-11-06 22:55:37 +01001302 if (unlikely(p->futex_state != FUTEX_STATE_OK)) {
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001303 /*
Thomas Gleixner3d4775d2019-11-06 22:55:37 +01001304 * The task is on the way out. When the futex state is
1305 * FUTEX_STATE_DEAD, we know that the task has finished
1306 * the cleanup:
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001307 */
Thomas Gleixnerda791a62018-12-10 14:35:14 +01001308 int ret = handle_exit_race(uaddr, uval, p);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001309
Thomas Gleixner1d615482009-11-17 14:54:03 +01001310 raw_spin_unlock_irq(&p->pi_lock);
Thomas Gleixner3ef240e2019-11-06 22:55:46 +01001311 /*
1312 * If the owner task is between FUTEX_STATE_EXITING and
1313 * FUTEX_STATE_DEAD then store the task pointer and keep
1314 * the reference on the task struct. The calling code will
1315 * drop all locks, wait for the task to reach
1316 * FUTEX_STATE_DEAD and then drop the refcount. This is
1317 * required to prevent a live lock when the current task
1318 * preempted the exiting task between the two states.
1319 */
1320 if (ret == -EBUSY)
1321 *exiting = p;
1322 else
1323 put_task_struct(p);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001324 return ret;
1325 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001326
Thomas Gleixner54a21782014-06-03 12:27:08 +00001327 /*
1328 * No existing pi state. First waiter. [2]
Peter Zijlstra734009e2017-03-22 11:35:52 +01001329 *
1330 * This creates pi_state, we have hb->lock held, this means nothing can
1331 * observe this state, wait_lock is irrelevant.
Thomas Gleixner54a21782014-06-03 12:27:08 +00001332 */
Ingo Molnarc87e2832006-06-27 02:54:58 -07001333 pi_state = alloc_pi_state();
1334
1335 /*
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001336 * Initialize the pi_mutex in locked state and make @p
Ingo Molnarc87e2832006-06-27 02:54:58 -07001337 * the owner of it:
1338 */
1339 rt_mutex_init_proxy_locked(&pi_state->pi_mutex, p);
1340
1341 /* Store the key for possible exit cleanups: */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001342 pi_state->key = *key;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001343
Ingo Molnar627371d2006-07-29 05:16:20 +02001344 WARN_ON(!list_empty(&pi_state->list));
Ingo Molnarc87e2832006-06-27 02:54:58 -07001345 list_add(&pi_state->list, &p->pi_state_list);
Peter Zijlstrac74aef22017-09-22 17:48:06 +02001346 /*
1347 * Assignment without holding pi_state->pi_mutex.wait_lock is safe
1348 * because there is no concurrency as the object is not published yet.
1349 */
Ingo Molnarc87e2832006-06-27 02:54:58 -07001350 pi_state->owner = p;
Thomas Gleixner1d615482009-11-17 14:54:03 +01001351 raw_spin_unlock_irq(&p->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001352
1353 put_task_struct(p);
1354
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001355 *ps = pi_state;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001356
1357 return 0;
1358}
1359
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001360static int lock_pi_update_atomic(u32 __user *uaddr, u32 uval, u32 newval)
1361{
Will Deacon6b4f4bc2019-02-28 11:58:08 +00001362 int err;
Kees Cook3f649ab2020-06-03 13:09:38 -07001363 u32 curval;
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001364
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001365 if (unlikely(should_fail_futex(true)))
1366 return -EFAULT;
1367
Will Deacon6b4f4bc2019-02-28 11:58:08 +00001368 err = cmpxchg_futex_value_locked(&curval, uaddr, uval, newval);
1369 if (unlikely(err))
1370 return err;
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001371
Peter Zijlstra734009e2017-03-22 11:35:52 +01001372 /* If user space value changed, let the caller retry */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001373 return curval != uval ? -EAGAIN : 0;
1374}
1375
Darren Hart1a520842009-04-03 13:39:52 -07001376/**
Darren Hartd96ee562009-09-21 22:30:22 -07001377 * futex_lock_pi_atomic() - Atomic work required to acquire a pi aware futex
Darren Hartbab5bc92009-04-07 23:23:50 -07001378 * @uaddr: the pi futex user address
1379 * @hb: the pi futex hash bucket
1380 * @key: the futex key associated with uaddr and hb
1381 * @ps: the pi_state pointer where we store the result of the
1382 * lookup
1383 * @task: the task to perform the atomic lock work for. This will
1384 * be "current" except in the case of requeue pi.
Thomas Gleixner3ef240e2019-11-06 22:55:46 +01001385 * @exiting: Pointer to store the task pointer of the owner task
1386 * which is in the middle of exiting
Darren Hartbab5bc92009-04-07 23:23:50 -07001387 * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0)
Darren Hart1a520842009-04-03 13:39:52 -07001388 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08001389 * Return:
Mauro Carvalho Chehab7b4ff1a2017-05-11 10:17:45 -03001390 * - 0 - ready to wait;
1391 * - 1 - acquired the lock;
1392 * - <0 - error
Darren Hart1a520842009-04-03 13:39:52 -07001393 *
Thomas Gleixnerc363b7e2021-08-15 23:29:06 +02001394 * The hb->lock must be held by the caller.
Thomas Gleixner3ef240e2019-11-06 22:55:46 +01001395 *
1396 * @exiting is only set when the return value is -EBUSY. If so, this holds
1397 * a refcount on the exiting task on return and the caller needs to drop it
1398 * after waiting for the exit to complete.
Darren Hart1a520842009-04-03 13:39:52 -07001399 */
1400static int futex_lock_pi_atomic(u32 __user *uaddr, struct futex_hash_bucket *hb,
1401 union futex_key *key,
1402 struct futex_pi_state **ps,
Thomas Gleixner3ef240e2019-11-06 22:55:46 +01001403 struct task_struct *task,
1404 struct task_struct **exiting,
1405 int set_waiters)
Darren Hart1a520842009-04-03 13:39:52 -07001406{
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001407 u32 uval, newval, vpid = task_pid_vnr(task);
Peter Zijlstra499f5ac2017-03-22 11:35:48 +01001408 struct futex_q *top_waiter;
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001409 int ret;
Darren Hart1a520842009-04-03 13:39:52 -07001410
1411 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001412 * Read the user space value first so we can validate a few
1413 * things before proceeding further.
Darren Hart1a520842009-04-03 13:39:52 -07001414 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001415 if (get_futex_value_locked(&uval, uaddr))
Darren Hart1a520842009-04-03 13:39:52 -07001416 return -EFAULT;
1417
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001418 if (unlikely(should_fail_futex(true)))
1419 return -EFAULT;
1420
Darren Hart1a520842009-04-03 13:39:52 -07001421 /*
1422 * Detect deadlocks.
1423 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001424 if ((unlikely((uval & FUTEX_TID_MASK) == vpid)))
Darren Hart1a520842009-04-03 13:39:52 -07001425 return -EDEADLK;
1426
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001427 if ((unlikely(should_fail_futex(true))))
1428 return -EDEADLK;
1429
Darren Hart1a520842009-04-03 13:39:52 -07001430 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001431 * Lookup existing state first. If it exists, try to attach to
1432 * its pi_state.
Darren Hart1a520842009-04-03 13:39:52 -07001433 */
Peter Zijlstra499f5ac2017-03-22 11:35:48 +01001434 top_waiter = futex_top_waiter(hb, key);
1435 if (top_waiter)
Peter Zijlstra734009e2017-03-22 11:35:52 +01001436 return attach_to_pi_state(uaddr, uval, top_waiter->pi_state, ps);
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001437
1438 /*
1439 * No waiter and user TID is 0. We are here because the
1440 * waiters or the owner died bit is set or called from
1441 * requeue_cmp_pi or for whatever reason something took the
1442 * syscall.
1443 */
1444 if (!(uval & FUTEX_TID_MASK)) {
Thomas Gleixnerb3eaa9f2014-06-03 12:27:06 +00001445 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001446 * We take over the futex. No other waiters and the user space
1447 * TID is 0. We preserve the owner died bit.
Thomas Gleixnerb3eaa9f2014-06-03 12:27:06 +00001448 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001449 newval = uval & FUTEX_OWNER_DIED;
1450 newval |= vpid;
1451
1452 /* The futex requeue_pi code can enforce the waiters bit */
1453 if (set_waiters)
1454 newval |= FUTEX_WAITERS;
1455
1456 ret = lock_pi_update_atomic(uaddr, uval, newval);
Thomas Gleixner4f07ec02021-09-02 11:48:48 +02001457 if (ret)
1458 return ret;
1459
1460 /*
1461 * If the waiter bit was requested the caller also needs PI
1462 * state attached to the new owner of the user space futex.
1463 *
1464 * @task is guaranteed to be alive and it cannot be exiting
1465 * because it is either sleeping or waiting in
1466 * futex_requeue_pi_wakeup_sync().
1467 */
1468 if (set_waiters) {
1469 ret = attach_to_pi_owner(uaddr, newval, key, ps,
1470 exiting);
1471 WARN_ON(ret);
1472 }
1473 return 1;
Thomas Gleixnerb3eaa9f2014-06-03 12:27:06 +00001474 }
Darren Hart1a520842009-04-03 13:39:52 -07001475
Darren Hart1a520842009-04-03 13:39:52 -07001476 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001477 * First waiter. Set the waiters bit before attaching ourself to
1478 * the owner. If owner tries to unlock, it will be forced into
1479 * the kernel and blocked on hb->lock.
Darren Hart1a520842009-04-03 13:39:52 -07001480 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001481 newval = uval | FUTEX_WAITERS;
1482 ret = lock_pi_update_atomic(uaddr, uval, newval);
1483 if (ret)
1484 return ret;
Darren Hart1a520842009-04-03 13:39:52 -07001485 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001486 * If the update of the user space value succeeded, we try to
1487 * attach to the owner. If that fails, no harm done, we only
1488 * set the FUTEX_WAITERS bit in the user space variable.
Darren Hart1a520842009-04-03 13:39:52 -07001489 */
Thomas Gleixner3ef240e2019-11-06 22:55:46 +01001490 return attach_to_pi_owner(uaddr, newval, key, ps, exiting);
Darren Hart1a520842009-04-03 13:39:52 -07001491}
1492
Lai Jiangshan2e129782010-12-22 14:18:50 +08001493/**
1494 * __unqueue_futex() - Remove the futex_q from its futex_hash_bucket
1495 * @q: The futex_q to unqueue
1496 *
1497 * The q->lock_ptr must not be NULL and must be held by the caller.
1498 */
1499static void __unqueue_futex(struct futex_q *q)
1500{
1501 struct futex_hash_bucket *hb;
1502
Lance Roy4de1a292018-10-02 22:38:57 -07001503 if (WARN_ON_SMP(!q->lock_ptr) || WARN_ON(plist_node_empty(&q->list)))
Lai Jiangshan2e129782010-12-22 14:18:50 +08001504 return;
Lance Roy4de1a292018-10-02 22:38:57 -07001505 lockdep_assert_held(q->lock_ptr);
Lai Jiangshan2e129782010-12-22 14:18:50 +08001506
1507 hb = container_of(q->lock_ptr, struct futex_hash_bucket, lock);
1508 plist_del(&q->list, &hb->chain);
Linus Torvalds11d46162014-03-20 22:11:17 -07001509 hb_waiters_dec(hb);
Lai Jiangshan2e129782010-12-22 14:18:50 +08001510}
1511
Ingo Molnarc87e2832006-06-27 02:54:58 -07001512/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 * The hash bucket lock must be held when this is called.
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001514 * Afterwards, the futex_q must not be accessed. Callers
1515 * must ensure to later call wake_up_q() for the actual
1516 * wakeups to occur.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 */
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001518static void mark_wake_futex(struct wake_q_head *wake_q, struct futex_q *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001520 struct task_struct *p = q->task;
1521
Darren Hartaa109902012-11-26 16:29:56 -08001522 if (WARN(q->pi_state || q->rt_waiter, "refusing to wake PI futex\n"))
1523 return;
1524
Peter Zijlstrab061c382018-11-29 14:44:49 +01001525 get_task_struct(p);
Lai Jiangshan2e129782010-12-22 14:18:50 +08001526 __unqueue_futex(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 /*
Darren Hart (VMware)38fcd062017-04-14 15:31:38 -07001528 * The waiting task can free the futex_q as soon as q->lock_ptr = NULL
1529 * is written, without taking any locks. This is possible in the event
1530 * of a spurious wakeup, for example. A memory barrier is required here
1531 * to prevent the following store to lock_ptr from getting ahead of the
1532 * plist_del in __unqueue_futex().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 */
Peter Zijlstra1b367ec2017-03-22 11:35:49 +01001534 smp_store_release(&q->lock_ptr, NULL);
Peter Zijlstrab061c382018-11-29 14:44:49 +01001535
1536 /*
1537 * Queue the task for later wakeup for after we've released
Davidlohr Bueso75145902019-10-22 20:34:50 -07001538 * the hb->lock.
Peter Zijlstrab061c382018-11-29 14:44:49 +01001539 */
Davidlohr Bueso07879c62018-12-18 11:53:52 -08001540 wake_q_add_safe(wake_q, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541}
1542
Peter Zijlstra16ffa122017-03-22 11:35:55 +01001543/*
1544 * Caller must hold a reference on @pi_state.
1545 */
1546static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_pi_state *pi_state)
Ingo Molnarc87e2832006-06-27 02:54:58 -07001547{
Davidlohr Bueso9a4b99f2021-02-26 09:50:26 -08001548 struct rt_mutex_waiter *top_waiter;
Peter Zijlstra16ffa122017-03-22 11:35:55 +01001549 struct task_struct *new_owner;
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001550 bool postunlock = false;
Thomas Gleixner7980aa32021-08-15 23:28:09 +02001551 DEFINE_RT_WAKE_Q(wqh);
1552 u32 curval, newval;
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001553 int ret = 0;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001554
Davidlohr Bueso9a4b99f2021-02-26 09:50:26 -08001555 top_waiter = rt_mutex_top_waiter(&pi_state->pi_mutex);
1556 if (WARN_ON_ONCE(!top_waiter)) {
Peter Zijlstra16ffa122017-03-22 11:35:55 +01001557 /*
Peter Zijlstrabebe5b52017-03-22 11:35:59 +01001558 * As per the comment in futex_unlock_pi() this should not happen.
Peter Zijlstra16ffa122017-03-22 11:35:55 +01001559 *
1560 * When this happens, give up our locks and try again, giving
1561 * the futex_lock_pi() instance time to complete, either by
1562 * waiting on the rtmutex or removing itself from the futex
1563 * queue.
1564 */
1565 ret = -EAGAIN;
1566 goto out_unlock;
Peter Zijlstra73d786b2017-03-22 11:35:54 +01001567 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001568
Davidlohr Bueso9a4b99f2021-02-26 09:50:26 -08001569 new_owner = top_waiter->task;
1570
Ingo Molnarc87e2832006-06-27 02:54:58 -07001571 /*
Peter Zijlstra16ffa122017-03-22 11:35:55 +01001572 * We pass it to the next owner. The WAITERS bit is always kept
1573 * enabled while there is PI state around. We cleanup the owner
1574 * died bit, because we are the owner.
Ingo Molnarc87e2832006-06-27 02:54:58 -07001575 */
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001576 newval = FUTEX_WAITERS | task_pid_vnr(new_owner);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001577
Mateusz Nosek921c7eb2020-09-27 02:08:58 +02001578 if (unlikely(should_fail_futex(true))) {
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001579 ret = -EFAULT;
Mateusz Nosek921c7eb2020-09-27 02:08:58 +02001580 goto out_unlock;
1581 }
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001582
Will Deacon6b4f4bc2019-02-28 11:58:08 +00001583 ret = cmpxchg_futex_value_locked(&curval, uaddr, uval, newval);
1584 if (!ret && (curval != uval)) {
Sebastian Andrzej Siewior89e9e662016-04-15 14:35:39 +02001585 /*
1586 * If a unconditional UNLOCK_PI operation (user space did not
1587 * try the TID->0 transition) raced with a waiter setting the
1588 * FUTEX_WAITERS flag between get_user() and locking the hash
1589 * bucket lock, retry the operation.
1590 */
1591 if ((FUTEX_TID_MASK & curval) == uval)
1592 ret = -EAGAIN;
1593 else
1594 ret = -EINVAL;
1595 }
Peter Zijlstra734009e2017-03-22 11:35:52 +01001596
Thomas Gleixnerc5cade22021-01-19 15:21:35 +01001597 if (!ret) {
1598 /*
1599 * This is a point of no return; once we modified the uval
1600 * there is no going back and subsequent operations must
1601 * not fail.
1602 */
1603 pi_state_update_owner(pi_state, new_owner);
Thomas Gleixner7980aa32021-08-15 23:28:09 +02001604 postunlock = __rt_mutex_futex_unlock(&pi_state->pi_mutex, &wqh);
Thomas Gleixnerc5cade22021-01-19 15:21:35 +01001605 }
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001606
Peter Zijlstra16ffa122017-03-22 11:35:55 +01001607out_unlock:
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001608 raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01001609
Peter Zijlstraaa2bfe52017-03-23 15:56:10 +01001610 if (postunlock)
Thomas Gleixner7980aa32021-08-15 23:28:09 +02001611 rt_mutex_postunlock(&wqh);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001612
Peter Zijlstra16ffa122017-03-22 11:35:55 +01001613 return ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001614}
1615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616/*
Ingo Molnar8b8f3192006-07-03 00:25:05 -07001617 * Express the locking dependencies for lockdep:
1618 */
1619static inline void
1620double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
1621{
1622 if (hb1 <= hb2) {
1623 spin_lock(&hb1->lock);
1624 if (hb1 < hb2)
1625 spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING);
1626 } else { /* hb1 > hb2 */
1627 spin_lock(&hb2->lock);
1628 spin_lock_nested(&hb1->lock, SINGLE_DEPTH_NESTING);
1629 }
1630}
1631
Darren Hart5eb3dc62009-03-12 00:55:52 -07001632static inline void
1633double_unlock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
1634{
Darren Hartf061d352009-03-12 15:11:18 -07001635 spin_unlock(&hb1->lock);
Ingo Molnar88f502f2009-03-13 10:32:07 +01001636 if (hb1 != hb2)
1637 spin_unlock(&hb2->lock);
Darren Hart5eb3dc62009-03-12 00:55:52 -07001638}
1639
Ingo Molnar8b8f3192006-07-03 00:25:05 -07001640/*
Darren Hartb2d09942009-03-12 00:55:37 -07001641 * Wake up waiters matching bitset queued on this futex (uaddr).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 */
Darren Hartb41277d2010-11-08 13:10:09 -08001643static int
1644futex_wake(u32 __user *uaddr, unsigned int flags, int nr_wake, u32 bitset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
Ingo Molnare2970f22006-06-27 02:54:47 -07001646 struct futex_hash_bucket *hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 struct futex_q *this, *next;
Peter Zijlstra38d47c12008-09-26 19:32:20 +02001648 union futex_key key = FUTEX_KEY_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 int ret;
Waiman Long194a6b52016-11-17 11:46:38 -05001650 DEFINE_WAKE_Q(wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Thomas Gleixnercd689982008-02-01 17:45:14 +01001652 if (!bitset)
1653 return -EINVAL;
1654
Linus Torvalds96d4f262019-01-03 18:57:57 -08001655 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, FUTEX_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 if (unlikely(ret != 0))
André Almeidad7c5ed72020-07-02 17:28:41 -03001657 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Ingo Molnare2970f22006-06-27 02:54:47 -07001659 hb = hash_futex(&key);
Davidlohr Buesob0c29f72014-01-12 15:31:25 -08001660
1661 /* Make sure we really have tasks to wakeup */
1662 if (!hb_waiters_pending(hb))
André Almeidad7c5ed72020-07-02 17:28:41 -03001663 return ret;
Davidlohr Buesob0c29f72014-01-12 15:31:25 -08001664
Ingo Molnare2970f22006-06-27 02:54:47 -07001665 spin_lock(&hb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Jason Low0d00c7b2014-01-12 15:31:22 -08001667 plist_for_each_entry_safe(this, next, &hb->chain, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 if (match_futex (&this->key, &key)) {
Darren Hart52400ba2009-04-03 13:40:49 -07001669 if (this->pi_state || this->rt_waiter) {
Ingo Molnared6f7b12006-07-01 04:35:46 -07001670 ret = -EINVAL;
1671 break;
1672 }
Thomas Gleixnercd689982008-02-01 17:45:14 +01001673
1674 /* Check if one of the bits is set in both bitsets */
1675 if (!(this->bitset & bitset))
1676 continue;
1677
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001678 mark_wake_futex(&wake_q, this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 if (++ret >= nr_wake)
1680 break;
1681 }
1682 }
1683
Ingo Molnare2970f22006-06-27 02:54:47 -07001684 spin_unlock(&hb->lock);
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001685 wake_up_q(&wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 return ret;
1687}
1688
Jiri Slaby30d6e0a2017-08-24 09:31:05 +02001689static int futex_atomic_op_inuser(unsigned int encoded_op, u32 __user *uaddr)
1690{
1691 unsigned int op = (encoded_op & 0x70000000) >> 28;
1692 unsigned int cmp = (encoded_op & 0x0f000000) >> 24;
Jiri Slabyd70ef222017-11-30 15:35:44 +01001693 int oparg = sign_extend32((encoded_op & 0x00fff000) >> 12, 11);
1694 int cmparg = sign_extend32(encoded_op & 0x00000fff, 11);
Jiri Slaby30d6e0a2017-08-24 09:31:05 +02001695 int oldval, ret;
1696
1697 if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28)) {
Jiri Slabye78c38f62017-10-23 13:41:51 +02001698 if (oparg < 0 || oparg > 31) {
1699 char comm[sizeof(current->comm)];
1700 /*
1701 * kill this print and return -EINVAL when userspace
1702 * is sane again
1703 */
1704 pr_info_ratelimited("futex_wake_op: %s tries to shift op by %d; fix this program\n",
1705 get_task_comm(comm, current), oparg);
1706 oparg &= 31;
1707 }
Jiri Slaby30d6e0a2017-08-24 09:31:05 +02001708 oparg = 1 << oparg;
1709 }
1710
Al Viroa08971e2020-02-16 10:17:27 -05001711 pagefault_disable();
Jiri Slaby30d6e0a2017-08-24 09:31:05 +02001712 ret = arch_futex_atomic_op_inuser(op, oparg, &oldval, uaddr);
Al Viroa08971e2020-02-16 10:17:27 -05001713 pagefault_enable();
Jiri Slaby30d6e0a2017-08-24 09:31:05 +02001714 if (ret)
1715 return ret;
1716
1717 switch (cmp) {
1718 case FUTEX_OP_CMP_EQ:
1719 return oldval == cmparg;
1720 case FUTEX_OP_CMP_NE:
1721 return oldval != cmparg;
1722 case FUTEX_OP_CMP_LT:
1723 return oldval < cmparg;
1724 case FUTEX_OP_CMP_GE:
1725 return oldval >= cmparg;
1726 case FUTEX_OP_CMP_LE:
1727 return oldval <= cmparg;
1728 case FUTEX_OP_CMP_GT:
1729 return oldval > cmparg;
1730 default:
1731 return -ENOSYS;
1732 }
1733}
1734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735/*
Jakub Jelinek4732efbe2005-09-06 15:16:25 -07001736 * Wake up all waiters hashed on the physical page that is mapped
1737 * to this virtual address:
1738 */
Ingo Molnare2970f22006-06-27 02:54:47 -07001739static int
Darren Hartb41277d2010-11-08 13:10:09 -08001740futex_wake_op(u32 __user *uaddr1, unsigned int flags, u32 __user *uaddr2,
Ingo Molnare2970f22006-06-27 02:54:47 -07001741 int nr_wake, int nr_wake2, int op)
Jakub Jelinek4732efbe2005-09-06 15:16:25 -07001742{
Peter Zijlstra38d47c12008-09-26 19:32:20 +02001743 union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
Ingo Molnare2970f22006-06-27 02:54:47 -07001744 struct futex_hash_bucket *hb1, *hb2;
Jakub Jelinek4732efbe2005-09-06 15:16:25 -07001745 struct futex_q *this, *next;
Darren Harte4dc5b72009-03-12 00:56:13 -07001746 int ret, op_ret;
Waiman Long194a6b52016-11-17 11:46:38 -05001747 DEFINE_WAKE_Q(wake_q);
Jakub Jelinek4732efbe2005-09-06 15:16:25 -07001748
Darren Harte4dc5b72009-03-12 00:56:13 -07001749retry:
Linus Torvalds96d4f262019-01-03 18:57:57 -08001750 ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, FUTEX_READ);
Jakub Jelinek4732efbe2005-09-06 15:16:25 -07001751 if (unlikely(ret != 0))
André Almeidad7c5ed72020-07-02 17:28:41 -03001752 return ret;
Linus Torvalds96d4f262019-01-03 18:57:57 -08001753 ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, FUTEX_WRITE);
Jakub Jelinek4732efbe2005-09-06 15:16:25 -07001754 if (unlikely(ret != 0))
André Almeidad7c5ed72020-07-02 17:28:41 -03001755 return ret;
Jakub Jelinek4732efbe2005-09-06 15:16:25 -07001756
Ingo Molnare2970f22006-06-27 02:54:47 -07001757 hb1 = hash_futex(&key1);
1758 hb2 = hash_futex(&key2);
Jakub Jelinek4732efbe2005-09-06 15:16:25 -07001759
Darren Harte4dc5b72009-03-12 00:56:13 -07001760retry_private:
Thomas Gleixnereaaea802009-10-04 09:34:17 +02001761 double_lock_hb(hb1, hb2);
Ingo Molnare2970f22006-06-27 02:54:47 -07001762 op_ret = futex_atomic_op_inuser(op, uaddr2);
Jakub Jelinek4732efbe2005-09-06 15:16:25 -07001763 if (unlikely(op_ret < 0)) {
Darren Hart5eb3dc62009-03-12 00:55:52 -07001764 double_unlock_hb(hb1, hb2);
Jakub Jelinek4732efbe2005-09-06 15:16:25 -07001765
Will Deacon6b4f4bc2019-02-28 11:58:08 +00001766 if (!IS_ENABLED(CONFIG_MMU) ||
1767 unlikely(op_ret != -EFAULT && op_ret != -EAGAIN)) {
1768 /*
1769 * we don't get EFAULT from MMU faults if we don't have
1770 * an MMU, but we might get them from range checking
1771 */
David Gibson796f8d92005-11-07 00:59:33 -08001772 ret = op_ret;
André Almeidad7c5ed72020-07-02 17:28:41 -03001773 return ret;
David Gibson796f8d92005-11-07 00:59:33 -08001774 }
1775
Will Deacon6b4f4bc2019-02-28 11:58:08 +00001776 if (op_ret == -EFAULT) {
1777 ret = fault_in_user_writeable(uaddr2);
1778 if (ret)
André Almeidad7c5ed72020-07-02 17:28:41 -03001779 return ret;
Will Deacon6b4f4bc2019-02-28 11:58:08 +00001780 }
Jakub Jelinek4732efbe2005-09-06 15:16:25 -07001781
Will Deacon6b4f4bc2019-02-28 11:58:08 +00001782 cond_resched();
Pavel Begunkova82adc72021-05-17 14:30:12 +01001783 if (!(flags & FLAGS_SHARED))
1784 goto retry_private;
Darren Harte4dc5b72009-03-12 00:56:13 -07001785 goto retry;
Jakub Jelinek4732efbe2005-09-06 15:16:25 -07001786 }
1787
Jason Low0d00c7b2014-01-12 15:31:22 -08001788 plist_for_each_entry_safe(this, next, &hb1->chain, list) {
Jakub Jelinek4732efbe2005-09-06 15:16:25 -07001789 if (match_futex (&this->key, &key1)) {
Darren Hartaa109902012-11-26 16:29:56 -08001790 if (this->pi_state || this->rt_waiter) {
1791 ret = -EINVAL;
1792 goto out_unlock;
1793 }
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001794 mark_wake_futex(&wake_q, this);
Jakub Jelinek4732efbe2005-09-06 15:16:25 -07001795 if (++ret >= nr_wake)
1796 break;
1797 }
1798 }
1799
1800 if (op_ret > 0) {
Jakub Jelinek4732efbe2005-09-06 15:16:25 -07001801 op_ret = 0;
Jason Low0d00c7b2014-01-12 15:31:22 -08001802 plist_for_each_entry_safe(this, next, &hb2->chain, list) {
Jakub Jelinek4732efbe2005-09-06 15:16:25 -07001803 if (match_futex (&this->key, &key2)) {
Darren Hartaa109902012-11-26 16:29:56 -08001804 if (this->pi_state || this->rt_waiter) {
1805 ret = -EINVAL;
1806 goto out_unlock;
1807 }
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001808 mark_wake_futex(&wake_q, this);
Jakub Jelinek4732efbe2005-09-06 15:16:25 -07001809 if (++op_ret >= nr_wake2)
1810 break;
1811 }
1812 }
1813 ret += op_ret;
1814 }
1815
Darren Hartaa109902012-11-26 16:29:56 -08001816out_unlock:
Darren Hart5eb3dc62009-03-12 00:55:52 -07001817 double_unlock_hb(hb1, hb2);
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001818 wake_up_q(&wake_q);
Jakub Jelinek4732efbe2005-09-06 15:16:25 -07001819 return ret;
1820}
1821
Darren Hart9121e472009-04-03 13:40:31 -07001822/**
1823 * requeue_futex() - Requeue a futex_q from one hb to another
1824 * @q: the futex_q to requeue
1825 * @hb1: the source hash_bucket
1826 * @hb2: the target hash_bucket
1827 * @key2: the new key for the requeued futex_q
1828 */
1829static inline
1830void requeue_futex(struct futex_q *q, struct futex_hash_bucket *hb1,
1831 struct futex_hash_bucket *hb2, union futex_key *key2)
1832{
1833
1834 /*
1835 * If key1 and key2 hash to the same bucket, no need to
1836 * requeue.
1837 */
1838 if (likely(&hb1->chain != &hb2->chain)) {
1839 plist_del(&q->list, &hb1->chain);
Linus Torvalds11d46162014-03-20 22:11:17 -07001840 hb_waiters_dec(hb1);
Linus Torvalds11d46162014-03-20 22:11:17 -07001841 hb_waiters_inc(hb2);
Davidlohr Buesofe1bce92016-04-20 20:09:24 -07001842 plist_add(&q->list, &hb2->chain);
Darren Hart9121e472009-04-03 13:40:31 -07001843 q->lock_ptr = &hb2->lock;
Darren Hart9121e472009-04-03 13:40:31 -07001844 }
Darren Hart9121e472009-04-03 13:40:31 -07001845 q->key = *key2;
1846}
1847
Thomas Gleixner07d91ef52021-08-15 23:29:18 +02001848static inline bool futex_requeue_pi_prepare(struct futex_q *q,
1849 struct futex_pi_state *pi_state)
1850{
1851 int old, new;
1852
1853 /*
1854 * Set state to Q_REQUEUE_PI_IN_PROGRESS unless an early wakeup has
1855 * already set Q_REQUEUE_PI_IGNORE to signal that requeue should
1856 * ignore the waiter.
1857 */
1858 old = atomic_read_acquire(&q->requeue_state);
1859 do {
1860 if (old == Q_REQUEUE_PI_IGNORE)
1861 return false;
1862
1863 /*
1864 * futex_proxy_trylock_atomic() might have set it to
1865 * IN_PROGRESS and a interleaved early wake to WAIT.
1866 *
1867 * It was considered to have an extra state for that
1868 * trylock, but that would just add more conditionals
1869 * all over the place for a dubious value.
1870 */
1871 if (old != Q_REQUEUE_PI_NONE)
1872 break;
1873
1874 new = Q_REQUEUE_PI_IN_PROGRESS;
1875 } while (!atomic_try_cmpxchg(&q->requeue_state, &old, new));
1876
1877 q->pi_state = pi_state;
1878 return true;
1879}
1880
1881static inline void futex_requeue_pi_complete(struct futex_q *q, int locked)
1882{
1883 int old, new;
1884
1885 old = atomic_read_acquire(&q->requeue_state);
1886 do {
1887 if (old == Q_REQUEUE_PI_IGNORE)
1888 return;
1889
1890 if (locked >= 0) {
1891 /* Requeue succeeded. Set DONE or LOCKED */
1892 WARN_ON_ONCE(old != Q_REQUEUE_PI_IN_PROGRESS &&
1893 old != Q_REQUEUE_PI_WAIT);
1894 new = Q_REQUEUE_PI_DONE + locked;
1895 } else if (old == Q_REQUEUE_PI_IN_PROGRESS) {
1896 /* Deadlock, no early wakeup interleave */
1897 new = Q_REQUEUE_PI_NONE;
1898 } else {
1899 /* Deadlock, early wakeup interleave. */
1900 WARN_ON_ONCE(old != Q_REQUEUE_PI_WAIT);
1901 new = Q_REQUEUE_PI_IGNORE;
1902 }
1903 } while (!atomic_try_cmpxchg(&q->requeue_state, &old, new));
1904
1905#ifdef CONFIG_PREEMPT_RT
1906 /* If the waiter interleaved with the requeue let it know */
1907 if (unlikely(old == Q_REQUEUE_PI_WAIT))
1908 rcuwait_wake_up(&q->requeue_wait);
1909#endif
1910}
1911
1912static inline int futex_requeue_pi_wakeup_sync(struct futex_q *q)
1913{
1914 int old, new;
1915
1916 old = atomic_read_acquire(&q->requeue_state);
1917 do {
1918 /* Is requeue done already? */
1919 if (old >= Q_REQUEUE_PI_DONE)
1920 return old;
1921
1922 /*
1923 * If not done, then tell the requeue code to either ignore
1924 * the waiter or to wake it up once the requeue is done.
1925 */
1926 new = Q_REQUEUE_PI_WAIT;
1927 if (old == Q_REQUEUE_PI_NONE)
1928 new = Q_REQUEUE_PI_IGNORE;
1929 } while (!atomic_try_cmpxchg(&q->requeue_state, &old, new));
1930
1931 /* If the requeue was in progress, wait for it to complete */
1932 if (old == Q_REQUEUE_PI_IN_PROGRESS) {
1933#ifdef CONFIG_PREEMPT_RT
1934 rcuwait_wait_event(&q->requeue_wait,
1935 atomic_read(&q->requeue_state) != Q_REQUEUE_PI_WAIT,
1936 TASK_UNINTERRUPTIBLE);
1937#else
1938 (void)atomic_cond_read_relaxed(&q->requeue_state, VAL != Q_REQUEUE_PI_WAIT);
1939#endif
1940 }
1941
1942 /*
1943 * Requeue is now either prohibited or complete. Reread state
1944 * because during the wait above it might have changed. Nothing
1945 * will modify q->requeue_state after this point.
1946 */
1947 return atomic_read(&q->requeue_state);
1948}
1949
Darren Hart52400ba2009-04-03 13:40:49 -07001950/**
1951 * requeue_pi_wake_futex() - Wake a task that acquired the lock during requeue
Darren Hartd96ee562009-09-21 22:30:22 -07001952 * @q: the futex_q
1953 * @key: the key of the requeue target futex
1954 * @hb: the hash_bucket of the requeue target futex
Darren Hart52400ba2009-04-03 13:40:49 -07001955 *
1956 * During futex_requeue, with requeue_pi=1, it is possible to acquire the
Thomas Gleixner249955e2021-09-02 11:48:50 +02001957 * target futex if it is uncontended or via a lock steal.
1958 *
1959 * 1) Set @q::key to the requeue target futex key so the waiter can detect
1960 * the wakeup on the right futex.
1961 *
1962 * 2) Dequeue @q from the hash bucket.
1963 *
1964 * 3) Set @q::rt_waiter to NULL so the woken up task can detect atomic lock
1965 * acquisition.
1966 *
1967 * 4) Set the q->lock_ptr to the requeue target hb->lock for the case that
1968 * the waiter has to fixup the pi state.
1969 *
1970 * 5) Complete the requeue state so the waiter can make progress. After
1971 * this point the waiter task can return from the syscall immediately in
1972 * case that the pi state does not have to be fixed up.
1973 *
1974 * 6) Wake the waiter task.
1975 *
1976 * Must be called with both q->lock_ptr and hb->lock held.
Darren Hart52400ba2009-04-03 13:40:49 -07001977 */
1978static inline
Darren Hartbeda2c72009-08-09 15:34:39 -07001979void requeue_pi_wake_futex(struct futex_q *q, union futex_key *key,
1980 struct futex_hash_bucket *hb)
Darren Hart52400ba2009-04-03 13:40:49 -07001981{
Darren Hart52400ba2009-04-03 13:40:49 -07001982 q->key = *key;
1983
Lai Jiangshan2e129782010-12-22 14:18:50 +08001984 __unqueue_futex(q);
Darren Hart52400ba2009-04-03 13:40:49 -07001985
1986 WARN_ON(!q->rt_waiter);
1987 q->rt_waiter = NULL;
1988
Darren Hartbeda2c72009-08-09 15:34:39 -07001989 q->lock_ptr = &hb->lock;
Darren Hartbeda2c72009-08-09 15:34:39 -07001990
Thomas Gleixner07d91ef52021-08-15 23:29:18 +02001991 /* Signal locked state to the waiter */
1992 futex_requeue_pi_complete(q, 1);
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001993 wake_up_state(q->task, TASK_NORMAL);
Darren Hart52400ba2009-04-03 13:40:49 -07001994}
1995
1996/**
1997 * futex_proxy_trylock_atomic() - Attempt an atomic lock for the top waiter
Darren Hartbab5bc92009-04-07 23:23:50 -07001998 * @pifutex: the user address of the to futex
1999 * @hb1: the from futex hash bucket, must be locked by the caller
2000 * @hb2: the to futex hash bucket, must be locked by the caller
2001 * @key1: the from futex key
2002 * @key2: the to futex key
2003 * @ps: address to store the pi_state pointer
Thomas Gleixner3ef240e2019-11-06 22:55:46 +01002004 * @exiting: Pointer to store the task pointer of the owner task
2005 * which is in the middle of exiting
Darren Hartbab5bc92009-04-07 23:23:50 -07002006 * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0)
Darren Hart52400ba2009-04-03 13:40:49 -07002007 *
2008 * Try and get the lock on behalf of the top waiter if we can do it atomically.
Darren Hartbab5bc92009-04-07 23:23:50 -07002009 * Wake the top waiter if we succeed. If the caller specified set_waiters,
2010 * then direct futex_lock_pi_atomic() to force setting the FUTEX_WAITERS bit.
2011 * hb1 and hb2 must be held by the caller.
Darren Hart52400ba2009-04-03 13:40:49 -07002012 *
Thomas Gleixner3ef240e2019-11-06 22:55:46 +01002013 * @exiting is only set when the return value is -EBUSY. If so, this holds
2014 * a refcount on the exiting task on return and the caller needs to drop it
2015 * after waiting for the exit to complete.
2016 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002017 * Return:
Mauro Carvalho Chehab7b4ff1a2017-05-11 10:17:45 -03002018 * - 0 - failed to acquire the lock atomically;
2019 * - >0 - acquired the lock, return value is vpid of the top_waiter
2020 * - <0 - error
Darren Hart52400ba2009-04-03 13:40:49 -07002021 */
Thomas Gleixner3ef240e2019-11-06 22:55:46 +01002022static int
2023futex_proxy_trylock_atomic(u32 __user *pifutex, struct futex_hash_bucket *hb1,
2024 struct futex_hash_bucket *hb2, union futex_key *key1,
2025 union futex_key *key2, struct futex_pi_state **ps,
2026 struct task_struct **exiting, int set_waiters)
Darren Hart52400ba2009-04-03 13:40:49 -07002027{
Darren Hartbab5bc92009-04-07 23:23:50 -07002028 struct futex_q *top_waiter = NULL;
Darren Hart52400ba2009-04-03 13:40:49 -07002029 u32 curval;
Thomas Gleixner866293e2014-05-12 20:45:34 +00002030 int ret, vpid;
Darren Hart52400ba2009-04-03 13:40:49 -07002031
2032 if (get_futex_value_locked(&curval, pifutex))
2033 return -EFAULT;
2034
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07002035 if (unlikely(should_fail_futex(true)))
2036 return -EFAULT;
2037
Darren Hartbab5bc92009-04-07 23:23:50 -07002038 /*
2039 * Find the top_waiter and determine if there are additional waiters.
2040 * If the caller intends to requeue more than 1 waiter to pifutex,
2041 * force futex_lock_pi_atomic() to set the FUTEX_WAITERS bit now,
2042 * as we have means to handle the possible fault. If not, don't set
Ingo Molnar93d09552021-05-12 20:04:28 +02002043 * the bit unnecessarily as it will force the subsequent unlock to enter
Darren Hartbab5bc92009-04-07 23:23:50 -07002044 * the kernel.
2045 */
Darren Hart52400ba2009-04-03 13:40:49 -07002046 top_waiter = futex_top_waiter(hb1, key1);
2047
2048 /* There are no waiters, nothing for us to do. */
2049 if (!top_waiter)
2050 return 0;
2051
Thomas Gleixnerdc7109a2021-08-15 23:29:04 +02002052 /*
2053 * Ensure that this is a waiter sitting in futex_wait_requeue_pi()
2054 * and waiting on the 'waitqueue' futex which is always !PI.
2055 */
2056 if (!top_waiter->rt_waiter || top_waiter->pi_state)
Colin Ian Kinga974b542021-08-18 14:18:40 +01002057 return -EINVAL;
Thomas Gleixnerdc7109a2021-08-15 23:29:04 +02002058
Darren Hart84bc4af2009-08-13 17:36:53 -07002059 /* Ensure we requeue to the expected futex. */
2060 if (!match_futex(top_waiter->requeue_pi_key, key2))
2061 return -EINVAL;
2062
Thomas Gleixner07d91ef52021-08-15 23:29:18 +02002063 /* Ensure that this does not race against an early wakeup */
2064 if (!futex_requeue_pi_prepare(top_waiter, NULL))
2065 return -EAGAIN;
2066
Darren Hart52400ba2009-04-03 13:40:49 -07002067 /*
Thomas Gleixner4f07ec02021-09-02 11:48:48 +02002068 * Try to take the lock for top_waiter and set the FUTEX_WAITERS bit
2069 * in the contended case or if @set_waiters is true.
2070 *
2071 * In the contended case PI state is attached to the lock owner. If
2072 * the user space lock can be acquired then PI state is attached to
2073 * the new owner (@top_waiter->task) when @set_waiters is true.
Darren Hart52400ba2009-04-03 13:40:49 -07002074 */
Thomas Gleixner866293e2014-05-12 20:45:34 +00002075 vpid = task_pid_vnr(top_waiter->task);
Darren Hartbab5bc92009-04-07 23:23:50 -07002076 ret = futex_lock_pi_atomic(pifutex, hb2, key2, ps, top_waiter->task,
Thomas Gleixner3ef240e2019-11-06 22:55:46 +01002077 exiting, set_waiters);
Thomas Gleixner866293e2014-05-12 20:45:34 +00002078 if (ret == 1) {
Thomas Gleixner4f07ec02021-09-02 11:48:48 +02002079 /*
2080 * Lock was acquired in user space and PI state was
2081 * attached to @top_waiter->task. That means state is fully
2082 * consistent and the waiter can return to user space
2083 * immediately after the wakeup.
2084 */
Darren Hartbeda2c72009-08-09 15:34:39 -07002085 requeue_pi_wake_futex(top_waiter, key2, hb2);
Thomas Gleixner07d91ef52021-08-15 23:29:18 +02002086 } else if (ret < 0) {
2087 /* Rewind top_waiter::requeue_state */
2088 futex_requeue_pi_complete(top_waiter, ret);
2089 } else {
2090 /*
2091 * futex_lock_pi_atomic() did not acquire the user space
2092 * futex, but managed to establish the proxy lock and pi
2093 * state. top_waiter::requeue_state cannot be fixed up here
2094 * because the waiter is not enqueued on the rtmutex
2095 * yet. This is handled at the callsite depending on the
2096 * result of rt_mutex_start_proxy_lock() which is
2097 * guaranteed to be reached with this function returning 0.
2098 */
Thomas Gleixner866293e2014-05-12 20:45:34 +00002099 }
Darren Hart52400ba2009-04-03 13:40:49 -07002100 return ret;
2101}
2102
2103/**
2104 * futex_requeue() - Requeue waiters from uaddr1 to uaddr2
Randy Dunlapfb62db22010-10-13 11:02:34 -07002105 * @uaddr1: source futex user address
Darren Hartb41277d2010-11-08 13:10:09 -08002106 * @flags: futex flags (FLAGS_SHARED, etc.)
Randy Dunlapfb62db22010-10-13 11:02:34 -07002107 * @uaddr2: target futex user address
2108 * @nr_wake: number of waiters to wake (must be 1 for requeue_pi)
2109 * @nr_requeue: number of waiters to requeue (0-INT_MAX)
2110 * @cmpval: @uaddr1 expected value (or %NULL)
2111 * @requeue_pi: if we are attempting to requeue from a non-pi futex to a
Darren Hartb41277d2010-11-08 13:10:09 -08002112 * pi futex (pi to pi requeue is not supported)
Darren Hart52400ba2009-04-03 13:40:49 -07002113 *
2114 * Requeue waiters on uaddr1 to uaddr2. In the requeue_pi case, try to acquire
2115 * uaddr2 atomically on behalf of the top waiter.
2116 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002117 * Return:
Mauro Carvalho Chehab7b4ff1a2017-05-11 10:17:45 -03002118 * - >=0 - on success, the number of tasks requeued or woken;
2119 * - <0 - on error
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 */
Darren Hartb41277d2010-11-08 13:10:09 -08002121static int futex_requeue(u32 __user *uaddr1, unsigned int flags,
2122 u32 __user *uaddr2, int nr_wake, int nr_requeue,
2123 u32 *cmpval, int requeue_pi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
Peter Zijlstra38d47c12008-09-26 19:32:20 +02002125 union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
Peter Zijlstra4b39f992020-03-04 13:24:24 +01002126 int task_count = 0, ret;
Darren Hart52400ba2009-04-03 13:40:49 -07002127 struct futex_pi_state *pi_state = NULL;
Ingo Molnare2970f22006-06-27 02:54:47 -07002128 struct futex_hash_bucket *hb1, *hb2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 struct futex_q *this, *next;
Waiman Long194a6b52016-11-17 11:46:38 -05002130 DEFINE_WAKE_Q(wake_q);
Darren Hart52400ba2009-04-03 13:40:49 -07002131
Li Jinyuefbe0e832017-12-14 17:04:54 +08002132 if (nr_wake < 0 || nr_requeue < 0)
2133 return -EINVAL;
2134
Nicolas Pitrebc2eecd2017-08-01 00:31:32 -04002135 /*
2136 * When PI not supported: return -ENOSYS if requeue_pi is true,
2137 * consequently the compiler knows requeue_pi is always false past
2138 * this point which will optimize away all the conditional code
2139 * further down.
2140 */
2141 if (!IS_ENABLED(CONFIG_FUTEX_PI) && requeue_pi)
2142 return -ENOSYS;
2143
Darren Hart52400ba2009-04-03 13:40:49 -07002144 if (requeue_pi) {
2145 /*
Thomas Gleixnere9c243a2014-06-03 12:27:06 +00002146 * Requeue PI only works on two distinct uaddrs. This
2147 * check is only valid for private futexes. See below.
2148 */
2149 if (uaddr1 == uaddr2)
2150 return -EINVAL;
2151
2152 /*
Thomas Gleixnerc18eaa32021-08-15 23:29:14 +02002153 * futex_requeue() allows the caller to define the number
2154 * of waiters to wake up via the @nr_wake argument. With
2155 * REQUEUE_PI, waking up more than one waiter is creating
2156 * more problems than it solves. Waking up a waiter makes
2157 * only sense if the PI futex @uaddr2 is uncontended as
2158 * this allows the requeue code to acquire the futex
2159 * @uaddr2 before waking the waiter. The waiter can then
2160 * return to user space without further action. A secondary
2161 * wakeup would just make the futex_wait_requeue_pi()
2162 * handling more complex, because that code would have to
2163 * look up pi_state and do more or less all the handling
2164 * which the requeue code has to do for the to be requeued
2165 * waiters. So restrict the number of waiters to wake to
2166 * one, and only wake it up when the PI futex is
2167 * uncontended. Otherwise requeue it and let the unlock of
2168 * the PI futex handle the wakeup.
2169 *
2170 * All REQUEUE_PI users, e.g. pthread_cond_signal() and
2171 * pthread_cond_broadcast() must use nr_wake=1.
Darren Hart52400ba2009-04-03 13:40:49 -07002172 */
2173 if (nr_wake != 1)
2174 return -EINVAL;
Thomas Gleixnerd69cba52021-08-15 23:29:15 +02002175
2176 /*
2177 * requeue_pi requires a pi_state, try to allocate it now
2178 * without any locks in case it fails.
2179 */
2180 if (refill_pi_state_cache())
2181 return -ENOMEM;
Darren Hart52400ba2009-04-03 13:40:49 -07002182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Darren Hart42d35d42008-12-29 15:49:53 -08002184retry:
Linus Torvalds96d4f262019-01-03 18:57:57 -08002185 ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, FUTEX_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 if (unlikely(ret != 0))
André Almeidad7c5ed72020-07-02 17:28:41 -03002187 return ret;
Shawn Bohrer9ea71502011-06-30 11:21:32 -05002188 ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2,
Linus Torvalds96d4f262019-01-03 18:57:57 -08002189 requeue_pi ? FUTEX_WRITE : FUTEX_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 if (unlikely(ret != 0))
André Almeidad7c5ed72020-07-02 17:28:41 -03002191 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
Thomas Gleixnere9c243a2014-06-03 12:27:06 +00002193 /*
2194 * The check above which compares uaddrs is not sufficient for
2195 * shared futexes. We need to compare the keys:
2196 */
André Almeidad7c5ed72020-07-02 17:28:41 -03002197 if (requeue_pi && match_futex(&key1, &key2))
2198 return -EINVAL;
Thomas Gleixnere9c243a2014-06-03 12:27:06 +00002199
Ingo Molnare2970f22006-06-27 02:54:47 -07002200 hb1 = hash_futex(&key1);
2201 hb2 = hash_futex(&key2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
Darren Harte4dc5b72009-03-12 00:56:13 -07002203retry_private:
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07002204 hb_waiters_inc(hb2);
Ingo Molnar8b8f3192006-07-03 00:25:05 -07002205 double_lock_hb(hb1, hb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Ingo Molnare2970f22006-06-27 02:54:47 -07002207 if (likely(cmpval != NULL)) {
2208 u32 curval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Ingo Molnare2970f22006-06-27 02:54:47 -07002210 ret = get_futex_value_locked(&curval, uaddr1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
2212 if (unlikely(ret)) {
Darren Hart5eb3dc62009-03-12 00:55:52 -07002213 double_unlock_hb(hb1, hb2);
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07002214 hb_waiters_dec(hb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Darren Harte4dc5b72009-03-12 00:56:13 -07002216 ret = get_user(curval, uaddr1);
2217 if (ret)
André Almeidad7c5ed72020-07-02 17:28:41 -03002218 return ret;
Darren Harte4dc5b72009-03-12 00:56:13 -07002219
Darren Hartb41277d2010-11-08 13:10:09 -08002220 if (!(flags & FLAGS_SHARED))
Darren Harte4dc5b72009-03-12 00:56:13 -07002221 goto retry_private;
2222
Darren Harte4dc5b72009-03-12 00:56:13 -07002223 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 }
Ingo Molnare2970f22006-06-27 02:54:47 -07002225 if (curval != *cmpval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 ret = -EAGAIN;
2227 goto out_unlock;
2228 }
2229 }
2230
Thomas Gleixner8e746332021-08-15 23:29:09 +02002231 if (requeue_pi) {
Thomas Gleixner3ef240e2019-11-06 22:55:46 +01002232 struct task_struct *exiting = NULL;
2233
Darren Hartbab5bc92009-04-07 23:23:50 -07002234 /*
2235 * Attempt to acquire uaddr2 and wake the top waiter. If we
2236 * intend to requeue waiters, force setting the FUTEX_WAITERS
2237 * bit. We force this here where we are able to easily handle
2238 * faults rather in the requeue loop below.
Thomas Gleixner07d91ef52021-08-15 23:29:18 +02002239 *
2240 * Updates topwaiter::requeue_state if a top waiter exists.
Darren Hartbab5bc92009-04-07 23:23:50 -07002241 */
Darren Hart52400ba2009-04-03 13:40:49 -07002242 ret = futex_proxy_trylock_atomic(uaddr2, hb1, hb2, &key1,
Thomas Gleixner3ef240e2019-11-06 22:55:46 +01002243 &key2, &pi_state,
2244 &exiting, nr_requeue);
Darren Hart52400ba2009-04-03 13:40:49 -07002245
2246 /*
Thomas Gleixner4f07ec02021-09-02 11:48:48 +02002247 * At this point the top_waiter has either taken uaddr2 or
2248 * is waiting on it. In both cases pi_state has been
2249 * established and an initial refcount on it. In case of an
2250 * error there's nothing.
Thomas Gleixner07d91ef52021-08-15 23:29:18 +02002251 *
2252 * The top waiter's requeue_state is up to date:
2253 *
Thomas Gleixner4f07ec02021-09-02 11:48:48 +02002254 * - If the lock was acquired atomically (ret == 1), then
Thomas Gleixner07d91ef52021-08-15 23:29:18 +02002255 * the state is Q_REQUEUE_PI_LOCKED.
2256 *
Thomas Gleixner4f07ec02021-09-02 11:48:48 +02002257 * The top waiter has been dequeued and woken up and can
2258 * return to user space immediately. The kernel/user
2259 * space state is consistent. In case that there must be
2260 * more waiters requeued the WAITERS bit in the user
2261 * space futex is set so the top waiter task has to go
2262 * into the syscall slowpath to unlock the futex. This
2263 * will block until this requeue operation has been
2264 * completed and the hash bucket locks have been
2265 * dropped.
2266 *
Thomas Gleixner07d91ef52021-08-15 23:29:18 +02002267 * - If the trylock failed with an error (ret < 0) then
2268 * the state is either Q_REQUEUE_PI_NONE, i.e. "nothing
2269 * happened", or Q_REQUEUE_PI_IGNORE when there was an
2270 * interleaved early wakeup.
2271 *
2272 * - If the trylock did not succeed (ret == 0) then the
2273 * state is either Q_REQUEUE_PI_IN_PROGRESS or
2274 * Q_REQUEUE_PI_WAIT if an early wakeup interleaved.
2275 * This will be cleaned up in the loop below, which
2276 * cannot fail because futex_proxy_trylock_atomic() did
2277 * the same sanity checks for requeue_pi as the loop
2278 * below does.
Darren Hart52400ba2009-04-03 13:40:49 -07002279 */
Darren Hart52400ba2009-04-03 13:40:49 -07002280 switch (ret) {
2281 case 0:
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00002282 /* We hold a reference on the pi state. */
Darren Hart52400ba2009-04-03 13:40:49 -07002283 break;
Thomas Gleixner4959f2d2015-12-19 20:07:40 +00002284
Thomas Gleixner4f07ec02021-09-02 11:48:48 +02002285 case 1:
2286 /*
2287 * futex_proxy_trylock_atomic() acquired the user space
2288 * futex. Adjust task_count.
2289 */
2290 task_count++;
2291 ret = 0;
2292 break;
2293
Thomas Gleixner07d91ef52021-08-15 23:29:18 +02002294 /*
2295 * If the above failed, then pi_state is NULL and
2296 * waiter::requeue_state is correct.
2297 */
Darren Hart52400ba2009-04-03 13:40:49 -07002298 case -EFAULT:
2299 double_unlock_hb(hb1, hb2);
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07002300 hb_waiters_dec(hb2);
Thomas Gleixnerd0725992009-06-11 23:15:43 +02002301 ret = fault_in_user_writeable(uaddr2);
Darren Hart52400ba2009-04-03 13:40:49 -07002302 if (!ret)
2303 goto retry;
André Almeidad7c5ed72020-07-02 17:28:41 -03002304 return ret;
Thomas Gleixnerac31c7f2019-11-06 22:55:45 +01002305 case -EBUSY:
Darren Hart52400ba2009-04-03 13:40:49 -07002306 case -EAGAIN:
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00002307 /*
2308 * Two reasons for this:
Thomas Gleixnerac31c7f2019-11-06 22:55:45 +01002309 * - EBUSY: Owner is exiting and we just wait for the
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00002310 * exit to complete.
Thomas Gleixnerac31c7f2019-11-06 22:55:45 +01002311 * - EAGAIN: The user space value changed.
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00002312 */
Darren Hart52400ba2009-04-03 13:40:49 -07002313 double_unlock_hb(hb1, hb2);
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07002314 hb_waiters_dec(hb2);
Thomas Gleixner3ef240e2019-11-06 22:55:46 +01002315 /*
2316 * Handle the case where the owner is in the middle of
2317 * exiting. Wait for the exit to complete otherwise
2318 * this task might loop forever, aka. live lock.
2319 */
2320 wait_for_owner_exiting(ret, exiting);
Darren Hart52400ba2009-04-03 13:40:49 -07002321 cond_resched();
2322 goto retry;
2323 default:
2324 goto out_unlock;
2325 }
2326 }
2327
Jason Low0d00c7b2014-01-12 15:31:22 -08002328 plist_for_each_entry_safe(this, next, &hb1->chain, list) {
Darren Hart52400ba2009-04-03 13:40:49 -07002329 if (task_count - nr_wake >= nr_requeue)
2330 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Darren Hart52400ba2009-04-03 13:40:49 -07002332 if (!match_futex(&this->key, &key1))
2333 continue;
2334
Darren Hart392741e2009-08-07 15:20:48 -07002335 /*
Ingo Molnar93d09552021-05-12 20:04:28 +02002336 * FUTEX_WAIT_REQUEUE_PI and FUTEX_CMP_REQUEUE_PI should always
Darren Hart392741e2009-08-07 15:20:48 -07002337 * be paired with each other and no other futex ops.
Darren Hartaa109902012-11-26 16:29:56 -08002338 *
2339 * We should never be requeueing a futex_q with a pi_state,
2340 * which is awaiting a futex_unlock_pi().
Darren Hart392741e2009-08-07 15:20:48 -07002341 */
2342 if ((requeue_pi && !this->rt_waiter) ||
Darren Hartaa109902012-11-26 16:29:56 -08002343 (!requeue_pi && this->rt_waiter) ||
2344 this->pi_state) {
Darren Hart392741e2009-08-07 15:20:48 -07002345 ret = -EINVAL;
2346 break;
2347 }
Darren Hart52400ba2009-04-03 13:40:49 -07002348
Thomas Gleixner64b7b712021-08-15 23:29:12 +02002349 /* Plain futexes just wake or requeue and are done */
2350 if (!requeue_pi) {
2351 if (++task_count <= nr_wake)
2352 mark_wake_futex(&wake_q, this);
2353 else
2354 requeue_futex(this, hb1, hb2, &key2);
Darren Hart52400ba2009-04-03 13:40:49 -07002355 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 }
Darren Hart52400ba2009-04-03 13:40:49 -07002357
Darren Hart84bc4af2009-08-13 17:36:53 -07002358 /* Ensure we requeue to the expected futex for requeue_pi. */
Thomas Gleixner64b7b712021-08-15 23:29:12 +02002359 if (!match_futex(this->requeue_pi_key, &key2)) {
Darren Hart84bc4af2009-08-13 17:36:53 -07002360 ret = -EINVAL;
2361 break;
2362 }
2363
Darren Hart52400ba2009-04-03 13:40:49 -07002364 /*
2365 * Requeue nr_requeue waiters and possibly one more in the case
2366 * of requeue_pi if we couldn't acquire the lock atomically.
Thomas Gleixner64b7b712021-08-15 23:29:12 +02002367 *
2368 * Prepare the waiter to take the rt_mutex. Take a refcount
2369 * on the pi_state and store the pointer in the futex_q
2370 * object of the waiter.
Darren Hart52400ba2009-04-03 13:40:49 -07002371 */
Thomas Gleixner64b7b712021-08-15 23:29:12 +02002372 get_pi_state(pi_state);
Thomas Gleixner07d91ef52021-08-15 23:29:18 +02002373
2374 /* Don't requeue when the waiter is already on the way out. */
2375 if (!futex_requeue_pi_prepare(this, pi_state)) {
2376 /*
2377 * Early woken waiter signaled that it is on the
2378 * way out. Drop the pi_state reference and try the
2379 * next waiter. @this->pi_state is still NULL.
2380 */
2381 put_pi_state(pi_state);
2382 continue;
2383 }
2384
Thomas Gleixner64b7b712021-08-15 23:29:12 +02002385 ret = rt_mutex_start_proxy_lock(&pi_state->pi_mutex,
Thomas Gleixner07d91ef52021-08-15 23:29:18 +02002386 this->rt_waiter,
2387 this->task);
2388
Thomas Gleixner64b7b712021-08-15 23:29:12 +02002389 if (ret == 1) {
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00002390 /*
Thomas Gleixner64b7b712021-08-15 23:29:12 +02002391 * We got the lock. We do neither drop the refcount
2392 * on pi_state nor clear this->pi_state because the
2393 * waiter needs the pi_state for cleaning up the
2394 * user space value. It will drop the refcount
Thomas Gleixner07d91ef52021-08-15 23:29:18 +02002395 * after doing so. this::requeue_state is updated
2396 * in the wakeup as well.
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00002397 */
Thomas Gleixner64b7b712021-08-15 23:29:12 +02002398 requeue_pi_wake_futex(this, &key2, hb2);
2399 task_count++;
Thomas Gleixner07d91ef52021-08-15 23:29:18 +02002400 } else if (!ret) {
2401 /* Waiter is queued, move it to hb2 */
2402 requeue_futex(this, hb1, hb2, &key2);
2403 futex_requeue_pi_complete(this, 0);
2404 task_count++;
2405 } else {
Thomas Gleixner64b7b712021-08-15 23:29:12 +02002406 /*
2407 * rt_mutex_start_proxy_lock() detected a potential
2408 * deadlock when we tried to queue that waiter.
2409 * Drop the pi_state reference which we took above
2410 * and remove the pointer to the state from the
2411 * waiters futex_q object.
2412 */
2413 this->pi_state = NULL;
2414 put_pi_state(pi_state);
Thomas Gleixner07d91ef52021-08-15 23:29:18 +02002415 futex_requeue_pi_complete(this, ret);
Thomas Gleixner64b7b712021-08-15 23:29:12 +02002416 /*
2417 * We stop queueing more waiters and let user space
2418 * deal with the mess.
2419 */
2420 break;
Darren Hart52400ba2009-04-03 13:40:49 -07002421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 }
2423
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00002424 /*
Thomas Gleixner4f07ec02021-09-02 11:48:48 +02002425 * We took an extra initial reference to the pi_state in
2426 * futex_proxy_trylock_atomic(). We need to drop it here again.
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00002427 */
Thomas Gleixner29e9ee52015-12-19 20:07:39 +00002428 put_pi_state(pi_state);
Thomas Gleixner885c2cb2015-12-19 20:07:41 +00002429
2430out_unlock:
Darren Hart5eb3dc62009-03-12 00:55:52 -07002431 double_unlock_hb(hb1, hb2);
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07002432 wake_up_q(&wake_q);
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07002433 hb_waiters_dec(hb2);
Darren Hart52400ba2009-04-03 13:40:49 -07002434 return ret ? ret : task_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435}
2436
2437/* The key must be already stored in q->key. */
Eric Sesterhenn82af7ac2008-01-25 10:40:46 +01002438static inline struct futex_hash_bucket *queue_lock(struct futex_q *q)
Namhyung Kim15e408c2010-09-14 21:43:48 +09002439 __acquires(&hb->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440{
Ingo Molnare2970f22006-06-27 02:54:47 -07002441 struct futex_hash_bucket *hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Ingo Molnare2970f22006-06-27 02:54:47 -07002443 hb = hash_futex(&q->key);
Linus Torvalds11d46162014-03-20 22:11:17 -07002444
2445 /*
2446 * Increment the counter before taking the lock so that
2447 * a potential waker won't miss a to-be-slept task that is
2448 * waiting for the spinlock. This is safe as all queue_lock()
2449 * users end up calling queue_me(). Similarly, for housekeeping,
2450 * decrement the counter at queue_unlock() when some error has
2451 * occurred and we don't end up adding the task to the list.
2452 */
Davidlohr Bueso6f568eb2019-02-06 10:56:02 -08002453 hb_waiters_inc(hb); /* implies smp_mb(); (A) */
Linus Torvalds11d46162014-03-20 22:11:17 -07002454
Ingo Molnare2970f22006-06-27 02:54:47 -07002455 q->lock_ptr = &hb->lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Davidlohr Bueso6f568eb2019-02-06 10:56:02 -08002457 spin_lock(&hb->lock);
Ingo Molnare2970f22006-06-27 02:54:47 -07002458 return hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459}
2460
Darren Hartd40d65c2009-09-21 22:30:15 -07002461static inline void
Jason Low0d00c7b2014-01-12 15:31:22 -08002462queue_unlock(struct futex_hash_bucket *hb)
Namhyung Kim15e408c2010-09-14 21:43:48 +09002463 __releases(&hb->lock)
Darren Hartd40d65c2009-09-21 22:30:15 -07002464{
2465 spin_unlock(&hb->lock);
Linus Torvalds11d46162014-03-20 22:11:17 -07002466 hb_waiters_dec(hb);
Darren Hartd40d65c2009-09-21 22:30:15 -07002467}
2468
Peter Zijlstracfafcd12017-03-22 11:35:58 +01002469static inline void __queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
Pierre Peifferec92d082007-05-09 02:35:00 -07002471 int prio;
2472
2473 /*
2474 * The priority used to register this element is
2475 * - either the real thread-priority for the real-time threads
2476 * (i.e. threads with a priority lower than MAX_RT_PRIO)
2477 * - or MAX_RT_PRIO for non-RT threads.
2478 * Thus, all RT-threads are woken first in priority order, and
2479 * the others are woken last, in FIFO order.
2480 */
2481 prio = min(current->normal_prio, MAX_RT_PRIO);
2482
2483 plist_node_init(&q->list, prio);
Pierre Peifferec92d082007-05-09 02:35:00 -07002484 plist_add(&q->list, &hb->chain);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002485 q->task = current;
Peter Zijlstracfafcd12017-03-22 11:35:58 +01002486}
2487
2488/**
2489 * queue_me() - Enqueue the futex_q on the futex_hash_bucket
2490 * @q: The futex_q to enqueue
2491 * @hb: The destination hash bucket
2492 *
2493 * The hb->lock must be held by the caller, and is released here. A call to
2494 * queue_me() is typically paired with exactly one call to unqueue_me(). The
2495 * exceptions involve the PI related operations, which may use unqueue_me_pi()
2496 * or nothing if the unqueue is done as part of the wake process and the unqueue
2497 * state is implicit in the state of woken task (see futex_wait_requeue_pi() for
2498 * an example).
2499 */
2500static inline void queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
2501 __releases(&hb->lock)
2502{
2503 __queue_me(q, hb);
Ingo Molnare2970f22006-06-27 02:54:47 -07002504 spin_unlock(&hb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505}
2506
Darren Hartd40d65c2009-09-21 22:30:15 -07002507/**
2508 * unqueue_me() - Remove the futex_q from its futex_hash_bucket
2509 * @q: The futex_q to unqueue
2510 *
2511 * The q->lock_ptr must not be held by the caller. A call to unqueue_me() must
2512 * be paired with exactly one earlier call to queue_me().
2513 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002514 * Return:
Mauro Carvalho Chehab7b4ff1a2017-05-11 10:17:45 -03002515 * - 1 - if the futex_q was still queued (and we removed unqueued it);
2516 * - 0 - if the futex_q was already removed by the waking thread
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518static int unqueue_me(struct futex_q *q)
2519{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 spinlock_t *lock_ptr;
Ingo Molnare2970f22006-06-27 02:54:47 -07002521 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
2523 /* In the common case we don't take the spinlock, which is nice. */
Darren Hart42d35d42008-12-29 15:49:53 -08002524retry:
Jianyu Zhan29b75eb2016-03-07 09:32:24 +08002525 /*
2526 * q->lock_ptr can change between this read and the following spin_lock.
2527 * Use READ_ONCE to forbid the compiler from reloading q->lock_ptr and
2528 * optimizing lock_ptr out of the logic below.
2529 */
2530 lock_ptr = READ_ONCE(q->lock_ptr);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07002531 if (lock_ptr != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 spin_lock(lock_ptr);
2533 /*
2534 * q->lock_ptr can change between reading it and
2535 * spin_lock(), causing us to take the wrong lock. This
2536 * corrects the race condition.
2537 *
2538 * Reasoning goes like this: if we have the wrong lock,
2539 * q->lock_ptr must have changed (maybe several times)
2540 * between reading it and the spin_lock(). It can
2541 * change again after the spin_lock() but only if it was
2542 * already changed before the spin_lock(). It cannot,
2543 * however, change back to the original value. Therefore
2544 * we can detect whether we acquired the correct lock.
2545 */
2546 if (unlikely(lock_ptr != q->lock_ptr)) {
2547 spin_unlock(lock_ptr);
2548 goto retry;
2549 }
Lai Jiangshan2e129782010-12-22 14:18:50 +08002550 __unqueue_futex(q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002551
2552 BUG_ON(q->pi_state);
2553
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 spin_unlock(lock_ptr);
2555 ret = 1;
2556 }
2557
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 return ret;
2559}
2560
Ingo Molnarc87e2832006-06-27 02:54:58 -07002561/*
Ingo Molnar93d09552021-05-12 20:04:28 +02002562 * PI futexes can not be requeued and must remove themselves from the
Davidlohr Buesoa3f24282021-02-26 09:50:28 -08002563 * hash bucket. The hash bucket lock (i.e. lock_ptr) is held.
Ingo Molnarc87e2832006-06-27 02:54:58 -07002564 */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002565static void unqueue_me_pi(struct futex_q *q)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002566{
Lai Jiangshan2e129782010-12-22 14:18:50 +08002567 __unqueue_futex(q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002568
2569 BUG_ON(!q->pi_state);
Thomas Gleixner29e9ee52015-12-19 20:07:39 +00002570 put_pi_state(q->pi_state);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002571 q->pi_state = NULL;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002572}
2573
Thomas Gleixnerf2dac392021-01-19 16:26:38 +01002574static int __fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
2575 struct task_struct *argowner)
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002576{
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002577 struct futex_pi_state *pi_state = q->pi_state;
Peter Zijlstrac1e2f0e2017-12-08 13:49:39 +01002578 struct task_struct *oldowner, *newowner;
Thomas Gleixnerf2dac392021-01-19 16:26:38 +01002579 u32 uval, curval, newval, newtid;
2580 int err = 0;
Peter Zijlstra734009e2017-03-22 11:35:52 +01002581
2582 oldowner = pi_state->owner;
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002583
2584 /*
Peter Zijlstrac1e2f0e2017-12-08 13:49:39 +01002585 * We are here because either:
Peter Zijlstra16ffa122017-03-22 11:35:55 +01002586 *
Peter Zijlstrac1e2f0e2017-12-08 13:49:39 +01002587 * - we stole the lock and pi_state->owner needs updating to reflect
2588 * that (@argowner == current),
2589 *
2590 * or:
2591 *
2592 * - someone stole our lock and we need to fix things to point to the
2593 * new owner (@argowner == NULL).
2594 *
2595 * Either way, we have to replace the TID in the user space variable.
Lai Jiangshan81612392011-01-14 17:09:41 +08002596 * This must be atomic as we have to preserve the owner died bit here.
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002597 *
Darren Hartb2d09942009-03-12 00:55:37 -07002598 * Note: We write the user space value _before_ changing the pi_state
2599 * because we can fault here. Imagine swapped out pages or a fork
2600 * that marked all the anonymous memory readonly for cow.
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002601 *
Peter Zijlstra734009e2017-03-22 11:35:52 +01002602 * Modifying pi_state _before_ the user space value would leave the
2603 * pi_state in an inconsistent state when we fault here, because we
2604 * need to drop the locks to handle the fault. This might be observed
Thomas Gleixnerf6f4ec02021-08-15 23:29:07 +02002605 * in the PID checks when attaching to PI state .
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002606 */
2607retry:
Peter Zijlstrac1e2f0e2017-12-08 13:49:39 +01002608 if (!argowner) {
2609 if (oldowner != current) {
2610 /*
2611 * We raced against a concurrent self; things are
2612 * already fixed up. Nothing to do.
2613 */
Thomas Gleixnerf2dac392021-01-19 16:26:38 +01002614 return 0;
Peter Zijlstrac1e2f0e2017-12-08 13:49:39 +01002615 }
2616
2617 if (__rt_mutex_futex_trylock(&pi_state->pi_mutex)) {
Thomas Gleixner12bb3f72021-01-20 16:00:24 +01002618 /* We got the lock. pi_state is correct. Tell caller. */
Thomas Gleixnerf2dac392021-01-19 16:26:38 +01002619 return 1;
Peter Zijlstrac1e2f0e2017-12-08 13:49:39 +01002620 }
2621
2622 /*
Mike Galbraith9f5d1c32020-11-04 16:12:44 +01002623 * The trylock just failed, so either there is an owner or
2624 * there is a higher priority waiter than this one.
Peter Zijlstrac1e2f0e2017-12-08 13:49:39 +01002625 */
2626 newowner = rt_mutex_owner(&pi_state->pi_mutex);
Mike Galbraith9f5d1c32020-11-04 16:12:44 +01002627 /*
2628 * If the higher priority waiter has not yet taken over the
2629 * rtmutex then newowner is NULL. We can't return here with
2630 * that state because it's inconsistent vs. the user space
2631 * state. So drop the locks and try again. It's a valid
2632 * situation and not any different from the other retry
2633 * conditions.
2634 */
2635 if (unlikely(!newowner)) {
2636 err = -EAGAIN;
2637 goto handle_err;
2638 }
Peter Zijlstrac1e2f0e2017-12-08 13:49:39 +01002639 } else {
2640 WARN_ON_ONCE(argowner != current);
2641 if (oldowner == current) {
2642 /*
2643 * We raced against a concurrent self; things are
2644 * already fixed up. Nothing to do.
2645 */
Thomas Gleixnerf2dac392021-01-19 16:26:38 +01002646 return 1;
Peter Zijlstrac1e2f0e2017-12-08 13:49:39 +01002647 }
2648 newowner = argowner;
2649 }
2650
2651 newtid = task_pid_vnr(newowner) | FUTEX_WAITERS;
Peter Zijlstraa97cb0e2018-01-22 11:39:47 +01002652 /* Owner died? */
2653 if (!pi_state->owner)
2654 newtid |= FUTEX_OWNER_DIED;
Peter Zijlstrac1e2f0e2017-12-08 13:49:39 +01002655
Will Deacon6b4f4bc2019-02-28 11:58:08 +00002656 err = get_futex_value_locked(&uval, uaddr);
2657 if (err)
2658 goto handle_err;
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002659
Peter Zijlstra16ffa122017-03-22 11:35:55 +01002660 for (;;) {
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002661 newval = (uval & FUTEX_OWNER_DIED) | newtid;
2662
Will Deacon6b4f4bc2019-02-28 11:58:08 +00002663 err = cmpxchg_futex_value_locked(&curval, uaddr, uval, newval);
2664 if (err)
2665 goto handle_err;
2666
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002667 if (curval == uval)
2668 break;
2669 uval = curval;
2670 }
2671
2672 /*
2673 * We fixed up user space. Now we need to fix the pi_state
2674 * itself.
2675 */
Thomas Gleixnerc5cade22021-01-19 15:21:35 +01002676 pi_state_update_owner(pi_state, newowner);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002677
Thomas Gleixner12bb3f72021-01-20 16:00:24 +01002678 return argowner == current;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002679
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002680 /*
Will Deacon6b4f4bc2019-02-28 11:58:08 +00002681 * In order to reschedule or handle a page fault, we need to drop the
2682 * locks here. In the case of a fault, this gives the other task
2683 * (either the highest priority waiter itself or the task which stole
2684 * the rtmutex) the chance to try the fixup of the pi_state. So once we
2685 * are back from handling the fault we need to check the pi_state after
2686 * reacquiring the locks and before trying to do another fixup. When
2687 * the fixup has been done already we simply return.
Peter Zijlstra734009e2017-03-22 11:35:52 +01002688 *
2689 * Note: we hold both hb->lock and pi_mutex->wait_lock. We can safely
2690 * drop hb->lock since the caller owns the hb -> futex_q relation.
2691 * Dropping the pi_mutex->wait_lock requires the state revalidate.
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002692 */
Will Deacon6b4f4bc2019-02-28 11:58:08 +00002693handle_err:
Peter Zijlstra734009e2017-03-22 11:35:52 +01002694 raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002695 spin_unlock(q->lock_ptr);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002696
Will Deacon6b4f4bc2019-02-28 11:58:08 +00002697 switch (err) {
2698 case -EFAULT:
Thomas Gleixnerf2dac392021-01-19 16:26:38 +01002699 err = fault_in_user_writeable(uaddr);
Will Deacon6b4f4bc2019-02-28 11:58:08 +00002700 break;
2701
2702 case -EAGAIN:
2703 cond_resched();
Thomas Gleixnerf2dac392021-01-19 16:26:38 +01002704 err = 0;
Will Deacon6b4f4bc2019-02-28 11:58:08 +00002705 break;
2706
2707 default:
2708 WARN_ON_ONCE(1);
Will Deacon6b4f4bc2019-02-28 11:58:08 +00002709 break;
2710 }
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002711
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002712 spin_lock(q->lock_ptr);
Peter Zijlstra734009e2017-03-22 11:35:52 +01002713 raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002714
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002715 /*
2716 * Check if someone else fixed it for us:
2717 */
Thomas Gleixnerf2dac392021-01-19 16:26:38 +01002718 if (pi_state->owner != oldowner)
2719 return argowner == current;
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002720
Thomas Gleixnerf2dac392021-01-19 16:26:38 +01002721 /* Retry if err was -EAGAIN or the fault in succeeded */
2722 if (!err)
2723 goto retry;
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002724
Thomas Gleixner34b1a1c2021-01-18 19:01:21 +01002725 /*
2726 * fault_in_user_writeable() failed so user state is immutable. At
2727 * best we can make the kernel state consistent but user state will
2728 * be most likely hosed and any subsequent unlock operation will be
2729 * rejected due to PI futex rule [10].
2730 *
2731 * Ensure that the rtmutex owner is also the pi_state owner despite
2732 * the user space value claiming something different. There is no
2733 * point in unlocking the rtmutex if current is the owner as it
2734 * would need to wait until the next waiter has taken the rtmutex
2735 * to guarantee consistent state. Keep it simple. Userspace asked
2736 * for this wreckaged state.
2737 *
2738 * The rtmutex has an owner - either current or some other
2739 * task. See the EAGAIN loop above.
2740 */
2741 pi_state_update_owner(pi_state, rt_mutex_owner(&pi_state->pi_mutex));
Peter Zijlstra734009e2017-03-22 11:35:52 +01002742
Thomas Gleixnerf2dac392021-01-19 16:26:38 +01002743 return err;
2744}
Peter Zijlstra734009e2017-03-22 11:35:52 +01002745
Thomas Gleixnerf2dac392021-01-19 16:26:38 +01002746static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
2747 struct task_struct *argowner)
2748{
2749 struct futex_pi_state *pi_state = q->pi_state;
2750 int ret;
2751
2752 lockdep_assert_held(q->lock_ptr);
2753
2754 raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
2755 ret = __fixup_pi_state_owner(uaddr, q, argowner);
Peter Zijlstra734009e2017-03-22 11:35:52 +01002756 raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
2757 return ret;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002758}
2759
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002760static long futex_wait_restart(struct restart_block *restart);
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07002761
Darren Hartca5f9522009-04-03 13:39:33 -07002762/**
Darren Hartdd973992009-04-03 13:40:02 -07002763 * fixup_owner() - Post lock pi_state and corner case management
2764 * @uaddr: user address of the futex
Darren Hartdd973992009-04-03 13:40:02 -07002765 * @q: futex_q (contains pi_state and access to the rt_mutex)
2766 * @locked: if the attempt to take the rt_mutex succeeded (1) or not (0)
2767 *
2768 * After attempting to lock an rt_mutex, this function is called to cleanup
2769 * the pi_state owner as well as handle race conditions that may allow us to
2770 * acquire the lock. Must be called with the hb lock held.
2771 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002772 * Return:
Mauro Carvalho Chehab7b4ff1a2017-05-11 10:17:45 -03002773 * - 1 - success, lock taken;
2774 * - 0 - success, lock not taken;
2775 * - <0 - on error (-EFAULT)
Darren Hartdd973992009-04-03 13:40:02 -07002776 */
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002777static int fixup_owner(u32 __user *uaddr, struct futex_q *q, int locked)
Darren Hartdd973992009-04-03 13:40:02 -07002778{
Darren Hartdd973992009-04-03 13:40:02 -07002779 if (locked) {
2780 /*
2781 * Got the lock. We might not be the anticipated owner if we
2782 * did a lock-steal - fix up the PI-state in that case:
Peter Zijlstra16ffa122017-03-22 11:35:55 +01002783 *
Peter Zijlstrac1e2f0e2017-12-08 13:49:39 +01002784 * Speculative pi_state->owner read (we don't hold wait_lock);
2785 * since we own the lock pi_state->owner == current is the
2786 * stable state, anything else needs more attention.
Darren Hartdd973992009-04-03 13:40:02 -07002787 */
2788 if (q->pi_state->owner != current)
Thomas Gleixner12bb3f72021-01-20 16:00:24 +01002789 return fixup_pi_state_owner(uaddr, q, current);
2790 return 1;
Darren Hartdd973992009-04-03 13:40:02 -07002791 }
2792
2793 /*
Peter Zijlstrac1e2f0e2017-12-08 13:49:39 +01002794 * If we didn't get the lock; check if anybody stole it from us. In
2795 * that case, we need to fix up the uval to point to them instead of
2796 * us, otherwise bad things happen. [10]
2797 *
2798 * Another speculative read; pi_state->owner == current is unstable
2799 * but needs our attention.
2800 */
Thomas Gleixner12bb3f72021-01-20 16:00:24 +01002801 if (q->pi_state->owner == current)
2802 return fixup_pi_state_owner(uaddr, q, NULL);
Peter Zijlstrac1e2f0e2017-12-08 13:49:39 +01002803
2804 /*
Darren Hartdd973992009-04-03 13:40:02 -07002805 * Paranoia check. If we did not take the lock, then we should not be
Thomas Gleixner04b79c52021-01-19 16:06:10 +01002806 * the owner of the rt_mutex. Warn and establish consistent state.
Darren Hartdd973992009-04-03 13:40:02 -07002807 */
Thomas Gleixner04b79c52021-01-19 16:06:10 +01002808 if (WARN_ON_ONCE(rt_mutex_owner(&q->pi_state->pi_mutex) == current))
2809 return fixup_pi_state_owner(uaddr, q, current);
Darren Hartdd973992009-04-03 13:40:02 -07002810
Thomas Gleixner12bb3f72021-01-20 16:00:24 +01002811 return 0;
Darren Hartdd973992009-04-03 13:40:02 -07002812}
2813
2814/**
Darren Hartca5f9522009-04-03 13:39:33 -07002815 * futex_wait_queue_me() - queue_me() and wait for wakeup, timeout, or signal
2816 * @hb: the futex hash bucket, must be locked by the caller
2817 * @q: the futex_q to queue up on
2818 * @timeout: the prepared hrtimer_sleeper, or null for no timeout
Darren Hartca5f9522009-04-03 13:39:33 -07002819 */
2820static void futex_wait_queue_me(struct futex_hash_bucket *hb, struct futex_q *q,
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02002821 struct hrtimer_sleeper *timeout)
Darren Hartca5f9522009-04-03 13:39:33 -07002822{
Darren Hart9beba3c2009-09-24 11:54:47 -07002823 /*
2824 * The task state is guaranteed to be set before another task can
Peter Zijlstrab92b8b32015-05-12 10:51:55 +02002825 * wake it. set_current_state() is implemented using smp_store_mb() and
Darren Hart9beba3c2009-09-24 11:54:47 -07002826 * queue_me() calls spin_unlock() upon completion, both serializing
2827 * access to the hash list and forcing another memory barrier.
2828 */
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02002829 set_current_state(TASK_INTERRUPTIBLE);
Darren Hart0729e192009-09-21 22:30:38 -07002830 queue_me(q, hb);
Darren Hartca5f9522009-04-03 13:39:33 -07002831
2832 /* Arm the timer */
Thomas Gleixner2e4b0d32015-04-14 21:09:13 +00002833 if (timeout)
Thomas Gleixner9dd88132019-07-30 21:16:55 +02002834 hrtimer_sleeper_start_expires(timeout, HRTIMER_MODE_ABS);
Darren Hartca5f9522009-04-03 13:39:33 -07002835
2836 /*
Darren Hart0729e192009-09-21 22:30:38 -07002837 * If we have been removed from the hash list, then another task
2838 * has tried to wake us, and we can skip the call to schedule().
Darren Hartca5f9522009-04-03 13:39:33 -07002839 */
2840 if (likely(!plist_node_empty(&q->list))) {
2841 /*
2842 * If the timer has already expired, current will already be
2843 * flagged for rescheduling. Only call schedule if there
2844 * is no timeout, or if it has yet to expire.
2845 */
2846 if (!timeout || timeout->task)
Colin Cross88c80042013-05-01 18:35:05 -07002847 freezable_schedule();
Darren Hartca5f9522009-04-03 13:39:33 -07002848 }
2849 __set_current_state(TASK_RUNNING);
2850}
2851
Darren Hartf8010732009-04-03 13:40:40 -07002852/**
2853 * futex_wait_setup() - Prepare to wait on a futex
2854 * @uaddr: the futex userspace address
2855 * @val: the expected value
Darren Hartb41277d2010-11-08 13:10:09 -08002856 * @flags: futex flags (FLAGS_SHARED, etc.)
Darren Hartf8010732009-04-03 13:40:40 -07002857 * @q: the associated futex_q
2858 * @hb: storage for hash_bucket pointer to be returned to caller
2859 *
2860 * Setup the futex_q and locate the hash_bucket. Get the futex value and
2861 * compare it with the expected value. Handle atomic faults internally.
Thomas Gleixnerc363b7e2021-08-15 23:29:06 +02002862 * Return with the hb lock held on success, and unlocked on failure.
Darren Hartf8010732009-04-03 13:40:40 -07002863 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002864 * Return:
Mauro Carvalho Chehab7b4ff1a2017-05-11 10:17:45 -03002865 * - 0 - uaddr contains val and hb has been locked;
2866 * - <1 - -EFAULT or -EWOULDBLOCK (uaddr does not contain val) and hb is unlocked
Darren Hartf8010732009-04-03 13:40:40 -07002867 */
Darren Hartb41277d2010-11-08 13:10:09 -08002868static int futex_wait_setup(u32 __user *uaddr, u32 val, unsigned int flags,
Darren Hartf8010732009-04-03 13:40:40 -07002869 struct futex_q *q, struct futex_hash_bucket **hb)
2870{
2871 u32 uval;
2872 int ret;
2873
2874 /*
2875 * Access the page AFTER the hash-bucket is locked.
2876 * Order is important:
2877 *
2878 * Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val);
2879 * Userspace waker: if (cond(var)) { var = new; futex_wake(&var); }
2880 *
2881 * The basic logical guarantee of a futex is that it blocks ONLY
2882 * if cond(var) is known to be true at the time of blocking, for
Michel Lespinasse8fe8f542011-03-06 18:07:50 -08002883 * any cond. If we locked the hash-bucket after testing *uaddr, that
2884 * would open a race condition where we could block indefinitely with
Darren Hartf8010732009-04-03 13:40:40 -07002885 * cond(var) false, which would violate the guarantee.
2886 *
Michel Lespinasse8fe8f542011-03-06 18:07:50 -08002887 * On the other hand, we insert q and release the hash-bucket only
2888 * after testing *uaddr. This guarantees that futex_wait() will NOT
2889 * absorb a wakeup if *uaddr does not match the desired values
2890 * while the syscall executes.
Darren Hartf8010732009-04-03 13:40:40 -07002891 */
2892retry:
Linus Torvalds96d4f262019-01-03 18:57:57 -08002893 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q->key, FUTEX_READ);
Darren Hartf8010732009-04-03 13:40:40 -07002894 if (unlikely(ret != 0))
Darren Harta5a2a0c2009-04-10 09:50:05 -07002895 return ret;
Darren Hartf8010732009-04-03 13:40:40 -07002896
2897retry_private:
2898 *hb = queue_lock(q);
2899
2900 ret = get_futex_value_locked(&uval, uaddr);
2901
2902 if (ret) {
Jason Low0d00c7b2014-01-12 15:31:22 -08002903 queue_unlock(*hb);
Darren Hartf8010732009-04-03 13:40:40 -07002904
2905 ret = get_user(uval, uaddr);
2906 if (ret)
André Almeidad7c5ed72020-07-02 17:28:41 -03002907 return ret;
Darren Hartf8010732009-04-03 13:40:40 -07002908
Darren Hartb41277d2010-11-08 13:10:09 -08002909 if (!(flags & FLAGS_SHARED))
Darren Hartf8010732009-04-03 13:40:40 -07002910 goto retry_private;
2911
Darren Hartf8010732009-04-03 13:40:40 -07002912 goto retry;
2913 }
2914
2915 if (uval != val) {
Jason Low0d00c7b2014-01-12 15:31:22 -08002916 queue_unlock(*hb);
Darren Hartf8010732009-04-03 13:40:40 -07002917 ret = -EWOULDBLOCK;
2918 }
2919
Darren Hartf8010732009-04-03 13:40:40 -07002920 return ret;
2921}
2922
Darren Hartb41277d2010-11-08 13:10:09 -08002923static int futex_wait(u32 __user *uaddr, unsigned int flags, u32 val,
2924 ktime_t *abs_time, u32 bitset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925{
Waiman Long5ca584d2019-05-28 12:03:45 -04002926 struct hrtimer_sleeper timeout, *to;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002927 struct restart_block *restart;
Ingo Molnare2970f22006-06-27 02:54:47 -07002928 struct futex_hash_bucket *hb;
Darren Hart5bdb05f2010-11-08 13:40:28 -08002929 struct futex_q q = futex_q_init;
Ingo Molnare2970f22006-06-27 02:54:47 -07002930 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931
Thomas Gleixnercd689982008-02-01 17:45:14 +01002932 if (!bitset)
2933 return -EINVAL;
Thomas Gleixnercd689982008-02-01 17:45:14 +01002934 q.bitset = bitset;
Darren Hartca5f9522009-04-03 13:39:33 -07002935
Waiman Long5ca584d2019-05-28 12:03:45 -04002936 to = futex_setup_timer(abs_time, &timeout, flags,
2937 current->timer_slack_ns);
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002938retry:
Darren Hart7ada8762010-10-17 08:35:04 -07002939 /*
Thomas Gleixnerc363b7e2021-08-15 23:29:06 +02002940 * Prepare to wait on uaddr. On success, it holds hb->lock and q
2941 * is initialized.
Darren Hart7ada8762010-10-17 08:35:04 -07002942 */
Darren Hartb41277d2010-11-08 13:10:09 -08002943 ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
Darren Hartf8010732009-04-03 13:40:40 -07002944 if (ret)
Darren Hart42d35d42008-12-29 15:49:53 -08002945 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
Darren Hartca5f9522009-04-03 13:39:33 -07002947 /* queue_me and wait for wakeup, timeout, or a signal. */
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02002948 futex_wait_queue_me(hb, &q, to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949
2950 /* If we were woken (and unqueued), we succeeded, whatever. */
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002951 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 if (!unqueue_me(&q))
Darren Hart7ada8762010-10-17 08:35:04 -07002953 goto out;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002954 ret = -ETIMEDOUT;
Darren Hartca5f9522009-04-03 13:39:33 -07002955 if (to && !to->task)
Darren Hart7ada8762010-10-17 08:35:04 -07002956 goto out;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002957
Ingo Molnare2970f22006-06-27 02:54:47 -07002958 /*
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002959 * We expect signal_pending(current), but we might be the
2960 * victim of a spurious wakeup as well.
Ingo Molnare2970f22006-06-27 02:54:47 -07002961 */
Darren Hart7ada8762010-10-17 08:35:04 -07002962 if (!signal_pending(current))
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002963 goto retry;
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002964
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002965 ret = -ERESTARTSYS;
Pierre Peifferc19384b2007-05-09 02:35:02 -07002966 if (!abs_time)
Darren Hart7ada8762010-10-17 08:35:04 -07002967 goto out;
Steven Rostedtce6bd422007-12-05 15:46:09 +01002968
Andy Lutomirskif56141e2015-02-12 15:01:14 -08002969 restart = &current->restart_block;
Namhyung Kima3c74c52010-09-14 21:43:47 +09002970 restart->futex.uaddr = uaddr;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002971 restart->futex.val = val;
Thomas Gleixner2456e852016-12-25 11:38:40 +01002972 restart->futex.time = *abs_time;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002973 restart->futex.bitset = bitset;
Darren Hart0cd9c642011-04-14 15:41:57 -07002974 restart->futex.flags = flags | FLAGS_HAS_TIMEOUT;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002975
Oleg Nesterov5abbe512021-02-01 18:46:41 +01002976 ret = set_restart_fn(restart, futex_wait_restart);
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002977
Darren Hart42d35d42008-12-29 15:49:53 -08002978out:
Darren Hartca5f9522009-04-03 13:39:33 -07002979 if (to) {
2980 hrtimer_cancel(&to->timer);
2981 destroy_hrtimer_on_stack(&to->timer);
2982 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07002983 return ret;
2984}
2985
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002986
2987static long futex_wait_restart(struct restart_block *restart)
2988{
Namhyung Kima3c74c52010-09-14 21:43:47 +09002989 u32 __user *uaddr = restart->futex.uaddr;
Darren Harta72188d2009-04-03 13:40:22 -07002990 ktime_t t, *tp = NULL;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002991
Darren Harta72188d2009-04-03 13:40:22 -07002992 if (restart->futex.flags & FLAGS_HAS_TIMEOUT) {
Thomas Gleixner2456e852016-12-25 11:38:40 +01002993 t = restart->futex.time;
Darren Harta72188d2009-04-03 13:40:22 -07002994 tp = &t;
2995 }
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002996 restart->fn = do_no_restart_syscall;
Darren Hartb41277d2010-11-08 13:10:09 -08002997
2998 return (long)futex_wait(uaddr, restart->futex.flags,
2999 restart->futex.val, tp, restart->futex.bitset);
Nick Piggin72c1bbf2007-05-08 00:26:43 -07003000}
3001
3002
Ingo Molnarc87e2832006-06-27 02:54:58 -07003003/*
3004 * Userspace tried a 0 -> TID atomic transition of the futex value
3005 * and failed. The kernel side here does the whole locking operation:
Davidlohr Bueso767f5092015-06-29 23:26:01 -07003006 * if there are waiters then it will block as a consequence of relying
3007 * on rt-mutexes, it does PI, etc. (Due to races the kernel might see
3008 * a 0 value of the futex too.).
3009 *
3010 * Also serves as futex trylock_pi()'ing, and due semantics.
Ingo Molnarc87e2832006-06-27 02:54:58 -07003011 */
Michael Kerrisk996636d2015-01-16 20:28:06 +01003012static int futex_lock_pi(u32 __user *uaddr, unsigned int flags,
Darren Hartb41277d2010-11-08 13:10:09 -08003013 ktime_t *time, int trylock)
Ingo Molnarc87e2832006-06-27 02:54:58 -07003014{
Waiman Long5ca584d2019-05-28 12:03:45 -04003015 struct hrtimer_sleeper timeout, *to;
Thomas Gleixner3ef240e2019-11-06 22:55:46 +01003016 struct task_struct *exiting = NULL;
Peter Zijlstracfafcd12017-03-22 11:35:58 +01003017 struct rt_mutex_waiter rt_waiter;
Ingo Molnarc87e2832006-06-27 02:54:58 -07003018 struct futex_hash_bucket *hb;
Darren Hart5bdb05f2010-11-08 13:40:28 -08003019 struct futex_q q = futex_q_init;
Darren Hartdd973992009-04-03 13:40:02 -07003020 int res, ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -07003021
Nicolas Pitrebc2eecd2017-08-01 00:31:32 -04003022 if (!IS_ENABLED(CONFIG_FUTEX_PI))
3023 return -ENOSYS;
3024
Ingo Molnarc87e2832006-06-27 02:54:58 -07003025 if (refill_pi_state_cache())
3026 return -ENOMEM;
3027
Thomas Gleixnere112c412021-04-22 21:44:22 +02003028 to = futex_setup_timer(time, &timeout, flags, 0);
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07003029
Darren Hart42d35d42008-12-29 15:49:53 -08003030retry:
Linus Torvalds96d4f262019-01-03 18:57:57 -08003031 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q.key, FUTEX_WRITE);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003032 if (unlikely(ret != 0))
Darren Hart42d35d42008-12-29 15:49:53 -08003033 goto out;
Ingo Molnarc87e2832006-06-27 02:54:58 -07003034
Darren Harte4dc5b72009-03-12 00:56:13 -07003035retry_private:
Eric Sesterhenn82af7ac2008-01-25 10:40:46 +01003036 hb = queue_lock(&q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003037
Thomas Gleixner3ef240e2019-11-06 22:55:46 +01003038 ret = futex_lock_pi_atomic(uaddr, hb, &q.key, &q.pi_state, current,
3039 &exiting, 0);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003040 if (unlikely(ret)) {
Davidlohr Bueso767f5092015-06-29 23:26:01 -07003041 /*
3042 * Atomic work succeeded and we got the lock,
3043 * or failed. Either way, we do _not_ block.
3044 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07003045 switch (ret) {
Darren Hart1a520842009-04-03 13:39:52 -07003046 case 1:
3047 /* We got the lock. */
3048 ret = 0;
3049 goto out_unlock_put_key;
3050 case -EFAULT:
3051 goto uaddr_faulted;
Thomas Gleixnerac31c7f2019-11-06 22:55:45 +01003052 case -EBUSY:
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07003053 case -EAGAIN:
3054 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00003055 * Two reasons for this:
Thomas Gleixnerac31c7f2019-11-06 22:55:45 +01003056 * - EBUSY: Task is exiting and we just wait for the
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00003057 * exit to complete.
Thomas Gleixnerac31c7f2019-11-06 22:55:45 +01003058 * - EAGAIN: The user space value changed.
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07003059 */
Jason Low0d00c7b2014-01-12 15:31:22 -08003060 queue_unlock(hb);
Thomas Gleixner3ef240e2019-11-06 22:55:46 +01003061 /*
3062 * Handle the case where the owner is in the middle of
3063 * exiting. Wait for the exit to complete otherwise
3064 * this task might loop forever, aka. live lock.
3065 */
3066 wait_for_owner_exiting(ret, exiting);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07003067 cond_resched();
3068 goto retry;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07003069 default:
Darren Hart42d35d42008-12-29 15:49:53 -08003070 goto out_unlock_put_key;
Ingo Molnarc87e2832006-06-27 02:54:58 -07003071 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07003072 }
3073
Peter Zijlstracfafcd12017-03-22 11:35:58 +01003074 WARN_ON(!q.pi_state);
3075
Ingo Molnarc87e2832006-06-27 02:54:58 -07003076 /*
3077 * Only actually queue now that the atomic ops are done:
3078 */
Peter Zijlstracfafcd12017-03-22 11:35:58 +01003079 __queue_me(&q, hb);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003080
Peter Zijlstracfafcd12017-03-22 11:35:58 +01003081 if (trylock) {
Peter Zijlstra5293c2e2017-03-22 11:35:51 +01003082 ret = rt_mutex_futex_trylock(&q.pi_state->pi_mutex);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003083 /* Fixup the trylock return value: */
3084 ret = ret ? 0 : -EWOULDBLOCK;
Peter Zijlstracfafcd12017-03-22 11:35:58 +01003085 goto no_block;
Ingo Molnarc87e2832006-06-27 02:54:58 -07003086 }
3087
Peter Zijlstracfafcd12017-03-22 11:35:58 +01003088 rt_mutex_init_waiter(&rt_waiter);
Peter Zijlstra56222b22017-03-22 11:36:00 +01003089
3090 /*
3091 * On PREEMPT_RT_FULL, when hb->lock becomes an rt_mutex, we must not
3092 * hold it while doing rt_mutex_start_proxy(), because then it will
3093 * include hb->lock in the blocking chain, even through we'll not in
3094 * fact hold it while blocking. This will lead it to report -EDEADLK
3095 * and BUG when futex_unlock_pi() interleaves with this.
3096 *
3097 * Therefore acquire wait_lock while holding hb->lock, but drop the
Thomas Gleixner1a1fb982019-01-29 23:15:12 +01003098 * latter before calling __rt_mutex_start_proxy_lock(). This
3099 * interleaves with futex_unlock_pi() -- which does a similar lock
3100 * handoff -- such that the latter can observe the futex_q::pi_state
3101 * before __rt_mutex_start_proxy_lock() is done.
Peter Zijlstra56222b22017-03-22 11:36:00 +01003102 */
3103 raw_spin_lock_irq(&q.pi_state->pi_mutex.wait_lock);
3104 spin_unlock(q.lock_ptr);
Thomas Gleixner1a1fb982019-01-29 23:15:12 +01003105 /*
3106 * __rt_mutex_start_proxy_lock() unconditionally enqueues the @rt_waiter
3107 * such that futex_unlock_pi() is guaranteed to observe the waiter when
3108 * it sees the futex_q::pi_state.
3109 */
Peter Zijlstra56222b22017-03-22 11:36:00 +01003110 ret = __rt_mutex_start_proxy_lock(&q.pi_state->pi_mutex, &rt_waiter, current);
3111 raw_spin_unlock_irq(&q.pi_state->pi_mutex.wait_lock);
3112
Peter Zijlstracfafcd12017-03-22 11:35:58 +01003113 if (ret) {
3114 if (ret == 1)
3115 ret = 0;
Thomas Gleixner1a1fb982019-01-29 23:15:12 +01003116 goto cleanup;
Peter Zijlstracfafcd12017-03-22 11:35:58 +01003117 }
3118
Peter Zijlstracfafcd12017-03-22 11:35:58 +01003119 if (unlikely(to))
Thomas Gleixner9dd88132019-07-30 21:16:55 +02003120 hrtimer_sleeper_start_expires(to, HRTIMER_MODE_ABS);
Peter Zijlstracfafcd12017-03-22 11:35:58 +01003121
3122 ret = rt_mutex_wait_proxy_lock(&q.pi_state->pi_mutex, to, &rt_waiter);
3123
Thomas Gleixner1a1fb982019-01-29 23:15:12 +01003124cleanup:
Vernon Mauerya99e4e42006-07-01 04:35:42 -07003125 spin_lock(q.lock_ptr);
Darren Hartdd973992009-04-03 13:40:02 -07003126 /*
Thomas Gleixner1a1fb982019-01-29 23:15:12 +01003127 * If we failed to acquire the lock (deadlock/signal/timeout), we must
Peter Zijlstracfafcd12017-03-22 11:35:58 +01003128 * first acquire the hb->lock before removing the lock from the
Thomas Gleixner1a1fb982019-01-29 23:15:12 +01003129 * rt_mutex waitqueue, such that we can keep the hb and rt_mutex wait
3130 * lists consistent.
Peter Zijlstra56222b22017-03-22 11:36:00 +01003131 *
3132 * In particular; it is important that futex_unlock_pi() can not
3133 * observe this inconsistency.
Peter Zijlstracfafcd12017-03-22 11:35:58 +01003134 */
3135 if (ret && !rt_mutex_cleanup_proxy_lock(&q.pi_state->pi_mutex, &rt_waiter))
3136 ret = 0;
3137
3138no_block:
3139 /*
Darren Hartdd973992009-04-03 13:40:02 -07003140 * Fixup the pi_state owner and possibly acquire the lock if we
3141 * haven't already.
3142 */
Thomas Gleixnerae791a22010-11-10 13:30:36 +01003143 res = fixup_owner(uaddr, &q, !ret);
Darren Hartdd973992009-04-03 13:40:02 -07003144 /*
Ingo Molnar93d09552021-05-12 20:04:28 +02003145 * If fixup_owner() returned an error, propagate that. If it acquired
Darren Hartdd973992009-04-03 13:40:02 -07003146 * the lock, clear our -ETIMEDOUT or -EINTR.
3147 */
3148 if (res)
3149 ret = (res < 0) ? res : 0;
Ingo Molnarc87e2832006-06-27 02:54:58 -07003150
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07003151 unqueue_me_pi(&q);
Davidlohr Buesoa3f24282021-02-26 09:50:28 -08003152 spin_unlock(q.lock_ptr);
André Almeida9180bd42020-07-02 17:28:40 -03003153 goto out;
Ingo Molnarc87e2832006-06-27 02:54:58 -07003154
Darren Hart42d35d42008-12-29 15:49:53 -08003155out_unlock_put_key:
Jason Low0d00c7b2014-01-12 15:31:22 -08003156 queue_unlock(hb);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003157
Darren Hart42d35d42008-12-29 15:49:53 -08003158out:
Thomas Gleixner97181f92017-04-10 18:03:36 +02003159 if (to) {
3160 hrtimer_cancel(&to->timer);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07003161 destroy_hrtimer_on_stack(&to->timer);
Thomas Gleixner97181f92017-04-10 18:03:36 +02003162 }
Darren Hartdd973992009-04-03 13:40:02 -07003163 return ret != -EINTR ? ret : -ERESTARTNOINTR;
Ingo Molnarc87e2832006-06-27 02:54:58 -07003164
Darren Hart42d35d42008-12-29 15:49:53 -08003165uaddr_faulted:
Jason Low0d00c7b2014-01-12 15:31:22 -08003166 queue_unlock(hb);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07003167
Thomas Gleixnerd0725992009-06-11 23:15:43 +02003168 ret = fault_in_user_writeable(uaddr);
Darren Harte4dc5b72009-03-12 00:56:13 -07003169 if (ret)
André Almeida9180bd42020-07-02 17:28:40 -03003170 goto out;
Ingo Molnarc87e2832006-06-27 02:54:58 -07003171
Darren Hartb41277d2010-11-08 13:10:09 -08003172 if (!(flags & FLAGS_SHARED))
Darren Harte4dc5b72009-03-12 00:56:13 -07003173 goto retry_private;
3174
Darren Harte4dc5b72009-03-12 00:56:13 -07003175 goto retry;
Ingo Molnarc87e2832006-06-27 02:54:58 -07003176}
3177
3178/*
Ingo Molnarc87e2832006-06-27 02:54:58 -07003179 * Userspace attempted a TID -> 0 atomic transition, and failed.
3180 * This is the in-kernel slowpath: we look up the PI state (if any),
3181 * and do the rt-mutex unlock.
3182 */
Darren Hartb41277d2010-11-08 13:10:09 -08003183static int futex_unlock_pi(u32 __user *uaddr, unsigned int flags)
Ingo Molnarc87e2832006-06-27 02:54:58 -07003184{
Kees Cook3f649ab2020-06-03 13:09:38 -07003185 u32 curval, uval, vpid = task_pid_vnr(current);
Peter Zijlstra38d47c12008-09-26 19:32:20 +02003186 union futex_key key = FUTEX_KEY_INIT;
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00003187 struct futex_hash_bucket *hb;
Peter Zijlstra499f5ac2017-03-22 11:35:48 +01003188 struct futex_q *top_waiter;
Darren Harte4dc5b72009-03-12 00:56:13 -07003189 int ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -07003190
Nicolas Pitrebc2eecd2017-08-01 00:31:32 -04003191 if (!IS_ENABLED(CONFIG_FUTEX_PI))
3192 return -ENOSYS;
3193
Ingo Molnarc87e2832006-06-27 02:54:58 -07003194retry:
3195 if (get_user(uval, uaddr))
3196 return -EFAULT;
3197 /*
3198 * We release only a lock we actually own:
3199 */
Thomas Gleixnerc0c9ed12011-03-11 11:51:22 +01003200 if ((uval & FUTEX_TID_MASK) != vpid)
Ingo Molnarc87e2832006-06-27 02:54:58 -07003201 return -EPERM;
Ingo Molnarc87e2832006-06-27 02:54:58 -07003202
Linus Torvalds96d4f262019-01-03 18:57:57 -08003203 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, FUTEX_WRITE);
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00003204 if (ret)
3205 return ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -07003206
3207 hb = hash_futex(&key);
3208 spin_lock(&hb->lock);
3209
Ingo Molnarc87e2832006-06-27 02:54:58 -07003210 /*
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00003211 * Check waiters first. We do not trust user space values at
3212 * all and we at least want to know if user space fiddled
3213 * with the futex value instead of blindly unlocking.
Ingo Molnarc87e2832006-06-27 02:54:58 -07003214 */
Peter Zijlstra499f5ac2017-03-22 11:35:48 +01003215 top_waiter = futex_top_waiter(hb, &key);
3216 if (top_waiter) {
Peter Zijlstra16ffa122017-03-22 11:35:55 +01003217 struct futex_pi_state *pi_state = top_waiter->pi_state;
3218
3219 ret = -EINVAL;
3220 if (!pi_state)
3221 goto out_unlock;
3222
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02003223 /*
Peter Zijlstra16ffa122017-03-22 11:35:55 +01003224 * If current does not own the pi_state then the futex is
3225 * inconsistent and user space fiddled with the futex value.
3226 */
3227 if (pi_state->owner != current)
3228 goto out_unlock;
3229
Peter Zijlstra16ffa122017-03-22 11:35:55 +01003230 get_pi_state(pi_state);
Peter Zijlstrabebe5b52017-03-22 11:35:59 +01003231 /*
Peter Zijlstrabebe5b52017-03-22 11:35:59 +01003232 * By taking wait_lock while still holding hb->lock, we ensure
3233 * there is no point where we hold neither; and therefore
3234 * wake_futex_pi() must observe a state consistent with what we
3235 * observed.
Thomas Gleixner1a1fb982019-01-29 23:15:12 +01003236 *
3237 * In particular; this forces __rt_mutex_start_proxy() to
3238 * complete such that we're guaranteed to observe the
3239 * rt_waiter. Also see the WARN in wake_futex_pi().
Peter Zijlstrabebe5b52017-03-22 11:35:59 +01003240 */
3241 raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
Peter Zijlstra16ffa122017-03-22 11:35:55 +01003242 spin_unlock(&hb->lock);
3243
Peter Zijlstrac74aef22017-09-22 17:48:06 +02003244 /* drops pi_state->pi_mutex.wait_lock */
Peter Zijlstra16ffa122017-03-22 11:35:55 +01003245 ret = wake_futex_pi(uaddr, uval, pi_state);
3246
3247 put_pi_state(pi_state);
3248
3249 /*
3250 * Success, we're done! No tricky corner cases.
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02003251 */
3252 if (!ret)
Jangwoong Kim0f9438502020-12-30 21:29:53 +09003253 return ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -07003254 /*
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00003255 * The atomic access to the futex value generated a
3256 * pagefault, so retry the user-access and the wakeup:
Ingo Molnarc87e2832006-06-27 02:54:58 -07003257 */
3258 if (ret == -EFAULT)
3259 goto pi_faulted;
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02003260 /*
Sebastian Andrzej Siewior89e9e662016-04-15 14:35:39 +02003261 * A unconditional UNLOCK_PI op raced against a waiter
3262 * setting the FUTEX_WAITERS bit. Try again.
3263 */
Will Deacon6b4f4bc2019-02-28 11:58:08 +00003264 if (ret == -EAGAIN)
3265 goto pi_retry;
Sebastian Andrzej Siewior89e9e662016-04-15 14:35:39 +02003266 /*
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02003267 * wake_futex_pi has detected invalid state. Tell user
3268 * space.
3269 */
Jangwoong Kim0f9438502020-12-30 21:29:53 +09003270 return ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -07003271 }
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00003272
Ingo Molnarc87e2832006-06-27 02:54:58 -07003273 /*
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00003274 * We have no kernel internal state, i.e. no waiters in the
3275 * kernel. Waiters which are about to queue themselves are stuck
3276 * on hb->lock. So we can safely ignore them. We do neither
3277 * preserve the WAITERS bit not the OWNER_DIED one. We are the
3278 * owner.
Ingo Molnarc87e2832006-06-27 02:54:58 -07003279 */
Will Deacon6b4f4bc2019-02-28 11:58:08 +00003280 if ((ret = cmpxchg_futex_value_locked(&curval, uaddr, uval, 0))) {
Peter Zijlstra16ffa122017-03-22 11:35:55 +01003281 spin_unlock(&hb->lock);
Will Deacon6b4f4bc2019-02-28 11:58:08 +00003282 switch (ret) {
3283 case -EFAULT:
3284 goto pi_faulted;
3285
3286 case -EAGAIN:
3287 goto pi_retry;
3288
3289 default:
3290 WARN_ON_ONCE(1);
Jangwoong Kim0f9438502020-12-30 21:29:53 +09003291 return ret;
Will Deacon6b4f4bc2019-02-28 11:58:08 +00003292 }
Peter Zijlstra16ffa122017-03-22 11:35:55 +01003293 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07003294
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00003295 /*
3296 * If uval has changed, let user space handle it.
3297 */
3298 ret = (curval == uval) ? 0 : -EAGAIN;
3299
Ingo Molnarc87e2832006-06-27 02:54:58 -07003300out_unlock:
3301 spin_unlock(&hb->lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003302 return ret;
3303
Will Deacon6b4f4bc2019-02-28 11:58:08 +00003304pi_retry:
Will Deacon6b4f4bc2019-02-28 11:58:08 +00003305 cond_resched();
3306 goto retry;
3307
Ingo Molnarc87e2832006-06-27 02:54:58 -07003308pi_faulted:
Ingo Molnarc87e2832006-06-27 02:54:58 -07003309
Thomas Gleixnerd0725992009-06-11 23:15:43 +02003310 ret = fault_in_user_writeable(uaddr);
Darren Hartb5686362008-12-18 15:06:34 -08003311 if (!ret)
Ingo Molnarc87e2832006-06-27 02:54:58 -07003312 goto retry;
3313
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 return ret;
3315}
3316
Darren Hart52400ba2009-04-03 13:40:49 -07003317/**
Thomas Gleixner6231acb2021-08-15 23:29:17 +02003318 * handle_early_requeue_pi_wakeup() - Handle early wakeup on the initial futex
Darren Hart52400ba2009-04-03 13:40:49 -07003319 * @hb: the hash_bucket futex_q was original enqueued on
3320 * @q: the futex_q woken while waiting to be requeued
Darren Hart52400ba2009-04-03 13:40:49 -07003321 * @timeout: the timeout associated with the wait (NULL if none)
3322 *
Thomas Gleixner6231acb2021-08-15 23:29:17 +02003323 * Determine the cause for the early wakeup.
Darren Hart52400ba2009-04-03 13:40:49 -07003324 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08003325 * Return:
Thomas Gleixner6231acb2021-08-15 23:29:17 +02003326 * -EWOULDBLOCK or -ETIMEDOUT or -ERESTARTNOINTR
Darren Hart52400ba2009-04-03 13:40:49 -07003327 */
3328static inline
3329int handle_early_requeue_pi_wakeup(struct futex_hash_bucket *hb,
Thomas Gleixner6231acb2021-08-15 23:29:17 +02003330 struct futex_q *q,
Darren Hart52400ba2009-04-03 13:40:49 -07003331 struct hrtimer_sleeper *timeout)
3332{
Thomas Gleixner6231acb2021-08-15 23:29:17 +02003333 int ret;
Darren Hart52400ba2009-04-03 13:40:49 -07003334
3335 /*
3336 * With the hb lock held, we avoid races while we process the wakeup.
3337 * We only need to hold hb (and not hb2) to ensure atomicity as the
3338 * wakeup code can't change q.key from uaddr to uaddr2 if we hold hb.
3339 * It can't be requeued from uaddr2 to something else since we don't
3340 * support a PI aware source futex for requeue.
3341 */
Thomas Gleixner6231acb2021-08-15 23:29:17 +02003342 WARN_ON_ONCE(&hb->lock != q->lock_ptr);
Darren Hart52400ba2009-04-03 13:40:49 -07003343
Thomas Gleixner6231acb2021-08-15 23:29:17 +02003344 /*
3345 * We were woken prior to requeue by a timeout or a signal.
3346 * Unqueue the futex_q and determine which it was.
3347 */
3348 plist_del(&q->list, &hb->chain);
3349 hb_waiters_dec(hb);
3350
3351 /* Handle spurious wakeups gracefully */
3352 ret = -EWOULDBLOCK;
3353 if (timeout && !timeout->task)
3354 ret = -ETIMEDOUT;
3355 else if (signal_pending(current))
3356 ret = -ERESTARTNOINTR;
Darren Hart52400ba2009-04-03 13:40:49 -07003357 return ret;
3358}
3359
3360/**
3361 * futex_wait_requeue_pi() - Wait on uaddr and take uaddr2
Darren Hart56ec1602009-09-21 22:29:59 -07003362 * @uaddr: the futex we initially wait on (non-pi)
Darren Hartb41277d2010-11-08 13:10:09 -08003363 * @flags: futex flags (FLAGS_SHARED, FLAGS_CLOCKRT, etc.), they must be
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07003364 * the same type, no requeueing from private to shared, etc.
Darren Hart52400ba2009-04-03 13:40:49 -07003365 * @val: the expected value of uaddr
3366 * @abs_time: absolute timeout
Darren Hart56ec1602009-09-21 22:29:59 -07003367 * @bitset: 32 bit wakeup bitset set by userspace, defaults to all
Darren Hart52400ba2009-04-03 13:40:49 -07003368 * @uaddr2: the pi futex we will take prior to returning to user-space
3369 *
3370 * The caller will wait on uaddr and will be requeued by futex_requeue() to
Darren Hart6f7b0a22012-07-20 11:53:31 -07003371 * uaddr2 which must be PI aware and unique from uaddr. Normal wakeup will wake
3372 * on uaddr2 and complete the acquisition of the rt_mutex prior to returning to
3373 * userspace. This ensures the rt_mutex maintains an owner when it has waiters;
3374 * without one, the pi logic would not know which task to boost/deboost, if
3375 * there was a need to.
Darren Hart52400ba2009-04-03 13:40:49 -07003376 *
3377 * We call schedule in futex_wait_queue_me() when we enqueue and return there
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08003378 * via the following--
Darren Hart52400ba2009-04-03 13:40:49 -07003379 * 1) wakeup on uaddr2 after an atomic lock acquisition by futex_requeue()
Darren Hartcc6db4e2009-07-31 16:20:10 -07003380 * 2) wakeup on uaddr2 after a requeue
3381 * 3) signal
3382 * 4) timeout
Darren Hart52400ba2009-04-03 13:40:49 -07003383 *
Darren Hartcc6db4e2009-07-31 16:20:10 -07003384 * If 3, cleanup and return -ERESTARTNOINTR.
Darren Hart52400ba2009-04-03 13:40:49 -07003385 *
3386 * If 2, we may then block on trying to take the rt_mutex and return via:
3387 * 5) successful lock
3388 * 6) signal
3389 * 7) timeout
3390 * 8) other lock acquisition failure
3391 *
Darren Hartcc6db4e2009-07-31 16:20:10 -07003392 * If 6, return -EWOULDBLOCK (restarting the syscall would do the same).
Darren Hart52400ba2009-04-03 13:40:49 -07003393 *
3394 * If 4 or 7, we cleanup and return with -ETIMEDOUT.
3395 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08003396 * Return:
Mauro Carvalho Chehab7b4ff1a2017-05-11 10:17:45 -03003397 * - 0 - On success;
3398 * - <0 - On error
Darren Hart52400ba2009-04-03 13:40:49 -07003399 */
Darren Hartb41277d2010-11-08 13:10:09 -08003400static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
Darren Hart52400ba2009-04-03 13:40:49 -07003401 u32 val, ktime_t *abs_time, u32 bitset,
Darren Hartb41277d2010-11-08 13:10:09 -08003402 u32 __user *uaddr2)
Darren Hart52400ba2009-04-03 13:40:49 -07003403{
Waiman Long5ca584d2019-05-28 12:03:45 -04003404 struct hrtimer_sleeper timeout, *to;
Darren Hart52400ba2009-04-03 13:40:49 -07003405 struct rt_mutex_waiter rt_waiter;
Darren Hart52400ba2009-04-03 13:40:49 -07003406 struct futex_hash_bucket *hb;
Darren Hart5bdb05f2010-11-08 13:40:28 -08003407 union futex_key key2 = FUTEX_KEY_INIT;
3408 struct futex_q q = futex_q_init;
Thomas Gleixner07d91ef52021-08-15 23:29:18 +02003409 struct rt_mutex_base *pi_mutex;
Darren Hart52400ba2009-04-03 13:40:49 -07003410 int res, ret;
Darren Hart52400ba2009-04-03 13:40:49 -07003411
Nicolas Pitrebc2eecd2017-08-01 00:31:32 -04003412 if (!IS_ENABLED(CONFIG_FUTEX_PI))
3413 return -ENOSYS;
3414
Darren Hart6f7b0a22012-07-20 11:53:31 -07003415 if (uaddr == uaddr2)
3416 return -EINVAL;
3417
Darren Hart52400ba2009-04-03 13:40:49 -07003418 if (!bitset)
3419 return -EINVAL;
3420
Waiman Long5ca584d2019-05-28 12:03:45 -04003421 to = futex_setup_timer(abs_time, &timeout, flags,
3422 current->timer_slack_ns);
Darren Hart52400ba2009-04-03 13:40:49 -07003423
3424 /*
3425 * The waiter is allocated on our stack, manipulated by the requeue
3426 * code while we sleep on uaddr.
3427 */
Peter Zijlstra50809352017-03-22 11:35:56 +01003428 rt_mutex_init_waiter(&rt_waiter);
Darren Hart52400ba2009-04-03 13:40:49 -07003429
Linus Torvalds96d4f262019-01-03 18:57:57 -08003430 ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, FUTEX_WRITE);
Darren Hart52400ba2009-04-03 13:40:49 -07003431 if (unlikely(ret != 0))
3432 goto out;
3433
Darren Hart84bc4af2009-08-13 17:36:53 -07003434 q.bitset = bitset;
3435 q.rt_waiter = &rt_waiter;
3436 q.requeue_pi_key = &key2;
3437
Darren Hart7ada8762010-10-17 08:35:04 -07003438 /*
Thomas Gleixnerc363b7e2021-08-15 23:29:06 +02003439 * Prepare to wait on uaddr. On success, it holds hb->lock and q
3440 * is initialized.
Darren Hart7ada8762010-10-17 08:35:04 -07003441 */
Darren Hartb41277d2010-11-08 13:10:09 -08003442 ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
Thomas Gleixnerc8b15a72009-05-20 09:18:50 +02003443 if (ret)
André Almeida9180bd42020-07-02 17:28:40 -03003444 goto out;
Darren Hart52400ba2009-04-03 13:40:49 -07003445
Thomas Gleixnere9c243a2014-06-03 12:27:06 +00003446 /*
3447 * The check above which compares uaddrs is not sufficient for
3448 * shared futexes. We need to compare the keys:
3449 */
3450 if (match_futex(&q.key, &key2)) {
Thomas Gleixner13c42c22014-09-11 23:44:35 +02003451 queue_unlock(hb);
Thomas Gleixnere9c243a2014-06-03 12:27:06 +00003452 ret = -EINVAL;
André Almeida9180bd42020-07-02 17:28:40 -03003453 goto out;
Thomas Gleixnere9c243a2014-06-03 12:27:06 +00003454 }
3455
Darren Hart52400ba2009-04-03 13:40:49 -07003456 /* Queue the futex_q, drop the hb lock, wait for wakeup. */
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02003457 futex_wait_queue_me(hb, &q, to);
Darren Hart52400ba2009-04-03 13:40:49 -07003458
Thomas Gleixner07d91ef52021-08-15 23:29:18 +02003459 switch (futex_requeue_pi_wakeup_sync(&q)) {
3460 case Q_REQUEUE_PI_IGNORE:
3461 /* The waiter is still on uaddr1 */
3462 spin_lock(&hb->lock);
Thomas Gleixner6231acb2021-08-15 23:29:17 +02003463 ret = handle_early_requeue_pi_wakeup(hb, &q, to);
Thomas Gleixner07d91ef52021-08-15 23:29:18 +02003464 spin_unlock(&hb->lock);
3465 break;
Darren Hart52400ba2009-04-03 13:40:49 -07003466
Thomas Gleixner07d91ef52021-08-15 23:29:18 +02003467 case Q_REQUEUE_PI_LOCKED:
3468 /* The requeue acquired the lock */
Darren Hart52400ba2009-04-03 13:40:49 -07003469 if (q.pi_state && (q.pi_state->owner != current)) {
3470 spin_lock(q.lock_ptr);
Davidlohr Buesoa1565aa2021-02-26 09:50:27 -08003471 ret = fixup_owner(uaddr2, &q, true);
Thomas Gleixnerfb75a422015-12-19 20:07:38 +00003472 /*
Thomas Gleixner07d91ef52021-08-15 23:29:18 +02003473 * Drop the reference to the pi state which the
3474 * requeue_pi() code acquired for us.
Thomas Gleixnerfb75a422015-12-19 20:07:38 +00003475 */
Thomas Gleixner29e9ee52015-12-19 20:07:39 +00003476 put_pi_state(q.pi_state);
Darren Hart52400ba2009-04-03 13:40:49 -07003477 spin_unlock(q.lock_ptr);
Thomas Gleixner12bb3f72021-01-20 16:00:24 +01003478 /*
3479 * Adjust the return value. It's either -EFAULT or
3480 * success (1) but the caller expects 0 for success.
3481 */
3482 ret = ret < 0 ? ret : 0;
Darren Hart52400ba2009-04-03 13:40:49 -07003483 }
Thomas Gleixner07d91ef52021-08-15 23:29:18 +02003484 break;
Peter Zijlstrac236c8e2017-03-04 10:27:18 +01003485
Thomas Gleixner07d91ef52021-08-15 23:29:18 +02003486 case Q_REQUEUE_PI_DONE:
3487 /* Requeue completed. Current is 'pi_blocked_on' the rtmutex */
Darren Hart52400ba2009-04-03 13:40:49 -07003488 pi_mutex = &q.pi_state->pi_mutex;
Peter Zijlstra38d589f2017-03-22 11:35:57 +01003489 ret = rt_mutex_wait_proxy_lock(pi_mutex, to, &rt_waiter);
Darren Hart52400ba2009-04-03 13:40:49 -07003490
Thomas Gleixner07d91ef52021-08-15 23:29:18 +02003491 /* Current is not longer pi_blocked_on */
Darren Hart52400ba2009-04-03 13:40:49 -07003492 spin_lock(q.lock_ptr);
Peter Zijlstra38d589f2017-03-22 11:35:57 +01003493 if (ret && !rt_mutex_cleanup_proxy_lock(pi_mutex, &rt_waiter))
3494 ret = 0;
3495
3496 debug_rt_mutex_free_waiter(&rt_waiter);
Darren Hart52400ba2009-04-03 13:40:49 -07003497 /*
3498 * Fixup the pi_state owner and possibly acquire the lock if we
3499 * haven't already.
3500 */
Thomas Gleixnerae791a22010-11-10 13:30:36 +01003501 res = fixup_owner(uaddr2, &q, !ret);
Darren Hart52400ba2009-04-03 13:40:49 -07003502 /*
Ingo Molnar93d09552021-05-12 20:04:28 +02003503 * If fixup_owner() returned an error, propagate that. If it
Darren Hart56ec1602009-09-21 22:29:59 -07003504 * acquired the lock, clear -ETIMEDOUT or -EINTR.
Darren Hart52400ba2009-04-03 13:40:49 -07003505 */
3506 if (res)
3507 ret = (res < 0) ? res : 0;
3508
Darren Hart52400ba2009-04-03 13:40:49 -07003509 unqueue_me_pi(&q);
Davidlohr Buesoa3f24282021-02-26 09:50:28 -08003510 spin_unlock(q.lock_ptr);
Darren Hart52400ba2009-04-03 13:40:49 -07003511
Thomas Gleixner07d91ef52021-08-15 23:29:18 +02003512 if (ret == -EINTR) {
3513 /*
3514 * We've already been requeued, but cannot restart
3515 * by calling futex_lock_pi() directly. We could
3516 * restart this syscall, but it would detect that
3517 * the user space "val" changed and return
3518 * -EWOULDBLOCK. Save the overhead of the restart
3519 * and return -EWOULDBLOCK directly.
3520 */
3521 ret = -EWOULDBLOCK;
3522 }
3523 break;
3524 default:
3525 BUG();
Darren Hart52400ba2009-04-03 13:40:49 -07003526 }
3527
Darren Hart52400ba2009-04-03 13:40:49 -07003528out:
3529 if (to) {
3530 hrtimer_cancel(&to->timer);
3531 destroy_hrtimer_on_stack(&to->timer);
3532 }
3533 return ret;
3534}
3535
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003536/*
3537 * Support for robust futexes: the kernel cleans up held futexes at
3538 * thread exit time.
3539 *
3540 * Implementation: user-space maintains a per-thread list of locks it
3541 * is holding. Upon do_exit(), the kernel carefully walks this list,
3542 * and marks all locks that are owned by this thread with the
Ingo Molnarc87e2832006-06-27 02:54:58 -07003543 * FUTEX_OWNER_DIED bit, and wakes up a waiter (if any). The list is
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003544 * always manipulated with the lock held, so the list is private and
3545 * per-thread. Userspace also maintains a per-thread 'list_op_pending'
3546 * field, to allow the kernel to clean up if the thread dies after
3547 * acquiring the lock, but just before it could have added itself to
3548 * the list. There can only be one such pending lock.
3549 */
3550
3551/**
Darren Hartd96ee562009-09-21 22:30:22 -07003552 * sys_set_robust_list() - Set the robust-futex list head of a task
3553 * @head: pointer to the list-head
3554 * @len: length of the list-head, as userspace expects
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003555 */
Heiko Carstens836f92a2009-01-14 14:14:33 +01003556SYSCALL_DEFINE2(set_robust_list, struct robust_list_head __user *, head,
3557 size_t, len)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003558{
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08003559 if (!futex_cmpxchg_enabled)
3560 return -ENOSYS;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003561 /*
3562 * The kernel knows only one size for now:
3563 */
3564 if (unlikely(len != sizeof(*head)))
3565 return -EINVAL;
3566
3567 current->robust_list = head;
3568
3569 return 0;
3570}
3571
3572/**
Darren Hartd96ee562009-09-21 22:30:22 -07003573 * sys_get_robust_list() - Get the robust-futex list head of a task
3574 * @pid: pid of the process [zero for current task]
3575 * @head_ptr: pointer to a list-head pointer, the kernel fills it in
3576 * @len_ptr: pointer to a length field, the kernel fills in the header size
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003577 */
Heiko Carstens836f92a2009-01-14 14:14:33 +01003578SYSCALL_DEFINE3(get_robust_list, int, pid,
3579 struct robust_list_head __user * __user *, head_ptr,
3580 size_t __user *, len_ptr)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003581{
Al Viroba46df92006-10-10 22:46:07 +01003582 struct robust_list_head __user *head;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003583 unsigned long ret;
Kees Cookbdbb7762012-03-19 16:12:53 -07003584 struct task_struct *p;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003585
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08003586 if (!futex_cmpxchg_enabled)
3587 return -ENOSYS;
3588
Kees Cookbdbb7762012-03-19 16:12:53 -07003589 rcu_read_lock();
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003590
Kees Cookbdbb7762012-03-19 16:12:53 -07003591 ret = -ESRCH;
3592 if (!pid)
3593 p = current;
3594 else {
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07003595 p = find_task_by_vpid(pid);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003596 if (!p)
3597 goto err_unlock;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003598 }
3599
Kees Cookbdbb7762012-03-19 16:12:53 -07003600 ret = -EPERM;
Jann Horncaaee622016-01-20 15:00:04 -08003601 if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS))
Kees Cookbdbb7762012-03-19 16:12:53 -07003602 goto err_unlock;
3603
3604 head = p->robust_list;
3605 rcu_read_unlock();
3606
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003607 if (put_user(sizeof(*head), len_ptr))
3608 return -EFAULT;
3609 return put_user(head, head_ptr);
3610
3611err_unlock:
Oleg Nesterovaaa2a972006-09-29 02:00:55 -07003612 rcu_read_unlock();
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003613
3614 return ret;
3615}
3616
Yang Taoca16d5b2019-11-06 22:55:35 +01003617/* Constants for the pending_op argument of handle_futex_death */
3618#define HANDLE_DEATH_PENDING true
3619#define HANDLE_DEATH_LIST false
3620
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003621/*
3622 * Process a futex-list entry, check whether it's owned by the
3623 * dying task, and do notification if so:
3624 */
Yang Taoca16d5b2019-11-06 22:55:35 +01003625static int handle_futex_death(u32 __user *uaddr, struct task_struct *curr,
3626 bool pi, bool pending_op)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003627{
Kees Cook3f649ab2020-06-03 13:09:38 -07003628 u32 uval, nval, mval;
Will Deacon6b4f4bc2019-02-28 11:58:08 +00003629 int err;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003630
Chen Jie5a071682019-03-15 03:44:38 +00003631 /* Futex address must be 32bit aligned */
3632 if ((((unsigned long)uaddr) % sizeof(*uaddr)) != 0)
3633 return -1;
3634
Ingo Molnar8f17d3a2006-03-27 01:16:27 -08003635retry:
3636 if (get_user(uval, uaddr))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003637 return -1;
3638
Yang Taoca16d5b2019-11-06 22:55:35 +01003639 /*
3640 * Special case for regular (non PI) futexes. The unlock path in
3641 * user space has two race scenarios:
3642 *
3643 * 1. The unlock path releases the user space futex value and
3644 * before it can execute the futex() syscall to wake up
3645 * waiters it is killed.
3646 *
3647 * 2. A woken up waiter is killed before it can acquire the
3648 * futex in user space.
3649 *
3650 * In both cases the TID validation below prevents a wakeup of
3651 * potential waiters which can cause these waiters to block
3652 * forever.
3653 *
3654 * In both cases the following conditions are met:
3655 *
3656 * 1) task->robust_list->list_op_pending != NULL
3657 * @pending_op == true
3658 * 2) User space futex value == 0
3659 * 3) Regular futex: @pi == false
3660 *
3661 * If these conditions are met, it is safe to attempt waking up a
3662 * potential waiter without touching the user space futex value and
3663 * trying to set the OWNER_DIED bit. The user space futex value is
3664 * uncontended and the rest of the user space mutex state is
3665 * consistent, so a woken waiter will just take over the
3666 * uncontended futex. Setting the OWNER_DIED bit would create
3667 * inconsistent state and malfunction of the user space owner died
3668 * handling.
3669 */
3670 if (pending_op && !pi && !uval) {
3671 futex_wake(uaddr, 1, 1, FUTEX_BITSET_MATCH_ANY);
3672 return 0;
3673 }
3674
Will Deacon6b4f4bc2019-02-28 11:58:08 +00003675 if ((uval & FUTEX_TID_MASK) != task_pid_vnr(curr))
3676 return 0;
3677
3678 /*
3679 * Ok, this dying thread is truly holding a futex
3680 * of interest. Set the OWNER_DIED bit atomically
3681 * via cmpxchg, and if the value had FUTEX_WAITERS
3682 * set, wake up a waiter (if any). (We have to do a
3683 * futex_wake() even if OWNER_DIED is already set -
3684 * to handle the rare but possible case of recursive
3685 * thread-death.) The rest of the cleanup is done in
3686 * userspace.
3687 */
3688 mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
3689
3690 /*
3691 * We are not holding a lock here, but we want to have
3692 * the pagefault_disable/enable() protection because
3693 * we want to handle the fault gracefully. If the
3694 * access fails we try to fault in the futex with R/W
3695 * verification via get_user_pages. get_user() above
3696 * does not guarantee R/W access. If that fails we
3697 * give up and leave the futex locked.
3698 */
3699 if ((err = cmpxchg_futex_value_locked(&nval, uaddr, uval, mval))) {
3700 switch (err) {
3701 case -EFAULT:
Thomas Gleixner6e0aa9f2011-03-14 10:34:35 +01003702 if (fault_in_user_writeable(uaddr))
3703 return -1;
3704 goto retry;
Will Deacon6b4f4bc2019-02-28 11:58:08 +00003705
3706 case -EAGAIN:
3707 cond_resched();
Ingo Molnar8f17d3a2006-03-27 01:16:27 -08003708 goto retry;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003709
Will Deacon6b4f4bc2019-02-28 11:58:08 +00003710 default:
3711 WARN_ON_ONCE(1);
3712 return err;
3713 }
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003714 }
Will Deacon6b4f4bc2019-02-28 11:58:08 +00003715
3716 if (nval != uval)
3717 goto retry;
3718
3719 /*
3720 * Wake robust non-PI futexes here. The wakeup of
3721 * PI futexes happens in exit_pi_state():
3722 */
3723 if (!pi && (uval & FUTEX_WAITERS))
3724 futex_wake(uaddr, 1, 1, FUTEX_BITSET_MATCH_ANY);
3725
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003726 return 0;
3727}
3728
3729/*
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003730 * Fetch a robust-list pointer. Bit 0 signals PI futexes:
3731 */
3732static inline int fetch_robust_entry(struct robust_list __user **entry,
Al Viroba46df92006-10-10 22:46:07 +01003733 struct robust_list __user * __user *head,
Namhyung Kim1dcc41b2010-09-14 21:43:46 +09003734 unsigned int *pi)
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003735{
3736 unsigned long uentry;
3737
Al Viroba46df92006-10-10 22:46:07 +01003738 if (get_user(uentry, (unsigned long __user *)head))
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003739 return -EFAULT;
3740
Al Viroba46df92006-10-10 22:46:07 +01003741 *entry = (void __user *)(uentry & ~1UL);
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003742 *pi = uentry & 1;
3743
3744 return 0;
3745}
3746
3747/*
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003748 * Walk curr->robust_list (very carefully, it's a userspace list!)
3749 * and mark any locks found there dead, and notify any waiters.
3750 *
3751 * We silently return on any sign of list-walking problem.
3752 */
Thomas Gleixnerba31c1a42019-11-06 22:55:36 +01003753static void exit_robust_list(struct task_struct *curr)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003754{
3755 struct robust_list_head __user *head = curr->robust_list;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003756 struct robust_list __user *entry, *next_entry, *pending;
Darren Hart4c115e92010-11-04 15:00:00 -04003757 unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
Kees Cook3f649ab2020-06-03 13:09:38 -07003758 unsigned int next_pi;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003759 unsigned long futex_offset;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003760 int rc;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003761
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08003762 if (!futex_cmpxchg_enabled)
3763 return;
3764
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003765 /*
3766 * Fetch the list head (which was registered earlier, via
3767 * sys_set_robust_list()):
3768 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003769 if (fetch_robust_entry(&entry, &head->list.next, &pi))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003770 return;
3771 /*
3772 * Fetch the relative futex offset:
3773 */
3774 if (get_user(futex_offset, &head->futex_offset))
3775 return;
3776 /*
3777 * Fetch any possibly pending lock-add first, and handle it
3778 * if it exists:
3779 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003780 if (fetch_robust_entry(&pending, &head->list_op_pending, &pip))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003781 return;
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003782
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003783 next_entry = NULL; /* avoid warning with gcc */
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003784 while (entry != &head->list) {
3785 /*
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003786 * Fetch the next entry in the list before calling
3787 * handle_futex_death:
3788 */
3789 rc = fetch_robust_entry(&next_entry, &entry->next, &next_pi);
3790 /*
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003791 * A pending lock might already be on the list, so
Ingo Molnarc87e2832006-06-27 02:54:58 -07003792 * don't process it twice:
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003793 */
Yang Taoca16d5b2019-11-06 22:55:35 +01003794 if (entry != pending) {
Al Viroba46df92006-10-10 22:46:07 +01003795 if (handle_futex_death((void __user *)entry + futex_offset,
Yang Taoca16d5b2019-11-06 22:55:35 +01003796 curr, pi, HANDLE_DEATH_LIST))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003797 return;
Yang Taoca16d5b2019-11-06 22:55:35 +01003798 }
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003799 if (rc)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003800 return;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003801 entry = next_entry;
3802 pi = next_pi;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003803 /*
3804 * Avoid excessively long or circular lists:
3805 */
3806 if (!--limit)
3807 break;
3808
3809 cond_resched();
3810 }
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003811
Yang Taoca16d5b2019-11-06 22:55:35 +01003812 if (pending) {
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003813 handle_futex_death((void __user *)pending + futex_offset,
Yang Taoca16d5b2019-11-06 22:55:35 +01003814 curr, pip, HANDLE_DEATH_PENDING);
3815 }
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003816}
3817
Thomas Gleixneraf8cbda2019-11-06 22:55:43 +01003818static void futex_cleanup(struct task_struct *tsk)
Thomas Gleixnerba31c1a42019-11-06 22:55:36 +01003819{
3820 if (unlikely(tsk->robust_list)) {
3821 exit_robust_list(tsk);
3822 tsk->robust_list = NULL;
3823 }
3824
3825#ifdef CONFIG_COMPAT
3826 if (unlikely(tsk->compat_robust_list)) {
3827 compat_exit_robust_list(tsk);
3828 tsk->compat_robust_list = NULL;
3829 }
3830#endif
3831
3832 if (unlikely(!list_empty(&tsk->pi_state_list)))
3833 exit_pi_state_list(tsk);
3834}
3835
Thomas Gleixner18f69432019-11-06 22:55:41 +01003836/**
3837 * futex_exit_recursive - Set the tasks futex state to FUTEX_STATE_DEAD
3838 * @tsk: task to set the state on
3839 *
3840 * Set the futex exit state of the task lockless. The futex waiter code
3841 * observes that state when a task is exiting and loops until the task has
3842 * actually finished the futex cleanup. The worst case for this is that the
3843 * waiter runs through the wait loop until the state becomes visible.
3844 *
3845 * This is called from the recursive fault handling path in do_exit().
3846 *
3847 * This is best effort. Either the futex exit code has run already or
3848 * not. If the OWNER_DIED bit has been set on the futex then the waiter can
3849 * take it over. If not, the problem is pushed back to user space. If the
3850 * futex exit code did not run yet, then an already queued waiter might
3851 * block forever, but there is nothing which can be done about that.
3852 */
3853void futex_exit_recursive(struct task_struct *tsk)
3854{
Thomas Gleixner3f186d92019-11-06 22:55:44 +01003855 /* If the state is FUTEX_STATE_EXITING then futex_exit_mutex is held */
3856 if (tsk->futex_state == FUTEX_STATE_EXITING)
3857 mutex_unlock(&tsk->futex_exit_mutex);
Thomas Gleixner18f69432019-11-06 22:55:41 +01003858 tsk->futex_state = FUTEX_STATE_DEAD;
3859}
3860
Thomas Gleixneraf8cbda2019-11-06 22:55:43 +01003861static void futex_cleanup_begin(struct task_struct *tsk)
Thomas Gleixner150d7152019-11-06 22:55:39 +01003862{
Thomas Gleixner18f69432019-11-06 22:55:41 +01003863 /*
Thomas Gleixner3f186d92019-11-06 22:55:44 +01003864 * Prevent various race issues against a concurrent incoming waiter
3865 * including live locks by forcing the waiter to block on
3866 * tsk->futex_exit_mutex when it observes FUTEX_STATE_EXITING in
3867 * attach_to_pi_owner().
3868 */
3869 mutex_lock(&tsk->futex_exit_mutex);
3870
3871 /*
Thomas Gleixner4a8e9912019-11-06 22:55:42 +01003872 * Switch the state to FUTEX_STATE_EXITING under tsk->pi_lock.
3873 *
3874 * This ensures that all subsequent checks of tsk->futex_state in
3875 * attach_to_pi_owner() must observe FUTEX_STATE_EXITING with
3876 * tsk->pi_lock held.
3877 *
3878 * It guarantees also that a pi_state which was queued right before
3879 * the state change under tsk->pi_lock by a concurrent waiter must
3880 * be observed in exit_pi_state_list().
Thomas Gleixner18f69432019-11-06 22:55:41 +01003881 */
3882 raw_spin_lock_irq(&tsk->pi_lock);
Thomas Gleixner4a8e9912019-11-06 22:55:42 +01003883 tsk->futex_state = FUTEX_STATE_EXITING;
Thomas Gleixner18f69432019-11-06 22:55:41 +01003884 raw_spin_unlock_irq(&tsk->pi_lock);
Thomas Gleixneraf8cbda2019-11-06 22:55:43 +01003885}
Thomas Gleixner18f69432019-11-06 22:55:41 +01003886
Thomas Gleixneraf8cbda2019-11-06 22:55:43 +01003887static void futex_cleanup_end(struct task_struct *tsk, int state)
3888{
3889 /*
3890 * Lockless store. The only side effect is that an observer might
3891 * take another loop until it becomes visible.
3892 */
3893 tsk->futex_state = state;
Thomas Gleixner3f186d92019-11-06 22:55:44 +01003894 /*
3895 * Drop the exit protection. This unblocks waiters which observed
3896 * FUTEX_STATE_EXITING to reevaluate the state.
3897 */
3898 mutex_unlock(&tsk->futex_exit_mutex);
Thomas Gleixneraf8cbda2019-11-06 22:55:43 +01003899}
Thomas Gleixner18f69432019-11-06 22:55:41 +01003900
Thomas Gleixneraf8cbda2019-11-06 22:55:43 +01003901void futex_exec_release(struct task_struct *tsk)
3902{
3903 /*
3904 * The state handling is done for consistency, but in the case of
Ingo Molnar93d09552021-05-12 20:04:28 +02003905 * exec() there is no way to prevent further damage as the PID stays
Thomas Gleixneraf8cbda2019-11-06 22:55:43 +01003906 * the same. But for the unlikely and arguably buggy case that a
3907 * futex is held on exec(), this provides at least as much state
3908 * consistency protection which is possible.
3909 */
3910 futex_cleanup_begin(tsk);
3911 futex_cleanup(tsk);
3912 /*
3913 * Reset the state to FUTEX_STATE_OK. The task is alive and about
3914 * exec a new binary.
3915 */
3916 futex_cleanup_end(tsk, FUTEX_STATE_OK);
3917}
3918
3919void futex_exit_release(struct task_struct *tsk)
3920{
3921 futex_cleanup_begin(tsk);
3922 futex_cleanup(tsk);
3923 futex_cleanup_end(tsk, FUTEX_STATE_DEAD);
Thomas Gleixner150d7152019-11-06 22:55:39 +01003924}
3925
Pierre Peifferc19384b2007-05-09 02:35:02 -07003926long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
Ingo Molnare2970f22006-06-27 02:54:47 -07003927 u32 __user *uaddr2, u32 val2, u32 val3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928{
Thomas Gleixner81b40532012-02-15 12:17:09 +01003929 int cmd = op & FUTEX_CMD_MASK;
Darren Hartb41277d2010-11-08 13:10:09 -08003930 unsigned int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003932 if (!(op & FUTEX_PRIVATE_FLAG))
Darren Hartb41277d2010-11-08 13:10:09 -08003933 flags |= FLAGS_SHARED;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003934
Darren Hartb41277d2010-11-08 13:10:09 -08003935 if (op & FUTEX_CLOCK_REALTIME) {
3936 flags |= FLAGS_CLOCKRT;
Thomas Gleixnerbf22a692021-04-22 21:44:23 +02003937 if (cmd != FUTEX_WAIT_BITSET && cmd != FUTEX_WAIT_REQUEUE_PI &&
3938 cmd != FUTEX_LOCK_PI2)
Darren Hartb41277d2010-11-08 13:10:09 -08003939 return -ENOSYS;
3940 }
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003941
3942 switch (cmd) {
Thomas Gleixner59263b52012-02-15 12:08:34 +01003943 case FUTEX_LOCK_PI:
Thomas Gleixnerbf22a692021-04-22 21:44:23 +02003944 case FUTEX_LOCK_PI2:
Thomas Gleixner59263b52012-02-15 12:08:34 +01003945 case FUTEX_UNLOCK_PI:
3946 case FUTEX_TRYLOCK_PI:
3947 case FUTEX_WAIT_REQUEUE_PI:
3948 case FUTEX_CMP_REQUEUE_PI:
3949 if (!futex_cmpxchg_enabled)
3950 return -ENOSYS;
3951 }
3952
3953 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 case FUTEX_WAIT:
Thomas Gleixnercd689982008-02-01 17:45:14 +01003955 val3 = FUTEX_BITSET_MATCH_ANY;
Miaohe Lin405fa8a2020-08-13 08:21:17 -04003956 fallthrough;
Thomas Gleixnercd689982008-02-01 17:45:14 +01003957 case FUTEX_WAIT_BITSET:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003958 return futex_wait(uaddr, flags, val, timeout, val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 case FUTEX_WAKE:
Thomas Gleixnercd689982008-02-01 17:45:14 +01003960 val3 = FUTEX_BITSET_MATCH_ANY;
Miaohe Lin405fa8a2020-08-13 08:21:17 -04003961 fallthrough;
Thomas Gleixnercd689982008-02-01 17:45:14 +01003962 case FUTEX_WAKE_BITSET:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003963 return futex_wake(uaddr, flags, val, val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 case FUTEX_REQUEUE:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003965 return futex_requeue(uaddr, flags, uaddr2, val, val2, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 case FUTEX_CMP_REQUEUE:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003967 return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 0);
Jakub Jelinek4732efbe2005-09-06 15:16:25 -07003968 case FUTEX_WAKE_OP:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003969 return futex_wake_op(uaddr, flags, uaddr2, val, val2, val3);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003970 case FUTEX_LOCK_PI:
Thomas Gleixnere112c412021-04-22 21:44:22 +02003971 flags |= FLAGS_CLOCKRT;
Thomas Gleixnerbf22a692021-04-22 21:44:23 +02003972 fallthrough;
3973 case FUTEX_LOCK_PI2:
Michael Kerrisk996636d2015-01-16 20:28:06 +01003974 return futex_lock_pi(uaddr, flags, timeout, 0);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003975 case FUTEX_UNLOCK_PI:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003976 return futex_unlock_pi(uaddr, flags);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003977 case FUTEX_TRYLOCK_PI:
Michael Kerrisk996636d2015-01-16 20:28:06 +01003978 return futex_lock_pi(uaddr, flags, NULL, 1);
Darren Hart52400ba2009-04-03 13:40:49 -07003979 case FUTEX_WAIT_REQUEUE_PI:
3980 val3 = FUTEX_BITSET_MATCH_ANY;
Thomas Gleixner81b40532012-02-15 12:17:09 +01003981 return futex_wait_requeue_pi(uaddr, flags, val, timeout, val3,
3982 uaddr2);
Darren Hart52400ba2009-04-03 13:40:49 -07003983 case FUTEX_CMP_REQUEUE_PI:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003984 return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 }
Thomas Gleixner81b40532012-02-15 12:17:09 +01003986 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987}
3988
Thomas Gleixner51cf94d2021-04-22 21:44:21 +02003989static __always_inline bool futex_cmd_has_timeout(u32 cmd)
3990{
3991 switch (cmd) {
3992 case FUTEX_WAIT:
3993 case FUTEX_LOCK_PI:
Thomas Gleixnerbf22a692021-04-22 21:44:23 +02003994 case FUTEX_LOCK_PI2:
Thomas Gleixner51cf94d2021-04-22 21:44:21 +02003995 case FUTEX_WAIT_BITSET:
3996 case FUTEX_WAIT_REQUEUE_PI:
3997 return true;
3998 }
3999 return false;
4000}
4001
4002static __always_inline int
4003futex_init_timeout(u32 cmd, u32 op, struct timespec64 *ts, ktime_t *t)
4004{
4005 if (!timespec64_valid(ts))
4006 return -EINVAL;
4007
4008 *t = timespec64_to_ktime(*ts);
4009 if (cmd == FUTEX_WAIT)
4010 *t = ktime_add_safe(ktime_get(), *t);
4011 else if (cmd != FUTEX_LOCK_PI && !(op & FUTEX_CLOCK_REALTIME))
4012 *t = timens_ktime_to_host(CLOCK_MONOTONIC, *t);
4013 return 0;
4014}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015
Heiko Carstens17da2bd2009-01-14 14:14:10 +01004016SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val,
Alejandro Colomar1ce53e22020-11-28 13:39:46 +01004017 const struct __kernel_timespec __user *, utime,
4018 u32 __user *, uaddr2, u32, val3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019{
Thomas Gleixner51cf94d2021-04-22 21:44:21 +02004020 int ret, cmd = op & FUTEX_CMD_MASK;
Pierre Peifferc19384b2007-05-09 02:35:02 -07004021 ktime_t t, *tp = NULL;
Thomas Gleixner51cf94d2021-04-22 21:44:21 +02004022 struct timespec64 ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023
Thomas Gleixner51cf94d2021-04-22 21:44:21 +02004024 if (utime && futex_cmd_has_timeout(cmd)) {
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07004025 if (unlikely(should_fail_futex(!(op & FUTEX_PRIVATE_FLAG))))
4026 return -EFAULT;
Arnd Bergmannbec2f7c2018-04-17 17:23:35 +02004027 if (get_timespec64(&ts, utime))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 return -EFAULT;
Thomas Gleixner51cf94d2021-04-22 21:44:21 +02004029 ret = futex_init_timeout(cmd, op, &ts, &t);
4030 if (ret)
4031 return ret;
Pierre Peifferc19384b2007-05-09 02:35:02 -07004032 tp = &t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034
Thomas Gleixnerb097d5e2021-04-22 21:44:20 +02004035 return do_futex(uaddr, op, val, tp, uaddr2, (unsigned long)utime, val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036}
4037
Arnd Bergmann04e77122018-04-17 16:31:07 +02004038#ifdef CONFIG_COMPAT
4039/*
4040 * Fetch a robust-list pointer. Bit 0 signals PI futexes:
4041 */
4042static inline int
4043compat_fetch_robust_entry(compat_uptr_t *uentry, struct robust_list __user **entry,
4044 compat_uptr_t __user *head, unsigned int *pi)
4045{
4046 if (get_user(*uentry, head))
4047 return -EFAULT;
4048
4049 *entry = compat_ptr((*uentry) & ~1);
4050 *pi = (unsigned int)(*uentry) & 1;
4051
4052 return 0;
4053}
4054
4055static void __user *futex_uaddr(struct robust_list __user *entry,
4056 compat_long_t futex_offset)
4057{
4058 compat_uptr_t base = ptr_to_compat(entry);
4059 void __user *uaddr = compat_ptr(base + futex_offset);
4060
4061 return uaddr;
4062}
4063
4064/*
4065 * Walk curr->robust_list (very carefully, it's a userspace list!)
4066 * and mark any locks found there dead, and notify any waiters.
4067 *
4068 * We silently return on any sign of list-walking problem.
4069 */
Thomas Gleixnerba31c1a42019-11-06 22:55:36 +01004070static void compat_exit_robust_list(struct task_struct *curr)
Arnd Bergmann04e77122018-04-17 16:31:07 +02004071{
4072 struct compat_robust_list_head __user *head = curr->compat_robust_list;
4073 struct robust_list __user *entry, *next_entry, *pending;
4074 unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
Kees Cook3f649ab2020-06-03 13:09:38 -07004075 unsigned int next_pi;
Arnd Bergmann04e77122018-04-17 16:31:07 +02004076 compat_uptr_t uentry, next_uentry, upending;
4077 compat_long_t futex_offset;
4078 int rc;
4079
4080 if (!futex_cmpxchg_enabled)
4081 return;
4082
4083 /*
4084 * Fetch the list head (which was registered earlier, via
4085 * sys_set_robust_list()):
4086 */
4087 if (compat_fetch_robust_entry(&uentry, &entry, &head->list.next, &pi))
4088 return;
4089 /*
4090 * Fetch the relative futex offset:
4091 */
4092 if (get_user(futex_offset, &head->futex_offset))
4093 return;
4094 /*
4095 * Fetch any possibly pending lock-add first, and handle it
4096 * if it exists:
4097 */
4098 if (compat_fetch_robust_entry(&upending, &pending,
4099 &head->list_op_pending, &pip))
4100 return;
4101
4102 next_entry = NULL; /* avoid warning with gcc */
4103 while (entry != (struct robust_list __user *) &head->list) {
4104 /*
4105 * Fetch the next entry in the list before calling
4106 * handle_futex_death:
4107 */
4108 rc = compat_fetch_robust_entry(&next_uentry, &next_entry,
4109 (compat_uptr_t __user *)&entry->next, &next_pi);
4110 /*
4111 * A pending lock might already be on the list, so
4112 * dont process it twice:
4113 */
4114 if (entry != pending) {
4115 void __user *uaddr = futex_uaddr(entry, futex_offset);
4116
Yang Taoca16d5b2019-11-06 22:55:35 +01004117 if (handle_futex_death(uaddr, curr, pi,
4118 HANDLE_DEATH_LIST))
Arnd Bergmann04e77122018-04-17 16:31:07 +02004119 return;
4120 }
4121 if (rc)
4122 return;
4123 uentry = next_uentry;
4124 entry = next_entry;
4125 pi = next_pi;
4126 /*
4127 * Avoid excessively long or circular lists:
4128 */
4129 if (!--limit)
4130 break;
4131
4132 cond_resched();
4133 }
4134 if (pending) {
4135 void __user *uaddr = futex_uaddr(pending, futex_offset);
4136
Yang Taoca16d5b2019-11-06 22:55:35 +01004137 handle_futex_death(uaddr, curr, pip, HANDLE_DEATH_PENDING);
Arnd Bergmann04e77122018-04-17 16:31:07 +02004138 }
4139}
4140
4141COMPAT_SYSCALL_DEFINE2(set_robust_list,
4142 struct compat_robust_list_head __user *, head,
4143 compat_size_t, len)
4144{
4145 if (!futex_cmpxchg_enabled)
4146 return -ENOSYS;
4147
4148 if (unlikely(len != sizeof(*head)))
4149 return -EINVAL;
4150
4151 current->compat_robust_list = head;
4152
4153 return 0;
4154}
4155
4156COMPAT_SYSCALL_DEFINE3(get_robust_list, int, pid,
4157 compat_uptr_t __user *, head_ptr,
4158 compat_size_t __user *, len_ptr)
4159{
4160 struct compat_robust_list_head __user *head;
4161 unsigned long ret;
4162 struct task_struct *p;
4163
4164 if (!futex_cmpxchg_enabled)
4165 return -ENOSYS;
4166
4167 rcu_read_lock();
4168
4169 ret = -ESRCH;
4170 if (!pid)
4171 p = current;
4172 else {
4173 p = find_task_by_vpid(pid);
4174 if (!p)
4175 goto err_unlock;
4176 }
4177
4178 ret = -EPERM;
4179 if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS))
4180 goto err_unlock;
4181
4182 head = p->compat_robust_list;
4183 rcu_read_unlock();
4184
4185 if (put_user(sizeof(*head), len_ptr))
4186 return -EFAULT;
4187 return put_user(ptr_to_compat(head), head_ptr);
4188
4189err_unlock:
4190 rcu_read_unlock();
4191
4192 return ret;
4193}
Arnd Bergmannbec2f7c2018-04-17 17:23:35 +02004194#endif /* CONFIG_COMPAT */
Arnd Bergmann04e77122018-04-17 16:31:07 +02004195
Arnd Bergmannbec2f7c2018-04-17 17:23:35 +02004196#ifdef CONFIG_COMPAT_32BIT_TIME
Arnd Bergmann8dabe722019-01-07 00:33:08 +01004197SYSCALL_DEFINE6(futex_time32, u32 __user *, uaddr, int, op, u32, val,
Alejandro Colomar1ce53e22020-11-28 13:39:46 +01004198 const struct old_timespec32 __user *, utime, u32 __user *, uaddr2,
Arnd Bergmann04e77122018-04-17 16:31:07 +02004199 u32, val3)
4200{
Thomas Gleixner51cf94d2021-04-22 21:44:21 +02004201 int ret, cmd = op & FUTEX_CMD_MASK;
Arnd Bergmann04e77122018-04-17 16:31:07 +02004202 ktime_t t, *tp = NULL;
Thomas Gleixner51cf94d2021-04-22 21:44:21 +02004203 struct timespec64 ts;
Arnd Bergmann04e77122018-04-17 16:31:07 +02004204
Thomas Gleixner51cf94d2021-04-22 21:44:21 +02004205 if (utime && futex_cmd_has_timeout(cmd)) {
Arnd Bergmannbec2f7c2018-04-17 17:23:35 +02004206 if (get_old_timespec32(&ts, utime))
Arnd Bergmann04e77122018-04-17 16:31:07 +02004207 return -EFAULT;
Thomas Gleixner51cf94d2021-04-22 21:44:21 +02004208 ret = futex_init_timeout(cmd, op, &ts, &t);
4209 if (ret)
4210 return ret;
Arnd Bergmann04e77122018-04-17 16:31:07 +02004211 tp = &t;
4212 }
Arnd Bergmann04e77122018-04-17 16:31:07 +02004213
Thomas Gleixnerb097d5e2021-04-22 21:44:20 +02004214 return do_futex(uaddr, op, val, tp, uaddr2, (unsigned long)utime, val3);
Arnd Bergmann04e77122018-04-17 16:31:07 +02004215}
Arnd Bergmannbec2f7c2018-04-17 17:23:35 +02004216#endif /* CONFIG_COMPAT_32BIT_TIME */
Arnd Bergmann04e77122018-04-17 16:31:07 +02004217
Heiko Carstens03b8c7b2014-03-02 13:09:47 +01004218static void __init futex_detect_cmpxchg(void)
4219{
4220#ifndef CONFIG_HAVE_FUTEX_CMPXCHG
4221 u32 curval;
4222
4223 /*
4224 * This will fail and we want it. Some arch implementations do
4225 * runtime detection of the futex_atomic_cmpxchg_inatomic()
4226 * functionality. We want to know that before we call in any
4227 * of the complex code paths. Also we want to prevent
4228 * registration of robust lists in that case. NULL is
4229 * guaranteed to fault and we get -EFAULT on functional
4230 * implementation, the non-functional ones will return
4231 * -ENOSYS.
4232 */
4233 if (cmpxchg_futex_value_locked(&curval, NULL, 0, 0) == -EFAULT)
4234 futex_cmpxchg_enabled = 1;
4235#endif
4236}
4237
Benjamin Herrenschmidtf6d107f2008-03-27 14:52:15 +11004238static int __init futex_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239{
Heiko Carstens63b1a812014-01-16 14:54:50 +01004240 unsigned int futex_shift;
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -08004241 unsigned long i;
4242
4243#if CONFIG_BASE_SMALL
4244 futex_hashsize = 16;
4245#else
4246 futex_hashsize = roundup_pow_of_two(256 * num_possible_cpus());
4247#endif
4248
4249 futex_queues = alloc_large_system_hash("futex", sizeof(*futex_queues),
4250 futex_hashsize, 0,
4251 futex_hashsize < 256 ? HASH_SMALL : 0,
Heiko Carstens63b1a812014-01-16 14:54:50 +01004252 &futex_shift, NULL,
4253 futex_hashsize, futex_hashsize);
4254 futex_hashsize = 1UL << futex_shift;
Heiko Carstens03b8c7b2014-03-02 13:09:47 +01004255
4256 futex_detect_cmpxchg();
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08004257
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -08004258 for (i = 0; i < futex_hashsize; i++) {
Linus Torvalds11d46162014-03-20 22:11:17 -07004259 atomic_set(&futex_queues[i].waiters, 0);
Dima Zavin732375c2011-07-07 17:27:59 -07004260 plist_head_init(&futex_queues[i].chain);
Thomas Gleixner3e4ab742008-02-23 15:23:55 -08004261 spin_lock_init(&futex_queues[i].lock);
4262 }
4263
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 return 0;
4265}
Yang Yang25f71d12016-12-30 16:17:55 +08004266core_initcall(futex_init);