2 * (C) 2001, 2002, 2003, 2004 Rusty Russell
4 * This code is licenced under the GPL.
6 #include <linux/sched/mm.h>
7 #include <linux/proc_fs.h>
9 #include <linux/init.h>
10 #include <linux/notifier.h>
11 #include <linux/sched/signal.h>
12 #include <linux/sched/hotplug.h>
13 #include <linux/sched/isolation.h>
14 #include <linux/sched/task.h>
15 #include <linux/sched/smt.h>
16 #include <linux/unistd.h>
17 #include <linux/cpu.h>
18 #include <linux/oom.h>
19 #include <linux/rcupdate.h>
20 #include <linux/export.h>
21 #include <linux/bug.h>
22 #include <linux/kthread.h>
23 #include <linux/stop_machine.h>
24 #include <linux/mutex.h>
25 #include <linux/gfp.h>
26 #include <linux/suspend.h>
27 #include <linux/lockdep.h>
28 #include <linux/tick.h>
29 #include <linux/irq.h>
30 #include <linux/nmi.h>
31 #include <linux/smpboot.h>
32 #include <linux/relay.h>
33 #include <linux/slab.h>
34 #include <linux/percpu-rwsem.h>
35 #include <linux/cpuset.h>
36 #include <linux/random.h>
38 #include <trace/events/power.h>
39 #define CREATE_TRACE_POINTS
40 #include <trace/events/cpuhp.h>
45 * cpuhp_cpu_state - Per cpu hotplug state storage
46 * @state: The current cpu state
47 * @target: The target state
48 * @thread: Pointer to the hotplug thread
49 * @should_run: Thread should execute
50 * @rollback: Perform a rollback
51 * @single: Single callback invocation
52 * @bringup: Single callback bringup or teardown selector
53 * @cb_state: The state for a single callback (install/uninstall)
54 * @result: Result of the operation
55 * @done_up: Signal completion to the issuer of the task for cpu-up
56 * @done_down: Signal completion to the issuer of the task for cpu-down
58 struct cpuhp_cpu_state {
59 enum cpuhp_state state;
60 enum cpuhp_state target;
61 enum cpuhp_state fail;
63 struct task_struct *thread;
68 struct hlist_node *node;
69 struct hlist_node *last;
70 enum cpuhp_state cb_state;
72 struct completion done_up;
73 struct completion done_down;
77 static DEFINE_PER_CPU(struct cpuhp_cpu_state, cpuhp_state) = {
78 .fail = CPUHP_INVALID,
82 cpumask_t cpus_booted_once_mask;
85 #if defined(CONFIG_LOCKDEP) && defined(CONFIG_SMP)
86 static struct lockdep_map cpuhp_state_up_map =
87 STATIC_LOCKDEP_MAP_INIT("cpuhp_state-up", &cpuhp_state_up_map);
88 static struct lockdep_map cpuhp_state_down_map =
89 STATIC_LOCKDEP_MAP_INIT("cpuhp_state-down", &cpuhp_state_down_map);
92 static inline void cpuhp_lock_acquire(bool bringup)
94 lock_map_acquire(bringup ? &cpuhp_state_up_map : &cpuhp_state_down_map);
97 static inline void cpuhp_lock_release(bool bringup)
99 lock_map_release(bringup ? &cpuhp_state_up_map : &cpuhp_state_down_map);
103 static inline void cpuhp_lock_acquire(bool bringup) { }
104 static inline void cpuhp_lock_release(bool bringup) { }
109 * cpuhp_step - Hotplug state machine step
110 * @name: Name of the step
111 * @startup: Startup function of the step
112 * @teardown: Teardown function of the step
113 * @cant_stop: Bringup/teardown can't be stopped at this step
118 int (*single)(unsigned int cpu);
119 int (*multi)(unsigned int cpu,
120 struct hlist_node *node);
123 int (*single)(unsigned int cpu);
124 int (*multi)(unsigned int cpu,
125 struct hlist_node *node);
127 struct hlist_head list;
132 static DEFINE_MUTEX(cpuhp_state_mutex);
133 static struct cpuhp_step cpuhp_hp_states[];
135 static struct cpuhp_step *cpuhp_get_step(enum cpuhp_state state)
137 return cpuhp_hp_states + state;
141 * cpuhp_invoke_callback _ Invoke the callbacks for a given state
142 * @cpu: The cpu for which the callback should be invoked
143 * @state: The state to do callbacks for
144 * @bringup: True if the bringup callback should be invoked
145 * @node: For multi-instance, do a single entry callback for install/remove
146 * @lastp: For multi-instance rollback, remember how far we got
148 * Called from cpu hotplug and from the state register machinery.
150 static int cpuhp_invoke_callback(unsigned int cpu, enum cpuhp_state state,
151 bool bringup, struct hlist_node *node,
152 struct hlist_node **lastp)
154 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
155 struct cpuhp_step *step = cpuhp_get_step(state);
156 int (*cbm)(unsigned int cpu, struct hlist_node *node);
157 int (*cb)(unsigned int cpu);
160 if (st->fail == state) {
161 st->fail = CPUHP_INVALID;
163 if (!(bringup ? step->startup.single : step->teardown.single))
169 if (!step->multi_instance) {
170 WARN_ON_ONCE(lastp && *lastp);
171 cb = bringup ? step->startup.single : step->teardown.single;
174 trace_cpuhp_enter(cpu, st->target, state, cb);
176 trace_cpuhp_exit(cpu, st->state, state, ret);
179 cbm = bringup ? step->startup.multi : step->teardown.multi;
183 /* Single invocation for instance add/remove */
185 WARN_ON_ONCE(lastp && *lastp);
186 trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
187 ret = cbm(cpu, node);
188 trace_cpuhp_exit(cpu, st->state, state, ret);
192 /* State transition. Invoke on all instances */
194 hlist_for_each(node, &step->list) {
195 if (lastp && node == *lastp)
198 trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
199 ret = cbm(cpu, node);
200 trace_cpuhp_exit(cpu, st->state, state, ret);
214 /* Rollback the instances if one failed */
215 cbm = !bringup ? step->startup.multi : step->teardown.multi;
219 hlist_for_each(node, &step->list) {
223 trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
224 ret = cbm(cpu, node);
225 trace_cpuhp_exit(cpu, st->state, state, ret);
227 * Rollback must not fail,
235 static bool cpuhp_is_ap_state(enum cpuhp_state state)
238 * The extra check for CPUHP_TEARDOWN_CPU is only for documentation
239 * purposes as that state is handled explicitly in cpu_down.
241 return state > CPUHP_BRINGUP_CPU && state != CPUHP_TEARDOWN_CPU;
244 static inline void wait_for_ap_thread(struct cpuhp_cpu_state *st, bool bringup)
246 struct completion *done = bringup ? &st->done_up : &st->done_down;
247 wait_for_completion(done);
250 static inline void complete_ap_thread(struct cpuhp_cpu_state *st, bool bringup)
252 struct completion *done = bringup ? &st->done_up : &st->done_down;
257 * The former STARTING/DYING states, ran with IRQs disabled and must not fail.
259 static bool cpuhp_is_atomic_state(enum cpuhp_state state)
261 return CPUHP_AP_IDLE_DEAD <= state && state < CPUHP_AP_ONLINE;
264 /* Serializes the updates to cpu_online_mask, cpu_present_mask */
265 static DEFINE_MUTEX(cpu_add_remove_lock);
266 bool cpuhp_tasks_frozen;
267 EXPORT_SYMBOL_GPL(cpuhp_tasks_frozen);
270 * The following two APIs (cpu_maps_update_begin/done) must be used when
271 * attempting to serialize the updates to cpu_online_mask & cpu_present_mask.
273 void cpu_maps_update_begin(void)
275 mutex_lock(&cpu_add_remove_lock);
278 void cpu_maps_update_done(void)
280 mutex_unlock(&cpu_add_remove_lock);
284 * If set, cpu_up and cpu_down will return -EBUSY and do nothing.
285 * Should always be manipulated under cpu_add_remove_lock
287 static int cpu_hotplug_disabled;
289 #ifdef CONFIG_HOTPLUG_CPU
291 DEFINE_STATIC_PERCPU_RWSEM(cpu_hotplug_lock);
293 void cpus_read_lock(void)
295 percpu_down_read(&cpu_hotplug_lock);
297 EXPORT_SYMBOL_GPL(cpus_read_lock);
299 int cpus_read_trylock(void)
301 return percpu_down_read_trylock(&cpu_hotplug_lock);
303 EXPORT_SYMBOL_GPL(cpus_read_trylock);
305 void cpus_read_unlock(void)
307 percpu_up_read(&cpu_hotplug_lock);
309 EXPORT_SYMBOL_GPL(cpus_read_unlock);
311 void cpus_write_lock(void)
313 percpu_down_write(&cpu_hotplug_lock);
316 void cpus_write_unlock(void)
318 percpu_up_write(&cpu_hotplug_lock);
321 void lockdep_assert_cpus_held(void)
324 * We can't have hotplug operations before userspace starts running,
325 * and some init codepaths will knowingly not take the hotplug lock.
326 * This is all valid, so mute lockdep until it makes sense to report
329 if (system_state < SYSTEM_RUNNING)
332 percpu_rwsem_assert_held(&cpu_hotplug_lock);
335 static void lockdep_acquire_cpus_lock(void)
337 rwsem_acquire(&cpu_hotplug_lock.rw_sem.dep_map, 0, 0, _THIS_IP_);
340 static void lockdep_release_cpus_lock(void)
342 rwsem_release(&cpu_hotplug_lock.rw_sem.dep_map, 1, _THIS_IP_);
346 * Wait for currently running CPU hotplug operations to complete (if any) and
347 * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
348 * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the
349 * hotplug path before performing hotplug operations. So acquiring that lock
350 * guarantees mutual exclusion from any currently running hotplug operations.
352 void cpu_hotplug_disable(void)
354 cpu_maps_update_begin();
355 cpu_hotplug_disabled++;
356 cpu_maps_update_done();
358 EXPORT_SYMBOL_GPL(cpu_hotplug_disable);
360 static void __cpu_hotplug_enable(void)
362 if (WARN_ONCE(!cpu_hotplug_disabled, "Unbalanced cpu hotplug enable\n"))
364 cpu_hotplug_disabled--;
367 void cpu_hotplug_enable(void)
369 cpu_maps_update_begin();
370 __cpu_hotplug_enable();
371 cpu_maps_update_done();
373 EXPORT_SYMBOL_GPL(cpu_hotplug_enable);
377 static void lockdep_acquire_cpus_lock(void)
381 static void lockdep_release_cpus_lock(void)
385 #endif /* CONFIG_HOTPLUG_CPU */
388 * Architectures that need SMT-specific errata handling during SMT hotplug
389 * should override this.
391 void __weak arch_smt_update(void) { }
393 #ifdef CONFIG_HOTPLUG_SMT
394 enum cpuhp_smt_control cpu_smt_control __read_mostly = CPU_SMT_ENABLED;
396 void __init cpu_smt_disable(bool force)
398 if (!cpu_smt_possible())
402 pr_info("SMT: Force disabled\n");
403 cpu_smt_control = CPU_SMT_FORCE_DISABLED;
405 pr_info("SMT: disabled\n");
406 cpu_smt_control = CPU_SMT_DISABLED;
411 * The decision whether SMT is supported can only be done after the full
412 * CPU identification. Called from architecture code.
414 void __init cpu_smt_check_topology(void)
416 if (!topology_smt_supported())
417 cpu_smt_control = CPU_SMT_NOT_SUPPORTED;
420 static int __init smt_cmdline_disable(char *str)
422 cpu_smt_disable(str && !strcmp(str, "force"));
425 early_param("nosmt", smt_cmdline_disable);
427 static inline bool cpu_smt_allowed(unsigned int cpu)
429 if (cpu_smt_control == CPU_SMT_ENABLED)
432 if (topology_is_primary_thread(cpu))
436 * On x86 it's required to boot all logical CPUs at least once so
437 * that the init code can get a chance to set CR4.MCE on each
438 * CPU. Otherwise, a broadacasted MCE observing CR4.MCE=0b on any
439 * core will shutdown the machine.
441 return !cpumask_test_cpu(cpu, &cpus_booted_once_mask);
444 /* Returns true if SMT is not supported of forcefully (irreversibly) disabled */
445 bool cpu_smt_possible(void)
447 return cpu_smt_control != CPU_SMT_FORCE_DISABLED &&
448 cpu_smt_control != CPU_SMT_NOT_SUPPORTED;
450 EXPORT_SYMBOL_GPL(cpu_smt_possible);
452 static inline bool cpu_smt_allowed(unsigned int cpu) { return true; }
455 static inline enum cpuhp_state
456 cpuhp_set_state(struct cpuhp_cpu_state *st, enum cpuhp_state target)
458 enum cpuhp_state prev_state = st->state;
460 st->rollback = false;
465 st->bringup = st->state < target;
471 cpuhp_reset_state(struct cpuhp_cpu_state *st, enum cpuhp_state prev_state)
476 * If we have st->last we need to undo partial multi_instance of this
477 * state first. Otherwise start undo at the previous state.
486 st->target = prev_state;
487 st->bringup = !st->bringup;
490 /* Regular hotplug invocation of the AP hotplug thread */
491 static void __cpuhp_kick_ap(struct cpuhp_cpu_state *st)
493 if (!st->single && st->state == st->target)
498 * Make sure the above stores are visible before should_run becomes
499 * true. Paired with the mb() above in cpuhp_thread_fun()
502 st->should_run = true;
503 wake_up_process(st->thread);
504 wait_for_ap_thread(st, st->bringup);
507 static int cpuhp_kick_ap(struct cpuhp_cpu_state *st, enum cpuhp_state target)
509 enum cpuhp_state prev_state;
512 prev_state = cpuhp_set_state(st, target);
514 if ((ret = st->result)) {
515 cpuhp_reset_state(st, prev_state);
522 static int bringup_wait_for_ap(unsigned int cpu)
524 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
526 /* Wait for the CPU to reach CPUHP_AP_ONLINE_IDLE */
527 wait_for_ap_thread(st, true);
528 if (WARN_ON_ONCE((!cpu_online(cpu))))
531 /* Unpark the hotplug thread of the target cpu */
532 kthread_unpark(st->thread);
535 * SMT soft disabling on X86 requires to bring the CPU out of the
536 * BIOS 'wait for SIPI' state in order to set the CR4.MCE bit. The
537 * CPU marked itself as booted_once in notify_cpu_starting() so the
538 * cpu_smt_allowed() check will now return false if this is not the
541 if (!cpu_smt_allowed(cpu))
544 if (st->target <= CPUHP_AP_ONLINE_IDLE)
547 return cpuhp_kick_ap(st, st->target);
550 static int bringup_cpu(unsigned int cpu)
552 struct task_struct *idle = idle_thread_get(cpu);
556 * Some architectures have to walk the irq descriptors to
557 * setup the vector space for the cpu which comes online.
558 * Prevent irq alloc/free across the bringup.
562 /* Arch-specific enabling code. */
563 ret = __cpu_up(cpu, idle);
567 return bringup_wait_for_ap(cpu);
570 static int finish_cpu(unsigned int cpu)
572 struct task_struct *idle = idle_thread_get(cpu);
573 struct mm_struct *mm = idle->active_mm;
576 * idle_task_exit() will have switched to &init_mm, now
577 * clean up any remaining active_mm state.
580 idle->active_mm = &init_mm;
586 * Hotplug state machine related functions
589 static void undo_cpu_up(unsigned int cpu, struct cpuhp_cpu_state *st)
591 for (st->state--; st->state > st->target; st->state--)
592 cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
595 static inline bool can_rollback_cpu(struct cpuhp_cpu_state *st)
597 if (IS_ENABLED(CONFIG_HOTPLUG_CPU))
600 * When CPU hotplug is disabled, then taking the CPU down is not
601 * possible because takedown_cpu() and the architecture and
602 * subsystem specific mechanisms are not available. So the CPU
603 * which would be completely unplugged again needs to stay around
604 * in the current state.
606 return st->state <= CPUHP_BRINGUP_CPU;
609 static int cpuhp_up_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
610 enum cpuhp_state target)
612 enum cpuhp_state prev_state = st->state;
615 while (st->state < target) {
617 ret = cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
619 if (can_rollback_cpu(st)) {
620 st->target = prev_state;
621 undo_cpu_up(cpu, st);
630 * The cpu hotplug threads manage the bringup and teardown of the cpus
632 static void cpuhp_create(unsigned int cpu)
634 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
636 init_completion(&st->done_up);
637 init_completion(&st->done_down);
640 static int cpuhp_should_run(unsigned int cpu)
642 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
644 return st->should_run;
648 * Execute teardown/startup callbacks on the plugged cpu. Also used to invoke
649 * callbacks when a state gets [un]installed at runtime.
651 * Each invocation of this function by the smpboot thread does a single AP
654 * It has 3 modes of operation:
655 * - single: runs st->cb_state
656 * - up: runs ++st->state, while st->state < st->target
657 * - down: runs st->state--, while st->state > st->target
659 * When complete or on error, should_run is cleared and the completion is fired.
661 static void cpuhp_thread_fun(unsigned int cpu)
663 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
664 bool bringup = st->bringup;
665 enum cpuhp_state state;
667 if (WARN_ON_ONCE(!st->should_run))
671 * ACQUIRE for the cpuhp_should_run() load of ->should_run. Ensures
672 * that if we see ->should_run we also see the rest of the state.
677 * The BP holds the hotplug lock, but we're now running on the AP,
678 * ensure that anybody asserting the lock is held, will actually find
681 lockdep_acquire_cpus_lock();
682 cpuhp_lock_acquire(bringup);
685 state = st->cb_state;
686 st->should_run = false;
691 st->should_run = (st->state < st->target);
692 WARN_ON_ONCE(st->state > st->target);
696 st->should_run = (st->state > st->target);
697 WARN_ON_ONCE(st->state < st->target);
701 WARN_ON_ONCE(!cpuhp_is_ap_state(state));
703 if (cpuhp_is_atomic_state(state)) {
705 st->result = cpuhp_invoke_callback(cpu, state, bringup, st->node, &st->last);
709 * STARTING/DYING must not fail!
711 WARN_ON_ONCE(st->result);
713 st->result = cpuhp_invoke_callback(cpu, state, bringup, st->node, &st->last);
718 * If we fail on a rollback, we're up a creek without no
719 * paddle, no way forward, no way back. We loose, thanks for
722 WARN_ON_ONCE(st->rollback);
723 st->should_run = false;
726 cpuhp_lock_release(bringup);
727 lockdep_release_cpus_lock();
730 complete_ap_thread(st, bringup);
733 /* Invoke a single callback on a remote cpu */
735 cpuhp_invoke_ap_callback(int cpu, enum cpuhp_state state, bool bringup,
736 struct hlist_node *node)
738 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
741 if (!cpu_online(cpu))
744 cpuhp_lock_acquire(false);
745 cpuhp_lock_release(false);
747 cpuhp_lock_acquire(true);
748 cpuhp_lock_release(true);
751 * If we are up and running, use the hotplug thread. For early calls
752 * we invoke the thread function directly.
755 return cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
757 st->rollback = false;
761 st->bringup = bringup;
762 st->cb_state = state;
768 * If we failed and did a partial, do a rollback.
770 if ((ret = st->result) && st->last) {
772 st->bringup = !bringup;
778 * Clean up the leftovers so the next hotplug operation wont use stale
781 st->node = st->last = NULL;
785 static int cpuhp_kick_ap_work(unsigned int cpu)
787 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
788 enum cpuhp_state prev_state = st->state;
791 cpuhp_lock_acquire(false);
792 cpuhp_lock_release(false);
794 cpuhp_lock_acquire(true);
795 cpuhp_lock_release(true);
797 trace_cpuhp_enter(cpu, st->target, prev_state, cpuhp_kick_ap_work);
798 ret = cpuhp_kick_ap(st, st->target);
799 trace_cpuhp_exit(cpu, st->state, prev_state, ret);
804 static struct smp_hotplug_thread cpuhp_threads = {
805 .store = &cpuhp_state.thread,
806 .create = &cpuhp_create,
807 .thread_should_run = cpuhp_should_run,
808 .thread_fn = cpuhp_thread_fun,
809 .thread_comm = "cpuhp/%u",
813 void __init cpuhp_threads_init(void)
815 BUG_ON(smpboot_register_percpu_thread(&cpuhp_threads));
816 kthread_unpark(this_cpu_read(cpuhp_state.thread));
821 * Serialize hotplug trainwrecks outside of the cpu_hotplug_lock
824 * The operation is still serialized against concurrent CPU hotplug via
825 * cpu_add_remove_lock, i.e. CPU map protection. But it is _not_
826 * serialized against other hotplug related activity like adding or
827 * removing of state callbacks and state instances, which invoke either the
828 * startup or the teardown callback of the affected state.
830 * This is required for subsystems which are unfixable vs. CPU hotplug and
831 * evade lock inversion problems by scheduling work which has to be
832 * completed _before_ cpu_up()/_cpu_down() returns.
834 * Don't even think about adding anything to this for any new code or even
835 * drivers. It's only purpose is to keep existing lock order trainwrecks
838 * For cpu_down() there might be valid reasons to finish cleanups which are
839 * not required to be done under cpu_hotplug_lock, but that's a different
840 * story and would be not invoked via this.
842 static void cpu_up_down_serialize_trainwrecks(bool tasks_frozen)
845 * cpusets delegate hotplug operations to a worker to "solve" the
846 * lock order problems. Wait for the worker, but only if tasks are
847 * _not_ frozen (suspend, hibernate) as that would wait forever.
849 * The wait is required because otherwise the hotplug operation
850 * returns with inconsistent state, which could even be observed in
851 * user space when a new CPU is brought up. The CPU plug uevent
852 * would be delivered and user space reacting on it would fail to
853 * move tasks to the newly plugged CPU up to the point where the
854 * work has finished because up to that point the newly plugged CPU
855 * is not assignable in cpusets/cgroups. On unplug that's not
856 * necessarily a visible issue, but it is still inconsistent state,
857 * which is the real problem which needs to be "fixed". This can't
858 * prevent the transient state between scheduling the work and
859 * returning from waiting for it.
862 cpuset_wait_for_hotplug();
865 #ifdef CONFIG_HOTPLUG_CPU
866 #ifndef arch_clear_mm_cpumask_cpu
867 #define arch_clear_mm_cpumask_cpu(cpu, mm) cpumask_clear_cpu(cpu, mm_cpumask(mm))
871 * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU
874 * This function walks all processes, finds a valid mm struct for each one and
875 * then clears a corresponding bit in mm's cpumask. While this all sounds
876 * trivial, there are various non-obvious corner cases, which this function
877 * tries to solve in a safe manner.
879 * Also note that the function uses a somewhat relaxed locking scheme, so it may
880 * be called only for an already offlined CPU.
882 void clear_tasks_mm_cpumask(int cpu)
884 struct task_struct *p;
887 * This function is called after the cpu is taken down and marked
888 * offline, so its not like new tasks will ever get this cpu set in
889 * their mm mask. -- Peter Zijlstra
890 * Thus, we may use rcu_read_lock() here, instead of grabbing
891 * full-fledged tasklist_lock.
893 WARN_ON(cpu_online(cpu));
895 for_each_process(p) {
896 struct task_struct *t;
899 * Main thread might exit, but other threads may still have
900 * a valid mm. Find one.
902 t = find_lock_task_mm(p);
905 arch_clear_mm_cpumask_cpu(cpu, t->mm);
911 /* Take this CPU down. */
912 static int take_cpu_down(void *_param)
914 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
915 enum cpuhp_state target = max((int)st->target, CPUHP_AP_OFFLINE);
916 int err, cpu = smp_processor_id();
919 /* Ensure this CPU doesn't handle any more interrupts. */
920 err = __cpu_disable();
925 * We get here while we are in CPUHP_TEARDOWN_CPU state and we must not
926 * do this step again.
928 WARN_ON(st->state != CPUHP_TEARDOWN_CPU);
930 /* Invoke the former CPU_DYING callbacks */
931 for (; st->state > target; st->state--) {
932 ret = cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
934 * DYING must not fail!
939 /* Give up timekeeping duties */
940 tick_handover_do_timer();
941 /* Remove CPU from timer broadcasting */
942 tick_offline_cpu(cpu);
943 /* Park the stopper thread */
944 stop_machine_park(cpu);
948 static int takedown_cpu(unsigned int cpu)
950 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
953 /* Park the smpboot threads */
954 kthread_park(per_cpu_ptr(&cpuhp_state, cpu)->thread);
957 * Prevent irq alloc/free while the dying cpu reorganizes the
958 * interrupt affinities.
963 * So now all preempt/rcu users must observe !cpu_active().
965 err = stop_machine_cpuslocked(take_cpu_down, NULL, cpumask_of(cpu));
967 /* CPU refused to die */
969 /* Unpark the hotplug thread so we can rollback there */
970 kthread_unpark(per_cpu_ptr(&cpuhp_state, cpu)->thread);
973 BUG_ON(cpu_online(cpu));
976 * The teardown callback for CPUHP_AP_SCHED_STARTING will have removed
977 * all runnable tasks from the CPU, there's only the idle task left now
978 * that the migration thread is done doing the stop_machine thing.
980 * Wait for the stop thread to go away.
982 wait_for_ap_thread(st, false);
983 BUG_ON(st->state != CPUHP_AP_IDLE_DEAD);
985 /* Interrupts are moved away from the dying cpu, reenable alloc/free */
988 hotplug_cpu__broadcast_tick_pull(cpu);
989 /* This actually kills the CPU. */
992 tick_cleanup_dead_cpu(cpu);
993 rcutree_migrate_callbacks(cpu);
997 static void cpuhp_complete_idle_dead(void *arg)
999 struct cpuhp_cpu_state *st = arg;
1001 complete_ap_thread(st, false);
1004 void cpuhp_report_idle_dead(void)
1006 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
1008 BUG_ON(st->state != CPUHP_AP_OFFLINE);
1009 rcu_report_dead(smp_processor_id());
1010 st->state = CPUHP_AP_IDLE_DEAD;
1012 * We cannot call complete after rcu_report_dead() so we delegate it
1015 smp_call_function_single(cpumask_first(cpu_online_mask),
1016 cpuhp_complete_idle_dead, st, 0);
1019 static void undo_cpu_down(unsigned int cpu, struct cpuhp_cpu_state *st)
1021 for (st->state++; st->state < st->target; st->state++)
1022 cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
1025 static int cpuhp_down_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
1026 enum cpuhp_state target)
1028 enum cpuhp_state prev_state = st->state;
1031 for (; st->state > target; st->state--) {
1032 ret = cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
1034 st->target = prev_state;
1035 if (st->state < prev_state)
1036 undo_cpu_down(cpu, st);
1043 /* Requires cpu_add_remove_lock to be held */
1044 static int __ref _cpu_down(unsigned int cpu, int tasks_frozen,
1045 enum cpuhp_state target)
1047 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1048 int prev_state, ret = 0;
1050 if (num_online_cpus() == 1)
1053 if (!cpu_present(cpu))
1058 cpuhp_tasks_frozen = tasks_frozen;
1060 prev_state = cpuhp_set_state(st, target);
1062 * If the current CPU state is in the range of the AP hotplug thread,
1063 * then we need to kick the thread.
1065 if (st->state > CPUHP_TEARDOWN_CPU) {
1066 st->target = max((int)target, CPUHP_TEARDOWN_CPU);
1067 ret = cpuhp_kick_ap_work(cpu);
1069 * The AP side has done the error rollback already. Just
1070 * return the error code..
1076 * We might have stopped still in the range of the AP hotplug
1077 * thread. Nothing to do anymore.
1079 if (st->state > CPUHP_TEARDOWN_CPU)
1082 st->target = target;
1085 * The AP brought itself down to CPUHP_TEARDOWN_CPU. So we need
1086 * to do the further cleanups.
1088 ret = cpuhp_down_callbacks(cpu, st, target);
1089 if (ret && st->state == CPUHP_TEARDOWN_CPU && st->state < prev_state) {
1090 cpuhp_reset_state(st, prev_state);
1091 __cpuhp_kick_ap(st);
1095 cpus_write_unlock();
1097 * Do post unplug cleanup. This is still protected against
1098 * concurrent CPU hotplug via cpu_add_remove_lock.
1100 lockup_detector_cleanup();
1102 cpu_up_down_serialize_trainwrecks(tasks_frozen);
1106 static int cpu_down_maps_locked(unsigned int cpu, enum cpuhp_state target)
1108 if (cpu_hotplug_disabled)
1110 return _cpu_down(cpu, 0, target);
1113 static int do_cpu_down(unsigned int cpu, enum cpuhp_state target)
1117 cpu_maps_update_begin();
1118 err = cpu_down_maps_locked(cpu, target);
1119 cpu_maps_update_done();
1123 int cpu_down(unsigned int cpu)
1125 return do_cpu_down(cpu, CPUHP_OFFLINE);
1127 EXPORT_SYMBOL(cpu_down);
1130 #define takedown_cpu NULL
1131 #endif /*CONFIG_HOTPLUG_CPU*/
1134 * notify_cpu_starting(cpu) - Invoke the callbacks on the starting CPU
1135 * @cpu: cpu that just started
1137 * It must be called by the arch code on the new cpu, before the new cpu
1138 * enables interrupts and before the "boot" cpu returns from __cpu_up().
1140 void notify_cpu_starting(unsigned int cpu)
1142 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1143 enum cpuhp_state target = min((int)st->target, CPUHP_AP_ONLINE);
1146 rcu_cpu_starting(cpu); /* Enables RCU usage on this CPU. */
1147 cpumask_set_cpu(cpu, &cpus_booted_once_mask);
1148 while (st->state < target) {
1150 ret = cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
1152 * STARTING must not fail!
1159 * Called from the idle task. Wake up the controlling task which brings the
1160 * hotplug thread of the upcoming CPU up and then delegates the rest of the
1161 * online bringup to the hotplug thread.
1163 void cpuhp_online_idle(enum cpuhp_state state)
1165 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
1167 /* Happens for the boot cpu */
1168 if (state != CPUHP_AP_ONLINE_IDLE)
1172 * Unpart the stopper thread before we start the idle loop (and start
1173 * scheduling); this ensures the stopper task is always available.
1175 stop_machine_unpark(smp_processor_id());
1177 st->state = CPUHP_AP_ONLINE_IDLE;
1178 complete_ap_thread(st, true);
1181 /* Requires cpu_add_remove_lock to be held */
1182 static int _cpu_up(unsigned int cpu, int tasks_frozen, enum cpuhp_state target)
1184 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1185 struct task_struct *idle;
1190 if (!cpu_present(cpu)) {
1196 * The caller of do_cpu_up might have raced with another
1197 * caller. Ignore it for now.
1199 if (st->state >= target)
1202 if (st->state == CPUHP_OFFLINE) {
1203 /* Let it fail before we try to bring the cpu up */
1204 idle = idle_thread_get(cpu);
1206 ret = PTR_ERR(idle);
1211 cpuhp_tasks_frozen = tasks_frozen;
1213 cpuhp_set_state(st, target);
1215 * If the current CPU state is in the range of the AP hotplug thread,
1216 * then we need to kick the thread once more.
1218 if (st->state > CPUHP_BRINGUP_CPU) {
1219 ret = cpuhp_kick_ap_work(cpu);
1221 * The AP side has done the error rollback already. Just
1222 * return the error code..
1229 * Try to reach the target state. We max out on the BP at
1230 * CPUHP_BRINGUP_CPU. After that the AP hotplug thread is
1231 * responsible for bringing it up to the target state.
1233 target = min((int)target, CPUHP_BRINGUP_CPU);
1234 ret = cpuhp_up_callbacks(cpu, st, target);
1236 cpus_write_unlock();
1238 cpu_up_down_serialize_trainwrecks(tasks_frozen);
1242 static int do_cpu_up(unsigned int cpu, enum cpuhp_state target)
1246 if (!cpu_possible(cpu)) {
1247 pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n",
1249 #if defined(CONFIG_IA64)
1250 pr_err("please check additional_cpus= boot parameter\n");
1255 err = try_online_node(cpu_to_node(cpu));
1259 cpu_maps_update_begin();
1261 if (cpu_hotplug_disabled) {
1265 if (!cpu_smt_allowed(cpu)) {
1270 err = _cpu_up(cpu, 0, target);
1272 cpu_maps_update_done();
1276 int cpu_up(unsigned int cpu)
1278 return do_cpu_up(cpu, CPUHP_ONLINE);
1280 EXPORT_SYMBOL_GPL(cpu_up);
1282 #ifdef CONFIG_PM_SLEEP_SMP
1283 static cpumask_var_t frozen_cpus;
1285 int __freeze_secondary_cpus(int primary, bool suspend)
1289 cpu_maps_update_begin();
1290 if (primary == -1) {
1291 primary = cpumask_first(cpu_online_mask);
1292 if (!housekeeping_cpu(primary, HK_FLAG_TIMER))
1293 primary = housekeeping_any_cpu(HK_FLAG_TIMER);
1295 if (!cpu_online(primary))
1296 primary = cpumask_first(cpu_online_mask);
1300 * We take down all of the non-boot CPUs in one shot to avoid races
1301 * with the userspace trying to use the CPU hotplug at the same time
1303 cpumask_clear(frozen_cpus);
1305 pr_info("Disabling non-boot CPUs ...\n");
1306 for_each_online_cpu(cpu) {
1310 if (suspend && pm_wakeup_pending()) {
1311 pr_info("Wakeup pending. Abort CPU freeze\n");
1316 trace_suspend_resume(TPS("CPU_OFF"), cpu, true);
1317 error = _cpu_down(cpu, 1, CPUHP_OFFLINE);
1318 trace_suspend_resume(TPS("CPU_OFF"), cpu, false);
1320 cpumask_set_cpu(cpu, frozen_cpus);
1322 pr_err("Error taking CPU%d down: %d\n", cpu, error);
1328 BUG_ON(num_online_cpus() > 1);
1330 pr_err("Non-boot CPUs are not disabled\n");
1333 * Make sure the CPUs won't be enabled by someone else. We need to do
1334 * this even in case of failure as all disable_nonboot_cpus() users are
1335 * supposed to do enable_nonboot_cpus() on the failure path.
1337 cpu_hotplug_disabled++;
1339 cpu_maps_update_done();
1343 void __weak arch_enable_nonboot_cpus_begin(void)
1347 void __weak arch_enable_nonboot_cpus_end(void)
1351 void enable_nonboot_cpus(void)
1355 /* Allow everyone to use the CPU hotplug again */
1356 cpu_maps_update_begin();
1357 __cpu_hotplug_enable();
1358 if (cpumask_empty(frozen_cpus))
1361 pr_info("Enabling non-boot CPUs ...\n");
1363 arch_enable_nonboot_cpus_begin();
1365 for_each_cpu(cpu, frozen_cpus) {
1366 trace_suspend_resume(TPS("CPU_ON"), cpu, true);
1367 error = _cpu_up(cpu, 1, CPUHP_ONLINE);
1368 trace_suspend_resume(TPS("CPU_ON"), cpu, false);
1370 pr_info("CPU%d is up\n", cpu);
1373 pr_warn("Error taking CPU%d up: %d\n", cpu, error);
1376 arch_enable_nonboot_cpus_end();
1378 cpumask_clear(frozen_cpus);
1380 cpu_maps_update_done();
1383 static int __init alloc_frozen_cpus(void)
1385 if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
1389 core_initcall(alloc_frozen_cpus);
1392 * When callbacks for CPU hotplug notifications are being executed, we must
1393 * ensure that the state of the system with respect to the tasks being frozen
1394 * or not, as reported by the notification, remains unchanged *throughout the
1395 * duration* of the execution of the callbacks.
1396 * Hence we need to prevent the freezer from racing with regular CPU hotplug.
1398 * This synchronization is implemented by mutually excluding regular CPU
1399 * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
1400 * Hibernate notifications.
1403 cpu_hotplug_pm_callback(struct notifier_block *nb,
1404 unsigned long action, void *ptr)
1408 case PM_SUSPEND_PREPARE:
1409 case PM_HIBERNATION_PREPARE:
1410 cpu_hotplug_disable();
1413 case PM_POST_SUSPEND:
1414 case PM_POST_HIBERNATION:
1415 cpu_hotplug_enable();
1426 static int __init cpu_hotplug_pm_sync_init(void)
1429 * cpu_hotplug_pm_callback has higher priority than x86
1430 * bsp_pm_callback which depends on cpu_hotplug_pm_callback
1431 * to disable cpu hotplug to avoid cpu hotplug race.
1433 pm_notifier(cpu_hotplug_pm_callback, 0);
1436 core_initcall(cpu_hotplug_pm_sync_init);
1438 #endif /* CONFIG_PM_SLEEP_SMP */
1442 #endif /* CONFIG_SMP */
1444 /* Boot processor state steps */
1445 static struct cpuhp_step cpuhp_hp_states[] = {
1448 .startup.single = NULL,
1449 .teardown.single = NULL,
1452 [CPUHP_CREATE_THREADS]= {
1453 .name = "threads:prepare",
1454 .startup.single = smpboot_create_threads,
1455 .teardown.single = NULL,
1458 [CPUHP_PERF_PREPARE] = {
1459 .name = "perf:prepare",
1460 .startup.single = perf_event_init_cpu,
1461 .teardown.single = perf_event_exit_cpu,
1463 [CPUHP_RANDOM_PREPARE] = {
1464 .name = "random:prepare",
1465 .startup.single = random_prepare_cpu,
1466 .teardown.single = NULL,
1468 [CPUHP_WORKQUEUE_PREP] = {
1469 .name = "workqueue:prepare",
1470 .startup.single = workqueue_prepare_cpu,
1471 .teardown.single = NULL,
1473 [CPUHP_HRTIMERS_PREPARE] = {
1474 .name = "hrtimers:prepare",
1475 .startup.single = hrtimers_prepare_cpu,
1476 .teardown.single = hrtimers_dead_cpu,
1478 [CPUHP_SMPCFD_PREPARE] = {
1479 .name = "smpcfd:prepare",
1480 .startup.single = smpcfd_prepare_cpu,
1481 .teardown.single = smpcfd_dead_cpu,
1483 [CPUHP_RELAY_PREPARE] = {
1484 .name = "relay:prepare",
1485 .startup.single = relay_prepare_cpu,
1486 .teardown.single = NULL,
1488 [CPUHP_SLAB_PREPARE] = {
1489 .name = "slab:prepare",
1490 .startup.single = slab_prepare_cpu,
1491 .teardown.single = slab_dead_cpu,
1493 [CPUHP_RCUTREE_PREP] = {
1494 .name = "RCU/tree:prepare",
1495 .startup.single = rcutree_prepare_cpu,
1496 .teardown.single = rcutree_dead_cpu,
1499 * On the tear-down path, timers_dead_cpu() must be invoked
1500 * before blk_mq_queue_reinit_notify() from notify_dead(),
1501 * otherwise a RCU stall occurs.
1503 [CPUHP_TIMERS_PREPARE] = {
1504 .name = "timers:prepare",
1505 .startup.single = timers_prepare_cpu,
1506 .teardown.single = timers_dead_cpu,
1508 /* Kicks the plugged cpu into life */
1509 [CPUHP_BRINGUP_CPU] = {
1510 .name = "cpu:bringup",
1511 .startup.single = bringup_cpu,
1512 .teardown.single = finish_cpu,
1515 /* Final state before CPU kills itself */
1516 [CPUHP_AP_IDLE_DEAD] = {
1517 .name = "idle:dead",
1520 * Last state before CPU enters the idle loop to die. Transient state
1521 * for synchronization.
1523 [CPUHP_AP_OFFLINE] = {
1524 .name = "ap:offline",
1527 /* First state is scheduler control. Interrupts are disabled */
1528 [CPUHP_AP_SCHED_STARTING] = {
1529 .name = "sched:starting",
1530 .startup.single = sched_cpu_starting,
1531 .teardown.single = sched_cpu_dying,
1533 [CPUHP_AP_RCUTREE_DYING] = {
1534 .name = "RCU/tree:dying",
1535 .startup.single = NULL,
1536 .teardown.single = rcutree_dying_cpu,
1538 [CPUHP_AP_SMPCFD_DYING] = {
1539 .name = "smpcfd:dying",
1540 .startup.single = NULL,
1541 .teardown.single = smpcfd_dying_cpu,
1543 /* Entry state on starting. Interrupts enabled from here on. Transient
1544 * state for synchronsization */
1545 [CPUHP_AP_ONLINE] = {
1546 .name = "ap:online",
1549 * Handled on controll processor until the plugged processor manages
1552 [CPUHP_TEARDOWN_CPU] = {
1553 .name = "cpu:teardown",
1554 .startup.single = NULL,
1555 .teardown.single = takedown_cpu,
1558 /* Handle smpboot threads park/unpark */
1559 [CPUHP_AP_SMPBOOT_THREADS] = {
1560 .name = "smpboot/threads:online",
1561 .startup.single = smpboot_unpark_threads,
1562 .teardown.single = smpboot_park_threads,
1564 [CPUHP_AP_IRQ_AFFINITY_ONLINE] = {
1565 .name = "irq/affinity:online",
1566 .startup.single = irq_affinity_online_cpu,
1567 .teardown.single = NULL,
1569 [CPUHP_AP_PERF_ONLINE] = {
1570 .name = "perf:online",
1571 .startup.single = perf_event_init_cpu,
1572 .teardown.single = perf_event_exit_cpu,
1574 [CPUHP_AP_WATCHDOG_ONLINE] = {
1575 .name = "lockup_detector:online",
1576 .startup.single = lockup_detector_online_cpu,
1577 .teardown.single = lockup_detector_offline_cpu,
1579 [CPUHP_AP_WORKQUEUE_ONLINE] = {
1580 .name = "workqueue:online",
1581 .startup.single = workqueue_online_cpu,
1582 .teardown.single = workqueue_offline_cpu,
1584 [CPUHP_AP_RANDOM_ONLINE] = {
1585 .name = "random:online",
1586 .startup.single = random_online_cpu,
1587 .teardown.single = NULL,
1589 [CPUHP_AP_RCUTREE_ONLINE] = {
1590 .name = "RCU/tree:online",
1591 .startup.single = rcutree_online_cpu,
1592 .teardown.single = rcutree_offline_cpu,
1596 * The dynamically registered state space is here
1600 /* Last state is scheduler control setting the cpu active */
1601 [CPUHP_AP_ACTIVE] = {
1602 .name = "sched:active",
1603 .startup.single = sched_cpu_activate,
1604 .teardown.single = sched_cpu_deactivate,
1608 /* CPU is fully up and running. */
1611 .startup.single = NULL,
1612 .teardown.single = NULL,
1616 /* Sanity check for callbacks */
1617 static int cpuhp_cb_check(enum cpuhp_state state)
1619 if (state <= CPUHP_OFFLINE || state >= CPUHP_ONLINE)
1625 * Returns a free for dynamic slot assignment of the Online state. The states
1626 * are protected by the cpuhp_slot_states mutex and an empty slot is identified
1627 * by having no name assigned.
1629 static int cpuhp_reserve_state(enum cpuhp_state state)
1631 enum cpuhp_state i, end;
1632 struct cpuhp_step *step;
1635 case CPUHP_AP_ONLINE_DYN:
1636 step = cpuhp_hp_states + CPUHP_AP_ONLINE_DYN;
1637 end = CPUHP_AP_ONLINE_DYN_END;
1639 case CPUHP_BP_PREPARE_DYN:
1640 step = cpuhp_hp_states + CPUHP_BP_PREPARE_DYN;
1641 end = CPUHP_BP_PREPARE_DYN_END;
1647 for (i = state; i <= end; i++, step++) {
1651 WARN(1, "No more dynamic states available for CPU hotplug\n");
1655 static int cpuhp_store_callbacks(enum cpuhp_state state, const char *name,
1656 int (*startup)(unsigned int cpu),
1657 int (*teardown)(unsigned int cpu),
1658 bool multi_instance)
1660 /* (Un)Install the callbacks for further cpu hotplug operations */
1661 struct cpuhp_step *sp;
1665 * If name is NULL, then the state gets removed.
1667 * CPUHP_AP_ONLINE_DYN and CPUHP_BP_PREPARE_DYN are handed out on
1668 * the first allocation from these dynamic ranges, so the removal
1669 * would trigger a new allocation and clear the wrong (already
1670 * empty) state, leaving the callbacks of the to be cleared state
1671 * dangling, which causes wreckage on the next hotplug operation.
1673 if (name && (state == CPUHP_AP_ONLINE_DYN ||
1674 state == CPUHP_BP_PREPARE_DYN)) {
1675 ret = cpuhp_reserve_state(state);
1680 sp = cpuhp_get_step(state);
1681 if (name && sp->name)
1684 sp->startup.single = startup;
1685 sp->teardown.single = teardown;
1687 sp->multi_instance = multi_instance;
1688 INIT_HLIST_HEAD(&sp->list);
1692 static void *cpuhp_get_teardown_cb(enum cpuhp_state state)
1694 return cpuhp_get_step(state)->teardown.single;
1698 * Call the startup/teardown function for a step either on the AP or
1699 * on the current CPU.
1701 static int cpuhp_issue_call(int cpu, enum cpuhp_state state, bool bringup,
1702 struct hlist_node *node)
1704 struct cpuhp_step *sp = cpuhp_get_step(state);
1708 * If there's nothing to do, we done.
1709 * Relies on the union for multi_instance.
1711 if ((bringup && !sp->startup.single) ||
1712 (!bringup && !sp->teardown.single))
1715 * The non AP bound callbacks can fail on bringup. On teardown
1716 * e.g. module removal we crash for now.
1719 if (cpuhp_is_ap_state(state))
1720 ret = cpuhp_invoke_ap_callback(cpu, state, bringup, node);
1722 ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
1724 ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
1726 BUG_ON(ret && !bringup);
1731 * Called from __cpuhp_setup_state on a recoverable failure.
1733 * Note: The teardown callbacks for rollback are not allowed to fail!
1735 static void cpuhp_rollback_install(int failedcpu, enum cpuhp_state state,
1736 struct hlist_node *node)
1740 /* Roll back the already executed steps on the other cpus */
1741 for_each_present_cpu(cpu) {
1742 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1743 int cpustate = st->state;
1745 if (cpu >= failedcpu)
1748 /* Did we invoke the startup call on that cpu ? */
1749 if (cpustate >= state)
1750 cpuhp_issue_call(cpu, state, false, node);
1754 int __cpuhp_state_add_instance_cpuslocked(enum cpuhp_state state,
1755 struct hlist_node *node,
1758 struct cpuhp_step *sp;
1762 lockdep_assert_cpus_held();
1764 sp = cpuhp_get_step(state);
1765 if (sp->multi_instance == false)
1768 mutex_lock(&cpuhp_state_mutex);
1770 if (!invoke || !sp->startup.multi)
1774 * Try to call the startup callback for each present cpu
1775 * depending on the hotplug state of the cpu.
1777 for_each_present_cpu(cpu) {
1778 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1779 int cpustate = st->state;
1781 if (cpustate < state)
1784 ret = cpuhp_issue_call(cpu, state, true, node);
1786 if (sp->teardown.multi)
1787 cpuhp_rollback_install(cpu, state, node);
1793 hlist_add_head(node, &sp->list);
1795 mutex_unlock(&cpuhp_state_mutex);
1799 int __cpuhp_state_add_instance(enum cpuhp_state state, struct hlist_node *node,
1805 ret = __cpuhp_state_add_instance_cpuslocked(state, node, invoke);
1809 EXPORT_SYMBOL_GPL(__cpuhp_state_add_instance);
1812 * __cpuhp_setup_state_cpuslocked - Setup the callbacks for an hotplug machine state
1813 * @state: The state to setup
1814 * @invoke: If true, the startup function is invoked for cpus where
1815 * cpu state >= @state
1816 * @startup: startup callback function
1817 * @teardown: teardown callback function
1818 * @multi_instance: State is set up for multiple instances which get
1821 * The caller needs to hold cpus read locked while calling this function.
1824 * Positive state number if @state is CPUHP_AP_ONLINE_DYN
1825 * 0 for all other states
1826 * On failure: proper (negative) error code
1828 int __cpuhp_setup_state_cpuslocked(enum cpuhp_state state,
1829 const char *name, bool invoke,
1830 int (*startup)(unsigned int cpu),
1831 int (*teardown)(unsigned int cpu),
1832 bool multi_instance)
1837 lockdep_assert_cpus_held();
1839 if (cpuhp_cb_check(state) || !name)
1842 mutex_lock(&cpuhp_state_mutex);
1844 ret = cpuhp_store_callbacks(state, name, startup, teardown,
1847 dynstate = state == CPUHP_AP_ONLINE_DYN;
1848 if (ret > 0 && dynstate) {
1853 if (ret || !invoke || !startup)
1857 * Try to call the startup callback for each present cpu
1858 * depending on the hotplug state of the cpu.
1860 for_each_present_cpu(cpu) {
1861 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1862 int cpustate = st->state;
1864 if (cpustate < state)
1867 ret = cpuhp_issue_call(cpu, state, true, NULL);
1870 cpuhp_rollback_install(cpu, state, NULL);
1871 cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
1876 mutex_unlock(&cpuhp_state_mutex);
1878 * If the requested state is CPUHP_AP_ONLINE_DYN, return the
1879 * dynamically allocated state in case of success.
1881 if (!ret && dynstate)
1885 EXPORT_SYMBOL(__cpuhp_setup_state_cpuslocked);
1887 int __cpuhp_setup_state(enum cpuhp_state state,
1888 const char *name, bool invoke,
1889 int (*startup)(unsigned int cpu),
1890 int (*teardown)(unsigned int cpu),
1891 bool multi_instance)
1896 ret = __cpuhp_setup_state_cpuslocked(state, name, invoke, startup,
1897 teardown, multi_instance);
1901 EXPORT_SYMBOL(__cpuhp_setup_state);
1903 int __cpuhp_state_remove_instance(enum cpuhp_state state,
1904 struct hlist_node *node, bool invoke)
1906 struct cpuhp_step *sp = cpuhp_get_step(state);
1909 BUG_ON(cpuhp_cb_check(state));
1911 if (!sp->multi_instance)
1915 mutex_lock(&cpuhp_state_mutex);
1917 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, node);
1934 mutex_unlock(&cpuhp_state_mutex);
1939 EXPORT_SYMBOL_GPL(__cpuhp_state_remove_instance);
1942 * __cpuhp_remove_state_cpuslocked - Remove the callbacks for an hotplug machine state
1943 * @state: The state to remove
1944 * @invoke: If true, the teardown function is invoked for cpus where
1945 * cpu state >= @state
1947 * The caller needs to hold cpus read locked while calling this function.
1948 * The teardown callback is currently not allowed to fail. Think
1949 * about module removal!
1951 void __cpuhp_remove_state_cpuslocked(enum cpuhp_state state, bool invoke)
1953 struct cpuhp_step *sp = cpuhp_get_step(state);
1956 BUG_ON(cpuhp_cb_check(state));
1958 lockdep_assert_cpus_held();
1960 mutex_lock(&cpuhp_state_mutex);
1961 if (sp->multi_instance) {
1962 WARN(!hlist_empty(&sp->list),
1963 "Error: Removing state %d which has instances left.\n",
1968 if (!invoke || !cpuhp_get_teardown_cb(state))
1972 * Call the teardown callback for each present cpu depending
1973 * on the hotplug state of the cpu. This function is not
1974 * allowed to fail currently!
1976 for_each_present_cpu(cpu) {
1977 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1978 int cpustate = st->state;
1980 if (cpustate >= state)
1981 cpuhp_issue_call(cpu, state, false, NULL);
1984 cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
1985 mutex_unlock(&cpuhp_state_mutex);
1987 EXPORT_SYMBOL(__cpuhp_remove_state_cpuslocked);
1989 void __cpuhp_remove_state(enum cpuhp_state state, bool invoke)
1992 __cpuhp_remove_state_cpuslocked(state, invoke);
1995 EXPORT_SYMBOL(__cpuhp_remove_state);
1997 #ifdef CONFIG_HOTPLUG_SMT
1998 static void cpuhp_offline_cpu_device(unsigned int cpu)
2000 struct device *dev = get_cpu_device(cpu);
2002 dev->offline = true;
2003 /* Tell user space about the state change */
2004 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2007 static void cpuhp_online_cpu_device(unsigned int cpu)
2009 struct device *dev = get_cpu_device(cpu);
2011 dev->offline = false;
2012 /* Tell user space about the state change */
2013 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2016 int cpuhp_smt_disable(enum cpuhp_smt_control ctrlval)
2020 cpu_maps_update_begin();
2021 for_each_online_cpu(cpu) {
2022 if (topology_is_primary_thread(cpu))
2024 ret = cpu_down_maps_locked(cpu, CPUHP_OFFLINE);
2028 * As this needs to hold the cpu maps lock it's impossible
2029 * to call device_offline() because that ends up calling
2030 * cpu_down() which takes cpu maps lock. cpu maps lock
2031 * needs to be held as this might race against in kernel
2032 * abusers of the hotplug machinery (thermal management).
2034 * So nothing would update device:offline state. That would
2035 * leave the sysfs entry stale and prevent onlining after
2036 * smt control has been changed to 'off' again. This is
2037 * called under the sysfs hotplug lock, so it is properly
2038 * serialized against the regular offline usage.
2040 cpuhp_offline_cpu_device(cpu);
2043 cpu_smt_control = ctrlval;
2044 cpu_maps_update_done();
2048 int cpuhp_smt_enable(void)
2052 cpu_maps_update_begin();
2053 cpu_smt_control = CPU_SMT_ENABLED;
2054 for_each_present_cpu(cpu) {
2055 /* Skip online CPUs and CPUs on offline nodes */
2056 if (cpu_online(cpu) || !node_online(cpu_to_node(cpu)))
2058 ret = _cpu_up(cpu, 0, CPUHP_ONLINE);
2061 /* See comment in cpuhp_smt_disable() */
2062 cpuhp_online_cpu_device(cpu);
2064 cpu_maps_update_done();
2069 #if defined(CONFIG_SYSFS) && defined(CONFIG_HOTPLUG_CPU)
2070 static ssize_t show_cpuhp_state(struct device *dev,
2071 struct device_attribute *attr, char *buf)
2073 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
2075 return sprintf(buf, "%d\n", st->state);
2077 static DEVICE_ATTR(state, 0444, show_cpuhp_state, NULL);
2079 static ssize_t write_cpuhp_target(struct device *dev,
2080 struct device_attribute *attr,
2081 const char *buf, size_t count)
2083 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
2084 struct cpuhp_step *sp;
2087 ret = kstrtoint(buf, 10, &target);
2091 #ifdef CONFIG_CPU_HOTPLUG_STATE_CONTROL
2092 if (target < CPUHP_OFFLINE || target > CPUHP_ONLINE)
2095 if (target != CPUHP_OFFLINE && target != CPUHP_ONLINE)
2099 ret = lock_device_hotplug_sysfs();
2103 mutex_lock(&cpuhp_state_mutex);
2104 sp = cpuhp_get_step(target);
2105 ret = !sp->name || sp->cant_stop ? -EINVAL : 0;
2106 mutex_unlock(&cpuhp_state_mutex);
2110 if (st->state < target)
2111 ret = do_cpu_up(dev->id, target);
2113 ret = do_cpu_down(dev->id, target);
2115 unlock_device_hotplug();
2116 return ret ? ret : count;
2119 static ssize_t show_cpuhp_target(struct device *dev,
2120 struct device_attribute *attr, char *buf)
2122 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
2124 return sprintf(buf, "%d\n", st->target);
2126 static DEVICE_ATTR(target, 0644, show_cpuhp_target, write_cpuhp_target);
2129 static ssize_t write_cpuhp_fail(struct device *dev,
2130 struct device_attribute *attr,
2131 const char *buf, size_t count)
2133 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
2134 struct cpuhp_step *sp;
2137 ret = kstrtoint(buf, 10, &fail);
2141 if (fail < CPUHP_OFFLINE || fail > CPUHP_ONLINE)
2145 * Cannot fail STARTING/DYING callbacks.
2147 if (cpuhp_is_atomic_state(fail))
2151 * Cannot fail anything that doesn't have callbacks.
2153 mutex_lock(&cpuhp_state_mutex);
2154 sp = cpuhp_get_step(fail);
2155 if (!sp->startup.single && !sp->teardown.single)
2157 mutex_unlock(&cpuhp_state_mutex);
2166 static ssize_t show_cpuhp_fail(struct device *dev,
2167 struct device_attribute *attr, char *buf)
2169 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
2171 return sprintf(buf, "%d\n", st->fail);
2174 static DEVICE_ATTR(fail, 0644, show_cpuhp_fail, write_cpuhp_fail);
2176 static struct attribute *cpuhp_cpu_attrs[] = {
2177 &dev_attr_state.attr,
2178 &dev_attr_target.attr,
2179 &dev_attr_fail.attr,
2183 static const struct attribute_group cpuhp_cpu_attr_group = {
2184 .attrs = cpuhp_cpu_attrs,
2189 static ssize_t show_cpuhp_states(struct device *dev,
2190 struct device_attribute *attr, char *buf)
2192 ssize_t cur, res = 0;
2195 mutex_lock(&cpuhp_state_mutex);
2196 for (i = CPUHP_OFFLINE; i <= CPUHP_ONLINE; i++) {
2197 struct cpuhp_step *sp = cpuhp_get_step(i);
2200 cur = sprintf(buf, "%3d: %s\n", i, sp->name);
2205 mutex_unlock(&cpuhp_state_mutex);
2208 static DEVICE_ATTR(states, 0444, show_cpuhp_states, NULL);
2210 static struct attribute *cpuhp_cpu_root_attrs[] = {
2211 &dev_attr_states.attr,
2215 static const struct attribute_group cpuhp_cpu_root_attr_group = {
2216 .attrs = cpuhp_cpu_root_attrs,
2221 #ifdef CONFIG_HOTPLUG_SMT
2224 __store_smt_control(struct device *dev, struct device_attribute *attr,
2225 const char *buf, size_t count)
2229 if (sysfs_streq(buf, "on"))
2230 ctrlval = CPU_SMT_ENABLED;
2231 else if (sysfs_streq(buf, "off"))
2232 ctrlval = CPU_SMT_DISABLED;
2233 else if (sysfs_streq(buf, "forceoff"))
2234 ctrlval = CPU_SMT_FORCE_DISABLED;
2238 if (cpu_smt_control == CPU_SMT_FORCE_DISABLED)
2241 if (cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
2244 ret = lock_device_hotplug_sysfs();
2248 if (ctrlval != cpu_smt_control) {
2250 case CPU_SMT_ENABLED:
2251 ret = cpuhp_smt_enable();
2253 case CPU_SMT_DISABLED:
2254 case CPU_SMT_FORCE_DISABLED:
2255 ret = cpuhp_smt_disable(ctrlval);
2260 unlock_device_hotplug();
2261 return ret ? ret : count;
2264 #else /* !CONFIG_HOTPLUG_SMT */
2266 __store_smt_control(struct device *dev, struct device_attribute *attr,
2267 const char *buf, size_t count)
2271 #endif /* CONFIG_HOTPLUG_SMT */
2273 static const char *smt_states[] = {
2274 [CPU_SMT_ENABLED] = "on",
2275 [CPU_SMT_DISABLED] = "off",
2276 [CPU_SMT_FORCE_DISABLED] = "forceoff",
2277 [CPU_SMT_NOT_SUPPORTED] = "notsupported",
2278 [CPU_SMT_NOT_IMPLEMENTED] = "notimplemented",
2282 show_smt_control(struct device *dev, struct device_attribute *attr, char *buf)
2284 const char *state = smt_states[cpu_smt_control];
2286 return snprintf(buf, PAGE_SIZE - 2, "%s\n", state);
2290 store_smt_control(struct device *dev, struct device_attribute *attr,
2291 const char *buf, size_t count)
2293 return __store_smt_control(dev, attr, buf, count);
2295 static DEVICE_ATTR(control, 0644, show_smt_control, store_smt_control);
2298 show_smt_active(struct device *dev, struct device_attribute *attr, char *buf)
2300 return snprintf(buf, PAGE_SIZE - 2, "%d\n", sched_smt_active());
2302 static DEVICE_ATTR(active, 0444, show_smt_active, NULL);
2304 static struct attribute *cpuhp_smt_attrs[] = {
2305 &dev_attr_control.attr,
2306 &dev_attr_active.attr,
2310 static const struct attribute_group cpuhp_smt_attr_group = {
2311 .attrs = cpuhp_smt_attrs,
2316 static int __init cpu_smt_sysfs_init(void)
2318 return sysfs_create_group(&cpu_subsys.dev_root->kobj,
2319 &cpuhp_smt_attr_group);
2322 static int __init cpuhp_sysfs_init(void)
2326 ret = cpu_smt_sysfs_init();
2330 ret = sysfs_create_group(&cpu_subsys.dev_root->kobj,
2331 &cpuhp_cpu_root_attr_group);
2335 for_each_possible_cpu(cpu) {
2336 struct device *dev = get_cpu_device(cpu);
2340 ret = sysfs_create_group(&dev->kobj, &cpuhp_cpu_attr_group);
2346 device_initcall(cpuhp_sysfs_init);
2347 #endif /* CONFIG_SYSFS && CONFIG_HOTPLUG_CPU */
2350 * cpu_bit_bitmap[] is a special, "compressed" data structure that
2351 * represents all NR_CPUS bits binary values of 1<<nr.
2353 * It is used by cpumask_of() to get a constant address to a CPU
2354 * mask value that has a single bit set only.
2357 /* cpu_bit_bitmap[0] is empty - so we can back into it */
2358 #define MASK_DECLARE_1(x) [x+1][0] = (1UL << (x))
2359 #define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
2360 #define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
2361 #define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
2363 const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
2365 MASK_DECLARE_8(0), MASK_DECLARE_8(8),
2366 MASK_DECLARE_8(16), MASK_DECLARE_8(24),
2367 #if BITS_PER_LONG > 32
2368 MASK_DECLARE_8(32), MASK_DECLARE_8(40),
2369 MASK_DECLARE_8(48), MASK_DECLARE_8(56),
2372 EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
2374 const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
2375 EXPORT_SYMBOL(cpu_all_bits);
2377 #ifdef CONFIG_INIT_ALL_POSSIBLE
2378 struct cpumask __cpu_possible_mask __read_mostly
2381 struct cpumask __cpu_possible_mask __read_mostly;
2383 EXPORT_SYMBOL(__cpu_possible_mask);
2385 struct cpumask __cpu_online_mask __read_mostly;
2386 EXPORT_SYMBOL(__cpu_online_mask);
2388 struct cpumask __cpu_present_mask __read_mostly;
2389 EXPORT_SYMBOL(__cpu_present_mask);
2391 struct cpumask __cpu_active_mask __read_mostly;
2392 EXPORT_SYMBOL(__cpu_active_mask);
2394 atomic_t __num_online_cpus __read_mostly;
2395 EXPORT_SYMBOL(__num_online_cpus);
2397 void init_cpu_present(const struct cpumask *src)
2399 cpumask_copy(&__cpu_present_mask, src);
2402 void init_cpu_possible(const struct cpumask *src)
2404 cpumask_copy(&__cpu_possible_mask, src);
2407 void init_cpu_online(const struct cpumask *src)
2409 cpumask_copy(&__cpu_online_mask, src);
2412 void set_cpu_online(unsigned int cpu, bool online)
2415 * atomic_inc/dec() is required to handle the horrid abuse of this
2416 * function by the reboot and kexec code which invoke it from
2417 * IPI/NMI broadcasts when shutting down CPUs. Invocation from
2418 * regular CPU hotplug is properly serialized.
2420 * Note, that the fact that __num_online_cpus is of type atomic_t
2421 * does not protect readers which are not serialized against
2422 * concurrent hotplug operations.
2425 if (!cpumask_test_and_set_cpu(cpu, &__cpu_online_mask))
2426 atomic_inc(&__num_online_cpus);
2428 if (cpumask_test_and_clear_cpu(cpu, &__cpu_online_mask))
2429 atomic_dec(&__num_online_cpus);
2434 * Activate the first processor.
2436 void __init boot_cpu_init(void)
2438 int cpu = smp_processor_id();
2440 /* Mark the boot cpu "present", "online" etc for SMP and UP case */
2441 set_cpu_online(cpu, true);
2442 set_cpu_active(cpu, true);
2443 set_cpu_present(cpu, true);
2444 set_cpu_possible(cpu, true);
2447 __boot_cpu_id = cpu;
2452 * Must be called _AFTER_ setting up the per_cpu areas
2454 void __init boot_cpu_hotplug_init(void)
2457 cpumask_set_cpu(smp_processor_id(), &cpus_booted_once_mask);
2459 this_cpu_write(cpuhp_state.state, CPUHP_ONLINE);
2463 * These are used for a global "mitigations=" cmdline option for toggling
2464 * optional CPU mitigations.
2466 enum cpu_mitigations {
2467 CPU_MITIGATIONS_OFF,
2468 CPU_MITIGATIONS_AUTO,
2469 CPU_MITIGATIONS_AUTO_NOSMT,
2472 static enum cpu_mitigations cpu_mitigations __ro_after_init =
2473 CPU_MITIGATIONS_AUTO;
2475 static int __init mitigations_parse_cmdline(char *arg)
2477 if (!strcmp(arg, "off"))
2478 cpu_mitigations = CPU_MITIGATIONS_OFF;
2479 else if (!strcmp(arg, "auto"))
2480 cpu_mitigations = CPU_MITIGATIONS_AUTO;
2481 else if (!strcmp(arg, "auto,nosmt"))
2482 cpu_mitigations = CPU_MITIGATIONS_AUTO_NOSMT;
2484 pr_crit("Unsupported mitigations=%s, system may still be vulnerable\n",
2489 early_param("mitigations", mitigations_parse_cmdline);
2491 /* mitigations=off */
2492 bool cpu_mitigations_off(void)
2494 return cpu_mitigations == CPU_MITIGATIONS_OFF;
2496 EXPORT_SYMBOL_GPL(cpu_mitigations_off);
2498 /* mitigations=auto,nosmt */
2499 bool cpu_mitigations_auto_nosmt(void)
2501 return cpu_mitigations == CPU_MITIGATIONS_AUTO_NOSMT;
2503 EXPORT_SYMBOL_GPL(cpu_mitigations_auto_nosmt);