blob: 3b7f7713d9a4148e4ee0c9e9a748be0009b7d5c7 [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
Pierre Peifferd0aa7a72007-05-09 02:35:02 -070059#ifdef CONFIG_DEBUG_RT_MUTEXES
60# include "rtmutex-debug.h"
61#else
62# include "rtmutex.h"
63#endif
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define FUTEX_HASHBITS (CONFIG_BASE_SMALL ? 4 : 8)
66
67/*
Ingo Molnarc87e2832006-06-27 02:54:58 -070068 * Priority Inheritance state:
69 */
70struct futex_pi_state {
71 /*
72 * list of 'owned' pi_state instances - these have to be
73 * cleaned up in do_exit() if the task exits prematurely:
74 */
75 struct list_head list;
76
77 /*
78 * The PI object:
79 */
80 struct rt_mutex pi_mutex;
81
82 struct task_struct *owner;
83 atomic_t refcount;
84
85 union futex_key key;
86};
87
88/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * We use this hashed waitqueue instead of a normal wait_queue_t, so
90 * we can wake only the relevant ones (hashed queues may be shared).
91 *
92 * A futex_q has a woken state, just like tasks have TASK_RUNNING.
Pierre Peifferec92d082007-05-09 02:35:00 -070093 * It is considered woken when plist_node_empty(&q->list) || q->lock_ptr == 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 * The order of wakup is always to make the first condition true, then
95 * wake up q->waiters, then make the second condition true.
96 */
97struct futex_q {
Pierre Peifferec92d082007-05-09 02:35:00 -070098 struct plist_node list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 wait_queue_head_t waiters;
100
Ingo Molnare2970f22006-06-27 02:54:47 -0700101 /* Which hash list lock to use: */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 spinlock_t *lock_ptr;
103
Ingo Molnare2970f22006-06-27 02:54:47 -0700104 /* Key which the futex is hashed on: */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 union futex_key key;
106
Ingo Molnare2970f22006-06-27 02:54:47 -0700107 /* For fd, sigio sent using these: */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 int fd;
109 struct file *filp;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700110
111 /* Optional priority inheritance state: */
112 struct futex_pi_state *pi_state;
113 struct task_struct *task;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700114
115 /*
116 * This waiter is used in case of requeue from a
117 * normal futex to a PI-futex
118 */
119 struct rt_mutex_waiter waiter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120};
121
122/*
123 * Split the global futex_lock into every hash list lock.
124 */
125struct futex_hash_bucket {
Pierre Peifferec92d082007-05-09 02:35:00 -0700126 spinlock_t lock;
127 struct plist_head chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128};
129
130static struct futex_hash_bucket futex_queues[1<<FUTEX_HASHBITS];
131
132/* Futex-fs vfsmount entry: */
133static struct vfsmount *futex_mnt;
134
135/*
136 * We hash on the keys returned from get_futex_key (see below).
137 */
138static struct futex_hash_bucket *hash_futex(union futex_key *key)
139{
140 u32 hash = jhash2((u32*)&key->both.word,
141 (sizeof(key->both.word)+sizeof(key->both.ptr))/4,
142 key->both.offset);
143 return &futex_queues[hash & ((1 << FUTEX_HASHBITS)-1)];
144}
145
146/*
147 * Return 1 if two futex_keys are equal, 0 otherwise.
148 */
149static inline int match_futex(union futex_key *key1, union futex_key *key2)
150{
151 return (key1->both.word == key2->both.word
152 && key1->both.ptr == key2->both.ptr
153 && key1->both.offset == key2->both.offset);
154}
155
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700156/**
157 * get_futex_key - Get parameters which are the keys for a futex.
158 * @uaddr: virtual address of the futex
159 * @shared: NULL for a PROCESS_PRIVATE futex,
160 * &current->mm->mmap_sem for a PROCESS_SHARED futex
161 * @key: address where result is stored.
162 *
163 * Returns a negative error code or 0
164 * The key words are stored in *key on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 *
Josef "Jeff" Sipekf3a43f32006-12-08 02:36:43 -0800166 * For shared mappings, it's (page->index, vma->vm_file->f_path.dentry->d_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 * offset_within_page). For private mappings, it's (uaddr, current->mm).
168 * We can usually work out the index without swapping in the page.
169 *
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700170 * fshared is NULL for PROCESS_PRIVATE futexes
171 * For other futexes, it points to &current->mm->mmap_sem and
172 * caller must have taken the reader lock. but NOT any spinlocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700174int get_futex_key(u32 __user *uaddr, struct rw_semaphore *fshared,
175 union futex_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Ingo Molnare2970f22006-06-27 02:54:47 -0700177 unsigned long address = (unsigned long)uaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 struct mm_struct *mm = current->mm;
179 struct vm_area_struct *vma;
180 struct page *page;
181 int err;
182
183 /*
184 * The futex address must be "naturally" aligned.
185 */
Ingo Molnare2970f22006-06-27 02:54:47 -0700186 key->both.offset = address % PAGE_SIZE;
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700187 if (unlikely((address % sizeof(u32)) != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return -EINVAL;
Ingo Molnare2970f22006-06-27 02:54:47 -0700189 address -= key->both.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 /*
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700192 * PROCESS_PRIVATE futexes are fast.
193 * As the mm cannot disappear under us and the 'key' only needs
194 * virtual address, we dont even have to find the underlying vma.
195 * Note : We do have to check 'uaddr' is a valid user address,
196 * but access_ok() should be faster than find_vma()
197 */
198 if (!fshared) {
199 if (unlikely(!access_ok(VERIFY_WRITE, uaddr, sizeof(u32))))
200 return -EFAULT;
201 key->private.mm = mm;
202 key->private.address = address;
203 return 0;
204 }
205 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 * The futex is hashed differently depending on whether
207 * it's in a shared or private mapping. So check vma first.
208 */
Ingo Molnare2970f22006-06-27 02:54:47 -0700209 vma = find_extend_vma(mm, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (unlikely(!vma))
211 return -EFAULT;
212
213 /*
214 * Permissions.
215 */
216 if (unlikely((vma->vm_flags & (VM_IO|VM_READ)) != VM_READ))
217 return (vma->vm_flags & VM_IO) ? -EPERM : -EACCES;
218
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700219 /* Save the user address in the ley */
220 key->uaddr = uaddr;
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 /*
223 * Private mappings are handled in a simple way.
224 *
225 * NOTE: When userspace waits on a MAP_SHARED mapping, even if
226 * it's a read-only handle, it's expected that futexes attach to
227 * the object not the particular process. Therefore we use
228 * VM_MAYSHARE here, not VM_SHARED which is restricted to shared
229 * mappings of _writable_ handles.
230 */
231 if (likely(!(vma->vm_flags & VM_MAYSHARE))) {
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700232 key->both.offset |= FUT_OFF_MMSHARED; /* reference taken on mm */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 key->private.mm = mm;
Ingo Molnare2970f22006-06-27 02:54:47 -0700234 key->private.address = address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return 0;
236 }
237
238 /*
239 * Linear file mappings are also simple.
240 */
Josef "Jeff" Sipekf3a43f32006-12-08 02:36:43 -0800241 key->shared.inode = vma->vm_file->f_path.dentry->d_inode;
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700242 key->both.offset |= FUT_OFF_INODE; /* inode-based key. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if (likely(!(vma->vm_flags & VM_NONLINEAR))) {
Ingo Molnare2970f22006-06-27 02:54:47 -0700244 key->shared.pgoff = (((address - vma->vm_start) >> PAGE_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 + vma->vm_pgoff);
246 return 0;
247 }
248
249 /*
250 * We could walk the page table to read the non-linear
251 * pte, and get the page index without fetching the page
252 * from swap. But that's a lot of code to duplicate here
253 * for a rare case, so we simply fetch the page.
254 */
Ingo Molnare2970f22006-06-27 02:54:47 -0700255 err = get_user_pages(current, mm, address, 1, 0, 0, &page, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (err >= 0) {
257 key->shared.pgoff =
258 page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
259 put_page(page);
260 return 0;
261 }
262 return err;
263}
Rusty Russell9adef582007-05-08 00:26:42 -0700264EXPORT_SYMBOL_GPL(get_futex_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266/*
267 * Take a reference to the resource addressed by a key.
268 * Can be called while holding spinlocks.
269 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 */
Rusty Russell9adef582007-05-08 00:26:42 -0700271inline void get_futex_key_refs(union futex_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700273 if (key->both.ptr == 0)
274 return;
275 switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
276 case FUT_OFF_INODE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 atomic_inc(&key->shared.inode->i_count);
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700278 break;
279 case FUT_OFF_MMSHARED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 atomic_inc(&key->private.mm->mm_count);
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700281 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283}
Rusty Russell9adef582007-05-08 00:26:42 -0700284EXPORT_SYMBOL_GPL(get_futex_key_refs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286/*
287 * Drop a reference to the resource addressed by a key.
288 * The hash bucket spinlock must not be held.
289 */
Rusty Russell9adef582007-05-08 00:26:42 -0700290void drop_futex_key_refs(union futex_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700292 if (key->both.ptr == 0)
293 return;
294 switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
295 case FUT_OFF_INODE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 iput(key->shared.inode);
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700297 break;
298 case FUT_OFF_MMSHARED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 mmdrop(key->private.mm);
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700300 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302}
Rusty Russell9adef582007-05-08 00:26:42 -0700303EXPORT_SYMBOL_GPL(drop_futex_key_refs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Ingo Molnare2970f22006-06-27 02:54:47 -0700305static inline int get_futex_value_locked(u32 *dest, u32 __user *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 int ret;
308
Peter Zijlstraa8663742006-12-06 20:32:20 -0800309 pagefault_disable();
Ingo Molnare2970f22006-06-27 02:54:47 -0700310 ret = __copy_from_user_inatomic(dest, from, sizeof(u32));
Peter Zijlstraa8663742006-12-06 20:32:20 -0800311 pagefault_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 return ret ? -EFAULT : 0;
314}
315
316/*
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700317 * Fault handling.
318 * if fshared is non NULL, current->mm->mmap_sem is already held
Ingo Molnarc87e2832006-06-27 02:54:58 -0700319 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700320static int futex_handle_fault(unsigned long address,
321 struct rw_semaphore *fshared, int attempt)
Ingo Molnarc87e2832006-06-27 02:54:58 -0700322{
323 struct vm_area_struct * vma;
324 struct mm_struct *mm = current->mm;
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700325 int ret = -EFAULT;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700326
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700327 if (attempt > 2)
328 return ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700329
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700330 if (!fshared)
331 down_read(&mm->mmap_sem);
332 vma = find_vma(mm, address);
333 if (vma && address >= vma->vm_start &&
334 (vma->vm_flags & VM_WRITE)) {
335 switch (handle_mm_fault(mm, vma, address, 1)) {
336 case VM_FAULT_MINOR:
337 ret = 0;
338 current->min_flt++;
339 break;
340 case VM_FAULT_MAJOR:
341 ret = 0;
342 current->maj_flt++;
343 break;
344 }
Ingo Molnarc87e2832006-06-27 02:54:58 -0700345 }
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700346 if (!fshared)
347 up_read(&mm->mmap_sem);
348 return ret;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700349}
350
351/*
352 * PI code:
353 */
354static int refill_pi_state_cache(void)
355{
356 struct futex_pi_state *pi_state;
357
358 if (likely(current->pi_state_cache))
359 return 0;
360
Burman Yan4668edc2006-12-06 20:38:51 -0800361 pi_state = kzalloc(sizeof(*pi_state), GFP_KERNEL);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700362
363 if (!pi_state)
364 return -ENOMEM;
365
Ingo Molnarc87e2832006-06-27 02:54:58 -0700366 INIT_LIST_HEAD(&pi_state->list);
367 /* pi_mutex gets initialized later */
368 pi_state->owner = NULL;
369 atomic_set(&pi_state->refcount, 1);
370
371 current->pi_state_cache = pi_state;
372
373 return 0;
374}
375
376static struct futex_pi_state * alloc_pi_state(void)
377{
378 struct futex_pi_state *pi_state = current->pi_state_cache;
379
380 WARN_ON(!pi_state);
381 current->pi_state_cache = NULL;
382
383 return pi_state;
384}
385
386static void free_pi_state(struct futex_pi_state *pi_state)
387{
388 if (!atomic_dec_and_test(&pi_state->refcount))
389 return;
390
391 /*
392 * If pi_state->owner is NULL, the owner is most probably dying
393 * and has cleaned up the pi_state already
394 */
395 if (pi_state->owner) {
396 spin_lock_irq(&pi_state->owner->pi_lock);
397 list_del_init(&pi_state->list);
398 spin_unlock_irq(&pi_state->owner->pi_lock);
399
400 rt_mutex_proxy_unlock(&pi_state->pi_mutex, pi_state->owner);
401 }
402
403 if (current->pi_state_cache)
404 kfree(pi_state);
405 else {
406 /*
407 * pi_state->list is already empty.
408 * clear pi_state->owner.
409 * refcount is at 0 - put it back to 1.
410 */
411 pi_state->owner = NULL;
412 atomic_set(&pi_state->refcount, 1);
413 current->pi_state_cache = pi_state;
414 }
415}
416
417/*
418 * Look up the task based on what TID userspace gave us.
419 * We dont trust it.
420 */
421static struct task_struct * futex_find_get_task(pid_t pid)
422{
423 struct task_struct *p;
424
Oleg Nesterovd359b542006-09-29 02:00:55 -0700425 rcu_read_lock();
Ingo Molnarc87e2832006-06-27 02:54:58 -0700426 p = find_task_by_pid(pid);
427 if (!p)
428 goto out_unlock;
429 if ((current->euid != p->euid) && (current->euid != p->uid)) {
430 p = NULL;
431 goto out_unlock;
432 }
Ingo Molnarc87e2832006-06-27 02:54:58 -0700433 get_task_struct(p);
434out_unlock:
Oleg Nesterovd359b542006-09-29 02:00:55 -0700435 rcu_read_unlock();
Ingo Molnarc87e2832006-06-27 02:54:58 -0700436
437 return p;
438}
439
440/*
441 * This task is holding PI mutexes at exit time => bad.
442 * Kernel cleans up PI-state, but userspace is likely hosed.
443 * (Robust-futex cleanup is separate and might save the day for userspace.)
444 */
445void exit_pi_state_list(struct task_struct *curr)
446{
Ingo Molnarc87e2832006-06-27 02:54:58 -0700447 struct list_head *next, *head = &curr->pi_state_list;
448 struct futex_pi_state *pi_state;
Ingo Molnar627371d2006-07-29 05:16:20 +0200449 struct futex_hash_bucket *hb;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700450 union futex_key key;
451
452 /*
453 * We are a ZOMBIE and nobody can enqueue itself on
454 * pi_state_list anymore, but we have to be careful
Ingo Molnar627371d2006-07-29 05:16:20 +0200455 * versus waiters unqueueing themselves:
Ingo Molnarc87e2832006-06-27 02:54:58 -0700456 */
457 spin_lock_irq(&curr->pi_lock);
458 while (!list_empty(head)) {
459
460 next = head->next;
461 pi_state = list_entry(next, struct futex_pi_state, list);
462 key = pi_state->key;
Ingo Molnar627371d2006-07-29 05:16:20 +0200463 hb = hash_futex(&key);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700464 spin_unlock_irq(&curr->pi_lock);
465
Ingo Molnarc87e2832006-06-27 02:54:58 -0700466 spin_lock(&hb->lock);
467
468 spin_lock_irq(&curr->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +0200469 /*
470 * We dropped the pi-lock, so re-check whether this
471 * task still owns the PI-state:
472 */
Ingo Molnarc87e2832006-06-27 02:54:58 -0700473 if (head->next != next) {
474 spin_unlock(&hb->lock);
475 continue;
476 }
477
Ingo Molnarc87e2832006-06-27 02:54:58 -0700478 WARN_ON(pi_state->owner != curr);
Ingo Molnar627371d2006-07-29 05:16:20 +0200479 WARN_ON(list_empty(&pi_state->list));
480 list_del_init(&pi_state->list);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700481 pi_state->owner = NULL;
482 spin_unlock_irq(&curr->pi_lock);
483
484 rt_mutex_unlock(&pi_state->pi_mutex);
485
486 spin_unlock(&hb->lock);
487
488 spin_lock_irq(&curr->pi_lock);
489 }
490 spin_unlock_irq(&curr->pi_lock);
491}
492
493static int
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700494lookup_pi_state(u32 uval, struct futex_hash_bucket *hb,
495 union futex_key *key, struct futex_pi_state **ps)
Ingo Molnarc87e2832006-06-27 02:54:58 -0700496{
497 struct futex_pi_state *pi_state = NULL;
498 struct futex_q *this, *next;
Pierre Peifferec92d082007-05-09 02:35:00 -0700499 struct plist_head *head;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700500 struct task_struct *p;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700501 pid_t pid = uval & FUTEX_TID_MASK;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700502
503 head = &hb->chain;
504
Pierre Peifferec92d082007-05-09 02:35:00 -0700505 plist_for_each_entry_safe(this, next, head, list) {
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700506 if (match_futex(&this->key, key)) {
Ingo Molnarc87e2832006-06-27 02:54:58 -0700507 /*
508 * Another waiter already exists - bump up
509 * the refcount and return its pi_state:
510 */
511 pi_state = this->pi_state;
Thomas Gleixner06a9ec22006-07-10 04:44:30 -0700512 /*
513 * Userspace might have messed up non PI and PI futexes
514 */
515 if (unlikely(!pi_state))
516 return -EINVAL;
517
Ingo Molnar627371d2006-07-29 05:16:20 +0200518 WARN_ON(!atomic_read(&pi_state->refcount));
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700519 WARN_ON(pid && pi_state->owner &&
520 pi_state->owner->pid != pid);
Ingo Molnar627371d2006-07-29 05:16:20 +0200521
Ingo Molnarc87e2832006-06-27 02:54:58 -0700522 atomic_inc(&pi_state->refcount);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700523 *ps = pi_state;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700524
525 return 0;
526 }
527 }
528
529 /*
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200530 * We are the first waiter - try to look up the real owner and attach
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700531 * the new pi_state to it, but bail out when TID = 0
Ingo Molnarc87e2832006-06-27 02:54:58 -0700532 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700533 if (!pid)
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200534 return -ESRCH;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700535 p = futex_find_get_task(pid);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700536 if (IS_ERR(p))
537 return PTR_ERR(p);
538
539 /*
540 * We need to look at the task state flags to figure out,
541 * whether the task is exiting. To protect against the do_exit
542 * change of the task flags, we do this protected by
543 * p->pi_lock:
544 */
545 spin_lock_irq(&p->pi_lock);
546 if (unlikely(p->flags & PF_EXITING)) {
547 /*
548 * The task is on the way out. When PF_EXITPIDONE is
549 * set, we know that the task has finished the
550 * cleanup:
551 */
552 int ret = (p->flags & PF_EXITPIDONE) ? -ESRCH : -EAGAIN;
553
554 spin_unlock_irq(&p->pi_lock);
555 put_task_struct(p);
556 return ret;
557 }
Ingo Molnarc87e2832006-06-27 02:54:58 -0700558
559 pi_state = alloc_pi_state();
560
561 /*
562 * Initialize the pi_mutex in locked state and make 'p'
563 * the owner of it:
564 */
565 rt_mutex_init_proxy_locked(&pi_state->pi_mutex, p);
566
567 /* Store the key for possible exit cleanups: */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700568 pi_state->key = *key;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700569
Ingo Molnar627371d2006-07-29 05:16:20 +0200570 WARN_ON(!list_empty(&pi_state->list));
Ingo Molnarc87e2832006-06-27 02:54:58 -0700571 list_add(&pi_state->list, &p->pi_state_list);
572 pi_state->owner = p;
573 spin_unlock_irq(&p->pi_lock);
574
575 put_task_struct(p);
576
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700577 *ps = pi_state;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700578
579 return 0;
580}
581
582/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 * The hash bucket lock must be held when this is called.
584 * Afterwards, the futex_q must not be accessed.
585 */
586static void wake_futex(struct futex_q *q)
587{
Pierre Peifferec92d082007-05-09 02:35:00 -0700588 plist_del(&q->list, &q->list.plist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (q->filp)
590 send_sigio(&q->filp->f_owner, q->fd, POLL_IN);
591 /*
592 * The lock in wake_up_all() is a crucial memory barrier after the
Pierre Peifferec92d082007-05-09 02:35:00 -0700593 * plist_del() and also before assigning to q->lock_ptr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 */
595 wake_up_all(&q->waiters);
596 /*
597 * The waiting task can free the futex_q as soon as this is written,
598 * without taking any locks. This must come last.
Andrew Morton8e311082005-12-23 19:54:46 -0800599 *
600 * A memory barrier is required here to prevent the following store
601 * to lock_ptr from getting ahead of the wakeup. Clearing the lock
602 * at the end of wake_up_all() does not prevent this store from
603 * moving.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 */
Ralf Baechleccdea2f2006-12-06 20:40:26 -0800605 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 q->lock_ptr = NULL;
607}
608
Ingo Molnarc87e2832006-06-27 02:54:58 -0700609static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this)
610{
611 struct task_struct *new_owner;
612 struct futex_pi_state *pi_state = this->pi_state;
613 u32 curval, newval;
614
615 if (!pi_state)
616 return -EINVAL;
617
Ingo Molnar217788672007-03-16 13:38:31 -0800618 spin_lock(&pi_state->pi_mutex.wait_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700619 new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);
620
621 /*
622 * This happens when we have stolen the lock and the original
623 * pending owner did not enqueue itself back on the rt_mutex.
624 * Thats not a tragedy. We know that way, that a lock waiter
625 * is on the fly. We make the futex_q waiter the pending owner.
626 */
627 if (!new_owner)
628 new_owner = this->task;
629
630 /*
631 * We pass it to the next owner. (The WAITERS bit is always
632 * kept enabled while there is PI state around. We must also
633 * preserve the owner died bit.)
634 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200635 if (!(uval & FUTEX_OWNER_DIED)) {
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700636 int ret = 0;
637
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200638 newval = FUTEX_WAITERS | new_owner->pid;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700639 /* Keep the FUTEX_WAITER_REQUEUED flag if it was set */
640 newval |= (uval & FUTEX_WAITER_REQUEUED);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700641
Peter Zijlstraa8663742006-12-06 20:32:20 -0800642 pagefault_disable();
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200643 curval = futex_atomic_cmpxchg_inatomic(uaddr, uval, newval);
Peter Zijlstraa8663742006-12-06 20:32:20 -0800644 pagefault_enable();
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700645
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200646 if (curval == -EFAULT)
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700647 ret = -EFAULT;
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200648 if (curval != uval)
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -0700649 ret = -EINVAL;
650 if (ret) {
651 spin_unlock(&pi_state->pi_mutex.wait_lock);
652 return ret;
653 }
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200654 }
Ingo Molnarc87e2832006-06-27 02:54:58 -0700655
Ingo Molnar627371d2006-07-29 05:16:20 +0200656 spin_lock_irq(&pi_state->owner->pi_lock);
657 WARN_ON(list_empty(&pi_state->list));
658 list_del_init(&pi_state->list);
659 spin_unlock_irq(&pi_state->owner->pi_lock);
660
661 spin_lock_irq(&new_owner->pi_lock);
662 WARN_ON(!list_empty(&pi_state->list));
Ingo Molnarc87e2832006-06-27 02:54:58 -0700663 list_add(&pi_state->list, &new_owner->pi_state_list);
664 pi_state->owner = new_owner;
Ingo Molnar627371d2006-07-29 05:16:20 +0200665 spin_unlock_irq(&new_owner->pi_lock);
666
Ingo Molnar217788672007-03-16 13:38:31 -0800667 spin_unlock(&pi_state->pi_mutex.wait_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700668 rt_mutex_unlock(&pi_state->pi_mutex);
669
670 return 0;
671}
672
673static int unlock_futex_pi(u32 __user *uaddr, u32 uval)
674{
675 u32 oldval;
676
677 /*
678 * There is no waiter, so we unlock the futex. The owner died
679 * bit has not to be preserved here. We are the owner:
680 */
Peter Zijlstraa8663742006-12-06 20:32:20 -0800681 pagefault_disable();
Ingo Molnarc87e2832006-06-27 02:54:58 -0700682 oldval = futex_atomic_cmpxchg_inatomic(uaddr, uval, 0);
Peter Zijlstraa8663742006-12-06 20:32:20 -0800683 pagefault_enable();
Ingo Molnarc87e2832006-06-27 02:54:58 -0700684
685 if (oldval == -EFAULT)
686 return oldval;
687 if (oldval != uval)
688 return -EAGAIN;
689
690 return 0;
691}
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693/*
Ingo Molnar8b8f3192006-07-03 00:25:05 -0700694 * Express the locking dependencies for lockdep:
695 */
696static inline void
697double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
698{
699 if (hb1 <= hb2) {
700 spin_lock(&hb1->lock);
701 if (hb1 < hb2)
702 spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING);
703 } else { /* hb1 > hb2 */
704 spin_lock(&hb2->lock);
705 spin_lock_nested(&hb1->lock, SINGLE_DEPTH_NESTING);
706 }
707}
708
709/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 * Wake up all waiters hashed on the physical page that is mapped
711 * to this virtual address:
712 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700713static int futex_wake(u32 __user *uaddr, struct rw_semaphore *fshared,
714 int nr_wake)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Ingo Molnare2970f22006-06-27 02:54:47 -0700716 struct futex_hash_bucket *hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 struct futex_q *this, *next;
Pierre Peifferec92d082007-05-09 02:35:00 -0700718 struct plist_head *head;
Ingo Molnare2970f22006-06-27 02:54:47 -0700719 union futex_key key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 int ret;
721
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700722 if (fshared)
723 down_read(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700725 ret = get_futex_key(uaddr, fshared, &key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (unlikely(ret != 0))
727 goto out;
728
Ingo Molnare2970f22006-06-27 02:54:47 -0700729 hb = hash_futex(&key);
730 spin_lock(&hb->lock);
731 head = &hb->chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Pierre Peifferec92d082007-05-09 02:35:00 -0700733 plist_for_each_entry_safe(this, next, head, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (match_futex (&this->key, &key)) {
Ingo Molnared6f7b12006-07-01 04:35:46 -0700735 if (this->pi_state) {
736 ret = -EINVAL;
737 break;
738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 wake_futex(this);
740 if (++ret >= nr_wake)
741 break;
742 }
743 }
744
Ingo Molnare2970f22006-06-27 02:54:47 -0700745 spin_unlock(&hb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746out:
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700747 if (fshared)
748 up_read(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return ret;
750}
751
752/*
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700753 * Called from futex_requeue_pi.
754 * Set FUTEX_WAITERS and FUTEX_WAITER_REQUEUED flags on the
755 * PI-futex value; search its associated pi_state if an owner exist
756 * or create a new one without owner.
757 */
758static inline int
759lookup_pi_state_for_requeue(u32 __user *uaddr, struct futex_hash_bucket *hb,
760 union futex_key *key,
761 struct futex_pi_state **pi_state)
762{
763 u32 curval, uval, newval;
764
765retry:
766 /*
767 * We can't handle a fault cleanly because we can't
768 * release the locks here. Simply return the fault.
769 */
770 if (get_futex_value_locked(&curval, uaddr))
771 return -EFAULT;
772
773 /* set the flags FUTEX_WAITERS and FUTEX_WAITER_REQUEUED */
774 if ((curval & (FUTEX_WAITERS | FUTEX_WAITER_REQUEUED))
775 != (FUTEX_WAITERS | FUTEX_WAITER_REQUEUED)) {
776 /*
777 * No waiters yet, we prepare the futex to have some waiters.
778 */
779
780 uval = curval;
781 newval = uval | FUTEX_WAITERS | FUTEX_WAITER_REQUEUED;
782
783 pagefault_disable();
784 curval = futex_atomic_cmpxchg_inatomic(uaddr, uval, newval);
785 pagefault_enable();
786
787 if (unlikely(curval == -EFAULT))
788 return -EFAULT;
789 if (unlikely(curval != uval))
790 goto retry;
791 }
792
793 if (!(curval & FUTEX_TID_MASK)
794 || lookup_pi_state(curval, hb, key, pi_state)) {
795 /* the futex has no owner (yet) or the lookup failed:
796 allocate one pi_state without owner */
797
798 *pi_state = alloc_pi_state();
799
800 /* Already stores the key: */
801 (*pi_state)->key = *key;
802
803 /* init the mutex without owner */
804 __rt_mutex_init(&(*pi_state)->pi_mutex, NULL);
805 }
806
807 return 0;
808}
809
810/*
811 * Keep the first nr_wake waiter from futex1, wake up one,
812 * and requeue the next nr_requeue waiters following hashed on
813 * one physical page to another physical page (PI-futex uaddr2)
814 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700815static int futex_requeue_pi(u32 __user *uaddr1,
816 struct rw_semaphore *fshared,
817 u32 __user *uaddr2,
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700818 int nr_wake, int nr_requeue, u32 *cmpval)
819{
820 union futex_key key1, key2;
821 struct futex_hash_bucket *hb1, *hb2;
822 struct plist_head *head1;
823 struct futex_q *this, *next;
824 struct futex_pi_state *pi_state2 = NULL;
825 struct rt_mutex_waiter *waiter, *top_waiter = NULL;
826 struct rt_mutex *lock2 = NULL;
827 int ret, drop_count = 0;
828
829 if (refill_pi_state_cache())
830 return -ENOMEM;
831
832retry:
833 /*
834 * First take all the futex related locks:
835 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700836 if (fshared)
837 down_read(fshared);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700838
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700839 ret = get_futex_key(uaddr1, fshared, &key1);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700840 if (unlikely(ret != 0))
841 goto out;
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700842 ret = get_futex_key(uaddr2, fshared, &key2);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700843 if (unlikely(ret != 0))
844 goto out;
845
846 hb1 = hash_futex(&key1);
847 hb2 = hash_futex(&key2);
848
849 double_lock_hb(hb1, hb2);
850
851 if (likely(cmpval != NULL)) {
852 u32 curval;
853
854 ret = get_futex_value_locked(&curval, uaddr1);
855
856 if (unlikely(ret)) {
857 spin_unlock(&hb1->lock);
858 if (hb1 != hb2)
859 spin_unlock(&hb2->lock);
860
861 /*
862 * If we would have faulted, release mmap_sem, fault
863 * it in and start all over again.
864 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700865 if (fshared)
866 up_read(fshared);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700867
868 ret = get_user(curval, uaddr1);
869
870 if (!ret)
871 goto retry;
872
873 return ret;
874 }
875 if (curval != *cmpval) {
876 ret = -EAGAIN;
877 goto out_unlock;
878 }
879 }
880
881 head1 = &hb1->chain;
882 plist_for_each_entry_safe(this, next, head1, list) {
883 if (!match_futex (&this->key, &key1))
884 continue;
885 if (++ret <= nr_wake) {
886 wake_futex(this);
887 } else {
888 /*
889 * FIRST: get and set the pi_state
890 */
891 if (!pi_state2) {
892 int s;
893 /* do this only the first time we requeue someone */
894 s = lookup_pi_state_for_requeue(uaddr2, hb2,
895 &key2, &pi_state2);
896 if (s) {
897 ret = s;
898 goto out_unlock;
899 }
900
901 lock2 = &pi_state2->pi_mutex;
902 spin_lock(&lock2->wait_lock);
903
904 /* Save the top waiter of the wait_list */
905 if (rt_mutex_has_waiters(lock2))
906 top_waiter = rt_mutex_top_waiter(lock2);
907 } else
908 atomic_inc(&pi_state2->refcount);
909
910
911 this->pi_state = pi_state2;
912
913 /*
914 * SECOND: requeue futex_q to the correct hashbucket
915 */
916
917 /*
918 * If key1 and key2 hash to the same bucket, no need to
919 * requeue.
920 */
921 if (likely(head1 != &hb2->chain)) {
922 plist_del(&this->list, &hb1->chain);
923 plist_add(&this->list, &hb2->chain);
924 this->lock_ptr = &hb2->lock;
925#ifdef CONFIG_DEBUG_PI_LIST
926 this->list.plist.lock = &hb2->lock;
927#endif
928 }
929 this->key = key2;
930 get_futex_key_refs(&key2);
931 drop_count++;
932
933
934 /*
935 * THIRD: queue it to lock2
936 */
937 spin_lock_irq(&this->task->pi_lock);
938 waiter = &this->waiter;
939 waiter->task = this->task;
940 waiter->lock = lock2;
941 plist_node_init(&waiter->list_entry, this->task->prio);
942 plist_node_init(&waiter->pi_list_entry, this->task->prio);
943 plist_add(&waiter->list_entry, &lock2->wait_list);
944 this->task->pi_blocked_on = waiter;
945 spin_unlock_irq(&this->task->pi_lock);
946
947 if (ret - nr_wake >= nr_requeue)
948 break;
949 }
950 }
951
952 /* If we've requeued some tasks and the top_waiter of the rt_mutex
953 has changed, we must adjust the priority of the owner, if any */
954 if (drop_count) {
955 struct task_struct *owner = rt_mutex_owner(lock2);
956 if (owner &&
957 (top_waiter != (waiter = rt_mutex_top_waiter(lock2)))) {
958 int chain_walk = 0;
959
960 spin_lock_irq(&owner->pi_lock);
961 if (top_waiter)
962 plist_del(&top_waiter->pi_list_entry, &owner->pi_waiters);
963 else
964 /*
965 * There was no waiters before the requeue,
966 * the flag must be updated
967 */
968 mark_rt_mutex_waiters(lock2);
969
970 plist_add(&waiter->pi_list_entry, &owner->pi_waiters);
971 __rt_mutex_adjust_prio(owner);
972 if (owner->pi_blocked_on) {
973 chain_walk = 1;
974 get_task_struct(owner);
975 }
976
977 spin_unlock_irq(&owner->pi_lock);
978 spin_unlock(&lock2->wait_lock);
979
980 if (chain_walk)
981 rt_mutex_adjust_prio_chain(owner, 0, lock2, NULL,
982 current);
983 } else {
984 /* No owner or the top_waiter does not change */
985 mark_rt_mutex_waiters(lock2);
986 spin_unlock(&lock2->wait_lock);
987 }
988 }
989
990out_unlock:
991 spin_unlock(&hb1->lock);
992 if (hb1 != hb2)
993 spin_unlock(&hb2->lock);
994
995 /* drop_futex_key_refs() must be called outside the spinlocks. */
996 while (--drop_count >= 0)
997 drop_futex_key_refs(&key1);
998
999out:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001000 if (fshared)
1001 up_read(fshared);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001002 return ret;
1003}
1004
1005/*
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001006 * Wake up all waiters hashed on the physical page that is mapped
1007 * to this virtual address:
1008 */
Ingo Molnare2970f22006-06-27 02:54:47 -07001009static int
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001010futex_wake_op(u32 __user *uaddr1, struct rw_semaphore *fshared,
1011 u32 __user *uaddr2,
Ingo Molnare2970f22006-06-27 02:54:47 -07001012 int nr_wake, int nr_wake2, int op)
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001013{
1014 union futex_key key1, key2;
Ingo Molnare2970f22006-06-27 02:54:47 -07001015 struct futex_hash_bucket *hb1, *hb2;
Pierre Peifferec92d082007-05-09 02:35:00 -07001016 struct plist_head *head;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001017 struct futex_q *this, *next;
1018 int ret, op_ret, attempt = 0;
1019
1020retryfull:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001021 if (fshared)
1022 down_read(fshared);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001023
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001024 ret = get_futex_key(uaddr1, fshared, &key1);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001025 if (unlikely(ret != 0))
1026 goto out;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001027 ret = get_futex_key(uaddr2, fshared, &key2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001028 if (unlikely(ret != 0))
1029 goto out;
1030
Ingo Molnare2970f22006-06-27 02:54:47 -07001031 hb1 = hash_futex(&key1);
1032 hb2 = hash_futex(&key2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001033
1034retry:
Ingo Molnar8b8f3192006-07-03 00:25:05 -07001035 double_lock_hb(hb1, hb2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001036
Ingo Molnare2970f22006-06-27 02:54:47 -07001037 op_ret = futex_atomic_op_inuser(op, uaddr2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001038 if (unlikely(op_ret < 0)) {
Ingo Molnare2970f22006-06-27 02:54:47 -07001039 u32 dummy;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001040
Ingo Molnare2970f22006-06-27 02:54:47 -07001041 spin_unlock(&hb1->lock);
1042 if (hb1 != hb2)
1043 spin_unlock(&hb2->lock);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001044
David Howells7ee1dd32006-01-06 00:11:44 -08001045#ifndef CONFIG_MMU
Ingo Molnare2970f22006-06-27 02:54:47 -07001046 /*
1047 * we don't get EFAULT from MMU faults if we don't have an MMU,
1048 * but we might get them from range checking
1049 */
David Howells7ee1dd32006-01-06 00:11:44 -08001050 ret = op_ret;
1051 goto out;
1052#endif
1053
David Gibson796f8d92005-11-07 00:59:33 -08001054 if (unlikely(op_ret != -EFAULT)) {
1055 ret = op_ret;
1056 goto out;
1057 }
1058
Ingo Molnare2970f22006-06-27 02:54:47 -07001059 /*
1060 * futex_atomic_op_inuser needs to both read and write
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001061 * *(int __user *)uaddr2, but we can't modify it
1062 * non-atomically. Therefore, if get_user below is not
1063 * enough, we need to handle the fault ourselves, while
Ingo Molnare2970f22006-06-27 02:54:47 -07001064 * still holding the mmap_sem.
1065 */
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001066 if (attempt++) {
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001067 ret = futex_handle_fault((unsigned long)uaddr2,
1068 fshared, attempt);
1069 if (ret)
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001070 goto out;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001071 goto retry;
1072 }
1073
Ingo Molnare2970f22006-06-27 02:54:47 -07001074 /*
1075 * If we would have faulted, release mmap_sem,
1076 * fault it in and start all over again.
1077 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001078 if (fshared)
1079 up_read(fshared);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001080
Ingo Molnare2970f22006-06-27 02:54:47 -07001081 ret = get_user(dummy, uaddr2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001082 if (ret)
1083 return ret;
1084
1085 goto retryfull;
1086 }
1087
Ingo Molnare2970f22006-06-27 02:54:47 -07001088 head = &hb1->chain;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001089
Pierre Peifferec92d082007-05-09 02:35:00 -07001090 plist_for_each_entry_safe(this, next, head, list) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001091 if (match_futex (&this->key, &key1)) {
1092 wake_futex(this);
1093 if (++ret >= nr_wake)
1094 break;
1095 }
1096 }
1097
1098 if (op_ret > 0) {
Ingo Molnare2970f22006-06-27 02:54:47 -07001099 head = &hb2->chain;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001100
1101 op_ret = 0;
Pierre Peifferec92d082007-05-09 02:35:00 -07001102 plist_for_each_entry_safe(this, next, head, list) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001103 if (match_futex (&this->key, &key2)) {
1104 wake_futex(this);
1105 if (++op_ret >= nr_wake2)
1106 break;
1107 }
1108 }
1109 ret += op_ret;
1110 }
1111
Ingo Molnare2970f22006-06-27 02:54:47 -07001112 spin_unlock(&hb1->lock);
1113 if (hb1 != hb2)
1114 spin_unlock(&hb2->lock);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001115out:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001116 if (fshared)
1117 up_read(fshared);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001118 return ret;
1119}
1120
1121/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 * Requeue all waiters hashed on one physical page to another
1123 * physical page.
1124 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001125static int futex_requeue(u32 __user *uaddr1, struct rw_semaphore *fshared,
1126 u32 __user *uaddr2,
Ingo Molnare2970f22006-06-27 02:54:47 -07001127 int nr_wake, int nr_requeue, u32 *cmpval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
1129 union futex_key key1, key2;
Ingo Molnare2970f22006-06-27 02:54:47 -07001130 struct futex_hash_bucket *hb1, *hb2;
Pierre Peifferec92d082007-05-09 02:35:00 -07001131 struct plist_head *head1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 struct futex_q *this, *next;
1133 int ret, drop_count = 0;
1134
1135 retry:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001136 if (fshared)
1137 down_read(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001139 ret = get_futex_key(uaddr1, fshared, &key1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (unlikely(ret != 0))
1141 goto out;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001142 ret = get_futex_key(uaddr2, fshared, &key2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (unlikely(ret != 0))
1144 goto out;
1145
Ingo Molnare2970f22006-06-27 02:54:47 -07001146 hb1 = hash_futex(&key1);
1147 hb2 = hash_futex(&key2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Ingo Molnar8b8f3192006-07-03 00:25:05 -07001149 double_lock_hb(hb1, hb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Ingo Molnare2970f22006-06-27 02:54:47 -07001151 if (likely(cmpval != NULL)) {
1152 u32 curval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Ingo Molnare2970f22006-06-27 02:54:47 -07001154 ret = get_futex_value_locked(&curval, uaddr1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 if (unlikely(ret)) {
Ingo Molnare2970f22006-06-27 02:54:47 -07001157 spin_unlock(&hb1->lock);
1158 if (hb1 != hb2)
1159 spin_unlock(&hb2->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Ingo Molnare2970f22006-06-27 02:54:47 -07001161 /*
1162 * If we would have faulted, release mmap_sem, fault
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 * it in and start all over again.
1164 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001165 if (fshared)
1166 up_read(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Ingo Molnare2970f22006-06-27 02:54:47 -07001168 ret = get_user(curval, uaddr1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 if (!ret)
1171 goto retry;
1172
1173 return ret;
1174 }
Ingo Molnare2970f22006-06-27 02:54:47 -07001175 if (curval != *cmpval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 ret = -EAGAIN;
1177 goto out_unlock;
1178 }
1179 }
1180
Ingo Molnare2970f22006-06-27 02:54:47 -07001181 head1 = &hb1->chain;
Pierre Peifferec92d082007-05-09 02:35:00 -07001182 plist_for_each_entry_safe(this, next, head1, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (!match_futex (&this->key, &key1))
1184 continue;
1185 if (++ret <= nr_wake) {
1186 wake_futex(this);
1187 } else {
Sebastien Dugue59e0e0a2006-06-27 02:55:03 -07001188 /*
1189 * If key1 and key2 hash to the same bucket, no need to
1190 * requeue.
1191 */
1192 if (likely(head1 != &hb2->chain)) {
Pierre Peifferec92d082007-05-09 02:35:00 -07001193 plist_del(&this->list, &hb1->chain);
1194 plist_add(&this->list, &hb2->chain);
Sebastien Dugue59e0e0a2006-06-27 02:55:03 -07001195 this->lock_ptr = &hb2->lock;
Pierre Peifferec92d082007-05-09 02:35:00 -07001196#ifdef CONFIG_DEBUG_PI_LIST
1197 this->list.plist.lock = &hb2->lock;
1198#endif
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 this->key = key2;
Rusty Russell9adef582007-05-08 00:26:42 -07001201 get_futex_key_refs(&key2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 drop_count++;
1203
1204 if (ret - nr_wake >= nr_requeue)
1205 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 }
1207 }
1208
1209out_unlock:
Ingo Molnare2970f22006-06-27 02:54:47 -07001210 spin_unlock(&hb1->lock);
1211 if (hb1 != hb2)
1212 spin_unlock(&hb2->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Rusty Russell9adef582007-05-08 00:26:42 -07001214 /* drop_futex_key_refs() must be called outside the spinlocks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 while (--drop_count >= 0)
Rusty Russell9adef582007-05-08 00:26:42 -07001216 drop_futex_key_refs(&key1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218out:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001219 if (fshared)
1220 up_read(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 return ret;
1222}
1223
1224/* The key must be already stored in q->key. */
1225static inline struct futex_hash_bucket *
1226queue_lock(struct futex_q *q, int fd, struct file *filp)
1227{
Ingo Molnare2970f22006-06-27 02:54:47 -07001228 struct futex_hash_bucket *hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 q->fd = fd;
1231 q->filp = filp;
1232
1233 init_waitqueue_head(&q->waiters);
1234
Rusty Russell9adef582007-05-08 00:26:42 -07001235 get_futex_key_refs(&q->key);
Ingo Molnare2970f22006-06-27 02:54:47 -07001236 hb = hash_futex(&q->key);
1237 q->lock_ptr = &hb->lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Ingo Molnare2970f22006-06-27 02:54:47 -07001239 spin_lock(&hb->lock);
1240 return hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
Ingo Molnare2970f22006-06-27 02:54:47 -07001243static inline void __queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
Pierre Peifferec92d082007-05-09 02:35:00 -07001245 int prio;
1246
1247 /*
1248 * The priority used to register this element is
1249 * - either the real thread-priority for the real-time threads
1250 * (i.e. threads with a priority lower than MAX_RT_PRIO)
1251 * - or MAX_RT_PRIO for non-RT threads.
1252 * Thus, all RT-threads are woken first in priority order, and
1253 * the others are woken last, in FIFO order.
1254 */
1255 prio = min(current->normal_prio, MAX_RT_PRIO);
1256
1257 plist_node_init(&q->list, prio);
1258#ifdef CONFIG_DEBUG_PI_LIST
1259 q->list.plist.lock = &hb->lock;
1260#endif
1261 plist_add(&q->list, &hb->chain);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001262 q->task = current;
Ingo Molnare2970f22006-06-27 02:54:47 -07001263 spin_unlock(&hb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264}
1265
1266static inline void
Ingo Molnare2970f22006-06-27 02:54:47 -07001267queue_unlock(struct futex_q *q, struct futex_hash_bucket *hb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
Ingo Molnare2970f22006-06-27 02:54:47 -07001269 spin_unlock(&hb->lock);
Rusty Russell9adef582007-05-08 00:26:42 -07001270 drop_futex_key_refs(&q->key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
1273/*
1274 * queue_me and unqueue_me must be called as a pair, each
1275 * exactly once. They are called with the hashed spinlock held.
1276 */
1277
1278/* The key must be already stored in q->key. */
1279static void queue_me(struct futex_q *q, int fd, struct file *filp)
1280{
Ingo Molnare2970f22006-06-27 02:54:47 -07001281 struct futex_hash_bucket *hb;
1282
1283 hb = queue_lock(q, fd, filp);
1284 __queue_me(q, hb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
1286
1287/* Return 1 if we were still queued (ie. 0 means we were woken) */
1288static int unqueue_me(struct futex_q *q)
1289{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 spinlock_t *lock_ptr;
Ingo Molnare2970f22006-06-27 02:54:47 -07001291 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293 /* In the common case we don't take the spinlock, which is nice. */
1294 retry:
1295 lock_ptr = q->lock_ptr;
Christian Borntraegere91467e2006-08-05 12:13:52 -07001296 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 if (lock_ptr != 0) {
1298 spin_lock(lock_ptr);
1299 /*
1300 * q->lock_ptr can change between reading it and
1301 * spin_lock(), causing us to take the wrong lock. This
1302 * corrects the race condition.
1303 *
1304 * Reasoning goes like this: if we have the wrong lock,
1305 * q->lock_ptr must have changed (maybe several times)
1306 * between reading it and the spin_lock(). It can
1307 * change again after the spin_lock() but only if it was
1308 * already changed before the spin_lock(). It cannot,
1309 * however, change back to the original value. Therefore
1310 * we can detect whether we acquired the correct lock.
1311 */
1312 if (unlikely(lock_ptr != q->lock_ptr)) {
1313 spin_unlock(lock_ptr);
1314 goto retry;
1315 }
Pierre Peifferec92d082007-05-09 02:35:00 -07001316 WARN_ON(plist_node_empty(&q->list));
1317 plist_del(&q->list, &q->list.plist);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001318
1319 BUG_ON(q->pi_state);
1320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 spin_unlock(lock_ptr);
1322 ret = 1;
1323 }
1324
Rusty Russell9adef582007-05-08 00:26:42 -07001325 drop_futex_key_refs(&q->key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 return ret;
1327}
1328
Ingo Molnarc87e2832006-06-27 02:54:58 -07001329/*
1330 * PI futexes can not be requeued and must remove themself from the
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001331 * hash bucket. The hash bucket lock (i.e. lock_ptr) is held on entry
1332 * and dropped here.
Ingo Molnarc87e2832006-06-27 02:54:58 -07001333 */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001334static void unqueue_me_pi(struct futex_q *q)
Ingo Molnarc87e2832006-06-27 02:54:58 -07001335{
Pierre Peifferec92d082007-05-09 02:35:00 -07001336 WARN_ON(plist_node_empty(&q->list));
1337 plist_del(&q->list, &q->list.plist);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001338
1339 BUG_ON(!q->pi_state);
1340 free_pi_state(q->pi_state);
1341 q->pi_state = NULL;
1342
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001343 spin_unlock(q->lock_ptr);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001344
Rusty Russell9adef582007-05-08 00:26:42 -07001345 drop_futex_key_refs(&q->key);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001346}
1347
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001348/*
1349 * Fixup the pi_state owner with current.
1350 *
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001351 * Must be called with hash bucket lock held and mm->sem held for non
1352 * private futexes.
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001353 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001354static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001355 struct task_struct *curr)
1356{
1357 u32 newtid = curr->pid | FUTEX_WAITERS;
1358 struct futex_pi_state *pi_state = q->pi_state;
1359 u32 uval, curval, newval;
1360 int ret;
1361
1362 /* Owner died? */
1363 if (pi_state->owner != NULL) {
1364 spin_lock_irq(&pi_state->owner->pi_lock);
1365 WARN_ON(list_empty(&pi_state->list));
1366 list_del_init(&pi_state->list);
1367 spin_unlock_irq(&pi_state->owner->pi_lock);
1368 } else
1369 newtid |= FUTEX_OWNER_DIED;
1370
1371 pi_state->owner = curr;
1372
1373 spin_lock_irq(&curr->pi_lock);
1374 WARN_ON(!list_empty(&pi_state->list));
1375 list_add(&pi_state->list, &curr->pi_state_list);
1376 spin_unlock_irq(&curr->pi_lock);
1377
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001378 /*
1379 * We own it, so we have to replace the pending owner
1380 * TID. This must be atomic as we have preserve the
1381 * owner died bit here.
1382 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001383 ret = get_futex_value_locked(&uval, uaddr);
1384
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001385 while (!ret) {
1386 newval = (uval & FUTEX_OWNER_DIED) | newtid;
1387 newval |= (uval & FUTEX_WAITER_REQUEUED);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001388
1389 pagefault_disable();
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001390 curval = futex_atomic_cmpxchg_inatomic(uaddr,
1391 uval, newval);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001392 pagefault_enable();
1393
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001394 if (curval == -EFAULT)
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001395 ret = -EFAULT;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001396 if (curval == uval)
1397 break;
1398 uval = curval;
1399 }
1400 return ret;
1401}
1402
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001403/*
1404 * In case we must use restart_block to restart a futex_wait,
1405 * we encode in the 'arg3' shared capability
1406 */
1407#define ARG3_SHARED 1
1408
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001409static long futex_wait_restart(struct restart_block *restart);
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001410static int futex_wait(u32 __user *uaddr, struct rw_semaphore *fshared,
1411 u32 val, ktime_t *abs_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
Ingo Molnarc87e2832006-06-27 02:54:58 -07001413 struct task_struct *curr = current;
1414 DECLARE_WAITQUEUE(wait, curr);
Ingo Molnare2970f22006-06-27 02:54:47 -07001415 struct futex_hash_bucket *hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 struct futex_q q;
Ingo Molnare2970f22006-06-27 02:54:47 -07001417 u32 uval;
1418 int ret;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001419 struct hrtimer_sleeper t, *to = NULL;
Pierre Peifferc19384b2007-05-09 02:35:02 -07001420 int rem = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Ingo Molnarc87e2832006-06-27 02:54:58 -07001422 q.pi_state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 retry:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001424 if (fshared)
1425 down_read(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001427 ret = get_futex_key(uaddr, fshared, &q.key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 if (unlikely(ret != 0))
1429 goto out_release_sem;
1430
Ingo Molnare2970f22006-06-27 02:54:47 -07001431 hb = queue_lock(&q, -1, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 /*
1434 * Access the page AFTER the futex is queued.
1435 * Order is important:
1436 *
1437 * Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val);
1438 * Userspace waker: if (cond(var)) { var = new; futex_wake(&var); }
1439 *
1440 * The basic logical guarantee of a futex is that it blocks ONLY
1441 * if cond(var) is known to be true at the time of blocking, for
1442 * any cond. If we queued after testing *uaddr, that would open
1443 * a race condition where we could block indefinitely with
1444 * cond(var) false, which would violate the guarantee.
1445 *
1446 * A consequence is that futex_wait() can return zero and absorb
1447 * a wakeup when *uaddr != val on entry to the syscall. This is
1448 * rare, but normal.
1449 *
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001450 * for shared futexes, we hold the mmap semaphore, so the mapping
1451 * cannot have changed since we looked it up in get_futex_key.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 */
Ingo Molnare2970f22006-06-27 02:54:47 -07001453 ret = get_futex_value_locked(&uval, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 if (unlikely(ret)) {
Ingo Molnare2970f22006-06-27 02:54:47 -07001456 queue_unlock(&q, hb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Ingo Molnare2970f22006-06-27 02:54:47 -07001458 /*
1459 * If we would have faulted, release mmap_sem, fault it in and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 * start all over again.
1461 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001462 if (fshared)
1463 up_read(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Ingo Molnare2970f22006-06-27 02:54:47 -07001465 ret = get_user(uval, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467 if (!ret)
1468 goto retry;
1469 return ret;
1470 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001471 ret = -EWOULDBLOCK;
1472 if (uval != val)
1473 goto out_unlock_release_sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001475 /*
1476 * This rt_mutex_waiter structure is prepared here and will
1477 * be used only if this task is requeued from a normal futex to
1478 * a PI-futex with futex_requeue_pi.
1479 */
1480 debug_rt_mutex_init_waiter(&q.waiter);
1481 q.waiter.task = NULL;
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 /* Only actually queue if *uaddr contained val. */
Ingo Molnare2970f22006-06-27 02:54:47 -07001484 __queue_me(&q, hb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 /*
1487 * Now the futex is queued and we have checked the data, we
1488 * don't want to hold mmap_sem while we sleep.
Ingo Molnarc87e2832006-06-27 02:54:58 -07001489 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001490 if (fshared)
1491 up_read(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
1493 /*
1494 * There might have been scheduling since the queue_me(), as we
1495 * cannot hold a spinlock across the get_user() in case it
1496 * faults, and we cannot just set TASK_INTERRUPTIBLE state when
1497 * queueing ourselves into the futex hash. This code thus has to
1498 * rely on the futex_wake() code removing us from hash when it
1499 * wakes us up.
1500 */
1501
1502 /* add_wait_queue is the barrier after __set_current_state. */
1503 __set_current_state(TASK_INTERRUPTIBLE);
1504 add_wait_queue(&q.waiters, &wait);
1505 /*
Pierre Peifferec92d082007-05-09 02:35:00 -07001506 * !plist_node_empty() is safe here without any lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 * q.lock_ptr != 0 is not safe, because of ordering against wakeup.
1508 */
Pierre Peifferec92d082007-05-09 02:35:00 -07001509 if (likely(!plist_node_empty(&q.list))) {
Pierre Peifferc19384b2007-05-09 02:35:02 -07001510 if (!abs_time)
1511 schedule();
1512 else {
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001513 to = &t;
Pierre Peifferc19384b2007-05-09 02:35:02 -07001514 hrtimer_init(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1515 hrtimer_init_sleeper(&t, current);
1516 t.timer.expires = *abs_time;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001517
Pierre Peifferc19384b2007-05-09 02:35:02 -07001518 hrtimer_start(&t.timer, t.timer.expires, HRTIMER_MODE_ABS);
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001519
Pierre Peifferc19384b2007-05-09 02:35:02 -07001520 /*
1521 * the timer could have already expired, in which
1522 * case current would be flagged for rescheduling.
1523 * Don't bother calling schedule.
1524 */
1525 if (likely(t.task))
1526 schedule();
1527
1528 hrtimer_cancel(&t.timer);
1529
1530 /* Flag if a timeout occured */
1531 rem = (t.task == NULL);
1532 }
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 __set_current_state(TASK_RUNNING);
1535
1536 /*
1537 * NOTE: we don't remove ourselves from the waitqueue because
1538 * we are the only user of it.
1539 */
1540
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001541 if (q.pi_state) {
1542 /*
1543 * We were woken but have been requeued on a PI-futex.
1544 * We have to complete the lock acquisition by taking
1545 * the rtmutex.
1546 */
1547
1548 struct rt_mutex *lock = &q.pi_state->pi_mutex;
1549
1550 spin_lock(&lock->wait_lock);
1551 if (unlikely(q.waiter.task)) {
1552 remove_waiter(lock, &q.waiter);
1553 }
1554 spin_unlock(&lock->wait_lock);
1555
1556 if (rem)
1557 ret = -ETIMEDOUT;
1558 else
1559 ret = rt_mutex_timed_lock(lock, to, 1);
1560
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001561 if (fshared)
1562 down_read(fshared);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001563 spin_lock(q.lock_ptr);
1564
1565 /*
1566 * Got the lock. We might not be the anticipated owner if we
1567 * did a lock-steal - fix up the PI-state in that case.
1568 */
1569 if (!ret && q.pi_state->owner != curr) {
1570 /*
1571 * We MUST play with the futex we were requeued on,
1572 * NOT the current futex.
1573 * We can retrieve it from the key of the pi_state
1574 */
1575 uaddr = q.pi_state->key.uaddr;
1576
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001577 ret = fixup_pi_state_owner(uaddr, &q, curr);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001578 } else {
1579 /*
1580 * Catch the rare case, where the lock was released
1581 * when we were on the way back before we locked
1582 * the hash bucket.
1583 */
1584 if (ret && q.pi_state->owner == curr) {
1585 if (rt_mutex_trylock(&q.pi_state->pi_mutex))
1586 ret = 0;
1587 }
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001588 }
1589
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001590 /* Unqueue and drop the lock */
1591 unqueue_me_pi(&q);
1592 if (fshared)
1593 up_read(fshared);
1594
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001595 debug_rt_mutex_free_waiter(&q.waiter);
1596
1597 return ret;
1598 }
1599
1600 debug_rt_mutex_free_waiter(&q.waiter);
1601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 /* If we were woken (and unqueued), we succeeded, whatever. */
1603 if (!unqueue_me(&q))
1604 return 0;
Pierre Peifferc19384b2007-05-09 02:35:02 -07001605 if (rem)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 return -ETIMEDOUT;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001607
Ingo Molnare2970f22006-06-27 02:54:47 -07001608 /*
1609 * We expect signal_pending(current), but another thread may
1610 * have handled it for us already.
1611 */
Pierre Peifferc19384b2007-05-09 02:35:02 -07001612 if (!abs_time)
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001613 return -ERESTARTSYS;
1614 else {
1615 struct restart_block *restart;
1616 restart = &current_thread_info()->restart_block;
1617 restart->fn = futex_wait_restart;
1618 restart->arg0 = (unsigned long)uaddr;
1619 restart->arg1 = (unsigned long)val;
Pierre Peifferc19384b2007-05-09 02:35:02 -07001620 restart->arg2 = (unsigned long)abs_time;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001621 restart->arg3 = 0;
1622 if (fshared)
1623 restart->arg3 |= ARG3_SHARED;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001624 return -ERESTART_RESTARTBLOCK;
1625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Ingo Molnarc87e2832006-06-27 02:54:58 -07001627 out_unlock_release_sem:
1628 queue_unlock(&q, hb);
1629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 out_release_sem:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001631 if (fshared)
1632 up_read(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001633 return ret;
1634}
1635
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001636
1637static long futex_wait_restart(struct restart_block *restart)
1638{
1639 u32 __user *uaddr = (u32 __user *)restart->arg0;
1640 u32 val = (u32)restart->arg1;
Pierre Peifferc19384b2007-05-09 02:35:02 -07001641 ktime_t *abs_time = (ktime_t *)restart->arg2;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001642 struct rw_semaphore *fshared = NULL;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001643
1644 restart->fn = do_no_restart_syscall;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001645 if (restart->arg3 & ARG3_SHARED)
1646 fshared = &current->mm->mmap_sem;
1647 return (long)futex_wait(uaddr, fshared, val, abs_time);
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001648}
1649
1650
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001651static void set_pi_futex_owner(struct futex_hash_bucket *hb,
1652 union futex_key *key, struct task_struct *p)
1653{
1654 struct plist_head *head;
1655 struct futex_q *this, *next;
1656 struct futex_pi_state *pi_state = NULL;
1657 struct rt_mutex *lock;
1658
1659 /* Search a waiter that should already exists */
1660
1661 head = &hb->chain;
1662
1663 plist_for_each_entry_safe(this, next, head, list) {
1664 if (match_futex (&this->key, key)) {
1665 pi_state = this->pi_state;
1666 break;
1667 }
1668 }
1669
1670 BUG_ON(!pi_state);
1671
1672 /* set p as pi_state's owner */
1673 lock = &pi_state->pi_mutex;
1674
1675 spin_lock(&lock->wait_lock);
1676 spin_lock_irq(&p->pi_lock);
1677
1678 list_add(&pi_state->list, &p->pi_state_list);
1679 pi_state->owner = p;
1680
1681
1682 /* set p as pi_mutex's owner */
1683 debug_rt_mutex_proxy_lock(lock, p);
1684 WARN_ON(rt_mutex_owner(lock));
1685 rt_mutex_set_owner(lock, p, 0);
1686 rt_mutex_deadlock_account_lock(lock, p);
1687
1688 plist_add(&rt_mutex_top_waiter(lock)->pi_list_entry,
1689 &p->pi_waiters);
1690 __rt_mutex_adjust_prio(p);
1691
1692 spin_unlock_irq(&p->pi_lock);
1693 spin_unlock(&lock->wait_lock);
1694}
1695
Ingo Molnarc87e2832006-06-27 02:54:58 -07001696/*
1697 * Userspace tried a 0 -> TID atomic transition of the futex value
1698 * and failed. The kernel side here does the whole locking operation:
1699 * if there are waiters then it will block, it does PI, etc. (Due to
1700 * races the kernel might see a 0 value of the futex too.)
1701 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001702static int futex_lock_pi(u32 __user *uaddr, struct rw_semaphore *fshared,
1703 int detect, ktime_t *time, int trylock)
Ingo Molnarc87e2832006-06-27 02:54:58 -07001704{
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07001705 struct hrtimer_sleeper timeout, *to = NULL;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001706 struct task_struct *curr = current;
1707 struct futex_hash_bucket *hb;
1708 u32 uval, newval, curval;
1709 struct futex_q q;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001710 int ret, lock_taken, ownerdied = 0, attempt = 0;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001711
1712 if (refill_pi_state_cache())
1713 return -ENOMEM;
1714
Pierre Peifferc19384b2007-05-09 02:35:02 -07001715 if (time) {
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07001716 to = &timeout;
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001717 hrtimer_init(&to->timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07001718 hrtimer_init_sleeper(to, current);
Pierre Peifferc19384b2007-05-09 02:35:02 -07001719 to->timer.expires = *time;
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07001720 }
1721
Ingo Molnarc87e2832006-06-27 02:54:58 -07001722 q.pi_state = NULL;
1723 retry:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001724 if (fshared)
1725 down_read(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001726
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001727 ret = get_futex_key(uaddr, fshared, &q.key);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001728 if (unlikely(ret != 0))
1729 goto out_release_sem;
1730
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001731 retry_unlocked:
Ingo Molnarc87e2832006-06-27 02:54:58 -07001732 hb = queue_lock(&q, -1, NULL);
1733
1734 retry_locked:
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001735 ret = lock_taken = 0;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001736
Ingo Molnarc87e2832006-06-27 02:54:58 -07001737 /*
1738 * To avoid races, we attempt to take the lock here again
1739 * (by doing a 0 -> TID atomic cmpxchg), while holding all
1740 * the locks. It will most likely not succeed.
1741 */
1742 newval = current->pid;
1743
Peter Zijlstraa8663742006-12-06 20:32:20 -08001744 pagefault_disable();
Ingo Molnarc87e2832006-06-27 02:54:58 -07001745 curval = futex_atomic_cmpxchg_inatomic(uaddr, 0, newval);
Peter Zijlstraa8663742006-12-06 20:32:20 -08001746 pagefault_enable();
Ingo Molnarc87e2832006-06-27 02:54:58 -07001747
1748 if (unlikely(curval == -EFAULT))
1749 goto uaddr_faulted;
1750
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001751 /*
1752 * Detect deadlocks. In case of REQUEUE_PI this is a valid
1753 * situation and we return success to user space.
1754 */
Ingo Molnarc87e2832006-06-27 02:54:58 -07001755 if (unlikely((curval & FUTEX_TID_MASK) == current->pid)) {
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001756 if (!(curval & FUTEX_WAITER_REQUEUED))
1757 ret = -EDEADLK;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001758 goto out_unlock_release_sem;
1759 }
1760
1761 /*
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001762 * Surprise - we got the lock. Just return to userspace:
Ingo Molnarc87e2832006-06-27 02:54:58 -07001763 */
1764 if (unlikely(!curval))
1765 goto out_unlock_release_sem;
1766
1767 uval = curval;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001768
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001769 /*
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001770 * Set the WAITERS flag, so the owner will know it has someone
1771 * to wake at next unlock
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001772 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001773 newval = curval | FUTEX_WAITERS;
1774
1775 /*
1776 * There are two cases, where a futex might have no owner (the
1777 * owner TID is 0): OWNER_DIED or REQUEUE. We take over the
1778 * futex in this case. We also do an unconditional take over,
1779 * when the owner of the futex died.
1780 *
1781 * This is safe as we are protected by the hash bucket lock !
1782 */
1783 if (unlikely(ownerdied || !(curval & FUTEX_TID_MASK))) {
1784 /* Keep the OWNER_DIED and REQUEUE bits */
1785 newval = (curval & ~FUTEX_TID_MASK) | current->pid;
1786 ownerdied = 0;
1787 lock_taken = 1;
1788 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001789
Peter Zijlstraa8663742006-12-06 20:32:20 -08001790 pagefault_disable();
Ingo Molnarc87e2832006-06-27 02:54:58 -07001791 curval = futex_atomic_cmpxchg_inatomic(uaddr, uval, newval);
Peter Zijlstraa8663742006-12-06 20:32:20 -08001792 pagefault_enable();
Ingo Molnarc87e2832006-06-27 02:54:58 -07001793
1794 if (unlikely(curval == -EFAULT))
1795 goto uaddr_faulted;
1796 if (unlikely(curval != uval))
1797 goto retry_locked;
1798
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001799 /*
1800 * We took the lock due to requeue or owner died take over.
1801 */
1802 if (unlikely(lock_taken)) {
1803 /* For requeue we need to fixup the pi_futex */
1804 if (curval & FUTEX_WAITER_REQUEUED)
1805 set_pi_futex_owner(hb, &q.key, curr);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001806 goto out_unlock_release_sem;
1807 }
1808
Ingo Molnarc87e2832006-06-27 02:54:58 -07001809 /*
1810 * We dont have the lock. Look up the PI state (or create it if
1811 * we are the first waiter):
1812 */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001813 ret = lookup_pi_state(uval, hb, &q.key, &q.pi_state);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001814
1815 if (unlikely(ret)) {
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001816 switch (ret) {
Ingo Molnarc87e2832006-06-27 02:54:58 -07001817
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001818 case -EAGAIN:
1819 /*
1820 * Task is exiting and we just wait for the
1821 * exit to complete.
1822 */
1823 queue_unlock(&q, hb);
1824 if (fshared)
1825 up_read(fshared);
1826 cond_resched();
1827 goto retry;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001828
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001829 case -ESRCH:
1830 /*
1831 * No owner found for this futex. Check if the
1832 * OWNER_DIED bit is set to figure out whether
1833 * this is a robust futex or not.
1834 */
1835 if (get_futex_value_locked(&curval, uaddr))
Ingo Molnarc87e2832006-06-27 02:54:58 -07001836 goto uaddr_faulted;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001837
1838 /*
1839 * We simply start over in case of a robust
1840 * futex. The code above will take the futex
1841 * and return happy.
1842 */
1843 if (curval & FUTEX_OWNER_DIED) {
1844 ownerdied = 1;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001845 goto retry_locked;
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001846 }
1847 default:
1848 goto out_unlock_release_sem;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001849 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001850 }
1851
1852 /*
1853 * Only actually queue now that the atomic ops are done:
1854 */
1855 __queue_me(&q, hb);
1856
1857 /*
1858 * Now the futex is queued and we have checked the data, we
1859 * don't want to hold mmap_sem while we sleep.
1860 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001861 if (fshared)
1862 up_read(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001863
1864 WARN_ON(!q.pi_state);
1865 /*
1866 * Block on the PI mutex:
1867 */
1868 if (!trylock)
1869 ret = rt_mutex_timed_lock(&q.pi_state->pi_mutex, to, 1);
1870 else {
1871 ret = rt_mutex_trylock(&q.pi_state->pi_mutex);
1872 /* Fixup the trylock return value: */
1873 ret = ret ? 0 : -EWOULDBLOCK;
1874 }
1875
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001876 if (fshared)
1877 down_read(fshared);
Vernon Mauerya99e4e42006-07-01 04:35:42 -07001878 spin_lock(q.lock_ptr);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001879
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001880 if (!ret) {
1881 /*
1882 * Got the lock. We might not be the anticipated owner
1883 * if we did a lock-steal - fix up the PI-state in
1884 * that case:
1885 */
1886 if (q.pi_state->owner != curr)
1887 ret = fixup_pi_state_owner(uaddr, &q, curr);
1888 } else {
Ingo Molnarc87e2832006-06-27 02:54:58 -07001889 /*
1890 * Catch the rare case, where the lock was released
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001891 * when we were on the way back before we locked the
1892 * hash bucket.
Ingo Molnarc87e2832006-06-27 02:54:58 -07001893 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001894 if (q.pi_state->owner == curr &&
1895 rt_mutex_trylock(&q.pi_state->pi_mutex)) {
1896 ret = 0;
1897 } else {
1898 /*
1899 * Paranoia check. If we did not take the lock
1900 * in the trylock above, then we should not be
1901 * the owner of the rtmutex, neither the real
1902 * nor the pending one:
1903 */
1904 if (rt_mutex_owner(&q.pi_state->pi_mutex) == curr)
1905 printk(KERN_ERR "futex_lock_pi: ret = %d "
1906 "pi-mutex: %p pi-state %p\n", ret,
1907 q.pi_state->pi_mutex.owner,
1908 q.pi_state->owner);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001909 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001910 }
1911
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001912 /* Unqueue and drop the lock */
1913 unqueue_me_pi(&q);
1914 if (fshared)
1915 up_read(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001916
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07001917 return ret != -EINTR ? ret : -ERESTARTNOINTR;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001918
1919 out_unlock_release_sem:
1920 queue_unlock(&q, hb);
1921
1922 out_release_sem:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001923 if (fshared)
1924 up_read(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001925 return ret;
1926
1927 uaddr_faulted:
1928 /*
1929 * We have to r/w *(int __user *)uaddr, but we can't modify it
1930 * non-atomically. Therefore, if get_user below is not
1931 * enough, we need to handle the fault ourselves, while
1932 * still holding the mmap_sem.
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001933 *
1934 * ... and hb->lock. :-) --ANK
Ingo Molnarc87e2832006-06-27 02:54:58 -07001935 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001936 queue_unlock(&q, hb);
1937
Ingo Molnarc87e2832006-06-27 02:54:58 -07001938 if (attempt++) {
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001939 ret = futex_handle_fault((unsigned long)uaddr, fshared,
1940 attempt);
1941 if (ret)
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001942 goto out_release_sem;
1943 goto retry_unlocked;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001944 }
1945
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001946 if (fshared)
1947 up_read(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001948
1949 ret = get_user(uval, uaddr);
1950 if (!ret && (uval != -EFAULT))
1951 goto retry;
1952
1953 return ret;
1954}
1955
1956/*
Ingo Molnarc87e2832006-06-27 02:54:58 -07001957 * Userspace attempted a TID -> 0 atomic transition, and failed.
1958 * This is the in-kernel slowpath: we look up the PI state (if any),
1959 * and do the rt-mutex unlock.
1960 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001961static int futex_unlock_pi(u32 __user *uaddr, struct rw_semaphore *fshared)
Ingo Molnarc87e2832006-06-27 02:54:58 -07001962{
1963 struct futex_hash_bucket *hb;
1964 struct futex_q *this, *next;
1965 u32 uval;
Pierre Peifferec92d082007-05-09 02:35:00 -07001966 struct plist_head *head;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001967 union futex_key key;
1968 int ret, attempt = 0;
1969
1970retry:
1971 if (get_user(uval, uaddr))
1972 return -EFAULT;
1973 /*
1974 * We release only a lock we actually own:
1975 */
1976 if ((uval & FUTEX_TID_MASK) != current->pid)
1977 return -EPERM;
1978 /*
1979 * First take all the futex related locks:
1980 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001981 if (fshared)
1982 down_read(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001983
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001984 ret = get_futex_key(uaddr, fshared, &key);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001985 if (unlikely(ret != 0))
1986 goto out;
1987
1988 hb = hash_futex(&key);
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07001989retry_unlocked:
Ingo Molnarc87e2832006-06-27 02:54:58 -07001990 spin_lock(&hb->lock);
1991
Ingo Molnarc87e2832006-06-27 02:54:58 -07001992 /*
1993 * To avoid races, try to do the TID -> 0 atomic transition
1994 * again. If it succeeds then we can return without waking
1995 * anyone else up:
1996 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001997 if (!(uval & FUTEX_OWNER_DIED)) {
Peter Zijlstraa8663742006-12-06 20:32:20 -08001998 pagefault_disable();
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001999 uval = futex_atomic_cmpxchg_inatomic(uaddr, current->pid, 0);
Peter Zijlstraa8663742006-12-06 20:32:20 -08002000 pagefault_enable();
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002001 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07002002
2003 if (unlikely(uval == -EFAULT))
2004 goto pi_faulted;
2005 /*
2006 * Rare case: we managed to release the lock atomically,
2007 * no need to wake anyone else up:
2008 */
2009 if (unlikely(uval == current->pid))
2010 goto out_unlock;
2011
2012 /*
2013 * Ok, other tasks may need to be woken up - check waiters
2014 * and do the wakeup if necessary:
2015 */
2016 head = &hb->chain;
2017
Pierre Peifferec92d082007-05-09 02:35:00 -07002018 plist_for_each_entry_safe(this, next, head, list) {
Ingo Molnarc87e2832006-06-27 02:54:58 -07002019 if (!match_futex (&this->key, &key))
2020 continue;
2021 ret = wake_futex_pi(uaddr, uval, this);
2022 /*
2023 * The atomic access to the futex value
2024 * generated a pagefault, so retry the
2025 * user-access and the wakeup:
2026 */
2027 if (ret == -EFAULT)
2028 goto pi_faulted;
2029 goto out_unlock;
2030 }
2031 /*
2032 * No waiters - kernel unlocks the futex:
2033 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002034 if (!(uval & FUTEX_OWNER_DIED)) {
2035 ret = unlock_futex_pi(uaddr, uval);
2036 if (ret == -EFAULT)
2037 goto pi_faulted;
2038 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07002039
2040out_unlock:
2041 spin_unlock(&hb->lock);
2042out:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002043 if (fshared)
2044 up_read(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002045
2046 return ret;
2047
2048pi_faulted:
2049 /*
2050 * We have to r/w *(int __user *)uaddr, but we can't modify it
2051 * non-atomically. Therefore, if get_user below is not
2052 * enough, we need to handle the fault ourselves, while
2053 * still holding the mmap_sem.
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002054 *
2055 * ... and hb->lock. --ANK
Ingo Molnarc87e2832006-06-27 02:54:58 -07002056 */
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002057 spin_unlock(&hb->lock);
2058
Ingo Molnarc87e2832006-06-27 02:54:58 -07002059 if (attempt++) {
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002060 ret = futex_handle_fault((unsigned long)uaddr, fshared,
2061 attempt);
2062 if (ret)
Alexey Kuznetsov778e9a92007-06-08 13:47:00 -07002063 goto out;
2064 goto retry_unlocked;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002065 }
2066
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002067 if (fshared)
2068 up_read(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002069
2070 ret = get_user(uval, uaddr);
2071 if (!ret && (uval != -EFAULT))
2072 goto retry;
2073
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 return ret;
2075}
2076
2077static int futex_close(struct inode *inode, struct file *filp)
2078{
2079 struct futex_q *q = filp->private_data;
2080
2081 unqueue_me(q);
2082 kfree(q);
Ingo Molnare2970f22006-06-27 02:54:47 -07002083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 return 0;
2085}
2086
2087/* This is one-shot: once it's gone off you need a new fd */
2088static unsigned int futex_poll(struct file *filp,
2089 struct poll_table_struct *wait)
2090{
2091 struct futex_q *q = filp->private_data;
2092 int ret = 0;
2093
2094 poll_wait(filp, &q->waiters, wait);
2095
2096 /*
Pierre Peifferec92d082007-05-09 02:35:00 -07002097 * plist_node_empty() is safe here without any lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 * q->lock_ptr != 0 is not safe, because of ordering against wakeup.
2099 */
Pierre Peifferec92d082007-05-09 02:35:00 -07002100 if (plist_node_empty(&q->list))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 ret = POLLIN | POLLRDNORM;
2102
2103 return ret;
2104}
2105
Helge Deller15ad7cd2006-12-06 20:40:36 -08002106static const struct file_operations futex_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 .release = futex_close,
2108 .poll = futex_poll,
2109};
2110
2111/*
2112 * Signal allows caller to avoid the race which would occur if they
2113 * set the sigio stuff up afterwards.
2114 */
Ingo Molnare2970f22006-06-27 02:54:47 -07002115static int futex_fd(u32 __user *uaddr, int signal)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116{
2117 struct futex_q *q;
2118 struct file *filp;
2119 int ret, err;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002120 struct rw_semaphore *fshared;
Andrew Morton19c6b6e2006-11-02 22:07:17 -08002121 static unsigned long printk_interval;
2122
2123 if (printk_timed_ratelimit(&printk_interval, 60 * 60 * 1000)) {
2124 printk(KERN_WARNING "Process `%s' used FUTEX_FD, which "
2125 "will be removed from the kernel in June 2007\n",
2126 current->comm);
2127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
2129 ret = -EINVAL;
Jesper Juhl7ed20e12005-05-01 08:59:14 -07002130 if (!valid_signal(signal))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 goto out;
2132
2133 ret = get_unused_fd();
2134 if (ret < 0)
2135 goto out;
2136 filp = get_empty_filp();
2137 if (!filp) {
2138 put_unused_fd(ret);
2139 ret = -ENFILE;
2140 goto out;
2141 }
2142 filp->f_op = &futex_fops;
Josef "Jeff" Sipekf3a43f32006-12-08 02:36:43 -08002143 filp->f_path.mnt = mntget(futex_mnt);
2144 filp->f_path.dentry = dget(futex_mnt->mnt_root);
2145 filp->f_mapping = filp->f_path.dentry->d_inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
2147 if (signal) {
Eric W. Biederman609d7fa2006-10-02 02:17:15 -07002148 err = __f_setown(filp, task_pid(current), PIDTYPE_PID, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 if (err < 0) {
Pekka Enberg39ed3fd2005-09-06 15:17:44 -07002150 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 }
2152 filp->f_owner.signum = signal;
2153 }
2154
2155 q = kmalloc(sizeof(*q), GFP_KERNEL);
2156 if (!q) {
Pekka Enberg39ed3fd2005-09-06 15:17:44 -07002157 err = -ENOMEM;
2158 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07002160 q->pi_state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002162 fshared = &current->mm->mmap_sem;
2163 down_read(fshared);
2164 err = get_futex_key(uaddr, fshared, &q->key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
2166 if (unlikely(err != 0)) {
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002167 up_read(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 kfree(q);
Pekka Enberg39ed3fd2005-09-06 15:17:44 -07002169 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 }
2171
2172 /*
2173 * queue_me() must be called before releasing mmap_sem, because
2174 * key->shared.inode needs to be referenced while holding it.
2175 */
2176 filp->private_data = q;
2177
2178 queue_me(q, ret, filp);
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002179 up_read(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
2181 /* Now we map fd to filp, so userspace can access it */
2182 fd_install(ret, filp);
2183out:
2184 return ret;
Pekka Enberg39ed3fd2005-09-06 15:17:44 -07002185error:
2186 put_unused_fd(ret);
2187 put_filp(filp);
2188 ret = err;
2189 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190}
2191
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002192/*
2193 * Support for robust futexes: the kernel cleans up held futexes at
2194 * thread exit time.
2195 *
2196 * Implementation: user-space maintains a per-thread list of locks it
2197 * is holding. Upon do_exit(), the kernel carefully walks this list,
2198 * and marks all locks that are owned by this thread with the
Ingo Molnarc87e2832006-06-27 02:54:58 -07002199 * FUTEX_OWNER_DIED bit, and wakes up a waiter (if any). The list is
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002200 * always manipulated with the lock held, so the list is private and
2201 * per-thread. Userspace also maintains a per-thread 'list_op_pending'
2202 * field, to allow the kernel to clean up if the thread dies after
2203 * acquiring the lock, but just before it could have added itself to
2204 * the list. There can only be one such pending lock.
2205 */
2206
2207/**
2208 * sys_set_robust_list - set the robust-futex list head of a task
2209 * @head: pointer to the list-head
2210 * @len: length of the list-head, as userspace expects
2211 */
2212asmlinkage long
2213sys_set_robust_list(struct robust_list_head __user *head,
2214 size_t len)
2215{
2216 /*
2217 * The kernel knows only one size for now:
2218 */
2219 if (unlikely(len != sizeof(*head)))
2220 return -EINVAL;
2221
2222 current->robust_list = head;
2223
2224 return 0;
2225}
2226
2227/**
2228 * sys_get_robust_list - get the robust-futex list head of a task
2229 * @pid: pid of the process [zero for current task]
2230 * @head_ptr: pointer to a list-head pointer, the kernel fills it in
2231 * @len_ptr: pointer to a length field, the kernel fills in the header size
2232 */
2233asmlinkage long
Al Viroba46df92006-10-10 22:46:07 +01002234sys_get_robust_list(int pid, struct robust_list_head __user * __user *head_ptr,
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002235 size_t __user *len_ptr)
2236{
Al Viroba46df92006-10-10 22:46:07 +01002237 struct robust_list_head __user *head;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002238 unsigned long ret;
2239
2240 if (!pid)
2241 head = current->robust_list;
2242 else {
2243 struct task_struct *p;
2244
2245 ret = -ESRCH;
Oleg Nesterovaaa2a972006-09-29 02:00:55 -07002246 rcu_read_lock();
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002247 p = find_task_by_pid(pid);
2248 if (!p)
2249 goto err_unlock;
2250 ret = -EPERM;
2251 if ((current->euid != p->euid) && (current->euid != p->uid) &&
2252 !capable(CAP_SYS_PTRACE))
2253 goto err_unlock;
2254 head = p->robust_list;
Oleg Nesterovaaa2a972006-09-29 02:00:55 -07002255 rcu_read_unlock();
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002256 }
2257
2258 if (put_user(sizeof(*head), len_ptr))
2259 return -EFAULT;
2260 return put_user(head, head_ptr);
2261
2262err_unlock:
Oleg Nesterovaaa2a972006-09-29 02:00:55 -07002263 rcu_read_unlock();
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002264
2265 return ret;
2266}
2267
2268/*
2269 * Process a futex-list entry, check whether it's owned by the
2270 * dying task, and do notification if so:
2271 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002272int handle_futex_death(u32 __user *uaddr, struct task_struct *curr, int pi)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002273{
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002274 u32 uval, nval, mval;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002275
Ingo Molnar8f17d3a2006-03-27 01:16:27 -08002276retry:
2277 if (get_user(uval, uaddr))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002278 return -1;
2279
Ingo Molnar8f17d3a2006-03-27 01:16:27 -08002280 if ((uval & FUTEX_TID_MASK) == curr->pid) {
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002281 /*
2282 * Ok, this dying thread is truly holding a futex
2283 * of interest. Set the OWNER_DIED bit atomically
2284 * via cmpxchg, and if the value had FUTEX_WAITERS
2285 * set, wake up a waiter (if any). (We have to do a
2286 * futex_wake() even if OWNER_DIED is already set -
2287 * to handle the rare but possible case of recursive
2288 * thread-death.) The rest of the cleanup is done in
2289 * userspace.
2290 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002291 mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002292 /* Also keep the FUTEX_WAITER_REQUEUED flag if set */
2293 mval |= (uval & FUTEX_WAITER_REQUEUED);
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002294 nval = futex_atomic_cmpxchg_inatomic(uaddr, uval, mval);
2295
Ingo Molnarc87e2832006-06-27 02:54:58 -07002296 if (nval == -EFAULT)
2297 return -1;
2298
2299 if (nval != uval)
Ingo Molnar8f17d3a2006-03-27 01:16:27 -08002300 goto retry;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002301
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002302 /*
2303 * Wake robust non-PI futexes here. The wakeup of
2304 * PI futexes happens in exit_pi_state():
2305 */
2306 if (!pi) {
2307 if (uval & FUTEX_WAITERS)
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002308 futex_wake(uaddr, &curr->mm->mmap_sem, 1);
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002309 }
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002310 }
2311 return 0;
2312}
2313
2314/*
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002315 * Fetch a robust-list pointer. Bit 0 signals PI futexes:
2316 */
2317static inline int fetch_robust_entry(struct robust_list __user **entry,
Al Viroba46df92006-10-10 22:46:07 +01002318 struct robust_list __user * __user *head,
2319 int *pi)
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002320{
2321 unsigned long uentry;
2322
Al Viroba46df92006-10-10 22:46:07 +01002323 if (get_user(uentry, (unsigned long __user *)head))
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002324 return -EFAULT;
2325
Al Viroba46df92006-10-10 22:46:07 +01002326 *entry = (void __user *)(uentry & ~1UL);
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002327 *pi = uentry & 1;
2328
2329 return 0;
2330}
2331
2332/*
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002333 * Walk curr->robust_list (very carefully, it's a userspace list!)
2334 * and mark any locks found there dead, and notify any waiters.
2335 *
2336 * We silently return on any sign of list-walking problem.
2337 */
2338void exit_robust_list(struct task_struct *curr)
2339{
2340 struct robust_list_head __user *head = curr->robust_list;
2341 struct robust_list __user *entry, *pending;
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002342 unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002343 unsigned long futex_offset;
2344
2345 /*
2346 * Fetch the list head (which was registered earlier, via
2347 * sys_set_robust_list()):
2348 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002349 if (fetch_robust_entry(&entry, &head->list.next, &pi))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002350 return;
2351 /*
2352 * Fetch the relative futex offset:
2353 */
2354 if (get_user(futex_offset, &head->futex_offset))
2355 return;
2356 /*
2357 * Fetch any possibly pending lock-add first, and handle it
2358 * if it exists:
2359 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002360 if (fetch_robust_entry(&pending, &head->list_op_pending, &pip))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002361 return;
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002362
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002363 if (pending)
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002364 handle_futex_death((void __user *)pending + futex_offset,
2365 curr, pip);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002366
2367 while (entry != &head->list) {
2368 /*
2369 * A pending lock might already be on the list, so
Ingo Molnarc87e2832006-06-27 02:54:58 -07002370 * don't process it twice:
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002371 */
2372 if (entry != pending)
Al Viroba46df92006-10-10 22:46:07 +01002373 if (handle_futex_death((void __user *)entry + futex_offset,
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002374 curr, pi))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002375 return;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002376 /*
2377 * Fetch the next entry in the list:
2378 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002379 if (fetch_robust_entry(&entry, &entry->next, &pi))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002380 return;
2381 /*
2382 * Avoid excessively long or circular lists:
2383 */
2384 if (!--limit)
2385 break;
2386
2387 cond_resched();
2388 }
2389}
2390
Pierre Peifferc19384b2007-05-09 02:35:02 -07002391long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
Ingo Molnare2970f22006-06-27 02:54:47 -07002392 u32 __user *uaddr2, u32 val2, u32 val3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393{
2394 int ret;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002395 int cmd = op & FUTEX_CMD_MASK;
2396 struct rw_semaphore *fshared = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002398 if (!(op & FUTEX_PRIVATE_FLAG))
2399 fshared = &current->mm->mmap_sem;
2400
2401 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 case FUTEX_WAIT:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002403 ret = futex_wait(uaddr, fshared, val, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 break;
2405 case FUTEX_WAKE:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002406 ret = futex_wake(uaddr, fshared, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 break;
2408 case FUTEX_FD:
2409 /* non-zero val means F_SETOWN(getpid()) & F_SETSIG(val) */
2410 ret = futex_fd(uaddr, val);
2411 break;
2412 case FUTEX_REQUEUE:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002413 ret = futex_requeue(uaddr, fshared, uaddr2, val, val2, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 break;
2415 case FUTEX_CMP_REQUEUE:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002416 ret = futex_requeue(uaddr, fshared, uaddr2, val, val2, &val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 break;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07002418 case FUTEX_WAKE_OP:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002419 ret = futex_wake_op(uaddr, fshared, uaddr2, val, val2, val3);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07002420 break;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002421 case FUTEX_LOCK_PI:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002422 ret = futex_lock_pi(uaddr, fshared, val, timeout, 0);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002423 break;
2424 case FUTEX_UNLOCK_PI:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002425 ret = futex_unlock_pi(uaddr, fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002426 break;
2427 case FUTEX_TRYLOCK_PI:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002428 ret = futex_lock_pi(uaddr, fshared, 0, timeout, 1);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002429 break;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002430 case FUTEX_CMP_REQUEUE_PI:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002431 ret = futex_requeue_pi(uaddr, fshared, uaddr2, val, val2, &val3);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002432 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 default:
2434 ret = -ENOSYS;
2435 }
2436 return ret;
2437}
2438
2439
Ingo Molnare2970f22006-06-27 02:54:47 -07002440asmlinkage long sys_futex(u32 __user *uaddr, int op, u32 val,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 struct timespec __user *utime, u32 __user *uaddr2,
Ingo Molnare2970f22006-06-27 02:54:47 -07002442 u32 val3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443{
Pierre Peifferc19384b2007-05-09 02:35:02 -07002444 struct timespec ts;
2445 ktime_t t, *tp = NULL;
Ingo Molnare2970f22006-06-27 02:54:47 -07002446 u32 val2 = 0;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002447 int cmd = op & FUTEX_CMD_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002449 if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI)) {
Pierre Peifferc19384b2007-05-09 02:35:02 -07002450 if (copy_from_user(&ts, utime, sizeof(ts)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 return -EFAULT;
Pierre Peifferc19384b2007-05-09 02:35:02 -07002452 if (!timespec_valid(&ts))
Thomas Gleixner9741ef92006-03-31 02:31:32 -08002453 return -EINVAL;
Pierre Peifferc19384b2007-05-09 02:35:02 -07002454
2455 t = timespec_to_ktime(ts);
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002456 if (cmd == FUTEX_WAIT)
Pierre Peifferc19384b2007-05-09 02:35:02 -07002457 t = ktime_add(ktime_get(), t);
2458 tp = &t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 }
2460 /*
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002461 * requeue parameter in 'utime' if cmd == FUTEX_REQUEUE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002463 if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE
2464 || cmd == FUTEX_CMP_REQUEUE_PI)
Ingo Molnare2970f22006-06-27 02:54:47 -07002465 val2 = (u32) (unsigned long) utime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
Pierre Peifferc19384b2007-05-09 02:35:02 -07002467 return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468}
2469
David Howells454e2392006-06-23 02:02:57 -07002470static int futexfs_get_sb(struct file_system_type *fs_type,
2471 int flags, const char *dev_name, void *data,
2472 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473{
David Howells454e2392006-06-23 02:02:57 -07002474 return get_sb_pseudo(fs_type, "futex", NULL, 0xBAD1DEA, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475}
2476
2477static struct file_system_type futex_fs_type = {
2478 .name = "futexfs",
2479 .get_sb = futexfs_get_sb,
2480 .kill_sb = kill_anon_super,
2481};
2482
2483static int __init init(void)
2484{
Akinobu Mita95362fa2006-12-06 20:39:03 -08002485 int i = register_filesystem(&futex_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
Akinobu Mita95362fa2006-12-06 20:39:03 -08002487 if (i)
2488 return i;
2489
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 futex_mnt = kern_mount(&futex_fs_type);
Akinobu Mita95362fa2006-12-06 20:39:03 -08002491 if (IS_ERR(futex_mnt)) {
2492 unregister_filesystem(&futex_fs_type);
2493 return PTR_ERR(futex_mnt);
2494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
2496 for (i = 0; i < ARRAY_SIZE(futex_queues); i++) {
Pierre Peifferec92d082007-05-09 02:35:00 -07002497 plist_head_init(&futex_queues[i].chain, &futex_queues[i].lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 spin_lock_init(&futex_queues[i].lock);
2499 }
2500 return 0;
2501}
2502__initcall(init);