47 #include <linux/slab.h>
48 #include <linux/poll.h>
53 #include <linux/futex.h>
57 #include <linux/signal.h>
58 #include <linux/export.h>
59 #include <linux/magic.h>
62 #include <linux/ptrace.h>
64 #include <asm/futex.h>
70 #define FUTEX_HASHBITS (CONFIG_BASE_SMALL ? 4 : 8)
76 #define FLAGS_SHARED 0x01
77 #define FLAGS_CLOCKRT 0x02
78 #define FLAGS_HAS_TIMEOUT 0x04
135 static const struct futex_q futex_q_init = {
159 (
sizeof(key->
both.word)+
sizeof(key->
both.ptr))/4,
170 && key1->
both.word == key2->
both.word
172 && key1->
both.offset == key2->
both.offset);
199 static void drop_futex_key_refs(
union futex_key *key)
201 if (!key->
both.ptr) {
248 address -= key->
both.offset;
262 get_futex_key_refs(key);
281 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
288 page_head = compound_head(page);
299 if (page != page_head) {
310 page_head = compound_head(page);
311 if (page != page_head) {
317 lock_page(page_head);
335 int shmem_swizzled = PageSwapCache(page_head);
350 if (PageAnon(page_head)) {
369 get_futex_key_refs(key);
377 static inline void put_futex_key(
union futex_key *key)
379 drop_futex_key_refs(key);
394 static int fault_in_user_writeable(
u32 __user *uaddr)
404 return ret < 0 ? ret : 0;
420 if (match_futex(&this->key, key))
426 static int cmpxchg_futex_value_locked(
u32 *curval,
u32 __user *uaddr,
432 ret = futex_atomic_cmpxchg_inatomic(curval, uaddr, uval, newval);
453 static int refill_pi_state_cache(
void)
460 pi_state = kzalloc(
sizeof(*pi_state),
GFP_KERNEL);
465 INIT_LIST_HEAD(&pi_state->
list);
471 current->pi_state_cache = pi_state;
495 if (pi_state->
owner) {
497 list_del_init(&pi_state->
list);
513 current->pi_state_cache = pi_state;
555 while (!list_empty(head)) {
560 hb = hash_futex(&key);
563 spin_lock(&hb->
lock);
570 if (head->
next != next) {
571 spin_unlock(&hb->
lock);
577 list_del_init(&pi_state->
list);
583 spin_unlock(&hb->
lock);
603 if (match_futex(&this->key, key)) {
608 pi_state = this->pi_state;
627 if (pid && pi_state->
owner) {
632 if (pid != task_pid_vnr(pi_state->
owner))
649 p = futex_find_get_task(pid);
673 pi_state = alloc_pi_state();
685 list_add(&pi_state->
list, &p->pi_state_list);
719 int lock_taken,
ret, force_take = 0;
720 u32 uval, newval, curval, vpid = task_pid_vnr(task);
723 ret = lock_taken = 0;
734 if (
unlikely(cmpxchg_futex_value_locked(&curval, uaddr, 0, newval)))
740 if ((
unlikely((curval & FUTEX_TID_MASK) == vpid)))
765 newval = (curval & ~FUTEX_TID_MASK) | vpid;
770 if (
unlikely(cmpxchg_futex_value_locked(&curval, uaddr, uval, newval)))
785 ret = lookup_pi_state(uval, hb, key, ps);
800 if (get_futex_value_locked(&curval, uaddr))
808 if (!(curval & FUTEX_TID_MASK)) {
826 static void __unqueue_futex(
struct futex_q *
q)
842 static void wake_futex(
struct futex_q *q)
872 static int wake_futex_pi(
u32 __user *uaddr,
u32 uval,
struct futex_q *
this)
897 new_owner = this->
task;
909 if (cmpxchg_futex_value_locked(&curval, uaddr, uval, newval))
911 else if (curval != uval)
921 list_del_init(&pi_state->
list);
926 list_add(&pi_state->
list, &new_owner->pi_state_list);
927 pi_state->
owner = new_owner;
936 static int unlock_futex_pi(
u32 __user *uaddr,
u32 uval)
944 if (cmpxchg_futex_value_locked(&oldval, uaddr, uval, 0))
959 spin_lock(&hb1->
lock);
963 spin_lock(&hb2->
lock);
971 spin_unlock(&hb1->
lock);
973 spin_unlock(&hb2->
lock);
980 futex_wake(
u32 __user *uaddr,
unsigned int flags,
int nr_wake,
u32 bitset)
995 hb = hash_futex(&key);
996 spin_lock(&hb->
lock);
1000 if (match_futex (&this->key, &key)) {
1001 if (this->pi_state || this->rt_waiter) {
1007 if (!(this->bitset & bitset))
1011 if (++ret >= nr_wake)
1016 spin_unlock(&hb->
lock);
1017 put_futex_key(&key);
1027 futex_wake_op(
u32 __user *uaddr1,
unsigned int flags,
u32 __user *uaddr2,
1028 int nr_wake,
int nr_wake2,
int op)
1044 hb1 = hash_futex(&key1);
1045 hb2 = hash_futex(&key2);
1048 double_lock_hb(hb1, hb2);
1052 double_unlock_hb(hb1, hb2);
1068 ret = fault_in_user_writeable(uaddr2);
1075 put_futex_key(&key2);
1076 put_futex_key(&key1);
1083 if (match_futex (&this->key, &key1)) {
1084 if (this->pi_state || this->
rt_waiter) {
1089 if (++ret >= nr_wake)
1099 if (match_futex (&this->key, &key2)) {
1100 if (this->pi_state || this->
rt_waiter) {
1105 if (++op_ret >= nr_wake2)
1113 double_unlock_hb(hb1, hb2);
1115 put_futex_key(&key2);
1117 put_futex_key(&key1);
1143 get_futex_key_refs(key2);
1165 get_futex_key_refs(key);
1198 static int futex_proxy_trylock_atomic(
u32 __user *pifutex,
1208 if (get_futex_value_locked(&curval, pifutex))
1219 top_waiter = futex_top_waiter(hb1, key1);
1234 ret = futex_lock_pi_atomic(pifutex, hb2, key2, ps, top_waiter->
task,
1237 requeue_pi_wake_futex(top_waiter, key2, hb2);
1260 static int futex_requeue(
u32 __user *uaddr1,
unsigned int flags,
1261 u32 __user *uaddr2,
int nr_wake,
int nr_requeue,
1262 u32 *cmpval,
int requeue_pi)
1265 int drop_count = 0, task_count = 0,
ret;
1277 if (refill_pi_state_cache())
1294 if (pi_state !=
NULL) {
1299 free_pi_state(pi_state);
1306 ret = get_futex_key(uaddr2, flags &
FLAGS_SHARED, &key2,
1311 hb1 = hash_futex(&key1);
1312 hb2 = hash_futex(&key2);
1315 double_lock_hb(hb1, hb2);
1320 ret = get_futex_value_locked(&curval, uaddr1);
1323 double_unlock_hb(hb1, hb2);
1332 put_futex_key(&key2);
1333 put_futex_key(&key1);
1336 if (curval != *cmpval) {
1342 if (requeue_pi && (task_count - nr_wake < nr_requeue)) {
1349 ret = futex_proxy_trylock_atomic(uaddr2, hb1, hb2, &key1,
1350 &key2, &pi_state, nr_requeue);
1362 ret = get_futex_value_locked(&curval2, uaddr2);
1364 ret = lookup_pi_state(curval2, hb2, &key2,
1372 double_unlock_hb(hb1, hb2);
1373 put_futex_key(&key2);
1374 put_futex_key(&key1);
1375 ret = fault_in_user_writeable(uaddr2);
1381 double_unlock_hb(hb1, hb2);
1382 put_futex_key(&key2);
1383 put_futex_key(&key1);
1391 head1 = &hb1->
chain;
1393 if (task_count - nr_wake >= nr_requeue)
1396 if (!match_futex(&this->key, &key1))
1418 if (++task_count <= nr_wake && !requeue_pi) {
1442 requeue_pi_wake_futex(
this, &key2, hb2);
1447 this->pi_state =
NULL;
1448 free_pi_state(pi_state);
1452 requeue_futex(
this, hb1, hb2, &key2);
1457 double_unlock_hb(hb1, hb2);
1465 while (--drop_count >= 0)
1466 drop_futex_key_refs(&key1);
1469 put_futex_key(&key2);
1471 put_futex_key(&key1);
1473 if (pi_state !=
NULL)
1474 free_pi_state(pi_state);
1475 return ret ? ret : task_count;
1484 hb = hash_futex(&q->
key);
1487 spin_lock(&hb->
lock);
1495 spin_unlock(&hb->
lock);
1525 plist_node_init(&q->
list, prio);
1528 spin_unlock(&hb->
lock);
1542 static int unqueue_me(
struct futex_q *q)
1551 if (lock_ptr !=
NULL) {
1552 spin_lock(lock_ptr);
1567 spin_unlock(lock_ptr);
1574 spin_unlock(lock_ptr);
1578 drop_futex_key_refs(&q->
key);
1587 static void unqueue_me_pi(
struct futex_q *q)
1605 static int fixup_pi_state_owner(
u32 __user *uaddr,
struct futex_q *q,
1615 if (!pi_state->
owner)
1636 if (get_futex_value_locked(&uval, uaddr))
1642 if (cmpxchg_futex_value_locked(&curval, uaddr, uval, newval))
1656 list_del_init(&pi_state->
list);
1660 pi_state->
owner = newowner;
1664 list_add(&pi_state->
list, &newowner->pi_state_list);
1681 ret = fault_in_user_writeable(uaddr);
1688 if (pi_state->
owner != oldowner)
1725 ret = fixup_pi_state_owner(uaddr, q,
current);
1750 owner = rt_mutex_owner(&q->
pi_state->pi_mutex);
1754 ret = fixup_pi_state_owner(uaddr, q, owner);
1764 "pi-state %p\n", ret,
1769 return ret ? ret :
locked;
1793 if (!hrtimer_active(&timeout->
timer))
1807 if (!timeout || timeout->
task)
1830 static int futex_wait_setup(
u32 __user *uaddr,
u32 val,
unsigned int flags,
1860 *hb = queue_lock(q);
1862 ret = get_futex_value_locked(&uval, uaddr);
1865 queue_unlock(q, *hb);
1874 put_futex_key(&q->
key);
1879 queue_unlock(q, *hb);
1885 put_futex_key(&q->
key);
1889 static int futex_wait(
u32 __user *uaddr,
unsigned int flags,
u32 val,
1895 struct futex_q q = futex_q_init;
1909 hrtimer_set_expires_range_ns(&to->
timer, *abs_time,
1918 ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
1923 futex_wait_queue_me(hb, &q, to);
1928 if (!unqueue_me(&q))
1931 if (to && !to->
task)
1946 restart->
fn = futex_wait_restart;
1958 destroy_hrtimer_on_stack(&to->
timer);
1964 static long futex_wait_restart(
struct restart_block *restart)
1975 return (
long)futex_wait(uaddr, restart->
futex.flags,
1976 restart->
futex.val, tp, restart->
futex.bitset);
1986 static int futex_lock_pi(
u32 __user *uaddr,
unsigned int flags,
int detect,
1991 struct futex_q q = futex_q_init;
1994 if (refill_pi_state_cache())
2002 hrtimer_set_expires(&to->
timer, *time);
2011 hb = queue_lock(&q);
2019 goto out_unlock_put_key;
2027 queue_unlock(&q, hb);
2028 put_futex_key(&q.
key);
2032 goto out_unlock_put_key;
2058 res = fixup_owner(uaddr, &q, !ret);
2064 ret = (res < 0) ? res : 0;
2079 queue_unlock(&q, hb);
2082 put_futex_key(&q.
key);
2085 destroy_hrtimer_on_stack(&to->
timer);
2089 queue_unlock(&q, hb);
2091 ret = fault_in_user_writeable(uaddr);
2098 put_futex_key(&q.
key);
2107 static int futex_unlock_pi(
u32 __user *uaddr,
unsigned int flags)
2122 if ((uval & FUTEX_TID_MASK) != vpid)
2129 hb = hash_futex(&key);
2130 spin_lock(&hb->
lock);
2137 if (!(uval & FUTEX_OWNER_DIED) &&
2138 cmpxchg_futex_value_locked(&uval, uaddr, vpid, 0))
2154 if (!match_futex (&this->key, &key))
2156 ret = wake_futex_pi(uaddr, uval,
this);
2169 if (!(uval & FUTEX_OWNER_DIED)) {
2170 ret = unlock_futex_pi(uaddr, uval);
2176 spin_unlock(&hb->
lock);
2177 put_futex_key(&key);
2183 spin_unlock(&hb->
lock);
2184 put_futex_key(&key);
2186 ret = fault_in_user_writeable(uaddr);
2223 if (!match_futex(&q->
key, key2)) {
2233 if (timeout && !timeout->
task)
2235 else if (signal_pending(
current))
2282 static int futex_wait_requeue_pi(
u32 __user *uaddr,
unsigned int flags,
2291 struct futex_q q = futex_q_init;
2294 if (uaddr == uaddr2)
2306 hrtimer_set_expires_range_ns(&to->
timer, *abs_time,
2329 ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
2334 futex_wait_queue_me(hb, &q, to);
2336 spin_lock(&hb->
lock);
2337 ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
2338 spin_unlock(&hb->
lock);
2359 ret = fixup_pi_state_owner(uaddr2, &q,
current);
2378 res = fixup_owner(uaddr2, &q, !ret);
2384 ret = (res < 0) ? res : 0;
2395 if (pi_mutex && rt_mutex_owner(pi_mutex) ==
current)
2397 }
else if (ret == -
EINTR) {
2409 put_futex_key(&q.
key);
2411 put_futex_key(&key2);
2416 destroy_hrtimer_on_stack(&to->
timer);
2465 size_t __user *, len_ptr)
2474 WARN_ONCE(1,
"deprecated: get_robust_list will be deleted in 2013.\n");
2491 head = p->robust_list;
2494 if (
put_user(
sizeof(*head), len_ptr))
2516 if ((uval & FUTEX_TID_MASK) == task_pid_vnr(curr)) {
2537 if (cmpxchg_futex_value_locked(&nval, uaddr, uval, mval)) {
2538 if (fault_in_user_writeable(uaddr))
2562 unsigned long uentry;
2564 if (
get_user(uentry, (
unsigned long __user *)head))
2567 *entry = (
void __user *)(uentry & ~1
UL);
2585 unsigned long futex_offset;
2595 if (fetch_robust_entry(&entry, &head->
list.next, &pi))
2610 while (entry != &head->
list) {
2615 rc = fetch_robust_entry(&next_entry, &entry->
next, &next_pi);
2620 if (entry != pending)
2646 unsigned int flags = 0;
2671 return futex_wait(uaddr, flags, val, timeout, val3);
2675 return futex_wake(uaddr, flags, val, val3);
2677 return futex_requeue(uaddr, flags, uaddr2, val, val2,
NULL, 0);
2679 return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 0);
2681 return futex_wake_op(uaddr, flags, uaddr2, val, val2, val3);
2683 return futex_lock_pi(uaddr, flags, val, timeout, 0);
2685 return futex_unlock_pi(uaddr, flags);
2687 return futex_lock_pi(uaddr, flags, 0, timeout, 1);
2690 return futex_wait_requeue_pi(uaddr, flags, val, timeout, val3,
2693 return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 1);
2713 if (!timespec_valid(&ts))
2716 t = timespec_to_ktime(ts);
2727 val2 = (
u32) (
unsigned long)
utime;
2729 return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
2732 static int __init futex_init(
void)
2747 if (cmpxchg_futex_value_locked(&curval,
NULL, 0, 0) == -
EFAULT)
2750 for (i = 0; i <
ARRAY_SIZE(futex_queues); i++) {
2751 plist_head_init(&futex_queues[i].
chain);