sched: hoist ASSERT_EXCLUSIVE_WRITER(p->on_rq) above WRITE_ONCE
PATCH 1/1 sched: hoist ASSERT_EXCLUSIVE_WRITER(p->on_rq) above WRITE_ONCE
In {activate|deactivate}_task(), hoist ASSERT_EXCLUSIVE_WRITER() to be above WRITE_ONCE(p->on_rq), which matches the ordering listed in the KCSAN documentation, kcsan-checks.h code comments, and the usage pattern we already have in __block_task().
--- kernel/sched/core.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/sched/core.c +2 -2
@@ -2066,16 +2066,16 @@ void activate_task(struct rq *rq, struct task_struct *p, int flags)enqueue_task (rq, p, flags); -WRITE_ONCE (p->on_rq,TASK_ON_RQ_QUEUED );ASSERT_EXCLUSIVE_WRITER (p->on_rq); +WRITE_ONCE (p->on_rq,TASK_ON_RQ_QUEUED ); }void deactivate_task (struct rq *rq,struct task_struct *p,int flags) {SCHED_WARN_ON (flags &DEQUEUE_SLEEP ); -WRITE_ONCE (p->on_rq,TASK_ON_RQ_MIGRATING );ASSERT_EXCLUSIVE_WRITER (p->on_rq); +WRITE_ONCE (p->on_rq,TASK_ON_RQ_MIGRATING );/* * Code explicitly relies on TASK_ON_RQ_MIGRATING begin set *before*
Signature · 1 line
2.43.0
Quoted 37 lines · On Thu, Nov 14, 2024 at 09:53:52AM -0700 Jon Kohler wrote:
In {activate|deactivate}_task(), hoist
ASSERT_EXCLUSIVE_WRITER()to be aboveWRITE_ONCE(p->on_rq), which matches the ordering listed in the KCSAN documentation,kcsan-checks.hcode comments, and the usage pattern we already have in__block_task().Signed-off-by Jon Kohler <[email protected]>--- kernel/sched/core.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)kernel/sched/core.c +2
-2@@ -2066,16 +2066,16 @@ void activate_task(struct rq *rq, struct task_struct *p, int flags)enqueue_task (rq, p, flags); -WRITE_ONCE (p->on_rq,TASK_ON_RQ_QUEUED );ASSERT_EXCLUSIVE_WRITER (p->on_rq); +WRITE_ONCE (p->on_rq,TASK_ON_RQ_QUEUED ); }void deactivate_task (struct rq *rq,struct task_struct *p,int flags) {SCHED_WARN_ON (flags &DEQUEUE_SLEEP ); -WRITE_ONCE (p->on_rq,TASK_ON_RQ_MIGRATING );ASSERT_EXCLUSIVE_WRITER (p->on_rq); +WRITE_ONCE (p->on_rq,TASK_ON_RQ_MIGRATING );/* * Code explicitly relies on TASK_ON_RQ_MIGRATING begin set *before* Signature · 1 line
2.43.0
This looks fine to me and it makes sense to have the assert before the write. A quick grep showed that this is by no means a universal pattern at the moment.
Cheers,
Phil
Quoted 41 lines · Phil Auld
!-------------------------------------------------------------------|
CAUTION: External Email|-------------------------------------------------------------------!
Quoted 31 lines · On Thu, Nov 14, 2024 at 09:53:52AM -0700 Jon Kohler wrote:
In {activate|deactivate}_task(), hoist
ASSERT_EXCLUSIVE_WRITER()to be aboveWRITE_ONCE(p->on_rq), which matches the ordering listed in the KCSAN documentation,kcsan-checks.hcode comments, and the usage pattern we already have in__block_task().Signed-off-by Jon Kohler <[email protected]>---
kernel/sched/core.c| 4 ++--1 file changed, 2 insertions(+), 2 deletions(-)kernel/sched/core.c +2
-2@@ -2066,16 +2066,16 @@ void activate_task(struct rq *rq, struct task_struct *p, int flags)enqueue_task (rq, p, flags); -WRITE_ONCE (p->on_rq,TASK_ON_RQ_QUEUED );ASSERT_EXCLUSIVE_WRITER (p->on_rq); +WRITE_ONCE (p->on_rq,TASK_ON_RQ_QUEUED ); }void deactivate_task (struct rq *rq,struct task_struct *p,int flags) {SCHED_WARN_ON (flags &DEQUEUE_SLEEP ); -WRITE_ONCE (p->on_rq,TASK_ON_RQ_MIGRATING );ASSERT_EXCLUSIVE_WRITER (p->on_rq); +WRITE_ONCE (p->on_rq,TASK_ON_RQ_MIGRATING );
Quoted 11 lines
Quoted 6 lines
kernel/sched/core.c +0
-0/* * Code explicitly relies on TASK_ON_RQ_MIGRATING begin set *before* Signature · 1 line
2.43.0This looks fine to me and it makes sense to have the assert before the write. A quick grep showed that this is by no means a universal pattern at the moment.
I’d have to imaging having the assert before must be the right way to do this, just from a logic control flow perspective. I’m happy to fix ’the others', or do you think I should let them sit there?
Quoted 9 lines
Reviewed-by Phil Auld <[email protected]>Cheers,
Phil
Quoted 53 lines · On Thu, Nov 14, 2024 at 07:01:13PM +0000 Jon Kohler wrote:
Quoted 41 lines · Phil Auld
!-------------------------------------------------------------------|
CAUTION: External Email|-------------------------------------------------------------------!
Quoted 31 lines · On Thu, Nov 14, 2024 at 09:53:52AM -0700 Jon Kohler wrote:
In {activate|deactivate}_task(), hoist
ASSERT_EXCLUSIVE_WRITER()to be aboveWRITE_ONCE(p->on_rq), which matches the ordering listed in the KCSAN documentation,kcsan-checks.hcode comments, and the usage pattern we already have in__block_task().Signed-off-by Jon Kohler <[email protected]>---
kernel/sched/core.c| 4 ++--1 file changed, 2 insertions(+), 2 deletions(-)kernel/sched/core.c +2
-2@@ -2066,16 +2066,16 @@ void activate_task(struct rq *rq, struct task_struct *p, int flags)enqueue_task (rq, p, flags); -WRITE_ONCE (p->on_rq,TASK_ON_RQ_QUEUED );ASSERT_EXCLUSIVE_WRITER (p->on_rq); +WRITE_ONCE (p->on_rq,TASK_ON_RQ_QUEUED ); }void deactivate_task (struct rq *rq,struct task_struct *p,int flags) {SCHED_WARN_ON (flags &DEQUEUE_SLEEP ); -WRITE_ONCE (p->on_rq,TASK_ON_RQ_MIGRATING );ASSERT_EXCLUSIVE_WRITER (p->on_rq); +WRITE_ONCE (p->on_rq,TASK_ON_RQ_MIGRATING );Quoted 11 lines
Quoted 6 lines
kernel/sched/core.c +0
-0/* * Code explicitly relies on TASK_ON_RQ_MIGRATING begin set *before* Signature · 1 line
2.43.0This looks fine to me and it makes sense to have the assert before the write. A quick grep showed that this is by no means a universal pattern at the moment.
Quoted 4 lines
I’d have to imaging having the assert before must be the right way to do this, just from a logic control flow perspective. I’m happy to fix ’the others', or do you think I should let them sit there?
I don't know. I don't think it matters much since the assert is really independent of the actual write. Like I said it makes sense to have it first to me but others may see it as just moving code around for no strong reason. Peter may or may not decide to pick this one up. Other "mis-ordered" uses are in code maintained by different folks.
You can see if anyone else weighs in...
Cheers,
Phil
Quoted 10 lines
Quoted 9 lines
Reviewed-by Phil Auld <[email protected]>Cheers,
Phil
Peter Zijlstra
maintainerQuoted 7 lines · Phil Auld
I don't know. I don't think it matters much since the assert is really independent of the actual write. Like I said it makes sense to have it first to me but others may see it as just moving code around for no strong reason. Peter may or may not decide to pick this one up. Other "mis-ordered" uses are in code maintained by different folks.
You can see if anyone else weighs in...
So I'm not entirely about this patch... :-)
Per commit b55945c500c5 ("sched: Fix pick_next_task_fair() vs try_to_wake_up() race") we can see that this placement is not equivalent.
Placing it before the store means that nobody else will store to it until you've done your store.
Placing it after means that nobody else will attempt the store, irrespective of the store you just did.
That is, the ASSERT after the store is a stronger assert.
In case of activate_task(), we mark the task as on_rq, and this state is protected by rq->lock (which we hold), so there must not be any stores for as long as we hold that lock. So after is the right place.
In case of deactivate_task(), this is a hand-off, but the handoff doesn't happen until after dequeue_task() and set_task_cpu(). So at this point, again, nobody should be modifying it.
Quoted 8 lines · On Fri, Nov 15, 2024 at 10:58:47AM +0100 Peter Zijlstra wrote:
Quoted 6 lines · Phil Auld
I don't know. I don't think it matters much since the assert is really independent of the actual write. Like I said it makes sense to have it first to me but others may see it as just moving code around for no strong reason. Peter may or may not decide to pick this one up. Other "mis-ordered" uses are in code maintained by different folks.
Quoted 4 lines
Quoted 1 line
You can see if anyone else weighs in...
So I'm not entirely about this patch... :-)
I'm not entirely either :)
Quoted 3 lines
Per commit
b55945c500c5("sched: Fixpick_next_task_fair()vstry_to_wake_up()race") we can see that this placement is not equivalent.
Subtle. That kind of thing (someone else acting on the write) was my hesitation but I couldn't quite wrap my head around it.
That's why we have other people weigh in ...
Thanks!
Quoted 5 lines
Placing it before the store means that nobody else will store to it until you've done your store.
Placing it after means that nobody else will attempt the store, irrespective of the store you just did.
Presumably until some barrier or release at least, right?
I've got to dig into the kcsan stuff a bit more. It looks really interesting.
Cheers,
Phil
Quoted 12 lines
That is, the ASSERT after the store is a stronger assert.
In case of
activate_task(), we mark the task ason_rq, and this state is protected byrq->lock(which we hold), so there must not be any stores for as long as we hold that lock. So after is the right place.In case of
deactivate_task(), this is a hand-off, but the handoff doesn't happen until afterdequeue_task()andset_task_cpu(). So at this point, again, nobody should be modifying it.
Peter Zijlstra
maintainerQuoted 9 lines · Phil Auld
Quoted 8 lines · On Fri, Nov 15, 2024 at 10:58:47AM +0100 Peter Zijlstra wrote:
Quoted 6 lines · Phil Auld
I don't know. I don't think it matters much since the assert is really independent of the actual write. Like I said it makes sense to have it first to me but others may see it as just moving code around for no strong reason. Peter may or may not decide to pick this one up. Other "mis-ordered" uses are in code maintained by different folks.
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
You can see if anyone else weighs in...
So I'm not entirely about this patch... :-)
I'm not entirely either :)
Hehe, typing is forever hard :-)