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