• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * kernel/stop_machine.c
4  *
5  * Copyright (C) 2008, 2005	IBM Corporation.
6  * Copyright (C) 2008, 2005	Rusty Russell rusty@rustcorp.com.au
7  * Copyright (C) 2010		SUSE Linux Products GmbH
8  * Copyright (C) 2010		Tejun Heo <tj@kernel.org>
9  */
10 #include <linux/compiler.h>
11 #include <linux/completion.h>
12 #include <linux/cpu.h>
13 #include <linux/init.h>
14 #include <linux/kthread.h>
15 #include <linux/export.h>
16 #include <linux/percpu.h>
17 #include <linux/sched.h>
18 #include <linux/stop_machine.h>
19 #include <linux/interrupt.h>
20 #include <linux/kallsyms.h>
21 #include <linux/smpboot.h>
22 #include <linux/atomic.h>
23 #include <linux/nmi.h>
24 #include <linux/sched/wake_q.h>
25 #include <linux/slab.h>
26 
27 /* the actual stopper, one per every possible cpu, enabled on online cpus */
28 struct cpu_stopper {
29 	struct task_struct	*thread;
30 
31 	raw_spinlock_t		lock;
32 	bool			enabled;	/* is this stopper enabled? */
33 	struct list_head	works;		/* list of pending works */
34 
35 	struct cpu_stop_work	stop_work;	/* for stop_cpus */
36 };
37 
38 static DEFINE_PER_CPU(struct cpu_stopper, cpu_stopper);
39 static bool stop_machine_initialized = false;
40 
41 /* static data for stop_cpus */
42 static DEFINE_MUTEX(stop_cpus_mutex);
43 static bool stop_cpus_in_progress;
44 
cpu_stop_init_done(struct cpu_stop_done * done,unsigned int nr_todo)45 static void cpu_stop_init_done(struct cpu_stop_done *done, unsigned int nr_todo)
46 {
47 	memset(done, 0, sizeof(*done));
48 	atomic_set(&done->nr_todo, nr_todo);
49 	init_completion(&done->completion);
50 }
51 
52 /* signal completion unless @done is NULL */
cpu_stop_signal_done(struct cpu_stop_done * done)53 static void cpu_stop_signal_done(struct cpu_stop_done *done)
54 {
55 	if (atomic_dec_and_test(&done->nr_todo))
56 		complete(&done->completion);
57 }
58 
__cpu_stop_queue_work(struct cpu_stopper * stopper,struct cpu_stop_work * work,struct wake_q_head * wakeq)59 static void __cpu_stop_queue_work(struct cpu_stopper *stopper,
60 					struct cpu_stop_work *work,
61 					struct wake_q_head *wakeq)
62 {
63 	list_add_tail(&work->list, &stopper->works);
64 	wake_q_add(wakeq, stopper->thread);
65 }
66 
67 /* queue @work to @stopper.  if offline, @work is completed immediately */
cpu_stop_queue_work(unsigned int cpu,struct cpu_stop_work * work)68 static bool cpu_stop_queue_work(unsigned int cpu, struct cpu_stop_work *work)
69 {
70 	struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
71 	DEFINE_WAKE_Q(wakeq);
72 	unsigned long flags;
73 	bool enabled;
74 
75 	preempt_disable();
76 	raw_spin_lock_irqsave(&stopper->lock, flags);
77 	enabled = stopper->enabled;
78 	if (enabled)
79 		__cpu_stop_queue_work(stopper, work, &wakeq);
80 	else if (work->done)
81 		cpu_stop_signal_done(work->done);
82 	raw_spin_unlock_irqrestore(&stopper->lock, flags);
83 
84 	wake_up_q(&wakeq);
85 	preempt_enable();
86 
87 	return enabled;
88 }
89 
90 /**
91  * stop_one_cpu - stop a cpu
92  * @cpu: cpu to stop
93  * @fn: function to execute
94  * @arg: argument to @fn
95  *
96  * Execute @fn(@arg) on @cpu.  @fn is run in a process context with
97  * the highest priority preempting any task on the cpu and
98  * monopolizing it.  This function returns after the execution is
99  * complete.
100  *
101  * This function doesn't guarantee @cpu stays online till @fn
102  * completes.  If @cpu goes down in the middle, execution may happen
103  * partially or fully on different cpus.  @fn should either be ready
104  * for that or the caller should ensure that @cpu stays online until
105  * this function completes.
106  *
107  * CONTEXT:
108  * Might sleep.
109  *
110  * RETURNS:
111  * -ENOENT if @fn(@arg) was not executed because @cpu was offline;
112  * otherwise, the return value of @fn.
113  */
stop_one_cpu(unsigned int cpu,cpu_stop_fn_t fn,void * arg)114 int stop_one_cpu(unsigned int cpu, cpu_stop_fn_t fn, void *arg)
115 {
116 	struct cpu_stop_done done;
117 	struct cpu_stop_work work = { .fn = fn, .arg = arg, .done = &done };
118 
119 	cpu_stop_init_done(&done, 1);
120 	if (!cpu_stop_queue_work(cpu, &work))
121 		return -ENOENT;
122 	/*
123 	 * In case @cpu == smp_proccessor_id() we can avoid a sleep+wakeup
124 	 * cycle by doing a preemption:
125 	 */
126 	cond_resched();
127 	wait_for_completion(&done.completion);
128 	return done.ret;
129 }
130 
131 /* This controls the threads on each CPU. */
132 enum multi_stop_state {
133 	/* Dummy starting state for thread. */
134 	MULTI_STOP_NONE,
135 	/* Awaiting everyone to be scheduled. */
136 	MULTI_STOP_PREPARE,
137 	/* Disable interrupts. */
138 	MULTI_STOP_DISABLE_IRQ,
139 	/* Run the function */
140 	MULTI_STOP_RUN,
141 	/* Exit */
142 	MULTI_STOP_EXIT,
143 };
144 
145 struct multi_stop_data {
146 	cpu_stop_fn_t		fn;
147 	void			*data;
148 	/* Like num_online_cpus(), but hotplug cpu uses us, so we need this. */
149 	unsigned int		num_threads;
150 	const struct cpumask	*active_cpus;
151 
152 	enum multi_stop_state	state;
153 	atomic_t		thread_ack;
154 };
155 
set_state(struct multi_stop_data * msdata,enum multi_stop_state newstate)156 static void set_state(struct multi_stop_data *msdata,
157 		      enum multi_stop_state newstate)
158 {
159 	/* Reset ack counter. */
160 	atomic_set(&msdata->thread_ack, msdata->num_threads);
161 	smp_wmb();
162 	WRITE_ONCE(msdata->state, newstate);
163 }
164 
165 /* Last one to ack a state moves to the next state. */
ack_state(struct multi_stop_data * msdata)166 static void ack_state(struct multi_stop_data *msdata)
167 {
168 	if (atomic_dec_and_test(&msdata->thread_ack))
169 		set_state(msdata, msdata->state + 1);
170 }
171 
stop_machine_yield(const struct cpumask * cpumask)172 notrace void __weak stop_machine_yield(const struct cpumask *cpumask)
173 {
174 	cpu_relax();
175 }
176 
177 /* This is the cpu_stop function which stops the CPU. */
multi_cpu_stop(void * data)178 static int multi_cpu_stop(void *data)
179 {
180 	struct multi_stop_data *msdata = data;
181 	enum multi_stop_state newstate, curstate = MULTI_STOP_NONE;
182 	int cpu = smp_processor_id(), err = 0;
183 	const struct cpumask *cpumask;
184 	unsigned long flags;
185 	bool is_active;
186 
187 	/*
188 	 * When called from stop_machine_from_inactive_cpu(), irq might
189 	 * already be disabled.  Save the state and restore it on exit.
190 	 */
191 	local_save_flags(flags);
192 
193 	if (!msdata->active_cpus) {
194 		cpumask = cpu_online_mask;
195 		is_active = cpu == cpumask_first(cpumask);
196 	} else {
197 		cpumask = msdata->active_cpus;
198 		is_active = cpumask_test_cpu(cpu, cpumask);
199 	}
200 
201 	/* Simple state machine */
202 	do {
203 		/* Chill out and ensure we re-read multi_stop_state. */
204 		stop_machine_yield(cpumask);
205 		newstate = READ_ONCE(msdata->state);
206 		if (newstate != curstate) {
207 			curstate = newstate;
208 			switch (curstate) {
209 			case MULTI_STOP_DISABLE_IRQ:
210 				local_irq_disable();
211 				hard_irq_disable();
212 				break;
213 			case MULTI_STOP_RUN:
214 				if (is_active)
215 					err = msdata->fn(msdata->data);
216 				break;
217 			default:
218 				break;
219 			}
220 			ack_state(msdata);
221 		} else if (curstate > MULTI_STOP_PREPARE) {
222 			/*
223 			 * At this stage all other CPUs we depend on must spin
224 			 * in the same loop. Any reason for hard-lockup should
225 			 * be detected and reported on their side.
226 			 */
227 			touch_nmi_watchdog();
228 		}
229 		rcu_momentary_dyntick_idle();
230 	} while (curstate != MULTI_STOP_EXIT);
231 
232 	local_irq_restore(flags);
233 	return err;
234 }
235 
cpu_stop_queue_two_works(int cpu1,struct cpu_stop_work * work1,int cpu2,struct cpu_stop_work * work2)236 static int cpu_stop_queue_two_works(int cpu1, struct cpu_stop_work *work1,
237 				    int cpu2, struct cpu_stop_work *work2)
238 {
239 	struct cpu_stopper *stopper1 = per_cpu_ptr(&cpu_stopper, cpu1);
240 	struct cpu_stopper *stopper2 = per_cpu_ptr(&cpu_stopper, cpu2);
241 	DEFINE_WAKE_Q(wakeq);
242 	int err;
243 
244 retry:
245 	/*
246 	 * The waking up of stopper threads has to happen in the same
247 	 * scheduling context as the queueing.  Otherwise, there is a
248 	 * possibility of one of the above stoppers being woken up by another
249 	 * CPU, and preempting us. This will cause us to not wake up the other
250 	 * stopper forever.
251 	 */
252 	preempt_disable();
253 	raw_spin_lock_irq(&stopper1->lock);
254 	raw_spin_lock_nested(&stopper2->lock, SINGLE_DEPTH_NESTING);
255 
256 	if (!stopper1->enabled || !stopper2->enabled) {
257 		err = -ENOENT;
258 		goto unlock;
259 	}
260 
261 	/*
262 	 * Ensure that if we race with __stop_cpus() the stoppers won't get
263 	 * queued up in reverse order leading to system deadlock.
264 	 *
265 	 * We can't miss stop_cpus_in_progress if queue_stop_cpus_work() has
266 	 * queued a work on cpu1 but not on cpu2, we hold both locks.
267 	 *
268 	 * It can be falsely true but it is safe to spin until it is cleared,
269 	 * queue_stop_cpus_work() does everything under preempt_disable().
270 	 */
271 	if (unlikely(stop_cpus_in_progress)) {
272 		err = -EDEADLK;
273 		goto unlock;
274 	}
275 
276 	err = 0;
277 	__cpu_stop_queue_work(stopper1, work1, &wakeq);
278 	__cpu_stop_queue_work(stopper2, work2, &wakeq);
279 
280 unlock:
281 	raw_spin_unlock(&stopper2->lock);
282 	raw_spin_unlock_irq(&stopper1->lock);
283 
284 	if (unlikely(err == -EDEADLK)) {
285 		preempt_enable();
286 
287 		while (stop_cpus_in_progress)
288 			cpu_relax();
289 
290 		goto retry;
291 	}
292 
293 	wake_up_q(&wakeq);
294 	preempt_enable();
295 
296 	return err;
297 }
298 /**
299  * stop_two_cpus - stops two cpus
300  * @cpu1: the cpu to stop
301  * @cpu2: the other cpu to stop
302  * @fn: function to execute
303  * @arg: argument to @fn
304  *
305  * Stops both the current and specified CPU and runs @fn on one of them.
306  *
307  * returns when both are completed.
308  */
stop_two_cpus(unsigned int cpu1,unsigned int cpu2,cpu_stop_fn_t fn,void * arg)309 int stop_two_cpus(unsigned int cpu1, unsigned int cpu2, cpu_stop_fn_t fn, void *arg)
310 {
311 	struct cpu_stop_done done;
312 	struct cpu_stop_work work1, work2;
313 	struct multi_stop_data msdata;
314 
315 	msdata = (struct multi_stop_data){
316 		.fn = fn,
317 		.data = arg,
318 		.num_threads = 2,
319 		.active_cpus = cpumask_of(cpu1),
320 	};
321 
322 	work1 = work2 = (struct cpu_stop_work){
323 		.fn = multi_cpu_stop,
324 		.arg = &msdata,
325 		.done = &done
326 	};
327 
328 	cpu_stop_init_done(&done, 2);
329 	set_state(&msdata, MULTI_STOP_PREPARE);
330 
331 	if (cpu1 > cpu2)
332 		swap(cpu1, cpu2);
333 	if (cpu_stop_queue_two_works(cpu1, &work1, cpu2, &work2))
334 		return -ENOENT;
335 
336 	wait_for_completion(&done.completion);
337 	return done.ret;
338 }
339 
340 /**
341  * stop_one_cpu_nowait - stop a cpu but don't wait for completion
342  * @cpu: cpu to stop
343  * @fn: function to execute
344  * @arg: argument to @fn
345  * @work_buf: pointer to cpu_stop_work structure
346  *
347  * Similar to stop_one_cpu() but doesn't wait for completion.  The
348  * caller is responsible for ensuring @work_buf is currently unused
349  * and will remain untouched until stopper starts executing @fn.
350  *
351  * CONTEXT:
352  * Don't care.
353  *
354  * RETURNS:
355  * true if cpu_stop_work was queued successfully and @fn will be called,
356  * false otherwise.
357  */
stop_one_cpu_nowait(unsigned int cpu,cpu_stop_fn_t fn,void * arg,struct cpu_stop_work * work_buf)358 bool stop_one_cpu_nowait(unsigned int cpu, cpu_stop_fn_t fn, void *arg,
359 			struct cpu_stop_work *work_buf)
360 {
361 	*work_buf = (struct cpu_stop_work){ .fn = fn, .arg = arg, };
362 	return cpu_stop_queue_work(cpu, work_buf);
363 }
364 EXPORT_SYMBOL_GPL(stop_one_cpu_nowait);
365 
366 /**
367  * stop_one_cpu_async - stop a cpu and wait for completion in a separated
368  *			function: stop_wait_work()
369  * @cpu: cpu to stop
370  * @fn: function to execute
371  * @arg: argument to @fn
372  * @work_buf: pointer to cpu_stop_work structure
373  *
374  * CONTEXT:
375  * Might sleep.
376  *
377  * RETURNS:
378  * 0 if cpu_stop_work was queued successfully and @fn will be called.
379  * ENOENT if @fn(@arg) was not executed because @cpu was offline.
380  */
stop_one_cpu_async(unsigned int cpu,cpu_stop_fn_t fn,void * arg,struct cpu_stop_work * work_buf,struct cpu_stop_done * done)381 int stop_one_cpu_async(unsigned int cpu, cpu_stop_fn_t fn, void *arg,
382 		       struct cpu_stop_work *work_buf,
383 		       struct cpu_stop_done *done)
384 {
385 	cpu_stop_init_done(done, 1);
386 
387 	work_buf->done = done;
388 	work_buf->fn = fn;
389 	work_buf->arg = arg;
390 
391 	if (cpu_stop_queue_work(cpu, work_buf))
392 		return 0;
393 
394 	work_buf->done = NULL;
395 
396 	return -ENOENT;
397 }
398 
399 /**
400  * cpu_stop_work_wait - wait for a stop initiated by stop_one_cpu_async().
401  * @work_buf: pointer to cpu_stop_work structure
402  *
403  * CONTEXT:
404  * Might sleep.
405  */
cpu_stop_work_wait(struct cpu_stop_work * work_buf)406 void cpu_stop_work_wait(struct cpu_stop_work *work_buf)
407 {
408 	struct cpu_stop_done *done = work_buf->done;
409 
410 	wait_for_completion(&done->completion);
411 	work_buf->done = NULL;
412 }
413 
queue_stop_cpus_work(const struct cpumask * cpumask,cpu_stop_fn_t fn,void * arg,struct cpu_stop_done * done)414 static bool queue_stop_cpus_work(const struct cpumask *cpumask,
415 				 cpu_stop_fn_t fn, void *arg,
416 				 struct cpu_stop_done *done)
417 {
418 	struct cpu_stop_work *work;
419 	unsigned int cpu;
420 	bool queued = false;
421 
422 	/*
423 	 * Disable preemption while queueing to avoid getting
424 	 * preempted by a stopper which might wait for other stoppers
425 	 * to enter @fn which can lead to deadlock.
426 	 */
427 	preempt_disable();
428 	stop_cpus_in_progress = true;
429 	barrier();
430 	for_each_cpu(cpu, cpumask) {
431 		work = &per_cpu(cpu_stopper.stop_work, cpu);
432 		work->fn = fn;
433 		work->arg = arg;
434 		work->done = done;
435 		if (cpu_stop_queue_work(cpu, work))
436 			queued = true;
437 	}
438 	barrier();
439 	stop_cpus_in_progress = false;
440 	preempt_enable();
441 
442 	return queued;
443 }
444 
__stop_cpus(const struct cpumask * cpumask,cpu_stop_fn_t fn,void * arg)445 static int __stop_cpus(const struct cpumask *cpumask,
446 		       cpu_stop_fn_t fn, void *arg)
447 {
448 	struct cpu_stop_done done;
449 
450 	cpu_stop_init_done(&done, cpumask_weight(cpumask));
451 	if (!queue_stop_cpus_work(cpumask, fn, arg, &done))
452 		return -ENOENT;
453 	wait_for_completion(&done.completion);
454 	return done.ret;
455 }
456 
457 /**
458  * stop_cpus - stop multiple cpus
459  * @cpumask: cpus to stop
460  * @fn: function to execute
461  * @arg: argument to @fn
462  *
463  * Execute @fn(@arg) on online cpus in @cpumask.  On each target cpu,
464  * @fn is run in a process context with the highest priority
465  * preempting any task on the cpu and monopolizing it.  This function
466  * returns after all executions are complete.
467  *
468  * This function doesn't guarantee the cpus in @cpumask stay online
469  * till @fn completes.  If some cpus go down in the middle, execution
470  * on the cpu may happen partially or fully on different cpus.  @fn
471  * should either be ready for that or the caller should ensure that
472  * the cpus stay online until this function completes.
473  *
474  * All stop_cpus() calls are serialized making it safe for @fn to wait
475  * for all cpus to start executing it.
476  *
477  * CONTEXT:
478  * Might sleep.
479  *
480  * RETURNS:
481  * -ENOENT if @fn(@arg) was not executed at all because all cpus in
482  * @cpumask were offline; otherwise, 0 if all executions of @fn
483  * returned 0, any non zero return value if any returned non zero.
484  */
stop_cpus(const struct cpumask * cpumask,cpu_stop_fn_t fn,void * arg)485 static int stop_cpus(const struct cpumask *cpumask, cpu_stop_fn_t fn, void *arg)
486 {
487 	int ret;
488 
489 	/* static works are used, process one request at a time */
490 	mutex_lock(&stop_cpus_mutex);
491 	ret = __stop_cpus(cpumask, fn, arg);
492 	mutex_unlock(&stop_cpus_mutex);
493 	return ret;
494 }
495 
cpu_stop_should_run(unsigned int cpu)496 static int cpu_stop_should_run(unsigned int cpu)
497 {
498 	struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
499 	unsigned long flags;
500 	int run;
501 
502 	raw_spin_lock_irqsave(&stopper->lock, flags);
503 	run = !list_empty(&stopper->works);
504 	raw_spin_unlock_irqrestore(&stopper->lock, flags);
505 	return run;
506 }
507 
cpu_stopper_thread(unsigned int cpu)508 static void cpu_stopper_thread(unsigned int cpu)
509 {
510 	struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
511 	struct cpu_stop_work *work;
512 
513 repeat:
514 	work = NULL;
515 	raw_spin_lock_irq(&stopper->lock);
516 	if (!list_empty(&stopper->works)) {
517 		work = list_first_entry(&stopper->works,
518 					struct cpu_stop_work, list);
519 		list_del_init(&work->list);
520 	}
521 	raw_spin_unlock_irq(&stopper->lock);
522 
523 	if (work) {
524 		cpu_stop_fn_t fn = work->fn;
525 		void *arg = work->arg;
526 		struct cpu_stop_done *done = work->done;
527 		int ret;
528 
529 		/* cpu stop callbacks must not sleep, make in_atomic() == T */
530 		preempt_count_inc();
531 		ret = fn(arg);
532 		if (done) {
533 			if (ret)
534 				done->ret = ret;
535 			cpu_stop_signal_done(done);
536 		}
537 		preempt_count_dec();
538 		WARN_ONCE(preempt_count(),
539 			  "cpu_stop: %ps(%p) leaked preempt count\n", fn, arg);
540 		goto repeat;
541 	}
542 }
543 
stop_machine_park(int cpu)544 void stop_machine_park(int cpu)
545 {
546 	struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
547 	/*
548 	 * Lockless. cpu_stopper_thread() will take stopper->lock and flush
549 	 * the pending works before it parks, until then it is fine to queue
550 	 * the new works.
551 	 */
552 	stopper->enabled = false;
553 	kthread_park(stopper->thread);
554 }
555 
556 extern void sched_set_stop_task(int cpu, struct task_struct *stop);
557 
cpu_stop_create(unsigned int cpu)558 static void cpu_stop_create(unsigned int cpu)
559 {
560 	sched_set_stop_task(cpu, per_cpu(cpu_stopper.thread, cpu));
561 }
562 
cpu_stop_park(unsigned int cpu)563 static void cpu_stop_park(unsigned int cpu)
564 {
565 	struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
566 
567 	WARN_ON(!list_empty(&stopper->works));
568 }
569 
stop_machine_unpark(int cpu)570 void stop_machine_unpark(int cpu)
571 {
572 	struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
573 
574 	stopper->enabled = true;
575 	kthread_unpark(stopper->thread);
576 }
577 
578 static struct smp_hotplug_thread cpu_stop_threads = {
579 	.store			= &cpu_stopper.thread,
580 	.thread_should_run	= cpu_stop_should_run,
581 	.thread_fn		= cpu_stopper_thread,
582 	.thread_comm		= "migration/%u",
583 	.create			= cpu_stop_create,
584 	.park			= cpu_stop_park,
585 	.selfparking		= true,
586 };
587 
cpu_stop_init(void)588 static int __init cpu_stop_init(void)
589 {
590 	unsigned int cpu;
591 
592 	for_each_possible_cpu(cpu) {
593 		struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
594 
595 		raw_spin_lock_init(&stopper->lock);
596 		INIT_LIST_HEAD(&stopper->works);
597 	}
598 
599 	BUG_ON(smpboot_register_percpu_thread(&cpu_stop_threads));
600 	stop_machine_unpark(raw_smp_processor_id());
601 	stop_machine_initialized = true;
602 	return 0;
603 }
604 early_initcall(cpu_stop_init);
605 
stop_machine_cpuslocked(cpu_stop_fn_t fn,void * data,const struct cpumask * cpus)606 int stop_machine_cpuslocked(cpu_stop_fn_t fn, void *data,
607 			    const struct cpumask *cpus)
608 {
609 	struct multi_stop_data msdata = {
610 		.fn = fn,
611 		.data = data,
612 		.num_threads = num_online_cpus(),
613 		.active_cpus = cpus,
614 	};
615 
616 	lockdep_assert_cpus_held();
617 
618 	if (!stop_machine_initialized) {
619 		/*
620 		 * Handle the case where stop_machine() is called
621 		 * early in boot before stop_machine() has been
622 		 * initialized.
623 		 */
624 		unsigned long flags;
625 		int ret;
626 
627 		WARN_ON_ONCE(msdata.num_threads != 1);
628 
629 		local_irq_save(flags);
630 		hard_irq_disable();
631 		ret = (*fn)(data);
632 		local_irq_restore(flags);
633 
634 		return ret;
635 	}
636 
637 	/* Set the initial state and stop all online cpus. */
638 	set_state(&msdata, MULTI_STOP_PREPARE);
639 	return stop_cpus(cpu_online_mask, multi_cpu_stop, &msdata);
640 }
641 
stop_machine(cpu_stop_fn_t fn,void * data,const struct cpumask * cpus)642 int stop_machine(cpu_stop_fn_t fn, void *data, const struct cpumask *cpus)
643 {
644 	int ret;
645 
646 	/* No CPUs can come up or down during this. */
647 	cpus_read_lock();
648 	ret = stop_machine_cpuslocked(fn, data, cpus);
649 	cpus_read_unlock();
650 	return ret;
651 }
652 EXPORT_SYMBOL_GPL(stop_machine);
653 
654 /**
655  * stop_machine_from_inactive_cpu - stop_machine() from inactive CPU
656  * @fn: the function to run
657  * @data: the data ptr for the @fn()
658  * @cpus: the cpus to run the @fn() on (NULL = any online cpu)
659  *
660  * This is identical to stop_machine() but can be called from a CPU which
661  * is not active.  The local CPU is in the process of hotplug (so no other
662  * CPU hotplug can start) and not marked active and doesn't have enough
663  * context to sleep.
664  *
665  * This function provides stop_machine() functionality for such state by
666  * using busy-wait for synchronization and executing @fn directly for local
667  * CPU.
668  *
669  * CONTEXT:
670  * Local CPU is inactive.  Temporarily stops all active CPUs.
671  *
672  * RETURNS:
673  * 0 if all executions of @fn returned 0, any non zero return value if any
674  * returned non zero.
675  */
stop_machine_from_inactive_cpu(cpu_stop_fn_t fn,void * data,const struct cpumask * cpus)676 int stop_machine_from_inactive_cpu(cpu_stop_fn_t fn, void *data,
677 				  const struct cpumask *cpus)
678 {
679 	struct multi_stop_data msdata = { .fn = fn, .data = data,
680 					    .active_cpus = cpus };
681 	struct cpu_stop_done done;
682 	int ret;
683 
684 	/* Local CPU must be inactive and CPU hotplug in progress. */
685 	BUG_ON(cpu_active(raw_smp_processor_id()));
686 	msdata.num_threads = num_active_cpus() + 1;	/* +1 for local */
687 
688 	/* No proper task established and can't sleep - busy wait for lock. */
689 	while (!mutex_trylock(&stop_cpus_mutex))
690 		cpu_relax();
691 
692 	/* Schedule work on other CPUs and execute directly for local CPU */
693 	set_state(&msdata, MULTI_STOP_PREPARE);
694 	cpu_stop_init_done(&done, num_active_cpus());
695 	queue_stop_cpus_work(cpu_active_mask, multi_cpu_stop, &msdata,
696 			     &done);
697 	ret = multi_cpu_stop(&msdata);
698 
699 	/* Busy wait for completion. */
700 	while (!completion_done(&done.completion))
701 		cpu_relax();
702 
703 	mutex_unlock(&stop_cpus_mutex);
704 	return ret ?: done.ret;
705 }
706