blob: 5c3f45d07c534a7ecc07f50c427e8390809cf165 [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>
Jakub Jelinek4732efb2005-09-06 15:16:25 -070055#include <asm/futex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Ingo Molnarc87e2832006-06-27 02:54:58 -070057#include "rtmutex_common.h"
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#define FUTEX_HASHBITS (CONFIG_BASE_SMALL ? 4 : 8)
60
61/*
Ingo Molnarc87e2832006-06-27 02:54:58 -070062 * Priority Inheritance state:
63 */
64struct futex_pi_state {
65 /*
66 * list of 'owned' pi_state instances - these have to be
67 * cleaned up in do_exit() if the task exits prematurely:
68 */
69 struct list_head list;
70
71 /*
72 * The PI object:
73 */
74 struct rt_mutex pi_mutex;
75
76 struct task_struct *owner;
77 atomic_t refcount;
78
79 union futex_key key;
80};
81
82/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 * We use this hashed waitqueue instead of a normal wait_queue_t, so
84 * we can wake only the relevant ones (hashed queues may be shared).
85 *
86 * A futex_q has a woken state, just like tasks have TASK_RUNNING.
Pierre Peifferec92d082007-05-09 02:35:00 -070087 * It is considered woken when plist_node_empty(&q->list) || q->lock_ptr == 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * The order of wakup is always to make the first condition true, then
89 * wake up q->waiters, then make the second condition true.
90 */
91struct futex_q {
Pierre Peifferec92d082007-05-09 02:35:00 -070092 struct plist_node list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 wait_queue_head_t waiters;
94
Ingo Molnare2970f22006-06-27 02:54:47 -070095 /* Which hash list lock to use: */
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 spinlock_t *lock_ptr;
97
Ingo Molnare2970f22006-06-27 02:54:47 -070098 /* Key which the futex is hashed on: */
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 union futex_key key;
100
Ingo Molnare2970f22006-06-27 02:54:47 -0700101 /* For fd, sigio sent using these: */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 int fd;
103 struct file *filp;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700104
105 /* Optional priority inheritance state: */
106 struct futex_pi_state *pi_state;
107 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108};
109
110/*
111 * Split the global futex_lock into every hash list lock.
112 */
113struct futex_hash_bucket {
Pierre Peifferec92d082007-05-09 02:35:00 -0700114 spinlock_t lock;
115 struct plist_head chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116};
117
118static struct futex_hash_bucket futex_queues[1<<FUTEX_HASHBITS];
119
120/* Futex-fs vfsmount entry: */
121static struct vfsmount *futex_mnt;
122
123/*
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700124 * Take mm->mmap_sem, when futex is shared
125 */
126static inline void futex_lock_mm(struct rw_semaphore *fshared)
127{
128 if (fshared)
129 down_read(fshared);
130}
131
132/*
133 * Release mm->mmap_sem, when the futex is shared
134 */
135static inline void futex_unlock_mm(struct rw_semaphore *fshared)
136{
137 if (fshared)
138 up_read(fshared);
139}
140
141/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 * We hash on the keys returned from get_futex_key (see below).
143 */
144static struct futex_hash_bucket *hash_futex(union futex_key *key)
145{
146 u32 hash = jhash2((u32*)&key->both.word,
147 (sizeof(key->both.word)+sizeof(key->both.ptr))/4,
148 key->both.offset);
149 return &futex_queues[hash & ((1 << FUTEX_HASHBITS)-1)];
150}
151
152/*
153 * Return 1 if two futex_keys are equal, 0 otherwise.
154 */
155static inline int match_futex(union futex_key *key1, union futex_key *key2)
156{
157 return (key1->both.word == key2->both.word
158 && key1->both.ptr == key2->both.ptr
159 && key1->both.offset == key2->both.offset);
160}
161
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700162/**
163 * get_futex_key - Get parameters which are the keys for a futex.
164 * @uaddr: virtual address of the futex
165 * @shared: NULL for a PROCESS_PRIVATE futex,
166 * &current->mm->mmap_sem for a PROCESS_SHARED futex
167 * @key: address where result is stored.
168 *
169 * Returns a negative error code or 0
170 * The key words are stored in *key on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 *
Josef "Jeff" Sipekf3a43f32006-12-08 02:36:43 -0800172 * For shared mappings, it's (page->index, vma->vm_file->f_path.dentry->d_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 * offset_within_page). For private mappings, it's (uaddr, current->mm).
174 * We can usually work out the index without swapping in the page.
175 *
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700176 * fshared is NULL for PROCESS_PRIVATE futexes
177 * For other futexes, it points to &current->mm->mmap_sem and
178 * caller must have taken the reader lock. but NOT any spinlocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700180int get_futex_key(u32 __user *uaddr, struct rw_semaphore *fshared,
181 union futex_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Ingo Molnare2970f22006-06-27 02:54:47 -0700183 unsigned long address = (unsigned long)uaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 struct mm_struct *mm = current->mm;
185 struct vm_area_struct *vma;
186 struct page *page;
187 int err;
188
189 /*
190 * The futex address must be "naturally" aligned.
191 */
Ingo Molnare2970f22006-06-27 02:54:47 -0700192 key->both.offset = address % PAGE_SIZE;
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700193 if (unlikely((address % sizeof(u32)) != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return -EINVAL;
Ingo Molnare2970f22006-06-27 02:54:47 -0700195 address -= key->both.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 /*
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700198 * PROCESS_PRIVATE futexes are fast.
199 * As the mm cannot disappear under us and the 'key' only needs
200 * virtual address, we dont even have to find the underlying vma.
201 * Note : We do have to check 'uaddr' is a valid user address,
202 * but access_ok() should be faster than find_vma()
203 */
204 if (!fshared) {
205 if (unlikely(!access_ok(VERIFY_WRITE, uaddr, sizeof(u32))))
206 return -EFAULT;
207 key->private.mm = mm;
208 key->private.address = address;
209 return 0;
210 }
211 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 * The futex is hashed differently depending on whether
213 * it's in a shared or private mapping. So check vma first.
214 */
Ingo Molnare2970f22006-06-27 02:54:47 -0700215 vma = find_extend_vma(mm, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (unlikely(!vma))
217 return -EFAULT;
218
219 /*
220 * Permissions.
221 */
222 if (unlikely((vma->vm_flags & (VM_IO|VM_READ)) != VM_READ))
223 return (vma->vm_flags & VM_IO) ? -EPERM : -EACCES;
224
225 /*
226 * Private mappings are handled in a simple way.
227 *
228 * NOTE: When userspace waits on a MAP_SHARED mapping, even if
229 * it's a read-only handle, it's expected that futexes attach to
230 * the object not the particular process. Therefore we use
231 * VM_MAYSHARE here, not VM_SHARED which is restricted to shared
232 * mappings of _writable_ handles.
233 */
234 if (likely(!(vma->vm_flags & VM_MAYSHARE))) {
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700235 key->both.offset |= FUT_OFF_MMSHARED; /* reference taken on mm */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 key->private.mm = mm;
Ingo Molnare2970f22006-06-27 02:54:47 -0700237 key->private.address = address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return 0;
239 }
240
241 /*
242 * Linear file mappings are also simple.
243 */
Josef "Jeff" Sipekf3a43f32006-12-08 02:36:43 -0800244 key->shared.inode = vma->vm_file->f_path.dentry->d_inode;
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700245 key->both.offset |= FUT_OFF_INODE; /* inode-based key. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (likely(!(vma->vm_flags & VM_NONLINEAR))) {
Ingo Molnare2970f22006-06-27 02:54:47 -0700247 key->shared.pgoff = (((address - vma->vm_start) >> PAGE_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 + vma->vm_pgoff);
249 return 0;
250 }
251
252 /*
253 * We could walk the page table to read the non-linear
254 * pte, and get the page index without fetching the page
255 * from swap. But that's a lot of code to duplicate here
256 * for a rare case, so we simply fetch the page.
257 */
Ingo Molnare2970f22006-06-27 02:54:47 -0700258 err = get_user_pages(current, mm, address, 1, 0, 0, &page, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (err >= 0) {
260 key->shared.pgoff =
261 page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
262 put_page(page);
263 return 0;
264 }
265 return err;
266}
Rusty Russell9adef582007-05-08 00:26:42 -0700267EXPORT_SYMBOL_GPL(get_futex_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269/*
270 * Take a reference to the resource addressed by a key.
271 * Can be called while holding spinlocks.
272 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 */
Rusty Russell9adef582007-05-08 00:26:42 -0700274inline void get_futex_key_refs(union futex_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700276 if (key->both.ptr == 0)
277 return;
278 switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
279 case FUT_OFF_INODE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 atomic_inc(&key->shared.inode->i_count);
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700281 break;
282 case FUT_OFF_MMSHARED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 atomic_inc(&key->private.mm->mm_count);
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700284 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286}
Rusty Russell9adef582007-05-08 00:26:42 -0700287EXPORT_SYMBOL_GPL(get_futex_key_refs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289/*
290 * Drop a reference to the resource addressed by a key.
291 * The hash bucket spinlock must not be held.
292 */
Rusty Russell9adef582007-05-08 00:26:42 -0700293void drop_futex_key_refs(union futex_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700295 if (key->both.ptr == 0)
296 return;
297 switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
298 case FUT_OFF_INODE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 iput(key->shared.inode);
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700300 break;
301 case FUT_OFF_MMSHARED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 mmdrop(key->private.mm);
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700303 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305}
Rusty Russell9adef582007-05-08 00:26:42 -0700306EXPORT_SYMBOL_GPL(drop_futex_key_refs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700308static u32 cmpxchg_futex_value_locked(u32 __user *uaddr, u32 uval, u32 newval)
309{
310 u32 curval;
311
312 pagefault_disable();
313 curval = futex_atomic_cmpxchg_inatomic(uaddr, uval, newval);
314 pagefault_enable();
315
316 return curval;
317}
318
319static int get_futex_value_locked(u32 *dest, u32 __user *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 int ret;
322
Peter Zijlstraa8663742006-12-06 20:32:20 -0800323 pagefault_disable();
Ingo Molnare2970f22006-06-27 02:54:47 -0700324 ret = __copy_from_user_inatomic(dest, from, sizeof(u32));
Peter Zijlstraa8663742006-12-06 20:32:20 -0800325 pagefault_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 return ret ? -EFAULT : 0;
328}
329
330/*
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700331 * Fault handling.
332 * if fshared is non NULL, current->mm->mmap_sem is already held
Ingo Molnarc87e2832006-06-27 02:54:58 -0700333 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700334static int futex_handle_fault(unsigned long address,
335 struct rw_semaphore *fshared, int attempt)
Ingo Molnarc87e2832006-06-27 02:54:58 -0700336{
337 struct vm_area_struct * vma;
338 struct mm_struct *mm = current->mm;
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700339 int ret = -EFAULT;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700340
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700341 if (attempt > 2)
342 return ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700343
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700344 if (!fshared)
345 down_read(&mm->mmap_sem);
346 vma = find_vma(mm, address);
347 if (vma && address >= vma->vm_start &&
348 (vma->vm_flags & VM_WRITE)) {
349 switch (handle_mm_fault(mm, vma, address, 1)) {
350 case VM_FAULT_MINOR:
351 ret = 0;
352 current->min_flt++;
353 break;
354 case VM_FAULT_MAJOR:
355 ret = 0;
356 current->maj_flt++;
357 break;
358 }
Ingo Molnarc87e2832006-06-27 02:54:58 -0700359 }
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700360 if (!fshared)
361 up_read(&mm->mmap_sem);
362 return ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700363}
364
365/*
366 * PI code:
367 */
368static int refill_pi_state_cache(void)
369{
370 struct futex_pi_state *pi_state;
371
372 if (likely(current->pi_state_cache))
373 return 0;
374
Burman Yan4668edc2006-12-06 20:38:51 -0800375 pi_state = kzalloc(sizeof(*pi_state), GFP_KERNEL);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700376
377 if (!pi_state)
378 return -ENOMEM;
379
Ingo Molnarc87e2832006-06-27 02:54:58 -0700380 INIT_LIST_HEAD(&pi_state->list);
381 /* pi_mutex gets initialized later */
382 pi_state->owner = NULL;
383 atomic_set(&pi_state->refcount, 1);
384
385 current->pi_state_cache = pi_state;
386
387 return 0;
388}
389
390static struct futex_pi_state * alloc_pi_state(void)
391{
392 struct futex_pi_state *pi_state = current->pi_state_cache;
393
394 WARN_ON(!pi_state);
395 current->pi_state_cache = NULL;
396
397 return pi_state;
398}
399
400static void free_pi_state(struct futex_pi_state *pi_state)
401{
402 if (!atomic_dec_and_test(&pi_state->refcount))
403 return;
404
405 /*
406 * If pi_state->owner is NULL, the owner is most probably dying
407 * and has cleaned up the pi_state already
408 */
409 if (pi_state->owner) {
410 spin_lock_irq(&pi_state->owner->pi_lock);
411 list_del_init(&pi_state->list);
412 spin_unlock_irq(&pi_state->owner->pi_lock);
413
414 rt_mutex_proxy_unlock(&pi_state->pi_mutex, pi_state->owner);
415 }
416
417 if (current->pi_state_cache)
418 kfree(pi_state);
419 else {
420 /*
421 * pi_state->list is already empty.
422 * clear pi_state->owner.
423 * refcount is at 0 - put it back to 1.
424 */
425 pi_state->owner = NULL;
426 atomic_set(&pi_state->refcount, 1);
427 current->pi_state_cache = pi_state;
428 }
429}
430
431/*
432 * Look up the task based on what TID userspace gave us.
433 * We dont trust it.
434 */
435static struct task_struct * futex_find_get_task(pid_t pid)
436{
437 struct task_struct *p;
438
Oleg Nesterovd359b542006-09-29 02:00:55 -0700439 rcu_read_lock();
Ingo Molnarc87e2832006-06-27 02:54:58 -0700440 p = find_task_by_pid(pid);
Thomas Gleixnera06381f2007-06-23 11:48:40 +0200441
442 if (!p || ((current->euid != p->euid) && (current->euid != p->uid)))
443 p = ERR_PTR(-ESRCH);
444 else
445 get_task_struct(p);
446
Oleg Nesterovd359b542006-09-29 02:00:55 -0700447 rcu_read_unlock();
Ingo Molnarc87e2832006-06-27 02:54:58 -0700448
449 return p;
450}
451
452/*
453 * This task is holding PI mutexes at exit time => bad.
454 * Kernel cleans up PI-state, but userspace is likely hosed.
455 * (Robust-futex cleanup is separate and might save the day for userspace.)
456 */
457void exit_pi_state_list(struct task_struct *curr)
458{
Ingo Molnarc87e2832006-06-27 02:54:58 -0700459 struct list_head *next, *head = &curr->pi_state_list;
460 struct futex_pi_state *pi_state;
Ingo Molnar627371d2006-07-29 05:16:20 +0200461 struct futex_hash_bucket *hb;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700462 union futex_key key;
463
464 /*
465 * We are a ZOMBIE and nobody can enqueue itself on
466 * pi_state_list anymore, but we have to be careful
Ingo Molnar627371d2006-07-29 05:16:20 +0200467 * versus waiters unqueueing themselves:
Ingo Molnarc87e2832006-06-27 02:54:58 -0700468 */
469 spin_lock_irq(&curr->pi_lock);
470 while (!list_empty(head)) {
471
472 next = head->next;
473 pi_state = list_entry(next, struct futex_pi_state, list);
474 key = pi_state->key;
Ingo Molnar627371d2006-07-29 05:16:20 +0200475 hb = hash_futex(&key);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700476 spin_unlock_irq(&curr->pi_lock);
477
Ingo Molnarc87e2832006-06-27 02:54:58 -0700478 spin_lock(&hb->lock);
479
480 spin_lock_irq(&curr->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +0200481 /*
482 * We dropped the pi-lock, so re-check whether this
483 * task still owns the PI-state:
484 */
Ingo Molnarc87e2832006-06-27 02:54:58 -0700485 if (head->next != next) {
486 spin_unlock(&hb->lock);
487 continue;
488 }
489
Ingo Molnarc87e2832006-06-27 02:54:58 -0700490 WARN_ON(pi_state->owner != curr);
Ingo Molnar627371d2006-07-29 05:16:20 +0200491 WARN_ON(list_empty(&pi_state->list));
492 list_del_init(&pi_state->list);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700493 pi_state->owner = NULL;
494 spin_unlock_irq(&curr->pi_lock);
495
496 rt_mutex_unlock(&pi_state->pi_mutex);
497
498 spin_unlock(&hb->lock);
499
500 spin_lock_irq(&curr->pi_lock);
501 }
502 spin_unlock_irq(&curr->pi_lock);
503}
504
505static int
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700506lookup_pi_state(u32 uval, struct futex_hash_bucket *hb,
507 union futex_key *key, struct futex_pi_state **ps)
Ingo Molnarc87e2832006-06-27 02:54:58 -0700508{
509 struct futex_pi_state *pi_state = NULL;
510 struct futex_q *this, *next;
Pierre Peifferec92d082007-05-09 02:35:00 -0700511 struct plist_head *head;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700512 struct task_struct *p;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700513 pid_t pid = uval & FUTEX_TID_MASK;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700514
515 head = &hb->chain;
516
Pierre Peifferec92d082007-05-09 02:35:00 -0700517 plist_for_each_entry_safe(this, next, head, list) {
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700518 if (match_futex(&this->key, key)) {
Ingo Molnarc87e2832006-06-27 02:54:58 -0700519 /*
520 * Another waiter already exists - bump up
521 * the refcount and return its pi_state:
522 */
523 pi_state = this->pi_state;
Thomas Gleixner06a9ec22006-07-10 04:44:30 -0700524 /*
525 * Userspace might have messed up non PI and PI futexes
526 */
527 if (unlikely(!pi_state))
528 return -EINVAL;
529
Ingo Molnar627371d2006-07-29 05:16:20 +0200530 WARN_ON(!atomic_read(&pi_state->refcount));
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700531 WARN_ON(pid && pi_state->owner &&
532 pi_state->owner->pid != pid);
Ingo Molnar627371d2006-07-29 05:16:20 +0200533
Ingo Molnarc87e2832006-06-27 02:54:58 -0700534 atomic_inc(&pi_state->refcount);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700535 *ps = pi_state;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700536
537 return 0;
538 }
539 }
540
541 /*
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200542 * We are the first waiter - try to look up the real owner and attach
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700543 * the new pi_state to it, but bail out when TID = 0
Ingo Molnarc87e2832006-06-27 02:54:58 -0700544 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700545 if (!pid)
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200546 return -ESRCH;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700547 p = futex_find_get_task(pid);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700548 if (IS_ERR(p))
549 return PTR_ERR(p);
550
551 /*
552 * We need to look at the task state flags to figure out,
553 * whether the task is exiting. To protect against the do_exit
554 * change of the task flags, we do this protected by
555 * p->pi_lock:
556 */
557 spin_lock_irq(&p->pi_lock);
558 if (unlikely(p->flags & PF_EXITING)) {
559 /*
560 * The task is on the way out. When PF_EXITPIDONE is
561 * set, we know that the task has finished the
562 * cleanup:
563 */
564 int ret = (p->flags & PF_EXITPIDONE) ? -ESRCH : -EAGAIN;
565
566 spin_unlock_irq(&p->pi_lock);
567 put_task_struct(p);
568 return ret;
569 }
Ingo Molnarc87e2832006-06-27 02:54:58 -0700570
571 pi_state = alloc_pi_state();
572
573 /*
574 * Initialize the pi_mutex in locked state and make 'p'
575 * the owner of it:
576 */
577 rt_mutex_init_proxy_locked(&pi_state->pi_mutex, p);
578
579 /* Store the key for possible exit cleanups: */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700580 pi_state->key = *key;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700581
Ingo Molnar627371d2006-07-29 05:16:20 +0200582 WARN_ON(!list_empty(&pi_state->list));
Ingo Molnarc87e2832006-06-27 02:54:58 -0700583 list_add(&pi_state->list, &p->pi_state_list);
584 pi_state->owner = p;
585 spin_unlock_irq(&p->pi_lock);
586
587 put_task_struct(p);
588
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700589 *ps = pi_state;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700590
591 return 0;
592}
593
594/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 * The hash bucket lock must be held when this is called.
596 * Afterwards, the futex_q must not be accessed.
597 */
598static void wake_futex(struct futex_q *q)
599{
Pierre Peifferec92d082007-05-09 02:35:00 -0700600 plist_del(&q->list, &q->list.plist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (q->filp)
602 send_sigio(&q->filp->f_owner, q->fd, POLL_IN);
603 /*
604 * The lock in wake_up_all() is a crucial memory barrier after the
Pierre Peifferec92d082007-05-09 02:35:00 -0700605 * plist_del() and also before assigning to q->lock_ptr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 */
607 wake_up_all(&q->waiters);
608 /*
609 * The waiting task can free the futex_q as soon as this is written,
610 * without taking any locks. This must come last.
Andrew Morton8e311082005-12-23 19:54:46 -0800611 *
612 * A memory barrier is required here to prevent the following store
613 * to lock_ptr from getting ahead of the wakeup. Clearing the lock
614 * at the end of wake_up_all() does not prevent this store from
615 * moving.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 */
Ralf Baechleccdea2f2006-12-06 20:40:26 -0800617 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 q->lock_ptr = NULL;
619}
620
Ingo Molnarc87e2832006-06-27 02:54:58 -0700621static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this)
622{
623 struct task_struct *new_owner;
624 struct futex_pi_state *pi_state = this->pi_state;
625 u32 curval, newval;
626
627 if (!pi_state)
628 return -EINVAL;
629
Ingo Molnar217788672007-03-16 13:38:31 -0800630 spin_lock(&pi_state->pi_mutex.wait_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700631 new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);
632
633 /*
634 * This happens when we have stolen the lock and the original
635 * pending owner did not enqueue itself back on the rt_mutex.
636 * Thats not a tragedy. We know that way, that a lock waiter
637 * is on the fly. We make the futex_q waiter the pending owner.
638 */
639 if (!new_owner)
640 new_owner = this->task;
641
642 /*
643 * We pass it to the next owner. (The WAITERS bit is always
644 * kept enabled while there is PI state around. We must also
645 * preserve the owner died bit.)
646 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200647 if (!(uval & FUTEX_OWNER_DIED)) {
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700648 int ret = 0;
649
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200650 newval = FUTEX_WAITERS | new_owner->pid;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700651
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700652 curval = cmpxchg_futex_value_locked(uaddr, uval, newval);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700653
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200654 if (curval == -EFAULT)
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700655 ret = -EFAULT;
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200656 if (curval != uval)
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700657 ret = -EINVAL;
658 if (ret) {
659 spin_unlock(&pi_state->pi_mutex.wait_lock);
660 return ret;
661 }
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200662 }
Ingo Molnarc87e2832006-06-27 02:54:58 -0700663
Ingo Molnar627371d2006-07-29 05:16:20 +0200664 spin_lock_irq(&pi_state->owner->pi_lock);
665 WARN_ON(list_empty(&pi_state->list));
666 list_del_init(&pi_state->list);
667 spin_unlock_irq(&pi_state->owner->pi_lock);
668
669 spin_lock_irq(&new_owner->pi_lock);
670 WARN_ON(!list_empty(&pi_state->list));
Ingo Molnarc87e2832006-06-27 02:54:58 -0700671 list_add(&pi_state->list, &new_owner->pi_state_list);
672 pi_state->owner = new_owner;
Ingo Molnar627371d2006-07-29 05:16:20 +0200673 spin_unlock_irq(&new_owner->pi_lock);
674
Ingo Molnar217788672007-03-16 13:38:31 -0800675 spin_unlock(&pi_state->pi_mutex.wait_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700676 rt_mutex_unlock(&pi_state->pi_mutex);
677
678 return 0;
679}
680
681static int unlock_futex_pi(u32 __user *uaddr, u32 uval)
682{
683 u32 oldval;
684
685 /*
686 * There is no waiter, so we unlock the futex. The owner died
687 * bit has not to be preserved here. We are the owner:
688 */
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700689 oldval = cmpxchg_futex_value_locked(uaddr, uval, 0);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700690
691 if (oldval == -EFAULT)
692 return oldval;
693 if (oldval != uval)
694 return -EAGAIN;
695
696 return 0;
697}
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699/*
Ingo Molnar8b8f3192006-07-03 00:25:05 -0700700 * Express the locking dependencies for lockdep:
701 */
702static inline void
703double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
704{
705 if (hb1 <= hb2) {
706 spin_lock(&hb1->lock);
707 if (hb1 < hb2)
708 spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING);
709 } else { /* hb1 > hb2 */
710 spin_lock(&hb2->lock);
711 spin_lock_nested(&hb1->lock, SINGLE_DEPTH_NESTING);
712 }
713}
714
715/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 * Wake up all waiters hashed on the physical page that is mapped
717 * to this virtual address:
718 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700719static int futex_wake(u32 __user *uaddr, struct rw_semaphore *fshared,
720 int nr_wake)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Ingo Molnare2970f22006-06-27 02:54:47 -0700722 struct futex_hash_bucket *hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 struct futex_q *this, *next;
Pierre Peifferec92d082007-05-09 02:35:00 -0700724 struct plist_head *head;
Ingo Molnare2970f22006-06-27 02:54:47 -0700725 union futex_key key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 int ret;
727
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700728 futex_lock_mm(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700730 ret = get_futex_key(uaddr, fshared, &key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (unlikely(ret != 0))
732 goto out;
733
Ingo Molnare2970f22006-06-27 02:54:47 -0700734 hb = hash_futex(&key);
735 spin_lock(&hb->lock);
736 head = &hb->chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Pierre Peifferec92d082007-05-09 02:35:00 -0700738 plist_for_each_entry_safe(this, next, head, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (match_futex (&this->key, &key)) {
Ingo Molnared6f7b12006-07-01 04:35:46 -0700740 if (this->pi_state) {
741 ret = -EINVAL;
742 break;
743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 wake_futex(this);
745 if (++ret >= nr_wake)
746 break;
747 }
748 }
749
Ingo Molnare2970f22006-06-27 02:54:47 -0700750 spin_unlock(&hb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751out:
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700752 futex_unlock_mm(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return ret;
754}
755
756/*
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700757 * Wake up all waiters hashed on the physical page that is mapped
758 * to this virtual address:
759 */
Ingo Molnare2970f22006-06-27 02:54:47 -0700760static int
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700761futex_wake_op(u32 __user *uaddr1, struct rw_semaphore *fshared,
762 u32 __user *uaddr2,
Ingo Molnare2970f22006-06-27 02:54:47 -0700763 int nr_wake, int nr_wake2, int op)
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700764{
765 union futex_key key1, key2;
Ingo Molnare2970f22006-06-27 02:54:47 -0700766 struct futex_hash_bucket *hb1, *hb2;
Pierre Peifferec92d082007-05-09 02:35:00 -0700767 struct plist_head *head;
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700768 struct futex_q *this, *next;
769 int ret, op_ret, attempt = 0;
770
771retryfull:
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700772 futex_lock_mm(fshared);
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700773
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700774 ret = get_futex_key(uaddr1, fshared, &key1);
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700775 if (unlikely(ret != 0))
776 goto out;
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700777 ret = get_futex_key(uaddr2, fshared, &key2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700778 if (unlikely(ret != 0))
779 goto out;
780
Ingo Molnare2970f22006-06-27 02:54:47 -0700781 hb1 = hash_futex(&key1);
782 hb2 = hash_futex(&key2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700783
784retry:
Ingo Molnar8b8f3192006-07-03 00:25:05 -0700785 double_lock_hb(hb1, hb2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700786
Ingo Molnare2970f22006-06-27 02:54:47 -0700787 op_ret = futex_atomic_op_inuser(op, uaddr2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700788 if (unlikely(op_ret < 0)) {
Ingo Molnare2970f22006-06-27 02:54:47 -0700789 u32 dummy;
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700790
Ingo Molnare2970f22006-06-27 02:54:47 -0700791 spin_unlock(&hb1->lock);
792 if (hb1 != hb2)
793 spin_unlock(&hb2->lock);
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700794
David Howells7ee1dd32006-01-06 00:11:44 -0800795#ifndef CONFIG_MMU
Ingo Molnare2970f22006-06-27 02:54:47 -0700796 /*
797 * we don't get EFAULT from MMU faults if we don't have an MMU,
798 * but we might get them from range checking
799 */
David Howells7ee1dd32006-01-06 00:11:44 -0800800 ret = op_ret;
801 goto out;
802#endif
803
David Gibson796f8d92005-11-07 00:59:33 -0800804 if (unlikely(op_ret != -EFAULT)) {
805 ret = op_ret;
806 goto out;
807 }
808
Ingo Molnare2970f22006-06-27 02:54:47 -0700809 /*
810 * futex_atomic_op_inuser needs to both read and write
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700811 * *(int __user *)uaddr2, but we can't modify it
812 * non-atomically. Therefore, if get_user below is not
813 * enough, we need to handle the fault ourselves, while
Ingo Molnare2970f22006-06-27 02:54:47 -0700814 * still holding the mmap_sem.
815 */
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700816 if (attempt++) {
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700817 ret = futex_handle_fault((unsigned long)uaddr2,
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700818 fshared, attempt);
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700819 if (ret)
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700820 goto out;
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700821 goto retry;
822 }
823
Ingo Molnare2970f22006-06-27 02:54:47 -0700824 /*
825 * If we would have faulted, release mmap_sem,
826 * fault it in and start all over again.
827 */
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700828 futex_unlock_mm(fshared);
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700829
Ingo Molnare2970f22006-06-27 02:54:47 -0700830 ret = get_user(dummy, uaddr2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700831 if (ret)
832 return ret;
833
834 goto retryfull;
835 }
836
Ingo Molnare2970f22006-06-27 02:54:47 -0700837 head = &hb1->chain;
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700838
Pierre Peifferec92d082007-05-09 02:35:00 -0700839 plist_for_each_entry_safe(this, next, head, list) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700840 if (match_futex (&this->key, &key1)) {
841 wake_futex(this);
842 if (++ret >= nr_wake)
843 break;
844 }
845 }
846
847 if (op_ret > 0) {
Ingo Molnare2970f22006-06-27 02:54:47 -0700848 head = &hb2->chain;
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700849
850 op_ret = 0;
Pierre Peifferec92d082007-05-09 02:35:00 -0700851 plist_for_each_entry_safe(this, next, head, list) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700852 if (match_futex (&this->key, &key2)) {
853 wake_futex(this);
854 if (++op_ret >= nr_wake2)
855 break;
856 }
857 }
858 ret += op_ret;
859 }
860
Ingo Molnare2970f22006-06-27 02:54:47 -0700861 spin_unlock(&hb1->lock);
862 if (hb1 != hb2)
863 spin_unlock(&hb2->lock);
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700864out:
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700865 futex_unlock_mm(fshared);
866
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700867 return ret;
868}
869
870/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 * Requeue all waiters hashed on one physical page to another
872 * physical page.
873 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700874static int futex_requeue(u32 __user *uaddr1, struct rw_semaphore *fshared,
875 u32 __user *uaddr2,
Ingo Molnare2970f22006-06-27 02:54:47 -0700876 int nr_wake, int nr_requeue, u32 *cmpval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
878 union futex_key key1, key2;
Ingo Molnare2970f22006-06-27 02:54:47 -0700879 struct futex_hash_bucket *hb1, *hb2;
Pierre Peifferec92d082007-05-09 02:35:00 -0700880 struct plist_head *head1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 struct futex_q *this, *next;
882 int ret, drop_count = 0;
883
884 retry:
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700885 futex_lock_mm(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700887 ret = get_futex_key(uaddr1, fshared, &key1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 if (unlikely(ret != 0))
889 goto out;
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700890 ret = get_futex_key(uaddr2, fshared, &key2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if (unlikely(ret != 0))
892 goto out;
893
Ingo Molnare2970f22006-06-27 02:54:47 -0700894 hb1 = hash_futex(&key1);
895 hb2 = hash_futex(&key2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Ingo Molnar8b8f3192006-07-03 00:25:05 -0700897 double_lock_hb(hb1, hb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Ingo Molnare2970f22006-06-27 02:54:47 -0700899 if (likely(cmpval != NULL)) {
900 u32 curval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Ingo Molnare2970f22006-06-27 02:54:47 -0700902 ret = get_futex_value_locked(&curval, uaddr1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 if (unlikely(ret)) {
Ingo Molnare2970f22006-06-27 02:54:47 -0700905 spin_unlock(&hb1->lock);
906 if (hb1 != hb2)
907 spin_unlock(&hb2->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Ingo Molnare2970f22006-06-27 02:54:47 -0700909 /*
910 * If we would have faulted, release mmap_sem, fault
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 * it in and start all over again.
912 */
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700913 futex_unlock_mm(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Ingo Molnare2970f22006-06-27 02:54:47 -0700915 ret = get_user(curval, uaddr1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 if (!ret)
918 goto retry;
919
920 return ret;
921 }
Ingo Molnare2970f22006-06-27 02:54:47 -0700922 if (curval != *cmpval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 ret = -EAGAIN;
924 goto out_unlock;
925 }
926 }
927
Ingo Molnare2970f22006-06-27 02:54:47 -0700928 head1 = &hb1->chain;
Pierre Peifferec92d082007-05-09 02:35:00 -0700929 plist_for_each_entry_safe(this, next, head1, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (!match_futex (&this->key, &key1))
931 continue;
932 if (++ret <= nr_wake) {
933 wake_futex(this);
934 } else {
Sebastien Dugue59e0e0a2006-06-27 02:55:03 -0700935 /*
936 * If key1 and key2 hash to the same bucket, no need to
937 * requeue.
938 */
939 if (likely(head1 != &hb2->chain)) {
Pierre Peifferec92d082007-05-09 02:35:00 -0700940 plist_del(&this->list, &hb1->chain);
941 plist_add(&this->list, &hb2->chain);
Sebastien Dugue59e0e0a2006-06-27 02:55:03 -0700942 this->lock_ptr = &hb2->lock;
Pierre Peifferec92d082007-05-09 02:35:00 -0700943#ifdef CONFIG_DEBUG_PI_LIST
944 this->list.plist.lock = &hb2->lock;
945#endif
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 this->key = key2;
Rusty Russell9adef582007-05-08 00:26:42 -0700948 get_futex_key_refs(&key2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 drop_count++;
950
951 if (ret - nr_wake >= nr_requeue)
952 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
954 }
955
956out_unlock:
Ingo Molnare2970f22006-06-27 02:54:47 -0700957 spin_unlock(&hb1->lock);
958 if (hb1 != hb2)
959 spin_unlock(&hb2->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Rusty Russell9adef582007-05-08 00:26:42 -0700961 /* drop_futex_key_refs() must be called outside the spinlocks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 while (--drop_count >= 0)
Rusty Russell9adef582007-05-08 00:26:42 -0700963 drop_futex_key_refs(&key1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965out:
Thomas Gleixner36cf3b52007-07-15 23:41:20 -0700966 futex_unlock_mm(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return ret;
968}
969
970/* The key must be already stored in q->key. */
971static inline struct futex_hash_bucket *
972queue_lock(struct futex_q *q, int fd, struct file *filp)
973{
Ingo Molnare2970f22006-06-27 02:54:47 -0700974 struct futex_hash_bucket *hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 q->fd = fd;
977 q->filp = filp;
978
979 init_waitqueue_head(&q->waiters);
980
Rusty Russell9adef582007-05-08 00:26:42 -0700981 get_futex_key_refs(&q->key);
Ingo Molnare2970f22006-06-27 02:54:47 -0700982 hb = hash_futex(&q->key);
983 q->lock_ptr = &hb->lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Ingo Molnare2970f22006-06-27 02:54:47 -0700985 spin_lock(&hb->lock);
986 return hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
Ingo Molnare2970f22006-06-27 02:54:47 -0700989static inline void __queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
Pierre Peifferec92d082007-05-09 02:35:00 -0700991 int prio;
992
993 /*
994 * The priority used to register this element is
995 * - either the real thread-priority for the real-time threads
996 * (i.e. threads with a priority lower than MAX_RT_PRIO)
997 * - or MAX_RT_PRIO for non-RT threads.
998 * Thus, all RT-threads are woken first in priority order, and
999 * the others are woken last, in FIFO order.
1000 */
1001 prio = min(current->normal_prio, MAX_RT_PRIO);
1002
1003 plist_node_init(&q->list, prio);
1004#ifdef CONFIG_DEBUG_PI_LIST
1005 q->list.plist.lock = &hb->lock;
1006#endif
1007 plist_add(&q->list, &hb->chain);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001008 q->task = current;
Ingo Molnare2970f22006-06-27 02:54:47 -07001009 spin_unlock(&hb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010}
1011
1012static inline void
Ingo Molnare2970f22006-06-27 02:54:47 -07001013queue_unlock(struct futex_q *q, struct futex_hash_bucket *hb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
Ingo Molnare2970f22006-06-27 02:54:47 -07001015 spin_unlock(&hb->lock);
Rusty Russell9adef582007-05-08 00:26:42 -07001016 drop_futex_key_refs(&q->key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
1019/*
1020 * queue_me and unqueue_me must be called as a pair, each
1021 * exactly once. They are called with the hashed spinlock held.
1022 */
1023
1024/* The key must be already stored in q->key. */
1025static void queue_me(struct futex_q *q, int fd, struct file *filp)
1026{
Ingo Molnare2970f22006-06-27 02:54:47 -07001027 struct futex_hash_bucket *hb;
1028
1029 hb = queue_lock(q, fd, filp);
1030 __queue_me(q, hb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
1032
1033/* Return 1 if we were still queued (ie. 0 means we were woken) */
1034static int unqueue_me(struct futex_q *q)
1035{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 spinlock_t *lock_ptr;
Ingo Molnare2970f22006-06-27 02:54:47 -07001037 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 /* In the common case we don't take the spinlock, which is nice. */
1040 retry:
1041 lock_ptr = q->lock_ptr;
Christian Borntraegere91467e2006-08-05 12:13:52 -07001042 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 if (lock_ptr != 0) {
1044 spin_lock(lock_ptr);
1045 /*
1046 * q->lock_ptr can change between reading it and
1047 * spin_lock(), causing us to take the wrong lock. This
1048 * corrects the race condition.
1049 *
1050 * Reasoning goes like this: if we have the wrong lock,
1051 * q->lock_ptr must have changed (maybe several times)
1052 * between reading it and the spin_lock(). It can
1053 * change again after the spin_lock() but only if it was
1054 * already changed before the spin_lock(). It cannot,
1055 * however, change back to the original value. Therefore
1056 * we can detect whether we acquired the correct lock.
1057 */
1058 if (unlikely(lock_ptr != q->lock_ptr)) {
1059 spin_unlock(lock_ptr);
1060 goto retry;
1061 }
Pierre Peifferec92d082007-05-09 02:35:00 -07001062 WARN_ON(plist_node_empty(&q->list));
1063 plist_del(&q->list, &q->list.plist);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001064
1065 BUG_ON(q->pi_state);
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 spin_unlock(lock_ptr);
1068 ret = 1;
1069 }
1070
Rusty Russell9adef582007-05-08 00:26:42 -07001071 drop_futex_key_refs(&q->key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 return ret;
1073}
1074
Ingo Molnarc87e2832006-06-27 02:54:58 -07001075/*
1076 * PI futexes can not be requeued and must remove themself from the
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001077 * hash bucket. The hash bucket lock (i.e. lock_ptr) is held on entry
1078 * and dropped here.
Ingo Molnarc87e2832006-06-27 02:54:58 -07001079 */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001080static void unqueue_me_pi(struct futex_q *q)
Ingo Molnarc87e2832006-06-27 02:54:58 -07001081{
Pierre Peifferec92d082007-05-09 02:35:00 -07001082 WARN_ON(plist_node_empty(&q->list));
1083 plist_del(&q->list, &q->list.plist);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001084
1085 BUG_ON(!q->pi_state);
1086 free_pi_state(q->pi_state);
1087 q->pi_state = NULL;
1088
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001089 spin_unlock(q->lock_ptr);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001090
Rusty Russell9adef582007-05-08 00:26:42 -07001091 drop_futex_key_refs(&q->key);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001092}
1093
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001094/*
1095 * Fixup the pi_state owner with current.
1096 *
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001097 * Must be called with hash bucket lock held and mm->sem held for non
1098 * private futexes.
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001099 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001100static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001101 struct task_struct *curr)
1102{
1103 u32 newtid = curr->pid | FUTEX_WAITERS;
1104 struct futex_pi_state *pi_state = q->pi_state;
1105 u32 uval, curval, newval;
1106 int ret;
1107
1108 /* Owner died? */
1109 if (pi_state->owner != NULL) {
1110 spin_lock_irq(&pi_state->owner->pi_lock);
1111 WARN_ON(list_empty(&pi_state->list));
1112 list_del_init(&pi_state->list);
1113 spin_unlock_irq(&pi_state->owner->pi_lock);
1114 } else
1115 newtid |= FUTEX_OWNER_DIED;
1116
1117 pi_state->owner = curr;
1118
1119 spin_lock_irq(&curr->pi_lock);
1120 WARN_ON(!list_empty(&pi_state->list));
1121 list_add(&pi_state->list, &curr->pi_state_list);
1122 spin_unlock_irq(&curr->pi_lock);
1123
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001124 /*
1125 * We own it, so we have to replace the pending owner
1126 * TID. This must be atomic as we have preserve the
1127 * owner died bit here.
1128 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001129 ret = get_futex_value_locked(&uval, uaddr);
1130
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001131 while (!ret) {
1132 newval = (uval & FUTEX_OWNER_DIED) | newtid;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001133
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001134 curval = cmpxchg_futex_value_locked(uaddr, uval, newval);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001135
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001136 if (curval == -EFAULT)
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001137 ret = -EFAULT;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001138 if (curval == uval)
1139 break;
1140 uval = curval;
1141 }
1142 return ret;
1143}
1144
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001145/*
1146 * In case we must use restart_block to restart a futex_wait,
1147 * we encode in the 'arg3' shared capability
1148 */
1149#define ARG3_SHARED 1
1150
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001151static long futex_wait_restart(struct restart_block *restart);
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001152
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001153static int futex_wait(u32 __user *uaddr, struct rw_semaphore *fshared,
1154 u32 val, ktime_t *abs_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
Ingo Molnarc87e2832006-06-27 02:54:58 -07001156 struct task_struct *curr = current;
1157 DECLARE_WAITQUEUE(wait, curr);
Ingo Molnare2970f22006-06-27 02:54:47 -07001158 struct futex_hash_bucket *hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 struct futex_q q;
Ingo Molnare2970f22006-06-27 02:54:47 -07001160 u32 uval;
1161 int ret;
Thomas Gleixnerbd197232007-06-17 21:11:10 +02001162 struct hrtimer_sleeper t;
Pierre Peifferc19384b2007-05-09 02:35:02 -07001163 int rem = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Ingo Molnarc87e2832006-06-27 02:54:58 -07001165 q.pi_state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 retry:
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001167 futex_lock_mm(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001169 ret = get_futex_key(uaddr, fshared, &q.key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if (unlikely(ret != 0))
1171 goto out_release_sem;
1172
Ingo Molnare2970f22006-06-27 02:54:47 -07001173 hb = queue_lock(&q, -1, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175 /*
1176 * Access the page AFTER the futex is queued.
1177 * Order is important:
1178 *
1179 * Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val);
1180 * Userspace waker: if (cond(var)) { var = new; futex_wake(&var); }
1181 *
1182 * The basic logical guarantee of a futex is that it blocks ONLY
1183 * if cond(var) is known to be true at the time of blocking, for
1184 * any cond. If we queued after testing *uaddr, that would open
1185 * a race condition where we could block indefinitely with
1186 * cond(var) false, which would violate the guarantee.
1187 *
1188 * A consequence is that futex_wait() can return zero and absorb
1189 * a wakeup when *uaddr != val on entry to the syscall. This is
1190 * rare, but normal.
1191 *
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001192 * for shared futexes, we hold the mmap semaphore, so the mapping
1193 * cannot have changed since we looked it up in get_futex_key.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 */
Ingo Molnare2970f22006-06-27 02:54:47 -07001195 ret = get_futex_value_locked(&uval, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 if (unlikely(ret)) {
Ingo Molnare2970f22006-06-27 02:54:47 -07001198 queue_unlock(&q, hb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Ingo Molnare2970f22006-06-27 02:54:47 -07001200 /*
1201 * If we would have faulted, release mmap_sem, fault it in and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 * start all over again.
1203 */
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001204 futex_unlock_mm(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Ingo Molnare2970f22006-06-27 02:54:47 -07001206 ret = get_user(uval, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 if (!ret)
1209 goto retry;
1210 return ret;
1211 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001212 ret = -EWOULDBLOCK;
1213 if (uval != val)
1214 goto out_unlock_release_sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 /* Only actually queue if *uaddr contained val. */
Ingo Molnare2970f22006-06-27 02:54:47 -07001217 __queue_me(&q, hb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 /*
1220 * Now the futex is queued and we have checked the data, we
1221 * don't want to hold mmap_sem while we sleep.
Ingo Molnarc87e2832006-06-27 02:54:58 -07001222 */
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001223 futex_unlock_mm(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 /*
1226 * There might have been scheduling since the queue_me(), as we
1227 * cannot hold a spinlock across the get_user() in case it
1228 * faults, and we cannot just set TASK_INTERRUPTIBLE state when
1229 * queueing ourselves into the futex hash. This code thus has to
1230 * rely on the futex_wake() code removing us from hash when it
1231 * wakes us up.
1232 */
1233
1234 /* add_wait_queue is the barrier after __set_current_state. */
1235 __set_current_state(TASK_INTERRUPTIBLE);
1236 add_wait_queue(&q.waiters, &wait);
1237 /*
Pierre Peifferec92d082007-05-09 02:35:00 -07001238 * !plist_node_empty() is safe here without any lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 * q.lock_ptr != 0 is not safe, because of ordering against wakeup.
1240 */
Pierre Peifferec92d082007-05-09 02:35:00 -07001241 if (likely(!plist_node_empty(&q.list))) {
Pierre Peifferc19384b2007-05-09 02:35:02 -07001242 if (!abs_time)
1243 schedule();
1244 else {
1245 hrtimer_init(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1246 hrtimer_init_sleeper(&t, current);
1247 t.timer.expires = *abs_time;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001248
Pierre Peifferc19384b2007-05-09 02:35:02 -07001249 hrtimer_start(&t.timer, t.timer.expires, HRTIMER_MODE_ABS);
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001250
Pierre Peifferc19384b2007-05-09 02:35:02 -07001251 /*
1252 * the timer could have already expired, in which
1253 * case current would be flagged for rescheduling.
1254 * Don't bother calling schedule.
1255 */
1256 if (likely(t.task))
1257 schedule();
1258
1259 hrtimer_cancel(&t.timer);
1260
1261 /* Flag if a timeout occured */
1262 rem = (t.task == NULL);
1263 }
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 __set_current_state(TASK_RUNNING);
1266
1267 /*
1268 * NOTE: we don't remove ourselves from the waitqueue because
1269 * we are the only user of it.
1270 */
1271
1272 /* If we were woken (and unqueued), we succeeded, whatever. */
1273 if (!unqueue_me(&q))
1274 return 0;
Pierre Peifferc19384b2007-05-09 02:35:02 -07001275 if (rem)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 return -ETIMEDOUT;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001277
Ingo Molnare2970f22006-06-27 02:54:47 -07001278 /*
1279 * We expect signal_pending(current), but another thread may
1280 * have handled it for us already.
1281 */
Pierre Peifferc19384b2007-05-09 02:35:02 -07001282 if (!abs_time)
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001283 return -ERESTARTSYS;
1284 else {
1285 struct restart_block *restart;
1286 restart = &current_thread_info()->restart_block;
1287 restart->fn = futex_wait_restart;
1288 restart->arg0 = (unsigned long)uaddr;
1289 restart->arg1 = (unsigned long)val;
Pierre Peifferc19384b2007-05-09 02:35:02 -07001290 restart->arg2 = (unsigned long)abs_time;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001291 restart->arg3 = 0;
1292 if (fshared)
1293 restart->arg3 |= ARG3_SHARED;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001294 return -ERESTART_RESTARTBLOCK;
1295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Ingo Molnarc87e2832006-06-27 02:54:58 -07001297 out_unlock_release_sem:
1298 queue_unlock(&q, hb);
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 out_release_sem:
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001301 futex_unlock_mm(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001302 return ret;
1303}
1304
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001305
1306static long futex_wait_restart(struct restart_block *restart)
1307{
1308 u32 __user *uaddr = (u32 __user *)restart->arg0;
1309 u32 val = (u32)restart->arg1;
Pierre Peifferc19384b2007-05-09 02:35:02 -07001310 ktime_t *abs_time = (ktime_t *)restart->arg2;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001311 struct rw_semaphore *fshared = NULL;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001312
1313 restart->fn = do_no_restart_syscall;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001314 if (restart->arg3 & ARG3_SHARED)
1315 fshared = &current->mm->mmap_sem;
1316 return (long)futex_wait(uaddr, fshared, val, abs_time);
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001317}
1318
1319
Ingo Molnarc87e2832006-06-27 02:54:58 -07001320/*
1321 * Userspace tried a 0 -> TID atomic transition of the futex value
1322 * and failed. The kernel side here does the whole locking operation:
1323 * if there are waiters then it will block, it does PI, etc. (Due to
1324 * races the kernel might see a 0 value of the futex too.)
1325 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001326static int futex_lock_pi(u32 __user *uaddr, struct rw_semaphore *fshared,
1327 int detect, ktime_t *time, int trylock)
Ingo Molnarc87e2832006-06-27 02:54:58 -07001328{
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07001329 struct hrtimer_sleeper timeout, *to = NULL;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001330 struct task_struct *curr = current;
1331 struct futex_hash_bucket *hb;
1332 u32 uval, newval, curval;
1333 struct futex_q q;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001334 int ret, lock_taken, ownerdied = 0, attempt = 0;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001335
1336 if (refill_pi_state_cache())
1337 return -ENOMEM;
1338
Pierre Peifferc19384b2007-05-09 02:35:02 -07001339 if (time) {
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07001340 to = &timeout;
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001341 hrtimer_init(&to->timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07001342 hrtimer_init_sleeper(to, current);
Pierre Peifferc19384b2007-05-09 02:35:02 -07001343 to->timer.expires = *time;
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07001344 }
1345
Ingo Molnarc87e2832006-06-27 02:54:58 -07001346 q.pi_state = NULL;
1347 retry:
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001348 futex_lock_mm(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001349
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001350 ret = get_futex_key(uaddr, fshared, &q.key);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001351 if (unlikely(ret != 0))
1352 goto out_release_sem;
1353
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001354 retry_unlocked:
Ingo Molnarc87e2832006-06-27 02:54:58 -07001355 hb = queue_lock(&q, -1, NULL);
1356
1357 retry_locked:
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001358 ret = lock_taken = 0;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001359
Ingo Molnarc87e2832006-06-27 02:54:58 -07001360 /*
1361 * To avoid races, we attempt to take the lock here again
1362 * (by doing a 0 -> TID atomic cmpxchg), while holding all
1363 * the locks. It will most likely not succeed.
1364 */
1365 newval = current->pid;
1366
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001367 curval = cmpxchg_futex_value_locked(uaddr, 0, newval);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001368
1369 if (unlikely(curval == -EFAULT))
1370 goto uaddr_faulted;
1371
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001372 /*
1373 * Detect deadlocks. In case of REQUEUE_PI this is a valid
1374 * situation and we return success to user space.
1375 */
Ingo Molnarc87e2832006-06-27 02:54:58 -07001376 if (unlikely((curval & FUTEX_TID_MASK) == current->pid)) {
Thomas Gleixnerbd197232007-06-17 21:11:10 +02001377 ret = -EDEADLK;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001378 goto out_unlock_release_sem;
1379 }
1380
1381 /*
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001382 * Surprise - we got the lock. Just return to userspace:
Ingo Molnarc87e2832006-06-27 02:54:58 -07001383 */
1384 if (unlikely(!curval))
1385 goto out_unlock_release_sem;
1386
1387 uval = curval;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001388
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001389 /*
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001390 * Set the WAITERS flag, so the owner will know it has someone
1391 * to wake at next unlock
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001392 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001393 newval = curval | FUTEX_WAITERS;
1394
1395 /*
1396 * There are two cases, where a futex might have no owner (the
Thomas Gleixnerbd197232007-06-17 21:11:10 +02001397 * owner TID is 0): OWNER_DIED. We take over the futex in this
1398 * case. We also do an unconditional take over, when the owner
1399 * of the futex died.
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001400 *
1401 * This is safe as we are protected by the hash bucket lock !
1402 */
1403 if (unlikely(ownerdied || !(curval & FUTEX_TID_MASK))) {
Thomas Gleixnerbd197232007-06-17 21:11:10 +02001404 /* Keep the OWNER_DIED bit */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001405 newval = (curval & ~FUTEX_TID_MASK) | current->pid;
1406 ownerdied = 0;
1407 lock_taken = 1;
1408 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001409
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001410 curval = cmpxchg_futex_value_locked(uaddr, uval, newval);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001411
1412 if (unlikely(curval == -EFAULT))
1413 goto uaddr_faulted;
1414 if (unlikely(curval != uval))
1415 goto retry_locked;
1416
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001417 /*
Thomas Gleixnerbd197232007-06-17 21:11:10 +02001418 * We took the lock due to owner died take over.
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001419 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +02001420 if (unlikely(lock_taken))
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001421 goto out_unlock_release_sem;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001422
Ingo Molnarc87e2832006-06-27 02:54:58 -07001423 /*
1424 * We dont have the lock. Look up the PI state (or create it if
1425 * we are the first waiter):
1426 */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001427 ret = lookup_pi_state(uval, hb, &q.key, &q.pi_state);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001428
1429 if (unlikely(ret)) {
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001430 switch (ret) {
Ingo Molnarc87e2832006-06-27 02:54:58 -07001431
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001432 case -EAGAIN:
1433 /*
1434 * Task is exiting and we just wait for the
1435 * exit to complete.
1436 */
1437 queue_unlock(&q, hb);
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001438 futex_unlock_mm(fshared);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001439 cond_resched();
1440 goto retry;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001441
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001442 case -ESRCH:
1443 /*
1444 * No owner found for this futex. Check if the
1445 * OWNER_DIED bit is set to figure out whether
1446 * this is a robust futex or not.
1447 */
1448 if (get_futex_value_locked(&curval, uaddr))
Ingo Molnarc87e2832006-06-27 02:54:58 -07001449 goto uaddr_faulted;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001450
1451 /*
1452 * We simply start over in case of a robust
1453 * futex. The code above will take the futex
1454 * and return happy.
1455 */
1456 if (curval & FUTEX_OWNER_DIED) {
1457 ownerdied = 1;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001458 goto retry_locked;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001459 }
1460 default:
1461 goto out_unlock_release_sem;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001462 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001463 }
1464
1465 /*
1466 * Only actually queue now that the atomic ops are done:
1467 */
1468 __queue_me(&q, hb);
1469
1470 /*
1471 * Now the futex is queued and we have checked the data, we
1472 * don't want to hold mmap_sem while we sleep.
1473 */
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001474 futex_unlock_mm(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001475
1476 WARN_ON(!q.pi_state);
1477 /*
1478 * Block on the PI mutex:
1479 */
1480 if (!trylock)
1481 ret = rt_mutex_timed_lock(&q.pi_state->pi_mutex, to, 1);
1482 else {
1483 ret = rt_mutex_trylock(&q.pi_state->pi_mutex);
1484 /* Fixup the trylock return value: */
1485 ret = ret ? 0 : -EWOULDBLOCK;
1486 }
1487
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001488 futex_lock_mm(fshared);
Vernon Mauerya99e4e42006-07-01 04:35:42 -07001489 spin_lock(q.lock_ptr);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001490
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001491 if (!ret) {
1492 /*
1493 * Got the lock. We might not be the anticipated owner
1494 * if we did a lock-steal - fix up the PI-state in
1495 * that case:
1496 */
1497 if (q.pi_state->owner != curr)
1498 ret = fixup_pi_state_owner(uaddr, &q, curr);
1499 } else {
Ingo Molnarc87e2832006-06-27 02:54:58 -07001500 /*
1501 * Catch the rare case, where the lock was released
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001502 * when we were on the way back before we locked the
1503 * hash bucket.
Ingo Molnarc87e2832006-06-27 02:54:58 -07001504 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001505 if (q.pi_state->owner == curr &&
1506 rt_mutex_trylock(&q.pi_state->pi_mutex)) {
1507 ret = 0;
1508 } else {
1509 /*
1510 * Paranoia check. If we did not take the lock
1511 * in the trylock above, then we should not be
1512 * the owner of the rtmutex, neither the real
1513 * nor the pending one:
1514 */
1515 if (rt_mutex_owner(&q.pi_state->pi_mutex) == curr)
1516 printk(KERN_ERR "futex_lock_pi: ret = %d "
1517 "pi-mutex: %p pi-state %p\n", ret,
1518 q.pi_state->pi_mutex.owner,
1519 q.pi_state->owner);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001520 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001521 }
1522
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001523 /* Unqueue and drop the lock */
1524 unqueue_me_pi(&q);
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001525 futex_unlock_mm(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001526
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07001527 return ret != -EINTR ? ret : -ERESTARTNOINTR;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001528
1529 out_unlock_release_sem:
1530 queue_unlock(&q, hb);
1531
1532 out_release_sem:
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001533 futex_unlock_mm(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001534 return ret;
1535
1536 uaddr_faulted:
1537 /*
1538 * We have to r/w *(int __user *)uaddr, but we can't modify it
1539 * non-atomically. Therefore, if get_user below is not
1540 * enough, we need to handle the fault ourselves, while
1541 * still holding the mmap_sem.
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001542 *
1543 * ... and hb->lock. :-) --ANK
Ingo Molnarc87e2832006-06-27 02:54:58 -07001544 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001545 queue_unlock(&q, hb);
1546
Ingo Molnarc87e2832006-06-27 02:54:58 -07001547 if (attempt++) {
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001548 ret = futex_handle_fault((unsigned long)uaddr, fshared,
1549 attempt);
1550 if (ret)
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001551 goto out_release_sem;
1552 goto retry_unlocked;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001553 }
1554
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001555 futex_unlock_mm(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001556
1557 ret = get_user(uval, uaddr);
1558 if (!ret && (uval != -EFAULT))
1559 goto retry;
1560
1561 return ret;
1562}
1563
1564/*
Ingo Molnarc87e2832006-06-27 02:54:58 -07001565 * Userspace attempted a TID -> 0 atomic transition, and failed.
1566 * This is the in-kernel slowpath: we look up the PI state (if any),
1567 * and do the rt-mutex unlock.
1568 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001569static int futex_unlock_pi(u32 __user *uaddr, struct rw_semaphore *fshared)
Ingo Molnarc87e2832006-06-27 02:54:58 -07001570{
1571 struct futex_hash_bucket *hb;
1572 struct futex_q *this, *next;
1573 u32 uval;
Pierre Peifferec92d082007-05-09 02:35:00 -07001574 struct plist_head *head;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001575 union futex_key key;
1576 int ret, attempt = 0;
1577
1578retry:
1579 if (get_user(uval, uaddr))
1580 return -EFAULT;
1581 /*
1582 * We release only a lock we actually own:
1583 */
1584 if ((uval & FUTEX_TID_MASK) != current->pid)
1585 return -EPERM;
1586 /*
1587 * First take all the futex related locks:
1588 */
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001589 futex_lock_mm(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001590
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001591 ret = get_futex_key(uaddr, fshared, &key);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001592 if (unlikely(ret != 0))
1593 goto out;
1594
1595 hb = hash_futex(&key);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001596retry_unlocked:
Ingo Molnarc87e2832006-06-27 02:54:58 -07001597 spin_lock(&hb->lock);
1598
Ingo Molnarc87e2832006-06-27 02:54:58 -07001599 /*
1600 * To avoid races, try to do the TID -> 0 atomic transition
1601 * again. If it succeeds then we can return without waking
1602 * anyone else up:
1603 */
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001604 if (!(uval & FUTEX_OWNER_DIED))
1605 uval = cmpxchg_futex_value_locked(uaddr, current->pid, 0);
1606
Ingo Molnarc87e2832006-06-27 02:54:58 -07001607
1608 if (unlikely(uval == -EFAULT))
1609 goto pi_faulted;
1610 /*
1611 * Rare case: we managed to release the lock atomically,
1612 * no need to wake anyone else up:
1613 */
1614 if (unlikely(uval == current->pid))
1615 goto out_unlock;
1616
1617 /*
1618 * Ok, other tasks may need to be woken up - check waiters
1619 * and do the wakeup if necessary:
1620 */
1621 head = &hb->chain;
1622
Pierre Peifferec92d082007-05-09 02:35:00 -07001623 plist_for_each_entry_safe(this, next, head, list) {
Ingo Molnarc87e2832006-06-27 02:54:58 -07001624 if (!match_futex (&this->key, &key))
1625 continue;
1626 ret = wake_futex_pi(uaddr, uval, this);
1627 /*
1628 * The atomic access to the futex value
1629 * generated a pagefault, so retry the
1630 * user-access and the wakeup:
1631 */
1632 if (ret == -EFAULT)
1633 goto pi_faulted;
1634 goto out_unlock;
1635 }
1636 /*
1637 * No waiters - kernel unlocks the futex:
1638 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001639 if (!(uval & FUTEX_OWNER_DIED)) {
1640 ret = unlock_futex_pi(uaddr, uval);
1641 if (ret == -EFAULT)
1642 goto pi_faulted;
1643 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001644
1645out_unlock:
1646 spin_unlock(&hb->lock);
1647out:
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001648 futex_unlock_mm(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001649
1650 return ret;
1651
1652pi_faulted:
1653 /*
1654 * We have to r/w *(int __user *)uaddr, but we can't modify it
1655 * non-atomically. Therefore, if get_user below is not
1656 * enough, we need to handle the fault ourselves, while
1657 * still holding the mmap_sem.
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001658 *
1659 * ... and hb->lock. --ANK
Ingo Molnarc87e2832006-06-27 02:54:58 -07001660 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001661 spin_unlock(&hb->lock);
1662
Ingo Molnarc87e2832006-06-27 02:54:58 -07001663 if (attempt++) {
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001664 ret = futex_handle_fault((unsigned long)uaddr, fshared,
1665 attempt);
1666 if (ret)
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001667 goto out;
1668 goto retry_unlocked;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001669 }
1670
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001671 futex_unlock_mm(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001672
1673 ret = get_user(uval, uaddr);
1674 if (!ret && (uval != -EFAULT))
1675 goto retry;
1676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 return ret;
1678}
1679
1680static int futex_close(struct inode *inode, struct file *filp)
1681{
1682 struct futex_q *q = filp->private_data;
1683
1684 unqueue_me(q);
1685 kfree(q);
Ingo Molnare2970f22006-06-27 02:54:47 -07001686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 return 0;
1688}
1689
1690/* This is one-shot: once it's gone off you need a new fd */
1691static unsigned int futex_poll(struct file *filp,
1692 struct poll_table_struct *wait)
1693{
1694 struct futex_q *q = filp->private_data;
1695 int ret = 0;
1696
1697 poll_wait(filp, &q->waiters, wait);
1698
1699 /*
Pierre Peifferec92d082007-05-09 02:35:00 -07001700 * plist_node_empty() is safe here without any lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 * q->lock_ptr != 0 is not safe, because of ordering against wakeup.
1702 */
Pierre Peifferec92d082007-05-09 02:35:00 -07001703 if (plist_node_empty(&q->list))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 ret = POLLIN | POLLRDNORM;
1705
1706 return ret;
1707}
1708
Helge Deller15ad7cd2006-12-06 20:40:36 -08001709static const struct file_operations futex_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 .release = futex_close,
1711 .poll = futex_poll,
1712};
1713
1714/*
1715 * Signal allows caller to avoid the race which would occur if they
1716 * set the sigio stuff up afterwards.
1717 */
Ingo Molnare2970f22006-06-27 02:54:47 -07001718static int futex_fd(u32 __user *uaddr, int signal)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
1720 struct futex_q *q;
1721 struct file *filp;
1722 int ret, err;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001723 struct rw_semaphore *fshared;
Andrew Morton19c6b6e2006-11-02 22:07:17 -08001724 static unsigned long printk_interval;
1725
1726 if (printk_timed_ratelimit(&printk_interval, 60 * 60 * 1000)) {
1727 printk(KERN_WARNING "Process `%s' used FUTEX_FD, which "
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001728 "will be removed from the kernel in June 2007\n",
1729 current->comm);
Andrew Morton19c6b6e2006-11-02 22:07:17 -08001730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 ret = -EINVAL;
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001733 if (!valid_signal(signal))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 goto out;
1735
1736 ret = get_unused_fd();
1737 if (ret < 0)
1738 goto out;
1739 filp = get_empty_filp();
1740 if (!filp) {
1741 put_unused_fd(ret);
1742 ret = -ENFILE;
1743 goto out;
1744 }
1745 filp->f_op = &futex_fops;
Josef "Jeff" Sipekf3a43f32006-12-08 02:36:43 -08001746 filp->f_path.mnt = mntget(futex_mnt);
1747 filp->f_path.dentry = dget(futex_mnt->mnt_root);
1748 filp->f_mapping = filp->f_path.dentry->d_inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
1750 if (signal) {
Eric W. Biederman609d7fa2006-10-02 02:17:15 -07001751 err = __f_setown(filp, task_pid(current), PIDTYPE_PID, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 if (err < 0) {
Pekka Enberg39ed3fd2005-09-06 15:17:44 -07001753 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 }
1755 filp->f_owner.signum = signal;
1756 }
1757
1758 q = kmalloc(sizeof(*q), GFP_KERNEL);
1759 if (!q) {
Pekka Enberg39ed3fd2005-09-06 15:17:44 -07001760 err = -ENOMEM;
1761 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001763 q->pi_state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001765 fshared = &current->mm->mmap_sem;
1766 down_read(fshared);
1767 err = get_futex_key(uaddr, fshared, &q->key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
1769 if (unlikely(err != 0)) {
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001770 up_read(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 kfree(q);
Pekka Enberg39ed3fd2005-09-06 15:17:44 -07001772 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
1774
1775 /*
1776 * queue_me() must be called before releasing mmap_sem, because
1777 * key->shared.inode needs to be referenced while holding it.
1778 */
1779 filp->private_data = q;
1780
1781 queue_me(q, ret, filp);
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001782 up_read(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
1784 /* Now we map fd to filp, so userspace can access it */
1785 fd_install(ret, filp);
1786out:
1787 return ret;
Pekka Enberg39ed3fd2005-09-06 15:17:44 -07001788error:
1789 put_unused_fd(ret);
1790 put_filp(filp);
1791 ret = err;
1792 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793}
1794
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001795/*
1796 * Support for robust futexes: the kernel cleans up held futexes at
1797 * thread exit time.
1798 *
1799 * Implementation: user-space maintains a per-thread list of locks it
1800 * is holding. Upon do_exit(), the kernel carefully walks this list,
1801 * and marks all locks that are owned by this thread with the
Ingo Molnarc87e2832006-06-27 02:54:58 -07001802 * FUTEX_OWNER_DIED bit, and wakes up a waiter (if any). The list is
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001803 * always manipulated with the lock held, so the list is private and
1804 * per-thread. Userspace also maintains a per-thread 'list_op_pending'
1805 * field, to allow the kernel to clean up if the thread dies after
1806 * acquiring the lock, but just before it could have added itself to
1807 * the list. There can only be one such pending lock.
1808 */
1809
1810/**
1811 * sys_set_robust_list - set the robust-futex list head of a task
1812 * @head: pointer to the list-head
1813 * @len: length of the list-head, as userspace expects
1814 */
1815asmlinkage long
1816sys_set_robust_list(struct robust_list_head __user *head,
1817 size_t len)
1818{
1819 /*
1820 * The kernel knows only one size for now:
1821 */
1822 if (unlikely(len != sizeof(*head)))
1823 return -EINVAL;
1824
1825 current->robust_list = head;
1826
1827 return 0;
1828}
1829
1830/**
1831 * sys_get_robust_list - get the robust-futex list head of a task
1832 * @pid: pid of the process [zero for current task]
1833 * @head_ptr: pointer to a list-head pointer, the kernel fills it in
1834 * @len_ptr: pointer to a length field, the kernel fills in the header size
1835 */
1836asmlinkage long
Al Viroba46df92006-10-10 22:46:07 +01001837sys_get_robust_list(int pid, struct robust_list_head __user * __user *head_ptr,
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001838 size_t __user *len_ptr)
1839{
Al Viroba46df92006-10-10 22:46:07 +01001840 struct robust_list_head __user *head;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001841 unsigned long ret;
1842
1843 if (!pid)
1844 head = current->robust_list;
1845 else {
1846 struct task_struct *p;
1847
1848 ret = -ESRCH;
Oleg Nesterovaaa2a972006-09-29 02:00:55 -07001849 rcu_read_lock();
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001850 p = find_task_by_pid(pid);
1851 if (!p)
1852 goto err_unlock;
1853 ret = -EPERM;
1854 if ((current->euid != p->euid) && (current->euid != p->uid) &&
1855 !capable(CAP_SYS_PTRACE))
1856 goto err_unlock;
1857 head = p->robust_list;
Oleg Nesterovaaa2a972006-09-29 02:00:55 -07001858 rcu_read_unlock();
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001859 }
1860
1861 if (put_user(sizeof(*head), len_ptr))
1862 return -EFAULT;
1863 return put_user(head, head_ptr);
1864
1865err_unlock:
Oleg Nesterovaaa2a972006-09-29 02:00:55 -07001866 rcu_read_unlock();
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001867
1868 return ret;
1869}
1870
1871/*
1872 * Process a futex-list entry, check whether it's owned by the
1873 * dying task, and do notification if so:
1874 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001875int handle_futex_death(u32 __user *uaddr, struct task_struct *curr, int pi)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001876{
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001877 u32 uval, nval, mval;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001878
Ingo Molnar8f17d3a2006-03-27 01:16:27 -08001879retry:
1880 if (get_user(uval, uaddr))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001881 return -1;
1882
Ingo Molnar8f17d3a2006-03-27 01:16:27 -08001883 if ((uval & FUTEX_TID_MASK) == curr->pid) {
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001884 /*
1885 * Ok, this dying thread is truly holding a futex
1886 * of interest. Set the OWNER_DIED bit atomically
1887 * via cmpxchg, and if the value had FUTEX_WAITERS
1888 * set, wake up a waiter (if any). (We have to do a
1889 * futex_wake() even if OWNER_DIED is already set -
1890 * to handle the rare but possible case of recursive
1891 * thread-death.) The rest of the cleanup is done in
1892 * userspace.
1893 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001894 mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
1895 nval = futex_atomic_cmpxchg_inatomic(uaddr, uval, mval);
1896
Ingo Molnarc87e2832006-06-27 02:54:58 -07001897 if (nval == -EFAULT)
1898 return -1;
1899
1900 if (nval != uval)
Ingo Molnar8f17d3a2006-03-27 01:16:27 -08001901 goto retry;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001902
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001903 /*
1904 * Wake robust non-PI futexes here. The wakeup of
1905 * PI futexes happens in exit_pi_state():
1906 */
Thomas Gleixner36cf3b52007-07-15 23:41:20 -07001907 if (!pi && (uval & FUTEX_WAITERS))
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001908 futex_wake(uaddr, &curr->mm->mmap_sem, 1);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001909 }
1910 return 0;
1911}
1912
1913/*
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001914 * Fetch a robust-list pointer. Bit 0 signals PI futexes:
1915 */
1916static inline int fetch_robust_entry(struct robust_list __user **entry,
Al Viroba46df92006-10-10 22:46:07 +01001917 struct robust_list __user * __user *head,
1918 int *pi)
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001919{
1920 unsigned long uentry;
1921
Al Viroba46df92006-10-10 22:46:07 +01001922 if (get_user(uentry, (unsigned long __user *)head))
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001923 return -EFAULT;
1924
Al Viroba46df92006-10-10 22:46:07 +01001925 *entry = (void __user *)(uentry & ~1UL);
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001926 *pi = uentry & 1;
1927
1928 return 0;
1929}
1930
1931/*
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001932 * Walk curr->robust_list (very carefully, it's a userspace list!)
1933 * and mark any locks found there dead, and notify any waiters.
1934 *
1935 * We silently return on any sign of list-walking problem.
1936 */
1937void exit_robust_list(struct task_struct *curr)
1938{
1939 struct robust_list_head __user *head = curr->robust_list;
1940 struct robust_list __user *entry, *pending;
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001941 unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001942 unsigned long futex_offset;
1943
1944 /*
1945 * Fetch the list head (which was registered earlier, via
1946 * sys_set_robust_list()):
1947 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001948 if (fetch_robust_entry(&entry, &head->list.next, &pi))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001949 return;
1950 /*
1951 * Fetch the relative futex offset:
1952 */
1953 if (get_user(futex_offset, &head->futex_offset))
1954 return;
1955 /*
1956 * Fetch any possibly pending lock-add first, and handle it
1957 * if it exists:
1958 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001959 if (fetch_robust_entry(&pending, &head->list_op_pending, &pip))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001960 return;
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001961
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001962 if (pending)
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001963 handle_futex_death((void __user *)pending + futex_offset,
1964 curr, pip);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001965
1966 while (entry != &head->list) {
1967 /*
1968 * A pending lock might already be on the list, so
Ingo Molnarc87e2832006-06-27 02:54:58 -07001969 * don't process it twice:
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001970 */
1971 if (entry != pending)
Al Viroba46df92006-10-10 22:46:07 +01001972 if (handle_futex_death((void __user *)entry + futex_offset,
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001973 curr, pi))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001974 return;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001975 /*
1976 * Fetch the next entry in the list:
1977 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001978 if (fetch_robust_entry(&entry, &entry->next, &pi))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08001979 return;
1980 /*
1981 * Avoid excessively long or circular lists:
1982 */
1983 if (!--limit)
1984 break;
1985
1986 cond_resched();
1987 }
1988}
1989
Pierre Peifferc19384b2007-05-09 02:35:02 -07001990long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
Ingo Molnare2970f22006-06-27 02:54:47 -07001991 u32 __user *uaddr2, u32 val2, u32 val3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
1993 int ret;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001994 int cmd = op & FUTEX_CMD_MASK;
1995 struct rw_semaphore *fshared = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001997 if (!(op & FUTEX_PRIVATE_FLAG))
1998 fshared = &current->mm->mmap_sem;
1999
2000 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 case FUTEX_WAIT:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002002 ret = futex_wait(uaddr, fshared, val, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 break;
2004 case FUTEX_WAKE:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002005 ret = futex_wake(uaddr, fshared, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 break;
2007 case FUTEX_FD:
2008 /* non-zero val means F_SETOWN(getpid()) & F_SETSIG(val) */
2009 ret = futex_fd(uaddr, val);
2010 break;
2011 case FUTEX_REQUEUE:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002012 ret = futex_requeue(uaddr, fshared, uaddr2, val, val2, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 break;
2014 case FUTEX_CMP_REQUEUE:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002015 ret = futex_requeue(uaddr, fshared, uaddr2, val, val2, &val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 break;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07002017 case FUTEX_WAKE_OP:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002018 ret = futex_wake_op(uaddr, fshared, uaddr2, val, val2, val3);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07002019 break;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002020 case FUTEX_LOCK_PI:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002021 ret = futex_lock_pi(uaddr, fshared, val, timeout, 0);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002022 break;
2023 case FUTEX_UNLOCK_PI:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002024 ret = futex_unlock_pi(uaddr, fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002025 break;
2026 case FUTEX_TRYLOCK_PI:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002027 ret = futex_lock_pi(uaddr, fshared, 0, timeout, 1);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002028 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 default:
2030 ret = -ENOSYS;
2031 }
2032 return ret;
2033}
2034
2035
Ingo Molnare2970f22006-06-27 02:54:47 -07002036asmlinkage long sys_futex(u32 __user *uaddr, int op, u32 val,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 struct timespec __user *utime, u32 __user *uaddr2,
Ingo Molnare2970f22006-06-27 02:54:47 -07002038 u32 val3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
Pierre Peifferc19384b2007-05-09 02:35:02 -07002040 struct timespec ts;
2041 ktime_t t, *tp = NULL;
Ingo Molnare2970f22006-06-27 02:54:47 -07002042 u32 val2 = 0;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002043 int cmd = op & FUTEX_CMD_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002045 if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI)) {
Pierre Peifferc19384b2007-05-09 02:35:02 -07002046 if (copy_from_user(&ts, utime, sizeof(ts)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 return -EFAULT;
Pierre Peifferc19384b2007-05-09 02:35:02 -07002048 if (!timespec_valid(&ts))
Thomas Gleixner9741ef92006-03-31 02:31:32 -08002049 return -EINVAL;
Pierre Peifferc19384b2007-05-09 02:35:02 -07002050
2051 t = timespec_to_ktime(ts);
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002052 if (cmd == FUTEX_WAIT)
Pierre Peifferc19384b2007-05-09 02:35:02 -07002053 t = ktime_add(ktime_get(), t);
2054 tp = &t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 }
2056 /*
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002057 * requeue parameter in 'utime' if cmd == FUTEX_REQUEUE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 */
Thomas Gleixnerbd197232007-06-17 21:11:10 +02002059 if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE)
Ingo Molnare2970f22006-06-27 02:54:47 -07002060 val2 = (u32) (unsigned long) utime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Pierre Peifferc19384b2007-05-09 02:35:02 -07002062 return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063}
2064
David Howells454e2392006-06-23 02:02:57 -07002065static int futexfs_get_sb(struct file_system_type *fs_type,
2066 int flags, const char *dev_name, void *data,
2067 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068{
David Howells454e2392006-06-23 02:02:57 -07002069 return get_sb_pseudo(fs_type, "futex", NULL, 0xBAD1DEA, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070}
2071
2072static struct file_system_type futex_fs_type = {
2073 .name = "futexfs",
2074 .get_sb = futexfs_get_sb,
2075 .kill_sb = kill_anon_super,
2076};
2077
2078static int __init init(void)
2079{
Akinobu Mita95362fa2006-12-06 20:39:03 -08002080 int i = register_filesystem(&futex_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Akinobu Mita95362fa2006-12-06 20:39:03 -08002082 if (i)
2083 return i;
2084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 futex_mnt = kern_mount(&futex_fs_type);
Akinobu Mita95362fa2006-12-06 20:39:03 -08002086 if (IS_ERR(futex_mnt)) {
2087 unregister_filesystem(&futex_fs_type);
2088 return PTR_ERR(futex_mnt);
2089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
2091 for (i = 0; i < ARRAY_SIZE(futex_queues); i++) {
Pierre Peifferec92d082007-05-09 02:35:00 -07002092 plist_head_init(&futex_queues[i].chain, &futex_queues[i].lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 spin_lock_init(&futex_queues[i].lock);
2094 }
2095 return 0;
2096}
2097__initcall(init);