Skip to content

Commit b80a2bf

Browse files
Peter ZijlstraKAGA-KOKO
authored andcommitted
stop_machine: Reflow cpu_stop_queue_two_works()
The code flow in cpu_stop_queue_two_works() is a little arcane; fix this by lifting the preempt_disable() to the top to create more natural nesting wrt the spinlocks and make the wake_up_q() and preempt_enable() unconditional at the end. Furthermore, enable preemption in the -EDEADLK case, such that we spin-wait with preemption enabled. Suggested-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de> Cc: isaacm@codeaurora.org Cc: matt@codeblueprint.co.uk Cc: psodagud@codeaurora.org Cc: gregkh@linuxfoundation.org Cc: pkondeti@codeaurora.org Cc: stable@vger.kernel.org Link: https://lkml.kernel.org/r/20180730112140.GH2494@hirez.programming.kicks-ass.net
1 parent b6a60cf commit b80a2bf

1 file changed

Lines changed: 23 additions & 18 deletions

File tree

‎kernel/stop_machine.c‎

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,24 @@ static int cpu_stop_queue_two_works(int cpu1, struct cpu_stop_work *work1,
236236
struct cpu_stopper *stopper2 = per_cpu_ptr(&cpu_stopper, cpu2);
237237
DEFINE_WAKE_Q(wakeq);
238238
int err;
239+
239240
retry:
241+
/*
242+
* The waking up of stopper threads has to happen in the same
243+
* scheduling context as the queueing. Otherwise, there is a
244+
* possibility of one of the above stoppers being woken up by another
245+
* CPU, and preempting us. This will cause us to not wake up the other
246+
* stopper forever.
247+
*/
248+
preempt_disable();
240249
raw_spin_lock_irq(&stopper1->lock);
241250
raw_spin_lock_nested(&stopper2->lock, SINGLE_DEPTH_NESTING);
242251

243-
err = -ENOENT;
244-
if (!stopper1->enabled || !stopper2->enabled)
252+
if (!stopper1->enabled || !stopper2->enabled) {
253+
err = -ENOENT;
245254
goto unlock;
255+
}
256+
246257
/*
247258
* Ensure that if we race with __stop_cpus() the stoppers won't get
248259
* queued up in reverse order leading to system deadlock.
@@ -253,36 +264,30 @@ static int cpu_stop_queue_two_works(int cpu1, struct cpu_stop_work *work1,
253264
* It can be falsely true but it is safe to spin until it is cleared,
254265
* queue_stop_cpus_work() does everything under preempt_disable().
255266
*/
256-
err = -EDEADLK;
257-
if (unlikely(stop_cpus_in_progress))
258-
goto unlock;
267+
if (unlikely(stop_cpus_in_progress)) {
268+
err = -EDEADLK;
269+
goto unlock;
270+
}
259271

260272
err = 0;
261273
__cpu_stop_queue_work(stopper1, work1, &wakeq);
262274
__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();
275+
272276
unlock:
273277
raw_spin_unlock(&stopper2->lock);
274278
raw_spin_unlock_irq(&stopper1->lock);
275279

276280
if (unlikely(err == -EDEADLK)) {
281+
preempt_enable();
282+
277283
while (stop_cpus_in_progress)
278284
cpu_relax();
285+
279286
goto retry;
280287
}
281288

282-
if (!err) {
283-
wake_up_q(&wakeq);
284-
preempt_enable();
285-
}
289+
wake_up_q(&wakeq);
290+
preempt_enable();
286291

287292
return err;
288293
}

0 commit comments

Comments
 (0)