Re: [PATCH] sched: Fix race in rt_mutex_pre_schedule by removing non-atomic fetch_and_set
From: Steven Rostedt
Date: Tue Aug 26 2025 - 10:10:33 EST
On Tue, 26 Aug 2025 19:08:33 +0800
cuiguoqi <cuiguoqi@xxxxxxxxxx> wrote:
> @@ -7078,11 +7078,11 @@ const struct sched_class *__setscheduler_class(struct task_struct *p, int prio)
> * name such that if someone were to implement this function we get to compare
> * notes.
> */
> -#define fetch_and_set(x, v) ({ int _x = (x); (x) = (v); _x; })
>
> void rt_mutex_pre_schedule(void)
> {
> - lockdep_assert(!fetch_and_set(current->sched_rt_mutex, 1));
> + lockdep_assert(!current->sched_rt_mutex);
> + current->sched_rt_mutex = 1;
> sched_submit_work(current);
> }
>
> @@ -7095,7 +7095,9 @@ void rt_mutex_schedule(void)
> void rt_mutex_post_schedule(void)
> {
> sched_update_worker(current);
> - lockdep_assert(fetch_and_set(current->sched_rt_mutex, 0));
> + lockdep_assert(current->sched_rt_mutex);
> + current->sched_rt_mutex = 0;
> +
> }
Honestly, without adding a READ_ONCE() or barrier() I don't see how your
change would prevent the compiler from making the code any different?
It may have "fixed" your issue because the compiler may have done things
differently, but this change doesn't guarantee anything.
Also, could you show an example of how current->sched_rt_mutex could be
corrupted?
-- Steve