memory-hotplug.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. .. _admin_guide_memory_hotplug:
  2. ==============
  3. Memory Hotplug
  4. ==============
  5. :Created: Jul 28 2007
  6. :Updated: Add description of notifier of memory hotplug: Oct 11 2007
  7. This document is about memory hotplug including how-to-use and current status.
  8. Because Memory Hotplug is still under development, contents of this text will
  9. be changed often.
  10. .. contents:: :local:
  11. .. note::
  12. (1) x86_64's has special implementation for memory hotplug.
  13. This text does not describe it.
  14. (2) This text assumes that sysfs is mounted at ``/sys``.
  15. Introduction
  16. ============
  17. Purpose of memory hotplug
  18. -------------------------
  19. Memory Hotplug allows users to increase/decrease the amount of memory.
  20. Generally, there are two purposes.
  21. (A) For changing the amount of memory.
  22. This is to allow a feature like capacity on demand.
  23. (B) For installing/removing DIMMs or NUMA-nodes physically.
  24. This is to exchange DIMMs/NUMA-nodes, reduce power consumption, etc.
  25. (A) is required by highly virtualized environments and (B) is required by
  26. hardware which supports memory power management.
  27. Linux memory hotplug is designed for both purpose.
  28. Phases of memory hotplug
  29. ------------------------
  30. There are 2 phases in Memory Hotplug:
  31. 1) Physical Memory Hotplug phase
  32. 2) Logical Memory Hotplug phase.
  33. The First phase is to communicate hardware/firmware and make/erase
  34. environment for hotplugged memory. Basically, this phase is necessary
  35. for the purpose (B), but this is good phase for communication between
  36. highly virtualized environments too.
  37. When memory is hotplugged, the kernel recognizes new memory, makes new memory
  38. management tables, and makes sysfs files for new memory's operation.
  39. If firmware supports notification of connection of new memory to OS,
  40. this phase is triggered automatically. ACPI can notify this event. If not,
  41. "probe" operation by system administration is used instead.
  42. (see :ref:`memory_hotplug_physical_mem`).
  43. Logical Memory Hotplug phase is to change memory state into
  44. available/unavailable for users. Amount of memory from user's view is
  45. changed by this phase. The kernel makes all memory in it as free pages
  46. when a memory range is available.
  47. In this document, this phase is described as online/offline.
  48. Logical Memory Hotplug phase is triggered by write of sysfs file by system
  49. administrator. For the hot-add case, it must be executed after Physical Hotplug
  50. phase by hand.
  51. (However, if you writes udev's hotplug scripts for memory hotplug, these
  52. phases can be execute in seamless way.)
  53. Unit of Memory online/offline operation
  54. ---------------------------------------
  55. Memory hotplug uses SPARSEMEM memory model which allows memory to be divided
  56. into chunks of the same size. These chunks are called "sections". The size of
  57. a memory section is architecture dependent. For example, power uses 16MiB, ia64
  58. uses 1GiB.
  59. Memory sections are combined into chunks referred to as "memory blocks". The
  60. size of a memory block is architecture dependent and represents the logical
  61. unit upon which memory online/offline operations are to be performed. The
  62. default size of a memory block is the same as memory section size unless an
  63. architecture specifies otherwise. (see :ref:`memory_hotplug_sysfs_files`.)
  64. To determine the size (in bytes) of a memory block please read this file::
  65. /sys/devices/system/memory/block_size_bytes
  66. Kernel Configuration
  67. ====================
  68. To use memory hotplug feature, kernel must be compiled with following
  69. config options.
  70. - For all memory hotplug:
  71. - Memory model -> Sparse Memory (``CONFIG_SPARSEMEM``)
  72. - Allow for memory hot-add (``CONFIG_MEMORY_HOTPLUG``)
  73. - To enable memory removal, the following are also necessary:
  74. - Allow for memory hot remove (``CONFIG_MEMORY_HOTREMOVE``)
  75. - Page Migration (``CONFIG_MIGRATION``)
  76. - For ACPI memory hotplug, the following are also necessary:
  77. - Memory hotplug (under ACPI Support menu) (``CONFIG_ACPI_HOTPLUG_MEMORY``)
  78. - This option can be kernel module.
  79. - As a related configuration, if your box has a feature of NUMA-node hotplug
  80. via ACPI, then this option is necessary too.
  81. - ACPI0004,PNP0A05 and PNP0A06 Container Driver (under ACPI Support menu)
  82. (``CONFIG_ACPI_CONTAINER``).
  83. This option can be kernel module too.
  84. .. _memory_hotplug_sysfs_files:
  85. sysfs files for memory hotplug
  86. ==============================
  87. All memory blocks have their device information in sysfs. Each memory block
  88. is described under ``/sys/devices/system/memory`` as::
  89. /sys/devices/system/memory/memoryXXX
  90. where XXX is the memory block id.
  91. For the memory block covered by the sysfs directory. It is expected that all
  92. memory sections in this range are present and no memory holes exist in the
  93. range. Currently there is no way to determine if there is a memory hole, but
  94. the existence of one should not affect the hotplug capabilities of the memory
  95. block.
  96. For example, assume 1GiB memory block size. A device for a memory starting at
  97. 0x100000000 is ``/sys/device/system/memory/memory4``::
  98. (0x100000000 / 1Gib = 4)
  99. This device covers address range [0x100000000 ... 0x140000000)
  100. Under each memory block, you can see 5 files:
  101. - ``/sys/devices/system/memory/memoryXXX/phys_index``
  102. - ``/sys/devices/system/memory/memoryXXX/phys_device``
  103. - ``/sys/devices/system/memory/memoryXXX/state``
  104. - ``/sys/devices/system/memory/memoryXXX/removable``
  105. - ``/sys/devices/system/memory/memoryXXX/valid_zones``
  106. =================== ============================================================
  107. ``phys_index`` read-only and contains memory block id, same as XXX.
  108. ``state`` read-write
  109. - at read: contains online/offline state of memory.
  110. - at write: user can specify "online_kernel",
  111. "online_movable", "online", "offline" command
  112. which will be performed on all sections in the block.
  113. ``phys_device`` read-only: designed to show the name of physical memory
  114. device. This is not well implemented now.
  115. ``removable`` read-only: contains an integer value indicating
  116. whether the memory block is removable or not
  117. removable. A value of 1 indicates that the memory
  118. block is removable and a value of 0 indicates that
  119. it is not removable. A memory block is removable only if
  120. every section in the block is removable.
  121. ``valid_zones`` read-only: designed to show which zones this memory block
  122. can be onlined to.
  123. The first column shows it`s default zone.
  124. "memory6/valid_zones: Normal Movable" shows this memoryblock
  125. can be onlined to ZONE_NORMAL by default and to ZONE_MOVABLE
  126. by online_movable.
  127. "memory7/valid_zones: Movable Normal" shows this memoryblock
  128. can be onlined to ZONE_MOVABLE by default and to ZONE_NORMAL
  129. by online_kernel.
  130. =================== ============================================================
  131. .. note::
  132. These directories/files appear after physical memory hotplug phase.
  133. If CONFIG_NUMA is enabled the memoryXXX/ directories can also be accessed
  134. via symbolic links located in the ``/sys/devices/system/node/node*`` directories.
  135. For example::
  136. /sys/devices/system/node/node0/memory9 -> ../../memory/memory9
  137. A backlink will also be created::
  138. /sys/devices/system/memory/memory9/node0 -> ../../node/node0
  139. .. _memory_hotplug_physical_mem:
  140. Physical memory hot-add phase
  141. =============================
  142. Hardware(Firmware) Support
  143. --------------------------
  144. On x86_64/ia64 platform, memory hotplug by ACPI is supported.
  145. In general, the firmware (ACPI) which supports memory hotplug defines
  146. memory class object of _HID "PNP0C80". When a notify is asserted to PNP0C80,
  147. Linux's ACPI handler does hot-add memory to the system and calls a hotplug udev
  148. script. This will be done automatically.
  149. But scripts for memory hotplug are not contained in generic udev package(now).
  150. You may have to write it by yourself or online/offline memory by hand.
  151. Please see :ref:`memory_hotplug_how_to_online_memory` and
  152. :ref:`memory_hotplug_how_to_offline_memory`.
  153. If firmware supports NUMA-node hotplug, and defines an object _HID "ACPI0004",
  154. "PNP0A05", or "PNP0A06", notification is asserted to it, and ACPI handler
  155. calls hotplug code for all of objects which are defined in it.
  156. If memory device is found, memory hotplug code will be called.
  157. Notify memory hot-add event by hand
  158. -----------------------------------
  159. On some architectures, the firmware may not notify the kernel of a memory
  160. hotplug event. Therefore, the memory "probe" interface is supported to
  161. explicitly notify the kernel. This interface depends on
  162. CONFIG_ARCH_MEMORY_PROBE and can be configured on powerpc, sh, and x86
  163. if hotplug is supported, although for x86 this should be handled by ACPI
  164. notification.
  165. Probe interface is located at::
  166. /sys/devices/system/memory/probe
  167. You can tell the physical address of new memory to the kernel by::
  168. % echo start_address_of_new_memory > /sys/devices/system/memory/probe
  169. Then, [start_address_of_new_memory, start_address_of_new_memory +
  170. memory_block_size] memory range is hot-added. In this case, hotplug script is
  171. not called (in current implementation). You'll have to online memory by
  172. yourself. Please see :ref:`memory_hotplug_how_to_online_memory`.
  173. Logical Memory hot-add phase
  174. ============================
  175. State of memory
  176. ---------------
  177. To see (online/offline) state of a memory block, read 'state' file::
  178. % cat /sys/device/system/memory/memoryXXX/state
  179. - If the memory block is online, you'll read "online".
  180. - If the memory block is offline, you'll read "offline".
  181. .. _memory_hotplug_how_to_online_memory:
  182. How to online memory
  183. --------------------
  184. When the memory is hot-added, the kernel decides whether or not to "online"
  185. it according to the policy which can be read from "auto_online_blocks" file::
  186. % cat /sys/devices/system/memory/auto_online_blocks
  187. The default depends on the CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE kernel config
  188. option. If it is disabled the default is "offline" which means the newly added
  189. memory is not in a ready-to-use state and you have to "online" the newly added
  190. memory blocks manually. Automatic onlining can be requested by writing "online"
  191. to "auto_online_blocks" file::
  192. % echo online > /sys/devices/system/memory/auto_online_blocks
  193. This sets a global policy and impacts all memory blocks that will subsequently
  194. be hotplugged. Currently offline blocks keep their state. It is possible, under
  195. certain circumstances, that some memory blocks will be added but will fail to
  196. online. User space tools can check their "state" files
  197. (``/sys/devices/system/memory/memoryXXX/state``) and try to online them manually.
  198. If the automatic onlining wasn't requested, failed, or some memory block was
  199. offlined it is possible to change the individual block's state by writing to the
  200. "state" file::
  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 doesn't belong to any zone an appropriate kernel zone
  204. (usually ZONE_NORMAL) will be used unless movable_node kernel command line
  205. option is specified when ZONE_MOVABLE will be used.
  206. You can explicitly request to associate it with ZONE_MOVABLE by::
  207. % echo online_movable > /sys/devices/system/memory/memoryXXX/state
  208. .. note:: current limit: this memory block must be adjacent to ZONE_MOVABLE
  209. Or you can explicitly request a kernel zone (usually ZONE_NORMAL) by::
  210. % echo online_kernel > /sys/devices/system/memory/memoryXXX/state
  211. .. note:: current limit: this memory block must be adjacent to ZONE_NORMAL
  212. An explicit zone onlining can fail (e.g. when the range is already within
  213. and existing and incompatible zone already).
  214. After this, memory block XXX's state will be 'online' and the amount of
  215. available memory will be increased.
  216. This may be changed in future.
  217. Logical memory remove
  218. =====================
  219. Memory offline and ZONE_MOVABLE
  220. -------------------------------
  221. Memory offlining is more complicated than memory online. Because memory offline
  222. has to make the whole memory block be unused, memory offline can fail if
  223. the memory block includes memory which cannot be freed.
  224. In general, memory offline can use 2 techniques.
  225. (1) reclaim and free all memory in the memory block.
  226. (2) migrate all pages in the memory block.
  227. In the current implementation, Linux's memory offline uses method (2), freeing
  228. all pages in the memory block by page migration. But not all pages are
  229. migratable. Under current Linux, migratable pages are anonymous pages and
  230. page caches. For offlining a memory block by migration, the kernel has to
  231. guarantee that the memory block contains only migratable pages.
  232. Now, a boot option for making a memory block which consists of migratable pages
  233. is supported. By specifying "kernelcore=" or "movablecore=" boot option, you can
  234. create ZONE_MOVABLE...a zone which is just used for movable pages.
  235. (See also Documentation/admin-guide/kernel-parameters.rst)
  236. Assume the system has "TOTAL" amount of memory at boot time, this boot option
  237. creates ZONE_MOVABLE as following.
  238. 1) When kernelcore=YYYY boot option is used,
  239. Size of memory not for movable pages (not for offline) is YYYY.
  240. Size of memory for movable pages (for offline) is TOTAL-YYYY.
  241. 2) When movablecore=ZZZZ boot option is used,
  242. Size of memory not for movable pages (not for offline) is TOTAL - ZZZZ.
  243. Size of memory for movable pages (for offline) is ZZZZ.
  244. .. note::
  245. Unfortunately, there is no information to show which memory block belongs
  246. to ZONE_MOVABLE. This is TBD.
  247. .. _memory_hotplug_how_to_offline_memory:
  248. How to offline memory
  249. ---------------------
  250. You can offline a memory block by using the same sysfs interface that was used
  251. in memory onlining::
  252. % echo offline > /sys/devices/system/memory/memoryXXX/state
  253. If offline succeeds, the state of the memory block is changed to be "offline".
  254. If it fails, some error core (like -EBUSY) will be returned by the kernel.
  255. Even if a memory block does not belong to ZONE_MOVABLE, you can try to offline
  256. it. If it doesn't contain 'unmovable' memory, you'll get success.
  257. A memory block under ZONE_MOVABLE is considered to be able to be offlined
  258. easily. But under some busy state, it may return -EBUSY. Even if a memory
  259. block cannot be offlined due to -EBUSY, you can retry offlining it and may be
  260. able to offline it (or not). (For example, a page is referred to by some kernel
  261. internal call and released soon.)
  262. Consideration:
  263. Memory hotplug's design direction is to make the possibility of memory
  264. offlining higher and to guarantee unplugging memory under any situation. But
  265. it needs more work. Returning -EBUSY under some situation may be good because
  266. the user can decide to retry more or not by himself. Currently, memory
  267. offlining code does some amount of retry with 120 seconds timeout.
  268. Physical memory remove
  269. ======================
  270. Need more implementation yet....
  271. - Notification completion of remove works by OS to firmware.
  272. - Guard from remove if not yet.
  273. Future Work
  274. ===========
  275. - allowing memory hot-add to ZONE_MOVABLE. maybe we need some switch like
  276. sysctl or new control file.
  277. - showing memory block and physical device relationship.
  278. - test and make it better memory offlining.
  279. - support HugeTLB page migration and offlining.
  280. - memmap removing at memory offline.
  281. - physical remove memory.