blob: 86b2600381b616db0d157ac9e172a3fc55315be4 [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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * Thanks to Ben LaHaise for yelling "hashed waitqueues" loudly
23 * enough at me, Linus for the original (flawed) idea, Matthew
24 * Kirkwood for proof-of-concept implementation.
25 *
26 * "The futexes are also cursed."
27 * "But they come in a choice of three flavours!"
28 *
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
33 *
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
38 *
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
42 */
43#include <linux/slab.h>
44#include <linux/poll.h>
45#include <linux/fs.h>
46#include <linux/file.h>
47#include <linux/jhash.h>
48#include <linux/init.h>
49#include <linux/futex.h>
50#include <linux/mount.h>
51#include <linux/pagemap.h>
52#include <linux/syscalls.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070053#include <linux/signal.h>
Rusty Russell9adef582007-05-08 00:26:42 -070054#include <linux/module.h>
Andrey Mirkinfd5eea42007-10-16 23:30:13 -070055#include <linux/magic.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070056#include <linux/pid.h>
57#include <linux/nsproxy.h>
58
Jakub Jelinek4732efb2005-09-06 15:16:25 -070059#include <asm/futex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Ingo Molnarc87e2832006-06-27 02:54:58 -070061#include "rtmutex_common.h"
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#define FUTEX_HASHBITS (CONFIG_BASE_SMALL ? 4 : 8)
64
65/*
Ingo Molnarc87e2832006-06-27 02:54:58 -070066 * Priority Inheritance state:
67 */
68struct futex_pi_state {
69 /*
70 * list of 'owned' pi_state instances - these have to be
71 * cleaned up in do_exit() if the task exits prematurely:
72 */
73 struct list_head list;
74
75 /*
76 * The PI object:
77 */
78 struct rt_mutex pi_mutex;
79
80 struct task_struct *owner;
81 atomic_t refcount;
82
83 union futex_key key;
84};
85
86/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * We use this hashed waitqueue instead of a normal wait_queue_t, so
88 * we can wake only the relevant ones (hashed queues may be shared).
89 *
90 * A futex_q has a woken state, just like tasks have TASK_RUNNING.
Pierre Peifferec92d082007-05-09 02:35:00 -070091 * It is considered woken when plist_node_empty(&q->list) || q->lock_ptr == 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * The order of wakup is always to make the first condition true, then
93 * wake up q->waiters, then make the second condition true.
94 */
95struct futex_q {
Pierre Peifferec92d082007-05-09 02:35:00 -070096 struct plist_node list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 wait_queue_head_t waiters;
98
Ingo Molnare2970f22006-06-27 02:54:47 -070099 /* Which hash list lock to use: */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 spinlock_t *lock_ptr;
101
Ingo Molnare2970f22006-06-27 02:54:47 -0700102 /* Key which the futex is hashed on: */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 union futex_key key;
104
Ingo Molnare2970f22006-06-27 02:54:47 -0700105 /* For fd, sigio sent using these: */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 int fd;
107 struct file *filp;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700108
109 /* Optional priority inheritance state: */
110 struct futex_pi_state *pi_state;
111 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112};
113
114/*
115 * Split the global futex_lock into every hash list lock.
116 */
117struct futex_hash_bucket {
Pierre Peifferec92d082007-05-09 02:35:00 -0700118 spinlock_t lock;
119 struct plist_head chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120};
121
122static struct futex_hash_bucket futex_queues[1<<FUTEX_HASHBITS];
123
124/* Futex-fs vfsmount entry: */
125static struct vfsmount *futex_mnt;
126
127/*
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700128 * Take mm->mmap_sem, when futex is shared
129 */
130static inline void futex_lock_mm(struct rw_semaphore *fshared)
131{
132 if (fshared)
133 down_read(fshared);
134}
135
136/*
137 * Release mm->mmap_sem, when the futex is shared
138 */
139static inline void futex_unlock_mm(struct rw_semaphore *fshared)
140{
141 if (fshared)
142 up_read(fshared);
143}
144
145/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 * We hash on the keys returned from get_futex_key (see below).
147 */
148static struct futex_hash_bucket *hash_futex(union futex_key *key)
149{
150 u32 hash = jhash2((u32*)&key->both.word,
151 (sizeof(key->both.word)+sizeof(key->both.ptr))/4,
152 key->both.offset);
153 return &futex_queues[hash & ((1 << FUTEX_HASHBITS)-1)];
154}
155
156/*
157 * Return 1 if two futex_keys are equal, 0 otherwise.
158 */
159static inline int match_futex(union futex_key *key1, union futex_key *key2)
160{
161 return (key1->both.word == key2->both.word
162 && key1->both.ptr == key2->both.ptr
163 && key1->both.offset == key2->both.offset);
164}
165
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700166/**
167 * get_futex_key - Get parameters which are the keys for a futex.
168 * @uaddr: virtual address of the futex
169 * @shared: NULL for a PROCESS_PRIVATE futex,
170 * &current->mm->mmap_sem for a PROCESS_SHARED futex
171 * @key: address where result is stored.
172 *
173 * Returns a negative error code or 0
174 * The key words are stored in *key on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 *
Josef "Jeff" Sipekf3a43f32006-12-08 02:36:43 -0800176 * For shared mappings, it's (page->index, vma->vm_file->f_path.dentry->d_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 * offset_within_page). For private mappings, it's (uaddr, current->mm).
178 * We can usually work out the index without swapping in the page.
179 *
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700180 * fshared is NULL for PROCESS_PRIVATE futexes
181 * For other futexes, it points to &current->mm->mmap_sem and
182 * caller must have taken the reader lock. but NOT any spinlocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700184int get_futex_key(u32 __user *uaddr, struct rw_semaphore *fshared,
185 union futex_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Ingo Molnare2970f22006-06-27 02:54:47 -0700187 unsigned long address = (unsigned long)uaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 struct mm_struct *mm = current->mm;
189 struct vm_area_struct *vma;
190 struct page *page;
191 int err;
192
193 /*
194 * The futex address must be "naturally" aligned.
195 */
Ingo Molnare2970f22006-06-27 02:54:47 -0700196 key->both.offset = address % PAGE_SIZE;
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700197 if (unlikely((address % sizeof(u32)) != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return -EINVAL;
Ingo Molnare2970f22006-06-27 02:54:47 -0700199 address -= key->both.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 /*
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700202 * PROCESS_PRIVATE futexes are fast.
203 * As the mm cannot disappear under us and the 'key' only needs
204 * virtual address, we dont even have to find the underlying vma.
205 * Note : We do have to check 'uaddr' is a valid user address,
206 * but access_ok() should be faster than find_vma()
207 */
208 if (!fshared) {
209 if (unlikely(!access_ok(VERIFY_WRITE, uaddr, sizeof(u32))))
210 return -EFAULT;
211 key->private.mm = mm;
212 key->private.address = address;
213 return 0;
214 }
215 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 * The futex is hashed differently depending on whether
217 * it's in a shared or private mapping. So check vma first.
218 */
Ingo Molnare2970f22006-06-27 02:54:47 -0700219 vma = find_extend_vma(mm, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (unlikely(!vma))
221 return -EFAULT;
222
223 /*
224 * Permissions.
225 */
226 if (unlikely((vma->vm_flags & (VM_IO|VM_READ)) != VM_READ))
227 return (vma->vm_flags & VM_IO) ? -EPERM : -EACCES;
228
229 /*
230 * Private mappings are handled in a simple way.
231 *
232 * NOTE: When userspace waits on a MAP_SHARED mapping, even if
233 * it's a read-only handle, it's expected that futexes attach to
234 * the object not the particular process. Therefore we use
235 * VM_MAYSHARE here, not VM_SHARED which is restricted to shared
236 * mappings of _writable_ handles.
237 */
238 if (likely(!(vma->vm_flags & VM_MAYSHARE))) {
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700239 key->both.offset |= FUT_OFF_MMSHARED; /* reference taken on mm */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 key->private.mm = mm;
Ingo Molnare2970f22006-06-27 02:54:47 -0700241 key->private.address = address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return 0;
243 }
244
245 /*
246 * Linear file mappings are also simple.
247 */
Josef "Jeff" Sipekf3a43f32006-12-08 02:36:43 -0800248 key->shared.inode = vma->vm_file->f_path.dentry->d_inode;
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700249 key->both.offset |= FUT_OFF_INODE; /* inode-based key. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (likely(!(vma->vm_flags & VM_NONLINEAR))) {
Ingo Molnare2970f22006-06-27 02:54:47 -0700251 key->shared.pgoff = (((address - vma->vm_start) >> PAGE_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 + vma->vm_pgoff);
253 return 0;
254 }
255
256 /*
257 * We could walk the page table to read the non-linear
258 * pte, and get the page index without fetching the page
259 * from swap. But that's a lot of code to duplicate here
260 * for a rare case, so we simply fetch the page.
261 */
Ingo Molnare2970f22006-06-27 02:54:47 -0700262 err = get_user_pages(current, mm, address, 1, 0, 0, &page, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (err >= 0) {
264 key->shared.pgoff =
265 page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
266 put_page(page);
267 return 0;
268 }
269 return err;
270}
Rusty Russell9adef582007-05-08 00:26:42 -0700271EXPORT_SYMBOL_GPL(get_futex_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273/*
274 * Take a reference to the resource addressed by a key.
275 * Can be called while holding spinlocks.
276 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 */
Rusty Russell9adef582007-05-08 00:26:42 -0700278inline void get_futex_key_refs(union futex_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700280 if (key->both.ptr == 0)
281 return;
282 switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
283 case FUT_OFF_INODE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 atomic_inc(&key->shared.inode->i_count);
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700285 break;
286 case FUT_OFF_MMSHARED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 atomic_inc(&key->private.mm->mm_count);
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700288 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290}
Rusty Russell9adef582007-05-08 00:26:42 -0700291EXPORT_SYMBOL_GPL(get_futex_key_refs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293/*
294 * Drop a reference to the resource addressed by a key.
295 * The hash bucket spinlock must not be held.
296 */
Rusty Russell9adef582007-05-08 00:26:42 -0700297void drop_futex_key_refs(union futex_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Stephen Hemmingerc80544d2007-10-18 03:07:05 -0700299 if (!key->both.ptr)
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700300 return;
301 switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
302 case FUT_OFF_INODE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 iput(key->shared.inode);
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700304 break;
305 case FUT_OFF_MMSHARED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 mmdrop(key->private.mm);
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700307 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
309}
Rusty Russell9adef582007-05-08 00:26:42 -0700310EXPORT_SYMBOL_GPL(drop_futex_key_refs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700312static u32 cmpxchg_futex_value_locked(u32 __user *uaddr, u32 uval, u32 newval)
313{
314 u32 curval;
315
316 pagefault_disable();
317 curval = futex_atomic_cmpxchg_inatomic(uaddr, uval, newval);
318 pagefault_enable();
319
320 return curval;
321}
322
323static int get_futex_value_locked(u32 *dest, u32 __user *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 int ret;
326
Peter Zijlstraa8663742006-12-06 20:32:20 -0800327 pagefault_disable();
Ingo Molnare2970f22006-06-27 02:54:47 -0700328 ret = __copy_from_user_inatomic(dest, from, sizeof(u32));
Peter Zijlstraa8663742006-12-06 20:32:20 -0800329 pagefault_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 return ret ? -EFAULT : 0;
332}
333
334/*
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700335 * Fault handling.
336 * if fshared is non NULL, current->mm->mmap_sem is already held
Ingo Molnarc87e2832006-06-27 02:54:58 -0700337 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700338static int futex_handle_fault(unsigned long address,
339 struct rw_semaphore *fshared, int attempt)
Ingo Molnarc87e2832006-06-27 02:54:58 -0700340{
341 struct vm_area_struct * vma;
342 struct mm_struct *mm = current->mm;
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700343 int ret = -EFAULT;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700344
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700345 if (attempt > 2)
346 return ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700347
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700348 if (!fshared)
349 down_read(&mm->mmap_sem);
350 vma = find_vma(mm, address);
351 if (vma && address >= vma->vm_start &&
352 (vma->vm_flags & VM_WRITE)) {
Nick Piggin83c54072007-07-19 01:47:05 -0700353 int fault;
354 fault = handle_mm_fault(mm, vma, address, 1);
355 if (unlikely((fault & VM_FAULT_ERROR))) {
356#if 0
357 /* XXX: let's do this when we verify it is OK */
358 if (ret & VM_FAULT_OOM)
359 ret = -ENOMEM;
360#endif
361 } else {
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700362 ret = 0;
Nick Piggin83c54072007-07-19 01:47:05 -0700363 if (fault & VM_FAULT_MAJOR)
364 current->maj_flt++;
365 else
366 current->min_flt++;
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700367 }
Ingo Molnarc87e2832006-06-27 02:54:58 -0700368 }
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700369 if (!fshared)
370 up_read(&mm->mmap_sem);
371 return ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700372}
373
374/*
375 * PI code:
376 */
377static int refill_pi_state_cache(void)
378{
379 struct futex_pi_state *pi_state;
380
381 if (likely(current->pi_state_cache))
382 return 0;
383
Burman Yan4668edc2006-12-06 20:38:51 -0800384 pi_state = kzalloc(sizeof(*pi_state), GFP_KERNEL);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700385
386 if (!pi_state)
387 return -ENOMEM;
388
Ingo Molnarc87e2832006-06-27 02:54:58 -0700389 INIT_LIST_HEAD(&pi_state->list);
390 /* pi_mutex gets initialized later */
391 pi_state->owner = NULL;
392 atomic_set(&pi_state->refcount, 1);
393
394 current->pi_state_cache = pi_state;
395
396 return 0;
397}
398
399static struct futex_pi_state * alloc_pi_state(void)
400{
401 struct futex_pi_state *pi_state = current->pi_state_cache;
402
403 WARN_ON(!pi_state);
404 current->pi_state_cache = NULL;
405
406 return pi_state;
407}
408
409static void free_pi_state(struct futex_pi_state *pi_state)
410{
411 if (!atomic_dec_and_test(&pi_state->refcount))
412 return;
413
414 /*
415 * If pi_state->owner is NULL, the owner is most probably dying
416 * and has cleaned up the pi_state already
417 */
418 if (pi_state->owner) {
419 spin_lock_irq(&pi_state->owner->pi_lock);
420 list_del_init(&pi_state->list);
421 spin_unlock_irq(&pi_state->owner->pi_lock);
422
423 rt_mutex_proxy_unlock(&pi_state->pi_mutex, pi_state->owner);
424 }
425
426 if (current->pi_state_cache)
427 kfree(pi_state);
428 else {
429 /*
430 * pi_state->list is already empty.
431 * clear pi_state->owner.
432 * refcount is at 0 - put it back to 1.
433 */
434 pi_state->owner = NULL;
435 atomic_set(&pi_state->refcount, 1);
436 current->pi_state_cache = pi_state;
437 }
438}
439
440/*
441 * Look up the task based on what TID userspace gave us.
442 * We dont trust it.
443 */
444static struct task_struct * futex_find_get_task(pid_t pid)
445{
446 struct task_struct *p;
447
Oleg Nesterovd359b542006-09-29 02:00:55 -0700448 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700449 p = find_task_by_pid_ns(pid,
450 current->nsproxy->pid_ns);
Thomas Gleixnera06381f2007-06-23 11:48:40 +0200451
452 if (!p || ((current->euid != p->euid) && (current->euid != p->uid)))
453 p = ERR_PTR(-ESRCH);
454 else
455 get_task_struct(p);
456
Oleg Nesterovd359b542006-09-29 02:00:55 -0700457 rcu_read_unlock();
Ingo Molnarc87e2832006-06-27 02:54:58 -0700458
459 return p;
460}
461
462/*
463 * This task is holding PI mutexes at exit time => bad.
464 * Kernel cleans up PI-state, but userspace is likely hosed.
465 * (Robust-futex cleanup is separate and might save the day for userspace.)
466 */
467void exit_pi_state_list(struct task_struct *curr)
468{
Ingo Molnarc87e2832006-06-27 02:54:58 -0700469 struct list_head *next, *head = &curr->pi_state_list;
470 struct futex_pi_state *pi_state;
Ingo Molnar627371d2006-07-29 05:16:20 +0200471 struct futex_hash_bucket *hb;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700472 union futex_key key;
473
474 /*
475 * We are a ZOMBIE and nobody can enqueue itself on
476 * pi_state_list anymore, but we have to be careful
Ingo Molnar627371d2006-07-29 05:16:20 +0200477 * versus waiters unqueueing themselves:
Ingo Molnarc87e2832006-06-27 02:54:58 -0700478 */
479 spin_lock_irq(&curr->pi_lock);
480 while (!list_empty(head)) {
481
482 next = head->next;
483 pi_state = list_entry(next, struct futex_pi_state, list);
484 key = pi_state->key;
Ingo Molnar627371d2006-07-29 05:16:20 +0200485 hb = hash_futex(&key);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700486 spin_unlock_irq(&curr->pi_lock);
487
Ingo Molnarc87e2832006-06-27 02:54:58 -0700488 spin_lock(&hb->lock);
489
490 spin_lock_irq(&curr->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +0200491 /*
492 * We dropped the pi-lock, so re-check whether this
493 * task still owns the PI-state:
494 */
Ingo Molnarc87e2832006-06-27 02:54:58 -0700495 if (head->next != next) {
496 spin_unlock(&hb->lock);
497 continue;
498 }
499
Ingo Molnarc87e2832006-06-27 02:54:58 -0700500 WARN_ON(pi_state->owner != curr);
Ingo Molnar627371d2006-07-29 05:16:20 +0200501 WARN_ON(list_empty(&pi_state->list));
502 list_del_init(&pi_state->list);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700503 pi_state->owner = NULL;
504 spin_unlock_irq(&curr->pi_lock);
505
506 rt_mutex_unlock(&pi_state->pi_mutex);
507
508 spin_unlock(&hb->lock);
509
510 spin_lock_irq(&curr->pi_lock);
511 }
512 spin_unlock_irq(&curr->pi_lock);
513}
514
515static int
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700516lookup_pi_state(u32 uval, struct futex_hash_bucket *hb,
517 union futex_key *key, struct futex_pi_state **ps)
Ingo Molnarc87e2832006-06-27 02:54:58 -0700518{
519 struct futex_pi_state *pi_state = NULL;
520 struct futex_q *this, *next;
Pierre Peifferec92d082007-05-09 02:35:00 -0700521 struct plist_head *head;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700522 struct task_struct *p;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700523 pid_t pid = uval & FUTEX_TID_MASK;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700524
525 head = &hb->chain;
526
Pierre Peifferec92d082007-05-09 02:35:00 -0700527 plist_for_each_entry_safe(this, next, head, list) {
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700528 if (match_futex(&this->key, key)) {
Ingo Molnarc87e2832006-06-27 02:54:58 -0700529 /*
530 * Another waiter already exists - bump up
531 * the refcount and return its pi_state:
532 */
533 pi_state = this->pi_state;
Thomas Gleixner06a9ec22006-07-10 04:44:30 -0700534 /*
535 * Userspace might have messed up non PI and PI futexes
536 */
537 if (unlikely(!pi_state))
538 return -EINVAL;
539
Ingo Molnar627371d2006-07-29 05:16:20 +0200540 WARN_ON(!atomic_read(&pi_state->refcount));
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700541 WARN_ON(pid && pi_state->owner &&
542 pi_state->owner->pid != pid);
Ingo Molnar627371d2006-07-29 05:16:20 +0200543
Ingo Molnarc87e2832006-06-27 02:54:58 -0700544 atomic_inc(&pi_state->refcount);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700545 *ps = pi_state;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700546
547 return 0;
548 }
549 }
550
551 /*
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200552 * We are the first waiter - try to look up the real owner and attach
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700553 * the new pi_state to it, but bail out when TID = 0
Ingo Molnarc87e2832006-06-27 02:54:58 -0700554 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700555 if (!pid)
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200556 return -ESRCH;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700557 p = futex_find_get_task(pid);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700558 if (IS_ERR(p))
559 return PTR_ERR(p);
560
561 /*
562 * We need to look at the task state flags to figure out,
563 * whether the task is exiting. To protect against the do_exit
564 * change of the task flags, we do this protected by
565 * p->pi_lock:
566 */
567 spin_lock_irq(&p->pi_lock);
568 if (unlikely(p->flags & PF_EXITING)) {
569 /*
570 * The task is on the way out. When PF_EXITPIDONE is
571 * set, we know that the task has finished the
572 * cleanup:
573 */
574 int ret = (p->flags & PF_EXITPIDONE) ? -ESRCH : -EAGAIN;
575
576 spin_unlock_irq(&p->pi_lock);
577 put_task_struct(p);
578 return ret;
579 }
Ingo Molnarc87e2832006-06-27 02:54:58 -0700580
581 pi_state = alloc_pi_state();
582
583 /*
584 * Initialize the pi_mutex in locked state and make 'p'
585 * the owner of it:
586 */
587 rt_mutex_init_proxy_locked(&pi_state->pi_mutex, p);
588
589 /* Store the key for possible exit cleanups: */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700590 pi_state->key = *key;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700591
Ingo Molnar627371d2006-07-29 05:16:20 +0200592 WARN_ON(!list_empty(&pi_state->list));
Ingo Molnarc87e2832006-06-27 02:54:58 -0700593 list_add(&pi_state->list, &p->pi_state_list);
594 pi_state->owner = p;
595 spin_unlock_irq(&p->pi_lock);
596
597 put_task_struct(p);
598
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700599 *ps = pi_state;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700600
601 return 0;
602}
603
604/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 * The hash bucket lock must be held when this is called.
606 * Afterwards, the futex_q must not be accessed.
607 */
608static void wake_futex(struct futex_q *q)
609{
Pierre Peifferec92d082007-05-09 02:35:00 -0700610 plist_del(&q->list, &q->list.plist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (q->filp)
612 send_sigio(&q->filp->f_owner, q->fd, POLL_IN);
613 /*
614 * The lock in wake_up_all() is a crucial memory barrier after the
Pierre Peifferec92d082007-05-09 02:35:00 -0700615 * plist_del() and also before assigning to q->lock_ptr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 */
617 wake_up_all(&q->waiters);
618 /*
619 * The waiting task can free the futex_q as soon as this is written,
620 * without taking any locks. This must come last.
Andrew Morton8e311082005-12-23 19:54:46 -0800621 *
622 * A memory barrier is required here to prevent the following store
623 * to lock_ptr from getting ahead of the wakeup. Clearing the lock
624 * at the end of wake_up_all() does not prevent this store from
625 * moving.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 */
Ralf Baechleccdea2f2006-12-06 20:40:26 -0800627 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 q->lock_ptr = NULL;
629}
630
Ingo Molnarc87e2832006-06-27 02:54:58 -0700631static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this)
632{
633 struct task_struct *new_owner;
634 struct futex_pi_state *pi_state = this->pi_state;
635 u32 curval, newval;
636
637 if (!pi_state)
638 return -EINVAL;
639
Ingo Molnar217788672007-03-16 13:38:31 -0800640 spin_lock(&pi_state->pi_mutex.wait_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700641 new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);
642
643 /*
644 * This happens when we have stolen the lock and the original
645 * pending owner did not enqueue itself back on the rt_mutex.
646 * Thats not a tragedy. We know that way, that a lock waiter
647 * is on the fly. We make the futex_q waiter the pending owner.
648 */
649 if (!new_owner)
650 new_owner = this->task;
651
652 /*
653 * We pass it to the next owner. (The WAITERS bit is always
654 * kept enabled while there is PI state around. We must also
655 * preserve the owner died bit.)
656 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200657 if (!(uval & FUTEX_OWNER_DIED)) {
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700658 int ret = 0;
659
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700660 newval = FUTEX_WAITERS | task_pid_vnr(new_owner);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700661
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700662 curval = cmpxchg_futex_value_locked(uaddr, uval, newval);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700663
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200664 if (curval == -EFAULT)
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700665 ret = -EFAULT;
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200666 if (curval != uval)
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700667 ret = -EINVAL;
668 if (ret) {
669 spin_unlock(&pi_state->pi_mutex.wait_lock);
670 return ret;
671 }
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200672 }
Ingo Molnarc87e2832006-06-27 02:54:58 -0700673
Ingo Molnar627371d2006-07-29 05:16:20 +0200674 spin_lock_irq(&pi_state->owner->pi_lock);
675 WARN_ON(list_empty(&pi_state->list));
676 list_del_init(&pi_state->list);
677 spin_unlock_irq(&pi_state->owner->pi_lock);
678
679 spin_lock_irq(&new_owner->pi_lock);
680 WARN_ON(!list_empty(&pi_state->list));
Ingo Molnarc87e2832006-06-27 02:54:58 -0700681 list_add(&pi_state->list, &new_owner->pi_state_list);
682 pi_state->owner = new_owner;
Ingo Molnar627371d2006-07-29 05:16:20 +0200683 spin_unlock_irq(&new_owner->pi_lock);
684
Ingo Molnar217788672007-03-16 13:38:31 -0800685 spin_unlock(&pi_state->pi_mutex.wait_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700686 rt_mutex_unlock(&pi_state->pi_mutex);
687
688 return 0;
689}
690
691static int unlock_futex_pi(u32 __user *uaddr, u32 uval)
692{
693 u32 oldval;
694
695 /*
696 * There is no waiter, so we unlock the futex. The owner died
697 * bit has not to be preserved here. We are the owner:
698 */
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700699 oldval = cmpxchg_futex_value_locked(uaddr, uval, 0);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700700
701 if (oldval == -EFAULT)
702 return oldval;
703 if (oldval != uval)
704 return -EAGAIN;
705
706 return 0;
707}
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709/*
Ingo Molnar8b8f3192006-07-03 00:25:05 -0700710 * Express the locking dependencies for lockdep:
711 */
712static inline void
713double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
714{
715 if (hb1 <= hb2) {
716 spin_lock(&hb1->lock);
717 if (hb1 < hb2)
718 spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING);
719 } else { /* hb1 > hb2 */
720 spin_lock(&hb2->lock);
721 spin_lock_nested(&hb1->lock, SINGLE_DEPTH_NESTING);
722 }
723}
724
725/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 * Wake up all waiters hashed on the physical page that is mapped
727 * to this virtual address:
728 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700729static int futex_wake(u32 __user *uaddr, struct rw_semaphore *fshared,
730 int nr_wake)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Ingo Molnare2970f22006-06-27 02:54:47 -0700732 struct futex_hash_bucket *hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 struct futex_q *this, *next;
Pierre Peifferec92d082007-05-09 02:35:00 -0700734 struct plist_head *head;
Ingo Molnare2970f22006-06-27 02:54:47 -0700735 union futex_key key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 int ret;
737
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700738 futex_lock_mm(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700740 ret = get_futex_key(uaddr, fshared, &key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (unlikely(ret != 0))
742 goto out;
743
Ingo Molnare2970f22006-06-27 02:54:47 -0700744 hb = hash_futex(&key);
745 spin_lock(&hb->lock);
746 head = &hb->chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Pierre Peifferec92d082007-05-09 02:35:00 -0700748 plist_for_each_entry_safe(this, next, head, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if (match_futex (&this->key, &key)) {
Ingo Molnared6f7b12006-07-01 04:35:46 -0700750 if (this->pi_state) {
751 ret = -EINVAL;
752 break;
753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 wake_futex(this);
755 if (++ret >= nr_wake)
756 break;
757 }
758 }
759
Ingo Molnare2970f22006-06-27 02:54:47 -0700760 spin_unlock(&hb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761out:
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700762 futex_unlock_mm(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return ret;
764}
765
766/*
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700767 * Wake up all waiters hashed on the physical page that is mapped
768 * to this virtual address:
769 */
Ingo Molnare2970f22006-06-27 02:54:47 -0700770static int
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700771futex_wake_op(u32 __user *uaddr1, struct rw_semaphore *fshared,
772 u32 __user *uaddr2,
Ingo Molnare2970f22006-06-27 02:54:47 -0700773 int nr_wake, int nr_wake2, int op)
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700774{
775 union futex_key key1, key2;
Ingo Molnare2970f22006-06-27 02:54:47 -0700776 struct futex_hash_bucket *hb1, *hb2;
Pierre Peifferec92d082007-05-09 02:35:00 -0700777 struct plist_head *head;
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700778 struct futex_q *this, *next;
779 int ret, op_ret, attempt = 0;
780
781retryfull:
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700782 futex_lock_mm(fshared);
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700783
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700784 ret = get_futex_key(uaddr1, fshared, &key1);
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700785 if (unlikely(ret != 0))
786 goto out;
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700787 ret = get_futex_key(uaddr2, fshared, &key2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700788 if (unlikely(ret != 0))
789 goto out;
790
Ingo Molnare2970f22006-06-27 02:54:47 -0700791 hb1 = hash_futex(&key1);
792 hb2 = hash_futex(&key2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700793
794retry:
Ingo Molnar8b8f3192006-07-03 00:25:05 -0700795 double_lock_hb(hb1, hb2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700796
Ingo Molnare2970f22006-06-27 02:54:47 -0700797 op_ret = futex_atomic_op_inuser(op, uaddr2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700798 if (unlikely(op_ret < 0)) {
Ingo Molnare2970f22006-06-27 02:54:47 -0700799 u32 dummy;
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700800
Ingo Molnare2970f22006-06-27 02:54:47 -0700801 spin_unlock(&hb1->lock);
802 if (hb1 != hb2)
803 spin_unlock(&hb2->lock);
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700804
David Howells7ee1dd32006-01-06 00:11:44 -0800805#ifndef CONFIG_MMU
Ingo Molnare2970f22006-06-27 02:54:47 -0700806 /*
807 * we don't get EFAULT from MMU faults if we don't have an MMU,
808 * but we might get them from range checking
809 */
David Howells7ee1dd32006-01-06 00:11:44 -0800810 ret = op_ret;
811 goto out;
812#endif
813
David Gibson796f8d92005-11-07 00:59:33 -0800814 if (unlikely(op_ret != -EFAULT)) {
815 ret = op_ret;
816 goto out;
817 }
818
Ingo Molnare2970f22006-06-27 02:54:47 -0700819 /*
820 * futex_atomic_op_inuser needs to both read and write
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700821 * *(int __user *)uaddr2, but we can't modify it
822 * non-atomically. Therefore, if get_user below is not
823 * enough, we need to handle the fault ourselves, while
Ingo Molnare2970f22006-06-27 02:54:47 -0700824 * still holding the mmap_sem.
825 */
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700826 if (attempt++) {
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700827 ret = futex_handle_fault((unsigned long)uaddr2,
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700828 fshared, attempt);
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700829 if (ret)
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700830 goto out;
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700831 goto retry;
832 }
833
Ingo Molnare2970f22006-06-27 02:54:47 -0700834 /*
835 * If we would have faulted, release mmap_sem,
836 * fault it in and start all over again.
837 */
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700838 futex_unlock_mm(fshared);
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700839
Ingo Molnare2970f22006-06-27 02:54:47 -0700840 ret = get_user(dummy, uaddr2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700841 if (ret)
842 return ret;
843
844 goto retryfull;
845 }
846
Ingo Molnare2970f22006-06-27 02:54:47 -0700847 head = &hb1->chain;
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700848
Pierre Peifferec92d082007-05-09 02:35:00 -0700849 plist_for_each_entry_safe(this, next, head, list) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700850 if (match_futex (&this->key, &key1)) {
851 wake_futex(this);
852 if (++ret >= nr_wake)
853 break;
854 }
855 }
856
857 if (op_ret > 0) {
Ingo Molnare2970f22006-06-27 02:54:47 -0700858 head = &hb2->chain;
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700859
860 op_ret = 0;
Pierre Peifferec92d082007-05-09 02:35:00 -0700861 plist_for_each_entry_safe(this, next, head, list) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700862 if (match_futex (&this->key, &key2)) {
863 wake_futex(this);
864 if (++op_ret >= nr_wake2)
865 break;
866 }
867 }
868 ret += op_ret;
869 }
870
Ingo Molnare2970f22006-06-27 02:54:47 -0700871 spin_unlock(&hb1->lock);
872 if (hb1 != hb2)
873 spin_unlock(&hb2->lock);
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700874out:
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700875 futex_unlock_mm(fshared);
876
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700877 return ret;
878}
879
880/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 * Requeue all waiters hashed on one physical page to another
882 * physical page.
883 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700884static int futex_requeue(u32 __user *uaddr1, struct rw_semaphore *fshared,
885 u32 __user *uaddr2,
Ingo Molnare2970f22006-06-27 02:54:47 -0700886 int nr_wake, int nr_requeue, u32 *cmpval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
888 union futex_key key1, key2;
Ingo Molnare2970f22006-06-27 02:54:47 -0700889 struct futex_hash_bucket *hb1, *hb2;
Pierre Peifferec92d082007-05-09 02:35:00 -0700890 struct plist_head *head1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 struct futex_q *this, *next;
892 int ret, drop_count = 0;
893
894 retry:
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700895 futex_lock_mm(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700897 ret = get_futex_key(uaddr1, fshared, &key1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 if (unlikely(ret != 0))
899 goto out;
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700900 ret = get_futex_key(uaddr2, fshared, &key2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 if (unlikely(ret != 0))
902 goto out;
903
Ingo Molnare2970f22006-06-27 02:54:47 -0700904 hb1 = hash_futex(&key1);
905 hb2 = hash_futex(&key2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Ingo Molnar8b8f3192006-07-03 00:25:05 -0700907 double_lock_hb(hb1, hb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Ingo Molnare2970f22006-06-27 02:54:47 -0700909 if (likely(cmpval != NULL)) {
910 u32 curval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Ingo Molnare2970f22006-06-27 02:54:47 -0700912 ret = get_futex_value_locked(&curval, uaddr1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 if (unlikely(ret)) {
Ingo Molnare2970f22006-06-27 02:54:47 -0700915 spin_unlock(&hb1->lock);
916 if (hb1 != hb2)
917 spin_unlock(&hb2->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Ingo Molnare2970f22006-06-27 02:54:47 -0700919 /*
920 * If we would have faulted, release mmap_sem, fault
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 * it in and start all over again.
922 */
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700923 futex_unlock_mm(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Ingo Molnare2970f22006-06-27 02:54:47 -0700925 ret = get_user(curval, uaddr1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 if (!ret)
928 goto retry;
929
930 return ret;
931 }
Ingo Molnare2970f22006-06-27 02:54:47 -0700932 if (curval != *cmpval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 ret = -EAGAIN;
934 goto out_unlock;
935 }
936 }
937
Ingo Molnare2970f22006-06-27 02:54:47 -0700938 head1 = &hb1->chain;
Pierre Peifferec92d082007-05-09 02:35:00 -0700939 plist_for_each_entry_safe(this, next, head1, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (!match_futex (&this->key, &key1))
941 continue;
942 if (++ret <= nr_wake) {
943 wake_futex(this);
944 } else {
Sebastien Dugue59e0e0a2006-06-27 02:55:03 -0700945 /*
946 * If key1 and key2 hash to the same bucket, no need to
947 * requeue.
948 */
949 if (likely(head1 != &hb2->chain)) {
Pierre Peifferec92d082007-05-09 02:35:00 -0700950 plist_del(&this->list, &hb1->chain);
951 plist_add(&this->list, &hb2->chain);
Sebastien Dugue59e0e0a2006-06-27 02:55:03 -0700952 this->lock_ptr = &hb2->lock;
Pierre Peifferec92d082007-05-09 02:35:00 -0700953#ifdef CONFIG_DEBUG_PI_LIST
954 this->list.plist.lock = &hb2->lock;
955#endif
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 this->key = key2;
Rusty Russell9adef582007-05-08 00:26:42 -0700958 get_futex_key_refs(&key2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 drop_count++;
960
961 if (ret - nr_wake >= nr_requeue)
962 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
964 }
965
966out_unlock:
Ingo Molnare2970f22006-06-27 02:54:47 -0700967 spin_unlock(&hb1->lock);
968 if (hb1 != hb2)
969 spin_unlock(&hb2->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Rusty Russell9adef582007-05-08 00:26:42 -0700971 /* drop_futex_key_refs() must be called outside the spinlocks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 while (--drop_count >= 0)
Rusty Russell9adef582007-05-08 00:26:42 -0700973 drop_futex_key_refs(&key1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975out:
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700976 futex_unlock_mm(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return ret;
978}
979
980/* The key must be already stored in q->key. */
981static inline struct futex_hash_bucket *
982queue_lock(struct futex_q *q, int fd, struct file *filp)
983{
Ingo Molnare2970f22006-06-27 02:54:47 -0700984 struct futex_hash_bucket *hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 q->fd = fd;
987 q->filp = filp;
988
989 init_waitqueue_head(&q->waiters);
990
Rusty Russell9adef582007-05-08 00:26:42 -0700991 get_futex_key_refs(&q->key);
Ingo Molnare2970f22006-06-27 02:54:47 -0700992 hb = hash_futex(&q->key);
993 q->lock_ptr = &hb->lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Ingo Molnare2970f22006-06-27 02:54:47 -0700995 spin_lock(&hb->lock);
996 return hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
Ingo Molnare2970f22006-06-27 02:54:47 -0700999static inline void __queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Pierre Peifferec92d082007-05-09 02:35:00 -07001001 int prio;
1002
1003 /*
1004 * The priority used to register this element is
1005 * - either the real thread-priority for the real-time threads
1006 * (i.e. threads with a priority lower than MAX_RT_PRIO)
1007 * - or MAX_RT_PRIO for non-RT threads.
1008 * Thus, all RT-threads are woken first in priority order, and
1009 * the others are woken last, in FIFO order.
1010 */
1011 prio = min(current->normal_prio, MAX_RT_PRIO);
1012
1013 plist_node_init(&q->list, prio);
1014#ifdef CONFIG_DEBUG_PI_LIST
1015 q->list.plist.lock = &hb->lock;
1016#endif
1017 plist_add(&q->list, &hb->chain);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001018 q->task = current;
Ingo Molnare2970f22006-06-27 02:54:47 -07001019 spin_unlock(&hb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
1021
1022static inline void
Ingo Molnare2970f22006-06-27 02:54:47 -07001023queue_unlock(struct futex_q *q, struct futex_hash_bucket *hb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Ingo Molnare2970f22006-06-27 02:54:47 -07001025 spin_unlock(&hb->lock);
Rusty Russell9adef582007-05-08 00:26:42 -07001026 drop_futex_key_refs(&q->key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028
1029/*
1030 * queue_me and unqueue_me must be called as a pair, each
1031 * exactly once. They are called with the hashed spinlock held.
1032 */
1033
1034/* The key must be already stored in q->key. */
1035static void queue_me(struct futex_q *q, int fd, struct file *filp)
1036{
Ingo Molnare2970f22006-06-27 02:54:47 -07001037 struct futex_hash_bucket *hb;
1038
1039 hb = queue_lock(q, fd, filp);
1040 __queue_me(q, hb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
1043/* Return 1 if we were still queued (ie. 0 means we were woken) */
1044static int unqueue_me(struct futex_q *q)
1045{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 spinlock_t *lock_ptr;
Ingo Molnare2970f22006-06-27 02:54:47 -07001047 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049 /* In the common case we don't take the spinlock, which is nice. */
1050 retry:
1051 lock_ptr = q->lock_ptr;
Christian Borntraegere91467e2006-08-05 12:13:52 -07001052 barrier();
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07001053 if (lock_ptr != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 spin_lock(lock_ptr);
1055 /*
1056 * q->lock_ptr can change between reading it and
1057 * spin_lock(), causing us to take the wrong lock. This
1058 * corrects the race condition.
1059 *
1060 * Reasoning goes like this: if we have the wrong lock,
1061 * q->lock_ptr must have changed (maybe several times)
1062 * between reading it and the spin_lock(). It can
1063 * change again after the spin_lock() but only if it was
1064 * already changed before the spin_lock(). It cannot,
1065 * however, change back to the original value. Therefore
1066 * we can detect whether we acquired the correct lock.
1067 */
1068 if (unlikely(lock_ptr != q->lock_ptr)) {
1069 spin_unlock(lock_ptr);
1070 goto retry;
1071 }
Pierre Peifferec92d082007-05-09 02:35:00 -07001072 WARN_ON(plist_node_empty(&q->list));
1073 plist_del(&q->list, &q->list.plist);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001074
1075 BUG_ON(q->pi_state);
1076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 spin_unlock(lock_ptr);
1078 ret = 1;
1079 }
1080
Rusty Russell9adef582007-05-08 00:26:42 -07001081 drop_futex_key_refs(&q->key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return ret;
1083}
1084
Ingo Molnarc87e2832006-06-27 02:54:58 -07001085/*
1086 * PI futexes can not be requeued and must remove themself from the
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001087 * hash bucket. The hash bucket lock (i.e. lock_ptr) is held on entry
1088 * and dropped here.
Ingo Molnarc87e2832006-06-27 02:54:58 -07001089 */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001090static void unqueue_me_pi(struct futex_q *q)
Ingo Molnarc87e2832006-06-27 02:54:58 -07001091{
Pierre Peifferec92d082007-05-09 02:35:00 -07001092 WARN_ON(plist_node_empty(&q->list));
1093 plist_del(&q->list, &q->list.plist);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001094
1095 BUG_ON(!q->pi_state);
1096 free_pi_state(q->pi_state);
1097 q->pi_state = NULL;
1098
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001099 spin_unlock(q->lock_ptr);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001100
Rusty Russell9adef582007-05-08 00:26:42 -07001101 drop_futex_key_refs(&q->key);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001102}
1103
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001104/*
1105 * Fixup the pi_state owner with current.
1106 *
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001107 * Must be called with hash bucket lock held and mm->sem held for non
1108 * private futexes.
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001109 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001110static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001111 struct task_struct *curr)
1112{
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001113 u32 newtid = task_pid_vnr(curr) | FUTEX_WAITERS;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001114 struct futex_pi_state *pi_state = q->pi_state;
1115 u32 uval, curval, newval;
1116 int ret;
1117
1118 /* Owner died? */
1119 if (pi_state->owner != NULL) {
1120 spin_lock_irq(&pi_state->owner->pi_lock);
1121 WARN_ON(list_empty(&pi_state->list));
1122 list_del_init(&pi_state->list);
1123 spin_unlock_irq(&pi_state->owner->pi_lock);
1124 } else
1125 newtid |= FUTEX_OWNER_DIED;
1126
1127 pi_state->owner = curr;
1128
1129 spin_lock_irq(&curr->pi_lock);
1130 WARN_ON(!list_empty(&pi_state->list));
1131 list_add(&pi_state->list, &curr->pi_state_list);
1132 spin_unlock_irq(&curr->pi_lock);
1133
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001134 /*
1135 * We own it, so we have to replace the pending owner
1136 * TID. This must be atomic as we have preserve the
1137 * owner died bit here.
1138 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001139 ret = get_futex_value_locked(&uval, uaddr);
1140
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001141 while (!ret) {
1142 newval = (uval & FUTEX_OWNER_DIED) | newtid;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001143
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001144 curval = cmpxchg_futex_value_locked(uaddr, uval, newval);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001145
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001146 if (curval == -EFAULT)
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001147 ret = -EFAULT;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001148 if (curval == uval)
1149 break;
1150 uval = curval;
1151 }
1152 return ret;
1153}
1154
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001155/*
1156 * In case we must use restart_block to restart a futex_wait,
1157 * we encode in the 'arg3' shared capability
1158 */
1159#define ARG3_SHARED 1
1160
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001161static long futex_wait_restart(struct restart_block *restart);
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001162
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001163static int futex_wait(u32 __user *uaddr, struct rw_semaphore *fshared,
1164 u32 val, ktime_t *abs_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
Ingo Molnarc87e2832006-06-27 02:54:58 -07001166 struct task_struct *curr = current;
1167 DECLARE_WAITQUEUE(wait, curr);
Ingo Molnare2970f22006-06-27 02:54:47 -07001168 struct futex_hash_bucket *hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 struct futex_q q;
Ingo Molnare2970f22006-06-27 02:54:47 -07001170 u32 uval;
1171 int ret;
Thomas Gleixnerbd197232007-06-17 21:11:10 +02001172 struct hrtimer_sleeper t;
Pierre Peifferc19384b2007-05-09 02:35:02 -07001173 int rem = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Ingo Molnarc87e2832006-06-27 02:54:58 -07001175 q.pi_state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 retry:
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001177 futex_lock_mm(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001179 ret = get_futex_key(uaddr, fshared, &q.key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (unlikely(ret != 0))
1181 goto out_release_sem;
1182
Ingo Molnare2970f22006-06-27 02:54:47 -07001183 hb = queue_lock(&q, -1, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 /*
1186 * Access the page AFTER the futex is queued.
1187 * Order is important:
1188 *
1189 * Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val);
1190 * Userspace waker: if (cond(var)) { var = new; futex_wake(&var); }
1191 *
1192 * The basic logical guarantee of a futex is that it blocks ONLY
1193 * if cond(var) is known to be true at the time of blocking, for
1194 * any cond. If we queued after testing *uaddr, that would open
1195 * a race condition where we could block indefinitely with
1196 * cond(var) false, which would violate the guarantee.
1197 *
1198 * A consequence is that futex_wait() can return zero and absorb
1199 * a wakeup when *uaddr != val on entry to the syscall. This is
1200 * rare, but normal.
1201 *
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001202 * for shared futexes, we hold the mmap semaphore, so the mapping
1203 * cannot have changed since we looked it up in get_futex_key.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 */
Ingo Molnare2970f22006-06-27 02:54:47 -07001205 ret = get_futex_value_locked(&uval, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 if (unlikely(ret)) {
Ingo Molnare2970f22006-06-27 02:54:47 -07001208 queue_unlock(&q, hb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Ingo Molnare2970f22006-06-27 02:54:47 -07001210 /*
1211 * If we would have faulted, release mmap_sem, fault it in and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 * start all over again.
1213 */
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001214 futex_unlock_mm(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Ingo Molnare2970f22006-06-27 02:54:47 -07001216 ret = get_user(uval, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 if (!ret)
1219 goto retry;
1220 return ret;
1221 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001222 ret = -EWOULDBLOCK;
1223 if (uval != val)
1224 goto out_unlock_release_sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 /* Only actually queue if *uaddr contained val. */
Ingo Molnare2970f22006-06-27 02:54:47 -07001227 __queue_me(&q, hb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 /*
1230 * Now the futex is queued and we have checked the data, we
1231 * don't want to hold mmap_sem while we sleep.
Ingo Molnarc87e2832006-06-27 02:54:58 -07001232 */
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001233 futex_unlock_mm(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 /*
1236 * There might have been scheduling since the queue_me(), as we
1237 * cannot hold a spinlock across the get_user() in case it
1238 * faults, and we cannot just set TASK_INTERRUPTIBLE state when
1239 * queueing ourselves into the futex hash. This code thus has to
1240 * rely on the futex_wake() code removing us from hash when it
1241 * wakes us up.
1242 */
1243
1244 /* add_wait_queue is the barrier after __set_current_state. */
1245 __set_current_state(TASK_INTERRUPTIBLE);
1246 add_wait_queue(&q.waiters, &wait);
1247 /*
Pierre Peifferec92d082007-05-09 02:35:00 -07001248 * !plist_node_empty() is safe here without any lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 * q.lock_ptr != 0 is not safe, because of ordering against wakeup.
1250 */
Pierre Peifferec92d082007-05-09 02:35:00 -07001251 if (likely(!plist_node_empty(&q.list))) {
Pierre Peifferc19384b2007-05-09 02:35:02 -07001252 if (!abs_time)
1253 schedule();
1254 else {
1255 hrtimer_init(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1256 hrtimer_init_sleeper(&t, current);
1257 t.timer.expires = *abs_time;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001258
Pierre Peifferc19384b2007-05-09 02:35:02 -07001259 hrtimer_start(&t.timer, t.timer.expires, HRTIMER_MODE_ABS);
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001260
Pierre Peifferc19384b2007-05-09 02:35:02 -07001261 /*
1262 * the timer could have already expired, in which
1263 * case current would be flagged for rescheduling.
1264 * Don't bother calling schedule.
1265 */
1266 if (likely(t.task))
1267 schedule();
1268
1269 hrtimer_cancel(&t.timer);
1270
1271 /* Flag if a timeout occured */
1272 rem = (t.task == NULL);
1273 }
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 __set_current_state(TASK_RUNNING);
1276
1277 /*
1278 * NOTE: we don't remove ourselves from the waitqueue because
1279 * we are the only user of it.
1280 */
1281
1282 /* If we were woken (and unqueued), we succeeded, whatever. */
1283 if (!unqueue_me(&q))
1284 return 0;
Pierre Peifferc19384b2007-05-09 02:35:02 -07001285 if (rem)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return -ETIMEDOUT;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001287
Ingo Molnare2970f22006-06-27 02:54:47 -07001288 /*
1289 * We expect signal_pending(current), but another thread may
1290 * have handled it for us already.
1291 */
Pierre Peifferc19384b2007-05-09 02:35:02 -07001292 if (!abs_time)
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001293 return -ERESTARTSYS;
1294 else {
1295 struct restart_block *restart;
1296 restart = &current_thread_info()->restart_block;
1297 restart->fn = futex_wait_restart;
1298 restart->arg0 = (unsigned long)uaddr;
1299 restart->arg1 = (unsigned long)val;
Pierre Peifferc19384b2007-05-09 02:35:02 -07001300 restart->arg2 = (unsigned long)abs_time;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001301 restart->arg3 = 0;
1302 if (fshared)
1303 restart->arg3 |= ARG3_SHARED;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001304 return -ERESTART_RESTARTBLOCK;
1305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Ingo Molnarc87e2832006-06-27 02:54:58 -07001307 out_unlock_release_sem:
1308 queue_unlock(&q, hb);
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 out_release_sem:
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001311 futex_unlock_mm(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001312 return ret;
1313}
1314
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001315
1316static long futex_wait_restart(struct restart_block *restart)
1317{
1318 u32 __user *uaddr = (u32 __user *)restart->arg0;
1319 u32 val = (u32)restart->arg1;
Pierre Peifferc19384b2007-05-09 02:35:02 -07001320 ktime_t *abs_time = (ktime_t *)restart->arg2;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001321 struct rw_semaphore *fshared = NULL;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001322
1323 restart->fn = do_no_restart_syscall;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001324 if (restart->arg3 & ARG3_SHARED)
1325 fshared = &current->mm->mmap_sem;
1326 return (long)futex_wait(uaddr, fshared, val, abs_time);
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001327}
1328
1329
Ingo Molnarc87e2832006-06-27 02:54:58 -07001330/*
1331 * Userspace tried a 0 -> TID atomic transition of the futex value
1332 * and failed. The kernel side here does the whole locking operation:
1333 * if there are waiters then it will block, it does PI, etc. (Due to
1334 * races the kernel might see a 0 value of the futex too.)
1335 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001336static int futex_lock_pi(u32 __user *uaddr, struct rw_semaphore *fshared,
1337 int detect, ktime_t *time, int trylock)
Ingo Molnarc87e2832006-06-27 02:54:58 -07001338{
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07001339 struct hrtimer_sleeper timeout, *to = NULL;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001340 struct task_struct *curr = current;
1341 struct futex_hash_bucket *hb;
1342 u32 uval, newval, curval;
1343 struct futex_q q;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001344 int ret, lock_taken, ownerdied = 0, attempt = 0;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001345
1346 if (refill_pi_state_cache())
1347 return -ENOMEM;
1348
Pierre Peifferc19384b2007-05-09 02:35:02 -07001349 if (time) {
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07001350 to = &timeout;
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001351 hrtimer_init(&to->timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07001352 hrtimer_init_sleeper(to, current);
Pierre Peifferc19384b2007-05-09 02:35:02 -07001353 to->timer.expires = *time;
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07001354 }
1355
Ingo Molnarc87e2832006-06-27 02:54:58 -07001356 q.pi_state = NULL;
1357 retry:
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001358 futex_lock_mm(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001359
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001360 ret = get_futex_key(uaddr, fshared, &q.key);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001361 if (unlikely(ret != 0))
1362 goto out_release_sem;
1363
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001364 retry_unlocked:
Ingo Molnarc87e2832006-06-27 02:54:58 -07001365 hb = queue_lock(&q, -1, NULL);
1366
1367 retry_locked:
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001368 ret = lock_taken = 0;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001369
Ingo Molnarc87e2832006-06-27 02:54:58 -07001370 /*
1371 * To avoid races, we attempt to take the lock here again
1372 * (by doing a 0 -> TID atomic cmpxchg), while holding all
1373 * the locks. It will most likely not succeed.
1374 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001375 newval = task_pid_vnr(current);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001376
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001377 curval = cmpxchg_futex_value_locked(uaddr, 0, newval);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001378
1379 if (unlikely(curval == -EFAULT))
1380 goto uaddr_faulted;
1381
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001382 /*
1383 * Detect deadlocks. In case of REQUEUE_PI this is a valid
1384 * situation and we return success to user space.
1385 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001386 if (unlikely((curval & FUTEX_TID_MASK) == task_pid_vnr(current))) {
Thomas Gleixnerbd197232007-06-17 21:11:10 +02001387 ret = -EDEADLK;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001388 goto out_unlock_release_sem;
1389 }
1390
1391 /*
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001392 * Surprise - we got the lock. Just return to userspace:
Ingo Molnarc87e2832006-06-27 02:54:58 -07001393 */
1394 if (unlikely(!curval))
1395 goto out_unlock_release_sem;
1396
1397 uval = curval;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001398
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001399 /*
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001400 * Set the WAITERS flag, so the owner will know it has someone
1401 * to wake at next unlock
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001402 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001403 newval = curval | FUTEX_WAITERS;
1404
1405 /*
1406 * There are two cases, where a futex might have no owner (the
Thomas Gleixnerbd197232007-06-17 21:11:10 +02001407 * owner TID is 0): OWNER_DIED. We take over the futex in this
1408 * case. We also do an unconditional take over, when the owner
1409 * of the futex died.
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001410 *
1411 * This is safe as we are protected by the hash bucket lock !
1412 */
1413 if (unlikely(ownerdied || !(curval & FUTEX_TID_MASK))) {
Thomas Gleixnerbd197232007-06-17 21:11:10 +02001414 /* Keep the OWNER_DIED bit */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001415 newval = (curval & ~FUTEX_TID_MASK) | task_pid_vnr(current);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001416 ownerdied = 0;
1417 lock_taken = 1;
1418 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001419
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001420 curval = cmpxchg_futex_value_locked(uaddr, uval, newval);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001421
1422 if (unlikely(curval == -EFAULT))
1423 goto uaddr_faulted;
1424 if (unlikely(curval != uval))
1425 goto retry_locked;
1426
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001427 /*
Thomas Gleixnerbd197232007-06-17 21:11:10 +02001428 * We took the lock due to owner died take over.
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001429 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +02001430 if (unlikely(lock_taken))
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001431 goto out_unlock_release_sem;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001432
Ingo Molnarc87e2832006-06-27 02:54:58 -07001433 /*
1434 * We dont have the lock. Look up the PI state (or create it if
1435 * we are the first waiter):
1436 */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001437 ret = lookup_pi_state(uval, hb, &q.key, &q.pi_state);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001438
1439 if (unlikely(ret)) {
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001440 switch (ret) {
Ingo Molnarc87e2832006-06-27 02:54:58 -07001441
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001442 case -EAGAIN:
1443 /*
1444 * Task is exiting and we just wait for the
1445 * exit to complete.
1446 */
1447 queue_unlock(&q, hb);
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001448 futex_unlock_mm(fshared);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001449 cond_resched();
1450 goto retry;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001451
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001452 case -ESRCH:
1453 /*
1454 * No owner found for this futex. Check if the
1455 * OWNER_DIED bit is set to figure out whether
1456 * this is a robust futex or not.
1457 */
1458 if (get_futex_value_locked(&curval, uaddr))
Ingo Molnarc87e2832006-06-27 02:54:58 -07001459 goto uaddr_faulted;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001460
1461 /*
1462 * We simply start over in case of a robust
1463 * futex. The code above will take the futex
1464 * and return happy.
1465 */
1466 if (curval & FUTEX_OWNER_DIED) {
1467 ownerdied = 1;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001468 goto retry_locked;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001469 }
1470 default:
1471 goto out_unlock_release_sem;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001472 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001473 }
1474
1475 /*
1476 * Only actually queue now that the atomic ops are done:
1477 */
1478 __queue_me(&q, hb);
1479
1480 /*
1481 * Now the futex is queued and we have checked the data, we
1482 * don't want to hold mmap_sem while we sleep.
1483 */
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001484 futex_unlock_mm(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001485
1486 WARN_ON(!q.pi_state);
1487 /*
1488 * Block on the PI mutex:
1489 */
1490 if (!trylock)
1491 ret = rt_mutex_timed_lock(&q.pi_state->pi_mutex, to, 1);
1492 else {
1493 ret = rt_mutex_trylock(&q.pi_state->pi_mutex);
1494 /* Fixup the trylock return value: */
1495 ret = ret ? 0 : -EWOULDBLOCK;
1496 }
1497
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001498 futex_lock_mm(fshared);
Vernon Mauerya99e4e42006-07-01 04:35:42 -07001499 spin_lock(q.lock_ptr);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001500
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001501 if (!ret) {
1502 /*
1503 * Got the lock. We might not be the anticipated owner
1504 * if we did a lock-steal - fix up the PI-state in
1505 * that case:
1506 */
1507 if (q.pi_state->owner != curr)
1508 ret = fixup_pi_state_owner(uaddr, &q, curr);
1509 } else {
Ingo Molnarc87e2832006-06-27 02:54:58 -07001510 /*
1511 * Catch the rare case, where the lock was released
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001512 * when we were on the way back before we locked the
1513 * hash bucket.
Ingo Molnarc87e2832006-06-27 02:54:58 -07001514 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001515 if (q.pi_state->owner == curr &&
1516 rt_mutex_trylock(&q.pi_state->pi_mutex)) {
1517 ret = 0;
1518 } else {
1519 /*
1520 * Paranoia check. If we did not take the lock
1521 * in the trylock above, then we should not be
1522 * the owner of the rtmutex, neither the real
1523 * nor the pending one:
1524 */
1525 if (rt_mutex_owner(&q.pi_state->pi_mutex) == curr)
1526 printk(KERN_ERR "futex_lock_pi: ret = %d "
1527 "pi-mutex: %p pi-state %p\n", ret,
1528 q.pi_state->pi_mutex.owner,
1529 q.pi_state->owner);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001530 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001531 }
1532
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001533 /* Unqueue and drop the lock */
1534 unqueue_me_pi(&q);
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001535 futex_unlock_mm(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001536
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07001537 return ret != -EINTR ? ret : -ERESTARTNOINTR;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001538
1539 out_unlock_release_sem:
1540 queue_unlock(&q, hb);
1541
1542 out_release_sem:
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001543 futex_unlock_mm(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001544 return ret;
1545
1546 uaddr_faulted:
1547 /*
1548 * We have to r/w *(int __user *)uaddr, but we can't modify it
1549 * non-atomically. Therefore, if get_user below is not
1550 * enough, we need to handle the fault ourselves, while
1551 * still holding the mmap_sem.
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001552 *
1553 * ... and hb->lock. :-) --ANK
Ingo Molnarc87e2832006-06-27 02:54:58 -07001554 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001555 queue_unlock(&q, hb);
1556
Ingo Molnarc87e2832006-06-27 02:54:58 -07001557 if (attempt++) {
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001558 ret = futex_handle_fault((unsigned long)uaddr, fshared,
1559 attempt);
1560 if (ret)
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001561 goto out_release_sem;
1562 goto retry_unlocked;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001563 }
1564
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001565 futex_unlock_mm(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001566
1567 ret = get_user(uval, uaddr);
1568 if (!ret && (uval != -EFAULT))
1569 goto retry;
1570
1571 return ret;
1572}
1573
1574/*
Ingo Molnarc87e2832006-06-27 02:54:58 -07001575 * Userspace attempted a TID -> 0 atomic transition, and failed.
1576 * This is the in-kernel slowpath: we look up the PI state (if any),
1577 * and do the rt-mutex unlock.
1578 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001579static int futex_unlock_pi(u32 __user *uaddr, struct rw_semaphore *fshared)
Ingo Molnarc87e2832006-06-27 02:54:58 -07001580{
1581 struct futex_hash_bucket *hb;
1582 struct futex_q *this, *next;
1583 u32 uval;
Pierre Peifferec92d082007-05-09 02:35:00 -07001584 struct plist_head *head;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001585 union futex_key key;
1586 int ret, attempt = 0;
1587
1588retry:
1589 if (get_user(uval, uaddr))
1590 return -EFAULT;
1591 /*
1592 * We release only a lock we actually own:
1593 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001594 if ((uval & FUTEX_TID_MASK) != task_pid_vnr(current))
Ingo Molnarc87e2832006-06-27 02:54:58 -07001595 return -EPERM;
1596 /*
1597 * First take all the futex related locks:
1598 */
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001599 futex_lock_mm(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001600
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001601 ret = get_futex_key(uaddr, fshared, &key);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001602 if (unlikely(ret != 0))
1603 goto out;
1604
1605 hb = hash_futex(&key);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001606retry_unlocked:
Ingo Molnarc87e2832006-06-27 02:54:58 -07001607 spin_lock(&hb->lock);
1608
Ingo Molnarc87e2832006-06-27 02:54:58 -07001609 /*
1610 * To avoid races, try to do the TID -> 0 atomic transition
1611 * again. If it succeeds then we can return without waking
1612 * anyone else up:
1613 */
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001614 if (!(uval & FUTEX_OWNER_DIED))
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001615 uval = cmpxchg_futex_value_locked(uaddr, task_pid_vnr(current), 0);
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001616
Ingo Molnarc87e2832006-06-27 02:54:58 -07001617
1618 if (unlikely(uval == -EFAULT))
1619 goto pi_faulted;
1620 /*
1621 * Rare case: we managed to release the lock atomically,
1622 * no need to wake anyone else up:
1623 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001624 if (unlikely(uval == task_pid_vnr(current)))
Ingo Molnarc87e2832006-06-27 02:54:58 -07001625 goto out_unlock;
1626
1627 /*
1628 * Ok, other tasks may need to be woken up - check waiters
1629 * and do the wakeup if necessary:
1630 */
1631 head = &hb->chain;
1632
Pierre Peifferec92d082007-05-09 02:35:00 -07001633 plist_for_each_entry_safe(this, next, head, list) {
Ingo Molnarc87e2832006-06-27 02:54:58 -07001634 if (!match_futex (&this->key, &key))
1635 continue;
1636 ret = wake_futex_pi(uaddr, uval, this);
1637 /*
1638 * The atomic access to the futex value
1639 * generated a pagefault, so retry the
1640 * user-access and the wakeup:
1641 */
1642 if (ret == -EFAULT)
1643 goto pi_faulted;
1644 goto out_unlock;
1645 }
1646 /*
1647 * No waiters - kernel unlocks the futex:
1648 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001649 if (!(uval & FUTEX_OWNER_DIED)) {
1650 ret = unlock_futex_pi(uaddr, uval);
1651 if (ret == -EFAULT)
1652 goto pi_faulted;
1653 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001654
1655out_unlock:
1656 spin_unlock(&hb->lock);
1657out:
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001658 futex_unlock_mm(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001659
1660 return ret;
1661
1662pi_faulted:
1663 /*
1664 * We have to r/w *(int __user *)uaddr, but we can't modify it
1665 * non-atomically. Therefore, if get_user below is not
1666 * enough, we need to handle the fault ourselves, while
1667 * still holding the mmap_sem.
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001668 *
1669 * ... and hb->lock. --ANK
Ingo Molnarc87e2832006-06-27 02:54:58 -07001670 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001671 spin_unlock(&hb->lock);
1672
Ingo Molnarc87e2832006-06-27 02:54:58 -07001673 if (attempt++) {
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001674 ret = futex_handle_fault((unsigned long)uaddr, fshared,
1675 attempt);
1676 if (ret)
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001677 goto out;
john stultz187226f2007-08-22 14:01:10 -07001678 uval = 0;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001679 goto retry_unlocked;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001680 }
1681
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001682 futex_unlock_mm(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001683
1684 ret = get_user(uval, uaddr);
1685 if (!ret && (uval != -EFAULT))
1686 goto retry;
1687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 return ret;
1689}
1690
1691static int futex_close(struct inode *inode, struct file *filp)
1692{
1693 struct futex_q *q = filp->private_data;
1694
1695 unqueue_me(q);
1696 kfree(q);
Ingo Molnare2970f22006-06-27 02:54:47 -07001697
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 return 0;
1699}
1700
1701/* This is one-shot: once it's gone off you need a new fd */
1702static unsigned int futex_poll(struct file *filp,
1703 struct poll_table_struct *wait)
1704{
1705 struct futex_q *q = filp->private_data;
1706 int ret = 0;
1707
1708 poll_wait(filp, &q->waiters, wait);
1709
1710 /*
Pierre Peifferec92d082007-05-09 02:35:00 -07001711 * plist_node_empty() is safe here without any lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 * q->lock_ptr != 0 is not safe, because of ordering against wakeup.
1713 */
Pierre Peifferec92d082007-05-09 02:35:00 -07001714 if (plist_node_empty(&q->list))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 ret = POLLIN | POLLRDNORM;
1716
1717 return ret;
1718}
1719
Helge Deller15ad7cd2006-12-06 20:40:36 -08001720static const struct file_operations futex_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 .release = futex_close,
1722 .poll = futex_poll,
1723};
1724
1725/*
1726 * Signal allows caller to avoid the race which would occur if they
1727 * set the sigio stuff up afterwards.
1728 */
Ingo Molnare2970f22006-06-27 02:54:47 -07001729static int futex_fd(u32 __user *uaddr, int signal)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
1731 struct futex_q *q;
1732 struct file *filp;
1733 int ret, err;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001734 struct rw_semaphore *fshared;
Andrew Morton19c6b6e2006-11-02 22:07:17 -08001735 static unsigned long printk_interval;
1736
1737 if (printk_timed_ratelimit(&printk_interval, 60 * 60 * 1000)) {
1738 printk(KERN_WARNING "Process `%s' used FUTEX_FD, which "
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001739 "will be removed from the kernel in June 2007\n",
1740 current->comm);
Andrew Morton19c6b6e2006-11-02 22:07:17 -08001741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
1743 ret = -EINVAL;
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001744 if (!valid_signal(signal))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 goto out;
1746
1747 ret = get_unused_fd();
1748 if (ret < 0)
1749 goto out;
1750 filp = get_empty_filp();
1751 if (!filp) {
1752 put_unused_fd(ret);
1753 ret = -ENFILE;
1754 goto out;
1755 }
1756 filp->f_op = &futex_fops;
Josef "Jeff" Sipekf3a43f32006-12-08 02:36:43 -08001757 filp->f_path.mnt = mntget(futex_mnt);
1758 filp->f_path.dentry = dget(futex_mnt->mnt_root);
1759 filp->f_mapping = filp->f_path.dentry->d_inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
1761 if (signal) {
Eric W. Biederman609d7fa2006-10-02 02:17:15 -07001762 err = __f_setown(filp, task_pid(current), PIDTYPE_PID, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 if (err < 0) {
Pekka Enberg39ed3fd2005-09-06 15:17:44 -07001764 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 }
1766 filp->f_owner.signum = signal;
1767 }
1768
1769 q = kmalloc(sizeof(*q), GFP_KERNEL);
1770 if (!q) {
Pekka Enberg39ed3fd2005-09-06 15:17:44 -07001771 err = -ENOMEM;
1772 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001774 q->pi_state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001776 fshared = &current->mm->mmap_sem;
1777 down_read(fshared);
1778 err = get_futex_key(uaddr, fshared, &q->key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
1780 if (unlikely(err != 0)) {
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001781 up_read(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 kfree(q);
Pekka Enberg39ed3fd2005-09-06 15:17:44 -07001783 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 }
1785
1786 /*
1787 * queue_me() must be called before releasing mmap_sem, because
1788 * key->shared.inode needs to be referenced while holding it.
1789 */
1790 filp->private_data = q;
1791
1792 queue_me(q, ret, filp);
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001793 up_read(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
1795 /* Now we map fd to filp, so userspace can access it */
1796 fd_install(ret, filp);
1797out:
1798 return ret;
Pekka Enberg39ed3fd2005-09-06 15:17:44 -07001799error:
1800 put_unused_fd(ret);
1801 put_filp(filp);
1802 ret = err;
1803 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804}
1805
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001806/*
1807 * Support for robust futexes: the kernel cleans up held futexes at
1808 * thread exit time.
1809 *
1810 * Implementation: user-space maintains a per-thread list of locks it
1811 * is holding. Upon do_exit(), the kernel carefully walks this list,
1812 * and marks all locks that are owned by this thread with the
Ingo Molnarc87e2832006-06-27 02:54:58 -07001813 * FUTEX_OWNER_DIED bit, and wakes up a waiter (if any). The list is
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001814 * always manipulated with the lock held, so the list is private and
1815 * per-thread. Userspace also maintains a per-thread 'list_op_pending'
1816 * field, to allow the kernel to clean up if the thread dies after
1817 * acquiring the lock, but just before it could have added itself to
1818 * the list. There can only be one such pending lock.
1819 */
1820
1821/**
1822 * sys_set_robust_list - set the robust-futex list head of a task
1823 * @head: pointer to the list-head
1824 * @len: length of the list-head, as userspace expects
1825 */
1826asmlinkage long
1827sys_set_robust_list(struct robust_list_head __user *head,
1828 size_t len)
1829{
1830 /*
1831 * The kernel knows only one size for now:
1832 */
1833 if (unlikely(len != sizeof(*head)))
1834 return -EINVAL;
1835
1836 current->robust_list = head;
1837
1838 return 0;
1839}
1840
1841/**
1842 * sys_get_robust_list - get the robust-futex list head of a task
1843 * @pid: pid of the process [zero for current task]
1844 * @head_ptr: pointer to a list-head pointer, the kernel fills it in
1845 * @len_ptr: pointer to a length field, the kernel fills in the header size
1846 */
1847asmlinkage long
Al Viroba46df92006-10-10 22:46:07 +01001848sys_get_robust_list(int pid, struct robust_list_head __user * __user *head_ptr,
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001849 size_t __user *len_ptr)
1850{
Al Viroba46df92006-10-10 22:46:07 +01001851 struct robust_list_head __user *head;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001852 unsigned long ret;
1853
1854 if (!pid)
1855 head = current->robust_list;
1856 else {
1857 struct task_struct *p;
1858
1859 ret = -ESRCH;
Oleg Nesterovaaa2a972006-09-29 02:00:55 -07001860 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001861 p = find_task_by_pid_ns(pid,
1862 current->nsproxy->pid_ns);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001863 if (!p)
1864 goto err_unlock;
1865 ret = -EPERM;
1866 if ((current->euid != p->euid) && (current->euid != p->uid) &&
1867 !capable(CAP_SYS_PTRACE))
1868 goto err_unlock;
1869 head = p->robust_list;
Oleg Nesterovaaa2a972006-09-29 02:00:55 -07001870 rcu_read_unlock();
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001871 }
1872
1873 if (put_user(sizeof(*head), len_ptr))
1874 return -EFAULT;
1875 return put_user(head, head_ptr);
1876
1877err_unlock:
Oleg Nesterovaaa2a972006-09-29 02:00:55 -07001878 rcu_read_unlock();
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001879
1880 return ret;
1881}
1882
1883/*
1884 * Process a futex-list entry, check whether it's owned by the
1885 * dying task, and do notification if so:
1886 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001887int handle_futex_death(u32 __user *uaddr, struct task_struct *curr, int pi)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001888{
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001889 u32 uval, nval, mval;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001890
Ingo Molnar8f17d3a2006-03-27 01:16:27 -08001891retry:
1892 if (get_user(uval, uaddr))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001893 return -1;
1894
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001895 if ((uval & FUTEX_TID_MASK) == task_pid_vnr(curr)) {
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001896 /*
1897 * Ok, this dying thread is truly holding a futex
1898 * of interest. Set the OWNER_DIED bit atomically
1899 * via cmpxchg, and if the value had FUTEX_WAITERS
1900 * set, wake up a waiter (if any). (We have to do a
1901 * futex_wake() even if OWNER_DIED is already set -
1902 * to handle the rare but possible case of recursive
1903 * thread-death.) The rest of the cleanup is done in
1904 * userspace.
1905 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001906 mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
1907 nval = futex_atomic_cmpxchg_inatomic(uaddr, uval, mval);
1908
Ingo Molnarc87e2832006-06-27 02:54:58 -07001909 if (nval == -EFAULT)
1910 return -1;
1911
1912 if (nval != uval)
Ingo Molnar8f17d3a2006-03-27 01:16:27 -08001913 goto retry;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001914
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001915 /*
1916 * Wake robust non-PI futexes here. The wakeup of
1917 * PI futexes happens in exit_pi_state():
1918 */
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001919 if (!pi && (uval & FUTEX_WAITERS))
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001920 futex_wake(uaddr, &curr->mm->mmap_sem, 1);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001921 }
1922 return 0;
1923}
1924
1925/*
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001926 * Fetch a robust-list pointer. Bit 0 signals PI futexes:
1927 */
1928static inline int fetch_robust_entry(struct robust_list __user **entry,
Al Viroba46df92006-10-10 22:46:07 +01001929 struct robust_list __user * __user *head,
1930 int *pi)
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001931{
1932 unsigned long uentry;
1933
Al Viroba46df92006-10-10 22:46:07 +01001934 if (get_user(uentry, (unsigned long __user *)head))
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001935 return -EFAULT;
1936
Al Viroba46df92006-10-10 22:46:07 +01001937 *entry = (void __user *)(uentry & ~1UL);
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001938 *pi = uentry & 1;
1939
1940 return 0;
1941}
1942
1943/*
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001944 * Walk curr->robust_list (very carefully, it's a userspace list!)
1945 * and mark any locks found there dead, and notify any waiters.
1946 *
1947 * We silently return on any sign of list-walking problem.
1948 */
1949void exit_robust_list(struct task_struct *curr)
1950{
1951 struct robust_list_head __user *head = curr->robust_list;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07001952 struct robust_list __user *entry, *next_entry, *pending;
1953 unsigned int limit = ROBUST_LIST_LIMIT, pi, next_pi, pip;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001954 unsigned long futex_offset;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07001955 int rc;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001956
1957 /*
1958 * Fetch the list head (which was registered earlier, via
1959 * sys_set_robust_list()):
1960 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001961 if (fetch_robust_entry(&entry, &head->list.next, &pi))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001962 return;
1963 /*
1964 * Fetch the relative futex offset:
1965 */
1966 if (get_user(futex_offset, &head->futex_offset))
1967 return;
1968 /*
1969 * Fetch any possibly pending lock-add first, and handle it
1970 * if it exists:
1971 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001972 if (fetch_robust_entry(&pending, &head->list_op_pending, &pip))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001973 return;
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001974
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07001975 next_entry = NULL; /* avoid warning with gcc */
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001976 while (entry != &head->list) {
1977 /*
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07001978 * Fetch the next entry in the list before calling
1979 * handle_futex_death:
1980 */
1981 rc = fetch_robust_entry(&next_entry, &entry->next, &next_pi);
1982 /*
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001983 * A pending lock might already be on the list, so
Ingo Molnarc87e2832006-06-27 02:54:58 -07001984 * don't process it twice:
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001985 */
1986 if (entry != pending)
Al Viroba46df92006-10-10 22:46:07 +01001987 if (handle_futex_death((void __user *)entry + futex_offset,
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001988 curr, pi))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001989 return;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07001990 if (rc)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001991 return;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07001992 entry = next_entry;
1993 pi = next_pi;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001994 /*
1995 * Avoid excessively long or circular lists:
1996 */
1997 if (!--limit)
1998 break;
1999
2000 cond_resched();
2001 }
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -07002002
2003 if (pending)
2004 handle_futex_death((void __user *)pending + futex_offset,
2005 curr, pip);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002006}
2007
Pierre Peifferc19384b2007-05-09 02:35:02 -07002008long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
Ingo Molnare2970f22006-06-27 02:54:47 -07002009 u32 __user *uaddr2, u32 val2, u32 val3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
2011 int ret;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002012 int cmd = op & FUTEX_CMD_MASK;
2013 struct rw_semaphore *fshared = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002015 if (!(op & FUTEX_PRIVATE_FLAG))
2016 fshared = &current->mm->mmap_sem;
2017
2018 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 case FUTEX_WAIT:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002020 ret = futex_wait(uaddr, fshared, val, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 break;
2022 case FUTEX_WAKE:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002023 ret = futex_wake(uaddr, fshared, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 break;
2025 case FUTEX_FD:
2026 /* non-zero val means F_SETOWN(getpid()) & F_SETSIG(val) */
2027 ret = futex_fd(uaddr, val);
2028 break;
2029 case FUTEX_REQUEUE:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002030 ret = futex_requeue(uaddr, fshared, uaddr2, val, val2, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 break;
2032 case FUTEX_CMP_REQUEUE:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002033 ret = futex_requeue(uaddr, fshared, uaddr2, val, val2, &val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 break;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07002035 case FUTEX_WAKE_OP:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002036 ret = futex_wake_op(uaddr, fshared, uaddr2, val, val2, val3);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07002037 break;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002038 case FUTEX_LOCK_PI:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002039 ret = futex_lock_pi(uaddr, fshared, val, timeout, 0);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002040 break;
2041 case FUTEX_UNLOCK_PI:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002042 ret = futex_unlock_pi(uaddr, fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002043 break;
2044 case FUTEX_TRYLOCK_PI:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002045 ret = futex_lock_pi(uaddr, fshared, 0, timeout, 1);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002046 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 default:
2048 ret = -ENOSYS;
2049 }
2050 return ret;
2051}
2052
2053
Ingo Molnare2970f22006-06-27 02:54:47 -07002054asmlinkage long sys_futex(u32 __user *uaddr, int op, u32 val,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 struct timespec __user *utime, u32 __user *uaddr2,
Ingo Molnare2970f22006-06-27 02:54:47 -07002056 u32 val3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
Pierre Peifferc19384b2007-05-09 02:35:02 -07002058 struct timespec ts;
2059 ktime_t t, *tp = NULL;
Ingo Molnare2970f22006-06-27 02:54:47 -07002060 u32 val2 = 0;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002061 int cmd = op & FUTEX_CMD_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002063 if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI)) {
Pierre Peifferc19384b2007-05-09 02:35:02 -07002064 if (copy_from_user(&ts, utime, sizeof(ts)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 return -EFAULT;
Pierre Peifferc19384b2007-05-09 02:35:02 -07002066 if (!timespec_valid(&ts))
Thomas Gleixner9741ef92006-03-31 02:31:32 -08002067 return -EINVAL;
Pierre Peifferc19384b2007-05-09 02:35:02 -07002068
2069 t = timespec_to_ktime(ts);
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002070 if (cmd == FUTEX_WAIT)
Pierre Peifferc19384b2007-05-09 02:35:02 -07002071 t = ktime_add(ktime_get(), t);
2072 tp = &t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 }
2074 /*
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002075 * requeue parameter in 'utime' if cmd == FUTEX_REQUEUE.
Andreas Schwabf54f0982007-07-31 00:38:51 -07002076 * number of waiters to wake in 'utime' if cmd == FUTEX_WAKE_OP.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 */
Andreas Schwabf54f0982007-07-31 00:38:51 -07002078 if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE ||
2079 cmd == FUTEX_WAKE_OP)
Ingo Molnare2970f22006-06-27 02:54:47 -07002080 val2 = (u32) (unsigned long) utime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Pierre Peifferc19384b2007-05-09 02:35:02 -07002082 return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083}
2084
David Howells454e2392006-06-23 02:02:57 -07002085static int futexfs_get_sb(struct file_system_type *fs_type,
2086 int flags, const char *dev_name, void *data,
2087 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
Andrey Mirkinfd5eea42007-10-16 23:30:13 -07002089 return get_sb_pseudo(fs_type, "futex", NULL, FUTEXFS_SUPER_MAGIC, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090}
2091
2092static struct file_system_type futex_fs_type = {
2093 .name = "futexfs",
2094 .get_sb = futexfs_get_sb,
2095 .kill_sb = kill_anon_super,
2096};
2097
2098static int __init init(void)
2099{
Akinobu Mita95362fa2006-12-06 20:39:03 -08002100 int i = register_filesystem(&futex_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Akinobu Mita95362fa2006-12-06 20:39:03 -08002102 if (i)
2103 return i;
2104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 futex_mnt = kern_mount(&futex_fs_type);
Akinobu Mita95362fa2006-12-06 20:39:03 -08002106 if (IS_ERR(futex_mnt)) {
2107 unregister_filesystem(&futex_fs_type);
2108 return PTR_ERR(futex_mnt);
2109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
2111 for (i = 0; i < ARRAY_SIZE(futex_queues); i++) {
Pierre Peifferec92d082007-05-09 02:35:00 -07002112 plist_head_init(&futex_queues[i].chain, &futex_queues[i].lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 spin_lock_init(&futex_queues[i].lock);
2114 }
2115 return 0;
2116}
2117__initcall(init);