CPU hotplug in the Kernel¶
- Date:
September, 2021
- Author:
Sebastian Andrzej Siewior <bigeasy@linutronix.de>,Rusty Russell <rusty@rustcorp.com.au>,Srivatsa Vaddagiri <vatsa@in.ibm.com>,Ashok Raj <ashok.raj@intel.com>,Joel Schopp <jschopp@austin.ibm.com>,Thomas Gleixner <tglx@kernel.org>
Introduction¶
Modern advances in system architectures have introduced advanced errorreporting and correction capabilities in processors. There are couple OEMS thatsupport NUMA hardware which are hot pluggable as well, where physical nodeinsertion and removal require support for CPU hotplug.
Such advances require CPUs available to a kernel to be removed either forprovisioning reasons, or for RAS purposes to keep an offending CPU offsystem execution path. Hence the need for CPU hotplug support in theLinux kernel.
A more novel use of CPU-hotplug support is its use today in suspend resumesupport for SMP. Dual-core and HT support makes even a laptop run SMP kernelswhich didn’t support these methods.
Command Line Switches¶
maxcpus=nRestrict boot time CPUs ton. Say if you have four CPUs, using
maxcpus=2will only boot two. You can choose to bring theother CPUs later online.nr_cpus=nRestrict the total amount of CPUs the kernel will support. If the numbersupplied here is lower than the number of physically available CPUs, thenthose CPUs can not be brought online later.
possible_cpus=nThis option sets
possible_cpusbits incpu_possible_mask.This option is limited to the X86 and S390 architecture.
cpu0_hotplugAllow to shutdown CPU0.
This option is limited to the X86 architecture.
CPU maps¶
cpu_possible_maskBitmap of possible CPUs that can ever be available in thesystem. This is used to allocate some boot time memory for per_cpu variablesthat aren’t designed to grow/shrink as CPUs are made available or removed.Once set during boot time discovery phase, the map is static, i.e no bitsare added or removed anytime. Trimming it accurately for your system needsupfront can save some boot time memory.
cpu_online_maskBitmap of all CPUs currently online. Its set in
__cpu_up()after a CPU is available for kernel scheduling and ready to receiveinterrupts from devices. Its cleared when a CPU is brought down using__cpu_disable(), before which all OS services including interrupts aremigrated to another target CPU.cpu_present_maskBitmap of CPUs currently present in the system. Not allof them may be online. When physical hotplug is processed by the relevantsubsystem (e.g ACPI) can change and new bit either be added or removedfrom the map depending on the event is hot-add/hot-remove. There are currentlyno locking rules as of now. Typical usage is to init topology during boot,at which time hotplug is disabled.
You really don’t need to manipulate any of the system CPU maps. They shouldbe read-only for most use. When setting up per-cpu resources almost always usecpu_possible_mask orfor_each_possible_cpu() to iterate. To macrofor_each_cpu() can be used to iterate over a custom CPU mask.
Never use anything other thancpumask_t to represent bitmap of CPUs.
Using CPU hotplug¶
The kernel optionCONFIG_HOTPLUG_CPU needs to be enabled. It is currentlyavailable on multiple architectures including ARM, MIPS, PowerPC and X86. Theconfiguration is done via the sysfs interface:
$ ls -lh /sys/devices/system/cputotal 0drwxr-xr-x 9 root root 0 Dec 21 16:33 cpu0drwxr-xr-x 9 root root 0 Dec 21 16:33 cpu1drwxr-xr-x 9 root root 0 Dec 21 16:33 cpu2drwxr-xr-x 9 root root 0 Dec 21 16:33 cpu3drwxr-xr-x 9 root root 0 Dec 21 16:33 cpu4drwxr-xr-x 9 root root 0 Dec 21 16:33 cpu5drwxr-xr-x 9 root root 0 Dec 21 16:33 cpu6drwxr-xr-x 9 root root 0 Dec 21 16:33 cpu7drwxr-xr-x 2 root root 0 Dec 21 16:33 hotplug-r--r--r-- 1 root root 4.0K Dec 21 16:33 offline-r--r--r-- 1 root root 4.0K Dec 21 16:33 online-r--r--r-- 1 root root 4.0K Dec 21 16:33 possible-r--r--r-- 1 root root 4.0K Dec 21 16:33 present
The filesoffline,online,possible,present represent the CPU masks.Each CPU folder contains anonline file which controls the logical on (1) andoff (0) state. To logically shutdown CPU4:
$ echo 0 > /sys/devices/system/cpu/cpu4/online smpboot: CPU 4 is now offline
Once the CPU is shutdown, it will be removed from/proc/interrupts,/proc/cpuinfo and should also not be shown visible by thetop command. Tobring CPU4 back online:
$ echo 1 > /sys/devices/system/cpu/cpu4/onlinesmpboot: Booting Node 0 Processor 4 APIC 0x1
The CPU is usable again. This should work on all CPUs, but CPU0 is often specialand excluded from CPU hotplug.
The CPU hotplug coordination¶
The offline case¶
Once a CPU has been logically shutdown the teardown callbacks of registeredhotplug states will be invoked, starting withCPUHP_ONLINE and terminatingat stateCPUHP_OFFLINE. This includes:
If tasks are frozen due to a suspend operation thencpuhp_tasks_frozenwill be set to true.
All processes are migrated away from this outgoing CPU to new CPUs.The new CPU is chosen from each process’ current cpuset, which may bea subset of all online CPUs.
All interrupts targeted to this CPU are migrated to a new CPU
timers are also migrated to a new CPU
Once all services are migrated, kernel calls an arch specific routine
__cpu_disable()to perform arch specific cleanup.
The CPU hotplug API¶
CPU hotplug state machine¶
CPU hotplug uses a trivial state machine with a linear state space fromCPUHP_OFFLINE to CPUHP_ONLINE. Each state has a startup and a teardowncallback.
When a CPU is onlined, the startup callbacks are invoked sequentially untilthe state CPUHP_ONLINE is reached. They can also be invoked when thecallbacks of a state are set up or an instance is added to a multi-instancestate.
When a CPU is offlined the teardown callbacks are invoked in the reverseorder sequentially until the state CPUHP_OFFLINE is reached. They can alsobe invoked when the callbacks of a state are removed or an instance isremoved from a multi-instance state.
If a usage site requires only a callback in one direction of the hotplugoperations (CPU online or CPU offline) then the other not-required callbackcan be set to NULL when the state is set up.
The state space is divided into three sections:
The PREPARE section
The PREPARE section covers the state space from CPUHP_OFFLINE toCPUHP_BRINGUP_CPU.
The startup callbacks in this section are invoked before the CPU isstarted during a CPU online operation. The teardown callbacks are invokedafter the CPU has become dysfunctional during a CPU offline operation.
The callbacks are invoked on a control CPU as they can’t obviously run onthe hotplugged CPU which is either not yet started or has becomedysfunctional already.
The startup callbacks are used to setup resources which are required tobring a CPU successfully online. The teardown callbacks are used to freeresources or to move pending work to an online CPU after the hotpluggedCPU became dysfunctional.
The startup callbacks are allowed to fail. If a callback fails, the CPUonline operation is aborted and the CPU is brought down to the previousstate (usually CPUHP_OFFLINE) again.
The teardown callbacks in this section are not allowed to fail.
The STARTING section
The STARTING section covers the state space between CPUHP_BRINGUP_CPU + 1and CPUHP_AP_ONLINE.
The startup callbacks in this section are invoked on the hotplugged CPUwith interrupts disabled during a CPU online operation in the early CPUsetup code. The teardown callbacks are invoked with interrupts disabledon the hotplugged CPU during a CPU offline operation shortly before theCPU is completely shut down.
The callbacks in this section are not allowed to fail.
The callbacks are used for low level hardware initialization/shutdown andfor core subsystems.
The ONLINE section
The ONLINE section covers the state space between CPUHP_AP_ONLINE + 1 andCPUHP_ONLINE.
The startup callbacks in this section are invoked on the hotplugged CPUduring a CPU online operation. The teardown callbacks are invoked on thehotplugged CPU during a CPU offline operation.
The callbacks are invoked in the context of the per CPU hotplug thread,which is pinned on the hotplugged CPU. The callbacks are invoked withinterrupts and preemption enabled.
The callbacks are allowed to fail. When a callback fails the hotplugoperation is aborted and the CPU is brought back to the previous state.
CPU online/offline operations¶
A successful online operation looks like this:
[CPUHP_OFFLINE][CPUHP_OFFLINE + 1]->startup() -> success[CPUHP_OFFLINE + 2]->startup() -> success[CPUHP_OFFLINE + 3] -> skipped because startup == NULL...[CPUHP_BRINGUP_CPU]->startup() -> success=== End of PREPARE section[CPUHP_BRINGUP_CPU + 1]->startup() -> success...[CPUHP_AP_ONLINE]->startup() -> success=== End of STARTUP section[CPUHP_AP_ONLINE + 1]->startup() -> success...[CPUHP_ONLINE - 1]->startup() -> success[CPUHP_ONLINE]
A successful offline operation looks like this:
[CPUHP_ONLINE][CPUHP_ONLINE - 1]->teardown() -> success...[CPUHP_AP_ONLINE + 1]->teardown() -> success=== Start of STARTUP section[CPUHP_AP_ONLINE]->teardown() -> success...[CPUHP_BRINGUP_ONLINE - 1]->teardown()...=== Start of PREPARE section[CPUHP_BRINGUP_CPU]->teardown()[CPUHP_OFFLINE + 3]->teardown()[CPUHP_OFFLINE + 2] -> skipped because teardown == NULL[CPUHP_OFFLINE + 1]->teardown()[CPUHP_OFFLINE]
A failed online operation looks like this:
[CPUHP_OFFLINE][CPUHP_OFFLINE + 1]->startup() -> success[CPUHP_OFFLINE + 2]->startup() -> success[CPUHP_OFFLINE + 3] -> skipped because startup == NULL...[CPUHP_BRINGUP_CPU]->startup() -> success=== End of PREPARE section[CPUHP_BRINGUP_CPU + 1]->startup() -> success...[CPUHP_AP_ONLINE]->startup() -> success=== End of STARTUP section[CPUHP_AP_ONLINE + 1]->startup() -> success---[CPUHP_AP_ONLINE + N]->startup() -> fail[CPUHP_AP_ONLINE + (N - 1)]->teardown()...[CPUHP_AP_ONLINE + 1]->teardown()=== Start of STARTUP section[CPUHP_AP_ONLINE]->teardown()...[CPUHP_BRINGUP_ONLINE - 1]->teardown()...=== Start of PREPARE section[CPUHP_BRINGUP_CPU]->teardown()[CPUHP_OFFLINE + 3]->teardown()[CPUHP_OFFLINE + 2] -> skipped because teardown == NULL[CPUHP_OFFLINE + 1]->teardown()[CPUHP_OFFLINE]
A failed offline operation looks like this:
[CPUHP_ONLINE][CPUHP_ONLINE - 1]->teardown() -> success...[CPUHP_ONLINE - N]->teardown() -> fail[CPUHP_ONLINE - (N - 1)]->startup()...[CPUHP_ONLINE - 1]->startup()[CPUHP_ONLINE]
Recursive failures cannot be handled sensibly. Look at the followingexample of a recursive fail due to a failed offline operation:
[CPUHP_ONLINE][CPUHP_ONLINE - 1]->teardown() -> success...[CPUHP_ONLINE - N]->teardown() -> fail[CPUHP_ONLINE - (N - 1)]->startup() -> success[CPUHP_ONLINE - (N - 2)]->startup() -> fail
The CPU hotplug state machine stops right here and does not try to go backdown again because that would likely result in an endless loop:
[CPUHP_ONLINE - (N - 1)]->teardown() -> success[CPUHP_ONLINE - N]->teardown() -> fail[CPUHP_ONLINE - (N - 1)]->startup() -> success[CPUHP_ONLINE - (N - 2)]->startup() -> fail[CPUHP_ONLINE - (N - 1)]->teardown() -> success[CPUHP_ONLINE - N]->teardown() -> fail
Lather, rinse and repeat. In this case the CPU left in state:
[CPUHP_ONLINE - (N - 1)]
which at least lets the system make progress and gives the user a chance todebug or even resolve the situation.
Allocating a state¶
There are two ways to allocate a CPU hotplug state:
Static allocation
Static allocation has to be used when the subsystem or driver hasordering requirements versus other CPU hotplug states. E.g. the PERF corestartup callback has to be invoked before the PERF driver startupcallbacks during a CPU online operation. During a CPU offline operationthe driver teardown callbacks have to be invoked before the core teardowncallback. The statically allocated states are described by constants inthe cpuhp_state
enumwhichcan be found in include/linux/cpuhotplug.h.Insert the state into the
enumatthe proper place so the orderingrequirements are fulfilled. The state constant has to be used for statesetup and removal.Static allocation is also required when the state callbacks are not setup at runtime and are part of the initializer of the CPU hotplug statearray in kernel/cpu.c.
Dynamic allocation
When there are no ordering requirements for the state callbacks thendynamic allocation is the preferred method. The state number is allocatedby the setup function and returned to the caller on success.
Only the PREPARE and ONLINE sections provide a dynamic allocationrange. The STARTING section does not as most of the callbacks in thatsection have explicit ordering requirements.
Setup of a CPU hotplug state¶
The core code provides the following functions to setup a state:
cpuhp_setup_state(state, name, startup, teardown)
cpuhp_setup_state_nocalls(state, name, startup, teardown)
cpuhp_setup_state_cpuslocked(state, name, startup, teardown)
cpuhp_setup_state_nocalls_cpuslocked(state, name, startup, teardown)
For cases where a driver or a subsystem has multiple instances and the sameCPU hotplug state callbacks need to be invoked for each instance, the CPUhotplug core provides multi-instance support. The advantage over driverspecific instance lists is that the instance related functions are fullyserialized against CPU hotplug operations and provide the automaticinvocations of the state callbacks on add and removal. To set up such amulti-instance state the following function is available:
cpuhp_setup_state_multi(state, name, startup, teardown)
The @state argument is either a statically allocated state or one of theconstants for dynamically allocated states - CPUHP_BP_PREPARE_DYN,CPUHP_AP_ONLINE_DYN - depending on the state section (PREPARE, ONLINE) forwhich a dynamic state should be allocated.
The @name argument is used for sysfs output and for instrumentation. Thenaming convention is “subsys:mode” or “subsys/driver:mode”,e.g. “perf:mode” or “perf/x86:mode”. The common mode names are:
prepare | For states in the PREPARE section |
dead | For states in the PREPARE section which do not providea startup callback |
starting | For states in the STARTING section |
dying | For states in the STARTING section which do not providea startup callback |
online | For states in the ONLINE section |
offline | For states in the ONLINE section which do not providea startup callback |
As the @name argument is only used for sysfs and instrumentation other modedescriptors can be used as well if they describe the nature of the statebetter than the common ones.
Examples for @name arguments: “perf/online”, “perf/x86:prepare”,“RCU/tree:dying”, “sched/waitempty”
The @startup argument is a function pointer to the callback which should beinvoked during a CPU online operation. If the usage site does not require astartup callback set the pointer to NULL.
The @teardown argument is a function pointer to the callback which shouldbe invoked during a CPU offline operation. If the usage site does notrequire a teardown callback set the pointer to NULL.
The functions differ in the way how the installed callbacks are treated:
cpuhp_setup_state_nocalls(),cpuhp_setup_state_nocalls_cpuslocked()andcpuhp_setup_state_multi()only install the callbacks
cpuhp_setup_state()andcpuhp_setup_state_cpuslocked()install thecallbacks and invoke the @startup callback (if not NULL) for all onlineCPUs which have currently a state greater than the newly installedstate. Depending on the state section the callback is either invoked onthe current CPU (PREPARE section) or on each online CPU (ONLINEsection) in the context of the CPU’s hotplug thread.If a callback fails for CPU N then the teardown callback for CPU0 .. N-1 is invoked to rollback the operation. The state setup fails,the callbacks for the state are not installed and in case of dynamicallocation the allocated state is freed.
The state setup and the callback invocations are serialized against CPUhotplug operations. If the setup function has to be called from a CPUhotplug read locked region, then the_cpuslocked() variants have to beused. These functions cannot be used from within CPU hotplug callbacks.
- The function return values:
0
Statically allocated state was successfully set up
>0
Dynamically allocated state was successfully set up.
The returned number is the state number which was allocated. Ifthe state callbacks have to be removed later, e.g. moduleremoval, then this number has to be saved by the caller and usedas @state argument for the state remove function. Formulti-instance states the dynamically allocated state number isalso required as @state argument for the instance add/removeoperations.
<0
Operation failed
Removal of a CPU hotplug state¶
To remove a previously set up state, the following functions are provided:
cpuhp_remove_state(state)
cpuhp_remove_state_nocalls(state)
cpuhp_remove_state_nocalls_cpuslocked(state)
cpuhp_remove_multi_state(state)
The @state argument is either a statically allocated state or the statenumber which was allocated in the dynamic range by cpuhp_setup_state*(). Ifthe state is in the dynamic range, then the state number is freed andavailable for dynamic allocation again.
The functions differ in the way how the installed callbacks are treated:
cpuhp_remove_state_nocalls(),cpuhp_remove_state_nocalls_cpuslocked()andcpuhp_remove_multi_state()only remove the callbacks.
cpuhp_remove_state()removes the callbacks and invokes the teardowncallback (if not NULL) for all online CPUs which have currently a stategreater than the removed state. Depending on the state section thecallback is either invoked on the current CPU (PREPARE section) or oneach online CPU (ONLINE section) in the context of the CPU’s hotplugthread.In order to complete the removal, the teardown callback should not fail.
The state removal and the callback invocations are serialized against CPUhotplug operations. If the remove function has to be called from a CPUhotplug read locked region, then the_cpuslocked() variants have to beused. These functions cannot be used from within CPU hotplug callbacks.
If a multi-instance state is removed then the caller has to remove allinstances first.
Multi-Instance state instance management¶
Once the multi-instance state is set up, instances can be added to thestate:
cpuhp_state_add_instance(state, node)
cpuhp_state_add_instance_nocalls(state, node)
The @state argument is either a statically allocated state or the statenumber which was allocated in the dynamic range bycpuhp_setup_state_multi().
The @node argument is a pointer to an hlist_node which is embedded in theinstance’s data structure. The pointer is handed to the multi-instancestate callbacks and can be used by the callback to retrieve the instanceviacontainer_of().
The functions differ in the way how the installed callbacks are treated:
cpuhp_state_add_instance_nocalls()and only adds the instance to themulti-instance state’s node list.
cpuhp_state_add_instance()adds the instance and invokes the startupcallback (if not NULL) associated with @state for all online CPUs whichhave currently a state greater than @state. The callback is onlyinvoked for the to be added instance. Depending on the state sectionthe callback is either invoked on the current CPU (PREPARE section) oron each online CPU (ONLINE section) in the context of the CPU’s hotplugthread.If a callback fails for CPU N then the teardown callback for CPU0 .. N-1 is invoked to rollback the operation, the function fails andthe instance is not added to the node list of the multi-instance state.
To remove an instance from the state’s node list these functions areavailable:
cpuhp_state_remove_instance(state, node)
cpuhp_state_remove_instance_nocalls(state, node)
The arguments are the same as for the cpuhp_state_add_instance*()variants above.
The functions differ in the way how the installed callbacks are treated:
cpuhp_state_remove_instance_nocalls()only removes the instance from thestate’s node list.
cpuhp_state_remove_instance()removes the instance and invokes theteardown callback (if not NULL) associated with @state for all onlineCPUs which have currently a state greater than @state. The callback isonly invoked for the to be removed instance. Depending on the statesection the callback is either invoked on the current CPU (PREPAREsection) or on each online CPU (ONLINE section) in the context of theCPU’s hotplug thread.In order to complete the removal, the teardown callback should not fail.
The node list add/remove operations and the callback invocations areserialized against CPU hotplug operations. These functions cannot be usedfrom within CPU hotplug callbacks and CPU hotplug read locked regions.
Examples¶
Setup and teardown a statically allocated state in the STARTING section fornotifications on online and offline operations:
ret = cpuhp_setup_state(CPUHP_SUBSYS_STARTING, "subsys:starting", subsys_cpu_starting, subsys_cpu_dying);if (ret < 0) return ret;....cpuhp_remove_state(CPUHP_SUBSYS_STARTING);
Setup and teardown a dynamically allocated state in the ONLINE sectionfor notifications on offline operations:
state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "subsys:offline", NULL, subsys_cpu_offline);if (state < 0) return state;....cpuhp_remove_state(state);
Setup and teardown a dynamically allocated state in the ONLINE sectionfor notifications on online operations without invoking the callbacks:
state = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "subsys:online", subsys_cpu_online, NULL);if (state < 0) return state;....cpuhp_remove_state_nocalls(state);
Setup, use and teardown a dynamically allocated multi-instance state in theONLINE section for notifications on online and offline operation:
state = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "subsys:online", subsys_cpu_online, subsys_cpu_offline);if (state < 0) return state;....ret = cpuhp_state_add_instance(state, &inst1->node);if (ret) return ret;....ret = cpuhp_state_add_instance(state, &inst2->node);if (ret) return ret;....cpuhp_remove_instance(state, &inst1->node);....cpuhp_remove_instance(state, &inst2->node);....cpuhp_remove_multi_state(state);
Testing of hotplug states¶
One way to verify whether a custom state is working as expected or not is toshutdown a CPU and then put it online again. It is also possible to put the CPUto certain state (for instanceCPUHP_AP_ONLINE) and then go back toCPUHP_ONLINE. This would simulate an error one state afterCPUHP_AP_ONLINEwhich would lead to rollback to the online state.
All registered states are enumerated in/sys/devices/system/cpu/hotplug/states
$ tail /sys/devices/system/cpu/hotplug/states138: mm/vmscan:online139: mm/vmstat:online140: lib/percpu_cnt:online141: acpi/cpu-drv:online142: base/cacheinfo:online143: virtio/net:online144: x86/mce:online145: printk:online168: sched:active169: online
To rollback CPU4 tolib/percpu_cnt:online and back online just issue:
$ cat /sys/devices/system/cpu/cpu4/hotplug/state169$ echo 140 > /sys/devices/system/cpu/cpu4/hotplug/target$ cat /sys/devices/system/cpu/cpu4/hotplug/state140
It is important to note that the teardown callback of state 140 have beeninvoked. And now get back online:
$ echo 169 > /sys/devices/system/cpu/cpu4/hotplug/target$ cat /sys/devices/system/cpu/cpu4/hotplug/state169
With trace events enabled, the individual steps are visible, too:
# TASK-PID CPU# TIMESTAMP FUNCTION# | | | | | bash-394 [001] 22.976: cpuhp_enter: cpu: 0004 target: 140 step: 169 (cpuhp_kick_ap_work) cpuhp/4-31 [004] 22.977: cpuhp_enter: cpu: 0004 target: 140 step: 168 (sched_cpu_deactivate) cpuhp/4-31 [004] 22.990: cpuhp_exit: cpu: 0004 state: 168 step: 168 ret: 0 cpuhp/4-31 [004] 22.991: cpuhp_enter: cpu: 0004 target: 140 step: 144 (mce_cpu_pre_down) cpuhp/4-31 [004] 22.992: cpuhp_exit: cpu: 0004 state: 144 step: 144 ret: 0 cpuhp/4-31 [004] 22.993: cpuhp_multi_enter: cpu: 0004 target: 140 step: 143 (virtnet_cpu_down_prep) cpuhp/4-31 [004] 22.994: cpuhp_exit: cpu: 0004 state: 143 step: 143 ret: 0 cpuhp/4-31 [004] 22.995: cpuhp_enter: cpu: 0004 target: 140 step: 142 (cacheinfo_cpu_pre_down) cpuhp/4-31 [004] 22.996: cpuhp_exit: cpu: 0004 state: 142 step: 142 ret: 0 bash-394 [001] 22.997: cpuhp_exit: cpu: 0004 state: 140 step: 169 ret: 0 bash-394 [005] 95.540: cpuhp_enter: cpu: 0004 target: 169 step: 140 (cpuhp_kick_ap_work) cpuhp/4-31 [004] 95.541: cpuhp_enter: cpu: 0004 target: 169 step: 141 (acpi_soft_cpu_online) cpuhp/4-31 [004] 95.542: cpuhp_exit: cpu: 0004 state: 141 step: 141 ret: 0 cpuhp/4-31 [004] 95.543: cpuhp_enter: cpu: 0004 target: 169 step: 142 (cacheinfo_cpu_online) cpuhp/4-31 [004] 95.544: cpuhp_exit: cpu: 0004 state: 142 step: 142 ret: 0 cpuhp/4-31 [004] 95.545: cpuhp_multi_enter: cpu: 0004 target: 169 step: 143 (virtnet_cpu_online) cpuhp/4-31 [004] 95.546: cpuhp_exit: cpu: 0004 state: 143 step: 143 ret: 0 cpuhp/4-31 [004] 95.547: cpuhp_enter: cpu: 0004 target: 169 step: 144 (mce_cpu_online) cpuhp/4-31 [004] 95.548: cpuhp_exit: cpu: 0004 state: 144 step: 144 ret: 0 cpuhp/4-31 [004] 95.549: cpuhp_enter: cpu: 0004 target: 169 step: 145 (console_cpu_notify) cpuhp/4-31 [004] 95.550: cpuhp_exit: cpu: 0004 state: 145 step: 145 ret: 0 cpuhp/4-31 [004] 95.551: cpuhp_enter: cpu: 0004 target: 169 step: 168 (sched_cpu_activate) cpuhp/4-31 [004] 95.552: cpuhp_exit: cpu: 0004 state: 168 step: 168 ret: 0 bash-394 [005] 95.553: cpuhp_exit: cpu: 0004 state: 169 step: 140 ret: 0
As it an be seen, CPU4 went down until timestamp 22.996 and then back up until95.552. All invoked callbacks including their return codes are visible in thetrace.
Architecture’s requirements¶
The following functions and configurations are required:
CONFIG_HOTPLUG_CPUThis entry needs to be enabled in Kconfig
__cpu_up()Arch interface to bring up a CPU
__cpu_disable()Arch interface to shutdown a CPU, no more interrupts can be handled by thekernel after the routine returns. This includes the shutdown of the timer.
__cpu_die()This actually supposed to ensure death of the CPU. Actually look at someexample code in other arch that implement CPU hotplug. The processor is takendown from the
idle()loop for that specific architecture.__cpu_die()typically waits for some per_cpu state to be set, to ensure the processor deadroutine is called to be sure positively.
User Space Notification¶
After CPU successfully onlined or offline udev events are sent. A udev rule like:
SUBSYSTEM=="cpu", DRIVERS=="processor", DEVPATH=="/devices/system/cpu/*", RUN+="the_hotplug_receiver.sh"
will receive all events. A script like:
#!/bin/shif [ "${ACTION}" = "offline" ]then echo "CPU ${DEVPATH##*/} offline"elif [ "${ACTION}" = "online" ]then echo "CPU ${DEVPATH##*/} online"fican process the event further.
When changes to the CPUs in the system occur, the sysfs file/sys/devices/system/cpu/crash_hotplug contains ‘1’ if the kernelupdates the kdump capture kernel list of CPUs itself (via elfcorehdr andother relevant kexec segment), or ‘0’ if userspace must update the kdumpcapture kernel list of CPUs.
The availability depends on the CONFIG_HOTPLUG_CPU kernel configurationoption.
To skip userspace processing of CPU hot un/plug events for kdump(i.e. the unload-then-reload to obtain a current list of CPUs), this sysfsfile can be used in a udev rule as follows:
SUBSYSTEM==”cpu”, ATTRS{crash_hotplug}==”1”, GOTO=”kdump_reload_end”
For a CPU hot un/plug event, if the architecture supports kernel updatesof the elfcorehdr (which contains the list of CPUs) and other relevantkexec segments, then the rule skips the unload-then-reload of the kdumpcapture kernel.
Kernel Inline Documentations Reference¶
- intcpuhp_setup_state(enumcpuhp_statestate,constchar*name,int(*startup)(unsignedintcpu),int(*teardown)(unsignedintcpu))¶
Setup hotplug state callbacks with calling thestartup callback
Parameters
enumcpuhp_statestateThe state for which the calls are installed
constchar*nameName of the callback (will be used in debug output)
int(*startup)(unsignedintcpu)startup callback function or NULL if not required
int(*teardown)(unsignedintcpu)teardown callback function or NULL if not required
Description
Installs the callback functions and invokes thestartup callback onthe online cpus which have already reached thestate.
- intcpuhp_setup_state_cpuslocked(enumcpuhp_statestate,constchar*name,int(*startup)(unsignedintcpu),int(*teardown)(unsignedintcpu))¶
Setup hotplug state callbacks with callingstartup callback from a
cpus_read_lock()held region
Parameters
enumcpuhp_statestateThe state for which the calls are installed
constchar*nameName of the callback (will be used in debug output)
int(*startup)(unsignedintcpu)startup callback function or NULL if not required
int(*teardown)(unsignedintcpu)teardown callback function or NULL if not required
Description
Same ascpuhp_setup_state() except that it must be invoked from within acpus_read_lock() held region.
- intcpuhp_setup_state_nocalls(enumcpuhp_statestate,constchar*name,int(*startup)(unsignedintcpu),int(*teardown)(unsignedintcpu))¶
Setup hotplug state callbacks without calling thestartup callback
Parameters
enumcpuhp_statestateThe state for which the calls are installed
constchar*nameName of the callback.
int(*startup)(unsignedintcpu)startup callback function or NULL if not required
int(*teardown)(unsignedintcpu)teardown callback function or NULL if not required
Description
Same ascpuhp_setup_state() except that thestartup callback is notinvoked during installation. NOP if SMP=n or HOTPLUG_CPU=n.
- intcpuhp_setup_state_nocalls_cpuslocked(enumcpuhp_statestate,constchar*name,int(*startup)(unsignedintcpu),int(*teardown)(unsignedintcpu))¶
Setup hotplug state callbacks without invoking thestartup callback from a
cpus_read_lock()held region callbacks
Parameters
enumcpuhp_statestateThe state for which the calls are installed
constchar*nameName of the callback.
int(*startup)(unsignedintcpu)startup callback function or NULL if not required
int(*teardown)(unsignedintcpu)teardown callback function or NULL if not required
Description
Same ascpuhp_setup_state_nocalls() except that it must be invoked fromwithin acpus_read_lock() held region.
- intcpuhp_setup_state_multi(enumcpuhp_statestate,constchar*name,int(*startup)(unsignedintcpu,structhlist_node*node),int(*teardown)(unsignedintcpu,structhlist_node*node))¶
Add callbacks for multi state
Parameters
enumcpuhp_statestateThe state for which the calls are installed
constchar*nameName of the callback.
int(*startup)(unsignedintcpu,structhlist_node*node)startup callback function or NULL if not required
int(*teardown)(unsignedintcpu,structhlist_node*node)teardown callback function or NULL if not required
Description
Sets the internal multi_instance flag and prepares a state to work as a multiinstance callback. No callbacks are invoked at this point. The callbacks areinvoked once an instance for this state are registered viacpuhp_state_add_instance() orcpuhp_state_add_instance_nocalls()
- intcpuhp_state_add_instance(enumcpuhp_statestate,structhlist_node*node)¶
Add an instance for a state and invoke startup callback.
Parameters
enumcpuhp_statestateThe state for which the instance is installed
structhlist_node*nodeThe node for this individual state.
Description
Installs the instance for thestate and invokes the registered startupcallback on the online cpus which have already reached thestate. Thestate must have been earlier marked as multi-instance bycpuhp_setup_state_multi().
- intcpuhp_state_add_instance_nocalls(enumcpuhp_statestate,structhlist_node*node)¶
Add an instance for a state without invoking the startup callback.
Parameters
enumcpuhp_statestateThe state for which the instance is installed
structhlist_node*nodeThe node for this individual state.
Description
Installs the instance for thestate. Thestate must have been earliermarked as multi-instance by cpuhp_setup_state_multi. NOP if SMP=n orHOTPLUG_CPU=n.
- intcpuhp_state_add_instance_nocalls_cpuslocked(enumcpuhp_statestate,structhlist_node*node)¶
Add an instance for a state without invoking the startup callback from a
cpus_read_lock()held region.
Parameters
enumcpuhp_statestateThe state for which the instance is installed
structhlist_node*nodeThe node for this individual state.
Description
Same ascpuhp_state_add_instance_nocalls() except that it must beinvoked from within acpus_read_lock() held region.
- voidcpuhp_remove_state(enumcpuhp_statestate)¶
Remove hotplug state callbacks and invoke the teardown
Parameters
enumcpuhp_statestateThe state for which the calls are removed
Description
Removes the callback functions and invokes the teardown callback onthe online cpus which have already reached thestate.
- voidcpuhp_remove_state_nocalls(enumcpuhp_statestate)¶
Remove hotplug state callbacks without invoking the teardown callback
Parameters
enumcpuhp_statestateThe state for which the calls are removed
- voidcpuhp_remove_state_nocalls_cpuslocked(enumcpuhp_statestate)¶
Remove hotplug state callbacks without invoking teardown from a
cpus_read_lock()held region.
Parameters
enumcpuhp_statestateThe state for which the calls are removed
Description
Same as cpuhp_remove_statenocalls() except that it must be invokedfrom within acpus_read_lock() held region.
- voidcpuhp_remove_multi_state(enumcpuhp_statestate)¶
Remove hotplug multi state callback
Parameters
enumcpuhp_statestateThe state for which the calls are removed
Description
Removes the callback functions from a multi state. This is the reverse ofcpuhp_setup_state_multi(). All instances should have been removed beforeinvoking this function.
- intcpuhp_state_remove_instance(enumcpuhp_statestate,structhlist_node*node)¶
Remove hotplug instance from state and invoke the teardown callback
Parameters
enumcpuhp_statestateThe state from which the instance is removed
structhlist_node*nodeThe node for this individual state.
Description
Removes the instance and invokes the teardown callback on the online cpuswhich have already reachedstate.
- intcpuhp_state_remove_instance_nocalls(enumcpuhp_statestate,structhlist_node*node)¶
Remove hotplug instance from state without invoking the teardown callback
Parameters
enumcpuhp_statestateThe state from which the instance is removed
structhlist_node*nodeThe node for this individual state.
Description
Removes the instance without invoking the teardown callback.