• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* CPU control.
2  * (C) 2001, 2002, 2003, 2004 Rusty Russell
3  *
4  * This code is licenced under the GPL.
5  */
6 #include <linux/proc_fs.h>
7 #include <linux/smp.h>
8 #include <linux/init.h>
9 #include <linux/notifier.h>
10 #include <linux/sched.h>
11 #include <linux/unistd.h>
12 #include <linux/cpu.h>
13 #include <linux/oom.h>
14 #include <linux/rcupdate.h>
15 #include <linux/export.h>
16 #include <linux/bug.h>
17 #include <linux/kthread.h>
18 #include <linux/stop_machine.h>
19 #include <linux/mutex.h>
20 #include <linux/gfp.h>
21 #include <linux/suspend.h>
22 #include <linux/lockdep.h>
23 #include <linux/tick.h>
24 #include <linux/irq.h>
25 #include <linux/smpboot.h>
26 #include <linux/relay.h>
27 #include <linux/slab.h>
28 
29 #include <trace/events/power.h>
30 #define CREATE_TRACE_POINTS
31 #include <trace/events/cpuhp.h>
32 
33 #include "smpboot.h"
34 
35 /**
36  * cpuhp_cpu_state - Per cpu hotplug state storage
37  * @state:	The current cpu state
38  * @target:	The target state
39  * @thread:	Pointer to the hotplug thread
40  * @should_run:	Thread should execute
41  * @rollback:	Perform a rollback
42  * @single:	Single callback invocation
43  * @bringup:	Single callback bringup or teardown selector
44  * @cb_state:	The state for a single callback (install/uninstall)
45  * @result:	Result of the operation
46  * @done:	Signal completion to the issuer of the task
47  */
48 struct cpuhp_cpu_state {
49 	enum cpuhp_state	state;
50 	enum cpuhp_state	target;
51 #ifdef CONFIG_SMP
52 	struct task_struct	*thread;
53 	bool			should_run;
54 	bool			rollback;
55 	bool			single;
56 	bool			bringup;
57 	struct hlist_node	*node;
58 	enum cpuhp_state	cb_state;
59 	int			result;
60 	struct completion	done;
61 #endif
62 };
63 
64 static DEFINE_PER_CPU(struct cpuhp_cpu_state, cpuhp_state);
65 
66 #if defined(CONFIG_LOCKDEP) && defined(CONFIG_SMP)
67 static struct lock_class_key cpuhp_state_key;
68 static struct lockdep_map cpuhp_state_lock_map =
69 	STATIC_LOCKDEP_MAP_INIT("cpuhp_state", &cpuhp_state_key);
70 #endif
71 
72 /**
73  * cpuhp_step - Hotplug state machine step
74  * @name:	Name of the step
75  * @startup:	Startup function of the step
76  * @teardown:	Teardown function of the step
77  * @skip_onerr:	Do not invoke the functions on error rollback
78  *		Will go away once the notifiers	are gone
79  * @cant_stop:	Bringup/teardown can't be stopped at this step
80  */
81 struct cpuhp_step {
82 	const char		*name;
83 	union {
84 		int		(*single)(unsigned int cpu);
85 		int		(*multi)(unsigned int cpu,
86 					 struct hlist_node *node);
87 	} startup;
88 	union {
89 		int		(*single)(unsigned int cpu);
90 		int		(*multi)(unsigned int cpu,
91 					 struct hlist_node *node);
92 	} teardown;
93 	struct hlist_head	list;
94 	bool			skip_onerr;
95 	bool			cant_stop;
96 	bool			multi_instance;
97 };
98 
99 static DEFINE_MUTEX(cpuhp_state_mutex);
100 static struct cpuhp_step cpuhp_bp_states[];
101 static struct cpuhp_step cpuhp_ap_states[];
102 
cpuhp_is_ap_state(enum cpuhp_state state)103 static bool cpuhp_is_ap_state(enum cpuhp_state state)
104 {
105 	/*
106 	 * The extra check for CPUHP_TEARDOWN_CPU is only for documentation
107 	 * purposes as that state is handled explicitly in cpu_down.
108 	 */
109 	return state > CPUHP_BRINGUP_CPU && state != CPUHP_TEARDOWN_CPU;
110 }
111 
cpuhp_get_step(enum cpuhp_state state)112 static struct cpuhp_step *cpuhp_get_step(enum cpuhp_state state)
113 {
114 	struct cpuhp_step *sp;
115 
116 	sp = cpuhp_is_ap_state(state) ? cpuhp_ap_states : cpuhp_bp_states;
117 	return sp + state;
118 }
119 
120 /**
121  * cpuhp_invoke_callback _ Invoke the callbacks for a given state
122  * @cpu:	The cpu for which the callback should be invoked
123  * @step:	The step in the state machine
124  * @bringup:	True if the bringup callback should be invoked
125  *
126  * Called from cpu hotplug and from the state register machinery.
127  */
cpuhp_invoke_callback(unsigned int cpu,enum cpuhp_state state,bool bringup,struct hlist_node * node)128 static int cpuhp_invoke_callback(unsigned int cpu, enum cpuhp_state state,
129 				 bool bringup, struct hlist_node *node)
130 {
131 	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
132 	struct cpuhp_step *step = cpuhp_get_step(state);
133 	int (*cbm)(unsigned int cpu, struct hlist_node *node);
134 	int (*cb)(unsigned int cpu);
135 	int ret, cnt;
136 
137 	if (!step->multi_instance) {
138 		cb = bringup ? step->startup.single : step->teardown.single;
139 		if (!cb)
140 			return 0;
141 		trace_cpuhp_enter(cpu, st->target, state, cb);
142 		ret = cb(cpu);
143 		trace_cpuhp_exit(cpu, st->state, state, ret);
144 		return ret;
145 	}
146 	cbm = bringup ? step->startup.multi : step->teardown.multi;
147 	if (!cbm)
148 		return 0;
149 
150 	/* Single invocation for instance add/remove */
151 	if (node) {
152 		trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
153 		ret = cbm(cpu, node);
154 		trace_cpuhp_exit(cpu, st->state, state, ret);
155 		return ret;
156 	}
157 
158 	/* State transition. Invoke on all instances */
159 	cnt = 0;
160 	hlist_for_each(node, &step->list) {
161 		trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
162 		ret = cbm(cpu, node);
163 		trace_cpuhp_exit(cpu, st->state, state, ret);
164 		if (ret)
165 			goto err;
166 		cnt++;
167 	}
168 	return 0;
169 err:
170 	/* Rollback the instances if one failed */
171 	cbm = !bringup ? step->startup.multi : step->teardown.multi;
172 	if (!cbm)
173 		return ret;
174 
175 	hlist_for_each(node, &step->list) {
176 		if (!cnt--)
177 			break;
178 		cbm(cpu, node);
179 	}
180 	return ret;
181 }
182 
183 #ifdef CONFIG_SMP
184 /* Serializes the updates to cpu_online_mask, cpu_present_mask */
185 static DEFINE_MUTEX(cpu_add_remove_lock);
186 bool cpuhp_tasks_frozen;
187 EXPORT_SYMBOL_GPL(cpuhp_tasks_frozen);
188 
189 /*
190  * The following two APIs (cpu_maps_update_begin/done) must be used when
191  * attempting to serialize the updates to cpu_online_mask & cpu_present_mask.
192  * The APIs cpu_notifier_register_begin/done() must be used to protect CPU
193  * hotplug callback (un)registration performed using __register_cpu_notifier()
194  * or __unregister_cpu_notifier().
195  */
cpu_maps_update_begin(void)196 void cpu_maps_update_begin(void)
197 {
198 	mutex_lock(&cpu_add_remove_lock);
199 }
200 EXPORT_SYMBOL(cpu_notifier_register_begin);
201 
cpu_maps_update_done(void)202 void cpu_maps_update_done(void)
203 {
204 	mutex_unlock(&cpu_add_remove_lock);
205 }
206 EXPORT_SYMBOL(cpu_notifier_register_done);
207 
208 static RAW_NOTIFIER_HEAD(cpu_chain);
209 
210 /* If set, cpu_up and cpu_down will return -EBUSY and do nothing.
211  * Should always be manipulated under cpu_add_remove_lock
212  */
213 static int cpu_hotplug_disabled;
214 
215 #ifdef CONFIG_HOTPLUG_CPU
216 
217 static struct {
218 	struct task_struct *active_writer;
219 	/* wait queue to wake up the active_writer */
220 	wait_queue_head_t wq;
221 	/* verifies that no writer will get active while readers are active */
222 	struct mutex lock;
223 	/*
224 	 * Also blocks the new readers during
225 	 * an ongoing cpu hotplug operation.
226 	 */
227 	atomic_t refcount;
228 
229 #ifdef CONFIG_DEBUG_LOCK_ALLOC
230 	struct lockdep_map dep_map;
231 #endif
232 } cpu_hotplug = {
233 	.active_writer = NULL,
234 	.wq = __WAIT_QUEUE_HEAD_INITIALIZER(cpu_hotplug.wq),
235 	.lock = __MUTEX_INITIALIZER(cpu_hotplug.lock),
236 #ifdef CONFIG_DEBUG_LOCK_ALLOC
237 	.dep_map = STATIC_LOCKDEP_MAP_INIT("cpu_hotplug.dep_map", &cpu_hotplug.dep_map),
238 #endif
239 };
240 
241 /* Lockdep annotations for get/put_online_cpus() and cpu_hotplug_begin/end() */
242 #define cpuhp_lock_acquire_read() lock_map_acquire_read(&cpu_hotplug.dep_map)
243 #define cpuhp_lock_acquire_tryread() \
244 				  lock_map_acquire_tryread(&cpu_hotplug.dep_map)
245 #define cpuhp_lock_acquire()      lock_map_acquire(&cpu_hotplug.dep_map)
246 #define cpuhp_lock_release()      lock_map_release(&cpu_hotplug.dep_map)
247 
248 
get_online_cpus(void)249 void get_online_cpus(void)
250 {
251 	might_sleep();
252 	if (cpu_hotplug.active_writer == current)
253 		return;
254 	cpuhp_lock_acquire_read();
255 	mutex_lock(&cpu_hotplug.lock);
256 	atomic_inc(&cpu_hotplug.refcount);
257 	mutex_unlock(&cpu_hotplug.lock);
258 }
259 EXPORT_SYMBOL_GPL(get_online_cpus);
260 
put_online_cpus(void)261 void put_online_cpus(void)
262 {
263 	int refcount;
264 
265 	if (cpu_hotplug.active_writer == current)
266 		return;
267 
268 	refcount = atomic_dec_return(&cpu_hotplug.refcount);
269 	if (WARN_ON(refcount < 0)) /* try to fix things up */
270 		atomic_inc(&cpu_hotplug.refcount);
271 
272 	if (refcount <= 0 && waitqueue_active(&cpu_hotplug.wq))
273 		wake_up(&cpu_hotplug.wq);
274 
275 	cpuhp_lock_release();
276 
277 }
278 EXPORT_SYMBOL_GPL(put_online_cpus);
279 
280 /*
281  * This ensures that the hotplug operation can begin only when the
282  * refcount goes to zero.
283  *
284  * Note that during a cpu-hotplug operation, the new readers, if any,
285  * will be blocked by the cpu_hotplug.lock
286  *
287  * Since cpu_hotplug_begin() is always called after invoking
288  * cpu_maps_update_begin(), we can be sure that only one writer is active.
289  *
290  * Note that theoretically, there is a possibility of a livelock:
291  * - Refcount goes to zero, last reader wakes up the sleeping
292  *   writer.
293  * - Last reader unlocks the cpu_hotplug.lock.
294  * - A new reader arrives at this moment, bumps up the refcount.
295  * - The writer acquires the cpu_hotplug.lock finds the refcount
296  *   non zero and goes to sleep again.
297  *
298  * However, this is very difficult to achieve in practice since
299  * get_online_cpus() not an api which is called all that often.
300  *
301  */
cpu_hotplug_begin(void)302 void cpu_hotplug_begin(void)
303 {
304 	DEFINE_WAIT(wait);
305 
306 	cpu_hotplug.active_writer = current;
307 	cpuhp_lock_acquire();
308 
309 	for (;;) {
310 		mutex_lock(&cpu_hotplug.lock);
311 		prepare_to_wait(&cpu_hotplug.wq, &wait, TASK_UNINTERRUPTIBLE);
312 		if (likely(!atomic_read(&cpu_hotplug.refcount)))
313 				break;
314 		mutex_unlock(&cpu_hotplug.lock);
315 		schedule();
316 	}
317 	finish_wait(&cpu_hotplug.wq, &wait);
318 }
319 
cpu_hotplug_done(void)320 void cpu_hotplug_done(void)
321 {
322 	cpu_hotplug.active_writer = NULL;
323 	mutex_unlock(&cpu_hotplug.lock);
324 	cpuhp_lock_release();
325 }
326 
327 /*
328  * Wait for currently running CPU hotplug operations to complete (if any) and
329  * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
330  * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the
331  * hotplug path before performing hotplug operations. So acquiring that lock
332  * guarantees mutual exclusion from any currently running hotplug operations.
333  */
cpu_hotplug_disable(void)334 void cpu_hotplug_disable(void)
335 {
336 	cpu_maps_update_begin();
337 	cpu_hotplug_disabled++;
338 	cpu_maps_update_done();
339 }
340 EXPORT_SYMBOL_GPL(cpu_hotplug_disable);
341 
__cpu_hotplug_enable(void)342 static void __cpu_hotplug_enable(void)
343 {
344 	if (WARN_ONCE(!cpu_hotplug_disabled, "Unbalanced cpu hotplug enable\n"))
345 		return;
346 	cpu_hotplug_disabled--;
347 }
348 
cpu_hotplug_enable(void)349 void cpu_hotplug_enable(void)
350 {
351 	cpu_maps_update_begin();
352 	__cpu_hotplug_enable();
353 	cpu_maps_update_done();
354 }
355 EXPORT_SYMBOL_GPL(cpu_hotplug_enable);
356 #endif	/* CONFIG_HOTPLUG_CPU */
357 
358 /* Need to know about CPUs going up/down? */
register_cpu_notifier(struct notifier_block * nb)359 int register_cpu_notifier(struct notifier_block *nb)
360 {
361 	int ret;
362 	cpu_maps_update_begin();
363 	ret = raw_notifier_chain_register(&cpu_chain, nb);
364 	cpu_maps_update_done();
365 	return ret;
366 }
367 
__register_cpu_notifier(struct notifier_block * nb)368 int __register_cpu_notifier(struct notifier_block *nb)
369 {
370 	return raw_notifier_chain_register(&cpu_chain, nb);
371 }
372 
__cpu_notify(unsigned long val,unsigned int cpu,int nr_to_call,int * nr_calls)373 static int __cpu_notify(unsigned long val, unsigned int cpu, int nr_to_call,
374 			int *nr_calls)
375 {
376 	unsigned long mod = cpuhp_tasks_frozen ? CPU_TASKS_FROZEN : 0;
377 	void *hcpu = (void *)(long)cpu;
378 
379 	int ret;
380 
381 	ret = __raw_notifier_call_chain(&cpu_chain, val | mod, hcpu, nr_to_call,
382 					nr_calls);
383 
384 	return notifier_to_errno(ret);
385 }
386 
cpu_notify(unsigned long val,unsigned int cpu)387 static int cpu_notify(unsigned long val, unsigned int cpu)
388 {
389 	return __cpu_notify(val, cpu, -1, NULL);
390 }
391 
cpu_notify_nofail(unsigned long val,unsigned int cpu)392 static void cpu_notify_nofail(unsigned long val, unsigned int cpu)
393 {
394 	BUG_ON(cpu_notify(val, cpu));
395 }
396 
397 /* Notifier wrappers for transitioning to state machine */
notify_prepare(unsigned int cpu)398 static int notify_prepare(unsigned int cpu)
399 {
400 	int nr_calls = 0;
401 	int ret;
402 
403 	ret = __cpu_notify(CPU_UP_PREPARE, cpu, -1, &nr_calls);
404 	if (ret) {
405 		nr_calls--;
406 		printk(KERN_WARNING "%s: attempt to bring up CPU %u failed\n",
407 				__func__, cpu);
408 		__cpu_notify(CPU_UP_CANCELED, cpu, nr_calls, NULL);
409 	}
410 	return ret;
411 }
412 
notify_online(unsigned int cpu)413 static int notify_online(unsigned int cpu)
414 {
415 	cpu_notify(CPU_ONLINE, cpu);
416 	return 0;
417 }
418 
419 static void __cpuhp_kick_ap_work(struct cpuhp_cpu_state *st);
420 
bringup_wait_for_ap(unsigned int cpu)421 static int bringup_wait_for_ap(unsigned int cpu)
422 {
423 	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
424 
425 	/* Wait for the CPU to reach CPUHP_AP_ONLINE_IDLE */
426 	wait_for_completion(&st->done);
427 	if (WARN_ON_ONCE((!cpu_online(cpu))))
428 		return -ECANCELED;
429 
430 	/* Unpark the stopper thread and the hotplug thread of the target cpu */
431 	stop_machine_unpark(cpu);
432 	kthread_unpark(st->thread);
433 
434 	/* Should we go further up ? */
435 	if (st->target > CPUHP_AP_ONLINE_IDLE) {
436 		__cpuhp_kick_ap_work(st);
437 		wait_for_completion(&st->done);
438 	}
439 	return st->result;
440 }
441 
bringup_cpu(unsigned int cpu)442 static int bringup_cpu(unsigned int cpu)
443 {
444 	struct task_struct *idle = idle_thread_get(cpu);
445 	int ret;
446 
447 	/*
448 	 * Some architectures have to walk the irq descriptors to
449 	 * setup the vector space for the cpu which comes online.
450 	 * Prevent irq alloc/free across the bringup.
451 	 */
452 	irq_lock_sparse();
453 
454 	/* Arch-specific enabling code. */
455 	ret = __cpu_up(cpu, idle);
456 	irq_unlock_sparse();
457 	if (ret) {
458 		cpu_notify(CPU_UP_CANCELED, cpu);
459 		return ret;
460 	}
461 	return bringup_wait_for_ap(cpu);
462 }
463 
464 /*
465  * Hotplug state machine related functions
466  */
undo_cpu_down(unsigned int cpu,struct cpuhp_cpu_state * st)467 static void undo_cpu_down(unsigned int cpu, struct cpuhp_cpu_state *st)
468 {
469 	for (st->state++; st->state < st->target; st->state++) {
470 		struct cpuhp_step *step = cpuhp_get_step(st->state);
471 
472 		if (!step->skip_onerr)
473 			cpuhp_invoke_callback(cpu, st->state, true, NULL);
474 	}
475 }
476 
cpuhp_down_callbacks(unsigned int cpu,struct cpuhp_cpu_state * st,enum cpuhp_state target)477 static int cpuhp_down_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
478 				enum cpuhp_state target)
479 {
480 	enum cpuhp_state prev_state = st->state;
481 	int ret = 0;
482 
483 	for (; st->state > target; st->state--) {
484 		ret = cpuhp_invoke_callback(cpu, st->state, false, NULL);
485 		if (ret) {
486 			st->target = prev_state;
487 			undo_cpu_down(cpu, st);
488 			break;
489 		}
490 	}
491 	return ret;
492 }
493 
undo_cpu_up(unsigned int cpu,struct cpuhp_cpu_state * st)494 static void undo_cpu_up(unsigned int cpu, struct cpuhp_cpu_state *st)
495 {
496 	for (st->state--; st->state > st->target; st->state--) {
497 		struct cpuhp_step *step = cpuhp_get_step(st->state);
498 
499 		if (!step->skip_onerr)
500 			cpuhp_invoke_callback(cpu, st->state, false, NULL);
501 	}
502 }
503 
cpuhp_up_callbacks(unsigned int cpu,struct cpuhp_cpu_state * st,enum cpuhp_state target)504 static int cpuhp_up_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
505 			      enum cpuhp_state target)
506 {
507 	enum cpuhp_state prev_state = st->state;
508 	int ret = 0;
509 
510 	while (st->state < target) {
511 		st->state++;
512 		ret = cpuhp_invoke_callback(cpu, st->state, true, NULL);
513 		if (ret) {
514 			st->target = prev_state;
515 			undo_cpu_up(cpu, st);
516 			break;
517 		}
518 	}
519 	return ret;
520 }
521 
522 /*
523  * The cpu hotplug threads manage the bringup and teardown of the cpus
524  */
cpuhp_create(unsigned int cpu)525 static void cpuhp_create(unsigned int cpu)
526 {
527 	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
528 
529 	init_completion(&st->done);
530 }
531 
cpuhp_should_run(unsigned int cpu)532 static int cpuhp_should_run(unsigned int cpu)
533 {
534 	struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
535 
536 	return st->should_run;
537 }
538 
539 /* Execute the teardown callbacks. Used to be CPU_DOWN_PREPARE */
cpuhp_ap_offline(unsigned int cpu,struct cpuhp_cpu_state * st)540 static int cpuhp_ap_offline(unsigned int cpu, struct cpuhp_cpu_state *st)
541 {
542 	enum cpuhp_state target = max((int)st->target, CPUHP_TEARDOWN_CPU);
543 
544 	return cpuhp_down_callbacks(cpu, st, target);
545 }
546 
547 /* Execute the online startup callbacks. Used to be CPU_ONLINE */
cpuhp_ap_online(unsigned int cpu,struct cpuhp_cpu_state * st)548 static int cpuhp_ap_online(unsigned int cpu, struct cpuhp_cpu_state *st)
549 {
550 	return cpuhp_up_callbacks(cpu, st, st->target);
551 }
552 
553 /*
554  * Execute teardown/startup callbacks on the plugged cpu. Also used to invoke
555  * callbacks when a state gets [un]installed at runtime.
556  */
cpuhp_thread_fun(unsigned int cpu)557 static void cpuhp_thread_fun(unsigned int cpu)
558 {
559 	struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
560 	int ret = 0;
561 
562 	/*
563 	 * Paired with the mb() in cpuhp_kick_ap_work and
564 	 * cpuhp_invoke_ap_callback, so the work set is consistent visible.
565 	 */
566 	smp_mb();
567 	if (!st->should_run)
568 		return;
569 
570 	st->should_run = false;
571 
572 	lock_map_acquire(&cpuhp_state_lock_map);
573 	/* Single callback invocation for [un]install ? */
574 	if (st->single) {
575 		if (st->cb_state < CPUHP_AP_ONLINE) {
576 			local_irq_disable();
577 			ret = cpuhp_invoke_callback(cpu, st->cb_state,
578 						    st->bringup, st->node);
579 			local_irq_enable();
580 		} else {
581 			ret = cpuhp_invoke_callback(cpu, st->cb_state,
582 						    st->bringup, st->node);
583 		}
584 	} else if (st->rollback) {
585 		BUG_ON(st->state < CPUHP_AP_ONLINE_IDLE);
586 
587 		undo_cpu_down(cpu, st);
588 		/*
589 		 * This is a momentary workaround to keep the notifier users
590 		 * happy. Will go away once we got rid of the notifiers.
591 		 */
592 		cpu_notify_nofail(CPU_DOWN_FAILED, cpu);
593 		st->rollback = false;
594 	} else {
595 		/* Cannot happen .... */
596 		BUG_ON(st->state < CPUHP_AP_ONLINE_IDLE);
597 
598 		/* Regular hotplug work */
599 		if (st->state < st->target)
600 			ret = cpuhp_ap_online(cpu, st);
601 		else if (st->state > st->target)
602 			ret = cpuhp_ap_offline(cpu, st);
603 	}
604 	lock_map_release(&cpuhp_state_lock_map);
605 	st->result = ret;
606 	complete(&st->done);
607 }
608 
609 /* Invoke a single callback on a remote cpu */
610 static int
cpuhp_invoke_ap_callback(int cpu,enum cpuhp_state state,bool bringup,struct hlist_node * node)611 cpuhp_invoke_ap_callback(int cpu, enum cpuhp_state state, bool bringup,
612 			 struct hlist_node *node)
613 {
614 	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
615 
616 	if (!cpu_online(cpu))
617 		return 0;
618 
619 	lock_map_acquire(&cpuhp_state_lock_map);
620 	lock_map_release(&cpuhp_state_lock_map);
621 
622 	/*
623 	 * If we are up and running, use the hotplug thread. For early calls
624 	 * we invoke the thread function directly.
625 	 */
626 	if (!st->thread)
627 		return cpuhp_invoke_callback(cpu, state, bringup, node);
628 
629 	st->cb_state = state;
630 	st->single = true;
631 	st->bringup = bringup;
632 	st->node = node;
633 
634 	/*
635 	 * Make sure the above stores are visible before should_run becomes
636 	 * true. Paired with the mb() above in cpuhp_thread_fun()
637 	 */
638 	smp_mb();
639 	st->should_run = true;
640 	wake_up_process(st->thread);
641 	wait_for_completion(&st->done);
642 	return st->result;
643 }
644 
645 /* Regular hotplug invocation of the AP hotplug thread */
__cpuhp_kick_ap_work(struct cpuhp_cpu_state * st)646 static void __cpuhp_kick_ap_work(struct cpuhp_cpu_state *st)
647 {
648 	st->result = 0;
649 	st->single = false;
650 	/*
651 	 * Make sure the above stores are visible before should_run becomes
652 	 * true. Paired with the mb() above in cpuhp_thread_fun()
653 	 */
654 	smp_mb();
655 	st->should_run = true;
656 	wake_up_process(st->thread);
657 }
658 
cpuhp_kick_ap_work(unsigned int cpu)659 static int cpuhp_kick_ap_work(unsigned int cpu)
660 {
661 	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
662 	enum cpuhp_state state = st->state;
663 
664 	trace_cpuhp_enter(cpu, st->target, state, cpuhp_kick_ap_work);
665 	lock_map_acquire(&cpuhp_state_lock_map);
666 	lock_map_release(&cpuhp_state_lock_map);
667 	__cpuhp_kick_ap_work(st);
668 	wait_for_completion(&st->done);
669 	trace_cpuhp_exit(cpu, st->state, state, st->result);
670 	return st->result;
671 }
672 
673 static struct smp_hotplug_thread cpuhp_threads = {
674 	.store			= &cpuhp_state.thread,
675 	.create			= &cpuhp_create,
676 	.thread_should_run	= cpuhp_should_run,
677 	.thread_fn		= cpuhp_thread_fun,
678 	.thread_comm		= "cpuhp/%u",
679 	.selfparking		= true,
680 };
681 
cpuhp_threads_init(void)682 void __init cpuhp_threads_init(void)
683 {
684 	BUG_ON(smpboot_register_percpu_thread(&cpuhp_threads));
685 	kthread_unpark(this_cpu_read(cpuhp_state.thread));
686 }
687 
688 EXPORT_SYMBOL(register_cpu_notifier);
689 EXPORT_SYMBOL(__register_cpu_notifier);
unregister_cpu_notifier(struct notifier_block * nb)690 void unregister_cpu_notifier(struct notifier_block *nb)
691 {
692 	cpu_maps_update_begin();
693 	raw_notifier_chain_unregister(&cpu_chain, nb);
694 	cpu_maps_update_done();
695 }
696 EXPORT_SYMBOL(unregister_cpu_notifier);
697 
__unregister_cpu_notifier(struct notifier_block * nb)698 void __unregister_cpu_notifier(struct notifier_block *nb)
699 {
700 	raw_notifier_chain_unregister(&cpu_chain, nb);
701 }
702 EXPORT_SYMBOL(__unregister_cpu_notifier);
703 
704 #ifdef CONFIG_HOTPLUG_CPU
705 /**
706  * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU
707  * @cpu: a CPU id
708  *
709  * This function walks all processes, finds a valid mm struct for each one and
710  * then clears a corresponding bit in mm's cpumask.  While this all sounds
711  * trivial, there are various non-obvious corner cases, which this function
712  * tries to solve in a safe manner.
713  *
714  * Also note that the function uses a somewhat relaxed locking scheme, so it may
715  * be called only for an already offlined CPU.
716  */
clear_tasks_mm_cpumask(int cpu)717 void clear_tasks_mm_cpumask(int cpu)
718 {
719 	struct task_struct *p;
720 
721 	/*
722 	 * This function is called after the cpu is taken down and marked
723 	 * offline, so its not like new tasks will ever get this cpu set in
724 	 * their mm mask. -- Peter Zijlstra
725 	 * Thus, we may use rcu_read_lock() here, instead of grabbing
726 	 * full-fledged tasklist_lock.
727 	 */
728 	WARN_ON(cpu_online(cpu));
729 	rcu_read_lock();
730 	for_each_process(p) {
731 		struct task_struct *t;
732 
733 		/*
734 		 * Main thread might exit, but other threads may still have
735 		 * a valid mm. Find one.
736 		 */
737 		t = find_lock_task_mm(p);
738 		if (!t)
739 			continue;
740 		cpumask_clear_cpu(cpu, mm_cpumask(t->mm));
741 		task_unlock(t);
742 	}
743 	rcu_read_unlock();
744 }
745 
check_for_tasks(int dead_cpu)746 static inline void check_for_tasks(int dead_cpu)
747 {
748 	struct task_struct *g, *p;
749 
750 	read_lock(&tasklist_lock);
751 	for_each_process_thread(g, p) {
752 		if (!p->on_rq)
753 			continue;
754 		/*
755 		 * We do the check with unlocked task_rq(p)->lock.
756 		 * Order the reading to do not warn about a task,
757 		 * which was running on this cpu in the past, and
758 		 * it's just been woken on another cpu.
759 		 */
760 		rmb();
761 		if (task_cpu(p) != dead_cpu)
762 			continue;
763 
764 		pr_warn("Task %s (pid=%d) is on cpu %d (state=%ld, flags=%x)\n",
765 			p->comm, task_pid_nr(p), dead_cpu, p->state, p->flags);
766 	}
767 	read_unlock(&tasklist_lock);
768 }
769 
notify_down_prepare(unsigned int cpu)770 static int notify_down_prepare(unsigned int cpu)
771 {
772 	int err, nr_calls = 0;
773 
774 	err = __cpu_notify(CPU_DOWN_PREPARE, cpu, -1, &nr_calls);
775 	if (err) {
776 		nr_calls--;
777 		__cpu_notify(CPU_DOWN_FAILED, cpu, nr_calls, NULL);
778 		pr_warn("%s: attempt to take down CPU %u failed\n",
779 				__func__, cpu);
780 	}
781 	return err;
782 }
783 
784 /* Take this CPU down. */
take_cpu_down(void * _param)785 static int take_cpu_down(void *_param)
786 {
787 	struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
788 	enum cpuhp_state target = max((int)st->target, CPUHP_AP_OFFLINE);
789 	int err, cpu = smp_processor_id();
790 
791 	/* Ensure this CPU doesn't handle any more interrupts. */
792 	err = __cpu_disable();
793 	if (err < 0)
794 		return err;
795 
796 	/*
797 	 * We get here while we are in CPUHP_TEARDOWN_CPU state and we must not
798 	 * do this step again.
799 	 */
800 	WARN_ON(st->state != CPUHP_TEARDOWN_CPU);
801 	st->state--;
802 	/* Invoke the former CPU_DYING callbacks */
803 	for (; st->state > target; st->state--)
804 		cpuhp_invoke_callback(cpu, st->state, false, NULL);
805 
806 	/* Give up timekeeping duties */
807 	tick_handover_do_timer();
808 	/* Park the stopper thread */
809 	stop_machine_park(cpu);
810 	return 0;
811 }
812 
takedown_cpu(unsigned int cpu)813 static int takedown_cpu(unsigned int cpu)
814 {
815 	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
816 	int err;
817 
818 	/* Park the smpboot threads */
819 	kthread_park(per_cpu_ptr(&cpuhp_state, cpu)->thread);
820 	smpboot_park_threads(cpu);
821 
822 	/*
823 	 * Prevent irq alloc/free while the dying cpu reorganizes the
824 	 * interrupt affinities.
825 	 */
826 	irq_lock_sparse();
827 
828 	/*
829 	 * So now all preempt/rcu users must observe !cpu_active().
830 	 */
831 	err = stop_machine(take_cpu_down, NULL, cpumask_of(cpu));
832 	if (err) {
833 		/* CPU refused to die */
834 		irq_unlock_sparse();
835 		/* Unpark the hotplug thread so we can rollback there */
836 		kthread_unpark(per_cpu_ptr(&cpuhp_state, cpu)->thread);
837 		return err;
838 	}
839 	BUG_ON(cpu_online(cpu));
840 
841 	/*
842 	 * The CPUHP_AP_SCHED_MIGRATE_DYING callback will have removed all
843 	 * runnable tasks from the cpu, there's only the idle task left now
844 	 * that the migration thread is done doing the stop_machine thing.
845 	 *
846 	 * Wait for the stop thread to go away.
847 	 */
848 	wait_for_completion(&st->done);
849 	BUG_ON(st->state != CPUHP_AP_IDLE_DEAD);
850 
851 	/* Interrupts are moved away from the dying cpu, reenable alloc/free */
852 	irq_unlock_sparse();
853 
854 	hotplug_cpu__broadcast_tick_pull(cpu);
855 	/* This actually kills the CPU. */
856 	__cpu_die(cpu);
857 
858 	tick_cleanup_dead_cpu(cpu);
859 	return 0;
860 }
861 
notify_dead(unsigned int cpu)862 static int notify_dead(unsigned int cpu)
863 {
864 	cpu_notify_nofail(CPU_DEAD, cpu);
865 	check_for_tasks(cpu);
866 	return 0;
867 }
868 
cpuhp_complete_idle_dead(void * arg)869 static void cpuhp_complete_idle_dead(void *arg)
870 {
871 	struct cpuhp_cpu_state *st = arg;
872 
873 	complete(&st->done);
874 }
875 
cpuhp_report_idle_dead(void)876 void cpuhp_report_idle_dead(void)
877 {
878 	struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
879 
880 	BUG_ON(st->state != CPUHP_AP_OFFLINE);
881 	rcu_report_dead(smp_processor_id());
882 	st->state = CPUHP_AP_IDLE_DEAD;
883 	/*
884 	 * We cannot call complete after rcu_report_dead() so we delegate it
885 	 * to an online cpu.
886 	 */
887 	smp_call_function_single(cpumask_first(cpu_online_mask),
888 				 cpuhp_complete_idle_dead, st, 0);
889 }
890 
891 #else
892 #define notify_down_prepare	NULL
893 #define takedown_cpu		NULL
894 #define notify_dead		NULL
895 #endif
896 
897 #ifdef CONFIG_HOTPLUG_CPU
898 
899 /* Requires cpu_add_remove_lock to be held */
_cpu_down(unsigned int cpu,int tasks_frozen,enum cpuhp_state target)900 static int __ref _cpu_down(unsigned int cpu, int tasks_frozen,
901 			   enum cpuhp_state target)
902 {
903 	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
904 	int prev_state, ret = 0;
905 	bool hasdied = false;
906 
907 	if (num_online_cpus() == 1)
908 		return -EBUSY;
909 
910 	if (!cpu_present(cpu))
911 		return -EINVAL;
912 
913 	cpu_hotplug_begin();
914 
915 	cpuhp_tasks_frozen = tasks_frozen;
916 
917 	prev_state = st->state;
918 	st->target = target;
919 	/*
920 	 * If the current CPU state is in the range of the AP hotplug thread,
921 	 * then we need to kick the thread.
922 	 */
923 	if (st->state > CPUHP_TEARDOWN_CPU) {
924 		ret = cpuhp_kick_ap_work(cpu);
925 		/*
926 		 * The AP side has done the error rollback already. Just
927 		 * return the error code..
928 		 */
929 		if (ret)
930 			goto out;
931 
932 		/*
933 		 * We might have stopped still in the range of the AP hotplug
934 		 * thread. Nothing to do anymore.
935 		 */
936 		if (st->state > CPUHP_TEARDOWN_CPU)
937 			goto out;
938 	}
939 	/*
940 	 * The AP brought itself down to CPUHP_TEARDOWN_CPU. So we need
941 	 * to do the further cleanups.
942 	 */
943 	ret = cpuhp_down_callbacks(cpu, st, target);
944 	if (ret && st->state > CPUHP_TEARDOWN_CPU && st->state < prev_state) {
945 		st->target = prev_state;
946 		st->rollback = true;
947 		cpuhp_kick_ap_work(cpu);
948 	}
949 
950 	hasdied = prev_state != st->state && st->state == CPUHP_OFFLINE;
951 out:
952 	cpu_hotplug_done();
953 	/* This post dead nonsense must die */
954 	if (!ret && hasdied)
955 		cpu_notify_nofail(CPU_POST_DEAD, cpu);
956 	return ret;
957 }
958 
do_cpu_down(unsigned int cpu,enum cpuhp_state target)959 static int do_cpu_down(unsigned int cpu, enum cpuhp_state target)
960 {
961 	int err;
962 
963 	cpu_maps_update_begin();
964 
965 	if (cpu_hotplug_disabled) {
966 		err = -EBUSY;
967 		goto out;
968 	}
969 
970 	err = _cpu_down(cpu, 0, target);
971 
972 out:
973 	cpu_maps_update_done();
974 	return err;
975 }
cpu_down(unsigned int cpu)976 int cpu_down(unsigned int cpu)
977 {
978 	return do_cpu_down(cpu, CPUHP_OFFLINE);
979 }
980 EXPORT_SYMBOL(cpu_down);
981 #endif /*CONFIG_HOTPLUG_CPU*/
982 
983 /**
984  * notify_cpu_starting(cpu) - Invoke the callbacks on the starting CPU
985  * @cpu: cpu that just started
986  *
987  * It must be called by the arch code on the new cpu, before the new cpu
988  * enables interrupts and before the "boot" cpu returns from __cpu_up().
989  */
notify_cpu_starting(unsigned int cpu)990 void notify_cpu_starting(unsigned int cpu)
991 {
992 	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
993 	enum cpuhp_state target = min((int)st->target, CPUHP_AP_ONLINE);
994 
995 	rcu_cpu_starting(cpu);	/* Enables RCU usage on this CPU. */
996 	while (st->state < target) {
997 		st->state++;
998 		cpuhp_invoke_callback(cpu, st->state, true, NULL);
999 	}
1000 }
1001 
1002 /*
1003  * Called from the idle task. Wake up the controlling task which brings the
1004  * stopper and the hotplug thread of the upcoming CPU up and then delegates
1005  * the rest of the online bringup to the hotplug thread.
1006  */
cpuhp_online_idle(enum cpuhp_state state)1007 void cpuhp_online_idle(enum cpuhp_state state)
1008 {
1009 	struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
1010 
1011 	/* Happens for the boot cpu */
1012 	if (state != CPUHP_AP_ONLINE_IDLE)
1013 		return;
1014 
1015 	st->state = CPUHP_AP_ONLINE_IDLE;
1016 	complete(&st->done);
1017 }
1018 
1019 /* Requires cpu_add_remove_lock to be held */
_cpu_up(unsigned int cpu,int tasks_frozen,enum cpuhp_state target)1020 static int _cpu_up(unsigned int cpu, int tasks_frozen, enum cpuhp_state target)
1021 {
1022 	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1023 	struct task_struct *idle;
1024 	int ret = 0;
1025 
1026 	cpu_hotplug_begin();
1027 
1028 	if (!cpu_present(cpu)) {
1029 		ret = -EINVAL;
1030 		goto out;
1031 	}
1032 
1033 	/*
1034 	 * The caller of do_cpu_up might have raced with another
1035 	 * caller. Ignore it for now.
1036 	 */
1037 	if (st->state >= target)
1038 		goto out;
1039 
1040 	if (st->state == CPUHP_OFFLINE) {
1041 		/* Let it fail before we try to bring the cpu up */
1042 		idle = idle_thread_get(cpu);
1043 		if (IS_ERR(idle)) {
1044 			ret = PTR_ERR(idle);
1045 			goto out;
1046 		}
1047 	}
1048 
1049 	cpuhp_tasks_frozen = tasks_frozen;
1050 
1051 	st->target = target;
1052 	/*
1053 	 * If the current CPU state is in the range of the AP hotplug thread,
1054 	 * then we need to kick the thread once more.
1055 	 */
1056 	if (st->state > CPUHP_BRINGUP_CPU) {
1057 		ret = cpuhp_kick_ap_work(cpu);
1058 		/*
1059 		 * The AP side has done the error rollback already. Just
1060 		 * return the error code..
1061 		 */
1062 		if (ret)
1063 			goto out;
1064 	}
1065 
1066 	/*
1067 	 * Try to reach the target state. We max out on the BP at
1068 	 * CPUHP_BRINGUP_CPU. After that the AP hotplug thread is
1069 	 * responsible for bringing it up to the target state.
1070 	 */
1071 	target = min((int)target, CPUHP_BRINGUP_CPU);
1072 	ret = cpuhp_up_callbacks(cpu, st, target);
1073 out:
1074 	cpu_hotplug_done();
1075 	return ret;
1076 }
1077 
do_cpu_up(unsigned int cpu,enum cpuhp_state target)1078 static int do_cpu_up(unsigned int cpu, enum cpuhp_state target)
1079 {
1080 	int err = 0;
1081 
1082 	if (!cpu_possible(cpu)) {
1083 		pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n",
1084 		       cpu);
1085 #if defined(CONFIG_IA64)
1086 		pr_err("please check additional_cpus= boot parameter\n");
1087 #endif
1088 		return -EINVAL;
1089 	}
1090 
1091 	err = try_online_node(cpu_to_node(cpu));
1092 	if (err)
1093 		return err;
1094 
1095 	cpu_maps_update_begin();
1096 
1097 	if (cpu_hotplug_disabled) {
1098 		err = -EBUSY;
1099 		goto out;
1100 	}
1101 
1102 	err = _cpu_up(cpu, 0, target);
1103 out:
1104 	cpu_maps_update_done();
1105 	return err;
1106 }
1107 
cpu_up(unsigned int cpu)1108 int cpu_up(unsigned int cpu)
1109 {
1110 	return do_cpu_up(cpu, CPUHP_ONLINE);
1111 }
1112 EXPORT_SYMBOL_GPL(cpu_up);
1113 
1114 #ifdef CONFIG_PM_SLEEP_SMP
1115 static cpumask_var_t frozen_cpus;
1116 
freeze_secondary_cpus(int primary)1117 int freeze_secondary_cpus(int primary)
1118 {
1119 	int cpu, error = 0;
1120 
1121 	cpu_maps_update_begin();
1122 	if (!cpu_online(primary))
1123 		primary = cpumask_first(cpu_online_mask);
1124 	/*
1125 	 * We take down all of the non-boot CPUs in one shot to avoid races
1126 	 * with the userspace trying to use the CPU hotplug at the same time
1127 	 */
1128 	cpumask_clear(frozen_cpus);
1129 
1130 	pr_info("Disabling non-boot CPUs ...\n");
1131 	for_each_online_cpu(cpu) {
1132 		if (cpu == primary)
1133 			continue;
1134 		trace_suspend_resume(TPS("CPU_OFF"), cpu, true);
1135 		error = _cpu_down(cpu, 1, CPUHP_OFFLINE);
1136 		trace_suspend_resume(TPS("CPU_OFF"), cpu, false);
1137 		if (!error)
1138 			cpumask_set_cpu(cpu, frozen_cpus);
1139 		else {
1140 			pr_err("Error taking CPU%d down: %d\n", cpu, error);
1141 			break;
1142 		}
1143 	}
1144 
1145 	if (!error)
1146 		BUG_ON(num_online_cpus() > 1);
1147 	else
1148 		pr_err("Non-boot CPUs are not disabled\n");
1149 
1150 	/*
1151 	 * Make sure the CPUs won't be enabled by someone else. We need to do
1152 	 * this even in case of failure as all disable_nonboot_cpus() users are
1153 	 * supposed to do enable_nonboot_cpus() on the failure path.
1154 	 */
1155 	cpu_hotplug_disabled++;
1156 
1157 	cpu_maps_update_done();
1158 	return error;
1159 }
1160 
arch_enable_nonboot_cpus_begin(void)1161 void __weak arch_enable_nonboot_cpus_begin(void)
1162 {
1163 }
1164 
arch_enable_nonboot_cpus_end(void)1165 void __weak arch_enable_nonboot_cpus_end(void)
1166 {
1167 }
1168 
enable_nonboot_cpus(void)1169 void enable_nonboot_cpus(void)
1170 {
1171 	int cpu, error;
1172 	struct device *cpu_device;
1173 
1174 	/* Allow everyone to use the CPU hotplug again */
1175 	cpu_maps_update_begin();
1176 	__cpu_hotplug_enable();
1177 	if (cpumask_empty(frozen_cpus))
1178 		goto out;
1179 
1180 	pr_info("Enabling non-boot CPUs ...\n");
1181 
1182 	arch_enable_nonboot_cpus_begin();
1183 
1184 	for_each_cpu(cpu, frozen_cpus) {
1185 		trace_suspend_resume(TPS("CPU_ON"), cpu, true);
1186 		error = _cpu_up(cpu, 1, CPUHP_ONLINE);
1187 		trace_suspend_resume(TPS("CPU_ON"), cpu, false);
1188 		if (!error) {
1189 			pr_info("CPU%d is up\n", cpu);
1190 			cpu_device = get_cpu_device(cpu);
1191 			if (!cpu_device)
1192 				pr_err("%s: failed to get cpu%d device\n",
1193 				       __func__, cpu);
1194 			else
1195 				kobject_uevent(&cpu_device->kobj, KOBJ_ONLINE);
1196 			continue;
1197 		}
1198 		pr_warn("Error taking CPU%d up: %d\n", cpu, error);
1199 	}
1200 
1201 	arch_enable_nonboot_cpus_end();
1202 
1203 	cpumask_clear(frozen_cpus);
1204 out:
1205 	cpu_maps_update_done();
1206 }
1207 
alloc_frozen_cpus(void)1208 static int __init alloc_frozen_cpus(void)
1209 {
1210 	if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
1211 		return -ENOMEM;
1212 	return 0;
1213 }
1214 core_initcall(alloc_frozen_cpus);
1215 
1216 /*
1217  * When callbacks for CPU hotplug notifications are being executed, we must
1218  * ensure that the state of the system with respect to the tasks being frozen
1219  * or not, as reported by the notification, remains unchanged *throughout the
1220  * duration* of the execution of the callbacks.
1221  * Hence we need to prevent the freezer from racing with regular CPU hotplug.
1222  *
1223  * This synchronization is implemented by mutually excluding regular CPU
1224  * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
1225  * Hibernate notifications.
1226  */
1227 static int
cpu_hotplug_pm_callback(struct notifier_block * nb,unsigned long action,void * ptr)1228 cpu_hotplug_pm_callback(struct notifier_block *nb,
1229 			unsigned long action, void *ptr)
1230 {
1231 	switch (action) {
1232 
1233 	case PM_SUSPEND_PREPARE:
1234 	case PM_HIBERNATION_PREPARE:
1235 		cpu_hotplug_disable();
1236 		break;
1237 
1238 	case PM_POST_SUSPEND:
1239 	case PM_POST_HIBERNATION:
1240 		cpu_hotplug_enable();
1241 		break;
1242 
1243 	default:
1244 		return NOTIFY_DONE;
1245 	}
1246 
1247 	return NOTIFY_OK;
1248 }
1249 
1250 
cpu_hotplug_pm_sync_init(void)1251 static int __init cpu_hotplug_pm_sync_init(void)
1252 {
1253 	/*
1254 	 * cpu_hotplug_pm_callback has higher priority than x86
1255 	 * bsp_pm_callback which depends on cpu_hotplug_pm_callback
1256 	 * to disable cpu hotplug to avoid cpu hotplug race.
1257 	 */
1258 	pm_notifier(cpu_hotplug_pm_callback, 0);
1259 	return 0;
1260 }
1261 core_initcall(cpu_hotplug_pm_sync_init);
1262 
1263 #endif /* CONFIG_PM_SLEEP_SMP */
1264 
1265 #endif /* CONFIG_SMP */
1266 
1267 /* Boot processor state steps */
1268 static struct cpuhp_step cpuhp_bp_states[] = {
1269 	[CPUHP_OFFLINE] = {
1270 		.name			= "offline",
1271 		.startup.single		= NULL,
1272 		.teardown.single	= NULL,
1273 	},
1274 #ifdef CONFIG_SMP
1275 	[CPUHP_CREATE_THREADS]= {
1276 		.name			= "threads:prepare",
1277 		.startup.single		= smpboot_create_threads,
1278 		.teardown.single	= NULL,
1279 		.cant_stop		= true,
1280 	},
1281 	[CPUHP_PERF_PREPARE] = {
1282 		.name			= "perf:prepare",
1283 		.startup.single		= perf_event_init_cpu,
1284 		.teardown.single	= perf_event_exit_cpu,
1285 	},
1286 	[CPUHP_WORKQUEUE_PREP] = {
1287 		.name			= "workqueue:prepare",
1288 		.startup.single		= workqueue_prepare_cpu,
1289 		.teardown.single	= NULL,
1290 	},
1291 	[CPUHP_HRTIMERS_PREPARE] = {
1292 		.name			= "hrtimers:prepare",
1293 		.startup.single		= hrtimers_prepare_cpu,
1294 		.teardown.single	= hrtimers_dead_cpu,
1295 	},
1296 	[CPUHP_SMPCFD_PREPARE] = {
1297 		.name			= "smpcfd:prepare",
1298 		.startup.single		= smpcfd_prepare_cpu,
1299 		.teardown.single	= smpcfd_dead_cpu,
1300 	},
1301 	[CPUHP_RELAY_PREPARE] = {
1302 		.name			= "relay:prepare",
1303 		.startup.single		= relay_prepare_cpu,
1304 		.teardown.single	= NULL,
1305 	},
1306 	[CPUHP_SLAB_PREPARE] = {
1307 		.name			= "slab:prepare",
1308 		.startup.single		= slab_prepare_cpu,
1309 		.teardown.single	= slab_dead_cpu,
1310 	},
1311 	[CPUHP_RCUTREE_PREP] = {
1312 		.name			= "RCU/tree:prepare",
1313 		.startup.single		= rcutree_prepare_cpu,
1314 		.teardown.single	= rcutree_dead_cpu,
1315 	},
1316 	/*
1317 	 * Preparatory and dead notifiers. Will be replaced once the notifiers
1318 	 * are converted to states.
1319 	 */
1320 	[CPUHP_NOTIFY_PREPARE] = {
1321 		.name			= "notify:prepare",
1322 		.startup.single		= notify_prepare,
1323 		.teardown.single	= notify_dead,
1324 		.skip_onerr		= true,
1325 		.cant_stop		= true,
1326 	},
1327 	/*
1328 	 * On the tear-down path, timers_dead_cpu() must be invoked
1329 	 * before blk_mq_queue_reinit_notify() from notify_dead(),
1330 	 * otherwise a RCU stall occurs.
1331 	 */
1332 	[CPUHP_TIMERS_PREPARE] = {
1333 		.name			= "timers:dead",
1334 		.startup.single		= timers_prepare_cpu,
1335 		.teardown.single	= timers_dead_cpu,
1336 	},
1337 	/* Kicks the plugged cpu into life */
1338 	[CPUHP_BRINGUP_CPU] = {
1339 		.name			= "cpu:bringup",
1340 		.startup.single		= bringup_cpu,
1341 		.teardown.single	= NULL,
1342 		.cant_stop		= true,
1343 	},
1344 	/*
1345 	 * Handled on controll processor until the plugged processor manages
1346 	 * this itself.
1347 	 */
1348 	[CPUHP_TEARDOWN_CPU] = {
1349 		.name			= "cpu:teardown",
1350 		.startup.single		= NULL,
1351 		.teardown.single	= takedown_cpu,
1352 		.cant_stop		= true,
1353 	},
1354 #else
1355 	[CPUHP_BRINGUP_CPU] = { },
1356 #endif
1357 };
1358 
1359 /* Application processor state steps */
1360 static struct cpuhp_step cpuhp_ap_states[] = {
1361 #ifdef CONFIG_SMP
1362 	/* Final state before CPU kills itself */
1363 	[CPUHP_AP_IDLE_DEAD] = {
1364 		.name			= "idle:dead",
1365 	},
1366 	/*
1367 	 * Last state before CPU enters the idle loop to die. Transient state
1368 	 * for synchronization.
1369 	 */
1370 	[CPUHP_AP_OFFLINE] = {
1371 		.name			= "ap:offline",
1372 		.cant_stop		= true,
1373 	},
1374 	/* First state is scheduler control. Interrupts are disabled */
1375 	[CPUHP_AP_SCHED_STARTING] = {
1376 		.name			= "sched:starting",
1377 		.startup.single		= sched_cpu_starting,
1378 		.teardown.single	= sched_cpu_dying,
1379 	},
1380 	[CPUHP_AP_RCUTREE_DYING] = {
1381 		.name			= "RCU/tree:dying",
1382 		.startup.single		= NULL,
1383 		.teardown.single	= rcutree_dying_cpu,
1384 	},
1385 	[CPUHP_AP_SMPCFD_DYING] = {
1386 		.name			= "smpcfd:dying",
1387 		.startup.single		= NULL,
1388 		.teardown.single	= smpcfd_dying_cpu,
1389 	},
1390 	/* Entry state on starting. Interrupts enabled from here on. Transient
1391 	 * state for synchronsization */
1392 	[CPUHP_AP_ONLINE] = {
1393 		.name			= "ap:online",
1394 	},
1395 	/* Handle smpboot threads park/unpark */
1396 	[CPUHP_AP_SMPBOOT_THREADS] = {
1397 		.name			= "smpboot/threads:online",
1398 		.startup.single		= smpboot_unpark_threads,
1399 		.teardown.single	= NULL,
1400 	},
1401 	[CPUHP_AP_PERF_ONLINE] = {
1402 		.name			= "perf:online",
1403 		.startup.single		= perf_event_init_cpu,
1404 		.teardown.single	= perf_event_exit_cpu,
1405 	},
1406 	[CPUHP_AP_WORKQUEUE_ONLINE] = {
1407 		.name			= "workqueue:online",
1408 		.startup.single		= workqueue_online_cpu,
1409 		.teardown.single	= workqueue_offline_cpu,
1410 	},
1411 	[CPUHP_AP_RCUTREE_ONLINE] = {
1412 		.name			= "RCU/tree:online",
1413 		.startup.single		= rcutree_online_cpu,
1414 		.teardown.single	= rcutree_offline_cpu,
1415 	},
1416 
1417 	/*
1418 	 * Online/down_prepare notifiers. Will be removed once the notifiers
1419 	 * are converted to states.
1420 	 */
1421 	[CPUHP_AP_NOTIFY_ONLINE] = {
1422 		.name			= "notify:online",
1423 		.startup.single		= notify_online,
1424 		.teardown.single	= notify_down_prepare,
1425 		.skip_onerr		= true,
1426 	},
1427 #endif
1428 	/*
1429 	 * The dynamically registered state space is here
1430 	 */
1431 
1432 #ifdef CONFIG_SMP
1433 	/* Last state is scheduler control setting the cpu active */
1434 	[CPUHP_AP_ACTIVE] = {
1435 		.name			= "sched:active",
1436 		.startup.single		= sched_cpu_activate,
1437 		.teardown.single	= sched_cpu_deactivate,
1438 	},
1439 #endif
1440 
1441 	/* CPU is fully up and running. */
1442 	[CPUHP_ONLINE] = {
1443 		.name			= "online",
1444 		.startup.single		= NULL,
1445 		.teardown.single	= NULL,
1446 	},
1447 };
1448 
1449 /* Sanity check for callbacks */
cpuhp_cb_check(enum cpuhp_state state)1450 static int cpuhp_cb_check(enum cpuhp_state state)
1451 {
1452 	if (state <= CPUHP_OFFLINE || state >= CPUHP_ONLINE)
1453 		return -EINVAL;
1454 	return 0;
1455 }
1456 
cpuhp_store_callbacks(enum cpuhp_state state,const char * name,int (* startup)(unsigned int cpu),int (* teardown)(unsigned int cpu),bool multi_instance)1457 static void cpuhp_store_callbacks(enum cpuhp_state state,
1458 				  const char *name,
1459 				  int (*startup)(unsigned int cpu),
1460 				  int (*teardown)(unsigned int cpu),
1461 				  bool multi_instance)
1462 {
1463 	/* (Un)Install the callbacks for further cpu hotplug operations */
1464 	struct cpuhp_step *sp;
1465 
1466 	sp = cpuhp_get_step(state);
1467 	sp->startup.single = startup;
1468 	sp->teardown.single = teardown;
1469 	sp->name = name;
1470 	sp->multi_instance = multi_instance;
1471 	INIT_HLIST_HEAD(&sp->list);
1472 }
1473 
cpuhp_get_teardown_cb(enum cpuhp_state state)1474 static void *cpuhp_get_teardown_cb(enum cpuhp_state state)
1475 {
1476 	return cpuhp_get_step(state)->teardown.single;
1477 }
1478 
1479 /*
1480  * Call the startup/teardown function for a step either on the AP or
1481  * on the current CPU.
1482  */
cpuhp_issue_call(int cpu,enum cpuhp_state state,bool bringup,struct hlist_node * node)1483 static int cpuhp_issue_call(int cpu, enum cpuhp_state state, bool bringup,
1484 			    struct hlist_node *node)
1485 {
1486 	struct cpuhp_step *sp = cpuhp_get_step(state);
1487 	int ret;
1488 
1489 	if ((bringup && !sp->startup.single) ||
1490 	    (!bringup && !sp->teardown.single))
1491 		return 0;
1492 	/*
1493 	 * The non AP bound callbacks can fail on bringup. On teardown
1494 	 * e.g. module removal we crash for now.
1495 	 */
1496 #ifdef CONFIG_SMP
1497 	if (cpuhp_is_ap_state(state))
1498 		ret = cpuhp_invoke_ap_callback(cpu, state, bringup, node);
1499 	else
1500 		ret = cpuhp_invoke_callback(cpu, state, bringup, node);
1501 #else
1502 	ret = cpuhp_invoke_callback(cpu, state, bringup, node);
1503 #endif
1504 	BUG_ON(ret && !bringup);
1505 	return ret;
1506 }
1507 
1508 /*
1509  * Called from __cpuhp_setup_state on a recoverable failure.
1510  *
1511  * Note: The teardown callbacks for rollback are not allowed to fail!
1512  */
cpuhp_rollback_install(int failedcpu,enum cpuhp_state state,struct hlist_node * node)1513 static void cpuhp_rollback_install(int failedcpu, enum cpuhp_state state,
1514 				   struct hlist_node *node)
1515 {
1516 	int cpu;
1517 
1518 	/* Roll back the already executed steps on the other cpus */
1519 	for_each_present_cpu(cpu) {
1520 		struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1521 		int cpustate = st->state;
1522 
1523 		if (cpu >= failedcpu)
1524 			break;
1525 
1526 		/* Did we invoke the startup call on that cpu ? */
1527 		if (cpustate >= state)
1528 			cpuhp_issue_call(cpu, state, false, node);
1529 	}
1530 }
1531 
1532 /*
1533  * Returns a free for dynamic slot assignment of the Online state. The states
1534  * are protected by the cpuhp_slot_states mutex and an empty slot is identified
1535  * by having no name assigned.
1536  */
cpuhp_reserve_state(enum cpuhp_state state)1537 static int cpuhp_reserve_state(enum cpuhp_state state)
1538 {
1539 	enum cpuhp_state i;
1540 
1541 	for (i = CPUHP_AP_ONLINE_DYN; i <= CPUHP_AP_ONLINE_DYN_END; i++) {
1542 		if (cpuhp_ap_states[i].name)
1543 			continue;
1544 
1545 		cpuhp_ap_states[i].name = "Reserved";
1546 		return i;
1547 	}
1548 	WARN(1, "No more dynamic states available for CPU hotplug\n");
1549 	return -ENOSPC;
1550 }
1551 
__cpuhp_state_add_instance(enum cpuhp_state state,struct hlist_node * node,bool invoke)1552 int __cpuhp_state_add_instance(enum cpuhp_state state, struct hlist_node *node,
1553 			       bool invoke)
1554 {
1555 	struct cpuhp_step *sp;
1556 	int cpu;
1557 	int ret;
1558 
1559 	sp = cpuhp_get_step(state);
1560 	if (sp->multi_instance == false)
1561 		return -EINVAL;
1562 
1563 	get_online_cpus();
1564 	mutex_lock(&cpuhp_state_mutex);
1565 
1566 	if (!invoke || !sp->startup.multi)
1567 		goto add_node;
1568 
1569 	/*
1570 	 * Try to call the startup callback for each present cpu
1571 	 * depending on the hotplug state of the cpu.
1572 	 */
1573 	for_each_present_cpu(cpu) {
1574 		struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1575 		int cpustate = st->state;
1576 
1577 		if (cpustate < state)
1578 			continue;
1579 
1580 		ret = cpuhp_issue_call(cpu, state, true, node);
1581 		if (ret) {
1582 			if (sp->teardown.multi)
1583 				cpuhp_rollback_install(cpu, state, node);
1584 			goto err;
1585 		}
1586 	}
1587 add_node:
1588 	ret = 0;
1589 	hlist_add_head(node, &sp->list);
1590 
1591 err:
1592 	mutex_unlock(&cpuhp_state_mutex);
1593 	put_online_cpus();
1594 	return ret;
1595 }
1596 EXPORT_SYMBOL_GPL(__cpuhp_state_add_instance);
1597 
1598 /**
1599  * __cpuhp_setup_state - Setup the callbacks for an hotplug machine state
1600  * @state:	The state to setup
1601  * @invoke:	If true, the startup function is invoked for cpus where
1602  *		cpu state >= @state
1603  * @startup:	startup callback function
1604  * @teardown:	teardown callback function
1605  *
1606  * Returns 0 if successful, otherwise a proper error code
1607  */
__cpuhp_setup_state(enum cpuhp_state state,const char * name,bool invoke,int (* startup)(unsigned int cpu),int (* teardown)(unsigned int cpu),bool multi_instance)1608 int __cpuhp_setup_state(enum cpuhp_state state,
1609 			const char *name, bool invoke,
1610 			int (*startup)(unsigned int cpu),
1611 			int (*teardown)(unsigned int cpu),
1612 			bool multi_instance)
1613 {
1614 	int cpu, ret = 0;
1615 	int dyn_state = 0;
1616 
1617 	if (cpuhp_cb_check(state) || !name)
1618 		return -EINVAL;
1619 
1620 	get_online_cpus();
1621 	mutex_lock(&cpuhp_state_mutex);
1622 
1623 	/* currently assignments for the ONLINE state are possible */
1624 	if (state == CPUHP_AP_ONLINE_DYN) {
1625 		dyn_state = 1;
1626 		ret = cpuhp_reserve_state(state);
1627 		if (ret < 0)
1628 			goto out;
1629 		state = ret;
1630 	}
1631 
1632 	cpuhp_store_callbacks(state, name, startup, teardown, multi_instance);
1633 
1634 	if (!invoke || !startup)
1635 		goto out;
1636 
1637 	/*
1638 	 * Try to call the startup callback for each present cpu
1639 	 * depending on the hotplug state of the cpu.
1640 	 */
1641 	for_each_present_cpu(cpu) {
1642 		struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1643 		int cpustate = st->state;
1644 
1645 		if (cpustate < state)
1646 			continue;
1647 
1648 		ret = cpuhp_issue_call(cpu, state, true, NULL);
1649 		if (ret) {
1650 			if (teardown)
1651 				cpuhp_rollback_install(cpu, state, NULL);
1652 			cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
1653 			goto out;
1654 		}
1655 	}
1656 out:
1657 	mutex_unlock(&cpuhp_state_mutex);
1658 
1659 	put_online_cpus();
1660 	if (!ret && dyn_state)
1661 		return state;
1662 	return ret;
1663 }
1664 EXPORT_SYMBOL(__cpuhp_setup_state);
1665 
__cpuhp_state_remove_instance(enum cpuhp_state state,struct hlist_node * node,bool invoke)1666 int __cpuhp_state_remove_instance(enum cpuhp_state state,
1667 				  struct hlist_node *node, bool invoke)
1668 {
1669 	struct cpuhp_step *sp = cpuhp_get_step(state);
1670 	int cpu;
1671 
1672 	BUG_ON(cpuhp_cb_check(state));
1673 
1674 	if (!sp->multi_instance)
1675 		return -EINVAL;
1676 
1677 	get_online_cpus();
1678 	mutex_lock(&cpuhp_state_mutex);
1679 
1680 	if (!invoke || !cpuhp_get_teardown_cb(state))
1681 		goto remove;
1682 	/*
1683 	 * Call the teardown callback for each present cpu depending
1684 	 * on the hotplug state of the cpu. This function is not
1685 	 * allowed to fail currently!
1686 	 */
1687 	for_each_present_cpu(cpu) {
1688 		struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1689 		int cpustate = st->state;
1690 
1691 		if (cpustate >= state)
1692 			cpuhp_issue_call(cpu, state, false, node);
1693 	}
1694 
1695 remove:
1696 	hlist_del(node);
1697 	mutex_unlock(&cpuhp_state_mutex);
1698 	put_online_cpus();
1699 
1700 	return 0;
1701 }
1702 EXPORT_SYMBOL_GPL(__cpuhp_state_remove_instance);
1703 /**
1704  * __cpuhp_remove_state - Remove the callbacks for an hotplug machine state
1705  * @state:	The state to remove
1706  * @invoke:	If true, the teardown function is invoked for cpus where
1707  *		cpu state >= @state
1708  *
1709  * The teardown callback is currently not allowed to fail. Think
1710  * about module removal!
1711  */
__cpuhp_remove_state(enum cpuhp_state state,bool invoke)1712 void __cpuhp_remove_state(enum cpuhp_state state, bool invoke)
1713 {
1714 	struct cpuhp_step *sp = cpuhp_get_step(state);
1715 	int cpu;
1716 
1717 	BUG_ON(cpuhp_cb_check(state));
1718 
1719 	get_online_cpus();
1720 	mutex_lock(&cpuhp_state_mutex);
1721 
1722 	if (sp->multi_instance) {
1723 		WARN(!hlist_empty(&sp->list),
1724 		     "Error: Removing state %d which has instances left.\n",
1725 		     state);
1726 		goto remove;
1727 	}
1728 
1729 	if (!invoke || !cpuhp_get_teardown_cb(state))
1730 		goto remove;
1731 
1732 	/*
1733 	 * Call the teardown callback for each present cpu depending
1734 	 * on the hotplug state of the cpu. This function is not
1735 	 * allowed to fail currently!
1736 	 */
1737 	for_each_present_cpu(cpu) {
1738 		struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1739 		int cpustate = st->state;
1740 
1741 		if (cpustate >= state)
1742 			cpuhp_issue_call(cpu, state, false, NULL);
1743 	}
1744 remove:
1745 	cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
1746 	mutex_unlock(&cpuhp_state_mutex);
1747 	put_online_cpus();
1748 }
1749 EXPORT_SYMBOL(__cpuhp_remove_state);
1750 
1751 #if defined(CONFIG_SYSFS) && defined(CONFIG_HOTPLUG_CPU)
show_cpuhp_state(struct device * dev,struct device_attribute * attr,char * buf)1752 static ssize_t show_cpuhp_state(struct device *dev,
1753 				struct device_attribute *attr, char *buf)
1754 {
1755 	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
1756 
1757 	return sprintf(buf, "%d\n", st->state);
1758 }
1759 static DEVICE_ATTR(state, 0444, show_cpuhp_state, NULL);
1760 
write_cpuhp_target(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1761 static ssize_t write_cpuhp_target(struct device *dev,
1762 				  struct device_attribute *attr,
1763 				  const char *buf, size_t count)
1764 {
1765 	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
1766 	struct cpuhp_step *sp;
1767 	int target, ret;
1768 
1769 	ret = kstrtoint(buf, 10, &target);
1770 	if (ret)
1771 		return ret;
1772 
1773 #ifdef CONFIG_CPU_HOTPLUG_STATE_CONTROL
1774 	if (target < CPUHP_OFFLINE || target > CPUHP_ONLINE)
1775 		return -EINVAL;
1776 #else
1777 	if (target != CPUHP_OFFLINE && target != CPUHP_ONLINE)
1778 		return -EINVAL;
1779 #endif
1780 
1781 	ret = lock_device_hotplug_sysfs();
1782 	if (ret)
1783 		return ret;
1784 
1785 	mutex_lock(&cpuhp_state_mutex);
1786 	sp = cpuhp_get_step(target);
1787 	ret = !sp->name || sp->cant_stop ? -EINVAL : 0;
1788 	mutex_unlock(&cpuhp_state_mutex);
1789 	if (ret)
1790 		goto out;
1791 
1792 	if (st->state < target)
1793 		ret = do_cpu_up(dev->id, target);
1794 	else
1795 		ret = do_cpu_down(dev->id, target);
1796 out:
1797 	unlock_device_hotplug();
1798 	return ret ? ret : count;
1799 }
1800 
show_cpuhp_target(struct device * dev,struct device_attribute * attr,char * buf)1801 static ssize_t show_cpuhp_target(struct device *dev,
1802 				 struct device_attribute *attr, char *buf)
1803 {
1804 	struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
1805 
1806 	return sprintf(buf, "%d\n", st->target);
1807 }
1808 static DEVICE_ATTR(target, 0644, show_cpuhp_target, write_cpuhp_target);
1809 
1810 static struct attribute *cpuhp_cpu_attrs[] = {
1811 	&dev_attr_state.attr,
1812 	&dev_attr_target.attr,
1813 	NULL
1814 };
1815 
1816 static struct attribute_group cpuhp_cpu_attr_group = {
1817 	.attrs = cpuhp_cpu_attrs,
1818 	.name = "hotplug",
1819 	NULL
1820 };
1821 
show_cpuhp_states(struct device * dev,struct device_attribute * attr,char * buf)1822 static ssize_t show_cpuhp_states(struct device *dev,
1823 				 struct device_attribute *attr, char *buf)
1824 {
1825 	ssize_t cur, res = 0;
1826 	int i;
1827 
1828 	mutex_lock(&cpuhp_state_mutex);
1829 	for (i = CPUHP_OFFLINE; i <= CPUHP_ONLINE; i++) {
1830 		struct cpuhp_step *sp = cpuhp_get_step(i);
1831 
1832 		if (sp->name) {
1833 			cur = sprintf(buf, "%3d: %s\n", i, sp->name);
1834 			buf += cur;
1835 			res += cur;
1836 		}
1837 	}
1838 	mutex_unlock(&cpuhp_state_mutex);
1839 	return res;
1840 }
1841 static DEVICE_ATTR(states, 0444, show_cpuhp_states, NULL);
1842 
1843 static struct attribute *cpuhp_cpu_root_attrs[] = {
1844 	&dev_attr_states.attr,
1845 	NULL
1846 };
1847 
1848 static struct attribute_group cpuhp_cpu_root_attr_group = {
1849 	.attrs = cpuhp_cpu_root_attrs,
1850 	.name = "hotplug",
1851 	NULL
1852 };
1853 
cpuhp_sysfs_init(void)1854 static int __init cpuhp_sysfs_init(void)
1855 {
1856 	int cpu, ret;
1857 
1858 	ret = sysfs_create_group(&cpu_subsys.dev_root->kobj,
1859 				 &cpuhp_cpu_root_attr_group);
1860 	if (ret)
1861 		return ret;
1862 
1863 	for_each_possible_cpu(cpu) {
1864 		struct device *dev = get_cpu_device(cpu);
1865 
1866 		if (!dev)
1867 			continue;
1868 		ret = sysfs_create_group(&dev->kobj, &cpuhp_cpu_attr_group);
1869 		if (ret)
1870 			return ret;
1871 	}
1872 	return 0;
1873 }
1874 device_initcall(cpuhp_sysfs_init);
1875 #endif
1876 
1877 /*
1878  * cpu_bit_bitmap[] is a special, "compressed" data structure that
1879  * represents all NR_CPUS bits binary values of 1<<nr.
1880  *
1881  * It is used by cpumask_of() to get a constant address to a CPU
1882  * mask value that has a single bit set only.
1883  */
1884 
1885 /* cpu_bit_bitmap[0] is empty - so we can back into it */
1886 #define MASK_DECLARE_1(x)	[x+1][0] = (1UL << (x))
1887 #define MASK_DECLARE_2(x)	MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
1888 #define MASK_DECLARE_4(x)	MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
1889 #define MASK_DECLARE_8(x)	MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
1890 
1891 const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
1892 
1893 	MASK_DECLARE_8(0),	MASK_DECLARE_8(8),
1894 	MASK_DECLARE_8(16),	MASK_DECLARE_8(24),
1895 #if BITS_PER_LONG > 32
1896 	MASK_DECLARE_8(32),	MASK_DECLARE_8(40),
1897 	MASK_DECLARE_8(48),	MASK_DECLARE_8(56),
1898 #endif
1899 };
1900 EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
1901 
1902 const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
1903 EXPORT_SYMBOL(cpu_all_bits);
1904 
1905 #ifdef CONFIG_INIT_ALL_POSSIBLE
1906 struct cpumask __cpu_possible_mask __read_mostly
1907 	= {CPU_BITS_ALL};
1908 #else
1909 struct cpumask __cpu_possible_mask __read_mostly;
1910 #endif
1911 EXPORT_SYMBOL(__cpu_possible_mask);
1912 
1913 struct cpumask __cpu_online_mask __read_mostly;
1914 EXPORT_SYMBOL(__cpu_online_mask);
1915 
1916 struct cpumask __cpu_present_mask __read_mostly;
1917 EXPORT_SYMBOL(__cpu_present_mask);
1918 
1919 struct cpumask __cpu_active_mask __read_mostly;
1920 EXPORT_SYMBOL(__cpu_active_mask);
1921 
init_cpu_present(const struct cpumask * src)1922 void init_cpu_present(const struct cpumask *src)
1923 {
1924 	cpumask_copy(&__cpu_present_mask, src);
1925 }
1926 
init_cpu_possible(const struct cpumask * src)1927 void init_cpu_possible(const struct cpumask *src)
1928 {
1929 	cpumask_copy(&__cpu_possible_mask, src);
1930 }
1931 
init_cpu_online(const struct cpumask * src)1932 void init_cpu_online(const struct cpumask *src)
1933 {
1934 	cpumask_copy(&__cpu_online_mask, src);
1935 }
1936 
1937 /*
1938  * Activate the first processor.
1939  */
boot_cpu_init(void)1940 void __init boot_cpu_init(void)
1941 {
1942 	int cpu = smp_processor_id();
1943 
1944 	/* Mark the boot cpu "present", "online" etc for SMP and UP case */
1945 	set_cpu_online(cpu, true);
1946 	set_cpu_active(cpu, true);
1947 	set_cpu_present(cpu, true);
1948 	set_cpu_possible(cpu, true);
1949 }
1950 
1951 /*
1952  * Must be called _AFTER_ setting up the per_cpu areas
1953  */
boot_cpu_state_init(void)1954 void __init boot_cpu_state_init(void)
1955 {
1956 	per_cpu_ptr(&cpuhp_state, smp_processor_id())->state = CPUHP_ONLINE;
1957 }
1958 
1959 static ATOMIC_NOTIFIER_HEAD(idle_notifier);
1960 
idle_notifier_register(struct notifier_block * n)1961 void idle_notifier_register(struct notifier_block *n)
1962 {
1963 	atomic_notifier_chain_register(&idle_notifier, n);
1964 }
1965 EXPORT_SYMBOL_GPL(idle_notifier_register);
1966 
idle_notifier_unregister(struct notifier_block * n)1967 void idle_notifier_unregister(struct notifier_block *n)
1968 {
1969 	atomic_notifier_chain_unregister(&idle_notifier, n);
1970 }
1971 EXPORT_SYMBOL_GPL(idle_notifier_unregister);
1972 
idle_notifier_call_chain(unsigned long val)1973 void idle_notifier_call_chain(unsigned long val)
1974 {
1975 	atomic_notifier_call_chain(&idle_notifier, val, NULL);
1976 }
1977 EXPORT_SYMBOL_GPL(idle_notifier_call_chain);
1978