blob: f00a693bb5f13e3e45331e54df2503a3a65362d6 [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{
Thomas Gleixner95c53832020-03-08 19:07:17 +0100393 u32 hash = jhash2((u32 *)key, offsetof(typeof(*key), both.offset) / 4,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 key->both.offset);
Thomas Gleixner95c53832020-03-08 19:07:17 +0100395
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
Thomas Gleixner1b573632021-02-03 13:45:39 +0000991 * TID out of sync. Except one error case where the kernel is denied
992 * write access to the user address, see fixup_pi_state_owner().
Thomas Gleixner54a21782014-06-03 12:27:08 +0000993 */
Thomas Gleixnere60cbc52014-06-11 20:45:39 +0000994
995/*
996 * Validate that the existing waiter has a pi_state and sanity check
997 * the pi_state against the user space value. If correct, attach to
998 * it.
999 */
1000static int attach_to_pi_state(u32 uval, struct futex_pi_state *pi_state,
1001 struct futex_pi_state **ps)
1002{
1003 pid_t pid = uval & FUTEX_TID_MASK;
1004
1005 /*
1006 * Userspace might have messed up non-PI and PI futexes [3]
1007 */
1008 if (unlikely(!pi_state))
1009 return -EINVAL;
1010
1011 WARN_ON(!atomic_read(&pi_state->refcount));
1012
1013 /*
1014 * Handle the owner died case:
1015 */
1016 if (uval & FUTEX_OWNER_DIED) {
1017 /*
1018 * exit_pi_state_list sets owner to NULL and wakes the
1019 * topmost waiter. The task which acquires the
1020 * pi_state->rt_mutex will fixup owner.
1021 */
1022 if (!pi_state->owner) {
1023 /*
1024 * No pi state owner, but the user space TID
1025 * is not 0. Inconsistent state. [5]
1026 */
1027 if (pid)
1028 return -EINVAL;
1029 /*
1030 * Take a ref on the state and return success. [4]
1031 */
1032 goto out_state;
1033 }
1034
1035 /*
1036 * If TID is 0, then either the dying owner has not
1037 * yet executed exit_pi_state_list() or some waiter
1038 * acquired the rtmutex in the pi state, but did not
1039 * yet fixup the TID in user space.
1040 *
1041 * Take a ref on the state and return success. [6]
1042 */
1043 if (!pid)
1044 goto out_state;
1045 } else {
1046 /*
1047 * If the owner died bit is not set, then the pi_state
1048 * must have an owner. [7]
1049 */
1050 if (!pi_state->owner)
1051 return -EINVAL;
1052 }
1053
1054 /*
1055 * Bail out if user space manipulated the futex value. If pi
1056 * state exists then the owner TID must be the same as the
1057 * user space TID. [9/10]
1058 */
1059 if (pid != task_pid_vnr(pi_state->owner))
1060 return -EINVAL;
1061out_state:
1062 atomic_inc(&pi_state->refcount);
1063 *ps = pi_state;
1064 return 0;
1065}
1066
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001067/*
1068 * Lookup the task for the TID provided from user space and attach to
1069 * it after doing proper sanity checks.
1070 */
1071static int attach_to_pi_owner(u32 uval, union futex_key *key,
1072 struct futex_pi_state **ps)
Ingo Molnarc87e2832006-06-27 02:54:58 -07001073{
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001074 pid_t pid = uval & FUTEX_TID_MASK;
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001075 struct futex_pi_state *pi_state;
1076 struct task_struct *p;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001077
1078 /*
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001079 * We are the first waiter - try to look up the real owner and attach
Thomas Gleixner54a21782014-06-03 12:27:08 +00001080 * the new pi_state to it, but bail out when TID = 0 [1]
Ingo Molnarc87e2832006-06-27 02:54:58 -07001081 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001082 if (!pid)
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001083 return -ESRCH;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001084 p = futex_find_get_task(pid);
Michal Hocko7a0ea092010-06-30 09:51:19 +02001085 if (!p)
1086 return -ESRCH;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001087
Oleg Nesterova2129462015-02-02 15:05:36 +01001088 if (unlikely(p->flags & PF_KTHREAD)) {
Thomas Gleixnerf0d71b32014-05-12 20:45:35 +00001089 put_task_struct(p);
1090 return -EPERM;
1091 }
1092
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001093 /*
1094 * We need to look at the task state flags to figure out,
1095 * whether the task is exiting. To protect against the do_exit
1096 * change of the task flags, we do this protected by
1097 * p->pi_lock:
1098 */
Thomas Gleixner1d615482009-11-17 14:54:03 +01001099 raw_spin_lock_irq(&p->pi_lock);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001100 if (unlikely(p->flags & PF_EXITING)) {
1101 /*
1102 * The task is on the way out. When PF_EXITPIDONE is
1103 * set, we know that the task has finished the
1104 * cleanup:
1105 */
1106 int ret = (p->flags & PF_EXITPIDONE) ? -ESRCH : -EAGAIN;
1107
Thomas Gleixner1d615482009-11-17 14:54:03 +01001108 raw_spin_unlock_irq(&p->pi_lock);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001109 put_task_struct(p);
1110 return ret;
1111 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001112
Thomas Gleixner54a21782014-06-03 12:27:08 +00001113 /*
1114 * No existing pi state. First waiter. [2]
1115 */
Ingo Molnarc87e2832006-06-27 02:54:58 -07001116 pi_state = alloc_pi_state();
1117
1118 /*
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001119 * Initialize the pi_mutex in locked state and make @p
Ingo Molnarc87e2832006-06-27 02:54:58 -07001120 * the owner of it:
1121 */
1122 rt_mutex_init_proxy_locked(&pi_state->pi_mutex, p);
1123
1124 /* Store the key for possible exit cleanups: */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001125 pi_state->key = *key;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001126
Ingo Molnar627371d2006-07-29 05:16:20 +02001127 WARN_ON(!list_empty(&pi_state->list));
Ingo Molnarc87e2832006-06-27 02:54:58 -07001128 list_add(&pi_state->list, &p->pi_state_list);
1129 pi_state->owner = p;
Thomas Gleixner1d615482009-11-17 14:54:03 +01001130 raw_spin_unlock_irq(&p->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001131
1132 put_task_struct(p);
1133
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001134 *ps = pi_state;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001135
1136 return 0;
1137}
1138
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001139static int lookup_pi_state(u32 uval, struct futex_hash_bucket *hb,
1140 union futex_key *key, struct futex_pi_state **ps)
1141{
1142 struct futex_q *match = futex_top_waiter(hb, key);
1143
1144 /*
1145 * If there is a waiter on that futex, validate it and
1146 * attach to the pi_state when the validation succeeds.
1147 */
1148 if (match)
1149 return attach_to_pi_state(uval, match->pi_state, ps);
1150
1151 /*
1152 * We are the first waiter - try to look up the owner based on
1153 * @uval and attach to it.
1154 */
1155 return attach_to_pi_owner(uval, key, ps);
1156}
1157
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001158static int lock_pi_update_atomic(u32 __user *uaddr, u32 uval, u32 newval)
1159{
1160 u32 uninitialized_var(curval);
1161
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001162 if (unlikely(should_fail_futex(true)))
1163 return -EFAULT;
1164
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001165 if (unlikely(cmpxchg_futex_value_locked(&curval, uaddr, uval, newval)))
1166 return -EFAULT;
1167
1168 /*If user space value changed, let the caller retry */
1169 return curval != uval ? -EAGAIN : 0;
1170}
1171
Darren Hart1a520842009-04-03 13:39:52 -07001172/**
Darren Hartd96ee562009-09-21 22:30:22 -07001173 * futex_lock_pi_atomic() - Atomic work required to acquire a pi aware futex
Darren Hartbab5bc92009-04-07 23:23:50 -07001174 * @uaddr: the pi futex user address
1175 * @hb: the pi futex hash bucket
1176 * @key: the futex key associated with uaddr and hb
1177 * @ps: the pi_state pointer where we store the result of the
1178 * lookup
1179 * @task: the task to perform the atomic lock work for. This will
1180 * be "current" except in the case of requeue pi.
1181 * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0)
Darren Hart1a520842009-04-03 13:39:52 -07001182 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08001183 * Return:
1184 * 0 - ready to wait;
1185 * 1 - acquired the lock;
Darren Hart1a520842009-04-03 13:39:52 -07001186 * <0 - error
1187 *
1188 * The hb->lock and futex_key refs shall be held by the caller.
1189 */
1190static int futex_lock_pi_atomic(u32 __user *uaddr, struct futex_hash_bucket *hb,
1191 union futex_key *key,
1192 struct futex_pi_state **ps,
Darren Hartbab5bc92009-04-07 23:23:50 -07001193 struct task_struct *task, int set_waiters)
Darren Hart1a520842009-04-03 13:39:52 -07001194{
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001195 u32 uval, newval, vpid = task_pid_vnr(task);
1196 struct futex_q *match;
1197 int ret;
Darren Hart1a520842009-04-03 13:39:52 -07001198
1199 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001200 * Read the user space value first so we can validate a few
1201 * things before proceeding further.
Darren Hart1a520842009-04-03 13:39:52 -07001202 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001203 if (get_futex_value_locked(&uval, uaddr))
Darren Hart1a520842009-04-03 13:39:52 -07001204 return -EFAULT;
1205
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001206 if (unlikely(should_fail_futex(true)))
1207 return -EFAULT;
1208
Darren Hart1a520842009-04-03 13:39:52 -07001209 /*
1210 * Detect deadlocks.
1211 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001212 if ((unlikely((uval & FUTEX_TID_MASK) == vpid)))
Darren Hart1a520842009-04-03 13:39:52 -07001213 return -EDEADLK;
1214
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001215 if ((unlikely(should_fail_futex(true))))
1216 return -EDEADLK;
1217
Darren Hart1a520842009-04-03 13:39:52 -07001218 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001219 * Lookup existing state first. If it exists, try to attach to
1220 * its pi_state.
Darren Hart1a520842009-04-03 13:39:52 -07001221 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001222 match = futex_top_waiter(hb, key);
1223 if (match)
1224 return attach_to_pi_state(uval, match->pi_state, ps);
1225
1226 /*
1227 * No waiter and user TID is 0. We are here because the
1228 * waiters or the owner died bit is set or called from
1229 * requeue_cmp_pi or for whatever reason something took the
1230 * syscall.
1231 */
1232 if (!(uval & FUTEX_TID_MASK)) {
Thomas Gleixnerb3eaa9f2014-06-03 12:27:06 +00001233 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001234 * We take over the futex. No other waiters and the user space
1235 * TID is 0. We preserve the owner died bit.
Thomas Gleixnerb3eaa9f2014-06-03 12:27:06 +00001236 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001237 newval = uval & FUTEX_OWNER_DIED;
1238 newval |= vpid;
1239
1240 /* The futex requeue_pi code can enforce the waiters bit */
1241 if (set_waiters)
1242 newval |= FUTEX_WAITERS;
1243
1244 ret = lock_pi_update_atomic(uaddr, uval, newval);
1245 /* If the take over worked, return 1 */
1246 return ret < 0 ? ret : 1;
Thomas Gleixnerb3eaa9f2014-06-03 12:27:06 +00001247 }
Darren Hart1a520842009-04-03 13:39:52 -07001248
Darren Hart1a520842009-04-03 13:39:52 -07001249 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001250 * First waiter. Set the waiters bit before attaching ourself to
1251 * the owner. If owner tries to unlock, it will be forced into
1252 * the kernel and blocked on hb->lock.
Darren Hart1a520842009-04-03 13:39:52 -07001253 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001254 newval = uval | FUTEX_WAITERS;
1255 ret = lock_pi_update_atomic(uaddr, uval, newval);
1256 if (ret)
1257 return ret;
Darren Hart1a520842009-04-03 13:39:52 -07001258 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001259 * If the update of the user space value succeeded, we try to
1260 * attach to the owner. If that fails, no harm done, we only
1261 * set the FUTEX_WAITERS bit in the user space variable.
Darren Hart1a520842009-04-03 13:39:52 -07001262 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001263 return attach_to_pi_owner(uval, key, ps);
Darren Hart1a520842009-04-03 13:39:52 -07001264}
1265
Lai Jiangshan2e129782010-12-22 14:18:50 +08001266/**
1267 * __unqueue_futex() - Remove the futex_q from its futex_hash_bucket
1268 * @q: The futex_q to unqueue
1269 *
1270 * The q->lock_ptr must not be NULL and must be held by the caller.
1271 */
1272static void __unqueue_futex(struct futex_q *q)
1273{
1274 struct futex_hash_bucket *hb;
1275
Steven Rostedt29096202011-03-17 15:21:07 -04001276 if (WARN_ON_SMP(!q->lock_ptr || !spin_is_locked(q->lock_ptr))
1277 || WARN_ON(plist_node_empty(&q->list)))
Lai Jiangshan2e129782010-12-22 14:18:50 +08001278 return;
1279
1280 hb = container_of(q->lock_ptr, struct futex_hash_bucket, lock);
1281 plist_del(&q->list, &hb->chain);
Linus Torvalds11d46162014-03-20 22:11:17 -07001282 hb_waiters_dec(hb);
Lai Jiangshan2e129782010-12-22 14:18:50 +08001283}
1284
Ingo Molnarc87e2832006-06-27 02:54:58 -07001285/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 * The hash bucket lock must be held when this is called.
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001287 * Afterwards, the futex_q must not be accessed. Callers
1288 * must ensure to later call wake_up_q() for the actual
1289 * wakeups to occur.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 */
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001291static void mark_wake_futex(struct wake_q_head *wake_q, struct futex_q *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001293 struct task_struct *p = q->task;
1294
Darren Hartaa109902012-11-26 16:29:56 -08001295 if (WARN(q->pi_state || q->rt_waiter, "refusing to wake PI futex\n"))
1296 return;
1297
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001298 /*
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001299 * Queue the task for later wakeup for after we've released
1300 * the hb->lock. wake_q_add() grabs reference to p.
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001301 */
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001302 wake_q_add(wake_q, p);
Lai Jiangshan2e129782010-12-22 14:18:50 +08001303 __unqueue_futex(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 /*
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001305 * The waiting task can free the futex_q as soon as
1306 * q->lock_ptr = NULL is written, without taking any locks. A
1307 * memory barrier is required here to prevent the following
1308 * store to lock_ptr from getting ahead of the plist_del.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 */
Ralf Baechleccdea2f2006-12-06 20:40:26 -08001310 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 q->lock_ptr = NULL;
1312}
1313
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001314static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this,
1315 struct futex_hash_bucket *hb)
Ingo Molnarc87e2832006-06-27 02:54:58 -07001316{
1317 struct task_struct *new_owner;
1318 struct futex_pi_state *pi_state = this->pi_state;
Vitaliy Ivanov7cfdaf32011-07-07 15:10:31 +03001319 u32 uninitialized_var(curval), newval;
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001320 WAKE_Q(wake_q);
1321 bool deboost;
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001322 int ret = 0;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001323
1324 if (!pi_state)
1325 return -EINVAL;
1326
Thomas Gleixner51246bf2010-02-02 11:40:27 +01001327 /*
1328 * If current does not own the pi_state then the futex is
1329 * inconsistent and user space fiddled with the futex value.
1330 */
1331 if (pi_state->owner != current)
1332 return -EINVAL;
1333
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001334 raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001335 new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);
1336
1337 /*
Steven Rostedtf123c982011-01-06 15:08:29 -05001338 * It is possible that the next waiter (the one that brought
1339 * this owner to the kernel) timed out and is no longer
1340 * waiting on the lock.
Ingo Molnarc87e2832006-06-27 02:54:58 -07001341 */
1342 if (!new_owner)
1343 new_owner = this->task;
1344
1345 /*
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001346 * We pass it to the next owner. The WAITERS bit is always
1347 * kept enabled while there is PI state around. We cleanup the
1348 * owner died bit, because we are the owner.
Ingo Molnarc87e2832006-06-27 02:54:58 -07001349 */
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001350 newval = FUTEX_WAITERS | task_pid_vnr(new_owner);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001351
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001352 if (unlikely(should_fail_futex(true)))
1353 ret = -EFAULT;
1354
Sebastian Andrzej Siewior89e9e662016-04-15 14:35:39 +02001355 if (cmpxchg_futex_value_locked(&curval, uaddr, uval, newval)) {
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001356 ret = -EFAULT;
Sebastian Andrzej Siewior89e9e662016-04-15 14:35:39 +02001357 } else if (curval != uval) {
1358 /*
1359 * If a unconditional UNLOCK_PI operation (user space did not
1360 * try the TID->0 transition) raced with a waiter setting the
1361 * FUTEX_WAITERS flag between get_user() and locking the hash
1362 * bucket lock, retry the operation.
1363 */
1364 if ((FUTEX_TID_MASK & curval) == uval)
1365 ret = -EAGAIN;
1366 else
1367 ret = -EINVAL;
1368 }
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001369 if (ret) {
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001370 raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001371 return ret;
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001372 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001373
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001374 raw_spin_lock(&pi_state->owner->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +02001375 WARN_ON(list_empty(&pi_state->list));
1376 list_del_init(&pi_state->list);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001377 raw_spin_unlock(&pi_state->owner->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +02001378
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001379 raw_spin_lock(&new_owner->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +02001380 WARN_ON(!list_empty(&pi_state->list));
Ingo Molnarc87e2832006-06-27 02:54:58 -07001381 list_add(&pi_state->list, &new_owner->pi_state_list);
1382 pi_state->owner = new_owner;
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001383 raw_spin_unlock(&new_owner->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +02001384
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01001385 raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001386
1387 deboost = rt_mutex_futex_unlock(&pi_state->pi_mutex, &wake_q);
1388
1389 /*
1390 * First unlock HB so the waiter does not spin on it once he got woken
1391 * up. Second wake up the waiter before the priority is adjusted. If we
1392 * deboost first (and lose our higher priority), then the task might get
1393 * scheduled away before the wake up can take place.
1394 */
1395 spin_unlock(&hb->lock);
1396 wake_up_q(&wake_q);
1397 if (deboost)
1398 rt_mutex_adjust_prio(current);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001399
1400 return 0;
1401}
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403/*
Ingo Molnar8b8f3192006-07-03 00:25:05 -07001404 * Express the locking dependencies for lockdep:
1405 */
1406static inline void
1407double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
1408{
1409 if (hb1 <= hb2) {
1410 spin_lock(&hb1->lock);
1411 if (hb1 < hb2)
1412 spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING);
1413 } else { /* hb1 > hb2 */
1414 spin_lock(&hb2->lock);
1415 spin_lock_nested(&hb1->lock, SINGLE_DEPTH_NESTING);
1416 }
1417}
1418
Darren Hart5eb3dc62009-03-12 00:55:52 -07001419static inline void
1420double_unlock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
1421{
Darren Hartf061d352009-03-12 15:11:18 -07001422 spin_unlock(&hb1->lock);
Ingo Molnar88f502f2009-03-13 10:32:07 +01001423 if (hb1 != hb2)
1424 spin_unlock(&hb2->lock);
Darren Hart5eb3dc62009-03-12 00:55:52 -07001425}
1426
Ingo Molnar8b8f3192006-07-03 00:25:05 -07001427/*
Darren Hartb2d09942009-03-12 00:55:37 -07001428 * Wake up waiters matching bitset queued on this futex (uaddr).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 */
Darren Hartb41277d2010-11-08 13:10:09 -08001430static int
1431futex_wake(u32 __user *uaddr, unsigned int flags, int nr_wake, u32 bitset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
Ingo Molnare2970f22006-06-27 02:54:47 -07001433 struct futex_hash_bucket *hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 struct futex_q *this, *next;
Peter Zijlstra38d47c12008-09-26 19:32:20 +02001435 union futex_key key = FUTEX_KEY_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 int ret;
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001437 WAKE_Q(wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Thomas Gleixnercd689982008-02-01 17:45:14 +01001439 if (!bitset)
1440 return -EINVAL;
1441
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001442 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 if (unlikely(ret != 0))
1444 goto out;
1445
Ingo Molnare2970f22006-06-27 02:54:47 -07001446 hb = hash_futex(&key);
Davidlohr Buesob0c29f72014-01-12 15:31:25 -08001447
1448 /* Make sure we really have tasks to wakeup */
1449 if (!hb_waiters_pending(hb))
1450 goto out_put_key;
1451
Ingo Molnare2970f22006-06-27 02:54:47 -07001452 spin_lock(&hb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Jason Low0d00c7b2014-01-12 15:31:22 -08001454 plist_for_each_entry_safe(this, next, &hb->chain, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 if (match_futex (&this->key, &key)) {
Darren Hart52400ba2009-04-03 13:40:49 -07001456 if (this->pi_state || this->rt_waiter) {
Ingo Molnared6f7b12006-07-01 04:35:46 -07001457 ret = -EINVAL;
1458 break;
1459 }
Thomas Gleixnercd689982008-02-01 17:45:14 +01001460
1461 /* Check if one of the bits is set in both bitsets */
1462 if (!(this->bitset & bitset))
1463 continue;
1464
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001465 mark_wake_futex(&wake_q, this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 if (++ret >= nr_wake)
1467 break;
1468 }
1469 }
1470
Ingo Molnare2970f22006-06-27 02:54:47 -07001471 spin_unlock(&hb->lock);
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001472 wake_up_q(&wake_q);
Davidlohr Buesob0c29f72014-01-12 15:31:25 -08001473out_put_key:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001474 put_futex_key(&key);
Darren Hart42d35d42008-12-29 15:49:53 -08001475out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 return ret;
1477}
1478
Jiri Slaby81da9f82017-08-24 09:31:05 +02001479static int futex_atomic_op_inuser(unsigned int encoded_op, u32 __user *uaddr)
1480{
1481 unsigned int op = (encoded_op & 0x70000000) >> 28;
1482 unsigned int cmp = (encoded_op & 0x0f000000) >> 24;
Jiri Slabya1640092017-11-30 15:35:44 +01001483 int oparg = sign_extend32((encoded_op & 0x00fff000) >> 12, 11);
1484 int cmparg = sign_extend32(encoded_op & 0x00000fff, 11);
Jiri Slaby81da9f82017-08-24 09:31:05 +02001485 int oldval, ret;
1486
1487 if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28)) {
Jiri Slabyda92e8a2017-10-23 13:41:51 +02001488 if (oparg < 0 || oparg > 31) {
1489 char comm[sizeof(current->comm)];
1490 /*
1491 * kill this print and return -EINVAL when userspace
1492 * is sane again
1493 */
1494 pr_info_ratelimited("futex_wake_op: %s tries to shift op by %d; fix this program\n",
1495 get_task_comm(comm, current), oparg);
1496 oparg &= 31;
1497 }
Jiri Slaby81da9f82017-08-24 09:31:05 +02001498 oparg = 1 << oparg;
1499 }
1500
1501 if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
1502 return -EFAULT;
1503
1504 ret = arch_futex_atomic_op_inuser(op, oparg, &oldval, uaddr);
1505 if (ret)
1506 return ret;
1507
1508 switch (cmp) {
1509 case FUTEX_OP_CMP_EQ:
1510 return oldval == cmparg;
1511 case FUTEX_OP_CMP_NE:
1512 return oldval != cmparg;
1513 case FUTEX_OP_CMP_LT:
1514 return oldval < cmparg;
1515 case FUTEX_OP_CMP_GE:
1516 return oldval >= cmparg;
1517 case FUTEX_OP_CMP_LE:
1518 return oldval <= cmparg;
1519 case FUTEX_OP_CMP_GT:
1520 return oldval > cmparg;
1521 default:
1522 return -ENOSYS;
1523 }
1524}
1525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526/*
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001527 * Wake up all waiters hashed on the physical page that is mapped
1528 * to this virtual address:
1529 */
Ingo Molnare2970f22006-06-27 02:54:47 -07001530static int
Darren Hartb41277d2010-11-08 13:10:09 -08001531futex_wake_op(u32 __user *uaddr1, unsigned int flags, u32 __user *uaddr2,
Ingo Molnare2970f22006-06-27 02:54:47 -07001532 int nr_wake, int nr_wake2, int op)
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001533{
Peter Zijlstra38d47c12008-09-26 19:32:20 +02001534 union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
Ingo Molnare2970f22006-06-27 02:54:47 -07001535 struct futex_hash_bucket *hb1, *hb2;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001536 struct futex_q *this, *next;
Darren Harte4dc5b72009-03-12 00:56:13 -07001537 int ret, op_ret;
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001538 WAKE_Q(wake_q);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001539
Darren Harte4dc5b72009-03-12 00:56:13 -07001540retry:
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001541 ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, VERIFY_READ);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001542 if (unlikely(ret != 0))
1543 goto out;
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001544 ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001545 if (unlikely(ret != 0))
Darren Hart42d35d42008-12-29 15:49:53 -08001546 goto out_put_key1;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001547
Ingo Molnare2970f22006-06-27 02:54:47 -07001548 hb1 = hash_futex(&key1);
1549 hb2 = hash_futex(&key2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001550
Darren Harte4dc5b72009-03-12 00:56:13 -07001551retry_private:
Thomas Gleixnereaaea802009-10-04 09:34:17 +02001552 double_lock_hb(hb1, hb2);
Ingo Molnare2970f22006-06-27 02:54:47 -07001553 op_ret = futex_atomic_op_inuser(op, uaddr2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001554 if (unlikely(op_ret < 0)) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001555
Darren Hart5eb3dc62009-03-12 00:55:52 -07001556 double_unlock_hb(hb1, hb2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001557
David Howells7ee1dd32006-01-06 00:11:44 -08001558#ifndef CONFIG_MMU
Ingo Molnare2970f22006-06-27 02:54:47 -07001559 /*
1560 * we don't get EFAULT from MMU faults if we don't have an MMU,
1561 * but we might get them from range checking
1562 */
David Howells7ee1dd32006-01-06 00:11:44 -08001563 ret = op_ret;
Darren Hart42d35d42008-12-29 15:49:53 -08001564 goto out_put_keys;
David Howells7ee1dd32006-01-06 00:11:44 -08001565#endif
1566
David Gibson796f8d92005-11-07 00:59:33 -08001567 if (unlikely(op_ret != -EFAULT)) {
1568 ret = op_ret;
Darren Hart42d35d42008-12-29 15:49:53 -08001569 goto out_put_keys;
David Gibson796f8d92005-11-07 00:59:33 -08001570 }
1571
Thomas Gleixnerd0725992009-06-11 23:15:43 +02001572 ret = fault_in_user_writeable(uaddr2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001573 if (ret)
Darren Hartde87fcc2009-03-12 00:55:46 -07001574 goto out_put_keys;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001575
Darren Hartb41277d2010-11-08 13:10:09 -08001576 if (!(flags & FLAGS_SHARED))
Darren Harte4dc5b72009-03-12 00:56:13 -07001577 goto retry_private;
1578
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001579 put_futex_key(&key2);
1580 put_futex_key(&key1);
Darren Harte4dc5b72009-03-12 00:56:13 -07001581 goto retry;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001582 }
1583
Jason Low0d00c7b2014-01-12 15:31:22 -08001584 plist_for_each_entry_safe(this, next, &hb1->chain, list) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001585 if (match_futex (&this->key, &key1)) {
Darren Hartaa109902012-11-26 16:29:56 -08001586 if (this->pi_state || this->rt_waiter) {
1587 ret = -EINVAL;
1588 goto out_unlock;
1589 }
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001590 mark_wake_futex(&wake_q, this);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001591 if (++ret >= nr_wake)
1592 break;
1593 }
1594 }
1595
1596 if (op_ret > 0) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001597 op_ret = 0;
Jason Low0d00c7b2014-01-12 15:31:22 -08001598 plist_for_each_entry_safe(this, next, &hb2->chain, list) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001599 if (match_futex (&this->key, &key2)) {
Darren Hartaa109902012-11-26 16:29:56 -08001600 if (this->pi_state || this->rt_waiter) {
1601 ret = -EINVAL;
1602 goto out_unlock;
1603 }
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001604 mark_wake_futex(&wake_q, this);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001605 if (++op_ret >= nr_wake2)
1606 break;
1607 }
1608 }
1609 ret += op_ret;
1610 }
1611
Darren Hartaa109902012-11-26 16:29:56 -08001612out_unlock:
Darren Hart5eb3dc62009-03-12 00:55:52 -07001613 double_unlock_hb(hb1, hb2);
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001614 wake_up_q(&wake_q);
Darren Hart42d35d42008-12-29 15:49:53 -08001615out_put_keys:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001616 put_futex_key(&key2);
Darren Hart42d35d42008-12-29 15:49:53 -08001617out_put_key1:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001618 put_futex_key(&key1);
Darren Hart42d35d42008-12-29 15:49:53 -08001619out:
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001620 return ret;
1621}
1622
Darren Hart9121e472009-04-03 13:40:31 -07001623/**
1624 * requeue_futex() - Requeue a futex_q from one hb to another
1625 * @q: the futex_q to requeue
1626 * @hb1: the source hash_bucket
1627 * @hb2: the target hash_bucket
1628 * @key2: the new key for the requeued futex_q
1629 */
1630static inline
1631void requeue_futex(struct futex_q *q, struct futex_hash_bucket *hb1,
1632 struct futex_hash_bucket *hb2, union futex_key *key2)
1633{
1634
1635 /*
1636 * If key1 and key2 hash to the same bucket, no need to
1637 * requeue.
1638 */
1639 if (likely(&hb1->chain != &hb2->chain)) {
1640 plist_del(&q->list, &hb1->chain);
Linus Torvalds11d46162014-03-20 22:11:17 -07001641 hb_waiters_dec(hb1);
Linus Torvalds11d46162014-03-20 22:11:17 -07001642 hb_waiters_inc(hb2);
Davidlohr Buesofe1bce92016-04-20 20:09:24 -07001643 plist_add(&q->list, &hb2->chain);
Darren Hart9121e472009-04-03 13:40:31 -07001644 q->lock_ptr = &hb2->lock;
Darren Hart9121e472009-04-03 13:40:31 -07001645 }
1646 get_futex_key_refs(key2);
1647 q->key = *key2;
1648}
1649
Darren Hart52400ba2009-04-03 13:40:49 -07001650/**
1651 * requeue_pi_wake_futex() - Wake a task that acquired the lock during requeue
Darren Hartd96ee562009-09-21 22:30:22 -07001652 * @q: the futex_q
1653 * @key: the key of the requeue target futex
1654 * @hb: the hash_bucket of the requeue target futex
Darren Hart52400ba2009-04-03 13:40:49 -07001655 *
1656 * During futex_requeue, with requeue_pi=1, it is possible to acquire the
1657 * target futex if it is uncontended or via a lock steal. Set the futex_q key
1658 * to the requeue target futex so the waiter can detect the wakeup on the right
1659 * futex, but remove it from the hb and NULL the rt_waiter so it can detect
Darren Hartbeda2c72009-08-09 15:34:39 -07001660 * atomic lock acquisition. Set the q->lock_ptr to the requeue target hb->lock
1661 * to protect access to the pi_state to fixup the owner later. Must be called
1662 * with both q->lock_ptr and hb->lock held.
Darren Hart52400ba2009-04-03 13:40:49 -07001663 */
1664static inline
Darren Hartbeda2c72009-08-09 15:34:39 -07001665void requeue_pi_wake_futex(struct futex_q *q, union futex_key *key,
1666 struct futex_hash_bucket *hb)
Darren Hart52400ba2009-04-03 13:40:49 -07001667{
Darren Hart52400ba2009-04-03 13:40:49 -07001668 get_futex_key_refs(key);
1669 q->key = *key;
1670
Lai Jiangshan2e129782010-12-22 14:18:50 +08001671 __unqueue_futex(q);
Darren Hart52400ba2009-04-03 13:40:49 -07001672
1673 WARN_ON(!q->rt_waiter);
1674 q->rt_waiter = NULL;
1675
Darren Hartbeda2c72009-08-09 15:34:39 -07001676 q->lock_ptr = &hb->lock;
Darren Hartbeda2c72009-08-09 15:34:39 -07001677
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001678 wake_up_state(q->task, TASK_NORMAL);
Darren Hart52400ba2009-04-03 13:40:49 -07001679}
1680
1681/**
1682 * futex_proxy_trylock_atomic() - Attempt an atomic lock for the top waiter
Darren Hartbab5bc92009-04-07 23:23:50 -07001683 * @pifutex: the user address of the to futex
1684 * @hb1: the from futex hash bucket, must be locked by the caller
1685 * @hb2: the to futex hash bucket, must be locked by the caller
1686 * @key1: the from futex key
1687 * @key2: the to futex key
1688 * @ps: address to store the pi_state pointer
1689 * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0)
Darren Hart52400ba2009-04-03 13:40:49 -07001690 *
1691 * Try and get the lock on behalf of the top waiter if we can do it atomically.
Darren Hartbab5bc92009-04-07 23:23:50 -07001692 * Wake the top waiter if we succeed. If the caller specified set_waiters,
1693 * then direct futex_lock_pi_atomic() to force setting the FUTEX_WAITERS bit.
1694 * hb1 and hb2 must be held by the caller.
Darren Hart52400ba2009-04-03 13:40:49 -07001695 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08001696 * Return:
1697 * 0 - failed to acquire the lock atomically;
Thomas Gleixner866293e2014-05-12 20:45:34 +00001698 * >0 - acquired the lock, return value is vpid of the top_waiter
Darren Hart52400ba2009-04-03 13:40:49 -07001699 * <0 - error
1700 */
1701static int futex_proxy_trylock_atomic(u32 __user *pifutex,
1702 struct futex_hash_bucket *hb1,
1703 struct futex_hash_bucket *hb2,
1704 union futex_key *key1, union futex_key *key2,
Darren Hartbab5bc92009-04-07 23:23:50 -07001705 struct futex_pi_state **ps, int set_waiters)
Darren Hart52400ba2009-04-03 13:40:49 -07001706{
Darren Hartbab5bc92009-04-07 23:23:50 -07001707 struct futex_q *top_waiter = NULL;
Darren Hart52400ba2009-04-03 13:40:49 -07001708 u32 curval;
Thomas Gleixner866293e2014-05-12 20:45:34 +00001709 int ret, vpid;
Darren Hart52400ba2009-04-03 13:40:49 -07001710
1711 if (get_futex_value_locked(&curval, pifutex))
1712 return -EFAULT;
1713
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001714 if (unlikely(should_fail_futex(true)))
1715 return -EFAULT;
1716
Darren Hartbab5bc92009-04-07 23:23:50 -07001717 /*
1718 * Find the top_waiter and determine if there are additional waiters.
1719 * If the caller intends to requeue more than 1 waiter to pifutex,
1720 * force futex_lock_pi_atomic() to set the FUTEX_WAITERS bit now,
1721 * as we have means to handle the possible fault. If not, don't set
1722 * the bit unecessarily as it will force the subsequent unlock to enter
1723 * the kernel.
1724 */
Darren Hart52400ba2009-04-03 13:40:49 -07001725 top_waiter = futex_top_waiter(hb1, key1);
1726
1727 /* There are no waiters, nothing for us to do. */
1728 if (!top_waiter)
1729 return 0;
1730
Darren Hart84bc4af2009-08-13 17:36:53 -07001731 /* Ensure we requeue to the expected futex. */
1732 if (!match_futex(top_waiter->requeue_pi_key, key2))
1733 return -EINVAL;
1734
Darren Hart52400ba2009-04-03 13:40:49 -07001735 /*
Darren Hartbab5bc92009-04-07 23:23:50 -07001736 * Try to take the lock for top_waiter. Set the FUTEX_WAITERS bit in
1737 * the contended case or if set_waiters is 1. The pi_state is returned
1738 * in ps in contended cases.
Darren Hart52400ba2009-04-03 13:40:49 -07001739 */
Thomas Gleixner866293e2014-05-12 20:45:34 +00001740 vpid = task_pid_vnr(top_waiter->task);
Darren Hartbab5bc92009-04-07 23:23:50 -07001741 ret = futex_lock_pi_atomic(pifutex, hb2, key2, ps, top_waiter->task,
1742 set_waiters);
Thomas Gleixner866293e2014-05-12 20:45:34 +00001743 if (ret == 1) {
Darren Hartbeda2c72009-08-09 15:34:39 -07001744 requeue_pi_wake_futex(top_waiter, key2, hb2);
Thomas Gleixner866293e2014-05-12 20:45:34 +00001745 return vpid;
1746 }
Darren Hart52400ba2009-04-03 13:40:49 -07001747 return ret;
1748}
1749
1750/**
1751 * futex_requeue() - Requeue waiters from uaddr1 to uaddr2
Randy Dunlapfb62db22010-10-13 11:02:34 -07001752 * @uaddr1: source futex user address
Darren Hartb41277d2010-11-08 13:10:09 -08001753 * @flags: futex flags (FLAGS_SHARED, etc.)
Randy Dunlapfb62db22010-10-13 11:02:34 -07001754 * @uaddr2: target futex user address
1755 * @nr_wake: number of waiters to wake (must be 1 for requeue_pi)
1756 * @nr_requeue: number of waiters to requeue (0-INT_MAX)
1757 * @cmpval: @uaddr1 expected value (or %NULL)
1758 * @requeue_pi: if we are attempting to requeue from a non-pi futex to a
Darren Hartb41277d2010-11-08 13:10:09 -08001759 * pi futex (pi to pi requeue is not supported)
Darren Hart52400ba2009-04-03 13:40:49 -07001760 *
1761 * Requeue waiters on uaddr1 to uaddr2. In the requeue_pi case, try to acquire
1762 * uaddr2 atomically on behalf of the top waiter.
1763 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08001764 * Return:
1765 * >=0 - on success, the number of tasks requeued or woken;
Darren Hart52400ba2009-04-03 13:40:49 -07001766 * <0 - on error
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 */
Darren Hartb41277d2010-11-08 13:10:09 -08001768static int futex_requeue(u32 __user *uaddr1, unsigned int flags,
1769 u32 __user *uaddr2, int nr_wake, int nr_requeue,
1770 u32 *cmpval, int requeue_pi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771{
Peter Zijlstra38d47c12008-09-26 19:32:20 +02001772 union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
Darren Hart52400ba2009-04-03 13:40:49 -07001773 int drop_count = 0, task_count = 0, ret;
1774 struct futex_pi_state *pi_state = NULL;
Ingo Molnare2970f22006-06-27 02:54:47 -07001775 struct futex_hash_bucket *hb1, *hb2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 struct futex_q *this, *next;
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001777 WAKE_Q(wake_q);
Darren Hart52400ba2009-04-03 13:40:49 -07001778
Li Jinyued8a31702017-12-14 17:04:54 +08001779 if (nr_wake < 0 || nr_requeue < 0)
1780 return -EINVAL;
1781
Darren Hart52400ba2009-04-03 13:40:49 -07001782 if (requeue_pi) {
1783 /*
Thomas Gleixnere9c243a2014-06-03 12:27:06 +00001784 * Requeue PI only works on two distinct uaddrs. This
1785 * check is only valid for private futexes. See below.
1786 */
1787 if (uaddr1 == uaddr2)
1788 return -EINVAL;
1789
1790 /*
Darren Hart52400ba2009-04-03 13:40:49 -07001791 * requeue_pi requires a pi_state, try to allocate it now
1792 * without any locks in case it fails.
1793 */
1794 if (refill_pi_state_cache())
1795 return -ENOMEM;
1796 /*
1797 * requeue_pi must wake as many tasks as it can, up to nr_wake
1798 * + nr_requeue, since it acquires the rt_mutex prior to
1799 * returning to userspace, so as to not leave the rt_mutex with
1800 * waiters and no owner. However, second and third wake-ups
1801 * cannot be predicted as they involve race conditions with the
1802 * first wake and a fault while looking up the pi_state. Both
1803 * pthread_cond_signal() and pthread_cond_broadcast() should
1804 * use nr_wake=1.
1805 */
1806 if (nr_wake != 1)
1807 return -EINVAL;
1808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Darren Hart42d35d42008-12-29 15:49:53 -08001810retry:
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001811 ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 if (unlikely(ret != 0))
1813 goto out;
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001814 ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2,
1815 requeue_pi ? VERIFY_WRITE : VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 if (unlikely(ret != 0))
Darren Hart42d35d42008-12-29 15:49:53 -08001817 goto out_put_key1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Thomas Gleixnere9c243a2014-06-03 12:27:06 +00001819 /*
1820 * The check above which compares uaddrs is not sufficient for
1821 * shared futexes. We need to compare the keys:
1822 */
1823 if (requeue_pi && match_futex(&key1, &key2)) {
1824 ret = -EINVAL;
1825 goto out_put_keys;
1826 }
1827
Ingo Molnare2970f22006-06-27 02:54:47 -07001828 hb1 = hash_futex(&key1);
1829 hb2 = hash_futex(&key2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Darren Harte4dc5b72009-03-12 00:56:13 -07001831retry_private:
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07001832 hb_waiters_inc(hb2);
Ingo Molnar8b8f3192006-07-03 00:25:05 -07001833 double_lock_hb(hb1, hb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Ingo Molnare2970f22006-06-27 02:54:47 -07001835 if (likely(cmpval != NULL)) {
1836 u32 curval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Ingo Molnare2970f22006-06-27 02:54:47 -07001838 ret = get_futex_value_locked(&curval, uaddr1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
1840 if (unlikely(ret)) {
Darren Hart5eb3dc62009-03-12 00:55:52 -07001841 double_unlock_hb(hb1, hb2);
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07001842 hb_waiters_dec(hb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Darren Harte4dc5b72009-03-12 00:56:13 -07001844 ret = get_user(curval, uaddr1);
1845 if (ret)
1846 goto out_put_keys;
1847
Darren Hartb41277d2010-11-08 13:10:09 -08001848 if (!(flags & FLAGS_SHARED))
Darren Harte4dc5b72009-03-12 00:56:13 -07001849 goto retry_private;
1850
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001851 put_futex_key(&key2);
1852 put_futex_key(&key1);
Darren Harte4dc5b72009-03-12 00:56:13 -07001853 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 }
Ingo Molnare2970f22006-06-27 02:54:47 -07001855 if (curval != *cmpval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 ret = -EAGAIN;
1857 goto out_unlock;
1858 }
1859 }
1860
Darren Hart52400ba2009-04-03 13:40:49 -07001861 if (requeue_pi && (task_count - nr_wake < nr_requeue)) {
Darren Hartbab5bc92009-04-07 23:23:50 -07001862 /*
1863 * Attempt to acquire uaddr2 and wake the top waiter. If we
1864 * intend to requeue waiters, force setting the FUTEX_WAITERS
1865 * bit. We force this here where we are able to easily handle
1866 * faults rather in the requeue loop below.
1867 */
Darren Hart52400ba2009-04-03 13:40:49 -07001868 ret = futex_proxy_trylock_atomic(uaddr2, hb1, hb2, &key1,
Darren Hartbab5bc92009-04-07 23:23:50 -07001869 &key2, &pi_state, nr_requeue);
Darren Hart52400ba2009-04-03 13:40:49 -07001870
1871 /*
1872 * At this point the top_waiter has either taken uaddr2 or is
1873 * waiting on it. If the former, then the pi_state will not
1874 * exist yet, look it up one more time to ensure we have a
Thomas Gleixner866293e2014-05-12 20:45:34 +00001875 * reference to it. If the lock was taken, ret contains the
1876 * vpid of the top waiter task.
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00001877 * If the lock was not taken, we have pi_state and an initial
1878 * refcount on it. In case of an error we have nothing.
Darren Hart52400ba2009-04-03 13:40:49 -07001879 */
Thomas Gleixner866293e2014-05-12 20:45:34 +00001880 if (ret > 0) {
Darren Hart52400ba2009-04-03 13:40:49 -07001881 WARN_ON(pi_state);
Darren Hart89061d32009-10-15 15:30:48 -07001882 drop_count++;
Darren Hart52400ba2009-04-03 13:40:49 -07001883 task_count++;
Thomas Gleixner866293e2014-05-12 20:45:34 +00001884 /*
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00001885 * If we acquired the lock, then the user space value
1886 * of uaddr2 should be vpid. It cannot be changed by
1887 * the top waiter as it is blocked on hb2 lock if it
1888 * tries to do so. If something fiddled with it behind
1889 * our back the pi state lookup might unearth it. So
1890 * we rather use the known value than rereading and
1891 * handing potential crap to lookup_pi_state.
1892 *
1893 * If that call succeeds then we have pi_state and an
1894 * initial refcount on it.
Thomas Gleixner866293e2014-05-12 20:45:34 +00001895 */
Thomas Gleixner54a21782014-06-03 12:27:08 +00001896 ret = lookup_pi_state(ret, hb2, &key2, &pi_state);
Darren Hart52400ba2009-04-03 13:40:49 -07001897 }
1898
1899 switch (ret) {
1900 case 0:
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00001901 /* We hold a reference on the pi state. */
Darren Hart52400ba2009-04-03 13:40:49 -07001902 break;
Thomas Gleixner4959f2d2015-12-19 20:07:40 +00001903
1904 /* If the above failed, then pi_state is NULL */
Darren Hart52400ba2009-04-03 13:40:49 -07001905 case -EFAULT:
1906 double_unlock_hb(hb1, hb2);
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07001907 hb_waiters_dec(hb2);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001908 put_futex_key(&key2);
1909 put_futex_key(&key1);
Thomas Gleixnerd0725992009-06-11 23:15:43 +02001910 ret = fault_in_user_writeable(uaddr2);
Darren Hart52400ba2009-04-03 13:40:49 -07001911 if (!ret)
1912 goto retry;
1913 goto out;
1914 case -EAGAIN:
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001915 /*
1916 * Two reasons for this:
1917 * - Owner is exiting and we just wait for the
1918 * exit to complete.
1919 * - The user space value changed.
1920 */
Darren Hart52400ba2009-04-03 13:40:49 -07001921 double_unlock_hb(hb1, hb2);
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07001922 hb_waiters_dec(hb2);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001923 put_futex_key(&key2);
1924 put_futex_key(&key1);
Darren Hart52400ba2009-04-03 13:40:49 -07001925 cond_resched();
1926 goto retry;
1927 default:
1928 goto out_unlock;
1929 }
1930 }
1931
Jason Low0d00c7b2014-01-12 15:31:22 -08001932 plist_for_each_entry_safe(this, next, &hb1->chain, list) {
Darren Hart52400ba2009-04-03 13:40:49 -07001933 if (task_count - nr_wake >= nr_requeue)
1934 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Darren Hart52400ba2009-04-03 13:40:49 -07001936 if (!match_futex(&this->key, &key1))
1937 continue;
1938
Darren Hart392741e2009-08-07 15:20:48 -07001939 /*
1940 * FUTEX_WAIT_REQEUE_PI and FUTEX_CMP_REQUEUE_PI should always
1941 * be paired with each other and no other futex ops.
Darren Hartaa109902012-11-26 16:29:56 -08001942 *
1943 * We should never be requeueing a futex_q with a pi_state,
1944 * which is awaiting a futex_unlock_pi().
Darren Hart392741e2009-08-07 15:20:48 -07001945 */
1946 if ((requeue_pi && !this->rt_waiter) ||
Darren Hartaa109902012-11-26 16:29:56 -08001947 (!requeue_pi && this->rt_waiter) ||
1948 this->pi_state) {
Darren Hart392741e2009-08-07 15:20:48 -07001949 ret = -EINVAL;
1950 break;
1951 }
Darren Hart52400ba2009-04-03 13:40:49 -07001952
1953 /*
1954 * Wake nr_wake waiters. For requeue_pi, if we acquired the
1955 * lock, we already woke the top_waiter. If not, it will be
1956 * woken by futex_unlock_pi().
1957 */
1958 if (++task_count <= nr_wake && !requeue_pi) {
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001959 mark_wake_futex(&wake_q, this);
Darren Hart52400ba2009-04-03 13:40:49 -07001960 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 }
Darren Hart52400ba2009-04-03 13:40:49 -07001962
Darren Hart84bc4af2009-08-13 17:36:53 -07001963 /* Ensure we requeue to the expected futex for requeue_pi. */
1964 if (requeue_pi && !match_futex(this->requeue_pi_key, &key2)) {
1965 ret = -EINVAL;
1966 break;
1967 }
1968
Darren Hart52400ba2009-04-03 13:40:49 -07001969 /*
1970 * Requeue nr_requeue waiters and possibly one more in the case
1971 * of requeue_pi if we couldn't acquire the lock atomically.
1972 */
1973 if (requeue_pi) {
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00001974 /*
1975 * Prepare the waiter to take the rt_mutex. Take a
1976 * refcount on the pi_state and store the pointer in
1977 * the futex_q object of the waiter.
1978 */
Darren Hart52400ba2009-04-03 13:40:49 -07001979 atomic_inc(&pi_state->refcount);
1980 this->pi_state = pi_state;
1981 ret = rt_mutex_start_proxy_lock(&pi_state->pi_mutex,
1982 this->rt_waiter,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001983 this->task);
Darren Hart52400ba2009-04-03 13:40:49 -07001984 if (ret == 1) {
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00001985 /*
1986 * We got the lock. We do neither drop the
1987 * refcount on pi_state nor clear
1988 * this->pi_state because the waiter needs the
1989 * pi_state for cleaning up the user space
1990 * value. It will drop the refcount after
1991 * doing so.
1992 */
Darren Hartbeda2c72009-08-09 15:34:39 -07001993 requeue_pi_wake_futex(this, &key2, hb2);
Darren Hart89061d32009-10-15 15:30:48 -07001994 drop_count++;
Darren Hart52400ba2009-04-03 13:40:49 -07001995 continue;
1996 } else if (ret) {
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00001997 /*
1998 * rt_mutex_start_proxy_lock() detected a
1999 * potential deadlock when we tried to queue
2000 * that waiter. Drop the pi_state reference
2001 * which we took above and remove the pointer
2002 * to the state from the waiters futex_q
2003 * object.
2004 */
Darren Hart52400ba2009-04-03 13:40:49 -07002005 this->pi_state = NULL;
Thomas Gleixner29e9ee52015-12-19 20:07:39 +00002006 put_pi_state(pi_state);
Thomas Gleixner885c2cb2015-12-19 20:07:41 +00002007 /*
2008 * We stop queueing more waiters and let user
2009 * space deal with the mess.
2010 */
2011 break;
Darren Hart52400ba2009-04-03 13:40:49 -07002012 }
2013 }
2014 requeue_futex(this, hb1, hb2, &key2);
2015 drop_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 }
2017
Thomas Gleixnerecb38b72015-12-19 20:07:39 +00002018 /*
2019 * We took an extra initial reference to the pi_state either
2020 * in futex_proxy_trylock_atomic() or in lookup_pi_state(). We
2021 * need to drop it here again.
2022 */
Thomas Gleixner29e9ee52015-12-19 20:07:39 +00002023 put_pi_state(pi_state);
Thomas Gleixner885c2cb2015-12-19 20:07:41 +00002024
2025out_unlock:
Darren Hart5eb3dc62009-03-12 00:55:52 -07002026 double_unlock_hb(hb1, hb2);
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07002027 wake_up_q(&wake_q);
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07002028 hb_waiters_dec(hb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Darren Hartcd84a422009-04-02 14:19:38 -07002030 /*
2031 * drop_futex_key_refs() must be called outside the spinlocks. During
2032 * the requeue we moved futex_q's from the hash bucket at key1 to the
2033 * one at key2 and updated their key pointer. We no longer need to
2034 * hold the references to key1.
2035 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 while (--drop_count >= 0)
Rusty Russell9adef582007-05-08 00:26:42 -07002037 drop_futex_key_refs(&key1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
Darren Hart42d35d42008-12-29 15:49:53 -08002039out_put_keys:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002040 put_futex_key(&key2);
Darren Hart42d35d42008-12-29 15:49:53 -08002041out_put_key1:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002042 put_futex_key(&key1);
Darren Hart42d35d42008-12-29 15:49:53 -08002043out:
Darren Hart52400ba2009-04-03 13:40:49 -07002044 return ret ? ret : task_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045}
2046
2047/* The key must be already stored in q->key. */
Eric Sesterhenn82af7ac2008-01-25 10:40:46 +01002048static inline struct futex_hash_bucket *queue_lock(struct futex_q *q)
Namhyung Kim15e408c2010-09-14 21:43:48 +09002049 __acquires(&hb->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
Ingo Molnare2970f22006-06-27 02:54:47 -07002051 struct futex_hash_bucket *hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
Ingo Molnare2970f22006-06-27 02:54:47 -07002053 hb = hash_futex(&q->key);
Linus Torvalds11d46162014-03-20 22:11:17 -07002054
2055 /*
2056 * Increment the counter before taking the lock so that
2057 * a potential waker won't miss a to-be-slept task that is
2058 * waiting for the spinlock. This is safe as all queue_lock()
2059 * users end up calling queue_me(). Similarly, for housekeeping,
2060 * decrement the counter at queue_unlock() when some error has
2061 * occurred and we don't end up adding the task to the list.
2062 */
2063 hb_waiters_inc(hb);
2064
Ingo Molnare2970f22006-06-27 02:54:47 -07002065 q->lock_ptr = &hb->lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Davidlohr Bueso8ad7b372016-02-09 11:15:13 -08002067 spin_lock(&hb->lock); /* implies smp_mb(); (A) */
Ingo Molnare2970f22006-06-27 02:54:47 -07002068 return hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069}
2070
Darren Hartd40d65c2009-09-21 22:30:15 -07002071static inline void
Jason Low0d00c7b2014-01-12 15:31:22 -08002072queue_unlock(struct futex_hash_bucket *hb)
Namhyung Kim15e408c2010-09-14 21:43:48 +09002073 __releases(&hb->lock)
Darren Hartd40d65c2009-09-21 22:30:15 -07002074{
2075 spin_unlock(&hb->lock);
Linus Torvalds11d46162014-03-20 22:11:17 -07002076 hb_waiters_dec(hb);
Darren Hartd40d65c2009-09-21 22:30:15 -07002077}
2078
2079/**
2080 * queue_me() - Enqueue the futex_q on the futex_hash_bucket
2081 * @q: The futex_q to enqueue
2082 * @hb: The destination hash bucket
2083 *
2084 * The hb->lock must be held by the caller, and is released here. A call to
2085 * queue_me() is typically paired with exactly one call to unqueue_me(). The
2086 * exceptions involve the PI related operations, which may use unqueue_me_pi()
2087 * or nothing if the unqueue is done as part of the wake process and the unqueue
2088 * state is implicit in the state of woken task (see futex_wait_requeue_pi() for
2089 * an example).
2090 */
Eric Sesterhenn82af7ac2008-01-25 10:40:46 +01002091static inline void queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
Namhyung Kim15e408c2010-09-14 21:43:48 +09002092 __releases(&hb->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
Pierre Peifferec92d082007-05-09 02:35:00 -07002094 int prio;
2095
2096 /*
2097 * The priority used to register this element is
2098 * - either the real thread-priority for the real-time threads
2099 * (i.e. threads with a priority lower than MAX_RT_PRIO)
2100 * - or MAX_RT_PRIO for non-RT threads.
2101 * Thus, all RT-threads are woken first in priority order, and
2102 * the others are woken last, in FIFO order.
2103 */
2104 prio = min(current->normal_prio, MAX_RT_PRIO);
2105
2106 plist_node_init(&q->list, prio);
Pierre Peifferec92d082007-05-09 02:35:00 -07002107 plist_add(&q->list, &hb->chain);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002108 q->task = current;
Ingo Molnare2970f22006-06-27 02:54:47 -07002109 spin_unlock(&hb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110}
2111
Darren Hartd40d65c2009-09-21 22:30:15 -07002112/**
2113 * unqueue_me() - Remove the futex_q from its futex_hash_bucket
2114 * @q: The futex_q to unqueue
2115 *
2116 * The q->lock_ptr must not be held by the caller. A call to unqueue_me() must
2117 * be paired with exactly one earlier call to queue_me().
2118 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002119 * Return:
2120 * 1 - if the futex_q was still queued (and we removed unqueued it);
Darren Hartd40d65c2009-09-21 22:30:15 -07002121 * 0 - if the futex_q was already removed by the waking thread
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123static int unqueue_me(struct futex_q *q)
2124{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 spinlock_t *lock_ptr;
Ingo Molnare2970f22006-06-27 02:54:47 -07002126 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
2128 /* In the common case we don't take the spinlock, which is nice. */
Darren Hart42d35d42008-12-29 15:49:53 -08002129retry:
Jianyu Zhan29b75eb2016-03-07 09:32:24 +08002130 /*
2131 * q->lock_ptr can change between this read and the following spin_lock.
2132 * Use READ_ONCE to forbid the compiler from reloading q->lock_ptr and
2133 * optimizing lock_ptr out of the logic below.
2134 */
2135 lock_ptr = READ_ONCE(q->lock_ptr);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07002136 if (lock_ptr != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 spin_lock(lock_ptr);
2138 /*
2139 * q->lock_ptr can change between reading it and
2140 * spin_lock(), causing us to take the wrong lock. This
2141 * corrects the race condition.
2142 *
2143 * Reasoning goes like this: if we have the wrong lock,
2144 * q->lock_ptr must have changed (maybe several times)
2145 * between reading it and the spin_lock(). It can
2146 * change again after the spin_lock() but only if it was
2147 * already changed before the spin_lock(). It cannot,
2148 * however, change back to the original value. Therefore
2149 * we can detect whether we acquired the correct lock.
2150 */
2151 if (unlikely(lock_ptr != q->lock_ptr)) {
2152 spin_unlock(lock_ptr);
2153 goto retry;
2154 }
Lai Jiangshan2e129782010-12-22 14:18:50 +08002155 __unqueue_futex(q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002156
2157 BUG_ON(q->pi_state);
2158
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 spin_unlock(lock_ptr);
2160 ret = 1;
2161 }
2162
Rusty Russell9adef582007-05-08 00:26:42 -07002163 drop_futex_key_refs(&q->key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 return ret;
2165}
2166
Ingo Molnarc87e2832006-06-27 02:54:58 -07002167/*
2168 * PI futexes can not be requeued and must remove themself from the
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002169 * hash bucket. The hash bucket lock (i.e. lock_ptr) is held on entry
2170 * and dropped here.
Ingo Molnarc87e2832006-06-27 02:54:58 -07002171 */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002172static void unqueue_me_pi(struct futex_q *q)
Namhyung Kim15e408c2010-09-14 21:43:48 +09002173 __releases(q->lock_ptr)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002174{
Lai Jiangshan2e129782010-12-22 14:18:50 +08002175 __unqueue_futex(q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002176
2177 BUG_ON(!q->pi_state);
Thomas Gleixner29e9ee52015-12-19 20:07:39 +00002178 put_pi_state(q->pi_state);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002179 q->pi_state = NULL;
2180
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002181 spin_unlock(q->lock_ptr);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002182}
2183
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002184/*
Thomas Gleixnercdf71a12008-01-08 19:47:38 +01002185 * Fixup the pi_state owner with the new owner.
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002186 *
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002187 * Must be called with hash bucket lock held and mm->sem held for non
2188 * private futexes.
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002189 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002190static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002191 struct task_struct *newowner)
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002192{
Thomas Gleixnercdf71a12008-01-08 19:47:38 +01002193 u32 newtid = task_pid_vnr(newowner) | FUTEX_WAITERS;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002194 struct futex_pi_state *pi_state = q->pi_state;
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002195 struct task_struct *oldowner = pi_state->owner;
Vitaliy Ivanov7cfdaf32011-07-07 15:10:31 +03002196 u32 uval, uninitialized_var(curval), newval;
Darren Harte4dc5b72009-03-12 00:56:13 -07002197 int ret;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002198
2199 /* Owner died? */
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002200 if (!pi_state->owner)
2201 newtid |= FUTEX_OWNER_DIED;
2202
2203 /*
2204 * We are here either because we stole the rtmutex from the
Lai Jiangshan81612392011-01-14 17:09:41 +08002205 * previous highest priority waiter or we are the highest priority
2206 * waiter but failed to get the rtmutex the first time.
2207 * We have to replace the newowner TID in the user space variable.
2208 * This must be atomic as we have to preserve the owner died bit here.
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002209 *
Darren Hartb2d09942009-03-12 00:55:37 -07002210 * Note: We write the user space value _before_ changing the pi_state
2211 * because we can fault here. Imagine swapped out pages or a fork
2212 * that marked all the anonymous memory readonly for cow.
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002213 *
2214 * Modifying pi_state _before_ the user space value would
2215 * leave the pi_state in an inconsistent state when we fault
2216 * here, because we need to drop the hash bucket lock to
2217 * handle the fault. This might be observed in the PID check
2218 * in lookup_pi_state.
2219 */
2220retry:
2221 if (get_futex_value_locked(&uval, uaddr))
2222 goto handle_fault;
2223
2224 while (1) {
2225 newval = (uval & FUTEX_OWNER_DIED) | newtid;
2226
Michel Lespinasse37a9d912011-03-10 18:48:51 -08002227 if (cmpxchg_futex_value_locked(&curval, uaddr, uval, newval))
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002228 goto handle_fault;
2229 if (curval == uval)
2230 break;
2231 uval = curval;
2232 }
2233
2234 /*
2235 * We fixed up user space. Now we need to fix the pi_state
2236 * itself.
2237 */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002238 if (pi_state->owner != NULL) {
Thomas Gleixner1d615482009-11-17 14:54:03 +01002239 raw_spin_lock_irq(&pi_state->owner->pi_lock);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002240 WARN_ON(list_empty(&pi_state->list));
2241 list_del_init(&pi_state->list);
Thomas Gleixner1d615482009-11-17 14:54:03 +01002242 raw_spin_unlock_irq(&pi_state->owner->pi_lock);
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002243 }
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002244
Thomas Gleixnercdf71a12008-01-08 19:47:38 +01002245 pi_state->owner = newowner;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002246
Thomas Gleixner1d615482009-11-17 14:54:03 +01002247 raw_spin_lock_irq(&newowner->pi_lock);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002248 WARN_ON(!list_empty(&pi_state->list));
Thomas Gleixnercdf71a12008-01-08 19:47:38 +01002249 list_add(&pi_state->list, &newowner->pi_state_list);
Thomas Gleixner1d615482009-11-17 14:54:03 +01002250 raw_spin_unlock_irq(&newowner->pi_lock);
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002251 return 0;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002252
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002253 /*
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002254 * To handle the page fault we need to drop the hash bucket
Lai Jiangshan81612392011-01-14 17:09:41 +08002255 * lock here. That gives the other task (either the highest priority
2256 * waiter itself or the task which stole the rtmutex) the
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002257 * chance to try the fixup of the pi_state. So once we are
2258 * back from handling the fault we need to check the pi_state
2259 * after reacquiring the hash bucket lock and before trying to
2260 * do another fixup. When the fixup has been done already we
2261 * simply return.
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002262 */
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002263handle_fault:
2264 spin_unlock(q->lock_ptr);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002265
Thomas Gleixnerd0725992009-06-11 23:15:43 +02002266 ret = fault_in_user_writeable(uaddr);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002267
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002268 spin_lock(q->lock_ptr);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002269
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002270 /*
2271 * Check if someone else fixed it for us:
2272 */
2273 if (pi_state->owner != oldowner)
2274 return 0;
2275
2276 if (ret)
2277 return ret;
2278
2279 goto retry;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002280}
2281
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002282static long futex_wait_restart(struct restart_block *restart);
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07002283
Darren Hartca5f9522009-04-03 13:39:33 -07002284/**
Darren Hartdd973992009-04-03 13:40:02 -07002285 * fixup_owner() - Post lock pi_state and corner case management
2286 * @uaddr: user address of the futex
Darren Hartdd973992009-04-03 13:40:02 -07002287 * @q: futex_q (contains pi_state and access to the rt_mutex)
2288 * @locked: if the attempt to take the rt_mutex succeeded (1) or not (0)
2289 *
2290 * After attempting to lock an rt_mutex, this function is called to cleanup
2291 * the pi_state owner as well as handle race conditions that may allow us to
2292 * acquire the lock. Must be called with the hb lock held.
2293 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002294 * Return:
2295 * 1 - success, lock taken;
2296 * 0 - success, lock not taken;
Darren Hartdd973992009-04-03 13:40:02 -07002297 * <0 - on error (-EFAULT)
2298 */
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002299static int fixup_owner(u32 __user *uaddr, struct futex_q *q, int locked)
Darren Hartdd973992009-04-03 13:40:02 -07002300{
2301 struct task_struct *owner;
2302 int ret = 0;
2303
2304 if (locked) {
2305 /*
2306 * Got the lock. We might not be the anticipated owner if we
2307 * did a lock-steal - fix up the PI-state in that case:
2308 */
2309 if (q->pi_state->owner != current)
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002310 ret = fixup_pi_state_owner(uaddr, q, current);
Darren Hartdd973992009-04-03 13:40:02 -07002311 goto out;
2312 }
2313
2314 /*
2315 * Catch the rare case, where the lock was released when we were on the
2316 * way back before we locked the hash bucket.
2317 */
2318 if (q->pi_state->owner == current) {
2319 /*
2320 * Try to get the rt_mutex now. This might fail as some other
2321 * task acquired the rt_mutex after we removed ourself from the
2322 * rt_mutex waiters list.
2323 */
2324 if (rt_mutex_trylock(&q->pi_state->pi_mutex)) {
2325 locked = 1;
2326 goto out;
2327 }
2328
2329 /*
2330 * pi_state is incorrect, some other task did a lock steal and
2331 * we returned due to timeout or signal without taking the
Lai Jiangshan81612392011-01-14 17:09:41 +08002332 * rt_mutex. Too late.
Darren Hartdd973992009-04-03 13:40:02 -07002333 */
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01002334 raw_spin_lock_irq(&q->pi_state->pi_mutex.wait_lock);
Darren Hartdd973992009-04-03 13:40:02 -07002335 owner = rt_mutex_owner(&q->pi_state->pi_mutex);
Lai Jiangshan81612392011-01-14 17:09:41 +08002336 if (!owner)
2337 owner = rt_mutex_next_owner(&q->pi_state->pi_mutex);
Thomas Gleixnerb4abf912016-01-13 11:25:38 +01002338 raw_spin_unlock_irq(&q->pi_state->pi_mutex.wait_lock);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002339 ret = fixup_pi_state_owner(uaddr, q, owner);
Darren Hartdd973992009-04-03 13:40:02 -07002340 goto out;
2341 }
2342
2343 /*
2344 * Paranoia check. If we did not take the lock, then we should not be
Lai Jiangshan81612392011-01-14 17:09:41 +08002345 * the owner of the rt_mutex.
Darren Hartdd973992009-04-03 13:40:02 -07002346 */
2347 if (rt_mutex_owner(&q->pi_state->pi_mutex) == current)
2348 printk(KERN_ERR "fixup_owner: ret = %d pi-mutex: %p "
2349 "pi-state %p\n", ret,
2350 q->pi_state->pi_mutex.owner,
2351 q->pi_state->owner);
2352
2353out:
2354 return ret ? ret : locked;
2355}
2356
2357/**
Darren Hartca5f9522009-04-03 13:39:33 -07002358 * futex_wait_queue_me() - queue_me() and wait for wakeup, timeout, or signal
2359 * @hb: the futex hash bucket, must be locked by the caller
2360 * @q: the futex_q to queue up on
2361 * @timeout: the prepared hrtimer_sleeper, or null for no timeout
Darren Hartca5f9522009-04-03 13:39:33 -07002362 */
2363static void futex_wait_queue_me(struct futex_hash_bucket *hb, struct futex_q *q,
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02002364 struct hrtimer_sleeper *timeout)
Darren Hartca5f9522009-04-03 13:39:33 -07002365{
Darren Hart9beba3c2009-09-24 11:54:47 -07002366 /*
2367 * The task state is guaranteed to be set before another task can
Peter Zijlstrab92b8b32015-05-12 10:51:55 +02002368 * wake it. set_current_state() is implemented using smp_store_mb() and
Darren Hart9beba3c2009-09-24 11:54:47 -07002369 * queue_me() calls spin_unlock() upon completion, both serializing
2370 * access to the hash list and forcing another memory barrier.
2371 */
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02002372 set_current_state(TASK_INTERRUPTIBLE);
Darren Hart0729e192009-09-21 22:30:38 -07002373 queue_me(q, hb);
Darren Hartca5f9522009-04-03 13:39:33 -07002374
2375 /* Arm the timer */
Thomas Gleixner2e4b0d32015-04-14 21:09:13 +00002376 if (timeout)
Darren Hartca5f9522009-04-03 13:39:33 -07002377 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
Darren Hartca5f9522009-04-03 13:39:33 -07002378
2379 /*
Darren Hart0729e192009-09-21 22:30:38 -07002380 * If we have been removed from the hash list, then another task
2381 * has tried to wake us, and we can skip the call to schedule().
Darren Hartca5f9522009-04-03 13:39:33 -07002382 */
2383 if (likely(!plist_node_empty(&q->list))) {
2384 /*
2385 * If the timer has already expired, current will already be
2386 * flagged for rescheduling. Only call schedule if there
2387 * is no timeout, or if it has yet to expire.
2388 */
2389 if (!timeout || timeout->task)
Colin Cross88c80042013-05-01 18:35:05 -07002390 freezable_schedule();
Darren Hartca5f9522009-04-03 13:39:33 -07002391 }
2392 __set_current_state(TASK_RUNNING);
2393}
2394
Darren Hartf8010732009-04-03 13:40:40 -07002395/**
2396 * futex_wait_setup() - Prepare to wait on a futex
2397 * @uaddr: the futex userspace address
2398 * @val: the expected value
Darren Hartb41277d2010-11-08 13:10:09 -08002399 * @flags: futex flags (FLAGS_SHARED, etc.)
Darren Hartf8010732009-04-03 13:40:40 -07002400 * @q: the associated futex_q
2401 * @hb: storage for hash_bucket pointer to be returned to caller
2402 *
2403 * Setup the futex_q and locate the hash_bucket. Get the futex value and
2404 * compare it with the expected value. Handle atomic faults internally.
2405 * Return with the hb lock held and a q.key reference on success, and unlocked
2406 * with no q.key reference on failure.
2407 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002408 * Return:
2409 * 0 - uaddr contains val and hb has been locked;
Bart Van Asscheca4a04c2011-07-17 09:01:00 +02002410 * <1 - -EFAULT or -EWOULDBLOCK (uaddr does not contain val) and hb is unlocked
Darren Hartf8010732009-04-03 13:40:40 -07002411 */
Darren Hartb41277d2010-11-08 13:10:09 -08002412static int futex_wait_setup(u32 __user *uaddr, u32 val, unsigned int flags,
Darren Hartf8010732009-04-03 13:40:40 -07002413 struct futex_q *q, struct futex_hash_bucket **hb)
2414{
2415 u32 uval;
2416 int ret;
2417
2418 /*
2419 * Access the page AFTER the hash-bucket is locked.
2420 * Order is important:
2421 *
2422 * Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val);
2423 * Userspace waker: if (cond(var)) { var = new; futex_wake(&var); }
2424 *
2425 * The basic logical guarantee of a futex is that it blocks ONLY
2426 * if cond(var) is known to be true at the time of blocking, for
Michel Lespinasse8fe8f542011-03-06 18:07:50 -08002427 * any cond. If we locked the hash-bucket after testing *uaddr, that
2428 * would open a race condition where we could block indefinitely with
Darren Hartf8010732009-04-03 13:40:40 -07002429 * cond(var) false, which would violate the guarantee.
2430 *
Michel Lespinasse8fe8f542011-03-06 18:07:50 -08002431 * On the other hand, we insert q and release the hash-bucket only
2432 * after testing *uaddr. This guarantees that futex_wait() will NOT
2433 * absorb a wakeup if *uaddr does not match the desired values
2434 * while the syscall executes.
Darren Hartf8010732009-04-03 13:40:40 -07002435 */
2436retry:
Shawn Bohrer9ea71502011-06-30 11:21:32 -05002437 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q->key, VERIFY_READ);
Darren Hartf8010732009-04-03 13:40:40 -07002438 if (unlikely(ret != 0))
Darren Harta5a2a0c2009-04-10 09:50:05 -07002439 return ret;
Darren Hartf8010732009-04-03 13:40:40 -07002440
2441retry_private:
2442 *hb = queue_lock(q);
2443
2444 ret = get_futex_value_locked(&uval, uaddr);
2445
2446 if (ret) {
Jason Low0d00c7b2014-01-12 15:31:22 -08002447 queue_unlock(*hb);
Darren Hartf8010732009-04-03 13:40:40 -07002448
2449 ret = get_user(uval, uaddr);
2450 if (ret)
2451 goto out;
2452
Darren Hartb41277d2010-11-08 13:10:09 -08002453 if (!(flags & FLAGS_SHARED))
Darren Hartf8010732009-04-03 13:40:40 -07002454 goto retry_private;
2455
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002456 put_futex_key(&q->key);
Darren Hartf8010732009-04-03 13:40:40 -07002457 goto retry;
2458 }
2459
2460 if (uval != val) {
Jason Low0d00c7b2014-01-12 15:31:22 -08002461 queue_unlock(*hb);
Darren Hartf8010732009-04-03 13:40:40 -07002462 ret = -EWOULDBLOCK;
2463 }
2464
2465out:
2466 if (ret)
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002467 put_futex_key(&q->key);
Darren Hartf8010732009-04-03 13:40:40 -07002468 return ret;
2469}
2470
Darren Hartb41277d2010-11-08 13:10:09 -08002471static int futex_wait(u32 __user *uaddr, unsigned int flags, u32 val,
2472 ktime_t *abs_time, u32 bitset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473{
Darren Hartca5f9522009-04-03 13:39:33 -07002474 struct hrtimer_sleeper timeout, *to = NULL;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002475 struct restart_block *restart;
Ingo Molnare2970f22006-06-27 02:54:47 -07002476 struct futex_hash_bucket *hb;
Darren Hart5bdb05f2010-11-08 13:40:28 -08002477 struct futex_q q = futex_q_init;
Ingo Molnare2970f22006-06-27 02:54:47 -07002478 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
Thomas Gleixnercd689982008-02-01 17:45:14 +01002480 if (!bitset)
2481 return -EINVAL;
Thomas Gleixnercd689982008-02-01 17:45:14 +01002482 q.bitset = bitset;
Darren Hartca5f9522009-04-03 13:39:33 -07002483
2484 if (abs_time) {
2485 to = &timeout;
2486
Darren Hartb41277d2010-11-08 13:10:09 -08002487 hrtimer_init_on_stack(&to->timer, (flags & FLAGS_CLOCKRT) ?
2488 CLOCK_REALTIME : CLOCK_MONOTONIC,
2489 HRTIMER_MODE_ABS);
Darren Hartca5f9522009-04-03 13:39:33 -07002490 hrtimer_init_sleeper(to, current);
2491 hrtimer_set_expires_range_ns(&to->timer, *abs_time,
2492 current->timer_slack_ns);
2493 }
2494
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002495retry:
Darren Hart7ada8762010-10-17 08:35:04 -07002496 /*
2497 * Prepare to wait on uaddr. On success, holds hb lock and increments
2498 * q.key refs.
2499 */
Darren Hartb41277d2010-11-08 13:10:09 -08002500 ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
Darren Hartf8010732009-04-03 13:40:40 -07002501 if (ret)
Darren Hart42d35d42008-12-29 15:49:53 -08002502 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
Darren Hartca5f9522009-04-03 13:39:33 -07002504 /* queue_me and wait for wakeup, timeout, or a signal. */
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02002505 futex_wait_queue_me(hb, &q, to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
2507 /* If we were woken (and unqueued), we succeeded, whatever. */
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002508 ret = 0;
Darren Hart7ada8762010-10-17 08:35:04 -07002509 /* unqueue_me() drops q.key ref */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 if (!unqueue_me(&q))
Darren Hart7ada8762010-10-17 08:35:04 -07002511 goto out;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002512 ret = -ETIMEDOUT;
Darren Hartca5f9522009-04-03 13:39:33 -07002513 if (to && !to->task)
Darren Hart7ada8762010-10-17 08:35:04 -07002514 goto out;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002515
Ingo Molnare2970f22006-06-27 02:54:47 -07002516 /*
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002517 * We expect signal_pending(current), but we might be the
2518 * victim of a spurious wakeup as well.
Ingo Molnare2970f22006-06-27 02:54:47 -07002519 */
Darren Hart7ada8762010-10-17 08:35:04 -07002520 if (!signal_pending(current))
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002521 goto retry;
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002522
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002523 ret = -ERESTARTSYS;
Pierre Peifferc19384b2007-05-09 02:35:02 -07002524 if (!abs_time)
Darren Hart7ada8762010-10-17 08:35:04 -07002525 goto out;
Steven Rostedtce6bd422007-12-05 15:46:09 +01002526
Andy Lutomirskif56141e2015-02-12 15:01:14 -08002527 restart = &current->restart_block;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002528 restart->fn = futex_wait_restart;
Namhyung Kima3c74c52010-09-14 21:43:47 +09002529 restart->futex.uaddr = uaddr;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002530 restart->futex.val = val;
2531 restart->futex.time = abs_time->tv64;
2532 restart->futex.bitset = bitset;
Darren Hart0cd9c642011-04-14 15:41:57 -07002533 restart->futex.flags = flags | FLAGS_HAS_TIMEOUT;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002534
2535 ret = -ERESTART_RESTARTBLOCK;
2536
Darren Hart42d35d42008-12-29 15:49:53 -08002537out:
Darren Hartca5f9522009-04-03 13:39:33 -07002538 if (to) {
2539 hrtimer_cancel(&to->timer);
2540 destroy_hrtimer_on_stack(&to->timer);
2541 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07002542 return ret;
2543}
2544
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002545
2546static long futex_wait_restart(struct restart_block *restart)
2547{
Namhyung Kima3c74c52010-09-14 21:43:47 +09002548 u32 __user *uaddr = restart->futex.uaddr;
Darren Harta72188d2009-04-03 13:40:22 -07002549 ktime_t t, *tp = NULL;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002550
Darren Harta72188d2009-04-03 13:40:22 -07002551 if (restart->futex.flags & FLAGS_HAS_TIMEOUT) {
2552 t.tv64 = restart->futex.time;
2553 tp = &t;
2554 }
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002555 restart->fn = do_no_restart_syscall;
Darren Hartb41277d2010-11-08 13:10:09 -08002556
2557 return (long)futex_wait(uaddr, restart->futex.flags,
2558 restart->futex.val, tp, restart->futex.bitset);
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002559}
2560
2561
Ingo Molnarc87e2832006-06-27 02:54:58 -07002562/*
2563 * Userspace tried a 0 -> TID atomic transition of the futex value
2564 * and failed. The kernel side here does the whole locking operation:
Davidlohr Bueso767f5092015-06-29 23:26:01 -07002565 * if there are waiters then it will block as a consequence of relying
2566 * on rt-mutexes, it does PI, etc. (Due to races the kernel might see
2567 * a 0 value of the futex too.).
2568 *
2569 * Also serves as futex trylock_pi()'ing, and due semantics.
Ingo Molnarc87e2832006-06-27 02:54:58 -07002570 */
Michael Kerrisk996636d2015-01-16 20:28:06 +01002571static int futex_lock_pi(u32 __user *uaddr, unsigned int flags,
Darren Hartb41277d2010-11-08 13:10:09 -08002572 ktime_t *time, int trylock)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002573{
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07002574 struct hrtimer_sleeper timeout, *to = NULL;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002575 struct futex_hash_bucket *hb;
Darren Hart5bdb05f2010-11-08 13:40:28 -08002576 struct futex_q q = futex_q_init;
Darren Hartdd973992009-04-03 13:40:02 -07002577 int res, ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002578
2579 if (refill_pi_state_cache())
2580 return -ENOMEM;
2581
Pierre Peifferc19384b2007-05-09 02:35:02 -07002582 if (time) {
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07002583 to = &timeout;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07002584 hrtimer_init_on_stack(&to->timer, CLOCK_REALTIME,
2585 HRTIMER_MODE_ABS);
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07002586 hrtimer_init_sleeper(to, current);
Arjan van de Vencc584b22008-09-01 15:02:30 -07002587 hrtimer_set_expires(&to->timer, *time);
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07002588 }
2589
Darren Hart42d35d42008-12-29 15:49:53 -08002590retry:
Shawn Bohrer9ea71502011-06-30 11:21:32 -05002591 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q.key, VERIFY_WRITE);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002592 if (unlikely(ret != 0))
Darren Hart42d35d42008-12-29 15:49:53 -08002593 goto out;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002594
Darren Harte4dc5b72009-03-12 00:56:13 -07002595retry_private:
Eric Sesterhenn82af7ac2008-01-25 10:40:46 +01002596 hb = queue_lock(&q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002597
Darren Hartbab5bc92009-04-07 23:23:50 -07002598 ret = futex_lock_pi_atomic(uaddr, hb, &q.key, &q.pi_state, current, 0);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002599 if (unlikely(ret)) {
Davidlohr Bueso767f5092015-06-29 23:26:01 -07002600 /*
2601 * Atomic work succeeded and we got the lock,
2602 * or failed. Either way, we do _not_ block.
2603 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002604 switch (ret) {
Darren Hart1a520842009-04-03 13:39:52 -07002605 case 1:
2606 /* We got the lock. */
2607 ret = 0;
2608 goto out_unlock_put_key;
2609 case -EFAULT:
2610 goto uaddr_faulted;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002611 case -EAGAIN:
2612 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00002613 * Two reasons for this:
2614 * - Task is exiting and we just wait for the
2615 * exit to complete.
2616 * - The user space value changed.
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002617 */
Jason Low0d00c7b2014-01-12 15:31:22 -08002618 queue_unlock(hb);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002619 put_futex_key(&q.key);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002620 cond_resched();
2621 goto retry;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002622 default:
Darren Hart42d35d42008-12-29 15:49:53 -08002623 goto out_unlock_put_key;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002624 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07002625 }
2626
2627 /*
2628 * Only actually queue now that the atomic ops are done:
2629 */
Eric Sesterhenn82af7ac2008-01-25 10:40:46 +01002630 queue_me(&q, hb);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002631
Ingo Molnarc87e2832006-06-27 02:54:58 -07002632 WARN_ON(!q.pi_state);
2633 /*
2634 * Block on the PI mutex:
2635 */
Thomas Gleixnerc051b212014-05-22 03:25:50 +00002636 if (!trylock) {
2637 ret = rt_mutex_timed_futex_lock(&q.pi_state->pi_mutex, to);
2638 } else {
Ingo Molnarc87e2832006-06-27 02:54:58 -07002639 ret = rt_mutex_trylock(&q.pi_state->pi_mutex);
2640 /* Fixup the trylock return value: */
2641 ret = ret ? 0 : -EWOULDBLOCK;
2642 }
2643
Vernon Mauerya99e4e42006-07-01 04:35:42 -07002644 spin_lock(q.lock_ptr);
Darren Hartdd973992009-04-03 13:40:02 -07002645 /*
2646 * Fixup the pi_state owner and possibly acquire the lock if we
2647 * haven't already.
2648 */
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002649 res = fixup_owner(uaddr, &q, !ret);
Darren Hartdd973992009-04-03 13:40:02 -07002650 /*
2651 * If fixup_owner() returned an error, proprogate that. If it acquired
2652 * the lock, clear our -ETIMEDOUT or -EINTR.
2653 */
2654 if (res)
2655 ret = (res < 0) ? res : 0;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002656
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002657 /* Unqueue and drop the lock */
2658 unqueue_me_pi(&q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002659
Mikael Pettersson5ecb01c2010-01-23 22:36:29 +01002660 goto out_put_key;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002661
Darren Hart42d35d42008-12-29 15:49:53 -08002662out_unlock_put_key:
Jason Low0d00c7b2014-01-12 15:31:22 -08002663 queue_unlock(hb);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002664
Darren Hart42d35d42008-12-29 15:49:53 -08002665out_put_key:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002666 put_futex_key(&q.key);
Darren Hart42d35d42008-12-29 15:49:53 -08002667out:
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07002668 if (to)
2669 destroy_hrtimer_on_stack(&to->timer);
Darren Hartdd973992009-04-03 13:40:02 -07002670 return ret != -EINTR ? ret : -ERESTARTNOINTR;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002671
Darren Hart42d35d42008-12-29 15:49:53 -08002672uaddr_faulted:
Jason Low0d00c7b2014-01-12 15:31:22 -08002673 queue_unlock(hb);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002674
Thomas Gleixnerd0725992009-06-11 23:15:43 +02002675 ret = fault_in_user_writeable(uaddr);
Darren Harte4dc5b72009-03-12 00:56:13 -07002676 if (ret)
2677 goto out_put_key;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002678
Darren Hartb41277d2010-11-08 13:10:09 -08002679 if (!(flags & FLAGS_SHARED))
Darren Harte4dc5b72009-03-12 00:56:13 -07002680 goto retry_private;
2681
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002682 put_futex_key(&q.key);
Darren Harte4dc5b72009-03-12 00:56:13 -07002683 goto retry;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002684}
2685
2686/*
Ingo Molnarc87e2832006-06-27 02:54:58 -07002687 * Userspace attempted a TID -> 0 atomic transition, and failed.
2688 * This is the in-kernel slowpath: we look up the PI state (if any),
2689 * and do the rt-mutex unlock.
2690 */
Darren Hartb41277d2010-11-08 13:10:09 -08002691static int futex_unlock_pi(u32 __user *uaddr, unsigned int flags)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002692{
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002693 u32 uninitialized_var(curval), uval, vpid = task_pid_vnr(current);
Peter Zijlstra38d47c12008-09-26 19:32:20 +02002694 union futex_key key = FUTEX_KEY_INIT;
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002695 struct futex_hash_bucket *hb;
2696 struct futex_q *match;
Darren Harte4dc5b72009-03-12 00:56:13 -07002697 int ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002698
2699retry:
2700 if (get_user(uval, uaddr))
2701 return -EFAULT;
2702 /*
2703 * We release only a lock we actually own:
2704 */
Thomas Gleixnerc0c9ed12011-03-11 11:51:22 +01002705 if ((uval & FUTEX_TID_MASK) != vpid)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002706 return -EPERM;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002707
Shawn Bohrer9ea71502011-06-30 11:21:32 -05002708 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, VERIFY_WRITE);
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002709 if (ret)
2710 return ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002711
2712 hb = hash_futex(&key);
2713 spin_lock(&hb->lock);
2714
Ingo Molnarc87e2832006-06-27 02:54:58 -07002715 /*
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002716 * Check waiters first. We do not trust user space values at
2717 * all and we at least want to know if user space fiddled
2718 * with the futex value instead of blindly unlocking.
Ingo Molnarc87e2832006-06-27 02:54:58 -07002719 */
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002720 match = futex_top_waiter(hb, &key);
2721 if (match) {
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02002722 ret = wake_futex_pi(uaddr, uval, match, hb);
2723 /*
2724 * In case of success wake_futex_pi dropped the hash
2725 * bucket lock.
2726 */
2727 if (!ret)
2728 goto out_putkey;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002729 /*
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002730 * The atomic access to the futex value generated a
2731 * pagefault, so retry the user-access and the wakeup:
Ingo Molnarc87e2832006-06-27 02:54:58 -07002732 */
2733 if (ret == -EFAULT)
2734 goto pi_faulted;
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02002735 /*
Sebastian Andrzej Siewior89e9e662016-04-15 14:35:39 +02002736 * A unconditional UNLOCK_PI op raced against a waiter
2737 * setting the FUTEX_WAITERS bit. Try again.
2738 */
2739 if (ret == -EAGAIN) {
2740 spin_unlock(&hb->lock);
2741 put_futex_key(&key);
2742 goto retry;
2743 }
2744 /*
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02002745 * wake_futex_pi has detected invalid state. Tell user
2746 * space.
2747 */
Ingo Molnarc87e2832006-06-27 02:54:58 -07002748 goto out_unlock;
2749 }
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002750
Ingo Molnarc87e2832006-06-27 02:54:58 -07002751 /*
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002752 * We have no kernel internal state, i.e. no waiters in the
2753 * kernel. Waiters which are about to queue themselves are stuck
2754 * on hb->lock. So we can safely ignore them. We do neither
2755 * preserve the WAITERS bit not the OWNER_DIED one. We are the
2756 * owner.
Ingo Molnarc87e2832006-06-27 02:54:58 -07002757 */
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002758 if (cmpxchg_futex_value_locked(&curval, uaddr, uval, 0))
Thomas Gleixner13fbca42014-06-03 12:27:07 +00002759 goto pi_faulted;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002760
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002761 /*
2762 * If uval has changed, let user space handle it.
2763 */
2764 ret = (curval == uval) ? 0 : -EAGAIN;
2765
Ingo Molnarc87e2832006-06-27 02:54:58 -07002766out_unlock:
2767 spin_unlock(&hb->lock);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02002768out_putkey:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002769 put_futex_key(&key);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002770 return ret;
2771
2772pi_faulted:
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002773 spin_unlock(&hb->lock);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002774 put_futex_key(&key);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002775
Thomas Gleixnerd0725992009-06-11 23:15:43 +02002776 ret = fault_in_user_writeable(uaddr);
Darren Hartb5686362008-12-18 15:06:34 -08002777 if (!ret)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002778 goto retry;
2779
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 return ret;
2781}
2782
Darren Hart52400ba2009-04-03 13:40:49 -07002783/**
2784 * handle_early_requeue_pi_wakeup() - Detect early wakeup on the initial futex
2785 * @hb: the hash_bucket futex_q was original enqueued on
2786 * @q: the futex_q woken while waiting to be requeued
2787 * @key2: the futex_key of the requeue target futex
2788 * @timeout: the timeout associated with the wait (NULL if none)
2789 *
2790 * Detect if the task was woken on the initial futex as opposed to the requeue
2791 * target futex. If so, determine if it was a timeout or a signal that caused
2792 * the wakeup and return the appropriate error code to the caller. Must be
2793 * called with the hb lock held.
2794 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002795 * Return:
2796 * 0 = no early wakeup detected;
2797 * <0 = -ETIMEDOUT or -ERESTARTNOINTR
Darren Hart52400ba2009-04-03 13:40:49 -07002798 */
2799static inline
2800int handle_early_requeue_pi_wakeup(struct futex_hash_bucket *hb,
2801 struct futex_q *q, union futex_key *key2,
2802 struct hrtimer_sleeper *timeout)
2803{
2804 int ret = 0;
2805
2806 /*
2807 * With the hb lock held, we avoid races while we process the wakeup.
2808 * We only need to hold hb (and not hb2) to ensure atomicity as the
2809 * wakeup code can't change q.key from uaddr to uaddr2 if we hold hb.
2810 * It can't be requeued from uaddr2 to something else since we don't
2811 * support a PI aware source futex for requeue.
2812 */
2813 if (!match_futex(&q->key, key2)) {
2814 WARN_ON(q->lock_ptr && (&hb->lock != q->lock_ptr));
2815 /*
2816 * We were woken prior to requeue by a timeout or a signal.
2817 * Unqueue the futex_q and determine which it was.
2818 */
Lai Jiangshan2e129782010-12-22 14:18:50 +08002819 plist_del(&q->list, &hb->chain);
Linus Torvalds11d46162014-03-20 22:11:17 -07002820 hb_waiters_dec(hb);
Darren Hart52400ba2009-04-03 13:40:49 -07002821
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002822 /* Handle spurious wakeups gracefully */
Thomas Gleixner11df6dd2009-10-28 20:26:48 +01002823 ret = -EWOULDBLOCK;
Darren Hart52400ba2009-04-03 13:40:49 -07002824 if (timeout && !timeout->task)
2825 ret = -ETIMEDOUT;
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002826 else if (signal_pending(current))
Thomas Gleixner1c840c12009-05-20 09:22:40 +02002827 ret = -ERESTARTNOINTR;
Darren Hart52400ba2009-04-03 13:40:49 -07002828 }
2829 return ret;
2830}
2831
2832/**
2833 * futex_wait_requeue_pi() - Wait on uaddr and take uaddr2
Darren Hart56ec1602009-09-21 22:29:59 -07002834 * @uaddr: the futex we initially wait on (non-pi)
Darren Hartb41277d2010-11-08 13:10:09 -08002835 * @flags: futex flags (FLAGS_SHARED, FLAGS_CLOCKRT, etc.), they must be
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07002836 * the same type, no requeueing from private to shared, etc.
Darren Hart52400ba2009-04-03 13:40:49 -07002837 * @val: the expected value of uaddr
2838 * @abs_time: absolute timeout
Darren Hart56ec1602009-09-21 22:29:59 -07002839 * @bitset: 32 bit wakeup bitset set by userspace, defaults to all
Darren Hart52400ba2009-04-03 13:40:49 -07002840 * @uaddr2: the pi futex we will take prior to returning to user-space
2841 *
2842 * The caller will wait on uaddr and will be requeued by futex_requeue() to
Darren Hart6f7b0a22012-07-20 11:53:31 -07002843 * uaddr2 which must be PI aware and unique from uaddr. Normal wakeup will wake
2844 * on uaddr2 and complete the acquisition of the rt_mutex prior to returning to
2845 * userspace. This ensures the rt_mutex maintains an owner when it has waiters;
2846 * without one, the pi logic would not know which task to boost/deboost, if
2847 * there was a need to.
Darren Hart52400ba2009-04-03 13:40:49 -07002848 *
2849 * We call schedule in futex_wait_queue_me() when we enqueue and return there
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002850 * via the following--
Darren Hart52400ba2009-04-03 13:40:49 -07002851 * 1) wakeup on uaddr2 after an atomic lock acquisition by futex_requeue()
Darren Hartcc6db4e2009-07-31 16:20:10 -07002852 * 2) wakeup on uaddr2 after a requeue
2853 * 3) signal
2854 * 4) timeout
Darren Hart52400ba2009-04-03 13:40:49 -07002855 *
Darren Hartcc6db4e2009-07-31 16:20:10 -07002856 * If 3, cleanup and return -ERESTARTNOINTR.
Darren Hart52400ba2009-04-03 13:40:49 -07002857 *
2858 * If 2, we may then block on trying to take the rt_mutex and return via:
2859 * 5) successful lock
2860 * 6) signal
2861 * 7) timeout
2862 * 8) other lock acquisition failure
2863 *
Darren Hartcc6db4e2009-07-31 16:20:10 -07002864 * If 6, return -EWOULDBLOCK (restarting the syscall would do the same).
Darren Hart52400ba2009-04-03 13:40:49 -07002865 *
2866 * If 4 or 7, we cleanup and return with -ETIMEDOUT.
2867 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002868 * Return:
2869 * 0 - On success;
Darren Hart52400ba2009-04-03 13:40:49 -07002870 * <0 - On error
2871 */
Darren Hartb41277d2010-11-08 13:10:09 -08002872static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
Darren Hart52400ba2009-04-03 13:40:49 -07002873 u32 val, ktime_t *abs_time, u32 bitset,
Darren Hartb41277d2010-11-08 13:10:09 -08002874 u32 __user *uaddr2)
Darren Hart52400ba2009-04-03 13:40:49 -07002875{
2876 struct hrtimer_sleeper timeout, *to = NULL;
2877 struct rt_mutex_waiter rt_waiter;
Darren Hart52400ba2009-04-03 13:40:49 -07002878 struct futex_hash_bucket *hb;
Darren Hart5bdb05f2010-11-08 13:40:28 -08002879 union futex_key key2 = FUTEX_KEY_INIT;
2880 struct futex_q q = futex_q_init;
Darren Hart52400ba2009-04-03 13:40:49 -07002881 int res, ret;
Darren Hart52400ba2009-04-03 13:40:49 -07002882
Darren Hart6f7b0a22012-07-20 11:53:31 -07002883 if (uaddr == uaddr2)
2884 return -EINVAL;
2885
Darren Hart52400ba2009-04-03 13:40:49 -07002886 if (!bitset)
2887 return -EINVAL;
2888
2889 if (abs_time) {
2890 to = &timeout;
Darren Hartb41277d2010-11-08 13:10:09 -08002891 hrtimer_init_on_stack(&to->timer, (flags & FLAGS_CLOCKRT) ?
2892 CLOCK_REALTIME : CLOCK_MONOTONIC,
2893 HRTIMER_MODE_ABS);
Darren Hart52400ba2009-04-03 13:40:49 -07002894 hrtimer_init_sleeper(to, current);
2895 hrtimer_set_expires_range_ns(&to->timer, *abs_time,
2896 current->timer_slack_ns);
2897 }
2898
2899 /*
2900 * The waiter is allocated on our stack, manipulated by the requeue
2901 * code while we sleep on uaddr.
2902 */
2903 debug_rt_mutex_init_waiter(&rt_waiter);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01002904 RB_CLEAR_NODE(&rt_waiter.pi_tree_entry);
2905 RB_CLEAR_NODE(&rt_waiter.tree_entry);
Darren Hart52400ba2009-04-03 13:40:49 -07002906 rt_waiter.task = NULL;
2907
Shawn Bohrer9ea71502011-06-30 11:21:32 -05002908 ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE);
Darren Hart52400ba2009-04-03 13:40:49 -07002909 if (unlikely(ret != 0))
2910 goto out;
2911
Darren Hart84bc4af2009-08-13 17:36:53 -07002912 q.bitset = bitset;
2913 q.rt_waiter = &rt_waiter;
2914 q.requeue_pi_key = &key2;
2915
Darren Hart7ada8762010-10-17 08:35:04 -07002916 /*
2917 * Prepare to wait on uaddr. On success, increments q.key (key1) ref
2918 * count.
2919 */
Darren Hartb41277d2010-11-08 13:10:09 -08002920 ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
Thomas Gleixnerc8b15a72009-05-20 09:18:50 +02002921 if (ret)
2922 goto out_key2;
Darren Hart52400ba2009-04-03 13:40:49 -07002923
Thomas Gleixnere9c243a2014-06-03 12:27:06 +00002924 /*
2925 * The check above which compares uaddrs is not sufficient for
2926 * shared futexes. We need to compare the keys:
2927 */
2928 if (match_futex(&q.key, &key2)) {
Thomas Gleixner13c42c22014-09-11 23:44:35 +02002929 queue_unlock(hb);
Thomas Gleixnere9c243a2014-06-03 12:27:06 +00002930 ret = -EINVAL;
2931 goto out_put_keys;
2932 }
2933
Darren Hart52400ba2009-04-03 13:40:49 -07002934 /* Queue the futex_q, drop the hb lock, wait for wakeup. */
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02002935 futex_wait_queue_me(hb, &q, to);
Darren Hart52400ba2009-04-03 13:40:49 -07002936
2937 spin_lock(&hb->lock);
2938 ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
2939 spin_unlock(&hb->lock);
2940 if (ret)
2941 goto out_put_keys;
2942
2943 /*
2944 * In order for us to be here, we know our q.key == key2, and since
2945 * we took the hb->lock above, we also know that futex_requeue() has
2946 * completed and we no longer have to concern ourselves with a wakeup
Darren Hart7ada8762010-10-17 08:35:04 -07002947 * race with the atomic proxy lock acquisition by the requeue code. The
2948 * futex_requeue dropped our key1 reference and incremented our key2
2949 * reference count.
Darren Hart52400ba2009-04-03 13:40:49 -07002950 */
2951
2952 /* Check if the requeue code acquired the second futex for us. */
2953 if (!q.rt_waiter) {
2954 /*
2955 * Got the lock. We might not be the anticipated owner if we
2956 * did a lock-steal - fix up the PI-state in that case.
2957 */
2958 if (q.pi_state && (q.pi_state->owner != current)) {
2959 spin_lock(q.lock_ptr);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002960 ret = fixup_pi_state_owner(uaddr2, &q, current);
Thomas Gleixnerfb75a422015-12-19 20:07:38 +00002961 /*
2962 * Drop the reference to the pi state which
2963 * the requeue_pi() code acquired for us.
2964 */
Thomas Gleixner29e9ee52015-12-19 20:07:39 +00002965 put_pi_state(q.pi_state);
Darren Hart52400ba2009-04-03 13:40:49 -07002966 spin_unlock(q.lock_ptr);
2967 }
2968 } else {
Peter Zijlstra6244ffc2017-03-04 10:27:18 +01002969 struct rt_mutex *pi_mutex;
2970
Darren Hart52400ba2009-04-03 13:40:49 -07002971 /*
2972 * We have been woken up by futex_unlock_pi(), a timeout, or a
2973 * signal. futex_unlock_pi() will not destroy the lock_ptr nor
2974 * the pi_state.
2975 */
Darren Hartf27071c2012-07-20 11:53:30 -07002976 WARN_ON(!q.pi_state);
Darren Hart52400ba2009-04-03 13:40:49 -07002977 pi_mutex = &q.pi_state->pi_mutex;
Peter Zijlstrace813552017-03-22 11:35:57 +01002978 ret = rt_mutex_wait_proxy_lock(pi_mutex, to, &rt_waiter);
Darren Hart52400ba2009-04-03 13:40:49 -07002979
2980 spin_lock(q.lock_ptr);
Peter Zijlstrace813552017-03-22 11:35:57 +01002981 if (ret && !rt_mutex_cleanup_proxy_lock(pi_mutex, &rt_waiter))
2982 ret = 0;
2983
2984 debug_rt_mutex_free_waiter(&rt_waiter);
Darren Hart52400ba2009-04-03 13:40:49 -07002985 /*
2986 * Fixup the pi_state owner and possibly acquire the lock if we
2987 * haven't already.
2988 */
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002989 res = fixup_owner(uaddr2, &q, !ret);
Darren Hart52400ba2009-04-03 13:40:49 -07002990 /*
2991 * If fixup_owner() returned an error, proprogate that. If it
Darren Hart56ec1602009-09-21 22:29:59 -07002992 * acquired the lock, clear -ETIMEDOUT or -EINTR.
Darren Hart52400ba2009-04-03 13:40:49 -07002993 */
2994 if (res)
2995 ret = (res < 0) ? res : 0;
2996
2997 /* Unqueue and drop the lock. */
2998 unqueue_me_pi(&q);
2999 }
3000
Peter Zijlstra6244ffc2017-03-04 10:27:18 +01003001 if (ret == -EINTR) {
Darren Hart52400ba2009-04-03 13:40:49 -07003002 /*
Darren Hartcc6db4e2009-07-31 16:20:10 -07003003 * We've already been requeued, but cannot restart by calling
3004 * futex_lock_pi() directly. We could restart this syscall, but
3005 * it would detect that the user space "val" changed and return
3006 * -EWOULDBLOCK. Save the overhead of the restart and return
3007 * -EWOULDBLOCK directly.
Darren Hart52400ba2009-04-03 13:40:49 -07003008 */
Thomas Gleixner20708872009-05-19 23:04:59 +02003009 ret = -EWOULDBLOCK;
Darren Hart52400ba2009-04-03 13:40:49 -07003010 }
3011
3012out_put_keys:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01003013 put_futex_key(&q.key);
Thomas Gleixnerc8b15a72009-05-20 09:18:50 +02003014out_key2:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01003015 put_futex_key(&key2);
Darren Hart52400ba2009-04-03 13:40:49 -07003016
3017out:
3018 if (to) {
3019 hrtimer_cancel(&to->timer);
3020 destroy_hrtimer_on_stack(&to->timer);
3021 }
3022 return ret;
3023}
3024
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003025/*
3026 * Support for robust futexes: the kernel cleans up held futexes at
3027 * thread exit time.
3028 *
3029 * Implementation: user-space maintains a per-thread list of locks it
3030 * is holding. Upon do_exit(), the kernel carefully walks this list,
3031 * and marks all locks that are owned by this thread with the
Ingo Molnarc87e2832006-06-27 02:54:58 -07003032 * FUTEX_OWNER_DIED bit, and wakes up a waiter (if any). The list is
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003033 * always manipulated with the lock held, so the list is private and
3034 * per-thread. Userspace also maintains a per-thread 'list_op_pending'
3035 * field, to allow the kernel to clean up if the thread dies after
3036 * acquiring the lock, but just before it could have added itself to
3037 * the list. There can only be one such pending lock.
3038 */
3039
3040/**
Darren Hartd96ee562009-09-21 22:30:22 -07003041 * sys_set_robust_list() - Set the robust-futex list head of a task
3042 * @head: pointer to the list-head
3043 * @len: length of the list-head, as userspace expects
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003044 */
Heiko Carstens836f92a2009-01-14 14:14:33 +01003045SYSCALL_DEFINE2(set_robust_list, struct robust_list_head __user *, head,
3046 size_t, len)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003047{
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08003048 if (!futex_cmpxchg_enabled)
3049 return -ENOSYS;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003050 /*
3051 * The kernel knows only one size for now:
3052 */
3053 if (unlikely(len != sizeof(*head)))
3054 return -EINVAL;
3055
3056 current->robust_list = head;
3057
3058 return 0;
3059}
3060
3061/**
Darren Hartd96ee562009-09-21 22:30:22 -07003062 * sys_get_robust_list() - Get the robust-futex list head of a task
3063 * @pid: pid of the process [zero for current task]
3064 * @head_ptr: pointer to a list-head pointer, the kernel fills it in
3065 * @len_ptr: pointer to a length field, the kernel fills in the header size
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003066 */
Heiko Carstens836f92a2009-01-14 14:14:33 +01003067SYSCALL_DEFINE3(get_robust_list, int, pid,
3068 struct robust_list_head __user * __user *, head_ptr,
3069 size_t __user *, len_ptr)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003070{
Al Viroba46df92006-10-10 22:46:07 +01003071 struct robust_list_head __user *head;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003072 unsigned long ret;
Kees Cookbdbb7762012-03-19 16:12:53 -07003073 struct task_struct *p;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003074
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08003075 if (!futex_cmpxchg_enabled)
3076 return -ENOSYS;
3077
Kees Cookbdbb7762012-03-19 16:12:53 -07003078 rcu_read_lock();
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003079
Kees Cookbdbb7762012-03-19 16:12:53 -07003080 ret = -ESRCH;
3081 if (!pid)
3082 p = current;
3083 else {
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07003084 p = find_task_by_vpid(pid);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003085 if (!p)
3086 goto err_unlock;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003087 }
3088
Kees Cookbdbb7762012-03-19 16:12:53 -07003089 ret = -EPERM;
Jann Horncaaee622016-01-20 15:00:04 -08003090 if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS))
Kees Cookbdbb7762012-03-19 16:12:53 -07003091 goto err_unlock;
3092
3093 head = p->robust_list;
3094 rcu_read_unlock();
3095
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003096 if (put_user(sizeof(*head), len_ptr))
3097 return -EFAULT;
3098 return put_user(head, head_ptr);
3099
3100err_unlock:
Oleg Nesterovaaa2a972006-09-29 02:00:55 -07003101 rcu_read_unlock();
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003102
3103 return ret;
3104}
3105
3106/*
3107 * Process a futex-list entry, check whether it's owned by the
3108 * dying task, and do notification if so:
3109 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003110int handle_futex_death(u32 __user *uaddr, struct task_struct *curr, int pi)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003111{
Vitaliy Ivanov7cfdaf32011-07-07 15:10:31 +03003112 u32 uval, uninitialized_var(nval), mval;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003113
Chen Jie726c28f2019-03-15 03:44:38 +00003114 /* Futex address must be 32bit aligned */
3115 if ((((unsigned long)uaddr) % sizeof(*uaddr)) != 0)
3116 return -1;
3117
Ingo Molnar8f17d3a2006-03-27 01:16:27 -08003118retry:
3119 if (get_user(uval, uaddr))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003120 return -1;
3121
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003122 if ((uval & FUTEX_TID_MASK) == task_pid_vnr(curr)) {
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003123 /*
3124 * Ok, this dying thread is truly holding a futex
3125 * of interest. Set the OWNER_DIED bit atomically
3126 * via cmpxchg, and if the value had FUTEX_WAITERS
3127 * set, wake up a waiter (if any). (We have to do a
3128 * futex_wake() even if OWNER_DIED is already set -
3129 * to handle the rare but possible case of recursive
3130 * thread-death.) The rest of the cleanup is done in
3131 * userspace.
3132 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003133 mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
Thomas Gleixner6e0aa9f2011-03-14 10:34:35 +01003134 /*
3135 * We are not holding a lock here, but we want to have
3136 * the pagefault_disable/enable() protection because
3137 * we want to handle the fault gracefully. If the
3138 * access fails we try to fault in the futex with R/W
3139 * verification via get_user_pages. get_user() above
3140 * does not guarantee R/W access. If that fails we
3141 * give up and leave the futex locked.
3142 */
3143 if (cmpxchg_futex_value_locked(&nval, uaddr, uval, mval)) {
3144 if (fault_in_user_writeable(uaddr))
3145 return -1;
3146 goto retry;
3147 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07003148 if (nval != uval)
Ingo Molnar8f17d3a2006-03-27 01:16:27 -08003149 goto retry;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003150
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003151 /*
3152 * Wake robust non-PI futexes here. The wakeup of
3153 * PI futexes happens in exit_pi_state():
3154 */
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07003155 if (!pi && (uval & FUTEX_WAITERS))
Peter Zijlstrac2f9f202008-09-26 19:32:23 +02003156 futex_wake(uaddr, 1, 1, FUTEX_BITSET_MATCH_ANY);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003157 }
3158 return 0;
3159}
3160
3161/*
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003162 * Fetch a robust-list pointer. Bit 0 signals PI futexes:
3163 */
3164static inline int fetch_robust_entry(struct robust_list __user **entry,
Al Viroba46df92006-10-10 22:46:07 +01003165 struct robust_list __user * __user *head,
Namhyung Kim1dcc41b2010-09-14 21:43:46 +09003166 unsigned int *pi)
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003167{
3168 unsigned long uentry;
3169
Al Viroba46df92006-10-10 22:46:07 +01003170 if (get_user(uentry, (unsigned long __user *)head))
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003171 return -EFAULT;
3172
Al Viroba46df92006-10-10 22:46:07 +01003173 *entry = (void __user *)(uentry & ~1UL);
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003174 *pi = uentry & 1;
3175
3176 return 0;
3177}
3178
3179/*
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003180 * Walk curr->robust_list (very carefully, it's a userspace list!)
3181 * and mark any locks found there dead, and notify any waiters.
3182 *
3183 * We silently return on any sign of list-walking problem.
3184 */
3185void exit_robust_list(struct task_struct *curr)
3186{
3187 struct robust_list_head __user *head = curr->robust_list;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003188 struct robust_list __user *entry, *next_entry, *pending;
Darren Hart4c115e92010-11-04 15:00:00 -04003189 unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
3190 unsigned int uninitialized_var(next_pi);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003191 unsigned long futex_offset;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003192 int rc;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003193
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08003194 if (!futex_cmpxchg_enabled)
3195 return;
3196
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003197 /*
3198 * Fetch the list head (which was registered earlier, via
3199 * sys_set_robust_list()):
3200 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003201 if (fetch_robust_entry(&entry, &head->list.next, &pi))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003202 return;
3203 /*
3204 * Fetch the relative futex offset:
3205 */
3206 if (get_user(futex_offset, &head->futex_offset))
3207 return;
3208 /*
3209 * Fetch any possibly pending lock-add first, and handle it
3210 * if it exists:
3211 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003212 if (fetch_robust_entry(&pending, &head->list_op_pending, &pip))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003213 return;
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003214
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003215 next_entry = NULL; /* avoid warning with gcc */
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003216 while (entry != &head->list) {
3217 /*
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003218 * Fetch the next entry in the list before calling
3219 * handle_futex_death:
3220 */
3221 rc = fetch_robust_entry(&next_entry, &entry->next, &next_pi);
3222 /*
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003223 * A pending lock might already be on the list, so
Ingo Molnarc87e2832006-06-27 02:54:58 -07003224 * don't process it twice:
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003225 */
3226 if (entry != pending)
Al Viroba46df92006-10-10 22:46:07 +01003227 if (handle_futex_death((void __user *)entry + futex_offset,
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003228 curr, pi))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003229 return;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003230 if (rc)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003231 return;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003232 entry = next_entry;
3233 pi = next_pi;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003234 /*
3235 * Avoid excessively long or circular lists:
3236 */
3237 if (!--limit)
3238 break;
3239
3240 cond_resched();
3241 }
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003242
3243 if (pending)
3244 handle_futex_death((void __user *)pending + futex_offset,
3245 curr, pip);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003246}
3247
Pierre Peifferc19384b2007-05-09 02:35:02 -07003248long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
Ingo Molnare2970f22006-06-27 02:54:47 -07003249 u32 __user *uaddr2, u32 val2, u32 val3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250{
Thomas Gleixner81b40532012-02-15 12:17:09 +01003251 int cmd = op & FUTEX_CMD_MASK;
Darren Hartb41277d2010-11-08 13:10:09 -08003252 unsigned int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003254 if (!(op & FUTEX_PRIVATE_FLAG))
Darren Hartb41277d2010-11-08 13:10:09 -08003255 flags |= FLAGS_SHARED;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003256
Darren Hartb41277d2010-11-08 13:10:09 -08003257 if (op & FUTEX_CLOCK_REALTIME) {
3258 flags |= FLAGS_CLOCKRT;
Darren Hart337f1302015-12-18 13:36:37 -08003259 if (cmd != FUTEX_WAIT && cmd != FUTEX_WAIT_BITSET && \
3260 cmd != FUTEX_WAIT_REQUEUE_PI)
Darren Hartb41277d2010-11-08 13:10:09 -08003261 return -ENOSYS;
3262 }
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003263
3264 switch (cmd) {
Thomas Gleixner59263b52012-02-15 12:08:34 +01003265 case FUTEX_LOCK_PI:
3266 case FUTEX_UNLOCK_PI:
3267 case FUTEX_TRYLOCK_PI:
3268 case FUTEX_WAIT_REQUEUE_PI:
3269 case FUTEX_CMP_REQUEUE_PI:
3270 if (!futex_cmpxchg_enabled)
3271 return -ENOSYS;
3272 }
3273
3274 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 case FUTEX_WAIT:
Thomas Gleixnercd689982008-02-01 17:45:14 +01003276 val3 = FUTEX_BITSET_MATCH_ANY;
3277 case FUTEX_WAIT_BITSET:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003278 return futex_wait(uaddr, flags, val, timeout, val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 case FUTEX_WAKE:
Thomas Gleixnercd689982008-02-01 17:45:14 +01003280 val3 = FUTEX_BITSET_MATCH_ANY;
3281 case FUTEX_WAKE_BITSET:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003282 return futex_wake(uaddr, flags, val, val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 case FUTEX_REQUEUE:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003284 return futex_requeue(uaddr, flags, uaddr2, val, val2, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 case FUTEX_CMP_REQUEUE:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003286 return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 0);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07003287 case FUTEX_WAKE_OP:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003288 return futex_wake_op(uaddr, flags, uaddr2, val, val2, val3);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003289 case FUTEX_LOCK_PI:
Michael Kerrisk996636d2015-01-16 20:28:06 +01003290 return futex_lock_pi(uaddr, flags, timeout, 0);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003291 case FUTEX_UNLOCK_PI:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003292 return futex_unlock_pi(uaddr, flags);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003293 case FUTEX_TRYLOCK_PI:
Michael Kerrisk996636d2015-01-16 20:28:06 +01003294 return futex_lock_pi(uaddr, flags, NULL, 1);
Darren Hart52400ba2009-04-03 13:40:49 -07003295 case FUTEX_WAIT_REQUEUE_PI:
3296 val3 = FUTEX_BITSET_MATCH_ANY;
Thomas Gleixner81b40532012-02-15 12:17:09 +01003297 return futex_wait_requeue_pi(uaddr, flags, val, timeout, val3,
3298 uaddr2);
Darren Hart52400ba2009-04-03 13:40:49 -07003299 case FUTEX_CMP_REQUEUE_PI:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003300 return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 }
Thomas Gleixner81b40532012-02-15 12:17:09 +01003302 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303}
3304
3305
Heiko Carstens17da2bd2009-01-14 14:14:10 +01003306SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val,
3307 struct timespec __user *, utime, u32 __user *, uaddr2,
3308 u32, val3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309{
Pierre Peifferc19384b2007-05-09 02:35:02 -07003310 struct timespec ts;
3311 ktime_t t, *tp = NULL;
Ingo Molnare2970f22006-06-27 02:54:47 -07003312 u32 val2 = 0;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003313 int cmd = op & FUTEX_CMD_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314
Thomas Gleixnercd689982008-02-01 17:45:14 +01003315 if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI ||
Darren Hart52400ba2009-04-03 13:40:49 -07003316 cmd == FUTEX_WAIT_BITSET ||
3317 cmd == FUTEX_WAIT_REQUEUE_PI)) {
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07003318 if (unlikely(should_fail_futex(!(op & FUTEX_PRIVATE_FLAG))))
3319 return -EFAULT;
Pierre Peifferc19384b2007-05-09 02:35:02 -07003320 if (copy_from_user(&ts, utime, sizeof(ts)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 return -EFAULT;
Pierre Peifferc19384b2007-05-09 02:35:02 -07003322 if (!timespec_valid(&ts))
Thomas Gleixner9741ef962006-03-31 02:31:32 -08003323 return -EINVAL;
Pierre Peifferc19384b2007-05-09 02:35:02 -07003324
3325 t = timespec_to_ktime(ts);
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003326 if (cmd == FUTEX_WAIT)
Thomas Gleixner5a7780e2008-02-13 09:20:43 +01003327 t = ktime_add_safe(ktime_get(), t);
Pierre Peifferc19384b2007-05-09 02:35:02 -07003328 tp = &t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 }
3330 /*
Darren Hart52400ba2009-04-03 13:40:49 -07003331 * requeue parameter in 'utime' if cmd == FUTEX_*_REQUEUE_*.
Andreas Schwabf54f0982007-07-31 00:38:51 -07003332 * number of waiters to wake in 'utime' if cmd == FUTEX_WAKE_OP.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 */
Andreas Schwabf54f0982007-07-31 00:38:51 -07003334 if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE ||
Darren Hartba9c22f2009-04-20 22:22:22 -07003335 cmd == FUTEX_CMP_REQUEUE_PI || cmd == FUTEX_WAKE_OP)
Ingo Molnare2970f22006-06-27 02:54:47 -07003336 val2 = (u32) (unsigned long) utime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337
Pierre Peifferc19384b2007-05-09 02:35:02 -07003338 return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339}
3340
Heiko Carstens03b8c7b2014-03-02 13:09:47 +01003341static void __init futex_detect_cmpxchg(void)
3342{
3343#ifndef CONFIG_HAVE_FUTEX_CMPXCHG
3344 u32 curval;
3345
3346 /*
3347 * This will fail and we want it. Some arch implementations do
3348 * runtime detection of the futex_atomic_cmpxchg_inatomic()
3349 * functionality. We want to know that before we call in any
3350 * of the complex code paths. Also we want to prevent
3351 * registration of robust lists in that case. NULL is
3352 * guaranteed to fault and we get -EFAULT on functional
3353 * implementation, the non-functional ones will return
3354 * -ENOSYS.
3355 */
3356 if (cmpxchg_futex_value_locked(&curval, NULL, 0, 0) == -EFAULT)
3357 futex_cmpxchg_enabled = 1;
3358#endif
3359}
3360
Benjamin Herrenschmidtf6d107f2008-03-27 14:52:15 +11003361static int __init futex_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362{
Heiko Carstens63b1a812014-01-16 14:54:50 +01003363 unsigned int futex_shift;
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -08003364 unsigned long i;
3365
3366#if CONFIG_BASE_SMALL
3367 futex_hashsize = 16;
3368#else
3369 futex_hashsize = roundup_pow_of_two(256 * num_possible_cpus());
3370#endif
3371
3372 futex_queues = alloc_large_system_hash("futex", sizeof(*futex_queues),
3373 futex_hashsize, 0,
3374 futex_hashsize < 256 ? HASH_SMALL : 0,
Heiko Carstens63b1a812014-01-16 14:54:50 +01003375 &futex_shift, NULL,
3376 futex_hashsize, futex_hashsize);
3377 futex_hashsize = 1UL << futex_shift;
Heiko Carstens03b8c7b2014-03-02 13:09:47 +01003378
3379 futex_detect_cmpxchg();
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08003380
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -08003381 for (i = 0; i < futex_hashsize; i++) {
Linus Torvalds11d46162014-03-20 22:11:17 -07003382 atomic_set(&futex_queues[i].waiters, 0);
Dima Zavin732375c2011-07-07 17:27:59 -07003383 plist_head_init(&futex_queues[i].chain);
Thomas Gleixner3e4ab742008-02-23 15:23:55 -08003384 spin_lock_init(&futex_queues[i].lock);
3385 }
3386
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 return 0;
3388}
Yang Yang808de342016-12-30 16:17:55 +08003389core_initcall(futex_init);