Skip to content

Commit f445027

Browse files
Jeremy Fitzhardingetorvalds
authored andcommitted
stop_machine: make stop_machine safe and efficient to call early
Make stop_machine() safe to call early in boot, before SMP has been set up, by simply calling the callback function directly if there's only one CPU online. [ Fixes from AKPM: - add comment - local_irq_flags, not save_flags - also call hard_irq_disable() for systems which need it Tejun suggested using an explicit flag rather than just looking at the online cpu count. ] Cc: Tejun Heo <tj@kernel.org> Acked-by: Rusty Russell <rusty@rustcorp.com.au> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: H. Peter Anvin <hpa@linux.intel.com> Cc: Ingo Molnar <mingo@elte.hu> Cc: Steven Rostedt <rostedt@goodmis.org> Acked-by: Tejun Heo <htejun@gmail.com> Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com> Signed-off-by: Jeremy Fitzhardinge <jeremy.fitzhardinge@citrix.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent aaaa287 commit f445027

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

‎kernel/stop_machine.c‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ struct cpu_stopper {
4141
};
4242

4343
static DEFINE_PER_CPU(struct cpu_stopper, cpu_stopper);
44+
static bool stop_machine_initialized = false;
4445

4546
static void cpu_stop_init_done(struct cpu_stop_done *done, unsigned int nr_todo)
4647
{
@@ -386,6 +387,8 @@ static int __init cpu_stop_init(void)
386387
cpu_stop_cpu_callback(&cpu_stop_cpu_notifier, CPU_ONLINE, bcpu);
387388
register_cpu_notifier(&cpu_stop_cpu_notifier);
388389

390+
stop_machine_initialized = true;
391+
389392
return 0;
390393
}
391394
early_initcall(cpu_stop_init);
@@ -485,6 +488,25 @@ int __stop_machine(int (*fn)(void *), void *data, const struct cpumask *cpus)
485488
.num_threads = num_online_cpus(),
486489
.active_cpus = cpus };
487490

491+
if (!stop_machine_initialized) {
492+
/*
493+
* Handle the case where stop_machine() is called
494+
* early in boot before stop_machine() has been
495+
* initialized.
496+
*/
497+
unsigned long flags;
498+
int ret;
499+
500+
WARN_ON_ONCE(smdata.num_threads != 1);
501+
502+
local_irq_save(flags);
503+
hard_irq_disable();
504+
ret = (*fn)(data);
505+
local_irq_restore(flags);
506+
507+
return ret;
508+
}
509+
488510
/* Set the initial state and stop all online cpus. */
489511
set_state(&smdata, STOPMACHINE_PREPARE);
490512
return stop_cpus(cpu_online_mask, stop_machine_cpu_stop, &smdata);

0 commit comments

Comments
 (0)