]>
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 | ||
1da177e4 LT |
59 | #define FUTEX_HASHBITS (CONFIG_BASE_SMALL ? 4 : 8) |
60 | ||
c87e2837 IM |
61 | /* |
62 | * Priority Inheritance state: | |
63 | */ | |
64 | struct futex_pi_state { | |
65 | /* | |
66 | * list of 'owned' pi_state instances - these have to be | |
67 | * cleaned up in do_exit() if the task exits prematurely: | |
68 | */ | |
69 | struct list_head list; | |
70 | ||
71 | /* | |
72 | * The PI object: | |
73 | */ | |
74 | struct rt_mutex pi_mutex; | |
75 | ||
76 | struct task_struct *owner; | |
77 | atomic_t refcount; | |
78 | ||
79 | union futex_key key; | |
80 | }; | |
81 | ||
1da177e4 LT |
82 | /* |
83 | * We use this hashed waitqueue instead of a normal wait_queue_t, so | |
84 | * we can wake only the relevant ones (hashed queues may be shared). | |
85 | * | |
86 | * A futex_q has a woken state, just like tasks have TASK_RUNNING. | |
ec92d082 | 87 | * It is considered woken when plist_node_empty(&q->list) || q->lock_ptr == 0. |
1da177e4 LT |
88 | * The order of wakup is always to make the first condition true, then |
89 | * wake up q->waiters, then make the second condition true. | |
90 | */ | |
91 | struct futex_q { | |
ec92d082 | 92 | struct plist_node list; |
1da177e4 LT |
93 | wait_queue_head_t waiters; |
94 | ||
e2970f2f | 95 | /* Which hash list lock to use: */ |
1da177e4 LT |
96 | spinlock_t *lock_ptr; |
97 | ||
e2970f2f | 98 | /* Key which the futex is hashed on: */ |
1da177e4 LT |
99 | union futex_key key; |
100 | ||
e2970f2f | 101 | /* For fd, sigio sent using these: */ |
1da177e4 LT |
102 | int fd; |
103 | struct file *filp; | |
c87e2837 IM |
104 | |
105 | /* Optional priority inheritance state: */ | |
106 | struct futex_pi_state *pi_state; | |
107 | struct task_struct *task; | |
1da177e4 LT |
108 | }; |
109 | ||
110 | /* | |
111 | * Split the global futex_lock into every hash list lock. | |
112 | */ | |
113 | struct futex_hash_bucket { | |
ec92d082 PP |
114 | spinlock_t lock; |
115 | struct plist_head chain; | |
1da177e4 LT |
116 | }; |
117 | ||
118 | static struct futex_hash_bucket futex_queues[1<<FUTEX_HASHBITS]; | |
119 | ||
120 | /* Futex-fs vfsmount entry: */ | |
121 | static struct vfsmount *futex_mnt; | |
122 | ||
123 | /* | |
124 | * We hash on the keys returned from get_futex_key (see below). | |
125 | */ | |
126 | static struct futex_hash_bucket *hash_futex(union futex_key *key) | |
127 | { | |
128 | u32 hash = jhash2((u32*)&key->both.word, | |
129 | (sizeof(key->both.word)+sizeof(key->both.ptr))/4, | |
130 | key->both.offset); | |
131 | return &futex_queues[hash & ((1 << FUTEX_HASHBITS)-1)]; | |
132 | } | |
133 | ||
134 | /* | |
135 | * Return 1 if two futex_keys are equal, 0 otherwise. | |
136 | */ | |
137 | static inline int match_futex(union futex_key *key1, union futex_key *key2) | |
138 | { | |
139 | return (key1->both.word == key2->both.word | |
140 | && key1->both.ptr == key2->both.ptr | |
141 | && key1->both.offset == key2->both.offset); | |
142 | } | |
143 | ||
34f01cc1 ED |
144 | /** |
145 | * get_futex_key - Get parameters which are the keys for a futex. | |
146 | * @uaddr: virtual address of the futex | |
147 | * @shared: NULL for a PROCESS_PRIVATE futex, | |
148 | * ¤t->mm->mmap_sem for a PROCESS_SHARED futex | |
149 | * @key: address where result is stored. | |
150 | * | |
151 | * Returns a negative error code or 0 | |
152 | * The key words are stored in *key on success. | |
1da177e4 | 153 | * |
f3a43f3f | 154 | * For shared mappings, it's (page->index, vma->vm_file->f_path.dentry->d_inode, |
1da177e4 LT |
155 | * offset_within_page). For private mappings, it's (uaddr, current->mm). |
156 | * We can usually work out the index without swapping in the page. | |
157 | * | |
34f01cc1 ED |
158 | * fshared is NULL for PROCESS_PRIVATE futexes |
159 | * For other futexes, it points to ¤t->mm->mmap_sem and | |
160 | * caller must have taken the reader lock. but NOT any spinlocks. | |
1da177e4 | 161 | */ |
34f01cc1 ED |
162 | int get_futex_key(u32 __user *uaddr, struct rw_semaphore *fshared, |
163 | union futex_key *key) | |
1da177e4 | 164 | { |
e2970f2f | 165 | unsigned long address = (unsigned long)uaddr; |
1da177e4 LT |
166 | struct mm_struct *mm = current->mm; |
167 | struct vm_area_struct *vma; | |
168 | struct page *page; | |
169 | int err; | |
170 | ||
171 | /* | |
172 | * The futex address must be "naturally" aligned. | |
173 | */ | |
e2970f2f | 174 | key->both.offset = address % PAGE_SIZE; |
34f01cc1 | 175 | if (unlikely((address % sizeof(u32)) != 0)) |
1da177e4 | 176 | return -EINVAL; |
e2970f2f | 177 | address -= key->both.offset; |
1da177e4 | 178 | |
34f01cc1 ED |
179 | /* |
180 | * PROCESS_PRIVATE futexes are fast. | |
181 | * As the mm cannot disappear under us and the 'key' only needs | |
182 | * virtual address, we dont even have to find the underlying vma. | |
183 | * Note : We do have to check 'uaddr' is a valid user address, | |
184 | * but access_ok() should be faster than find_vma() | |
185 | */ | |
186 | if (!fshared) { | |
187 | if (unlikely(!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))) | |
188 | return -EFAULT; | |
189 | key->private.mm = mm; | |
190 | key->private.address = address; | |
191 | return 0; | |
192 | } | |
1da177e4 LT |
193 | /* |
194 | * The futex is hashed differently depending on whether | |
195 | * it's in a shared or private mapping. So check vma first. | |
196 | */ | |
e2970f2f | 197 | vma = find_extend_vma(mm, address); |
1da177e4 LT |
198 | if (unlikely(!vma)) |
199 | return -EFAULT; | |
200 | ||
201 | /* | |
202 | * Permissions. | |
203 | */ | |
204 | if (unlikely((vma->vm_flags & (VM_IO|VM_READ)) != VM_READ)) | |
205 | return (vma->vm_flags & VM_IO) ? -EPERM : -EACCES; | |
206 | ||
207 | /* | |
208 | * Private mappings are handled in a simple way. | |
209 | * | |
210 | * NOTE: When userspace waits on a MAP_SHARED mapping, even if | |
211 | * it's a read-only handle, it's expected that futexes attach to | |
212 | * the object not the particular process. Therefore we use | |
213 | * VM_MAYSHARE here, not VM_SHARED which is restricted to shared | |
214 | * mappings of _writable_ handles. | |
215 | */ | |
216 | if (likely(!(vma->vm_flags & VM_MAYSHARE))) { | |
34f01cc1 | 217 | key->both.offset |= FUT_OFF_MMSHARED; /* reference taken on mm */ |
1da177e4 | 218 | key->private.mm = mm; |
e2970f2f | 219 | key->private.address = address; |
1da177e4 LT |
220 | return 0; |
221 | } | |
222 | ||
223 | /* | |
224 | * Linear file mappings are also simple. | |
225 | */ | |
f3a43f3f | 226 | key->shared.inode = vma->vm_file->f_path.dentry->d_inode; |
34f01cc1 | 227 | key->both.offset |= FUT_OFF_INODE; /* inode-based key. */ |
1da177e4 | 228 | if (likely(!(vma->vm_flags & VM_NONLINEAR))) { |
e2970f2f | 229 | key->shared.pgoff = (((address - vma->vm_start) >> PAGE_SHIFT) |
1da177e4 LT |
230 | + vma->vm_pgoff); |
231 | return 0; | |
232 | } | |
233 | ||
234 | /* | |
235 | * We could walk the page table to read the non-linear | |
236 | * pte, and get the page index without fetching the page | |
237 | * from swap. But that's a lot of code to duplicate here | |
238 | * for a rare case, so we simply fetch the page. | |
239 | */ | |
e2970f2f | 240 | err = get_user_pages(current, mm, address, 1, 0, 0, &page, NULL); |
1da177e4 LT |
241 | if (err >= 0) { |
242 | key->shared.pgoff = | |
243 | page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT); | |
244 | put_page(page); | |
245 | return 0; | |
246 | } | |
247 | return err; | |
248 | } | |
9adef58b | 249 | EXPORT_SYMBOL_GPL(get_futex_key); |
1da177e4 LT |
250 | |
251 | /* | |
252 | * Take a reference to the resource addressed by a key. | |
253 | * Can be called while holding spinlocks. | |
254 | * | |
1da177e4 | 255 | */ |
9adef58b | 256 | inline void get_futex_key_refs(union futex_key *key) |
1da177e4 | 257 | { |
34f01cc1 ED |
258 | if (key->both.ptr == 0) |
259 | return; | |
260 | switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) { | |
261 | case FUT_OFF_INODE: | |
1da177e4 | 262 | atomic_inc(&key->shared.inode->i_count); |
34f01cc1 ED |
263 | break; |
264 | case FUT_OFF_MMSHARED: | |
1da177e4 | 265 | atomic_inc(&key->private.mm->mm_count); |
34f01cc1 | 266 | break; |
1da177e4 LT |
267 | } |
268 | } | |
9adef58b | 269 | EXPORT_SYMBOL_GPL(get_futex_key_refs); |
1da177e4 LT |
270 | |
271 | /* | |
272 | * Drop a reference to the resource addressed by a key. | |
273 | * The hash bucket spinlock must not be held. | |
274 | */ | |
9adef58b | 275 | void drop_futex_key_refs(union futex_key *key) |
1da177e4 | 276 | { |
34f01cc1 ED |
277 | if (key->both.ptr == 0) |
278 | return; | |
279 | switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) { | |
280 | case FUT_OFF_INODE: | |
1da177e4 | 281 | iput(key->shared.inode); |
34f01cc1 ED |
282 | break; |
283 | case FUT_OFF_MMSHARED: | |
1da177e4 | 284 | mmdrop(key->private.mm); |
34f01cc1 | 285 | break; |
1da177e4 LT |
286 | } |
287 | } | |
9adef58b | 288 | EXPORT_SYMBOL_GPL(drop_futex_key_refs); |
1da177e4 | 289 | |
e2970f2f | 290 | static inline int get_futex_value_locked(u32 *dest, u32 __user *from) |
1da177e4 LT |
291 | { |
292 | int ret; | |
293 | ||
a866374a | 294 | pagefault_disable(); |
e2970f2f | 295 | ret = __copy_from_user_inatomic(dest, from, sizeof(u32)); |
a866374a | 296 | pagefault_enable(); |
1da177e4 LT |
297 | |
298 | return ret ? -EFAULT : 0; | |
299 | } | |
300 | ||
c87e2837 | 301 | /* |
34f01cc1 ED |
302 | * Fault handling. |
303 | * if fshared is non NULL, current->mm->mmap_sem is already held | |
c87e2837 | 304 | */ |
34f01cc1 ED |
305 | static int futex_handle_fault(unsigned long address, |
306 | struct rw_semaphore *fshared, int attempt) | |
c87e2837 IM |
307 | { |
308 | struct vm_area_struct * vma; | |
309 | struct mm_struct *mm = current->mm; | |
34f01cc1 | 310 | int ret = -EFAULT; |
c87e2837 | 311 | |
34f01cc1 ED |
312 | if (attempt > 2) |
313 | return ret; | |
c87e2837 | 314 | |
34f01cc1 ED |
315 | if (!fshared) |
316 | down_read(&mm->mmap_sem); | |
317 | vma = find_vma(mm, address); | |
318 | if (vma && address >= vma->vm_start && | |
319 | (vma->vm_flags & VM_WRITE)) { | |
320 | switch (handle_mm_fault(mm, vma, address, 1)) { | |
321 | case VM_FAULT_MINOR: | |
322 | ret = 0; | |
323 | current->min_flt++; | |
324 | break; | |
325 | case VM_FAULT_MAJOR: | |
326 | ret = 0; | |
327 | current->maj_flt++; | |
328 | break; | |
329 | } | |
c87e2837 | 330 | } |
34f01cc1 ED |
331 | if (!fshared) |
332 | up_read(&mm->mmap_sem); | |
333 | return ret; | |
c87e2837 IM |
334 | } |
335 | ||
336 | /* | |
337 | * PI code: | |
338 | */ | |
339 | static int refill_pi_state_cache(void) | |
340 | { | |
341 | struct futex_pi_state *pi_state; | |
342 | ||
343 | if (likely(current->pi_state_cache)) | |
344 | return 0; | |
345 | ||
4668edc3 | 346 | pi_state = kzalloc(sizeof(*pi_state), GFP_KERNEL); |
c87e2837 IM |
347 | |
348 | if (!pi_state) | |
349 | return -ENOMEM; | |
350 | ||
c87e2837 IM |
351 | INIT_LIST_HEAD(&pi_state->list); |
352 | /* pi_mutex gets initialized later */ | |
353 | pi_state->owner = NULL; | |
354 | atomic_set(&pi_state->refcount, 1); | |
355 | ||
356 | current->pi_state_cache = pi_state; | |
357 | ||
358 | return 0; | |
359 | } | |
360 | ||
361 | static struct futex_pi_state * alloc_pi_state(void) | |
362 | { | |
363 | struct futex_pi_state *pi_state = current->pi_state_cache; | |
364 | ||
365 | WARN_ON(!pi_state); | |
366 | current->pi_state_cache = NULL; | |
367 | ||
368 | return pi_state; | |
369 | } | |
370 | ||
371 | static void free_pi_state(struct futex_pi_state *pi_state) | |
372 | { | |
373 | if (!atomic_dec_and_test(&pi_state->refcount)) | |
374 | return; | |
375 | ||
376 | /* | |
377 | * If pi_state->owner is NULL, the owner is most probably dying | |
378 | * and has cleaned up the pi_state already | |
379 | */ | |
380 | if (pi_state->owner) { | |
381 | spin_lock_irq(&pi_state->owner->pi_lock); | |
382 | list_del_init(&pi_state->list); | |
383 | spin_unlock_irq(&pi_state->owner->pi_lock); | |
384 | ||
385 | rt_mutex_proxy_unlock(&pi_state->pi_mutex, pi_state->owner); | |
386 | } | |
387 | ||
388 | if (current->pi_state_cache) | |
389 | kfree(pi_state); | |
390 | else { | |
391 | /* | |
392 | * pi_state->list is already empty. | |
393 | * clear pi_state->owner. | |
394 | * refcount is at 0 - put it back to 1. | |
395 | */ | |
396 | pi_state->owner = NULL; | |
397 | atomic_set(&pi_state->refcount, 1); | |
398 | current->pi_state_cache = pi_state; | |
399 | } | |
400 | } | |
401 | ||
402 | /* | |
403 | * Look up the task based on what TID userspace gave us. | |
404 | * We dont trust it. | |
405 | */ | |
406 | static struct task_struct * futex_find_get_task(pid_t pid) | |
407 | { | |
408 | struct task_struct *p; | |
409 | ||
d359b549 | 410 | rcu_read_lock(); |
c87e2837 | 411 | p = find_task_by_pid(pid); |
a06381fe TG |
412 | |
413 | if (!p || ((current->euid != p->euid) && (current->euid != p->uid))) | |
414 | p = ERR_PTR(-ESRCH); | |
415 | else | |
416 | get_task_struct(p); | |
417 | ||
d359b549 | 418 | rcu_read_unlock(); |
c87e2837 IM |
419 | |
420 | return p; | |
421 | } | |
422 | ||
423 | /* | |
424 | * This task is holding PI mutexes at exit time => bad. | |
425 | * Kernel cleans up PI-state, but userspace is likely hosed. | |
426 | * (Robust-futex cleanup is separate and might save the day for userspace.) | |
427 | */ | |
428 | void exit_pi_state_list(struct task_struct *curr) | |
429 | { | |
c87e2837 IM |
430 | struct list_head *next, *head = &curr->pi_state_list; |
431 | struct futex_pi_state *pi_state; | |
627371d7 | 432 | struct futex_hash_bucket *hb; |
c87e2837 IM |
433 | union futex_key key; |
434 | ||
435 | /* | |
436 | * We are a ZOMBIE and nobody can enqueue itself on | |
437 | * pi_state_list anymore, but we have to be careful | |
627371d7 | 438 | * versus waiters unqueueing themselves: |
c87e2837 IM |
439 | */ |
440 | spin_lock_irq(&curr->pi_lock); | |
441 | while (!list_empty(head)) { | |
442 | ||
443 | next = head->next; | |
444 | pi_state = list_entry(next, struct futex_pi_state, list); | |
445 | key = pi_state->key; | |
627371d7 | 446 | hb = hash_futex(&key); |
c87e2837 IM |
447 | spin_unlock_irq(&curr->pi_lock); |
448 | ||
c87e2837 IM |
449 | spin_lock(&hb->lock); |
450 | ||
451 | spin_lock_irq(&curr->pi_lock); | |
627371d7 IM |
452 | /* |
453 | * We dropped the pi-lock, so re-check whether this | |
454 | * task still owns the PI-state: | |
455 | */ | |
c87e2837 IM |
456 | if (head->next != next) { |
457 | spin_unlock(&hb->lock); | |
458 | continue; | |
459 | } | |
460 | ||
c87e2837 | 461 | WARN_ON(pi_state->owner != curr); |
627371d7 IM |
462 | WARN_ON(list_empty(&pi_state->list)); |
463 | list_del_init(&pi_state->list); | |
c87e2837 IM |
464 | pi_state->owner = NULL; |
465 | spin_unlock_irq(&curr->pi_lock); | |
466 | ||
467 | rt_mutex_unlock(&pi_state->pi_mutex); | |
468 | ||
469 | spin_unlock(&hb->lock); | |
470 | ||
471 | spin_lock_irq(&curr->pi_lock); | |
472 | } | |
473 | spin_unlock_irq(&curr->pi_lock); | |
474 | } | |
475 | ||
476 | static int | |
d0aa7a70 PP |
477 | lookup_pi_state(u32 uval, struct futex_hash_bucket *hb, |
478 | union futex_key *key, struct futex_pi_state **ps) | |
c87e2837 IM |
479 | { |
480 | struct futex_pi_state *pi_state = NULL; | |
481 | struct futex_q *this, *next; | |
ec92d082 | 482 | struct plist_head *head; |
c87e2837 | 483 | struct task_struct *p; |
778e9a9c | 484 | pid_t pid = uval & FUTEX_TID_MASK; |
c87e2837 IM |
485 | |
486 | head = &hb->chain; | |
487 | ||
ec92d082 | 488 | plist_for_each_entry_safe(this, next, head, list) { |
d0aa7a70 | 489 | if (match_futex(&this->key, key)) { |
c87e2837 IM |
490 | /* |
491 | * Another waiter already exists - bump up | |
492 | * the refcount and return its pi_state: | |
493 | */ | |
494 | pi_state = this->pi_state; | |
06a9ec29 TG |
495 | /* |
496 | * Userspace might have messed up non PI and PI futexes | |
497 | */ | |
498 | if (unlikely(!pi_state)) | |
499 | return -EINVAL; | |
500 | ||
627371d7 | 501 | WARN_ON(!atomic_read(&pi_state->refcount)); |
778e9a9c AK |
502 | WARN_ON(pid && pi_state->owner && |
503 | pi_state->owner->pid != pid); | |
627371d7 | 504 | |
c87e2837 | 505 | atomic_inc(&pi_state->refcount); |
d0aa7a70 | 506 | *ps = pi_state; |
c87e2837 IM |
507 | |
508 | return 0; | |
509 | } | |
510 | } | |
511 | ||
512 | /* | |
e3f2ddea | 513 | * We are the first waiter - try to look up the real owner and attach |
778e9a9c | 514 | * the new pi_state to it, but bail out when TID = 0 |
c87e2837 | 515 | */ |
778e9a9c | 516 | if (!pid) |
e3f2ddea | 517 | return -ESRCH; |
c87e2837 | 518 | p = futex_find_get_task(pid); |
778e9a9c AK |
519 | if (IS_ERR(p)) |
520 | return PTR_ERR(p); | |
521 | ||
522 | /* | |
523 | * We need to look at the task state flags to figure out, | |
524 | * whether the task is exiting. To protect against the do_exit | |
525 | * change of the task flags, we do this protected by | |
526 | * p->pi_lock: | |
527 | */ | |
528 | spin_lock_irq(&p->pi_lock); | |
529 | if (unlikely(p->flags & PF_EXITING)) { | |
530 | /* | |
531 | * The task is on the way out. When PF_EXITPIDONE is | |
532 | * set, we know that the task has finished the | |
533 | * cleanup: | |
534 | */ | |
535 | int ret = (p->flags & PF_EXITPIDONE) ? -ESRCH : -EAGAIN; | |
536 | ||
537 | spin_unlock_irq(&p->pi_lock); | |
538 | put_task_struct(p); | |
539 | return ret; | |
540 | } | |
c87e2837 IM |
541 | |
542 | pi_state = alloc_pi_state(); | |
543 | ||
544 | /* | |
545 | * Initialize the pi_mutex in locked state and make 'p' | |
546 | * the owner of it: | |
547 | */ | |
548 | rt_mutex_init_proxy_locked(&pi_state->pi_mutex, p); | |
549 | ||
550 | /* Store the key for possible exit cleanups: */ | |
d0aa7a70 | 551 | pi_state->key = *key; |
c87e2837 | 552 | |
627371d7 | 553 | WARN_ON(!list_empty(&pi_state->list)); |
c87e2837 IM |
554 | list_add(&pi_state->list, &p->pi_state_list); |
555 | pi_state->owner = p; | |
556 | spin_unlock_irq(&p->pi_lock); | |
557 | ||
558 | put_task_struct(p); | |
559 | ||
d0aa7a70 | 560 | *ps = pi_state; |
c87e2837 IM |
561 | |
562 | return 0; | |
563 | } | |
564 | ||
1da177e4 LT |
565 | /* |
566 | * The hash bucket lock must be held when this is called. | |
567 | * Afterwards, the futex_q must not be accessed. | |
568 | */ | |
569 | static void wake_futex(struct futex_q *q) | |
570 | { | |
ec92d082 | 571 | plist_del(&q->list, &q->list.plist); |
1da177e4 LT |
572 | if (q->filp) |
573 | send_sigio(&q->filp->f_owner, q->fd, POLL_IN); | |
574 | /* | |
575 | * The lock in wake_up_all() is a crucial memory barrier after the | |
ec92d082 | 576 | * plist_del() and also before assigning to q->lock_ptr. |
1da177e4 LT |
577 | */ |
578 | wake_up_all(&q->waiters); | |
579 | /* | |
580 | * The waiting task can free the futex_q as soon as this is written, | |
581 | * without taking any locks. This must come last. | |
8e31108b AM |
582 | * |
583 | * A memory barrier is required here to prevent the following store | |
584 | * to lock_ptr from getting ahead of the wakeup. Clearing the lock | |
585 | * at the end of wake_up_all() does not prevent this store from | |
586 | * moving. | |
1da177e4 | 587 | */ |
ccdea2f8 | 588 | smp_wmb(); |
1da177e4 LT |
589 | q->lock_ptr = NULL; |
590 | } | |
591 | ||
c87e2837 IM |
592 | static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this) |
593 | { | |
594 | struct task_struct *new_owner; | |
595 | struct futex_pi_state *pi_state = this->pi_state; | |
596 | u32 curval, newval; | |
597 | ||
598 | if (!pi_state) | |
599 | return -EINVAL; | |
600 | ||
21778867 | 601 | spin_lock(&pi_state->pi_mutex.wait_lock); |
c87e2837 IM |
602 | new_owner = rt_mutex_next_owner(&pi_state->pi_mutex); |
603 | ||
604 | /* | |
605 | * This happens when we have stolen the lock and the original | |
606 | * pending owner did not enqueue itself back on the rt_mutex. | |
607 | * Thats not a tragedy. We know that way, that a lock waiter | |
608 | * is on the fly. We make the futex_q waiter the pending owner. | |
609 | */ | |
610 | if (!new_owner) | |
611 | new_owner = this->task; | |
612 | ||
613 | /* | |
614 | * We pass it to the next owner. (The WAITERS bit is always | |
615 | * kept enabled while there is PI state around. We must also | |
616 | * preserve the owner died bit.) | |
617 | */ | |
e3f2ddea | 618 | if (!(uval & FUTEX_OWNER_DIED)) { |
778e9a9c AK |
619 | int ret = 0; |
620 | ||
e3f2ddea IM |
621 | newval = FUTEX_WAITERS | new_owner->pid; |
622 | ||
a866374a | 623 | pagefault_disable(); |
e3f2ddea | 624 | curval = futex_atomic_cmpxchg_inatomic(uaddr, uval, newval); |
a866374a | 625 | pagefault_enable(); |
778e9a9c | 626 | |
e3f2ddea | 627 | if (curval == -EFAULT) |
778e9a9c | 628 | ret = -EFAULT; |
e3f2ddea | 629 | if (curval != uval) |
778e9a9c AK |
630 | ret = -EINVAL; |
631 | if (ret) { | |
632 | spin_unlock(&pi_state->pi_mutex.wait_lock); | |
633 | return ret; | |
634 | } | |
e3f2ddea | 635 | } |
c87e2837 | 636 | |
627371d7 IM |
637 | spin_lock_irq(&pi_state->owner->pi_lock); |
638 | WARN_ON(list_empty(&pi_state->list)); | |
639 | list_del_init(&pi_state->list); | |
640 | spin_unlock_irq(&pi_state->owner->pi_lock); | |
641 | ||
642 | spin_lock_irq(&new_owner->pi_lock); | |
643 | WARN_ON(!list_empty(&pi_state->list)); | |
c87e2837 IM |
644 | list_add(&pi_state->list, &new_owner->pi_state_list); |
645 | pi_state->owner = new_owner; | |
627371d7 IM |
646 | spin_unlock_irq(&new_owner->pi_lock); |
647 | ||
21778867 | 648 | spin_unlock(&pi_state->pi_mutex.wait_lock); |
c87e2837 IM |
649 | rt_mutex_unlock(&pi_state->pi_mutex); |
650 | ||
651 | return 0; | |
652 | } | |
653 | ||
654 | static int unlock_futex_pi(u32 __user *uaddr, u32 uval) | |
655 | { | |
656 | u32 oldval; | |
657 | ||
658 | /* | |
659 | * There is no waiter, so we unlock the futex. The owner died | |
660 | * bit has not to be preserved here. We are the owner: | |
661 | */ | |
a866374a | 662 | pagefault_disable(); |
c87e2837 | 663 | oldval = futex_atomic_cmpxchg_inatomic(uaddr, uval, 0); |
a866374a | 664 | pagefault_enable(); |
c87e2837 IM |
665 | |
666 | if (oldval == -EFAULT) | |
667 | return oldval; | |
668 | if (oldval != uval) | |
669 | return -EAGAIN; | |
670 | ||
671 | return 0; | |
672 | } | |
673 | ||
8b8f319f IM |
674 | /* |
675 | * Express the locking dependencies for lockdep: | |
676 | */ | |
677 | static inline void | |
678 | double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2) | |
679 | { | |
680 | if (hb1 <= hb2) { | |
681 | spin_lock(&hb1->lock); | |
682 | if (hb1 < hb2) | |
683 | spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING); | |
684 | } else { /* hb1 > hb2 */ | |
685 | spin_lock(&hb2->lock); | |
686 | spin_lock_nested(&hb1->lock, SINGLE_DEPTH_NESTING); | |
687 | } | |
688 | } | |
689 | ||
1da177e4 LT |
690 | /* |
691 | * Wake up all waiters hashed on the physical page that is mapped | |
692 | * to this virtual address: | |
693 | */ | |
34f01cc1 ED |
694 | static int futex_wake(u32 __user *uaddr, struct rw_semaphore *fshared, |
695 | int nr_wake) | |
1da177e4 | 696 | { |
e2970f2f | 697 | struct futex_hash_bucket *hb; |
1da177e4 | 698 | struct futex_q *this, *next; |
ec92d082 | 699 | struct plist_head *head; |
e2970f2f | 700 | union futex_key key; |
1da177e4 LT |
701 | int ret; |
702 | ||
34f01cc1 ED |
703 | if (fshared) |
704 | down_read(fshared); | |
1da177e4 | 705 | |
34f01cc1 | 706 | ret = get_futex_key(uaddr, fshared, &key); |
1da177e4 LT |
707 | if (unlikely(ret != 0)) |
708 | goto out; | |
709 | ||
e2970f2f IM |
710 | hb = hash_futex(&key); |
711 | spin_lock(&hb->lock); | |
712 | head = &hb->chain; | |
1da177e4 | 713 | |
ec92d082 | 714 | plist_for_each_entry_safe(this, next, head, list) { |
1da177e4 | 715 | if (match_futex (&this->key, &key)) { |
ed6f7b10 IM |
716 | if (this->pi_state) { |
717 | ret = -EINVAL; | |
718 | break; | |
719 | } | |
1da177e4 LT |
720 | wake_futex(this); |
721 | if (++ret >= nr_wake) | |
722 | break; | |
723 | } | |
724 | } | |
725 | ||
e2970f2f | 726 | spin_unlock(&hb->lock); |
1da177e4 | 727 | out: |
34f01cc1 ED |
728 | if (fshared) |
729 | up_read(fshared); | |
1da177e4 LT |
730 | return ret; |
731 | } | |
732 | ||
4732efbe JJ |
733 | /* |
734 | * Wake up all waiters hashed on the physical page that is mapped | |
735 | * to this virtual address: | |
736 | */ | |
e2970f2f | 737 | static int |
34f01cc1 ED |
738 | futex_wake_op(u32 __user *uaddr1, struct rw_semaphore *fshared, |
739 | u32 __user *uaddr2, | |
e2970f2f | 740 | int nr_wake, int nr_wake2, int op) |
4732efbe JJ |
741 | { |
742 | union futex_key key1, key2; | |
e2970f2f | 743 | struct futex_hash_bucket *hb1, *hb2; |
ec92d082 | 744 | struct plist_head *head; |
4732efbe JJ |
745 | struct futex_q *this, *next; |
746 | int ret, op_ret, attempt = 0; | |
747 | ||
748 | retryfull: | |
34f01cc1 ED |
749 | if (fshared) |
750 | down_read(fshared); | |
4732efbe | 751 | |
34f01cc1 | 752 | ret = get_futex_key(uaddr1, fshared, &key1); |
4732efbe JJ |
753 | if (unlikely(ret != 0)) |
754 | goto out; | |
34f01cc1 | 755 | ret = get_futex_key(uaddr2, fshared, &key2); |
4732efbe JJ |
756 | if (unlikely(ret != 0)) |
757 | goto out; | |
758 | ||
e2970f2f IM |
759 | hb1 = hash_futex(&key1); |
760 | hb2 = hash_futex(&key2); | |
4732efbe JJ |
761 | |
762 | retry: | |
8b8f319f | 763 | double_lock_hb(hb1, hb2); |
4732efbe | 764 | |
e2970f2f | 765 | op_ret = futex_atomic_op_inuser(op, uaddr2); |
4732efbe | 766 | if (unlikely(op_ret < 0)) { |
e2970f2f | 767 | u32 dummy; |
4732efbe | 768 | |
e2970f2f IM |
769 | spin_unlock(&hb1->lock); |
770 | if (hb1 != hb2) | |
771 | spin_unlock(&hb2->lock); | |
4732efbe | 772 | |
7ee1dd3f | 773 | #ifndef CONFIG_MMU |
e2970f2f IM |
774 | /* |
775 | * we don't get EFAULT from MMU faults if we don't have an MMU, | |
776 | * but we might get them from range checking | |
777 | */ | |
7ee1dd3f DH |
778 | ret = op_ret; |
779 | goto out; | |
780 | #endif | |
781 | ||
796f8d9b DG |
782 | if (unlikely(op_ret != -EFAULT)) { |
783 | ret = op_ret; | |
784 | goto out; | |
785 | } | |
786 | ||
e2970f2f IM |
787 | /* |
788 | * futex_atomic_op_inuser needs to both read and write | |
4732efbe JJ |
789 | * *(int __user *)uaddr2, but we can't modify it |
790 | * non-atomically. Therefore, if get_user below is not | |
791 | * enough, we need to handle the fault ourselves, while | |
e2970f2f IM |
792 | * still holding the mmap_sem. |
793 | */ | |
4732efbe | 794 | if (attempt++) { |
34f01cc1 ED |
795 | ret = futex_handle_fault((unsigned long)uaddr2, |
796 | fshared, attempt); | |
797 | if (ret) | |
4732efbe | 798 | goto out; |
4732efbe JJ |
799 | goto retry; |
800 | } | |
801 | ||
e2970f2f IM |
802 | /* |
803 | * If we would have faulted, release mmap_sem, | |
804 | * fault it in and start all over again. | |
805 | */ | |
34f01cc1 ED |
806 | if (fshared) |
807 | up_read(fshared); | |
4732efbe | 808 | |
e2970f2f | 809 | ret = get_user(dummy, uaddr2); |
4732efbe JJ |
810 | if (ret) |
811 | return ret; | |
812 | ||
813 | goto retryfull; | |
814 | } | |
815 | ||
e2970f2f | 816 | head = &hb1->chain; |
4732efbe | 817 | |
ec92d082 | 818 | plist_for_each_entry_safe(this, next, head, list) { |
4732efbe JJ |
819 | if (match_futex (&this->key, &key1)) { |
820 | wake_futex(this); | |
821 | if (++ret >= nr_wake) | |
822 | break; | |
823 | } | |
824 | } | |
825 | ||
826 | if (op_ret > 0) { | |
e2970f2f | 827 | head = &hb2->chain; |
4732efbe JJ |
828 | |
829 | op_ret = 0; | |
ec92d082 | 830 | plist_for_each_entry_safe(this, next, head, list) { |
4732efbe JJ |
831 | if (match_futex (&this->key, &key2)) { |
832 | wake_futex(this); | |
833 | if (++op_ret >= nr_wake2) | |
834 | break; | |
835 | } | |
836 | } | |
837 | ret += op_ret; | |
838 | } | |
839 | ||
e2970f2f IM |
840 | spin_unlock(&hb1->lock); |
841 | if (hb1 != hb2) | |
842 | spin_unlock(&hb2->lock); | |
4732efbe | 843 | out: |
34f01cc1 ED |
844 | if (fshared) |
845 | up_read(fshared); | |
4732efbe JJ |
846 | return ret; |
847 | } | |
848 | ||
1da177e4 LT |
849 | /* |
850 | * Requeue all waiters hashed on one physical page to another | |
851 | * physical page. | |
852 | */ | |
34f01cc1 ED |
853 | static int futex_requeue(u32 __user *uaddr1, struct rw_semaphore *fshared, |
854 | u32 __user *uaddr2, | |
e2970f2f | 855 | int nr_wake, int nr_requeue, u32 *cmpval) |
1da177e4 LT |
856 | { |
857 | union futex_key key1, key2; | |
e2970f2f | 858 | struct futex_hash_bucket *hb1, *hb2; |
ec92d082 | 859 | struct plist_head *head1; |
1da177e4 LT |
860 | struct futex_q *this, *next; |
861 | int ret, drop_count = 0; | |
862 | ||
863 | retry: | |
34f01cc1 ED |
864 | if (fshared) |
865 | down_read(fshared); | |
1da177e4 | 866 | |
34f01cc1 | 867 | ret = get_futex_key(uaddr1, fshared, &key1); |
1da177e4 LT |
868 | if (unlikely(ret != 0)) |
869 | goto out; | |
34f01cc1 | 870 | ret = get_futex_key(uaddr2, fshared, &key2); |
1da177e4 LT |
871 | if (unlikely(ret != 0)) |
872 | goto out; | |
873 | ||
e2970f2f IM |
874 | hb1 = hash_futex(&key1); |
875 | hb2 = hash_futex(&key2); | |
1da177e4 | 876 | |
8b8f319f | 877 | double_lock_hb(hb1, hb2); |
1da177e4 | 878 | |
e2970f2f IM |
879 | if (likely(cmpval != NULL)) { |
880 | u32 curval; | |
1da177e4 | 881 | |
e2970f2f | 882 | ret = get_futex_value_locked(&curval, uaddr1); |
1da177e4 LT |
883 | |
884 | if (unlikely(ret)) { | |
e2970f2f IM |
885 | spin_unlock(&hb1->lock); |
886 | if (hb1 != hb2) | |
887 | spin_unlock(&hb2->lock); | |
1da177e4 | 888 | |
e2970f2f IM |
889 | /* |
890 | * If we would have faulted, release mmap_sem, fault | |
1da177e4 LT |
891 | * it in and start all over again. |
892 | */ | |
34f01cc1 ED |
893 | if (fshared) |
894 | up_read(fshared); | |
1da177e4 | 895 | |
e2970f2f | 896 | ret = get_user(curval, uaddr1); |
1da177e4 LT |
897 | |
898 | if (!ret) | |
899 | goto retry; | |
900 | ||
901 | return ret; | |
902 | } | |
e2970f2f | 903 | if (curval != *cmpval) { |
1da177e4 LT |
904 | ret = -EAGAIN; |
905 | goto out_unlock; | |
906 | } | |
907 | } | |
908 | ||
e2970f2f | 909 | head1 = &hb1->chain; |
ec92d082 | 910 | plist_for_each_entry_safe(this, next, head1, list) { |
1da177e4 LT |
911 | if (!match_futex (&this->key, &key1)) |
912 | continue; | |
913 | if (++ret <= nr_wake) { | |
914 | wake_futex(this); | |
915 | } else { | |
59e0e0ac SD |
916 | /* |
917 | * If key1 and key2 hash to the same bucket, no need to | |
918 | * requeue. | |
919 | */ | |
920 | if (likely(head1 != &hb2->chain)) { | |
ec92d082 PP |
921 | plist_del(&this->list, &hb1->chain); |
922 | plist_add(&this->list, &hb2->chain); | |
59e0e0ac | 923 | this->lock_ptr = &hb2->lock; |
ec92d082 PP |
924 | #ifdef CONFIG_DEBUG_PI_LIST |
925 | this->list.plist.lock = &hb2->lock; | |
926 | #endif | |
778e9a9c | 927 | } |
1da177e4 | 928 | this->key = key2; |
9adef58b | 929 | get_futex_key_refs(&key2); |
1da177e4 LT |
930 | drop_count++; |
931 | ||
932 | if (ret - nr_wake >= nr_requeue) | |
933 | break; | |
1da177e4 LT |
934 | } |
935 | } | |
936 | ||
937 | out_unlock: | |
e2970f2f IM |
938 | spin_unlock(&hb1->lock); |
939 | if (hb1 != hb2) | |
940 | spin_unlock(&hb2->lock); | |
1da177e4 | 941 | |
9adef58b | 942 | /* drop_futex_key_refs() must be called outside the spinlocks. */ |
1da177e4 | 943 | while (--drop_count >= 0) |
9adef58b | 944 | drop_futex_key_refs(&key1); |
1da177e4 LT |
945 | |
946 | out: | |
34f01cc1 ED |
947 | if (fshared) |
948 | up_read(fshared); | |
1da177e4 LT |
949 | return ret; |
950 | } | |
951 | ||
952 | /* The key must be already stored in q->key. */ | |
953 | static inline struct futex_hash_bucket * | |
954 | queue_lock(struct futex_q *q, int fd, struct file *filp) | |
955 | { | |
e2970f2f | 956 | struct futex_hash_bucket *hb; |
1da177e4 LT |
957 | |
958 | q->fd = fd; | |
959 | q->filp = filp; | |
960 | ||
961 | init_waitqueue_head(&q->waiters); | |
962 | ||
9adef58b | 963 | get_futex_key_refs(&q->key); |
e2970f2f IM |
964 | hb = hash_futex(&q->key); |
965 | q->lock_ptr = &hb->lock; | |
1da177e4 | 966 | |
e2970f2f IM |
967 | spin_lock(&hb->lock); |
968 | return hb; | |
1da177e4 LT |
969 | } |
970 | ||
e2970f2f | 971 | static inline void __queue_me(struct futex_q *q, struct futex_hash_bucket *hb) |
1da177e4 | 972 | { |
ec92d082 PP |
973 | int prio; |
974 | ||
975 | /* | |
976 | * The priority used to register this element is | |
977 | * - either the real thread-priority for the real-time threads | |
978 | * (i.e. threads with a priority lower than MAX_RT_PRIO) | |
979 | * - or MAX_RT_PRIO for non-RT threads. | |
980 | * Thus, all RT-threads are woken first in priority order, and | |
981 | * the others are woken last, in FIFO order. | |
982 | */ | |
983 | prio = min(current->normal_prio, MAX_RT_PRIO); | |
984 | ||
985 | plist_node_init(&q->list, prio); | |
986 | #ifdef CONFIG_DEBUG_PI_LIST | |
987 | q->list.plist.lock = &hb->lock; | |
988 | #endif | |
989 | plist_add(&q->list, &hb->chain); | |
c87e2837 | 990 | q->task = current; |
e2970f2f | 991 | spin_unlock(&hb->lock); |
1da177e4 LT |
992 | } |
993 | ||
994 | static inline void | |
e2970f2f | 995 | queue_unlock(struct futex_q *q, struct futex_hash_bucket *hb) |
1da177e4 | 996 | { |
e2970f2f | 997 | spin_unlock(&hb->lock); |
9adef58b | 998 | drop_futex_key_refs(&q->key); |
1da177e4 LT |
999 | } |
1000 | ||
1001 | /* | |
1002 | * queue_me and unqueue_me must be called as a pair, each | |
1003 | * exactly once. They are called with the hashed spinlock held. | |
1004 | */ | |
1005 | ||
1006 | /* The key must be already stored in q->key. */ | |
1007 | static void queue_me(struct futex_q *q, int fd, struct file *filp) | |
1008 | { | |
e2970f2f IM |
1009 | struct futex_hash_bucket *hb; |
1010 | ||
1011 | hb = queue_lock(q, fd, filp); | |
1012 | __queue_me(q, hb); | |
1da177e4 LT |
1013 | } |
1014 | ||
1015 | /* Return 1 if we were still queued (ie. 0 means we were woken) */ | |
1016 | static int unqueue_me(struct futex_q *q) | |
1017 | { | |
1da177e4 | 1018 | spinlock_t *lock_ptr; |
e2970f2f | 1019 | int ret = 0; |
1da177e4 LT |
1020 | |
1021 | /* In the common case we don't take the spinlock, which is nice. */ | |
1022 | retry: | |
1023 | lock_ptr = q->lock_ptr; | |
e91467ec | 1024 | barrier(); |
1da177e4 LT |
1025 | if (lock_ptr != 0) { |
1026 | spin_lock(lock_ptr); | |
1027 | /* | |
1028 | * q->lock_ptr can change between reading it and | |
1029 | * spin_lock(), causing us to take the wrong lock. This | |
1030 | * corrects the race condition. | |
1031 | * | |
1032 | * Reasoning goes like this: if we have the wrong lock, | |
1033 | * q->lock_ptr must have changed (maybe several times) | |
1034 | * between reading it and the spin_lock(). It can | |
1035 | * change again after the spin_lock() but only if it was | |
1036 | * already changed before the spin_lock(). It cannot, | |
1037 | * however, change back to the original value. Therefore | |
1038 | * we can detect whether we acquired the correct lock. | |
1039 | */ | |
1040 | if (unlikely(lock_ptr != q->lock_ptr)) { | |
1041 | spin_unlock(lock_ptr); | |
1042 | goto retry; | |
1043 | } | |
ec92d082 PP |
1044 | WARN_ON(plist_node_empty(&q->list)); |
1045 | plist_del(&q->list, &q->list.plist); | |
c87e2837 IM |
1046 | |
1047 | BUG_ON(q->pi_state); | |
1048 | ||
1da177e4 LT |
1049 | spin_unlock(lock_ptr); |
1050 | ret = 1; | |
1051 | } | |
1052 | ||
9adef58b | 1053 | drop_futex_key_refs(&q->key); |
1da177e4 LT |
1054 | return ret; |
1055 | } | |
1056 | ||
c87e2837 IM |
1057 | /* |
1058 | * PI futexes can not be requeued and must remove themself from the | |
d0aa7a70 PP |
1059 | * hash bucket. The hash bucket lock (i.e. lock_ptr) is held on entry |
1060 | * and dropped here. | |
c87e2837 | 1061 | */ |
d0aa7a70 | 1062 | static void unqueue_me_pi(struct futex_q *q) |
c87e2837 | 1063 | { |
ec92d082 PP |
1064 | WARN_ON(plist_node_empty(&q->list)); |
1065 | plist_del(&q->list, &q->list.plist); | |
c87e2837 IM |
1066 | |
1067 | BUG_ON(!q->pi_state); | |
1068 | free_pi_state(q->pi_state); | |
1069 | q->pi_state = NULL; | |
1070 | ||
d0aa7a70 | 1071 | spin_unlock(q->lock_ptr); |
c87e2837 | 1072 | |
9adef58b | 1073 | drop_futex_key_refs(&q->key); |
c87e2837 IM |
1074 | } |
1075 | ||
d0aa7a70 PP |
1076 | /* |
1077 | * Fixup the pi_state owner with current. | |
1078 | * | |
778e9a9c AK |
1079 | * Must be called with hash bucket lock held and mm->sem held for non |
1080 | * private futexes. | |
d0aa7a70 | 1081 | */ |
778e9a9c | 1082 | static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q, |
d0aa7a70 PP |
1083 | struct task_struct *curr) |
1084 | { | |
1085 | u32 newtid = curr->pid | FUTEX_WAITERS; | |
1086 | struct futex_pi_state *pi_state = q->pi_state; | |
1087 | u32 uval, curval, newval; | |
1088 | int ret; | |
1089 | ||
1090 | /* Owner died? */ | |
1091 | if (pi_state->owner != NULL) { | |
1092 | spin_lock_irq(&pi_state->owner->pi_lock); | |
1093 | WARN_ON(list_empty(&pi_state->list)); | |
1094 | list_del_init(&pi_state->list); | |
1095 | spin_unlock_irq(&pi_state->owner->pi_lock); | |
1096 | } else | |
1097 | newtid |= FUTEX_OWNER_DIED; | |
1098 | ||
1099 | pi_state->owner = curr; | |
1100 | ||
1101 | spin_lock_irq(&curr->pi_lock); | |
1102 | WARN_ON(!list_empty(&pi_state->list)); | |
1103 | list_add(&pi_state->list, &curr->pi_state_list); | |
1104 | spin_unlock_irq(&curr->pi_lock); | |
1105 | ||
d0aa7a70 PP |
1106 | /* |
1107 | * We own it, so we have to replace the pending owner | |
1108 | * TID. This must be atomic as we have preserve the | |
1109 | * owner died bit here. | |
1110 | */ | |
778e9a9c AK |
1111 | ret = get_futex_value_locked(&uval, uaddr); |
1112 | ||
d0aa7a70 PP |
1113 | while (!ret) { |
1114 | newval = (uval & FUTEX_OWNER_DIED) | newtid; | |
778e9a9c AK |
1115 | |
1116 | pagefault_disable(); | |
d0aa7a70 PP |
1117 | curval = futex_atomic_cmpxchg_inatomic(uaddr, |
1118 | uval, newval); | |
778e9a9c AK |
1119 | pagefault_enable(); |
1120 | ||
d0aa7a70 | 1121 | if (curval == -EFAULT) |
778e9a9c | 1122 | ret = -EFAULT; |
d0aa7a70 PP |
1123 | if (curval == uval) |
1124 | break; | |
1125 | uval = curval; | |
1126 | } | |
1127 | return ret; | |
1128 | } | |
1129 | ||
34f01cc1 ED |
1130 | /* |
1131 | * In case we must use restart_block to restart a futex_wait, | |
1132 | * we encode in the 'arg3' shared capability | |
1133 | */ | |
1134 | #define ARG3_SHARED 1 | |
1135 | ||
72c1bbf3 | 1136 | static long futex_wait_restart(struct restart_block *restart); |
34f01cc1 ED |
1137 | static int futex_wait(u32 __user *uaddr, struct rw_semaphore *fshared, |
1138 | u32 val, ktime_t *abs_time) | |
1da177e4 | 1139 | { |
c87e2837 IM |
1140 | struct task_struct *curr = current; |
1141 | DECLARE_WAITQUEUE(wait, curr); | |
e2970f2f | 1142 | struct futex_hash_bucket *hb; |
1da177e4 | 1143 | struct futex_q q; |
e2970f2f IM |
1144 | u32 uval; |
1145 | int ret; | |
bd197234 | 1146 | struct hrtimer_sleeper t; |
c19384b5 | 1147 | int rem = 0; |
1da177e4 | 1148 | |
c87e2837 | 1149 | q.pi_state = NULL; |
1da177e4 | 1150 | retry: |
34f01cc1 ED |
1151 | if (fshared) |
1152 | down_read(fshared); | |
1da177e4 | 1153 | |
34f01cc1 | 1154 | ret = get_futex_key(uaddr, fshared, &q.key); |
1da177e4 LT |
1155 | if (unlikely(ret != 0)) |
1156 | goto out_release_sem; | |
1157 | ||
e2970f2f | 1158 | hb = queue_lock(&q, -1, NULL); |
1da177e4 LT |
1159 | |
1160 | /* | |
1161 | * Access the page AFTER the futex is queued. | |
1162 | * Order is important: | |
1163 | * | |
1164 | * Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val); | |
1165 | * Userspace waker: if (cond(var)) { var = new; futex_wake(&var); } | |
1166 | * | |
1167 | * The basic logical guarantee of a futex is that it blocks ONLY | |
1168 | * if cond(var) is known to be true at the time of blocking, for | |
1169 | * any cond. If we queued after testing *uaddr, that would open | |
1170 | * a race condition where we could block indefinitely with | |
1171 | * cond(var) false, which would violate the guarantee. | |
1172 | * | |
1173 | * A consequence is that futex_wait() can return zero and absorb | |
1174 | * a wakeup when *uaddr != val on entry to the syscall. This is | |
1175 | * rare, but normal. | |
1176 | * | |
34f01cc1 ED |
1177 | * for shared futexes, we hold the mmap semaphore, so the mapping |
1178 | * cannot have changed since we looked it up in get_futex_key. | |
1da177e4 | 1179 | */ |
e2970f2f | 1180 | ret = get_futex_value_locked(&uval, uaddr); |
1da177e4 LT |
1181 | |
1182 | if (unlikely(ret)) { | |
e2970f2f | 1183 | queue_unlock(&q, hb); |
1da177e4 | 1184 | |
e2970f2f IM |
1185 | /* |
1186 | * If we would have faulted, release mmap_sem, fault it in and | |
1da177e4 LT |
1187 | * start all over again. |
1188 | */ | |
34f01cc1 ED |
1189 | if (fshared) |
1190 | up_read(fshared); | |
1da177e4 | 1191 | |
e2970f2f | 1192 | ret = get_user(uval, uaddr); |
1da177e4 LT |
1193 | |
1194 | if (!ret) | |
1195 | goto retry; | |
1196 | return ret; | |
1197 | } | |
c87e2837 IM |
1198 | ret = -EWOULDBLOCK; |
1199 | if (uval != val) | |
1200 | goto out_unlock_release_sem; | |
1da177e4 LT |
1201 | |
1202 | /* Only actually queue if *uaddr contained val. */ | |
e2970f2f | 1203 | __queue_me(&q, hb); |
1da177e4 LT |
1204 | |
1205 | /* | |
1206 | * Now the futex is queued and we have checked the data, we | |
1207 | * don't want to hold mmap_sem while we sleep. | |
c87e2837 | 1208 | */ |
34f01cc1 ED |
1209 | if (fshared) |
1210 | up_read(fshared); | |
1da177e4 LT |
1211 | |
1212 | /* | |
1213 | * There might have been scheduling since the queue_me(), as we | |
1214 | * cannot hold a spinlock across the get_user() in case it | |
1215 | * faults, and we cannot just set TASK_INTERRUPTIBLE state when | |
1216 | * queueing ourselves into the futex hash. This code thus has to | |
1217 | * rely on the futex_wake() code removing us from hash when it | |
1218 | * wakes us up. | |
1219 | */ | |
1220 | ||
1221 | /* add_wait_queue is the barrier after __set_current_state. */ | |
1222 | __set_current_state(TASK_INTERRUPTIBLE); | |
1223 | add_wait_queue(&q.waiters, &wait); | |
1224 | /* | |
ec92d082 | 1225 | * !plist_node_empty() is safe here without any lock. |
1da177e4 LT |
1226 | * q.lock_ptr != 0 is not safe, because of ordering against wakeup. |
1227 | */ | |
ec92d082 | 1228 | if (likely(!plist_node_empty(&q.list))) { |
c19384b5 PP |
1229 | if (!abs_time) |
1230 | schedule(); | |
1231 | else { | |
1232 | hrtimer_init(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); | |
1233 | hrtimer_init_sleeper(&t, current); | |
1234 | t.timer.expires = *abs_time; | |
1235 | ||
1236 | hrtimer_start(&t.timer, t.timer.expires, HRTIMER_MODE_ABS); | |
1237 | ||
1238 | /* | |
1239 | * the timer could have already expired, in which | |
1240 | * case current would be flagged for rescheduling. | |
1241 | * Don't bother calling schedule. | |
1242 | */ | |
1243 | if (likely(t.task)) | |
1244 | schedule(); | |
1245 | ||
1246 | hrtimer_cancel(&t.timer); | |
72c1bbf3 | 1247 | |
c19384b5 PP |
1248 | /* Flag if a timeout occured */ |
1249 | rem = (t.task == NULL); | |
1250 | } | |
72c1bbf3 | 1251 | } |
1da177e4 LT |
1252 | __set_current_state(TASK_RUNNING); |
1253 | ||
1254 | /* | |
1255 | * NOTE: we don't remove ourselves from the waitqueue because | |
1256 | * we are the only user of it. | |
1257 | */ | |
1258 | ||
1259 | /* If we were woken (and unqueued), we succeeded, whatever. */ | |
1260 | if (!unqueue_me(&q)) | |
1261 | return 0; | |
c19384b5 | 1262 | if (rem) |
1da177e4 | 1263 | return -ETIMEDOUT; |
72c1bbf3 | 1264 | |
e2970f2f IM |
1265 | /* |
1266 | * We expect signal_pending(current), but another thread may | |
1267 | * have handled it for us already. | |
1268 | */ | |
c19384b5 | 1269 | if (!abs_time) |
72c1bbf3 NP |
1270 | return -ERESTARTSYS; |
1271 | else { | |
1272 | struct restart_block *restart; | |
1273 | restart = ¤t_thread_info()->restart_block; | |
1274 | restart->fn = futex_wait_restart; | |
1275 | restart->arg0 = (unsigned long)uaddr; | |
1276 | restart->arg1 = (unsigned long)val; | |
c19384b5 | 1277 | restart->arg2 = (unsigned long)abs_time; |
34f01cc1 ED |
1278 | restart->arg3 = 0; |
1279 | if (fshared) | |
1280 | restart->arg3 |= ARG3_SHARED; | |
72c1bbf3 NP |
1281 | return -ERESTART_RESTARTBLOCK; |
1282 | } | |
1da177e4 | 1283 | |
c87e2837 IM |
1284 | out_unlock_release_sem: |
1285 | queue_unlock(&q, hb); | |
1286 | ||
1da177e4 | 1287 | out_release_sem: |
34f01cc1 ED |
1288 | if (fshared) |
1289 | up_read(fshared); | |
c87e2837 IM |
1290 | return ret; |
1291 | } | |
1292 | ||
72c1bbf3 NP |
1293 | |
1294 | static long futex_wait_restart(struct restart_block *restart) | |
1295 | { | |
1296 | u32 __user *uaddr = (u32 __user *)restart->arg0; | |
1297 | u32 val = (u32)restart->arg1; | |
c19384b5 | 1298 | ktime_t *abs_time = (ktime_t *)restart->arg2; |
34f01cc1 | 1299 | struct rw_semaphore *fshared = NULL; |
72c1bbf3 NP |
1300 | |
1301 | restart->fn = do_no_restart_syscall; | |
34f01cc1 ED |
1302 | if (restart->arg3 & ARG3_SHARED) |
1303 | fshared = ¤t->mm->mmap_sem; | |
1304 | return (long)futex_wait(uaddr, fshared, val, abs_time); | |
72c1bbf3 NP |
1305 | } |
1306 | ||
1307 | ||
c87e2837 IM |
1308 | /* |
1309 | * Userspace tried a 0 -> TID atomic transition of the futex value | |
1310 | * and failed. The kernel side here does the whole locking operation: | |
1311 | * if there are waiters then it will block, it does PI, etc. (Due to | |
1312 | * races the kernel might see a 0 value of the futex too.) | |
1313 | */ | |
34f01cc1 ED |
1314 | static int futex_lock_pi(u32 __user *uaddr, struct rw_semaphore *fshared, |
1315 | int detect, ktime_t *time, int trylock) | |
c87e2837 | 1316 | { |
c5780e97 | 1317 | struct hrtimer_sleeper timeout, *to = NULL; |
c87e2837 IM |
1318 | struct task_struct *curr = current; |
1319 | struct futex_hash_bucket *hb; | |
1320 | u32 uval, newval, curval; | |
1321 | struct futex_q q; | |
778e9a9c | 1322 | int ret, lock_taken, ownerdied = 0, attempt = 0; |
c87e2837 IM |
1323 | |
1324 | if (refill_pi_state_cache()) | |
1325 | return -ENOMEM; | |
1326 | ||
c19384b5 | 1327 | if (time) { |
c5780e97 | 1328 | to = &timeout; |
c9cb2e3d | 1329 | hrtimer_init(&to->timer, CLOCK_REALTIME, HRTIMER_MODE_ABS); |
c5780e97 | 1330 | hrtimer_init_sleeper(to, current); |
c19384b5 | 1331 | to->timer.expires = *time; |
c5780e97 TG |
1332 | } |
1333 | ||
c87e2837 IM |
1334 | q.pi_state = NULL; |
1335 | retry: | |
34f01cc1 ED |
1336 | if (fshared) |
1337 | down_read(fshared); | |
c87e2837 | 1338 | |
34f01cc1 | 1339 | ret = get_futex_key(uaddr, fshared, &q.key); |
c87e2837 IM |
1340 | if (unlikely(ret != 0)) |
1341 | goto out_release_sem; | |
1342 | ||
778e9a9c | 1343 | retry_unlocked: |
c87e2837 IM |
1344 | hb = queue_lock(&q, -1, NULL); |
1345 | ||
1346 | retry_locked: | |
778e9a9c | 1347 | ret = lock_taken = 0; |
d0aa7a70 | 1348 | |
c87e2837 IM |
1349 | /* |
1350 | * To avoid races, we attempt to take the lock here again | |
1351 | * (by doing a 0 -> TID atomic cmpxchg), while holding all | |
1352 | * the locks. It will most likely not succeed. | |
1353 | */ | |
1354 | newval = current->pid; | |
1355 | ||
a866374a | 1356 | pagefault_disable(); |
c87e2837 | 1357 | curval = futex_atomic_cmpxchg_inatomic(uaddr, 0, newval); |
a866374a | 1358 | pagefault_enable(); |
c87e2837 IM |
1359 | |
1360 | if (unlikely(curval == -EFAULT)) | |
1361 | goto uaddr_faulted; | |
1362 | ||
778e9a9c AK |
1363 | /* |
1364 | * Detect deadlocks. In case of REQUEUE_PI this is a valid | |
1365 | * situation and we return success to user space. | |
1366 | */ | |
c87e2837 | 1367 | if (unlikely((curval & FUTEX_TID_MASK) == current->pid)) { |
bd197234 | 1368 | ret = -EDEADLK; |
c87e2837 IM |
1369 | goto out_unlock_release_sem; |
1370 | } | |
1371 | ||
1372 | /* | |
778e9a9c | 1373 | * Surprise - we got the lock. Just return to userspace: |
c87e2837 IM |
1374 | */ |
1375 | if (unlikely(!curval)) | |
1376 | goto out_unlock_release_sem; | |
1377 | ||
1378 | uval = curval; | |
778e9a9c | 1379 | |
d0aa7a70 | 1380 | /* |
778e9a9c AK |
1381 | * Set the WAITERS flag, so the owner will know it has someone |
1382 | * to wake at next unlock | |
d0aa7a70 | 1383 | */ |
778e9a9c AK |
1384 | newval = curval | FUTEX_WAITERS; |
1385 | ||
1386 | /* | |
1387 | * There are two cases, where a futex might have no owner (the | |
bd197234 TG |
1388 | * owner TID is 0): OWNER_DIED. We take over the futex in this |
1389 | * case. We also do an unconditional take over, when the owner | |
1390 | * of the futex died. | |
778e9a9c AK |
1391 | * |
1392 | * This is safe as we are protected by the hash bucket lock ! | |
1393 | */ | |
1394 | if (unlikely(ownerdied || !(curval & FUTEX_TID_MASK))) { | |
bd197234 | 1395 | /* Keep the OWNER_DIED bit */ |
778e9a9c AK |
1396 | newval = (curval & ~FUTEX_TID_MASK) | current->pid; |
1397 | ownerdied = 0; | |
1398 | lock_taken = 1; | |
1399 | } | |
c87e2837 | 1400 | |
a866374a | 1401 | pagefault_disable(); |
c87e2837 | 1402 | curval = futex_atomic_cmpxchg_inatomic(uaddr, uval, newval); |
a866374a | 1403 | pagefault_enable(); |
c87e2837 IM |
1404 | |
1405 | if (unlikely(curval == -EFAULT)) | |
1406 | goto uaddr_faulted; | |
1407 | if (unlikely(curval != uval)) | |
1408 | goto retry_locked; | |
1409 | ||
778e9a9c | 1410 | /* |
bd197234 | 1411 | * We took the lock due to owner died take over. |
778e9a9c | 1412 | */ |
bd197234 | 1413 | if (unlikely(lock_taken)) |
d0aa7a70 | 1414 | goto out_unlock_release_sem; |
d0aa7a70 | 1415 | |
c87e2837 IM |
1416 | /* |
1417 | * We dont have the lock. Look up the PI state (or create it if | |
1418 | * we are the first waiter): | |
1419 | */ | |
d0aa7a70 | 1420 | ret = lookup_pi_state(uval, hb, &q.key, &q.pi_state); |
c87e2837 IM |
1421 | |
1422 | if (unlikely(ret)) { | |
778e9a9c | 1423 | switch (ret) { |
c87e2837 | 1424 | |
778e9a9c AK |
1425 | case -EAGAIN: |
1426 | /* | |
1427 | * Task is exiting and we just wait for the | |
1428 | * exit to complete. | |
1429 | */ | |
1430 | queue_unlock(&q, hb); | |
1431 | if (fshared) | |
1432 | up_read(fshared); | |
1433 | cond_resched(); | |
1434 | goto retry; | |
c87e2837 | 1435 | |
778e9a9c AK |
1436 | case -ESRCH: |
1437 | /* | |
1438 | * No owner found for this futex. Check if the | |
1439 | * OWNER_DIED bit is set to figure out whether | |
1440 | * this is a robust futex or not. | |
1441 | */ | |
1442 | if (get_futex_value_locked(&curval, uaddr)) | |
c87e2837 | 1443 | goto uaddr_faulted; |
778e9a9c AK |
1444 | |
1445 | /* | |
1446 | * We simply start over in case of a robust | |
1447 | * futex. The code above will take the futex | |
1448 | * and return happy. | |
1449 | */ | |
1450 | if (curval & FUTEX_OWNER_DIED) { | |
1451 | ownerdied = 1; | |
c87e2837 | 1452 | goto retry_locked; |
778e9a9c AK |
1453 | } |
1454 | default: | |
1455 | goto out_unlock_release_sem; | |
c87e2837 | 1456 | } |
c87e2837 IM |
1457 | } |
1458 | ||
1459 | /* | |
1460 | * Only actually queue now that the atomic ops are done: | |
1461 | */ | |
1462 | __queue_me(&q, hb); | |
1463 | ||
1464 | /* | |
1465 | * Now the futex is queued and we have checked the data, we | |
1466 | * don't want to hold mmap_sem while we sleep. | |
1467 | */ | |
34f01cc1 ED |
1468 | if (fshared) |
1469 | up_read(fshared); | |
c87e2837 IM |
1470 | |
1471 | WARN_ON(!q.pi_state); | |
1472 | /* | |
1473 | * Block on the PI mutex: | |
1474 | */ | |
1475 | if (!trylock) | |
1476 | ret = rt_mutex_timed_lock(&q.pi_state->pi_mutex, to, 1); | |
1477 | else { | |
1478 | ret = rt_mutex_trylock(&q.pi_state->pi_mutex); | |
1479 | /* Fixup the trylock return value: */ | |
1480 | ret = ret ? 0 : -EWOULDBLOCK; | |
1481 | } | |
1482 | ||
34f01cc1 ED |
1483 | if (fshared) |
1484 | down_read(fshared); | |
a99e4e41 | 1485 | spin_lock(q.lock_ptr); |
c87e2837 | 1486 | |
778e9a9c AK |
1487 | if (!ret) { |
1488 | /* | |
1489 | * Got the lock. We might not be the anticipated owner | |
1490 | * if we did a lock-steal - fix up the PI-state in | |
1491 | * that case: | |
1492 | */ | |
1493 | if (q.pi_state->owner != curr) | |
1494 | ret = fixup_pi_state_owner(uaddr, &q, curr); | |
1495 | } else { | |
c87e2837 IM |
1496 | /* |
1497 | * Catch the rare case, where the lock was released | |
778e9a9c AK |
1498 | * when we were on the way back before we locked the |
1499 | * hash bucket. | |
c87e2837 | 1500 | */ |
778e9a9c AK |
1501 | if (q.pi_state->owner == curr && |
1502 | rt_mutex_trylock(&q.pi_state->pi_mutex)) { | |
1503 | ret = 0; | |
1504 | } else { | |
1505 | /* | |
1506 | * Paranoia check. If we did not take the lock | |
1507 | * in the trylock above, then we should not be | |
1508 | * the owner of the rtmutex, neither the real | |
1509 | * nor the pending one: | |
1510 | */ | |
1511 | if (rt_mutex_owner(&q.pi_state->pi_mutex) == curr) | |
1512 | printk(KERN_ERR "futex_lock_pi: ret = %d " | |
1513 | "pi-mutex: %p pi-state %p\n", ret, | |
1514 | q.pi_state->pi_mutex.owner, | |
1515 | q.pi_state->owner); | |
c87e2837 | 1516 | } |
c87e2837 IM |
1517 | } |
1518 | ||
778e9a9c AK |
1519 | /* Unqueue and drop the lock */ |
1520 | unqueue_me_pi(&q); | |
1521 | if (fshared) | |
1522 | up_read(fshared); | |
c87e2837 | 1523 | |
c5780e97 | 1524 | return ret != -EINTR ? ret : -ERESTARTNOINTR; |
c87e2837 IM |
1525 | |
1526 | out_unlock_release_sem: | |
1527 | queue_unlock(&q, hb); | |
1528 | ||
1529 | out_release_sem: | |
34f01cc1 ED |
1530 | if (fshared) |
1531 | up_read(fshared); | |
c87e2837 IM |
1532 | return ret; |
1533 | ||
1534 | uaddr_faulted: | |
1535 | /* | |
1536 | * We have to r/w *(int __user *)uaddr, but we can't modify it | |
1537 | * non-atomically. Therefore, if get_user below is not | |
1538 | * enough, we need to handle the fault ourselves, while | |
1539 | * still holding the mmap_sem. | |
778e9a9c AK |
1540 | * |
1541 | * ... and hb->lock. :-) --ANK | |
c87e2837 | 1542 | */ |
778e9a9c AK |
1543 | queue_unlock(&q, hb); |
1544 | ||
c87e2837 | 1545 | if (attempt++) { |
34f01cc1 ED |
1546 | ret = futex_handle_fault((unsigned long)uaddr, fshared, |
1547 | attempt); | |
1548 | if (ret) | |
778e9a9c AK |
1549 | goto out_release_sem; |
1550 | goto retry_unlocked; | |
c87e2837 IM |
1551 | } |
1552 | ||
34f01cc1 ED |
1553 | if (fshared) |
1554 | up_read(fshared); | |
c87e2837 IM |
1555 | |
1556 | ret = get_user(uval, uaddr); | |
1557 | if (!ret && (uval != -EFAULT)) | |
1558 | goto retry; | |
1559 | ||
1560 | return ret; | |
1561 | } | |
1562 | ||
c87e2837 IM |
1563 | /* |
1564 | * Userspace attempted a TID -> 0 atomic transition, and failed. | |
1565 | * This is the in-kernel slowpath: we look up the PI state (if any), | |
1566 | * and do the rt-mutex unlock. | |
1567 | */ | |
34f01cc1 | 1568 | static int futex_unlock_pi(u32 __user *uaddr, struct rw_semaphore *fshared) |
c87e2837 IM |
1569 | { |
1570 | struct futex_hash_bucket *hb; | |
1571 | struct futex_q *this, *next; | |
1572 | u32 uval; | |
ec92d082 | 1573 | struct plist_head *head; |
c87e2837 IM |
1574 | union futex_key key; |
1575 | int ret, attempt = 0; | |
1576 | ||
1577 | retry: | |
1578 | if (get_user(uval, uaddr)) | |
1579 | return -EFAULT; | |
1580 | /* | |
1581 | * We release only a lock we actually own: | |
1582 | */ | |
1583 | if ((uval & FUTEX_TID_MASK) != current->pid) | |
1584 | return -EPERM; | |
1585 | /* | |
1586 | * First take all the futex related locks: | |
1587 | */ | |
34f01cc1 ED |
1588 | if (fshared) |
1589 | down_read(fshared); | |
c87e2837 | 1590 | |
34f01cc1 | 1591 | ret = get_futex_key(uaddr, fshared, &key); |
c87e2837 IM |
1592 | if (unlikely(ret != 0)) |
1593 | goto out; | |
1594 | ||
1595 | hb = hash_futex(&key); | |
778e9a9c | 1596 | retry_unlocked: |
c87e2837 IM |
1597 | spin_lock(&hb->lock); |
1598 | ||
c87e2837 IM |
1599 | /* |
1600 | * To avoid races, try to do the TID -> 0 atomic transition | |
1601 | * again. If it succeeds then we can return without waking | |
1602 | * anyone else up: | |
1603 | */ | |
e3f2ddea | 1604 | if (!(uval & FUTEX_OWNER_DIED)) { |
a866374a | 1605 | pagefault_disable(); |
e3f2ddea | 1606 | uval = futex_atomic_cmpxchg_inatomic(uaddr, current->pid, 0); |
a866374a | 1607 | pagefault_enable(); |
e3f2ddea | 1608 | } |
c87e2837 IM |
1609 | |
1610 | if (unlikely(uval == -EFAULT)) | |
1611 | goto pi_faulted; | |
1612 | /* | |
1613 | * Rare case: we managed to release the lock atomically, | |
1614 | * no need to wake anyone else up: | |
1615 | */ | |
1616 | if (unlikely(uval == current->pid)) | |
1617 | goto out_unlock; | |
1618 | ||
1619 | /* | |
1620 | * Ok, other tasks may need to be woken up - check waiters | |
1621 | * and do the wakeup if necessary: | |
1622 | */ | |
1623 | head = &hb->chain; | |
1624 | ||
ec92d082 | 1625 | plist_for_each_entry_safe(this, next, head, list) { |
c87e2837 IM |
1626 | if (!match_futex (&this->key, &key)) |
1627 | continue; | |
1628 | ret = wake_futex_pi(uaddr, uval, this); | |
1629 | /* | |
1630 | * The atomic access to the futex value | |
1631 | * generated a pagefault, so retry the | |
1632 | * user-access and the wakeup: | |
1633 | */ | |
1634 | if (ret == -EFAULT) | |
1635 | goto pi_faulted; | |
1636 | goto out_unlock; | |
1637 | } | |
1638 | /* | |
1639 | * No waiters - kernel unlocks the futex: | |
1640 | */ | |
e3f2ddea IM |
1641 | if (!(uval & FUTEX_OWNER_DIED)) { |
1642 | ret = unlock_futex_pi(uaddr, uval); | |
1643 | if (ret == -EFAULT) | |
1644 | goto pi_faulted; | |
1645 | } | |
c87e2837 IM |
1646 | |
1647 | out_unlock: | |
1648 | spin_unlock(&hb->lock); | |
1649 | out: | |
34f01cc1 ED |
1650 | if (fshared) |
1651 | up_read(fshared); | |
c87e2837 IM |
1652 | |
1653 | return ret; | |
1654 | ||
1655 | pi_faulted: | |
1656 | /* | |
1657 | * We have to r/w *(int __user *)uaddr, but we can't modify it | |
1658 | * non-atomically. Therefore, if get_user below is not | |
1659 | * enough, we need to handle the fault ourselves, while | |
1660 | * still holding the mmap_sem. | |
778e9a9c AK |
1661 | * |
1662 | * ... and hb->lock. --ANK | |
c87e2837 | 1663 | */ |
778e9a9c AK |
1664 | spin_unlock(&hb->lock); |
1665 | ||
c87e2837 | 1666 | if (attempt++) { |
34f01cc1 ED |
1667 | ret = futex_handle_fault((unsigned long)uaddr, fshared, |
1668 | attempt); | |
1669 | if (ret) | |
778e9a9c AK |
1670 | goto out; |
1671 | goto retry_unlocked; | |
c87e2837 IM |
1672 | } |
1673 | ||
34f01cc1 ED |
1674 | if (fshared) |
1675 | up_read(fshared); | |
c87e2837 IM |
1676 | |
1677 | ret = get_user(uval, uaddr); | |
1678 | if (!ret && (uval != -EFAULT)) | |
1679 | goto retry; | |
1680 | ||
1da177e4 LT |
1681 | return ret; |
1682 | } | |
1683 | ||
1684 | static int futex_close(struct inode *inode, struct file *filp) | |
1685 | { | |
1686 | struct futex_q *q = filp->private_data; | |
1687 | ||
1688 | unqueue_me(q); | |
1689 | kfree(q); | |
e2970f2f | 1690 | |
1da177e4 LT |
1691 | return 0; |
1692 | } | |
1693 | ||
1694 | /* This is one-shot: once it's gone off you need a new fd */ | |
1695 | static unsigned int futex_poll(struct file *filp, | |
1696 | struct poll_table_struct *wait) | |
1697 | { | |
1698 | struct futex_q *q = filp->private_data; | |
1699 | int ret = 0; | |
1700 | ||
1701 | poll_wait(filp, &q->waiters, wait); | |
1702 | ||
1703 | /* | |
ec92d082 | 1704 | * plist_node_empty() is safe here without any lock. |
1da177e4 LT |
1705 | * q->lock_ptr != 0 is not safe, because of ordering against wakeup. |
1706 | */ | |
ec92d082 | 1707 | if (plist_node_empty(&q->list)) |
1da177e4 LT |
1708 | ret = POLLIN | POLLRDNORM; |
1709 | ||
1710 | return ret; | |
1711 | } | |
1712 | ||
15ad7cdc | 1713 | static const struct file_operations futex_fops = { |
1da177e4 LT |
1714 | .release = futex_close, |
1715 | .poll = futex_poll, | |
1716 | }; | |
1717 | ||
1718 | /* | |
1719 | * Signal allows caller to avoid the race which would occur if they | |
1720 | * set the sigio stuff up afterwards. | |
1721 | */ | |
e2970f2f | 1722 | static int futex_fd(u32 __user *uaddr, int signal) |
1da177e4 LT |
1723 | { |
1724 | struct futex_q *q; | |
1725 | struct file *filp; | |
1726 | int ret, err; | |
34f01cc1 | 1727 | struct rw_semaphore *fshared; |
19c6b6ed AM |
1728 | static unsigned long printk_interval; |
1729 | ||
1730 | if (printk_timed_ratelimit(&printk_interval, 60 * 60 * 1000)) { | |
1731 | printk(KERN_WARNING "Process `%s' used FUTEX_FD, which " | |
1732 | "will be removed from the kernel in June 2007\n", | |
1733 | current->comm); | |
1734 | } | |
1da177e4 LT |
1735 | |
1736 | ret = -EINVAL; | |
7ed20e1a | 1737 | if (!valid_signal(signal)) |
1da177e4 LT |
1738 | goto out; |
1739 | ||
1740 | ret = get_unused_fd(); | |
1741 | if (ret < 0) | |
1742 | goto out; | |
1743 | filp = get_empty_filp(); | |
1744 | if (!filp) { | |
1745 | put_unused_fd(ret); | |
1746 | ret = -ENFILE; | |
1747 | goto out; | |
1748 | } | |
1749 | filp->f_op = &futex_fops; | |
f3a43f3f JJS |
1750 | filp->f_path.mnt = mntget(futex_mnt); |
1751 | filp->f_path.dentry = dget(futex_mnt->mnt_root); | |
1752 | filp->f_mapping = filp->f_path.dentry->d_inode->i_mapping; | |
1da177e4 LT |
1753 | |
1754 | if (signal) { | |
609d7fa9 | 1755 | err = __f_setown(filp, task_pid(current), PIDTYPE_PID, 1); |
1da177e4 | 1756 | if (err < 0) { |
39ed3fde | 1757 | goto error; |
1da177e4 LT |
1758 | } |
1759 | filp->f_owner.signum = signal; | |
1760 | } | |
1761 | ||
1762 | q = kmalloc(sizeof(*q), GFP_KERNEL); | |
1763 | if (!q) { | |
39ed3fde PE |
1764 | err = -ENOMEM; |
1765 | goto error; | |
1da177e4 | 1766 | } |
c87e2837 | 1767 | q->pi_state = NULL; |
1da177e4 | 1768 | |
34f01cc1 ED |
1769 | fshared = ¤t->mm->mmap_sem; |
1770 | down_read(fshared); | |
1771 | err = get_futex_key(uaddr, fshared, &q->key); | |
1da177e4 LT |
1772 | |
1773 | if (unlikely(err != 0)) { | |
34f01cc1 | 1774 | up_read(fshared); |
1da177e4 | 1775 | kfree(q); |
39ed3fde | 1776 | goto error; |
1da177e4 LT |
1777 | } |
1778 | ||
1779 | /* | |
1780 | * queue_me() must be called before releasing mmap_sem, because | |
1781 | * key->shared.inode needs to be referenced while holding it. | |
1782 | */ | |
1783 | filp->private_data = q; | |
1784 | ||
1785 | queue_me(q, ret, filp); | |
34f01cc1 | 1786 | up_read(fshared); |
1da177e4 LT |
1787 | |
1788 | /* Now we map fd to filp, so userspace can access it */ | |
1789 | fd_install(ret, filp); | |
1790 | out: | |
1791 | return ret; | |
39ed3fde PE |
1792 | error: |
1793 | put_unused_fd(ret); | |
1794 | put_filp(filp); | |
1795 | ret = err; | |
1796 | goto out; | |
1da177e4 LT |
1797 | } |
1798 | ||
0771dfef IM |
1799 | /* |
1800 | * Support for robust futexes: the kernel cleans up held futexes at | |
1801 | * thread exit time. | |
1802 | * | |
1803 | * Implementation: user-space maintains a per-thread list of locks it | |
1804 | * is holding. Upon do_exit(), the kernel carefully walks this list, | |
1805 | * and marks all locks that are owned by this thread with the | |
c87e2837 | 1806 | * FUTEX_OWNER_DIED bit, and wakes up a waiter (if any). The list is |
0771dfef IM |
1807 | * always manipulated with the lock held, so the list is private and |
1808 | * per-thread. Userspace also maintains a per-thread 'list_op_pending' | |
1809 | * field, to allow the kernel to clean up if the thread dies after | |
1810 | * acquiring the lock, but just before it could have added itself to | |
1811 | * the list. There can only be one such pending lock. | |
1812 | */ | |
1813 | ||
1814 | /** | |
1815 | * sys_set_robust_list - set the robust-futex list head of a task | |
1816 | * @head: pointer to the list-head | |
1817 | * @len: length of the list-head, as userspace expects | |
1818 | */ | |
1819 | asmlinkage long | |
1820 | sys_set_robust_list(struct robust_list_head __user *head, | |
1821 | size_t len) | |
1822 | { | |
1823 | /* | |
1824 | * The kernel knows only one size for now: | |
1825 | */ | |
1826 | if (unlikely(len != sizeof(*head))) | |
1827 | return -EINVAL; | |
1828 | ||
1829 | current->robust_list = head; | |
1830 | ||
1831 | return 0; | |
1832 | } | |
1833 | ||
1834 | /** | |
1835 | * sys_get_robust_list - get the robust-futex list head of a task | |
1836 | * @pid: pid of the process [zero for current task] | |
1837 | * @head_ptr: pointer to a list-head pointer, the kernel fills it in | |
1838 | * @len_ptr: pointer to a length field, the kernel fills in the header size | |
1839 | */ | |
1840 | asmlinkage long | |
ba46df98 | 1841 | sys_get_robust_list(int pid, struct robust_list_head __user * __user *head_ptr, |
0771dfef IM |
1842 | size_t __user *len_ptr) |
1843 | { | |
ba46df98 | 1844 | struct robust_list_head __user *head; |
0771dfef IM |
1845 | unsigned long ret; |
1846 | ||
1847 | if (!pid) | |
1848 | head = current->robust_list; | |
1849 | else { | |
1850 | struct task_struct *p; | |
1851 | ||
1852 | ret = -ESRCH; | |
aaa2a97e | 1853 | rcu_read_lock(); |
0771dfef IM |
1854 | p = find_task_by_pid(pid); |
1855 | if (!p) | |
1856 | goto err_unlock; | |
1857 | ret = -EPERM; | |
1858 | if ((current->euid != p->euid) && (current->euid != p->uid) && | |
1859 | !capable(CAP_SYS_PTRACE)) | |
1860 | goto err_unlock; | |
1861 | head = p->robust_list; | |
aaa2a97e | 1862 | rcu_read_unlock(); |
0771dfef IM |
1863 | } |
1864 | ||
1865 | if (put_user(sizeof(*head), len_ptr)) | |
1866 | return -EFAULT; | |
1867 | return put_user(head, head_ptr); | |
1868 | ||
1869 | err_unlock: | |
aaa2a97e | 1870 | rcu_read_unlock(); |
0771dfef IM |
1871 | |
1872 | return ret; | |
1873 | } | |
1874 | ||
1875 | /* | |
1876 | * Process a futex-list entry, check whether it's owned by the | |
1877 | * dying task, and do notification if so: | |
1878 | */ | |
e3f2ddea | 1879 | int handle_futex_death(u32 __user *uaddr, struct task_struct *curr, int pi) |
0771dfef | 1880 | { |
e3f2ddea | 1881 | u32 uval, nval, mval; |
0771dfef | 1882 | |
8f17d3a5 IM |
1883 | retry: |
1884 | if (get_user(uval, uaddr)) | |
0771dfef IM |
1885 | return -1; |
1886 | ||
8f17d3a5 | 1887 | if ((uval & FUTEX_TID_MASK) == curr->pid) { |
0771dfef IM |
1888 | /* |
1889 | * Ok, this dying thread is truly holding a futex | |
1890 | * of interest. Set the OWNER_DIED bit atomically | |
1891 | * via cmpxchg, and if the value had FUTEX_WAITERS | |
1892 | * set, wake up a waiter (if any). (We have to do a | |
1893 | * futex_wake() even if OWNER_DIED is already set - | |
1894 | * to handle the rare but possible case of recursive | |
1895 | * thread-death.) The rest of the cleanup is done in | |
1896 | * userspace. | |
1897 | */ | |
e3f2ddea IM |
1898 | mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED; |
1899 | nval = futex_atomic_cmpxchg_inatomic(uaddr, uval, mval); | |
1900 | ||
c87e2837 IM |
1901 | if (nval == -EFAULT) |
1902 | return -1; | |
1903 | ||
1904 | if (nval != uval) | |
8f17d3a5 | 1905 | goto retry; |
0771dfef | 1906 | |
e3f2ddea IM |
1907 | /* |
1908 | * Wake robust non-PI futexes here. The wakeup of | |
1909 | * PI futexes happens in exit_pi_state(): | |
1910 | */ | |
1911 | if (!pi) { | |
1912 | if (uval & FUTEX_WAITERS) | |
34f01cc1 | 1913 | futex_wake(uaddr, &curr->mm->mmap_sem, 1); |
e3f2ddea | 1914 | } |
0771dfef IM |
1915 | } |
1916 | return 0; | |
1917 | } | |
1918 | ||
e3f2ddea IM |
1919 | /* |
1920 | * Fetch a robust-list pointer. Bit 0 signals PI futexes: | |
1921 | */ | |
1922 | static inline int fetch_robust_entry(struct robust_list __user **entry, | |
ba46df98 AV |
1923 | struct robust_list __user * __user *head, |
1924 | int *pi) | |
e3f2ddea IM |
1925 | { |
1926 | unsigned long uentry; | |
1927 | ||
ba46df98 | 1928 | if (get_user(uentry, (unsigned long __user *)head)) |
e3f2ddea IM |
1929 | return -EFAULT; |
1930 | ||
ba46df98 | 1931 | *entry = (void __user *)(uentry & ~1UL); |
e3f2ddea IM |
1932 | *pi = uentry & 1; |
1933 | ||
1934 | return 0; | |
1935 | } | |
1936 | ||
0771dfef IM |
1937 | /* |
1938 | * Walk curr->robust_list (very carefully, it's a userspace list!) | |
1939 | * and mark any locks found there dead, and notify any waiters. | |
1940 | * | |
1941 | * We silently return on any sign of list-walking problem. | |
1942 | */ | |
1943 | void exit_robust_list(struct task_struct *curr) | |
1944 | { | |
1945 | struct robust_list_head __user *head = curr->robust_list; | |
1946 | struct robust_list __user *entry, *pending; | |
e3f2ddea | 1947 | unsigned int limit = ROBUST_LIST_LIMIT, pi, pip; |
0771dfef IM |
1948 | unsigned long futex_offset; |
1949 | ||
1950 | /* | |
1951 | * Fetch the list head (which was registered earlier, via | |
1952 | * sys_set_robust_list()): | |
1953 | */ | |
e3f2ddea | 1954 | if (fetch_robust_entry(&entry, &head->list.next, &pi)) |
0771dfef IM |
1955 | return; |
1956 | /* | |
1957 | * Fetch the relative futex offset: | |
1958 | */ | |
1959 | if (get_user(futex_offset, &head->futex_offset)) | |
1960 | return; | |
1961 | /* | |
1962 | * Fetch any possibly pending lock-add first, and handle it | |
1963 | * if it exists: | |
1964 | */ | |
e3f2ddea | 1965 | if (fetch_robust_entry(&pending, &head->list_op_pending, &pip)) |
0771dfef | 1966 | return; |
e3f2ddea | 1967 | |
0771dfef | 1968 | if (pending) |
34f01cc1 ED |
1969 | handle_futex_death((void __user *)pending + futex_offset, |
1970 | curr, pip); | |
0771dfef IM |
1971 | |
1972 | while (entry != &head->list) { | |
1973 | /* | |
1974 | * A pending lock might already be on the list, so | |
c87e2837 | 1975 | * don't process it twice: |
0771dfef IM |
1976 | */ |
1977 | if (entry != pending) | |
ba46df98 | 1978 | if (handle_futex_death((void __user *)entry + futex_offset, |
e3f2ddea | 1979 | curr, pi)) |
0771dfef | 1980 | return; |
0771dfef IM |
1981 | /* |
1982 | * Fetch the next entry in the list: | |
1983 | */ | |
e3f2ddea | 1984 | if (fetch_robust_entry(&entry, &entry->next, &pi)) |
0771dfef IM |
1985 | return; |
1986 | /* | |
1987 | * Avoid excessively long or circular lists: | |
1988 | */ | |
1989 | if (!--limit) | |
1990 | break; | |
1991 | ||
1992 | cond_resched(); | |
1993 | } | |
1994 | } | |
1995 | ||
c19384b5 | 1996 | long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout, |
e2970f2f | 1997 | u32 __user *uaddr2, u32 val2, u32 val3) |
1da177e4 LT |
1998 | { |
1999 | int ret; | |
34f01cc1 ED |
2000 | int cmd = op & FUTEX_CMD_MASK; |
2001 | struct rw_semaphore *fshared = NULL; | |
2002 | ||
2003 | if (!(op & FUTEX_PRIVATE_FLAG)) | |
2004 | fshared = ¤t->mm->mmap_sem; | |
1da177e4 | 2005 | |
34f01cc1 | 2006 | switch (cmd) { |
1da177e4 | 2007 | case FUTEX_WAIT: |
34f01cc1 | 2008 | ret = futex_wait(uaddr, fshared, val, timeout); |
1da177e4 LT |
2009 | break; |
2010 | case FUTEX_WAKE: | |
34f01cc1 | 2011 | ret = futex_wake(uaddr, fshared, val); |
1da177e4 LT |
2012 | break; |
2013 | case FUTEX_FD: | |
2014 | /* non-zero val means F_SETOWN(getpid()) & F_SETSIG(val) */ | |
2015 | ret = futex_fd(uaddr, val); | |
2016 | break; | |
2017 | case FUTEX_REQUEUE: | |
34f01cc1 | 2018 | ret = futex_requeue(uaddr, fshared, uaddr2, val, val2, NULL); |
1da177e4 LT |
2019 | break; |
2020 | case FUTEX_CMP_REQUEUE: | |
34f01cc1 | 2021 | ret = futex_requeue(uaddr, fshared, uaddr2, val, val2, &val3); |
1da177e4 | 2022 | break; |
4732efbe | 2023 | case FUTEX_WAKE_OP: |
34f01cc1 | 2024 | ret = futex_wake_op(uaddr, fshared, uaddr2, val, val2, val3); |
4732efbe | 2025 | break; |
c87e2837 | 2026 | case FUTEX_LOCK_PI: |
34f01cc1 | 2027 | ret = futex_lock_pi(uaddr, fshared, val, timeout, 0); |
c87e2837 IM |
2028 | break; |
2029 | case FUTEX_UNLOCK_PI: | |
34f01cc1 | 2030 | ret = futex_unlock_pi(uaddr, fshared); |
c87e2837 IM |
2031 | break; |
2032 | case FUTEX_TRYLOCK_PI: | |
34f01cc1 | 2033 | ret = futex_lock_pi(uaddr, fshared, 0, timeout, 1); |
c87e2837 | 2034 | break; |
1da177e4 LT |
2035 | default: |
2036 | ret = -ENOSYS; | |
2037 | } | |
2038 | return ret; | |
2039 | } | |
2040 | ||
2041 | ||
e2970f2f | 2042 | asmlinkage long sys_futex(u32 __user *uaddr, int op, u32 val, |
1da177e4 | 2043 | struct timespec __user *utime, u32 __user *uaddr2, |
e2970f2f | 2044 | u32 val3) |
1da177e4 | 2045 | { |
c19384b5 PP |
2046 | struct timespec ts; |
2047 | ktime_t t, *tp = NULL; | |
e2970f2f | 2048 | u32 val2 = 0; |
34f01cc1 | 2049 | int cmd = op & FUTEX_CMD_MASK; |
1da177e4 | 2050 | |
34f01cc1 | 2051 | if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI)) { |
c19384b5 | 2052 | if (copy_from_user(&ts, utime, sizeof(ts)) != 0) |
1da177e4 | 2053 | return -EFAULT; |
c19384b5 | 2054 | if (!timespec_valid(&ts)) |
9741ef96 | 2055 | return -EINVAL; |
c19384b5 PP |
2056 | |
2057 | t = timespec_to_ktime(ts); | |
34f01cc1 | 2058 | if (cmd == FUTEX_WAIT) |
c19384b5 PP |
2059 | t = ktime_add(ktime_get(), t); |
2060 | tp = &t; | |
1da177e4 LT |
2061 | } |
2062 | /* | |
34f01cc1 | 2063 | * requeue parameter in 'utime' if cmd == FUTEX_REQUEUE. |
1da177e4 | 2064 | */ |
bd197234 | 2065 | if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE) |
e2970f2f | 2066 | val2 = (u32) (unsigned long) utime; |
1da177e4 | 2067 | |
c19384b5 | 2068 | return do_futex(uaddr, op, val, tp, uaddr2, val2, val3); |
1da177e4 LT |
2069 | } |
2070 | ||
454e2398 DH |
2071 | static int futexfs_get_sb(struct file_system_type *fs_type, |
2072 | int flags, const char *dev_name, void *data, | |
2073 | struct vfsmount *mnt) | |
1da177e4 | 2074 | { |
454e2398 | 2075 | return get_sb_pseudo(fs_type, "futex", NULL, 0xBAD1DEA, mnt); |
1da177e4 LT |
2076 | } |
2077 | ||
2078 | static struct file_system_type futex_fs_type = { | |
2079 | .name = "futexfs", | |
2080 | .get_sb = futexfs_get_sb, | |
2081 | .kill_sb = kill_anon_super, | |
2082 | }; | |
2083 | ||
2084 | static int __init init(void) | |
2085 | { | |
95362fa9 AM |
2086 | int i = register_filesystem(&futex_fs_type); |
2087 | ||
2088 | if (i) | |
2089 | return i; | |
1da177e4 | 2090 | |
1da177e4 | 2091 | futex_mnt = kern_mount(&futex_fs_type); |
95362fa9 AM |
2092 | if (IS_ERR(futex_mnt)) { |
2093 | unregister_filesystem(&futex_fs_type); | |
2094 | return PTR_ERR(futex_mnt); | |
2095 | } | |
1da177e4 LT |
2096 | |
2097 | for (i = 0; i < ARRAY_SIZE(futex_queues); i++) { | |
ec92d082 | 2098 | plist_head_init(&futex_queues[i].chain, &futex_queues[i].lock); |
1da177e4 LT |
2099 | spin_lock_init(&futex_queues[i].lock); |
2100 | } | |
2101 | return 0; | |
2102 | } | |
2103 | __initcall(init); |