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>
34 #include <linux/random.h>
36 #include <trace/events/power.h>
37 #define CREATE_TRACE_POINTS
38 #include <trace/events/cpuhp.h>
43 * cpuhp_cpu_state - Per cpu hotplug state storage
44 * @state: The current cpu state
45 * @target: The target state
46 * @thread: Pointer to the hotplug thread
47 * @should_run: Thread should execute
48 * @rollback: Perform a rollback
49 * @single: Single callback invocation
50 * @bringup: Single callback bringup or teardown selector
51 * @cb_state: The state for a single callback (install/uninstall)
52 * @result: Result of the operation
53 * @done_up: Signal completion to the issuer of the task for cpu-up
54 * @done_down: Signal completion to the issuer of the task for cpu-down
56 struct cpuhp_cpu_state {
57 enum cpuhp_state state;
58 enum cpuhp_state target;
59 enum cpuhp_state fail;
61 struct task_struct *thread;
67 struct hlist_node *node;
68 struct hlist_node *last;
69 enum cpuhp_state cb_state;
71 struct completion done_up;
72 struct completion done_down;
76 static DEFINE_PER_CPU(struct cpuhp_cpu_state, cpuhp_state) = {
77 .fail = CPUHP_INVALID,
80 #if defined(CONFIG_LOCKDEP) && defined(CONFIG_SMP)
81 static struct lockdep_map cpuhp_state_up_map =
82 STATIC_LOCKDEP_MAP_INIT("cpuhp_state-up", &cpuhp_state_up_map);
83 static struct lockdep_map cpuhp_state_down_map =
84 STATIC_LOCKDEP_MAP_INIT("cpuhp_state-down", &cpuhp_state_down_map);
87 static void inline cpuhp_lock_acquire(bool bringup)
89 lock_map_acquire(bringup ? &cpuhp_state_up_map : &cpuhp_state_down_map);
92 static void inline cpuhp_lock_release(bool bringup)
94 lock_map_release(bringup ? &cpuhp_state_up_map : &cpuhp_state_down_map);
98 static void inline cpuhp_lock_acquire(bool bringup) { }
99 static void inline cpuhp_lock_release(bool bringup) { }
104 * cpuhp_step - Hotplug state machine step
105 * @name: Name of the step
106 * @startup: Startup function of the step
107 * @teardown: Teardown function of the step
108 * @skip_onerr: Do not invoke the functions on error rollback
109 * Will go away once the notifiers are gone
110 * @cant_stop: Bringup/teardown can't be stopped at this step
115 int (*single)(unsigned int cpu);
116 int (*multi)(unsigned int cpu,
117 struct hlist_node *node);
120 int (*single)(unsigned int cpu);
121 int (*multi)(unsigned int cpu,
122 struct hlist_node *node);
124 struct hlist_head list;
130 static DEFINE_MUTEX(cpuhp_state_mutex);
131 static struct cpuhp_step cpuhp_bp_states[];
132 static struct cpuhp_step cpuhp_ap_states[];
134 static bool cpuhp_is_ap_state(enum cpuhp_state state)
137 * The extra check for CPUHP_TEARDOWN_CPU is only for documentation
138 * purposes as that state is handled explicitly in cpu_down.
140 return state > CPUHP_BRINGUP_CPU && state != CPUHP_TEARDOWN_CPU;
143 static struct cpuhp_step *cpuhp_get_step(enum cpuhp_state state)
145 struct cpuhp_step *sp;
147 sp = cpuhp_is_ap_state(state) ? cpuhp_ap_states : cpuhp_bp_states;
152 * cpuhp_invoke_callback _ Invoke the callbacks for a given state
153 * @cpu: The cpu for which the callback should be invoked
154 * @state: The state to do callbacks for
155 * @bringup: True if the bringup callback should be invoked
156 * @node: For multi-instance, do a single entry callback for install/remove
157 * @lastp: For multi-instance rollback, remember how far we got
159 * Called from cpu hotplug and from the state register machinery.
161 static int cpuhp_invoke_callback(unsigned int cpu, enum cpuhp_state state,
162 bool bringup, struct hlist_node *node,
163 struct hlist_node **lastp)
165 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
166 struct cpuhp_step *step = cpuhp_get_step(state);
167 int (*cbm)(unsigned int cpu, struct hlist_node *node);
168 int (*cb)(unsigned int cpu);
171 if (st->fail == state) {
172 st->fail = CPUHP_INVALID;
174 if (!(bringup ? step->startup.single : step->teardown.single))
180 if (!step->multi_instance) {
181 WARN_ON_ONCE(lastp && *lastp);
182 cb = bringup ? step->startup.single : step->teardown.single;
185 trace_cpuhp_enter(cpu, st->target, state, cb);
187 trace_cpuhp_exit(cpu, st->state, state, ret);
190 cbm = bringup ? step->startup.multi : step->teardown.multi;
194 /* Single invocation for instance add/remove */
196 WARN_ON_ONCE(lastp && *lastp);
197 trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
198 ret = cbm(cpu, node);
199 trace_cpuhp_exit(cpu, st->state, state, ret);
203 /* State transition. Invoke on all instances */
205 hlist_for_each(node, &step->list) {
206 if (lastp && node == *lastp)
209 trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
210 ret = cbm(cpu, node);
211 trace_cpuhp_exit(cpu, st->state, state, ret);
225 /* Rollback the instances if one failed */
226 cbm = !bringup ? step->startup.multi : step->teardown.multi;
230 hlist_for_each(node, &step->list) {
234 trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
235 ret = cbm(cpu, node);
236 trace_cpuhp_exit(cpu, st->state, state, ret);
238 * Rollback must not fail,
246 static inline void wait_for_ap_thread(struct cpuhp_cpu_state *st, bool bringup)
248 struct completion *done = bringup ? &st->done_up : &st->done_down;
249 wait_for_completion(done);
252 static inline void complete_ap_thread(struct cpuhp_cpu_state *st, bool bringup)
254 struct completion *done = bringup ? &st->done_up : &st->done_down;
259 * The former STARTING/DYING states, ran with IRQs disabled and must not fail.
261 static bool cpuhp_is_atomic_state(enum cpuhp_state state)
263 return CPUHP_AP_IDLE_DEAD <= state && state < CPUHP_AP_ONLINE;
266 /* Serializes the updates to cpu_online_mask, cpu_present_mask */
267 static DEFINE_MUTEX(cpu_add_remove_lock);
268 bool cpuhp_tasks_frozen;
269 EXPORT_SYMBOL_GPL(cpuhp_tasks_frozen);
272 * The following two APIs (cpu_maps_update_begin/done) must be used when
273 * attempting to serialize the updates to cpu_online_mask & cpu_present_mask.
275 void cpu_maps_update_begin(void)
277 mutex_lock(&cpu_add_remove_lock);
280 void cpu_maps_update_done(void)
282 mutex_unlock(&cpu_add_remove_lock);
286 * If set, cpu_up and cpu_down will return -EBUSY and do nothing.
287 * Should always be manipulated under cpu_add_remove_lock
289 static int cpu_hotplug_disabled;
291 #ifdef CONFIG_HOTPLUG_CPU
293 DEFINE_STATIC_PERCPU_RWSEM(cpu_hotplug_lock);
295 void cpus_read_lock(void)
297 percpu_down_read(&cpu_hotplug_lock);
299 EXPORT_SYMBOL_GPL(cpus_read_lock);
301 void cpus_read_unlock(void)
303 percpu_up_read(&cpu_hotplug_lock);
305 EXPORT_SYMBOL_GPL(cpus_read_unlock);
307 void cpus_write_lock(void)
309 percpu_down_write(&cpu_hotplug_lock);
312 void cpus_write_unlock(void)
314 percpu_up_write(&cpu_hotplug_lock);
317 void lockdep_assert_cpus_held(void)
320 * We can't have hotplug operations before userspace starts running,
321 * and some init codepaths will knowingly not take the hotplug lock.
322 * This is all valid, so mute lockdep until it makes sense to report
325 if (system_state < SYSTEM_RUNNING)
328 percpu_rwsem_assert_held(&cpu_hotplug_lock);
332 * Wait for currently running CPU hotplug operations to complete (if any) and
333 * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
334 * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the
335 * hotplug path before performing hotplug operations. So acquiring that lock
336 * guarantees mutual exclusion from any currently running hotplug operations.
338 void cpu_hotplug_disable(void)
340 cpu_maps_update_begin();
341 cpu_hotplug_disabled++;
342 cpu_maps_update_done();
344 EXPORT_SYMBOL_GPL(cpu_hotplug_disable);
346 static void __cpu_hotplug_enable(void)
348 if (WARN_ONCE(!cpu_hotplug_disabled, "Unbalanced cpu hotplug enable\n"))
350 cpu_hotplug_disabled--;
353 void cpu_hotplug_enable(void)
355 cpu_maps_update_begin();
356 __cpu_hotplug_enable();
357 cpu_maps_update_done();
359 EXPORT_SYMBOL_GPL(cpu_hotplug_enable);
360 #endif /* CONFIG_HOTPLUG_CPU */
363 * Architectures that need SMT-specific errata handling during SMT hotplug
364 * should override this.
366 void __weak arch_smt_update(void) { }
368 #ifdef CONFIG_HOTPLUG_SMT
369 enum cpuhp_smt_control cpu_smt_control __read_mostly = CPU_SMT_ENABLED;
371 void __init cpu_smt_disable(bool force)
373 if (cpu_smt_control == CPU_SMT_FORCE_DISABLED ||
374 cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
378 pr_info("SMT: Force disabled\n");
379 cpu_smt_control = CPU_SMT_FORCE_DISABLED;
381 pr_info("SMT: disabled\n");
382 cpu_smt_control = CPU_SMT_DISABLED;
387 * The decision whether SMT is supported can only be done after the full
388 * CPU identification. Called from architecture code.
390 void __init cpu_smt_check_topology(void)
392 if (!topology_smt_supported())
393 cpu_smt_control = CPU_SMT_NOT_SUPPORTED;
396 static int __init smt_cmdline_disable(char *str)
398 cpu_smt_disable(str && !strcmp(str, "force"));
401 early_param("nosmt", smt_cmdline_disable);
403 static inline bool cpu_smt_allowed(unsigned int cpu)
405 if (cpu_smt_control == CPU_SMT_ENABLED)
408 if (topology_is_primary_thread(cpu))
412 * On x86 it's required to boot all logical CPUs at least once so
413 * that the init code can get a chance to set CR4.MCE on each
414 * CPU. Otherwise, a broadacasted MCE observing CR4.MCE=0b on any
415 * core will shutdown the machine.
417 return !per_cpu(cpuhp_state, cpu).booted_once;
420 static inline bool cpu_smt_allowed(unsigned int cpu) { return true; }
423 static inline enum cpuhp_state
424 cpuhp_set_state(struct cpuhp_cpu_state *st, enum cpuhp_state target)
426 enum cpuhp_state prev_state = st->state;
428 st->rollback = false;
433 st->bringup = st->state < target;
439 cpuhp_reset_state(struct cpuhp_cpu_state *st, enum cpuhp_state prev_state)
444 * If we have st->last we need to undo partial multi_instance of this
445 * state first. Otherwise start undo at the previous state.
454 st->target = prev_state;
455 st->bringup = !st->bringup;
458 /* Regular hotplug invocation of the AP hotplug thread */
459 static void __cpuhp_kick_ap(struct cpuhp_cpu_state *st)
461 if (!st->single && st->state == st->target)
466 * Make sure the above stores are visible before should_run becomes
467 * true. Paired with the mb() above in cpuhp_thread_fun()
470 st->should_run = true;
471 wake_up_process(st->thread);
472 wait_for_ap_thread(st, st->bringup);
475 static int cpuhp_kick_ap(struct cpuhp_cpu_state *st, enum cpuhp_state target)
477 enum cpuhp_state prev_state;
480 prev_state = cpuhp_set_state(st, target);
482 if ((ret = st->result)) {
483 cpuhp_reset_state(st, prev_state);
490 static int bringup_wait_for_ap(unsigned int cpu)
492 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
494 /* Wait for the CPU to reach CPUHP_AP_ONLINE_IDLE */
495 wait_for_ap_thread(st, true);
496 if (WARN_ON_ONCE((!cpu_online(cpu))))
499 /* Unpark the hotplug thread of the target cpu */
500 kthread_unpark(st->thread);
503 * SMT soft disabling on X86 requires to bring the CPU out of the
504 * BIOS 'wait for SIPI' state in order to set the CR4.MCE bit. The
505 * CPU marked itself as booted_once in cpu_notify_starting() so the
506 * cpu_smt_allowed() check will now return false if this is not the
509 if (!cpu_smt_allowed(cpu))
512 if (st->target <= CPUHP_AP_ONLINE_IDLE)
515 return cpuhp_kick_ap(st, st->target);
518 static int bringup_cpu(unsigned int cpu)
520 struct task_struct *idle = idle_thread_get(cpu);
524 * Some architectures have to walk the irq descriptors to
525 * setup the vector space for the cpu which comes online.
526 * Prevent irq alloc/free across the bringup.
530 /* Arch-specific enabling code. */
531 ret = __cpu_up(cpu, idle);
535 return bringup_wait_for_ap(cpu);
539 * Hotplug state machine related functions
542 static void undo_cpu_up(unsigned int cpu, struct cpuhp_cpu_state *st)
544 for (st->state--; st->state > st->target; st->state--) {
545 struct cpuhp_step *step = cpuhp_get_step(st->state);
547 if (!step->skip_onerr)
548 cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
552 static inline bool can_rollback_cpu(struct cpuhp_cpu_state *st)
554 if (IS_ENABLED(CONFIG_HOTPLUG_CPU))
557 * When CPU hotplug is disabled, then taking the CPU down is not
558 * possible because takedown_cpu() and the architecture and
559 * subsystem specific mechanisms are not available. So the CPU
560 * which would be completely unplugged again needs to stay around
561 * in the current state.
563 return st->state <= CPUHP_BRINGUP_CPU;
566 static int cpuhp_up_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
567 enum cpuhp_state target)
569 enum cpuhp_state prev_state = st->state;
572 while (st->state < target) {
574 ret = cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
576 if (can_rollback_cpu(st)) {
577 st->target = prev_state;
578 undo_cpu_up(cpu, st);
587 * The cpu hotplug threads manage the bringup and teardown of the cpus
589 static void cpuhp_create(unsigned int cpu)
591 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
593 init_completion(&st->done_up);
594 init_completion(&st->done_down);
597 static int cpuhp_should_run(unsigned int cpu)
599 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
601 return st->should_run;
605 * Execute teardown/startup callbacks on the plugged cpu. Also used to invoke
606 * callbacks when a state gets [un]installed at runtime.
608 * Each invocation of this function by the smpboot thread does a single AP
611 * It has 3 modes of operation:
612 * - single: runs st->cb_state
613 * - up: runs ++st->state, while st->state < st->target
614 * - down: runs st->state--, while st->state > st->target
616 * When complete or on error, should_run is cleared and the completion is fired.
618 static void cpuhp_thread_fun(unsigned int cpu)
620 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
621 bool bringup = st->bringup;
622 enum cpuhp_state state;
624 if (WARN_ON_ONCE(!st->should_run))
628 * ACQUIRE for the cpuhp_should_run() load of ->should_run. Ensures
629 * that if we see ->should_run we also see the rest of the state.
633 cpuhp_lock_acquire(bringup);
636 state = st->cb_state;
637 st->should_run = false;
642 st->should_run = (st->state < st->target);
643 WARN_ON_ONCE(st->state > st->target);
647 st->should_run = (st->state > st->target);
648 WARN_ON_ONCE(st->state < st->target);
652 WARN_ON_ONCE(!cpuhp_is_ap_state(state));
655 struct cpuhp_step *step = cpuhp_get_step(state);
656 if (step->skip_onerr)
660 if (cpuhp_is_atomic_state(state)) {
662 st->result = cpuhp_invoke_callback(cpu, state, bringup, st->node, &st->last);
666 * STARTING/DYING must not fail!
668 WARN_ON_ONCE(st->result);
670 st->result = cpuhp_invoke_callback(cpu, state, bringup, st->node, &st->last);
675 * If we fail on a rollback, we're up a creek without no
676 * paddle, no way forward, no way back. We loose, thanks for
679 WARN_ON_ONCE(st->rollback);
680 st->should_run = false;
684 cpuhp_lock_release(bringup);
687 complete_ap_thread(st, bringup);
690 /* Invoke a single callback on a remote cpu */
692 cpuhp_invoke_ap_callback(int cpu, enum cpuhp_state state, bool bringup,
693 struct hlist_node *node)
695 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
698 if (!cpu_online(cpu))
701 cpuhp_lock_acquire(false);
702 cpuhp_lock_release(false);
704 cpuhp_lock_acquire(true);
705 cpuhp_lock_release(true);
708 * If we are up and running, use the hotplug thread. For early calls
709 * we invoke the thread function directly.
712 return cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
714 st->rollback = false;
718 st->bringup = bringup;
719 st->cb_state = state;
725 * If we failed and did a partial, do a rollback.
727 if ((ret = st->result) && st->last) {
729 st->bringup = !bringup;
735 * Clean up the leftovers so the next hotplug operation wont use stale
738 st->node = st->last = NULL;
742 static int cpuhp_kick_ap_work(unsigned int cpu)
744 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
745 enum cpuhp_state prev_state = st->state;
748 cpuhp_lock_acquire(false);
749 cpuhp_lock_release(false);
751 cpuhp_lock_acquire(true);
752 cpuhp_lock_release(true);
754 trace_cpuhp_enter(cpu, st->target, prev_state, cpuhp_kick_ap_work);
755 ret = cpuhp_kick_ap(st, st->target);
756 trace_cpuhp_exit(cpu, st->state, prev_state, ret);
761 static struct smp_hotplug_thread cpuhp_threads = {
762 .store = &cpuhp_state.thread,
763 .create = &cpuhp_create,
764 .thread_should_run = cpuhp_should_run,
765 .thread_fn = cpuhp_thread_fun,
766 .thread_comm = "cpuhp/%u",
770 void __init cpuhp_threads_init(void)
772 BUG_ON(smpboot_register_percpu_thread(&cpuhp_threads));
773 kthread_unpark(this_cpu_read(cpuhp_state.thread));
778 * Serialize hotplug trainwrecks outside of the cpu_hotplug_lock
781 * The operation is still serialized against concurrent CPU hotplug via
782 * cpu_add_remove_lock, i.e. CPU map protection. But it is _not_
783 * serialized against other hotplug related activity like adding or
784 * removing of state callbacks and state instances, which invoke either the
785 * startup or the teardown callback of the affected state.
787 * This is required for subsystems which are unfixable vs. CPU hotplug and
788 * evade lock inversion problems by scheduling work which has to be
789 * completed _before_ cpu_up()/_cpu_down() returns.
791 * Don't even think about adding anything to this for any new code or even
792 * drivers. It's only purpose is to keep existing lock order trainwrecks
795 * For cpu_down() there might be valid reasons to finish cleanups which are
796 * not required to be done under cpu_hotplug_lock, but that's a different
797 * story and would be not invoked via this.
799 static void cpu_up_down_serialize_trainwrecks(bool tasks_frozen)
802 * cpusets delegate hotplug operations to a worker to "solve" the
803 * lock order problems. Wait for the worker, but only if tasks are
804 * _not_ frozen (suspend, hibernate) as that would wait forever.
806 * The wait is required because otherwise the hotplug operation
807 * returns with inconsistent state, which could even be observed in
808 * user space when a new CPU is brought up. The CPU plug uevent
809 * would be delivered and user space reacting on it would fail to
810 * move tasks to the newly plugged CPU up to the point where the
811 * work has finished because up to that point the newly plugged CPU
812 * is not assignable in cpusets/cgroups. On unplug that's not
813 * necessarily a visible issue, but it is still inconsistent state,
814 * which is the real problem which needs to be "fixed". This can't
815 * prevent the transient state between scheduling the work and
816 * returning from waiting for it.
819 cpuset_wait_for_hotplug();
822 #ifdef CONFIG_HOTPLUG_CPU
823 #ifndef arch_clear_mm_cpumask_cpu
824 #define arch_clear_mm_cpumask_cpu(cpu, mm) cpumask_clear_cpu(cpu, mm_cpumask(mm))
828 * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU
831 * This function walks all processes, finds a valid mm struct for each one and
832 * then clears a corresponding bit in mm's cpumask. While this all sounds
833 * trivial, there are various non-obvious corner cases, which this function
834 * tries to solve in a safe manner.
836 * Also note that the function uses a somewhat relaxed locking scheme, so it may
837 * be called only for an already offlined CPU.
839 void clear_tasks_mm_cpumask(int cpu)
841 struct task_struct *p;
844 * This function is called after the cpu is taken down and marked
845 * offline, so its not like new tasks will ever get this cpu set in
846 * their mm mask. -- Peter Zijlstra
847 * Thus, we may use rcu_read_lock() here, instead of grabbing
848 * full-fledged tasklist_lock.
850 WARN_ON(cpu_online(cpu));
852 for_each_process(p) {
853 struct task_struct *t;
856 * Main thread might exit, but other threads may still have
857 * a valid mm. Find one.
859 t = find_lock_task_mm(p);
862 arch_clear_mm_cpumask_cpu(cpu, t->mm);
868 /* Take this CPU down. */
869 static int take_cpu_down(void *_param)
871 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
872 enum cpuhp_state target = max((int)st->target, CPUHP_AP_OFFLINE);
873 int err, cpu = smp_processor_id();
876 /* Ensure this CPU doesn't handle any more interrupts. */
877 err = __cpu_disable();
882 * We get here while we are in CPUHP_TEARDOWN_CPU state and we must not
883 * do this step again.
885 WARN_ON(st->state != CPUHP_TEARDOWN_CPU);
887 /* Invoke the former CPU_DYING callbacks */
888 for (; st->state > target; st->state--) {
889 ret = cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
891 * DYING must not fail!
896 /* Give up timekeeping duties */
897 tick_handover_do_timer();
898 /* Park the stopper thread */
899 stop_machine_park(cpu);
903 static int takedown_cpu(unsigned int cpu)
905 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
908 /* Park the smpboot threads */
909 kthread_park(per_cpu_ptr(&cpuhp_state, cpu)->thread);
912 * Prevent irq alloc/free while the dying cpu reorganizes the
913 * interrupt affinities.
918 * So now all preempt/rcu users must observe !cpu_active().
920 err = stop_machine_cpuslocked(take_cpu_down, NULL, cpumask_of(cpu));
922 /* CPU refused to die */
924 /* Unpark the hotplug thread so we can rollback there */
925 kthread_unpark(per_cpu_ptr(&cpuhp_state, cpu)->thread);
928 BUG_ON(cpu_online(cpu));
931 * The CPUHP_AP_SCHED_MIGRATE_DYING callback will have removed all
932 * runnable tasks from the cpu, there's only the idle task left now
933 * that the migration thread is done doing the stop_machine thing.
935 * Wait for the stop thread to go away.
937 wait_for_ap_thread(st, false);
938 BUG_ON(st->state != CPUHP_AP_IDLE_DEAD);
940 /* Interrupts are moved away from the dying cpu, reenable alloc/free */
943 hotplug_cpu__broadcast_tick_pull(cpu);
944 /* This actually kills the CPU. */
947 tick_cleanup_dead_cpu(cpu);
948 rcutree_migrate_callbacks(cpu);
952 static void cpuhp_complete_idle_dead(void *arg)
954 struct cpuhp_cpu_state *st = arg;
956 complete_ap_thread(st, false);
959 void cpuhp_report_idle_dead(void)
961 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
963 BUG_ON(st->state != CPUHP_AP_OFFLINE);
964 rcu_report_dead(smp_processor_id());
965 st->state = CPUHP_AP_IDLE_DEAD;
967 * We cannot call complete after rcu_report_dead() so we delegate it
970 smp_call_function_single(cpumask_first(cpu_online_mask),
971 cpuhp_complete_idle_dead, st, 0);
974 static void undo_cpu_down(unsigned int cpu, struct cpuhp_cpu_state *st)
976 for (st->state++; st->state < st->target; st->state++) {
977 struct cpuhp_step *step = cpuhp_get_step(st->state);
979 if (!step->skip_onerr)
980 cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
984 static int cpuhp_down_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
985 enum cpuhp_state target)
987 enum cpuhp_state prev_state = st->state;
990 for (; st->state > target; st->state--) {
991 ret = cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
993 st->target = prev_state;
994 if (st->state < prev_state)
995 undo_cpu_down(cpu, st);
1002 /* Requires cpu_add_remove_lock to be held */
1003 static int __ref _cpu_down(unsigned int cpu, int tasks_frozen,
1004 enum cpuhp_state target)
1006 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1007 int prev_state, ret = 0;
1009 if (num_online_cpus() == 1)
1012 if (!cpu_present(cpu))
1017 cpuhp_tasks_frozen = tasks_frozen;
1019 prev_state = cpuhp_set_state(st, target);
1021 * If the current CPU state is in the range of the AP hotplug thread,
1022 * then we need to kick the thread.
1024 if (st->state > CPUHP_TEARDOWN_CPU) {
1025 st->target = max((int)target, CPUHP_TEARDOWN_CPU);
1026 ret = cpuhp_kick_ap_work(cpu);
1028 * The AP side has done the error rollback already. Just
1029 * return the error code..
1035 * We might have stopped still in the range of the AP hotplug
1036 * thread. Nothing to do anymore.
1038 if (st->state > CPUHP_TEARDOWN_CPU)
1041 st->target = target;
1044 * The AP brought itself down to CPUHP_TEARDOWN_CPU. So we need
1045 * to do the further cleanups.
1047 ret = cpuhp_down_callbacks(cpu, st, target);
1048 if (ret && st->state == CPUHP_TEARDOWN_CPU && st->state < prev_state) {
1049 cpuhp_reset_state(st, prev_state);
1050 __cpuhp_kick_ap(st);
1054 cpus_write_unlock();
1056 * Do post unplug cleanup. This is still protected against
1057 * concurrent CPU hotplug via cpu_add_remove_lock.
1059 lockup_detector_cleanup();
1061 cpu_up_down_serialize_trainwrecks(tasks_frozen);
1065 static int cpu_down_maps_locked(unsigned int cpu, enum cpuhp_state target)
1067 if (cpu_hotplug_disabled)
1069 return _cpu_down(cpu, 0, target);
1072 static int do_cpu_down(unsigned int cpu, enum cpuhp_state target)
1076 cpu_maps_update_begin();
1077 err = cpu_down_maps_locked(cpu, target);
1078 cpu_maps_update_done();
1082 int cpu_down(unsigned int cpu)
1084 return do_cpu_down(cpu, CPUHP_OFFLINE);
1086 EXPORT_SYMBOL(cpu_down);
1089 #define takedown_cpu NULL
1090 #endif /*CONFIG_HOTPLUG_CPU*/
1093 * notify_cpu_starting(cpu) - Invoke the callbacks on the starting CPU
1094 * @cpu: cpu that just started
1096 * It must be called by the arch code on the new cpu, before the new cpu
1097 * enables interrupts and before the "boot" cpu returns from __cpu_up().
1099 void notify_cpu_starting(unsigned int cpu)
1101 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1102 enum cpuhp_state target = min((int)st->target, CPUHP_AP_ONLINE);
1105 rcu_cpu_starting(cpu); /* Enables RCU usage on this CPU. */
1106 st->booted_once = true;
1107 while (st->state < target) {
1109 ret = cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
1111 * STARTING must not fail!
1118 * Called from the idle task. Wake up the controlling task which brings the
1119 * hotplug thread of the upcoming CPU up and then delegates the rest of the
1120 * online bringup to the hotplug thread.
1122 void cpuhp_online_idle(enum cpuhp_state state)
1124 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
1126 /* Happens for the boot cpu */
1127 if (state != CPUHP_AP_ONLINE_IDLE)
1131 * Unpart the stopper thread before we start the idle loop (and start
1132 * scheduling); this ensures the stopper task is always available.
1134 stop_machine_unpark(smp_processor_id());
1136 st->state = CPUHP_AP_ONLINE_IDLE;
1137 complete_ap_thread(st, true);
1140 /* Requires cpu_add_remove_lock to be held */
1141 static int _cpu_up(unsigned int cpu, int tasks_frozen, enum cpuhp_state target)
1143 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1144 struct task_struct *idle;
1149 if (!cpu_present(cpu)) {
1155 * The caller of do_cpu_up might have raced with another
1156 * caller. Ignore it for now.
1158 if (st->state >= target)
1161 if (st->state == CPUHP_OFFLINE) {
1162 /* Let it fail before we try to bring the cpu up */
1163 idle = idle_thread_get(cpu);
1165 ret = PTR_ERR(idle);
1170 cpuhp_tasks_frozen = tasks_frozen;
1172 cpuhp_set_state(st, target);
1174 * If the current CPU state is in the range of the AP hotplug thread,
1175 * then we need to kick the thread once more.
1177 if (st->state > CPUHP_BRINGUP_CPU) {
1178 ret = cpuhp_kick_ap_work(cpu);
1180 * The AP side has done the error rollback already. Just
1181 * return the error code..
1188 * Try to reach the target state. We max out on the BP at
1189 * CPUHP_BRINGUP_CPU. After that the AP hotplug thread is
1190 * responsible for bringing it up to the target state.
1192 target = min((int)target, CPUHP_BRINGUP_CPU);
1193 ret = cpuhp_up_callbacks(cpu, st, target);
1195 cpus_write_unlock();
1197 cpu_up_down_serialize_trainwrecks(tasks_frozen);
1201 static int do_cpu_up(unsigned int cpu, enum cpuhp_state target)
1205 if (!cpu_possible(cpu)) {
1206 pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n",
1208 #if defined(CONFIG_IA64)
1209 pr_err("please check additional_cpus= boot parameter\n");
1214 err = try_online_node(cpu_to_node(cpu));
1218 cpu_maps_update_begin();
1220 if (cpu_hotplug_disabled) {
1224 if (!cpu_smt_allowed(cpu)) {
1229 err = _cpu_up(cpu, 0, target);
1231 cpu_maps_update_done();
1235 int cpu_up(unsigned int cpu)
1237 return do_cpu_up(cpu, CPUHP_ONLINE);
1239 EXPORT_SYMBOL_GPL(cpu_up);
1241 #ifdef CONFIG_PM_SLEEP_SMP
1242 static cpumask_var_t frozen_cpus;
1244 int freeze_secondary_cpus(int primary)
1248 cpu_maps_update_begin();
1249 if (!cpu_online(primary))
1250 primary = cpumask_first(cpu_online_mask);
1252 * We take down all of the non-boot CPUs in one shot to avoid races
1253 * with the userspace trying to use the CPU hotplug at the same time
1255 cpumask_clear(frozen_cpus);
1257 pr_info("Disabling non-boot CPUs ...\n");
1258 for_each_online_cpu(cpu) {
1261 trace_suspend_resume(TPS("CPU_OFF"), cpu, true);
1262 error = _cpu_down(cpu, 1, CPUHP_OFFLINE);
1263 trace_suspend_resume(TPS("CPU_OFF"), cpu, false);
1265 cpumask_set_cpu(cpu, frozen_cpus);
1267 pr_err("Error taking CPU%d down: %d\n", cpu, error);
1273 BUG_ON(num_online_cpus() > 1);
1275 pr_err("Non-boot CPUs are not disabled\n");
1278 * Make sure the CPUs won't be enabled by someone else. We need to do
1279 * this even in case of failure as all disable_nonboot_cpus() users are
1280 * supposed to do enable_nonboot_cpus() on the failure path.
1282 cpu_hotplug_disabled++;
1284 cpu_maps_update_done();
1288 void __weak arch_enable_nonboot_cpus_begin(void)
1292 void __weak arch_enable_nonboot_cpus_end(void)
1296 void enable_nonboot_cpus(void)
1300 /* Allow everyone to use the CPU hotplug again */
1301 cpu_maps_update_begin();
1302 __cpu_hotplug_enable();
1303 if (cpumask_empty(frozen_cpus))
1306 pr_info("Enabling non-boot CPUs ...\n");
1308 arch_enable_nonboot_cpus_begin();
1310 for_each_cpu(cpu, frozen_cpus) {
1311 trace_suspend_resume(TPS("CPU_ON"), cpu, true);
1312 error = _cpu_up(cpu, 1, CPUHP_ONLINE);
1313 trace_suspend_resume(TPS("CPU_ON"), cpu, false);
1315 pr_info("CPU%d is up\n", cpu);
1318 pr_warn("Error taking CPU%d up: %d\n", cpu, error);
1321 arch_enable_nonboot_cpus_end();
1323 cpumask_clear(frozen_cpus);
1325 cpu_maps_update_done();
1328 static int __init alloc_frozen_cpus(void)
1330 if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
1334 core_initcall(alloc_frozen_cpus);
1337 * When callbacks for CPU hotplug notifications are being executed, we must
1338 * ensure that the state of the system with respect to the tasks being frozen
1339 * or not, as reported by the notification, remains unchanged *throughout the
1340 * duration* of the execution of the callbacks.
1341 * Hence we need to prevent the freezer from racing with regular CPU hotplug.
1343 * This synchronization is implemented by mutually excluding regular CPU
1344 * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
1345 * Hibernate notifications.
1348 cpu_hotplug_pm_callback(struct notifier_block *nb,
1349 unsigned long action, void *ptr)
1353 case PM_SUSPEND_PREPARE:
1354 case PM_HIBERNATION_PREPARE:
1355 cpu_hotplug_disable();
1358 case PM_POST_SUSPEND:
1359 case PM_POST_HIBERNATION:
1360 cpu_hotplug_enable();
1371 static int __init cpu_hotplug_pm_sync_init(void)
1374 * cpu_hotplug_pm_callback has higher priority than x86
1375 * bsp_pm_callback which depends on cpu_hotplug_pm_callback
1376 * to disable cpu hotplug to avoid cpu hotplug race.
1378 pm_notifier(cpu_hotplug_pm_callback, 0);
1381 core_initcall(cpu_hotplug_pm_sync_init);
1383 #endif /* CONFIG_PM_SLEEP_SMP */
1387 #endif /* CONFIG_SMP */
1389 /* Boot processor state steps */
1390 static struct cpuhp_step cpuhp_bp_states[] = {
1393 .startup.single = NULL,
1394 .teardown.single = NULL,
1397 [CPUHP_CREATE_THREADS]= {
1398 .name = "threads:prepare",
1399 .startup.single = smpboot_create_threads,
1400 .teardown.single = NULL,
1403 [CPUHP_PERF_PREPARE] = {
1404 .name = "perf:prepare",
1405 .startup.single = perf_event_init_cpu,
1406 .teardown.single = perf_event_exit_cpu,
1408 [CPUHP_RANDOM_PREPARE] = {
1409 .name = "random:prepare",
1410 .startup.single = random_prepare_cpu,
1411 .teardown.single = NULL,
1413 [CPUHP_WORKQUEUE_PREP] = {
1414 .name = "workqueue:prepare",
1415 .startup.single = workqueue_prepare_cpu,
1416 .teardown.single = NULL,
1418 [CPUHP_HRTIMERS_PREPARE] = {
1419 .name = "hrtimers:prepare",
1420 .startup.single = hrtimers_prepare_cpu,
1421 .teardown.single = hrtimers_dead_cpu,
1423 [CPUHP_SMPCFD_PREPARE] = {
1424 .name = "smpcfd:prepare",
1425 .startup.single = smpcfd_prepare_cpu,
1426 .teardown.single = smpcfd_dead_cpu,
1428 [CPUHP_RELAY_PREPARE] = {
1429 .name = "relay:prepare",
1430 .startup.single = relay_prepare_cpu,
1431 .teardown.single = NULL,
1433 [CPUHP_SLAB_PREPARE] = {
1434 .name = "slab:prepare",
1435 .startup.single = slab_prepare_cpu,
1436 .teardown.single = slab_dead_cpu,
1438 [CPUHP_RCUTREE_PREP] = {
1439 .name = "RCU/tree:prepare",
1440 .startup.single = rcutree_prepare_cpu,
1441 .teardown.single = rcutree_dead_cpu,
1444 * On the tear-down path, timers_dead_cpu() must be invoked
1445 * before blk_mq_queue_reinit_notify() from notify_dead(),
1446 * otherwise a RCU stall occurs.
1448 [CPUHP_TIMERS_PREPARE] = {
1449 .name = "timers:dead",
1450 .startup.single = timers_prepare_cpu,
1451 .teardown.single = timers_dead_cpu,
1453 /* Kicks the plugged cpu into life */
1454 [CPUHP_BRINGUP_CPU] = {
1455 .name = "cpu:bringup",
1456 .startup.single = bringup_cpu,
1457 .teardown.single = NULL,
1461 * Handled on controll processor until the plugged processor manages
1464 [CPUHP_TEARDOWN_CPU] = {
1465 .name = "cpu:teardown",
1466 .startup.single = NULL,
1467 .teardown.single = takedown_cpu,
1471 [CPUHP_BRINGUP_CPU] = { },
1475 /* Application processor state steps */
1476 static struct cpuhp_step cpuhp_ap_states[] = {
1478 /* Final state before CPU kills itself */
1479 [CPUHP_AP_IDLE_DEAD] = {
1480 .name = "idle:dead",
1483 * Last state before CPU enters the idle loop to die. Transient state
1484 * for synchronization.
1486 [CPUHP_AP_OFFLINE] = {
1487 .name = "ap:offline",
1490 /* First state is scheduler control. Interrupts are disabled */
1491 [CPUHP_AP_SCHED_STARTING] = {
1492 .name = "sched:starting",
1493 .startup.single = sched_cpu_starting,
1494 .teardown.single = sched_cpu_dying,
1496 [CPUHP_AP_RCUTREE_DYING] = {
1497 .name = "RCU/tree:dying",
1498 .startup.single = NULL,
1499 .teardown.single = rcutree_dying_cpu,
1501 [CPUHP_AP_SMPCFD_DYING] = {
1502 .name = "smpcfd:dying",
1503 .startup.single = NULL,
1504 .teardown.single = smpcfd_dying_cpu,
1506 /* Entry state on starting. Interrupts enabled from here on. Transient
1507 * state for synchronsization */
1508 [CPUHP_AP_ONLINE] = {
1509 .name = "ap:online",
1511 /* Handle smpboot threads park/unpark */
1512 [CPUHP_AP_SMPBOOT_THREADS] = {
1513 .name = "smpboot/threads:online",
1514 .startup.single = smpboot_unpark_threads,
1515 .teardown.single = smpboot_park_threads,
1517 [CPUHP_AP_IRQ_AFFINITY_ONLINE] = {
1518 .name = "irq/affinity:online",
1519 .startup.single = irq_affinity_online_cpu,
1520 .teardown.single = NULL,
1522 [CPUHP_AP_PERF_ONLINE] = {
1523 .name = "perf:online",
1524 .startup.single = perf_event_init_cpu,
1525 .teardown.single = perf_event_exit_cpu,
1527 [CPUHP_AP_WORKQUEUE_ONLINE] = {
1528 .name = "workqueue:online",
1529 .startup.single = workqueue_online_cpu,
1530 .teardown.single = workqueue_offline_cpu,
1532 [CPUHP_AP_RANDOM_ONLINE] = {
1533 .name = "random:online",
1534 .startup.single = random_online_cpu,
1535 .teardown.single = NULL,
1537 [CPUHP_AP_RCUTREE_ONLINE] = {
1538 .name = "RCU/tree:online",
1539 .startup.single = rcutree_online_cpu,
1540 .teardown.single = rcutree_offline_cpu,
1544 * The dynamically registered state space is here
1548 /* Last state is scheduler control setting the cpu active */
1549 [CPUHP_AP_ACTIVE] = {
1550 .name = "sched:active",
1551 .startup.single = sched_cpu_activate,
1552 .teardown.single = sched_cpu_deactivate,
1556 /* CPU is fully up and running. */
1559 .startup.single = NULL,
1560 .teardown.single = NULL,
1564 /* Sanity check for callbacks */
1565 static int cpuhp_cb_check(enum cpuhp_state state)
1567 if (state <= CPUHP_OFFLINE || state >= CPUHP_ONLINE)
1573 * Returns a free for dynamic slot assignment of the Online state. The states
1574 * are protected by the cpuhp_slot_states mutex and an empty slot is identified
1575 * by having no name assigned.
1577 static int cpuhp_reserve_state(enum cpuhp_state state)
1579 enum cpuhp_state i, end;
1580 struct cpuhp_step *step;
1583 case CPUHP_AP_ONLINE_DYN:
1584 step = cpuhp_ap_states + CPUHP_AP_ONLINE_DYN;
1585 end = CPUHP_AP_ONLINE_DYN_END;
1587 case CPUHP_BP_PREPARE_DYN:
1588 step = cpuhp_bp_states + CPUHP_BP_PREPARE_DYN;
1589 end = CPUHP_BP_PREPARE_DYN_END;
1595 for (i = state; i <= end; i++, step++) {
1599 WARN(1, "No more dynamic states available for CPU hotplug\n");
1603 static int cpuhp_store_callbacks(enum cpuhp_state state, const char *name,
1604 int (*startup)(unsigned int cpu),
1605 int (*teardown)(unsigned int cpu),
1606 bool multi_instance)
1608 /* (Un)Install the callbacks for further cpu hotplug operations */
1609 struct cpuhp_step *sp;
1613 * If name is NULL, then the state gets removed.
1615 * CPUHP_AP_ONLINE_DYN and CPUHP_BP_PREPARE_DYN are handed out on
1616 * the first allocation from these dynamic ranges, so the removal
1617 * would trigger a new allocation and clear the wrong (already
1618 * empty) state, leaving the callbacks of the to be cleared state
1619 * dangling, which causes wreckage on the next hotplug operation.
1621 if (name && (state == CPUHP_AP_ONLINE_DYN ||
1622 state == CPUHP_BP_PREPARE_DYN)) {
1623 ret = cpuhp_reserve_state(state);
1628 sp = cpuhp_get_step(state);
1629 if (name && sp->name)
1632 sp->startup.single = startup;
1633 sp->teardown.single = teardown;
1635 sp->multi_instance = multi_instance;
1636 INIT_HLIST_HEAD(&sp->list);
1640 static void *cpuhp_get_teardown_cb(enum cpuhp_state state)
1642 return cpuhp_get_step(state)->teardown.single;
1646 * Call the startup/teardown function for a step either on the AP or
1647 * on the current CPU.
1649 static int cpuhp_issue_call(int cpu, enum cpuhp_state state, bool bringup,
1650 struct hlist_node *node)
1652 struct cpuhp_step *sp = cpuhp_get_step(state);
1656 * If there's nothing to do, we done.
1657 * Relies on the union for multi_instance.
1659 if ((bringup && !sp->startup.single) ||
1660 (!bringup && !sp->teardown.single))
1663 * The non AP bound callbacks can fail on bringup. On teardown
1664 * e.g. module removal we crash for now.
1667 if (cpuhp_is_ap_state(state))
1668 ret = cpuhp_invoke_ap_callback(cpu, state, bringup, node);
1670 ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
1672 ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
1674 BUG_ON(ret && !bringup);
1679 * Called from __cpuhp_setup_state on a recoverable failure.
1681 * Note: The teardown callbacks for rollback are not allowed to fail!
1683 static void cpuhp_rollback_install(int failedcpu, enum cpuhp_state state,
1684 struct hlist_node *node)
1688 /* Roll back the already executed steps on the other cpus */
1689 for_each_present_cpu(cpu) {
1690 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1691 int cpustate = st->state;
1693 if (cpu >= failedcpu)
1696 /* Did we invoke the startup call on that cpu ? */
1697 if (cpustate >= state)
1698 cpuhp_issue_call(cpu, state, false, node);
1702 int __cpuhp_state_add_instance_cpuslocked(enum cpuhp_state state,
1703 struct hlist_node *node,
1706 struct cpuhp_step *sp;
1710 lockdep_assert_cpus_held();
1712 sp = cpuhp_get_step(state);
1713 if (sp->multi_instance == false)
1716 mutex_lock(&cpuhp_state_mutex);
1718 if (!invoke || !sp->startup.multi)
1722 * Try to call the startup callback for each present cpu
1723 * depending on the hotplug state of the cpu.
1725 for_each_present_cpu(cpu) {
1726 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1727 int cpustate = st->state;
1729 if (cpustate < state)
1732 ret = cpuhp_issue_call(cpu, state, true, node);
1734 if (sp->teardown.multi)
1735 cpuhp_rollback_install(cpu, state, node);
1741 hlist_add_head(node, &sp->list);
1743 mutex_unlock(&cpuhp_state_mutex);
1747 int __cpuhp_state_add_instance(enum cpuhp_state state, struct hlist_node *node,
1753 ret = __cpuhp_state_add_instance_cpuslocked(state, node, invoke);
1757 EXPORT_SYMBOL_GPL(__cpuhp_state_add_instance);
1760 * __cpuhp_setup_state_cpuslocked - Setup the callbacks for an hotplug machine state
1761 * @state: The state to setup
1762 * @invoke: If true, the startup function is invoked for cpus where
1763 * cpu state >= @state
1764 * @startup: startup callback function
1765 * @teardown: teardown callback function
1766 * @multi_instance: State is set up for multiple instances which get
1769 * The caller needs to hold cpus read locked while calling this function.
1772 * Positive state number if @state is CPUHP_AP_ONLINE_DYN
1773 * 0 for all other states
1774 * On failure: proper (negative) error code
1776 int __cpuhp_setup_state_cpuslocked(enum cpuhp_state state,
1777 const char *name, bool invoke,
1778 int (*startup)(unsigned int cpu),
1779 int (*teardown)(unsigned int cpu),
1780 bool multi_instance)
1785 lockdep_assert_cpus_held();
1787 if (cpuhp_cb_check(state) || !name)
1790 mutex_lock(&cpuhp_state_mutex);
1792 ret = cpuhp_store_callbacks(state, name, startup, teardown,
1795 dynstate = state == CPUHP_AP_ONLINE_DYN;
1796 if (ret > 0 && dynstate) {
1801 if (ret || !invoke || !startup)
1805 * Try to call the startup callback for each present cpu
1806 * depending on the hotplug state of the cpu.
1808 for_each_present_cpu(cpu) {
1809 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1810 int cpustate = st->state;
1812 if (cpustate < state)
1815 ret = cpuhp_issue_call(cpu, state, true, NULL);
1818 cpuhp_rollback_install(cpu, state, NULL);
1819 cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
1824 mutex_unlock(&cpuhp_state_mutex);
1826 * If the requested state is CPUHP_AP_ONLINE_DYN, return the
1827 * dynamically allocated state in case of success.
1829 if (!ret && dynstate)
1833 EXPORT_SYMBOL(__cpuhp_setup_state_cpuslocked);
1835 int __cpuhp_setup_state(enum cpuhp_state state,
1836 const char *name, bool invoke,
1837 int (*startup)(unsigned int cpu),
1838 int (*teardown)(unsigned int cpu),
1839 bool multi_instance)
1844 ret = __cpuhp_setup_state_cpuslocked(state, name, invoke, startup,
1845 teardown, multi_instance);
1849 EXPORT_SYMBOL(__cpuhp_setup_state);
1851 int __cpuhp_state_remove_instance(enum cpuhp_state state,
1852 struct hlist_node *node, bool invoke)
1854 struct cpuhp_step *sp = cpuhp_get_step(state);
1857 BUG_ON(cpuhp_cb_check(state));
1859 if (!sp->multi_instance)
1863 mutex_lock(&cpuhp_state_mutex);
1865 if (!invoke || !cpuhp_get_teardown_cb(state))
1868 * Call the teardown callback for each present cpu depending
1869 * on the hotplug state of the cpu. This function is not
1870 * allowed to fail currently!
1872 for_each_present_cpu(cpu) {
1873 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1874 int cpustate = st->state;
1876 if (cpustate >= state)
1877 cpuhp_issue_call(cpu, state, false, node);
1882 mutex_unlock(&cpuhp_state_mutex);
1887 EXPORT_SYMBOL_GPL(__cpuhp_state_remove_instance);
1890 * __cpuhp_remove_state_cpuslocked - Remove the callbacks for an hotplug machine state
1891 * @state: The state to remove
1892 * @invoke: If true, the teardown function is invoked for cpus where
1893 * cpu state >= @state
1895 * The caller needs to hold cpus read locked while calling this function.
1896 * The teardown callback is currently not allowed to fail. Think
1897 * about module removal!
1899 void __cpuhp_remove_state_cpuslocked(enum cpuhp_state state, bool invoke)
1901 struct cpuhp_step *sp = cpuhp_get_step(state);
1904 BUG_ON(cpuhp_cb_check(state));
1906 lockdep_assert_cpus_held();
1908 mutex_lock(&cpuhp_state_mutex);
1909 if (sp->multi_instance) {
1910 WARN(!hlist_empty(&sp->list),
1911 "Error: Removing state %d which has instances left.\n",
1916 if (!invoke || !cpuhp_get_teardown_cb(state))
1920 * Call the teardown callback for each present cpu depending
1921 * on the hotplug state of the cpu. This function is not
1922 * allowed to fail currently!
1924 for_each_present_cpu(cpu) {
1925 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1926 int cpustate = st->state;
1928 if (cpustate >= state)
1929 cpuhp_issue_call(cpu, state, false, NULL);
1932 cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
1933 mutex_unlock(&cpuhp_state_mutex);
1935 EXPORT_SYMBOL(__cpuhp_remove_state_cpuslocked);
1937 void __cpuhp_remove_state(enum cpuhp_state state, bool invoke)
1940 __cpuhp_remove_state_cpuslocked(state, invoke);
1943 EXPORT_SYMBOL(__cpuhp_remove_state);
1945 #if defined(CONFIG_SYSFS) && defined(CONFIG_HOTPLUG_CPU)
1946 static ssize_t show_cpuhp_state(struct device *dev,
1947 struct device_attribute *attr, char *buf)
1949 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
1951 return sprintf(buf, "%d\n", st->state);
1953 static DEVICE_ATTR(state, 0444, show_cpuhp_state, NULL);
1955 static ssize_t write_cpuhp_target(struct device *dev,
1956 struct device_attribute *attr,
1957 const char *buf, size_t count)
1959 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
1960 struct cpuhp_step *sp;
1963 ret = kstrtoint(buf, 10, &target);
1967 #ifdef CONFIG_CPU_HOTPLUG_STATE_CONTROL
1968 if (target < CPUHP_OFFLINE || target > CPUHP_ONLINE)
1971 if (target != CPUHP_OFFLINE && target != CPUHP_ONLINE)
1975 ret = lock_device_hotplug_sysfs();
1979 mutex_lock(&cpuhp_state_mutex);
1980 sp = cpuhp_get_step(target);
1981 ret = !sp->name || sp->cant_stop ? -EINVAL : 0;
1982 mutex_unlock(&cpuhp_state_mutex);
1986 if (st->state < target)
1987 ret = do_cpu_up(dev->id, target);
1989 ret = do_cpu_down(dev->id, target);
1991 unlock_device_hotplug();
1992 return ret ? ret : count;
1995 static ssize_t show_cpuhp_target(struct device *dev,
1996 struct device_attribute *attr, char *buf)
1998 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
2000 return sprintf(buf, "%d\n", st->target);
2002 static DEVICE_ATTR(target, 0644, show_cpuhp_target, write_cpuhp_target);
2005 static ssize_t write_cpuhp_fail(struct device *dev,
2006 struct device_attribute *attr,
2007 const char *buf, size_t count)
2009 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
2010 struct cpuhp_step *sp;
2013 ret = kstrtoint(buf, 10, &fail);
2017 if (fail < CPUHP_OFFLINE || fail > CPUHP_ONLINE)
2021 * Cannot fail STARTING/DYING callbacks.
2023 if (cpuhp_is_atomic_state(fail))
2027 * Cannot fail anything that doesn't have callbacks.
2029 mutex_lock(&cpuhp_state_mutex);
2030 sp = cpuhp_get_step(fail);
2031 if (!sp->startup.single && !sp->teardown.single)
2033 mutex_unlock(&cpuhp_state_mutex);
2042 static ssize_t show_cpuhp_fail(struct device *dev,
2043 struct device_attribute *attr, char *buf)
2045 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
2047 return sprintf(buf, "%d\n", st->fail);
2050 static DEVICE_ATTR(fail, 0644, show_cpuhp_fail, write_cpuhp_fail);
2052 static struct attribute *cpuhp_cpu_attrs[] = {
2053 &dev_attr_state.attr,
2054 &dev_attr_target.attr,
2055 &dev_attr_fail.attr,
2059 static const struct attribute_group cpuhp_cpu_attr_group = {
2060 .attrs = cpuhp_cpu_attrs,
2065 static ssize_t show_cpuhp_states(struct device *dev,
2066 struct device_attribute *attr, char *buf)
2068 ssize_t cur, res = 0;
2071 mutex_lock(&cpuhp_state_mutex);
2072 for (i = CPUHP_OFFLINE; i <= CPUHP_ONLINE; i++) {
2073 struct cpuhp_step *sp = cpuhp_get_step(i);
2076 cur = sprintf(buf, "%3d: %s\n", i, sp->name);
2081 mutex_unlock(&cpuhp_state_mutex);
2084 static DEVICE_ATTR(states, 0444, show_cpuhp_states, NULL);
2086 static struct attribute *cpuhp_cpu_root_attrs[] = {
2087 &dev_attr_states.attr,
2091 static const struct attribute_group cpuhp_cpu_root_attr_group = {
2092 .attrs = cpuhp_cpu_root_attrs,
2097 #ifdef CONFIG_HOTPLUG_SMT
2099 static const char *smt_states[] = {
2100 [CPU_SMT_ENABLED] = "on",
2101 [CPU_SMT_DISABLED] = "off",
2102 [CPU_SMT_FORCE_DISABLED] = "forceoff",
2103 [CPU_SMT_NOT_SUPPORTED] = "notsupported",
2107 show_smt_control(struct device *dev, struct device_attribute *attr, char *buf)
2109 return snprintf(buf, PAGE_SIZE - 2, "%s\n", smt_states[cpu_smt_control]);
2112 static void cpuhp_offline_cpu_device(unsigned int cpu)
2114 struct device *dev = get_cpu_device(cpu);
2116 dev->offline = true;
2117 /* Tell user space about the state change */
2118 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2121 static void cpuhp_online_cpu_device(unsigned int cpu)
2123 struct device *dev = get_cpu_device(cpu);
2125 dev->offline = false;
2126 /* Tell user space about the state change */
2127 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2130 int cpuhp_smt_disable(enum cpuhp_smt_control ctrlval)
2134 cpu_maps_update_begin();
2135 for_each_online_cpu(cpu) {
2136 if (topology_is_primary_thread(cpu))
2138 ret = cpu_down_maps_locked(cpu, CPUHP_OFFLINE);
2142 * As this needs to hold the cpu maps lock it's impossible
2143 * to call device_offline() because that ends up calling
2144 * cpu_down() which takes cpu maps lock. cpu maps lock
2145 * needs to be held as this might race against in kernel
2146 * abusers of the hotplug machinery (thermal management).
2148 * So nothing would update device:offline state. That would
2149 * leave the sysfs entry stale and prevent onlining after
2150 * smt control has been changed to 'off' again. This is
2151 * called under the sysfs hotplug lock, so it is properly
2152 * serialized against the regular offline usage.
2154 cpuhp_offline_cpu_device(cpu);
2157 cpu_smt_control = ctrlval;
2158 cpu_maps_update_done();
2162 int cpuhp_smt_enable(void)
2166 cpu_maps_update_begin();
2167 cpu_smt_control = CPU_SMT_ENABLED;
2168 for_each_present_cpu(cpu) {
2169 /* Skip online CPUs and CPUs on offline nodes */
2170 if (cpu_online(cpu) || !node_online(cpu_to_node(cpu)))
2172 ret = _cpu_up(cpu, 0, CPUHP_ONLINE);
2175 /* See comment in cpuhp_smt_disable() */
2176 cpuhp_online_cpu_device(cpu);
2178 cpu_maps_update_done();
2183 store_smt_control(struct device *dev, struct device_attribute *attr,
2184 const char *buf, size_t count)
2188 if (sysfs_streq(buf, "on"))
2189 ctrlval = CPU_SMT_ENABLED;
2190 else if (sysfs_streq(buf, "off"))
2191 ctrlval = CPU_SMT_DISABLED;
2192 else if (sysfs_streq(buf, "forceoff"))
2193 ctrlval = CPU_SMT_FORCE_DISABLED;
2197 if (cpu_smt_control == CPU_SMT_FORCE_DISABLED)
2200 if (cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
2203 ret = lock_device_hotplug_sysfs();
2207 if (ctrlval != cpu_smt_control) {
2209 case CPU_SMT_ENABLED:
2210 ret = cpuhp_smt_enable();
2212 case CPU_SMT_DISABLED:
2213 case CPU_SMT_FORCE_DISABLED:
2214 ret = cpuhp_smt_disable(ctrlval);
2219 unlock_device_hotplug();
2220 return ret ? ret : count;
2222 static DEVICE_ATTR(control, 0644, show_smt_control, store_smt_control);
2225 show_smt_active(struct device *dev, struct device_attribute *attr, char *buf)
2227 bool active = topology_max_smt_threads() > 1;
2229 return snprintf(buf, PAGE_SIZE - 2, "%d\n", active);
2231 static DEVICE_ATTR(active, 0444, show_smt_active, NULL);
2233 static struct attribute *cpuhp_smt_attrs[] = {
2234 &dev_attr_control.attr,
2235 &dev_attr_active.attr,
2239 static const struct attribute_group cpuhp_smt_attr_group = {
2240 .attrs = cpuhp_smt_attrs,
2245 static int __init cpu_smt_state_init(void)
2247 return sysfs_create_group(&cpu_subsys.dev_root->kobj,
2248 &cpuhp_smt_attr_group);
2252 static inline int cpu_smt_state_init(void) { return 0; }
2255 static int __init cpuhp_sysfs_init(void)
2259 ret = cpu_smt_state_init();
2263 ret = sysfs_create_group(&cpu_subsys.dev_root->kobj,
2264 &cpuhp_cpu_root_attr_group);
2268 for_each_possible_cpu(cpu) {
2269 struct device *dev = get_cpu_device(cpu);
2273 ret = sysfs_create_group(&dev->kobj, &cpuhp_cpu_attr_group);
2279 device_initcall(cpuhp_sysfs_init);
2283 * cpu_bit_bitmap[] is a special, "compressed" data structure that
2284 * represents all NR_CPUS bits binary values of 1<<nr.
2286 * It is used by cpumask_of() to get a constant address to a CPU
2287 * mask value that has a single bit set only.
2290 /* cpu_bit_bitmap[0] is empty - so we can back into it */
2291 #define MASK_DECLARE_1(x) [x+1][0] = (1UL << (x))
2292 #define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
2293 #define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
2294 #define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
2296 const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
2298 MASK_DECLARE_8(0), MASK_DECLARE_8(8),
2299 MASK_DECLARE_8(16), MASK_DECLARE_8(24),
2300 #if BITS_PER_LONG > 32
2301 MASK_DECLARE_8(32), MASK_DECLARE_8(40),
2302 MASK_DECLARE_8(48), MASK_DECLARE_8(56),
2305 EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
2307 const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
2308 EXPORT_SYMBOL(cpu_all_bits);
2310 #ifdef CONFIG_INIT_ALL_POSSIBLE
2311 struct cpumask __cpu_possible_mask __read_mostly
2314 struct cpumask __cpu_possible_mask __read_mostly;
2316 EXPORT_SYMBOL(__cpu_possible_mask);
2318 struct cpumask __cpu_online_mask __read_mostly;
2319 EXPORT_SYMBOL(__cpu_online_mask);
2321 struct cpumask __cpu_present_mask __read_mostly;
2322 EXPORT_SYMBOL(__cpu_present_mask);
2324 struct cpumask __cpu_active_mask __read_mostly;
2325 EXPORT_SYMBOL(__cpu_active_mask);
2327 void init_cpu_present(const struct cpumask *src)
2329 cpumask_copy(&__cpu_present_mask, src);
2332 void init_cpu_possible(const struct cpumask *src)
2334 cpumask_copy(&__cpu_possible_mask, src);
2337 void init_cpu_online(const struct cpumask *src)
2339 cpumask_copy(&__cpu_online_mask, src);
2343 * Activate the first processor.
2345 void __init boot_cpu_init(void)
2347 int cpu = smp_processor_id();
2349 /* Mark the boot cpu "present", "online" etc for SMP and UP case */
2350 set_cpu_online(cpu, true);
2351 set_cpu_active(cpu, true);
2352 set_cpu_present(cpu, true);
2353 set_cpu_possible(cpu, true);
2356 __boot_cpu_id = cpu;
2361 * Must be called _AFTER_ setting up the per_cpu areas
2363 void __init boot_cpu_hotplug_init(void)
2366 this_cpu_write(cpuhp_state.booted_once, true);
2368 this_cpu_write(cpuhp_state.state, CPUHP_ONLINE);
2372 * These are used for a global "mitigations=" cmdline option for toggling
2373 * optional CPU mitigations.
2375 enum cpu_mitigations {
2376 CPU_MITIGATIONS_OFF,
2377 CPU_MITIGATIONS_AUTO,
2378 CPU_MITIGATIONS_AUTO_NOSMT,
2381 static enum cpu_mitigations cpu_mitigations __ro_after_init =
2382 CPU_MITIGATIONS_AUTO;
2384 static int __init mitigations_parse_cmdline(char *arg)
2386 if (!strcmp(arg, "off"))
2387 cpu_mitigations = CPU_MITIGATIONS_OFF;
2388 else if (!strcmp(arg, "auto"))
2389 cpu_mitigations = CPU_MITIGATIONS_AUTO;
2390 else if (!strcmp(arg, "auto,nosmt"))
2391 cpu_mitigations = CPU_MITIGATIONS_AUTO_NOSMT;
2393 pr_crit("Unsupported mitigations=%s, system may still be vulnerable\n",
2398 early_param("mitigations", mitigations_parse_cmdline);
2400 /* mitigations=off */
2401 bool cpu_mitigations_off(void)
2403 return cpu_mitigations == CPU_MITIGATIONS_OFF;
2405 EXPORT_SYMBOL_GPL(cpu_mitigations_off);
2407 /* mitigations=auto,nosmt */
2408 bool cpu_mitigations_auto_nosmt(void)
2410 return cpu_mitigations == CPU_MITIGATIONS_AUTO_NOSMT;
2412 EXPORT_SYMBOL_GPL(cpu_mitigations_auto_nosmt);