blob: 4a60ef55dab4e7bccc8cf0111445aa9a0e9b952b [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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * Thanks to Ben LaHaise for yelling "hashed waitqueues" loudly
20 * enough at me, Linus for the original (flawed) idea, Matthew
21 * Kirkwood for proof-of-concept implementation.
22 *
23 * "The futexes are also cursed."
24 * "But they come in a choice of three flavours!"
25 *
26 * This program is free software; you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation; either version 2 of the License, or
29 * (at your option) any later version.
30 *
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
35 *
36 * You should have received a copy of the GNU General Public License
37 * along with this program; if not, write to the Free Software
38 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39 */
40#include <linux/slab.h>
41#include <linux/poll.h>
42#include <linux/fs.h>
43#include <linux/file.h>
44#include <linux/jhash.h>
45#include <linux/init.h>
46#include <linux/futex.h>
47#include <linux/mount.h>
48#include <linux/pagemap.h>
49#include <linux/syscalls.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070050#include <linux/signal.h>
Rusty Russell9adef582007-05-08 00:26:42 -070051#include <linux/module.h>
Jakub Jelinek4732efb2005-09-06 15:16:25 -070052#include <asm/futex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Ingo Molnarc87e2832006-06-27 02:54:58 -070054#include "rtmutex_common.h"
55
Pierre Peifferd0aa7a72007-05-09 02:35:02 -070056#ifdef CONFIG_DEBUG_RT_MUTEXES
57# include "rtmutex-debug.h"
58#else
59# include "rtmutex.h"
60#endif
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define FUTEX_HASHBITS (CONFIG_BASE_SMALL ? 4 : 8)
63
64/*
Ingo Molnarc87e2832006-06-27 02:54:58 -070065 * Priority Inheritance state:
66 */
67struct futex_pi_state {
68 /*
69 * list of 'owned' pi_state instances - these have to be
70 * cleaned up in do_exit() if the task exits prematurely:
71 */
72 struct list_head list;
73
74 /*
75 * The PI object:
76 */
77 struct rt_mutex pi_mutex;
78
79 struct task_struct *owner;
80 atomic_t refcount;
81
82 union futex_key key;
83};
84
85/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * We use this hashed waitqueue instead of a normal wait_queue_t, so
87 * we can wake only the relevant ones (hashed queues may be shared).
88 *
89 * A futex_q has a woken state, just like tasks have TASK_RUNNING.
Pierre Peifferec92d082007-05-09 02:35:00 -070090 * It is considered woken when plist_node_empty(&q->list) || q->lock_ptr == 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * The order of wakup is always to make the first condition true, then
92 * wake up q->waiters, then make the second condition true.
93 */
94struct futex_q {
Pierre Peifferec92d082007-05-09 02:35:00 -070095 struct plist_node list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 wait_queue_head_t waiters;
97
Ingo Molnare2970f22006-06-27 02:54:47 -070098 /* Which hash list lock to use: */
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 spinlock_t *lock_ptr;
100
Ingo Molnare2970f22006-06-27 02:54:47 -0700101 /* Key which the futex is hashed on: */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 union futex_key key;
103
Ingo Molnare2970f22006-06-27 02:54:47 -0700104 /* For fd, sigio sent using these: */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 int fd;
106 struct file *filp;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700107
108 /* Optional priority inheritance state: */
109 struct futex_pi_state *pi_state;
110 struct task_struct *task;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700111
112 /*
113 * This waiter is used in case of requeue from a
114 * normal futex to a PI-futex
115 */
116 struct rt_mutex_waiter waiter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117};
118
119/*
120 * Split the global futex_lock into every hash list lock.
121 */
122struct futex_hash_bucket {
Pierre Peifferec92d082007-05-09 02:35:00 -0700123 spinlock_t lock;
124 struct plist_head chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
127static struct futex_hash_bucket futex_queues[1<<FUTEX_HASHBITS];
128
129/* Futex-fs vfsmount entry: */
130static struct vfsmount *futex_mnt;
131
132/*
133 * We hash on the keys returned from get_futex_key (see below).
134 */
135static struct futex_hash_bucket *hash_futex(union futex_key *key)
136{
137 u32 hash = jhash2((u32*)&key->both.word,
138 (sizeof(key->both.word)+sizeof(key->both.ptr))/4,
139 key->both.offset);
140 return &futex_queues[hash & ((1 << FUTEX_HASHBITS)-1)];
141}
142
143/*
144 * Return 1 if two futex_keys are equal, 0 otherwise.
145 */
146static inline int match_futex(union futex_key *key1, union futex_key *key2)
147{
148 return (key1->both.word == key2->both.word
149 && key1->both.ptr == key2->both.ptr
150 && key1->both.offset == key2->both.offset);
151}
152
153/*
154 * Get parameters which are the keys for a futex.
155 *
Josef "Jeff" Sipekf3a43f32006-12-08 02:36:43 -0800156 * For shared mappings, it's (page->index, vma->vm_file->f_path.dentry->d_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 * offset_within_page). For private mappings, it's (uaddr, current->mm).
158 * We can usually work out the index without swapping in the page.
159 *
160 * Returns: 0, or negative error code.
161 * The key words are stored in *key on success.
162 *
163 * Should be called with &current->mm->mmap_sem but NOT any spinlocks.
164 */
Rusty Russell9adef582007-05-08 00:26:42 -0700165int get_futex_key(u32 __user *uaddr, union futex_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Ingo Molnare2970f22006-06-27 02:54:47 -0700167 unsigned long address = (unsigned long)uaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 struct mm_struct *mm = current->mm;
169 struct vm_area_struct *vma;
170 struct page *page;
171 int err;
172
173 /*
174 * The futex address must be "naturally" aligned.
175 */
Ingo Molnare2970f22006-06-27 02:54:47 -0700176 key->both.offset = address % PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 if (unlikely((key->both.offset % sizeof(u32)) != 0))
178 return -EINVAL;
Ingo Molnare2970f22006-06-27 02:54:47 -0700179 address -= key->both.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 /*
182 * The futex is hashed differently depending on whether
183 * it's in a shared or private mapping. So check vma first.
184 */
Ingo Molnare2970f22006-06-27 02:54:47 -0700185 vma = find_extend_vma(mm, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if (unlikely(!vma))
187 return -EFAULT;
188
189 /*
190 * Permissions.
191 */
192 if (unlikely((vma->vm_flags & (VM_IO|VM_READ)) != VM_READ))
193 return (vma->vm_flags & VM_IO) ? -EPERM : -EACCES;
194
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700195 /* Save the user address in the ley */
196 key->uaddr = uaddr;
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 /*
199 * Private mappings are handled in a simple way.
200 *
201 * NOTE: When userspace waits on a MAP_SHARED mapping, even if
202 * it's a read-only handle, it's expected that futexes attach to
203 * the object not the particular process. Therefore we use
204 * VM_MAYSHARE here, not VM_SHARED which is restricted to shared
205 * mappings of _writable_ handles.
206 */
207 if (likely(!(vma->vm_flags & VM_MAYSHARE))) {
208 key->private.mm = mm;
Ingo Molnare2970f22006-06-27 02:54:47 -0700209 key->private.address = address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return 0;
211 }
212
213 /*
214 * Linear file mappings are also simple.
215 */
Josef "Jeff" Sipekf3a43f32006-12-08 02:36:43 -0800216 key->shared.inode = vma->vm_file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 key->both.offset++; /* Bit 0 of offset indicates inode-based key. */
218 if (likely(!(vma->vm_flags & VM_NONLINEAR))) {
Ingo Molnare2970f22006-06-27 02:54:47 -0700219 key->shared.pgoff = (((address - vma->vm_start) >> PAGE_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 + vma->vm_pgoff);
221 return 0;
222 }
223
224 /*
225 * We could walk the page table to read the non-linear
226 * pte, and get the page index without fetching the page
227 * from swap. But that's a lot of code to duplicate here
228 * for a rare case, so we simply fetch the page.
229 */
Ingo Molnare2970f22006-06-27 02:54:47 -0700230 err = get_user_pages(current, mm, address, 1, 0, 0, &page, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (err >= 0) {
232 key->shared.pgoff =
233 page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
234 put_page(page);
235 return 0;
236 }
237 return err;
238}
Rusty Russell9adef582007-05-08 00:26:42 -0700239EXPORT_SYMBOL_GPL(get_futex_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241/*
242 * Take a reference to the resource addressed by a key.
243 * Can be called while holding spinlocks.
244 *
245 * NOTE: mmap_sem MUST be held between get_futex_key() and calling this
246 * function, if it is called at all. mmap_sem keeps key->shared.inode valid.
247 */
Rusty Russell9adef582007-05-08 00:26:42 -0700248inline void get_futex_key_refs(union futex_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 if (key->both.ptr != 0) {
251 if (key->both.offset & 1)
252 atomic_inc(&key->shared.inode->i_count);
253 else
254 atomic_inc(&key->private.mm->mm_count);
255 }
256}
Rusty Russell9adef582007-05-08 00:26:42 -0700257EXPORT_SYMBOL_GPL(get_futex_key_refs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259/*
260 * Drop a reference to the resource addressed by a key.
261 * The hash bucket spinlock must not be held.
262 */
Rusty Russell9adef582007-05-08 00:26:42 -0700263void drop_futex_key_refs(union futex_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 if (key->both.ptr != 0) {
266 if (key->both.offset & 1)
267 iput(key->shared.inode);
268 else
269 mmdrop(key->private.mm);
270 }
271}
Rusty Russell9adef582007-05-08 00:26:42 -0700272EXPORT_SYMBOL_GPL(drop_futex_key_refs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Ingo Molnare2970f22006-06-27 02:54:47 -0700274static inline int get_futex_value_locked(u32 *dest, u32 __user *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 int ret;
277
Peter Zijlstraa8663742006-12-06 20:32:20 -0800278 pagefault_disable();
Ingo Molnare2970f22006-06-27 02:54:47 -0700279 ret = __copy_from_user_inatomic(dest, from, sizeof(u32));
Peter Zijlstraa8663742006-12-06 20:32:20 -0800280 pagefault_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 return ret ? -EFAULT : 0;
283}
284
285/*
Ingo Molnarc87e2832006-06-27 02:54:58 -0700286 * Fault handling. Called with current->mm->mmap_sem held.
287 */
288static int futex_handle_fault(unsigned long address, int attempt)
289{
290 struct vm_area_struct * vma;
291 struct mm_struct *mm = current->mm;
292
john stultze579dcb2006-08-13 23:24:24 -0700293 if (attempt > 2 || !(vma = find_vma(mm, address)) ||
Ingo Molnarc87e2832006-06-27 02:54:58 -0700294 vma->vm_start > address || !(vma->vm_flags & VM_WRITE))
295 return -EFAULT;
296
297 switch (handle_mm_fault(mm, vma, address, 1)) {
298 case VM_FAULT_MINOR:
299 current->min_flt++;
300 break;
301 case VM_FAULT_MAJOR:
302 current->maj_flt++;
303 break;
304 default:
305 return -EFAULT;
306 }
307 return 0;
308}
309
310/*
311 * PI code:
312 */
313static int refill_pi_state_cache(void)
314{
315 struct futex_pi_state *pi_state;
316
317 if (likely(current->pi_state_cache))
318 return 0;
319
Burman Yan4668edc2006-12-06 20:38:51 -0800320 pi_state = kzalloc(sizeof(*pi_state), GFP_KERNEL);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700321
322 if (!pi_state)
323 return -ENOMEM;
324
Ingo Molnarc87e2832006-06-27 02:54:58 -0700325 INIT_LIST_HEAD(&pi_state->list);
326 /* pi_mutex gets initialized later */
327 pi_state->owner = NULL;
328 atomic_set(&pi_state->refcount, 1);
329
330 current->pi_state_cache = pi_state;
331
332 return 0;
333}
334
335static struct futex_pi_state * alloc_pi_state(void)
336{
337 struct futex_pi_state *pi_state = current->pi_state_cache;
338
339 WARN_ON(!pi_state);
340 current->pi_state_cache = NULL;
341
342 return pi_state;
343}
344
345static void free_pi_state(struct futex_pi_state *pi_state)
346{
347 if (!atomic_dec_and_test(&pi_state->refcount))
348 return;
349
350 /*
351 * If pi_state->owner is NULL, the owner is most probably dying
352 * and has cleaned up the pi_state already
353 */
354 if (pi_state->owner) {
355 spin_lock_irq(&pi_state->owner->pi_lock);
356 list_del_init(&pi_state->list);
357 spin_unlock_irq(&pi_state->owner->pi_lock);
358
359 rt_mutex_proxy_unlock(&pi_state->pi_mutex, pi_state->owner);
360 }
361
362 if (current->pi_state_cache)
363 kfree(pi_state);
364 else {
365 /*
366 * pi_state->list is already empty.
367 * clear pi_state->owner.
368 * refcount is at 0 - put it back to 1.
369 */
370 pi_state->owner = NULL;
371 atomic_set(&pi_state->refcount, 1);
372 current->pi_state_cache = pi_state;
373 }
374}
375
376/*
377 * Look up the task based on what TID userspace gave us.
378 * We dont trust it.
379 */
380static struct task_struct * futex_find_get_task(pid_t pid)
381{
382 struct task_struct *p;
383
Oleg Nesterovd359b542006-09-29 02:00:55 -0700384 rcu_read_lock();
Ingo Molnarc87e2832006-06-27 02:54:58 -0700385 p = find_task_by_pid(pid);
386 if (!p)
387 goto out_unlock;
388 if ((current->euid != p->euid) && (current->euid != p->uid)) {
389 p = NULL;
390 goto out_unlock;
391 }
Oleg Nesterovd015bae2006-08-27 01:23:40 -0700392 if (p->exit_state != 0) {
Ingo Molnarc87e2832006-06-27 02:54:58 -0700393 p = NULL;
394 goto out_unlock;
395 }
396 get_task_struct(p);
397out_unlock:
Oleg Nesterovd359b542006-09-29 02:00:55 -0700398 rcu_read_unlock();
Ingo Molnarc87e2832006-06-27 02:54:58 -0700399
400 return p;
401}
402
403/*
404 * This task is holding PI mutexes at exit time => bad.
405 * Kernel cleans up PI-state, but userspace is likely hosed.
406 * (Robust-futex cleanup is separate and might save the day for userspace.)
407 */
408void exit_pi_state_list(struct task_struct *curr)
409{
Ingo Molnarc87e2832006-06-27 02:54:58 -0700410 struct list_head *next, *head = &curr->pi_state_list;
411 struct futex_pi_state *pi_state;
Ingo Molnar627371d2006-07-29 05:16:20 +0200412 struct futex_hash_bucket *hb;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700413 union futex_key key;
414
415 /*
416 * We are a ZOMBIE and nobody can enqueue itself on
417 * pi_state_list anymore, but we have to be careful
Ingo Molnar627371d2006-07-29 05:16:20 +0200418 * versus waiters unqueueing themselves:
Ingo Molnarc87e2832006-06-27 02:54:58 -0700419 */
420 spin_lock_irq(&curr->pi_lock);
421 while (!list_empty(head)) {
422
423 next = head->next;
424 pi_state = list_entry(next, struct futex_pi_state, list);
425 key = pi_state->key;
Ingo Molnar627371d2006-07-29 05:16:20 +0200426 hb = hash_futex(&key);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700427 spin_unlock_irq(&curr->pi_lock);
428
Ingo Molnarc87e2832006-06-27 02:54:58 -0700429 spin_lock(&hb->lock);
430
431 spin_lock_irq(&curr->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +0200432 /*
433 * We dropped the pi-lock, so re-check whether this
434 * task still owns the PI-state:
435 */
Ingo Molnarc87e2832006-06-27 02:54:58 -0700436 if (head->next != next) {
437 spin_unlock(&hb->lock);
438 continue;
439 }
440
Ingo Molnarc87e2832006-06-27 02:54:58 -0700441 WARN_ON(pi_state->owner != curr);
Ingo Molnar627371d2006-07-29 05:16:20 +0200442 WARN_ON(list_empty(&pi_state->list));
443 list_del_init(&pi_state->list);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700444 pi_state->owner = NULL;
445 spin_unlock_irq(&curr->pi_lock);
446
447 rt_mutex_unlock(&pi_state->pi_mutex);
448
449 spin_unlock(&hb->lock);
450
451 spin_lock_irq(&curr->pi_lock);
452 }
453 spin_unlock_irq(&curr->pi_lock);
454}
455
456static int
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700457lookup_pi_state(u32 uval, struct futex_hash_bucket *hb,
458 union futex_key *key, struct futex_pi_state **ps)
Ingo Molnarc87e2832006-06-27 02:54:58 -0700459{
460 struct futex_pi_state *pi_state = NULL;
461 struct futex_q *this, *next;
Pierre Peifferec92d082007-05-09 02:35:00 -0700462 struct plist_head *head;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700463 struct task_struct *p;
464 pid_t pid;
465
466 head = &hb->chain;
467
Pierre Peifferec92d082007-05-09 02:35:00 -0700468 plist_for_each_entry_safe(this, next, head, list) {
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700469 if (match_futex(&this->key, key)) {
Ingo Molnarc87e2832006-06-27 02:54:58 -0700470 /*
471 * Another waiter already exists - bump up
472 * the refcount and return its pi_state:
473 */
474 pi_state = this->pi_state;
Thomas Gleixner06a9ec22006-07-10 04:44:30 -0700475 /*
476 * Userspace might have messed up non PI and PI futexes
477 */
478 if (unlikely(!pi_state))
479 return -EINVAL;
480
Ingo Molnar627371d2006-07-29 05:16:20 +0200481 WARN_ON(!atomic_read(&pi_state->refcount));
482
Ingo Molnarc87e2832006-06-27 02:54:58 -0700483 atomic_inc(&pi_state->refcount);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700484 *ps = pi_state;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700485
486 return 0;
487 }
488 }
489
490 /*
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200491 * We are the first waiter - try to look up the real owner and attach
492 * the new pi_state to it, but bail out when the owner died bit is set
493 * and TID = 0:
Ingo Molnarc87e2832006-06-27 02:54:58 -0700494 */
495 pid = uval & FUTEX_TID_MASK;
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200496 if (!pid && (uval & FUTEX_OWNER_DIED))
497 return -ESRCH;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700498 p = futex_find_get_task(pid);
499 if (!p)
500 return -ESRCH;
501
502 pi_state = alloc_pi_state();
503
504 /*
505 * Initialize the pi_mutex in locked state and make 'p'
506 * the owner of it:
507 */
508 rt_mutex_init_proxy_locked(&pi_state->pi_mutex, p);
509
510 /* Store the key for possible exit cleanups: */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700511 pi_state->key = *key;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700512
513 spin_lock_irq(&p->pi_lock);
Ingo Molnar627371d2006-07-29 05:16:20 +0200514 WARN_ON(!list_empty(&pi_state->list));
Ingo Molnarc87e2832006-06-27 02:54:58 -0700515 list_add(&pi_state->list, &p->pi_state_list);
516 pi_state->owner = p;
517 spin_unlock_irq(&p->pi_lock);
518
519 put_task_struct(p);
520
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700521 *ps = pi_state;
Ingo Molnarc87e2832006-06-27 02:54:58 -0700522
523 return 0;
524}
525
526/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 * The hash bucket lock must be held when this is called.
528 * Afterwards, the futex_q must not be accessed.
529 */
530static void wake_futex(struct futex_q *q)
531{
Pierre Peifferec92d082007-05-09 02:35:00 -0700532 plist_del(&q->list, &q->list.plist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (q->filp)
534 send_sigio(&q->filp->f_owner, q->fd, POLL_IN);
535 /*
536 * The lock in wake_up_all() is a crucial memory barrier after the
Pierre Peifferec92d082007-05-09 02:35:00 -0700537 * plist_del() and also before assigning to q->lock_ptr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 */
539 wake_up_all(&q->waiters);
540 /*
541 * The waiting task can free the futex_q as soon as this is written,
542 * without taking any locks. This must come last.
Andrew Morton8e311082005-12-23 19:54:46 -0800543 *
544 * A memory barrier is required here to prevent the following store
545 * to lock_ptr from getting ahead of the wakeup. Clearing the lock
546 * at the end of wake_up_all() does not prevent this store from
547 * moving.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 */
Ralf Baechleccdea2f2006-12-06 20:40:26 -0800549 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 q->lock_ptr = NULL;
551}
552
Ingo Molnarc87e2832006-06-27 02:54:58 -0700553static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this)
554{
555 struct task_struct *new_owner;
556 struct futex_pi_state *pi_state = this->pi_state;
557 u32 curval, newval;
558
559 if (!pi_state)
560 return -EINVAL;
561
Ingo Molnar217788672007-03-16 13:38:31 -0800562 spin_lock(&pi_state->pi_mutex.wait_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700563 new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);
564
565 /*
566 * This happens when we have stolen the lock and the original
567 * pending owner did not enqueue itself back on the rt_mutex.
568 * Thats not a tragedy. We know that way, that a lock waiter
569 * is on the fly. We make the futex_q waiter the pending owner.
570 */
571 if (!new_owner)
572 new_owner = this->task;
573
574 /*
575 * We pass it to the next owner. (The WAITERS bit is always
576 * kept enabled while there is PI state around. We must also
577 * preserve the owner died bit.)
578 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200579 if (!(uval & FUTEX_OWNER_DIED)) {
580 newval = FUTEX_WAITERS | new_owner->pid;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700581 /* Keep the FUTEX_WAITER_REQUEUED flag if it was set */
582 newval |= (uval & FUTEX_WAITER_REQUEUED);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700583
Peter Zijlstraa8663742006-12-06 20:32:20 -0800584 pagefault_disable();
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200585 curval = futex_atomic_cmpxchg_inatomic(uaddr, uval, newval);
Peter Zijlstraa8663742006-12-06 20:32:20 -0800586 pagefault_enable();
Ingo Molnare3f2dde2006-07-29 05:17:57 +0200587 if (curval == -EFAULT)
588 return -EFAULT;
589 if (curval != uval)
590 return -EINVAL;
591 }
Ingo Molnarc87e2832006-06-27 02:54:58 -0700592
Ingo Molnar627371d2006-07-29 05:16:20 +0200593 spin_lock_irq(&pi_state->owner->pi_lock);
594 WARN_ON(list_empty(&pi_state->list));
595 list_del_init(&pi_state->list);
596 spin_unlock_irq(&pi_state->owner->pi_lock);
597
598 spin_lock_irq(&new_owner->pi_lock);
599 WARN_ON(!list_empty(&pi_state->list));
Ingo Molnarc87e2832006-06-27 02:54:58 -0700600 list_add(&pi_state->list, &new_owner->pi_state_list);
601 pi_state->owner = new_owner;
Ingo Molnar627371d2006-07-29 05:16:20 +0200602 spin_unlock_irq(&new_owner->pi_lock);
603
Ingo Molnar217788672007-03-16 13:38:31 -0800604 spin_unlock(&pi_state->pi_mutex.wait_lock);
Ingo Molnarc87e2832006-06-27 02:54:58 -0700605 rt_mutex_unlock(&pi_state->pi_mutex);
606
607 return 0;
608}
609
610static int unlock_futex_pi(u32 __user *uaddr, u32 uval)
611{
612 u32 oldval;
613
614 /*
615 * There is no waiter, so we unlock the futex. The owner died
616 * bit has not to be preserved here. We are the owner:
617 */
Peter Zijlstraa8663742006-12-06 20:32:20 -0800618 pagefault_disable();
Ingo Molnarc87e2832006-06-27 02:54:58 -0700619 oldval = futex_atomic_cmpxchg_inatomic(uaddr, uval, 0);
Peter Zijlstraa8663742006-12-06 20:32:20 -0800620 pagefault_enable();
Ingo Molnarc87e2832006-06-27 02:54:58 -0700621
622 if (oldval == -EFAULT)
623 return oldval;
624 if (oldval != uval)
625 return -EAGAIN;
626
627 return 0;
628}
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630/*
Ingo Molnar8b8f3192006-07-03 00:25:05 -0700631 * Express the locking dependencies for lockdep:
632 */
633static inline void
634double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
635{
636 if (hb1 <= hb2) {
637 spin_lock(&hb1->lock);
638 if (hb1 < hb2)
639 spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING);
640 } else { /* hb1 > hb2 */
641 spin_lock(&hb2->lock);
642 spin_lock_nested(&hb1->lock, SINGLE_DEPTH_NESTING);
643 }
644}
645
646/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 * Wake up all waiters hashed on the physical page that is mapped
648 * to this virtual address:
649 */
Ingo Molnare2970f22006-06-27 02:54:47 -0700650static int futex_wake(u32 __user *uaddr, int nr_wake)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Ingo Molnare2970f22006-06-27 02:54:47 -0700652 struct futex_hash_bucket *hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 struct futex_q *this, *next;
Pierre Peifferec92d082007-05-09 02:35:00 -0700654 struct plist_head *head;
Ingo Molnare2970f22006-06-27 02:54:47 -0700655 union futex_key key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 int ret;
657
658 down_read(&current->mm->mmap_sem);
659
660 ret = get_futex_key(uaddr, &key);
661 if (unlikely(ret != 0))
662 goto out;
663
Ingo Molnare2970f22006-06-27 02:54:47 -0700664 hb = hash_futex(&key);
665 spin_lock(&hb->lock);
666 head = &hb->chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Pierre Peifferec92d082007-05-09 02:35:00 -0700668 plist_for_each_entry_safe(this, next, head, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (match_futex (&this->key, &key)) {
Ingo Molnared6f7b12006-07-01 04:35:46 -0700670 if (this->pi_state) {
671 ret = -EINVAL;
672 break;
673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 wake_futex(this);
675 if (++ret >= nr_wake)
676 break;
677 }
678 }
679
Ingo Molnare2970f22006-06-27 02:54:47 -0700680 spin_unlock(&hb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681out:
682 up_read(&current->mm->mmap_sem);
683 return ret;
684}
685
686/*
Pierre Peifferd0aa7a72007-05-09 02:35:02 -0700687 * Called from futex_requeue_pi.
688 * Set FUTEX_WAITERS and FUTEX_WAITER_REQUEUED flags on the
689 * PI-futex value; search its associated pi_state if an owner exist
690 * or create a new one without owner.
691 */
692static inline int
693lookup_pi_state_for_requeue(u32 __user *uaddr, struct futex_hash_bucket *hb,
694 union futex_key *key,
695 struct futex_pi_state **pi_state)
696{
697 u32 curval, uval, newval;
698
699retry:
700 /*
701 * We can't handle a fault cleanly because we can't
702 * release the locks here. Simply return the fault.
703 */
704 if (get_futex_value_locked(&curval, uaddr))
705 return -EFAULT;
706
707 /* set the flags FUTEX_WAITERS and FUTEX_WAITER_REQUEUED */
708 if ((curval & (FUTEX_WAITERS | FUTEX_WAITER_REQUEUED))
709 != (FUTEX_WAITERS | FUTEX_WAITER_REQUEUED)) {
710 /*
711 * No waiters yet, we prepare the futex to have some waiters.
712 */
713
714 uval = curval;
715 newval = uval | FUTEX_WAITERS | FUTEX_WAITER_REQUEUED;
716
717 pagefault_disable();
718 curval = futex_atomic_cmpxchg_inatomic(uaddr, uval, newval);
719 pagefault_enable();
720
721 if (unlikely(curval == -EFAULT))
722 return -EFAULT;
723 if (unlikely(curval != uval))
724 goto retry;
725 }
726
727 if (!(curval & FUTEX_TID_MASK)
728 || lookup_pi_state(curval, hb, key, pi_state)) {
729 /* the futex has no owner (yet) or the lookup failed:
730 allocate one pi_state without owner */
731
732 *pi_state = alloc_pi_state();
733
734 /* Already stores the key: */
735 (*pi_state)->key = *key;
736
737 /* init the mutex without owner */
738 __rt_mutex_init(&(*pi_state)->pi_mutex, NULL);
739 }
740
741 return 0;
742}
743
744/*
745 * Keep the first nr_wake waiter from futex1, wake up one,
746 * and requeue the next nr_requeue waiters following hashed on
747 * one physical page to another physical page (PI-futex uaddr2)
748 */
749static int futex_requeue_pi(u32 __user *uaddr1, u32 __user *uaddr2,
750 int nr_wake, int nr_requeue, u32 *cmpval)
751{
752 union futex_key key1, key2;
753 struct futex_hash_bucket *hb1, *hb2;
754 struct plist_head *head1;
755 struct futex_q *this, *next;
756 struct futex_pi_state *pi_state2 = NULL;
757 struct rt_mutex_waiter *waiter, *top_waiter = NULL;
758 struct rt_mutex *lock2 = NULL;
759 int ret, drop_count = 0;
760
761 if (refill_pi_state_cache())
762 return -ENOMEM;
763
764retry:
765 /*
766 * First take all the futex related locks:
767 */
768 down_read(&current->mm->mmap_sem);
769
770 ret = get_futex_key(uaddr1, &key1);
771 if (unlikely(ret != 0))
772 goto out;
773 ret = get_futex_key(uaddr2, &key2);
774 if (unlikely(ret != 0))
775 goto out;
776
777 hb1 = hash_futex(&key1);
778 hb2 = hash_futex(&key2);
779
780 double_lock_hb(hb1, hb2);
781
782 if (likely(cmpval != NULL)) {
783 u32 curval;
784
785 ret = get_futex_value_locked(&curval, uaddr1);
786
787 if (unlikely(ret)) {
788 spin_unlock(&hb1->lock);
789 if (hb1 != hb2)
790 spin_unlock(&hb2->lock);
791
792 /*
793 * If we would have faulted, release mmap_sem, fault
794 * it in and start all over again.
795 */
796 up_read(&current->mm->mmap_sem);
797
798 ret = get_user(curval, uaddr1);
799
800 if (!ret)
801 goto retry;
802
803 return ret;
804 }
805 if (curval != *cmpval) {
806 ret = -EAGAIN;
807 goto out_unlock;
808 }
809 }
810
811 head1 = &hb1->chain;
812 plist_for_each_entry_safe(this, next, head1, list) {
813 if (!match_futex (&this->key, &key1))
814 continue;
815 if (++ret <= nr_wake) {
816 wake_futex(this);
817 } else {
818 /*
819 * FIRST: get and set the pi_state
820 */
821 if (!pi_state2) {
822 int s;
823 /* do this only the first time we requeue someone */
824 s = lookup_pi_state_for_requeue(uaddr2, hb2,
825 &key2, &pi_state2);
826 if (s) {
827 ret = s;
828 goto out_unlock;
829 }
830
831 lock2 = &pi_state2->pi_mutex;
832 spin_lock(&lock2->wait_lock);
833
834 /* Save the top waiter of the wait_list */
835 if (rt_mutex_has_waiters(lock2))
836 top_waiter = rt_mutex_top_waiter(lock2);
837 } else
838 atomic_inc(&pi_state2->refcount);
839
840
841 this->pi_state = pi_state2;
842
843 /*
844 * SECOND: requeue futex_q to the correct hashbucket
845 */
846
847 /*
848 * If key1 and key2 hash to the same bucket, no need to
849 * requeue.
850 */
851 if (likely(head1 != &hb2->chain)) {
852 plist_del(&this->list, &hb1->chain);
853 plist_add(&this->list, &hb2->chain);
854 this->lock_ptr = &hb2->lock;
855#ifdef CONFIG_DEBUG_PI_LIST
856 this->list.plist.lock = &hb2->lock;
857#endif
858 }
859 this->key = key2;
860 get_futex_key_refs(&key2);
861 drop_count++;
862
863
864 /*
865 * THIRD: queue it to lock2
866 */
867 spin_lock_irq(&this->task->pi_lock);
868 waiter = &this->waiter;
869 waiter->task = this->task;
870 waiter->lock = lock2;
871 plist_node_init(&waiter->list_entry, this->task->prio);
872 plist_node_init(&waiter->pi_list_entry, this->task->prio);
873 plist_add(&waiter->list_entry, &lock2->wait_list);
874 this->task->pi_blocked_on = waiter;
875 spin_unlock_irq(&this->task->pi_lock);
876
877 if (ret - nr_wake >= nr_requeue)
878 break;
879 }
880 }
881
882 /* If we've requeued some tasks and the top_waiter of the rt_mutex
883 has changed, we must adjust the priority of the owner, if any */
884 if (drop_count) {
885 struct task_struct *owner = rt_mutex_owner(lock2);
886 if (owner &&
887 (top_waiter != (waiter = rt_mutex_top_waiter(lock2)))) {
888 int chain_walk = 0;
889
890 spin_lock_irq(&owner->pi_lock);
891 if (top_waiter)
892 plist_del(&top_waiter->pi_list_entry, &owner->pi_waiters);
893 else
894 /*
895 * There was no waiters before the requeue,
896 * the flag must be updated
897 */
898 mark_rt_mutex_waiters(lock2);
899
900 plist_add(&waiter->pi_list_entry, &owner->pi_waiters);
901 __rt_mutex_adjust_prio(owner);
902 if (owner->pi_blocked_on) {
903 chain_walk = 1;
904 get_task_struct(owner);
905 }
906
907 spin_unlock_irq(&owner->pi_lock);
908 spin_unlock(&lock2->wait_lock);
909
910 if (chain_walk)
911 rt_mutex_adjust_prio_chain(owner, 0, lock2, NULL,
912 current);
913 } else {
914 /* No owner or the top_waiter does not change */
915 mark_rt_mutex_waiters(lock2);
916 spin_unlock(&lock2->wait_lock);
917 }
918 }
919
920out_unlock:
921 spin_unlock(&hb1->lock);
922 if (hb1 != hb2)
923 spin_unlock(&hb2->lock);
924
925 /* drop_futex_key_refs() must be called outside the spinlocks. */
926 while (--drop_count >= 0)
927 drop_futex_key_refs(&key1);
928
929out:
930 up_read(&current->mm->mmap_sem);
931 return ret;
932}
933
934/*
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700935 * Wake up all waiters hashed on the physical page that is mapped
936 * to this virtual address:
937 */
Ingo Molnare2970f22006-06-27 02:54:47 -0700938static int
939futex_wake_op(u32 __user *uaddr1, u32 __user *uaddr2,
940 int nr_wake, int nr_wake2, int op)
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700941{
942 union futex_key key1, key2;
Ingo Molnare2970f22006-06-27 02:54:47 -0700943 struct futex_hash_bucket *hb1, *hb2;
Pierre Peifferec92d082007-05-09 02:35:00 -0700944 struct plist_head *head;
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700945 struct futex_q *this, *next;
946 int ret, op_ret, attempt = 0;
947
948retryfull:
949 down_read(&current->mm->mmap_sem);
950
951 ret = get_futex_key(uaddr1, &key1);
952 if (unlikely(ret != 0))
953 goto out;
954 ret = get_futex_key(uaddr2, &key2);
955 if (unlikely(ret != 0))
956 goto out;
957
Ingo Molnare2970f22006-06-27 02:54:47 -0700958 hb1 = hash_futex(&key1);
959 hb2 = hash_futex(&key2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700960
961retry:
Ingo Molnar8b8f3192006-07-03 00:25:05 -0700962 double_lock_hb(hb1, hb2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700963
Ingo Molnare2970f22006-06-27 02:54:47 -0700964 op_ret = futex_atomic_op_inuser(op, uaddr2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700965 if (unlikely(op_ret < 0)) {
Ingo Molnare2970f22006-06-27 02:54:47 -0700966 u32 dummy;
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700967
Ingo Molnare2970f22006-06-27 02:54:47 -0700968 spin_unlock(&hb1->lock);
969 if (hb1 != hb2)
970 spin_unlock(&hb2->lock);
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700971
David Howells7ee1dd32006-01-06 00:11:44 -0800972#ifndef CONFIG_MMU
Ingo Molnare2970f22006-06-27 02:54:47 -0700973 /*
974 * we don't get EFAULT from MMU faults if we don't have an MMU,
975 * but we might get them from range checking
976 */
David Howells7ee1dd32006-01-06 00:11:44 -0800977 ret = op_ret;
978 goto out;
979#endif
980
David Gibson796f8d92005-11-07 00:59:33 -0800981 if (unlikely(op_ret != -EFAULT)) {
982 ret = op_ret;
983 goto out;
984 }
985
Ingo Molnare2970f22006-06-27 02:54:47 -0700986 /*
987 * futex_atomic_op_inuser needs to both read and write
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700988 * *(int __user *)uaddr2, but we can't modify it
989 * non-atomically. Therefore, if get_user below is not
990 * enough, we need to handle the fault ourselves, while
Ingo Molnare2970f22006-06-27 02:54:47 -0700991 * still holding the mmap_sem.
992 */
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700993 if (attempt++) {
Ingo Molnarc87e2832006-06-27 02:54:58 -0700994 if (futex_handle_fault((unsigned long)uaddr2,
john stultze579dcb2006-08-13 23:24:24 -0700995 attempt)) {
996 ret = -EFAULT;
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700997 goto out;
john stultze579dcb2006-08-13 23:24:24 -0700998 }
Jakub Jelinek4732efb2005-09-06 15:16:25 -0700999 goto retry;
1000 }
1001
Ingo Molnare2970f22006-06-27 02:54:47 -07001002 /*
1003 * If we would have faulted, release mmap_sem,
1004 * fault it in and start all over again.
1005 */
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001006 up_read(&current->mm->mmap_sem);
1007
Ingo Molnare2970f22006-06-27 02:54:47 -07001008 ret = get_user(dummy, uaddr2);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001009 if (ret)
1010 return ret;
1011
1012 goto retryfull;
1013 }
1014
Ingo Molnare2970f22006-06-27 02:54:47 -07001015 head = &hb1->chain;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001016
Pierre Peifferec92d082007-05-09 02:35:00 -07001017 plist_for_each_entry_safe(this, next, head, list) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001018 if (match_futex (&this->key, &key1)) {
1019 wake_futex(this);
1020 if (++ret >= nr_wake)
1021 break;
1022 }
1023 }
1024
1025 if (op_ret > 0) {
Ingo Molnare2970f22006-06-27 02:54:47 -07001026 head = &hb2->chain;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001027
1028 op_ret = 0;
Pierre Peifferec92d082007-05-09 02:35:00 -07001029 plist_for_each_entry_safe(this, next, head, list) {
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001030 if (match_futex (&this->key, &key2)) {
1031 wake_futex(this);
1032 if (++op_ret >= nr_wake2)
1033 break;
1034 }
1035 }
1036 ret += op_ret;
1037 }
1038
Ingo Molnare2970f22006-06-27 02:54:47 -07001039 spin_unlock(&hb1->lock);
1040 if (hb1 != hb2)
1041 spin_unlock(&hb2->lock);
Jakub Jelinek4732efb2005-09-06 15:16:25 -07001042out:
1043 up_read(&current->mm->mmap_sem);
1044 return ret;
1045}
1046
1047/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 * Requeue all waiters hashed on one physical page to another
1049 * physical page.
1050 */
Ingo Molnare2970f22006-06-27 02:54:47 -07001051static int futex_requeue(u32 __user *uaddr1, u32 __user *uaddr2,
1052 int nr_wake, int nr_requeue, u32 *cmpval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
1054 union futex_key key1, key2;
Ingo Molnare2970f22006-06-27 02:54:47 -07001055 struct futex_hash_bucket *hb1, *hb2;
Pierre Peifferec92d082007-05-09 02:35:00 -07001056 struct plist_head *head1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 struct futex_q *this, *next;
1058 int ret, drop_count = 0;
1059
1060 retry:
1061 down_read(&current->mm->mmap_sem);
1062
1063 ret = get_futex_key(uaddr1, &key1);
1064 if (unlikely(ret != 0))
1065 goto out;
1066 ret = get_futex_key(uaddr2, &key2);
1067 if (unlikely(ret != 0))
1068 goto out;
1069
Ingo Molnare2970f22006-06-27 02:54:47 -07001070 hb1 = hash_futex(&key1);
1071 hb2 = hash_futex(&key2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Ingo Molnar8b8f3192006-07-03 00:25:05 -07001073 double_lock_hb(hb1, hb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Ingo Molnare2970f22006-06-27 02:54:47 -07001075 if (likely(cmpval != NULL)) {
1076 u32 curval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Ingo Molnare2970f22006-06-27 02:54:47 -07001078 ret = get_futex_value_locked(&curval, uaddr1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 if (unlikely(ret)) {
Ingo Molnare2970f22006-06-27 02:54:47 -07001081 spin_unlock(&hb1->lock);
1082 if (hb1 != hb2)
1083 spin_unlock(&hb2->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Ingo Molnare2970f22006-06-27 02:54:47 -07001085 /*
1086 * If we would have faulted, release mmap_sem, fault
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 * it in and start all over again.
1088 */
1089 up_read(&current->mm->mmap_sem);
1090
Ingo Molnare2970f22006-06-27 02:54:47 -07001091 ret = get_user(curval, uaddr1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 if (!ret)
1094 goto retry;
1095
1096 return ret;
1097 }
Ingo Molnare2970f22006-06-27 02:54:47 -07001098 if (curval != *cmpval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 ret = -EAGAIN;
1100 goto out_unlock;
1101 }
1102 }
1103
Ingo Molnare2970f22006-06-27 02:54:47 -07001104 head1 = &hb1->chain;
Pierre Peifferec92d082007-05-09 02:35:00 -07001105 plist_for_each_entry_safe(this, next, head1, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (!match_futex (&this->key, &key1))
1107 continue;
1108 if (++ret <= nr_wake) {
1109 wake_futex(this);
1110 } else {
Sebastien Dugue59e0e0a2006-06-27 02:55:03 -07001111 /*
1112 * If key1 and key2 hash to the same bucket, no need to
1113 * requeue.
1114 */
1115 if (likely(head1 != &hb2->chain)) {
Pierre Peifferec92d082007-05-09 02:35:00 -07001116 plist_del(&this->list, &hb1->chain);
1117 plist_add(&this->list, &hb2->chain);
Sebastien Dugue59e0e0a2006-06-27 02:55:03 -07001118 this->lock_ptr = &hb2->lock;
Pierre Peifferec92d082007-05-09 02:35:00 -07001119#ifdef CONFIG_DEBUG_PI_LIST
1120 this->list.plist.lock = &hb2->lock;
1121#endif
1122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 this->key = key2;
Rusty Russell9adef582007-05-08 00:26:42 -07001124 get_futex_key_refs(&key2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 drop_count++;
1126
1127 if (ret - nr_wake >= nr_requeue)
1128 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
1130 }
1131
1132out_unlock:
Ingo Molnare2970f22006-06-27 02:54:47 -07001133 spin_unlock(&hb1->lock);
1134 if (hb1 != hb2)
1135 spin_unlock(&hb2->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Rusty Russell9adef582007-05-08 00:26:42 -07001137 /* drop_futex_key_refs() must be called outside the spinlocks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 while (--drop_count >= 0)
Rusty Russell9adef582007-05-08 00:26:42 -07001139 drop_futex_key_refs(&key1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141out:
1142 up_read(&current->mm->mmap_sem);
1143 return ret;
1144}
1145
1146/* The key must be already stored in q->key. */
1147static inline struct futex_hash_bucket *
1148queue_lock(struct futex_q *q, int fd, struct file *filp)
1149{
Ingo Molnare2970f22006-06-27 02:54:47 -07001150 struct futex_hash_bucket *hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 q->fd = fd;
1153 q->filp = filp;
1154
1155 init_waitqueue_head(&q->waiters);
1156
Rusty Russell9adef582007-05-08 00:26:42 -07001157 get_futex_key_refs(&q->key);
Ingo Molnare2970f22006-06-27 02:54:47 -07001158 hb = hash_futex(&q->key);
1159 q->lock_ptr = &hb->lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Ingo Molnare2970f22006-06-27 02:54:47 -07001161 spin_lock(&hb->lock);
1162 return hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
1164
Ingo Molnare2970f22006-06-27 02:54:47 -07001165static inline void __queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
Pierre Peifferec92d082007-05-09 02:35:00 -07001167 int prio;
1168
1169 /*
1170 * The priority used to register this element is
1171 * - either the real thread-priority for the real-time threads
1172 * (i.e. threads with a priority lower than MAX_RT_PRIO)
1173 * - or MAX_RT_PRIO for non-RT threads.
1174 * Thus, all RT-threads are woken first in priority order, and
1175 * the others are woken last, in FIFO order.
1176 */
1177 prio = min(current->normal_prio, MAX_RT_PRIO);
1178
1179 plist_node_init(&q->list, prio);
1180#ifdef CONFIG_DEBUG_PI_LIST
1181 q->list.plist.lock = &hb->lock;
1182#endif
1183 plist_add(&q->list, &hb->chain);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001184 q->task = current;
Ingo Molnare2970f22006-06-27 02:54:47 -07001185 spin_unlock(&hb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186}
1187
1188static inline void
Ingo Molnare2970f22006-06-27 02:54:47 -07001189queue_unlock(struct futex_q *q, struct futex_hash_bucket *hb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
Ingo Molnare2970f22006-06-27 02:54:47 -07001191 spin_unlock(&hb->lock);
Rusty Russell9adef582007-05-08 00:26:42 -07001192 drop_futex_key_refs(&q->key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193}
1194
1195/*
1196 * queue_me and unqueue_me must be called as a pair, each
1197 * exactly once. They are called with the hashed spinlock held.
1198 */
1199
1200/* The key must be already stored in q->key. */
1201static void queue_me(struct futex_q *q, int fd, struct file *filp)
1202{
Ingo Molnare2970f22006-06-27 02:54:47 -07001203 struct futex_hash_bucket *hb;
1204
1205 hb = queue_lock(q, fd, filp);
1206 __queue_me(q, hb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
1209/* Return 1 if we were still queued (ie. 0 means we were woken) */
1210static int unqueue_me(struct futex_q *q)
1211{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 spinlock_t *lock_ptr;
Ingo Molnare2970f22006-06-27 02:54:47 -07001213 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 /* In the common case we don't take the spinlock, which is nice. */
1216 retry:
1217 lock_ptr = q->lock_ptr;
Christian Borntraegere91467e2006-08-05 12:13:52 -07001218 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 if (lock_ptr != 0) {
1220 spin_lock(lock_ptr);
1221 /*
1222 * q->lock_ptr can change between reading it and
1223 * spin_lock(), causing us to take the wrong lock. This
1224 * corrects the race condition.
1225 *
1226 * Reasoning goes like this: if we have the wrong lock,
1227 * q->lock_ptr must have changed (maybe several times)
1228 * between reading it and the spin_lock(). It can
1229 * change again after the spin_lock() but only if it was
1230 * already changed before the spin_lock(). It cannot,
1231 * however, change back to the original value. Therefore
1232 * we can detect whether we acquired the correct lock.
1233 */
1234 if (unlikely(lock_ptr != q->lock_ptr)) {
1235 spin_unlock(lock_ptr);
1236 goto retry;
1237 }
Pierre Peifferec92d082007-05-09 02:35:00 -07001238 WARN_ON(plist_node_empty(&q->list));
1239 plist_del(&q->list, &q->list.plist);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001240
1241 BUG_ON(q->pi_state);
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 spin_unlock(lock_ptr);
1244 ret = 1;
1245 }
1246
Rusty Russell9adef582007-05-08 00:26:42 -07001247 drop_futex_key_refs(&q->key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 return ret;
1249}
1250
Ingo Molnarc87e2832006-06-27 02:54:58 -07001251/*
1252 * PI futexes can not be requeued and must remove themself from the
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001253 * hash bucket. The hash bucket lock (i.e. lock_ptr) is held on entry
1254 * and dropped here.
Ingo Molnarc87e2832006-06-27 02:54:58 -07001255 */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001256static void unqueue_me_pi(struct futex_q *q)
Ingo Molnarc87e2832006-06-27 02:54:58 -07001257{
Pierre Peifferec92d082007-05-09 02:35:00 -07001258 WARN_ON(plist_node_empty(&q->list));
1259 plist_del(&q->list, &q->list.plist);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001260
1261 BUG_ON(!q->pi_state);
1262 free_pi_state(q->pi_state);
1263 q->pi_state = NULL;
1264
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001265 spin_unlock(q->lock_ptr);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001266
Rusty Russell9adef582007-05-08 00:26:42 -07001267 drop_futex_key_refs(&q->key);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001268}
1269
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001270/*
1271 * Fixup the pi_state owner with current.
1272 *
1273 * The cur->mm semaphore must be held, it is released at return of this
1274 * function.
1275 */
1276static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
1277 struct futex_hash_bucket *hb,
1278 struct task_struct *curr)
1279{
1280 u32 newtid = curr->pid | FUTEX_WAITERS;
1281 struct futex_pi_state *pi_state = q->pi_state;
1282 u32 uval, curval, newval;
1283 int ret;
1284
1285 /* Owner died? */
1286 if (pi_state->owner != NULL) {
1287 spin_lock_irq(&pi_state->owner->pi_lock);
1288 WARN_ON(list_empty(&pi_state->list));
1289 list_del_init(&pi_state->list);
1290 spin_unlock_irq(&pi_state->owner->pi_lock);
1291 } else
1292 newtid |= FUTEX_OWNER_DIED;
1293
1294 pi_state->owner = curr;
1295
1296 spin_lock_irq(&curr->pi_lock);
1297 WARN_ON(!list_empty(&pi_state->list));
1298 list_add(&pi_state->list, &curr->pi_state_list);
1299 spin_unlock_irq(&curr->pi_lock);
1300
1301 /* Unqueue and drop the lock */
1302 unqueue_me_pi(q);
1303 up_read(&curr->mm->mmap_sem);
1304 /*
1305 * We own it, so we have to replace the pending owner
1306 * TID. This must be atomic as we have preserve the
1307 * owner died bit here.
1308 */
1309 ret = get_user(uval, uaddr);
1310 while (!ret) {
1311 newval = (uval & FUTEX_OWNER_DIED) | newtid;
1312 newval |= (uval & FUTEX_WAITER_REQUEUED);
1313 curval = futex_atomic_cmpxchg_inatomic(uaddr,
1314 uval, newval);
1315 if (curval == -EFAULT)
1316 ret = -EFAULT;
1317 if (curval == uval)
1318 break;
1319 uval = curval;
1320 }
1321 return ret;
1322}
1323
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001324static long futex_wait_restart(struct restart_block *restart);
Pierre Peifferc19384b2007-05-09 02:35:02 -07001325static int futex_wait(u32 __user *uaddr, u32 val, ktime_t *abs_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
Ingo Molnarc87e2832006-06-27 02:54:58 -07001327 struct task_struct *curr = current;
1328 DECLARE_WAITQUEUE(wait, curr);
Ingo Molnare2970f22006-06-27 02:54:47 -07001329 struct futex_hash_bucket *hb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 struct futex_q q;
Ingo Molnare2970f22006-06-27 02:54:47 -07001331 u32 uval;
1332 int ret;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001333 struct hrtimer_sleeper t, *to = NULL;
Pierre Peifferc19384b2007-05-09 02:35:02 -07001334 int rem = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Ingo Molnarc87e2832006-06-27 02:54:58 -07001336 q.pi_state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 retry:
Ingo Molnarc87e2832006-06-27 02:54:58 -07001338 down_read(&curr->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 ret = get_futex_key(uaddr, &q.key);
1341 if (unlikely(ret != 0))
1342 goto out_release_sem;
1343
Ingo Molnare2970f22006-06-27 02:54:47 -07001344 hb = queue_lock(&q, -1, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 /*
1347 * Access the page AFTER the futex is queued.
1348 * Order is important:
1349 *
1350 * Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val);
1351 * Userspace waker: if (cond(var)) { var = new; futex_wake(&var); }
1352 *
1353 * The basic logical guarantee of a futex is that it blocks ONLY
1354 * if cond(var) is known to be true at the time of blocking, for
1355 * any cond. If we queued after testing *uaddr, that would open
1356 * a race condition where we could block indefinitely with
1357 * cond(var) false, which would violate the guarantee.
1358 *
1359 * A consequence is that futex_wait() can return zero and absorb
1360 * a wakeup when *uaddr != val on entry to the syscall. This is
1361 * rare, but normal.
1362 *
1363 * We hold the mmap semaphore, so the mapping cannot have changed
1364 * since we looked it up in get_futex_key.
1365 */
Ingo Molnare2970f22006-06-27 02:54:47 -07001366 ret = get_futex_value_locked(&uval, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 if (unlikely(ret)) {
Ingo Molnare2970f22006-06-27 02:54:47 -07001369 queue_unlock(&q, hb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Ingo Molnare2970f22006-06-27 02:54:47 -07001371 /*
1372 * If we would have faulted, release mmap_sem, fault it in and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 * start all over again.
1374 */
Ingo Molnarc87e2832006-06-27 02:54:58 -07001375 up_read(&curr->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Ingo Molnare2970f22006-06-27 02:54:47 -07001377 ret = get_user(uval, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 if (!ret)
1380 goto retry;
1381 return ret;
1382 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001383 ret = -EWOULDBLOCK;
1384 if (uval != val)
1385 goto out_unlock_release_sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001387 /*
1388 * This rt_mutex_waiter structure is prepared here and will
1389 * be used only if this task is requeued from a normal futex to
1390 * a PI-futex with futex_requeue_pi.
1391 */
1392 debug_rt_mutex_init_waiter(&q.waiter);
1393 q.waiter.task = NULL;
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 /* Only actually queue if *uaddr contained val. */
Ingo Molnare2970f22006-06-27 02:54:47 -07001396 __queue_me(&q, hb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 /*
1399 * Now the futex is queued and we have checked the data, we
1400 * don't want to hold mmap_sem while we sleep.
Ingo Molnarc87e2832006-06-27 02:54:58 -07001401 */
1402 up_read(&curr->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 /*
1405 * There might have been scheduling since the queue_me(), as we
1406 * cannot hold a spinlock across the get_user() in case it
1407 * faults, and we cannot just set TASK_INTERRUPTIBLE state when
1408 * queueing ourselves into the futex hash. This code thus has to
1409 * rely on the futex_wake() code removing us from hash when it
1410 * wakes us up.
1411 */
1412
1413 /* add_wait_queue is the barrier after __set_current_state. */
1414 __set_current_state(TASK_INTERRUPTIBLE);
1415 add_wait_queue(&q.waiters, &wait);
1416 /*
Pierre Peifferec92d082007-05-09 02:35:00 -07001417 * !plist_node_empty() is safe here without any lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 * q.lock_ptr != 0 is not safe, because of ordering against wakeup.
1419 */
Pierre Peifferec92d082007-05-09 02:35:00 -07001420 if (likely(!plist_node_empty(&q.list))) {
Pierre Peifferc19384b2007-05-09 02:35:02 -07001421 if (!abs_time)
1422 schedule();
1423 else {
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001424 to = &t;
Pierre Peifferc19384b2007-05-09 02:35:02 -07001425 hrtimer_init(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1426 hrtimer_init_sleeper(&t, current);
1427 t.timer.expires = *abs_time;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001428
Pierre Peifferc19384b2007-05-09 02:35:02 -07001429 hrtimer_start(&t.timer, t.timer.expires, HRTIMER_MODE_ABS);
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001430
Pierre Peifferc19384b2007-05-09 02:35:02 -07001431 /*
1432 * the timer could have already expired, in which
1433 * case current would be flagged for rescheduling.
1434 * Don't bother calling schedule.
1435 */
1436 if (likely(t.task))
1437 schedule();
1438
1439 hrtimer_cancel(&t.timer);
1440
1441 /* Flag if a timeout occured */
1442 rem = (t.task == NULL);
1443 }
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 __set_current_state(TASK_RUNNING);
1446
1447 /*
1448 * NOTE: we don't remove ourselves from the waitqueue because
1449 * we are the only user of it.
1450 */
1451
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001452 if (q.pi_state) {
1453 /*
1454 * We were woken but have been requeued on a PI-futex.
1455 * We have to complete the lock acquisition by taking
1456 * the rtmutex.
1457 */
1458
1459 struct rt_mutex *lock = &q.pi_state->pi_mutex;
1460
1461 spin_lock(&lock->wait_lock);
1462 if (unlikely(q.waiter.task)) {
1463 remove_waiter(lock, &q.waiter);
1464 }
1465 spin_unlock(&lock->wait_lock);
1466
1467 if (rem)
1468 ret = -ETIMEDOUT;
1469 else
1470 ret = rt_mutex_timed_lock(lock, to, 1);
1471
1472 down_read(&curr->mm->mmap_sem);
1473 spin_lock(q.lock_ptr);
1474
1475 /*
1476 * Got the lock. We might not be the anticipated owner if we
1477 * did a lock-steal - fix up the PI-state in that case.
1478 */
1479 if (!ret && q.pi_state->owner != curr) {
1480 /*
1481 * We MUST play with the futex we were requeued on,
1482 * NOT the current futex.
1483 * We can retrieve it from the key of the pi_state
1484 */
1485 uaddr = q.pi_state->key.uaddr;
1486
1487 /* mmap_sem and hash_bucket lock are unlocked at
1488 return of this function */
1489 ret = fixup_pi_state_owner(uaddr, &q, hb, curr);
1490 } else {
1491 /*
1492 * Catch the rare case, where the lock was released
1493 * when we were on the way back before we locked
1494 * the hash bucket.
1495 */
1496 if (ret && q.pi_state->owner == curr) {
1497 if (rt_mutex_trylock(&q.pi_state->pi_mutex))
1498 ret = 0;
1499 }
1500 /* Unqueue and drop the lock */
1501 unqueue_me_pi(&q);
1502 up_read(&curr->mm->mmap_sem);
1503 }
1504
1505 debug_rt_mutex_free_waiter(&q.waiter);
1506
1507 return ret;
1508 }
1509
1510 debug_rt_mutex_free_waiter(&q.waiter);
1511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 /* If we were woken (and unqueued), we succeeded, whatever. */
1513 if (!unqueue_me(&q))
1514 return 0;
Pierre Peifferc19384b2007-05-09 02:35:02 -07001515 if (rem)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 return -ETIMEDOUT;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001517
Ingo Molnare2970f22006-06-27 02:54:47 -07001518 /*
1519 * We expect signal_pending(current), but another thread may
1520 * have handled it for us already.
1521 */
Pierre Peifferc19384b2007-05-09 02:35:02 -07001522 if (!abs_time)
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001523 return -ERESTARTSYS;
1524 else {
1525 struct restart_block *restart;
1526 restart = &current_thread_info()->restart_block;
1527 restart->fn = futex_wait_restart;
1528 restart->arg0 = (unsigned long)uaddr;
1529 restart->arg1 = (unsigned long)val;
Pierre Peifferc19384b2007-05-09 02:35:02 -07001530 restart->arg2 = (unsigned long)abs_time;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001531 return -ERESTART_RESTARTBLOCK;
1532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Ingo Molnarc87e2832006-06-27 02:54:58 -07001534 out_unlock_release_sem:
1535 queue_unlock(&q, hb);
1536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 out_release_sem:
Ingo Molnarc87e2832006-06-27 02:54:58 -07001538 up_read(&curr->mm->mmap_sem);
1539 return ret;
1540}
1541
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001542
1543static long futex_wait_restart(struct restart_block *restart)
1544{
1545 u32 __user *uaddr = (u32 __user *)restart->arg0;
1546 u32 val = (u32)restart->arg1;
Pierre Peifferc19384b2007-05-09 02:35:02 -07001547 ktime_t *abs_time = (ktime_t *)restart->arg2;
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001548
1549 restart->fn = do_no_restart_syscall;
Pierre Peifferc19384b2007-05-09 02:35:02 -07001550 return (long)futex_wait(uaddr, val, abs_time);
Nick Piggin72c1bbf2007-05-08 00:26:43 -07001551}
1552
1553
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001554static void set_pi_futex_owner(struct futex_hash_bucket *hb,
1555 union futex_key *key, struct task_struct *p)
1556{
1557 struct plist_head *head;
1558 struct futex_q *this, *next;
1559 struct futex_pi_state *pi_state = NULL;
1560 struct rt_mutex *lock;
1561
1562 /* Search a waiter that should already exists */
1563
1564 head = &hb->chain;
1565
1566 plist_for_each_entry_safe(this, next, head, list) {
1567 if (match_futex (&this->key, key)) {
1568 pi_state = this->pi_state;
1569 break;
1570 }
1571 }
1572
1573 BUG_ON(!pi_state);
1574
1575 /* set p as pi_state's owner */
1576 lock = &pi_state->pi_mutex;
1577
1578 spin_lock(&lock->wait_lock);
1579 spin_lock_irq(&p->pi_lock);
1580
1581 list_add(&pi_state->list, &p->pi_state_list);
1582 pi_state->owner = p;
1583
1584
1585 /* set p as pi_mutex's owner */
1586 debug_rt_mutex_proxy_lock(lock, p);
1587 WARN_ON(rt_mutex_owner(lock));
1588 rt_mutex_set_owner(lock, p, 0);
1589 rt_mutex_deadlock_account_lock(lock, p);
1590
1591 plist_add(&rt_mutex_top_waiter(lock)->pi_list_entry,
1592 &p->pi_waiters);
1593 __rt_mutex_adjust_prio(p);
1594
1595 spin_unlock_irq(&p->pi_lock);
1596 spin_unlock(&lock->wait_lock);
1597}
1598
Ingo Molnarc87e2832006-06-27 02:54:58 -07001599/*
1600 * Userspace tried a 0 -> TID atomic transition of the futex value
1601 * and failed. The kernel side here does the whole locking operation:
1602 * if there are waiters then it will block, it does PI, etc. (Due to
1603 * races the kernel might see a 0 value of the futex too.)
1604 */
Pierre Peifferc19384b2007-05-09 02:35:02 -07001605static int futex_lock_pi(u32 __user *uaddr, int detect, ktime_t *time,
1606 int trylock)
Ingo Molnarc87e2832006-06-27 02:54:58 -07001607{
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07001608 struct hrtimer_sleeper timeout, *to = NULL;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001609 struct task_struct *curr = current;
1610 struct futex_hash_bucket *hb;
1611 u32 uval, newval, curval;
1612 struct futex_q q;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001613 int ret, lock_held, attempt = 0;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001614
1615 if (refill_pi_state_cache())
1616 return -ENOMEM;
1617
Pierre Peifferc19384b2007-05-09 02:35:02 -07001618 if (time) {
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07001619 to = &timeout;
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001620 hrtimer_init(&to->timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07001621 hrtimer_init_sleeper(to, current);
Pierre Peifferc19384b2007-05-09 02:35:02 -07001622 to->timer.expires = *time;
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07001623 }
1624
Ingo Molnarc87e2832006-06-27 02:54:58 -07001625 q.pi_state = NULL;
1626 retry:
1627 down_read(&curr->mm->mmap_sem);
1628
1629 ret = get_futex_key(uaddr, &q.key);
1630 if (unlikely(ret != 0))
1631 goto out_release_sem;
1632
1633 hb = queue_lock(&q, -1, NULL);
1634
1635 retry_locked:
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001636 lock_held = 0;
1637
Ingo Molnarc87e2832006-06-27 02:54:58 -07001638 /*
1639 * To avoid races, we attempt to take the lock here again
1640 * (by doing a 0 -> TID atomic cmpxchg), while holding all
1641 * the locks. It will most likely not succeed.
1642 */
1643 newval = current->pid;
1644
Peter Zijlstraa8663742006-12-06 20:32:20 -08001645 pagefault_disable();
Ingo Molnarc87e2832006-06-27 02:54:58 -07001646 curval = futex_atomic_cmpxchg_inatomic(uaddr, 0, newval);
Peter Zijlstraa8663742006-12-06 20:32:20 -08001647 pagefault_enable();
Ingo Molnarc87e2832006-06-27 02:54:58 -07001648
1649 if (unlikely(curval == -EFAULT))
1650 goto uaddr_faulted;
1651
1652 /* We own the lock already */
1653 if (unlikely((curval & FUTEX_TID_MASK) == current->pid)) {
1654 if (!detect && 0)
1655 force_sig(SIGKILL, current);
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001656 /*
1657 * Normally, this check is done in user space.
1658 * In case of requeue, the owner may attempt to lock this futex,
1659 * even if the ownership has already been given by the previous
1660 * waker.
1661 * In the usual case, this is a case of deadlock, but not in case
1662 * of REQUEUE_PI.
1663 */
1664 if (!(curval & FUTEX_WAITER_REQUEUED))
1665 ret = -EDEADLK;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001666 goto out_unlock_release_sem;
1667 }
1668
1669 /*
1670 * Surprise - we got the lock. Just return
1671 * to userspace:
1672 */
1673 if (unlikely(!curval))
1674 goto out_unlock_release_sem;
1675
1676 uval = curval;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001677 /*
1678 * In case of a requeue, check if there already is an owner
1679 * If not, just take the futex.
1680 */
1681 if ((curval & FUTEX_WAITER_REQUEUED) && !(curval & FUTEX_TID_MASK)) {
1682 /* set current as futex owner */
1683 newval = curval | current->pid;
1684 lock_held = 1;
1685 } else
1686 /* Set the WAITERS flag, so the owner will know it has someone
1687 to wake at next unlock */
1688 newval = curval | FUTEX_WAITERS;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001689
Peter Zijlstraa8663742006-12-06 20:32:20 -08001690 pagefault_disable();
Ingo Molnarc87e2832006-06-27 02:54:58 -07001691 curval = futex_atomic_cmpxchg_inatomic(uaddr, uval, newval);
Peter Zijlstraa8663742006-12-06 20:32:20 -08001692 pagefault_enable();
Ingo Molnarc87e2832006-06-27 02:54:58 -07001693
1694 if (unlikely(curval == -EFAULT))
1695 goto uaddr_faulted;
1696 if (unlikely(curval != uval))
1697 goto retry_locked;
1698
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001699 if (lock_held) {
1700 set_pi_futex_owner(hb, &q.key, curr);
1701 goto out_unlock_release_sem;
1702 }
1703
Ingo Molnarc87e2832006-06-27 02:54:58 -07001704 /*
1705 * We dont have the lock. Look up the PI state (or create it if
1706 * we are the first waiter):
1707 */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001708 ret = lookup_pi_state(uval, hb, &q.key, &q.pi_state);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001709
1710 if (unlikely(ret)) {
1711 /*
1712 * There were no waiters and the owner task lookup
1713 * failed. When the OWNER_DIED bit is set, then we
1714 * know that this is a robust futex and we actually
1715 * take the lock. This is safe as we are protected by
1716 * the hash bucket lock. We also set the waiters bit
1717 * unconditionally here, to simplify glibc handling of
1718 * multiple tasks racing to acquire the lock and
1719 * cleanup the problems which were left by the dead
1720 * owner.
1721 */
1722 if (curval & FUTEX_OWNER_DIED) {
1723 uval = newval;
1724 newval = current->pid |
1725 FUTEX_OWNER_DIED | FUTEX_WAITERS;
1726
Peter Zijlstraa8663742006-12-06 20:32:20 -08001727 pagefault_disable();
Ingo Molnarc87e2832006-06-27 02:54:58 -07001728 curval = futex_atomic_cmpxchg_inatomic(uaddr,
1729 uval, newval);
Peter Zijlstraa8663742006-12-06 20:32:20 -08001730 pagefault_enable();
Ingo Molnarc87e2832006-06-27 02:54:58 -07001731
1732 if (unlikely(curval == -EFAULT))
1733 goto uaddr_faulted;
1734 if (unlikely(curval != uval))
1735 goto retry_locked;
1736 ret = 0;
1737 }
1738 goto out_unlock_release_sem;
1739 }
1740
1741 /*
1742 * Only actually queue now that the atomic ops are done:
1743 */
1744 __queue_me(&q, hb);
1745
1746 /*
1747 * Now the futex is queued and we have checked the data, we
1748 * don't want to hold mmap_sem while we sleep.
1749 */
1750 up_read(&curr->mm->mmap_sem);
1751
1752 WARN_ON(!q.pi_state);
1753 /*
1754 * Block on the PI mutex:
1755 */
1756 if (!trylock)
1757 ret = rt_mutex_timed_lock(&q.pi_state->pi_mutex, to, 1);
1758 else {
1759 ret = rt_mutex_trylock(&q.pi_state->pi_mutex);
1760 /* Fixup the trylock return value: */
1761 ret = ret ? 0 : -EWOULDBLOCK;
1762 }
1763
1764 down_read(&curr->mm->mmap_sem);
Vernon Mauerya99e4e42006-07-01 04:35:42 -07001765 spin_lock(q.lock_ptr);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001766
1767 /*
1768 * Got the lock. We might not be the anticipated owner if we
1769 * did a lock-steal - fix up the PI-state in that case.
1770 */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001771 if (!ret && q.pi_state->owner != curr)
1772 /* mmap_sem is unlocked at return of this function */
1773 ret = fixup_pi_state_owner(uaddr, &q, hb, curr);
1774 else {
Ingo Molnarc87e2832006-06-27 02:54:58 -07001775 /*
1776 * Catch the rare case, where the lock was released
1777 * when we were on the way back before we locked
1778 * the hash bucket.
1779 */
1780 if (ret && q.pi_state->owner == curr) {
1781 if (rt_mutex_trylock(&q.pi_state->pi_mutex))
1782 ret = 0;
1783 }
1784 /* Unqueue and drop the lock */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07001785 unqueue_me_pi(&q);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001786 up_read(&curr->mm->mmap_sem);
1787 }
1788
1789 if (!detect && ret == -EDEADLK && 0)
1790 force_sig(SIGKILL, current);
1791
Thomas Gleixnerc5780e92006-09-08 09:47:15 -07001792 return ret != -EINTR ? ret : -ERESTARTNOINTR;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001793
1794 out_unlock_release_sem:
1795 queue_unlock(&q, hb);
1796
1797 out_release_sem:
1798 up_read(&curr->mm->mmap_sem);
1799 return ret;
1800
1801 uaddr_faulted:
1802 /*
1803 * We have to r/w *(int __user *)uaddr, but we can't modify it
1804 * non-atomically. Therefore, if get_user below is not
1805 * enough, we need to handle the fault ourselves, while
1806 * still holding the mmap_sem.
1807 */
1808 if (attempt++) {
john stultze579dcb2006-08-13 23:24:24 -07001809 if (futex_handle_fault((unsigned long)uaddr, attempt)) {
1810 ret = -EFAULT;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001811 goto out_unlock_release_sem;
john stultze579dcb2006-08-13 23:24:24 -07001812 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001813 goto retry_locked;
1814 }
1815
1816 queue_unlock(&q, hb);
1817 up_read(&curr->mm->mmap_sem);
1818
1819 ret = get_user(uval, uaddr);
1820 if (!ret && (uval != -EFAULT))
1821 goto retry;
1822
1823 return ret;
1824}
1825
1826/*
Ingo Molnarc87e2832006-06-27 02:54:58 -07001827 * Userspace attempted a TID -> 0 atomic transition, and failed.
1828 * This is the in-kernel slowpath: we look up the PI state (if any),
1829 * and do the rt-mutex unlock.
1830 */
1831static int futex_unlock_pi(u32 __user *uaddr)
1832{
1833 struct futex_hash_bucket *hb;
1834 struct futex_q *this, *next;
1835 u32 uval;
Pierre Peifferec92d082007-05-09 02:35:00 -07001836 struct plist_head *head;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001837 union futex_key key;
1838 int ret, attempt = 0;
1839
1840retry:
1841 if (get_user(uval, uaddr))
1842 return -EFAULT;
1843 /*
1844 * We release only a lock we actually own:
1845 */
1846 if ((uval & FUTEX_TID_MASK) != current->pid)
1847 return -EPERM;
1848 /*
1849 * First take all the futex related locks:
1850 */
1851 down_read(&current->mm->mmap_sem);
1852
1853 ret = get_futex_key(uaddr, &key);
1854 if (unlikely(ret != 0))
1855 goto out;
1856
1857 hb = hash_futex(&key);
1858 spin_lock(&hb->lock);
1859
1860retry_locked:
1861 /*
1862 * To avoid races, try to do the TID -> 0 atomic transition
1863 * again. If it succeeds then we can return without waking
1864 * anyone else up:
1865 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001866 if (!(uval & FUTEX_OWNER_DIED)) {
Peter Zijlstraa8663742006-12-06 20:32:20 -08001867 pagefault_disable();
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001868 uval = futex_atomic_cmpxchg_inatomic(uaddr, current->pid, 0);
Peter Zijlstraa8663742006-12-06 20:32:20 -08001869 pagefault_enable();
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001870 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001871
1872 if (unlikely(uval == -EFAULT))
1873 goto pi_faulted;
1874 /*
1875 * Rare case: we managed to release the lock atomically,
1876 * no need to wake anyone else up:
1877 */
1878 if (unlikely(uval == current->pid))
1879 goto out_unlock;
1880
1881 /*
1882 * Ok, other tasks may need to be woken up - check waiters
1883 * and do the wakeup if necessary:
1884 */
1885 head = &hb->chain;
1886
Pierre Peifferec92d082007-05-09 02:35:00 -07001887 plist_for_each_entry_safe(this, next, head, list) {
Ingo Molnarc87e2832006-06-27 02:54:58 -07001888 if (!match_futex (&this->key, &key))
1889 continue;
1890 ret = wake_futex_pi(uaddr, uval, this);
1891 /*
1892 * The atomic access to the futex value
1893 * generated a pagefault, so retry the
1894 * user-access and the wakeup:
1895 */
1896 if (ret == -EFAULT)
1897 goto pi_faulted;
1898 goto out_unlock;
1899 }
1900 /*
1901 * No waiters - kernel unlocks the futex:
1902 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02001903 if (!(uval & FUTEX_OWNER_DIED)) {
1904 ret = unlock_futex_pi(uaddr, uval);
1905 if (ret == -EFAULT)
1906 goto pi_faulted;
1907 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001908
1909out_unlock:
1910 spin_unlock(&hb->lock);
1911out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 up_read(&current->mm->mmap_sem);
Ingo Molnarc87e2832006-06-27 02:54:58 -07001913
1914 return ret;
1915
1916pi_faulted:
1917 /*
1918 * We have to r/w *(int __user *)uaddr, but we can't modify it
1919 * non-atomically. Therefore, if get_user below is not
1920 * enough, we need to handle the fault ourselves, while
1921 * still holding the mmap_sem.
1922 */
1923 if (attempt++) {
john stultze579dcb2006-08-13 23:24:24 -07001924 if (futex_handle_fault((unsigned long)uaddr, attempt)) {
1925 ret = -EFAULT;
Ingo Molnarc87e2832006-06-27 02:54:58 -07001926 goto out_unlock;
john stultze579dcb2006-08-13 23:24:24 -07001927 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07001928 goto retry_locked;
1929 }
1930
1931 spin_unlock(&hb->lock);
1932 up_read(&current->mm->mmap_sem);
1933
1934 ret = get_user(uval, uaddr);
1935 if (!ret && (uval != -EFAULT))
1936 goto retry;
1937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 return ret;
1939}
1940
1941static int futex_close(struct inode *inode, struct file *filp)
1942{
1943 struct futex_q *q = filp->private_data;
1944
1945 unqueue_me(q);
1946 kfree(q);
Ingo Molnare2970f22006-06-27 02:54:47 -07001947
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 return 0;
1949}
1950
1951/* This is one-shot: once it's gone off you need a new fd */
1952static unsigned int futex_poll(struct file *filp,
1953 struct poll_table_struct *wait)
1954{
1955 struct futex_q *q = filp->private_data;
1956 int ret = 0;
1957
1958 poll_wait(filp, &q->waiters, wait);
1959
1960 /*
Pierre Peifferec92d082007-05-09 02:35:00 -07001961 * plist_node_empty() is safe here without any lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 * q->lock_ptr != 0 is not safe, because of ordering against wakeup.
1963 */
Pierre Peifferec92d082007-05-09 02:35:00 -07001964 if (plist_node_empty(&q->list))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 ret = POLLIN | POLLRDNORM;
1966
1967 return ret;
1968}
1969
Helge Deller15ad7cd2006-12-06 20:40:36 -08001970static const struct file_operations futex_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 .release = futex_close,
1972 .poll = futex_poll,
1973};
1974
1975/*
1976 * Signal allows caller to avoid the race which would occur if they
1977 * set the sigio stuff up afterwards.
1978 */
Ingo Molnare2970f22006-06-27 02:54:47 -07001979static int futex_fd(u32 __user *uaddr, int signal)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
1981 struct futex_q *q;
1982 struct file *filp;
1983 int ret, err;
Andrew Morton19c6b6e2006-11-02 22:07:17 -08001984 static unsigned long printk_interval;
1985
1986 if (printk_timed_ratelimit(&printk_interval, 60 * 60 * 1000)) {
1987 printk(KERN_WARNING "Process `%s' used FUTEX_FD, which "
1988 "will be removed from the kernel in June 2007\n",
1989 current->comm);
1990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
1992 ret = -EINVAL;
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001993 if (!valid_signal(signal))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 goto out;
1995
1996 ret = get_unused_fd();
1997 if (ret < 0)
1998 goto out;
1999 filp = get_empty_filp();
2000 if (!filp) {
2001 put_unused_fd(ret);
2002 ret = -ENFILE;
2003 goto out;
2004 }
2005 filp->f_op = &futex_fops;
Josef "Jeff" Sipekf3a43f32006-12-08 02:36:43 -08002006 filp->f_path.mnt = mntget(futex_mnt);
2007 filp->f_path.dentry = dget(futex_mnt->mnt_root);
2008 filp->f_mapping = filp->f_path.dentry->d_inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010 if (signal) {
Eric W. Biederman609d7fa2006-10-02 02:17:15 -07002011 err = __f_setown(filp, task_pid(current), PIDTYPE_PID, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 if (err < 0) {
Pekka Enberg39ed3fd2005-09-06 15:17:44 -07002013 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 }
2015 filp->f_owner.signum = signal;
2016 }
2017
2018 q = kmalloc(sizeof(*q), GFP_KERNEL);
2019 if (!q) {
Pekka Enberg39ed3fd2005-09-06 15:17:44 -07002020 err = -ENOMEM;
2021 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 }
Ingo Molnarc87e2832006-06-27 02:54:58 -07002023 q->pi_state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
2025 down_read(&current->mm->mmap_sem);
2026 err = get_futex_key(uaddr, &q->key);
2027
2028 if (unlikely(err != 0)) {
2029 up_read(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 kfree(q);
Pekka Enberg39ed3fd2005-09-06 15:17:44 -07002031 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 }
2033
2034 /*
2035 * queue_me() must be called before releasing mmap_sem, because
2036 * key->shared.inode needs to be referenced while holding it.
2037 */
2038 filp->private_data = q;
2039
2040 queue_me(q, ret, filp);
2041 up_read(&current->mm->mmap_sem);
2042
2043 /* Now we map fd to filp, so userspace can access it */
2044 fd_install(ret, filp);
2045out:
2046 return ret;
Pekka Enberg39ed3fd2005-09-06 15:17:44 -07002047error:
2048 put_unused_fd(ret);
2049 put_filp(filp);
2050 ret = err;
2051 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052}
2053
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002054/*
2055 * Support for robust futexes: the kernel cleans up held futexes at
2056 * thread exit time.
2057 *
2058 * Implementation: user-space maintains a per-thread list of locks it
2059 * is holding. Upon do_exit(), the kernel carefully walks this list,
2060 * and marks all locks that are owned by this thread with the
Ingo Molnarc87e2832006-06-27 02:54:58 -07002061 * FUTEX_OWNER_DIED bit, and wakes up a waiter (if any). The list is
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002062 * always manipulated with the lock held, so the list is private and
2063 * per-thread. Userspace also maintains a per-thread 'list_op_pending'
2064 * field, to allow the kernel to clean up if the thread dies after
2065 * acquiring the lock, but just before it could have added itself to
2066 * the list. There can only be one such pending lock.
2067 */
2068
2069/**
2070 * sys_set_robust_list - set the robust-futex list head of a task
2071 * @head: pointer to the list-head
2072 * @len: length of the list-head, as userspace expects
2073 */
2074asmlinkage long
2075sys_set_robust_list(struct robust_list_head __user *head,
2076 size_t len)
2077{
2078 /*
2079 * The kernel knows only one size for now:
2080 */
2081 if (unlikely(len != sizeof(*head)))
2082 return -EINVAL;
2083
2084 current->robust_list = head;
2085
2086 return 0;
2087}
2088
2089/**
2090 * sys_get_robust_list - get the robust-futex list head of a task
2091 * @pid: pid of the process [zero for current task]
2092 * @head_ptr: pointer to a list-head pointer, the kernel fills it in
2093 * @len_ptr: pointer to a length field, the kernel fills in the header size
2094 */
2095asmlinkage long
Al Viroba46df92006-10-10 22:46:07 +01002096sys_get_robust_list(int pid, struct robust_list_head __user * __user *head_ptr,
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002097 size_t __user *len_ptr)
2098{
Al Viroba46df92006-10-10 22:46:07 +01002099 struct robust_list_head __user *head;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002100 unsigned long ret;
2101
2102 if (!pid)
2103 head = current->robust_list;
2104 else {
2105 struct task_struct *p;
2106
2107 ret = -ESRCH;
Oleg Nesterovaaa2a972006-09-29 02:00:55 -07002108 rcu_read_lock();
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002109 p = find_task_by_pid(pid);
2110 if (!p)
2111 goto err_unlock;
2112 ret = -EPERM;
2113 if ((current->euid != p->euid) && (current->euid != p->uid) &&
2114 !capable(CAP_SYS_PTRACE))
2115 goto err_unlock;
2116 head = p->robust_list;
Oleg Nesterovaaa2a972006-09-29 02:00:55 -07002117 rcu_read_unlock();
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002118 }
2119
2120 if (put_user(sizeof(*head), len_ptr))
2121 return -EFAULT;
2122 return put_user(head, head_ptr);
2123
2124err_unlock:
Oleg Nesterovaaa2a972006-09-29 02:00:55 -07002125 rcu_read_unlock();
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002126
2127 return ret;
2128}
2129
2130/*
2131 * Process a futex-list entry, check whether it's owned by the
2132 * dying task, and do notification if so:
2133 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002134int handle_futex_death(u32 __user *uaddr, struct task_struct *curr, int pi)
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002135{
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002136 u32 uval, nval, mval;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002137
Ingo Molnar8f17d3a2006-03-27 01:16:27 -08002138retry:
2139 if (get_user(uval, uaddr))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002140 return -1;
2141
Ingo Molnar8f17d3a2006-03-27 01:16:27 -08002142 if ((uval & FUTEX_TID_MASK) == curr->pid) {
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002143 /*
2144 * Ok, this dying thread is truly holding a futex
2145 * of interest. Set the OWNER_DIED bit atomically
2146 * via cmpxchg, and if the value had FUTEX_WAITERS
2147 * set, wake up a waiter (if any). (We have to do a
2148 * futex_wake() even if OWNER_DIED is already set -
2149 * to handle the rare but possible case of recursive
2150 * thread-death.) The rest of the cleanup is done in
2151 * userspace.
2152 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002153 mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002154 /* Also keep the FUTEX_WAITER_REQUEUED flag if set */
2155 mval |= (uval & FUTEX_WAITER_REQUEUED);
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002156 nval = futex_atomic_cmpxchg_inatomic(uaddr, uval, mval);
2157
Ingo Molnarc87e2832006-06-27 02:54:58 -07002158 if (nval == -EFAULT)
2159 return -1;
2160
2161 if (nval != uval)
Ingo Molnar8f17d3a2006-03-27 01:16:27 -08002162 goto retry;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002163
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002164 /*
2165 * Wake robust non-PI futexes here. The wakeup of
2166 * PI futexes happens in exit_pi_state():
2167 */
2168 if (!pi) {
2169 if (uval & FUTEX_WAITERS)
2170 futex_wake(uaddr, 1);
2171 }
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002172 }
2173 return 0;
2174}
2175
2176/*
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002177 * Fetch a robust-list pointer. Bit 0 signals PI futexes:
2178 */
2179static inline int fetch_robust_entry(struct robust_list __user **entry,
Al Viroba46df92006-10-10 22:46:07 +01002180 struct robust_list __user * __user *head,
2181 int *pi)
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002182{
2183 unsigned long uentry;
2184
Al Viroba46df92006-10-10 22:46:07 +01002185 if (get_user(uentry, (unsigned long __user *)head))
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002186 return -EFAULT;
2187
Al Viroba46df92006-10-10 22:46:07 +01002188 *entry = (void __user *)(uentry & ~1UL);
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002189 *pi = uentry & 1;
2190
2191 return 0;
2192}
2193
2194/*
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002195 * Walk curr->robust_list (very carefully, it's a userspace list!)
2196 * and mark any locks found there dead, and notify any waiters.
2197 *
2198 * We silently return on any sign of list-walking problem.
2199 */
2200void exit_robust_list(struct task_struct *curr)
2201{
2202 struct robust_list_head __user *head = curr->robust_list;
2203 struct robust_list __user *entry, *pending;
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002204 unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002205 unsigned long futex_offset;
2206
2207 /*
2208 * Fetch the list head (which was registered earlier, via
2209 * sys_set_robust_list()):
2210 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002211 if (fetch_robust_entry(&entry, &head->list.next, &pi))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002212 return;
2213 /*
2214 * Fetch the relative futex offset:
2215 */
2216 if (get_user(futex_offset, &head->futex_offset))
2217 return;
2218 /*
2219 * Fetch any possibly pending lock-add first, and handle it
2220 * if it exists:
2221 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002222 if (fetch_robust_entry(&pending, &head->list_op_pending, &pip))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002223 return;
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002224
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002225 if (pending)
Al Viroba46df92006-10-10 22:46:07 +01002226 handle_futex_death((void __user *)pending + futex_offset, curr, pip);
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002227
2228 while (entry != &head->list) {
2229 /*
2230 * A pending lock might already be on the list, so
Ingo Molnarc87e2832006-06-27 02:54:58 -07002231 * don't process it twice:
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002232 */
2233 if (entry != pending)
Al Viroba46df92006-10-10 22:46:07 +01002234 if (handle_futex_death((void __user *)entry + futex_offset,
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002235 curr, pi))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002236 return;
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002237 /*
2238 * Fetch the next entry in the list:
2239 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +02002240 if (fetch_robust_entry(&entry, &entry->next, &pi))
Ingo Molnar0771dfe2006-03-27 01:16:22 -08002241 return;
2242 /*
2243 * Avoid excessively long or circular lists:
2244 */
2245 if (!--limit)
2246 break;
2247
2248 cond_resched();
2249 }
2250}
2251
Pierre Peifferc19384b2007-05-09 02:35:02 -07002252long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
Ingo Molnare2970f22006-06-27 02:54:47 -07002253 u32 __user *uaddr2, u32 val2, u32 val3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254{
2255 int ret;
2256
2257 switch (op) {
2258 case FUTEX_WAIT:
2259 ret = futex_wait(uaddr, val, timeout);
2260 break;
2261 case FUTEX_WAKE:
2262 ret = futex_wake(uaddr, val);
2263 break;
2264 case FUTEX_FD:
2265 /* non-zero val means F_SETOWN(getpid()) & F_SETSIG(val) */
2266 ret = futex_fd(uaddr, val);
2267 break;
2268 case FUTEX_REQUEUE:
2269 ret = futex_requeue(uaddr, uaddr2, val, val2, NULL);
2270 break;
2271 case FUTEX_CMP_REQUEUE:
2272 ret = futex_requeue(uaddr, uaddr2, val, val2, &val3);
2273 break;
Jakub Jelinek4732efb2005-09-06 15:16:25 -07002274 case FUTEX_WAKE_OP:
2275 ret = futex_wake_op(uaddr, uaddr2, val, val2, val3);
2276 break;
Ingo Molnarc87e2832006-06-27 02:54:58 -07002277 case FUTEX_LOCK_PI:
Pierre Peifferc19384b2007-05-09 02:35:02 -07002278 ret = futex_lock_pi(uaddr, val, timeout, 0);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002279 break;
2280 case FUTEX_UNLOCK_PI:
2281 ret = futex_unlock_pi(uaddr);
2282 break;
2283 case FUTEX_TRYLOCK_PI:
Pierre Peifferc19384b2007-05-09 02:35:02 -07002284 ret = futex_lock_pi(uaddr, 0, timeout, 1);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002285 break;
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002286 case FUTEX_CMP_REQUEUE_PI:
2287 ret = futex_requeue_pi(uaddr, uaddr2, val, val2, &val3);
2288 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 default:
2290 ret = -ENOSYS;
2291 }
2292 return ret;
2293}
2294
2295
Ingo Molnare2970f22006-06-27 02:54:47 -07002296asmlinkage long sys_futex(u32 __user *uaddr, int op, u32 val,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 struct timespec __user *utime, u32 __user *uaddr2,
Ingo Molnare2970f22006-06-27 02:54:47 -07002298 u32 val3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299{
Pierre Peifferc19384b2007-05-09 02:35:02 -07002300 struct timespec ts;
2301 ktime_t t, *tp = NULL;
Ingo Molnare2970f22006-06-27 02:54:47 -07002302 u32 val2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
Ingo Molnarc87e2832006-06-27 02:54:58 -07002304 if (utime && (op == FUTEX_WAIT || op == FUTEX_LOCK_PI)) {
Pierre Peifferc19384b2007-05-09 02:35:02 -07002305 if (copy_from_user(&ts, utime, sizeof(ts)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 return -EFAULT;
Pierre Peifferc19384b2007-05-09 02:35:02 -07002307 if (!timespec_valid(&ts))
Thomas Gleixner9741ef92006-03-31 02:31:32 -08002308 return -EINVAL;
Pierre Peifferc19384b2007-05-09 02:35:02 -07002309
2310 t = timespec_to_ktime(ts);
Ingo Molnarc87e2832006-06-27 02:54:58 -07002311 if (op == FUTEX_WAIT)
Pierre Peifferc19384b2007-05-09 02:35:02 -07002312 t = ktime_add(ktime_get(), t);
2313 tp = &t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 }
2315 /*
2316 * requeue parameter in 'utime' if op == FUTEX_REQUEUE.
2317 */
Pierre Peifferd0aa7a72007-05-09 02:35:02 -07002318 if (op == FUTEX_REQUEUE || op == FUTEX_CMP_REQUEUE
2319 || op == FUTEX_CMP_REQUEUE_PI)
Ingo Molnare2970f22006-06-27 02:54:47 -07002320 val2 = (u32) (unsigned long) utime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Pierre Peifferc19384b2007-05-09 02:35:02 -07002322 return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323}
2324
David Howells454e2392006-06-23 02:02:57 -07002325static int futexfs_get_sb(struct file_system_type *fs_type,
2326 int flags, const char *dev_name, void *data,
2327 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328{
David Howells454e2392006-06-23 02:02:57 -07002329 return get_sb_pseudo(fs_type, "futex", NULL, 0xBAD1DEA, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330}
2331
2332static struct file_system_type futex_fs_type = {
2333 .name = "futexfs",
2334 .get_sb = futexfs_get_sb,
2335 .kill_sb = kill_anon_super,
2336};
2337
2338static int __init init(void)
2339{
Akinobu Mita95362fa2006-12-06 20:39:03 -08002340 int i = register_filesystem(&futex_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Akinobu Mita95362fa2006-12-06 20:39:03 -08002342 if (i)
2343 return i;
2344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 futex_mnt = kern_mount(&futex_fs_type);
Akinobu Mita95362fa2006-12-06 20:39:03 -08002346 if (IS_ERR(futex_mnt)) {
2347 unregister_filesystem(&futex_fs_type);
2348 return PTR_ERR(futex_mnt);
2349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
2351 for (i = 0; i < ARRAY_SIZE(futex_queues); i++) {
Pierre Peifferec92d082007-05-09 02:35:00 -07002352 plist_head_init(&futex_queues[i].chain, &futex_queues[i].lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 spin_lock_init(&futex_queues[i].lock);
2354 }
2355 return 0;
2356}
2357__initcall(init);