]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | * | |
0771dfef IM |
11 | * 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 | * | |
c87e2837 IM |
15 | * 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 | * | |
34f01cc1 ED |
19 | * PRIVATE futexes by Eric Dumazet |
20 | * Copyright (C) 2007 Eric Dumazet <dada1@cosmosbay.com> | |
21 | * | |
1da177e4 LT |
22 | * 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> | |
7ed20e1a | 53 | #include <linux/signal.h> |
9adef58b | 54 | #include <linux/module.h> |
4732efbe | 55 | #include <asm/futex.h> |
1da177e4 | 56 | |
c87e2837 IM |
57 | #include "rtmutex_common.h" |
58 | ||
d0aa7a70 PP |
59 | #ifdef CONFIG_DEBUG_RT_MUTEXES |
60 | # include "rtmutex-debug.h" | |
61 | #else | |
62 | # include "rtmutex.h" | |
63 | #endif | |
64 | ||
1da177e4 LT |
65 | #define FUTEX_HASHBITS (CONFIG_BASE_SMALL ? 4 : 8) |
66 | ||
c87e2837 IM |
67 | /* |
68 | * Priority Inheritance state: | |
69 | */ | |
70 | struct 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 | ||
1da177e4 LT |
88 | /* |
89 | * 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. | |
ec92d082 | 93 | * It is considered woken when plist_node_empty(&q->list) || q->lock_ptr == 0. |
1da177e4 LT |
94 | * 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 | */ | |
97 | struct futex_q { | |
ec92d082 | 98 | struct plist_node list; |
1da177e4 LT |
99 | wait_queue_head_t waiters; |
100 | ||
e2970f2f | 101 | /* Which hash list lock to use: */ |
1da177e4 LT |
102 | spinlock_t *lock_ptr; |
103 | ||
e2970f2f | 104 | /* Key which the futex is hashed on: */ |
1da177e4 LT |
105 | union futex_key key; |
106 | ||
e2970f2f | 107 | /* For fd, sigio sent using these: */ |
1da177e4 LT |
108 | int fd; |
109 | struct file *filp; | |
c87e2837 IM |
110 | |
111 | /* Optional priority inheritance state: */ | |
112 | struct futex_pi_state *pi_state; | |
113 | struct task_struct *task; | |
d0aa7a70 PP |
114 | |
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; | |
1da177e4 LT |
120 | }; |
121 | ||
122 | /* | |
123 | * Split the global futex_lock into every hash list lock. | |
124 | */ | |
125 | struct futex_hash_bucket { | |
ec92d082 PP |
126 | spinlock_t lock; |
127 | struct plist_head chain; | |
1da177e4 LT |
128 | }; |
129 | ||
130 | static struct futex_hash_bucket futex_queues[1<<FUTEX_HASHBITS]; | |
131 | ||
132 | /* Futex-fs vfsmount entry: */ | |
133 | static struct vfsmount *futex_mnt; | |
134 | ||
135 | /* | |
136 | * We hash on the keys returned from get_futex_key (see below). | |
137 | */ | |
138 | static 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 | */ | |
149 | static 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 | ||
34f01cc1 ED |
156 | /** |
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 | * ¤t->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. | |
1da177e4 | 165 | * |
f3a43f3f | 166 | * For shared mappings, it's (page->index, vma->vm_file->f_path.dentry->d_inode, |
1da177e4 LT |
167 | * 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 | * | |
34f01cc1 ED |
170 | * fshared is NULL for PROCESS_PRIVATE futexes |
171 | * For other futexes, it points to ¤t->mm->mmap_sem and | |
172 | * caller must have taken the reader lock. but NOT any spinlocks. | |
1da177e4 | 173 | */ |
34f01cc1 ED |
174 | int get_futex_key(u32 __user *uaddr, struct rw_semaphore *fshared, |
175 | union futex_key *key) | |
1da177e4 | 176 | { |
e2970f2f | 177 | unsigned long address = (unsigned long)uaddr; |
1da177e4 LT |
178 | 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 | */ | |
e2970f2f | 186 | key->both.offset = address % PAGE_SIZE; |
34f01cc1 | 187 | if (unlikely((address % sizeof(u32)) != 0)) |
1da177e4 | 188 | return -EINVAL; |
e2970f2f | 189 | address -= key->both.offset; |
1da177e4 | 190 | |
34f01cc1 ED |
191 | /* |
192 | * 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 | } | |
1da177e4 LT |
205 | /* |
206 | * The futex is hashed differently depending on whether | |
207 | * it's in a shared or private mapping. So check vma first. | |
208 | */ | |
e2970f2f | 209 | vma = find_extend_vma(mm, address); |
1da177e4 LT |
210 | 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 | ||
d0aa7a70 PP |
219 | /* Save the user address in the ley */ |
220 | key->uaddr = uaddr; | |
221 | ||
1da177e4 LT |
222 | /* |
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))) { | |
34f01cc1 | 232 | key->both.offset |= FUT_OFF_MMSHARED; /* reference taken on mm */ |
1da177e4 | 233 | key->private.mm = mm; |
e2970f2f | 234 | key->private.address = address; |
1da177e4 LT |
235 | return 0; |
236 | } | |
237 | ||
238 | /* | |
239 | * Linear file mappings are also simple. | |
240 | */ | |
f3a43f3f | 241 | key->shared.inode = vma->vm_file->f_path.dentry->d_inode; |
34f01cc1 | 242 | key->both.offset |= FUT_OFF_INODE; /* inode-based key. */ |
1da177e4 | 243 | if (likely(!(vma->vm_flags & VM_NONLINEAR))) { |
e2970f2f | 244 | key->shared.pgoff = (((address - vma->vm_start) >> PAGE_SHIFT) |
1da177e4 LT |
245 | + 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 | */ | |
e2970f2f | 255 | err = get_user_pages(current, mm, address, 1, 0, 0, &page, NULL); |
1da177e4 LT |
256 | 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 | } | |
9adef58b | 264 | EXPORT_SYMBOL_GPL(get_futex_key); |
1da177e4 LT |
265 | |
266 | /* | |
267 | * Take a reference to the resource addressed by a key. | |
268 | * Can be called while holding spinlocks. | |
269 | * | |
1da177e4 | 270 | */ |
9adef58b | 271 | inline void get_futex_key_refs(union futex_key *key) |
1da177e4 | 272 | { |
34f01cc1 ED |
273 | if (key->both.ptr == 0) |
274 | return; | |
275 | switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) { | |
276 | case FUT_OFF_INODE: | |
1da177e4 | 277 | atomic_inc(&key->shared.inode->i_count); |
34f01cc1 ED |
278 | break; |
279 | case FUT_OFF_MMSHARED: | |
1da177e4 | 280 | atomic_inc(&key->private.mm->mm_count); |
34f01cc1 | 281 | break; |
1da177e4 LT |
282 | } |
283 | } | |
9adef58b | 284 | EXPORT_SYMBOL_GPL(get_futex_key_refs); |
1da177e4 LT |
285 | |
286 | /* | |
287 | * Drop a reference to the resource addressed by a key. | |
288 | * The hash bucket spinlock must not be held. | |
289 | */ | |
9adef58b | 290 | void drop_futex_key_refs(union futex_key *key) |
1da177e4 | 291 | { |
34f01cc1 ED |
292 | if (key->both.ptr == 0) |
293 | return; | |
294 | switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) { | |
295 | case FUT_OFF_INODE: | |
1da177e4 | 296 | iput(key->shared.inode); |
34f01cc1 ED |
297 | break; |
298 | case FUT_OFF_MMSHARED: | |
1da177e4 | 299 | mmdrop(key->private.mm); |
34f01cc1 | 300 | break; |
1da177e4 LT |
301 | } |
302 | } | |
9adef58b | 303 | EXPORT_SYMBOL_GPL(drop_futex_key_refs); |
1da177e4 | 304 | |
e2970f2f | 305 | static inline int get_futex_value_locked(u32 *dest, u32 __user *from) |
1da177e4 LT |
306 | { |
307 | int ret; | |
308 | ||
a866374a | 309 | pagefault_disable(); |
e2970f2f | 310 | ret = __copy_from_user_inatomic(dest, from, sizeof(u32)); |
a866374a | 311 | pagefault_enable(); |
1da177e4 LT |
312 | |
313 | return ret ? -EFAULT : 0; | |
314 | } | |
315 | ||
c87e2837 | 316 | /* |
34f01cc1 ED |
317 | * Fault handling. |
318 | * if fshared is non NULL, current->mm->mmap_sem is already held | |
c87e2837 | 319 | */ |
34f01cc1 ED |
320 | static int futex_handle_fault(unsigned long address, |
321 | struct rw_semaphore *fshared, int attempt) | |
c87e2837 IM |
322 | { |
323 | struct vm_area_struct * vma; | |
324 | struct mm_struct *mm = current->mm; | |
34f01cc1 | 325 | int ret = -EFAULT; |
c87e2837 | 326 | |
34f01cc1 ED |
327 | if (attempt > 2) |
328 | return ret; | |
c87e2837 | 329 | |
34f01cc1 ED |
330 | 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 | } | |
c87e2837 | 345 | } |
34f01cc1 ED |
346 | if (!fshared) |
347 | up_read(&mm->mmap_sem); | |
348 | return ret; | |
c87e2837 IM |
349 | } |
350 | ||
351 | /* | |
352 | * PI code: | |
353 | */ | |
354 | static 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 | ||
4668edc3 | 361 | pi_state = kzalloc(sizeof(*pi_state), GFP_KERNEL); |
c87e2837 IM |
362 | |
363 | if (!pi_state) | |
364 | return -ENOMEM; | |
365 | ||
c87e2837 IM |
366 | 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 | ||
376 | static 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 | ||
386 | static 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 | */ | |
421 | static struct task_struct * futex_find_get_task(pid_t pid) | |
422 | { | |
423 | struct task_struct *p; | |
424 | ||
d359b549 | 425 | rcu_read_lock(); |
c87e2837 IM |
426 | 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 | } | |
d015baeb | 433 | if (p->exit_state != 0) { |
c87e2837 IM |
434 | p = NULL; |
435 | goto out_unlock; | |
436 | } | |
437 | get_task_struct(p); | |
438 | out_unlock: | |
d359b549 | 439 | rcu_read_unlock(); |
c87e2837 IM |
440 | |
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 | */ | |
449 | void exit_pi_state_list(struct task_struct *curr) | |
450 | { | |
c87e2837 IM |
451 | struct list_head *next, *head = &curr->pi_state_list; |
452 | struct futex_pi_state *pi_state; | |
627371d7 | 453 | struct futex_hash_bucket *hb; |
c87e2837 IM |
454 | 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 | |
627371d7 | 459 | * versus waiters unqueueing themselves: |
c87e2837 IM |
460 | */ |
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; | |
627371d7 | 467 | hb = hash_futex(&key); |
c87e2837 IM |
468 | spin_unlock_irq(&curr->pi_lock); |
469 | ||
c87e2837 IM |
470 | spin_lock(&hb->lock); |
471 | ||
472 | spin_lock_irq(&curr->pi_lock); | |
627371d7 IM |
473 | /* |
474 | * We dropped the pi-lock, so re-check whether this | |
475 | * task still owns the PI-state: | |
476 | */ | |
c87e2837 IM |
477 | if (head->next != next) { |
478 | spin_unlock(&hb->lock); | |
479 | continue; | |
480 | } | |
481 | ||
c87e2837 | 482 | WARN_ON(pi_state->owner != curr); |
627371d7 IM |
483 | WARN_ON(list_empty(&pi_state->list)); |
484 | list_del_init(&pi_state->list); | |
c87e2837 IM |
485 | 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 | ||
497 | static int | |
d0aa7a70 PP |
498 | lookup_pi_state(u32 uval, struct futex_hash_bucket *hb, |
499 | union futex_key *key, struct futex_pi_state **ps) | |
c87e2837 IM |
500 | { |
501 | struct futex_pi_state *pi_state = NULL; | |
502 | struct futex_q *this, *next; | |
ec92d082 | 503 | struct plist_head *head; |
c87e2837 IM |
504 | struct task_struct *p; |
505 | pid_t pid; | |
506 | ||
507 | head = &hb->chain; | |
508 | ||
ec92d082 | 509 | plist_for_each_entry_safe(this, next, head, list) { |
d0aa7a70 | 510 | if (match_futex(&this->key, key)) { |
c87e2837 IM |
511 | /* |
512 | * Another waiter already exists - bump up | |
513 | * the refcount and return its pi_state: | |
514 | */ | |
515 | pi_state = this->pi_state; | |
06a9ec29 TG |
516 | /* |
517 | * Userspace might have messed up non PI and PI futexes | |
518 | */ | |
519 | if (unlikely(!pi_state)) | |
520 | return -EINVAL; | |
521 | ||
627371d7 IM |
522 | WARN_ON(!atomic_read(&pi_state->refcount)); |
523 | ||
c87e2837 | 524 | atomic_inc(&pi_state->refcount); |
d0aa7a70 | 525 | *ps = pi_state; |
c87e2837 IM |
526 | |
527 | return 0; | |
528 | } | |
529 | } | |
530 | ||
531 | /* | |
e3f2ddea IM |
532 | * 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: | |
c87e2837 IM |
535 | */ |
536 | pid = uval & FUTEX_TID_MASK; | |
e3f2ddea IM |
537 | if (!pid && (uval & FUTEX_OWNER_DIED)) |
538 | return -ESRCH; | |
c87e2837 IM |
539 | 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: */ | |
d0aa7a70 | 552 | pi_state->key = *key; |
c87e2837 IM |
553 | |
554 | spin_lock_irq(&p->pi_lock); | |
627371d7 | 555 | WARN_ON(!list_empty(&pi_state->list)); |
c87e2837 IM |
556 | 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 | ||
d0aa7a70 | 562 | *ps = pi_state; |
c87e2837 IM |
563 | |
564 | return 0; | |
565 | } | |
566 | ||
1da177e4 LT |
567 | /* |
568 | * The hash bucket lock must be held when this is called. | |
569 | * Afterwards, the futex_q must not be accessed. | |
570 | */ | |
571 | static void wake_futex(struct futex_q *q) | |
572 | { | |
ec92d082 | 573 | plist_del(&q->list, &q->list.plist); |
1da177e4 LT |
574 | 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 | |
ec92d082 | 578 | * plist_del() and also before assigning to q->lock_ptr. |
1da177e4 LT |
579 | */ |
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. | |
8e31108b AM |
584 | * |
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. | |
1da177e4 | 589 | */ |
ccdea2f8 | 590 | smp_wmb(); |
1da177e4 LT |
591 | q->lock_ptr = NULL; |
592 | } | |
593 | ||
c87e2837 IM |
594 | static 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 | ||
21778867 | 603 | spin_lock(&pi_state->pi_mutex.wait_lock); |
c87e2837 IM |
604 | 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 | */ | |
e3f2ddea IM |
620 | if (!(uval & FUTEX_OWNER_DIED)) { |
621 | newval = FUTEX_WAITERS | new_owner->pid; | |
d0aa7a70 PP |
622 | /* Keep the FUTEX_WAITER_REQUEUED flag if it was set */ |
623 | newval |= (uval & FUTEX_WAITER_REQUEUED); | |
e3f2ddea | 624 | |
a866374a | 625 | pagefault_disable(); |
e3f2ddea | 626 | curval = futex_atomic_cmpxchg_inatomic(uaddr, uval, newval); |
a866374a | 627 | pagefault_enable(); |
e3f2ddea IM |
628 | if (curval == -EFAULT) |
629 | return -EFAULT; | |
630 | if (curval != uval) | |
631 | return -EINVAL; | |
632 | } | |
c87e2837 | 633 | |
627371d7 IM |
634 | 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)); | |
c87e2837 IM |
641 | list_add(&pi_state->list, &new_owner->pi_state_list); |
642 | pi_state->owner = new_owner; | |
627371d7 IM |
643 | spin_unlock_irq(&new_owner->pi_lock); |
644 | ||
21778867 | 645 | spin_unlock(&pi_state->pi_mutex.wait_lock); |
c87e2837 IM |
646 | rt_mutex_unlock(&pi_state->pi_mutex); |
647 | ||
648 | return 0; | |
649 | } | |
650 | ||
651 | static 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 | */ | |
a866374a | 659 | pagefault_disable(); |
c87e2837 | 660 | oldval = futex_atomic_cmpxchg_inatomic(uaddr, uval, 0); |
a866374a | 661 | pagefault_enable(); |
c87e2837 IM |
662 | |
663 | if (oldval == -EFAULT) | |
664 | return oldval; | |
665 | if (oldval != uval) | |
666 | return -EAGAIN; | |
667 | ||
668 | return 0; | |
669 | } | |
670 | ||
8b8f319f IM |
671 | /* |
672 | * Express the locking dependencies for lockdep: | |
673 | */ | |
674 | static inline void | |
675 | double_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 | ||
1da177e4 LT |
687 | /* |
688 | * Wake up all waiters hashed on the physical page that is mapped | |
689 | * to this virtual address: | |
690 | */ | |
34f01cc1 ED |
691 | static int futex_wake(u32 __user *uaddr, struct rw_semaphore *fshared, |
692 | int nr_wake) | |
1da177e4 | 693 | { |
e2970f2f | 694 | struct futex_hash_bucket *hb; |
1da177e4 | 695 | struct futex_q *this, *next; |
ec92d082 | 696 | struct plist_head *head; |
e2970f2f | 697 | union futex_key key; |
1da177e4 LT |
698 | int ret; |
699 | ||
34f01cc1 ED |
700 | if (fshared) |
701 | down_read(fshared); | |
1da177e4 | 702 | |
34f01cc1 | 703 | ret = get_futex_key(uaddr, fshared, &key); |
1da177e4 LT |
704 | if (unlikely(ret != 0)) |
705 | goto out; | |
706 | ||
e2970f2f IM |
707 | hb = hash_futex(&key); |
708 | spin_lock(&hb->lock); | |
709 | head = &hb->chain; | |
1da177e4 | 710 | |
ec92d082 | 711 | plist_for_each_entry_safe(this, next, head, list) { |
1da177e4 | 712 | if (match_futex (&this->key, &key)) { |
ed6f7b10 IM |
713 | if (this->pi_state) { |
714 | ret = -EINVAL; | |
715 | break; | |
716 | } | |
1da177e4 LT |
717 | wake_futex(this); |
718 | if (++ret >= nr_wake) | |
719 | break; | |
720 | } | |
721 | } | |
722 | ||
e2970f2f | 723 | spin_unlock(&hb->lock); |
1da177e4 | 724 | out: |
34f01cc1 ED |
725 | if (fshared) |
726 | up_read(fshared); | |
1da177e4 LT |
727 | return ret; |
728 | } | |
729 | ||
d0aa7a70 PP |
730 | /* |
731 | * 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 | */ | |
736 | static inline int | |
737 | lookup_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 | ||
743 | retry: | |
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 | */ | |
34f01cc1 ED |
793 | static int futex_requeue_pi(u32 __user *uaddr1, |
794 | struct rw_semaphore *fshared, | |
795 | u32 __user *uaddr2, | |
d0aa7a70 PP |
796 | 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 | ||
810 | retry: | |
811 | /* | |
812 | * First take all the futex related locks: | |
813 | */ | |
34f01cc1 ED |
814 | if (fshared) |
815 | down_read(fshared); | |
d0aa7a70 | 816 | |
34f01cc1 | 817 | ret = get_futex_key(uaddr1, fshared, &key1); |
d0aa7a70 PP |
818 | if (unlikely(ret != 0)) |
819 | goto out; | |
34f01cc1 | 820 | ret = get_futex_key(uaddr2, fshared, &key2); |
d0aa7a70 PP |
821 | 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 | */ | |
34f01cc1 ED |
843 | if (fshared) |
844 | up_read(fshared); | |
d0aa7a70 PP |
845 | |
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 | ||
968 | out_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 | ||
977 | out: | |
34f01cc1 ED |
978 | if (fshared) |
979 | up_read(fshared); | |
d0aa7a70 PP |
980 | return ret; |
981 | } | |
982 | ||
4732efbe JJ |
983 | /* |
984 | * Wake up all waiters hashed on the physical page that is mapped | |
985 | * to this virtual address: | |
986 | */ | |
e2970f2f | 987 | static int |
34f01cc1 ED |
988 | futex_wake_op(u32 __user *uaddr1, struct rw_semaphore *fshared, |
989 | u32 __user *uaddr2, | |
e2970f2f | 990 | int nr_wake, int nr_wake2, int op) |
4732efbe JJ |
991 | { |
992 | union futex_key key1, key2; | |
e2970f2f | 993 | struct futex_hash_bucket *hb1, *hb2; |
ec92d082 | 994 | struct plist_head *head; |
4732efbe JJ |
995 | struct futex_q *this, *next; |
996 | int ret, op_ret, attempt = 0; | |
997 | ||
998 | retryfull: | |
34f01cc1 ED |
999 | if (fshared) |
1000 | down_read(fshared); | |
4732efbe | 1001 | |
34f01cc1 | 1002 | ret = get_futex_key(uaddr1, fshared, &key1); |
4732efbe JJ |
1003 | if (unlikely(ret != 0)) |
1004 | goto out; | |
34f01cc1 | 1005 | ret = get_futex_key(uaddr2, fshared, &key2); |
4732efbe JJ |
1006 | if (unlikely(ret != 0)) |
1007 | goto out; | |
1008 | ||
e2970f2f IM |
1009 | hb1 = hash_futex(&key1); |
1010 | hb2 = hash_futex(&key2); | |
4732efbe JJ |
1011 | |
1012 | retry: | |
8b8f319f | 1013 | double_lock_hb(hb1, hb2); |
4732efbe | 1014 | |
e2970f2f | 1015 | op_ret = futex_atomic_op_inuser(op, uaddr2); |
4732efbe | 1016 | if (unlikely(op_ret < 0)) { |
e2970f2f | 1017 | u32 dummy; |
4732efbe | 1018 | |
e2970f2f IM |
1019 | spin_unlock(&hb1->lock); |
1020 | if (hb1 != hb2) | |
1021 | spin_unlock(&hb2->lock); | |
4732efbe | 1022 | |
7ee1dd3f | 1023 | #ifndef CONFIG_MMU |
e2970f2f IM |
1024 | /* |
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 | */ | |
7ee1dd3f DH |
1028 | ret = op_ret; |
1029 | goto out; | |
1030 | #endif | |
1031 | ||
796f8d9b DG |
1032 | if (unlikely(op_ret != -EFAULT)) { |
1033 | ret = op_ret; | |
1034 | goto out; | |
1035 | } | |
1036 | ||
e2970f2f IM |
1037 | /* |
1038 | * futex_atomic_op_inuser needs to both read and write | |
4732efbe JJ |
1039 | * *(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 | |
e2970f2f IM |
1042 | * still holding the mmap_sem. |
1043 | */ | |
4732efbe | 1044 | if (attempt++) { |
34f01cc1 ED |
1045 | ret = futex_handle_fault((unsigned long)uaddr2, |
1046 | fshared, attempt); | |
1047 | if (ret) | |
4732efbe | 1048 | goto out; |
4732efbe JJ |
1049 | goto retry; |
1050 | } | |
1051 | ||
e2970f2f IM |
1052 | /* |
1053 | * If we would have faulted, release mmap_sem, | |
1054 | * fault it in and start all over again. | |
1055 | */ | |
34f01cc1 ED |
1056 | if (fshared) |
1057 | up_read(fshared); | |
4732efbe | 1058 | |
e2970f2f | 1059 | ret = get_user(dummy, uaddr2); |
4732efbe JJ |
1060 | if (ret) |
1061 | return ret; | |
1062 | ||
1063 | goto retryfull; | |
1064 | } | |
1065 | ||
e2970f2f | 1066 | head = &hb1->chain; |
4732efbe | 1067 | |
ec92d082 | 1068 | plist_for_each_entry_safe(this, next, head, list) { |
4732efbe JJ |
1069 | 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) { | |
e2970f2f | 1077 | head = &hb2->chain; |
4732efbe JJ |
1078 | |
1079 | op_ret = 0; | |
ec92d082 | 1080 | plist_for_each_entry_safe(this, next, head, list) { |
4732efbe JJ |
1081 | 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 | ||
e2970f2f IM |
1090 | spin_unlock(&hb1->lock); |
1091 | if (hb1 != hb2) | |
1092 | spin_unlock(&hb2->lock); | |
4732efbe | 1093 | out: |
34f01cc1 ED |
1094 | if (fshared) |
1095 | up_read(fshared); | |
4732efbe JJ |
1096 | return ret; |
1097 | } | |
1098 | ||
1da177e4 LT |
1099 | /* |
1100 | * Requeue all waiters hashed on one physical page to another | |
1101 | * physical page. | |
1102 | */ | |
34f01cc1 ED |
1103 | static int futex_requeue(u32 __user *uaddr1, struct rw_semaphore *fshared, |
1104 | u32 __user *uaddr2, | |
e2970f2f | 1105 | int nr_wake, int nr_requeue, u32 *cmpval) |
1da177e4 LT |
1106 | { |
1107 | union futex_key key1, key2; | |
e2970f2f | 1108 | struct futex_hash_bucket *hb1, *hb2; |
ec92d082 | 1109 | struct plist_head *head1; |
1da177e4 LT |
1110 | struct futex_q *this, *next; |
1111 | int ret, drop_count = 0; | |
1112 | ||
1113 | retry: | |
34f01cc1 ED |
1114 | if (fshared) |
1115 | down_read(fshared); | |
1da177e4 | 1116 | |
34f01cc1 | 1117 | ret = get_futex_key(uaddr1, fshared, &key1); |
1da177e4 LT |
1118 | if (unlikely(ret != 0)) |
1119 | goto out; | |
34f01cc1 | 1120 | ret = get_futex_key(uaddr2, fshared, &key2); |
1da177e4 LT |
1121 | if (unlikely(ret != 0)) |
1122 | goto out; | |
1123 | ||
e2970f2f IM |
1124 | hb1 = hash_futex(&key1); |
1125 | hb2 = hash_futex(&key2); | |
1da177e4 | 1126 | |
8b8f319f | 1127 | double_lock_hb(hb1, hb2); |
1da177e4 | 1128 | |
e2970f2f IM |
1129 | if (likely(cmpval != NULL)) { |
1130 | u32 curval; | |
1da177e4 | 1131 | |
e2970f2f | 1132 | ret = get_futex_value_locked(&curval, uaddr1); |
1da177e4 LT |
1133 | |
1134 | if (unlikely(ret)) { | |
e2970f2f IM |
1135 | spin_unlock(&hb1->lock); |
1136 | if (hb1 != hb2) | |
1137 | spin_unlock(&hb2->lock); | |
1da177e4 | 1138 | |
e2970f2f IM |
1139 | /* |
1140 | * If we would have faulted, release mmap_sem, fault | |
1da177e4 LT |
1141 | * it in and start all over again. |
1142 | */ | |
34f01cc1 ED |
1143 | if (fshared) |
1144 | up_read(fshared); | |
1da177e4 | 1145 | |
e2970f2f | 1146 | ret = get_user(curval, uaddr1); |
1da177e4 LT |
1147 | |
1148 | if (!ret) | |
1149 | goto retry; | |
1150 | ||
1151 | return ret; | |
1152 | } | |
e2970f2f | 1153 | if (curval != *cmpval) { |
1da177e4 LT |
1154 | ret = -EAGAIN; |
1155 | goto out_unlock; | |
1156 | } | |
1157 | } | |
1158 | ||
e2970f2f | 1159 | head1 = &hb1->chain; |
ec92d082 | 1160 | plist_for_each_entry_safe(this, next, head1, list) { |
1da177e4 LT |
1161 | if (!match_futex (&this->key, &key1)) |
1162 | continue; | |
1163 | if (++ret <= nr_wake) { | |
1164 | wake_futex(this); | |
1165 | } else { | |
59e0e0ac SD |
1166 | /* |
1167 | * If key1 and key2 hash to the same bucket, no need to | |
1168 | * requeue. | |
1169 | */ | |
1170 | if (likely(head1 != &hb2->chain)) { | |
ec92d082 PP |
1171 | plist_del(&this->list, &hb1->chain); |
1172 | plist_add(&this->list, &hb2->chain); | |
59e0e0ac | 1173 | this->lock_ptr = &hb2->lock; |
ec92d082 PP |
1174 | #ifdef CONFIG_DEBUG_PI_LIST |
1175 | this->list.plist.lock = &hb2->lock; | |
1176 | #endif | |
1177 | } | |
1da177e4 | 1178 | this->key = key2; |
9adef58b | 1179 | get_futex_key_refs(&key2); |
1da177e4 LT |
1180 | drop_count++; |
1181 | ||
1182 | if (ret - nr_wake >= nr_requeue) | |
1183 | break; | |
1da177e4 LT |
1184 | } |
1185 | } | |
1186 | ||
1187 | out_unlock: | |
e2970f2f IM |
1188 | spin_unlock(&hb1->lock); |
1189 | if (hb1 != hb2) | |
1190 | spin_unlock(&hb2->lock); | |
1da177e4 | 1191 | |
9adef58b | 1192 | /* drop_futex_key_refs() must be called outside the spinlocks. */ |
1da177e4 | 1193 | while (--drop_count >= 0) |
9adef58b | 1194 | drop_futex_key_refs(&key1); |
1da177e4 LT |
1195 | |
1196 | out: | |
34f01cc1 ED |
1197 | if (fshared) |
1198 | up_read(fshared); | |
1da177e4 LT |
1199 | return ret; |
1200 | } | |
1201 | ||
1202 | /* The key must be already stored in q->key. */ | |
1203 | static inline struct futex_hash_bucket * | |
1204 | queue_lock(struct futex_q *q, int fd, struct file *filp) | |
1205 | { | |
e2970f2f | 1206 | struct futex_hash_bucket *hb; |
1da177e4 LT |
1207 | |
1208 | q->fd = fd; | |
1209 | q->filp = filp; | |
1210 | ||
1211 | init_waitqueue_head(&q->waiters); | |
1212 | ||
9adef58b | 1213 | get_futex_key_refs(&q->key); |
e2970f2f IM |
1214 | hb = hash_futex(&q->key); |
1215 | q->lock_ptr = &hb->lock; | |
1da177e4 | 1216 | |
e2970f2f IM |
1217 | spin_lock(&hb->lock); |
1218 | return hb; | |
1da177e4 LT |
1219 | } |
1220 | ||
e2970f2f | 1221 | static inline void __queue_me(struct futex_q *q, struct futex_hash_bucket *hb) |
1da177e4 | 1222 | { |
ec92d082 PP |
1223 | 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); | |
c87e2837 | 1240 | q->task = current; |
e2970f2f | 1241 | spin_unlock(&hb->lock); |
1da177e4 LT |
1242 | } |
1243 | ||
1244 | static inline void | |
e2970f2f | 1245 | queue_unlock(struct futex_q *q, struct futex_hash_bucket *hb) |
1da177e4 | 1246 | { |
e2970f2f | 1247 | spin_unlock(&hb->lock); |
9adef58b | 1248 | drop_futex_key_refs(&q->key); |
1da177e4 LT |
1249 | } |
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. */ | |
1257 | static void queue_me(struct futex_q *q, int fd, struct file *filp) | |
1258 | { | |
e2970f2f IM |
1259 | struct futex_hash_bucket *hb; |
1260 | ||
1261 | hb = queue_lock(q, fd, filp); | |
1262 | __queue_me(q, hb); | |
1da177e4 LT |
1263 | } |
1264 | ||
1265 | /* Return 1 if we were still queued (ie. 0 means we were woken) */ | |
1266 | static int unqueue_me(struct futex_q *q) | |
1267 | { | |
1da177e4 | 1268 | spinlock_t *lock_ptr; |
e2970f2f | 1269 | int ret = 0; |
1da177e4 LT |
1270 | |
1271 | /* In the common case we don't take the spinlock, which is nice. */ | |
1272 | retry: | |
1273 | lock_ptr = q->lock_ptr; | |
e91467ec | 1274 | barrier(); |
1da177e4 LT |
1275 | 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 | } | |
ec92d082 PP |
1294 | WARN_ON(plist_node_empty(&q->list)); |
1295 | plist_del(&q->list, &q->list.plist); | |
c87e2837 IM |
1296 | |
1297 | BUG_ON(q->pi_state); | |
1298 | ||
1da177e4 LT |
1299 | spin_unlock(lock_ptr); |
1300 | ret = 1; | |
1301 | } | |
1302 | ||
9adef58b | 1303 | drop_futex_key_refs(&q->key); |
1da177e4 LT |
1304 | return ret; |
1305 | } | |
1306 | ||
c87e2837 IM |
1307 | /* |
1308 | * PI futexes can not be requeued and must remove themself from the | |
d0aa7a70 PP |
1309 | * hash bucket. The hash bucket lock (i.e. lock_ptr) is held on entry |
1310 | * and dropped here. | |
c87e2837 | 1311 | */ |
d0aa7a70 | 1312 | static void unqueue_me_pi(struct futex_q *q) |
c87e2837 | 1313 | { |
ec92d082 PP |
1314 | WARN_ON(plist_node_empty(&q->list)); |
1315 | plist_del(&q->list, &q->list.plist); | |
c87e2837 IM |
1316 | |
1317 | BUG_ON(!q->pi_state); | |
1318 | free_pi_state(q->pi_state); | |
1319 | q->pi_state = NULL; | |
1320 | ||
d0aa7a70 | 1321 | spin_unlock(q->lock_ptr); |
c87e2837 | 1322 | |
9adef58b | 1323 | drop_futex_key_refs(&q->key); |
c87e2837 IM |
1324 | } |
1325 | ||
d0aa7a70 PP |
1326 | /* |
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 | */ | |
34f01cc1 ED |
1332 | static int fixup_pi_state_owner(u32 __user *uaddr, struct rw_semaphore *fshared, |
1333 | struct futex_q *q, | |
d0aa7a70 PP |
1334 | 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); | |
34f01cc1 ED |
1360 | if (fshared) |
1361 | up_read(fshared); | |
d0aa7a70 PP |
1362 | /* |
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 | ||
34f01cc1 ED |
1382 | /* |
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 | ||
72c1bbf3 | 1388 | static long futex_wait_restart(struct restart_block *restart); |
34f01cc1 ED |
1389 | static int futex_wait(u32 __user *uaddr, struct rw_semaphore *fshared, |
1390 | u32 val, ktime_t *abs_time) | |
1da177e4 | 1391 | { |
c87e2837 IM |
1392 | struct task_struct *curr = current; |
1393 | DECLARE_WAITQUEUE(wait, curr); | |
e2970f2f | 1394 | struct futex_hash_bucket *hb; |
1da177e4 | 1395 | struct futex_q q; |
e2970f2f IM |
1396 | u32 uval; |
1397 | int ret; | |
d0aa7a70 | 1398 | struct hrtimer_sleeper t, *to = NULL; |
c19384b5 | 1399 | int rem = 0; |
1da177e4 | 1400 | |
c87e2837 | 1401 | q.pi_state = NULL; |
1da177e4 | 1402 | retry: |
34f01cc1 ED |
1403 | if (fshared) |
1404 | down_read(fshared); | |
1da177e4 | 1405 | |
34f01cc1 | 1406 | ret = get_futex_key(uaddr, fshared, &q.key); |
1da177e4 LT |
1407 | if (unlikely(ret != 0)) |
1408 | goto out_release_sem; | |
1409 | ||
e2970f2f | 1410 | hb = queue_lock(&q, -1, NULL); |
1da177e4 LT |
1411 | |
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 | * | |
34f01cc1 ED |
1429 | * for shared futexes, we hold the mmap semaphore, so the mapping |
1430 | * cannot have changed since we looked it up in get_futex_key. | |
1da177e4 | 1431 | */ |
e2970f2f | 1432 | ret = get_futex_value_locked(&uval, uaddr); |
1da177e4 LT |
1433 | |
1434 | if (unlikely(ret)) { | |
e2970f2f | 1435 | queue_unlock(&q, hb); |
1da177e4 | 1436 | |
e2970f2f IM |
1437 | /* |
1438 | * If we would have faulted, release mmap_sem, fault it in and | |
1da177e4 LT |
1439 | * start all over again. |
1440 | */ | |
34f01cc1 ED |
1441 | if (fshared) |
1442 | up_read(fshared); | |
1da177e4 | 1443 | |
e2970f2f | 1444 | ret = get_user(uval, uaddr); |
1da177e4 LT |
1445 | |
1446 | if (!ret) | |
1447 | goto retry; | |
1448 | return ret; | |
1449 | } | |
c87e2837 IM |
1450 | ret = -EWOULDBLOCK; |
1451 | if (uval != val) | |
1452 | goto out_unlock_release_sem; | |
1da177e4 | 1453 | |
d0aa7a70 PP |
1454 | /* |
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 | ||
1da177e4 | 1462 | /* Only actually queue if *uaddr contained val. */ |
e2970f2f | 1463 | __queue_me(&q, hb); |
1da177e4 LT |
1464 | |
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. | |
c87e2837 | 1468 | */ |
34f01cc1 ED |
1469 | if (fshared) |
1470 | up_read(fshared); | |
1da177e4 LT |
1471 | |
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 | /* | |
ec92d082 | 1485 | * !plist_node_empty() is safe here without any lock. |
1da177e4 LT |
1486 | * q.lock_ptr != 0 is not safe, because of ordering against wakeup. |
1487 | */ | |
ec92d082 | 1488 | if (likely(!plist_node_empty(&q.list))) { |
c19384b5 PP |
1489 | if (!abs_time) |
1490 | schedule(); | |
1491 | else { | |
d0aa7a70 | 1492 | to = &t; |
c19384b5 PP |
1493 | hrtimer_init(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); |
1494 | hrtimer_init_sleeper(&t, current); | |
1495 | t.timer.expires = *abs_time; | |
1496 | ||
1497 | hrtimer_start(&t.timer, t.timer.expires, HRTIMER_MODE_ABS); | |
1498 | ||
1499 | /* | |
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); | |
72c1bbf3 | 1508 | |
c19384b5 PP |
1509 | /* Flag if a timeout occured */ |
1510 | rem = (t.task == NULL); | |
1511 | } | |
72c1bbf3 | 1512 | } |
1da177e4 LT |
1513 | __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 | ||
d0aa7a70 PP |
1520 | 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 | ||
34f01cc1 ED |
1540 | if (fshared) |
1541 | down_read(fshared); | |
d0aa7a70 PP |
1542 | 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 */ | |
34f01cc1 ED |
1558 | ret = fixup_pi_state_owner(uaddr, fshared, |
1559 | &q, hb, curr); | |
d0aa7a70 PP |
1560 | } 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); | |
34f01cc1 ED |
1572 | if (fshared) |
1573 | up_read(fshared); | |
d0aa7a70 PP |
1574 | } |
1575 | ||
1576 | debug_rt_mutex_free_waiter(&q.waiter); | |
1577 | ||
1578 | return ret; | |
1579 | } | |
1580 | ||
1581 | debug_rt_mutex_free_waiter(&q.waiter); | |
1582 | ||
1da177e4 LT |
1583 | /* If we were woken (and unqueued), we succeeded, whatever. */ |
1584 | if (!unqueue_me(&q)) | |
1585 | return 0; | |
c19384b5 | 1586 | if (rem) |
1da177e4 | 1587 | return -ETIMEDOUT; |
72c1bbf3 | 1588 | |
e2970f2f IM |
1589 | /* |
1590 | * We expect signal_pending(current), but another thread may | |
1591 | * have handled it for us already. | |
1592 | */ | |
c19384b5 | 1593 | if (!abs_time) |
72c1bbf3 NP |
1594 | return -ERESTARTSYS; |
1595 | else { | |
1596 | struct restart_block *restart; | |
1597 | restart = ¤t_thread_info()->restart_block; | |
1598 | restart->fn = futex_wait_restart; | |
1599 | restart->arg0 = (unsigned long)uaddr; | |
1600 | restart->arg1 = (unsigned long)val; | |
c19384b5 | 1601 | restart->arg2 = (unsigned long)abs_time; |
34f01cc1 ED |
1602 | restart->arg3 = 0; |
1603 | if (fshared) | |
1604 | restart->arg3 |= ARG3_SHARED; | |
72c1bbf3 NP |
1605 | return -ERESTART_RESTARTBLOCK; |
1606 | } | |
1da177e4 | 1607 | |
c87e2837 IM |
1608 | out_unlock_release_sem: |
1609 | queue_unlock(&q, hb); | |
1610 | ||
1da177e4 | 1611 | out_release_sem: |
34f01cc1 ED |
1612 | if (fshared) |
1613 | up_read(fshared); | |
c87e2837 IM |
1614 | return ret; |
1615 | } | |
1616 | ||
72c1bbf3 NP |
1617 | |
1618 | static long futex_wait_restart(struct restart_block *restart) | |
1619 | { | |
1620 | u32 __user *uaddr = (u32 __user *)restart->arg0; | |
1621 | u32 val = (u32)restart->arg1; | |
c19384b5 | 1622 | ktime_t *abs_time = (ktime_t *)restart->arg2; |
34f01cc1 | 1623 | struct rw_semaphore *fshared = NULL; |
72c1bbf3 NP |
1624 | |
1625 | restart->fn = do_no_restart_syscall; | |
34f01cc1 ED |
1626 | if (restart->arg3 & ARG3_SHARED) |
1627 | fshared = ¤t->mm->mmap_sem; | |
1628 | return (long)futex_wait(uaddr, fshared, val, abs_time); | |
72c1bbf3 NP |
1629 | } |
1630 | ||
1631 | ||
d0aa7a70 PP |
1632 | static 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 | ||
c87e2837 IM |
1677 | /* |
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 | */ | |
34f01cc1 ED |
1683 | static int futex_lock_pi(u32 __user *uaddr, struct rw_semaphore *fshared, |
1684 | int detect, ktime_t *time, int trylock) | |
c87e2837 | 1685 | { |
c5780e97 | 1686 | struct hrtimer_sleeper timeout, *to = NULL; |
c87e2837 IM |
1687 | struct task_struct *curr = current; |
1688 | struct futex_hash_bucket *hb; | |
1689 | u32 uval, newval, curval; | |
1690 | struct futex_q q; | |
d0aa7a70 | 1691 | int ret, lock_held, attempt = 0; |
c87e2837 IM |
1692 | |
1693 | if (refill_pi_state_cache()) | |
1694 | return -ENOMEM; | |
1695 | ||
c19384b5 | 1696 | if (time) { |
c5780e97 | 1697 | to = &timeout; |
c9cb2e3d | 1698 | hrtimer_init(&to->timer, CLOCK_REALTIME, HRTIMER_MODE_ABS); |
c5780e97 | 1699 | hrtimer_init_sleeper(to, current); |
c19384b5 | 1700 | to->timer.expires = *time; |
c5780e97 TG |
1701 | } |
1702 | ||
c87e2837 IM |
1703 | q.pi_state = NULL; |
1704 | retry: | |
34f01cc1 ED |
1705 | if (fshared) |
1706 | down_read(fshared); | |
c87e2837 | 1707 | |
34f01cc1 | 1708 | ret = get_futex_key(uaddr, fshared, &q.key); |
c87e2837 IM |
1709 | if (unlikely(ret != 0)) |
1710 | goto out_release_sem; | |
1711 | ||
1712 | hb = queue_lock(&q, -1, NULL); | |
1713 | ||
1714 | retry_locked: | |
d0aa7a70 PP |
1715 | lock_held = 0; |
1716 | ||
c87e2837 IM |
1717 | /* |
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 | ||
a866374a | 1724 | pagefault_disable(); |
c87e2837 | 1725 | curval = futex_atomic_cmpxchg_inatomic(uaddr, 0, newval); |
a866374a | 1726 | pagefault_enable(); |
c87e2837 IM |
1727 | |
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); | |
d0aa7a70 PP |
1735 | /* |
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; | |
c87e2837 IM |
1745 | 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; | |
d0aa7a70 PP |
1756 | /* |
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; | |
c87e2837 | 1768 | |
a866374a | 1769 | pagefault_disable(); |
c87e2837 | 1770 | curval = futex_atomic_cmpxchg_inatomic(uaddr, uval, newval); |
a866374a | 1771 | pagefault_enable(); |
c87e2837 IM |
1772 | |
1773 | if (unlikely(curval == -EFAULT)) | |
1774 | goto uaddr_faulted; | |
1775 | if (unlikely(curval != uval)) | |
1776 | goto retry_locked; | |
1777 | ||
d0aa7a70 PP |
1778 | if (lock_held) { |
1779 | set_pi_futex_owner(hb, &q.key, curr); | |
1780 | goto out_unlock_release_sem; | |
1781 | } | |
1782 | ||
c87e2837 IM |
1783 | /* |
1784 | * We dont have the lock. Look up the PI state (or create it if | |
1785 | * we are the first waiter): | |
1786 | */ | |
d0aa7a70 | 1787 | ret = lookup_pi_state(uval, hb, &q.key, &q.pi_state); |
c87e2837 IM |
1788 | |
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 | ||
a866374a | 1806 | pagefault_disable(); |
c87e2837 IM |
1807 | curval = futex_atomic_cmpxchg_inatomic(uaddr, |
1808 | uval, newval); | |
a866374a | 1809 | pagefault_enable(); |
c87e2837 IM |
1810 | |
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 | */ | |
34f01cc1 ED |
1829 | if (fshared) |
1830 | up_read(fshared); | |
c87e2837 IM |
1831 | |
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 | ||
34f01cc1 ED |
1844 | if (fshared) |
1845 | down_read(fshared); | |
a99e4e41 | 1846 | spin_lock(q.lock_ptr); |
c87e2837 IM |
1847 | |
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 | */ | |
d0aa7a70 PP |
1852 | if (!ret && q.pi_state->owner != curr) |
1853 | /* mmap_sem is unlocked at return of this function */ | |
34f01cc1 | 1854 | ret = fixup_pi_state_owner(uaddr, fshared, &q, hb, curr); |
d0aa7a70 | 1855 | else { |
c87e2837 IM |
1856 | /* |
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 */ | |
d0aa7a70 | 1866 | unqueue_me_pi(&q); |
34f01cc1 ED |
1867 | if (fshared) |
1868 | up_read(fshared); | |
c87e2837 IM |
1869 | } |
1870 | ||
1871 | if (!detect && ret == -EDEADLK && 0) | |
1872 | force_sig(SIGKILL, current); | |
1873 | ||
c5780e97 | 1874 | return ret != -EINTR ? ret : -ERESTARTNOINTR; |
c87e2837 IM |
1875 | |
1876 | out_unlock_release_sem: | |
1877 | queue_unlock(&q, hb); | |
1878 | ||
1879 | out_release_sem: | |
34f01cc1 ED |
1880 | if (fshared) |
1881 | up_read(fshared); | |
c87e2837 IM |
1882 | 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++) { | |
34f01cc1 ED |
1892 | ret = futex_handle_fault((unsigned long)uaddr, fshared, |
1893 | attempt); | |
1894 | if (ret) | |
c87e2837 | 1895 | goto out_unlock_release_sem; |
c87e2837 IM |
1896 | goto retry_locked; |
1897 | } | |
1898 | ||
1899 | queue_unlock(&q, hb); | |
34f01cc1 ED |
1900 | if (fshared) |
1901 | up_read(fshared); | |
c87e2837 IM |
1902 | |
1903 | ret = get_user(uval, uaddr); | |
1904 | if (!ret && (uval != -EFAULT)) | |
1905 | goto retry; | |
1906 | ||
1907 | return ret; | |
1908 | } | |
1909 | ||
c87e2837 IM |
1910 | /* |
1911 | * 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 | */ | |
34f01cc1 | 1915 | static int futex_unlock_pi(u32 __user *uaddr, struct rw_semaphore *fshared) |
c87e2837 IM |
1916 | { |
1917 | struct futex_hash_bucket *hb; | |
1918 | struct futex_q *this, *next; | |
1919 | u32 uval; | |
ec92d082 | 1920 | struct plist_head *head; |
c87e2837 IM |
1921 | union futex_key key; |
1922 | int ret, attempt = 0; | |
1923 | ||
1924 | retry: | |
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 | */ | |
34f01cc1 ED |
1935 | if (fshared) |
1936 | down_read(fshared); | |
c87e2837 | 1937 | |
34f01cc1 | 1938 | ret = get_futex_key(uaddr, fshared, &key); |
c87e2837 IM |
1939 | if (unlikely(ret != 0)) |
1940 | goto out; | |
1941 | ||
1942 | hb = hash_futex(&key); | |
1943 | spin_lock(&hb->lock); | |
1944 | ||
1945 | retry_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 | */ | |
e3f2ddea | 1951 | if (!(uval & FUTEX_OWNER_DIED)) { |
a866374a | 1952 | pagefault_disable(); |
e3f2ddea | 1953 | uval = futex_atomic_cmpxchg_inatomic(uaddr, current->pid, 0); |
a866374a | 1954 | pagefault_enable(); |
e3f2ddea | 1955 | } |
c87e2837 IM |
1956 | |
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 | ||
ec92d082 | 1972 | plist_for_each_entry_safe(this, next, head, list) { |
c87e2837 IM |
1973 | 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 | */ | |
e3f2ddea IM |
1988 | if (!(uval & FUTEX_OWNER_DIED)) { |
1989 | ret = unlock_futex_pi(uaddr, uval); | |
1990 | if (ret == -EFAULT) | |
1991 | goto pi_faulted; | |
1992 | } | |
c87e2837 IM |
1993 | |
1994 | out_unlock: | |
1995 | spin_unlock(&hb->lock); | |
1996 | out: | |
34f01cc1 ED |
1997 | if (fshared) |
1998 | up_read(fshared); | |
c87e2837 IM |
1999 | |
2000 | return ret; | |
2001 | ||
2002 | pi_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++) { | |
34f01cc1 ED |
2010 | ret = futex_handle_fault((unsigned long)uaddr, fshared, |
2011 | attempt); | |
2012 | if (ret) | |
c87e2837 | 2013 | goto out_unlock; |
c87e2837 IM |
2014 | goto retry_locked; |
2015 | } | |
2016 | ||
2017 | spin_unlock(&hb->lock); | |
34f01cc1 ED |
2018 | if (fshared) |
2019 | up_read(fshared); | |
c87e2837 IM |
2020 | |
2021 | ret = get_user(uval, uaddr); | |
2022 | if (!ret && (uval != -EFAULT)) | |
2023 | goto retry; | |
2024 | ||
1da177e4 LT |
2025 | return ret; |
2026 | } | |
2027 | ||
2028 | static 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); | |
e2970f2f | 2034 | |
1da177e4 LT |
2035 | return 0; |
2036 | } | |
2037 | ||
2038 | /* This is one-shot: once it's gone off you need a new fd */ | |
2039 | static 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 | /* | |
ec92d082 | 2048 | * plist_node_empty() is safe here without any lock. |
1da177e4 LT |
2049 | * q->lock_ptr != 0 is not safe, because of ordering against wakeup. |
2050 | */ | |
ec92d082 | 2051 | if (plist_node_empty(&q->list)) |
1da177e4 LT |
2052 | ret = POLLIN | POLLRDNORM; |
2053 | ||
2054 | return ret; | |
2055 | } | |
2056 | ||
15ad7cdc | 2057 | static const struct file_operations futex_fops = { |
1da177e4 LT |
2058 | .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 | */ | |
e2970f2f | 2066 | static int futex_fd(u32 __user *uaddr, int signal) |
1da177e4 LT |
2067 | { |
2068 | struct futex_q *q; | |
2069 | struct file *filp; | |
2070 | int ret, err; | |
34f01cc1 | 2071 | struct rw_semaphore *fshared; |
19c6b6ed AM |
2072 | 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 | } | |
1da177e4 LT |
2079 | |
2080 | ret = -EINVAL; | |
7ed20e1a | 2081 | if (!valid_signal(signal)) |
1da177e4 LT |
2082 | 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; | |
f3a43f3f JJS |
2094 | 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; | |
1da177e4 LT |
2097 | |
2098 | if (signal) { | |
609d7fa9 | 2099 | err = __f_setown(filp, task_pid(current), PIDTYPE_PID, 1); |
1da177e4 | 2100 | if (err < 0) { |
39ed3fde | 2101 | goto error; |
1da177e4 LT |
2102 | } |
2103 | filp->f_owner.signum = signal; | |
2104 | } | |
2105 | ||
2106 | q = kmalloc(sizeof(*q), GFP_KERNEL); | |
2107 | if (!q) { | |
39ed3fde PE |
2108 | err = -ENOMEM; |
2109 | goto error; | |
1da177e4 | 2110 | } |
c87e2837 | 2111 | q->pi_state = NULL; |
1da177e4 | 2112 | |
34f01cc1 ED |
2113 | fshared = ¤t->mm->mmap_sem; |
2114 | down_read(fshared); | |
2115 | err = get_futex_key(uaddr, fshared, &q->key); | |
1da177e4 LT |
2116 | |
2117 | if (unlikely(err != 0)) { | |
34f01cc1 | 2118 | up_read(fshared); |
1da177e4 | 2119 | kfree(q); |
39ed3fde | 2120 | goto error; |
1da177e4 LT |
2121 | } |
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); | |
34f01cc1 | 2130 | up_read(fshared); |
1da177e4 LT |
2131 | |
2132 | /* Now we map fd to filp, so userspace can access it */ | |
2133 | fd_install(ret, filp); | |
2134 | out: | |
2135 | return ret; | |
39ed3fde PE |
2136 | error: |
2137 | put_unused_fd(ret); | |
2138 | put_filp(filp); | |
2139 | ret = err; | |
2140 | goto out; | |
1da177e4 LT |
2141 | } |
2142 | ||
0771dfef IM |
2143 | /* |
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 | |
c87e2837 | 2150 | * FUTEX_OWNER_DIED bit, and wakes up a waiter (if any). The list is |
0771dfef IM |
2151 | * 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 | */ | |
2163 | asmlinkage long | |
2164 | sys_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 | */ | |
2184 | asmlinkage long | |
ba46df98 | 2185 | sys_get_robust_list(int pid, struct robust_list_head __user * __user *head_ptr, |
0771dfef IM |
2186 | size_t __user *len_ptr) |
2187 | { | |
ba46df98 | 2188 | struct robust_list_head __user *head; |
0771dfef IM |
2189 | unsigned long ret; |
2190 | ||
2191 | if (!pid) | |
2192 | head = current->robust_list; | |
2193 | else { | |
2194 | struct task_struct *p; | |
2195 | ||
2196 | ret = -ESRCH; | |
aaa2a97e | 2197 | rcu_read_lock(); |
0771dfef IM |
2198 | 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; | |
aaa2a97e | 2206 | rcu_read_unlock(); |
0771dfef IM |
2207 | } |
2208 | ||
2209 | if (put_user(sizeof(*head), len_ptr)) | |
2210 | return -EFAULT; | |
2211 | return put_user(head, head_ptr); | |
2212 | ||
2213 | err_unlock: | |
aaa2a97e | 2214 | rcu_read_unlock(); |
0771dfef IM |
2215 | |
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 | */ | |
e3f2ddea | 2223 | int handle_futex_death(u32 __user *uaddr, struct task_struct *curr, int pi) |
0771dfef | 2224 | { |
e3f2ddea | 2225 | u32 uval, nval, mval; |
0771dfef | 2226 | |
8f17d3a5 IM |
2227 | retry: |
2228 | if (get_user(uval, uaddr)) | |
0771dfef IM |
2229 | return -1; |
2230 | ||
8f17d3a5 | 2231 | if ((uval & FUTEX_TID_MASK) == curr->pid) { |
0771dfef IM |
2232 | /* |
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 | */ | |
e3f2ddea | 2242 | mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED; |
d0aa7a70 PP |
2243 | /* Also keep the FUTEX_WAITER_REQUEUED flag if set */ |
2244 | mval |= (uval & FUTEX_WAITER_REQUEUED); | |
e3f2ddea IM |
2245 | nval = futex_atomic_cmpxchg_inatomic(uaddr, uval, mval); |
2246 | ||
c87e2837 IM |
2247 | if (nval == -EFAULT) |
2248 | return -1; | |
2249 | ||
2250 | if (nval != uval) | |
8f17d3a5 | 2251 | goto retry; |
0771dfef | 2252 | |
e3f2ddea IM |
2253 | /* |
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) | |
34f01cc1 | 2259 | futex_wake(uaddr, &curr->mm->mmap_sem, 1); |
e3f2ddea | 2260 | } |
0771dfef IM |
2261 | } |
2262 | return 0; | |
2263 | } | |
2264 | ||
e3f2ddea IM |
2265 | /* |
2266 | * Fetch a robust-list pointer. Bit 0 signals PI futexes: | |
2267 | */ | |
2268 | static inline int fetch_robust_entry(struct robust_list __user **entry, | |
ba46df98 AV |
2269 | struct robust_list __user * __user *head, |
2270 | int *pi) | |
e3f2ddea IM |
2271 | { |
2272 | unsigned long uentry; | |
2273 | ||
ba46df98 | 2274 | if (get_user(uentry, (unsigned long __user *)head)) |
e3f2ddea IM |
2275 | return -EFAULT; |
2276 | ||
ba46df98 | 2277 | *entry = (void __user *)(uentry & ~1UL); |
e3f2ddea IM |
2278 | *pi = uentry & 1; |
2279 | ||
2280 | return 0; | |
2281 | } | |
2282 | ||
0771dfef IM |
2283 | /* |
2284 | * 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 | */ | |
2289 | void 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; | |
e3f2ddea | 2293 | unsigned int limit = ROBUST_LIST_LIMIT, pi, pip; |
0771dfef IM |
2294 | unsigned long futex_offset; |
2295 | ||
2296 | /* | |
2297 | * Fetch the list head (which was registered earlier, via | |
2298 | * sys_set_robust_list()): | |
2299 | */ | |
e3f2ddea | 2300 | if (fetch_robust_entry(&entry, &head->list.next, &pi)) |
0771dfef IM |
2301 | 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 | */ | |
e3f2ddea | 2311 | if (fetch_robust_entry(&pending, &head->list_op_pending, &pip)) |
0771dfef | 2312 | return; |
e3f2ddea | 2313 | |
0771dfef | 2314 | if (pending) |
34f01cc1 ED |
2315 | handle_futex_death((void __user *)pending + futex_offset, |
2316 | curr, pip); | |
0771dfef IM |
2317 | |
2318 | while (entry != &head->list) { | |
2319 | /* | |
2320 | * A pending lock might already be on the list, so | |
c87e2837 | 2321 | * don't process it twice: |
0771dfef IM |
2322 | */ |
2323 | if (entry != pending) | |
ba46df98 | 2324 | if (handle_futex_death((void __user *)entry + futex_offset, |
e3f2ddea | 2325 | curr, pi)) |
0771dfef | 2326 | return; |
0771dfef IM |
2327 | /* |
2328 | * Fetch the next entry in the list: | |
2329 | */ | |
e3f2ddea | 2330 | if (fetch_robust_entry(&entry, &entry->next, &pi)) |
0771dfef IM |
2331 | return; |
2332 | /* | |
2333 | * Avoid excessively long or circular lists: | |
2334 | */ | |
2335 | if (!--limit) | |
2336 | break; | |
2337 | ||
2338 | cond_resched(); | |
2339 | } | |
2340 | } | |
2341 | ||
c19384b5 | 2342 | long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout, |
e2970f2f | 2343 | u32 __user *uaddr2, u32 val2, u32 val3) |
1da177e4 LT |
2344 | { |
2345 | int ret; | |
34f01cc1 ED |
2346 | int cmd = op & FUTEX_CMD_MASK; |
2347 | struct rw_semaphore *fshared = NULL; | |
2348 | ||
2349 | if (!(op & FUTEX_PRIVATE_FLAG)) | |
2350 | fshared = ¤t->mm->mmap_sem; | |
1da177e4 | 2351 | |
34f01cc1 | 2352 | switch (cmd) { |
1da177e4 | 2353 | case FUTEX_WAIT: |
34f01cc1 | 2354 | ret = futex_wait(uaddr, fshared, val, timeout); |
1da177e4 LT |
2355 | break; |
2356 | case FUTEX_WAKE: | |
34f01cc1 | 2357 | ret = futex_wake(uaddr, fshared, val); |
1da177e4 LT |
2358 | 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: | |
34f01cc1 | 2364 | ret = futex_requeue(uaddr, fshared, uaddr2, val, val2, NULL); |
1da177e4 LT |
2365 | break; |
2366 | case FUTEX_CMP_REQUEUE: | |
34f01cc1 | 2367 | ret = futex_requeue(uaddr, fshared, uaddr2, val, val2, &val3); |
1da177e4 | 2368 | break; |
4732efbe | 2369 | case FUTEX_WAKE_OP: |
34f01cc1 | 2370 | ret = futex_wake_op(uaddr, fshared, uaddr2, val, val2, val3); |
4732efbe | 2371 | break; |
c87e2837 | 2372 | case FUTEX_LOCK_PI: |
34f01cc1 | 2373 | ret = futex_lock_pi(uaddr, fshared, val, timeout, 0); |
c87e2837 IM |
2374 | break; |
2375 | case FUTEX_UNLOCK_PI: | |
34f01cc1 | 2376 | ret = futex_unlock_pi(uaddr, fshared); |
c87e2837 IM |
2377 | break; |
2378 | case FUTEX_TRYLOCK_PI: | |
34f01cc1 | 2379 | ret = futex_lock_pi(uaddr, fshared, 0, timeout, 1); |
c87e2837 | 2380 | break; |
d0aa7a70 | 2381 | case FUTEX_CMP_REQUEUE_PI: |
34f01cc1 | 2382 | ret = futex_requeue_pi(uaddr, fshared, uaddr2, val, val2, &val3); |
d0aa7a70 | 2383 | break; |
1da177e4 LT |
2384 | default: |
2385 | ret = -ENOSYS; | |
2386 | } | |
2387 | return ret; | |
2388 | } | |
2389 | ||
2390 | ||
e2970f2f | 2391 | asmlinkage long sys_futex(u32 __user *uaddr, int op, u32 val, |
1da177e4 | 2392 | struct timespec __user *utime, u32 __user *uaddr2, |
e2970f2f | 2393 | u32 val3) |
1da177e4 | 2394 | { |
c19384b5 PP |
2395 | struct timespec ts; |
2396 | ktime_t t, *tp = NULL; | |
e2970f2f | 2397 | u32 val2 = 0; |
34f01cc1 | 2398 | int cmd = op & FUTEX_CMD_MASK; |
1da177e4 | 2399 | |
34f01cc1 | 2400 | if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI)) { |
c19384b5 | 2401 | if (copy_from_user(&ts, utime, sizeof(ts)) != 0) |
1da177e4 | 2402 | return -EFAULT; |
c19384b5 | 2403 | if (!timespec_valid(&ts)) |
9741ef96 | 2404 | return -EINVAL; |
c19384b5 PP |
2405 | |
2406 | t = timespec_to_ktime(ts); | |
34f01cc1 | 2407 | if (cmd == FUTEX_WAIT) |
c19384b5 PP |
2408 | t = ktime_add(ktime_get(), t); |
2409 | tp = &t; | |
1da177e4 LT |
2410 | } |
2411 | /* | |
34f01cc1 | 2412 | * requeue parameter in 'utime' if cmd == FUTEX_REQUEUE. |
1da177e4 | 2413 | */ |
34f01cc1 ED |
2414 | if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE |
2415 | || cmd == FUTEX_CMP_REQUEUE_PI) | |
e2970f2f | 2416 | val2 = (u32) (unsigned long) utime; |
1da177e4 | 2417 | |
c19384b5 | 2418 | return do_futex(uaddr, op, val, tp, uaddr2, val2, val3); |
1da177e4 LT |
2419 | } |
2420 | ||
454e2398 DH |
2421 | static int futexfs_get_sb(struct file_system_type *fs_type, |
2422 | int flags, const char *dev_name, void *data, | |
2423 | struct vfsmount *mnt) | |
1da177e4 | 2424 | { |
454e2398 | 2425 | return get_sb_pseudo(fs_type, "futex", NULL, 0xBAD1DEA, mnt); |
1da177e4 LT |
2426 | } |
2427 | ||
2428 | static struct file_system_type futex_fs_type = { | |
2429 | .name = "futexfs", | |
2430 | .get_sb = futexfs_get_sb, | |
2431 | .kill_sb = kill_anon_super, | |
2432 | }; | |
2433 | ||
2434 | static int __init init(void) | |
2435 | { | |
95362fa9 AM |
2436 | int i = register_filesystem(&futex_fs_type); |
2437 | ||
2438 | if (i) | |
2439 | return i; | |
1da177e4 | 2440 | |
1da177e4 | 2441 | futex_mnt = kern_mount(&futex_fs_type); |
95362fa9 AM |
2442 | if (IS_ERR(futex_mnt)) { |
2443 | unregister_filesystem(&futex_fs_type); | |
2444 | return PTR_ERR(futex_mnt); | |
2445 | } | |
1da177e4 LT |
2446 | |
2447 | for (i = 0; i < ARRAY_SIZE(futex_queues); i++) { | |
ec92d082 | 2448 | plist_head_init(&futex_queues[i].chain, &futex_queues[i].lock); |
1da177e4 LT |
2449 | spin_lock_init(&futex_queues[i].lock); |
2450 | } | |
2451 | return 0; | |
2452 | } | |
2453 | __initcall(init); |