• Thomas Gleixner's avatar
    futex: Handle early deadlock return correctly · 6a6f4647
    Thomas Gleixner authored
    commit 1a1fb985 upstream.
    commit 56222b21 ("futex: Drop hb->lock before enqueueing on the
    rtmutex") changed the locking rules in the futex code so that the hash
    bucket lock is not longer held while the waiter is enqueued into the
    rtmutex wait list. This made the lock and the unlock path symmetric, but
    unfortunately the possible early exit from __rt_mutex_proxy_start() due to
    a detected deadlock was not updated accordingly. That allows a concurrent
    unlocker to observe inconsitent state which triggers the warning in the
    unlock path.
    futex_lock_pi()                         futex_unlock_pi()
      queue(hb_waiter)				lock(hb->lock)
                                            // acquired hb->lock
                                            hb_waiter = futex_top_waiter()
         ---> fail
         ---> returns -EDEADLOCK
                                            // acquired wait_lock
    					  --> returns NULL
                                              --> WARN
    The problem is caused by the remove(rtmutex_waiter) in the failure case of
    __rt_mutex_proxy_start() as this lets the unlocker observe a waiter in the
    hash bucket but no waiter on the rtmutex, i.e. inconsistent state.
    The original commit handles this correctly for the other early return cases
    (timeout, signal) by delaying the removal of the rtmutex waiter until the
    returning task reacquired the hash bucket lock.
    Treat the failure case of __rt_mutex_proxy_start() in the same way and let
    the existing cleanup code handle the eventual handover of the rtmutex
    gracefully. The regular rt_mutex_proxy_start() gains the rtmutex waiter
    removal for the failure case, so that the other callsites are still
    operating correctly.
    Add proper comments to the code so all these details are fully documented.
    Thanks to Peter for helping with the analysis and writing the really
    valuable code comments.
    Fixes: 56222b21 ("futex: Drop hb->lock before enqueueing on the rtmutex")
    Reported-by: 's avatarHeiko Carstens <heiko.carstens@de.ibm.com>
    Co-developed-by: 's avatarPeter Zijlstra <peterz@infradead.org>
    Signed-off-by: 's avatarPeter Zijlstra <peterz@infradead.org>
    Signed-off-by: 's avatarThomas Gleixner <tglx@linutronix.de>
    Tested-by: 's avatarHeiko Carstens <heiko.carstens@de.ibm.com>
    Cc: Martin Schwidefsky <schwidefsky@de.ibm.com>
    Cc: linux-s390@vger.kernel.org
    Cc: Stefan Liebler <stli@linux.ibm.com>
    Cc: Sebastian Sewior <bigeasy@linutronix.de>
    Cc: stable@vger.kernel.org
    Link: https://lkml.kernel.org/r/alpine.DEB.2.21.1901292311410.1950@nanos.tec.linutronix.deSigned-off-by: 's avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
futex.c 99.6 KB