memory-hotplug.txt 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 in sysfs. Each section is part of
  101. a memory block under /sys/devices/system/memory as
  102. /sys/devices/system/memory/memoryXXX
  103. (XXX is the section id.)
  104. Now, XXX is defined as (start_address_of_section / section_size) of the first
  105. section contained in the memory block. The files 'phys_index' and
  106. 'end_phys_index' under each directory report the beginning and end section id's
  107. for the memory block covered by the sysfs directory. It is expected that all
  108. memory sections in this range are present and no memory holes exist in the
  109. range. Currently there is no way to determine if there is a memory hole, but
  110. the existence of one should not affect the hotplug capabilities of the memory
  111. block.
  112. For example, assume 1GiB section size. A device for a memory starting at
  113. 0x100000000 is /sys/device/system/memory/memory4
  114. (0x100000000 / 1Gib = 4)
  115. This device covers address range [0x100000000 ... 0x140000000)
  116. Under each section, you can see 4 or 5 files, the end_phys_index file being
  117. a recent addition and not present on older kernels.
  118. /sys/devices/system/memory/memoryXXX/start_phys_index
  119. /sys/devices/system/memory/memoryXXX/end_phys_index
  120. /sys/devices/system/memory/memoryXXX/phys_device
  121. /sys/devices/system/memory/memoryXXX/state
  122. /sys/devices/system/memory/memoryXXX/removable
  123. 'phys_index' : read-only and contains section id of the first section
  124. in the memory block, same as XXX.
  125. 'end_phys_index' : read-only and contains section id of the last section
  126. in the memory block.
  127. 'state' : read-write
  128. at read: contains online/offline state of memory.
  129. at write: user can specify "online", "offline" command
  130. which will be performed on al sections in the block.
  131. 'phys_device' : read-only: designed to show the name of physical memory
  132. device. This is not well implemented now.
  133. 'removable' : read-only: contains an integer value indicating
  134. whether the memory block is removable or not
  135. removable. A value of 1 indicates that the memory
  136. block is removable and a value of 0 indicates that
  137. it is not removable. A memory block is removable only if
  138. every section in the block is removable.
  139. NOTE:
  140. These directories/files appear after physical memory hotplug phase.
  141. If CONFIG_NUMA is enabled the memoryXXX/ directories can also be accessed
  142. via symbolic links located in the /sys/devices/system/node/node* directories.
  143. For example:
  144. /sys/devices/system/node/node0/memory9 -> ../../memory/memory9
  145. A backlink will also be created:
  146. /sys/devices/system/memory/memory9/node0 -> ../../node/node0
  147. --------------------------------
  148. 4. Physical memory hot-add phase
  149. --------------------------------
  150. 4.1 Hardware(Firmware) Support
  151. ------------
  152. On x86_64/ia64 platform, memory hotplug by ACPI is supported.
  153. In general, the firmware (ACPI) which supports memory hotplug defines
  154. memory class object of _HID "PNP0C80". When a notify is asserted to PNP0C80,
  155. Linux's ACPI handler does hot-add memory to the system and calls a hotplug udev
  156. script. This will be done automatically.
  157. But scripts for memory hotplug are not contained in generic udev package(now).
  158. You may have to write it by yourself or online/offline memory by hand.
  159. Please see "How to online memory", "How to offline memory" in this text.
  160. If firmware supports NUMA-node hotplug, and defines an object _HID "ACPI0004",
  161. "PNP0A05", or "PNP0A06", notification is asserted to it, and ACPI handler
  162. calls hotplug code for all of objects which are defined in it.
  163. If memory device is found, memory hotplug code will be called.
  164. 4.2 Notify memory hot-add event by hand
  165. ------------
  166. In some environments, especially virtualized environment, firmware will not
  167. notify memory hotplug event to the kernel. For such environment, "probe"
  168. interface is supported. This interface depends on CONFIG_ARCH_MEMORY_PROBE.
  169. Now, CONFIG_ARCH_MEMORY_PROBE is supported only by powerpc but it does not
  170. contain highly architecture codes. Please add config if you need "probe"
  171. interface.
  172. Probe interface is located at
  173. /sys/devices/system/memory/probe
  174. You can tell the physical address of new memory to the kernel by
  175. % echo start_address_of_new_memory > /sys/devices/system/memory/probe
  176. Then, [start_address_of_new_memory, start_address_of_new_memory + section_size)
  177. memory range is hot-added. In this case, hotplug script is not called (in
  178. current implementation). You'll have to online memory by yourself.
  179. Please see "How to online memory" in this text.
  180. ------------------------------
  181. 5. Logical Memory hot-add phase
  182. ------------------------------
  183. 5.1. State of memory
  184. ------------
  185. To see (online/offline) state of memory section, read 'state' file.
  186. % cat /sys/device/system/memory/memoryXXX/state
  187. If the memory section is online, you'll read "online".
  188. If the memory section is offline, you'll read "offline".
  189. 5.2. How to online memory
  190. ------------
  191. Even if the memory is hot-added, it is not at ready-to-use state.
  192. For using newly added memory, you have to "online" the memory section.
  193. For onlining, you have to write "online" to the section's state file as:
  194. % echo online > /sys/devices/system/memory/memoryXXX/state
  195. After this, section memoryXXX's state will be 'online' and the amount of
  196. available memory will be increased.
  197. Currently, newly added memory is added as ZONE_NORMAL (for powerpc, ZONE_DMA).
  198. This may be changed in future.
  199. ------------------------
  200. 6. Logical memory remove
  201. ------------------------
  202. 6.1 Memory offline and ZONE_MOVABLE
  203. ------------
  204. Memory offlining is more complicated than memory online. Because memory offline
  205. has to make the whole memory section be unused, memory offline can fail if
  206. the section includes memory which cannot be freed.
  207. In general, memory offline can use 2 techniques.
  208. (1) reclaim and free all memory in the section.
  209. (2) migrate all pages in the section.
  210. In the current implementation, Linux's memory offline uses method (2), freeing
  211. all pages in the section by page migration. But not all pages are
  212. migratable. Under current Linux, migratable pages are anonymous pages and
  213. page caches. For offlining a section by migration, the kernel has to guarantee
  214. that the section contains only migratable pages.
  215. Now, a boot option for making a section which consists of migratable pages is
  216. supported. By specifying "kernelcore=" or "movablecore=" boot option, you can
  217. create ZONE_MOVABLE...a zone which is just used for movable pages.
  218. (See also Documentation/kernel-parameters.txt)
  219. Assume the system has "TOTAL" amount of memory at boot time, this boot option
  220. creates ZONE_MOVABLE as following.
  221. 1) When kernelcore=YYYY boot option is used,
  222. Size of memory not for movable pages (not for offline) is YYYY.
  223. Size of memory for movable pages (for offline) is TOTAL-YYYY.
  224. 2) When movablecore=ZZZZ boot option is used,
  225. Size of memory not for movable pages (not for offline) is TOTAL - ZZZZ.
  226. Size of memory for movable pages (for offline) is ZZZZ.
  227. Note) Unfortunately, there is no information to show which section belongs
  228. to ZONE_MOVABLE. This is TBD.
  229. 6.2. How to offline memory
  230. ------------
  231. You can offline a section by using the same sysfs interface that was used in
  232. memory onlining.
  233. % echo offline > /sys/devices/system/memory/memoryXXX/state
  234. If offline succeeds, the state of the memory section is changed to be "offline".
  235. If it fails, some error core (like -EBUSY) will be returned by the kernel.
  236. Even if a section does not belong to ZONE_MOVABLE, you can try to offline it.
  237. If it doesn't contain 'unmovable' memory, you'll get success.
  238. A section under ZONE_MOVABLE is considered to be able to be offlined easily.
  239. But under some busy state, it may return -EBUSY. Even if a memory section
  240. cannot be offlined due to -EBUSY, you can retry offlining it and may be able to
  241. offline it (or not).
  242. (For example, a page is referred to by some kernel internal call and released
  243. soon.)
  244. Consideration:
  245. Memory hotplug's design direction is to make the possibility of memory offlining
  246. higher and to guarantee unplugging memory under any situation. But it needs
  247. more work. Returning -EBUSY under some situation may be good because the user
  248. can decide to retry more or not by himself. Currently, memory offlining code
  249. does some amount of retry with 120 seconds timeout.
  250. -------------------------
  251. 7. Physical memory remove
  252. -------------------------
  253. Need more implementation yet....
  254. - Notification completion of remove works by OS to firmware.
  255. - Guard from remove if not yet.
  256. --------------------------------
  257. 8. Memory hotplug event notifier
  258. --------------------------------
  259. Memory hotplug has event notifer. There are 6 types of notification.
  260. MEMORY_GOING_ONLINE
  261. Generated before new memory becomes available in order to be able to
  262. prepare subsystems to handle memory. The page allocator is still unable
  263. to allocate from the new memory.
  264. MEMORY_CANCEL_ONLINE
  265. Generated if MEMORY_GOING_ONLINE fails.
  266. MEMORY_ONLINE
  267. Generated when memory has successfully brought online. The callback may
  268. allocate pages from the new memory.
  269. MEMORY_GOING_OFFLINE
  270. Generated to begin the process of offlining memory. Allocations are no
  271. longer possible from the memory but some of the memory to be offlined
  272. is still in use. The callback can be used to free memory known to a
  273. subsystem from the indicated memory section.
  274. MEMORY_CANCEL_OFFLINE
  275. Generated if MEMORY_GOING_OFFLINE fails. Memory is available again from
  276. the section that we attempted to offline.
  277. MEMORY_OFFLINE
  278. Generated after offlining memory is complete.
  279. A callback routine can be registered by
  280. hotplug_memory_notifier(callback_func, priority)
  281. The second argument of callback function (action) is event types of above.
  282. The third argument is passed by pointer of struct memory_notify.
  283. struct memory_notify {
  284. unsigned long start_pfn;
  285. unsigned long nr_pages;
  286. int status_change_nid;
  287. }
  288. start_pfn is start_pfn of online/offline memory.
  289. nr_pages is # of pages of online/offline memory.
  290. status_change_nid is set node id when N_HIGH_MEMORY of nodemask is (will be)
  291. set/clear. It means a new(memoryless) node gets new memory by online and a
  292. node loses all memory. If this is -1, then nodemask status is not changed.
  293. If status_changed_nid >= 0, callback should create/discard structures for the
  294. node if necessary.
  295. --------------
  296. 9. Future Work
  297. --------------
  298. - allowing memory hot-add to ZONE_MOVABLE. maybe we need some switch like
  299. sysctl or new control file.
  300. - showing memory section and physical device relationship.
  301. - showing memory section is under ZONE_MOVABLE or not
  302. - test and make it better memory offlining.
  303. - support HugeTLB page migration and offlining.
  304. - memmap removing at memory offline.
  305. - physical remove memory.