2 * (C) 2001, 2002, 2003, 2004 Rusty Russell
4 * This code is licenced under the GPL.
6 #include <linux/proc_fs.h>
8 #include <linux/init.h>
9 #include <linux/notifier.h>
10 #include <linux/sched/signal.h>
11 #include <linux/sched/hotplug.h>
12 #include <linux/sched/task.h>
13 #include <linux/sched/smt.h>
14 #include <linux/unistd.h>
15 #include <linux/cpu.h>
16 #include <linux/oom.h>
17 #include <linux/rcupdate.h>
18 #include <linux/export.h>
19 #include <linux/bug.h>
20 #include <linux/kthread.h>
21 #include <linux/stop_machine.h>
22 #include <linux/mutex.h>
23 #include <linux/gfp.h>
24 #include <linux/suspend.h>
25 #include <linux/lockdep.h>
26 #include <linux/tick.h>
27 #include <linux/irq.h>
28 #include <linux/nmi.h>
29 #include <linux/smpboot.h>
30 #include <linux/relay.h>
31 #include <linux/slab.h>
32 #include <linux/percpu-rwsem.h>
33 #include <linux/cpuset.h>
35 #include <trace/events/power.h>
36 #define CREATE_TRACE_POINTS
37 #include <trace/events/cpuhp.h>
42 * cpuhp_cpu_state - Per cpu hotplug state storage
43 * @state: The current cpu state
44 * @target: The target state
45 * @thread: Pointer to the hotplug thread
46 * @should_run: Thread should execute
47 * @rollback: Perform a rollback
48 * @single: Single callback invocation
49 * @bringup: Single callback bringup or teardown selector
50 * @cb_state: The state for a single callback (install/uninstall)
51 * @result: Result of the operation
52 * @done_up: Signal completion to the issuer of the task for cpu-up
53 * @done_down: Signal completion to the issuer of the task for cpu-down
55 struct cpuhp_cpu_state {
56 enum cpuhp_state state;
57 enum cpuhp_state target;
58 enum cpuhp_state fail;
60 struct task_struct *thread;
66 struct hlist_node *node;
67 struct hlist_node *last;
68 enum cpuhp_state cb_state;
70 struct completion done_up;
71 struct completion done_down;
75 static DEFINE_PER_CPU(struct cpuhp_cpu_state, cpuhp_state) = {
76 .fail = CPUHP_INVALID,
79 #if defined(CONFIG_LOCKDEP) && defined(CONFIG_SMP)
80 static struct lockdep_map cpuhp_state_up_map =
81 STATIC_LOCKDEP_MAP_INIT("cpuhp_state-up", &cpuhp_state_up_map);
82 static struct lockdep_map cpuhp_state_down_map =
83 STATIC_LOCKDEP_MAP_INIT("cpuhp_state-down", &cpuhp_state_down_map);
86 static void inline cpuhp_lock_acquire(bool bringup)
88 lock_map_acquire(bringup ? &cpuhp_state_up_map : &cpuhp_state_down_map);
91 static void inline cpuhp_lock_release(bool bringup)
93 lock_map_release(bringup ? &cpuhp_state_up_map : &cpuhp_state_down_map);
97 static void inline cpuhp_lock_acquire(bool bringup) { }
98 static void inline cpuhp_lock_release(bool bringup) { }
103 * cpuhp_step - Hotplug state machine step
104 * @name: Name of the step
105 * @startup: Startup function of the step
106 * @teardown: Teardown function of the step
107 * @skip_onerr: Do not invoke the functions on error rollback
108 * Will go away once the notifiers are gone
109 * @cant_stop: Bringup/teardown can't be stopped at this step
114 int (*single)(unsigned int cpu);
115 int (*multi)(unsigned int cpu,
116 struct hlist_node *node);
119 int (*single)(unsigned int cpu);
120 int (*multi)(unsigned int cpu,
121 struct hlist_node *node);
123 struct hlist_head list;
129 static DEFINE_MUTEX(cpuhp_state_mutex);
130 static struct cpuhp_step cpuhp_bp_states[];
131 static struct cpuhp_step cpuhp_ap_states[];
133 static bool cpuhp_is_ap_state(enum cpuhp_state state)
136 * The extra check for CPUHP_TEARDOWN_CPU is only for documentation
137 * purposes as that state is handled explicitly in cpu_down.
139 return state > CPUHP_BRINGUP_CPU && state != CPUHP_TEARDOWN_CPU;
142 static struct cpuhp_step *cpuhp_get_step(enum cpuhp_state state)
144 struct cpuhp_step *sp;
146 sp = cpuhp_is_ap_state(state) ? cpuhp_ap_states : cpuhp_bp_states;
151 * cpuhp_invoke_callback _ Invoke the callbacks for a given state
152 * @cpu: The cpu for which the callback should be invoked
153 * @state: The state to do callbacks for
154 * @bringup: True if the bringup callback should be invoked
155 * @node: For multi-instance, do a single entry callback for install/remove
156 * @lastp: For multi-instance rollback, remember how far we got
158 * Called from cpu hotplug and from the state register machinery.
160 static int cpuhp_invoke_callback(unsigned int cpu, enum cpuhp_state state,
161 bool bringup, struct hlist_node *node,
162 struct hlist_node **lastp)
164 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
165 struct cpuhp_step *step = cpuhp_get_step(state);
166 int (*cbm)(unsigned int cpu, struct hlist_node *node);
167 int (*cb)(unsigned int cpu);
170 if (st->fail == state) {
171 st->fail = CPUHP_INVALID;
173 if (!(bringup ? step->startup.single : step->teardown.single))
179 if (!step->multi_instance) {
180 WARN_ON_ONCE(lastp && *lastp);
181 cb = bringup ? step->startup.single : step->teardown.single;
184 trace_cpuhp_enter(cpu, st->target, state, cb);
186 trace_cpuhp_exit(cpu, st->state, state, ret);
189 cbm = bringup ? step->startup.multi : step->teardown.multi;
193 /* Single invocation for instance add/remove */
195 WARN_ON_ONCE(lastp && *lastp);
196 trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
197 ret = cbm(cpu, node);
198 trace_cpuhp_exit(cpu, st->state, state, ret);
202 /* State transition. Invoke on all instances */
204 hlist_for_each(node, &step->list) {
205 if (lastp && node == *lastp)
208 trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
209 ret = cbm(cpu, node);
210 trace_cpuhp_exit(cpu, st->state, state, ret);
224 /* Rollback the instances if one failed */
225 cbm = !bringup ? step->startup.multi : step->teardown.multi;
229 hlist_for_each(node, &step->list) {
233 trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
234 ret = cbm(cpu, node);
235 trace_cpuhp_exit(cpu, st->state, state, ret);
237 * Rollback must not fail,
245 static inline void wait_for_ap_thread(struct cpuhp_cpu_state *st, bool bringup)
247 struct completion *done = bringup ? &st->done_up : &st->done_down;
248 wait_for_completion(done);
251 static inline void complete_ap_thread(struct cpuhp_cpu_state *st, bool bringup)
253 struct completion *done = bringup ? &st->done_up : &st->done_down;
258 * The former STARTING/DYING states, ran with IRQs disabled and must not fail.
260 static bool cpuhp_is_atomic_state(enum cpuhp_state state)
262 return CPUHP_AP_IDLE_DEAD <= state && state < CPUHP_AP_ONLINE;
265 /* Serializes the updates to cpu_online_mask, cpu_present_mask */
266 static DEFINE_MUTEX(cpu_add_remove_lock);
267 bool cpuhp_tasks_frozen;
268 EXPORT_SYMBOL_GPL(cpuhp_tasks_frozen);
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.
274 void cpu_maps_update_begin(void)
276 mutex_lock(&cpu_add_remove_lock);
279 void cpu_maps_update_done(void)
281 mutex_unlock(&cpu_add_remove_lock);
285 * If set, cpu_up and cpu_down will return -EBUSY and do nothing.
286 * Should always be manipulated under cpu_add_remove_lock
288 static int cpu_hotplug_disabled;
290 #ifdef CONFIG_HOTPLUG_CPU
292 DEFINE_STATIC_PERCPU_RWSEM(cpu_hotplug_lock);
294 void cpus_read_lock(void)
296 percpu_down_read(&cpu_hotplug_lock);
298 EXPORT_SYMBOL_GPL(cpus_read_lock);
300 void cpus_read_unlock(void)
302 percpu_up_read(&cpu_hotplug_lock);
304 EXPORT_SYMBOL_GPL(cpus_read_unlock);
306 void cpus_write_lock(void)
308 percpu_down_write(&cpu_hotplug_lock);
311 void cpus_write_unlock(void)
313 percpu_up_write(&cpu_hotplug_lock);
316 void lockdep_assert_cpus_held(void)
319 * We can't have hotplug operations before userspace starts running,
320 * and some init codepaths will knowingly not take the hotplug lock.
321 * This is all valid, so mute lockdep until it makes sense to report
324 if (system_state < SYSTEM_RUNNING)
327 percpu_rwsem_assert_held(&cpu_hotplug_lock);
331 * Wait for currently running CPU hotplug operations to complete (if any) and
332 * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
333 * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the
334 * hotplug path before performing hotplug operations. So acquiring that lock
335 * guarantees mutual exclusion from any currently running hotplug operations.
337 void cpu_hotplug_disable(void)
339 cpu_maps_update_begin();
340 cpu_hotplug_disabled++;
341 cpu_maps_update_done();
343 EXPORT_SYMBOL_GPL(cpu_hotplug_disable);
345 static void __cpu_hotplug_enable(void)
347 if (WARN_ONCE(!cpu_hotplug_disabled, "Unbalanced cpu hotplug enable\n"))
349 cpu_hotplug_disabled--;
352 void cpu_hotplug_enable(void)
354 cpu_maps_update_begin();
355 __cpu_hotplug_enable();
356 cpu_maps_update_done();
358 EXPORT_SYMBOL_GPL(cpu_hotplug_enable);
359 #endif /* CONFIG_HOTPLUG_CPU */
362 * Architectures that need SMT-specific errata handling during SMT hotplug
363 * should override this.
365 void __weak arch_smt_update(void) { }
367 #ifdef CONFIG_HOTPLUG_SMT
368 enum cpuhp_smt_control cpu_smt_control __read_mostly = CPU_SMT_ENABLED;
370 void __init cpu_smt_disable(bool force)
372 if (cpu_smt_control == CPU_SMT_FORCE_DISABLED ||
373 cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
377 pr_info("SMT: Force disabled\n");
378 cpu_smt_control = CPU_SMT_FORCE_DISABLED;
380 pr_info("SMT: disabled\n");
381 cpu_smt_control = CPU_SMT_DISABLED;
386 * The decision whether SMT is supported can only be done after the full
387 * CPU identification. Called from architecture code.
389 void __init cpu_smt_check_topology(void)
391 if (!topology_smt_supported())
392 cpu_smt_control = CPU_SMT_NOT_SUPPORTED;
395 static int __init smt_cmdline_disable(char *str)
397 cpu_smt_disable(str && !strcmp(str, "force"));
400 early_param("nosmt", smt_cmdline_disable);
402 static inline bool cpu_smt_allowed(unsigned int cpu)
404 if (cpu_smt_control == CPU_SMT_ENABLED)
407 if (topology_is_primary_thread(cpu))
411 * On x86 it's required to boot all logical CPUs at least once so
412 * that the init code can get a chance to set CR4.MCE on each
413 * CPU. Otherwise, a broadacasted MCE observing CR4.MCE=0b on any
414 * core will shutdown the machine.
416 return !per_cpu(cpuhp_state, cpu).booted_once;
419 static inline bool cpu_smt_allowed(unsigned int cpu) { return true; }
422 static inline enum cpuhp_state
423 cpuhp_set_state(struct cpuhp_cpu_state *st, enum cpuhp_state target)
425 enum cpuhp_state prev_state = st->state;
427 st->rollback = false;
432 st->bringup = st->state < target;
438 cpuhp_reset_state(struct cpuhp_cpu_state *st, enum cpuhp_state prev_state)
443 * If we have st->last we need to undo partial multi_instance of this
444 * state first. Otherwise start undo at the previous state.
453 st->target = prev_state;
454 st->bringup = !st->bringup;
457 /* Regular hotplug invocation of the AP hotplug thread */
458 static void __cpuhp_kick_ap(struct cpuhp_cpu_state *st)
460 if (!st->single && st->state == st->target)
465 * Make sure the above stores are visible before should_run becomes
466 * true. Paired with the mb() above in cpuhp_thread_fun()
469 st->should_run = true;
470 wake_up_process(st->thread);
471 wait_for_ap_thread(st, st->bringup);
474 static int cpuhp_kick_ap(struct cpuhp_cpu_state *st, enum cpuhp_state target)
476 enum cpuhp_state prev_state;
479 prev_state = cpuhp_set_state(st, target);
481 if ((ret = st->result)) {
482 cpuhp_reset_state(st, prev_state);
489 static int bringup_wait_for_ap(unsigned int cpu)
491 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
493 /* Wait for the CPU to reach CPUHP_AP_ONLINE_IDLE */
494 wait_for_ap_thread(st, true);
495 if (WARN_ON_ONCE((!cpu_online(cpu))))
498 /* Unpark the hotplug thread of the target cpu */
499 kthread_unpark(st->thread);
502 * SMT soft disabling on X86 requires to bring the CPU out of the
503 * BIOS 'wait for SIPI' state in order to set the CR4.MCE bit. The
504 * CPU marked itself as booted_once in cpu_notify_starting() so the
505 * cpu_smt_allowed() check will now return false if this is not the
508 if (!cpu_smt_allowed(cpu))
511 if (st->target <= CPUHP_AP_ONLINE_IDLE)
514 return cpuhp_kick_ap(st, st->target);
517 static int bringup_cpu(unsigned int cpu)
519 struct task_struct *idle = idle_thread_get(cpu);
523 * Some architectures have to walk the irq descriptors to
524 * setup the vector space for the cpu which comes online.
525 * Prevent irq alloc/free across the bringup.
529 /* Arch-specific enabling code. */
530 ret = __cpu_up(cpu, idle);
534 return bringup_wait_for_ap(cpu);
538 * Hotplug state machine related functions
541 static void undo_cpu_up(unsigned int cpu, struct cpuhp_cpu_state *st)
543 for (st->state--; st->state > st->target; st->state--) {
544 struct cpuhp_step *step = cpuhp_get_step(st->state);
546 if (!step->skip_onerr)
547 cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
551 static inline bool can_rollback_cpu(struct cpuhp_cpu_state *st)
553 if (IS_ENABLED(CONFIG_HOTPLUG_CPU))
556 * When CPU hotplug is disabled, then taking the CPU down is not
557 * possible because takedown_cpu() and the architecture and
558 * subsystem specific mechanisms are not available. So the CPU
559 * which would be completely unplugged again needs to stay around
560 * in the current state.
562 return st->state <= CPUHP_BRINGUP_CPU;
565 static int cpuhp_up_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
566 enum cpuhp_state target)
568 enum cpuhp_state prev_state = st->state;
571 while (st->state < target) {
573 ret = cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
575 if (can_rollback_cpu(st)) {
576 st->target = prev_state;
577 undo_cpu_up(cpu, st);
586 * The cpu hotplug threads manage the bringup and teardown of the cpus
588 static void cpuhp_create(unsigned int cpu)
590 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
592 init_completion(&st->done_up);
593 init_completion(&st->done_down);
596 static int cpuhp_should_run(unsigned int cpu)
598 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
600 return st->should_run;
604 * Execute teardown/startup callbacks on the plugged cpu. Also used to invoke
605 * callbacks when a state gets [un]installed at runtime.
607 * Each invocation of this function by the smpboot thread does a single AP
610 * It has 3 modes of operation:
611 * - single: runs st->cb_state
612 * - up: runs ++st->state, while st->state < st->target
613 * - down: runs st->state--, while st->state > st->target
615 * When complete or on error, should_run is cleared and the completion is fired.
617 static void cpuhp_thread_fun(unsigned int cpu)
619 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
620 bool bringup = st->bringup;
621 enum cpuhp_state state;
623 if (WARN_ON_ONCE(!st->should_run))
627 * ACQUIRE for the cpuhp_should_run() load of ->should_run. Ensures
628 * that if we see ->should_run we also see the rest of the state.
632 cpuhp_lock_acquire(bringup);
635 state = st->cb_state;
636 st->should_run = false;
641 st->should_run = (st->state < st->target);
642 WARN_ON_ONCE(st->state > st->target);
646 st->should_run = (st->state > st->target);
647 WARN_ON_ONCE(st->state < st->target);
651 WARN_ON_ONCE(!cpuhp_is_ap_state(state));
654 struct cpuhp_step *step = cpuhp_get_step(state);
655 if (step->skip_onerr)
659 if (cpuhp_is_atomic_state(state)) {
661 st->result = cpuhp_invoke_callback(cpu, state, bringup, st->node, &st->last);
665 * STARTING/DYING must not fail!
667 WARN_ON_ONCE(st->result);
669 st->result = cpuhp_invoke_callback(cpu, state, bringup, st->node, &st->last);
674 * If we fail on a rollback, we're up a creek without no
675 * paddle, no way forward, no way back. We loose, thanks for
678 WARN_ON_ONCE(st->rollback);
679 st->should_run = false;
683 cpuhp_lock_release(bringup);
686 complete_ap_thread(st, bringup);
689 /* Invoke a single callback on a remote cpu */
691 cpuhp_invoke_ap_callback(int cpu, enum cpuhp_state state, bool bringup,
692 struct hlist_node *node)
694 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
697 if (!cpu_online(cpu))
700 cpuhp_lock_acquire(false);
701 cpuhp_lock_release(false);
703 cpuhp_lock_acquire(true);
704 cpuhp_lock_release(true);
707 * If we are up and running, use the hotplug thread. For early calls
708 * we invoke the thread function directly.
711 return cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
713 st->rollback = false;
717 st->bringup = bringup;
718 st->cb_state = state;
724 * If we failed and did a partial, do a rollback.
726 if ((ret = st->result) && st->last) {
728 st->bringup = !bringup;
734 * Clean up the leftovers so the next hotplug operation wont use stale
737 st->node = st->last = NULL;
741 static int cpuhp_kick_ap_work(unsigned int cpu)
743 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
744 enum cpuhp_state prev_state = st->state;
747 cpuhp_lock_acquire(false);
748 cpuhp_lock_release(false);
750 cpuhp_lock_acquire(true);
751 cpuhp_lock_release(true);
753 trace_cpuhp_enter(cpu, st->target, prev_state, cpuhp_kick_ap_work);
754 ret = cpuhp_kick_ap(st, st->target);
755 trace_cpuhp_exit(cpu, st->state, prev_state, ret);
760 static struct smp_hotplug_thread cpuhp_threads = {
761 .store = &cpuhp_state.thread,
762 .create = &cpuhp_create,
763 .thread_should_run = cpuhp_should_run,
764 .thread_fn = cpuhp_thread_fun,
765 .thread_comm = "cpuhp/%u",
769 void __init cpuhp_threads_init(void)
771 BUG_ON(smpboot_register_percpu_thread(&cpuhp_threads));
772 kthread_unpark(this_cpu_read(cpuhp_state.thread));
777 * Serialize hotplug trainwrecks outside of the cpu_hotplug_lock
780 * The operation is still serialized against concurrent CPU hotplug via
781 * cpu_add_remove_lock, i.e. CPU map protection. But it is _not_
782 * serialized against other hotplug related activity like adding or
783 * removing of state callbacks and state instances, which invoke either the
784 * startup or the teardown callback of the affected state.
786 * This is required for subsystems which are unfixable vs. CPU hotplug and
787 * evade lock inversion problems by scheduling work which has to be
788 * completed _before_ cpu_up()/_cpu_down() returns.
790 * Don't even think about adding anything to this for any new code or even
791 * drivers. It's only purpose is to keep existing lock order trainwrecks
794 * For cpu_down() there might be valid reasons to finish cleanups which are
795 * not required to be done under cpu_hotplug_lock, but that's a different
796 * story and would be not invoked via this.
798 static void cpu_up_down_serialize_trainwrecks(bool tasks_frozen)
801 * cpusets delegate hotplug operations to a worker to "solve" the
802 * lock order problems. Wait for the worker, but only if tasks are
803 * _not_ frozen (suspend, hibernate) as that would wait forever.
805 * The wait is required because otherwise the hotplug operation
806 * returns with inconsistent state, which could even be observed in
807 * user space when a new CPU is brought up. The CPU plug uevent
808 * would be delivered and user space reacting on it would fail to
809 * move tasks to the newly plugged CPU up to the point where the
810 * work has finished because up to that point the newly plugged CPU
811 * is not assignable in cpusets/cgroups. On unplug that's not
812 * necessarily a visible issue, but it is still inconsistent state,
813 * which is the real problem which needs to be "fixed". This can't
814 * prevent the transient state between scheduling the work and
815 * returning from waiting for it.
818 cpuset_wait_for_hotplug();
821 #ifdef CONFIG_HOTPLUG_CPU
822 #ifndef arch_clear_mm_cpumask_cpu
823 #define arch_clear_mm_cpumask_cpu(cpu, mm) cpumask_clear_cpu(cpu, mm_cpumask(mm))
827 * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU
830 * This function walks all processes, finds a valid mm struct for each one and
831 * then clears a corresponding bit in mm's cpumask. While this all sounds
832 * trivial, there are various non-obvious corner cases, which this function
833 * tries to solve in a safe manner.
835 * Also note that the function uses a somewhat relaxed locking scheme, so it may
836 * be called only for an already offlined CPU.
838 void clear_tasks_mm_cpumask(int cpu)
840 struct task_struct *p;
843 * This function is called after the cpu is taken down and marked
844 * offline, so its not like new tasks will ever get this cpu set in
845 * their mm mask. -- Peter Zijlstra
846 * Thus, we may use rcu_read_lock() here, instead of grabbing
847 * full-fledged tasklist_lock.
849 WARN_ON(cpu_online(cpu));
851 for_each_process(p) {
852 struct task_struct *t;
855 * Main thread might exit, but other threads may still have
856 * a valid mm. Find one.
858 t = find_lock_task_mm(p);
861 arch_clear_mm_cpumask_cpu(cpu, t->mm);
867 /* Take this CPU down. */
868 static int take_cpu_down(void *_param)
870 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
871 enum cpuhp_state target = max((int)st->target, CPUHP_AP_OFFLINE);
872 int err, cpu = smp_processor_id();
875 /* Ensure this CPU doesn't handle any more interrupts. */
876 err = __cpu_disable();
881 * We get here while we are in CPUHP_TEARDOWN_CPU state and we must not
882 * do this step again.
884 WARN_ON(st->state != CPUHP_TEARDOWN_CPU);
886 /* Invoke the former CPU_DYING callbacks */
887 for (; st->state > target; st->state--) {
888 ret = cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
890 * DYING must not fail!
895 /* Give up timekeeping duties */
896 tick_handover_do_timer();
897 /* Park the stopper thread */
898 stop_machine_park(cpu);
902 static int takedown_cpu(unsigned int cpu)
904 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
907 /* Park the smpboot threads */
908 kthread_park(per_cpu_ptr(&cpuhp_state, cpu)->thread);
911 * Prevent irq alloc/free while the dying cpu reorganizes the
912 * interrupt affinities.
917 * So now all preempt/rcu users must observe !cpu_active().
919 err = stop_machine_cpuslocked(take_cpu_down, NULL, cpumask_of(cpu));
921 /* CPU refused to die */
923 /* Unpark the hotplug thread so we can rollback there */
924 kthread_unpark(per_cpu_ptr(&cpuhp_state, cpu)->thread);
927 BUG_ON(cpu_online(cpu));
930 * The CPUHP_AP_SCHED_MIGRATE_DYING callback will have removed all
931 * runnable tasks from the cpu, there's only the idle task left now
932 * that the migration thread is done doing the stop_machine thing.
934 * Wait for the stop thread to go away.
936 wait_for_ap_thread(st, false);
937 BUG_ON(st->state != CPUHP_AP_IDLE_DEAD);
939 /* Interrupts are moved away from the dying cpu, reenable alloc/free */
942 hotplug_cpu__broadcast_tick_pull(cpu);
943 /* This actually kills the CPU. */
946 tick_cleanup_dead_cpu(cpu);
947 rcutree_migrate_callbacks(cpu);
951 static void cpuhp_complete_idle_dead(void *arg)
953 struct cpuhp_cpu_state *st = arg;
955 complete_ap_thread(st, false);
958 void cpuhp_report_idle_dead(void)
960 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
962 BUG_ON(st->state != CPUHP_AP_OFFLINE);
963 rcu_report_dead(smp_processor_id());
964 st->state = CPUHP_AP_IDLE_DEAD;
966 * We cannot call complete after rcu_report_dead() so we delegate it
969 smp_call_function_single(cpumask_first(cpu_online_mask),
970 cpuhp_complete_idle_dead, st, 0);
973 static void undo_cpu_down(unsigned int cpu, struct cpuhp_cpu_state *st)
975 for (st->state++; st->state < st->target; st->state++) {
976 struct cpuhp_step *step = cpuhp_get_step(st->state);
978 if (!step->skip_onerr)
979 cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
983 static int cpuhp_down_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
984 enum cpuhp_state target)
986 enum cpuhp_state prev_state = st->state;
989 for (; st->state > target; st->state--) {
990 ret = cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
992 st->target = prev_state;
993 if (st->state < prev_state)
994 undo_cpu_down(cpu, st);
1001 /* Requires cpu_add_remove_lock to be held */
1002 static int __ref _cpu_down(unsigned int cpu, int tasks_frozen,
1003 enum cpuhp_state target)
1005 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1006 int prev_state, ret = 0;
1008 if (num_online_cpus() == 1)
1011 if (!cpu_present(cpu))
1016 cpuhp_tasks_frozen = tasks_frozen;
1018 prev_state = cpuhp_set_state(st, target);
1020 * If the current CPU state is in the range of the AP hotplug thread,
1021 * then we need to kick the thread.
1023 if (st->state > CPUHP_TEARDOWN_CPU) {
1024 st->target = max((int)target, CPUHP_TEARDOWN_CPU);
1025 ret = cpuhp_kick_ap_work(cpu);
1027 * The AP side has done the error rollback already. Just
1028 * return the error code..
1034 * We might have stopped still in the range of the AP hotplug
1035 * thread. Nothing to do anymore.
1037 if (st->state > CPUHP_TEARDOWN_CPU)
1040 st->target = target;
1043 * The AP brought itself down to CPUHP_TEARDOWN_CPU. So we need
1044 * to do the further cleanups.
1046 ret = cpuhp_down_callbacks(cpu, st, target);
1047 if (ret && st->state == CPUHP_TEARDOWN_CPU && st->state < prev_state) {
1048 cpuhp_reset_state(st, prev_state);
1049 __cpuhp_kick_ap(st);
1053 cpus_write_unlock();
1055 * Do post unplug cleanup. This is still protected against
1056 * concurrent CPU hotplug via cpu_add_remove_lock.
1058 lockup_detector_cleanup();
1060 cpu_up_down_serialize_trainwrecks(tasks_frozen);
1064 static int cpu_down_maps_locked(unsigned int cpu, enum cpuhp_state target)
1066 if (cpu_hotplug_disabled)
1068 return _cpu_down(cpu, 0, target);
1071 static int do_cpu_down(unsigned int cpu, enum cpuhp_state target)
1075 cpu_maps_update_begin();
1076 err = cpu_down_maps_locked(cpu, target);
1077 cpu_maps_update_done();
1081 int cpu_down(unsigned int cpu)
1083 return do_cpu_down(cpu, CPUHP_OFFLINE);
1085 EXPORT_SYMBOL(cpu_down);
1088 #define takedown_cpu NULL
1089 #endif /*CONFIG_HOTPLUG_CPU*/
1092 * notify_cpu_starting(cpu) - Invoke the callbacks on the starting CPU
1093 * @cpu: cpu that just started
1095 * It must be called by the arch code on the new cpu, before the new cpu
1096 * enables interrupts and before the "boot" cpu returns from __cpu_up().
1098 void notify_cpu_starting(unsigned int cpu)
1100 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1101 enum cpuhp_state target = min((int)st->target, CPUHP_AP_ONLINE);
1104 rcu_cpu_starting(cpu); /* Enables RCU usage on this CPU. */
1105 st->booted_once = true;
1106 while (st->state < target) {
1108 ret = cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
1110 * STARTING must not fail!
1117 * Called from the idle task. Wake up the controlling task which brings the
1118 * hotplug thread of the upcoming CPU up and then delegates the rest of the
1119 * online bringup to the hotplug thread.
1121 void cpuhp_online_idle(enum cpuhp_state state)
1123 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
1125 /* Happens for the boot cpu */
1126 if (state != CPUHP_AP_ONLINE_IDLE)
1130 * Unpart the stopper thread before we start the idle loop (and start
1131 * scheduling); this ensures the stopper task is always available.
1133 stop_machine_unpark(smp_processor_id());
1135 st->state = CPUHP_AP_ONLINE_IDLE;
1136 complete_ap_thread(st, true);
1139 /* Requires cpu_add_remove_lock to be held */
1140 static int _cpu_up(unsigned int cpu, int tasks_frozen, enum cpuhp_state target)
1142 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1143 struct task_struct *idle;
1148 if (!cpu_present(cpu)) {
1154 * The caller of do_cpu_up might have raced with another
1155 * caller. Ignore it for now.
1157 if (st->state >= target)
1160 if (st->state == CPUHP_OFFLINE) {
1161 /* Let it fail before we try to bring the cpu up */
1162 idle = idle_thread_get(cpu);
1164 ret = PTR_ERR(idle);
1169 cpuhp_tasks_frozen = tasks_frozen;
1171 cpuhp_set_state(st, target);
1173 * If the current CPU state is in the range of the AP hotplug thread,
1174 * then we need to kick the thread once more.
1176 if (st->state > CPUHP_BRINGUP_CPU) {
1177 ret = cpuhp_kick_ap_work(cpu);
1179 * The AP side has done the error rollback already. Just
1180 * return the error code..
1187 * Try to reach the target state. We max out on the BP at
1188 * CPUHP_BRINGUP_CPU. After that the AP hotplug thread is
1189 * responsible for bringing it up to the target state.
1191 target = min((int)target, CPUHP_BRINGUP_CPU);
1192 ret = cpuhp_up_callbacks(cpu, st, target);
1194 cpus_write_unlock();
1196 cpu_up_down_serialize_trainwrecks(tasks_frozen);
1200 static int do_cpu_up(unsigned int cpu, enum cpuhp_state target)
1204 if (!cpu_possible(cpu)) {
1205 pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n",
1207 #if defined(CONFIG_IA64)
1208 pr_err("please check additional_cpus= boot parameter\n");
1213 err = try_online_node(cpu_to_node(cpu));
1217 cpu_maps_update_begin();
1219 if (cpu_hotplug_disabled) {
1223 if (!cpu_smt_allowed(cpu)) {
1228 err = _cpu_up(cpu, 0, target);
1230 cpu_maps_update_done();
1234 int cpu_up(unsigned int cpu)
1236 return do_cpu_up(cpu, CPUHP_ONLINE);
1238 EXPORT_SYMBOL_GPL(cpu_up);
1240 #ifdef CONFIG_PM_SLEEP_SMP
1241 static cpumask_var_t frozen_cpus;
1243 int freeze_secondary_cpus(int primary)
1247 cpu_maps_update_begin();
1248 if (!cpu_online(primary))
1249 primary = cpumask_first(cpu_online_mask);
1251 * We take down all of the non-boot CPUs in one shot to avoid races
1252 * with the userspace trying to use the CPU hotplug at the same time
1254 cpumask_clear(frozen_cpus);
1256 pr_info("Disabling non-boot CPUs ...\n");
1257 for_each_online_cpu(cpu) {
1260 trace_suspend_resume(TPS("CPU_OFF"), cpu, true);
1261 error = _cpu_down(cpu, 1, CPUHP_OFFLINE);
1262 trace_suspend_resume(TPS("CPU_OFF"), cpu, false);
1264 cpumask_set_cpu(cpu, frozen_cpus);
1266 pr_err("Error taking CPU%d down: %d\n", cpu, error);
1272 BUG_ON(num_online_cpus() > 1);
1274 pr_err("Non-boot CPUs are not disabled\n");
1277 * Make sure the CPUs won't be enabled by someone else. We need to do
1278 * this even in case of failure as all disable_nonboot_cpus() users are
1279 * supposed to do enable_nonboot_cpus() on the failure path.
1281 cpu_hotplug_disabled++;
1283 cpu_maps_update_done();
1287 void __weak arch_enable_nonboot_cpus_begin(void)
1291 void __weak arch_enable_nonboot_cpus_end(void)
1295 void enable_nonboot_cpus(void)
1299 /* Allow everyone to use the CPU hotplug again */
1300 cpu_maps_update_begin();
1301 __cpu_hotplug_enable();
1302 if (cpumask_empty(frozen_cpus))
1305 pr_info("Enabling non-boot CPUs ...\n");
1307 arch_enable_nonboot_cpus_begin();
1309 for_each_cpu(cpu, frozen_cpus) {
1310 trace_suspend_resume(TPS("CPU_ON"), cpu, true);
1311 error = _cpu_up(cpu, 1, CPUHP_ONLINE);
1312 trace_suspend_resume(TPS("CPU_ON"), cpu, false);
1314 pr_info("CPU%d is up\n", cpu);
1317 pr_warn("Error taking CPU%d up: %d\n", cpu, error);
1320 arch_enable_nonboot_cpus_end();
1322 cpumask_clear(frozen_cpus);
1324 cpu_maps_update_done();
1327 static int __init alloc_frozen_cpus(void)
1329 if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
1333 core_initcall(alloc_frozen_cpus);
1336 * When callbacks for CPU hotplug notifications are being executed, we must
1337 * ensure that the state of the system with respect to the tasks being frozen
1338 * or not, as reported by the notification, remains unchanged *throughout the
1339 * duration* of the execution of the callbacks.
1340 * Hence we need to prevent the freezer from racing with regular CPU hotplug.
1342 * This synchronization is implemented by mutually excluding regular CPU
1343 * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
1344 * Hibernate notifications.
1347 cpu_hotplug_pm_callback(struct notifier_block *nb,
1348 unsigned long action, void *ptr)
1352 case PM_SUSPEND_PREPARE:
1353 case PM_HIBERNATION_PREPARE:
1354 cpu_hotplug_disable();
1357 case PM_POST_SUSPEND:
1358 case PM_POST_HIBERNATION:
1359 cpu_hotplug_enable();
1370 static int __init cpu_hotplug_pm_sync_init(void)
1373 * cpu_hotplug_pm_callback has higher priority than x86
1374 * bsp_pm_callback which depends on cpu_hotplug_pm_callback
1375 * to disable cpu hotplug to avoid cpu hotplug race.
1377 pm_notifier(cpu_hotplug_pm_callback, 0);
1380 core_initcall(cpu_hotplug_pm_sync_init);
1382 #endif /* CONFIG_PM_SLEEP_SMP */
1386 #endif /* CONFIG_SMP */
1388 /* Boot processor state steps */
1389 static struct cpuhp_step cpuhp_bp_states[] = {
1392 .startup.single = NULL,
1393 .teardown.single = NULL,
1396 [CPUHP_CREATE_THREADS]= {
1397 .name = "threads:prepare",
1398 .startup.single = smpboot_create_threads,
1399 .teardown.single = NULL,
1402 [CPUHP_PERF_PREPARE] = {
1403 .name = "perf:prepare",
1404 .startup.single = perf_event_init_cpu,
1405 .teardown.single = perf_event_exit_cpu,
1407 [CPUHP_WORKQUEUE_PREP] = {
1408 .name = "workqueue:prepare",
1409 .startup.single = workqueue_prepare_cpu,
1410 .teardown.single = NULL,
1412 [CPUHP_HRTIMERS_PREPARE] = {
1413 .name = "hrtimers:prepare",
1414 .startup.single = hrtimers_prepare_cpu,
1415 .teardown.single = hrtimers_dead_cpu,
1417 [CPUHP_SMPCFD_PREPARE] = {
1418 .name = "smpcfd:prepare",
1419 .startup.single = smpcfd_prepare_cpu,
1420 .teardown.single = smpcfd_dead_cpu,
1422 [CPUHP_RELAY_PREPARE] = {
1423 .name = "relay:prepare",
1424 .startup.single = relay_prepare_cpu,
1425 .teardown.single = NULL,
1427 [CPUHP_SLAB_PREPARE] = {
1428 .name = "slab:prepare",
1429 .startup.single = slab_prepare_cpu,
1430 .teardown.single = slab_dead_cpu,
1432 [CPUHP_RCUTREE_PREP] = {
1433 .name = "RCU/tree:prepare",
1434 .startup.single = rcutree_prepare_cpu,
1435 .teardown.single = rcutree_dead_cpu,
1438 * On the tear-down path, timers_dead_cpu() must be invoked
1439 * before blk_mq_queue_reinit_notify() from notify_dead(),
1440 * otherwise a RCU stall occurs.
1442 [CPUHP_TIMERS_PREPARE] = {
1443 .name = "timers:dead",
1444 .startup.single = timers_prepare_cpu,
1445 .teardown.single = timers_dead_cpu,
1447 /* Kicks the plugged cpu into life */
1448 [CPUHP_BRINGUP_CPU] = {
1449 .name = "cpu:bringup",
1450 .startup.single = bringup_cpu,
1451 .teardown.single = NULL,
1455 * Handled on controll processor until the plugged processor manages
1458 [CPUHP_TEARDOWN_CPU] = {
1459 .name = "cpu:teardown",
1460 .startup.single = NULL,
1461 .teardown.single = takedown_cpu,
1465 [CPUHP_BRINGUP_CPU] = { },
1469 /* Application processor state steps */
1470 static struct cpuhp_step cpuhp_ap_states[] = {
1472 /* Final state before CPU kills itself */
1473 [CPUHP_AP_IDLE_DEAD] = {
1474 .name = "idle:dead",
1477 * Last state before CPU enters the idle loop to die. Transient state
1478 * for synchronization.
1480 [CPUHP_AP_OFFLINE] = {
1481 .name = "ap:offline",
1484 /* First state is scheduler control. Interrupts are disabled */
1485 [CPUHP_AP_SCHED_STARTING] = {
1486 .name = "sched:starting",
1487 .startup.single = sched_cpu_starting,
1488 .teardown.single = sched_cpu_dying,
1490 [CPUHP_AP_RCUTREE_DYING] = {
1491 .name = "RCU/tree:dying",
1492 .startup.single = NULL,
1493 .teardown.single = rcutree_dying_cpu,
1495 [CPUHP_AP_SMPCFD_DYING] = {
1496 .name = "smpcfd:dying",
1497 .startup.single = NULL,
1498 .teardown.single = smpcfd_dying_cpu,
1500 /* Entry state on starting. Interrupts enabled from here on. Transient
1501 * state for synchronsization */
1502 [CPUHP_AP_ONLINE] = {
1503 .name = "ap:online",
1505 /* Handle smpboot threads park/unpark */
1506 [CPUHP_AP_SMPBOOT_THREADS] = {
1507 .name = "smpboot/threads:online",
1508 .startup.single = smpboot_unpark_threads,
1509 .teardown.single = smpboot_park_threads,
1511 [CPUHP_AP_IRQ_AFFINITY_ONLINE] = {
1512 .name = "irq/affinity:online",
1513 .startup.single = irq_affinity_online_cpu,
1514 .teardown.single = NULL,
1516 [CPUHP_AP_PERF_ONLINE] = {
1517 .name = "perf:online",
1518 .startup.single = perf_event_init_cpu,
1519 .teardown.single = perf_event_exit_cpu,
1521 [CPUHP_AP_WORKQUEUE_ONLINE] = {
1522 .name = "workqueue:online",
1523 .startup.single = workqueue_online_cpu,
1524 .teardown.single = workqueue_offline_cpu,
1526 [CPUHP_AP_RCUTREE_ONLINE] = {
1527 .name = "RCU/tree:online",
1528 .startup.single = rcutree_online_cpu,
1529 .teardown.single = rcutree_offline_cpu,
1533 * The dynamically registered state space is here
1537 /* Last state is scheduler control setting the cpu active */
1538 [CPUHP_AP_ACTIVE] = {
1539 .name = "sched:active",
1540 .startup.single = sched_cpu_activate,
1541 .teardown.single = sched_cpu_deactivate,
1545 /* CPU is fully up and running. */
1548 .startup.single = NULL,
1549 .teardown.single = NULL,
1553 /* Sanity check for callbacks */
1554 static int cpuhp_cb_check(enum cpuhp_state state)
1556 if (state <= CPUHP_OFFLINE || state >= CPUHP_ONLINE)
1562 * Returns a free for dynamic slot assignment of the Online state. The states
1563 * are protected by the cpuhp_slot_states mutex and an empty slot is identified
1564 * by having no name assigned.
1566 static int cpuhp_reserve_state(enum cpuhp_state state)
1568 enum cpuhp_state i, end;
1569 struct cpuhp_step *step;
1572 case CPUHP_AP_ONLINE_DYN:
1573 step = cpuhp_ap_states + CPUHP_AP_ONLINE_DYN;
1574 end = CPUHP_AP_ONLINE_DYN_END;
1576 case CPUHP_BP_PREPARE_DYN:
1577 step = cpuhp_bp_states + CPUHP_BP_PREPARE_DYN;
1578 end = CPUHP_BP_PREPARE_DYN_END;
1584 for (i = state; i <= end; i++, step++) {
1588 WARN(1, "No more dynamic states available for CPU hotplug\n");
1592 static int cpuhp_store_callbacks(enum cpuhp_state state, const char *name,
1593 int (*startup)(unsigned int cpu),
1594 int (*teardown)(unsigned int cpu),
1595 bool multi_instance)
1597 /* (Un)Install the callbacks for further cpu hotplug operations */
1598 struct cpuhp_step *sp;
1602 * If name is NULL, then the state gets removed.
1604 * CPUHP_AP_ONLINE_DYN and CPUHP_BP_PREPARE_DYN are handed out on
1605 * the first allocation from these dynamic ranges, so the removal
1606 * would trigger a new allocation and clear the wrong (already
1607 * empty) state, leaving the callbacks of the to be cleared state
1608 * dangling, which causes wreckage on the next hotplug operation.
1610 if (name && (state == CPUHP_AP_ONLINE_DYN ||
1611 state == CPUHP_BP_PREPARE_DYN)) {
1612 ret = cpuhp_reserve_state(state);
1617 sp = cpuhp_get_step(state);
1618 if (name && sp->name)
1621 sp->startup.single = startup;
1622 sp->teardown.single = teardown;
1624 sp->multi_instance = multi_instance;
1625 INIT_HLIST_HEAD(&sp->list);
1629 static void *cpuhp_get_teardown_cb(enum cpuhp_state state)
1631 return cpuhp_get_step(state)->teardown.single;
1635 * Call the startup/teardown function for a step either on the AP or
1636 * on the current CPU.
1638 static int cpuhp_issue_call(int cpu, enum cpuhp_state state, bool bringup,
1639 struct hlist_node *node)
1641 struct cpuhp_step *sp = cpuhp_get_step(state);
1645 * If there's nothing to do, we done.
1646 * Relies on the union for multi_instance.
1648 if ((bringup && !sp->startup.single) ||
1649 (!bringup && !sp->teardown.single))
1652 * The non AP bound callbacks can fail on bringup. On teardown
1653 * e.g. module removal we crash for now.
1656 if (cpuhp_is_ap_state(state))
1657 ret = cpuhp_invoke_ap_callback(cpu, state, bringup, node);
1659 ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
1661 ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
1663 BUG_ON(ret && !bringup);
1668 * Called from __cpuhp_setup_state on a recoverable failure.
1670 * Note: The teardown callbacks for rollback are not allowed to fail!
1672 static void cpuhp_rollback_install(int failedcpu, enum cpuhp_state state,
1673 struct hlist_node *node)
1677 /* Roll back the already executed steps on the other cpus */
1678 for_each_present_cpu(cpu) {
1679 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1680 int cpustate = st->state;
1682 if (cpu >= failedcpu)
1685 /* Did we invoke the startup call on that cpu ? */
1686 if (cpustate >= state)
1687 cpuhp_issue_call(cpu, state, false, node);
1691 int __cpuhp_state_add_instance_cpuslocked(enum cpuhp_state state,
1692 struct hlist_node *node,
1695 struct cpuhp_step *sp;
1699 lockdep_assert_cpus_held();
1701 sp = cpuhp_get_step(state);
1702 if (sp->multi_instance == false)
1705 mutex_lock(&cpuhp_state_mutex);
1707 if (!invoke || !sp->startup.multi)
1711 * Try to call the startup callback for each present cpu
1712 * depending on the hotplug state of the cpu.
1714 for_each_present_cpu(cpu) {
1715 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1716 int cpustate = st->state;
1718 if (cpustate < state)
1721 ret = cpuhp_issue_call(cpu, state, true, node);
1723 if (sp->teardown.multi)
1724 cpuhp_rollback_install(cpu, state, node);
1730 hlist_add_head(node, &sp->list);
1732 mutex_unlock(&cpuhp_state_mutex);
1736 int __cpuhp_state_add_instance(enum cpuhp_state state, struct hlist_node *node,
1742 ret = __cpuhp_state_add_instance_cpuslocked(state, node, invoke);
1746 EXPORT_SYMBOL_GPL(__cpuhp_state_add_instance);
1749 * __cpuhp_setup_state_cpuslocked - Setup the callbacks for an hotplug machine state
1750 * @state: The state to setup
1751 * @invoke: If true, the startup function is invoked for cpus where
1752 * cpu state >= @state
1753 * @startup: startup callback function
1754 * @teardown: teardown callback function
1755 * @multi_instance: State is set up for multiple instances which get
1758 * The caller needs to hold cpus read locked while calling this function.
1761 * Positive state number if @state is CPUHP_AP_ONLINE_DYN
1762 * 0 for all other states
1763 * On failure: proper (negative) error code
1765 int __cpuhp_setup_state_cpuslocked(enum cpuhp_state state,
1766 const char *name, bool invoke,
1767 int (*startup)(unsigned int cpu),
1768 int (*teardown)(unsigned int cpu),
1769 bool multi_instance)
1774 lockdep_assert_cpus_held();
1776 if (cpuhp_cb_check(state) || !name)
1779 mutex_lock(&cpuhp_state_mutex);
1781 ret = cpuhp_store_callbacks(state, name, startup, teardown,
1784 dynstate = state == CPUHP_AP_ONLINE_DYN;
1785 if (ret > 0 && dynstate) {
1790 if (ret || !invoke || !startup)
1794 * Try to call the startup callback for each present cpu
1795 * depending on the hotplug state of the cpu.
1797 for_each_present_cpu(cpu) {
1798 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1799 int cpustate = st->state;
1801 if (cpustate < state)
1804 ret = cpuhp_issue_call(cpu, state, true, NULL);
1807 cpuhp_rollback_install(cpu, state, NULL);
1808 cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
1813 mutex_unlock(&cpuhp_state_mutex);
1815 * If the requested state is CPUHP_AP_ONLINE_DYN, return the
1816 * dynamically allocated state in case of success.
1818 if (!ret && dynstate)
1822 EXPORT_SYMBOL(__cpuhp_setup_state_cpuslocked);
1824 int __cpuhp_setup_state(enum cpuhp_state state,
1825 const char *name, bool invoke,
1826 int (*startup)(unsigned int cpu),
1827 int (*teardown)(unsigned int cpu),
1828 bool multi_instance)
1833 ret = __cpuhp_setup_state_cpuslocked(state, name, invoke, startup,
1834 teardown, multi_instance);
1838 EXPORT_SYMBOL(__cpuhp_setup_state);
1840 int __cpuhp_state_remove_instance(enum cpuhp_state state,
1841 struct hlist_node *node, bool invoke)
1843 struct cpuhp_step *sp = cpuhp_get_step(state);
1846 BUG_ON(cpuhp_cb_check(state));
1848 if (!sp->multi_instance)
1852 mutex_lock(&cpuhp_state_mutex);
1854 if (!invoke || !cpuhp_get_teardown_cb(state))
1857 * Call the teardown callback for each present cpu depending
1858 * on the hotplug state of the cpu. This function is not
1859 * allowed to fail currently!
1861 for_each_present_cpu(cpu) {
1862 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1863 int cpustate = st->state;
1865 if (cpustate >= state)
1866 cpuhp_issue_call(cpu, state, false, node);
1871 mutex_unlock(&cpuhp_state_mutex);
1876 EXPORT_SYMBOL_GPL(__cpuhp_state_remove_instance);
1879 * __cpuhp_remove_state_cpuslocked - Remove the callbacks for an hotplug machine state
1880 * @state: The state to remove
1881 * @invoke: If true, the teardown function is invoked for cpus where
1882 * cpu state >= @state
1884 * The caller needs to hold cpus read locked while calling this function.
1885 * The teardown callback is currently not allowed to fail. Think
1886 * about module removal!
1888 void __cpuhp_remove_state_cpuslocked(enum cpuhp_state state, bool invoke)
1890 struct cpuhp_step *sp = cpuhp_get_step(state);
1893 BUG_ON(cpuhp_cb_check(state));
1895 lockdep_assert_cpus_held();
1897 mutex_lock(&cpuhp_state_mutex);
1898 if (sp->multi_instance) {
1899 WARN(!hlist_empty(&sp->list),
1900 "Error: Removing state %d which has instances left.\n",
1905 if (!invoke || !cpuhp_get_teardown_cb(state))
1909 * Call the teardown callback for each present cpu depending
1910 * on the hotplug state of the cpu. This function is not
1911 * allowed to fail currently!
1913 for_each_present_cpu(cpu) {
1914 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1915 int cpustate = st->state;
1917 if (cpustate >= state)
1918 cpuhp_issue_call(cpu, state, false, NULL);
1921 cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
1922 mutex_unlock(&cpuhp_state_mutex);
1924 EXPORT_SYMBOL(__cpuhp_remove_state_cpuslocked);
1926 void __cpuhp_remove_state(enum cpuhp_state state, bool invoke)
1929 __cpuhp_remove_state_cpuslocked(state, invoke);
1932 EXPORT_SYMBOL(__cpuhp_remove_state);
1934 #if defined(CONFIG_SYSFS) && defined(CONFIG_HOTPLUG_CPU)
1935 static ssize_t show_cpuhp_state(struct device *dev,
1936 struct device_attribute *attr, char *buf)
1938 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
1940 return sprintf(buf, "%d\n", st->state);
1942 static DEVICE_ATTR(state, 0444, show_cpuhp_state, NULL);
1944 static ssize_t write_cpuhp_target(struct device *dev,
1945 struct device_attribute *attr,
1946 const char *buf, size_t count)
1948 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
1949 struct cpuhp_step *sp;
1952 ret = kstrtoint(buf, 10, &target);
1956 #ifdef CONFIG_CPU_HOTPLUG_STATE_CONTROL
1957 if (target < CPUHP_OFFLINE || target > CPUHP_ONLINE)
1960 if (target != CPUHP_OFFLINE && target != CPUHP_ONLINE)
1964 ret = lock_device_hotplug_sysfs();
1968 mutex_lock(&cpuhp_state_mutex);
1969 sp = cpuhp_get_step(target);
1970 ret = !sp->name || sp->cant_stop ? -EINVAL : 0;
1971 mutex_unlock(&cpuhp_state_mutex);
1975 if (st->state < target)
1976 ret = do_cpu_up(dev->id, target);
1978 ret = do_cpu_down(dev->id, target);
1980 unlock_device_hotplug();
1981 return ret ? ret : count;
1984 static ssize_t show_cpuhp_target(struct device *dev,
1985 struct device_attribute *attr, char *buf)
1987 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
1989 return sprintf(buf, "%d\n", st->target);
1991 static DEVICE_ATTR(target, 0644, show_cpuhp_target, write_cpuhp_target);
1994 static ssize_t write_cpuhp_fail(struct device *dev,
1995 struct device_attribute *attr,
1996 const char *buf, size_t count)
1998 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
1999 struct cpuhp_step *sp;
2002 ret = kstrtoint(buf, 10, &fail);
2006 if (fail < CPUHP_OFFLINE || fail > CPUHP_ONLINE)
2010 * Cannot fail STARTING/DYING callbacks.
2012 if (cpuhp_is_atomic_state(fail))
2016 * Cannot fail anything that doesn't have callbacks.
2018 mutex_lock(&cpuhp_state_mutex);
2019 sp = cpuhp_get_step(fail);
2020 if (!sp->startup.single && !sp->teardown.single)
2022 mutex_unlock(&cpuhp_state_mutex);
2031 static ssize_t show_cpuhp_fail(struct device *dev,
2032 struct device_attribute *attr, char *buf)
2034 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
2036 return sprintf(buf, "%d\n", st->fail);
2039 static DEVICE_ATTR(fail, 0644, show_cpuhp_fail, write_cpuhp_fail);
2041 static struct attribute *cpuhp_cpu_attrs[] = {
2042 &dev_attr_state.attr,
2043 &dev_attr_target.attr,
2044 &dev_attr_fail.attr,
2048 static const struct attribute_group cpuhp_cpu_attr_group = {
2049 .attrs = cpuhp_cpu_attrs,
2054 static ssize_t show_cpuhp_states(struct device *dev,
2055 struct device_attribute *attr, char *buf)
2057 ssize_t cur, res = 0;
2060 mutex_lock(&cpuhp_state_mutex);
2061 for (i = CPUHP_OFFLINE; i <= CPUHP_ONLINE; i++) {
2062 struct cpuhp_step *sp = cpuhp_get_step(i);
2065 cur = sprintf(buf, "%3d: %s\n", i, sp->name);
2070 mutex_unlock(&cpuhp_state_mutex);
2073 static DEVICE_ATTR(states, 0444, show_cpuhp_states, NULL);
2075 static struct attribute *cpuhp_cpu_root_attrs[] = {
2076 &dev_attr_states.attr,
2080 static const struct attribute_group cpuhp_cpu_root_attr_group = {
2081 .attrs = cpuhp_cpu_root_attrs,
2086 #ifdef CONFIG_HOTPLUG_SMT
2088 static const char *smt_states[] = {
2089 [CPU_SMT_ENABLED] = "on",
2090 [CPU_SMT_DISABLED] = "off",
2091 [CPU_SMT_FORCE_DISABLED] = "forceoff",
2092 [CPU_SMT_NOT_SUPPORTED] = "notsupported",
2096 show_smt_control(struct device *dev, struct device_attribute *attr, char *buf)
2098 return snprintf(buf, PAGE_SIZE - 2, "%s\n", smt_states[cpu_smt_control]);
2101 static void cpuhp_offline_cpu_device(unsigned int cpu)
2103 struct device *dev = get_cpu_device(cpu);
2105 dev->offline = true;
2106 /* Tell user space about the state change */
2107 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2110 static void cpuhp_online_cpu_device(unsigned int cpu)
2112 struct device *dev = get_cpu_device(cpu);
2114 dev->offline = false;
2115 /* Tell user space about the state change */
2116 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2119 int cpuhp_smt_disable(enum cpuhp_smt_control ctrlval)
2123 cpu_maps_update_begin();
2124 for_each_online_cpu(cpu) {
2125 if (topology_is_primary_thread(cpu))
2127 ret = cpu_down_maps_locked(cpu, CPUHP_OFFLINE);
2131 * As this needs to hold the cpu maps lock it's impossible
2132 * to call device_offline() because that ends up calling
2133 * cpu_down() which takes cpu maps lock. cpu maps lock
2134 * needs to be held as this might race against in kernel
2135 * abusers of the hotplug machinery (thermal management).
2137 * So nothing would update device:offline state. That would
2138 * leave the sysfs entry stale and prevent onlining after
2139 * smt control has been changed to 'off' again. This is
2140 * called under the sysfs hotplug lock, so it is properly
2141 * serialized against the regular offline usage.
2143 cpuhp_offline_cpu_device(cpu);
2146 cpu_smt_control = ctrlval;
2147 cpu_maps_update_done();
2151 int cpuhp_smt_enable(void)
2155 cpu_maps_update_begin();
2156 cpu_smt_control = CPU_SMT_ENABLED;
2157 for_each_present_cpu(cpu) {
2158 /* Skip online CPUs and CPUs on offline nodes */
2159 if (cpu_online(cpu) || !node_online(cpu_to_node(cpu)))
2161 ret = _cpu_up(cpu, 0, CPUHP_ONLINE);
2164 /* See comment in cpuhp_smt_disable() */
2165 cpuhp_online_cpu_device(cpu);
2167 cpu_maps_update_done();
2172 store_smt_control(struct device *dev, struct device_attribute *attr,
2173 const char *buf, size_t count)
2177 if (sysfs_streq(buf, "on"))
2178 ctrlval = CPU_SMT_ENABLED;
2179 else if (sysfs_streq(buf, "off"))
2180 ctrlval = CPU_SMT_DISABLED;
2181 else if (sysfs_streq(buf, "forceoff"))
2182 ctrlval = CPU_SMT_FORCE_DISABLED;
2186 if (cpu_smt_control == CPU_SMT_FORCE_DISABLED)
2189 if (cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
2192 ret = lock_device_hotplug_sysfs();
2196 if (ctrlval != cpu_smt_control) {
2198 case CPU_SMT_ENABLED:
2199 ret = cpuhp_smt_enable();
2201 case CPU_SMT_DISABLED:
2202 case CPU_SMT_FORCE_DISABLED:
2203 ret = cpuhp_smt_disable(ctrlval);
2208 unlock_device_hotplug();
2209 return ret ? ret : count;
2211 static DEVICE_ATTR(control, 0644, show_smt_control, store_smt_control);
2214 show_smt_active(struct device *dev, struct device_attribute *attr, char *buf)
2216 bool active = topology_max_smt_threads() > 1;
2218 return snprintf(buf, PAGE_SIZE - 2, "%d\n", active);
2220 static DEVICE_ATTR(active, 0444, show_smt_active, NULL);
2222 static struct attribute *cpuhp_smt_attrs[] = {
2223 &dev_attr_control.attr,
2224 &dev_attr_active.attr,
2228 static const struct attribute_group cpuhp_smt_attr_group = {
2229 .attrs = cpuhp_smt_attrs,
2234 static int __init cpu_smt_state_init(void)
2236 return sysfs_create_group(&cpu_subsys.dev_root->kobj,
2237 &cpuhp_smt_attr_group);
2241 static inline int cpu_smt_state_init(void) { return 0; }
2244 static int __init cpuhp_sysfs_init(void)
2248 ret = cpu_smt_state_init();
2252 ret = sysfs_create_group(&cpu_subsys.dev_root->kobj,
2253 &cpuhp_cpu_root_attr_group);
2257 for_each_possible_cpu(cpu) {
2258 struct device *dev = get_cpu_device(cpu);
2262 ret = sysfs_create_group(&dev->kobj, &cpuhp_cpu_attr_group);
2268 device_initcall(cpuhp_sysfs_init);
2272 * cpu_bit_bitmap[] is a special, "compressed" data structure that
2273 * represents all NR_CPUS bits binary values of 1<<nr.
2275 * It is used by cpumask_of() to get a constant address to a CPU
2276 * mask value that has a single bit set only.
2279 /* cpu_bit_bitmap[0] is empty - so we can back into it */
2280 #define MASK_DECLARE_1(x) [x+1][0] = (1UL << (x))
2281 #define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
2282 #define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
2283 #define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
2285 const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
2287 MASK_DECLARE_8(0), MASK_DECLARE_8(8),
2288 MASK_DECLARE_8(16), MASK_DECLARE_8(24),
2289 #if BITS_PER_LONG > 32
2290 MASK_DECLARE_8(32), MASK_DECLARE_8(40),
2291 MASK_DECLARE_8(48), MASK_DECLARE_8(56),
2294 EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
2296 const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
2297 EXPORT_SYMBOL(cpu_all_bits);
2299 #ifdef CONFIG_INIT_ALL_POSSIBLE
2300 struct cpumask __cpu_possible_mask __read_mostly
2303 struct cpumask __cpu_possible_mask __read_mostly;
2305 EXPORT_SYMBOL(__cpu_possible_mask);
2307 struct cpumask __cpu_online_mask __read_mostly;
2308 EXPORT_SYMBOL(__cpu_online_mask);
2310 struct cpumask __cpu_present_mask __read_mostly;
2311 EXPORT_SYMBOL(__cpu_present_mask);
2313 struct cpumask __cpu_active_mask __read_mostly;
2314 EXPORT_SYMBOL(__cpu_active_mask);
2316 void init_cpu_present(const struct cpumask *src)
2318 cpumask_copy(&__cpu_present_mask, src);
2321 void init_cpu_possible(const struct cpumask *src)
2323 cpumask_copy(&__cpu_possible_mask, src);
2326 void init_cpu_online(const struct cpumask *src)
2328 cpumask_copy(&__cpu_online_mask, src);
2332 * Activate the first processor.
2334 void __init boot_cpu_init(void)
2336 int cpu = smp_processor_id();
2338 /* Mark the boot cpu "present", "online" etc for SMP and UP case */
2339 set_cpu_online(cpu, true);
2340 set_cpu_active(cpu, true);
2341 set_cpu_present(cpu, true);
2342 set_cpu_possible(cpu, true);
2345 __boot_cpu_id = cpu;
2350 * Must be called _AFTER_ setting up the per_cpu areas
2352 void __init boot_cpu_hotplug_init(void)
2355 this_cpu_write(cpuhp_state.booted_once, true);
2357 this_cpu_write(cpuhp_state.state, CPUHP_ONLINE);
2361 * These are used for a global "mitigations=" cmdline option for toggling
2362 * optional CPU mitigations.
2364 enum cpu_mitigations {
2365 CPU_MITIGATIONS_OFF,
2366 CPU_MITIGATIONS_AUTO,
2367 CPU_MITIGATIONS_AUTO_NOSMT,
2370 static enum cpu_mitigations cpu_mitigations __ro_after_init =
2371 CPU_MITIGATIONS_AUTO;
2373 static int __init mitigations_parse_cmdline(char *arg)
2375 if (!strcmp(arg, "off"))
2376 cpu_mitigations = CPU_MITIGATIONS_OFF;
2377 else if (!strcmp(arg, "auto"))
2378 cpu_mitigations = CPU_MITIGATIONS_AUTO;
2379 else if (!strcmp(arg, "auto,nosmt"))
2380 cpu_mitigations = CPU_MITIGATIONS_AUTO_NOSMT;
2382 pr_crit("Unsupported mitigations=%s, system may still be vulnerable\n",
2387 early_param("mitigations", mitigations_parse_cmdline);
2389 /* mitigations=off */
2390 bool cpu_mitigations_off(void)
2392 return cpu_mitigations == CPU_MITIGATIONS_OFF;
2394 EXPORT_SYMBOL_GPL(cpu_mitigations_off);
2396 /* mitigations=auto,nosmt */
2397 bool cpu_mitigations_auto_nosmt(void)
2399 return cpu_mitigations == CPU_MITIGATIONS_AUTO_NOSMT;
2401 EXPORT_SYMBOL_GPL(cpu_mitigations_auto_nosmt);