mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: cuiguoqi <cuiguoqi@kylinos.cn>
Cc: rostedt@goodmis.org, bigeasy@linutronix.de, bsegall@google.com,
	clrkwllms@kernel.org, dietmar.eggemann@arm.com,
	guoqi0226@163.com, juri.lelli@redhat.com,
	linux-kernel@vger.kernel.org, linux-rt-devel@lists.linux.dev,
	mgorman@suse.de, mingo@redhat.com, vincent.guittot@linaro.org,
	vschneid@redhat.com
Subject: Re: [PATCH] sched: Fix race in rt_mutex_pre_schedule by removing non-atomic fetch_and_set
Date: Mon, 6 Oct 2025 21:07:39 +0200	[thread overview]
Message-ID: <20251006190739.GZ3245006@noisy.programming.kicks-ass.net> (raw)
In-Reply-To: <20250827081750.3606616-1-cuiguoqi@kylinos.cn>

On Wed, Aug 27, 2025 at 04:17:50PM +0800, cuiguoqi wrote:
> The issue arises during EDEADLK testing in `lib/locking-selftest.c` when `is_wait_die=1`.
> 
> In this mode, the current thread's `debug_locks` flag is disabled via `__debug_locks_off` (which calls `xchg(&debug_locks, 0)`) during the blocking path of `rt_mutex_slowlock`, specifically in `rt_mutex_slowlock_block()`:
> 
>   rt_mutex_slowlock()
>     rt_mutex_pre_schedule()
>       rt_mutex_slowlock_block()
>         DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock)
>           __debug_locks_off();  // xchg(&debug_locks, 0)
> 
> However, `rt_mutex_post_schedule()` still performs:
> 
>   lockdep_assert(fetch_and_set(current->sched_rt_mutex, 0));
> 
> Which expands to:
> 
>   do {
>       WARN_ON(debug_locks && !( ({ int _x = current->sched_rt_mutex; current->sched_rt_mutex = 0; _x; }) ));
>   } while (0)
> 
> The generated assembly shows that the entire assertion is conditional on `debug_locks`:
> 
>   adrp    x0, debug_locks
>   ldr     w0, [x0]
>   cbz     w0, .label_skip_warn   // Skip WARN if debug_locks == 0
> 
> This means: if `debug_locks` was cleared earlier, the check on `current->sched_rt_mutex` is effectively skipped, and the flag may remain set.
> 
> Later, when the same task re-enters `rt_mutex_slowlock`, it calls `lockdep_reset()` to re-enable `debug_locks`, but the stale `current->sched_rt_mutex` state (left over from the previous lock attempt) causes a false-positive warning in `rt_mutex_pre_schedule()`:
> 
>   WARNING: CPU: 0 PID: 0 at kernel/sched/core.c:7085 rt_mutex_pre_schedule+0xa8/0x108
> 
> Because:
>   - `rt_mutex_pre_schedule()` asserts `!current->sched_rt_mutex`
>   - But the flag was never properly cleared due to the skipped post-schedule check.
> 
> This is not a data race on the flag itself, but a **state inconsistency caused by conditional debugging logic** — the `fetch_and_set` macro is not atomic, but more importantly, the assertion is bypassed when `debug_locks` is off, breaking the expected state transition.

Yeah, I can't really make myself care too much. This means you've
already had errors before -- resulting in debug_locks getting cleared.
Fix those and this problem goes away.

debug_locks is inherently racy; I don't see value in trying to fix all
that.

      reply	other threads:[~2025-10-06 19:07 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-26 11:08 cuiguoqi
2025-08-26 13:56 ` Steven Rostedt
2025-08-26 14:16   ` Sebastian Andrzej Siewior
2025-08-27  8:17   ` cuiguoqi
2025-10-06 19:07     ` Peter Zijlstra [this message]

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=20251006190739.GZ3245006@noisy.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=bigeasy@linutronix.de \
    --cc=bsegall@google.com \
    --cc=clrkwllms@kernel.org \
    --cc=cuiguoqi@kylinos.cn \
    --cc=dietmar.eggemann@arm.com \
    --cc=guoqi0226@163.com \
    --cc=juri.lelli@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rt-devel@lists.linux.dev \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=rostedt@goodmis.org \
    --cc=vincent.guittot@linaro.org \
    --cc=vschneid@redhat.com \
    /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®