blob: c3c7f5494bfd4649e96169d37fb8e6abc06ee33d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Fast Userspace Mutexes (which I call "Futexes!").
3 * (C) Rusty Russell, IBM 2002
4 *
5 * Generalized futexes, futex requeueing, misc fixes by Ingo Molnar
6 * (C) Copyright 2003 Red Hat Inc, All Rights Reserved
7 *
8 * Removed page pinning, fix privately mapped COW pages and other cleanups
9 * (C) Copyright 2003, 2004 Jamie Lokier
10 *
Ingo Molnar0771dfe2006-03-27 01:16:22 -080011 * Robust futex support started by Ingo Molnar
12 * (C) Copyright 2006 Red Hat Inc, All Rights Reserved
13 * Thanks to Thomas Gleixner for suggestions, analysis and fixes.
14 *
Ingo Molnarc87e2832006-06-27 02:54:58 -070015 * PI-futex support started by Ingo Molnar and Thomas Gleixner
16 * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
17 * Copyright (C) 2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
18 *
Eric Dumazet34f01cc2007-05-09 02:35:04 -070019 * PRIVATE futexes by Eric Dumazet
20 * Copyright (C) 2007 Eric Dumazet <dada1@cosmosbay.com>
21 *
Darren Hart52400ba2009-04-03 13:40:49 -070022 * Requeue-PI support by Darren Hart <dvhltc@us.ibm.com>
23 * Copyright (C) IBM Corporation, 2009
24 * Thanks to Thomas Gleixner for conceptual design and careful reviews.
25 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 * Thanks to Ben LaHaise for yelling "hashed waitqueues" loudly
27 * enough at me, Linus for the original (flawed) idea, Matthew
28 * Kirkwood for proof-of-concept implementation.
29 *
30 * "The futexes are also cursed."
31 * "But they come in a choice of three flavours!"
32 *
33 * This program is free software; you can redistribute it and/or modify
34 * it under the terms of the GNU General Public License as published by
35 * the Free Software Foundation; either version 2 of the License, or
36 * (at your option) any later version.
37 *
38 * This program is distributed in the hope that it will be useful,
39 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 * GNU General Public License for more details.
42 *
43 * You should have received a copy of the GNU General Public License
44 * along with this program; if not, write to the Free Software
45 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
46 */
Arnd Bergmannbdb116c2021-02-01 10:01:32 +000047#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/slab.h>
49#include <linux/poll.h>
50#include <linux/fs.h>
51#include <linux/file.h>
52#include <linux/jhash.h>
53#include <linux/init.h>
54#include <linux/futex.h>
55#include <linux/mount.h>
56#include <linux/pagemap.h>
57#include <linux/syscalls.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070058#include <linux/signal.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040059#include <linux/export.h>
Andrey Mirkinfd5eea42007-10-16 23:30:13 -070060#include <linux/magic.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070061#include <linux/pid.h>
62#include <linux/nsproxy.h>
Kees Cookbdbb7762012-03-19 16:12:53 -070063#include <linux/ptrace.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060064#include <linux/sched/rt.h>
Zhang Yi13d60f42013-06-25 21:19:31 +080065#include <linux/hugetlb.h>
Colin Cross88c80042013-05-01 18:35:05 -070066#include <linux/freezer.h>
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -080067#include <linux/bootmem.h>
Davidlohr Buesoab51fba2015-06-29 23:26:02 -070068#include <linux/fault-inject.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070069
Jakub Jelinek4732efb2005-09-06 15:16:25 -070070#include <asm/futex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Peter Zijlstra1696a8b2013-10-31 18:18:19 +010072#include "locking/rtmutex_common.h"
Ingo Molnarc87e2832006-06-27 02:54:58 -070073
Thomas Gleixner99b60ce2014-01-12 15:31:24 -080074/*
Davidlohr Buesod7e8af12014-04-09 11:55:07 -070075 * READ this before attempting to hack on futexes!
76 *
77 * Basic futex operation and ordering guarantees
78 * =============================================
Thomas Gleixner99b60ce2014-01-12 15:31:24 -080079 *
80 * The waiter reads the futex value in user space and calls
81 * futex_wait(). This function computes the hash bucket and acquires
82 * the hash bucket lock. After that it reads the futex user space value
Davidlohr Buesob0c29f72014-01-12 15:31:25 -080083 * again and verifies that the data has not changed. If it has not changed
84 * it enqueues itself into the hash bucket, releases the hash bucket lock
85 * and schedules.
Thomas Gleixner99b60ce2014-01-12 15:31:24 -080086 *
87 * The waker side modifies the user space value of the futex and calls
Davidlohr Buesob0c29f72014-01-12 15:31:25 -080088 * futex_wake(). This function computes the hash bucket and acquires the
89 * hash bucket lock. Then it looks for waiters on that futex in the hash
90 * bucket and wakes them.
Thomas Gleixner99b60ce2014-01-12 15:31:24 -080091 *
Davidlohr Buesob0c29f72014-01-12 15:31:25 -080092 * In futex wake up scenarios where no tasks are blocked on a futex, taking
93 * the hb spinlock can be avoided and simply return. In order for this
94 * optimization to work, ordering guarantees must exist so that the waiter
95 * being added to the list is acknowledged when the list is concurrently being
96 * checked by the waker, avoiding scenarios like the following:
Thomas Gleixner99b60ce2014-01-12 15:31:24 -080097 *
98 * CPU 0 CPU 1
99 * val = *futex;
100 * sys_futex(WAIT, futex, val);
101 * futex_wait(futex, val);
102 * uval = *futex;
103 * *futex = newval;
104 * sys_futex(WAKE, futex);
105 * futex_wake(futex);
106 * if (queue_empty())
107 * return;
108 * if (uval == val)
109 * lock(hash_bucket(futex));
110 * queue();
111 * unlock(hash_bucket(futex));
112 * schedule();
113 *
114 * This would cause the waiter on CPU 0 to wait forever because it
115 * missed the transition of the user space value from val to newval
116 * and the waker did not find the waiter in the hash bucket queue.
Thomas Gleixner99b60ce2014-01-12 15:31:24 -0800117 *
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800118 * The correct serialization ensures that a waiter either observes
119 * the changed user space value before blocking or is woken by a
120 * concurrent waker:
121 *
122 * CPU 0 CPU 1
Thomas Gleixner99b60ce2014-01-12 15:31:24 -0800123 * val = *futex;
124 * sys_futex(WAIT, futex, val);
125 * futex_wait(futex, val);
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800126 *
Davidlohr Buesod7e8af12014-04-09 11:55:07 -0700127 * waiters++; (a)
Davidlohr Bueso8ad7b372016-02-09 11:15:13 -0800128 * smp_mb(); (A) <-- paired with -.
129 * |
130 * lock(hash_bucket(futex)); |
131 * |
132 * uval = *futex; |
133 * | *futex = newval;
134 * | sys_futex(WAKE, futex);
135 * | futex_wake(futex);
136 * |
137 * `--------> smp_mb(); (B)
Thomas Gleixner99b60ce2014-01-12 15:31:24 -0800138 * if (uval == val)
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800139 * queue();
Thomas Gleixner99b60ce2014-01-12 15:31:24 -0800140 * unlock(hash_bucket(futex));
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800141 * schedule(); if (waiters)
142 * lock(hash_bucket(futex));
Davidlohr Buesod7e8af12014-04-09 11:55:07 -0700143 * else wake_waiters(futex);
144 * waiters--; (b) unlock(hash_bucket(futex));
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800145 *
Davidlohr Buesod7e8af12014-04-09 11:55:07 -0700146 * Where (A) orders the waiters increment and the futex value read through
147 * atomic operations (see hb_waiters_inc) and where (B) orders the write
Davidlohr Bueso993b2ff2014-10-23 20:27:00 -0700148 * to futex and the waiters read -- this is done by the barriers for both
149 * shared and private futexes in get_futex_key_refs().
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800150 *
151 * This yields the following case (where X:=waiters, Y:=futex):
152 *
153 * X = Y = 0
154 *
155 * w[X]=1 w[Y]=1
156 * MB MB
157 * r[Y]=y r[X]=x
158 *
159 * Which guarantees that x==0 && y==0 is impossible; which translates back into
160 * the guarantee that we cannot both miss the futex variable change and the
161 * enqueue.
Davidlohr Buesod7e8af12014-04-09 11:55:07 -0700162 *
163 * Note that a new waiter is accounted for in (a) even when it is possible that
164 * the wait call can return error, in which case we backtrack from it in (b).
165 * Refer to the comment in queue_lock().
166 *
167 * Similarly, in order to account for waiters being requeued on another
168 * address we always increment the waiters for the destination bucket before
169 * acquiring the lock. It then decrements them again after releasing it -
170 * the code that actually moves the futex(es) between hash buckets (requeue_futex)
171 * will do the additional required waiter count housekeeping. This is done for
172 * double_lock_hb() and double_unlock_hb(), respectively.
Thomas Gleixner99b60ce2014-01-12 15:31:24 -0800173 */
174
Arnd Bergmannbdb116c2021-02-01 10:01:32 +0000175#ifdef CONFIG_HAVE_FUTEX_CMPXCHG
176#define futex_cmpxchg_enabled 1
177#else
178static int __read_mostly futex_cmpxchg_enabled;
Heiko Carstens03b8c7b2014-03-02 13:09:47 +0100179#endif
Thomas Gleixnera0c1e902008-02-23 15:23:57 -0800180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/*
Darren Hartb41277d2010-11-08 13:10:09 -0800182 * Futex flags used to encode options to functions and preserve them across
183 * restarts.
184 */
Thomas Gleixner784bdf32016-07-29 16:32:30 +0200185#ifdef CONFIG_MMU
186# define FLAGS_SHARED 0x01
187#else
188/*
189 * NOMMU does not have per process address space. Let the compiler optimize
190 * code away.
191 */
192# define FLAGS_SHARED 0x00
193#endif
Darren Hartb41277d2010-11-08 13:10:09 -0800194#define FLAGS_CLOCKRT 0x02
195#define FLAGS_HAS_TIMEOUT 0x04
196
197/*
Ingo Molnarc87e2832006-06-27 02:54:58 -0700198 * Priority Inheritance state:
199 */
200struct futex_pi_state {
201 /*
202 * list of 'owned' pi_state instances - these have to be
203 * cleaned up in do_exit() if the task exits prematurely:
204 */
205 struct list_head list;
206
207 /*
208 * The PI object:
209 */
210 struct rt_mutex pi_mutex;
211
212 struct task_struct *owner;
213 atomic_t refcount;
214
215 union futex_key key;
216};
217
Darren Hartd8d88fb2009-09-21 22:30:30 -0700218/**
219 * struct futex_q - The hashed futex queue entry, one per waiting task
Randy Dunlapfb62db22010-10-13 11:02:34 -0700220 * @list: priority-sorted list of tasks waiting on this futex
Darren Hartd8d88fb2009-09-21 22:30:30 -0700221 * @task: the task waiting on the futex
222 * @lock_ptr: the hash bucket lock
223 * @key: the key the futex is hashed on
224 * @pi_state: optional priority inheritance state
225 * @rt_waiter: rt_waiter storage for use with requeue_pi
226 * @requeue_pi_key: the requeue_pi target futex key
227 * @bitset: bitset for the optional bitmasked wakeup
228 *
229 * We use this hashed waitqueue, instead of a normal wait_queue_t, so
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 * we can wake only the relevant ones (hashed queues may be shared).
231 *
232 * A futex_q has a woken state, just like tasks have TASK_RUNNING.
Pierre Peifferec92d082007-05-09 02:35:00 -0700233 * It is considered woken when plist_node_empty(&q->list) || q->lock_ptr == 0.
Randy Dunlapfb62db22010-10-13 11:02:34 -0700234 * The order of wakeup is always to make the first condition true, then
Darren Hartd8d88fb2009-09-21 22:30:30 -0700235 * the second.
236 *
237 * PI futexes are typically woken before they are removed from the hash list via
238 * the rt_mutex code. See unqueue_me_pi().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 */
240struct futex_q {
Pierre Peifferec92d082007-05-09 02:35:00 -0700241 struct plist_node list;
Darren Hartd8d88fb2009-09-21 22:30:30 -0700242
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +0200243 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 spinlock_t *lock_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 union futex_key key;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700246 struct futex_pi_state *pi_state;
Darren Hart52400ba2009-04-03 13:40:49 -0700247 struct rt_mutex_waiter *rt_waiter;
Darren Hart84bc4af2009-08-13 17:36:53 -0700248 union futex_key *requeue_pi_key;
Thomas Gleixnercd689982008-02-01 17:45:14 +0100249 u32 bitset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250};
251
Darren Hart5bdb05f2010-11-08 13:40:28 -0800252static const struct futex_q futex_q_init = {
253 /* list gets initialized in queue_me()*/
254 .key = FUTEX_KEY_INIT,
255 .bitset = FUTEX_BITSET_MATCH_ANY
256};
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258/*
Darren Hartb2d09942009-03-12 00:55:37 -0700259 * Hash buckets are shared by all the futex_keys that hash to the same
260 * location. Each key may have multiple futex_q structures, one for each task
261 * waiting on a futex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 */
263struct futex_hash_bucket {
Linus Torvalds11d46162014-03-20 22:11:17 -0700264 atomic_t waiters;
Pierre Peifferec92d082007-05-09 02:35:00 -0700265 spinlock_t lock;
266 struct plist_head chain;
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -0800267} ____cacheline_aligned_in_smp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Rasmus Villemoesac742d32015-09-09 23:36:40 +0200269/*
270 * The base of the bucket array and its size are always used together
271 * (after initialization only in hash_futex()), so ensure that they
272 * reside in the same cacheline.
273 */
274static struct {
275 struct futex_hash_bucket *queues;
276 unsigned long hashsize;
277} __futex_data __read_mostly __aligned(2*sizeof(long));
278#define futex_queues (__futex_data.queues)
279#define futex_hashsize (__futex_data.hashsize)
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -0800280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Davidlohr Buesoab51fba2015-06-29 23:26:02 -0700282/*
283 * Fault injections for futexes.
284 */
285#ifdef CONFIG_FAIL_FUTEX
286
287static struct {
288 struct fault_attr attr;
289
Viresh Kumar621a5f72015-09-26 15:04:07 -0700290 bool ignore_private;
Davidlohr Buesoab51fba2015-06-29 23:26:02 -0700291} fail_futex = {
292 .attr = FAULT_ATTR_INITIALIZER,
Viresh Kumar621a5f72015-09-26 15:04:07 -0700293 .ignore_private = false,
Davidlohr Buesoab51fba2015-06-29 23:26:02 -0700294};
295
296static int __init setup_fail_futex(char *str)
297{
298 return setup_fault_attr(&fail_futex.attr, str);
299}
300__setup("fail_futex=", setup_fail_futex);
301
kbuild test robot5d285a72015-07-21 01:40:45 +0800302static bool should_fail_futex(bool fshared)
Davidlohr Buesoab51fba2015-06-29 23:26:02 -0700303{
304 if (fail_futex.ignore_private && !fshared)
305 return false;
306
307 return should_fail(&fail_futex.attr, 1);
308}
309
310#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
311
312static int __init fail_futex_debugfs(void)
313{
314 umode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
315 struct dentry *dir;
316
317 dir = fault_create_debugfs_attr("fail_futex", NULL,
318 &fail_futex.attr);
319 if (IS_ERR(dir))
320 return PTR_ERR(dir);
321
322 if (!debugfs_create_bool("ignore-private", mode, dir,
323 &fail_futex.ignore_private)) {
324 debugfs_remove_recursive(dir);
325 return -ENOMEM;
326 }
327
328 return 0;
329}
330
331late_initcall(fail_futex_debugfs);
332
333#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
334
335#else
336static inline bool should_fail_futex(bool fshared)
337{
338 return false;
339}
340#endif /* CONFIG_FAIL_FUTEX */
341
Thomas Gleixner25f319b2021-02-01 10:01:33 +0000342#ifdef CONFIG_COMPAT
343static void compat_exit_robust_list(struct task_struct *curr);
344#else
345static inline void compat_exit_robust_list(struct task_struct *curr) { }
346#endif
347
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800348static inline void futex_get_mm(union futex_key *key)
349{
350 atomic_inc(&key->private.mm->mm_count);
351 /*
352 * Ensure futex_get_mm() implies a full barrier such that
353 * get_futex_key() implies a full barrier. This is relied upon
Davidlohr Bueso8ad7b372016-02-09 11:15:13 -0800354 * as smp_mb(); (B), see the ordering comment above.
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800355 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100356 smp_mb__after_atomic();
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800357}
358
Linus Torvalds11d46162014-03-20 22:11:17 -0700359/*
360 * Reflects a new waiter being added to the waitqueue.
361 */
362static inline void hb_waiters_inc(struct futex_hash_bucket *hb)
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800363{
364#ifdef CONFIG_SMP
Linus Torvalds11d46162014-03-20 22:11:17 -0700365 atomic_inc(&hb->waiters);
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800366 /*
Linus Torvalds11d46162014-03-20 22:11:17 -0700367 * Full barrier (A), see the ordering comment above.
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800368 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100369 smp_mb__after_atomic();
Linus Torvalds11d46162014-03-20 22:11:17 -0700370#endif
371}
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800372
Linus Torvalds11d46162014-03-20 22:11:17 -0700373/*
374 * Reflects a waiter being removed from the waitqueue by wakeup
375 * paths.
376 */
377static inline void hb_waiters_dec(struct futex_hash_bucket *hb)
378{
379#ifdef CONFIG_SMP
380 atomic_dec(&hb->waiters);
381#endif
382}
383
384static inline int hb_waiters_pending(struct futex_hash_bucket *hb)
385{
386#ifdef CONFIG_SMP
387 return atomic_read(&hb->waiters);
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800388#else
Linus Torvalds11d46162014-03-20 22:11:17 -0700389 return 1;
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800390#endif
391}
392
Thomas Gleixnere8b61b32016-06-01 10:43:29 +0200393/**
394 * hash_futex - Return the hash bucket in the global hash
395 * @key: Pointer to the futex key for which the hash is calculated
396 *
397 * We hash on the keys returned from get_futex_key (see below) and return the
398 * corresponding hash bucket in the global hash.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 */
400static struct futex_hash_bucket *hash_futex(union futex_key *key)
401{
Thomas Gleixner95c53832020-03-08 19:07:17 +0100402 u32 hash = jhash2((u32 *)key, offsetof(typeof(*key), both.offset) / 4,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 key->both.offset);
Thomas Gleixner95c53832020-03-08 19:07:17 +0100404
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -0800405 return &futex_queues[hash & (futex_hashsize - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
Thomas Gleixnere8b61b32016-06-01 10:43:29 +0200408
409/**
410 * match_futex - Check whether two futex keys are equal
411 * @key1: Pointer to key1
412 * @key2: Pointer to key2
413 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 * Return 1 if two futex_keys are equal, 0 otherwise.
415 */
416static inline int match_futex(union futex_key *key1, union futex_key *key2)
417{
Darren Hart2bc87202009-10-14 10:12:39 -0700418 return (key1 && key2
419 && key1->both.word == key2->both.word
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 && key1->both.ptr == key2->both.ptr
421 && key1->both.offset == key2->both.offset);
422}
423
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200424/*
425 * Take a reference to the resource addressed by a key.
426 * Can be called while holding spinlocks.
427 *
428 */
429static void get_futex_key_refs(union futex_key *key)
430{
431 if (!key->both.ptr)
432 return;
433
Thomas Gleixner784bdf32016-07-29 16:32:30 +0200434 /*
435 * On MMU less systems futexes are always "private" as there is no per
436 * process address space. We need the smp wmb nevertheless - yes,
437 * arch/blackfin has MMU less SMP ...
438 */
439 if (!IS_ENABLED(CONFIG_MMU)) {
440 smp_mb(); /* explicit smp_mb(); (B) */
441 return;
442 }
443
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200444 switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
445 case FUT_OFF_INODE:
Peter Zijlstrafb099f3b2020-03-04 11:28:31 +0100446 smp_mb(); /* explicit smp_mb(); (B) */
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200447 break;
448 case FUT_OFF_MMSHARED:
Davidlohr Bueso8ad7b372016-02-09 11:15:13 -0800449 futex_get_mm(key); /* implies smp_mb(); (B) */
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200450 break;
Catalin Marinas76835b0e2014-10-17 17:38:49 +0100451 default:
Davidlohr Bueso993b2ff2014-10-23 20:27:00 -0700452 /*
453 * Private futexes do not hold reference on an inode or
454 * mm, therefore the only purpose of calling get_futex_key_refs
455 * is because we need the barrier for the lockless waiter check.
456 */
Davidlohr Bueso8ad7b372016-02-09 11:15:13 -0800457 smp_mb(); /* explicit smp_mb(); (B) */
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200458 }
459}
460
461/*
462 * Drop a reference to the resource addressed by a key.
Davidlohr Bueso993b2ff2014-10-23 20:27:00 -0700463 * The hash bucket spinlock must not be held. This is
464 * a no-op for private futexes, see comment in the get
465 * counterpart.
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200466 */
467static void drop_futex_key_refs(union futex_key *key)
468{
Darren Hart90621c42008-12-29 19:43:21 -0800469 if (!key->both.ptr) {
470 /* If we're here then we tried to put a key we failed to get */
471 WARN_ON_ONCE(1);
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200472 return;
Darren Hart90621c42008-12-29 19:43:21 -0800473 }
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200474
Thomas Gleixner784bdf32016-07-29 16:32:30 +0200475 if (!IS_ENABLED(CONFIG_MMU))
476 return;
477
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200478 switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
479 case FUT_OFF_INODE:
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200480 break;
481 case FUT_OFF_MMSHARED:
482 mmdrop(key->private.mm);
483 break;
484 }
485}
486
Peter Zijlstrafb099f3b2020-03-04 11:28:31 +0100487/*
488 * Generate a machine wide unique identifier for this inode.
489 *
490 * This relies on u64 not wrapping in the life-time of the machine; which with
491 * 1ns resolution means almost 585 years.
492 *
493 * This further relies on the fact that a well formed program will not unmap
494 * the file while it has a (shared) futex waiting on it. This mapping will have
495 * a file reference which pins the mount and inode.
496 *
497 * If for some reason an inode gets evicted and read back in again, it will get
498 * a new sequence number and will _NOT_ match, even though it is the exact same
499 * file.
500 *
501 * It is important that match_futex() will never have a false-positive, esp.
502 * for PI futexes that can mess up the state. The above argues that false-negatives
503 * are only possible for malformed programs.
504 */
505static u64 get_inode_sequence_number(struct inode *inode)
506{
507 static atomic64_t i_seq;
508 u64 old;
509
510 /* Does the inode already have a sequence number? */
511 old = atomic64_read(&inode->i_sequence);
512 if (likely(old))
513 return old;
514
515 for (;;) {
516 u64 new = atomic64_add_return(1, &i_seq);
517 if (WARN_ON_ONCE(!new))
518 continue;
519
520 old = atomic64_cmpxchg_relaxed(&inode->i_sequence, 0, new);
521 if (old)
522 return old;
523 return new;
524 }
525}
526
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700527/**
Darren Hartd96ee562009-09-21 22:30:22 -0700528 * get_futex_key() - Get parameters which are the keys for a futex
529 * @uaddr: virtual address of the futex
530 * @fshared: 0 for a PROCESS_PRIVATE futex, 1 for PROCESS_SHARED
531 * @key: address where result is stored.
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500532 * @rw: mapping needs to be read/write (values: VERIFY_READ,
533 * VERIFY_WRITE)
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700534 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -0800535 * Return: a negative error code or 0
536 *
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700537 * The key words are stored in *key on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 *
Peter Zijlstrafb099f3b2020-03-04 11:28:31 +0100539 * For shared mappings (when @fshared), the key is:
540 * ( inode->i_sequence, page->index, offset_within_page )
541 * [ also see get_inode_sequence_number() ]
542 *
543 * For private mappings (or when !@fshared), the key is:
544 * ( current->mm, address, 0 )
545 *
546 * This allows (cross process, where applicable) identification of the futex
547 * without keeping the page pinned for the duration of the FUTEX_WAIT.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 *
Darren Hartb2d09942009-03-12 00:55:37 -0700549 * lock_page() might sleep, the caller should not hold a spinlock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 */
Thomas Gleixner64d13042009-05-18 21:20:10 +0200551static int
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500552get_futex_key(u32 __user *uaddr, int fshared, union futex_key *key, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Ingo Molnare2970f22006-06-27 02:54:47 -0700554 unsigned long address = (unsigned long)uaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 struct mm_struct *mm = current->mm;
Mel Gorman077fa7a2016-06-08 14:25:22 +0100556 struct page *page, *tail;
Kirill A. Shutemov14d27ab2016-01-15 16:53:00 -0800557 struct address_space *mapping;
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500558 int err, ro = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 /*
561 * The futex address must be "naturally" aligned.
562 */
Ingo Molnare2970f22006-06-27 02:54:47 -0700563 key->both.offset = address % PAGE_SIZE;
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700564 if (unlikely((address % sizeof(u32)) != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return -EINVAL;
Ingo Molnare2970f22006-06-27 02:54:47 -0700566 address -= key->both.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Linus Torvalds5cdec2d2013-12-12 09:53:51 -0800568 if (unlikely(!access_ok(rw, uaddr, sizeof(u32))))
569 return -EFAULT;
570
Davidlohr Buesoab51fba2015-06-29 23:26:02 -0700571 if (unlikely(should_fail_futex(fshared)))
572 return -EFAULT;
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 /*
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700575 * PROCESS_PRIVATE futexes are fast.
576 * As the mm cannot disappear under us and the 'key' only needs
577 * virtual address, we dont even have to find the underlying vma.
578 * Note : We do have to check 'uaddr' is a valid user address,
579 * but access_ok() should be faster than find_vma()
580 */
581 if (!fshared) {
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700582 key->private.mm = mm;
583 key->private.address = address;
Davidlohr Bueso8ad7b372016-02-09 11:15:13 -0800584 get_futex_key_refs(key); /* implies smp_mb(); (B) */
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700585 return 0;
586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200588again:
Davidlohr Buesoab51fba2015-06-29 23:26:02 -0700589 /* Ignore any VERIFY_READ mapping (futex common case) */
590 if (unlikely(should_fail_futex(fshared)))
591 return -EFAULT;
592
KOSAKI Motohiro7485d0d2010-01-05 16:32:43 +0900593 err = get_user_pages_fast(address, 1, 1, &page);
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500594 /*
595 * If write access is not required (eg. FUTEX_WAIT), try
596 * and get read-only access.
597 */
598 if (err == -EFAULT && rw == VERIFY_READ) {
599 err = get_user_pages_fast(address, 1, 0, &page);
600 ro = 1;
601 }
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200602 if (err < 0)
603 return err;
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500604 else
605 err = 0;
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200606
Mel Gorman65d8fc72016-02-09 11:15:14 -0800607 /*
608 * The treatment of mapping from this point on is critical. The page
609 * lock protects many things but in this context the page lock
610 * stabilizes mapping, prevents inode freeing in the shared
611 * file-backed region case and guards against movement to swap cache.
612 *
613 * Strictly speaking the page lock is not needed in all cases being
614 * considered here and page lock forces unnecessarily serialization
615 * From this point on, mapping will be re-verified if necessary and
616 * page lock will be acquired only if it is unavoidable
Mel Gorman077fa7a2016-06-08 14:25:22 +0100617 *
618 * Mapping checks require the head page for any compound page so the
619 * head page and mapping is looked up now. For anonymous pages, it
620 * does not matter if the page splits in the future as the key is
621 * based on the address. For filesystem-backed pages, the tail is
622 * required as the index of the page determines the key. For
623 * base pages, there is no tail page and tail == page.
Mel Gorman65d8fc72016-02-09 11:15:14 -0800624 */
Mel Gorman077fa7a2016-06-08 14:25:22 +0100625 tail = page;
Mel Gorman65d8fc72016-02-09 11:15:14 -0800626 page = compound_head(page);
627 mapping = READ_ONCE(page->mapping);
628
Hugh Dickinse6780f72011-12-31 11:44:01 -0800629 /*
Kirill A. Shutemov14d27ab2016-01-15 16:53:00 -0800630 * If page->mapping is NULL, then it cannot be a PageAnon
Hugh Dickinse6780f72011-12-31 11:44:01 -0800631 * page; but it might be the ZERO_PAGE or in the gate area or
632 * in a special mapping (all cases which we are happy to fail);
633 * or it may have been a good file page when get_user_pages_fast
634 * found it, but truncated or holepunched or subjected to
635 * invalidate_complete_page2 before we got the page lock (also
636 * cases which we are happy to fail). And we hold a reference,
637 * so refcount care in invalidate_complete_page's remove_mapping
638 * prevents drop_caches from setting mapping to NULL beneath us.
639 *
640 * The case we do have to guard against is when memory pressure made
641 * shmem_writepage move it from filecache to swapcache beneath us:
Kirill A. Shutemov14d27ab2016-01-15 16:53:00 -0800642 * an unlikely race, but we do need to retry for page->mapping.
Hugh Dickinse6780f72011-12-31 11:44:01 -0800643 */
Mel Gorman65d8fc72016-02-09 11:15:14 -0800644 if (unlikely(!mapping)) {
645 int shmem_swizzled;
646
647 /*
648 * Page lock is required to identify which special case above
649 * applies. If this is really a shmem page then the page lock
650 * will prevent unexpected transitions.
651 */
652 lock_page(page);
653 shmem_swizzled = PageSwapCache(page) || page->mapping;
Kirill A. Shutemov14d27ab2016-01-15 16:53:00 -0800654 unlock_page(page);
655 put_page(page);
Mel Gorman65d8fc72016-02-09 11:15:14 -0800656
Hugh Dickinse6780f72011-12-31 11:44:01 -0800657 if (shmem_swizzled)
658 goto again;
Mel Gorman65d8fc72016-02-09 11:15:14 -0800659
Hugh Dickinse6780f72011-12-31 11:44:01 -0800660 return -EFAULT;
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 /*
664 * Private mappings are handled in a simple way.
665 *
Mel Gorman65d8fc72016-02-09 11:15:14 -0800666 * If the futex key is stored on an anonymous page, then the associated
667 * object is the mm which is implicitly pinned by the calling process.
668 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 * NOTE: When userspace waits on a MAP_SHARED mapping, even if
670 * it's a read-only handle, it's expected that futexes attach to
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200671 * the object not the particular process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 */
Kirill A. Shutemov14d27ab2016-01-15 16:53:00 -0800673 if (PageAnon(page)) {
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500674 /*
675 * A RO anonymous page will never change and thus doesn't make
676 * sense for futex operations.
677 */
Davidlohr Buesoab51fba2015-06-29 23:26:02 -0700678 if (unlikely(should_fail_futex(fshared)) || ro) {
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500679 err = -EFAULT;
680 goto out;
681 }
682
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200683 key->both.offset |= FUT_OFF_MMSHARED; /* ref taken on mm */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 key->private.mm = mm;
Ingo Molnare2970f22006-06-27 02:54:47 -0700685 key->private.address = address;
Mel Gorman65d8fc72016-02-09 11:15:14 -0800686
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200687 } else {
Mel Gorman65d8fc72016-02-09 11:15:14 -0800688 struct inode *inode;
689
690 /*
691 * The associated futex object in this case is the inode and
692 * the page->mapping must be traversed. Ordinarily this should
693 * be stabilised under page lock but it's not strictly
694 * necessary in this case as we just want to pin the inode, not
695 * update the radix tree or anything like that.
696 *
697 * The RCU read lock is taken as the inode is finally freed
698 * under RCU. If the mapping still matches expectations then the
699 * mapping->host can be safely accessed as being a valid inode.
700 */
701 rcu_read_lock();
702
703 if (READ_ONCE(page->mapping) != mapping) {
704 rcu_read_unlock();
705 put_page(page);
706
707 goto again;
708 }
709
710 inode = READ_ONCE(mapping->host);
711 if (!inode) {
712 rcu_read_unlock();
713 put_page(page);
714
715 goto again;
716 }
717
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200718 key->both.offset |= FUT_OFF_INODE; /* inode-based key */
Peter Zijlstrafb099f3b2020-03-04 11:28:31 +0100719 key->shared.i_seq = get_inode_sequence_number(inode);
Mel Gorman077fa7a2016-06-08 14:25:22 +0100720 key->shared.pgoff = basepage_index(tail);
Mel Gorman65d8fc72016-02-09 11:15:14 -0800721 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
723
Peter Zijlstrafb099f3b2020-03-04 11:28:31 +0100724 get_futex_key_refs(key); /* implies smp_mb(); (B) */
725
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500726out:
Kirill A. Shutemov14d27ab2016-01-15 16:53:00 -0800727 put_page(page);
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500728 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
Thomas Gleixnerae791a22010-11-10 13:30:36 +0100731static inline void put_futex_key(union futex_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200733 drop_futex_key_refs(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
735
Darren Hartd96ee562009-09-21 22:30:22 -0700736/**
737 * fault_in_user_writeable() - Fault in user address and verify RW access
Thomas Gleixnerd0725992009-06-11 23:15:43 +0200738 * @uaddr: pointer to faulting user space address
739 *
740 * Slow path to fixup the fault we just took in the atomic write
741 * access to @uaddr.
742 *
Randy Dunlapfb62db22010-10-13 11:02:34 -0700743 * We have no generic implementation of a non-destructive write to the
Thomas Gleixnerd0725992009-06-11 23:15:43 +0200744 * user address. We know that we faulted in the atomic pagefault
745 * disabled section so we can as well avoid the #PF overhead by
746 * calling get_user_pages() right away.
747 */
748static int fault_in_user_writeable(u32 __user *uaddr)
749{
Andi Kleen722d0172009-12-08 13:19:42 +0100750 struct mm_struct *mm = current->mm;
751 int ret;
752
753 down_read(&mm->mmap_sem);
Benjamin Herrenschmidt2efaca92011-07-25 17:12:32 -0700754 ret = fixup_user_fault(current, mm, (unsigned long)uaddr,
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800755 FAULT_FLAG_WRITE, NULL);
Andi Kleen722d0172009-12-08 13:19:42 +0100756 up_read(&mm->mmap_sem);
757
Thomas Gleixnerd0725992009-06-11 23:15:43 +0200758 return ret < 0 ? ret : 0;
759}
760
Darren Hart4b1c4862009-04-03 13:39:42 -0700761/**
762 * futex_top_waiter() - Return the highest priority waiter on a futex
Darren Hartd96ee562009-09-21 22:30:22 -0700763 * @hb: the hash bucket the futex_q's reside in
764 * @key: the futex key (to distinguish it from other futex futex_q's)
Darren Hart4b1c4862009-04-03 13:39:42 -0700765 *
766 * Must be called with the hb lock held.
767 */
768static struct futex_q *futex_top_waiter(struct futex_hash_bucket *hb,
769 union futex_key *key)
770{
771 struct futex_q *this;
772
773 plist_for_each_entry(this, &hb->chain, list) {
774 if (match_futex(&this->key, key))
775 return this;
776 }
777 return NULL;
778}
779
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800780static int cmpxchg_futex_value_locked(u32 *curval, u32 __user *uaddr,
781 u32 uval, u32 newval)
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700782{
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800783 int ret;
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700784
785 pagefault_disable();
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800786 ret = futex_atomic_cmpxchg_inatomic(curval, uaddr, uval, newval);
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700787 pagefault_enable();
788
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800789 return ret;
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700790}
791
792static int get_futex_value_locked(u32 *dest, u32 __user *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
794 int ret;
795
Peter Zijlstraa8663742006-12-06 20:32:20 -0800796 pagefault_disable();
Linus Torvaldsbd28b142016-05-22 17:21:27 -0700797 ret = __get_user(*dest, from);
Peter Zijlstraa8663742006-12-06 20:32:20 -0800798 pagefault_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 return ret ? -EFAULT : 0;
801}
802
Ingo Molnarc87e2832006-06-27 02:54:58 -0700803
804/*
805 * PI code:
806 */
807static int refill_pi_state_cache(void)
808{
809 struct futex_pi_state *pi_state;
810
811 if (likely(current->pi_state_cache))
812 return 0;
813
Burman Yan4668edc2006-12-06 20:38:51 -0800814 pi_state = kzalloc(sizeof(*pi_state), GFP_KERNEL);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700815
816 if (!pi_state)
817 return -ENOMEM;
818
Ingo Molnarc87e2832006-06-27 02:54:58 -0700819 INIT_LIST_HEAD(&pi_state->list);
820 /* pi_mutex gets initialized later */
821 pi_state->owner = NULL;
822 atomic_set(&pi_state->refcount, 1);
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200823 pi_state->key = FUTEX_KEY_INIT;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700824
825 current->pi_state_cache = pi_state;
826
827 return 0;
828}
829
Ben Hutchings921a7e32021-03-01 18:31:22 +0100830static struct futex_pi_state *alloc_pi_state(void)
Ingo Molnarc87e2832006-06-27 02:54:58 -0700831{
832 struct futex_pi_state *pi_state = current->pi_state_cache;
833
834 WARN_ON(!pi_state);
835 current->pi_state_cache = NULL;
836
837 return pi_state;
838}
839
Thomas Gleixner76bc0ec2021-02-03 13:45:35 +0000840static void pi_state_update_owner(struct futex_pi_state *pi_state,
841 struct task_struct *new_owner)
842{
843 struct task_struct *old_owner = pi_state->owner;
844
845 lockdep_assert_held(&pi_state->pi_mutex.wait_lock);
846
847 if (old_owner) {
848 raw_spin_lock(&old_owner->pi_lock);
849 WARN_ON(list_empty(&pi_state->list));
850 list_del_init(&pi_state->list);
851 raw_spin_unlock(&old_owner->pi_lock);
852 }
853
854 if (new_owner) {
855 raw_spin_lock(&new_owner->pi_lock);
856 WARN_ON(!list_empty(&pi_state->list));
857 list_add(&pi_state->list, &new_owner->pi_state_list);
858 pi_state->owner = new_owner;
859 raw_spin_unlock(&new_owner->pi_lock);
860 }
861}
862
Ben Hutchings921a7e32021-03-01 18:31:22 +0100863static void get_pi_state(struct futex_pi_state *pi_state)
864{
865 WARN_ON_ONCE(!atomic_inc_not_zero(&pi_state->refcount));
866}
867
Brian Silverman30a6b802014-10-25 20:20:37 -0400868/*
Thomas Gleixner29e9ee52015-12-19 20:07:39 +0000869 * Drops a reference to the pi_state object and frees or caches it
870 * when the last reference is gone.
Brian Silverman30a6b802014-10-25 20:20:37 -0400871 */
Thomas Gleixner29e9ee52015-12-19 20:07:39 +0000872static void put_pi_state(struct futex_pi_state *pi_state)
Ingo Molnarc87e2832006-06-27 02:54:58 -0700873{
Brian Silverman30a6b802014-10-25 20:20:37 -0400874 if (!pi_state)
875 return;
876
Ingo Molnarc87e2832006-06-27 02:54:58 -0700877 if (!atomic_dec_and_test(&pi_state->refcount))
878 return;
879
880 /*
881 * If pi_state->owner is NULL, the owner is most probably dying
882 * and has cleaned up the pi_state already
883 */
884 if (pi_state->owner) {
Ben Hutchings69015302021-03-01 18:32:02 +0100885 unsigned long flags;
886
887 raw_spin_lock_irqsave(&pi_state->pi_mutex.wait_lock, flags);
Thomas Gleixner7d455bb2021-02-03 13:45:37 +0000888 pi_state_update_owner(pi_state, NULL);
Thomas Gleixner285b6242021-02-03 13:45:36 +0000889 rt_mutex_proxy_unlock(&pi_state->pi_mutex);
Ben Hutchings69015302021-03-01 18:32:02 +0100890 raw_spin_unlock_irqrestore(&pi_state->pi_mutex.wait_lock, flags);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700891 }
892
Ben Hutchings9787adc2021-03-01 18:31:48 +0100893 if (current->pi_state_cache) {
Ingo Molnarc87e2832006-06-27 02:54:58 -0700894 kfree(pi_state);
Ben Hutchings9787adc2021-03-01 18:31:48 +0100895 } else {
Ingo Molnarc87e2832006-06-27 02:54:58 -0700896 /*
897 * pi_state->list is already empty.
898 * clear pi_state->owner.
899 * refcount is at 0 - put it back to 1.
900 */
901 pi_state->owner = NULL;
902 atomic_set(&pi_state->refcount, 1);
903 current->pi_state_cache = pi_state;
904 }
905}
906
907/*
908 * Look up the task based on what TID userspace gave us.
909 * We dont trust it.
910 */
Ben Hutchings921a7e32021-03-01 18:31:22 +0100911static struct task_struct *futex_find_get_task(pid_t pid)
Ingo Molnarc87e2832006-06-27 02:54:58 -0700912{
913 struct task_struct *p;
914
Oleg Nesterovd359b542006-09-29 02:00:55 -0700915 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700916 p = find_task_by_vpid(pid);
Michal Hocko7a0ea092010-06-30 09:51:19 +0200917 if (p)
918 get_task_struct(p);
Thomas Gleixnera06381f2007-06-23 11:48:40 +0200919
Oleg Nesterovd359b542006-09-29 02:00:55 -0700920 rcu_read_unlock();
Ingo Molnarc87e2832006-06-27 02:54:58 -0700921
922 return p;
923}
924
925/*
926 * This task is holding PI mutexes at exit time => bad.
927 * Kernel cleans up PI-state, but userspace is likely hosed.
928 * (Robust-futex cleanup is separate and might save the day for userspace.)
929 */
Thomas Gleixner25f319b2021-02-01 10:01:33 +0000930static void exit_pi_state_list(struct task_struct *curr)
Ingo Molnarc87e2832006-06-27 02:54:58 -0700931{
Ingo Molnarc87e2832006-06-27 02:54:58 -0700932 struct list_head *next, *head = &curr->pi_state_list;
933 struct futex_pi_state *pi_state;
Ingo Molnar627371d2006-07-29 05:16:20 +0200934 struct futex_hash_bucket *hb;
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200935 union futex_key key = FUTEX_KEY_INIT;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700936
Thomas Gleixnera0c1e902008-02-23 15:23:57 -0800937 if (!futex_cmpxchg_enabled)
938 return;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700939 /*
940 * We are a ZOMBIE and nobody can enqueue itself on
941 * pi_state_list anymore, but we have to be careful
Ingo Molnar627371d2006-07-29 05:16:20 +0200942 * versus waiters unqueueing themselves:
Ingo Molnarc87e2832006-06-27 02:54:58 -0700943 */
Thomas Gleixner1d615482009-11-17 14:54:03 +0100944 raw_spin_lock_irq(&curr->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700945 while (!list_empty(head)) {
Ingo Molnarc87e2832006-06-27 02:54:58 -0700946 next = head->next;
947 pi_state = list_entry(next, struct futex_pi_state, list);
948 key = pi_state->key;
Ingo Molnar627371d2006-07-29 05:16:20 +0200949 hb = hash_futex(&key);
Ben Hutchingsda1b9ad2021-03-01 18:31:55 +0100950
951 /*
952 * We can race against put_pi_state() removing itself from the
953 * list (a waiter going away). put_pi_state() will first
954 * decrement the reference count and then modify the list, so
955 * its possible to see the list entry but fail this reference
956 * acquire.
957 *
958 * In that case; drop the locks to let put_pi_state() make
959 * progress and retry the loop.
960 */
961 if (!atomic_inc_not_zero(&pi_state->refcount)) {
962 raw_spin_unlock_irq(&curr->pi_lock);
963 cpu_relax();
964 raw_spin_lock_irq(&curr->pi_lock);
965 continue;
966 }
Thomas Gleixner1d615482009-11-17 14:54:03 +0100967 raw_spin_unlock_irq(&curr->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700968
Ingo Molnarc87e2832006-06-27 02:54:58 -0700969 spin_lock(&hb->lock);
Ben Hutchings9787adc2021-03-01 18:31:48 +0100970 raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
971 raw_spin_lock(&curr->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +0200972 /*
973 * We dropped the pi-lock, so re-check whether this
974 * task still owns the PI-state:
975 */
Ingo Molnarc87e2832006-06-27 02:54:58 -0700976 if (head->next != next) {
Ben Hutchingsda1b9ad2021-03-01 18:31:55 +0100977 /* retain curr->pi_lock for the loop invariant */
Ben Hutchings9787adc2021-03-01 18:31:48 +0100978 raw_spin_unlock(&pi_state->pi_mutex.wait_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700979 spin_unlock(&hb->lock);
Ben Hutchingsda1b9ad2021-03-01 18:31:55 +0100980 put_pi_state(pi_state);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700981 continue;
982 }
983
Ingo Molnarc87e2832006-06-27 02:54:58 -0700984 WARN_ON(pi_state->owner != curr);
Ingo Molnar627371d2006-07-29 05:16:20 +0200985 WARN_ON(list_empty(&pi_state->list));
986 list_del_init(&pi_state->list);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700987 pi_state->owner = NULL;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700988
Ben Hutchingsda1b9ad2021-03-01 18:31:55 +0100989 raw_spin_unlock(&curr->pi_lock);
Ben Hutchings9787adc2021-03-01 18:31:48 +0100990 raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700991 spin_unlock(&hb->lock);
992
Ben Hutchings312d9d62021-03-01 18:31:30 +0100993 rt_mutex_futex_unlock(&pi_state->pi_mutex);
994 put_pi_state(pi_state);
995
Thomas Gleixner1d615482009-11-17 14:54:03 +0100996 raw_spin_lock_irq(&curr->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700997 }
Thomas Gleixner1d615482009-11-17 14:54:03 +0100998 raw_spin_unlock_irq(&curr->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700999}
1000
Thomas Gleixner54a21782014-06-03 12:27:08 +00001001/*
1002 * We need to check the following states:
1003 *
1004 * Waiter | pi_state | pi->owner | uTID | uODIED | ?
1005 *
1006 * [1] NULL | --- | --- | 0 | 0/1 | Valid
1007 * [2] NULL | --- | --- | >0 | 0/1 | Valid
1008 *
1009 * [3] Found | NULL | -- | Any | 0/1 | Invalid
1010 *
1011 * [4] Found | Found | NULL | 0 | 1 | Valid
1012 * [5] Found | Found | NULL | >0 | 1 | Invalid
1013 *
1014 * [6] Found | Found | task | 0 | 1 | Valid
1015 *
1016 * [7] Found | Found | NULL | Any | 0 | Invalid
1017 *
1018 * [8] Found | Found | task | ==taskTID | 0/1 | Valid
1019 * [9] Found | Found | task | 0 | 0 | Invalid
1020 * [10] Found | Found | task | !=taskTID | 0/1 | Invalid
1021 *
1022 * [1] Indicates that the kernel can acquire the futex atomically. We
1023 * came came here due to a stale FUTEX_WAITERS/FUTEX_OWNER_DIED bit.
1024 *
1025 * [2] Valid, if TID does not belong to a kernel thread. If no matching
1026 * thread is found then it indicates that the owner TID has died.
1027 *
1028 * [3] Invalid. The waiter is queued on a non PI futex
1029 *
1030 * [4] Valid state after exit_robust_list(), which sets the user space
1031 * value to FUTEX_WAITERS | FUTEX_OWNER_DIED.
1032 *
1033 * [5] The user space value got manipulated between exit_robust_list()
1034 * and exit_pi_state_list()
1035 *
1036 * [6] Valid state after exit_pi_state_list() which sets the new owner in
1037 * the pi_state but cannot access the user space value.
1038 *
1039 * [7] pi_state->owner can only be NULL when the OWNER_DIED bit is set.
1040 *
1041 * [8] Owner and user space value match
1042 *
1043 * [9] There is no transient state which sets the user space TID to 0
1044 * except exit_robust_list(), but this is indicated by the
1045 * FUTEX_OWNER_DIED bit. See [4]
1046 *
1047 * [10] There is no transient state which leaves owner and user space
Thomas Gleixnerb960d9a2021-02-03 13:45:39 +00001048 * TID out of sync. Except one error case where the kernel is denied
1049 * write access to the user address, see fixup_pi_state_owner().
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00001050 *
1051 *
1052 * Serialization and lifetime rules:
1053 *
1054 * hb->lock:
1055 *
1056 * hb -> futex_q, relation
1057 * futex_q -> pi_state, relation
1058 *
1059 * (cannot be raw because hb can contain arbitrary amount
1060 * of futex_q's)
1061 *
1062 * pi_mutex->wait_lock:
1063 *
1064 * {uval, pi_state}
1065 *
1066 * (and pi_mutex 'obviously')
1067 *
1068 * p->pi_lock:
1069 *
1070 * p->pi_state_list -> pi_state->list, relation
1071 *
1072 * pi_state->refcount:
1073 *
1074 * pi_state lifetime
1075 *
1076 *
1077 * Lock order:
1078 *
1079 * hb->lock
1080 * pi_mutex->wait_lock
1081 * p->pi_lock
1082 *
Thomas Gleixner54a21782014-06-03 12:27:08 +00001083 */
Thomas Gleixnere60cbc52014-06-11 20:45:39 +00001084
1085/*
1086 * Validate that the existing waiter has a pi_state and sanity check
1087 * the pi_state against the user space value. If correct, attach to
1088 * it.
1089 */
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00001090static int attach_to_pi_state(u32 __user *uaddr, u32 uval,
1091 struct futex_pi_state *pi_state,
Thomas Gleixnere60cbc52014-06-11 20:45:39 +00001092 struct futex_pi_state **ps)
1093{
1094 pid_t pid = uval & FUTEX_TID_MASK;
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00001095 int ret, uval2;
Thomas Gleixnere60cbc52014-06-11 20:45:39 +00001096
1097 /*
1098 * Userspace might have messed up non-PI and PI futexes [3]
1099 */
1100 if (unlikely(!pi_state))
1101 return -EINVAL;
1102
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00001103 /*
1104 * We get here with hb->lock held, and having found a
1105 * futex_top_waiter(). This means that futex_lock_pi() of said futex_q
1106 * has dropped the hb->lock in between queue_me() and unqueue_me_pi(),
1107 * which in turn means that futex_lock_pi() still has a reference on
1108 * our pi_state.
Ben Hutchings312d9d62021-03-01 18:31:30 +01001109 *
1110 * The waiter holding a reference on @pi_state also protects against
1111 * the unlocked put_pi_state() in futex_unlock_pi(), futex_lock_pi()
1112 * and futex_wait_requeue_pi() as it cannot go to 0 and consequently
1113 * free pi_state before we can take a reference ourselves.
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00001114 */
Thomas Gleixnere60cbc52014-06-11 20:45:39 +00001115 WARN_ON(!atomic_read(&pi_state->refcount));
1116
1117 /*
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00001118 * Now that we have a pi_state, we can acquire wait_lock
1119 * and do the state validation.
1120 */
1121 raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
1122
1123 /*
1124 * Since {uval, pi_state} is serialized by wait_lock, and our current
1125 * uval was read without holding it, it can have changed. Verify it
1126 * still is what we expect it to be, otherwise retry the entire
1127 * operation.
1128 */
1129 if (get_futex_value_locked(&uval2, uaddr))
1130 goto out_efault;
1131
1132 if (uval != uval2)
1133 goto out_eagain;
1134
1135 /*
Thomas Gleixnere60cbc52014-06-11 20:45:39 +00001136 * Handle the owner died case:
1137 */
1138 if (uval & FUTEX_OWNER_DIED) {
1139 /*
1140 * exit_pi_state_list sets owner to NULL and wakes the
1141 * topmost waiter. The task which acquires the
1142 * pi_state->rt_mutex will fixup owner.
1143 */
1144 if (!pi_state->owner) {
1145 /*
1146 * No pi state owner, but the user space TID
1147 * is not 0. Inconsistent state. [5]
1148 */
1149 if (pid)
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00001150 goto out_einval;
Thomas Gleixnere60cbc52014-06-11 20:45:39 +00001151 /*
1152 * Take a ref on the state and return success. [4]
1153 */
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00001154 goto out_attach;
Thomas Gleixnere60cbc52014-06-11 20:45:39 +00001155 }
1156
1157 /*
1158 * If TID is 0, then either the dying owner has not
1159 * yet executed exit_pi_state_list() or some waiter
1160 * acquired the rtmutex in the pi state, but did not
1161 * yet fixup the TID in user space.
1162 *
1163 * Take a ref on the state and return success. [6]
1164 */
1165 if (!pid)
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00001166 goto out_attach;
Thomas Gleixnere60cbc52014-06-11 20:45:39 +00001167 } else {
1168 /*
1169 * If the owner died bit is not set, then the pi_state
1170 * must have an owner. [7]
1171 */
1172 if (!pi_state->owner)
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00001173 goto out_einval;
Thomas Gleixnere60cbc52014-06-11 20:45:39 +00001174 }
1175
1176 /*
1177 * Bail out if user space manipulated the futex value. If pi
1178 * state exists then the owner TID must be the same as the
1179 * user space TID. [9/10]
1180 */
1181 if (pid != task_pid_vnr(pi_state->owner))
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00001182 goto out_einval;
1183
1184out_attach:
Ben Hutchings921a7e32021-03-01 18:31:22 +01001185 get_pi_state(pi_state);
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00001186 raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
Thomas Gleixnere60cbc52014-06-11 20:45:39 +00001187 *ps = pi_state;
1188 return 0;
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00001189
1190out_einval:
1191 ret = -EINVAL;
1192 goto out_error;
1193
1194out_eagain:
1195 ret = -EAGAIN;
1196 goto out_error;
1197
1198out_efault:
1199 ret = -EFAULT;
1200 goto out_error;
1201
1202out_error:
1203 raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
1204 return ret;
Thomas Gleixnere60cbc52014-06-11 20:45:39 +00001205}
1206
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001207/**
1208 * wait_for_owner_exiting - Block until the owner has exited
1209 * @exiting: Pointer to the exiting task
1210 *
1211 * Caller must hold a refcount on @exiting.
1212 */
1213static void wait_for_owner_exiting(int ret, struct task_struct *exiting)
1214{
1215 if (ret != -EBUSY) {
1216 WARN_ON_ONCE(exiting);
1217 return;
1218 }
1219
1220 if (WARN_ON_ONCE(ret == -EBUSY && !exiting))
1221 return;
1222
1223 mutex_lock(&exiting->futex_exit_mutex);
1224 /*
1225 * No point in doing state checking here. If the waiter got here
1226 * while the task was in exec()->exec_futex_release() then it can
1227 * have any FUTEX_STATE_* value when the waiter has acquired the
1228 * mutex. OK, if running, EXITING or DEAD if it reached exit()
1229 * already. Highly unlikely and not a problem. Just one more round
1230 * through the futex maze.
1231 */
1232 mutex_unlock(&exiting->futex_exit_mutex);
1233
1234 put_task_struct(exiting);
1235}
1236
Thomas Gleixner9c3f3982021-02-11 09:27:00 +00001237static int handle_exit_race(u32 __user *uaddr, u32 uval,
1238 struct task_struct *tsk)
1239{
1240 u32 uval2;
1241
1242 /*
Thomas Gleixner91509e82021-02-24 18:09:23 +08001243 * If the futex exit state is not yet FUTEX_STATE_DEAD, tell the
1244 * caller that the alleged owner is busy.
Thomas Gleixner9c3f3982021-02-11 09:27:00 +00001245 */
1246 if (tsk && tsk->futex_state != FUTEX_STATE_DEAD)
Thomas Gleixner91509e82021-02-24 18:09:23 +08001247 return -EBUSY;
Thomas Gleixner9c3f3982021-02-11 09:27:00 +00001248
1249 /*
1250 * Reread the user space value to handle the following situation:
1251 *
1252 * CPU0 CPU1
1253 *
1254 * sys_exit() sys_futex()
1255 * do_exit() futex_lock_pi()
1256 * futex_lock_pi_atomic()
1257 * exit_signals(tsk) No waiters:
1258 * tsk->flags |= PF_EXITING; *uaddr == 0x00000PID
1259 * mm_release(tsk) Set waiter bit
1260 * exit_robust_list(tsk) { *uaddr = 0x80000PID;
1261 * Set owner died attach_to_pi_owner() {
1262 * *uaddr = 0xC0000000; tsk = get_task(PID);
1263 * } if (!tsk->flags & PF_EXITING) {
1264 * ... attach();
1265 * tsk->futex_state = } else {
1266 * FUTEX_STATE_DEAD; if (tsk->futex_state !=
1267 * FUTEX_STATE_DEAD)
1268 * return -EAGAIN;
1269 * return -ESRCH; <--- FAIL
1270 * }
1271 *
1272 * Returning ESRCH unconditionally is wrong here because the
1273 * user space value has been changed by the exiting task.
1274 *
1275 * The same logic applies to the case where the exiting task is
1276 * already gone.
1277 */
1278 if (get_futex_value_locked(&uval2, uaddr))
1279 return -EFAULT;
1280
1281 /* If the user space value has changed, try again. */
1282 if (uval2 != uval)
1283 return -EAGAIN;
1284
1285 /*
1286 * The exiting task did not have a robust list, the robust list was
1287 * corrupted or the user space value in *uaddr is simply bogus.
1288 * Give up and tell user space.
1289 */
1290 return -ESRCH;
1291}
1292
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001293/*
1294 * Lookup the task for the TID provided from user space and attach to
1295 * it after doing proper sanity checks.
1296 */
Thomas Gleixner9c3f3982021-02-11 09:27:00 +00001297static int attach_to_pi_owner(u32 __user *uaddr, u32 uval, union futex_key *key,
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001298 struct futex_pi_state **ps,
1299 struct task_struct **exiting)
Ingo Molnarc87e2832006-06-27 02:54:58 -07001300{
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001301 pid_t pid = uval & FUTEX_TID_MASK;
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001302 struct futex_pi_state *pi_state;
1303 struct task_struct *p;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001304
1305 /*
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001306 * We are the first waiter - try to look up the real owner and attach
Thomas Gleixner54a21782014-06-03 12:27:08 +00001307 * the new pi_state to it, but bail out when TID = 0 [1]
Thomas Gleixner9c3f3982021-02-11 09:27:00 +00001308 *
1309 * The !pid check is paranoid. None of the call sites should end up
1310 * with pid == 0, but better safe than sorry. Let the caller retry
Ingo Molnarc87e2832006-06-27 02:54:58 -07001311 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001312 if (!pid)
Thomas Gleixner9c3f3982021-02-11 09:27:00 +00001313 return -EAGAIN;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001314 p = futex_find_get_task(pid);
Michal Hocko7a0ea092010-06-30 09:51:19 +02001315 if (!p)
Thomas Gleixner9c3f3982021-02-11 09:27:00 +00001316 return handle_exit_race(uaddr, uval, NULL);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001317
Oleg Nesterova2129462015-02-02 15:05:36 +01001318 if (unlikely(p->flags & PF_KTHREAD)) {
Thomas Gleixnerf0d71b32014-05-12 20:45:35 +00001319 put_task_struct(p);
1320 return -EPERM;
1321 }
1322
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001323 /*
Thomas Gleixner2c116892021-02-01 10:01:34 +00001324 * We need to look at the task state to figure out, whether the
1325 * task is exiting. To protect against the change of the task state
1326 * in futex_exit_release(), we do this protected by p->pi_lock:
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001327 */
Thomas Gleixner1d615482009-11-17 14:54:03 +01001328 raw_spin_lock_irq(&p->pi_lock);
Thomas Gleixner2c116892021-02-01 10:01:34 +00001329 if (unlikely(p->futex_state != FUTEX_STATE_OK)) {
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001330 /*
Thomas Gleixner2c116892021-02-01 10:01:34 +00001331 * The task is on the way out. When the futex state is
1332 * FUTEX_STATE_DEAD, we know that the task has finished
1333 * the cleanup:
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001334 */
Thomas Gleixner9c3f3982021-02-11 09:27:00 +00001335 int ret = handle_exit_race(uaddr, uval, p);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001336
Thomas Gleixner1d615482009-11-17 14:54:03 +01001337 raw_spin_unlock_irq(&p->pi_lock);
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001338 /*
1339 * If the owner task is between FUTEX_STATE_EXITING and
1340 * FUTEX_STATE_DEAD then store the task pointer and keep
1341 * the reference on the task struct. The calling code will
1342 * drop all locks, wait for the task to reach
1343 * FUTEX_STATE_DEAD and then drop the refcount. This is
1344 * required to prevent a live lock when the current task
1345 * preempted the exiting task between the two states.
1346 */
1347 if (ret == -EBUSY)
1348 *exiting = p;
1349 else
1350 put_task_struct(p);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001351 return ret;
1352 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001353
Thomas Gleixner54a21782014-06-03 12:27:08 +00001354 /*
1355 * No existing pi state. First waiter. [2]
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00001356 *
1357 * This creates pi_state, we have hb->lock held, this means nothing can
1358 * observe this state, wait_lock is irrelevant.
Thomas Gleixner54a21782014-06-03 12:27:08 +00001359 */
Ingo Molnarc87e2832006-06-27 02:54:58 -07001360 pi_state = alloc_pi_state();
1361
1362 /*
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001363 * Initialize the pi_mutex in locked state and make @p
Ingo Molnarc87e2832006-06-27 02:54:58 -07001364 * the owner of it:
1365 */
1366 rt_mutex_init_proxy_locked(&pi_state->pi_mutex, p);
1367
1368 /* Store the key for possible exit cleanups: */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001369 pi_state->key = *key;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001370
Ingo Molnar627371d2006-07-29 05:16:20 +02001371 WARN_ON(!list_empty(&pi_state->list));
Ingo Molnarc87e2832006-06-27 02:54:58 -07001372 list_add(&pi_state->list, &p->pi_state_list);
Ben Hutchings9787adc2021-03-01 18:31:48 +01001373 /*
1374 * Assignment without holding pi_state->pi_mutex.wait_lock is safe
1375 * because there is no concurrency as the object is not published yet.
1376 */
Ingo Molnarc87e2832006-06-27 02:54:58 -07001377 pi_state->owner = p;
Thomas Gleixner1d615482009-11-17 14:54:03 +01001378 raw_spin_unlock_irq(&p->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001379
1380 put_task_struct(p);
1381
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001382 *ps = pi_state;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001383
1384 return 0;
1385}
1386
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00001387static int lookup_pi_state(u32 __user *uaddr, u32 uval,
1388 struct futex_hash_bucket *hb,
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001389 union futex_key *key, struct futex_pi_state **ps,
1390 struct task_struct **exiting)
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001391{
Ben Hutchingsbfefc9e2021-03-01 18:30:39 +01001392 struct futex_q *top_waiter = futex_top_waiter(hb, key);
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001393
1394 /*
1395 * If there is a waiter on that futex, validate it and
1396 * attach to the pi_state when the validation succeeds.
1397 */
Ben Hutchingsbfefc9e2021-03-01 18:30:39 +01001398 if (top_waiter)
1399 return attach_to_pi_state(uaddr, uval, top_waiter->pi_state, ps);
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001400
1401 /*
1402 * We are the first waiter - try to look up the owner based on
1403 * @uval and attach to it.
1404 */
Thomas Gleixner9c3f3982021-02-11 09:27:00 +00001405 return attach_to_pi_owner(uaddr, uval, key, ps, exiting);
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001406}
1407
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001408static int lock_pi_update_atomic(u32 __user *uaddr, u32 uval, u32 newval)
1409{
Will Deacon8682c2e2021-03-28 22:42:44 +02001410 int err;
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001411 u32 uninitialized_var(curval);
1412
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001413 if (unlikely(should_fail_futex(true)))
1414 return -EFAULT;
1415
Will Deacon8682c2e2021-03-28 22:42:44 +02001416 err = cmpxchg_futex_value_locked(&curval, uaddr, uval, newval);
1417 if (unlikely(err))
1418 return err;
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001419
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00001420 /* If user space value changed, let the caller retry */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001421 return curval != uval ? -EAGAIN : 0;
1422}
1423
Darren Hart1a520842009-04-03 13:39:52 -07001424/**
Darren Hartd96ee562009-09-21 22:30:22 -07001425 * futex_lock_pi_atomic() - Atomic work required to acquire a pi aware futex
Darren Hartbab5bc92009-04-07 23:23:50 -07001426 * @uaddr: the pi futex user address
1427 * @hb: the pi futex hash bucket
1428 * @key: the futex key associated with uaddr and hb
1429 * @ps: the pi_state pointer where we store the result of the
1430 * lookup
1431 * @task: the task to perform the atomic lock work for. This will
1432 * be "current" except in the case of requeue pi.
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001433 * @exiting: Pointer to store the task pointer of the owner task
1434 * which is in the middle of exiting
Darren Hartbab5bc92009-04-07 23:23:50 -07001435 * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0)
Darren Hart1a520842009-04-03 13:39:52 -07001436 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08001437 * Return:
1438 * 0 - ready to wait;
1439 * 1 - acquired the lock;
Darren Hart1a520842009-04-03 13:39:52 -07001440 * <0 - error
1441 *
1442 * The hb->lock and futex_key refs shall be held by the caller.
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001443 *
1444 * @exiting is only set when the return value is -EBUSY. If so, this holds
1445 * a refcount on the exiting task on return and the caller needs to drop it
1446 * after waiting for the exit to complete.
Darren Hart1a520842009-04-03 13:39:52 -07001447 */
1448static int futex_lock_pi_atomic(u32 __user *uaddr, struct futex_hash_bucket *hb,
1449 union futex_key *key,
1450 struct futex_pi_state **ps,
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001451 struct task_struct *task,
1452 struct task_struct **exiting,
1453 int set_waiters)
Darren Hart1a520842009-04-03 13:39:52 -07001454{
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001455 u32 uval, newval, vpid = task_pid_vnr(task);
Ben Hutchingsbfefc9e2021-03-01 18:30:39 +01001456 struct futex_q *top_waiter;
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001457 int ret;
Darren Hart1a520842009-04-03 13:39:52 -07001458
1459 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001460 * Read the user space value first so we can validate a few
1461 * things before proceeding further.
Darren Hart1a520842009-04-03 13:39:52 -07001462 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001463 if (get_futex_value_locked(&uval, uaddr))
Darren Hart1a520842009-04-03 13:39:52 -07001464 return -EFAULT;
1465
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001466 if (unlikely(should_fail_futex(true)))
1467 return -EFAULT;
1468
Darren Hart1a520842009-04-03 13:39:52 -07001469 /*
1470 * Detect deadlocks.
1471 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001472 if ((unlikely((uval & FUTEX_TID_MASK) == vpid)))
Darren Hart1a520842009-04-03 13:39:52 -07001473 return -EDEADLK;
1474
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001475 if ((unlikely(should_fail_futex(true))))
1476 return -EDEADLK;
1477
Darren Hart1a520842009-04-03 13:39:52 -07001478 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001479 * Lookup existing state first. If it exists, try to attach to
1480 * its pi_state.
Darren Hart1a520842009-04-03 13:39:52 -07001481 */
Ben Hutchingsbfefc9e2021-03-01 18:30:39 +01001482 top_waiter = futex_top_waiter(hb, key);
1483 if (top_waiter)
1484 return attach_to_pi_state(uaddr, uval, top_waiter->pi_state, ps);
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001485
1486 /*
1487 * No waiter and user TID is 0. We are here because the
1488 * waiters or the owner died bit is set or called from
1489 * requeue_cmp_pi or for whatever reason something took the
1490 * syscall.
1491 */
1492 if (!(uval & FUTEX_TID_MASK)) {
Thomas Gleixnerb3eaa9f2014-06-03 12:27:06 +00001493 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001494 * We take over the futex. No other waiters and the user space
1495 * TID is 0. We preserve the owner died bit.
Thomas Gleixnerb3eaa9f2014-06-03 12:27:06 +00001496 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001497 newval = uval & FUTEX_OWNER_DIED;
1498 newval |= vpid;
1499
1500 /* The futex requeue_pi code can enforce the waiters bit */
1501 if (set_waiters)
1502 newval |= FUTEX_WAITERS;
1503
1504 ret = lock_pi_update_atomic(uaddr, uval, newval);
1505 /* If the take over worked, return 1 */
1506 return ret < 0 ? ret : 1;
Thomas Gleixnerb3eaa9f2014-06-03 12:27:06 +00001507 }
Darren Hart1a520842009-04-03 13:39:52 -07001508
Darren Hart1a520842009-04-03 13:39:52 -07001509 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001510 * First waiter. Set the waiters bit before attaching ourself to
1511 * the owner. If owner tries to unlock, it will be forced into
1512 * the kernel and blocked on hb->lock.
Darren Hart1a520842009-04-03 13:39:52 -07001513 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001514 newval = uval | FUTEX_WAITERS;
1515 ret = lock_pi_update_atomic(uaddr, uval, newval);
1516 if (ret)
1517 return ret;
Darren Hart1a520842009-04-03 13:39:52 -07001518 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001519 * If the update of the user space value succeeded, we try to
1520 * attach to the owner. If that fails, no harm done, we only
1521 * set the FUTEX_WAITERS bit in the user space variable.
Darren Hart1a520842009-04-03 13:39:52 -07001522 */
Thomas Gleixner9c3f3982021-02-11 09:27:00 +00001523 return attach_to_pi_owner(uaddr, newval, key, ps, exiting);
Darren Hart1a520842009-04-03 13:39:52 -07001524}
1525
Lai Jiangshan2e129782010-12-22 14:18:50 +08001526/**
1527 * __unqueue_futex() - Remove the futex_q from its futex_hash_bucket
1528 * @q: The futex_q to unqueue
1529 *
1530 * The q->lock_ptr must not be NULL and must be held by the caller.
1531 */
1532static void __unqueue_futex(struct futex_q *q)
1533{
1534 struct futex_hash_bucket *hb;
1535
Steven Rostedt29096202011-03-17 15:21:07 -04001536 if (WARN_ON_SMP(!q->lock_ptr || !spin_is_locked(q->lock_ptr))
1537 || WARN_ON(plist_node_empty(&q->list)))
Lai Jiangshan2e129782010-12-22 14:18:50 +08001538 return;
1539
1540 hb = container_of(q->lock_ptr, struct futex_hash_bucket, lock);
1541 plist_del(&q->list, &hb->chain);
Linus Torvalds11d46162014-03-20 22:11:17 -07001542 hb_waiters_dec(hb);
Lai Jiangshan2e129782010-12-22 14:18:50 +08001543}
1544
Ingo Molnarc87e2832006-06-27 02:54:58 -07001545/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 * The hash bucket lock must be held when this is called.
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001547 * Afterwards, the futex_q must not be accessed. Callers
1548 * must ensure to later call wake_up_q() for the actual
1549 * wakeups to occur.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 */
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001551static void mark_wake_futex(struct wake_q_head *wake_q, struct futex_q *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001553 struct task_struct *p = q->task;
1554
Darren Hartaa109902012-11-26 16:29:56 -08001555 if (WARN(q->pi_state || q->rt_waiter, "refusing to wake PI futex\n"))
1556 return;
1557
Peter Zijlstra5083fb82021-03-28 22:42:32 +02001558 get_task_struct(p);
Lai Jiangshan2e129782010-12-22 14:18:50 +08001559 __unqueue_futex(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 /*
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001561 * The waiting task can free the futex_q as soon as
1562 * q->lock_ptr = NULL is written, without taking any locks. A
1563 * memory barrier is required here to prevent the following
1564 * store to lock_ptr from getting ahead of the plist_del.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 */
Peter Zijlstra77d6a4c2021-03-28 22:40:54 +02001566 smp_store_release(&q->lock_ptr, NULL);
Peter Zijlstra5083fb82021-03-28 22:42:32 +02001567
1568 /*
1569 * Queue the task for later wakeup for after we've released
1570 * the hb->lock. wake_q_add() grabs reference to p.
1571 */
1572 wake_q_add(wake_q, p);
1573 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574}
1575
Ben Hutchings312d9d62021-03-01 18:31:30 +01001576/*
1577 * Caller must hold a reference on @pi_state.
1578 */
1579static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_pi_state *pi_state)
Ingo Molnarc87e2832006-06-27 02:54:58 -07001580{
Vitaliy Ivanov7cfdaf32011-07-07 15:10:31 +03001581 u32 uninitialized_var(curval), newval;
Ben Hutchings312d9d62021-03-01 18:31:30 +01001582 struct task_struct *new_owner;
1583 bool deboost = false;
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001584 WAKE_Q(wake_q);
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001585 int ret = 0;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001586
Ingo Molnarc87e2832006-06-27 02:54:58 -07001587 new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);
Ben Hutchings25a678d2021-03-01 18:31:39 +01001588 if (WARN_ON_ONCE(!new_owner)) {
Ben Hutchings312d9d62021-03-01 18:31:30 +01001589 /*
Ben Hutchings25a678d2021-03-01 18:31:39 +01001590 * As per the comment in futex_unlock_pi() this should not happen.
Ben Hutchings312d9d62021-03-01 18:31:30 +01001591 *
1592 * When this happens, give up our locks and try again, giving
1593 * the futex_lock_pi() instance time to complete, either by
1594 * waiting on the rtmutex or removing itself from the futex
1595 * queue.
1596 */
1597 ret = -EAGAIN;
1598 goto out_unlock;
Peter Zijlstra71f093c2021-02-03 13:45:32 +00001599 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001600
1601 /*
Ben Hutchings312d9d62021-03-01 18:31:30 +01001602 * We pass it to the next owner. The WAITERS bit is always kept
1603 * enabled while there is PI state around. We cleanup the owner
1604 * died bit, because we are the owner.
Ingo Molnarc87e2832006-06-27 02:54:58 -07001605 */
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001606 newval = FUTEX_WAITERS | task_pid_vnr(new_owner);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001607
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001608 if (unlikely(should_fail_futex(true)))
1609 ret = -EFAULT;
1610
Will Deacon8682c2e2021-03-28 22:42:44 +02001611 ret = cmpxchg_futex_value_locked(&curval, uaddr, uval, newval);
1612 if (!ret && (curval != uval)) {
Sebastian Andrzej Siewior89e9e662016-04-15 14:35:39 +02001613 /*
1614 * If a unconditional UNLOCK_PI operation (user space did not
1615 * try the TID->0 transition) raced with a waiter setting the
1616 * FUTEX_WAITERS flag between get_user() and locking the hash
1617 * bucket lock, retry the operation.
1618 */
1619 if ((FUTEX_TID_MASK & curval) == uval)
1620 ret = -EAGAIN;
1621 else
1622 ret = -EINVAL;
1623 }
Thomas Gleixner76bc0ec2021-02-03 13:45:35 +00001624
1625 if (!ret) {
1626 /*
1627 * This is a point of no return; once we modified the uval
1628 * there is no going back and subsequent operations must
1629 * not fail.
1630 */
1631 pi_state_update_owner(pi_state, new_owner);
1632 deboost = __rt_mutex_futex_unlock(&pi_state->pi_mutex, &wake_q);
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001633 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001634
Ben Hutchings312d9d62021-03-01 18:31:30 +01001635out_unlock:
Peter Zijlstra2c60d4a2021-02-03 13:45:30 +00001636 raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
Peter Zijlstra2c60d4a2021-02-03 13:45:30 +00001637
1638 if (deboost) {
1639 wake_up_q(&wake_q);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001640 rt_mutex_adjust_prio(current);
Peter Zijlstra2c60d4a2021-02-03 13:45:30 +00001641 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001642
Ben Hutchings312d9d62021-03-01 18:31:30 +01001643 return ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001644}
1645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646/*
Ingo Molnar8b8f3192006-07-03 00:25:05 -07001647 * Express the locking dependencies for lockdep:
1648 */
1649static inline void
1650double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
1651{
1652 if (hb1 <= hb2) {
1653 spin_lock(&hb1->lock);
1654 if (hb1 < hb2)
1655 spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING);
1656 } else { /* hb1 > hb2 */
1657 spin_lock(&hb2->lock);
1658 spin_lock_nested(&hb1->lock, SINGLE_DEPTH_NESTING);
1659 }
1660}
1661
Darren Hart5eb3dc62009-03-12 00:55:52 -07001662static inline void
1663double_unlock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
1664{
Darren Hartf061d352009-03-12 15:11:18 -07001665 spin_unlock(&hb1->lock);
Ingo Molnar88f502f2009-03-13 10:32:07 +01001666 if (hb1 != hb2)
1667 spin_unlock(&hb2->lock);
Darren Hart5eb3dc62009-03-12 00:55:52 -07001668}
1669
Ingo Molnar8b8f3192006-07-03 00:25:05 -07001670/*
Darren Hartb2d09942009-03-12 00:55:37 -07001671 * Wake up waiters matching bitset queued on this futex (uaddr).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 */
Darren Hartb41277d2010-11-08 13:10:09 -08001673static int
1674futex_wake(u32 __user *uaddr, unsigned int flags, int nr_wake, u32 bitset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
Ingo Molnare2970f22006-06-27 02:54:47 -07001676 struct futex_hash_bucket *hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 struct futex_q *this, *next;
Peter Zijlstra38d47c12008-09-26 19:32:20 +02001678 union futex_key key = FUTEX_KEY_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 int ret;
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001680 WAKE_Q(wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Thomas Gleixnercd689982008-02-01 17:45:14 +01001682 if (!bitset)
1683 return -EINVAL;
1684
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001685 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 if (unlikely(ret != 0))
1687 goto out;
1688
Ingo Molnare2970f22006-06-27 02:54:47 -07001689 hb = hash_futex(&key);
Davidlohr Buesob0c29f72014-01-12 15:31:25 -08001690
1691 /* Make sure we really have tasks to wakeup */
1692 if (!hb_waiters_pending(hb))
1693 goto out_put_key;
1694
Ingo Molnare2970f22006-06-27 02:54:47 -07001695 spin_lock(&hb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Jason Low0d00c7b2014-01-12 15:31:22 -08001697 plist_for_each_entry_safe(this, next, &hb->chain, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 if (match_futex (&this->key, &key)) {
Darren Hart52400ba2009-04-03 13:40:49 -07001699 if (this->pi_state || this->rt_waiter) {
Ingo Molnared6f7b12006-07-01 04:35:46 -07001700 ret = -EINVAL;
1701 break;
1702 }
Thomas Gleixnercd689982008-02-01 17:45:14 +01001703
1704 /* Check if one of the bits is set in both bitsets */
1705 if (!(this->bitset & bitset))
1706 continue;
1707
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001708 mark_wake_futex(&wake_q, this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 if (++ret >= nr_wake)
1710 break;
1711 }
1712 }
1713
Ingo Molnare2970f22006-06-27 02:54:47 -07001714 spin_unlock(&hb->lock);
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001715 wake_up_q(&wake_q);
Davidlohr Buesob0c29f72014-01-12 15:31:25 -08001716out_put_key:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001717 put_futex_key(&key);
Darren Hart42d35d42008-12-29 15:49:53 -08001718out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 return ret;
1720}
1721
Jiri Slaby81da9f82017-08-24 09:31:05 +02001722static int futex_atomic_op_inuser(unsigned int encoded_op, u32 __user *uaddr)
1723{
1724 unsigned int op = (encoded_op & 0x70000000) >> 28;
1725 unsigned int cmp = (encoded_op & 0x0f000000) >> 24;
Jiri Slabya1640092017-11-30 15:35:44 +01001726 int oparg = sign_extend32((encoded_op & 0x00fff000) >> 12, 11);
1727 int cmparg = sign_extend32(encoded_op & 0x00000fff, 11);
Jiri Slaby81da9f82017-08-24 09:31:05 +02001728 int oldval, ret;
1729
1730 if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28)) {
Jiri Slabyda92e8a2017-10-23 13:41:51 +02001731 if (oparg < 0 || oparg > 31) {
1732 char comm[sizeof(current->comm)];
1733 /*
1734 * kill this print and return -EINVAL when userspace
1735 * is sane again
1736 */
1737 pr_info_ratelimited("futex_wake_op: %s tries to shift op by %d; fix this program\n",
1738 get_task_comm(comm, current), oparg);
1739 oparg &= 31;
1740 }
Jiri Slaby81da9f82017-08-24 09:31:05 +02001741 oparg = 1 << oparg;
1742 }
1743
1744 if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
1745 return -EFAULT;
1746
1747 ret = arch_futex_atomic_op_inuser(op, oparg, &oldval, uaddr);
1748 if (ret)
1749 return ret;
1750
1751 switch (cmp) {
1752 case FUTEX_OP_CMP_EQ:
1753 return oldval == cmparg;
1754 case FUTEX_OP_CMP_NE:
1755 return oldval != cmparg;
1756 case FUTEX_OP_CMP_LT:
1757 return oldval < cmparg;
1758 case FUTEX_OP_CMP_GE:
1759 return oldval >= cmparg;
1760 case FUTEX_OP_CMP_LE:
1761 return oldval <= cmparg;
1762 case FUTEX_OP_CMP_GT:
1763 return oldval > cmparg;
1764 default:
1765 return -ENOSYS;
1766 }
1767}
1768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769/*
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001770 * Wake up all waiters hashed on the physical page that is mapped
1771 * to this virtual address:
1772 */
Ingo Molnare2970f22006-06-27 02:54:47 -07001773static int
Darren Hartb41277d2010-11-08 13:10:09 -08001774futex_wake_op(u32 __user *uaddr1, unsigned int flags, u32 __user *uaddr2,
Ingo Molnare2970f22006-06-27 02:54:47 -07001775 int nr_wake, int nr_wake2, int op)
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001776{
Peter Zijlstra38d47c12008-09-26 19:32:20 +02001777 union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
Ingo Molnare2970f22006-06-27 02:54:47 -07001778 struct futex_hash_bucket *hb1, *hb2;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001779 struct futex_q *this, *next;
Darren Harte4dc5b72009-03-12 00:56:13 -07001780 int ret, op_ret;
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001781 WAKE_Q(wake_q);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001782
Darren Harte4dc5b72009-03-12 00:56:13 -07001783retry:
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001784 ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, VERIFY_READ);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001785 if (unlikely(ret != 0))
1786 goto out;
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001787 ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001788 if (unlikely(ret != 0))
Darren Hart42d35d42008-12-29 15:49:53 -08001789 goto out_put_key1;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001790
Ingo Molnare2970f22006-06-27 02:54:47 -07001791 hb1 = hash_futex(&key1);
1792 hb2 = hash_futex(&key2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001793
Darren Harte4dc5b72009-03-12 00:56:13 -07001794retry_private:
Thomas Gleixnereaaea802009-10-04 09:34:17 +02001795 double_lock_hb(hb1, hb2);
Ingo Molnare2970f22006-06-27 02:54:47 -07001796 op_ret = futex_atomic_op_inuser(op, uaddr2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001797 if (unlikely(op_ret < 0)) {
Darren Hart5eb3dc62009-03-12 00:55:52 -07001798 double_unlock_hb(hb1, hb2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001799
Will Deacon8682c2e2021-03-28 22:42:44 +02001800 if (!IS_ENABLED(CONFIG_MMU) ||
1801 unlikely(op_ret != -EFAULT && op_ret != -EAGAIN)) {
1802 /*
1803 * we don't get EFAULT from MMU faults if we don't have
1804 * an MMU, but we might get them from range checking
1805 */
David Gibson796f8d92005-11-07 00:59:33 -08001806 ret = op_ret;
Darren Hart42d35d42008-12-29 15:49:53 -08001807 goto out_put_keys;
David Gibson796f8d92005-11-07 00:59:33 -08001808 }
1809
Will Deacon8682c2e2021-03-28 22:42:44 +02001810 if (op_ret == -EFAULT) {
1811 ret = fault_in_user_writeable(uaddr2);
1812 if (ret)
1813 goto out_put_keys;
1814 }
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001815
Will Deacon8682c2e2021-03-28 22:42:44 +02001816 if (!(flags & FLAGS_SHARED)) {
1817 cond_resched();
Darren Harte4dc5b72009-03-12 00:56:13 -07001818 goto retry_private;
Will Deacon8682c2e2021-03-28 22:42:44 +02001819 }
Darren Harte4dc5b72009-03-12 00:56:13 -07001820
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001821 put_futex_key(&key2);
1822 put_futex_key(&key1);
Will Deacon8682c2e2021-03-28 22:42:44 +02001823 cond_resched();
Darren Harte4dc5b72009-03-12 00:56:13 -07001824 goto retry;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001825 }
1826
Jason Low0d00c7b2014-01-12 15:31:22 -08001827 plist_for_each_entry_safe(this, next, &hb1->chain, list) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001828 if (match_futex (&this->key, &key1)) {
Darren Hartaa109902012-11-26 16:29:56 -08001829 if (this->pi_state || this->rt_waiter) {
1830 ret = -EINVAL;
1831 goto out_unlock;
1832 }
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001833 mark_wake_futex(&wake_q, this);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001834 if (++ret >= nr_wake)
1835 break;
1836 }
1837 }
1838
1839 if (op_ret > 0) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001840 op_ret = 0;
Jason Low0d00c7b2014-01-12 15:31:22 -08001841 plist_for_each_entry_safe(this, next, &hb2->chain, list) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001842 if (match_futex (&this->key, &key2)) {
Darren Hartaa109902012-11-26 16:29:56 -08001843 if (this->pi_state || this->rt_waiter) {
1844 ret = -EINVAL;
1845 goto out_unlock;
1846 }
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001847 mark_wake_futex(&wake_q, this);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001848 if (++op_ret >= nr_wake2)
1849 break;
1850 }
1851 }
1852 ret += op_ret;
1853 }
1854
Darren Hartaa109902012-11-26 16:29:56 -08001855out_unlock:
Darren Hart5eb3dc62009-03-12 00:55:52 -07001856 double_unlock_hb(hb1, hb2);
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001857 wake_up_q(&wake_q);
Darren Hart42d35d42008-12-29 15:49:53 -08001858out_put_keys:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001859 put_futex_key(&key2);
Darren Hart42d35d42008-12-29 15:49:53 -08001860out_put_key1:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001861 put_futex_key(&key1);
Darren Hart42d35d42008-12-29 15:49:53 -08001862out:
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001863 return ret;
1864}
1865
Darren Hart9121e472009-04-03 13:40:31 -07001866/**
1867 * requeue_futex() - Requeue a futex_q from one hb to another
1868 * @q: the futex_q to requeue
1869 * @hb1: the source hash_bucket
1870 * @hb2: the target hash_bucket
1871 * @key2: the new key for the requeued futex_q
1872 */
1873static inline
1874void requeue_futex(struct futex_q *q, struct futex_hash_bucket *hb1,
1875 struct futex_hash_bucket *hb2, union futex_key *key2)
1876{
1877
1878 /*
1879 * If key1 and key2 hash to the same bucket, no need to
1880 * requeue.
1881 */
1882 if (likely(&hb1->chain != &hb2->chain)) {
1883 plist_del(&q->list, &hb1->chain);
Linus Torvalds11d46162014-03-20 22:11:17 -07001884 hb_waiters_dec(hb1);
Linus Torvalds11d46162014-03-20 22:11:17 -07001885 hb_waiters_inc(hb2);
Davidlohr Buesofe1bce92016-04-20 20:09:24 -07001886 plist_add(&q->list, &hb2->chain);
Darren Hart9121e472009-04-03 13:40:31 -07001887 q->lock_ptr = &hb2->lock;
Darren Hart9121e472009-04-03 13:40:31 -07001888 }
1889 get_futex_key_refs(key2);
1890 q->key = *key2;
1891}
1892
Darren Hart52400ba2009-04-03 13:40:49 -07001893/**
1894 * requeue_pi_wake_futex() - Wake a task that acquired the lock during requeue
Darren Hartd96ee562009-09-21 22:30:22 -07001895 * @q: the futex_q
1896 * @key: the key of the requeue target futex
1897 * @hb: the hash_bucket of the requeue target futex
Darren Hart52400ba2009-04-03 13:40:49 -07001898 *
1899 * During futex_requeue, with requeue_pi=1, it is possible to acquire the
1900 * target futex if it is uncontended or via a lock steal. Set the futex_q key
1901 * to the requeue target futex so the waiter can detect the wakeup on the right
1902 * futex, but remove it from the hb and NULL the rt_waiter so it can detect
Darren Hartbeda2c72009-08-09 15:34:39 -07001903 * atomic lock acquisition. Set the q->lock_ptr to the requeue target hb->lock
1904 * to protect access to the pi_state to fixup the owner later. Must be called
1905 * with both q->lock_ptr and hb->lock held.
Darren Hart52400ba2009-04-03 13:40:49 -07001906 */
1907static inline
Darren Hartbeda2c72009-08-09 15:34:39 -07001908void requeue_pi_wake_futex(struct futex_q *q, union futex_key *key,
1909 struct futex_hash_bucket *hb)
Darren Hart52400ba2009-04-03 13:40:49 -07001910{
Darren Hart52400ba2009-04-03 13:40:49 -07001911 get_futex_key_refs(key);
1912 q->key = *key;
1913
Lai Jiangshan2e129782010-12-22 14:18:50 +08001914 __unqueue_futex(q);
Darren Hart52400ba2009-04-03 13:40:49 -07001915
1916 WARN_ON(!q->rt_waiter);
1917 q->rt_waiter = NULL;
1918
Darren Hartbeda2c72009-08-09 15:34:39 -07001919 q->lock_ptr = &hb->lock;
Darren Hartbeda2c72009-08-09 15:34:39 -07001920
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001921 wake_up_state(q->task, TASK_NORMAL);
Darren Hart52400ba2009-04-03 13:40:49 -07001922}
1923
1924/**
1925 * futex_proxy_trylock_atomic() - Attempt an atomic lock for the top waiter
Darren Hartbab5bc92009-04-07 23:23:50 -07001926 * @pifutex: the user address of the to futex
1927 * @hb1: the from futex hash bucket, must be locked by the caller
1928 * @hb2: the to futex hash bucket, must be locked by the caller
1929 * @key1: the from futex key
1930 * @key2: the to futex key
1931 * @ps: address to store the pi_state pointer
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001932 * @exiting: Pointer to store the task pointer of the owner task
1933 * which is in the middle of exiting
Darren Hartbab5bc92009-04-07 23:23:50 -07001934 * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0)
Darren Hart52400ba2009-04-03 13:40:49 -07001935 *
1936 * Try and get the lock on behalf of the top waiter if we can do it atomically.
Darren Hartbab5bc92009-04-07 23:23:50 -07001937 * Wake the top waiter if we succeed. If the caller specified set_waiters,
1938 * then direct futex_lock_pi_atomic() to force setting the FUTEX_WAITERS bit.
1939 * hb1 and hb2 must be held by the caller.
Darren Hart52400ba2009-04-03 13:40:49 -07001940 *
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001941 * @exiting is only set when the return value is -EBUSY. If so, this holds
1942 * a refcount on the exiting task on return and the caller needs to drop it
1943 * after waiting for the exit to complete.
1944 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08001945 * Return:
1946 * 0 - failed to acquire the lock atomically;
Thomas Gleixner866293e2014-05-12 20:45:34 +00001947 * >0 - acquired the lock, return value is vpid of the top_waiter
Darren Hart52400ba2009-04-03 13:40:49 -07001948 * <0 - error
1949 */
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001950static int
1951futex_proxy_trylock_atomic(u32 __user *pifutex, struct futex_hash_bucket *hb1,
1952 struct futex_hash_bucket *hb2, union futex_key *key1,
1953 union futex_key *key2, struct futex_pi_state **ps,
1954 struct task_struct **exiting, int set_waiters)
Darren Hart52400ba2009-04-03 13:40:49 -07001955{
Darren Hartbab5bc92009-04-07 23:23:50 -07001956 struct futex_q *top_waiter = NULL;
Darren Hart52400ba2009-04-03 13:40:49 -07001957 u32 curval;
Thomas Gleixner866293e2014-05-12 20:45:34 +00001958 int ret, vpid;
Darren Hart52400ba2009-04-03 13:40:49 -07001959
1960 if (get_futex_value_locked(&curval, pifutex))
1961 return -EFAULT;
1962
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001963 if (unlikely(should_fail_futex(true)))
1964 return -EFAULT;
1965
Darren Hartbab5bc92009-04-07 23:23:50 -07001966 /*
1967 * Find the top_waiter and determine if there are additional waiters.
1968 * If the caller intends to requeue more than 1 waiter to pifutex,
1969 * force futex_lock_pi_atomic() to set the FUTEX_WAITERS bit now,
1970 * as we have means to handle the possible fault. If not, don't set
1971 * the bit unecessarily as it will force the subsequent unlock to enter
1972 * the kernel.
1973 */
Darren Hart52400ba2009-04-03 13:40:49 -07001974 top_waiter = futex_top_waiter(hb1, key1);
1975
1976 /* There are no waiters, nothing for us to do. */
1977 if (!top_waiter)
1978 return 0;
1979
Darren Hart84bc4af2009-08-13 17:36:53 -07001980 /* Ensure we requeue to the expected futex. */
1981 if (!match_futex(top_waiter->requeue_pi_key, key2))
1982 return -EINVAL;
1983
Darren Hart52400ba2009-04-03 13:40:49 -07001984 /*
Darren Hartbab5bc92009-04-07 23:23:50 -07001985 * Try to take the lock for top_waiter. Set the FUTEX_WAITERS bit in
1986 * the contended case or if set_waiters is 1. The pi_state is returned
1987 * in ps in contended cases.
Darren Hart52400ba2009-04-03 13:40:49 -07001988 */
Thomas Gleixner866293e2014-05-12 20:45:34 +00001989 vpid = task_pid_vnr(top_waiter->task);
Darren Hartbab5bc92009-04-07 23:23:50 -07001990 ret = futex_lock_pi_atomic(pifutex, hb2, key2, ps, top_waiter->task,
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001991 exiting, set_waiters);
Thomas Gleixner866293e2014-05-12 20:45:34 +00001992 if (ret == 1) {
Darren Hartbeda2c72009-08-09 15:34:39 -07001993 requeue_pi_wake_futex(top_waiter, key2, hb2);
Thomas Gleixner866293e2014-05-12 20:45:34 +00001994 return vpid;
1995 }
Darren Hart52400ba2009-04-03 13:40:49 -07001996 return ret;
1997}
1998
1999/**
2000 * futex_requeue() - Requeue waiters from uaddr1 to uaddr2
Randy Dunlapfb62db22010-10-13 11:02:34 -07002001 * @uaddr1: source futex user address
Darren Hartb41277d2010-11-08 13:10:09 -08002002 * @flags: futex flags (FLAGS_SHARED, etc.)
Randy Dunlapfb62db22010-10-13 11:02:34 -07002003 * @uaddr2: target futex user address
2004 * @nr_wake: number of waiters to wake (must be 1 for requeue_pi)
2005 * @nr_requeue: number of waiters to requeue (0-INT_MAX)
2006 * @cmpval: @uaddr1 expected value (or %NULL)
2007 * @requeue_pi: if we are attempting to requeue from a non-pi futex to a
Darren Hartb41277d2010-11-08 13:10:09 -08002008 * pi futex (pi to pi requeue is not supported)
Darren Hart52400ba2009-04-03 13:40:49 -07002009 *
2010 * Requeue waiters on uaddr1 to uaddr2. In the requeue_pi case, try to acquire
2011 * uaddr2 atomically on behalf of the top waiter.
2012 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002013 * Return:
2014 * >=0 - on success, the number of tasks requeued or woken;
Darren Hart52400ba2009-04-03 13:40:49 -07002015 * <0 - on error
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 */
Darren Hartb41277d2010-11-08 13:10:09 -08002017static int futex_requeue(u32 __user *uaddr1, unsigned int flags,
2018 u32 __user *uaddr2, int nr_wake, int nr_requeue,
2019 u32 *cmpval, int requeue_pi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
Peter Zijlstra38d47c12008-09-26 19:32:20 +02002021 union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
Darren Hart52400ba2009-04-03 13:40:49 -07002022 int drop_count = 0, task_count = 0, ret;
2023 struct futex_pi_state *pi_state = NULL;
Ingo Molnare2970f22006-06-27 02:54:47 -07002024 struct futex_hash_bucket *hb1, *hb2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 struct futex_q *this, *next;
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07002026 WAKE_Q(wake_q);
Darren Hart52400ba2009-04-03 13:40:49 -07002027
Li Jinyued8a31702017-12-14 17:04:54 +08002028 if (nr_wake < 0 || nr_requeue < 0)
2029 return -EINVAL;
2030
Darren Hart52400ba2009-04-03 13:40:49 -07002031 if (requeue_pi) {
2032 /*
Thomas Gleixnere9c243a2014-06-03 12:27:06 +00002033 * Requeue PI only works on two distinct uaddrs. This
2034 * check is only valid for private futexes. See below.
2035 */
2036 if (uaddr1 == uaddr2)
2037 return -EINVAL;
2038
2039 /*
Darren Hart52400ba2009-04-03 13:40:49 -07002040 * requeue_pi requires a pi_state, try to allocate it now
2041 * without any locks in case it fails.
2042 */
2043 if (refill_pi_state_cache())
2044 return -ENOMEM;
2045 /*
2046 * requeue_pi must wake as many tasks as it can, up to nr_wake
2047 * + nr_requeue, since it acquires the rt_mutex prior to
2048 * returning to userspace, so as to not leave the rt_mutex with
2049 * waiters and no owner. However, second and third wake-ups
2050 * cannot be predicted as they involve race conditions with the
2051 * first wake and a fault while looking up the pi_state. Both
2052 * pthread_cond_signal() and pthread_cond_broadcast() should
2053 * use nr_wake=1.
2054 */
2055 if (nr_wake != 1)
2056 return -EINVAL;
2057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Darren Hart42d35d42008-12-29 15:49:53 -08002059retry:
Shawn Bohrer9ea71502011-06-30 11:21:32 -05002060 ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 if (unlikely(ret != 0))
2062 goto out;
Shawn Bohrer9ea71502011-06-30 11:21:32 -05002063 ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2,
2064 requeue_pi ? VERIFY_WRITE : VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 if (unlikely(ret != 0))
Darren Hart42d35d42008-12-29 15:49:53 -08002066 goto out_put_key1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Thomas Gleixnere9c243a2014-06-03 12:27:06 +00002068 /*
2069 * The check above which compares uaddrs is not sufficient for
2070 * shared futexes. We need to compare the keys:
2071 */
2072 if (requeue_pi && match_futex(&key1, &key2)) {
2073 ret = -EINVAL;
2074 goto out_put_keys;
2075 }
2076
Ingo Molnare2970f22006-06-27 02:54:47 -07002077 hb1 = hash_futex(&key1);
2078 hb2 = hash_futex(&key2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Darren Harte4dc5b72009-03-12 00:56:13 -07002080retry_private:
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07002081 hb_waiters_inc(hb2);
Ingo Molnar8b8f3192006-07-03 00:25:05 -07002082 double_lock_hb(hb1, hb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Ingo Molnare2970f22006-06-27 02:54:47 -07002084 if (likely(cmpval != NULL)) {
2085 u32 curval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Ingo Molnare2970f22006-06-27 02:54:47 -07002087 ret = get_futex_value_locked(&curval, uaddr1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
2089 if (unlikely(ret)) {
Darren Hart5eb3dc62009-03-12 00:55:52 -07002090 double_unlock_hb(hb1, hb2);
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07002091 hb_waiters_dec(hb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Darren Harte4dc5b72009-03-12 00:56:13 -07002093 ret = get_user(curval, uaddr1);
2094 if (ret)
2095 goto out_put_keys;
2096
Darren Hartb41277d2010-11-08 13:10:09 -08002097 if (!(flags & FLAGS_SHARED))
Darren Harte4dc5b72009-03-12 00:56:13 -07002098 goto retry_private;
2099
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002100 put_futex_key(&key2);
2101 put_futex_key(&key1);
Darren Harte4dc5b72009-03-12 00:56:13 -07002102 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 }
Ingo Molnare2970f22006-06-27 02:54:47 -07002104 if (curval != *cmpval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 ret = -EAGAIN;
2106 goto out_unlock;
2107 }
2108 }
2109
Darren Hart52400ba2009-04-03 13:40:49 -07002110 if (requeue_pi && (task_count - nr_wake < nr_requeue)) {
Thomas Gleixnercf16e422021-02-01 10:01:43 +00002111 struct task_struct *exiting = NULL;
2112
Darren Hartbab5bc92009-04-07 23:23:50 -07002113 /*
2114 * Attempt to acquire uaddr2 and wake the top waiter. If we
2115 * intend to requeue waiters, force setting the FUTEX_WAITERS
2116 * bit. We force this here where we are able to easily handle
2117 * faults rather in the requeue loop below.
2118 */
Darren Hart52400ba2009-04-03 13:40:49 -07002119 ret = futex_proxy_trylock_atomic(uaddr2, hb1, hb2, &key1,
Thomas Gleixnercf16e422021-02-01 10:01:43 +00002120 &key2, &pi_state,
2121 &exiting, nr_requeue);
Darren Hart52400ba2009-04-03 13:40:49 -07002122
2123 /*
2124 * At this point the top_waiter has either taken uaddr2 or is
2125 * waiting on it. If the former, then the pi_state will not
2126 * exist yet, look it up one more time to ensure we have a
Thomas Gleixner866293e2014-05-12 20:45:34 +00002127 * reference to it. If the lock was taken, ret contains the
2128 * vpid of the top waiter task.
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00002129 * If the lock was not taken, we have pi_state and an initial
2130 * refcount on it. In case of an error we have nothing.
Darren Hart52400ba2009-04-03 13:40:49 -07002131 */
Thomas Gleixner866293e2014-05-12 20:45:34 +00002132 if (ret > 0) {
Darren Hart52400ba2009-04-03 13:40:49 -07002133 WARN_ON(pi_state);
Darren Hart89061d32009-10-15 15:30:48 -07002134 drop_count++;
Darren Hart52400ba2009-04-03 13:40:49 -07002135 task_count++;
Thomas Gleixner866293e2014-05-12 20:45:34 +00002136 /*
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00002137 * If we acquired the lock, then the user space value
2138 * of uaddr2 should be vpid. It cannot be changed by
2139 * the top waiter as it is blocked on hb2 lock if it
2140 * tries to do so. If something fiddled with it behind
2141 * our back the pi state lookup might unearth it. So
2142 * we rather use the known value than rereading and
2143 * handing potential crap to lookup_pi_state.
2144 *
2145 * If that call succeeds then we have pi_state and an
2146 * initial refcount on it.
Thomas Gleixner866293e2014-05-12 20:45:34 +00002147 */
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00002148 ret = lookup_pi_state(uaddr2, ret, hb2, &key2,
Thomas Gleixnercf16e422021-02-01 10:01:43 +00002149 &pi_state, &exiting);
Darren Hart52400ba2009-04-03 13:40:49 -07002150 }
2151
2152 switch (ret) {
2153 case 0:
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00002154 /* We hold a reference on the pi state. */
Darren Hart52400ba2009-04-03 13:40:49 -07002155 break;
Thomas Gleixner4959f2d2015-12-19 20:07:40 +00002156
2157 /* If the above failed, then pi_state is NULL */
Darren Hart52400ba2009-04-03 13:40:49 -07002158 case -EFAULT:
2159 double_unlock_hb(hb1, hb2);
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07002160 hb_waiters_dec(hb2);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002161 put_futex_key(&key2);
2162 put_futex_key(&key1);
Thomas Gleixnerd0725992009-06-11 23:15:43 +02002163 ret = fault_in_user_writeable(uaddr2);
Darren Hart52400ba2009-04-03 13:40:49 -07002164 if (!ret)
2165 goto retry;
2166 goto out;
Thomas Gleixnerc27f3922021-02-01 10:01:42 +00002167 case -EBUSY:
Darren Hart52400ba2009-04-03 13:40:49 -07002168 case -EAGAIN:
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00002169 /*
2170 * Two reasons for this:
Thomas Gleixnerc27f3922021-02-01 10:01:42 +00002171 * - EBUSY: Owner is exiting and we just wait for the
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00002172 * exit to complete.
Thomas Gleixnerc27f3922021-02-01 10:01:42 +00002173 * - EAGAIN: The user space value changed.
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00002174 */
Darren Hart52400ba2009-04-03 13:40:49 -07002175 double_unlock_hb(hb1, hb2);
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07002176 hb_waiters_dec(hb2);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002177 put_futex_key(&key2);
2178 put_futex_key(&key1);
Thomas Gleixnercf16e422021-02-01 10:01:43 +00002179 /*
2180 * Handle the case where the owner is in the middle of
2181 * exiting. Wait for the exit to complete otherwise
2182 * this task might loop forever, aka. live lock.
2183 */
2184 wait_for_owner_exiting(ret, exiting);
Darren Hart52400ba2009-04-03 13:40:49 -07002185 cond_resched();
2186 goto retry;
2187 default:
2188 goto out_unlock;
2189 }
2190 }
2191
Jason Low0d00c7b2014-01-12 15:31:22 -08002192 plist_for_each_entry_safe(this, next, &hb1->chain, list) {
Darren Hart52400ba2009-04-03 13:40:49 -07002193 if (task_count - nr_wake >= nr_requeue)
2194 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Darren Hart52400ba2009-04-03 13:40:49 -07002196 if (!match_futex(&this->key, &key1))
2197 continue;
2198
Darren Hart392741e2009-08-07 15:20:48 -07002199 /*
2200 * FUTEX_WAIT_REQEUE_PI and FUTEX_CMP_REQUEUE_PI should always
2201 * be paired with each other and no other futex ops.
Darren Hartaa109902012-11-26 16:29:56 -08002202 *
2203 * We should never be requeueing a futex_q with a pi_state,
2204 * which is awaiting a futex_unlock_pi().
Darren Hart392741e2009-08-07 15:20:48 -07002205 */
2206 if ((requeue_pi && !this->rt_waiter) ||
Darren Hartaa109902012-11-26 16:29:56 -08002207 (!requeue_pi && this->rt_waiter) ||
2208 this->pi_state) {
Darren Hart392741e2009-08-07 15:20:48 -07002209 ret = -EINVAL;
2210 break;
2211 }
Darren Hart52400ba2009-04-03 13:40:49 -07002212
2213 /*
2214 * Wake nr_wake waiters. For requeue_pi, if we acquired the
2215 * lock, we already woke the top_waiter. If not, it will be
2216 * woken by futex_unlock_pi().
2217 */
2218 if (++task_count <= nr_wake && !requeue_pi) {
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07002219 mark_wake_futex(&wake_q, this);
Darren Hart52400ba2009-04-03 13:40:49 -07002220 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 }
Darren Hart52400ba2009-04-03 13:40:49 -07002222
Darren Hart84bc4af2009-08-13 17:36:53 -07002223 /* Ensure we requeue to the expected futex for requeue_pi. */
2224 if (requeue_pi && !match_futex(this->requeue_pi_key, &key2)) {
2225 ret = -EINVAL;
2226 break;
2227 }
2228
Darren Hart52400ba2009-04-03 13:40:49 -07002229 /*
2230 * Requeue nr_requeue waiters and possibly one more in the case
2231 * of requeue_pi if we couldn't acquire the lock atomically.
2232 */
2233 if (requeue_pi) {
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00002234 /*
2235 * Prepare the waiter to take the rt_mutex. Take a
2236 * refcount on the pi_state and store the pointer in
2237 * the futex_q object of the waiter.
2238 */
Ben Hutchings921a7e32021-03-01 18:31:22 +01002239 get_pi_state(pi_state);
Darren Hart52400ba2009-04-03 13:40:49 -07002240 this->pi_state = pi_state;
2241 ret = rt_mutex_start_proxy_lock(&pi_state->pi_mutex,
2242 this->rt_waiter,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00002243 this->task);
Darren Hart52400ba2009-04-03 13:40:49 -07002244 if (ret == 1) {
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00002245 /*
2246 * We got the lock. We do neither drop the
2247 * refcount on pi_state nor clear
2248 * this->pi_state because the waiter needs the
2249 * pi_state for cleaning up the user space
2250 * value. It will drop the refcount after
2251 * doing so.
2252 */
Darren Hartbeda2c72009-08-09 15:34:39 -07002253 requeue_pi_wake_futex(this, &key2, hb2);
Darren Hart89061d32009-10-15 15:30:48 -07002254 drop_count++;
Darren Hart52400ba2009-04-03 13:40:49 -07002255 continue;
2256 } else if (ret) {
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00002257 /*
2258 * rt_mutex_start_proxy_lock() detected a
2259 * potential deadlock when we tried to queue
2260 * that waiter. Drop the pi_state reference
2261 * which we took above and remove the pointer
2262 * to the state from the waiters futex_q
2263 * object.
2264 */
Darren Hart52400ba2009-04-03 13:40:49 -07002265 this->pi_state = NULL;
Thomas Gleixner29e9ee52015-12-19 20:07:39 +00002266 put_pi_state(pi_state);
Thomas Gleixner885c2cb2015-12-19 20:07:41 +00002267 /*
2268 * We stop queueing more waiters and let user
2269 * space deal with the mess.
2270 */
2271 break;
Darren Hart52400ba2009-04-03 13:40:49 -07002272 }
2273 }
2274 requeue_futex(this, hb1, hb2, &key2);
2275 drop_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 }
2277
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00002278 /*
2279 * We took an extra initial reference to the pi_state either
2280 * in futex_proxy_trylock_atomic() or in lookup_pi_state(). We
2281 * need to drop it here again.
2282 */
Thomas Gleixner29e9ee52015-12-19 20:07:39 +00002283 put_pi_state(pi_state);
Thomas Gleixner885c2cb2015-12-19 20:07:41 +00002284
2285out_unlock:
Darren Hart5eb3dc62009-03-12 00:55:52 -07002286 double_unlock_hb(hb1, hb2);
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07002287 wake_up_q(&wake_q);
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07002288 hb_waiters_dec(hb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
Darren Hartcd84a422009-04-02 14:19:38 -07002290 /*
2291 * drop_futex_key_refs() must be called outside the spinlocks. During
2292 * the requeue we moved futex_q's from the hash bucket at key1 to the
2293 * one at key2 and updated their key pointer. We no longer need to
2294 * hold the references to key1.
2295 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 while (--drop_count >= 0)
Rusty Russell9adef582007-05-08 00:26:42 -07002297 drop_futex_key_refs(&key1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
Darren Hart42d35d42008-12-29 15:49:53 -08002299out_put_keys:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002300 put_futex_key(&key2);
Darren Hart42d35d42008-12-29 15:49:53 -08002301out_put_key1:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002302 put_futex_key(&key1);
Darren Hart42d35d42008-12-29 15:49:53 -08002303out:
Darren Hart52400ba2009-04-03 13:40:49 -07002304 return ret ? ret : task_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305}
2306
2307/* The key must be already stored in q->key. */
Eric Sesterhenn82af7ac2008-01-25 10:40:46 +01002308static inline struct futex_hash_bucket *queue_lock(struct futex_q *q)
Namhyung Kim15e408c2010-09-14 21:43:48 +09002309 __acquires(&hb->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310{
Ingo Molnare2970f22006-06-27 02:54:47 -07002311 struct futex_hash_bucket *hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Ingo Molnare2970f22006-06-27 02:54:47 -07002313 hb = hash_futex(&q->key);
Linus Torvalds11d46162014-03-20 22:11:17 -07002314
2315 /*
2316 * Increment the counter before taking the lock so that
2317 * a potential waker won't miss a to-be-slept task that is
2318 * waiting for the spinlock. This is safe as all queue_lock()
2319 * users end up calling queue_me(). Similarly, for housekeeping,
2320 * decrement the counter at queue_unlock() when some error has
2321 * occurred and we don't end up adding the task to the list.
2322 */
2323 hb_waiters_inc(hb);
2324
Ingo Molnare2970f22006-06-27 02:54:47 -07002325 q->lock_ptr = &hb->lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
Davidlohr Bueso8ad7b372016-02-09 11:15:13 -08002327 spin_lock(&hb->lock); /* implies smp_mb(); (A) */
Ingo Molnare2970f22006-06-27 02:54:47 -07002328 return hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329}
2330
Darren Hartd40d65c2009-09-21 22:30:15 -07002331static inline void
Jason Low0d00c7b2014-01-12 15:31:22 -08002332queue_unlock(struct futex_hash_bucket *hb)
Namhyung Kim15e408c2010-09-14 21:43:48 +09002333 __releases(&hb->lock)
Darren Hartd40d65c2009-09-21 22:30:15 -07002334{
2335 spin_unlock(&hb->lock);
Linus Torvalds11d46162014-03-20 22:11:17 -07002336 hb_waiters_dec(hb);
Darren Hartd40d65c2009-09-21 22:30:15 -07002337}
2338
Peter Zijlstra13c98b02021-03-28 22:41:42 +02002339static inline void __queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340{
Pierre Peifferec92d082007-05-09 02:35:00 -07002341 int prio;
2342
2343 /*
2344 * The priority used to register this element is
2345 * - either the real thread-priority for the real-time threads
2346 * (i.e. threads with a priority lower than MAX_RT_PRIO)
2347 * - or MAX_RT_PRIO for non-RT threads.
2348 * Thus, all RT-threads are woken first in priority order, and
2349 * the others are woken last, in FIFO order.
2350 */
2351 prio = min(current->normal_prio, MAX_RT_PRIO);
2352
2353 plist_node_init(&q->list, prio);
Pierre Peifferec92d082007-05-09 02:35:00 -07002354 plist_add(&q->list, &hb->chain);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002355 q->task = current;
Peter Zijlstra13c98b02021-03-28 22:41:42 +02002356}
2357
2358/**
2359 * queue_me() - Enqueue the futex_q on the futex_hash_bucket
2360 * @q: The futex_q to enqueue
2361 * @hb: The destination hash bucket
2362 *
2363 * The hb->lock must be held by the caller, and is released here. A call to
2364 * queue_me() is typically paired with exactly one call to unqueue_me(). The
2365 * exceptions involve the PI related operations, which may use unqueue_me_pi()
2366 * or nothing if the unqueue is done as part of the wake process and the unqueue
2367 * state is implicit in the state of woken task (see futex_wait_requeue_pi() for
2368 * an example).
2369 */
2370static inline void queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
2371 __releases(&hb->lock)
2372{
2373 __queue_me(q, hb);
Ingo Molnare2970f22006-06-27 02:54:47 -07002374 spin_unlock(&hb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375}
2376
Darren Hartd40d65c2009-09-21 22:30:15 -07002377/**
2378 * unqueue_me() - Remove the futex_q from its futex_hash_bucket
2379 * @q: The futex_q to unqueue
2380 *
2381 * The q->lock_ptr must not be held by the caller. A call to unqueue_me() must
2382 * be paired with exactly one earlier call to queue_me().
2383 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002384 * Return:
2385 * 1 - if the futex_q was still queued (and we removed unqueued it);
Darren Hartd40d65c2009-09-21 22:30:15 -07002386 * 0 - if the futex_q was already removed by the waking thread
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388static int unqueue_me(struct futex_q *q)
2389{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 spinlock_t *lock_ptr;
Ingo Molnare2970f22006-06-27 02:54:47 -07002391 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
2393 /* In the common case we don't take the spinlock, which is nice. */
Darren Hart42d35d42008-12-29 15:49:53 -08002394retry:
Jianyu Zhan29b75eb2016-03-07 09:32:24 +08002395 /*
2396 * q->lock_ptr can change between this read and the following spin_lock.
2397 * Use READ_ONCE to forbid the compiler from reloading q->lock_ptr and
2398 * optimizing lock_ptr out of the logic below.
2399 */
2400 lock_ptr = READ_ONCE(q->lock_ptr);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07002401 if (lock_ptr != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 spin_lock(lock_ptr);
2403 /*
2404 * q->lock_ptr can change between reading it and
2405 * spin_lock(), causing us to take the wrong lock. This
2406 * corrects the race condition.
2407 *
2408 * Reasoning goes like this: if we have the wrong lock,
2409 * q->lock_ptr must have changed (maybe several times)
2410 * between reading it and the spin_lock(). It can
2411 * change again after the spin_lock() but only if it was
2412 * already changed before the spin_lock(). It cannot,
2413 * however, change back to the original value. Therefore
2414 * we can detect whether we acquired the correct lock.
2415 */
2416 if (unlikely(lock_ptr != q->lock_ptr)) {
2417 spin_unlock(lock_ptr);
2418 goto retry;
2419 }
Lai Jiangshan2e129782010-12-22 14:18:50 +08002420 __unqueue_futex(q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002421
2422 BUG_ON(q->pi_state);
2423
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 spin_unlock(lock_ptr);
2425 ret = 1;
2426 }
2427
Rusty Russell9adef582007-05-08 00:26:42 -07002428 drop_futex_key_refs(&q->key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 return ret;
2430}
2431
Ingo Molnarc87e2832006-06-27 02:54:58 -07002432/*
2433 * PI futexes can not be requeued and must remove themself from the
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002434 * hash bucket. The hash bucket lock (i.e. lock_ptr) is held on entry
2435 * and dropped here.
Ingo Molnarc87e2832006-06-27 02:54:58 -07002436 */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002437static void unqueue_me_pi(struct futex_q *q)
Namhyung Kim15e408c2010-09-14 21:43:48 +09002438 __releases(q->lock_ptr)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002439{
Lai Jiangshan2e129782010-12-22 14:18:50 +08002440 __unqueue_futex(q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002441
2442 BUG_ON(!q->pi_state);
Thomas Gleixner29e9ee52015-12-19 20:07:39 +00002443 put_pi_state(q->pi_state);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002444 q->pi_state = NULL;
2445
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002446 spin_unlock(q->lock_ptr);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002447}
2448
Thomas Gleixner48ab8e82021-02-03 13:45:38 +00002449static int __fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
2450 struct task_struct *argowner)
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002451{
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002452 struct futex_pi_state *pi_state = q->pi_state;
Peter Zijlstra781691c2021-02-03 13:45:33 +00002453 struct task_struct *oldowner, *newowner;
Thomas Gleixner48ab8e82021-02-03 13:45:38 +00002454 u32 uval, curval, newval, newtid;
2455 int err = 0;
Peter Zijlstra781691c2021-02-03 13:45:33 +00002456
2457 oldowner = pi_state->owner;
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002458
2459 /*
Peter Zijlstra781691c2021-02-03 13:45:33 +00002460 * We are here because either:
2461 *
2462 * - we stole the lock and pi_state->owner needs updating to reflect
2463 * that (@argowner == current),
2464 *
2465 * or:
2466 *
2467 * - someone stole our lock and we need to fix things to point to the
2468 * new owner (@argowner == NULL).
2469 *
2470 * Either way, we have to replace the TID in the user space variable.
Lai Jiangshan81612392011-01-14 17:09:41 +08002471 * This must be atomic as we have to preserve the owner died bit here.
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002472 *
Darren Hartb2d09942009-03-12 00:55:37 -07002473 * Note: We write the user space value _before_ changing the pi_state
2474 * because we can fault here. Imagine swapped out pages or a fork
2475 * that marked all the anonymous memory readonly for cow.
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002476 *
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00002477 * Modifying pi_state _before_ the user space value would leave the
2478 * pi_state in an inconsistent state when we fault here, because we
2479 * need to drop the locks to handle the fault. This might be observed
2480 * in the PID check in lookup_pi_state.
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002481 */
2482retry:
Peter Zijlstra781691c2021-02-03 13:45:33 +00002483 if (!argowner) {
2484 if (oldowner != current) {
2485 /*
2486 * We raced against a concurrent self; things are
2487 * already fixed up. Nothing to do.
2488 */
2489 return 0;
2490 }
2491
2492 if (__rt_mutex_futex_trylock(&pi_state->pi_mutex)) {
Thomas Gleixnerad8fdba2021-02-11 09:26:58 +00002493 /* We got the lock. pi_state is correct. Tell caller. */
Thomas Gleixner48ab8e82021-02-03 13:45:38 +00002494 return 1;
Peter Zijlstra781691c2021-02-03 13:45:33 +00002495 }
2496
2497 /*
2498 * Since we just failed the trylock; there must be an owner.
2499 */
2500 newowner = rt_mutex_owner(&pi_state->pi_mutex);
2501 BUG_ON(!newowner);
2502 } else {
2503 WARN_ON_ONCE(argowner != current);
2504 if (oldowner == current) {
2505 /*
2506 * We raced against a concurrent self; things are
2507 * already fixed up. Nothing to do.
2508 */
Thomas Gleixner48ab8e82021-02-03 13:45:38 +00002509 return 1;
Peter Zijlstra781691c2021-02-03 13:45:33 +00002510 }
2511 newowner = argowner;
2512 }
2513
2514 newtid = task_pid_vnr(newowner) | FUTEX_WAITERS;
Peter Zijlstra0d351802018-01-22 11:39:47 +01002515 /* Owner died? */
2516 if (!pi_state->owner)
2517 newtid |= FUTEX_OWNER_DIED;
Peter Zijlstra781691c2021-02-03 13:45:33 +00002518
Will Deacon8682c2e2021-03-28 22:42:44 +02002519 err = get_futex_value_locked(&uval, uaddr);
2520 if (err)
2521 goto handle_err;
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002522
Ben Hutchings312d9d62021-03-01 18:31:30 +01002523 for (;;) {
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002524 newval = (uval & FUTEX_OWNER_DIED) | newtid;
2525
Will Deacon8682c2e2021-03-28 22:42:44 +02002526 err = cmpxchg_futex_value_locked(&curval, uaddr, uval, newval);
2527 if (err)
2528 goto handle_err;
2529
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002530 if (curval == uval)
2531 break;
2532 uval = curval;
2533 }
2534
2535 /*
2536 * We fixed up user space. Now we need to fix the pi_state
2537 * itself.
2538 */
Thomas Gleixner76bc0ec2021-02-03 13:45:35 +00002539 pi_state_update_owner(pi_state, newowner);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002540
Thomas Gleixnerad8fdba2021-02-11 09:26:58 +00002541 return argowner == current;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002542
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002543 /*
Will Deacon8682c2e2021-03-28 22:42:44 +02002544 * In order to reschedule or handle a page fault, we need to drop the
2545 * locks here. In the case of a fault, this gives the other task
2546 * (either the highest priority waiter itself or the task which stole
2547 * the rtmutex) the chance to try the fixup of the pi_state. So once we
2548 * are back from handling the fault we need to check the pi_state after
2549 * reacquiring the locks and before trying to do another fixup. When
2550 * the fixup has been done already we simply return.
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00002551 *
2552 * Note: we hold both hb->lock and pi_mutex->wait_lock. We can safely
2553 * drop hb->lock since the caller owns the hb -> futex_q relation.
2554 * Dropping the pi_mutex->wait_lock requires the state revalidate.
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002555 */
Will Deacon8682c2e2021-03-28 22:42:44 +02002556handle_err:
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00002557 raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002558 spin_unlock(q->lock_ptr);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002559
Will Deacon8682c2e2021-03-28 22:42:44 +02002560 switch (err) {
2561 case -EFAULT:
2562 err = fault_in_user_writeable(uaddr);
2563 break;
2564
2565 case -EAGAIN:
2566 cond_resched();
2567 err = 0;
2568 break;
2569
2570 default:
2571 WARN_ON_ONCE(1);
2572 break;
2573 }
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002574
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002575 spin_lock(q->lock_ptr);
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00002576 raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002577
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002578 /*
2579 * Check if someone else fixed it for us:
2580 */
2581 if (pi_state->owner != oldowner)
Thomas Gleixner48ab8e82021-02-03 13:45:38 +00002582 return argowner == current;
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002583
Thomas Gleixner48ab8e82021-02-03 13:45:38 +00002584 /* Retry if err was -EAGAIN or the fault in succeeded */
2585 if (!err)
2586 goto retry;
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002587
Thomas Gleixnerb960d9a2021-02-03 13:45:39 +00002588 /*
2589 * fault_in_user_writeable() failed so user state is immutable. At
2590 * best we can make the kernel state consistent but user state will
2591 * be most likely hosed and any subsequent unlock operation will be
2592 * rejected due to PI futex rule [10].
2593 *
2594 * Ensure that the rtmutex owner is also the pi_state owner despite
2595 * the user space value claiming something different. There is no
2596 * point in unlocking the rtmutex if current is the owner as it
2597 * would need to wait until the next waiter has taken the rtmutex
2598 * to guarantee consistent state. Keep it simple. Userspace asked
2599 * for this wreckaged state.
2600 *
2601 * The rtmutex has an owner - either current or some other
2602 * task. See the EAGAIN loop above.
2603 */
2604 pi_state_update_owner(pi_state, rt_mutex_owner(&pi_state->pi_mutex));
2605
Thomas Gleixner48ab8e82021-02-03 13:45:38 +00002606 return err;
2607}
2608
2609static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
2610 struct task_struct *argowner)
2611{
2612 struct futex_pi_state *pi_state = q->pi_state;
2613 int ret;
2614
2615 lockdep_assert_held(q->lock_ptr);
2616
2617 raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
2618 ret = __fixup_pi_state_owner(uaddr, q, argowner);
2619 raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
2620 return ret;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002621}
2622
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002623static long futex_wait_restart(struct restart_block *restart);
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07002624
Darren Hartca5f9522009-04-03 13:39:33 -07002625/**
Darren Hartdd973992009-04-03 13:40:02 -07002626 * fixup_owner() - Post lock pi_state and corner case management
2627 * @uaddr: user address of the futex
Darren Hartdd973992009-04-03 13:40:02 -07002628 * @q: futex_q (contains pi_state and access to the rt_mutex)
2629 * @locked: if the attempt to take the rt_mutex succeeded (1) or not (0)
2630 *
2631 * After attempting to lock an rt_mutex, this function is called to cleanup
2632 * the pi_state owner as well as handle race conditions that may allow us to
2633 * acquire the lock. Must be called with the hb lock held.
2634 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002635 * Return:
2636 * 1 - success, lock taken;
2637 * 0 - success, lock not taken;
Darren Hartdd973992009-04-03 13:40:02 -07002638 * <0 - on error (-EFAULT)
2639 */
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002640static int fixup_owner(u32 __user *uaddr, struct futex_q *q, int locked)
Darren Hartdd973992009-04-03 13:40:02 -07002641{
Darren Hartdd973992009-04-03 13:40:02 -07002642 if (locked) {
2643 /*
2644 * Got the lock. We might not be the anticipated owner if we
2645 * did a lock-steal - fix up the PI-state in that case:
Peter Zijlstra781691c2021-02-03 13:45:33 +00002646 *
2647 * Speculative pi_state->owner read (we don't hold wait_lock);
2648 * since we own the lock pi_state->owner == current is the
2649 * stable state, anything else needs more attention.
Darren Hartdd973992009-04-03 13:40:02 -07002650 */
2651 if (q->pi_state->owner != current)
Thomas Gleixnerad8fdba2021-02-11 09:26:58 +00002652 return fixup_pi_state_owner(uaddr, q, current);
2653 return 1;
Darren Hartdd973992009-04-03 13:40:02 -07002654 }
2655
2656 /*
Peter Zijlstra781691c2021-02-03 13:45:33 +00002657 * If we didn't get the lock; check if anybody stole it from us. In
2658 * that case, we need to fix up the uval to point to them instead of
2659 * us, otherwise bad things happen. [10]
2660 *
2661 * Another speculative read; pi_state->owner == current is unstable
2662 * but needs our attention.
2663 */
Thomas Gleixnerad8fdba2021-02-11 09:26:58 +00002664 if (q->pi_state->owner == current)
2665 return fixup_pi_state_owner(uaddr, q, NULL);
Peter Zijlstra781691c2021-02-03 13:45:33 +00002666
2667 /*
Darren Hartdd973992009-04-03 13:40:02 -07002668 * Paranoia check. If we did not take the lock, then we should not be
Thomas Gleixner083895e2021-02-03 13:45:34 +00002669 * the owner of the rt_mutex. Warn and establish consistent state.
Darren Hartdd973992009-04-03 13:40:02 -07002670 */
Thomas Gleixner083895e2021-02-03 13:45:34 +00002671 if (WARN_ON_ONCE(rt_mutex_owner(&q->pi_state->pi_mutex) == current))
2672 return fixup_pi_state_owner(uaddr, q, current);
Darren Hartdd973992009-04-03 13:40:02 -07002673
Thomas Gleixnerad8fdba2021-02-11 09:26:58 +00002674 return 0;
Darren Hartdd973992009-04-03 13:40:02 -07002675}
2676
2677/**
Darren Hartca5f9522009-04-03 13:39:33 -07002678 * futex_wait_queue_me() - queue_me() and wait for wakeup, timeout, or signal
2679 * @hb: the futex hash bucket, must be locked by the caller
2680 * @q: the futex_q to queue up on
2681 * @timeout: the prepared hrtimer_sleeper, or null for no timeout
Darren Hartca5f9522009-04-03 13:39:33 -07002682 */
2683static void futex_wait_queue_me(struct futex_hash_bucket *hb, struct futex_q *q,
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02002684 struct hrtimer_sleeper *timeout)
Darren Hartca5f9522009-04-03 13:39:33 -07002685{
Darren Hart9beba3c2009-09-24 11:54:47 -07002686 /*
2687 * The task state is guaranteed to be set before another task can
Peter Zijlstrab92b8b32015-05-12 10:51:55 +02002688 * wake it. set_current_state() is implemented using smp_store_mb() and
Darren Hart9beba3c2009-09-24 11:54:47 -07002689 * queue_me() calls spin_unlock() upon completion, both serializing
2690 * access to the hash list and forcing another memory barrier.
2691 */
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02002692 set_current_state(TASK_INTERRUPTIBLE);
Darren Hart0729e192009-09-21 22:30:38 -07002693 queue_me(q, hb);
Darren Hartca5f9522009-04-03 13:39:33 -07002694
2695 /* Arm the timer */
Thomas Gleixner2e4b0d32015-04-14 21:09:13 +00002696 if (timeout)
Darren Hartca5f9522009-04-03 13:39:33 -07002697 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
Darren Hartca5f9522009-04-03 13:39:33 -07002698
2699 /*
Darren Hart0729e192009-09-21 22:30:38 -07002700 * If we have been removed from the hash list, then another task
2701 * has tried to wake us, and we can skip the call to schedule().
Darren Hartca5f9522009-04-03 13:39:33 -07002702 */
2703 if (likely(!plist_node_empty(&q->list))) {
2704 /*
2705 * If the timer has already expired, current will already be
2706 * flagged for rescheduling. Only call schedule if there
2707 * is no timeout, or if it has yet to expire.
2708 */
2709 if (!timeout || timeout->task)
Colin Cross88c80042013-05-01 18:35:05 -07002710 freezable_schedule();
Darren Hartca5f9522009-04-03 13:39:33 -07002711 }
2712 __set_current_state(TASK_RUNNING);
2713}
2714
Darren Hartf8010732009-04-03 13:40:40 -07002715/**
2716 * futex_wait_setup() - Prepare to wait on a futex
2717 * @uaddr: the futex userspace address
2718 * @val: the expected value
Darren Hartb41277d2010-11-08 13:10:09 -08002719 * @flags: futex flags (FLAGS_SHARED, etc.)
Darren Hartf8010732009-04-03 13:40:40 -07002720 * @q: the associated futex_q
2721 * @hb: storage for hash_bucket pointer to be returned to caller
2722 *
2723 * Setup the futex_q and locate the hash_bucket. Get the futex value and
2724 * compare it with the expected value. Handle atomic faults internally.
2725 * Return with the hb lock held and a q.key reference on success, and unlocked
2726 * with no q.key reference on failure.
2727 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002728 * Return:
2729 * 0 - uaddr contains val and hb has been locked;
Bart Van Asscheca4a04c2011-07-17 09:01:00 +02002730 * <1 - -EFAULT or -EWOULDBLOCK (uaddr does not contain val) and hb is unlocked
Darren Hartf8010732009-04-03 13:40:40 -07002731 */
Darren Hartb41277d2010-11-08 13:10:09 -08002732static int futex_wait_setup(u32 __user *uaddr, u32 val, unsigned int flags,
Darren Hartf8010732009-04-03 13:40:40 -07002733 struct futex_q *q, struct futex_hash_bucket **hb)
2734{
2735 u32 uval;
2736 int ret;
2737
2738 /*
2739 * Access the page AFTER the hash-bucket is locked.
2740 * Order is important:
2741 *
2742 * Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val);
2743 * Userspace waker: if (cond(var)) { var = new; futex_wake(&var); }
2744 *
2745 * The basic logical guarantee of a futex is that it blocks ONLY
2746 * if cond(var) is known to be true at the time of blocking, for
Michel Lespinasse8fe8f542011-03-06 18:07:50 -08002747 * any cond. If we locked the hash-bucket after testing *uaddr, that
2748 * would open a race condition where we could block indefinitely with
Darren Hartf8010732009-04-03 13:40:40 -07002749 * cond(var) false, which would violate the guarantee.
2750 *
Michel Lespinasse8fe8f542011-03-06 18:07:50 -08002751 * On the other hand, we insert q and release the hash-bucket only
2752 * after testing *uaddr. This guarantees that futex_wait() will NOT
2753 * absorb a wakeup if *uaddr does not match the desired values
2754 * while the syscall executes.
Darren Hartf8010732009-04-03 13:40:40 -07002755 */
2756retry:
Shawn Bohrer9ea71502011-06-30 11:21:32 -05002757 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q->key, VERIFY_READ);
Darren Hartf8010732009-04-03 13:40:40 -07002758 if (unlikely(ret != 0))
Darren Harta5a2a0c2009-04-10 09:50:05 -07002759 return ret;
Darren Hartf8010732009-04-03 13:40:40 -07002760
2761retry_private:
2762 *hb = queue_lock(q);
2763
2764 ret = get_futex_value_locked(&uval, uaddr);
2765
2766 if (ret) {
Jason Low0d00c7b2014-01-12 15:31:22 -08002767 queue_unlock(*hb);
Darren Hartf8010732009-04-03 13:40:40 -07002768
2769 ret = get_user(uval, uaddr);
2770 if (ret)
2771 goto out;
2772
Darren Hartb41277d2010-11-08 13:10:09 -08002773 if (!(flags & FLAGS_SHARED))
Darren Hartf8010732009-04-03 13:40:40 -07002774 goto retry_private;
2775
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002776 put_futex_key(&q->key);
Darren Hartf8010732009-04-03 13:40:40 -07002777 goto retry;
2778 }
2779
2780 if (uval != val) {
Jason Low0d00c7b2014-01-12 15:31:22 -08002781 queue_unlock(*hb);
Darren Hartf8010732009-04-03 13:40:40 -07002782 ret = -EWOULDBLOCK;
2783 }
2784
2785out:
2786 if (ret)
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002787 put_futex_key(&q->key);
Darren Hartf8010732009-04-03 13:40:40 -07002788 return ret;
2789}
2790
Darren Hartb41277d2010-11-08 13:10:09 -08002791static int futex_wait(u32 __user *uaddr, unsigned int flags, u32 val,
2792 ktime_t *abs_time, u32 bitset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793{
Darren Hartca5f9522009-04-03 13:39:33 -07002794 struct hrtimer_sleeper timeout, *to = NULL;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002795 struct restart_block *restart;
Ingo Molnare2970f22006-06-27 02:54:47 -07002796 struct futex_hash_bucket *hb;
Darren Hart5bdb05f2010-11-08 13:40:28 -08002797 struct futex_q q = futex_q_init;
Ingo Molnare2970f22006-06-27 02:54:47 -07002798 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
Thomas Gleixnercd689982008-02-01 17:45:14 +01002800 if (!bitset)
2801 return -EINVAL;
Thomas Gleixnercd689982008-02-01 17:45:14 +01002802 q.bitset = bitset;
Darren Hartca5f9522009-04-03 13:39:33 -07002803
2804 if (abs_time) {
2805 to = &timeout;
2806
Darren Hartb41277d2010-11-08 13:10:09 -08002807 hrtimer_init_on_stack(&to->timer, (flags & FLAGS_CLOCKRT) ?
2808 CLOCK_REALTIME : CLOCK_MONOTONIC,
2809 HRTIMER_MODE_ABS);
Darren Hartca5f9522009-04-03 13:39:33 -07002810 hrtimer_init_sleeper(to, current);
2811 hrtimer_set_expires_range_ns(&to->timer, *abs_time,
2812 current->timer_slack_ns);
2813 }
2814
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002815retry:
Darren Hart7ada8762010-10-17 08:35:04 -07002816 /*
2817 * Prepare to wait on uaddr. On success, holds hb lock and increments
2818 * q.key refs.
2819 */
Darren Hartb41277d2010-11-08 13:10:09 -08002820 ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
Darren Hartf8010732009-04-03 13:40:40 -07002821 if (ret)
Darren Hart42d35d42008-12-29 15:49:53 -08002822 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Darren Hartca5f9522009-04-03 13:39:33 -07002824 /* queue_me and wait for wakeup, timeout, or a signal. */
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02002825 futex_wait_queue_me(hb, &q, to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
2827 /* If we were woken (and unqueued), we succeeded, whatever. */
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002828 ret = 0;
Darren Hart7ada8762010-10-17 08:35:04 -07002829 /* unqueue_me() drops q.key ref */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 if (!unqueue_me(&q))
Darren Hart7ada8762010-10-17 08:35:04 -07002831 goto out;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002832 ret = -ETIMEDOUT;
Darren Hartca5f9522009-04-03 13:39:33 -07002833 if (to && !to->task)
Darren Hart7ada8762010-10-17 08:35:04 -07002834 goto out;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002835
Ingo Molnare2970f22006-06-27 02:54:47 -07002836 /*
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002837 * We expect signal_pending(current), but we might be the
2838 * victim of a spurious wakeup as well.
Ingo Molnare2970f22006-06-27 02:54:47 -07002839 */
Darren Hart7ada8762010-10-17 08:35:04 -07002840 if (!signal_pending(current))
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002841 goto retry;
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002842
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002843 ret = -ERESTARTSYS;
Pierre Peifferc19384b2007-05-09 02:35:02 -07002844 if (!abs_time)
Darren Hart7ada8762010-10-17 08:35:04 -07002845 goto out;
Steven Rostedtce6bd422007-12-05 15:46:09 +01002846
Andy Lutomirskif56141e2015-02-12 15:01:14 -08002847 restart = &current->restart_block;
Namhyung Kima3c74c52010-09-14 21:43:47 +09002848 restart->futex.uaddr = uaddr;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002849 restart->futex.val = val;
2850 restart->futex.time = abs_time->tv64;
2851 restart->futex.bitset = bitset;
Darren Hart0cd9c642011-04-14 15:41:57 -07002852 restart->futex.flags = flags | FLAGS_HAS_TIMEOUT;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002853
Oleg Nesterov376a76a2021-02-01 18:46:41 +01002854 ret = set_restart_fn(restart, futex_wait_restart);
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002855
Darren Hart42d35d42008-12-29 15:49:53 -08002856out:
Darren Hartca5f9522009-04-03 13:39:33 -07002857 if (to) {
2858 hrtimer_cancel(&to->timer);
2859 destroy_hrtimer_on_stack(&to->timer);
2860 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07002861 return ret;
2862}
2863
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002864
2865static long futex_wait_restart(struct restart_block *restart)
2866{
Namhyung Kima3c74c52010-09-14 21:43:47 +09002867 u32 __user *uaddr = restart->futex.uaddr;
Darren Harta72188d2009-04-03 13:40:22 -07002868 ktime_t t, *tp = NULL;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002869
Darren Harta72188d2009-04-03 13:40:22 -07002870 if (restart->futex.flags & FLAGS_HAS_TIMEOUT) {
2871 t.tv64 = restart->futex.time;
2872 tp = &t;
2873 }
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002874 restart->fn = do_no_restart_syscall;
Darren Hartb41277d2010-11-08 13:10:09 -08002875
2876 return (long)futex_wait(uaddr, restart->futex.flags,
2877 restart->futex.val, tp, restart->futex.bitset);
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002878}
2879
2880
Ingo Molnarc87e2832006-06-27 02:54:58 -07002881/*
2882 * Userspace tried a 0 -> TID atomic transition of the futex value
2883 * and failed. The kernel side here does the whole locking operation:
Davidlohr Bueso767f5092015-06-29 23:26:01 -07002884 * if there are waiters then it will block as a consequence of relying
2885 * on rt-mutexes, it does PI, etc. (Due to races the kernel might see
2886 * a 0 value of the futex too.).
2887 *
2888 * Also serves as futex trylock_pi()'ing, and due semantics.
Ingo Molnarc87e2832006-06-27 02:54:58 -07002889 */
Michael Kerrisk996636d2015-01-16 20:28:06 +01002890static int futex_lock_pi(u32 __user *uaddr, unsigned int flags,
Darren Hartb41277d2010-11-08 13:10:09 -08002891 ktime_t *time, int trylock)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002892{
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07002893 struct hrtimer_sleeper timeout, *to = NULL;
Thomas Gleixnercf16e422021-02-01 10:01:43 +00002894 struct task_struct *exiting = NULL;
Peter Zijlstra13c98b02021-03-28 22:41:42 +02002895 struct rt_mutex_waiter rt_waiter;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002896 struct futex_hash_bucket *hb;
Darren Hart5bdb05f2010-11-08 13:40:28 -08002897 struct futex_q q = futex_q_init;
Darren Hartdd973992009-04-03 13:40:02 -07002898 int res, ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002899
2900 if (refill_pi_state_cache())
2901 return -ENOMEM;
2902
Pierre Peifferc19384b2007-05-09 02:35:02 -07002903 if (time) {
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07002904 to = &timeout;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07002905 hrtimer_init_on_stack(&to->timer, CLOCK_REALTIME,
2906 HRTIMER_MODE_ABS);
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07002907 hrtimer_init_sleeper(to, current);
Arjan van de Vencc584b22008-09-01 15:02:30 -07002908 hrtimer_set_expires(&to->timer, *time);
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07002909 }
2910
Darren Hart42d35d42008-12-29 15:49:53 -08002911retry:
Shawn Bohrer9ea71502011-06-30 11:21:32 -05002912 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q.key, VERIFY_WRITE);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002913 if (unlikely(ret != 0))
Darren Hart42d35d42008-12-29 15:49:53 -08002914 goto out;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002915
Darren Harte4dc5b72009-03-12 00:56:13 -07002916retry_private:
Eric Sesterhenn82af7ac2008-01-25 10:40:46 +01002917 hb = queue_lock(&q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002918
Thomas Gleixnercf16e422021-02-01 10:01:43 +00002919 ret = futex_lock_pi_atomic(uaddr, hb, &q.key, &q.pi_state, current,
2920 &exiting, 0);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002921 if (unlikely(ret)) {
Davidlohr Bueso767f5092015-06-29 23:26:01 -07002922 /*
2923 * Atomic work succeeded and we got the lock,
2924 * or failed. Either way, we do _not_ block.
2925 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002926 switch (ret) {
Darren Hart1a520842009-04-03 13:39:52 -07002927 case 1:
2928 /* We got the lock. */
2929 ret = 0;
2930 goto out_unlock_put_key;
2931 case -EFAULT:
2932 goto uaddr_faulted;
Thomas Gleixnerc27f3922021-02-01 10:01:42 +00002933 case -EBUSY:
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002934 case -EAGAIN:
2935 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00002936 * Two reasons for this:
Thomas Gleixnerc27f3922021-02-01 10:01:42 +00002937 * - EBUSY: Task is exiting and we just wait for the
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00002938 * exit to complete.
Thomas Gleixnerc27f3922021-02-01 10:01:42 +00002939 * - EAGAIN: The user space value changed.
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002940 */
Jason Low0d00c7b2014-01-12 15:31:22 -08002941 queue_unlock(hb);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002942 put_futex_key(&q.key);
Thomas Gleixnercf16e422021-02-01 10:01:43 +00002943 /*
2944 * Handle the case where the owner is in the middle of
2945 * exiting. Wait for the exit to complete otherwise
2946 * this task might loop forever, aka. live lock.
2947 */
2948 wait_for_owner_exiting(ret, exiting);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002949 cond_resched();
2950 goto retry;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002951 default:
Darren Hart42d35d42008-12-29 15:49:53 -08002952 goto out_unlock_put_key;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002953 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07002954 }
2955
Peter Zijlstra13c98b02021-03-28 22:41:42 +02002956 WARN_ON(!q.pi_state);
2957
Ingo Molnarc87e2832006-06-27 02:54:58 -07002958 /*
2959 * Only actually queue now that the atomic ops are done:
2960 */
Peter Zijlstra13c98b02021-03-28 22:41:42 +02002961 __queue_me(&q, hb);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002962
Peter Zijlstra13c98b02021-03-28 22:41:42 +02002963 if (trylock) {
Peter Zijlstra2c60d4a2021-02-03 13:45:30 +00002964 ret = rt_mutex_futex_trylock(&q.pi_state->pi_mutex);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002965 /* Fixup the trylock return value: */
2966 ret = ret ? 0 : -EWOULDBLOCK;
Peter Zijlstra13c98b02021-03-28 22:41:42 +02002967 goto no_block;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002968 }
2969
Peter Zijlstra13c98b02021-03-28 22:41:42 +02002970 rt_mutex_init_waiter(&rt_waiter);
Peter Zijlstrafc9f98f2021-03-28 22:41:51 +02002971
2972 /*
2973 * On PREEMPT_RT_FULL, when hb->lock becomes an rt_mutex, we must not
2974 * hold it while doing rt_mutex_start_proxy(), because then it will
2975 * include hb->lock in the blocking chain, even through we'll not in
2976 * fact hold it while blocking. This will lead it to report -EDEADLK
2977 * and BUG when futex_unlock_pi() interleaves with this.
2978 *
2979 * Therefore acquire wait_lock while holding hb->lock, but drop the
Thomas Gleixnerb4f92d82021-03-28 22:42:20 +02002980 * latter before calling __rt_mutex_start_proxy_lock(). This
2981 * interleaves with futex_unlock_pi() -- which does a similar lock
2982 * handoff -- such that the latter can observe the futex_q::pi_state
2983 * before __rt_mutex_start_proxy_lock() is done.
Peter Zijlstrafc9f98f2021-03-28 22:41:51 +02002984 */
2985 raw_spin_lock_irq(&q.pi_state->pi_mutex.wait_lock);
2986 spin_unlock(q.lock_ptr);
Thomas Gleixnerb4f92d82021-03-28 22:42:20 +02002987 /*
2988 * __rt_mutex_start_proxy_lock() unconditionally enqueues the @rt_waiter
2989 * such that futex_unlock_pi() is guaranteed to observe the waiter when
2990 * it sees the futex_q::pi_state.
2991 */
Peter Zijlstrafc9f98f2021-03-28 22:41:51 +02002992 ret = __rt_mutex_start_proxy_lock(&q.pi_state->pi_mutex, &rt_waiter, current);
2993 raw_spin_unlock_irq(&q.pi_state->pi_mutex.wait_lock);
2994
Peter Zijlstra13c98b02021-03-28 22:41:42 +02002995 if (ret) {
2996 if (ret == 1)
2997 ret = 0;
Thomas Gleixnerb4f92d82021-03-28 22:42:20 +02002998 goto cleanup;
Peter Zijlstra13c98b02021-03-28 22:41:42 +02002999 }
3000
Peter Zijlstra13c98b02021-03-28 22:41:42 +02003001 if (unlikely(to))
3002 hrtimer_start_expires(&to->timer, HRTIMER_MODE_ABS);
3003
3004 ret = rt_mutex_wait_proxy_lock(&q.pi_state->pi_mutex, to, &rt_waiter);
3005
Thomas Gleixnerb4f92d82021-03-28 22:42:20 +02003006cleanup:
Vernon Mauerya99e4e42006-07-01 04:35:42 -07003007 spin_lock(q.lock_ptr);
Darren Hartdd973992009-04-03 13:40:02 -07003008 /*
Thomas Gleixnerb4f92d82021-03-28 22:42:20 +02003009 * If we failed to acquire the lock (deadlock/signal/timeout), we must
Peter Zijlstra13c98b02021-03-28 22:41:42 +02003010 * first acquire the hb->lock before removing the lock from the
Thomas Gleixnerb4f92d82021-03-28 22:42:20 +02003011 * rt_mutex waitqueue, such that we can keep the hb and rt_mutex wait
3012 * lists consistent.
Peter Zijlstrafc9f98f2021-03-28 22:41:51 +02003013 *
3014 * In particular; it is important that futex_unlock_pi() can not
3015 * observe this inconsistency.
Peter Zijlstra13c98b02021-03-28 22:41:42 +02003016 */
3017 if (ret && !rt_mutex_cleanup_proxy_lock(&q.pi_state->pi_mutex, &rt_waiter))
3018 ret = 0;
3019
3020no_block:
3021 /*
Darren Hartdd973992009-04-03 13:40:02 -07003022 * Fixup the pi_state owner and possibly acquire the lock if we
3023 * haven't already.
3024 */
Thomas Gleixnerae791a22010-11-10 13:30:36 +01003025 res = fixup_owner(uaddr, &q, !ret);
Darren Hartdd973992009-04-03 13:40:02 -07003026 /*
3027 * If fixup_owner() returned an error, proprogate that. If it acquired
3028 * the lock, clear our -ETIMEDOUT or -EINTR.
3029 */
3030 if (res)
3031 ret = (res < 0) ? res : 0;
Ingo Molnarc87e2832006-06-27 02:54:58 -07003032
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07003033 /* Unqueue and drop the lock */
3034 unqueue_me_pi(&q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003035
Mikael Pettersson5ecb01c2010-01-23 22:36:29 +01003036 goto out_put_key;
Ingo Molnarc87e2832006-06-27 02:54:58 -07003037
Darren Hart42d35d42008-12-29 15:49:53 -08003038out_unlock_put_key:
Jason Low0d00c7b2014-01-12 15:31:22 -08003039 queue_unlock(hb);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003040
Darren Hart42d35d42008-12-29 15:49:53 -08003041out_put_key:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01003042 put_futex_key(&q.key);
Darren Hart42d35d42008-12-29 15:49:53 -08003043out:
Thomas Gleixner85de4712021-03-28 22:42:00 +02003044 if (to) {
3045 hrtimer_cancel(&to->timer);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07003046 destroy_hrtimer_on_stack(&to->timer);
Thomas Gleixner85de4712021-03-28 22:42:00 +02003047 }
Darren Hartdd973992009-04-03 13:40:02 -07003048 return ret != -EINTR ? ret : -ERESTARTNOINTR;
Ingo Molnarc87e2832006-06-27 02:54:58 -07003049
Darren Hart42d35d42008-12-29 15:49:53 -08003050uaddr_faulted:
Jason Low0d00c7b2014-01-12 15:31:22 -08003051 queue_unlock(hb);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07003052
Thomas Gleixnerd0725992009-06-11 23:15:43 +02003053 ret = fault_in_user_writeable(uaddr);
Darren Harte4dc5b72009-03-12 00:56:13 -07003054 if (ret)
3055 goto out_put_key;
Ingo Molnarc87e2832006-06-27 02:54:58 -07003056
Darren Hartb41277d2010-11-08 13:10:09 -08003057 if (!(flags & FLAGS_SHARED))
Darren Harte4dc5b72009-03-12 00:56:13 -07003058 goto retry_private;
3059
Thomas Gleixnerae791a22010-11-10 13:30:36 +01003060 put_futex_key(&q.key);
Darren Harte4dc5b72009-03-12 00:56:13 -07003061 goto retry;
Ingo Molnarc87e2832006-06-27 02:54:58 -07003062}
3063
3064/*
Ingo Molnarc87e2832006-06-27 02:54:58 -07003065 * Userspace attempted a TID -> 0 atomic transition, and failed.
3066 * This is the in-kernel slowpath: we look up the PI state (if any),
3067 * and do the rt-mutex unlock.
3068 */
Darren Hartb41277d2010-11-08 13:10:09 -08003069static int futex_unlock_pi(u32 __user *uaddr, unsigned int flags)
Ingo Molnarc87e2832006-06-27 02:54:58 -07003070{
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00003071 u32 uninitialized_var(curval), uval, vpid = task_pid_vnr(current);
Peter Zijlstra38d47c12008-09-26 19:32:20 +02003072 union futex_key key = FUTEX_KEY_INIT;
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00003073 struct futex_hash_bucket *hb;
Ben Hutchingsbfefc9e2021-03-01 18:30:39 +01003074 struct futex_q *top_waiter;
Darren Harte4dc5b72009-03-12 00:56:13 -07003075 int ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -07003076
3077retry:
3078 if (get_user(uval, uaddr))
3079 return -EFAULT;
3080 /*
3081 * We release only a lock we actually own:
3082 */
Thomas Gleixnerc0c9ed12011-03-11 11:51:22 +01003083 if ((uval & FUTEX_TID_MASK) != vpid)
Ingo Molnarc87e2832006-06-27 02:54:58 -07003084 return -EPERM;
Ingo Molnarc87e2832006-06-27 02:54:58 -07003085
Shawn Bohrer9ea71502011-06-30 11:21:32 -05003086 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, VERIFY_WRITE);
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00003087 if (ret)
3088 return ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -07003089
3090 hb = hash_futex(&key);
3091 spin_lock(&hb->lock);
3092
Ingo Molnarc87e2832006-06-27 02:54:58 -07003093 /*
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00003094 * Check waiters first. We do not trust user space values at
3095 * all and we at least want to know if user space fiddled
3096 * with the futex value instead of blindly unlocking.
Ingo Molnarc87e2832006-06-27 02:54:58 -07003097 */
Ben Hutchingsbfefc9e2021-03-01 18:30:39 +01003098 top_waiter = futex_top_waiter(hb, &key);
3099 if (top_waiter) {
Ben Hutchings312d9d62021-03-01 18:31:30 +01003100 struct futex_pi_state *pi_state = top_waiter->pi_state;
3101
3102 ret = -EINVAL;
3103 if (!pi_state)
3104 goto out_unlock;
3105
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02003106 /*
Ben Hutchings312d9d62021-03-01 18:31:30 +01003107 * If current does not own the pi_state then the futex is
3108 * inconsistent and user space fiddled with the futex value.
3109 */
3110 if (pi_state->owner != current)
3111 goto out_unlock;
3112
Ben Hutchings312d9d62021-03-01 18:31:30 +01003113 get_pi_state(pi_state);
Ben Hutchings25a678d2021-03-01 18:31:39 +01003114 /*
Ben Hutchings25a678d2021-03-01 18:31:39 +01003115 * By taking wait_lock while still holding hb->lock, we ensure
3116 * there is no point where we hold neither; and therefore
3117 * wake_futex_pi() must observe a state consistent with what we
3118 * observed.
Thomas Gleixnerb4f92d82021-03-28 22:42:20 +02003119 *
3120 * In particular; this forces __rt_mutex_start_proxy() to
3121 * complete such that we're guaranteed to observe the
3122 * rt_waiter. Also see the WARN in wake_futex_pi().
Ben Hutchings25a678d2021-03-01 18:31:39 +01003123 */
3124 raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
Ben Hutchings312d9d62021-03-01 18:31:30 +01003125 spin_unlock(&hb->lock);
3126
Ben Hutchings9787adc2021-03-01 18:31:48 +01003127 /* drops pi_state->pi_mutex.wait_lock */
Ben Hutchings312d9d62021-03-01 18:31:30 +01003128 ret = wake_futex_pi(uaddr, uval, pi_state);
3129
3130 put_pi_state(pi_state);
3131
3132 /*
3133 * Success, we're done! No tricky corner cases.
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02003134 */
3135 if (!ret)
3136 goto out_putkey;
Ingo Molnarc87e2832006-06-27 02:54:58 -07003137 /*
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00003138 * The atomic access to the futex value generated a
3139 * pagefault, so retry the user-access and the wakeup:
Ingo Molnarc87e2832006-06-27 02:54:58 -07003140 */
3141 if (ret == -EFAULT)
3142 goto pi_faulted;
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02003143 /*
Sebastian Andrzej Siewior89e9e662016-04-15 14:35:39 +02003144 * A unconditional UNLOCK_PI op raced against a waiter
3145 * setting the FUTEX_WAITERS bit. Try again.
3146 */
Will Deacon8682c2e2021-03-28 22:42:44 +02003147 if (ret == -EAGAIN)
3148 goto pi_retry;
Sebastian Andrzej Siewior89e9e662016-04-15 14:35:39 +02003149 /*
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02003150 * wake_futex_pi has detected invalid state. Tell user
3151 * space.
3152 */
Ben Hutchings312d9d62021-03-01 18:31:30 +01003153 goto out_putkey;
Ingo Molnarc87e2832006-06-27 02:54:58 -07003154 }
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00003155
Ingo Molnarc87e2832006-06-27 02:54:58 -07003156 /*
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00003157 * We have no kernel internal state, i.e. no waiters in the
3158 * kernel. Waiters which are about to queue themselves are stuck
3159 * on hb->lock. So we can safely ignore them. We do neither
3160 * preserve the WAITERS bit not the OWNER_DIED one. We are the
3161 * owner.
Ingo Molnarc87e2832006-06-27 02:54:58 -07003162 */
Will Deacon8682c2e2021-03-28 22:42:44 +02003163 if ((ret = cmpxchg_futex_value_locked(&curval, uaddr, uval, 0))) {
Ben Hutchings312d9d62021-03-01 18:31:30 +01003164 spin_unlock(&hb->lock);
Will Deacon8682c2e2021-03-28 22:42:44 +02003165 switch (ret) {
3166 case -EFAULT:
3167 goto pi_faulted;
3168
3169 case -EAGAIN:
3170 goto pi_retry;
3171
3172 default:
3173 WARN_ON_ONCE(1);
3174 goto out_putkey;
3175 }
Ben Hutchings312d9d62021-03-01 18:31:30 +01003176 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07003177
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00003178 /*
3179 * If uval has changed, let user space handle it.
3180 */
3181 ret = (curval == uval) ? 0 : -EAGAIN;
3182
Ingo Molnarc87e2832006-06-27 02:54:58 -07003183out_unlock:
3184 spin_unlock(&hb->lock);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02003185out_putkey:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01003186 put_futex_key(&key);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003187 return ret;
3188
Will Deacon8682c2e2021-03-28 22:42:44 +02003189pi_retry:
3190 put_futex_key(&key);
3191 cond_resched();
3192 goto retry;
3193
Ingo Molnarc87e2832006-06-27 02:54:58 -07003194pi_faulted:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01003195 put_futex_key(&key);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003196
Thomas Gleixnerd0725992009-06-11 23:15:43 +02003197 ret = fault_in_user_writeable(uaddr);
Darren Hartb5686362008-12-18 15:06:34 -08003198 if (!ret)
Ingo Molnarc87e2832006-06-27 02:54:58 -07003199 goto retry;
3200
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 return ret;
3202}
3203
Darren Hart52400ba2009-04-03 13:40:49 -07003204/**
3205 * handle_early_requeue_pi_wakeup() - Detect early wakeup on the initial futex
3206 * @hb: the hash_bucket futex_q was original enqueued on
3207 * @q: the futex_q woken while waiting to be requeued
3208 * @key2: the futex_key of the requeue target futex
3209 * @timeout: the timeout associated with the wait (NULL if none)
3210 *
3211 * Detect if the task was woken on the initial futex as opposed to the requeue
3212 * target futex. If so, determine if it was a timeout or a signal that caused
3213 * the wakeup and return the appropriate error code to the caller. Must be
3214 * called with the hb lock held.
3215 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08003216 * Return:
3217 * 0 = no early wakeup detected;
3218 * <0 = -ETIMEDOUT or -ERESTARTNOINTR
Darren Hart52400ba2009-04-03 13:40:49 -07003219 */
3220static inline
3221int handle_early_requeue_pi_wakeup(struct futex_hash_bucket *hb,
3222 struct futex_q *q, union futex_key *key2,
3223 struct hrtimer_sleeper *timeout)
3224{
3225 int ret = 0;
3226
3227 /*
3228 * With the hb lock held, we avoid races while we process the wakeup.
3229 * We only need to hold hb (and not hb2) to ensure atomicity as the
3230 * wakeup code can't change q.key from uaddr to uaddr2 if we hold hb.
3231 * It can't be requeued from uaddr2 to something else since we don't
3232 * support a PI aware source futex for requeue.
3233 */
3234 if (!match_futex(&q->key, key2)) {
3235 WARN_ON(q->lock_ptr && (&hb->lock != q->lock_ptr));
3236 /*
3237 * We were woken prior to requeue by a timeout or a signal.
3238 * Unqueue the futex_q and determine which it was.
3239 */
Lai Jiangshan2e129782010-12-22 14:18:50 +08003240 plist_del(&q->list, &hb->chain);
Linus Torvalds11d46162014-03-20 22:11:17 -07003241 hb_waiters_dec(hb);
Darren Hart52400ba2009-04-03 13:40:49 -07003242
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02003243 /* Handle spurious wakeups gracefully */
Thomas Gleixner11df6dd2009-10-28 20:26:48 +01003244 ret = -EWOULDBLOCK;
Darren Hart52400ba2009-04-03 13:40:49 -07003245 if (timeout && !timeout->task)
3246 ret = -ETIMEDOUT;
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02003247 else if (signal_pending(current))
Thomas Gleixner1c840c12009-05-20 09:22:40 +02003248 ret = -ERESTARTNOINTR;
Darren Hart52400ba2009-04-03 13:40:49 -07003249 }
3250 return ret;
3251}
3252
3253/**
3254 * futex_wait_requeue_pi() - Wait on uaddr and take uaddr2
Darren Hart56ec1602009-09-21 22:29:59 -07003255 * @uaddr: the futex we initially wait on (non-pi)
Darren Hartb41277d2010-11-08 13:10:09 -08003256 * @flags: futex flags (FLAGS_SHARED, FLAGS_CLOCKRT, etc.), they must be
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07003257 * the same type, no requeueing from private to shared, etc.
Darren Hart52400ba2009-04-03 13:40:49 -07003258 * @val: the expected value of uaddr
3259 * @abs_time: absolute timeout
Darren Hart56ec1602009-09-21 22:29:59 -07003260 * @bitset: 32 bit wakeup bitset set by userspace, defaults to all
Darren Hart52400ba2009-04-03 13:40:49 -07003261 * @uaddr2: the pi futex we will take prior to returning to user-space
3262 *
3263 * The caller will wait on uaddr and will be requeued by futex_requeue() to
Darren Hart6f7b0a22012-07-20 11:53:31 -07003264 * uaddr2 which must be PI aware and unique from uaddr. Normal wakeup will wake
3265 * on uaddr2 and complete the acquisition of the rt_mutex prior to returning to
3266 * userspace. This ensures the rt_mutex maintains an owner when it has waiters;
3267 * without one, the pi logic would not know which task to boost/deboost, if
3268 * there was a need to.
Darren Hart52400ba2009-04-03 13:40:49 -07003269 *
3270 * We call schedule in futex_wait_queue_me() when we enqueue and return there
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08003271 * via the following--
Darren Hart52400ba2009-04-03 13:40:49 -07003272 * 1) wakeup on uaddr2 after an atomic lock acquisition by futex_requeue()
Darren Hartcc6db4e2009-07-31 16:20:10 -07003273 * 2) wakeup on uaddr2 after a requeue
3274 * 3) signal
3275 * 4) timeout
Darren Hart52400ba2009-04-03 13:40:49 -07003276 *
Darren Hartcc6db4e2009-07-31 16:20:10 -07003277 * If 3, cleanup and return -ERESTARTNOINTR.
Darren Hart52400ba2009-04-03 13:40:49 -07003278 *
3279 * If 2, we may then block on trying to take the rt_mutex and return via:
3280 * 5) successful lock
3281 * 6) signal
3282 * 7) timeout
3283 * 8) other lock acquisition failure
3284 *
Darren Hartcc6db4e2009-07-31 16:20:10 -07003285 * If 6, return -EWOULDBLOCK (restarting the syscall would do the same).
Darren Hart52400ba2009-04-03 13:40:49 -07003286 *
3287 * If 4 or 7, we cleanup and return with -ETIMEDOUT.
3288 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08003289 * Return:
3290 * 0 - On success;
Darren Hart52400ba2009-04-03 13:40:49 -07003291 * <0 - On error
3292 */
Darren Hartb41277d2010-11-08 13:10:09 -08003293static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
Darren Hart52400ba2009-04-03 13:40:49 -07003294 u32 val, ktime_t *abs_time, u32 bitset,
Darren Hartb41277d2010-11-08 13:10:09 -08003295 u32 __user *uaddr2)
Darren Hart52400ba2009-04-03 13:40:49 -07003296{
3297 struct hrtimer_sleeper timeout, *to = NULL;
3298 struct rt_mutex_waiter rt_waiter;
Darren Hart52400ba2009-04-03 13:40:49 -07003299 struct futex_hash_bucket *hb;
Darren Hart5bdb05f2010-11-08 13:40:28 -08003300 union futex_key key2 = FUTEX_KEY_INIT;
3301 struct futex_q q = futex_q_init;
Darren Hart52400ba2009-04-03 13:40:49 -07003302 int res, ret;
Darren Hart52400ba2009-04-03 13:40:49 -07003303
Darren Hart6f7b0a22012-07-20 11:53:31 -07003304 if (uaddr == uaddr2)
3305 return -EINVAL;
3306
Darren Hart52400ba2009-04-03 13:40:49 -07003307 if (!bitset)
3308 return -EINVAL;
3309
3310 if (abs_time) {
3311 to = &timeout;
Darren Hartb41277d2010-11-08 13:10:09 -08003312 hrtimer_init_on_stack(&to->timer, (flags & FLAGS_CLOCKRT) ?
3313 CLOCK_REALTIME : CLOCK_MONOTONIC,
3314 HRTIMER_MODE_ABS);
Darren Hart52400ba2009-04-03 13:40:49 -07003315 hrtimer_init_sleeper(to, current);
3316 hrtimer_set_expires_range_ns(&to->timer, *abs_time,
3317 current->timer_slack_ns);
3318 }
3319
3320 /*
3321 * The waiter is allocated on our stack, manipulated by the requeue
3322 * code while we sleep on uaddr.
3323 */
Peter Zijlstra55404eb2021-03-28 22:41:33 +02003324 rt_mutex_init_waiter(&rt_waiter);
Darren Hart52400ba2009-04-03 13:40:49 -07003325
Shawn Bohrer9ea71502011-06-30 11:21:32 -05003326 ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE);
Darren Hart52400ba2009-04-03 13:40:49 -07003327 if (unlikely(ret != 0))
3328 goto out;
3329
Darren Hart84bc4af2009-08-13 17:36:53 -07003330 q.bitset = bitset;
3331 q.rt_waiter = &rt_waiter;
3332 q.requeue_pi_key = &key2;
3333
Darren Hart7ada8762010-10-17 08:35:04 -07003334 /*
3335 * Prepare to wait on uaddr. On success, increments q.key (key1) ref
3336 * count.
3337 */
Darren Hartb41277d2010-11-08 13:10:09 -08003338 ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
Thomas Gleixnerc8b15a72009-05-20 09:18:50 +02003339 if (ret)
3340 goto out_key2;
Darren Hart52400ba2009-04-03 13:40:49 -07003341
Thomas Gleixnere9c243a2014-06-03 12:27:06 +00003342 /*
3343 * The check above which compares uaddrs is not sufficient for
3344 * shared futexes. We need to compare the keys:
3345 */
3346 if (match_futex(&q.key, &key2)) {
Thomas Gleixner13c42c22014-09-11 23:44:35 +02003347 queue_unlock(hb);
Thomas Gleixnere9c243a2014-06-03 12:27:06 +00003348 ret = -EINVAL;
3349 goto out_put_keys;
3350 }
3351
Darren Hart52400ba2009-04-03 13:40:49 -07003352 /* Queue the futex_q, drop the hb lock, wait for wakeup. */
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02003353 futex_wait_queue_me(hb, &q, to);
Darren Hart52400ba2009-04-03 13:40:49 -07003354
3355 spin_lock(&hb->lock);
3356 ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
3357 spin_unlock(&hb->lock);
3358 if (ret)
3359 goto out_put_keys;
3360
3361 /*
3362 * In order for us to be here, we know our q.key == key2, and since
3363 * we took the hb->lock above, we also know that futex_requeue() has
3364 * completed and we no longer have to concern ourselves with a wakeup
Darren Hart7ada8762010-10-17 08:35:04 -07003365 * race with the atomic proxy lock acquisition by the requeue code. The
3366 * futex_requeue dropped our key1 reference and incremented our key2
3367 * reference count.
Darren Hart52400ba2009-04-03 13:40:49 -07003368 */
3369
3370 /* Check if the requeue code acquired the second futex for us. */
3371 if (!q.rt_waiter) {
3372 /*
3373 * Got the lock. We might not be the anticipated owner if we
3374 * did a lock-steal - fix up the PI-state in that case.
3375 */
3376 if (q.pi_state && (q.pi_state->owner != current)) {
3377 spin_lock(q.lock_ptr);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01003378 ret = fixup_pi_state_owner(uaddr2, &q, current);
Thomas Gleixnerfb75a422015-12-19 20:07:38 +00003379 /*
3380 * Drop the reference to the pi state which
3381 * the requeue_pi() code acquired for us.
3382 */
Thomas Gleixner29e9ee52015-12-19 20:07:39 +00003383 put_pi_state(q.pi_state);
Darren Hart52400ba2009-04-03 13:40:49 -07003384 spin_unlock(q.lock_ptr);
Thomas Gleixnerad8fdba2021-02-11 09:26:58 +00003385 /*
3386 * Adjust the return value. It's either -EFAULT or
3387 * success (1) but the caller expects 0 for success.
3388 */
3389 ret = ret < 0 ? ret : 0;
Darren Hart52400ba2009-04-03 13:40:49 -07003390 }
3391 } else {
Peter Zijlstra6244ffc2017-03-04 10:27:18 +01003392 struct rt_mutex *pi_mutex;
3393
Darren Hart52400ba2009-04-03 13:40:49 -07003394 /*
3395 * We have been woken up by futex_unlock_pi(), a timeout, or a
3396 * signal. futex_unlock_pi() will not destroy the lock_ptr nor
3397 * the pi_state.
3398 */
Darren Hartf27071c2012-07-20 11:53:30 -07003399 WARN_ON(!q.pi_state);
Darren Hart52400ba2009-04-03 13:40:49 -07003400 pi_mutex = &q.pi_state->pi_mutex;
Peter Zijlstrace813552017-03-22 11:35:57 +01003401 ret = rt_mutex_wait_proxy_lock(pi_mutex, to, &rt_waiter);
Darren Hart52400ba2009-04-03 13:40:49 -07003402
3403 spin_lock(q.lock_ptr);
Peter Zijlstrace813552017-03-22 11:35:57 +01003404 if (ret && !rt_mutex_cleanup_proxy_lock(pi_mutex, &rt_waiter))
3405 ret = 0;
3406
3407 debug_rt_mutex_free_waiter(&rt_waiter);
Darren Hart52400ba2009-04-03 13:40:49 -07003408 /*
3409 * Fixup the pi_state owner and possibly acquire the lock if we
3410 * haven't already.
3411 */
Thomas Gleixnerae791a22010-11-10 13:30:36 +01003412 res = fixup_owner(uaddr2, &q, !ret);
Darren Hart52400ba2009-04-03 13:40:49 -07003413 /*
3414 * If fixup_owner() returned an error, proprogate that. If it
Darren Hart56ec1602009-09-21 22:29:59 -07003415 * acquired the lock, clear -ETIMEDOUT or -EINTR.
Darren Hart52400ba2009-04-03 13:40:49 -07003416 */
3417 if (res)
3418 ret = (res < 0) ? res : 0;
3419
3420 /* Unqueue and drop the lock. */
3421 unqueue_me_pi(&q);
3422 }
3423
Peter Zijlstra6244ffc2017-03-04 10:27:18 +01003424 if (ret == -EINTR) {
Darren Hart52400ba2009-04-03 13:40:49 -07003425 /*
Darren Hartcc6db4e2009-07-31 16:20:10 -07003426 * We've already been requeued, but cannot restart by calling
3427 * futex_lock_pi() directly. We could restart this syscall, but
3428 * it would detect that the user space "val" changed and return
3429 * -EWOULDBLOCK. Save the overhead of the restart and return
3430 * -EWOULDBLOCK directly.
Darren Hart52400ba2009-04-03 13:40:49 -07003431 */
Thomas Gleixner20708872009-05-19 23:04:59 +02003432 ret = -EWOULDBLOCK;
Darren Hart52400ba2009-04-03 13:40:49 -07003433 }
3434
3435out_put_keys:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01003436 put_futex_key(&q.key);
Thomas Gleixnerc8b15a72009-05-20 09:18:50 +02003437out_key2:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01003438 put_futex_key(&key2);
Darren Hart52400ba2009-04-03 13:40:49 -07003439
3440out:
3441 if (to) {
3442 hrtimer_cancel(&to->timer);
3443 destroy_hrtimer_on_stack(&to->timer);
3444 }
3445 return ret;
3446}
3447
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003448/*
3449 * Support for robust futexes: the kernel cleans up held futexes at
3450 * thread exit time.
3451 *
3452 * Implementation: user-space maintains a per-thread list of locks it
3453 * is holding. Upon do_exit(), the kernel carefully walks this list,
3454 * and marks all locks that are owned by this thread with the
Ingo Molnarc87e2832006-06-27 02:54:58 -07003455 * FUTEX_OWNER_DIED bit, and wakes up a waiter (if any). The list is
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003456 * always manipulated with the lock held, so the list is private and
3457 * per-thread. Userspace also maintains a per-thread 'list_op_pending'
3458 * field, to allow the kernel to clean up if the thread dies after
3459 * acquiring the lock, but just before it could have added itself to
3460 * the list. There can only be one such pending lock.
3461 */
3462
3463/**
Darren Hartd96ee562009-09-21 22:30:22 -07003464 * sys_set_robust_list() - Set the robust-futex list head of a task
3465 * @head: pointer to the list-head
3466 * @len: length of the list-head, as userspace expects
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003467 */
Heiko Carstens836f92a2009-01-14 14:14:33 +01003468SYSCALL_DEFINE2(set_robust_list, struct robust_list_head __user *, head,
3469 size_t, len)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003470{
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08003471 if (!futex_cmpxchg_enabled)
3472 return -ENOSYS;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003473 /*
3474 * The kernel knows only one size for now:
3475 */
3476 if (unlikely(len != sizeof(*head)))
3477 return -EINVAL;
3478
3479 current->robust_list = head;
3480
3481 return 0;
3482}
3483
3484/**
Darren Hartd96ee562009-09-21 22:30:22 -07003485 * sys_get_robust_list() - Get the robust-futex list head of a task
3486 * @pid: pid of the process [zero for current task]
3487 * @head_ptr: pointer to a list-head pointer, the kernel fills it in
3488 * @len_ptr: pointer to a length field, the kernel fills in the header size
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003489 */
Heiko Carstens836f92a2009-01-14 14:14:33 +01003490SYSCALL_DEFINE3(get_robust_list, int, pid,
3491 struct robust_list_head __user * __user *, head_ptr,
3492 size_t __user *, len_ptr)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003493{
Al Viroba46df92006-10-10 22:46:07 +01003494 struct robust_list_head __user *head;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003495 unsigned long ret;
Kees Cookbdbb7762012-03-19 16:12:53 -07003496 struct task_struct *p;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003497
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08003498 if (!futex_cmpxchg_enabled)
3499 return -ENOSYS;
3500
Kees Cookbdbb7762012-03-19 16:12:53 -07003501 rcu_read_lock();
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003502
Kees Cookbdbb7762012-03-19 16:12:53 -07003503 ret = -ESRCH;
3504 if (!pid)
3505 p = current;
3506 else {
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07003507 p = find_task_by_vpid(pid);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003508 if (!p)
3509 goto err_unlock;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003510 }
3511
Kees Cookbdbb7762012-03-19 16:12:53 -07003512 ret = -EPERM;
Jann Horncaaee622016-01-20 15:00:04 -08003513 if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS))
Kees Cookbdbb7762012-03-19 16:12:53 -07003514 goto err_unlock;
3515
3516 head = p->robust_list;
3517 rcu_read_unlock();
3518
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003519 if (put_user(sizeof(*head), len_ptr))
3520 return -EFAULT;
3521 return put_user(head, head_ptr);
3522
3523err_unlock:
Oleg Nesterovaaa2a972006-09-29 02:00:55 -07003524 rcu_read_unlock();
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003525
3526 return ret;
3527}
3528
3529/*
3530 * Process a futex-list entry, check whether it's owned by the
3531 * dying task, and do notification if so:
3532 */
Arnd Bergmannbdb116c2021-02-01 10:01:32 +00003533static int handle_futex_death(u32 __user *uaddr, struct task_struct *curr, int pi)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003534{
Vitaliy Ivanov7cfdaf32011-07-07 15:10:31 +03003535 u32 uval, uninitialized_var(nval), mval;
Will Deacon8682c2e2021-03-28 22:42:44 +02003536 int err;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003537
Chen Jie726c28f2019-03-15 03:44:38 +00003538 /* Futex address must be 32bit aligned */
3539 if ((((unsigned long)uaddr) % sizeof(*uaddr)) != 0)
3540 return -1;
3541
Ingo Molnar8f17d3a2006-03-27 01:16:27 -08003542retry:
3543 if (get_user(uval, uaddr))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003544 return -1;
3545
Will Deacon8682c2e2021-03-28 22:42:44 +02003546 if ((uval & FUTEX_TID_MASK) != task_pid_vnr(curr))
3547 return 0;
3548
3549 /*
3550 * Ok, this dying thread is truly holding a futex
3551 * of interest. Set the OWNER_DIED bit atomically
3552 * via cmpxchg, and if the value had FUTEX_WAITERS
3553 * set, wake up a waiter (if any). (We have to do a
3554 * futex_wake() even if OWNER_DIED is already set -
3555 * to handle the rare but possible case of recursive
3556 * thread-death.) The rest of the cleanup is done in
3557 * userspace.
3558 */
3559 mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
3560
3561 /*
3562 * We are not holding a lock here, but we want to have
3563 * the pagefault_disable/enable() protection because
3564 * we want to handle the fault gracefully. If the
3565 * access fails we try to fault in the futex with R/W
3566 * verification via get_user_pages. get_user() above
3567 * does not guarantee R/W access. If that fails we
3568 * give up and leave the futex locked.
3569 */
3570 if ((err = cmpxchg_futex_value_locked(&nval, uaddr, uval, mval))) {
3571 switch (err) {
3572 case -EFAULT:
Thomas Gleixner6e0aa9f2011-03-14 10:34:35 +01003573 if (fault_in_user_writeable(uaddr))
3574 return -1;
3575 goto retry;
Will Deacon8682c2e2021-03-28 22:42:44 +02003576
3577 case -EAGAIN:
3578 cond_resched();
Ingo Molnar8f17d3a2006-03-27 01:16:27 -08003579 goto retry;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003580
Will Deacon8682c2e2021-03-28 22:42:44 +02003581 default:
3582 WARN_ON_ONCE(1);
3583 return err;
3584 }
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003585 }
Will Deacon8682c2e2021-03-28 22:42:44 +02003586
3587 if (nval != uval)
3588 goto retry;
3589
3590 /*
3591 * Wake robust non-PI futexes here. The wakeup of
3592 * PI futexes happens in exit_pi_state():
3593 */
3594 if (!pi && (uval & FUTEX_WAITERS))
3595 futex_wake(uaddr, 1, 1, FUTEX_BITSET_MATCH_ANY);
3596
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003597 return 0;
3598}
3599
3600/*
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003601 * Fetch a robust-list pointer. Bit 0 signals PI futexes:
3602 */
3603static inline int fetch_robust_entry(struct robust_list __user **entry,
Al Viroba46df92006-10-10 22:46:07 +01003604 struct robust_list __user * __user *head,
Namhyung Kim1dcc41b2010-09-14 21:43:46 +09003605 unsigned int *pi)
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003606{
3607 unsigned long uentry;
3608
Al Viroba46df92006-10-10 22:46:07 +01003609 if (get_user(uentry, (unsigned long __user *)head))
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003610 return -EFAULT;
3611
Al Viroba46df92006-10-10 22:46:07 +01003612 *entry = (void __user *)(uentry & ~1UL);
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003613 *pi = uentry & 1;
3614
3615 return 0;
3616}
3617
3618/*
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003619 * Walk curr->robust_list (very carefully, it's a userspace list!)
3620 * and mark any locks found there dead, and notify any waiters.
3621 *
3622 * We silently return on any sign of list-walking problem.
3623 */
Thomas Gleixner25f319b2021-02-01 10:01:33 +00003624static void exit_robust_list(struct task_struct *curr)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003625{
3626 struct robust_list_head __user *head = curr->robust_list;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003627 struct robust_list __user *entry, *next_entry, *pending;
Darren Hart4c115e92010-11-04 15:00:00 -04003628 unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
3629 unsigned int uninitialized_var(next_pi);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003630 unsigned long futex_offset;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003631 int rc;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003632
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08003633 if (!futex_cmpxchg_enabled)
3634 return;
3635
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003636 /*
3637 * Fetch the list head (which was registered earlier, via
3638 * sys_set_robust_list()):
3639 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003640 if (fetch_robust_entry(&entry, &head->list.next, &pi))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003641 return;
3642 /*
3643 * Fetch the relative futex offset:
3644 */
3645 if (get_user(futex_offset, &head->futex_offset))
3646 return;
3647 /*
3648 * Fetch any possibly pending lock-add first, and handle it
3649 * if it exists:
3650 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003651 if (fetch_robust_entry(&pending, &head->list_op_pending, &pip))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003652 return;
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003653
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003654 next_entry = NULL; /* avoid warning with gcc */
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003655 while (entry != &head->list) {
3656 /*
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003657 * Fetch the next entry in the list before calling
3658 * handle_futex_death:
3659 */
3660 rc = fetch_robust_entry(&next_entry, &entry->next, &next_pi);
3661 /*
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003662 * A pending lock might already be on the list, so
Ingo Molnarc87e2832006-06-27 02:54:58 -07003663 * don't process it twice:
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003664 */
3665 if (entry != pending)
Al Viroba46df92006-10-10 22:46:07 +01003666 if (handle_futex_death((void __user *)entry + futex_offset,
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003667 curr, pi))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003668 return;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003669 if (rc)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003670 return;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003671 entry = next_entry;
3672 pi = next_pi;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003673 /*
3674 * Avoid excessively long or circular lists:
3675 */
3676 if (!--limit)
3677 break;
3678
3679 cond_resched();
3680 }
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003681
3682 if (pending)
3683 handle_futex_death((void __user *)pending + futex_offset,
3684 curr, pip);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003685}
3686
Thomas Gleixnerff3a33f2021-02-01 10:01:40 +00003687static void futex_cleanup(struct task_struct *tsk)
Thomas Gleixner25f319b2021-02-01 10:01:33 +00003688{
3689 if (unlikely(tsk->robust_list)) {
3690 exit_robust_list(tsk);
3691 tsk->robust_list = NULL;
3692 }
3693
3694#ifdef CONFIG_COMPAT
3695 if (unlikely(tsk->compat_robust_list)) {
3696 compat_exit_robust_list(tsk);
3697 tsk->compat_robust_list = NULL;
3698 }
3699#endif
3700
3701 if (unlikely(!list_empty(&tsk->pi_state_list)))
3702 exit_pi_state_list(tsk);
3703}
3704
Thomas Gleixner32d78282021-02-01 10:01:38 +00003705/**
3706 * futex_exit_recursive - Set the tasks futex state to FUTEX_STATE_DEAD
3707 * @tsk: task to set the state on
3708 *
3709 * Set the futex exit state of the task lockless. The futex waiter code
3710 * observes that state when a task is exiting and loops until the task has
3711 * actually finished the futex cleanup. The worst case for this is that the
3712 * waiter runs through the wait loop until the state becomes visible.
3713 *
3714 * This is called from the recursive fault handling path in do_exit().
3715 *
3716 * This is best effort. Either the futex exit code has run already or
3717 * not. If the OWNER_DIED bit has been set on the futex then the waiter can
3718 * take it over. If not, the problem is pushed back to user space. If the
3719 * futex exit code did not run yet, then an already queued waiter might
3720 * block forever, but there is nothing which can be done about that.
3721 */
3722void futex_exit_recursive(struct task_struct *tsk)
3723{
Thomas Gleixnerad3466a2021-02-01 10:01:41 +00003724 /* If the state is FUTEX_STATE_EXITING then futex_exit_mutex is held */
3725 if (tsk->futex_state == FUTEX_STATE_EXITING)
3726 mutex_unlock(&tsk->futex_exit_mutex);
Thomas Gleixner32d78282021-02-01 10:01:38 +00003727 tsk->futex_state = FUTEX_STATE_DEAD;
3728}
3729
Thomas Gleixnerff3a33f2021-02-01 10:01:40 +00003730static void futex_cleanup_begin(struct task_struct *tsk)
Thomas Gleixner8a16d8a2021-02-01 10:01:36 +00003731{
Thomas Gleixner32d78282021-02-01 10:01:38 +00003732 /*
Thomas Gleixnerad3466a2021-02-01 10:01:41 +00003733 * Prevent various race issues against a concurrent incoming waiter
3734 * including live locks by forcing the waiter to block on
3735 * tsk->futex_exit_mutex when it observes FUTEX_STATE_EXITING in
3736 * attach_to_pi_owner().
3737 */
3738 mutex_lock(&tsk->futex_exit_mutex);
3739
3740 /*
Thomas Gleixner0ba263f2021-02-01 10:01:39 +00003741 * Switch the state to FUTEX_STATE_EXITING under tsk->pi_lock.
3742 *
3743 * This ensures that all subsequent checks of tsk->futex_state in
3744 * attach_to_pi_owner() must observe FUTEX_STATE_EXITING with
3745 * tsk->pi_lock held.
3746 *
3747 * It guarantees also that a pi_state which was queued right before
3748 * the state change under tsk->pi_lock by a concurrent waiter must
3749 * be observed in exit_pi_state_list().
Thomas Gleixner32d78282021-02-01 10:01:38 +00003750 */
3751 raw_spin_lock_irq(&tsk->pi_lock);
Thomas Gleixner0ba263f2021-02-01 10:01:39 +00003752 tsk->futex_state = FUTEX_STATE_EXITING;
Thomas Gleixner32d78282021-02-01 10:01:38 +00003753 raw_spin_unlock_irq(&tsk->pi_lock);
Thomas Gleixnerff3a33f2021-02-01 10:01:40 +00003754}
Thomas Gleixner32d78282021-02-01 10:01:38 +00003755
Thomas Gleixnerff3a33f2021-02-01 10:01:40 +00003756static void futex_cleanup_end(struct task_struct *tsk, int state)
3757{
3758 /*
3759 * Lockless store. The only side effect is that an observer might
3760 * take another loop until it becomes visible.
3761 */
3762 tsk->futex_state = state;
Thomas Gleixnerad3466a2021-02-01 10:01:41 +00003763 /*
3764 * Drop the exit protection. This unblocks waiters which observed
3765 * FUTEX_STATE_EXITING to reevaluate the state.
3766 */
3767 mutex_unlock(&tsk->futex_exit_mutex);
Thomas Gleixnerff3a33f2021-02-01 10:01:40 +00003768}
Thomas Gleixner32d78282021-02-01 10:01:38 +00003769
Thomas Gleixnerff3a33f2021-02-01 10:01:40 +00003770void futex_exec_release(struct task_struct *tsk)
3771{
3772 /*
3773 * The state handling is done for consistency, but in the case of
3774 * exec() there is no way to prevent futher damage as the PID stays
3775 * the same. But for the unlikely and arguably buggy case that a
3776 * futex is held on exec(), this provides at least as much state
3777 * consistency protection which is possible.
3778 */
3779 futex_cleanup_begin(tsk);
3780 futex_cleanup(tsk);
3781 /*
3782 * Reset the state to FUTEX_STATE_OK. The task is alive and about
3783 * exec a new binary.
3784 */
3785 futex_cleanup_end(tsk, FUTEX_STATE_OK);
3786}
3787
3788void futex_exit_release(struct task_struct *tsk)
3789{
3790 futex_cleanup_begin(tsk);
3791 futex_cleanup(tsk);
3792 futex_cleanup_end(tsk, FUTEX_STATE_DEAD);
Thomas Gleixner8a16d8a2021-02-01 10:01:36 +00003793}
3794
Pierre Peifferc19384b2007-05-09 02:35:02 -07003795long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
Ingo Molnare2970f22006-06-27 02:54:47 -07003796 u32 __user *uaddr2, u32 val2, u32 val3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797{
Thomas Gleixner81b40532012-02-15 12:17:09 +01003798 int cmd = op & FUTEX_CMD_MASK;
Darren Hartb41277d2010-11-08 13:10:09 -08003799 unsigned int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003801 if (!(op & FUTEX_PRIVATE_FLAG))
Darren Hartb41277d2010-11-08 13:10:09 -08003802 flags |= FLAGS_SHARED;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003803
Darren Hartb41277d2010-11-08 13:10:09 -08003804 if (op & FUTEX_CLOCK_REALTIME) {
3805 flags |= FLAGS_CLOCKRT;
Darren Hart337f1302015-12-18 13:36:37 -08003806 if (cmd != FUTEX_WAIT && cmd != FUTEX_WAIT_BITSET && \
3807 cmd != FUTEX_WAIT_REQUEUE_PI)
Darren Hartb41277d2010-11-08 13:10:09 -08003808 return -ENOSYS;
3809 }
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003810
3811 switch (cmd) {
Thomas Gleixner59263b52012-02-15 12:08:34 +01003812 case FUTEX_LOCK_PI:
3813 case FUTEX_UNLOCK_PI:
3814 case FUTEX_TRYLOCK_PI:
3815 case FUTEX_WAIT_REQUEUE_PI:
3816 case FUTEX_CMP_REQUEUE_PI:
3817 if (!futex_cmpxchg_enabled)
3818 return -ENOSYS;
3819 }
3820
3821 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 case FUTEX_WAIT:
Thomas Gleixnercd689982008-02-01 17:45:14 +01003823 val3 = FUTEX_BITSET_MATCH_ANY;
3824 case FUTEX_WAIT_BITSET:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003825 return futex_wait(uaddr, flags, val, timeout, val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 case FUTEX_WAKE:
Thomas Gleixnercd689982008-02-01 17:45:14 +01003827 val3 = FUTEX_BITSET_MATCH_ANY;
3828 case FUTEX_WAKE_BITSET:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003829 return futex_wake(uaddr, flags, val, val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 case FUTEX_REQUEUE:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003831 return futex_requeue(uaddr, flags, uaddr2, val, val2, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 case FUTEX_CMP_REQUEUE:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003833 return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 0);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07003834 case FUTEX_WAKE_OP:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003835 return futex_wake_op(uaddr, flags, uaddr2, val, val2, val3);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003836 case FUTEX_LOCK_PI:
Michael Kerrisk996636d2015-01-16 20:28:06 +01003837 return futex_lock_pi(uaddr, flags, timeout, 0);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003838 case FUTEX_UNLOCK_PI:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003839 return futex_unlock_pi(uaddr, flags);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003840 case FUTEX_TRYLOCK_PI:
Michael Kerrisk996636d2015-01-16 20:28:06 +01003841 return futex_lock_pi(uaddr, flags, NULL, 1);
Darren Hart52400ba2009-04-03 13:40:49 -07003842 case FUTEX_WAIT_REQUEUE_PI:
3843 val3 = FUTEX_BITSET_MATCH_ANY;
Thomas Gleixner81b40532012-02-15 12:17:09 +01003844 return futex_wait_requeue_pi(uaddr, flags, val, timeout, val3,
3845 uaddr2);
Darren Hart52400ba2009-04-03 13:40:49 -07003846 case FUTEX_CMP_REQUEUE_PI:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003847 return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 }
Thomas Gleixner81b40532012-02-15 12:17:09 +01003849 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850}
3851
3852
Heiko Carstens17da2bd2009-01-14 14:14:10 +01003853SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val,
3854 struct timespec __user *, utime, u32 __user *, uaddr2,
3855 u32, val3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856{
Pierre Peifferc19384b2007-05-09 02:35:02 -07003857 struct timespec ts;
3858 ktime_t t, *tp = NULL;
Ingo Molnare2970f22006-06-27 02:54:47 -07003859 u32 val2 = 0;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003860 int cmd = op & FUTEX_CMD_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861
Thomas Gleixnercd689982008-02-01 17:45:14 +01003862 if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI ||
Darren Hart52400ba2009-04-03 13:40:49 -07003863 cmd == FUTEX_WAIT_BITSET ||
3864 cmd == FUTEX_WAIT_REQUEUE_PI)) {
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07003865 if (unlikely(should_fail_futex(!(op & FUTEX_PRIVATE_FLAG))))
3866 return -EFAULT;
Pierre Peifferc19384b2007-05-09 02:35:02 -07003867 if (copy_from_user(&ts, utime, sizeof(ts)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 return -EFAULT;
Pierre Peifferc19384b2007-05-09 02:35:02 -07003869 if (!timespec_valid(&ts))
Thomas Gleixner9741ef962006-03-31 02:31:32 -08003870 return -EINVAL;
Pierre Peifferc19384b2007-05-09 02:35:02 -07003871
3872 t = timespec_to_ktime(ts);
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003873 if (cmd == FUTEX_WAIT)
Thomas Gleixner5a7780e2008-02-13 09:20:43 +01003874 t = ktime_add_safe(ktime_get(), t);
Pierre Peifferc19384b2007-05-09 02:35:02 -07003875 tp = &t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 }
3877 /*
Darren Hart52400ba2009-04-03 13:40:49 -07003878 * requeue parameter in 'utime' if cmd == FUTEX_*_REQUEUE_*.
Andreas Schwabf54f0982007-07-31 00:38:51 -07003879 * number of waiters to wake in 'utime' if cmd == FUTEX_WAKE_OP.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 */
Andreas Schwabf54f0982007-07-31 00:38:51 -07003881 if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE ||
Darren Hartba9c22f2009-04-20 22:22:22 -07003882 cmd == FUTEX_CMP_REQUEUE_PI || cmd == FUTEX_WAKE_OP)
Ingo Molnare2970f22006-06-27 02:54:47 -07003883 val2 = (u32) (unsigned long) utime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884
Pierre Peifferc19384b2007-05-09 02:35:02 -07003885 return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886}
3887
Arnd Bergmannbdb116c2021-02-01 10:01:32 +00003888#ifdef CONFIG_COMPAT
3889/*
3890 * Fetch a robust-list pointer. Bit 0 signals PI futexes:
3891 */
3892static inline int
3893compat_fetch_robust_entry(compat_uptr_t *uentry, struct robust_list __user **entry,
3894 compat_uptr_t __user *head, unsigned int *pi)
3895{
3896 if (get_user(*uentry, head))
3897 return -EFAULT;
3898
3899 *entry = compat_ptr((*uentry) & ~1);
3900 *pi = (unsigned int)(*uentry) & 1;
3901
3902 return 0;
3903}
3904
3905static void __user *futex_uaddr(struct robust_list __user *entry,
3906 compat_long_t futex_offset)
3907{
3908 compat_uptr_t base = ptr_to_compat(entry);
3909 void __user *uaddr = compat_ptr(base + futex_offset);
3910
3911 return uaddr;
3912}
3913
3914/*
3915 * Walk curr->robust_list (very carefully, it's a userspace list!)
3916 * and mark any locks found there dead, and notify any waiters.
3917 *
3918 * We silently return on any sign of list-walking problem.
3919 */
3920void compat_exit_robust_list(struct task_struct *curr)
3921{
3922 struct compat_robust_list_head __user *head = curr->compat_robust_list;
3923 struct robust_list __user *entry, *next_entry, *pending;
3924 unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
3925 unsigned int uninitialized_var(next_pi);
3926 compat_uptr_t uentry, next_uentry, upending;
3927 compat_long_t futex_offset;
3928 int rc;
3929
3930 if (!futex_cmpxchg_enabled)
3931 return;
3932
3933 /*
3934 * Fetch the list head (which was registered earlier, via
3935 * sys_set_robust_list()):
3936 */
3937 if (compat_fetch_robust_entry(&uentry, &entry, &head->list.next, &pi))
3938 return;
3939 /*
3940 * Fetch the relative futex offset:
3941 */
3942 if (get_user(futex_offset, &head->futex_offset))
3943 return;
3944 /*
3945 * Fetch any possibly pending lock-add first, and handle it
3946 * if it exists:
3947 */
3948 if (compat_fetch_robust_entry(&upending, &pending,
3949 &head->list_op_pending, &pip))
3950 return;
3951
3952 next_entry = NULL; /* avoid warning with gcc */
3953 while (entry != (struct robust_list __user *) &head->list) {
3954 /*
3955 * Fetch the next entry in the list before calling
3956 * handle_futex_death:
3957 */
3958 rc = compat_fetch_robust_entry(&next_uentry, &next_entry,
3959 (compat_uptr_t __user *)&entry->next, &next_pi);
3960 /*
3961 * A pending lock might already be on the list, so
3962 * dont process it twice:
3963 */
3964 if (entry != pending) {
3965 void __user *uaddr = futex_uaddr(entry, futex_offset);
3966
3967 if (handle_futex_death(uaddr, curr, pi))
3968 return;
3969 }
3970 if (rc)
3971 return;
3972 uentry = next_uentry;
3973 entry = next_entry;
3974 pi = next_pi;
3975 /*
3976 * Avoid excessively long or circular lists:
3977 */
3978 if (!--limit)
3979 break;
3980
3981 cond_resched();
3982 }
3983 if (pending) {
3984 void __user *uaddr = futex_uaddr(pending, futex_offset);
3985
3986 handle_futex_death(uaddr, curr, pip);
3987 }
3988}
3989
3990COMPAT_SYSCALL_DEFINE2(set_robust_list,
3991 struct compat_robust_list_head __user *, head,
3992 compat_size_t, len)
3993{
3994 if (!futex_cmpxchg_enabled)
3995 return -ENOSYS;
3996
3997 if (unlikely(len != sizeof(*head)))
3998 return -EINVAL;
3999
4000 current->compat_robust_list = head;
4001
4002 return 0;
4003}
4004
4005COMPAT_SYSCALL_DEFINE3(get_robust_list, int, pid,
4006 compat_uptr_t __user *, head_ptr,
4007 compat_size_t __user *, len_ptr)
4008{
4009 struct compat_robust_list_head __user *head;
4010 unsigned long ret;
4011 struct task_struct *p;
4012
4013 if (!futex_cmpxchg_enabled)
4014 return -ENOSYS;
4015
4016 rcu_read_lock();
4017
4018 ret = -ESRCH;
4019 if (!pid)
4020 p = current;
4021 else {
4022 p = find_task_by_vpid(pid);
4023 if (!p)
4024 goto err_unlock;
4025 }
4026
4027 ret = -EPERM;
4028 if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS))
4029 goto err_unlock;
4030
4031 head = p->compat_robust_list;
4032 rcu_read_unlock();
4033
4034 if (put_user(sizeof(*head), len_ptr))
4035 return -EFAULT;
4036 return put_user(ptr_to_compat(head), head_ptr);
4037
4038err_unlock:
4039 rcu_read_unlock();
4040
4041 return ret;
4042}
4043
4044COMPAT_SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val,
4045 struct compat_timespec __user *, utime, u32 __user *, uaddr2,
4046 u32, val3)
4047{
4048 struct timespec ts;
4049 ktime_t t, *tp = NULL;
4050 int val2 = 0;
4051 int cmd = op & FUTEX_CMD_MASK;
4052
4053 if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI ||
4054 cmd == FUTEX_WAIT_BITSET ||
4055 cmd == FUTEX_WAIT_REQUEUE_PI)) {
4056 if (compat_get_timespec(&ts, utime))
4057 return -EFAULT;
4058 if (!timespec_valid(&ts))
4059 return -EINVAL;
4060
4061 t = timespec_to_ktime(ts);
4062 if (cmd == FUTEX_WAIT)
4063 t = ktime_add_safe(ktime_get(), t);
4064 tp = &t;
4065 }
4066 if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE ||
4067 cmd == FUTEX_CMP_REQUEUE_PI || cmd == FUTEX_WAKE_OP)
4068 val2 = (int) (unsigned long) utime;
4069
4070 return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
4071}
4072#endif /* CONFIG_COMPAT */
4073
Heiko Carstens03b8c7b2014-03-02 13:09:47 +01004074static void __init futex_detect_cmpxchg(void)
4075{
4076#ifndef CONFIG_HAVE_FUTEX_CMPXCHG
4077 u32 curval;
4078
4079 /*
4080 * This will fail and we want it. Some arch implementations do
4081 * runtime detection of the futex_atomic_cmpxchg_inatomic()
4082 * functionality. We want to know that before we call in any
4083 * of the complex code paths. Also we want to prevent
4084 * registration of robust lists in that case. NULL is
4085 * guaranteed to fault and we get -EFAULT on functional
4086 * implementation, the non-functional ones will return
4087 * -ENOSYS.
4088 */
4089 if (cmpxchg_futex_value_locked(&curval, NULL, 0, 0) == -EFAULT)
4090 futex_cmpxchg_enabled = 1;
4091#endif
4092}
4093
Benjamin Herrenschmidtf6d107f2008-03-27 14:52:15 +11004094static int __init futex_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095{
Heiko Carstens63b1a812014-01-16 14:54:50 +01004096 unsigned int futex_shift;
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -08004097 unsigned long i;
4098
4099#if CONFIG_BASE_SMALL
4100 futex_hashsize = 16;
4101#else
4102 futex_hashsize = roundup_pow_of_two(256 * num_possible_cpus());
4103#endif
4104
4105 futex_queues = alloc_large_system_hash("futex", sizeof(*futex_queues),
4106 futex_hashsize, 0,
4107 futex_hashsize < 256 ? HASH_SMALL : 0,
Heiko Carstens63b1a812014-01-16 14:54:50 +01004108 &futex_shift, NULL,
4109 futex_hashsize, futex_hashsize);
4110 futex_hashsize = 1UL << futex_shift;
Heiko Carstens03b8c7b2014-03-02 13:09:47 +01004111
4112 futex_detect_cmpxchg();
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08004113
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -08004114 for (i = 0; i < futex_hashsize; i++) {
Linus Torvalds11d46162014-03-20 22:11:17 -07004115 atomic_set(&futex_queues[i].waiters, 0);
Dima Zavin732375c2011-07-07 17:27:59 -07004116 plist_head_init(&futex_queues[i].chain);
Thomas Gleixner3e4ab742008-02-23 15:23:55 -08004117 spin_lock_init(&futex_queues[i].lock);
4118 }
4119
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 return 0;
4121}
Yang Yang808de342016-12-30 16:17:55 +08004122core_initcall(futex_init);