Skip to content

Commit 2610e88

Browse files
Isaac J. ManjarresIngo Molnar
authored andcommitted
stop_machine: Disable preemption after queueing stopper threads
This commit: 9fb8d5d ("stop_machine, Disable preemption when waking two stopper threads") does not fully address the race condition that can occur as follows: On one CPU, call it CPU 3, thread 1 invokes cpu_stop_queue_two_works(2, 3,...), and the execution is such that thread 1 queues the works for migration/2 and migration/3, and is preempted after releasing the locks for migration/2 and migration/3, but before waking the threads. Then, On CPU 2, a kworker, call it thread 2, is running, and it invokes cpu_stop_queue_two_works(1, 2,...), such that thread 2 queues the works for migration/1 and migration/2. Meanwhile, on CPU 3, thread 1 resumes execution, and wakes migration/2 and migration/3. This means that when CPU 2 releases the locks for migration/1 and migration/2, but before it wakes those threads, it can be preempted by migration/2. If thread 2 is preempted by migration/2, then migration/2 will execute the first work item successfully, since migration/3 was woken up by CPU 3, but when it goes to execute the second work item, it disables preemption, calls multi_cpu_stop(), and thus, CPU 2 will wait forever for migration/1, which should have been woken up by thread 2. However migration/1 cannot be woken up by thread 2, since it is a kworker, so it is affine to CPU 2, but CPU 2 is running migration/2 with preemption disabled, so thread 2 will never run. Disable preemption after queueing works for stopper threads to ensure that the operation of queueing the works and waking the stopper threads is atomic. Co-Developed-by: Prasad Sodagudi <[email protected]> Co-Developed-by: Pavankumar Kondeti <[email protected]> Signed-off-by: Isaac J. Manjarres <[email protected]> Signed-off-by: Prasad Sodagudi <[email protected]> Signed-off-by: Pavankumar Kondeti <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: [email protected] Cc: [email protected] Cc: [email protected] Fixes: 9fb8d5d ("stop_machine, Disable preemption when waking two stopper threads") Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Ingo Molnar <[email protected]>
1 parent 6cd0c58 commit 2610e88

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

kernel/stop_machine.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,15 @@ static int cpu_stop_queue_two_works(int cpu1, struct cpu_stop_work *work1,
260260
err = 0;
261261
__cpu_stop_queue_work(stopper1, work1, &wakeq);
262262
__cpu_stop_queue_work(stopper2, work2, &wakeq);
263+
/*
264+
* The waking up of stopper threads has to happen
265+
* in the same scheduling context as the queueing.
266+
* Otherwise, there is a possibility of one of the
267+
* above stoppers being woken up by another CPU,
268+
* and preempting us. This will cause us to n ot
269+
* wake up the other stopper forever.
270+
*/
271+
preempt_disable();
263272
unlock:
264273
raw_spin_unlock(&stopper2->lock);
265274
raw_spin_unlock_irq(&stopper1->lock);
@@ -271,7 +280,6 @@ static int cpu_stop_queue_two_works(int cpu1, struct cpu_stop_work *work1,
271280
}
272281

273282
if (!err) {
274-
preempt_disable();
275283
wake_up_q(&wakeq);
276284
preempt_enable();
277285
}

0 commit comments

Comments
 (0)