memory-hotplug.txt 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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 which allows memory to be divided
  72. into chunks of the same size. These chunks are called "sections". The size of
  73. a memory section is architecture dependent. For example, power uses 16MiB, ia64
  74. uses 1GiB.
  75. Memory sections are combined into chunks referred to as "memory blocks". The
  76. size of a memory block is architecture dependent and represents the logical
  77. unit upon which memory online/offline operations are to be performed. The
  78. default size of a memory block is the same as memory section size unless an
  79. architecture specifies otherwise. (see Section 3.)
  80. To determine the size (in bytes) of a memory block please read this file:
  81. /sys/devices/system/memory/block_size_bytes
  82. -----------------------
  83. 2. Kernel Configuration
  84. -----------------------
  85. To use memory hotplug feature, kernel must be compiled with following
  86. config options.
  87. - For all memory hotplug
  88. Memory model -> Sparse Memory (CONFIG_SPARSEMEM)
  89. Allow for memory hot-add (CONFIG_MEMORY_HOTPLUG)
  90. - To enable memory removal, the followings are also necessary
  91. Allow for memory hot remove (CONFIG_MEMORY_HOTREMOVE)
  92. Page Migration (CONFIG_MIGRATION)
  93. - For ACPI memory hotplug, the followings are also necessary
  94. Memory hotplug (under ACPI Support menu) (CONFIG_ACPI_HOTPLUG_MEMORY)
  95. This option can be kernel module.
  96. - As a related configuration, if your box has a feature of NUMA-node hotplug
  97. via ACPI, then this option is necessary too.
  98. ACPI0004,PNP0A05 and PNP0A06 Container Driver (under ACPI Support menu)
  99. (CONFIG_ACPI_CONTAINER).
  100. This option can be kernel module too.
  101. --------------------------------
  102. 3 sysfs files for memory hotplug
  103. --------------------------------
  104. All memory blocks have their device information in sysfs. Each memory block
  105. is described under /sys/devices/system/memory as
  106. /sys/devices/system/memory/memoryXXX
  107. (XXX is the memory block id.)
  108. For the memory block covered by the sysfs directory. It is expected that all
  109. memory sections in this range are present and no memory holes exist in the
  110. range. Currently there is no way to determine if there is a memory hole, but
  111. the existence of one should not affect the hotplug capabilities of the memory
  112. block.
  113. For example, assume 1GiB memory block size. A device for a memory starting at
  114. 0x100000000 is /sys/device/system/memory/memory4
  115. (0x100000000 / 1Gib = 4)
  116. This device covers address range [0x100000000 ... 0x140000000)
  117. Under each memory block, you can see 5 files:
  118. /sys/devices/system/memory/memoryXXX/phys_index
  119. /sys/devices/system/memory/memoryXXX/phys_device
  120. /sys/devices/system/memory/memoryXXX/state
  121. /sys/devices/system/memory/memoryXXX/removable
  122. /sys/devices/system/memory/memoryXXX/valid_zones
  123. 'phys_index' : read-only and contains memory block id, same as XXX.
  124. 'state' : read-write
  125. at read: contains online/offline state of memory.
  126. at write: user can specify "online_kernel",
  127. "online_movable", "online", "offline" command
  128. which will be performed on all sections in the block.
  129. 'phys_device' : read-only: designed to show the name of physical memory
  130. device. This is not well implemented now.
  131. 'removable' : read-only: contains an integer value indicating
  132. whether the memory block is removable or not
  133. removable. A value of 1 indicates that the memory
  134. block is removable and a value of 0 indicates that
  135. it is not removable. A memory block is removable only if
  136. every section in the block is removable.
  137. 'valid_zones' : read-only: designed to show which zones this memory block
  138. can be onlined to.
  139. The first column shows it's default zone.
  140. "memory6/valid_zones: Normal Movable" shows this memoryblock
  141. can be onlined to ZONE_NORMAL by default and to ZONE_MOVABLE
  142. by online_movable.
  143. "memory7/valid_zones: Movable Normal" shows this memoryblock
  144. can be onlined to ZONE_MOVABLE by default and to ZONE_NORMAL
  145. by online_kernel.
  146. NOTE:
  147. These directories/files appear after physical memory hotplug phase.
  148. If CONFIG_NUMA is enabled the memoryXXX/ directories can also be accessed
  149. via symbolic links located in the /sys/devices/system/node/node* directories.
  150. For example:
  151. /sys/devices/system/node/node0/memory9 -> ../../memory/memory9
  152. A backlink will also be created:
  153. /sys/devices/system/memory/memory9/node0 -> ../../node/node0
  154. --------------------------------
  155. 4. Physical memory hot-add phase
  156. --------------------------------
  157. 4.1 Hardware(Firmware) Support
  158. ------------
  159. On x86_64/ia64 platform, memory hotplug by ACPI is supported.
  160. In general, the firmware (ACPI) which supports memory hotplug defines
  161. memory class object of _HID "PNP0C80". When a notify is asserted to PNP0C80,
  162. Linux's ACPI handler does hot-add memory to the system and calls a hotplug udev
  163. script. This will be done automatically.
  164. But scripts for memory hotplug are not contained in generic udev package(now).
  165. You may have to write it by yourself or online/offline memory by hand.
  166. Please see "How to online memory", "How to offline memory" in this text.
  167. If firmware supports NUMA-node hotplug, and defines an object _HID "ACPI0004",
  168. "PNP0A05", or "PNP0A06", notification is asserted to it, and ACPI handler
  169. calls hotplug code for all of objects which are defined in it.
  170. If memory device is found, memory hotplug code will be called.
  171. 4.2 Notify memory hot-add event by hand
  172. ------------
  173. On some architectures, the firmware may not notify the kernel of a memory
  174. hotplug event. Therefore, the memory "probe" interface is supported to
  175. explicitly notify the kernel. This interface depends on
  176. CONFIG_ARCH_MEMORY_PROBE and can be configured on powerpc, sh, and x86
  177. if hotplug is supported, although for x86 this should be handled by ACPI
  178. notification.
  179. Probe interface is located at
  180. /sys/devices/system/memory/probe
  181. You can tell the physical address of new memory to the kernel by
  182. % echo start_address_of_new_memory > /sys/devices/system/memory/probe
  183. Then, [start_address_of_new_memory, start_address_of_new_memory +
  184. memory_block_size] memory range is hot-added. In this case, hotplug script is
  185. not called (in current implementation). You'll have to online memory by
  186. yourself. Please see "How to online memory" in this text.
  187. ------------------------------
  188. 5. Logical Memory hot-add phase
  189. ------------------------------
  190. 5.1. State of memory
  191. ------------
  192. To see (online/offline) state of a memory block, read 'state' file.
  193. % cat /sys/device/system/memory/memoryXXX/state
  194. If the memory block is online, you'll read "online".
  195. If the memory block is offline, you'll read "offline".
  196. 5.2. How to online memory
  197. ------------
  198. Even if the memory is hot-added, it is not at ready-to-use state.
  199. For using newly added memory, you have to "online" the memory block.
  200. For onlining, you have to write "online" to the memory block's state file as:
  201. % echo online > /sys/devices/system/memory/memoryXXX/state
  202. This onlining will not change the ZONE type of the target memory block,
  203. If the memory block is in ZONE_NORMAL, you can change it to ZONE_MOVABLE:
  204. % echo online_movable > /sys/devices/system/memory/memoryXXX/state
  205. (NOTE: current limit: this memory block must be adjacent to ZONE_MOVABLE)
  206. And if the memory block is in ZONE_MOVABLE, you can change it to ZONE_NORMAL:
  207. % echo online_kernel > /sys/devices/system/memory/memoryXXX/state
  208. (NOTE: current limit: this memory block must be adjacent to ZONE_NORMAL)
  209. After this, memory block XXX's state will be 'online' and the amount of
  210. available memory will be increased.
  211. Currently, newly added memory is added as ZONE_NORMAL (for powerpc, ZONE_DMA).
  212. This may be changed in future.
  213. ------------------------
  214. 6. Logical memory remove
  215. ------------------------
  216. 6.1 Memory offline and ZONE_MOVABLE
  217. ------------
  218. Memory offlining is more complicated than memory online. Because memory offline
  219. has to make the whole memory block be unused, memory offline can fail if
  220. the memory block includes memory which cannot be freed.
  221. In general, memory offline can use 2 techniques.
  222. (1) reclaim and free all memory in the memory block.
  223. (2) migrate all pages in the memory block.
  224. In the current implementation, Linux's memory offline uses method (2), freeing
  225. all pages in the memory block by page migration. But not all pages are
  226. migratable. Under current Linux, migratable pages are anonymous pages and
  227. page caches. For offlining a memory block by migration, the kernel has to
  228. guarantee that the memory block contains only migratable pages.
  229. Now, a boot option for making a memory block which consists of migratable pages
  230. is supported. By specifying "kernelcore=" or "movablecore=" boot option, you can
  231. create ZONE_MOVABLE...a zone which is just used for movable pages.
  232. (See also Documentation/kernel-parameters.txt)
  233. Assume the system has "TOTAL" amount of memory at boot time, this boot option
  234. creates ZONE_MOVABLE as following.
  235. 1) When kernelcore=YYYY boot option is used,
  236. Size of memory not for movable pages (not for offline) is YYYY.
  237. Size of memory for movable pages (for offline) is TOTAL-YYYY.
  238. 2) When movablecore=ZZZZ boot option is used,
  239. Size of memory not for movable pages (not for offline) is TOTAL - ZZZZ.
  240. Size of memory for movable pages (for offline) is ZZZZ.
  241. Note: Unfortunately, there is no information to show which memory block belongs
  242. to ZONE_MOVABLE. This is TBD.
  243. 6.2. How to offline memory
  244. ------------
  245. You can offline a memory block by using the same sysfs interface that was used
  246. in memory onlining.
  247. % echo offline > /sys/devices/system/memory/memoryXXX/state
  248. If offline succeeds, the state of the memory block is changed to be "offline".
  249. If it fails, some error core (like -EBUSY) will be returned by the kernel.
  250. Even if a memory block does not belong to ZONE_MOVABLE, you can try to offline
  251. it. If it doesn't contain 'unmovable' memory, you'll get success.
  252. A memory block under ZONE_MOVABLE is considered to be able to be offlined
  253. easily. But under some busy state, it may return -EBUSY. Even if a memory
  254. block cannot be offlined due to -EBUSY, you can retry offlining it and may be
  255. able to offline it (or not). (For example, a page is referred to by some kernel
  256. internal call and released soon.)
  257. Consideration:
  258. Memory hotplug's design direction is to make the possibility of memory offlining
  259. higher and to guarantee unplugging memory under any situation. But it needs
  260. more work. Returning -EBUSY under some situation may be good because the user
  261. can decide to retry more or not by himself. Currently, memory offlining code
  262. does some amount of retry with 120 seconds timeout.
  263. -------------------------
  264. 7. Physical memory remove
  265. -------------------------
  266. Need more implementation yet....
  267. - Notification completion of remove works by OS to firmware.
  268. - Guard from remove if not yet.
  269. --------------------------------
  270. 8. Memory hotplug event notifier
  271. --------------------------------
  272. Hotplugging events are sent to a notification queue.
  273. There are six types of notification defined in include/linux/memory.h:
  274. MEM_GOING_ONLINE
  275. Generated before new memory becomes available in order to be able to
  276. prepare subsystems to handle memory. The page allocator is still unable
  277. to allocate from the new memory.
  278. MEM_CANCEL_ONLINE
  279. Generated if MEMORY_GOING_ONLINE fails.
  280. MEM_ONLINE
  281. Generated when memory has successfully brought online. The callback may
  282. allocate pages from the new memory.
  283. MEM_GOING_OFFLINE
  284. Generated to begin the process of offlining memory. Allocations are no
  285. longer possible from the memory but some of the memory to be offlined
  286. is still in use. The callback can be used to free memory known to a
  287. subsystem from the indicated memory block.
  288. MEM_CANCEL_OFFLINE
  289. Generated if MEMORY_GOING_OFFLINE fails. Memory is available again from
  290. the memory block that we attempted to offline.
  291. MEM_OFFLINE
  292. Generated after offlining memory is complete.
  293. A callback routine can be registered by calling
  294. hotplug_memory_notifier(callback_func, priority)
  295. Callback functions with higher values of priority are called before callback
  296. functions with lower values.
  297. A callback function must have the following prototype:
  298. int callback_func(
  299. struct notifier_block *self, unsigned long action, void *arg);
  300. The first argument of the callback function (self) is a pointer to the block
  301. of the notifier chain that points to the callback function itself.
  302. The second argument (action) is one of the event types described above.
  303. The third argument (arg) passes a pointer of struct memory_notify.
  304. struct memory_notify {
  305. unsigned long start_pfn;
  306. unsigned long nr_pages;
  307. int status_change_nid_normal;
  308. int status_change_nid_high;
  309. int status_change_nid;
  310. }
  311. start_pfn is start_pfn of online/offline memory.
  312. nr_pages is # of pages of online/offline memory.
  313. status_change_nid_normal is set node id when N_NORMAL_MEMORY of nodemask
  314. is (will be) set/clear, if this is -1, then nodemask status is not changed.
  315. status_change_nid_high is set node id when N_HIGH_MEMORY of nodemask
  316. is (will be) set/clear, if this is -1, then nodemask status is not changed.
  317. status_change_nid is set node id when N_MEMORY of nodemask is (will be)
  318. set/clear. It means a new(memoryless) node gets new memory by online and a
  319. node loses all memory. If this is -1, then nodemask status is not changed.
  320. If status_changed_nid* >= 0, callback should create/discard structures for the
  321. node if necessary.
  322. The callback routine shall return one of the values
  323. NOTIFY_DONE, NOTIFY_OK, NOTIFY_BAD, NOTIFY_STOP
  324. defined in include/linux/notifier.h
  325. NOTIFY_DONE and NOTIFY_OK have no effect on the further processing.
  326. NOTIFY_BAD is used as response to the MEM_GOING_ONLINE, MEM_GOING_OFFLINE,
  327. MEM_ONLINE, or MEM_OFFLINE action to cancel hotplugging. It stops
  328. further processing of the notification queue.
  329. NOTIFY_STOP stops further processing of the notification queue.
  330. --------------
  331. 9. Future Work
  332. --------------
  333. - allowing memory hot-add to ZONE_MOVABLE. maybe we need some switch like
  334. sysctl or new control file.
  335. - showing memory block and physical device relationship.
  336. - test and make it better memory offlining.
  337. - support HugeTLB page migration and offlining.
  338. - memmap removing at memory offline.
  339. - physical remove memory.