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