[COMMON] kernel: cpu: fix conflict
[GitHub/MotorolaMobilityLLC/kernel-slsi.git] / kernel / cpu.c
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/signal.h>
11 #include <linux/sched/hotplug.h>
12 #include <linux/sched/task.h>
13 #include <linux/sched/smt.h>
14 #include <linux/unistd.h>
15 #include <linux/cpu.h>
16 #include <linux/oom.h>
17 #include <linux/rcupdate.h>
18 #include <linux/export.h>
19 #include <linux/bug.h>
20 #include <linux/kthread.h>
21 #include <linux/stop_machine.h>
22 #include <linux/mutex.h>
23 #include <linux/gfp.h>
24 #include <linux/suspend.h>
25 #include <linux/lockdep.h>
26 #include <linux/tick.h>
27 #include <linux/irq.h>
28 #include <linux/nmi.h>
29 #include <linux/smpboot.h>
30 #include <linux/relay.h>
31 #include <linux/slab.h>
32 #include <linux/cpuset.h>
33
34 #include <soc/samsung/exynos-emc.h>
35
36 #include <trace/events/power.h>
37 #define CREATE_TRACE_POINTS
38 #include <trace/events/cpuhp.h>
39
40 #include "smpboot.h"
41
42 /**
43 * cpuhp_cpu_state - Per cpu hotplug state storage
44 * @state: The current cpu state
45 * @target: The target state
46 * @thread: Pointer to the hotplug thread
47 * @should_run: Thread should execute
48 * @rollback: Perform a rollback
49 * @single: Single callback invocation
50 * @bringup: Single callback bringup or teardown selector
51 * @cb_state: The state for a single callback (install/uninstall)
52 * @result: Result of the operation
53 * @done_up: Signal completion to the issuer of the task for cpu-up
54 * @done_down: Signal completion to the issuer of the task for cpu-down
55 */
56 struct cpuhp_cpu_state {
57 enum cpuhp_state state;
58 enum cpuhp_state target;
59 enum cpuhp_state fail;
60 #ifdef CONFIG_SMP
61 struct task_struct *thread;
62 bool should_run;
63 bool rollback;
64 bool single;
65 bool bringup;
66 bool booted_once;
67 struct hlist_node *node;
68 struct hlist_node *last;
69 enum cpuhp_state cb_state;
70 int result;
71 struct completion done_up;
72 struct completion done_down;
73 #endif
74 };
75
76 static DEFINE_PER_CPU(struct cpuhp_cpu_state, cpuhp_state) = {
77 .fail = CPUHP_INVALID,
78 };
79
80 #if defined(CONFIG_LOCKDEP) && defined(CONFIG_SMP)
81 static struct lock_class_key cpuhp_state_key;
82 static struct lockdep_map cpuhp_state_lock_map =
83 STATIC_LOCKDEP_MAP_INIT("cpuhp_state", &cpuhp_state_key);
84 #endif
85
86 /**
87 * cpuhp_step - Hotplug state machine step
88 * @name: Name of the step
89 * @startup: Startup function of the step
90 * @teardown: Teardown function of the step
91 * @skip_onerr: Do not invoke the functions on error rollback
92 * Will go away once the notifiers are gone
93 * @cant_stop: Bringup/teardown can't be stopped at this step
94 */
95 struct cpuhp_step {
96 const char *name;
97 union {
98 int (*single)(unsigned int cpu);
99 int (*multi)(unsigned int cpu,
100 struct hlist_node *node);
101 } startup;
102 union {
103 int (*single)(unsigned int cpu);
104 int (*multi)(unsigned int cpu,
105 struct hlist_node *node);
106 } teardown;
107 struct hlist_head list;
108 bool skip_onerr;
109 bool cant_stop;
110 bool multi_instance;
111 };
112
113 static DEFINE_MUTEX(cpuhp_state_mutex);
114 static struct cpuhp_step cpuhp_bp_states[];
115 static struct cpuhp_step cpuhp_ap_states[];
116
117 static bool cpuhp_is_ap_state(enum cpuhp_state state)
118 {
119 /*
120 * The extra check for CPUHP_TEARDOWN_CPU is only for documentation
121 * purposes as that state is handled explicitly in cpu_down.
122 */
123 return state > CPUHP_BRINGUP_CPU && state != CPUHP_TEARDOWN_CPU;
124 }
125
126 static struct cpuhp_step *cpuhp_get_step(enum cpuhp_state state)
127 {
128 struct cpuhp_step *sp;
129
130 sp = cpuhp_is_ap_state(state) ? cpuhp_ap_states : cpuhp_bp_states;
131 return sp + state;
132 }
133
134 /**
135 * cpuhp_invoke_callback _ Invoke the callbacks for a given state
136 * @cpu: The cpu for which the callback should be invoked
137 * @state: The state to do callbacks for
138 * @bringup: True if the bringup callback should be invoked
139 * @node: For multi-instance, do a single entry callback for install/remove
140 * @lastp: For multi-instance rollback, remember how far we got
141 *
142 * Called from cpu hotplug and from the state register machinery.
143 */
144 static int cpuhp_invoke_callback(unsigned int cpu, enum cpuhp_state state,
145 bool bringup, struct hlist_node *node,
146 struct hlist_node **lastp)
147 {
148 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
149 struct cpuhp_step *step = cpuhp_get_step(state);
150 int (*cbm)(unsigned int cpu, struct hlist_node *node);
151 int (*cb)(unsigned int cpu);
152 int ret, cnt;
153
154 if (st->fail == state) {
155 st->fail = CPUHP_INVALID;
156
157 if (!(bringup ? step->startup.single : step->teardown.single))
158 return 0;
159
160 return -EAGAIN;
161 }
162
163 if (!step->multi_instance) {
164 WARN_ON_ONCE(lastp && *lastp);
165 cb = bringup ? step->startup.single : step->teardown.single;
166 if (!cb)
167 return 0;
168 trace_cpuhp_enter(cpu, st->target, state, cb);
169 ret = cb(cpu);
170 trace_cpuhp_exit(cpu, st->state, state, ret);
171 return ret;
172 }
173 cbm = bringup ? step->startup.multi : step->teardown.multi;
174 if (!cbm)
175 return 0;
176
177 /* Single invocation for instance add/remove */
178 if (node) {
179 WARN_ON_ONCE(lastp && *lastp);
180 trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
181 ret = cbm(cpu, node);
182 trace_cpuhp_exit(cpu, st->state, state, ret);
183 return ret;
184 }
185
186 /* State transition. Invoke on all instances */
187 cnt = 0;
188 hlist_for_each(node, &step->list) {
189 if (lastp && node == *lastp)
190 break;
191
192 trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
193 ret = cbm(cpu, node);
194 trace_cpuhp_exit(cpu, st->state, state, ret);
195 if (ret) {
196 if (!lastp)
197 goto err;
198
199 *lastp = node;
200 return ret;
201 }
202 cnt++;
203 }
204 if (lastp)
205 *lastp = NULL;
206 return 0;
207 err:
208 /* Rollback the instances if one failed */
209 cbm = !bringup ? step->startup.multi : step->teardown.multi;
210 if (!cbm)
211 return ret;
212
213 hlist_for_each(node, &step->list) {
214 if (!cnt--)
215 break;
216
217 trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
218 ret = cbm(cpu, node);
219 trace_cpuhp_exit(cpu, st->state, state, ret);
220 /*
221 * Rollback must not fail,
222 */
223 WARN_ON_ONCE(ret);
224 }
225 return ret;
226 }
227
228 #ifdef CONFIG_SMP
229 static inline void wait_for_ap_thread(struct cpuhp_cpu_state *st, bool bringup)
230 {
231 struct completion *done = bringup ? &st->done_up : &st->done_down;
232 wait_for_completion(done);
233 }
234
235 static inline void complete_ap_thread(struct cpuhp_cpu_state *st, bool bringup)
236 {
237 struct completion *done = bringup ? &st->done_up : &st->done_down;
238 complete(done);
239 }
240
241 /*
242 * The former STARTING/DYING states, ran with IRQs disabled and must not fail.
243 */
244 static bool cpuhp_is_atomic_state(enum cpuhp_state state)
245 {
246 return CPUHP_AP_IDLE_DEAD <= state && state < CPUHP_AP_ONLINE;
247 }
248
249 /* Serializes the updates to cpu_online_mask, cpu_present_mask */
250 static DEFINE_MUTEX(cpu_add_remove_lock);
251 bool cpuhp_tasks_frozen;
252 EXPORT_SYMBOL_GPL(cpuhp_tasks_frozen);
253
254 /*
255 * The following two APIs (cpu_maps_update_begin/done) must be used when
256 * attempting to serialize the updates to cpu_online_mask & cpu_present_mask.
257 */
258 void cpu_maps_update_begin(void)
259 {
260 mutex_lock(&cpu_add_remove_lock);
261 }
262
263 void cpu_maps_update_done(void)
264 {
265 mutex_unlock(&cpu_add_remove_lock);
266 }
267
268 /* If set, cpu_up and cpu_down will return -EBUSY and do nothing.
269 * Should always be manipulated under cpu_add_remove_lock
270 */
271 static int cpu_hotplug_disabled;
272
273 #ifdef CONFIG_HOTPLUG_CPU
274
275 static struct {
276 struct task_struct *active_writer;
277 /* wait queue to wake up the active_writer */
278 wait_queue_head_t wq;
279 /* verifies that no writer will get active while readers are active */
280 struct mutex lock;
281 /*
282 * Also blocks the new readers during
283 * an ongoing cpu hotplug operation.
284 */
285 atomic_t refcount;
286
287 #ifdef CONFIG_DEBUG_LOCK_ALLOC
288 struct lockdep_map dep_map;
289 #endif
290 } cpu_hotplug = {
291 .active_writer = NULL,
292 .wq = __WAIT_QUEUE_HEAD_INITIALIZER(cpu_hotplug.wq),
293 .lock = __MUTEX_INITIALIZER(cpu_hotplug.lock),
294 #ifdef CONFIG_DEBUG_LOCK_ALLOC
295 .dep_map = STATIC_LOCKDEP_MAP_INIT("cpu_hotplug.dep_map", &cpu_hotplug.dep_map),
296 #endif
297 };
298
299 /* Lockdep annotations for get/put_online_cpus() and cpu_hotplug_begin/end() */
300 #define cpuhp_lock_acquire_read() lock_map_acquire_read(&cpu_hotplug.dep_map)
301 #define cpuhp_lock_acquire_tryread() \
302 lock_map_acquire_tryread(&cpu_hotplug.dep_map)
303 #define cpuhp_lock_acquire() lock_map_acquire(&cpu_hotplug.dep_map)
304 #define cpuhp_lock_release() lock_map_release(&cpu_hotplug.dep_map)
305
306
307 void cpus_read_lock(void)
308 {
309 might_sleep();
310 if (cpu_hotplug.active_writer == current)
311 return;
312 cpuhp_lock_acquire_read();
313 mutex_lock(&cpu_hotplug.lock);
314 atomic_inc(&cpu_hotplug.refcount);
315 mutex_unlock(&cpu_hotplug.lock);
316 }
317 EXPORT_SYMBOL_GPL(cpus_read_lock);
318
319 void cpus_read_unlock(void)
320 {
321 int refcount;
322
323 if (cpu_hotplug.active_writer == current)
324 return;
325
326 refcount = atomic_dec_return(&cpu_hotplug.refcount);
327 if (WARN_ON(refcount < 0)) /* try to fix things up */
328 atomic_inc(&cpu_hotplug.refcount);
329
330 if (refcount <= 0 && waitqueue_active(&cpu_hotplug.wq))
331 wake_up(&cpu_hotplug.wq);
332
333 cpuhp_lock_release();
334
335 }
336 EXPORT_SYMBOL_GPL(cpus_read_unlock);
337
338 /*
339 * This ensures that the hotplug operation can begin only when the
340 * refcount goes to zero.
341 *
342 * Note that during a cpu-hotplug operation, the new readers, if any,
343 * will be blocked by the cpu_hotplug.lock
344 *
345 * Since cpu_hotplug_begin() is always called after invoking
346 * cpu_maps_update_begin(), we can be sure that only one writer is active.
347 *
348 * Note that theoretically, there is a possibility of a livelock:
349 * - Refcount goes to zero, last reader wakes up the sleeping
350 * writer.
351 * - Last reader unlocks the cpu_hotplug.lock.
352 * - A new reader arrives at this moment, bumps up the refcount.
353 * - The writer acquires the cpu_hotplug.lock finds the refcount
354 * non zero and goes to sleep again.
355 *
356 * However, this is very difficult to achieve in practice since
357 * get_online_cpus() not an api which is called all that often.
358 *
359 */
360 void cpus_write_lock(void)
361 {
362 DEFINE_WAIT(wait);
363
364 cpu_hotplug.active_writer = current;
365 cpuhp_lock_acquire();
366
367 for (;;) {
368 mutex_lock(&cpu_hotplug.lock);
369 prepare_to_wait(&cpu_hotplug.wq, &wait, TASK_UNINTERRUPTIBLE);
370 if (likely(!atomic_read(&cpu_hotplug.refcount)))
371 break;
372 mutex_unlock(&cpu_hotplug.lock);
373 schedule();
374 }
375 finish_wait(&cpu_hotplug.wq, &wait);
376 }
377
378 void cpus_write_unlock(void)
379 {
380 cpu_hotplug.active_writer = NULL;
381 mutex_unlock(&cpu_hotplug.lock);
382 cpuhp_lock_release();
383 }
384
385 /*
386 * Wait for currently running CPU hotplug operations to complete (if any) and
387 * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
388 * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the
389 * hotplug path before performing hotplug operations. So acquiring that lock
390 * guarantees mutual exclusion from any currently running hotplug operations.
391 */
392 void cpu_hotplug_disable(void)
393 {
394 cpu_maps_update_begin();
395 cpu_hotplug_disabled++;
396 cpu_maps_update_done();
397 }
398 EXPORT_SYMBOL_GPL(cpu_hotplug_disable);
399
400 static void __cpu_hotplug_enable(void)
401 {
402 if (WARN_ONCE(!cpu_hotplug_disabled, "Unbalanced cpu hotplug enable\n"))
403 return;
404 cpu_hotplug_disabled--;
405 }
406
407 void cpu_hotplug_enable(void)
408 {
409 cpu_maps_update_begin();
410 __cpu_hotplug_enable();
411 cpu_maps_update_done();
412 }
413 EXPORT_SYMBOL_GPL(cpu_hotplug_enable);
414 #endif /* CONFIG_HOTPLUG_CPU */
415
416 /*
417 * Architectures that need SMT-specific errata handling during SMT hotplug
418 * should override this.
419 */
420 void __weak arch_smt_update(void) { }
421
422 #ifdef CONFIG_HOTPLUG_SMT
423 enum cpuhp_smt_control cpu_smt_control __read_mostly = CPU_SMT_ENABLED;
424
425 void __init cpu_smt_disable(bool force)
426 {
427 if (cpu_smt_control == CPU_SMT_FORCE_DISABLED ||
428 cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
429 return;
430
431 if (force) {
432 pr_info("SMT: Force disabled\n");
433 cpu_smt_control = CPU_SMT_FORCE_DISABLED;
434 } else {
435 cpu_smt_control = CPU_SMT_DISABLED;
436 }
437 }
438
439 /*
440 * The decision whether SMT is supported can only be done after the full
441 * CPU identification. Called from architecture code.
442 */
443 void __init cpu_smt_check_topology(void)
444 {
445 if (!topology_smt_supported())
446 cpu_smt_control = CPU_SMT_NOT_SUPPORTED;
447 }
448
449 static int __init smt_cmdline_disable(char *str)
450 {
451 cpu_smt_disable(str && !strcmp(str, "force"));
452 return 0;
453 }
454 early_param("nosmt", smt_cmdline_disable);
455
456 static inline bool cpu_smt_allowed(unsigned int cpu)
457 {
458 if (cpu_smt_control == CPU_SMT_ENABLED)
459 return true;
460
461 if (topology_is_primary_thread(cpu))
462 return true;
463
464 /*
465 * On x86 it's required to boot all logical CPUs at least once so
466 * that the init code can get a chance to set CR4.MCE on each
467 * CPU. Otherwise, a broadacasted MCE observing CR4.MCE=0b on any
468 * core will shutdown the machine.
469 */
470 return !per_cpu(cpuhp_state, cpu).booted_once;
471 }
472 #else
473 static inline bool cpu_smt_allowed(unsigned int cpu) { return true; }
474 #endif
475
476 static inline enum cpuhp_state
477 cpuhp_set_state(struct cpuhp_cpu_state *st, enum cpuhp_state target)
478 {
479 enum cpuhp_state prev_state = st->state;
480
481 st->rollback = false;
482 st->last = NULL;
483
484 st->target = target;
485 st->single = false;
486 st->bringup = st->state < target;
487
488 return prev_state;
489 }
490
491 static inline void
492 cpuhp_reset_state(struct cpuhp_cpu_state *st, enum cpuhp_state prev_state)
493 {
494 st->rollback = true;
495
496 /*
497 * If we have st->last we need to undo partial multi_instance of this
498 * state first. Otherwise start undo at the previous state.
499 */
500 if (!st->last) {
501 if (st->bringup)
502 st->state--;
503 else
504 st->state++;
505 }
506
507 st->target = prev_state;
508 st->bringup = !st->bringup;
509 }
510
511 /* Regular hotplug invocation of the AP hotplug thread */
512 static void __cpuhp_kick_ap(struct cpuhp_cpu_state *st)
513 {
514 if (!st->single && st->state == st->target)
515 return;
516
517 st->result = 0;
518 /*
519 * Make sure the above stores are visible before should_run becomes
520 * true. Paired with the mb() above in cpuhp_thread_fun()
521 */
522 smp_mb();
523 st->should_run = true;
524 wake_up_process(st->thread);
525 wait_for_ap_thread(st, st->bringup);
526 }
527
528 static int cpuhp_kick_ap(struct cpuhp_cpu_state *st, enum cpuhp_state target)
529 {
530 enum cpuhp_state prev_state;
531 int ret;
532
533 prev_state = cpuhp_set_state(st, target);
534 __cpuhp_kick_ap(st);
535 if ((ret = st->result)) {
536 cpuhp_reset_state(st, prev_state);
537 __cpuhp_kick_ap(st);
538 }
539
540 return ret;
541 }
542 /* Notifier wrappers for transitioning to state machine */
543
544 static int bringup_wait_for_ap(unsigned int cpu)
545 {
546 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
547
548 /* Wait for the CPU to reach CPUHP_AP_ONLINE_IDLE */
549 wait_for_ap_thread(st, true);
550 if (WARN_ON_ONCE((!cpu_online(cpu))))
551 return -ECANCELED;
552
553 /* Unpark the stopper thread and the hotplug thread of the target cpu */
554 stop_machine_unpark(cpu);
555 kthread_unpark(st->thread);
556
557 /*
558 * SMT soft disabling on X86 requires to bring the CPU out of the
559 * BIOS 'wait for SIPI' state in order to set the CR4.MCE bit. The
560 * CPU marked itself as booted_once in cpu_notify_starting() so the
561 * cpu_smt_allowed() check will now return false if this is not the
562 * primary sibling.
563 */
564 if (!cpu_smt_allowed(cpu))
565 return -ECANCELED;
566
567 if (st->target <= CPUHP_AP_ONLINE_IDLE)
568 return 0;
569
570 return cpuhp_kick_ap(st, st->target);
571 }
572
573 static int bringup_cpu(unsigned int cpu)
574 {
575 struct task_struct *idle = idle_thread_get(cpu);
576 int ret;
577
578 /*
579 * Some architectures have to walk the irq descriptors to
580 * setup the vector space for the cpu which comes online.
581 * Prevent irq alloc/free across the bringup.
582 */
583 irq_lock_sparse();
584
585 /* Arch-specific enabling code. */
586 ret = __cpu_up(cpu, idle);
587 irq_unlock_sparse();
588 if (ret)
589 return ret;
590 return bringup_wait_for_ap(cpu);
591 }
592
593 /*
594 * Hotplug state machine related functions
595 */
596
597 static void undo_cpu_up(unsigned int cpu, struct cpuhp_cpu_state *st)
598 {
599 for (st->state--; st->state > st->target; st->state--) {
600 struct cpuhp_step *step = cpuhp_get_step(st->state);
601
602 if (!step->skip_onerr)
603 cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
604 }
605 }
606
607 static inline bool can_rollback_cpu(struct cpuhp_cpu_state *st)
608 {
609 if (IS_ENABLED(CONFIG_HOTPLUG_CPU))
610 return true;
611 /*
612 * When CPU hotplug is disabled, then taking the CPU down is not
613 * possible because takedown_cpu() and the architecture and
614 * subsystem specific mechanisms are not available. So the CPU
615 * which would be completely unplugged again needs to stay around
616 * in the current state.
617 */
618 return st->state <= CPUHP_BRINGUP_CPU;
619 }
620
621 static int cpuhp_up_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
622 enum cpuhp_state target)
623 {
624 enum cpuhp_state prev_state = st->state;
625 int ret = 0;
626
627 while (st->state < target) {
628 st->state++;
629 ret = cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
630 if (ret) {
631 if (can_rollback_cpu(st)) {
632 st->target = prev_state;
633 undo_cpu_up(cpu, st);
634 }
635 break;
636 }
637 }
638 return ret;
639 }
640
641 /*
642 * The cpu hotplug threads manage the bringup and teardown of the cpus
643 */
644 static void cpuhp_create(unsigned int cpu)
645 {
646 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
647
648 init_completion(&st->done_up);
649 init_completion(&st->done_down);
650 }
651
652 static int cpuhp_should_run(unsigned int cpu)
653 {
654 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
655
656 return st->should_run;
657 }
658
659 /*
660 * Execute teardown/startup callbacks on the plugged cpu. Also used to invoke
661 * callbacks when a state gets [un]installed at runtime.
662 *
663 * Each invocation of this function by the smpboot thread does a single AP
664 * state callback.
665 *
666 * It has 3 modes of operation:
667 * - single: runs st->cb_state
668 * - up: runs ++st->state, while st->state < st->target
669 * - down: runs st->state--, while st->state > st->target
670 *
671 * When complete or on error, should_run is cleared and the completion is fired.
672 */
673 static void cpuhp_thread_fun(unsigned int cpu)
674 {
675 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
676 bool bringup = st->bringup;
677 enum cpuhp_state state;
678
679 if (WARN_ON_ONCE(!st->should_run))
680 return;
681
682 /*
683 * ACQUIRE for the cpuhp_should_run() load of ->should_run. Ensures
684 * that if we see ->should_run we also see the rest of the state.
685 */
686 smp_mb();
687
688 <<<<<<< HEAD
689 if (WARN_ON_ONCE(!st->should_run))
690 return;
691
692 lock_map_acquire(&cpuhp_state_lock_map);
693 =======
694 cpuhp_lock_acquire(bringup);
695 >>>>>>> android-4.14-p
696
697 if (st->single) {
698 state = st->cb_state;
699 st->should_run = false;
700 } else {
701 if (bringup) {
702 st->state++;
703 state = st->state;
704 st->should_run = (st->state < st->target);
705 WARN_ON_ONCE(st->state > st->target);
706 } else {
707 state = st->state;
708 st->state--;
709 st->should_run = (st->state > st->target);
710 WARN_ON_ONCE(st->state < st->target);
711 }
712 }
713
714 WARN_ON_ONCE(!cpuhp_is_ap_state(state));
715
716 if (st->rollback) {
717 struct cpuhp_step *step = cpuhp_get_step(state);
718 if (step->skip_onerr)
719 goto next;
720 }
721
722 if (cpuhp_is_atomic_state(state)) {
723 local_irq_disable();
724 st->result = cpuhp_invoke_callback(cpu, state, bringup, st->node, &st->last);
725 local_irq_enable();
726
727 /*
728 * STARTING/DYING must not fail!
729 */
730 WARN_ON_ONCE(st->result);
731 } else {
732 st->result = cpuhp_invoke_callback(cpu, state, bringup, st->node, &st->last);
733 }
734
735 if (st->result) {
736 /*
737 * If we fail on a rollback, we're up a creek without no
738 * paddle, no way forward, no way back. We loose, thanks for
739 * playing.
740 */
741 WARN_ON_ONCE(st->rollback);
742 st->should_run = false;
743 }
744
745 next:
746 lock_map_release(&cpuhp_state_lock_map);
747
748 if (!st->should_run)
749 complete_ap_thread(st, bringup);
750 }
751
752 /* Invoke a single callback on a remote cpu */
753 static int
754 cpuhp_invoke_ap_callback(int cpu, enum cpuhp_state state, bool bringup,
755 struct hlist_node *node)
756 {
757 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
758 int ret;
759
760 if (!cpu_online(cpu))
761 return 0;
762
763 lock_map_acquire(&cpuhp_state_lock_map);
764 lock_map_release(&cpuhp_state_lock_map);
765
766 /*
767 * If we are up and running, use the hotplug thread. For early calls
768 * we invoke the thread function directly.
769 */
770 if (!st->thread)
771 return cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
772
773 st->rollback = false;
774 st->last = NULL;
775
776 st->node = node;
777 st->bringup = bringup;
778 st->cb_state = state;
779 st->single = true;
780
781 __cpuhp_kick_ap(st);
782
783 /*
784 * If we failed and did a partial, do a rollback.
785 */
786 if ((ret = st->result) && st->last) {
787 st->rollback = true;
788 st->bringup = !bringup;
789
790 __cpuhp_kick_ap(st);
791 }
792
793 /*
794 * Clean up the leftovers so the next hotplug operation wont use stale
795 * data.
796 */
797 st->node = st->last = NULL;
798 return ret;
799 }
800
801 static int cpuhp_fast_kick_ap_work_pre(unsigned int cpu)
802 {
803 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
804 enum cpuhp_state prev_state = st->state;
805
806 lock_map_acquire(&cpuhp_state_lock_map);
807 lock_map_release(&cpuhp_state_lock_map);
808
809 trace_cpuhp_enter(cpu, st->target, prev_state,
810 cpuhp_fast_kick_ap_work_pre);
811
812 cpuhp_set_state(st, st->target);
813 if (!st->single && st->state == st->target)
814 return prev_state;
815
816 st->result = 0;
817 /*
818 * Make sure the above stores are visible before should_run becomes
819 * true. Paired with the mb() above in cpuhp_thread_fun()
820 */
821 smp_mb();
822 st->should_run = true;
823 wake_up_process(st->thread);
824
825 return prev_state;
826 }
827
828 static int cpuhp_fast_kick_ap_work_post(unsigned int cpu,
829 enum cpuhp_state prev_state)
830 {
831 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
832 int ret;
833
834 wait_for_ap_thread(st, st->bringup);
835 if ((ret = st->result)) {
836 cpuhp_reset_state(st, prev_state);
837 __cpuhp_kick_ap(st);
838 }
839 trace_cpuhp_exit(cpu, st->state, prev_state, ret);
840
841 return ret;
842 }
843
844 static int cpuhp_kick_ap_work(unsigned int cpu)
845 {
846 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
847 enum cpuhp_state prev_state = st->state;
848 int ret;
849
850 lock_map_acquire(&cpuhp_state_lock_map);
851 lock_map_release(&cpuhp_state_lock_map);
852
853 trace_cpuhp_enter(cpu, st->target, prev_state, cpuhp_kick_ap_work);
854 ret = cpuhp_kick_ap(st, st->target);
855 trace_cpuhp_exit(cpu, st->state, prev_state, ret);
856
857 return ret;
858 }
859
860 static struct smp_hotplug_thread cpuhp_threads = {
861 .store = &cpuhp_state.thread,
862 .create = &cpuhp_create,
863 .thread_should_run = cpuhp_should_run,
864 .thread_fn = cpuhp_thread_fun,
865 .thread_comm = "cpuhp/%u",
866 .selfparking = true,
867 };
868
869 void __init cpuhp_threads_init(void)
870 {
871 BUG_ON(smpboot_register_percpu_thread(&cpuhp_threads));
872 kthread_unpark(this_cpu_read(cpuhp_state.thread));
873 }
874
875 #ifdef CONFIG_HOTPLUG_CPU
876 /**
877 * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU
878 * @cpu: a CPU id
879 *
880 * This function walks all processes, finds a valid mm struct for each one and
881 * then clears a corresponding bit in mm's cpumask. While this all sounds
882 * trivial, there are various non-obvious corner cases, which this function
883 * tries to solve in a safe manner.
884 *
885 * Also note that the function uses a somewhat relaxed locking scheme, so it may
886 * be called only for an already offlined CPU.
887 */
888 void clear_tasks_mm_cpumask(int cpu)
889 {
890 struct task_struct *p;
891
892 /*
893 * This function is called after the cpu is taken down and marked
894 * offline, so its not like new tasks will ever get this cpu set in
895 * their mm mask. -- Peter Zijlstra
896 * Thus, we may use rcu_read_lock() here, instead of grabbing
897 * full-fledged tasklist_lock.
898 */
899 WARN_ON(cpu_online(cpu));
900 rcu_read_lock();
901 for_each_process(p) {
902 struct task_struct *t;
903
904 /*
905 * Main thread might exit, but other threads may still have
906 * a valid mm. Find one.
907 */
908 t = find_lock_task_mm(p);
909 if (!t)
910 continue;
911 cpumask_clear_cpu(cpu, mm_cpumask(t->mm));
912 task_unlock(t);
913 }
914 rcu_read_unlock();
915 }
916
917 /* Take this CPU down. */
918 static int take_cpu_down(void *_param)
919 {
920 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
921 enum cpuhp_state target = max((int)st->target, CPUHP_AP_OFFLINE);
922 int err, cpu = smp_processor_id();
923 int ret;
924
925 /* Ensure this CPU doesn't handle any more interrupts. */
926 err = __cpu_disable();
927 if (err < 0)
928 return err;
929
930 /*
931 * We get here while we are in CPUHP_TEARDOWN_CPU state and we must not
932 * do this step again.
933 */
934 WARN_ON(st->state != CPUHP_TEARDOWN_CPU);
935 st->state--;
936 /* Invoke the former CPU_DYING callbacks */
937 for (; st->state > target; st->state--) {
938 ret = cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
939 /*
940 * DYING must not fail!
941 */
942 WARN_ON_ONCE(ret);
943 }
944
945 /* Give up timekeeping duties */
946 tick_handover_do_timer();
947 /* Park the stopper thread */
948 stop_machine_park(cpu);
949 return 0;
950 }
951
952 static int takedown_cpu(unsigned int cpu);
953 static int takedown_cpus(const struct cpumask *down_cpus)
954 {
955 struct cpuhp_cpu_state *st;
956 int err, cpu;
957
958 /* Park the smpboot threads */
959 for_each_cpu(cpu, down_cpus) {
960 st = per_cpu_ptr(&cpuhp_state, cpu);
961 trace_cpuhp_enter(cpu, st->target, st->state, takedown_cpu);
962
963 kthread_park(per_cpu_ptr(&cpuhp_state, cpu)->thread);
964 smpboot_park_threads(cpu);
965 }
966
967 /*
968 * Prevent irq alloc/free while the dying cpu reorganizes the
969 * interrupt affinities.
970 */
971 irq_lock_sparse();
972
973 /*
974 * So now all preempt/rcu users must observe !cpu_active().
975 */
976 err = stop_machine_cpuslocked(take_cpu_down, NULL, down_cpus);
977 if (err) {
978 /* CPU refused to die */
979 irq_unlock_sparse();
980 for_each_cpu(cpu, down_cpus) {
981 st = per_cpu_ptr(&cpuhp_state, cpu);
982 st->target = st->state;
983
984 /* Unpark the hotplug thread so we can rollback there */
985 kthread_unpark(per_cpu_ptr(&cpuhp_state, cpu)->thread);
986 }
987 return err;
988 }
989
990 for_each_cpu(cpu, down_cpus) {
991 st = per_cpu_ptr(&cpuhp_state, cpu);
992 BUG_ON(cpu_online(cpu));
993
994 /*
995 * The CPUHP_AP_SCHED_MIGRATE_DYING callback will have removed all
996 * runnable tasks from the cpu, there's only the idle task left now
997 * that the migration thread is done doing the stop_machine thing.
998 *
999 * Wait for the stop thread to go away.
1000 */
1001 wait_for_ap_thread(st, false);
1002 BUG_ON(st->state != CPUHP_AP_IDLE_DEAD);
1003 }
1004
1005
1006 /* Interrupts are moved away from the dying cpu, reenable alloc/free */
1007 irq_unlock_sparse();
1008
1009 for_each_cpu(cpu, down_cpus) {
1010 st = per_cpu_ptr(&cpuhp_state, cpu);
1011
1012 hotplug_cpu__broadcast_tick_pull(cpu);
1013 /* This actually kills the CPU. */
1014 __cpu_die(cpu);
1015 tick_cleanup_dead_cpu(cpu);
1016 rcutree_migrate_callbacks(cpu);
1017
1018 trace_cpuhp_exit(cpu, st->state, st->state, st->result);
1019 }
1020
1021 return 0;
1022 }
1023
1024
1025 static int takedown_cpu(unsigned int cpu)
1026 {
1027 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1028 int err;
1029
1030 /* Park the smpboot threads */
1031 kthread_park(per_cpu_ptr(&cpuhp_state, cpu)->thread);
1032
1033 /*
1034 * Prevent irq alloc/free while the dying cpu reorganizes the
1035 * interrupt affinities.
1036 */
1037 irq_lock_sparse();
1038
1039 /*
1040 * So now all preempt/rcu users must observe !cpu_active().
1041 */
1042 err = stop_machine_cpuslocked(take_cpu_down, NULL, cpumask_of(cpu));
1043 if (err) {
1044 /* CPU refused to die */
1045 irq_unlock_sparse();
1046 /* Unpark the hotplug thread so we can rollback there */
1047 kthread_unpark(per_cpu_ptr(&cpuhp_state, cpu)->thread);
1048 return err;
1049 }
1050 BUG_ON(cpu_online(cpu));
1051
1052 /*
1053 * The CPUHP_AP_SCHED_MIGRATE_DYING callback will have removed all
1054 * runnable tasks from the cpu, there's only the idle task left now
1055 * that the migration thread is done doing the stop_machine thing.
1056 *
1057 * Wait for the stop thread to go away.
1058 */
1059 wait_for_ap_thread(st, false);
1060 BUG_ON(st->state != CPUHP_AP_IDLE_DEAD);
1061
1062 /* Interrupts are moved away from the dying cpu, reenable alloc/free */
1063 irq_unlock_sparse();
1064
1065 hotplug_cpu__broadcast_tick_pull(cpu);
1066 /* This actually kills the CPU. */
1067 __cpu_die(cpu);
1068
1069 tick_cleanup_dead_cpu(cpu);
1070 rcutree_migrate_callbacks(cpu);
1071 return 0;
1072 }
1073
1074 static void cpuhp_complete_idle_dead(void *arg)
1075 {
1076 struct cpuhp_cpu_state *st = arg;
1077
1078 complete_ap_thread(st, false);
1079 }
1080
1081 void cpuhp_report_idle_dead(void)
1082 {
1083 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
1084
1085 BUG_ON(st->state != CPUHP_AP_OFFLINE);
1086 rcu_report_dead(smp_processor_id());
1087 st->state = CPUHP_AP_IDLE_DEAD;
1088 /*
1089 * We cannot call complete after rcu_report_dead() so we delegate it
1090 * to an online cpu.
1091 */
1092 smp_call_function_single(cpumask_first(cpu_online_mask),
1093 cpuhp_complete_idle_dead, st, 0);
1094 }
1095
1096 static void undo_cpu_down(unsigned int cpu, struct cpuhp_cpu_state *st)
1097 {
1098 for (st->state++; st->state < st->target; st->state++) {
1099 struct cpuhp_step *step = cpuhp_get_step(st->state);
1100
1101 if (!step->skip_onerr)
1102 cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
1103 }
1104 }
1105
1106 static int cpuhp_down_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
1107 enum cpuhp_state target)
1108 {
1109 enum cpuhp_state prev_state = st->state;
1110 int ret = 0;
1111
1112 for (; st->state > target; st->state--) {
1113 ret = cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
1114 if (ret) {
1115 st->target = prev_state;
1116 if (st->state < prev_state)
1117 undo_cpu_down(cpu, st);
1118 break;
1119 }
1120 }
1121 return ret;
1122 }
1123 static int __ref _cpus_down(struct cpumask cpus, int tasks_frozen,
1124 enum cpuhp_state target)
1125 {
1126 struct cpuhp_cpu_state *st;
1127 cpumask_t ap_work_cpus = CPU_MASK_NONE;
1128 cpumask_t take_down_cpus = CPU_MASK_NONE;
1129 int prev_state[8] = {0};
1130 int ret = 0;
1131 int cpu;
1132
1133 if (num_online_cpus() == 1)
1134 return -EBUSY;
1135
1136 for_each_cpu(cpu, &cpus)
1137 if (!cpu_present(cpu))
1138 return -EINVAL;
1139
1140 cpus_write_lock();
1141 cpuhp_tasks_frozen = tasks_frozen;
1142
1143 cpumask_copy(&cpu_fastoff_mask, &cpus);
1144 for_each_cpu(cpu, &cpus) {
1145 st = per_cpu_ptr(&cpuhp_state, cpu);
1146 prev_state[cpu] = cpuhp_set_state(st, target);
1147 if (st->state > CPUHP_TEARDOWN_CPU)
1148 cpumask_set_cpu(cpu, &ap_work_cpus);
1149 else
1150 cpumask_set_cpu(cpu, &take_down_cpus);
1151 }
1152
1153 for_each_cpu(cpu, &ap_work_cpus) {
1154 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1155 emc_cpu_pre_off_callback(cpu);
1156 set_cpu_active(cpu, false);
1157 st->state = CPUHP_AP_EXYNOS_IDLE_CTRL;
1158 }
1159
1160 cpuset_update_active_cpus();
1161
1162 for_each_cpu(cpu, &ap_work_cpus) {
1163 st = per_cpu_ptr(&cpuhp_state, cpu);
1164 set_cpu_active(cpu, false);
1165 st->state = CPUHP_AP_EXYNOS_IDLE_CTRL;
1166 }
1167
1168 cpuset_update_active_cpus();
1169
1170 for_each_cpu(cpu, &ap_work_cpus) {
1171 st = per_cpu_ptr(&cpuhp_state, cpu);
1172 st->target = max((int)target, CPUHP_TEARDOWN_CPU);
1173 cpuhp_fast_kick_ap_work_pre(cpu);
1174 }
1175
1176 for_each_cpu(cpu, &ap_work_cpus) {
1177 st = per_cpu_ptr(&cpuhp_state, cpu);
1178 cpuhp_fast_kick_ap_work_post(cpu, prev_state[cpu]);
1179 /*
1180 * We might have stopped still in the range of the AP hotplug
1181 * thread. Nothing to do anymore.
1182 */
1183 st->target = target;
1184 cpumask_set_cpu(cpu, &take_down_cpus);
1185 }
1186
1187 /* Hotplug out of all cpu failed */
1188 if (cpumask_empty(&take_down_cpus))
1189 goto out;
1190
1191 ret = takedown_cpus(&take_down_cpus);
1192 if (ret)
1193 panic("%s: fauiled to takedown_cpus\n", __func__);
1194
1195
1196 for_each_cpu(cpu, &take_down_cpus) {
1197 st = per_cpu_ptr(&cpuhp_state, cpu);
1198 ret = cpuhp_down_callbacks(cpu, st, target);
1199 if (ret && st->state > CPUHP_TEARDOWN_CPU && st->state < prev_state[cpu]) {
1200 cpuhp_reset_state(st, prev_state[cpu]);
1201 __cpuhp_kick_ap(st);
1202 }
1203 }
1204
1205 cpumask_clear(&cpu_fastoff_mask);
1206
1207 out:
1208 cpus_write_unlock();
1209
1210 /*
1211 * Do post unplug cleanup. This is still protected against
1212 * concurrent CPU hotplug via cpu_add_remove_lock.
1213 */
1214 lockup_detector_cleanup();
1215
1216 return ret;
1217 }
1218
1219 int cpus_down(struct cpumask cpus)
1220 {
1221 int err, cpu;
1222
1223 trace_cpus_down_enter(cpumask_first(&cpus));
1224 cpu_maps_update_begin();
1225
1226 if (cpu_hotplug_disabled) {
1227 err = -EBUSY;
1228 goto out;
1229 }
1230
1231 for_each_cpu(cpu, &cpus)
1232 if (!cpu_online(cpu)) {
1233 cpumask_clear_cpu(cpu, &cpus);
1234 pr_warn("cpus_down: cpu%d is not online\n", cpu);
1235 }
1236
1237 err = _cpus_down(cpus, 0, CPUHP_OFFLINE);
1238
1239 out:
1240 cpu_maps_update_done();
1241 trace_cpus_down_exit(cpumask_first(&cpus));
1242 return err;
1243 }
1244 EXPORT_SYMBOL_GPL(cpus_down);
1245
1246 /* Requires cpu_add_remove_lock to be held */
1247 static int __ref _cpu_down(unsigned int cpu, int tasks_frozen,
1248 enum cpuhp_state target)
1249 {
1250 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1251 int prev_state, ret = 0;
1252
1253 if (num_online_cpus() == 1)
1254 return -EBUSY;
1255
1256 if (!cpu_present(cpu))
1257 return -EINVAL;
1258
1259 cpus_write_lock();
1260
1261 cpuhp_tasks_frozen = tasks_frozen;
1262
1263 prev_state = cpuhp_set_state(st, target);
1264 /*
1265 * If the current CPU state is in the range of the AP hotplug thread,
1266 * then we need to kick the thread.
1267 */
1268 if (st->state > CPUHP_TEARDOWN_CPU) {
1269 st->target = max((int)target, CPUHP_TEARDOWN_CPU);
1270 ret = cpuhp_kick_ap_work(cpu);
1271 /*
1272 * The AP side has done the error rollback already. Just
1273 * return the error code..
1274 */
1275 if (ret)
1276 goto out;
1277
1278 /*
1279 * We might have stopped still in the range of the AP hotplug
1280 * thread. Nothing to do anymore.
1281 */
1282 if (st->state > CPUHP_TEARDOWN_CPU)
1283 goto out;
1284
1285 st->target = target;
1286 }
1287 /*
1288 * The AP brought itself down to CPUHP_TEARDOWN_CPU. So we need
1289 * to do the further cleanups.
1290 */
1291 ret = cpuhp_down_callbacks(cpu, st, target);
1292 if (ret && st->state == CPUHP_TEARDOWN_CPU && st->state < prev_state) {
1293 cpuhp_reset_state(st, prev_state);
1294 __cpuhp_kick_ap(st);
1295 }
1296
1297 out:
1298 cpus_write_unlock();
1299 /*
1300 * Do post unplug cleanup. This is still protected against
1301 * concurrent CPU hotplug via cpu_add_remove_lock.
1302 */
1303 lockup_detector_cleanup();
1304 arch_smt_update();
1305 return ret;
1306 }
1307
1308 static int cpu_down_maps_locked(unsigned int cpu, enum cpuhp_state target)
1309 {
1310 if (cpu_hotplug_disabled)
1311 return -EBUSY;
1312 return _cpu_down(cpu, 0, target);
1313 }
1314
1315 static int do_cpu_down(unsigned int cpu, enum cpuhp_state target)
1316 {
1317 int err;
1318
1319 cpu_maps_update_begin();
1320 err = cpu_down_maps_locked(cpu, target);
1321 cpu_maps_update_done();
1322 return err;
1323 }
1324
1325 int cpu_down(unsigned int cpu)
1326 {
1327 return do_cpu_down(cpu, CPUHP_OFFLINE);
1328 }
1329 EXPORT_SYMBOL(cpu_down);
1330
1331 #else
1332 #define takedown_cpu NULL
1333 #endif /*CONFIG_HOTPLUG_CPU*/
1334
1335 /**
1336 * notify_cpu_starting(cpu) - Invoke the callbacks on the starting CPU
1337 * @cpu: cpu that just started
1338 *
1339 * It must be called by the arch code on the new cpu, before the new cpu
1340 * enables interrupts and before the "boot" cpu returns from __cpu_up().
1341 */
1342 void notify_cpu_starting(unsigned int cpu)
1343 {
1344 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1345 enum cpuhp_state target = min((int)st->target, CPUHP_AP_ONLINE);
1346 int ret;
1347
1348 rcu_cpu_starting(cpu); /* Enables RCU usage on this CPU. */
1349 st->booted_once = true;
1350 while (st->state < target) {
1351 st->state++;
1352 ret = cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
1353 /*
1354 * STARTING must not fail!
1355 */
1356 WARN_ON_ONCE(ret);
1357 }
1358 }
1359
1360 /*
1361 * Called from the idle task. Wake up the controlling task which brings the
1362 * stopper and the hotplug thread of the upcoming CPU up and then delegates
1363 * the rest of the online bringup to the hotplug thread.
1364 */
1365 void cpuhp_online_idle(enum cpuhp_state state)
1366 {
1367 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
1368
1369 /* Happens for the boot cpu */
1370 if (state != CPUHP_AP_ONLINE_IDLE)
1371 return;
1372
1373 st->state = CPUHP_AP_ONLINE_IDLE;
1374 complete_ap_thread(st, true);
1375 }
1376
1377 /* Requires cpu_add_remove_lock to be held */
1378 static int __ref _cpus_up(struct cpumask cpus, int tasks_frozen,
1379 enum cpuhp_state target)
1380 {
1381 struct cpuhp_cpu_state *st;
1382 cpumask_t ap_work_cpus = CPU_MASK_NONE;
1383 cpumask_t bringup_cpus = CPU_MASK_NONE;
1384 int prev_state[8] = {0};
1385 struct task_struct *idle;
1386 int cpu;
1387 int ret = 0;
1388
1389 cpus_write_lock();
1390
1391 for_each_cpu(cpu, &cpus)
1392 if (!cpu_present(cpu)) {
1393 pr_warn("_cpus_up: cpu%d is not present\n", cpu);
1394 cpumask_clear_cpu(cpu, &cpus);
1395 }
1396
1397 cpumask_copy(&cpu_faston_mask, &cpus);
1398
1399 for_each_cpu(cpu, &cpu_faston_mask) {
1400 st = per_cpu_ptr(&cpuhp_state, cpu);
1401 /*
1402 * The caller of do_cpu_up might have raced with another
1403 * caller. Ignore it for now.
1404 */
1405 if (st->state >= target)
1406 continue;
1407
1408 if (st->state == CPUHP_OFFLINE) {
1409 /* Let it fail before we try to bring the cpu up */
1410 idle = idle_thread_get(cpu);
1411 if (IS_ERR(idle)) {
1412 ret = PTR_ERR(idle);
1413 continue;
1414 }
1415 }
1416
1417 prev_state[cpu] = cpuhp_set_state(st, target);
1418
1419 if (st->state > CPUHP_BRINGUP_CPU)
1420 cpumask_set_cpu(cpu, &ap_work_cpus);
1421 else
1422 cpumask_set_cpu(cpu, &bringup_cpus);
1423
1424 }
1425
1426 cpuhp_tasks_frozen = tasks_frozen;
1427 /*
1428 * If the current CPU state is in the range of the AP hotplug thread,
1429 * then we need to kick the thread once more.
1430 */
1431 for_each_cpu(cpu, &ap_work_cpus)
1432 cpuhp_fast_kick_ap_work_pre(cpu);
1433
1434 for_each_cpu(cpu, &ap_work_cpus)
1435 cpuhp_fast_kick_ap_work_post(cpu, prev_state[cpu]);
1436
1437 /* Hotplug out of all cpu failed */
1438 if (cpumask_empty(&bringup_cpus))
1439 goto out;
1440
1441 /*
1442 * Try to reach the target state. We max out on the BP at
1443 * CPUHP_BRINGUP_CPU. After that the AP hotplug thread is
1444 * responsible for bringing it up to the target state.
1445 */
1446 target = min((int)target, CPUHP_BRINGUP_CPU);
1447 for_each_cpu(cpu, &bringup_cpus) {
1448 st = per_cpu_ptr(&cpuhp_state, cpu);
1449 ret = cpuhp_up_callbacks(cpu, st, target);
1450 if (ret)
1451 panic("%s: fauiled to bringup_cpus\n", __func__);
1452 }
1453 out:
1454 cpumask_clear(&cpu_faston_mask);
1455 cpus_write_unlock();
1456
1457 return ret;
1458 }
1459
1460
1461 /* Requires cpu_add_remove_lock to be held */
1462 static int _cpu_up(unsigned int cpu, int tasks_frozen, enum cpuhp_state target)
1463 {
1464 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1465 struct task_struct *idle;
1466 int ret = 0;
1467
1468 cpus_write_lock();
1469
1470 if (!cpu_present(cpu)) {
1471 ret = -EINVAL;
1472 goto out;
1473 }
1474
1475 /*
1476 * The caller of do_cpu_up might have raced with another
1477 * caller. Ignore it for now.
1478 */
1479 if (st->state >= target)
1480 goto out;
1481
1482 if (st->state == CPUHP_OFFLINE) {
1483 /* Let it fail before we try to bring the cpu up */
1484 idle = idle_thread_get(cpu);
1485 if (IS_ERR(idle)) {
1486 ret = PTR_ERR(idle);
1487 goto out;
1488 }
1489 }
1490
1491 cpuhp_tasks_frozen = tasks_frozen;
1492
1493 cpuhp_set_state(st, target);
1494 /*
1495 * If the current CPU state is in the range of the AP hotplug thread,
1496 * then we need to kick the thread once more.
1497 */
1498 if (st->state > CPUHP_BRINGUP_CPU) {
1499 ret = cpuhp_kick_ap_work(cpu);
1500 /*
1501 * The AP side has done the error rollback already. Just
1502 * return the error code..
1503 */
1504 if (ret)
1505 goto out;
1506 }
1507
1508 /*
1509 * Try to reach the target state. We max out on the BP at
1510 * CPUHP_BRINGUP_CPU. After that the AP hotplug thread is
1511 * responsible for bringing it up to the target state.
1512 */
1513 target = min((int)target, CPUHP_BRINGUP_CPU);
1514 ret = cpuhp_up_callbacks(cpu, st, target);
1515 out:
1516 cpus_write_unlock();
1517 arch_smt_update();
1518 return ret;
1519 }
1520
1521 static int do_cpu_up(unsigned int cpu, enum cpuhp_state target)
1522 {
1523 int err = 0;
1524
1525 if (!cpu_possible(cpu)) {
1526 pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n",
1527 cpu);
1528 #if defined(CONFIG_IA64)
1529 pr_err("please check additional_cpus= boot parameter\n");
1530 #endif
1531 return -EINVAL;
1532 }
1533
1534 err = try_online_node(cpu_to_node(cpu));
1535 if (err)
1536 return err;
1537
1538 cpu_maps_update_begin();
1539
1540 if (cpu_hotplug_disabled) {
1541 err = -EBUSY;
1542 goto out;
1543 }
1544 if (!cpu_smt_allowed(cpu)) {
1545 err = -EPERM;
1546 goto out;
1547 }
1548
1549 err = _cpu_up(cpu, 0, target);
1550 out:
1551 cpu_maps_update_done();
1552 return err;
1553 }
1554
1555 int cpu_up(unsigned int cpu)
1556 {
1557 return do_cpu_up(cpu, CPUHP_ONLINE);
1558 }
1559 EXPORT_SYMBOL_GPL(cpu_up);
1560
1561 int cpus_up(struct cpumask cpus)
1562 {
1563 int cpu, err = 0;
1564
1565 trace_cpus_up_enter(cpumask_first(&cpus));
1566 for_each_cpu(cpu, &cpus)
1567 if (cpu_online(cpu)) {
1568 cpumask_clear_cpu(cpu, &cpus);
1569 pr_warn("cpus_up: cpu%d is already online\n", cpu);
1570 }
1571
1572 for_each_cpu(cpu, &cpus) {
1573 err = try_online_node(cpu_to_node(cpu));
1574 if (err)
1575 return err;
1576 }
1577
1578 cpu_maps_update_begin();
1579
1580 if (cpu_hotplug_disabled) {
1581 err = -EBUSY;
1582 goto out;
1583 }
1584
1585 err = _cpus_up(cpus, 0, CPUHP_ONLINE);
1586 out:
1587 cpu_maps_update_done();
1588 trace_cpus_up_exit(cpumask_first(&cpus));
1589
1590 return err;
1591 }
1592 EXPORT_SYMBOL_GPL(cpus_up);
1593
1594 #ifdef CONFIG_PM_SLEEP_SMP
1595 static cpumask_var_t frozen_cpus;
1596
1597 int freeze_secondary_cpus(int primary)
1598 {
1599 int cpu, error = 0;
1600
1601 cpu_maps_update_begin();
1602 if (!cpu_online(primary))
1603 primary = cpumask_first(cpu_online_mask);
1604 /*
1605 * We take down all of the non-boot CPUs in one shot to avoid races
1606 * with the userspace trying to use the CPU hotplug at the same time
1607 */
1608 cpumask_clear(frozen_cpus);
1609
1610 pr_info("Disabling non-boot CPUs ...\n");
1611 for_each_online_cpu(cpu) {
1612 if (cpu == primary)
1613 continue;
1614 trace_suspend_resume(TPS("CPU_OFF"), cpu, true);
1615 error = _cpu_down(cpu, 1, CPUHP_OFFLINE);
1616 trace_suspend_resume(TPS("CPU_OFF"), cpu, false);
1617 if (!error)
1618 cpumask_set_cpu(cpu, frozen_cpus);
1619 else {
1620 pr_err("Error taking CPU%d down: %d\n", cpu, error);
1621 break;
1622 }
1623 }
1624
1625 if (!error)
1626 BUG_ON(num_online_cpus() > 1);
1627 else
1628 pr_err("Non-boot CPUs are not disabled\n");
1629
1630 /*
1631 * Make sure the CPUs won't be enabled by someone else. We need to do
1632 * this even in case of failure as all disable_nonboot_cpus() users are
1633 * supposed to do enable_nonboot_cpus() on the failure path.
1634 */
1635 cpu_hotplug_disabled++;
1636
1637 cpu_maps_update_done();
1638 return error;
1639 }
1640
1641 void __weak arch_enable_nonboot_cpus_begin(void)
1642 {
1643 }
1644
1645 void __weak arch_enable_nonboot_cpus_end(void)
1646 {
1647 }
1648
1649 void enable_nonboot_cpus(void)
1650 {
1651 int cpu, error;
1652 struct device *cpu_device;
1653
1654 /* Allow everyone to use the CPU hotplug again */
1655 cpu_maps_update_begin();
1656 __cpu_hotplug_enable();
1657 if (cpumask_empty(frozen_cpus))
1658 goto out;
1659
1660 pr_info("Enabling non-boot CPUs ...\n");
1661
1662 arch_enable_nonboot_cpus_begin();
1663
1664 for_each_cpu(cpu, frozen_cpus) {
1665 trace_suspend_resume(TPS("CPU_ON"), cpu, true);
1666 error = _cpu_up(cpu, 1, CPUHP_ONLINE);
1667 trace_suspend_resume(TPS("CPU_ON"), cpu, false);
1668 if (!error) {
1669 pr_info("CPU%d is up\n", cpu);
1670 cpu_device = get_cpu_device(cpu);
1671 if (!cpu_device)
1672 pr_err("%s: failed to get cpu%d device\n",
1673 __func__, cpu);
1674 else
1675 kobject_uevent(&cpu_device->kobj, KOBJ_ONLINE);
1676 continue;
1677 }
1678 pr_warn("Error taking CPU%d up: %d\n", cpu, error);
1679 }
1680
1681 arch_enable_nonboot_cpus_end();
1682
1683 cpumask_clear(frozen_cpus);
1684 out:
1685 cpu_maps_update_done();
1686 }
1687
1688 static int __init alloc_frozen_cpus(void)
1689 {
1690 if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
1691 return -ENOMEM;
1692 return 0;
1693 }
1694 core_initcall(alloc_frozen_cpus);
1695
1696 /*
1697 * When callbacks for CPU hotplug notifications are being executed, we must
1698 * ensure that the state of the system with respect to the tasks being frozen
1699 * or not, as reported by the notification, remains unchanged *throughout the
1700 * duration* of the execution of the callbacks.
1701 * Hence we need to prevent the freezer from racing with regular CPU hotplug.
1702 *
1703 * This synchronization is implemented by mutually excluding regular CPU
1704 * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
1705 * Hibernate notifications.
1706 */
1707 static int
1708 cpu_hotplug_pm_callback(struct notifier_block *nb,
1709 unsigned long action, void *ptr)
1710 {
1711 switch (action) {
1712
1713 case PM_SUSPEND_PREPARE:
1714 case PM_HIBERNATION_PREPARE:
1715 cpu_hotplug_disable();
1716 break;
1717
1718 case PM_POST_SUSPEND:
1719 case PM_POST_HIBERNATION:
1720 cpu_hotplug_enable();
1721 break;
1722
1723 default:
1724 return NOTIFY_DONE;
1725 }
1726
1727 return NOTIFY_OK;
1728 }
1729
1730
1731 struct cpumask cpu_fastoff_mask;
1732 EXPORT_SYMBOL(cpu_fastoff_mask);
1733 struct cpumask cpu_faston_mask;
1734 EXPORT_SYMBOL(cpu_faston_mask);
1735 static int __init cpu_hotplug_pm_sync_init(void)
1736 {
1737 /*
1738 * cpu_hotplug_pm_callback has higher priority than x86
1739 * bsp_pm_callback which depends on cpu_hotplug_pm_callback
1740 * to disable cpu hotplug to avoid cpu hotplug race.
1741 */
1742 pm_notifier(cpu_hotplug_pm_callback, 0);
1743 cpumask_clear(&cpu_fastoff_mask);
1744 cpumask_clear(&cpu_faston_mask);
1745
1746 return 0;
1747 }
1748 core_initcall(cpu_hotplug_pm_sync_init);
1749
1750 #endif /* CONFIG_PM_SLEEP_SMP */
1751
1752 int __boot_cpu_id;
1753
1754 #endif /* CONFIG_SMP */
1755
1756 /* Boot processor state steps */
1757 static struct cpuhp_step cpuhp_bp_states[] = {
1758 [CPUHP_OFFLINE] = {
1759 .name = "offline",
1760 .startup.single = NULL,
1761 .teardown.single = NULL,
1762 },
1763 #ifdef CONFIG_SMP
1764 [CPUHP_CREATE_THREADS]= {
1765 .name = "threads:prepare",
1766 .startup.single = smpboot_create_threads,
1767 .teardown.single = NULL,
1768 .cant_stop = true,
1769 },
1770 [CPUHP_PERF_PREPARE] = {
1771 .name = "perf:prepare",
1772 .startup.single = perf_event_init_cpu,
1773 .teardown.single = perf_event_exit_cpu,
1774 },
1775 [CPUHP_WORKQUEUE_PREP] = {
1776 .name = "workqueue:prepare",
1777 .startup.single = workqueue_prepare_cpu,
1778 .teardown.single = NULL,
1779 },
1780 [CPUHP_HRTIMERS_PREPARE] = {
1781 .name = "hrtimers:prepare",
1782 .startup.single = hrtimers_prepare_cpu,
1783 .teardown.single = hrtimers_dead_cpu,
1784 },
1785 [CPUHP_SMPCFD_PREPARE] = {
1786 .name = "smpcfd:prepare",
1787 .startup.single = smpcfd_prepare_cpu,
1788 .teardown.single = smpcfd_dead_cpu,
1789 },
1790 [CPUHP_RELAY_PREPARE] = {
1791 .name = "relay:prepare",
1792 .startup.single = relay_prepare_cpu,
1793 .teardown.single = NULL,
1794 },
1795 [CPUHP_SLAB_PREPARE] = {
1796 .name = "slab:prepare",
1797 .startup.single = slab_prepare_cpu,
1798 .teardown.single = slab_dead_cpu,
1799 },
1800 [CPUHP_RCUTREE_PREP] = {
1801 .name = "RCU/tree:prepare",
1802 .startup.single = rcutree_prepare_cpu,
1803 .teardown.single = rcutree_dead_cpu,
1804 },
1805 /*
1806 * On the tear-down path, timers_dead_cpu() must be invoked
1807 * before blk_mq_queue_reinit_notify() from notify_dead(),
1808 * otherwise a RCU stall occurs.
1809 */
1810 [CPUHP_TIMERS_PREPARE] = {
1811 .name = "timers:dead",
1812 .startup.single = timers_prepare_cpu,
1813 .teardown.single = timers_dead_cpu,
1814 },
1815 /* Kicks the plugged cpu into life */
1816 [CPUHP_BRINGUP_CPU] = {
1817 .name = "cpu:bringup",
1818 .startup.single = bringup_cpu,
1819 .teardown.single = NULL,
1820 .cant_stop = true,
1821 },
1822 /*
1823 * Handled on controll processor until the plugged processor manages
1824 * this itself.
1825 */
1826 [CPUHP_TEARDOWN_CPU] = {
1827 .name = "cpu:teardown",
1828 .startup.single = NULL,
1829 .teardown.single = takedown_cpu,
1830 .cant_stop = true,
1831 },
1832 #else
1833 [CPUHP_BRINGUP_CPU] = { },
1834 #endif
1835 };
1836
1837 /* Application processor state steps */
1838 static struct cpuhp_step cpuhp_ap_states[] = {
1839 #ifdef CONFIG_SMP
1840 /* Final state before CPU kills itself */
1841 [CPUHP_AP_IDLE_DEAD] = {
1842 .name = "idle:dead",
1843 },
1844 /*
1845 * Last state before CPU enters the idle loop to die. Transient state
1846 * for synchronization.
1847 */
1848 [CPUHP_AP_OFFLINE] = {
1849 .name = "ap:offline",
1850 .cant_stop = true,
1851 },
1852 /* First state is scheduler control. Interrupts are disabled */
1853 [CPUHP_AP_SCHED_STARTING] = {
1854 .name = "sched:starting",
1855 .startup.single = sched_cpu_starting,
1856 .teardown.single = sched_cpu_dying,
1857 },
1858 [CPUHP_AP_RCUTREE_DYING] = {
1859 .name = "RCU/tree:dying",
1860 .startup.single = NULL,
1861 .teardown.single = rcutree_dying_cpu,
1862 },
1863 [CPUHP_AP_SMPCFD_DYING] = {
1864 .name = "smpcfd:dying",
1865 .startup.single = NULL,
1866 .teardown.single = smpcfd_dying_cpu,
1867 },
1868 /* Entry state on starting. Interrupts enabled from here on. Transient
1869 * state for synchronsization */
1870 [CPUHP_AP_ONLINE] = {
1871 .name = "ap:online",
1872 },
1873 /* Handle smpboot threads park/unpark */
1874 [CPUHP_AP_SMPBOOT_THREADS] = {
1875 .name = "smpboot/threads:online",
1876 .startup.single = smpboot_unpark_threads,
1877 .teardown.single = smpboot_park_threads,
1878 },
1879 [CPUHP_AP_IRQ_AFFINITY_ONLINE] = {
1880 .name = "irq/affinity:online",
1881 .startup.single = irq_affinity_online_cpu,
1882 .teardown.single = NULL,
1883 },
1884 [CPUHP_AP_PERF_ONLINE] = {
1885 .name = "perf:online",
1886 .startup.single = perf_event_init_cpu,
1887 .teardown.single = perf_event_exit_cpu,
1888 },
1889 [CPUHP_AP_WORKQUEUE_ONLINE] = {
1890 .name = "workqueue:online",
1891 .startup.single = workqueue_online_cpu,
1892 .teardown.single = workqueue_offline_cpu,
1893 },
1894 [CPUHP_AP_RCUTREE_ONLINE] = {
1895 .name = "RCU/tree:online",
1896 .startup.single = rcutree_online_cpu,
1897 .teardown.single = rcutree_offline_cpu,
1898 },
1899 #endif
1900 /*
1901 * The dynamically registered state space is here
1902 */
1903
1904 #ifdef CONFIG_SMP
1905 /* Last state is scheduler control setting the cpu active */
1906 [CPUHP_AP_ACTIVE] = {
1907 .name = "sched:active",
1908 .startup.single = sched_cpu_activate,
1909 .teardown.single = sched_cpu_deactivate,
1910 },
1911 #endif
1912
1913 /* CPU is fully up and running. */
1914 [CPUHP_ONLINE] = {
1915 .name = "online",
1916 .startup.single = NULL,
1917 .teardown.single = NULL,
1918 },
1919 };
1920
1921 /* Sanity check for callbacks */
1922 static int cpuhp_cb_check(enum cpuhp_state state)
1923 {
1924 if (state <= CPUHP_OFFLINE || state >= CPUHP_ONLINE)
1925 return -EINVAL;
1926 return 0;
1927 }
1928
1929 /*
1930 * Returns a free for dynamic slot assignment of the Online state. The states
1931 * are protected by the cpuhp_slot_states mutex and an empty slot is identified
1932 * by having no name assigned.
1933 */
1934 static int cpuhp_reserve_state(enum cpuhp_state state)
1935 {
1936 enum cpuhp_state i, end;
1937 struct cpuhp_step *step;
1938
1939 switch (state) {
1940 case CPUHP_AP_ONLINE_DYN:
1941 step = cpuhp_ap_states + CPUHP_AP_ONLINE_DYN;
1942 end = CPUHP_AP_ONLINE_DYN_END;
1943 break;
1944 case CPUHP_BP_PREPARE_DYN:
1945 step = cpuhp_bp_states + CPUHP_BP_PREPARE_DYN;
1946 end = CPUHP_BP_PREPARE_DYN_END;
1947 break;
1948 default:
1949 return -EINVAL;
1950 }
1951
1952 for (i = state; i <= end; i++, step++) {
1953 if (!step->name)
1954 return i;
1955 }
1956 WARN(1, "No more dynamic states available for CPU hotplug\n");
1957 return -ENOSPC;
1958 }
1959
1960 static int cpuhp_store_callbacks(enum cpuhp_state state, const char *name,
1961 int (*startup)(unsigned int cpu),
1962 int (*teardown)(unsigned int cpu),
1963 bool multi_instance)
1964 {
1965 /* (Un)Install the callbacks for further cpu hotplug operations */
1966 struct cpuhp_step *sp;
1967 int ret = 0;
1968
1969 /*
1970 * If name is NULL, then the state gets removed.
1971 *
1972 * CPUHP_AP_ONLINE_DYN and CPUHP_BP_PREPARE_DYN are handed out on
1973 * the first allocation from these dynamic ranges, so the removal
1974 * would trigger a new allocation and clear the wrong (already
1975 * empty) state, leaving the callbacks of the to be cleared state
1976 * dangling, which causes wreckage on the next hotplug operation.
1977 */
1978 if (name && (state == CPUHP_AP_ONLINE_DYN ||
1979 state == CPUHP_BP_PREPARE_DYN)) {
1980 ret = cpuhp_reserve_state(state);
1981 if (ret < 0)
1982 return ret;
1983 state = ret;
1984 }
1985 sp = cpuhp_get_step(state);
1986 if (name && sp->name)
1987 return -EBUSY;
1988
1989 sp->startup.single = startup;
1990 sp->teardown.single = teardown;
1991 sp->name = name;
1992 sp->multi_instance = multi_instance;
1993 INIT_HLIST_HEAD(&sp->list);
1994 return ret;
1995 }
1996
1997 static void *cpuhp_get_teardown_cb(enum cpuhp_state state)
1998 {
1999 return cpuhp_get_step(state)->teardown.single;
2000 }
2001
2002 /*
2003 * Call the startup/teardown function for a step either on the AP or
2004 * on the current CPU.
2005 */
2006 static int cpuhp_issue_call(int cpu, enum cpuhp_state state, bool bringup,
2007 struct hlist_node *node)
2008 {
2009 struct cpuhp_step *sp = cpuhp_get_step(state);
2010 int ret;
2011
2012 /*
2013 * If there's nothing to do, we done.
2014 * Relies on the union for multi_instance.
2015 */
2016 if ((bringup && !sp->startup.single) ||
2017 (!bringup && !sp->teardown.single))
2018 return 0;
2019 /*
2020 * The non AP bound callbacks can fail on bringup. On teardown
2021 * e.g. module removal we crash for now.
2022 */
2023 #ifdef CONFIG_SMP
2024 if (cpuhp_is_ap_state(state))
2025 ret = cpuhp_invoke_ap_callback(cpu, state, bringup, node);
2026 else
2027 ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
2028 #else
2029 ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
2030 #endif
2031 BUG_ON(ret && !bringup);
2032 return ret;
2033 }
2034
2035 /*
2036 * Called from __cpuhp_setup_state on a recoverable failure.
2037 *
2038 * Note: The teardown callbacks for rollback are not allowed to fail!
2039 */
2040 static void cpuhp_rollback_install(int failedcpu, enum cpuhp_state state,
2041 struct hlist_node *node)
2042 {
2043 int cpu;
2044
2045 /* Roll back the already executed steps on the other cpus */
2046 for_each_present_cpu(cpu) {
2047 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
2048 int cpustate = st->state;
2049
2050 if (cpu >= failedcpu)
2051 break;
2052
2053 /* Did we invoke the startup call on that cpu ? */
2054 if (cpustate >= state)
2055 cpuhp_issue_call(cpu, state, false, node);
2056 }
2057 }
2058
2059 int __cpuhp_state_add_instance_cpuslocked(enum cpuhp_state state,
2060 struct hlist_node *node,
2061 bool invoke)
2062 {
2063 struct cpuhp_step *sp;
2064 int cpu;
2065 int ret;
2066
2067 lockdep_assert_cpus_held();
2068
2069 sp = cpuhp_get_step(state);
2070 if (sp->multi_instance == false)
2071 return -EINVAL;
2072
2073 mutex_lock(&cpuhp_state_mutex);
2074
2075 if (!invoke || !sp->startup.multi)
2076 goto add_node;
2077
2078 /*
2079 * Try to call the startup callback for each present cpu
2080 * depending on the hotplug state of the cpu.
2081 */
2082 for_each_present_cpu(cpu) {
2083 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
2084 int cpustate = st->state;
2085
2086 if (cpustate < state)
2087 continue;
2088
2089 ret = cpuhp_issue_call(cpu, state, true, node);
2090 if (ret) {
2091 if (sp->teardown.multi)
2092 cpuhp_rollback_install(cpu, state, node);
2093 goto unlock;
2094 }
2095 }
2096 add_node:
2097 ret = 0;
2098 hlist_add_head(node, &sp->list);
2099 unlock:
2100 mutex_unlock(&cpuhp_state_mutex);
2101 return ret;
2102 }
2103
2104 int __cpuhp_state_add_instance(enum cpuhp_state state, struct hlist_node *node,
2105 bool invoke)
2106 {
2107 int ret;
2108
2109 cpus_read_lock();
2110 ret = __cpuhp_state_add_instance_cpuslocked(state, node, invoke);
2111 cpus_read_unlock();
2112 return ret;
2113 }
2114 EXPORT_SYMBOL_GPL(__cpuhp_state_add_instance);
2115
2116 /**
2117 * __cpuhp_setup_state_cpuslocked - Setup the callbacks for an hotplug machine state
2118 * @state: The state to setup
2119 * @invoke: If true, the startup function is invoked for cpus where
2120 * cpu state >= @state
2121 * @startup: startup callback function
2122 * @teardown: teardown callback function
2123 * @multi_instance: State is set up for multiple instances which get
2124 * added afterwards.
2125 *
2126 * The caller needs to hold cpus read locked while calling this function.
2127 * Returns:
2128 * On success:
2129 * Positive state number if @state is CPUHP_AP_ONLINE_DYN
2130 * 0 for all other states
2131 * On failure: proper (negative) error code
2132 */
2133 int __cpuhp_setup_state_cpuslocked(enum cpuhp_state state,
2134 const char *name, bool invoke,
2135 int (*startup)(unsigned int cpu),
2136 int (*teardown)(unsigned int cpu),
2137 bool multi_instance)
2138 {
2139 int cpu, ret = 0;
2140 bool dynstate;
2141
2142 lockdep_assert_cpus_held();
2143
2144 if (cpuhp_cb_check(state) || !name)
2145 return -EINVAL;
2146
2147 mutex_lock(&cpuhp_state_mutex);
2148
2149 ret = cpuhp_store_callbacks(state, name, startup, teardown,
2150 multi_instance);
2151
2152 dynstate = state == CPUHP_AP_ONLINE_DYN;
2153 if (ret > 0 && dynstate) {
2154 state = ret;
2155 ret = 0;
2156 }
2157
2158 if (ret || !invoke || !startup)
2159 goto out;
2160
2161 /*
2162 * Try to call the startup callback for each present cpu
2163 * depending on the hotplug state of the cpu.
2164 */
2165 for_each_present_cpu(cpu) {
2166 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
2167 int cpustate = st->state;
2168
2169 if (cpustate < state)
2170 continue;
2171
2172 ret = cpuhp_issue_call(cpu, state, true, NULL);
2173 if (ret) {
2174 if (teardown)
2175 cpuhp_rollback_install(cpu, state, NULL);
2176 cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
2177 goto out;
2178 }
2179 }
2180 out:
2181 mutex_unlock(&cpuhp_state_mutex);
2182 /*
2183 * If the requested state is CPUHP_AP_ONLINE_DYN, return the
2184 * dynamically allocated state in case of success.
2185 */
2186 if (!ret && dynstate)
2187 return state;
2188 return ret;
2189 }
2190 EXPORT_SYMBOL(__cpuhp_setup_state_cpuslocked);
2191
2192 int __cpuhp_setup_state(enum cpuhp_state state,
2193 const char *name, bool invoke,
2194 int (*startup)(unsigned int cpu),
2195 int (*teardown)(unsigned int cpu),
2196 bool multi_instance)
2197 {
2198 int ret;
2199
2200 cpus_read_lock();
2201 ret = __cpuhp_setup_state_cpuslocked(state, name, invoke, startup,
2202 teardown, multi_instance);
2203 cpus_read_unlock();
2204 return ret;
2205 }
2206 EXPORT_SYMBOL(__cpuhp_setup_state);
2207
2208 int __cpuhp_state_remove_instance(enum cpuhp_state state,
2209 struct hlist_node *node, bool invoke)
2210 {
2211 struct cpuhp_step *sp = cpuhp_get_step(state);
2212 int cpu;
2213
2214 BUG_ON(cpuhp_cb_check(state));
2215
2216 if (!sp->multi_instance)
2217 return -EINVAL;
2218
2219 cpus_read_lock();
2220 mutex_lock(&cpuhp_state_mutex);
2221
2222 if (!invoke || !cpuhp_get_teardown_cb(state))
2223 goto remove;
2224 /*
2225 * Call the teardown callback for each present cpu depending
2226 * on the hotplug state of the cpu. This function is not
2227 * allowed to fail currently!
2228 */
2229 for_each_present_cpu(cpu) {
2230 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
2231 int cpustate = st->state;
2232
2233 if (cpustate >= state)
2234 cpuhp_issue_call(cpu, state, false, node);
2235 }
2236
2237 remove:
2238 hlist_del(node);
2239 mutex_unlock(&cpuhp_state_mutex);
2240 cpus_read_unlock();
2241
2242 return 0;
2243 }
2244 EXPORT_SYMBOL_GPL(__cpuhp_state_remove_instance);
2245
2246 /**
2247 * __cpuhp_remove_state_cpuslocked - Remove the callbacks for an hotplug machine state
2248 * @state: The state to remove
2249 * @invoke: If true, the teardown function is invoked for cpus where
2250 * cpu state >= @state
2251 *
2252 * The caller needs to hold cpus read locked while calling this function.
2253 * The teardown callback is currently not allowed to fail. Think
2254 * about module removal!
2255 */
2256 void __cpuhp_remove_state_cpuslocked(enum cpuhp_state state, bool invoke)
2257 {
2258 struct cpuhp_step *sp = cpuhp_get_step(state);
2259 int cpu;
2260
2261 BUG_ON(cpuhp_cb_check(state));
2262
2263 lockdep_assert_cpus_held();
2264
2265 mutex_lock(&cpuhp_state_mutex);
2266 if (sp->multi_instance) {
2267 WARN(!hlist_empty(&sp->list),
2268 "Error: Removing state %d which has instances left.\n",
2269 state);
2270 goto remove;
2271 }
2272
2273 if (!invoke || !cpuhp_get_teardown_cb(state))
2274 goto remove;
2275
2276 /*
2277 * Call the teardown callback for each present cpu depending
2278 * on the hotplug state of the cpu. This function is not
2279 * allowed to fail currently!
2280 */
2281 for_each_present_cpu(cpu) {
2282 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
2283 int cpustate = st->state;
2284
2285 if (cpustate >= state)
2286 cpuhp_issue_call(cpu, state, false, NULL);
2287 }
2288 remove:
2289 cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
2290 mutex_unlock(&cpuhp_state_mutex);
2291 }
2292 EXPORT_SYMBOL(__cpuhp_remove_state_cpuslocked);
2293
2294 void __cpuhp_remove_state(enum cpuhp_state state, bool invoke)
2295 {
2296 cpus_read_lock();
2297 __cpuhp_remove_state_cpuslocked(state, invoke);
2298 cpus_read_unlock();
2299 }
2300 EXPORT_SYMBOL(__cpuhp_remove_state);
2301
2302 #if defined(CONFIG_SYSFS) && defined(CONFIG_HOTPLUG_CPU)
2303 static ssize_t show_cpuhp_state(struct device *dev,
2304 struct device_attribute *attr, char *buf)
2305 {
2306 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
2307
2308 return sprintf(buf, "%d\n", st->state);
2309 }
2310 static DEVICE_ATTR(state, 0444, show_cpuhp_state, NULL);
2311
2312 static ssize_t write_cpuhp_target(struct device *dev,
2313 struct device_attribute *attr,
2314 const char *buf, size_t count)
2315 {
2316 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
2317 struct cpuhp_step *sp;
2318 int target, ret;
2319
2320 ret = kstrtoint(buf, 10, &target);
2321 if (ret)
2322 return ret;
2323
2324 #ifdef CONFIG_CPU_HOTPLUG_STATE_CONTROL
2325 if (target < CPUHP_OFFLINE || target > CPUHP_ONLINE)
2326 return -EINVAL;
2327 #else
2328 if (target != CPUHP_OFFLINE && target != CPUHP_ONLINE)
2329 return -EINVAL;
2330 #endif
2331
2332 ret = lock_device_hotplug_sysfs();
2333 if (ret)
2334 return ret;
2335
2336 mutex_lock(&cpuhp_state_mutex);
2337 sp = cpuhp_get_step(target);
2338 ret = !sp->name || sp->cant_stop ? -EINVAL : 0;
2339 mutex_unlock(&cpuhp_state_mutex);
2340 if (ret)
2341 goto out;
2342
2343 if (st->state < target)
2344 ret = do_cpu_up(dev->id, target);
2345 else
2346 ret = do_cpu_down(dev->id, target);
2347 out:
2348 unlock_device_hotplug();
2349 return ret ? ret : count;
2350 }
2351
2352 static ssize_t show_cpuhp_target(struct device *dev,
2353 struct device_attribute *attr, char *buf)
2354 {
2355 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
2356
2357 return sprintf(buf, "%d\n", st->target);
2358 }
2359 static DEVICE_ATTR(target, 0644, show_cpuhp_target, write_cpuhp_target);
2360
2361
2362 static ssize_t write_cpuhp_fail(struct device *dev,
2363 struct device_attribute *attr,
2364 const char *buf, size_t count)
2365 {
2366 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
2367 struct cpuhp_step *sp;
2368 int fail, ret;
2369
2370 ret = kstrtoint(buf, 10, &fail);
2371 if (ret)
2372 return ret;
2373
2374 /*
2375 * Cannot fail STARTING/DYING callbacks.
2376 */
2377 if (cpuhp_is_atomic_state(fail))
2378 return -EINVAL;
2379
2380 /*
2381 * Cannot fail anything that doesn't have callbacks.
2382 */
2383 mutex_lock(&cpuhp_state_mutex);
2384 sp = cpuhp_get_step(fail);
2385 if (!sp->startup.single && !sp->teardown.single)
2386 ret = -EINVAL;
2387 mutex_unlock(&cpuhp_state_mutex);
2388 if (ret)
2389 return ret;
2390
2391 st->fail = fail;
2392
2393 return count;
2394 }
2395
2396 static ssize_t show_cpuhp_fail(struct device *dev,
2397 struct device_attribute *attr, char *buf)
2398 {
2399 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
2400
2401 return sprintf(buf, "%d\n", st->fail);
2402 }
2403
2404 static DEVICE_ATTR(fail, 0644, show_cpuhp_fail, write_cpuhp_fail);
2405
2406 static struct attribute *cpuhp_cpu_attrs[] = {
2407 &dev_attr_state.attr,
2408 &dev_attr_target.attr,
2409 &dev_attr_fail.attr,
2410 NULL
2411 };
2412
2413 static const struct attribute_group cpuhp_cpu_attr_group = {
2414 .attrs = cpuhp_cpu_attrs,
2415 .name = "hotplug",
2416 NULL
2417 };
2418
2419 static ssize_t show_cpuhp_states(struct device *dev,
2420 struct device_attribute *attr, char *buf)
2421 {
2422 ssize_t cur, res = 0;
2423 int i;
2424
2425 mutex_lock(&cpuhp_state_mutex);
2426 for (i = CPUHP_OFFLINE; i <= CPUHP_ONLINE; i++) {
2427 struct cpuhp_step *sp = cpuhp_get_step(i);
2428
2429 if (sp->name) {
2430 cur = sprintf(buf, "%3d: %s\n", i, sp->name);
2431 buf += cur;
2432 res += cur;
2433 }
2434 }
2435 mutex_unlock(&cpuhp_state_mutex);
2436 return res;
2437 }
2438 static DEVICE_ATTR(states, 0444, show_cpuhp_states, NULL);
2439
2440 static struct attribute *cpuhp_cpu_root_attrs[] = {
2441 &dev_attr_states.attr,
2442 NULL
2443 };
2444
2445 static const struct attribute_group cpuhp_cpu_root_attr_group = {
2446 .attrs = cpuhp_cpu_root_attrs,
2447 .name = "hotplug",
2448 NULL
2449 };
2450
2451 #ifdef CONFIG_HOTPLUG_SMT
2452
2453 static const char *smt_states[] = {
2454 [CPU_SMT_ENABLED] = "on",
2455 [CPU_SMT_DISABLED] = "off",
2456 [CPU_SMT_FORCE_DISABLED] = "forceoff",
2457 [CPU_SMT_NOT_SUPPORTED] = "notsupported",
2458 };
2459
2460 static ssize_t
2461 show_smt_control(struct device *dev, struct device_attribute *attr, char *buf)
2462 {
2463 return snprintf(buf, PAGE_SIZE - 2, "%s\n", smt_states[cpu_smt_control]);
2464 }
2465
2466 static void cpuhp_offline_cpu_device(unsigned int cpu)
2467 {
2468 struct device *dev = get_cpu_device(cpu);
2469
2470 dev->offline = true;
2471 /* Tell user space about the state change */
2472 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2473 }
2474
2475 static void cpuhp_online_cpu_device(unsigned int cpu)
2476 {
2477 struct device *dev = get_cpu_device(cpu);
2478
2479 dev->offline = false;
2480 /* Tell user space about the state change */
2481 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2482 }
2483
2484 static int cpuhp_smt_disable(enum cpuhp_smt_control ctrlval)
2485 {
2486 int cpu, ret = 0;
2487
2488 cpu_maps_update_begin();
2489 for_each_online_cpu(cpu) {
2490 if (topology_is_primary_thread(cpu))
2491 continue;
2492 ret = cpu_down_maps_locked(cpu, CPUHP_OFFLINE);
2493 if (ret)
2494 break;
2495 /*
2496 * As this needs to hold the cpu maps lock it's impossible
2497 * to call device_offline() because that ends up calling
2498 * cpu_down() which takes cpu maps lock. cpu maps lock
2499 * needs to be held as this might race against in kernel
2500 * abusers of the hotplug machinery (thermal management).
2501 *
2502 * So nothing would update device:offline state. That would
2503 * leave the sysfs entry stale and prevent onlining after
2504 * smt control has been changed to 'off' again. This is
2505 * called under the sysfs hotplug lock, so it is properly
2506 * serialized against the regular offline usage.
2507 */
2508 cpuhp_offline_cpu_device(cpu);
2509 }
2510 if (!ret) {
2511 cpu_smt_control = ctrlval;
2512 arch_smt_update();
2513 }
2514 cpu_maps_update_done();
2515 return ret;
2516 }
2517
2518 static int cpuhp_smt_enable(void)
2519 {
2520 int cpu, ret = 0;
2521
2522 cpu_maps_update_begin();
2523 cpu_smt_control = CPU_SMT_ENABLED;
2524 arch_smt_update();
2525 for_each_present_cpu(cpu) {
2526 /* Skip online CPUs and CPUs on offline nodes */
2527 if (cpu_online(cpu) || !node_online(cpu_to_node(cpu)))
2528 continue;
2529 ret = _cpu_up(cpu, 0, CPUHP_ONLINE);
2530 if (ret)
2531 break;
2532 /* See comment in cpuhp_smt_disable() */
2533 cpuhp_online_cpu_device(cpu);
2534 }
2535 cpu_maps_update_done();
2536 return ret;
2537 }
2538
2539 static ssize_t
2540 store_smt_control(struct device *dev, struct device_attribute *attr,
2541 const char *buf, size_t count)
2542 {
2543 int ctrlval, ret;
2544
2545 if (sysfs_streq(buf, "on"))
2546 ctrlval = CPU_SMT_ENABLED;
2547 else if (sysfs_streq(buf, "off"))
2548 ctrlval = CPU_SMT_DISABLED;
2549 else if (sysfs_streq(buf, "forceoff"))
2550 ctrlval = CPU_SMT_FORCE_DISABLED;
2551 else
2552 return -EINVAL;
2553
2554 if (cpu_smt_control == CPU_SMT_FORCE_DISABLED)
2555 return -EPERM;
2556
2557 if (cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
2558 return -ENODEV;
2559
2560 ret = lock_device_hotplug_sysfs();
2561 if (ret)
2562 return ret;
2563
2564 if (ctrlval != cpu_smt_control) {
2565 switch (ctrlval) {
2566 case CPU_SMT_ENABLED:
2567 ret = cpuhp_smt_enable();
2568 break;
2569 case CPU_SMT_DISABLED:
2570 case CPU_SMT_FORCE_DISABLED:
2571 ret = cpuhp_smt_disable(ctrlval);
2572 break;
2573 }
2574 }
2575
2576 unlock_device_hotplug();
2577 return ret ? ret : count;
2578 }
2579 static DEVICE_ATTR(control, 0644, show_smt_control, store_smt_control);
2580
2581 static ssize_t
2582 show_smt_active(struct device *dev, struct device_attribute *attr, char *buf)
2583 {
2584 bool active = topology_max_smt_threads() > 1;
2585
2586 return snprintf(buf, PAGE_SIZE - 2, "%d\n", active);
2587 }
2588 static DEVICE_ATTR(active, 0444, show_smt_active, NULL);
2589
2590 static struct attribute *cpuhp_smt_attrs[] = {
2591 &dev_attr_control.attr,
2592 &dev_attr_active.attr,
2593 NULL
2594 };
2595
2596 static const struct attribute_group cpuhp_smt_attr_group = {
2597 .attrs = cpuhp_smt_attrs,
2598 .name = "smt",
2599 NULL
2600 };
2601
2602 static int __init cpu_smt_state_init(void)
2603 {
2604 return sysfs_create_group(&cpu_subsys.dev_root->kobj,
2605 &cpuhp_smt_attr_group);
2606 }
2607
2608 #else
2609 static inline int cpu_smt_state_init(void) { return 0; }
2610 #endif
2611
2612 static int __init cpuhp_sysfs_init(void)
2613 {
2614 int cpu, ret;
2615
2616 ret = cpu_smt_state_init();
2617 if (ret)
2618 return ret;
2619
2620 ret = sysfs_create_group(&cpu_subsys.dev_root->kobj,
2621 &cpuhp_cpu_root_attr_group);
2622 if (ret)
2623 return ret;
2624
2625 for_each_possible_cpu(cpu) {
2626 struct device *dev = get_cpu_device(cpu);
2627
2628 if (!dev)
2629 continue;
2630 ret = sysfs_create_group(&dev->kobj, &cpuhp_cpu_attr_group);
2631 if (ret)
2632 return ret;
2633 }
2634 return 0;
2635 }
2636 device_initcall(cpuhp_sysfs_init);
2637 #endif
2638
2639 /*
2640 * cpu_bit_bitmap[] is a special, "compressed" data structure that
2641 * represents all NR_CPUS bits binary values of 1<<nr.
2642 *
2643 * It is used by cpumask_of() to get a constant address to a CPU
2644 * mask value that has a single bit set only.
2645 */
2646
2647 /* cpu_bit_bitmap[0] is empty - so we can back into it */
2648 #define MASK_DECLARE_1(x) [x+1][0] = (1UL << (x))
2649 #define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
2650 #define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
2651 #define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
2652
2653 const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
2654
2655 MASK_DECLARE_8(0), MASK_DECLARE_8(8),
2656 MASK_DECLARE_8(16), MASK_DECLARE_8(24),
2657 #if BITS_PER_LONG > 32
2658 MASK_DECLARE_8(32), MASK_DECLARE_8(40),
2659 MASK_DECLARE_8(48), MASK_DECLARE_8(56),
2660 #endif
2661 };
2662 EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
2663
2664 const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
2665 EXPORT_SYMBOL(cpu_all_bits);
2666
2667 #ifdef CONFIG_INIT_ALL_POSSIBLE
2668 struct cpumask __cpu_possible_mask __read_mostly
2669 = {CPU_BITS_ALL};
2670 #else
2671 struct cpumask __cpu_possible_mask __read_mostly;
2672 #endif
2673 EXPORT_SYMBOL(__cpu_possible_mask);
2674
2675 struct cpumask __cpu_online_mask __read_mostly;
2676 EXPORT_SYMBOL(__cpu_online_mask);
2677
2678 struct cpumask __cpu_present_mask __read_mostly;
2679 EXPORT_SYMBOL(__cpu_present_mask);
2680
2681 struct cpumask __cpu_active_mask __read_mostly;
2682 EXPORT_SYMBOL(__cpu_active_mask);
2683
2684 void init_cpu_present(const struct cpumask *src)
2685 {
2686 cpumask_copy(&__cpu_present_mask, src);
2687 }
2688
2689 void init_cpu_possible(const struct cpumask *src)
2690 {
2691 cpumask_copy(&__cpu_possible_mask, src);
2692 }
2693
2694 void init_cpu_online(const struct cpumask *src)
2695 {
2696 cpumask_copy(&__cpu_online_mask, src);
2697 }
2698
2699 /*
2700 * Activate the first processor.
2701 */
2702 void __init boot_cpu_init(void)
2703 {
2704 int cpu = smp_processor_id();
2705
2706 /* Mark the boot cpu "present", "online" etc for SMP and UP case */
2707 set_cpu_online(cpu, true);
2708 set_cpu_active(cpu, true);
2709 set_cpu_present(cpu, true);
2710 set_cpu_possible(cpu, true);
2711
2712 #ifdef CONFIG_SMP
2713 __boot_cpu_id = cpu;
2714 #endif
2715 }
2716
2717 /*
2718 * Must be called _AFTER_ setting up the per_cpu areas
2719 */
2720 void __init boot_cpu_hotplug_init(void)
2721 {
2722 #ifdef CONFIG_SMP
2723 this_cpu_write(cpuhp_state.booted_once, true);
2724 #endif
2725 this_cpu_write(cpuhp_state.state, CPUHP_ONLINE);
2726 }