blob: 97799bf82543401bd0e2c07750a7aa4865dd2c2e [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
830static struct futex_pi_state * alloc_pi_state(void)
831{
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
Brian Silverman30a6b802014-10-25 20:20:37 -0400840/*
Thomas Gleixner29e9ee52015-12-19 20:07:39 +0000841 * Drops a reference to the pi_state object and frees or caches it
842 * when the last reference is gone.
843 *
Brian Silverman30a6b802014-10-25 20:20:37 -0400844 * Must be called with the hb lock held.
845 */
Thomas Gleixner29e9ee52015-12-19 20:07:39 +0000846static void put_pi_state(struct futex_pi_state *pi_state)
Ingo Molnarc87e2832006-06-27 02:54:58 -0700847{
Brian Silverman30a6b802014-10-25 20:20:37 -0400848 if (!pi_state)
849 return;
850
Ingo Molnarc87e2832006-06-27 02:54:58 -0700851 if (!atomic_dec_and_test(&pi_state->refcount))
852 return;
853
854 /*
855 * If pi_state->owner is NULL, the owner is most probably dying
856 * and has cleaned up the pi_state already
857 */
858 if (pi_state->owner) {
Thomas Gleixner1d615482009-11-17 14:54:03 +0100859 raw_spin_lock_irq(&pi_state->owner->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700860 list_del_init(&pi_state->list);
Thomas Gleixner1d615482009-11-17 14:54:03 +0100861 raw_spin_unlock_irq(&pi_state->owner->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700862
863 rt_mutex_proxy_unlock(&pi_state->pi_mutex, pi_state->owner);
864 }
865
866 if (current->pi_state_cache)
867 kfree(pi_state);
868 else {
869 /*
870 * pi_state->list is already empty.
871 * clear pi_state->owner.
872 * refcount is at 0 - put it back to 1.
873 */
874 pi_state->owner = NULL;
875 atomic_set(&pi_state->refcount, 1);
876 current->pi_state_cache = pi_state;
877 }
878}
879
880/*
881 * Look up the task based on what TID userspace gave us.
882 * We dont trust it.
883 */
884static struct task_struct * futex_find_get_task(pid_t pid)
885{
886 struct task_struct *p;
887
Oleg Nesterovd359b542006-09-29 02:00:55 -0700888 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700889 p = find_task_by_vpid(pid);
Michal Hocko7a0ea092010-06-30 09:51:19 +0200890 if (p)
891 get_task_struct(p);
Thomas Gleixnera06381f2007-06-23 11:48:40 +0200892
Oleg Nesterovd359b542006-09-29 02:00:55 -0700893 rcu_read_unlock();
Ingo Molnarc87e2832006-06-27 02:54:58 -0700894
895 return p;
896}
897
898/*
899 * This task is holding PI mutexes at exit time => bad.
900 * Kernel cleans up PI-state, but userspace is likely hosed.
901 * (Robust-futex cleanup is separate and might save the day for userspace.)
902 */
Thomas Gleixner25f319b2021-02-01 10:01:33 +0000903static void exit_pi_state_list(struct task_struct *curr)
Ingo Molnarc87e2832006-06-27 02:54:58 -0700904{
Ingo Molnarc87e2832006-06-27 02:54:58 -0700905 struct list_head *next, *head = &curr->pi_state_list;
906 struct futex_pi_state *pi_state;
Ingo Molnar627371d2006-07-29 05:16:20 +0200907 struct futex_hash_bucket *hb;
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200908 union futex_key key = FUTEX_KEY_INIT;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700909
Thomas Gleixnera0c1e902008-02-23 15:23:57 -0800910 if (!futex_cmpxchg_enabled)
911 return;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700912 /*
913 * We are a ZOMBIE and nobody can enqueue itself on
914 * pi_state_list anymore, but we have to be careful
Ingo Molnar627371d2006-07-29 05:16:20 +0200915 * versus waiters unqueueing themselves:
Ingo Molnarc87e2832006-06-27 02:54:58 -0700916 */
Thomas Gleixner1d615482009-11-17 14:54:03 +0100917 raw_spin_lock_irq(&curr->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700918 while (!list_empty(head)) {
919
920 next = head->next;
921 pi_state = list_entry(next, struct futex_pi_state, list);
922 key = pi_state->key;
Ingo Molnar627371d2006-07-29 05:16:20 +0200923 hb = hash_futex(&key);
Thomas Gleixner1d615482009-11-17 14:54:03 +0100924 raw_spin_unlock_irq(&curr->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700925
Ingo Molnarc87e2832006-06-27 02:54:58 -0700926 spin_lock(&hb->lock);
927
Thomas Gleixner1d615482009-11-17 14:54:03 +0100928 raw_spin_lock_irq(&curr->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +0200929 /*
930 * We dropped the pi-lock, so re-check whether this
931 * task still owns the PI-state:
932 */
Ingo Molnarc87e2832006-06-27 02:54:58 -0700933 if (head->next != next) {
934 spin_unlock(&hb->lock);
935 continue;
936 }
937
Ingo Molnarc87e2832006-06-27 02:54:58 -0700938 WARN_ON(pi_state->owner != curr);
Ingo Molnar627371d2006-07-29 05:16:20 +0200939 WARN_ON(list_empty(&pi_state->list));
940 list_del_init(&pi_state->list);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700941 pi_state->owner = NULL;
Thomas Gleixner1d615482009-11-17 14:54:03 +0100942 raw_spin_unlock_irq(&curr->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700943
Peter Zijlstra2c60d4a2021-02-03 13:45:30 +0000944 rt_mutex_futex_unlock(&pi_state->pi_mutex);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700945
946 spin_unlock(&hb->lock);
947
Thomas Gleixner1d615482009-11-17 14:54:03 +0100948 raw_spin_lock_irq(&curr->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700949 }
Thomas Gleixner1d615482009-11-17 14:54:03 +0100950 raw_spin_unlock_irq(&curr->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700951}
952
Thomas Gleixner54a21782014-06-03 12:27:08 +0000953/*
954 * We need to check the following states:
955 *
956 * Waiter | pi_state | pi->owner | uTID | uODIED | ?
957 *
958 * [1] NULL | --- | --- | 0 | 0/1 | Valid
959 * [2] NULL | --- | --- | >0 | 0/1 | Valid
960 *
961 * [3] Found | NULL | -- | Any | 0/1 | Invalid
962 *
963 * [4] Found | Found | NULL | 0 | 1 | Valid
964 * [5] Found | Found | NULL | >0 | 1 | Invalid
965 *
966 * [6] Found | Found | task | 0 | 1 | Valid
967 *
968 * [7] Found | Found | NULL | Any | 0 | Invalid
969 *
970 * [8] Found | Found | task | ==taskTID | 0/1 | Valid
971 * [9] Found | Found | task | 0 | 0 | Invalid
972 * [10] Found | Found | task | !=taskTID | 0/1 | Invalid
973 *
974 * [1] Indicates that the kernel can acquire the futex atomically. We
975 * came came here due to a stale FUTEX_WAITERS/FUTEX_OWNER_DIED bit.
976 *
977 * [2] Valid, if TID does not belong to a kernel thread. If no matching
978 * thread is found then it indicates that the owner TID has died.
979 *
980 * [3] Invalid. The waiter is queued on a non PI futex
981 *
982 * [4] Valid state after exit_robust_list(), which sets the user space
983 * value to FUTEX_WAITERS | FUTEX_OWNER_DIED.
984 *
985 * [5] The user space value got manipulated between exit_robust_list()
986 * and exit_pi_state_list()
987 *
988 * [6] Valid state after exit_pi_state_list() which sets the new owner in
989 * the pi_state but cannot access the user space value.
990 *
991 * [7] pi_state->owner can only be NULL when the OWNER_DIED bit is set.
992 *
993 * [8] Owner and user space value match
994 *
995 * [9] There is no transient state which sets the user space TID to 0
996 * except exit_robust_list(), but this is indicated by the
997 * FUTEX_OWNER_DIED bit. See [4]
998 *
999 * [10] There is no transient state which leaves owner and user space
1000 * TID out of sync.
1001 */
Thomas Gleixnere60cbc52014-06-11 20:45:39 +00001002
1003/*
1004 * Validate that the existing waiter has a pi_state and sanity check
1005 * the pi_state against the user space value. If correct, attach to
1006 * it.
1007 */
1008static int attach_to_pi_state(u32 uval, struct futex_pi_state *pi_state,
1009 struct futex_pi_state **ps)
1010{
1011 pid_t pid = uval & FUTEX_TID_MASK;
1012
1013 /*
1014 * Userspace might have messed up non-PI and PI futexes [3]
1015 */
1016 if (unlikely(!pi_state))
1017 return -EINVAL;
1018
1019 WARN_ON(!atomic_read(&pi_state->refcount));
1020
1021 /*
1022 * Handle the owner died case:
1023 */
1024 if (uval & FUTEX_OWNER_DIED) {
1025 /*
1026 * exit_pi_state_list sets owner to NULL and wakes the
1027 * topmost waiter. The task which acquires the
1028 * pi_state->rt_mutex will fixup owner.
1029 */
1030 if (!pi_state->owner) {
1031 /*
1032 * No pi state owner, but the user space TID
1033 * is not 0. Inconsistent state. [5]
1034 */
1035 if (pid)
1036 return -EINVAL;
1037 /*
1038 * Take a ref on the state and return success. [4]
1039 */
1040 goto out_state;
1041 }
1042
1043 /*
1044 * If TID is 0, then either the dying owner has not
1045 * yet executed exit_pi_state_list() or some waiter
1046 * acquired the rtmutex in the pi state, but did not
1047 * yet fixup the TID in user space.
1048 *
1049 * Take a ref on the state and return success. [6]
1050 */
1051 if (!pid)
1052 goto out_state;
1053 } else {
1054 /*
1055 * If the owner died bit is not set, then the pi_state
1056 * must have an owner. [7]
1057 */
1058 if (!pi_state->owner)
1059 return -EINVAL;
1060 }
1061
1062 /*
1063 * Bail out if user space manipulated the futex value. If pi
1064 * state exists then the owner TID must be the same as the
1065 * user space TID. [9/10]
1066 */
1067 if (pid != task_pid_vnr(pi_state->owner))
1068 return -EINVAL;
1069out_state:
1070 atomic_inc(&pi_state->refcount);
1071 *ps = pi_state;
1072 return 0;
1073}
1074
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001075/**
1076 * wait_for_owner_exiting - Block until the owner has exited
1077 * @exiting: Pointer to the exiting task
1078 *
1079 * Caller must hold a refcount on @exiting.
1080 */
1081static void wait_for_owner_exiting(int ret, struct task_struct *exiting)
1082{
1083 if (ret != -EBUSY) {
1084 WARN_ON_ONCE(exiting);
1085 return;
1086 }
1087
1088 if (WARN_ON_ONCE(ret == -EBUSY && !exiting))
1089 return;
1090
1091 mutex_lock(&exiting->futex_exit_mutex);
1092 /*
1093 * No point in doing state checking here. If the waiter got here
1094 * while the task was in exec()->exec_futex_release() then it can
1095 * have any FUTEX_STATE_* value when the waiter has acquired the
1096 * mutex. OK, if running, EXITING or DEAD if it reached exit()
1097 * already. Highly unlikely and not a problem. Just one more round
1098 * through the futex maze.
1099 */
1100 mutex_unlock(&exiting->futex_exit_mutex);
1101
1102 put_task_struct(exiting);
1103}
1104
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001105/*
1106 * Lookup the task for the TID provided from user space and attach to
1107 * it after doing proper sanity checks.
1108 */
1109static int attach_to_pi_owner(u32 uval, union futex_key *key,
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001110 struct futex_pi_state **ps,
1111 struct task_struct **exiting)
Ingo Molnarc87e2832006-06-27 02:54:58 -07001112{
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001113 pid_t pid = uval & FUTEX_TID_MASK;
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001114 struct futex_pi_state *pi_state;
1115 struct task_struct *p;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001116
1117 /*
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001118 * We are the first waiter - try to look up the real owner and attach
Thomas Gleixner54a21782014-06-03 12:27:08 +00001119 * the new pi_state to it, but bail out when TID = 0 [1]
Ingo Molnarc87e2832006-06-27 02:54:58 -07001120 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001121 if (!pid)
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001122 return -ESRCH;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001123 p = futex_find_get_task(pid);
Michal Hocko7a0ea092010-06-30 09:51:19 +02001124 if (!p)
1125 return -ESRCH;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001126
Oleg Nesterova2129462015-02-02 15:05:36 +01001127 if (unlikely(p->flags & PF_KTHREAD)) {
Thomas Gleixnerf0d71b32014-05-12 20:45:35 +00001128 put_task_struct(p);
1129 return -EPERM;
1130 }
1131
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001132 /*
Thomas Gleixner2c116892021-02-01 10:01:34 +00001133 * We need to look at the task state to figure out, whether the
1134 * task is exiting. To protect against the change of the task state
1135 * in futex_exit_release(), we do this protected by p->pi_lock:
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001136 */
Thomas Gleixner1d615482009-11-17 14:54:03 +01001137 raw_spin_lock_irq(&p->pi_lock);
Thomas Gleixner2c116892021-02-01 10:01:34 +00001138 if (unlikely(p->futex_state != FUTEX_STATE_OK)) {
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001139 /*
Thomas Gleixner2c116892021-02-01 10:01:34 +00001140 * The task is on the way out. When the futex state is
1141 * FUTEX_STATE_DEAD, we know that the task has finished
1142 * the cleanup:
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001143 */
Thomas Gleixner2c116892021-02-01 10:01:34 +00001144 int ret = (p->futex_state = FUTEX_STATE_DEAD) ? -ESRCH : -EAGAIN;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001145
Thomas Gleixner1d615482009-11-17 14:54:03 +01001146 raw_spin_unlock_irq(&p->pi_lock);
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001147 /*
1148 * If the owner task is between FUTEX_STATE_EXITING and
1149 * FUTEX_STATE_DEAD then store the task pointer and keep
1150 * the reference on the task struct. The calling code will
1151 * drop all locks, wait for the task to reach
1152 * FUTEX_STATE_DEAD and then drop the refcount. This is
1153 * required to prevent a live lock when the current task
1154 * preempted the exiting task between the two states.
1155 */
1156 if (ret == -EBUSY)
1157 *exiting = p;
1158 else
1159 put_task_struct(p);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001160 return ret;
1161 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001162
Thomas Gleixner54a21782014-06-03 12:27:08 +00001163 /*
1164 * No existing pi state. First waiter. [2]
1165 */
Ingo Molnarc87e2832006-06-27 02:54:58 -07001166 pi_state = alloc_pi_state();
1167
1168 /*
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001169 * Initialize the pi_mutex in locked state and make @p
Ingo Molnarc87e2832006-06-27 02:54:58 -07001170 * the owner of it:
1171 */
1172 rt_mutex_init_proxy_locked(&pi_state->pi_mutex, p);
1173
1174 /* Store the key for possible exit cleanups: */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001175 pi_state->key = *key;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001176
Ingo Molnar627371d2006-07-29 05:16:20 +02001177 WARN_ON(!list_empty(&pi_state->list));
Ingo Molnarc87e2832006-06-27 02:54:58 -07001178 list_add(&pi_state->list, &p->pi_state_list);
1179 pi_state->owner = p;
Thomas Gleixner1d615482009-11-17 14:54:03 +01001180 raw_spin_unlock_irq(&p->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001181
1182 put_task_struct(p);
1183
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001184 *ps = pi_state;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001185
1186 return 0;
1187}
1188
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001189static int lookup_pi_state(u32 uval, struct futex_hash_bucket *hb,
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001190 union futex_key *key, struct futex_pi_state **ps,
1191 struct task_struct **exiting)
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001192{
1193 struct futex_q *match = futex_top_waiter(hb, key);
1194
1195 /*
1196 * If there is a waiter on that futex, validate it and
1197 * attach to the pi_state when the validation succeeds.
1198 */
1199 if (match)
1200 return attach_to_pi_state(uval, match->pi_state, ps);
1201
1202 /*
1203 * We are the first waiter - try to look up the owner based on
1204 * @uval and attach to it.
1205 */
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001206 return attach_to_pi_owner(uval, key, ps, exiting);
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001207}
1208
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001209static int lock_pi_update_atomic(u32 __user *uaddr, u32 uval, u32 newval)
1210{
1211 u32 uninitialized_var(curval);
1212
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001213 if (unlikely(should_fail_futex(true)))
1214 return -EFAULT;
1215
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001216 if (unlikely(cmpxchg_futex_value_locked(&curval, uaddr, uval, newval)))
1217 return -EFAULT;
1218
1219 /*If user space value changed, let the caller retry */
1220 return curval != uval ? -EAGAIN : 0;
1221}
1222
Darren Hart1a520842009-04-03 13:39:52 -07001223/**
Darren Hartd96ee562009-09-21 22:30:22 -07001224 * futex_lock_pi_atomic() - Atomic work required to acquire a pi aware futex
Darren Hartbab5bc92009-04-07 23:23:50 -07001225 * @uaddr: the pi futex user address
1226 * @hb: the pi futex hash bucket
1227 * @key: the futex key associated with uaddr and hb
1228 * @ps: the pi_state pointer where we store the result of the
1229 * lookup
1230 * @task: the task to perform the atomic lock work for. This will
1231 * be "current" except in the case of requeue pi.
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001232 * @exiting: Pointer to store the task pointer of the owner task
1233 * which is in the middle of exiting
Darren Hartbab5bc92009-04-07 23:23:50 -07001234 * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0)
Darren Hart1a520842009-04-03 13:39:52 -07001235 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08001236 * Return:
1237 * 0 - ready to wait;
1238 * 1 - acquired the lock;
Darren Hart1a520842009-04-03 13:39:52 -07001239 * <0 - error
1240 *
1241 * The hb->lock and futex_key refs shall be held by the caller.
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001242 *
1243 * @exiting is only set when the return value is -EBUSY. If so, this holds
1244 * a refcount on the exiting task on return and the caller needs to drop it
1245 * after waiting for the exit to complete.
Darren Hart1a520842009-04-03 13:39:52 -07001246 */
1247static int futex_lock_pi_atomic(u32 __user *uaddr, struct futex_hash_bucket *hb,
1248 union futex_key *key,
1249 struct futex_pi_state **ps,
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001250 struct task_struct *task,
1251 struct task_struct **exiting,
1252 int set_waiters)
Darren Hart1a520842009-04-03 13:39:52 -07001253{
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001254 u32 uval, newval, vpid = task_pid_vnr(task);
1255 struct futex_q *match;
1256 int ret;
Darren Hart1a520842009-04-03 13:39:52 -07001257
1258 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001259 * Read the user space value first so we can validate a few
1260 * things before proceeding further.
Darren Hart1a520842009-04-03 13:39:52 -07001261 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001262 if (get_futex_value_locked(&uval, uaddr))
Darren Hart1a520842009-04-03 13:39:52 -07001263 return -EFAULT;
1264
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001265 if (unlikely(should_fail_futex(true)))
1266 return -EFAULT;
1267
Darren Hart1a520842009-04-03 13:39:52 -07001268 /*
1269 * Detect deadlocks.
1270 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001271 if ((unlikely((uval & FUTEX_TID_MASK) == vpid)))
Darren Hart1a520842009-04-03 13:39:52 -07001272 return -EDEADLK;
1273
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001274 if ((unlikely(should_fail_futex(true))))
1275 return -EDEADLK;
1276
Darren Hart1a520842009-04-03 13:39:52 -07001277 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001278 * Lookup existing state first. If it exists, try to attach to
1279 * its pi_state.
Darren Hart1a520842009-04-03 13:39:52 -07001280 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001281 match = futex_top_waiter(hb, key);
1282 if (match)
1283 return attach_to_pi_state(uval, match->pi_state, ps);
1284
1285 /*
1286 * No waiter and user TID is 0. We are here because the
1287 * waiters or the owner died bit is set or called from
1288 * requeue_cmp_pi or for whatever reason something took the
1289 * syscall.
1290 */
1291 if (!(uval & FUTEX_TID_MASK)) {
Thomas Gleixnerb3eaa9f2014-06-03 12:27:06 +00001292 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001293 * We take over the futex. No other waiters and the user space
1294 * TID is 0. We preserve the owner died bit.
Thomas Gleixnerb3eaa9f2014-06-03 12:27:06 +00001295 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001296 newval = uval & FUTEX_OWNER_DIED;
1297 newval |= vpid;
1298
1299 /* The futex requeue_pi code can enforce the waiters bit */
1300 if (set_waiters)
1301 newval |= FUTEX_WAITERS;
1302
1303 ret = lock_pi_update_atomic(uaddr, uval, newval);
1304 /* If the take over worked, return 1 */
1305 return ret < 0 ? ret : 1;
Thomas Gleixnerb3eaa9f2014-06-03 12:27:06 +00001306 }
Darren Hart1a520842009-04-03 13:39:52 -07001307
Darren Hart1a520842009-04-03 13:39:52 -07001308 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001309 * First waiter. Set the waiters bit before attaching ourself to
1310 * the owner. If owner tries to unlock, it will be forced into
1311 * the kernel and blocked on hb->lock.
Darren Hart1a520842009-04-03 13:39:52 -07001312 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001313 newval = uval | FUTEX_WAITERS;
1314 ret = lock_pi_update_atomic(uaddr, uval, newval);
1315 if (ret)
1316 return ret;
Darren Hart1a520842009-04-03 13:39:52 -07001317 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001318 * If the update of the user space value succeeded, we try to
1319 * attach to the owner. If that fails, no harm done, we only
1320 * set the FUTEX_WAITERS bit in the user space variable.
Darren Hart1a520842009-04-03 13:39:52 -07001321 */
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001322 return attach_to_pi_owner(uval, key, ps, exiting);
Darren Hart1a520842009-04-03 13:39:52 -07001323}
1324
Lai Jiangshan2e129782010-12-22 14:18:50 +08001325/**
1326 * __unqueue_futex() - Remove the futex_q from its futex_hash_bucket
1327 * @q: The futex_q to unqueue
1328 *
1329 * The q->lock_ptr must not be NULL and must be held by the caller.
1330 */
1331static void __unqueue_futex(struct futex_q *q)
1332{
1333 struct futex_hash_bucket *hb;
1334
Steven Rostedt29096202011-03-17 15:21:07 -04001335 if (WARN_ON_SMP(!q->lock_ptr || !spin_is_locked(q->lock_ptr))
1336 || WARN_ON(plist_node_empty(&q->list)))
Lai Jiangshan2e129782010-12-22 14:18:50 +08001337 return;
1338
1339 hb = container_of(q->lock_ptr, struct futex_hash_bucket, lock);
1340 plist_del(&q->list, &hb->chain);
Linus Torvalds11d46162014-03-20 22:11:17 -07001341 hb_waiters_dec(hb);
Lai Jiangshan2e129782010-12-22 14:18:50 +08001342}
1343
Ingo Molnarc87e2832006-06-27 02:54:58 -07001344/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 * The hash bucket lock must be held when this is called.
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001346 * Afterwards, the futex_q must not be accessed. Callers
1347 * must ensure to later call wake_up_q() for the actual
1348 * wakeups to occur.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 */
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001350static void mark_wake_futex(struct wake_q_head *wake_q, struct futex_q *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001352 struct task_struct *p = q->task;
1353
Darren Hartaa109902012-11-26 16:29:56 -08001354 if (WARN(q->pi_state || q->rt_waiter, "refusing to wake PI futex\n"))
1355 return;
1356
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001357 /*
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001358 * Queue the task for later wakeup for after we've released
1359 * the hb->lock. wake_q_add() grabs reference to p.
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001360 */
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001361 wake_q_add(wake_q, p);
Lai Jiangshan2e129782010-12-22 14:18:50 +08001362 __unqueue_futex(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 /*
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001364 * The waiting task can free the futex_q as soon as
1365 * q->lock_ptr = NULL is written, without taking any locks. A
1366 * memory barrier is required here to prevent the following
1367 * store to lock_ptr from getting ahead of the plist_del.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 */
Ralf Baechleccdea2f2006-12-06 20:40:26 -08001369 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 q->lock_ptr = NULL;
1371}
1372
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001373static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this,
1374 struct futex_hash_bucket *hb)
Ingo Molnarc87e2832006-06-27 02:54:58 -07001375{
1376 struct task_struct *new_owner;
1377 struct futex_pi_state *pi_state = this->pi_state;
Vitaliy Ivanov7cfdaf32011-07-07 15:10:31 +03001378 u32 uninitialized_var(curval), newval;
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001379 WAKE_Q(wake_q);
1380 bool deboost;
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001381 int ret = 0;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001382
1383 if (!pi_state)
1384 return -EINVAL;
1385
Thomas Gleixner51246bf2010-02-02 11:40:27 +01001386 /*
1387 * If current does not own the pi_state then the futex is
1388 * inconsistent and user space fiddled with the futex value.
1389 */
1390 if (pi_state->owner != current)
1391 return -EINVAL;
1392
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001393 raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001394 new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);
1395
1396 /*
Steven Rostedtf123c982011-01-06 15:08:29 -05001397 * It is possible that the next waiter (the one that brought
1398 * this owner to the kernel) timed out and is no longer
1399 * waiting on the lock.
Ingo Molnarc87e2832006-06-27 02:54:58 -07001400 */
1401 if (!new_owner)
1402 new_owner = this->task;
1403
1404 /*
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001405 * We pass it to the next owner. The WAITERS bit is always
1406 * kept enabled while there is PI state around. We cleanup the
1407 * owner died bit, because we are the owner.
Ingo Molnarc87e2832006-06-27 02:54:58 -07001408 */
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001409 newval = FUTEX_WAITERS | task_pid_vnr(new_owner);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001410
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001411 if (unlikely(should_fail_futex(true)))
1412 ret = -EFAULT;
1413
Sebastian Andrzej Siewior89e9e662016-04-15 14:35:39 +02001414 if (cmpxchg_futex_value_locked(&curval, uaddr, uval, newval)) {
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001415 ret = -EFAULT;
Sebastian Andrzej Siewior89e9e662016-04-15 14:35:39 +02001416 } else if (curval != uval) {
1417 /*
1418 * If a unconditional UNLOCK_PI operation (user space did not
1419 * try the TID->0 transition) raced with a waiter setting the
1420 * FUTEX_WAITERS flag between get_user() and locking the hash
1421 * bucket lock, retry the operation.
1422 */
1423 if ((FUTEX_TID_MASK & curval) == uval)
1424 ret = -EAGAIN;
1425 else
1426 ret = -EINVAL;
1427 }
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001428 if (ret) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001429 raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001430 return ret;
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001431 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001432
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001433 raw_spin_lock(&pi_state->owner->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +02001434 WARN_ON(list_empty(&pi_state->list));
1435 list_del_init(&pi_state->list);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001436 raw_spin_unlock(&pi_state->owner->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +02001437
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001438 raw_spin_lock(&new_owner->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +02001439 WARN_ON(!list_empty(&pi_state->list));
Ingo Molnarc87e2832006-06-27 02:54:58 -07001440 list_add(&pi_state->list, &new_owner->pi_state_list);
1441 pi_state->owner = new_owner;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001442 raw_spin_unlock(&new_owner->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +02001443
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001444 /*
Peter Zijlstra2c60d4a2021-02-03 13:45:30 +00001445 * We've updated the uservalue, this unlock cannot fail.
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001446 */
Peter Zijlstra2c60d4a2021-02-03 13:45:30 +00001447 deboost = __rt_mutex_futex_unlock(&pi_state->pi_mutex, &wake_q);
1448
1449 raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001450 spin_unlock(&hb->lock);
Peter Zijlstra2c60d4a2021-02-03 13:45:30 +00001451
1452 if (deboost) {
1453 wake_up_q(&wake_q);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001454 rt_mutex_adjust_prio(current);
Peter Zijlstra2c60d4a2021-02-03 13:45:30 +00001455 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001456
1457 return 0;
1458}
1459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460/*
Ingo Molnar8b8f3192006-07-03 00:25:05 -07001461 * Express the locking dependencies for lockdep:
1462 */
1463static inline void
1464double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
1465{
1466 if (hb1 <= hb2) {
1467 spin_lock(&hb1->lock);
1468 if (hb1 < hb2)
1469 spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING);
1470 } else { /* hb1 > hb2 */
1471 spin_lock(&hb2->lock);
1472 spin_lock_nested(&hb1->lock, SINGLE_DEPTH_NESTING);
1473 }
1474}
1475
Darren Hart5eb3dc62009-03-12 00:55:52 -07001476static inline void
1477double_unlock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
1478{
Darren Hartf061d352009-03-12 15:11:18 -07001479 spin_unlock(&hb1->lock);
Ingo Molnar88f502f2009-03-13 10:32:07 +01001480 if (hb1 != hb2)
1481 spin_unlock(&hb2->lock);
Darren Hart5eb3dc62009-03-12 00:55:52 -07001482}
1483
Ingo Molnar8b8f3192006-07-03 00:25:05 -07001484/*
Darren Hartb2d09942009-03-12 00:55:37 -07001485 * Wake up waiters matching bitset queued on this futex (uaddr).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 */
Darren Hartb41277d2010-11-08 13:10:09 -08001487static int
1488futex_wake(u32 __user *uaddr, unsigned int flags, int nr_wake, u32 bitset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
Ingo Molnare2970f22006-06-27 02:54:47 -07001490 struct futex_hash_bucket *hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 struct futex_q *this, *next;
Peter Zijlstra38d47c12008-09-26 19:32:20 +02001492 union futex_key key = FUTEX_KEY_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 int ret;
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001494 WAKE_Q(wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Thomas Gleixnercd689982008-02-01 17:45:14 +01001496 if (!bitset)
1497 return -EINVAL;
1498
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001499 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 if (unlikely(ret != 0))
1501 goto out;
1502
Ingo Molnare2970f22006-06-27 02:54:47 -07001503 hb = hash_futex(&key);
Davidlohr Buesob0c29f72014-01-12 15:31:25 -08001504
1505 /* Make sure we really have tasks to wakeup */
1506 if (!hb_waiters_pending(hb))
1507 goto out_put_key;
1508
Ingo Molnare2970f22006-06-27 02:54:47 -07001509 spin_lock(&hb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Jason Low0d00c7b2014-01-12 15:31:22 -08001511 plist_for_each_entry_safe(this, next, &hb->chain, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 if (match_futex (&this->key, &key)) {
Darren Hart52400ba2009-04-03 13:40:49 -07001513 if (this->pi_state || this->rt_waiter) {
Ingo Molnared6f7b12006-07-01 04:35:46 -07001514 ret = -EINVAL;
1515 break;
1516 }
Thomas Gleixnercd689982008-02-01 17:45:14 +01001517
1518 /* Check if one of the bits is set in both bitsets */
1519 if (!(this->bitset & bitset))
1520 continue;
1521
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001522 mark_wake_futex(&wake_q, this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 if (++ret >= nr_wake)
1524 break;
1525 }
1526 }
1527
Ingo Molnare2970f22006-06-27 02:54:47 -07001528 spin_unlock(&hb->lock);
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001529 wake_up_q(&wake_q);
Davidlohr Buesob0c29f72014-01-12 15:31:25 -08001530out_put_key:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001531 put_futex_key(&key);
Darren Hart42d35d42008-12-29 15:49:53 -08001532out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 return ret;
1534}
1535
Jiri Slaby81da9f82017-08-24 09:31:05 +02001536static int futex_atomic_op_inuser(unsigned int encoded_op, u32 __user *uaddr)
1537{
1538 unsigned int op = (encoded_op & 0x70000000) >> 28;
1539 unsigned int cmp = (encoded_op & 0x0f000000) >> 24;
Jiri Slabya1640092017-11-30 15:35:44 +01001540 int oparg = sign_extend32((encoded_op & 0x00fff000) >> 12, 11);
1541 int cmparg = sign_extend32(encoded_op & 0x00000fff, 11);
Jiri Slaby81da9f82017-08-24 09:31:05 +02001542 int oldval, ret;
1543
1544 if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28)) {
Jiri Slabyda92e8a2017-10-23 13:41:51 +02001545 if (oparg < 0 || oparg > 31) {
1546 char comm[sizeof(current->comm)];
1547 /*
1548 * kill this print and return -EINVAL when userspace
1549 * is sane again
1550 */
1551 pr_info_ratelimited("futex_wake_op: %s tries to shift op by %d; fix this program\n",
1552 get_task_comm(comm, current), oparg);
1553 oparg &= 31;
1554 }
Jiri Slaby81da9f82017-08-24 09:31:05 +02001555 oparg = 1 << oparg;
1556 }
1557
1558 if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
1559 return -EFAULT;
1560
1561 ret = arch_futex_atomic_op_inuser(op, oparg, &oldval, uaddr);
1562 if (ret)
1563 return ret;
1564
1565 switch (cmp) {
1566 case FUTEX_OP_CMP_EQ:
1567 return oldval == cmparg;
1568 case FUTEX_OP_CMP_NE:
1569 return oldval != cmparg;
1570 case FUTEX_OP_CMP_LT:
1571 return oldval < cmparg;
1572 case FUTEX_OP_CMP_GE:
1573 return oldval >= cmparg;
1574 case FUTEX_OP_CMP_LE:
1575 return oldval <= cmparg;
1576 case FUTEX_OP_CMP_GT:
1577 return oldval > cmparg;
1578 default:
1579 return -ENOSYS;
1580 }
1581}
1582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583/*
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001584 * Wake up all waiters hashed on the physical page that is mapped
1585 * to this virtual address:
1586 */
Ingo Molnare2970f22006-06-27 02:54:47 -07001587static int
Darren Hartb41277d2010-11-08 13:10:09 -08001588futex_wake_op(u32 __user *uaddr1, unsigned int flags, u32 __user *uaddr2,
Ingo Molnare2970f22006-06-27 02:54:47 -07001589 int nr_wake, int nr_wake2, int op)
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001590{
Peter Zijlstra38d47c12008-09-26 19:32:20 +02001591 union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
Ingo Molnare2970f22006-06-27 02:54:47 -07001592 struct futex_hash_bucket *hb1, *hb2;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001593 struct futex_q *this, *next;
Darren Harte4dc5b72009-03-12 00:56:13 -07001594 int ret, op_ret;
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001595 WAKE_Q(wake_q);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001596
Darren Harte4dc5b72009-03-12 00:56:13 -07001597retry:
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001598 ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, VERIFY_READ);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001599 if (unlikely(ret != 0))
1600 goto out;
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001601 ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001602 if (unlikely(ret != 0))
Darren Hart42d35d42008-12-29 15:49:53 -08001603 goto out_put_key1;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001604
Ingo Molnare2970f22006-06-27 02:54:47 -07001605 hb1 = hash_futex(&key1);
1606 hb2 = hash_futex(&key2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001607
Darren Harte4dc5b72009-03-12 00:56:13 -07001608retry_private:
Thomas Gleixnereaaea802009-10-04 09:34:17 +02001609 double_lock_hb(hb1, hb2);
Ingo Molnare2970f22006-06-27 02:54:47 -07001610 op_ret = futex_atomic_op_inuser(op, uaddr2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001611 if (unlikely(op_ret < 0)) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001612
Darren Hart5eb3dc62009-03-12 00:55:52 -07001613 double_unlock_hb(hb1, hb2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001614
David Howells7ee1dd32006-01-06 00:11:44 -08001615#ifndef CONFIG_MMU
Ingo Molnare2970f22006-06-27 02:54:47 -07001616 /*
1617 * we don't get EFAULT from MMU faults if we don't have an MMU,
1618 * but we might get them from range checking
1619 */
David Howells7ee1dd32006-01-06 00:11:44 -08001620 ret = op_ret;
Darren Hart42d35d42008-12-29 15:49:53 -08001621 goto out_put_keys;
David Howells7ee1dd32006-01-06 00:11:44 -08001622#endif
1623
David Gibson796f8d92005-11-07 00:59:33 -08001624 if (unlikely(op_ret != -EFAULT)) {
1625 ret = op_ret;
Darren Hart42d35d42008-12-29 15:49:53 -08001626 goto out_put_keys;
David Gibson796f8d92005-11-07 00:59:33 -08001627 }
1628
Thomas Gleixnerd0725992009-06-11 23:15:43 +02001629 ret = fault_in_user_writeable(uaddr2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001630 if (ret)
Darren Hartde87fcc2009-03-12 00:55:46 -07001631 goto out_put_keys;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001632
Darren Hartb41277d2010-11-08 13:10:09 -08001633 if (!(flags & FLAGS_SHARED))
Darren Harte4dc5b72009-03-12 00:56:13 -07001634 goto retry_private;
1635
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001636 put_futex_key(&key2);
1637 put_futex_key(&key1);
Darren Harte4dc5b72009-03-12 00:56:13 -07001638 goto retry;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001639 }
1640
Jason Low0d00c7b2014-01-12 15:31:22 -08001641 plist_for_each_entry_safe(this, next, &hb1->chain, list) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001642 if (match_futex (&this->key, &key1)) {
Darren Hartaa109902012-11-26 16:29:56 -08001643 if (this->pi_state || this->rt_waiter) {
1644 ret = -EINVAL;
1645 goto out_unlock;
1646 }
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001647 mark_wake_futex(&wake_q, this);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001648 if (++ret >= nr_wake)
1649 break;
1650 }
1651 }
1652
1653 if (op_ret > 0) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001654 op_ret = 0;
Jason Low0d00c7b2014-01-12 15:31:22 -08001655 plist_for_each_entry_safe(this, next, &hb2->chain, list) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001656 if (match_futex (&this->key, &key2)) {
Darren Hartaa109902012-11-26 16:29:56 -08001657 if (this->pi_state || this->rt_waiter) {
1658 ret = -EINVAL;
1659 goto out_unlock;
1660 }
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001661 mark_wake_futex(&wake_q, this);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001662 if (++op_ret >= nr_wake2)
1663 break;
1664 }
1665 }
1666 ret += op_ret;
1667 }
1668
Darren Hartaa109902012-11-26 16:29:56 -08001669out_unlock:
Darren Hart5eb3dc62009-03-12 00:55:52 -07001670 double_unlock_hb(hb1, hb2);
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001671 wake_up_q(&wake_q);
Darren Hart42d35d42008-12-29 15:49:53 -08001672out_put_keys:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001673 put_futex_key(&key2);
Darren Hart42d35d42008-12-29 15:49:53 -08001674out_put_key1:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001675 put_futex_key(&key1);
Darren Hart42d35d42008-12-29 15:49:53 -08001676out:
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001677 return ret;
1678}
1679
Darren Hart9121e472009-04-03 13:40:31 -07001680/**
1681 * requeue_futex() - Requeue a futex_q from one hb to another
1682 * @q: the futex_q to requeue
1683 * @hb1: the source hash_bucket
1684 * @hb2: the target hash_bucket
1685 * @key2: the new key for the requeued futex_q
1686 */
1687static inline
1688void requeue_futex(struct futex_q *q, struct futex_hash_bucket *hb1,
1689 struct futex_hash_bucket *hb2, union futex_key *key2)
1690{
1691
1692 /*
1693 * If key1 and key2 hash to the same bucket, no need to
1694 * requeue.
1695 */
1696 if (likely(&hb1->chain != &hb2->chain)) {
1697 plist_del(&q->list, &hb1->chain);
Linus Torvalds11d46162014-03-20 22:11:17 -07001698 hb_waiters_dec(hb1);
Linus Torvalds11d46162014-03-20 22:11:17 -07001699 hb_waiters_inc(hb2);
Davidlohr Buesofe1bce92016-04-20 20:09:24 -07001700 plist_add(&q->list, &hb2->chain);
Darren Hart9121e472009-04-03 13:40:31 -07001701 q->lock_ptr = &hb2->lock;
Darren Hart9121e472009-04-03 13:40:31 -07001702 }
1703 get_futex_key_refs(key2);
1704 q->key = *key2;
1705}
1706
Darren Hart52400ba2009-04-03 13:40:49 -07001707/**
1708 * requeue_pi_wake_futex() - Wake a task that acquired the lock during requeue
Darren Hartd96ee562009-09-21 22:30:22 -07001709 * @q: the futex_q
1710 * @key: the key of the requeue target futex
1711 * @hb: the hash_bucket of the requeue target futex
Darren Hart52400ba2009-04-03 13:40:49 -07001712 *
1713 * During futex_requeue, with requeue_pi=1, it is possible to acquire the
1714 * target futex if it is uncontended or via a lock steal. Set the futex_q key
1715 * to the requeue target futex so the waiter can detect the wakeup on the right
1716 * futex, but remove it from the hb and NULL the rt_waiter so it can detect
Darren Hartbeda2c72009-08-09 15:34:39 -07001717 * atomic lock acquisition. Set the q->lock_ptr to the requeue target hb->lock
1718 * to protect access to the pi_state to fixup the owner later. Must be called
1719 * with both q->lock_ptr and hb->lock held.
Darren Hart52400ba2009-04-03 13:40:49 -07001720 */
1721static inline
Darren Hartbeda2c72009-08-09 15:34:39 -07001722void requeue_pi_wake_futex(struct futex_q *q, union futex_key *key,
1723 struct futex_hash_bucket *hb)
Darren Hart52400ba2009-04-03 13:40:49 -07001724{
Darren Hart52400ba2009-04-03 13:40:49 -07001725 get_futex_key_refs(key);
1726 q->key = *key;
1727
Lai Jiangshan2e129782010-12-22 14:18:50 +08001728 __unqueue_futex(q);
Darren Hart52400ba2009-04-03 13:40:49 -07001729
1730 WARN_ON(!q->rt_waiter);
1731 q->rt_waiter = NULL;
1732
Darren Hartbeda2c72009-08-09 15:34:39 -07001733 q->lock_ptr = &hb->lock;
Darren Hartbeda2c72009-08-09 15:34:39 -07001734
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001735 wake_up_state(q->task, TASK_NORMAL);
Darren Hart52400ba2009-04-03 13:40:49 -07001736}
1737
1738/**
1739 * futex_proxy_trylock_atomic() - Attempt an atomic lock for the top waiter
Darren Hartbab5bc92009-04-07 23:23:50 -07001740 * @pifutex: the user address of the to futex
1741 * @hb1: the from futex hash bucket, must be locked by the caller
1742 * @hb2: the to futex hash bucket, must be locked by the caller
1743 * @key1: the from futex key
1744 * @key2: the to futex key
1745 * @ps: address to store the pi_state pointer
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001746 * @exiting: Pointer to store the task pointer of the owner task
1747 * which is in the middle of exiting
Darren Hartbab5bc92009-04-07 23:23:50 -07001748 * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0)
Darren Hart52400ba2009-04-03 13:40:49 -07001749 *
1750 * Try and get the lock on behalf of the top waiter if we can do it atomically.
Darren Hartbab5bc92009-04-07 23:23:50 -07001751 * Wake the top waiter if we succeed. If the caller specified set_waiters,
1752 * then direct futex_lock_pi_atomic() to force setting the FUTEX_WAITERS bit.
1753 * hb1 and hb2 must be held by the caller.
Darren Hart52400ba2009-04-03 13:40:49 -07001754 *
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001755 * @exiting is only set when the return value is -EBUSY. If so, this holds
1756 * a refcount on the exiting task on return and the caller needs to drop it
1757 * after waiting for the exit to complete.
1758 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08001759 * Return:
1760 * 0 - failed to acquire the lock atomically;
Thomas Gleixner866293e2014-05-12 20:45:34 +00001761 * >0 - acquired the lock, return value is vpid of the top_waiter
Darren Hart52400ba2009-04-03 13:40:49 -07001762 * <0 - error
1763 */
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001764static int
1765futex_proxy_trylock_atomic(u32 __user *pifutex, struct futex_hash_bucket *hb1,
1766 struct futex_hash_bucket *hb2, union futex_key *key1,
1767 union futex_key *key2, struct futex_pi_state **ps,
1768 struct task_struct **exiting, int set_waiters)
Darren Hart52400ba2009-04-03 13:40:49 -07001769{
Darren Hartbab5bc92009-04-07 23:23:50 -07001770 struct futex_q *top_waiter = NULL;
Darren Hart52400ba2009-04-03 13:40:49 -07001771 u32 curval;
Thomas Gleixner866293e2014-05-12 20:45:34 +00001772 int ret, vpid;
Darren Hart52400ba2009-04-03 13:40:49 -07001773
1774 if (get_futex_value_locked(&curval, pifutex))
1775 return -EFAULT;
1776
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001777 if (unlikely(should_fail_futex(true)))
1778 return -EFAULT;
1779
Darren Hartbab5bc92009-04-07 23:23:50 -07001780 /*
1781 * Find the top_waiter and determine if there are additional waiters.
1782 * If the caller intends to requeue more than 1 waiter to pifutex,
1783 * force futex_lock_pi_atomic() to set the FUTEX_WAITERS bit now,
1784 * as we have means to handle the possible fault. If not, don't set
1785 * the bit unecessarily as it will force the subsequent unlock to enter
1786 * the kernel.
1787 */
Darren Hart52400ba2009-04-03 13:40:49 -07001788 top_waiter = futex_top_waiter(hb1, key1);
1789
1790 /* There are no waiters, nothing for us to do. */
1791 if (!top_waiter)
1792 return 0;
1793
Darren Hart84bc4af2009-08-13 17:36:53 -07001794 /* Ensure we requeue to the expected futex. */
1795 if (!match_futex(top_waiter->requeue_pi_key, key2))
1796 return -EINVAL;
1797
Darren Hart52400ba2009-04-03 13:40:49 -07001798 /*
Darren Hartbab5bc92009-04-07 23:23:50 -07001799 * Try to take the lock for top_waiter. Set the FUTEX_WAITERS bit in
1800 * the contended case or if set_waiters is 1. The pi_state is returned
1801 * in ps in contended cases.
Darren Hart52400ba2009-04-03 13:40:49 -07001802 */
Thomas Gleixner866293e2014-05-12 20:45:34 +00001803 vpid = task_pid_vnr(top_waiter->task);
Darren Hartbab5bc92009-04-07 23:23:50 -07001804 ret = futex_lock_pi_atomic(pifutex, hb2, key2, ps, top_waiter->task,
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001805 exiting, set_waiters);
Thomas Gleixner866293e2014-05-12 20:45:34 +00001806 if (ret == 1) {
Darren Hartbeda2c72009-08-09 15:34:39 -07001807 requeue_pi_wake_futex(top_waiter, key2, hb2);
Thomas Gleixner866293e2014-05-12 20:45:34 +00001808 return vpid;
1809 }
Darren Hart52400ba2009-04-03 13:40:49 -07001810 return ret;
1811}
1812
1813/**
1814 * futex_requeue() - Requeue waiters from uaddr1 to uaddr2
Randy Dunlapfb62db22010-10-13 11:02:34 -07001815 * @uaddr1: source futex user address
Darren Hartb41277d2010-11-08 13:10:09 -08001816 * @flags: futex flags (FLAGS_SHARED, etc.)
Randy Dunlapfb62db22010-10-13 11:02:34 -07001817 * @uaddr2: target futex user address
1818 * @nr_wake: number of waiters to wake (must be 1 for requeue_pi)
1819 * @nr_requeue: number of waiters to requeue (0-INT_MAX)
1820 * @cmpval: @uaddr1 expected value (or %NULL)
1821 * @requeue_pi: if we are attempting to requeue from a non-pi futex to a
Darren Hartb41277d2010-11-08 13:10:09 -08001822 * pi futex (pi to pi requeue is not supported)
Darren Hart52400ba2009-04-03 13:40:49 -07001823 *
1824 * Requeue waiters on uaddr1 to uaddr2. In the requeue_pi case, try to acquire
1825 * uaddr2 atomically on behalf of the top waiter.
1826 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08001827 * Return:
1828 * >=0 - on success, the number of tasks requeued or woken;
Darren Hart52400ba2009-04-03 13:40:49 -07001829 * <0 - on error
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 */
Darren Hartb41277d2010-11-08 13:10:09 -08001831static int futex_requeue(u32 __user *uaddr1, unsigned int flags,
1832 u32 __user *uaddr2, int nr_wake, int nr_requeue,
1833 u32 *cmpval, int requeue_pi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
Peter Zijlstra38d47c12008-09-26 19:32:20 +02001835 union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
Darren Hart52400ba2009-04-03 13:40:49 -07001836 int drop_count = 0, task_count = 0, ret;
1837 struct futex_pi_state *pi_state = NULL;
Ingo Molnare2970f22006-06-27 02:54:47 -07001838 struct futex_hash_bucket *hb1, *hb2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 struct futex_q *this, *next;
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001840 WAKE_Q(wake_q);
Darren Hart52400ba2009-04-03 13:40:49 -07001841
Li Jinyued8a31702017-12-14 17:04:54 +08001842 if (nr_wake < 0 || nr_requeue < 0)
1843 return -EINVAL;
1844
Darren Hart52400ba2009-04-03 13:40:49 -07001845 if (requeue_pi) {
1846 /*
Thomas Gleixnere9c243a2014-06-03 12:27:06 +00001847 * Requeue PI only works on two distinct uaddrs. This
1848 * check is only valid for private futexes. See below.
1849 */
1850 if (uaddr1 == uaddr2)
1851 return -EINVAL;
1852
1853 /*
Darren Hart52400ba2009-04-03 13:40:49 -07001854 * requeue_pi requires a pi_state, try to allocate it now
1855 * without any locks in case it fails.
1856 */
1857 if (refill_pi_state_cache())
1858 return -ENOMEM;
1859 /*
1860 * requeue_pi must wake as many tasks as it can, up to nr_wake
1861 * + nr_requeue, since it acquires the rt_mutex prior to
1862 * returning to userspace, so as to not leave the rt_mutex with
1863 * waiters and no owner. However, second and third wake-ups
1864 * cannot be predicted as they involve race conditions with the
1865 * first wake and a fault while looking up the pi_state. Both
1866 * pthread_cond_signal() and pthread_cond_broadcast() should
1867 * use nr_wake=1.
1868 */
1869 if (nr_wake != 1)
1870 return -EINVAL;
1871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Darren Hart42d35d42008-12-29 15:49:53 -08001873retry:
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001874 ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 if (unlikely(ret != 0))
1876 goto out;
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001877 ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2,
1878 requeue_pi ? VERIFY_WRITE : VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 if (unlikely(ret != 0))
Darren Hart42d35d42008-12-29 15:49:53 -08001880 goto out_put_key1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Thomas Gleixnere9c243a2014-06-03 12:27:06 +00001882 /*
1883 * The check above which compares uaddrs is not sufficient for
1884 * shared futexes. We need to compare the keys:
1885 */
1886 if (requeue_pi && match_futex(&key1, &key2)) {
1887 ret = -EINVAL;
1888 goto out_put_keys;
1889 }
1890
Ingo Molnare2970f22006-06-27 02:54:47 -07001891 hb1 = hash_futex(&key1);
1892 hb2 = hash_futex(&key2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Darren Harte4dc5b72009-03-12 00:56:13 -07001894retry_private:
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07001895 hb_waiters_inc(hb2);
Ingo Molnar8b8f3192006-07-03 00:25:05 -07001896 double_lock_hb(hb1, hb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Ingo Molnare2970f22006-06-27 02:54:47 -07001898 if (likely(cmpval != NULL)) {
1899 u32 curval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Ingo Molnare2970f22006-06-27 02:54:47 -07001901 ret = get_futex_value_locked(&curval, uaddr1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
1903 if (unlikely(ret)) {
Darren Hart5eb3dc62009-03-12 00:55:52 -07001904 double_unlock_hb(hb1, hb2);
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07001905 hb_waiters_dec(hb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Darren Harte4dc5b72009-03-12 00:56:13 -07001907 ret = get_user(curval, uaddr1);
1908 if (ret)
1909 goto out_put_keys;
1910
Darren Hartb41277d2010-11-08 13:10:09 -08001911 if (!(flags & FLAGS_SHARED))
Darren Harte4dc5b72009-03-12 00:56:13 -07001912 goto retry_private;
1913
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001914 put_futex_key(&key2);
1915 put_futex_key(&key1);
Darren Harte4dc5b72009-03-12 00:56:13 -07001916 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 }
Ingo Molnare2970f22006-06-27 02:54:47 -07001918 if (curval != *cmpval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 ret = -EAGAIN;
1920 goto out_unlock;
1921 }
1922 }
1923
Darren Hart52400ba2009-04-03 13:40:49 -07001924 if (requeue_pi && (task_count - nr_wake < nr_requeue)) {
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001925 struct task_struct *exiting = NULL;
1926
Darren Hartbab5bc92009-04-07 23:23:50 -07001927 /*
1928 * Attempt to acquire uaddr2 and wake the top waiter. If we
1929 * intend to requeue waiters, force setting the FUTEX_WAITERS
1930 * bit. We force this here where we are able to easily handle
1931 * faults rather in the requeue loop below.
1932 */
Darren Hart52400ba2009-04-03 13:40:49 -07001933 ret = futex_proxy_trylock_atomic(uaddr2, hb1, hb2, &key1,
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001934 &key2, &pi_state,
1935 &exiting, nr_requeue);
Darren Hart52400ba2009-04-03 13:40:49 -07001936
1937 /*
1938 * At this point the top_waiter has either taken uaddr2 or is
1939 * waiting on it. If the former, then the pi_state will not
1940 * exist yet, look it up one more time to ensure we have a
Thomas Gleixner866293e2014-05-12 20:45:34 +00001941 * reference to it. If the lock was taken, ret contains the
1942 * vpid of the top waiter task.
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00001943 * If the lock was not taken, we have pi_state and an initial
1944 * refcount on it. In case of an error we have nothing.
Darren Hart52400ba2009-04-03 13:40:49 -07001945 */
Thomas Gleixner866293e2014-05-12 20:45:34 +00001946 if (ret > 0) {
Darren Hart52400ba2009-04-03 13:40:49 -07001947 WARN_ON(pi_state);
Darren Hart89061d32009-10-15 15:30:48 -07001948 drop_count++;
Darren Hart52400ba2009-04-03 13:40:49 -07001949 task_count++;
Thomas Gleixner866293e2014-05-12 20:45:34 +00001950 /*
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00001951 * If we acquired the lock, then the user space value
1952 * of uaddr2 should be vpid. It cannot be changed by
1953 * the top waiter as it is blocked on hb2 lock if it
1954 * tries to do so. If something fiddled with it behind
1955 * our back the pi state lookup might unearth it. So
1956 * we rather use the known value than rereading and
1957 * handing potential crap to lookup_pi_state.
1958 *
1959 * If that call succeeds then we have pi_state and an
1960 * initial refcount on it.
Thomas Gleixner866293e2014-05-12 20:45:34 +00001961 */
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001962 ret = lookup_pi_state(ret, hb2, &key2,
1963 &pi_state, &exiting);
Darren Hart52400ba2009-04-03 13:40:49 -07001964 }
1965
1966 switch (ret) {
1967 case 0:
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00001968 /* We hold a reference on the pi state. */
Darren Hart52400ba2009-04-03 13:40:49 -07001969 break;
Thomas Gleixner4959f2d2015-12-19 20:07:40 +00001970
1971 /* If the above failed, then pi_state is NULL */
Darren Hart52400ba2009-04-03 13:40:49 -07001972 case -EFAULT:
1973 double_unlock_hb(hb1, hb2);
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07001974 hb_waiters_dec(hb2);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001975 put_futex_key(&key2);
1976 put_futex_key(&key1);
Thomas Gleixnerd0725992009-06-11 23:15:43 +02001977 ret = fault_in_user_writeable(uaddr2);
Darren Hart52400ba2009-04-03 13:40:49 -07001978 if (!ret)
1979 goto retry;
1980 goto out;
Thomas Gleixnerc27f3922021-02-01 10:01:42 +00001981 case -EBUSY:
Darren Hart52400ba2009-04-03 13:40:49 -07001982 case -EAGAIN:
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001983 /*
1984 * Two reasons for this:
Thomas Gleixnerc27f3922021-02-01 10:01:42 +00001985 * - EBUSY: Owner is exiting and we just wait for the
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001986 * exit to complete.
Thomas Gleixnerc27f3922021-02-01 10:01:42 +00001987 * - EAGAIN: The user space value changed.
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001988 */
Darren Hart52400ba2009-04-03 13:40:49 -07001989 double_unlock_hb(hb1, hb2);
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07001990 hb_waiters_dec(hb2);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001991 put_futex_key(&key2);
1992 put_futex_key(&key1);
Thomas Gleixnercf16e422021-02-01 10:01:43 +00001993 /*
1994 * Handle the case where the owner is in the middle of
1995 * exiting. Wait for the exit to complete otherwise
1996 * this task might loop forever, aka. live lock.
1997 */
1998 wait_for_owner_exiting(ret, exiting);
Darren Hart52400ba2009-04-03 13:40:49 -07001999 cond_resched();
2000 goto retry;
2001 default:
2002 goto out_unlock;
2003 }
2004 }
2005
Jason Low0d00c7b2014-01-12 15:31:22 -08002006 plist_for_each_entry_safe(this, next, &hb1->chain, list) {
Darren Hart52400ba2009-04-03 13:40:49 -07002007 if (task_count - nr_wake >= nr_requeue)
2008 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Darren Hart52400ba2009-04-03 13:40:49 -07002010 if (!match_futex(&this->key, &key1))
2011 continue;
2012
Darren Hart392741e2009-08-07 15:20:48 -07002013 /*
2014 * FUTEX_WAIT_REQEUE_PI and FUTEX_CMP_REQUEUE_PI should always
2015 * be paired with each other and no other futex ops.
Darren Hartaa109902012-11-26 16:29:56 -08002016 *
2017 * We should never be requeueing a futex_q with a pi_state,
2018 * which is awaiting a futex_unlock_pi().
Darren Hart392741e2009-08-07 15:20:48 -07002019 */
2020 if ((requeue_pi && !this->rt_waiter) ||
Darren Hartaa109902012-11-26 16:29:56 -08002021 (!requeue_pi && this->rt_waiter) ||
2022 this->pi_state) {
Darren Hart392741e2009-08-07 15:20:48 -07002023 ret = -EINVAL;
2024 break;
2025 }
Darren Hart52400ba2009-04-03 13:40:49 -07002026
2027 /*
2028 * Wake nr_wake waiters. For requeue_pi, if we acquired the
2029 * lock, we already woke the top_waiter. If not, it will be
2030 * woken by futex_unlock_pi().
2031 */
2032 if (++task_count <= nr_wake && !requeue_pi) {
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07002033 mark_wake_futex(&wake_q, this);
Darren Hart52400ba2009-04-03 13:40:49 -07002034 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 }
Darren Hart52400ba2009-04-03 13:40:49 -07002036
Darren Hart84bc4af2009-08-13 17:36:53 -07002037 /* Ensure we requeue to the expected futex for requeue_pi. */
2038 if (requeue_pi && !match_futex(this->requeue_pi_key, &key2)) {
2039 ret = -EINVAL;
2040 break;
2041 }
2042
Darren Hart52400ba2009-04-03 13:40:49 -07002043 /*
2044 * Requeue nr_requeue waiters and possibly one more in the case
2045 * of requeue_pi if we couldn't acquire the lock atomically.
2046 */
2047 if (requeue_pi) {
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00002048 /*
2049 * Prepare the waiter to take the rt_mutex. Take a
2050 * refcount on the pi_state and store the pointer in
2051 * the futex_q object of the waiter.
2052 */
Darren Hart52400ba2009-04-03 13:40:49 -07002053 atomic_inc(&pi_state->refcount);
2054 this->pi_state = pi_state;
2055 ret = rt_mutex_start_proxy_lock(&pi_state->pi_mutex,
2056 this->rt_waiter,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00002057 this->task);
Darren Hart52400ba2009-04-03 13:40:49 -07002058 if (ret == 1) {
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00002059 /*
2060 * We got the lock. We do neither drop the
2061 * refcount on pi_state nor clear
2062 * this->pi_state because the waiter needs the
2063 * pi_state for cleaning up the user space
2064 * value. It will drop the refcount after
2065 * doing so.
2066 */
Darren Hartbeda2c72009-08-09 15:34:39 -07002067 requeue_pi_wake_futex(this, &key2, hb2);
Darren Hart89061d32009-10-15 15:30:48 -07002068 drop_count++;
Darren Hart52400ba2009-04-03 13:40:49 -07002069 continue;
2070 } else if (ret) {
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00002071 /*
2072 * rt_mutex_start_proxy_lock() detected a
2073 * potential deadlock when we tried to queue
2074 * that waiter. Drop the pi_state reference
2075 * which we took above and remove the pointer
2076 * to the state from the waiters futex_q
2077 * object.
2078 */
Darren Hart52400ba2009-04-03 13:40:49 -07002079 this->pi_state = NULL;
Thomas Gleixner29e9ee52015-12-19 20:07:39 +00002080 put_pi_state(pi_state);
Thomas Gleixner885c2cb2015-12-19 20:07:41 +00002081 /*
2082 * We stop queueing more waiters and let user
2083 * space deal with the mess.
2084 */
2085 break;
Darren Hart52400ba2009-04-03 13:40:49 -07002086 }
2087 }
2088 requeue_futex(this, hb1, hb2, &key2);
2089 drop_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 }
2091
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00002092 /*
2093 * We took an extra initial reference to the pi_state either
2094 * in futex_proxy_trylock_atomic() or in lookup_pi_state(). We
2095 * need to drop it here again.
2096 */
Thomas Gleixner29e9ee52015-12-19 20:07:39 +00002097 put_pi_state(pi_state);
Thomas Gleixner885c2cb2015-12-19 20:07:41 +00002098
2099out_unlock:
Darren Hart5eb3dc62009-03-12 00:55:52 -07002100 double_unlock_hb(hb1, hb2);
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07002101 wake_up_q(&wake_q);
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07002102 hb_waiters_dec(hb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Darren Hartcd84a422009-04-02 14:19:38 -07002104 /*
2105 * drop_futex_key_refs() must be called outside the spinlocks. During
2106 * the requeue we moved futex_q's from the hash bucket at key1 to the
2107 * one at key2 and updated their key pointer. We no longer need to
2108 * hold the references to key1.
2109 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 while (--drop_count >= 0)
Rusty Russell9adef582007-05-08 00:26:42 -07002111 drop_futex_key_refs(&key1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Darren Hart42d35d42008-12-29 15:49:53 -08002113out_put_keys:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002114 put_futex_key(&key2);
Darren Hart42d35d42008-12-29 15:49:53 -08002115out_put_key1:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002116 put_futex_key(&key1);
Darren Hart42d35d42008-12-29 15:49:53 -08002117out:
Darren Hart52400ba2009-04-03 13:40:49 -07002118 return ret ? ret : task_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119}
2120
2121/* The key must be already stored in q->key. */
Eric Sesterhenn82af7ac2008-01-25 10:40:46 +01002122static inline struct futex_hash_bucket *queue_lock(struct futex_q *q)
Namhyung Kim15e408c2010-09-14 21:43:48 +09002123 __acquires(&hb->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
Ingo Molnare2970f22006-06-27 02:54:47 -07002125 struct futex_hash_bucket *hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Ingo Molnare2970f22006-06-27 02:54:47 -07002127 hb = hash_futex(&q->key);
Linus Torvalds11d46162014-03-20 22:11:17 -07002128
2129 /*
2130 * Increment the counter before taking the lock so that
2131 * a potential waker won't miss a to-be-slept task that is
2132 * waiting for the spinlock. This is safe as all queue_lock()
2133 * users end up calling queue_me(). Similarly, for housekeeping,
2134 * decrement the counter at queue_unlock() when some error has
2135 * occurred and we don't end up adding the task to the list.
2136 */
2137 hb_waiters_inc(hb);
2138
Ingo Molnare2970f22006-06-27 02:54:47 -07002139 q->lock_ptr = &hb->lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Davidlohr Bueso8ad7b372016-02-09 11:15:13 -08002141 spin_lock(&hb->lock); /* implies smp_mb(); (A) */
Ingo Molnare2970f22006-06-27 02:54:47 -07002142 return hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}
2144
Darren Hartd40d65c2009-09-21 22:30:15 -07002145static inline void
Jason Low0d00c7b2014-01-12 15:31:22 -08002146queue_unlock(struct futex_hash_bucket *hb)
Namhyung Kim15e408c2010-09-14 21:43:48 +09002147 __releases(&hb->lock)
Darren Hartd40d65c2009-09-21 22:30:15 -07002148{
2149 spin_unlock(&hb->lock);
Linus Torvalds11d46162014-03-20 22:11:17 -07002150 hb_waiters_dec(hb);
Darren Hartd40d65c2009-09-21 22:30:15 -07002151}
2152
2153/**
2154 * queue_me() - Enqueue the futex_q on the futex_hash_bucket
2155 * @q: The futex_q to enqueue
2156 * @hb: The destination hash bucket
2157 *
2158 * The hb->lock must be held by the caller, and is released here. A call to
2159 * queue_me() is typically paired with exactly one call to unqueue_me(). The
2160 * exceptions involve the PI related operations, which may use unqueue_me_pi()
2161 * or nothing if the unqueue is done as part of the wake process and the unqueue
2162 * state is implicit in the state of woken task (see futex_wait_requeue_pi() for
2163 * an example).
2164 */
Eric Sesterhenn82af7ac2008-01-25 10:40:46 +01002165static inline void queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
Namhyung Kim15e408c2010-09-14 21:43:48 +09002166 __releases(&hb->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167{
Pierre Peifferec92d082007-05-09 02:35:00 -07002168 int prio;
2169
2170 /*
2171 * The priority used to register this element is
2172 * - either the real thread-priority for the real-time threads
2173 * (i.e. threads with a priority lower than MAX_RT_PRIO)
2174 * - or MAX_RT_PRIO for non-RT threads.
2175 * Thus, all RT-threads are woken first in priority order, and
2176 * the others are woken last, in FIFO order.
2177 */
2178 prio = min(current->normal_prio, MAX_RT_PRIO);
2179
2180 plist_node_init(&q->list, prio);
Pierre Peifferec92d082007-05-09 02:35:00 -07002181 plist_add(&q->list, &hb->chain);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002182 q->task = current;
Ingo Molnare2970f22006-06-27 02:54:47 -07002183 spin_unlock(&hb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184}
2185
Darren Hartd40d65c2009-09-21 22:30:15 -07002186/**
2187 * unqueue_me() - Remove the futex_q from its futex_hash_bucket
2188 * @q: The futex_q to unqueue
2189 *
2190 * The q->lock_ptr must not be held by the caller. A call to unqueue_me() must
2191 * be paired with exactly one earlier call to queue_me().
2192 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002193 * Return:
2194 * 1 - if the futex_q was still queued (and we removed unqueued it);
Darren Hartd40d65c2009-09-21 22:30:15 -07002195 * 0 - if the futex_q was already removed by the waking thread
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197static int unqueue_me(struct futex_q *q)
2198{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 spinlock_t *lock_ptr;
Ingo Molnare2970f22006-06-27 02:54:47 -07002200 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
2202 /* In the common case we don't take the spinlock, which is nice. */
Darren Hart42d35d42008-12-29 15:49:53 -08002203retry:
Jianyu Zhan29b75eb2016-03-07 09:32:24 +08002204 /*
2205 * q->lock_ptr can change between this read and the following spin_lock.
2206 * Use READ_ONCE to forbid the compiler from reloading q->lock_ptr and
2207 * optimizing lock_ptr out of the logic below.
2208 */
2209 lock_ptr = READ_ONCE(q->lock_ptr);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07002210 if (lock_ptr != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 spin_lock(lock_ptr);
2212 /*
2213 * q->lock_ptr can change between reading it and
2214 * spin_lock(), causing us to take the wrong lock. This
2215 * corrects the race condition.
2216 *
2217 * Reasoning goes like this: if we have the wrong lock,
2218 * q->lock_ptr must have changed (maybe several times)
2219 * between reading it and the spin_lock(). It can
2220 * change again after the spin_lock() but only if it was
2221 * already changed before the spin_lock(). It cannot,
2222 * however, change back to the original value. Therefore
2223 * we can detect whether we acquired the correct lock.
2224 */
2225 if (unlikely(lock_ptr != q->lock_ptr)) {
2226 spin_unlock(lock_ptr);
2227 goto retry;
2228 }
Lai Jiangshan2e129782010-12-22 14:18:50 +08002229 __unqueue_futex(q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002230
2231 BUG_ON(q->pi_state);
2232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 spin_unlock(lock_ptr);
2234 ret = 1;
2235 }
2236
Rusty Russell9adef582007-05-08 00:26:42 -07002237 drop_futex_key_refs(&q->key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 return ret;
2239}
2240
Ingo Molnarc87e2832006-06-27 02:54:58 -07002241/*
2242 * PI futexes can not be requeued and must remove themself from the
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002243 * hash bucket. The hash bucket lock (i.e. lock_ptr) is held on entry
2244 * and dropped here.
Ingo Molnarc87e2832006-06-27 02:54:58 -07002245 */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002246static void unqueue_me_pi(struct futex_q *q)
Namhyung Kim15e408c2010-09-14 21:43:48 +09002247 __releases(q->lock_ptr)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002248{
Lai Jiangshan2e129782010-12-22 14:18:50 +08002249 __unqueue_futex(q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002250
2251 BUG_ON(!q->pi_state);
Thomas Gleixner29e9ee52015-12-19 20:07:39 +00002252 put_pi_state(q->pi_state);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002253 q->pi_state = NULL;
2254
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002255 spin_unlock(q->lock_ptr);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002256}
2257
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002258/*
Thomas Gleixnercdf71a12008-01-08 19:47:38 +01002259 * Fixup the pi_state owner with the new owner.
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002260 *
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002261 * Must be called with hash bucket lock held and mm->sem held for non
2262 * private futexes.
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002263 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002264static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002265 struct task_struct *newowner)
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002266{
Thomas Gleixnercdf71a12008-01-08 19:47:38 +01002267 u32 newtid = task_pid_vnr(newowner) | FUTEX_WAITERS;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002268 struct futex_pi_state *pi_state = q->pi_state;
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002269 struct task_struct *oldowner = pi_state->owner;
Vitaliy Ivanov7cfdaf32011-07-07 15:10:31 +03002270 u32 uval, uninitialized_var(curval), newval;
Darren Harte4dc5b72009-03-12 00:56:13 -07002271 int ret;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002272
2273 /* Owner died? */
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002274 if (!pi_state->owner)
2275 newtid |= FUTEX_OWNER_DIED;
2276
2277 /*
2278 * We are here either because we stole the rtmutex from the
Lai Jiangshan81612392011-01-14 17:09:41 +08002279 * previous highest priority waiter or we are the highest priority
2280 * waiter but failed to get the rtmutex the first time.
2281 * We have to replace the newowner TID in the user space variable.
2282 * This must be atomic as we have to preserve the owner died bit here.
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002283 *
Darren Hartb2d09942009-03-12 00:55:37 -07002284 * Note: We write the user space value _before_ changing the pi_state
2285 * because we can fault here. Imagine swapped out pages or a fork
2286 * that marked all the anonymous memory readonly for cow.
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002287 *
2288 * Modifying pi_state _before_ the user space value would
2289 * leave the pi_state in an inconsistent state when we fault
2290 * here, because we need to drop the hash bucket lock to
2291 * handle the fault. This might be observed in the PID check
2292 * in lookup_pi_state.
2293 */
2294retry:
2295 if (get_futex_value_locked(&uval, uaddr))
2296 goto handle_fault;
2297
2298 while (1) {
2299 newval = (uval & FUTEX_OWNER_DIED) | newtid;
2300
Michel Lespinasse37a9d912011-03-10 18:48:51 -08002301 if (cmpxchg_futex_value_locked(&curval, uaddr, uval, newval))
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002302 goto handle_fault;
2303 if (curval == uval)
2304 break;
2305 uval = curval;
2306 }
2307
2308 /*
2309 * We fixed up user space. Now we need to fix the pi_state
2310 * itself.
2311 */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002312 if (pi_state->owner != NULL) {
Thomas Gleixner1d615482009-11-17 14:54:03 +01002313 raw_spin_lock_irq(&pi_state->owner->pi_lock);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002314 WARN_ON(list_empty(&pi_state->list));
2315 list_del_init(&pi_state->list);
Thomas Gleixner1d615482009-11-17 14:54:03 +01002316 raw_spin_unlock_irq(&pi_state->owner->pi_lock);
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002317 }
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002318
Thomas Gleixnercdf71a12008-01-08 19:47:38 +01002319 pi_state->owner = newowner;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002320
Thomas Gleixner1d615482009-11-17 14:54:03 +01002321 raw_spin_lock_irq(&newowner->pi_lock);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002322 WARN_ON(!list_empty(&pi_state->list));
Thomas Gleixnercdf71a12008-01-08 19:47:38 +01002323 list_add(&pi_state->list, &newowner->pi_state_list);
Thomas Gleixner1d615482009-11-17 14:54:03 +01002324 raw_spin_unlock_irq(&newowner->pi_lock);
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002325 return 0;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002326
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002327 /*
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002328 * To handle the page fault we need to drop the hash bucket
Lai Jiangshan81612392011-01-14 17:09:41 +08002329 * lock here. That gives the other task (either the highest priority
2330 * waiter itself or the task which stole the rtmutex) the
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002331 * chance to try the fixup of the pi_state. So once we are
2332 * back from handling the fault we need to check the pi_state
2333 * after reacquiring the hash bucket lock and before trying to
2334 * do another fixup. When the fixup has been done already we
2335 * simply return.
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002336 */
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002337handle_fault:
2338 spin_unlock(q->lock_ptr);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002339
Thomas Gleixnerd0725992009-06-11 23:15:43 +02002340 ret = fault_in_user_writeable(uaddr);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002341
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002342 spin_lock(q->lock_ptr);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002343
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002344 /*
2345 * Check if someone else fixed it for us:
2346 */
2347 if (pi_state->owner != oldowner)
2348 return 0;
2349
2350 if (ret)
2351 return ret;
2352
2353 goto retry;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002354}
2355
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002356static long futex_wait_restart(struct restart_block *restart);
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07002357
Darren Hartca5f9522009-04-03 13:39:33 -07002358/**
Darren Hartdd973992009-04-03 13:40:02 -07002359 * fixup_owner() - Post lock pi_state and corner case management
2360 * @uaddr: user address of the futex
Darren Hartdd973992009-04-03 13:40:02 -07002361 * @q: futex_q (contains pi_state and access to the rt_mutex)
2362 * @locked: if the attempt to take the rt_mutex succeeded (1) or not (0)
2363 *
2364 * After attempting to lock an rt_mutex, this function is called to cleanup
2365 * the pi_state owner as well as handle race conditions that may allow us to
2366 * acquire the lock. Must be called with the hb lock held.
2367 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002368 * Return:
2369 * 1 - success, lock taken;
2370 * 0 - success, lock not taken;
Darren Hartdd973992009-04-03 13:40:02 -07002371 * <0 - on error (-EFAULT)
2372 */
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002373static int fixup_owner(u32 __user *uaddr, struct futex_q *q, int locked)
Darren Hartdd973992009-04-03 13:40:02 -07002374{
2375 struct task_struct *owner;
2376 int ret = 0;
2377
2378 if (locked) {
2379 /*
2380 * Got the lock. We might not be the anticipated owner if we
2381 * did a lock-steal - fix up the PI-state in that case:
2382 */
2383 if (q->pi_state->owner != current)
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002384 ret = fixup_pi_state_owner(uaddr, q, current);
Darren Hartdd973992009-04-03 13:40:02 -07002385 goto out;
2386 }
2387
2388 /*
2389 * Catch the rare case, where the lock was released when we were on the
2390 * way back before we locked the hash bucket.
2391 */
2392 if (q->pi_state->owner == current) {
2393 /*
2394 * Try to get the rt_mutex now. This might fail as some other
2395 * task acquired the rt_mutex after we removed ourself from the
2396 * rt_mutex waiters list.
2397 */
Peter Zijlstra2c60d4a2021-02-03 13:45:30 +00002398 if (rt_mutex_futex_trylock(&q->pi_state->pi_mutex)) {
Darren Hartdd973992009-04-03 13:40:02 -07002399 locked = 1;
2400 goto out;
2401 }
2402
2403 /*
2404 * pi_state is incorrect, some other task did a lock steal and
2405 * we returned due to timeout or signal without taking the
Lai Jiangshan81612392011-01-14 17:09:41 +08002406 * rt_mutex. Too late.
Darren Hartdd973992009-04-03 13:40:02 -07002407 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01002408 raw_spin_lock_irq(&q->pi_state->pi_mutex.wait_lock);
Darren Hartdd973992009-04-03 13:40:02 -07002409 owner = rt_mutex_owner(&q->pi_state->pi_mutex);
Lai Jiangshan81612392011-01-14 17:09:41 +08002410 if (!owner)
2411 owner = rt_mutex_next_owner(&q->pi_state->pi_mutex);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01002412 raw_spin_unlock_irq(&q->pi_state->pi_mutex.wait_lock);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002413 ret = fixup_pi_state_owner(uaddr, q, owner);
Darren Hartdd973992009-04-03 13:40:02 -07002414 goto out;
2415 }
2416
2417 /*
2418 * Paranoia check. If we did not take the lock, then we should not be
Lai Jiangshan81612392011-01-14 17:09:41 +08002419 * the owner of the rt_mutex.
Darren Hartdd973992009-04-03 13:40:02 -07002420 */
2421 if (rt_mutex_owner(&q->pi_state->pi_mutex) == current)
2422 printk(KERN_ERR "fixup_owner: ret = %d pi-mutex: %p "
2423 "pi-state %p\n", ret,
2424 q->pi_state->pi_mutex.owner,
2425 q->pi_state->owner);
2426
2427out:
2428 return ret ? ret : locked;
2429}
2430
2431/**
Darren Hartca5f9522009-04-03 13:39:33 -07002432 * futex_wait_queue_me() - queue_me() and wait for wakeup, timeout, or signal
2433 * @hb: the futex hash bucket, must be locked by the caller
2434 * @q: the futex_q to queue up on
2435 * @timeout: the prepared hrtimer_sleeper, or null for no timeout
Darren Hartca5f9522009-04-03 13:39:33 -07002436 */
2437static void futex_wait_queue_me(struct futex_hash_bucket *hb, struct futex_q *q,
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02002438 struct hrtimer_sleeper *timeout)
Darren Hartca5f9522009-04-03 13:39:33 -07002439{
Darren Hart9beba3c2009-09-24 11:54:47 -07002440 /*
2441 * The task state is guaranteed to be set before another task can
Peter Zijlstrab92b8b32015-05-12 10:51:55 +02002442 * wake it. set_current_state() is implemented using smp_store_mb() and
Darren Hart9beba3c2009-09-24 11:54:47 -07002443 * queue_me() calls spin_unlock() upon completion, both serializing
2444 * access to the hash list and forcing another memory barrier.
2445 */
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02002446 set_current_state(TASK_INTERRUPTIBLE);
Darren Hart0729e192009-09-21 22:30:38 -07002447 queue_me(q, hb);
Darren Hartca5f9522009-04-03 13:39:33 -07002448
2449 /* Arm the timer */
Thomas Gleixner2e4b0d32015-04-14 21:09:13 +00002450 if (timeout)
Darren Hartca5f9522009-04-03 13:39:33 -07002451 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
Darren Hartca5f9522009-04-03 13:39:33 -07002452
2453 /*
Darren Hart0729e192009-09-21 22:30:38 -07002454 * If we have been removed from the hash list, then another task
2455 * has tried to wake us, and we can skip the call to schedule().
Darren Hartca5f9522009-04-03 13:39:33 -07002456 */
2457 if (likely(!plist_node_empty(&q->list))) {
2458 /*
2459 * If the timer has already expired, current will already be
2460 * flagged for rescheduling. Only call schedule if there
2461 * is no timeout, or if it has yet to expire.
2462 */
2463 if (!timeout || timeout->task)
Colin Cross88c80042013-05-01 18:35:05 -07002464 freezable_schedule();
Darren Hartca5f9522009-04-03 13:39:33 -07002465 }
2466 __set_current_state(TASK_RUNNING);
2467}
2468
Darren Hartf8010732009-04-03 13:40:40 -07002469/**
2470 * futex_wait_setup() - Prepare to wait on a futex
2471 * @uaddr: the futex userspace address
2472 * @val: the expected value
Darren Hartb41277d2010-11-08 13:10:09 -08002473 * @flags: futex flags (FLAGS_SHARED, etc.)
Darren Hartf8010732009-04-03 13:40:40 -07002474 * @q: the associated futex_q
2475 * @hb: storage for hash_bucket pointer to be returned to caller
2476 *
2477 * Setup the futex_q and locate the hash_bucket. Get the futex value and
2478 * compare it with the expected value. Handle atomic faults internally.
2479 * Return with the hb lock held and a q.key reference on success, and unlocked
2480 * with no q.key reference on failure.
2481 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002482 * Return:
2483 * 0 - uaddr contains val and hb has been locked;
Bart Van Asscheca4a04c2011-07-17 09:01:00 +02002484 * <1 - -EFAULT or -EWOULDBLOCK (uaddr does not contain val) and hb is unlocked
Darren Hartf8010732009-04-03 13:40:40 -07002485 */
Darren Hartb41277d2010-11-08 13:10:09 -08002486static int futex_wait_setup(u32 __user *uaddr, u32 val, unsigned int flags,
Darren Hartf8010732009-04-03 13:40:40 -07002487 struct futex_q *q, struct futex_hash_bucket **hb)
2488{
2489 u32 uval;
2490 int ret;
2491
2492 /*
2493 * Access the page AFTER the hash-bucket is locked.
2494 * Order is important:
2495 *
2496 * Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val);
2497 * Userspace waker: if (cond(var)) { var = new; futex_wake(&var); }
2498 *
2499 * The basic logical guarantee of a futex is that it blocks ONLY
2500 * if cond(var) is known to be true at the time of blocking, for
Michel Lespinasse8fe8f542011-03-06 18:07:50 -08002501 * any cond. If we locked the hash-bucket after testing *uaddr, that
2502 * would open a race condition where we could block indefinitely with
Darren Hartf8010732009-04-03 13:40:40 -07002503 * cond(var) false, which would violate the guarantee.
2504 *
Michel Lespinasse8fe8f542011-03-06 18:07:50 -08002505 * On the other hand, we insert q and release the hash-bucket only
2506 * after testing *uaddr. This guarantees that futex_wait() will NOT
2507 * absorb a wakeup if *uaddr does not match the desired values
2508 * while the syscall executes.
Darren Hartf8010732009-04-03 13:40:40 -07002509 */
2510retry:
Shawn Bohrer9ea71502011-06-30 11:21:32 -05002511 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q->key, VERIFY_READ);
Darren Hartf8010732009-04-03 13:40:40 -07002512 if (unlikely(ret != 0))
Darren Harta5a2a0c2009-04-10 09:50:05 -07002513 return ret;
Darren Hartf8010732009-04-03 13:40:40 -07002514
2515retry_private:
2516 *hb = queue_lock(q);
2517
2518 ret = get_futex_value_locked(&uval, uaddr);
2519
2520 if (ret) {
Jason Low0d00c7b2014-01-12 15:31:22 -08002521 queue_unlock(*hb);
Darren Hartf8010732009-04-03 13:40:40 -07002522
2523 ret = get_user(uval, uaddr);
2524 if (ret)
2525 goto out;
2526
Darren Hartb41277d2010-11-08 13:10:09 -08002527 if (!(flags & FLAGS_SHARED))
Darren Hartf8010732009-04-03 13:40:40 -07002528 goto retry_private;
2529
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002530 put_futex_key(&q->key);
Darren Hartf8010732009-04-03 13:40:40 -07002531 goto retry;
2532 }
2533
2534 if (uval != val) {
Jason Low0d00c7b2014-01-12 15:31:22 -08002535 queue_unlock(*hb);
Darren Hartf8010732009-04-03 13:40:40 -07002536 ret = -EWOULDBLOCK;
2537 }
2538
2539out:
2540 if (ret)
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002541 put_futex_key(&q->key);
Darren Hartf8010732009-04-03 13:40:40 -07002542 return ret;
2543}
2544
Darren Hartb41277d2010-11-08 13:10:09 -08002545static int futex_wait(u32 __user *uaddr, unsigned int flags, u32 val,
2546 ktime_t *abs_time, u32 bitset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547{
Darren Hartca5f9522009-04-03 13:39:33 -07002548 struct hrtimer_sleeper timeout, *to = NULL;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002549 struct restart_block *restart;
Ingo Molnare2970f22006-06-27 02:54:47 -07002550 struct futex_hash_bucket *hb;
Darren Hart5bdb05f2010-11-08 13:40:28 -08002551 struct futex_q q = futex_q_init;
Ingo Molnare2970f22006-06-27 02:54:47 -07002552 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
Thomas Gleixnercd689982008-02-01 17:45:14 +01002554 if (!bitset)
2555 return -EINVAL;
Thomas Gleixnercd689982008-02-01 17:45:14 +01002556 q.bitset = bitset;
Darren Hartca5f9522009-04-03 13:39:33 -07002557
2558 if (abs_time) {
2559 to = &timeout;
2560
Darren Hartb41277d2010-11-08 13:10:09 -08002561 hrtimer_init_on_stack(&to->timer, (flags & FLAGS_CLOCKRT) ?
2562 CLOCK_REALTIME : CLOCK_MONOTONIC,
2563 HRTIMER_MODE_ABS);
Darren Hartca5f9522009-04-03 13:39:33 -07002564 hrtimer_init_sleeper(to, current);
2565 hrtimer_set_expires_range_ns(&to->timer, *abs_time,
2566 current->timer_slack_ns);
2567 }
2568
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002569retry:
Darren Hart7ada8762010-10-17 08:35:04 -07002570 /*
2571 * Prepare to wait on uaddr. On success, holds hb lock and increments
2572 * q.key refs.
2573 */
Darren Hartb41277d2010-11-08 13:10:09 -08002574 ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
Darren Hartf8010732009-04-03 13:40:40 -07002575 if (ret)
Darren Hart42d35d42008-12-29 15:49:53 -08002576 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
Darren Hartca5f9522009-04-03 13:39:33 -07002578 /* queue_me and wait for wakeup, timeout, or a signal. */
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02002579 futex_wait_queue_me(hb, &q, to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
2581 /* If we were woken (and unqueued), we succeeded, whatever. */
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002582 ret = 0;
Darren Hart7ada8762010-10-17 08:35:04 -07002583 /* unqueue_me() drops q.key ref */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 if (!unqueue_me(&q))
Darren Hart7ada8762010-10-17 08:35:04 -07002585 goto out;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002586 ret = -ETIMEDOUT;
Darren Hartca5f9522009-04-03 13:39:33 -07002587 if (to && !to->task)
Darren Hart7ada8762010-10-17 08:35:04 -07002588 goto out;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002589
Ingo Molnare2970f22006-06-27 02:54:47 -07002590 /*
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002591 * We expect signal_pending(current), but we might be the
2592 * victim of a spurious wakeup as well.
Ingo Molnare2970f22006-06-27 02:54:47 -07002593 */
Darren Hart7ada8762010-10-17 08:35:04 -07002594 if (!signal_pending(current))
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002595 goto retry;
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002596
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002597 ret = -ERESTARTSYS;
Pierre Peifferc19384b2007-05-09 02:35:02 -07002598 if (!abs_time)
Darren Hart7ada8762010-10-17 08:35:04 -07002599 goto out;
Steven Rostedtce6bd422007-12-05 15:46:09 +01002600
Andy Lutomirskif56141e2015-02-12 15:01:14 -08002601 restart = &current->restart_block;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002602 restart->fn = futex_wait_restart;
Namhyung Kima3c74c52010-09-14 21:43:47 +09002603 restart->futex.uaddr = uaddr;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002604 restart->futex.val = val;
2605 restart->futex.time = abs_time->tv64;
2606 restart->futex.bitset = bitset;
Darren Hart0cd9c642011-04-14 15:41:57 -07002607 restart->futex.flags = flags | FLAGS_HAS_TIMEOUT;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002608
2609 ret = -ERESTART_RESTARTBLOCK;
2610
Darren Hart42d35d42008-12-29 15:49:53 -08002611out:
Darren Hartca5f9522009-04-03 13:39:33 -07002612 if (to) {
2613 hrtimer_cancel(&to->timer);
2614 destroy_hrtimer_on_stack(&to->timer);
2615 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07002616 return ret;
2617}
2618
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002619
2620static long futex_wait_restart(struct restart_block *restart)
2621{
Namhyung Kima3c74c52010-09-14 21:43:47 +09002622 u32 __user *uaddr = restart->futex.uaddr;
Darren Harta72188d2009-04-03 13:40:22 -07002623 ktime_t t, *tp = NULL;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002624
Darren Harta72188d2009-04-03 13:40:22 -07002625 if (restart->futex.flags & FLAGS_HAS_TIMEOUT) {
2626 t.tv64 = restart->futex.time;
2627 tp = &t;
2628 }
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002629 restart->fn = do_no_restart_syscall;
Darren Hartb41277d2010-11-08 13:10:09 -08002630
2631 return (long)futex_wait(uaddr, restart->futex.flags,
2632 restart->futex.val, tp, restart->futex.bitset);
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002633}
2634
2635
Ingo Molnarc87e2832006-06-27 02:54:58 -07002636/*
2637 * Userspace tried a 0 -> TID atomic transition of the futex value
2638 * and failed. The kernel side here does the whole locking operation:
Davidlohr Bueso767f5092015-06-29 23:26:01 -07002639 * if there are waiters then it will block as a consequence of relying
2640 * on rt-mutexes, it does PI, etc. (Due to races the kernel might see
2641 * a 0 value of the futex too.).
2642 *
2643 * Also serves as futex trylock_pi()'ing, and due semantics.
Ingo Molnarc87e2832006-06-27 02:54:58 -07002644 */
Michael Kerrisk996636d2015-01-16 20:28:06 +01002645static int futex_lock_pi(u32 __user *uaddr, unsigned int flags,
Darren Hartb41277d2010-11-08 13:10:09 -08002646 ktime_t *time, int trylock)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002647{
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07002648 struct hrtimer_sleeper timeout, *to = NULL;
Thomas Gleixnercf16e422021-02-01 10:01:43 +00002649 struct task_struct *exiting = NULL;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002650 struct futex_hash_bucket *hb;
Darren Hart5bdb05f2010-11-08 13:40:28 -08002651 struct futex_q q = futex_q_init;
Darren Hartdd973992009-04-03 13:40:02 -07002652 int res, ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002653
2654 if (refill_pi_state_cache())
2655 return -ENOMEM;
2656
Pierre Peifferc19384b2007-05-09 02:35:02 -07002657 if (time) {
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07002658 to = &timeout;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07002659 hrtimer_init_on_stack(&to->timer, CLOCK_REALTIME,
2660 HRTIMER_MODE_ABS);
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07002661 hrtimer_init_sleeper(to, current);
Arjan van de Vencc584b22008-09-01 15:02:30 -07002662 hrtimer_set_expires(&to->timer, *time);
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07002663 }
2664
Darren Hart42d35d42008-12-29 15:49:53 -08002665retry:
Shawn Bohrer9ea71502011-06-30 11:21:32 -05002666 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q.key, VERIFY_WRITE);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002667 if (unlikely(ret != 0))
Darren Hart42d35d42008-12-29 15:49:53 -08002668 goto out;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002669
Darren Harte4dc5b72009-03-12 00:56:13 -07002670retry_private:
Eric Sesterhenn82af7ac2008-01-25 10:40:46 +01002671 hb = queue_lock(&q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002672
Thomas Gleixnercf16e422021-02-01 10:01:43 +00002673 ret = futex_lock_pi_atomic(uaddr, hb, &q.key, &q.pi_state, current,
2674 &exiting, 0);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002675 if (unlikely(ret)) {
Davidlohr Bueso767f5092015-06-29 23:26:01 -07002676 /*
2677 * Atomic work succeeded and we got the lock,
2678 * or failed. Either way, we do _not_ block.
2679 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002680 switch (ret) {
Darren Hart1a520842009-04-03 13:39:52 -07002681 case 1:
2682 /* We got the lock. */
2683 ret = 0;
2684 goto out_unlock_put_key;
2685 case -EFAULT:
2686 goto uaddr_faulted;
Thomas Gleixnerc27f3922021-02-01 10:01:42 +00002687 case -EBUSY:
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002688 case -EAGAIN:
2689 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00002690 * Two reasons for this:
Thomas Gleixnerc27f3922021-02-01 10:01:42 +00002691 * - EBUSY: Task is exiting and we just wait for the
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00002692 * exit to complete.
Thomas Gleixnerc27f3922021-02-01 10:01:42 +00002693 * - EAGAIN: The user space value changed.
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002694 */
Jason Low0d00c7b2014-01-12 15:31:22 -08002695 queue_unlock(hb);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002696 put_futex_key(&q.key);
Thomas Gleixnercf16e422021-02-01 10:01:43 +00002697 /*
2698 * Handle the case where the owner is in the middle of
2699 * exiting. Wait for the exit to complete otherwise
2700 * this task might loop forever, aka. live lock.
2701 */
2702 wait_for_owner_exiting(ret, exiting);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002703 cond_resched();
2704 goto retry;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002705 default:
Darren Hart42d35d42008-12-29 15:49:53 -08002706 goto out_unlock_put_key;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002707 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07002708 }
2709
2710 /*
2711 * Only actually queue now that the atomic ops are done:
2712 */
Eric Sesterhenn82af7ac2008-01-25 10:40:46 +01002713 queue_me(&q, hb);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002714
Ingo Molnarc87e2832006-06-27 02:54:58 -07002715 WARN_ON(!q.pi_state);
2716 /*
2717 * Block on the PI mutex:
2718 */
Thomas Gleixnerc051b212014-05-22 03:25:50 +00002719 if (!trylock) {
2720 ret = rt_mutex_timed_futex_lock(&q.pi_state->pi_mutex, to);
2721 } else {
Peter Zijlstra2c60d4a2021-02-03 13:45:30 +00002722 ret = rt_mutex_futex_trylock(&q.pi_state->pi_mutex);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002723 /* Fixup the trylock return value: */
2724 ret = ret ? 0 : -EWOULDBLOCK;
2725 }
2726
Vernon Mauerya99e4e42006-07-01 04:35:42 -07002727 spin_lock(q.lock_ptr);
Darren Hartdd973992009-04-03 13:40:02 -07002728 /*
2729 * Fixup the pi_state owner and possibly acquire the lock if we
2730 * haven't already.
2731 */
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002732 res = fixup_owner(uaddr, &q, !ret);
Darren Hartdd973992009-04-03 13:40:02 -07002733 /*
2734 * If fixup_owner() returned an error, proprogate that. If it acquired
2735 * the lock, clear our -ETIMEDOUT or -EINTR.
2736 */
2737 if (res)
2738 ret = (res < 0) ? res : 0;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002739
Darren Harte8f63862009-03-12 00:56:06 -07002740 /*
Darren Hartdd973992009-04-03 13:40:02 -07002741 * If fixup_owner() faulted and was unable to handle the fault, unlock
2742 * it and return the fault to userspace.
Darren Harte8f63862009-03-12 00:56:06 -07002743 */
2744 if (ret && (rt_mutex_owner(&q.pi_state->pi_mutex) == current))
Peter Zijlstra2c60d4a2021-02-03 13:45:30 +00002745 rt_mutex_futex_unlock(&q.pi_state->pi_mutex);
Darren Harte8f63862009-03-12 00:56:06 -07002746
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002747 /* Unqueue and drop the lock */
2748 unqueue_me_pi(&q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002749
Mikael Pettersson5ecb01c2010-01-23 22:36:29 +01002750 goto out_put_key;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002751
Darren Hart42d35d42008-12-29 15:49:53 -08002752out_unlock_put_key:
Jason Low0d00c7b2014-01-12 15:31:22 -08002753 queue_unlock(hb);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002754
Darren Hart42d35d42008-12-29 15:49:53 -08002755out_put_key:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002756 put_futex_key(&q.key);
Darren Hart42d35d42008-12-29 15:49:53 -08002757out:
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07002758 if (to)
2759 destroy_hrtimer_on_stack(&to->timer);
Darren Hartdd973992009-04-03 13:40:02 -07002760 return ret != -EINTR ? ret : -ERESTARTNOINTR;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002761
Darren Hart42d35d42008-12-29 15:49:53 -08002762uaddr_faulted:
Jason Low0d00c7b2014-01-12 15:31:22 -08002763 queue_unlock(hb);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002764
Thomas Gleixnerd0725992009-06-11 23:15:43 +02002765 ret = fault_in_user_writeable(uaddr);
Darren Harte4dc5b72009-03-12 00:56:13 -07002766 if (ret)
2767 goto out_put_key;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002768
Darren Hartb41277d2010-11-08 13:10:09 -08002769 if (!(flags & FLAGS_SHARED))
Darren Harte4dc5b72009-03-12 00:56:13 -07002770 goto retry_private;
2771
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002772 put_futex_key(&q.key);
Darren Harte4dc5b72009-03-12 00:56:13 -07002773 goto retry;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002774}
2775
2776/*
Ingo Molnarc87e2832006-06-27 02:54:58 -07002777 * Userspace attempted a TID -> 0 atomic transition, and failed.
2778 * This is the in-kernel slowpath: we look up the PI state (if any),
2779 * and do the rt-mutex unlock.
2780 */
Darren Hartb41277d2010-11-08 13:10:09 -08002781static int futex_unlock_pi(u32 __user *uaddr, unsigned int flags)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002782{
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002783 u32 uninitialized_var(curval), uval, vpid = task_pid_vnr(current);
Peter Zijlstra38d47c12008-09-26 19:32:20 +02002784 union futex_key key = FUTEX_KEY_INIT;
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002785 struct futex_hash_bucket *hb;
2786 struct futex_q *match;
Darren Harte4dc5b72009-03-12 00:56:13 -07002787 int ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002788
2789retry:
2790 if (get_user(uval, uaddr))
2791 return -EFAULT;
2792 /*
2793 * We release only a lock we actually own:
2794 */
Thomas Gleixnerc0c9ed12011-03-11 11:51:22 +01002795 if ((uval & FUTEX_TID_MASK) != vpid)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002796 return -EPERM;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002797
Shawn Bohrer9ea71502011-06-30 11:21:32 -05002798 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, VERIFY_WRITE);
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002799 if (ret)
2800 return ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002801
2802 hb = hash_futex(&key);
2803 spin_lock(&hb->lock);
2804
Ingo Molnarc87e2832006-06-27 02:54:58 -07002805 /*
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002806 * Check waiters first. We do not trust user space values at
2807 * all and we at least want to know if user space fiddled
2808 * with the futex value instead of blindly unlocking.
Ingo Molnarc87e2832006-06-27 02:54:58 -07002809 */
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002810 match = futex_top_waiter(hb, &key);
2811 if (match) {
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02002812 ret = wake_futex_pi(uaddr, uval, match, hb);
2813 /*
2814 * In case of success wake_futex_pi dropped the hash
2815 * bucket lock.
2816 */
2817 if (!ret)
2818 goto out_putkey;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002819 /*
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002820 * The atomic access to the futex value generated a
2821 * pagefault, so retry the user-access and the wakeup:
Ingo Molnarc87e2832006-06-27 02:54:58 -07002822 */
2823 if (ret == -EFAULT)
2824 goto pi_faulted;
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02002825 /*
Sebastian Andrzej Siewior89e9e662016-04-15 14:35:39 +02002826 * A unconditional UNLOCK_PI op raced against a waiter
2827 * setting the FUTEX_WAITERS bit. Try again.
2828 */
2829 if (ret == -EAGAIN) {
2830 spin_unlock(&hb->lock);
2831 put_futex_key(&key);
2832 goto retry;
2833 }
2834 /*
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02002835 * wake_futex_pi has detected invalid state. Tell user
2836 * space.
2837 */
Ingo Molnarc87e2832006-06-27 02:54:58 -07002838 goto out_unlock;
2839 }
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002840
Ingo Molnarc87e2832006-06-27 02:54:58 -07002841 /*
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002842 * We have no kernel internal state, i.e. no waiters in the
2843 * kernel. Waiters which are about to queue themselves are stuck
2844 * on hb->lock. So we can safely ignore them. We do neither
2845 * preserve the WAITERS bit not the OWNER_DIED one. We are the
2846 * owner.
Ingo Molnarc87e2832006-06-27 02:54:58 -07002847 */
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002848 if (cmpxchg_futex_value_locked(&curval, uaddr, uval, 0))
Thomas Gleixner13fbca42014-06-03 12:27:07 +00002849 goto pi_faulted;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002850
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002851 /*
2852 * If uval has changed, let user space handle it.
2853 */
2854 ret = (curval == uval) ? 0 : -EAGAIN;
2855
Ingo Molnarc87e2832006-06-27 02:54:58 -07002856out_unlock:
2857 spin_unlock(&hb->lock);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02002858out_putkey:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002859 put_futex_key(&key);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002860 return ret;
2861
2862pi_faulted:
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002863 spin_unlock(&hb->lock);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002864 put_futex_key(&key);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002865
Thomas Gleixnerd0725992009-06-11 23:15:43 +02002866 ret = fault_in_user_writeable(uaddr);
Darren Hartb5686362008-12-18 15:06:34 -08002867 if (!ret)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002868 goto retry;
2869
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 return ret;
2871}
2872
Darren Hart52400ba2009-04-03 13:40:49 -07002873/**
2874 * handle_early_requeue_pi_wakeup() - Detect early wakeup on the initial futex
2875 * @hb: the hash_bucket futex_q was original enqueued on
2876 * @q: the futex_q woken while waiting to be requeued
2877 * @key2: the futex_key of the requeue target futex
2878 * @timeout: the timeout associated with the wait (NULL if none)
2879 *
2880 * Detect if the task was woken on the initial futex as opposed to the requeue
2881 * target futex. If so, determine if it was a timeout or a signal that caused
2882 * the wakeup and return the appropriate error code to the caller. Must be
2883 * called with the hb lock held.
2884 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002885 * Return:
2886 * 0 = no early wakeup detected;
2887 * <0 = -ETIMEDOUT or -ERESTARTNOINTR
Darren Hart52400ba2009-04-03 13:40:49 -07002888 */
2889static inline
2890int handle_early_requeue_pi_wakeup(struct futex_hash_bucket *hb,
2891 struct futex_q *q, union futex_key *key2,
2892 struct hrtimer_sleeper *timeout)
2893{
2894 int ret = 0;
2895
2896 /*
2897 * With the hb lock held, we avoid races while we process the wakeup.
2898 * We only need to hold hb (and not hb2) to ensure atomicity as the
2899 * wakeup code can't change q.key from uaddr to uaddr2 if we hold hb.
2900 * It can't be requeued from uaddr2 to something else since we don't
2901 * support a PI aware source futex for requeue.
2902 */
2903 if (!match_futex(&q->key, key2)) {
2904 WARN_ON(q->lock_ptr && (&hb->lock != q->lock_ptr));
2905 /*
2906 * We were woken prior to requeue by a timeout or a signal.
2907 * Unqueue the futex_q and determine which it was.
2908 */
Lai Jiangshan2e129782010-12-22 14:18:50 +08002909 plist_del(&q->list, &hb->chain);
Linus Torvalds11d46162014-03-20 22:11:17 -07002910 hb_waiters_dec(hb);
Darren Hart52400ba2009-04-03 13:40:49 -07002911
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002912 /* Handle spurious wakeups gracefully */
Thomas Gleixner11df6dd2009-10-28 20:26:48 +01002913 ret = -EWOULDBLOCK;
Darren Hart52400ba2009-04-03 13:40:49 -07002914 if (timeout && !timeout->task)
2915 ret = -ETIMEDOUT;
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002916 else if (signal_pending(current))
Thomas Gleixner1c840c12009-05-20 09:22:40 +02002917 ret = -ERESTARTNOINTR;
Darren Hart52400ba2009-04-03 13:40:49 -07002918 }
2919 return ret;
2920}
2921
2922/**
2923 * futex_wait_requeue_pi() - Wait on uaddr and take uaddr2
Darren Hart56ec1602009-09-21 22:29:59 -07002924 * @uaddr: the futex we initially wait on (non-pi)
Darren Hartb41277d2010-11-08 13:10:09 -08002925 * @flags: futex flags (FLAGS_SHARED, FLAGS_CLOCKRT, etc.), they must be
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07002926 * the same type, no requeueing from private to shared, etc.
Darren Hart52400ba2009-04-03 13:40:49 -07002927 * @val: the expected value of uaddr
2928 * @abs_time: absolute timeout
Darren Hart56ec1602009-09-21 22:29:59 -07002929 * @bitset: 32 bit wakeup bitset set by userspace, defaults to all
Darren Hart52400ba2009-04-03 13:40:49 -07002930 * @uaddr2: the pi futex we will take prior to returning to user-space
2931 *
2932 * The caller will wait on uaddr and will be requeued by futex_requeue() to
Darren Hart6f7b0a22012-07-20 11:53:31 -07002933 * uaddr2 which must be PI aware and unique from uaddr. Normal wakeup will wake
2934 * on uaddr2 and complete the acquisition of the rt_mutex prior to returning to
2935 * userspace. This ensures the rt_mutex maintains an owner when it has waiters;
2936 * without one, the pi logic would not know which task to boost/deboost, if
2937 * there was a need to.
Darren Hart52400ba2009-04-03 13:40:49 -07002938 *
2939 * We call schedule in futex_wait_queue_me() when we enqueue and return there
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002940 * via the following--
Darren Hart52400ba2009-04-03 13:40:49 -07002941 * 1) wakeup on uaddr2 after an atomic lock acquisition by futex_requeue()
Darren Hartcc6db4e2009-07-31 16:20:10 -07002942 * 2) wakeup on uaddr2 after a requeue
2943 * 3) signal
2944 * 4) timeout
Darren Hart52400ba2009-04-03 13:40:49 -07002945 *
Darren Hartcc6db4e2009-07-31 16:20:10 -07002946 * If 3, cleanup and return -ERESTARTNOINTR.
Darren Hart52400ba2009-04-03 13:40:49 -07002947 *
2948 * If 2, we may then block on trying to take the rt_mutex and return via:
2949 * 5) successful lock
2950 * 6) signal
2951 * 7) timeout
2952 * 8) other lock acquisition failure
2953 *
Darren Hartcc6db4e2009-07-31 16:20:10 -07002954 * If 6, return -EWOULDBLOCK (restarting the syscall would do the same).
Darren Hart52400ba2009-04-03 13:40:49 -07002955 *
2956 * If 4 or 7, we cleanup and return with -ETIMEDOUT.
2957 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002958 * Return:
2959 * 0 - On success;
Darren Hart52400ba2009-04-03 13:40:49 -07002960 * <0 - On error
2961 */
Darren Hartb41277d2010-11-08 13:10:09 -08002962static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
Darren Hart52400ba2009-04-03 13:40:49 -07002963 u32 val, ktime_t *abs_time, u32 bitset,
Darren Hartb41277d2010-11-08 13:10:09 -08002964 u32 __user *uaddr2)
Darren Hart52400ba2009-04-03 13:40:49 -07002965{
2966 struct hrtimer_sleeper timeout, *to = NULL;
2967 struct rt_mutex_waiter rt_waiter;
Darren Hart52400ba2009-04-03 13:40:49 -07002968 struct futex_hash_bucket *hb;
Darren Hart5bdb05f2010-11-08 13:40:28 -08002969 union futex_key key2 = FUTEX_KEY_INIT;
2970 struct futex_q q = futex_q_init;
Darren Hart52400ba2009-04-03 13:40:49 -07002971 int res, ret;
Darren Hart52400ba2009-04-03 13:40:49 -07002972
Darren Hart6f7b0a22012-07-20 11:53:31 -07002973 if (uaddr == uaddr2)
2974 return -EINVAL;
2975
Darren Hart52400ba2009-04-03 13:40:49 -07002976 if (!bitset)
2977 return -EINVAL;
2978
2979 if (abs_time) {
2980 to = &timeout;
Darren Hartb41277d2010-11-08 13:10:09 -08002981 hrtimer_init_on_stack(&to->timer, (flags & FLAGS_CLOCKRT) ?
2982 CLOCK_REALTIME : CLOCK_MONOTONIC,
2983 HRTIMER_MODE_ABS);
Darren Hart52400ba2009-04-03 13:40:49 -07002984 hrtimer_init_sleeper(to, current);
2985 hrtimer_set_expires_range_ns(&to->timer, *abs_time,
2986 current->timer_slack_ns);
2987 }
2988
2989 /*
2990 * The waiter is allocated on our stack, manipulated by the requeue
2991 * code while we sleep on uaddr.
2992 */
2993 debug_rt_mutex_init_waiter(&rt_waiter);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01002994 RB_CLEAR_NODE(&rt_waiter.pi_tree_entry);
2995 RB_CLEAR_NODE(&rt_waiter.tree_entry);
Darren Hart52400ba2009-04-03 13:40:49 -07002996 rt_waiter.task = NULL;
2997
Shawn Bohrer9ea71502011-06-30 11:21:32 -05002998 ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE);
Darren Hart52400ba2009-04-03 13:40:49 -07002999 if (unlikely(ret != 0))
3000 goto out;
3001
Darren Hart84bc4af2009-08-13 17:36:53 -07003002 q.bitset = bitset;
3003 q.rt_waiter = &rt_waiter;
3004 q.requeue_pi_key = &key2;
3005
Darren Hart7ada8762010-10-17 08:35:04 -07003006 /*
3007 * Prepare to wait on uaddr. On success, increments q.key (key1) ref
3008 * count.
3009 */
Darren Hartb41277d2010-11-08 13:10:09 -08003010 ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
Thomas Gleixnerc8b15a72009-05-20 09:18:50 +02003011 if (ret)
3012 goto out_key2;
Darren Hart52400ba2009-04-03 13:40:49 -07003013
Thomas Gleixnere9c243a2014-06-03 12:27:06 +00003014 /*
3015 * The check above which compares uaddrs is not sufficient for
3016 * shared futexes. We need to compare the keys:
3017 */
3018 if (match_futex(&q.key, &key2)) {
Thomas Gleixner13c42c22014-09-11 23:44:35 +02003019 queue_unlock(hb);
Thomas Gleixnere9c243a2014-06-03 12:27:06 +00003020 ret = -EINVAL;
3021 goto out_put_keys;
3022 }
3023
Darren Hart52400ba2009-04-03 13:40:49 -07003024 /* Queue the futex_q, drop the hb lock, wait for wakeup. */
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02003025 futex_wait_queue_me(hb, &q, to);
Darren Hart52400ba2009-04-03 13:40:49 -07003026
3027 spin_lock(&hb->lock);
3028 ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
3029 spin_unlock(&hb->lock);
3030 if (ret)
3031 goto out_put_keys;
3032
3033 /*
3034 * In order for us to be here, we know our q.key == key2, and since
3035 * we took the hb->lock above, we also know that futex_requeue() has
3036 * completed and we no longer have to concern ourselves with a wakeup
Darren Hart7ada8762010-10-17 08:35:04 -07003037 * race with the atomic proxy lock acquisition by the requeue code. The
3038 * futex_requeue dropped our key1 reference and incremented our key2
3039 * reference count.
Darren Hart52400ba2009-04-03 13:40:49 -07003040 */
3041
3042 /* Check if the requeue code acquired the second futex for us. */
3043 if (!q.rt_waiter) {
3044 /*
3045 * Got the lock. We might not be the anticipated owner if we
3046 * did a lock-steal - fix up the PI-state in that case.
3047 */
3048 if (q.pi_state && (q.pi_state->owner != current)) {
3049 spin_lock(q.lock_ptr);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01003050 ret = fixup_pi_state_owner(uaddr2, &q, current);
Peter Zijlstra15221812017-03-04 10:27:19 +01003051 if (ret && rt_mutex_owner(&q.pi_state->pi_mutex) == current)
Peter Zijlstra2c60d4a2021-02-03 13:45:30 +00003052 rt_mutex_futex_unlock(&q.pi_state->pi_mutex);
Thomas Gleixnerfb75a422015-12-19 20:07:38 +00003053 /*
3054 * Drop the reference to the pi state which
3055 * the requeue_pi() code acquired for us.
3056 */
Thomas Gleixner29e9ee52015-12-19 20:07:39 +00003057 put_pi_state(q.pi_state);
Darren Hart52400ba2009-04-03 13:40:49 -07003058 spin_unlock(q.lock_ptr);
3059 }
3060 } else {
Peter Zijlstra6244ffc2017-03-04 10:27:18 +01003061 struct rt_mutex *pi_mutex;
3062
Darren Hart52400ba2009-04-03 13:40:49 -07003063 /*
3064 * We have been woken up by futex_unlock_pi(), a timeout, or a
3065 * signal. futex_unlock_pi() will not destroy the lock_ptr nor
3066 * the pi_state.
3067 */
Darren Hartf27071c2012-07-20 11:53:30 -07003068 WARN_ON(!q.pi_state);
Darren Hart52400ba2009-04-03 13:40:49 -07003069 pi_mutex = &q.pi_state->pi_mutex;
Peter Zijlstrace813552017-03-22 11:35:57 +01003070 ret = rt_mutex_wait_proxy_lock(pi_mutex, to, &rt_waiter);
Darren Hart52400ba2009-04-03 13:40:49 -07003071
3072 spin_lock(q.lock_ptr);
Peter Zijlstrace813552017-03-22 11:35:57 +01003073 if (ret && !rt_mutex_cleanup_proxy_lock(pi_mutex, &rt_waiter))
3074 ret = 0;
3075
3076 debug_rt_mutex_free_waiter(&rt_waiter);
Darren Hart52400ba2009-04-03 13:40:49 -07003077 /*
3078 * Fixup the pi_state owner and possibly acquire the lock if we
3079 * haven't already.
3080 */
Thomas Gleixnerae791a22010-11-10 13:30:36 +01003081 res = fixup_owner(uaddr2, &q, !ret);
Darren Hart52400ba2009-04-03 13:40:49 -07003082 /*
3083 * If fixup_owner() returned an error, proprogate that. If it
Darren Hart56ec1602009-09-21 22:29:59 -07003084 * acquired the lock, clear -ETIMEDOUT or -EINTR.
Darren Hart52400ba2009-04-03 13:40:49 -07003085 */
3086 if (res)
3087 ret = (res < 0) ? res : 0;
3088
Peter Zijlstra6244ffc2017-03-04 10:27:18 +01003089 /*
3090 * If fixup_pi_state_owner() faulted and was unable to handle
3091 * the fault, unlock the rt_mutex and return the fault to
3092 * userspace.
3093 */
3094 if (ret && rt_mutex_owner(pi_mutex) == current)
Peter Zijlstra2c60d4a2021-02-03 13:45:30 +00003095 rt_mutex_futex_unlock(pi_mutex);
Peter Zijlstra6244ffc2017-03-04 10:27:18 +01003096
Darren Hart52400ba2009-04-03 13:40:49 -07003097 /* Unqueue and drop the lock. */
3098 unqueue_me_pi(&q);
3099 }
3100
Peter Zijlstra6244ffc2017-03-04 10:27:18 +01003101 if (ret == -EINTR) {
Darren Hart52400ba2009-04-03 13:40:49 -07003102 /*
Darren Hartcc6db4e2009-07-31 16:20:10 -07003103 * We've already been requeued, but cannot restart by calling
3104 * futex_lock_pi() directly. We could restart this syscall, but
3105 * it would detect that the user space "val" changed and return
3106 * -EWOULDBLOCK. Save the overhead of the restart and return
3107 * -EWOULDBLOCK directly.
Darren Hart52400ba2009-04-03 13:40:49 -07003108 */
Thomas Gleixner20708872009-05-19 23:04:59 +02003109 ret = -EWOULDBLOCK;
Darren Hart52400ba2009-04-03 13:40:49 -07003110 }
3111
3112out_put_keys:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01003113 put_futex_key(&q.key);
Thomas Gleixnerc8b15a72009-05-20 09:18:50 +02003114out_key2:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01003115 put_futex_key(&key2);
Darren Hart52400ba2009-04-03 13:40:49 -07003116
3117out:
3118 if (to) {
3119 hrtimer_cancel(&to->timer);
3120 destroy_hrtimer_on_stack(&to->timer);
3121 }
3122 return ret;
3123}
3124
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003125/*
3126 * Support for robust futexes: the kernel cleans up held futexes at
3127 * thread exit time.
3128 *
3129 * Implementation: user-space maintains a per-thread list of locks it
3130 * is holding. Upon do_exit(), the kernel carefully walks this list,
3131 * and marks all locks that are owned by this thread with the
Ingo Molnarc87e2832006-06-27 02:54:58 -07003132 * FUTEX_OWNER_DIED bit, and wakes up a waiter (if any). The list is
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003133 * always manipulated with the lock held, so the list is private and
3134 * per-thread. Userspace also maintains a per-thread 'list_op_pending'
3135 * field, to allow the kernel to clean up if the thread dies after
3136 * acquiring the lock, but just before it could have added itself to
3137 * the list. There can only be one such pending lock.
3138 */
3139
3140/**
Darren Hartd96ee562009-09-21 22:30:22 -07003141 * sys_set_robust_list() - Set the robust-futex list head of a task
3142 * @head: pointer to the list-head
3143 * @len: length of the list-head, as userspace expects
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003144 */
Heiko Carstens836f92a2009-01-14 14:14:33 +01003145SYSCALL_DEFINE2(set_robust_list, struct robust_list_head __user *, head,
3146 size_t, len)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003147{
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08003148 if (!futex_cmpxchg_enabled)
3149 return -ENOSYS;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003150 /*
3151 * The kernel knows only one size for now:
3152 */
3153 if (unlikely(len != sizeof(*head)))
3154 return -EINVAL;
3155
3156 current->robust_list = head;
3157
3158 return 0;
3159}
3160
3161/**
Darren Hartd96ee562009-09-21 22:30:22 -07003162 * sys_get_robust_list() - Get the robust-futex list head of a task
3163 * @pid: pid of the process [zero for current task]
3164 * @head_ptr: pointer to a list-head pointer, the kernel fills it in
3165 * @len_ptr: pointer to a length field, the kernel fills in the header size
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003166 */
Heiko Carstens836f92a2009-01-14 14:14:33 +01003167SYSCALL_DEFINE3(get_robust_list, int, pid,
3168 struct robust_list_head __user * __user *, head_ptr,
3169 size_t __user *, len_ptr)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003170{
Al Viroba46df92006-10-10 22:46:07 +01003171 struct robust_list_head __user *head;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003172 unsigned long ret;
Kees Cookbdbb7762012-03-19 16:12:53 -07003173 struct task_struct *p;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003174
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08003175 if (!futex_cmpxchg_enabled)
3176 return -ENOSYS;
3177
Kees Cookbdbb7762012-03-19 16:12:53 -07003178 rcu_read_lock();
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003179
Kees Cookbdbb7762012-03-19 16:12:53 -07003180 ret = -ESRCH;
3181 if (!pid)
3182 p = current;
3183 else {
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07003184 p = find_task_by_vpid(pid);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003185 if (!p)
3186 goto err_unlock;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003187 }
3188
Kees Cookbdbb7762012-03-19 16:12:53 -07003189 ret = -EPERM;
Jann Horncaaee622016-01-20 15:00:04 -08003190 if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS))
Kees Cookbdbb7762012-03-19 16:12:53 -07003191 goto err_unlock;
3192
3193 head = p->robust_list;
3194 rcu_read_unlock();
3195
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003196 if (put_user(sizeof(*head), len_ptr))
3197 return -EFAULT;
3198 return put_user(head, head_ptr);
3199
3200err_unlock:
Oleg Nesterovaaa2a972006-09-29 02:00:55 -07003201 rcu_read_unlock();
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003202
3203 return ret;
3204}
3205
3206/*
3207 * Process a futex-list entry, check whether it's owned by the
3208 * dying task, and do notification if so:
3209 */
Arnd Bergmannbdb116c2021-02-01 10:01:32 +00003210static int handle_futex_death(u32 __user *uaddr, struct task_struct *curr, int pi)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003211{
Vitaliy Ivanov7cfdaf32011-07-07 15:10:31 +03003212 u32 uval, uninitialized_var(nval), mval;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003213
Chen Jie726c28f2019-03-15 03:44:38 +00003214 /* Futex address must be 32bit aligned */
3215 if ((((unsigned long)uaddr) % sizeof(*uaddr)) != 0)
3216 return -1;
3217
Ingo Molnar8f17d3a2006-03-27 01:16:27 -08003218retry:
3219 if (get_user(uval, uaddr))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003220 return -1;
3221
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003222 if ((uval & FUTEX_TID_MASK) == task_pid_vnr(curr)) {
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003223 /*
3224 * Ok, this dying thread is truly holding a futex
3225 * of interest. Set the OWNER_DIED bit atomically
3226 * via cmpxchg, and if the value had FUTEX_WAITERS
3227 * set, wake up a waiter (if any). (We have to do a
3228 * futex_wake() even if OWNER_DIED is already set -
3229 * to handle the rare but possible case of recursive
3230 * thread-death.) The rest of the cleanup is done in
3231 * userspace.
3232 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003233 mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
Thomas Gleixner6e0aa9f2011-03-14 10:34:35 +01003234 /*
3235 * We are not holding a lock here, but we want to have
3236 * the pagefault_disable/enable() protection because
3237 * we want to handle the fault gracefully. If the
3238 * access fails we try to fault in the futex with R/W
3239 * verification via get_user_pages. get_user() above
3240 * does not guarantee R/W access. If that fails we
3241 * give up and leave the futex locked.
3242 */
3243 if (cmpxchg_futex_value_locked(&nval, uaddr, uval, mval)) {
3244 if (fault_in_user_writeable(uaddr))
3245 return -1;
3246 goto retry;
3247 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07003248 if (nval != uval)
Ingo Molnar8f17d3a2006-03-27 01:16:27 -08003249 goto retry;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003250
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003251 /*
3252 * Wake robust non-PI futexes here. The wakeup of
3253 * PI futexes happens in exit_pi_state():
3254 */
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07003255 if (!pi && (uval & FUTEX_WAITERS))
Peter Zijlstrac2f9f202008-09-26 19:32:23 +02003256 futex_wake(uaddr, 1, 1, FUTEX_BITSET_MATCH_ANY);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003257 }
3258 return 0;
3259}
3260
3261/*
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003262 * Fetch a robust-list pointer. Bit 0 signals PI futexes:
3263 */
3264static inline int fetch_robust_entry(struct robust_list __user **entry,
Al Viroba46df92006-10-10 22:46:07 +01003265 struct robust_list __user * __user *head,
Namhyung Kim1dcc41b2010-09-14 21:43:46 +09003266 unsigned int *pi)
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003267{
3268 unsigned long uentry;
3269
Al Viroba46df92006-10-10 22:46:07 +01003270 if (get_user(uentry, (unsigned long __user *)head))
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003271 return -EFAULT;
3272
Al Viroba46df92006-10-10 22:46:07 +01003273 *entry = (void __user *)(uentry & ~1UL);
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003274 *pi = uentry & 1;
3275
3276 return 0;
3277}
3278
3279/*
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003280 * Walk curr->robust_list (very carefully, it's a userspace list!)
3281 * and mark any locks found there dead, and notify any waiters.
3282 *
3283 * We silently return on any sign of list-walking problem.
3284 */
Thomas Gleixner25f319b2021-02-01 10:01:33 +00003285static void exit_robust_list(struct task_struct *curr)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003286{
3287 struct robust_list_head __user *head = curr->robust_list;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003288 struct robust_list __user *entry, *next_entry, *pending;
Darren Hart4c115e92010-11-04 15:00:00 -04003289 unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
3290 unsigned int uninitialized_var(next_pi);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003291 unsigned long futex_offset;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003292 int rc;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003293
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08003294 if (!futex_cmpxchg_enabled)
3295 return;
3296
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003297 /*
3298 * Fetch the list head (which was registered earlier, via
3299 * sys_set_robust_list()):
3300 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003301 if (fetch_robust_entry(&entry, &head->list.next, &pi))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003302 return;
3303 /*
3304 * Fetch the relative futex offset:
3305 */
3306 if (get_user(futex_offset, &head->futex_offset))
3307 return;
3308 /*
3309 * Fetch any possibly pending lock-add first, and handle it
3310 * if it exists:
3311 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003312 if (fetch_robust_entry(&pending, &head->list_op_pending, &pip))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003313 return;
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003314
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003315 next_entry = NULL; /* avoid warning with gcc */
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003316 while (entry != &head->list) {
3317 /*
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003318 * Fetch the next entry in the list before calling
3319 * handle_futex_death:
3320 */
3321 rc = fetch_robust_entry(&next_entry, &entry->next, &next_pi);
3322 /*
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003323 * A pending lock might already be on the list, so
Ingo Molnarc87e2832006-06-27 02:54:58 -07003324 * don't process it twice:
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003325 */
3326 if (entry != pending)
Al Viroba46df92006-10-10 22:46:07 +01003327 if (handle_futex_death((void __user *)entry + futex_offset,
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003328 curr, pi))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003329 return;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003330 if (rc)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003331 return;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003332 entry = next_entry;
3333 pi = next_pi;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003334 /*
3335 * Avoid excessively long or circular lists:
3336 */
3337 if (!--limit)
3338 break;
3339
3340 cond_resched();
3341 }
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003342
3343 if (pending)
3344 handle_futex_death((void __user *)pending + futex_offset,
3345 curr, pip);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003346}
3347
Thomas Gleixnerff3a33f2021-02-01 10:01:40 +00003348static void futex_cleanup(struct task_struct *tsk)
Thomas Gleixner25f319b2021-02-01 10:01:33 +00003349{
3350 if (unlikely(tsk->robust_list)) {
3351 exit_robust_list(tsk);
3352 tsk->robust_list = NULL;
3353 }
3354
3355#ifdef CONFIG_COMPAT
3356 if (unlikely(tsk->compat_robust_list)) {
3357 compat_exit_robust_list(tsk);
3358 tsk->compat_robust_list = NULL;
3359 }
3360#endif
3361
3362 if (unlikely(!list_empty(&tsk->pi_state_list)))
3363 exit_pi_state_list(tsk);
3364}
3365
Thomas Gleixner32d78282021-02-01 10:01:38 +00003366/**
3367 * futex_exit_recursive - Set the tasks futex state to FUTEX_STATE_DEAD
3368 * @tsk: task to set the state on
3369 *
3370 * Set the futex exit state of the task lockless. The futex waiter code
3371 * observes that state when a task is exiting and loops until the task has
3372 * actually finished the futex cleanup. The worst case for this is that the
3373 * waiter runs through the wait loop until the state becomes visible.
3374 *
3375 * This is called from the recursive fault handling path in do_exit().
3376 *
3377 * This is best effort. Either the futex exit code has run already or
3378 * not. If the OWNER_DIED bit has been set on the futex then the waiter can
3379 * take it over. If not, the problem is pushed back to user space. If the
3380 * futex exit code did not run yet, then an already queued waiter might
3381 * block forever, but there is nothing which can be done about that.
3382 */
3383void futex_exit_recursive(struct task_struct *tsk)
3384{
Thomas Gleixnerad3466a2021-02-01 10:01:41 +00003385 /* If the state is FUTEX_STATE_EXITING then futex_exit_mutex is held */
3386 if (tsk->futex_state == FUTEX_STATE_EXITING)
3387 mutex_unlock(&tsk->futex_exit_mutex);
Thomas Gleixner32d78282021-02-01 10:01:38 +00003388 tsk->futex_state = FUTEX_STATE_DEAD;
3389}
3390
Thomas Gleixnerff3a33f2021-02-01 10:01:40 +00003391static void futex_cleanup_begin(struct task_struct *tsk)
Thomas Gleixner8a16d8a2021-02-01 10:01:36 +00003392{
Thomas Gleixner32d78282021-02-01 10:01:38 +00003393 /*
Thomas Gleixnerad3466a2021-02-01 10:01:41 +00003394 * Prevent various race issues against a concurrent incoming waiter
3395 * including live locks by forcing the waiter to block on
3396 * tsk->futex_exit_mutex when it observes FUTEX_STATE_EXITING in
3397 * attach_to_pi_owner().
3398 */
3399 mutex_lock(&tsk->futex_exit_mutex);
3400
3401 /*
Thomas Gleixner0ba263f2021-02-01 10:01:39 +00003402 * Switch the state to FUTEX_STATE_EXITING under tsk->pi_lock.
3403 *
3404 * This ensures that all subsequent checks of tsk->futex_state in
3405 * attach_to_pi_owner() must observe FUTEX_STATE_EXITING with
3406 * tsk->pi_lock held.
3407 *
3408 * It guarantees also that a pi_state which was queued right before
3409 * the state change under tsk->pi_lock by a concurrent waiter must
3410 * be observed in exit_pi_state_list().
Thomas Gleixner32d78282021-02-01 10:01:38 +00003411 */
3412 raw_spin_lock_irq(&tsk->pi_lock);
Thomas Gleixner0ba263f2021-02-01 10:01:39 +00003413 tsk->futex_state = FUTEX_STATE_EXITING;
Thomas Gleixner32d78282021-02-01 10:01:38 +00003414 raw_spin_unlock_irq(&tsk->pi_lock);
Thomas Gleixnerff3a33f2021-02-01 10:01:40 +00003415}
Thomas Gleixner32d78282021-02-01 10:01:38 +00003416
Thomas Gleixnerff3a33f2021-02-01 10:01:40 +00003417static void futex_cleanup_end(struct task_struct *tsk, int state)
3418{
3419 /*
3420 * Lockless store. The only side effect is that an observer might
3421 * take another loop until it becomes visible.
3422 */
3423 tsk->futex_state = state;
Thomas Gleixnerad3466a2021-02-01 10:01:41 +00003424 /*
3425 * Drop the exit protection. This unblocks waiters which observed
3426 * FUTEX_STATE_EXITING to reevaluate the state.
3427 */
3428 mutex_unlock(&tsk->futex_exit_mutex);
Thomas Gleixnerff3a33f2021-02-01 10:01:40 +00003429}
Thomas Gleixner32d78282021-02-01 10:01:38 +00003430
Thomas Gleixnerff3a33f2021-02-01 10:01:40 +00003431void futex_exec_release(struct task_struct *tsk)
3432{
3433 /*
3434 * The state handling is done for consistency, but in the case of
3435 * exec() there is no way to prevent futher damage as the PID stays
3436 * the same. But for the unlikely and arguably buggy case that a
3437 * futex is held on exec(), this provides at least as much state
3438 * consistency protection which is possible.
3439 */
3440 futex_cleanup_begin(tsk);
3441 futex_cleanup(tsk);
3442 /*
3443 * Reset the state to FUTEX_STATE_OK. The task is alive and about
3444 * exec a new binary.
3445 */
3446 futex_cleanup_end(tsk, FUTEX_STATE_OK);
3447}
3448
3449void futex_exit_release(struct task_struct *tsk)
3450{
3451 futex_cleanup_begin(tsk);
3452 futex_cleanup(tsk);
3453 futex_cleanup_end(tsk, FUTEX_STATE_DEAD);
Thomas Gleixner8a16d8a2021-02-01 10:01:36 +00003454}
3455
Pierre Peifferc19384b2007-05-09 02:35:02 -07003456long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
Ingo Molnare2970f22006-06-27 02:54:47 -07003457 u32 __user *uaddr2, u32 val2, u32 val3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458{
Thomas Gleixner81b40532012-02-15 12:17:09 +01003459 int cmd = op & FUTEX_CMD_MASK;
Darren Hartb41277d2010-11-08 13:10:09 -08003460 unsigned int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003462 if (!(op & FUTEX_PRIVATE_FLAG))
Darren Hartb41277d2010-11-08 13:10:09 -08003463 flags |= FLAGS_SHARED;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003464
Darren Hartb41277d2010-11-08 13:10:09 -08003465 if (op & FUTEX_CLOCK_REALTIME) {
3466 flags |= FLAGS_CLOCKRT;
Darren Hart337f1302015-12-18 13:36:37 -08003467 if (cmd != FUTEX_WAIT && cmd != FUTEX_WAIT_BITSET && \
3468 cmd != FUTEX_WAIT_REQUEUE_PI)
Darren Hartb41277d2010-11-08 13:10:09 -08003469 return -ENOSYS;
3470 }
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003471
3472 switch (cmd) {
Thomas Gleixner59263b52012-02-15 12:08:34 +01003473 case FUTEX_LOCK_PI:
3474 case FUTEX_UNLOCK_PI:
3475 case FUTEX_TRYLOCK_PI:
3476 case FUTEX_WAIT_REQUEUE_PI:
3477 case FUTEX_CMP_REQUEUE_PI:
3478 if (!futex_cmpxchg_enabled)
3479 return -ENOSYS;
3480 }
3481
3482 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 case FUTEX_WAIT:
Thomas Gleixnercd689982008-02-01 17:45:14 +01003484 val3 = FUTEX_BITSET_MATCH_ANY;
3485 case FUTEX_WAIT_BITSET:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003486 return futex_wait(uaddr, flags, val, timeout, val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 case FUTEX_WAKE:
Thomas Gleixnercd689982008-02-01 17:45:14 +01003488 val3 = FUTEX_BITSET_MATCH_ANY;
3489 case FUTEX_WAKE_BITSET:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003490 return futex_wake(uaddr, flags, val, val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 case FUTEX_REQUEUE:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003492 return futex_requeue(uaddr, flags, uaddr2, val, val2, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 case FUTEX_CMP_REQUEUE:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003494 return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 0);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07003495 case FUTEX_WAKE_OP:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003496 return futex_wake_op(uaddr, flags, uaddr2, val, val2, val3);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003497 case FUTEX_LOCK_PI:
Michael Kerrisk996636d2015-01-16 20:28:06 +01003498 return futex_lock_pi(uaddr, flags, timeout, 0);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003499 case FUTEX_UNLOCK_PI:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003500 return futex_unlock_pi(uaddr, flags);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003501 case FUTEX_TRYLOCK_PI:
Michael Kerrisk996636d2015-01-16 20:28:06 +01003502 return futex_lock_pi(uaddr, flags, NULL, 1);
Darren Hart52400ba2009-04-03 13:40:49 -07003503 case FUTEX_WAIT_REQUEUE_PI:
3504 val3 = FUTEX_BITSET_MATCH_ANY;
Thomas Gleixner81b40532012-02-15 12:17:09 +01003505 return futex_wait_requeue_pi(uaddr, flags, val, timeout, val3,
3506 uaddr2);
Darren Hart52400ba2009-04-03 13:40:49 -07003507 case FUTEX_CMP_REQUEUE_PI:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003508 return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 }
Thomas Gleixner81b40532012-02-15 12:17:09 +01003510 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511}
3512
3513
Heiko Carstens17da2bd2009-01-14 14:14:10 +01003514SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val,
3515 struct timespec __user *, utime, u32 __user *, uaddr2,
3516 u32, val3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517{
Pierre Peifferc19384b2007-05-09 02:35:02 -07003518 struct timespec ts;
3519 ktime_t t, *tp = NULL;
Ingo Molnare2970f22006-06-27 02:54:47 -07003520 u32 val2 = 0;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003521 int cmd = op & FUTEX_CMD_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522
Thomas Gleixnercd689982008-02-01 17:45:14 +01003523 if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI ||
Darren Hart52400ba2009-04-03 13:40:49 -07003524 cmd == FUTEX_WAIT_BITSET ||
3525 cmd == FUTEX_WAIT_REQUEUE_PI)) {
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07003526 if (unlikely(should_fail_futex(!(op & FUTEX_PRIVATE_FLAG))))
3527 return -EFAULT;
Pierre Peifferc19384b2007-05-09 02:35:02 -07003528 if (copy_from_user(&ts, utime, sizeof(ts)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 return -EFAULT;
Pierre Peifferc19384b2007-05-09 02:35:02 -07003530 if (!timespec_valid(&ts))
Thomas Gleixner9741ef962006-03-31 02:31:32 -08003531 return -EINVAL;
Pierre Peifferc19384b2007-05-09 02:35:02 -07003532
3533 t = timespec_to_ktime(ts);
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003534 if (cmd == FUTEX_WAIT)
Thomas Gleixner5a7780e2008-02-13 09:20:43 +01003535 t = ktime_add_safe(ktime_get(), t);
Pierre Peifferc19384b2007-05-09 02:35:02 -07003536 tp = &t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 }
3538 /*
Darren Hart52400ba2009-04-03 13:40:49 -07003539 * requeue parameter in 'utime' if cmd == FUTEX_*_REQUEUE_*.
Andreas Schwabf54f0982007-07-31 00:38:51 -07003540 * number of waiters to wake in 'utime' if cmd == FUTEX_WAKE_OP.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 */
Andreas Schwabf54f0982007-07-31 00:38:51 -07003542 if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE ||
Darren Hartba9c22f2009-04-20 22:22:22 -07003543 cmd == FUTEX_CMP_REQUEUE_PI || cmd == FUTEX_WAKE_OP)
Ingo Molnare2970f22006-06-27 02:54:47 -07003544 val2 = (u32) (unsigned long) utime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
Pierre Peifferc19384b2007-05-09 02:35:02 -07003546 return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547}
3548
Arnd Bergmannbdb116c2021-02-01 10:01:32 +00003549#ifdef CONFIG_COMPAT
3550/*
3551 * Fetch a robust-list pointer. Bit 0 signals PI futexes:
3552 */
3553static inline int
3554compat_fetch_robust_entry(compat_uptr_t *uentry, struct robust_list __user **entry,
3555 compat_uptr_t __user *head, unsigned int *pi)
3556{
3557 if (get_user(*uentry, head))
3558 return -EFAULT;
3559
3560 *entry = compat_ptr((*uentry) & ~1);
3561 *pi = (unsigned int)(*uentry) & 1;
3562
3563 return 0;
3564}
3565
3566static void __user *futex_uaddr(struct robust_list __user *entry,
3567 compat_long_t futex_offset)
3568{
3569 compat_uptr_t base = ptr_to_compat(entry);
3570 void __user *uaddr = compat_ptr(base + futex_offset);
3571
3572 return uaddr;
3573}
3574
3575/*
3576 * Walk curr->robust_list (very carefully, it's a userspace list!)
3577 * and mark any locks found there dead, and notify any waiters.
3578 *
3579 * We silently return on any sign of list-walking problem.
3580 */
3581void compat_exit_robust_list(struct task_struct *curr)
3582{
3583 struct compat_robust_list_head __user *head = curr->compat_robust_list;
3584 struct robust_list __user *entry, *next_entry, *pending;
3585 unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
3586 unsigned int uninitialized_var(next_pi);
3587 compat_uptr_t uentry, next_uentry, upending;
3588 compat_long_t futex_offset;
3589 int rc;
3590
3591 if (!futex_cmpxchg_enabled)
3592 return;
3593
3594 /*
3595 * Fetch the list head (which was registered earlier, via
3596 * sys_set_robust_list()):
3597 */
3598 if (compat_fetch_robust_entry(&uentry, &entry, &head->list.next, &pi))
3599 return;
3600 /*
3601 * Fetch the relative futex offset:
3602 */
3603 if (get_user(futex_offset, &head->futex_offset))
3604 return;
3605 /*
3606 * Fetch any possibly pending lock-add first, and handle it
3607 * if it exists:
3608 */
3609 if (compat_fetch_robust_entry(&upending, &pending,
3610 &head->list_op_pending, &pip))
3611 return;
3612
3613 next_entry = NULL; /* avoid warning with gcc */
3614 while (entry != (struct robust_list __user *) &head->list) {
3615 /*
3616 * Fetch the next entry in the list before calling
3617 * handle_futex_death:
3618 */
3619 rc = compat_fetch_robust_entry(&next_uentry, &next_entry,
3620 (compat_uptr_t __user *)&entry->next, &next_pi);
3621 /*
3622 * A pending lock might already be on the list, so
3623 * dont process it twice:
3624 */
3625 if (entry != pending) {
3626 void __user *uaddr = futex_uaddr(entry, futex_offset);
3627
3628 if (handle_futex_death(uaddr, curr, pi))
3629 return;
3630 }
3631 if (rc)
3632 return;
3633 uentry = next_uentry;
3634 entry = next_entry;
3635 pi = next_pi;
3636 /*
3637 * Avoid excessively long or circular lists:
3638 */
3639 if (!--limit)
3640 break;
3641
3642 cond_resched();
3643 }
3644 if (pending) {
3645 void __user *uaddr = futex_uaddr(pending, futex_offset);
3646
3647 handle_futex_death(uaddr, curr, pip);
3648 }
3649}
3650
3651COMPAT_SYSCALL_DEFINE2(set_robust_list,
3652 struct compat_robust_list_head __user *, head,
3653 compat_size_t, len)
3654{
3655 if (!futex_cmpxchg_enabled)
3656 return -ENOSYS;
3657
3658 if (unlikely(len != sizeof(*head)))
3659 return -EINVAL;
3660
3661 current->compat_robust_list = head;
3662
3663 return 0;
3664}
3665
3666COMPAT_SYSCALL_DEFINE3(get_robust_list, int, pid,
3667 compat_uptr_t __user *, head_ptr,
3668 compat_size_t __user *, len_ptr)
3669{
3670 struct compat_robust_list_head __user *head;
3671 unsigned long ret;
3672 struct task_struct *p;
3673
3674 if (!futex_cmpxchg_enabled)
3675 return -ENOSYS;
3676
3677 rcu_read_lock();
3678
3679 ret = -ESRCH;
3680 if (!pid)
3681 p = current;
3682 else {
3683 p = find_task_by_vpid(pid);
3684 if (!p)
3685 goto err_unlock;
3686 }
3687
3688 ret = -EPERM;
3689 if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS))
3690 goto err_unlock;
3691
3692 head = p->compat_robust_list;
3693 rcu_read_unlock();
3694
3695 if (put_user(sizeof(*head), len_ptr))
3696 return -EFAULT;
3697 return put_user(ptr_to_compat(head), head_ptr);
3698
3699err_unlock:
3700 rcu_read_unlock();
3701
3702 return ret;
3703}
3704
3705COMPAT_SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val,
3706 struct compat_timespec __user *, utime, u32 __user *, uaddr2,
3707 u32, val3)
3708{
3709 struct timespec ts;
3710 ktime_t t, *tp = NULL;
3711 int val2 = 0;
3712 int cmd = op & FUTEX_CMD_MASK;
3713
3714 if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI ||
3715 cmd == FUTEX_WAIT_BITSET ||
3716 cmd == FUTEX_WAIT_REQUEUE_PI)) {
3717 if (compat_get_timespec(&ts, utime))
3718 return -EFAULT;
3719 if (!timespec_valid(&ts))
3720 return -EINVAL;
3721
3722 t = timespec_to_ktime(ts);
3723 if (cmd == FUTEX_WAIT)
3724 t = ktime_add_safe(ktime_get(), t);
3725 tp = &t;
3726 }
3727 if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE ||
3728 cmd == FUTEX_CMP_REQUEUE_PI || cmd == FUTEX_WAKE_OP)
3729 val2 = (int) (unsigned long) utime;
3730
3731 return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
3732}
3733#endif /* CONFIG_COMPAT */
3734
Heiko Carstens03b8c7b2014-03-02 13:09:47 +01003735static void __init futex_detect_cmpxchg(void)
3736{
3737#ifndef CONFIG_HAVE_FUTEX_CMPXCHG
3738 u32 curval;
3739
3740 /*
3741 * This will fail and we want it. Some arch implementations do
3742 * runtime detection of the futex_atomic_cmpxchg_inatomic()
3743 * functionality. We want to know that before we call in any
3744 * of the complex code paths. Also we want to prevent
3745 * registration of robust lists in that case. NULL is
3746 * guaranteed to fault and we get -EFAULT on functional
3747 * implementation, the non-functional ones will return
3748 * -ENOSYS.
3749 */
3750 if (cmpxchg_futex_value_locked(&curval, NULL, 0, 0) == -EFAULT)
3751 futex_cmpxchg_enabled = 1;
3752#endif
3753}
3754
Benjamin Herrenschmidtf6d107f2008-03-27 14:52:15 +11003755static int __init futex_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756{
Heiko Carstens63b1a812014-01-16 14:54:50 +01003757 unsigned int futex_shift;
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -08003758 unsigned long i;
3759
3760#if CONFIG_BASE_SMALL
3761 futex_hashsize = 16;
3762#else
3763 futex_hashsize = roundup_pow_of_two(256 * num_possible_cpus());
3764#endif
3765
3766 futex_queues = alloc_large_system_hash("futex", sizeof(*futex_queues),
3767 futex_hashsize, 0,
3768 futex_hashsize < 256 ? HASH_SMALL : 0,
Heiko Carstens63b1a812014-01-16 14:54:50 +01003769 &futex_shift, NULL,
3770 futex_hashsize, futex_hashsize);
3771 futex_hashsize = 1UL << futex_shift;
Heiko Carstens03b8c7b2014-03-02 13:09:47 +01003772
3773 futex_detect_cmpxchg();
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08003774
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -08003775 for (i = 0; i < futex_hashsize; i++) {
Linus Torvalds11d46162014-03-20 22:11:17 -07003776 atomic_set(&futex_queues[i].waiters, 0);
Dima Zavin732375c2011-07-07 17:27:59 -07003777 plist_head_init(&futex_queues[i].chain);
Thomas Gleixner3e4ab742008-02-23 15:23:55 -08003778 spin_lock_init(&futex_queues[i].lock);
3779 }
3780
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 return 0;
3782}
Yang Yang808de342016-12-30 16:17:55 +08003783core_initcall(futex_init);