blob: 50f61d0e51b59321a2088582711c7b440446e585 [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
944 rt_mutex_unlock(&pi_state->pi_mutex);
945
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 Gleixner04e1b2e2014-06-11 20:45:40 +00001075/*
1076 * Lookup the task for the TID provided from user space and attach to
1077 * it after doing proper sanity checks.
1078 */
1079static int attach_to_pi_owner(u32 uval, union futex_key *key,
1080 struct futex_pi_state **ps)
Ingo Molnarc87e2832006-06-27 02:54:58 -07001081{
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001082 pid_t pid = uval & FUTEX_TID_MASK;
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001083 struct futex_pi_state *pi_state;
1084 struct task_struct *p;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001085
1086 /*
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001087 * We are the first waiter - try to look up the real owner and attach
Thomas Gleixner54a21782014-06-03 12:27:08 +00001088 * the new pi_state to it, but bail out when TID = 0 [1]
Ingo Molnarc87e2832006-06-27 02:54:58 -07001089 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001090 if (!pid)
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001091 return -ESRCH;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001092 p = futex_find_get_task(pid);
Michal Hocko7a0ea092010-06-30 09:51:19 +02001093 if (!p)
1094 return -ESRCH;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001095
Oleg Nesterova2129462015-02-02 15:05:36 +01001096 if (unlikely(p->flags & PF_KTHREAD)) {
Thomas Gleixnerf0d71b32014-05-12 20:45:35 +00001097 put_task_struct(p);
1098 return -EPERM;
1099 }
1100
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001101 /*
Thomas Gleixner2c116892021-02-01 10:01:34 +00001102 * We need to look at the task state to figure out, whether the
1103 * task is exiting. To protect against the change of the task state
1104 * in futex_exit_release(), we do this protected by p->pi_lock:
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001105 */
Thomas Gleixner1d615482009-11-17 14:54:03 +01001106 raw_spin_lock_irq(&p->pi_lock);
Thomas Gleixner2c116892021-02-01 10:01:34 +00001107 if (unlikely(p->futex_state != FUTEX_STATE_OK)) {
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001108 /*
Thomas Gleixner2c116892021-02-01 10:01:34 +00001109 * The task is on the way out. When the futex state is
1110 * FUTEX_STATE_DEAD, we know that the task has finished
1111 * the cleanup:
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001112 */
Thomas Gleixner2c116892021-02-01 10:01:34 +00001113 int ret = (p->futex_state = FUTEX_STATE_DEAD) ? -ESRCH : -EAGAIN;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001114
Thomas Gleixner1d615482009-11-17 14:54:03 +01001115 raw_spin_unlock_irq(&p->pi_lock);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001116 put_task_struct(p);
1117 return ret;
1118 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001119
Thomas Gleixner54a21782014-06-03 12:27:08 +00001120 /*
1121 * No existing pi state. First waiter. [2]
1122 */
Ingo Molnarc87e2832006-06-27 02:54:58 -07001123 pi_state = alloc_pi_state();
1124
1125 /*
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001126 * Initialize the pi_mutex in locked state and make @p
Ingo Molnarc87e2832006-06-27 02:54:58 -07001127 * the owner of it:
1128 */
1129 rt_mutex_init_proxy_locked(&pi_state->pi_mutex, p);
1130
1131 /* Store the key for possible exit cleanups: */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001132 pi_state->key = *key;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001133
Ingo Molnar627371d2006-07-29 05:16:20 +02001134 WARN_ON(!list_empty(&pi_state->list));
Ingo Molnarc87e2832006-06-27 02:54:58 -07001135 list_add(&pi_state->list, &p->pi_state_list);
1136 pi_state->owner = p;
Thomas Gleixner1d615482009-11-17 14:54:03 +01001137 raw_spin_unlock_irq(&p->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001138
1139 put_task_struct(p);
1140
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001141 *ps = pi_state;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001142
1143 return 0;
1144}
1145
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001146static int lookup_pi_state(u32 uval, struct futex_hash_bucket *hb,
1147 union futex_key *key, struct futex_pi_state **ps)
1148{
1149 struct futex_q *match = futex_top_waiter(hb, key);
1150
1151 /*
1152 * If there is a waiter on that futex, validate it and
1153 * attach to the pi_state when the validation succeeds.
1154 */
1155 if (match)
1156 return attach_to_pi_state(uval, match->pi_state, ps);
1157
1158 /*
1159 * We are the first waiter - try to look up the owner based on
1160 * @uval and attach to it.
1161 */
1162 return attach_to_pi_owner(uval, key, ps);
1163}
1164
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001165static int lock_pi_update_atomic(u32 __user *uaddr, u32 uval, u32 newval)
1166{
1167 u32 uninitialized_var(curval);
1168
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001169 if (unlikely(should_fail_futex(true)))
1170 return -EFAULT;
1171
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001172 if (unlikely(cmpxchg_futex_value_locked(&curval, uaddr, uval, newval)))
1173 return -EFAULT;
1174
1175 /*If user space value changed, let the caller retry */
1176 return curval != uval ? -EAGAIN : 0;
1177}
1178
Darren Hart1a520842009-04-03 13:39:52 -07001179/**
Darren Hartd96ee562009-09-21 22:30:22 -07001180 * futex_lock_pi_atomic() - Atomic work required to acquire a pi aware futex
Darren Hartbab5bc92009-04-07 23:23:50 -07001181 * @uaddr: the pi futex user address
1182 * @hb: the pi futex hash bucket
1183 * @key: the futex key associated with uaddr and hb
1184 * @ps: the pi_state pointer where we store the result of the
1185 * lookup
1186 * @task: the task to perform the atomic lock work for. This will
1187 * be "current" except in the case of requeue pi.
1188 * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0)
Darren Hart1a520842009-04-03 13:39:52 -07001189 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08001190 * Return:
1191 * 0 - ready to wait;
1192 * 1 - acquired the lock;
Darren Hart1a520842009-04-03 13:39:52 -07001193 * <0 - error
1194 *
1195 * The hb->lock and futex_key refs shall be held by the caller.
1196 */
1197static int futex_lock_pi_atomic(u32 __user *uaddr, struct futex_hash_bucket *hb,
1198 union futex_key *key,
1199 struct futex_pi_state **ps,
Darren Hartbab5bc92009-04-07 23:23:50 -07001200 struct task_struct *task, int set_waiters)
Darren Hart1a520842009-04-03 13:39:52 -07001201{
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001202 u32 uval, newval, vpid = task_pid_vnr(task);
1203 struct futex_q *match;
1204 int ret;
Darren Hart1a520842009-04-03 13:39:52 -07001205
1206 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001207 * Read the user space value first so we can validate a few
1208 * things before proceeding further.
Darren Hart1a520842009-04-03 13:39:52 -07001209 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001210 if (get_futex_value_locked(&uval, uaddr))
Darren Hart1a520842009-04-03 13:39:52 -07001211 return -EFAULT;
1212
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001213 if (unlikely(should_fail_futex(true)))
1214 return -EFAULT;
1215
Darren Hart1a520842009-04-03 13:39:52 -07001216 /*
1217 * Detect deadlocks.
1218 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001219 if ((unlikely((uval & FUTEX_TID_MASK) == vpid)))
Darren Hart1a520842009-04-03 13:39:52 -07001220 return -EDEADLK;
1221
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001222 if ((unlikely(should_fail_futex(true))))
1223 return -EDEADLK;
1224
Darren Hart1a520842009-04-03 13:39:52 -07001225 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001226 * Lookup existing state first. If it exists, try to attach to
1227 * its pi_state.
Darren Hart1a520842009-04-03 13:39:52 -07001228 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001229 match = futex_top_waiter(hb, key);
1230 if (match)
1231 return attach_to_pi_state(uval, match->pi_state, ps);
1232
1233 /*
1234 * No waiter and user TID is 0. We are here because the
1235 * waiters or the owner died bit is set or called from
1236 * requeue_cmp_pi or for whatever reason something took the
1237 * syscall.
1238 */
1239 if (!(uval & FUTEX_TID_MASK)) {
Thomas Gleixnerb3eaa9f2014-06-03 12:27:06 +00001240 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001241 * We take over the futex. No other waiters and the user space
1242 * TID is 0. We preserve the owner died bit.
Thomas Gleixnerb3eaa9f2014-06-03 12:27:06 +00001243 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001244 newval = uval & FUTEX_OWNER_DIED;
1245 newval |= vpid;
1246
1247 /* The futex requeue_pi code can enforce the waiters bit */
1248 if (set_waiters)
1249 newval |= FUTEX_WAITERS;
1250
1251 ret = lock_pi_update_atomic(uaddr, uval, newval);
1252 /* If the take over worked, return 1 */
1253 return ret < 0 ? ret : 1;
Thomas Gleixnerb3eaa9f2014-06-03 12:27:06 +00001254 }
Darren Hart1a520842009-04-03 13:39:52 -07001255
Darren Hart1a520842009-04-03 13:39:52 -07001256 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001257 * First waiter. Set the waiters bit before attaching ourself to
1258 * the owner. If owner tries to unlock, it will be forced into
1259 * the kernel and blocked on hb->lock.
Darren Hart1a520842009-04-03 13:39:52 -07001260 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001261 newval = uval | FUTEX_WAITERS;
1262 ret = lock_pi_update_atomic(uaddr, uval, newval);
1263 if (ret)
1264 return ret;
Darren Hart1a520842009-04-03 13:39:52 -07001265 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001266 * If the update of the user space value succeeded, we try to
1267 * attach to the owner. If that fails, no harm done, we only
1268 * set the FUTEX_WAITERS bit in the user space variable.
Darren Hart1a520842009-04-03 13:39:52 -07001269 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001270 return attach_to_pi_owner(uval, key, ps);
Darren Hart1a520842009-04-03 13:39:52 -07001271}
1272
Lai Jiangshan2e129782010-12-22 14:18:50 +08001273/**
1274 * __unqueue_futex() - Remove the futex_q from its futex_hash_bucket
1275 * @q: The futex_q to unqueue
1276 *
1277 * The q->lock_ptr must not be NULL and must be held by the caller.
1278 */
1279static void __unqueue_futex(struct futex_q *q)
1280{
1281 struct futex_hash_bucket *hb;
1282
Steven Rostedt29096202011-03-17 15:21:07 -04001283 if (WARN_ON_SMP(!q->lock_ptr || !spin_is_locked(q->lock_ptr))
1284 || WARN_ON(plist_node_empty(&q->list)))
Lai Jiangshan2e129782010-12-22 14:18:50 +08001285 return;
1286
1287 hb = container_of(q->lock_ptr, struct futex_hash_bucket, lock);
1288 plist_del(&q->list, &hb->chain);
Linus Torvalds11d46162014-03-20 22:11:17 -07001289 hb_waiters_dec(hb);
Lai Jiangshan2e129782010-12-22 14:18:50 +08001290}
1291
Ingo Molnarc87e2832006-06-27 02:54:58 -07001292/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 * The hash bucket lock must be held when this is called.
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001294 * Afterwards, the futex_q must not be accessed. Callers
1295 * must ensure to later call wake_up_q() for the actual
1296 * wakeups to occur.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 */
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001298static void mark_wake_futex(struct wake_q_head *wake_q, struct futex_q *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001300 struct task_struct *p = q->task;
1301
Darren Hartaa109902012-11-26 16:29:56 -08001302 if (WARN(q->pi_state || q->rt_waiter, "refusing to wake PI futex\n"))
1303 return;
1304
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001305 /*
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001306 * Queue the task for later wakeup for after we've released
1307 * the hb->lock. wake_q_add() grabs reference to p.
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001308 */
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001309 wake_q_add(wake_q, p);
Lai Jiangshan2e129782010-12-22 14:18:50 +08001310 __unqueue_futex(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 /*
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001312 * The waiting task can free the futex_q as soon as
1313 * q->lock_ptr = NULL is written, without taking any locks. A
1314 * memory barrier is required here to prevent the following
1315 * store to lock_ptr from getting ahead of the plist_del.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 */
Ralf Baechleccdea2f2006-12-06 20:40:26 -08001317 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 q->lock_ptr = NULL;
1319}
1320
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001321static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this,
1322 struct futex_hash_bucket *hb)
Ingo Molnarc87e2832006-06-27 02:54:58 -07001323{
1324 struct task_struct *new_owner;
1325 struct futex_pi_state *pi_state = this->pi_state;
Vitaliy Ivanov7cfdaf32011-07-07 15:10:31 +03001326 u32 uninitialized_var(curval), newval;
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001327 WAKE_Q(wake_q);
1328 bool deboost;
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001329 int ret = 0;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001330
1331 if (!pi_state)
1332 return -EINVAL;
1333
Thomas Gleixner51246bf2010-02-02 11:40:27 +01001334 /*
1335 * If current does not own the pi_state then the futex is
1336 * inconsistent and user space fiddled with the futex value.
1337 */
1338 if (pi_state->owner != current)
1339 return -EINVAL;
1340
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001341 raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001342 new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);
1343
1344 /*
Steven Rostedtf123c982011-01-06 15:08:29 -05001345 * It is possible that the next waiter (the one that brought
1346 * this owner to the kernel) timed out and is no longer
1347 * waiting on the lock.
Ingo Molnarc87e2832006-06-27 02:54:58 -07001348 */
1349 if (!new_owner)
1350 new_owner = this->task;
1351
1352 /*
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001353 * We pass it to the next owner. The WAITERS bit is always
1354 * kept enabled while there is PI state around. We cleanup the
1355 * owner died bit, because we are the owner.
Ingo Molnarc87e2832006-06-27 02:54:58 -07001356 */
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001357 newval = FUTEX_WAITERS | task_pid_vnr(new_owner);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001358
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001359 if (unlikely(should_fail_futex(true)))
1360 ret = -EFAULT;
1361
Sebastian Andrzej Siewior89e9e662016-04-15 14:35:39 +02001362 if (cmpxchg_futex_value_locked(&curval, uaddr, uval, newval)) {
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001363 ret = -EFAULT;
Sebastian Andrzej Siewior89e9e662016-04-15 14:35:39 +02001364 } else if (curval != uval) {
1365 /*
1366 * If a unconditional UNLOCK_PI operation (user space did not
1367 * try the TID->0 transition) raced with a waiter setting the
1368 * FUTEX_WAITERS flag between get_user() and locking the hash
1369 * bucket lock, retry the operation.
1370 */
1371 if ((FUTEX_TID_MASK & curval) == uval)
1372 ret = -EAGAIN;
1373 else
1374 ret = -EINVAL;
1375 }
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001376 if (ret) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001377 raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001378 return ret;
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001379 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001380
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001381 raw_spin_lock(&pi_state->owner->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +02001382 WARN_ON(list_empty(&pi_state->list));
1383 list_del_init(&pi_state->list);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001384 raw_spin_unlock(&pi_state->owner->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +02001385
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001386 raw_spin_lock(&new_owner->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +02001387 WARN_ON(!list_empty(&pi_state->list));
Ingo Molnarc87e2832006-06-27 02:54:58 -07001388 list_add(&pi_state->list, &new_owner->pi_state_list);
1389 pi_state->owner = new_owner;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001390 raw_spin_unlock(&new_owner->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +02001391
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001392 raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001393
1394 deboost = rt_mutex_futex_unlock(&pi_state->pi_mutex, &wake_q);
1395
1396 /*
1397 * First unlock HB so the waiter does not spin on it once he got woken
1398 * up. Second wake up the waiter before the priority is adjusted. If we
1399 * deboost first (and lose our higher priority), then the task might get
1400 * scheduled away before the wake up can take place.
1401 */
1402 spin_unlock(&hb->lock);
1403 wake_up_q(&wake_q);
1404 if (deboost)
1405 rt_mutex_adjust_prio(current);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001406
1407 return 0;
1408}
1409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410/*
Ingo Molnar8b8f3192006-07-03 00:25:05 -07001411 * Express the locking dependencies for lockdep:
1412 */
1413static inline void
1414double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
1415{
1416 if (hb1 <= hb2) {
1417 spin_lock(&hb1->lock);
1418 if (hb1 < hb2)
1419 spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING);
1420 } else { /* hb1 > hb2 */
1421 spin_lock(&hb2->lock);
1422 spin_lock_nested(&hb1->lock, SINGLE_DEPTH_NESTING);
1423 }
1424}
1425
Darren Hart5eb3dc62009-03-12 00:55:52 -07001426static inline void
1427double_unlock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
1428{
Darren Hartf061d352009-03-12 15:11:18 -07001429 spin_unlock(&hb1->lock);
Ingo Molnar88f502f2009-03-13 10:32:07 +01001430 if (hb1 != hb2)
1431 spin_unlock(&hb2->lock);
Darren Hart5eb3dc62009-03-12 00:55:52 -07001432}
1433
Ingo Molnar8b8f3192006-07-03 00:25:05 -07001434/*
Darren Hartb2d09942009-03-12 00:55:37 -07001435 * Wake up waiters matching bitset queued on this futex (uaddr).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 */
Darren Hartb41277d2010-11-08 13:10:09 -08001437static int
1438futex_wake(u32 __user *uaddr, unsigned int flags, int nr_wake, u32 bitset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
Ingo Molnare2970f22006-06-27 02:54:47 -07001440 struct futex_hash_bucket *hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 struct futex_q *this, *next;
Peter Zijlstra38d47c12008-09-26 19:32:20 +02001442 union futex_key key = FUTEX_KEY_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 int ret;
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001444 WAKE_Q(wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Thomas Gleixnercd689982008-02-01 17:45:14 +01001446 if (!bitset)
1447 return -EINVAL;
1448
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001449 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 if (unlikely(ret != 0))
1451 goto out;
1452
Ingo Molnare2970f22006-06-27 02:54:47 -07001453 hb = hash_futex(&key);
Davidlohr Buesob0c29f72014-01-12 15:31:25 -08001454
1455 /* Make sure we really have tasks to wakeup */
1456 if (!hb_waiters_pending(hb))
1457 goto out_put_key;
1458
Ingo Molnare2970f22006-06-27 02:54:47 -07001459 spin_lock(&hb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Jason Low0d00c7b2014-01-12 15:31:22 -08001461 plist_for_each_entry_safe(this, next, &hb->chain, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 if (match_futex (&this->key, &key)) {
Darren Hart52400ba2009-04-03 13:40:49 -07001463 if (this->pi_state || this->rt_waiter) {
Ingo Molnared6f7b12006-07-01 04:35:46 -07001464 ret = -EINVAL;
1465 break;
1466 }
Thomas Gleixnercd689982008-02-01 17:45:14 +01001467
1468 /* Check if one of the bits is set in both bitsets */
1469 if (!(this->bitset & bitset))
1470 continue;
1471
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001472 mark_wake_futex(&wake_q, this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 if (++ret >= nr_wake)
1474 break;
1475 }
1476 }
1477
Ingo Molnare2970f22006-06-27 02:54:47 -07001478 spin_unlock(&hb->lock);
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001479 wake_up_q(&wake_q);
Davidlohr Buesob0c29f72014-01-12 15:31:25 -08001480out_put_key:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001481 put_futex_key(&key);
Darren Hart42d35d42008-12-29 15:49:53 -08001482out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 return ret;
1484}
1485
Jiri Slaby81da9f82017-08-24 09:31:05 +02001486static int futex_atomic_op_inuser(unsigned int encoded_op, u32 __user *uaddr)
1487{
1488 unsigned int op = (encoded_op & 0x70000000) >> 28;
1489 unsigned int cmp = (encoded_op & 0x0f000000) >> 24;
Jiri Slabya1640092017-11-30 15:35:44 +01001490 int oparg = sign_extend32((encoded_op & 0x00fff000) >> 12, 11);
1491 int cmparg = sign_extend32(encoded_op & 0x00000fff, 11);
Jiri Slaby81da9f82017-08-24 09:31:05 +02001492 int oldval, ret;
1493
1494 if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28)) {
Jiri Slabyda92e8a2017-10-23 13:41:51 +02001495 if (oparg < 0 || oparg > 31) {
1496 char comm[sizeof(current->comm)];
1497 /*
1498 * kill this print and return -EINVAL when userspace
1499 * is sane again
1500 */
1501 pr_info_ratelimited("futex_wake_op: %s tries to shift op by %d; fix this program\n",
1502 get_task_comm(comm, current), oparg);
1503 oparg &= 31;
1504 }
Jiri Slaby81da9f82017-08-24 09:31:05 +02001505 oparg = 1 << oparg;
1506 }
1507
1508 if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
1509 return -EFAULT;
1510
1511 ret = arch_futex_atomic_op_inuser(op, oparg, &oldval, uaddr);
1512 if (ret)
1513 return ret;
1514
1515 switch (cmp) {
1516 case FUTEX_OP_CMP_EQ:
1517 return oldval == cmparg;
1518 case FUTEX_OP_CMP_NE:
1519 return oldval != cmparg;
1520 case FUTEX_OP_CMP_LT:
1521 return oldval < cmparg;
1522 case FUTEX_OP_CMP_GE:
1523 return oldval >= cmparg;
1524 case FUTEX_OP_CMP_LE:
1525 return oldval <= cmparg;
1526 case FUTEX_OP_CMP_GT:
1527 return oldval > cmparg;
1528 default:
1529 return -ENOSYS;
1530 }
1531}
1532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533/*
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001534 * Wake up all waiters hashed on the physical page that is mapped
1535 * to this virtual address:
1536 */
Ingo Molnare2970f22006-06-27 02:54:47 -07001537static int
Darren Hartb41277d2010-11-08 13:10:09 -08001538futex_wake_op(u32 __user *uaddr1, unsigned int flags, u32 __user *uaddr2,
Ingo Molnare2970f22006-06-27 02:54:47 -07001539 int nr_wake, int nr_wake2, int op)
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001540{
Peter Zijlstra38d47c12008-09-26 19:32:20 +02001541 union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
Ingo Molnare2970f22006-06-27 02:54:47 -07001542 struct futex_hash_bucket *hb1, *hb2;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001543 struct futex_q *this, *next;
Darren Harte4dc5b72009-03-12 00:56:13 -07001544 int ret, op_ret;
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001545 WAKE_Q(wake_q);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001546
Darren Harte4dc5b72009-03-12 00:56:13 -07001547retry:
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001548 ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, VERIFY_READ);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001549 if (unlikely(ret != 0))
1550 goto out;
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001551 ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001552 if (unlikely(ret != 0))
Darren Hart42d35d42008-12-29 15:49:53 -08001553 goto out_put_key1;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001554
Ingo Molnare2970f22006-06-27 02:54:47 -07001555 hb1 = hash_futex(&key1);
1556 hb2 = hash_futex(&key2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001557
Darren Harte4dc5b72009-03-12 00:56:13 -07001558retry_private:
Thomas Gleixnereaaea802009-10-04 09:34:17 +02001559 double_lock_hb(hb1, hb2);
Ingo Molnare2970f22006-06-27 02:54:47 -07001560 op_ret = futex_atomic_op_inuser(op, uaddr2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001561 if (unlikely(op_ret < 0)) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001562
Darren Hart5eb3dc62009-03-12 00:55:52 -07001563 double_unlock_hb(hb1, hb2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001564
David Howells7ee1dd32006-01-06 00:11:44 -08001565#ifndef CONFIG_MMU
Ingo Molnare2970f22006-06-27 02:54:47 -07001566 /*
1567 * we don't get EFAULT from MMU faults if we don't have an MMU,
1568 * but we might get them from range checking
1569 */
David Howells7ee1dd32006-01-06 00:11:44 -08001570 ret = op_ret;
Darren Hart42d35d42008-12-29 15:49:53 -08001571 goto out_put_keys;
David Howells7ee1dd32006-01-06 00:11:44 -08001572#endif
1573
David Gibson796f8d92005-11-07 00:59:33 -08001574 if (unlikely(op_ret != -EFAULT)) {
1575 ret = op_ret;
Darren Hart42d35d42008-12-29 15:49:53 -08001576 goto out_put_keys;
David Gibson796f8d92005-11-07 00:59:33 -08001577 }
1578
Thomas Gleixnerd0725992009-06-11 23:15:43 +02001579 ret = fault_in_user_writeable(uaddr2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001580 if (ret)
Darren Hartde87fcc2009-03-12 00:55:46 -07001581 goto out_put_keys;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001582
Darren Hartb41277d2010-11-08 13:10:09 -08001583 if (!(flags & FLAGS_SHARED))
Darren Harte4dc5b72009-03-12 00:56:13 -07001584 goto retry_private;
1585
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001586 put_futex_key(&key2);
1587 put_futex_key(&key1);
Darren Harte4dc5b72009-03-12 00:56:13 -07001588 goto retry;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001589 }
1590
Jason Low0d00c7b2014-01-12 15:31:22 -08001591 plist_for_each_entry_safe(this, next, &hb1->chain, list) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001592 if (match_futex (&this->key, &key1)) {
Darren Hartaa109902012-11-26 16:29:56 -08001593 if (this->pi_state || this->rt_waiter) {
1594 ret = -EINVAL;
1595 goto out_unlock;
1596 }
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001597 mark_wake_futex(&wake_q, this);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001598 if (++ret >= nr_wake)
1599 break;
1600 }
1601 }
1602
1603 if (op_ret > 0) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001604 op_ret = 0;
Jason Low0d00c7b2014-01-12 15:31:22 -08001605 plist_for_each_entry_safe(this, next, &hb2->chain, list) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001606 if (match_futex (&this->key, &key2)) {
Darren Hartaa109902012-11-26 16:29:56 -08001607 if (this->pi_state || this->rt_waiter) {
1608 ret = -EINVAL;
1609 goto out_unlock;
1610 }
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001611 mark_wake_futex(&wake_q, this);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001612 if (++op_ret >= nr_wake2)
1613 break;
1614 }
1615 }
1616 ret += op_ret;
1617 }
1618
Darren Hartaa109902012-11-26 16:29:56 -08001619out_unlock:
Darren Hart5eb3dc62009-03-12 00:55:52 -07001620 double_unlock_hb(hb1, hb2);
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001621 wake_up_q(&wake_q);
Darren Hart42d35d42008-12-29 15:49:53 -08001622out_put_keys:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001623 put_futex_key(&key2);
Darren Hart42d35d42008-12-29 15:49:53 -08001624out_put_key1:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001625 put_futex_key(&key1);
Darren Hart42d35d42008-12-29 15:49:53 -08001626out:
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001627 return ret;
1628}
1629
Darren Hart9121e472009-04-03 13:40:31 -07001630/**
1631 * requeue_futex() - Requeue a futex_q from one hb to another
1632 * @q: the futex_q to requeue
1633 * @hb1: the source hash_bucket
1634 * @hb2: the target hash_bucket
1635 * @key2: the new key for the requeued futex_q
1636 */
1637static inline
1638void requeue_futex(struct futex_q *q, struct futex_hash_bucket *hb1,
1639 struct futex_hash_bucket *hb2, union futex_key *key2)
1640{
1641
1642 /*
1643 * If key1 and key2 hash to the same bucket, no need to
1644 * requeue.
1645 */
1646 if (likely(&hb1->chain != &hb2->chain)) {
1647 plist_del(&q->list, &hb1->chain);
Linus Torvalds11d46162014-03-20 22:11:17 -07001648 hb_waiters_dec(hb1);
Linus Torvalds11d46162014-03-20 22:11:17 -07001649 hb_waiters_inc(hb2);
Davidlohr Buesofe1bce92016-04-20 20:09:24 -07001650 plist_add(&q->list, &hb2->chain);
Darren Hart9121e472009-04-03 13:40:31 -07001651 q->lock_ptr = &hb2->lock;
Darren Hart9121e472009-04-03 13:40:31 -07001652 }
1653 get_futex_key_refs(key2);
1654 q->key = *key2;
1655}
1656
Darren Hart52400ba2009-04-03 13:40:49 -07001657/**
1658 * requeue_pi_wake_futex() - Wake a task that acquired the lock during requeue
Darren Hartd96ee562009-09-21 22:30:22 -07001659 * @q: the futex_q
1660 * @key: the key of the requeue target futex
1661 * @hb: the hash_bucket of the requeue target futex
Darren Hart52400ba2009-04-03 13:40:49 -07001662 *
1663 * During futex_requeue, with requeue_pi=1, it is possible to acquire the
1664 * target futex if it is uncontended or via a lock steal. Set the futex_q key
1665 * to the requeue target futex so the waiter can detect the wakeup on the right
1666 * futex, but remove it from the hb and NULL the rt_waiter so it can detect
Darren Hartbeda2c72009-08-09 15:34:39 -07001667 * atomic lock acquisition. Set the q->lock_ptr to the requeue target hb->lock
1668 * to protect access to the pi_state to fixup the owner later. Must be called
1669 * with both q->lock_ptr and hb->lock held.
Darren Hart52400ba2009-04-03 13:40:49 -07001670 */
1671static inline
Darren Hartbeda2c72009-08-09 15:34:39 -07001672void requeue_pi_wake_futex(struct futex_q *q, union futex_key *key,
1673 struct futex_hash_bucket *hb)
Darren Hart52400ba2009-04-03 13:40:49 -07001674{
Darren Hart52400ba2009-04-03 13:40:49 -07001675 get_futex_key_refs(key);
1676 q->key = *key;
1677
Lai Jiangshan2e129782010-12-22 14:18:50 +08001678 __unqueue_futex(q);
Darren Hart52400ba2009-04-03 13:40:49 -07001679
1680 WARN_ON(!q->rt_waiter);
1681 q->rt_waiter = NULL;
1682
Darren Hartbeda2c72009-08-09 15:34:39 -07001683 q->lock_ptr = &hb->lock;
Darren Hartbeda2c72009-08-09 15:34:39 -07001684
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001685 wake_up_state(q->task, TASK_NORMAL);
Darren Hart52400ba2009-04-03 13:40:49 -07001686}
1687
1688/**
1689 * futex_proxy_trylock_atomic() - Attempt an atomic lock for the top waiter
Darren Hartbab5bc92009-04-07 23:23:50 -07001690 * @pifutex: the user address of the to futex
1691 * @hb1: the from futex hash bucket, must be locked by the caller
1692 * @hb2: the to futex hash bucket, must be locked by the caller
1693 * @key1: the from futex key
1694 * @key2: the to futex key
1695 * @ps: address to store the pi_state pointer
1696 * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0)
Darren Hart52400ba2009-04-03 13:40:49 -07001697 *
1698 * Try and get the lock on behalf of the top waiter if we can do it atomically.
Darren Hartbab5bc92009-04-07 23:23:50 -07001699 * Wake the top waiter if we succeed. If the caller specified set_waiters,
1700 * then direct futex_lock_pi_atomic() to force setting the FUTEX_WAITERS bit.
1701 * hb1 and hb2 must be held by the caller.
Darren Hart52400ba2009-04-03 13:40:49 -07001702 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08001703 * Return:
1704 * 0 - failed to acquire the lock atomically;
Thomas Gleixner866293e2014-05-12 20:45:34 +00001705 * >0 - acquired the lock, return value is vpid of the top_waiter
Darren Hart52400ba2009-04-03 13:40:49 -07001706 * <0 - error
1707 */
1708static int futex_proxy_trylock_atomic(u32 __user *pifutex,
1709 struct futex_hash_bucket *hb1,
1710 struct futex_hash_bucket *hb2,
1711 union futex_key *key1, union futex_key *key2,
Darren Hartbab5bc92009-04-07 23:23:50 -07001712 struct futex_pi_state **ps, int set_waiters)
Darren Hart52400ba2009-04-03 13:40:49 -07001713{
Darren Hartbab5bc92009-04-07 23:23:50 -07001714 struct futex_q *top_waiter = NULL;
Darren Hart52400ba2009-04-03 13:40:49 -07001715 u32 curval;
Thomas Gleixner866293e2014-05-12 20:45:34 +00001716 int ret, vpid;
Darren Hart52400ba2009-04-03 13:40:49 -07001717
1718 if (get_futex_value_locked(&curval, pifutex))
1719 return -EFAULT;
1720
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001721 if (unlikely(should_fail_futex(true)))
1722 return -EFAULT;
1723
Darren Hartbab5bc92009-04-07 23:23:50 -07001724 /*
1725 * Find the top_waiter and determine if there are additional waiters.
1726 * If the caller intends to requeue more than 1 waiter to pifutex,
1727 * force futex_lock_pi_atomic() to set the FUTEX_WAITERS bit now,
1728 * as we have means to handle the possible fault. If not, don't set
1729 * the bit unecessarily as it will force the subsequent unlock to enter
1730 * the kernel.
1731 */
Darren Hart52400ba2009-04-03 13:40:49 -07001732 top_waiter = futex_top_waiter(hb1, key1);
1733
1734 /* There are no waiters, nothing for us to do. */
1735 if (!top_waiter)
1736 return 0;
1737
Darren Hart84bc4af2009-08-13 17:36:53 -07001738 /* Ensure we requeue to the expected futex. */
1739 if (!match_futex(top_waiter->requeue_pi_key, key2))
1740 return -EINVAL;
1741
Darren Hart52400ba2009-04-03 13:40:49 -07001742 /*
Darren Hartbab5bc92009-04-07 23:23:50 -07001743 * Try to take the lock for top_waiter. Set the FUTEX_WAITERS bit in
1744 * the contended case or if set_waiters is 1. The pi_state is returned
1745 * in ps in contended cases.
Darren Hart52400ba2009-04-03 13:40:49 -07001746 */
Thomas Gleixner866293e2014-05-12 20:45:34 +00001747 vpid = task_pid_vnr(top_waiter->task);
Darren Hartbab5bc92009-04-07 23:23:50 -07001748 ret = futex_lock_pi_atomic(pifutex, hb2, key2, ps, top_waiter->task,
1749 set_waiters);
Thomas Gleixner866293e2014-05-12 20:45:34 +00001750 if (ret == 1) {
Darren Hartbeda2c72009-08-09 15:34:39 -07001751 requeue_pi_wake_futex(top_waiter, key2, hb2);
Thomas Gleixner866293e2014-05-12 20:45:34 +00001752 return vpid;
1753 }
Darren Hart52400ba2009-04-03 13:40:49 -07001754 return ret;
1755}
1756
1757/**
1758 * futex_requeue() - Requeue waiters from uaddr1 to uaddr2
Randy Dunlapfb62db22010-10-13 11:02:34 -07001759 * @uaddr1: source futex user address
Darren Hartb41277d2010-11-08 13:10:09 -08001760 * @flags: futex flags (FLAGS_SHARED, etc.)
Randy Dunlapfb62db22010-10-13 11:02:34 -07001761 * @uaddr2: target futex user address
1762 * @nr_wake: number of waiters to wake (must be 1 for requeue_pi)
1763 * @nr_requeue: number of waiters to requeue (0-INT_MAX)
1764 * @cmpval: @uaddr1 expected value (or %NULL)
1765 * @requeue_pi: if we are attempting to requeue from a non-pi futex to a
Darren Hartb41277d2010-11-08 13:10:09 -08001766 * pi futex (pi to pi requeue is not supported)
Darren Hart52400ba2009-04-03 13:40:49 -07001767 *
1768 * Requeue waiters on uaddr1 to uaddr2. In the requeue_pi case, try to acquire
1769 * uaddr2 atomically on behalf of the top waiter.
1770 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08001771 * Return:
1772 * >=0 - on success, the number of tasks requeued or woken;
Darren Hart52400ba2009-04-03 13:40:49 -07001773 * <0 - on error
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 */
Darren Hartb41277d2010-11-08 13:10:09 -08001775static int futex_requeue(u32 __user *uaddr1, unsigned int flags,
1776 u32 __user *uaddr2, int nr_wake, int nr_requeue,
1777 u32 *cmpval, int requeue_pi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778{
Peter Zijlstra38d47c12008-09-26 19:32:20 +02001779 union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
Darren Hart52400ba2009-04-03 13:40:49 -07001780 int drop_count = 0, task_count = 0, ret;
1781 struct futex_pi_state *pi_state = NULL;
Ingo Molnare2970f22006-06-27 02:54:47 -07001782 struct futex_hash_bucket *hb1, *hb2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 struct futex_q *this, *next;
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001784 WAKE_Q(wake_q);
Darren Hart52400ba2009-04-03 13:40:49 -07001785
Li Jinyued8a31702017-12-14 17:04:54 +08001786 if (nr_wake < 0 || nr_requeue < 0)
1787 return -EINVAL;
1788
Darren Hart52400ba2009-04-03 13:40:49 -07001789 if (requeue_pi) {
1790 /*
Thomas Gleixnere9c243a2014-06-03 12:27:06 +00001791 * Requeue PI only works on two distinct uaddrs. This
1792 * check is only valid for private futexes. See below.
1793 */
1794 if (uaddr1 == uaddr2)
1795 return -EINVAL;
1796
1797 /*
Darren Hart52400ba2009-04-03 13:40:49 -07001798 * requeue_pi requires a pi_state, try to allocate it now
1799 * without any locks in case it fails.
1800 */
1801 if (refill_pi_state_cache())
1802 return -ENOMEM;
1803 /*
1804 * requeue_pi must wake as many tasks as it can, up to nr_wake
1805 * + nr_requeue, since it acquires the rt_mutex prior to
1806 * returning to userspace, so as to not leave the rt_mutex with
1807 * waiters and no owner. However, second and third wake-ups
1808 * cannot be predicted as they involve race conditions with the
1809 * first wake and a fault while looking up the pi_state. Both
1810 * pthread_cond_signal() and pthread_cond_broadcast() should
1811 * use nr_wake=1.
1812 */
1813 if (nr_wake != 1)
1814 return -EINVAL;
1815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Darren Hart42d35d42008-12-29 15:49:53 -08001817retry:
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001818 ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 if (unlikely(ret != 0))
1820 goto out;
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001821 ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2,
1822 requeue_pi ? VERIFY_WRITE : VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 if (unlikely(ret != 0))
Darren Hart42d35d42008-12-29 15:49:53 -08001824 goto out_put_key1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Thomas Gleixnere9c243a2014-06-03 12:27:06 +00001826 /*
1827 * The check above which compares uaddrs is not sufficient for
1828 * shared futexes. We need to compare the keys:
1829 */
1830 if (requeue_pi && match_futex(&key1, &key2)) {
1831 ret = -EINVAL;
1832 goto out_put_keys;
1833 }
1834
Ingo Molnare2970f22006-06-27 02:54:47 -07001835 hb1 = hash_futex(&key1);
1836 hb2 = hash_futex(&key2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Darren Harte4dc5b72009-03-12 00:56:13 -07001838retry_private:
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07001839 hb_waiters_inc(hb2);
Ingo Molnar8b8f3192006-07-03 00:25:05 -07001840 double_lock_hb(hb1, hb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Ingo Molnare2970f22006-06-27 02:54:47 -07001842 if (likely(cmpval != NULL)) {
1843 u32 curval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Ingo Molnare2970f22006-06-27 02:54:47 -07001845 ret = get_futex_value_locked(&curval, uaddr1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
1847 if (unlikely(ret)) {
Darren Hart5eb3dc62009-03-12 00:55:52 -07001848 double_unlock_hb(hb1, hb2);
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07001849 hb_waiters_dec(hb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Darren Harte4dc5b72009-03-12 00:56:13 -07001851 ret = get_user(curval, uaddr1);
1852 if (ret)
1853 goto out_put_keys;
1854
Darren Hartb41277d2010-11-08 13:10:09 -08001855 if (!(flags & FLAGS_SHARED))
Darren Harte4dc5b72009-03-12 00:56:13 -07001856 goto retry_private;
1857
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001858 put_futex_key(&key2);
1859 put_futex_key(&key1);
Darren Harte4dc5b72009-03-12 00:56:13 -07001860 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 }
Ingo Molnare2970f22006-06-27 02:54:47 -07001862 if (curval != *cmpval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 ret = -EAGAIN;
1864 goto out_unlock;
1865 }
1866 }
1867
Darren Hart52400ba2009-04-03 13:40:49 -07001868 if (requeue_pi && (task_count - nr_wake < nr_requeue)) {
Darren Hartbab5bc92009-04-07 23:23:50 -07001869 /*
1870 * Attempt to acquire uaddr2 and wake the top waiter. If we
1871 * intend to requeue waiters, force setting the FUTEX_WAITERS
1872 * bit. We force this here where we are able to easily handle
1873 * faults rather in the requeue loop below.
1874 */
Darren Hart52400ba2009-04-03 13:40:49 -07001875 ret = futex_proxy_trylock_atomic(uaddr2, hb1, hb2, &key1,
Darren Hartbab5bc92009-04-07 23:23:50 -07001876 &key2, &pi_state, nr_requeue);
Darren Hart52400ba2009-04-03 13:40:49 -07001877
1878 /*
1879 * At this point the top_waiter has either taken uaddr2 or is
1880 * waiting on it. If the former, then the pi_state will not
1881 * exist yet, look it up one more time to ensure we have a
Thomas Gleixner866293e2014-05-12 20:45:34 +00001882 * reference to it. If the lock was taken, ret contains the
1883 * vpid of the top waiter task.
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00001884 * If the lock was not taken, we have pi_state and an initial
1885 * refcount on it. In case of an error we have nothing.
Darren Hart52400ba2009-04-03 13:40:49 -07001886 */
Thomas Gleixner866293e2014-05-12 20:45:34 +00001887 if (ret > 0) {
Darren Hart52400ba2009-04-03 13:40:49 -07001888 WARN_ON(pi_state);
Darren Hart89061d32009-10-15 15:30:48 -07001889 drop_count++;
Darren Hart52400ba2009-04-03 13:40:49 -07001890 task_count++;
Thomas Gleixner866293e2014-05-12 20:45:34 +00001891 /*
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00001892 * If we acquired the lock, then the user space value
1893 * of uaddr2 should be vpid. It cannot be changed by
1894 * the top waiter as it is blocked on hb2 lock if it
1895 * tries to do so. If something fiddled with it behind
1896 * our back the pi state lookup might unearth it. So
1897 * we rather use the known value than rereading and
1898 * handing potential crap to lookup_pi_state.
1899 *
1900 * If that call succeeds then we have pi_state and an
1901 * initial refcount on it.
Thomas Gleixner866293e2014-05-12 20:45:34 +00001902 */
Thomas Gleixner54a21782014-06-03 12:27:08 +00001903 ret = lookup_pi_state(ret, hb2, &key2, &pi_state);
Darren Hart52400ba2009-04-03 13:40:49 -07001904 }
1905
1906 switch (ret) {
1907 case 0:
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00001908 /* We hold a reference on the pi state. */
Darren Hart52400ba2009-04-03 13:40:49 -07001909 break;
Thomas Gleixner4959f2d2015-12-19 20:07:40 +00001910
1911 /* If the above failed, then pi_state is NULL */
Darren Hart52400ba2009-04-03 13:40:49 -07001912 case -EFAULT:
1913 double_unlock_hb(hb1, hb2);
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07001914 hb_waiters_dec(hb2);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001915 put_futex_key(&key2);
1916 put_futex_key(&key1);
Thomas Gleixnerd0725992009-06-11 23:15:43 +02001917 ret = fault_in_user_writeable(uaddr2);
Darren Hart52400ba2009-04-03 13:40:49 -07001918 if (!ret)
1919 goto retry;
1920 goto out;
1921 case -EAGAIN:
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001922 /*
1923 * Two reasons for this:
1924 * - Owner is exiting and we just wait for the
1925 * exit to complete.
1926 * - The user space value changed.
1927 */
Darren Hart52400ba2009-04-03 13:40:49 -07001928 double_unlock_hb(hb1, hb2);
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07001929 hb_waiters_dec(hb2);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001930 put_futex_key(&key2);
1931 put_futex_key(&key1);
Darren Hart52400ba2009-04-03 13:40:49 -07001932 cond_resched();
1933 goto retry;
1934 default:
1935 goto out_unlock;
1936 }
1937 }
1938
Jason Low0d00c7b2014-01-12 15:31:22 -08001939 plist_for_each_entry_safe(this, next, &hb1->chain, list) {
Darren Hart52400ba2009-04-03 13:40:49 -07001940 if (task_count - nr_wake >= nr_requeue)
1941 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Darren Hart52400ba2009-04-03 13:40:49 -07001943 if (!match_futex(&this->key, &key1))
1944 continue;
1945
Darren Hart392741e2009-08-07 15:20:48 -07001946 /*
1947 * FUTEX_WAIT_REQEUE_PI and FUTEX_CMP_REQUEUE_PI should always
1948 * be paired with each other and no other futex ops.
Darren Hartaa109902012-11-26 16:29:56 -08001949 *
1950 * We should never be requeueing a futex_q with a pi_state,
1951 * which is awaiting a futex_unlock_pi().
Darren Hart392741e2009-08-07 15:20:48 -07001952 */
1953 if ((requeue_pi && !this->rt_waiter) ||
Darren Hartaa109902012-11-26 16:29:56 -08001954 (!requeue_pi && this->rt_waiter) ||
1955 this->pi_state) {
Darren Hart392741e2009-08-07 15:20:48 -07001956 ret = -EINVAL;
1957 break;
1958 }
Darren Hart52400ba2009-04-03 13:40:49 -07001959
1960 /*
1961 * Wake nr_wake waiters. For requeue_pi, if we acquired the
1962 * lock, we already woke the top_waiter. If not, it will be
1963 * woken by futex_unlock_pi().
1964 */
1965 if (++task_count <= nr_wake && !requeue_pi) {
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001966 mark_wake_futex(&wake_q, this);
Darren Hart52400ba2009-04-03 13:40:49 -07001967 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 }
Darren Hart52400ba2009-04-03 13:40:49 -07001969
Darren Hart84bc4af2009-08-13 17:36:53 -07001970 /* Ensure we requeue to the expected futex for requeue_pi. */
1971 if (requeue_pi && !match_futex(this->requeue_pi_key, &key2)) {
1972 ret = -EINVAL;
1973 break;
1974 }
1975
Darren Hart52400ba2009-04-03 13:40:49 -07001976 /*
1977 * Requeue nr_requeue waiters and possibly one more in the case
1978 * of requeue_pi if we couldn't acquire the lock atomically.
1979 */
1980 if (requeue_pi) {
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00001981 /*
1982 * Prepare the waiter to take the rt_mutex. Take a
1983 * refcount on the pi_state and store the pointer in
1984 * the futex_q object of the waiter.
1985 */
Darren Hart52400ba2009-04-03 13:40:49 -07001986 atomic_inc(&pi_state->refcount);
1987 this->pi_state = pi_state;
1988 ret = rt_mutex_start_proxy_lock(&pi_state->pi_mutex,
1989 this->rt_waiter,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001990 this->task);
Darren Hart52400ba2009-04-03 13:40:49 -07001991 if (ret == 1) {
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00001992 /*
1993 * We got the lock. We do neither drop the
1994 * refcount on pi_state nor clear
1995 * this->pi_state because the waiter needs the
1996 * pi_state for cleaning up the user space
1997 * value. It will drop the refcount after
1998 * doing so.
1999 */
Darren Hartbeda2c72009-08-09 15:34:39 -07002000 requeue_pi_wake_futex(this, &key2, hb2);
Darren Hart89061d32009-10-15 15:30:48 -07002001 drop_count++;
Darren Hart52400ba2009-04-03 13:40:49 -07002002 continue;
2003 } else if (ret) {
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00002004 /*
2005 * rt_mutex_start_proxy_lock() detected a
2006 * potential deadlock when we tried to queue
2007 * that waiter. Drop the pi_state reference
2008 * which we took above and remove the pointer
2009 * to the state from the waiters futex_q
2010 * object.
2011 */
Darren Hart52400ba2009-04-03 13:40:49 -07002012 this->pi_state = NULL;
Thomas Gleixner29e9ee52015-12-19 20:07:39 +00002013 put_pi_state(pi_state);
Thomas Gleixner885c2cb2015-12-19 20:07:41 +00002014 /*
2015 * We stop queueing more waiters and let user
2016 * space deal with the mess.
2017 */
2018 break;
Darren Hart52400ba2009-04-03 13:40:49 -07002019 }
2020 }
2021 requeue_futex(this, hb1, hb2, &key2);
2022 drop_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 }
2024
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00002025 /*
2026 * We took an extra initial reference to the pi_state either
2027 * in futex_proxy_trylock_atomic() or in lookup_pi_state(). We
2028 * need to drop it here again.
2029 */
Thomas Gleixner29e9ee52015-12-19 20:07:39 +00002030 put_pi_state(pi_state);
Thomas Gleixner885c2cb2015-12-19 20:07:41 +00002031
2032out_unlock:
Darren Hart5eb3dc62009-03-12 00:55:52 -07002033 double_unlock_hb(hb1, hb2);
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07002034 wake_up_q(&wake_q);
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07002035 hb_waiters_dec(hb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Darren Hartcd84a422009-04-02 14:19:38 -07002037 /*
2038 * drop_futex_key_refs() must be called outside the spinlocks. During
2039 * the requeue we moved futex_q's from the hash bucket at key1 to the
2040 * one at key2 and updated their key pointer. We no longer need to
2041 * hold the references to key1.
2042 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 while (--drop_count >= 0)
Rusty Russell9adef582007-05-08 00:26:42 -07002044 drop_futex_key_refs(&key1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Darren Hart42d35d42008-12-29 15:49:53 -08002046out_put_keys:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002047 put_futex_key(&key2);
Darren Hart42d35d42008-12-29 15:49:53 -08002048out_put_key1:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002049 put_futex_key(&key1);
Darren Hart42d35d42008-12-29 15:49:53 -08002050out:
Darren Hart52400ba2009-04-03 13:40:49 -07002051 return ret ? ret : task_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052}
2053
2054/* The key must be already stored in q->key. */
Eric Sesterhenn82af7ac2008-01-25 10:40:46 +01002055static inline struct futex_hash_bucket *queue_lock(struct futex_q *q)
Namhyung Kim15e408c2010-09-14 21:43:48 +09002056 __acquires(&hb->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
Ingo Molnare2970f22006-06-27 02:54:47 -07002058 struct futex_hash_bucket *hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Ingo Molnare2970f22006-06-27 02:54:47 -07002060 hb = hash_futex(&q->key);
Linus Torvalds11d46162014-03-20 22:11:17 -07002061
2062 /*
2063 * Increment the counter before taking the lock so that
2064 * a potential waker won't miss a to-be-slept task that is
2065 * waiting for the spinlock. This is safe as all queue_lock()
2066 * users end up calling queue_me(). Similarly, for housekeeping,
2067 * decrement the counter at queue_unlock() when some error has
2068 * occurred and we don't end up adding the task to the list.
2069 */
2070 hb_waiters_inc(hb);
2071
Ingo Molnare2970f22006-06-27 02:54:47 -07002072 q->lock_ptr = &hb->lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Davidlohr Bueso8ad7b372016-02-09 11:15:13 -08002074 spin_lock(&hb->lock); /* implies smp_mb(); (A) */
Ingo Molnare2970f22006-06-27 02:54:47 -07002075 return hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076}
2077
Darren Hartd40d65c2009-09-21 22:30:15 -07002078static inline void
Jason Low0d00c7b2014-01-12 15:31:22 -08002079queue_unlock(struct futex_hash_bucket *hb)
Namhyung Kim15e408c2010-09-14 21:43:48 +09002080 __releases(&hb->lock)
Darren Hartd40d65c2009-09-21 22:30:15 -07002081{
2082 spin_unlock(&hb->lock);
Linus Torvalds11d46162014-03-20 22:11:17 -07002083 hb_waiters_dec(hb);
Darren Hartd40d65c2009-09-21 22:30:15 -07002084}
2085
2086/**
2087 * queue_me() - Enqueue the futex_q on the futex_hash_bucket
2088 * @q: The futex_q to enqueue
2089 * @hb: The destination hash bucket
2090 *
2091 * The hb->lock must be held by the caller, and is released here. A call to
2092 * queue_me() is typically paired with exactly one call to unqueue_me(). The
2093 * exceptions involve the PI related operations, which may use unqueue_me_pi()
2094 * or nothing if the unqueue is done as part of the wake process and the unqueue
2095 * state is implicit in the state of woken task (see futex_wait_requeue_pi() for
2096 * an example).
2097 */
Eric Sesterhenn82af7ac2008-01-25 10:40:46 +01002098static inline void queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
Namhyung Kim15e408c2010-09-14 21:43:48 +09002099 __releases(&hb->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100{
Pierre Peifferec92d082007-05-09 02:35:00 -07002101 int prio;
2102
2103 /*
2104 * The priority used to register this element is
2105 * - either the real thread-priority for the real-time threads
2106 * (i.e. threads with a priority lower than MAX_RT_PRIO)
2107 * - or MAX_RT_PRIO for non-RT threads.
2108 * Thus, all RT-threads are woken first in priority order, and
2109 * the others are woken last, in FIFO order.
2110 */
2111 prio = min(current->normal_prio, MAX_RT_PRIO);
2112
2113 plist_node_init(&q->list, prio);
Pierre Peifferec92d082007-05-09 02:35:00 -07002114 plist_add(&q->list, &hb->chain);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002115 q->task = current;
Ingo Molnare2970f22006-06-27 02:54:47 -07002116 spin_unlock(&hb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117}
2118
Darren Hartd40d65c2009-09-21 22:30:15 -07002119/**
2120 * unqueue_me() - Remove the futex_q from its futex_hash_bucket
2121 * @q: The futex_q to unqueue
2122 *
2123 * The q->lock_ptr must not be held by the caller. A call to unqueue_me() must
2124 * be paired with exactly one earlier call to queue_me().
2125 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002126 * Return:
2127 * 1 - if the futex_q was still queued (and we removed unqueued it);
Darren Hartd40d65c2009-09-21 22:30:15 -07002128 * 0 - if the futex_q was already removed by the waking thread
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130static int unqueue_me(struct futex_q *q)
2131{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 spinlock_t *lock_ptr;
Ingo Molnare2970f22006-06-27 02:54:47 -07002133 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
2135 /* In the common case we don't take the spinlock, which is nice. */
Darren Hart42d35d42008-12-29 15:49:53 -08002136retry:
Jianyu Zhan29b75eb2016-03-07 09:32:24 +08002137 /*
2138 * q->lock_ptr can change between this read and the following spin_lock.
2139 * Use READ_ONCE to forbid the compiler from reloading q->lock_ptr and
2140 * optimizing lock_ptr out of the logic below.
2141 */
2142 lock_ptr = READ_ONCE(q->lock_ptr);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07002143 if (lock_ptr != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 spin_lock(lock_ptr);
2145 /*
2146 * q->lock_ptr can change between reading it and
2147 * spin_lock(), causing us to take the wrong lock. This
2148 * corrects the race condition.
2149 *
2150 * Reasoning goes like this: if we have the wrong lock,
2151 * q->lock_ptr must have changed (maybe several times)
2152 * between reading it and the spin_lock(). It can
2153 * change again after the spin_lock() but only if it was
2154 * already changed before the spin_lock(). It cannot,
2155 * however, change back to the original value. Therefore
2156 * we can detect whether we acquired the correct lock.
2157 */
2158 if (unlikely(lock_ptr != q->lock_ptr)) {
2159 spin_unlock(lock_ptr);
2160 goto retry;
2161 }
Lai Jiangshan2e129782010-12-22 14:18:50 +08002162 __unqueue_futex(q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002163
2164 BUG_ON(q->pi_state);
2165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 spin_unlock(lock_ptr);
2167 ret = 1;
2168 }
2169
Rusty Russell9adef582007-05-08 00:26:42 -07002170 drop_futex_key_refs(&q->key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 return ret;
2172}
2173
Ingo Molnarc87e2832006-06-27 02:54:58 -07002174/*
2175 * PI futexes can not be requeued and must remove themself from the
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002176 * hash bucket. The hash bucket lock (i.e. lock_ptr) is held on entry
2177 * and dropped here.
Ingo Molnarc87e2832006-06-27 02:54:58 -07002178 */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002179static void unqueue_me_pi(struct futex_q *q)
Namhyung Kim15e408c2010-09-14 21:43:48 +09002180 __releases(q->lock_ptr)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002181{
Lai Jiangshan2e129782010-12-22 14:18:50 +08002182 __unqueue_futex(q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002183
2184 BUG_ON(!q->pi_state);
Thomas Gleixner29e9ee52015-12-19 20:07:39 +00002185 put_pi_state(q->pi_state);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002186 q->pi_state = NULL;
2187
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002188 spin_unlock(q->lock_ptr);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002189}
2190
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002191/*
Thomas Gleixnercdf71a12008-01-08 19:47:38 +01002192 * Fixup the pi_state owner with the new owner.
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002193 *
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002194 * Must be called with hash bucket lock held and mm->sem held for non
2195 * private futexes.
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002196 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002197static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002198 struct task_struct *newowner)
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002199{
Thomas Gleixnercdf71a12008-01-08 19:47:38 +01002200 u32 newtid = task_pid_vnr(newowner) | FUTEX_WAITERS;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002201 struct futex_pi_state *pi_state = q->pi_state;
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002202 struct task_struct *oldowner = pi_state->owner;
Vitaliy Ivanov7cfdaf32011-07-07 15:10:31 +03002203 u32 uval, uninitialized_var(curval), newval;
Darren Harte4dc5b72009-03-12 00:56:13 -07002204 int ret;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002205
2206 /* Owner died? */
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002207 if (!pi_state->owner)
2208 newtid |= FUTEX_OWNER_DIED;
2209
2210 /*
2211 * We are here either because we stole the rtmutex from the
Lai Jiangshan81612392011-01-14 17:09:41 +08002212 * previous highest priority waiter or we are the highest priority
2213 * waiter but failed to get the rtmutex the first time.
2214 * We have to replace the newowner TID in the user space variable.
2215 * This must be atomic as we have to preserve the owner died bit here.
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002216 *
Darren Hartb2d09942009-03-12 00:55:37 -07002217 * Note: We write the user space value _before_ changing the pi_state
2218 * because we can fault here. Imagine swapped out pages or a fork
2219 * that marked all the anonymous memory readonly for cow.
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002220 *
2221 * Modifying pi_state _before_ the user space value would
2222 * leave the pi_state in an inconsistent state when we fault
2223 * here, because we need to drop the hash bucket lock to
2224 * handle the fault. This might be observed in the PID check
2225 * in lookup_pi_state.
2226 */
2227retry:
2228 if (get_futex_value_locked(&uval, uaddr))
2229 goto handle_fault;
2230
2231 while (1) {
2232 newval = (uval & FUTEX_OWNER_DIED) | newtid;
2233
Michel Lespinasse37a9d912011-03-10 18:48:51 -08002234 if (cmpxchg_futex_value_locked(&curval, uaddr, uval, newval))
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002235 goto handle_fault;
2236 if (curval == uval)
2237 break;
2238 uval = curval;
2239 }
2240
2241 /*
2242 * We fixed up user space. Now we need to fix the pi_state
2243 * itself.
2244 */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002245 if (pi_state->owner != NULL) {
Thomas Gleixner1d615482009-11-17 14:54:03 +01002246 raw_spin_lock_irq(&pi_state->owner->pi_lock);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002247 WARN_ON(list_empty(&pi_state->list));
2248 list_del_init(&pi_state->list);
Thomas Gleixner1d615482009-11-17 14:54:03 +01002249 raw_spin_unlock_irq(&pi_state->owner->pi_lock);
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002250 }
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002251
Thomas Gleixnercdf71a12008-01-08 19:47:38 +01002252 pi_state->owner = newowner;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002253
Thomas Gleixner1d615482009-11-17 14:54:03 +01002254 raw_spin_lock_irq(&newowner->pi_lock);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002255 WARN_ON(!list_empty(&pi_state->list));
Thomas Gleixnercdf71a12008-01-08 19:47:38 +01002256 list_add(&pi_state->list, &newowner->pi_state_list);
Thomas Gleixner1d615482009-11-17 14:54:03 +01002257 raw_spin_unlock_irq(&newowner->pi_lock);
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002258 return 0;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002259
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002260 /*
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002261 * To handle the page fault we need to drop the hash bucket
Lai Jiangshan81612392011-01-14 17:09:41 +08002262 * lock here. That gives the other task (either the highest priority
2263 * waiter itself or the task which stole the rtmutex) the
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002264 * chance to try the fixup of the pi_state. So once we are
2265 * back from handling the fault we need to check the pi_state
2266 * after reacquiring the hash bucket lock and before trying to
2267 * do another fixup. When the fixup has been done already we
2268 * simply return.
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002269 */
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002270handle_fault:
2271 spin_unlock(q->lock_ptr);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002272
Thomas Gleixnerd0725992009-06-11 23:15:43 +02002273 ret = fault_in_user_writeable(uaddr);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002274
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002275 spin_lock(q->lock_ptr);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002276
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002277 /*
2278 * Check if someone else fixed it for us:
2279 */
2280 if (pi_state->owner != oldowner)
2281 return 0;
2282
2283 if (ret)
2284 return ret;
2285
2286 goto retry;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002287}
2288
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002289static long futex_wait_restart(struct restart_block *restart);
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07002290
Darren Hartca5f9522009-04-03 13:39:33 -07002291/**
Darren Hartdd973992009-04-03 13:40:02 -07002292 * fixup_owner() - Post lock pi_state and corner case management
2293 * @uaddr: user address of the futex
Darren Hartdd973992009-04-03 13:40:02 -07002294 * @q: futex_q (contains pi_state and access to the rt_mutex)
2295 * @locked: if the attempt to take the rt_mutex succeeded (1) or not (0)
2296 *
2297 * After attempting to lock an rt_mutex, this function is called to cleanup
2298 * the pi_state owner as well as handle race conditions that may allow us to
2299 * acquire the lock. Must be called with the hb lock held.
2300 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002301 * Return:
2302 * 1 - success, lock taken;
2303 * 0 - success, lock not taken;
Darren Hartdd973992009-04-03 13:40:02 -07002304 * <0 - on error (-EFAULT)
2305 */
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002306static int fixup_owner(u32 __user *uaddr, struct futex_q *q, int locked)
Darren Hartdd973992009-04-03 13:40:02 -07002307{
2308 struct task_struct *owner;
2309 int ret = 0;
2310
2311 if (locked) {
2312 /*
2313 * Got the lock. We might not be the anticipated owner if we
2314 * did a lock-steal - fix up the PI-state in that case:
2315 */
2316 if (q->pi_state->owner != current)
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002317 ret = fixup_pi_state_owner(uaddr, q, current);
Darren Hartdd973992009-04-03 13:40:02 -07002318 goto out;
2319 }
2320
2321 /*
2322 * Catch the rare case, where the lock was released when we were on the
2323 * way back before we locked the hash bucket.
2324 */
2325 if (q->pi_state->owner == current) {
2326 /*
2327 * Try to get the rt_mutex now. This might fail as some other
2328 * task acquired the rt_mutex after we removed ourself from the
2329 * rt_mutex waiters list.
2330 */
2331 if (rt_mutex_trylock(&q->pi_state->pi_mutex)) {
2332 locked = 1;
2333 goto out;
2334 }
2335
2336 /*
2337 * pi_state is incorrect, some other task did a lock steal and
2338 * we returned due to timeout or signal without taking the
Lai Jiangshan81612392011-01-14 17:09:41 +08002339 * rt_mutex. Too late.
Darren Hartdd973992009-04-03 13:40:02 -07002340 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01002341 raw_spin_lock_irq(&q->pi_state->pi_mutex.wait_lock);
Darren Hartdd973992009-04-03 13:40:02 -07002342 owner = rt_mutex_owner(&q->pi_state->pi_mutex);
Lai Jiangshan81612392011-01-14 17:09:41 +08002343 if (!owner)
2344 owner = rt_mutex_next_owner(&q->pi_state->pi_mutex);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01002345 raw_spin_unlock_irq(&q->pi_state->pi_mutex.wait_lock);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002346 ret = fixup_pi_state_owner(uaddr, q, owner);
Darren Hartdd973992009-04-03 13:40:02 -07002347 goto out;
2348 }
2349
2350 /*
2351 * Paranoia check. If we did not take the lock, then we should not be
Lai Jiangshan81612392011-01-14 17:09:41 +08002352 * the owner of the rt_mutex.
Darren Hartdd973992009-04-03 13:40:02 -07002353 */
2354 if (rt_mutex_owner(&q->pi_state->pi_mutex) == current)
2355 printk(KERN_ERR "fixup_owner: ret = %d pi-mutex: %p "
2356 "pi-state %p\n", ret,
2357 q->pi_state->pi_mutex.owner,
2358 q->pi_state->owner);
2359
2360out:
2361 return ret ? ret : locked;
2362}
2363
2364/**
Darren Hartca5f9522009-04-03 13:39:33 -07002365 * futex_wait_queue_me() - queue_me() and wait for wakeup, timeout, or signal
2366 * @hb: the futex hash bucket, must be locked by the caller
2367 * @q: the futex_q to queue up on
2368 * @timeout: the prepared hrtimer_sleeper, or null for no timeout
Darren Hartca5f9522009-04-03 13:39:33 -07002369 */
2370static void futex_wait_queue_me(struct futex_hash_bucket *hb, struct futex_q *q,
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02002371 struct hrtimer_sleeper *timeout)
Darren Hartca5f9522009-04-03 13:39:33 -07002372{
Darren Hart9beba3c2009-09-24 11:54:47 -07002373 /*
2374 * The task state is guaranteed to be set before another task can
Peter Zijlstrab92b8b32015-05-12 10:51:55 +02002375 * wake it. set_current_state() is implemented using smp_store_mb() and
Darren Hart9beba3c2009-09-24 11:54:47 -07002376 * queue_me() calls spin_unlock() upon completion, both serializing
2377 * access to the hash list and forcing another memory barrier.
2378 */
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02002379 set_current_state(TASK_INTERRUPTIBLE);
Darren Hart0729e192009-09-21 22:30:38 -07002380 queue_me(q, hb);
Darren Hartca5f9522009-04-03 13:39:33 -07002381
2382 /* Arm the timer */
Thomas Gleixner2e4b0d32015-04-14 21:09:13 +00002383 if (timeout)
Darren Hartca5f9522009-04-03 13:39:33 -07002384 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
Darren Hartca5f9522009-04-03 13:39:33 -07002385
2386 /*
Darren Hart0729e192009-09-21 22:30:38 -07002387 * If we have been removed from the hash list, then another task
2388 * has tried to wake us, and we can skip the call to schedule().
Darren Hartca5f9522009-04-03 13:39:33 -07002389 */
2390 if (likely(!plist_node_empty(&q->list))) {
2391 /*
2392 * If the timer has already expired, current will already be
2393 * flagged for rescheduling. Only call schedule if there
2394 * is no timeout, or if it has yet to expire.
2395 */
2396 if (!timeout || timeout->task)
Colin Cross88c80042013-05-01 18:35:05 -07002397 freezable_schedule();
Darren Hartca5f9522009-04-03 13:39:33 -07002398 }
2399 __set_current_state(TASK_RUNNING);
2400}
2401
Darren Hartf8010732009-04-03 13:40:40 -07002402/**
2403 * futex_wait_setup() - Prepare to wait on a futex
2404 * @uaddr: the futex userspace address
2405 * @val: the expected value
Darren Hartb41277d2010-11-08 13:10:09 -08002406 * @flags: futex flags (FLAGS_SHARED, etc.)
Darren Hartf8010732009-04-03 13:40:40 -07002407 * @q: the associated futex_q
2408 * @hb: storage for hash_bucket pointer to be returned to caller
2409 *
2410 * Setup the futex_q and locate the hash_bucket. Get the futex value and
2411 * compare it with the expected value. Handle atomic faults internally.
2412 * Return with the hb lock held and a q.key reference on success, and unlocked
2413 * with no q.key reference on failure.
2414 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002415 * Return:
2416 * 0 - uaddr contains val and hb has been locked;
Bart Van Asscheca4a04c2011-07-17 09:01:00 +02002417 * <1 - -EFAULT or -EWOULDBLOCK (uaddr does not contain val) and hb is unlocked
Darren Hartf8010732009-04-03 13:40:40 -07002418 */
Darren Hartb41277d2010-11-08 13:10:09 -08002419static int futex_wait_setup(u32 __user *uaddr, u32 val, unsigned int flags,
Darren Hartf8010732009-04-03 13:40:40 -07002420 struct futex_q *q, struct futex_hash_bucket **hb)
2421{
2422 u32 uval;
2423 int ret;
2424
2425 /*
2426 * Access the page AFTER the hash-bucket is locked.
2427 * Order is important:
2428 *
2429 * Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val);
2430 * Userspace waker: if (cond(var)) { var = new; futex_wake(&var); }
2431 *
2432 * The basic logical guarantee of a futex is that it blocks ONLY
2433 * if cond(var) is known to be true at the time of blocking, for
Michel Lespinasse8fe8f542011-03-06 18:07:50 -08002434 * any cond. If we locked the hash-bucket after testing *uaddr, that
2435 * would open a race condition where we could block indefinitely with
Darren Hartf8010732009-04-03 13:40:40 -07002436 * cond(var) false, which would violate the guarantee.
2437 *
Michel Lespinasse8fe8f542011-03-06 18:07:50 -08002438 * On the other hand, we insert q and release the hash-bucket only
2439 * after testing *uaddr. This guarantees that futex_wait() will NOT
2440 * absorb a wakeup if *uaddr does not match the desired values
2441 * while the syscall executes.
Darren Hartf8010732009-04-03 13:40:40 -07002442 */
2443retry:
Shawn Bohrer9ea71502011-06-30 11:21:32 -05002444 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q->key, VERIFY_READ);
Darren Hartf8010732009-04-03 13:40:40 -07002445 if (unlikely(ret != 0))
Darren Harta5a2a0c2009-04-10 09:50:05 -07002446 return ret;
Darren Hartf8010732009-04-03 13:40:40 -07002447
2448retry_private:
2449 *hb = queue_lock(q);
2450
2451 ret = get_futex_value_locked(&uval, uaddr);
2452
2453 if (ret) {
Jason Low0d00c7b2014-01-12 15:31:22 -08002454 queue_unlock(*hb);
Darren Hartf8010732009-04-03 13:40:40 -07002455
2456 ret = get_user(uval, uaddr);
2457 if (ret)
2458 goto out;
2459
Darren Hartb41277d2010-11-08 13:10:09 -08002460 if (!(flags & FLAGS_SHARED))
Darren Hartf8010732009-04-03 13:40:40 -07002461 goto retry_private;
2462
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002463 put_futex_key(&q->key);
Darren Hartf8010732009-04-03 13:40:40 -07002464 goto retry;
2465 }
2466
2467 if (uval != val) {
Jason Low0d00c7b2014-01-12 15:31:22 -08002468 queue_unlock(*hb);
Darren Hartf8010732009-04-03 13:40:40 -07002469 ret = -EWOULDBLOCK;
2470 }
2471
2472out:
2473 if (ret)
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002474 put_futex_key(&q->key);
Darren Hartf8010732009-04-03 13:40:40 -07002475 return ret;
2476}
2477
Darren Hartb41277d2010-11-08 13:10:09 -08002478static int futex_wait(u32 __user *uaddr, unsigned int flags, u32 val,
2479 ktime_t *abs_time, u32 bitset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480{
Darren Hartca5f9522009-04-03 13:39:33 -07002481 struct hrtimer_sleeper timeout, *to = NULL;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002482 struct restart_block *restart;
Ingo Molnare2970f22006-06-27 02:54:47 -07002483 struct futex_hash_bucket *hb;
Darren Hart5bdb05f2010-11-08 13:40:28 -08002484 struct futex_q q = futex_q_init;
Ingo Molnare2970f22006-06-27 02:54:47 -07002485 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
Thomas Gleixnercd689982008-02-01 17:45:14 +01002487 if (!bitset)
2488 return -EINVAL;
Thomas Gleixnercd689982008-02-01 17:45:14 +01002489 q.bitset = bitset;
Darren Hartca5f9522009-04-03 13:39:33 -07002490
2491 if (abs_time) {
2492 to = &timeout;
2493
Darren Hartb41277d2010-11-08 13:10:09 -08002494 hrtimer_init_on_stack(&to->timer, (flags & FLAGS_CLOCKRT) ?
2495 CLOCK_REALTIME : CLOCK_MONOTONIC,
2496 HRTIMER_MODE_ABS);
Darren Hartca5f9522009-04-03 13:39:33 -07002497 hrtimer_init_sleeper(to, current);
2498 hrtimer_set_expires_range_ns(&to->timer, *abs_time,
2499 current->timer_slack_ns);
2500 }
2501
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002502retry:
Darren Hart7ada8762010-10-17 08:35:04 -07002503 /*
2504 * Prepare to wait on uaddr. On success, holds hb lock and increments
2505 * q.key refs.
2506 */
Darren Hartb41277d2010-11-08 13:10:09 -08002507 ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
Darren Hartf8010732009-04-03 13:40:40 -07002508 if (ret)
Darren Hart42d35d42008-12-29 15:49:53 -08002509 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
Darren Hartca5f9522009-04-03 13:39:33 -07002511 /* queue_me and wait for wakeup, timeout, or a signal. */
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02002512 futex_wait_queue_me(hb, &q, to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
2514 /* If we were woken (and unqueued), we succeeded, whatever. */
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002515 ret = 0;
Darren Hart7ada8762010-10-17 08:35:04 -07002516 /* unqueue_me() drops q.key ref */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 if (!unqueue_me(&q))
Darren Hart7ada8762010-10-17 08:35:04 -07002518 goto out;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002519 ret = -ETIMEDOUT;
Darren Hartca5f9522009-04-03 13:39:33 -07002520 if (to && !to->task)
Darren Hart7ada8762010-10-17 08:35:04 -07002521 goto out;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002522
Ingo Molnare2970f22006-06-27 02:54:47 -07002523 /*
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002524 * We expect signal_pending(current), but we might be the
2525 * victim of a spurious wakeup as well.
Ingo Molnare2970f22006-06-27 02:54:47 -07002526 */
Darren Hart7ada8762010-10-17 08:35:04 -07002527 if (!signal_pending(current))
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002528 goto retry;
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002529
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002530 ret = -ERESTARTSYS;
Pierre Peifferc19384b2007-05-09 02:35:02 -07002531 if (!abs_time)
Darren Hart7ada8762010-10-17 08:35:04 -07002532 goto out;
Steven Rostedtce6bd422007-12-05 15:46:09 +01002533
Andy Lutomirskif56141e2015-02-12 15:01:14 -08002534 restart = &current->restart_block;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002535 restart->fn = futex_wait_restart;
Namhyung Kima3c74c52010-09-14 21:43:47 +09002536 restart->futex.uaddr = uaddr;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002537 restart->futex.val = val;
2538 restart->futex.time = abs_time->tv64;
2539 restart->futex.bitset = bitset;
Darren Hart0cd9c642011-04-14 15:41:57 -07002540 restart->futex.flags = flags | FLAGS_HAS_TIMEOUT;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002541
2542 ret = -ERESTART_RESTARTBLOCK;
2543
Darren Hart42d35d42008-12-29 15:49:53 -08002544out:
Darren Hartca5f9522009-04-03 13:39:33 -07002545 if (to) {
2546 hrtimer_cancel(&to->timer);
2547 destroy_hrtimer_on_stack(&to->timer);
2548 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07002549 return ret;
2550}
2551
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002552
2553static long futex_wait_restart(struct restart_block *restart)
2554{
Namhyung Kima3c74c52010-09-14 21:43:47 +09002555 u32 __user *uaddr = restart->futex.uaddr;
Darren Harta72188d2009-04-03 13:40:22 -07002556 ktime_t t, *tp = NULL;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002557
Darren Harta72188d2009-04-03 13:40:22 -07002558 if (restart->futex.flags & FLAGS_HAS_TIMEOUT) {
2559 t.tv64 = restart->futex.time;
2560 tp = &t;
2561 }
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002562 restart->fn = do_no_restart_syscall;
Darren Hartb41277d2010-11-08 13:10:09 -08002563
2564 return (long)futex_wait(uaddr, restart->futex.flags,
2565 restart->futex.val, tp, restart->futex.bitset);
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002566}
2567
2568
Ingo Molnarc87e2832006-06-27 02:54:58 -07002569/*
2570 * Userspace tried a 0 -> TID atomic transition of the futex value
2571 * and failed. The kernel side here does the whole locking operation:
Davidlohr Bueso767f5092015-06-29 23:26:01 -07002572 * if there are waiters then it will block as a consequence of relying
2573 * on rt-mutexes, it does PI, etc. (Due to races the kernel might see
2574 * a 0 value of the futex too.).
2575 *
2576 * Also serves as futex trylock_pi()'ing, and due semantics.
Ingo Molnarc87e2832006-06-27 02:54:58 -07002577 */
Michael Kerrisk996636d2015-01-16 20:28:06 +01002578static int futex_lock_pi(u32 __user *uaddr, unsigned int flags,
Darren Hartb41277d2010-11-08 13:10:09 -08002579 ktime_t *time, int trylock)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002580{
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07002581 struct hrtimer_sleeper timeout, *to = NULL;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002582 struct futex_hash_bucket *hb;
Darren Hart5bdb05f2010-11-08 13:40:28 -08002583 struct futex_q q = futex_q_init;
Darren Hartdd973992009-04-03 13:40:02 -07002584 int res, ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002585
2586 if (refill_pi_state_cache())
2587 return -ENOMEM;
2588
Pierre Peifferc19384b2007-05-09 02:35:02 -07002589 if (time) {
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07002590 to = &timeout;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07002591 hrtimer_init_on_stack(&to->timer, CLOCK_REALTIME,
2592 HRTIMER_MODE_ABS);
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07002593 hrtimer_init_sleeper(to, current);
Arjan van de Vencc584b22008-09-01 15:02:30 -07002594 hrtimer_set_expires(&to->timer, *time);
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07002595 }
2596
Darren Hart42d35d42008-12-29 15:49:53 -08002597retry:
Shawn Bohrer9ea71502011-06-30 11:21:32 -05002598 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q.key, VERIFY_WRITE);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002599 if (unlikely(ret != 0))
Darren Hart42d35d42008-12-29 15:49:53 -08002600 goto out;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002601
Darren Harte4dc5b72009-03-12 00:56:13 -07002602retry_private:
Eric Sesterhenn82af7ac2008-01-25 10:40:46 +01002603 hb = queue_lock(&q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002604
Darren Hartbab5bc92009-04-07 23:23:50 -07002605 ret = futex_lock_pi_atomic(uaddr, hb, &q.key, &q.pi_state, current, 0);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002606 if (unlikely(ret)) {
Davidlohr Bueso767f5092015-06-29 23:26:01 -07002607 /*
2608 * Atomic work succeeded and we got the lock,
2609 * or failed. Either way, we do _not_ block.
2610 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002611 switch (ret) {
Darren Hart1a520842009-04-03 13:39:52 -07002612 case 1:
2613 /* We got the lock. */
2614 ret = 0;
2615 goto out_unlock_put_key;
2616 case -EFAULT:
2617 goto uaddr_faulted;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002618 case -EAGAIN:
2619 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00002620 * Two reasons for this:
2621 * - Task is exiting and we just wait for the
2622 * exit to complete.
2623 * - The user space value changed.
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002624 */
Jason Low0d00c7b2014-01-12 15:31:22 -08002625 queue_unlock(hb);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002626 put_futex_key(&q.key);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002627 cond_resched();
2628 goto retry;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002629 default:
Darren Hart42d35d42008-12-29 15:49:53 -08002630 goto out_unlock_put_key;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002631 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07002632 }
2633
2634 /*
2635 * Only actually queue now that the atomic ops are done:
2636 */
Eric Sesterhenn82af7ac2008-01-25 10:40:46 +01002637 queue_me(&q, hb);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002638
Ingo Molnarc87e2832006-06-27 02:54:58 -07002639 WARN_ON(!q.pi_state);
2640 /*
2641 * Block on the PI mutex:
2642 */
Thomas Gleixnerc051b212014-05-22 03:25:50 +00002643 if (!trylock) {
2644 ret = rt_mutex_timed_futex_lock(&q.pi_state->pi_mutex, to);
2645 } else {
Ingo Molnarc87e2832006-06-27 02:54:58 -07002646 ret = rt_mutex_trylock(&q.pi_state->pi_mutex);
2647 /* Fixup the trylock return value: */
2648 ret = ret ? 0 : -EWOULDBLOCK;
2649 }
2650
Vernon Mauerya99e4e42006-07-01 04:35:42 -07002651 spin_lock(q.lock_ptr);
Darren Hartdd973992009-04-03 13:40:02 -07002652 /*
2653 * Fixup the pi_state owner and possibly acquire the lock if we
2654 * haven't already.
2655 */
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002656 res = fixup_owner(uaddr, &q, !ret);
Darren Hartdd973992009-04-03 13:40:02 -07002657 /*
2658 * If fixup_owner() returned an error, proprogate that. If it acquired
2659 * the lock, clear our -ETIMEDOUT or -EINTR.
2660 */
2661 if (res)
2662 ret = (res < 0) ? res : 0;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002663
Darren Harte8f63862009-03-12 00:56:06 -07002664 /*
Darren Hartdd973992009-04-03 13:40:02 -07002665 * If fixup_owner() faulted and was unable to handle the fault, unlock
2666 * it and return the fault to userspace.
Darren Harte8f63862009-03-12 00:56:06 -07002667 */
2668 if (ret && (rt_mutex_owner(&q.pi_state->pi_mutex) == current))
2669 rt_mutex_unlock(&q.pi_state->pi_mutex);
2670
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002671 /* Unqueue and drop the lock */
2672 unqueue_me_pi(&q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002673
Mikael Pettersson5ecb01c2010-01-23 22:36:29 +01002674 goto out_put_key;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002675
Darren Hart42d35d42008-12-29 15:49:53 -08002676out_unlock_put_key:
Jason Low0d00c7b2014-01-12 15:31:22 -08002677 queue_unlock(hb);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002678
Darren Hart42d35d42008-12-29 15:49:53 -08002679out_put_key:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002680 put_futex_key(&q.key);
Darren Hart42d35d42008-12-29 15:49:53 -08002681out:
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07002682 if (to)
2683 destroy_hrtimer_on_stack(&to->timer);
Darren Hartdd973992009-04-03 13:40:02 -07002684 return ret != -EINTR ? ret : -ERESTARTNOINTR;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002685
Darren Hart42d35d42008-12-29 15:49:53 -08002686uaddr_faulted:
Jason Low0d00c7b2014-01-12 15:31:22 -08002687 queue_unlock(hb);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002688
Thomas Gleixnerd0725992009-06-11 23:15:43 +02002689 ret = fault_in_user_writeable(uaddr);
Darren Harte4dc5b72009-03-12 00:56:13 -07002690 if (ret)
2691 goto out_put_key;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002692
Darren Hartb41277d2010-11-08 13:10:09 -08002693 if (!(flags & FLAGS_SHARED))
Darren Harte4dc5b72009-03-12 00:56:13 -07002694 goto retry_private;
2695
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002696 put_futex_key(&q.key);
Darren Harte4dc5b72009-03-12 00:56:13 -07002697 goto retry;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002698}
2699
2700/*
Ingo Molnarc87e2832006-06-27 02:54:58 -07002701 * Userspace attempted a TID -> 0 atomic transition, and failed.
2702 * This is the in-kernel slowpath: we look up the PI state (if any),
2703 * and do the rt-mutex unlock.
2704 */
Darren Hartb41277d2010-11-08 13:10:09 -08002705static int futex_unlock_pi(u32 __user *uaddr, unsigned int flags)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002706{
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002707 u32 uninitialized_var(curval), uval, vpid = task_pid_vnr(current);
Peter Zijlstra38d47c12008-09-26 19:32:20 +02002708 union futex_key key = FUTEX_KEY_INIT;
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002709 struct futex_hash_bucket *hb;
2710 struct futex_q *match;
Darren Harte4dc5b72009-03-12 00:56:13 -07002711 int ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002712
2713retry:
2714 if (get_user(uval, uaddr))
2715 return -EFAULT;
2716 /*
2717 * We release only a lock we actually own:
2718 */
Thomas Gleixnerc0c9ed12011-03-11 11:51:22 +01002719 if ((uval & FUTEX_TID_MASK) != vpid)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002720 return -EPERM;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002721
Shawn Bohrer9ea71502011-06-30 11:21:32 -05002722 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, VERIFY_WRITE);
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002723 if (ret)
2724 return ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002725
2726 hb = hash_futex(&key);
2727 spin_lock(&hb->lock);
2728
Ingo Molnarc87e2832006-06-27 02:54:58 -07002729 /*
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002730 * Check waiters first. We do not trust user space values at
2731 * all and we at least want to know if user space fiddled
2732 * with the futex value instead of blindly unlocking.
Ingo Molnarc87e2832006-06-27 02:54:58 -07002733 */
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002734 match = futex_top_waiter(hb, &key);
2735 if (match) {
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02002736 ret = wake_futex_pi(uaddr, uval, match, hb);
2737 /*
2738 * In case of success wake_futex_pi dropped the hash
2739 * bucket lock.
2740 */
2741 if (!ret)
2742 goto out_putkey;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002743 /*
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002744 * The atomic access to the futex value generated a
2745 * pagefault, so retry the user-access and the wakeup:
Ingo Molnarc87e2832006-06-27 02:54:58 -07002746 */
2747 if (ret == -EFAULT)
2748 goto pi_faulted;
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02002749 /*
Sebastian Andrzej Siewior89e9e662016-04-15 14:35:39 +02002750 * A unconditional UNLOCK_PI op raced against a waiter
2751 * setting the FUTEX_WAITERS bit. Try again.
2752 */
2753 if (ret == -EAGAIN) {
2754 spin_unlock(&hb->lock);
2755 put_futex_key(&key);
2756 goto retry;
2757 }
2758 /*
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02002759 * wake_futex_pi has detected invalid state. Tell user
2760 * space.
2761 */
Ingo Molnarc87e2832006-06-27 02:54:58 -07002762 goto out_unlock;
2763 }
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002764
Ingo Molnarc87e2832006-06-27 02:54:58 -07002765 /*
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002766 * We have no kernel internal state, i.e. no waiters in the
2767 * kernel. Waiters which are about to queue themselves are stuck
2768 * on hb->lock. So we can safely ignore them. We do neither
2769 * preserve the WAITERS bit not the OWNER_DIED one. We are the
2770 * owner.
Ingo Molnarc87e2832006-06-27 02:54:58 -07002771 */
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002772 if (cmpxchg_futex_value_locked(&curval, uaddr, uval, 0))
Thomas Gleixner13fbca42014-06-03 12:27:07 +00002773 goto pi_faulted;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002774
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002775 /*
2776 * If uval has changed, let user space handle it.
2777 */
2778 ret = (curval == uval) ? 0 : -EAGAIN;
2779
Ingo Molnarc87e2832006-06-27 02:54:58 -07002780out_unlock:
2781 spin_unlock(&hb->lock);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02002782out_putkey:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002783 put_futex_key(&key);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002784 return ret;
2785
2786pi_faulted:
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002787 spin_unlock(&hb->lock);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002788 put_futex_key(&key);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002789
Thomas Gleixnerd0725992009-06-11 23:15:43 +02002790 ret = fault_in_user_writeable(uaddr);
Darren Hartb5686362008-12-18 15:06:34 -08002791 if (!ret)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002792 goto retry;
2793
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 return ret;
2795}
2796
Darren Hart52400ba2009-04-03 13:40:49 -07002797/**
2798 * handle_early_requeue_pi_wakeup() - Detect early wakeup on the initial futex
2799 * @hb: the hash_bucket futex_q was original enqueued on
2800 * @q: the futex_q woken while waiting to be requeued
2801 * @key2: the futex_key of the requeue target futex
2802 * @timeout: the timeout associated with the wait (NULL if none)
2803 *
2804 * Detect if the task was woken on the initial futex as opposed to the requeue
2805 * target futex. If so, determine if it was a timeout or a signal that caused
2806 * the wakeup and return the appropriate error code to the caller. Must be
2807 * called with the hb lock held.
2808 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002809 * Return:
2810 * 0 = no early wakeup detected;
2811 * <0 = -ETIMEDOUT or -ERESTARTNOINTR
Darren Hart52400ba2009-04-03 13:40:49 -07002812 */
2813static inline
2814int handle_early_requeue_pi_wakeup(struct futex_hash_bucket *hb,
2815 struct futex_q *q, union futex_key *key2,
2816 struct hrtimer_sleeper *timeout)
2817{
2818 int ret = 0;
2819
2820 /*
2821 * With the hb lock held, we avoid races while we process the wakeup.
2822 * We only need to hold hb (and not hb2) to ensure atomicity as the
2823 * wakeup code can't change q.key from uaddr to uaddr2 if we hold hb.
2824 * It can't be requeued from uaddr2 to something else since we don't
2825 * support a PI aware source futex for requeue.
2826 */
2827 if (!match_futex(&q->key, key2)) {
2828 WARN_ON(q->lock_ptr && (&hb->lock != q->lock_ptr));
2829 /*
2830 * We were woken prior to requeue by a timeout or a signal.
2831 * Unqueue the futex_q and determine which it was.
2832 */
Lai Jiangshan2e129782010-12-22 14:18:50 +08002833 plist_del(&q->list, &hb->chain);
Linus Torvalds11d46162014-03-20 22:11:17 -07002834 hb_waiters_dec(hb);
Darren Hart52400ba2009-04-03 13:40:49 -07002835
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002836 /* Handle spurious wakeups gracefully */
Thomas Gleixner11df6dd2009-10-28 20:26:48 +01002837 ret = -EWOULDBLOCK;
Darren Hart52400ba2009-04-03 13:40:49 -07002838 if (timeout && !timeout->task)
2839 ret = -ETIMEDOUT;
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002840 else if (signal_pending(current))
Thomas Gleixner1c840c12009-05-20 09:22:40 +02002841 ret = -ERESTARTNOINTR;
Darren Hart52400ba2009-04-03 13:40:49 -07002842 }
2843 return ret;
2844}
2845
2846/**
2847 * futex_wait_requeue_pi() - Wait on uaddr and take uaddr2
Darren Hart56ec1602009-09-21 22:29:59 -07002848 * @uaddr: the futex we initially wait on (non-pi)
Darren Hartb41277d2010-11-08 13:10:09 -08002849 * @flags: futex flags (FLAGS_SHARED, FLAGS_CLOCKRT, etc.), they must be
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07002850 * the same type, no requeueing from private to shared, etc.
Darren Hart52400ba2009-04-03 13:40:49 -07002851 * @val: the expected value of uaddr
2852 * @abs_time: absolute timeout
Darren Hart56ec1602009-09-21 22:29:59 -07002853 * @bitset: 32 bit wakeup bitset set by userspace, defaults to all
Darren Hart52400ba2009-04-03 13:40:49 -07002854 * @uaddr2: the pi futex we will take prior to returning to user-space
2855 *
2856 * The caller will wait on uaddr and will be requeued by futex_requeue() to
Darren Hart6f7b0a22012-07-20 11:53:31 -07002857 * uaddr2 which must be PI aware and unique from uaddr. Normal wakeup will wake
2858 * on uaddr2 and complete the acquisition of the rt_mutex prior to returning to
2859 * userspace. This ensures the rt_mutex maintains an owner when it has waiters;
2860 * without one, the pi logic would not know which task to boost/deboost, if
2861 * there was a need to.
Darren Hart52400ba2009-04-03 13:40:49 -07002862 *
2863 * We call schedule in futex_wait_queue_me() when we enqueue and return there
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002864 * via the following--
Darren Hart52400ba2009-04-03 13:40:49 -07002865 * 1) wakeup on uaddr2 after an atomic lock acquisition by futex_requeue()
Darren Hartcc6db4e2009-07-31 16:20:10 -07002866 * 2) wakeup on uaddr2 after a requeue
2867 * 3) signal
2868 * 4) timeout
Darren Hart52400ba2009-04-03 13:40:49 -07002869 *
Darren Hartcc6db4e2009-07-31 16:20:10 -07002870 * If 3, cleanup and return -ERESTARTNOINTR.
Darren Hart52400ba2009-04-03 13:40:49 -07002871 *
2872 * If 2, we may then block on trying to take the rt_mutex and return via:
2873 * 5) successful lock
2874 * 6) signal
2875 * 7) timeout
2876 * 8) other lock acquisition failure
2877 *
Darren Hartcc6db4e2009-07-31 16:20:10 -07002878 * If 6, return -EWOULDBLOCK (restarting the syscall would do the same).
Darren Hart52400ba2009-04-03 13:40:49 -07002879 *
2880 * If 4 or 7, we cleanup and return with -ETIMEDOUT.
2881 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002882 * Return:
2883 * 0 - On success;
Darren Hart52400ba2009-04-03 13:40:49 -07002884 * <0 - On error
2885 */
Darren Hartb41277d2010-11-08 13:10:09 -08002886static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
Darren Hart52400ba2009-04-03 13:40:49 -07002887 u32 val, ktime_t *abs_time, u32 bitset,
Darren Hartb41277d2010-11-08 13:10:09 -08002888 u32 __user *uaddr2)
Darren Hart52400ba2009-04-03 13:40:49 -07002889{
2890 struct hrtimer_sleeper timeout, *to = NULL;
2891 struct rt_mutex_waiter rt_waiter;
Darren Hart52400ba2009-04-03 13:40:49 -07002892 struct futex_hash_bucket *hb;
Darren Hart5bdb05f2010-11-08 13:40:28 -08002893 union futex_key key2 = FUTEX_KEY_INIT;
2894 struct futex_q q = futex_q_init;
Darren Hart52400ba2009-04-03 13:40:49 -07002895 int res, ret;
Darren Hart52400ba2009-04-03 13:40:49 -07002896
Darren Hart6f7b0a22012-07-20 11:53:31 -07002897 if (uaddr == uaddr2)
2898 return -EINVAL;
2899
Darren Hart52400ba2009-04-03 13:40:49 -07002900 if (!bitset)
2901 return -EINVAL;
2902
2903 if (abs_time) {
2904 to = &timeout;
Darren Hartb41277d2010-11-08 13:10:09 -08002905 hrtimer_init_on_stack(&to->timer, (flags & FLAGS_CLOCKRT) ?
2906 CLOCK_REALTIME : CLOCK_MONOTONIC,
2907 HRTIMER_MODE_ABS);
Darren Hart52400ba2009-04-03 13:40:49 -07002908 hrtimer_init_sleeper(to, current);
2909 hrtimer_set_expires_range_ns(&to->timer, *abs_time,
2910 current->timer_slack_ns);
2911 }
2912
2913 /*
2914 * The waiter is allocated on our stack, manipulated by the requeue
2915 * code while we sleep on uaddr.
2916 */
2917 debug_rt_mutex_init_waiter(&rt_waiter);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01002918 RB_CLEAR_NODE(&rt_waiter.pi_tree_entry);
2919 RB_CLEAR_NODE(&rt_waiter.tree_entry);
Darren Hart52400ba2009-04-03 13:40:49 -07002920 rt_waiter.task = NULL;
2921
Shawn Bohrer9ea71502011-06-30 11:21:32 -05002922 ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE);
Darren Hart52400ba2009-04-03 13:40:49 -07002923 if (unlikely(ret != 0))
2924 goto out;
2925
Darren Hart84bc4af2009-08-13 17:36:53 -07002926 q.bitset = bitset;
2927 q.rt_waiter = &rt_waiter;
2928 q.requeue_pi_key = &key2;
2929
Darren Hart7ada8762010-10-17 08:35:04 -07002930 /*
2931 * Prepare to wait on uaddr. On success, increments q.key (key1) ref
2932 * count.
2933 */
Darren Hartb41277d2010-11-08 13:10:09 -08002934 ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
Thomas Gleixnerc8b15a72009-05-20 09:18:50 +02002935 if (ret)
2936 goto out_key2;
Darren Hart52400ba2009-04-03 13:40:49 -07002937
Thomas Gleixnere9c243a2014-06-03 12:27:06 +00002938 /*
2939 * The check above which compares uaddrs is not sufficient for
2940 * shared futexes. We need to compare the keys:
2941 */
2942 if (match_futex(&q.key, &key2)) {
Thomas Gleixner13c42c22014-09-11 23:44:35 +02002943 queue_unlock(hb);
Thomas Gleixnere9c243a2014-06-03 12:27:06 +00002944 ret = -EINVAL;
2945 goto out_put_keys;
2946 }
2947
Darren Hart52400ba2009-04-03 13:40:49 -07002948 /* Queue the futex_q, drop the hb lock, wait for wakeup. */
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02002949 futex_wait_queue_me(hb, &q, to);
Darren Hart52400ba2009-04-03 13:40:49 -07002950
2951 spin_lock(&hb->lock);
2952 ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
2953 spin_unlock(&hb->lock);
2954 if (ret)
2955 goto out_put_keys;
2956
2957 /*
2958 * In order for us to be here, we know our q.key == key2, and since
2959 * we took the hb->lock above, we also know that futex_requeue() has
2960 * completed and we no longer have to concern ourselves with a wakeup
Darren Hart7ada8762010-10-17 08:35:04 -07002961 * race with the atomic proxy lock acquisition by the requeue code. The
2962 * futex_requeue dropped our key1 reference and incremented our key2
2963 * reference count.
Darren Hart52400ba2009-04-03 13:40:49 -07002964 */
2965
2966 /* Check if the requeue code acquired the second futex for us. */
2967 if (!q.rt_waiter) {
2968 /*
2969 * Got the lock. We might not be the anticipated owner if we
2970 * did a lock-steal - fix up the PI-state in that case.
2971 */
2972 if (q.pi_state && (q.pi_state->owner != current)) {
2973 spin_lock(q.lock_ptr);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002974 ret = fixup_pi_state_owner(uaddr2, &q, current);
Peter Zijlstra15221812017-03-04 10:27:19 +01002975 if (ret && rt_mutex_owner(&q.pi_state->pi_mutex) == current)
2976 rt_mutex_unlock(&q.pi_state->pi_mutex);
Thomas Gleixnerfb75a422015-12-19 20:07:38 +00002977 /*
2978 * Drop the reference to the pi state which
2979 * the requeue_pi() code acquired for us.
2980 */
Thomas Gleixner29e9ee52015-12-19 20:07:39 +00002981 put_pi_state(q.pi_state);
Darren Hart52400ba2009-04-03 13:40:49 -07002982 spin_unlock(q.lock_ptr);
2983 }
2984 } else {
Peter Zijlstra6244ffc2017-03-04 10:27:18 +01002985 struct rt_mutex *pi_mutex;
2986
Darren Hart52400ba2009-04-03 13:40:49 -07002987 /*
2988 * We have been woken up by futex_unlock_pi(), a timeout, or a
2989 * signal. futex_unlock_pi() will not destroy the lock_ptr nor
2990 * the pi_state.
2991 */
Darren Hartf27071c2012-07-20 11:53:30 -07002992 WARN_ON(!q.pi_state);
Darren Hart52400ba2009-04-03 13:40:49 -07002993 pi_mutex = &q.pi_state->pi_mutex;
Peter Zijlstrace813552017-03-22 11:35:57 +01002994 ret = rt_mutex_wait_proxy_lock(pi_mutex, to, &rt_waiter);
Darren Hart52400ba2009-04-03 13:40:49 -07002995
2996 spin_lock(q.lock_ptr);
Peter Zijlstrace813552017-03-22 11:35:57 +01002997 if (ret && !rt_mutex_cleanup_proxy_lock(pi_mutex, &rt_waiter))
2998 ret = 0;
2999
3000 debug_rt_mutex_free_waiter(&rt_waiter);
Darren Hart52400ba2009-04-03 13:40:49 -07003001 /*
3002 * Fixup the pi_state owner and possibly acquire the lock if we
3003 * haven't already.
3004 */
Thomas Gleixnerae791a22010-11-10 13:30:36 +01003005 res = fixup_owner(uaddr2, &q, !ret);
Darren Hart52400ba2009-04-03 13:40:49 -07003006 /*
3007 * If fixup_owner() returned an error, proprogate that. If it
Darren Hart56ec1602009-09-21 22:29:59 -07003008 * acquired the lock, clear -ETIMEDOUT or -EINTR.
Darren Hart52400ba2009-04-03 13:40:49 -07003009 */
3010 if (res)
3011 ret = (res < 0) ? res : 0;
3012
Peter Zijlstra6244ffc2017-03-04 10:27:18 +01003013 /*
3014 * If fixup_pi_state_owner() faulted and was unable to handle
3015 * the fault, unlock the rt_mutex and return the fault to
3016 * userspace.
3017 */
3018 if (ret && rt_mutex_owner(pi_mutex) == current)
3019 rt_mutex_unlock(pi_mutex);
3020
Darren Hart52400ba2009-04-03 13:40:49 -07003021 /* Unqueue and drop the lock. */
3022 unqueue_me_pi(&q);
3023 }
3024
Peter Zijlstra6244ffc2017-03-04 10:27:18 +01003025 if (ret == -EINTR) {
Darren Hart52400ba2009-04-03 13:40:49 -07003026 /*
Darren Hartcc6db4e2009-07-31 16:20:10 -07003027 * We've already been requeued, but cannot restart by calling
3028 * futex_lock_pi() directly. We could restart this syscall, but
3029 * it would detect that the user space "val" changed and return
3030 * -EWOULDBLOCK. Save the overhead of the restart and return
3031 * -EWOULDBLOCK directly.
Darren Hart52400ba2009-04-03 13:40:49 -07003032 */
Thomas Gleixner20708872009-05-19 23:04:59 +02003033 ret = -EWOULDBLOCK;
Darren Hart52400ba2009-04-03 13:40:49 -07003034 }
3035
3036out_put_keys:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01003037 put_futex_key(&q.key);
Thomas Gleixnerc8b15a72009-05-20 09:18:50 +02003038out_key2:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01003039 put_futex_key(&key2);
Darren Hart52400ba2009-04-03 13:40:49 -07003040
3041out:
3042 if (to) {
3043 hrtimer_cancel(&to->timer);
3044 destroy_hrtimer_on_stack(&to->timer);
3045 }
3046 return ret;
3047}
3048
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003049/*
3050 * Support for robust futexes: the kernel cleans up held futexes at
3051 * thread exit time.
3052 *
3053 * Implementation: user-space maintains a per-thread list of locks it
3054 * is holding. Upon do_exit(), the kernel carefully walks this list,
3055 * and marks all locks that are owned by this thread with the
Ingo Molnarc87e2832006-06-27 02:54:58 -07003056 * FUTEX_OWNER_DIED bit, and wakes up a waiter (if any). The list is
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003057 * always manipulated with the lock held, so the list is private and
3058 * per-thread. Userspace also maintains a per-thread 'list_op_pending'
3059 * field, to allow the kernel to clean up if the thread dies after
3060 * acquiring the lock, but just before it could have added itself to
3061 * the list. There can only be one such pending lock.
3062 */
3063
3064/**
Darren Hartd96ee562009-09-21 22:30:22 -07003065 * sys_set_robust_list() - Set the robust-futex list head of a task
3066 * @head: pointer to the list-head
3067 * @len: length of the list-head, as userspace expects
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003068 */
Heiko Carstens836f92a2009-01-14 14:14:33 +01003069SYSCALL_DEFINE2(set_robust_list, struct robust_list_head __user *, head,
3070 size_t, len)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003071{
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08003072 if (!futex_cmpxchg_enabled)
3073 return -ENOSYS;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003074 /*
3075 * The kernel knows only one size for now:
3076 */
3077 if (unlikely(len != sizeof(*head)))
3078 return -EINVAL;
3079
3080 current->robust_list = head;
3081
3082 return 0;
3083}
3084
3085/**
Darren Hartd96ee562009-09-21 22:30:22 -07003086 * sys_get_robust_list() - Get the robust-futex list head of a task
3087 * @pid: pid of the process [zero for current task]
3088 * @head_ptr: pointer to a list-head pointer, the kernel fills it in
3089 * @len_ptr: pointer to a length field, the kernel fills in the header size
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003090 */
Heiko Carstens836f92a2009-01-14 14:14:33 +01003091SYSCALL_DEFINE3(get_robust_list, int, pid,
3092 struct robust_list_head __user * __user *, head_ptr,
3093 size_t __user *, len_ptr)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003094{
Al Viroba46df92006-10-10 22:46:07 +01003095 struct robust_list_head __user *head;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003096 unsigned long ret;
Kees Cookbdbb7762012-03-19 16:12:53 -07003097 struct task_struct *p;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003098
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08003099 if (!futex_cmpxchg_enabled)
3100 return -ENOSYS;
3101
Kees Cookbdbb7762012-03-19 16:12:53 -07003102 rcu_read_lock();
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003103
Kees Cookbdbb7762012-03-19 16:12:53 -07003104 ret = -ESRCH;
3105 if (!pid)
3106 p = current;
3107 else {
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07003108 p = find_task_by_vpid(pid);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003109 if (!p)
3110 goto err_unlock;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003111 }
3112
Kees Cookbdbb7762012-03-19 16:12:53 -07003113 ret = -EPERM;
Jann Horncaaee622016-01-20 15:00:04 -08003114 if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS))
Kees Cookbdbb7762012-03-19 16:12:53 -07003115 goto err_unlock;
3116
3117 head = p->robust_list;
3118 rcu_read_unlock();
3119
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003120 if (put_user(sizeof(*head), len_ptr))
3121 return -EFAULT;
3122 return put_user(head, head_ptr);
3123
3124err_unlock:
Oleg Nesterovaaa2a972006-09-29 02:00:55 -07003125 rcu_read_unlock();
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003126
3127 return ret;
3128}
3129
3130/*
3131 * Process a futex-list entry, check whether it's owned by the
3132 * dying task, and do notification if so:
3133 */
Arnd Bergmannbdb116c2021-02-01 10:01:32 +00003134static int handle_futex_death(u32 __user *uaddr, struct task_struct *curr, int pi)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003135{
Vitaliy Ivanov7cfdaf32011-07-07 15:10:31 +03003136 u32 uval, uninitialized_var(nval), mval;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003137
Chen Jie726c28f2019-03-15 03:44:38 +00003138 /* Futex address must be 32bit aligned */
3139 if ((((unsigned long)uaddr) % sizeof(*uaddr)) != 0)
3140 return -1;
3141
Ingo Molnar8f17d3a2006-03-27 01:16:27 -08003142retry:
3143 if (get_user(uval, uaddr))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003144 return -1;
3145
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003146 if ((uval & FUTEX_TID_MASK) == task_pid_vnr(curr)) {
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003147 /*
3148 * Ok, this dying thread is truly holding a futex
3149 * of interest. Set the OWNER_DIED bit atomically
3150 * via cmpxchg, and if the value had FUTEX_WAITERS
3151 * set, wake up a waiter (if any). (We have to do a
3152 * futex_wake() even if OWNER_DIED is already set -
3153 * to handle the rare but possible case of recursive
3154 * thread-death.) The rest of the cleanup is done in
3155 * userspace.
3156 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003157 mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
Thomas Gleixner6e0aa9f2011-03-14 10:34:35 +01003158 /*
3159 * We are not holding a lock here, but we want to have
3160 * the pagefault_disable/enable() protection because
3161 * we want to handle the fault gracefully. If the
3162 * access fails we try to fault in the futex with R/W
3163 * verification via get_user_pages. get_user() above
3164 * does not guarantee R/W access. If that fails we
3165 * give up and leave the futex locked.
3166 */
3167 if (cmpxchg_futex_value_locked(&nval, uaddr, uval, mval)) {
3168 if (fault_in_user_writeable(uaddr))
3169 return -1;
3170 goto retry;
3171 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07003172 if (nval != uval)
Ingo Molnar8f17d3a2006-03-27 01:16:27 -08003173 goto retry;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003174
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003175 /*
3176 * Wake robust non-PI futexes here. The wakeup of
3177 * PI futexes happens in exit_pi_state():
3178 */
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07003179 if (!pi && (uval & FUTEX_WAITERS))
Peter Zijlstrac2f9f202008-09-26 19:32:23 +02003180 futex_wake(uaddr, 1, 1, FUTEX_BITSET_MATCH_ANY);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003181 }
3182 return 0;
3183}
3184
3185/*
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003186 * Fetch a robust-list pointer. Bit 0 signals PI futexes:
3187 */
3188static inline int fetch_robust_entry(struct robust_list __user **entry,
Al Viroba46df92006-10-10 22:46:07 +01003189 struct robust_list __user * __user *head,
Namhyung Kim1dcc41b2010-09-14 21:43:46 +09003190 unsigned int *pi)
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003191{
3192 unsigned long uentry;
3193
Al Viroba46df92006-10-10 22:46:07 +01003194 if (get_user(uentry, (unsigned long __user *)head))
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003195 return -EFAULT;
3196
Al Viroba46df92006-10-10 22:46:07 +01003197 *entry = (void __user *)(uentry & ~1UL);
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003198 *pi = uentry & 1;
3199
3200 return 0;
3201}
3202
3203/*
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003204 * Walk curr->robust_list (very carefully, it's a userspace list!)
3205 * and mark any locks found there dead, and notify any waiters.
3206 *
3207 * We silently return on any sign of list-walking problem.
3208 */
Thomas Gleixner25f319b2021-02-01 10:01:33 +00003209static void exit_robust_list(struct task_struct *curr)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003210{
3211 struct robust_list_head __user *head = curr->robust_list;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003212 struct robust_list __user *entry, *next_entry, *pending;
Darren Hart4c115e92010-11-04 15:00:00 -04003213 unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
3214 unsigned int uninitialized_var(next_pi);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003215 unsigned long futex_offset;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003216 int rc;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003217
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08003218 if (!futex_cmpxchg_enabled)
3219 return;
3220
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003221 /*
3222 * Fetch the list head (which was registered earlier, via
3223 * sys_set_robust_list()):
3224 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003225 if (fetch_robust_entry(&entry, &head->list.next, &pi))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003226 return;
3227 /*
3228 * Fetch the relative futex offset:
3229 */
3230 if (get_user(futex_offset, &head->futex_offset))
3231 return;
3232 /*
3233 * Fetch any possibly pending lock-add first, and handle it
3234 * if it exists:
3235 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003236 if (fetch_robust_entry(&pending, &head->list_op_pending, &pip))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003237 return;
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003238
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003239 next_entry = NULL; /* avoid warning with gcc */
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003240 while (entry != &head->list) {
3241 /*
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003242 * Fetch the next entry in the list before calling
3243 * handle_futex_death:
3244 */
3245 rc = fetch_robust_entry(&next_entry, &entry->next, &next_pi);
3246 /*
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003247 * A pending lock might already be on the list, so
Ingo Molnarc87e2832006-06-27 02:54:58 -07003248 * don't process it twice:
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003249 */
3250 if (entry != pending)
Al Viroba46df92006-10-10 22:46:07 +01003251 if (handle_futex_death((void __user *)entry + futex_offset,
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003252 curr, pi))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003253 return;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003254 if (rc)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003255 return;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003256 entry = next_entry;
3257 pi = next_pi;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003258 /*
3259 * Avoid excessively long or circular lists:
3260 */
3261 if (!--limit)
3262 break;
3263
3264 cond_resched();
3265 }
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003266
3267 if (pending)
3268 handle_futex_death((void __user *)pending + futex_offset,
3269 curr, pip);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003270}
3271
Thomas Gleixnerff3a33f2021-02-01 10:01:40 +00003272static void futex_cleanup(struct task_struct *tsk)
Thomas Gleixner25f319b2021-02-01 10:01:33 +00003273{
3274 if (unlikely(tsk->robust_list)) {
3275 exit_robust_list(tsk);
3276 tsk->robust_list = NULL;
3277 }
3278
3279#ifdef CONFIG_COMPAT
3280 if (unlikely(tsk->compat_robust_list)) {
3281 compat_exit_robust_list(tsk);
3282 tsk->compat_robust_list = NULL;
3283 }
3284#endif
3285
3286 if (unlikely(!list_empty(&tsk->pi_state_list)))
3287 exit_pi_state_list(tsk);
3288}
3289
Thomas Gleixner32d78282021-02-01 10:01:38 +00003290/**
3291 * futex_exit_recursive - Set the tasks futex state to FUTEX_STATE_DEAD
3292 * @tsk: task to set the state on
3293 *
3294 * Set the futex exit state of the task lockless. The futex waiter code
3295 * observes that state when a task is exiting and loops until the task has
3296 * actually finished the futex cleanup. The worst case for this is that the
3297 * waiter runs through the wait loop until the state becomes visible.
3298 *
3299 * This is called from the recursive fault handling path in do_exit().
3300 *
3301 * This is best effort. Either the futex exit code has run already or
3302 * not. If the OWNER_DIED bit has been set on the futex then the waiter can
3303 * take it over. If not, the problem is pushed back to user space. If the
3304 * futex exit code did not run yet, then an already queued waiter might
3305 * block forever, but there is nothing which can be done about that.
3306 */
3307void futex_exit_recursive(struct task_struct *tsk)
3308{
3309 tsk->futex_state = FUTEX_STATE_DEAD;
3310}
3311
Thomas Gleixnerff3a33f2021-02-01 10:01:40 +00003312static void futex_cleanup_begin(struct task_struct *tsk)
Thomas Gleixner8a16d8a2021-02-01 10:01:36 +00003313{
Thomas Gleixner32d78282021-02-01 10:01:38 +00003314 /*
Thomas Gleixner0ba263f2021-02-01 10:01:39 +00003315 * Switch the state to FUTEX_STATE_EXITING under tsk->pi_lock.
3316 *
3317 * This ensures that all subsequent checks of tsk->futex_state in
3318 * attach_to_pi_owner() must observe FUTEX_STATE_EXITING with
3319 * tsk->pi_lock held.
3320 *
3321 * It guarantees also that a pi_state which was queued right before
3322 * the state change under tsk->pi_lock by a concurrent waiter must
3323 * be observed in exit_pi_state_list().
Thomas Gleixner32d78282021-02-01 10:01:38 +00003324 */
3325 raw_spin_lock_irq(&tsk->pi_lock);
Thomas Gleixner0ba263f2021-02-01 10:01:39 +00003326 tsk->futex_state = FUTEX_STATE_EXITING;
Thomas Gleixner32d78282021-02-01 10:01:38 +00003327 raw_spin_unlock_irq(&tsk->pi_lock);
Thomas Gleixnerff3a33f2021-02-01 10:01:40 +00003328}
Thomas Gleixner32d78282021-02-01 10:01:38 +00003329
Thomas Gleixnerff3a33f2021-02-01 10:01:40 +00003330static void futex_cleanup_end(struct task_struct *tsk, int state)
3331{
3332 /*
3333 * Lockless store. The only side effect is that an observer might
3334 * take another loop until it becomes visible.
3335 */
3336 tsk->futex_state = state;
3337}
Thomas Gleixner32d78282021-02-01 10:01:38 +00003338
Thomas Gleixnerff3a33f2021-02-01 10:01:40 +00003339void futex_exec_release(struct task_struct *tsk)
3340{
3341 /*
3342 * The state handling is done for consistency, but in the case of
3343 * exec() there is no way to prevent futher damage as the PID stays
3344 * the same. But for the unlikely and arguably buggy case that a
3345 * futex is held on exec(), this provides at least as much state
3346 * consistency protection which is possible.
3347 */
3348 futex_cleanup_begin(tsk);
3349 futex_cleanup(tsk);
3350 /*
3351 * Reset the state to FUTEX_STATE_OK. The task is alive and about
3352 * exec a new binary.
3353 */
3354 futex_cleanup_end(tsk, FUTEX_STATE_OK);
3355}
3356
3357void futex_exit_release(struct task_struct *tsk)
3358{
3359 futex_cleanup_begin(tsk);
3360 futex_cleanup(tsk);
3361 futex_cleanup_end(tsk, FUTEX_STATE_DEAD);
Thomas Gleixner8a16d8a2021-02-01 10:01:36 +00003362}
3363
Pierre Peifferc19384b2007-05-09 02:35:02 -07003364long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
Ingo Molnare2970f22006-06-27 02:54:47 -07003365 u32 __user *uaddr2, u32 val2, u32 val3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366{
Thomas Gleixner81b40532012-02-15 12:17:09 +01003367 int cmd = op & FUTEX_CMD_MASK;
Darren Hartb41277d2010-11-08 13:10:09 -08003368 unsigned int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003370 if (!(op & FUTEX_PRIVATE_FLAG))
Darren Hartb41277d2010-11-08 13:10:09 -08003371 flags |= FLAGS_SHARED;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003372
Darren Hartb41277d2010-11-08 13:10:09 -08003373 if (op & FUTEX_CLOCK_REALTIME) {
3374 flags |= FLAGS_CLOCKRT;
Darren Hart337f1302015-12-18 13:36:37 -08003375 if (cmd != FUTEX_WAIT && cmd != FUTEX_WAIT_BITSET && \
3376 cmd != FUTEX_WAIT_REQUEUE_PI)
Darren Hartb41277d2010-11-08 13:10:09 -08003377 return -ENOSYS;
3378 }
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003379
3380 switch (cmd) {
Thomas Gleixner59263b52012-02-15 12:08:34 +01003381 case FUTEX_LOCK_PI:
3382 case FUTEX_UNLOCK_PI:
3383 case FUTEX_TRYLOCK_PI:
3384 case FUTEX_WAIT_REQUEUE_PI:
3385 case FUTEX_CMP_REQUEUE_PI:
3386 if (!futex_cmpxchg_enabled)
3387 return -ENOSYS;
3388 }
3389
3390 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 case FUTEX_WAIT:
Thomas Gleixnercd689982008-02-01 17:45:14 +01003392 val3 = FUTEX_BITSET_MATCH_ANY;
3393 case FUTEX_WAIT_BITSET:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003394 return futex_wait(uaddr, flags, val, timeout, val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 case FUTEX_WAKE:
Thomas Gleixnercd689982008-02-01 17:45:14 +01003396 val3 = FUTEX_BITSET_MATCH_ANY;
3397 case FUTEX_WAKE_BITSET:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003398 return futex_wake(uaddr, flags, val, val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 case FUTEX_REQUEUE:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003400 return futex_requeue(uaddr, flags, uaddr2, val, val2, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 case FUTEX_CMP_REQUEUE:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003402 return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 0);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07003403 case FUTEX_WAKE_OP:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003404 return futex_wake_op(uaddr, flags, uaddr2, val, val2, val3);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003405 case FUTEX_LOCK_PI:
Michael Kerrisk996636d2015-01-16 20:28:06 +01003406 return futex_lock_pi(uaddr, flags, timeout, 0);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003407 case FUTEX_UNLOCK_PI:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003408 return futex_unlock_pi(uaddr, flags);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003409 case FUTEX_TRYLOCK_PI:
Michael Kerrisk996636d2015-01-16 20:28:06 +01003410 return futex_lock_pi(uaddr, flags, NULL, 1);
Darren Hart52400ba2009-04-03 13:40:49 -07003411 case FUTEX_WAIT_REQUEUE_PI:
3412 val3 = FUTEX_BITSET_MATCH_ANY;
Thomas Gleixner81b40532012-02-15 12:17:09 +01003413 return futex_wait_requeue_pi(uaddr, flags, val, timeout, val3,
3414 uaddr2);
Darren Hart52400ba2009-04-03 13:40:49 -07003415 case FUTEX_CMP_REQUEUE_PI:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003416 return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 }
Thomas Gleixner81b40532012-02-15 12:17:09 +01003418 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419}
3420
3421
Heiko Carstens17da2bd2009-01-14 14:14:10 +01003422SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val,
3423 struct timespec __user *, utime, u32 __user *, uaddr2,
3424 u32, val3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425{
Pierre Peifferc19384b2007-05-09 02:35:02 -07003426 struct timespec ts;
3427 ktime_t t, *tp = NULL;
Ingo Molnare2970f22006-06-27 02:54:47 -07003428 u32 val2 = 0;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003429 int cmd = op & FUTEX_CMD_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430
Thomas Gleixnercd689982008-02-01 17:45:14 +01003431 if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI ||
Darren Hart52400ba2009-04-03 13:40:49 -07003432 cmd == FUTEX_WAIT_BITSET ||
3433 cmd == FUTEX_WAIT_REQUEUE_PI)) {
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07003434 if (unlikely(should_fail_futex(!(op & FUTEX_PRIVATE_FLAG))))
3435 return -EFAULT;
Pierre Peifferc19384b2007-05-09 02:35:02 -07003436 if (copy_from_user(&ts, utime, sizeof(ts)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 return -EFAULT;
Pierre Peifferc19384b2007-05-09 02:35:02 -07003438 if (!timespec_valid(&ts))
Thomas Gleixner9741ef962006-03-31 02:31:32 -08003439 return -EINVAL;
Pierre Peifferc19384b2007-05-09 02:35:02 -07003440
3441 t = timespec_to_ktime(ts);
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003442 if (cmd == FUTEX_WAIT)
Thomas Gleixner5a7780e2008-02-13 09:20:43 +01003443 t = ktime_add_safe(ktime_get(), t);
Pierre Peifferc19384b2007-05-09 02:35:02 -07003444 tp = &t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 }
3446 /*
Darren Hart52400ba2009-04-03 13:40:49 -07003447 * requeue parameter in 'utime' if cmd == FUTEX_*_REQUEUE_*.
Andreas Schwabf54f0982007-07-31 00:38:51 -07003448 * number of waiters to wake in 'utime' if cmd == FUTEX_WAKE_OP.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 */
Andreas Schwabf54f0982007-07-31 00:38:51 -07003450 if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE ||
Darren Hartba9c22f2009-04-20 22:22:22 -07003451 cmd == FUTEX_CMP_REQUEUE_PI || cmd == FUTEX_WAKE_OP)
Ingo Molnare2970f22006-06-27 02:54:47 -07003452 val2 = (u32) (unsigned long) utime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453
Pierre Peifferc19384b2007-05-09 02:35:02 -07003454 return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455}
3456
Arnd Bergmannbdb116c2021-02-01 10:01:32 +00003457#ifdef CONFIG_COMPAT
3458/*
3459 * Fetch a robust-list pointer. Bit 0 signals PI futexes:
3460 */
3461static inline int
3462compat_fetch_robust_entry(compat_uptr_t *uentry, struct robust_list __user **entry,
3463 compat_uptr_t __user *head, unsigned int *pi)
3464{
3465 if (get_user(*uentry, head))
3466 return -EFAULT;
3467
3468 *entry = compat_ptr((*uentry) & ~1);
3469 *pi = (unsigned int)(*uentry) & 1;
3470
3471 return 0;
3472}
3473
3474static void __user *futex_uaddr(struct robust_list __user *entry,
3475 compat_long_t futex_offset)
3476{
3477 compat_uptr_t base = ptr_to_compat(entry);
3478 void __user *uaddr = compat_ptr(base + futex_offset);
3479
3480 return uaddr;
3481}
3482
3483/*
3484 * Walk curr->robust_list (very carefully, it's a userspace list!)
3485 * and mark any locks found there dead, and notify any waiters.
3486 *
3487 * We silently return on any sign of list-walking problem.
3488 */
3489void compat_exit_robust_list(struct task_struct *curr)
3490{
3491 struct compat_robust_list_head __user *head = curr->compat_robust_list;
3492 struct robust_list __user *entry, *next_entry, *pending;
3493 unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
3494 unsigned int uninitialized_var(next_pi);
3495 compat_uptr_t uentry, next_uentry, upending;
3496 compat_long_t futex_offset;
3497 int rc;
3498
3499 if (!futex_cmpxchg_enabled)
3500 return;
3501
3502 /*
3503 * Fetch the list head (which was registered earlier, via
3504 * sys_set_robust_list()):
3505 */
3506 if (compat_fetch_robust_entry(&uentry, &entry, &head->list.next, &pi))
3507 return;
3508 /*
3509 * Fetch the relative futex offset:
3510 */
3511 if (get_user(futex_offset, &head->futex_offset))
3512 return;
3513 /*
3514 * Fetch any possibly pending lock-add first, and handle it
3515 * if it exists:
3516 */
3517 if (compat_fetch_robust_entry(&upending, &pending,
3518 &head->list_op_pending, &pip))
3519 return;
3520
3521 next_entry = NULL; /* avoid warning with gcc */
3522 while (entry != (struct robust_list __user *) &head->list) {
3523 /*
3524 * Fetch the next entry in the list before calling
3525 * handle_futex_death:
3526 */
3527 rc = compat_fetch_robust_entry(&next_uentry, &next_entry,
3528 (compat_uptr_t __user *)&entry->next, &next_pi);
3529 /*
3530 * A pending lock might already be on the list, so
3531 * dont process it twice:
3532 */
3533 if (entry != pending) {
3534 void __user *uaddr = futex_uaddr(entry, futex_offset);
3535
3536 if (handle_futex_death(uaddr, curr, pi))
3537 return;
3538 }
3539 if (rc)
3540 return;
3541 uentry = next_uentry;
3542 entry = next_entry;
3543 pi = next_pi;
3544 /*
3545 * Avoid excessively long or circular lists:
3546 */
3547 if (!--limit)
3548 break;
3549
3550 cond_resched();
3551 }
3552 if (pending) {
3553 void __user *uaddr = futex_uaddr(pending, futex_offset);
3554
3555 handle_futex_death(uaddr, curr, pip);
3556 }
3557}
3558
3559COMPAT_SYSCALL_DEFINE2(set_robust_list,
3560 struct compat_robust_list_head __user *, head,
3561 compat_size_t, len)
3562{
3563 if (!futex_cmpxchg_enabled)
3564 return -ENOSYS;
3565
3566 if (unlikely(len != sizeof(*head)))
3567 return -EINVAL;
3568
3569 current->compat_robust_list = head;
3570
3571 return 0;
3572}
3573
3574COMPAT_SYSCALL_DEFINE3(get_robust_list, int, pid,
3575 compat_uptr_t __user *, head_ptr,
3576 compat_size_t __user *, len_ptr)
3577{
3578 struct compat_robust_list_head __user *head;
3579 unsigned long ret;
3580 struct task_struct *p;
3581
3582 if (!futex_cmpxchg_enabled)
3583 return -ENOSYS;
3584
3585 rcu_read_lock();
3586
3587 ret = -ESRCH;
3588 if (!pid)
3589 p = current;
3590 else {
3591 p = find_task_by_vpid(pid);
3592 if (!p)
3593 goto err_unlock;
3594 }
3595
3596 ret = -EPERM;
3597 if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS))
3598 goto err_unlock;
3599
3600 head = p->compat_robust_list;
3601 rcu_read_unlock();
3602
3603 if (put_user(sizeof(*head), len_ptr))
3604 return -EFAULT;
3605 return put_user(ptr_to_compat(head), head_ptr);
3606
3607err_unlock:
3608 rcu_read_unlock();
3609
3610 return ret;
3611}
3612
3613COMPAT_SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val,
3614 struct compat_timespec __user *, utime, u32 __user *, uaddr2,
3615 u32, val3)
3616{
3617 struct timespec ts;
3618 ktime_t t, *tp = NULL;
3619 int val2 = 0;
3620 int cmd = op & FUTEX_CMD_MASK;
3621
3622 if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI ||
3623 cmd == FUTEX_WAIT_BITSET ||
3624 cmd == FUTEX_WAIT_REQUEUE_PI)) {
3625 if (compat_get_timespec(&ts, utime))
3626 return -EFAULT;
3627 if (!timespec_valid(&ts))
3628 return -EINVAL;
3629
3630 t = timespec_to_ktime(ts);
3631 if (cmd == FUTEX_WAIT)
3632 t = ktime_add_safe(ktime_get(), t);
3633 tp = &t;
3634 }
3635 if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE ||
3636 cmd == FUTEX_CMP_REQUEUE_PI || cmd == FUTEX_WAKE_OP)
3637 val2 = (int) (unsigned long) utime;
3638
3639 return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
3640}
3641#endif /* CONFIG_COMPAT */
3642
Heiko Carstens03b8c7b2014-03-02 13:09:47 +01003643static void __init futex_detect_cmpxchg(void)
3644{
3645#ifndef CONFIG_HAVE_FUTEX_CMPXCHG
3646 u32 curval;
3647
3648 /*
3649 * This will fail and we want it. Some arch implementations do
3650 * runtime detection of the futex_atomic_cmpxchg_inatomic()
3651 * functionality. We want to know that before we call in any
3652 * of the complex code paths. Also we want to prevent
3653 * registration of robust lists in that case. NULL is
3654 * guaranteed to fault and we get -EFAULT on functional
3655 * implementation, the non-functional ones will return
3656 * -ENOSYS.
3657 */
3658 if (cmpxchg_futex_value_locked(&curval, NULL, 0, 0) == -EFAULT)
3659 futex_cmpxchg_enabled = 1;
3660#endif
3661}
3662
Benjamin Herrenschmidtf6d107f2008-03-27 14:52:15 +11003663static int __init futex_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664{
Heiko Carstens63b1a812014-01-16 14:54:50 +01003665 unsigned int futex_shift;
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -08003666 unsigned long i;
3667
3668#if CONFIG_BASE_SMALL
3669 futex_hashsize = 16;
3670#else
3671 futex_hashsize = roundup_pow_of_two(256 * num_possible_cpus());
3672#endif
3673
3674 futex_queues = alloc_large_system_hash("futex", sizeof(*futex_queues),
3675 futex_hashsize, 0,
3676 futex_hashsize < 256 ? HASH_SMALL : 0,
Heiko Carstens63b1a812014-01-16 14:54:50 +01003677 &futex_shift, NULL,
3678 futex_hashsize, futex_hashsize);
3679 futex_hashsize = 1UL << futex_shift;
Heiko Carstens03b8c7b2014-03-02 13:09:47 +01003680
3681 futex_detect_cmpxchg();
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08003682
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -08003683 for (i = 0; i < futex_hashsize; i++) {
Linus Torvalds11d46162014-03-20 22:11:17 -07003684 atomic_set(&futex_queues[i].waiters, 0);
Dima Zavin732375c2011-07-07 17:27:59 -07003685 plist_head_init(&futex_queues[i].chain);
Thomas Gleixner3e4ab742008-02-23 15:23:55 -08003686 spin_lock_init(&futex_queues[i].lock);
3687 }
3688
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 return 0;
3690}
Yang Yang808de342016-12-30 16:17:55 +08003691core_initcall(futex_init);