Merge tag 'drm/tegra/for-5.1-rc2' of git://anongit.freedesktop.org/tegra/linux into...
[linux-2.6-microblaze.git] / kernel / futex.c
index fdd312d..9e40cf7 100644 (file)
@@ -68,6 +68,7 @@
 #include <linux/freezer.h>
 #include <linux/memblock.h>
 #include <linux/fault-inject.h>
+#include <linux/refcount.h>
 
 #include <asm/futex.h>
 
@@ -212,7 +213,7 @@ struct futex_pi_state {
        struct rt_mutex pi_mutex;
 
        struct task_struct *owner;
-       atomic_t refcount;
+       refcount_t refcount;
 
        union futex_key key;
 } __randomize_layout;
@@ -321,12 +322,8 @@ static int __init fail_futex_debugfs(void)
        if (IS_ERR(dir))
                return PTR_ERR(dir);
 
-       if (!debugfs_create_bool("ignore-private", mode, dir,
-                                &fail_futex.ignore_private)) {
-               debugfs_remove_recursive(dir);
-               return -ENOMEM;
-       }
-
+       debugfs_create_bool("ignore-private", mode, dir,
+                           &fail_futex.ignore_private);
        return 0;
 }
 
@@ -803,7 +800,7 @@ static int refill_pi_state_cache(void)
        INIT_LIST_HEAD(&pi_state->list);
        /* pi_mutex gets initialized later */
        pi_state->owner = NULL;
-       atomic_set(&pi_state->refcount, 1);
+       refcount_set(&pi_state->refcount, 1);
        pi_state->key = FUTEX_KEY_INIT;
 
        current->pi_state_cache = pi_state;
@@ -823,7 +820,7 @@ static struct futex_pi_state *alloc_pi_state(void)
 
 static void get_pi_state(struct futex_pi_state *pi_state)
 {
-       WARN_ON_ONCE(!atomic_inc_not_zero(&pi_state->refcount));
+       WARN_ON_ONCE(!refcount_inc_not_zero(&pi_state->refcount));
 }
 
 /*
@@ -835,7 +832,7 @@ static void put_pi_state(struct futex_pi_state *pi_state)
        if (!pi_state)
                return;
 
-       if (!atomic_dec_and_test(&pi_state->refcount))
+       if (!refcount_dec_and_test(&pi_state->refcount))
                return;
 
        /*
@@ -865,7 +862,7 @@ static void put_pi_state(struct futex_pi_state *pi_state)
                 * refcount is at 0 - put it back to 1.
                 */
                pi_state->owner = NULL;
-               atomic_set(&pi_state->refcount, 1);
+               refcount_set(&pi_state->refcount, 1);
                current->pi_state_cache = pi_state;
        }
 }
@@ -908,7 +905,7 @@ void exit_pi_state_list(struct task_struct *curr)
                 * In that case; drop the locks to let put_pi_state() make
                 * progress and retry the loop.
                 */
-               if (!atomic_inc_not_zero(&pi_state->refcount)) {
+               if (!refcount_inc_not_zero(&pi_state->refcount)) {
                        raw_spin_unlock_irq(&curr->pi_lock);
                        cpu_relax();
                        raw_spin_lock_irq(&curr->pi_lock);
@@ -1064,7 +1061,7 @@ static int attach_to_pi_state(u32 __user *uaddr, u32 uval,
         * and futex_wait_requeue_pi() as it cannot go to 0 and consequently
         * free pi_state before we can take a reference ourselves.
         */
-       WARN_ON(!atomic_read(&pi_state->refcount));
+       WARN_ON(!refcount_read(&pi_state->refcount));
 
        /*
         * Now that we have a pi_state, we can acquire wait_lock
@@ -1467,8 +1464,7 @@ static void mark_wake_futex(struct wake_q_head *wake_q, struct futex_q *q)
         * Queue the task for later wakeup for after we've released
         * the hb->lock. wake_q_add() grabs reference to p.
         */
-       wake_q_add(wake_q, p);
-       put_task_struct(p);
+       wake_q_add_safe(wake_q, p);
 }
 
 /*
@@ -2221,11 +2217,11 @@ static inline struct futex_hash_bucket *queue_lock(struct futex_q *q)
         * decrement the counter at queue_unlock() when some error has
         * occurred and we don't end up adding the task to the list.
         */
-       hb_waiters_inc(hb);
+       hb_waiters_inc(hb); /* implies smp_mb(); (A) */
 
        q->lock_ptr = &hb->lock;
 
-       spin_lock(&hb->lock); /* implies smp_mb(); (A) */
+       spin_lock(&hb->lock);
        return hb;
 }
 
@@ -2861,35 +2857,39 @@ retry_private:
         * and BUG when futex_unlock_pi() interleaves with this.
         *
         * Therefore acquire wait_lock while holding hb->lock, but drop the
-        * latter before calling rt_mutex_start_proxy_lock(). This still fully
-        * serializes against futex_unlock_pi() as that does the exact same
-        * lock handoff sequence.
+        * latter before calling __rt_mutex_start_proxy_lock(). This
+        * interleaves with futex_unlock_pi() -- which does a similar lock
+        * handoff -- such that the latter can observe the futex_q::pi_state
+        * before __rt_mutex_start_proxy_lock() is done.
         */
        raw_spin_lock_irq(&q.pi_state->pi_mutex.wait_lock);
        spin_unlock(q.lock_ptr);
+       /*
+        * __rt_mutex_start_proxy_lock() unconditionally enqueues the @rt_waiter
+        * such that futex_unlock_pi() is guaranteed to observe the waiter when
+        * it sees the futex_q::pi_state.
+        */
        ret = __rt_mutex_start_proxy_lock(&q.pi_state->pi_mutex, &rt_waiter, current);
        raw_spin_unlock_irq(&q.pi_state->pi_mutex.wait_lock);
 
        if (ret) {
                if (ret == 1)
                        ret = 0;
-
-               spin_lock(q.lock_ptr);
-               goto no_block;
+               goto cleanup;
        }
 
-
        if (unlikely(to))
                hrtimer_start_expires(&to->timer, HRTIMER_MODE_ABS);
 
        ret = rt_mutex_wait_proxy_lock(&q.pi_state->pi_mutex, to, &rt_waiter);
 
+cleanup:
        spin_lock(q.lock_ptr);
        /*
-        * If we failed to acquire the lock (signal/timeout), we must
+        * If we failed to acquire the lock (deadlock/signal/timeout), we must
         * first acquire the hb->lock before removing the lock from the
-        * rt_mutex waitqueue, such that we can keep the hb and rt_mutex
-        * wait lists consistent.
+        * rt_mutex waitqueue, such that we can keep the hb and rt_mutex wait
+        * lists consistent.
         *
         * In particular; it is important that futex_unlock_pi() can not
         * observe this inconsistency.
@@ -3013,6 +3013,10 @@ retry:
                 * there is no point where we hold neither; and therefore
                 * wake_futex_pi() must observe a state consistent with what we
                 * observed.
+                *
+                * In particular; this forces __rt_mutex_start_proxy() to
+                * complete such that we're guaranteed to observe the
+                * rt_waiter. Also see the WARN in wake_futex_pi().
                 */
                raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
                spin_unlock(&hb->lock);
@@ -3432,6 +3436,10 @@ static int handle_futex_death(u32 __user *uaddr, struct task_struct *curr, int p
 {
        u32 uval, uninitialized_var(nval), mval;
 
+       /* Futex address must be 32bit aligned */
+       if ((((unsigned long)uaddr) % sizeof(*uaddr)) != 0)
+               return -1;
+
 retry:
        if (get_user(uval, uaddr))
                return -1;
@@ -3815,7 +3823,7 @@ err_unlock:
 #endif /* CONFIG_COMPAT */
 
 #ifdef CONFIG_COMPAT_32BIT_TIME
-COMPAT_SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val,
+SYSCALL_DEFINE6(futex_time32, u32 __user *, uaddr, int, op, u32, val,
                struct old_timespec32 __user *, utime, u32 __user *, uaddr2,
                u32, val3)
 {