sched: add READ_ONCE to task_on_rq_queued
PATCH v2 1/1 sched: add READ_ONCE to task_on_rq_queued
From: Harshit Agarwal <[email protected]>
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.
--- kernel/sched/sched.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/sched.h +1 -1
@@ -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)
Signature · 1 line
2.43.0
Peter Zijlstra
maintainerQuoted 21 lines · Jon Kohler
From: Harshit Agarwal <[email protected]>
task_on_rq_queuedreadp->on_rqwithoutREAD_ONCE, thoughp->on_rqis set withWRITE_ONCEin {activate|deactivate}_task andsmp_store_releasein__block_task, and also read withREAD_ONCEintask_on_rq_migrating.Make all of these accesses pair together by adding
READ_ONCEin thetask_on_rq_queued.Signed-off-by Harshit Agarwal <[email protected]>Reviewed-by Phil Auld <[email protected]>Cc Jon Kohler <[email protected]>--- kernel/sched/sched.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/sched/sched.h +0
-0
kernel/sched/sched.h static inline int task_on_cpu(struct rq *rq, struct task_struct *p) · line 2281
Quoted 6 lines
kernel/sched/sched.h +1
-1@@ -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;The following commit has been merged into the sched/core branch of tip:
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 <[email protected]> AuthorDate: Thu, 14 Nov 2024 14:08:11 -07:00 Committer: Peter Zijlstra <[email protected]> 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.
--- kernel/sched/sched.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/sched.h +1 -1
@@ -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)