blob: 5d17e3a83f8c39ad021c8ed4550593766bce6806 [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>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070067
Jakub Jelinek4732efb2005-09-06 15:16:25 -070068#include <asm/futex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Peter Zijlstra1696a8b2013-10-31 18:18:19 +010070#include "locking/rtmutex_common.h"
Ingo Molnarc87e2832006-06-27 02:54:58 -070071
Thomas Gleixner99b60ce2014-01-12 15:31:24 -080072/*
73 * Basic futex operation and ordering guarantees:
74 *
75 * The waiter reads the futex value in user space and calls
76 * futex_wait(). This function computes the hash bucket and acquires
77 * the hash bucket lock. After that it reads the futex user space value
Davidlohr Buesob0c29f72014-01-12 15:31:25 -080078 * again and verifies that the data has not changed. If it has not changed
79 * it enqueues itself into the hash bucket, releases the hash bucket lock
80 * and schedules.
Thomas Gleixner99b60ce2014-01-12 15:31:24 -080081 *
82 * The waker side modifies the user space value of the futex and calls
Davidlohr Buesob0c29f72014-01-12 15:31:25 -080083 * futex_wake(). This function computes the hash bucket and acquires the
84 * hash bucket lock. Then it looks for waiters on that futex in the hash
85 * bucket and wakes them.
Thomas Gleixner99b60ce2014-01-12 15:31:24 -080086 *
Davidlohr Buesob0c29f72014-01-12 15:31:25 -080087 * In futex wake up scenarios where no tasks are blocked on a futex, taking
88 * the hb spinlock can be avoided and simply return. In order for this
89 * optimization to work, ordering guarantees must exist so that the waiter
90 * being added to the list is acknowledged when the list is concurrently being
91 * checked by the waker, avoiding scenarios like the following:
Thomas Gleixner99b60ce2014-01-12 15:31:24 -080092 *
93 * CPU 0 CPU 1
94 * val = *futex;
95 * sys_futex(WAIT, futex, val);
96 * futex_wait(futex, val);
97 * uval = *futex;
98 * *futex = newval;
99 * sys_futex(WAKE, futex);
100 * futex_wake(futex);
101 * if (queue_empty())
102 * return;
103 * if (uval == val)
104 * lock(hash_bucket(futex));
105 * queue();
106 * unlock(hash_bucket(futex));
107 * schedule();
108 *
109 * This would cause the waiter on CPU 0 to wait forever because it
110 * missed the transition of the user space value from val to newval
111 * and the waker did not find the waiter in the hash bucket queue.
Thomas Gleixner99b60ce2014-01-12 15:31:24 -0800112 *
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800113 * The correct serialization ensures that a waiter either observes
114 * the changed user space value before blocking or is woken by a
115 * concurrent waker:
116 *
117 * CPU 0 CPU 1
Thomas Gleixner99b60ce2014-01-12 15:31:24 -0800118 * val = *futex;
119 * sys_futex(WAIT, futex, val);
120 * futex_wait(futex, val);
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800121 *
122 * waiters++;
123 * mb(); (A) <-- paired with -.
124 * |
125 * lock(hash_bucket(futex)); |
126 * |
127 * uval = *futex; |
128 * | *futex = newval;
129 * | sys_futex(WAKE, futex);
130 * | futex_wake(futex);
131 * |
132 * `-------> mb(); (B)
Thomas Gleixner99b60ce2014-01-12 15:31:24 -0800133 * if (uval == val)
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800134 * queue();
Thomas Gleixner99b60ce2014-01-12 15:31:24 -0800135 * unlock(hash_bucket(futex));
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800136 * schedule(); if (waiters)
137 * lock(hash_bucket(futex));
138 * wake_waiters(futex);
139 * unlock(hash_bucket(futex));
140 *
141 * Where (A) orders the waiters increment and the futex value read -- this
142 * is guaranteed by the head counter in the hb spinlock; and where (B)
143 * orders the write to futex and the waiters read -- this is done by the
144 * barriers in get_futex_key_refs(), through either ihold or atomic_inc,
145 * depending on the futex type.
146 *
147 * This yields the following case (where X:=waiters, Y:=futex):
148 *
149 * X = Y = 0
150 *
151 * w[X]=1 w[Y]=1
152 * MB MB
153 * r[Y]=y r[X]=x
154 *
155 * Which guarantees that x==0 && y==0 is impossible; which translates back into
156 * the guarantee that we cannot both miss the futex variable change and the
157 * enqueue.
Thomas Gleixner99b60ce2014-01-12 15:31:24 -0800158 */
159
Heiko Carstens03b8c7b2014-03-02 13:09:47 +0100160#ifndef CONFIG_HAVE_FUTEX_CMPXCHG
Thomas Gleixnera0c1e902008-02-23 15:23:57 -0800161int __read_mostly futex_cmpxchg_enabled;
Heiko Carstens03b8c7b2014-03-02 13:09:47 +0100162#endif
Thomas Gleixnera0c1e902008-02-23 15:23:57 -0800163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164/*
Darren Hartb41277d2010-11-08 13:10:09 -0800165 * Futex flags used to encode options to functions and preserve them across
166 * restarts.
167 */
168#define FLAGS_SHARED 0x01
169#define FLAGS_CLOCKRT 0x02
170#define FLAGS_HAS_TIMEOUT 0x04
171
172/*
Ingo Molnarc87e2832006-06-27 02:54:58 -0700173 * Priority Inheritance state:
174 */
175struct futex_pi_state {
176 /*
177 * list of 'owned' pi_state instances - these have to be
178 * cleaned up in do_exit() if the task exits prematurely:
179 */
180 struct list_head list;
181
182 /*
183 * The PI object:
184 */
185 struct rt_mutex pi_mutex;
186
187 struct task_struct *owner;
188 atomic_t refcount;
189
190 union futex_key key;
191};
192
Darren Hartd8d88fb2009-09-21 22:30:30 -0700193/**
194 * struct futex_q - The hashed futex queue entry, one per waiting task
Randy Dunlapfb62db22010-10-13 11:02:34 -0700195 * @list: priority-sorted list of tasks waiting on this futex
Darren Hartd8d88fb2009-09-21 22:30:30 -0700196 * @task: the task waiting on the futex
197 * @lock_ptr: the hash bucket lock
198 * @key: the key the futex is hashed on
199 * @pi_state: optional priority inheritance state
200 * @rt_waiter: rt_waiter storage for use with requeue_pi
201 * @requeue_pi_key: the requeue_pi target futex key
202 * @bitset: bitset for the optional bitmasked wakeup
203 *
204 * We use this hashed waitqueue, instead of a normal wait_queue_t, so
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 * we can wake only the relevant ones (hashed queues may be shared).
206 *
207 * A futex_q has a woken state, just like tasks have TASK_RUNNING.
Pierre Peifferec92d082007-05-09 02:35:00 -0700208 * It is considered woken when plist_node_empty(&q->list) || q->lock_ptr == 0.
Randy Dunlapfb62db22010-10-13 11:02:34 -0700209 * The order of wakeup is always to make the first condition true, then
Darren Hartd8d88fb2009-09-21 22:30:30 -0700210 * the second.
211 *
212 * PI futexes are typically woken before they are removed from the hash list via
213 * the rt_mutex code. See unqueue_me_pi().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 */
215struct futex_q {
Pierre Peifferec92d082007-05-09 02:35:00 -0700216 struct plist_node list;
Darren Hartd8d88fb2009-09-21 22:30:30 -0700217
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +0200218 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 spinlock_t *lock_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 union futex_key key;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700221 struct futex_pi_state *pi_state;
Darren Hart52400ba2009-04-03 13:40:49 -0700222 struct rt_mutex_waiter *rt_waiter;
Darren Hart84bc4af2009-08-13 17:36:53 -0700223 union futex_key *requeue_pi_key;
Thomas Gleixnercd689982008-02-01 17:45:14 +0100224 u32 bitset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225};
226
Darren Hart5bdb05f2010-11-08 13:40:28 -0800227static const struct futex_q futex_q_init = {
228 /* list gets initialized in queue_me()*/
229 .key = FUTEX_KEY_INIT,
230 .bitset = FUTEX_BITSET_MATCH_ANY
231};
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233/*
Darren Hartb2d09942009-03-12 00:55:37 -0700234 * Hash buckets are shared by all the futex_keys that hash to the same
235 * location. Each key may have multiple futex_q structures, one for each task
236 * waiting on a futex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 */
238struct futex_hash_bucket {
Pierre Peifferec92d082007-05-09 02:35:00 -0700239 spinlock_t lock;
240 struct plist_head chain;
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -0800241} ____cacheline_aligned_in_smp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -0800243static unsigned long __read_mostly futex_hashsize;
244
245static struct futex_hash_bucket *futex_queues;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800247static inline void futex_get_mm(union futex_key *key)
248{
249 atomic_inc(&key->private.mm->mm_count);
250 /*
251 * Ensure futex_get_mm() implies a full barrier such that
252 * get_futex_key() implies a full barrier. This is relied upon
253 * as full barrier (B), see the ordering comment above.
254 */
255 smp_mb__after_atomic_inc();
256}
257
258static inline bool hb_waiters_pending(struct futex_hash_bucket *hb)
259{
260#ifdef CONFIG_SMP
261 /*
262 * Tasks trying to enter the critical region are most likely
263 * potential waiters that will be added to the plist. Ensure
264 * that wakers won't miss to-be-slept tasks in the window between
265 * the wait call and the actual plist_add.
266 */
267 if (spin_is_locked(&hb->lock))
268 return true;
269 smp_rmb(); /* Make sure we check the lock state first */
270
271 return !plist_head_empty(&hb->chain);
272#else
273 return true;
274#endif
275}
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277/*
278 * We hash on the keys returned from get_futex_key (see below).
279 */
280static struct futex_hash_bucket *hash_futex(union futex_key *key)
281{
282 u32 hash = jhash2((u32*)&key->both.word,
283 (sizeof(key->both.word)+sizeof(key->both.ptr))/4,
284 key->both.offset);
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -0800285 return &futex_queues[hash & (futex_hashsize - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
288/*
289 * Return 1 if two futex_keys are equal, 0 otherwise.
290 */
291static inline int match_futex(union futex_key *key1, union futex_key *key2)
292{
Darren Hart2bc87202009-10-14 10:12:39 -0700293 return (key1 && key2
294 && key1->both.word == key2->both.word
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 && key1->both.ptr == key2->both.ptr
296 && key1->both.offset == key2->both.offset);
297}
298
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200299/*
300 * Take a reference to the resource addressed by a key.
301 * Can be called while holding spinlocks.
302 *
303 */
304static void get_futex_key_refs(union futex_key *key)
305{
306 if (!key->both.ptr)
307 return;
308
309 switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
310 case FUT_OFF_INODE:
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800311 ihold(key->shared.inode); /* implies MB (B) */
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200312 break;
313 case FUT_OFF_MMSHARED:
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800314 futex_get_mm(key); /* implies MB (B) */
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200315 break;
316 }
317}
318
319/*
320 * Drop a reference to the resource addressed by a key.
321 * The hash bucket spinlock must not be held.
322 */
323static void drop_futex_key_refs(union futex_key *key)
324{
Darren Hart90621c42008-12-29 19:43:21 -0800325 if (!key->both.ptr) {
326 /* If we're here then we tried to put a key we failed to get */
327 WARN_ON_ONCE(1);
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200328 return;
Darren Hart90621c42008-12-29 19:43:21 -0800329 }
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200330
331 switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
332 case FUT_OFF_INODE:
333 iput(key->shared.inode);
334 break;
335 case FUT_OFF_MMSHARED:
336 mmdrop(key->private.mm);
337 break;
338 }
339}
340
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700341/**
Darren Hartd96ee562009-09-21 22:30:22 -0700342 * get_futex_key() - Get parameters which are the keys for a futex
343 * @uaddr: virtual address of the futex
344 * @fshared: 0 for a PROCESS_PRIVATE futex, 1 for PROCESS_SHARED
345 * @key: address where result is stored.
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500346 * @rw: mapping needs to be read/write (values: VERIFY_READ,
347 * VERIFY_WRITE)
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700348 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -0800349 * Return: a negative error code or 0
350 *
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700351 * The key words are stored in *key on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 *
Al Viro6131ffa2013-02-27 16:59:05 -0500353 * For shared mappings, it's (page->index, file_inode(vma->vm_file),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 * offset_within_page). For private mappings, it's (uaddr, current->mm).
355 * We can usually work out the index without swapping in the page.
356 *
Darren Hartb2d09942009-03-12 00:55:37 -0700357 * lock_page() might sleep, the caller should not hold a spinlock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 */
Thomas Gleixner64d13042009-05-18 21:20:10 +0200359static int
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500360get_futex_key(u32 __user *uaddr, int fshared, union futex_key *key, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Ingo Molnare2970f22006-06-27 02:54:47 -0700362 unsigned long address = (unsigned long)uaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 struct mm_struct *mm = current->mm;
Andrea Arcangelia5b338f2011-01-13 15:46:34 -0800364 struct page *page, *page_head;
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500365 int err, ro = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 /*
368 * The futex address must be "naturally" aligned.
369 */
Ingo Molnare2970f22006-06-27 02:54:47 -0700370 key->both.offset = address % PAGE_SIZE;
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700371 if (unlikely((address % sizeof(u32)) != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return -EINVAL;
Ingo Molnare2970f22006-06-27 02:54:47 -0700373 address -= key->both.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Linus Torvalds5cdec2d2013-12-12 09:53:51 -0800375 if (unlikely(!access_ok(rw, uaddr, sizeof(u32))))
376 return -EFAULT;
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 /*
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700379 * PROCESS_PRIVATE futexes are fast.
380 * As the mm cannot disappear under us and the 'key' only needs
381 * virtual address, we dont even have to find the underlying vma.
382 * Note : We do have to check 'uaddr' is a valid user address,
383 * but access_ok() should be faster than find_vma()
384 */
385 if (!fshared) {
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700386 key->private.mm = mm;
387 key->private.address = address;
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800388 get_futex_key_refs(key); /* implies MB (B) */
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700389 return 0;
390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200392again:
KOSAKI Motohiro7485d0d2010-01-05 16:32:43 +0900393 err = get_user_pages_fast(address, 1, 1, &page);
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500394 /*
395 * If write access is not required (eg. FUTEX_WAIT), try
396 * and get read-only access.
397 */
398 if (err == -EFAULT && rw == VERIFY_READ) {
399 err = get_user_pages_fast(address, 1, 0, &page);
400 ro = 1;
401 }
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200402 if (err < 0)
403 return err;
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500404 else
405 err = 0;
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200406
Andrea Arcangelia5b338f2011-01-13 15:46:34 -0800407#ifdef CONFIG_TRANSPARENT_HUGEPAGE
408 page_head = page;
409 if (unlikely(PageTail(page))) {
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200410 put_page(page);
Andrea Arcangelia5b338f2011-01-13 15:46:34 -0800411 /* serialize against __split_huge_page_splitting() */
412 local_irq_disable();
Linus Torvaldsf12d5bf2013-12-12 09:38:42 -0800413 if (likely(__get_user_pages_fast(address, 1, !ro, &page) == 1)) {
Andrea Arcangelia5b338f2011-01-13 15:46:34 -0800414 page_head = compound_head(page);
415 /*
416 * page_head is valid pointer but we must pin
417 * it before taking the PG_lock and/or
418 * PG_compound_lock. The moment we re-enable
419 * irqs __split_huge_page_splitting() can
420 * return and the head page can be freed from
421 * under us. We can't take the PG_lock and/or
422 * PG_compound_lock on a page that could be
423 * freed from under us.
424 */
425 if (page != page_head) {
426 get_page(page_head);
427 put_page(page);
428 }
429 local_irq_enable();
430 } else {
431 local_irq_enable();
432 goto again;
433 }
434 }
435#else
436 page_head = compound_head(page);
437 if (page != page_head) {
438 get_page(page_head);
439 put_page(page);
440 }
441#endif
442
443 lock_page(page_head);
Hugh Dickinse6780f72011-12-31 11:44:01 -0800444
445 /*
446 * If page_head->mapping is NULL, then it cannot be a PageAnon
447 * page; but it might be the ZERO_PAGE or in the gate area or
448 * in a special mapping (all cases which we are happy to fail);
449 * or it may have been a good file page when get_user_pages_fast
450 * found it, but truncated or holepunched or subjected to
451 * invalidate_complete_page2 before we got the page lock (also
452 * cases which we are happy to fail). And we hold a reference,
453 * so refcount care in invalidate_complete_page's remove_mapping
454 * prevents drop_caches from setting mapping to NULL beneath us.
455 *
456 * The case we do have to guard against is when memory pressure made
457 * shmem_writepage move it from filecache to swapcache beneath us:
458 * an unlikely race, but we do need to retry for page_head->mapping.
459 */
Andrea Arcangelia5b338f2011-01-13 15:46:34 -0800460 if (!page_head->mapping) {
Hugh Dickinse6780f72011-12-31 11:44:01 -0800461 int shmem_swizzled = PageSwapCache(page_head);
Andrea Arcangelia5b338f2011-01-13 15:46:34 -0800462 unlock_page(page_head);
463 put_page(page_head);
Hugh Dickinse6780f72011-12-31 11:44:01 -0800464 if (shmem_swizzled)
465 goto again;
466 return -EFAULT;
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 /*
470 * Private mappings are handled in a simple way.
471 *
472 * NOTE: When userspace waits on a MAP_SHARED mapping, even if
473 * it's a read-only handle, it's expected that futexes attach to
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200474 * the object not the particular process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 */
Andrea Arcangelia5b338f2011-01-13 15:46:34 -0800476 if (PageAnon(page_head)) {
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500477 /*
478 * A RO anonymous page will never change and thus doesn't make
479 * sense for futex operations.
480 */
481 if (ro) {
482 err = -EFAULT;
483 goto out;
484 }
485
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200486 key->both.offset |= FUT_OFF_MMSHARED; /* ref taken on mm */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 key->private.mm = mm;
Ingo Molnare2970f22006-06-27 02:54:47 -0700488 key->private.address = address;
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200489 } else {
490 key->both.offset |= FUT_OFF_INODE; /* inode-based key */
Andrea Arcangelia5b338f2011-01-13 15:46:34 -0800491 key->shared.inode = page_head->mapping->host;
Zhang Yi13d60f42013-06-25 21:19:31 +0800492 key->shared.pgoff = basepage_index(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
494
Davidlohr Buesob0c29f72014-01-12 15:31:25 -0800495 get_futex_key_refs(key); /* implies MB (B) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500497out:
Andrea Arcangelia5b338f2011-01-13 15:46:34 -0800498 unlock_page(page_head);
499 put_page(page_head);
Shawn Bohrer9ea71502011-06-30 11:21:32 -0500500 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
Thomas Gleixnerae791a22010-11-10 13:30:36 +0100503static inline void put_futex_key(union futex_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200505 drop_futex_key_refs(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
Darren Hartd96ee562009-09-21 22:30:22 -0700508/**
509 * fault_in_user_writeable() - Fault in user address and verify RW access
Thomas Gleixnerd0725992009-06-11 23:15:43 +0200510 * @uaddr: pointer to faulting user space address
511 *
512 * Slow path to fixup the fault we just took in the atomic write
513 * access to @uaddr.
514 *
Randy Dunlapfb62db22010-10-13 11:02:34 -0700515 * We have no generic implementation of a non-destructive write to the
Thomas Gleixnerd0725992009-06-11 23:15:43 +0200516 * user address. We know that we faulted in the atomic pagefault
517 * disabled section so we can as well avoid the #PF overhead by
518 * calling get_user_pages() right away.
519 */
520static int fault_in_user_writeable(u32 __user *uaddr)
521{
Andi Kleen722d0172009-12-08 13:19:42 +0100522 struct mm_struct *mm = current->mm;
523 int ret;
524
525 down_read(&mm->mmap_sem);
Benjamin Herrenschmidt2efaca92011-07-25 17:12:32 -0700526 ret = fixup_user_fault(current, mm, (unsigned long)uaddr,
527 FAULT_FLAG_WRITE);
Andi Kleen722d0172009-12-08 13:19:42 +0100528 up_read(&mm->mmap_sem);
529
Thomas Gleixnerd0725992009-06-11 23:15:43 +0200530 return ret < 0 ? ret : 0;
531}
532
Darren Hart4b1c4862009-04-03 13:39:42 -0700533/**
534 * futex_top_waiter() - Return the highest priority waiter on a futex
Darren Hartd96ee562009-09-21 22:30:22 -0700535 * @hb: the hash bucket the futex_q's reside in
536 * @key: the futex key (to distinguish it from other futex futex_q's)
Darren Hart4b1c4862009-04-03 13:39:42 -0700537 *
538 * Must be called with the hb lock held.
539 */
540static struct futex_q *futex_top_waiter(struct futex_hash_bucket *hb,
541 union futex_key *key)
542{
543 struct futex_q *this;
544
545 plist_for_each_entry(this, &hb->chain, list) {
546 if (match_futex(&this->key, key))
547 return this;
548 }
549 return NULL;
550}
551
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800552static int cmpxchg_futex_value_locked(u32 *curval, u32 __user *uaddr,
553 u32 uval, u32 newval)
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700554{
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800555 int ret;
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700556
557 pagefault_disable();
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800558 ret = futex_atomic_cmpxchg_inatomic(curval, uaddr, uval, newval);
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700559 pagefault_enable();
560
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800561 return ret;
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700562}
563
564static int get_futex_value_locked(u32 *dest, u32 __user *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
566 int ret;
567
Peter Zijlstraa8663742006-12-06 20:32:20 -0800568 pagefault_disable();
Ingo Molnare2970f22006-06-27 02:54:47 -0700569 ret = __copy_from_user_inatomic(dest, from, sizeof(u32));
Peter Zijlstraa8663742006-12-06 20:32:20 -0800570 pagefault_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 return ret ? -EFAULT : 0;
573}
574
Ingo Molnarc87e2832006-06-27 02:54:58 -0700575
576/*
577 * PI code:
578 */
579static int refill_pi_state_cache(void)
580{
581 struct futex_pi_state *pi_state;
582
583 if (likely(current->pi_state_cache))
584 return 0;
585
Burman Yan4668edc2006-12-06 20:38:51 -0800586 pi_state = kzalloc(sizeof(*pi_state), GFP_KERNEL);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700587
588 if (!pi_state)
589 return -ENOMEM;
590
Ingo Molnarc87e2832006-06-27 02:54:58 -0700591 INIT_LIST_HEAD(&pi_state->list);
592 /* pi_mutex gets initialized later */
593 pi_state->owner = NULL;
594 atomic_set(&pi_state->refcount, 1);
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200595 pi_state->key = FUTEX_KEY_INIT;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700596
597 current->pi_state_cache = pi_state;
598
599 return 0;
600}
601
602static struct futex_pi_state * alloc_pi_state(void)
603{
604 struct futex_pi_state *pi_state = current->pi_state_cache;
605
606 WARN_ON(!pi_state);
607 current->pi_state_cache = NULL;
608
609 return pi_state;
610}
611
612static void free_pi_state(struct futex_pi_state *pi_state)
613{
614 if (!atomic_dec_and_test(&pi_state->refcount))
615 return;
616
617 /*
618 * If pi_state->owner is NULL, the owner is most probably dying
619 * and has cleaned up the pi_state already
620 */
621 if (pi_state->owner) {
Thomas Gleixner1d615482009-11-17 14:54:03 +0100622 raw_spin_lock_irq(&pi_state->owner->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700623 list_del_init(&pi_state->list);
Thomas Gleixner1d615482009-11-17 14:54:03 +0100624 raw_spin_unlock_irq(&pi_state->owner->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700625
626 rt_mutex_proxy_unlock(&pi_state->pi_mutex, pi_state->owner);
627 }
628
629 if (current->pi_state_cache)
630 kfree(pi_state);
631 else {
632 /*
633 * pi_state->list is already empty.
634 * clear pi_state->owner.
635 * refcount is at 0 - put it back to 1.
636 */
637 pi_state->owner = NULL;
638 atomic_set(&pi_state->refcount, 1);
639 current->pi_state_cache = pi_state;
640 }
641}
642
643/*
644 * Look up the task based on what TID userspace gave us.
645 * We dont trust it.
646 */
647static struct task_struct * futex_find_get_task(pid_t pid)
648{
649 struct task_struct *p;
650
Oleg Nesterovd359b542006-09-29 02:00:55 -0700651 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700652 p = find_task_by_vpid(pid);
Michal Hocko7a0ea092010-06-30 09:51:19 +0200653 if (p)
654 get_task_struct(p);
Thomas Gleixnera06381f2007-06-23 11:48:40 +0200655
Oleg Nesterovd359b542006-09-29 02:00:55 -0700656 rcu_read_unlock();
Ingo Molnarc87e2832006-06-27 02:54:58 -0700657
658 return p;
659}
660
661/*
662 * This task is holding PI mutexes at exit time => bad.
663 * Kernel cleans up PI-state, but userspace is likely hosed.
664 * (Robust-futex cleanup is separate and might save the day for userspace.)
665 */
666void exit_pi_state_list(struct task_struct *curr)
667{
Ingo Molnarc87e2832006-06-27 02:54:58 -0700668 struct list_head *next, *head = &curr->pi_state_list;
669 struct futex_pi_state *pi_state;
Ingo Molnar627371d2006-07-29 05:16:20 +0200670 struct futex_hash_bucket *hb;
Peter Zijlstra38d47c12008-09-26 19:32:20 +0200671 union futex_key key = FUTEX_KEY_INIT;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700672
Thomas Gleixnera0c1e902008-02-23 15:23:57 -0800673 if (!futex_cmpxchg_enabled)
674 return;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700675 /*
676 * We are a ZOMBIE and nobody can enqueue itself on
677 * pi_state_list anymore, but we have to be careful
Ingo Molnar627371d2006-07-29 05:16:20 +0200678 * versus waiters unqueueing themselves:
Ingo Molnarc87e2832006-06-27 02:54:58 -0700679 */
Thomas Gleixner1d615482009-11-17 14:54:03 +0100680 raw_spin_lock_irq(&curr->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700681 while (!list_empty(head)) {
682
683 next = head->next;
684 pi_state = list_entry(next, struct futex_pi_state, list);
685 key = pi_state->key;
Ingo Molnar627371d2006-07-29 05:16:20 +0200686 hb = hash_futex(&key);
Thomas Gleixner1d615482009-11-17 14:54:03 +0100687 raw_spin_unlock_irq(&curr->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700688
Ingo Molnarc87e2832006-06-27 02:54:58 -0700689 spin_lock(&hb->lock);
690
Thomas Gleixner1d615482009-11-17 14:54:03 +0100691 raw_spin_lock_irq(&curr->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +0200692 /*
693 * We dropped the pi-lock, so re-check whether this
694 * task still owns the PI-state:
695 */
Ingo Molnarc87e2832006-06-27 02:54:58 -0700696 if (head->next != next) {
697 spin_unlock(&hb->lock);
698 continue;
699 }
700
Ingo Molnarc87e2832006-06-27 02:54:58 -0700701 WARN_ON(pi_state->owner != curr);
Ingo Molnar627371d2006-07-29 05:16:20 +0200702 WARN_ON(list_empty(&pi_state->list));
703 list_del_init(&pi_state->list);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700704 pi_state->owner = NULL;
Thomas Gleixner1d615482009-11-17 14:54:03 +0100705 raw_spin_unlock_irq(&curr->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700706
707 rt_mutex_unlock(&pi_state->pi_mutex);
708
709 spin_unlock(&hb->lock);
710
Thomas Gleixner1d615482009-11-17 14:54:03 +0100711 raw_spin_lock_irq(&curr->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700712 }
Thomas Gleixner1d615482009-11-17 14:54:03 +0100713 raw_spin_unlock_irq(&curr->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700714}
715
716static int
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700717lookup_pi_state(u32 uval, struct futex_hash_bucket *hb,
718 union futex_key *key, struct futex_pi_state **ps)
Ingo Molnarc87e2832006-06-27 02:54:58 -0700719{
720 struct futex_pi_state *pi_state = NULL;
721 struct futex_q *this, *next;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700722 struct task_struct *p;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700723 pid_t pid = uval & FUTEX_TID_MASK;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700724
Jason Low0d00c7b2014-01-12 15:31:22 -0800725 plist_for_each_entry_safe(this, next, &hb->chain, list) {
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700726 if (match_futex(&this->key, key)) {
Ingo Molnarc87e2832006-06-27 02:54:58 -0700727 /*
728 * Another waiter already exists - bump up
729 * the refcount and return its pi_state:
730 */
731 pi_state = this->pi_state;
Thomas Gleixner06a9ec22006-07-10 04:44:30 -0700732 /*
Randy Dunlapfb62db22010-10-13 11:02:34 -0700733 * Userspace might have messed up non-PI and PI futexes
Thomas Gleixner06a9ec22006-07-10 04:44:30 -0700734 */
735 if (unlikely(!pi_state))
736 return -EINVAL;
737
Ingo Molnar627371d2006-07-29 05:16:20 +0200738 WARN_ON(!atomic_read(&pi_state->refcount));
Thomas Gleixner59647b62010-02-03 09:33:05 +0100739
740 /*
741 * When pi_state->owner is NULL then the owner died
742 * and another waiter is on the fly. pi_state->owner
743 * is fixed up by the task which acquires
744 * pi_state->rt_mutex.
745 *
746 * We do not check for pid == 0 which can happen when
747 * the owner died and robust_list_exit() cleared the
748 * TID.
749 */
750 if (pid && pi_state->owner) {
751 /*
752 * Bail out if user space manipulated the
753 * futex value.
754 */
755 if (pid != task_pid_vnr(pi_state->owner))
756 return -EINVAL;
757 }
Ingo Molnar627371d2006-07-29 05:16:20 +0200758
Ingo Molnarc87e2832006-06-27 02:54:58 -0700759 atomic_inc(&pi_state->refcount);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700760 *ps = pi_state;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700761
762 return 0;
763 }
764 }
765
766 /*
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200767 * We are the first waiter - try to look up the real owner and attach
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700768 * the new pi_state to it, but bail out when TID = 0
Ingo Molnarc87e2832006-06-27 02:54:58 -0700769 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700770 if (!pid)
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200771 return -ESRCH;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700772 p = futex_find_get_task(pid);
Michal Hocko7a0ea092010-06-30 09:51:19 +0200773 if (!p)
774 return -ESRCH;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700775
776 /*
777 * We need to look at the task state flags to figure out,
778 * whether the task is exiting. To protect against the do_exit
779 * change of the task flags, we do this protected by
780 * p->pi_lock:
781 */
Thomas Gleixner1d615482009-11-17 14:54:03 +0100782 raw_spin_lock_irq(&p->pi_lock);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700783 if (unlikely(p->flags & PF_EXITING)) {
784 /*
785 * The task is on the way out. When PF_EXITPIDONE is
786 * set, we know that the task has finished the
787 * cleanup:
788 */
789 int ret = (p->flags & PF_EXITPIDONE) ? -ESRCH : -EAGAIN;
790
Thomas Gleixner1d615482009-11-17 14:54:03 +0100791 raw_spin_unlock_irq(&p->pi_lock);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700792 put_task_struct(p);
793 return ret;
794 }
Ingo Molnarc87e2832006-06-27 02:54:58 -0700795
796 pi_state = alloc_pi_state();
797
798 /*
799 * Initialize the pi_mutex in locked state and make 'p'
800 * the owner of it:
801 */
802 rt_mutex_init_proxy_locked(&pi_state->pi_mutex, p);
803
804 /* Store the key for possible exit cleanups: */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700805 pi_state->key = *key;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700806
Ingo Molnar627371d2006-07-29 05:16:20 +0200807 WARN_ON(!list_empty(&pi_state->list));
Ingo Molnarc87e2832006-06-27 02:54:58 -0700808 list_add(&pi_state->list, &p->pi_state_list);
809 pi_state->owner = p;
Thomas Gleixner1d615482009-11-17 14:54:03 +0100810 raw_spin_unlock_irq(&p->pi_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700811
812 put_task_struct(p);
813
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700814 *ps = pi_state;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700815
816 return 0;
817}
818
Darren Hart1a520842009-04-03 13:39:52 -0700819/**
Darren Hartd96ee562009-09-21 22:30:22 -0700820 * futex_lock_pi_atomic() - Atomic work required to acquire a pi aware futex
Darren Hartbab5bc92009-04-07 23:23:50 -0700821 * @uaddr: the pi futex user address
822 * @hb: the pi futex hash bucket
823 * @key: the futex key associated with uaddr and hb
824 * @ps: the pi_state pointer where we store the result of the
825 * lookup
826 * @task: the task to perform the atomic lock work for. This will
827 * be "current" except in the case of requeue pi.
828 * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0)
Darren Hart1a520842009-04-03 13:39:52 -0700829 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -0800830 * Return:
831 * 0 - ready to wait;
832 * 1 - acquired the lock;
Darren Hart1a520842009-04-03 13:39:52 -0700833 * <0 - error
834 *
835 * The hb->lock and futex_key refs shall be held by the caller.
836 */
837static int futex_lock_pi_atomic(u32 __user *uaddr, struct futex_hash_bucket *hb,
838 union futex_key *key,
839 struct futex_pi_state **ps,
Darren Hartbab5bc92009-04-07 23:23:50 -0700840 struct task_struct *task, int set_waiters)
Darren Hart1a520842009-04-03 13:39:52 -0700841{
Thomas Gleixner59fa6242012-10-23 22:29:38 +0200842 int lock_taken, ret, force_take = 0;
Thomas Gleixnerc0c9ed12011-03-11 11:51:22 +0100843 u32 uval, newval, curval, vpid = task_pid_vnr(task);
Darren Hart1a520842009-04-03 13:39:52 -0700844
845retry:
846 ret = lock_taken = 0;
847
848 /*
849 * To avoid races, we attempt to take the lock here again
850 * (by doing a 0 -> TID atomic cmpxchg), while holding all
851 * the locks. It will most likely not succeed.
852 */
Thomas Gleixnerc0c9ed12011-03-11 11:51:22 +0100853 newval = vpid;
Darren Hartbab5bc92009-04-07 23:23:50 -0700854 if (set_waiters)
855 newval |= FUTEX_WAITERS;
Darren Hart1a520842009-04-03 13:39:52 -0700856
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800857 if (unlikely(cmpxchg_futex_value_locked(&curval, uaddr, 0, newval)))
Darren Hart1a520842009-04-03 13:39:52 -0700858 return -EFAULT;
859
860 /*
861 * Detect deadlocks.
862 */
Thomas Gleixnerc0c9ed12011-03-11 11:51:22 +0100863 if ((unlikely((curval & FUTEX_TID_MASK) == vpid)))
Darren Hart1a520842009-04-03 13:39:52 -0700864 return -EDEADLK;
865
866 /*
867 * Surprise - we got the lock. Just return to userspace:
868 */
869 if (unlikely(!curval))
870 return 1;
871
872 uval = curval;
873
874 /*
875 * Set the FUTEX_WAITERS flag, so the owner will know it has someone
876 * to wake at the next unlock.
877 */
878 newval = curval | FUTEX_WAITERS;
879
880 /*
Thomas Gleixner59fa6242012-10-23 22:29:38 +0200881 * Should we force take the futex? See below.
Darren Hart1a520842009-04-03 13:39:52 -0700882 */
Thomas Gleixner59fa6242012-10-23 22:29:38 +0200883 if (unlikely(force_take)) {
884 /*
885 * Keep the OWNER_DIED and the WAITERS bit and set the
886 * new TID value.
887 */
Thomas Gleixnerc0c9ed12011-03-11 11:51:22 +0100888 newval = (curval & ~FUTEX_TID_MASK) | vpid;
Thomas Gleixner59fa6242012-10-23 22:29:38 +0200889 force_take = 0;
Darren Hart1a520842009-04-03 13:39:52 -0700890 lock_taken = 1;
891 }
892
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800893 if (unlikely(cmpxchg_futex_value_locked(&curval, uaddr, uval, newval)))
Darren Hart1a520842009-04-03 13:39:52 -0700894 return -EFAULT;
895 if (unlikely(curval != uval))
896 goto retry;
897
898 /*
Thomas Gleixner59fa6242012-10-23 22:29:38 +0200899 * We took the lock due to forced take over.
Darren Hart1a520842009-04-03 13:39:52 -0700900 */
901 if (unlikely(lock_taken))
902 return 1;
903
904 /*
905 * We dont have the lock. Look up the PI state (or create it if
906 * we are the first waiter):
907 */
908 ret = lookup_pi_state(uval, hb, key, ps);
909
910 if (unlikely(ret)) {
911 switch (ret) {
912 case -ESRCH:
913 /*
Thomas Gleixner59fa6242012-10-23 22:29:38 +0200914 * We failed to find an owner for this
915 * futex. So we have no pi_state to block
916 * on. This can happen in two cases:
917 *
918 * 1) The owner died
919 * 2) A stale FUTEX_WAITERS bit
920 *
921 * Re-read the futex value.
Darren Hart1a520842009-04-03 13:39:52 -0700922 */
923 if (get_futex_value_locked(&curval, uaddr))
924 return -EFAULT;
925
926 /*
Thomas Gleixner59fa6242012-10-23 22:29:38 +0200927 * If the owner died or we have a stale
928 * WAITERS bit the owner TID in the user space
929 * futex is 0.
Darren Hart1a520842009-04-03 13:39:52 -0700930 */
Thomas Gleixner59fa6242012-10-23 22:29:38 +0200931 if (!(curval & FUTEX_TID_MASK)) {
932 force_take = 1;
Darren Hart1a520842009-04-03 13:39:52 -0700933 goto retry;
934 }
935 default:
936 break;
937 }
938 }
939
940 return ret;
941}
942
Lai Jiangshan2e129782010-12-22 14:18:50 +0800943/**
944 * __unqueue_futex() - Remove the futex_q from its futex_hash_bucket
945 * @q: The futex_q to unqueue
946 *
947 * The q->lock_ptr must not be NULL and must be held by the caller.
948 */
949static void __unqueue_futex(struct futex_q *q)
950{
951 struct futex_hash_bucket *hb;
952
Steven Rostedt29096202011-03-17 15:21:07 -0400953 if (WARN_ON_SMP(!q->lock_ptr || !spin_is_locked(q->lock_ptr))
954 || WARN_ON(plist_node_empty(&q->list)))
Lai Jiangshan2e129782010-12-22 14:18:50 +0800955 return;
956
957 hb = container_of(q->lock_ptr, struct futex_hash_bucket, lock);
958 plist_del(&q->list, &hb->chain);
959}
960
Ingo Molnarc87e2832006-06-27 02:54:58 -0700961/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 * The hash bucket lock must be held when this is called.
963 * Afterwards, the futex_q must not be accessed.
964 */
965static void wake_futex(struct futex_q *q)
966{
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +0200967 struct task_struct *p = q->task;
968
Darren Hartaa109902012-11-26 16:29:56 -0800969 if (WARN(q->pi_state || q->rt_waiter, "refusing to wake PI futex\n"))
970 return;
971
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +0200972 /*
973 * We set q->lock_ptr = NULL _before_ we wake up the task. If
Randy Dunlapfb62db22010-10-13 11:02:34 -0700974 * a non-futex wake up happens on another CPU then the task
975 * might exit and p would dereference a non-existing task
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +0200976 * struct. Prevent this by holding a reference on p across the
977 * wake up.
978 */
979 get_task_struct(p);
980
Lai Jiangshan2e129782010-12-22 14:18:50 +0800981 __unqueue_futex(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 /*
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +0200983 * The waiting task can free the futex_q as soon as
984 * q->lock_ptr = NULL is written, without taking any locks. A
985 * memory barrier is required here to prevent the following
986 * store to lock_ptr from getting ahead of the plist_del.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 */
Ralf Baechleccdea2f2006-12-06 20:40:26 -0800988 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 q->lock_ptr = NULL;
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +0200990
991 wake_up_state(p, TASK_NORMAL);
992 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
994
Ingo Molnarc87e2832006-06-27 02:54:58 -0700995static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this)
996{
997 struct task_struct *new_owner;
998 struct futex_pi_state *pi_state = this->pi_state;
Vitaliy Ivanov7cfdaf32011-07-07 15:10:31 +0300999 u32 uninitialized_var(curval), newval;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001000
1001 if (!pi_state)
1002 return -EINVAL;
1003
Thomas Gleixner51246bf2010-02-02 11:40:27 +01001004 /*
1005 * If current does not own the pi_state then the futex is
1006 * inconsistent and user space fiddled with the futex value.
1007 */
1008 if (pi_state->owner != current)
1009 return -EINVAL;
1010
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001011 raw_spin_lock(&pi_state->pi_mutex.wait_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001012 new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);
1013
1014 /*
Steven Rostedtf123c982011-01-06 15:08:29 -05001015 * It is possible that the next waiter (the one that brought
1016 * this owner to the kernel) timed out and is no longer
1017 * waiting on the lock.
Ingo Molnarc87e2832006-06-27 02:54:58 -07001018 */
1019 if (!new_owner)
1020 new_owner = this->task;
1021
1022 /*
1023 * We pass it to the next owner. (The WAITERS bit is always
1024 * kept enabled while there is PI state around. We must also
1025 * preserve the owner died bit.)
1026 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001027 if (!(uval & FUTEX_OWNER_DIED)) {
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001028 int ret = 0;
1029
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001030 newval = FUTEX_WAITERS | task_pid_vnr(new_owner);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001031
Michel Lespinasse37a9d912011-03-10 18:48:51 -08001032 if (cmpxchg_futex_value_locked(&curval, uaddr, uval, newval))
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001033 ret = -EFAULT;
Thomas Gleixnercde898f2007-12-05 15:46:09 +01001034 else if (curval != uval)
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001035 ret = -EINVAL;
1036 if (ret) {
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001037 raw_spin_unlock(&pi_state->pi_mutex.wait_lock);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001038 return ret;
1039 }
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001040 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001041
Thomas Gleixner1d615482009-11-17 14:54:03 +01001042 raw_spin_lock_irq(&pi_state->owner->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +02001043 WARN_ON(list_empty(&pi_state->list));
1044 list_del_init(&pi_state->list);
Thomas Gleixner1d615482009-11-17 14:54:03 +01001045 raw_spin_unlock_irq(&pi_state->owner->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +02001046
Thomas Gleixner1d615482009-11-17 14:54:03 +01001047 raw_spin_lock_irq(&new_owner->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +02001048 WARN_ON(!list_empty(&pi_state->list));
Ingo Molnarc87e2832006-06-27 02:54:58 -07001049 list_add(&pi_state->list, &new_owner->pi_state_list);
1050 pi_state->owner = new_owner;
Thomas Gleixner1d615482009-11-17 14:54:03 +01001051 raw_spin_unlock_irq(&new_owner->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +02001052
Thomas Gleixnerd209d742009-11-17 18:22:11 +01001053 raw_spin_unlock(&pi_state->pi_mutex.wait_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001054 rt_mutex_unlock(&pi_state->pi_mutex);
1055
1056 return 0;
1057}
1058
1059static int unlock_futex_pi(u32 __user *uaddr, u32 uval)
1060{
Vitaliy Ivanov7cfdaf32011-07-07 15:10:31 +03001061 u32 uninitialized_var(oldval);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001062
1063 /*
1064 * There is no waiter, so we unlock the futex. The owner died
1065 * bit has not to be preserved here. We are the owner:
1066 */
Michel Lespinasse37a9d912011-03-10 18:48:51 -08001067 if (cmpxchg_futex_value_locked(&oldval, uaddr, uval, 0))
1068 return -EFAULT;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001069 if (oldval != uval)
1070 return -EAGAIN;
1071
1072 return 0;
1073}
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075/*
Ingo Molnar8b8f3192006-07-03 00:25:05 -07001076 * Express the locking dependencies for lockdep:
1077 */
1078static inline void
1079double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
1080{
1081 if (hb1 <= hb2) {
1082 spin_lock(&hb1->lock);
1083 if (hb1 < hb2)
1084 spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING);
1085 } else { /* hb1 > hb2 */
1086 spin_lock(&hb2->lock);
1087 spin_lock_nested(&hb1->lock, SINGLE_DEPTH_NESTING);
1088 }
1089}
1090
Darren Hart5eb3dc62009-03-12 00:55:52 -07001091static inline void
1092double_unlock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
1093{
Darren Hartf061d352009-03-12 15:11:18 -07001094 spin_unlock(&hb1->lock);
Ingo Molnar88f502f2009-03-13 10:32:07 +01001095 if (hb1 != hb2)
1096 spin_unlock(&hb2->lock);
Darren Hart5eb3dc62009-03-12 00:55:52 -07001097}
1098
Ingo Molnar8b8f3192006-07-03 00:25:05 -07001099/*
Darren Hartb2d09942009-03-12 00:55:37 -07001100 * Wake up waiters matching bitset queued on this futex (uaddr).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 */
Darren Hartb41277d2010-11-08 13:10:09 -08001102static int
1103futex_wake(u32 __user *uaddr, unsigned int flags, int nr_wake, u32 bitset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
Ingo Molnare2970f22006-06-27 02:54:47 -07001105 struct futex_hash_bucket *hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 struct futex_q *this, *next;
Peter Zijlstra38d47c12008-09-26 19:32:20 +02001107 union futex_key key = FUTEX_KEY_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 int ret;
1109
Thomas Gleixnercd689982008-02-01 17:45:14 +01001110 if (!bitset)
1111 return -EINVAL;
1112
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001113 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (unlikely(ret != 0))
1115 goto out;
1116
Ingo Molnare2970f22006-06-27 02:54:47 -07001117 hb = hash_futex(&key);
Davidlohr Buesob0c29f72014-01-12 15:31:25 -08001118
1119 /* Make sure we really have tasks to wakeup */
1120 if (!hb_waiters_pending(hb))
1121 goto out_put_key;
1122
Ingo Molnare2970f22006-06-27 02:54:47 -07001123 spin_lock(&hb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Jason Low0d00c7b2014-01-12 15:31:22 -08001125 plist_for_each_entry_safe(this, next, &hb->chain, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (match_futex (&this->key, &key)) {
Darren Hart52400ba2009-04-03 13:40:49 -07001127 if (this->pi_state || this->rt_waiter) {
Ingo Molnared6f7b12006-07-01 04:35:46 -07001128 ret = -EINVAL;
1129 break;
1130 }
Thomas Gleixnercd689982008-02-01 17:45:14 +01001131
1132 /* Check if one of the bits is set in both bitsets */
1133 if (!(this->bitset & bitset))
1134 continue;
1135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 wake_futex(this);
1137 if (++ret >= nr_wake)
1138 break;
1139 }
1140 }
1141
Ingo Molnare2970f22006-06-27 02:54:47 -07001142 spin_unlock(&hb->lock);
Davidlohr Buesob0c29f72014-01-12 15:31:25 -08001143out_put_key:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001144 put_futex_key(&key);
Darren Hart42d35d42008-12-29 15:49:53 -08001145out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 return ret;
1147}
1148
1149/*
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001150 * Wake up all waiters hashed on the physical page that is mapped
1151 * to this virtual address:
1152 */
Ingo Molnare2970f22006-06-27 02:54:47 -07001153static int
Darren Hartb41277d2010-11-08 13:10:09 -08001154futex_wake_op(u32 __user *uaddr1, unsigned int flags, u32 __user *uaddr2,
Ingo Molnare2970f22006-06-27 02:54:47 -07001155 int nr_wake, int nr_wake2, int op)
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001156{
Peter Zijlstra38d47c12008-09-26 19:32:20 +02001157 union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
Ingo Molnare2970f22006-06-27 02:54:47 -07001158 struct futex_hash_bucket *hb1, *hb2;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001159 struct futex_q *this, *next;
Darren Harte4dc5b72009-03-12 00:56:13 -07001160 int ret, op_ret;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001161
Darren Harte4dc5b72009-03-12 00:56:13 -07001162retry:
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001163 ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, VERIFY_READ);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001164 if (unlikely(ret != 0))
1165 goto out;
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001166 ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001167 if (unlikely(ret != 0))
Darren Hart42d35d42008-12-29 15:49:53 -08001168 goto out_put_key1;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001169
Ingo Molnare2970f22006-06-27 02:54:47 -07001170 hb1 = hash_futex(&key1);
1171 hb2 = hash_futex(&key2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001172
Darren Harte4dc5b72009-03-12 00:56:13 -07001173retry_private:
Thomas Gleixnereaaea802009-10-04 09:34:17 +02001174 double_lock_hb(hb1, hb2);
Ingo Molnare2970f22006-06-27 02:54:47 -07001175 op_ret = futex_atomic_op_inuser(op, uaddr2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001176 if (unlikely(op_ret < 0)) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001177
Darren Hart5eb3dc62009-03-12 00:55:52 -07001178 double_unlock_hb(hb1, hb2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001179
David Howells7ee1dd32006-01-06 00:11:44 -08001180#ifndef CONFIG_MMU
Ingo Molnare2970f22006-06-27 02:54:47 -07001181 /*
1182 * we don't get EFAULT from MMU faults if we don't have an MMU,
1183 * but we might get them from range checking
1184 */
David Howells7ee1dd32006-01-06 00:11:44 -08001185 ret = op_ret;
Darren Hart42d35d42008-12-29 15:49:53 -08001186 goto out_put_keys;
David Howells7ee1dd32006-01-06 00:11:44 -08001187#endif
1188
David Gibson796f8d92005-11-07 00:59:33 -08001189 if (unlikely(op_ret != -EFAULT)) {
1190 ret = op_ret;
Darren Hart42d35d42008-12-29 15:49:53 -08001191 goto out_put_keys;
David Gibson796f8d92005-11-07 00:59:33 -08001192 }
1193
Thomas Gleixnerd0725992009-06-11 23:15:43 +02001194 ret = fault_in_user_writeable(uaddr2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001195 if (ret)
Darren Hartde87fcc2009-03-12 00:55:46 -07001196 goto out_put_keys;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001197
Darren Hartb41277d2010-11-08 13:10:09 -08001198 if (!(flags & FLAGS_SHARED))
Darren Harte4dc5b72009-03-12 00:56:13 -07001199 goto retry_private;
1200
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001201 put_futex_key(&key2);
1202 put_futex_key(&key1);
Darren Harte4dc5b72009-03-12 00:56:13 -07001203 goto retry;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001204 }
1205
Jason Low0d00c7b2014-01-12 15:31:22 -08001206 plist_for_each_entry_safe(this, next, &hb1->chain, list) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001207 if (match_futex (&this->key, &key1)) {
Darren Hartaa109902012-11-26 16:29:56 -08001208 if (this->pi_state || this->rt_waiter) {
1209 ret = -EINVAL;
1210 goto out_unlock;
1211 }
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001212 wake_futex(this);
1213 if (++ret >= nr_wake)
1214 break;
1215 }
1216 }
1217
1218 if (op_ret > 0) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001219 op_ret = 0;
Jason Low0d00c7b2014-01-12 15:31:22 -08001220 plist_for_each_entry_safe(this, next, &hb2->chain, list) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001221 if (match_futex (&this->key, &key2)) {
Darren Hartaa109902012-11-26 16:29:56 -08001222 if (this->pi_state || this->rt_waiter) {
1223 ret = -EINVAL;
1224 goto out_unlock;
1225 }
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001226 wake_futex(this);
1227 if (++op_ret >= nr_wake2)
1228 break;
1229 }
1230 }
1231 ret += op_ret;
1232 }
1233
Darren Hartaa109902012-11-26 16:29:56 -08001234out_unlock:
Darren Hart5eb3dc62009-03-12 00:55:52 -07001235 double_unlock_hb(hb1, hb2);
Darren Hart42d35d42008-12-29 15:49:53 -08001236out_put_keys:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001237 put_futex_key(&key2);
Darren Hart42d35d42008-12-29 15:49:53 -08001238out_put_key1:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001239 put_futex_key(&key1);
Darren Hart42d35d42008-12-29 15:49:53 -08001240out:
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001241 return ret;
1242}
1243
Darren Hart9121e472009-04-03 13:40:31 -07001244/**
1245 * requeue_futex() - Requeue a futex_q from one hb to another
1246 * @q: the futex_q to requeue
1247 * @hb1: the source hash_bucket
1248 * @hb2: the target hash_bucket
1249 * @key2: the new key for the requeued futex_q
1250 */
1251static inline
1252void requeue_futex(struct futex_q *q, struct futex_hash_bucket *hb1,
1253 struct futex_hash_bucket *hb2, union futex_key *key2)
1254{
1255
1256 /*
1257 * If key1 and key2 hash to the same bucket, no need to
1258 * requeue.
1259 */
1260 if (likely(&hb1->chain != &hb2->chain)) {
1261 plist_del(&q->list, &hb1->chain);
1262 plist_add(&q->list, &hb2->chain);
1263 q->lock_ptr = &hb2->lock;
Darren Hart9121e472009-04-03 13:40:31 -07001264 }
1265 get_futex_key_refs(key2);
1266 q->key = *key2;
1267}
1268
Darren Hart52400ba2009-04-03 13:40:49 -07001269/**
1270 * requeue_pi_wake_futex() - Wake a task that acquired the lock during requeue
Darren Hartd96ee562009-09-21 22:30:22 -07001271 * @q: the futex_q
1272 * @key: the key of the requeue target futex
1273 * @hb: the hash_bucket of the requeue target futex
Darren Hart52400ba2009-04-03 13:40:49 -07001274 *
1275 * During futex_requeue, with requeue_pi=1, it is possible to acquire the
1276 * target futex if it is uncontended or via a lock steal. Set the futex_q key
1277 * to the requeue target futex so the waiter can detect the wakeup on the right
1278 * futex, but remove it from the hb and NULL the rt_waiter so it can detect
Darren Hartbeda2c72009-08-09 15:34:39 -07001279 * atomic lock acquisition. Set the q->lock_ptr to the requeue target hb->lock
1280 * to protect access to the pi_state to fixup the owner later. Must be called
1281 * with both q->lock_ptr and hb->lock held.
Darren Hart52400ba2009-04-03 13:40:49 -07001282 */
1283static inline
Darren Hartbeda2c72009-08-09 15:34:39 -07001284void requeue_pi_wake_futex(struct futex_q *q, union futex_key *key,
1285 struct futex_hash_bucket *hb)
Darren Hart52400ba2009-04-03 13:40:49 -07001286{
Darren Hart52400ba2009-04-03 13:40:49 -07001287 get_futex_key_refs(key);
1288 q->key = *key;
1289
Lai Jiangshan2e129782010-12-22 14:18:50 +08001290 __unqueue_futex(q);
Darren Hart52400ba2009-04-03 13:40:49 -07001291
1292 WARN_ON(!q->rt_waiter);
1293 q->rt_waiter = NULL;
1294
Darren Hartbeda2c72009-08-09 15:34:39 -07001295 q->lock_ptr = &hb->lock;
Darren Hartbeda2c72009-08-09 15:34:39 -07001296
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001297 wake_up_state(q->task, TASK_NORMAL);
Darren Hart52400ba2009-04-03 13:40:49 -07001298}
1299
1300/**
1301 * futex_proxy_trylock_atomic() - Attempt an atomic lock for the top waiter
Darren Hartbab5bc92009-04-07 23:23:50 -07001302 * @pifutex: the user address of the to futex
1303 * @hb1: the from futex hash bucket, must be locked by the caller
1304 * @hb2: the to futex hash bucket, must be locked by the caller
1305 * @key1: the from futex key
1306 * @key2: the to futex key
1307 * @ps: address to store the pi_state pointer
1308 * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0)
Darren Hart52400ba2009-04-03 13:40:49 -07001309 *
1310 * Try and get the lock on behalf of the top waiter if we can do it atomically.
Darren Hartbab5bc92009-04-07 23:23:50 -07001311 * Wake the top waiter if we succeed. If the caller specified set_waiters,
1312 * then direct futex_lock_pi_atomic() to force setting the FUTEX_WAITERS bit.
1313 * hb1 and hb2 must be held by the caller.
Darren Hart52400ba2009-04-03 13:40:49 -07001314 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08001315 * Return:
1316 * 0 - failed to acquire the lock atomically;
1317 * 1 - acquired the lock;
Darren Hart52400ba2009-04-03 13:40:49 -07001318 * <0 - error
1319 */
1320static int futex_proxy_trylock_atomic(u32 __user *pifutex,
1321 struct futex_hash_bucket *hb1,
1322 struct futex_hash_bucket *hb2,
1323 union futex_key *key1, union futex_key *key2,
Darren Hartbab5bc92009-04-07 23:23:50 -07001324 struct futex_pi_state **ps, int set_waiters)
Darren Hart52400ba2009-04-03 13:40:49 -07001325{
Darren Hartbab5bc92009-04-07 23:23:50 -07001326 struct futex_q *top_waiter = NULL;
Darren Hart52400ba2009-04-03 13:40:49 -07001327 u32 curval;
1328 int ret;
1329
1330 if (get_futex_value_locked(&curval, pifutex))
1331 return -EFAULT;
1332
Darren Hartbab5bc92009-04-07 23:23:50 -07001333 /*
1334 * Find the top_waiter and determine if there are additional waiters.
1335 * If the caller intends to requeue more than 1 waiter to pifutex,
1336 * force futex_lock_pi_atomic() to set the FUTEX_WAITERS bit now,
1337 * as we have means to handle the possible fault. If not, don't set
1338 * the bit unecessarily as it will force the subsequent unlock to enter
1339 * the kernel.
1340 */
Darren Hart52400ba2009-04-03 13:40:49 -07001341 top_waiter = futex_top_waiter(hb1, key1);
1342
1343 /* There are no waiters, nothing for us to do. */
1344 if (!top_waiter)
1345 return 0;
1346
Darren Hart84bc4af2009-08-13 17:36:53 -07001347 /* Ensure we requeue to the expected futex. */
1348 if (!match_futex(top_waiter->requeue_pi_key, key2))
1349 return -EINVAL;
1350
Darren Hart52400ba2009-04-03 13:40:49 -07001351 /*
Darren Hartbab5bc92009-04-07 23:23:50 -07001352 * Try to take the lock for top_waiter. Set the FUTEX_WAITERS bit in
1353 * the contended case or if set_waiters is 1. The pi_state is returned
1354 * in ps in contended cases.
Darren Hart52400ba2009-04-03 13:40:49 -07001355 */
Darren Hartbab5bc92009-04-07 23:23:50 -07001356 ret = futex_lock_pi_atomic(pifutex, hb2, key2, ps, top_waiter->task,
1357 set_waiters);
Darren Hart52400ba2009-04-03 13:40:49 -07001358 if (ret == 1)
Darren Hartbeda2c72009-08-09 15:34:39 -07001359 requeue_pi_wake_futex(top_waiter, key2, hb2);
Darren Hart52400ba2009-04-03 13:40:49 -07001360
1361 return ret;
1362}
1363
1364/**
1365 * futex_requeue() - Requeue waiters from uaddr1 to uaddr2
Randy Dunlapfb62db22010-10-13 11:02:34 -07001366 * @uaddr1: source futex user address
Darren Hartb41277d2010-11-08 13:10:09 -08001367 * @flags: futex flags (FLAGS_SHARED, etc.)
Randy Dunlapfb62db22010-10-13 11:02:34 -07001368 * @uaddr2: target futex user address
1369 * @nr_wake: number of waiters to wake (must be 1 for requeue_pi)
1370 * @nr_requeue: number of waiters to requeue (0-INT_MAX)
1371 * @cmpval: @uaddr1 expected value (or %NULL)
1372 * @requeue_pi: if we are attempting to requeue from a non-pi futex to a
Darren Hartb41277d2010-11-08 13:10:09 -08001373 * pi futex (pi to pi requeue is not supported)
Darren Hart52400ba2009-04-03 13:40:49 -07001374 *
1375 * Requeue waiters on uaddr1 to uaddr2. In the requeue_pi case, try to acquire
1376 * uaddr2 atomically on behalf of the top waiter.
1377 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08001378 * Return:
1379 * >=0 - on success, the number of tasks requeued or woken;
Darren Hart52400ba2009-04-03 13:40:49 -07001380 * <0 - on error
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 */
Darren Hartb41277d2010-11-08 13:10:09 -08001382static int futex_requeue(u32 __user *uaddr1, unsigned int flags,
1383 u32 __user *uaddr2, int nr_wake, int nr_requeue,
1384 u32 *cmpval, int requeue_pi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
Peter Zijlstra38d47c12008-09-26 19:32:20 +02001386 union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
Darren Hart52400ba2009-04-03 13:40:49 -07001387 int drop_count = 0, task_count = 0, ret;
1388 struct futex_pi_state *pi_state = NULL;
Ingo Molnare2970f22006-06-27 02:54:47 -07001389 struct futex_hash_bucket *hb1, *hb2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 struct futex_q *this, *next;
Darren Hart52400ba2009-04-03 13:40:49 -07001391 u32 curval2;
1392
1393 if (requeue_pi) {
1394 /*
1395 * requeue_pi requires a pi_state, try to allocate it now
1396 * without any locks in case it fails.
1397 */
1398 if (refill_pi_state_cache())
1399 return -ENOMEM;
1400 /*
1401 * requeue_pi must wake as many tasks as it can, up to nr_wake
1402 * + nr_requeue, since it acquires the rt_mutex prior to
1403 * returning to userspace, so as to not leave the rt_mutex with
1404 * waiters and no owner. However, second and third wake-ups
1405 * cannot be predicted as they involve race conditions with the
1406 * first wake and a fault while looking up the pi_state. Both
1407 * pthread_cond_signal() and pthread_cond_broadcast() should
1408 * use nr_wake=1.
1409 */
1410 if (nr_wake != 1)
1411 return -EINVAL;
1412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Darren Hart42d35d42008-12-29 15:49:53 -08001414retry:
Darren Hart52400ba2009-04-03 13:40:49 -07001415 if (pi_state != NULL) {
1416 /*
1417 * We will have to lookup the pi_state again, so free this one
1418 * to keep the accounting correct.
1419 */
1420 free_pi_state(pi_state);
1421 pi_state = NULL;
1422 }
1423
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001424 ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 if (unlikely(ret != 0))
1426 goto out;
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001427 ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2,
1428 requeue_pi ? VERIFY_WRITE : VERIFY_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 if (unlikely(ret != 0))
Darren Hart42d35d42008-12-29 15:49:53 -08001430 goto out_put_key1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Ingo Molnare2970f22006-06-27 02:54:47 -07001432 hb1 = hash_futex(&key1);
1433 hb2 = hash_futex(&key2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Darren Harte4dc5b72009-03-12 00:56:13 -07001435retry_private:
Ingo Molnar8b8f3192006-07-03 00:25:05 -07001436 double_lock_hb(hb1, hb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Ingo Molnare2970f22006-06-27 02:54:47 -07001438 if (likely(cmpval != NULL)) {
1439 u32 curval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Ingo Molnare2970f22006-06-27 02:54:47 -07001441 ret = get_futex_value_locked(&curval, uaddr1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 if (unlikely(ret)) {
Darren Hart5eb3dc62009-03-12 00:55:52 -07001444 double_unlock_hb(hb1, hb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Darren Harte4dc5b72009-03-12 00:56:13 -07001446 ret = get_user(curval, uaddr1);
1447 if (ret)
1448 goto out_put_keys;
1449
Darren Hartb41277d2010-11-08 13:10:09 -08001450 if (!(flags & FLAGS_SHARED))
Darren Harte4dc5b72009-03-12 00:56:13 -07001451 goto retry_private;
1452
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001453 put_futex_key(&key2);
1454 put_futex_key(&key1);
Darren Harte4dc5b72009-03-12 00:56:13 -07001455 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
Ingo Molnare2970f22006-06-27 02:54:47 -07001457 if (curval != *cmpval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 ret = -EAGAIN;
1459 goto out_unlock;
1460 }
1461 }
1462
Darren Hart52400ba2009-04-03 13:40:49 -07001463 if (requeue_pi && (task_count - nr_wake < nr_requeue)) {
Darren Hartbab5bc92009-04-07 23:23:50 -07001464 /*
1465 * Attempt to acquire uaddr2 and wake the top waiter. If we
1466 * intend to requeue waiters, force setting the FUTEX_WAITERS
1467 * bit. We force this here where we are able to easily handle
1468 * faults rather in the requeue loop below.
1469 */
Darren Hart52400ba2009-04-03 13:40:49 -07001470 ret = futex_proxy_trylock_atomic(uaddr2, hb1, hb2, &key1,
Darren Hartbab5bc92009-04-07 23:23:50 -07001471 &key2, &pi_state, nr_requeue);
Darren Hart52400ba2009-04-03 13:40:49 -07001472
1473 /*
1474 * At this point the top_waiter has either taken uaddr2 or is
1475 * waiting on it. If the former, then the pi_state will not
1476 * exist yet, look it up one more time to ensure we have a
1477 * reference to it.
1478 */
1479 if (ret == 1) {
1480 WARN_ON(pi_state);
Darren Hart89061d32009-10-15 15:30:48 -07001481 drop_count++;
Darren Hart52400ba2009-04-03 13:40:49 -07001482 task_count++;
1483 ret = get_futex_value_locked(&curval2, uaddr2);
1484 if (!ret)
1485 ret = lookup_pi_state(curval2, hb2, &key2,
1486 &pi_state);
1487 }
1488
1489 switch (ret) {
1490 case 0:
1491 break;
1492 case -EFAULT:
1493 double_unlock_hb(hb1, hb2);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001494 put_futex_key(&key2);
1495 put_futex_key(&key1);
Thomas Gleixnerd0725992009-06-11 23:15:43 +02001496 ret = fault_in_user_writeable(uaddr2);
Darren Hart52400ba2009-04-03 13:40:49 -07001497 if (!ret)
1498 goto retry;
1499 goto out;
1500 case -EAGAIN:
1501 /* The owner was exiting, try again. */
1502 double_unlock_hb(hb1, hb2);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001503 put_futex_key(&key2);
1504 put_futex_key(&key1);
Darren Hart52400ba2009-04-03 13:40:49 -07001505 cond_resched();
1506 goto retry;
1507 default:
1508 goto out_unlock;
1509 }
1510 }
1511
Jason Low0d00c7b2014-01-12 15:31:22 -08001512 plist_for_each_entry_safe(this, next, &hb1->chain, list) {
Darren Hart52400ba2009-04-03 13:40:49 -07001513 if (task_count - nr_wake >= nr_requeue)
1514 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Darren Hart52400ba2009-04-03 13:40:49 -07001516 if (!match_futex(&this->key, &key1))
1517 continue;
1518
Darren Hart392741e2009-08-07 15:20:48 -07001519 /*
1520 * FUTEX_WAIT_REQEUE_PI and FUTEX_CMP_REQUEUE_PI should always
1521 * be paired with each other and no other futex ops.
Darren Hartaa109902012-11-26 16:29:56 -08001522 *
1523 * We should never be requeueing a futex_q with a pi_state,
1524 * which is awaiting a futex_unlock_pi().
Darren Hart392741e2009-08-07 15:20:48 -07001525 */
1526 if ((requeue_pi && !this->rt_waiter) ||
Darren Hartaa109902012-11-26 16:29:56 -08001527 (!requeue_pi && this->rt_waiter) ||
1528 this->pi_state) {
Darren Hart392741e2009-08-07 15:20:48 -07001529 ret = -EINVAL;
1530 break;
1531 }
Darren Hart52400ba2009-04-03 13:40:49 -07001532
1533 /*
1534 * Wake nr_wake waiters. For requeue_pi, if we acquired the
1535 * lock, we already woke the top_waiter. If not, it will be
1536 * woken by futex_unlock_pi().
1537 */
1538 if (++task_count <= nr_wake && !requeue_pi) {
1539 wake_futex(this);
1540 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
Darren Hart52400ba2009-04-03 13:40:49 -07001542
Darren Hart84bc4af2009-08-13 17:36:53 -07001543 /* Ensure we requeue to the expected futex for requeue_pi. */
1544 if (requeue_pi && !match_futex(this->requeue_pi_key, &key2)) {
1545 ret = -EINVAL;
1546 break;
1547 }
1548
Darren Hart52400ba2009-04-03 13:40:49 -07001549 /*
1550 * Requeue nr_requeue waiters and possibly one more in the case
1551 * of requeue_pi if we couldn't acquire the lock atomically.
1552 */
1553 if (requeue_pi) {
1554 /* Prepare the waiter to take the rt_mutex. */
1555 atomic_inc(&pi_state->refcount);
1556 this->pi_state = pi_state;
1557 ret = rt_mutex_start_proxy_lock(&pi_state->pi_mutex,
1558 this->rt_waiter,
1559 this->task, 1);
1560 if (ret == 1) {
1561 /* We got the lock. */
Darren Hartbeda2c72009-08-09 15:34:39 -07001562 requeue_pi_wake_futex(this, &key2, hb2);
Darren Hart89061d32009-10-15 15:30:48 -07001563 drop_count++;
Darren Hart52400ba2009-04-03 13:40:49 -07001564 continue;
1565 } else if (ret) {
1566 /* -EDEADLK */
1567 this->pi_state = NULL;
1568 free_pi_state(pi_state);
1569 goto out_unlock;
1570 }
1571 }
1572 requeue_futex(this, hb1, hb2, &key2);
1573 drop_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 }
1575
1576out_unlock:
Darren Hart5eb3dc62009-03-12 00:55:52 -07001577 double_unlock_hb(hb1, hb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Darren Hartcd84a422009-04-02 14:19:38 -07001579 /*
1580 * drop_futex_key_refs() must be called outside the spinlocks. During
1581 * the requeue we moved futex_q's from the hash bucket at key1 to the
1582 * one at key2 and updated their key pointer. We no longer need to
1583 * hold the references to key1.
1584 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 while (--drop_count >= 0)
Rusty Russell9adef582007-05-08 00:26:42 -07001586 drop_futex_key_refs(&key1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Darren Hart42d35d42008-12-29 15:49:53 -08001588out_put_keys:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001589 put_futex_key(&key2);
Darren Hart42d35d42008-12-29 15:49:53 -08001590out_put_key1:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001591 put_futex_key(&key1);
Darren Hart42d35d42008-12-29 15:49:53 -08001592out:
Darren Hart52400ba2009-04-03 13:40:49 -07001593 if (pi_state != NULL)
1594 free_pi_state(pi_state);
1595 return ret ? ret : task_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596}
1597
1598/* The key must be already stored in q->key. */
Eric Sesterhenn82af7ac2008-01-25 10:40:46 +01001599static inline struct futex_hash_bucket *queue_lock(struct futex_q *q)
Namhyung Kim15e408c2010-09-14 21:43:48 +09001600 __acquires(&hb->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
Ingo Molnare2970f22006-06-27 02:54:47 -07001602 struct futex_hash_bucket *hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Ingo Molnare2970f22006-06-27 02:54:47 -07001604 hb = hash_futex(&q->key);
1605 q->lock_ptr = &hb->lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Davidlohr Buesob0c29f72014-01-12 15:31:25 -08001607 spin_lock(&hb->lock); /* implies MB (A) */
Ingo Molnare2970f22006-06-27 02:54:47 -07001608 return hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609}
1610
Darren Hartd40d65c2009-09-21 22:30:15 -07001611static inline void
Jason Low0d00c7b2014-01-12 15:31:22 -08001612queue_unlock(struct futex_hash_bucket *hb)
Namhyung Kim15e408c2010-09-14 21:43:48 +09001613 __releases(&hb->lock)
Darren Hartd40d65c2009-09-21 22:30:15 -07001614{
1615 spin_unlock(&hb->lock);
Darren Hartd40d65c2009-09-21 22:30:15 -07001616}
1617
1618/**
1619 * queue_me() - Enqueue the futex_q on the futex_hash_bucket
1620 * @q: The futex_q to enqueue
1621 * @hb: The destination hash bucket
1622 *
1623 * The hb->lock must be held by the caller, and is released here. A call to
1624 * queue_me() is typically paired with exactly one call to unqueue_me(). The
1625 * exceptions involve the PI related operations, which may use unqueue_me_pi()
1626 * or nothing if the unqueue is done as part of the wake process and the unqueue
1627 * state is implicit in the state of woken task (see futex_wait_requeue_pi() for
1628 * an example).
1629 */
Eric Sesterhenn82af7ac2008-01-25 10:40:46 +01001630static inline void queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
Namhyung Kim15e408c2010-09-14 21:43:48 +09001631 __releases(&hb->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
Pierre Peifferec92d082007-05-09 02:35:00 -07001633 int prio;
1634
1635 /*
1636 * The priority used to register this element is
1637 * - either the real thread-priority for the real-time threads
1638 * (i.e. threads with a priority lower than MAX_RT_PRIO)
1639 * - or MAX_RT_PRIO for non-RT threads.
1640 * Thus, all RT-threads are woken first in priority order, and
1641 * the others are woken last, in FIFO order.
1642 */
1643 prio = min(current->normal_prio, MAX_RT_PRIO);
1644
1645 plist_node_init(&q->list, prio);
Pierre Peifferec92d082007-05-09 02:35:00 -07001646 plist_add(&q->list, &hb->chain);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001647 q->task = current;
Ingo Molnare2970f22006-06-27 02:54:47 -07001648 spin_unlock(&hb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649}
1650
Darren Hartd40d65c2009-09-21 22:30:15 -07001651/**
1652 * unqueue_me() - Remove the futex_q from its futex_hash_bucket
1653 * @q: The futex_q to unqueue
1654 *
1655 * The q->lock_ptr must not be held by the caller. A call to unqueue_me() must
1656 * be paired with exactly one earlier call to queue_me().
1657 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08001658 * Return:
1659 * 1 - if the futex_q was still queued (and we removed unqueued it);
Darren Hartd40d65c2009-09-21 22:30:15 -07001660 * 0 - if the futex_q was already removed by the waking thread
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662static int unqueue_me(struct futex_q *q)
1663{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 spinlock_t *lock_ptr;
Ingo Molnare2970f22006-06-27 02:54:47 -07001665 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 /* In the common case we don't take the spinlock, which is nice. */
Darren Hart42d35d42008-12-29 15:49:53 -08001668retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 lock_ptr = q->lock_ptr;
Christian Borntraegere91467e2006-08-05 12:13:52 -07001670 barrier();
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07001671 if (lock_ptr != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 spin_lock(lock_ptr);
1673 /*
1674 * q->lock_ptr can change between reading it and
1675 * spin_lock(), causing us to take the wrong lock. This
1676 * corrects the race condition.
1677 *
1678 * Reasoning goes like this: if we have the wrong lock,
1679 * q->lock_ptr must have changed (maybe several times)
1680 * between reading it and the spin_lock(). It can
1681 * change again after the spin_lock() but only if it was
1682 * already changed before the spin_lock(). It cannot,
1683 * however, change back to the original value. Therefore
1684 * we can detect whether we acquired the correct lock.
1685 */
1686 if (unlikely(lock_ptr != q->lock_ptr)) {
1687 spin_unlock(lock_ptr);
1688 goto retry;
1689 }
Lai Jiangshan2e129782010-12-22 14:18:50 +08001690 __unqueue_futex(q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001691
1692 BUG_ON(q->pi_state);
1693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 spin_unlock(lock_ptr);
1695 ret = 1;
1696 }
1697
Rusty Russell9adef582007-05-08 00:26:42 -07001698 drop_futex_key_refs(&q->key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 return ret;
1700}
1701
Ingo Molnarc87e2832006-06-27 02:54:58 -07001702/*
1703 * PI futexes can not be requeued and must remove themself from the
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001704 * hash bucket. The hash bucket lock (i.e. lock_ptr) is held on entry
1705 * and dropped here.
Ingo Molnarc87e2832006-06-27 02:54:58 -07001706 */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001707static void unqueue_me_pi(struct futex_q *q)
Namhyung Kim15e408c2010-09-14 21:43:48 +09001708 __releases(q->lock_ptr)
Ingo Molnarc87e2832006-06-27 02:54:58 -07001709{
Lai Jiangshan2e129782010-12-22 14:18:50 +08001710 __unqueue_futex(q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001711
1712 BUG_ON(!q->pi_state);
1713 free_pi_state(q->pi_state);
1714 q->pi_state = NULL;
1715
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001716 spin_unlock(q->lock_ptr);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001717}
1718
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001719/*
Thomas Gleixnercdf71a12008-01-08 19:47:38 +01001720 * Fixup the pi_state owner with the new owner.
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001721 *
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001722 * Must be called with hash bucket lock held and mm->sem held for non
1723 * private futexes.
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001724 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001725static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001726 struct task_struct *newowner)
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001727{
Thomas Gleixnercdf71a12008-01-08 19:47:38 +01001728 u32 newtid = task_pid_vnr(newowner) | FUTEX_WAITERS;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001729 struct futex_pi_state *pi_state = q->pi_state;
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02001730 struct task_struct *oldowner = pi_state->owner;
Vitaliy Ivanov7cfdaf32011-07-07 15:10:31 +03001731 u32 uval, uninitialized_var(curval), newval;
Darren Harte4dc5b72009-03-12 00:56:13 -07001732 int ret;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001733
1734 /* Owner died? */
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02001735 if (!pi_state->owner)
1736 newtid |= FUTEX_OWNER_DIED;
1737
1738 /*
1739 * We are here either because we stole the rtmutex from the
Lai Jiangshan81612392011-01-14 17:09:41 +08001740 * previous highest priority waiter or we are the highest priority
1741 * waiter but failed to get the rtmutex the first time.
1742 * We have to replace the newowner TID in the user space variable.
1743 * This must be atomic as we have to preserve the owner died bit here.
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02001744 *
Darren Hartb2d09942009-03-12 00:55:37 -07001745 * Note: We write the user space value _before_ changing the pi_state
1746 * because we can fault here. Imagine swapped out pages or a fork
1747 * that marked all the anonymous memory readonly for cow.
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02001748 *
1749 * Modifying pi_state _before_ the user space value would
1750 * leave the pi_state in an inconsistent state when we fault
1751 * here, because we need to drop the hash bucket lock to
1752 * handle the fault. This might be observed in the PID check
1753 * in lookup_pi_state.
1754 */
1755retry:
1756 if (get_futex_value_locked(&uval, uaddr))
1757 goto handle_fault;
1758
1759 while (1) {
1760 newval = (uval & FUTEX_OWNER_DIED) | newtid;
1761
Michel Lespinasse37a9d912011-03-10 18:48:51 -08001762 if (cmpxchg_futex_value_locked(&curval, uaddr, uval, newval))
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02001763 goto handle_fault;
1764 if (curval == uval)
1765 break;
1766 uval = curval;
1767 }
1768
1769 /*
1770 * We fixed up user space. Now we need to fix the pi_state
1771 * itself.
1772 */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001773 if (pi_state->owner != NULL) {
Thomas Gleixner1d615482009-11-17 14:54:03 +01001774 raw_spin_lock_irq(&pi_state->owner->pi_lock);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001775 WARN_ON(list_empty(&pi_state->list));
1776 list_del_init(&pi_state->list);
Thomas Gleixner1d615482009-11-17 14:54:03 +01001777 raw_spin_unlock_irq(&pi_state->owner->pi_lock);
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02001778 }
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001779
Thomas Gleixnercdf71a12008-01-08 19:47:38 +01001780 pi_state->owner = newowner;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001781
Thomas Gleixner1d615482009-11-17 14:54:03 +01001782 raw_spin_lock_irq(&newowner->pi_lock);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001783 WARN_ON(!list_empty(&pi_state->list));
Thomas Gleixnercdf71a12008-01-08 19:47:38 +01001784 list_add(&pi_state->list, &newowner->pi_state_list);
Thomas Gleixner1d615482009-11-17 14:54:03 +01001785 raw_spin_unlock_irq(&newowner->pi_lock);
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02001786 return 0;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001787
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001788 /*
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02001789 * To handle the page fault we need to drop the hash bucket
Lai Jiangshan81612392011-01-14 17:09:41 +08001790 * lock here. That gives the other task (either the highest priority
1791 * waiter itself or the task which stole the rtmutex) the
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02001792 * chance to try the fixup of the pi_state. So once we are
1793 * back from handling the fault we need to check the pi_state
1794 * after reacquiring the hash bucket lock and before trying to
1795 * do another fixup. When the fixup has been done already we
1796 * simply return.
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001797 */
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02001798handle_fault:
1799 spin_unlock(q->lock_ptr);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001800
Thomas Gleixnerd0725992009-06-11 23:15:43 +02001801 ret = fault_in_user_writeable(uaddr);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001802
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02001803 spin_lock(q->lock_ptr);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001804
Thomas Gleixner1b7558e2008-06-23 11:21:58 +02001805 /*
1806 * Check if someone else fixed it for us:
1807 */
1808 if (pi_state->owner != oldowner)
1809 return 0;
1810
1811 if (ret)
1812 return ret;
1813
1814 goto retry;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001815}
1816
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001817static long futex_wait_restart(struct restart_block *restart);
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001818
Darren Hartca5f9522009-04-03 13:39:33 -07001819/**
Darren Hartdd973992009-04-03 13:40:02 -07001820 * fixup_owner() - Post lock pi_state and corner case management
1821 * @uaddr: user address of the futex
Darren Hartdd973992009-04-03 13:40:02 -07001822 * @q: futex_q (contains pi_state and access to the rt_mutex)
1823 * @locked: if the attempt to take the rt_mutex succeeded (1) or not (0)
1824 *
1825 * After attempting to lock an rt_mutex, this function is called to cleanup
1826 * the pi_state owner as well as handle race conditions that may allow us to
1827 * acquire the lock. Must be called with the hb lock held.
1828 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08001829 * Return:
1830 * 1 - success, lock taken;
1831 * 0 - success, lock not taken;
Darren Hartdd973992009-04-03 13:40:02 -07001832 * <0 - on error (-EFAULT)
1833 */
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001834static int fixup_owner(u32 __user *uaddr, struct futex_q *q, int locked)
Darren Hartdd973992009-04-03 13:40:02 -07001835{
1836 struct task_struct *owner;
1837 int ret = 0;
1838
1839 if (locked) {
1840 /*
1841 * Got the lock. We might not be the anticipated owner if we
1842 * did a lock-steal - fix up the PI-state in that case:
1843 */
1844 if (q->pi_state->owner != current)
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001845 ret = fixup_pi_state_owner(uaddr, q, current);
Darren Hartdd973992009-04-03 13:40:02 -07001846 goto out;
1847 }
1848
1849 /*
1850 * Catch the rare case, where the lock was released when we were on the
1851 * way back before we locked the hash bucket.
1852 */
1853 if (q->pi_state->owner == current) {
1854 /*
1855 * Try to get the rt_mutex now. This might fail as some other
1856 * task acquired the rt_mutex after we removed ourself from the
1857 * rt_mutex waiters list.
1858 */
1859 if (rt_mutex_trylock(&q->pi_state->pi_mutex)) {
1860 locked = 1;
1861 goto out;
1862 }
1863
1864 /*
1865 * pi_state is incorrect, some other task did a lock steal and
1866 * we returned due to timeout or signal without taking the
Lai Jiangshan81612392011-01-14 17:09:41 +08001867 * rt_mutex. Too late.
Darren Hartdd973992009-04-03 13:40:02 -07001868 */
Lai Jiangshan81612392011-01-14 17:09:41 +08001869 raw_spin_lock(&q->pi_state->pi_mutex.wait_lock);
Darren Hartdd973992009-04-03 13:40:02 -07001870 owner = rt_mutex_owner(&q->pi_state->pi_mutex);
Lai Jiangshan81612392011-01-14 17:09:41 +08001871 if (!owner)
1872 owner = rt_mutex_next_owner(&q->pi_state->pi_mutex);
1873 raw_spin_unlock(&q->pi_state->pi_mutex.wait_lock);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001874 ret = fixup_pi_state_owner(uaddr, q, owner);
Darren Hartdd973992009-04-03 13:40:02 -07001875 goto out;
1876 }
1877
1878 /*
1879 * Paranoia check. If we did not take the lock, then we should not be
Lai Jiangshan81612392011-01-14 17:09:41 +08001880 * the owner of the rt_mutex.
Darren Hartdd973992009-04-03 13:40:02 -07001881 */
1882 if (rt_mutex_owner(&q->pi_state->pi_mutex) == current)
1883 printk(KERN_ERR "fixup_owner: ret = %d pi-mutex: %p "
1884 "pi-state %p\n", ret,
1885 q->pi_state->pi_mutex.owner,
1886 q->pi_state->owner);
1887
1888out:
1889 return ret ? ret : locked;
1890}
1891
1892/**
Darren Hartca5f9522009-04-03 13:39:33 -07001893 * futex_wait_queue_me() - queue_me() and wait for wakeup, timeout, or signal
1894 * @hb: the futex hash bucket, must be locked by the caller
1895 * @q: the futex_q to queue up on
1896 * @timeout: the prepared hrtimer_sleeper, or null for no timeout
Darren Hartca5f9522009-04-03 13:39:33 -07001897 */
1898static void futex_wait_queue_me(struct futex_hash_bucket *hb, struct futex_q *q,
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001899 struct hrtimer_sleeper *timeout)
Darren Hartca5f9522009-04-03 13:39:33 -07001900{
Darren Hart9beba3c2009-09-24 11:54:47 -07001901 /*
1902 * The task state is guaranteed to be set before another task can
1903 * wake it. set_current_state() is implemented using set_mb() and
1904 * queue_me() calls spin_unlock() upon completion, both serializing
1905 * access to the hash list and forcing another memory barrier.
1906 */
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02001907 set_current_state(TASK_INTERRUPTIBLE);
Darren Hart0729e192009-09-21 22:30:38 -07001908 queue_me(q, hb);
Darren Hartca5f9522009-04-03 13:39:33 -07001909
1910 /* Arm the timer */
1911 if (timeout) {
1912 hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
1913 if (!hrtimer_active(&timeout->timer))
1914 timeout->task = NULL;
1915 }
1916
1917 /*
Darren Hart0729e192009-09-21 22:30:38 -07001918 * If we have been removed from the hash list, then another task
1919 * has tried to wake us, and we can skip the call to schedule().
Darren Hartca5f9522009-04-03 13:39:33 -07001920 */
1921 if (likely(!plist_node_empty(&q->list))) {
1922 /*
1923 * If the timer has already expired, current will already be
1924 * flagged for rescheduling. Only call schedule if there
1925 * is no timeout, or if it has yet to expire.
1926 */
1927 if (!timeout || timeout->task)
Colin Cross88c80042013-05-01 18:35:05 -07001928 freezable_schedule();
Darren Hartca5f9522009-04-03 13:39:33 -07001929 }
1930 __set_current_state(TASK_RUNNING);
1931}
1932
Darren Hartf8010732009-04-03 13:40:40 -07001933/**
1934 * futex_wait_setup() - Prepare to wait on a futex
1935 * @uaddr: the futex userspace address
1936 * @val: the expected value
Darren Hartb41277d2010-11-08 13:10:09 -08001937 * @flags: futex flags (FLAGS_SHARED, etc.)
Darren Hartf8010732009-04-03 13:40:40 -07001938 * @q: the associated futex_q
1939 * @hb: storage for hash_bucket pointer to be returned to caller
1940 *
1941 * Setup the futex_q and locate the hash_bucket. Get the futex value and
1942 * compare it with the expected value. Handle atomic faults internally.
1943 * Return with the hb lock held and a q.key reference on success, and unlocked
1944 * with no q.key reference on failure.
1945 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08001946 * Return:
1947 * 0 - uaddr contains val and hb has been locked;
Bart Van Asscheca4a04c2011-07-17 09:01:00 +02001948 * <1 - -EFAULT or -EWOULDBLOCK (uaddr does not contain val) and hb is unlocked
Darren Hartf8010732009-04-03 13:40:40 -07001949 */
Darren Hartb41277d2010-11-08 13:10:09 -08001950static int futex_wait_setup(u32 __user *uaddr, u32 val, unsigned int flags,
Darren Hartf8010732009-04-03 13:40:40 -07001951 struct futex_q *q, struct futex_hash_bucket **hb)
1952{
1953 u32 uval;
1954 int ret;
1955
1956 /*
1957 * Access the page AFTER the hash-bucket is locked.
1958 * Order is important:
1959 *
1960 * Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val);
1961 * Userspace waker: if (cond(var)) { var = new; futex_wake(&var); }
1962 *
1963 * The basic logical guarantee of a futex is that it blocks ONLY
1964 * if cond(var) is known to be true at the time of blocking, for
Michel Lespinasse8fe8f542011-03-06 18:07:50 -08001965 * any cond. If we locked the hash-bucket after testing *uaddr, that
1966 * would open a race condition where we could block indefinitely with
Darren Hartf8010732009-04-03 13:40:40 -07001967 * cond(var) false, which would violate the guarantee.
1968 *
Michel Lespinasse8fe8f542011-03-06 18:07:50 -08001969 * On the other hand, we insert q and release the hash-bucket only
1970 * after testing *uaddr. This guarantees that futex_wait() will NOT
1971 * absorb a wakeup if *uaddr does not match the desired values
1972 * while the syscall executes.
Darren Hartf8010732009-04-03 13:40:40 -07001973 */
1974retry:
Shawn Bohrer9ea71502011-06-30 11:21:32 -05001975 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q->key, VERIFY_READ);
Darren Hartf8010732009-04-03 13:40:40 -07001976 if (unlikely(ret != 0))
Darren Harta5a2a0c2009-04-10 09:50:05 -07001977 return ret;
Darren Hartf8010732009-04-03 13:40:40 -07001978
1979retry_private:
1980 *hb = queue_lock(q);
1981
1982 ret = get_futex_value_locked(&uval, uaddr);
1983
1984 if (ret) {
Jason Low0d00c7b2014-01-12 15:31:22 -08001985 queue_unlock(*hb);
Darren Hartf8010732009-04-03 13:40:40 -07001986
1987 ret = get_user(uval, uaddr);
1988 if (ret)
1989 goto out;
1990
Darren Hartb41277d2010-11-08 13:10:09 -08001991 if (!(flags & FLAGS_SHARED))
Darren Hartf8010732009-04-03 13:40:40 -07001992 goto retry_private;
1993
Thomas Gleixnerae791a22010-11-10 13:30:36 +01001994 put_futex_key(&q->key);
Darren Hartf8010732009-04-03 13:40:40 -07001995 goto retry;
1996 }
1997
1998 if (uval != val) {
Jason Low0d00c7b2014-01-12 15:31:22 -08001999 queue_unlock(*hb);
Darren Hartf8010732009-04-03 13:40:40 -07002000 ret = -EWOULDBLOCK;
2001 }
2002
2003out:
2004 if (ret)
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002005 put_futex_key(&q->key);
Darren Hartf8010732009-04-03 13:40:40 -07002006 return ret;
2007}
2008
Darren Hartb41277d2010-11-08 13:10:09 -08002009static int futex_wait(u32 __user *uaddr, unsigned int flags, u32 val,
2010 ktime_t *abs_time, u32 bitset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011{
Darren Hartca5f9522009-04-03 13:39:33 -07002012 struct hrtimer_sleeper timeout, *to = NULL;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002013 struct restart_block *restart;
Ingo Molnare2970f22006-06-27 02:54:47 -07002014 struct futex_hash_bucket *hb;
Darren Hart5bdb05f2010-11-08 13:40:28 -08002015 struct futex_q q = futex_q_init;
Ingo Molnare2970f22006-06-27 02:54:47 -07002016 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Thomas Gleixnercd689982008-02-01 17:45:14 +01002018 if (!bitset)
2019 return -EINVAL;
Thomas Gleixnercd689982008-02-01 17:45:14 +01002020 q.bitset = bitset;
Darren Hartca5f9522009-04-03 13:39:33 -07002021
2022 if (abs_time) {
2023 to = &timeout;
2024
Darren Hartb41277d2010-11-08 13:10:09 -08002025 hrtimer_init_on_stack(&to->timer, (flags & FLAGS_CLOCKRT) ?
2026 CLOCK_REALTIME : CLOCK_MONOTONIC,
2027 HRTIMER_MODE_ABS);
Darren Hartca5f9522009-04-03 13:39:33 -07002028 hrtimer_init_sleeper(to, current);
2029 hrtimer_set_expires_range_ns(&to->timer, *abs_time,
2030 current->timer_slack_ns);
2031 }
2032
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002033retry:
Darren Hart7ada8762010-10-17 08:35:04 -07002034 /*
2035 * Prepare to wait on uaddr. On success, holds hb lock and increments
2036 * q.key refs.
2037 */
Darren Hartb41277d2010-11-08 13:10:09 -08002038 ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
Darren Hartf8010732009-04-03 13:40:40 -07002039 if (ret)
Darren Hart42d35d42008-12-29 15:49:53 -08002040 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
Darren Hartca5f9522009-04-03 13:39:33 -07002042 /* queue_me and wait for wakeup, timeout, or a signal. */
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02002043 futex_wait_queue_me(hb, &q, to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
2045 /* If we were woken (and unqueued), we succeeded, whatever. */
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002046 ret = 0;
Darren Hart7ada8762010-10-17 08:35:04 -07002047 /* unqueue_me() drops q.key ref */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 if (!unqueue_me(&q))
Darren Hart7ada8762010-10-17 08:35:04 -07002049 goto out;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002050 ret = -ETIMEDOUT;
Darren Hartca5f9522009-04-03 13:39:33 -07002051 if (to && !to->task)
Darren Hart7ada8762010-10-17 08:35:04 -07002052 goto out;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002053
Ingo Molnare2970f22006-06-27 02:54:47 -07002054 /*
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002055 * We expect signal_pending(current), but we might be the
2056 * victim of a spurious wakeup as well.
Ingo Molnare2970f22006-06-27 02:54:47 -07002057 */
Darren Hart7ada8762010-10-17 08:35:04 -07002058 if (!signal_pending(current))
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002059 goto retry;
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002060
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002061 ret = -ERESTARTSYS;
Pierre Peifferc19384b2007-05-09 02:35:02 -07002062 if (!abs_time)
Darren Hart7ada8762010-10-17 08:35:04 -07002063 goto out;
Steven Rostedtce6bd422007-12-05 15:46:09 +01002064
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002065 restart = &current_thread_info()->restart_block;
2066 restart->fn = futex_wait_restart;
Namhyung Kima3c74c52010-09-14 21:43:47 +09002067 restart->futex.uaddr = uaddr;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002068 restart->futex.val = val;
2069 restart->futex.time = abs_time->tv64;
2070 restart->futex.bitset = bitset;
Darren Hart0cd9c642011-04-14 15:41:57 -07002071 restart->futex.flags = flags | FLAGS_HAS_TIMEOUT;
Peter Zijlstra2fff78c2009-02-11 18:10:10 +01002072
2073 ret = -ERESTART_RESTARTBLOCK;
2074
Darren Hart42d35d42008-12-29 15:49:53 -08002075out:
Darren Hartca5f9522009-04-03 13:39:33 -07002076 if (to) {
2077 hrtimer_cancel(&to->timer);
2078 destroy_hrtimer_on_stack(&to->timer);
2079 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07002080 return ret;
2081}
2082
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002083
2084static long futex_wait_restart(struct restart_block *restart)
2085{
Namhyung Kima3c74c52010-09-14 21:43:47 +09002086 u32 __user *uaddr = restart->futex.uaddr;
Darren Harta72188d2009-04-03 13:40:22 -07002087 ktime_t t, *tp = NULL;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002088
Darren Harta72188d2009-04-03 13:40:22 -07002089 if (restart->futex.flags & FLAGS_HAS_TIMEOUT) {
2090 t.tv64 = restart->futex.time;
2091 tp = &t;
2092 }
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002093 restart->fn = do_no_restart_syscall;
Darren Hartb41277d2010-11-08 13:10:09 -08002094
2095 return (long)futex_wait(uaddr, restart->futex.flags,
2096 restart->futex.val, tp, restart->futex.bitset);
Nick Piggin72c1bbf2007-05-08 00:26:43 -07002097}
2098
2099
Ingo Molnarc87e2832006-06-27 02:54:58 -07002100/*
2101 * Userspace tried a 0 -> TID atomic transition of the futex value
2102 * and failed. The kernel side here does the whole locking operation:
2103 * if there are waiters then it will block, it does PI, etc. (Due to
2104 * races the kernel might see a 0 value of the futex too.)
2105 */
Darren Hartb41277d2010-11-08 13:10:09 -08002106static int futex_lock_pi(u32 __user *uaddr, unsigned int flags, int detect,
2107 ktime_t *time, int trylock)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002108{
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07002109 struct hrtimer_sleeper timeout, *to = NULL;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002110 struct futex_hash_bucket *hb;
Darren Hart5bdb05f2010-11-08 13:40:28 -08002111 struct futex_q q = futex_q_init;
Darren Hartdd973992009-04-03 13:40:02 -07002112 int res, ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002113
2114 if (refill_pi_state_cache())
2115 return -ENOMEM;
2116
Pierre Peifferc19384b2007-05-09 02:35:02 -07002117 if (time) {
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07002118 to = &timeout;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07002119 hrtimer_init_on_stack(&to->timer, CLOCK_REALTIME,
2120 HRTIMER_MODE_ABS);
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07002121 hrtimer_init_sleeper(to, current);
Arjan van de Vencc584b22008-09-01 15:02:30 -07002122 hrtimer_set_expires(&to->timer, *time);
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07002123 }
2124
Darren Hart42d35d42008-12-29 15:49:53 -08002125retry:
Shawn Bohrer9ea71502011-06-30 11:21:32 -05002126 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q.key, VERIFY_WRITE);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002127 if (unlikely(ret != 0))
Darren Hart42d35d42008-12-29 15:49:53 -08002128 goto out;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002129
Darren Harte4dc5b72009-03-12 00:56:13 -07002130retry_private:
Eric Sesterhenn82af7ac2008-01-25 10:40:46 +01002131 hb = queue_lock(&q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002132
Darren Hartbab5bc92009-04-07 23:23:50 -07002133 ret = futex_lock_pi_atomic(uaddr, hb, &q.key, &q.pi_state, current, 0);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002134 if (unlikely(ret)) {
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002135 switch (ret) {
Darren Hart1a520842009-04-03 13:39:52 -07002136 case 1:
2137 /* We got the lock. */
2138 ret = 0;
2139 goto out_unlock_put_key;
2140 case -EFAULT:
2141 goto uaddr_faulted;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002142 case -EAGAIN:
2143 /*
2144 * Task is exiting and we just wait for the
2145 * exit to complete.
2146 */
Jason Low0d00c7b2014-01-12 15:31:22 -08002147 queue_unlock(hb);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002148 put_futex_key(&q.key);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002149 cond_resched();
2150 goto retry;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002151 default:
Darren Hart42d35d42008-12-29 15:49:53 -08002152 goto out_unlock_put_key;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002153 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07002154 }
2155
2156 /*
2157 * Only actually queue now that the atomic ops are done:
2158 */
Eric Sesterhenn82af7ac2008-01-25 10:40:46 +01002159 queue_me(&q, hb);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002160
Ingo Molnarc87e2832006-06-27 02:54:58 -07002161 WARN_ON(!q.pi_state);
2162 /*
2163 * Block on the PI mutex:
2164 */
2165 if (!trylock)
2166 ret = rt_mutex_timed_lock(&q.pi_state->pi_mutex, to, 1);
2167 else {
2168 ret = rt_mutex_trylock(&q.pi_state->pi_mutex);
2169 /* Fixup the trylock return value: */
2170 ret = ret ? 0 : -EWOULDBLOCK;
2171 }
2172
Vernon Mauerya99e4e42006-07-01 04:35:42 -07002173 spin_lock(q.lock_ptr);
Darren Hartdd973992009-04-03 13:40:02 -07002174 /*
2175 * Fixup the pi_state owner and possibly acquire the lock if we
2176 * haven't already.
2177 */
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002178 res = fixup_owner(uaddr, &q, !ret);
Darren Hartdd973992009-04-03 13:40:02 -07002179 /*
2180 * If fixup_owner() returned an error, proprogate that. If it acquired
2181 * the lock, clear our -ETIMEDOUT or -EINTR.
2182 */
2183 if (res)
2184 ret = (res < 0) ? res : 0;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002185
Darren Harte8f63862009-03-12 00:56:06 -07002186 /*
Darren Hartdd973992009-04-03 13:40:02 -07002187 * If fixup_owner() faulted and was unable to handle the fault, unlock
2188 * it and return the fault to userspace.
Darren Harte8f63862009-03-12 00:56:06 -07002189 */
2190 if (ret && (rt_mutex_owner(&q.pi_state->pi_mutex) == current))
2191 rt_mutex_unlock(&q.pi_state->pi_mutex);
2192
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002193 /* Unqueue and drop the lock */
2194 unqueue_me_pi(&q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002195
Mikael Pettersson5ecb01c2010-01-23 22:36:29 +01002196 goto out_put_key;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002197
Darren Hart42d35d42008-12-29 15:49:53 -08002198out_unlock_put_key:
Jason Low0d00c7b2014-01-12 15:31:22 -08002199 queue_unlock(hb);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002200
Darren Hart42d35d42008-12-29 15:49:53 -08002201out_put_key:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002202 put_futex_key(&q.key);
Darren Hart42d35d42008-12-29 15:49:53 -08002203out:
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07002204 if (to)
2205 destroy_hrtimer_on_stack(&to->timer);
Darren Hartdd973992009-04-03 13:40:02 -07002206 return ret != -EINTR ? ret : -ERESTARTNOINTR;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002207
Darren Hart42d35d42008-12-29 15:49:53 -08002208uaddr_faulted:
Jason Low0d00c7b2014-01-12 15:31:22 -08002209 queue_unlock(hb);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002210
Thomas Gleixnerd0725992009-06-11 23:15:43 +02002211 ret = fault_in_user_writeable(uaddr);
Darren Harte4dc5b72009-03-12 00:56:13 -07002212 if (ret)
2213 goto out_put_key;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002214
Darren Hartb41277d2010-11-08 13:10:09 -08002215 if (!(flags & FLAGS_SHARED))
Darren Harte4dc5b72009-03-12 00:56:13 -07002216 goto retry_private;
2217
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002218 put_futex_key(&q.key);
Darren Harte4dc5b72009-03-12 00:56:13 -07002219 goto retry;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002220}
2221
2222/*
Ingo Molnarc87e2832006-06-27 02:54:58 -07002223 * Userspace attempted a TID -> 0 atomic transition, and failed.
2224 * This is the in-kernel slowpath: we look up the PI state (if any),
2225 * and do the rt-mutex unlock.
2226 */
Darren Hartb41277d2010-11-08 13:10:09 -08002227static int futex_unlock_pi(u32 __user *uaddr, unsigned int flags)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002228{
2229 struct futex_hash_bucket *hb;
2230 struct futex_q *this, *next;
Peter Zijlstra38d47c12008-09-26 19:32:20 +02002231 union futex_key key = FUTEX_KEY_INIT;
Thomas Gleixnerc0c9ed12011-03-11 11:51:22 +01002232 u32 uval, vpid = task_pid_vnr(current);
Darren Harte4dc5b72009-03-12 00:56:13 -07002233 int ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002234
2235retry:
2236 if (get_user(uval, uaddr))
2237 return -EFAULT;
2238 /*
2239 * We release only a lock we actually own:
2240 */
Thomas Gleixnerc0c9ed12011-03-11 11:51:22 +01002241 if ((uval & FUTEX_TID_MASK) != vpid)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002242 return -EPERM;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002243
Shawn Bohrer9ea71502011-06-30 11:21:32 -05002244 ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, VERIFY_WRITE);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002245 if (unlikely(ret != 0))
2246 goto out;
2247
2248 hb = hash_futex(&key);
2249 spin_lock(&hb->lock);
2250
Ingo Molnarc87e2832006-06-27 02:54:58 -07002251 /*
2252 * To avoid races, try to do the TID -> 0 atomic transition
2253 * again. If it succeeds then we can return without waking
2254 * anyone else up:
2255 */
Michel Lespinasse37a9d912011-03-10 18:48:51 -08002256 if (!(uval & FUTEX_OWNER_DIED) &&
2257 cmpxchg_futex_value_locked(&uval, uaddr, vpid, 0))
Ingo Molnarc87e2832006-06-27 02:54:58 -07002258 goto pi_faulted;
2259 /*
2260 * Rare case: we managed to release the lock atomically,
2261 * no need to wake anyone else up:
2262 */
Thomas Gleixnerc0c9ed12011-03-11 11:51:22 +01002263 if (unlikely(uval == vpid))
Ingo Molnarc87e2832006-06-27 02:54:58 -07002264 goto out_unlock;
2265
2266 /*
2267 * Ok, other tasks may need to be woken up - check waiters
2268 * and do the wakeup if necessary:
2269 */
Jason Low0d00c7b2014-01-12 15:31:22 -08002270 plist_for_each_entry_safe(this, next, &hb->chain, list) {
Ingo Molnarc87e2832006-06-27 02:54:58 -07002271 if (!match_futex (&this->key, &key))
2272 continue;
2273 ret = wake_futex_pi(uaddr, uval, this);
2274 /*
2275 * The atomic access to the futex value
2276 * generated a pagefault, so retry the
2277 * user-access and the wakeup:
2278 */
2279 if (ret == -EFAULT)
2280 goto pi_faulted;
2281 goto out_unlock;
2282 }
2283 /*
2284 * No waiters - kernel unlocks the futex:
2285 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002286 if (!(uval & FUTEX_OWNER_DIED)) {
2287 ret = unlock_futex_pi(uaddr, uval);
2288 if (ret == -EFAULT)
2289 goto pi_faulted;
2290 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07002291
2292out_unlock:
2293 spin_unlock(&hb->lock);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002294 put_futex_key(&key);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002295
Darren Hart42d35d42008-12-29 15:49:53 -08002296out:
Ingo Molnarc87e2832006-06-27 02:54:58 -07002297 return ret;
2298
2299pi_faulted:
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002300 spin_unlock(&hb->lock);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002301 put_futex_key(&key);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002302
Thomas Gleixnerd0725992009-06-11 23:15:43 +02002303 ret = fault_in_user_writeable(uaddr);
Darren Hartb5686362008-12-18 15:06:34 -08002304 if (!ret)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002305 goto retry;
2306
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 return ret;
2308}
2309
Darren Hart52400ba2009-04-03 13:40:49 -07002310/**
2311 * handle_early_requeue_pi_wakeup() - Detect early wakeup on the initial futex
2312 * @hb: the hash_bucket futex_q was original enqueued on
2313 * @q: the futex_q woken while waiting to be requeued
2314 * @key2: the futex_key of the requeue target futex
2315 * @timeout: the timeout associated with the wait (NULL if none)
2316 *
2317 * Detect if the task was woken on the initial futex as opposed to the requeue
2318 * target futex. If so, determine if it was a timeout or a signal that caused
2319 * the wakeup and return the appropriate error code to the caller. Must be
2320 * called with the hb lock held.
2321 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002322 * Return:
2323 * 0 = no early wakeup detected;
2324 * <0 = -ETIMEDOUT or -ERESTARTNOINTR
Darren Hart52400ba2009-04-03 13:40:49 -07002325 */
2326static inline
2327int handle_early_requeue_pi_wakeup(struct futex_hash_bucket *hb,
2328 struct futex_q *q, union futex_key *key2,
2329 struct hrtimer_sleeper *timeout)
2330{
2331 int ret = 0;
2332
2333 /*
2334 * With the hb lock held, we avoid races while we process the wakeup.
2335 * We only need to hold hb (and not hb2) to ensure atomicity as the
2336 * wakeup code can't change q.key from uaddr to uaddr2 if we hold hb.
2337 * It can't be requeued from uaddr2 to something else since we don't
2338 * support a PI aware source futex for requeue.
2339 */
2340 if (!match_futex(&q->key, key2)) {
2341 WARN_ON(q->lock_ptr && (&hb->lock != q->lock_ptr));
2342 /*
2343 * We were woken prior to requeue by a timeout or a signal.
2344 * Unqueue the futex_q and determine which it was.
2345 */
Lai Jiangshan2e129782010-12-22 14:18:50 +08002346 plist_del(&q->list, &hb->chain);
Darren Hart52400ba2009-04-03 13:40:49 -07002347
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002348 /* Handle spurious wakeups gracefully */
Thomas Gleixner11df6dd2009-10-28 20:26:48 +01002349 ret = -EWOULDBLOCK;
Darren Hart52400ba2009-04-03 13:40:49 -07002350 if (timeout && !timeout->task)
2351 ret = -ETIMEDOUT;
Thomas Gleixnerd58e6572009-10-13 20:40:43 +02002352 else if (signal_pending(current))
Thomas Gleixner1c840c12009-05-20 09:22:40 +02002353 ret = -ERESTARTNOINTR;
Darren Hart52400ba2009-04-03 13:40:49 -07002354 }
2355 return ret;
2356}
2357
2358/**
2359 * futex_wait_requeue_pi() - Wait on uaddr and take uaddr2
Darren Hart56ec1602009-09-21 22:29:59 -07002360 * @uaddr: the futex we initially wait on (non-pi)
Darren Hartb41277d2010-11-08 13:10:09 -08002361 * @flags: futex flags (FLAGS_SHARED, FLAGS_CLOCKRT, etc.), they must be
Darren Hart52400ba2009-04-03 13:40:49 -07002362 * the same type, no requeueing from private to shared, etc.
2363 * @val: the expected value of uaddr
2364 * @abs_time: absolute timeout
Darren Hart56ec1602009-09-21 22:29:59 -07002365 * @bitset: 32 bit wakeup bitset set by userspace, defaults to all
Darren Hart52400ba2009-04-03 13:40:49 -07002366 * @uaddr2: the pi futex we will take prior to returning to user-space
2367 *
2368 * The caller will wait on uaddr and will be requeued by futex_requeue() to
Darren Hart6f7b0a22012-07-20 11:53:31 -07002369 * uaddr2 which must be PI aware and unique from uaddr. Normal wakeup will wake
2370 * on uaddr2 and complete the acquisition of the rt_mutex prior to returning to
2371 * userspace. This ensures the rt_mutex maintains an owner when it has waiters;
2372 * without one, the pi logic would not know which task to boost/deboost, if
2373 * there was a need to.
Darren Hart52400ba2009-04-03 13:40:49 -07002374 *
2375 * We call schedule in futex_wait_queue_me() when we enqueue and return there
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002376 * via the following--
Darren Hart52400ba2009-04-03 13:40:49 -07002377 * 1) wakeup on uaddr2 after an atomic lock acquisition by futex_requeue()
Darren Hartcc6db4e2009-07-31 16:20:10 -07002378 * 2) wakeup on uaddr2 after a requeue
2379 * 3) signal
2380 * 4) timeout
Darren Hart52400ba2009-04-03 13:40:49 -07002381 *
Darren Hartcc6db4e2009-07-31 16:20:10 -07002382 * If 3, cleanup and return -ERESTARTNOINTR.
Darren Hart52400ba2009-04-03 13:40:49 -07002383 *
2384 * If 2, we may then block on trying to take the rt_mutex and return via:
2385 * 5) successful lock
2386 * 6) signal
2387 * 7) timeout
2388 * 8) other lock acquisition failure
2389 *
Darren Hartcc6db4e2009-07-31 16:20:10 -07002390 * If 6, return -EWOULDBLOCK (restarting the syscall would do the same).
Darren Hart52400ba2009-04-03 13:40:49 -07002391 *
2392 * If 4 or 7, we cleanup and return with -ETIMEDOUT.
2393 *
Randy Dunlap6c23cbb2013-03-05 10:00:24 -08002394 * Return:
2395 * 0 - On success;
Darren Hart52400ba2009-04-03 13:40:49 -07002396 * <0 - On error
2397 */
Darren Hartb41277d2010-11-08 13:10:09 -08002398static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
Darren Hart52400ba2009-04-03 13:40:49 -07002399 u32 val, ktime_t *abs_time, u32 bitset,
Darren Hartb41277d2010-11-08 13:10:09 -08002400 u32 __user *uaddr2)
Darren Hart52400ba2009-04-03 13:40:49 -07002401{
2402 struct hrtimer_sleeper timeout, *to = NULL;
2403 struct rt_mutex_waiter rt_waiter;
2404 struct rt_mutex *pi_mutex = NULL;
Darren Hart52400ba2009-04-03 13:40:49 -07002405 struct futex_hash_bucket *hb;
Darren Hart5bdb05f2010-11-08 13:40:28 -08002406 union futex_key key2 = FUTEX_KEY_INIT;
2407 struct futex_q q = futex_q_init;
Darren Hart52400ba2009-04-03 13:40:49 -07002408 int res, ret;
Darren Hart52400ba2009-04-03 13:40:49 -07002409
Darren Hart6f7b0a22012-07-20 11:53:31 -07002410 if (uaddr == uaddr2)
2411 return -EINVAL;
2412
Darren Hart52400ba2009-04-03 13:40:49 -07002413 if (!bitset)
2414 return -EINVAL;
2415
2416 if (abs_time) {
2417 to = &timeout;
Darren Hartb41277d2010-11-08 13:10:09 -08002418 hrtimer_init_on_stack(&to->timer, (flags & FLAGS_CLOCKRT) ?
2419 CLOCK_REALTIME : CLOCK_MONOTONIC,
2420 HRTIMER_MODE_ABS);
Darren Hart52400ba2009-04-03 13:40:49 -07002421 hrtimer_init_sleeper(to, current);
2422 hrtimer_set_expires_range_ns(&to->timer, *abs_time,
2423 current->timer_slack_ns);
2424 }
2425
2426 /*
2427 * The waiter is allocated on our stack, manipulated by the requeue
2428 * code while we sleep on uaddr.
2429 */
2430 debug_rt_mutex_init_waiter(&rt_waiter);
Peter Zijlstrafb00aca2013-11-07 14:43:43 +01002431 RB_CLEAR_NODE(&rt_waiter.pi_tree_entry);
2432 RB_CLEAR_NODE(&rt_waiter.tree_entry);
Darren Hart52400ba2009-04-03 13:40:49 -07002433 rt_waiter.task = NULL;
2434
Shawn Bohrer9ea71502011-06-30 11:21:32 -05002435 ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE);
Darren Hart52400ba2009-04-03 13:40:49 -07002436 if (unlikely(ret != 0))
2437 goto out;
2438
Darren Hart84bc4af2009-08-13 17:36:53 -07002439 q.bitset = bitset;
2440 q.rt_waiter = &rt_waiter;
2441 q.requeue_pi_key = &key2;
2442
Darren Hart7ada8762010-10-17 08:35:04 -07002443 /*
2444 * Prepare to wait on uaddr. On success, increments q.key (key1) ref
2445 * count.
2446 */
Darren Hartb41277d2010-11-08 13:10:09 -08002447 ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
Thomas Gleixnerc8b15a72009-05-20 09:18:50 +02002448 if (ret)
2449 goto out_key2;
Darren Hart52400ba2009-04-03 13:40:49 -07002450
2451 /* Queue the futex_q, drop the hb lock, wait for wakeup. */
Thomas Gleixnerf1a11e02009-05-05 19:21:40 +02002452 futex_wait_queue_me(hb, &q, to);
Darren Hart52400ba2009-04-03 13:40:49 -07002453
2454 spin_lock(&hb->lock);
2455 ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
2456 spin_unlock(&hb->lock);
2457 if (ret)
2458 goto out_put_keys;
2459
2460 /*
2461 * In order for us to be here, we know our q.key == key2, and since
2462 * we took the hb->lock above, we also know that futex_requeue() has
2463 * completed and we no longer have to concern ourselves with a wakeup
Darren Hart7ada8762010-10-17 08:35:04 -07002464 * race with the atomic proxy lock acquisition by the requeue code. The
2465 * futex_requeue dropped our key1 reference and incremented our key2
2466 * reference count.
Darren Hart52400ba2009-04-03 13:40:49 -07002467 */
2468
2469 /* Check if the requeue code acquired the second futex for us. */
2470 if (!q.rt_waiter) {
2471 /*
2472 * Got the lock. We might not be the anticipated owner if we
2473 * did a lock-steal - fix up the PI-state in that case.
2474 */
2475 if (q.pi_state && (q.pi_state->owner != current)) {
2476 spin_lock(q.lock_ptr);
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002477 ret = fixup_pi_state_owner(uaddr2, &q, current);
Darren Hart52400ba2009-04-03 13:40:49 -07002478 spin_unlock(q.lock_ptr);
2479 }
2480 } else {
2481 /*
2482 * We have been woken up by futex_unlock_pi(), a timeout, or a
2483 * signal. futex_unlock_pi() will not destroy the lock_ptr nor
2484 * the pi_state.
2485 */
Darren Hartf27071c2012-07-20 11:53:30 -07002486 WARN_ON(!q.pi_state);
Darren Hart52400ba2009-04-03 13:40:49 -07002487 pi_mutex = &q.pi_state->pi_mutex;
2488 ret = rt_mutex_finish_proxy_lock(pi_mutex, to, &rt_waiter, 1);
2489 debug_rt_mutex_free_waiter(&rt_waiter);
2490
2491 spin_lock(q.lock_ptr);
2492 /*
2493 * Fixup the pi_state owner and possibly acquire the lock if we
2494 * haven't already.
2495 */
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002496 res = fixup_owner(uaddr2, &q, !ret);
Darren Hart52400ba2009-04-03 13:40:49 -07002497 /*
2498 * If fixup_owner() returned an error, proprogate that. If it
Darren Hart56ec1602009-09-21 22:29:59 -07002499 * acquired the lock, clear -ETIMEDOUT or -EINTR.
Darren Hart52400ba2009-04-03 13:40:49 -07002500 */
2501 if (res)
2502 ret = (res < 0) ? res : 0;
2503
2504 /* Unqueue and drop the lock. */
2505 unqueue_me_pi(&q);
2506 }
2507
2508 /*
2509 * If fixup_pi_state_owner() faulted and was unable to handle the
2510 * fault, unlock the rt_mutex and return the fault to userspace.
2511 */
2512 if (ret == -EFAULT) {
Darren Hartb6070a82012-07-20 11:53:29 -07002513 if (pi_mutex && rt_mutex_owner(pi_mutex) == current)
Darren Hart52400ba2009-04-03 13:40:49 -07002514 rt_mutex_unlock(pi_mutex);
2515 } else if (ret == -EINTR) {
Darren Hart52400ba2009-04-03 13:40:49 -07002516 /*
Darren Hartcc6db4e2009-07-31 16:20:10 -07002517 * We've already been requeued, but cannot restart by calling
2518 * futex_lock_pi() directly. We could restart this syscall, but
2519 * it would detect that the user space "val" changed and return
2520 * -EWOULDBLOCK. Save the overhead of the restart and return
2521 * -EWOULDBLOCK directly.
Darren Hart52400ba2009-04-03 13:40:49 -07002522 */
Thomas Gleixner20708872009-05-19 23:04:59 +02002523 ret = -EWOULDBLOCK;
Darren Hart52400ba2009-04-03 13:40:49 -07002524 }
2525
2526out_put_keys:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002527 put_futex_key(&q.key);
Thomas Gleixnerc8b15a72009-05-20 09:18:50 +02002528out_key2:
Thomas Gleixnerae791a22010-11-10 13:30:36 +01002529 put_futex_key(&key2);
Darren Hart52400ba2009-04-03 13:40:49 -07002530
2531out:
2532 if (to) {
2533 hrtimer_cancel(&to->timer);
2534 destroy_hrtimer_on_stack(&to->timer);
2535 }
2536 return ret;
2537}
2538
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002539/*
2540 * Support for robust futexes: the kernel cleans up held futexes at
2541 * thread exit time.
2542 *
2543 * Implementation: user-space maintains a per-thread list of locks it
2544 * is holding. Upon do_exit(), the kernel carefully walks this list,
2545 * and marks all locks that are owned by this thread with the
Ingo Molnarc87e2832006-06-27 02:54:58 -07002546 * FUTEX_OWNER_DIED bit, and wakes up a waiter (if any). The list is
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002547 * always manipulated with the lock held, so the list is private and
2548 * per-thread. Userspace also maintains a per-thread 'list_op_pending'
2549 * field, to allow the kernel to clean up if the thread dies after
2550 * acquiring the lock, but just before it could have added itself to
2551 * the list. There can only be one such pending lock.
2552 */
2553
2554/**
Darren Hartd96ee562009-09-21 22:30:22 -07002555 * sys_set_robust_list() - Set the robust-futex list head of a task
2556 * @head: pointer to the list-head
2557 * @len: length of the list-head, as userspace expects
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002558 */
Heiko Carstens836f92a2009-01-14 14:14:33 +01002559SYSCALL_DEFINE2(set_robust_list, struct robust_list_head __user *, head,
2560 size_t, len)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002561{
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08002562 if (!futex_cmpxchg_enabled)
2563 return -ENOSYS;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002564 /*
2565 * The kernel knows only one size for now:
2566 */
2567 if (unlikely(len != sizeof(*head)))
2568 return -EINVAL;
2569
2570 current->robust_list = head;
2571
2572 return 0;
2573}
2574
2575/**
Darren Hartd96ee562009-09-21 22:30:22 -07002576 * sys_get_robust_list() - Get the robust-futex list head of a task
2577 * @pid: pid of the process [zero for current task]
2578 * @head_ptr: pointer to a list-head pointer, the kernel fills it in
2579 * @len_ptr: pointer to a length field, the kernel fills in the header size
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002580 */
Heiko Carstens836f92a2009-01-14 14:14:33 +01002581SYSCALL_DEFINE3(get_robust_list, int, pid,
2582 struct robust_list_head __user * __user *, head_ptr,
2583 size_t __user *, len_ptr)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002584{
Al Viroba46df92006-10-10 22:46:07 +01002585 struct robust_list_head __user *head;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002586 unsigned long ret;
Kees Cookbdbb7762012-03-19 16:12:53 -07002587 struct task_struct *p;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002588
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08002589 if (!futex_cmpxchg_enabled)
2590 return -ENOSYS;
2591
Kees Cookbdbb7762012-03-19 16:12:53 -07002592 rcu_read_lock();
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002593
Kees Cookbdbb7762012-03-19 16:12:53 -07002594 ret = -ESRCH;
2595 if (!pid)
2596 p = current;
2597 else {
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07002598 p = find_task_by_vpid(pid);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002599 if (!p)
2600 goto err_unlock;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002601 }
2602
Kees Cookbdbb7762012-03-19 16:12:53 -07002603 ret = -EPERM;
2604 if (!ptrace_may_access(p, PTRACE_MODE_READ))
2605 goto err_unlock;
2606
2607 head = p->robust_list;
2608 rcu_read_unlock();
2609
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002610 if (put_user(sizeof(*head), len_ptr))
2611 return -EFAULT;
2612 return put_user(head, head_ptr);
2613
2614err_unlock:
Oleg Nesterovaaa2a972006-09-29 02:00:55 -07002615 rcu_read_unlock();
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002616
2617 return ret;
2618}
2619
2620/*
2621 * Process a futex-list entry, check whether it's owned by the
2622 * dying task, and do notification if so:
2623 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002624int handle_futex_death(u32 __user *uaddr, struct task_struct *curr, int pi)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002625{
Vitaliy Ivanov7cfdaf32011-07-07 15:10:31 +03002626 u32 uval, uninitialized_var(nval), mval;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002627
Ingo Molnar8f17d3a2006-03-27 01:16:27 -08002628retry:
2629 if (get_user(uval, uaddr))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002630 return -1;
2631
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002632 if ((uval & FUTEX_TID_MASK) == task_pid_vnr(curr)) {
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002633 /*
2634 * Ok, this dying thread is truly holding a futex
2635 * of interest. Set the OWNER_DIED bit atomically
2636 * via cmpxchg, and if the value had FUTEX_WAITERS
2637 * set, wake up a waiter (if any). (We have to do a
2638 * futex_wake() even if OWNER_DIED is already set -
2639 * to handle the rare but possible case of recursive
2640 * thread-death.) The rest of the cleanup is done in
2641 * userspace.
2642 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002643 mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
Thomas Gleixner6e0aa9f2011-03-14 10:34:35 +01002644 /*
2645 * We are not holding a lock here, but we want to have
2646 * the pagefault_disable/enable() protection because
2647 * we want to handle the fault gracefully. If the
2648 * access fails we try to fault in the futex with R/W
2649 * verification via get_user_pages. get_user() above
2650 * does not guarantee R/W access. If that fails we
2651 * give up and leave the futex locked.
2652 */
2653 if (cmpxchg_futex_value_locked(&nval, uaddr, uval, mval)) {
2654 if (fault_in_user_writeable(uaddr))
2655 return -1;
2656 goto retry;
2657 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07002658 if (nval != uval)
Ingo Molnar8f17d3a2006-03-27 01:16:27 -08002659 goto retry;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002660
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002661 /*
2662 * Wake robust non-PI futexes here. The wakeup of
2663 * PI futexes happens in exit_pi_state():
2664 */
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07002665 if (!pi && (uval & FUTEX_WAITERS))
Peter Zijlstrac2f9f202008-09-26 19:32:23 +02002666 futex_wake(uaddr, 1, 1, FUTEX_BITSET_MATCH_ANY);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002667 }
2668 return 0;
2669}
2670
2671/*
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002672 * Fetch a robust-list pointer. Bit 0 signals PI futexes:
2673 */
2674static inline int fetch_robust_entry(struct robust_list __user **entry,
Al Viroba46df92006-10-10 22:46:07 +01002675 struct robust_list __user * __user *head,
Namhyung Kim1dcc41b2010-09-14 21:43:46 +09002676 unsigned int *pi)
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002677{
2678 unsigned long uentry;
2679
Al Viroba46df92006-10-10 22:46:07 +01002680 if (get_user(uentry, (unsigned long __user *)head))
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002681 return -EFAULT;
2682
Al Viroba46df92006-10-10 22:46:07 +01002683 *entry = (void __user *)(uentry & ~1UL);
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002684 *pi = uentry & 1;
2685
2686 return 0;
2687}
2688
2689/*
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002690 * Walk curr->robust_list (very carefully, it's a userspace list!)
2691 * and mark any locks found there dead, and notify any waiters.
2692 *
2693 * We silently return on any sign of list-walking problem.
2694 */
2695void exit_robust_list(struct task_struct *curr)
2696{
2697 struct robust_list_head __user *head = curr->robust_list;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07002698 struct robust_list __user *entry, *next_entry, *pending;
Darren Hart4c115e92010-11-04 15:00:00 -04002699 unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
2700 unsigned int uninitialized_var(next_pi);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002701 unsigned long futex_offset;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07002702 int rc;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002703
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08002704 if (!futex_cmpxchg_enabled)
2705 return;
2706
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002707 /*
2708 * Fetch the list head (which was registered earlier, via
2709 * sys_set_robust_list()):
2710 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002711 if (fetch_robust_entry(&entry, &head->list.next, &pi))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002712 return;
2713 /*
2714 * Fetch the relative futex offset:
2715 */
2716 if (get_user(futex_offset, &head->futex_offset))
2717 return;
2718 /*
2719 * Fetch any possibly pending lock-add first, and handle it
2720 * if it exists:
2721 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002722 if (fetch_robust_entry(&pending, &head->list_op_pending, &pip))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002723 return;
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002724
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07002725 next_entry = NULL; /* avoid warning with gcc */
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002726 while (entry != &head->list) {
2727 /*
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07002728 * Fetch the next entry in the list before calling
2729 * handle_futex_death:
2730 */
2731 rc = fetch_robust_entry(&next_entry, &entry->next, &next_pi);
2732 /*
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002733 * A pending lock might already be on the list, so
Ingo Molnarc87e2832006-06-27 02:54:58 -07002734 * don't process it twice:
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002735 */
2736 if (entry != pending)
Al Viroba46df92006-10-10 22:46:07 +01002737 if (handle_futex_death((void __user *)entry + futex_offset,
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002738 curr, pi))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002739 return;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07002740 if (rc)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002741 return;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07002742 entry = next_entry;
2743 pi = next_pi;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002744 /*
2745 * Avoid excessively long or circular lists:
2746 */
2747 if (!--limit)
2748 break;
2749
2750 cond_resched();
2751 }
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07002752
2753 if (pending)
2754 handle_futex_death((void __user *)pending + futex_offset,
2755 curr, pip);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002756}
2757
Pierre Peifferc19384b2007-05-09 02:35:02 -07002758long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
Ingo Molnare2970f22006-06-27 02:54:47 -07002759 u32 __user *uaddr2, u32 val2, u32 val3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760{
Thomas Gleixner81b40532012-02-15 12:17:09 +01002761 int cmd = op & FUTEX_CMD_MASK;
Darren Hartb41277d2010-11-08 13:10:09 -08002762 unsigned int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002764 if (!(op & FUTEX_PRIVATE_FLAG))
Darren Hartb41277d2010-11-08 13:10:09 -08002765 flags |= FLAGS_SHARED;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002766
Darren Hartb41277d2010-11-08 13:10:09 -08002767 if (op & FUTEX_CLOCK_REALTIME) {
2768 flags |= FLAGS_CLOCKRT;
2769 if (cmd != FUTEX_WAIT_BITSET && cmd != FUTEX_WAIT_REQUEUE_PI)
2770 return -ENOSYS;
2771 }
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002772
2773 switch (cmd) {
Thomas Gleixner59263b52012-02-15 12:08:34 +01002774 case FUTEX_LOCK_PI:
2775 case FUTEX_UNLOCK_PI:
2776 case FUTEX_TRYLOCK_PI:
2777 case FUTEX_WAIT_REQUEUE_PI:
2778 case FUTEX_CMP_REQUEUE_PI:
2779 if (!futex_cmpxchg_enabled)
2780 return -ENOSYS;
2781 }
2782
2783 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 case FUTEX_WAIT:
Thomas Gleixnercd689982008-02-01 17:45:14 +01002785 val3 = FUTEX_BITSET_MATCH_ANY;
2786 case FUTEX_WAIT_BITSET:
Thomas Gleixner81b40532012-02-15 12:17:09 +01002787 return futex_wait(uaddr, flags, val, timeout, val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 case FUTEX_WAKE:
Thomas Gleixnercd689982008-02-01 17:45:14 +01002789 val3 = FUTEX_BITSET_MATCH_ANY;
2790 case FUTEX_WAKE_BITSET:
Thomas Gleixner81b40532012-02-15 12:17:09 +01002791 return futex_wake(uaddr, flags, val, val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 case FUTEX_REQUEUE:
Thomas Gleixner81b40532012-02-15 12:17:09 +01002793 return futex_requeue(uaddr, flags, uaddr2, val, val2, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 case FUTEX_CMP_REQUEUE:
Thomas Gleixner81b40532012-02-15 12:17:09 +01002795 return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 0);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07002796 case FUTEX_WAKE_OP:
Thomas Gleixner81b40532012-02-15 12:17:09 +01002797 return futex_wake_op(uaddr, flags, uaddr2, val, val2, val3);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002798 case FUTEX_LOCK_PI:
Thomas Gleixner81b40532012-02-15 12:17:09 +01002799 return futex_lock_pi(uaddr, flags, val, timeout, 0);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002800 case FUTEX_UNLOCK_PI:
Thomas Gleixner81b40532012-02-15 12:17:09 +01002801 return futex_unlock_pi(uaddr, flags);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002802 case FUTEX_TRYLOCK_PI:
Thomas Gleixner81b40532012-02-15 12:17:09 +01002803 return futex_lock_pi(uaddr, flags, 0, timeout, 1);
Darren Hart52400ba2009-04-03 13:40:49 -07002804 case FUTEX_WAIT_REQUEUE_PI:
2805 val3 = FUTEX_BITSET_MATCH_ANY;
Thomas Gleixner81b40532012-02-15 12:17:09 +01002806 return futex_wait_requeue_pi(uaddr, flags, val, timeout, val3,
2807 uaddr2);
Darren Hart52400ba2009-04-03 13:40:49 -07002808 case FUTEX_CMP_REQUEUE_PI:
Thomas Gleixner81b40532012-02-15 12:17:09 +01002809 return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 }
Thomas Gleixner81b40532012-02-15 12:17:09 +01002811 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812}
2813
2814
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002815SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val,
2816 struct timespec __user *, utime, u32 __user *, uaddr2,
2817 u32, val3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818{
Pierre Peifferc19384b2007-05-09 02:35:02 -07002819 struct timespec ts;
2820 ktime_t t, *tp = NULL;
Ingo Molnare2970f22006-06-27 02:54:47 -07002821 u32 val2 = 0;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002822 int cmd = op & FUTEX_CMD_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Thomas Gleixnercd689982008-02-01 17:45:14 +01002824 if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI ||
Darren Hart52400ba2009-04-03 13:40:49 -07002825 cmd == FUTEX_WAIT_BITSET ||
2826 cmd == FUTEX_WAIT_REQUEUE_PI)) {
Pierre Peifferc19384b2007-05-09 02:35:02 -07002827 if (copy_from_user(&ts, utime, sizeof(ts)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 return -EFAULT;
Pierre Peifferc19384b2007-05-09 02:35:02 -07002829 if (!timespec_valid(&ts))
Thomas Gleixner9741ef962006-03-31 02:31:32 -08002830 return -EINVAL;
Pierre Peifferc19384b2007-05-09 02:35:02 -07002831
2832 t = timespec_to_ktime(ts);
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002833 if (cmd == FUTEX_WAIT)
Thomas Gleixner5a7780e2008-02-13 09:20:43 +01002834 t = ktime_add_safe(ktime_get(), t);
Pierre Peifferc19384b2007-05-09 02:35:02 -07002835 tp = &t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 }
2837 /*
Darren Hart52400ba2009-04-03 13:40:49 -07002838 * requeue parameter in 'utime' if cmd == FUTEX_*_REQUEUE_*.
Andreas Schwabf54f0982007-07-31 00:38:51 -07002839 * number of waiters to wake in 'utime' if cmd == FUTEX_WAKE_OP.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 */
Andreas Schwabf54f0982007-07-31 00:38:51 -07002841 if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE ||
Darren Hartba9c22f2009-04-20 22:22:22 -07002842 cmd == FUTEX_CMP_REQUEUE_PI || cmd == FUTEX_WAKE_OP)
Ingo Molnare2970f22006-06-27 02:54:47 -07002843 val2 = (u32) (unsigned long) utime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844
Pierre Peifferc19384b2007-05-09 02:35:02 -07002845 return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846}
2847
Heiko Carstens03b8c7b2014-03-02 13:09:47 +01002848static void __init futex_detect_cmpxchg(void)
2849{
2850#ifndef CONFIG_HAVE_FUTEX_CMPXCHG
2851 u32 curval;
2852
2853 /*
2854 * This will fail and we want it. Some arch implementations do
2855 * runtime detection of the futex_atomic_cmpxchg_inatomic()
2856 * functionality. We want to know that before we call in any
2857 * of the complex code paths. Also we want to prevent
2858 * registration of robust lists in that case. NULL is
2859 * guaranteed to fault and we get -EFAULT on functional
2860 * implementation, the non-functional ones will return
2861 * -ENOSYS.
2862 */
2863 if (cmpxchg_futex_value_locked(&curval, NULL, 0, 0) == -EFAULT)
2864 futex_cmpxchg_enabled = 1;
2865#endif
2866}
2867
Benjamin Herrenschmidtf6d107f2008-03-27 14:52:15 +11002868static int __init futex_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869{
Heiko Carstens63b1a812014-01-16 14:54:50 +01002870 unsigned int futex_shift;
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -08002871 unsigned long i;
2872
2873#if CONFIG_BASE_SMALL
2874 futex_hashsize = 16;
2875#else
2876 futex_hashsize = roundup_pow_of_two(256 * num_possible_cpus());
2877#endif
2878
2879 futex_queues = alloc_large_system_hash("futex", sizeof(*futex_queues),
2880 futex_hashsize, 0,
2881 futex_hashsize < 256 ? HASH_SMALL : 0,
Heiko Carstens63b1a812014-01-16 14:54:50 +01002882 &futex_shift, NULL,
2883 futex_hashsize, futex_hashsize);
2884 futex_hashsize = 1UL << futex_shift;
Heiko Carstens03b8c7b2014-03-02 13:09:47 +01002885
2886 futex_detect_cmpxchg();
Thomas Gleixnera0c1e902008-02-23 15:23:57 -08002887
Davidlohr Buesoa52b89e2014-01-12 15:31:23 -08002888 for (i = 0; i < futex_hashsize; i++) {
Dima Zavin732375c2011-07-07 17:27:59 -07002889 plist_head_init(&futex_queues[i].chain);
Thomas Gleixner3e4ab742008-02-23 15:23:55 -08002890 spin_lock_init(&futex_queues[i].lock);
2891 }
2892
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 return 0;
2894}
Benjamin Herrenschmidtf6d107f2008-03-27 14:52:15 +11002895__initcall(futex_init);