blob: 6e443efc65f41e8cd1d0bdf2d31afc63207390c1 [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 Buesob0c29f72014-01-12 15:31:25 -0800127 * 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 * `-------> 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 */
182#define FLAGS_SHARED 0x01
183#define FLAGS_CLOCKRT 0x02
184#define FLAGS_HAS_TIMEOUT 0x04
185
186/*
Ingo Molnarc87e2832006-06-27 02:54:58 -0700187 * Priority Inheritance state:
188 */
189struct futex_pi_state {
190 /*
191 * list of 'owned' pi_state instances - these have to be
192 * cleaned up in do_exit() if the task exits prematurely:
193 */
194 struct list_head list;
195
196 /*
197 * The PI object:
198 */
199 struct rt_mutex pi_mutex;
200
201 struct task_struct *owner;
202 atomic_t refcount;
203
204 union futex_key key;
205};
206
Darren Hartd8d88fb2009-09-21 22:30:30 -0700207/**
208 * struct futex_q - The hashed futex queue entry, one per waiting task
Randy Dunlapfb62db22010-10-13 11:02:34 -0700209 * @list: priority-sorted list of tasks waiting on this futex
Darren Hartd8d88fb2009-09-21 22:30:30 -0700210 * @task: the task waiting on the futex
211 * @lock_ptr: the hash bucket lock
212 * @key: the key the futex is hashed on
213 * @pi_state: optional priority inheritance state
214 * @rt_waiter: rt_waiter storage for use with requeue_pi
215 * @requeue_pi_key: the requeue_pi target futex key
216 * @bitset: bitset for the optional bitmasked wakeup
217 *
218 * We use this hashed waitqueue, instead of a normal wait_queue_t, so
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 * we can wake only the relevant ones (hashed queues may be shared).
220 *
221 * A futex_q has a woken state, just like tasks have TASK_RUNNING.
Pierre Peifferec92d082007-05-09 02:35:00 -0700222 * It is considered woken when plist_node_empty(&q->list) || q->lock_ptr == 0.
Randy Dunlapfb62db22010-10-13 11:02:34 -0700223 * The order of wakeup is always to make the first condition true, then
Darren Hartd8d88fb2009-09-21 22:30:30 -0700224 * the second.
225 *
226 * PI futexes are typically woken before they are removed from the hash list via
227 * the rt_mutex code. See unqueue_me_pi().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 */
229struct futex_q {
Pierre Peifferec92d082007-05-09 02:35:00 -0700230 struct plist_node list;
Darren Hartd8d88fb2009-09-21 22:30:30 -0700231
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +0200232 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 spinlock_t *lock_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 union futex_key key;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700235 struct futex_pi_state *pi_state;
Darren Hart52400ba2009-04-03 13:40:49 -0700236 struct rt_mutex_waiter *rt_waiter;
Darren Hart84bc4af2009-08-13 17:36:53 -0700237 union futex_key *requeue_pi_key;
Thomas Gleixnercd689982008-02-01 17:45:14 +0100238 u32 bitset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239};
240
Darren Hart5bdb05f2010-11-08 13:40:28 -0800241static const struct futex_q futex_q_init = {
242 /* list gets initialized in queue_me()*/
243 .key = FUTEX_KEY_INIT,
244 .bitset = FUTEX_BITSET_MATCH_ANY
245};
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247/*
Darren Hartb2d09942009-03-12 00:55:37 -0700248 * Hash buckets are shared by all the futex_keys that hash to the same
249 * location. Each key may have multiple futex_q structures, one for each task
250 * waiting on a futex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 */
252struct futex_hash_bucket {
Linus Torvalds11d46162014-03-20 22:11:17 -0700253 atomic_t waiters;
Pierre Peifferec92d082007-05-09 02:35:00 -0700254 spinlock_t lock;
255 struct plist_head chain;
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -0800256} ____cacheline_aligned_in_smp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -0800258static unsigned long __read_mostly futex_hashsize;
259
260static struct futex_hash_bucket *futex_queues;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Davidlohr Buesoab51fba2015-06-29 23:26:02 -0700262/*
263 * Fault injections for futexes.
264 */
265#ifdef CONFIG_FAIL_FUTEX
266
267static struct {
268 struct fault_attr attr;
269
270 u32 ignore_private;
271} fail_futex = {
272 .attr = FAULT_ATTR_INITIALIZER,
273 .ignore_private = 0,
274};
275
276static int __init setup_fail_futex(char *str)
277{
278 return setup_fault_attr(&fail_futex.attr, str);
279}
280__setup("fail_futex=", setup_fail_futex);
281
kbuild test robot5d285a72015-07-21 01:40:45 +0800282static bool should_fail_futex(bool fshared)
Davidlohr Buesoab51fba2015-06-29 23:26:02 -0700283{
284 if (fail_futex.ignore_private && !fshared)
285 return false;
286
287 return should_fail(&fail_futex.attr, 1);
288}
289
290#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
291
292static int __init fail_futex_debugfs(void)
293{
294 umode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
295 struct dentry *dir;
296
297 dir = fault_create_debugfs_attr("fail_futex", NULL,
298 &fail_futex.attr);
299 if (IS_ERR(dir))
300 return PTR_ERR(dir);
301
302 if (!debugfs_create_bool("ignore-private", mode, dir,
303 &fail_futex.ignore_private)) {
304 debugfs_remove_recursive(dir);
305 return -ENOMEM;
306 }
307
308 return 0;
309}
310
311late_initcall(fail_futex_debugfs);
312
313#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
314
315#else
316static inline bool should_fail_futex(bool fshared)
317{
318 return false;
319}
320#endif /* CONFIG_FAIL_FUTEX */
321
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800322static inline void futex_get_mm(union futex_key *key)
323{
324 atomic_inc(&key->private.mm->mm_count);
325 /*
326 * Ensure futex_get_mm() implies a full barrier such that
327 * get_futex_key() implies a full barrier. This is relied upon
328 * as full barrier (B), see the ordering comment above.
329 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100330 smp_mb__after_atomic();
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800331}
332
Linus Torvalds11d46162014-03-20 22:11:17 -0700333/*
334 * Reflects a new waiter being added to the waitqueue.
335 */
336static inline void hb_waiters_inc(struct futex_hash_bucket *hb)
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800337{
338#ifdef CONFIG_SMP
Linus Torvalds11d46162014-03-20 22:11:17 -0700339 atomic_inc(&hb->waiters);
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800340 /*
Linus Torvalds11d46162014-03-20 22:11:17 -0700341 * Full barrier (A), see the ordering comment above.
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800342 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100343 smp_mb__after_atomic();
Linus Torvalds11d46162014-03-20 22:11:17 -0700344#endif
345}
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800346
Linus Torvalds11d46162014-03-20 22:11:17 -0700347/*
348 * Reflects a waiter being removed from the waitqueue by wakeup
349 * paths.
350 */
351static inline void hb_waiters_dec(struct futex_hash_bucket *hb)
352{
353#ifdef CONFIG_SMP
354 atomic_dec(&hb->waiters);
355#endif
356}
357
358static inline int hb_waiters_pending(struct futex_hash_bucket *hb)
359{
360#ifdef CONFIG_SMP
361 return atomic_read(&hb->waiters);
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800362#else
Linus Torvalds11d46162014-03-20 22:11:17 -0700363 return 1;
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800364#endif
365}
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367/*
368 * We hash on the keys returned from get_futex_key (see below).
369 */
370static struct futex_hash_bucket *hash_futex(union futex_key *key)
371{
372 u32 hash = jhash2((u32*)&key->both.word,
373 (sizeof(key->both.word)+sizeof(key->both.ptr))/4,
374 key->both.offset);
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -0800375 return &futex_queues[hash & (futex_hashsize - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
378/*
379 * Return 1 if two futex_keys are equal, 0 otherwise.
380 */
381static inline int match_futex(union futex_key *key1, union futex_key *key2)
382{
Darren Hart2bc87202009-10-14 10:12:39 -0700383 return (key1 && key2
384 && key1->both.word == key2->both.word
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 && key1->both.ptr == key2->both.ptr
386 && key1->both.offset == key2->both.offset);
387}
388
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200389/*
390 * Take a reference to the resource addressed by a key.
391 * Can be called while holding spinlocks.
392 *
393 */
394static void get_futex_key_refs(union futex_key *key)
395{
396 if (!key->both.ptr)
397 return;
398
399 switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
400 case FUT_OFF_INODE:
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800401 ihold(key->shared.inode); /* implies MB (B) */
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200402 break;
403 case FUT_OFF_MMSHARED:
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800404 futex_get_mm(key); /* implies MB (B) */
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200405 break;
Catalin Marinas76835b0e2014-10-17 17:38:49 +0100406 default:
Davidlohr Bueso993b2ff2014-10-23 20:27:00 -0700407 /*
408 * Private futexes do not hold reference on an inode or
409 * mm, therefore the only purpose of calling get_futex_key_refs
410 * is because we need the barrier for the lockless waiter check.
411 */
Catalin Marinas76835b0e2014-10-17 17:38:49 +0100412 smp_mb(); /* explicit MB (B) */
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200413 }
414}
415
416/*
417 * Drop a reference to the resource addressed by a key.
Davidlohr Bueso993b2ff2014-10-23 20:27:00 -0700418 * The hash bucket spinlock must not be held. This is
419 * a no-op for private futexes, see comment in the get
420 * counterpart.
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200421 */
422static void drop_futex_key_refs(union futex_key *key)
423{
Darren Hart90621c42008-12-29 19:43:21 -0800424 if (!key->both.ptr) {
425 /* If we're here then we tried to put a key we failed to get */
426 WARN_ON_ONCE(1);
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200427 return;
Darren Hart90621c42008-12-29 19:43:21 -0800428 }
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200429
430 switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
431 case FUT_OFF_INODE:
432 iput(key->shared.inode);
433 break;
434 case FUT_OFF_MMSHARED:
435 mmdrop(key->private.mm);
436 break;
437 }
438}
439
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700440/**
Darren Hartd96ee562009-09-21 22:30:22 -0700441 * get_futex_key() - Get parameters which are the keys for a futex
442 * @uaddr: virtual address of the futex
443 * @fshared: 0 for a PROCESS_PRIVATE futex, 1 for PROCESS_SHARED
444 * @key: address where result is stored.
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500445 * @rw: mapping needs to be read/write (values: VERIFY_READ,
446 * VERIFY_WRITE)
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700447 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -0800448 * Return: a negative error code or 0
449 *
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700450 * The key words are stored in *key on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 *
Al Viro6131ffa2013-02-27 16:59:05 -0500452 * For shared mappings, it's (page->index, file_inode(vma->vm_file),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 * offset_within_page). For private mappings, it's (uaddr, current->mm).
454 * We can usually work out the index without swapping in the page.
455 *
Darren Hartb2d09942009-03-12 00:55:37 -0700456 * lock_page() might sleep, the caller should not hold a spinlock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 */
Thomas Gleixner64d13042009-05-18 21:20:10 +0200458static int
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500459get_futex_key(u32 __user *uaddr, int fshared, union futex_key *key, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Ingo Molnare2970f22006-06-27 02:54:47 -0700461 unsigned long address = (unsigned long)uaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 struct mm_struct *mm = current->mm;
Andrea Arcangelia5b338f2011-01-13 15:46:34 -0800463 struct page *page, *page_head;
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500464 int err, ro = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 /*
467 * The futex address must be "naturally" aligned.
468 */
Ingo Molnare2970f22006-06-27 02:54:47 -0700469 key->both.offset = address % PAGE_SIZE;
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700470 if (unlikely((address % sizeof(u32)) != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return -EINVAL;
Ingo Molnare2970f22006-06-27 02:54:47 -0700472 address -= key->both.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Linus Torvalds5cdec2d2013-12-12 09:53:51 -0800474 if (unlikely(!access_ok(rw, uaddr, sizeof(u32))))
475 return -EFAULT;
476
Davidlohr Buesoab51fba2015-06-29 23:26:02 -0700477 if (unlikely(should_fail_futex(fshared)))
478 return -EFAULT;
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 /*
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700481 * PROCESS_PRIVATE futexes are fast.
482 * As the mm cannot disappear under us and the 'key' only needs
483 * virtual address, we dont even have to find the underlying vma.
484 * Note : We do have to check 'uaddr' is a valid user address,
485 * but access_ok() should be faster than find_vma()
486 */
487 if (!fshared) {
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700488 key->private.mm = mm;
489 key->private.address = address;
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800490 get_futex_key_refs(key); /* implies MB (B) */
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700491 return 0;
492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200494again:
Davidlohr Buesoab51fba2015-06-29 23:26:02 -0700495 /* Ignore any VERIFY_READ mapping (futex common case) */
496 if (unlikely(should_fail_futex(fshared)))
497 return -EFAULT;
498
KOSAKI Motohiro7485d0d2010-01-05 16:32:43 +0900499 err = get_user_pages_fast(address, 1, 1, &page);
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500500 /*
501 * If write access is not required (eg. FUTEX_WAIT), try
502 * and get read-only access.
503 */
504 if (err == -EFAULT && rw == VERIFY_READ) {
505 err = get_user_pages_fast(address, 1, 0, &page);
506 ro = 1;
507 }
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200508 if (err < 0)
509 return err;
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500510 else
511 err = 0;
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200512
Andrea Arcangelia5b338f2011-01-13 15:46:34 -0800513#ifdef CONFIG_TRANSPARENT_HUGEPAGE
514 page_head = page;
515 if (unlikely(PageTail(page))) {
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200516 put_page(page);
Andrea Arcangelia5b338f2011-01-13 15:46:34 -0800517 /* serialize against __split_huge_page_splitting() */
518 local_irq_disable();
Linus Torvaldsf12d5bf2013-12-12 09:38:42 -0800519 if (likely(__get_user_pages_fast(address, 1, !ro, &page) == 1)) {
Andrea Arcangelia5b338f2011-01-13 15:46:34 -0800520 page_head = compound_head(page);
521 /*
522 * page_head is valid pointer but we must pin
523 * it before taking the PG_lock and/or
524 * PG_compound_lock. The moment we re-enable
525 * irqs __split_huge_page_splitting() can
526 * return and the head page can be freed from
527 * under us. We can't take the PG_lock and/or
528 * PG_compound_lock on a page that could be
529 * freed from under us.
530 */
531 if (page != page_head) {
532 get_page(page_head);
533 put_page(page);
534 }
535 local_irq_enable();
536 } else {
537 local_irq_enable();
538 goto again;
539 }
540 }
541#else
542 page_head = compound_head(page);
543 if (page != page_head) {
544 get_page(page_head);
545 put_page(page);
546 }
547#endif
548
549 lock_page(page_head);
Hugh Dickinse6780f72011-12-31 11:44:01 -0800550
551 /*
552 * If page_head->mapping is NULL, then it cannot be a PageAnon
553 * page; but it might be the ZERO_PAGE or in the gate area or
554 * in a special mapping (all cases which we are happy to fail);
555 * or it may have been a good file page when get_user_pages_fast
556 * found it, but truncated or holepunched or subjected to
557 * invalidate_complete_page2 before we got the page lock (also
558 * cases which we are happy to fail). And we hold a reference,
559 * so refcount care in invalidate_complete_page's remove_mapping
560 * prevents drop_caches from setting mapping to NULL beneath us.
561 *
562 * The case we do have to guard against is when memory pressure made
563 * shmem_writepage move it from filecache to swapcache beneath us:
564 * an unlikely race, but we do need to retry for page_head->mapping.
565 */
Andrea Arcangelia5b338f2011-01-13 15:46:34 -0800566 if (!page_head->mapping) {
Hugh Dickinse6780f72011-12-31 11:44:01 -0800567 int shmem_swizzled = PageSwapCache(page_head);
Andrea Arcangelia5b338f2011-01-13 15:46:34 -0800568 unlock_page(page_head);
569 put_page(page_head);
Hugh Dickinse6780f72011-12-31 11:44:01 -0800570 if (shmem_swizzled)
571 goto again;
572 return -EFAULT;
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 /*
576 * Private mappings are handled in a simple way.
577 *
578 * NOTE: When userspace waits on a MAP_SHARED mapping, even if
579 * it's a read-only handle, it's expected that futexes attach to
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200580 * the object not the particular process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 */
Andrea Arcangelia5b338f2011-01-13 15:46:34 -0800582 if (PageAnon(page_head)) {
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500583 /*
584 * A RO anonymous page will never change and thus doesn't make
585 * sense for futex operations.
586 */
Davidlohr Buesoab51fba2015-06-29 23:26:02 -0700587 if (unlikely(should_fail_futex(fshared)) || ro) {
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500588 err = -EFAULT;
589 goto out;
590 }
591
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200592 key->both.offset |= FUT_OFF_MMSHARED; /* ref taken on mm */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 key->private.mm = mm;
Ingo Molnare2970f22006-06-27 02:54:47 -0700594 key->private.address = address;
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200595 } else {
596 key->both.offset |= FUT_OFF_INODE; /* inode-based key */
Andrea Arcangelia5b338f2011-01-13 15:46:34 -0800597 key->shared.inode = page_head->mapping->host;
Zhang Yi13d60f42013-06-25 21:19:31 +0800598 key->shared.pgoff = basepage_index(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
600
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800601 get_futex_key_refs(key); /* implies MB (B) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500603out:
Andrea Arcangelia5b338f2011-01-13 15:46:34 -0800604 unlock_page(page_head);
605 put_page(page_head);
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500606 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
Thomas Gleixnerae791a22010-11-10 13:30:36 +0100609static inline void put_futex_key(union futex_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200611 drop_futex_key_refs(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
Darren Hartd96ee562009-09-21 22:30:22 -0700614/**
615 * fault_in_user_writeable() - Fault in user address and verify RW access
Thomas Gleixnerd0725992009-06-11 23:15:43 +0200616 * @uaddr: pointer to faulting user space address
617 *
618 * Slow path to fixup the fault we just took in the atomic write
619 * access to @uaddr.
620 *
Randy Dunlapfb62db22010-10-13 11:02:34 -0700621 * We have no generic implementation of a non-destructive write to the
Thomas Gleixnerd0725992009-06-11 23:15:43 +0200622 * user address. We know that we faulted in the atomic pagefault
623 * disabled section so we can as well avoid the #PF overhead by
624 * calling get_user_pages() right away.
625 */
626static int fault_in_user_writeable(u32 __user *uaddr)
627{
Andi Kleen722d0172009-12-08 13:19:42 +0100628 struct mm_struct *mm = current->mm;
629 int ret;
630
631 down_read(&mm->mmap_sem);
Benjamin Herrenschmidt2efaca92011-07-25 17:12:32 -0700632 ret = fixup_user_fault(current, mm, (unsigned long)uaddr,
633 FAULT_FLAG_WRITE);
Andi Kleen722d0172009-12-08 13:19:42 +0100634 up_read(&mm->mmap_sem);
635
Thomas Gleixnerd0725992009-06-11 23:15:43 +0200636 return ret < 0 ? ret : 0;
637}
638
Darren Hart4b1c4862009-04-03 13:39:42 -0700639/**
640 * futex_top_waiter() - Return the highest priority waiter on a futex
Darren Hartd96ee562009-09-21 22:30:22 -0700641 * @hb: the hash bucket the futex_q's reside in
642 * @key: the futex key (to distinguish it from other futex futex_q's)
Darren Hart4b1c4862009-04-03 13:39:42 -0700643 *
644 * Must be called with the hb lock held.
645 */
646static struct futex_q *futex_top_waiter(struct futex_hash_bucket *hb,
647 union futex_key *key)
648{
649 struct futex_q *this;
650
651 plist_for_each_entry(this, &hb->chain, list) {
652 if (match_futex(&this->key, key))
653 return this;
654 }
655 return NULL;
656}
657
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800658static int cmpxchg_futex_value_locked(u32 *curval, u32 __user *uaddr,
659 u32 uval, u32 newval)
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700660{
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800661 int ret;
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700662
663 pagefault_disable();
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800664 ret = futex_atomic_cmpxchg_inatomic(curval, uaddr, uval, newval);
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700665 pagefault_enable();
666
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800667 return ret;
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700668}
669
670static int get_futex_value_locked(u32 *dest, u32 __user *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 int ret;
673
Peter Zijlstraa8663742006-12-06 20:32:20 -0800674 pagefault_disable();
Ingo Molnare2970f22006-06-27 02:54:47 -0700675 ret = __copy_from_user_inatomic(dest, from, sizeof(u32));
Peter Zijlstraa8663742006-12-06 20:32:20 -0800676 pagefault_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 return ret ? -EFAULT : 0;
679}
680
Ingo Molnarc87e2832006-06-27 02:54:58 -0700681
682/*
683 * PI code:
684 */
685static int refill_pi_state_cache(void)
686{
687 struct futex_pi_state *pi_state;
688
689 if (likely(current->pi_state_cache))
690 return 0;
691
Burman Yan4668edc2006-12-06 20:38:51 -0800692 pi_state = kzalloc(sizeof(*pi_state), GFP_KERNEL);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700693
694 if (!pi_state)
695 return -ENOMEM;
696
Ingo Molnarc87e2832006-06-27 02:54:58 -0700697 INIT_LIST_HEAD(&pi_state->list);
698 /* pi_mutex gets initialized later */
699 pi_state->owner = NULL;
700 atomic_set(&pi_state->refcount, 1);
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200701 pi_state->key = FUTEX_KEY_INIT;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700702
703 current->pi_state_cache = pi_state;
704
705 return 0;
706}
707
708static struct futex_pi_state * alloc_pi_state(void)
709{
710 struct futex_pi_state *pi_state = current->pi_state_cache;
711
712 WARN_ON(!pi_state);
713 current->pi_state_cache = NULL;
714
715 return pi_state;
716}
717
Brian Silverman30a6b802014-10-25 20:20:37 -0400718/*
719 * Must be called with the hb lock held.
720 */
Ingo Molnarc87e2832006-06-27 02:54:58 -0700721static void free_pi_state(struct futex_pi_state *pi_state)
722{
Brian Silverman30a6b802014-10-25 20:20:37 -0400723 if (!pi_state)
724 return;
725
Ingo Molnarc87e2832006-06-27 02:54:58 -0700726 if (!atomic_dec_and_test(&pi_state->refcount))
727 return;
728
729 /*
730 * If pi_state->owner is NULL, the owner is most probably dying
731 * and has cleaned up the pi_state already
732 */
733 if (pi_state->owner) {
Thomas Gleixner1d615482009-11-17 14:54:03 +0100734 raw_spin_lock_irq(&pi_state->owner->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700735 list_del_init(&pi_state->list);
Thomas Gleixner1d615482009-11-17 14:54:03 +0100736 raw_spin_unlock_irq(&pi_state->owner->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700737
738 rt_mutex_proxy_unlock(&pi_state->pi_mutex, pi_state->owner);
739 }
740
741 if (current->pi_state_cache)
742 kfree(pi_state);
743 else {
744 /*
745 * pi_state->list is already empty.
746 * clear pi_state->owner.
747 * refcount is at 0 - put it back to 1.
748 */
749 pi_state->owner = NULL;
750 atomic_set(&pi_state->refcount, 1);
751 current->pi_state_cache = pi_state;
752 }
753}
754
755/*
756 * Look up the task based on what TID userspace gave us.
757 * We dont trust it.
758 */
759static struct task_struct * futex_find_get_task(pid_t pid)
760{
761 struct task_struct *p;
762
Oleg Nesterovd359b542006-09-29 02:00:55 -0700763 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700764 p = find_task_by_vpid(pid);
Michal Hocko7a0ea092010-06-30 09:51:19 +0200765 if (p)
766 get_task_struct(p);
Thomas Gleixnera06381f2007-06-23 11:48:40 +0200767
Oleg Nesterovd359b542006-09-29 02:00:55 -0700768 rcu_read_unlock();
Ingo Molnarc87e2832006-06-27 02:54:58 -0700769
770 return p;
771}
772
773/*
774 * This task is holding PI mutexes at exit time => bad.
775 * Kernel cleans up PI-state, but userspace is likely hosed.
776 * (Robust-futex cleanup is separate and might save the day for userspace.)
777 */
778void exit_pi_state_list(struct task_struct *curr)
779{
Ingo Molnarc87e2832006-06-27 02:54:58 -0700780 struct list_head *next, *head = &curr->pi_state_list;
781 struct futex_pi_state *pi_state;
Ingo Molnar627371d2006-07-29 05:16:20 +0200782 struct futex_hash_bucket *hb;
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200783 union futex_key key = FUTEX_KEY_INIT;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700784
Thomas Gleixnera0c1e902008-02-23 15:23:57 -0800785 if (!futex_cmpxchg_enabled)
786 return;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700787 /*
788 * We are a ZOMBIE and nobody can enqueue itself on
789 * pi_state_list anymore, but we have to be careful
Ingo Molnar627371d2006-07-29 05:16:20 +0200790 * versus waiters unqueueing themselves:
Ingo Molnarc87e2832006-06-27 02:54:58 -0700791 */
Thomas Gleixner1d615482009-11-17 14:54:03 +0100792 raw_spin_lock_irq(&curr->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700793 while (!list_empty(head)) {
794
795 next = head->next;
796 pi_state = list_entry(next, struct futex_pi_state, list);
797 key = pi_state->key;
Ingo Molnar627371d2006-07-29 05:16:20 +0200798 hb = hash_futex(&key);
Thomas Gleixner1d615482009-11-17 14:54:03 +0100799 raw_spin_unlock_irq(&curr->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700800
Ingo Molnarc87e2832006-06-27 02:54:58 -0700801 spin_lock(&hb->lock);
802
Thomas Gleixner1d615482009-11-17 14:54:03 +0100803 raw_spin_lock_irq(&curr->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +0200804 /*
805 * We dropped the pi-lock, so re-check whether this
806 * task still owns the PI-state:
807 */
Ingo Molnarc87e2832006-06-27 02:54:58 -0700808 if (head->next != next) {
809 spin_unlock(&hb->lock);
810 continue;
811 }
812
Ingo Molnarc87e2832006-06-27 02:54:58 -0700813 WARN_ON(pi_state->owner != curr);
Ingo Molnar627371d2006-07-29 05:16:20 +0200814 WARN_ON(list_empty(&pi_state->list));
815 list_del_init(&pi_state->list);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700816 pi_state->owner = NULL;
Thomas Gleixner1d615482009-11-17 14:54:03 +0100817 raw_spin_unlock_irq(&curr->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700818
819 rt_mutex_unlock(&pi_state->pi_mutex);
820
821 spin_unlock(&hb->lock);
822
Thomas Gleixner1d615482009-11-17 14:54:03 +0100823 raw_spin_lock_irq(&curr->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700824 }
Thomas Gleixner1d615482009-11-17 14:54:03 +0100825 raw_spin_unlock_irq(&curr->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700826}
827
Thomas Gleixner54a21782014-06-03 12:27:08 +0000828/*
829 * We need to check the following states:
830 *
831 * Waiter | pi_state | pi->owner | uTID | uODIED | ?
832 *
833 * [1] NULL | --- | --- | 0 | 0/1 | Valid
834 * [2] NULL | --- | --- | >0 | 0/1 | Valid
835 *
836 * [3] Found | NULL | -- | Any | 0/1 | Invalid
837 *
838 * [4] Found | Found | NULL | 0 | 1 | Valid
839 * [5] Found | Found | NULL | >0 | 1 | Invalid
840 *
841 * [6] Found | Found | task | 0 | 1 | Valid
842 *
843 * [7] Found | Found | NULL | Any | 0 | Invalid
844 *
845 * [8] Found | Found | task | ==taskTID | 0/1 | Valid
846 * [9] Found | Found | task | 0 | 0 | Invalid
847 * [10] Found | Found | task | !=taskTID | 0/1 | Invalid
848 *
849 * [1] Indicates that the kernel can acquire the futex atomically. We
850 * came came here due to a stale FUTEX_WAITERS/FUTEX_OWNER_DIED bit.
851 *
852 * [2] Valid, if TID does not belong to a kernel thread. If no matching
853 * thread is found then it indicates that the owner TID has died.
854 *
855 * [3] Invalid. The waiter is queued on a non PI futex
856 *
857 * [4] Valid state after exit_robust_list(), which sets the user space
858 * value to FUTEX_WAITERS | FUTEX_OWNER_DIED.
859 *
860 * [5] The user space value got manipulated between exit_robust_list()
861 * and exit_pi_state_list()
862 *
863 * [6] Valid state after exit_pi_state_list() which sets the new owner in
864 * the pi_state but cannot access the user space value.
865 *
866 * [7] pi_state->owner can only be NULL when the OWNER_DIED bit is set.
867 *
868 * [8] Owner and user space value match
869 *
870 * [9] There is no transient state which sets the user space TID to 0
871 * except exit_robust_list(), but this is indicated by the
872 * FUTEX_OWNER_DIED bit. See [4]
873 *
874 * [10] There is no transient state which leaves owner and user space
875 * TID out of sync.
876 */
Thomas Gleixnere60cbc52014-06-11 20:45:39 +0000877
878/*
879 * Validate that the existing waiter has a pi_state and sanity check
880 * the pi_state against the user space value. If correct, attach to
881 * it.
882 */
883static int attach_to_pi_state(u32 uval, struct futex_pi_state *pi_state,
884 struct futex_pi_state **ps)
885{
886 pid_t pid = uval & FUTEX_TID_MASK;
887
888 /*
889 * Userspace might have messed up non-PI and PI futexes [3]
890 */
891 if (unlikely(!pi_state))
892 return -EINVAL;
893
894 WARN_ON(!atomic_read(&pi_state->refcount));
895
896 /*
897 * Handle the owner died case:
898 */
899 if (uval & FUTEX_OWNER_DIED) {
900 /*
901 * exit_pi_state_list sets owner to NULL and wakes the
902 * topmost waiter. The task which acquires the
903 * pi_state->rt_mutex will fixup owner.
904 */
905 if (!pi_state->owner) {
906 /*
907 * No pi state owner, but the user space TID
908 * is not 0. Inconsistent state. [5]
909 */
910 if (pid)
911 return -EINVAL;
912 /*
913 * Take a ref on the state and return success. [4]
914 */
915 goto out_state;
916 }
917
918 /*
919 * If TID is 0, then either the dying owner has not
920 * yet executed exit_pi_state_list() or some waiter
921 * acquired the rtmutex in the pi state, but did not
922 * yet fixup the TID in user space.
923 *
924 * Take a ref on the state and return success. [6]
925 */
926 if (!pid)
927 goto out_state;
928 } else {
929 /*
930 * If the owner died bit is not set, then the pi_state
931 * must have an owner. [7]
932 */
933 if (!pi_state->owner)
934 return -EINVAL;
935 }
936
937 /*
938 * Bail out if user space manipulated the futex value. If pi
939 * state exists then the owner TID must be the same as the
940 * user space TID. [9/10]
941 */
942 if (pid != task_pid_vnr(pi_state->owner))
943 return -EINVAL;
944out_state:
945 atomic_inc(&pi_state->refcount);
946 *ps = pi_state;
947 return 0;
948}
949
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +0000950/*
951 * Lookup the task for the TID provided from user space and attach to
952 * it after doing proper sanity checks.
953 */
954static int attach_to_pi_owner(u32 uval, union futex_key *key,
955 struct futex_pi_state **ps)
Ingo Molnarc87e2832006-06-27 02:54:58 -0700956{
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700957 pid_t pid = uval & FUTEX_TID_MASK;
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +0000958 struct futex_pi_state *pi_state;
959 struct task_struct *p;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700960
961 /*
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200962 * We are the first waiter - try to look up the real owner and attach
Thomas Gleixner54a21782014-06-03 12:27:08 +0000963 * the new pi_state to it, but bail out when TID = 0 [1]
Ingo Molnarc87e2832006-06-27 02:54:58 -0700964 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700965 if (!pid)
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200966 return -ESRCH;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700967 p = futex_find_get_task(pid);
Michal Hocko7a0ea092010-06-30 09:51:19 +0200968 if (!p)
969 return -ESRCH;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700970
Oleg Nesterova2129462015-02-02 15:05:36 +0100971 if (unlikely(p->flags & PF_KTHREAD)) {
Thomas Gleixnerf0d71b32014-05-12 20:45:35 +0000972 put_task_struct(p);
973 return -EPERM;
974 }
975
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700976 /*
977 * We need to look at the task state flags to figure out,
978 * whether the task is exiting. To protect against the do_exit
979 * change of the task flags, we do this protected by
980 * p->pi_lock:
981 */
Thomas Gleixner1d615482009-11-17 14:54:03 +0100982 raw_spin_lock_irq(&p->pi_lock);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700983 if (unlikely(p->flags & PF_EXITING)) {
984 /*
985 * The task is on the way out. When PF_EXITPIDONE is
986 * set, we know that the task has finished the
987 * cleanup:
988 */
989 int ret = (p->flags & PF_EXITPIDONE) ? -ESRCH : -EAGAIN;
990
Thomas Gleixner1d615482009-11-17 14:54:03 +0100991 raw_spin_unlock_irq(&p->pi_lock);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700992 put_task_struct(p);
993 return ret;
994 }
Ingo Molnarc87e2832006-06-27 02:54:58 -0700995
Thomas Gleixner54a21782014-06-03 12:27:08 +0000996 /*
997 * No existing pi state. First waiter. [2]
998 */
Ingo Molnarc87e2832006-06-27 02:54:58 -0700999 pi_state = alloc_pi_state();
1000
1001 /*
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001002 * Initialize the pi_mutex in locked state and make @p
Ingo Molnarc87e2832006-06-27 02:54:58 -07001003 * the owner of it:
1004 */
1005 rt_mutex_init_proxy_locked(&pi_state->pi_mutex, p);
1006
1007 /* Store the key for possible exit cleanups: */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001008 pi_state->key = *key;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001009
Ingo Molnar627371d2006-07-29 05:16:20 +02001010 WARN_ON(!list_empty(&pi_state->list));
Ingo Molnarc87e2832006-06-27 02:54:58 -07001011 list_add(&pi_state->list, &p->pi_state_list);
1012 pi_state->owner = p;
Thomas Gleixner1d615482009-11-17 14:54:03 +01001013 raw_spin_unlock_irq(&p->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001014
1015 put_task_struct(p);
1016
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001017 *ps = pi_state;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001018
1019 return 0;
1020}
1021
Thomas Gleixner04e1b2e2014-06-11 20:45:40 +00001022static int lookup_pi_state(u32 uval, struct futex_hash_bucket *hb,
1023 union futex_key *key, struct futex_pi_state **ps)
1024{
1025 struct futex_q *match = futex_top_waiter(hb, key);
1026
1027 /*
1028 * If there is a waiter on that futex, validate it and
1029 * attach to the pi_state when the validation succeeds.
1030 */
1031 if (match)
1032 return attach_to_pi_state(uval, match->pi_state, ps);
1033
1034 /*
1035 * We are the first waiter - try to look up the owner based on
1036 * @uval and attach to it.
1037 */
1038 return attach_to_pi_owner(uval, key, ps);
1039}
1040
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001041static int lock_pi_update_atomic(u32 __user *uaddr, u32 uval, u32 newval)
1042{
1043 u32 uninitialized_var(curval);
1044
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001045 if (unlikely(should_fail_futex(true)))
1046 return -EFAULT;
1047
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001048 if (unlikely(cmpxchg_futex_value_locked(&curval, uaddr, uval, newval)))
1049 return -EFAULT;
1050
1051 /*If user space value changed, let the caller retry */
1052 return curval != uval ? -EAGAIN : 0;
1053}
1054
Darren Hart1a520842009-04-03 13:39:52 -07001055/**
Darren Hartd96ee562009-09-21 22:30:22 -07001056 * futex_lock_pi_atomic() - Atomic work required to acquire a pi aware futex
Darren Hartbab5bc92009-04-07 23:23:50 -07001057 * @uaddr: the pi futex user address
1058 * @hb: the pi futex hash bucket
1059 * @key: the futex key associated with uaddr and hb
1060 * @ps: the pi_state pointer where we store the result of the
1061 * lookup
1062 * @task: the task to perform the atomic lock work for. This will
1063 * be "current" except in the case of requeue pi.
1064 * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0)
Darren Hart1a520842009-04-03 13:39:52 -07001065 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08001066 * Return:
1067 * 0 - ready to wait;
1068 * 1 - acquired the lock;
Darren Hart1a520842009-04-03 13:39:52 -07001069 * <0 - error
1070 *
1071 * The hb->lock and futex_key refs shall be held by the caller.
1072 */
1073static int futex_lock_pi_atomic(u32 __user *uaddr, struct futex_hash_bucket *hb,
1074 union futex_key *key,
1075 struct futex_pi_state **ps,
Darren Hartbab5bc92009-04-07 23:23:50 -07001076 struct task_struct *task, int set_waiters)
Darren Hart1a520842009-04-03 13:39:52 -07001077{
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001078 u32 uval, newval, vpid = task_pid_vnr(task);
1079 struct futex_q *match;
1080 int ret;
Darren Hart1a520842009-04-03 13:39:52 -07001081
1082 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001083 * Read the user space value first so we can validate a few
1084 * things before proceeding further.
Darren Hart1a520842009-04-03 13:39:52 -07001085 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001086 if (get_futex_value_locked(&uval, uaddr))
Darren Hart1a520842009-04-03 13:39:52 -07001087 return -EFAULT;
1088
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001089 if (unlikely(should_fail_futex(true)))
1090 return -EFAULT;
1091
Darren Hart1a520842009-04-03 13:39:52 -07001092 /*
1093 * Detect deadlocks.
1094 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001095 if ((unlikely((uval & FUTEX_TID_MASK) == vpid)))
Darren Hart1a520842009-04-03 13:39:52 -07001096 return -EDEADLK;
1097
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001098 if ((unlikely(should_fail_futex(true))))
1099 return -EDEADLK;
1100
Darren Hart1a520842009-04-03 13:39:52 -07001101 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001102 * Lookup existing state first. If it exists, try to attach to
1103 * its pi_state.
Darren Hart1a520842009-04-03 13:39:52 -07001104 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001105 match = futex_top_waiter(hb, key);
1106 if (match)
1107 return attach_to_pi_state(uval, match->pi_state, ps);
1108
1109 /*
1110 * No waiter and user TID is 0. We are here because the
1111 * waiters or the owner died bit is set or called from
1112 * requeue_cmp_pi or for whatever reason something took the
1113 * syscall.
1114 */
1115 if (!(uval & FUTEX_TID_MASK)) {
Thomas Gleixnerb3eaa9f2014-06-03 12:27:06 +00001116 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001117 * We take over the futex. No other waiters and the user space
1118 * TID is 0. We preserve the owner died bit.
Thomas Gleixnerb3eaa9f2014-06-03 12:27:06 +00001119 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001120 newval = uval & FUTEX_OWNER_DIED;
1121 newval |= vpid;
1122
1123 /* The futex requeue_pi code can enforce the waiters bit */
1124 if (set_waiters)
1125 newval |= FUTEX_WAITERS;
1126
1127 ret = lock_pi_update_atomic(uaddr, uval, newval);
1128 /* If the take over worked, return 1 */
1129 return ret < 0 ? ret : 1;
Thomas Gleixnerb3eaa9f2014-06-03 12:27:06 +00001130 }
Darren Hart1a520842009-04-03 13:39:52 -07001131
Darren Hart1a520842009-04-03 13:39:52 -07001132 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001133 * First waiter. Set the waiters bit before attaching ourself to
1134 * the owner. If owner tries to unlock, it will be forced into
1135 * the kernel and blocked on hb->lock.
Darren Hart1a520842009-04-03 13:39:52 -07001136 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001137 newval = uval | FUTEX_WAITERS;
1138 ret = lock_pi_update_atomic(uaddr, uval, newval);
1139 if (ret)
1140 return ret;
Darren Hart1a520842009-04-03 13:39:52 -07001141 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001142 * If the update of the user space value succeeded, we try to
1143 * attach to the owner. If that fails, no harm done, we only
1144 * set the FUTEX_WAITERS bit in the user space variable.
Darren Hart1a520842009-04-03 13:39:52 -07001145 */
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001146 return attach_to_pi_owner(uval, key, ps);
Darren Hart1a520842009-04-03 13:39:52 -07001147}
1148
Lai Jiangshan2e129782010-12-22 14:18:50 +08001149/**
1150 * __unqueue_futex() - Remove the futex_q from its futex_hash_bucket
1151 * @q: The futex_q to unqueue
1152 *
1153 * The q->lock_ptr must not be NULL and must be held by the caller.
1154 */
1155static void __unqueue_futex(struct futex_q *q)
1156{
1157 struct futex_hash_bucket *hb;
1158
Steven Rostedt29096202011-03-17 15:21:07 -04001159 if (WARN_ON_SMP(!q->lock_ptr || !spin_is_locked(q->lock_ptr))
1160 || WARN_ON(plist_node_empty(&q->list)))
Lai Jiangshan2e129782010-12-22 14:18:50 +08001161 return;
1162
1163 hb = container_of(q->lock_ptr, struct futex_hash_bucket, lock);
1164 plist_del(&q->list, &hb->chain);
Linus Torvalds11d46162014-03-20 22:11:17 -07001165 hb_waiters_dec(hb);
Lai Jiangshan2e129782010-12-22 14:18:50 +08001166}
1167
Ingo Molnarc87e2832006-06-27 02:54:58 -07001168/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 * The hash bucket lock must be held when this is called.
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001170 * Afterwards, the futex_q must not be accessed. Callers
1171 * must ensure to later call wake_up_q() for the actual
1172 * wakeups to occur.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 */
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001174static void mark_wake_futex(struct wake_q_head *wake_q, struct futex_q *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001176 struct task_struct *p = q->task;
1177
Darren Hartaa109902012-11-26 16:29:56 -08001178 if (WARN(q->pi_state || q->rt_waiter, "refusing to wake PI futex\n"))
1179 return;
1180
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001181 /*
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001182 * Queue the task for later wakeup for after we've released
1183 * the hb->lock. wake_q_add() grabs reference to p.
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001184 */
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001185 wake_q_add(wake_q, p);
Lai Jiangshan2e129782010-12-22 14:18:50 +08001186 __unqueue_futex(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 /*
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001188 * The waiting task can free the futex_q as soon as
1189 * q->lock_ptr = NULL is written, without taking any locks. A
1190 * memory barrier is required here to prevent the following
1191 * store to lock_ptr from getting ahead of the plist_del.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 */
Ralf Baechleccdea2f2006-12-06 20:40:26 -08001193 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 q->lock_ptr = NULL;
1195}
1196
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001197static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this,
1198 struct futex_hash_bucket *hb)
Ingo Molnarc87e2832006-06-27 02:54:58 -07001199{
1200 struct task_struct *new_owner;
1201 struct futex_pi_state *pi_state = this->pi_state;
Vitaliy Ivanov7cfdaf32011-07-07 15:10:31 +03001202 u32 uninitialized_var(curval), newval;
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001203 WAKE_Q(wake_q);
1204 bool deboost;
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001205 int ret = 0;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001206
1207 if (!pi_state)
1208 return -EINVAL;
1209
Thomas Gleixner51246bf2010-02-02 11:40:27 +01001210 /*
1211 * If current does not own the pi_state then the futex is
1212 * inconsistent and user space fiddled with the futex value.
1213 */
1214 if (pi_state->owner != current)
1215 return -EINVAL;
1216
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001217 raw_spin_lock(&pi_state->pi_mutex.wait_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001218 new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);
1219
1220 /*
Steven Rostedtf123c982011-01-06 15:08:29 -05001221 * It is possible that the next waiter (the one that brought
1222 * this owner to the kernel) timed out and is no longer
1223 * waiting on the lock.
Ingo Molnarc87e2832006-06-27 02:54:58 -07001224 */
1225 if (!new_owner)
1226 new_owner = this->task;
1227
1228 /*
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001229 * We pass it to the next owner. The WAITERS bit is always
1230 * kept enabled while there is PI state around. We cleanup the
1231 * owner died bit, because we are the owner.
Ingo Molnarc87e2832006-06-27 02:54:58 -07001232 */
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001233 newval = FUTEX_WAITERS | task_pid_vnr(new_owner);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001234
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001235 if (unlikely(should_fail_futex(true)))
1236 ret = -EFAULT;
1237
Thomas Gleixner13fbca42014-06-03 12:27:07 +00001238 if (cmpxchg_futex_value_locked(&curval, uaddr, uval, newval))
1239 ret = -EFAULT;
1240 else if (curval != uval)
1241 ret = -EINVAL;
1242 if (ret) {
1243 raw_spin_unlock(&pi_state->pi_mutex.wait_lock);
1244 return ret;
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001245 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001246
Thomas Gleixner1d615482009-11-17 14:54:03 +01001247 raw_spin_lock_irq(&pi_state->owner->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +02001248 WARN_ON(list_empty(&pi_state->list));
1249 list_del_init(&pi_state->list);
Thomas Gleixner1d615482009-11-17 14:54:03 +01001250 raw_spin_unlock_irq(&pi_state->owner->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +02001251
Thomas Gleixner1d615482009-11-17 14:54:03 +01001252 raw_spin_lock_irq(&new_owner->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +02001253 WARN_ON(!list_empty(&pi_state->list));
Ingo Molnarc87e2832006-06-27 02:54:58 -07001254 list_add(&pi_state->list, &new_owner->pi_state_list);
1255 pi_state->owner = new_owner;
Thomas Gleixner1d615482009-11-17 14:54:03 +01001256 raw_spin_unlock_irq(&new_owner->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +02001257
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001258 raw_spin_unlock(&pi_state->pi_mutex.wait_lock);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02001259
1260 deboost = rt_mutex_futex_unlock(&pi_state->pi_mutex, &wake_q);
1261
1262 /*
1263 * First unlock HB so the waiter does not spin on it once he got woken
1264 * up. Second wake up the waiter before the priority is adjusted. If we
1265 * deboost first (and lose our higher priority), then the task might get
1266 * scheduled away before the wake up can take place.
1267 */
1268 spin_unlock(&hb->lock);
1269 wake_up_q(&wake_q);
1270 if (deboost)
1271 rt_mutex_adjust_prio(current);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001272
1273 return 0;
1274}
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276/*
Ingo Molnar8b8f3192006-07-03 00:25:05 -07001277 * Express the locking dependencies for lockdep:
1278 */
1279static inline void
1280double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
1281{
1282 if (hb1 <= hb2) {
1283 spin_lock(&hb1->lock);
1284 if (hb1 < hb2)
1285 spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING);
1286 } else { /* hb1 > hb2 */
1287 spin_lock(&hb2->lock);
1288 spin_lock_nested(&hb1->lock, SINGLE_DEPTH_NESTING);
1289 }
1290}
1291
Darren Hart5eb3dc62009-03-12 00:55:52 -07001292static inline void
1293double_unlock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
1294{
Darren Hartf061d352009-03-12 15:11:18 -07001295 spin_unlock(&hb1->lock);
Ingo Molnar88f502f2009-03-13 10:32:07 +01001296 if (hb1 != hb2)
1297 spin_unlock(&hb2->lock);
Darren Hart5eb3dc62009-03-12 00:55:52 -07001298}
1299
Ingo Molnar8b8f3192006-07-03 00:25:05 -07001300/*
Darren Hartb2d09942009-03-12 00:55:37 -07001301 * Wake up waiters matching bitset queued on this futex (uaddr).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 */
Darren Hartb41277d2010-11-08 13:10:09 -08001303static int
1304futex_wake(u32 __user *uaddr, unsigned int flags, int nr_wake, u32 bitset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
Ingo Molnare2970f22006-06-27 02:54:47 -07001306 struct futex_hash_bucket *hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 struct futex_q *this, *next;
Peter Zijlstra38d47c12008-09-26 19:32:20 +02001308 union futex_key key = FUTEX_KEY_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 int ret;
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001310 WAKE_Q(wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Thomas Gleixnercd689982008-02-01 17:45:14 +01001312 if (!bitset)
1313 return -EINVAL;
1314
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001315 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 if (unlikely(ret != 0))
1317 goto out;
1318
Ingo Molnare2970f22006-06-27 02:54:47 -07001319 hb = hash_futex(&key);
Davidlohr Buesob0c29f72014-01-12 15:31:25 -08001320
1321 /* Make sure we really have tasks to wakeup */
1322 if (!hb_waiters_pending(hb))
1323 goto out_put_key;
1324
Ingo Molnare2970f22006-06-27 02:54:47 -07001325 spin_lock(&hb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Jason Low0d00c7b2014-01-12 15:31:22 -08001327 plist_for_each_entry_safe(this, next, &hb->chain, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 if (match_futex (&this->key, &key)) {
Darren Hart52400ba2009-04-03 13:40:49 -07001329 if (this->pi_state || this->rt_waiter) {
Ingo Molnared6f7b12006-07-01 04:35:46 -07001330 ret = -EINVAL;
1331 break;
1332 }
Thomas Gleixnercd689982008-02-01 17:45:14 +01001333
1334 /* Check if one of the bits is set in both bitsets */
1335 if (!(this->bitset & bitset))
1336 continue;
1337
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001338 mark_wake_futex(&wake_q, this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 if (++ret >= nr_wake)
1340 break;
1341 }
1342 }
1343
Ingo Molnare2970f22006-06-27 02:54:47 -07001344 spin_unlock(&hb->lock);
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001345 wake_up_q(&wake_q);
Davidlohr Buesob0c29f72014-01-12 15:31:25 -08001346out_put_key:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001347 put_futex_key(&key);
Darren Hart42d35d42008-12-29 15:49:53 -08001348out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return ret;
1350}
1351
1352/*
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001353 * Wake up all waiters hashed on the physical page that is mapped
1354 * to this virtual address:
1355 */
Ingo Molnare2970f22006-06-27 02:54:47 -07001356static int
Darren Hartb41277d2010-11-08 13:10:09 -08001357futex_wake_op(u32 __user *uaddr1, unsigned int flags, u32 __user *uaddr2,
Ingo Molnare2970f22006-06-27 02:54:47 -07001358 int nr_wake, int nr_wake2, int op)
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001359{
Peter Zijlstra38d47c12008-09-26 19:32:20 +02001360 union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
Ingo Molnare2970f22006-06-27 02:54:47 -07001361 struct futex_hash_bucket *hb1, *hb2;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001362 struct futex_q *this, *next;
Darren Harte4dc5b72009-03-12 00:56:13 -07001363 int ret, op_ret;
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001364 WAKE_Q(wake_q);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001365
Darren Harte4dc5b72009-03-12 00:56:13 -07001366retry:
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001367 ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, VERIFY_READ);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001368 if (unlikely(ret != 0))
1369 goto out;
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001370 ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001371 if (unlikely(ret != 0))
Darren Hart42d35d42008-12-29 15:49:53 -08001372 goto out_put_key1;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001373
Ingo Molnare2970f22006-06-27 02:54:47 -07001374 hb1 = hash_futex(&key1);
1375 hb2 = hash_futex(&key2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001376
Darren Harte4dc5b72009-03-12 00:56:13 -07001377retry_private:
Thomas Gleixnereaaea802009-10-04 09:34:17 +02001378 double_lock_hb(hb1, hb2);
Ingo Molnare2970f22006-06-27 02:54:47 -07001379 op_ret = futex_atomic_op_inuser(op, uaddr2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001380 if (unlikely(op_ret < 0)) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001381
Darren Hart5eb3dc62009-03-12 00:55:52 -07001382 double_unlock_hb(hb1, hb2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001383
David Howells7ee1dd32006-01-06 00:11:44 -08001384#ifndef CONFIG_MMU
Ingo Molnare2970f22006-06-27 02:54:47 -07001385 /*
1386 * we don't get EFAULT from MMU faults if we don't have an MMU,
1387 * but we might get them from range checking
1388 */
David Howells7ee1dd32006-01-06 00:11:44 -08001389 ret = op_ret;
Darren Hart42d35d42008-12-29 15:49:53 -08001390 goto out_put_keys;
David Howells7ee1dd32006-01-06 00:11:44 -08001391#endif
1392
David Gibson796f8d92005-11-07 00:59:33 -08001393 if (unlikely(op_ret != -EFAULT)) {
1394 ret = op_ret;
Darren Hart42d35d42008-12-29 15:49:53 -08001395 goto out_put_keys;
David Gibson796f8d92005-11-07 00:59:33 -08001396 }
1397
Thomas Gleixnerd0725992009-06-11 23:15:43 +02001398 ret = fault_in_user_writeable(uaddr2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001399 if (ret)
Darren Hartde87fcc2009-03-12 00:55:46 -07001400 goto out_put_keys;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001401
Darren Hartb41277d2010-11-08 13:10:09 -08001402 if (!(flags & FLAGS_SHARED))
Darren Harte4dc5b72009-03-12 00:56:13 -07001403 goto retry_private;
1404
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001405 put_futex_key(&key2);
1406 put_futex_key(&key1);
Darren Harte4dc5b72009-03-12 00:56:13 -07001407 goto retry;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001408 }
1409
Jason Low0d00c7b2014-01-12 15:31:22 -08001410 plist_for_each_entry_safe(this, next, &hb1->chain, list) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001411 if (match_futex (&this->key, &key1)) {
Darren Hartaa109902012-11-26 16:29:56 -08001412 if (this->pi_state || this->rt_waiter) {
1413 ret = -EINVAL;
1414 goto out_unlock;
1415 }
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001416 mark_wake_futex(&wake_q, this);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001417 if (++ret >= nr_wake)
1418 break;
1419 }
1420 }
1421
1422 if (op_ret > 0) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001423 op_ret = 0;
Jason Low0d00c7b2014-01-12 15:31:22 -08001424 plist_for_each_entry_safe(this, next, &hb2->chain, list) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001425 if (match_futex (&this->key, &key2)) {
Darren Hartaa109902012-11-26 16:29:56 -08001426 if (this->pi_state || this->rt_waiter) {
1427 ret = -EINVAL;
1428 goto out_unlock;
1429 }
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001430 mark_wake_futex(&wake_q, this);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001431 if (++op_ret >= nr_wake2)
1432 break;
1433 }
1434 }
1435 ret += op_ret;
1436 }
1437
Darren Hartaa109902012-11-26 16:29:56 -08001438out_unlock:
Darren Hart5eb3dc62009-03-12 00:55:52 -07001439 double_unlock_hb(hb1, hb2);
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001440 wake_up_q(&wake_q);
Darren Hart42d35d42008-12-29 15:49:53 -08001441out_put_keys:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001442 put_futex_key(&key2);
Darren Hart42d35d42008-12-29 15:49:53 -08001443out_put_key1:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001444 put_futex_key(&key1);
Darren Hart42d35d42008-12-29 15:49:53 -08001445out:
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001446 return ret;
1447}
1448
Darren Hart9121e472009-04-03 13:40:31 -07001449/**
1450 * requeue_futex() - Requeue a futex_q from one hb to another
1451 * @q: the futex_q to requeue
1452 * @hb1: the source hash_bucket
1453 * @hb2: the target hash_bucket
1454 * @key2: the new key for the requeued futex_q
1455 */
1456static inline
1457void requeue_futex(struct futex_q *q, struct futex_hash_bucket *hb1,
1458 struct futex_hash_bucket *hb2, union futex_key *key2)
1459{
1460
1461 /*
1462 * If key1 and key2 hash to the same bucket, no need to
1463 * requeue.
1464 */
1465 if (likely(&hb1->chain != &hb2->chain)) {
1466 plist_del(&q->list, &hb1->chain);
Linus Torvalds11d46162014-03-20 22:11:17 -07001467 hb_waiters_dec(hb1);
Darren Hart9121e472009-04-03 13:40:31 -07001468 plist_add(&q->list, &hb2->chain);
Linus Torvalds11d46162014-03-20 22:11:17 -07001469 hb_waiters_inc(hb2);
Darren Hart9121e472009-04-03 13:40:31 -07001470 q->lock_ptr = &hb2->lock;
Darren Hart9121e472009-04-03 13:40:31 -07001471 }
1472 get_futex_key_refs(key2);
1473 q->key = *key2;
1474}
1475
Darren Hart52400ba2009-04-03 13:40:49 -07001476/**
1477 * requeue_pi_wake_futex() - Wake a task that acquired the lock during requeue
Darren Hartd96ee562009-09-21 22:30:22 -07001478 * @q: the futex_q
1479 * @key: the key of the requeue target futex
1480 * @hb: the hash_bucket of the requeue target futex
Darren Hart52400ba2009-04-03 13:40:49 -07001481 *
1482 * During futex_requeue, with requeue_pi=1, it is possible to acquire the
1483 * target futex if it is uncontended or via a lock steal. Set the futex_q key
1484 * to the requeue target futex so the waiter can detect the wakeup on the right
1485 * futex, but remove it from the hb and NULL the rt_waiter so it can detect
Darren Hartbeda2c72009-08-09 15:34:39 -07001486 * atomic lock acquisition. Set the q->lock_ptr to the requeue target hb->lock
1487 * to protect access to the pi_state to fixup the owner later. Must be called
1488 * with both q->lock_ptr and hb->lock held.
Darren Hart52400ba2009-04-03 13:40:49 -07001489 */
1490static inline
Darren Hartbeda2c72009-08-09 15:34:39 -07001491void requeue_pi_wake_futex(struct futex_q *q, union futex_key *key,
1492 struct futex_hash_bucket *hb)
Darren Hart52400ba2009-04-03 13:40:49 -07001493{
Darren Hart52400ba2009-04-03 13:40:49 -07001494 get_futex_key_refs(key);
1495 q->key = *key;
1496
Lai Jiangshan2e129782010-12-22 14:18:50 +08001497 __unqueue_futex(q);
Darren Hart52400ba2009-04-03 13:40:49 -07001498
1499 WARN_ON(!q->rt_waiter);
1500 q->rt_waiter = NULL;
1501
Darren Hartbeda2c72009-08-09 15:34:39 -07001502 q->lock_ptr = &hb->lock;
Darren Hartbeda2c72009-08-09 15:34:39 -07001503
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001504 wake_up_state(q->task, TASK_NORMAL);
Darren Hart52400ba2009-04-03 13:40:49 -07001505}
1506
1507/**
1508 * futex_proxy_trylock_atomic() - Attempt an atomic lock for the top waiter
Darren Hartbab5bc92009-04-07 23:23:50 -07001509 * @pifutex: the user address of the to futex
1510 * @hb1: the from futex hash bucket, must be locked by the caller
1511 * @hb2: the to futex hash bucket, must be locked by the caller
1512 * @key1: the from futex key
1513 * @key2: the to futex key
1514 * @ps: address to store the pi_state pointer
1515 * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0)
Darren Hart52400ba2009-04-03 13:40:49 -07001516 *
1517 * Try and get the lock on behalf of the top waiter if we can do it atomically.
Darren Hartbab5bc92009-04-07 23:23:50 -07001518 * Wake the top waiter if we succeed. If the caller specified set_waiters,
1519 * then direct futex_lock_pi_atomic() to force setting the FUTEX_WAITERS bit.
1520 * hb1 and hb2 must be held by the caller.
Darren Hart52400ba2009-04-03 13:40:49 -07001521 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08001522 * Return:
1523 * 0 - failed to acquire the lock atomically;
Thomas Gleixner866293e2014-05-12 20:45:34 +00001524 * >0 - acquired the lock, return value is vpid of the top_waiter
Darren Hart52400ba2009-04-03 13:40:49 -07001525 * <0 - error
1526 */
1527static int futex_proxy_trylock_atomic(u32 __user *pifutex,
1528 struct futex_hash_bucket *hb1,
1529 struct futex_hash_bucket *hb2,
1530 union futex_key *key1, union futex_key *key2,
Darren Hartbab5bc92009-04-07 23:23:50 -07001531 struct futex_pi_state **ps, int set_waiters)
Darren Hart52400ba2009-04-03 13:40:49 -07001532{
Darren Hartbab5bc92009-04-07 23:23:50 -07001533 struct futex_q *top_waiter = NULL;
Darren Hart52400ba2009-04-03 13:40:49 -07001534 u32 curval;
Thomas Gleixner866293e2014-05-12 20:45:34 +00001535 int ret, vpid;
Darren Hart52400ba2009-04-03 13:40:49 -07001536
1537 if (get_futex_value_locked(&curval, pifutex))
1538 return -EFAULT;
1539
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07001540 if (unlikely(should_fail_futex(true)))
1541 return -EFAULT;
1542
Darren Hartbab5bc92009-04-07 23:23:50 -07001543 /*
1544 * Find the top_waiter and determine if there are additional waiters.
1545 * If the caller intends to requeue more than 1 waiter to pifutex,
1546 * force futex_lock_pi_atomic() to set the FUTEX_WAITERS bit now,
1547 * as we have means to handle the possible fault. If not, don't set
1548 * the bit unecessarily as it will force the subsequent unlock to enter
1549 * the kernel.
1550 */
Darren Hart52400ba2009-04-03 13:40:49 -07001551 top_waiter = futex_top_waiter(hb1, key1);
1552
1553 /* There are no waiters, nothing for us to do. */
1554 if (!top_waiter)
1555 return 0;
1556
Darren Hart84bc4af2009-08-13 17:36:53 -07001557 /* Ensure we requeue to the expected futex. */
1558 if (!match_futex(top_waiter->requeue_pi_key, key2))
1559 return -EINVAL;
1560
Darren Hart52400ba2009-04-03 13:40:49 -07001561 /*
Darren Hartbab5bc92009-04-07 23:23:50 -07001562 * Try to take the lock for top_waiter. Set the FUTEX_WAITERS bit in
1563 * the contended case or if set_waiters is 1. The pi_state is returned
1564 * in ps in contended cases.
Darren Hart52400ba2009-04-03 13:40:49 -07001565 */
Thomas Gleixner866293e2014-05-12 20:45:34 +00001566 vpid = task_pid_vnr(top_waiter->task);
Darren Hartbab5bc92009-04-07 23:23:50 -07001567 ret = futex_lock_pi_atomic(pifutex, hb2, key2, ps, top_waiter->task,
1568 set_waiters);
Thomas Gleixner866293e2014-05-12 20:45:34 +00001569 if (ret == 1) {
Darren Hartbeda2c72009-08-09 15:34:39 -07001570 requeue_pi_wake_futex(top_waiter, key2, hb2);
Thomas Gleixner866293e2014-05-12 20:45:34 +00001571 return vpid;
1572 }
Darren Hart52400ba2009-04-03 13:40:49 -07001573 return ret;
1574}
1575
1576/**
1577 * futex_requeue() - Requeue waiters from uaddr1 to uaddr2
Randy Dunlapfb62db22010-10-13 11:02:34 -07001578 * @uaddr1: source futex user address
Darren Hartb41277d2010-11-08 13:10:09 -08001579 * @flags: futex flags (FLAGS_SHARED, etc.)
Randy Dunlapfb62db22010-10-13 11:02:34 -07001580 * @uaddr2: target futex user address
1581 * @nr_wake: number of waiters to wake (must be 1 for requeue_pi)
1582 * @nr_requeue: number of waiters to requeue (0-INT_MAX)
1583 * @cmpval: @uaddr1 expected value (or %NULL)
1584 * @requeue_pi: if we are attempting to requeue from a non-pi futex to a
Darren Hartb41277d2010-11-08 13:10:09 -08001585 * pi futex (pi to pi requeue is not supported)
Darren Hart52400ba2009-04-03 13:40:49 -07001586 *
1587 * Requeue waiters on uaddr1 to uaddr2. In the requeue_pi case, try to acquire
1588 * uaddr2 atomically on behalf of the top waiter.
1589 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08001590 * Return:
1591 * >=0 - on success, the number of tasks requeued or woken;
Darren Hart52400ba2009-04-03 13:40:49 -07001592 * <0 - on error
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 */
Darren Hartb41277d2010-11-08 13:10:09 -08001594static int futex_requeue(u32 __user *uaddr1, unsigned int flags,
1595 u32 __user *uaddr2, int nr_wake, int nr_requeue,
1596 u32 *cmpval, int requeue_pi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
Peter Zijlstra38d47c12008-09-26 19:32:20 +02001598 union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
Darren Hart52400ba2009-04-03 13:40:49 -07001599 int drop_count = 0, task_count = 0, ret;
1600 struct futex_pi_state *pi_state = NULL;
Ingo Molnare2970f22006-06-27 02:54:47 -07001601 struct futex_hash_bucket *hb1, *hb2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 struct futex_q *this, *next;
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001603 WAKE_Q(wake_q);
Darren Hart52400ba2009-04-03 13:40:49 -07001604
1605 if (requeue_pi) {
1606 /*
Thomas Gleixnere9c243a2014-06-03 12:27:06 +00001607 * Requeue PI only works on two distinct uaddrs. This
1608 * check is only valid for private futexes. See below.
1609 */
1610 if (uaddr1 == uaddr2)
1611 return -EINVAL;
1612
1613 /*
Darren Hart52400ba2009-04-03 13:40:49 -07001614 * requeue_pi requires a pi_state, try to allocate it now
1615 * without any locks in case it fails.
1616 */
1617 if (refill_pi_state_cache())
1618 return -ENOMEM;
1619 /*
1620 * requeue_pi must wake as many tasks as it can, up to nr_wake
1621 * + nr_requeue, since it acquires the rt_mutex prior to
1622 * returning to userspace, so as to not leave the rt_mutex with
1623 * waiters and no owner. However, second and third wake-ups
1624 * cannot be predicted as they involve race conditions with the
1625 * first wake and a fault while looking up the pi_state. Both
1626 * pthread_cond_signal() and pthread_cond_broadcast() should
1627 * use nr_wake=1.
1628 */
1629 if (nr_wake != 1)
1630 return -EINVAL;
1631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Darren Hart42d35d42008-12-29 15:49:53 -08001633retry:
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001634 ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 if (unlikely(ret != 0))
1636 goto out;
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001637 ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2,
1638 requeue_pi ? VERIFY_WRITE : VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 if (unlikely(ret != 0))
Darren Hart42d35d42008-12-29 15:49:53 -08001640 goto out_put_key1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Thomas Gleixnere9c243a2014-06-03 12:27:06 +00001642 /*
1643 * The check above which compares uaddrs is not sufficient for
1644 * shared futexes. We need to compare the keys:
1645 */
1646 if (requeue_pi && match_futex(&key1, &key2)) {
1647 ret = -EINVAL;
1648 goto out_put_keys;
1649 }
1650
Ingo Molnare2970f22006-06-27 02:54:47 -07001651 hb1 = hash_futex(&key1);
1652 hb2 = hash_futex(&key2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Darren Harte4dc5b72009-03-12 00:56:13 -07001654retry_private:
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07001655 hb_waiters_inc(hb2);
Ingo Molnar8b8f3192006-07-03 00:25:05 -07001656 double_lock_hb(hb1, hb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Ingo Molnare2970f22006-06-27 02:54:47 -07001658 if (likely(cmpval != NULL)) {
1659 u32 curval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Ingo Molnare2970f22006-06-27 02:54:47 -07001661 ret = get_futex_value_locked(&curval, uaddr1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
1663 if (unlikely(ret)) {
Darren Hart5eb3dc62009-03-12 00:55:52 -07001664 double_unlock_hb(hb1, hb2);
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07001665 hb_waiters_dec(hb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Darren Harte4dc5b72009-03-12 00:56:13 -07001667 ret = get_user(curval, uaddr1);
1668 if (ret)
1669 goto out_put_keys;
1670
Darren Hartb41277d2010-11-08 13:10:09 -08001671 if (!(flags & FLAGS_SHARED))
Darren Harte4dc5b72009-03-12 00:56:13 -07001672 goto retry_private;
1673
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001674 put_futex_key(&key2);
1675 put_futex_key(&key1);
Darren Harte4dc5b72009-03-12 00:56:13 -07001676 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 }
Ingo Molnare2970f22006-06-27 02:54:47 -07001678 if (curval != *cmpval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 ret = -EAGAIN;
1680 goto out_unlock;
1681 }
1682 }
1683
Darren Hart52400ba2009-04-03 13:40:49 -07001684 if (requeue_pi && (task_count - nr_wake < nr_requeue)) {
Darren Hartbab5bc92009-04-07 23:23:50 -07001685 /*
1686 * Attempt to acquire uaddr2 and wake the top waiter. If we
1687 * intend to requeue waiters, force setting the FUTEX_WAITERS
1688 * bit. We force this here where we are able to easily handle
1689 * faults rather in the requeue loop below.
1690 */
Darren Hart52400ba2009-04-03 13:40:49 -07001691 ret = futex_proxy_trylock_atomic(uaddr2, hb1, hb2, &key1,
Darren Hartbab5bc92009-04-07 23:23:50 -07001692 &key2, &pi_state, nr_requeue);
Darren Hart52400ba2009-04-03 13:40:49 -07001693
1694 /*
1695 * At this point the top_waiter has either taken uaddr2 or is
1696 * waiting on it. If the former, then the pi_state will not
1697 * exist yet, look it up one more time to ensure we have a
Thomas Gleixner866293e2014-05-12 20:45:34 +00001698 * reference to it. If the lock was taken, ret contains the
1699 * vpid of the top waiter task.
Darren Hart52400ba2009-04-03 13:40:49 -07001700 */
Thomas Gleixner866293e2014-05-12 20:45:34 +00001701 if (ret > 0) {
Darren Hart52400ba2009-04-03 13:40:49 -07001702 WARN_ON(pi_state);
Darren Hart89061d32009-10-15 15:30:48 -07001703 drop_count++;
Darren Hart52400ba2009-04-03 13:40:49 -07001704 task_count++;
Thomas Gleixner866293e2014-05-12 20:45:34 +00001705 /*
1706 * If we acquired the lock, then the user
1707 * space value of uaddr2 should be vpid. It
1708 * cannot be changed by the top waiter as it
1709 * is blocked on hb2 lock if it tries to do
1710 * so. If something fiddled with it behind our
1711 * back the pi state lookup might unearth
1712 * it. So we rather use the known value than
1713 * rereading and handing potential crap to
1714 * lookup_pi_state.
1715 */
Thomas Gleixner54a21782014-06-03 12:27:08 +00001716 ret = lookup_pi_state(ret, hb2, &key2, &pi_state);
Darren Hart52400ba2009-04-03 13:40:49 -07001717 }
1718
1719 switch (ret) {
1720 case 0:
1721 break;
1722 case -EFAULT:
Brian Silverman30a6b802014-10-25 20:20:37 -04001723 free_pi_state(pi_state);
1724 pi_state = NULL;
Darren Hart52400ba2009-04-03 13:40:49 -07001725 double_unlock_hb(hb1, hb2);
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07001726 hb_waiters_dec(hb2);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001727 put_futex_key(&key2);
1728 put_futex_key(&key1);
Thomas Gleixnerd0725992009-06-11 23:15:43 +02001729 ret = fault_in_user_writeable(uaddr2);
Darren Hart52400ba2009-04-03 13:40:49 -07001730 if (!ret)
1731 goto retry;
1732 goto out;
1733 case -EAGAIN:
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00001734 /*
1735 * Two reasons for this:
1736 * - Owner is exiting and we just wait for the
1737 * exit to complete.
1738 * - The user space value changed.
1739 */
Brian Silverman30a6b802014-10-25 20:20:37 -04001740 free_pi_state(pi_state);
1741 pi_state = NULL;
Darren Hart52400ba2009-04-03 13:40:49 -07001742 double_unlock_hb(hb1, hb2);
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07001743 hb_waiters_dec(hb2);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001744 put_futex_key(&key2);
1745 put_futex_key(&key1);
Darren Hart52400ba2009-04-03 13:40:49 -07001746 cond_resched();
1747 goto retry;
1748 default:
1749 goto out_unlock;
1750 }
1751 }
1752
Jason Low0d00c7b2014-01-12 15:31:22 -08001753 plist_for_each_entry_safe(this, next, &hb1->chain, list) {
Darren Hart52400ba2009-04-03 13:40:49 -07001754 if (task_count - nr_wake >= nr_requeue)
1755 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Darren Hart52400ba2009-04-03 13:40:49 -07001757 if (!match_futex(&this->key, &key1))
1758 continue;
1759
Darren Hart392741e2009-08-07 15:20:48 -07001760 /*
1761 * FUTEX_WAIT_REQEUE_PI and FUTEX_CMP_REQUEUE_PI should always
1762 * be paired with each other and no other futex ops.
Darren Hartaa109902012-11-26 16:29:56 -08001763 *
1764 * We should never be requeueing a futex_q with a pi_state,
1765 * which is awaiting a futex_unlock_pi().
Darren Hart392741e2009-08-07 15:20:48 -07001766 */
1767 if ((requeue_pi && !this->rt_waiter) ||
Darren Hartaa109902012-11-26 16:29:56 -08001768 (!requeue_pi && this->rt_waiter) ||
1769 this->pi_state) {
Darren Hart392741e2009-08-07 15:20:48 -07001770 ret = -EINVAL;
1771 break;
1772 }
Darren Hart52400ba2009-04-03 13:40:49 -07001773
1774 /*
1775 * Wake nr_wake waiters. For requeue_pi, if we acquired the
1776 * lock, we already woke the top_waiter. If not, it will be
1777 * woken by futex_unlock_pi().
1778 */
1779 if (++task_count <= nr_wake && !requeue_pi) {
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001780 mark_wake_futex(&wake_q, this);
Darren Hart52400ba2009-04-03 13:40:49 -07001781 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 }
Darren Hart52400ba2009-04-03 13:40:49 -07001783
Darren Hart84bc4af2009-08-13 17:36:53 -07001784 /* Ensure we requeue to the expected futex for requeue_pi. */
1785 if (requeue_pi && !match_futex(this->requeue_pi_key, &key2)) {
1786 ret = -EINVAL;
1787 break;
1788 }
1789
Darren Hart52400ba2009-04-03 13:40:49 -07001790 /*
1791 * Requeue nr_requeue waiters and possibly one more in the case
1792 * of requeue_pi if we couldn't acquire the lock atomically.
1793 */
1794 if (requeue_pi) {
1795 /* Prepare the waiter to take the rt_mutex. */
1796 atomic_inc(&pi_state->refcount);
1797 this->pi_state = pi_state;
1798 ret = rt_mutex_start_proxy_lock(&pi_state->pi_mutex,
1799 this->rt_waiter,
Thomas Gleixnerc051b212014-05-22 03:25:50 +00001800 this->task);
Darren Hart52400ba2009-04-03 13:40:49 -07001801 if (ret == 1) {
1802 /* We got the lock. */
Darren Hartbeda2c72009-08-09 15:34:39 -07001803 requeue_pi_wake_futex(this, &key2, hb2);
Darren Hart89061d32009-10-15 15:30:48 -07001804 drop_count++;
Darren Hart52400ba2009-04-03 13:40:49 -07001805 continue;
1806 } else if (ret) {
1807 /* -EDEADLK */
1808 this->pi_state = NULL;
1809 free_pi_state(pi_state);
1810 goto out_unlock;
1811 }
1812 }
1813 requeue_futex(this, hb1, hb2, &key2);
1814 drop_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 }
1816
1817out_unlock:
Brian Silverman30a6b802014-10-25 20:20:37 -04001818 free_pi_state(pi_state);
Darren Hart5eb3dc62009-03-12 00:55:52 -07001819 double_unlock_hb(hb1, hb2);
Davidlohr Bueso1d0dcb32015-05-01 08:27:51 -07001820 wake_up_q(&wake_q);
Linus Torvalds69cd9eb2014-04-08 15:30:07 -07001821 hb_waiters_dec(hb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Darren Hartcd84a422009-04-02 14:19:38 -07001823 /*
1824 * drop_futex_key_refs() must be called outside the spinlocks. During
1825 * the requeue we moved futex_q's from the hash bucket at key1 to the
1826 * one at key2 and updated their key pointer. We no longer need to
1827 * hold the references to key1.
1828 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 while (--drop_count >= 0)
Rusty Russell9adef582007-05-08 00:26:42 -07001830 drop_futex_key_refs(&key1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Darren Hart42d35d42008-12-29 15:49:53 -08001832out_put_keys:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001833 put_futex_key(&key2);
Darren Hart42d35d42008-12-29 15:49:53 -08001834out_put_key1:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001835 put_futex_key(&key1);
Darren Hart42d35d42008-12-29 15:49:53 -08001836out:
Darren Hart52400ba2009-04-03 13:40:49 -07001837 return ret ? ret : task_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838}
1839
1840/* The key must be already stored in q->key. */
Eric Sesterhenn82af7ac2008-01-25 10:40:46 +01001841static inline struct futex_hash_bucket *queue_lock(struct futex_q *q)
Namhyung Kim15e408c2010-09-14 21:43:48 +09001842 __acquires(&hb->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843{
Ingo Molnare2970f22006-06-27 02:54:47 -07001844 struct futex_hash_bucket *hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Ingo Molnare2970f22006-06-27 02:54:47 -07001846 hb = hash_futex(&q->key);
Linus Torvalds11d46162014-03-20 22:11:17 -07001847
1848 /*
1849 * Increment the counter before taking the lock so that
1850 * a potential waker won't miss a to-be-slept task that is
1851 * waiting for the spinlock. This is safe as all queue_lock()
1852 * users end up calling queue_me(). Similarly, for housekeeping,
1853 * decrement the counter at queue_unlock() when some error has
1854 * occurred and we don't end up adding the task to the list.
1855 */
1856 hb_waiters_inc(hb);
1857
Ingo Molnare2970f22006-06-27 02:54:47 -07001858 q->lock_ptr = &hb->lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Davidlohr Buesob0c29f72014-01-12 15:31:25 -08001860 spin_lock(&hb->lock); /* implies MB (A) */
Ingo Molnare2970f22006-06-27 02:54:47 -07001861 return hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862}
1863
Darren Hartd40d65c2009-09-21 22:30:15 -07001864static inline void
Jason Low0d00c7b2014-01-12 15:31:22 -08001865queue_unlock(struct futex_hash_bucket *hb)
Namhyung Kim15e408c2010-09-14 21:43:48 +09001866 __releases(&hb->lock)
Darren Hartd40d65c2009-09-21 22:30:15 -07001867{
1868 spin_unlock(&hb->lock);
Linus Torvalds11d46162014-03-20 22:11:17 -07001869 hb_waiters_dec(hb);
Darren Hartd40d65c2009-09-21 22:30:15 -07001870}
1871
1872/**
1873 * queue_me() - Enqueue the futex_q on the futex_hash_bucket
1874 * @q: The futex_q to enqueue
1875 * @hb: The destination hash bucket
1876 *
1877 * The hb->lock must be held by the caller, and is released here. A call to
1878 * queue_me() is typically paired with exactly one call to unqueue_me(). The
1879 * exceptions involve the PI related operations, which may use unqueue_me_pi()
1880 * or nothing if the unqueue is done as part of the wake process and the unqueue
1881 * state is implicit in the state of woken task (see futex_wait_requeue_pi() for
1882 * an example).
1883 */
Eric Sesterhenn82af7ac2008-01-25 10:40:46 +01001884static inline void queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
Namhyung Kim15e408c2010-09-14 21:43:48 +09001885 __releases(&hb->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
Pierre Peifferec92d082007-05-09 02:35:00 -07001887 int prio;
1888
1889 /*
1890 * The priority used to register this element is
1891 * - either the real thread-priority for the real-time threads
1892 * (i.e. threads with a priority lower than MAX_RT_PRIO)
1893 * - or MAX_RT_PRIO for non-RT threads.
1894 * Thus, all RT-threads are woken first in priority order, and
1895 * the others are woken last, in FIFO order.
1896 */
1897 prio = min(current->normal_prio, MAX_RT_PRIO);
1898
1899 plist_node_init(&q->list, prio);
Pierre Peifferec92d082007-05-09 02:35:00 -07001900 plist_add(&q->list, &hb->chain);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001901 q->task = current;
Ingo Molnare2970f22006-06-27 02:54:47 -07001902 spin_unlock(&hb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903}
1904
Darren Hartd40d65c2009-09-21 22:30:15 -07001905/**
1906 * unqueue_me() - Remove the futex_q from its futex_hash_bucket
1907 * @q: The futex_q to unqueue
1908 *
1909 * The q->lock_ptr must not be held by the caller. A call to unqueue_me() must
1910 * be paired with exactly one earlier call to queue_me().
1911 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08001912 * Return:
1913 * 1 - if the futex_q was still queued (and we removed unqueued it);
Darren Hartd40d65c2009-09-21 22:30:15 -07001914 * 0 - if the futex_q was already removed by the waking thread
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916static int unqueue_me(struct futex_q *q)
1917{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 spinlock_t *lock_ptr;
Ingo Molnare2970f22006-06-27 02:54:47 -07001919 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
1921 /* In the common case we don't take the spinlock, which is nice. */
Darren Hart42d35d42008-12-29 15:49:53 -08001922retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 lock_ptr = q->lock_ptr;
Christian Borntraegere91467e2006-08-05 12:13:52 -07001924 barrier();
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07001925 if (lock_ptr != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 spin_lock(lock_ptr);
1927 /*
1928 * q->lock_ptr can change between reading it and
1929 * spin_lock(), causing us to take the wrong lock. This
1930 * corrects the race condition.
1931 *
1932 * Reasoning goes like this: if we have the wrong lock,
1933 * q->lock_ptr must have changed (maybe several times)
1934 * between reading it and the spin_lock(). It can
1935 * change again after the spin_lock() but only if it was
1936 * already changed before the spin_lock(). It cannot,
1937 * however, change back to the original value. Therefore
1938 * we can detect whether we acquired the correct lock.
1939 */
1940 if (unlikely(lock_ptr != q->lock_ptr)) {
1941 spin_unlock(lock_ptr);
1942 goto retry;
1943 }
Lai Jiangshan2e129782010-12-22 14:18:50 +08001944 __unqueue_futex(q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001945
1946 BUG_ON(q->pi_state);
1947
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 spin_unlock(lock_ptr);
1949 ret = 1;
1950 }
1951
Rusty Russell9adef582007-05-08 00:26:42 -07001952 drop_futex_key_refs(&q->key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 return ret;
1954}
1955
Ingo Molnarc87e2832006-06-27 02:54:58 -07001956/*
1957 * PI futexes can not be requeued and must remove themself from the
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001958 * hash bucket. The hash bucket lock (i.e. lock_ptr) is held on entry
1959 * and dropped here.
Ingo Molnarc87e2832006-06-27 02:54:58 -07001960 */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001961static void unqueue_me_pi(struct futex_q *q)
Namhyung Kim15e408c2010-09-14 21:43:48 +09001962 __releases(q->lock_ptr)
Ingo Molnarc87e2832006-06-27 02:54:58 -07001963{
Lai Jiangshan2e129782010-12-22 14:18:50 +08001964 __unqueue_futex(q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001965
1966 BUG_ON(!q->pi_state);
1967 free_pi_state(q->pi_state);
1968 q->pi_state = NULL;
1969
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001970 spin_unlock(q->lock_ptr);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001971}
1972
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001973/*
Thomas Gleixnercdf71a12008-01-08 19:47:38 +01001974 * Fixup the pi_state owner with the new owner.
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001975 *
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001976 * Must be called with hash bucket lock held and mm->sem held for non
1977 * private futexes.
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001978 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001979static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001980 struct task_struct *newowner)
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001981{
Thomas Gleixnercdf71a12008-01-08 19:47:38 +01001982 u32 newtid = task_pid_vnr(newowner) | FUTEX_WAITERS;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001983 struct futex_pi_state *pi_state = q->pi_state;
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02001984 struct task_struct *oldowner = pi_state->owner;
Vitaliy Ivanov7cfdaf32011-07-07 15:10:31 +03001985 u32 uval, uninitialized_var(curval), newval;
Darren Harte4dc5b72009-03-12 00:56:13 -07001986 int ret;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001987
1988 /* Owner died? */
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02001989 if (!pi_state->owner)
1990 newtid |= FUTEX_OWNER_DIED;
1991
1992 /*
1993 * We are here either because we stole the rtmutex from the
Lai Jiangshan81612392011-01-14 17:09:41 +08001994 * previous highest priority waiter or we are the highest priority
1995 * waiter but failed to get the rtmutex the first time.
1996 * We have to replace the newowner TID in the user space variable.
1997 * This must be atomic as we have to preserve the owner died bit here.
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02001998 *
Darren Hartb2d09942009-03-12 00:55:37 -07001999 * Note: We write the user space value _before_ changing the pi_state
2000 * because we can fault here. Imagine swapped out pages or a fork
2001 * that marked all the anonymous memory readonly for cow.
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002002 *
2003 * Modifying pi_state _before_ the user space value would
2004 * leave the pi_state in an inconsistent state when we fault
2005 * here, because we need to drop the hash bucket lock to
2006 * handle the fault. This might be observed in the PID check
2007 * in lookup_pi_state.
2008 */
2009retry:
2010 if (get_futex_value_locked(&uval, uaddr))
2011 goto handle_fault;
2012
2013 while (1) {
2014 newval = (uval & FUTEX_OWNER_DIED) | newtid;
2015
Michel Lespinasse37a9d912011-03-10 18:48:51 -08002016 if (cmpxchg_futex_value_locked(&curval, uaddr, uval, newval))
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002017 goto handle_fault;
2018 if (curval == uval)
2019 break;
2020 uval = curval;
2021 }
2022
2023 /*
2024 * We fixed up user space. Now we need to fix the pi_state
2025 * itself.
2026 */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002027 if (pi_state->owner != NULL) {
Thomas Gleixner1d615482009-11-17 14:54:03 +01002028 raw_spin_lock_irq(&pi_state->owner->pi_lock);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002029 WARN_ON(list_empty(&pi_state->list));
2030 list_del_init(&pi_state->list);
Thomas Gleixner1d615482009-11-17 14:54:03 +01002031 raw_spin_unlock_irq(&pi_state->owner->pi_lock);
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002032 }
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002033
Thomas Gleixnercdf71a12008-01-08 19:47:38 +01002034 pi_state->owner = newowner;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002035
Thomas Gleixner1d615482009-11-17 14:54:03 +01002036 raw_spin_lock_irq(&newowner->pi_lock);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002037 WARN_ON(!list_empty(&pi_state->list));
Thomas Gleixnercdf71a12008-01-08 19:47:38 +01002038 list_add(&pi_state->list, &newowner->pi_state_list);
Thomas Gleixner1d615482009-11-17 14:54:03 +01002039 raw_spin_unlock_irq(&newowner->pi_lock);
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002040 return 0;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002041
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002042 /*
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002043 * To handle the page fault we need to drop the hash bucket
Lai Jiangshan81612392011-01-14 17:09:41 +08002044 * lock here. That gives the other task (either the highest priority
2045 * waiter itself or the task which stole the rtmutex) the
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002046 * chance to try the fixup of the pi_state. So once we are
2047 * back from handling the fault we need to check the pi_state
2048 * after reacquiring the hash bucket lock and before trying to
2049 * do another fixup. When the fixup has been done already we
2050 * simply return.
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002051 */
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002052handle_fault:
2053 spin_unlock(q->lock_ptr);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002054
Thomas Gleixnerd0725992009-06-11 23:15:43 +02002055 ret = fault_in_user_writeable(uaddr);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002056
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002057 spin_lock(q->lock_ptr);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002058
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02002059 /*
2060 * Check if someone else fixed it for us:
2061 */
2062 if (pi_state->owner != oldowner)
2063 return 0;
2064
2065 if (ret)
2066 return ret;
2067
2068 goto retry;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002069}
2070
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002071static long futex_wait_restart(struct restart_block *restart);
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07002072
Darren Hartca5f9522009-04-03 13:39:33 -07002073/**
Darren Hartdd973992009-04-03 13:40:02 -07002074 * fixup_owner() - Post lock pi_state and corner case management
2075 * @uaddr: user address of the futex
Darren Hartdd973992009-04-03 13:40:02 -07002076 * @q: futex_q (contains pi_state and access to the rt_mutex)
2077 * @locked: if the attempt to take the rt_mutex succeeded (1) or not (0)
2078 *
2079 * After attempting to lock an rt_mutex, this function is called to cleanup
2080 * the pi_state owner as well as handle race conditions that may allow us to
2081 * acquire the lock. Must be called with the hb lock held.
2082 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002083 * Return:
2084 * 1 - success, lock taken;
2085 * 0 - success, lock not taken;
Darren Hartdd973992009-04-03 13:40:02 -07002086 * <0 - on error (-EFAULT)
2087 */
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002088static int fixup_owner(u32 __user *uaddr, struct futex_q *q, int locked)
Darren Hartdd973992009-04-03 13:40:02 -07002089{
2090 struct task_struct *owner;
2091 int ret = 0;
2092
2093 if (locked) {
2094 /*
2095 * Got the lock. We might not be the anticipated owner if we
2096 * did a lock-steal - fix up the PI-state in that case:
2097 */
2098 if (q->pi_state->owner != current)
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002099 ret = fixup_pi_state_owner(uaddr, q, current);
Darren Hartdd973992009-04-03 13:40:02 -07002100 goto out;
2101 }
2102
2103 /*
2104 * Catch the rare case, where the lock was released when we were on the
2105 * way back before we locked the hash bucket.
2106 */
2107 if (q->pi_state->owner == current) {
2108 /*
2109 * Try to get the rt_mutex now. This might fail as some other
2110 * task acquired the rt_mutex after we removed ourself from the
2111 * rt_mutex waiters list.
2112 */
2113 if (rt_mutex_trylock(&q->pi_state->pi_mutex)) {
2114 locked = 1;
2115 goto out;
2116 }
2117
2118 /*
2119 * pi_state is incorrect, some other task did a lock steal and
2120 * we returned due to timeout or signal without taking the
Lai Jiangshan81612392011-01-14 17:09:41 +08002121 * rt_mutex. Too late.
Darren Hartdd973992009-04-03 13:40:02 -07002122 */
Lai Jiangshan81612392011-01-14 17:09:41 +08002123 raw_spin_lock(&q->pi_state->pi_mutex.wait_lock);
Darren Hartdd973992009-04-03 13:40:02 -07002124 owner = rt_mutex_owner(&q->pi_state->pi_mutex);
Lai Jiangshan81612392011-01-14 17:09:41 +08002125 if (!owner)
2126 owner = rt_mutex_next_owner(&q->pi_state->pi_mutex);
2127 raw_spin_unlock(&q->pi_state->pi_mutex.wait_lock);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002128 ret = fixup_pi_state_owner(uaddr, q, owner);
Darren Hartdd973992009-04-03 13:40:02 -07002129 goto out;
2130 }
2131
2132 /*
2133 * Paranoia check. If we did not take the lock, then we should not be
Lai Jiangshan81612392011-01-14 17:09:41 +08002134 * the owner of the rt_mutex.
Darren Hartdd973992009-04-03 13:40:02 -07002135 */
2136 if (rt_mutex_owner(&q->pi_state->pi_mutex) == current)
2137 printk(KERN_ERR "fixup_owner: ret = %d pi-mutex: %p "
2138 "pi-state %p\n", ret,
2139 q->pi_state->pi_mutex.owner,
2140 q->pi_state->owner);
2141
2142out:
2143 return ret ? ret : locked;
2144}
2145
2146/**
Darren Hartca5f9522009-04-03 13:39:33 -07002147 * futex_wait_queue_me() - queue_me() and wait for wakeup, timeout, or signal
2148 * @hb: the futex hash bucket, must be locked by the caller
2149 * @q: the futex_q to queue up on
2150 * @timeout: the prepared hrtimer_sleeper, or null for no timeout
Darren Hartca5f9522009-04-03 13:39:33 -07002151 */
2152static void futex_wait_queue_me(struct futex_hash_bucket *hb, struct futex_q *q,
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02002153 struct hrtimer_sleeper *timeout)
Darren Hartca5f9522009-04-03 13:39:33 -07002154{
Darren Hart9beba3c2009-09-24 11:54:47 -07002155 /*
2156 * The task state is guaranteed to be set before another task can
Peter Zijlstrab92b8b32015-05-12 10:51:55 +02002157 * wake it. set_current_state() is implemented using smp_store_mb() and
Darren Hart9beba3c2009-09-24 11:54:47 -07002158 * queue_me() calls spin_unlock() upon completion, both serializing
2159 * access to the hash list and forcing another memory barrier.
2160 */
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02002161 set_current_state(TASK_INTERRUPTIBLE);
Darren Hart0729e192009-09-21 22:30:38 -07002162 queue_me(q, hb);
Darren Hartca5f9522009-04-03 13:39:33 -07002163
2164 /* Arm the timer */
Thomas Gleixner2e4b0d32015-04-14 21:09:13 +00002165 if (timeout)
Darren Hartca5f9522009-04-03 13:39:33 -07002166 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
Darren Hartca5f9522009-04-03 13:39:33 -07002167
2168 /*
Darren Hart0729e192009-09-21 22:30:38 -07002169 * If we have been removed from the hash list, then another task
2170 * has tried to wake us, and we can skip the call to schedule().
Darren Hartca5f9522009-04-03 13:39:33 -07002171 */
2172 if (likely(!plist_node_empty(&q->list))) {
2173 /*
2174 * If the timer has already expired, current will already be
2175 * flagged for rescheduling. Only call schedule if there
2176 * is no timeout, or if it has yet to expire.
2177 */
2178 if (!timeout || timeout->task)
Colin Cross88c80042013-05-01 18:35:05 -07002179 freezable_schedule();
Darren Hartca5f9522009-04-03 13:39:33 -07002180 }
2181 __set_current_state(TASK_RUNNING);
2182}
2183
Darren Hartf8010732009-04-03 13:40:40 -07002184/**
2185 * futex_wait_setup() - Prepare to wait on a futex
2186 * @uaddr: the futex userspace address
2187 * @val: the expected value
Darren Hartb41277d2010-11-08 13:10:09 -08002188 * @flags: futex flags (FLAGS_SHARED, etc.)
Darren Hartf8010732009-04-03 13:40:40 -07002189 * @q: the associated futex_q
2190 * @hb: storage for hash_bucket pointer to be returned to caller
2191 *
2192 * Setup the futex_q and locate the hash_bucket. Get the futex value and
2193 * compare it with the expected value. Handle atomic faults internally.
2194 * Return with the hb lock held and a q.key reference on success, and unlocked
2195 * with no q.key reference on failure.
2196 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002197 * Return:
2198 * 0 - uaddr contains val and hb has been locked;
Bart Van Asscheca4a04c2011-07-17 09:01:00 +02002199 * <1 - -EFAULT or -EWOULDBLOCK (uaddr does not contain val) and hb is unlocked
Darren Hartf8010732009-04-03 13:40:40 -07002200 */
Darren Hartb41277d2010-11-08 13:10:09 -08002201static int futex_wait_setup(u32 __user *uaddr, u32 val, unsigned int flags,
Darren Hartf8010732009-04-03 13:40:40 -07002202 struct futex_q *q, struct futex_hash_bucket **hb)
2203{
2204 u32 uval;
2205 int ret;
2206
2207 /*
2208 * Access the page AFTER the hash-bucket is locked.
2209 * Order is important:
2210 *
2211 * Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val);
2212 * Userspace waker: if (cond(var)) { var = new; futex_wake(&var); }
2213 *
2214 * The basic logical guarantee of a futex is that it blocks ONLY
2215 * if cond(var) is known to be true at the time of blocking, for
Michel Lespinasse8fe8f542011-03-06 18:07:50 -08002216 * any cond. If we locked the hash-bucket after testing *uaddr, that
2217 * would open a race condition where we could block indefinitely with
Darren Hartf8010732009-04-03 13:40:40 -07002218 * cond(var) false, which would violate the guarantee.
2219 *
Michel Lespinasse8fe8f542011-03-06 18:07:50 -08002220 * On the other hand, we insert q and release the hash-bucket only
2221 * after testing *uaddr. This guarantees that futex_wait() will NOT
2222 * absorb a wakeup if *uaddr does not match the desired values
2223 * while the syscall executes.
Darren Hartf8010732009-04-03 13:40:40 -07002224 */
2225retry:
Shawn Bohrer9ea71502011-06-30 11:21:32 -05002226 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q->key, VERIFY_READ);
Darren Hartf8010732009-04-03 13:40:40 -07002227 if (unlikely(ret != 0))
Darren Harta5a2a0c2009-04-10 09:50:05 -07002228 return ret;
Darren Hartf8010732009-04-03 13:40:40 -07002229
2230retry_private:
2231 *hb = queue_lock(q);
2232
2233 ret = get_futex_value_locked(&uval, uaddr);
2234
2235 if (ret) {
Jason Low0d00c7b2014-01-12 15:31:22 -08002236 queue_unlock(*hb);
Darren Hartf8010732009-04-03 13:40:40 -07002237
2238 ret = get_user(uval, uaddr);
2239 if (ret)
2240 goto out;
2241
Darren Hartb41277d2010-11-08 13:10:09 -08002242 if (!(flags & FLAGS_SHARED))
Darren Hartf8010732009-04-03 13:40:40 -07002243 goto retry_private;
2244
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002245 put_futex_key(&q->key);
Darren Hartf8010732009-04-03 13:40:40 -07002246 goto retry;
2247 }
2248
2249 if (uval != val) {
Jason Low0d00c7b2014-01-12 15:31:22 -08002250 queue_unlock(*hb);
Darren Hartf8010732009-04-03 13:40:40 -07002251 ret = -EWOULDBLOCK;
2252 }
2253
2254out:
2255 if (ret)
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002256 put_futex_key(&q->key);
Darren Hartf8010732009-04-03 13:40:40 -07002257 return ret;
2258}
2259
Darren Hartb41277d2010-11-08 13:10:09 -08002260static int futex_wait(u32 __user *uaddr, unsigned int flags, u32 val,
2261 ktime_t *abs_time, u32 bitset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262{
Darren Hartca5f9522009-04-03 13:39:33 -07002263 struct hrtimer_sleeper timeout, *to = NULL;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002264 struct restart_block *restart;
Ingo Molnare2970f22006-06-27 02:54:47 -07002265 struct futex_hash_bucket *hb;
Darren Hart5bdb05f2010-11-08 13:40:28 -08002266 struct futex_q q = futex_q_init;
Ingo Molnare2970f22006-06-27 02:54:47 -07002267 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
Thomas Gleixnercd689982008-02-01 17:45:14 +01002269 if (!bitset)
2270 return -EINVAL;
Thomas Gleixnercd689982008-02-01 17:45:14 +01002271 q.bitset = bitset;
Darren Hartca5f9522009-04-03 13:39:33 -07002272
2273 if (abs_time) {
2274 to = &timeout;
2275
Darren Hartb41277d2010-11-08 13:10:09 -08002276 hrtimer_init_on_stack(&to->timer, (flags & FLAGS_CLOCKRT) ?
2277 CLOCK_REALTIME : CLOCK_MONOTONIC,
2278 HRTIMER_MODE_ABS);
Darren Hartca5f9522009-04-03 13:39:33 -07002279 hrtimer_init_sleeper(to, current);
2280 hrtimer_set_expires_range_ns(&to->timer, *abs_time,
2281 current->timer_slack_ns);
2282 }
2283
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002284retry:
Darren Hart7ada8762010-10-17 08:35:04 -07002285 /*
2286 * Prepare to wait on uaddr. On success, holds hb lock and increments
2287 * q.key refs.
2288 */
Darren Hartb41277d2010-11-08 13:10:09 -08002289 ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
Darren Hartf8010732009-04-03 13:40:40 -07002290 if (ret)
Darren Hart42d35d42008-12-29 15:49:53 -08002291 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Darren Hartca5f9522009-04-03 13:39:33 -07002293 /* queue_me and wait for wakeup, timeout, or a signal. */
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02002294 futex_wait_queue_me(hb, &q, to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
2296 /* If we were woken (and unqueued), we succeeded, whatever. */
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002297 ret = 0;
Darren Hart7ada8762010-10-17 08:35:04 -07002298 /* unqueue_me() drops q.key ref */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 if (!unqueue_me(&q))
Darren Hart7ada8762010-10-17 08:35:04 -07002300 goto out;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002301 ret = -ETIMEDOUT;
Darren Hartca5f9522009-04-03 13:39:33 -07002302 if (to && !to->task)
Darren Hart7ada8762010-10-17 08:35:04 -07002303 goto out;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002304
Ingo Molnare2970f22006-06-27 02:54:47 -07002305 /*
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002306 * We expect signal_pending(current), but we might be the
2307 * victim of a spurious wakeup as well.
Ingo Molnare2970f22006-06-27 02:54:47 -07002308 */
Darren Hart7ada8762010-10-17 08:35:04 -07002309 if (!signal_pending(current))
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002310 goto retry;
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002311
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002312 ret = -ERESTARTSYS;
Pierre Peifferc19384b2007-05-09 02:35:02 -07002313 if (!abs_time)
Darren Hart7ada8762010-10-17 08:35:04 -07002314 goto out;
Steven Rostedtce6bd422007-12-05 15:46:09 +01002315
Andy Lutomirskif56141e2015-02-12 15:01:14 -08002316 restart = &current->restart_block;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002317 restart->fn = futex_wait_restart;
Namhyung Kima3c74c52010-09-14 21:43:47 +09002318 restart->futex.uaddr = uaddr;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002319 restart->futex.val = val;
2320 restart->futex.time = abs_time->tv64;
2321 restart->futex.bitset = bitset;
Darren Hart0cd9c642011-04-14 15:41:57 -07002322 restart->futex.flags = flags | FLAGS_HAS_TIMEOUT;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002323
2324 ret = -ERESTART_RESTARTBLOCK;
2325
Darren Hart42d35d42008-12-29 15:49:53 -08002326out:
Darren Hartca5f9522009-04-03 13:39:33 -07002327 if (to) {
2328 hrtimer_cancel(&to->timer);
2329 destroy_hrtimer_on_stack(&to->timer);
2330 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07002331 return ret;
2332}
2333
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002334
2335static long futex_wait_restart(struct restart_block *restart)
2336{
Namhyung Kima3c74c52010-09-14 21:43:47 +09002337 u32 __user *uaddr = restart->futex.uaddr;
Darren Harta72188d2009-04-03 13:40:22 -07002338 ktime_t t, *tp = NULL;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002339
Darren Harta72188d2009-04-03 13:40:22 -07002340 if (restart->futex.flags & FLAGS_HAS_TIMEOUT) {
2341 t.tv64 = restart->futex.time;
2342 tp = &t;
2343 }
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002344 restart->fn = do_no_restart_syscall;
Darren Hartb41277d2010-11-08 13:10:09 -08002345
2346 return (long)futex_wait(uaddr, restart->futex.flags,
2347 restart->futex.val, tp, restart->futex.bitset);
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002348}
2349
2350
Ingo Molnarc87e2832006-06-27 02:54:58 -07002351/*
2352 * Userspace tried a 0 -> TID atomic transition of the futex value
2353 * and failed. The kernel side here does the whole locking operation:
Davidlohr Bueso767f5092015-06-29 23:26:01 -07002354 * if there are waiters then it will block as a consequence of relying
2355 * on rt-mutexes, it does PI, etc. (Due to races the kernel might see
2356 * a 0 value of the futex too.).
2357 *
2358 * Also serves as futex trylock_pi()'ing, and due semantics.
Ingo Molnarc87e2832006-06-27 02:54:58 -07002359 */
Michael Kerrisk996636d2015-01-16 20:28:06 +01002360static int futex_lock_pi(u32 __user *uaddr, unsigned int flags,
Darren Hartb41277d2010-11-08 13:10:09 -08002361 ktime_t *time, int trylock)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002362{
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07002363 struct hrtimer_sleeper timeout, *to = NULL;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002364 struct futex_hash_bucket *hb;
Darren Hart5bdb05f2010-11-08 13:40:28 -08002365 struct futex_q q = futex_q_init;
Darren Hartdd973992009-04-03 13:40:02 -07002366 int res, ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002367
2368 if (refill_pi_state_cache())
2369 return -ENOMEM;
2370
Pierre Peifferc19384b2007-05-09 02:35:02 -07002371 if (time) {
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07002372 to = &timeout;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07002373 hrtimer_init_on_stack(&to->timer, CLOCK_REALTIME,
2374 HRTIMER_MODE_ABS);
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07002375 hrtimer_init_sleeper(to, current);
Arjan van de Vencc584b22008-09-01 15:02:30 -07002376 hrtimer_set_expires(&to->timer, *time);
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07002377 }
2378
Darren Hart42d35d42008-12-29 15:49:53 -08002379retry:
Shawn Bohrer9ea71502011-06-30 11:21:32 -05002380 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q.key, VERIFY_WRITE);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002381 if (unlikely(ret != 0))
Darren Hart42d35d42008-12-29 15:49:53 -08002382 goto out;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002383
Darren Harte4dc5b72009-03-12 00:56:13 -07002384retry_private:
Eric Sesterhenn82af7ac2008-01-25 10:40:46 +01002385 hb = queue_lock(&q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002386
Darren Hartbab5bc92009-04-07 23:23:50 -07002387 ret = futex_lock_pi_atomic(uaddr, hb, &q.key, &q.pi_state, current, 0);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002388 if (unlikely(ret)) {
Davidlohr Bueso767f5092015-06-29 23:26:01 -07002389 /*
2390 * Atomic work succeeded and we got the lock,
2391 * or failed. Either way, we do _not_ block.
2392 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002393 switch (ret) {
Darren Hart1a520842009-04-03 13:39:52 -07002394 case 1:
2395 /* We got the lock. */
2396 ret = 0;
2397 goto out_unlock_put_key;
2398 case -EFAULT:
2399 goto uaddr_faulted;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002400 case -EAGAIN:
2401 /*
Thomas Gleixneraf54d6a2014-06-11 20:45:41 +00002402 * Two reasons for this:
2403 * - Task is exiting and we just wait for the
2404 * exit to complete.
2405 * - The user space value changed.
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002406 */
Jason Low0d00c7b2014-01-12 15:31:22 -08002407 queue_unlock(hb);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002408 put_futex_key(&q.key);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002409 cond_resched();
2410 goto retry;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002411 default:
Darren Hart42d35d42008-12-29 15:49:53 -08002412 goto out_unlock_put_key;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002413 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07002414 }
2415
2416 /*
2417 * Only actually queue now that the atomic ops are done:
2418 */
Eric Sesterhenn82af7ac2008-01-25 10:40:46 +01002419 queue_me(&q, hb);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002420
Ingo Molnarc87e2832006-06-27 02:54:58 -07002421 WARN_ON(!q.pi_state);
2422 /*
2423 * Block on the PI mutex:
2424 */
Thomas Gleixnerc051b212014-05-22 03:25:50 +00002425 if (!trylock) {
2426 ret = rt_mutex_timed_futex_lock(&q.pi_state->pi_mutex, to);
2427 } else {
Ingo Molnarc87e2832006-06-27 02:54:58 -07002428 ret = rt_mutex_trylock(&q.pi_state->pi_mutex);
2429 /* Fixup the trylock return value: */
2430 ret = ret ? 0 : -EWOULDBLOCK;
2431 }
2432
Vernon Mauerya99e4e42006-07-01 04:35:42 -07002433 spin_lock(q.lock_ptr);
Darren Hartdd973992009-04-03 13:40:02 -07002434 /*
2435 * Fixup the pi_state owner and possibly acquire the lock if we
2436 * haven't already.
2437 */
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002438 res = fixup_owner(uaddr, &q, !ret);
Darren Hartdd973992009-04-03 13:40:02 -07002439 /*
2440 * If fixup_owner() returned an error, proprogate that. If it acquired
2441 * the lock, clear our -ETIMEDOUT or -EINTR.
2442 */
2443 if (res)
2444 ret = (res < 0) ? res : 0;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002445
Darren Harte8f63862009-03-12 00:56:06 -07002446 /*
Darren Hartdd973992009-04-03 13:40:02 -07002447 * If fixup_owner() faulted and was unable to handle the fault, unlock
2448 * it and return the fault to userspace.
Darren Harte8f63862009-03-12 00:56:06 -07002449 */
2450 if (ret && (rt_mutex_owner(&q.pi_state->pi_mutex) == current))
2451 rt_mutex_unlock(&q.pi_state->pi_mutex);
2452
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002453 /* Unqueue and drop the lock */
2454 unqueue_me_pi(&q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002455
Mikael Pettersson5ecb01c2010-01-23 22:36:29 +01002456 goto out_put_key;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002457
Darren Hart42d35d42008-12-29 15:49:53 -08002458out_unlock_put_key:
Jason Low0d00c7b2014-01-12 15:31:22 -08002459 queue_unlock(hb);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002460
Darren Hart42d35d42008-12-29 15:49:53 -08002461out_put_key:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002462 put_futex_key(&q.key);
Darren Hart42d35d42008-12-29 15:49:53 -08002463out:
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07002464 if (to)
2465 destroy_hrtimer_on_stack(&to->timer);
Darren Hartdd973992009-04-03 13:40:02 -07002466 return ret != -EINTR ? ret : -ERESTARTNOINTR;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002467
Darren Hart42d35d42008-12-29 15:49:53 -08002468uaddr_faulted:
Jason Low0d00c7b2014-01-12 15:31:22 -08002469 queue_unlock(hb);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002470
Thomas Gleixnerd0725992009-06-11 23:15:43 +02002471 ret = fault_in_user_writeable(uaddr);
Darren Harte4dc5b72009-03-12 00:56:13 -07002472 if (ret)
2473 goto out_put_key;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002474
Darren Hartb41277d2010-11-08 13:10:09 -08002475 if (!(flags & FLAGS_SHARED))
Darren Harte4dc5b72009-03-12 00:56:13 -07002476 goto retry_private;
2477
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002478 put_futex_key(&q.key);
Darren Harte4dc5b72009-03-12 00:56:13 -07002479 goto retry;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002480}
2481
2482/*
Ingo Molnarc87e2832006-06-27 02:54:58 -07002483 * Userspace attempted a TID -> 0 atomic transition, and failed.
2484 * This is the in-kernel slowpath: we look up the PI state (if any),
2485 * and do the rt-mutex unlock.
2486 */
Darren Hartb41277d2010-11-08 13:10:09 -08002487static int futex_unlock_pi(u32 __user *uaddr, unsigned int flags)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002488{
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002489 u32 uninitialized_var(curval), uval, vpid = task_pid_vnr(current);
Peter Zijlstra38d47c12008-09-26 19:32:20 +02002490 union futex_key key = FUTEX_KEY_INIT;
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002491 struct futex_hash_bucket *hb;
2492 struct futex_q *match;
Darren Harte4dc5b72009-03-12 00:56:13 -07002493 int ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002494
2495retry:
2496 if (get_user(uval, uaddr))
2497 return -EFAULT;
2498 /*
2499 * We release only a lock we actually own:
2500 */
Thomas Gleixnerc0c9ed12011-03-11 11:51:22 +01002501 if ((uval & FUTEX_TID_MASK) != vpid)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002502 return -EPERM;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002503
Shawn Bohrer9ea71502011-06-30 11:21:32 -05002504 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, VERIFY_WRITE);
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002505 if (ret)
2506 return ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002507
2508 hb = hash_futex(&key);
2509 spin_lock(&hb->lock);
2510
Ingo Molnarc87e2832006-06-27 02:54:58 -07002511 /*
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002512 * Check waiters first. We do not trust user space values at
2513 * all and we at least want to know if user space fiddled
2514 * with the futex value instead of blindly unlocking.
Ingo Molnarc87e2832006-06-27 02:54:58 -07002515 */
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002516 match = futex_top_waiter(hb, &key);
2517 if (match) {
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02002518 ret = wake_futex_pi(uaddr, uval, match, hb);
2519 /*
2520 * In case of success wake_futex_pi dropped the hash
2521 * bucket lock.
2522 */
2523 if (!ret)
2524 goto out_putkey;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002525 /*
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002526 * The atomic access to the futex value generated a
2527 * pagefault, so retry the user-access and the wakeup:
Ingo Molnarc87e2832006-06-27 02:54:58 -07002528 */
2529 if (ret == -EFAULT)
2530 goto pi_faulted;
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02002531 /*
2532 * wake_futex_pi has detected invalid state. Tell user
2533 * space.
2534 */
Ingo Molnarc87e2832006-06-27 02:54:58 -07002535 goto out_unlock;
2536 }
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002537
Ingo Molnarc87e2832006-06-27 02:54:58 -07002538 /*
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002539 * We have no kernel internal state, i.e. no waiters in the
2540 * kernel. Waiters which are about to queue themselves are stuck
2541 * on hb->lock. So we can safely ignore them. We do neither
2542 * preserve the WAITERS bit not the OWNER_DIED one. We are the
2543 * owner.
Ingo Molnarc87e2832006-06-27 02:54:58 -07002544 */
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002545 if (cmpxchg_futex_value_locked(&curval, uaddr, uval, 0))
Thomas Gleixner13fbca42014-06-03 12:27:07 +00002546 goto pi_faulted;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002547
Thomas Gleixnerccf9e6a2014-06-11 20:45:38 +00002548 /*
2549 * If uval has changed, let user space handle it.
2550 */
2551 ret = (curval == uval) ? 0 : -EAGAIN;
2552
Ingo Molnarc87e2832006-06-27 02:54:58 -07002553out_unlock:
2554 spin_unlock(&hb->lock);
Sebastian Andrzej Siewior802ab582015-06-17 10:33:50 +02002555out_putkey:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002556 put_futex_key(&key);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002557 return ret;
2558
2559pi_faulted:
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002560 spin_unlock(&hb->lock);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002561 put_futex_key(&key);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002562
Thomas Gleixnerd0725992009-06-11 23:15:43 +02002563 ret = fault_in_user_writeable(uaddr);
Darren Hartb5686362008-12-18 15:06:34 -08002564 if (!ret)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002565 goto retry;
2566
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 return ret;
2568}
2569
Darren Hart52400ba2009-04-03 13:40:49 -07002570/**
2571 * handle_early_requeue_pi_wakeup() - Detect early wakeup on the initial futex
2572 * @hb: the hash_bucket futex_q was original enqueued on
2573 * @q: the futex_q woken while waiting to be requeued
2574 * @key2: the futex_key of the requeue target futex
2575 * @timeout: the timeout associated with the wait (NULL if none)
2576 *
2577 * Detect if the task was woken on the initial futex as opposed to the requeue
2578 * target futex. If so, determine if it was a timeout or a signal that caused
2579 * the wakeup and return the appropriate error code to the caller. Must be
2580 * called with the hb lock held.
2581 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002582 * Return:
2583 * 0 = no early wakeup detected;
2584 * <0 = -ETIMEDOUT or -ERESTARTNOINTR
Darren Hart52400ba2009-04-03 13:40:49 -07002585 */
2586static inline
2587int handle_early_requeue_pi_wakeup(struct futex_hash_bucket *hb,
2588 struct futex_q *q, union futex_key *key2,
2589 struct hrtimer_sleeper *timeout)
2590{
2591 int ret = 0;
2592
2593 /*
2594 * With the hb lock held, we avoid races while we process the wakeup.
2595 * We only need to hold hb (and not hb2) to ensure atomicity as the
2596 * wakeup code can't change q.key from uaddr to uaddr2 if we hold hb.
2597 * It can't be requeued from uaddr2 to something else since we don't
2598 * support a PI aware source futex for requeue.
2599 */
2600 if (!match_futex(&q->key, key2)) {
2601 WARN_ON(q->lock_ptr && (&hb->lock != q->lock_ptr));
2602 /*
2603 * We were woken prior to requeue by a timeout or a signal.
2604 * Unqueue the futex_q and determine which it was.
2605 */
Lai Jiangshan2e129782010-12-22 14:18:50 +08002606 plist_del(&q->list, &hb->chain);
Linus Torvalds11d46162014-03-20 22:11:17 -07002607 hb_waiters_dec(hb);
Darren Hart52400ba2009-04-03 13:40:49 -07002608
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002609 /* Handle spurious wakeups gracefully */
Thomas Gleixner11df6dd2009-10-28 20:26:48 +01002610 ret = -EWOULDBLOCK;
Darren Hart52400ba2009-04-03 13:40:49 -07002611 if (timeout && !timeout->task)
2612 ret = -ETIMEDOUT;
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002613 else if (signal_pending(current))
Thomas Gleixner1c840c12009-05-20 09:22:40 +02002614 ret = -ERESTARTNOINTR;
Darren Hart52400ba2009-04-03 13:40:49 -07002615 }
2616 return ret;
2617}
2618
2619/**
2620 * futex_wait_requeue_pi() - Wait on uaddr and take uaddr2
Darren Hart56ec1602009-09-21 22:29:59 -07002621 * @uaddr: the futex we initially wait on (non-pi)
Darren Hartb41277d2010-11-08 13:10:09 -08002622 * @flags: futex flags (FLAGS_SHARED, FLAGS_CLOCKRT, etc.), they must be
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07002623 * the same type, no requeueing from private to shared, etc.
Darren Hart52400ba2009-04-03 13:40:49 -07002624 * @val: the expected value of uaddr
2625 * @abs_time: absolute timeout
Darren Hart56ec1602009-09-21 22:29:59 -07002626 * @bitset: 32 bit wakeup bitset set by userspace, defaults to all
Darren Hart52400ba2009-04-03 13:40:49 -07002627 * @uaddr2: the pi futex we will take prior to returning to user-space
2628 *
2629 * The caller will wait on uaddr and will be requeued by futex_requeue() to
Darren Hart6f7b0a22012-07-20 11:53:31 -07002630 * uaddr2 which must be PI aware and unique from uaddr. Normal wakeup will wake
2631 * on uaddr2 and complete the acquisition of the rt_mutex prior to returning to
2632 * userspace. This ensures the rt_mutex maintains an owner when it has waiters;
2633 * without one, the pi logic would not know which task to boost/deboost, if
2634 * there was a need to.
Darren Hart52400ba2009-04-03 13:40:49 -07002635 *
2636 * We call schedule in futex_wait_queue_me() when we enqueue and return there
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002637 * via the following--
Darren Hart52400ba2009-04-03 13:40:49 -07002638 * 1) wakeup on uaddr2 after an atomic lock acquisition by futex_requeue()
Darren Hartcc6db4e2009-07-31 16:20:10 -07002639 * 2) wakeup on uaddr2 after a requeue
2640 * 3) signal
2641 * 4) timeout
Darren Hart52400ba2009-04-03 13:40:49 -07002642 *
Darren Hartcc6db4e2009-07-31 16:20:10 -07002643 * If 3, cleanup and return -ERESTARTNOINTR.
Darren Hart52400ba2009-04-03 13:40:49 -07002644 *
2645 * If 2, we may then block on trying to take the rt_mutex and return via:
2646 * 5) successful lock
2647 * 6) signal
2648 * 7) timeout
2649 * 8) other lock acquisition failure
2650 *
Darren Hartcc6db4e2009-07-31 16:20:10 -07002651 * If 6, return -EWOULDBLOCK (restarting the syscall would do the same).
Darren Hart52400ba2009-04-03 13:40:49 -07002652 *
2653 * If 4 or 7, we cleanup and return with -ETIMEDOUT.
2654 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002655 * Return:
2656 * 0 - On success;
Darren Hart52400ba2009-04-03 13:40:49 -07002657 * <0 - On error
2658 */
Darren Hartb41277d2010-11-08 13:10:09 -08002659static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
Darren Hart52400ba2009-04-03 13:40:49 -07002660 u32 val, ktime_t *abs_time, u32 bitset,
Darren Hartb41277d2010-11-08 13:10:09 -08002661 u32 __user *uaddr2)
Darren Hart52400ba2009-04-03 13:40:49 -07002662{
2663 struct hrtimer_sleeper timeout, *to = NULL;
2664 struct rt_mutex_waiter rt_waiter;
2665 struct rt_mutex *pi_mutex = NULL;
Darren Hart52400ba2009-04-03 13:40:49 -07002666 struct futex_hash_bucket *hb;
Darren Hart5bdb05f2010-11-08 13:40:28 -08002667 union futex_key key2 = FUTEX_KEY_INIT;
2668 struct futex_q q = futex_q_init;
Darren Hart52400ba2009-04-03 13:40:49 -07002669 int res, ret;
Darren Hart52400ba2009-04-03 13:40:49 -07002670
Darren Hart6f7b0a22012-07-20 11:53:31 -07002671 if (uaddr == uaddr2)
2672 return -EINVAL;
2673
Darren Hart52400ba2009-04-03 13:40:49 -07002674 if (!bitset)
2675 return -EINVAL;
2676
2677 if (abs_time) {
2678 to = &timeout;
Darren Hartb41277d2010-11-08 13:10:09 -08002679 hrtimer_init_on_stack(&to->timer, (flags & FLAGS_CLOCKRT) ?
2680 CLOCK_REALTIME : CLOCK_MONOTONIC,
2681 HRTIMER_MODE_ABS);
Darren Hart52400ba2009-04-03 13:40:49 -07002682 hrtimer_init_sleeper(to, current);
2683 hrtimer_set_expires_range_ns(&to->timer, *abs_time,
2684 current->timer_slack_ns);
2685 }
2686
2687 /*
2688 * The waiter is allocated on our stack, manipulated by the requeue
2689 * code while we sleep on uaddr.
2690 */
2691 debug_rt_mutex_init_waiter(&rt_waiter);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01002692 RB_CLEAR_NODE(&rt_waiter.pi_tree_entry);
2693 RB_CLEAR_NODE(&rt_waiter.tree_entry);
Darren Hart52400ba2009-04-03 13:40:49 -07002694 rt_waiter.task = NULL;
2695
Shawn Bohrer9ea71502011-06-30 11:21:32 -05002696 ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE);
Darren Hart52400ba2009-04-03 13:40:49 -07002697 if (unlikely(ret != 0))
2698 goto out;
2699
Darren Hart84bc4af2009-08-13 17:36:53 -07002700 q.bitset = bitset;
2701 q.rt_waiter = &rt_waiter;
2702 q.requeue_pi_key = &key2;
2703
Darren Hart7ada8762010-10-17 08:35:04 -07002704 /*
2705 * Prepare to wait on uaddr. On success, increments q.key (key1) ref
2706 * count.
2707 */
Darren Hartb41277d2010-11-08 13:10:09 -08002708 ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
Thomas Gleixnerc8b15a72009-05-20 09:18:50 +02002709 if (ret)
2710 goto out_key2;
Darren Hart52400ba2009-04-03 13:40:49 -07002711
Thomas Gleixnere9c243a2014-06-03 12:27:06 +00002712 /*
2713 * The check above which compares uaddrs is not sufficient for
2714 * shared futexes. We need to compare the keys:
2715 */
2716 if (match_futex(&q.key, &key2)) {
Thomas Gleixner13c42c22014-09-11 23:44:35 +02002717 queue_unlock(hb);
Thomas Gleixnere9c243a2014-06-03 12:27:06 +00002718 ret = -EINVAL;
2719 goto out_put_keys;
2720 }
2721
Darren Hart52400ba2009-04-03 13:40:49 -07002722 /* Queue the futex_q, drop the hb lock, wait for wakeup. */
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02002723 futex_wait_queue_me(hb, &q, to);
Darren Hart52400ba2009-04-03 13:40:49 -07002724
2725 spin_lock(&hb->lock);
2726 ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
2727 spin_unlock(&hb->lock);
2728 if (ret)
2729 goto out_put_keys;
2730
2731 /*
2732 * In order for us to be here, we know our q.key == key2, and since
2733 * we took the hb->lock above, we also know that futex_requeue() has
2734 * completed and we no longer have to concern ourselves with a wakeup
Darren Hart7ada8762010-10-17 08:35:04 -07002735 * race with the atomic proxy lock acquisition by the requeue code. The
2736 * futex_requeue dropped our key1 reference and incremented our key2
2737 * reference count.
Darren Hart52400ba2009-04-03 13:40:49 -07002738 */
2739
2740 /* Check if the requeue code acquired the second futex for us. */
2741 if (!q.rt_waiter) {
2742 /*
2743 * Got the lock. We might not be the anticipated owner if we
2744 * did a lock-steal - fix up the PI-state in that case.
2745 */
2746 if (q.pi_state && (q.pi_state->owner != current)) {
2747 spin_lock(q.lock_ptr);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002748 ret = fixup_pi_state_owner(uaddr2, &q, current);
Darren Hart52400ba2009-04-03 13:40:49 -07002749 spin_unlock(q.lock_ptr);
2750 }
2751 } else {
2752 /*
2753 * We have been woken up by futex_unlock_pi(), a timeout, or a
2754 * signal. futex_unlock_pi() will not destroy the lock_ptr nor
2755 * the pi_state.
2756 */
Darren Hartf27071c2012-07-20 11:53:30 -07002757 WARN_ON(!q.pi_state);
Darren Hart52400ba2009-04-03 13:40:49 -07002758 pi_mutex = &q.pi_state->pi_mutex;
Thomas Gleixnerc051b212014-05-22 03:25:50 +00002759 ret = rt_mutex_finish_proxy_lock(pi_mutex, to, &rt_waiter);
Darren Hart52400ba2009-04-03 13:40:49 -07002760 debug_rt_mutex_free_waiter(&rt_waiter);
2761
2762 spin_lock(q.lock_ptr);
2763 /*
2764 * Fixup the pi_state owner and possibly acquire the lock if we
2765 * haven't already.
2766 */
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002767 res = fixup_owner(uaddr2, &q, !ret);
Darren Hart52400ba2009-04-03 13:40:49 -07002768 /*
2769 * If fixup_owner() returned an error, proprogate that. If it
Darren Hart56ec1602009-09-21 22:29:59 -07002770 * acquired the lock, clear -ETIMEDOUT or -EINTR.
Darren Hart52400ba2009-04-03 13:40:49 -07002771 */
2772 if (res)
2773 ret = (res < 0) ? res : 0;
2774
2775 /* Unqueue and drop the lock. */
2776 unqueue_me_pi(&q);
2777 }
2778
2779 /*
2780 * If fixup_pi_state_owner() faulted and was unable to handle the
2781 * fault, unlock the rt_mutex and return the fault to userspace.
2782 */
2783 if (ret == -EFAULT) {
Darren Hartb6070a82012-07-20 11:53:29 -07002784 if (pi_mutex && rt_mutex_owner(pi_mutex) == current)
Darren Hart52400ba2009-04-03 13:40:49 -07002785 rt_mutex_unlock(pi_mutex);
2786 } else if (ret == -EINTR) {
Darren Hart52400ba2009-04-03 13:40:49 -07002787 /*
Darren Hartcc6db4e2009-07-31 16:20:10 -07002788 * We've already been requeued, but cannot restart by calling
2789 * futex_lock_pi() directly. We could restart this syscall, but
2790 * it would detect that the user space "val" changed and return
2791 * -EWOULDBLOCK. Save the overhead of the restart and return
2792 * -EWOULDBLOCK directly.
Darren Hart52400ba2009-04-03 13:40:49 -07002793 */
Thomas Gleixner20708872009-05-19 23:04:59 +02002794 ret = -EWOULDBLOCK;
Darren Hart52400ba2009-04-03 13:40:49 -07002795 }
2796
2797out_put_keys:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002798 put_futex_key(&q.key);
Thomas Gleixnerc8b15a72009-05-20 09:18:50 +02002799out_key2:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002800 put_futex_key(&key2);
Darren Hart52400ba2009-04-03 13:40:49 -07002801
2802out:
2803 if (to) {
2804 hrtimer_cancel(&to->timer);
2805 destroy_hrtimer_on_stack(&to->timer);
2806 }
2807 return ret;
2808}
2809
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002810/*
2811 * Support for robust futexes: the kernel cleans up held futexes at
2812 * thread exit time.
2813 *
2814 * Implementation: user-space maintains a per-thread list of locks it
2815 * is holding. Upon do_exit(), the kernel carefully walks this list,
2816 * and marks all locks that are owned by this thread with the
Ingo Molnarc87e2832006-06-27 02:54:58 -07002817 * FUTEX_OWNER_DIED bit, and wakes up a waiter (if any). The list is
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002818 * always manipulated with the lock held, so the list is private and
2819 * per-thread. Userspace also maintains a per-thread 'list_op_pending'
2820 * field, to allow the kernel to clean up if the thread dies after
2821 * acquiring the lock, but just before it could have added itself to
2822 * the list. There can only be one such pending lock.
2823 */
2824
2825/**
Darren Hartd96ee562009-09-21 22:30:22 -07002826 * sys_set_robust_list() - Set the robust-futex list head of a task
2827 * @head: pointer to the list-head
2828 * @len: length of the list-head, as userspace expects
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002829 */
Heiko Carstens836f92a2009-01-14 14:14:33 +01002830SYSCALL_DEFINE2(set_robust_list, struct robust_list_head __user *, head,
2831 size_t, len)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002832{
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08002833 if (!futex_cmpxchg_enabled)
2834 return -ENOSYS;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002835 /*
2836 * The kernel knows only one size for now:
2837 */
2838 if (unlikely(len != sizeof(*head)))
2839 return -EINVAL;
2840
2841 current->robust_list = head;
2842
2843 return 0;
2844}
2845
2846/**
Darren Hartd96ee562009-09-21 22:30:22 -07002847 * sys_get_robust_list() - Get the robust-futex list head of a task
2848 * @pid: pid of the process [zero for current task]
2849 * @head_ptr: pointer to a list-head pointer, the kernel fills it in
2850 * @len_ptr: pointer to a length field, the kernel fills in the header size
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002851 */
Heiko Carstens836f92a2009-01-14 14:14:33 +01002852SYSCALL_DEFINE3(get_robust_list, int, pid,
2853 struct robust_list_head __user * __user *, head_ptr,
2854 size_t __user *, len_ptr)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002855{
Al Viroba46df92006-10-10 22:46:07 +01002856 struct robust_list_head __user *head;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002857 unsigned long ret;
Kees Cookbdbb7762012-03-19 16:12:53 -07002858 struct task_struct *p;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002859
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08002860 if (!futex_cmpxchg_enabled)
2861 return -ENOSYS;
2862
Kees Cookbdbb7762012-03-19 16:12:53 -07002863 rcu_read_lock();
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002864
Kees Cookbdbb7762012-03-19 16:12:53 -07002865 ret = -ESRCH;
2866 if (!pid)
2867 p = current;
2868 else {
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07002869 p = find_task_by_vpid(pid);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002870 if (!p)
2871 goto err_unlock;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002872 }
2873
Kees Cookbdbb7762012-03-19 16:12:53 -07002874 ret = -EPERM;
2875 if (!ptrace_may_access(p, PTRACE_MODE_READ))
2876 goto err_unlock;
2877
2878 head = p->robust_list;
2879 rcu_read_unlock();
2880
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002881 if (put_user(sizeof(*head), len_ptr))
2882 return -EFAULT;
2883 return put_user(head, head_ptr);
2884
2885err_unlock:
Oleg Nesterovaaa2a972006-09-29 02:00:55 -07002886 rcu_read_unlock();
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002887
2888 return ret;
2889}
2890
2891/*
2892 * Process a futex-list entry, check whether it's owned by the
2893 * dying task, and do notification if so:
2894 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002895int handle_futex_death(u32 __user *uaddr, struct task_struct *curr, int pi)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002896{
Vitaliy Ivanov7cfdaf32011-07-07 15:10:31 +03002897 u32 uval, uninitialized_var(nval), mval;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002898
Ingo Molnar8f17d3a2006-03-27 01:16:27 -08002899retry:
2900 if (get_user(uval, uaddr))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002901 return -1;
2902
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002903 if ((uval & FUTEX_TID_MASK) == task_pid_vnr(curr)) {
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002904 /*
2905 * Ok, this dying thread is truly holding a futex
2906 * of interest. Set the OWNER_DIED bit atomically
2907 * via cmpxchg, and if the value had FUTEX_WAITERS
2908 * set, wake up a waiter (if any). (We have to do a
2909 * futex_wake() even if OWNER_DIED is already set -
2910 * to handle the rare but possible case of recursive
2911 * thread-death.) The rest of the cleanup is done in
2912 * userspace.
2913 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002914 mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
Thomas Gleixner6e0aa9f2011-03-14 10:34:35 +01002915 /*
2916 * We are not holding a lock here, but we want to have
2917 * the pagefault_disable/enable() protection because
2918 * we want to handle the fault gracefully. If the
2919 * access fails we try to fault in the futex with R/W
2920 * verification via get_user_pages. get_user() above
2921 * does not guarantee R/W access. If that fails we
2922 * give up and leave the futex locked.
2923 */
2924 if (cmpxchg_futex_value_locked(&nval, uaddr, uval, mval)) {
2925 if (fault_in_user_writeable(uaddr))
2926 return -1;
2927 goto retry;
2928 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07002929 if (nval != uval)
Ingo Molnar8f17d3a2006-03-27 01:16:27 -08002930 goto retry;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002931
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002932 /*
2933 * Wake robust non-PI futexes here. The wakeup of
2934 * PI futexes happens in exit_pi_state():
2935 */
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07002936 if (!pi && (uval & FUTEX_WAITERS))
Peter Zijlstrac2f9f202008-09-26 19:32:23 +02002937 futex_wake(uaddr, 1, 1, FUTEX_BITSET_MATCH_ANY);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002938 }
2939 return 0;
2940}
2941
2942/*
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002943 * Fetch a robust-list pointer. Bit 0 signals PI futexes:
2944 */
2945static inline int fetch_robust_entry(struct robust_list __user **entry,
Al Viroba46df92006-10-10 22:46:07 +01002946 struct robust_list __user * __user *head,
Namhyung Kim1dcc41b2010-09-14 21:43:46 +09002947 unsigned int *pi)
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002948{
2949 unsigned long uentry;
2950
Al Viroba46df92006-10-10 22:46:07 +01002951 if (get_user(uentry, (unsigned long __user *)head))
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002952 return -EFAULT;
2953
Al Viroba46df92006-10-10 22:46:07 +01002954 *entry = (void __user *)(uentry & ~1UL);
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002955 *pi = uentry & 1;
2956
2957 return 0;
2958}
2959
2960/*
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002961 * Walk curr->robust_list (very carefully, it's a userspace list!)
2962 * and mark any locks found there dead, and notify any waiters.
2963 *
2964 * We silently return on any sign of list-walking problem.
2965 */
2966void exit_robust_list(struct task_struct *curr)
2967{
2968 struct robust_list_head __user *head = curr->robust_list;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07002969 struct robust_list __user *entry, *next_entry, *pending;
Darren Hart4c115e92010-11-04 15:00:00 -04002970 unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
2971 unsigned int uninitialized_var(next_pi);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002972 unsigned long futex_offset;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07002973 int rc;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002974
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08002975 if (!futex_cmpxchg_enabled)
2976 return;
2977
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002978 /*
2979 * Fetch the list head (which was registered earlier, via
2980 * sys_set_robust_list()):
2981 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002982 if (fetch_robust_entry(&entry, &head->list.next, &pi))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002983 return;
2984 /*
2985 * Fetch the relative futex offset:
2986 */
2987 if (get_user(futex_offset, &head->futex_offset))
2988 return;
2989 /*
2990 * Fetch any possibly pending lock-add first, and handle it
2991 * if it exists:
2992 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002993 if (fetch_robust_entry(&pending, &head->list_op_pending, &pip))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002994 return;
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002995
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07002996 next_entry = NULL; /* avoid warning with gcc */
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002997 while (entry != &head->list) {
2998 /*
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07002999 * Fetch the next entry in the list before calling
3000 * handle_futex_death:
3001 */
3002 rc = fetch_robust_entry(&next_entry, &entry->next, &next_pi);
3003 /*
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003004 * A pending lock might already be on the list, so
Ingo Molnarc87e2832006-06-27 02:54:58 -07003005 * don't process it twice:
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003006 */
3007 if (entry != pending)
Al Viroba46df92006-10-10 22:46:07 +01003008 if (handle_futex_death((void __user *)entry + futex_offset,
Ingo Molnare3f2dde2006-07-29 05:17:57 +02003009 curr, pi))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003010 return;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003011 if (rc)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003012 return;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003013 entry = next_entry;
3014 pi = next_pi;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003015 /*
3016 * Avoid excessively long or circular lists:
3017 */
3018 if (!--limit)
3019 break;
3020
3021 cond_resched();
3022 }
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07003023
3024 if (pending)
3025 handle_futex_death((void __user *)pending + futex_offset,
3026 curr, pip);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08003027}
3028
Pierre Peifferc19384b2007-05-09 02:35:02 -07003029long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
Ingo Molnare2970f22006-06-27 02:54:47 -07003030 u32 __user *uaddr2, u32 val2, u32 val3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031{
Thomas Gleixner81b40532012-02-15 12:17:09 +01003032 int cmd = op & FUTEX_CMD_MASK;
Darren Hartb41277d2010-11-08 13:10:09 -08003033 unsigned int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003035 if (!(op & FUTEX_PRIVATE_FLAG))
Darren Hartb41277d2010-11-08 13:10:09 -08003036 flags |= FLAGS_SHARED;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003037
Darren Hartb41277d2010-11-08 13:10:09 -08003038 if (op & FUTEX_CLOCK_REALTIME) {
3039 flags |= FLAGS_CLOCKRT;
3040 if (cmd != FUTEX_WAIT_BITSET && cmd != FUTEX_WAIT_REQUEUE_PI)
3041 return -ENOSYS;
3042 }
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003043
3044 switch (cmd) {
Thomas Gleixner59263b52012-02-15 12:08:34 +01003045 case FUTEX_LOCK_PI:
3046 case FUTEX_UNLOCK_PI:
3047 case FUTEX_TRYLOCK_PI:
3048 case FUTEX_WAIT_REQUEUE_PI:
3049 case FUTEX_CMP_REQUEUE_PI:
3050 if (!futex_cmpxchg_enabled)
3051 return -ENOSYS;
3052 }
3053
3054 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 case FUTEX_WAIT:
Thomas Gleixnercd689982008-02-01 17:45:14 +01003056 val3 = FUTEX_BITSET_MATCH_ANY;
3057 case FUTEX_WAIT_BITSET:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003058 return futex_wait(uaddr, flags, val, timeout, val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 case FUTEX_WAKE:
Thomas Gleixnercd689982008-02-01 17:45:14 +01003060 val3 = FUTEX_BITSET_MATCH_ANY;
3061 case FUTEX_WAKE_BITSET:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003062 return futex_wake(uaddr, flags, val, val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 case FUTEX_REQUEUE:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003064 return futex_requeue(uaddr, flags, uaddr2, val, val2, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 case FUTEX_CMP_REQUEUE:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003066 return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 0);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07003067 case FUTEX_WAKE_OP:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003068 return futex_wake_op(uaddr, flags, uaddr2, val, val2, val3);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003069 case FUTEX_LOCK_PI:
Michael Kerrisk996636d2015-01-16 20:28:06 +01003070 return futex_lock_pi(uaddr, flags, timeout, 0);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003071 case FUTEX_UNLOCK_PI:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003072 return futex_unlock_pi(uaddr, flags);
Ingo Molnarc87e2832006-06-27 02:54:58 -07003073 case FUTEX_TRYLOCK_PI:
Michael Kerrisk996636d2015-01-16 20:28:06 +01003074 return futex_lock_pi(uaddr, flags, NULL, 1);
Darren Hart52400ba2009-04-03 13:40:49 -07003075 case FUTEX_WAIT_REQUEUE_PI:
3076 val3 = FUTEX_BITSET_MATCH_ANY;
Thomas Gleixner81b40532012-02-15 12:17:09 +01003077 return futex_wait_requeue_pi(uaddr, flags, val, timeout, val3,
3078 uaddr2);
Darren Hart52400ba2009-04-03 13:40:49 -07003079 case FUTEX_CMP_REQUEUE_PI:
Thomas Gleixner81b40532012-02-15 12:17:09 +01003080 return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 }
Thomas Gleixner81b40532012-02-15 12:17:09 +01003082 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083}
3084
3085
Heiko Carstens17da2bd2009-01-14 14:14:10 +01003086SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val,
3087 struct timespec __user *, utime, u32 __user *, uaddr2,
3088 u32, val3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089{
Pierre Peifferc19384b2007-05-09 02:35:02 -07003090 struct timespec ts;
3091 ktime_t t, *tp = NULL;
Ingo Molnare2970f22006-06-27 02:54:47 -07003092 u32 val2 = 0;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003093 int cmd = op & FUTEX_CMD_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094
Thomas Gleixnercd689982008-02-01 17:45:14 +01003095 if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI ||
Darren Hart52400ba2009-04-03 13:40:49 -07003096 cmd == FUTEX_WAIT_BITSET ||
3097 cmd == FUTEX_WAIT_REQUEUE_PI)) {
Davidlohr Buesoab51fba2015-06-29 23:26:02 -07003098 if (unlikely(should_fail_futex(!(op & FUTEX_PRIVATE_FLAG))))
3099 return -EFAULT;
Pierre Peifferc19384b2007-05-09 02:35:02 -07003100 if (copy_from_user(&ts, utime, sizeof(ts)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 return -EFAULT;
Pierre Peifferc19384b2007-05-09 02:35:02 -07003102 if (!timespec_valid(&ts))
Thomas Gleixner9741ef962006-03-31 02:31:32 -08003103 return -EINVAL;
Pierre Peifferc19384b2007-05-09 02:35:02 -07003104
3105 t = timespec_to_ktime(ts);
Eric Dumazet34f01cc2007-05-09 02:35:04 -07003106 if (cmd == FUTEX_WAIT)
Thomas Gleixner5a7780e2008-02-13 09:20:43 +01003107 t = ktime_add_safe(ktime_get(), t);
Pierre Peifferc19384b2007-05-09 02:35:02 -07003108 tp = &t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 }
3110 /*
Darren Hart52400ba2009-04-03 13:40:49 -07003111 * requeue parameter in 'utime' if cmd == FUTEX_*_REQUEUE_*.
Andreas Schwabf54f0982007-07-31 00:38:51 -07003112 * number of waiters to wake in 'utime' if cmd == FUTEX_WAKE_OP.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 */
Andreas Schwabf54f0982007-07-31 00:38:51 -07003114 if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE ||
Darren Hartba9c22f2009-04-20 22:22:22 -07003115 cmd == FUTEX_CMP_REQUEUE_PI || cmd == FUTEX_WAKE_OP)
Ingo Molnare2970f22006-06-27 02:54:47 -07003116 val2 = (u32) (unsigned long) utime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
Pierre Peifferc19384b2007-05-09 02:35:02 -07003118 return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119}
3120
Heiko Carstens03b8c7b2014-03-02 13:09:47 +01003121static void __init futex_detect_cmpxchg(void)
3122{
3123#ifndef CONFIG_HAVE_FUTEX_CMPXCHG
3124 u32 curval;
3125
3126 /*
3127 * This will fail and we want it. Some arch implementations do
3128 * runtime detection of the futex_atomic_cmpxchg_inatomic()
3129 * functionality. We want to know that before we call in any
3130 * of the complex code paths. Also we want to prevent
3131 * registration of robust lists in that case. NULL is
3132 * guaranteed to fault and we get -EFAULT on functional
3133 * implementation, the non-functional ones will return
3134 * -ENOSYS.
3135 */
3136 if (cmpxchg_futex_value_locked(&curval, NULL, 0, 0) == -EFAULT)
3137 futex_cmpxchg_enabled = 1;
3138#endif
3139}
3140
Benjamin Herrenschmidtf6d107f2008-03-27 14:52:15 +11003141static int __init futex_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142{
Heiko Carstens63b1a812014-01-16 14:54:50 +01003143 unsigned int futex_shift;
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -08003144 unsigned long i;
3145
3146#if CONFIG_BASE_SMALL
3147 futex_hashsize = 16;
3148#else
3149 futex_hashsize = roundup_pow_of_two(256 * num_possible_cpus());
3150#endif
3151
3152 futex_queues = alloc_large_system_hash("futex", sizeof(*futex_queues),
3153 futex_hashsize, 0,
3154 futex_hashsize < 256 ? HASH_SMALL : 0,
Heiko Carstens63b1a812014-01-16 14:54:50 +01003155 &futex_shift, NULL,
3156 futex_hashsize, futex_hashsize);
3157 futex_hashsize = 1UL << futex_shift;
Heiko Carstens03b8c7b2014-03-02 13:09:47 +01003158
3159 futex_detect_cmpxchg();
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08003160
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -08003161 for (i = 0; i < futex_hashsize; i++) {
Linus Torvalds11d46162014-03-20 22:11:17 -07003162 atomic_set(&futex_queues[i].waiters, 0);
Dima Zavin732375c2011-07-07 17:27:59 -07003163 plist_head_init(&futex_queues[i].chain);
Thomas Gleixner3e4ab742008-02-23 15:23:55 -08003164 spin_lock_init(&futex_queues[i].lock);
3165 }
3166
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 return 0;
3168}
Benjamin Herrenschmidtf6d107f2008-03-27 14:52:15 +11003169__initcall(futex_init);