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