blob: 855dae277f830ec03c4427d7763e803bc5165798 [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 Hutchings9787adc2021-03-01 18:31:48 +0100885 raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
Thomas Gleixner7d455bb2021-02-03 13:45:37 +0000886 pi_state_update_owner(pi_state, NULL);
Thomas Gleixner285b6242021-02-03 13:45:36 +0000887 rt_mutex_proxy_unlock(&pi_state->pi_mutex);
Ben Hutchings9787adc2021-03-01 18:31:48 +0100888 raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700889 }
890
Ben Hutchings9787adc2021-03-01 18:31:48 +0100891 if (current->pi_state_cache) {
Ingo Molnarc87e2832006-06-27 02:54:58 -0700892 kfree(pi_state);
Ben Hutchings9787adc2021-03-01 18:31:48 +0100893 } else {
Ingo Molnarc87e2832006-06-27 02:54:58 -0700894 /*
895 * pi_state->list is already empty.
896 * clear pi_state->owner.
897 * refcount is at 0 - put it back to 1.
898 */
899 pi_state->owner = NULL;
900 atomic_set(&pi_state->refcount, 1);
901 current->pi_state_cache = pi_state;
902 }
903}
904
905/*
906 * Look up the task based on what TID userspace gave us.
907 * We dont trust it.
908 */
Ben Hutchings921a7e32021-03-01 18:31:22 +0100909static struct task_struct *futex_find_get_task(pid_t pid)
Ingo Molnarc87e2832006-06-27 02:54:58 -0700910{
911 struct task_struct *p;
912
Oleg Nesterovd359b542006-09-29 02:00:55 -0700913 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700914 p = find_task_by_vpid(pid);
Michal Hocko7a0ea092010-06-30 09:51:19 +0200915 if (p)
916 get_task_struct(p);
Thomas Gleixnera06381f2007-06-23 11:48:40 +0200917
Oleg Nesterovd359b542006-09-29 02:00:55 -0700918 rcu_read_unlock();
Ingo Molnarc87e2832006-06-27 02:54:58 -0700919
920 return p;
921}
922
923/*
924 * This task is holding PI mutexes at exit time => bad.
925 * Kernel cleans up PI-state, but userspace is likely hosed.
926 * (Robust-futex cleanup is separate and might save the day for userspace.)
927 */
Thomas Gleixner25f319b2021-02-01 10:01:33 +0000928static void exit_pi_state_list(struct task_struct *curr)
Ingo Molnarc87e2832006-06-27 02:54:58 -0700929{
Ingo Molnarc87e2832006-06-27 02:54:58 -0700930 struct list_head *next, *head = &curr->pi_state_list;
931 struct futex_pi_state *pi_state;
Ingo Molnar627371d2006-07-29 05:16:20 +0200932 struct futex_hash_bucket *hb;
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200933 union futex_key key = FUTEX_KEY_INIT;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700934
Thomas Gleixnera0c1e902008-02-23 15:23:57 -0800935 if (!futex_cmpxchg_enabled)
936 return;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700937 /*
938 * We are a ZOMBIE and nobody can enqueue itself on
939 * pi_state_list anymore, but we have to be careful
Ingo Molnar627371d2006-07-29 05:16:20 +0200940 * versus waiters unqueueing themselves:
Ingo Molnarc87e2832006-06-27 02:54:58 -0700941 */
Thomas Gleixner1d615482009-11-17 14:54:03 +0100942 raw_spin_lock_irq(&curr->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700943 while (!list_empty(head)) {
Ingo Molnarc87e2832006-06-27 02:54:58 -0700944 next = head->next;
945 pi_state = list_entry(next, struct futex_pi_state, list);
946 key = pi_state->key;
Ingo Molnar627371d2006-07-29 05:16:20 +0200947 hb = hash_futex(&key);
Ben Hutchingsda1b9ad2021-03-01 18:31:55 +0100948
949 /*
950 * We can race against put_pi_state() removing itself from the
951 * list (a waiter going away). put_pi_state() will first
952 * decrement the reference count and then modify the list, so
953 * its possible to see the list entry but fail this reference
954 * acquire.
955 *
956 * In that case; drop the locks to let put_pi_state() make
957 * progress and retry the loop.
958 */
959 if (!atomic_inc_not_zero(&pi_state->refcount)) {
960 raw_spin_unlock_irq(&curr->pi_lock);
961 cpu_relax();
962 raw_spin_lock_irq(&curr->pi_lock);
963 continue;
964 }
Thomas Gleixner1d615482009-11-17 14:54:03 +0100965 raw_spin_unlock_irq(&curr->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700966
Ingo Molnarc87e2832006-06-27 02:54:58 -0700967 spin_lock(&hb->lock);
Ben Hutchings9787adc2021-03-01 18:31:48 +0100968 raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
969 raw_spin_lock(&curr->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +0200970 /*
971 * We dropped the pi-lock, so re-check whether this
972 * task still owns the PI-state:
973 */
Ingo Molnarc87e2832006-06-27 02:54:58 -0700974 if (head->next != next) {
Ben Hutchingsda1b9ad2021-03-01 18:31:55 +0100975 /* retain curr->pi_lock for the loop invariant */
Ben Hutchings9787adc2021-03-01 18:31:48 +0100976 raw_spin_unlock(&pi_state->pi_mutex.wait_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700977 spin_unlock(&hb->lock);
Ben Hutchingsda1b9ad2021-03-01 18:31:55 +0100978 put_pi_state(pi_state);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700979 continue;
980 }
981
Ingo Molnarc87e2832006-06-27 02:54:58 -0700982 WARN_ON(pi_state->owner != curr);
Ingo Molnar627371d2006-07-29 05:16:20 +0200983 WARN_ON(list_empty(&pi_state->list));
984 list_del_init(&pi_state->list);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700985 pi_state->owner = NULL;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700986
Ben Hutchingsda1b9ad2021-03-01 18:31:55 +0100987 raw_spin_unlock(&curr->pi_lock);
Ben Hutchings9787adc2021-03-01 18:31:48 +0100988 raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700989 spin_unlock(&hb->lock);
990
Ben Hutchings312d9d62021-03-01 18:31:30 +0100991 rt_mutex_futex_unlock(&pi_state->pi_mutex);
992 put_pi_state(pi_state);
993
Thomas Gleixner1d615482009-11-17 14:54:03 +0100994 raw_spin_lock_irq(&curr->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700995 }
Thomas Gleixner1d615482009-11-17 14:54:03 +0100996 raw_spin_unlock_irq(&curr->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700997}
998
Thomas Gleixner54a21782014-06-03 12:27:08 +0000999/*
1000 * We need to check the following states:
1001 *
1002 * Waiter | pi_state | pi->owner | uTID | uODIED | ?
1003 *
1004 * [1] NULL | --- | --- | 0 | 0/1 | Valid
1005 * [2] NULL | --- | --- | >0 | 0/1 | Valid
1006 *
1007 * [3] Found | NULL | -- | Any | 0/1 | Invalid
1008 *
1009 * [4] Found | Found | NULL | 0 | 1 | Valid
1010 * [5] Found | Found | NULL | >0 | 1 | Invalid
1011 *
1012 * [6] Found | Found | task | 0 | 1 | Valid
1013 *
1014 * [7] Found | Found | NULL | Any | 0 | Invalid
1015 *
1016 * [8] Found | Found | task | ==taskTID | 0/1 | Valid
1017 * [9] Found | Found | task | 0 | 0 | Invalid
1018 * [10] Found | Found | task | !=taskTID | 0/1 | Invalid
1019 *
1020 * [1] Indicates that the kernel can acquire the futex atomically. We
1021 * came came here due to a stale FUTEX_WAITERS/FUTEX_OWNER_DIED bit.
1022 *
1023 * [2] Valid, if TID does not belong to a kernel thread. If no matching
1024 * thread is found then it indicates that the owner TID has died.
1025 *
1026 * [3] Invalid. The waiter is queued on a non PI futex
1027 *
1028 * [4] Valid state after exit_robust_list(), which sets the user space
1029 * value to FUTEX_WAITERS | FUTEX_OWNER_DIED.
1030 *
1031 * [5] The user space value got manipulated between exit_robust_list()
1032 * and exit_pi_state_list()
1033 *
1034 * [6] Valid state after exit_pi_state_list() which sets the new owner in
1035 * the pi_state but cannot access the user space value.
1036 *
1037 * [7] pi_state->owner can only be NULL when the OWNER_DIED bit is set.
1038 *
1039 * [8] Owner and user space value match
1040 *
1041 * [9] There is no transient state which sets the user space TID to 0
1042 * except exit_robust_list(), but this is indicated by the
1043 * FUTEX_OWNER_DIED bit. See [4]
1044 *
1045 * [10] There is no transient state which leaves owner and user space
Thomas Gleixnerb960d9a2021-02-03 13:45:39 +00001046 * TID out of sync. Except one error case where the kernel is denied
1047 * write access to the user address, see fixup_pi_state_owner().
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00001048 *
1049 *
1050 * Serialization and lifetime rules:
1051 *
1052 * hb->lock:
1053 *
1054 * hb -> futex_q, relation
1055 * futex_q -> pi_state, relation
1056 *
1057 * (cannot be raw because hb can contain arbitrary amount
1058 * of futex_q's)
1059 *
1060 * pi_mutex->wait_lock:
1061 *
1062 * {uval, pi_state}
1063 *
1064 * (and pi_mutex 'obviously')
1065 *
1066 * p->pi_lock:
1067 *
1068 * p->pi_state_list -> pi_state->list, relation
1069 *
1070 * pi_state->refcount:
1071 *
1072 * pi_state lifetime
1073 *
1074 *
1075 * Lock order:
1076 *
1077 * hb->lock
1078 * pi_mutex->wait_lock
1079 * p->pi_lock
1080 *
Thomas Gleixner54a21782014-06-03 12:27:08 +00001081 */
Thomas Gleixnere60cbc52014-06-11 20:45:39 +00001082
1083/*
1084 * Validate that the existing waiter has a pi_state and sanity check
1085 * the pi_state against the user space value. If correct, attach to
1086 * it.
1087 */
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00001088static int attach_to_pi_state(u32 __user *uaddr, u32 uval,
1089 struct futex_pi_state *pi_state,
Thomas Gleixnere60cbc52014-06-11 20:45:39 +00001090 struct futex_pi_state **ps)
1091{
1092 pid_t pid = uval & FUTEX_TID_MASK;
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00001093 int ret, uval2;
Thomas Gleixnere60cbc52014-06-11 20:45:39 +00001094
1095 /*
1096 * Userspace might have messed up non-PI and PI futexes [3]
1097 */
1098 if (unlikely(!pi_state))
1099 return -EINVAL;
1100
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00001101 /*
1102 * We get here with hb->lock held, and having found a
1103 * futex_top_waiter(). This means that futex_lock_pi() of said futex_q
1104 * has dropped the hb->lock in between queue_me() and unqueue_me_pi(),
1105 * which in turn means that futex_lock_pi() still has a reference on
1106 * our pi_state.
Ben Hutchings312d9d62021-03-01 18:31:30 +01001107 *
1108 * The waiter holding a reference on @pi_state also protects against
1109 * the unlocked put_pi_state() in futex_unlock_pi(), futex_lock_pi()
1110 * and futex_wait_requeue_pi() as it cannot go to 0 and consequently
1111 * free pi_state before we can take a reference ourselves.
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00001112 */
Thomas Gleixnere60cbc52014-06-11 20:45:39 +00001113 WARN_ON(!atomic_read(&pi_state->refcount));
1114
1115 /*
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00001116 * Now that we have a pi_state, we can acquire wait_lock
1117 * and do the state validation.
1118 */
1119 raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
1120
1121 /*
1122 * Since {uval, pi_state} is serialized by wait_lock, and our current
1123 * uval was read without holding it, it can have changed. Verify it
1124 * still is what we expect it to be, otherwise retry the entire
1125 * operation.
1126 */
1127 if (get_futex_value_locked(&uval2, uaddr))
1128 goto out_efault;
1129
1130 if (uval != uval2)
1131 goto out_eagain;
1132
1133 /*
Thomas Gleixnere60cbc52014-06-11 20:45:39 +00001134 * Handle the owner died case:
1135 */
1136 if (uval & FUTEX_OWNER_DIED) {
1137 /*
1138 * exit_pi_state_list sets owner to NULL and wakes the
1139 * topmost waiter. The task which acquires the
1140 * pi_state->rt_mutex will fixup owner.
1141 */
1142 if (!pi_state->owner) {
1143 /*
1144 * No pi state owner, but the user space TID
1145 * is not 0. Inconsistent state. [5]
1146 */
1147 if (pid)
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00001148 goto out_einval;
Thomas Gleixnere60cbc52014-06-11 20:45:39 +00001149 /*
1150 * Take a ref on the state and return success. [4]
1151 */
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00001152 goto out_attach;
Thomas Gleixnere60cbc52014-06-11 20:45:39 +00001153 }
1154
1155 /*
1156 * If TID is 0, then either the dying owner has not
1157 * yet executed exit_pi_state_list() or some waiter
1158 * acquired the rtmutex in the pi state, but did not
1159 * yet fixup the TID in user space.
1160 *
1161 * Take a ref on the state and return success. [6]
1162 */
1163 if (!pid)
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00001164 goto out_attach;
Thomas Gleixnere60cbc52014-06-11 20:45:39 +00001165 } else {
1166 /*
1167 * If the owner died bit is not set, then the pi_state
1168 * must have an owner. [7]
1169 */
1170 if (!pi_state->owner)
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00001171 goto out_einval;
Thomas Gleixnere60cbc52014-06-11 20:45:39 +00001172 }
1173
1174 /*
1175 * Bail out if user space manipulated the futex value. If pi
1176 * state exists then the owner TID must be the same as the
1177 * user space TID. [9/10]
1178 */
1179 if (pid != task_pid_vnr(pi_state->owner))
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00001180 goto out_einval;
1181
1182out_attach:
Ben Hutchings921a7e32021-03-01 18:31:22 +01001183 get_pi_state(pi_state);
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00001184 raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
Thomas Gleixnere60cbc52014-06-11 20:45:39 +00001185 *ps = pi_state;
1186 return 0;
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00001187
1188out_einval:
1189 ret = -EINVAL;
1190 goto out_error;
1191
1192out_eagain:
1193 ret = -EAGAIN;
1194 goto out_error;
1195
1196out_efault:
1197 ret = -EFAULT;
1198 goto out_error;
1199
1200out_error:
1201 raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
1202 return ret;
Thomas Gleixnere60cbc52014-06-11 20:45:39 +00001203}
1204
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001205/**
1206 * wait_for_owner_exiting - Block until the owner has exited
1207 * @exiting: Pointer to the exiting task
1208 *
1209 * Caller must hold a refcount on @exiting.
1210 */
1211static void wait_for_owner_exiting(int ret, struct task_struct *exiting)
1212{
1213 if (ret != -EBUSY) {
1214 WARN_ON_ONCE(exiting);
1215 return;
1216 }
1217
1218 if (WARN_ON_ONCE(ret == -EBUSY && !exiting))
1219 return;
1220
1221 mutex_lock(&exiting->futex_exit_mutex);
1222 /*
1223 * No point in doing state checking here. If the waiter got here
1224 * while the task was in exec()->exec_futex_release() then it can
1225 * have any FUTEX_STATE_* value when the waiter has acquired the
1226 * mutex. OK, if running, EXITING or DEAD if it reached exit()
1227 * already. Highly unlikely and not a problem. Just one more round
1228 * through the futex maze.
1229 */
1230 mutex_unlock(&exiting->futex_exit_mutex);
1231
1232 put_task_struct(exiting);
1233}
1234
Thomas Gleixner9c3f3982021-02-11 09:27:00 +00001235static int handle_exit_race(u32 __user *uaddr, u32 uval,
1236 struct task_struct *tsk)
1237{
1238 u32 uval2;
1239
1240 /*
Thomas Gleixner91509e82021-02-24 18:09:23 +08001241 * If the futex exit state is not yet FUTEX_STATE_DEAD, tell the
1242 * caller that the alleged owner is busy.
Thomas Gleixner9c3f3982021-02-11 09:27:00 +00001243 */
1244 if (tsk && tsk->futex_state != FUTEX_STATE_DEAD)
Thomas Gleixner91509e82021-02-24 18:09:23 +08001245 return -EBUSY;
Thomas Gleixner9c3f3982021-02-11 09:27:00 +00001246
1247 /*
1248 * Reread the user space value to handle the following situation:
1249 *
1250 * CPU0 CPU1
1251 *
1252 * sys_exit() sys_futex()
1253 * do_exit() futex_lock_pi()
1254 * futex_lock_pi_atomic()
1255 * exit_signals(tsk) No waiters:
1256 * tsk->flags |= PF_EXITING; *uaddr == 0x00000PID
1257 * mm_release(tsk) Set waiter bit
1258 * exit_robust_list(tsk) { *uaddr = 0x80000PID;
1259 * Set owner died attach_to_pi_owner() {
1260 * *uaddr = 0xC0000000; tsk = get_task(PID);
1261 * } if (!tsk->flags & PF_EXITING) {
1262 * ... attach();
1263 * tsk->futex_state = } else {
1264 * FUTEX_STATE_DEAD; if (tsk->futex_state !=
1265 * FUTEX_STATE_DEAD)
1266 * return -EAGAIN;
1267 * return -ESRCH; <--- FAIL
1268 * }
1269 *
1270 * Returning ESRCH unconditionally is wrong here because the
1271 * user space value has been changed by the exiting task.
1272 *
1273 * The same logic applies to the case where the exiting task is
1274 * already gone.
1275 */
1276 if (get_futex_value_locked(&uval2, uaddr))
1277 return -EFAULT;
1278
1279 /* If the user space value has changed, try again. */
1280 if (uval2 != uval)
1281 return -EAGAIN;
1282
1283 /*
1284 * The exiting task did not have a robust list, the robust list was
1285 * corrupted or the user space value in *uaddr is simply bogus.
1286 * Give up and tell user space.
1287 */
1288 return -ESRCH;
1289}
1290
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001291/*
1292 * Lookup the task for the TID provided from user space and attach to
1293 * it after doing proper sanity checks.
1294 */
Thomas Gleixner9c3f3982021-02-11 09:27:00 +00001295static int attach_to_pi_owner(u32 __user *uaddr, u32 uval, union futex_key *key,
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001296 struct futex_pi_state **ps,
1297 struct task_struct **exiting)
Ingo Molnarc87e2832006-06-27 02:54:58 -07001298{
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001299 pid_t pid = uval & FUTEX_TID_MASK;
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001300 struct futex_pi_state *pi_state;
1301 struct task_struct *p;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001302
1303 /*
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001304 * We are the first waiter - try to look up the real owner and attach
Thomas Gleixner54a21782014-06-03 12:27:08 +00001305 * the new pi_state to it, but bail out when TID = 0 [1]
Thomas Gleixner9c3f3982021-02-11 09:27:00 +00001306 *
1307 * The !pid check is paranoid. None of the call sites should end up
1308 * with pid == 0, but better safe than sorry. Let the caller retry
Ingo Molnarc87e2832006-06-27 02:54:58 -07001309 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001310 if (!pid)
Thomas Gleixner9c3f3982021-02-11 09:27:00 +00001311 return -EAGAIN;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001312 p = futex_find_get_task(pid);
Michal Hocko7a0ea092010-06-30 09:51:19 +02001313 if (!p)
Thomas Gleixner9c3f3982021-02-11 09:27:00 +00001314 return handle_exit_race(uaddr, uval, NULL);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001315
Oleg Nesterova2129462015-02-02 15:05:36 +01001316 if (unlikely(p->flags & PF_KTHREAD)) {
Thomas Gleixnerf0d71b32014-05-12 20:45:35 +00001317 put_task_struct(p);
1318 return -EPERM;
1319 }
1320
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001321 /*
Thomas Gleixner2c116892021-02-01 10:01:34 +00001322 * We need to look at the task state to figure out, whether the
1323 * task is exiting. To protect against the change of the task state
1324 * in futex_exit_release(), we do this protected by p->pi_lock:
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001325 */
Thomas Gleixner1d615482009-11-17 14:54:03 +01001326 raw_spin_lock_irq(&p->pi_lock);
Thomas Gleixner2c116892021-02-01 10:01:34 +00001327 if (unlikely(p->futex_state != FUTEX_STATE_OK)) {
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001328 /*
Thomas Gleixner2c116892021-02-01 10:01:34 +00001329 * The task is on the way out. When the futex state is
1330 * FUTEX_STATE_DEAD, we know that the task has finished
1331 * the cleanup:
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001332 */
Thomas Gleixner9c3f3982021-02-11 09:27:00 +00001333 int ret = handle_exit_race(uaddr, uval, p);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001334
Thomas Gleixner1d615482009-11-17 14:54:03 +01001335 raw_spin_unlock_irq(&p->pi_lock);
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001336 /*
1337 * If the owner task is between FUTEX_STATE_EXITING and
1338 * FUTEX_STATE_DEAD then store the task pointer and keep
1339 * the reference on the task struct. The calling code will
1340 * drop all locks, wait for the task to reach
1341 * FUTEX_STATE_DEAD and then drop the refcount. This is
1342 * required to prevent a live lock when the current task
1343 * preempted the exiting task between the two states.
1344 */
1345 if (ret == -EBUSY)
1346 *exiting = p;
1347 else
1348 put_task_struct(p);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001349 return ret;
1350 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001351
Thomas Gleixner54a21782014-06-03 12:27:08 +00001352 /*
1353 * No existing pi state. First waiter. [2]
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00001354 *
1355 * This creates pi_state, we have hb->lock held, this means nothing can
1356 * observe this state, wait_lock is irrelevant.
Thomas Gleixner54a21782014-06-03 12:27:08 +00001357 */
Ingo Molnarc87e2832006-06-27 02:54:58 -07001358 pi_state = alloc_pi_state();
1359
1360 /*
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001361 * Initialize the pi_mutex in locked state and make @p
Ingo Molnarc87e2832006-06-27 02:54:58 -07001362 * the owner of it:
1363 */
1364 rt_mutex_init_proxy_locked(&pi_state->pi_mutex, p);
1365
1366 /* Store the key for possible exit cleanups: */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001367 pi_state->key = *key;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001368
Ingo Molnar627371d2006-07-29 05:16:20 +02001369 WARN_ON(!list_empty(&pi_state->list));
Ingo Molnarc87e2832006-06-27 02:54:58 -07001370 list_add(&pi_state->list, &p->pi_state_list);
Ben Hutchings9787adc2021-03-01 18:31:48 +01001371 /*
1372 * Assignment without holding pi_state->pi_mutex.wait_lock is safe
1373 * because there is no concurrency as the object is not published yet.
1374 */
Ingo Molnarc87e2832006-06-27 02:54:58 -07001375 pi_state->owner = p;
Thomas Gleixner1d615482009-11-17 14:54:03 +01001376 raw_spin_unlock_irq(&p->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001377
1378 put_task_struct(p);
1379
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001380 *ps = pi_state;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001381
1382 return 0;
1383}
1384
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00001385static int lookup_pi_state(u32 __user *uaddr, u32 uval,
1386 struct futex_hash_bucket *hb,
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001387 union futex_key *key, struct futex_pi_state **ps,
1388 struct task_struct **exiting)
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001389{
Ben Hutchingsbfefc9e2021-03-01 18:30:39 +01001390 struct futex_q *top_waiter = futex_top_waiter(hb, key);
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001391
1392 /*
1393 * If there is a waiter on that futex, validate it and
1394 * attach to the pi_state when the validation succeeds.
1395 */
Ben Hutchingsbfefc9e2021-03-01 18:30:39 +01001396 if (top_waiter)
1397 return attach_to_pi_state(uaddr, uval, top_waiter->pi_state, ps);
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001398
1399 /*
1400 * We are the first waiter - try to look up the owner based on
1401 * @uval and attach to it.
1402 */
Thomas Gleixner9c3f3982021-02-11 09:27:00 +00001403 return attach_to_pi_owner(uaddr, uval, key, ps, exiting);
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001404}
1405
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001406static int lock_pi_update_atomic(u32 __user *uaddr, u32 uval, u32 newval)
1407{
1408 u32 uninitialized_var(curval);
1409
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001410 if (unlikely(should_fail_futex(true)))
1411 return -EFAULT;
1412
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001413 if (unlikely(cmpxchg_futex_value_locked(&curval, uaddr, uval, newval)))
1414 return -EFAULT;
1415
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00001416 /* If user space value changed, let the caller retry */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001417 return curval != uval ? -EAGAIN : 0;
1418}
1419
Darren Hart1a520842009-04-03 13:39:52 -07001420/**
Darren Hartd96ee562009-09-21 22:30:22 -07001421 * futex_lock_pi_atomic() - Atomic work required to acquire a pi aware futex
Darren Hartbab5bc92009-04-07 23:23:50 -07001422 * @uaddr: the pi futex user address
1423 * @hb: the pi futex hash bucket
1424 * @key: the futex key associated with uaddr and hb
1425 * @ps: the pi_state pointer where we store the result of the
1426 * lookup
1427 * @task: the task to perform the atomic lock work for. This will
1428 * be "current" except in the case of requeue pi.
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001429 * @exiting: Pointer to store the task pointer of the owner task
1430 * which is in the middle of exiting
Darren Hartbab5bc92009-04-07 23:23:50 -07001431 * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0)
Darren Hart1a520842009-04-03 13:39:52 -07001432 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08001433 * Return:
1434 * 0 - ready to wait;
1435 * 1 - acquired the lock;
Darren Hart1a520842009-04-03 13:39:52 -07001436 * <0 - error
1437 *
1438 * The hb->lock and futex_key refs shall be held by the caller.
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001439 *
1440 * @exiting is only set when the return value is -EBUSY. If so, this holds
1441 * a refcount on the exiting task on return and the caller needs to drop it
1442 * after waiting for the exit to complete.
Darren Hart1a520842009-04-03 13:39:52 -07001443 */
1444static int futex_lock_pi_atomic(u32 __user *uaddr, struct futex_hash_bucket *hb,
1445 union futex_key *key,
1446 struct futex_pi_state **ps,
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001447 struct task_struct *task,
1448 struct task_struct **exiting,
1449 int set_waiters)
Darren Hart1a520842009-04-03 13:39:52 -07001450{
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001451 u32 uval, newval, vpid = task_pid_vnr(task);
Ben Hutchingsbfefc9e2021-03-01 18:30:39 +01001452 struct futex_q *top_waiter;
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001453 int ret;
Darren Hart1a520842009-04-03 13:39:52 -07001454
1455 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001456 * Read the user space value first so we can validate a few
1457 * things before proceeding further.
Darren Hart1a520842009-04-03 13:39:52 -07001458 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001459 if (get_futex_value_locked(&uval, uaddr))
Darren Hart1a520842009-04-03 13:39:52 -07001460 return -EFAULT;
1461
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001462 if (unlikely(should_fail_futex(true)))
1463 return -EFAULT;
1464
Darren Hart1a520842009-04-03 13:39:52 -07001465 /*
1466 * Detect deadlocks.
1467 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001468 if ((unlikely((uval & FUTEX_TID_MASK) == vpid)))
Darren Hart1a520842009-04-03 13:39:52 -07001469 return -EDEADLK;
1470
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001471 if ((unlikely(should_fail_futex(true))))
1472 return -EDEADLK;
1473
Darren Hart1a520842009-04-03 13:39:52 -07001474 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001475 * Lookup existing state first. If it exists, try to attach to
1476 * its pi_state.
Darren Hart1a520842009-04-03 13:39:52 -07001477 */
Ben Hutchingsbfefc9e2021-03-01 18:30:39 +01001478 top_waiter = futex_top_waiter(hb, key);
1479 if (top_waiter)
1480 return attach_to_pi_state(uaddr, uval, top_waiter->pi_state, ps);
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001481
1482 /*
1483 * No waiter and user TID is 0. We are here because the
1484 * waiters or the owner died bit is set or called from
1485 * requeue_cmp_pi or for whatever reason something took the
1486 * syscall.
1487 */
1488 if (!(uval & FUTEX_TID_MASK)) {
Thomas Gleixnerb3eaa9f2014-06-03 12:27:06 +00001489 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001490 * We take over the futex. No other waiters and the user space
1491 * TID is 0. We preserve the owner died bit.
Thomas Gleixnerb3eaa9f2014-06-03 12:27:06 +00001492 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001493 newval = uval & FUTEX_OWNER_DIED;
1494 newval |= vpid;
1495
1496 /* The futex requeue_pi code can enforce the waiters bit */
1497 if (set_waiters)
1498 newval |= FUTEX_WAITERS;
1499
1500 ret = lock_pi_update_atomic(uaddr, uval, newval);
1501 /* If the take over worked, return 1 */
1502 return ret < 0 ? ret : 1;
Thomas Gleixnerb3eaa9f2014-06-03 12:27:06 +00001503 }
Darren Hart1a520842009-04-03 13:39:52 -07001504
Darren Hart1a520842009-04-03 13:39:52 -07001505 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001506 * First waiter. Set the waiters bit before attaching ourself to
1507 * the owner. If owner tries to unlock, it will be forced into
1508 * the kernel and blocked on hb->lock.
Darren Hart1a520842009-04-03 13:39:52 -07001509 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001510 newval = uval | FUTEX_WAITERS;
1511 ret = lock_pi_update_atomic(uaddr, uval, newval);
1512 if (ret)
1513 return ret;
Darren Hart1a520842009-04-03 13:39:52 -07001514 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001515 * If the update of the user space value succeeded, we try to
1516 * attach to the owner. If that fails, no harm done, we only
1517 * set the FUTEX_WAITERS bit in the user space variable.
Darren Hart1a520842009-04-03 13:39:52 -07001518 */
Thomas Gleixner9c3f3982021-02-11 09:27:00 +00001519 return attach_to_pi_owner(uaddr, newval, key, ps, exiting);
Darren Hart1a520842009-04-03 13:39:52 -07001520}
1521
Lai Jiangshan2e129782010-12-22 14:18:50 +08001522/**
1523 * __unqueue_futex() - Remove the futex_q from its futex_hash_bucket
1524 * @q: The futex_q to unqueue
1525 *
1526 * The q->lock_ptr must not be NULL and must be held by the caller.
1527 */
1528static void __unqueue_futex(struct futex_q *q)
1529{
1530 struct futex_hash_bucket *hb;
1531
Steven Rostedt29096202011-03-17 15:21:07 -04001532 if (WARN_ON_SMP(!q->lock_ptr || !spin_is_locked(q->lock_ptr))
1533 || WARN_ON(plist_node_empty(&q->list)))
Lai Jiangshan2e129782010-12-22 14:18:50 +08001534 return;
1535
1536 hb = container_of(q->lock_ptr, struct futex_hash_bucket, lock);
1537 plist_del(&q->list, &hb->chain);
Linus Torvalds11d46162014-03-20 22:11:17 -07001538 hb_waiters_dec(hb);
Lai Jiangshan2e129782010-12-22 14:18:50 +08001539}
1540
Ingo Molnarc87e2832006-06-27 02:54:58 -07001541/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 * The hash bucket lock must be held when this is called.
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001543 * Afterwards, the futex_q must not be accessed. Callers
1544 * must ensure to later call wake_up_q() for the actual
1545 * wakeups to occur.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 */
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001547static void mark_wake_futex(struct wake_q_head *wake_q, struct futex_q *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001549 struct task_struct *p = q->task;
1550
Darren Hartaa109902012-11-26 16:29:56 -08001551 if (WARN(q->pi_state || q->rt_waiter, "refusing to wake PI futex\n"))
1552 return;
1553
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001554 /*
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001555 * Queue the task for later wakeup for after we've released
1556 * the hb->lock. wake_q_add() grabs reference to p.
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001557 */
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001558 wake_q_add(wake_q, 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 */
Ralf Baechleccdea2f2006-12-06 20:40:26 -08001566 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 q->lock_ptr = NULL;
1568}
1569
Ben Hutchings312d9d62021-03-01 18:31:30 +01001570/*
1571 * Caller must hold a reference on @pi_state.
1572 */
1573static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_pi_state *pi_state)
Ingo Molnarc87e2832006-06-27 02:54:58 -07001574{
Vitaliy Ivanov7cfdaf32011-07-07 15:10:31 +03001575 u32 uninitialized_var(curval), newval;
Ben Hutchings312d9d62021-03-01 18:31:30 +01001576 struct task_struct *new_owner;
1577 bool deboost = false;
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001578 WAKE_Q(wake_q);
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001579 int ret = 0;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001580
Ingo Molnarc87e2832006-06-27 02:54:58 -07001581 new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);
Ben Hutchings25a678d2021-03-01 18:31:39 +01001582 if (WARN_ON_ONCE(!new_owner)) {
Ben Hutchings312d9d62021-03-01 18:31:30 +01001583 /*
Ben Hutchings25a678d2021-03-01 18:31:39 +01001584 * As per the comment in futex_unlock_pi() this should not happen.
Ben Hutchings312d9d62021-03-01 18:31:30 +01001585 *
1586 * When this happens, give up our locks and try again, giving
1587 * the futex_lock_pi() instance time to complete, either by
1588 * waiting on the rtmutex or removing itself from the futex
1589 * queue.
1590 */
1591 ret = -EAGAIN;
1592 goto out_unlock;
Peter Zijlstra71f093c2021-02-03 13:45:32 +00001593 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001594
1595 /*
Ben Hutchings312d9d62021-03-01 18:31:30 +01001596 * We pass it to the next owner. The WAITERS bit is always kept
1597 * enabled while there is PI state around. We cleanup the owner
1598 * died bit, because we are the owner.
Ingo Molnarc87e2832006-06-27 02:54:58 -07001599 */
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001600 newval = FUTEX_WAITERS | task_pid_vnr(new_owner);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001601
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001602 if (unlikely(should_fail_futex(true)))
1603 ret = -EFAULT;
1604
Sebastian Andrzej Siewior89e9e662016-04-15 14:35:39 +02001605 if (cmpxchg_futex_value_locked(&curval, uaddr, uval, newval)) {
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001606 ret = -EFAULT;
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00001607
Sebastian Andrzej Siewior89e9e662016-04-15 14:35:39 +02001608 } else if (curval != uval) {
1609 /*
1610 * If a unconditional UNLOCK_PI operation (user space did not
1611 * try the TID->0 transition) raced with a waiter setting the
1612 * FUTEX_WAITERS flag between get_user() and locking the hash
1613 * bucket lock, retry the operation.
1614 */
1615 if ((FUTEX_TID_MASK & curval) == uval)
1616 ret = -EAGAIN;
1617 else
1618 ret = -EINVAL;
1619 }
Thomas Gleixner76bc0ec2021-02-03 13:45:35 +00001620
1621 if (!ret) {
1622 /*
1623 * This is a point of no return; once we modified the uval
1624 * there is no going back and subsequent operations must
1625 * not fail.
1626 */
1627 pi_state_update_owner(pi_state, new_owner);
1628 deboost = __rt_mutex_futex_unlock(&pi_state->pi_mutex, &wake_q);
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001629 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001630
Ben Hutchings312d9d62021-03-01 18:31:30 +01001631out_unlock:
Peter Zijlstra2c60d4a2021-02-03 13:45:30 +00001632 raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
Peter Zijlstra2c60d4a2021-02-03 13:45:30 +00001633
1634 if (deboost) {
1635 wake_up_q(&wake_q);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001636 rt_mutex_adjust_prio(current);
Peter Zijlstra2c60d4a2021-02-03 13:45:30 +00001637 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001638
Ben Hutchings312d9d62021-03-01 18:31:30 +01001639 return ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001640}
1641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642/*
Ingo Molnar8b8f3192006-07-03 00:25:05 -07001643 * Express the locking dependencies for lockdep:
1644 */
1645static inline void
1646double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
1647{
1648 if (hb1 <= hb2) {
1649 spin_lock(&hb1->lock);
1650 if (hb1 < hb2)
1651 spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING);
1652 } else { /* hb1 > hb2 */
1653 spin_lock(&hb2->lock);
1654 spin_lock_nested(&hb1->lock, SINGLE_DEPTH_NESTING);
1655 }
1656}
1657
Darren Hart5eb3dc62009-03-12 00:55:52 -07001658static inline void
1659double_unlock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
1660{
Darren Hartf061d352009-03-12 15:11:18 -07001661 spin_unlock(&hb1->lock);
Ingo Molnar88f502f2009-03-13 10:32:07 +01001662 if (hb1 != hb2)
1663 spin_unlock(&hb2->lock);
Darren Hart5eb3dc62009-03-12 00:55:52 -07001664}
1665
Ingo Molnar8b8f3192006-07-03 00:25:05 -07001666/*
Darren Hartb2d09942009-03-12 00:55:37 -07001667 * Wake up waiters matching bitset queued on this futex (uaddr).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 */
Darren Hartb41277d2010-11-08 13:10:09 -08001669static int
1670futex_wake(u32 __user *uaddr, unsigned int flags, int nr_wake, u32 bitset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
Ingo Molnare2970f22006-06-27 02:54:47 -07001672 struct futex_hash_bucket *hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 struct futex_q *this, *next;
Peter Zijlstra38d47c12008-09-26 19:32:20 +02001674 union futex_key key = FUTEX_KEY_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 int ret;
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001676 WAKE_Q(wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Thomas Gleixnercd689982008-02-01 17:45:14 +01001678 if (!bitset)
1679 return -EINVAL;
1680
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001681 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 if (unlikely(ret != 0))
1683 goto out;
1684
Ingo Molnare2970f22006-06-27 02:54:47 -07001685 hb = hash_futex(&key);
Davidlohr Buesob0c29f72014-01-12 15:31:25 -08001686
1687 /* Make sure we really have tasks to wakeup */
1688 if (!hb_waiters_pending(hb))
1689 goto out_put_key;
1690
Ingo Molnare2970f22006-06-27 02:54:47 -07001691 spin_lock(&hb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Jason Low0d00c7b2014-01-12 15:31:22 -08001693 plist_for_each_entry_safe(this, next, &hb->chain, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 if (match_futex (&this->key, &key)) {
Darren Hart52400ba2009-04-03 13:40:49 -07001695 if (this->pi_state || this->rt_waiter) {
Ingo Molnared6f7b12006-07-01 04:35:46 -07001696 ret = -EINVAL;
1697 break;
1698 }
Thomas Gleixnercd689982008-02-01 17:45:14 +01001699
1700 /* Check if one of the bits is set in both bitsets */
1701 if (!(this->bitset & bitset))
1702 continue;
1703
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001704 mark_wake_futex(&wake_q, this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 if (++ret >= nr_wake)
1706 break;
1707 }
1708 }
1709
Ingo Molnare2970f22006-06-27 02:54:47 -07001710 spin_unlock(&hb->lock);
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001711 wake_up_q(&wake_q);
Davidlohr Buesob0c29f72014-01-12 15:31:25 -08001712out_put_key:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001713 put_futex_key(&key);
Darren Hart42d35d42008-12-29 15:49:53 -08001714out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 return ret;
1716}
1717
Jiri Slaby81da9f82017-08-24 09:31:05 +02001718static int futex_atomic_op_inuser(unsigned int encoded_op, u32 __user *uaddr)
1719{
1720 unsigned int op = (encoded_op & 0x70000000) >> 28;
1721 unsigned int cmp = (encoded_op & 0x0f000000) >> 24;
Jiri Slabya1640092017-11-30 15:35:44 +01001722 int oparg = sign_extend32((encoded_op & 0x00fff000) >> 12, 11);
1723 int cmparg = sign_extend32(encoded_op & 0x00000fff, 11);
Jiri Slaby81da9f82017-08-24 09:31:05 +02001724 int oldval, ret;
1725
1726 if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28)) {
Jiri Slabyda92e8a2017-10-23 13:41:51 +02001727 if (oparg < 0 || oparg > 31) {
1728 char comm[sizeof(current->comm)];
1729 /*
1730 * kill this print and return -EINVAL when userspace
1731 * is sane again
1732 */
1733 pr_info_ratelimited("futex_wake_op: %s tries to shift op by %d; fix this program\n",
1734 get_task_comm(comm, current), oparg);
1735 oparg &= 31;
1736 }
Jiri Slaby81da9f82017-08-24 09:31:05 +02001737 oparg = 1 << oparg;
1738 }
1739
1740 if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
1741 return -EFAULT;
1742
1743 ret = arch_futex_atomic_op_inuser(op, oparg, &oldval, uaddr);
1744 if (ret)
1745 return ret;
1746
1747 switch (cmp) {
1748 case FUTEX_OP_CMP_EQ:
1749 return oldval == cmparg;
1750 case FUTEX_OP_CMP_NE:
1751 return oldval != cmparg;
1752 case FUTEX_OP_CMP_LT:
1753 return oldval < cmparg;
1754 case FUTEX_OP_CMP_GE:
1755 return oldval >= cmparg;
1756 case FUTEX_OP_CMP_LE:
1757 return oldval <= cmparg;
1758 case FUTEX_OP_CMP_GT:
1759 return oldval > cmparg;
1760 default:
1761 return -ENOSYS;
1762 }
1763}
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765/*
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001766 * Wake up all waiters hashed on the physical page that is mapped
1767 * to this virtual address:
1768 */
Ingo Molnare2970f22006-06-27 02:54:47 -07001769static int
Darren Hartb41277d2010-11-08 13:10:09 -08001770futex_wake_op(u32 __user *uaddr1, unsigned int flags, u32 __user *uaddr2,
Ingo Molnare2970f22006-06-27 02:54:47 -07001771 int nr_wake, int nr_wake2, int op)
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001772{
Peter Zijlstra38d47c12008-09-26 19:32:20 +02001773 union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
Ingo Molnare2970f22006-06-27 02:54:47 -07001774 struct futex_hash_bucket *hb1, *hb2;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001775 struct futex_q *this, *next;
Darren Harte4dc5b72009-03-12 00:56:13 -07001776 int ret, op_ret;
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001777 WAKE_Q(wake_q);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001778
Darren Harte4dc5b72009-03-12 00:56:13 -07001779retry:
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001780 ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, VERIFY_READ);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001781 if (unlikely(ret != 0))
1782 goto out;
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001783 ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001784 if (unlikely(ret != 0))
Darren Hart42d35d42008-12-29 15:49:53 -08001785 goto out_put_key1;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001786
Ingo Molnare2970f22006-06-27 02:54:47 -07001787 hb1 = hash_futex(&key1);
1788 hb2 = hash_futex(&key2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001789
Darren Harte4dc5b72009-03-12 00:56:13 -07001790retry_private:
Thomas Gleixnereaaea802009-10-04 09:34:17 +02001791 double_lock_hb(hb1, hb2);
Ingo Molnare2970f22006-06-27 02:54:47 -07001792 op_ret = futex_atomic_op_inuser(op, uaddr2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001793 if (unlikely(op_ret < 0)) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001794
Darren Hart5eb3dc62009-03-12 00:55:52 -07001795 double_unlock_hb(hb1, hb2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001796
David Howells7ee1dd32006-01-06 00:11:44 -08001797#ifndef CONFIG_MMU
Ingo Molnare2970f22006-06-27 02:54:47 -07001798 /*
1799 * we don't get EFAULT from MMU faults if we don't have an MMU,
1800 * but we might get them from range checking
1801 */
David Howells7ee1dd32006-01-06 00:11:44 -08001802 ret = op_ret;
Darren Hart42d35d42008-12-29 15:49:53 -08001803 goto out_put_keys;
David Howells7ee1dd32006-01-06 00:11:44 -08001804#endif
1805
David Gibson796f8d92005-11-07 00:59:33 -08001806 if (unlikely(op_ret != -EFAULT)) {
1807 ret = op_ret;
Darren Hart42d35d42008-12-29 15:49:53 -08001808 goto out_put_keys;
David Gibson796f8d92005-11-07 00:59:33 -08001809 }
1810
Thomas Gleixnerd0725992009-06-11 23:15:43 +02001811 ret = fault_in_user_writeable(uaddr2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001812 if (ret)
Darren Hartde87fcc2009-03-12 00:55:46 -07001813 goto out_put_keys;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001814
Darren Hartb41277d2010-11-08 13:10:09 -08001815 if (!(flags & FLAGS_SHARED))
Darren Harte4dc5b72009-03-12 00:56:13 -07001816 goto retry_private;
1817
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001818 put_futex_key(&key2);
1819 put_futex_key(&key1);
Darren Harte4dc5b72009-03-12 00:56:13 -07001820 goto retry;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001821 }
1822
Jason Low0d00c7b2014-01-12 15:31:22 -08001823 plist_for_each_entry_safe(this, next, &hb1->chain, list) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001824 if (match_futex (&this->key, &key1)) {
Darren Hartaa109902012-11-26 16:29:56 -08001825 if (this->pi_state || this->rt_waiter) {
1826 ret = -EINVAL;
1827 goto out_unlock;
1828 }
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001829 mark_wake_futex(&wake_q, this);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001830 if (++ret >= nr_wake)
1831 break;
1832 }
1833 }
1834
1835 if (op_ret > 0) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001836 op_ret = 0;
Jason Low0d00c7b2014-01-12 15:31:22 -08001837 plist_for_each_entry_safe(this, next, &hb2->chain, list) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001838 if (match_futex (&this->key, &key2)) {
Darren Hartaa109902012-11-26 16:29:56 -08001839 if (this->pi_state || this->rt_waiter) {
1840 ret = -EINVAL;
1841 goto out_unlock;
1842 }
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001843 mark_wake_futex(&wake_q, this);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001844 if (++op_ret >= nr_wake2)
1845 break;
1846 }
1847 }
1848 ret += op_ret;
1849 }
1850
Darren Hartaa109902012-11-26 16:29:56 -08001851out_unlock:
Darren Hart5eb3dc62009-03-12 00:55:52 -07001852 double_unlock_hb(hb1, hb2);
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001853 wake_up_q(&wake_q);
Darren Hart42d35d42008-12-29 15:49:53 -08001854out_put_keys:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001855 put_futex_key(&key2);
Darren Hart42d35d42008-12-29 15:49:53 -08001856out_put_key1:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001857 put_futex_key(&key1);
Darren Hart42d35d42008-12-29 15:49:53 -08001858out:
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001859 return ret;
1860}
1861
Darren Hart9121e472009-04-03 13:40:31 -07001862/**
1863 * requeue_futex() - Requeue a futex_q from one hb to another
1864 * @q: the futex_q to requeue
1865 * @hb1: the source hash_bucket
1866 * @hb2: the target hash_bucket
1867 * @key2: the new key for the requeued futex_q
1868 */
1869static inline
1870void requeue_futex(struct futex_q *q, struct futex_hash_bucket *hb1,
1871 struct futex_hash_bucket *hb2, union futex_key *key2)
1872{
1873
1874 /*
1875 * If key1 and key2 hash to the same bucket, no need to
1876 * requeue.
1877 */
1878 if (likely(&hb1->chain != &hb2->chain)) {
1879 plist_del(&q->list, &hb1->chain);
Linus Torvalds11d46162014-03-20 22:11:17 -07001880 hb_waiters_dec(hb1);
Linus Torvalds11d46162014-03-20 22:11:17 -07001881 hb_waiters_inc(hb2);
Davidlohr Buesofe1bce92016-04-20 20:09:24 -07001882 plist_add(&q->list, &hb2->chain);
Darren Hart9121e472009-04-03 13:40:31 -07001883 q->lock_ptr = &hb2->lock;
Darren Hart9121e472009-04-03 13:40:31 -07001884 }
1885 get_futex_key_refs(key2);
1886 q->key = *key2;
1887}
1888
Darren Hart52400ba2009-04-03 13:40:49 -07001889/**
1890 * requeue_pi_wake_futex() - Wake a task that acquired the lock during requeue
Darren Hartd96ee562009-09-21 22:30:22 -07001891 * @q: the futex_q
1892 * @key: the key of the requeue target futex
1893 * @hb: the hash_bucket of the requeue target futex
Darren Hart52400ba2009-04-03 13:40:49 -07001894 *
1895 * During futex_requeue, with requeue_pi=1, it is possible to acquire the
1896 * target futex if it is uncontended or via a lock steal. Set the futex_q key
1897 * to the requeue target futex so the waiter can detect the wakeup on the right
1898 * futex, but remove it from the hb and NULL the rt_waiter so it can detect
Darren Hartbeda2c72009-08-09 15:34:39 -07001899 * atomic lock acquisition. Set the q->lock_ptr to the requeue target hb->lock
1900 * to protect access to the pi_state to fixup the owner later. Must be called
1901 * with both q->lock_ptr and hb->lock held.
Darren Hart52400ba2009-04-03 13:40:49 -07001902 */
1903static inline
Darren Hartbeda2c72009-08-09 15:34:39 -07001904void requeue_pi_wake_futex(struct futex_q *q, union futex_key *key,
1905 struct futex_hash_bucket *hb)
Darren Hart52400ba2009-04-03 13:40:49 -07001906{
Darren Hart52400ba2009-04-03 13:40:49 -07001907 get_futex_key_refs(key);
1908 q->key = *key;
1909
Lai Jiangshan2e129782010-12-22 14:18:50 +08001910 __unqueue_futex(q);
Darren Hart52400ba2009-04-03 13:40:49 -07001911
1912 WARN_ON(!q->rt_waiter);
1913 q->rt_waiter = NULL;
1914
Darren Hartbeda2c72009-08-09 15:34:39 -07001915 q->lock_ptr = &hb->lock;
Darren Hartbeda2c72009-08-09 15:34:39 -07001916
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001917 wake_up_state(q->task, TASK_NORMAL);
Darren Hart52400ba2009-04-03 13:40:49 -07001918}
1919
1920/**
1921 * futex_proxy_trylock_atomic() - Attempt an atomic lock for the top waiter
Darren Hartbab5bc92009-04-07 23:23:50 -07001922 * @pifutex: the user address of the to futex
1923 * @hb1: the from futex hash bucket, must be locked by the caller
1924 * @hb2: the to futex hash bucket, must be locked by the caller
1925 * @key1: the from futex key
1926 * @key2: the to futex key
1927 * @ps: address to store the pi_state pointer
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001928 * @exiting: Pointer to store the task pointer of the owner task
1929 * which is in the middle of exiting
Darren Hartbab5bc92009-04-07 23:23:50 -07001930 * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0)
Darren Hart52400ba2009-04-03 13:40:49 -07001931 *
1932 * Try and get the lock on behalf of the top waiter if we can do it atomically.
Darren Hartbab5bc92009-04-07 23:23:50 -07001933 * Wake the top waiter if we succeed. If the caller specified set_waiters,
1934 * then direct futex_lock_pi_atomic() to force setting the FUTEX_WAITERS bit.
1935 * hb1 and hb2 must be held by the caller.
Darren Hart52400ba2009-04-03 13:40:49 -07001936 *
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001937 * @exiting is only set when the return value is -EBUSY. If so, this holds
1938 * a refcount on the exiting task on return and the caller needs to drop it
1939 * after waiting for the exit to complete.
1940 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08001941 * Return:
1942 * 0 - failed to acquire the lock atomically;
Thomas Gleixner866293e2014-05-12 20:45:34 +00001943 * >0 - acquired the lock, return value is vpid of the top_waiter
Darren Hart52400ba2009-04-03 13:40:49 -07001944 * <0 - error
1945 */
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001946static int
1947futex_proxy_trylock_atomic(u32 __user *pifutex, struct futex_hash_bucket *hb1,
1948 struct futex_hash_bucket *hb2, union futex_key *key1,
1949 union futex_key *key2, struct futex_pi_state **ps,
1950 struct task_struct **exiting, int set_waiters)
Darren Hart52400ba2009-04-03 13:40:49 -07001951{
Darren Hartbab5bc92009-04-07 23:23:50 -07001952 struct futex_q *top_waiter = NULL;
Darren Hart52400ba2009-04-03 13:40:49 -07001953 u32 curval;
Thomas Gleixner866293e2014-05-12 20:45:34 +00001954 int ret, vpid;
Darren Hart52400ba2009-04-03 13:40:49 -07001955
1956 if (get_futex_value_locked(&curval, pifutex))
1957 return -EFAULT;
1958
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001959 if (unlikely(should_fail_futex(true)))
1960 return -EFAULT;
1961
Darren Hartbab5bc92009-04-07 23:23:50 -07001962 /*
1963 * Find the top_waiter and determine if there are additional waiters.
1964 * If the caller intends to requeue more than 1 waiter to pifutex,
1965 * force futex_lock_pi_atomic() to set the FUTEX_WAITERS bit now,
1966 * as we have means to handle the possible fault. If not, don't set
1967 * the bit unecessarily as it will force the subsequent unlock to enter
1968 * the kernel.
1969 */
Darren Hart52400ba2009-04-03 13:40:49 -07001970 top_waiter = futex_top_waiter(hb1, key1);
1971
1972 /* There are no waiters, nothing for us to do. */
1973 if (!top_waiter)
1974 return 0;
1975
Darren Hart84bc4af2009-08-13 17:36:53 -07001976 /* Ensure we requeue to the expected futex. */
1977 if (!match_futex(top_waiter->requeue_pi_key, key2))
1978 return -EINVAL;
1979
Darren Hart52400ba2009-04-03 13:40:49 -07001980 /*
Darren Hartbab5bc92009-04-07 23:23:50 -07001981 * Try to take the lock for top_waiter. Set the FUTEX_WAITERS bit in
1982 * the contended case or if set_waiters is 1. The pi_state is returned
1983 * in ps in contended cases.
Darren Hart52400ba2009-04-03 13:40:49 -07001984 */
Thomas Gleixner866293e2014-05-12 20:45:34 +00001985 vpid = task_pid_vnr(top_waiter->task);
Darren Hartbab5bc92009-04-07 23:23:50 -07001986 ret = futex_lock_pi_atomic(pifutex, hb2, key2, ps, top_waiter->task,
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001987 exiting, set_waiters);
Thomas Gleixner866293e2014-05-12 20:45:34 +00001988 if (ret == 1) {
Darren Hartbeda2c72009-08-09 15:34:39 -07001989 requeue_pi_wake_futex(top_waiter, key2, hb2);
Thomas Gleixner866293e2014-05-12 20:45:34 +00001990 return vpid;
1991 }
Darren Hart52400ba2009-04-03 13:40:49 -07001992 return ret;
1993}
1994
1995/**
1996 * futex_requeue() - Requeue waiters from uaddr1 to uaddr2
Randy Dunlapfb62db22010-10-13 11:02:34 -07001997 * @uaddr1: source futex user address
Darren Hartb41277d2010-11-08 13:10:09 -08001998 * @flags: futex flags (FLAGS_SHARED, etc.)
Randy Dunlapfb62db22010-10-13 11:02:34 -07001999 * @uaddr2: target futex user address
2000 * @nr_wake: number of waiters to wake (must be 1 for requeue_pi)
2001 * @nr_requeue: number of waiters to requeue (0-INT_MAX)
2002 * @cmpval: @uaddr1 expected value (or %NULL)
2003 * @requeue_pi: if we are attempting to requeue from a non-pi futex to a
Darren Hartb41277d2010-11-08 13:10:09 -08002004 * pi futex (pi to pi requeue is not supported)
Darren Hart52400ba2009-04-03 13:40:49 -07002005 *
2006 * Requeue waiters on uaddr1 to uaddr2. In the requeue_pi case, try to acquire
2007 * uaddr2 atomically on behalf of the top waiter.
2008 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002009 * Return:
2010 * >=0 - on success, the number of tasks requeued or woken;
Darren Hart52400ba2009-04-03 13:40:49 -07002011 * <0 - on error
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 */
Darren Hartb41277d2010-11-08 13:10:09 -08002013static int futex_requeue(u32 __user *uaddr1, unsigned int flags,
2014 u32 __user *uaddr2, int nr_wake, int nr_requeue,
2015 u32 *cmpval, int requeue_pi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
Peter Zijlstra38d47c12008-09-26 19:32:20 +02002017 union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
Darren Hart52400ba2009-04-03 13:40:49 -07002018 int drop_count = 0, task_count = 0, ret;
2019 struct futex_pi_state *pi_state = NULL;
Ingo Molnare2970f22006-06-27 02:54:47 -07002020 struct futex_hash_bucket *hb1, *hb2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 struct futex_q *this, *next;
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07002022 WAKE_Q(wake_q);
Darren Hart52400ba2009-04-03 13:40:49 -07002023
Li Jinyued8a31702017-12-14 17:04:54 +08002024 if (nr_wake < 0 || nr_requeue < 0)
2025 return -EINVAL;
2026
Darren Hart52400ba2009-04-03 13:40:49 -07002027 if (requeue_pi) {
2028 /*
Thomas Gleixnere9c243a2014-06-03 12:27:06 +00002029 * Requeue PI only works on two distinct uaddrs. This
2030 * check is only valid for private futexes. See below.
2031 */
2032 if (uaddr1 == uaddr2)
2033 return -EINVAL;
2034
2035 /*
Darren Hart52400ba2009-04-03 13:40:49 -07002036 * requeue_pi requires a pi_state, try to allocate it now
2037 * without any locks in case it fails.
2038 */
2039 if (refill_pi_state_cache())
2040 return -ENOMEM;
2041 /*
2042 * requeue_pi must wake as many tasks as it can, up to nr_wake
2043 * + nr_requeue, since it acquires the rt_mutex prior to
2044 * returning to userspace, so as to not leave the rt_mutex with
2045 * waiters and no owner. However, second and third wake-ups
2046 * cannot be predicted as they involve race conditions with the
2047 * first wake and a fault while looking up the pi_state. Both
2048 * pthread_cond_signal() and pthread_cond_broadcast() should
2049 * use nr_wake=1.
2050 */
2051 if (nr_wake != 1)
2052 return -EINVAL;
2053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Darren Hart42d35d42008-12-29 15:49:53 -08002055retry:
Shawn Bohrer9ea71502011-06-30 11:21:32 -05002056 ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 if (unlikely(ret != 0))
2058 goto out;
Shawn Bohrer9ea71502011-06-30 11:21:32 -05002059 ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2,
2060 requeue_pi ? VERIFY_WRITE : VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 if (unlikely(ret != 0))
Darren Hart42d35d42008-12-29 15:49:53 -08002062 goto out_put_key1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Thomas Gleixnere9c243a2014-06-03 12:27:06 +00002064 /*
2065 * The check above which compares uaddrs is not sufficient for
2066 * shared futexes. We need to compare the keys:
2067 */
2068 if (requeue_pi && match_futex(&key1, &key2)) {
2069 ret = -EINVAL;
2070 goto out_put_keys;
2071 }
2072
Ingo Molnare2970f22006-06-27 02:54:47 -07002073 hb1 = hash_futex(&key1);
2074 hb2 = hash_futex(&key2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Darren Harte4dc5b72009-03-12 00:56:13 -07002076retry_private:
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07002077 hb_waiters_inc(hb2);
Ingo Molnar8b8f3192006-07-03 00:25:05 -07002078 double_lock_hb(hb1, hb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Ingo Molnare2970f22006-06-27 02:54:47 -07002080 if (likely(cmpval != NULL)) {
2081 u32 curval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Ingo Molnare2970f22006-06-27 02:54:47 -07002083 ret = get_futex_value_locked(&curval, uaddr1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
2085 if (unlikely(ret)) {
Darren Hart5eb3dc62009-03-12 00:55:52 -07002086 double_unlock_hb(hb1, hb2);
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07002087 hb_waiters_dec(hb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Darren Harte4dc5b72009-03-12 00:56:13 -07002089 ret = get_user(curval, uaddr1);
2090 if (ret)
2091 goto out_put_keys;
2092
Darren Hartb41277d2010-11-08 13:10:09 -08002093 if (!(flags & FLAGS_SHARED))
Darren Harte4dc5b72009-03-12 00:56:13 -07002094 goto retry_private;
2095
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002096 put_futex_key(&key2);
2097 put_futex_key(&key1);
Darren Harte4dc5b72009-03-12 00:56:13 -07002098 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 }
Ingo Molnare2970f22006-06-27 02:54:47 -07002100 if (curval != *cmpval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 ret = -EAGAIN;
2102 goto out_unlock;
2103 }
2104 }
2105
Darren Hart52400ba2009-04-03 13:40:49 -07002106 if (requeue_pi && (task_count - nr_wake < nr_requeue)) {
Thomas Gleixnercf16e422021-02-01 10:01:43 +00002107 struct task_struct *exiting = NULL;
2108
Darren Hartbab5bc92009-04-07 23:23:50 -07002109 /*
2110 * Attempt to acquire uaddr2 and wake the top waiter. If we
2111 * intend to requeue waiters, force setting the FUTEX_WAITERS
2112 * bit. We force this here where we are able to easily handle
2113 * faults rather in the requeue loop below.
2114 */
Darren Hart52400ba2009-04-03 13:40:49 -07002115 ret = futex_proxy_trylock_atomic(uaddr2, hb1, hb2, &key1,
Thomas Gleixnercf16e422021-02-01 10:01:43 +00002116 &key2, &pi_state,
2117 &exiting, nr_requeue);
Darren Hart52400ba2009-04-03 13:40:49 -07002118
2119 /*
2120 * At this point the top_waiter has either taken uaddr2 or is
2121 * waiting on it. If the former, then the pi_state will not
2122 * exist yet, look it up one more time to ensure we have a
Thomas Gleixner866293e2014-05-12 20:45:34 +00002123 * reference to it. If the lock was taken, ret contains the
2124 * vpid of the top waiter task.
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00002125 * If the lock was not taken, we have pi_state and an initial
2126 * refcount on it. In case of an error we have nothing.
Darren Hart52400ba2009-04-03 13:40:49 -07002127 */
Thomas Gleixner866293e2014-05-12 20:45:34 +00002128 if (ret > 0) {
Darren Hart52400ba2009-04-03 13:40:49 -07002129 WARN_ON(pi_state);
Darren Hart89061d32009-10-15 15:30:48 -07002130 drop_count++;
Darren Hart52400ba2009-04-03 13:40:49 -07002131 task_count++;
Thomas Gleixner866293e2014-05-12 20:45:34 +00002132 /*
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00002133 * If we acquired the lock, then the user space value
2134 * of uaddr2 should be vpid. It cannot be changed by
2135 * the top waiter as it is blocked on hb2 lock if it
2136 * tries to do so. If something fiddled with it behind
2137 * our back the pi state lookup might unearth it. So
2138 * we rather use the known value than rereading and
2139 * handing potential crap to lookup_pi_state.
2140 *
2141 * If that call succeeds then we have pi_state and an
2142 * initial refcount on it.
Thomas Gleixner866293e2014-05-12 20:45:34 +00002143 */
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00002144 ret = lookup_pi_state(uaddr2, ret, hb2, &key2,
Thomas Gleixnercf16e422021-02-01 10:01:43 +00002145 &pi_state, &exiting);
Darren Hart52400ba2009-04-03 13:40:49 -07002146 }
2147
2148 switch (ret) {
2149 case 0:
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00002150 /* We hold a reference on the pi state. */
Darren Hart52400ba2009-04-03 13:40:49 -07002151 break;
Thomas Gleixner4959f2d2015-12-19 20:07:40 +00002152
2153 /* If the above failed, then pi_state is NULL */
Darren Hart52400ba2009-04-03 13:40:49 -07002154 case -EFAULT:
2155 double_unlock_hb(hb1, hb2);
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07002156 hb_waiters_dec(hb2);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002157 put_futex_key(&key2);
2158 put_futex_key(&key1);
Thomas Gleixnerd0725992009-06-11 23:15:43 +02002159 ret = fault_in_user_writeable(uaddr2);
Darren Hart52400ba2009-04-03 13:40:49 -07002160 if (!ret)
2161 goto retry;
2162 goto out;
Thomas Gleixnerc27f3922021-02-01 10:01:42 +00002163 case -EBUSY:
Darren Hart52400ba2009-04-03 13:40:49 -07002164 case -EAGAIN:
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00002165 /*
2166 * Two reasons for this:
Thomas Gleixnerc27f3922021-02-01 10:01:42 +00002167 * - EBUSY: Owner is exiting and we just wait for the
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00002168 * exit to complete.
Thomas Gleixnerc27f3922021-02-01 10:01:42 +00002169 * - EAGAIN: The user space value changed.
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00002170 */
Darren Hart52400ba2009-04-03 13:40:49 -07002171 double_unlock_hb(hb1, hb2);
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07002172 hb_waiters_dec(hb2);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002173 put_futex_key(&key2);
2174 put_futex_key(&key1);
Thomas Gleixnercf16e422021-02-01 10:01:43 +00002175 /*
2176 * Handle the case where the owner is in the middle of
2177 * exiting. Wait for the exit to complete otherwise
2178 * this task might loop forever, aka. live lock.
2179 */
2180 wait_for_owner_exiting(ret, exiting);
Darren Hart52400ba2009-04-03 13:40:49 -07002181 cond_resched();
2182 goto retry;
2183 default:
2184 goto out_unlock;
2185 }
2186 }
2187
Jason Low0d00c7b2014-01-12 15:31:22 -08002188 plist_for_each_entry_safe(this, next, &hb1->chain, list) {
Darren Hart52400ba2009-04-03 13:40:49 -07002189 if (task_count - nr_wake >= nr_requeue)
2190 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
Darren Hart52400ba2009-04-03 13:40:49 -07002192 if (!match_futex(&this->key, &key1))
2193 continue;
2194
Darren Hart392741e2009-08-07 15:20:48 -07002195 /*
2196 * FUTEX_WAIT_REQEUE_PI and FUTEX_CMP_REQUEUE_PI should always
2197 * be paired with each other and no other futex ops.
Darren Hartaa109902012-11-26 16:29:56 -08002198 *
2199 * We should never be requeueing a futex_q with a pi_state,
2200 * which is awaiting a futex_unlock_pi().
Darren Hart392741e2009-08-07 15:20:48 -07002201 */
2202 if ((requeue_pi && !this->rt_waiter) ||
Darren Hartaa109902012-11-26 16:29:56 -08002203 (!requeue_pi && this->rt_waiter) ||
2204 this->pi_state) {
Darren Hart392741e2009-08-07 15:20:48 -07002205 ret = -EINVAL;
2206 break;
2207 }
Darren Hart52400ba2009-04-03 13:40:49 -07002208
2209 /*
2210 * Wake nr_wake waiters. For requeue_pi, if we acquired the
2211 * lock, we already woke the top_waiter. If not, it will be
2212 * woken by futex_unlock_pi().
2213 */
2214 if (++task_count <= nr_wake && !requeue_pi) {
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07002215 mark_wake_futex(&wake_q, this);
Darren Hart52400ba2009-04-03 13:40:49 -07002216 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 }
Darren Hart52400ba2009-04-03 13:40:49 -07002218
Darren Hart84bc4af2009-08-13 17:36:53 -07002219 /* Ensure we requeue to the expected futex for requeue_pi. */
2220 if (requeue_pi && !match_futex(this->requeue_pi_key, &key2)) {
2221 ret = -EINVAL;
2222 break;
2223 }
2224
Darren Hart52400ba2009-04-03 13:40:49 -07002225 /*
2226 * Requeue nr_requeue waiters and possibly one more in the case
2227 * of requeue_pi if we couldn't acquire the lock atomically.
2228 */
2229 if (requeue_pi) {
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00002230 /*
2231 * Prepare the waiter to take the rt_mutex. Take a
2232 * refcount on the pi_state and store the pointer in
2233 * the futex_q object of the waiter.
2234 */
Ben Hutchings921a7e32021-03-01 18:31:22 +01002235 get_pi_state(pi_state);
Darren Hart52400ba2009-04-03 13:40:49 -07002236 this->pi_state = pi_state;
2237 ret = rt_mutex_start_proxy_lock(&pi_state->pi_mutex,
2238 this->rt_waiter,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00002239 this->task);
Darren Hart52400ba2009-04-03 13:40:49 -07002240 if (ret == 1) {
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00002241 /*
2242 * We got the lock. We do neither drop the
2243 * refcount on pi_state nor clear
2244 * this->pi_state because the waiter needs the
2245 * pi_state for cleaning up the user space
2246 * value. It will drop the refcount after
2247 * doing so.
2248 */
Darren Hartbeda2c72009-08-09 15:34:39 -07002249 requeue_pi_wake_futex(this, &key2, hb2);
Darren Hart89061d32009-10-15 15:30:48 -07002250 drop_count++;
Darren Hart52400ba2009-04-03 13:40:49 -07002251 continue;
2252 } else if (ret) {
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00002253 /*
2254 * rt_mutex_start_proxy_lock() detected a
2255 * potential deadlock when we tried to queue
2256 * that waiter. Drop the pi_state reference
2257 * which we took above and remove the pointer
2258 * to the state from the waiters futex_q
2259 * object.
2260 */
Darren Hart52400ba2009-04-03 13:40:49 -07002261 this->pi_state = NULL;
Thomas Gleixner29e9ee52015-12-19 20:07:39 +00002262 put_pi_state(pi_state);
Thomas Gleixner885c2cb2015-12-19 20:07:41 +00002263 /*
2264 * We stop queueing more waiters and let user
2265 * space deal with the mess.
2266 */
2267 break;
Darren Hart52400ba2009-04-03 13:40:49 -07002268 }
2269 }
2270 requeue_futex(this, hb1, hb2, &key2);
2271 drop_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 }
2273
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00002274 /*
2275 * We took an extra initial reference to the pi_state either
2276 * in futex_proxy_trylock_atomic() or in lookup_pi_state(). We
2277 * need to drop it here again.
2278 */
Thomas Gleixner29e9ee52015-12-19 20:07:39 +00002279 put_pi_state(pi_state);
Thomas Gleixner885c2cb2015-12-19 20:07:41 +00002280
2281out_unlock:
Darren Hart5eb3dc62009-03-12 00:55:52 -07002282 double_unlock_hb(hb1, hb2);
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07002283 wake_up_q(&wake_q);
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07002284 hb_waiters_dec(hb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
Darren Hartcd84a422009-04-02 14:19:38 -07002286 /*
2287 * drop_futex_key_refs() must be called outside the spinlocks. During
2288 * the requeue we moved futex_q's from the hash bucket at key1 to the
2289 * one at key2 and updated their key pointer. We no longer need to
2290 * hold the references to key1.
2291 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 while (--drop_count >= 0)
Rusty Russell9adef582007-05-08 00:26:42 -07002293 drop_futex_key_refs(&key1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
Darren Hart42d35d42008-12-29 15:49:53 -08002295out_put_keys:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002296 put_futex_key(&key2);
Darren Hart42d35d42008-12-29 15:49:53 -08002297out_put_key1:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002298 put_futex_key(&key1);
Darren Hart42d35d42008-12-29 15:49:53 -08002299out:
Darren Hart52400ba2009-04-03 13:40:49 -07002300 return ret ? ret : task_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301}
2302
2303/* The key must be already stored in q->key. */
Eric Sesterhenn82af7ac2008-01-25 10:40:46 +01002304static inline struct futex_hash_bucket *queue_lock(struct futex_q *q)
Namhyung Kim15e408c2010-09-14 21:43:48 +09002305 __acquires(&hb->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306{
Ingo Molnare2970f22006-06-27 02:54:47 -07002307 struct futex_hash_bucket *hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Ingo Molnare2970f22006-06-27 02:54:47 -07002309 hb = hash_futex(&q->key);
Linus Torvalds11d46162014-03-20 22:11:17 -07002310
2311 /*
2312 * Increment the counter before taking the lock so that
2313 * a potential waker won't miss a to-be-slept task that is
2314 * waiting for the spinlock. This is safe as all queue_lock()
2315 * users end up calling queue_me(). Similarly, for housekeeping,
2316 * decrement the counter at queue_unlock() when some error has
2317 * occurred and we don't end up adding the task to the list.
2318 */
2319 hb_waiters_inc(hb);
2320
Ingo Molnare2970f22006-06-27 02:54:47 -07002321 q->lock_ptr = &hb->lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Davidlohr Bueso8ad7b372016-02-09 11:15:13 -08002323 spin_lock(&hb->lock); /* implies smp_mb(); (A) */
Ingo Molnare2970f22006-06-27 02:54:47 -07002324 return hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325}
2326
Darren Hartd40d65c2009-09-21 22:30:15 -07002327static inline void
Jason Low0d00c7b2014-01-12 15:31:22 -08002328queue_unlock(struct futex_hash_bucket *hb)
Namhyung Kim15e408c2010-09-14 21:43:48 +09002329 __releases(&hb->lock)
Darren Hartd40d65c2009-09-21 22:30:15 -07002330{
2331 spin_unlock(&hb->lock);
Linus Torvalds11d46162014-03-20 22:11:17 -07002332 hb_waiters_dec(hb);
Darren Hartd40d65c2009-09-21 22:30:15 -07002333}
2334
2335/**
2336 * queue_me() - Enqueue the futex_q on the futex_hash_bucket
2337 * @q: The futex_q to enqueue
2338 * @hb: The destination hash bucket
2339 *
2340 * The hb->lock must be held by the caller, and is released here. A call to
2341 * queue_me() is typically paired with exactly one call to unqueue_me(). The
2342 * exceptions involve the PI related operations, which may use unqueue_me_pi()
2343 * or nothing if the unqueue is done as part of the wake process and the unqueue
2344 * state is implicit in the state of woken task (see futex_wait_requeue_pi() for
2345 * an example).
2346 */
Eric Sesterhenn82af7ac2008-01-25 10:40:46 +01002347static inline void queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
Namhyung Kim15e408c2010-09-14 21:43:48 +09002348 __releases(&hb->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349{
Pierre Peifferec92d082007-05-09 02:35:00 -07002350 int prio;
2351
2352 /*
2353 * The priority used to register this element is
2354 * - either the real thread-priority for the real-time threads
2355 * (i.e. threads with a priority lower than MAX_RT_PRIO)
2356 * - or MAX_RT_PRIO for non-RT threads.
2357 * Thus, all RT-threads are woken first in priority order, and
2358 * the others are woken last, in FIFO order.
2359 */
2360 prio = min(current->normal_prio, MAX_RT_PRIO);
2361
2362 plist_node_init(&q->list, prio);
Pierre Peifferec92d082007-05-09 02:35:00 -07002363 plist_add(&q->list, &hb->chain);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002364 q->task = current;
Ingo Molnare2970f22006-06-27 02:54:47 -07002365 spin_unlock(&hb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366}
2367
Darren Hartd40d65c2009-09-21 22:30:15 -07002368/**
2369 * unqueue_me() - Remove the futex_q from its futex_hash_bucket
2370 * @q: The futex_q to unqueue
2371 *
2372 * The q->lock_ptr must not be held by the caller. A call to unqueue_me() must
2373 * be paired with exactly one earlier call to queue_me().
2374 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002375 * Return:
2376 * 1 - if the futex_q was still queued (and we removed unqueued it);
Darren Hartd40d65c2009-09-21 22:30:15 -07002377 * 0 - if the futex_q was already removed by the waking thread
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379static int unqueue_me(struct futex_q *q)
2380{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 spinlock_t *lock_ptr;
Ingo Molnare2970f22006-06-27 02:54:47 -07002382 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
2384 /* In the common case we don't take the spinlock, which is nice. */
Darren Hart42d35d42008-12-29 15:49:53 -08002385retry:
Jianyu Zhan29b75eb2016-03-07 09:32:24 +08002386 /*
2387 * q->lock_ptr can change between this read and the following spin_lock.
2388 * Use READ_ONCE to forbid the compiler from reloading q->lock_ptr and
2389 * optimizing lock_ptr out of the logic below.
2390 */
2391 lock_ptr = READ_ONCE(q->lock_ptr);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07002392 if (lock_ptr != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 spin_lock(lock_ptr);
2394 /*
2395 * q->lock_ptr can change between reading it and
2396 * spin_lock(), causing us to take the wrong lock. This
2397 * corrects the race condition.
2398 *
2399 * Reasoning goes like this: if we have the wrong lock,
2400 * q->lock_ptr must have changed (maybe several times)
2401 * between reading it and the spin_lock(). It can
2402 * change again after the spin_lock() but only if it was
2403 * already changed before the spin_lock(). It cannot,
2404 * however, change back to the original value. Therefore
2405 * we can detect whether we acquired the correct lock.
2406 */
2407 if (unlikely(lock_ptr != q->lock_ptr)) {
2408 spin_unlock(lock_ptr);
2409 goto retry;
2410 }
Lai Jiangshan2e129782010-12-22 14:18:50 +08002411 __unqueue_futex(q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002412
2413 BUG_ON(q->pi_state);
2414
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 spin_unlock(lock_ptr);
2416 ret = 1;
2417 }
2418
Rusty Russell9adef582007-05-08 00:26:42 -07002419 drop_futex_key_refs(&q->key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 return ret;
2421}
2422
Ingo Molnarc87e2832006-06-27 02:54:58 -07002423/*
2424 * PI futexes can not be requeued and must remove themself from the
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002425 * hash bucket. The hash bucket lock (i.e. lock_ptr) is held on entry
2426 * and dropped here.
Ingo Molnarc87e2832006-06-27 02:54:58 -07002427 */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002428static void unqueue_me_pi(struct futex_q *q)
Namhyung Kim15e408c2010-09-14 21:43:48 +09002429 __releases(q->lock_ptr)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002430{
Lai Jiangshan2e129782010-12-22 14:18:50 +08002431 __unqueue_futex(q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002432
2433 BUG_ON(!q->pi_state);
Thomas Gleixner29e9ee52015-12-19 20:07:39 +00002434 put_pi_state(q->pi_state);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002435 q->pi_state = NULL;
2436
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002437 spin_unlock(q->lock_ptr);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002438}
2439
Thomas Gleixner48ab8e82021-02-03 13:45:38 +00002440static int __fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
2441 struct task_struct *argowner)
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002442{
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002443 struct futex_pi_state *pi_state = q->pi_state;
Peter Zijlstra781691c2021-02-03 13:45:33 +00002444 struct task_struct *oldowner, *newowner;
Thomas Gleixner48ab8e82021-02-03 13:45:38 +00002445 u32 uval, curval, newval, newtid;
2446 int err = 0;
Peter Zijlstra781691c2021-02-03 13:45:33 +00002447
2448 oldowner = pi_state->owner;
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002449
2450 /*
Peter Zijlstra781691c2021-02-03 13:45:33 +00002451 * We are here because either:
2452 *
2453 * - we stole the lock and pi_state->owner needs updating to reflect
2454 * that (@argowner == current),
2455 *
2456 * or:
2457 *
2458 * - someone stole our lock and we need to fix things to point to the
2459 * new owner (@argowner == NULL).
2460 *
2461 * Either way, we have to replace the TID in the user space variable.
Lai Jiangshan81612392011-01-14 17:09:41 +08002462 * This must be atomic as we have to preserve the owner died bit here.
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002463 *
Darren Hartb2d09942009-03-12 00:55:37 -07002464 * Note: We write the user space value _before_ changing the pi_state
2465 * because we can fault here. Imagine swapped out pages or a fork
2466 * that marked all the anonymous memory readonly for cow.
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002467 *
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00002468 * Modifying pi_state _before_ the user space value would leave the
2469 * pi_state in an inconsistent state when we fault here, because we
2470 * need to drop the locks to handle the fault. This might be observed
2471 * in the PID check in lookup_pi_state.
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002472 */
2473retry:
Peter Zijlstra781691c2021-02-03 13:45:33 +00002474 if (!argowner) {
2475 if (oldowner != current) {
2476 /*
2477 * We raced against a concurrent self; things are
2478 * already fixed up. Nothing to do.
2479 */
2480 return 0;
2481 }
2482
2483 if (__rt_mutex_futex_trylock(&pi_state->pi_mutex)) {
Thomas Gleixnerad8fdba2021-02-11 09:26:58 +00002484 /* We got the lock. pi_state is correct. Tell caller. */
Thomas Gleixner48ab8e82021-02-03 13:45:38 +00002485 return 1;
Peter Zijlstra781691c2021-02-03 13:45:33 +00002486 }
2487
2488 /*
2489 * Since we just failed the trylock; there must be an owner.
2490 */
2491 newowner = rt_mutex_owner(&pi_state->pi_mutex);
2492 BUG_ON(!newowner);
2493 } else {
2494 WARN_ON_ONCE(argowner != current);
2495 if (oldowner == current) {
2496 /*
2497 * We raced against a concurrent self; things are
2498 * already fixed up. Nothing to do.
2499 */
Thomas Gleixner48ab8e82021-02-03 13:45:38 +00002500 return 1;
Peter Zijlstra781691c2021-02-03 13:45:33 +00002501 }
2502 newowner = argowner;
2503 }
2504
2505 newtid = task_pid_vnr(newowner) | FUTEX_WAITERS;
Peter Zijlstra0d351802018-01-22 11:39:47 +01002506 /* Owner died? */
2507 if (!pi_state->owner)
2508 newtid |= FUTEX_OWNER_DIED;
Peter Zijlstra781691c2021-02-03 13:45:33 +00002509
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002510 if (get_futex_value_locked(&uval, uaddr))
2511 goto handle_fault;
2512
Ben Hutchings312d9d62021-03-01 18:31:30 +01002513 for (;;) {
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002514 newval = (uval & FUTEX_OWNER_DIED) | newtid;
2515
Michel Lespinasse37a9d912011-03-10 18:48:51 -08002516 if (cmpxchg_futex_value_locked(&curval, uaddr, uval, newval))
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002517 goto handle_fault;
2518 if (curval == uval)
2519 break;
2520 uval = curval;
2521 }
2522
2523 /*
2524 * We fixed up user space. Now we need to fix the pi_state
2525 * itself.
2526 */
Thomas Gleixner76bc0ec2021-02-03 13:45:35 +00002527 pi_state_update_owner(pi_state, newowner);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002528
Thomas Gleixnerad8fdba2021-02-11 09:26:58 +00002529 return argowner == current;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002530
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002531 /*
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00002532 * To handle the page fault we need to drop the locks here. That gives
2533 * the other task (either the highest priority waiter itself or the
2534 * task which stole the rtmutex) the chance to try the fixup of the
2535 * pi_state. So once we are back from handling the fault we need to
2536 * check the pi_state after reacquiring the locks and before trying to
2537 * do another fixup. When the fixup has been done already we simply
2538 * return.
2539 *
2540 * Note: we hold both hb->lock and pi_mutex->wait_lock. We can safely
2541 * drop hb->lock since the caller owns the hb -> futex_q relation.
2542 * Dropping the pi_mutex->wait_lock requires the state revalidate.
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002543 */
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002544handle_fault:
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00002545 raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002546 spin_unlock(q->lock_ptr);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002547
Thomas Gleixner48ab8e82021-02-03 13:45:38 +00002548 err = fault_in_user_writeable(uaddr);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002549
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002550 spin_lock(q->lock_ptr);
Peter Zijlstradc3f2ff2021-02-11 09:26:59 +00002551 raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002552
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002553 /*
2554 * Check if someone else fixed it for us:
2555 */
2556 if (pi_state->owner != oldowner)
Thomas Gleixner48ab8e82021-02-03 13:45:38 +00002557 return argowner == current;
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002558
Thomas Gleixner48ab8e82021-02-03 13:45:38 +00002559 /* Retry if err was -EAGAIN or the fault in succeeded */
2560 if (!err)
2561 goto retry;
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002562
Thomas Gleixnerb960d9a2021-02-03 13:45:39 +00002563 /*
2564 * fault_in_user_writeable() failed so user state is immutable. At
2565 * best we can make the kernel state consistent but user state will
2566 * be most likely hosed and any subsequent unlock operation will be
2567 * rejected due to PI futex rule [10].
2568 *
2569 * Ensure that the rtmutex owner is also the pi_state owner despite
2570 * the user space value claiming something different. There is no
2571 * point in unlocking the rtmutex if current is the owner as it
2572 * would need to wait until the next waiter has taken the rtmutex
2573 * to guarantee consistent state. Keep it simple. Userspace asked
2574 * for this wreckaged state.
2575 *
2576 * The rtmutex has an owner - either current or some other
2577 * task. See the EAGAIN loop above.
2578 */
2579 pi_state_update_owner(pi_state, rt_mutex_owner(&pi_state->pi_mutex));
2580
Thomas Gleixner48ab8e82021-02-03 13:45:38 +00002581 return err;
2582}
2583
2584static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
2585 struct task_struct *argowner)
2586{
2587 struct futex_pi_state *pi_state = q->pi_state;
2588 int ret;
2589
2590 lockdep_assert_held(q->lock_ptr);
2591
2592 raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
2593 ret = __fixup_pi_state_owner(uaddr, q, argowner);
2594 raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
2595 return ret;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002596}
2597
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002598static long futex_wait_restart(struct restart_block *restart);
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07002599
Darren Hartca5f9522009-04-03 13:39:33 -07002600/**
Darren Hartdd973992009-04-03 13:40:02 -07002601 * fixup_owner() - Post lock pi_state and corner case management
2602 * @uaddr: user address of the futex
Darren Hartdd973992009-04-03 13:40:02 -07002603 * @q: futex_q (contains pi_state and access to the rt_mutex)
2604 * @locked: if the attempt to take the rt_mutex succeeded (1) or not (0)
2605 *
2606 * After attempting to lock an rt_mutex, this function is called to cleanup
2607 * the pi_state owner as well as handle race conditions that may allow us to
2608 * acquire the lock. Must be called with the hb lock held.
2609 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002610 * Return:
2611 * 1 - success, lock taken;
2612 * 0 - success, lock not taken;
Darren Hartdd973992009-04-03 13:40:02 -07002613 * <0 - on error (-EFAULT)
2614 */
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002615static int fixup_owner(u32 __user *uaddr, struct futex_q *q, int locked)
Darren Hartdd973992009-04-03 13:40:02 -07002616{
Darren Hartdd973992009-04-03 13:40:02 -07002617 if (locked) {
2618 /*
2619 * Got the lock. We might not be the anticipated owner if we
2620 * did a lock-steal - fix up the PI-state in that case:
Peter Zijlstra781691c2021-02-03 13:45:33 +00002621 *
2622 * Speculative pi_state->owner read (we don't hold wait_lock);
2623 * since we own the lock pi_state->owner == current is the
2624 * stable state, anything else needs more attention.
Darren Hartdd973992009-04-03 13:40:02 -07002625 */
2626 if (q->pi_state->owner != current)
Thomas Gleixnerad8fdba2021-02-11 09:26:58 +00002627 return fixup_pi_state_owner(uaddr, q, current);
2628 return 1;
Darren Hartdd973992009-04-03 13:40:02 -07002629 }
2630
2631 /*
Peter Zijlstra781691c2021-02-03 13:45:33 +00002632 * If we didn't get the lock; check if anybody stole it from us. In
2633 * that case, we need to fix up the uval to point to them instead of
2634 * us, otherwise bad things happen. [10]
2635 *
2636 * Another speculative read; pi_state->owner == current is unstable
2637 * but needs our attention.
2638 */
Thomas Gleixnerad8fdba2021-02-11 09:26:58 +00002639 if (q->pi_state->owner == current)
2640 return fixup_pi_state_owner(uaddr, q, NULL);
Peter Zijlstra781691c2021-02-03 13:45:33 +00002641
2642 /*
Darren Hartdd973992009-04-03 13:40:02 -07002643 * Paranoia check. If we did not take the lock, then we should not be
Thomas Gleixner083895e2021-02-03 13:45:34 +00002644 * the owner of the rt_mutex. Warn and establish consistent state.
Darren Hartdd973992009-04-03 13:40:02 -07002645 */
Thomas Gleixner083895e2021-02-03 13:45:34 +00002646 if (WARN_ON_ONCE(rt_mutex_owner(&q->pi_state->pi_mutex) == current))
2647 return fixup_pi_state_owner(uaddr, q, current);
Darren Hartdd973992009-04-03 13:40:02 -07002648
Thomas Gleixnerad8fdba2021-02-11 09:26:58 +00002649 return 0;
Darren Hartdd973992009-04-03 13:40:02 -07002650}
2651
2652/**
Darren Hartca5f9522009-04-03 13:39:33 -07002653 * futex_wait_queue_me() - queue_me() and wait for wakeup, timeout, or signal
2654 * @hb: the futex hash bucket, must be locked by the caller
2655 * @q: the futex_q to queue up on
2656 * @timeout: the prepared hrtimer_sleeper, or null for no timeout
Darren Hartca5f9522009-04-03 13:39:33 -07002657 */
2658static void futex_wait_queue_me(struct futex_hash_bucket *hb, struct futex_q *q,
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02002659 struct hrtimer_sleeper *timeout)
Darren Hartca5f9522009-04-03 13:39:33 -07002660{
Darren Hart9beba3c2009-09-24 11:54:47 -07002661 /*
2662 * The task state is guaranteed to be set before another task can
Peter Zijlstrab92b8b32015-05-12 10:51:55 +02002663 * wake it. set_current_state() is implemented using smp_store_mb() and
Darren Hart9beba3c2009-09-24 11:54:47 -07002664 * queue_me() calls spin_unlock() upon completion, both serializing
2665 * access to the hash list and forcing another memory barrier.
2666 */
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02002667 set_current_state(TASK_INTERRUPTIBLE);
Darren Hart0729e192009-09-21 22:30:38 -07002668 queue_me(q, hb);
Darren Hartca5f9522009-04-03 13:39:33 -07002669
2670 /* Arm the timer */
Thomas Gleixner2e4b0d32015-04-14 21:09:13 +00002671 if (timeout)
Darren Hartca5f9522009-04-03 13:39:33 -07002672 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
Darren Hartca5f9522009-04-03 13:39:33 -07002673
2674 /*
Darren Hart0729e192009-09-21 22:30:38 -07002675 * If we have been removed from the hash list, then another task
2676 * has tried to wake us, and we can skip the call to schedule().
Darren Hartca5f9522009-04-03 13:39:33 -07002677 */
2678 if (likely(!plist_node_empty(&q->list))) {
2679 /*
2680 * If the timer has already expired, current will already be
2681 * flagged for rescheduling. Only call schedule if there
2682 * is no timeout, or if it has yet to expire.
2683 */
2684 if (!timeout || timeout->task)
Colin Cross88c80042013-05-01 18:35:05 -07002685 freezable_schedule();
Darren Hartca5f9522009-04-03 13:39:33 -07002686 }
2687 __set_current_state(TASK_RUNNING);
2688}
2689
Darren Hartf8010732009-04-03 13:40:40 -07002690/**
2691 * futex_wait_setup() - Prepare to wait on a futex
2692 * @uaddr: the futex userspace address
2693 * @val: the expected value
Darren Hartb41277d2010-11-08 13:10:09 -08002694 * @flags: futex flags (FLAGS_SHARED, etc.)
Darren Hartf8010732009-04-03 13:40:40 -07002695 * @q: the associated futex_q
2696 * @hb: storage for hash_bucket pointer to be returned to caller
2697 *
2698 * Setup the futex_q and locate the hash_bucket. Get the futex value and
2699 * compare it with the expected value. Handle atomic faults internally.
2700 * Return with the hb lock held and a q.key reference on success, and unlocked
2701 * with no q.key reference on failure.
2702 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002703 * Return:
2704 * 0 - uaddr contains val and hb has been locked;
Bart Van Asscheca4a04c2011-07-17 09:01:00 +02002705 * <1 - -EFAULT or -EWOULDBLOCK (uaddr does not contain val) and hb is unlocked
Darren Hartf8010732009-04-03 13:40:40 -07002706 */
Darren Hartb41277d2010-11-08 13:10:09 -08002707static int futex_wait_setup(u32 __user *uaddr, u32 val, unsigned int flags,
Darren Hartf8010732009-04-03 13:40:40 -07002708 struct futex_q *q, struct futex_hash_bucket **hb)
2709{
2710 u32 uval;
2711 int ret;
2712
2713 /*
2714 * Access the page AFTER the hash-bucket is locked.
2715 * Order is important:
2716 *
2717 * Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val);
2718 * Userspace waker: if (cond(var)) { var = new; futex_wake(&var); }
2719 *
2720 * The basic logical guarantee of a futex is that it blocks ONLY
2721 * if cond(var) is known to be true at the time of blocking, for
Michel Lespinasse8fe8f542011-03-06 18:07:50 -08002722 * any cond. If we locked the hash-bucket after testing *uaddr, that
2723 * would open a race condition where we could block indefinitely with
Darren Hartf8010732009-04-03 13:40:40 -07002724 * cond(var) false, which would violate the guarantee.
2725 *
Michel Lespinasse8fe8f542011-03-06 18:07:50 -08002726 * On the other hand, we insert q and release the hash-bucket only
2727 * after testing *uaddr. This guarantees that futex_wait() will NOT
2728 * absorb a wakeup if *uaddr does not match the desired values
2729 * while the syscall executes.
Darren Hartf8010732009-04-03 13:40:40 -07002730 */
2731retry:
Shawn Bohrer9ea71502011-06-30 11:21:32 -05002732 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q->key, VERIFY_READ);
Darren Hartf8010732009-04-03 13:40:40 -07002733 if (unlikely(ret != 0))
Darren Harta5a2a0c2009-04-10 09:50:05 -07002734 return ret;
Darren Hartf8010732009-04-03 13:40:40 -07002735
2736retry_private:
2737 *hb = queue_lock(q);
2738
2739 ret = get_futex_value_locked(&uval, uaddr);
2740
2741 if (ret) {
Jason Low0d00c7b2014-01-12 15:31:22 -08002742 queue_unlock(*hb);
Darren Hartf8010732009-04-03 13:40:40 -07002743
2744 ret = get_user(uval, uaddr);
2745 if (ret)
2746 goto out;
2747
Darren Hartb41277d2010-11-08 13:10:09 -08002748 if (!(flags & FLAGS_SHARED))
Darren Hartf8010732009-04-03 13:40:40 -07002749 goto retry_private;
2750
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002751 put_futex_key(&q->key);
Darren Hartf8010732009-04-03 13:40:40 -07002752 goto retry;
2753 }
2754
2755 if (uval != val) {
Jason Low0d00c7b2014-01-12 15:31:22 -08002756 queue_unlock(*hb);
Darren Hartf8010732009-04-03 13:40:40 -07002757 ret = -EWOULDBLOCK;
2758 }
2759
2760out:
2761 if (ret)
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002762 put_futex_key(&q->key);
Darren Hartf8010732009-04-03 13:40:40 -07002763 return ret;
2764}
2765
Darren Hartb41277d2010-11-08 13:10:09 -08002766static int futex_wait(u32 __user *uaddr, unsigned int flags, u32 val,
2767 ktime_t *abs_time, u32 bitset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768{
Darren Hartca5f9522009-04-03 13:39:33 -07002769 struct hrtimer_sleeper timeout, *to = NULL;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002770 struct restart_block *restart;
Ingo Molnare2970f22006-06-27 02:54:47 -07002771 struct futex_hash_bucket *hb;
Darren Hart5bdb05f2010-11-08 13:40:28 -08002772 struct futex_q q = futex_q_init;
Ingo Molnare2970f22006-06-27 02:54:47 -07002773 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
Thomas Gleixnercd689982008-02-01 17:45:14 +01002775 if (!bitset)
2776 return -EINVAL;
Thomas Gleixnercd689982008-02-01 17:45:14 +01002777 q.bitset = bitset;
Darren Hartca5f9522009-04-03 13:39:33 -07002778
2779 if (abs_time) {
2780 to = &timeout;
2781
Darren Hartb41277d2010-11-08 13:10:09 -08002782 hrtimer_init_on_stack(&to->timer, (flags & FLAGS_CLOCKRT) ?
2783 CLOCK_REALTIME : CLOCK_MONOTONIC,
2784 HRTIMER_MODE_ABS);
Darren Hartca5f9522009-04-03 13:39:33 -07002785 hrtimer_init_sleeper(to, current);
2786 hrtimer_set_expires_range_ns(&to->timer, *abs_time,
2787 current->timer_slack_ns);
2788 }
2789
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002790retry:
Darren Hart7ada8762010-10-17 08:35:04 -07002791 /*
2792 * Prepare to wait on uaddr. On success, holds hb lock and increments
2793 * q.key refs.
2794 */
Darren Hartb41277d2010-11-08 13:10:09 -08002795 ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
Darren Hartf8010732009-04-03 13:40:40 -07002796 if (ret)
Darren Hart42d35d42008-12-29 15:49:53 -08002797 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
Darren Hartca5f9522009-04-03 13:39:33 -07002799 /* queue_me and wait for wakeup, timeout, or a signal. */
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02002800 futex_wait_queue_me(hb, &q, to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
2802 /* If we were woken (and unqueued), we succeeded, whatever. */
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002803 ret = 0;
Darren Hart7ada8762010-10-17 08:35:04 -07002804 /* unqueue_me() drops q.key ref */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 if (!unqueue_me(&q))
Darren Hart7ada8762010-10-17 08:35:04 -07002806 goto out;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002807 ret = -ETIMEDOUT;
Darren Hartca5f9522009-04-03 13:39:33 -07002808 if (to && !to->task)
Darren Hart7ada8762010-10-17 08:35:04 -07002809 goto out;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002810
Ingo Molnare2970f22006-06-27 02:54:47 -07002811 /*
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002812 * We expect signal_pending(current), but we might be the
2813 * victim of a spurious wakeup as well.
Ingo Molnare2970f22006-06-27 02:54:47 -07002814 */
Darren Hart7ada8762010-10-17 08:35:04 -07002815 if (!signal_pending(current))
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002816 goto retry;
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002817
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002818 ret = -ERESTARTSYS;
Pierre Peifferc19384b2007-05-09 02:35:02 -07002819 if (!abs_time)
Darren Hart7ada8762010-10-17 08:35:04 -07002820 goto out;
Steven Rostedtce6bd422007-12-05 15:46:09 +01002821
Andy Lutomirskif56141e2015-02-12 15:01:14 -08002822 restart = &current->restart_block;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002823 restart->fn = futex_wait_restart;
Namhyung Kima3c74c52010-09-14 21:43:47 +09002824 restart->futex.uaddr = uaddr;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002825 restart->futex.val = val;
2826 restart->futex.time = abs_time->tv64;
2827 restart->futex.bitset = bitset;
Darren Hart0cd9c642011-04-14 15:41:57 -07002828 restart->futex.flags = flags | FLAGS_HAS_TIMEOUT;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002829
2830 ret = -ERESTART_RESTARTBLOCK;
2831
Darren Hart42d35d42008-12-29 15:49:53 -08002832out:
Darren Hartca5f9522009-04-03 13:39:33 -07002833 if (to) {
2834 hrtimer_cancel(&to->timer);
2835 destroy_hrtimer_on_stack(&to->timer);
2836 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07002837 return ret;
2838}
2839
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002840
2841static long futex_wait_restart(struct restart_block *restart)
2842{
Namhyung Kima3c74c52010-09-14 21:43:47 +09002843 u32 __user *uaddr = restart->futex.uaddr;
Darren Harta72188d2009-04-03 13:40:22 -07002844 ktime_t t, *tp = NULL;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002845
Darren Harta72188d2009-04-03 13:40:22 -07002846 if (restart->futex.flags & FLAGS_HAS_TIMEOUT) {
2847 t.tv64 = restart->futex.time;
2848 tp = &t;
2849 }
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002850 restart->fn = do_no_restart_syscall;
Darren Hartb41277d2010-11-08 13:10:09 -08002851
2852 return (long)futex_wait(uaddr, restart->futex.flags,
2853 restart->futex.val, tp, restart->futex.bitset);
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002854}
2855
2856
Ingo Molnarc87e2832006-06-27 02:54:58 -07002857/*
2858 * Userspace tried a 0 -> TID atomic transition of the futex value
2859 * and failed. The kernel side here does the whole locking operation:
Davidlohr Bueso767f5092015-06-29 23:26:01 -07002860 * if there are waiters then it will block as a consequence of relying
2861 * on rt-mutexes, it does PI, etc. (Due to races the kernel might see
2862 * a 0 value of the futex too.).
2863 *
2864 * Also serves as futex trylock_pi()'ing, and due semantics.
Ingo Molnarc87e2832006-06-27 02:54:58 -07002865 */
Michael Kerrisk996636d2015-01-16 20:28:06 +01002866static int futex_lock_pi(u32 __user *uaddr, unsigned int flags,
Darren Hartb41277d2010-11-08 13:10:09 -08002867 ktime_t *time, int trylock)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002868{
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07002869 struct hrtimer_sleeper timeout, *to = NULL;
Thomas Gleixnercf16e422021-02-01 10:01:43 +00002870 struct task_struct *exiting = NULL;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002871 struct futex_hash_bucket *hb;
Darren Hart5bdb05f2010-11-08 13:40:28 -08002872 struct futex_q q = futex_q_init;
Darren Hartdd973992009-04-03 13:40:02 -07002873 int res, ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002874
2875 if (refill_pi_state_cache())
2876 return -ENOMEM;
2877
Pierre Peifferc19384b2007-05-09 02:35:02 -07002878 if (time) {
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07002879 to = &timeout;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07002880 hrtimer_init_on_stack(&to->timer, CLOCK_REALTIME,
2881 HRTIMER_MODE_ABS);
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07002882 hrtimer_init_sleeper(to, current);
Arjan van de Vencc584b22008-09-01 15:02:30 -07002883 hrtimer_set_expires(&to->timer, *time);
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07002884 }
2885
Darren Hart42d35d42008-12-29 15:49:53 -08002886retry:
Shawn Bohrer9ea71502011-06-30 11:21:32 -05002887 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q.key, VERIFY_WRITE);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002888 if (unlikely(ret != 0))
Darren Hart42d35d42008-12-29 15:49:53 -08002889 goto out;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002890
Darren Harte4dc5b72009-03-12 00:56:13 -07002891retry_private:
Eric Sesterhenn82af7ac2008-01-25 10:40:46 +01002892 hb = queue_lock(&q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002893
Thomas Gleixnercf16e422021-02-01 10:01:43 +00002894 ret = futex_lock_pi_atomic(uaddr, hb, &q.key, &q.pi_state, current,
2895 &exiting, 0);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002896 if (unlikely(ret)) {
Davidlohr Bueso767f5092015-06-29 23:26:01 -07002897 /*
2898 * Atomic work succeeded and we got the lock,
2899 * or failed. Either way, we do _not_ block.
2900 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002901 switch (ret) {
Darren Hart1a520842009-04-03 13:39:52 -07002902 case 1:
2903 /* We got the lock. */
2904 ret = 0;
2905 goto out_unlock_put_key;
2906 case -EFAULT:
2907 goto uaddr_faulted;
Thomas Gleixnerc27f3922021-02-01 10:01:42 +00002908 case -EBUSY:
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002909 case -EAGAIN:
2910 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00002911 * Two reasons for this:
Thomas Gleixnerc27f3922021-02-01 10:01:42 +00002912 * - EBUSY: Task is exiting and we just wait for the
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00002913 * exit to complete.
Thomas Gleixnerc27f3922021-02-01 10:01:42 +00002914 * - EAGAIN: The user space value changed.
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002915 */
Jason Low0d00c7b2014-01-12 15:31:22 -08002916 queue_unlock(hb);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002917 put_futex_key(&q.key);
Thomas Gleixnercf16e422021-02-01 10:01:43 +00002918 /*
2919 * Handle the case where the owner is in the middle of
2920 * exiting. Wait for the exit to complete otherwise
2921 * this task might loop forever, aka. live lock.
2922 */
2923 wait_for_owner_exiting(ret, exiting);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002924 cond_resched();
2925 goto retry;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002926 default:
Darren Hart42d35d42008-12-29 15:49:53 -08002927 goto out_unlock_put_key;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002928 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07002929 }
2930
2931 /*
2932 * Only actually queue now that the atomic ops are done:
2933 */
Eric Sesterhenn82af7ac2008-01-25 10:40:46 +01002934 queue_me(&q, hb);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002935
Ingo Molnarc87e2832006-06-27 02:54:58 -07002936 WARN_ON(!q.pi_state);
2937 /*
2938 * Block on the PI mutex:
2939 */
Thomas Gleixnerc051b212014-05-22 03:25:50 +00002940 if (!trylock) {
2941 ret = rt_mutex_timed_futex_lock(&q.pi_state->pi_mutex, to);
2942 } else {
Peter Zijlstra2c60d4a2021-02-03 13:45:30 +00002943 ret = rt_mutex_futex_trylock(&q.pi_state->pi_mutex);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002944 /* Fixup the trylock return value: */
2945 ret = ret ? 0 : -EWOULDBLOCK;
2946 }
2947
Vernon Mauerya99e4e42006-07-01 04:35:42 -07002948 spin_lock(q.lock_ptr);
Darren Hartdd973992009-04-03 13:40:02 -07002949 /*
2950 * Fixup the pi_state owner and possibly acquire the lock if we
2951 * haven't already.
2952 */
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002953 res = fixup_owner(uaddr, &q, !ret);
Darren Hartdd973992009-04-03 13:40:02 -07002954 /*
2955 * If fixup_owner() returned an error, proprogate that. If it acquired
2956 * the lock, clear our -ETIMEDOUT or -EINTR.
2957 */
2958 if (res)
2959 ret = (res < 0) ? res : 0;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002960
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002961 /* Unqueue and drop the lock */
2962 unqueue_me_pi(&q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002963
Mikael Pettersson5ecb01c2010-01-23 22:36:29 +01002964 goto out_put_key;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002965
Darren Hart42d35d42008-12-29 15:49:53 -08002966out_unlock_put_key:
Jason Low0d00c7b2014-01-12 15:31:22 -08002967 queue_unlock(hb);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002968
Darren Hart42d35d42008-12-29 15:49:53 -08002969out_put_key:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002970 put_futex_key(&q.key);
Darren Hart42d35d42008-12-29 15:49:53 -08002971out:
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07002972 if (to)
2973 destroy_hrtimer_on_stack(&to->timer);
Darren Hartdd973992009-04-03 13:40:02 -07002974 return ret != -EINTR ? ret : -ERESTARTNOINTR;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002975
Darren Hart42d35d42008-12-29 15:49:53 -08002976uaddr_faulted:
Jason Low0d00c7b2014-01-12 15:31:22 -08002977 queue_unlock(hb);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002978
Thomas Gleixnerd0725992009-06-11 23:15:43 +02002979 ret = fault_in_user_writeable(uaddr);
Darren Harte4dc5b72009-03-12 00:56:13 -07002980 if (ret)
2981 goto out_put_key;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002982
Darren Hartb41277d2010-11-08 13:10:09 -08002983 if (!(flags & FLAGS_SHARED))
Darren Harte4dc5b72009-03-12 00:56:13 -07002984 goto retry_private;
2985
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002986 put_futex_key(&q.key);
Darren Harte4dc5b72009-03-12 00:56:13 -07002987 goto retry;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002988}
2989
2990/*
Ingo Molnarc87e2832006-06-27 02:54:58 -07002991 * Userspace attempted a TID -> 0 atomic transition, and failed.
2992 * This is the in-kernel slowpath: we look up the PI state (if any),
2993 * and do the rt-mutex unlock.
2994 */
Darren Hartb41277d2010-11-08 13:10:09 -08002995static int futex_unlock_pi(u32 __user *uaddr, unsigned int flags)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002996{
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002997 u32 uninitialized_var(curval), uval, vpid = task_pid_vnr(current);
Peter Zijlstra38d47c12008-09-26 19:32:20 +02002998 union futex_key key = FUTEX_KEY_INIT;
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002999 struct futex_hash_bucket *hb;
Ben Hutchingsbfefc9e2021-03-01 18:30:39 +01003000 struct futex_q *top_waiter;
Darren Harte4dc5b72009-03-12 00:56:13 -07003001 int ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -07003002
3003retry:
3004 if (get_user(uval, uaddr))
3005 return -EFAULT;
3006 /*
3007 * We release only a lock we actually own:
3008 */
Thomas Gleixnerc0c9ed12011-03-11 11:51:22 +01003009 if ((uval & FUTEX_TID_MASK) != vpid)
Ingo Molnarc87e2832006-06-27 02:54:58 -07003010 return -EPERM;
Ingo Molnarc87e2832006-06-27 02:54:58 -07003011
Shawn Bohrer9ea71502011-06-30 11:21:32 -05003012 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, VERIFY_WRITE);
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00003013 if (ret)
3014 return ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -07003015
3016 hb = hash_futex(&key);
3017 spin_lock(&hb->lock);
3018
Ingo Molnarc87e2832006-06-27 02:54:58 -07003019 /*
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00003020 * Check waiters first. We do not trust user space values at
3021 * all and we at least want to know if user space fiddled
3022 * with the futex value instead of blindly unlocking.
Ingo Molnarc87e2832006-06-27 02:54:58 -07003023 */
Ben Hutchingsbfefc9e2021-03-01 18:30:39 +01003024 top_waiter = futex_top_waiter(hb, &key);
3025 if (top_waiter) {
Ben Hutchings312d9d62021-03-01 18:31:30 +01003026 struct futex_pi_state *pi_state = top_waiter->pi_state;
3027
3028 ret = -EINVAL;
3029 if (!pi_state)
3030 goto out_unlock;
3031
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02003032 /*
Ben Hutchings312d9d62021-03-01 18:31:30 +01003033 * If current does not own the pi_state then the futex is
3034 * inconsistent and user space fiddled with the futex value.
3035 */
3036 if (pi_state->owner != current)
3037 goto out_unlock;
3038
Ben Hutchings312d9d62021-03-01 18:31:30 +01003039 get_pi_state(pi_state);
Ben Hutchings25a678d2021-03-01 18:31:39 +01003040 /*
3041 * Since modifying the wait_list is done while holding both
3042 * hb->lock and wait_lock, holding either is sufficient to
3043 * observe it.
3044 *
3045 * By taking wait_lock while still holding hb->lock, we ensure
3046 * there is no point where we hold neither; and therefore
3047 * wake_futex_pi() must observe a state consistent with what we
3048 * observed.
3049 */
3050 raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
Ben Hutchings312d9d62021-03-01 18:31:30 +01003051 spin_unlock(&hb->lock);
3052
Ben Hutchings9787adc2021-03-01 18:31:48 +01003053 /* drops pi_state->pi_mutex.wait_lock */
Ben Hutchings312d9d62021-03-01 18:31:30 +01003054 ret = wake_futex_pi(uaddr, uval, pi_state);
3055
3056 put_pi_state(pi_state);
3057
3058 /*
3059 * Success, we're done! No tricky corner cases.
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02003060 */
3061 if (!ret)
3062 goto out_putkey;
Ingo Molnarc87e2832006-06-27 02:54:58 -07003063 /*
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00003064 * The atomic access to the futex value generated a
3065 * pagefault, so retry the user-access and the wakeup:
Ingo Molnarc87e2832006-06-27 02:54:58 -07003066 */
3067 if (ret == -EFAULT)
3068 goto pi_faulted;
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02003069 /*
Sebastian Andrzej Siewior89e9e662016-04-15 14:35:39 +02003070 * A unconditional UNLOCK_PI op raced against a waiter
3071 * setting the FUTEX_WAITERS bit. Try again.
3072 */
3073 if (ret == -EAGAIN) {
Sebastian Andrzej Siewior89e9e662016-04-15 14:35:39 +02003074 put_futex_key(&key);
3075 goto retry;
3076 }
3077 /*
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02003078 * wake_futex_pi has detected invalid state. Tell user
3079 * space.
3080 */
Ben Hutchings312d9d62021-03-01 18:31:30 +01003081 goto out_putkey;
Ingo Molnarc87e2832006-06-27 02:54:58 -07003082 }
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00003083
Ingo Molnarc87e2832006-06-27 02:54:58 -07003084 /*
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00003085 * We have no kernel internal state, i.e. no waiters in the
3086 * kernel. Waiters which are about to queue themselves are stuck
3087 * on hb->lock. So we can safely ignore them. We do neither
3088 * preserve the WAITERS bit not the OWNER_DIED one. We are the
3089 * owner.
Ingo Molnarc87e2832006-06-27 02:54:58 -07003090 */
Ben Hutchings312d9d62021-03-01 18:31:30 +01003091 if (cmpxchg_futex_value_locked(&curval, uaddr, uval, 0)) {
3092 spin_unlock(&hb->lock);
Thomas Gleixner13fbca42014-06-03 12:27:07 +00003093 goto pi_faulted;
Ben Hutchings312d9d62021-03-01 18:31:30 +01003094 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07003095
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00003096 /*
3097 * If uval has changed, let user space handle it.
3098 */
3099 ret = (curval == uval) ? 0 : -EAGAIN;
3100
Ingo Molnarc87e2832006-06-27 02:54:58 -07003101out_unlock:
3102 spin_unlock(&hb->lock);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02003103out_putkey:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01003104 put_futex_key(&key);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003105 return ret;
3106
3107pi_faulted:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01003108 put_futex_key(&key);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003109
Thomas Gleixnerd0725992009-06-11 23:15:43 +02003110 ret = fault_in_user_writeable(uaddr);
Darren Hartb5686362008-12-18 15:06:34 -08003111 if (!ret)
Ingo Molnarc87e2832006-06-27 02:54:58 -07003112 goto retry;
3113
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 return ret;
3115}
3116
Darren Hart52400ba2009-04-03 13:40:49 -07003117/**
3118 * handle_early_requeue_pi_wakeup() - Detect early wakeup on the initial futex
3119 * @hb: the hash_bucket futex_q was original enqueued on
3120 * @q: the futex_q woken while waiting to be requeued
3121 * @key2: the futex_key of the requeue target futex
3122 * @timeout: the timeout associated with the wait (NULL if none)
3123 *
3124 * Detect if the task was woken on the initial futex as opposed to the requeue
3125 * target futex. If so, determine if it was a timeout or a signal that caused
3126 * the wakeup and return the appropriate error code to the caller. Must be
3127 * called with the hb lock held.
3128 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08003129 * Return:
3130 * 0 = no early wakeup detected;
3131 * <0 = -ETIMEDOUT or -ERESTARTNOINTR
Darren Hart52400ba2009-04-03 13:40:49 -07003132 */
3133static inline
3134int handle_early_requeue_pi_wakeup(struct futex_hash_bucket *hb,
3135 struct futex_q *q, union futex_key *key2,
3136 struct hrtimer_sleeper *timeout)
3137{
3138 int ret = 0;
3139
3140 /*
3141 * With the hb lock held, we avoid races while we process the wakeup.
3142 * We only need to hold hb (and not hb2) to ensure atomicity as the
3143 * wakeup code can't change q.key from uaddr to uaddr2 if we hold hb.
3144 * It can't be requeued from uaddr2 to something else since we don't
3145 * support a PI aware source futex for requeue.
3146 */
3147 if (!match_futex(&q->key, key2)) {
3148 WARN_ON(q->lock_ptr && (&hb->lock != q->lock_ptr));
3149 /*
3150 * We were woken prior to requeue by a timeout or a signal.
3151 * Unqueue the futex_q and determine which it was.
3152 */
Lai Jiangshan2e129782010-12-22 14:18:50 +08003153 plist_del(&q->list, &hb->chain);
Linus Torvalds11d46162014-03-20 22:11:17 -07003154 hb_waiters_dec(hb);
Darren Hart52400ba2009-04-03 13:40:49 -07003155
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02003156 /* Handle spurious wakeups gracefully */
Thomas Gleixner11df6dd2009-10-28 20:26:48 +01003157 ret = -EWOULDBLOCK;
Darren Hart52400ba2009-04-03 13:40:49 -07003158 if (timeout && !timeout->task)
3159 ret = -ETIMEDOUT;
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02003160 else if (signal_pending(current))
Thomas Gleixner1c840c12009-05-20 09:22:40 +02003161 ret = -ERESTARTNOINTR;
Darren Hart52400ba2009-04-03 13:40:49 -07003162 }
3163 return ret;
3164}
3165
3166/**
3167 * futex_wait_requeue_pi() - Wait on uaddr and take uaddr2
Darren Hart56ec1602009-09-21 22:29:59 -07003168 * @uaddr: the futex we initially wait on (non-pi)
Darren Hartb41277d2010-11-08 13:10:09 -08003169 * @flags: futex flags (FLAGS_SHARED, FLAGS_CLOCKRT, etc.), they must be
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07003170 * the same type, no requeueing from private to shared, etc.
Darren Hart52400ba2009-04-03 13:40:49 -07003171 * @val: the expected value of uaddr
3172 * @abs_time: absolute timeout
Darren Hart56ec1602009-09-21 22:29:59 -07003173 * @bitset: 32 bit wakeup bitset set by userspace, defaults to all
Darren Hart52400ba2009-04-03 13:40:49 -07003174 * @uaddr2: the pi futex we will take prior to returning to user-space
3175 *
3176 * The caller will wait on uaddr and will be requeued by futex_requeue() to
Darren Hart6f7b0a22012-07-20 11:53:31 -07003177 * uaddr2 which must be PI aware and unique from uaddr. Normal wakeup will wake
3178 * on uaddr2 and complete the acquisition of the rt_mutex prior to returning to
3179 * userspace. This ensures the rt_mutex maintains an owner when it has waiters;
3180 * without one, the pi logic would not know which task to boost/deboost, if
3181 * there was a need to.
Darren Hart52400ba2009-04-03 13:40:49 -07003182 *
3183 * We call schedule in futex_wait_queue_me() when we enqueue and return there
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08003184 * via the following--
Darren Hart52400ba2009-04-03 13:40:49 -07003185 * 1) wakeup on uaddr2 after an atomic lock acquisition by futex_requeue()
Darren Hartcc6db4e2009-07-31 16:20:10 -07003186 * 2) wakeup on uaddr2 after a requeue
3187 * 3) signal
3188 * 4) timeout
Darren Hart52400ba2009-04-03 13:40:49 -07003189 *
Darren Hartcc6db4e2009-07-31 16:20:10 -07003190 * If 3, cleanup and return -ERESTARTNOINTR.
Darren Hart52400ba2009-04-03 13:40:49 -07003191 *
3192 * If 2, we may then block on trying to take the rt_mutex and return via:
3193 * 5) successful lock
3194 * 6) signal
3195 * 7) timeout
3196 * 8) other lock acquisition failure
3197 *
Darren Hartcc6db4e2009-07-31 16:20:10 -07003198 * If 6, return -EWOULDBLOCK (restarting the syscall would do the same).
Darren Hart52400ba2009-04-03 13:40:49 -07003199 *
3200 * If 4 or 7, we cleanup and return with -ETIMEDOUT.
3201 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08003202 * Return:
3203 * 0 - On success;
Darren Hart52400ba2009-04-03 13:40:49 -07003204 * <0 - On error
3205 */
Darren Hartb41277d2010-11-08 13:10:09 -08003206static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
Darren Hart52400ba2009-04-03 13:40:49 -07003207 u32 val, ktime_t *abs_time, u32 bitset,
Darren Hartb41277d2010-11-08 13:10:09 -08003208 u32 __user *uaddr2)
Darren Hart52400ba2009-04-03 13:40:49 -07003209{
3210 struct hrtimer_sleeper timeout, *to = NULL;
3211 struct rt_mutex_waiter rt_waiter;
Darren Hart52400ba2009-04-03 13:40:49 -07003212 struct futex_hash_bucket *hb;
Darren Hart5bdb05f2010-11-08 13:40:28 -08003213 union futex_key key2 = FUTEX_KEY_INIT;
3214 struct futex_q q = futex_q_init;
Darren Hart52400ba2009-04-03 13:40:49 -07003215 int res, ret;
Darren Hart52400ba2009-04-03 13:40:49 -07003216
Darren Hart6f7b0a22012-07-20 11:53:31 -07003217 if (uaddr == uaddr2)
3218 return -EINVAL;
3219
Darren Hart52400ba2009-04-03 13:40:49 -07003220 if (!bitset)
3221 return -EINVAL;
3222
3223 if (abs_time) {
3224 to = &timeout;
Darren Hartb41277d2010-11-08 13:10:09 -08003225 hrtimer_init_on_stack(&to->timer, (flags & FLAGS_CLOCKRT) ?
3226 CLOCK_REALTIME : CLOCK_MONOTONIC,
3227 HRTIMER_MODE_ABS);
Darren Hart52400ba2009-04-03 13:40:49 -07003228 hrtimer_init_sleeper(to, current);
3229 hrtimer_set_expires_range_ns(&to->timer, *abs_time,
3230 current->timer_slack_ns);
3231 }
3232
3233 /*
3234 * The waiter is allocated on our stack, manipulated by the requeue
3235 * code while we sleep on uaddr.
3236 */
3237 debug_rt_mutex_init_waiter(&rt_waiter);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01003238 RB_CLEAR_NODE(&rt_waiter.pi_tree_entry);
3239 RB_CLEAR_NODE(&rt_waiter.tree_entry);
Darren Hart52400ba2009-04-03 13:40:49 -07003240 rt_waiter.task = NULL;
3241
Shawn Bohrer9ea71502011-06-30 11:21:32 -05003242 ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE);
Darren Hart52400ba2009-04-03 13:40:49 -07003243 if (unlikely(ret != 0))
3244 goto out;
3245
Darren Hart84bc4af2009-08-13 17:36:53 -07003246 q.bitset = bitset;
3247 q.rt_waiter = &rt_waiter;
3248 q.requeue_pi_key = &key2;
3249
Darren Hart7ada8762010-10-17 08:35:04 -07003250 /*
3251 * Prepare to wait on uaddr. On success, increments q.key (key1) ref
3252 * count.
3253 */
Darren Hartb41277d2010-11-08 13:10:09 -08003254 ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
Thomas Gleixnerc8b15a72009-05-20 09:18:50 +02003255 if (ret)
3256 goto out_key2;
Darren Hart52400ba2009-04-03 13:40:49 -07003257
Thomas Gleixnere9c243a2014-06-03 12:27:06 +00003258 /*
3259 * The check above which compares uaddrs is not sufficient for
3260 * shared futexes. We need to compare the keys:
3261 */
3262 if (match_futex(&q.key, &key2)) {
Thomas Gleixner13c42c22014-09-11 23:44:35 +02003263 queue_unlock(hb);
Thomas Gleixnere9c243a2014-06-03 12:27:06 +00003264 ret = -EINVAL;
3265 goto out_put_keys;
3266 }
3267
Darren Hart52400ba2009-04-03 13:40:49 -07003268 /* Queue the futex_q, drop the hb lock, wait for wakeup. */
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02003269 futex_wait_queue_me(hb, &q, to);
Darren Hart52400ba2009-04-03 13:40:49 -07003270
3271 spin_lock(&hb->lock);
3272 ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
3273 spin_unlock(&hb->lock);
3274 if (ret)
3275 goto out_put_keys;
3276
3277 /*
3278 * In order for us to be here, we know our q.key == key2, and since
3279 * we took the hb->lock above, we also know that futex_requeue() has
3280 * completed and we no longer have to concern ourselves with a wakeup
Darren Hart7ada8762010-10-17 08:35:04 -07003281 * race with the atomic proxy lock acquisition by the requeue code. The
3282 * futex_requeue dropped our key1 reference and incremented our key2
3283 * reference count.
Darren Hart52400ba2009-04-03 13:40:49 -07003284 */
3285
3286 /* Check if the requeue code acquired the second futex for us. */
3287 if (!q.rt_waiter) {
3288 /*
3289 * Got the lock. We might not be the anticipated owner if we
3290 * did a lock-steal - fix up the PI-state in that case.
3291 */
3292 if (q.pi_state && (q.pi_state->owner != current)) {
3293 spin_lock(q.lock_ptr);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01003294 ret = fixup_pi_state_owner(uaddr2, &q, current);
Thomas Gleixnerfb75a422015-12-19 20:07:38 +00003295 /*
3296 * Drop the reference to the pi state which
3297 * the requeue_pi() code acquired for us.
3298 */
Thomas Gleixner29e9ee52015-12-19 20:07:39 +00003299 put_pi_state(q.pi_state);
Darren Hart52400ba2009-04-03 13:40:49 -07003300 spin_unlock(q.lock_ptr);
Thomas Gleixnerad8fdba2021-02-11 09:26:58 +00003301 /*
3302 * Adjust the return value. It's either -EFAULT or
3303 * success (1) but the caller expects 0 for success.
3304 */
3305 ret = ret < 0 ? ret : 0;
Darren Hart52400ba2009-04-03 13:40:49 -07003306 }
3307 } else {
Peter Zijlstra6244ffc2017-03-04 10:27:18 +01003308 struct rt_mutex *pi_mutex;
3309
Darren Hart52400ba2009-04-03 13:40:49 -07003310 /*
3311 * We have been woken up by futex_unlock_pi(), a timeout, or a
3312 * signal. futex_unlock_pi() will not destroy the lock_ptr nor
3313 * the pi_state.
3314 */
Darren Hartf27071c2012-07-20 11:53:30 -07003315 WARN_ON(!q.pi_state);
Darren Hart52400ba2009-04-03 13:40:49 -07003316 pi_mutex = &q.pi_state->pi_mutex;
Peter Zijlstrace813552017-03-22 11:35:57 +01003317 ret = rt_mutex_wait_proxy_lock(pi_mutex, to, &rt_waiter);
Darren Hart52400ba2009-04-03 13:40:49 -07003318
3319 spin_lock(q.lock_ptr);
Peter Zijlstrace813552017-03-22 11:35:57 +01003320 if (ret && !rt_mutex_cleanup_proxy_lock(pi_mutex, &rt_waiter))
3321 ret = 0;
3322
3323 debug_rt_mutex_free_waiter(&rt_waiter);
Darren Hart52400ba2009-04-03 13:40:49 -07003324 /*
3325 * Fixup the pi_state owner and possibly acquire the lock if we
3326 * haven't already.
3327 */
Thomas Gleixnerae791a22010-11-10 13:30:36 +01003328 res = fixup_owner(uaddr2, &q, !ret);
Darren Hart52400ba2009-04-03 13:40:49 -07003329 /*
3330 * If fixup_owner() returned an error, proprogate that. If it
Darren Hart56ec1602009-09-21 22:29:59 -07003331 * acquired the lock, clear -ETIMEDOUT or -EINTR.
Darren Hart52400ba2009-04-03 13:40:49 -07003332 */
3333 if (res)
3334 ret = (res < 0) ? res : 0;
3335
3336 /* Unqueue and drop the lock. */
3337 unqueue_me_pi(&q);
3338 }
3339
Peter Zijlstra6244ffc2017-03-04 10:27:18 +01003340 if (ret == -EINTR) {
Darren Hart52400ba2009-04-03 13:40:49 -07003341 /*
Darren Hartcc6db4e2009-07-31 16:20:10 -07003342 * We've already been requeued, but cannot restart by calling
3343 * futex_lock_pi() directly. We could restart this syscall, but
3344 * it would detect that the user space "val" changed and return
3345 * -EWOULDBLOCK. Save the overhead of the restart and return
3346 * -EWOULDBLOCK directly.
Darren Hart52400ba2009-04-03 13:40:49 -07003347 */
Thomas Gleixner20708872009-05-19 23:04:59 +02003348 ret = -EWOULDBLOCK;
Darren Hart52400ba2009-04-03 13:40:49 -07003349 }
3350
3351out_put_keys:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01003352 put_futex_key(&q.key);
Thomas Gleixnerc8b15a72009-05-20 09:18:50 +02003353out_key2:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01003354 put_futex_key(&key2);
Darren Hart52400ba2009-04-03 13:40:49 -07003355
3356out:
3357 if (to) {
3358 hrtimer_cancel(&to->timer);
3359 destroy_hrtimer_on_stack(&to->timer);
3360 }
3361 return ret;
3362}
3363
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003364/*
3365 * Support for robust futexes: the kernel cleans up held futexes at
3366 * thread exit time.
3367 *
3368 * Implementation: user-space maintains a per-thread list of locks it
3369 * is holding. Upon do_exit(), the kernel carefully walks this list,
3370 * and marks all locks that are owned by this thread with the
Ingo Molnarc87e2832006-06-27 02:54:58 -07003371 * FUTEX_OWNER_DIED bit, and wakes up a waiter (if any). The list is
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003372 * always manipulated with the lock held, so the list is private and
3373 * per-thread. Userspace also maintains a per-thread 'list_op_pending'
3374 * field, to allow the kernel to clean up if the thread dies after
3375 * acquiring the lock, but just before it could have added itself to
3376 * the list. There can only be one such pending lock.
3377 */
3378
3379/**
Darren Hartd96ee562009-09-21 22:30:22 -07003380 * sys_set_robust_list() - Set the robust-futex list head of a task
3381 * @head: pointer to the list-head
3382 * @len: length of the list-head, as userspace expects
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003383 */
Heiko Carstens836f92a2009-01-14 14:14:33 +01003384SYSCALL_DEFINE2(set_robust_list, struct robust_list_head __user *, head,
3385 size_t, len)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003386{
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08003387 if (!futex_cmpxchg_enabled)
3388 return -ENOSYS;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003389 /*
3390 * The kernel knows only one size for now:
3391 */
3392 if (unlikely(len != sizeof(*head)))
3393 return -EINVAL;
3394
3395 current->robust_list = head;
3396
3397 return 0;
3398}
3399
3400/**
Darren Hartd96ee562009-09-21 22:30:22 -07003401 * sys_get_robust_list() - Get the robust-futex list head of a task
3402 * @pid: pid of the process [zero for current task]
3403 * @head_ptr: pointer to a list-head pointer, the kernel fills it in
3404 * @len_ptr: pointer to a length field, the kernel fills in the header size
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003405 */
Heiko Carstens836f92a2009-01-14 14:14:33 +01003406SYSCALL_DEFINE3(get_robust_list, int, pid,
3407 struct robust_list_head __user * __user *, head_ptr,
3408 size_t __user *, len_ptr)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003409{
Al Viroba46df92006-10-10 22:46:07 +01003410 struct robust_list_head __user *head;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003411 unsigned long ret;
Kees Cookbdbb7762012-03-19 16:12:53 -07003412 struct task_struct *p;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003413
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08003414 if (!futex_cmpxchg_enabled)
3415 return -ENOSYS;
3416
Kees Cookbdbb7762012-03-19 16:12:53 -07003417 rcu_read_lock();
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003418
Kees Cookbdbb7762012-03-19 16:12:53 -07003419 ret = -ESRCH;
3420 if (!pid)
3421 p = current;
3422 else {
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07003423 p = find_task_by_vpid(pid);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003424 if (!p)
3425 goto err_unlock;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003426 }
3427
Kees Cookbdbb7762012-03-19 16:12:53 -07003428 ret = -EPERM;
Jann Horncaaee622016-01-20 15:00:04 -08003429 if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS))
Kees Cookbdbb7762012-03-19 16:12:53 -07003430 goto err_unlock;
3431
3432 head = p->robust_list;
3433 rcu_read_unlock();
3434
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003435 if (put_user(sizeof(*head), len_ptr))
3436 return -EFAULT;
3437 return put_user(head, head_ptr);
3438
3439err_unlock:
Oleg Nesterovaaa2a972006-09-29 02:00:55 -07003440 rcu_read_unlock();
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003441
3442 return ret;
3443}
3444
3445/*
3446 * Process a futex-list entry, check whether it's owned by the
3447 * dying task, and do notification if so:
3448 */
Arnd Bergmannbdb116c2021-02-01 10:01:32 +00003449static int handle_futex_death(u32 __user *uaddr, struct task_struct *curr, int pi)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003450{
Vitaliy Ivanov7cfdaf32011-07-07 15:10:31 +03003451 u32 uval, uninitialized_var(nval), mval;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003452
Chen Jie726c28f2019-03-15 03:44:38 +00003453 /* Futex address must be 32bit aligned */
3454 if ((((unsigned long)uaddr) % sizeof(*uaddr)) != 0)
3455 return -1;
3456
Ingo Molnar8f17d3a2006-03-27 01:16:27 -08003457retry:
3458 if (get_user(uval, uaddr))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003459 return -1;
3460
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003461 if ((uval & FUTEX_TID_MASK) == task_pid_vnr(curr)) {
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003462 /*
3463 * Ok, this dying thread is truly holding a futex
3464 * of interest. Set the OWNER_DIED bit atomically
3465 * via cmpxchg, and if the value had FUTEX_WAITERS
3466 * set, wake up a waiter (if any). (We have to do a
3467 * futex_wake() even if OWNER_DIED is already set -
3468 * to handle the rare but possible case of recursive
3469 * thread-death.) The rest of the cleanup is done in
3470 * userspace.
3471 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003472 mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
Thomas Gleixner6e0aa9f2011-03-14 10:34:35 +01003473 /*
3474 * We are not holding a lock here, but we want to have
3475 * the pagefault_disable/enable() protection because
3476 * we want to handle the fault gracefully. If the
3477 * access fails we try to fault in the futex with R/W
3478 * verification via get_user_pages. get_user() above
3479 * does not guarantee R/W access. If that fails we
3480 * give up and leave the futex locked.
3481 */
3482 if (cmpxchg_futex_value_locked(&nval, uaddr, uval, mval)) {
3483 if (fault_in_user_writeable(uaddr))
3484 return -1;
3485 goto retry;
3486 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07003487 if (nval != uval)
Ingo Molnar8f17d3a2006-03-27 01:16:27 -08003488 goto retry;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003489
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003490 /*
3491 * Wake robust non-PI futexes here. The wakeup of
3492 * PI futexes happens in exit_pi_state():
3493 */
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07003494 if (!pi && (uval & FUTEX_WAITERS))
Peter Zijlstrac2f9f202008-09-26 19:32:23 +02003495 futex_wake(uaddr, 1, 1, FUTEX_BITSET_MATCH_ANY);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003496 }
3497 return 0;
3498}
3499
3500/*
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003501 * Fetch a robust-list pointer. Bit 0 signals PI futexes:
3502 */
3503static inline int fetch_robust_entry(struct robust_list __user **entry,
Al Viroba46df92006-10-10 22:46:07 +01003504 struct robust_list __user * __user *head,
Namhyung Kim1dcc41b2010-09-14 21:43:46 +09003505 unsigned int *pi)
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003506{
3507 unsigned long uentry;
3508
Al Viroba46df92006-10-10 22:46:07 +01003509 if (get_user(uentry, (unsigned long __user *)head))
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003510 return -EFAULT;
3511
Al Viroba46df92006-10-10 22:46:07 +01003512 *entry = (void __user *)(uentry & ~1UL);
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003513 *pi = uentry & 1;
3514
3515 return 0;
3516}
3517
3518/*
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003519 * Walk curr->robust_list (very carefully, it's a userspace list!)
3520 * and mark any locks found there dead, and notify any waiters.
3521 *
3522 * We silently return on any sign of list-walking problem.
3523 */
Thomas Gleixner25f319b2021-02-01 10:01:33 +00003524static void exit_robust_list(struct task_struct *curr)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003525{
3526 struct robust_list_head __user *head = curr->robust_list;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003527 struct robust_list __user *entry, *next_entry, *pending;
Darren Hart4c115e92010-11-04 15:00:00 -04003528 unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
3529 unsigned int uninitialized_var(next_pi);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003530 unsigned long futex_offset;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003531 int rc;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003532
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08003533 if (!futex_cmpxchg_enabled)
3534 return;
3535
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003536 /*
3537 * Fetch the list head (which was registered earlier, via
3538 * sys_set_robust_list()):
3539 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003540 if (fetch_robust_entry(&entry, &head->list.next, &pi))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003541 return;
3542 /*
3543 * Fetch the relative futex offset:
3544 */
3545 if (get_user(futex_offset, &head->futex_offset))
3546 return;
3547 /*
3548 * Fetch any possibly pending lock-add first, and handle it
3549 * if it exists:
3550 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003551 if (fetch_robust_entry(&pending, &head->list_op_pending, &pip))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003552 return;
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003553
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003554 next_entry = NULL; /* avoid warning with gcc */
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003555 while (entry != &head->list) {
3556 /*
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003557 * Fetch the next entry in the list before calling
3558 * handle_futex_death:
3559 */
3560 rc = fetch_robust_entry(&next_entry, &entry->next, &next_pi);
3561 /*
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003562 * A pending lock might already be on the list, so
Ingo Molnarc87e2832006-06-27 02:54:58 -07003563 * don't process it twice:
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003564 */
3565 if (entry != pending)
Al Viroba46df92006-10-10 22:46:07 +01003566 if (handle_futex_death((void __user *)entry + futex_offset,
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003567 curr, pi))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003568 return;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003569 if (rc)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003570 return;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003571 entry = next_entry;
3572 pi = next_pi;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003573 /*
3574 * Avoid excessively long or circular lists:
3575 */
3576 if (!--limit)
3577 break;
3578
3579 cond_resched();
3580 }
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003581
3582 if (pending)
3583 handle_futex_death((void __user *)pending + futex_offset,
3584 curr, pip);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003585}
3586
Thomas Gleixnerff3a33f2021-02-01 10:01:40 +00003587static void futex_cleanup(struct task_struct *tsk)
Thomas Gleixner25f319b2021-02-01 10:01:33 +00003588{
3589 if (unlikely(tsk->robust_list)) {
3590 exit_robust_list(tsk);
3591 tsk->robust_list = NULL;
3592 }
3593
3594#ifdef CONFIG_COMPAT
3595 if (unlikely(tsk->compat_robust_list)) {
3596 compat_exit_robust_list(tsk);
3597 tsk->compat_robust_list = NULL;
3598 }
3599#endif
3600
3601 if (unlikely(!list_empty(&tsk->pi_state_list)))
3602 exit_pi_state_list(tsk);
3603}
3604
Thomas Gleixner32d78282021-02-01 10:01:38 +00003605/**
3606 * futex_exit_recursive - Set the tasks futex state to FUTEX_STATE_DEAD
3607 * @tsk: task to set the state on
3608 *
3609 * Set the futex exit state of the task lockless. The futex waiter code
3610 * observes that state when a task is exiting and loops until the task has
3611 * actually finished the futex cleanup. The worst case for this is that the
3612 * waiter runs through the wait loop until the state becomes visible.
3613 *
3614 * This is called from the recursive fault handling path in do_exit().
3615 *
3616 * This is best effort. Either the futex exit code has run already or
3617 * not. If the OWNER_DIED bit has been set on the futex then the waiter can
3618 * take it over. If not, the problem is pushed back to user space. If the
3619 * futex exit code did not run yet, then an already queued waiter might
3620 * block forever, but there is nothing which can be done about that.
3621 */
3622void futex_exit_recursive(struct task_struct *tsk)
3623{
Thomas Gleixnerad3466a2021-02-01 10:01:41 +00003624 /* If the state is FUTEX_STATE_EXITING then futex_exit_mutex is held */
3625 if (tsk->futex_state == FUTEX_STATE_EXITING)
3626 mutex_unlock(&tsk->futex_exit_mutex);
Thomas Gleixner32d78282021-02-01 10:01:38 +00003627 tsk->futex_state = FUTEX_STATE_DEAD;
3628}
3629
Thomas Gleixnerff3a33f2021-02-01 10:01:40 +00003630static void futex_cleanup_begin(struct task_struct *tsk)
Thomas Gleixner8a16d8a2021-02-01 10:01:36 +00003631{
Thomas Gleixner32d78282021-02-01 10:01:38 +00003632 /*
Thomas Gleixnerad3466a2021-02-01 10:01:41 +00003633 * Prevent various race issues against a concurrent incoming waiter
3634 * including live locks by forcing the waiter to block on
3635 * tsk->futex_exit_mutex when it observes FUTEX_STATE_EXITING in
3636 * attach_to_pi_owner().
3637 */
3638 mutex_lock(&tsk->futex_exit_mutex);
3639
3640 /*
Thomas Gleixner0ba263f2021-02-01 10:01:39 +00003641 * Switch the state to FUTEX_STATE_EXITING under tsk->pi_lock.
3642 *
3643 * This ensures that all subsequent checks of tsk->futex_state in
3644 * attach_to_pi_owner() must observe FUTEX_STATE_EXITING with
3645 * tsk->pi_lock held.
3646 *
3647 * It guarantees also that a pi_state which was queued right before
3648 * the state change under tsk->pi_lock by a concurrent waiter must
3649 * be observed in exit_pi_state_list().
Thomas Gleixner32d78282021-02-01 10:01:38 +00003650 */
3651 raw_spin_lock_irq(&tsk->pi_lock);
Thomas Gleixner0ba263f2021-02-01 10:01:39 +00003652 tsk->futex_state = FUTEX_STATE_EXITING;
Thomas Gleixner32d78282021-02-01 10:01:38 +00003653 raw_spin_unlock_irq(&tsk->pi_lock);
Thomas Gleixnerff3a33f2021-02-01 10:01:40 +00003654}
Thomas Gleixner32d78282021-02-01 10:01:38 +00003655
Thomas Gleixnerff3a33f2021-02-01 10:01:40 +00003656static void futex_cleanup_end(struct task_struct *tsk, int state)
3657{
3658 /*
3659 * Lockless store. The only side effect is that an observer might
3660 * take another loop until it becomes visible.
3661 */
3662 tsk->futex_state = state;
Thomas Gleixnerad3466a2021-02-01 10:01:41 +00003663 /*
3664 * Drop the exit protection. This unblocks waiters which observed
3665 * FUTEX_STATE_EXITING to reevaluate the state.
3666 */
3667 mutex_unlock(&tsk->futex_exit_mutex);
Thomas Gleixnerff3a33f2021-02-01 10:01:40 +00003668}
Thomas Gleixner32d78282021-02-01 10:01:38 +00003669
Thomas Gleixnerff3a33f2021-02-01 10:01:40 +00003670void futex_exec_release(struct task_struct *tsk)
3671{
3672 /*
3673 * The state handling is done for consistency, but in the case of
3674 * exec() there is no way to prevent futher damage as the PID stays
3675 * the same. But for the unlikely and arguably buggy case that a
3676 * futex is held on exec(), this provides at least as much state
3677 * consistency protection which is possible.
3678 */
3679 futex_cleanup_begin(tsk);
3680 futex_cleanup(tsk);
3681 /*
3682 * Reset the state to FUTEX_STATE_OK. The task is alive and about
3683 * exec a new binary.
3684 */
3685 futex_cleanup_end(tsk, FUTEX_STATE_OK);
3686}
3687
3688void futex_exit_release(struct task_struct *tsk)
3689{
3690 futex_cleanup_begin(tsk);
3691 futex_cleanup(tsk);
3692 futex_cleanup_end(tsk, FUTEX_STATE_DEAD);
Thomas Gleixner8a16d8a2021-02-01 10:01:36 +00003693}
3694
Pierre Peifferc19384b2007-05-09 02:35:02 -07003695long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
Ingo Molnare2970f22006-06-27 02:54:47 -07003696 u32 __user *uaddr2, u32 val2, u32 val3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697{
Thomas Gleixner81b40532012-02-15 12:17:09 +01003698 int cmd = op & FUTEX_CMD_MASK;
Darren Hartb41277d2010-11-08 13:10:09 -08003699 unsigned int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003701 if (!(op & FUTEX_PRIVATE_FLAG))
Darren Hartb41277d2010-11-08 13:10:09 -08003702 flags |= FLAGS_SHARED;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003703
Darren Hartb41277d2010-11-08 13:10:09 -08003704 if (op & FUTEX_CLOCK_REALTIME) {
3705 flags |= FLAGS_CLOCKRT;
Darren Hart337f1302015-12-18 13:36:37 -08003706 if (cmd != FUTEX_WAIT && cmd != FUTEX_WAIT_BITSET && \
3707 cmd != FUTEX_WAIT_REQUEUE_PI)
Darren Hartb41277d2010-11-08 13:10:09 -08003708 return -ENOSYS;
3709 }
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003710
3711 switch (cmd) {
Thomas Gleixner59263b52012-02-15 12:08:34 +01003712 case FUTEX_LOCK_PI:
3713 case FUTEX_UNLOCK_PI:
3714 case FUTEX_TRYLOCK_PI:
3715 case FUTEX_WAIT_REQUEUE_PI:
3716 case FUTEX_CMP_REQUEUE_PI:
3717 if (!futex_cmpxchg_enabled)
3718 return -ENOSYS;
3719 }
3720
3721 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 case FUTEX_WAIT:
Thomas Gleixnercd689982008-02-01 17:45:14 +01003723 val3 = FUTEX_BITSET_MATCH_ANY;
3724 case FUTEX_WAIT_BITSET:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003725 return futex_wait(uaddr, flags, val, timeout, val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 case FUTEX_WAKE:
Thomas Gleixnercd689982008-02-01 17:45:14 +01003727 val3 = FUTEX_BITSET_MATCH_ANY;
3728 case FUTEX_WAKE_BITSET:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003729 return futex_wake(uaddr, flags, val, val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 case FUTEX_REQUEUE:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003731 return futex_requeue(uaddr, flags, uaddr2, val, val2, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 case FUTEX_CMP_REQUEUE:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003733 return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 0);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07003734 case FUTEX_WAKE_OP:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003735 return futex_wake_op(uaddr, flags, uaddr2, val, val2, val3);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003736 case FUTEX_LOCK_PI:
Michael Kerrisk996636d2015-01-16 20:28:06 +01003737 return futex_lock_pi(uaddr, flags, timeout, 0);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003738 case FUTEX_UNLOCK_PI:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003739 return futex_unlock_pi(uaddr, flags);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003740 case FUTEX_TRYLOCK_PI:
Michael Kerrisk996636d2015-01-16 20:28:06 +01003741 return futex_lock_pi(uaddr, flags, NULL, 1);
Darren Hart52400ba2009-04-03 13:40:49 -07003742 case FUTEX_WAIT_REQUEUE_PI:
3743 val3 = FUTEX_BITSET_MATCH_ANY;
Thomas Gleixner81b40532012-02-15 12:17:09 +01003744 return futex_wait_requeue_pi(uaddr, flags, val, timeout, val3,
3745 uaddr2);
Darren Hart52400ba2009-04-03 13:40:49 -07003746 case FUTEX_CMP_REQUEUE_PI:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003747 return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 }
Thomas Gleixner81b40532012-02-15 12:17:09 +01003749 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750}
3751
3752
Heiko Carstens17da2bd2009-01-14 14:14:10 +01003753SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val,
3754 struct timespec __user *, utime, u32 __user *, uaddr2,
3755 u32, val3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756{
Pierre Peifferc19384b2007-05-09 02:35:02 -07003757 struct timespec ts;
3758 ktime_t t, *tp = NULL;
Ingo Molnare2970f22006-06-27 02:54:47 -07003759 u32 val2 = 0;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003760 int cmd = op & FUTEX_CMD_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761
Thomas Gleixnercd689982008-02-01 17:45:14 +01003762 if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI ||
Darren Hart52400ba2009-04-03 13:40:49 -07003763 cmd == FUTEX_WAIT_BITSET ||
3764 cmd == FUTEX_WAIT_REQUEUE_PI)) {
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07003765 if (unlikely(should_fail_futex(!(op & FUTEX_PRIVATE_FLAG))))
3766 return -EFAULT;
Pierre Peifferc19384b2007-05-09 02:35:02 -07003767 if (copy_from_user(&ts, utime, sizeof(ts)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 return -EFAULT;
Pierre Peifferc19384b2007-05-09 02:35:02 -07003769 if (!timespec_valid(&ts))
Thomas Gleixner9741ef962006-03-31 02:31:32 -08003770 return -EINVAL;
Pierre Peifferc19384b2007-05-09 02:35:02 -07003771
3772 t = timespec_to_ktime(ts);
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003773 if (cmd == FUTEX_WAIT)
Thomas Gleixner5a7780e2008-02-13 09:20:43 +01003774 t = ktime_add_safe(ktime_get(), t);
Pierre Peifferc19384b2007-05-09 02:35:02 -07003775 tp = &t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 }
3777 /*
Darren Hart52400ba2009-04-03 13:40:49 -07003778 * requeue parameter in 'utime' if cmd == FUTEX_*_REQUEUE_*.
Andreas Schwabf54f0982007-07-31 00:38:51 -07003779 * number of waiters to wake in 'utime' if cmd == FUTEX_WAKE_OP.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 */
Andreas Schwabf54f0982007-07-31 00:38:51 -07003781 if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE ||
Darren Hartba9c22f2009-04-20 22:22:22 -07003782 cmd == FUTEX_CMP_REQUEUE_PI || cmd == FUTEX_WAKE_OP)
Ingo Molnare2970f22006-06-27 02:54:47 -07003783 val2 = (u32) (unsigned long) utime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784
Pierre Peifferc19384b2007-05-09 02:35:02 -07003785 return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786}
3787
Arnd Bergmannbdb116c2021-02-01 10:01:32 +00003788#ifdef CONFIG_COMPAT
3789/*
3790 * Fetch a robust-list pointer. Bit 0 signals PI futexes:
3791 */
3792static inline int
3793compat_fetch_robust_entry(compat_uptr_t *uentry, struct robust_list __user **entry,
3794 compat_uptr_t __user *head, unsigned int *pi)
3795{
3796 if (get_user(*uentry, head))
3797 return -EFAULT;
3798
3799 *entry = compat_ptr((*uentry) & ~1);
3800 *pi = (unsigned int)(*uentry) & 1;
3801
3802 return 0;
3803}
3804
3805static void __user *futex_uaddr(struct robust_list __user *entry,
3806 compat_long_t futex_offset)
3807{
3808 compat_uptr_t base = ptr_to_compat(entry);
3809 void __user *uaddr = compat_ptr(base + futex_offset);
3810
3811 return uaddr;
3812}
3813
3814/*
3815 * Walk curr->robust_list (very carefully, it's a userspace list!)
3816 * and mark any locks found there dead, and notify any waiters.
3817 *
3818 * We silently return on any sign of list-walking problem.
3819 */
3820void compat_exit_robust_list(struct task_struct *curr)
3821{
3822 struct compat_robust_list_head __user *head = curr->compat_robust_list;
3823 struct robust_list __user *entry, *next_entry, *pending;
3824 unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
3825 unsigned int uninitialized_var(next_pi);
3826 compat_uptr_t uentry, next_uentry, upending;
3827 compat_long_t futex_offset;
3828 int rc;
3829
3830 if (!futex_cmpxchg_enabled)
3831 return;
3832
3833 /*
3834 * Fetch the list head (which was registered earlier, via
3835 * sys_set_robust_list()):
3836 */
3837 if (compat_fetch_robust_entry(&uentry, &entry, &head->list.next, &pi))
3838 return;
3839 /*
3840 * Fetch the relative futex offset:
3841 */
3842 if (get_user(futex_offset, &head->futex_offset))
3843 return;
3844 /*
3845 * Fetch any possibly pending lock-add first, and handle it
3846 * if it exists:
3847 */
3848 if (compat_fetch_robust_entry(&upending, &pending,
3849 &head->list_op_pending, &pip))
3850 return;
3851
3852 next_entry = NULL; /* avoid warning with gcc */
3853 while (entry != (struct robust_list __user *) &head->list) {
3854 /*
3855 * Fetch the next entry in the list before calling
3856 * handle_futex_death:
3857 */
3858 rc = compat_fetch_robust_entry(&next_uentry, &next_entry,
3859 (compat_uptr_t __user *)&entry->next, &next_pi);
3860 /*
3861 * A pending lock might already be on the list, so
3862 * dont process it twice:
3863 */
3864 if (entry != pending) {
3865 void __user *uaddr = futex_uaddr(entry, futex_offset);
3866
3867 if (handle_futex_death(uaddr, curr, pi))
3868 return;
3869 }
3870 if (rc)
3871 return;
3872 uentry = next_uentry;
3873 entry = next_entry;
3874 pi = next_pi;
3875 /*
3876 * Avoid excessively long or circular lists:
3877 */
3878 if (!--limit)
3879 break;
3880
3881 cond_resched();
3882 }
3883 if (pending) {
3884 void __user *uaddr = futex_uaddr(pending, futex_offset);
3885
3886 handle_futex_death(uaddr, curr, pip);
3887 }
3888}
3889
3890COMPAT_SYSCALL_DEFINE2(set_robust_list,
3891 struct compat_robust_list_head __user *, head,
3892 compat_size_t, len)
3893{
3894 if (!futex_cmpxchg_enabled)
3895 return -ENOSYS;
3896
3897 if (unlikely(len != sizeof(*head)))
3898 return -EINVAL;
3899
3900 current->compat_robust_list = head;
3901
3902 return 0;
3903}
3904
3905COMPAT_SYSCALL_DEFINE3(get_robust_list, int, pid,
3906 compat_uptr_t __user *, head_ptr,
3907 compat_size_t __user *, len_ptr)
3908{
3909 struct compat_robust_list_head __user *head;
3910 unsigned long ret;
3911 struct task_struct *p;
3912
3913 if (!futex_cmpxchg_enabled)
3914 return -ENOSYS;
3915
3916 rcu_read_lock();
3917
3918 ret = -ESRCH;
3919 if (!pid)
3920 p = current;
3921 else {
3922 p = find_task_by_vpid(pid);
3923 if (!p)
3924 goto err_unlock;
3925 }
3926
3927 ret = -EPERM;
3928 if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS))
3929 goto err_unlock;
3930
3931 head = p->compat_robust_list;
3932 rcu_read_unlock();
3933
3934 if (put_user(sizeof(*head), len_ptr))
3935 return -EFAULT;
3936 return put_user(ptr_to_compat(head), head_ptr);
3937
3938err_unlock:
3939 rcu_read_unlock();
3940
3941 return ret;
3942}
3943
3944COMPAT_SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val,
3945 struct compat_timespec __user *, utime, u32 __user *, uaddr2,
3946 u32, val3)
3947{
3948 struct timespec ts;
3949 ktime_t t, *tp = NULL;
3950 int val2 = 0;
3951 int cmd = op & FUTEX_CMD_MASK;
3952
3953 if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI ||
3954 cmd == FUTEX_WAIT_BITSET ||
3955 cmd == FUTEX_WAIT_REQUEUE_PI)) {
3956 if (compat_get_timespec(&ts, utime))
3957 return -EFAULT;
3958 if (!timespec_valid(&ts))
3959 return -EINVAL;
3960
3961 t = timespec_to_ktime(ts);
3962 if (cmd == FUTEX_WAIT)
3963 t = ktime_add_safe(ktime_get(), t);
3964 tp = &t;
3965 }
3966 if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE ||
3967 cmd == FUTEX_CMP_REQUEUE_PI || cmd == FUTEX_WAKE_OP)
3968 val2 = (int) (unsigned long) utime;
3969
3970 return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
3971}
3972#endif /* CONFIG_COMPAT */
3973
Heiko Carstens03b8c7b2014-03-02 13:09:47 +01003974static void __init futex_detect_cmpxchg(void)
3975{
3976#ifndef CONFIG_HAVE_FUTEX_CMPXCHG
3977 u32 curval;
3978
3979 /*
3980 * This will fail and we want it. Some arch implementations do
3981 * runtime detection of the futex_atomic_cmpxchg_inatomic()
3982 * functionality. We want to know that before we call in any
3983 * of the complex code paths. Also we want to prevent
3984 * registration of robust lists in that case. NULL is
3985 * guaranteed to fault and we get -EFAULT on functional
3986 * implementation, the non-functional ones will return
3987 * -ENOSYS.
3988 */
3989 if (cmpxchg_futex_value_locked(&curval, NULL, 0, 0) == -EFAULT)
3990 futex_cmpxchg_enabled = 1;
3991#endif
3992}
3993
Benjamin Herrenschmidtf6d107f2008-03-27 14:52:15 +11003994static int __init futex_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995{
Heiko Carstens63b1a812014-01-16 14:54:50 +01003996 unsigned int futex_shift;
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -08003997 unsigned long i;
3998
3999#if CONFIG_BASE_SMALL
4000 futex_hashsize = 16;
4001#else
4002 futex_hashsize = roundup_pow_of_two(256 * num_possible_cpus());
4003#endif
4004
4005 futex_queues = alloc_large_system_hash("futex", sizeof(*futex_queues),
4006 futex_hashsize, 0,
4007 futex_hashsize < 256 ? HASH_SMALL : 0,
Heiko Carstens63b1a812014-01-16 14:54:50 +01004008 &futex_shift, NULL,
4009 futex_hashsize, futex_hashsize);
4010 futex_hashsize = 1UL << futex_shift;
Heiko Carstens03b8c7b2014-03-02 13:09:47 +01004011
4012 futex_detect_cmpxchg();
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08004013
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -08004014 for (i = 0; i < futex_hashsize; i++) {
Linus Torvalds11d46162014-03-20 22:11:17 -07004015 atomic_set(&futex_queues[i].waiters, 0);
Dima Zavin732375c2011-07-07 17:27:59 -07004016 plist_head_init(&futex_queues[i].chain);
Thomas Gleixner3e4ab742008-02-23 15:23:55 -08004017 spin_lock_init(&futex_queues[i].lock);
4018 }
4019
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 return 0;
4021}
Yang Yang808de342016-12-30 16:17:55 +08004022core_initcall(futex_init);