xref: /linux-6.15/kernel/stop_machine.c (revision 2af8780d)
16ff3f917SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21142d810STejun Heo /*
31142d810STejun Heo  * kernel/stop_machine.c
41142d810STejun Heo  *
51142d810STejun Heo  * Copyright (C) 2008, 2005	IBM Corporation.
61142d810STejun Heo  * Copyright (C) 2008, 2005	Rusty Russell [email protected]
71142d810STejun Heo  * Copyright (C) 2010		SUSE Linux Products GmbH
81142d810STejun Heo  * Copyright (C) 2010		Tejun Heo <[email protected]>
9e5582ca2SRusty Russell  */
10b1fc5833SMark Rutland #include <linux/compiler.h>
111142d810STejun Heo #include <linux/completion.h>
121da177e4SLinus Torvalds #include <linux/cpu.h>
131142d810STejun Heo #include <linux/init.h>
14ee527cd3SPrarit Bhargava #include <linux/kthread.h>
159984de1aSPaul Gortmaker #include <linux/export.h>
161142d810STejun Heo #include <linux/percpu.h>
17ee527cd3SPrarit Bhargava #include <linux/sched.h>
18ee527cd3SPrarit Bhargava #include <linux/stop_machine.h>
19a12bb444SBenjamin Herrenschmidt #include <linux/interrupt.h>
201142d810STejun Heo #include <linux/kallsyms.h>
2114e568e7SThomas Gleixner #include <linux/smpboot.h>
2260063497SArun Sharma #include <linux/atomic.h>
23ce4f06dcSOleg Nesterov #include <linux/nmi.h>
240b26351bSPeter Zijlstra #include <linux/sched/wake_q.h>
251142d810STejun Heo 
261142d810STejun Heo /*
271142d810STejun Heo  * Structure to determine completion condition and record errors.  May
281142d810STejun Heo  * be shared by works on different cpus.
291142d810STejun Heo  */
301142d810STejun Heo struct cpu_stop_done {
311142d810STejun Heo 	atomic_t		nr_todo;	/* nr left to execute */
321142d810STejun Heo 	int			ret;		/* collected return value */
331142d810STejun Heo 	struct completion	completion;	/* fired if nr_todo reaches 0 */
341142d810STejun Heo };
351142d810STejun Heo 
361142d810STejun Heo /* the actual stopper, one per every possible cpu, enabled on online cpus */
371142d810STejun Heo struct cpu_stopper {
3802cb7aa9SOleg Nesterov 	struct task_struct	*thread;
3902cb7aa9SOleg Nesterov 
40de5b55c1SThomas Gleixner 	raw_spinlock_t		lock;
41878ae127SRichard Kennedy 	bool			enabled;	/* is this stopper enabled? */
421142d810STejun Heo 	struct list_head	works;		/* list of pending works */
4302cb7aa9SOleg Nesterov 
4402cb7aa9SOleg Nesterov 	struct cpu_stop_work	stop_work;	/* for stop_cpus */
45a8b62fd0SPeter Zijlstra 	unsigned long		caller;
46a8b62fd0SPeter Zijlstra 	cpu_stop_fn_t		fn;
471142d810STejun Heo };
481142d810STejun Heo 
491142d810STejun Heo static DEFINE_PER_CPU(struct cpu_stopper, cpu_stopper);
50f445027eSJeremy Fitzhardinge static bool stop_machine_initialized = false;
511142d810STejun Heo 
print_stop_info(const char * log_lvl,struct task_struct * task)52a8b62fd0SPeter Zijlstra void print_stop_info(const char *log_lvl, struct task_struct *task)
53a8b62fd0SPeter Zijlstra {
54a8b62fd0SPeter Zijlstra 	/*
55a8b62fd0SPeter Zijlstra 	 * If @task is a stopper task, it cannot migrate and task_cpu() is
56a8b62fd0SPeter Zijlstra 	 * stable.
57a8b62fd0SPeter Zijlstra 	 */
58a8b62fd0SPeter Zijlstra 	struct cpu_stopper *stopper = per_cpu_ptr(&cpu_stopper, task_cpu(task));
59a8b62fd0SPeter Zijlstra 
60a8b62fd0SPeter Zijlstra 	if (task != stopper->thread)
61a8b62fd0SPeter Zijlstra 		return;
62a8b62fd0SPeter Zijlstra 
63a8b62fd0SPeter Zijlstra 	printk("%sStopper: %pS <- %pS\n", log_lvl, stopper->fn, (void *)stopper->caller);
64a8b62fd0SPeter Zijlstra }
65a8b62fd0SPeter Zijlstra 
66e6253970SOleg Nesterov /* static data for stop_cpus */
67e6253970SOleg Nesterov static DEFINE_MUTEX(stop_cpus_mutex);
68e6253970SOleg Nesterov static bool stop_cpus_in_progress;
697053ea1aSRik van Riel 
cpu_stop_init_done(struct cpu_stop_done * done,unsigned int nr_todo)701142d810STejun Heo static void cpu_stop_init_done(struct cpu_stop_done *done, unsigned int nr_todo)
711142d810STejun Heo {
721142d810STejun Heo 	memset(done, 0, sizeof(*done));
731142d810STejun Heo 	atomic_set(&done->nr_todo, nr_todo);
741142d810STejun Heo 	init_completion(&done->completion);
751142d810STejun Heo }
761142d810STejun Heo 
771142d810STejun Heo /* signal completion unless @done is NULL */
cpu_stop_signal_done(struct cpu_stop_done * done)786fa3b826SOleg Nesterov static void cpu_stop_signal_done(struct cpu_stop_done *done)
791142d810STejun Heo {
801142d810STejun Heo 	if (atomic_dec_and_test(&done->nr_todo))
811142d810STejun Heo 		complete(&done->completion);
821142d810STejun Heo }
831142d810STejun Heo 
__cpu_stop_queue_work(struct cpu_stopper * stopper,struct cpu_stop_work * work,struct wake_q_head * wakeq)845caa1c08SOleg Nesterov static void __cpu_stop_queue_work(struct cpu_stopper *stopper,
850b26351bSPeter Zijlstra 					struct cpu_stop_work *work,
860b26351bSPeter Zijlstra 					struct wake_q_head *wakeq)
875caa1c08SOleg Nesterov {
885caa1c08SOleg Nesterov 	list_add_tail(&work->list, &stopper->works);
890b26351bSPeter Zijlstra 	wake_q_add(wakeq, stopper->thread);
905caa1c08SOleg Nesterov }
915caa1c08SOleg Nesterov 
921142d810STejun Heo /* queue @work to @stopper.  if offline, @work is completed immediately */
cpu_stop_queue_work(unsigned int cpu,struct cpu_stop_work * work)931b034bd9SOleg Nesterov static bool cpu_stop_queue_work(unsigned int cpu, struct cpu_stop_work *work)
941142d810STejun Heo {
95860a0ffaSThomas Gleixner 	struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
960b26351bSPeter Zijlstra 	DEFINE_WAKE_Q(wakeq);
971142d810STejun Heo 	unsigned long flags;
981b034bd9SOleg Nesterov 	bool enabled;
991142d810STejun Heo 
100cfd35514SPrasad Sodagudi 	preempt_disable();
101de5b55c1SThomas Gleixner 	raw_spin_lock_irqsave(&stopper->lock, flags);
1021b034bd9SOleg Nesterov 	enabled = stopper->enabled;
1031b034bd9SOleg Nesterov 	if (enabled)
1040b26351bSPeter Zijlstra 		__cpu_stop_queue_work(stopper, work, &wakeq);
105dd2e3121SOleg Nesterov 	else if (work->done)
1066fa3b826SOleg Nesterov 		cpu_stop_signal_done(work->done);
107de5b55c1SThomas Gleixner 	raw_spin_unlock_irqrestore(&stopper->lock, flags);
1081b034bd9SOleg Nesterov 
1090b26351bSPeter Zijlstra 	wake_up_q(&wakeq);
110cfd35514SPrasad Sodagudi 	preempt_enable();
1110b26351bSPeter Zijlstra 
1121b034bd9SOleg Nesterov 	return enabled;
1131142d810STejun Heo }
1141142d810STejun Heo 
1151142d810STejun Heo /**
1161142d810STejun Heo  * stop_one_cpu - stop a cpu
1171142d810STejun Heo  * @cpu: cpu to stop
1181142d810STejun Heo  * @fn: function to execute
1191142d810STejun Heo  * @arg: argument to @fn
1201142d810STejun Heo  *
1211142d810STejun Heo  * Execute @fn(@arg) on @cpu.  @fn is run in a process context with
1221142d810STejun Heo  * the highest priority preempting any task on the cpu and
1231142d810STejun Heo  * monopolizing it.  This function returns after the execution is
1241142d810STejun Heo  * complete.
1251142d810STejun Heo  *
1261142d810STejun Heo  * This function doesn't guarantee @cpu stays online till @fn
1271142d810STejun Heo  * completes.  If @cpu goes down in the middle, execution may happen
1281142d810STejun Heo  * partially or fully on different cpus.  @fn should either be ready
1291142d810STejun Heo  * for that or the caller should ensure that @cpu stays online until
1301142d810STejun Heo  * this function completes.
1311142d810STejun Heo  *
1321142d810STejun Heo  * CONTEXT:
1331142d810STejun Heo  * Might sleep.
1341142d810STejun Heo  *
1351142d810STejun Heo  * RETURNS:
1361142d810STejun Heo  * -ENOENT if @fn(@arg) was not executed because @cpu was offline;
1371142d810STejun Heo  * otherwise, the return value of @fn.
1381142d810STejun Heo  */
stop_one_cpu(unsigned int cpu,cpu_stop_fn_t fn,void * arg)1391142d810STejun Heo int stop_one_cpu(unsigned int cpu, cpu_stop_fn_t fn, void *arg)
1401142d810STejun Heo {
1411142d810STejun Heo 	struct cpu_stop_done done;
142a8b62fd0SPeter Zijlstra 	struct cpu_stop_work work = { .fn = fn, .arg = arg, .done = &done, .caller = _RET_IP_ };
1431142d810STejun Heo 
1441142d810STejun Heo 	cpu_stop_init_done(&done, 1);
145958c5f84SOleg Nesterov 	if (!cpu_stop_queue_work(cpu, &work))
146958c5f84SOleg Nesterov 		return -ENOENT;
147bf89a304SCheng Chao 	/*
148bf89a304SCheng Chao 	 * In case @cpu == smp_proccessor_id() we can avoid a sleep+wakeup
149bf89a304SCheng Chao 	 * cycle by doing a preemption:
150bf89a304SCheng Chao 	 */
151bf89a304SCheng Chao 	cond_resched();
1521142d810STejun Heo 	wait_for_completion(&done.completion);
153958c5f84SOleg Nesterov 	return done.ret;
1541142d810STejun Heo }
1551142d810STejun Heo 
1561be0bd77SPeter Zijlstra /* This controls the threads on each CPU. */
1571be0bd77SPeter Zijlstra enum multi_stop_state {
1581be0bd77SPeter Zijlstra 	/* Dummy starting state for thread. */
1591be0bd77SPeter Zijlstra 	MULTI_STOP_NONE,
1601be0bd77SPeter Zijlstra 	/* Awaiting everyone to be scheduled. */
1611be0bd77SPeter Zijlstra 	MULTI_STOP_PREPARE,
1621be0bd77SPeter Zijlstra 	/* Disable interrupts. */
1631be0bd77SPeter Zijlstra 	MULTI_STOP_DISABLE_IRQ,
1641be0bd77SPeter Zijlstra 	/* Run the function */
1651be0bd77SPeter Zijlstra 	MULTI_STOP_RUN,
1661be0bd77SPeter Zijlstra 	/* Exit */
1671be0bd77SPeter Zijlstra 	MULTI_STOP_EXIT,
1681be0bd77SPeter Zijlstra };
1691be0bd77SPeter Zijlstra 
1701be0bd77SPeter Zijlstra struct multi_stop_data {
1719a301f22SOleg Nesterov 	cpu_stop_fn_t		fn;
1721be0bd77SPeter Zijlstra 	void			*data;
1731be0bd77SPeter Zijlstra 	/* Like num_online_cpus(), but hotplug cpu uses us, so we need this. */
1741be0bd77SPeter Zijlstra 	unsigned int		num_threads;
1751be0bd77SPeter Zijlstra 	const struct cpumask	*active_cpus;
1761be0bd77SPeter Zijlstra 
1771be0bd77SPeter Zijlstra 	enum multi_stop_state	state;
1781be0bd77SPeter Zijlstra 	atomic_t		thread_ack;
1791be0bd77SPeter Zijlstra };
1801be0bd77SPeter Zijlstra 
set_state(struct multi_stop_data * msdata,enum multi_stop_state newstate)1811be0bd77SPeter Zijlstra static void set_state(struct multi_stop_data *msdata,
1821be0bd77SPeter Zijlstra 		      enum multi_stop_state newstate)
1831be0bd77SPeter Zijlstra {
1841be0bd77SPeter Zijlstra 	/* Reset ack counter. */
1851be0bd77SPeter Zijlstra 	atomic_set(&msdata->thread_ack, msdata->num_threads);
1861be0bd77SPeter Zijlstra 	smp_wmb();
187b1fc5833SMark Rutland 	WRITE_ONCE(msdata->state, newstate);
1881be0bd77SPeter Zijlstra }
1891be0bd77SPeter Zijlstra 
1901be0bd77SPeter Zijlstra /* Last one to ack a state moves to the next state. */
ack_state(struct multi_stop_data * msdata)1911be0bd77SPeter Zijlstra static void ack_state(struct multi_stop_data *msdata)
1921be0bd77SPeter Zijlstra {
1931be0bd77SPeter Zijlstra 	if (atomic_dec_and_test(&msdata->thread_ack))
1941be0bd77SPeter Zijlstra 		set_state(msdata, msdata->state + 1);
1951be0bd77SPeter Zijlstra }
1961be0bd77SPeter Zijlstra 
stop_machine_yield(const struct cpumask * cpumask)1974230e2deSZong Li notrace void __weak stop_machine_yield(const struct cpumask *cpumask)
1984ecf0a43SHeiko Carstens {
1994ecf0a43SHeiko Carstens 	cpu_relax();
2004ecf0a43SHeiko Carstens }
2014ecf0a43SHeiko Carstens 
2021be0bd77SPeter Zijlstra /* This is the cpu_stop function which stops the CPU. */
multi_cpu_stop(void * data)2031be0bd77SPeter Zijlstra static int multi_cpu_stop(void *data)
2041be0bd77SPeter Zijlstra {
2051be0bd77SPeter Zijlstra 	struct multi_stop_data *msdata = data;
206b1fc5833SMark Rutland 	enum multi_stop_state newstate, curstate = MULTI_STOP_NONE;
2071be0bd77SPeter Zijlstra 	int cpu = smp_processor_id(), err = 0;
20838f2c691SMartin Schwidefsky 	const struct cpumask *cpumask;
2091be0bd77SPeter Zijlstra 	unsigned long flags;
2101be0bd77SPeter Zijlstra 	bool is_active;
2111be0bd77SPeter Zijlstra 
2121be0bd77SPeter Zijlstra 	/*
2131be0bd77SPeter Zijlstra 	 * When called from stop_machine_from_inactive_cpu(), irq might
2141be0bd77SPeter Zijlstra 	 * already be disabled.  Save the state and restore it on exit.
2151be0bd77SPeter Zijlstra 	 */
2161be0bd77SPeter Zijlstra 	local_save_flags(flags);
2171be0bd77SPeter Zijlstra 
21838f2c691SMartin Schwidefsky 	if (!msdata->active_cpus) {
21938f2c691SMartin Schwidefsky 		cpumask = cpu_online_mask;
22038f2c691SMartin Schwidefsky 		is_active = cpu == cpumask_first(cpumask);
22138f2c691SMartin Schwidefsky 	} else {
22238f2c691SMartin Schwidefsky 		cpumask = msdata->active_cpus;
22338f2c691SMartin Schwidefsky 		is_active = cpumask_test_cpu(cpu, cpumask);
22438f2c691SMartin Schwidefsky 	}
2251be0bd77SPeter Zijlstra 
2261be0bd77SPeter Zijlstra 	/* Simple state machine */
2271be0bd77SPeter Zijlstra 	do {
2281be0bd77SPeter Zijlstra 		/* Chill out and ensure we re-read multi_stop_state. */
2294ecf0a43SHeiko Carstens 		stop_machine_yield(cpumask);
230b1fc5833SMark Rutland 		newstate = READ_ONCE(msdata->state);
231b1fc5833SMark Rutland 		if (newstate != curstate) {
232b1fc5833SMark Rutland 			curstate = newstate;
2331be0bd77SPeter Zijlstra 			switch (curstate) {
2341be0bd77SPeter Zijlstra 			case MULTI_STOP_DISABLE_IRQ:
2351be0bd77SPeter Zijlstra 				local_irq_disable();
2361be0bd77SPeter Zijlstra 				hard_irq_disable();
2371be0bd77SPeter Zijlstra 				break;
2381be0bd77SPeter Zijlstra 			case MULTI_STOP_RUN:
2391be0bd77SPeter Zijlstra 				if (is_active)
2401be0bd77SPeter Zijlstra 					err = msdata->fn(msdata->data);
2411be0bd77SPeter Zijlstra 				break;
2421be0bd77SPeter Zijlstra 			default:
2431be0bd77SPeter Zijlstra 				break;
2441be0bd77SPeter Zijlstra 			}
2451be0bd77SPeter Zijlstra 			ack_state(msdata);
246ce4f06dcSOleg Nesterov 		} else if (curstate > MULTI_STOP_PREPARE) {
247ce4f06dcSOleg Nesterov 			/*
248ce4f06dcSOleg Nesterov 			 * At this stage all other CPUs we depend on must spin
249ce4f06dcSOleg Nesterov 			 * in the same loop. Any reason for hard-lockup should
250ce4f06dcSOleg Nesterov 			 * be detected and reported on their side.
251ce4f06dcSOleg Nesterov 			 */
252ce4f06dcSOleg Nesterov 			touch_nmi_watchdog();
253*2af8780dSPaul E. McKenney 			/* Also suppress RCU CPU stall warnings. */
25432a9f26eSValentin Schneider 			rcu_momentary_eqs();
255c861cac9SMukesh Ojha 		}
2561be0bd77SPeter Zijlstra 	} while (curstate != MULTI_STOP_EXIT);
2571be0bd77SPeter Zijlstra 
2581be0bd77SPeter Zijlstra 	local_irq_restore(flags);
2591be0bd77SPeter Zijlstra 	return err;
2601be0bd77SPeter Zijlstra }
2611be0bd77SPeter Zijlstra 
cpu_stop_queue_two_works(int cpu1,struct cpu_stop_work * work1,int cpu2,struct cpu_stop_work * work2)2625caa1c08SOleg Nesterov static int cpu_stop_queue_two_works(int cpu1, struct cpu_stop_work *work1,
2635caa1c08SOleg Nesterov 				    int cpu2, struct cpu_stop_work *work2)
2645caa1c08SOleg Nesterov {
265d8bc8535SOleg Nesterov 	struct cpu_stopper *stopper1 = per_cpu_ptr(&cpu_stopper, cpu1);
266d8bc8535SOleg Nesterov 	struct cpu_stopper *stopper2 = per_cpu_ptr(&cpu_stopper, cpu2);
2670b26351bSPeter Zijlstra 	DEFINE_WAKE_Q(wakeq);
268d8bc8535SOleg Nesterov 	int err;
269b80a2bfcSPeter Zijlstra 
270e6253970SOleg Nesterov retry:
271b80a2bfcSPeter Zijlstra 	/*
272b80a2bfcSPeter Zijlstra 	 * The waking up of stopper threads has to happen in the same
273b80a2bfcSPeter Zijlstra 	 * scheduling context as the queueing.  Otherwise, there is a
274b80a2bfcSPeter Zijlstra 	 * possibility of one of the above stoppers being woken up by another
275b80a2bfcSPeter Zijlstra 	 * CPU, and preempting us. This will cause us to not wake up the other
276b80a2bfcSPeter Zijlstra 	 * stopper forever.
277b80a2bfcSPeter Zijlstra 	 */
278b80a2bfcSPeter Zijlstra 	preempt_disable();
279de5b55c1SThomas Gleixner 	raw_spin_lock_irq(&stopper1->lock);
280de5b55c1SThomas Gleixner 	raw_spin_lock_nested(&stopper2->lock, SINGLE_DEPTH_NESTING);
281d8bc8535SOleg Nesterov 
282b80a2bfcSPeter Zijlstra 	if (!stopper1->enabled || !stopper2->enabled) {
283d8bc8535SOleg Nesterov 		err = -ENOENT;
284d8bc8535SOleg Nesterov 		goto unlock;
285b80a2bfcSPeter Zijlstra 	}
286b80a2bfcSPeter Zijlstra 
287e6253970SOleg Nesterov 	/*
288e6253970SOleg Nesterov 	 * Ensure that if we race with __stop_cpus() the stoppers won't get
289e6253970SOleg Nesterov 	 * queued up in reverse order leading to system deadlock.
290e6253970SOleg Nesterov 	 *
291e6253970SOleg Nesterov 	 * We can't miss stop_cpus_in_progress if queue_stop_cpus_work() has
292e6253970SOleg Nesterov 	 * queued a work on cpu1 but not on cpu2, we hold both locks.
293e6253970SOleg Nesterov 	 *
294e6253970SOleg Nesterov 	 * It can be falsely true but it is safe to spin until it is cleared,
295e6253970SOleg Nesterov 	 * queue_stop_cpus_work() does everything under preempt_disable().
296e6253970SOleg Nesterov 	 */
297b80a2bfcSPeter Zijlstra 	if (unlikely(stop_cpus_in_progress)) {
298e6253970SOleg Nesterov 		err = -EDEADLK;
299e6253970SOleg Nesterov 		goto unlock;
300b80a2bfcSPeter Zijlstra 	}
301d8bc8535SOleg Nesterov 
302d8bc8535SOleg Nesterov 	err = 0;
3030b26351bSPeter Zijlstra 	__cpu_stop_queue_work(stopper1, work1, &wakeq);
3040b26351bSPeter Zijlstra 	__cpu_stop_queue_work(stopper2, work2, &wakeq);
305b80a2bfcSPeter Zijlstra 
306d8bc8535SOleg Nesterov unlock:
307de5b55c1SThomas Gleixner 	raw_spin_unlock(&stopper2->lock);
308de5b55c1SThomas Gleixner 	raw_spin_unlock_irq(&stopper1->lock);
3095caa1c08SOleg Nesterov 
310e6253970SOleg Nesterov 	if (unlikely(err == -EDEADLK)) {
311b80a2bfcSPeter Zijlstra 		preempt_enable();
312b80a2bfcSPeter Zijlstra 
313e6253970SOleg Nesterov 		while (stop_cpus_in_progress)
314e6253970SOleg Nesterov 			cpu_relax();
315b80a2bfcSPeter Zijlstra 
316e6253970SOleg Nesterov 		goto retry;
317e6253970SOleg Nesterov 	}
3180b26351bSPeter Zijlstra 
3190b26351bSPeter Zijlstra 	wake_up_q(&wakeq);
3209fb8d5dcSIsaac J. Manjarres 	preempt_enable();
3210b26351bSPeter Zijlstra 
322d8bc8535SOleg Nesterov 	return err;
3235caa1c08SOleg Nesterov }
3241be0bd77SPeter Zijlstra /**
3251be0bd77SPeter Zijlstra  * stop_two_cpus - stops two cpus
3261be0bd77SPeter Zijlstra  * @cpu1: the cpu to stop
3271be0bd77SPeter Zijlstra  * @cpu2: the other cpu to stop
3281be0bd77SPeter Zijlstra  * @fn: function to execute
3291be0bd77SPeter Zijlstra  * @arg: argument to @fn
3301be0bd77SPeter Zijlstra  *
3311be0bd77SPeter Zijlstra  * Stops both the current and specified CPU and runs @fn on one of them.
3321be0bd77SPeter Zijlstra  *
3331be0bd77SPeter Zijlstra  * returns when both are completed.
3341be0bd77SPeter Zijlstra  */
stop_two_cpus(unsigned int cpu1,unsigned int cpu2,cpu_stop_fn_t fn,void * arg)3351be0bd77SPeter Zijlstra int stop_two_cpus(unsigned int cpu1, unsigned int cpu2, cpu_stop_fn_t fn, void *arg)
3361be0bd77SPeter Zijlstra {
3371be0bd77SPeter Zijlstra 	struct cpu_stop_done done;
3381be0bd77SPeter Zijlstra 	struct cpu_stop_work work1, work2;
3396acce3efSPeter Zijlstra 	struct multi_stop_data msdata;
3406acce3efSPeter Zijlstra 
3416acce3efSPeter Zijlstra 	msdata = (struct multi_stop_data){
3421be0bd77SPeter Zijlstra 		.fn = fn,
3431be0bd77SPeter Zijlstra 		.data = arg,
3441be0bd77SPeter Zijlstra 		.num_threads = 2,
3451be0bd77SPeter Zijlstra 		.active_cpus = cpumask_of(cpu1),
3461be0bd77SPeter Zijlstra 	};
3471be0bd77SPeter Zijlstra 
3481be0bd77SPeter Zijlstra 	work1 = work2 = (struct cpu_stop_work){
3491be0bd77SPeter Zijlstra 		.fn = multi_cpu_stop,
3501be0bd77SPeter Zijlstra 		.arg = &msdata,
351a8b62fd0SPeter Zijlstra 		.done = &done,
352a8b62fd0SPeter Zijlstra 		.caller = _RET_IP_,
3531be0bd77SPeter Zijlstra 	};
3541be0bd77SPeter Zijlstra 
3551be0bd77SPeter Zijlstra 	cpu_stop_init_done(&done, 2);
3561be0bd77SPeter Zijlstra 	set_state(&msdata, MULTI_STOP_PREPARE);
3571be0bd77SPeter Zijlstra 
3585caa1c08SOleg Nesterov 	if (cpu1 > cpu2)
3595caa1c08SOleg Nesterov 		swap(cpu1, cpu2);
3606a190051SOleg Nesterov 	if (cpu_stop_queue_two_works(cpu1, &work1, cpu2, &work2))
3615caa1c08SOleg Nesterov 		return -ENOENT;
3621be0bd77SPeter Zijlstra 
3631be0bd77SPeter Zijlstra 	wait_for_completion(&done.completion);
3646a190051SOleg Nesterov 	return done.ret;
3651be0bd77SPeter Zijlstra }
3661be0bd77SPeter Zijlstra 
3671142d810STejun Heo /**
3681142d810STejun Heo  * stop_one_cpu_nowait - stop a cpu but don't wait for completion
3691142d810STejun Heo  * @cpu: cpu to stop
3701142d810STejun Heo  * @fn: function to execute
3711142d810STejun Heo  * @arg: argument to @fn
372cf250040SFabian Frederick  * @work_buf: pointer to cpu_stop_work structure
3731142d810STejun Heo  *
3741142d810STejun Heo  * Similar to stop_one_cpu() but doesn't wait for completion.  The
3751142d810STejun Heo  * caller is responsible for ensuring @work_buf is currently unused
3761142d810STejun Heo  * and will remain untouched until stopper starts executing @fn.
3771142d810STejun Heo  *
3781142d810STejun Heo  * CONTEXT:
3791142d810STejun Heo  * Don't care.
3801b034bd9SOleg Nesterov  *
3811b034bd9SOleg Nesterov  * RETURNS:
3821b034bd9SOleg Nesterov  * true if cpu_stop_work was queued successfully and @fn will be called,
3831b034bd9SOleg Nesterov  * false otherwise.
3841142d810STejun Heo  */
stop_one_cpu_nowait(unsigned int cpu,cpu_stop_fn_t fn,void * arg,struct cpu_stop_work * work_buf)3851b034bd9SOleg Nesterov bool stop_one_cpu_nowait(unsigned int cpu, cpu_stop_fn_t fn, void *arg,
3861142d810STejun Heo 			struct cpu_stop_work *work_buf)
3871142d810STejun Heo {
388a8b62fd0SPeter Zijlstra 	*work_buf = (struct cpu_stop_work){ .fn = fn, .arg = arg, .caller = _RET_IP_, };
3891b034bd9SOleg Nesterov 	return cpu_stop_queue_work(cpu, work_buf);
3901142d810STejun Heo }
3911142d810STejun Heo 
queue_stop_cpus_work(const struct cpumask * cpumask,cpu_stop_fn_t fn,void * arg,struct cpu_stop_done * done)3924aff1ca6SOleg Nesterov static bool queue_stop_cpus_work(const struct cpumask *cpumask,
393fd7355baSTejun Heo 				 cpu_stop_fn_t fn, void *arg,
394fd7355baSTejun Heo 				 struct cpu_stop_done *done)
3951142d810STejun Heo {
3961142d810STejun Heo 	struct cpu_stop_work *work;
3971142d810STejun Heo 	unsigned int cpu;
3984aff1ca6SOleg Nesterov 	bool queued = false;
3991142d810STejun Heo 
4001142d810STejun Heo 	/*
4011142d810STejun Heo 	 * Disable preemption while queueing to avoid getting
4021142d810STejun Heo 	 * preempted by a stopper which might wait for other stoppers
4031142d810STejun Heo 	 * to enter @fn which can lead to deadlock.
4041142d810STejun Heo 	 */
405e6253970SOleg Nesterov 	preempt_disable();
406e6253970SOleg Nesterov 	stop_cpus_in_progress = true;
40799d84bf8SPeter Zijlstra 	barrier();
408b377c2a0SOleg Nesterov 	for_each_cpu(cpu, cpumask) {
409b377c2a0SOleg Nesterov 		work = &per_cpu(cpu_stopper.stop_work, cpu);
410b377c2a0SOleg Nesterov 		work->fn = fn;
411b377c2a0SOleg Nesterov 		work->arg = arg;
412b377c2a0SOleg Nesterov 		work->done = done;
4132a2f80ffSValentin Schneider 		work->caller = _RET_IP_;
4144aff1ca6SOleg Nesterov 		if (cpu_stop_queue_work(cpu, work))
4154aff1ca6SOleg Nesterov 			queued = true;
416b377c2a0SOleg Nesterov 	}
41799d84bf8SPeter Zijlstra 	barrier();
418e6253970SOleg Nesterov 	stop_cpus_in_progress = false;
419e6253970SOleg Nesterov 	preempt_enable();
4204aff1ca6SOleg Nesterov 
4214aff1ca6SOleg Nesterov 	return queued;
422fd7355baSTejun Heo }
4231142d810STejun Heo 
__stop_cpus(const struct cpumask * cpumask,cpu_stop_fn_t fn,void * arg)424fd7355baSTejun Heo static int __stop_cpus(const struct cpumask *cpumask,
425fd7355baSTejun Heo 		       cpu_stop_fn_t fn, void *arg)
426fd7355baSTejun Heo {
427fd7355baSTejun Heo 	struct cpu_stop_done done;
428fd7355baSTejun Heo 
429fd7355baSTejun Heo 	cpu_stop_init_done(&done, cpumask_weight(cpumask));
4304aff1ca6SOleg Nesterov 	if (!queue_stop_cpus_work(cpumask, fn, arg, &done))
4314aff1ca6SOleg Nesterov 		return -ENOENT;
4321142d810STejun Heo 	wait_for_completion(&done.completion);
4334aff1ca6SOleg Nesterov 	return done.ret;
4341142d810STejun Heo }
4351142d810STejun Heo 
4361142d810STejun Heo /**
4371142d810STejun Heo  * stop_cpus - stop multiple cpus
4381142d810STejun Heo  * @cpumask: cpus to stop
4391142d810STejun Heo  * @fn: function to execute
4401142d810STejun Heo  * @arg: argument to @fn
4411142d810STejun Heo  *
4421142d810STejun Heo  * Execute @fn(@arg) on online cpus in @cpumask.  On each target cpu,
4431142d810STejun Heo  * @fn is run in a process context with the highest priority
4441142d810STejun Heo  * preempting any task on the cpu and monopolizing it.  This function
4451142d810STejun Heo  * returns after all executions are complete.
4461142d810STejun Heo  *
4471142d810STejun Heo  * This function doesn't guarantee the cpus in @cpumask stay online
4481142d810STejun Heo  * till @fn completes.  If some cpus go down in the middle, execution
4491142d810STejun Heo  * on the cpu may happen partially or fully on different cpus.  @fn
4501142d810STejun Heo  * should either be ready for that or the caller should ensure that
4511142d810STejun Heo  * the cpus stay online until this function completes.
4521142d810STejun Heo  *
4531142d810STejun Heo  * All stop_cpus() calls are serialized making it safe for @fn to wait
4541142d810STejun Heo  * for all cpus to start executing it.
4551142d810STejun Heo  *
4561142d810STejun Heo  * CONTEXT:
4571142d810STejun Heo  * Might sleep.
4581142d810STejun Heo  *
4591142d810STejun Heo  * RETURNS:
4601142d810STejun Heo  * -ENOENT if @fn(@arg) was not executed at all because all cpus in
4611142d810STejun Heo  * @cpumask were offline; otherwise, 0 if all executions of @fn
4621142d810STejun Heo  * returned 0, any non zero return value if any returned non zero.
4631142d810STejun Heo  */
stop_cpus(const struct cpumask * cpumask,cpu_stop_fn_t fn,void * arg)46435f4cd96SYangtao Li static int stop_cpus(const struct cpumask *cpumask, cpu_stop_fn_t fn, void *arg)
4651142d810STejun Heo {
4661142d810STejun Heo 	int ret;
4671142d810STejun Heo 
4681142d810STejun Heo 	/* static works are used, process one request at a time */
4691142d810STejun Heo 	mutex_lock(&stop_cpus_mutex);
4701142d810STejun Heo 	ret = __stop_cpus(cpumask, fn, arg);
4711142d810STejun Heo 	mutex_unlock(&stop_cpus_mutex);
4721142d810STejun Heo 	return ret;
4731142d810STejun Heo }
4741142d810STejun Heo 
cpu_stop_should_run(unsigned int cpu)47514e568e7SThomas Gleixner static int cpu_stop_should_run(unsigned int cpu)
4761142d810STejun Heo {
47714e568e7SThomas Gleixner 	struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
47814e568e7SThomas Gleixner 	unsigned long flags;
47914e568e7SThomas Gleixner 	int run;
48014e568e7SThomas Gleixner 
481de5b55c1SThomas Gleixner 	raw_spin_lock_irqsave(&stopper->lock, flags);
48214e568e7SThomas Gleixner 	run = !list_empty(&stopper->works);
483de5b55c1SThomas Gleixner 	raw_spin_unlock_irqrestore(&stopper->lock, flags);
48414e568e7SThomas Gleixner 	return run;
48514e568e7SThomas Gleixner }
48614e568e7SThomas Gleixner 
cpu_stopper_thread(unsigned int cpu)48714e568e7SThomas Gleixner static void cpu_stopper_thread(unsigned int cpu)
48814e568e7SThomas Gleixner {
48914e568e7SThomas Gleixner 	struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
4901142d810STejun Heo 	struct cpu_stop_work *work;
4911142d810STejun Heo 
4921142d810STejun Heo repeat:
4931142d810STejun Heo 	work = NULL;
494de5b55c1SThomas Gleixner 	raw_spin_lock_irq(&stopper->lock);
4951142d810STejun Heo 	if (!list_empty(&stopper->works)) {
4961142d810STejun Heo 		work = list_first_entry(&stopper->works,
4971142d810STejun Heo 					struct cpu_stop_work, list);
4981142d810STejun Heo 		list_del_init(&work->list);
4991142d810STejun Heo 	}
500de5b55c1SThomas Gleixner 	raw_spin_unlock_irq(&stopper->lock);
5011142d810STejun Heo 
5021142d810STejun Heo 	if (work) {
5031142d810STejun Heo 		cpu_stop_fn_t fn = work->fn;
5041142d810STejun Heo 		void *arg = work->arg;
5051142d810STejun Heo 		struct cpu_stop_done *done = work->done;
506accaf6eaSOleg Nesterov 		int ret;
5071142d810STejun Heo 
508accaf6eaSOleg Nesterov 		/* cpu stop callbacks must not sleep, make in_atomic() == T */
509a8b62fd0SPeter Zijlstra 		stopper->caller = work->caller;
510a8b62fd0SPeter Zijlstra 		stopper->fn = fn;
511accaf6eaSOleg Nesterov 		preempt_count_inc();
5121142d810STejun Heo 		ret = fn(arg);
513dd2e3121SOleg Nesterov 		if (done) {
514dd2e3121SOleg Nesterov 			if (ret)
5151142d810STejun Heo 				done->ret = ret;
5166fa3b826SOleg Nesterov 			cpu_stop_signal_done(done);
517dd2e3121SOleg Nesterov 		}
518accaf6eaSOleg Nesterov 		preempt_count_dec();
519a8b62fd0SPeter Zijlstra 		stopper->fn = NULL;
520a8b62fd0SPeter Zijlstra 		stopper->caller = 0;
5211142d810STejun Heo 		WARN_ONCE(preempt_count(),
522d75f773cSSakari Ailus 			  "cpu_stop: %ps(%p) leaked preempt count\n", fn, arg);
5231142d810STejun Heo 		goto repeat;
5241142d810STejun Heo 	}
52514e568e7SThomas Gleixner }
5261142d810STejun Heo 
stop_machine_park(int cpu)527233e7f26SOleg Nesterov void stop_machine_park(int cpu)
528233e7f26SOleg Nesterov {
529233e7f26SOleg Nesterov 	struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
530233e7f26SOleg Nesterov 	/*
531233e7f26SOleg Nesterov 	 * Lockless. cpu_stopper_thread() will take stopper->lock and flush
532233e7f26SOleg Nesterov 	 * the pending works before it parks, until then it is fine to queue
533233e7f26SOleg Nesterov 	 * the new works.
534233e7f26SOleg Nesterov 	 */
535233e7f26SOleg Nesterov 	stopper->enabled = false;
536233e7f26SOleg Nesterov 	kthread_park(stopper->thread);
537233e7f26SOleg Nesterov }
538233e7f26SOleg Nesterov 
cpu_stop_create(unsigned int cpu)53914e568e7SThomas Gleixner static void cpu_stop_create(unsigned int cpu)
5401142d810STejun Heo {
54102cb7aa9SOleg Nesterov 	sched_set_stop_task(cpu, per_cpu(cpu_stopper.thread, cpu));
54214e568e7SThomas Gleixner }
54314e568e7SThomas Gleixner 
cpu_stop_park(unsigned int cpu)54414e568e7SThomas Gleixner static void cpu_stop_park(unsigned int cpu)
54514e568e7SThomas Gleixner {
5461142d810STejun Heo 	struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
5479c6f7e43SIngo Molnar 
548233e7f26SOleg Nesterov 	WARN_ON(!list_empty(&stopper->works));
54914e568e7SThomas Gleixner }
55014e568e7SThomas Gleixner 
stop_machine_unpark(int cpu)551c00166d8SOleg Nesterov void stop_machine_unpark(int cpu)
552c00166d8SOleg Nesterov {
553c00166d8SOleg Nesterov 	struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
554c00166d8SOleg Nesterov 
555f0cf16cbSOleg Nesterov 	stopper->enabled = true;
556c00166d8SOleg Nesterov 	kthread_unpark(stopper->thread);
557c00166d8SOleg Nesterov }
558c00166d8SOleg Nesterov 
55914e568e7SThomas Gleixner static struct smp_hotplug_thread cpu_stop_threads = {
56002cb7aa9SOleg Nesterov 	.store			= &cpu_stopper.thread,
56114e568e7SThomas Gleixner 	.thread_should_run	= cpu_stop_should_run,
56214e568e7SThomas Gleixner 	.thread_fn		= cpu_stopper_thread,
56314e568e7SThomas Gleixner 	.thread_comm		= "migration/%u",
56414e568e7SThomas Gleixner 	.create			= cpu_stop_create,
56514e568e7SThomas Gleixner 	.park			= cpu_stop_park,
56614e568e7SThomas Gleixner 	.selfparking		= true,
5671142d810STejun Heo };
5681142d810STejun Heo 
cpu_stop_init(void)5691142d810STejun Heo static int __init cpu_stop_init(void)
5701142d810STejun Heo {
5711142d810STejun Heo 	unsigned int cpu;
5721142d810STejun Heo 
5731142d810STejun Heo 	for_each_possible_cpu(cpu) {
5741142d810STejun Heo 		struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
5751142d810STejun Heo 
576de5b55c1SThomas Gleixner 		raw_spin_lock_init(&stopper->lock);
5771142d810STejun Heo 		INIT_LIST_HEAD(&stopper->works);
5781142d810STejun Heo 	}
5791142d810STejun Heo 
58014e568e7SThomas Gleixner 	BUG_ON(smpboot_register_percpu_thread(&cpu_stop_threads));
581c00166d8SOleg Nesterov 	stop_machine_unpark(raw_smp_processor_id());
582f445027eSJeremy Fitzhardinge 	stop_machine_initialized = true;
5831142d810STejun Heo 	return 0;
5841142d810STejun Heo }
5851142d810STejun Heo early_initcall(cpu_stop_init);
5861da177e4SLinus Torvalds 
stop_machine_cpuslocked(cpu_stop_fn_t fn,void * data,const struct cpumask * cpus)587fe5595c0SSebastian Andrzej Siewior int stop_machine_cpuslocked(cpu_stop_fn_t fn, void *data,
588fe5595c0SSebastian Andrzej Siewior 			    const struct cpumask *cpus)
5891da177e4SLinus Torvalds {
5901be0bd77SPeter Zijlstra 	struct multi_stop_data msdata = {
5911be0bd77SPeter Zijlstra 		.fn = fn,
5921be0bd77SPeter Zijlstra 		.data = data,
5933fc1f1e2STejun Heo 		.num_threads = num_online_cpus(),
5941be0bd77SPeter Zijlstra 		.active_cpus = cpus,
5951be0bd77SPeter Zijlstra 	};
5961da177e4SLinus Torvalds 
597fe5595c0SSebastian Andrzej Siewior 	lockdep_assert_cpus_held();
598fe5595c0SSebastian Andrzej Siewior 
599f445027eSJeremy Fitzhardinge 	if (!stop_machine_initialized) {
600f445027eSJeremy Fitzhardinge 		/*
601f445027eSJeremy Fitzhardinge 		 * Handle the case where stop_machine() is called
602f445027eSJeremy Fitzhardinge 		 * early in boot before stop_machine() has been
603f445027eSJeremy Fitzhardinge 		 * initialized.
604f445027eSJeremy Fitzhardinge 		 */
605f445027eSJeremy Fitzhardinge 		unsigned long flags;
606f445027eSJeremy Fitzhardinge 		int ret;
607f445027eSJeremy Fitzhardinge 
6081be0bd77SPeter Zijlstra 		WARN_ON_ONCE(msdata.num_threads != 1);
609f445027eSJeremy Fitzhardinge 
610f445027eSJeremy Fitzhardinge 		local_irq_save(flags);
611f445027eSJeremy Fitzhardinge 		hard_irq_disable();
612f445027eSJeremy Fitzhardinge 		ret = (*fn)(data);
613f445027eSJeremy Fitzhardinge 		local_irq_restore(flags);
614f445027eSJeremy Fitzhardinge 
615f445027eSJeremy Fitzhardinge 		return ret;
616f445027eSJeremy Fitzhardinge 	}
617f445027eSJeremy Fitzhardinge 
6183fc1f1e2STejun Heo 	/* Set the initial state and stop all online cpus. */
6191be0bd77SPeter Zijlstra 	set_state(&msdata, MULTI_STOP_PREPARE);
6201be0bd77SPeter Zijlstra 	return stop_cpus(cpu_online_mask, multi_cpu_stop, &msdata);
6211da177e4SLinus Torvalds }
6221da177e4SLinus Torvalds 
stop_machine(cpu_stop_fn_t fn,void * data,const struct cpumask * cpus)6239a301f22SOleg Nesterov int stop_machine(cpu_stop_fn_t fn, void *data, const struct cpumask *cpus)
6241da177e4SLinus Torvalds {
6251da177e4SLinus Torvalds 	int ret;
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds 	/* No CPUs can come up or down during this. */
628fe5595c0SSebastian Andrzej Siewior 	cpus_read_lock();
629fe5595c0SSebastian Andrzej Siewior 	ret = stop_machine_cpuslocked(fn, data, cpus);
630fe5595c0SSebastian Andrzej Siewior 	cpus_read_unlock();
6311da177e4SLinus Torvalds 	return ret;
6321da177e4SLinus Torvalds }
633eeec4fadSRusty Russell EXPORT_SYMBOL_GPL(stop_machine);
634bbf1bb3eSTejun Heo 
6352760f5a4SPeter Zijlstra #ifdef CONFIG_SCHED_SMT
stop_core_cpuslocked(unsigned int cpu,cpu_stop_fn_t fn,void * data)6362760f5a4SPeter Zijlstra int stop_core_cpuslocked(unsigned int cpu, cpu_stop_fn_t fn, void *data)
6372760f5a4SPeter Zijlstra {
6382760f5a4SPeter Zijlstra 	const struct cpumask *smt_mask = cpu_smt_mask(cpu);
6392760f5a4SPeter Zijlstra 
6402760f5a4SPeter Zijlstra 	struct multi_stop_data msdata = {
6412760f5a4SPeter Zijlstra 		.fn = fn,
6422760f5a4SPeter Zijlstra 		.data = data,
6432760f5a4SPeter Zijlstra 		.num_threads = cpumask_weight(smt_mask),
6442760f5a4SPeter Zijlstra 		.active_cpus = smt_mask,
6452760f5a4SPeter Zijlstra 	};
6462760f5a4SPeter Zijlstra 
6472760f5a4SPeter Zijlstra 	lockdep_assert_cpus_held();
6482760f5a4SPeter Zijlstra 
6492760f5a4SPeter Zijlstra 	/* Set the initial state and stop all online cpus. */
6502760f5a4SPeter Zijlstra 	set_state(&msdata, MULTI_STOP_PREPARE);
6512760f5a4SPeter Zijlstra 	return stop_cpus(smt_mask, multi_cpu_stop, &msdata);
6522760f5a4SPeter Zijlstra }
6532760f5a4SPeter Zijlstra EXPORT_SYMBOL_GPL(stop_core_cpuslocked);
6542760f5a4SPeter Zijlstra #endif
6552760f5a4SPeter Zijlstra 
656f740e6cdSTejun Heo /**
657f740e6cdSTejun Heo  * stop_machine_from_inactive_cpu - stop_machine() from inactive CPU
658f740e6cdSTejun Heo  * @fn: the function to run
659f740e6cdSTejun Heo  * @data: the data ptr for the @fn()
660f740e6cdSTejun Heo  * @cpus: the cpus to run the @fn() on (NULL = any online cpu)
661f740e6cdSTejun Heo  *
662f740e6cdSTejun Heo  * This is identical to stop_machine() but can be called from a CPU which
663f740e6cdSTejun Heo  * is not active.  The local CPU is in the process of hotplug (so no other
664f740e6cdSTejun Heo  * CPU hotplug can start) and not marked active and doesn't have enough
665f740e6cdSTejun Heo  * context to sleep.
666f740e6cdSTejun Heo  *
667f740e6cdSTejun Heo  * This function provides stop_machine() functionality for such state by
668f740e6cdSTejun Heo  * using busy-wait for synchronization and executing @fn directly for local
669f740e6cdSTejun Heo  * CPU.
670f740e6cdSTejun Heo  *
671f740e6cdSTejun Heo  * CONTEXT:
672f740e6cdSTejun Heo  * Local CPU is inactive.  Temporarily stops all active CPUs.
673f740e6cdSTejun Heo  *
674f740e6cdSTejun Heo  * RETURNS:
675f740e6cdSTejun Heo  * 0 if all executions of @fn returned 0, any non zero return value if any
676f740e6cdSTejun Heo  * returned non zero.
677f740e6cdSTejun Heo  */
stop_machine_from_inactive_cpu(cpu_stop_fn_t fn,void * data,const struct cpumask * cpus)6789a301f22SOleg Nesterov int stop_machine_from_inactive_cpu(cpu_stop_fn_t fn, void *data,
679f740e6cdSTejun Heo 				  const struct cpumask *cpus)
680f740e6cdSTejun Heo {
6811be0bd77SPeter Zijlstra 	struct multi_stop_data msdata = { .fn = fn, .data = data,
682f740e6cdSTejun Heo 					    .active_cpus = cpus };
683f740e6cdSTejun Heo 	struct cpu_stop_done done;
684f740e6cdSTejun Heo 	int ret;
685f740e6cdSTejun Heo 
686f740e6cdSTejun Heo 	/* Local CPU must be inactive and CPU hotplug in progress. */
687f740e6cdSTejun Heo 	BUG_ON(cpu_active(raw_smp_processor_id()));
6881be0bd77SPeter Zijlstra 	msdata.num_threads = num_active_cpus() + 1;	/* +1 for local */
689f740e6cdSTejun Heo 
690f740e6cdSTejun Heo 	/* No proper task established and can't sleep - busy wait for lock. */
691f740e6cdSTejun Heo 	while (!mutex_trylock(&stop_cpus_mutex))
692f740e6cdSTejun Heo 		cpu_relax();
693f740e6cdSTejun Heo 
694f740e6cdSTejun Heo 	/* Schedule work on other CPUs and execute directly for local CPU */
6951be0bd77SPeter Zijlstra 	set_state(&msdata, MULTI_STOP_PREPARE);
696f740e6cdSTejun Heo 	cpu_stop_init_done(&done, num_active_cpus());
6971be0bd77SPeter Zijlstra 	queue_stop_cpus_work(cpu_active_mask, multi_cpu_stop, &msdata,
698f740e6cdSTejun Heo 			     &done);
6991be0bd77SPeter Zijlstra 	ret = multi_cpu_stop(&msdata);
700f740e6cdSTejun Heo 
701f740e6cdSTejun Heo 	/* Busy wait for completion. */
702f740e6cdSTejun Heo 	while (!completion_done(&done.completion))
703f740e6cdSTejun Heo 		cpu_relax();
704f740e6cdSTejun Heo 
705f740e6cdSTejun Heo 	mutex_unlock(&stop_cpus_mutex);
706f740e6cdSTejun Heo 	return ret ?: done.ret;
707f740e6cdSTejun Heo }
708