mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] sched: add READ_ONCE to task_on_rq_queued
@ 2024-11-14 21:08 Jon Kohler
  2024-11-15  9:50 ` Peter Zijlstra
  2024-12-02 11:14 ` [tip: sched/core] " tip-bot2 for Harshit Agarwal
  0 siblings, 2 replies; 3+ messages in thread
From: Jon Kohler @ 2024-11-14 21:08 UTC (permalink / raw)
  To: Ingo Molnar, Peter Zijlstra, Juri Lelli, Vincent Guittot,
	Dietmar Eggemann, Steven Rostedt, Ben Segall, Mel Gorman,
	Valentin Schneider, linux-kernel
  Cc: Harshit Agarwal, Phil Auld, Jon Kohler

From: Harshit Agarwal <harshit@nutanix.com>

task_on_rq_queued read p->on_rq without READ_ONCE, though p->on_rq is
set with WRITE_ONCE in {activate|deactivate}_task and smp_store_release
in __block_task, and also read with READ_ONCE in task_on_rq_migrating.

Make all of these accesses pair together by adding READ_ONCE in the
task_on_rq_queued.

Signed-off-by: Harshit Agarwal <harshit@nutanix.com>
Reviewed-by: Phil Auld <pauld@redhat.com>
Cc: Jon Kohler <jon@nutanix.com>
---
 kernel/sched/sched.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index c03b3d7b320e..dbbe5ce0dd96 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -2277,7 +2277,7 @@ static inline int task_on_cpu(struct rq *rq, struct task_struct *p)
 
 static inline int task_on_rq_queued(struct task_struct *p)
 {
-	return p->on_rq == TASK_ON_RQ_QUEUED;
+	return READ_ONCE(p->on_rq) == TASK_ON_RQ_QUEUED;
 }
 
 static inline int task_on_rq_migrating(struct task_struct *p)
-- 
2.43.0


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

* Re: [PATCH v2] sched: add READ_ONCE to task_on_rq_queued
  2024-11-14 21:08 [PATCH v2] sched: add READ_ONCE to task_on_rq_queued Jon Kohler
@ 2024-11-15  9:50 ` Peter Zijlstra
  2024-12-02 11:14 ` [tip: sched/core] " tip-bot2 for Harshit Agarwal
  1 sibling, 0 replies; 3+ messages in thread
From: Peter Zijlstra @ 2024-11-15  9:50 UTC (permalink / raw)
  To: Jon Kohler
  Cc: Ingo Molnar, Juri Lelli, Vincent Guittot, Dietmar Eggemann,
	Steven Rostedt, Ben Segall, Mel Gorman, Valentin Schneider,
	linux-kernel, Harshit Agarwal, Phil Auld

On Thu, Nov 14, 2024 at 02:08:11PM -0700, Jon Kohler wrote:
> From: Harshit Agarwal <harshit@nutanix.com>
> 
> task_on_rq_queued read p->on_rq without READ_ONCE, though p->on_rq is
> set with WRITE_ONCE in {activate|deactivate}_task and smp_store_release
> in __block_task, and also read with READ_ONCE in task_on_rq_migrating.
> 
> Make all of these accesses pair together by adding READ_ONCE in the
> task_on_rq_queued.
> 
> Signed-off-by: Harshit Agarwal <harshit@nutanix.com>
> Reviewed-by: Phil Auld <pauld@redhat.com>
> Cc: Jon Kohler <jon@nutanix.com>
> ---
>  kernel/sched/sched.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
> index c03b3d7b320e..dbbe5ce0dd96 100644
> --- a/kernel/sched/sched.h
> +++ b/kernel/sched/sched.h
> @@ -2277,7 +2277,7 @@ static inline int task_on_cpu(struct rq *rq, struct task_struct *p)
>  
>  static inline int task_on_rq_queued(struct task_struct *p)
>  {
> -	return p->on_rq == TASK_ON_RQ_QUEUED;
> +	return READ_ONCE(p->on_rq) == TASK_ON_RQ_QUEUED;
>  }

I think that strictly speaking we don't need it here, *IF* we see the
ON_RQ_QUEUED value, it must be stable.

But yeah, this is probably easier to reason about.

If you've got time, it might be worth to try something like:

	if (READ_ONCE(p->on_rq) == TASK_ON_RQ_QUEUED) {
		/*
		 * If we observe ON_RQ_QUEUED, it should be stable. IOW
		 * there should be no concurrent writes at this point.
		 */
		ASSERT_EXCLUSIVE_WRITER(p->on_rq);
		return true;
	}
	return false;

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

* [tip: sched/core] sched: add READ_ONCE to task_on_rq_queued
  2024-11-14 21:08 [PATCH v2] sched: add READ_ONCE to task_on_rq_queued Jon Kohler
  2024-11-15  9:50 ` Peter Zijlstra
@ 2024-12-02 11:14 ` tip-bot2 for Harshit Agarwal
  1 sibling, 0 replies; 3+ messages in thread
From: tip-bot2 for Harshit Agarwal @ 2024-12-02 11:14 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Harshit Agarwal, Peter Zijlstra (Intel), Phil Auld, x86, linux-kernel

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

Commit-ID:     59297e2093ceced86393a059a4bd36802311f7bb
Gitweb:        https://git.kernel.org/tip/59297e2093ceced86393a059a4bd36802311f7bb
Author:        Harshit Agarwal <harshit@nutanix.com>
AuthorDate:    Thu, 14 Nov 2024 14:08:11 -07:00
Committer:     Peter Zijlstra <peterz@infradead.org>
CommitterDate: Mon, 02 Dec 2024 12:01:30 +01:00

sched: add READ_ONCE to task_on_rq_queued

task_on_rq_queued read p->on_rq without READ_ONCE, though p->on_rq is
set with WRITE_ONCE in {activate|deactivate}_task and smp_store_release
in __block_task, and also read with READ_ONCE in task_on_rq_migrating.

Make all of these accesses pair together by adding READ_ONCE in the
task_on_rq_queued.

Signed-off-by: Harshit Agarwal <harshit@nutanix.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: Phil Auld <pauld@redhat.com>
Link: https://lkml.kernel.org/r/20241114210812.1836587-1-jon@nutanix.com
---
 kernel/sched/sched.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index 76f5f53..0f6790c 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -2271,7 +2271,7 @@ static inline int task_on_cpu(struct rq *rq, struct task_struct *p)
 
 static inline int task_on_rq_queued(struct task_struct *p)
 {
-	return p->on_rq == TASK_ON_RQ_QUEUED;
+	return READ_ONCE(p->on_rq) == TASK_ON_RQ_QUEUED;
 }
 
 static inline int task_on_rq_migrating(struct task_struct *p)

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

end of thread, other threads:[~2024-12-02 11:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-11-14 21:08 [PATCH v2] sched: add READ_ONCE to task_on_rq_queued Jon Kohler
2024-11-15  9:50 ` Peter Zijlstra
2024-12-02 11:14 ` [tip: sched/core] " tip-bot2 for Harshit Agarwal

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®