blob: b7ce15c67e324b468d13599d47df7423adcd69d4 [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 }
Oleg Nesterovd015bae2006-08-27 01:23:40 -0700433 if (p->exit_state != 0) {
Ingo Molnarc87e2832006-06-27 02:54:58 -0700434 p = NULL;
435 goto out_unlock;
436 }
437 get_task_struct(p);
438out_unlock:
Oleg Nesterovd359b542006-09-29 02:00:55 -0700439 rcu_read_unlock();
Ingo Molnarc87e2832006-06-27 02:54:58 -0700440
441 return p;
442}
443
444/*
445 * This task is holding PI mutexes at exit time => bad.
446 * Kernel cleans up PI-state, but userspace is likely hosed.
447 * (Robust-futex cleanup is separate and might save the day for userspace.)
448 */
449void exit_pi_state_list(struct task_struct *curr)
450{
Ingo Molnarc87e2832006-06-27 02:54:58 -0700451 struct list_head *next, *head = &curr->pi_state_list;
452 struct futex_pi_state *pi_state;
Ingo Molnar627371d2006-07-29 05:16:20 +0200453 struct futex_hash_bucket *hb;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700454 union futex_key key;
455
456 /*
457 * We are a ZOMBIE and nobody can enqueue itself on
458 * pi_state_list anymore, but we have to be careful
Ingo Molnar627371d2006-07-29 05:16:20 +0200459 * versus waiters unqueueing themselves:
Ingo Molnarc87e2832006-06-27 02:54:58 -0700460 */
461 spin_lock_irq(&curr->pi_lock);
462 while (!list_empty(head)) {
463
464 next = head->next;
465 pi_state = list_entry(next, struct futex_pi_state, list);
466 key = pi_state->key;
Ingo Molnar627371d2006-07-29 05:16:20 +0200467 hb = hash_futex(&key);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700468 spin_unlock_irq(&curr->pi_lock);
469
Ingo Molnarc87e2832006-06-27 02:54:58 -0700470 spin_lock(&hb->lock);
471
472 spin_lock_irq(&curr->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +0200473 /*
474 * We dropped the pi-lock, so re-check whether this
475 * task still owns the PI-state:
476 */
Ingo Molnarc87e2832006-06-27 02:54:58 -0700477 if (head->next != next) {
478 spin_unlock(&hb->lock);
479 continue;
480 }
481
Ingo Molnarc87e2832006-06-27 02:54:58 -0700482 WARN_ON(pi_state->owner != curr);
Ingo Molnar627371d2006-07-29 05:16:20 +0200483 WARN_ON(list_empty(&pi_state->list));
484 list_del_init(&pi_state->list);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700485 pi_state->owner = NULL;
486 spin_unlock_irq(&curr->pi_lock);
487
488 rt_mutex_unlock(&pi_state->pi_mutex);
489
490 spin_unlock(&hb->lock);
491
492 spin_lock_irq(&curr->pi_lock);
493 }
494 spin_unlock_irq(&curr->pi_lock);
495}
496
497static int
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700498lookup_pi_state(u32 uval, struct futex_hash_bucket *hb,
499 union futex_key *key, struct futex_pi_state **ps)
Ingo Molnarc87e2832006-06-27 02:54:58 -0700500{
501 struct futex_pi_state *pi_state = NULL;
502 struct futex_q *this, *next;
Pierre Peifferec92d082007-05-09 02:35:00 -0700503 struct plist_head *head;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700504 struct task_struct *p;
505 pid_t pid;
506
507 head = &hb->chain;
508
Pierre Peifferec92d082007-05-09 02:35:00 -0700509 plist_for_each_entry_safe(this, next, head, list) {
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700510 if (match_futex(&this->key, key)) {
Ingo Molnarc87e2832006-06-27 02:54:58 -0700511 /*
512 * Another waiter already exists - bump up
513 * the refcount and return its pi_state:
514 */
515 pi_state = this->pi_state;
Thomas Gleixner06a9ec22006-07-10 04:44:30 -0700516 /*
517 * Userspace might have messed up non PI and PI futexes
518 */
519 if (unlikely(!pi_state))
520 return -EINVAL;
521
Ingo Molnar627371d2006-07-29 05:16:20 +0200522 WARN_ON(!atomic_read(&pi_state->refcount));
523
Ingo Molnarc87e2832006-06-27 02:54:58 -0700524 atomic_inc(&pi_state->refcount);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700525 *ps = pi_state;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700526
527 return 0;
528 }
529 }
530
531 /*
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200532 * We are the first waiter - try to look up the real owner and attach
533 * the new pi_state to it, but bail out when the owner died bit is set
534 * and TID = 0:
Ingo Molnarc87e2832006-06-27 02:54:58 -0700535 */
536 pid = uval & FUTEX_TID_MASK;
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200537 if (!pid && (uval & FUTEX_OWNER_DIED))
538 return -ESRCH;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700539 p = futex_find_get_task(pid);
540 if (!p)
541 return -ESRCH;
542
543 pi_state = alloc_pi_state();
544
545 /*
546 * Initialize the pi_mutex in locked state and make 'p'
547 * the owner of it:
548 */
549 rt_mutex_init_proxy_locked(&pi_state->pi_mutex, p);
550
551 /* Store the key for possible exit cleanups: */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700552 pi_state->key = *key;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700553
554 spin_lock_irq(&p->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +0200555 WARN_ON(!list_empty(&pi_state->list));
Ingo Molnarc87e2832006-06-27 02:54:58 -0700556 list_add(&pi_state->list, &p->pi_state_list);
557 pi_state->owner = p;
558 spin_unlock_irq(&p->pi_lock);
559
560 put_task_struct(p);
561
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700562 *ps = pi_state;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700563
564 return 0;
565}
566
567/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 * The hash bucket lock must be held when this is called.
569 * Afterwards, the futex_q must not be accessed.
570 */
571static void wake_futex(struct futex_q *q)
572{
Pierre Peifferec92d082007-05-09 02:35:00 -0700573 plist_del(&q->list, &q->list.plist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (q->filp)
575 send_sigio(&q->filp->f_owner, q->fd, POLL_IN);
576 /*
577 * The lock in wake_up_all() is a crucial memory barrier after the
Pierre Peifferec92d082007-05-09 02:35:00 -0700578 * plist_del() and also before assigning to q->lock_ptr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 */
580 wake_up_all(&q->waiters);
581 /*
582 * The waiting task can free the futex_q as soon as this is written,
583 * without taking any locks. This must come last.
Andrew Morton8e311082005-12-23 19:54:46 -0800584 *
585 * A memory barrier is required here to prevent the following store
586 * to lock_ptr from getting ahead of the wakeup. Clearing the lock
587 * at the end of wake_up_all() does not prevent this store from
588 * moving.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 */
Ralf Baechleccdea2f2006-12-06 20:40:26 -0800590 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 q->lock_ptr = NULL;
592}
593
Ingo Molnarc87e2832006-06-27 02:54:58 -0700594static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this)
595{
596 struct task_struct *new_owner;
597 struct futex_pi_state *pi_state = this->pi_state;
598 u32 curval, newval;
599
600 if (!pi_state)
601 return -EINVAL;
602
Ingo Molnar217788672007-03-16 13:38:31 -0800603 spin_lock(&pi_state->pi_mutex.wait_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700604 new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);
605
606 /*
607 * This happens when we have stolen the lock and the original
608 * pending owner did not enqueue itself back on the rt_mutex.
609 * Thats not a tragedy. We know that way, that a lock waiter
610 * is on the fly. We make the futex_q waiter the pending owner.
611 */
612 if (!new_owner)
613 new_owner = this->task;
614
615 /*
616 * We pass it to the next owner. (The WAITERS bit is always
617 * kept enabled while there is PI state around. We must also
618 * preserve the owner died bit.)
619 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200620 if (!(uval & FUTEX_OWNER_DIED)) {
621 newval = FUTEX_WAITERS | new_owner->pid;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700622 /* Keep the FUTEX_WAITER_REQUEUED flag if it was set */
623 newval |= (uval & FUTEX_WAITER_REQUEUED);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700624
Peter Zijlstraa8663742006-12-06 20:32:20 -0800625 pagefault_disable();
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200626 curval = futex_atomic_cmpxchg_inatomic(uaddr, uval, newval);
Peter Zijlstraa8663742006-12-06 20:32:20 -0800627 pagefault_enable();
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200628 if (curval == -EFAULT)
629 return -EFAULT;
630 if (curval != uval)
631 return -EINVAL;
632 }
Ingo Molnarc87e2832006-06-27 02:54:58 -0700633
Ingo Molnar627371d2006-07-29 05:16:20 +0200634 spin_lock_irq(&pi_state->owner->pi_lock);
635 WARN_ON(list_empty(&pi_state->list));
636 list_del_init(&pi_state->list);
637 spin_unlock_irq(&pi_state->owner->pi_lock);
638
639 spin_lock_irq(&new_owner->pi_lock);
640 WARN_ON(!list_empty(&pi_state->list));
Ingo Molnarc87e2832006-06-27 02:54:58 -0700641 list_add(&pi_state->list, &new_owner->pi_state_list);
642 pi_state->owner = new_owner;
Ingo Molnar627371d2006-07-29 05:16:20 +0200643 spin_unlock_irq(&new_owner->pi_lock);
644
Ingo Molnar217788672007-03-16 13:38:31 -0800645 spin_unlock(&pi_state->pi_mutex.wait_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700646 rt_mutex_unlock(&pi_state->pi_mutex);
647
648 return 0;
649}
650
651static int unlock_futex_pi(u32 __user *uaddr, u32 uval)
652{
653 u32 oldval;
654
655 /*
656 * There is no waiter, so we unlock the futex. The owner died
657 * bit has not to be preserved here. We are the owner:
658 */
Peter Zijlstraa8663742006-12-06 20:32:20 -0800659 pagefault_disable();
Ingo Molnarc87e2832006-06-27 02:54:58 -0700660 oldval = futex_atomic_cmpxchg_inatomic(uaddr, uval, 0);
Peter Zijlstraa8663742006-12-06 20:32:20 -0800661 pagefault_enable();
Ingo Molnarc87e2832006-06-27 02:54:58 -0700662
663 if (oldval == -EFAULT)
664 return oldval;
665 if (oldval != uval)
666 return -EAGAIN;
667
668 return 0;
669}
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671/*
Ingo Molnar8b8f3192006-07-03 00:25:05 -0700672 * Express the locking dependencies for lockdep:
673 */
674static inline void
675double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
676{
677 if (hb1 <= hb2) {
678 spin_lock(&hb1->lock);
679 if (hb1 < hb2)
680 spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING);
681 } else { /* hb1 > hb2 */
682 spin_lock(&hb2->lock);
683 spin_lock_nested(&hb1->lock, SINGLE_DEPTH_NESTING);
684 }
685}
686
687/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 * Wake up all waiters hashed on the physical page that is mapped
689 * to this virtual address:
690 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700691static int futex_wake(u32 __user *uaddr, struct rw_semaphore *fshared,
692 int nr_wake)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Ingo Molnare2970f22006-06-27 02:54:47 -0700694 struct futex_hash_bucket *hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 struct futex_q *this, *next;
Pierre Peifferec92d082007-05-09 02:35:00 -0700696 struct plist_head *head;
Ingo Molnare2970f22006-06-27 02:54:47 -0700697 union futex_key key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 int ret;
699
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700700 if (fshared)
701 down_read(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700703 ret = get_futex_key(uaddr, fshared, &key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (unlikely(ret != 0))
705 goto out;
706
Ingo Molnare2970f22006-06-27 02:54:47 -0700707 hb = hash_futex(&key);
708 spin_lock(&hb->lock);
709 head = &hb->chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Pierre Peifferec92d082007-05-09 02:35:00 -0700711 plist_for_each_entry_safe(this, next, head, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (match_futex (&this->key, &key)) {
Ingo Molnared6f7b12006-07-01 04:35:46 -0700713 if (this->pi_state) {
714 ret = -EINVAL;
715 break;
716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 wake_futex(this);
718 if (++ret >= nr_wake)
719 break;
720 }
721 }
722
Ingo Molnare2970f22006-06-27 02:54:47 -0700723 spin_unlock(&hb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724out:
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700725 if (fshared)
726 up_read(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return ret;
728}
729
730/*
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700731 * Called from futex_requeue_pi.
732 * Set FUTEX_WAITERS and FUTEX_WAITER_REQUEUED flags on the
733 * PI-futex value; search its associated pi_state if an owner exist
734 * or create a new one without owner.
735 */
736static inline int
737lookup_pi_state_for_requeue(u32 __user *uaddr, struct futex_hash_bucket *hb,
738 union futex_key *key,
739 struct futex_pi_state **pi_state)
740{
741 u32 curval, uval, newval;
742
743retry:
744 /*
745 * We can't handle a fault cleanly because we can't
746 * release the locks here. Simply return the fault.
747 */
748 if (get_futex_value_locked(&curval, uaddr))
749 return -EFAULT;
750
751 /* set the flags FUTEX_WAITERS and FUTEX_WAITER_REQUEUED */
752 if ((curval & (FUTEX_WAITERS | FUTEX_WAITER_REQUEUED))
753 != (FUTEX_WAITERS | FUTEX_WAITER_REQUEUED)) {
754 /*
755 * No waiters yet, we prepare the futex to have some waiters.
756 */
757
758 uval = curval;
759 newval = uval | FUTEX_WAITERS | FUTEX_WAITER_REQUEUED;
760
761 pagefault_disable();
762 curval = futex_atomic_cmpxchg_inatomic(uaddr, uval, newval);
763 pagefault_enable();
764
765 if (unlikely(curval == -EFAULT))
766 return -EFAULT;
767 if (unlikely(curval != uval))
768 goto retry;
769 }
770
771 if (!(curval & FUTEX_TID_MASK)
772 || lookup_pi_state(curval, hb, key, pi_state)) {
773 /* the futex has no owner (yet) or the lookup failed:
774 allocate one pi_state without owner */
775
776 *pi_state = alloc_pi_state();
777
778 /* Already stores the key: */
779 (*pi_state)->key = *key;
780
781 /* init the mutex without owner */
782 __rt_mutex_init(&(*pi_state)->pi_mutex, NULL);
783 }
784
785 return 0;
786}
787
788/*
789 * Keep the first nr_wake waiter from futex1, wake up one,
790 * and requeue the next nr_requeue waiters following hashed on
791 * one physical page to another physical page (PI-futex uaddr2)
792 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700793static int futex_requeue_pi(u32 __user *uaddr1,
794 struct rw_semaphore *fshared,
795 u32 __user *uaddr2,
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700796 int nr_wake, int nr_requeue, u32 *cmpval)
797{
798 union futex_key key1, key2;
799 struct futex_hash_bucket *hb1, *hb2;
800 struct plist_head *head1;
801 struct futex_q *this, *next;
802 struct futex_pi_state *pi_state2 = NULL;
803 struct rt_mutex_waiter *waiter, *top_waiter = NULL;
804 struct rt_mutex *lock2 = NULL;
805 int ret, drop_count = 0;
806
807 if (refill_pi_state_cache())
808 return -ENOMEM;
809
810retry:
811 /*
812 * First take all the futex related locks:
813 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700814 if (fshared)
815 down_read(fshared);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700816
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700817 ret = get_futex_key(uaddr1, fshared, &key1);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700818 if (unlikely(ret != 0))
819 goto out;
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700820 ret = get_futex_key(uaddr2, fshared, &key2);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700821 if (unlikely(ret != 0))
822 goto out;
823
824 hb1 = hash_futex(&key1);
825 hb2 = hash_futex(&key2);
826
827 double_lock_hb(hb1, hb2);
828
829 if (likely(cmpval != NULL)) {
830 u32 curval;
831
832 ret = get_futex_value_locked(&curval, uaddr1);
833
834 if (unlikely(ret)) {
835 spin_unlock(&hb1->lock);
836 if (hb1 != hb2)
837 spin_unlock(&hb2->lock);
838
839 /*
840 * If we would have faulted, release mmap_sem, fault
841 * it in and start all over again.
842 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700843 if (fshared)
844 up_read(fshared);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700845
846 ret = get_user(curval, uaddr1);
847
848 if (!ret)
849 goto retry;
850
851 return ret;
852 }
853 if (curval != *cmpval) {
854 ret = -EAGAIN;
855 goto out_unlock;
856 }
857 }
858
859 head1 = &hb1->chain;
860 plist_for_each_entry_safe(this, next, head1, list) {
861 if (!match_futex (&this->key, &key1))
862 continue;
863 if (++ret <= nr_wake) {
864 wake_futex(this);
865 } else {
866 /*
867 * FIRST: get and set the pi_state
868 */
869 if (!pi_state2) {
870 int s;
871 /* do this only the first time we requeue someone */
872 s = lookup_pi_state_for_requeue(uaddr2, hb2,
873 &key2, &pi_state2);
874 if (s) {
875 ret = s;
876 goto out_unlock;
877 }
878
879 lock2 = &pi_state2->pi_mutex;
880 spin_lock(&lock2->wait_lock);
881
882 /* Save the top waiter of the wait_list */
883 if (rt_mutex_has_waiters(lock2))
884 top_waiter = rt_mutex_top_waiter(lock2);
885 } else
886 atomic_inc(&pi_state2->refcount);
887
888
889 this->pi_state = pi_state2;
890
891 /*
892 * SECOND: requeue futex_q to the correct hashbucket
893 */
894
895 /*
896 * If key1 and key2 hash to the same bucket, no need to
897 * requeue.
898 */
899 if (likely(head1 != &hb2->chain)) {
900 plist_del(&this->list, &hb1->chain);
901 plist_add(&this->list, &hb2->chain);
902 this->lock_ptr = &hb2->lock;
903#ifdef CONFIG_DEBUG_PI_LIST
904 this->list.plist.lock = &hb2->lock;
905#endif
906 }
907 this->key = key2;
908 get_futex_key_refs(&key2);
909 drop_count++;
910
911
912 /*
913 * THIRD: queue it to lock2
914 */
915 spin_lock_irq(&this->task->pi_lock);
916 waiter = &this->waiter;
917 waiter->task = this->task;
918 waiter->lock = lock2;
919 plist_node_init(&waiter->list_entry, this->task->prio);
920 plist_node_init(&waiter->pi_list_entry, this->task->prio);
921 plist_add(&waiter->list_entry, &lock2->wait_list);
922 this->task->pi_blocked_on = waiter;
923 spin_unlock_irq(&this->task->pi_lock);
924
925 if (ret - nr_wake >= nr_requeue)
926 break;
927 }
928 }
929
930 /* If we've requeued some tasks and the top_waiter of the rt_mutex
931 has changed, we must adjust the priority of the owner, if any */
932 if (drop_count) {
933 struct task_struct *owner = rt_mutex_owner(lock2);
934 if (owner &&
935 (top_waiter != (waiter = rt_mutex_top_waiter(lock2)))) {
936 int chain_walk = 0;
937
938 spin_lock_irq(&owner->pi_lock);
939 if (top_waiter)
940 plist_del(&top_waiter->pi_list_entry, &owner->pi_waiters);
941 else
942 /*
943 * There was no waiters before the requeue,
944 * the flag must be updated
945 */
946 mark_rt_mutex_waiters(lock2);
947
948 plist_add(&waiter->pi_list_entry, &owner->pi_waiters);
949 __rt_mutex_adjust_prio(owner);
950 if (owner->pi_blocked_on) {
951 chain_walk = 1;
952 get_task_struct(owner);
953 }
954
955 spin_unlock_irq(&owner->pi_lock);
956 spin_unlock(&lock2->wait_lock);
957
958 if (chain_walk)
959 rt_mutex_adjust_prio_chain(owner, 0, lock2, NULL,
960 current);
961 } else {
962 /* No owner or the top_waiter does not change */
963 mark_rt_mutex_waiters(lock2);
964 spin_unlock(&lock2->wait_lock);
965 }
966 }
967
968out_unlock:
969 spin_unlock(&hb1->lock);
970 if (hb1 != hb2)
971 spin_unlock(&hb2->lock);
972
973 /* drop_futex_key_refs() must be called outside the spinlocks. */
974 while (--drop_count >= 0)
975 drop_futex_key_refs(&key1);
976
977out:
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700978 if (fshared)
979 up_read(fshared);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700980 return ret;
981}
982
983/*
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700984 * Wake up all waiters hashed on the physical page that is mapped
985 * to this virtual address:
986 */
Ingo Molnare2970f22006-06-27 02:54:47 -0700987static int
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700988futex_wake_op(u32 __user *uaddr1, struct rw_semaphore *fshared,
989 u32 __user *uaddr2,
Ingo Molnare2970f22006-06-27 02:54:47 -0700990 int nr_wake, int nr_wake2, int op)
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700991{
992 union futex_key key1, key2;
Ingo Molnare2970f22006-06-27 02:54:47 -0700993 struct futex_hash_bucket *hb1, *hb2;
Pierre Peifferec92d082007-05-09 02:35:00 -0700994 struct plist_head *head;
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700995 struct futex_q *this, *next;
996 int ret, op_ret, attempt = 0;
997
998retryfull:
Eric Dumazet34f01cc2007-05-09 02:35:04 -0700999 if (fshared)
1000 down_read(fshared);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001001
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001002 ret = get_futex_key(uaddr1, fshared, &key1);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001003 if (unlikely(ret != 0))
1004 goto out;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001005 ret = get_futex_key(uaddr2, fshared, &key2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001006 if (unlikely(ret != 0))
1007 goto out;
1008
Ingo Molnare2970f22006-06-27 02:54:47 -07001009 hb1 = hash_futex(&key1);
1010 hb2 = hash_futex(&key2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001011
1012retry:
Ingo Molnar8b8f3192006-07-03 00:25:05 -07001013 double_lock_hb(hb1, hb2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001014
Ingo Molnare2970f22006-06-27 02:54:47 -07001015 op_ret = futex_atomic_op_inuser(op, uaddr2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001016 if (unlikely(op_ret < 0)) {
Ingo Molnare2970f22006-06-27 02:54:47 -07001017 u32 dummy;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001018
Ingo Molnare2970f22006-06-27 02:54:47 -07001019 spin_unlock(&hb1->lock);
1020 if (hb1 != hb2)
1021 spin_unlock(&hb2->lock);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001022
David Howells7ee1dd32006-01-06 00:11:44 -08001023#ifndef CONFIG_MMU
Ingo Molnare2970f22006-06-27 02:54:47 -07001024 /*
1025 * we don't get EFAULT from MMU faults if we don't have an MMU,
1026 * but we might get them from range checking
1027 */
David Howells7ee1dd32006-01-06 00:11:44 -08001028 ret = op_ret;
1029 goto out;
1030#endif
1031
David Gibson796f8d92005-11-07 00:59:33 -08001032 if (unlikely(op_ret != -EFAULT)) {
1033 ret = op_ret;
1034 goto out;
1035 }
1036
Ingo Molnare2970f22006-06-27 02:54:47 -07001037 /*
1038 * futex_atomic_op_inuser needs to both read and write
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001039 * *(int __user *)uaddr2, but we can't modify it
1040 * non-atomically. Therefore, if get_user below is not
1041 * enough, we need to handle the fault ourselves, while
Ingo Molnare2970f22006-06-27 02:54:47 -07001042 * still holding the mmap_sem.
1043 */
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001044 if (attempt++) {
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001045 ret = futex_handle_fault((unsigned long)uaddr2,
1046 fshared, attempt);
1047 if (ret)
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001048 goto out;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001049 goto retry;
1050 }
1051
Ingo Molnare2970f22006-06-27 02:54:47 -07001052 /*
1053 * If we would have faulted, release mmap_sem,
1054 * fault it in and start all over again.
1055 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001056 if (fshared)
1057 up_read(fshared);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001058
Ingo Molnare2970f22006-06-27 02:54:47 -07001059 ret = get_user(dummy, uaddr2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001060 if (ret)
1061 return ret;
1062
1063 goto retryfull;
1064 }
1065
Ingo Molnare2970f22006-06-27 02:54:47 -07001066 head = &hb1->chain;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001067
Pierre Peifferec92d082007-05-09 02:35:00 -07001068 plist_for_each_entry_safe(this, next, head, list) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001069 if (match_futex (&this->key, &key1)) {
1070 wake_futex(this);
1071 if (++ret >= nr_wake)
1072 break;
1073 }
1074 }
1075
1076 if (op_ret > 0) {
Ingo Molnare2970f22006-06-27 02:54:47 -07001077 head = &hb2->chain;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001078
1079 op_ret = 0;
Pierre Peifferec92d082007-05-09 02:35:00 -07001080 plist_for_each_entry_safe(this, next, head, list) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001081 if (match_futex (&this->key, &key2)) {
1082 wake_futex(this);
1083 if (++op_ret >= nr_wake2)
1084 break;
1085 }
1086 }
1087 ret += op_ret;
1088 }
1089
Ingo Molnare2970f22006-06-27 02:54:47 -07001090 spin_unlock(&hb1->lock);
1091 if (hb1 != hb2)
1092 spin_unlock(&hb2->lock);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001093out:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001094 if (fshared)
1095 up_read(fshared);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001096 return ret;
1097}
1098
1099/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 * Requeue all waiters hashed on one physical page to another
1101 * physical page.
1102 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001103static int futex_requeue(u32 __user *uaddr1, struct rw_semaphore *fshared,
1104 u32 __user *uaddr2,
Ingo Molnare2970f22006-06-27 02:54:47 -07001105 int nr_wake, int nr_requeue, u32 *cmpval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
1107 union futex_key key1, key2;
Ingo Molnare2970f22006-06-27 02:54:47 -07001108 struct futex_hash_bucket *hb1, *hb2;
Pierre Peifferec92d082007-05-09 02:35:00 -07001109 struct plist_head *head1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 struct futex_q *this, *next;
1111 int ret, drop_count = 0;
1112
1113 retry:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001114 if (fshared)
1115 down_read(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001117 ret = get_futex_key(uaddr1, fshared, &key1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if (unlikely(ret != 0))
1119 goto out;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001120 ret = get_futex_key(uaddr2, fshared, &key2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (unlikely(ret != 0))
1122 goto out;
1123
Ingo Molnare2970f22006-06-27 02:54:47 -07001124 hb1 = hash_futex(&key1);
1125 hb2 = hash_futex(&key2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Ingo Molnar8b8f3192006-07-03 00:25:05 -07001127 double_lock_hb(hb1, hb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Ingo Molnare2970f22006-06-27 02:54:47 -07001129 if (likely(cmpval != NULL)) {
1130 u32 curval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Ingo Molnare2970f22006-06-27 02:54:47 -07001132 ret = get_futex_value_locked(&curval, uaddr1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 if (unlikely(ret)) {
Ingo Molnare2970f22006-06-27 02:54:47 -07001135 spin_unlock(&hb1->lock);
1136 if (hb1 != hb2)
1137 spin_unlock(&hb2->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Ingo Molnare2970f22006-06-27 02:54:47 -07001139 /*
1140 * If we would have faulted, release mmap_sem, fault
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 * it in and start all over again.
1142 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001143 if (fshared)
1144 up_read(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Ingo Molnare2970f22006-06-27 02:54:47 -07001146 ret = get_user(curval, uaddr1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 if (!ret)
1149 goto retry;
1150
1151 return ret;
1152 }
Ingo Molnare2970f22006-06-27 02:54:47 -07001153 if (curval != *cmpval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 ret = -EAGAIN;
1155 goto out_unlock;
1156 }
1157 }
1158
Ingo Molnare2970f22006-06-27 02:54:47 -07001159 head1 = &hb1->chain;
Pierre Peifferec92d082007-05-09 02:35:00 -07001160 plist_for_each_entry_safe(this, next, head1, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if (!match_futex (&this->key, &key1))
1162 continue;
1163 if (++ret <= nr_wake) {
1164 wake_futex(this);
1165 } else {
Sebastien Dugue59e0e0a2006-06-27 02:55:03 -07001166 /*
1167 * If key1 and key2 hash to the same bucket, no need to
1168 * requeue.
1169 */
1170 if (likely(head1 != &hb2->chain)) {
Pierre Peifferec92d082007-05-09 02:35:00 -07001171 plist_del(&this->list, &hb1->chain);
1172 plist_add(&this->list, &hb2->chain);
Sebastien Dugue59e0e0a2006-06-27 02:55:03 -07001173 this->lock_ptr = &hb2->lock;
Pierre Peifferec92d082007-05-09 02:35:00 -07001174#ifdef CONFIG_DEBUG_PI_LIST
1175 this->list.plist.lock = &hb2->lock;
1176#endif
1177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 this->key = key2;
Rusty Russell9adef582007-05-08 00:26:42 -07001179 get_futex_key_refs(&key2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 drop_count++;
1181
1182 if (ret - nr_wake >= nr_requeue)
1183 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
1185 }
1186
1187out_unlock:
Ingo Molnare2970f22006-06-27 02:54:47 -07001188 spin_unlock(&hb1->lock);
1189 if (hb1 != hb2)
1190 spin_unlock(&hb2->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Rusty Russell9adef582007-05-08 00:26:42 -07001192 /* drop_futex_key_refs() must be called outside the spinlocks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 while (--drop_count >= 0)
Rusty Russell9adef582007-05-08 00:26:42 -07001194 drop_futex_key_refs(&key1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196out:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001197 if (fshared)
1198 up_read(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 return ret;
1200}
1201
1202/* The key must be already stored in q->key. */
1203static inline struct futex_hash_bucket *
1204queue_lock(struct futex_q *q, int fd, struct file *filp)
1205{
Ingo Molnare2970f22006-06-27 02:54:47 -07001206 struct futex_hash_bucket *hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 q->fd = fd;
1209 q->filp = filp;
1210
1211 init_waitqueue_head(&q->waiters);
1212
Rusty Russell9adef582007-05-08 00:26:42 -07001213 get_futex_key_refs(&q->key);
Ingo Molnare2970f22006-06-27 02:54:47 -07001214 hb = hash_futex(&q->key);
1215 q->lock_ptr = &hb->lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Ingo Molnare2970f22006-06-27 02:54:47 -07001217 spin_lock(&hb->lock);
1218 return hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219}
1220
Ingo Molnare2970f22006-06-27 02:54:47 -07001221static inline void __queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
Pierre Peifferec92d082007-05-09 02:35:00 -07001223 int prio;
1224
1225 /*
1226 * The priority used to register this element is
1227 * - either the real thread-priority for the real-time threads
1228 * (i.e. threads with a priority lower than MAX_RT_PRIO)
1229 * - or MAX_RT_PRIO for non-RT threads.
1230 * Thus, all RT-threads are woken first in priority order, and
1231 * the others are woken last, in FIFO order.
1232 */
1233 prio = min(current->normal_prio, MAX_RT_PRIO);
1234
1235 plist_node_init(&q->list, prio);
1236#ifdef CONFIG_DEBUG_PI_LIST
1237 q->list.plist.lock = &hb->lock;
1238#endif
1239 plist_add(&q->list, &hb->chain);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001240 q->task = current;
Ingo Molnare2970f22006-06-27 02:54:47 -07001241 spin_unlock(&hb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
1243
1244static inline void
Ingo Molnare2970f22006-06-27 02:54:47 -07001245queue_unlock(struct futex_q *q, struct futex_hash_bucket *hb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
Ingo Molnare2970f22006-06-27 02:54:47 -07001247 spin_unlock(&hb->lock);
Rusty Russell9adef582007-05-08 00:26:42 -07001248 drop_futex_key_refs(&q->key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
1251/*
1252 * queue_me and unqueue_me must be called as a pair, each
1253 * exactly once. They are called with the hashed spinlock held.
1254 */
1255
1256/* The key must be already stored in q->key. */
1257static void queue_me(struct futex_q *q, int fd, struct file *filp)
1258{
Ingo Molnare2970f22006-06-27 02:54:47 -07001259 struct futex_hash_bucket *hb;
1260
1261 hb = queue_lock(q, fd, filp);
1262 __queue_me(q, hb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263}
1264
1265/* Return 1 if we were still queued (ie. 0 means we were woken) */
1266static int unqueue_me(struct futex_q *q)
1267{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 spinlock_t *lock_ptr;
Ingo Molnare2970f22006-06-27 02:54:47 -07001269 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 /* In the common case we don't take the spinlock, which is nice. */
1272 retry:
1273 lock_ptr = q->lock_ptr;
Christian Borntraegere91467e2006-08-05 12:13:52 -07001274 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 if (lock_ptr != 0) {
1276 spin_lock(lock_ptr);
1277 /*
1278 * q->lock_ptr can change between reading it and
1279 * spin_lock(), causing us to take the wrong lock. This
1280 * corrects the race condition.
1281 *
1282 * Reasoning goes like this: if we have the wrong lock,
1283 * q->lock_ptr must have changed (maybe several times)
1284 * between reading it and the spin_lock(). It can
1285 * change again after the spin_lock() but only if it was
1286 * already changed before the spin_lock(). It cannot,
1287 * however, change back to the original value. Therefore
1288 * we can detect whether we acquired the correct lock.
1289 */
1290 if (unlikely(lock_ptr != q->lock_ptr)) {
1291 spin_unlock(lock_ptr);
1292 goto retry;
1293 }
Pierre Peifferec92d082007-05-09 02:35:00 -07001294 WARN_ON(plist_node_empty(&q->list));
1295 plist_del(&q->list, &q->list.plist);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001296
1297 BUG_ON(q->pi_state);
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 spin_unlock(lock_ptr);
1300 ret = 1;
1301 }
1302
Rusty Russell9adef582007-05-08 00:26:42 -07001303 drop_futex_key_refs(&q->key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return ret;
1305}
1306
Ingo Molnarc87e2832006-06-27 02:54:58 -07001307/*
1308 * PI futexes can not be requeued and must remove themself from the
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001309 * hash bucket. The hash bucket lock (i.e. lock_ptr) is held on entry
1310 * and dropped here.
Ingo Molnarc87e2832006-06-27 02:54:58 -07001311 */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001312static void unqueue_me_pi(struct futex_q *q)
Ingo Molnarc87e2832006-06-27 02:54:58 -07001313{
Pierre Peifferec92d082007-05-09 02:35:00 -07001314 WARN_ON(plist_node_empty(&q->list));
1315 plist_del(&q->list, &q->list.plist);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001316
1317 BUG_ON(!q->pi_state);
1318 free_pi_state(q->pi_state);
1319 q->pi_state = NULL;
1320
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001321 spin_unlock(q->lock_ptr);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001322
Rusty Russell9adef582007-05-08 00:26:42 -07001323 drop_futex_key_refs(&q->key);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001324}
1325
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001326/*
1327 * Fixup the pi_state owner with current.
1328 *
1329 * The cur->mm semaphore must be held, it is released at return of this
1330 * function.
1331 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001332static int fixup_pi_state_owner(u32 __user *uaddr, struct rw_semaphore *fshared,
1333 struct futex_q *q,
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001334 struct futex_hash_bucket *hb,
1335 struct task_struct *curr)
1336{
1337 u32 newtid = curr->pid | FUTEX_WAITERS;
1338 struct futex_pi_state *pi_state = q->pi_state;
1339 u32 uval, curval, newval;
1340 int ret;
1341
1342 /* Owner died? */
1343 if (pi_state->owner != NULL) {
1344 spin_lock_irq(&pi_state->owner->pi_lock);
1345 WARN_ON(list_empty(&pi_state->list));
1346 list_del_init(&pi_state->list);
1347 spin_unlock_irq(&pi_state->owner->pi_lock);
1348 } else
1349 newtid |= FUTEX_OWNER_DIED;
1350
1351 pi_state->owner = curr;
1352
1353 spin_lock_irq(&curr->pi_lock);
1354 WARN_ON(!list_empty(&pi_state->list));
1355 list_add(&pi_state->list, &curr->pi_state_list);
1356 spin_unlock_irq(&curr->pi_lock);
1357
1358 /* Unqueue and drop the lock */
1359 unqueue_me_pi(q);
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001360 if (fshared)
1361 up_read(fshared);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001362 /*
1363 * We own it, so we have to replace the pending owner
1364 * TID. This must be atomic as we have preserve the
1365 * owner died bit here.
1366 */
1367 ret = get_user(uval, uaddr);
1368 while (!ret) {
1369 newval = (uval & FUTEX_OWNER_DIED) | newtid;
1370 newval |= (uval & FUTEX_WAITER_REQUEUED);
1371 curval = futex_atomic_cmpxchg_inatomic(uaddr,
1372 uval, newval);
1373 if (curval == -EFAULT)
1374 ret = -EFAULT;
1375 if (curval == uval)
1376 break;
1377 uval = curval;
1378 }
1379 return ret;
1380}
1381
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001382/*
1383 * In case we must use restart_block to restart a futex_wait,
1384 * we encode in the 'arg3' shared capability
1385 */
1386#define ARG3_SHARED 1
1387
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001388static long futex_wait_restart(struct restart_block *restart);
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001389static int futex_wait(u32 __user *uaddr, struct rw_semaphore *fshared,
1390 u32 val, ktime_t *abs_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
Ingo Molnarc87e2832006-06-27 02:54:58 -07001392 struct task_struct *curr = current;
1393 DECLARE_WAITQUEUE(wait, curr);
Ingo Molnare2970f22006-06-27 02:54:47 -07001394 struct futex_hash_bucket *hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 struct futex_q q;
Ingo Molnare2970f22006-06-27 02:54:47 -07001396 u32 uval;
1397 int ret;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001398 struct hrtimer_sleeper t, *to = NULL;
Pierre Peifferc19384b2007-05-09 02:35:02 -07001399 int rem = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Ingo Molnarc87e2832006-06-27 02:54:58 -07001401 q.pi_state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 retry:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001403 if (fshared)
1404 down_read(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001406 ret = get_futex_key(uaddr, fshared, &q.key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 if (unlikely(ret != 0))
1408 goto out_release_sem;
1409
Ingo Molnare2970f22006-06-27 02:54:47 -07001410 hb = queue_lock(&q, -1, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 /*
1413 * Access the page AFTER the futex is queued.
1414 * Order is important:
1415 *
1416 * Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val);
1417 * Userspace waker: if (cond(var)) { var = new; futex_wake(&var); }
1418 *
1419 * The basic logical guarantee of a futex is that it blocks ONLY
1420 * if cond(var) is known to be true at the time of blocking, for
1421 * any cond. If we queued after testing *uaddr, that would open
1422 * a race condition where we could block indefinitely with
1423 * cond(var) false, which would violate the guarantee.
1424 *
1425 * A consequence is that futex_wait() can return zero and absorb
1426 * a wakeup when *uaddr != val on entry to the syscall. This is
1427 * rare, but normal.
1428 *
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001429 * for shared futexes, we hold the mmap semaphore, so the mapping
1430 * cannot have changed since we looked it up in get_futex_key.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 */
Ingo Molnare2970f22006-06-27 02:54:47 -07001432 ret = get_futex_value_locked(&uval, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 if (unlikely(ret)) {
Ingo Molnare2970f22006-06-27 02:54:47 -07001435 queue_unlock(&q, hb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Ingo Molnare2970f22006-06-27 02:54:47 -07001437 /*
1438 * If we would have faulted, release mmap_sem, fault it in and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 * start all over again.
1440 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001441 if (fshared)
1442 up_read(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Ingo Molnare2970f22006-06-27 02:54:47 -07001444 ret = get_user(uval, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 if (!ret)
1447 goto retry;
1448 return ret;
1449 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001450 ret = -EWOULDBLOCK;
1451 if (uval != val)
1452 goto out_unlock_release_sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001454 /*
1455 * This rt_mutex_waiter structure is prepared here and will
1456 * be used only if this task is requeued from a normal futex to
1457 * a PI-futex with futex_requeue_pi.
1458 */
1459 debug_rt_mutex_init_waiter(&q.waiter);
1460 q.waiter.task = NULL;
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 /* Only actually queue if *uaddr contained val. */
Ingo Molnare2970f22006-06-27 02:54:47 -07001463 __queue_me(&q, hb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 /*
1466 * Now the futex is queued and we have checked the data, we
1467 * don't want to hold mmap_sem while we sleep.
Ingo Molnarc87e2832006-06-27 02:54:58 -07001468 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001469 if (fshared)
1470 up_read(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 /*
1473 * There might have been scheduling since the queue_me(), as we
1474 * cannot hold a spinlock across the get_user() in case it
1475 * faults, and we cannot just set TASK_INTERRUPTIBLE state when
1476 * queueing ourselves into the futex hash. This code thus has to
1477 * rely on the futex_wake() code removing us from hash when it
1478 * wakes us up.
1479 */
1480
1481 /* add_wait_queue is the barrier after __set_current_state. */
1482 __set_current_state(TASK_INTERRUPTIBLE);
1483 add_wait_queue(&q.waiters, &wait);
1484 /*
Pierre Peifferec92d082007-05-09 02:35:00 -07001485 * !plist_node_empty() is safe here without any lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 * q.lock_ptr != 0 is not safe, because of ordering against wakeup.
1487 */
Pierre Peifferec92d082007-05-09 02:35:00 -07001488 if (likely(!plist_node_empty(&q.list))) {
Pierre Peifferc19384b2007-05-09 02:35:02 -07001489 if (!abs_time)
1490 schedule();
1491 else {
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001492 to = &t;
Pierre Peifferc19384b2007-05-09 02:35:02 -07001493 hrtimer_init(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1494 hrtimer_init_sleeper(&t, current);
1495 t.timer.expires = *abs_time;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001496
Pierre Peifferc19384b2007-05-09 02:35:02 -07001497 hrtimer_start(&t.timer, t.timer.expires, HRTIMER_MODE_ABS);
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001498
Pierre Peifferc19384b2007-05-09 02:35:02 -07001499 /*
1500 * the timer could have already expired, in which
1501 * case current would be flagged for rescheduling.
1502 * Don't bother calling schedule.
1503 */
1504 if (likely(t.task))
1505 schedule();
1506
1507 hrtimer_cancel(&t.timer);
1508
1509 /* Flag if a timeout occured */
1510 rem = (t.task == NULL);
1511 }
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 __set_current_state(TASK_RUNNING);
1514
1515 /*
1516 * NOTE: we don't remove ourselves from the waitqueue because
1517 * we are the only user of it.
1518 */
1519
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001520 if (q.pi_state) {
1521 /*
1522 * We were woken but have been requeued on a PI-futex.
1523 * We have to complete the lock acquisition by taking
1524 * the rtmutex.
1525 */
1526
1527 struct rt_mutex *lock = &q.pi_state->pi_mutex;
1528
1529 spin_lock(&lock->wait_lock);
1530 if (unlikely(q.waiter.task)) {
1531 remove_waiter(lock, &q.waiter);
1532 }
1533 spin_unlock(&lock->wait_lock);
1534
1535 if (rem)
1536 ret = -ETIMEDOUT;
1537 else
1538 ret = rt_mutex_timed_lock(lock, to, 1);
1539
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001540 if (fshared)
1541 down_read(fshared);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001542 spin_lock(q.lock_ptr);
1543
1544 /*
1545 * Got the lock. We might not be the anticipated owner if we
1546 * did a lock-steal - fix up the PI-state in that case.
1547 */
1548 if (!ret && q.pi_state->owner != curr) {
1549 /*
1550 * We MUST play with the futex we were requeued on,
1551 * NOT the current futex.
1552 * We can retrieve it from the key of the pi_state
1553 */
1554 uaddr = q.pi_state->key.uaddr;
1555
1556 /* mmap_sem and hash_bucket lock are unlocked at
1557 return of this function */
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001558 ret = fixup_pi_state_owner(uaddr, fshared,
1559 &q, hb, curr);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001560 } else {
1561 /*
1562 * Catch the rare case, where the lock was released
1563 * when we were on the way back before we locked
1564 * the hash bucket.
1565 */
1566 if (ret && q.pi_state->owner == curr) {
1567 if (rt_mutex_trylock(&q.pi_state->pi_mutex))
1568 ret = 0;
1569 }
1570 /* Unqueue and drop the lock */
1571 unqueue_me_pi(&q);
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001572 if (fshared)
1573 up_read(fshared);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001574 }
1575
1576 debug_rt_mutex_free_waiter(&q.waiter);
1577
1578 return ret;
1579 }
1580
1581 debug_rt_mutex_free_waiter(&q.waiter);
1582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 /* If we were woken (and unqueued), we succeeded, whatever. */
1584 if (!unqueue_me(&q))
1585 return 0;
Pierre Peifferc19384b2007-05-09 02:35:02 -07001586 if (rem)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 return -ETIMEDOUT;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001588
Ingo Molnare2970f22006-06-27 02:54:47 -07001589 /*
1590 * We expect signal_pending(current), but another thread may
1591 * have handled it for us already.
1592 */
Pierre Peifferc19384b2007-05-09 02:35:02 -07001593 if (!abs_time)
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001594 return -ERESTARTSYS;
1595 else {
1596 struct restart_block *restart;
1597 restart = &current_thread_info()->restart_block;
1598 restart->fn = futex_wait_restart;
1599 restart->arg0 = (unsigned long)uaddr;
1600 restart->arg1 = (unsigned long)val;
Pierre Peifferc19384b2007-05-09 02:35:02 -07001601 restart->arg2 = (unsigned long)abs_time;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001602 restart->arg3 = 0;
1603 if (fshared)
1604 restart->arg3 |= ARG3_SHARED;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001605 return -ERESTART_RESTARTBLOCK;
1606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Ingo Molnarc87e2832006-06-27 02:54:58 -07001608 out_unlock_release_sem:
1609 queue_unlock(&q, hb);
1610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 out_release_sem:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001612 if (fshared)
1613 up_read(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001614 return ret;
1615}
1616
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001617
1618static long futex_wait_restart(struct restart_block *restart)
1619{
1620 u32 __user *uaddr = (u32 __user *)restart->arg0;
1621 u32 val = (u32)restart->arg1;
Pierre Peifferc19384b2007-05-09 02:35:02 -07001622 ktime_t *abs_time = (ktime_t *)restart->arg2;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001623 struct rw_semaphore *fshared = NULL;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001624
1625 restart->fn = do_no_restart_syscall;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001626 if (restart->arg3 & ARG3_SHARED)
1627 fshared = &current->mm->mmap_sem;
1628 return (long)futex_wait(uaddr, fshared, val, abs_time);
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001629}
1630
1631
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001632static void set_pi_futex_owner(struct futex_hash_bucket *hb,
1633 union futex_key *key, struct task_struct *p)
1634{
1635 struct plist_head *head;
1636 struct futex_q *this, *next;
1637 struct futex_pi_state *pi_state = NULL;
1638 struct rt_mutex *lock;
1639
1640 /* Search a waiter that should already exists */
1641
1642 head = &hb->chain;
1643
1644 plist_for_each_entry_safe(this, next, head, list) {
1645 if (match_futex (&this->key, key)) {
1646 pi_state = this->pi_state;
1647 break;
1648 }
1649 }
1650
1651 BUG_ON(!pi_state);
1652
1653 /* set p as pi_state's owner */
1654 lock = &pi_state->pi_mutex;
1655
1656 spin_lock(&lock->wait_lock);
1657 spin_lock_irq(&p->pi_lock);
1658
1659 list_add(&pi_state->list, &p->pi_state_list);
1660 pi_state->owner = p;
1661
1662
1663 /* set p as pi_mutex's owner */
1664 debug_rt_mutex_proxy_lock(lock, p);
1665 WARN_ON(rt_mutex_owner(lock));
1666 rt_mutex_set_owner(lock, p, 0);
1667 rt_mutex_deadlock_account_lock(lock, p);
1668
1669 plist_add(&rt_mutex_top_waiter(lock)->pi_list_entry,
1670 &p->pi_waiters);
1671 __rt_mutex_adjust_prio(p);
1672
1673 spin_unlock_irq(&p->pi_lock);
1674 spin_unlock(&lock->wait_lock);
1675}
1676
Ingo Molnarc87e2832006-06-27 02:54:58 -07001677/*
1678 * Userspace tried a 0 -> TID atomic transition of the futex value
1679 * and failed. The kernel side here does the whole locking operation:
1680 * if there are waiters then it will block, it does PI, etc. (Due to
1681 * races the kernel might see a 0 value of the futex too.)
1682 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001683static int futex_lock_pi(u32 __user *uaddr, struct rw_semaphore *fshared,
1684 int detect, ktime_t *time, int trylock)
Ingo Molnarc87e2832006-06-27 02:54:58 -07001685{
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07001686 struct hrtimer_sleeper timeout, *to = NULL;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001687 struct task_struct *curr = current;
1688 struct futex_hash_bucket *hb;
1689 u32 uval, newval, curval;
1690 struct futex_q q;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001691 int ret, lock_held, attempt = 0;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001692
1693 if (refill_pi_state_cache())
1694 return -ENOMEM;
1695
Pierre Peifferc19384b2007-05-09 02:35:02 -07001696 if (time) {
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07001697 to = &timeout;
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001698 hrtimer_init(&to->timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07001699 hrtimer_init_sleeper(to, current);
Pierre Peifferc19384b2007-05-09 02:35:02 -07001700 to->timer.expires = *time;
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07001701 }
1702
Ingo Molnarc87e2832006-06-27 02:54:58 -07001703 q.pi_state = NULL;
1704 retry:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001705 if (fshared)
1706 down_read(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001707
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001708 ret = get_futex_key(uaddr, fshared, &q.key);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001709 if (unlikely(ret != 0))
1710 goto out_release_sem;
1711
1712 hb = queue_lock(&q, -1, NULL);
1713
1714 retry_locked:
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001715 lock_held = 0;
1716
Ingo Molnarc87e2832006-06-27 02:54:58 -07001717 /*
1718 * To avoid races, we attempt to take the lock here again
1719 * (by doing a 0 -> TID atomic cmpxchg), while holding all
1720 * the locks. It will most likely not succeed.
1721 */
1722 newval = current->pid;
1723
Peter Zijlstraa8663742006-12-06 20:32:20 -08001724 pagefault_disable();
Ingo Molnarc87e2832006-06-27 02:54:58 -07001725 curval = futex_atomic_cmpxchg_inatomic(uaddr, 0, newval);
Peter Zijlstraa8663742006-12-06 20:32:20 -08001726 pagefault_enable();
Ingo Molnarc87e2832006-06-27 02:54:58 -07001727
1728 if (unlikely(curval == -EFAULT))
1729 goto uaddr_faulted;
1730
1731 /* We own the lock already */
1732 if (unlikely((curval & FUTEX_TID_MASK) == current->pid)) {
1733 if (!detect && 0)
1734 force_sig(SIGKILL, current);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001735 /*
1736 * Normally, this check is done in user space.
1737 * In case of requeue, the owner may attempt to lock this futex,
1738 * even if the ownership has already been given by the previous
1739 * waker.
1740 * In the usual case, this is a case of deadlock, but not in case
1741 * of REQUEUE_PI.
1742 */
1743 if (!(curval & FUTEX_WAITER_REQUEUED))
1744 ret = -EDEADLK;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001745 goto out_unlock_release_sem;
1746 }
1747
1748 /*
1749 * Surprise - we got the lock. Just return
1750 * to userspace:
1751 */
1752 if (unlikely(!curval))
1753 goto out_unlock_release_sem;
1754
1755 uval = curval;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001756 /*
1757 * In case of a requeue, check if there already is an owner
1758 * If not, just take the futex.
1759 */
1760 if ((curval & FUTEX_WAITER_REQUEUED) && !(curval & FUTEX_TID_MASK)) {
1761 /* set current as futex owner */
1762 newval = curval | current->pid;
1763 lock_held = 1;
1764 } else
1765 /* Set the WAITERS flag, so the owner will know it has someone
1766 to wake at next unlock */
1767 newval = curval | FUTEX_WAITERS;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001768
Peter Zijlstraa8663742006-12-06 20:32:20 -08001769 pagefault_disable();
Ingo Molnarc87e2832006-06-27 02:54:58 -07001770 curval = futex_atomic_cmpxchg_inatomic(uaddr, uval, newval);
Peter Zijlstraa8663742006-12-06 20:32:20 -08001771 pagefault_enable();
Ingo Molnarc87e2832006-06-27 02:54:58 -07001772
1773 if (unlikely(curval == -EFAULT))
1774 goto uaddr_faulted;
1775 if (unlikely(curval != uval))
1776 goto retry_locked;
1777
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001778 if (lock_held) {
1779 set_pi_futex_owner(hb, &q.key, curr);
1780 goto out_unlock_release_sem;
1781 }
1782
Ingo Molnarc87e2832006-06-27 02:54:58 -07001783 /*
1784 * We dont have the lock. Look up the PI state (or create it if
1785 * we are the first waiter):
1786 */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001787 ret = lookup_pi_state(uval, hb, &q.key, &q.pi_state);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001788
1789 if (unlikely(ret)) {
1790 /*
1791 * There were no waiters and the owner task lookup
1792 * failed. When the OWNER_DIED bit is set, then we
1793 * know that this is a robust futex and we actually
1794 * take the lock. This is safe as we are protected by
1795 * the hash bucket lock. We also set the waiters bit
1796 * unconditionally here, to simplify glibc handling of
1797 * multiple tasks racing to acquire the lock and
1798 * cleanup the problems which were left by the dead
1799 * owner.
1800 */
1801 if (curval & FUTEX_OWNER_DIED) {
1802 uval = newval;
1803 newval = current->pid |
1804 FUTEX_OWNER_DIED | FUTEX_WAITERS;
1805
Peter Zijlstraa8663742006-12-06 20:32:20 -08001806 pagefault_disable();
Ingo Molnarc87e2832006-06-27 02:54:58 -07001807 curval = futex_atomic_cmpxchg_inatomic(uaddr,
1808 uval, newval);
Peter Zijlstraa8663742006-12-06 20:32:20 -08001809 pagefault_enable();
Ingo Molnarc87e2832006-06-27 02:54:58 -07001810
1811 if (unlikely(curval == -EFAULT))
1812 goto uaddr_faulted;
1813 if (unlikely(curval != uval))
1814 goto retry_locked;
1815 ret = 0;
1816 }
1817 goto out_unlock_release_sem;
1818 }
1819
1820 /*
1821 * Only actually queue now that the atomic ops are done:
1822 */
1823 __queue_me(&q, hb);
1824
1825 /*
1826 * Now the futex is queued and we have checked the data, we
1827 * don't want to hold mmap_sem while we sleep.
1828 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001829 if (fshared)
1830 up_read(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001831
1832 WARN_ON(!q.pi_state);
1833 /*
1834 * Block on the PI mutex:
1835 */
1836 if (!trylock)
1837 ret = rt_mutex_timed_lock(&q.pi_state->pi_mutex, to, 1);
1838 else {
1839 ret = rt_mutex_trylock(&q.pi_state->pi_mutex);
1840 /* Fixup the trylock return value: */
1841 ret = ret ? 0 : -EWOULDBLOCK;
1842 }
1843
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001844 if (fshared)
1845 down_read(fshared);
Vernon Mauerya99e4e42006-07-01 04:35:42 -07001846 spin_lock(q.lock_ptr);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001847
1848 /*
1849 * Got the lock. We might not be the anticipated owner if we
1850 * did a lock-steal - fix up the PI-state in that case.
1851 */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001852 if (!ret && q.pi_state->owner != curr)
1853 /* mmap_sem is unlocked at return of this function */
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001854 ret = fixup_pi_state_owner(uaddr, fshared, &q, hb, curr);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001855 else {
Ingo Molnarc87e2832006-06-27 02:54:58 -07001856 /*
1857 * Catch the rare case, where the lock was released
1858 * when we were on the way back before we locked
1859 * the hash bucket.
1860 */
1861 if (ret && q.pi_state->owner == curr) {
1862 if (rt_mutex_trylock(&q.pi_state->pi_mutex))
1863 ret = 0;
1864 }
1865 /* Unqueue and drop the lock */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001866 unqueue_me_pi(&q);
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001867 if (fshared)
1868 up_read(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001869 }
1870
1871 if (!detect && ret == -EDEADLK && 0)
1872 force_sig(SIGKILL, current);
1873
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07001874 return ret != -EINTR ? ret : -ERESTARTNOINTR;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001875
1876 out_unlock_release_sem:
1877 queue_unlock(&q, hb);
1878
1879 out_release_sem:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001880 if (fshared)
1881 up_read(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001882 return ret;
1883
1884 uaddr_faulted:
1885 /*
1886 * We have to r/w *(int __user *)uaddr, but we can't modify it
1887 * non-atomically. Therefore, if get_user below is not
1888 * enough, we need to handle the fault ourselves, while
1889 * still holding the mmap_sem.
1890 */
1891 if (attempt++) {
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001892 ret = futex_handle_fault((unsigned long)uaddr, fshared,
1893 attempt);
1894 if (ret)
Ingo Molnarc87e2832006-06-27 02:54:58 -07001895 goto out_unlock_release_sem;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001896 goto retry_locked;
1897 }
1898
1899 queue_unlock(&q, hb);
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001900 if (fshared)
1901 up_read(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001902
1903 ret = get_user(uval, uaddr);
1904 if (!ret && (uval != -EFAULT))
1905 goto retry;
1906
1907 return ret;
1908}
1909
1910/*
Ingo Molnarc87e2832006-06-27 02:54:58 -07001911 * Userspace attempted a TID -> 0 atomic transition, and failed.
1912 * This is the in-kernel slowpath: we look up the PI state (if any),
1913 * and do the rt-mutex unlock.
1914 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001915static int futex_unlock_pi(u32 __user *uaddr, struct rw_semaphore *fshared)
Ingo Molnarc87e2832006-06-27 02:54:58 -07001916{
1917 struct futex_hash_bucket *hb;
1918 struct futex_q *this, *next;
1919 u32 uval;
Pierre Peifferec92d082007-05-09 02:35:00 -07001920 struct plist_head *head;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001921 union futex_key key;
1922 int ret, attempt = 0;
1923
1924retry:
1925 if (get_user(uval, uaddr))
1926 return -EFAULT;
1927 /*
1928 * We release only a lock we actually own:
1929 */
1930 if ((uval & FUTEX_TID_MASK) != current->pid)
1931 return -EPERM;
1932 /*
1933 * First take all the futex related locks:
1934 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001935 if (fshared)
1936 down_read(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001937
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001938 ret = get_futex_key(uaddr, fshared, &key);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001939 if (unlikely(ret != 0))
1940 goto out;
1941
1942 hb = hash_futex(&key);
1943 spin_lock(&hb->lock);
1944
1945retry_locked:
1946 /*
1947 * To avoid races, try to do the TID -> 0 atomic transition
1948 * again. If it succeeds then we can return without waking
1949 * anyone else up:
1950 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001951 if (!(uval & FUTEX_OWNER_DIED)) {
Peter Zijlstraa8663742006-12-06 20:32:20 -08001952 pagefault_disable();
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001953 uval = futex_atomic_cmpxchg_inatomic(uaddr, current->pid, 0);
Peter Zijlstraa8663742006-12-06 20:32:20 -08001954 pagefault_enable();
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001955 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001956
1957 if (unlikely(uval == -EFAULT))
1958 goto pi_faulted;
1959 /*
1960 * Rare case: we managed to release the lock atomically,
1961 * no need to wake anyone else up:
1962 */
1963 if (unlikely(uval == current->pid))
1964 goto out_unlock;
1965
1966 /*
1967 * Ok, other tasks may need to be woken up - check waiters
1968 * and do the wakeup if necessary:
1969 */
1970 head = &hb->chain;
1971
Pierre Peifferec92d082007-05-09 02:35:00 -07001972 plist_for_each_entry_safe(this, next, head, list) {
Ingo Molnarc87e2832006-06-27 02:54:58 -07001973 if (!match_futex (&this->key, &key))
1974 continue;
1975 ret = wake_futex_pi(uaddr, uval, this);
1976 /*
1977 * The atomic access to the futex value
1978 * generated a pagefault, so retry the
1979 * user-access and the wakeup:
1980 */
1981 if (ret == -EFAULT)
1982 goto pi_faulted;
1983 goto out_unlock;
1984 }
1985 /*
1986 * No waiters - kernel unlocks the futex:
1987 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001988 if (!(uval & FUTEX_OWNER_DIED)) {
1989 ret = unlock_futex_pi(uaddr, uval);
1990 if (ret == -EFAULT)
1991 goto pi_faulted;
1992 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001993
1994out_unlock:
1995 spin_unlock(&hb->lock);
1996out:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07001997 if (fshared)
1998 up_read(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001999
2000 return ret;
2001
2002pi_faulted:
2003 /*
2004 * We have to r/w *(int __user *)uaddr, but we can't modify it
2005 * non-atomically. Therefore, if get_user below is not
2006 * enough, we need to handle the fault ourselves, while
2007 * still holding the mmap_sem.
2008 */
2009 if (attempt++) {
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002010 ret = futex_handle_fault((unsigned long)uaddr, fshared,
2011 attempt);
2012 if (ret)
Ingo Molnarc87e2832006-06-27 02:54:58 -07002013 goto out_unlock;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002014 goto retry_locked;
2015 }
2016
2017 spin_unlock(&hb->lock);
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002018 if (fshared)
2019 up_read(fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002020
2021 ret = get_user(uval, uaddr);
2022 if (!ret && (uval != -EFAULT))
2023 goto retry;
2024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 return ret;
2026}
2027
2028static int futex_close(struct inode *inode, struct file *filp)
2029{
2030 struct futex_q *q = filp->private_data;
2031
2032 unqueue_me(q);
2033 kfree(q);
Ingo Molnare2970f22006-06-27 02:54:47 -07002034
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 return 0;
2036}
2037
2038/* This is one-shot: once it's gone off you need a new fd */
2039static unsigned int futex_poll(struct file *filp,
2040 struct poll_table_struct *wait)
2041{
2042 struct futex_q *q = filp->private_data;
2043 int ret = 0;
2044
2045 poll_wait(filp, &q->waiters, wait);
2046
2047 /*
Pierre Peifferec92d082007-05-09 02:35:00 -07002048 * plist_node_empty() is safe here without any lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 * q->lock_ptr != 0 is not safe, because of ordering against wakeup.
2050 */
Pierre Peifferec92d082007-05-09 02:35:00 -07002051 if (plist_node_empty(&q->list))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 ret = POLLIN | POLLRDNORM;
2053
2054 return ret;
2055}
2056
Helge Deller15ad7cd2006-12-06 20:40:36 -08002057static const struct file_operations futex_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 .release = futex_close,
2059 .poll = futex_poll,
2060};
2061
2062/*
2063 * Signal allows caller to avoid the race which would occur if they
2064 * set the sigio stuff up afterwards.
2065 */
Ingo Molnare2970f22006-06-27 02:54:47 -07002066static int futex_fd(u32 __user *uaddr, int signal)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
2068 struct futex_q *q;
2069 struct file *filp;
2070 int ret, err;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002071 struct rw_semaphore *fshared;
Andrew Morton19c6b6e2006-11-02 22:07:17 -08002072 static unsigned long printk_interval;
2073
2074 if (printk_timed_ratelimit(&printk_interval, 60 * 60 * 1000)) {
2075 printk(KERN_WARNING "Process `%s' used FUTEX_FD, which "
2076 "will be removed from the kernel in June 2007\n",
2077 current->comm);
2078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
2080 ret = -EINVAL;
Jesper Juhl7ed20e12005-05-01 08:59:14 -07002081 if (!valid_signal(signal))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 goto out;
2083
2084 ret = get_unused_fd();
2085 if (ret < 0)
2086 goto out;
2087 filp = get_empty_filp();
2088 if (!filp) {
2089 put_unused_fd(ret);
2090 ret = -ENFILE;
2091 goto out;
2092 }
2093 filp->f_op = &futex_fops;
Josef "Jeff" Sipekf3a43f32006-12-08 02:36:43 -08002094 filp->f_path.mnt = mntget(futex_mnt);
2095 filp->f_path.dentry = dget(futex_mnt->mnt_root);
2096 filp->f_mapping = filp->f_path.dentry->d_inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
2098 if (signal) {
Eric W. Biederman609d7fa2006-10-02 02:17:15 -07002099 err = __f_setown(filp, task_pid(current), PIDTYPE_PID, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 if (err < 0) {
Pekka Enberg39ed3fd2005-09-06 15:17:44 -07002101 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 }
2103 filp->f_owner.signum = signal;
2104 }
2105
2106 q = kmalloc(sizeof(*q), GFP_KERNEL);
2107 if (!q) {
Pekka Enberg39ed3fd2005-09-06 15:17:44 -07002108 err = -ENOMEM;
2109 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07002111 q->pi_state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002113 fshared = &current->mm->mmap_sem;
2114 down_read(fshared);
2115 err = get_futex_key(uaddr, fshared, &q->key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
2117 if (unlikely(err != 0)) {
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002118 up_read(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 kfree(q);
Pekka Enberg39ed3fd2005-09-06 15:17:44 -07002120 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 }
2122
2123 /*
2124 * queue_me() must be called before releasing mmap_sem, because
2125 * key->shared.inode needs to be referenced while holding it.
2126 */
2127 filp->private_data = q;
2128
2129 queue_me(q, ret, filp);
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002130 up_read(fshared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
2132 /* Now we map fd to filp, so userspace can access it */
2133 fd_install(ret, filp);
2134out:
2135 return ret;
Pekka Enberg39ed3fd2005-09-06 15:17:44 -07002136error:
2137 put_unused_fd(ret);
2138 put_filp(filp);
2139 ret = err;
2140 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141}
2142
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002143/*
2144 * Support for robust futexes: the kernel cleans up held futexes at
2145 * thread exit time.
2146 *
2147 * Implementation: user-space maintains a per-thread list of locks it
2148 * is holding. Upon do_exit(), the kernel carefully walks this list,
2149 * and marks all locks that are owned by this thread with the
Ingo Molnarc87e2832006-06-27 02:54:58 -07002150 * FUTEX_OWNER_DIED bit, and wakes up a waiter (if any). The list is
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002151 * always manipulated with the lock held, so the list is private and
2152 * per-thread. Userspace also maintains a per-thread 'list_op_pending'
2153 * field, to allow the kernel to clean up if the thread dies after
2154 * acquiring the lock, but just before it could have added itself to
2155 * the list. There can only be one such pending lock.
2156 */
2157
2158/**
2159 * sys_set_robust_list - set the robust-futex list head of a task
2160 * @head: pointer to the list-head
2161 * @len: length of the list-head, as userspace expects
2162 */
2163asmlinkage long
2164sys_set_robust_list(struct robust_list_head __user *head,
2165 size_t len)
2166{
2167 /*
2168 * The kernel knows only one size for now:
2169 */
2170 if (unlikely(len != sizeof(*head)))
2171 return -EINVAL;
2172
2173 current->robust_list = head;
2174
2175 return 0;
2176}
2177
2178/**
2179 * sys_get_robust_list - get the robust-futex list head of a task
2180 * @pid: pid of the process [zero for current task]
2181 * @head_ptr: pointer to a list-head pointer, the kernel fills it in
2182 * @len_ptr: pointer to a length field, the kernel fills in the header size
2183 */
2184asmlinkage long
Al Viroba46df92006-10-10 22:46:07 +01002185sys_get_robust_list(int pid, struct robust_list_head __user * __user *head_ptr,
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002186 size_t __user *len_ptr)
2187{
Al Viroba46df92006-10-10 22:46:07 +01002188 struct robust_list_head __user *head;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002189 unsigned long ret;
2190
2191 if (!pid)
2192 head = current->robust_list;
2193 else {
2194 struct task_struct *p;
2195
2196 ret = -ESRCH;
Oleg Nesterovaaa2a972006-09-29 02:00:55 -07002197 rcu_read_lock();
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002198 p = find_task_by_pid(pid);
2199 if (!p)
2200 goto err_unlock;
2201 ret = -EPERM;
2202 if ((current->euid != p->euid) && (current->euid != p->uid) &&
2203 !capable(CAP_SYS_PTRACE))
2204 goto err_unlock;
2205 head = p->robust_list;
Oleg Nesterovaaa2a972006-09-29 02:00:55 -07002206 rcu_read_unlock();
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002207 }
2208
2209 if (put_user(sizeof(*head), len_ptr))
2210 return -EFAULT;
2211 return put_user(head, head_ptr);
2212
2213err_unlock:
Oleg Nesterovaaa2a972006-09-29 02:00:55 -07002214 rcu_read_unlock();
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002215
2216 return ret;
2217}
2218
2219/*
2220 * Process a futex-list entry, check whether it's owned by the
2221 * dying task, and do notification if so:
2222 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002223int handle_futex_death(u32 __user *uaddr, struct task_struct *curr, int pi)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002224{
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002225 u32 uval, nval, mval;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002226
Ingo Molnar8f17d3a2006-03-27 01:16:27 -08002227retry:
2228 if (get_user(uval, uaddr))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002229 return -1;
2230
Ingo Molnar8f17d3a2006-03-27 01:16:27 -08002231 if ((uval & FUTEX_TID_MASK) == curr->pid) {
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002232 /*
2233 * Ok, this dying thread is truly holding a futex
2234 * of interest. Set the OWNER_DIED bit atomically
2235 * via cmpxchg, and if the value had FUTEX_WAITERS
2236 * set, wake up a waiter (if any). (We have to do a
2237 * futex_wake() even if OWNER_DIED is already set -
2238 * to handle the rare but possible case of recursive
2239 * thread-death.) The rest of the cleanup is done in
2240 * userspace.
2241 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002242 mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002243 /* Also keep the FUTEX_WAITER_REQUEUED flag if set */
2244 mval |= (uval & FUTEX_WAITER_REQUEUED);
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002245 nval = futex_atomic_cmpxchg_inatomic(uaddr, uval, mval);
2246
Ingo Molnarc87e2832006-06-27 02:54:58 -07002247 if (nval == -EFAULT)
2248 return -1;
2249
2250 if (nval != uval)
Ingo Molnar8f17d3a2006-03-27 01:16:27 -08002251 goto retry;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002252
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002253 /*
2254 * Wake robust non-PI futexes here. The wakeup of
2255 * PI futexes happens in exit_pi_state():
2256 */
2257 if (!pi) {
2258 if (uval & FUTEX_WAITERS)
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002259 futex_wake(uaddr, &curr->mm->mmap_sem, 1);
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002260 }
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002261 }
2262 return 0;
2263}
2264
2265/*
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002266 * Fetch a robust-list pointer. Bit 0 signals PI futexes:
2267 */
2268static inline int fetch_robust_entry(struct robust_list __user **entry,
Al Viroba46df92006-10-10 22:46:07 +01002269 struct robust_list __user * __user *head,
2270 int *pi)
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002271{
2272 unsigned long uentry;
2273
Al Viroba46df92006-10-10 22:46:07 +01002274 if (get_user(uentry, (unsigned long __user *)head))
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002275 return -EFAULT;
2276
Al Viroba46df92006-10-10 22:46:07 +01002277 *entry = (void __user *)(uentry & ~1UL);
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002278 *pi = uentry & 1;
2279
2280 return 0;
2281}
2282
2283/*
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002284 * Walk curr->robust_list (very carefully, it's a userspace list!)
2285 * and mark any locks found there dead, and notify any waiters.
2286 *
2287 * We silently return on any sign of list-walking problem.
2288 */
2289void exit_robust_list(struct task_struct *curr)
2290{
2291 struct robust_list_head __user *head = curr->robust_list;
2292 struct robust_list __user *entry, *pending;
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002293 unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002294 unsigned long futex_offset;
2295
2296 /*
2297 * Fetch the list head (which was registered earlier, via
2298 * sys_set_robust_list()):
2299 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002300 if (fetch_robust_entry(&entry, &head->list.next, &pi))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002301 return;
2302 /*
2303 * Fetch the relative futex offset:
2304 */
2305 if (get_user(futex_offset, &head->futex_offset))
2306 return;
2307 /*
2308 * Fetch any possibly pending lock-add first, and handle it
2309 * if it exists:
2310 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002311 if (fetch_robust_entry(&pending, &head->list_op_pending, &pip))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002312 return;
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002313
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002314 if (pending)
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002315 handle_futex_death((void __user *)pending + futex_offset,
2316 curr, pip);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002317
2318 while (entry != &head->list) {
2319 /*
2320 * A pending lock might already be on the list, so
Ingo Molnarc87e2832006-06-27 02:54:58 -07002321 * don't process it twice:
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002322 */
2323 if (entry != pending)
Al Viroba46df92006-10-10 22:46:07 +01002324 if (handle_futex_death((void __user *)entry + futex_offset,
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002325 curr, pi))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002326 return;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002327 /*
2328 * Fetch the next entry in the list:
2329 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002330 if (fetch_robust_entry(&entry, &entry->next, &pi))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002331 return;
2332 /*
2333 * Avoid excessively long or circular lists:
2334 */
2335 if (!--limit)
2336 break;
2337
2338 cond_resched();
2339 }
2340}
2341
Pierre Peifferc19384b2007-05-09 02:35:02 -07002342long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
Ingo Molnare2970f22006-06-27 02:54:47 -07002343 u32 __user *uaddr2, u32 val2, u32 val3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344{
2345 int ret;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002346 int cmd = op & FUTEX_CMD_MASK;
2347 struct rw_semaphore *fshared = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002349 if (!(op & FUTEX_PRIVATE_FLAG))
2350 fshared = &current->mm->mmap_sem;
2351
2352 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 case FUTEX_WAIT:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002354 ret = futex_wait(uaddr, fshared, val, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 break;
2356 case FUTEX_WAKE:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002357 ret = futex_wake(uaddr, fshared, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 break;
2359 case FUTEX_FD:
2360 /* non-zero val means F_SETOWN(getpid()) & F_SETSIG(val) */
2361 ret = futex_fd(uaddr, val);
2362 break;
2363 case FUTEX_REQUEUE:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002364 ret = futex_requeue(uaddr, fshared, uaddr2, val, val2, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 break;
2366 case FUTEX_CMP_REQUEUE:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002367 ret = futex_requeue(uaddr, fshared, uaddr2, val, val2, &val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 break;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07002369 case FUTEX_WAKE_OP:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002370 ret = futex_wake_op(uaddr, fshared, uaddr2, val, val2, val3);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07002371 break;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002372 case FUTEX_LOCK_PI:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002373 ret = futex_lock_pi(uaddr, fshared, val, timeout, 0);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002374 break;
2375 case FUTEX_UNLOCK_PI:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002376 ret = futex_unlock_pi(uaddr, fshared);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002377 break;
2378 case FUTEX_TRYLOCK_PI:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002379 ret = futex_lock_pi(uaddr, fshared, 0, timeout, 1);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002380 break;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002381 case FUTEX_CMP_REQUEUE_PI:
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002382 ret = futex_requeue_pi(uaddr, fshared, uaddr2, val, val2, &val3);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002383 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 default:
2385 ret = -ENOSYS;
2386 }
2387 return ret;
2388}
2389
2390
Ingo Molnare2970f22006-06-27 02:54:47 -07002391asmlinkage long sys_futex(u32 __user *uaddr, int op, u32 val,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 struct timespec __user *utime, u32 __user *uaddr2,
Ingo Molnare2970f22006-06-27 02:54:47 -07002393 u32 val3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394{
Pierre Peifferc19384b2007-05-09 02:35:02 -07002395 struct timespec ts;
2396 ktime_t t, *tp = NULL;
Ingo Molnare2970f22006-06-27 02:54:47 -07002397 u32 val2 = 0;
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002398 int cmd = op & FUTEX_CMD_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002400 if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI)) {
Pierre Peifferc19384b2007-05-09 02:35:02 -07002401 if (copy_from_user(&ts, utime, sizeof(ts)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 return -EFAULT;
Pierre Peifferc19384b2007-05-09 02:35:02 -07002403 if (!timespec_valid(&ts))
Thomas Gleixner9741ef962006-03-31 02:31:32 -08002404 return -EINVAL;
Pierre Peifferc19384b2007-05-09 02:35:02 -07002405
2406 t = timespec_to_ktime(ts);
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002407 if (cmd == FUTEX_WAIT)
Pierre Peifferc19384b2007-05-09 02:35:02 -07002408 t = ktime_add(ktime_get(), t);
2409 tp = &t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 }
2411 /*
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002412 * requeue parameter in 'utime' if cmd == FUTEX_REQUEUE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 */
Eric Dumazet34f01cc2007-05-09 02:35:04 -07002414 if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE
2415 || cmd == FUTEX_CMP_REQUEUE_PI)
Ingo Molnare2970f22006-06-27 02:54:47 -07002416 val2 = (u32) (unsigned long) utime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
Pierre Peifferc19384b2007-05-09 02:35:02 -07002418 return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419}
2420
David Howells454e2392006-06-23 02:02:57 -07002421static int futexfs_get_sb(struct file_system_type *fs_type,
2422 int flags, const char *dev_name, void *data,
2423 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424{
David Howells454e2392006-06-23 02:02:57 -07002425 return get_sb_pseudo(fs_type, "futex", NULL, 0xBAD1DEA, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426}
2427
2428static struct file_system_type futex_fs_type = {
2429 .name = "futexfs",
2430 .get_sb = futexfs_get_sb,
2431 .kill_sb = kill_anon_super,
2432};
2433
2434static int __init init(void)
2435{
Akinobu Mita95362fa2006-12-06 20:39:03 -08002436 int i = register_filesystem(&futex_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
Akinobu Mita95362fa2006-12-06 20:39:03 -08002438 if (i)
2439 return i;
2440
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 futex_mnt = kern_mount(&futex_fs_type);
Akinobu Mita95362fa2006-12-06 20:39:03 -08002442 if (IS_ERR(futex_mnt)) {
2443 unregister_filesystem(&futex_fs_type);
2444 return PTR_ERR(futex_mnt);
2445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
2447 for (i = 0; i < ARRAY_SIZE(futex_queues); i++) {
Pierre Peifferec92d082007-05-09 02:35:00 -07002448 plist_head_init(&futex_queues[i].chain, &futex_queues[i].lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 spin_lock_init(&futex_queues[i].lock);
2450 }
2451 return 0;
2452}
2453__initcall(init);