suspend-and-cpuhotplug.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. Interaction of Suspend code (S3) with the CPU hotplug infrastructure
  2. (C) 2011 - 2014 Srivatsa S. Bhat <srivatsa.bhat@linux.vnet.ibm.com>
  3. I. How does the regular CPU hotplug code differ from how the Suspend-to-RAM
  4. infrastructure uses it internally? And where do they share common code?
  5. Well, a picture is worth a thousand words... So ASCII art follows :-)
  6. [This depicts the current design in the kernel, and focusses only on the
  7. interactions involving the freezer and CPU hotplug and also tries to explain
  8. the locking involved. It outlines the notifications involved as well.
  9. But please note that here, only the call paths are illustrated, with the aim
  10. of describing where they take different paths and where they share code.
  11. What happens when regular CPU hotplug and Suspend-to-RAM race with each other
  12. is not depicted here.]
  13. On a high level, the suspend-resume cycle goes like this:
  14. |Freeze| -> |Disable nonboot| -> |Do suspend| -> |Enable nonboot| -> |Thaw |
  15. |tasks | | cpus | | | | cpus | |tasks|
  16. More details follow:
  17. Suspend call path
  18. -----------------
  19. Write 'mem' to
  20. /sys/power/state
  21. sysfs file
  22. |
  23. v
  24. Acquire pm_mutex lock
  25. |
  26. v
  27. Send PM_SUSPEND_PREPARE
  28. notifications
  29. |
  30. v
  31. Freeze tasks
  32. |
  33. |
  34. v
  35. disable_nonboot_cpus()
  36. /* start */
  37. |
  38. v
  39. Acquire cpu_add_remove_lock
  40. |
  41. v
  42. Iterate over CURRENTLY
  43. online CPUs
  44. |
  45. |
  46. | ----------
  47. v | L
  48. ======> _cpu_down() |
  49. | [This takes cpuhotplug.lock |
  50. Common | before taking down the CPU |
  51. code | and releases it when done] | O
  52. | While it is at it, notifications |
  53. | are sent when notable events occur, |
  54. ======> by running all registered callbacks. |
  55. | | O
  56. | |
  57. | |
  58. v |
  59. Note down these cpus in | P
  60. frozen_cpus mask ----------
  61. |
  62. v
  63. Disable regular cpu hotplug
  64. by increasing cpu_hotplug_disabled
  65. |
  66. v
  67. Release cpu_add_remove_lock
  68. |
  69. v
  70. /* disable_nonboot_cpus() complete */
  71. |
  72. v
  73. Do suspend
  74. Resuming back is likewise, with the counterparts being (in the order of
  75. execution during resume):
  76. * enable_nonboot_cpus() which involves:
  77. | Acquire cpu_add_remove_lock
  78. | Decrease cpu_hotplug_disabled, thereby enabling regular cpu hotplug
  79. | Call _cpu_up() [for all those cpus in the frozen_cpus mask, in a loop]
  80. | Release cpu_add_remove_lock
  81. v
  82. * thaw tasks
  83. * send PM_POST_SUSPEND notifications
  84. * Release pm_mutex lock.
  85. It is to be noted here that the pm_mutex lock is acquired at the very
  86. beginning, when we are just starting out to suspend, and then released only
  87. after the entire cycle is complete (i.e., suspend + resume).
  88. Regular CPU hotplug call path
  89. -----------------------------
  90. Write 0 (or 1) to
  91. /sys/devices/system/cpu/cpu*/online
  92. sysfs file
  93. |
  94. |
  95. v
  96. cpu_down()
  97. |
  98. v
  99. Acquire cpu_add_remove_lock
  100. |
  101. v
  102. If cpu_hotplug_disabled > 0
  103. return gracefully
  104. |
  105. |
  106. v
  107. ======> _cpu_down()
  108. | [This takes cpuhotplug.lock
  109. Common | before taking down the CPU
  110. code | and releases it when done]
  111. | While it is at it, notifications
  112. | are sent when notable events occur,
  113. ======> by running all registered callbacks.
  114. |
  115. |
  116. v
  117. Release cpu_add_remove_lock
  118. [That's it!, for
  119. regular CPU hotplug]
  120. So, as can be seen from the two diagrams (the parts marked as "Common code"),
  121. regular CPU hotplug and the suspend code path converge at the _cpu_down() and
  122. _cpu_up() functions. They differ in the arguments passed to these functions,
  123. in that during regular CPU hotplug, 0 is passed for the 'tasks_frozen'
  124. argument. But during suspend, since the tasks are already frozen by the time
  125. the non-boot CPUs are offlined or onlined, the _cpu_*() functions are called
  126. with the 'tasks_frozen' argument set to 1.
  127. [See below for some known issues regarding this.]
  128. Important files and functions/entry points:
  129. ------------------------------------------
  130. kernel/power/process.c : freeze_processes(), thaw_processes()
  131. kernel/power/suspend.c : suspend_prepare(), suspend_enter(), suspend_finish()
  132. kernel/cpu.c: cpu_[up|down](), _cpu_[up|down](), [disable|enable]_nonboot_cpus()
  133. II. What are the issues involved in CPU hotplug?
  134. -------------------------------------------
  135. There are some interesting situations involving CPU hotplug and microcode
  136. update on the CPUs, as discussed below:
  137. [Please bear in mind that the kernel requests the microcode images from
  138. userspace, using the request_firmware() function defined in
  139. drivers/base/firmware_class.c]
  140. a. When all the CPUs are identical:
  141. This is the most common situation and it is quite straightforward: we want
  142. to apply the same microcode revision to each of the CPUs.
  143. To give an example of x86, the collect_cpu_info() function defined in
  144. arch/x86/kernel/microcode_core.c helps in discovering the type of the CPU
  145. and thereby in applying the correct microcode revision to it.
  146. But note that the kernel does not maintain a common microcode image for the
  147. all CPUs, in order to handle case 'b' described below.
  148. b. When some of the CPUs are different than the rest:
  149. In this case since we probably need to apply different microcode revisions
  150. to different CPUs, the kernel maintains a copy of the correct microcode
  151. image for each CPU (after appropriate CPU type/model discovery using
  152. functions such as collect_cpu_info()).
  153. c. When a CPU is physically hot-unplugged and a new (and possibly different
  154. type of) CPU is hot-plugged into the system:
  155. In the current design of the kernel, whenever a CPU is taken offline during
  156. a regular CPU hotplug operation, upon receiving the CPU_DEAD notification
  157. (which is sent by the CPU hotplug code), the microcode update driver's
  158. callback for that event reacts by freeing the kernel's copy of the
  159. microcode image for that CPU.
  160. Hence, when a new CPU is brought online, since the kernel finds that it
  161. doesn't have the microcode image, it does the CPU type/model discovery
  162. afresh and then requests the userspace for the appropriate microcode image
  163. for that CPU, which is subsequently applied.
  164. For example, in x86, the mc_cpu_callback() function (which is the microcode
  165. update driver's callback registered for CPU hotplug events) calls
  166. microcode_update_cpu() which would call microcode_init_cpu() in this case,
  167. instead of microcode_resume_cpu() when it finds that the kernel doesn't
  168. have a valid microcode image. This ensures that the CPU type/model
  169. discovery is performed and the right microcode is applied to the CPU after
  170. getting it from userspace.
  171. d. Handling microcode update during suspend/hibernate:
  172. Strictly speaking, during a CPU hotplug operation which does not involve
  173. physically removing or inserting CPUs, the CPUs are not actually powered
  174. off during a CPU offline. They are just put to the lowest C-states possible.
  175. Hence, in such a case, it is not really necessary to re-apply microcode
  176. when the CPUs are brought back online, since they wouldn't have lost the
  177. image during the CPU offline operation.
  178. This is the usual scenario encountered during a resume after a suspend.
  179. However, in the case of hibernation, since all the CPUs are completely
  180. powered off, during restore it becomes necessary to apply the microcode
  181. images to all the CPUs.
  182. [Note that we don't expect someone to physically pull out nodes and insert
  183. nodes with a different type of CPUs in-between a suspend-resume or a
  184. hibernate/restore cycle.]
  185. In the current design of the kernel however, during a CPU offline operation
  186. as part of the suspend/hibernate cycle (the CPU_DEAD_FROZEN notification),
  187. the existing copy of microcode image in the kernel is not freed up.
  188. And during the CPU online operations (during resume/restore), since the
  189. kernel finds that it already has copies of the microcode images for all the
  190. CPUs, it just applies them to the CPUs, avoiding any re-discovery of CPU
  191. type/model and the need for validating whether the microcode revisions are
  192. right for the CPUs or not (due to the above assumption that physical CPU
  193. hotplug will not be done in-between suspend/resume or hibernate/restore
  194. cycles).
  195. III. Are there any known problems when regular CPU hotplug and suspend race
  196. with each other?
  197. Yes, they are listed below:
  198. 1. When invoking regular CPU hotplug, the 'tasks_frozen' argument passed to
  199. the _cpu_down() and _cpu_up() functions is *always* 0.
  200. This might not reflect the true current state of the system, since the
  201. tasks could have been frozen by an out-of-band event such as a suspend
  202. operation in progress. Hence, it will lead to wrong notifications being
  203. sent during the cpu online/offline events (eg, CPU_ONLINE notification
  204. instead of CPU_ONLINE_FROZEN) which in turn will lead to execution of
  205. inappropriate code by the callbacks registered for such CPU hotplug events.
  206. 2. If a regular CPU hotplug stress test happens to race with the freezer due
  207. to a suspend operation in progress at the same time, then we could hit the
  208. situation described below:
  209. * A regular cpu online operation continues its journey from userspace
  210. into the kernel, since the freezing has not yet begun.
  211. * Then freezer gets to work and freezes userspace.
  212. * If cpu online has not yet completed the microcode update stuff by now,
  213. it will now start waiting on the frozen userspace in the
  214. TASK_UNINTERRUPTIBLE state, in order to get the microcode image.
  215. * Now the freezer continues and tries to freeze the remaining tasks. But
  216. due to this wait mentioned above, the freezer won't be able to freeze
  217. the cpu online hotplug task and hence freezing of tasks fails.
  218. As a result of this task freezing failure, the suspend operation gets
  219. aborted.