mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
To: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: peterz@infradead.org, tglx@kernel.org, mingo@redhat.com,
	linux-kernel@vger.kernel.org, will@kernel.org, boqun@kernel.org,
	longman@redhat.com, rostedt@goodmis.org, clrkwllms@kernel.org,
	dvhart@infradead.org, dave@stgolabs.net, andrealmeid@igalia.com,
	shuah@kernel.org, juri.lelli@redhat.com,
	linux-rt-devel@lists.linux.dev, linux-kselftest@vger.kernel.org,
	stable@vger.kernel.org
Subject: Re: [PATCH v2 2/5] locking/rtmutex: Initialize the ww_mutex context of a waiter
Date: Wed, 18 Nov 2026 15:40:06 +0100	[thread overview]
Message-ID: <20261118144006.ms1fvDv1@linutronix.de> (raw)
In-Reply-To: <de727dc6-cd11-4a7e-a853-aecd895685a9@efficios.com>

On 2026-11-16 13:37:09 [-0500], Mathieu Desnoyers wrote:
> Hi Sebastian,
Hi Mathieu,

> > I *think* this is unrelated because the futex caller shouldn't end up
> > here. The __rt_mutex_slowlock() -> rt_mutex_handle_deadlock() should be
> > different for the futex and ww_mutex case.
> 
> The futex caller does not go through __rt_mutex_slowlock(). It gets to
> the chain walk through the proxy lock:
> 
>   futex_lock_pi()
>     rt_mutex_init_waiter(&rt_waiter)
>     __rt_mutex_start_proxy_lock()
>       task_blocks_on_rt_mutex(..., RT_MUTEX_FULL_CHAINWALK)
>         rt_mutex_adjust_prio_chain()
> 
> rt_mutex_adjust_prio_chain() reads waiter->ww_ctx in two places:
> 
> 	if (IS_ENABLED(CONFIG_PREEMPT_RT) && waiter->ww_ctx && detect_deadlock)
> 		detect_deadlock = false;
> 	[...]
> 		if (IS_ENABLED(CONFIG_PREEMPT_RT) && orig_waiter && orig_waiter->ww_ctx)
> 			ret = 0;
> 
> Both tests are under IS_ENABLED(CONFIG_PREEMPT_RT), not under
> build_ww_mutex(). So the futex build of the chain walk has them too.

Yes, this part. I realised this later…

> > Is this really the case?
> > You have a lock chain where the ww_mutex and a regular rtmutex interact?
> 
> No ww_mutex is needed. Two PI futexes are enough. Both waiters come from
> rt_mutex_init_waiter() in futex_lock_pi(), so both hold the 0x11 poison
> in ww_ctx with CONFIG_DEBUG_RT_MUTEXES. The chain walk takes them for
> ww_mutex waiters.
> 
> The deadlock case of futex_lock_pi_inherit (patch 4) is that ABBA. With
> PREEMPT_RT and CONFIG_DEBUG_RT_MUTEXES, it fails without this patch: the
> second FUTEX_LOCK_PI blocks. It passes with it.

right. That is an issue.

> > I would argue that the full-chainwalk depends now
> > on a random stack value _and_ __waiter_less() might access the random
> > value if ww_mutex is enqueued and a regular mutex exists in the lock
> > chain.
> 
> Yes, those are the two effects. I reproduced the first one only. The
> second one is from reading the code, which is why the changelog says
> "would dereference".

The deference _shouldn't_ happen because those two a different. However,
we could have a mix of those two and which point it becomes relevant.

> You are right that the changelog is confusing. It starts with the futex
> symptom, and only then says where the value is read. In the planned next
> version, I would put the mechanism first, with the call path above. Does
> this read better?
> 
>     rt_mutex_init_waiter() does not set waiter->ww_ctx. Only
 rt_mutex_waiter::ww_ctx please

>     __rt_mutex_slowlock_locked() sets it. So the waiters of PI futexes
>     and rtlocks keep the 0x11 poison of CONFIG_DEBUG_RT_MUTEXES. Without

or random garbage which.

>     that option, they keep what was on the stack, unless the compiler
>     zeroes it.
> 
>     With PREEMPT_RT, ww_mutexes are built on rtmutexes. They can create
>     a cycle in the lock graph legitimately, and the wound/die logic
>     resolves it. So rt_mutex_adjust_prio_chain() turns deadlock
>     detection off when it finds a waiter with a ww_ctx. It also drops
>     the -EDEADLK of a walk that started from such a waiter. Both tests
>     are under IS_ENABLED(CONFIG_PREEMPT_RT), so every build of the chain
>     walk has them.

I would strip that paragraph because *I* think it is more important to
mention the FUTEX case which no longer does proper chainwalk regardless
of PREEMPT_RT. Yes, ww_mutex has 

>     FUTEX_LOCK_PI gets there with no ww_mutex in the chain:
> 
>       futex_lock_pi()
>         rt_mutex_init_waiter()
>         __rt_mutex_start_proxy_lock()
>           task_blocks_on_rt_mutex()
>             rt_mutex_adjust_prio_chain()
> 
>     The chain walk takes its waiters for ww_mutex waiters. With
>     PREEMPT_RT and CONFIG_DEBUG_RT_MUTEXES, FUTEX_LOCK_PI then reports
>     no deadlock. Two threads that each block on a PI futex the other
>     holds sleep forever. Without PREEMPT_RT, the second one gets
>     EDEADLK. FUTEX_LOCK_PI also blocks on a chain deeper than
>     max_lock_depth, instead of returning EDEADLK.
> 
>     The ww_mutex build of the chain walk can also requeue such waiters.
>     On two of them with equal priority, __waiter_less() would
>     dereference the poison.

The futex code for PI_LOCK performs a RT_MUTEX_FULL_CHAINWALK during
locking. rt_mutex_adjust_prio_chain() then wrongly assumes that it is a
ww_mutex (due to random garbage in rt_mutex_waiter::ww_ctx) and does not
report a deadlock but expects the caller to handle it.

>     Fix this by clearing waiter->ww_ctx in rt_mutex_init_waiter().
rt_mutex_waiter::ww_ctx

This, Fixes: and 

Reviewed-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>

> Thanks,
> 
> Mathieu

Sebastian

  reply	other threads:[~2026-11-18 14:40 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-08 14:13 [PATCH v2 0/5] locking/rtmutex: priority inheritance fixes and tests Mathieu Desnoyers
2026-11-08 14:13 ` [PATCH v2 1/5] locking/rtmutex: Stop spinning on the owner when the waiter is woken Mathieu Desnoyers
2026-11-16 12:06   ` Sebastian Andrzej Siewior
2026-11-16 18:36     ` Mathieu Desnoyers
2026-11-18 14:19       ` Sebastian Andrzej Siewior
2026-11-08 14:13 ` [PATCH v2 2/5] locking/rtmutex: Initialize the ww_mutex context of a waiter Mathieu Desnoyers
2026-11-16 13:58   ` Sebastian Andrzej Siewior
2026-11-16 18:37     ` Mathieu Desnoyers
2026-11-18 14:40       ` Sebastian Andrzej Siewior [this message]
2026-11-08 14:13 ` [PATCH v2 3/5] locking/rtmutex: Clone the sort keys in rt_mutex_enqueue_pi() Mathieu Desnoyers
2026-11-16 15:11   ` Sebastian Andrzej Siewior
2026-11-16 18:35     ` Mathieu Desnoyers
2026-11-08 14:13 ` [PATCH v2 4/5] selftests/futex: Test priority inheritance on PI futexes Mathieu Desnoyers
2026-11-16 16:56   ` Sebastian Andrzej Siewior
2026-11-16 18:48     ` Mathieu Desnoyers
2026-11-08 14:13 ` [PATCH v2 5/5] locking/ww_mutex/test: Time an ABBA with both owners running Mathieu Desnoyers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261118144006.ms1fvDv1@linutronix.de \
    --to=bigeasy@linutronix.de \
    --cc=andrealmeid@igalia.com \
    --cc=boqun@kernel.org \
    --cc=clrkwllms@kernel.org \
    --cc=dave@stgolabs.net \
    --cc=dvhart@infradead.org \
    --cc=juri.lelli@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-rt-devel@lists.linux.dev \
    --cc=longman@redhat.com \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=shuah@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=tglx@kernel.org \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®