This is a note to let you know that I've just added the patch titled futex: Handle transient "ownerless" rtmutex state correctly to the 4.9-stable tree which can be found at: http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary The filename of the patch is: futex-handle-transient-ownerless-rtmutex-state-correctly.patch and it can be found in the queue-4.9 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let <stable@xxxxxxxxxxxxxxx> know about it. >From foo@baz Mon Mar 29 07:48:09 AM CEST 2021 From: Ben Hutchings <ben@xxxxxxxxxxxxxxx> Date: Sun, 28 Mar 2021 22:43:15 +0200 Subject: futex: Handle transient "ownerless" rtmutex state correctly To: stable@xxxxxxxxxxxxxxx Cc: Lee Jones <lee.jones@xxxxxxxxxx>, "Luis Claudio R. Goncalves" <lgoncalv@xxxxxxxxxx>, Florian Fainelli <f.fainelli@xxxxxxxxx> Message-ID: <YGDqY9a/qbJKK5eC@xxxxxxxxxxxxxxx> Content-Disposition: inline From: Mike Galbraith <efault@xxxxxx> commit 9f5d1c336a10c0d24e83e40b4c1b9539f7dba627 upstream. Gratian managed to trigger the BUG_ON(!newowner) in fixup_pi_state_owner(). This is one possible chain of events leading to this: Task Prio Operation T1 120 lock(F) T2 120 lock(F) -> blocks (top waiter) T3 50 (RT) lock(F) -> boosts T1 and blocks (new top waiter) XX timeout/ -> wakes T2 signal T1 50 unlock(F) -> wakes T3 (rtmutex->owner == NULL, waiter bit is set) T2 120 cleanup -> try_to_take_mutex() fails because T3 is the top waiter and the lower priority T2 cannot steal the lock. -> fixup_pi_state_owner() sees newowner == NULL -> BUG_ON() The comment states that this is invalid and rt_mutex_real_owner() must return a non NULL owner when the trylock failed, but in case of a queued and woken up waiter rt_mutex_real_owner() == NULL is a valid transient state. The higher priority waiter has simply not yet managed to take over the rtmutex. The BUG_ON() is therefore wrong and this is just another retry condition in fixup_pi_state_owner(). Drop the locks, so that T3 can make progress, and then try the fixup again. Gratian provided a great analysis, traces and a reproducer. The analysis is to the point, but it confused the hell out of that tglx dude who had to page in all the futex horrors again. Condensed version is above. [ tglx: Wrote comment and changelog ] Fixes: c1e2f0eaf015 ("futex: Avoid violating the 10th rule of futex") Reported-by: Gratian Crisan <gratian.crisan@xxxxxx> Signed-off-by: Mike Galbraith <efault@xxxxxx> Signed-off-by: Thomas Gleixner <tglx@xxxxxxxxxxxxx> Cc: stable@xxxxxxxxxxxxxxx Link: https://lore.kernel.org/r/87a6w6x7bb.fsf@xxxxxx Link: https://lore.kernel.org/r/87sg9pkvf7.fsf@xxxxxxxxxxxxxxxxxxxxxxx Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx> Signed-off-by: Ben Hutchings <ben@xxxxxxxxxxxxxxx> Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx> --- kernel/futex.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) --- a/kernel/futex.c +++ b/kernel/futex.c @@ -2497,10 +2497,22 @@ retry: } /* - * Since we just failed the trylock; there must be an owner. + * The trylock just failed, so either there is an owner or + * there is a higher priority waiter than this one. */ newowner = rt_mutex_owner(&pi_state->pi_mutex); - BUG_ON(!newowner); + /* + * If the higher priority waiter has not yet taken over the + * rtmutex then newowner is NULL. We can't return here with + * that state because it's inconsistent vs. the user space + * state. So drop the locks and try again. It's a valid + * situation and not any different from the other retry + * conditions. + */ + if (unlikely(!newowner)) { + err = -EAGAIN; + goto handle_err; + } } else { WARN_ON_ONCE(argowner != current); if (oldowner == current) { Patches currently in stable-queue which might be from ben@xxxxxxxxxxxxxxx are queue-4.9/futex-drop-hb-lock-before-enqueueing-on-the-rtmutex.patch queue-4.9/futex-rework-futex_lock_pi-to-use-rt_mutex_-_proxy_lock.patch queue-4.9/futex-avoid-freeing-an-active-timer.patch queue-4.9/futex-rt_mutex-introduce-rt_mutex_init_waiter.patch queue-4.9/futex-fix-incorrect-should_fail_futex-handling.patch queue-4.9/futex-rt_mutex-fix-rt_mutex_cleanup_proxy_lock.patch queue-4.9/futex-use-smp_store_release-in-mark_wake_futex.patch queue-4.9/futex-fix-possible-missed-wakeup.patch queue-4.9/locking-futex-allow-low-level-atomic-operations-to-return-eagain.patch queue-4.9/futex-prevent-robust-futex-exit-race.patch queue-4.9/futex-handle-transient-ownerless-rtmutex-state-correctly.patch queue-4.9/futex-handle-early-deadlock-return-correctly.patch queue-4.9/arm64-futex-bound-number-of-ldxr-stxr-loops-in-futex_wake_op.patch