sched: Use READ_ONCE()/WRITE_ONCE() in task_cpu()/__set_task_cpu()
PATCH 1/1 sched: Use READ_ONCE()/WRITE_ONCE() in task_cpu()/__set_task_cpu()
The smp_wmb() in move_queued_task() (c.f., __set_task_cpu()) pairs with the composition of the dependency and the ACQUIRE in task_rq_lock():
move_queued_task() task_rq_lock() [S] ->on_rq = MIGRATING [L] rq = task_rq() WMB (__set_task_cpu()) ACQUIRE (rq->lock); [S] ->cpu = new_cpu [L] ->on_rq
where "[L] rq = task_rq()" is ordered before "ACQUIRE (rq->lock)" by an address dependency and, in turn, "ACQUIRE (rq->lock)" is ordered before "[L] ->on_rq" by the ACQUIRE itself.
Use READ_ONCE() to load ->cpu in task_rq() (c.f., task_cpu()) to honour this address dependency between loads; also, mark the store to ->cpu in __set_task_cpu() by using WRITE_ONCE() in order to tell the compiler to not mess/tear this (synchronizing) memory access.
--- include/linux/sched.h | 4 ++-- kernel/sched/sched.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)
include/linux/sched.h +2 -2
@@ -1753,9 +1753,9 @@ static __always_inline bool need_resched(void)static inline unsigned int task_cpu (const struct task_struct *p) {#ifdef CONFIG_THREAD_INFO_IN_TASK -return p->cpu; +return READ_ONCE (p->cpu);#else -return task_thread_info (p)->cpu; +return READ_ONCE (task_thread_info (p)->cpu);#endif }
kernel/sched/sched.h +2 -2
@@ -1460,9 +1460,9 @@ static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)*/ smp_wmb ();#ifdef CONFIG_THREAD_INFO_IN_TASK - p->cpu = cpu; +WRITE_ONCE (p->cpu, cpu);#else -task_thread_info (p)->cpu = cpu; +WRITE_ONCE (task_thread_info (p)->cpu, cpu);#endif p->wake_cpu = cpu;#endif
Signature · 1 line
2.17.1
Quoted 9 lines · Andrea Parri
The
smp_wmb()inmove_queued_task()(c.f.,__set_task_cpu()) pairs with the composition of the dependency and the ACQUIRE intask_rq_lock():move_queued_task() task_rq_lock() [S] ->on_rq = MIGRATING [L] rq = task_rq() WMB (__set_task_cpu()) ACQUIRE (rq->lock); [S] ->cpu = new_cpu [L] ->on_rq
Quoted 8 lines
where "[L] rq =
task_rq()" is ordered before "ACQUIRE (rq->lock)" by an address dependency and, in turn, "ACQUIRE (rq->lock)" is ordered before "[L] ->on_rq" by the ACQUIRE itself.Use
READ_ONCE()to load ->cpu intask_rq()(c.f.,task_cpu()) to honour this address dependency between loads; also, mark the store to ->cpu in__set_task_cpu()by usingWRITE_ONCE()in order to tell the compiler to not mess/tear this (synchronizing) memory access.
In the light of the recent discussion about the integration of plain accesses in the LKMM (c.f., e.g., [1] and discussion thereof), I was considering even further changes to this in order to "reinforce" the above smp_wmb(). Here's two approaches (one of):
1) replace this smp_wmb()+WRITE_ONCE() with an smp_store_release();
2) or keep this smp_wmb()+WRITE_ONCE(), but use {WRITE,READ}_ONCE()
also for the accesses to ->on_rq.
What do you think? (maybe I'm just being too paranoid?)
Adding Will to the Cc: ((1) should be "painless" for x86, not sure about arm64...)
Andrea
[1] http://lkml.kernel.org/r/20190118155638.GA24442@andrea
Quoted 46 lines
Signed-off-by Andrea Parri <[email protected]>Cc Ingo Molnar <[email protected]>Cc Peter Zijlstra <[email protected]>Cc "Paul E. McKenney" <[email protected]>Cc Alan Stern <[email protected]>--- include/linux/sched.h | 4 ++-- kernel/sched/sched.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)include/linux/sched.h +2
-2@@ -1753,9 +1753,9 @@ static __always_inline bool need_resched(void)static inline unsigned int task_cpu (const struct task_struct *p) {#ifdef CONFIG_THREAD_INFO_IN_TASK -return p->cpu; +return READ_ONCE (p->cpu);#else -return task_thread_info (p)->cpu; +return READ_ONCE (task_thread_info (p)->cpu);#endif }kernel/sched/sched.h +2
-2@@ -1460,9 +1460,9 @@ static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)*/ smp_wmb ();#ifdef CONFIG_THREAD_INFO_IN_TASK - p->cpu = cpu; +WRITE_ONCE (p->cpu, cpu);#else -task_thread_info (p)->cpu = cpu; +WRITE_ONCE (task_thread_info (p)->cpu, cpu);#endif p->wake_cpu = cpu;#endif Signature · 1 line
2.17.1
Peter Zijlstra
maintainerQuoted 24 lines · Andrea Parri
Quoted 9 lines · Andrea Parri
The
smp_wmb()inmove_queued_task()(c.f.,__set_task_cpu()) pairs with the composition of the dependency and the ACQUIRE intask_rq_lock():move_queued_task() task_rq_lock() [S] ->on_rq = MIGRATING [L] rq = task_rq() WMB (__set_task_cpu()) ACQUIRE (rq->lock); [S] ->cpu = new_cpu [L] ->on_rqQuoted 8 lines
where "[L] rq =
task_rq()" is ordered before "ACQUIRE (rq->lock)" by an address dependency and, in turn, "ACQUIRE (rq->lock)" is ordered before "[L] ->on_rq" by the ACQUIRE itself.Use
READ_ONCE()to load ->cpu intask_rq()(c.f.,task_cpu()) to honour this address dependency between loads; also, mark the store to ->cpu in__set_task_cpu()by usingWRITE_ONCE()in order to tell the compiler to not mess/tear this (synchronizing) memory access.In the light of the recent discussion about the integration of plain accesses in the LKMM (c.f., e.g., [1] and discussion thereof), I was considering even further changes to this in order to "reinforce" the above
smp_wmb(). Here's two approaches (one of):
Quoted 4 lines
1) replace this smp_wmb()+WRITE_ONCE() with an
smp_store_release();2) or keep this smp_wmb()+WRITE_ONCE(), but use {WRITE,READ}_ONCE()
also for the accesses to ->on_rq.
That should be the least painful I think. Note that we never store a value larger than a single byte in that word, so tearing shouldn't be a problem, but yes, that makes it all neat and tidy.
Quoted 4 lines
What do you think? (maybe I'm just being too paranoid?)
Adding Will to the Cc: ((1) should be "painless" for x86, not sure about arm64...)
ARM64 should be fine, it is 32bit ARM that will suffer, because it uses smp_mb() to implement acquire/release.
Quoted 25 lines · Peter Zijlstra
Quoted 24 lines · Andrea Parri
Quoted 9 lines · Andrea Parri
The
smp_wmb()inmove_queued_task()(c.f.,__set_task_cpu()) pairs with the composition of the dependency and the ACQUIRE intask_rq_lock():move_queued_task() task_rq_lock() [S] ->on_rq = MIGRATING [L] rq = task_rq() WMB (__set_task_cpu()) ACQUIRE (rq->lock); [S] ->cpu = new_cpu [L] ->on_rqQuoted 8 lines
where "[L] rq =
task_rq()" is ordered before "ACQUIRE (rq->lock)" by an address dependency and, in turn, "ACQUIRE (rq->lock)" is ordered before "[L] ->on_rq" by the ACQUIRE itself.Use
READ_ONCE()to load ->cpu intask_rq()(c.f.,task_cpu()) to honour this address dependency between loads; also, mark the store to ->cpu in__set_task_cpu()by usingWRITE_ONCE()in order to tell the compiler to not mess/tear this (synchronizing) memory access.In the light of the recent discussion about the integration of plain accesses in the LKMM (c.f., e.g., [1] and discussion thereof), I was considering even further changes to this in order to "reinforce" the above
smp_wmb(). Here's two approaches (one of):
Quoted 8 lines
Quoted 4 lines
1) replace this smp_wmb()+WRITE_ONCE() with an
smp_store_release();2) or keep this smp_wmb()+WRITE_ONCE(), but use {WRITE,READ}_ONCE()
also for the accesses to ->on_rq.That should be the least painful I think. Note that we never store a value larger than a single byte in that word, so tearing shouldn't be a problem, but yes, that makes it all neat and tidy.
Thank you for the suggestion; I'll send the revisited patch shortly.
Andrea