sched/deadline: Fix potential race in dl_add_task_root_domain()
A message that is not in the archive (sent off-list or not yet received): https://lore.kernel.org/lkml/[email protected]
PATCH 1/1 sched/deadline: Fix potential race in dl_add_task_root_domain()
The access rule for local_cpu_mask_dl requires it to be called on the local CPU with preemption disabled. However, dl_add_task_root_domain() currently violates this rule.
Without preemption disabled, the following race can occur:
1. ThreadA calls dl_add_task_root_domain() on CPU 0
2. Gets pointer to CPU 0's local_cpu_mask_dl
3. ThreadA is preempted and migrated to CPU 1
4. ThreadA continues using CPU 0's local_cpu_mask_dl
5. Meanwhile, the scheduler on CPU 0 calls find_later_rq() which also uses local_cpu_mask_dl (with preemption properly disabled)
6. Both contexts now corrupt the same per-CPU buffer concurrently
Fix this by moving the local_cpu_mask_dl access to the preemption disabled section.
To: Tejun Heo <[email protected]>
Cc: Waiman Long <[email protected]>
Cc: Chen Ridong <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Juri Lelli <[email protected]>
Cc: Pierre Gondois <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Vincent Guittot <[email protected]>
Cc: Dietmar Eggemann <[email protected]>
Cc: Steven Rostedt <[email protected]>
Cc: Ben Segall <[email protected]>
Cc: Mel Gorman <[email protected]>
Cc: Valentin Schneider <[email protected]>
To: [email protected]
To: [email protected]
--- kernel/sched/deadline.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/sched/deadline.c +2 -1
@@ -2944,7 +2944,7 @@ void dl_add_task_root_domain(struct task_struct *p)struct rq *rq;struct dl_bw *dl_b;unsigned int cpu; -struct cpumask *msk =this_cpu_cpumask_var_ptr (local_cpu_mask_dl); +struct cpumask *msk;raw_spin_lock_irqsave (&p->pi_lock, rf.flags);if (!dl_task (p) ||dl_entity_is_special (&p->dl)) {
@@ -2952,6 +2952,7 @@ void dl_add_task_root_domain(struct task_struct *p)return ; } + msk =this_cpu_cpumask_var_ptr (local_cpu_mask_dl);/* * Get an active rq, whose rq->rd traces the correct root * domain.
Signature · 1 line
2.49.0
Quoted 54 lines · Pingfan Liu
The access rule for
local_cpu_mask_dlrequires it to be called on the local CPU with preemption disabled. However,dl_add_task_root_domain()currently violates this rule.Without preemption disabled, the following race can occur:
1. ThreadA calls
dl_add_task_root_domain()on CPU 0
2. Gets pointer to CPU 0'slocal_cpu_mask_dl
3. ThreadA is preempted and migrated to CPU 1
4. ThreadA continues using CPU 0'slocal_cpu_mask_dl
5. Meanwhile, the scheduler on CPU 0 callsfind_later_rq()which also useslocal_cpu_mask_dl(with preemption properly disabled)
6. Both contexts now corrupt the same per-CPU buffer concurrentlyFix this by moving the
local_cpu_mask_dlaccess to the preemption disabled section.Fixes 318e18ed22e8 ("sched/deadline: Walk up cpuset hierarchy to decide root domain when hot-unplug")Reported-by Juri Lelli <[email protected]>Signed-off-by Pingfan Liu <[email protected]>To: Tejun Heo <[email protected]>
Cc: Waiman Long <[email protected]>
Cc: Chen Ridong <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Juri Lelli <[email protected]>
Cc: Pierre Gondois <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Vincent Guittot <[email protected]>
Cc: Dietmar Eggemann <[email protected]>
Cc: Steven Rostedt <[email protected]>
Cc: Ben Segall <[email protected]>
Cc: Mel Gorman <[email protected]>
Cc: Valentin Schneider <[email protected]>
To: [email protected]
To: [email protected]--- kernel/sched/deadline.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)kernel/sched/deadline.c +1
-1@@ -2944,7 +2944,7 @@ void dl_add_task_root_domain(struct task_struct *p)struct rq *rq;struct dl_bw *dl_b;unsigned int cpu; -struct cpumask *msk =this_cpu_cpumask_var_ptr (local_cpu_mask_dl); +struct cpumask *msk;raw_spin_lock_irqsave (&p->pi_lock, rf.flags);if (!dl_task (p) ||dl_entity_is_special (&p->dl)) {
kernel/sched/deadline.c void dl_add_task_root_domain(struct task_struct *p) · line 2958
Quoted 7 lines
kernel/sched/deadline.c +1
-0@@ -2952,6 +2952,7 @@ void dl_add_task_root_domain(struct task_struct *p)return ; } + msk =this_cpu_cpumask_var_ptr (local_cpu_mask_dl);/* * Get an active rq, whose rq->rd traces the correct root * domain.
It will be clearerer by moving the statement down to before the dl_get_task_effective_cpus() call that uses msk. Please also update the comment as suggested by Juri.
Thanks,
Longman