Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

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