Re: [PATCH] sched/wait: Fix a kthread_park race with wait_woken()

From: Peter Zijlstra
Date: Thu May 11 2023 - 18:31:46 EST


On Thu, May 11, 2023 at 09:41:30PM +0000, John Stultz wrote:
> From: Arve Hjønnevåg <arve@xxxxxxxxxxx>
>
> kthread_park and wait_woken have a similar race that kthread_stop and
> wait_woken used to have before it was fixed in
> cb6538e740d7543cd989128625cf8cac4b471e0a. Extend that fix to also cover

cb6538e740d7 ("sched/wait: Fix a kthread race with wait_woken()")

> kthread_park.
>
> Cc: Ingo Molnar <mingo@xxxxxxxxxx>
> Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
> Cc: Juri Lelli <juri.lelli@xxxxxxxxxx>
> Cc: Vincent Guittot <vincent.guittot@xxxxxxxxxx>
> Cc: Dietmar Eggemann <dietmar.eggemann@xxxxxxx>
> Cc: Steven Rostedt <rostedt@xxxxxxxxxxx>
> Cc: Ben Segall <bsegall@xxxxxxxxxx>
> Cc: Mel Gorman <mgorman@xxxxxxx>
> Cc: Daniel Bristot de Oliveira <bristot@xxxxxxxxxx>
> Cc: Valentin Schneider <vschneid@xxxxxxxxxx>
> Signed-off-by: Arve Hjønnevåg <arve@xxxxxxxxxxx>
> Signed-off-by: John Stultz <jstultz@xxxxxxxxxx>
> ---
> This seemingly slipped by, so I wanted to resend it
> for review.
> ---
> kernel/sched/wait.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/kernel/sched/wait.c b/kernel/sched/wait.c
> index 133b74730738..a9cf49da884b 100644
> --- a/kernel/sched/wait.c
> +++ b/kernel/sched/wait.c
> @@ -425,9 +425,9 @@ int autoremove_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, i
> }
> EXPORT_SYMBOL(autoremove_wake_function);
>
> -static inline bool is_kthread_should_stop(void)
> +static inline bool is_kthread_should_stop_or_park(void)
> {
> - return (current->flags & PF_KTHREAD) && kthread_should_stop();
> + return (current->flags & PF_KTHREAD) && (kthread_should_stop() || kthread_should_park());
> }
>
> /*

That's a bit sad; that two function calls for checking two consecutive
bits in the same word :-(

If we move this to kthread.c and write it like:

kthread = __to_kthread(current);
if (!kthread)
return false;

return test_bit(KTHREAD_SHOULD_STOP, &kthread->flags) ||
test_bit(KTHREAD_SHOULD_PARK, &kthread->flags);

Then the compiler should be able to merge the two bits in a single load
and test due to constant_test_bit() -- do check though.

> @@ -459,7 +459,7 @@ long wait_woken(struct wait_queue_entry *wq_entry, unsigned mode, long timeout)
> * or woken_wake_function() sees our store to current->state.
> */
> set_current_state(mode); /* A */
> - if (!(wq_entry->flags & WQ_FLAG_WOKEN) && !is_kthread_should_stop())
> + if (!(wq_entry->flags & WQ_FLAG_WOKEN) && !is_kthread_should_stop_or_park())
> timeout = schedule_timeout(timeout);
> __set_current_state(TASK_RUNNING);
>
> --
> 2.40.1.606.ga4b1b128d6-goog
>