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/delay.h>
21 #include <linux/export.h>
22 #include <linux/bug.h>
23 #include <linux/kthread.h>
24 #include <linux/stop_machine.h>
25 #include <linux/mutex.h>
26 #include <linux/gfp.h>
27 #include <linux/suspend.h>
28 #include <linux/lockdep.h>
29 #include <linux/tick.h>
30 #include <linux/irq.h>
31 #include <linux/nmi.h>
32 #include <linux/smpboot.h>
33 #include <linux/relay.h>
34 #include <linux/slab.h>
35 #include <linux/scs.h>
36 #include <linux/percpu-rwsem.h>
37 #include <linux/cpuset.h>
38 #include <linux/random.h>
39 #include <linux/cc_platform.h>
40
41 #include <trace/events/power.h>
42 #define CREATE_TRACE_POINTS
43 #include <trace/events/cpuhp.h>
44
45 #include "smpboot.h"
46
47 /**
48 * struct cpuhp_cpu_state - Per cpu hotplug state storage
49 * @state: The current cpu state
50 * @target: The target state
51 * @fail: Current CPU hotplug callback state
52 * @thread: Pointer to the hotplug thread
53 * @should_run: Thread should execute
54 * @rollback: Perform a rollback
55 * @single: Single callback invocation
56 * @bringup: Single callback bringup or teardown selector
57 * @node: Remote CPU node; for multi-instance, do a
58 * single entry callback for install/remove
59 * @last: For multi-instance rollback, remember how far we got
60 * @cb_state: The state for a single callback (install/uninstall)
61 * @result: Result of the operation
62 * @ap_sync_state: State for AP synchronization
63 * @done_up: Signal completion to the issuer of the task for cpu-up
64 * @done_down: Signal completion to the issuer of the task for cpu-down
65 */
66 struct cpuhp_cpu_state {
67 enum cpuhp_state state;
68 enum cpuhp_state target;
69 enum cpuhp_state fail;
70 #ifdef CONFIG_SMP
71 struct task_struct *thread;
72 bool should_run;
73 bool rollback;
74 bool single;
75 bool bringup;
76 struct hlist_node *node;
77 struct hlist_node *last;
78 enum cpuhp_state cb_state;
79 int result;
80 atomic_t ap_sync_state;
81 struct completion done_up;
82 struct completion done_down;
83 #endif
84 };
85
86 static DEFINE_PER_CPU(struct cpuhp_cpu_state, cpuhp_state) = {
87 .fail = CPUHP_INVALID,
88 };
89
90 #ifdef CONFIG_SMP
91 cpumask_t cpus_booted_once_mask;
92 #endif
93
94 #if defined(CONFIG_LOCKDEP) && defined(CONFIG_SMP)
95 static struct lockdep_map cpuhp_state_up_map =
96 STATIC_LOCKDEP_MAP_INIT("cpuhp_state-up", &cpuhp_state_up_map);
97 static struct lockdep_map cpuhp_state_down_map =
98 STATIC_LOCKDEP_MAP_INIT("cpuhp_state-down", &cpuhp_state_down_map);
99
100
cpuhp_lock_acquire(bool bringup)101 static inline void cpuhp_lock_acquire(bool bringup)
102 {
103 lock_map_acquire(bringup ? &cpuhp_state_up_map : &cpuhp_state_down_map);
104 }
105
cpuhp_lock_release(bool bringup)106 static inline void cpuhp_lock_release(bool bringup)
107 {
108 lock_map_release(bringup ? &cpuhp_state_up_map : &cpuhp_state_down_map);
109 }
110 #else
111
cpuhp_lock_acquire(bool bringup)112 static inline void cpuhp_lock_acquire(bool bringup) { }
cpuhp_lock_release(bool bringup)113 static inline void cpuhp_lock_release(bool bringup) { }
114
115 #endif
116
117 /**
118 * struct cpuhp_step - Hotplug state machine step
119 * @name: Name of the step
120 * @startup: Startup function of the step
121 * @teardown: Teardown function of the step
122 * @cant_stop: Bringup/teardown can't be stopped at this step
123 * @multi_instance: State has multiple instances which get added afterwards
124 */
125 struct cpuhp_step {
126 const char *name;
127 union {
128 int (*single)(unsigned int cpu);
129 int (*multi)(unsigned int cpu,
130 struct hlist_node *node);
131 } startup;
132 union {
133 int (*single)(unsigned int cpu);
134 int (*multi)(unsigned int cpu,
135 struct hlist_node *node);
136 } teardown;
137 /* private: */
138 struct hlist_head list;
139 /* public: */
140 bool cant_stop;
141 bool multi_instance;
142 };
143
144 static DEFINE_MUTEX(cpuhp_state_mutex);
145 static struct cpuhp_step cpuhp_hp_states[];
146
cpuhp_get_step(enum cpuhp_state state)147 static struct cpuhp_step *cpuhp_get_step(enum cpuhp_state state)
148 {
149 return cpuhp_hp_states + state;
150 }
151
cpuhp_step_empty(bool bringup,struct cpuhp_step * step)152 static bool cpuhp_step_empty(bool bringup, struct cpuhp_step *step)
153 {
154 return bringup ? !step->startup.single : !step->teardown.single;
155 }
156
157 /**
158 * cpuhp_invoke_callback - Invoke the callbacks for a given state
159 * @cpu: The cpu for which the callback should be invoked
160 * @state: The state to do callbacks for
161 * @bringup: True if the bringup callback should be invoked
162 * @node: For multi-instance, do a single entry callback for install/remove
163 * @lastp: For multi-instance rollback, remember how far we got
164 *
165 * Called from cpu hotplug and from the state register machinery.
166 *
167 * Return: %0 on success or a negative errno code
168 */
cpuhp_invoke_callback(unsigned int cpu,enum cpuhp_state state,bool bringup,struct hlist_node * node,struct hlist_node ** lastp)169 static int cpuhp_invoke_callback(unsigned int cpu, enum cpuhp_state state,
170 bool bringup, struct hlist_node *node,
171 struct hlist_node **lastp)
172 {
173 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
174 struct cpuhp_step *step = cpuhp_get_step(state);
175 int (*cbm)(unsigned int cpu, struct hlist_node *node);
176 int (*cb)(unsigned int cpu);
177 int ret, cnt;
178
179 if (st->fail == state) {
180 st->fail = CPUHP_INVALID;
181 return -EAGAIN;
182 }
183
184 if (cpuhp_step_empty(bringup, step)) {
185 WARN_ON_ONCE(1);
186 return 0;
187 }
188
189 if (!step->multi_instance) {
190 WARN_ON_ONCE(lastp && *lastp);
191 cb = bringup ? step->startup.single : step->teardown.single;
192
193 trace_cpuhp_enter(cpu, st->target, state, cb);
194 ret = cb(cpu);
195 trace_cpuhp_exit(cpu, st->state, state, ret);
196 return ret;
197 }
198 cbm = bringup ? step->startup.multi : step->teardown.multi;
199
200 /* Single invocation for instance add/remove */
201 if (node) {
202 WARN_ON_ONCE(lastp && *lastp);
203 trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
204 ret = cbm(cpu, node);
205 trace_cpuhp_exit(cpu, st->state, state, ret);
206 return ret;
207 }
208
209 /* State transition. Invoke on all instances */
210 cnt = 0;
211 hlist_for_each(node, &step->list) {
212 if (lastp && node == *lastp)
213 break;
214
215 trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
216 ret = cbm(cpu, node);
217 trace_cpuhp_exit(cpu, st->state, state, ret);
218 if (ret) {
219 if (!lastp)
220 goto err;
221
222 *lastp = node;
223 return ret;
224 }
225 cnt++;
226 }
227 if (lastp)
228 *lastp = NULL;
229 return 0;
230 err:
231 /* Rollback the instances if one failed */
232 cbm = !bringup ? step->startup.multi : step->teardown.multi;
233 if (!cbm)
234 return ret;
235
236 hlist_for_each(node, &step->list) {
237 if (!cnt--)
238 break;
239
240 trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
241 ret = cbm(cpu, node);
242 trace_cpuhp_exit(cpu, st->state, state, ret);
243 /*
244 * Rollback must not fail,
245 */
246 WARN_ON_ONCE(ret);
247 }
248 return ret;
249 }
250
251 #ifdef CONFIG_SMP
cpuhp_is_ap_state(enum cpuhp_state state)252 static bool cpuhp_is_ap_state(enum cpuhp_state state)
253 {
254 /*
255 * The extra check for CPUHP_TEARDOWN_CPU is only for documentation
256 * purposes as that state is handled explicitly in cpu_down.
257 */
258 return state > CPUHP_BRINGUP_CPU && state != CPUHP_TEARDOWN_CPU;
259 }
260
wait_for_ap_thread(struct cpuhp_cpu_state * st,bool bringup)261 static inline void wait_for_ap_thread(struct cpuhp_cpu_state *st, bool bringup)
262 {
263 struct completion *done = bringup ? &st->done_up : &st->done_down;
264 wait_for_completion(done);
265 }
266
complete_ap_thread(struct cpuhp_cpu_state * st,bool bringup)267 static inline void complete_ap_thread(struct cpuhp_cpu_state *st, bool bringup)
268 {
269 struct completion *done = bringup ? &st->done_up : &st->done_down;
270 complete(done);
271 }
272
273 /*
274 * The former STARTING/DYING states, ran with IRQs disabled and must not fail.
275 */
cpuhp_is_atomic_state(enum cpuhp_state state)276 static bool cpuhp_is_atomic_state(enum cpuhp_state state)
277 {
278 return CPUHP_AP_IDLE_DEAD <= state && state < CPUHP_AP_ONLINE;
279 }
280
281 /* Synchronization state management */
282 enum cpuhp_sync_state {
283 SYNC_STATE_DEAD,
284 SYNC_STATE_KICKED,
285 SYNC_STATE_SHOULD_DIE,
286 SYNC_STATE_ALIVE,
287 SYNC_STATE_SHOULD_ONLINE,
288 SYNC_STATE_ONLINE,
289 };
290
291 #ifdef CONFIG_HOTPLUG_CORE_SYNC
292 /**
293 * cpuhp_ap_update_sync_state - Update synchronization state during bringup/teardown
294 * @state: The synchronization state to set
295 *
296 * No synchronization point. Just update of the synchronization state, but implies
297 * a full barrier so that the AP changes are visible before the control CPU proceeds.
298 */
cpuhp_ap_update_sync_state(enum cpuhp_sync_state state)299 static inline void cpuhp_ap_update_sync_state(enum cpuhp_sync_state state)
300 {
301 atomic_t *st = this_cpu_ptr(&cpuhp_state.ap_sync_state);
302
303 (void)atomic_xchg(st, state);
304 }
305
arch_cpuhp_sync_state_poll(void)306 void __weak arch_cpuhp_sync_state_poll(void) { cpu_relax(); }
307
cpuhp_wait_for_sync_state(unsigned int cpu,enum cpuhp_sync_state state,enum cpuhp_sync_state next_state)308 static bool cpuhp_wait_for_sync_state(unsigned int cpu, enum cpuhp_sync_state state,
309 enum cpuhp_sync_state next_state)
310 {
311 atomic_t *st = per_cpu_ptr(&cpuhp_state.ap_sync_state, cpu);
312 ktime_t now, end, start = ktime_get();
313 int sync;
314
315 end = start + 10ULL * NSEC_PER_SEC;
316
317 sync = atomic_read(st);
318 while (1) {
319 if (sync == state) {
320 if (!atomic_try_cmpxchg(st, &sync, next_state))
321 continue;
322 return true;
323 }
324
325 now = ktime_get();
326 if (now > end) {
327 /* Timeout. Leave the state unchanged */
328 return false;
329 } else if (now - start < NSEC_PER_MSEC) {
330 /* Poll for one millisecond */
331 arch_cpuhp_sync_state_poll();
332 } else {
333 usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
334 }
335 sync = atomic_read(st);
336 }
337 return true;
338 }
339 #else /* CONFIG_HOTPLUG_CORE_SYNC */
cpuhp_ap_update_sync_state(enum cpuhp_sync_state state)340 static inline void cpuhp_ap_update_sync_state(enum cpuhp_sync_state state) { }
341 #endif /* !CONFIG_HOTPLUG_CORE_SYNC */
342
343 #ifdef CONFIG_HOTPLUG_CORE_SYNC_DEAD
344 /**
345 * cpuhp_ap_report_dead - Update synchronization state to DEAD
346 *
347 * No synchronization point. Just update of the synchronization state.
348 */
cpuhp_ap_report_dead(void)349 void cpuhp_ap_report_dead(void)
350 {
351 cpuhp_ap_update_sync_state(SYNC_STATE_DEAD);
352 }
353
arch_cpuhp_cleanup_dead_cpu(unsigned int cpu)354 void __weak arch_cpuhp_cleanup_dead_cpu(unsigned int cpu) { }
355
356 /*
357 * Late CPU shutdown synchronization point. Cannot use cpuhp_state::done_down
358 * because the AP cannot issue complete() at this stage.
359 */
cpuhp_bp_sync_dead(unsigned int cpu)360 static void cpuhp_bp_sync_dead(unsigned int cpu)
361 {
362 atomic_t *st = per_cpu_ptr(&cpuhp_state.ap_sync_state, cpu);
363 int sync = atomic_read(st);
364
365 do {
366 /* CPU can have reported dead already. Don't overwrite that! */
367 if (sync == SYNC_STATE_DEAD)
368 break;
369 } while (!atomic_try_cmpxchg(st, &sync, SYNC_STATE_SHOULD_DIE));
370
371 if (cpuhp_wait_for_sync_state(cpu, SYNC_STATE_DEAD, SYNC_STATE_DEAD)) {
372 /* CPU reached dead state. Invoke the cleanup function */
373 arch_cpuhp_cleanup_dead_cpu(cpu);
374 return;
375 }
376
377 /* No further action possible. Emit message and give up. */
378 pr_err("CPU%u failed to report dead state\n", cpu);
379 }
380 #else /* CONFIG_HOTPLUG_CORE_SYNC_DEAD */
cpuhp_bp_sync_dead(unsigned int cpu)381 static inline void cpuhp_bp_sync_dead(unsigned int cpu) { }
382 #endif /* !CONFIG_HOTPLUG_CORE_SYNC_DEAD */
383
384 #ifdef CONFIG_HOTPLUG_CORE_SYNC_FULL
385 /**
386 * cpuhp_ap_sync_alive - Synchronize AP with the control CPU once it is alive
387 *
388 * Updates the AP synchronization state to SYNC_STATE_ALIVE and waits
389 * for the BP to release it.
390 */
cpuhp_ap_sync_alive(void)391 void cpuhp_ap_sync_alive(void)
392 {
393 atomic_t *st = this_cpu_ptr(&cpuhp_state.ap_sync_state);
394
395 cpuhp_ap_update_sync_state(SYNC_STATE_ALIVE);
396
397 /* Wait for the control CPU to release it. */
398 while (atomic_read(st) != SYNC_STATE_SHOULD_ONLINE)
399 cpu_relax();
400 }
401
cpuhp_can_boot_ap(unsigned int cpu)402 static bool cpuhp_can_boot_ap(unsigned int cpu)
403 {
404 atomic_t *st = per_cpu_ptr(&cpuhp_state.ap_sync_state, cpu);
405 int sync = atomic_read(st);
406
407 again:
408 switch (sync) {
409 case SYNC_STATE_DEAD:
410 /* CPU is properly dead */
411 break;
412 case SYNC_STATE_KICKED:
413 /* CPU did not come up in previous attempt */
414 break;
415 case SYNC_STATE_ALIVE:
416 /* CPU is stuck cpuhp_ap_sync_alive(). */
417 break;
418 default:
419 /* CPU failed to report online or dead and is in limbo state. */
420 return false;
421 }
422
423 /* Prepare for booting */
424 if (!atomic_try_cmpxchg(st, &sync, SYNC_STATE_KICKED))
425 goto again;
426
427 return true;
428 }
429
arch_cpuhp_cleanup_kick_cpu(unsigned int cpu)430 void __weak arch_cpuhp_cleanup_kick_cpu(unsigned int cpu) { }
431
432 /*
433 * Early CPU bringup synchronization point. Cannot use cpuhp_state::done_up
434 * because the AP cannot issue complete() so early in the bringup.
435 */
cpuhp_bp_sync_alive(unsigned int cpu)436 static int cpuhp_bp_sync_alive(unsigned int cpu)
437 {
438 int ret = 0;
439
440 if (!IS_ENABLED(CONFIG_HOTPLUG_CORE_SYNC_FULL))
441 return 0;
442
443 if (!cpuhp_wait_for_sync_state(cpu, SYNC_STATE_ALIVE, SYNC_STATE_SHOULD_ONLINE)) {
444 pr_err("CPU%u failed to report alive state\n", cpu);
445 ret = -EIO;
446 }
447
448 /* Let the architecture cleanup the kick alive mechanics. */
449 arch_cpuhp_cleanup_kick_cpu(cpu);
450 return ret;
451 }
452 #else /* CONFIG_HOTPLUG_CORE_SYNC_FULL */
cpuhp_bp_sync_alive(unsigned int cpu)453 static inline int cpuhp_bp_sync_alive(unsigned int cpu) { return 0; }
cpuhp_can_boot_ap(unsigned int cpu)454 static inline bool cpuhp_can_boot_ap(unsigned int cpu) { return true; }
455 #endif /* !CONFIG_HOTPLUG_CORE_SYNC_FULL */
456
457 /* Serializes the updates to cpu_online_mask, cpu_present_mask */
458 static DEFINE_MUTEX(cpu_add_remove_lock);
459 bool cpuhp_tasks_frozen;
460 EXPORT_SYMBOL_GPL(cpuhp_tasks_frozen);
461
462 /*
463 * The following two APIs (cpu_maps_update_begin/done) must be used when
464 * attempting to serialize the updates to cpu_online_mask & cpu_present_mask.
465 */
cpu_maps_update_begin(void)466 void cpu_maps_update_begin(void)
467 {
468 mutex_lock(&cpu_add_remove_lock);
469 }
470
cpu_maps_update_done(void)471 void cpu_maps_update_done(void)
472 {
473 mutex_unlock(&cpu_add_remove_lock);
474 }
475
476 /*
477 * If set, cpu_up and cpu_down will return -EBUSY and do nothing.
478 * Should always be manipulated under cpu_add_remove_lock
479 */
480 static int cpu_hotplug_disabled;
481
482 #ifdef CONFIG_HOTPLUG_CPU
483
484 DEFINE_STATIC_PERCPU_RWSEM(cpu_hotplug_lock);
485
486 static bool cpu_hotplug_offline_disabled __ro_after_init;
487
cpus_read_lock(void)488 void cpus_read_lock(void)
489 {
490 percpu_down_read(&cpu_hotplug_lock);
491 }
492 EXPORT_SYMBOL_GPL(cpus_read_lock);
493
cpus_read_trylock(void)494 int cpus_read_trylock(void)
495 {
496 return percpu_down_read_trylock(&cpu_hotplug_lock);
497 }
498 EXPORT_SYMBOL_GPL(cpus_read_trylock);
499
cpus_read_unlock(void)500 void cpus_read_unlock(void)
501 {
502 percpu_up_read(&cpu_hotplug_lock);
503 }
504 EXPORT_SYMBOL_GPL(cpus_read_unlock);
505
cpus_write_lock(void)506 void cpus_write_lock(void)
507 {
508 percpu_down_write(&cpu_hotplug_lock);
509 }
510
cpus_write_unlock(void)511 void cpus_write_unlock(void)
512 {
513 percpu_up_write(&cpu_hotplug_lock);
514 }
515
lockdep_assert_cpus_held(void)516 void lockdep_assert_cpus_held(void)
517 {
518 /*
519 * We can't have hotplug operations before userspace starts running,
520 * and some init codepaths will knowingly not take the hotplug lock.
521 * This is all valid, so mute lockdep until it makes sense to report
522 * unheld locks.
523 */
524 if (system_state < SYSTEM_RUNNING)
525 return;
526
527 percpu_rwsem_assert_held(&cpu_hotplug_lock);
528 }
529
530 #ifdef CONFIG_LOCKDEP
lockdep_is_cpus_held(void)531 int lockdep_is_cpus_held(void)
532 {
533 return percpu_rwsem_is_held(&cpu_hotplug_lock);
534 }
535 #endif
536
lockdep_acquire_cpus_lock(void)537 static void lockdep_acquire_cpus_lock(void)
538 {
539 rwsem_acquire(&cpu_hotplug_lock.dep_map, 0, 0, _THIS_IP_);
540 }
541
lockdep_release_cpus_lock(void)542 static void lockdep_release_cpus_lock(void)
543 {
544 rwsem_release(&cpu_hotplug_lock.dep_map, _THIS_IP_);
545 }
546
547 /* Declare CPU offlining not supported */
cpu_hotplug_disable_offlining(void)548 void cpu_hotplug_disable_offlining(void)
549 {
550 cpu_maps_update_begin();
551 cpu_hotplug_offline_disabled = true;
552 cpu_maps_update_done();
553 }
554
555 /*
556 * Wait for currently running CPU hotplug operations to complete (if any) and
557 * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
558 * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the
559 * hotplug path before performing hotplug operations. So acquiring that lock
560 * guarantees mutual exclusion from any currently running hotplug operations.
561 */
cpu_hotplug_disable(void)562 void cpu_hotplug_disable(void)
563 {
564 cpu_maps_update_begin();
565 cpu_hotplug_disabled++;
566 cpu_maps_update_done();
567 }
568 EXPORT_SYMBOL_GPL(cpu_hotplug_disable);
569
__cpu_hotplug_enable(void)570 static void __cpu_hotplug_enable(void)
571 {
572 if (WARN_ONCE(!cpu_hotplug_disabled, "Unbalanced cpu hotplug enable\n"))
573 return;
574 cpu_hotplug_disabled--;
575 }
576
cpu_hotplug_enable(void)577 void cpu_hotplug_enable(void)
578 {
579 cpu_maps_update_begin();
580 __cpu_hotplug_enable();
581 cpu_maps_update_done();
582 }
583 EXPORT_SYMBOL_GPL(cpu_hotplug_enable);
584
585 #else
586
lockdep_acquire_cpus_lock(void)587 static void lockdep_acquire_cpus_lock(void)
588 {
589 }
590
lockdep_release_cpus_lock(void)591 static void lockdep_release_cpus_lock(void)
592 {
593 }
594
595 #endif /* CONFIG_HOTPLUG_CPU */
596
597 /*
598 * Architectures that need SMT-specific errata handling during SMT hotplug
599 * should override this.
600 */
arch_smt_update(void)601 void __weak arch_smt_update(void) { }
602
603 #ifdef CONFIG_HOTPLUG_SMT
604
605 enum cpuhp_smt_control cpu_smt_control __read_mostly = CPU_SMT_ENABLED;
606 static unsigned int cpu_smt_max_threads __ro_after_init;
607 unsigned int cpu_smt_num_threads __read_mostly = UINT_MAX;
608
cpu_smt_disable(bool force)609 void __init cpu_smt_disable(bool force)
610 {
611 if (!cpu_smt_possible())
612 return;
613
614 if (force) {
615 pr_info("SMT: Force disabled\n");
616 cpu_smt_control = CPU_SMT_FORCE_DISABLED;
617 } else {
618 pr_info("SMT: disabled\n");
619 cpu_smt_control = CPU_SMT_DISABLED;
620 }
621 cpu_smt_num_threads = 1;
622 }
623
624 /*
625 * The decision whether SMT is supported can only be done after the full
626 * CPU identification. Called from architecture code.
627 */
cpu_smt_set_num_threads(unsigned int num_threads,unsigned int max_threads)628 void __init cpu_smt_set_num_threads(unsigned int num_threads,
629 unsigned int max_threads)
630 {
631 WARN_ON(!num_threads || (num_threads > max_threads));
632
633 if (max_threads == 1)
634 cpu_smt_control = CPU_SMT_NOT_SUPPORTED;
635
636 cpu_smt_max_threads = max_threads;
637
638 /*
639 * If SMT has been disabled via the kernel command line or SMT is
640 * not supported, set cpu_smt_num_threads to 1 for consistency.
641 * If enabled, take the architecture requested number of threads
642 * to bring up into account.
643 */
644 if (cpu_smt_control != CPU_SMT_ENABLED)
645 cpu_smt_num_threads = 1;
646 else if (num_threads < cpu_smt_num_threads)
647 cpu_smt_num_threads = num_threads;
648 }
649
smt_cmdline_disable(char * str)650 static int __init smt_cmdline_disable(char *str)
651 {
652 cpu_smt_disable(str && !strcmp(str, "force"));
653 return 0;
654 }
655 early_param("nosmt", smt_cmdline_disable);
656
657 /*
658 * For Archicture supporting partial SMT states check if the thread is allowed.
659 * Otherwise this has already been checked through cpu_smt_max_threads when
660 * setting the SMT level.
661 */
cpu_smt_thread_allowed(unsigned int cpu)662 static inline bool cpu_smt_thread_allowed(unsigned int cpu)
663 {
664 #ifdef CONFIG_SMT_NUM_THREADS_DYNAMIC
665 return topology_smt_thread_allowed(cpu);
666 #else
667 return true;
668 #endif
669 }
670
cpu_bootable(unsigned int cpu)671 static inline bool cpu_bootable(unsigned int cpu)
672 {
673 if (cpu_smt_control == CPU_SMT_ENABLED && cpu_smt_thread_allowed(cpu))
674 return true;
675
676 /* All CPUs are bootable if controls are not configured */
677 if (cpu_smt_control == CPU_SMT_NOT_IMPLEMENTED)
678 return true;
679
680 /* All CPUs are bootable if CPU is not SMT capable */
681 if (cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
682 return true;
683
684 if (topology_is_primary_thread(cpu))
685 return true;
686
687 /*
688 * On x86 it's required to boot all logical CPUs at least once so
689 * that the init code can get a chance to set CR4.MCE on each
690 * CPU. Otherwise, a broadcasted MCE observing CR4.MCE=0b on any
691 * core will shutdown the machine.
692 */
693 return !cpumask_test_cpu(cpu, &cpus_booted_once_mask);
694 }
695
696 /* Returns true if SMT is supported and not forcefully (irreversibly) disabled */
cpu_smt_possible(void)697 bool cpu_smt_possible(void)
698 {
699 return cpu_smt_control != CPU_SMT_FORCE_DISABLED &&
700 cpu_smt_control != CPU_SMT_NOT_SUPPORTED;
701 }
702 EXPORT_SYMBOL_GPL(cpu_smt_possible);
703
704 #else
cpu_bootable(unsigned int cpu)705 static inline bool cpu_bootable(unsigned int cpu) { return true; }
706 #endif
707
708 static inline enum cpuhp_state
cpuhp_set_state(int cpu,struct cpuhp_cpu_state * st,enum cpuhp_state target)709 cpuhp_set_state(int cpu, struct cpuhp_cpu_state *st, enum cpuhp_state target)
710 {
711 enum cpuhp_state prev_state = st->state;
712 bool bringup = st->state < target;
713
714 st->rollback = false;
715 st->last = NULL;
716
717 st->target = target;
718 st->single = false;
719 st->bringup = bringup;
720 if (cpu_dying(cpu) != !bringup)
721 set_cpu_dying(cpu, !bringup);
722
723 return prev_state;
724 }
725
726 static inline void
cpuhp_reset_state(int cpu,struct cpuhp_cpu_state * st,enum cpuhp_state prev_state)727 cpuhp_reset_state(int cpu, struct cpuhp_cpu_state *st,
728 enum cpuhp_state prev_state)
729 {
730 bool bringup = !st->bringup;
731
732 st->target = prev_state;
733
734 /*
735 * Already rolling back. No need invert the bringup value or to change
736 * the current state.
737 */
738 if (st->rollback)
739 return;
740
741 st->rollback = true;
742
743 /*
744 * If we have st->last we need to undo partial multi_instance of this
745 * state first. Otherwise start undo at the previous state.
746 */
747 if (!st->last) {
748 if (st->bringup)
749 st->state--;
750 else
751 st->state++;
752 }
753
754 st->bringup = bringup;
755 if (cpu_dying(cpu) != !bringup)
756 set_cpu_dying(cpu, !bringup);
757 }
758
759 /* Regular hotplug invocation of the AP hotplug thread */
__cpuhp_kick_ap(struct cpuhp_cpu_state * st)760 static void __cpuhp_kick_ap(struct cpuhp_cpu_state *st)
761 {
762 if (!st->single && st->state == st->target)
763 return;
764
765 st->result = 0;
766 /*
767 * Make sure the above stores are visible before should_run becomes
768 * true. Paired with the mb() above in cpuhp_thread_fun()
769 */
770 smp_mb();
771 st->should_run = true;
772 wake_up_process(st->thread);
773 wait_for_ap_thread(st, st->bringup);
774 }
775
cpuhp_kick_ap(int cpu,struct cpuhp_cpu_state * st,enum cpuhp_state target)776 static int cpuhp_kick_ap(int cpu, struct cpuhp_cpu_state *st,
777 enum cpuhp_state target)
778 {
779 enum cpuhp_state prev_state;
780 int ret;
781
782 prev_state = cpuhp_set_state(cpu, st, target);
783 __cpuhp_kick_ap(st);
784 if ((ret = st->result)) {
785 cpuhp_reset_state(cpu, st, prev_state);
786 __cpuhp_kick_ap(st);
787 }
788
789 return ret;
790 }
791
bringup_wait_for_ap_online(unsigned int cpu)792 static int bringup_wait_for_ap_online(unsigned int cpu)
793 {
794 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
795
796 /* Wait for the CPU to reach CPUHP_AP_ONLINE_IDLE */
797 wait_for_ap_thread(st, true);
798 if (WARN_ON_ONCE((!cpu_online(cpu))))
799 return -ECANCELED;
800
801 /* Unpark the hotplug thread of the target cpu */
802 kthread_unpark(st->thread);
803
804 /*
805 * SMT soft disabling on X86 requires to bring the CPU out of the
806 * BIOS 'wait for SIPI' state in order to set the CR4.MCE bit. The
807 * CPU marked itself as booted_once in notify_cpu_starting() so the
808 * cpu_bootable() check will now return false if this is not the
809 * primary sibling.
810 */
811 if (!cpu_bootable(cpu))
812 return -ECANCELED;
813 return 0;
814 }
815
816 #ifdef CONFIG_HOTPLUG_SPLIT_STARTUP
cpuhp_kick_ap_alive(unsigned int cpu)817 static int cpuhp_kick_ap_alive(unsigned int cpu)
818 {
819 if (!cpuhp_can_boot_ap(cpu))
820 return -EAGAIN;
821
822 return arch_cpuhp_kick_ap_alive(cpu, idle_thread_get(cpu));
823 }
824
cpuhp_bringup_ap(unsigned int cpu)825 static int cpuhp_bringup_ap(unsigned int cpu)
826 {
827 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
828 int ret;
829
830 /*
831 * Some architectures have to walk the irq descriptors to
832 * setup the vector space for the cpu which comes online.
833 * Prevent irq alloc/free across the bringup.
834 */
835 irq_lock_sparse();
836
837 ret = cpuhp_bp_sync_alive(cpu);
838 if (ret)
839 goto out_unlock;
840
841 ret = bringup_wait_for_ap_online(cpu);
842 if (ret)
843 goto out_unlock;
844
845 irq_unlock_sparse();
846
847 if (st->target <= CPUHP_AP_ONLINE_IDLE)
848 return 0;
849
850 return cpuhp_kick_ap(cpu, st, st->target);
851
852 out_unlock:
853 irq_unlock_sparse();
854 return ret;
855 }
856 #else
bringup_cpu(unsigned int cpu)857 static int bringup_cpu(unsigned int cpu)
858 {
859 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
860 struct task_struct *idle = idle_thread_get(cpu);
861 int ret;
862
863 if (!cpuhp_can_boot_ap(cpu))
864 return -EAGAIN;
865
866 /*
867 * Some architectures have to walk the irq descriptors to
868 * setup the vector space for the cpu which comes online.
869 *
870 * Prevent irq alloc/free across the bringup by acquiring the
871 * sparse irq lock. Hold it until the upcoming CPU completes the
872 * startup in cpuhp_online_idle() which allows to avoid
873 * intermediate synchronization points in the architecture code.
874 */
875 irq_lock_sparse();
876
877 ret = __cpu_up(cpu, idle);
878 if (ret)
879 goto out_unlock;
880
881 ret = cpuhp_bp_sync_alive(cpu);
882 if (ret)
883 goto out_unlock;
884
885 ret = bringup_wait_for_ap_online(cpu);
886 if (ret)
887 goto out_unlock;
888
889 irq_unlock_sparse();
890
891 if (st->target <= CPUHP_AP_ONLINE_IDLE)
892 return 0;
893
894 return cpuhp_kick_ap(cpu, st, st->target);
895
896 out_unlock:
897 irq_unlock_sparse();
898 return ret;
899 }
900 #endif
901
finish_cpu(unsigned int cpu)902 static int finish_cpu(unsigned int cpu)
903 {
904 struct task_struct *idle = idle_thread_get(cpu);
905 struct mm_struct *mm = idle->active_mm;
906
907 /*
908 * sched_force_init_mm() ensured the use of &init_mm,
909 * drop that refcount now that the CPU has stopped.
910 */
911 WARN_ON(mm != &init_mm);
912 idle->active_mm = NULL;
913 mmdrop_lazy_tlb(mm);
914
915 return 0;
916 }
917
918 /*
919 * Hotplug state machine related functions
920 */
921
922 /*
923 * Get the next state to run. Empty ones will be skipped. Returns true if a
924 * state must be run.
925 *
926 * st->state will be modified ahead of time, to match state_to_run, as if it
927 * has already ran.
928 */
cpuhp_next_state(bool bringup,enum cpuhp_state * state_to_run,struct cpuhp_cpu_state * st,enum cpuhp_state target)929 static bool cpuhp_next_state(bool bringup,
930 enum cpuhp_state *state_to_run,
931 struct cpuhp_cpu_state *st,
932 enum cpuhp_state target)
933 {
934 do {
935 if (bringup) {
936 if (st->state >= target)
937 return false;
938
939 *state_to_run = ++st->state;
940 } else {
941 if (st->state <= target)
942 return false;
943
944 *state_to_run = st->state--;
945 }
946
947 if (!cpuhp_step_empty(bringup, cpuhp_get_step(*state_to_run)))
948 break;
949 } while (true);
950
951 return true;
952 }
953
__cpuhp_invoke_callback_range(bool bringup,unsigned int cpu,struct cpuhp_cpu_state * st,enum cpuhp_state target,bool nofail)954 static int __cpuhp_invoke_callback_range(bool bringup,
955 unsigned int cpu,
956 struct cpuhp_cpu_state *st,
957 enum cpuhp_state target,
958 bool nofail)
959 {
960 enum cpuhp_state state;
961 int ret = 0;
962
963 while (cpuhp_next_state(bringup, &state, st, target)) {
964 int err;
965
966 err = cpuhp_invoke_callback(cpu, state, bringup, NULL, NULL);
967 if (!err)
968 continue;
969
970 if (nofail) {
971 pr_warn("CPU %u %s state %s (%d) failed (%d)\n",
972 cpu, bringup ? "UP" : "DOWN",
973 cpuhp_get_step(st->state)->name,
974 st->state, err);
975 ret = -1;
976 } else {
977 ret = err;
978 break;
979 }
980 }
981
982 return ret;
983 }
984
cpuhp_invoke_callback_range(bool bringup,unsigned int cpu,struct cpuhp_cpu_state * st,enum cpuhp_state target)985 static inline int cpuhp_invoke_callback_range(bool bringup,
986 unsigned int cpu,
987 struct cpuhp_cpu_state *st,
988 enum cpuhp_state target)
989 {
990 return __cpuhp_invoke_callback_range(bringup, cpu, st, target, false);
991 }
992
cpuhp_invoke_callback_range_nofail(bool bringup,unsigned int cpu,struct cpuhp_cpu_state * st,enum cpuhp_state target)993 static inline void cpuhp_invoke_callback_range_nofail(bool bringup,
994 unsigned int cpu,
995 struct cpuhp_cpu_state *st,
996 enum cpuhp_state target)
997 {
998 __cpuhp_invoke_callback_range(bringup, cpu, st, target, true);
999 }
1000
can_rollback_cpu(struct cpuhp_cpu_state * st)1001 static inline bool can_rollback_cpu(struct cpuhp_cpu_state *st)
1002 {
1003 if (IS_ENABLED(CONFIG_HOTPLUG_CPU))
1004 return true;
1005 /*
1006 * When CPU hotplug is disabled, then taking the CPU down is not
1007 * possible because takedown_cpu() and the architecture and
1008 * subsystem specific mechanisms are not available. So the CPU
1009 * which would be completely unplugged again needs to stay around
1010 * in the current state.
1011 */
1012 return st->state <= CPUHP_BRINGUP_CPU;
1013 }
1014
cpuhp_up_callbacks(unsigned int cpu,struct cpuhp_cpu_state * st,enum cpuhp_state target)1015 static int cpuhp_up_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
1016 enum cpuhp_state target)
1017 {
1018 enum cpuhp_state prev_state = st->state;
1019 int ret = 0;
1020
1021 ret = cpuhp_invoke_callback_range(true, cpu, st, target);
1022 if (ret) {
1023 pr_debug("CPU UP failed (%d) CPU %u state %s (%d)\n",
1024 ret, cpu, cpuhp_get_step(st->state)->name,
1025 st->state);
1026
1027 cpuhp_reset_state(cpu, st, prev_state);
1028 if (can_rollback_cpu(st))
1029 WARN_ON(cpuhp_invoke_callback_range(false, cpu, st,
1030 prev_state));
1031 }
1032 return ret;
1033 }
1034
1035 /*
1036 * The cpu hotplug threads manage the bringup and teardown of the cpus
1037 */
cpuhp_should_run(unsigned int cpu)1038 static int cpuhp_should_run(unsigned int cpu)
1039 {
1040 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
1041
1042 return st->should_run;
1043 }
1044
1045 /*
1046 * Execute teardown/startup callbacks on the plugged cpu. Also used to invoke
1047 * callbacks when a state gets [un]installed at runtime.
1048 *
1049 * Each invocation of this function by the smpboot thread does a single AP
1050 * state callback.
1051 *
1052 * It has 3 modes of operation:
1053 * - single: runs st->cb_state
1054 * - up: runs ++st->state, while st->state < st->target
1055 * - down: runs st->state--, while st->state > st->target
1056 *
1057 * When complete or on error, should_run is cleared and the completion is fired.
1058 */
cpuhp_thread_fun(unsigned int cpu)1059 static void cpuhp_thread_fun(unsigned int cpu)
1060 {
1061 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
1062 bool bringup = st->bringup;
1063 enum cpuhp_state state;
1064
1065 if (WARN_ON_ONCE(!st->should_run))
1066 return;
1067
1068 /*
1069 * ACQUIRE for the cpuhp_should_run() load of ->should_run. Ensures
1070 * that if we see ->should_run we also see the rest of the state.
1071 */
1072 smp_mb();
1073
1074 /*
1075 * The BP holds the hotplug lock, but we're now running on the AP,
1076 * ensure that anybody asserting the lock is held, will actually find
1077 * it so.
1078 */
1079 lockdep_acquire_cpus_lock();
1080 cpuhp_lock_acquire(bringup);
1081
1082 if (st->single) {
1083 state = st->cb_state;
1084 st->should_run = false;
1085 } else {
1086 st->should_run = cpuhp_next_state(bringup, &state, st, st->target);
1087 if (!st->should_run)
1088 goto end;
1089 }
1090
1091 WARN_ON_ONCE(!cpuhp_is_ap_state(state));
1092
1093 if (cpuhp_is_atomic_state(state)) {
1094 local_irq_disable();
1095 st->result = cpuhp_invoke_callback(cpu, state, bringup, st->node, &st->last);
1096 local_irq_enable();
1097
1098 /*
1099 * STARTING/DYING must not fail!
1100 */
1101 WARN_ON_ONCE(st->result);
1102 } else {
1103 st->result = cpuhp_invoke_callback(cpu, state, bringup, st->node, &st->last);
1104 }
1105
1106 if (st->result) {
1107 /*
1108 * If we fail on a rollback, we're up a creek without no
1109 * paddle, no way forward, no way back. We loose, thanks for
1110 * playing.
1111 */
1112 WARN_ON_ONCE(st->rollback);
1113 st->should_run = false;
1114 }
1115
1116 end:
1117 cpuhp_lock_release(bringup);
1118 lockdep_release_cpus_lock();
1119
1120 if (!st->should_run)
1121 complete_ap_thread(st, bringup);
1122 }
1123
1124 /* Invoke a single callback on a remote cpu */
1125 static int
cpuhp_invoke_ap_callback(int cpu,enum cpuhp_state state,bool bringup,struct hlist_node * node)1126 cpuhp_invoke_ap_callback(int cpu, enum cpuhp_state state, bool bringup,
1127 struct hlist_node *node)
1128 {
1129 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1130 int ret;
1131
1132 if (!cpu_online(cpu))
1133 return 0;
1134
1135 cpuhp_lock_acquire(false);
1136 cpuhp_lock_release(false);
1137
1138 cpuhp_lock_acquire(true);
1139 cpuhp_lock_release(true);
1140
1141 /*
1142 * If we are up and running, use the hotplug thread. For early calls
1143 * we invoke the thread function directly.
1144 */
1145 if (!st->thread)
1146 return cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
1147
1148 st->rollback = false;
1149 st->last = NULL;
1150
1151 st->node = node;
1152 st->bringup = bringup;
1153 st->cb_state = state;
1154 st->single = true;
1155
1156 __cpuhp_kick_ap(st);
1157
1158 /*
1159 * If we failed and did a partial, do a rollback.
1160 */
1161 if ((ret = st->result) && st->last) {
1162 st->rollback = true;
1163 st->bringup = !bringup;
1164
1165 __cpuhp_kick_ap(st);
1166 }
1167
1168 /*
1169 * Clean up the leftovers so the next hotplug operation wont use stale
1170 * data.
1171 */
1172 st->node = st->last = NULL;
1173 return ret;
1174 }
1175
cpuhp_kick_ap_work(unsigned int cpu)1176 static int cpuhp_kick_ap_work(unsigned int cpu)
1177 {
1178 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1179 enum cpuhp_state prev_state = st->state;
1180 int ret;
1181
1182 cpuhp_lock_acquire(false);
1183 cpuhp_lock_release(false);
1184
1185 cpuhp_lock_acquire(true);
1186 cpuhp_lock_release(true);
1187
1188 trace_cpuhp_enter(cpu, st->target, prev_state, cpuhp_kick_ap_work);
1189 ret = cpuhp_kick_ap(cpu, st, st->target);
1190 trace_cpuhp_exit(cpu, st->state, prev_state, ret);
1191
1192 return ret;
1193 }
1194
1195 static struct smp_hotplug_thread cpuhp_threads = {
1196 .store = &cpuhp_state.thread,
1197 .thread_should_run = cpuhp_should_run,
1198 .thread_fn = cpuhp_thread_fun,
1199 .thread_comm = "cpuhp/%u",
1200 .selfparking = true,
1201 };
1202
cpuhp_init_state(void)1203 static __init void cpuhp_init_state(void)
1204 {
1205 struct cpuhp_cpu_state *st;
1206 int cpu;
1207
1208 for_each_possible_cpu(cpu) {
1209 st = per_cpu_ptr(&cpuhp_state, cpu);
1210 init_completion(&st->done_up);
1211 init_completion(&st->done_down);
1212 }
1213 }
1214
cpuhp_threads_init(void)1215 void __init cpuhp_threads_init(void)
1216 {
1217 cpuhp_init_state();
1218 BUG_ON(smpboot_register_percpu_thread(&cpuhp_threads));
1219 kthread_unpark(this_cpu_read(cpuhp_state.thread));
1220 }
1221
1222 #ifdef CONFIG_HOTPLUG_CPU
1223 #ifndef arch_clear_mm_cpumask_cpu
1224 #define arch_clear_mm_cpumask_cpu(cpu, mm) cpumask_clear_cpu(cpu, mm_cpumask(mm))
1225 #endif
1226
1227 /**
1228 * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU
1229 * @cpu: a CPU id
1230 *
1231 * This function walks all processes, finds a valid mm struct for each one and
1232 * then clears a corresponding bit in mm's cpumask. While this all sounds
1233 * trivial, there are various non-obvious corner cases, which this function
1234 * tries to solve in a safe manner.
1235 *
1236 * Also note that the function uses a somewhat relaxed locking scheme, so it may
1237 * be called only for an already offlined CPU.
1238 */
clear_tasks_mm_cpumask(int cpu)1239 void clear_tasks_mm_cpumask(int cpu)
1240 {
1241 struct task_struct *p;
1242
1243 /*
1244 * This function is called after the cpu is taken down and marked
1245 * offline, so its not like new tasks will ever get this cpu set in
1246 * their mm mask. -- Peter Zijlstra
1247 * Thus, we may use rcu_read_lock() here, instead of grabbing
1248 * full-fledged tasklist_lock.
1249 */
1250 WARN_ON(cpu_online(cpu));
1251 rcu_read_lock();
1252 for_each_process(p) {
1253 struct task_struct *t;
1254
1255 /*
1256 * Main thread might exit, but other threads may still have
1257 * a valid mm. Find one.
1258 */
1259 t = find_lock_task_mm(p);
1260 if (!t)
1261 continue;
1262 arch_clear_mm_cpumask_cpu(cpu, t->mm);
1263 task_unlock(t);
1264 }
1265 rcu_read_unlock();
1266 }
1267
1268 /* Take this CPU down. */
take_cpu_down(void * _param)1269 static int take_cpu_down(void *_param)
1270 {
1271 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
1272 enum cpuhp_state target = max((int)st->target, CPUHP_AP_OFFLINE);
1273 int err, cpu = smp_processor_id();
1274
1275 /* Ensure this CPU doesn't handle any more interrupts. */
1276 err = __cpu_disable();
1277 if (err < 0)
1278 return err;
1279
1280 /*
1281 * Must be called from CPUHP_TEARDOWN_CPU, which means, as we are going
1282 * down, that the current state is CPUHP_TEARDOWN_CPU - 1.
1283 */
1284 WARN_ON(st->state != (CPUHP_TEARDOWN_CPU - 1));
1285
1286 /*
1287 * Invoke the former CPU_DYING callbacks. DYING must not fail!
1288 */
1289 cpuhp_invoke_callback_range_nofail(false, cpu, st, target);
1290
1291 /* Park the stopper thread */
1292 stop_machine_park(cpu);
1293 return 0;
1294 }
1295
takedown_cpu(unsigned int cpu)1296 static int takedown_cpu(unsigned int cpu)
1297 {
1298 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1299 int err;
1300
1301 /* Park the smpboot threads */
1302 kthread_park(st->thread);
1303
1304 /*
1305 * Prevent irq alloc/free while the dying cpu reorganizes the
1306 * interrupt affinities.
1307 */
1308 irq_lock_sparse();
1309
1310 /*
1311 * So now all preempt/rcu users must observe !cpu_active().
1312 */
1313 err = stop_machine_cpuslocked(take_cpu_down, NULL, cpumask_of(cpu));
1314 if (err) {
1315 /* CPU refused to die */
1316 irq_unlock_sparse();
1317 /* Unpark the hotplug thread so we can rollback there */
1318 kthread_unpark(st->thread);
1319 return err;
1320 }
1321 BUG_ON(cpu_online(cpu));
1322
1323 /*
1324 * The teardown callback for CPUHP_AP_SCHED_STARTING will have removed
1325 * all runnable tasks from the CPU, there's only the idle task left now
1326 * that the migration thread is done doing the stop_machine thing.
1327 *
1328 * Wait for the stop thread to go away.
1329 */
1330 wait_for_ap_thread(st, false);
1331 BUG_ON(st->state != CPUHP_AP_IDLE_DEAD);
1332
1333 /* Interrupts are moved away from the dying cpu, reenable alloc/free */
1334 irq_unlock_sparse();
1335
1336 hotplug_cpu__broadcast_tick_pull(cpu);
1337 /* This actually kills the CPU. */
1338 __cpu_die(cpu);
1339
1340 cpuhp_bp_sync_dead(cpu);
1341
1342 lockdep_cleanup_dead_cpu(cpu, idle_thread_get(cpu));
1343
1344 /*
1345 * Callbacks must be re-integrated right away to the RCU state machine.
1346 * Otherwise an RCU callback could block a further teardown function
1347 * waiting for its completion.
1348 */
1349 rcutree_migrate_callbacks(cpu);
1350
1351 return 0;
1352 }
1353
cpuhp_complete_idle_dead(void * arg)1354 static void cpuhp_complete_idle_dead(void *arg)
1355 {
1356 struct cpuhp_cpu_state *st = arg;
1357
1358 complete_ap_thread(st, false);
1359 }
1360
cpuhp_report_idle_dead(void)1361 void cpuhp_report_idle_dead(void)
1362 {
1363 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
1364
1365 BUG_ON(st->state != CPUHP_AP_OFFLINE);
1366 tick_assert_timekeeping_handover();
1367 rcutree_report_cpu_dead();
1368 st->state = CPUHP_AP_IDLE_DEAD;
1369 /*
1370 * We cannot call complete after rcutree_report_cpu_dead() so we delegate it
1371 * to an online cpu.
1372 */
1373 smp_call_function_single(cpumask_first(cpu_online_mask),
1374 cpuhp_complete_idle_dead, st, 0);
1375 }
1376
cpuhp_down_callbacks(unsigned int cpu,struct cpuhp_cpu_state * st,enum cpuhp_state target)1377 static int cpuhp_down_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
1378 enum cpuhp_state target)
1379 {
1380 enum cpuhp_state prev_state = st->state;
1381 int ret = 0;
1382
1383 ret = cpuhp_invoke_callback_range(false, cpu, st, target);
1384 if (ret) {
1385 pr_debug("CPU DOWN failed (%d) CPU %u state %s (%d)\n",
1386 ret, cpu, cpuhp_get_step(st->state)->name,
1387 st->state);
1388
1389 cpuhp_reset_state(cpu, st, prev_state);
1390
1391 if (st->state < prev_state)
1392 WARN_ON(cpuhp_invoke_callback_range(true, cpu, st,
1393 prev_state));
1394 }
1395
1396 return ret;
1397 }
1398
1399 /* Requires cpu_add_remove_lock to be held */
_cpu_down(unsigned int cpu,int tasks_frozen,enum cpuhp_state target)1400 static int __ref _cpu_down(unsigned int cpu, int tasks_frozen,
1401 enum cpuhp_state target)
1402 {
1403 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1404 int prev_state, ret = 0;
1405
1406 if (num_online_cpus() == 1)
1407 return -EBUSY;
1408
1409 if (!cpu_present(cpu))
1410 return -EINVAL;
1411
1412 cpus_write_lock();
1413
1414 cpuhp_tasks_frozen = tasks_frozen;
1415
1416 prev_state = cpuhp_set_state(cpu, st, target);
1417 /*
1418 * If the current CPU state is in the range of the AP hotplug thread,
1419 * then we need to kick the thread.
1420 */
1421 if (st->state > CPUHP_TEARDOWN_CPU) {
1422 st->target = max((int)target, CPUHP_TEARDOWN_CPU);
1423 ret = cpuhp_kick_ap_work(cpu);
1424 /*
1425 * The AP side has done the error rollback already. Just
1426 * return the error code..
1427 */
1428 if (ret)
1429 goto out;
1430
1431 /*
1432 * We might have stopped still in the range of the AP hotplug
1433 * thread. Nothing to do anymore.
1434 */
1435 if (st->state > CPUHP_TEARDOWN_CPU)
1436 goto out;
1437
1438 st->target = target;
1439 }
1440 /*
1441 * The AP brought itself down to CPUHP_TEARDOWN_CPU. So we need
1442 * to do the further cleanups.
1443 */
1444 ret = cpuhp_down_callbacks(cpu, st, target);
1445 if (ret && st->state < prev_state) {
1446 if (st->state == CPUHP_TEARDOWN_CPU) {
1447 cpuhp_reset_state(cpu, st, prev_state);
1448 __cpuhp_kick_ap(st);
1449 } else {
1450 WARN(1, "DEAD callback error for CPU%d", cpu);
1451 }
1452 }
1453
1454 out:
1455 cpus_write_unlock();
1456 arch_smt_update();
1457 return ret;
1458 }
1459
1460 struct cpu_down_work {
1461 unsigned int cpu;
1462 enum cpuhp_state target;
1463 };
1464
__cpu_down_maps_locked(void * arg)1465 static long __cpu_down_maps_locked(void *arg)
1466 {
1467 struct cpu_down_work *work = arg;
1468
1469 return _cpu_down(work->cpu, 0, work->target);
1470 }
1471
cpu_down_maps_locked(unsigned int cpu,enum cpuhp_state target)1472 static int cpu_down_maps_locked(unsigned int cpu, enum cpuhp_state target)
1473 {
1474 struct cpu_down_work work = { .cpu = cpu, .target = target, };
1475
1476 /*
1477 * If the platform does not support hotplug, report it explicitly to
1478 * differentiate it from a transient offlining failure.
1479 */
1480 if (cpu_hotplug_offline_disabled)
1481 return -EOPNOTSUPP;
1482 if (cpu_hotplug_disabled)
1483 return -EBUSY;
1484
1485 /*
1486 * Ensure that the control task does not run on the to be offlined
1487 * CPU to prevent a deadlock against cfs_b->period_timer.
1488 * Also keep at least one housekeeping cpu onlined to avoid generating
1489 * an empty sched_domain span.
1490 */
1491 for_each_cpu_and(cpu, cpu_online_mask, housekeeping_cpumask(HK_TYPE_DOMAIN)) {
1492 if (cpu != work.cpu)
1493 return work_on_cpu(cpu, __cpu_down_maps_locked, &work);
1494 }
1495 return -EBUSY;
1496 }
1497
cpu_down(unsigned int cpu,enum cpuhp_state target)1498 static int cpu_down(unsigned int cpu, enum cpuhp_state target)
1499 {
1500 int err;
1501
1502 cpu_maps_update_begin();
1503 err = cpu_down_maps_locked(cpu, target);
1504 cpu_maps_update_done();
1505 return err;
1506 }
1507
1508 /**
1509 * cpu_device_down - Bring down a cpu device
1510 * @dev: Pointer to the cpu device to offline
1511 *
1512 * This function is meant to be used by device core cpu subsystem only.
1513 *
1514 * Other subsystems should use remove_cpu() instead.
1515 *
1516 * Return: %0 on success or a negative errno code
1517 */
cpu_device_down(struct device * dev)1518 int cpu_device_down(struct device *dev)
1519 {
1520 return cpu_down(dev->id, CPUHP_OFFLINE);
1521 }
1522
remove_cpu(unsigned int cpu)1523 int remove_cpu(unsigned int cpu)
1524 {
1525 int ret;
1526
1527 lock_device_hotplug();
1528 ret = device_offline(get_cpu_device(cpu));
1529 unlock_device_hotplug();
1530
1531 return ret;
1532 }
1533 EXPORT_SYMBOL_GPL(remove_cpu);
1534
smp_shutdown_nonboot_cpus(unsigned int primary_cpu)1535 void smp_shutdown_nonboot_cpus(unsigned int primary_cpu)
1536 {
1537 unsigned int cpu;
1538 int error;
1539
1540 cpu_maps_update_begin();
1541
1542 /*
1543 * Make certain the cpu I'm about to reboot on is online.
1544 *
1545 * This is inline to what migrate_to_reboot_cpu() already do.
1546 */
1547 if (!cpu_online(primary_cpu))
1548 primary_cpu = cpumask_first(cpu_online_mask);
1549
1550 for_each_online_cpu(cpu) {
1551 if (cpu == primary_cpu)
1552 continue;
1553
1554 error = cpu_down_maps_locked(cpu, CPUHP_OFFLINE);
1555 if (error) {
1556 pr_err("Failed to offline CPU%d - error=%d",
1557 cpu, error);
1558 break;
1559 }
1560 }
1561
1562 /*
1563 * Ensure all but the reboot CPU are offline.
1564 */
1565 BUG_ON(num_online_cpus() > 1);
1566
1567 /*
1568 * Make sure the CPUs won't be enabled by someone else after this
1569 * point. Kexec will reboot to a new kernel shortly resetting
1570 * everything along the way.
1571 */
1572 cpu_hotplug_disabled++;
1573
1574 cpu_maps_update_done();
1575 }
1576
1577 #else
1578 #define takedown_cpu NULL
1579 #endif /*CONFIG_HOTPLUG_CPU*/
1580
1581 /**
1582 * notify_cpu_starting(cpu) - Invoke the callbacks on the starting CPU
1583 * @cpu: cpu that just started
1584 *
1585 * It must be called by the arch code on the new cpu, before the new cpu
1586 * enables interrupts and before the "boot" cpu returns from __cpu_up().
1587 */
notify_cpu_starting(unsigned int cpu)1588 void notify_cpu_starting(unsigned int cpu)
1589 {
1590 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1591 enum cpuhp_state target = min((int)st->target, CPUHP_AP_ONLINE);
1592
1593 rcutree_report_cpu_starting(cpu); /* Enables RCU usage on this CPU. */
1594 cpumask_set_cpu(cpu, &cpus_booted_once_mask);
1595
1596 /*
1597 * STARTING must not fail!
1598 */
1599 cpuhp_invoke_callback_range_nofail(true, cpu, st, target);
1600 }
1601
1602 /*
1603 * Called from the idle task. Wake up the controlling task which brings the
1604 * hotplug thread of the upcoming CPU up and then delegates the rest of the
1605 * online bringup to the hotplug thread.
1606 */
cpuhp_online_idle(enum cpuhp_state state)1607 void cpuhp_online_idle(enum cpuhp_state state)
1608 {
1609 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
1610
1611 /* Happens for the boot cpu */
1612 if (state != CPUHP_AP_ONLINE_IDLE)
1613 return;
1614
1615 cpuhp_ap_update_sync_state(SYNC_STATE_ONLINE);
1616
1617 /*
1618 * Unpark the stopper thread before we start the idle loop (and start
1619 * scheduling); this ensures the stopper task is always available.
1620 */
1621 stop_machine_unpark(smp_processor_id());
1622
1623 st->state = CPUHP_AP_ONLINE_IDLE;
1624 complete_ap_thread(st, true);
1625 }
1626
1627 /* Requires cpu_add_remove_lock to be held */
_cpu_up(unsigned int cpu,int tasks_frozen,enum cpuhp_state target)1628 static int _cpu_up(unsigned int cpu, int tasks_frozen, enum cpuhp_state target)
1629 {
1630 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1631 struct task_struct *idle;
1632 int ret = 0;
1633
1634 cpus_write_lock();
1635
1636 if (!cpu_present(cpu)) {
1637 ret = -EINVAL;
1638 goto out;
1639 }
1640
1641 /*
1642 * The caller of cpu_up() might have raced with another
1643 * caller. Nothing to do.
1644 */
1645 if (st->state >= target)
1646 goto out;
1647
1648 if (st->state == CPUHP_OFFLINE) {
1649 /* Let it fail before we try to bring the cpu up */
1650 idle = idle_thread_get(cpu);
1651 if (IS_ERR(idle)) {
1652 ret = PTR_ERR(idle);
1653 goto out;
1654 }
1655
1656 /*
1657 * Reset stale stack state from the last time this CPU was online.
1658 */
1659 scs_task_reset(idle);
1660 kasan_unpoison_task_stack(idle);
1661 }
1662
1663 cpuhp_tasks_frozen = tasks_frozen;
1664
1665 cpuhp_set_state(cpu, st, target);
1666 /*
1667 * If the current CPU state is in the range of the AP hotplug thread,
1668 * then we need to kick the thread once more.
1669 */
1670 if (st->state > CPUHP_BRINGUP_CPU) {
1671 ret = cpuhp_kick_ap_work(cpu);
1672 /*
1673 * The AP side has done the error rollback already. Just
1674 * return the error code..
1675 */
1676 if (ret)
1677 goto out;
1678 }
1679
1680 /*
1681 * Try to reach the target state. We max out on the BP at
1682 * CPUHP_BRINGUP_CPU. After that the AP hotplug thread is
1683 * responsible for bringing it up to the target state.
1684 */
1685 target = min((int)target, CPUHP_BRINGUP_CPU);
1686 ret = cpuhp_up_callbacks(cpu, st, target);
1687 out:
1688 cpus_write_unlock();
1689 arch_smt_update();
1690 return ret;
1691 }
1692
cpu_up(unsigned int cpu,enum cpuhp_state target)1693 static int cpu_up(unsigned int cpu, enum cpuhp_state target)
1694 {
1695 int err = 0;
1696
1697 if (!cpu_possible(cpu)) {
1698 pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n",
1699 cpu);
1700 return -EINVAL;
1701 }
1702
1703 err = try_online_node(cpu_to_node(cpu));
1704 if (err)
1705 return err;
1706
1707 cpu_maps_update_begin();
1708
1709 if (cpu_hotplug_disabled) {
1710 err = -EBUSY;
1711 goto out;
1712 }
1713 if (!cpu_bootable(cpu)) {
1714 err = -EPERM;
1715 goto out;
1716 }
1717
1718 err = _cpu_up(cpu, 0, target);
1719 out:
1720 cpu_maps_update_done();
1721 return err;
1722 }
1723
1724 /**
1725 * cpu_device_up - Bring up a cpu device
1726 * @dev: Pointer to the cpu device to online
1727 *
1728 * This function is meant to be used by device core cpu subsystem only.
1729 *
1730 * Other subsystems should use add_cpu() instead.
1731 *
1732 * Return: %0 on success or a negative errno code
1733 */
cpu_device_up(struct device * dev)1734 int cpu_device_up(struct device *dev)
1735 {
1736 return cpu_up(dev->id, CPUHP_ONLINE);
1737 }
1738
add_cpu(unsigned int cpu)1739 int add_cpu(unsigned int cpu)
1740 {
1741 int ret;
1742
1743 lock_device_hotplug();
1744 ret = device_online(get_cpu_device(cpu));
1745 unlock_device_hotplug();
1746
1747 return ret;
1748 }
1749 EXPORT_SYMBOL_GPL(add_cpu);
1750
1751 /**
1752 * bringup_hibernate_cpu - Bring up the CPU that we hibernated on
1753 * @sleep_cpu: The cpu we hibernated on and should be brought up.
1754 *
1755 * On some architectures like arm64, we can hibernate on any CPU, but on
1756 * wake up the CPU we hibernated on might be offline as a side effect of
1757 * using maxcpus= for example.
1758 *
1759 * Return: %0 on success or a negative errno code
1760 */
bringup_hibernate_cpu(unsigned int sleep_cpu)1761 int bringup_hibernate_cpu(unsigned int sleep_cpu)
1762 {
1763 int ret;
1764
1765 if (!cpu_online(sleep_cpu)) {
1766 pr_info("Hibernated on a CPU that is offline! Bringing CPU up.\n");
1767 ret = cpu_up(sleep_cpu, CPUHP_ONLINE);
1768 if (ret) {
1769 pr_err("Failed to bring hibernate-CPU up!\n");
1770 return ret;
1771 }
1772 }
1773 return 0;
1774 }
1775
cpuhp_bringup_mask(const struct cpumask * mask,unsigned int ncpus,enum cpuhp_state target)1776 static void __init cpuhp_bringup_mask(const struct cpumask *mask, unsigned int ncpus,
1777 enum cpuhp_state target)
1778 {
1779 unsigned int cpu;
1780
1781 for_each_cpu(cpu, mask) {
1782 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1783
1784 if (cpu_up(cpu, target) && can_rollback_cpu(st)) {
1785 /*
1786 * If this failed then cpu_up() might have only
1787 * rolled back to CPUHP_BP_KICK_AP for the final
1788 * online. Clean it up. NOOP if already rolled back.
1789 */
1790 WARN_ON(cpuhp_invoke_callback_range(false, cpu, st, CPUHP_OFFLINE));
1791 }
1792
1793 if (!--ncpus)
1794 break;
1795 }
1796 }
1797
1798 #ifdef CONFIG_HOTPLUG_PARALLEL
1799 static bool __cpuhp_parallel_bringup __ro_after_init = true;
1800
parallel_bringup_parse_param(char * arg)1801 static int __init parallel_bringup_parse_param(char *arg)
1802 {
1803 return kstrtobool(arg, &__cpuhp_parallel_bringup);
1804 }
1805 early_param("cpuhp.parallel", parallel_bringup_parse_param);
1806
1807 #ifdef CONFIG_HOTPLUG_SMT
cpuhp_smt_aware(void)1808 static inline bool cpuhp_smt_aware(void)
1809 {
1810 return cpu_smt_max_threads > 1;
1811 }
1812
cpuhp_get_primary_thread_mask(void)1813 static inline const struct cpumask *cpuhp_get_primary_thread_mask(void)
1814 {
1815 return cpu_primary_thread_mask;
1816 }
1817 #else
cpuhp_smt_aware(void)1818 static inline bool cpuhp_smt_aware(void)
1819 {
1820 return false;
1821 }
cpuhp_get_primary_thread_mask(void)1822 static inline const struct cpumask *cpuhp_get_primary_thread_mask(void)
1823 {
1824 return cpu_none_mask;
1825 }
1826 #endif
1827
arch_cpuhp_init_parallel_bringup(void)1828 bool __weak arch_cpuhp_init_parallel_bringup(void)
1829 {
1830 return true;
1831 }
1832
1833 /*
1834 * On architectures which have enabled parallel bringup this invokes all BP
1835 * prepare states for each of the to be onlined APs first. The last state
1836 * sends the startup IPI to the APs. The APs proceed through the low level
1837 * bringup code in parallel and then wait for the control CPU to release
1838 * them one by one for the final onlining procedure.
1839 *
1840 * This avoids waiting for each AP to respond to the startup IPI in
1841 * CPUHP_BRINGUP_CPU.
1842 */
cpuhp_bringup_cpus_parallel(unsigned int ncpus)1843 static bool __init cpuhp_bringup_cpus_parallel(unsigned int ncpus)
1844 {
1845 const struct cpumask *mask = cpu_present_mask;
1846
1847 if (__cpuhp_parallel_bringup)
1848 __cpuhp_parallel_bringup = arch_cpuhp_init_parallel_bringup();
1849 if (!__cpuhp_parallel_bringup)
1850 return false;
1851
1852 if (cpuhp_smt_aware()) {
1853 const struct cpumask *pmask = cpuhp_get_primary_thread_mask();
1854 static struct cpumask tmp_mask __initdata;
1855
1856 /*
1857 * X86 requires to prevent that SMT siblings stopped while
1858 * the primary thread does a microcode update for various
1859 * reasons. Bring the primary threads up first.
1860 */
1861 cpumask_and(&tmp_mask, mask, pmask);
1862 cpuhp_bringup_mask(&tmp_mask, ncpus, CPUHP_BP_KICK_AP);
1863 cpuhp_bringup_mask(&tmp_mask, ncpus, CPUHP_ONLINE);
1864 /* Account for the online CPUs */
1865 ncpus -= num_online_cpus();
1866 if (!ncpus)
1867 return true;
1868 /* Create the mask for secondary CPUs */
1869 cpumask_andnot(&tmp_mask, mask, pmask);
1870 mask = &tmp_mask;
1871 }
1872
1873 /* Bring the not-yet started CPUs up */
1874 cpuhp_bringup_mask(mask, ncpus, CPUHP_BP_KICK_AP);
1875 cpuhp_bringup_mask(mask, ncpus, CPUHP_ONLINE);
1876 return true;
1877 }
1878 #else
cpuhp_bringup_cpus_parallel(unsigned int ncpus)1879 static inline bool cpuhp_bringup_cpus_parallel(unsigned int ncpus) { return false; }
1880 #endif /* CONFIG_HOTPLUG_PARALLEL */
1881
bringup_nonboot_cpus(unsigned int max_cpus)1882 void __init bringup_nonboot_cpus(unsigned int max_cpus)
1883 {
1884 if (!max_cpus)
1885 return;
1886
1887 /* Try parallel bringup optimization if enabled */
1888 if (cpuhp_bringup_cpus_parallel(max_cpus))
1889 return;
1890
1891 /* Full per CPU serialized bringup */
1892 cpuhp_bringup_mask(cpu_present_mask, max_cpus, CPUHP_ONLINE);
1893 }
1894
1895 #ifdef CONFIG_PM_SLEEP_SMP
1896 static cpumask_var_t frozen_cpus;
1897
freeze_secondary_cpus(int primary)1898 int freeze_secondary_cpus(int primary)
1899 {
1900 int cpu, error = 0;
1901
1902 cpu_maps_update_begin();
1903 if (primary == -1) {
1904 primary = cpumask_first(cpu_online_mask);
1905 if (!housekeeping_cpu(primary, HK_TYPE_TIMER))
1906 primary = housekeeping_any_cpu(HK_TYPE_TIMER);
1907 } else {
1908 if (!cpu_online(primary))
1909 primary = cpumask_first(cpu_online_mask);
1910 }
1911
1912 /*
1913 * We take down all of the non-boot CPUs in one shot to avoid races
1914 * with the userspace trying to use the CPU hotplug at the same time
1915 */
1916 cpumask_clear(frozen_cpus);
1917
1918 pr_info("Disabling non-boot CPUs ...\n");
1919 for (cpu = nr_cpu_ids - 1; cpu >= 0; cpu--) {
1920 if (!cpu_online(cpu) || cpu == primary)
1921 continue;
1922
1923 if (pm_wakeup_pending()) {
1924 pr_info("Wakeup pending. Abort CPU freeze\n");
1925 error = -EBUSY;
1926 break;
1927 }
1928
1929 trace_suspend_resume(TPS("CPU_OFF"), cpu, true);
1930 error = _cpu_down(cpu, 1, CPUHP_OFFLINE);
1931 trace_suspend_resume(TPS("CPU_OFF"), cpu, false);
1932 if (!error)
1933 cpumask_set_cpu(cpu, frozen_cpus);
1934 else {
1935 pr_err("Error taking CPU%d down: %d\n", cpu, error);
1936 break;
1937 }
1938 }
1939
1940 if (!error)
1941 BUG_ON(num_online_cpus() > 1);
1942 else
1943 pr_err("Non-boot CPUs are not disabled\n");
1944
1945 /*
1946 * Make sure the CPUs won't be enabled by someone else. We need to do
1947 * this even in case of failure as all freeze_secondary_cpus() users are
1948 * supposed to do thaw_secondary_cpus() on the failure path.
1949 */
1950 cpu_hotplug_disabled++;
1951
1952 cpu_maps_update_done();
1953 return error;
1954 }
1955
arch_thaw_secondary_cpus_begin(void)1956 void __weak arch_thaw_secondary_cpus_begin(void)
1957 {
1958 }
1959
arch_thaw_secondary_cpus_end(void)1960 void __weak arch_thaw_secondary_cpus_end(void)
1961 {
1962 }
1963
thaw_secondary_cpus(void)1964 void thaw_secondary_cpus(void)
1965 {
1966 int cpu, error;
1967
1968 /* Allow everyone to use the CPU hotplug again */
1969 cpu_maps_update_begin();
1970 __cpu_hotplug_enable();
1971 if (cpumask_empty(frozen_cpus))
1972 goto out;
1973
1974 pr_info("Enabling non-boot CPUs ...\n");
1975
1976 arch_thaw_secondary_cpus_begin();
1977
1978 for_each_cpu(cpu, frozen_cpus) {
1979 trace_suspend_resume(TPS("CPU_ON"), cpu, true);
1980 error = _cpu_up(cpu, 1, CPUHP_ONLINE);
1981 trace_suspend_resume(TPS("CPU_ON"), cpu, false);
1982 if (!error) {
1983 pr_info("CPU%d is up\n", cpu);
1984 continue;
1985 }
1986 pr_warn("Error taking CPU%d up: %d\n", cpu, error);
1987 }
1988
1989 arch_thaw_secondary_cpus_end();
1990
1991 cpumask_clear(frozen_cpus);
1992 out:
1993 cpu_maps_update_done();
1994 }
1995
alloc_frozen_cpus(void)1996 static int __init alloc_frozen_cpus(void)
1997 {
1998 if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
1999 return -ENOMEM;
2000 return 0;
2001 }
2002 core_initcall(alloc_frozen_cpus);
2003
2004 /*
2005 * When callbacks for CPU hotplug notifications are being executed, we must
2006 * ensure that the state of the system with respect to the tasks being frozen
2007 * or not, as reported by the notification, remains unchanged *throughout the
2008 * duration* of the execution of the callbacks.
2009 * Hence we need to prevent the freezer from racing with regular CPU hotplug.
2010 *
2011 * This synchronization is implemented by mutually excluding regular CPU
2012 * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
2013 * Hibernate notifications.
2014 */
2015 static int
cpu_hotplug_pm_callback(struct notifier_block * nb,unsigned long action,void * ptr)2016 cpu_hotplug_pm_callback(struct notifier_block *nb,
2017 unsigned long action, void *ptr)
2018 {
2019 switch (action) {
2020
2021 case PM_SUSPEND_PREPARE:
2022 case PM_HIBERNATION_PREPARE:
2023 cpu_hotplug_disable();
2024 break;
2025
2026 case PM_POST_SUSPEND:
2027 case PM_POST_HIBERNATION:
2028 cpu_hotplug_enable();
2029 break;
2030
2031 default:
2032 return NOTIFY_DONE;
2033 }
2034
2035 return NOTIFY_OK;
2036 }
2037
2038
cpu_hotplug_pm_sync_init(void)2039 static int __init cpu_hotplug_pm_sync_init(void)
2040 {
2041 /*
2042 * cpu_hotplug_pm_callback has higher priority than x86
2043 * bsp_pm_callback which depends on cpu_hotplug_pm_callback
2044 * to disable cpu hotplug to avoid cpu hotplug race.
2045 */
2046 pm_notifier(cpu_hotplug_pm_callback, 0);
2047 return 0;
2048 }
2049 core_initcall(cpu_hotplug_pm_sync_init);
2050
2051 #endif /* CONFIG_PM_SLEEP_SMP */
2052
2053 int __boot_cpu_id;
2054
2055 #endif /* CONFIG_SMP */
2056
2057 /* Boot processor state steps */
2058 static struct cpuhp_step cpuhp_hp_states[] = {
2059 [CPUHP_OFFLINE] = {
2060 .name = "offline",
2061 .startup.single = NULL,
2062 .teardown.single = NULL,
2063 },
2064 #ifdef CONFIG_SMP
2065 [CPUHP_CREATE_THREADS]= {
2066 .name = "threads:prepare",
2067 .startup.single = smpboot_create_threads,
2068 .teardown.single = NULL,
2069 .cant_stop = true,
2070 },
2071 [CPUHP_PERF_PREPARE] = {
2072 .name = "perf:prepare",
2073 .startup.single = perf_event_init_cpu,
2074 .teardown.single = perf_event_exit_cpu,
2075 },
2076 [CPUHP_RANDOM_PREPARE] = {
2077 .name = "random:prepare",
2078 .startup.single = random_prepare_cpu,
2079 .teardown.single = NULL,
2080 },
2081 [CPUHP_WORKQUEUE_PREP] = {
2082 .name = "workqueue:prepare",
2083 .startup.single = workqueue_prepare_cpu,
2084 .teardown.single = NULL,
2085 },
2086 [CPUHP_HRTIMERS_PREPARE] = {
2087 .name = "hrtimers:prepare",
2088 .startup.single = hrtimers_prepare_cpu,
2089 .teardown.single = NULL,
2090 },
2091 [CPUHP_SMPCFD_PREPARE] = {
2092 .name = "smpcfd:prepare",
2093 .startup.single = smpcfd_prepare_cpu,
2094 .teardown.single = smpcfd_dead_cpu,
2095 },
2096 [CPUHP_RELAY_PREPARE] = {
2097 .name = "relay:prepare",
2098 .startup.single = relay_prepare_cpu,
2099 .teardown.single = NULL,
2100 },
2101 [CPUHP_RCUTREE_PREP] = {
2102 .name = "RCU/tree:prepare",
2103 .startup.single = rcutree_prepare_cpu,
2104 .teardown.single = rcutree_dead_cpu,
2105 },
2106 /*
2107 * On the tear-down path, timers_dead_cpu() must be invoked
2108 * before blk_mq_queue_reinit_notify() from notify_dead(),
2109 * otherwise a RCU stall occurs.
2110 */
2111 [CPUHP_TIMERS_PREPARE] = {
2112 .name = "timers:prepare",
2113 .startup.single = timers_prepare_cpu,
2114 .teardown.single = timers_dead_cpu,
2115 },
2116
2117 #ifdef CONFIG_HOTPLUG_SPLIT_STARTUP
2118 /*
2119 * Kicks the AP alive. AP will wait in cpuhp_ap_sync_alive() until
2120 * the next step will release it.
2121 */
2122 [CPUHP_BP_KICK_AP] = {
2123 .name = "cpu:kick_ap",
2124 .startup.single = cpuhp_kick_ap_alive,
2125 },
2126
2127 /*
2128 * Waits for the AP to reach cpuhp_ap_sync_alive() and then
2129 * releases it for the complete bringup.
2130 */
2131 [CPUHP_BRINGUP_CPU] = {
2132 .name = "cpu:bringup",
2133 .startup.single = cpuhp_bringup_ap,
2134 .teardown.single = finish_cpu,
2135 .cant_stop = true,
2136 },
2137 #else
2138 /*
2139 * All-in-one CPU bringup state which includes the kick alive.
2140 */
2141 [CPUHP_BRINGUP_CPU] = {
2142 .name = "cpu:bringup",
2143 .startup.single = bringup_cpu,
2144 .teardown.single = finish_cpu,
2145 .cant_stop = true,
2146 },
2147 #endif
2148 /* Final state before CPU kills itself */
2149 [CPUHP_AP_IDLE_DEAD] = {
2150 .name = "idle:dead",
2151 },
2152 /*
2153 * Last state before CPU enters the idle loop to die. Transient state
2154 * for synchronization.
2155 */
2156 [CPUHP_AP_OFFLINE] = {
2157 .name = "ap:offline",
2158 .cant_stop = true,
2159 },
2160 /* First state is scheduler control. Interrupts are disabled */
2161 [CPUHP_AP_SCHED_STARTING] = {
2162 .name = "sched:starting",
2163 .startup.single = sched_cpu_starting,
2164 .teardown.single = sched_cpu_dying,
2165 },
2166 [CPUHP_AP_RCUTREE_DYING] = {
2167 .name = "RCU/tree:dying",
2168 .startup.single = NULL,
2169 .teardown.single = rcutree_dying_cpu,
2170 },
2171 [CPUHP_AP_SMPCFD_DYING] = {
2172 .name = "smpcfd:dying",
2173 .startup.single = NULL,
2174 .teardown.single = smpcfd_dying_cpu,
2175 },
2176 [CPUHP_AP_HRTIMERS_DYING] = {
2177 .name = "hrtimers:dying",
2178 .startup.single = hrtimers_cpu_starting,
2179 .teardown.single = hrtimers_cpu_dying,
2180 },
2181 [CPUHP_AP_TICK_DYING] = {
2182 .name = "tick:dying",
2183 .startup.single = NULL,
2184 .teardown.single = tick_cpu_dying,
2185 },
2186 /* Entry state on starting. Interrupts enabled from here on. Transient
2187 * state for synchronsization */
2188 [CPUHP_AP_ONLINE] = {
2189 .name = "ap:online",
2190 },
2191 /*
2192 * Handled on control processor until the plugged processor manages
2193 * this itself.
2194 */
2195 [CPUHP_TEARDOWN_CPU] = {
2196 .name = "cpu:teardown",
2197 .startup.single = NULL,
2198 .teardown.single = takedown_cpu,
2199 .cant_stop = true,
2200 },
2201
2202 [CPUHP_AP_SCHED_WAIT_EMPTY] = {
2203 .name = "sched:waitempty",
2204 .startup.single = NULL,
2205 .teardown.single = sched_cpu_wait_empty,
2206 },
2207
2208 /* Handle smpboot threads park/unpark */
2209 [CPUHP_AP_SMPBOOT_THREADS] = {
2210 .name = "smpboot/threads:online",
2211 .startup.single = smpboot_unpark_threads,
2212 .teardown.single = smpboot_park_threads,
2213 },
2214 [CPUHP_AP_IRQ_AFFINITY_ONLINE] = {
2215 .name = "irq/affinity:online",
2216 .startup.single = irq_affinity_online_cpu,
2217 .teardown.single = NULL,
2218 },
2219 [CPUHP_AP_PERF_ONLINE] = {
2220 .name = "perf:online",
2221 .startup.single = perf_event_init_cpu,
2222 .teardown.single = perf_event_exit_cpu,
2223 },
2224 [CPUHP_AP_WATCHDOG_ONLINE] = {
2225 .name = "lockup_detector:online",
2226 .startup.single = lockup_detector_online_cpu,
2227 .teardown.single = lockup_detector_offline_cpu,
2228 },
2229 [CPUHP_AP_WORKQUEUE_ONLINE] = {
2230 .name = "workqueue:online",
2231 .startup.single = workqueue_online_cpu,
2232 .teardown.single = workqueue_offline_cpu,
2233 },
2234 [CPUHP_AP_RANDOM_ONLINE] = {
2235 .name = "random:online",
2236 .startup.single = random_online_cpu,
2237 .teardown.single = NULL,
2238 },
2239 [CPUHP_AP_RCUTREE_ONLINE] = {
2240 .name = "RCU/tree:online",
2241 .startup.single = rcutree_online_cpu,
2242 .teardown.single = rcutree_offline_cpu,
2243 },
2244 #endif
2245 /*
2246 * The dynamically registered state space is here
2247 */
2248
2249 #ifdef CONFIG_SMP
2250 /* Last state is scheduler control setting the cpu active */
2251 [CPUHP_AP_ACTIVE] = {
2252 .name = "sched:active",
2253 .startup.single = sched_cpu_activate,
2254 .teardown.single = sched_cpu_deactivate,
2255 },
2256 #endif
2257
2258 /* CPU is fully up and running. */
2259 [CPUHP_ONLINE] = {
2260 .name = "online",
2261 .startup.single = NULL,
2262 .teardown.single = NULL,
2263 },
2264 };
2265
2266 /* Sanity check for callbacks */
cpuhp_cb_check(enum cpuhp_state state)2267 static int cpuhp_cb_check(enum cpuhp_state state)
2268 {
2269 if (state <= CPUHP_OFFLINE || state >= CPUHP_ONLINE)
2270 return -EINVAL;
2271 return 0;
2272 }
2273
2274 /*
2275 * Returns a free for dynamic slot assignment of the Online state. The states
2276 * are protected by the cpuhp_slot_states mutex and an empty slot is identified
2277 * by having no name assigned.
2278 */
cpuhp_reserve_state(enum cpuhp_state state)2279 static int cpuhp_reserve_state(enum cpuhp_state state)
2280 {
2281 enum cpuhp_state i, end;
2282 struct cpuhp_step *step;
2283
2284 switch (state) {
2285 case CPUHP_AP_ONLINE_DYN:
2286 step = cpuhp_hp_states + CPUHP_AP_ONLINE_DYN;
2287 end = CPUHP_AP_ONLINE_DYN_END;
2288 break;
2289 case CPUHP_BP_PREPARE_DYN:
2290 step = cpuhp_hp_states + CPUHP_BP_PREPARE_DYN;
2291 end = CPUHP_BP_PREPARE_DYN_END;
2292 break;
2293 default:
2294 return -EINVAL;
2295 }
2296
2297 for (i = state; i <= end; i++, step++) {
2298 if (!step->name)
2299 return i;
2300 }
2301 WARN(1, "No more dynamic states available for CPU hotplug\n");
2302 return -ENOSPC;
2303 }
2304
cpuhp_store_callbacks(enum cpuhp_state state,const char * name,int (* startup)(unsigned int cpu),int (* teardown)(unsigned int cpu),bool multi_instance)2305 static int cpuhp_store_callbacks(enum cpuhp_state state, const char *name,
2306 int (*startup)(unsigned int cpu),
2307 int (*teardown)(unsigned int cpu),
2308 bool multi_instance)
2309 {
2310 /* (Un)Install the callbacks for further cpu hotplug operations */
2311 struct cpuhp_step *sp;
2312 int ret = 0;
2313
2314 /*
2315 * If name is NULL, then the state gets removed.
2316 *
2317 * CPUHP_AP_ONLINE_DYN and CPUHP_BP_PREPARE_DYN are handed out on
2318 * the first allocation from these dynamic ranges, so the removal
2319 * would trigger a new allocation and clear the wrong (already
2320 * empty) state, leaving the callbacks of the to be cleared state
2321 * dangling, which causes wreckage on the next hotplug operation.
2322 */
2323 if (name && (state == CPUHP_AP_ONLINE_DYN ||
2324 state == CPUHP_BP_PREPARE_DYN)) {
2325 ret = cpuhp_reserve_state(state);
2326 if (ret < 0)
2327 return ret;
2328 state = ret;
2329 }
2330 sp = cpuhp_get_step(state);
2331 if (name && sp->name)
2332 return -EBUSY;
2333
2334 sp->startup.single = startup;
2335 sp->teardown.single = teardown;
2336 sp->name = name;
2337 sp->multi_instance = multi_instance;
2338 INIT_HLIST_HEAD(&sp->list);
2339 return ret;
2340 }
2341
cpuhp_get_teardown_cb(enum cpuhp_state state)2342 static void *cpuhp_get_teardown_cb(enum cpuhp_state state)
2343 {
2344 return cpuhp_get_step(state)->teardown.single;
2345 }
2346
2347 /*
2348 * Call the startup/teardown function for a step either on the AP or
2349 * on the current CPU.
2350 */
cpuhp_issue_call(int cpu,enum cpuhp_state state,bool bringup,struct hlist_node * node)2351 static int cpuhp_issue_call(int cpu, enum cpuhp_state state, bool bringup,
2352 struct hlist_node *node)
2353 {
2354 struct cpuhp_step *sp = cpuhp_get_step(state);
2355 int ret;
2356
2357 /*
2358 * If there's nothing to do, we done.
2359 * Relies on the union for multi_instance.
2360 */
2361 if (cpuhp_step_empty(bringup, sp))
2362 return 0;
2363 /*
2364 * The non AP bound callbacks can fail on bringup. On teardown
2365 * e.g. module removal we crash for now.
2366 */
2367 #ifdef CONFIG_SMP
2368 if (cpuhp_is_ap_state(state))
2369 ret = cpuhp_invoke_ap_callback(cpu, state, bringup, node);
2370 else
2371 ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
2372 #else
2373 ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
2374 #endif
2375 BUG_ON(ret && !bringup);
2376 return ret;
2377 }
2378
2379 /*
2380 * Called from __cpuhp_setup_state on a recoverable failure.
2381 *
2382 * Note: The teardown callbacks for rollback are not allowed to fail!
2383 */
cpuhp_rollback_install(int failedcpu,enum cpuhp_state state,struct hlist_node * node)2384 static void cpuhp_rollback_install(int failedcpu, enum cpuhp_state state,
2385 struct hlist_node *node)
2386 {
2387 int cpu;
2388
2389 /* Roll back the already executed steps on the other cpus */
2390 for_each_present_cpu(cpu) {
2391 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
2392 int cpustate = st->state;
2393
2394 if (cpu >= failedcpu)
2395 break;
2396
2397 /* Did we invoke the startup call on that cpu ? */
2398 if (cpustate >= state)
2399 cpuhp_issue_call(cpu, state, false, node);
2400 }
2401 }
2402
__cpuhp_state_add_instance_cpuslocked(enum cpuhp_state state,struct hlist_node * node,bool invoke)2403 int __cpuhp_state_add_instance_cpuslocked(enum cpuhp_state state,
2404 struct hlist_node *node,
2405 bool invoke)
2406 {
2407 struct cpuhp_step *sp;
2408 int cpu;
2409 int ret;
2410
2411 lockdep_assert_cpus_held();
2412
2413 sp = cpuhp_get_step(state);
2414 if (sp->multi_instance == false)
2415 return -EINVAL;
2416
2417 mutex_lock(&cpuhp_state_mutex);
2418
2419 if (!invoke || !sp->startup.multi)
2420 goto add_node;
2421
2422 /*
2423 * Try to call the startup callback for each present cpu
2424 * depending on the hotplug state of the cpu.
2425 */
2426 for_each_present_cpu(cpu) {
2427 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
2428 int cpustate = st->state;
2429
2430 if (cpustate < state)
2431 continue;
2432
2433 ret = cpuhp_issue_call(cpu, state, true, node);
2434 if (ret) {
2435 if (sp->teardown.multi)
2436 cpuhp_rollback_install(cpu, state, node);
2437 goto unlock;
2438 }
2439 }
2440 add_node:
2441 ret = 0;
2442 hlist_add_head(node, &sp->list);
2443 unlock:
2444 mutex_unlock(&cpuhp_state_mutex);
2445 return ret;
2446 }
2447
__cpuhp_state_add_instance(enum cpuhp_state state,struct hlist_node * node,bool invoke)2448 int __cpuhp_state_add_instance(enum cpuhp_state state, struct hlist_node *node,
2449 bool invoke)
2450 {
2451 int ret;
2452
2453 cpus_read_lock();
2454 ret = __cpuhp_state_add_instance_cpuslocked(state, node, invoke);
2455 cpus_read_unlock();
2456 return ret;
2457 }
2458 EXPORT_SYMBOL_GPL(__cpuhp_state_add_instance);
2459
2460 /**
2461 * __cpuhp_setup_state_cpuslocked - Setup the callbacks for an hotplug machine state
2462 * @state: The state to setup
2463 * @name: Name of the step
2464 * @invoke: If true, the startup function is invoked for cpus where
2465 * cpu state >= @state
2466 * @startup: startup callback function
2467 * @teardown: teardown callback function
2468 * @multi_instance: State is set up for multiple instances which get
2469 * added afterwards.
2470 *
2471 * The caller needs to hold cpus read locked while calling this function.
2472 * Return:
2473 * On success:
2474 * Positive state number if @state is CPUHP_AP_ONLINE_DYN or CPUHP_BP_PREPARE_DYN;
2475 * 0 for all other states
2476 * On failure: proper (negative) error code
2477 */
__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)2478 int __cpuhp_setup_state_cpuslocked(enum cpuhp_state state,
2479 const char *name, bool invoke,
2480 int (*startup)(unsigned int cpu),
2481 int (*teardown)(unsigned int cpu),
2482 bool multi_instance)
2483 {
2484 int cpu, ret = 0;
2485 bool dynstate;
2486
2487 lockdep_assert_cpus_held();
2488
2489 if (cpuhp_cb_check(state) || !name)
2490 return -EINVAL;
2491
2492 mutex_lock(&cpuhp_state_mutex);
2493
2494 ret = cpuhp_store_callbacks(state, name, startup, teardown,
2495 multi_instance);
2496
2497 dynstate = state == CPUHP_AP_ONLINE_DYN || state == CPUHP_BP_PREPARE_DYN;
2498 if (ret > 0 && dynstate) {
2499 state = ret;
2500 ret = 0;
2501 }
2502
2503 if (ret || !invoke || !startup)
2504 goto out;
2505
2506 /*
2507 * Try to call the startup callback for each present cpu
2508 * depending on the hotplug state of the cpu.
2509 */
2510 for_each_present_cpu(cpu) {
2511 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
2512 int cpustate = st->state;
2513
2514 if (cpustate < state)
2515 continue;
2516
2517 ret = cpuhp_issue_call(cpu, state, true, NULL);
2518 if (ret) {
2519 if (teardown)
2520 cpuhp_rollback_install(cpu, state, NULL);
2521 cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
2522 goto out;
2523 }
2524 }
2525 out:
2526 mutex_unlock(&cpuhp_state_mutex);
2527 /*
2528 * If the requested state is CPUHP_AP_ONLINE_DYN or CPUHP_BP_PREPARE_DYN,
2529 * return the dynamically allocated state in case of success.
2530 */
2531 if (!ret && dynstate)
2532 return state;
2533 return ret;
2534 }
2535 EXPORT_SYMBOL(__cpuhp_setup_state_cpuslocked);
2536
__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)2537 int __cpuhp_setup_state(enum cpuhp_state state,
2538 const char *name, bool invoke,
2539 int (*startup)(unsigned int cpu),
2540 int (*teardown)(unsigned int cpu),
2541 bool multi_instance)
2542 {
2543 int ret;
2544
2545 cpus_read_lock();
2546 ret = __cpuhp_setup_state_cpuslocked(state, name, invoke, startup,
2547 teardown, multi_instance);
2548 cpus_read_unlock();
2549 return ret;
2550 }
2551 EXPORT_SYMBOL(__cpuhp_setup_state);
2552
__cpuhp_state_remove_instance(enum cpuhp_state state,struct hlist_node * node,bool invoke)2553 int __cpuhp_state_remove_instance(enum cpuhp_state state,
2554 struct hlist_node *node, bool invoke)
2555 {
2556 struct cpuhp_step *sp = cpuhp_get_step(state);
2557 int cpu;
2558
2559 BUG_ON(cpuhp_cb_check(state));
2560
2561 if (!sp->multi_instance)
2562 return -EINVAL;
2563
2564 cpus_read_lock();
2565 mutex_lock(&cpuhp_state_mutex);
2566
2567 if (!invoke || !cpuhp_get_teardown_cb(state))
2568 goto remove;
2569 /*
2570 * Call the teardown callback for each present cpu depending
2571 * on the hotplug state of the cpu. This function is not
2572 * allowed to fail currently!
2573 */
2574 for_each_present_cpu(cpu) {
2575 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
2576 int cpustate = st->state;
2577
2578 if (cpustate >= state)
2579 cpuhp_issue_call(cpu, state, false, node);
2580 }
2581
2582 remove:
2583 hlist_del(node);
2584 mutex_unlock(&cpuhp_state_mutex);
2585 cpus_read_unlock();
2586
2587 return 0;
2588 }
2589 EXPORT_SYMBOL_GPL(__cpuhp_state_remove_instance);
2590
2591 /**
2592 * __cpuhp_remove_state_cpuslocked - Remove the callbacks for an hotplug machine state
2593 * @state: The state to remove
2594 * @invoke: If true, the teardown function is invoked for cpus where
2595 * cpu state >= @state
2596 *
2597 * The caller needs to hold cpus read locked while calling this function.
2598 * The teardown callback is currently not allowed to fail. Think
2599 * about module removal!
2600 */
__cpuhp_remove_state_cpuslocked(enum cpuhp_state state,bool invoke)2601 void __cpuhp_remove_state_cpuslocked(enum cpuhp_state state, bool invoke)
2602 {
2603 struct cpuhp_step *sp = cpuhp_get_step(state);
2604 int cpu;
2605
2606 BUG_ON(cpuhp_cb_check(state));
2607
2608 lockdep_assert_cpus_held();
2609
2610 mutex_lock(&cpuhp_state_mutex);
2611 if (sp->multi_instance) {
2612 WARN(!hlist_empty(&sp->list),
2613 "Error: Removing state %d which has instances left.\n",
2614 state);
2615 goto remove;
2616 }
2617
2618 if (!invoke || !cpuhp_get_teardown_cb(state))
2619 goto remove;
2620
2621 /*
2622 * Call the teardown callback for each present cpu depending
2623 * on the hotplug state of the cpu. This function is not
2624 * allowed to fail currently!
2625 */
2626 for_each_present_cpu(cpu) {
2627 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
2628 int cpustate = st->state;
2629
2630 if (cpustate >= state)
2631 cpuhp_issue_call(cpu, state, false, NULL);
2632 }
2633 remove:
2634 cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
2635 mutex_unlock(&cpuhp_state_mutex);
2636 }
2637 EXPORT_SYMBOL(__cpuhp_remove_state_cpuslocked);
2638
__cpuhp_remove_state(enum cpuhp_state state,bool invoke)2639 void __cpuhp_remove_state(enum cpuhp_state state, bool invoke)
2640 {
2641 cpus_read_lock();
2642 __cpuhp_remove_state_cpuslocked(state, invoke);
2643 cpus_read_unlock();
2644 }
2645 EXPORT_SYMBOL(__cpuhp_remove_state);
2646
2647 #ifdef CONFIG_HOTPLUG_SMT
cpuhp_offline_cpu_device(unsigned int cpu)2648 static void cpuhp_offline_cpu_device(unsigned int cpu)
2649 {
2650 struct device *dev = get_cpu_device(cpu);
2651
2652 dev->offline = true;
2653 /* Tell user space about the state change */
2654 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2655 }
2656
cpuhp_online_cpu_device(unsigned int cpu)2657 static void cpuhp_online_cpu_device(unsigned int cpu)
2658 {
2659 struct device *dev = get_cpu_device(cpu);
2660
2661 dev->offline = false;
2662 /* Tell user space about the state change */
2663 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2664 }
2665
cpuhp_smt_disable(enum cpuhp_smt_control ctrlval)2666 int cpuhp_smt_disable(enum cpuhp_smt_control ctrlval)
2667 {
2668 int cpu, ret = 0;
2669
2670 cpu_maps_update_begin();
2671 for_each_online_cpu(cpu) {
2672 if (topology_is_primary_thread(cpu))
2673 continue;
2674 /*
2675 * Disable can be called with CPU_SMT_ENABLED when changing
2676 * from a higher to lower number of SMT threads per core.
2677 */
2678 if (ctrlval == CPU_SMT_ENABLED && cpu_smt_thread_allowed(cpu))
2679 continue;
2680 ret = cpu_down_maps_locked(cpu, CPUHP_OFFLINE);
2681 if (ret)
2682 break;
2683 /*
2684 * As this needs to hold the cpu maps lock it's impossible
2685 * to call device_offline() because that ends up calling
2686 * cpu_down() which takes cpu maps lock. cpu maps lock
2687 * needs to be held as this might race against in kernel
2688 * abusers of the hotplug machinery (thermal management).
2689 *
2690 * So nothing would update device:offline state. That would
2691 * leave the sysfs entry stale and prevent onlining after
2692 * smt control has been changed to 'off' again. This is
2693 * called under the sysfs hotplug lock, so it is properly
2694 * serialized against the regular offline usage.
2695 */
2696 cpuhp_offline_cpu_device(cpu);
2697 }
2698 if (!ret)
2699 cpu_smt_control = ctrlval;
2700 cpu_maps_update_done();
2701 return ret;
2702 }
2703
2704 /* Check if the core a CPU belongs to is online */
2705 #if !defined(topology_is_core_online)
topology_is_core_online(unsigned int cpu)2706 static inline bool topology_is_core_online(unsigned int cpu)
2707 {
2708 return true;
2709 }
2710 #endif
2711
cpuhp_smt_enable(void)2712 int cpuhp_smt_enable(void)
2713 {
2714 int cpu, ret = 0;
2715
2716 cpu_maps_update_begin();
2717 cpu_smt_control = CPU_SMT_ENABLED;
2718 for_each_present_cpu(cpu) {
2719 /* Skip online CPUs and CPUs on offline nodes */
2720 if (cpu_online(cpu) || !node_online(cpu_to_node(cpu)))
2721 continue;
2722 if (!cpu_smt_thread_allowed(cpu) || !topology_is_core_online(cpu))
2723 continue;
2724 ret = _cpu_up(cpu, 0, CPUHP_ONLINE);
2725 if (ret)
2726 break;
2727 /* See comment in cpuhp_smt_disable() */
2728 cpuhp_online_cpu_device(cpu);
2729 }
2730 cpu_maps_update_done();
2731 return ret;
2732 }
2733 #endif
2734
2735 #if defined(CONFIG_SYSFS) && defined(CONFIG_HOTPLUG_CPU)
state_show(struct device * dev,struct device_attribute * attr,char * buf)2736 static ssize_t state_show(struct device *dev,
2737 struct device_attribute *attr, char *buf)
2738 {
2739 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
2740
2741 return sprintf(buf, "%d\n", st->state);
2742 }
2743 static DEVICE_ATTR_RO(state);
2744
target_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2745 static ssize_t target_store(struct device *dev, struct device_attribute *attr,
2746 const char *buf, size_t count)
2747 {
2748 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
2749 struct cpuhp_step *sp;
2750 int target, ret;
2751
2752 ret = kstrtoint(buf, 10, &target);
2753 if (ret)
2754 return ret;
2755
2756 #ifdef CONFIG_CPU_HOTPLUG_STATE_CONTROL
2757 if (target < CPUHP_OFFLINE || target > CPUHP_ONLINE)
2758 return -EINVAL;
2759 #else
2760 if (target != CPUHP_OFFLINE && target != CPUHP_ONLINE)
2761 return -EINVAL;
2762 #endif
2763
2764 ret = lock_device_hotplug_sysfs();
2765 if (ret)
2766 return ret;
2767
2768 mutex_lock(&cpuhp_state_mutex);
2769 sp = cpuhp_get_step(target);
2770 ret = !sp->name || sp->cant_stop ? -EINVAL : 0;
2771 mutex_unlock(&cpuhp_state_mutex);
2772 if (ret)
2773 goto out;
2774
2775 if (st->state < target)
2776 ret = cpu_up(dev->id, target);
2777 else if (st->state > target)
2778 ret = cpu_down(dev->id, target);
2779 else if (WARN_ON(st->target != target))
2780 st->target = target;
2781 out:
2782 unlock_device_hotplug();
2783 return ret ? ret : count;
2784 }
2785
target_show(struct device * dev,struct device_attribute * attr,char * buf)2786 static ssize_t target_show(struct device *dev,
2787 struct device_attribute *attr, char *buf)
2788 {
2789 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
2790
2791 return sprintf(buf, "%d\n", st->target);
2792 }
2793 static DEVICE_ATTR_RW(target);
2794
fail_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2795 static ssize_t fail_store(struct device *dev, struct device_attribute *attr,
2796 const char *buf, size_t count)
2797 {
2798 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
2799 struct cpuhp_step *sp;
2800 int fail, ret;
2801
2802 ret = kstrtoint(buf, 10, &fail);
2803 if (ret)
2804 return ret;
2805
2806 if (fail == CPUHP_INVALID) {
2807 st->fail = fail;
2808 return count;
2809 }
2810
2811 if (fail < CPUHP_OFFLINE || fail > CPUHP_ONLINE)
2812 return -EINVAL;
2813
2814 /*
2815 * Cannot fail STARTING/DYING callbacks.
2816 */
2817 if (cpuhp_is_atomic_state(fail))
2818 return -EINVAL;
2819
2820 /*
2821 * DEAD callbacks cannot fail...
2822 * ... neither can CPUHP_BRINGUP_CPU during hotunplug. The latter
2823 * triggering STARTING callbacks, a failure in this state would
2824 * hinder rollback.
2825 */
2826 if (fail <= CPUHP_BRINGUP_CPU && st->state > CPUHP_BRINGUP_CPU)
2827 return -EINVAL;
2828
2829 /*
2830 * Cannot fail anything that doesn't have callbacks.
2831 */
2832 mutex_lock(&cpuhp_state_mutex);
2833 sp = cpuhp_get_step(fail);
2834 if (!sp->startup.single && !sp->teardown.single)
2835 ret = -EINVAL;
2836 mutex_unlock(&cpuhp_state_mutex);
2837 if (ret)
2838 return ret;
2839
2840 st->fail = fail;
2841
2842 return count;
2843 }
2844
fail_show(struct device * dev,struct device_attribute * attr,char * buf)2845 static ssize_t fail_show(struct device *dev,
2846 struct device_attribute *attr, char *buf)
2847 {
2848 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
2849
2850 return sprintf(buf, "%d\n", st->fail);
2851 }
2852
2853 static DEVICE_ATTR_RW(fail);
2854
2855 static struct attribute *cpuhp_cpu_attrs[] = {
2856 &dev_attr_state.attr,
2857 &dev_attr_target.attr,
2858 &dev_attr_fail.attr,
2859 NULL
2860 };
2861
2862 static const struct attribute_group cpuhp_cpu_attr_group = {
2863 .attrs = cpuhp_cpu_attrs,
2864 .name = "hotplug",
2865 };
2866
states_show(struct device * dev,struct device_attribute * attr,char * buf)2867 static ssize_t states_show(struct device *dev,
2868 struct device_attribute *attr, char *buf)
2869 {
2870 ssize_t cur, res = 0;
2871 int i;
2872
2873 mutex_lock(&cpuhp_state_mutex);
2874 for (i = CPUHP_OFFLINE; i <= CPUHP_ONLINE; i++) {
2875 struct cpuhp_step *sp = cpuhp_get_step(i);
2876
2877 if (sp->name) {
2878 cur = sprintf(buf, "%3d: %s\n", i, sp->name);
2879 buf += cur;
2880 res += cur;
2881 }
2882 }
2883 mutex_unlock(&cpuhp_state_mutex);
2884 return res;
2885 }
2886 static DEVICE_ATTR_RO(states);
2887
2888 static struct attribute *cpuhp_cpu_root_attrs[] = {
2889 &dev_attr_states.attr,
2890 NULL
2891 };
2892
2893 static const struct attribute_group cpuhp_cpu_root_attr_group = {
2894 .attrs = cpuhp_cpu_root_attrs,
2895 .name = "hotplug",
2896 };
2897
2898 #ifdef CONFIG_HOTPLUG_SMT
2899
cpu_smt_num_threads_valid(unsigned int threads)2900 static bool cpu_smt_num_threads_valid(unsigned int threads)
2901 {
2902 if (IS_ENABLED(CONFIG_SMT_NUM_THREADS_DYNAMIC))
2903 return threads >= 1 && threads <= cpu_smt_max_threads;
2904 return threads == 1 || threads == cpu_smt_max_threads;
2905 }
2906
2907 static ssize_t
__store_smt_control(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2908 __store_smt_control(struct device *dev, struct device_attribute *attr,
2909 const char *buf, size_t count)
2910 {
2911 int ctrlval, ret, num_threads, orig_threads;
2912 bool force_off;
2913
2914 if (cpu_smt_control == CPU_SMT_FORCE_DISABLED)
2915 return -EPERM;
2916
2917 if (cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
2918 return -ENODEV;
2919
2920 if (sysfs_streq(buf, "on")) {
2921 ctrlval = CPU_SMT_ENABLED;
2922 num_threads = cpu_smt_max_threads;
2923 } else if (sysfs_streq(buf, "off")) {
2924 ctrlval = CPU_SMT_DISABLED;
2925 num_threads = 1;
2926 } else if (sysfs_streq(buf, "forceoff")) {
2927 ctrlval = CPU_SMT_FORCE_DISABLED;
2928 num_threads = 1;
2929 } else if (kstrtoint(buf, 10, &num_threads) == 0) {
2930 if (num_threads == 1)
2931 ctrlval = CPU_SMT_DISABLED;
2932 else if (cpu_smt_num_threads_valid(num_threads))
2933 ctrlval = CPU_SMT_ENABLED;
2934 else
2935 return -EINVAL;
2936 } else {
2937 return -EINVAL;
2938 }
2939
2940 ret = lock_device_hotplug_sysfs();
2941 if (ret)
2942 return ret;
2943
2944 orig_threads = cpu_smt_num_threads;
2945 cpu_smt_num_threads = num_threads;
2946
2947 force_off = ctrlval != cpu_smt_control && ctrlval == CPU_SMT_FORCE_DISABLED;
2948
2949 if (num_threads > orig_threads)
2950 ret = cpuhp_smt_enable();
2951 else if (num_threads < orig_threads || force_off)
2952 ret = cpuhp_smt_disable(ctrlval);
2953
2954 unlock_device_hotplug();
2955 return ret ? ret : count;
2956 }
2957
2958 #else /* !CONFIG_HOTPLUG_SMT */
2959 static ssize_t
__store_smt_control(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2960 __store_smt_control(struct device *dev, struct device_attribute *attr,
2961 const char *buf, size_t count)
2962 {
2963 return -ENODEV;
2964 }
2965 #endif /* CONFIG_HOTPLUG_SMT */
2966
2967 static const char *smt_states[] = {
2968 [CPU_SMT_ENABLED] = "on",
2969 [CPU_SMT_DISABLED] = "off",
2970 [CPU_SMT_FORCE_DISABLED] = "forceoff",
2971 [CPU_SMT_NOT_SUPPORTED] = "notsupported",
2972 [CPU_SMT_NOT_IMPLEMENTED] = "notimplemented",
2973 };
2974
control_show(struct device * dev,struct device_attribute * attr,char * buf)2975 static ssize_t control_show(struct device *dev,
2976 struct device_attribute *attr, char *buf)
2977 {
2978 const char *state = smt_states[cpu_smt_control];
2979
2980 #ifdef CONFIG_HOTPLUG_SMT
2981 /*
2982 * If SMT is enabled but not all threads are enabled then show the
2983 * number of threads. If all threads are enabled show "on". Otherwise
2984 * show the state name.
2985 */
2986 if (cpu_smt_control == CPU_SMT_ENABLED &&
2987 cpu_smt_num_threads != cpu_smt_max_threads)
2988 return sysfs_emit(buf, "%d\n", cpu_smt_num_threads);
2989 #endif
2990
2991 return sysfs_emit(buf, "%s\n", state);
2992 }
2993
control_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2994 static ssize_t control_store(struct device *dev, struct device_attribute *attr,
2995 const char *buf, size_t count)
2996 {
2997 return __store_smt_control(dev, attr, buf, count);
2998 }
2999 static DEVICE_ATTR_RW(control);
3000
active_show(struct device * dev,struct device_attribute * attr,char * buf)3001 static ssize_t active_show(struct device *dev,
3002 struct device_attribute *attr, char *buf)
3003 {
3004 return sysfs_emit(buf, "%d\n", sched_smt_active());
3005 }
3006 static DEVICE_ATTR_RO(active);
3007
3008 static struct attribute *cpuhp_smt_attrs[] = {
3009 &dev_attr_control.attr,
3010 &dev_attr_active.attr,
3011 NULL
3012 };
3013
3014 static const struct attribute_group cpuhp_smt_attr_group = {
3015 .attrs = cpuhp_smt_attrs,
3016 .name = "smt",
3017 };
3018
cpu_smt_sysfs_init(void)3019 static int __init cpu_smt_sysfs_init(void)
3020 {
3021 struct device *dev_root;
3022 int ret = -ENODEV;
3023
3024 dev_root = bus_get_dev_root(&cpu_subsys);
3025 if (dev_root) {
3026 ret = sysfs_create_group(&dev_root->kobj, &cpuhp_smt_attr_group);
3027 put_device(dev_root);
3028 }
3029 return ret;
3030 }
3031
cpuhp_sysfs_init(void)3032 static int __init cpuhp_sysfs_init(void)
3033 {
3034 struct device *dev_root;
3035 int cpu, ret;
3036
3037 ret = cpu_smt_sysfs_init();
3038 if (ret)
3039 return ret;
3040
3041 dev_root = bus_get_dev_root(&cpu_subsys);
3042 if (dev_root) {
3043 ret = sysfs_create_group(&dev_root->kobj, &cpuhp_cpu_root_attr_group);
3044 put_device(dev_root);
3045 if (ret)
3046 return ret;
3047 }
3048
3049 for_each_possible_cpu(cpu) {
3050 struct device *dev = get_cpu_device(cpu);
3051
3052 if (!dev)
3053 continue;
3054 ret = sysfs_create_group(&dev->kobj, &cpuhp_cpu_attr_group);
3055 if (ret)
3056 return ret;
3057 }
3058 return 0;
3059 }
3060 device_initcall(cpuhp_sysfs_init);
3061 #endif /* CONFIG_SYSFS && CONFIG_HOTPLUG_CPU */
3062
3063 /*
3064 * cpu_bit_bitmap[] is a special, "compressed" data structure that
3065 * represents all NR_CPUS bits binary values of 1<<nr.
3066 *
3067 * It is used by cpumask_of() to get a constant address to a CPU
3068 * mask value that has a single bit set only.
3069 */
3070
3071 /* cpu_bit_bitmap[0] is empty - so we can back into it */
3072 #define MASK_DECLARE_1(x) [x+1][0] = (1UL << (x))
3073 #define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
3074 #define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
3075 #define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
3076
3077 const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
3078
3079 MASK_DECLARE_8(0), MASK_DECLARE_8(8),
3080 MASK_DECLARE_8(16), MASK_DECLARE_8(24),
3081 #if BITS_PER_LONG > 32
3082 MASK_DECLARE_8(32), MASK_DECLARE_8(40),
3083 MASK_DECLARE_8(48), MASK_DECLARE_8(56),
3084 #endif
3085 };
3086 EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
3087
3088 const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
3089 EXPORT_SYMBOL(cpu_all_bits);
3090
3091 #ifdef CONFIG_INIT_ALL_POSSIBLE
3092 struct cpumask __cpu_possible_mask __ro_after_init
3093 = {CPU_BITS_ALL};
3094 #else
3095 struct cpumask __cpu_possible_mask __ro_after_init;
3096 #endif
3097 EXPORT_SYMBOL(__cpu_possible_mask);
3098
3099 struct cpumask __cpu_online_mask __read_mostly;
3100 EXPORT_SYMBOL(__cpu_online_mask);
3101
3102 struct cpumask __cpu_enabled_mask __read_mostly;
3103 EXPORT_SYMBOL(__cpu_enabled_mask);
3104
3105 struct cpumask __cpu_present_mask __read_mostly;
3106 EXPORT_SYMBOL(__cpu_present_mask);
3107
3108 struct cpumask __cpu_active_mask __read_mostly;
3109 EXPORT_SYMBOL(__cpu_active_mask);
3110
3111 struct cpumask __cpu_dying_mask __read_mostly;
3112 EXPORT_SYMBOL(__cpu_dying_mask);
3113
3114 atomic_t __num_online_cpus __read_mostly;
3115 EXPORT_SYMBOL(__num_online_cpus);
3116
init_cpu_present(const struct cpumask * src)3117 void init_cpu_present(const struct cpumask *src)
3118 {
3119 cpumask_copy(&__cpu_present_mask, src);
3120 }
3121
init_cpu_possible(const struct cpumask * src)3122 void init_cpu_possible(const struct cpumask *src)
3123 {
3124 cpumask_copy(&__cpu_possible_mask, src);
3125 }
3126
set_cpu_online(unsigned int cpu,bool online)3127 void set_cpu_online(unsigned int cpu, bool online)
3128 {
3129 /*
3130 * atomic_inc/dec() is required to handle the horrid abuse of this
3131 * function by the reboot and kexec code which invoke it from
3132 * IPI/NMI broadcasts when shutting down CPUs. Invocation from
3133 * regular CPU hotplug is properly serialized.
3134 *
3135 * Note, that the fact that __num_online_cpus is of type atomic_t
3136 * does not protect readers which are not serialized against
3137 * concurrent hotplug operations.
3138 */
3139 if (online) {
3140 if (!cpumask_test_and_set_cpu(cpu, &__cpu_online_mask))
3141 atomic_inc(&__num_online_cpus);
3142 } else {
3143 if (cpumask_test_and_clear_cpu(cpu, &__cpu_online_mask))
3144 atomic_dec(&__num_online_cpus);
3145 }
3146 }
3147
3148 /*
3149 * Activate the first processor.
3150 */
boot_cpu_init(void)3151 void __init boot_cpu_init(void)
3152 {
3153 int cpu = smp_processor_id();
3154
3155 /* Mark the boot cpu "present", "online" etc for SMP and UP case */
3156 set_cpu_online(cpu, true);
3157 set_cpu_active(cpu, true);
3158 set_cpu_present(cpu, true);
3159 set_cpu_possible(cpu, true);
3160
3161 #ifdef CONFIG_SMP
3162 __boot_cpu_id = cpu;
3163 #endif
3164 }
3165
3166 /*
3167 * Must be called _AFTER_ setting up the per_cpu areas
3168 */
boot_cpu_hotplug_init(void)3169 void __init boot_cpu_hotplug_init(void)
3170 {
3171 #ifdef CONFIG_SMP
3172 cpumask_set_cpu(smp_processor_id(), &cpus_booted_once_mask);
3173 atomic_set(this_cpu_ptr(&cpuhp_state.ap_sync_state), SYNC_STATE_ONLINE);
3174 #endif
3175 this_cpu_write(cpuhp_state.state, CPUHP_ONLINE);
3176 this_cpu_write(cpuhp_state.target, CPUHP_ONLINE);
3177 }
3178
3179 #ifdef CONFIG_CPU_MITIGATIONS
3180 /*
3181 * These are used for a global "mitigations=" cmdline option for toggling
3182 * optional CPU mitigations.
3183 */
3184 enum cpu_mitigations {
3185 CPU_MITIGATIONS_OFF,
3186 CPU_MITIGATIONS_AUTO,
3187 CPU_MITIGATIONS_AUTO_NOSMT,
3188 };
3189
3190 static enum cpu_mitigations cpu_mitigations __ro_after_init = CPU_MITIGATIONS_AUTO;
3191
mitigations_parse_cmdline(char * arg)3192 static int __init mitigations_parse_cmdline(char *arg)
3193 {
3194 if (!strcmp(arg, "off"))
3195 cpu_mitigations = CPU_MITIGATIONS_OFF;
3196 else if (!strcmp(arg, "auto"))
3197 cpu_mitigations = CPU_MITIGATIONS_AUTO;
3198 else if (!strcmp(arg, "auto,nosmt"))
3199 cpu_mitigations = CPU_MITIGATIONS_AUTO_NOSMT;
3200 else
3201 pr_crit("Unsupported mitigations=%s, system may still be vulnerable\n",
3202 arg);
3203
3204 return 0;
3205 }
3206
3207 /* mitigations=off */
cpu_mitigations_off(void)3208 bool cpu_mitigations_off(void)
3209 {
3210 return cpu_mitigations == CPU_MITIGATIONS_OFF;
3211 }
3212 EXPORT_SYMBOL_GPL(cpu_mitigations_off);
3213
3214 /* mitigations=auto,nosmt */
cpu_mitigations_auto_nosmt(void)3215 bool cpu_mitigations_auto_nosmt(void)
3216 {
3217 return cpu_mitigations == CPU_MITIGATIONS_AUTO_NOSMT;
3218 }
3219 EXPORT_SYMBOL_GPL(cpu_mitigations_auto_nosmt);
3220 #else
mitigations_parse_cmdline(char * arg)3221 static int __init mitigations_parse_cmdline(char *arg)
3222 {
3223 pr_crit("Kernel compiled without mitigations, ignoring 'mitigations'; system may still be vulnerable\n");
3224 return 0;
3225 }
3226 #endif
3227 early_param("mitigations", mitigations_parse_cmdline);
3228