blob: b2404a5a2e4174c46f358182ae98937f07e86dae [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 */
47#include <linux/slab.h>
48#include <linux/poll.h>
49#include <linux/fs.h>
50#include <linux/file.h>
51#include <linux/jhash.h>
52#include <linux/init.h>
53#include <linux/futex.h>
54#include <linux/mount.h>
55#include <linux/pagemap.h>
56#include <linux/syscalls.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070057#include <linux/signal.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040058#include <linux/export.h>
Andrey Mirkinfd5eea42007-10-16 23:30:13 -070059#include <linux/magic.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070060#include <linux/pid.h>
61#include <linux/nsproxy.h>
Kees Cookbdbb7762012-03-19 16:12:53 -070062#include <linux/ptrace.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060063#include <linux/sched/rt.h>
Zhang Yi13d60f42013-06-25 21:19:31 +080064#include <linux/hugetlb.h>
Colin Cross88c80042013-05-01 18:35:05 -070065#include <linux/freezer.h>
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -080066#include <linux/bootmem.h>
Davidlohr Buesoab51fba2015-06-29 23:26:02 -070067#include <linux/fault-inject.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070068
Jakub Jelinek4732efb2005-09-06 15:16:25 -070069#include <asm/futex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Peter Zijlstra1696a8b2013-10-31 18:18:19 +010071#include "locking/rtmutex_common.h"
Ingo Molnarc87e2832006-06-27 02:54:58 -070072
Thomas Gleixner99b60ce2014-01-12 15:31:24 -080073/*
Davidlohr Buesod7e8af12014-04-09 11:55:07 -070074 * READ this before attempting to hack on futexes!
75 *
76 * Basic futex operation and ordering guarantees
77 * =============================================
Thomas Gleixner99b60ce2014-01-12 15:31:24 -080078 *
79 * The waiter reads the futex value in user space and calls
80 * futex_wait(). This function computes the hash bucket and acquires
81 * the hash bucket lock. After that it reads the futex user space value
Davidlohr Buesob0c29f72014-01-12 15:31:25 -080082 * again and verifies that the data has not changed. If it has not changed
83 * it enqueues itself into the hash bucket, releases the hash bucket lock
84 * and schedules.
Thomas Gleixner99b60ce2014-01-12 15:31:24 -080085 *
86 * The waker side modifies the user space value of the futex and calls
Davidlohr Buesob0c29f72014-01-12 15:31:25 -080087 * futex_wake(). This function computes the hash bucket and acquires the
88 * hash bucket lock. Then it looks for waiters on that futex in the hash
89 * bucket and wakes them.
Thomas Gleixner99b60ce2014-01-12 15:31:24 -080090 *
Davidlohr Buesob0c29f72014-01-12 15:31:25 -080091 * In futex wake up scenarios where no tasks are blocked on a futex, taking
92 * the hb spinlock can be avoided and simply return. In order for this
93 * optimization to work, ordering guarantees must exist so that the waiter
94 * being added to the list is acknowledged when the list is concurrently being
95 * checked by the waker, avoiding scenarios like the following:
Thomas Gleixner99b60ce2014-01-12 15:31:24 -080096 *
97 * CPU 0 CPU 1
98 * val = *futex;
99 * sys_futex(WAIT, futex, val);
100 * futex_wait(futex, val);
101 * uval = *futex;
102 * *futex = newval;
103 * sys_futex(WAKE, futex);
104 * futex_wake(futex);
105 * if (queue_empty())
106 * return;
107 * if (uval == val)
108 * lock(hash_bucket(futex));
109 * queue();
110 * unlock(hash_bucket(futex));
111 * schedule();
112 *
113 * This would cause the waiter on CPU 0 to wait forever because it
114 * missed the transition of the user space value from val to newval
115 * and the waker did not find the waiter in the hash bucket queue.
Thomas Gleixner99b60ce2014-01-12 15:31:24 -0800116 *
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800117 * The correct serialization ensures that a waiter either observes
118 * the changed user space value before blocking or is woken by a
119 * concurrent waker:
120 *
121 * CPU 0 CPU 1
Thomas Gleixner99b60ce2014-01-12 15:31:24 -0800122 * val = *futex;
123 * sys_futex(WAIT, futex, val);
124 * futex_wait(futex, val);
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800125 *
Davidlohr Buesod7e8af12014-04-09 11:55:07 -0700126 * waiters++; (a)
Davidlohr Bueso8ad7b372016-02-09 11:15:13 -0800127 * smp_mb(); (A) <-- paired with -.
128 * |
129 * lock(hash_bucket(futex)); |
130 * |
131 * uval = *futex; |
132 * | *futex = newval;
133 * | sys_futex(WAKE, futex);
134 * | futex_wake(futex);
135 * |
136 * `--------> smp_mb(); (B)
Thomas Gleixner99b60ce2014-01-12 15:31:24 -0800137 * if (uval == val)
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800138 * queue();
Thomas Gleixner99b60ce2014-01-12 15:31:24 -0800139 * unlock(hash_bucket(futex));
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800140 * schedule(); if (waiters)
141 * lock(hash_bucket(futex));
Davidlohr Buesod7e8af12014-04-09 11:55:07 -0700142 * else wake_waiters(futex);
143 * waiters--; (b) unlock(hash_bucket(futex));
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800144 *
Davidlohr Buesod7e8af12014-04-09 11:55:07 -0700145 * Where (A) orders the waiters increment and the futex value read through
146 * atomic operations (see hb_waiters_inc) and where (B) orders the write
Davidlohr Bueso993b2ff2014-10-23 20:27:00 -0700147 * to futex and the waiters read -- this is done by the barriers for both
148 * shared and private futexes in get_futex_key_refs().
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800149 *
150 * This yields the following case (where X:=waiters, Y:=futex):
151 *
152 * X = Y = 0
153 *
154 * w[X]=1 w[Y]=1
155 * MB MB
156 * r[Y]=y r[X]=x
157 *
158 * Which guarantees that x==0 && y==0 is impossible; which translates back into
159 * the guarantee that we cannot both miss the futex variable change and the
160 * enqueue.
Davidlohr Buesod7e8af12014-04-09 11:55:07 -0700161 *
162 * Note that a new waiter is accounted for in (a) even when it is possible that
163 * the wait call can return error, in which case we backtrack from it in (b).
164 * Refer to the comment in queue_lock().
165 *
166 * Similarly, in order to account for waiters being requeued on another
167 * address we always increment the waiters for the destination bucket before
168 * acquiring the lock. It then decrements them again after releasing it -
169 * the code that actually moves the futex(es) between hash buckets (requeue_futex)
170 * will do the additional required waiter count housekeeping. This is done for
171 * double_lock_hb() and double_unlock_hb(), respectively.
Thomas Gleixner99b60ce2014-01-12 15:31:24 -0800172 */
173
Heiko Carstens03b8c7b2014-03-02 13:09:47 +0100174#ifndef CONFIG_HAVE_FUTEX_CMPXCHG
Thomas Gleixnera0c1e902008-02-23 15:23:57 -0800175int __read_mostly futex_cmpxchg_enabled;
Heiko Carstens03b8c7b2014-03-02 13:09:47 +0100176#endif
Thomas Gleixnera0c1e902008-02-23 15:23:57 -0800177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/*
Darren Hartb41277d2010-11-08 13:10:09 -0800179 * Futex flags used to encode options to functions and preserve them across
180 * restarts.
181 */
Thomas Gleixner784bdf32016-07-29 16:32:30 +0200182#ifdef CONFIG_MMU
183# define FLAGS_SHARED 0x01
184#else
185/*
186 * NOMMU does not have per process address space. Let the compiler optimize
187 * code away.
188 */
189# define FLAGS_SHARED 0x00
190#endif
Darren Hartb41277d2010-11-08 13:10:09 -0800191#define FLAGS_CLOCKRT 0x02
192#define FLAGS_HAS_TIMEOUT 0x04
193
194/*
Ingo Molnarc87e2832006-06-27 02:54:58 -0700195 * Priority Inheritance state:
196 */
197struct futex_pi_state {
198 /*
199 * list of 'owned' pi_state instances - these have to be
200 * cleaned up in do_exit() if the task exits prematurely:
201 */
202 struct list_head list;
203
204 /*
205 * The PI object:
206 */
207 struct rt_mutex pi_mutex;
208
209 struct task_struct *owner;
210 atomic_t refcount;
211
212 union futex_key key;
213};
214
Darren Hartd8d88fb2009-09-21 22:30:30 -0700215/**
216 * struct futex_q - The hashed futex queue entry, one per waiting task
Randy Dunlapfb62db22010-10-13 11:02:34 -0700217 * @list: priority-sorted list of tasks waiting on this futex
Darren Hartd8d88fb2009-09-21 22:30:30 -0700218 * @task: the task waiting on the futex
219 * @lock_ptr: the hash bucket lock
220 * @key: the key the futex is hashed on
221 * @pi_state: optional priority inheritance state
222 * @rt_waiter: rt_waiter storage for use with requeue_pi
223 * @requeue_pi_key: the requeue_pi target futex key
224 * @bitset: bitset for the optional bitmasked wakeup
225 *
226 * We use this hashed waitqueue, instead of a normal wait_queue_t, so
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 * we can wake only the relevant ones (hashed queues may be shared).
228 *
229 * A futex_q has a woken state, just like tasks have TASK_RUNNING.
Pierre Peifferec92d082007-05-09 02:35:00 -0700230 * It is considered woken when plist_node_empty(&q->list) || q->lock_ptr == 0.
Randy Dunlapfb62db22010-10-13 11:02:34 -0700231 * The order of wakeup is always to make the first condition true, then
Darren Hartd8d88fb2009-09-21 22:30:30 -0700232 * the second.
233 *
234 * PI futexes are typically woken before they are removed from the hash list via
235 * the rt_mutex code. See unqueue_me_pi().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 */
237struct futex_q {
Pierre Peifferec92d082007-05-09 02:35:00 -0700238 struct plist_node list;
Darren Hartd8d88fb2009-09-21 22:30:30 -0700239
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +0200240 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 spinlock_t *lock_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 union futex_key key;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700243 struct futex_pi_state *pi_state;
Darren Hart52400ba2009-04-03 13:40:49 -0700244 struct rt_mutex_waiter *rt_waiter;
Darren Hart84bc4af2009-08-13 17:36:53 -0700245 union futex_key *requeue_pi_key;
Thomas Gleixnercd689982008-02-01 17:45:14 +0100246 u32 bitset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247};
248
Darren Hart5bdb05f2010-11-08 13:40:28 -0800249static const struct futex_q futex_q_init = {
250 /* list gets initialized in queue_me()*/
251 .key = FUTEX_KEY_INIT,
252 .bitset = FUTEX_BITSET_MATCH_ANY
253};
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255/*
Darren Hartb2d09942009-03-12 00:55:37 -0700256 * Hash buckets are shared by all the futex_keys that hash to the same
257 * location. Each key may have multiple futex_q structures, one for each task
258 * waiting on a futex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 */
260struct futex_hash_bucket {
Linus Torvalds11d46162014-03-20 22:11:17 -0700261 atomic_t waiters;
Pierre Peifferec92d082007-05-09 02:35:00 -0700262 spinlock_t lock;
263 struct plist_head chain;
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -0800264} ____cacheline_aligned_in_smp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Rasmus Villemoesac742d32015-09-09 23:36:40 +0200266/*
267 * The base of the bucket array and its size are always used together
268 * (after initialization only in hash_futex()), so ensure that they
269 * reside in the same cacheline.
270 */
271static struct {
272 struct futex_hash_bucket *queues;
273 unsigned long hashsize;
274} __futex_data __read_mostly __aligned(2*sizeof(long));
275#define futex_queues (__futex_data.queues)
276#define futex_hashsize (__futex_data.hashsize)
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -0800277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Davidlohr Buesoab51fba2015-06-29 23:26:02 -0700279/*
280 * Fault injections for futexes.
281 */
282#ifdef CONFIG_FAIL_FUTEX
283
284static struct {
285 struct fault_attr attr;
286
Viresh Kumar621a5f72015-09-26 15:04:07 -0700287 bool ignore_private;
Davidlohr Buesoab51fba2015-06-29 23:26:02 -0700288} fail_futex = {
289 .attr = FAULT_ATTR_INITIALIZER,
Viresh Kumar621a5f72015-09-26 15:04:07 -0700290 .ignore_private = false,
Davidlohr Buesoab51fba2015-06-29 23:26:02 -0700291};
292
293static int __init setup_fail_futex(char *str)
294{
295 return setup_fault_attr(&fail_futex.attr, str);
296}
297__setup("fail_futex=", setup_fail_futex);
298
kbuild test robot5d285a72015-07-21 01:40:45 +0800299static bool should_fail_futex(bool fshared)
Davidlohr Buesoab51fba2015-06-29 23:26:02 -0700300{
301 if (fail_futex.ignore_private && !fshared)
302 return false;
303
304 return should_fail(&fail_futex.attr, 1);
305}
306
307#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
308
309static int __init fail_futex_debugfs(void)
310{
311 umode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
312 struct dentry *dir;
313
314 dir = fault_create_debugfs_attr("fail_futex", NULL,
315 &fail_futex.attr);
316 if (IS_ERR(dir))
317 return PTR_ERR(dir);
318
319 if (!debugfs_create_bool("ignore-private", mode, dir,
320 &fail_futex.ignore_private)) {
321 debugfs_remove_recursive(dir);
322 return -ENOMEM;
323 }
324
325 return 0;
326}
327
328late_initcall(fail_futex_debugfs);
329
330#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
331
332#else
333static inline bool should_fail_futex(bool fshared)
334{
335 return false;
336}
337#endif /* CONFIG_FAIL_FUTEX */
338
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800339static inline void futex_get_mm(union futex_key *key)
340{
341 atomic_inc(&key->private.mm->mm_count);
342 /*
343 * Ensure futex_get_mm() implies a full barrier such that
344 * get_futex_key() implies a full barrier. This is relied upon
Davidlohr Bueso8ad7b372016-02-09 11:15:13 -0800345 * as smp_mb(); (B), see the ordering comment above.
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800346 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100347 smp_mb__after_atomic();
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800348}
349
Linus Torvalds11d46162014-03-20 22:11:17 -0700350/*
351 * Reflects a new waiter being added to the waitqueue.
352 */
353static inline void hb_waiters_inc(struct futex_hash_bucket *hb)
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800354{
355#ifdef CONFIG_SMP
Linus Torvalds11d46162014-03-20 22:11:17 -0700356 atomic_inc(&hb->waiters);
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800357 /*
Linus Torvalds11d46162014-03-20 22:11:17 -0700358 * Full barrier (A), see the ordering comment above.
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800359 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100360 smp_mb__after_atomic();
Linus Torvalds11d46162014-03-20 22:11:17 -0700361#endif
362}
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800363
Linus Torvalds11d46162014-03-20 22:11:17 -0700364/*
365 * Reflects a waiter being removed from the waitqueue by wakeup
366 * paths.
367 */
368static inline void hb_waiters_dec(struct futex_hash_bucket *hb)
369{
370#ifdef CONFIG_SMP
371 atomic_dec(&hb->waiters);
372#endif
373}
374
375static inline int hb_waiters_pending(struct futex_hash_bucket *hb)
376{
377#ifdef CONFIG_SMP
378 return atomic_read(&hb->waiters);
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800379#else
Linus Torvalds11d46162014-03-20 22:11:17 -0700380 return 1;
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800381#endif
382}
383
Thomas Gleixnere8b61b32016-06-01 10:43:29 +0200384/**
385 * hash_futex - Return the hash bucket in the global hash
386 * @key: Pointer to the futex key for which the hash is calculated
387 *
388 * We hash on the keys returned from get_futex_key (see below) and return the
389 * corresponding hash bucket in the global hash.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 */
391static struct futex_hash_bucket *hash_futex(union futex_key *key)
392{
393 u32 hash = jhash2((u32*)&key->both.word,
394 (sizeof(key->both.word)+sizeof(key->both.ptr))/4,
395 key->both.offset);
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -0800396 return &futex_queues[hash & (futex_hashsize - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
Thomas Gleixnere8b61b32016-06-01 10:43:29 +0200399
400/**
401 * match_futex - Check whether two futex keys are equal
402 * @key1: Pointer to key1
403 * @key2: Pointer to key2
404 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 * Return 1 if two futex_keys are equal, 0 otherwise.
406 */
407static inline int match_futex(union futex_key *key1, union futex_key *key2)
408{
Darren Hart2bc87202009-10-14 10:12:39 -0700409 return (key1 && key2
410 && key1->both.word == key2->both.word
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 && key1->both.ptr == key2->both.ptr
412 && key1->both.offset == key2->both.offset);
413}
414
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200415/*
416 * Take a reference to the resource addressed by a key.
417 * Can be called while holding spinlocks.
418 *
419 */
420static void get_futex_key_refs(union futex_key *key)
421{
422 if (!key->both.ptr)
423 return;
424
Thomas Gleixner784bdf32016-07-29 16:32:30 +0200425 /*
426 * On MMU less systems futexes are always "private" as there is no per
427 * process address space. We need the smp wmb nevertheless - yes,
428 * arch/blackfin has MMU less SMP ...
429 */
430 if (!IS_ENABLED(CONFIG_MMU)) {
431 smp_mb(); /* explicit smp_mb(); (B) */
432 return;
433 }
434
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200435 switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
436 case FUT_OFF_INODE:
Peter Zijlstrafb099f3b2020-03-04 11:28:31 +0100437 smp_mb(); /* explicit smp_mb(); (B) */
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200438 break;
439 case FUT_OFF_MMSHARED:
Davidlohr Bueso8ad7b372016-02-09 11:15:13 -0800440 futex_get_mm(key); /* implies smp_mb(); (B) */
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200441 break;
Catalin Marinas76835b0e2014-10-17 17:38:49 +0100442 default:
Davidlohr Bueso993b2ff2014-10-23 20:27:00 -0700443 /*
444 * Private futexes do not hold reference on an inode or
445 * mm, therefore the only purpose of calling get_futex_key_refs
446 * is because we need the barrier for the lockless waiter check.
447 */
Davidlohr Bueso8ad7b372016-02-09 11:15:13 -0800448 smp_mb(); /* explicit smp_mb(); (B) */
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200449 }
450}
451
452/*
453 * Drop a reference to the resource addressed by a key.
Davidlohr Bueso993b2ff2014-10-23 20:27:00 -0700454 * The hash bucket spinlock must not be held. This is
455 * a no-op for private futexes, see comment in the get
456 * counterpart.
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200457 */
458static void drop_futex_key_refs(union futex_key *key)
459{
Darren Hart90621c42008-12-29 19:43:21 -0800460 if (!key->both.ptr) {
461 /* If we're here then we tried to put a key we failed to get */
462 WARN_ON_ONCE(1);
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200463 return;
Darren Hart90621c42008-12-29 19:43:21 -0800464 }
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200465
Thomas Gleixner784bdf32016-07-29 16:32:30 +0200466 if (!IS_ENABLED(CONFIG_MMU))
467 return;
468
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200469 switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
470 case FUT_OFF_INODE:
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200471 break;
472 case FUT_OFF_MMSHARED:
473 mmdrop(key->private.mm);
474 break;
475 }
476}
477
Peter Zijlstrafb099f3b2020-03-04 11:28:31 +0100478/*
479 * Generate a machine wide unique identifier for this inode.
480 *
481 * This relies on u64 not wrapping in the life-time of the machine; which with
482 * 1ns resolution means almost 585 years.
483 *
484 * This further relies on the fact that a well formed program will not unmap
485 * the file while it has a (shared) futex waiting on it. This mapping will have
486 * a file reference which pins the mount and inode.
487 *
488 * If for some reason an inode gets evicted and read back in again, it will get
489 * a new sequence number and will _NOT_ match, even though it is the exact same
490 * file.
491 *
492 * It is important that match_futex() will never have a false-positive, esp.
493 * for PI futexes that can mess up the state. The above argues that false-negatives
494 * are only possible for malformed programs.
495 */
496static u64 get_inode_sequence_number(struct inode *inode)
497{
498 static atomic64_t i_seq;
499 u64 old;
500
501 /* Does the inode already have a sequence number? */
502 old = atomic64_read(&inode->i_sequence);
503 if (likely(old))
504 return old;
505
506 for (;;) {
507 u64 new = atomic64_add_return(1, &i_seq);
508 if (WARN_ON_ONCE(!new))
509 continue;
510
511 old = atomic64_cmpxchg_relaxed(&inode->i_sequence, 0, new);
512 if (old)
513 return old;
514 return new;
515 }
516}
517
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700518/**
Darren Hartd96ee562009-09-21 22:30:22 -0700519 * get_futex_key() - Get parameters which are the keys for a futex
520 * @uaddr: virtual address of the futex
521 * @fshared: 0 for a PROCESS_PRIVATE futex, 1 for PROCESS_SHARED
522 * @key: address where result is stored.
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500523 * @rw: mapping needs to be read/write (values: VERIFY_READ,
524 * VERIFY_WRITE)
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700525 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -0800526 * Return: a negative error code or 0
527 *
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700528 * The key words are stored in *key on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 *
Peter Zijlstrafb099f3b2020-03-04 11:28:31 +0100530 * For shared mappings (when @fshared), the key is:
531 * ( inode->i_sequence, page->index, offset_within_page )
532 * [ also see get_inode_sequence_number() ]
533 *
534 * For private mappings (or when !@fshared), the key is:
535 * ( current->mm, address, 0 )
536 *
537 * This allows (cross process, where applicable) identification of the futex
538 * without keeping the page pinned for the duration of the FUTEX_WAIT.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 *
Darren Hartb2d09942009-03-12 00:55:37 -0700540 * lock_page() might sleep, the caller should not hold a spinlock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 */
Thomas Gleixner64d13042009-05-18 21:20:10 +0200542static int
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500543get_futex_key(u32 __user *uaddr, int fshared, union futex_key *key, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Ingo Molnare2970f22006-06-27 02:54:47 -0700545 unsigned long address = (unsigned long)uaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 struct mm_struct *mm = current->mm;
Mel Gorman077fa7a2016-06-08 14:25:22 +0100547 struct page *page, *tail;
Kirill A. Shutemov14d27ab2016-01-15 16:53:00 -0800548 struct address_space *mapping;
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500549 int err, ro = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 /*
552 * The futex address must be "naturally" aligned.
553 */
Ingo Molnare2970f22006-06-27 02:54:47 -0700554 key->both.offset = address % PAGE_SIZE;
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700555 if (unlikely((address % sizeof(u32)) != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return -EINVAL;
Ingo Molnare2970f22006-06-27 02:54:47 -0700557 address -= key->both.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Linus Torvalds5cdec2d2013-12-12 09:53:51 -0800559 if (unlikely(!access_ok(rw, uaddr, sizeof(u32))))
560 return -EFAULT;
561
Davidlohr Buesoab51fba2015-06-29 23:26:02 -0700562 if (unlikely(should_fail_futex(fshared)))
563 return -EFAULT;
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 /*
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700566 * PROCESS_PRIVATE futexes are fast.
567 * As the mm cannot disappear under us and the 'key' only needs
568 * virtual address, we dont even have to find the underlying vma.
569 * Note : We do have to check 'uaddr' is a valid user address,
570 * but access_ok() should be faster than find_vma()
571 */
572 if (!fshared) {
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700573 key->private.mm = mm;
574 key->private.address = address;
Davidlohr Bueso8ad7b372016-02-09 11:15:13 -0800575 get_futex_key_refs(key); /* implies smp_mb(); (B) */
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700576 return 0;
577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200579again:
Davidlohr Buesoab51fba2015-06-29 23:26:02 -0700580 /* Ignore any VERIFY_READ mapping (futex common case) */
581 if (unlikely(should_fail_futex(fshared)))
582 return -EFAULT;
583
KOSAKI Motohiro7485d0d2010-01-05 16:32:43 +0900584 err = get_user_pages_fast(address, 1, 1, &page);
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500585 /*
586 * If write access is not required (eg. FUTEX_WAIT), try
587 * and get read-only access.
588 */
589 if (err == -EFAULT && rw == VERIFY_READ) {
590 err = get_user_pages_fast(address, 1, 0, &page);
591 ro = 1;
592 }
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200593 if (err < 0)
594 return err;
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500595 else
596 err = 0;
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200597
Mel Gorman65d8fc72016-02-09 11:15:14 -0800598 /*
599 * The treatment of mapping from this point on is critical. The page
600 * lock protects many things but in this context the page lock
601 * stabilizes mapping, prevents inode freeing in the shared
602 * file-backed region case and guards against movement to swap cache.
603 *
604 * Strictly speaking the page lock is not needed in all cases being
605 * considered here and page lock forces unnecessarily serialization
606 * From this point on, mapping will be re-verified if necessary and
607 * page lock will be acquired only if it is unavoidable
Mel Gorman077fa7a2016-06-08 14:25:22 +0100608 *
609 * Mapping checks require the head page for any compound page so the
610 * head page and mapping is looked up now. For anonymous pages, it
611 * does not matter if the page splits in the future as the key is
612 * based on the address. For filesystem-backed pages, the tail is
613 * required as the index of the page determines the key. For
614 * base pages, there is no tail page and tail == page.
Mel Gorman65d8fc72016-02-09 11:15:14 -0800615 */
Mel Gorman077fa7a2016-06-08 14:25:22 +0100616 tail = page;
Mel Gorman65d8fc72016-02-09 11:15:14 -0800617 page = compound_head(page);
618 mapping = READ_ONCE(page->mapping);
619
Hugh Dickinse6780f72011-12-31 11:44:01 -0800620 /*
Kirill A. Shutemov14d27ab2016-01-15 16:53:00 -0800621 * If page->mapping is NULL, then it cannot be a PageAnon
Hugh Dickinse6780f72011-12-31 11:44:01 -0800622 * page; but it might be the ZERO_PAGE or in the gate area or
623 * in a special mapping (all cases which we are happy to fail);
624 * or it may have been a good file page when get_user_pages_fast
625 * found it, but truncated or holepunched or subjected to
626 * invalidate_complete_page2 before we got the page lock (also
627 * cases which we are happy to fail). And we hold a reference,
628 * so refcount care in invalidate_complete_page's remove_mapping
629 * prevents drop_caches from setting mapping to NULL beneath us.
630 *
631 * The case we do have to guard against is when memory pressure made
632 * shmem_writepage move it from filecache to swapcache beneath us:
Kirill A. Shutemov14d27ab2016-01-15 16:53:00 -0800633 * an unlikely race, but we do need to retry for page->mapping.
Hugh Dickinse6780f72011-12-31 11:44:01 -0800634 */
Mel Gorman65d8fc72016-02-09 11:15:14 -0800635 if (unlikely(!mapping)) {
636 int shmem_swizzled;
637
638 /*
639 * Page lock is required to identify which special case above
640 * applies. If this is really a shmem page then the page lock
641 * will prevent unexpected transitions.
642 */
643 lock_page(page);
644 shmem_swizzled = PageSwapCache(page) || page->mapping;
Kirill A. Shutemov14d27ab2016-01-15 16:53:00 -0800645 unlock_page(page);
646 put_page(page);
Mel Gorman65d8fc72016-02-09 11:15:14 -0800647
Hugh Dickinse6780f72011-12-31 11:44:01 -0800648 if (shmem_swizzled)
649 goto again;
Mel Gorman65d8fc72016-02-09 11:15:14 -0800650
Hugh Dickinse6780f72011-12-31 11:44:01 -0800651 return -EFAULT;
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 /*
655 * Private mappings are handled in a simple way.
656 *
Mel Gorman65d8fc72016-02-09 11:15:14 -0800657 * If the futex key is stored on an anonymous page, then the associated
658 * object is the mm which is implicitly pinned by the calling process.
659 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 * NOTE: When userspace waits on a MAP_SHARED mapping, even if
661 * it's a read-only handle, it's expected that futexes attach to
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200662 * the object not the particular process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 */
Kirill A. Shutemov14d27ab2016-01-15 16:53:00 -0800664 if (PageAnon(page)) {
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500665 /*
666 * A RO anonymous page will never change and thus doesn't make
667 * sense for futex operations.
668 */
Davidlohr Buesoab51fba2015-06-29 23:26:02 -0700669 if (unlikely(should_fail_futex(fshared)) || ro) {
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500670 err = -EFAULT;
671 goto out;
672 }
673
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200674 key->both.offset |= FUT_OFF_MMSHARED; /* ref taken on mm */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 key->private.mm = mm;
Ingo Molnare2970f22006-06-27 02:54:47 -0700676 key->private.address = address;
Mel Gorman65d8fc72016-02-09 11:15:14 -0800677
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200678 } else {
Mel Gorman65d8fc72016-02-09 11:15:14 -0800679 struct inode *inode;
680
681 /*
682 * The associated futex object in this case is the inode and
683 * the page->mapping must be traversed. Ordinarily this should
684 * be stabilised under page lock but it's not strictly
685 * necessary in this case as we just want to pin the inode, not
686 * update the radix tree or anything like that.
687 *
688 * The RCU read lock is taken as the inode is finally freed
689 * under RCU. If the mapping still matches expectations then the
690 * mapping->host can be safely accessed as being a valid inode.
691 */
692 rcu_read_lock();
693
694 if (READ_ONCE(page->mapping) != mapping) {
695 rcu_read_unlock();
696 put_page(page);
697
698 goto again;
699 }
700
701 inode = READ_ONCE(mapping->host);
702 if (!inode) {
703 rcu_read_unlock();
704 put_page(page);
705
706 goto again;
707 }
708
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200709 key->both.offset |= FUT_OFF_INODE; /* inode-based key */
Peter Zijlstrafb099f3b2020-03-04 11:28:31 +0100710 key->shared.i_seq = get_inode_sequence_number(inode);
Mel Gorman077fa7a2016-06-08 14:25:22 +0100711 key->shared.pgoff = basepage_index(tail);
Mel Gorman65d8fc72016-02-09 11:15:14 -0800712 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
714
Peter Zijlstrafb099f3b2020-03-04 11:28:31 +0100715 get_futex_key_refs(key); /* implies smp_mb(); (B) */
716
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500717out:
Kirill A. Shutemov14d27ab2016-01-15 16:53:00 -0800718 put_page(page);
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500719 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
Thomas Gleixnerae791a22010-11-10 13:30:36 +0100722static inline void put_futex_key(union futex_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200724 drop_futex_key_refs(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
Darren Hartd96ee562009-09-21 22:30:22 -0700727/**
728 * fault_in_user_writeable() - Fault in user address and verify RW access
Thomas Gleixnerd0725992009-06-11 23:15:43 +0200729 * @uaddr: pointer to faulting user space address
730 *
731 * Slow path to fixup the fault we just took in the atomic write
732 * access to @uaddr.
733 *
Randy Dunlapfb62db22010-10-13 11:02:34 -0700734 * We have no generic implementation of a non-destructive write to the
Thomas Gleixnerd0725992009-06-11 23:15:43 +0200735 * user address. We know that we faulted in the atomic pagefault
736 * disabled section so we can as well avoid the #PF overhead by
737 * calling get_user_pages() right away.
738 */
739static int fault_in_user_writeable(u32 __user *uaddr)
740{
Andi Kleen722d0172009-12-08 13:19:42 +0100741 struct mm_struct *mm = current->mm;
742 int ret;
743
744 down_read(&mm->mmap_sem);
Benjamin Herrenschmidt2efaca92011-07-25 17:12:32 -0700745 ret = fixup_user_fault(current, mm, (unsigned long)uaddr,
Dominik Dingel4a9e1cd2016-01-15 16:57:04 -0800746 FAULT_FLAG_WRITE, NULL);
Andi Kleen722d0172009-12-08 13:19:42 +0100747 up_read(&mm->mmap_sem);
748
Thomas Gleixnerd0725992009-06-11 23:15:43 +0200749 return ret < 0 ? ret : 0;
750}
751
Darren Hart4b1c4862009-04-03 13:39:42 -0700752/**
753 * futex_top_waiter() - Return the highest priority waiter on a futex
Darren Hartd96ee562009-09-21 22:30:22 -0700754 * @hb: the hash bucket the futex_q's reside in
755 * @key: the futex key (to distinguish it from other futex futex_q's)
Darren Hart4b1c4862009-04-03 13:39:42 -0700756 *
757 * Must be called with the hb lock held.
758 */
759static struct futex_q *futex_top_waiter(struct futex_hash_bucket *hb,
760 union futex_key *key)
761{
762 struct futex_q *this;
763
764 plist_for_each_entry(this, &hb->chain, list) {
765 if (match_futex(&this->key, key))
766 return this;
767 }
768 return NULL;
769}
770
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800771static int cmpxchg_futex_value_locked(u32 *curval, u32 __user *uaddr,
772 u32 uval, u32 newval)
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700773{
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800774 int ret;
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700775
776 pagefault_disable();
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800777 ret = futex_atomic_cmpxchg_inatomic(curval, uaddr, uval, newval);
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700778 pagefault_enable();
779
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800780 return ret;
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700781}
782
783static int get_futex_value_locked(u32 *dest, u32 __user *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
785 int ret;
786
Peter Zijlstraa8663742006-12-06 20:32:20 -0800787 pagefault_disable();
Linus Torvaldsbd28b142016-05-22 17:21:27 -0700788 ret = __get_user(*dest, from);
Peter Zijlstraa8663742006-12-06 20:32:20 -0800789 pagefault_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 return ret ? -EFAULT : 0;
792}
793
Ingo Molnarc87e2832006-06-27 02:54:58 -0700794
795/*
796 * PI code:
797 */
798static int refill_pi_state_cache(void)
799{
800 struct futex_pi_state *pi_state;
801
802 if (likely(current->pi_state_cache))
803 return 0;
804
Burman Yan4668edc2006-12-06 20:38:51 -0800805 pi_state = kzalloc(sizeof(*pi_state), GFP_KERNEL);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700806
807 if (!pi_state)
808 return -ENOMEM;
809
Ingo Molnarc87e2832006-06-27 02:54:58 -0700810 INIT_LIST_HEAD(&pi_state->list);
811 /* pi_mutex gets initialized later */
812 pi_state->owner = NULL;
813 atomic_set(&pi_state->refcount, 1);
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200814 pi_state->key = FUTEX_KEY_INIT;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700815
816 current->pi_state_cache = pi_state;
817
818 return 0;
819}
820
821static struct futex_pi_state * alloc_pi_state(void)
822{
823 struct futex_pi_state *pi_state = current->pi_state_cache;
824
825 WARN_ON(!pi_state);
826 current->pi_state_cache = NULL;
827
828 return pi_state;
829}
830
Brian Silverman30a6b802014-10-25 20:20:37 -0400831/*
Thomas Gleixner29e9ee52015-12-19 20:07:39 +0000832 * Drops a reference to the pi_state object and frees or caches it
833 * when the last reference is gone.
834 *
Brian Silverman30a6b802014-10-25 20:20:37 -0400835 * Must be called with the hb lock held.
836 */
Thomas Gleixner29e9ee52015-12-19 20:07:39 +0000837static void put_pi_state(struct futex_pi_state *pi_state)
Ingo Molnarc87e2832006-06-27 02:54:58 -0700838{
Brian Silverman30a6b802014-10-25 20:20:37 -0400839 if (!pi_state)
840 return;
841
Ingo Molnarc87e2832006-06-27 02:54:58 -0700842 if (!atomic_dec_and_test(&pi_state->refcount))
843 return;
844
845 /*
846 * If pi_state->owner is NULL, the owner is most probably dying
847 * and has cleaned up the pi_state already
848 */
849 if (pi_state->owner) {
Thomas Gleixner1d615482009-11-17 14:54:03 +0100850 raw_spin_lock_irq(&pi_state->owner->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700851 list_del_init(&pi_state->list);
Thomas Gleixner1d615482009-11-17 14:54:03 +0100852 raw_spin_unlock_irq(&pi_state->owner->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700853
854 rt_mutex_proxy_unlock(&pi_state->pi_mutex, pi_state->owner);
855 }
856
857 if (current->pi_state_cache)
858 kfree(pi_state);
859 else {
860 /*
861 * pi_state->list is already empty.
862 * clear pi_state->owner.
863 * refcount is at 0 - put it back to 1.
864 */
865 pi_state->owner = NULL;
866 atomic_set(&pi_state->refcount, 1);
867 current->pi_state_cache = pi_state;
868 }
869}
870
871/*
872 * Look up the task based on what TID userspace gave us.
873 * We dont trust it.
874 */
875static struct task_struct * futex_find_get_task(pid_t pid)
876{
877 struct task_struct *p;
878
Oleg Nesterovd359b542006-09-29 02:00:55 -0700879 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700880 p = find_task_by_vpid(pid);
Michal Hocko7a0ea092010-06-30 09:51:19 +0200881 if (p)
882 get_task_struct(p);
Thomas Gleixnera06381f2007-06-23 11:48:40 +0200883
Oleg Nesterovd359b542006-09-29 02:00:55 -0700884 rcu_read_unlock();
Ingo Molnarc87e2832006-06-27 02:54:58 -0700885
886 return p;
887}
888
889/*
890 * This task is holding PI mutexes at exit time => bad.
891 * Kernel cleans up PI-state, but userspace is likely hosed.
892 * (Robust-futex cleanup is separate and might save the day for userspace.)
893 */
894void exit_pi_state_list(struct task_struct *curr)
895{
Ingo Molnarc87e2832006-06-27 02:54:58 -0700896 struct list_head *next, *head = &curr->pi_state_list;
897 struct futex_pi_state *pi_state;
Ingo Molnar627371d2006-07-29 05:16:20 +0200898 struct futex_hash_bucket *hb;
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200899 union futex_key key = FUTEX_KEY_INIT;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700900
Thomas Gleixnera0c1e902008-02-23 15:23:57 -0800901 if (!futex_cmpxchg_enabled)
902 return;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700903 /*
904 * We are a ZOMBIE and nobody can enqueue itself on
905 * pi_state_list anymore, but we have to be careful
Ingo Molnar627371d2006-07-29 05:16:20 +0200906 * versus waiters unqueueing themselves:
Ingo Molnarc87e2832006-06-27 02:54:58 -0700907 */
Thomas Gleixner1d615482009-11-17 14:54:03 +0100908 raw_spin_lock_irq(&curr->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700909 while (!list_empty(head)) {
910
911 next = head->next;
912 pi_state = list_entry(next, struct futex_pi_state, list);
913 key = pi_state->key;
Ingo Molnar627371d2006-07-29 05:16:20 +0200914 hb = hash_futex(&key);
Thomas Gleixner1d615482009-11-17 14:54:03 +0100915 raw_spin_unlock_irq(&curr->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700916
Ingo Molnarc87e2832006-06-27 02:54:58 -0700917 spin_lock(&hb->lock);
918
Thomas Gleixner1d615482009-11-17 14:54:03 +0100919 raw_spin_lock_irq(&curr->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +0200920 /*
921 * We dropped the pi-lock, so re-check whether this
922 * task still owns the PI-state:
923 */
Ingo Molnarc87e2832006-06-27 02:54:58 -0700924 if (head->next != next) {
925 spin_unlock(&hb->lock);
926 continue;
927 }
928
Ingo Molnarc87e2832006-06-27 02:54:58 -0700929 WARN_ON(pi_state->owner != curr);
Ingo Molnar627371d2006-07-29 05:16:20 +0200930 WARN_ON(list_empty(&pi_state->list));
931 list_del_init(&pi_state->list);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700932 pi_state->owner = NULL;
Thomas Gleixner1d615482009-11-17 14:54:03 +0100933 raw_spin_unlock_irq(&curr->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700934
935 rt_mutex_unlock(&pi_state->pi_mutex);
936
937 spin_unlock(&hb->lock);
938
Thomas Gleixner1d615482009-11-17 14:54:03 +0100939 raw_spin_lock_irq(&curr->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700940 }
Thomas Gleixner1d615482009-11-17 14:54:03 +0100941 raw_spin_unlock_irq(&curr->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700942}
943
Thomas Gleixner54a21782014-06-03 12:27:08 +0000944/*
945 * We need to check the following states:
946 *
947 * Waiter | pi_state | pi->owner | uTID | uODIED | ?
948 *
949 * [1] NULL | --- | --- | 0 | 0/1 | Valid
950 * [2] NULL | --- | --- | >0 | 0/1 | Valid
951 *
952 * [3] Found | NULL | -- | Any | 0/1 | Invalid
953 *
954 * [4] Found | Found | NULL | 0 | 1 | Valid
955 * [5] Found | Found | NULL | >0 | 1 | Invalid
956 *
957 * [6] Found | Found | task | 0 | 1 | Valid
958 *
959 * [7] Found | Found | NULL | Any | 0 | Invalid
960 *
961 * [8] Found | Found | task | ==taskTID | 0/1 | Valid
962 * [9] Found | Found | task | 0 | 0 | Invalid
963 * [10] Found | Found | task | !=taskTID | 0/1 | Invalid
964 *
965 * [1] Indicates that the kernel can acquire the futex atomically. We
966 * came came here due to a stale FUTEX_WAITERS/FUTEX_OWNER_DIED bit.
967 *
968 * [2] Valid, if TID does not belong to a kernel thread. If no matching
969 * thread is found then it indicates that the owner TID has died.
970 *
971 * [3] Invalid. The waiter is queued on a non PI futex
972 *
973 * [4] Valid state after exit_robust_list(), which sets the user space
974 * value to FUTEX_WAITERS | FUTEX_OWNER_DIED.
975 *
976 * [5] The user space value got manipulated between exit_robust_list()
977 * and exit_pi_state_list()
978 *
979 * [6] Valid state after exit_pi_state_list() which sets the new owner in
980 * the pi_state but cannot access the user space value.
981 *
982 * [7] pi_state->owner can only be NULL when the OWNER_DIED bit is set.
983 *
984 * [8] Owner and user space value match
985 *
986 * [9] There is no transient state which sets the user space TID to 0
987 * except exit_robust_list(), but this is indicated by the
988 * FUTEX_OWNER_DIED bit. See [4]
989 *
990 * [10] There is no transient state which leaves owner and user space
991 * TID out of sync.
992 */
Thomas Gleixnere60cbc52014-06-11 20:45:39 +0000993
994/*
995 * Validate that the existing waiter has a pi_state and sanity check
996 * the pi_state against the user space value. If correct, attach to
997 * it.
998 */
999static int attach_to_pi_state(u32 uval, struct futex_pi_state *pi_state,
1000 struct futex_pi_state **ps)
1001{
1002 pid_t pid = uval & FUTEX_TID_MASK;
1003
1004 /*
1005 * Userspace might have messed up non-PI and PI futexes [3]
1006 */
1007 if (unlikely(!pi_state))
1008 return -EINVAL;
1009
1010 WARN_ON(!atomic_read(&pi_state->refcount));
1011
1012 /*
1013 * Handle the owner died case:
1014 */
1015 if (uval & FUTEX_OWNER_DIED) {
1016 /*
1017 * exit_pi_state_list sets owner to NULL and wakes the
1018 * topmost waiter. The task which acquires the
1019 * pi_state->rt_mutex will fixup owner.
1020 */
1021 if (!pi_state->owner) {
1022 /*
1023 * No pi state owner, but the user space TID
1024 * is not 0. Inconsistent state. [5]
1025 */
1026 if (pid)
1027 return -EINVAL;
1028 /*
1029 * Take a ref on the state and return success. [4]
1030 */
1031 goto out_state;
1032 }
1033
1034 /*
1035 * If TID is 0, then either the dying owner has not
1036 * yet executed exit_pi_state_list() or some waiter
1037 * acquired the rtmutex in the pi state, but did not
1038 * yet fixup the TID in user space.
1039 *
1040 * Take a ref on the state and return success. [6]
1041 */
1042 if (!pid)
1043 goto out_state;
1044 } else {
1045 /*
1046 * If the owner died bit is not set, then the pi_state
1047 * must have an owner. [7]
1048 */
1049 if (!pi_state->owner)
1050 return -EINVAL;
1051 }
1052
1053 /*
1054 * Bail out if user space manipulated the futex value. If pi
1055 * state exists then the owner TID must be the same as the
1056 * user space TID. [9/10]
1057 */
1058 if (pid != task_pid_vnr(pi_state->owner))
1059 return -EINVAL;
1060out_state:
1061 atomic_inc(&pi_state->refcount);
1062 *ps = pi_state;
1063 return 0;
1064}
1065
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001066/*
1067 * Lookup the task for the TID provided from user space and attach to
1068 * it after doing proper sanity checks.
1069 */
1070static int attach_to_pi_owner(u32 uval, union futex_key *key,
1071 struct futex_pi_state **ps)
Ingo Molnarc87e2832006-06-27 02:54:58 -07001072{
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001073 pid_t pid = uval & FUTEX_TID_MASK;
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001074 struct futex_pi_state *pi_state;
1075 struct task_struct *p;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001076
1077 /*
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001078 * We are the first waiter - try to look up the real owner and attach
Thomas Gleixner54a21782014-06-03 12:27:08 +00001079 * the new pi_state to it, but bail out when TID = 0 [1]
Ingo Molnarc87e2832006-06-27 02:54:58 -07001080 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001081 if (!pid)
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001082 return -ESRCH;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001083 p = futex_find_get_task(pid);
Michal Hocko7a0ea092010-06-30 09:51:19 +02001084 if (!p)
1085 return -ESRCH;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001086
Oleg Nesterova2129462015-02-02 15:05:36 +01001087 if (unlikely(p->flags & PF_KTHREAD)) {
Thomas Gleixnerf0d71b32014-05-12 20:45:35 +00001088 put_task_struct(p);
1089 return -EPERM;
1090 }
1091
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001092 /*
1093 * We need to look at the task state flags to figure out,
1094 * whether the task is exiting. To protect against the do_exit
1095 * change of the task flags, we do this protected by
1096 * p->pi_lock:
1097 */
Thomas Gleixner1d615482009-11-17 14:54:03 +01001098 raw_spin_lock_irq(&p->pi_lock);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001099 if (unlikely(p->flags & PF_EXITING)) {
1100 /*
1101 * The task is on the way out. When PF_EXITPIDONE is
1102 * set, we know that the task has finished the
1103 * cleanup:
1104 */
1105 int ret = (p->flags & PF_EXITPIDONE) ? -ESRCH : -EAGAIN;
1106
Thomas Gleixner1d615482009-11-17 14:54:03 +01001107 raw_spin_unlock_irq(&p->pi_lock);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001108 put_task_struct(p);
1109 return ret;
1110 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001111
Thomas Gleixner54a21782014-06-03 12:27:08 +00001112 /*
1113 * No existing pi state. First waiter. [2]
1114 */
Ingo Molnarc87e2832006-06-27 02:54:58 -07001115 pi_state = alloc_pi_state();
1116
1117 /*
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001118 * Initialize the pi_mutex in locked state and make @p
Ingo Molnarc87e2832006-06-27 02:54:58 -07001119 * the owner of it:
1120 */
1121 rt_mutex_init_proxy_locked(&pi_state->pi_mutex, p);
1122
1123 /* Store the key for possible exit cleanups: */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001124 pi_state->key = *key;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001125
Ingo Molnar627371d2006-07-29 05:16:20 +02001126 WARN_ON(!list_empty(&pi_state->list));
Ingo Molnarc87e2832006-06-27 02:54:58 -07001127 list_add(&pi_state->list, &p->pi_state_list);
1128 pi_state->owner = p;
Thomas Gleixner1d615482009-11-17 14:54:03 +01001129 raw_spin_unlock_irq(&p->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001130
1131 put_task_struct(p);
1132
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001133 *ps = pi_state;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001134
1135 return 0;
1136}
1137
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001138static int lookup_pi_state(u32 uval, struct futex_hash_bucket *hb,
1139 union futex_key *key, struct futex_pi_state **ps)
1140{
1141 struct futex_q *match = futex_top_waiter(hb, key);
1142
1143 /*
1144 * If there is a waiter on that futex, validate it and
1145 * attach to the pi_state when the validation succeeds.
1146 */
1147 if (match)
1148 return attach_to_pi_state(uval, match->pi_state, ps);
1149
1150 /*
1151 * We are the first waiter - try to look up the owner based on
1152 * @uval and attach to it.
1153 */
1154 return attach_to_pi_owner(uval, key, ps);
1155}
1156
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001157static int lock_pi_update_atomic(u32 __user *uaddr, u32 uval, u32 newval)
1158{
1159 u32 uninitialized_var(curval);
1160
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001161 if (unlikely(should_fail_futex(true)))
1162 return -EFAULT;
1163
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001164 if (unlikely(cmpxchg_futex_value_locked(&curval, uaddr, uval, newval)))
1165 return -EFAULT;
1166
1167 /*If user space value changed, let the caller retry */
1168 return curval != uval ? -EAGAIN : 0;
1169}
1170
Darren Hart1a520842009-04-03 13:39:52 -07001171/**
Darren Hartd96ee562009-09-21 22:30:22 -07001172 * futex_lock_pi_atomic() - Atomic work required to acquire a pi aware futex
Darren Hartbab5bc92009-04-07 23:23:50 -07001173 * @uaddr: the pi futex user address
1174 * @hb: the pi futex hash bucket
1175 * @key: the futex key associated with uaddr and hb
1176 * @ps: the pi_state pointer where we store the result of the
1177 * lookup
1178 * @task: the task to perform the atomic lock work for. This will
1179 * be "current" except in the case of requeue pi.
1180 * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0)
Darren Hart1a520842009-04-03 13:39:52 -07001181 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08001182 * Return:
1183 * 0 - ready to wait;
1184 * 1 - acquired the lock;
Darren Hart1a520842009-04-03 13:39:52 -07001185 * <0 - error
1186 *
1187 * The hb->lock and futex_key refs shall be held by the caller.
1188 */
1189static int futex_lock_pi_atomic(u32 __user *uaddr, struct futex_hash_bucket *hb,
1190 union futex_key *key,
1191 struct futex_pi_state **ps,
Darren Hartbab5bc92009-04-07 23:23:50 -07001192 struct task_struct *task, int set_waiters)
Darren Hart1a520842009-04-03 13:39:52 -07001193{
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001194 u32 uval, newval, vpid = task_pid_vnr(task);
1195 struct futex_q *match;
1196 int ret;
Darren Hart1a520842009-04-03 13:39:52 -07001197
1198 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001199 * Read the user space value first so we can validate a few
1200 * things before proceeding further.
Darren Hart1a520842009-04-03 13:39:52 -07001201 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001202 if (get_futex_value_locked(&uval, uaddr))
Darren Hart1a520842009-04-03 13:39:52 -07001203 return -EFAULT;
1204
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001205 if (unlikely(should_fail_futex(true)))
1206 return -EFAULT;
1207
Darren Hart1a520842009-04-03 13:39:52 -07001208 /*
1209 * Detect deadlocks.
1210 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001211 if ((unlikely((uval & FUTEX_TID_MASK) == vpid)))
Darren Hart1a520842009-04-03 13:39:52 -07001212 return -EDEADLK;
1213
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001214 if ((unlikely(should_fail_futex(true))))
1215 return -EDEADLK;
1216
Darren Hart1a520842009-04-03 13:39:52 -07001217 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001218 * Lookup existing state first. If it exists, try to attach to
1219 * its pi_state.
Darren Hart1a520842009-04-03 13:39:52 -07001220 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001221 match = futex_top_waiter(hb, key);
1222 if (match)
1223 return attach_to_pi_state(uval, match->pi_state, ps);
1224
1225 /*
1226 * No waiter and user TID is 0. We are here because the
1227 * waiters or the owner died bit is set or called from
1228 * requeue_cmp_pi or for whatever reason something took the
1229 * syscall.
1230 */
1231 if (!(uval & FUTEX_TID_MASK)) {
Thomas Gleixnerb3eaa9f2014-06-03 12:27:06 +00001232 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001233 * We take over the futex. No other waiters and the user space
1234 * TID is 0. We preserve the owner died bit.
Thomas Gleixnerb3eaa9f2014-06-03 12:27:06 +00001235 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001236 newval = uval & FUTEX_OWNER_DIED;
1237 newval |= vpid;
1238
1239 /* The futex requeue_pi code can enforce the waiters bit */
1240 if (set_waiters)
1241 newval |= FUTEX_WAITERS;
1242
1243 ret = lock_pi_update_atomic(uaddr, uval, newval);
1244 /* If the take over worked, return 1 */
1245 return ret < 0 ? ret : 1;
Thomas Gleixnerb3eaa9f2014-06-03 12:27:06 +00001246 }
Darren Hart1a520842009-04-03 13:39:52 -07001247
Darren Hart1a520842009-04-03 13:39:52 -07001248 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001249 * First waiter. Set the waiters bit before attaching ourself to
1250 * the owner. If owner tries to unlock, it will be forced into
1251 * the kernel and blocked on hb->lock.
Darren Hart1a520842009-04-03 13:39:52 -07001252 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001253 newval = uval | FUTEX_WAITERS;
1254 ret = lock_pi_update_atomic(uaddr, uval, newval);
1255 if (ret)
1256 return ret;
Darren Hart1a520842009-04-03 13:39:52 -07001257 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001258 * If the update of the user space value succeeded, we try to
1259 * attach to the owner. If that fails, no harm done, we only
1260 * set the FUTEX_WAITERS bit in the user space variable.
Darren Hart1a520842009-04-03 13:39:52 -07001261 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001262 return attach_to_pi_owner(uval, key, ps);
Darren Hart1a520842009-04-03 13:39:52 -07001263}
1264
Lai Jiangshan2e129782010-12-22 14:18:50 +08001265/**
1266 * __unqueue_futex() - Remove the futex_q from its futex_hash_bucket
1267 * @q: The futex_q to unqueue
1268 *
1269 * The q->lock_ptr must not be NULL and must be held by the caller.
1270 */
1271static void __unqueue_futex(struct futex_q *q)
1272{
1273 struct futex_hash_bucket *hb;
1274
Steven Rostedt29096202011-03-17 15:21:07 -04001275 if (WARN_ON_SMP(!q->lock_ptr || !spin_is_locked(q->lock_ptr))
1276 || WARN_ON(plist_node_empty(&q->list)))
Lai Jiangshan2e129782010-12-22 14:18:50 +08001277 return;
1278
1279 hb = container_of(q->lock_ptr, struct futex_hash_bucket, lock);
1280 plist_del(&q->list, &hb->chain);
Linus Torvalds11d46162014-03-20 22:11:17 -07001281 hb_waiters_dec(hb);
Lai Jiangshan2e129782010-12-22 14:18:50 +08001282}
1283
Ingo Molnarc87e2832006-06-27 02:54:58 -07001284/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 * The hash bucket lock must be held when this is called.
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001286 * Afterwards, the futex_q must not be accessed. Callers
1287 * must ensure to later call wake_up_q() for the actual
1288 * wakeups to occur.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 */
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001290static void mark_wake_futex(struct wake_q_head *wake_q, struct futex_q *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001292 struct task_struct *p = q->task;
1293
Darren Hartaa109902012-11-26 16:29:56 -08001294 if (WARN(q->pi_state || q->rt_waiter, "refusing to wake PI futex\n"))
1295 return;
1296
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001297 /*
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001298 * Queue the task for later wakeup for after we've released
1299 * the hb->lock. wake_q_add() grabs reference to p.
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001300 */
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001301 wake_q_add(wake_q, p);
Lai Jiangshan2e129782010-12-22 14:18:50 +08001302 __unqueue_futex(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 /*
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001304 * The waiting task can free the futex_q as soon as
1305 * q->lock_ptr = NULL is written, without taking any locks. A
1306 * memory barrier is required here to prevent the following
1307 * store to lock_ptr from getting ahead of the plist_del.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 */
Ralf Baechleccdea2f2006-12-06 20:40:26 -08001309 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 q->lock_ptr = NULL;
1311}
1312
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001313static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this,
1314 struct futex_hash_bucket *hb)
Ingo Molnarc87e2832006-06-27 02:54:58 -07001315{
1316 struct task_struct *new_owner;
1317 struct futex_pi_state *pi_state = this->pi_state;
Vitaliy Ivanov7cfdaf32011-07-07 15:10:31 +03001318 u32 uninitialized_var(curval), newval;
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001319 WAKE_Q(wake_q);
1320 bool deboost;
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001321 int ret = 0;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001322
1323 if (!pi_state)
1324 return -EINVAL;
1325
Thomas Gleixner51246bf2010-02-02 11:40:27 +01001326 /*
1327 * If current does not own the pi_state then the futex is
1328 * inconsistent and user space fiddled with the futex value.
1329 */
1330 if (pi_state->owner != current)
1331 return -EINVAL;
1332
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001333 raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001334 new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);
1335
1336 /*
Steven Rostedtf123c982011-01-06 15:08:29 -05001337 * It is possible that the next waiter (the one that brought
1338 * this owner to the kernel) timed out and is no longer
1339 * waiting on the lock.
Ingo Molnarc87e2832006-06-27 02:54:58 -07001340 */
1341 if (!new_owner)
1342 new_owner = this->task;
1343
1344 /*
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001345 * We pass it to the next owner. The WAITERS bit is always
1346 * kept enabled while there is PI state around. We cleanup the
1347 * owner died bit, because we are the owner.
Ingo Molnarc87e2832006-06-27 02:54:58 -07001348 */
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001349 newval = FUTEX_WAITERS | task_pid_vnr(new_owner);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001350
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001351 if (unlikely(should_fail_futex(true)))
1352 ret = -EFAULT;
1353
Sebastian Andrzej Siewior89e9e662016-04-15 14:35:39 +02001354 if (cmpxchg_futex_value_locked(&curval, uaddr, uval, newval)) {
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001355 ret = -EFAULT;
Sebastian Andrzej Siewior89e9e662016-04-15 14:35:39 +02001356 } else if (curval != uval) {
1357 /*
1358 * If a unconditional UNLOCK_PI operation (user space did not
1359 * try the TID->0 transition) raced with a waiter setting the
1360 * FUTEX_WAITERS flag between get_user() and locking the hash
1361 * bucket lock, retry the operation.
1362 */
1363 if ((FUTEX_TID_MASK & curval) == uval)
1364 ret = -EAGAIN;
1365 else
1366 ret = -EINVAL;
1367 }
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001368 if (ret) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001369 raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001370 return ret;
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001371 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001372
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001373 raw_spin_lock(&pi_state->owner->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +02001374 WARN_ON(list_empty(&pi_state->list));
1375 list_del_init(&pi_state->list);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001376 raw_spin_unlock(&pi_state->owner->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +02001377
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001378 raw_spin_lock(&new_owner->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +02001379 WARN_ON(!list_empty(&pi_state->list));
Ingo Molnarc87e2832006-06-27 02:54:58 -07001380 list_add(&pi_state->list, &new_owner->pi_state_list);
1381 pi_state->owner = new_owner;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001382 raw_spin_unlock(&new_owner->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +02001383
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001384 raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001385
1386 deboost = rt_mutex_futex_unlock(&pi_state->pi_mutex, &wake_q);
1387
1388 /*
1389 * First unlock HB so the waiter does not spin on it once he got woken
1390 * up. Second wake up the waiter before the priority is adjusted. If we
1391 * deboost first (and lose our higher priority), then the task might get
1392 * scheduled away before the wake up can take place.
1393 */
1394 spin_unlock(&hb->lock);
1395 wake_up_q(&wake_q);
1396 if (deboost)
1397 rt_mutex_adjust_prio(current);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001398
1399 return 0;
1400}
1401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402/*
Ingo Molnar8b8f3192006-07-03 00:25:05 -07001403 * Express the locking dependencies for lockdep:
1404 */
1405static inline void
1406double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
1407{
1408 if (hb1 <= hb2) {
1409 spin_lock(&hb1->lock);
1410 if (hb1 < hb2)
1411 spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING);
1412 } else { /* hb1 > hb2 */
1413 spin_lock(&hb2->lock);
1414 spin_lock_nested(&hb1->lock, SINGLE_DEPTH_NESTING);
1415 }
1416}
1417
Darren Hart5eb3dc62009-03-12 00:55:52 -07001418static inline void
1419double_unlock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
1420{
Darren Hartf061d352009-03-12 15:11:18 -07001421 spin_unlock(&hb1->lock);
Ingo Molnar88f502f2009-03-13 10:32:07 +01001422 if (hb1 != hb2)
1423 spin_unlock(&hb2->lock);
Darren Hart5eb3dc62009-03-12 00:55:52 -07001424}
1425
Ingo Molnar8b8f3192006-07-03 00:25:05 -07001426/*
Darren Hartb2d09942009-03-12 00:55:37 -07001427 * Wake up waiters matching bitset queued on this futex (uaddr).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 */
Darren Hartb41277d2010-11-08 13:10:09 -08001429static int
1430futex_wake(u32 __user *uaddr, unsigned int flags, int nr_wake, u32 bitset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
Ingo Molnare2970f22006-06-27 02:54:47 -07001432 struct futex_hash_bucket *hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 struct futex_q *this, *next;
Peter Zijlstra38d47c12008-09-26 19:32:20 +02001434 union futex_key key = FUTEX_KEY_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 int ret;
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001436 WAKE_Q(wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Thomas Gleixnercd689982008-02-01 17:45:14 +01001438 if (!bitset)
1439 return -EINVAL;
1440
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001441 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 if (unlikely(ret != 0))
1443 goto out;
1444
Ingo Molnare2970f22006-06-27 02:54:47 -07001445 hb = hash_futex(&key);
Davidlohr Buesob0c29f72014-01-12 15:31:25 -08001446
1447 /* Make sure we really have tasks to wakeup */
1448 if (!hb_waiters_pending(hb))
1449 goto out_put_key;
1450
Ingo Molnare2970f22006-06-27 02:54:47 -07001451 spin_lock(&hb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Jason Low0d00c7b2014-01-12 15:31:22 -08001453 plist_for_each_entry_safe(this, next, &hb->chain, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 if (match_futex (&this->key, &key)) {
Darren Hart52400ba2009-04-03 13:40:49 -07001455 if (this->pi_state || this->rt_waiter) {
Ingo Molnared6f7b12006-07-01 04:35:46 -07001456 ret = -EINVAL;
1457 break;
1458 }
Thomas Gleixnercd689982008-02-01 17:45:14 +01001459
1460 /* Check if one of the bits is set in both bitsets */
1461 if (!(this->bitset & bitset))
1462 continue;
1463
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001464 mark_wake_futex(&wake_q, this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 if (++ret >= nr_wake)
1466 break;
1467 }
1468 }
1469
Ingo Molnare2970f22006-06-27 02:54:47 -07001470 spin_unlock(&hb->lock);
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001471 wake_up_q(&wake_q);
Davidlohr Buesob0c29f72014-01-12 15:31:25 -08001472out_put_key:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001473 put_futex_key(&key);
Darren Hart42d35d42008-12-29 15:49:53 -08001474out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 return ret;
1476}
1477
Jiri Slaby81da9f82017-08-24 09:31:05 +02001478static int futex_atomic_op_inuser(unsigned int encoded_op, u32 __user *uaddr)
1479{
1480 unsigned int op = (encoded_op & 0x70000000) >> 28;
1481 unsigned int cmp = (encoded_op & 0x0f000000) >> 24;
Jiri Slabya1640092017-11-30 15:35:44 +01001482 int oparg = sign_extend32((encoded_op & 0x00fff000) >> 12, 11);
1483 int cmparg = sign_extend32(encoded_op & 0x00000fff, 11);
Jiri Slaby81da9f82017-08-24 09:31:05 +02001484 int oldval, ret;
1485
1486 if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28)) {
Jiri Slabyda92e8a2017-10-23 13:41:51 +02001487 if (oparg < 0 || oparg > 31) {
1488 char comm[sizeof(current->comm)];
1489 /*
1490 * kill this print and return -EINVAL when userspace
1491 * is sane again
1492 */
1493 pr_info_ratelimited("futex_wake_op: %s tries to shift op by %d; fix this program\n",
1494 get_task_comm(comm, current), oparg);
1495 oparg &= 31;
1496 }
Jiri Slaby81da9f82017-08-24 09:31:05 +02001497 oparg = 1 << oparg;
1498 }
1499
1500 if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
1501 return -EFAULT;
1502
1503 ret = arch_futex_atomic_op_inuser(op, oparg, &oldval, uaddr);
1504 if (ret)
1505 return ret;
1506
1507 switch (cmp) {
1508 case FUTEX_OP_CMP_EQ:
1509 return oldval == cmparg;
1510 case FUTEX_OP_CMP_NE:
1511 return oldval != cmparg;
1512 case FUTEX_OP_CMP_LT:
1513 return oldval < cmparg;
1514 case FUTEX_OP_CMP_GE:
1515 return oldval >= cmparg;
1516 case FUTEX_OP_CMP_LE:
1517 return oldval <= cmparg;
1518 case FUTEX_OP_CMP_GT:
1519 return oldval > cmparg;
1520 default:
1521 return -ENOSYS;
1522 }
1523}
1524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525/*
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001526 * Wake up all waiters hashed on the physical page that is mapped
1527 * to this virtual address:
1528 */
Ingo Molnare2970f22006-06-27 02:54:47 -07001529static int
Darren Hartb41277d2010-11-08 13:10:09 -08001530futex_wake_op(u32 __user *uaddr1, unsigned int flags, u32 __user *uaddr2,
Ingo Molnare2970f22006-06-27 02:54:47 -07001531 int nr_wake, int nr_wake2, int op)
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001532{
Peter Zijlstra38d47c12008-09-26 19:32:20 +02001533 union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
Ingo Molnare2970f22006-06-27 02:54:47 -07001534 struct futex_hash_bucket *hb1, *hb2;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001535 struct futex_q *this, *next;
Darren Harte4dc5b72009-03-12 00:56:13 -07001536 int ret, op_ret;
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001537 WAKE_Q(wake_q);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001538
Darren Harte4dc5b72009-03-12 00:56:13 -07001539retry:
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001540 ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, VERIFY_READ);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001541 if (unlikely(ret != 0))
1542 goto out;
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001543 ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001544 if (unlikely(ret != 0))
Darren Hart42d35d42008-12-29 15:49:53 -08001545 goto out_put_key1;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001546
Ingo Molnare2970f22006-06-27 02:54:47 -07001547 hb1 = hash_futex(&key1);
1548 hb2 = hash_futex(&key2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001549
Darren Harte4dc5b72009-03-12 00:56:13 -07001550retry_private:
Thomas Gleixnereaaea802009-10-04 09:34:17 +02001551 double_lock_hb(hb1, hb2);
Ingo Molnare2970f22006-06-27 02:54:47 -07001552 op_ret = futex_atomic_op_inuser(op, uaddr2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001553 if (unlikely(op_ret < 0)) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001554
Darren Hart5eb3dc62009-03-12 00:55:52 -07001555 double_unlock_hb(hb1, hb2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001556
David Howells7ee1dd32006-01-06 00:11:44 -08001557#ifndef CONFIG_MMU
Ingo Molnare2970f22006-06-27 02:54:47 -07001558 /*
1559 * we don't get EFAULT from MMU faults if we don't have an MMU,
1560 * but we might get them from range checking
1561 */
David Howells7ee1dd32006-01-06 00:11:44 -08001562 ret = op_ret;
Darren Hart42d35d42008-12-29 15:49:53 -08001563 goto out_put_keys;
David Howells7ee1dd32006-01-06 00:11:44 -08001564#endif
1565
David Gibson796f8d92005-11-07 00:59:33 -08001566 if (unlikely(op_ret != -EFAULT)) {
1567 ret = op_ret;
Darren Hart42d35d42008-12-29 15:49:53 -08001568 goto out_put_keys;
David Gibson796f8d92005-11-07 00:59:33 -08001569 }
1570
Thomas Gleixnerd0725992009-06-11 23:15:43 +02001571 ret = fault_in_user_writeable(uaddr2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001572 if (ret)
Darren Hartde87fcc2009-03-12 00:55:46 -07001573 goto out_put_keys;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001574
Darren Hartb41277d2010-11-08 13:10:09 -08001575 if (!(flags & FLAGS_SHARED))
Darren Harte4dc5b72009-03-12 00:56:13 -07001576 goto retry_private;
1577
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001578 put_futex_key(&key2);
1579 put_futex_key(&key1);
Darren Harte4dc5b72009-03-12 00:56:13 -07001580 goto retry;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001581 }
1582
Jason Low0d00c7b2014-01-12 15:31:22 -08001583 plist_for_each_entry_safe(this, next, &hb1->chain, list) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001584 if (match_futex (&this->key, &key1)) {
Darren Hartaa109902012-11-26 16:29:56 -08001585 if (this->pi_state || this->rt_waiter) {
1586 ret = -EINVAL;
1587 goto out_unlock;
1588 }
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001589 mark_wake_futex(&wake_q, this);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001590 if (++ret >= nr_wake)
1591 break;
1592 }
1593 }
1594
1595 if (op_ret > 0) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001596 op_ret = 0;
Jason Low0d00c7b2014-01-12 15:31:22 -08001597 plist_for_each_entry_safe(this, next, &hb2->chain, list) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001598 if (match_futex (&this->key, &key2)) {
Darren Hartaa109902012-11-26 16:29:56 -08001599 if (this->pi_state || this->rt_waiter) {
1600 ret = -EINVAL;
1601 goto out_unlock;
1602 }
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001603 mark_wake_futex(&wake_q, this);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001604 if (++op_ret >= nr_wake2)
1605 break;
1606 }
1607 }
1608 ret += op_ret;
1609 }
1610
Darren Hartaa109902012-11-26 16:29:56 -08001611out_unlock:
Darren Hart5eb3dc62009-03-12 00:55:52 -07001612 double_unlock_hb(hb1, hb2);
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001613 wake_up_q(&wake_q);
Darren Hart42d35d42008-12-29 15:49:53 -08001614out_put_keys:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001615 put_futex_key(&key2);
Darren Hart42d35d42008-12-29 15:49:53 -08001616out_put_key1:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001617 put_futex_key(&key1);
Darren Hart42d35d42008-12-29 15:49:53 -08001618out:
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001619 return ret;
1620}
1621
Darren Hart9121e472009-04-03 13:40:31 -07001622/**
1623 * requeue_futex() - Requeue a futex_q from one hb to another
1624 * @q: the futex_q to requeue
1625 * @hb1: the source hash_bucket
1626 * @hb2: the target hash_bucket
1627 * @key2: the new key for the requeued futex_q
1628 */
1629static inline
1630void requeue_futex(struct futex_q *q, struct futex_hash_bucket *hb1,
1631 struct futex_hash_bucket *hb2, union futex_key *key2)
1632{
1633
1634 /*
1635 * If key1 and key2 hash to the same bucket, no need to
1636 * requeue.
1637 */
1638 if (likely(&hb1->chain != &hb2->chain)) {
1639 plist_del(&q->list, &hb1->chain);
Linus Torvalds11d46162014-03-20 22:11:17 -07001640 hb_waiters_dec(hb1);
Linus Torvalds11d46162014-03-20 22:11:17 -07001641 hb_waiters_inc(hb2);
Davidlohr Buesofe1bce92016-04-20 20:09:24 -07001642 plist_add(&q->list, &hb2->chain);
Darren Hart9121e472009-04-03 13:40:31 -07001643 q->lock_ptr = &hb2->lock;
Darren Hart9121e472009-04-03 13:40:31 -07001644 }
1645 get_futex_key_refs(key2);
1646 q->key = *key2;
1647}
1648
Darren Hart52400ba2009-04-03 13:40:49 -07001649/**
1650 * requeue_pi_wake_futex() - Wake a task that acquired the lock during requeue
Darren Hartd96ee562009-09-21 22:30:22 -07001651 * @q: the futex_q
1652 * @key: the key of the requeue target futex
1653 * @hb: the hash_bucket of the requeue target futex
Darren Hart52400ba2009-04-03 13:40:49 -07001654 *
1655 * During futex_requeue, with requeue_pi=1, it is possible to acquire the
1656 * target futex if it is uncontended or via a lock steal. Set the futex_q key
1657 * to the requeue target futex so the waiter can detect the wakeup on the right
1658 * futex, but remove it from the hb and NULL the rt_waiter so it can detect
Darren Hartbeda2c72009-08-09 15:34:39 -07001659 * atomic lock acquisition. Set the q->lock_ptr to the requeue target hb->lock
1660 * to protect access to the pi_state to fixup the owner later. Must be called
1661 * with both q->lock_ptr and hb->lock held.
Darren Hart52400ba2009-04-03 13:40:49 -07001662 */
1663static inline
Darren Hartbeda2c72009-08-09 15:34:39 -07001664void requeue_pi_wake_futex(struct futex_q *q, union futex_key *key,
1665 struct futex_hash_bucket *hb)
Darren Hart52400ba2009-04-03 13:40:49 -07001666{
Darren Hart52400ba2009-04-03 13:40:49 -07001667 get_futex_key_refs(key);
1668 q->key = *key;
1669
Lai Jiangshan2e129782010-12-22 14:18:50 +08001670 __unqueue_futex(q);
Darren Hart52400ba2009-04-03 13:40:49 -07001671
1672 WARN_ON(!q->rt_waiter);
1673 q->rt_waiter = NULL;
1674
Darren Hartbeda2c72009-08-09 15:34:39 -07001675 q->lock_ptr = &hb->lock;
Darren Hartbeda2c72009-08-09 15:34:39 -07001676
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001677 wake_up_state(q->task, TASK_NORMAL);
Darren Hart52400ba2009-04-03 13:40:49 -07001678}
1679
1680/**
1681 * futex_proxy_trylock_atomic() - Attempt an atomic lock for the top waiter
Darren Hartbab5bc92009-04-07 23:23:50 -07001682 * @pifutex: the user address of the to futex
1683 * @hb1: the from futex hash bucket, must be locked by the caller
1684 * @hb2: the to futex hash bucket, must be locked by the caller
1685 * @key1: the from futex key
1686 * @key2: the to futex key
1687 * @ps: address to store the pi_state pointer
1688 * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0)
Darren Hart52400ba2009-04-03 13:40:49 -07001689 *
1690 * Try and get the lock on behalf of the top waiter if we can do it atomically.
Darren Hartbab5bc92009-04-07 23:23:50 -07001691 * Wake the top waiter if we succeed. If the caller specified set_waiters,
1692 * then direct futex_lock_pi_atomic() to force setting the FUTEX_WAITERS bit.
1693 * hb1 and hb2 must be held by the caller.
Darren Hart52400ba2009-04-03 13:40:49 -07001694 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08001695 * Return:
1696 * 0 - failed to acquire the lock atomically;
Thomas Gleixner866293e2014-05-12 20:45:34 +00001697 * >0 - acquired the lock, return value is vpid of the top_waiter
Darren Hart52400ba2009-04-03 13:40:49 -07001698 * <0 - error
1699 */
1700static int futex_proxy_trylock_atomic(u32 __user *pifutex,
1701 struct futex_hash_bucket *hb1,
1702 struct futex_hash_bucket *hb2,
1703 union futex_key *key1, union futex_key *key2,
Darren Hartbab5bc92009-04-07 23:23:50 -07001704 struct futex_pi_state **ps, int set_waiters)
Darren Hart52400ba2009-04-03 13:40:49 -07001705{
Darren Hartbab5bc92009-04-07 23:23:50 -07001706 struct futex_q *top_waiter = NULL;
Darren Hart52400ba2009-04-03 13:40:49 -07001707 u32 curval;
Thomas Gleixner866293e2014-05-12 20:45:34 +00001708 int ret, vpid;
Darren Hart52400ba2009-04-03 13:40:49 -07001709
1710 if (get_futex_value_locked(&curval, pifutex))
1711 return -EFAULT;
1712
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001713 if (unlikely(should_fail_futex(true)))
1714 return -EFAULT;
1715
Darren Hartbab5bc92009-04-07 23:23:50 -07001716 /*
1717 * Find the top_waiter and determine if there are additional waiters.
1718 * If the caller intends to requeue more than 1 waiter to pifutex,
1719 * force futex_lock_pi_atomic() to set the FUTEX_WAITERS bit now,
1720 * as we have means to handle the possible fault. If not, don't set
1721 * the bit unecessarily as it will force the subsequent unlock to enter
1722 * the kernel.
1723 */
Darren Hart52400ba2009-04-03 13:40:49 -07001724 top_waiter = futex_top_waiter(hb1, key1);
1725
1726 /* There are no waiters, nothing for us to do. */
1727 if (!top_waiter)
1728 return 0;
1729
Darren Hart84bc4af2009-08-13 17:36:53 -07001730 /* Ensure we requeue to the expected futex. */
1731 if (!match_futex(top_waiter->requeue_pi_key, key2))
1732 return -EINVAL;
1733
Darren Hart52400ba2009-04-03 13:40:49 -07001734 /*
Darren Hartbab5bc92009-04-07 23:23:50 -07001735 * Try to take the lock for top_waiter. Set the FUTEX_WAITERS bit in
1736 * the contended case or if set_waiters is 1. The pi_state is returned
1737 * in ps in contended cases.
Darren Hart52400ba2009-04-03 13:40:49 -07001738 */
Thomas Gleixner866293e2014-05-12 20:45:34 +00001739 vpid = task_pid_vnr(top_waiter->task);
Darren Hartbab5bc92009-04-07 23:23:50 -07001740 ret = futex_lock_pi_atomic(pifutex, hb2, key2, ps, top_waiter->task,
1741 set_waiters);
Thomas Gleixner866293e2014-05-12 20:45:34 +00001742 if (ret == 1) {
Darren Hartbeda2c72009-08-09 15:34:39 -07001743 requeue_pi_wake_futex(top_waiter, key2, hb2);
Thomas Gleixner866293e2014-05-12 20:45:34 +00001744 return vpid;
1745 }
Darren Hart52400ba2009-04-03 13:40:49 -07001746 return ret;
1747}
1748
1749/**
1750 * futex_requeue() - Requeue waiters from uaddr1 to uaddr2
Randy Dunlapfb62db22010-10-13 11:02:34 -07001751 * @uaddr1: source futex user address
Darren Hartb41277d2010-11-08 13:10:09 -08001752 * @flags: futex flags (FLAGS_SHARED, etc.)
Randy Dunlapfb62db22010-10-13 11:02:34 -07001753 * @uaddr2: target futex user address
1754 * @nr_wake: number of waiters to wake (must be 1 for requeue_pi)
1755 * @nr_requeue: number of waiters to requeue (0-INT_MAX)
1756 * @cmpval: @uaddr1 expected value (or %NULL)
1757 * @requeue_pi: if we are attempting to requeue from a non-pi futex to a
Darren Hartb41277d2010-11-08 13:10:09 -08001758 * pi futex (pi to pi requeue is not supported)
Darren Hart52400ba2009-04-03 13:40:49 -07001759 *
1760 * Requeue waiters on uaddr1 to uaddr2. In the requeue_pi case, try to acquire
1761 * uaddr2 atomically on behalf of the top waiter.
1762 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08001763 * Return:
1764 * >=0 - on success, the number of tasks requeued or woken;
Darren Hart52400ba2009-04-03 13:40:49 -07001765 * <0 - on error
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 */
Darren Hartb41277d2010-11-08 13:10:09 -08001767static int futex_requeue(u32 __user *uaddr1, unsigned int flags,
1768 u32 __user *uaddr2, int nr_wake, int nr_requeue,
1769 u32 *cmpval, int requeue_pi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
Peter Zijlstra38d47c12008-09-26 19:32:20 +02001771 union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
Darren Hart52400ba2009-04-03 13:40:49 -07001772 int drop_count = 0, task_count = 0, ret;
1773 struct futex_pi_state *pi_state = NULL;
Ingo Molnare2970f22006-06-27 02:54:47 -07001774 struct futex_hash_bucket *hb1, *hb2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 struct futex_q *this, *next;
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001776 WAKE_Q(wake_q);
Darren Hart52400ba2009-04-03 13:40:49 -07001777
Li Jinyued8a31702017-12-14 17:04:54 +08001778 if (nr_wake < 0 || nr_requeue < 0)
1779 return -EINVAL;
1780
Darren Hart52400ba2009-04-03 13:40:49 -07001781 if (requeue_pi) {
1782 /*
Thomas Gleixnere9c243a2014-06-03 12:27:06 +00001783 * Requeue PI only works on two distinct uaddrs. This
1784 * check is only valid for private futexes. See below.
1785 */
1786 if (uaddr1 == uaddr2)
1787 return -EINVAL;
1788
1789 /*
Darren Hart52400ba2009-04-03 13:40:49 -07001790 * requeue_pi requires a pi_state, try to allocate it now
1791 * without any locks in case it fails.
1792 */
1793 if (refill_pi_state_cache())
1794 return -ENOMEM;
1795 /*
1796 * requeue_pi must wake as many tasks as it can, up to nr_wake
1797 * + nr_requeue, since it acquires the rt_mutex prior to
1798 * returning to userspace, so as to not leave the rt_mutex with
1799 * waiters and no owner. However, second and third wake-ups
1800 * cannot be predicted as they involve race conditions with the
1801 * first wake and a fault while looking up the pi_state. Both
1802 * pthread_cond_signal() and pthread_cond_broadcast() should
1803 * use nr_wake=1.
1804 */
1805 if (nr_wake != 1)
1806 return -EINVAL;
1807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Darren Hart42d35d42008-12-29 15:49:53 -08001809retry:
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001810 ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 if (unlikely(ret != 0))
1812 goto out;
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001813 ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2,
1814 requeue_pi ? VERIFY_WRITE : VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 if (unlikely(ret != 0))
Darren Hart42d35d42008-12-29 15:49:53 -08001816 goto out_put_key1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Thomas Gleixnere9c243a2014-06-03 12:27:06 +00001818 /*
1819 * The check above which compares uaddrs is not sufficient for
1820 * shared futexes. We need to compare the keys:
1821 */
1822 if (requeue_pi && match_futex(&key1, &key2)) {
1823 ret = -EINVAL;
1824 goto out_put_keys;
1825 }
1826
Ingo Molnare2970f22006-06-27 02:54:47 -07001827 hb1 = hash_futex(&key1);
1828 hb2 = hash_futex(&key2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Darren Harte4dc5b72009-03-12 00:56:13 -07001830retry_private:
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07001831 hb_waiters_inc(hb2);
Ingo Molnar8b8f3192006-07-03 00:25:05 -07001832 double_lock_hb(hb1, hb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Ingo Molnare2970f22006-06-27 02:54:47 -07001834 if (likely(cmpval != NULL)) {
1835 u32 curval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
Ingo Molnare2970f22006-06-27 02:54:47 -07001837 ret = get_futex_value_locked(&curval, uaddr1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
1839 if (unlikely(ret)) {
Darren Hart5eb3dc62009-03-12 00:55:52 -07001840 double_unlock_hb(hb1, hb2);
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07001841 hb_waiters_dec(hb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Darren Harte4dc5b72009-03-12 00:56:13 -07001843 ret = get_user(curval, uaddr1);
1844 if (ret)
1845 goto out_put_keys;
1846
Darren Hartb41277d2010-11-08 13:10:09 -08001847 if (!(flags & FLAGS_SHARED))
Darren Harte4dc5b72009-03-12 00:56:13 -07001848 goto retry_private;
1849
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001850 put_futex_key(&key2);
1851 put_futex_key(&key1);
Darren Harte4dc5b72009-03-12 00:56:13 -07001852 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 }
Ingo Molnare2970f22006-06-27 02:54:47 -07001854 if (curval != *cmpval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 ret = -EAGAIN;
1856 goto out_unlock;
1857 }
1858 }
1859
Darren Hart52400ba2009-04-03 13:40:49 -07001860 if (requeue_pi && (task_count - nr_wake < nr_requeue)) {
Darren Hartbab5bc92009-04-07 23:23:50 -07001861 /*
1862 * Attempt to acquire uaddr2 and wake the top waiter. If we
1863 * intend to requeue waiters, force setting the FUTEX_WAITERS
1864 * bit. We force this here where we are able to easily handle
1865 * faults rather in the requeue loop below.
1866 */
Darren Hart52400ba2009-04-03 13:40:49 -07001867 ret = futex_proxy_trylock_atomic(uaddr2, hb1, hb2, &key1,
Darren Hartbab5bc92009-04-07 23:23:50 -07001868 &key2, &pi_state, nr_requeue);
Darren Hart52400ba2009-04-03 13:40:49 -07001869
1870 /*
1871 * At this point the top_waiter has either taken uaddr2 or is
1872 * waiting on it. If the former, then the pi_state will not
1873 * exist yet, look it up one more time to ensure we have a
Thomas Gleixner866293e2014-05-12 20:45:34 +00001874 * reference to it. If the lock was taken, ret contains the
1875 * vpid of the top waiter task.
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00001876 * If the lock was not taken, we have pi_state and an initial
1877 * refcount on it. In case of an error we have nothing.
Darren Hart52400ba2009-04-03 13:40:49 -07001878 */
Thomas Gleixner866293e2014-05-12 20:45:34 +00001879 if (ret > 0) {
Darren Hart52400ba2009-04-03 13:40:49 -07001880 WARN_ON(pi_state);
Darren Hart89061d32009-10-15 15:30:48 -07001881 drop_count++;
Darren Hart52400ba2009-04-03 13:40:49 -07001882 task_count++;
Thomas Gleixner866293e2014-05-12 20:45:34 +00001883 /*
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00001884 * If we acquired the lock, then the user space value
1885 * of uaddr2 should be vpid. It cannot be changed by
1886 * the top waiter as it is blocked on hb2 lock if it
1887 * tries to do so. If something fiddled with it behind
1888 * our back the pi state lookup might unearth it. So
1889 * we rather use the known value than rereading and
1890 * handing potential crap to lookup_pi_state.
1891 *
1892 * If that call succeeds then we have pi_state and an
1893 * initial refcount on it.
Thomas Gleixner866293e2014-05-12 20:45:34 +00001894 */
Thomas Gleixner54a21782014-06-03 12:27:08 +00001895 ret = lookup_pi_state(ret, hb2, &key2, &pi_state);
Darren Hart52400ba2009-04-03 13:40:49 -07001896 }
1897
1898 switch (ret) {
1899 case 0:
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00001900 /* We hold a reference on the pi state. */
Darren Hart52400ba2009-04-03 13:40:49 -07001901 break;
Thomas Gleixner4959f2d2015-12-19 20:07:40 +00001902
1903 /* If the above failed, then pi_state is NULL */
Darren Hart52400ba2009-04-03 13:40:49 -07001904 case -EFAULT:
1905 double_unlock_hb(hb1, hb2);
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07001906 hb_waiters_dec(hb2);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001907 put_futex_key(&key2);
1908 put_futex_key(&key1);
Thomas Gleixnerd0725992009-06-11 23:15:43 +02001909 ret = fault_in_user_writeable(uaddr2);
Darren Hart52400ba2009-04-03 13:40:49 -07001910 if (!ret)
1911 goto retry;
1912 goto out;
1913 case -EAGAIN:
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001914 /*
1915 * Two reasons for this:
1916 * - Owner is exiting and we just wait for the
1917 * exit to complete.
1918 * - The user space value changed.
1919 */
Darren Hart52400ba2009-04-03 13:40:49 -07001920 double_unlock_hb(hb1, hb2);
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07001921 hb_waiters_dec(hb2);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001922 put_futex_key(&key2);
1923 put_futex_key(&key1);
Darren Hart52400ba2009-04-03 13:40:49 -07001924 cond_resched();
1925 goto retry;
1926 default:
1927 goto out_unlock;
1928 }
1929 }
1930
Jason Low0d00c7b2014-01-12 15:31:22 -08001931 plist_for_each_entry_safe(this, next, &hb1->chain, list) {
Darren Hart52400ba2009-04-03 13:40:49 -07001932 if (task_count - nr_wake >= nr_requeue)
1933 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Darren Hart52400ba2009-04-03 13:40:49 -07001935 if (!match_futex(&this->key, &key1))
1936 continue;
1937
Darren Hart392741e2009-08-07 15:20:48 -07001938 /*
1939 * FUTEX_WAIT_REQEUE_PI and FUTEX_CMP_REQUEUE_PI should always
1940 * be paired with each other and no other futex ops.
Darren Hartaa109902012-11-26 16:29:56 -08001941 *
1942 * We should never be requeueing a futex_q with a pi_state,
1943 * which is awaiting a futex_unlock_pi().
Darren Hart392741e2009-08-07 15:20:48 -07001944 */
1945 if ((requeue_pi && !this->rt_waiter) ||
Darren Hartaa109902012-11-26 16:29:56 -08001946 (!requeue_pi && this->rt_waiter) ||
1947 this->pi_state) {
Darren Hart392741e2009-08-07 15:20:48 -07001948 ret = -EINVAL;
1949 break;
1950 }
Darren Hart52400ba2009-04-03 13:40:49 -07001951
1952 /*
1953 * Wake nr_wake waiters. For requeue_pi, if we acquired the
1954 * lock, we already woke the top_waiter. If not, it will be
1955 * woken by futex_unlock_pi().
1956 */
1957 if (++task_count <= nr_wake && !requeue_pi) {
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001958 mark_wake_futex(&wake_q, this);
Darren Hart52400ba2009-04-03 13:40:49 -07001959 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 }
Darren Hart52400ba2009-04-03 13:40:49 -07001961
Darren Hart84bc4af2009-08-13 17:36:53 -07001962 /* Ensure we requeue to the expected futex for requeue_pi. */
1963 if (requeue_pi && !match_futex(this->requeue_pi_key, &key2)) {
1964 ret = -EINVAL;
1965 break;
1966 }
1967
Darren Hart52400ba2009-04-03 13:40:49 -07001968 /*
1969 * Requeue nr_requeue waiters and possibly one more in the case
1970 * of requeue_pi if we couldn't acquire the lock atomically.
1971 */
1972 if (requeue_pi) {
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00001973 /*
1974 * Prepare the waiter to take the rt_mutex. Take a
1975 * refcount on the pi_state and store the pointer in
1976 * the futex_q object of the waiter.
1977 */
Darren Hart52400ba2009-04-03 13:40:49 -07001978 atomic_inc(&pi_state->refcount);
1979 this->pi_state = pi_state;
1980 ret = rt_mutex_start_proxy_lock(&pi_state->pi_mutex,
1981 this->rt_waiter,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001982 this->task);
Darren Hart52400ba2009-04-03 13:40:49 -07001983 if (ret == 1) {
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00001984 /*
1985 * We got the lock. We do neither drop the
1986 * refcount on pi_state nor clear
1987 * this->pi_state because the waiter needs the
1988 * pi_state for cleaning up the user space
1989 * value. It will drop the refcount after
1990 * doing so.
1991 */
Darren Hartbeda2c72009-08-09 15:34:39 -07001992 requeue_pi_wake_futex(this, &key2, hb2);
Darren Hart89061d32009-10-15 15:30:48 -07001993 drop_count++;
Darren Hart52400ba2009-04-03 13:40:49 -07001994 continue;
1995 } else if (ret) {
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00001996 /*
1997 * rt_mutex_start_proxy_lock() detected a
1998 * potential deadlock when we tried to queue
1999 * that waiter. Drop the pi_state reference
2000 * which we took above and remove the pointer
2001 * to the state from the waiters futex_q
2002 * object.
2003 */
Darren Hart52400ba2009-04-03 13:40:49 -07002004 this->pi_state = NULL;
Thomas Gleixner29e9ee52015-12-19 20:07:39 +00002005 put_pi_state(pi_state);
Thomas Gleixner885c2cb2015-12-19 20:07:41 +00002006 /*
2007 * We stop queueing more waiters and let user
2008 * space deal with the mess.
2009 */
2010 break;
Darren Hart52400ba2009-04-03 13:40:49 -07002011 }
2012 }
2013 requeue_futex(this, hb1, hb2, &key2);
2014 drop_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 }
2016
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00002017 /*
2018 * We took an extra initial reference to the pi_state either
2019 * in futex_proxy_trylock_atomic() or in lookup_pi_state(). We
2020 * need to drop it here again.
2021 */
Thomas Gleixner29e9ee52015-12-19 20:07:39 +00002022 put_pi_state(pi_state);
Thomas Gleixner885c2cb2015-12-19 20:07:41 +00002023
2024out_unlock:
Darren Hart5eb3dc62009-03-12 00:55:52 -07002025 double_unlock_hb(hb1, hb2);
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07002026 wake_up_q(&wake_q);
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07002027 hb_waiters_dec(hb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Darren Hartcd84a422009-04-02 14:19:38 -07002029 /*
2030 * drop_futex_key_refs() must be called outside the spinlocks. During
2031 * the requeue we moved futex_q's from the hash bucket at key1 to the
2032 * one at key2 and updated their key pointer. We no longer need to
2033 * hold the references to key1.
2034 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 while (--drop_count >= 0)
Rusty Russell9adef582007-05-08 00:26:42 -07002036 drop_futex_key_refs(&key1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Darren Hart42d35d42008-12-29 15:49:53 -08002038out_put_keys:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002039 put_futex_key(&key2);
Darren Hart42d35d42008-12-29 15:49:53 -08002040out_put_key1:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002041 put_futex_key(&key1);
Darren Hart42d35d42008-12-29 15:49:53 -08002042out:
Darren Hart52400ba2009-04-03 13:40:49 -07002043 return ret ? ret : task_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044}
2045
2046/* The key must be already stored in q->key. */
Eric Sesterhenn82af7ac2008-01-25 10:40:46 +01002047static inline struct futex_hash_bucket *queue_lock(struct futex_q *q)
Namhyung Kim15e408c2010-09-14 21:43:48 +09002048 __acquires(&hb->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
Ingo Molnare2970f22006-06-27 02:54:47 -07002050 struct futex_hash_bucket *hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Ingo Molnare2970f22006-06-27 02:54:47 -07002052 hb = hash_futex(&q->key);
Linus Torvalds11d46162014-03-20 22:11:17 -07002053
2054 /*
2055 * Increment the counter before taking the lock so that
2056 * a potential waker won't miss a to-be-slept task that is
2057 * waiting for the spinlock. This is safe as all queue_lock()
2058 * users end up calling queue_me(). Similarly, for housekeeping,
2059 * decrement the counter at queue_unlock() when some error has
2060 * occurred and we don't end up adding the task to the list.
2061 */
2062 hb_waiters_inc(hb);
2063
Ingo Molnare2970f22006-06-27 02:54:47 -07002064 q->lock_ptr = &hb->lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Davidlohr Bueso8ad7b372016-02-09 11:15:13 -08002066 spin_lock(&hb->lock); /* implies smp_mb(); (A) */
Ingo Molnare2970f22006-06-27 02:54:47 -07002067 return hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068}
2069
Darren Hartd40d65c2009-09-21 22:30:15 -07002070static inline void
Jason Low0d00c7b2014-01-12 15:31:22 -08002071queue_unlock(struct futex_hash_bucket *hb)
Namhyung Kim15e408c2010-09-14 21:43:48 +09002072 __releases(&hb->lock)
Darren Hartd40d65c2009-09-21 22:30:15 -07002073{
2074 spin_unlock(&hb->lock);
Linus Torvalds11d46162014-03-20 22:11:17 -07002075 hb_waiters_dec(hb);
Darren Hartd40d65c2009-09-21 22:30:15 -07002076}
2077
2078/**
2079 * queue_me() - Enqueue the futex_q on the futex_hash_bucket
2080 * @q: The futex_q to enqueue
2081 * @hb: The destination hash bucket
2082 *
2083 * The hb->lock must be held by the caller, and is released here. A call to
2084 * queue_me() is typically paired with exactly one call to unqueue_me(). The
2085 * exceptions involve the PI related operations, which may use unqueue_me_pi()
2086 * or nothing if the unqueue is done as part of the wake process and the unqueue
2087 * state is implicit in the state of woken task (see futex_wait_requeue_pi() for
2088 * an example).
2089 */
Eric Sesterhenn82af7ac2008-01-25 10:40:46 +01002090static inline void queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
Namhyung Kim15e408c2010-09-14 21:43:48 +09002091 __releases(&hb->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
Pierre Peifferec92d082007-05-09 02:35:00 -07002093 int prio;
2094
2095 /*
2096 * The priority used to register this element is
2097 * - either the real thread-priority for the real-time threads
2098 * (i.e. threads with a priority lower than MAX_RT_PRIO)
2099 * - or MAX_RT_PRIO for non-RT threads.
2100 * Thus, all RT-threads are woken first in priority order, and
2101 * the others are woken last, in FIFO order.
2102 */
2103 prio = min(current->normal_prio, MAX_RT_PRIO);
2104
2105 plist_node_init(&q->list, prio);
Pierre Peifferec92d082007-05-09 02:35:00 -07002106 plist_add(&q->list, &hb->chain);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002107 q->task = current;
Ingo Molnare2970f22006-06-27 02:54:47 -07002108 spin_unlock(&hb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109}
2110
Darren Hartd40d65c2009-09-21 22:30:15 -07002111/**
2112 * unqueue_me() - Remove the futex_q from its futex_hash_bucket
2113 * @q: The futex_q to unqueue
2114 *
2115 * The q->lock_ptr must not be held by the caller. A call to unqueue_me() must
2116 * be paired with exactly one earlier call to queue_me().
2117 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002118 * Return:
2119 * 1 - if the futex_q was still queued (and we removed unqueued it);
Darren Hartd40d65c2009-09-21 22:30:15 -07002120 * 0 - if the futex_q was already removed by the waking thread
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122static int unqueue_me(struct futex_q *q)
2123{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 spinlock_t *lock_ptr;
Ingo Molnare2970f22006-06-27 02:54:47 -07002125 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
2127 /* In the common case we don't take the spinlock, which is nice. */
Darren Hart42d35d42008-12-29 15:49:53 -08002128retry:
Jianyu Zhan29b75eb2016-03-07 09:32:24 +08002129 /*
2130 * q->lock_ptr can change between this read and the following spin_lock.
2131 * Use READ_ONCE to forbid the compiler from reloading q->lock_ptr and
2132 * optimizing lock_ptr out of the logic below.
2133 */
2134 lock_ptr = READ_ONCE(q->lock_ptr);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07002135 if (lock_ptr != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 spin_lock(lock_ptr);
2137 /*
2138 * q->lock_ptr can change between reading it and
2139 * spin_lock(), causing us to take the wrong lock. This
2140 * corrects the race condition.
2141 *
2142 * Reasoning goes like this: if we have the wrong lock,
2143 * q->lock_ptr must have changed (maybe several times)
2144 * between reading it and the spin_lock(). It can
2145 * change again after the spin_lock() but only if it was
2146 * already changed before the spin_lock(). It cannot,
2147 * however, change back to the original value. Therefore
2148 * we can detect whether we acquired the correct lock.
2149 */
2150 if (unlikely(lock_ptr != q->lock_ptr)) {
2151 spin_unlock(lock_ptr);
2152 goto retry;
2153 }
Lai Jiangshan2e129782010-12-22 14:18:50 +08002154 __unqueue_futex(q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002155
2156 BUG_ON(q->pi_state);
2157
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 spin_unlock(lock_ptr);
2159 ret = 1;
2160 }
2161
Rusty Russell9adef582007-05-08 00:26:42 -07002162 drop_futex_key_refs(&q->key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 return ret;
2164}
2165
Ingo Molnarc87e2832006-06-27 02:54:58 -07002166/*
2167 * PI futexes can not be requeued and must remove themself from the
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002168 * hash bucket. The hash bucket lock (i.e. lock_ptr) is held on entry
2169 * and dropped here.
Ingo Molnarc87e2832006-06-27 02:54:58 -07002170 */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002171static void unqueue_me_pi(struct futex_q *q)
Namhyung Kim15e408c2010-09-14 21:43:48 +09002172 __releases(q->lock_ptr)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002173{
Lai Jiangshan2e129782010-12-22 14:18:50 +08002174 __unqueue_futex(q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002175
2176 BUG_ON(!q->pi_state);
Thomas Gleixner29e9ee52015-12-19 20:07:39 +00002177 put_pi_state(q->pi_state);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002178 q->pi_state = NULL;
2179
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002180 spin_unlock(q->lock_ptr);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002181}
2182
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002183/*
Thomas Gleixnercdf71a12008-01-08 19:47:38 +01002184 * Fixup the pi_state owner with the new owner.
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002185 *
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002186 * Must be called with hash bucket lock held and mm->sem held for non
2187 * private futexes.
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002188 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002189static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002190 struct task_struct *newowner)
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002191{
Thomas Gleixnercdf71a12008-01-08 19:47:38 +01002192 u32 newtid = task_pid_vnr(newowner) | FUTEX_WAITERS;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002193 struct futex_pi_state *pi_state = q->pi_state;
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002194 struct task_struct *oldowner = pi_state->owner;
Vitaliy Ivanov7cfdaf32011-07-07 15:10:31 +03002195 u32 uval, uninitialized_var(curval), newval;
Darren Harte4dc5b72009-03-12 00:56:13 -07002196 int ret;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002197
2198 /* Owner died? */
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002199 if (!pi_state->owner)
2200 newtid |= FUTEX_OWNER_DIED;
2201
2202 /*
2203 * We are here either because we stole the rtmutex from the
Lai Jiangshan81612392011-01-14 17:09:41 +08002204 * previous highest priority waiter or we are the highest priority
2205 * waiter but failed to get the rtmutex the first time.
2206 * We have to replace the newowner TID in the user space variable.
2207 * This must be atomic as we have to preserve the owner died bit here.
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002208 *
Darren Hartb2d09942009-03-12 00:55:37 -07002209 * Note: We write the user space value _before_ changing the pi_state
2210 * because we can fault here. Imagine swapped out pages or a fork
2211 * that marked all the anonymous memory readonly for cow.
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002212 *
2213 * Modifying pi_state _before_ the user space value would
2214 * leave the pi_state in an inconsistent state when we fault
2215 * here, because we need to drop the hash bucket lock to
2216 * handle the fault. This might be observed in the PID check
2217 * in lookup_pi_state.
2218 */
2219retry:
2220 if (get_futex_value_locked(&uval, uaddr))
2221 goto handle_fault;
2222
2223 while (1) {
2224 newval = (uval & FUTEX_OWNER_DIED) | newtid;
2225
Michel Lespinasse37a9d912011-03-10 18:48:51 -08002226 if (cmpxchg_futex_value_locked(&curval, uaddr, uval, newval))
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002227 goto handle_fault;
2228 if (curval == uval)
2229 break;
2230 uval = curval;
2231 }
2232
2233 /*
2234 * We fixed up user space. Now we need to fix the pi_state
2235 * itself.
2236 */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002237 if (pi_state->owner != NULL) {
Thomas Gleixner1d615482009-11-17 14:54:03 +01002238 raw_spin_lock_irq(&pi_state->owner->pi_lock);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002239 WARN_ON(list_empty(&pi_state->list));
2240 list_del_init(&pi_state->list);
Thomas Gleixner1d615482009-11-17 14:54:03 +01002241 raw_spin_unlock_irq(&pi_state->owner->pi_lock);
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002242 }
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002243
Thomas Gleixnercdf71a12008-01-08 19:47:38 +01002244 pi_state->owner = newowner;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002245
Thomas Gleixner1d615482009-11-17 14:54:03 +01002246 raw_spin_lock_irq(&newowner->pi_lock);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002247 WARN_ON(!list_empty(&pi_state->list));
Thomas Gleixnercdf71a12008-01-08 19:47:38 +01002248 list_add(&pi_state->list, &newowner->pi_state_list);
Thomas Gleixner1d615482009-11-17 14:54:03 +01002249 raw_spin_unlock_irq(&newowner->pi_lock);
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002250 return 0;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002251
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002252 /*
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002253 * To handle the page fault we need to drop the hash bucket
Lai Jiangshan81612392011-01-14 17:09:41 +08002254 * lock here. That gives the other task (either the highest priority
2255 * waiter itself or the task which stole the rtmutex) the
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002256 * chance to try the fixup of the pi_state. So once we are
2257 * back from handling the fault we need to check the pi_state
2258 * after reacquiring the hash bucket lock and before trying to
2259 * do another fixup. When the fixup has been done already we
2260 * simply return.
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002261 */
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002262handle_fault:
2263 spin_unlock(q->lock_ptr);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002264
Thomas Gleixnerd0725992009-06-11 23:15:43 +02002265 ret = fault_in_user_writeable(uaddr);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002266
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002267 spin_lock(q->lock_ptr);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002268
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002269 /*
2270 * Check if someone else fixed it for us:
2271 */
2272 if (pi_state->owner != oldowner)
2273 return 0;
2274
2275 if (ret)
2276 return ret;
2277
2278 goto retry;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002279}
2280
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002281static long futex_wait_restart(struct restart_block *restart);
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07002282
Darren Hartca5f9522009-04-03 13:39:33 -07002283/**
Darren Hartdd973992009-04-03 13:40:02 -07002284 * fixup_owner() - Post lock pi_state and corner case management
2285 * @uaddr: user address of the futex
Darren Hartdd973992009-04-03 13:40:02 -07002286 * @q: futex_q (contains pi_state and access to the rt_mutex)
2287 * @locked: if the attempt to take the rt_mutex succeeded (1) or not (0)
2288 *
2289 * After attempting to lock an rt_mutex, this function is called to cleanup
2290 * the pi_state owner as well as handle race conditions that may allow us to
2291 * acquire the lock. Must be called with the hb lock held.
2292 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002293 * Return:
2294 * 1 - success, lock taken;
2295 * 0 - success, lock not taken;
Darren Hartdd973992009-04-03 13:40:02 -07002296 * <0 - on error (-EFAULT)
2297 */
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002298static int fixup_owner(u32 __user *uaddr, struct futex_q *q, int locked)
Darren Hartdd973992009-04-03 13:40:02 -07002299{
2300 struct task_struct *owner;
2301 int ret = 0;
2302
2303 if (locked) {
2304 /*
2305 * Got the lock. We might not be the anticipated owner if we
2306 * did a lock-steal - fix up the PI-state in that case:
2307 */
2308 if (q->pi_state->owner != current)
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002309 ret = fixup_pi_state_owner(uaddr, q, current);
Darren Hartdd973992009-04-03 13:40:02 -07002310 goto out;
2311 }
2312
2313 /*
2314 * Catch the rare case, where the lock was released when we were on the
2315 * way back before we locked the hash bucket.
2316 */
2317 if (q->pi_state->owner == current) {
2318 /*
2319 * Try to get the rt_mutex now. This might fail as some other
2320 * task acquired the rt_mutex after we removed ourself from the
2321 * rt_mutex waiters list.
2322 */
2323 if (rt_mutex_trylock(&q->pi_state->pi_mutex)) {
2324 locked = 1;
2325 goto out;
2326 }
2327
2328 /*
2329 * pi_state is incorrect, some other task did a lock steal and
2330 * we returned due to timeout or signal without taking the
Lai Jiangshan81612392011-01-14 17:09:41 +08002331 * rt_mutex. Too late.
Darren Hartdd973992009-04-03 13:40:02 -07002332 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01002333 raw_spin_lock_irq(&q->pi_state->pi_mutex.wait_lock);
Darren Hartdd973992009-04-03 13:40:02 -07002334 owner = rt_mutex_owner(&q->pi_state->pi_mutex);
Lai Jiangshan81612392011-01-14 17:09:41 +08002335 if (!owner)
2336 owner = rt_mutex_next_owner(&q->pi_state->pi_mutex);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01002337 raw_spin_unlock_irq(&q->pi_state->pi_mutex.wait_lock);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002338 ret = fixup_pi_state_owner(uaddr, q, owner);
Darren Hartdd973992009-04-03 13:40:02 -07002339 goto out;
2340 }
2341
2342 /*
2343 * Paranoia check. If we did not take the lock, then we should not be
Lai Jiangshan81612392011-01-14 17:09:41 +08002344 * the owner of the rt_mutex.
Darren Hartdd973992009-04-03 13:40:02 -07002345 */
2346 if (rt_mutex_owner(&q->pi_state->pi_mutex) == current)
2347 printk(KERN_ERR "fixup_owner: ret = %d pi-mutex: %p "
2348 "pi-state %p\n", ret,
2349 q->pi_state->pi_mutex.owner,
2350 q->pi_state->owner);
2351
2352out:
2353 return ret ? ret : locked;
2354}
2355
2356/**
Darren Hartca5f9522009-04-03 13:39:33 -07002357 * futex_wait_queue_me() - queue_me() and wait for wakeup, timeout, or signal
2358 * @hb: the futex hash bucket, must be locked by the caller
2359 * @q: the futex_q to queue up on
2360 * @timeout: the prepared hrtimer_sleeper, or null for no timeout
Darren Hartca5f9522009-04-03 13:39:33 -07002361 */
2362static void futex_wait_queue_me(struct futex_hash_bucket *hb, struct futex_q *q,
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02002363 struct hrtimer_sleeper *timeout)
Darren Hartca5f9522009-04-03 13:39:33 -07002364{
Darren Hart9beba3c2009-09-24 11:54:47 -07002365 /*
2366 * The task state is guaranteed to be set before another task can
Peter Zijlstrab92b8b32015-05-12 10:51:55 +02002367 * wake it. set_current_state() is implemented using smp_store_mb() and
Darren Hart9beba3c2009-09-24 11:54:47 -07002368 * queue_me() calls spin_unlock() upon completion, both serializing
2369 * access to the hash list and forcing another memory barrier.
2370 */
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02002371 set_current_state(TASK_INTERRUPTIBLE);
Darren Hart0729e192009-09-21 22:30:38 -07002372 queue_me(q, hb);
Darren Hartca5f9522009-04-03 13:39:33 -07002373
2374 /* Arm the timer */
Thomas Gleixner2e4b0d32015-04-14 21:09:13 +00002375 if (timeout)
Darren Hartca5f9522009-04-03 13:39:33 -07002376 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
Darren Hartca5f9522009-04-03 13:39:33 -07002377
2378 /*
Darren Hart0729e192009-09-21 22:30:38 -07002379 * If we have been removed from the hash list, then another task
2380 * has tried to wake us, and we can skip the call to schedule().
Darren Hartca5f9522009-04-03 13:39:33 -07002381 */
2382 if (likely(!plist_node_empty(&q->list))) {
2383 /*
2384 * If the timer has already expired, current will already be
2385 * flagged for rescheduling. Only call schedule if there
2386 * is no timeout, or if it has yet to expire.
2387 */
2388 if (!timeout || timeout->task)
Colin Cross88c80042013-05-01 18:35:05 -07002389 freezable_schedule();
Darren Hartca5f9522009-04-03 13:39:33 -07002390 }
2391 __set_current_state(TASK_RUNNING);
2392}
2393
Darren Hartf8010732009-04-03 13:40:40 -07002394/**
2395 * futex_wait_setup() - Prepare to wait on a futex
2396 * @uaddr: the futex userspace address
2397 * @val: the expected value
Darren Hartb41277d2010-11-08 13:10:09 -08002398 * @flags: futex flags (FLAGS_SHARED, etc.)
Darren Hartf8010732009-04-03 13:40:40 -07002399 * @q: the associated futex_q
2400 * @hb: storage for hash_bucket pointer to be returned to caller
2401 *
2402 * Setup the futex_q and locate the hash_bucket. Get the futex value and
2403 * compare it with the expected value. Handle atomic faults internally.
2404 * Return with the hb lock held and a q.key reference on success, and unlocked
2405 * with no q.key reference on failure.
2406 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002407 * Return:
2408 * 0 - uaddr contains val and hb has been locked;
Bart Van Asscheca4a04c2011-07-17 09:01:00 +02002409 * <1 - -EFAULT or -EWOULDBLOCK (uaddr does not contain val) and hb is unlocked
Darren Hartf8010732009-04-03 13:40:40 -07002410 */
Darren Hartb41277d2010-11-08 13:10:09 -08002411static int futex_wait_setup(u32 __user *uaddr, u32 val, unsigned int flags,
Darren Hartf8010732009-04-03 13:40:40 -07002412 struct futex_q *q, struct futex_hash_bucket **hb)
2413{
2414 u32 uval;
2415 int ret;
2416
2417 /*
2418 * Access the page AFTER the hash-bucket is locked.
2419 * Order is important:
2420 *
2421 * Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val);
2422 * Userspace waker: if (cond(var)) { var = new; futex_wake(&var); }
2423 *
2424 * The basic logical guarantee of a futex is that it blocks ONLY
2425 * if cond(var) is known to be true at the time of blocking, for
Michel Lespinasse8fe8f542011-03-06 18:07:50 -08002426 * any cond. If we locked the hash-bucket after testing *uaddr, that
2427 * would open a race condition where we could block indefinitely with
Darren Hartf8010732009-04-03 13:40:40 -07002428 * cond(var) false, which would violate the guarantee.
2429 *
Michel Lespinasse8fe8f542011-03-06 18:07:50 -08002430 * On the other hand, we insert q and release the hash-bucket only
2431 * after testing *uaddr. This guarantees that futex_wait() will NOT
2432 * absorb a wakeup if *uaddr does not match the desired values
2433 * while the syscall executes.
Darren Hartf8010732009-04-03 13:40:40 -07002434 */
2435retry:
Shawn Bohrer9ea71502011-06-30 11:21:32 -05002436 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q->key, VERIFY_READ);
Darren Hartf8010732009-04-03 13:40:40 -07002437 if (unlikely(ret != 0))
Darren Harta5a2a0c2009-04-10 09:50:05 -07002438 return ret;
Darren Hartf8010732009-04-03 13:40:40 -07002439
2440retry_private:
2441 *hb = queue_lock(q);
2442
2443 ret = get_futex_value_locked(&uval, uaddr);
2444
2445 if (ret) {
Jason Low0d00c7b2014-01-12 15:31:22 -08002446 queue_unlock(*hb);
Darren Hartf8010732009-04-03 13:40:40 -07002447
2448 ret = get_user(uval, uaddr);
2449 if (ret)
2450 goto out;
2451
Darren Hartb41277d2010-11-08 13:10:09 -08002452 if (!(flags & FLAGS_SHARED))
Darren Hartf8010732009-04-03 13:40:40 -07002453 goto retry_private;
2454
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002455 put_futex_key(&q->key);
Darren Hartf8010732009-04-03 13:40:40 -07002456 goto retry;
2457 }
2458
2459 if (uval != val) {
Jason Low0d00c7b2014-01-12 15:31:22 -08002460 queue_unlock(*hb);
Darren Hartf8010732009-04-03 13:40:40 -07002461 ret = -EWOULDBLOCK;
2462 }
2463
2464out:
2465 if (ret)
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002466 put_futex_key(&q->key);
Darren Hartf8010732009-04-03 13:40:40 -07002467 return ret;
2468}
2469
Darren Hartb41277d2010-11-08 13:10:09 -08002470static int futex_wait(u32 __user *uaddr, unsigned int flags, u32 val,
2471 ktime_t *abs_time, u32 bitset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472{
Darren Hartca5f9522009-04-03 13:39:33 -07002473 struct hrtimer_sleeper timeout, *to = NULL;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002474 struct restart_block *restart;
Ingo Molnare2970f22006-06-27 02:54:47 -07002475 struct futex_hash_bucket *hb;
Darren Hart5bdb05f2010-11-08 13:40:28 -08002476 struct futex_q q = futex_q_init;
Ingo Molnare2970f22006-06-27 02:54:47 -07002477 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Thomas Gleixnercd689982008-02-01 17:45:14 +01002479 if (!bitset)
2480 return -EINVAL;
Thomas Gleixnercd689982008-02-01 17:45:14 +01002481 q.bitset = bitset;
Darren Hartca5f9522009-04-03 13:39:33 -07002482
2483 if (abs_time) {
2484 to = &timeout;
2485
Darren Hartb41277d2010-11-08 13:10:09 -08002486 hrtimer_init_on_stack(&to->timer, (flags & FLAGS_CLOCKRT) ?
2487 CLOCK_REALTIME : CLOCK_MONOTONIC,
2488 HRTIMER_MODE_ABS);
Darren Hartca5f9522009-04-03 13:39:33 -07002489 hrtimer_init_sleeper(to, current);
2490 hrtimer_set_expires_range_ns(&to->timer, *abs_time,
2491 current->timer_slack_ns);
2492 }
2493
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002494retry:
Darren Hart7ada8762010-10-17 08:35:04 -07002495 /*
2496 * Prepare to wait on uaddr. On success, holds hb lock and increments
2497 * q.key refs.
2498 */
Darren Hartb41277d2010-11-08 13:10:09 -08002499 ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
Darren Hartf8010732009-04-03 13:40:40 -07002500 if (ret)
Darren Hart42d35d42008-12-29 15:49:53 -08002501 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
Darren Hartca5f9522009-04-03 13:39:33 -07002503 /* queue_me and wait for wakeup, timeout, or a signal. */
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02002504 futex_wait_queue_me(hb, &q, to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505
2506 /* If we were woken (and unqueued), we succeeded, whatever. */
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002507 ret = 0;
Darren Hart7ada8762010-10-17 08:35:04 -07002508 /* unqueue_me() drops q.key ref */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 if (!unqueue_me(&q))
Darren Hart7ada8762010-10-17 08:35:04 -07002510 goto out;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002511 ret = -ETIMEDOUT;
Darren Hartca5f9522009-04-03 13:39:33 -07002512 if (to && !to->task)
Darren Hart7ada8762010-10-17 08:35:04 -07002513 goto out;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002514
Ingo Molnare2970f22006-06-27 02:54:47 -07002515 /*
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002516 * We expect signal_pending(current), but we might be the
2517 * victim of a spurious wakeup as well.
Ingo Molnare2970f22006-06-27 02:54:47 -07002518 */
Darren Hart7ada8762010-10-17 08:35:04 -07002519 if (!signal_pending(current))
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002520 goto retry;
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002521
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002522 ret = -ERESTARTSYS;
Pierre Peifferc19384b2007-05-09 02:35:02 -07002523 if (!abs_time)
Darren Hart7ada8762010-10-17 08:35:04 -07002524 goto out;
Steven Rostedtce6bd422007-12-05 15:46:09 +01002525
Andy Lutomirskif56141e2015-02-12 15:01:14 -08002526 restart = &current->restart_block;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002527 restart->fn = futex_wait_restart;
Namhyung Kima3c74c52010-09-14 21:43:47 +09002528 restart->futex.uaddr = uaddr;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002529 restart->futex.val = val;
2530 restart->futex.time = abs_time->tv64;
2531 restart->futex.bitset = bitset;
Darren Hart0cd9c642011-04-14 15:41:57 -07002532 restart->futex.flags = flags | FLAGS_HAS_TIMEOUT;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002533
2534 ret = -ERESTART_RESTARTBLOCK;
2535
Darren Hart42d35d42008-12-29 15:49:53 -08002536out:
Darren Hartca5f9522009-04-03 13:39:33 -07002537 if (to) {
2538 hrtimer_cancel(&to->timer);
2539 destroy_hrtimer_on_stack(&to->timer);
2540 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07002541 return ret;
2542}
2543
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002544
2545static long futex_wait_restart(struct restart_block *restart)
2546{
Namhyung Kima3c74c52010-09-14 21:43:47 +09002547 u32 __user *uaddr = restart->futex.uaddr;
Darren Harta72188d2009-04-03 13:40:22 -07002548 ktime_t t, *tp = NULL;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002549
Darren Harta72188d2009-04-03 13:40:22 -07002550 if (restart->futex.flags & FLAGS_HAS_TIMEOUT) {
2551 t.tv64 = restart->futex.time;
2552 tp = &t;
2553 }
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002554 restart->fn = do_no_restart_syscall;
Darren Hartb41277d2010-11-08 13:10:09 -08002555
2556 return (long)futex_wait(uaddr, restart->futex.flags,
2557 restart->futex.val, tp, restart->futex.bitset);
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002558}
2559
2560
Ingo Molnarc87e2832006-06-27 02:54:58 -07002561/*
2562 * Userspace tried a 0 -> TID atomic transition of the futex value
2563 * and failed. The kernel side here does the whole locking operation:
Davidlohr Bueso767f5092015-06-29 23:26:01 -07002564 * if there are waiters then it will block as a consequence of relying
2565 * on rt-mutexes, it does PI, etc. (Due to races the kernel might see
2566 * a 0 value of the futex too.).
2567 *
2568 * Also serves as futex trylock_pi()'ing, and due semantics.
Ingo Molnarc87e2832006-06-27 02:54:58 -07002569 */
Michael Kerrisk996636d2015-01-16 20:28:06 +01002570static int futex_lock_pi(u32 __user *uaddr, unsigned int flags,
Darren Hartb41277d2010-11-08 13:10:09 -08002571 ktime_t *time, int trylock)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002572{
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07002573 struct hrtimer_sleeper timeout, *to = NULL;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002574 struct futex_hash_bucket *hb;
Darren Hart5bdb05f2010-11-08 13:40:28 -08002575 struct futex_q q = futex_q_init;
Darren Hartdd973992009-04-03 13:40:02 -07002576 int res, ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002577
2578 if (refill_pi_state_cache())
2579 return -ENOMEM;
2580
Pierre Peifferc19384b2007-05-09 02:35:02 -07002581 if (time) {
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07002582 to = &timeout;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07002583 hrtimer_init_on_stack(&to->timer, CLOCK_REALTIME,
2584 HRTIMER_MODE_ABS);
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07002585 hrtimer_init_sleeper(to, current);
Arjan van de Vencc584b22008-09-01 15:02:30 -07002586 hrtimer_set_expires(&to->timer, *time);
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07002587 }
2588
Darren Hart42d35d42008-12-29 15:49:53 -08002589retry:
Shawn Bohrer9ea71502011-06-30 11:21:32 -05002590 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q.key, VERIFY_WRITE);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002591 if (unlikely(ret != 0))
Darren Hart42d35d42008-12-29 15:49:53 -08002592 goto out;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002593
Darren Harte4dc5b72009-03-12 00:56:13 -07002594retry_private:
Eric Sesterhenn82af7ac2008-01-25 10:40:46 +01002595 hb = queue_lock(&q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002596
Darren Hartbab5bc92009-04-07 23:23:50 -07002597 ret = futex_lock_pi_atomic(uaddr, hb, &q.key, &q.pi_state, current, 0);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002598 if (unlikely(ret)) {
Davidlohr Bueso767f5092015-06-29 23:26:01 -07002599 /*
2600 * Atomic work succeeded and we got the lock,
2601 * or failed. Either way, we do _not_ block.
2602 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002603 switch (ret) {
Darren Hart1a520842009-04-03 13:39:52 -07002604 case 1:
2605 /* We got the lock. */
2606 ret = 0;
2607 goto out_unlock_put_key;
2608 case -EFAULT:
2609 goto uaddr_faulted;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002610 case -EAGAIN:
2611 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00002612 * Two reasons for this:
2613 * - Task is exiting and we just wait for the
2614 * exit to complete.
2615 * - The user space value changed.
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002616 */
Jason Low0d00c7b2014-01-12 15:31:22 -08002617 queue_unlock(hb);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002618 put_futex_key(&q.key);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002619 cond_resched();
2620 goto retry;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002621 default:
Darren Hart42d35d42008-12-29 15:49:53 -08002622 goto out_unlock_put_key;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002623 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07002624 }
2625
2626 /*
2627 * Only actually queue now that the atomic ops are done:
2628 */
Eric Sesterhenn82af7ac2008-01-25 10:40:46 +01002629 queue_me(&q, hb);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002630
Ingo Molnarc87e2832006-06-27 02:54:58 -07002631 WARN_ON(!q.pi_state);
2632 /*
2633 * Block on the PI mutex:
2634 */
Thomas Gleixnerc051b212014-05-22 03:25:50 +00002635 if (!trylock) {
2636 ret = rt_mutex_timed_futex_lock(&q.pi_state->pi_mutex, to);
2637 } else {
Ingo Molnarc87e2832006-06-27 02:54:58 -07002638 ret = rt_mutex_trylock(&q.pi_state->pi_mutex);
2639 /* Fixup the trylock return value: */
2640 ret = ret ? 0 : -EWOULDBLOCK;
2641 }
2642
Vernon Mauerya99e4e42006-07-01 04:35:42 -07002643 spin_lock(q.lock_ptr);
Darren Hartdd973992009-04-03 13:40:02 -07002644 /*
2645 * Fixup the pi_state owner and possibly acquire the lock if we
2646 * haven't already.
2647 */
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002648 res = fixup_owner(uaddr, &q, !ret);
Darren Hartdd973992009-04-03 13:40:02 -07002649 /*
2650 * If fixup_owner() returned an error, proprogate that. If it acquired
2651 * the lock, clear our -ETIMEDOUT or -EINTR.
2652 */
2653 if (res)
2654 ret = (res < 0) ? res : 0;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002655
Darren Harte8f63862009-03-12 00:56:06 -07002656 /*
Darren Hartdd973992009-04-03 13:40:02 -07002657 * If fixup_owner() faulted and was unable to handle the fault, unlock
2658 * it and return the fault to userspace.
Darren Harte8f63862009-03-12 00:56:06 -07002659 */
2660 if (ret && (rt_mutex_owner(&q.pi_state->pi_mutex) == current))
2661 rt_mutex_unlock(&q.pi_state->pi_mutex);
2662
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002663 /* Unqueue and drop the lock */
2664 unqueue_me_pi(&q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002665
Mikael Pettersson5ecb01c2010-01-23 22:36:29 +01002666 goto out_put_key;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002667
Darren Hart42d35d42008-12-29 15:49:53 -08002668out_unlock_put_key:
Jason Low0d00c7b2014-01-12 15:31:22 -08002669 queue_unlock(hb);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002670
Darren Hart42d35d42008-12-29 15:49:53 -08002671out_put_key:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002672 put_futex_key(&q.key);
Darren Hart42d35d42008-12-29 15:49:53 -08002673out:
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07002674 if (to)
2675 destroy_hrtimer_on_stack(&to->timer);
Darren Hartdd973992009-04-03 13:40:02 -07002676 return ret != -EINTR ? ret : -ERESTARTNOINTR;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002677
Darren Hart42d35d42008-12-29 15:49:53 -08002678uaddr_faulted:
Jason Low0d00c7b2014-01-12 15:31:22 -08002679 queue_unlock(hb);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002680
Thomas Gleixnerd0725992009-06-11 23:15:43 +02002681 ret = fault_in_user_writeable(uaddr);
Darren Harte4dc5b72009-03-12 00:56:13 -07002682 if (ret)
2683 goto out_put_key;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002684
Darren Hartb41277d2010-11-08 13:10:09 -08002685 if (!(flags & FLAGS_SHARED))
Darren Harte4dc5b72009-03-12 00:56:13 -07002686 goto retry_private;
2687
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002688 put_futex_key(&q.key);
Darren Harte4dc5b72009-03-12 00:56:13 -07002689 goto retry;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002690}
2691
2692/*
Ingo Molnarc87e2832006-06-27 02:54:58 -07002693 * Userspace attempted a TID -> 0 atomic transition, and failed.
2694 * This is the in-kernel slowpath: we look up the PI state (if any),
2695 * and do the rt-mutex unlock.
2696 */
Darren Hartb41277d2010-11-08 13:10:09 -08002697static int futex_unlock_pi(u32 __user *uaddr, unsigned int flags)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002698{
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002699 u32 uninitialized_var(curval), uval, vpid = task_pid_vnr(current);
Peter Zijlstra38d47c12008-09-26 19:32:20 +02002700 union futex_key key = FUTEX_KEY_INIT;
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002701 struct futex_hash_bucket *hb;
2702 struct futex_q *match;
Darren Harte4dc5b72009-03-12 00:56:13 -07002703 int ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002704
2705retry:
2706 if (get_user(uval, uaddr))
2707 return -EFAULT;
2708 /*
2709 * We release only a lock we actually own:
2710 */
Thomas Gleixnerc0c9ed12011-03-11 11:51:22 +01002711 if ((uval & FUTEX_TID_MASK) != vpid)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002712 return -EPERM;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002713
Shawn Bohrer9ea71502011-06-30 11:21:32 -05002714 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, VERIFY_WRITE);
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002715 if (ret)
2716 return ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002717
2718 hb = hash_futex(&key);
2719 spin_lock(&hb->lock);
2720
Ingo Molnarc87e2832006-06-27 02:54:58 -07002721 /*
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002722 * Check waiters first. We do not trust user space values at
2723 * all and we at least want to know if user space fiddled
2724 * with the futex value instead of blindly unlocking.
Ingo Molnarc87e2832006-06-27 02:54:58 -07002725 */
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002726 match = futex_top_waiter(hb, &key);
2727 if (match) {
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02002728 ret = wake_futex_pi(uaddr, uval, match, hb);
2729 /*
2730 * In case of success wake_futex_pi dropped the hash
2731 * bucket lock.
2732 */
2733 if (!ret)
2734 goto out_putkey;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002735 /*
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002736 * The atomic access to the futex value generated a
2737 * pagefault, so retry the user-access and the wakeup:
Ingo Molnarc87e2832006-06-27 02:54:58 -07002738 */
2739 if (ret == -EFAULT)
2740 goto pi_faulted;
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02002741 /*
Sebastian Andrzej Siewior89e9e662016-04-15 14:35:39 +02002742 * A unconditional UNLOCK_PI op raced against a waiter
2743 * setting the FUTEX_WAITERS bit. Try again.
2744 */
2745 if (ret == -EAGAIN) {
2746 spin_unlock(&hb->lock);
2747 put_futex_key(&key);
2748 goto retry;
2749 }
2750 /*
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02002751 * wake_futex_pi has detected invalid state. Tell user
2752 * space.
2753 */
Ingo Molnarc87e2832006-06-27 02:54:58 -07002754 goto out_unlock;
2755 }
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002756
Ingo Molnarc87e2832006-06-27 02:54:58 -07002757 /*
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002758 * We have no kernel internal state, i.e. no waiters in the
2759 * kernel. Waiters which are about to queue themselves are stuck
2760 * on hb->lock. So we can safely ignore them. We do neither
2761 * preserve the WAITERS bit not the OWNER_DIED one. We are the
2762 * owner.
Ingo Molnarc87e2832006-06-27 02:54:58 -07002763 */
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002764 if (cmpxchg_futex_value_locked(&curval, uaddr, uval, 0))
Thomas Gleixner13fbca42014-06-03 12:27:07 +00002765 goto pi_faulted;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002766
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002767 /*
2768 * If uval has changed, let user space handle it.
2769 */
2770 ret = (curval == uval) ? 0 : -EAGAIN;
2771
Ingo Molnarc87e2832006-06-27 02:54:58 -07002772out_unlock:
2773 spin_unlock(&hb->lock);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02002774out_putkey:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002775 put_futex_key(&key);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002776 return ret;
2777
2778pi_faulted:
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002779 spin_unlock(&hb->lock);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002780 put_futex_key(&key);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002781
Thomas Gleixnerd0725992009-06-11 23:15:43 +02002782 ret = fault_in_user_writeable(uaddr);
Darren Hartb5686362008-12-18 15:06:34 -08002783 if (!ret)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002784 goto retry;
2785
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 return ret;
2787}
2788
Darren Hart52400ba2009-04-03 13:40:49 -07002789/**
2790 * handle_early_requeue_pi_wakeup() - Detect early wakeup on the initial futex
2791 * @hb: the hash_bucket futex_q was original enqueued on
2792 * @q: the futex_q woken while waiting to be requeued
2793 * @key2: the futex_key of the requeue target futex
2794 * @timeout: the timeout associated with the wait (NULL if none)
2795 *
2796 * Detect if the task was woken on the initial futex as opposed to the requeue
2797 * target futex. If so, determine if it was a timeout or a signal that caused
2798 * the wakeup and return the appropriate error code to the caller. Must be
2799 * called with the hb lock held.
2800 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002801 * Return:
2802 * 0 = no early wakeup detected;
2803 * <0 = -ETIMEDOUT or -ERESTARTNOINTR
Darren Hart52400ba2009-04-03 13:40:49 -07002804 */
2805static inline
2806int handle_early_requeue_pi_wakeup(struct futex_hash_bucket *hb,
2807 struct futex_q *q, union futex_key *key2,
2808 struct hrtimer_sleeper *timeout)
2809{
2810 int ret = 0;
2811
2812 /*
2813 * With the hb lock held, we avoid races while we process the wakeup.
2814 * We only need to hold hb (and not hb2) to ensure atomicity as the
2815 * wakeup code can't change q.key from uaddr to uaddr2 if we hold hb.
2816 * It can't be requeued from uaddr2 to something else since we don't
2817 * support a PI aware source futex for requeue.
2818 */
2819 if (!match_futex(&q->key, key2)) {
2820 WARN_ON(q->lock_ptr && (&hb->lock != q->lock_ptr));
2821 /*
2822 * We were woken prior to requeue by a timeout or a signal.
2823 * Unqueue the futex_q and determine which it was.
2824 */
Lai Jiangshan2e129782010-12-22 14:18:50 +08002825 plist_del(&q->list, &hb->chain);
Linus Torvalds11d46162014-03-20 22:11:17 -07002826 hb_waiters_dec(hb);
Darren Hart52400ba2009-04-03 13:40:49 -07002827
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002828 /* Handle spurious wakeups gracefully */
Thomas Gleixner11df6dd2009-10-28 20:26:48 +01002829 ret = -EWOULDBLOCK;
Darren Hart52400ba2009-04-03 13:40:49 -07002830 if (timeout && !timeout->task)
2831 ret = -ETIMEDOUT;
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002832 else if (signal_pending(current))
Thomas Gleixner1c840c12009-05-20 09:22:40 +02002833 ret = -ERESTARTNOINTR;
Darren Hart52400ba2009-04-03 13:40:49 -07002834 }
2835 return ret;
2836}
2837
2838/**
2839 * futex_wait_requeue_pi() - Wait on uaddr and take uaddr2
Darren Hart56ec1602009-09-21 22:29:59 -07002840 * @uaddr: the futex we initially wait on (non-pi)
Darren Hartb41277d2010-11-08 13:10:09 -08002841 * @flags: futex flags (FLAGS_SHARED, FLAGS_CLOCKRT, etc.), they must be
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07002842 * the same type, no requeueing from private to shared, etc.
Darren Hart52400ba2009-04-03 13:40:49 -07002843 * @val: the expected value of uaddr
2844 * @abs_time: absolute timeout
Darren Hart56ec1602009-09-21 22:29:59 -07002845 * @bitset: 32 bit wakeup bitset set by userspace, defaults to all
Darren Hart52400ba2009-04-03 13:40:49 -07002846 * @uaddr2: the pi futex we will take prior to returning to user-space
2847 *
2848 * The caller will wait on uaddr and will be requeued by futex_requeue() to
Darren Hart6f7b0a22012-07-20 11:53:31 -07002849 * uaddr2 which must be PI aware and unique from uaddr. Normal wakeup will wake
2850 * on uaddr2 and complete the acquisition of the rt_mutex prior to returning to
2851 * userspace. This ensures the rt_mutex maintains an owner when it has waiters;
2852 * without one, the pi logic would not know which task to boost/deboost, if
2853 * there was a need to.
Darren Hart52400ba2009-04-03 13:40:49 -07002854 *
2855 * We call schedule in futex_wait_queue_me() when we enqueue and return there
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002856 * via the following--
Darren Hart52400ba2009-04-03 13:40:49 -07002857 * 1) wakeup on uaddr2 after an atomic lock acquisition by futex_requeue()
Darren Hartcc6db4e2009-07-31 16:20:10 -07002858 * 2) wakeup on uaddr2 after a requeue
2859 * 3) signal
2860 * 4) timeout
Darren Hart52400ba2009-04-03 13:40:49 -07002861 *
Darren Hartcc6db4e2009-07-31 16:20:10 -07002862 * If 3, cleanup and return -ERESTARTNOINTR.
Darren Hart52400ba2009-04-03 13:40:49 -07002863 *
2864 * If 2, we may then block on trying to take the rt_mutex and return via:
2865 * 5) successful lock
2866 * 6) signal
2867 * 7) timeout
2868 * 8) other lock acquisition failure
2869 *
Darren Hartcc6db4e2009-07-31 16:20:10 -07002870 * If 6, return -EWOULDBLOCK (restarting the syscall would do the same).
Darren Hart52400ba2009-04-03 13:40:49 -07002871 *
2872 * If 4 or 7, we cleanup and return with -ETIMEDOUT.
2873 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002874 * Return:
2875 * 0 - On success;
Darren Hart52400ba2009-04-03 13:40:49 -07002876 * <0 - On error
2877 */
Darren Hartb41277d2010-11-08 13:10:09 -08002878static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
Darren Hart52400ba2009-04-03 13:40:49 -07002879 u32 val, ktime_t *abs_time, u32 bitset,
Darren Hartb41277d2010-11-08 13:10:09 -08002880 u32 __user *uaddr2)
Darren Hart52400ba2009-04-03 13:40:49 -07002881{
2882 struct hrtimer_sleeper timeout, *to = NULL;
2883 struct rt_mutex_waiter rt_waiter;
Darren Hart52400ba2009-04-03 13:40:49 -07002884 struct futex_hash_bucket *hb;
Darren Hart5bdb05f2010-11-08 13:40:28 -08002885 union futex_key key2 = FUTEX_KEY_INIT;
2886 struct futex_q q = futex_q_init;
Darren Hart52400ba2009-04-03 13:40:49 -07002887 int res, ret;
Darren Hart52400ba2009-04-03 13:40:49 -07002888
Darren Hart6f7b0a22012-07-20 11:53:31 -07002889 if (uaddr == uaddr2)
2890 return -EINVAL;
2891
Darren Hart52400ba2009-04-03 13:40:49 -07002892 if (!bitset)
2893 return -EINVAL;
2894
2895 if (abs_time) {
2896 to = &timeout;
Darren Hartb41277d2010-11-08 13:10:09 -08002897 hrtimer_init_on_stack(&to->timer, (flags & FLAGS_CLOCKRT) ?
2898 CLOCK_REALTIME : CLOCK_MONOTONIC,
2899 HRTIMER_MODE_ABS);
Darren Hart52400ba2009-04-03 13:40:49 -07002900 hrtimer_init_sleeper(to, current);
2901 hrtimer_set_expires_range_ns(&to->timer, *abs_time,
2902 current->timer_slack_ns);
2903 }
2904
2905 /*
2906 * The waiter is allocated on our stack, manipulated by the requeue
2907 * code while we sleep on uaddr.
2908 */
2909 debug_rt_mutex_init_waiter(&rt_waiter);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01002910 RB_CLEAR_NODE(&rt_waiter.pi_tree_entry);
2911 RB_CLEAR_NODE(&rt_waiter.tree_entry);
Darren Hart52400ba2009-04-03 13:40:49 -07002912 rt_waiter.task = NULL;
2913
Shawn Bohrer9ea71502011-06-30 11:21:32 -05002914 ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE);
Darren Hart52400ba2009-04-03 13:40:49 -07002915 if (unlikely(ret != 0))
2916 goto out;
2917
Darren Hart84bc4af2009-08-13 17:36:53 -07002918 q.bitset = bitset;
2919 q.rt_waiter = &rt_waiter;
2920 q.requeue_pi_key = &key2;
2921
Darren Hart7ada8762010-10-17 08:35:04 -07002922 /*
2923 * Prepare to wait on uaddr. On success, increments q.key (key1) ref
2924 * count.
2925 */
Darren Hartb41277d2010-11-08 13:10:09 -08002926 ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
Thomas Gleixnerc8b15a72009-05-20 09:18:50 +02002927 if (ret)
2928 goto out_key2;
Darren Hart52400ba2009-04-03 13:40:49 -07002929
Thomas Gleixnere9c243a2014-06-03 12:27:06 +00002930 /*
2931 * The check above which compares uaddrs is not sufficient for
2932 * shared futexes. We need to compare the keys:
2933 */
2934 if (match_futex(&q.key, &key2)) {
Thomas Gleixner13c42c22014-09-11 23:44:35 +02002935 queue_unlock(hb);
Thomas Gleixnere9c243a2014-06-03 12:27:06 +00002936 ret = -EINVAL;
2937 goto out_put_keys;
2938 }
2939
Darren Hart52400ba2009-04-03 13:40:49 -07002940 /* Queue the futex_q, drop the hb lock, wait for wakeup. */
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02002941 futex_wait_queue_me(hb, &q, to);
Darren Hart52400ba2009-04-03 13:40:49 -07002942
2943 spin_lock(&hb->lock);
2944 ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
2945 spin_unlock(&hb->lock);
2946 if (ret)
2947 goto out_put_keys;
2948
2949 /*
2950 * In order for us to be here, we know our q.key == key2, and since
2951 * we took the hb->lock above, we also know that futex_requeue() has
2952 * completed and we no longer have to concern ourselves with a wakeup
Darren Hart7ada8762010-10-17 08:35:04 -07002953 * race with the atomic proxy lock acquisition by the requeue code. The
2954 * futex_requeue dropped our key1 reference and incremented our key2
2955 * reference count.
Darren Hart52400ba2009-04-03 13:40:49 -07002956 */
2957
2958 /* Check if the requeue code acquired the second futex for us. */
2959 if (!q.rt_waiter) {
2960 /*
2961 * Got the lock. We might not be the anticipated owner if we
2962 * did a lock-steal - fix up the PI-state in that case.
2963 */
2964 if (q.pi_state && (q.pi_state->owner != current)) {
2965 spin_lock(q.lock_ptr);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002966 ret = fixup_pi_state_owner(uaddr2, &q, current);
Peter Zijlstra15221812017-03-04 10:27:19 +01002967 if (ret && rt_mutex_owner(&q.pi_state->pi_mutex) == current)
2968 rt_mutex_unlock(&q.pi_state->pi_mutex);
Thomas Gleixnerfb75a422015-12-19 20:07:38 +00002969 /*
2970 * Drop the reference to the pi state which
2971 * the requeue_pi() code acquired for us.
2972 */
Thomas Gleixner29e9ee52015-12-19 20:07:39 +00002973 put_pi_state(q.pi_state);
Darren Hart52400ba2009-04-03 13:40:49 -07002974 spin_unlock(q.lock_ptr);
2975 }
2976 } else {
Peter Zijlstra6244ffc2017-03-04 10:27:18 +01002977 struct rt_mutex *pi_mutex;
2978
Darren Hart52400ba2009-04-03 13:40:49 -07002979 /*
2980 * We have been woken up by futex_unlock_pi(), a timeout, or a
2981 * signal. futex_unlock_pi() will not destroy the lock_ptr nor
2982 * the pi_state.
2983 */
Darren Hartf27071c2012-07-20 11:53:30 -07002984 WARN_ON(!q.pi_state);
Darren Hart52400ba2009-04-03 13:40:49 -07002985 pi_mutex = &q.pi_state->pi_mutex;
Peter Zijlstrace813552017-03-22 11:35:57 +01002986 ret = rt_mutex_wait_proxy_lock(pi_mutex, to, &rt_waiter);
Darren Hart52400ba2009-04-03 13:40:49 -07002987
2988 spin_lock(q.lock_ptr);
Peter Zijlstrace813552017-03-22 11:35:57 +01002989 if (ret && !rt_mutex_cleanup_proxy_lock(pi_mutex, &rt_waiter))
2990 ret = 0;
2991
2992 debug_rt_mutex_free_waiter(&rt_waiter);
Darren Hart52400ba2009-04-03 13:40:49 -07002993 /*
2994 * Fixup the pi_state owner and possibly acquire the lock if we
2995 * haven't already.
2996 */
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002997 res = fixup_owner(uaddr2, &q, !ret);
Darren Hart52400ba2009-04-03 13:40:49 -07002998 /*
2999 * If fixup_owner() returned an error, proprogate that. If it
Darren Hart56ec1602009-09-21 22:29:59 -07003000 * acquired the lock, clear -ETIMEDOUT or -EINTR.
Darren Hart52400ba2009-04-03 13:40:49 -07003001 */
3002 if (res)
3003 ret = (res < 0) ? res : 0;
3004
Peter Zijlstra6244ffc2017-03-04 10:27:18 +01003005 /*
3006 * If fixup_pi_state_owner() faulted and was unable to handle
3007 * the fault, unlock the rt_mutex and return the fault to
3008 * userspace.
3009 */
3010 if (ret && rt_mutex_owner(pi_mutex) == current)
3011 rt_mutex_unlock(pi_mutex);
3012
Darren Hart52400ba2009-04-03 13:40:49 -07003013 /* Unqueue and drop the lock. */
3014 unqueue_me_pi(&q);
3015 }
3016
Peter Zijlstra6244ffc2017-03-04 10:27:18 +01003017 if (ret == -EINTR) {
Darren Hart52400ba2009-04-03 13:40:49 -07003018 /*
Darren Hartcc6db4e2009-07-31 16:20:10 -07003019 * We've already been requeued, but cannot restart by calling
3020 * futex_lock_pi() directly. We could restart this syscall, but
3021 * it would detect that the user space "val" changed and return
3022 * -EWOULDBLOCK. Save the overhead of the restart and return
3023 * -EWOULDBLOCK directly.
Darren Hart52400ba2009-04-03 13:40:49 -07003024 */
Thomas Gleixner20708872009-05-19 23:04:59 +02003025 ret = -EWOULDBLOCK;
Darren Hart52400ba2009-04-03 13:40:49 -07003026 }
3027
3028out_put_keys:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01003029 put_futex_key(&q.key);
Thomas Gleixnerc8b15a72009-05-20 09:18:50 +02003030out_key2:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01003031 put_futex_key(&key2);
Darren Hart52400ba2009-04-03 13:40:49 -07003032
3033out:
3034 if (to) {
3035 hrtimer_cancel(&to->timer);
3036 destroy_hrtimer_on_stack(&to->timer);
3037 }
3038 return ret;
3039}
3040
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003041/*
3042 * Support for robust futexes: the kernel cleans up held futexes at
3043 * thread exit time.
3044 *
3045 * Implementation: user-space maintains a per-thread list of locks it
3046 * is holding. Upon do_exit(), the kernel carefully walks this list,
3047 * and marks all locks that are owned by this thread with the
Ingo Molnarc87e2832006-06-27 02:54:58 -07003048 * FUTEX_OWNER_DIED bit, and wakes up a waiter (if any). The list is
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003049 * always manipulated with the lock held, so the list is private and
3050 * per-thread. Userspace also maintains a per-thread 'list_op_pending'
3051 * field, to allow the kernel to clean up if the thread dies after
3052 * acquiring the lock, but just before it could have added itself to
3053 * the list. There can only be one such pending lock.
3054 */
3055
3056/**
Darren Hartd96ee562009-09-21 22:30:22 -07003057 * sys_set_robust_list() - Set the robust-futex list head of a task
3058 * @head: pointer to the list-head
3059 * @len: length of the list-head, as userspace expects
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003060 */
Heiko Carstens836f92a2009-01-14 14:14:33 +01003061SYSCALL_DEFINE2(set_robust_list, struct robust_list_head __user *, head,
3062 size_t, len)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003063{
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08003064 if (!futex_cmpxchg_enabled)
3065 return -ENOSYS;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003066 /*
3067 * The kernel knows only one size for now:
3068 */
3069 if (unlikely(len != sizeof(*head)))
3070 return -EINVAL;
3071
3072 current->robust_list = head;
3073
3074 return 0;
3075}
3076
3077/**
Darren Hartd96ee562009-09-21 22:30:22 -07003078 * sys_get_robust_list() - Get the robust-futex list head of a task
3079 * @pid: pid of the process [zero for current task]
3080 * @head_ptr: pointer to a list-head pointer, the kernel fills it in
3081 * @len_ptr: pointer to a length field, the kernel fills in the header size
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003082 */
Heiko Carstens836f92a2009-01-14 14:14:33 +01003083SYSCALL_DEFINE3(get_robust_list, int, pid,
3084 struct robust_list_head __user * __user *, head_ptr,
3085 size_t __user *, len_ptr)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003086{
Al Viroba46df92006-10-10 22:46:07 +01003087 struct robust_list_head __user *head;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003088 unsigned long ret;
Kees Cookbdbb7762012-03-19 16:12:53 -07003089 struct task_struct *p;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003090
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08003091 if (!futex_cmpxchg_enabled)
3092 return -ENOSYS;
3093
Kees Cookbdbb7762012-03-19 16:12:53 -07003094 rcu_read_lock();
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003095
Kees Cookbdbb7762012-03-19 16:12:53 -07003096 ret = -ESRCH;
3097 if (!pid)
3098 p = current;
3099 else {
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07003100 p = find_task_by_vpid(pid);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003101 if (!p)
3102 goto err_unlock;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003103 }
3104
Kees Cookbdbb7762012-03-19 16:12:53 -07003105 ret = -EPERM;
Jann Horncaaee622016-01-20 15:00:04 -08003106 if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS))
Kees Cookbdbb7762012-03-19 16:12:53 -07003107 goto err_unlock;
3108
3109 head = p->robust_list;
3110 rcu_read_unlock();
3111
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003112 if (put_user(sizeof(*head), len_ptr))
3113 return -EFAULT;
3114 return put_user(head, head_ptr);
3115
3116err_unlock:
Oleg Nesterovaaa2a972006-09-29 02:00:55 -07003117 rcu_read_unlock();
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003118
3119 return ret;
3120}
3121
3122/*
3123 * Process a futex-list entry, check whether it's owned by the
3124 * dying task, and do notification if so:
3125 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003126int handle_futex_death(u32 __user *uaddr, struct task_struct *curr, int pi)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003127{
Vitaliy Ivanov7cfdaf32011-07-07 15:10:31 +03003128 u32 uval, uninitialized_var(nval), mval;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003129
Chen Jie726c28f2019-03-15 03:44:38 +00003130 /* Futex address must be 32bit aligned */
3131 if ((((unsigned long)uaddr) % sizeof(*uaddr)) != 0)
3132 return -1;
3133
Ingo Molnar8f17d3a2006-03-27 01:16:27 -08003134retry:
3135 if (get_user(uval, uaddr))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003136 return -1;
3137
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003138 if ((uval & FUTEX_TID_MASK) == task_pid_vnr(curr)) {
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003139 /*
3140 * Ok, this dying thread is truly holding a futex
3141 * of interest. Set the OWNER_DIED bit atomically
3142 * via cmpxchg, and if the value had FUTEX_WAITERS
3143 * set, wake up a waiter (if any). (We have to do a
3144 * futex_wake() even if OWNER_DIED is already set -
3145 * to handle the rare but possible case of recursive
3146 * thread-death.) The rest of the cleanup is done in
3147 * userspace.
3148 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003149 mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
Thomas Gleixner6e0aa9f2011-03-14 10:34:35 +01003150 /*
3151 * We are not holding a lock here, but we want to have
3152 * the pagefault_disable/enable() protection because
3153 * we want to handle the fault gracefully. If the
3154 * access fails we try to fault in the futex with R/W
3155 * verification via get_user_pages. get_user() above
3156 * does not guarantee R/W access. If that fails we
3157 * give up and leave the futex locked.
3158 */
3159 if (cmpxchg_futex_value_locked(&nval, uaddr, uval, mval)) {
3160 if (fault_in_user_writeable(uaddr))
3161 return -1;
3162 goto retry;
3163 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07003164 if (nval != uval)
Ingo Molnar8f17d3a2006-03-27 01:16:27 -08003165 goto retry;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003166
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003167 /*
3168 * Wake robust non-PI futexes here. The wakeup of
3169 * PI futexes happens in exit_pi_state():
3170 */
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07003171 if (!pi && (uval & FUTEX_WAITERS))
Peter Zijlstrac2f9f202008-09-26 19:32:23 +02003172 futex_wake(uaddr, 1, 1, FUTEX_BITSET_MATCH_ANY);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003173 }
3174 return 0;
3175}
3176
3177/*
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003178 * Fetch a robust-list pointer. Bit 0 signals PI futexes:
3179 */
3180static inline int fetch_robust_entry(struct robust_list __user **entry,
Al Viroba46df92006-10-10 22:46:07 +01003181 struct robust_list __user * __user *head,
Namhyung Kim1dcc41b2010-09-14 21:43:46 +09003182 unsigned int *pi)
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003183{
3184 unsigned long uentry;
3185
Al Viroba46df92006-10-10 22:46:07 +01003186 if (get_user(uentry, (unsigned long __user *)head))
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003187 return -EFAULT;
3188
Al Viroba46df92006-10-10 22:46:07 +01003189 *entry = (void __user *)(uentry & ~1UL);
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003190 *pi = uentry & 1;
3191
3192 return 0;
3193}
3194
3195/*
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003196 * Walk curr->robust_list (very carefully, it's a userspace list!)
3197 * and mark any locks found there dead, and notify any waiters.
3198 *
3199 * We silently return on any sign of list-walking problem.
3200 */
3201void exit_robust_list(struct task_struct *curr)
3202{
3203 struct robust_list_head __user *head = curr->robust_list;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003204 struct robust_list __user *entry, *next_entry, *pending;
Darren Hart4c115e92010-11-04 15:00:00 -04003205 unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
3206 unsigned int uninitialized_var(next_pi);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003207 unsigned long futex_offset;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003208 int rc;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003209
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08003210 if (!futex_cmpxchg_enabled)
3211 return;
3212
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003213 /*
3214 * Fetch the list head (which was registered earlier, via
3215 * sys_set_robust_list()):
3216 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003217 if (fetch_robust_entry(&entry, &head->list.next, &pi))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003218 return;
3219 /*
3220 * Fetch the relative futex offset:
3221 */
3222 if (get_user(futex_offset, &head->futex_offset))
3223 return;
3224 /*
3225 * Fetch any possibly pending lock-add first, and handle it
3226 * if it exists:
3227 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003228 if (fetch_robust_entry(&pending, &head->list_op_pending, &pip))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003229 return;
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003230
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003231 next_entry = NULL; /* avoid warning with gcc */
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003232 while (entry != &head->list) {
3233 /*
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003234 * Fetch the next entry in the list before calling
3235 * handle_futex_death:
3236 */
3237 rc = fetch_robust_entry(&next_entry, &entry->next, &next_pi);
3238 /*
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003239 * A pending lock might already be on the list, so
Ingo Molnarc87e2832006-06-27 02:54:58 -07003240 * don't process it twice:
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003241 */
3242 if (entry != pending)
Al Viroba46df92006-10-10 22:46:07 +01003243 if (handle_futex_death((void __user *)entry + futex_offset,
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003244 curr, pi))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003245 return;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003246 if (rc)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003247 return;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003248 entry = next_entry;
3249 pi = next_pi;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003250 /*
3251 * Avoid excessively long or circular lists:
3252 */
3253 if (!--limit)
3254 break;
3255
3256 cond_resched();
3257 }
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003258
3259 if (pending)
3260 handle_futex_death((void __user *)pending + futex_offset,
3261 curr, pip);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003262}
3263
Pierre Peifferc19384b2007-05-09 02:35:02 -07003264long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
Ingo Molnare2970f22006-06-27 02:54:47 -07003265 u32 __user *uaddr2, u32 val2, u32 val3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266{
Thomas Gleixner81b40532012-02-15 12:17:09 +01003267 int cmd = op & FUTEX_CMD_MASK;
Darren Hartb41277d2010-11-08 13:10:09 -08003268 unsigned int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003270 if (!(op & FUTEX_PRIVATE_FLAG))
Darren Hartb41277d2010-11-08 13:10:09 -08003271 flags |= FLAGS_SHARED;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003272
Darren Hartb41277d2010-11-08 13:10:09 -08003273 if (op & FUTEX_CLOCK_REALTIME) {
3274 flags |= FLAGS_CLOCKRT;
Darren Hart337f1302015-12-18 13:36:37 -08003275 if (cmd != FUTEX_WAIT && cmd != FUTEX_WAIT_BITSET && \
3276 cmd != FUTEX_WAIT_REQUEUE_PI)
Darren Hartb41277d2010-11-08 13:10:09 -08003277 return -ENOSYS;
3278 }
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003279
3280 switch (cmd) {
Thomas Gleixner59263b52012-02-15 12:08:34 +01003281 case FUTEX_LOCK_PI:
3282 case FUTEX_UNLOCK_PI:
3283 case FUTEX_TRYLOCK_PI:
3284 case FUTEX_WAIT_REQUEUE_PI:
3285 case FUTEX_CMP_REQUEUE_PI:
3286 if (!futex_cmpxchg_enabled)
3287 return -ENOSYS;
3288 }
3289
3290 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 case FUTEX_WAIT:
Thomas Gleixnercd689982008-02-01 17:45:14 +01003292 val3 = FUTEX_BITSET_MATCH_ANY;
3293 case FUTEX_WAIT_BITSET:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003294 return futex_wait(uaddr, flags, val, timeout, val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 case FUTEX_WAKE:
Thomas Gleixnercd689982008-02-01 17:45:14 +01003296 val3 = FUTEX_BITSET_MATCH_ANY;
3297 case FUTEX_WAKE_BITSET:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003298 return futex_wake(uaddr, flags, val, val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 case FUTEX_REQUEUE:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003300 return futex_requeue(uaddr, flags, uaddr2, val, val2, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 case FUTEX_CMP_REQUEUE:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003302 return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 0);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07003303 case FUTEX_WAKE_OP:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003304 return futex_wake_op(uaddr, flags, uaddr2, val, val2, val3);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003305 case FUTEX_LOCK_PI:
Michael Kerrisk996636d2015-01-16 20:28:06 +01003306 return futex_lock_pi(uaddr, flags, timeout, 0);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003307 case FUTEX_UNLOCK_PI:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003308 return futex_unlock_pi(uaddr, flags);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003309 case FUTEX_TRYLOCK_PI:
Michael Kerrisk996636d2015-01-16 20:28:06 +01003310 return futex_lock_pi(uaddr, flags, NULL, 1);
Darren Hart52400ba2009-04-03 13:40:49 -07003311 case FUTEX_WAIT_REQUEUE_PI:
3312 val3 = FUTEX_BITSET_MATCH_ANY;
Thomas Gleixner81b40532012-02-15 12:17:09 +01003313 return futex_wait_requeue_pi(uaddr, flags, val, timeout, val3,
3314 uaddr2);
Darren Hart52400ba2009-04-03 13:40:49 -07003315 case FUTEX_CMP_REQUEUE_PI:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003316 return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 }
Thomas Gleixner81b40532012-02-15 12:17:09 +01003318 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319}
3320
3321
Heiko Carstens17da2bd2009-01-14 14:14:10 +01003322SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val,
3323 struct timespec __user *, utime, u32 __user *, uaddr2,
3324 u32, val3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325{
Pierre Peifferc19384b2007-05-09 02:35:02 -07003326 struct timespec ts;
3327 ktime_t t, *tp = NULL;
Ingo Molnare2970f22006-06-27 02:54:47 -07003328 u32 val2 = 0;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003329 int cmd = op & FUTEX_CMD_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330
Thomas Gleixnercd689982008-02-01 17:45:14 +01003331 if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI ||
Darren Hart52400ba2009-04-03 13:40:49 -07003332 cmd == FUTEX_WAIT_BITSET ||
3333 cmd == FUTEX_WAIT_REQUEUE_PI)) {
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07003334 if (unlikely(should_fail_futex(!(op & FUTEX_PRIVATE_FLAG))))
3335 return -EFAULT;
Pierre Peifferc19384b2007-05-09 02:35:02 -07003336 if (copy_from_user(&ts, utime, sizeof(ts)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 return -EFAULT;
Pierre Peifferc19384b2007-05-09 02:35:02 -07003338 if (!timespec_valid(&ts))
Thomas Gleixner9741ef962006-03-31 02:31:32 -08003339 return -EINVAL;
Pierre Peifferc19384b2007-05-09 02:35:02 -07003340
3341 t = timespec_to_ktime(ts);
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003342 if (cmd == FUTEX_WAIT)
Thomas Gleixner5a7780e2008-02-13 09:20:43 +01003343 t = ktime_add_safe(ktime_get(), t);
Pierre Peifferc19384b2007-05-09 02:35:02 -07003344 tp = &t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 }
3346 /*
Darren Hart52400ba2009-04-03 13:40:49 -07003347 * requeue parameter in 'utime' if cmd == FUTEX_*_REQUEUE_*.
Andreas Schwabf54f0982007-07-31 00:38:51 -07003348 * number of waiters to wake in 'utime' if cmd == FUTEX_WAKE_OP.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 */
Andreas Schwabf54f0982007-07-31 00:38:51 -07003350 if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE ||
Darren Hartba9c22f2009-04-20 22:22:22 -07003351 cmd == FUTEX_CMP_REQUEUE_PI || cmd == FUTEX_WAKE_OP)
Ingo Molnare2970f22006-06-27 02:54:47 -07003352 val2 = (u32) (unsigned long) utime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353
Pierre Peifferc19384b2007-05-09 02:35:02 -07003354 return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355}
3356
Heiko Carstens03b8c7b2014-03-02 13:09:47 +01003357static void __init futex_detect_cmpxchg(void)
3358{
3359#ifndef CONFIG_HAVE_FUTEX_CMPXCHG
3360 u32 curval;
3361
3362 /*
3363 * This will fail and we want it. Some arch implementations do
3364 * runtime detection of the futex_atomic_cmpxchg_inatomic()
3365 * functionality. We want to know that before we call in any
3366 * of the complex code paths. Also we want to prevent
3367 * registration of robust lists in that case. NULL is
3368 * guaranteed to fault and we get -EFAULT on functional
3369 * implementation, the non-functional ones will return
3370 * -ENOSYS.
3371 */
3372 if (cmpxchg_futex_value_locked(&curval, NULL, 0, 0) == -EFAULT)
3373 futex_cmpxchg_enabled = 1;
3374#endif
3375}
3376
Benjamin Herrenschmidtf6d107f2008-03-27 14:52:15 +11003377static int __init futex_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378{
Heiko Carstens63b1a812014-01-16 14:54:50 +01003379 unsigned int futex_shift;
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -08003380 unsigned long i;
3381
3382#if CONFIG_BASE_SMALL
3383 futex_hashsize = 16;
3384#else
3385 futex_hashsize = roundup_pow_of_two(256 * num_possible_cpus());
3386#endif
3387
3388 futex_queues = alloc_large_system_hash("futex", sizeof(*futex_queues),
3389 futex_hashsize, 0,
3390 futex_hashsize < 256 ? HASH_SMALL : 0,
Heiko Carstens63b1a812014-01-16 14:54:50 +01003391 &futex_shift, NULL,
3392 futex_hashsize, futex_hashsize);
3393 futex_hashsize = 1UL << futex_shift;
Heiko Carstens03b8c7b2014-03-02 13:09:47 +01003394
3395 futex_detect_cmpxchg();
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08003396
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -08003397 for (i = 0; i < futex_hashsize; i++) {
Linus Torvalds11d46162014-03-20 22:11:17 -07003398 atomic_set(&futex_queues[i].waiters, 0);
Dima Zavin732375c2011-07-07 17:27:59 -07003399 plist_head_init(&futex_queues[i].chain);
Thomas Gleixner3e4ab742008-02-23 15:23:55 -08003400 spin_lock_init(&futex_queues[i].lock);
3401 }
3402
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 return 0;
3404}
Yang Yang808de342016-12-30 16:17:55 +08003405core_initcall(futex_init);