]> rtime.felk.cvut.cz Git - zynq/linux.git/blobdiff - kernel/futex.c
Apply preempt_rt patch-4.9-rt1.patch.xz
[zynq/linux.git] / kernel / futex.c
index 2c4be467fecdcb69e4cf77e84a0af71034513677..064917c2d9a54236faadfe15ea1518fbcc293ab2 100644 (file)
@@ -904,7 +904,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;
                }
 
@@ -1299,6 +1301,7 @@ static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this,
        struct futex_pi_state *pi_state = this->pi_state;
        u32 uninitialized_var(curval), newval;
        WAKE_Q(wake_q);
+       WAKE_Q(wake_sleeper_q);
        bool deboost;
        int ret = 0;
 
@@ -1365,7 +1368,8 @@ static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this,
 
        raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
 
-       deboost = rt_mutex_futex_unlock(&pi_state->pi_mutex, &wake_q);
+       deboost = rt_mutex_futex_unlock(&pi_state->pi_mutex, &wake_q,
+                                       &wake_sleeper_q);
 
        /*
         * First unlock HB so the waiter does not spin on it once he got woken
@@ -1373,8 +1377,9 @@ static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this,
         * deboost first (and lose our higher priority), then the task might get
         * scheduled away before the wake up can take place.
         */
-       spin_unlock(&hb->lock);
+       deboost |= spin_unlock_no_deboost(&hb->lock);
        wake_up_q(&wake_q);
+       wake_up_q_sleeper(&wake_sleeper_q);
        if (deboost)
                rt_mutex_adjust_prio(current);
 
@@ -1924,6 +1929,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;
+                               put_pi_state(pi_state);
+                               continue;
                        } else if (ret) {
                                /*
                                 * rt_mutex_start_proxy_lock() detected a
@@ -2814,7 +2829,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;
@@ -2839,10 +2854,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))
@@ -2873,20 +2885,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) {
@@ -2895,14 +2942,15 @@ 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);
                        /*
                         * Drop the reference to the pi state which
                         * the requeue_pi() code acquired for us.
                         */
                        put_pi_state(q.pi_state);
-                       spin_unlock(q.lock_ptr);
+                       spin_unlock(&hb2->lock);
                }
        } else {
                /*
@@ -2915,7 +2963,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.