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.h>
11 #include <linux/unistd.h>
12 #include <linux/cpu.h>
13 #include <linux/oom.h>
14 #include <linux/rcupdate.h>
15 #include <linux/export.h>
16 #include <linux/bug.h>
17 #include <linux/kthread.h>
18 #include <linux/stop_machine.h>
19 #include <linux/mutex.h>
20 #include <linux/gfp.h>
21 #include <linux/suspend.h>
22 #include <linux/lockdep.h>
23 #include <linux/tick.h>
24 #include <linux/irq.h>
25 #include <linux/smpboot.h>
26 #include <linux/relay.h>
27 #include <linux/slab.h>
29 #include <trace/events/power.h>
30 #define CREATE_TRACE_POINTS
31 #include <trace/events/cpuhp.h>
36 * cpuhp_cpu_state - Per cpu hotplug state storage
37 * @state: The current cpu state
38 * @target: The target state
39 * @thread: Pointer to the hotplug thread
40 * @should_run: Thread should execute
41 * @rollback: Perform a rollback
42 * @single: Single callback invocation
43 * @bringup: Single callback bringup or teardown selector
44 * @cb_state: The state for a single callback (install/uninstall)
45 * @result: Result of the operation
46 * @done: Signal completion to the issuer of the task
48 struct cpuhp_cpu_state
{
49 enum cpuhp_state state
;
50 enum cpuhp_state target
;
52 struct task_struct
*thread
;
57 struct hlist_node
*node
;
58 enum cpuhp_state cb_state
;
60 struct completion done
;
64 static DEFINE_PER_CPU(struct cpuhp_cpu_state
, cpuhp_state
);
66 #if defined(CONFIG_LOCKDEP) && defined(CONFIG_SMP)
67 static struct lock_class_key cpuhp_state_key
;
68 static struct lockdep_map cpuhp_state_lock_map
=
69 STATIC_LOCKDEP_MAP_INIT("cpuhp_state", &cpuhp_state_key
);
73 * cpuhp_step - Hotplug state machine step
74 * @name: Name of the step
75 * @startup: Startup function of the step
76 * @teardown: Teardown function of the step
77 * @skip_onerr: Do not invoke the functions on error rollback
78 * Will go away once the notifiers are gone
79 * @cant_stop: Bringup/teardown can't be stopped at this step
84 int (*single
)(unsigned int cpu
);
85 int (*multi
)(unsigned int cpu
,
86 struct hlist_node
*node
);
89 int (*single
)(unsigned int cpu
);
90 int (*multi
)(unsigned int cpu
,
91 struct hlist_node
*node
);
93 struct hlist_head list
;
99 static DEFINE_MUTEX(cpuhp_state_mutex
);
100 static struct cpuhp_step cpuhp_bp_states
[];
101 static struct cpuhp_step cpuhp_ap_states
[];
103 static bool cpuhp_is_ap_state(enum cpuhp_state state
)
106 * The extra check for CPUHP_TEARDOWN_CPU is only for documentation
107 * purposes as that state is handled explicitly in cpu_down.
109 return state
> CPUHP_BRINGUP_CPU
&& state
!= CPUHP_TEARDOWN_CPU
;
112 static struct cpuhp_step
*cpuhp_get_step(enum cpuhp_state state
)
114 struct cpuhp_step
*sp
;
116 sp
= cpuhp_is_ap_state(state
) ? cpuhp_ap_states
: cpuhp_bp_states
;
121 * cpuhp_invoke_callback _ Invoke the callbacks for a given state
122 * @cpu: The cpu for which the callback should be invoked
123 * @step: The step in the state machine
124 * @bringup: True if the bringup callback should be invoked
126 * Called from cpu hotplug and from the state register machinery.
128 static int cpuhp_invoke_callback(unsigned int cpu
, enum cpuhp_state state
,
129 bool bringup
, struct hlist_node
*node
)
131 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, cpu
);
132 struct cpuhp_step
*step
= cpuhp_get_step(state
);
133 int (*cbm
)(unsigned int cpu
, struct hlist_node
*node
);
134 int (*cb
)(unsigned int cpu
);
137 if (!step
->multi_instance
) {
138 cb
= bringup
? step
->startup
.single
: step
->teardown
.single
;
141 trace_cpuhp_enter(cpu
, st
->target
, state
, cb
);
143 trace_cpuhp_exit(cpu
, st
->state
, state
, ret
);
146 cbm
= bringup
? step
->startup
.multi
: step
->teardown
.multi
;
150 /* Single invocation for instance add/remove */
152 trace_cpuhp_multi_enter(cpu
, st
->target
, state
, cbm
, node
);
153 ret
= cbm(cpu
, node
);
154 trace_cpuhp_exit(cpu
, st
->state
, state
, ret
);
158 /* State transition. Invoke on all instances */
160 hlist_for_each(node
, &step
->list
) {
161 trace_cpuhp_multi_enter(cpu
, st
->target
, state
, cbm
, node
);
162 ret
= cbm(cpu
, node
);
163 trace_cpuhp_exit(cpu
, st
->state
, state
, ret
);
170 /* Rollback the instances if one failed */
171 cbm
= !bringup
? step
->startup
.multi
: step
->teardown
.multi
;
175 hlist_for_each(node
, &step
->list
) {
184 /* Serializes the updates to cpu_online_mask, cpu_present_mask */
185 static DEFINE_MUTEX(cpu_add_remove_lock
);
186 bool cpuhp_tasks_frozen
;
187 EXPORT_SYMBOL_GPL(cpuhp_tasks_frozen
);
190 * The following two APIs (cpu_maps_update_begin/done) must be used when
191 * attempting to serialize the updates to cpu_online_mask & cpu_present_mask.
192 * The APIs cpu_notifier_register_begin/done() must be used to protect CPU
193 * hotplug callback (un)registration performed using __register_cpu_notifier()
194 * or __unregister_cpu_notifier().
196 void cpu_maps_update_begin(void)
198 mutex_lock(&cpu_add_remove_lock
);
200 EXPORT_SYMBOL(cpu_notifier_register_begin
);
202 void cpu_maps_update_done(void)
204 mutex_unlock(&cpu_add_remove_lock
);
206 EXPORT_SYMBOL(cpu_notifier_register_done
);
208 static RAW_NOTIFIER_HEAD(cpu_chain
);
210 /* If set, cpu_up and cpu_down will return -EBUSY and do nothing.
211 * Should always be manipulated under cpu_add_remove_lock
213 static int cpu_hotplug_disabled
;
215 #ifdef CONFIG_HOTPLUG_CPU
218 struct task_struct
*active_writer
;
219 /* wait queue to wake up the active_writer */
220 wait_queue_head_t wq
;
221 /* verifies that no writer will get active while readers are active */
224 * Also blocks the new readers during
225 * an ongoing cpu hotplug operation.
229 #ifdef CONFIG_DEBUG_LOCK_ALLOC
230 struct lockdep_map dep_map
;
233 .active_writer
= NULL
,
234 .wq
= __WAIT_QUEUE_HEAD_INITIALIZER(cpu_hotplug
.wq
),
235 .lock
= __MUTEX_INITIALIZER(cpu_hotplug
.lock
),
236 #ifdef CONFIG_DEBUG_LOCK_ALLOC
237 .dep_map
= STATIC_LOCKDEP_MAP_INIT("cpu_hotplug.dep_map", &cpu_hotplug
.dep_map
),
241 /* Lockdep annotations for get/put_online_cpus() and cpu_hotplug_begin/end() */
242 #define cpuhp_lock_acquire_read() lock_map_acquire_read(&cpu_hotplug.dep_map)
243 #define cpuhp_lock_acquire_tryread() \
244 lock_map_acquire_tryread(&cpu_hotplug.dep_map)
245 #define cpuhp_lock_acquire() lock_map_acquire(&cpu_hotplug.dep_map)
246 #define cpuhp_lock_release() lock_map_release(&cpu_hotplug.dep_map)
249 void get_online_cpus(void)
252 if (cpu_hotplug
.active_writer
== current
)
254 cpuhp_lock_acquire_read();
255 mutex_lock(&cpu_hotplug
.lock
);
256 atomic_inc(&cpu_hotplug
.refcount
);
257 mutex_unlock(&cpu_hotplug
.lock
);
259 EXPORT_SYMBOL_GPL(get_online_cpus
);
261 void put_online_cpus(void)
265 if (cpu_hotplug
.active_writer
== current
)
268 refcount
= atomic_dec_return(&cpu_hotplug
.refcount
);
269 if (WARN_ON(refcount
< 0)) /* try to fix things up */
270 atomic_inc(&cpu_hotplug
.refcount
);
272 if (refcount
<= 0 && waitqueue_active(&cpu_hotplug
.wq
))
273 wake_up(&cpu_hotplug
.wq
);
275 cpuhp_lock_release();
278 EXPORT_SYMBOL_GPL(put_online_cpus
);
281 * This ensures that the hotplug operation can begin only when the
282 * refcount goes to zero.
284 * Note that during a cpu-hotplug operation, the new readers, if any,
285 * will be blocked by the cpu_hotplug.lock
287 * Since cpu_hotplug_begin() is always called after invoking
288 * cpu_maps_update_begin(), we can be sure that only one writer is active.
290 * Note that theoretically, there is a possibility of a livelock:
291 * - Refcount goes to zero, last reader wakes up the sleeping
293 * - Last reader unlocks the cpu_hotplug.lock.
294 * - A new reader arrives at this moment, bumps up the refcount.
295 * - The writer acquires the cpu_hotplug.lock finds the refcount
296 * non zero and goes to sleep again.
298 * However, this is very difficult to achieve in practice since
299 * get_online_cpus() not an api which is called all that often.
302 void cpu_hotplug_begin(void)
306 cpu_hotplug
.active_writer
= current
;
307 cpuhp_lock_acquire();
310 mutex_lock(&cpu_hotplug
.lock
);
311 prepare_to_wait(&cpu_hotplug
.wq
, &wait
, TASK_UNINTERRUPTIBLE
);
312 if (likely(!atomic_read(&cpu_hotplug
.refcount
)))
314 mutex_unlock(&cpu_hotplug
.lock
);
317 finish_wait(&cpu_hotplug
.wq
, &wait
);
320 void cpu_hotplug_done(void)
322 cpu_hotplug
.active_writer
= NULL
;
323 mutex_unlock(&cpu_hotplug
.lock
);
324 cpuhp_lock_release();
328 * Wait for currently running CPU hotplug operations to complete (if any) and
329 * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
330 * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the
331 * hotplug path before performing hotplug operations. So acquiring that lock
332 * guarantees mutual exclusion from any currently running hotplug operations.
334 void cpu_hotplug_disable(void)
336 cpu_maps_update_begin();
337 cpu_hotplug_disabled
++;
338 cpu_maps_update_done();
340 EXPORT_SYMBOL_GPL(cpu_hotplug_disable
);
342 static void __cpu_hotplug_enable(void)
344 if (WARN_ONCE(!cpu_hotplug_disabled
, "Unbalanced cpu hotplug enable\n"))
346 cpu_hotplug_disabled
--;
349 void cpu_hotplug_enable(void)
351 cpu_maps_update_begin();
352 __cpu_hotplug_enable();
353 cpu_maps_update_done();
355 EXPORT_SYMBOL_GPL(cpu_hotplug_enable
);
356 #endif /* CONFIG_HOTPLUG_CPU */
358 /* Need to know about CPUs going up/down? */
359 int register_cpu_notifier(struct notifier_block
*nb
)
362 cpu_maps_update_begin();
363 ret
= raw_notifier_chain_register(&cpu_chain
, nb
);
364 cpu_maps_update_done();
368 int __register_cpu_notifier(struct notifier_block
*nb
)
370 return raw_notifier_chain_register(&cpu_chain
, nb
);
373 static int __cpu_notify(unsigned long val
, unsigned int cpu
, int nr_to_call
,
376 unsigned long mod
= cpuhp_tasks_frozen
? CPU_TASKS_FROZEN
: 0;
377 void *hcpu
= (void *)(long)cpu
;
381 ret
= __raw_notifier_call_chain(&cpu_chain
, val
| mod
, hcpu
, nr_to_call
,
384 return notifier_to_errno(ret
);
387 static int cpu_notify(unsigned long val
, unsigned int cpu
)
389 return __cpu_notify(val
, cpu
, -1, NULL
);
392 static void cpu_notify_nofail(unsigned long val
, unsigned int cpu
)
394 BUG_ON(cpu_notify(val
, cpu
));
397 /* Notifier wrappers for transitioning to state machine */
398 static int notify_prepare(unsigned int cpu
)
403 ret
= __cpu_notify(CPU_UP_PREPARE
, cpu
, -1, &nr_calls
);
406 printk(KERN_WARNING
"%s: attempt to bring up CPU %u failed\n",
408 __cpu_notify(CPU_UP_CANCELED
, cpu
, nr_calls
, NULL
);
413 static int notify_online(unsigned int cpu
)
415 cpu_notify(CPU_ONLINE
, cpu
);
419 static void __cpuhp_kick_ap_work(struct cpuhp_cpu_state
*st
);
421 static int bringup_wait_for_ap(unsigned int cpu
)
423 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, cpu
);
425 /* Wait for the CPU to reach CPUHP_AP_ONLINE_IDLE */
426 wait_for_completion(&st
->done
);
427 if (WARN_ON_ONCE((!cpu_online(cpu
))))
430 /* Unpark the stopper thread and the hotplug thread of the target cpu */
431 stop_machine_unpark(cpu
);
432 kthread_unpark(st
->thread
);
434 /* Should we go further up ? */
435 if (st
->target
> CPUHP_AP_ONLINE_IDLE
) {
436 __cpuhp_kick_ap_work(st
);
437 wait_for_completion(&st
->done
);
442 static int bringup_cpu(unsigned int cpu
)
444 struct task_struct
*idle
= idle_thread_get(cpu
);
448 * Some architectures have to walk the irq descriptors to
449 * setup the vector space for the cpu which comes online.
450 * Prevent irq alloc/free across the bringup.
454 /* Arch-specific enabling code. */
455 ret
= __cpu_up(cpu
, idle
);
458 cpu_notify(CPU_UP_CANCELED
, cpu
);
461 return bringup_wait_for_ap(cpu
);
465 * Hotplug state machine related functions
467 static void undo_cpu_down(unsigned int cpu
, struct cpuhp_cpu_state
*st
)
469 for (st
->state
++; st
->state
< st
->target
; st
->state
++) {
470 struct cpuhp_step
*step
= cpuhp_get_step(st
->state
);
472 if (!step
->skip_onerr
)
473 cpuhp_invoke_callback(cpu
, st
->state
, true, NULL
);
477 static int cpuhp_down_callbacks(unsigned int cpu
, struct cpuhp_cpu_state
*st
,
478 enum cpuhp_state target
)
480 enum cpuhp_state prev_state
= st
->state
;
483 for (; st
->state
> target
; st
->state
--) {
484 ret
= cpuhp_invoke_callback(cpu
, st
->state
, false, NULL
);
486 st
->target
= prev_state
;
487 undo_cpu_down(cpu
, st
);
494 static void undo_cpu_up(unsigned int cpu
, struct cpuhp_cpu_state
*st
)
496 for (st
->state
--; st
->state
> st
->target
; st
->state
--) {
497 struct cpuhp_step
*step
= cpuhp_get_step(st
->state
);
499 if (!step
->skip_onerr
)
500 cpuhp_invoke_callback(cpu
, st
->state
, false, NULL
);
504 static int cpuhp_up_callbacks(unsigned int cpu
, struct cpuhp_cpu_state
*st
,
505 enum cpuhp_state target
)
507 enum cpuhp_state prev_state
= st
->state
;
510 while (st
->state
< target
) {
512 ret
= cpuhp_invoke_callback(cpu
, st
->state
, true, NULL
);
514 st
->target
= prev_state
;
515 undo_cpu_up(cpu
, st
);
523 * The cpu hotplug threads manage the bringup and teardown of the cpus
525 static void cpuhp_create(unsigned int cpu
)
527 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, cpu
);
529 init_completion(&st
->done
);
532 static int cpuhp_should_run(unsigned int cpu
)
534 struct cpuhp_cpu_state
*st
= this_cpu_ptr(&cpuhp_state
);
536 return st
->should_run
;
539 /* Execute the teardown callbacks. Used to be CPU_DOWN_PREPARE */
540 static int cpuhp_ap_offline(unsigned int cpu
, struct cpuhp_cpu_state
*st
)
542 enum cpuhp_state target
= max((int)st
->target
, CPUHP_TEARDOWN_CPU
);
544 return cpuhp_down_callbacks(cpu
, st
, target
);
547 /* Execute the online startup callbacks. Used to be CPU_ONLINE */
548 static int cpuhp_ap_online(unsigned int cpu
, struct cpuhp_cpu_state
*st
)
550 return cpuhp_up_callbacks(cpu
, st
, st
->target
);
554 * Execute teardown/startup callbacks on the plugged cpu. Also used to invoke
555 * callbacks when a state gets [un]installed at runtime.
557 static void cpuhp_thread_fun(unsigned int cpu
)
559 struct cpuhp_cpu_state
*st
= this_cpu_ptr(&cpuhp_state
);
563 * Paired with the mb() in cpuhp_kick_ap_work and
564 * cpuhp_invoke_ap_callback, so the work set is consistent visible.
570 st
->should_run
= false;
572 lock_map_acquire(&cpuhp_state_lock_map
);
573 /* Single callback invocation for [un]install ? */
575 if (st
->cb_state
< CPUHP_AP_ONLINE
) {
577 ret
= cpuhp_invoke_callback(cpu
, st
->cb_state
,
578 st
->bringup
, st
->node
);
581 ret
= cpuhp_invoke_callback(cpu
, st
->cb_state
,
582 st
->bringup
, st
->node
);
584 } else if (st
->rollback
) {
585 BUG_ON(st
->state
< CPUHP_AP_ONLINE_IDLE
);
587 undo_cpu_down(cpu
, st
);
589 * This is a momentary workaround to keep the notifier users
590 * happy. Will go away once we got rid of the notifiers.
592 cpu_notify_nofail(CPU_DOWN_FAILED
, cpu
);
593 st
->rollback
= false;
595 /* Cannot happen .... */
596 BUG_ON(st
->state
< CPUHP_AP_ONLINE_IDLE
);
598 /* Regular hotplug work */
599 if (st
->state
< st
->target
)
600 ret
= cpuhp_ap_online(cpu
, st
);
601 else if (st
->state
> st
->target
)
602 ret
= cpuhp_ap_offline(cpu
, st
);
604 lock_map_release(&cpuhp_state_lock_map
);
609 /* Invoke a single callback on a remote cpu */
611 cpuhp_invoke_ap_callback(int cpu
, enum cpuhp_state state
, bool bringup
,
612 struct hlist_node
*node
)
614 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, cpu
);
616 if (!cpu_online(cpu
))
619 lock_map_acquire(&cpuhp_state_lock_map
);
620 lock_map_release(&cpuhp_state_lock_map
);
623 * If we are up and running, use the hotplug thread. For early calls
624 * we invoke the thread function directly.
627 return cpuhp_invoke_callback(cpu
, state
, bringup
, node
);
629 st
->cb_state
= state
;
631 st
->bringup
= bringup
;
635 * Make sure the above stores are visible before should_run becomes
636 * true. Paired with the mb() above in cpuhp_thread_fun()
639 st
->should_run
= true;
640 wake_up_process(st
->thread
);
641 wait_for_completion(&st
->done
);
645 /* Regular hotplug invocation of the AP hotplug thread */
646 static void __cpuhp_kick_ap_work(struct cpuhp_cpu_state
*st
)
651 * Make sure the above stores are visible before should_run becomes
652 * true. Paired with the mb() above in cpuhp_thread_fun()
655 st
->should_run
= true;
656 wake_up_process(st
->thread
);
659 static int cpuhp_kick_ap_work(unsigned int cpu
)
661 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, cpu
);
662 enum cpuhp_state state
= st
->state
;
664 trace_cpuhp_enter(cpu
, st
->target
, state
, cpuhp_kick_ap_work
);
665 lock_map_acquire(&cpuhp_state_lock_map
);
666 lock_map_release(&cpuhp_state_lock_map
);
667 __cpuhp_kick_ap_work(st
);
668 wait_for_completion(&st
->done
);
669 trace_cpuhp_exit(cpu
, st
->state
, state
, st
->result
);
673 static struct smp_hotplug_thread cpuhp_threads
= {
674 .store
= &cpuhp_state
.thread
,
675 .create
= &cpuhp_create
,
676 .thread_should_run
= cpuhp_should_run
,
677 .thread_fn
= cpuhp_thread_fun
,
678 .thread_comm
= "cpuhp/%u",
682 void __init
cpuhp_threads_init(void)
684 BUG_ON(smpboot_register_percpu_thread(&cpuhp_threads
));
685 kthread_unpark(this_cpu_read(cpuhp_state
.thread
));
688 EXPORT_SYMBOL(register_cpu_notifier
);
689 EXPORT_SYMBOL(__register_cpu_notifier
);
690 void unregister_cpu_notifier(struct notifier_block
*nb
)
692 cpu_maps_update_begin();
693 raw_notifier_chain_unregister(&cpu_chain
, nb
);
694 cpu_maps_update_done();
696 EXPORT_SYMBOL(unregister_cpu_notifier
);
698 void __unregister_cpu_notifier(struct notifier_block
*nb
)
700 raw_notifier_chain_unregister(&cpu_chain
, nb
);
702 EXPORT_SYMBOL(__unregister_cpu_notifier
);
704 #ifdef CONFIG_HOTPLUG_CPU
706 * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU
709 * This function walks all processes, finds a valid mm struct for each one and
710 * then clears a corresponding bit in mm's cpumask. While this all sounds
711 * trivial, there are various non-obvious corner cases, which this function
712 * tries to solve in a safe manner.
714 * Also note that the function uses a somewhat relaxed locking scheme, so it may
715 * be called only for an already offlined CPU.
717 void clear_tasks_mm_cpumask(int cpu
)
719 struct task_struct
*p
;
722 * This function is called after the cpu is taken down and marked
723 * offline, so its not like new tasks will ever get this cpu set in
724 * their mm mask. -- Peter Zijlstra
725 * Thus, we may use rcu_read_lock() here, instead of grabbing
726 * full-fledged tasklist_lock.
728 WARN_ON(cpu_online(cpu
));
730 for_each_process(p
) {
731 struct task_struct
*t
;
734 * Main thread might exit, but other threads may still have
735 * a valid mm. Find one.
737 t
= find_lock_task_mm(p
);
740 cpumask_clear_cpu(cpu
, mm_cpumask(t
->mm
));
746 static inline void check_for_tasks(int dead_cpu
)
748 struct task_struct
*g
, *p
;
750 read_lock(&tasklist_lock
);
751 for_each_process_thread(g
, p
) {
755 * We do the check with unlocked task_rq(p)->lock.
756 * Order the reading to do not warn about a task,
757 * which was running on this cpu in the past, and
758 * it's just been woken on another cpu.
761 if (task_cpu(p
) != dead_cpu
)
764 pr_warn("Task %s (pid=%d) is on cpu %d (state=%ld, flags=%x)\n",
765 p
->comm
, task_pid_nr(p
), dead_cpu
, p
->state
, p
->flags
);
767 read_unlock(&tasklist_lock
);
770 static int notify_down_prepare(unsigned int cpu
)
772 int err
, nr_calls
= 0;
774 err
= __cpu_notify(CPU_DOWN_PREPARE
, cpu
, -1, &nr_calls
);
777 __cpu_notify(CPU_DOWN_FAILED
, cpu
, nr_calls
, NULL
);
778 pr_warn("%s: attempt to take down CPU %u failed\n",
784 /* Take this CPU down. */
785 static int take_cpu_down(void *_param
)
787 struct cpuhp_cpu_state
*st
= this_cpu_ptr(&cpuhp_state
);
788 enum cpuhp_state target
= max((int)st
->target
, CPUHP_AP_OFFLINE
);
789 int err
, cpu
= smp_processor_id();
791 /* Ensure this CPU doesn't handle any more interrupts. */
792 err
= __cpu_disable();
797 * We get here while we are in CPUHP_TEARDOWN_CPU state and we must not
798 * do this step again.
800 WARN_ON(st
->state
!= CPUHP_TEARDOWN_CPU
);
802 /* Invoke the former CPU_DYING callbacks */
803 for (; st
->state
> target
; st
->state
--)
804 cpuhp_invoke_callback(cpu
, st
->state
, false, NULL
);
806 /* Give up timekeeping duties */
807 tick_handover_do_timer();
808 /* Park the stopper thread */
809 stop_machine_park(cpu
);
813 static int takedown_cpu(unsigned int cpu
)
815 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, cpu
);
818 /* Park the smpboot threads */
819 kthread_park(per_cpu_ptr(&cpuhp_state
, cpu
)->thread
);
820 smpboot_park_threads(cpu
);
823 * Prevent irq alloc/free while the dying cpu reorganizes the
824 * interrupt affinities.
829 * So now all preempt/rcu users must observe !cpu_active().
831 err
= stop_machine(take_cpu_down
, NULL
, cpumask_of(cpu
));
833 /* CPU refused to die */
835 /* Unpark the hotplug thread so we can rollback there */
836 kthread_unpark(per_cpu_ptr(&cpuhp_state
, cpu
)->thread
);
839 BUG_ON(cpu_online(cpu
));
842 * The CPUHP_AP_SCHED_MIGRATE_DYING callback will have removed all
843 * runnable tasks from the cpu, there's only the idle task left now
844 * that the migration thread is done doing the stop_machine thing.
846 * Wait for the stop thread to go away.
848 wait_for_completion(&st
->done
);
849 BUG_ON(st
->state
!= CPUHP_AP_IDLE_DEAD
);
851 /* Interrupts are moved away from the dying cpu, reenable alloc/free */
854 hotplug_cpu__broadcast_tick_pull(cpu
);
855 /* This actually kills the CPU. */
858 tick_cleanup_dead_cpu(cpu
);
862 static int notify_dead(unsigned int cpu
)
864 cpu_notify_nofail(CPU_DEAD
, cpu
);
865 check_for_tasks(cpu
);
869 static void cpuhp_complete_idle_dead(void *arg
)
871 struct cpuhp_cpu_state
*st
= arg
;
876 void cpuhp_report_idle_dead(void)
878 struct cpuhp_cpu_state
*st
= this_cpu_ptr(&cpuhp_state
);
880 BUG_ON(st
->state
!= CPUHP_AP_OFFLINE
);
881 rcu_report_dead(smp_processor_id());
882 st
->state
= CPUHP_AP_IDLE_DEAD
;
884 * We cannot call complete after rcu_report_dead() so we delegate it
887 smp_call_function_single(cpumask_first(cpu_online_mask
),
888 cpuhp_complete_idle_dead
, st
, 0);
892 #define notify_down_prepare NULL
893 #define takedown_cpu NULL
894 #define notify_dead NULL
897 #ifdef CONFIG_HOTPLUG_CPU
899 /* Requires cpu_add_remove_lock to be held */
900 static int __ref
_cpu_down(unsigned int cpu
, int tasks_frozen
,
901 enum cpuhp_state target
)
903 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, cpu
);
904 int prev_state
, ret
= 0;
905 bool hasdied
= false;
907 if (num_online_cpus() == 1)
910 if (!cpu_present(cpu
))
915 cpuhp_tasks_frozen
= tasks_frozen
;
917 prev_state
= st
->state
;
920 * If the current CPU state is in the range of the AP hotplug thread,
921 * then we need to kick the thread.
923 if (st
->state
> CPUHP_TEARDOWN_CPU
) {
924 ret
= cpuhp_kick_ap_work(cpu
);
926 * The AP side has done the error rollback already. Just
927 * return the error code..
933 * We might have stopped still in the range of the AP hotplug
934 * thread. Nothing to do anymore.
936 if (st
->state
> CPUHP_TEARDOWN_CPU
)
940 * The AP brought itself down to CPUHP_TEARDOWN_CPU. So we need
941 * to do the further cleanups.
943 ret
= cpuhp_down_callbacks(cpu
, st
, target
);
944 if (ret
&& st
->state
> CPUHP_TEARDOWN_CPU
&& st
->state
< prev_state
) {
945 st
->target
= prev_state
;
947 cpuhp_kick_ap_work(cpu
);
950 hasdied
= prev_state
!= st
->state
&& st
->state
== CPUHP_OFFLINE
;
953 /* This post dead nonsense must die */
955 cpu_notify_nofail(CPU_POST_DEAD
, cpu
);
959 static int do_cpu_down(unsigned int cpu
, enum cpuhp_state target
)
963 cpu_maps_update_begin();
965 if (cpu_hotplug_disabled
) {
970 err
= _cpu_down(cpu
, 0, target
);
973 cpu_maps_update_done();
976 int cpu_down(unsigned int cpu
)
978 return do_cpu_down(cpu
, CPUHP_OFFLINE
);
980 EXPORT_SYMBOL(cpu_down
);
981 #endif /*CONFIG_HOTPLUG_CPU*/
984 * notify_cpu_starting(cpu) - Invoke the callbacks on the starting CPU
985 * @cpu: cpu that just started
987 * It must be called by the arch code on the new cpu, before the new cpu
988 * enables interrupts and before the "boot" cpu returns from __cpu_up().
990 void notify_cpu_starting(unsigned int cpu
)
992 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, cpu
);
993 enum cpuhp_state target
= min((int)st
->target
, CPUHP_AP_ONLINE
);
995 rcu_cpu_starting(cpu
); /* Enables RCU usage on this CPU. */
996 while (st
->state
< target
) {
998 cpuhp_invoke_callback(cpu
, st
->state
, true, NULL
);
1003 * Called from the idle task. Wake up the controlling task which brings the
1004 * stopper and the hotplug thread of the upcoming CPU up and then delegates
1005 * the rest of the online bringup to the hotplug thread.
1007 void cpuhp_online_idle(enum cpuhp_state state
)
1009 struct cpuhp_cpu_state
*st
= this_cpu_ptr(&cpuhp_state
);
1011 /* Happens for the boot cpu */
1012 if (state
!= CPUHP_AP_ONLINE_IDLE
)
1015 st
->state
= CPUHP_AP_ONLINE_IDLE
;
1016 complete(&st
->done
);
1019 /* Requires cpu_add_remove_lock to be held */
1020 static int _cpu_up(unsigned int cpu
, int tasks_frozen
, enum cpuhp_state target
)
1022 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, cpu
);
1023 struct task_struct
*idle
;
1026 cpu_hotplug_begin();
1028 if (!cpu_present(cpu
)) {
1034 * The caller of do_cpu_up might have raced with another
1035 * caller. Ignore it for now.
1037 if (st
->state
>= target
)
1040 if (st
->state
== CPUHP_OFFLINE
) {
1041 /* Let it fail before we try to bring the cpu up */
1042 idle
= idle_thread_get(cpu
);
1044 ret
= PTR_ERR(idle
);
1049 cpuhp_tasks_frozen
= tasks_frozen
;
1051 st
->target
= target
;
1053 * If the current CPU state is in the range of the AP hotplug thread,
1054 * then we need to kick the thread once more.
1056 if (st
->state
> CPUHP_BRINGUP_CPU
) {
1057 ret
= cpuhp_kick_ap_work(cpu
);
1059 * The AP side has done the error rollback already. Just
1060 * return the error code..
1067 * Try to reach the target state. We max out on the BP at
1068 * CPUHP_BRINGUP_CPU. After that the AP hotplug thread is
1069 * responsible for bringing it up to the target state.
1071 target
= min((int)target
, CPUHP_BRINGUP_CPU
);
1072 ret
= cpuhp_up_callbacks(cpu
, st
, target
);
1078 static int do_cpu_up(unsigned int cpu
, enum cpuhp_state target
)
1082 if (!cpu_possible(cpu
)) {
1083 pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n",
1085 #if defined(CONFIG_IA64)
1086 pr_err("please check additional_cpus= boot parameter\n");
1091 err
= try_online_node(cpu_to_node(cpu
));
1095 cpu_maps_update_begin();
1097 if (cpu_hotplug_disabled
) {
1102 err
= _cpu_up(cpu
, 0, target
);
1104 cpu_maps_update_done();
1108 int cpu_up(unsigned int cpu
)
1110 return do_cpu_up(cpu
, CPUHP_ONLINE
);
1112 EXPORT_SYMBOL_GPL(cpu_up
);
1114 #ifdef CONFIG_PM_SLEEP_SMP
1115 static cpumask_var_t frozen_cpus
;
1117 int freeze_secondary_cpus(int primary
)
1121 cpu_maps_update_begin();
1122 if (!cpu_online(primary
))
1123 primary
= cpumask_first(cpu_online_mask
);
1125 * We take down all of the non-boot CPUs in one shot to avoid races
1126 * with the userspace trying to use the CPU hotplug at the same time
1128 cpumask_clear(frozen_cpus
);
1130 pr_info("Disabling non-boot CPUs ...\n");
1131 for_each_online_cpu(cpu
) {
1134 trace_suspend_resume(TPS("CPU_OFF"), cpu
, true);
1135 error
= _cpu_down(cpu
, 1, CPUHP_OFFLINE
);
1136 trace_suspend_resume(TPS("CPU_OFF"), cpu
, false);
1138 cpumask_set_cpu(cpu
, frozen_cpus
);
1140 pr_err("Error taking CPU%d down: %d\n", cpu
, error
);
1146 BUG_ON(num_online_cpus() > 1);
1148 pr_err("Non-boot CPUs are not disabled\n");
1151 * Make sure the CPUs won't be enabled by someone else. We need to do
1152 * this even in case of failure as all disable_nonboot_cpus() users are
1153 * supposed to do enable_nonboot_cpus() on the failure path.
1155 cpu_hotplug_disabled
++;
1157 cpu_maps_update_done();
1161 void __weak
arch_enable_nonboot_cpus_begin(void)
1165 void __weak
arch_enable_nonboot_cpus_end(void)
1169 void enable_nonboot_cpus(void)
1173 /* Allow everyone to use the CPU hotplug again */
1174 cpu_maps_update_begin();
1175 __cpu_hotplug_enable();
1176 if (cpumask_empty(frozen_cpus
))
1179 pr_info("Enabling non-boot CPUs ...\n");
1181 arch_enable_nonboot_cpus_begin();
1183 for_each_cpu(cpu
, frozen_cpus
) {
1184 trace_suspend_resume(TPS("CPU_ON"), cpu
, true);
1185 error
= _cpu_up(cpu
, 1, CPUHP_ONLINE
);
1186 trace_suspend_resume(TPS("CPU_ON"), cpu
, false);
1188 pr_info("CPU%d is up\n", cpu
);
1191 pr_warn("Error taking CPU%d up: %d\n", cpu
, error
);
1194 arch_enable_nonboot_cpus_end();
1196 cpumask_clear(frozen_cpus
);
1198 cpu_maps_update_done();
1201 static int __init
alloc_frozen_cpus(void)
1203 if (!alloc_cpumask_var(&frozen_cpus
, GFP_KERNEL
|__GFP_ZERO
))
1207 core_initcall(alloc_frozen_cpus
);
1210 * When callbacks for CPU hotplug notifications are being executed, we must
1211 * ensure that the state of the system with respect to the tasks being frozen
1212 * or not, as reported by the notification, remains unchanged *throughout the
1213 * duration* of the execution of the callbacks.
1214 * Hence we need to prevent the freezer from racing with regular CPU hotplug.
1216 * This synchronization is implemented by mutually excluding regular CPU
1217 * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
1218 * Hibernate notifications.
1221 cpu_hotplug_pm_callback(struct notifier_block
*nb
,
1222 unsigned long action
, void *ptr
)
1226 case PM_SUSPEND_PREPARE
:
1227 case PM_HIBERNATION_PREPARE
:
1228 cpu_hotplug_disable();
1231 case PM_POST_SUSPEND
:
1232 case PM_POST_HIBERNATION
:
1233 cpu_hotplug_enable();
1244 static int __init
cpu_hotplug_pm_sync_init(void)
1247 * cpu_hotplug_pm_callback has higher priority than x86
1248 * bsp_pm_callback which depends on cpu_hotplug_pm_callback
1249 * to disable cpu hotplug to avoid cpu hotplug race.
1251 pm_notifier(cpu_hotplug_pm_callback
, 0);
1254 core_initcall(cpu_hotplug_pm_sync_init
);
1256 #endif /* CONFIG_PM_SLEEP_SMP */
1258 #endif /* CONFIG_SMP */
1260 /* Boot processor state steps */
1261 static struct cpuhp_step cpuhp_bp_states
[] = {
1264 .startup
.single
= NULL
,
1265 .teardown
.single
= NULL
,
1268 [CPUHP_CREATE_THREADS
]= {
1269 .name
= "threads:prepare",
1270 .startup
.single
= smpboot_create_threads
,
1271 .teardown
.single
= NULL
,
1274 [CPUHP_PERF_PREPARE
] = {
1275 .name
= "perf:prepare",
1276 .startup
.single
= perf_event_init_cpu
,
1277 .teardown
.single
= perf_event_exit_cpu
,
1279 [CPUHP_WORKQUEUE_PREP
] = {
1280 .name
= "workqueue:prepare",
1281 .startup
.single
= workqueue_prepare_cpu
,
1282 .teardown
.single
= NULL
,
1284 [CPUHP_HRTIMERS_PREPARE
] = {
1285 .name
= "hrtimers:prepare",
1286 .startup
.single
= hrtimers_prepare_cpu
,
1287 .teardown
.single
= hrtimers_dead_cpu
,
1289 [CPUHP_SMPCFD_PREPARE
] = {
1290 .name
= "smpcfd:prepare",
1291 .startup
.single
= smpcfd_prepare_cpu
,
1292 .teardown
.single
= smpcfd_dead_cpu
,
1294 [CPUHP_RELAY_PREPARE
] = {
1295 .name
= "relay:prepare",
1296 .startup
.single
= relay_prepare_cpu
,
1297 .teardown
.single
= NULL
,
1299 [CPUHP_SLAB_PREPARE
] = {
1300 .name
= "slab:prepare",
1301 .startup
.single
= slab_prepare_cpu
,
1302 .teardown
.single
= slab_dead_cpu
,
1304 [CPUHP_RCUTREE_PREP
] = {
1305 .name
= "RCU/tree:prepare",
1306 .startup
.single
= rcutree_prepare_cpu
,
1307 .teardown
.single
= rcutree_dead_cpu
,
1310 * Preparatory and dead notifiers. Will be replaced once the notifiers
1311 * are converted to states.
1313 [CPUHP_NOTIFY_PREPARE
] = {
1314 .name
= "notify:prepare",
1315 .startup
.single
= notify_prepare
,
1316 .teardown
.single
= notify_dead
,
1321 * On the tear-down path, timers_dead_cpu() must be invoked
1322 * before blk_mq_queue_reinit_notify() from notify_dead(),
1323 * otherwise a RCU stall occurs.
1325 [CPUHP_TIMERS_PREPARE
] = {
1326 .name
= "timers:dead",
1327 .startup
.single
= timers_prepare_cpu
,
1328 .teardown
.single
= timers_dead_cpu
,
1330 /* Kicks the plugged cpu into life */
1331 [CPUHP_BRINGUP_CPU
] = {
1332 .name
= "cpu:bringup",
1333 .startup
.single
= bringup_cpu
,
1334 .teardown
.single
= NULL
,
1338 * Handled on controll processor until the plugged processor manages
1341 [CPUHP_TEARDOWN_CPU
] = {
1342 .name
= "cpu:teardown",
1343 .startup
.single
= NULL
,
1344 .teardown
.single
= takedown_cpu
,
1348 [CPUHP_BRINGUP_CPU
] = { },
1352 /* Application processor state steps */
1353 static struct cpuhp_step cpuhp_ap_states
[] = {
1355 /* Final state before CPU kills itself */
1356 [CPUHP_AP_IDLE_DEAD
] = {
1357 .name
= "idle:dead",
1360 * Last state before CPU enters the idle loop to die. Transient state
1361 * for synchronization.
1363 [CPUHP_AP_OFFLINE
] = {
1364 .name
= "ap:offline",
1367 /* First state is scheduler control. Interrupts are disabled */
1368 [CPUHP_AP_SCHED_STARTING
] = {
1369 .name
= "sched:starting",
1370 .startup
.single
= sched_cpu_starting
,
1371 .teardown
.single
= sched_cpu_dying
,
1373 [CPUHP_AP_RCUTREE_DYING
] = {
1374 .name
= "RCU/tree:dying",
1375 .startup
.single
= NULL
,
1376 .teardown
.single
= rcutree_dying_cpu
,
1378 [CPUHP_AP_SMPCFD_DYING
] = {
1379 .name
= "smpcfd:dying",
1380 .startup
.single
= NULL
,
1381 .teardown
.single
= smpcfd_dying_cpu
,
1383 /* Entry state on starting. Interrupts enabled from here on. Transient
1384 * state for synchronsization */
1385 [CPUHP_AP_ONLINE
] = {
1386 .name
= "ap:online",
1388 /* Handle smpboot threads park/unpark */
1389 [CPUHP_AP_SMPBOOT_THREADS
] = {
1390 .name
= "smpboot/threads:online",
1391 .startup
.single
= smpboot_unpark_threads
,
1392 .teardown
.single
= NULL
,
1394 [CPUHP_AP_PERF_ONLINE
] = {
1395 .name
= "perf:online",
1396 .startup
.single
= perf_event_init_cpu
,
1397 .teardown
.single
= perf_event_exit_cpu
,
1399 [CPUHP_AP_WORKQUEUE_ONLINE
] = {
1400 .name
= "workqueue:online",
1401 .startup
.single
= workqueue_online_cpu
,
1402 .teardown
.single
= workqueue_offline_cpu
,
1404 [CPUHP_AP_RCUTREE_ONLINE
] = {
1405 .name
= "RCU/tree:online",
1406 .startup
.single
= rcutree_online_cpu
,
1407 .teardown
.single
= rcutree_offline_cpu
,
1411 * Online/down_prepare notifiers. Will be removed once the notifiers
1412 * are converted to states.
1414 [CPUHP_AP_NOTIFY_ONLINE
] = {
1415 .name
= "notify:online",
1416 .startup
.single
= notify_online
,
1417 .teardown
.single
= notify_down_prepare
,
1422 * The dynamically registered state space is here
1426 /* Last state is scheduler control setting the cpu active */
1427 [CPUHP_AP_ACTIVE
] = {
1428 .name
= "sched:active",
1429 .startup
.single
= sched_cpu_activate
,
1430 .teardown
.single
= sched_cpu_deactivate
,
1434 /* CPU is fully up and running. */
1437 .startup
.single
= NULL
,
1438 .teardown
.single
= NULL
,
1442 /* Sanity check for callbacks */
1443 static int cpuhp_cb_check(enum cpuhp_state state
)
1445 if (state
<= CPUHP_OFFLINE
|| state
>= CPUHP_ONLINE
)
1450 static void cpuhp_store_callbacks(enum cpuhp_state state
,
1452 int (*startup
)(unsigned int cpu
),
1453 int (*teardown
)(unsigned int cpu
),
1454 bool multi_instance
)
1456 /* (Un)Install the callbacks for further cpu hotplug operations */
1457 struct cpuhp_step
*sp
;
1459 sp
= cpuhp_get_step(state
);
1460 sp
->startup
.single
= startup
;
1461 sp
->teardown
.single
= teardown
;
1463 sp
->multi_instance
= multi_instance
;
1464 INIT_HLIST_HEAD(&sp
->list
);
1467 static void *cpuhp_get_teardown_cb(enum cpuhp_state state
)
1469 return cpuhp_get_step(state
)->teardown
.single
;
1473 * Call the startup/teardown function for a step either on the AP or
1474 * on the current CPU.
1476 static int cpuhp_issue_call(int cpu
, enum cpuhp_state state
, bool bringup
,
1477 struct hlist_node
*node
)
1479 struct cpuhp_step
*sp
= cpuhp_get_step(state
);
1482 if ((bringup
&& !sp
->startup
.single
) ||
1483 (!bringup
&& !sp
->teardown
.single
))
1486 * The non AP bound callbacks can fail on bringup. On teardown
1487 * e.g. module removal we crash for now.
1490 if (cpuhp_is_ap_state(state
))
1491 ret
= cpuhp_invoke_ap_callback(cpu
, state
, bringup
, node
);
1493 ret
= cpuhp_invoke_callback(cpu
, state
, bringup
, node
);
1495 ret
= cpuhp_invoke_callback(cpu
, state
, bringup
, node
);
1497 BUG_ON(ret
&& !bringup
);
1502 * Called from __cpuhp_setup_state on a recoverable failure.
1504 * Note: The teardown callbacks for rollback are not allowed to fail!
1506 static void cpuhp_rollback_install(int failedcpu
, enum cpuhp_state state
,
1507 struct hlist_node
*node
)
1511 /* Roll back the already executed steps on the other cpus */
1512 for_each_present_cpu(cpu
) {
1513 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, cpu
);
1514 int cpustate
= st
->state
;
1516 if (cpu
>= failedcpu
)
1519 /* Did we invoke the startup call on that cpu ? */
1520 if (cpustate
>= state
)
1521 cpuhp_issue_call(cpu
, state
, false, node
);
1526 * Returns a free for dynamic slot assignment of the Online state. The states
1527 * are protected by the cpuhp_slot_states mutex and an empty slot is identified
1528 * by having no name assigned.
1530 static int cpuhp_reserve_state(enum cpuhp_state state
)
1534 for (i
= CPUHP_AP_ONLINE_DYN
; i
<= CPUHP_AP_ONLINE_DYN_END
; i
++) {
1535 if (cpuhp_ap_states
[i
].name
)
1538 cpuhp_ap_states
[i
].name
= "Reserved";
1541 WARN(1, "No more dynamic states available for CPU hotplug\n");
1545 int __cpuhp_state_add_instance(enum cpuhp_state state
, struct hlist_node
*node
,
1548 struct cpuhp_step
*sp
;
1552 sp
= cpuhp_get_step(state
);
1553 if (sp
->multi_instance
== false)
1557 mutex_lock(&cpuhp_state_mutex
);
1559 if (!invoke
|| !sp
->startup
.multi
)
1563 * Try to call the startup callback for each present cpu
1564 * depending on the hotplug state of the cpu.
1566 for_each_present_cpu(cpu
) {
1567 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, cpu
);
1568 int cpustate
= st
->state
;
1570 if (cpustate
< state
)
1573 ret
= cpuhp_issue_call(cpu
, state
, true, node
);
1575 if (sp
->teardown
.multi
)
1576 cpuhp_rollback_install(cpu
, state
, node
);
1582 hlist_add_head(node
, &sp
->list
);
1585 mutex_unlock(&cpuhp_state_mutex
);
1589 EXPORT_SYMBOL_GPL(__cpuhp_state_add_instance
);
1592 * __cpuhp_setup_state - Setup the callbacks for an hotplug machine state
1593 * @state: The state to setup
1594 * @invoke: If true, the startup function is invoked for cpus where
1595 * cpu state >= @state
1596 * @startup: startup callback function
1597 * @teardown: teardown callback function
1599 * Returns 0 if successful, otherwise a proper error code
1601 int __cpuhp_setup_state(enum cpuhp_state state
,
1602 const char *name
, bool invoke
,
1603 int (*startup
)(unsigned int cpu
),
1604 int (*teardown
)(unsigned int cpu
),
1605 bool multi_instance
)
1610 if (cpuhp_cb_check(state
) || !name
)
1614 mutex_lock(&cpuhp_state_mutex
);
1616 /* currently assignments for the ONLINE state are possible */
1617 if (state
== CPUHP_AP_ONLINE_DYN
) {
1619 ret
= cpuhp_reserve_state(state
);
1625 cpuhp_store_callbacks(state
, name
, startup
, teardown
, multi_instance
);
1627 if (!invoke
|| !startup
)
1631 * Try to call the startup callback for each present cpu
1632 * depending on the hotplug state of the cpu.
1634 for_each_present_cpu(cpu
) {
1635 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, cpu
);
1636 int cpustate
= st
->state
;
1638 if (cpustate
< state
)
1641 ret
= cpuhp_issue_call(cpu
, state
, true, NULL
);
1644 cpuhp_rollback_install(cpu
, state
, NULL
);
1645 cpuhp_store_callbacks(state
, NULL
, NULL
, NULL
, false);
1650 mutex_unlock(&cpuhp_state_mutex
);
1653 if (!ret
&& dyn_state
)
1657 EXPORT_SYMBOL(__cpuhp_setup_state
);
1659 int __cpuhp_state_remove_instance(enum cpuhp_state state
,
1660 struct hlist_node
*node
, bool invoke
)
1662 struct cpuhp_step
*sp
= cpuhp_get_step(state
);
1665 BUG_ON(cpuhp_cb_check(state
));
1667 if (!sp
->multi_instance
)
1671 mutex_lock(&cpuhp_state_mutex
);
1673 if (!invoke
|| !cpuhp_get_teardown_cb(state
))
1676 * Call the teardown callback for each present cpu depending
1677 * on the hotplug state of the cpu. This function is not
1678 * allowed to fail currently!
1680 for_each_present_cpu(cpu
) {
1681 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, cpu
);
1682 int cpustate
= st
->state
;
1684 if (cpustate
>= state
)
1685 cpuhp_issue_call(cpu
, state
, false, node
);
1690 mutex_unlock(&cpuhp_state_mutex
);
1695 EXPORT_SYMBOL_GPL(__cpuhp_state_remove_instance
);
1697 * __cpuhp_remove_state - Remove the callbacks for an hotplug machine state
1698 * @state: The state to remove
1699 * @invoke: If true, the teardown function is invoked for cpus where
1700 * cpu state >= @state
1702 * The teardown callback is currently not allowed to fail. Think
1703 * about module removal!
1705 void __cpuhp_remove_state(enum cpuhp_state state
, bool invoke
)
1707 struct cpuhp_step
*sp
= cpuhp_get_step(state
);
1710 BUG_ON(cpuhp_cb_check(state
));
1713 mutex_lock(&cpuhp_state_mutex
);
1715 if (sp
->multi_instance
) {
1716 WARN(!hlist_empty(&sp
->list
),
1717 "Error: Removing state %d which has instances left.\n",
1722 if (!invoke
|| !cpuhp_get_teardown_cb(state
))
1726 * Call the teardown callback for each present cpu depending
1727 * on the hotplug state of the cpu. This function is not
1728 * allowed to fail currently!
1730 for_each_present_cpu(cpu
) {
1731 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, cpu
);
1732 int cpustate
= st
->state
;
1734 if (cpustate
>= state
)
1735 cpuhp_issue_call(cpu
, state
, false, NULL
);
1738 cpuhp_store_callbacks(state
, NULL
, NULL
, NULL
, false);
1739 mutex_unlock(&cpuhp_state_mutex
);
1742 EXPORT_SYMBOL(__cpuhp_remove_state
);
1744 #if defined(CONFIG_SYSFS) && defined(CONFIG_HOTPLUG_CPU)
1745 static ssize_t
show_cpuhp_state(struct device
*dev
,
1746 struct device_attribute
*attr
, char *buf
)
1748 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, dev
->id
);
1750 return sprintf(buf
, "%d\n", st
->state
);
1752 static DEVICE_ATTR(state
, 0444, show_cpuhp_state
, NULL
);
1754 static ssize_t
write_cpuhp_target(struct device
*dev
,
1755 struct device_attribute
*attr
,
1756 const char *buf
, size_t count
)
1758 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, dev
->id
);
1759 struct cpuhp_step
*sp
;
1762 ret
= kstrtoint(buf
, 10, &target
);
1766 #ifdef CONFIG_CPU_HOTPLUG_STATE_CONTROL
1767 if (target
< CPUHP_OFFLINE
|| target
> CPUHP_ONLINE
)
1770 if (target
!= CPUHP_OFFLINE
&& target
!= CPUHP_ONLINE
)
1774 ret
= lock_device_hotplug_sysfs();
1778 mutex_lock(&cpuhp_state_mutex
);
1779 sp
= cpuhp_get_step(target
);
1780 ret
= !sp
->name
|| sp
->cant_stop
? -EINVAL
: 0;
1781 mutex_unlock(&cpuhp_state_mutex
);
1785 if (st
->state
< target
)
1786 ret
= do_cpu_up(dev
->id
, target
);
1788 ret
= do_cpu_down(dev
->id
, target
);
1790 unlock_device_hotplug();
1791 return ret
? ret
: count
;
1794 static ssize_t
show_cpuhp_target(struct device
*dev
,
1795 struct device_attribute
*attr
, char *buf
)
1797 struct cpuhp_cpu_state
*st
= per_cpu_ptr(&cpuhp_state
, dev
->id
);
1799 return sprintf(buf
, "%d\n", st
->target
);
1801 static DEVICE_ATTR(target
, 0644, show_cpuhp_target
, write_cpuhp_target
);
1803 static struct attribute
*cpuhp_cpu_attrs
[] = {
1804 &dev_attr_state
.attr
,
1805 &dev_attr_target
.attr
,
1809 static struct attribute_group cpuhp_cpu_attr_group
= {
1810 .attrs
= cpuhp_cpu_attrs
,
1815 static ssize_t
show_cpuhp_states(struct device
*dev
,
1816 struct device_attribute
*attr
, char *buf
)
1818 ssize_t cur
, res
= 0;
1821 mutex_lock(&cpuhp_state_mutex
);
1822 for (i
= CPUHP_OFFLINE
; i
<= CPUHP_ONLINE
; i
++) {
1823 struct cpuhp_step
*sp
= cpuhp_get_step(i
);
1826 cur
= sprintf(buf
, "%3d: %s\n", i
, sp
->name
);
1831 mutex_unlock(&cpuhp_state_mutex
);
1834 static DEVICE_ATTR(states
, 0444, show_cpuhp_states
, NULL
);
1836 static struct attribute
*cpuhp_cpu_root_attrs
[] = {
1837 &dev_attr_states
.attr
,
1841 static struct attribute_group cpuhp_cpu_root_attr_group
= {
1842 .attrs
= cpuhp_cpu_root_attrs
,
1847 static int __init
cpuhp_sysfs_init(void)
1851 ret
= sysfs_create_group(&cpu_subsys
.dev_root
->kobj
,
1852 &cpuhp_cpu_root_attr_group
);
1856 for_each_possible_cpu(cpu
) {
1857 struct device
*dev
= get_cpu_device(cpu
);
1861 ret
= sysfs_create_group(&dev
->kobj
, &cpuhp_cpu_attr_group
);
1867 device_initcall(cpuhp_sysfs_init
);
1871 * cpu_bit_bitmap[] is a special, "compressed" data structure that
1872 * represents all NR_CPUS bits binary values of 1<<nr.
1874 * It is used by cpumask_of() to get a constant address to a CPU
1875 * mask value that has a single bit set only.
1878 /* cpu_bit_bitmap[0] is empty - so we can back into it */
1879 #define MASK_DECLARE_1(x) [x+1][0] = (1UL << (x))
1880 #define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
1881 #define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
1882 #define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
1884 const unsigned long cpu_bit_bitmap
[BITS_PER_LONG
+1][BITS_TO_LONGS(NR_CPUS
)] = {
1886 MASK_DECLARE_8(0), MASK_DECLARE_8(8),
1887 MASK_DECLARE_8(16), MASK_DECLARE_8(24),
1888 #if BITS_PER_LONG > 32
1889 MASK_DECLARE_8(32), MASK_DECLARE_8(40),
1890 MASK_DECLARE_8(48), MASK_DECLARE_8(56),
1893 EXPORT_SYMBOL_GPL(cpu_bit_bitmap
);
1895 const DECLARE_BITMAP(cpu_all_bits
, NR_CPUS
) = CPU_BITS_ALL
;
1896 EXPORT_SYMBOL(cpu_all_bits
);
1898 #ifdef CONFIG_INIT_ALL_POSSIBLE
1899 struct cpumask __cpu_possible_mask __read_mostly
1902 struct cpumask __cpu_possible_mask __read_mostly
;
1904 EXPORT_SYMBOL(__cpu_possible_mask
);
1906 struct cpumask __cpu_online_mask __read_mostly
;
1907 EXPORT_SYMBOL(__cpu_online_mask
);
1909 struct cpumask __cpu_present_mask __read_mostly
;
1910 EXPORT_SYMBOL(__cpu_present_mask
);
1912 struct cpumask __cpu_active_mask __read_mostly
;
1913 EXPORT_SYMBOL(__cpu_active_mask
);
1915 void init_cpu_present(const struct cpumask
*src
)
1917 cpumask_copy(&__cpu_present_mask
, src
);
1920 void init_cpu_possible(const struct cpumask
*src
)
1922 cpumask_copy(&__cpu_possible_mask
, src
);
1925 void init_cpu_online(const struct cpumask
*src
)
1927 cpumask_copy(&__cpu_online_mask
, src
);
1931 * Activate the first processor.
1933 void __init
boot_cpu_init(void)
1935 int cpu
= smp_processor_id();
1937 /* Mark the boot cpu "present", "online" etc for SMP and UP case */
1938 set_cpu_online(cpu
, true);
1939 set_cpu_active(cpu
, true);
1940 set_cpu_present(cpu
, true);
1941 set_cpu_possible(cpu
, true);
1945 * Must be called _AFTER_ setting up the per_cpu areas
1947 void __init
boot_cpu_state_init(void)
1949 per_cpu_ptr(&cpuhp_state
, smp_processor_id())->state
= CPUHP_ONLINE
;