memory-hotplug.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. ==============
  2. Memory Hotplug
  3. ==============
  4. Created: Jul 28 2007
  5. Add description of notifier of memory hotplug Oct 11 2007
  6. This document is about memory hotplug including how-to-use and current status.
  7. Because Memory Hotplug is still under development, contents of this text will
  8. be changed often.
  9. 1. Introduction
  10. 1.1 purpose of memory hotplug
  11. 1.2. Phases of memory hotplug
  12. 1.3. Unit of Memory online/offline operation
  13. 2. Kernel Configuration
  14. 3. sysfs files for memory hotplug
  15. 4. Physical memory hot-add phase
  16. 4.1 Hardware(Firmware) Support
  17. 4.2 Notify memory hot-add event by hand
  18. 5. Logical Memory hot-add phase
  19. 5.1. State of memory
  20. 5.2. How to online memory
  21. 6. Logical memory remove
  22. 6.1 Memory offline and ZONE_MOVABLE
  23. 6.2. How to offline memory
  24. 7. Physical memory remove
  25. 8. Memory hotplug event notifier
  26. 9. Future Work List
  27. Note(1): x86_64's has special implementation for memory hotplug.
  28. This text does not describe it.
  29. Note(2): This text assumes that sysfs is mounted at /sys.
  30. ---------------
  31. 1. Introduction
  32. ---------------
  33. 1.1 purpose of memory hotplug
  34. ------------
  35. Memory Hotplug allows users to increase/decrease the amount of memory.
  36. Generally, there are two purposes.
  37. (A) For changing the amount of memory.
  38. This is to allow a feature like capacity on demand.
  39. (B) For installing/removing DIMMs or NUMA-nodes physically.
  40. This is to exchange DIMMs/NUMA-nodes, reduce power consumption, etc.
  41. (A) is required by highly virtualized environments and (B) is required by
  42. hardware which supports memory power management.
  43. Linux memory hotplug is designed for both purpose.
  44. 1.2. Phases of memory hotplug
  45. ---------------
  46. There are 2 phases in Memory Hotplug.
  47. 1) Physical Memory Hotplug phase
  48. 2) Logical Memory Hotplug phase.
  49. The First phase is to communicate hardware/firmware and make/erase
  50. environment for hotplugged memory. Basically, this phase is necessary
  51. for the purpose (B), but this is good phase for communication between
  52. highly virtualized environments too.
  53. When memory is hotplugged, the kernel recognizes new memory, makes new memory
  54. management tables, and makes sysfs files for new memory's operation.
  55. If firmware supports notification of connection of new memory to OS,
  56. this phase is triggered automatically. ACPI can notify this event. If not,
  57. "probe" operation by system administration is used instead.
  58. (see Section 4.).
  59. Logical Memory Hotplug phase is to change memory state into
  60. available/unavailable for users. Amount of memory from user's view is
  61. changed by this phase. The kernel makes all memory in it as free pages
  62. when a memory range is available.
  63. In this document, this phase is described as online/offline.
  64. Logical Memory Hotplug phase is triggered by write of sysfs file by system
  65. administrator. For the hot-add case, it must be executed after Physical Hotplug
  66. phase by hand.
  67. (However, if you writes udev's hotplug scripts for memory hotplug, these
  68. phases can be execute in seamless way.)
  69. 1.3. Unit of Memory online/offline operation
  70. ------------
  71. Memory hotplug uses SPARSEMEM memory model. SPARSEMEM divides the whole memory
  72. into chunks of the same size. The chunk is called a "section". The size of
  73. a section is architecture dependent. For example, power uses 16MiB, ia64 uses
  74. 1GiB. The unit of online/offline operation is "one section". (see Section 3.)
  75. To determine the size of sections, please read this file:
  76. /sys/devices/system/memory/block_size_bytes
  77. This file shows the size of sections in byte.
  78. -----------------------
  79. 2. Kernel Configuration
  80. -----------------------
  81. To use memory hotplug feature, kernel must be compiled with following
  82. config options.
  83. - For all memory hotplug
  84. Memory model -> Sparse Memory (CONFIG_SPARSEMEM)
  85. Allow for memory hot-add (CONFIG_MEMORY_HOTPLUG)
  86. - To enable memory removal, the followings are also necessary
  87. Allow for memory hot remove (CONFIG_MEMORY_HOTREMOVE)
  88. Page Migration (CONFIG_MIGRATION)
  89. - For ACPI memory hotplug, the followings are also necessary
  90. Memory hotplug (under ACPI Support menu) (CONFIG_ACPI_HOTPLUG_MEMORY)
  91. This option can be kernel module.
  92. - As a related configuration, if your box has a feature of NUMA-node hotplug
  93. via ACPI, then this option is necessary too.
  94. ACPI0004,PNP0A05 and PNP0A06 Container Driver (under ACPI Support menu)
  95. (CONFIG_ACPI_CONTAINER).
  96. This option can be kernel module too.
  97. --------------------------------
  98. 4 sysfs files for memory hotplug
  99. --------------------------------
  100. All sections have their device information under /sys/devices/system/memory as
  101. /sys/devices/system/memory/memoryXXX
  102. (XXX is section id.)
  103. Now, XXX is defined as start_address_of_section / section_size.
  104. For example, assume 1GiB section size. A device for a memory starting at
  105. 0x100000000 is /sys/device/system/memory/memory4
  106. (0x100000000 / 1Gib = 4)
  107. This device covers address range [0x100000000 ... 0x140000000)
  108. Under each section, you can see 4 files.
  109. /sys/devices/system/memory/memoryXXX/phys_index
  110. /sys/devices/system/memory/memoryXXX/phys_device
  111. /sys/devices/system/memory/memoryXXX/state
  112. /sys/devices/system/memory/memoryXXX/removable
  113. 'phys_index' : read-only and contains section id, same as XXX.
  114. 'state' : read-write
  115. at read: contains online/offline state of memory.
  116. at write: user can specify "online", "offline" command
  117. 'phys_device': read-only: designed to show the name of physical memory device.
  118. This is not well implemented now.
  119. 'removable' : read-only: contains an integer value indicating
  120. whether the memory section is removable or not
  121. removable. A value of 1 indicates that the memory
  122. section is removable and a value of 0 indicates that
  123. it is not removable.
  124. NOTE:
  125. These directories/files appear after physical memory hotplug phase.
  126. If CONFIG_NUMA is enabled the
  127. /sys/devices/system/memory/memoryXXX memory section
  128. directories can also be accessed via symbolic links located in
  129. the /sys/devices/system/node/node* directories. For example:
  130. /sys/devices/system/node/node0/memory9 -> ../../memory/memory9
  131. --------------------------------
  132. 4. Physical memory hot-add phase
  133. --------------------------------
  134. 4.1 Hardware(Firmware) Support
  135. ------------
  136. On x86_64/ia64 platform, memory hotplug by ACPI is supported.
  137. In general, the firmware (ACPI) which supports memory hotplug defines
  138. memory class object of _HID "PNP0C80". When a notify is asserted to PNP0C80,
  139. Linux's ACPI handler does hot-add memory to the system and calls a hotplug udev
  140. script. This will be done automatically.
  141. But scripts for memory hotplug are not contained in generic udev package(now).
  142. You may have to write it by yourself or online/offline memory by hand.
  143. Please see "How to online memory", "How to offline memory" in this text.
  144. If firmware supports NUMA-node hotplug, and defines an object _HID "ACPI0004",
  145. "PNP0A05", or "PNP0A06", notification is asserted to it, and ACPI handler
  146. calls hotplug code for all of objects which are defined in it.
  147. If memory device is found, memory hotplug code will be called.
  148. 4.2 Notify memory hot-add event by hand
  149. ------------
  150. In some environments, especially virtualized environment, firmware will not
  151. notify memory hotplug event to the kernel. For such environment, "probe"
  152. interface is supported. This interface depends on CONFIG_ARCH_MEMORY_PROBE.
  153. Now, CONFIG_ARCH_MEMORY_PROBE is supported only by powerpc but it does not
  154. contain highly architecture codes. Please add config if you need "probe"
  155. interface.
  156. Probe interface is located at
  157. /sys/devices/system/memory/probe
  158. You can tell the physical address of new memory to the kernel by
  159. % echo start_address_of_new_memory > /sys/devices/system/memory/probe
  160. Then, [start_address_of_new_memory, start_address_of_new_memory + section_size)
  161. memory range is hot-added. In this case, hotplug script is not called (in
  162. current implementation). You'll have to online memory by yourself.
  163. Please see "How to online memory" in this text.
  164. ------------------------------
  165. 5. Logical Memory hot-add phase
  166. ------------------------------
  167. 5.1. State of memory
  168. ------------
  169. To see (online/offline) state of memory section, read 'state' file.
  170. % cat /sys/device/system/memory/memoryXXX/state
  171. If the memory section is online, you'll read "online".
  172. If the memory section is offline, you'll read "offline".
  173. 5.2. How to online memory
  174. ------------
  175. Even if the memory is hot-added, it is not at ready-to-use state.
  176. For using newly added memory, you have to "online" the memory section.
  177. For onlining, you have to write "online" to the section's state file as:
  178. % echo online > /sys/devices/system/memory/memoryXXX/state
  179. After this, section memoryXXX's state will be 'online' and the amount of
  180. available memory will be increased.
  181. Currently, newly added memory is added as ZONE_NORMAL (for powerpc, ZONE_DMA).
  182. This may be changed in future.
  183. ------------------------
  184. 6. Logical memory remove
  185. ------------------------
  186. 6.1 Memory offline and ZONE_MOVABLE
  187. ------------
  188. Memory offlining is more complicated than memory online. Because memory offline
  189. has to make the whole memory section be unused, memory offline can fail if
  190. the section includes memory which cannot be freed.
  191. In general, memory offline can use 2 techniques.
  192. (1) reclaim and free all memory in the section.
  193. (2) migrate all pages in the section.
  194. In the current implementation, Linux's memory offline uses method (2), freeing
  195. all pages in the section by page migration. But not all pages are
  196. migratable. Under current Linux, migratable pages are anonymous pages and
  197. page caches. For offlining a section by migration, the kernel has to guarantee
  198. that the section contains only migratable pages.
  199. Now, a boot option for making a section which consists of migratable pages is
  200. supported. By specifying "kernelcore=" or "movablecore=" boot option, you can
  201. create ZONE_MOVABLE...a zone which is just used for movable pages.
  202. (See also Documentation/kernel-parameters.txt)
  203. Assume the system has "TOTAL" amount of memory at boot time, this boot option
  204. creates ZONE_MOVABLE as following.
  205. 1) When kernelcore=YYYY boot option is used,
  206. Size of memory not for movable pages (not for offline) is YYYY.
  207. Size of memory for movable pages (for offline) is TOTAL-YYYY.
  208. 2) When movablecore=ZZZZ boot option is used,
  209. Size of memory not for movable pages (not for offline) is TOTAL - ZZZZ.
  210. Size of memory for movable pages (for offline) is ZZZZ.
  211. Note) Unfortunately, there is no information to show which section belongs
  212. to ZONE_MOVABLE. This is TBD.
  213. 6.2. How to offline memory
  214. ------------
  215. You can offline a section by using the same sysfs interface that was used in
  216. memory onlining.
  217. % echo offline > /sys/devices/system/memory/memoryXXX/state
  218. If offline succeeds, the state of the memory section is changed to be "offline".
  219. If it fails, some error core (like -EBUSY) will be returned by the kernel.
  220. Even if a section does not belong to ZONE_MOVABLE, you can try to offline it.
  221. If it doesn't contain 'unmovable' memory, you'll get success.
  222. A section under ZONE_MOVABLE is considered to be able to be offlined easily.
  223. But under some busy state, it may return -EBUSY. Even if a memory section
  224. cannot be offlined due to -EBUSY, you can retry offlining it and may be able to
  225. offline it (or not).
  226. (For example, a page is referred to by some kernel internal call and released
  227. soon.)
  228. Consideration:
  229. Memory hotplug's design direction is to make the possibility of memory offlining
  230. higher and to guarantee unplugging memory under any situation. But it needs
  231. more work. Returning -EBUSY under some situation may be good because the user
  232. can decide to retry more or not by himself. Currently, memory offlining code
  233. does some amount of retry with 120 seconds timeout.
  234. -------------------------
  235. 7. Physical memory remove
  236. -------------------------
  237. Need more implementation yet....
  238. - Notification completion of remove works by OS to firmware.
  239. - Guard from remove if not yet.
  240. --------------------------------
  241. 8. Memory hotplug event notifier
  242. --------------------------------
  243. Memory hotplug has event notifer. There are 6 types of notification.
  244. MEMORY_GOING_ONLINE
  245. Generated before new memory becomes available in order to be able to
  246. prepare subsystems to handle memory. The page allocator is still unable
  247. to allocate from the new memory.
  248. MEMORY_CANCEL_ONLINE
  249. Generated if MEMORY_GOING_ONLINE fails.
  250. MEMORY_ONLINE
  251. Generated when memory has successfully brought online. The callback may
  252. allocate pages from the new memory.
  253. MEMORY_GOING_OFFLINE
  254. Generated to begin the process of offlining memory. Allocations are no
  255. longer possible from the memory but some of the memory to be offlined
  256. is still in use. The callback can be used to free memory known to a
  257. subsystem from the indicated memory section.
  258. MEMORY_CANCEL_OFFLINE
  259. Generated if MEMORY_GOING_OFFLINE fails. Memory is available again from
  260. the section that we attempted to offline.
  261. MEMORY_OFFLINE
  262. Generated after offlining memory is complete.
  263. A callback routine can be registered by
  264. hotplug_memory_notifier(callback_func, priority)
  265. The second argument of callback function (action) is event types of above.
  266. The third argument is passed by pointer of struct memory_notify.
  267. struct memory_notify {
  268. unsigned long start_pfn;
  269. unsigned long nr_pages;
  270. int status_change_nid;
  271. }
  272. start_pfn is start_pfn of online/offline memory.
  273. nr_pages is # of pages of online/offline memory.
  274. status_change_nid is set node id when N_HIGH_MEMORY of nodemask is (will be)
  275. set/clear. It means a new(memoryless) node gets new memory by online and a
  276. node loses all memory. If this is -1, then nodemask status is not changed.
  277. If status_changed_nid >= 0, callback should create/discard structures for the
  278. node if necessary.
  279. --------------
  280. 9. Future Work
  281. --------------
  282. - allowing memory hot-add to ZONE_MOVABLE. maybe we need some switch like
  283. sysctl or new control file.
  284. - showing memory section and physical device relationship.
  285. - showing memory section is under ZONE_MOVABLE or not
  286. - test and make it better memory offlining.
  287. - support HugeTLB page migration and offlining.
  288. - memmap removing at memory offline.
  289. - physical remove memory.