public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [RESEND][PATCH v2] sched/wait: Fix a kthread_park race with wait_woken()
@ 2023-06-02 21:23 John Stultz
  2023-06-12 15:28 ` Valentin Schneider
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: John Stultz @ 2023-06-02 21:23 UTC (permalink / raw)
  To: LKML
  Cc: Arve Hjønnevåg, Ingo Molnar, Peter Zijlstra, Juri Lelli,
	Vincent Guittot, Dietmar Eggemann, Steven Rostedt, Ben Segall,
	Mel Gorman, Daniel Bristot de Oliveira, Valentin Schneider,
	android-kernel-team, John Stultz

From: Arve Hjønnevåg <arve@android.com>

kthread_park and wait_woken have a similar race that
kthread_stop and wait_woken used to have before it was fixed in
commit cb6538e740d7 ("sched/wait: Fix a kthread race with
wait_woken()"). Extend that fix to also cover kthread_park.

Cc: Ingo Molnar <mingo@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Juri Lelli <juri.lelli@redhat.com>
Cc: Vincent Guittot <vincent.guittot@linaro.org>
Cc: Dietmar Eggemann <dietmar.eggemann@arm.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Ben Segall <bsegall@google.com>
Cc: Mel Gorman <mgorman@suse.de>
Cc: Daniel Bristot de Oliveira <bristot@redhat.com>
Cc: Valentin Schneider <vschneid@redhat.com>
Cc: android-kernel-team <android-kernel-team@google.com>
Signed-off-by: Arve Hjønnevåg <arve@android.com>
[jstultz: Made changes suggested by Peter to optimize
 memory loads]
Signed-off-by: John Stultz <jstultz@google.com>
---
v2:
* Commit message tweaks, suggested by Peter
* Move logic to kthread.c & optimize to avoid duplicate memory
  loads, also suggested by Peter
---
 include/linux/kthread.h |  1 +
 kernel/kthread.c        | 10 ++++++++++
 kernel/sched/wait.c     |  7 +------
 3 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/include/linux/kthread.h b/include/linux/kthread.h
index 30e5bec81d2b..f1f95a71a4bc 100644
--- a/include/linux/kthread.h
+++ b/include/linux/kthread.h
@@ -89,6 +89,7 @@ int kthread_stop(struct task_struct *k);
 bool kthread_should_stop(void);
 bool kthread_should_park(void);
 bool __kthread_should_park(struct task_struct *k);
+bool kthread_should_stop_or_park(void);
 bool kthread_freezable_should_stop(bool *was_frozen);
 void *kthread_func(struct task_struct *k);
 void *kthread_data(struct task_struct *k);
diff --git a/kernel/kthread.c b/kernel/kthread.c
index 490792b1066e..07a057086d26 100644
--- a/kernel/kthread.c
+++ b/kernel/kthread.c
@@ -182,6 +182,16 @@ bool kthread_should_park(void)
 }
 EXPORT_SYMBOL_GPL(kthread_should_park);
 
+bool kthread_should_stop_or_park(void)
+{
+	struct kthread *kthread = __to_kthread(current);
+
+	if (!kthread)
+		return false;
+
+	return kthread->flags & (BIT(KTHREAD_SHOULD_STOP) | BIT(KTHREAD_SHOULD_PARK));
+}
+
 /**
  * kthread_freezable_should_stop - should this freezable kthread return now?
  * @was_frozen: optional out parameter, indicates whether %current was frozen
diff --git a/kernel/sched/wait.c b/kernel/sched/wait.c
index 133b74730738..48c53e4739ea 100644
--- a/kernel/sched/wait.c
+++ b/kernel/sched/wait.c
@@ -425,11 +425,6 @@ 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)
-{
-	return (current->flags & PF_KTHREAD) && kthread_should_stop();
-}
-
 /*
  * DEFINE_WAIT_FUNC(wait, woken_wake_func);
  *
@@ -459,7 +454,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) && !kthread_should_stop_or_park())
 		timeout = schedule_timeout(timeout);
 	__set_current_state(TASK_RUNNING);
 
-- 
2.41.0.rc2.161.g9c6817b8e7-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [RESEND][PATCH v2] sched/wait: Fix a kthread_park race with wait_woken()
  2023-06-02 21:23 [RESEND][PATCH v2] sched/wait: Fix a kthread_park race with wait_woken() John Stultz
@ 2023-06-12 15:28 ` Valentin Schneider
  2023-06-15 11:38 ` [tip: sched/core] " tip-bot2 for Arve Hjønnevåg
  2023-06-16 15:16 ` tip-bot2 for Arve Hjønnevåg
  2 siblings, 0 replies; 4+ messages in thread
From: Valentin Schneider @ 2023-06-12 15:28 UTC (permalink / raw)
  To: John Stultz, LKML
  Cc: Arve Hjønnevåg, Ingo Molnar, Peter Zijlstra, Juri Lelli,
	Vincent Guittot, Dietmar Eggemann, Steven Rostedt, Ben Segall,
	Mel Gorman, Daniel Bristot de Oliveira, android-kernel-team,
	John Stultz

On 02/06/23 21:23, John Stultz wrote:
> From: Arve Hjønnevåg <arve@android.com>
>
> kthread_park and wait_woken have a similar race that
> kthread_stop and wait_woken used to have before it was fixed in
> commit cb6538e740d7 ("sched/wait: Fix a kthread race with
> wait_woken()"). Extend that fix to also cover kthread_park.
>
> Cc: Ingo Molnar <mingo@redhat.com>
> Cc: Peter Zijlstra <peterz@infradead.org>
> Cc: Juri Lelli <juri.lelli@redhat.com>
> Cc: Vincent Guittot <vincent.guittot@linaro.org>
> Cc: Dietmar Eggemann <dietmar.eggemann@arm.com>
> Cc: Steven Rostedt <rostedt@goodmis.org>
> Cc: Ben Segall <bsegall@google.com>
> Cc: Mel Gorman <mgorman@suse.de>
> Cc: Daniel Bristot de Oliveira <bristot@redhat.com>
> Cc: Valentin Schneider <vschneid@redhat.com>
> Cc: android-kernel-team <android-kernel-team@google.com>
> Signed-off-by: Arve Hjønnevåg <arve@android.com>
> [jstultz: Made changes suggested by Peter to optimize
>  memory loads]
> Signed-off-by: John Stultz <jstultz@google.com>

Funny one, IIUC this will happen with any wakeup of a wait_woken()-user
that doesn't touch the wait_queue entry.

Reviewed-by: Valentin Schneider <vschneid@redhat.com>

I do have on question: what about signals? Browsing through wait_woken()
users, it looks like a few of them have a wait-loop break condition hinging
on signal_pending() (iio_buffer_read() is one of them). signal_wake_up()
doesn't touch the wait_queue entry, so is that a similar issue or am I
looking at a non-problem?


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [tip: sched/core] sched/wait: Fix a kthread_park race with wait_woken()
  2023-06-02 21:23 [RESEND][PATCH v2] sched/wait: Fix a kthread_park race with wait_woken() John Stultz
  2023-06-12 15:28 ` Valentin Schneider
@ 2023-06-15 11:38 ` tip-bot2 for Arve Hjønnevåg
  2023-06-16 15:16 ` tip-bot2 for Arve Hjønnevåg
  2 siblings, 0 replies; 4+ messages in thread
From: tip-bot2 for Arve Hjønnevåg @ 2023-06-15 11:38 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: arve, John Stultz, Peter Zijlstra (Intel), Valentin Schneider,
	x86, linux-kernel

The following commit has been merged into the sched/core branch of tip:

Commit-ID:     4b85308dac750b16bd273b0f4e9c9b478fbb886b
Gitweb:        https://git.kernel.org/tip/4b85308dac750b16bd273b0f4e9c9b478fbb886b
Author:        Arve Hjønnevåg <arve@android.com>
AuthorDate:    Fri, 02 Jun 2023 21:23:46 
Committer:     Peter Zijlstra <peterz@infradead.org>
CommitterDate: Thu, 15 Jun 2023 13:28:21 +02:00

sched/wait: Fix a kthread_park race with wait_woken()

kthread_park and wait_woken have a similar race that
kthread_stop and wait_woken used to have before it was fixed in
commit cb6538e740d7 ("sched/wait: Fix a kthread race with
wait_woken()"). Extend that fix to also cover kthread_park.

[jstultz: Made changes suggested by Peter to optimize
 memory loads]

Signed-off-by: Arve Hjønnevåg <arve@android.com>
Signed-off-by: John Stultz <jstultz@google.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: Valentin Schneider <vschneid@redhat.com>
Link: https://lore.kernel.org/r/20230602212350.535358-1-jstultz@google.com
---
 include/linux/kthread.h |  1 +
 kernel/kthread.c        | 10 ++++++++++
 kernel/sched/wait.c     |  7 +------
 3 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/include/linux/kthread.h b/include/linux/kthread.h
index 30e5bec..f1f95a7 100644
--- a/include/linux/kthread.h
+++ b/include/linux/kthread.h
@@ -89,6 +89,7 @@ int kthread_stop(struct task_struct *k);
 bool kthread_should_stop(void);
 bool kthread_should_park(void);
 bool __kthread_should_park(struct task_struct *k);
+bool kthread_should_stop_or_park(void);
 bool kthread_freezable_should_stop(bool *was_frozen);
 void *kthread_func(struct task_struct *k);
 void *kthread_data(struct task_struct *k);
diff --git a/kernel/kthread.c b/kernel/kthread.c
index 490792b..07a0570 100644
--- a/kernel/kthread.c
+++ b/kernel/kthread.c
@@ -182,6 +182,16 @@ bool kthread_should_park(void)
 }
 EXPORT_SYMBOL_GPL(kthread_should_park);
 
+bool kthread_should_stop_or_park(void)
+{
+	struct kthread *kthread = __to_kthread(current);
+
+	if (!kthread)
+		return false;
+
+	return kthread->flags & (BIT(KTHREAD_SHOULD_STOP) | BIT(KTHREAD_SHOULD_PARK));
+}
+
 /**
  * kthread_freezable_should_stop - should this freezable kthread return now?
  * @was_frozen: optional out parameter, indicates whether %current was frozen
diff --git a/kernel/sched/wait.c b/kernel/sched/wait.c
index 133b747..48c53e4 100644
--- a/kernel/sched/wait.c
+++ b/kernel/sched/wait.c
@@ -425,11 +425,6 @@ 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)
-{
-	return (current->flags & PF_KTHREAD) && kthread_should_stop();
-}
-
 /*
  * DEFINE_WAIT_FUNC(wait, woken_wake_func);
  *
@@ -459,7 +454,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) && !kthread_should_stop_or_park())
 		timeout = schedule_timeout(timeout);
 	__set_current_state(TASK_RUNNING);
 

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [tip: sched/core] sched/wait: Fix a kthread_park race with wait_woken()
  2023-06-02 21:23 [RESEND][PATCH v2] sched/wait: Fix a kthread_park race with wait_woken() John Stultz
  2023-06-12 15:28 ` Valentin Schneider
  2023-06-15 11:38 ` [tip: sched/core] " tip-bot2 for Arve Hjønnevåg
@ 2023-06-16 15:16 ` tip-bot2 for Arve Hjønnevåg
  2 siblings, 0 replies; 4+ messages in thread
From: tip-bot2 for Arve Hjønnevåg @ 2023-06-16 15:16 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: arve, John Stultz, Peter Zijlstra (Intel), Valentin Schneider,
	x86, linux-kernel

The following commit has been merged into the sched/core branch of tip:

Commit-ID:     ef73d6a4ef0b35524125c3cfc6deafc26a0c966a
Gitweb:        https://git.kernel.org/tip/ef73d6a4ef0b35524125c3cfc6deafc26a0c966a
Author:        Arve Hjønnevåg <arve@android.com>
AuthorDate:    Fri, 02 Jun 2023 21:23:46 
Committer:     Peter Zijlstra <peterz@infradead.org>
CommitterDate: Fri, 16 Jun 2023 17:08:01 +02:00

sched/wait: Fix a kthread_park race with wait_woken()

kthread_park and wait_woken have a similar race that
kthread_stop and wait_woken used to have before it was fixed in
commit cb6538e740d7 ("sched/wait: Fix a kthread race with
wait_woken()"). Extend that fix to also cover kthread_park.

[jstultz: Made changes suggested by Peter to optimize
 memory loads]

Signed-off-by: Arve Hjønnevåg <arve@android.com>
Signed-off-by: John Stultz <jstultz@google.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: Valentin Schneider <vschneid@redhat.com>
Link: https://lore.kernel.org/r/20230602212350.535358-1-jstultz@google.com
---
 include/linux/kthread.h |  1 +
 kernel/kthread.c        | 10 ++++++++++
 kernel/sched/wait.c     |  7 +------
 3 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/include/linux/kthread.h b/include/linux/kthread.h
index 30e5bec..f1f95a7 100644
--- a/include/linux/kthread.h
+++ b/include/linux/kthread.h
@@ -89,6 +89,7 @@ int kthread_stop(struct task_struct *k);
 bool kthread_should_stop(void);
 bool kthread_should_park(void);
 bool __kthread_should_park(struct task_struct *k);
+bool kthread_should_stop_or_park(void);
 bool kthread_freezable_should_stop(bool *was_frozen);
 void *kthread_func(struct task_struct *k);
 void *kthread_data(struct task_struct *k);
diff --git a/kernel/kthread.c b/kernel/kthread.c
index 490792b..07a0570 100644
--- a/kernel/kthread.c
+++ b/kernel/kthread.c
@@ -182,6 +182,16 @@ bool kthread_should_park(void)
 }
 EXPORT_SYMBOL_GPL(kthread_should_park);
 
+bool kthread_should_stop_or_park(void)
+{
+	struct kthread *kthread = __to_kthread(current);
+
+	if (!kthread)
+		return false;
+
+	return kthread->flags & (BIT(KTHREAD_SHOULD_STOP) | BIT(KTHREAD_SHOULD_PARK));
+}
+
 /**
  * kthread_freezable_should_stop - should this freezable kthread return now?
  * @was_frozen: optional out parameter, indicates whether %current was frozen
diff --git a/kernel/sched/wait.c b/kernel/sched/wait.c
index 133b747..48c53e4 100644
--- a/kernel/sched/wait.c
+++ b/kernel/sched/wait.c
@@ -425,11 +425,6 @@ 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)
-{
-	return (current->flags & PF_KTHREAD) && kthread_should_stop();
-}
-
 /*
  * DEFINE_WAIT_FUNC(wait, woken_wake_func);
  *
@@ -459,7 +454,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) && !kthread_should_stop_or_park())
 		timeout = schedule_timeout(timeout);
 	__set_current_state(TASK_RUNNING);
 

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-06-16 15:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-06-02 21:23 [RESEND][PATCH v2] sched/wait: Fix a kthread_park race with wait_woken() John Stultz
2023-06-12 15:28 ` Valentin Schneider
2023-06-15 11:38 ` [tip: sched/core] " tip-bot2 for Arve Hjønnevåg
2023-06-16 15:16 ` tip-bot2 for Arve Hjønnevåg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox