cpu_hotplug.rst 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. =========================
  2. CPU hotplug in the Kernel
  3. =========================
  4. :Date: September, 2021
  5. :Author: Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
  6. Rusty Russell <rusty@rustcorp.com.au>,
  7. Srivatsa Vaddagiri <vatsa@in.ibm.com>,
  8. Ashok Raj <ashok.raj@intel.com>,
  9. Joel Schopp <jschopp@austin.ibm.com>,
  10. Thomas Gleixner <tglx@kernel.org>
  11. Introduction
  12. ============
  13. Modern advances in system architectures have introduced advanced error
  14. reporting and correction capabilities in processors. There are couple OEMS that
  15. support NUMA hardware which are hot pluggable as well, where physical node
  16. insertion and removal require support for CPU hotplug.
  17. Such advances require CPUs available to a kernel to be removed either for
  18. provisioning reasons, or for RAS purposes to keep an offending CPU off
  19. system execution path. Hence the need for CPU hotplug support in the
  20. Linux kernel.
  21. A more novel use of CPU-hotplug support is its use today in suspend resume
  22. support for SMP. Dual-core and HT support makes even a laptop run SMP kernels
  23. which didn't support these methods.
  24. Command Line Switches
  25. =====================
  26. ``maxcpus=n``
  27. Restrict boot time CPUs to *n*. Say if you have four CPUs, using
  28. ``maxcpus=2`` will only boot two. You can choose to bring the
  29. other CPUs later online.
  30. ``nr_cpus=n``
  31. Restrict the total amount of CPUs the kernel will support. If the number
  32. supplied here is lower than the number of physically available CPUs, then
  33. those CPUs can not be brought online later.
  34. ``possible_cpus=n``
  35. This option sets ``possible_cpus`` bits in ``cpu_possible_mask``.
  36. This option is limited to the X86 and S390 architecture.
  37. ``cpu0_hotplug``
  38. Allow to shutdown CPU0.
  39. This option is limited to the X86 architecture.
  40. CPU maps
  41. ========
  42. ``cpu_possible_mask``
  43. Bitmap of possible CPUs that can ever be available in the
  44. system. This is used to allocate some boot time memory for per_cpu variables
  45. that aren't designed to grow/shrink as CPUs are made available or removed.
  46. Once set during boot time discovery phase, the map is static, i.e no bits
  47. are added or removed anytime. Trimming it accurately for your system needs
  48. upfront can save some boot time memory.
  49. ``cpu_online_mask``
  50. Bitmap of all CPUs currently online. Its set in ``__cpu_up()``
  51. after a CPU is available for kernel scheduling and ready to receive
  52. interrupts from devices. Its cleared when a CPU is brought down using
  53. ``__cpu_disable()``, before which all OS services including interrupts are
  54. migrated to another target CPU.
  55. ``cpu_present_mask``
  56. Bitmap of CPUs currently present in the system. Not all
  57. of them may be online. When physical hotplug is processed by the relevant
  58. subsystem (e.g ACPI) can change and new bit either be added or removed
  59. from the map depending on the event is hot-add/hot-remove. There are currently
  60. no locking rules as of now. Typical usage is to init topology during boot,
  61. at which time hotplug is disabled.
  62. You really don't need to manipulate any of the system CPU maps. They should
  63. be read-only for most use. When setting up per-cpu resources almost always use
  64. ``cpu_possible_mask`` or ``for_each_possible_cpu()`` to iterate. To macro
  65. ``for_each_cpu()`` can be used to iterate over a custom CPU mask.
  66. Never use anything other than ``cpumask_t`` to represent bitmap of CPUs.
  67. Using CPU hotplug
  68. =================
  69. The kernel option *CONFIG_HOTPLUG_CPU* needs to be enabled. It is currently
  70. available on multiple architectures including ARM, MIPS, PowerPC and X86. The
  71. configuration is done via the sysfs interface::
  72. $ ls -lh /sys/devices/system/cpu
  73. total 0
  74. drwxr-xr-x 9 root root 0 Dec 21 16:33 cpu0
  75. drwxr-xr-x 9 root root 0 Dec 21 16:33 cpu1
  76. drwxr-xr-x 9 root root 0 Dec 21 16:33 cpu2
  77. drwxr-xr-x 9 root root 0 Dec 21 16:33 cpu3
  78. drwxr-xr-x 9 root root 0 Dec 21 16:33 cpu4
  79. drwxr-xr-x 9 root root 0 Dec 21 16:33 cpu5
  80. drwxr-xr-x 9 root root 0 Dec 21 16:33 cpu6
  81. drwxr-xr-x 9 root root 0 Dec 21 16:33 cpu7
  82. drwxr-xr-x 2 root root 0 Dec 21 16:33 hotplug
  83. -r--r--r-- 1 root root 4.0K Dec 21 16:33 offline
  84. -r--r--r-- 1 root root 4.0K Dec 21 16:33 online
  85. -r--r--r-- 1 root root 4.0K Dec 21 16:33 possible
  86. -r--r--r-- 1 root root 4.0K Dec 21 16:33 present
  87. The files *offline*, *online*, *possible*, *present* represent the CPU masks.
  88. Each CPU folder contains an *online* file which controls the logical on (1) and
  89. off (0) state. To logically shutdown CPU4::
  90. $ echo 0 > /sys/devices/system/cpu/cpu4/online
  91. smpboot: CPU 4 is now offline
  92. Once the CPU is shutdown, it will be removed from */proc/interrupts*,
  93. */proc/cpuinfo* and should also not be shown visible by the *top* command. To
  94. bring CPU4 back online::
  95. $ echo 1 > /sys/devices/system/cpu/cpu4/online
  96. smpboot: Booting Node 0 Processor 4 APIC 0x1
  97. The CPU is usable again. This should work on all CPUs, but CPU0 is often special
  98. and excluded from CPU hotplug.
  99. The CPU hotplug coordination
  100. ============================
  101. The offline case
  102. ----------------
  103. Once a CPU has been logically shutdown the teardown callbacks of registered
  104. hotplug states will be invoked, starting with ``CPUHP_ONLINE`` and terminating
  105. at state ``CPUHP_OFFLINE``. This includes:
  106. * If tasks are frozen due to a suspend operation then *cpuhp_tasks_frozen*
  107. will be set to true.
  108. * All processes are migrated away from this outgoing CPU to new CPUs.
  109. The new CPU is chosen from each process' current cpuset, which may be
  110. a subset of all online CPUs.
  111. * All interrupts targeted to this CPU are migrated to a new CPU
  112. * timers are also migrated to a new CPU
  113. * Once all services are migrated, kernel calls an arch specific routine
  114. ``__cpu_disable()`` to perform arch specific cleanup.
  115. The CPU hotplug API
  116. ===================
  117. CPU hotplug state machine
  118. -------------------------
  119. CPU hotplug uses a trivial state machine with a linear state space from
  120. CPUHP_OFFLINE to CPUHP_ONLINE. Each state has a startup and a teardown
  121. callback.
  122. When a CPU is onlined, the startup callbacks are invoked sequentially until
  123. the state CPUHP_ONLINE is reached. They can also be invoked when the
  124. callbacks of a state are set up or an instance is added to a multi-instance
  125. state.
  126. When a CPU is offlined the teardown callbacks are invoked in the reverse
  127. order sequentially until the state CPUHP_OFFLINE is reached. They can also
  128. be invoked when the callbacks of a state are removed or an instance is
  129. removed from a multi-instance state.
  130. If a usage site requires only a callback in one direction of the hotplug
  131. operations (CPU online or CPU offline) then the other not-required callback
  132. can be set to NULL when the state is set up.
  133. The state space is divided into three sections:
  134. * The PREPARE section
  135. The PREPARE section covers the state space from CPUHP_OFFLINE to
  136. CPUHP_BRINGUP_CPU.
  137. The startup callbacks in this section are invoked before the CPU is
  138. started during a CPU online operation. The teardown callbacks are invoked
  139. after the CPU has become dysfunctional during a CPU offline operation.
  140. The callbacks are invoked on a control CPU as they can't obviously run on
  141. the hotplugged CPU which is either not yet started or has become
  142. dysfunctional already.
  143. The startup callbacks are used to setup resources which are required to
  144. bring a CPU successfully online. The teardown callbacks are used to free
  145. resources or to move pending work to an online CPU after the hotplugged
  146. CPU became dysfunctional.
  147. The startup callbacks are allowed to fail. If a callback fails, the CPU
  148. online operation is aborted and the CPU is brought down to the previous
  149. state (usually CPUHP_OFFLINE) again.
  150. The teardown callbacks in this section are not allowed to fail.
  151. * The STARTING section
  152. The STARTING section covers the state space between CPUHP_BRINGUP_CPU + 1
  153. and CPUHP_AP_ONLINE.
  154. The startup callbacks in this section are invoked on the hotplugged CPU
  155. with interrupts disabled during a CPU online operation in the early CPU
  156. setup code. The teardown callbacks are invoked with interrupts disabled
  157. on the hotplugged CPU during a CPU offline operation shortly before the
  158. CPU is completely shut down.
  159. The callbacks in this section are not allowed to fail.
  160. The callbacks are used for low level hardware initialization/shutdown and
  161. for core subsystems.
  162. * The ONLINE section
  163. The ONLINE section covers the state space between CPUHP_AP_ONLINE + 1 and
  164. CPUHP_ONLINE.
  165. The startup callbacks in this section are invoked on the hotplugged CPU
  166. during a CPU online operation. The teardown callbacks are invoked on the
  167. hotplugged CPU during a CPU offline operation.
  168. The callbacks are invoked in the context of the per CPU hotplug thread,
  169. which is pinned on the hotplugged CPU. The callbacks are invoked with
  170. interrupts and preemption enabled.
  171. The callbacks are allowed to fail. When a callback fails the hotplug
  172. operation is aborted and the CPU is brought back to the previous state.
  173. CPU online/offline operations
  174. -----------------------------
  175. A successful online operation looks like this::
  176. [CPUHP_OFFLINE]
  177. [CPUHP_OFFLINE + 1]->startup() -> success
  178. [CPUHP_OFFLINE + 2]->startup() -> success
  179. [CPUHP_OFFLINE + 3] -> skipped because startup == NULL
  180. ...
  181. [CPUHP_BRINGUP_CPU]->startup() -> success
  182. === End of PREPARE section
  183. [CPUHP_BRINGUP_CPU + 1]->startup() -> success
  184. ...
  185. [CPUHP_AP_ONLINE]->startup() -> success
  186. === End of STARTUP section
  187. [CPUHP_AP_ONLINE + 1]->startup() -> success
  188. ...
  189. [CPUHP_ONLINE - 1]->startup() -> success
  190. [CPUHP_ONLINE]
  191. A successful offline operation looks like this::
  192. [CPUHP_ONLINE]
  193. [CPUHP_ONLINE - 1]->teardown() -> success
  194. ...
  195. [CPUHP_AP_ONLINE + 1]->teardown() -> success
  196. === Start of STARTUP section
  197. [CPUHP_AP_ONLINE]->teardown() -> success
  198. ...
  199. [CPUHP_BRINGUP_ONLINE - 1]->teardown()
  200. ...
  201. === Start of PREPARE section
  202. [CPUHP_BRINGUP_CPU]->teardown()
  203. [CPUHP_OFFLINE + 3]->teardown()
  204. [CPUHP_OFFLINE + 2] -> skipped because teardown == NULL
  205. [CPUHP_OFFLINE + 1]->teardown()
  206. [CPUHP_OFFLINE]
  207. A failed online operation looks like this::
  208. [CPUHP_OFFLINE]
  209. [CPUHP_OFFLINE + 1]->startup() -> success
  210. [CPUHP_OFFLINE + 2]->startup() -> success
  211. [CPUHP_OFFLINE + 3] -> skipped because startup == NULL
  212. ...
  213. [CPUHP_BRINGUP_CPU]->startup() -> success
  214. === End of PREPARE section
  215. [CPUHP_BRINGUP_CPU + 1]->startup() -> success
  216. ...
  217. [CPUHP_AP_ONLINE]->startup() -> success
  218. === End of STARTUP section
  219. [CPUHP_AP_ONLINE + 1]->startup() -> success
  220. ---
  221. [CPUHP_AP_ONLINE + N]->startup() -> fail
  222. [CPUHP_AP_ONLINE + (N - 1)]->teardown()
  223. ...
  224. [CPUHP_AP_ONLINE + 1]->teardown()
  225. === Start of STARTUP section
  226. [CPUHP_AP_ONLINE]->teardown()
  227. ...
  228. [CPUHP_BRINGUP_ONLINE - 1]->teardown()
  229. ...
  230. === Start of PREPARE section
  231. [CPUHP_BRINGUP_CPU]->teardown()
  232. [CPUHP_OFFLINE + 3]->teardown()
  233. [CPUHP_OFFLINE + 2] -> skipped because teardown == NULL
  234. [CPUHP_OFFLINE + 1]->teardown()
  235. [CPUHP_OFFLINE]
  236. A failed offline operation looks like this::
  237. [CPUHP_ONLINE]
  238. [CPUHP_ONLINE - 1]->teardown() -> success
  239. ...
  240. [CPUHP_ONLINE - N]->teardown() -> fail
  241. [CPUHP_ONLINE - (N - 1)]->startup()
  242. ...
  243. [CPUHP_ONLINE - 1]->startup()
  244. [CPUHP_ONLINE]
  245. Recursive failures cannot be handled sensibly. Look at the following
  246. example of a recursive fail due to a failed offline operation: ::
  247. [CPUHP_ONLINE]
  248. [CPUHP_ONLINE - 1]->teardown() -> success
  249. ...
  250. [CPUHP_ONLINE - N]->teardown() -> fail
  251. [CPUHP_ONLINE - (N - 1)]->startup() -> success
  252. [CPUHP_ONLINE - (N - 2)]->startup() -> fail
  253. The CPU hotplug state machine stops right here and does not try to go back
  254. down again because that would likely result in an endless loop::
  255. [CPUHP_ONLINE - (N - 1)]->teardown() -> success
  256. [CPUHP_ONLINE - N]->teardown() -> fail
  257. [CPUHP_ONLINE - (N - 1)]->startup() -> success
  258. [CPUHP_ONLINE - (N - 2)]->startup() -> fail
  259. [CPUHP_ONLINE - (N - 1)]->teardown() -> success
  260. [CPUHP_ONLINE - N]->teardown() -> fail
  261. Lather, rinse and repeat. In this case the CPU left in state::
  262. [CPUHP_ONLINE - (N - 1)]
  263. which at least lets the system make progress and gives the user a chance to
  264. debug or even resolve the situation.
  265. Allocating a state
  266. ------------------
  267. There are two ways to allocate a CPU hotplug state:
  268. * Static allocation
  269. Static allocation has to be used when the subsystem or driver has
  270. ordering requirements versus other CPU hotplug states. E.g. the PERF core
  271. startup callback has to be invoked before the PERF driver startup
  272. callbacks during a CPU online operation. During a CPU offline operation
  273. the driver teardown callbacks have to be invoked before the core teardown
  274. callback. The statically allocated states are described by constants in
  275. the cpuhp_state enum which can be found in include/linux/cpuhotplug.h.
  276. Insert the state into the enum at the proper place so the ordering
  277. requirements are fulfilled. The state constant has to be used for state
  278. setup and removal.
  279. Static allocation is also required when the state callbacks are not set
  280. up at runtime and are part of the initializer of the CPU hotplug state
  281. array in kernel/cpu.c.
  282. * Dynamic allocation
  283. When there are no ordering requirements for the state callbacks then
  284. dynamic allocation is the preferred method. The state number is allocated
  285. by the setup function and returned to the caller on success.
  286. Only the PREPARE and ONLINE sections provide a dynamic allocation
  287. range. The STARTING section does not as most of the callbacks in that
  288. section have explicit ordering requirements.
  289. Setup of a CPU hotplug state
  290. ----------------------------
  291. The core code provides the following functions to setup a state:
  292. * cpuhp_setup_state(state, name, startup, teardown)
  293. * cpuhp_setup_state_nocalls(state, name, startup, teardown)
  294. * cpuhp_setup_state_cpuslocked(state, name, startup, teardown)
  295. * cpuhp_setup_state_nocalls_cpuslocked(state, name, startup, teardown)
  296. For cases where a driver or a subsystem has multiple instances and the same
  297. CPU hotplug state callbacks need to be invoked for each instance, the CPU
  298. hotplug core provides multi-instance support. The advantage over driver
  299. specific instance lists is that the instance related functions are fully
  300. serialized against CPU hotplug operations and provide the automatic
  301. invocations of the state callbacks on add and removal. To set up such a
  302. multi-instance state the following function is available:
  303. * cpuhp_setup_state_multi(state, name, startup, teardown)
  304. The @state argument is either a statically allocated state or one of the
  305. constants for dynamically allocated states - CPUHP_BP_PREPARE_DYN,
  306. CPUHP_AP_ONLINE_DYN - depending on the state section (PREPARE, ONLINE) for
  307. which a dynamic state should be allocated.
  308. The @name argument is used for sysfs output and for instrumentation. The
  309. naming convention is "subsys:mode" or "subsys/driver:mode",
  310. e.g. "perf:mode" or "perf/x86:mode". The common mode names are:
  311. ======== =======================================================
  312. prepare For states in the PREPARE section
  313. dead For states in the PREPARE section which do not provide
  314. a startup callback
  315. starting For states in the STARTING section
  316. dying For states in the STARTING section which do not provide
  317. a startup callback
  318. online For states in the ONLINE section
  319. offline For states in the ONLINE section which do not provide
  320. a startup callback
  321. ======== =======================================================
  322. As the @name argument is only used for sysfs and instrumentation other mode
  323. descriptors can be used as well if they describe the nature of the state
  324. better than the common ones.
  325. Examples for @name arguments: "perf/online", "perf/x86:prepare",
  326. "RCU/tree:dying", "sched/waitempty"
  327. The @startup argument is a function pointer to the callback which should be
  328. invoked during a CPU online operation. If the usage site does not require a
  329. startup callback set the pointer to NULL.
  330. The @teardown argument is a function pointer to the callback which should
  331. be invoked during a CPU offline operation. If the usage site does not
  332. require a teardown callback set the pointer to NULL.
  333. The functions differ in the way how the installed callbacks are treated:
  334. * cpuhp_setup_state_nocalls(), cpuhp_setup_state_nocalls_cpuslocked()
  335. and cpuhp_setup_state_multi() only install the callbacks
  336. * cpuhp_setup_state() and cpuhp_setup_state_cpuslocked() install the
  337. callbacks and invoke the @startup callback (if not NULL) for all online
  338. CPUs which have currently a state greater than the newly installed
  339. state. Depending on the state section the callback is either invoked on
  340. the current CPU (PREPARE section) or on each online CPU (ONLINE
  341. section) in the context of the CPU's hotplug thread.
  342. If a callback fails for CPU N then the teardown callback for CPU
  343. 0 .. N-1 is invoked to rollback the operation. The state setup fails,
  344. the callbacks for the state are not installed and in case of dynamic
  345. allocation the allocated state is freed.
  346. The state setup and the callback invocations are serialized against CPU
  347. hotplug operations. If the setup function has to be called from a CPU
  348. hotplug read locked region, then the _cpuslocked() variants have to be
  349. used. These functions cannot be used from within CPU hotplug callbacks.
  350. The function return values:
  351. ======== ===================================================================
  352. 0 Statically allocated state was successfully set up
  353. >0 Dynamically allocated state was successfully set up.
  354. The returned number is the state number which was allocated. If
  355. the state callbacks have to be removed later, e.g. module
  356. removal, then this number has to be saved by the caller and used
  357. as @state argument for the state remove function. For
  358. multi-instance states the dynamically allocated state number is
  359. also required as @state argument for the instance add/remove
  360. operations.
  361. <0 Operation failed
  362. ======== ===================================================================
  363. Removal of a CPU hotplug state
  364. ------------------------------
  365. To remove a previously set up state, the following functions are provided:
  366. * cpuhp_remove_state(state)
  367. * cpuhp_remove_state_nocalls(state)
  368. * cpuhp_remove_state_nocalls_cpuslocked(state)
  369. * cpuhp_remove_multi_state(state)
  370. The @state argument is either a statically allocated state or the state
  371. number which was allocated in the dynamic range by cpuhp_setup_state*(). If
  372. the state is in the dynamic range, then the state number is freed and
  373. available for dynamic allocation again.
  374. The functions differ in the way how the installed callbacks are treated:
  375. * cpuhp_remove_state_nocalls(), cpuhp_remove_state_nocalls_cpuslocked()
  376. and cpuhp_remove_multi_state() only remove the callbacks.
  377. * cpuhp_remove_state() removes the callbacks and invokes the teardown
  378. callback (if not NULL) for all online CPUs which have currently a state
  379. greater than the removed state. Depending on the state section the
  380. callback is either invoked on the current CPU (PREPARE section) or on
  381. each online CPU (ONLINE section) in the context of the CPU's hotplug
  382. thread.
  383. In order to complete the removal, the teardown callback should not fail.
  384. The state removal and the callback invocations are serialized against CPU
  385. hotplug operations. If the remove function has to be called from a CPU
  386. hotplug read locked region, then the _cpuslocked() variants have to be
  387. used. These functions cannot be used from within CPU hotplug callbacks.
  388. If a multi-instance state is removed then the caller has to remove all
  389. instances first.
  390. Multi-Instance state instance management
  391. ----------------------------------------
  392. Once the multi-instance state is set up, instances can be added to the
  393. state:
  394. * cpuhp_state_add_instance(state, node)
  395. * cpuhp_state_add_instance_nocalls(state, node)
  396. The @state argument is either a statically allocated state or the state
  397. number which was allocated in the dynamic range by cpuhp_setup_state_multi().
  398. The @node argument is a pointer to an hlist_node which is embedded in the
  399. instance's data structure. The pointer is handed to the multi-instance
  400. state callbacks and can be used by the callback to retrieve the instance
  401. via container_of().
  402. The functions differ in the way how the installed callbacks are treated:
  403. * cpuhp_state_add_instance_nocalls() and only adds the instance to the
  404. multi-instance state's node list.
  405. * cpuhp_state_add_instance() adds the instance and invokes the startup
  406. callback (if not NULL) associated with @state for all online CPUs which
  407. have currently a state greater than @state. The callback is only
  408. invoked for the to be added instance. Depending on the state section
  409. the callback is either invoked on the current CPU (PREPARE section) or
  410. on each online CPU (ONLINE section) in the context of the CPU's hotplug
  411. thread.
  412. If a callback fails for CPU N then the teardown callback for CPU
  413. 0 .. N-1 is invoked to rollback the operation, the function fails and
  414. the instance is not added to the node list of the multi-instance state.
  415. To remove an instance from the state's node list these functions are
  416. available:
  417. * cpuhp_state_remove_instance(state, node)
  418. * cpuhp_state_remove_instance_nocalls(state, node)
  419. The arguments are the same as for the cpuhp_state_add_instance*()
  420. variants above.
  421. The functions differ in the way how the installed callbacks are treated:
  422. * cpuhp_state_remove_instance_nocalls() only removes the instance from the
  423. state's node list.
  424. * cpuhp_state_remove_instance() removes the instance and invokes the
  425. teardown callback (if not NULL) associated with @state for all online
  426. CPUs which have currently a state greater than @state. The callback is
  427. only invoked for the to be removed instance. Depending on the state
  428. section the callback is either invoked on the current CPU (PREPARE
  429. section) or on each online CPU (ONLINE section) in the context of the
  430. CPU's hotplug thread.
  431. In order to complete the removal, the teardown callback should not fail.
  432. The node list add/remove operations and the callback invocations are
  433. serialized against CPU hotplug operations. These functions cannot be used
  434. from within CPU hotplug callbacks and CPU hotplug read locked regions.
  435. Examples
  436. --------
  437. Setup and teardown a statically allocated state in the STARTING section for
  438. notifications on online and offline operations::
  439. ret = cpuhp_setup_state(CPUHP_SUBSYS_STARTING, "subsys:starting", subsys_cpu_starting, subsys_cpu_dying);
  440. if (ret < 0)
  441. return ret;
  442. ....
  443. cpuhp_remove_state(CPUHP_SUBSYS_STARTING);
  444. Setup and teardown a dynamically allocated state in the ONLINE section
  445. for notifications on offline operations::
  446. state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "subsys:offline", NULL, subsys_cpu_offline);
  447. if (state < 0)
  448. return state;
  449. ....
  450. cpuhp_remove_state(state);
  451. Setup and teardown a dynamically allocated state in the ONLINE section
  452. for notifications on online operations without invoking the callbacks::
  453. state = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "subsys:online", subsys_cpu_online, NULL);
  454. if (state < 0)
  455. return state;
  456. ....
  457. cpuhp_remove_state_nocalls(state);
  458. Setup, use and teardown a dynamically allocated multi-instance state in the
  459. ONLINE section for notifications on online and offline operation::
  460. state = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "subsys:online", subsys_cpu_online, subsys_cpu_offline);
  461. if (state < 0)
  462. return state;
  463. ....
  464. ret = cpuhp_state_add_instance(state, &inst1->node);
  465. if (ret)
  466. return ret;
  467. ....
  468. ret = cpuhp_state_add_instance(state, &inst2->node);
  469. if (ret)
  470. return ret;
  471. ....
  472. cpuhp_remove_instance(state, &inst1->node);
  473. ....
  474. cpuhp_remove_instance(state, &inst2->node);
  475. ....
  476. cpuhp_remove_multi_state(state);
  477. Testing of hotplug states
  478. =========================
  479. One way to verify whether a custom state is working as expected or not is to
  480. shutdown a CPU and then put it online again. It is also possible to put the CPU
  481. to certain state (for instance *CPUHP_AP_ONLINE*) and then go back to
  482. *CPUHP_ONLINE*. This would simulate an error one state after *CPUHP_AP_ONLINE*
  483. which would lead to rollback to the online state.
  484. All registered states are enumerated in ``/sys/devices/system/cpu/hotplug/states`` ::
  485. $ tail /sys/devices/system/cpu/hotplug/states
  486. 138: mm/vmscan:online
  487. 139: mm/vmstat:online
  488. 140: lib/percpu_cnt:online
  489. 141: acpi/cpu-drv:online
  490. 142: base/cacheinfo:online
  491. 143: virtio/net:online
  492. 144: x86/mce:online
  493. 145: printk:online
  494. 168: sched:active
  495. 169: online
  496. To rollback CPU4 to ``lib/percpu_cnt:online`` and back online just issue::
  497. $ cat /sys/devices/system/cpu/cpu4/hotplug/state
  498. 169
  499. $ echo 140 > /sys/devices/system/cpu/cpu4/hotplug/target
  500. $ cat /sys/devices/system/cpu/cpu4/hotplug/state
  501. 140
  502. It is important to note that the teardown callback of state 140 have been
  503. invoked. And now get back online::
  504. $ echo 169 > /sys/devices/system/cpu/cpu4/hotplug/target
  505. $ cat /sys/devices/system/cpu/cpu4/hotplug/state
  506. 169
  507. With trace events enabled, the individual steps are visible, too::
  508. # TASK-PID CPU# TIMESTAMP FUNCTION
  509. # | | | | |
  510. bash-394 [001] 22.976: cpuhp_enter: cpu: 0004 target: 140 step: 169 (cpuhp_kick_ap_work)
  511. cpuhp/4-31 [004] 22.977: cpuhp_enter: cpu: 0004 target: 140 step: 168 (sched_cpu_deactivate)
  512. cpuhp/4-31 [004] 22.990: cpuhp_exit: cpu: 0004 state: 168 step: 168 ret: 0
  513. cpuhp/4-31 [004] 22.991: cpuhp_enter: cpu: 0004 target: 140 step: 144 (mce_cpu_pre_down)
  514. cpuhp/4-31 [004] 22.992: cpuhp_exit: cpu: 0004 state: 144 step: 144 ret: 0
  515. cpuhp/4-31 [004] 22.993: cpuhp_multi_enter: cpu: 0004 target: 140 step: 143 (virtnet_cpu_down_prep)
  516. cpuhp/4-31 [004] 22.994: cpuhp_exit: cpu: 0004 state: 143 step: 143 ret: 0
  517. cpuhp/4-31 [004] 22.995: cpuhp_enter: cpu: 0004 target: 140 step: 142 (cacheinfo_cpu_pre_down)
  518. cpuhp/4-31 [004] 22.996: cpuhp_exit: cpu: 0004 state: 142 step: 142 ret: 0
  519. bash-394 [001] 22.997: cpuhp_exit: cpu: 0004 state: 140 step: 169 ret: 0
  520. bash-394 [005] 95.540: cpuhp_enter: cpu: 0004 target: 169 step: 140 (cpuhp_kick_ap_work)
  521. cpuhp/4-31 [004] 95.541: cpuhp_enter: cpu: 0004 target: 169 step: 141 (acpi_soft_cpu_online)
  522. cpuhp/4-31 [004] 95.542: cpuhp_exit: cpu: 0004 state: 141 step: 141 ret: 0
  523. cpuhp/4-31 [004] 95.543: cpuhp_enter: cpu: 0004 target: 169 step: 142 (cacheinfo_cpu_online)
  524. cpuhp/4-31 [004] 95.544: cpuhp_exit: cpu: 0004 state: 142 step: 142 ret: 0
  525. cpuhp/4-31 [004] 95.545: cpuhp_multi_enter: cpu: 0004 target: 169 step: 143 (virtnet_cpu_online)
  526. cpuhp/4-31 [004] 95.546: cpuhp_exit: cpu: 0004 state: 143 step: 143 ret: 0
  527. cpuhp/4-31 [004] 95.547: cpuhp_enter: cpu: 0004 target: 169 step: 144 (mce_cpu_online)
  528. cpuhp/4-31 [004] 95.548: cpuhp_exit: cpu: 0004 state: 144 step: 144 ret: 0
  529. cpuhp/4-31 [004] 95.549: cpuhp_enter: cpu: 0004 target: 169 step: 145 (console_cpu_notify)
  530. cpuhp/4-31 [004] 95.550: cpuhp_exit: cpu: 0004 state: 145 step: 145 ret: 0
  531. cpuhp/4-31 [004] 95.551: cpuhp_enter: cpu: 0004 target: 169 step: 168 (sched_cpu_activate)
  532. cpuhp/4-31 [004] 95.552: cpuhp_exit: cpu: 0004 state: 168 step: 168 ret: 0
  533. bash-394 [005] 95.553: cpuhp_exit: cpu: 0004 state: 169 step: 140 ret: 0
  534. As it an be seen, CPU4 went down until timestamp 22.996 and then back up until
  535. 95.552. All invoked callbacks including their return codes are visible in the
  536. trace.
  537. Architecture's requirements
  538. ===========================
  539. The following functions and configurations are required:
  540. ``CONFIG_HOTPLUG_CPU``
  541. This entry needs to be enabled in Kconfig
  542. ``__cpu_up()``
  543. Arch interface to bring up a CPU
  544. ``__cpu_disable()``
  545. Arch interface to shutdown a CPU, no more interrupts can be handled by the
  546. kernel after the routine returns. This includes the shutdown of the timer.
  547. ``__cpu_die()``
  548. This actually supposed to ensure death of the CPU. Actually look at some
  549. example code in other arch that implement CPU hotplug. The processor is taken
  550. down from the ``idle()`` loop for that specific architecture. ``__cpu_die()``
  551. typically waits for some per_cpu state to be set, to ensure the processor dead
  552. routine is called to be sure positively.
  553. User Space Notification
  554. =======================
  555. After CPU successfully onlined or offline udev events are sent. A udev rule like::
  556. SUBSYSTEM=="cpu", DRIVERS=="processor", DEVPATH=="/devices/system/cpu/*", RUN+="the_hotplug_receiver.sh"
  557. will receive all events. A script like::
  558. #!/bin/sh
  559. if [ "${ACTION}" = "offline" ]
  560. then
  561. echo "CPU ${DEVPATH##*/} offline"
  562. elif [ "${ACTION}" = "online" ]
  563. then
  564. echo "CPU ${DEVPATH##*/} online"
  565. fi
  566. can process the event further.
  567. When changes to the CPUs in the system occur, the sysfs file
  568. /sys/devices/system/cpu/crash_hotplug contains '1' if the kernel
  569. updates the kdump capture kernel list of CPUs itself (via elfcorehdr and
  570. other relevant kexec segment), or '0' if userspace must update the kdump
  571. capture kernel list of CPUs.
  572. The availability depends on the CONFIG_HOTPLUG_CPU kernel configuration
  573. option.
  574. To skip userspace processing of CPU hot un/plug events for kdump
  575. (i.e. the unload-then-reload to obtain a current list of CPUs), this sysfs
  576. file can be used in a udev rule as follows:
  577. SUBSYSTEM=="cpu", ATTRS{crash_hotplug}=="1", GOTO="kdump_reload_end"
  578. For a CPU hot un/plug event, if the architecture supports kernel updates
  579. of the elfcorehdr (which contains the list of CPUs) and other relevant
  580. kexec segments, then the rule skips the unload-then-reload of the kdump
  581. capture kernel.
  582. Kernel Inline Documentations Reference
  583. ======================================
  584. .. kernel-doc:: include/linux/cpuhotplug.h