Skip to content

Commit f0498d2

Browse files
author
Peter Zijlstra
committed
sched: Fix stop_one_cpu_nowait() vs hotplug
Kuyo reported sporadic failures on a sched_setaffinity() vs CPU hotplug stress-test -- notably affine_move_task() remains stuck in wait_for_completion(), leading to a hung-task detector warning. Specifically, it was reported that stop_one_cpu_nowait(.fn = migration_cpu_stop) returns false -- this stopper is responsible for the matching complete(). The race scenario is: CPU0 CPU1 // doing _cpu_down() __set_cpus_allowed_ptr() task_rq_lock(); takedown_cpu() stop_machine_cpuslocked(take_cpu_down..) <PREEMPT: cpu_stopper_thread() MULTI_STOP_PREPARE ... __set_cpus_allowed_ptr_locked() affine_move_task() task_rq_unlock(); <PREEMPT: cpu_stopper_thread()\> ack_state() MULTI_STOP_RUN take_cpu_down() __cpu_disable(); stop_machine_park(); stopper->enabled = false; /> /> stop_one_cpu_nowait(.fn = migration_cpu_stop); if (stopper->enabled) // false!!! That is, by doing stop_one_cpu_nowait() after dropping rq-lock, the stopper thread gets a chance to preempt and allows the cpu-down for the target CPU to complete. OTOH, since stop_one_cpu_nowait() / cpu_stop_queue_work() needs to issue a wakeup, it must not be ran under the scheduler locks. Solve this apparent contradiction by keeping preemption disabled over the unlock + queue_stopper combination: preempt_disable(); task_rq_unlock(...); if (!stop_pending) stop_one_cpu_nowait(...) preempt_enable(); This respects the lock ordering contraints while still avoiding the above race. That is, if we find the CPU is online under rq-lock, the targeted stop_one_cpu_nowait() must succeed. Apply this pattern to all similar stop_one_cpu_nowait() invocations. Fixes: 6d337ea ("sched: Fix migrate_disable() vs set_cpus_allowed_ptr()") Reported-by: "Kuyo Chang (張建文)" <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Tested-by: "Kuyo Chang (張建文)" <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 0c29240 commit f0498d2

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

kernel/sched/core.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2645,9 +2645,11 @@ static int migration_cpu_stop(void *data)
26452645
* it.
26462646
*/
26472647
WARN_ON_ONCE(!pending->stop_pending);
2648+
preempt_disable();
26482649
task_rq_unlock(rq, p, &rf);
26492650
stop_one_cpu_nowait(task_cpu(p), migration_cpu_stop,
26502651
&pending->arg, &pending->stop_work);
2652+
preempt_enable();
26512653
return 0;
26522654
}
26532655
out:
@@ -2967,12 +2969,13 @@ static int affine_move_task(struct rq *rq, struct task_struct *p, struct rq_flag
29672969
complete = true;
29682970
}
29692971

2972+
preempt_disable();
29702973
task_rq_unlock(rq, p, rf);
2971-
29722974
if (push_task) {
29732975
stop_one_cpu_nowait(rq->cpu, push_cpu_stop,
29742976
p, &rq->push_work);
29752977
}
2978+
preempt_enable();
29762979

29772980
if (complete)
29782981
complete_all(&pending->done);
@@ -3038,12 +3041,13 @@ static int affine_move_task(struct rq *rq, struct task_struct *p, struct rq_flag
30383041
if (flags & SCA_MIGRATE_ENABLE)
30393042
p->migration_flags &= ~MDF_PUSH;
30403043

3044+
preempt_disable();
30413045
task_rq_unlock(rq, p, rf);
3042-
30433046
if (!stop_pending) {
30443047
stop_one_cpu_nowait(cpu_of(rq), migration_cpu_stop,
30453048
&pending->arg, &pending->stop_work);
30463049
}
3050+
preempt_enable();
30473051

30483052
if (flags & SCA_MIGRATE_ENABLE)
30493053
return 0;
@@ -9421,9 +9425,11 @@ static void balance_push(struct rq *rq)
94219425
* Temporarily drop rq->lock such that we can wake-up the stop task.
94229426
* Both preemption and IRQs are still disabled.
94239427
*/
9428+
preempt_disable();
94249429
raw_spin_rq_unlock(rq);
94259430
stop_one_cpu_nowait(rq->cpu, __balance_push_cpu_stop, push_task,
94269431
this_cpu_ptr(&push_work));
9432+
preempt_enable();
94279433
/*
94289434
* At this point need_resched() is true and we'll take the loop in
94299435
* schedule(). The next pick is obviously going to be the stop task

kernel/sched/deadline.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2420,9 +2420,11 @@ static void pull_dl_task(struct rq *this_rq)
24202420
double_unlock_balance(this_rq, src_rq);
24212421

24222422
if (push_task) {
2423+
preempt_disable();
24232424
raw_spin_rq_unlock(this_rq);
24242425
stop_one_cpu_nowait(src_rq->cpu, push_cpu_stop,
24252426
push_task, &src_rq->push_work);
2427+
preempt_enable();
24262428
raw_spin_rq_lock(this_rq);
24272429
}
24282430
}

kernel/sched/fair.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11254,13 +11254,15 @@ static int load_balance(int this_cpu, struct rq *this_rq,
1125411254
busiest->push_cpu = this_cpu;
1125511255
active_balance = 1;
1125611256
}
11257-
raw_spin_rq_unlock_irqrestore(busiest, flags);
1125811257

11258+
preempt_disable();
11259+
raw_spin_rq_unlock_irqrestore(busiest, flags);
1125911260
if (active_balance) {
1126011261
stop_one_cpu_nowait(cpu_of(busiest),
1126111262
active_load_balance_cpu_stop, busiest,
1126211263
&busiest->active_balance_work);
1126311264
}
11265+
preempt_enable();
1126411266
}
1126511267
} else {
1126611268
sd->nr_balance_failed = 0;

kernel/sched/rt.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2063,9 +2063,11 @@ static int push_rt_task(struct rq *rq, bool pull)
20632063
*/
20642064
push_task = get_push_task(rq);
20652065
if (push_task) {
2066+
preempt_disable();
20662067
raw_spin_rq_unlock(rq);
20672068
stop_one_cpu_nowait(rq->cpu, push_cpu_stop,
20682069
push_task, &rq->push_work);
2070+
preempt_enable();
20692071
raw_spin_rq_lock(rq);
20702072
}
20712073

@@ -2402,9 +2404,11 @@ static void pull_rt_task(struct rq *this_rq)
24022404
double_unlock_balance(this_rq, src_rq);
24032405

24042406
if (push_task) {
2407+
preempt_disable();
24052408
raw_spin_rq_unlock(this_rq);
24062409
stop_one_cpu_nowait(src_rq->cpu, push_cpu_stop,
24072410
push_task, &src_rq->push_work);
2411+
preempt_enable();
24082412
raw_spin_rq_lock(this_rq);
24092413
}
24102414
}

0 commit comments

Comments
 (0)