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/cpuset.h>
34 #include <soc/samsung/exynos-emc.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 lock_class_key cpuhp_state_key
;
82 static struct lockdep_map cpuhp_state_lock_map
=
83 STATIC_LOCKDEP_MAP_INIT("cpuhp_state", &cpuhp_state_key
);
87 * cpuhp_step - Hotplug state machine step
88 * @name: Name of the step
89 * @startup: Startup function of the step
90 * @teardown: Teardown function of the step
91 * @skip_onerr: Do not invoke the functions on error rollback
92 * Will go away once the notifiers are gone
93 * @cant_stop: Bringup/teardown can't be stopped at this step
98 int (*single
)(unsigned int cpu
);
99 int (*multi
)(unsigned int cpu
,
100 struct hlist_node
*node
);
103 int (*single
)(unsigned int cpu
);
104 int (*multi
)(unsigned int cpu
,
105 struct hlist_node
*node
);
107 struct hlist_head list
;
113 static DEFINE_MUTEX(cpuhp_state_mutex
);
114 static struct cpuhp_step cpuhp_bp_states
[];
115 static struct cpuhp_step cpuhp_ap_states
[];
117 static bool cpuhp_is_ap_state(enum cpuhp_state state
)
120 * The extra check for CPUHP_TEARDOWN_CPU is only for documentation
121 * purposes as that state is handled explicitly in cpu_down.
123 return state
> CPUHP_BRINGUP_CPU
&& state
!= CPUHP_TEARDOWN_CPU
;
126 static struct cpuhp_step
*cpuhp_get_step(enum cpuhp_state state
)
128 struct cpuhp_step
*sp
;
130 sp
= cpuhp_is_ap_state(state
) ? cpuhp_ap_states
: cpuhp_bp_states
;
135 * cpuhp_invoke_callback _ Invoke the callbacks for a given state
136 * @cpu: The cpu for which the callback should be invoked
137 * @state: The state to do callbacks for
138 * @bringup: True if the bringup callback should be invoked
139 * @node: For multi-instance, do a single entry callback for install/remove
140 * @lastp: For multi-instance rollback, remember how far we got
142 * Called from cpu hotplug and from the state register machinery.
144 static int cpuhp_invoke_callback(unsigned int cpu
, enum cpuhp_state state
,
145 bool bringup
, struct hlist_node
*node
,
146 struct hlist_node
**lastp
)
148 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, cpu
);
149 struct cpuhp_step
*step
= cpuhp_get_step(state
);
150 int (*cbm
)(unsigned int cpu
, struct hlist_node
*node
);
151 int (*cb
)(unsigned int cpu
);
154 if (st
->fail
== state
) {
155 st
->fail
= CPUHP_INVALID
;
157 if (!(bringup
? step
->startup
.single
: step
->teardown
.single
))
163 if (!step
->multi_instance
) {
164 WARN_ON_ONCE(lastp
&& *lastp
);
165 cb
= bringup
? step
->startup
.single
: step
->teardown
.single
;
168 trace_cpuhp_enter(cpu
, st
->target
, state
, cb
);
170 trace_cpuhp_exit(cpu
, st
->state
, state
, ret
);
173 cbm
= bringup
? step
->startup
.multi
: step
->teardown
.multi
;
177 /* Single invocation for instance add/remove */
179 WARN_ON_ONCE(lastp
&& *lastp
);
180 trace_cpuhp_multi_enter(cpu
, st
->target
, state
, cbm
, node
);
181 ret
= cbm(cpu
, node
);
182 trace_cpuhp_exit(cpu
, st
->state
, state
, ret
);
186 /* State transition. Invoke on all instances */
188 hlist_for_each(node
, &step
->list
) {
189 if (lastp
&& node
== *lastp
)
192 trace_cpuhp_multi_enter(cpu
, st
->target
, state
, cbm
, node
);
193 ret
= cbm(cpu
, node
);
194 trace_cpuhp_exit(cpu
, st
->state
, state
, ret
);
208 /* Rollback the instances if one failed */
209 cbm
= !bringup
? step
->startup
.multi
: step
->teardown
.multi
;
213 hlist_for_each(node
, &step
->list
) {
217 trace_cpuhp_multi_enter(cpu
, st
->target
, state
, cbm
, node
);
218 ret
= cbm(cpu
, node
);
219 trace_cpuhp_exit(cpu
, st
->state
, state
, ret
);
221 * Rollback must not fail,
229 static inline void wait_for_ap_thread(struct cpuhp_cpu_state
*st
, bool bringup
)
231 struct completion
*done
= bringup
? &st
->done_up
: &st
->done_down
;
232 wait_for_completion(done
);
235 static inline void complete_ap_thread(struct cpuhp_cpu_state
*st
, bool bringup
)
237 struct completion
*done
= bringup
? &st
->done_up
: &st
->done_down
;
242 * The former STARTING/DYING states, ran with IRQs disabled and must not fail.
244 static bool cpuhp_is_atomic_state(enum cpuhp_state state
)
246 return CPUHP_AP_IDLE_DEAD
<= state
&& state
< CPUHP_AP_ONLINE
;
249 /* Serializes the updates to cpu_online_mask, cpu_present_mask */
250 static DEFINE_MUTEX(cpu_add_remove_lock
);
251 bool cpuhp_tasks_frozen
;
252 EXPORT_SYMBOL_GPL(cpuhp_tasks_frozen
);
255 * The following two APIs (cpu_maps_update_begin/done) must be used when
256 * attempting to serialize the updates to cpu_online_mask & cpu_present_mask.
258 void cpu_maps_update_begin(void)
260 mutex_lock(&cpu_add_remove_lock
);
263 void cpu_maps_update_done(void)
265 mutex_unlock(&cpu_add_remove_lock
);
268 /* If set, cpu_up and cpu_down will return -EBUSY and do nothing.
269 * Should always be manipulated under cpu_add_remove_lock
271 static int cpu_hotplug_disabled
;
273 #ifdef CONFIG_HOTPLUG_CPU
276 struct task_struct
*active_writer
;
277 /* wait queue to wake up the active_writer */
278 wait_queue_head_t wq
;
279 /* verifies that no writer will get active while readers are active */
282 * Also blocks the new readers during
283 * an ongoing cpu hotplug operation.
287 #ifdef CONFIG_DEBUG_LOCK_ALLOC
288 struct lockdep_map dep_map
;
291 .active_writer
= NULL
,
292 .wq
= __WAIT_QUEUE_HEAD_INITIALIZER(cpu_hotplug
.wq
),
293 .lock
= __MUTEX_INITIALIZER(cpu_hotplug
.lock
),
294 #ifdef CONFIG_DEBUG_LOCK_ALLOC
295 .dep_map
= STATIC_LOCKDEP_MAP_INIT("cpu_hotplug.dep_map", &cpu_hotplug
.dep_map
),
299 /* Lockdep annotations for get/put_online_cpus() and cpu_hotplug_begin/end() */
300 #define cpuhp_lock_acquire_read() lock_map_acquire_read(&cpu_hotplug.dep_map)
301 #define cpuhp_lock_acquire_tryread() \
302 lock_map_acquire_tryread(&cpu_hotplug.dep_map)
303 #define cpuhp_lock_acquire() lock_map_acquire(&cpu_hotplug.dep_map)
304 #define cpuhp_lock_release() lock_map_release(&cpu_hotplug.dep_map)
307 void cpus_read_lock(void)
310 if (cpu_hotplug
.active_writer
== current
)
312 cpuhp_lock_acquire_read();
313 mutex_lock(&cpu_hotplug
.lock
);
314 atomic_inc(&cpu_hotplug
.refcount
);
315 mutex_unlock(&cpu_hotplug
.lock
);
317 EXPORT_SYMBOL_GPL(cpus_read_lock
);
319 void cpus_read_unlock(void)
323 if (cpu_hotplug
.active_writer
== current
)
326 refcount
= atomic_dec_return(&cpu_hotplug
.refcount
);
327 if (WARN_ON(refcount
< 0)) /* try to fix things up */
328 atomic_inc(&cpu_hotplug
.refcount
);
330 if (refcount
<= 0 && waitqueue_active(&cpu_hotplug
.wq
))
331 wake_up(&cpu_hotplug
.wq
);
333 cpuhp_lock_release();
336 EXPORT_SYMBOL_GPL(cpus_read_unlock
);
339 * This ensures that the hotplug operation can begin only when the
340 * refcount goes to zero.
342 * Note that during a cpu-hotplug operation, the new readers, if any,
343 * will be blocked by the cpu_hotplug.lock
345 * Since cpu_hotplug_begin() is always called after invoking
346 * cpu_maps_update_begin(), we can be sure that only one writer is active.
348 * Note that theoretically, there is a possibility of a livelock:
349 * - Refcount goes to zero, last reader wakes up the sleeping
351 * - Last reader unlocks the cpu_hotplug.lock.
352 * - A new reader arrives at this moment, bumps up the refcount.
353 * - The writer acquires the cpu_hotplug.lock finds the refcount
354 * non zero and goes to sleep again.
356 * However, this is very difficult to achieve in practice since
357 * get_online_cpus() not an api which is called all that often.
360 void cpus_write_lock(void)
364 cpu_hotplug
.active_writer
= current
;
365 cpuhp_lock_acquire();
368 mutex_lock(&cpu_hotplug
.lock
);
369 prepare_to_wait(&cpu_hotplug
.wq
, &wait
, TASK_UNINTERRUPTIBLE
);
370 if (likely(!atomic_read(&cpu_hotplug
.refcount
)))
372 mutex_unlock(&cpu_hotplug
.lock
);
375 finish_wait(&cpu_hotplug
.wq
, &wait
);
378 void cpus_write_unlock(void)
380 cpu_hotplug
.active_writer
= NULL
;
381 mutex_unlock(&cpu_hotplug
.lock
);
382 cpuhp_lock_release();
386 * Wait for currently running CPU hotplug operations to complete (if any) and
387 * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
388 * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the
389 * hotplug path before performing hotplug operations. So acquiring that lock
390 * guarantees mutual exclusion from any currently running hotplug operations.
392 void cpu_hotplug_disable(void)
394 cpu_maps_update_begin();
395 cpu_hotplug_disabled
++;
396 cpu_maps_update_done();
398 EXPORT_SYMBOL_GPL(cpu_hotplug_disable
);
400 static void __cpu_hotplug_enable(void)
402 if (WARN_ONCE(!cpu_hotplug_disabled
, "Unbalanced cpu hotplug enable\n"))
404 cpu_hotplug_disabled
--;
407 void cpu_hotplug_enable(void)
409 cpu_maps_update_begin();
410 __cpu_hotplug_enable();
411 cpu_maps_update_done();
413 EXPORT_SYMBOL_GPL(cpu_hotplug_enable
);
414 #endif /* CONFIG_HOTPLUG_CPU */
417 * Architectures that need SMT-specific errata handling during SMT hotplug
418 * should override this.
420 void __weak
arch_smt_update(void) { }
422 #ifdef CONFIG_HOTPLUG_SMT
423 enum cpuhp_smt_control cpu_smt_control __read_mostly
= CPU_SMT_ENABLED
;
425 void __init
cpu_smt_disable(bool force
)
427 if (cpu_smt_control
== CPU_SMT_FORCE_DISABLED
||
428 cpu_smt_control
== CPU_SMT_NOT_SUPPORTED
)
432 pr_info("SMT: Force disabled\n");
433 cpu_smt_control
= CPU_SMT_FORCE_DISABLED
;
435 cpu_smt_control
= CPU_SMT_DISABLED
;
440 * The decision whether SMT is supported can only be done after the full
441 * CPU identification. Called from architecture code.
443 void __init
cpu_smt_check_topology(void)
445 if (!topology_smt_supported())
446 cpu_smt_control
= CPU_SMT_NOT_SUPPORTED
;
449 static int __init
smt_cmdline_disable(char *str
)
451 cpu_smt_disable(str
&& !strcmp(str
, "force"));
454 early_param("nosmt", smt_cmdline_disable
);
456 static inline bool cpu_smt_allowed(unsigned int cpu
)
458 if (cpu_smt_control
== CPU_SMT_ENABLED
)
461 if (topology_is_primary_thread(cpu
))
465 * On x86 it's required to boot all logical CPUs at least once so
466 * that the init code can get a chance to set CR4.MCE on each
467 * CPU. Otherwise, a broadacasted MCE observing CR4.MCE=0b on any
468 * core will shutdown the machine.
470 return !per_cpu(cpuhp_state
, cpu
).booted_once
;
473 static inline bool cpu_smt_allowed(unsigned int cpu
) { return true; }
476 static inline enum cpuhp_state
477 cpuhp_set_state(struct cpuhp_cpu_state
*st
, enum cpuhp_state target
)
479 enum cpuhp_state prev_state
= st
->state
;
481 st
->rollback
= false;
486 st
->bringup
= st
->state
< target
;
492 cpuhp_reset_state(struct cpuhp_cpu_state
*st
, enum cpuhp_state prev_state
)
497 * If we have st->last we need to undo partial multi_instance of this
498 * state first. Otherwise start undo at the previous state.
507 st
->target
= prev_state
;
508 st
->bringup
= !st
->bringup
;
511 /* Regular hotplug invocation of the AP hotplug thread */
512 static void __cpuhp_kick_ap(struct cpuhp_cpu_state
*st
)
514 if (!st
->single
&& st
->state
== st
->target
)
519 * Make sure the above stores are visible before should_run becomes
520 * true. Paired with the mb() above in cpuhp_thread_fun()
523 st
->should_run
= true;
524 wake_up_process(st
->thread
);
525 wait_for_ap_thread(st
, st
->bringup
);
528 static int cpuhp_kick_ap(struct cpuhp_cpu_state
*st
, enum cpuhp_state target
)
530 enum cpuhp_state prev_state
;
533 prev_state
= cpuhp_set_state(st
, target
);
535 if ((ret
= st
->result
)) {
536 cpuhp_reset_state(st
, prev_state
);
542 /* Notifier wrappers for transitioning to state machine */
544 static int bringup_wait_for_ap(unsigned int cpu
)
546 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, cpu
);
548 /* Wait for the CPU to reach CPUHP_AP_ONLINE_IDLE */
549 wait_for_ap_thread(st
, true);
550 if (WARN_ON_ONCE((!cpu_online(cpu
))))
553 /* Unpark the stopper thread and the hotplug thread of the target cpu */
554 stop_machine_unpark(cpu
);
555 kthread_unpark(st
->thread
);
558 * SMT soft disabling on X86 requires to bring the CPU out of the
559 * BIOS 'wait for SIPI' state in order to set the CR4.MCE bit. The
560 * CPU marked itself as booted_once in cpu_notify_starting() so the
561 * cpu_smt_allowed() check will now return false if this is not the
564 if (!cpu_smt_allowed(cpu
))
567 if (st
->target
<= CPUHP_AP_ONLINE_IDLE
)
570 return cpuhp_kick_ap(st
, st
->target
);
573 static int bringup_cpu(unsigned int cpu
)
575 struct task_struct
*idle
= idle_thread_get(cpu
);
579 * Some architectures have to walk the irq descriptors to
580 * setup the vector space for the cpu which comes online.
581 * Prevent irq alloc/free across the bringup.
585 /* Arch-specific enabling code. */
586 ret
= __cpu_up(cpu
, idle
);
590 return bringup_wait_for_ap(cpu
);
594 * Hotplug state machine related functions
597 static void undo_cpu_up(unsigned int cpu
, struct cpuhp_cpu_state
*st
)
599 for (st
->state
--; st
->state
> st
->target
; st
->state
--) {
600 struct cpuhp_step
*step
= cpuhp_get_step(st
->state
);
602 if (!step
->skip_onerr
)
603 cpuhp_invoke_callback(cpu
, st
->state
, false, NULL
, NULL
);
607 static inline bool can_rollback_cpu(struct cpuhp_cpu_state
*st
)
609 if (IS_ENABLED(CONFIG_HOTPLUG_CPU
))
612 * When CPU hotplug is disabled, then taking the CPU down is not
613 * possible because takedown_cpu() and the architecture and
614 * subsystem specific mechanisms are not available. So the CPU
615 * which would be completely unplugged again needs to stay around
616 * in the current state.
618 return st
->state
<= CPUHP_BRINGUP_CPU
;
621 static int cpuhp_up_callbacks(unsigned int cpu
, struct cpuhp_cpu_state
*st
,
622 enum cpuhp_state target
)
624 enum cpuhp_state prev_state
= st
->state
;
627 while (st
->state
< target
) {
629 ret
= cpuhp_invoke_callback(cpu
, st
->state
, true, NULL
, NULL
);
631 if (can_rollback_cpu(st
)) {
632 st
->target
= prev_state
;
633 undo_cpu_up(cpu
, st
);
642 * The cpu hotplug threads manage the bringup and teardown of the cpus
644 static void cpuhp_create(unsigned int cpu
)
646 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, cpu
);
648 init_completion(&st
->done_up
);
649 init_completion(&st
->done_down
);
652 static int cpuhp_should_run(unsigned int cpu
)
654 struct cpuhp_cpu_state
*st
= this_cpu_ptr(&cpuhp_state
);
656 return st
->should_run
;
660 * Execute teardown/startup callbacks on the plugged cpu. Also used to invoke
661 * callbacks when a state gets [un]installed at runtime.
663 * Each invocation of this function by the smpboot thread does a single AP
666 * It has 3 modes of operation:
667 * - single: runs st->cb_state
668 * - up: runs ++st->state, while st->state < st->target
669 * - down: runs st->state--, while st->state > st->target
671 * When complete or on error, should_run is cleared and the completion is fired.
673 static void cpuhp_thread_fun(unsigned int cpu
)
675 struct cpuhp_cpu_state
*st
= this_cpu_ptr(&cpuhp_state
);
676 bool bringup
= st
->bringup
;
677 enum cpuhp_state state
;
679 if (WARN_ON_ONCE(!st
->should_run
))
683 * ACQUIRE for the cpuhp_should_run() load of ->should_run. Ensures
684 * that if we see ->should_run we also see the rest of the state.
689 if (WARN_ON_ONCE(!st
->should_run
))
692 lock_map_acquire(&cpuhp_state_lock_map
);
694 cpuhp_lock_acquire(bringup
);
695 >>>>>>> android
-4.14-p
698 state
= st
->cb_state
;
699 st
->should_run
= false;
704 st
->should_run
= (st
->state
< st
->target
);
705 WARN_ON_ONCE(st
->state
> st
->target
);
709 st
->should_run
= (st
->state
> st
->target
);
710 WARN_ON_ONCE(st
->state
< st
->target
);
714 WARN_ON_ONCE(!cpuhp_is_ap_state(state
));
717 struct cpuhp_step
*step
= cpuhp_get_step(state
);
718 if (step
->skip_onerr
)
722 if (cpuhp_is_atomic_state(state
)) {
724 st
->result
= cpuhp_invoke_callback(cpu
, state
, bringup
, st
->node
, &st
->last
);
728 * STARTING/DYING must not fail!
730 WARN_ON_ONCE(st
->result
);
732 st
->result
= cpuhp_invoke_callback(cpu
, state
, bringup
, st
->node
, &st
->last
);
737 * If we fail on a rollback, we're up a creek without no
738 * paddle, no way forward, no way back. We loose, thanks for
741 WARN_ON_ONCE(st
->rollback
);
742 st
->should_run
= false;
746 lock_map_release(&cpuhp_state_lock_map
);
749 complete_ap_thread(st
, bringup
);
752 /* Invoke a single callback on a remote cpu */
754 cpuhp_invoke_ap_callback(int cpu
, enum cpuhp_state state
, bool bringup
,
755 struct hlist_node
*node
)
757 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, cpu
);
760 if (!cpu_online(cpu
))
763 lock_map_acquire(&cpuhp_state_lock_map
);
764 lock_map_release(&cpuhp_state_lock_map
);
767 * If we are up and running, use the hotplug thread. For early calls
768 * we invoke the thread function directly.
771 return cpuhp_invoke_callback(cpu
, state
, bringup
, node
, NULL
);
773 st
->rollback
= false;
777 st
->bringup
= bringup
;
778 st
->cb_state
= state
;
784 * If we failed and did a partial, do a rollback.
786 if ((ret
= st
->result
) && st
->last
) {
788 st
->bringup
= !bringup
;
794 * Clean up the leftovers so the next hotplug operation wont use stale
797 st
->node
= st
->last
= NULL
;
801 static int cpuhp_fast_kick_ap_work_pre(unsigned int cpu
)
803 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, cpu
);
804 enum cpuhp_state prev_state
= st
->state
;
806 lock_map_acquire(&cpuhp_state_lock_map
);
807 lock_map_release(&cpuhp_state_lock_map
);
809 trace_cpuhp_enter(cpu
, st
->target
, prev_state
,
810 cpuhp_fast_kick_ap_work_pre
);
812 cpuhp_set_state(st
, st
->target
);
813 if (!st
->single
&& st
->state
== st
->target
)
818 * Make sure the above stores are visible before should_run becomes
819 * true. Paired with the mb() above in cpuhp_thread_fun()
822 st
->should_run
= true;
823 wake_up_process(st
->thread
);
828 static int cpuhp_fast_kick_ap_work_post(unsigned int cpu
,
829 enum cpuhp_state prev_state
)
831 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, cpu
);
834 wait_for_ap_thread(st
, st
->bringup
);
835 if ((ret
= st
->result
)) {
836 cpuhp_reset_state(st
, prev_state
);
839 trace_cpuhp_exit(cpu
, st
->state
, prev_state
, ret
);
844 static int cpuhp_kick_ap_work(unsigned int cpu
)
846 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, cpu
);
847 enum cpuhp_state prev_state
= st
->state
;
850 lock_map_acquire(&cpuhp_state_lock_map
);
851 lock_map_release(&cpuhp_state_lock_map
);
853 trace_cpuhp_enter(cpu
, st
->target
, prev_state
, cpuhp_kick_ap_work
);
854 ret
= cpuhp_kick_ap(st
, st
->target
);
855 trace_cpuhp_exit(cpu
, st
->state
, prev_state
, ret
);
860 static struct smp_hotplug_thread cpuhp_threads
= {
861 .store
= &cpuhp_state
.thread
,
862 .create
= &cpuhp_create
,
863 .thread_should_run
= cpuhp_should_run
,
864 .thread_fn
= cpuhp_thread_fun
,
865 .thread_comm
= "cpuhp/%u",
869 void __init
cpuhp_threads_init(void)
871 BUG_ON(smpboot_register_percpu_thread(&cpuhp_threads
));
872 kthread_unpark(this_cpu_read(cpuhp_state
.thread
));
875 #ifdef CONFIG_HOTPLUG_CPU
877 * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU
880 * This function walks all processes, finds a valid mm struct for each one and
881 * then clears a corresponding bit in mm's cpumask. While this all sounds
882 * trivial, there are various non-obvious corner cases, which this function
883 * tries to solve in a safe manner.
885 * Also note that the function uses a somewhat relaxed locking scheme, so it may
886 * be called only for an already offlined CPU.
888 void clear_tasks_mm_cpumask(int cpu
)
890 struct task_struct
*p
;
893 * This function is called after the cpu is taken down and marked
894 * offline, so its not like new tasks will ever get this cpu set in
895 * their mm mask. -- Peter Zijlstra
896 * Thus, we may use rcu_read_lock() here, instead of grabbing
897 * full-fledged tasklist_lock.
899 WARN_ON(cpu_online(cpu
));
901 for_each_process(p
) {
902 struct task_struct
*t
;
905 * Main thread might exit, but other threads may still have
906 * a valid mm. Find one.
908 t
= find_lock_task_mm(p
);
911 cpumask_clear_cpu(cpu
, mm_cpumask(t
->mm
));
917 /* Take this CPU down. */
918 static int take_cpu_down(void *_param
)
920 struct cpuhp_cpu_state
*st
= this_cpu_ptr(&cpuhp_state
);
921 enum cpuhp_state target
= max((int)st
->target
, CPUHP_AP_OFFLINE
);
922 int err
, cpu
= smp_processor_id();
925 /* Ensure this CPU doesn't handle any more interrupts. */
926 err
= __cpu_disable();
931 * We get here while we are in CPUHP_TEARDOWN_CPU state and we must not
932 * do this step again.
934 WARN_ON(st
->state
!= CPUHP_TEARDOWN_CPU
);
936 /* Invoke the former CPU_DYING callbacks */
937 for (; st
->state
> target
; st
->state
--) {
938 ret
= cpuhp_invoke_callback(cpu
, st
->state
, false, NULL
, NULL
);
940 * DYING must not fail!
945 /* Give up timekeeping duties */
946 tick_handover_do_timer();
947 /* Park the stopper thread */
948 stop_machine_park(cpu
);
952 static int takedown_cpu(unsigned int cpu
);
953 static int takedown_cpus(const struct cpumask
*down_cpus
)
955 struct cpuhp_cpu_state
*st
;
958 /* Park the smpboot threads */
959 for_each_cpu(cpu
, down_cpus
) {
960 st
= per_cpu_ptr(&cpuhp_state
, cpu
);
961 trace_cpuhp_enter(cpu
, st
->target
, st
->state
, takedown_cpu
);
963 kthread_park(per_cpu_ptr(&cpuhp_state
, cpu
)->thread
);
964 smpboot_park_threads(cpu
);
968 * Prevent irq alloc/free while the dying cpu reorganizes the
969 * interrupt affinities.
974 * So now all preempt/rcu users must observe !cpu_active().
976 err
= stop_machine_cpuslocked(take_cpu_down
, NULL
, down_cpus
);
978 /* CPU refused to die */
980 for_each_cpu(cpu
, down_cpus
) {
981 st
= per_cpu_ptr(&cpuhp_state
, cpu
);
982 st
->target
= st
->state
;
984 /* Unpark the hotplug thread so we can rollback there */
985 kthread_unpark(per_cpu_ptr(&cpuhp_state
, cpu
)->thread
);
990 for_each_cpu(cpu
, down_cpus
) {
991 st
= per_cpu_ptr(&cpuhp_state
, cpu
);
992 BUG_ON(cpu_online(cpu
));
995 * The CPUHP_AP_SCHED_MIGRATE_DYING callback will have removed all
996 * runnable tasks from the cpu, there's only the idle task left now
997 * that the migration thread is done doing the stop_machine thing.
999 * Wait for the stop thread to go away.
1001 wait_for_ap_thread(st
, false);
1002 BUG_ON(st
->state
!= CPUHP_AP_IDLE_DEAD
);
1006 /* Interrupts are moved away from the dying cpu, reenable alloc/free */
1007 irq_unlock_sparse();
1009 for_each_cpu(cpu
, down_cpus
) {
1010 st
= per_cpu_ptr(&cpuhp_state
, cpu
);
1012 hotplug_cpu__broadcast_tick_pull(cpu
);
1013 /* This actually kills the CPU. */
1015 tick_cleanup_dead_cpu(cpu
);
1016 rcutree_migrate_callbacks(cpu
);
1018 trace_cpuhp_exit(cpu
, st
->state
, st
->state
, st
->result
);
1025 static int takedown_cpu(unsigned int cpu
)
1027 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, cpu
);
1030 /* Park the smpboot threads */
1031 kthread_park(per_cpu_ptr(&cpuhp_state
, cpu
)->thread
);
1034 * Prevent irq alloc/free while the dying cpu reorganizes the
1035 * interrupt affinities.
1040 * So now all preempt/rcu users must observe !cpu_active().
1042 err
= stop_machine_cpuslocked(take_cpu_down
, NULL
, cpumask_of(cpu
));
1044 /* CPU refused to die */
1045 irq_unlock_sparse();
1046 /* Unpark the hotplug thread so we can rollback there */
1047 kthread_unpark(per_cpu_ptr(&cpuhp_state
, cpu
)->thread
);
1050 BUG_ON(cpu_online(cpu
));
1053 * The CPUHP_AP_SCHED_MIGRATE_DYING callback will have removed all
1054 * runnable tasks from the cpu, there's only the idle task left now
1055 * that the migration thread is done doing the stop_machine thing.
1057 * Wait for the stop thread to go away.
1059 wait_for_ap_thread(st
, false);
1060 BUG_ON(st
->state
!= CPUHP_AP_IDLE_DEAD
);
1062 /* Interrupts are moved away from the dying cpu, reenable alloc/free */
1063 irq_unlock_sparse();
1065 hotplug_cpu__broadcast_tick_pull(cpu
);
1066 /* This actually kills the CPU. */
1069 tick_cleanup_dead_cpu(cpu
);
1070 rcutree_migrate_callbacks(cpu
);
1074 static void cpuhp_complete_idle_dead(void *arg
)
1076 struct cpuhp_cpu_state
*st
= arg
;
1078 complete_ap_thread(st
, false);
1081 void cpuhp_report_idle_dead(void)
1083 struct cpuhp_cpu_state
*st
= this_cpu_ptr(&cpuhp_state
);
1085 BUG_ON(st
->state
!= CPUHP_AP_OFFLINE
);
1086 rcu_report_dead(smp_processor_id());
1087 st
->state
= CPUHP_AP_IDLE_DEAD
;
1089 * We cannot call complete after rcu_report_dead() so we delegate it
1092 smp_call_function_single(cpumask_first(cpu_online_mask
),
1093 cpuhp_complete_idle_dead
, st
, 0);
1096 static void undo_cpu_down(unsigned int cpu
, struct cpuhp_cpu_state
*st
)
1098 for (st
->state
++; st
->state
< st
->target
; st
->state
++) {
1099 struct cpuhp_step
*step
= cpuhp_get_step(st
->state
);
1101 if (!step
->skip_onerr
)
1102 cpuhp_invoke_callback(cpu
, st
->state
, true, NULL
, NULL
);
1106 static int cpuhp_down_callbacks(unsigned int cpu
, struct cpuhp_cpu_state
*st
,
1107 enum cpuhp_state target
)
1109 enum cpuhp_state prev_state
= st
->state
;
1112 for (; st
->state
> target
; st
->state
--) {
1113 ret
= cpuhp_invoke_callback(cpu
, st
->state
, false, NULL
, NULL
);
1115 st
->target
= prev_state
;
1116 if (st
->state
< prev_state
)
1117 undo_cpu_down(cpu
, st
);
1123 static int __ref
_cpus_down(struct cpumask cpus
, int tasks_frozen
,
1124 enum cpuhp_state target
)
1126 struct cpuhp_cpu_state
*st
;
1127 cpumask_t ap_work_cpus
= CPU_MASK_NONE
;
1128 cpumask_t take_down_cpus
= CPU_MASK_NONE
;
1129 int prev_state
[8] = {0};
1133 if (num_online_cpus() == 1)
1136 for_each_cpu(cpu
, &cpus
)
1137 if (!cpu_present(cpu
))
1141 cpuhp_tasks_frozen
= tasks_frozen
;
1143 cpumask_copy(&cpu_fastoff_mask
, &cpus
);
1144 for_each_cpu(cpu
, &cpus
) {
1145 st
= per_cpu_ptr(&cpuhp_state
, cpu
);
1146 prev_state
[cpu
] = cpuhp_set_state(st
, target
);
1147 if (st
->state
> CPUHP_TEARDOWN_CPU
)
1148 cpumask_set_cpu(cpu
, &ap_work_cpus
);
1150 cpumask_set_cpu(cpu
, &take_down_cpus
);
1153 for_each_cpu(cpu
, &ap_work_cpus
) {
1154 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, cpu
);
1155 emc_cpu_pre_off_callback(cpu
);
1156 set_cpu_active(cpu
, false);
1157 st
->state
= CPUHP_AP_EXYNOS_IDLE_CTRL
;
1160 cpuset_update_active_cpus();
1162 for_each_cpu(cpu
, &ap_work_cpus
) {
1163 st
= per_cpu_ptr(&cpuhp_state
, cpu
);
1164 set_cpu_active(cpu
, false);
1165 st
->state
= CPUHP_AP_EXYNOS_IDLE_CTRL
;
1168 cpuset_update_active_cpus();
1170 for_each_cpu(cpu
, &ap_work_cpus
) {
1171 st
= per_cpu_ptr(&cpuhp_state
, cpu
);
1172 st
->target
= max((int)target
, CPUHP_TEARDOWN_CPU
);
1173 cpuhp_fast_kick_ap_work_pre(cpu
);
1176 for_each_cpu(cpu
, &ap_work_cpus
) {
1177 st
= per_cpu_ptr(&cpuhp_state
, cpu
);
1178 cpuhp_fast_kick_ap_work_post(cpu
, prev_state
[cpu
]);
1180 * We might have stopped still in the range of the AP hotplug
1181 * thread. Nothing to do anymore.
1183 st
->target
= target
;
1184 cpumask_set_cpu(cpu
, &take_down_cpus
);
1187 /* Hotplug out of all cpu failed */
1188 if (cpumask_empty(&take_down_cpus
))
1191 ret
= takedown_cpus(&take_down_cpus
);
1193 panic("%s: fauiled to takedown_cpus\n", __func__
);
1196 for_each_cpu(cpu
, &take_down_cpus
) {
1197 st
= per_cpu_ptr(&cpuhp_state
, cpu
);
1198 ret
= cpuhp_down_callbacks(cpu
, st
, target
);
1199 if (ret
&& st
->state
> CPUHP_TEARDOWN_CPU
&& st
->state
< prev_state
[cpu
]) {
1200 cpuhp_reset_state(st
, prev_state
[cpu
]);
1201 __cpuhp_kick_ap(st
);
1205 cpumask_clear(&cpu_fastoff_mask
);
1208 cpus_write_unlock();
1211 * Do post unplug cleanup. This is still protected against
1212 * concurrent CPU hotplug via cpu_add_remove_lock.
1214 lockup_detector_cleanup();
1219 int cpus_down(struct cpumask cpus
)
1223 trace_cpus_down_enter(cpumask_first(&cpus
));
1224 cpu_maps_update_begin();
1226 if (cpu_hotplug_disabled
) {
1231 for_each_cpu(cpu
, &cpus
)
1232 if (!cpu_online(cpu
)) {
1233 cpumask_clear_cpu(cpu
, &cpus
);
1234 pr_warn("cpus_down: cpu%d is not online\n", cpu
);
1237 err
= _cpus_down(cpus
, 0, CPUHP_OFFLINE
);
1240 cpu_maps_update_done();
1241 trace_cpus_down_exit(cpumask_first(&cpus
));
1244 EXPORT_SYMBOL_GPL(cpus_down
);
1246 /* Requires cpu_add_remove_lock to be held */
1247 static int __ref
_cpu_down(unsigned int cpu
, int tasks_frozen
,
1248 enum cpuhp_state target
)
1250 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, cpu
);
1251 int prev_state
, ret
= 0;
1253 if (num_online_cpus() == 1)
1256 if (!cpu_present(cpu
))
1261 cpuhp_tasks_frozen
= tasks_frozen
;
1263 prev_state
= cpuhp_set_state(st
, target
);
1265 * If the current CPU state is in the range of the AP hotplug thread,
1266 * then we need to kick the thread.
1268 if (st
->state
> CPUHP_TEARDOWN_CPU
) {
1269 st
->target
= max((int)target
, CPUHP_TEARDOWN_CPU
);
1270 ret
= cpuhp_kick_ap_work(cpu
);
1272 * The AP side has done the error rollback already. Just
1273 * return the error code..
1279 * We might have stopped still in the range of the AP hotplug
1280 * thread. Nothing to do anymore.
1282 if (st
->state
> CPUHP_TEARDOWN_CPU
)
1285 st
->target
= target
;
1288 * The AP brought itself down to CPUHP_TEARDOWN_CPU. So we need
1289 * to do the further cleanups.
1291 ret
= cpuhp_down_callbacks(cpu
, st
, target
);
1292 if (ret
&& st
->state
== CPUHP_TEARDOWN_CPU
&& st
->state
< prev_state
) {
1293 cpuhp_reset_state(st
, prev_state
);
1294 __cpuhp_kick_ap(st
);
1298 cpus_write_unlock();
1300 * Do post unplug cleanup. This is still protected against
1301 * concurrent CPU hotplug via cpu_add_remove_lock.
1303 lockup_detector_cleanup();
1308 static int cpu_down_maps_locked(unsigned int cpu
, enum cpuhp_state target
)
1310 if (cpu_hotplug_disabled
)
1312 return _cpu_down(cpu
, 0, target
);
1315 static int do_cpu_down(unsigned int cpu
, enum cpuhp_state target
)
1319 cpu_maps_update_begin();
1320 err
= cpu_down_maps_locked(cpu
, target
);
1321 cpu_maps_update_done();
1325 int cpu_down(unsigned int cpu
)
1327 return do_cpu_down(cpu
, CPUHP_OFFLINE
);
1329 EXPORT_SYMBOL(cpu_down
);
1332 #define takedown_cpu NULL
1333 #endif /*CONFIG_HOTPLUG_CPU*/
1336 * notify_cpu_starting(cpu) - Invoke the callbacks on the starting CPU
1337 * @cpu: cpu that just started
1339 * It must be called by the arch code on the new cpu, before the new cpu
1340 * enables interrupts and before the "boot" cpu returns from __cpu_up().
1342 void notify_cpu_starting(unsigned int cpu
)
1344 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, cpu
);
1345 enum cpuhp_state target
= min((int)st
->target
, CPUHP_AP_ONLINE
);
1348 rcu_cpu_starting(cpu
); /* Enables RCU usage on this CPU. */
1349 st
->booted_once
= true;
1350 while (st
->state
< target
) {
1352 ret
= cpuhp_invoke_callback(cpu
, st
->state
, true, NULL
, NULL
);
1354 * STARTING must not fail!
1361 * Called from the idle task. Wake up the controlling task which brings the
1362 * stopper and the hotplug thread of the upcoming CPU up and then delegates
1363 * the rest of the online bringup to the hotplug thread.
1365 void cpuhp_online_idle(enum cpuhp_state state
)
1367 struct cpuhp_cpu_state
*st
= this_cpu_ptr(&cpuhp_state
);
1369 /* Happens for the boot cpu */
1370 if (state
!= CPUHP_AP_ONLINE_IDLE
)
1373 st
->state
= CPUHP_AP_ONLINE_IDLE
;
1374 complete_ap_thread(st
, true);
1377 /* Requires cpu_add_remove_lock to be held */
1378 static int __ref
_cpus_up(struct cpumask cpus
, int tasks_frozen
,
1379 enum cpuhp_state target
)
1381 struct cpuhp_cpu_state
*st
;
1382 cpumask_t ap_work_cpus
= CPU_MASK_NONE
;
1383 cpumask_t bringup_cpus
= CPU_MASK_NONE
;
1384 int prev_state
[8] = {0};
1385 struct task_struct
*idle
;
1391 for_each_cpu(cpu
, &cpus
)
1392 if (!cpu_present(cpu
)) {
1393 pr_warn("_cpus_up: cpu%d is not present\n", cpu
);
1394 cpumask_clear_cpu(cpu
, &cpus
);
1397 cpumask_copy(&cpu_faston_mask
, &cpus
);
1399 for_each_cpu(cpu
, &cpu_faston_mask
) {
1400 st
= per_cpu_ptr(&cpuhp_state
, cpu
);
1402 * The caller of do_cpu_up might have raced with another
1403 * caller. Ignore it for now.
1405 if (st
->state
>= target
)
1408 if (st
->state
== CPUHP_OFFLINE
) {
1409 /* Let it fail before we try to bring the cpu up */
1410 idle
= idle_thread_get(cpu
);
1412 ret
= PTR_ERR(idle
);
1417 prev_state
[cpu
] = cpuhp_set_state(st
, target
);
1419 if (st
->state
> CPUHP_BRINGUP_CPU
)
1420 cpumask_set_cpu(cpu
, &ap_work_cpus
);
1422 cpumask_set_cpu(cpu
, &bringup_cpus
);
1426 cpuhp_tasks_frozen
= tasks_frozen
;
1428 * If the current CPU state is in the range of the AP hotplug thread,
1429 * then we need to kick the thread once more.
1431 for_each_cpu(cpu
, &ap_work_cpus
)
1432 cpuhp_fast_kick_ap_work_pre(cpu
);
1434 for_each_cpu(cpu
, &ap_work_cpus
)
1435 cpuhp_fast_kick_ap_work_post(cpu
, prev_state
[cpu
]);
1437 /* Hotplug out of all cpu failed */
1438 if (cpumask_empty(&bringup_cpus
))
1442 * Try to reach the target state. We max out on the BP at
1443 * CPUHP_BRINGUP_CPU. After that the AP hotplug thread is
1444 * responsible for bringing it up to the target state.
1446 target
= min((int)target
, CPUHP_BRINGUP_CPU
);
1447 for_each_cpu(cpu
, &bringup_cpus
) {
1448 st
= per_cpu_ptr(&cpuhp_state
, cpu
);
1449 ret
= cpuhp_up_callbacks(cpu
, st
, target
);
1451 panic("%s: fauiled to bringup_cpus\n", __func__
);
1454 cpumask_clear(&cpu_faston_mask
);
1455 cpus_write_unlock();
1461 /* Requires cpu_add_remove_lock to be held */
1462 static int _cpu_up(unsigned int cpu
, int tasks_frozen
, enum cpuhp_state target
)
1464 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, cpu
);
1465 struct task_struct
*idle
;
1470 if (!cpu_present(cpu
)) {
1476 * The caller of do_cpu_up might have raced with another
1477 * caller. Ignore it for now.
1479 if (st
->state
>= target
)
1482 if (st
->state
== CPUHP_OFFLINE
) {
1483 /* Let it fail before we try to bring the cpu up */
1484 idle
= idle_thread_get(cpu
);
1486 ret
= PTR_ERR(idle
);
1491 cpuhp_tasks_frozen
= tasks_frozen
;
1493 cpuhp_set_state(st
, target
);
1495 * If the current CPU state is in the range of the AP hotplug thread,
1496 * then we need to kick the thread once more.
1498 if (st
->state
> CPUHP_BRINGUP_CPU
) {
1499 ret
= cpuhp_kick_ap_work(cpu
);
1501 * The AP side has done the error rollback already. Just
1502 * return the error code..
1509 * Try to reach the target state. We max out on the BP at
1510 * CPUHP_BRINGUP_CPU. After that the AP hotplug thread is
1511 * responsible for bringing it up to the target state.
1513 target
= min((int)target
, CPUHP_BRINGUP_CPU
);
1514 ret
= cpuhp_up_callbacks(cpu
, st
, target
);
1516 cpus_write_unlock();
1521 static int do_cpu_up(unsigned int cpu
, enum cpuhp_state target
)
1525 if (!cpu_possible(cpu
)) {
1526 pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n",
1528 #if defined(CONFIG_IA64)
1529 pr_err("please check additional_cpus= boot parameter\n");
1534 err
= try_online_node(cpu_to_node(cpu
));
1538 cpu_maps_update_begin();
1540 if (cpu_hotplug_disabled
) {
1544 if (!cpu_smt_allowed(cpu
)) {
1549 err
= _cpu_up(cpu
, 0, target
);
1551 cpu_maps_update_done();
1555 int cpu_up(unsigned int cpu
)
1557 return do_cpu_up(cpu
, CPUHP_ONLINE
);
1559 EXPORT_SYMBOL_GPL(cpu_up
);
1561 int cpus_up(struct cpumask cpus
)
1565 trace_cpus_up_enter(cpumask_first(&cpus
));
1566 for_each_cpu(cpu
, &cpus
)
1567 if (cpu_online(cpu
)) {
1568 cpumask_clear_cpu(cpu
, &cpus
);
1569 pr_warn("cpus_up: cpu%d is already online\n", cpu
);
1572 for_each_cpu(cpu
, &cpus
) {
1573 err
= try_online_node(cpu_to_node(cpu
));
1578 cpu_maps_update_begin();
1580 if (cpu_hotplug_disabled
) {
1585 err
= _cpus_up(cpus
, 0, CPUHP_ONLINE
);
1587 cpu_maps_update_done();
1588 trace_cpus_up_exit(cpumask_first(&cpus
));
1592 EXPORT_SYMBOL_GPL(cpus_up
);
1594 #ifdef CONFIG_PM_SLEEP_SMP
1595 static cpumask_var_t frozen_cpus
;
1597 int freeze_secondary_cpus(int primary
)
1601 cpu_maps_update_begin();
1602 if (!cpu_online(primary
))
1603 primary
= cpumask_first(cpu_online_mask
);
1605 * We take down all of the non-boot CPUs in one shot to avoid races
1606 * with the userspace trying to use the CPU hotplug at the same time
1608 cpumask_clear(frozen_cpus
);
1610 pr_info("Disabling non-boot CPUs ...\n");
1611 for_each_online_cpu(cpu
) {
1614 trace_suspend_resume(TPS("CPU_OFF"), cpu
, true);
1615 error
= _cpu_down(cpu
, 1, CPUHP_OFFLINE
);
1616 trace_suspend_resume(TPS("CPU_OFF"), cpu
, false);
1618 cpumask_set_cpu(cpu
, frozen_cpus
);
1620 pr_err("Error taking CPU%d down: %d\n", cpu
, error
);
1626 BUG_ON(num_online_cpus() > 1);
1628 pr_err("Non-boot CPUs are not disabled\n");
1631 * Make sure the CPUs won't be enabled by someone else. We need to do
1632 * this even in case of failure as all disable_nonboot_cpus() users are
1633 * supposed to do enable_nonboot_cpus() on the failure path.
1635 cpu_hotplug_disabled
++;
1637 cpu_maps_update_done();
1641 void __weak
arch_enable_nonboot_cpus_begin(void)
1645 void __weak
arch_enable_nonboot_cpus_end(void)
1649 void enable_nonboot_cpus(void)
1652 struct device
*cpu_device
;
1654 /* Allow everyone to use the CPU hotplug again */
1655 cpu_maps_update_begin();
1656 __cpu_hotplug_enable();
1657 if (cpumask_empty(frozen_cpus
))
1660 pr_info("Enabling non-boot CPUs ...\n");
1662 arch_enable_nonboot_cpus_begin();
1664 for_each_cpu(cpu
, frozen_cpus
) {
1665 trace_suspend_resume(TPS("CPU_ON"), cpu
, true);
1666 error
= _cpu_up(cpu
, 1, CPUHP_ONLINE
);
1667 trace_suspend_resume(TPS("CPU_ON"), cpu
, false);
1669 pr_info("CPU%d is up\n", cpu
);
1670 cpu_device
= get_cpu_device(cpu
);
1672 pr_err("%s: failed to get cpu%d device\n",
1675 kobject_uevent(&cpu_device
->kobj
, KOBJ_ONLINE
);
1678 pr_warn("Error taking CPU%d up: %d\n", cpu
, error
);
1681 arch_enable_nonboot_cpus_end();
1683 cpumask_clear(frozen_cpus
);
1685 cpu_maps_update_done();
1688 static int __init
alloc_frozen_cpus(void)
1690 if (!alloc_cpumask_var(&frozen_cpus
, GFP_KERNEL
|__GFP_ZERO
))
1694 core_initcall(alloc_frozen_cpus
);
1697 * When callbacks for CPU hotplug notifications are being executed, we must
1698 * ensure that the state of the system with respect to the tasks being frozen
1699 * or not, as reported by the notification, remains unchanged *throughout the
1700 * duration* of the execution of the callbacks.
1701 * Hence we need to prevent the freezer from racing with regular CPU hotplug.
1703 * This synchronization is implemented by mutually excluding regular CPU
1704 * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
1705 * Hibernate notifications.
1708 cpu_hotplug_pm_callback(struct notifier_block
*nb
,
1709 unsigned long action
, void *ptr
)
1713 case PM_SUSPEND_PREPARE
:
1714 case PM_HIBERNATION_PREPARE
:
1715 cpu_hotplug_disable();
1718 case PM_POST_SUSPEND
:
1719 case PM_POST_HIBERNATION
:
1720 cpu_hotplug_enable();
1731 struct cpumask cpu_fastoff_mask
;
1732 EXPORT_SYMBOL(cpu_fastoff_mask
);
1733 struct cpumask cpu_faston_mask
;
1734 EXPORT_SYMBOL(cpu_faston_mask
);
1735 static int __init
cpu_hotplug_pm_sync_init(void)
1738 * cpu_hotplug_pm_callback has higher priority than x86
1739 * bsp_pm_callback which depends on cpu_hotplug_pm_callback
1740 * to disable cpu hotplug to avoid cpu hotplug race.
1742 pm_notifier(cpu_hotplug_pm_callback
, 0);
1743 cpumask_clear(&cpu_fastoff_mask
);
1744 cpumask_clear(&cpu_faston_mask
);
1748 core_initcall(cpu_hotplug_pm_sync_init
);
1750 #endif /* CONFIG_PM_SLEEP_SMP */
1754 #endif /* CONFIG_SMP */
1756 /* Boot processor state steps */
1757 static struct cpuhp_step cpuhp_bp_states
[] = {
1760 .startup
.single
= NULL
,
1761 .teardown
.single
= NULL
,
1764 [CPUHP_CREATE_THREADS
]= {
1765 .name
= "threads:prepare",
1766 .startup
.single
= smpboot_create_threads
,
1767 .teardown
.single
= NULL
,
1770 [CPUHP_PERF_PREPARE
] = {
1771 .name
= "perf:prepare",
1772 .startup
.single
= perf_event_init_cpu
,
1773 .teardown
.single
= perf_event_exit_cpu
,
1775 [CPUHP_WORKQUEUE_PREP
] = {
1776 .name
= "workqueue:prepare",
1777 .startup
.single
= workqueue_prepare_cpu
,
1778 .teardown
.single
= NULL
,
1780 [CPUHP_HRTIMERS_PREPARE
] = {
1781 .name
= "hrtimers:prepare",
1782 .startup
.single
= hrtimers_prepare_cpu
,
1783 .teardown
.single
= hrtimers_dead_cpu
,
1785 [CPUHP_SMPCFD_PREPARE
] = {
1786 .name
= "smpcfd:prepare",
1787 .startup
.single
= smpcfd_prepare_cpu
,
1788 .teardown
.single
= smpcfd_dead_cpu
,
1790 [CPUHP_RELAY_PREPARE
] = {
1791 .name
= "relay:prepare",
1792 .startup
.single
= relay_prepare_cpu
,
1793 .teardown
.single
= NULL
,
1795 [CPUHP_SLAB_PREPARE
] = {
1796 .name
= "slab:prepare",
1797 .startup
.single
= slab_prepare_cpu
,
1798 .teardown
.single
= slab_dead_cpu
,
1800 [CPUHP_RCUTREE_PREP
] = {
1801 .name
= "RCU/tree:prepare",
1802 .startup
.single
= rcutree_prepare_cpu
,
1803 .teardown
.single
= rcutree_dead_cpu
,
1806 * On the tear-down path, timers_dead_cpu() must be invoked
1807 * before blk_mq_queue_reinit_notify() from notify_dead(),
1808 * otherwise a RCU stall occurs.
1810 [CPUHP_TIMERS_PREPARE
] = {
1811 .name
= "timers:dead",
1812 .startup
.single
= timers_prepare_cpu
,
1813 .teardown
.single
= timers_dead_cpu
,
1815 /* Kicks the plugged cpu into life */
1816 [CPUHP_BRINGUP_CPU
] = {
1817 .name
= "cpu:bringup",
1818 .startup
.single
= bringup_cpu
,
1819 .teardown
.single
= NULL
,
1823 * Handled on controll processor until the plugged processor manages
1826 [CPUHP_TEARDOWN_CPU
] = {
1827 .name
= "cpu:teardown",
1828 .startup
.single
= NULL
,
1829 .teardown
.single
= takedown_cpu
,
1833 [CPUHP_BRINGUP_CPU
] = { },
1837 /* Application processor state steps */
1838 static struct cpuhp_step cpuhp_ap_states
[] = {
1840 /* Final state before CPU kills itself */
1841 [CPUHP_AP_IDLE_DEAD
] = {
1842 .name
= "idle:dead",
1845 * Last state before CPU enters the idle loop to die. Transient state
1846 * for synchronization.
1848 [CPUHP_AP_OFFLINE
] = {
1849 .name
= "ap:offline",
1852 /* First state is scheduler control. Interrupts are disabled */
1853 [CPUHP_AP_SCHED_STARTING
] = {
1854 .name
= "sched:starting",
1855 .startup
.single
= sched_cpu_starting
,
1856 .teardown
.single
= sched_cpu_dying
,
1858 [CPUHP_AP_RCUTREE_DYING
] = {
1859 .name
= "RCU/tree:dying",
1860 .startup
.single
= NULL
,
1861 .teardown
.single
= rcutree_dying_cpu
,
1863 [CPUHP_AP_SMPCFD_DYING
] = {
1864 .name
= "smpcfd:dying",
1865 .startup
.single
= NULL
,
1866 .teardown
.single
= smpcfd_dying_cpu
,
1868 /* Entry state on starting. Interrupts enabled from here on. Transient
1869 * state for synchronsization */
1870 [CPUHP_AP_ONLINE
] = {
1871 .name
= "ap:online",
1873 /* Handle smpboot threads park/unpark */
1874 [CPUHP_AP_SMPBOOT_THREADS
] = {
1875 .name
= "smpboot/threads:online",
1876 .startup
.single
= smpboot_unpark_threads
,
1877 .teardown
.single
= smpboot_park_threads
,
1879 [CPUHP_AP_IRQ_AFFINITY_ONLINE
] = {
1880 .name
= "irq/affinity:online",
1881 .startup
.single
= irq_affinity_online_cpu
,
1882 .teardown
.single
= NULL
,
1884 [CPUHP_AP_PERF_ONLINE
] = {
1885 .name
= "perf:online",
1886 .startup
.single
= perf_event_init_cpu
,
1887 .teardown
.single
= perf_event_exit_cpu
,
1889 [CPUHP_AP_WORKQUEUE_ONLINE
] = {
1890 .name
= "workqueue:online",
1891 .startup
.single
= workqueue_online_cpu
,
1892 .teardown
.single
= workqueue_offline_cpu
,
1894 [CPUHP_AP_RCUTREE_ONLINE
] = {
1895 .name
= "RCU/tree:online",
1896 .startup
.single
= rcutree_online_cpu
,
1897 .teardown
.single
= rcutree_offline_cpu
,
1901 * The dynamically registered state space is here
1905 /* Last state is scheduler control setting the cpu active */
1906 [CPUHP_AP_ACTIVE
] = {
1907 .name
= "sched:active",
1908 .startup
.single
= sched_cpu_activate
,
1909 .teardown
.single
= sched_cpu_deactivate
,
1913 /* CPU is fully up and running. */
1916 .startup
.single
= NULL
,
1917 .teardown
.single
= NULL
,
1921 /* Sanity check for callbacks */
1922 static int cpuhp_cb_check(enum cpuhp_state state
)
1924 if (state
<= CPUHP_OFFLINE
|| state
>= CPUHP_ONLINE
)
1930 * Returns a free for dynamic slot assignment of the Online state. The states
1931 * are protected by the cpuhp_slot_states mutex and an empty slot is identified
1932 * by having no name assigned.
1934 static int cpuhp_reserve_state(enum cpuhp_state state
)
1936 enum cpuhp_state i
, end
;
1937 struct cpuhp_step
*step
;
1940 case CPUHP_AP_ONLINE_DYN
:
1941 step
= cpuhp_ap_states
+ CPUHP_AP_ONLINE_DYN
;
1942 end
= CPUHP_AP_ONLINE_DYN_END
;
1944 case CPUHP_BP_PREPARE_DYN
:
1945 step
= cpuhp_bp_states
+ CPUHP_BP_PREPARE_DYN
;
1946 end
= CPUHP_BP_PREPARE_DYN_END
;
1952 for (i
= state
; i
<= end
; i
++, step
++) {
1956 WARN(1, "No more dynamic states available for CPU hotplug\n");
1960 static int cpuhp_store_callbacks(enum cpuhp_state state
, const char *name
,
1961 int (*startup
)(unsigned int cpu
),
1962 int (*teardown
)(unsigned int cpu
),
1963 bool multi_instance
)
1965 /* (Un)Install the callbacks for further cpu hotplug operations */
1966 struct cpuhp_step
*sp
;
1970 * If name is NULL, then the state gets removed.
1972 * CPUHP_AP_ONLINE_DYN and CPUHP_BP_PREPARE_DYN are handed out on
1973 * the first allocation from these dynamic ranges, so the removal
1974 * would trigger a new allocation and clear the wrong (already
1975 * empty) state, leaving the callbacks of the to be cleared state
1976 * dangling, which causes wreckage on the next hotplug operation.
1978 if (name
&& (state
== CPUHP_AP_ONLINE_DYN
||
1979 state
== CPUHP_BP_PREPARE_DYN
)) {
1980 ret
= cpuhp_reserve_state(state
);
1985 sp
= cpuhp_get_step(state
);
1986 if (name
&& sp
->name
)
1989 sp
->startup
.single
= startup
;
1990 sp
->teardown
.single
= teardown
;
1992 sp
->multi_instance
= multi_instance
;
1993 INIT_HLIST_HEAD(&sp
->list
);
1997 static void *cpuhp_get_teardown_cb(enum cpuhp_state state
)
1999 return cpuhp_get_step(state
)->teardown
.single
;
2003 * Call the startup/teardown function for a step either on the AP or
2004 * on the current CPU.
2006 static int cpuhp_issue_call(int cpu
, enum cpuhp_state state
, bool bringup
,
2007 struct hlist_node
*node
)
2009 struct cpuhp_step
*sp
= cpuhp_get_step(state
);
2013 * If there's nothing to do, we done.
2014 * Relies on the union for multi_instance.
2016 if ((bringup
&& !sp
->startup
.single
) ||
2017 (!bringup
&& !sp
->teardown
.single
))
2020 * The non AP bound callbacks can fail on bringup. On teardown
2021 * e.g. module removal we crash for now.
2024 if (cpuhp_is_ap_state(state
))
2025 ret
= cpuhp_invoke_ap_callback(cpu
, state
, bringup
, node
);
2027 ret
= cpuhp_invoke_callback(cpu
, state
, bringup
, node
, NULL
);
2029 ret
= cpuhp_invoke_callback(cpu
, state
, bringup
, node
, NULL
);
2031 BUG_ON(ret
&& !bringup
);
2036 * Called from __cpuhp_setup_state on a recoverable failure.
2038 * Note: The teardown callbacks for rollback are not allowed to fail!
2040 static void cpuhp_rollback_install(int failedcpu
, enum cpuhp_state state
,
2041 struct hlist_node
*node
)
2045 /* Roll back the already executed steps on the other cpus */
2046 for_each_present_cpu(cpu
) {
2047 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, cpu
);
2048 int cpustate
= st
->state
;
2050 if (cpu
>= failedcpu
)
2053 /* Did we invoke the startup call on that cpu ? */
2054 if (cpustate
>= state
)
2055 cpuhp_issue_call(cpu
, state
, false, node
);
2059 int __cpuhp_state_add_instance_cpuslocked(enum cpuhp_state state
,
2060 struct hlist_node
*node
,
2063 struct cpuhp_step
*sp
;
2067 lockdep_assert_cpus_held();
2069 sp
= cpuhp_get_step(state
);
2070 if (sp
->multi_instance
== false)
2073 mutex_lock(&cpuhp_state_mutex
);
2075 if (!invoke
|| !sp
->startup
.multi
)
2079 * Try to call the startup callback for each present cpu
2080 * depending on the hotplug state of the cpu.
2082 for_each_present_cpu(cpu
) {
2083 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, cpu
);
2084 int cpustate
= st
->state
;
2086 if (cpustate
< state
)
2089 ret
= cpuhp_issue_call(cpu
, state
, true, node
);
2091 if (sp
->teardown
.multi
)
2092 cpuhp_rollback_install(cpu
, state
, node
);
2098 hlist_add_head(node
, &sp
->list
);
2100 mutex_unlock(&cpuhp_state_mutex
);
2104 int __cpuhp_state_add_instance(enum cpuhp_state state
, struct hlist_node
*node
,
2110 ret
= __cpuhp_state_add_instance_cpuslocked(state
, node
, invoke
);
2114 EXPORT_SYMBOL_GPL(__cpuhp_state_add_instance
);
2117 * __cpuhp_setup_state_cpuslocked - Setup the callbacks for an hotplug machine state
2118 * @state: The state to setup
2119 * @invoke: If true, the startup function is invoked for cpus where
2120 * cpu state >= @state
2121 * @startup: startup callback function
2122 * @teardown: teardown callback function
2123 * @multi_instance: State is set up for multiple instances which get
2126 * The caller needs to hold cpus read locked while calling this function.
2129 * Positive state number if @state is CPUHP_AP_ONLINE_DYN
2130 * 0 for all other states
2131 * On failure: proper (negative) error code
2133 int __cpuhp_setup_state_cpuslocked(enum cpuhp_state state
,
2134 const char *name
, bool invoke
,
2135 int (*startup
)(unsigned int cpu
),
2136 int (*teardown
)(unsigned int cpu
),
2137 bool multi_instance
)
2142 lockdep_assert_cpus_held();
2144 if (cpuhp_cb_check(state
) || !name
)
2147 mutex_lock(&cpuhp_state_mutex
);
2149 ret
= cpuhp_store_callbacks(state
, name
, startup
, teardown
,
2152 dynstate
= state
== CPUHP_AP_ONLINE_DYN
;
2153 if (ret
> 0 && dynstate
) {
2158 if (ret
|| !invoke
|| !startup
)
2162 * Try to call the startup callback for each present cpu
2163 * depending on the hotplug state of the cpu.
2165 for_each_present_cpu(cpu
) {
2166 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, cpu
);
2167 int cpustate
= st
->state
;
2169 if (cpustate
< state
)
2172 ret
= cpuhp_issue_call(cpu
, state
, true, NULL
);
2175 cpuhp_rollback_install(cpu
, state
, NULL
);
2176 cpuhp_store_callbacks(state
, NULL
, NULL
, NULL
, false);
2181 mutex_unlock(&cpuhp_state_mutex
);
2183 * If the requested state is CPUHP_AP_ONLINE_DYN, return the
2184 * dynamically allocated state in case of success.
2186 if (!ret
&& dynstate
)
2190 EXPORT_SYMBOL(__cpuhp_setup_state_cpuslocked
);
2192 int __cpuhp_setup_state(enum cpuhp_state state
,
2193 const char *name
, bool invoke
,
2194 int (*startup
)(unsigned int cpu
),
2195 int (*teardown
)(unsigned int cpu
),
2196 bool multi_instance
)
2201 ret
= __cpuhp_setup_state_cpuslocked(state
, name
, invoke
, startup
,
2202 teardown
, multi_instance
);
2206 EXPORT_SYMBOL(__cpuhp_setup_state
);
2208 int __cpuhp_state_remove_instance(enum cpuhp_state state
,
2209 struct hlist_node
*node
, bool invoke
)
2211 struct cpuhp_step
*sp
= cpuhp_get_step(state
);
2214 BUG_ON(cpuhp_cb_check(state
));
2216 if (!sp
->multi_instance
)
2220 mutex_lock(&cpuhp_state_mutex
);
2222 if (!invoke
|| !cpuhp_get_teardown_cb(state
))
2225 * Call the teardown callback for each present cpu depending
2226 * on the hotplug state of the cpu. This function is not
2227 * allowed to fail currently!
2229 for_each_present_cpu(cpu
) {
2230 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, cpu
);
2231 int cpustate
= st
->state
;
2233 if (cpustate
>= state
)
2234 cpuhp_issue_call(cpu
, state
, false, node
);
2239 mutex_unlock(&cpuhp_state_mutex
);
2244 EXPORT_SYMBOL_GPL(__cpuhp_state_remove_instance
);
2247 * __cpuhp_remove_state_cpuslocked - Remove the callbacks for an hotplug machine state
2248 * @state: The state to remove
2249 * @invoke: If true, the teardown function is invoked for cpus where
2250 * cpu state >= @state
2252 * The caller needs to hold cpus read locked while calling this function.
2253 * The teardown callback is currently not allowed to fail. Think
2254 * about module removal!
2256 void __cpuhp_remove_state_cpuslocked(enum cpuhp_state state
, bool invoke
)
2258 struct cpuhp_step
*sp
= cpuhp_get_step(state
);
2261 BUG_ON(cpuhp_cb_check(state
));
2263 lockdep_assert_cpus_held();
2265 mutex_lock(&cpuhp_state_mutex
);
2266 if (sp
->multi_instance
) {
2267 WARN(!hlist_empty(&sp
->list
),
2268 "Error: Removing state %d which has instances left.\n",
2273 if (!invoke
|| !cpuhp_get_teardown_cb(state
))
2277 * Call the teardown callback for each present cpu depending
2278 * on the hotplug state of the cpu. This function is not
2279 * allowed to fail currently!
2281 for_each_present_cpu(cpu
) {
2282 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, cpu
);
2283 int cpustate
= st
->state
;
2285 if (cpustate
>= state
)
2286 cpuhp_issue_call(cpu
, state
, false, NULL
);
2289 cpuhp_store_callbacks(state
, NULL
, NULL
, NULL
, false);
2290 mutex_unlock(&cpuhp_state_mutex
);
2292 EXPORT_SYMBOL(__cpuhp_remove_state_cpuslocked
);
2294 void __cpuhp_remove_state(enum cpuhp_state state
, bool invoke
)
2297 __cpuhp_remove_state_cpuslocked(state
, invoke
);
2300 EXPORT_SYMBOL(__cpuhp_remove_state
);
2302 #if defined(CONFIG_SYSFS) && defined(CONFIG_HOTPLUG_CPU)
2303 static ssize_t
show_cpuhp_state(struct device
*dev
,
2304 struct device_attribute
*attr
, char *buf
)
2306 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, dev
->id
);
2308 return sprintf(buf
, "%d\n", st
->state
);
2310 static DEVICE_ATTR(state
, 0444, show_cpuhp_state
, NULL
);
2312 static ssize_t
write_cpuhp_target(struct device
*dev
,
2313 struct device_attribute
*attr
,
2314 const char *buf
, size_t count
)
2316 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, dev
->id
);
2317 struct cpuhp_step
*sp
;
2320 ret
= kstrtoint(buf
, 10, &target
);
2324 #ifdef CONFIG_CPU_HOTPLUG_STATE_CONTROL
2325 if (target
< CPUHP_OFFLINE
|| target
> CPUHP_ONLINE
)
2328 if (target
!= CPUHP_OFFLINE
&& target
!= CPUHP_ONLINE
)
2332 ret
= lock_device_hotplug_sysfs();
2336 mutex_lock(&cpuhp_state_mutex
);
2337 sp
= cpuhp_get_step(target
);
2338 ret
= !sp
->name
|| sp
->cant_stop
? -EINVAL
: 0;
2339 mutex_unlock(&cpuhp_state_mutex
);
2343 if (st
->state
< target
)
2344 ret
= do_cpu_up(dev
->id
, target
);
2346 ret
= do_cpu_down(dev
->id
, target
);
2348 unlock_device_hotplug();
2349 return ret
? ret
: count
;
2352 static ssize_t
show_cpuhp_target(struct device
*dev
,
2353 struct device_attribute
*attr
, char *buf
)
2355 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, dev
->id
);
2357 return sprintf(buf
, "%d\n", st
->target
);
2359 static DEVICE_ATTR(target
, 0644, show_cpuhp_target
, write_cpuhp_target
);
2362 static ssize_t
write_cpuhp_fail(struct device
*dev
,
2363 struct device_attribute
*attr
,
2364 const char *buf
, size_t count
)
2366 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, dev
->id
);
2367 struct cpuhp_step
*sp
;
2370 ret
= kstrtoint(buf
, 10, &fail
);
2375 * Cannot fail STARTING/DYING callbacks.
2377 if (cpuhp_is_atomic_state(fail
))
2381 * Cannot fail anything that doesn't have callbacks.
2383 mutex_lock(&cpuhp_state_mutex
);
2384 sp
= cpuhp_get_step(fail
);
2385 if (!sp
->startup
.single
&& !sp
->teardown
.single
)
2387 mutex_unlock(&cpuhp_state_mutex
);
2396 static ssize_t
show_cpuhp_fail(struct device
*dev
,
2397 struct device_attribute
*attr
, char *buf
)
2399 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, dev
->id
);
2401 return sprintf(buf
, "%d\n", st
->fail
);
2404 static DEVICE_ATTR(fail
, 0644, show_cpuhp_fail
, write_cpuhp_fail
);
2406 static struct attribute
*cpuhp_cpu_attrs
[] = {
2407 &dev_attr_state
.attr
,
2408 &dev_attr_target
.attr
,
2409 &dev_attr_fail
.attr
,
2413 static const struct attribute_group cpuhp_cpu_attr_group
= {
2414 .attrs
= cpuhp_cpu_attrs
,
2419 static ssize_t
show_cpuhp_states(struct device
*dev
,
2420 struct device_attribute
*attr
, char *buf
)
2422 ssize_t cur
, res
= 0;
2425 mutex_lock(&cpuhp_state_mutex
);
2426 for (i
= CPUHP_OFFLINE
; i
<= CPUHP_ONLINE
; i
++) {
2427 struct cpuhp_step
*sp
= cpuhp_get_step(i
);
2430 cur
= sprintf(buf
, "%3d: %s\n", i
, sp
->name
);
2435 mutex_unlock(&cpuhp_state_mutex
);
2438 static DEVICE_ATTR(states
, 0444, show_cpuhp_states
, NULL
);
2440 static struct attribute
*cpuhp_cpu_root_attrs
[] = {
2441 &dev_attr_states
.attr
,
2445 static const struct attribute_group cpuhp_cpu_root_attr_group
= {
2446 .attrs
= cpuhp_cpu_root_attrs
,
2451 #ifdef CONFIG_HOTPLUG_SMT
2453 static const char *smt_states
[] = {
2454 [CPU_SMT_ENABLED
] = "on",
2455 [CPU_SMT_DISABLED
] = "off",
2456 [CPU_SMT_FORCE_DISABLED
] = "forceoff",
2457 [CPU_SMT_NOT_SUPPORTED
] = "notsupported",
2461 show_smt_control(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
2463 return snprintf(buf
, PAGE_SIZE
- 2, "%s\n", smt_states
[cpu_smt_control
]);
2466 static void cpuhp_offline_cpu_device(unsigned int cpu
)
2468 struct device
*dev
= get_cpu_device(cpu
);
2470 dev
->offline
= true;
2471 /* Tell user space about the state change */
2472 kobject_uevent(&dev
->kobj
, KOBJ_OFFLINE
);
2475 static void cpuhp_online_cpu_device(unsigned int cpu
)
2477 struct device
*dev
= get_cpu_device(cpu
);
2479 dev
->offline
= false;
2480 /* Tell user space about the state change */
2481 kobject_uevent(&dev
->kobj
, KOBJ_ONLINE
);
2484 static int cpuhp_smt_disable(enum cpuhp_smt_control ctrlval
)
2488 cpu_maps_update_begin();
2489 for_each_online_cpu(cpu
) {
2490 if (topology_is_primary_thread(cpu
))
2492 ret
= cpu_down_maps_locked(cpu
, CPUHP_OFFLINE
);
2496 * As this needs to hold the cpu maps lock it's impossible
2497 * to call device_offline() because that ends up calling
2498 * cpu_down() which takes cpu maps lock. cpu maps lock
2499 * needs to be held as this might race against in kernel
2500 * abusers of the hotplug machinery (thermal management).
2502 * So nothing would update device:offline state. That would
2503 * leave the sysfs entry stale and prevent onlining after
2504 * smt control has been changed to 'off' again. This is
2505 * called under the sysfs hotplug lock, so it is properly
2506 * serialized against the regular offline usage.
2508 cpuhp_offline_cpu_device(cpu
);
2511 cpu_smt_control
= ctrlval
;
2514 cpu_maps_update_done();
2518 static int cpuhp_smt_enable(void)
2522 cpu_maps_update_begin();
2523 cpu_smt_control
= CPU_SMT_ENABLED
;
2525 for_each_present_cpu(cpu
) {
2526 /* Skip online CPUs and CPUs on offline nodes */
2527 if (cpu_online(cpu
) || !node_online(cpu_to_node(cpu
)))
2529 ret
= _cpu_up(cpu
, 0, CPUHP_ONLINE
);
2532 /* See comment in cpuhp_smt_disable() */
2533 cpuhp_online_cpu_device(cpu
);
2535 cpu_maps_update_done();
2540 store_smt_control(struct device
*dev
, struct device_attribute
*attr
,
2541 const char *buf
, size_t count
)
2545 if (sysfs_streq(buf
, "on"))
2546 ctrlval
= CPU_SMT_ENABLED
;
2547 else if (sysfs_streq(buf
, "off"))
2548 ctrlval
= CPU_SMT_DISABLED
;
2549 else if (sysfs_streq(buf
, "forceoff"))
2550 ctrlval
= CPU_SMT_FORCE_DISABLED
;
2554 if (cpu_smt_control
== CPU_SMT_FORCE_DISABLED
)
2557 if (cpu_smt_control
== CPU_SMT_NOT_SUPPORTED
)
2560 ret
= lock_device_hotplug_sysfs();
2564 if (ctrlval
!= cpu_smt_control
) {
2566 case CPU_SMT_ENABLED
:
2567 ret
= cpuhp_smt_enable();
2569 case CPU_SMT_DISABLED
:
2570 case CPU_SMT_FORCE_DISABLED
:
2571 ret
= cpuhp_smt_disable(ctrlval
);
2576 unlock_device_hotplug();
2577 return ret
? ret
: count
;
2579 static DEVICE_ATTR(control
, 0644, show_smt_control
, store_smt_control
);
2582 show_smt_active(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
2584 bool active
= topology_max_smt_threads() > 1;
2586 return snprintf(buf
, PAGE_SIZE
- 2, "%d\n", active
);
2588 static DEVICE_ATTR(active
, 0444, show_smt_active
, NULL
);
2590 static struct attribute
*cpuhp_smt_attrs
[] = {
2591 &dev_attr_control
.attr
,
2592 &dev_attr_active
.attr
,
2596 static const struct attribute_group cpuhp_smt_attr_group
= {
2597 .attrs
= cpuhp_smt_attrs
,
2602 static int __init
cpu_smt_state_init(void)
2604 return sysfs_create_group(&cpu_subsys
.dev_root
->kobj
,
2605 &cpuhp_smt_attr_group
);
2609 static inline int cpu_smt_state_init(void) { return 0; }
2612 static int __init
cpuhp_sysfs_init(void)
2616 ret
= cpu_smt_state_init();
2620 ret
= sysfs_create_group(&cpu_subsys
.dev_root
->kobj
,
2621 &cpuhp_cpu_root_attr_group
);
2625 for_each_possible_cpu(cpu
) {
2626 struct device
*dev
= get_cpu_device(cpu
);
2630 ret
= sysfs_create_group(&dev
->kobj
, &cpuhp_cpu_attr_group
);
2636 device_initcall(cpuhp_sysfs_init
);
2640 * cpu_bit_bitmap[] is a special, "compressed" data structure that
2641 * represents all NR_CPUS bits binary values of 1<<nr.
2643 * It is used by cpumask_of() to get a constant address to a CPU
2644 * mask value that has a single bit set only.
2647 /* cpu_bit_bitmap[0] is empty - so we can back into it */
2648 #define MASK_DECLARE_1(x) [x+1][0] = (1UL << (x))
2649 #define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
2650 #define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
2651 #define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
2653 const unsigned long cpu_bit_bitmap
[BITS_PER_LONG
+1][BITS_TO_LONGS(NR_CPUS
)] = {
2655 MASK_DECLARE_8(0), MASK_DECLARE_8(8),
2656 MASK_DECLARE_8(16), MASK_DECLARE_8(24),
2657 #if BITS_PER_LONG > 32
2658 MASK_DECLARE_8(32), MASK_DECLARE_8(40),
2659 MASK_DECLARE_8(48), MASK_DECLARE_8(56),
2662 EXPORT_SYMBOL_GPL(cpu_bit_bitmap
);
2664 const DECLARE_BITMAP(cpu_all_bits
, NR_CPUS
) = CPU_BITS_ALL
;
2665 EXPORT_SYMBOL(cpu_all_bits
);
2667 #ifdef CONFIG_INIT_ALL_POSSIBLE
2668 struct cpumask __cpu_possible_mask __read_mostly
2671 struct cpumask __cpu_possible_mask __read_mostly
;
2673 EXPORT_SYMBOL(__cpu_possible_mask
);
2675 struct cpumask __cpu_online_mask __read_mostly
;
2676 EXPORT_SYMBOL(__cpu_online_mask
);
2678 struct cpumask __cpu_present_mask __read_mostly
;
2679 EXPORT_SYMBOL(__cpu_present_mask
);
2681 struct cpumask __cpu_active_mask __read_mostly
;
2682 EXPORT_SYMBOL(__cpu_active_mask
);
2684 void init_cpu_present(const struct cpumask
*src
)
2686 cpumask_copy(&__cpu_present_mask
, src
);
2689 void init_cpu_possible(const struct cpumask
*src
)
2691 cpumask_copy(&__cpu_possible_mask
, src
);
2694 void init_cpu_online(const struct cpumask
*src
)
2696 cpumask_copy(&__cpu_online_mask
, src
);
2700 * Activate the first processor.
2702 void __init
boot_cpu_init(void)
2704 int cpu
= smp_processor_id();
2706 /* Mark the boot cpu "present", "online" etc for SMP and UP case */
2707 set_cpu_online(cpu
, true);
2708 set_cpu_active(cpu
, true);
2709 set_cpu_present(cpu
, true);
2710 set_cpu_possible(cpu
, true);
2713 __boot_cpu_id
= cpu
;
2718 * Must be called _AFTER_ setting up the per_cpu areas
2720 void __init
boot_cpu_hotplug_init(void)
2723 this_cpu_write(cpuhp_state
.booted_once
, true);
2725 this_cpu_write(cpuhp_state
.state
, CPUHP_ONLINE
);