Skip to content

Commit c546951

Browse files
Andrea ParriIngo Molnar
authored andcommitted
sched/core: Use READ_ONCE()/WRITE_ONCE() in move_queued_task()/task_rq_lock()
move_queued_task() synchronizes with task_rq_lock() as follows: 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 honor this address dependency. Also, mark the accesses to ->cpu and ->on_rq with READ_ONCE()/WRITE_ONCE() to comply with the LKMM. Signed-off-by: Andrea Parri <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Cc: Alan Stern <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Mike Galbraith <[email protected]> Cc: Paul E. McKenney <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Will Deacon <[email protected]> Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Ingo Molnar <[email protected]>
1 parent 1ca4fa3 commit c546951

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

include/linux/sched.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,9 +1745,9 @@ static __always_inline bool need_resched(void)
17451745
static inline unsigned int task_cpu(const struct task_struct *p)
17461746
{
17471747
#ifdef CONFIG_THREAD_INFO_IN_TASK
1748-
return p->cpu;
1748+
return READ_ONCE(p->cpu);
17491749
#else
1750-
return task_thread_info(p)->cpu;
1750+
return READ_ONCE(task_thread_info(p)->cpu);
17511751
#endif
17521752
}
17531753

kernel/sched/core.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,12 @@ struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf)
107107
* [L] ->on_rq
108108
* RELEASE (rq->lock)
109109
*
110-
* If we observe the old CPU in task_rq_lock, the acquire of
110+
* If we observe the old CPU in task_rq_lock(), the acquire of
111111
* the old rq->lock will fully serialize against the stores.
112112
*
113-
* If we observe the new CPU in task_rq_lock, the acquire will
114-
* pair with the WMB to ensure we must then also see migrating.
113+
* If we observe the new CPU in task_rq_lock(), the address
114+
* dependency headed by '[L] rq = task_rq()' and the acquire
115+
* will pair with the WMB to ensure we then also see migrating.
115116
*/
116117
if (likely(rq == task_rq(p) && !task_on_rq_migrating(p))) {
117118
rq_pin_lock(rq, rf);
@@ -916,7 +917,7 @@ static struct rq *move_queued_task(struct rq *rq, struct rq_flags *rf,
916917
{
917918
lockdep_assert_held(&rq->lock);
918919

919-
p->on_rq = TASK_ON_RQ_MIGRATING;
920+
WRITE_ONCE(p->on_rq, TASK_ON_RQ_MIGRATING);
920921
dequeue_task(rq, p, DEQUEUE_NOCLOCK);
921922
set_task_cpu(p, new_cpu);
922923
rq_unlock(rq, rf);

kernel/sched/sched.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,9 +1479,9 @@ static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
14791479
*/
14801480
smp_wmb();
14811481
#ifdef CONFIG_THREAD_INFO_IN_TASK
1482-
p->cpu = cpu;
1482+
WRITE_ONCE(p->cpu, cpu);
14831483
#else
1484-
task_thread_info(p)->cpu = cpu;
1484+
WRITE_ONCE(task_thread_info(p)->cpu, cpu);
14851485
#endif
14861486
p->wake_cpu = cpu;
14871487
#endif
@@ -1582,7 +1582,7 @@ static inline int task_on_rq_queued(struct task_struct *p)
15821582

15831583
static inline int task_on_rq_migrating(struct task_struct *p)
15841584
{
1585-
return p->on_rq == TASK_ON_RQ_MIGRATING;
1585+
return READ_ONCE(p->on_rq) == TASK_ON_RQ_MIGRATING;
15861586
}
15871587

15881588
/*

0 commit comments

Comments
 (0)