]> rtime.felk.cvut.cz Git - zynq/linux.git/blobdiff - kernel/futex.c
Merge branch '4.0.8-rt6'
[zynq/linux.git] / kernel / futex.c
index 2a5e3830e953b72cca4aeac4ec537e276f0f355b..a353a594f8fd7b07b20a47825e57a344bf3d5f78 100644 (file)
@@ -738,7 +738,9 @@ void exit_pi_state_list(struct task_struct *curr)
                 * task still owns the PI-state:
                 */
                if (head->next != next) {
+                       raw_spin_unlock_irq(&curr->pi_lock);
                        spin_unlock(&hb->lock);
+                       raw_spin_lock_irq(&curr->pi_lock);
                        continue;
                }
 
@@ -1090,9 +1092,11 @@ static void __unqueue_futex(struct futex_q *q)
 
 /*
  * The hash bucket lock must be held when this is called.
- * Afterwards, the futex_q must not be accessed.
+ * Afterwards, the futex_q must not be accessed. Callers
+ * must ensure to later call wake_up_q() for the actual
+ * wakeups to occur.
  */
-static void wake_futex(struct futex_q *q)
+static void mark_wake_futex(struct wake_q_head *wake_q, struct futex_q *q)
 {
        struct task_struct *p = q->task;
 
@@ -1100,14 +1104,10 @@ static void wake_futex(struct futex_q *q)
                return;
 
        /*
-        * We set q->lock_ptr = NULL _before_ we wake up the task. If
-        * a non-futex wake up happens on another CPU then the task
-        * might exit and p would dereference a non-existing task
-        * struct. Prevent this by holding a reference on p across the
-        * wake up.
+        * Queue the task for later wakeup for after we've released
+        * the hb->lock. wake_q_add() grabs reference to p.
         */
-       get_task_struct(p);
-
+       wake_q_add(wake_q, p);
        __unqueue_futex(q);
        /*
         * The waiting task can free the futex_q as soon as
@@ -1117,16 +1117,15 @@ static void wake_futex(struct futex_q *q)
         */
        smp_wmb();
        q->lock_ptr = NULL;
-
-       wake_up_state(p, TASK_NORMAL);
-       put_task_struct(p);
 }
 
-static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this)
+static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this,
+                        struct futex_hash_bucket *hb)
 {
        struct task_struct *new_owner;
        struct futex_pi_state *pi_state = this->pi_state;
        u32 uninitialized_var(curval), newval;
+       bool deboost;
        int ret = 0;
 
        if (!pi_state)
@@ -1178,7 +1177,17 @@ static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this)
        raw_spin_unlock_irq(&new_owner->pi_lock);
 
        raw_spin_unlock(&pi_state->pi_mutex.wait_lock);
-       rt_mutex_unlock(&pi_state->pi_mutex);
+
+       deboost = rt_mutex_futex_unlock(&pi_state->pi_mutex);
+
+       /*
+        * We deboost after dropping hb->lock. That prevents a double
+        * wakeup on RT.
+        */
+       spin_unlock(&hb->lock);
+
+       if (deboost)
+               rt_mutex_adjust_prio(current);
 
        return 0;
 }
@@ -1217,6 +1226,7 @@ futex_wake(u32 __user *uaddr, unsigned int flags, int nr_wake, u32 bitset)
        struct futex_q *this, *next;
        union futex_key key = FUTEX_KEY_INIT;
        int ret;
+       WAKE_Q(wake_q);
 
        if (!bitset)
                return -EINVAL;
@@ -1244,13 +1254,14 @@ futex_wake(u32 __user *uaddr, unsigned int flags, int nr_wake, u32 bitset)
                        if (!(this->bitset & bitset))
                                continue;
 
-                       wake_futex(this);
+                       mark_wake_futex(&wake_q, this);
                        if (++ret >= nr_wake)
                                break;
                }
        }
 
        spin_unlock(&hb->lock);
+       wake_up_q(&wake_q);
 out_put_key:
        put_futex_key(&key);
 out:
@@ -1269,6 +1280,7 @@ futex_wake_op(u32 __user *uaddr1, unsigned int flags, u32 __user *uaddr2,
        struct futex_hash_bucket *hb1, *hb2;
        struct futex_q *this, *next;
        int ret, op_ret;
+       WAKE_Q(wake_q);
 
 retry:
        ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, VERIFY_READ);
@@ -1320,7 +1332,7 @@ retry_private:
                                ret = -EINVAL;
                                goto out_unlock;
                        }
-                       wake_futex(this);
+                       mark_wake_futex(&wake_q, this);
                        if (++ret >= nr_wake)
                                break;
                }
@@ -1334,7 +1346,7 @@ retry_private:
                                        ret = -EINVAL;
                                        goto out_unlock;
                                }
-                               wake_futex(this);
+                               mark_wake_futex(&wake_q, this);
                                if (++op_ret >= nr_wake2)
                                        break;
                        }
@@ -1344,6 +1356,7 @@ retry_private:
 
 out_unlock:
        double_unlock_hb(hb1, hb2);
+       wake_up_q(&wake_q);
 out_put_keys:
        put_futex_key(&key2);
 out_put_key1:
@@ -1503,6 +1516,7 @@ static int futex_requeue(u32 __user *uaddr1, unsigned int flags,
        struct futex_pi_state *pi_state = NULL;
        struct futex_hash_bucket *hb1, *hb2;
        struct futex_q *this, *next;
+       WAKE_Q(wake_q);
 
        if (requeue_pi) {
                /*
@@ -1679,7 +1693,7 @@ retry_private:
                 * woken by futex_unlock_pi().
                 */
                if (++task_count <= nr_wake && !requeue_pi) {
-                       wake_futex(this);
+                       mark_wake_futex(&wake_q, this);
                        continue;
                }
 
@@ -1705,6 +1719,16 @@ retry_private:
                                requeue_pi_wake_futex(this, &key2, hb2);
                                drop_count++;
                                continue;
+                       } else if (ret == -EAGAIN) {
+                               /*
+                                * Waiter was woken by timeout or
+                                * signal and has set pi_blocked_on to
+                                * PI_WAKEUP_INPROGRESS before we
+                                * tried to enqueue it on the rtmutex.
+                                */
+                               this->pi_state = NULL;
+                               free_pi_state(pi_state);
+                               continue;
                        } else if (ret) {
                                /* -EDEADLK */
                                this->pi_state = NULL;
@@ -1719,6 +1743,7 @@ retry_private:
 out_unlock:
        free_pi_state(pi_state);
        double_unlock_hb(hb1, hb2);
+       wake_up_q(&wake_q);
        hb_waiters_dec(hb2);
 
        /*
@@ -2412,13 +2437,26 @@ retry:
         */
        match = futex_top_waiter(hb, &key);
        if (match) {
-               ret = wake_futex_pi(uaddr, uval, match);
+               ret = wake_futex_pi(uaddr, uval, match, hb);
+
+               /*
+                * In case of success wake_futex_pi dropped the hash
+                * bucket lock.
+                */
+               if (!ret)
+                       goto out_putkey;
+
                /*
                 * The atomic access to the futex value generated a
                 * pagefault, so retry the user-access and the wakeup:
                 */
                if (ret == -EFAULT)
                        goto pi_faulted;
+
+               /*
+                * wake_futex_pi has detected invalid state. Tell user
+                * space.
+                */
                goto out_unlock;
        }
 
@@ -2439,6 +2477,7 @@ retry:
 
 out_unlock:
        spin_unlock(&hb->lock);
+out_putkey:
        put_futex_key(&key);
        return ret;
 
@@ -2549,7 +2588,7 @@ static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
        struct hrtimer_sleeper timeout, *to = NULL;
        struct rt_mutex_waiter rt_waiter;
        struct rt_mutex *pi_mutex = NULL;
-       struct futex_hash_bucket *hb;
+       struct futex_hash_bucket *hb, *hb2;
        union futex_key key2 = FUTEX_KEY_INIT;
        struct futex_q q = futex_q_init;
        int res, ret;
@@ -2574,10 +2613,7 @@ static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
         * The waiter is allocated on our stack, manipulated by the requeue
         * code while we sleep on uaddr.
         */
-       debug_rt_mutex_init_waiter(&rt_waiter);
-       RB_CLEAR_NODE(&rt_waiter.pi_tree_entry);
-       RB_CLEAR_NODE(&rt_waiter.tree_entry);
-       rt_waiter.task = NULL;
+       rt_mutex_init_waiter(&rt_waiter, false);
 
        ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE);
        if (unlikely(ret != 0))
@@ -2608,20 +2644,55 @@ static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
        /* Queue the futex_q, drop the hb lock, wait for wakeup. */
        futex_wait_queue_me(hb, &q, to);
 
-       spin_lock(&hb->lock);
-       ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
-       spin_unlock(&hb->lock);
-       if (ret)
-               goto out_put_keys;
+       /*
+        * On RT we must avoid races with requeue and trying to block
+        * on two mutexes (hb->lock and uaddr2's rtmutex) by
+        * serializing access to pi_blocked_on with pi_lock.
+        */
+       raw_spin_lock_irq(&current->pi_lock);
+       if (current->pi_blocked_on) {
+               /*
+                * We have been requeued or are in the process of
+                * being requeued.
+                */
+               raw_spin_unlock_irq(&current->pi_lock);
+       } else {
+               /*
+                * Setting pi_blocked_on to PI_WAKEUP_INPROGRESS
+                * prevents a concurrent requeue from moving us to the
+                * uaddr2 rtmutex. After that we can safely acquire
+                * (and possibly block on) hb->lock.
+                */
+               current->pi_blocked_on = PI_WAKEUP_INPROGRESS;
+               raw_spin_unlock_irq(&current->pi_lock);
+
+               spin_lock(&hb->lock);
+
+               /*
+                * Clean up pi_blocked_on. We might leak it otherwise
+                * when we succeeded with the hb->lock in the fast
+                * path.
+                */
+               raw_spin_lock_irq(&current->pi_lock);
+               current->pi_blocked_on = NULL;
+               raw_spin_unlock_irq(&current->pi_lock);
+
+               ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
+               spin_unlock(&hb->lock);
+               if (ret)
+                       goto out_put_keys;
+       }
 
        /*
-        * In order for us to be here, we know our q.key == key2, and since
-        * we took the hb->lock above, we also know that futex_requeue() has
-        * completed and we no longer have to concern ourselves with a wakeup
-        * race with the atomic proxy lock acquisition by the requeue code. The
-        * futex_requeue dropped our key1 reference and incremented our key2
-        * reference count.
+        * In order to be here, we have either been requeued, are in
+        * the process of being requeued, or requeue successfully
+        * acquired uaddr2 on our behalf.  If pi_blocked_on was
+        * non-null above, we may be racing with a requeue.  Do not
+        * rely on q->lock_ptr to be hb2->lock until after blocking on
+        * hb->lock or hb2->lock. The futex_requeue dropped our key1
+        * reference and incremented our key2 reference count.
         */
+       hb2 = hash_futex(&key2);
 
        /* Check if the requeue code acquired the second futex for us. */
        if (!q.rt_waiter) {
@@ -2630,9 +2701,10 @@ static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
                 * did a lock-steal - fix up the PI-state in that case.
                 */
                if (q.pi_state && (q.pi_state->owner != current)) {
-                       spin_lock(q.lock_ptr);
+                       spin_lock(&hb2->lock);
+                       BUG_ON(&hb2->lock != q.lock_ptr);
                        ret = fixup_pi_state_owner(uaddr2, &q, current);
-                       spin_unlock(q.lock_ptr);
+                       spin_unlock(&hb2->lock);
                }
        } else {
                /*
@@ -2645,7 +2717,8 @@ static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
                ret = rt_mutex_finish_proxy_lock(pi_mutex, to, &rt_waiter);
                debug_rt_mutex_free_waiter(&rt_waiter);
 
-               spin_lock(q.lock_ptr);
+               spin_lock(&hb2->lock);
+               BUG_ON(&hb2->lock != q.lock_ptr);
                /*
                 * Fixup the pi_state owner and possibly acquire the lock if we
                 * haven't already.