1*6bf53999SMike Rapoport.. _admin_guide_memory_hotplug: 2*6bf53999SMike Rapoport 3*6bf53999SMike Rapoport============== 4*6bf53999SMike RapoportMemory Hotplug 5*6bf53999SMike Rapoport============== 6*6bf53999SMike Rapoport 7*6bf53999SMike Rapoport:Created: Jul 28 2007 8*6bf53999SMike Rapoport:Updated: Add description of notifier of memory hotplug: Oct 11 2007 9*6bf53999SMike Rapoport 10*6bf53999SMike RapoportThis document is about memory hotplug including how-to-use and current status. 11*6bf53999SMike RapoportBecause Memory Hotplug is still under development, contents of this text will 12*6bf53999SMike Rapoportbe changed often. 13*6bf53999SMike Rapoport 14*6bf53999SMike Rapoport.. contents:: :local: 15*6bf53999SMike Rapoport 16*6bf53999SMike Rapoport.. CONTENTS 17*6bf53999SMike Rapoport 18*6bf53999SMike Rapoport 1. Introduction 19*6bf53999SMike Rapoport 1.1 Purpose of memory hotplug 20*6bf53999SMike Rapoport 1.2. Phases of memory hotplug 21*6bf53999SMike Rapoport 1.3. Unit of Memory online/offline operation 22*6bf53999SMike Rapoport 2. Kernel Configuration 23*6bf53999SMike Rapoport 3. sysfs files for memory hotplug 24*6bf53999SMike Rapoport 4. Physical memory hot-add phase 25*6bf53999SMike Rapoport 4.1 Hardware(Firmware) Support 26*6bf53999SMike Rapoport 4.2 Notify memory hot-add event by hand 27*6bf53999SMike Rapoport 5. Logical Memory hot-add phase 28*6bf53999SMike Rapoport 5.1. State of memory 29*6bf53999SMike Rapoport 5.2. How to online memory 30*6bf53999SMike Rapoport 6. Logical memory remove 31*6bf53999SMike Rapoport 6.1 Memory offline and ZONE_MOVABLE 32*6bf53999SMike Rapoport 6.2. How to offline memory 33*6bf53999SMike Rapoport 7. Physical memory remove 34*6bf53999SMike Rapoport 8. Memory hotplug event notifier 35*6bf53999SMike Rapoport 9. Future Work List 36*6bf53999SMike Rapoport 37*6bf53999SMike Rapoport 38*6bf53999SMike Rapoport.. note:: 39*6bf53999SMike Rapoport 40*6bf53999SMike Rapoport (1) x86_64's has special implementation for memory hotplug. 41*6bf53999SMike Rapoport This text does not describe it. 42*6bf53999SMike Rapoport (2) This text assumes that sysfs is mounted at ``/sys``. 43*6bf53999SMike Rapoport 44*6bf53999SMike Rapoport 45*6bf53999SMike RapoportIntroduction 46*6bf53999SMike Rapoport============ 47*6bf53999SMike Rapoport 48*6bf53999SMike RapoportPurpose of memory hotplug 49*6bf53999SMike Rapoport------------------------- 50*6bf53999SMike Rapoport 51*6bf53999SMike RapoportMemory Hotplug allows users to increase/decrease the amount of memory. 52*6bf53999SMike RapoportGenerally, there are two purposes. 53*6bf53999SMike Rapoport 54*6bf53999SMike Rapoport(A) For changing the amount of memory. 55*6bf53999SMike Rapoport This is to allow a feature like capacity on demand. 56*6bf53999SMike Rapoport(B) For installing/removing DIMMs or NUMA-nodes physically. 57*6bf53999SMike Rapoport This is to exchange DIMMs/NUMA-nodes, reduce power consumption, etc. 58*6bf53999SMike Rapoport 59*6bf53999SMike Rapoport(A) is required by highly virtualized environments and (B) is required by 60*6bf53999SMike Rapoporthardware which supports memory power management. 61*6bf53999SMike Rapoport 62*6bf53999SMike RapoportLinux memory hotplug is designed for both purpose. 63*6bf53999SMike Rapoport 64*6bf53999SMike RapoportPhases of memory hotplug 65*6bf53999SMike Rapoport------------------------ 66*6bf53999SMike Rapoport 67*6bf53999SMike RapoportThere are 2 phases in Memory Hotplug: 68*6bf53999SMike Rapoport 69*6bf53999SMike Rapoport 1) Physical Memory Hotplug phase 70*6bf53999SMike Rapoport 2) Logical Memory Hotplug phase. 71*6bf53999SMike Rapoport 72*6bf53999SMike RapoportThe First phase is to communicate hardware/firmware and make/erase 73*6bf53999SMike Rapoportenvironment for hotplugged memory. Basically, this phase is necessary 74*6bf53999SMike Rapoportfor the purpose (B), but this is good phase for communication between 75*6bf53999SMike Rapoporthighly virtualized environments too. 76*6bf53999SMike Rapoport 77*6bf53999SMike RapoportWhen memory is hotplugged, the kernel recognizes new memory, makes new memory 78*6bf53999SMike Rapoportmanagement tables, and makes sysfs files for new memory's operation. 79*6bf53999SMike Rapoport 80*6bf53999SMike RapoportIf firmware supports notification of connection of new memory to OS, 81*6bf53999SMike Rapoportthis phase is triggered automatically. ACPI can notify this event. If not, 82*6bf53999SMike Rapoport"probe" operation by system administration is used instead. 83*6bf53999SMike Rapoport(see :ref:`memory_hotplug_physical_mem`). 84*6bf53999SMike Rapoport 85*6bf53999SMike RapoportLogical Memory Hotplug phase is to change memory state into 86*6bf53999SMike Rapoportavailable/unavailable for users. Amount of memory from user's view is 87*6bf53999SMike Rapoportchanged by this phase. The kernel makes all memory in it as free pages 88*6bf53999SMike Rapoportwhen a memory range is available. 89*6bf53999SMike Rapoport 90*6bf53999SMike RapoportIn this document, this phase is described as online/offline. 91*6bf53999SMike Rapoport 92*6bf53999SMike RapoportLogical Memory Hotplug phase is triggered by write of sysfs file by system 93*6bf53999SMike Rapoportadministrator. For the hot-add case, it must be executed after Physical Hotplug 94*6bf53999SMike Rapoportphase by hand. 95*6bf53999SMike Rapoport(However, if you writes udev's hotplug scripts for memory hotplug, these 96*6bf53999SMike Rapoportphases can be execute in seamless way.) 97*6bf53999SMike Rapoport 98*6bf53999SMike RapoportUnit of Memory online/offline operation 99*6bf53999SMike Rapoport--------------------------------------- 100*6bf53999SMike Rapoport 101*6bf53999SMike RapoportMemory hotplug uses SPARSEMEM memory model which allows memory to be divided 102*6bf53999SMike Rapoportinto chunks of the same size. These chunks are called "sections". The size of 103*6bf53999SMike Rapoporta memory section is architecture dependent. For example, power uses 16MiB, ia64 104*6bf53999SMike Rapoportuses 1GiB. 105*6bf53999SMike Rapoport 106*6bf53999SMike RapoportMemory sections are combined into chunks referred to as "memory blocks". The 107*6bf53999SMike Rapoportsize of a memory block is architecture dependent and represents the logical 108*6bf53999SMike Rapoportunit upon which memory online/offline operations are to be performed. The 109*6bf53999SMike Rapoportdefault size of a memory block is the same as memory section size unless an 110*6bf53999SMike Rapoportarchitecture specifies otherwise. (see :ref:`memory_hotplug_sysfs_files`.) 111*6bf53999SMike Rapoport 112*6bf53999SMike RapoportTo determine the size (in bytes) of a memory block please read this file:: 113*6bf53999SMike Rapoport 114*6bf53999SMike Rapoport /sys/devices/system/memory/block_size_bytes 115*6bf53999SMike Rapoport 116*6bf53999SMike RapoportKernel Configuration 117*6bf53999SMike Rapoport==================== 118*6bf53999SMike Rapoport 119*6bf53999SMike RapoportTo use memory hotplug feature, kernel must be compiled with following 120*6bf53999SMike Rapoportconfig options. 121*6bf53999SMike Rapoport 122*6bf53999SMike Rapoport- For all memory hotplug: 123*6bf53999SMike Rapoport - Memory model -> Sparse Memory (``CONFIG_SPARSEMEM``) 124*6bf53999SMike Rapoport - Allow for memory hot-add (``CONFIG_MEMORY_HOTPLUG``) 125*6bf53999SMike Rapoport 126*6bf53999SMike Rapoport- To enable memory removal, the following are also necessary: 127*6bf53999SMike Rapoport - Allow for memory hot remove (``CONFIG_MEMORY_HOTREMOVE``) 128*6bf53999SMike Rapoport - Page Migration (``CONFIG_MIGRATION``) 129*6bf53999SMike Rapoport 130*6bf53999SMike Rapoport- For ACPI memory hotplug, the following are also necessary: 131*6bf53999SMike Rapoport - Memory hotplug (under ACPI Support menu) (``CONFIG_ACPI_HOTPLUG_MEMORY``) 132*6bf53999SMike Rapoport - This option can be kernel module. 133*6bf53999SMike Rapoport 134*6bf53999SMike Rapoport- As a related configuration, if your box has a feature of NUMA-node hotplug 135*6bf53999SMike Rapoport via ACPI, then this option is necessary too. 136*6bf53999SMike Rapoport 137*6bf53999SMike Rapoport - ACPI0004,PNP0A05 and PNP0A06 Container Driver (under ACPI Support menu) 138*6bf53999SMike Rapoport (``CONFIG_ACPI_CONTAINER``). 139*6bf53999SMike Rapoport 140*6bf53999SMike Rapoport This option can be kernel module too. 141*6bf53999SMike Rapoport 142*6bf53999SMike Rapoport 143*6bf53999SMike Rapoport.. _memory_hotplug_sysfs_files: 144*6bf53999SMike Rapoport 145*6bf53999SMike Rapoportsysfs files for memory hotplug 146*6bf53999SMike Rapoport============================== 147*6bf53999SMike Rapoport 148*6bf53999SMike RapoportAll memory blocks have their device information in sysfs. Each memory block 149*6bf53999SMike Rapoportis described under ``/sys/devices/system/memory`` as:: 150*6bf53999SMike Rapoport 151*6bf53999SMike Rapoport /sys/devices/system/memory/memoryXXX 152*6bf53999SMike Rapoport 153*6bf53999SMike Rapoportwhere XXX is the memory block id. 154*6bf53999SMike Rapoport 155*6bf53999SMike RapoportFor the memory block covered by the sysfs directory. It is expected that all 156*6bf53999SMike Rapoportmemory sections in this range are present and no memory holes exist in the 157*6bf53999SMike Rapoportrange. Currently there is no way to determine if there is a memory hole, but 158*6bf53999SMike Rapoportthe existence of one should not affect the hotplug capabilities of the memory 159*6bf53999SMike Rapoportblock. 160*6bf53999SMike Rapoport 161*6bf53999SMike RapoportFor example, assume 1GiB memory block size. A device for a memory starting at 162*6bf53999SMike Rapoport0x100000000 is ``/sys/device/system/memory/memory4``:: 163*6bf53999SMike Rapoport 164*6bf53999SMike Rapoport (0x100000000 / 1Gib = 4) 165*6bf53999SMike Rapoport 166*6bf53999SMike RapoportThis device covers address range [0x100000000 ... 0x140000000) 167*6bf53999SMike Rapoport 168*6bf53999SMike RapoportUnder each memory block, you can see 5 files: 169*6bf53999SMike Rapoport 170*6bf53999SMike Rapoport- ``/sys/devices/system/memory/memoryXXX/phys_index`` 171*6bf53999SMike Rapoport- ``/sys/devices/system/memory/memoryXXX/phys_device`` 172*6bf53999SMike Rapoport- ``/sys/devices/system/memory/memoryXXX/state`` 173*6bf53999SMike Rapoport- ``/sys/devices/system/memory/memoryXXX/removable`` 174*6bf53999SMike Rapoport- ``/sys/devices/system/memory/memoryXXX/valid_zones`` 175*6bf53999SMike Rapoport 176*6bf53999SMike Rapoport=================== ============================================================ 177*6bf53999SMike Rapoport``phys_index`` read-only and contains memory block id, same as XXX. 178*6bf53999SMike Rapoport``state`` read-write 179*6bf53999SMike Rapoport 180*6bf53999SMike Rapoport - at read: contains online/offline state of memory. 181*6bf53999SMike Rapoport - at write: user can specify "online_kernel", 182*6bf53999SMike Rapoport 183*6bf53999SMike Rapoport "online_movable", "online", "offline" command 184*6bf53999SMike Rapoport which will be performed on all sections in the block. 185*6bf53999SMike Rapoport``phys_device`` read-only: designed to show the name of physical memory 186*6bf53999SMike Rapoport device. This is not well implemented now. 187*6bf53999SMike Rapoport``removable`` read-only: contains an integer value indicating 188*6bf53999SMike Rapoport whether the memory block is removable or not 189*6bf53999SMike Rapoport removable. A value of 1 indicates that the memory 190*6bf53999SMike Rapoport block is removable and a value of 0 indicates that 191*6bf53999SMike Rapoport it is not removable. A memory block is removable only if 192*6bf53999SMike Rapoport every section in the block is removable. 193*6bf53999SMike Rapoport``valid_zones`` read-only: designed to show which zones this memory block 194*6bf53999SMike Rapoport can be onlined to. 195*6bf53999SMike Rapoport 196*6bf53999SMike Rapoport The first column shows it`s default zone. 197*6bf53999SMike Rapoport 198*6bf53999SMike Rapoport "memory6/valid_zones: Normal Movable" shows this memoryblock 199*6bf53999SMike Rapoport can be onlined to ZONE_NORMAL by default and to ZONE_MOVABLE 200*6bf53999SMike Rapoport by online_movable. 201*6bf53999SMike Rapoport 202*6bf53999SMike Rapoport "memory7/valid_zones: Movable Normal" shows this memoryblock 203*6bf53999SMike Rapoport can be onlined to ZONE_MOVABLE by default and to ZONE_NORMAL 204*6bf53999SMike Rapoport by online_kernel. 205*6bf53999SMike Rapoport=================== ============================================================ 206*6bf53999SMike Rapoport 207*6bf53999SMike Rapoport.. note:: 208*6bf53999SMike Rapoport 209*6bf53999SMike Rapoport These directories/files appear after physical memory hotplug phase. 210*6bf53999SMike Rapoport 211*6bf53999SMike RapoportIf CONFIG_NUMA is enabled the memoryXXX/ directories can also be accessed 212*6bf53999SMike Rapoportvia symbolic links located in the ``/sys/devices/system/node/node*`` directories. 213*6bf53999SMike Rapoport 214*6bf53999SMike RapoportFor example:: 215*6bf53999SMike Rapoport 216*6bf53999SMike Rapoport /sys/devices/system/node/node0/memory9 -> ../../memory/memory9 217*6bf53999SMike Rapoport 218*6bf53999SMike RapoportA backlink will also be created:: 219*6bf53999SMike Rapoport 220*6bf53999SMike Rapoport /sys/devices/system/memory/memory9/node0 -> ../../node/node0 221*6bf53999SMike Rapoport 222*6bf53999SMike Rapoport.. _memory_hotplug_physical_mem: 223*6bf53999SMike Rapoport 224*6bf53999SMike RapoportPhysical memory hot-add phase 225*6bf53999SMike Rapoport============================= 226*6bf53999SMike Rapoport 227*6bf53999SMike RapoportHardware(Firmware) Support 228*6bf53999SMike Rapoport-------------------------- 229*6bf53999SMike Rapoport 230*6bf53999SMike RapoportOn x86_64/ia64 platform, memory hotplug by ACPI is supported. 231*6bf53999SMike Rapoport 232*6bf53999SMike RapoportIn general, the firmware (ACPI) which supports memory hotplug defines 233*6bf53999SMike Rapoportmemory class object of _HID "PNP0C80". When a notify is asserted to PNP0C80, 234*6bf53999SMike RapoportLinux's ACPI handler does hot-add memory to the system and calls a hotplug udev 235*6bf53999SMike Rapoportscript. This will be done automatically. 236*6bf53999SMike Rapoport 237*6bf53999SMike RapoportBut scripts for memory hotplug are not contained in generic udev package(now). 238*6bf53999SMike RapoportYou may have to write it by yourself or online/offline memory by hand. 239*6bf53999SMike RapoportPlease see :ref:`memory_hotplug_how_to_online_memory` and 240*6bf53999SMike Rapoport:ref:`memory_hotplug_how_to_offline_memory`. 241*6bf53999SMike Rapoport 242*6bf53999SMike RapoportIf firmware supports NUMA-node hotplug, and defines an object _HID "ACPI0004", 243*6bf53999SMike Rapoport"PNP0A05", or "PNP0A06", notification is asserted to it, and ACPI handler 244*6bf53999SMike Rapoportcalls hotplug code for all of objects which are defined in it. 245*6bf53999SMike RapoportIf memory device is found, memory hotplug code will be called. 246*6bf53999SMike Rapoport 247*6bf53999SMike RapoportNotify memory hot-add event by hand 248*6bf53999SMike Rapoport----------------------------------- 249*6bf53999SMike Rapoport 250*6bf53999SMike RapoportOn some architectures, the firmware may not notify the kernel of a memory 251*6bf53999SMike Rapoporthotplug event. Therefore, the memory "probe" interface is supported to 252*6bf53999SMike Rapoportexplicitly notify the kernel. This interface depends on 253*6bf53999SMike RapoportCONFIG_ARCH_MEMORY_PROBE and can be configured on powerpc, sh, and x86 254*6bf53999SMike Rapoportif hotplug is supported, although for x86 this should be handled by ACPI 255*6bf53999SMike Rapoportnotification. 256*6bf53999SMike Rapoport 257*6bf53999SMike RapoportProbe interface is located at:: 258*6bf53999SMike Rapoport 259*6bf53999SMike Rapoport /sys/devices/system/memory/probe 260*6bf53999SMike Rapoport 261*6bf53999SMike RapoportYou can tell the physical address of new memory to the kernel by:: 262*6bf53999SMike Rapoport 263*6bf53999SMike Rapoport % echo start_address_of_new_memory > /sys/devices/system/memory/probe 264*6bf53999SMike Rapoport 265*6bf53999SMike RapoportThen, [start_address_of_new_memory, start_address_of_new_memory + 266*6bf53999SMike Rapoportmemory_block_size] memory range is hot-added. In this case, hotplug script is 267*6bf53999SMike Rapoportnot called (in current implementation). You'll have to online memory by 268*6bf53999SMike Rapoportyourself. Please see :ref:`memory_hotplug_how_to_online_memory`. 269*6bf53999SMike Rapoport 270*6bf53999SMike RapoportLogical Memory hot-add phase 271*6bf53999SMike Rapoport============================ 272*6bf53999SMike Rapoport 273*6bf53999SMike RapoportState of memory 274*6bf53999SMike Rapoport--------------- 275*6bf53999SMike Rapoport 276*6bf53999SMike RapoportTo see (online/offline) state of a memory block, read 'state' file:: 277*6bf53999SMike Rapoport 278*6bf53999SMike Rapoport % cat /sys/device/system/memory/memoryXXX/state 279*6bf53999SMike Rapoport 280*6bf53999SMike Rapoport 281*6bf53999SMike Rapoport- If the memory block is online, you'll read "online". 282*6bf53999SMike Rapoport- If the memory block is offline, you'll read "offline". 283*6bf53999SMike Rapoport 284*6bf53999SMike Rapoport 285*6bf53999SMike Rapoport.. _memory_hotplug_how_to_online_memory: 286*6bf53999SMike Rapoport 287*6bf53999SMike RapoportHow to online memory 288*6bf53999SMike Rapoport-------------------- 289*6bf53999SMike Rapoport 290*6bf53999SMike RapoportWhen the memory is hot-added, the kernel decides whether or not to "online" 291*6bf53999SMike Rapoportit according to the policy which can be read from "auto_online_blocks" file:: 292*6bf53999SMike Rapoport 293*6bf53999SMike Rapoport % cat /sys/devices/system/memory/auto_online_blocks 294*6bf53999SMike Rapoport 295*6bf53999SMike RapoportThe default depends on the CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE kernel config 296*6bf53999SMike Rapoportoption. If it is disabled the default is "offline" which means the newly added 297*6bf53999SMike Rapoportmemory is not in a ready-to-use state and you have to "online" the newly added 298*6bf53999SMike Rapoportmemory blocks manually. Automatic onlining can be requested by writing "online" 299*6bf53999SMike Rapoportto "auto_online_blocks" file:: 300*6bf53999SMike Rapoport 301*6bf53999SMike Rapoport % echo online > /sys/devices/system/memory/auto_online_blocks 302*6bf53999SMike Rapoport 303*6bf53999SMike RapoportThis sets a global policy and impacts all memory blocks that will subsequently 304*6bf53999SMike Rapoportbe hotplugged. Currently offline blocks keep their state. It is possible, under 305*6bf53999SMike Rapoportcertain circumstances, that some memory blocks will be added but will fail to 306*6bf53999SMike Rapoportonline. User space tools can check their "state" files 307*6bf53999SMike Rapoport(``/sys/devices/system/memory/memoryXXX/state``) and try to online them manually. 308*6bf53999SMike Rapoport 309*6bf53999SMike RapoportIf the automatic onlining wasn't requested, failed, or some memory block was 310*6bf53999SMike Rapoportofflined it is possible to change the individual block's state by writing to the 311*6bf53999SMike Rapoport"state" file:: 312*6bf53999SMike Rapoport 313*6bf53999SMike Rapoport % echo online > /sys/devices/system/memory/memoryXXX/state 314*6bf53999SMike Rapoport 315*6bf53999SMike RapoportThis onlining will not change the ZONE type of the target memory block, 316*6bf53999SMike RapoportIf the memory block doesn't belong to any zone an appropriate kernel zone 317*6bf53999SMike Rapoport(usually ZONE_NORMAL) will be used unless movable_node kernel command line 318*6bf53999SMike Rapoportoption is specified when ZONE_MOVABLE will be used. 319*6bf53999SMike Rapoport 320*6bf53999SMike RapoportYou can explicitly request to associate it with ZONE_MOVABLE by:: 321*6bf53999SMike Rapoport 322*6bf53999SMike Rapoport % echo online_movable > /sys/devices/system/memory/memoryXXX/state 323*6bf53999SMike Rapoport 324*6bf53999SMike Rapoport.. note:: current limit: this memory block must be adjacent to ZONE_MOVABLE 325*6bf53999SMike Rapoport 326*6bf53999SMike RapoportOr you can explicitly request a kernel zone (usually ZONE_NORMAL) by:: 327*6bf53999SMike Rapoport 328*6bf53999SMike Rapoport % echo online_kernel > /sys/devices/system/memory/memoryXXX/state 329*6bf53999SMike Rapoport 330*6bf53999SMike Rapoport.. note:: current limit: this memory block must be adjacent to ZONE_NORMAL 331*6bf53999SMike Rapoport 332*6bf53999SMike RapoportAn explicit zone onlining can fail (e.g. when the range is already within 333*6bf53999SMike Rapoportand existing and incompatible zone already). 334*6bf53999SMike Rapoport 335*6bf53999SMike RapoportAfter this, memory block XXX's state will be 'online' and the amount of 336*6bf53999SMike Rapoportavailable memory will be increased. 337*6bf53999SMike Rapoport 338*6bf53999SMike RapoportThis may be changed in future. 339*6bf53999SMike Rapoport 340*6bf53999SMike RapoportLogical memory remove 341*6bf53999SMike Rapoport===================== 342*6bf53999SMike Rapoport 343*6bf53999SMike RapoportMemory offline and ZONE_MOVABLE 344*6bf53999SMike Rapoport------------------------------- 345*6bf53999SMike Rapoport 346*6bf53999SMike RapoportMemory offlining is more complicated than memory online. Because memory offline 347*6bf53999SMike Rapoporthas to make the whole memory block be unused, memory offline can fail if 348*6bf53999SMike Rapoportthe memory block includes memory which cannot be freed. 349*6bf53999SMike Rapoport 350*6bf53999SMike RapoportIn general, memory offline can use 2 techniques. 351*6bf53999SMike Rapoport 352*6bf53999SMike Rapoport(1) reclaim and free all memory in the memory block. 353*6bf53999SMike Rapoport(2) migrate all pages in the memory block. 354*6bf53999SMike Rapoport 355*6bf53999SMike RapoportIn the current implementation, Linux's memory offline uses method (2), freeing 356*6bf53999SMike Rapoportall pages in the memory block by page migration. But not all pages are 357*6bf53999SMike Rapoportmigratable. Under current Linux, migratable pages are anonymous pages and 358*6bf53999SMike Rapoportpage caches. For offlining a memory block by migration, the kernel has to 359*6bf53999SMike Rapoportguarantee that the memory block contains only migratable pages. 360*6bf53999SMike Rapoport 361*6bf53999SMike RapoportNow, a boot option for making a memory block which consists of migratable pages 362*6bf53999SMike Rapoportis supported. By specifying "kernelcore=" or "movablecore=" boot option, you can 363*6bf53999SMike Rapoportcreate ZONE_MOVABLE...a zone which is just used for movable pages. 364*6bf53999SMike Rapoport(See also Documentation/admin-guide/kernel-parameters.rst) 365*6bf53999SMike Rapoport 366*6bf53999SMike RapoportAssume the system has "TOTAL" amount of memory at boot time, this boot option 367*6bf53999SMike Rapoportcreates ZONE_MOVABLE as following. 368*6bf53999SMike Rapoport 369*6bf53999SMike Rapoport1) When kernelcore=YYYY boot option is used, 370*6bf53999SMike Rapoport Size of memory not for movable pages (not for offline) is YYYY. 371*6bf53999SMike Rapoport Size of memory for movable pages (for offline) is TOTAL-YYYY. 372*6bf53999SMike Rapoport 373*6bf53999SMike Rapoport2) When movablecore=ZZZZ boot option is used, 374*6bf53999SMike Rapoport Size of memory not for movable pages (not for offline) is TOTAL - ZZZZ. 375*6bf53999SMike Rapoport Size of memory for movable pages (for offline) is ZZZZ. 376*6bf53999SMike Rapoport 377*6bf53999SMike Rapoport.. note:: 378*6bf53999SMike Rapoport 379*6bf53999SMike Rapoport Unfortunately, there is no information to show which memory block belongs 380*6bf53999SMike Rapoport to ZONE_MOVABLE. This is TBD. 381*6bf53999SMike Rapoport 382*6bf53999SMike Rapoport.. _memory_hotplug_how_to_offline_memory: 383*6bf53999SMike Rapoport 384*6bf53999SMike RapoportHow to offline memory 385*6bf53999SMike Rapoport--------------------- 386*6bf53999SMike Rapoport 387*6bf53999SMike RapoportYou can offline a memory block by using the same sysfs interface that was used 388*6bf53999SMike Rapoportin memory onlining:: 389*6bf53999SMike Rapoport 390*6bf53999SMike Rapoport % echo offline > /sys/devices/system/memory/memoryXXX/state 391*6bf53999SMike Rapoport 392*6bf53999SMike RapoportIf offline succeeds, the state of the memory block is changed to be "offline". 393*6bf53999SMike RapoportIf it fails, some error core (like -EBUSY) will be returned by the kernel. 394*6bf53999SMike RapoportEven if a memory block does not belong to ZONE_MOVABLE, you can try to offline 395*6bf53999SMike Rapoportit. If it doesn't contain 'unmovable' memory, you'll get success. 396*6bf53999SMike Rapoport 397*6bf53999SMike RapoportA memory block under ZONE_MOVABLE is considered to be able to be offlined 398*6bf53999SMike Rapoporteasily. But under some busy state, it may return -EBUSY. Even if a memory 399*6bf53999SMike Rapoportblock cannot be offlined due to -EBUSY, you can retry offlining it and may be 400*6bf53999SMike Rapoportable to offline it (or not). (For example, a page is referred to by some kernel 401*6bf53999SMike Rapoportinternal call and released soon.) 402*6bf53999SMike Rapoport 403*6bf53999SMike RapoportConsideration: 404*6bf53999SMike Rapoport Memory hotplug's design direction is to make the possibility of memory 405*6bf53999SMike Rapoport offlining higher and to guarantee unplugging memory under any situation. But 406*6bf53999SMike Rapoport it needs more work. Returning -EBUSY under some situation may be good because 407*6bf53999SMike Rapoport the user can decide to retry more or not by himself. Currently, memory 408*6bf53999SMike Rapoport offlining code does some amount of retry with 120 seconds timeout. 409*6bf53999SMike Rapoport 410*6bf53999SMike RapoportPhysical memory remove 411*6bf53999SMike Rapoport====================== 412*6bf53999SMike Rapoport 413*6bf53999SMike RapoportNeed more implementation yet.... 414*6bf53999SMike Rapoport - Notification completion of remove works by OS to firmware. 415*6bf53999SMike Rapoport - Guard from remove if not yet. 416*6bf53999SMike Rapoport 417*6bf53999SMike RapoportMemory hotplug event notifier 418*6bf53999SMike Rapoport============================= 419*6bf53999SMike Rapoport 420*6bf53999SMike RapoportHotplugging events are sent to a notification queue. 421*6bf53999SMike Rapoport 422*6bf53999SMike RapoportThere are six types of notification defined in ``include/linux/memory.h``: 423*6bf53999SMike Rapoport 424*6bf53999SMike RapoportMEM_GOING_ONLINE 425*6bf53999SMike Rapoport Generated before new memory becomes available in order to be able to 426*6bf53999SMike Rapoport prepare subsystems to handle memory. The page allocator is still unable 427*6bf53999SMike Rapoport to allocate from the new memory. 428*6bf53999SMike Rapoport 429*6bf53999SMike RapoportMEM_CANCEL_ONLINE 430*6bf53999SMike Rapoport Generated if MEMORY_GOING_ONLINE fails. 431*6bf53999SMike Rapoport 432*6bf53999SMike RapoportMEM_ONLINE 433*6bf53999SMike Rapoport Generated when memory has successfully brought online. The callback may 434*6bf53999SMike Rapoport allocate pages from the new memory. 435*6bf53999SMike Rapoport 436*6bf53999SMike RapoportMEM_GOING_OFFLINE 437*6bf53999SMike Rapoport Generated to begin the process of offlining memory. Allocations are no 438*6bf53999SMike Rapoport longer possible from the memory but some of the memory to be offlined 439*6bf53999SMike Rapoport is still in use. The callback can be used to free memory known to a 440*6bf53999SMike Rapoport subsystem from the indicated memory block. 441*6bf53999SMike Rapoport 442*6bf53999SMike RapoportMEM_CANCEL_OFFLINE 443*6bf53999SMike Rapoport Generated if MEMORY_GOING_OFFLINE fails. Memory is available again from 444*6bf53999SMike Rapoport the memory block that we attempted to offline. 445*6bf53999SMike Rapoport 446*6bf53999SMike RapoportMEM_OFFLINE 447*6bf53999SMike Rapoport Generated after offlining memory is complete. 448*6bf53999SMike Rapoport 449*6bf53999SMike RapoportA callback routine can be registered by calling:: 450*6bf53999SMike Rapoport 451*6bf53999SMike Rapoport hotplug_memory_notifier(callback_func, priority) 452*6bf53999SMike Rapoport 453*6bf53999SMike RapoportCallback functions with higher values of priority are called before callback 454*6bf53999SMike Rapoportfunctions with lower values. 455*6bf53999SMike Rapoport 456*6bf53999SMike RapoportA callback function must have the following prototype:: 457*6bf53999SMike Rapoport 458*6bf53999SMike Rapoport int callback_func( 459*6bf53999SMike Rapoport struct notifier_block *self, unsigned long action, void *arg); 460*6bf53999SMike Rapoport 461*6bf53999SMike RapoportThe first argument of the callback function (self) is a pointer to the block 462*6bf53999SMike Rapoportof the notifier chain that points to the callback function itself. 463*6bf53999SMike RapoportThe second argument (action) is one of the event types described above. 464*6bf53999SMike RapoportThe third argument (arg) passes a pointer of struct memory_notify:: 465*6bf53999SMike Rapoport 466*6bf53999SMike Rapoport struct memory_notify { 467*6bf53999SMike Rapoport unsigned long start_pfn; 468*6bf53999SMike Rapoport unsigned long nr_pages; 469*6bf53999SMike Rapoport int status_change_nid_normal; 470*6bf53999SMike Rapoport int status_change_nid_high; 471*6bf53999SMike Rapoport int status_change_nid; 472*6bf53999SMike Rapoport } 473*6bf53999SMike Rapoport 474*6bf53999SMike Rapoport- start_pfn is start_pfn of online/offline memory. 475*6bf53999SMike Rapoport- nr_pages is # of pages of online/offline memory. 476*6bf53999SMike Rapoport- status_change_nid_normal is set node id when N_NORMAL_MEMORY of nodemask 477*6bf53999SMike Rapoport is (will be) set/clear, if this is -1, then nodemask status is not changed. 478*6bf53999SMike Rapoport- status_change_nid_high is set node id when N_HIGH_MEMORY of nodemask 479*6bf53999SMike Rapoport is (will be) set/clear, if this is -1, then nodemask status is not changed. 480*6bf53999SMike Rapoport- status_change_nid is set node id when N_MEMORY of nodemask is (will be) 481*6bf53999SMike Rapoport set/clear. It means a new(memoryless) node gets new memory by online and a 482*6bf53999SMike Rapoport node loses all memory. If this is -1, then nodemask status is not changed. 483*6bf53999SMike Rapoport 484*6bf53999SMike Rapoport If status_changed_nid* >= 0, callback should create/discard structures for the 485*6bf53999SMike Rapoport node if necessary. 486*6bf53999SMike Rapoport 487*6bf53999SMike RapoportThe callback routine shall return one of the values 488*6bf53999SMike RapoportNOTIFY_DONE, NOTIFY_OK, NOTIFY_BAD, NOTIFY_STOP 489*6bf53999SMike Rapoportdefined in ``include/linux/notifier.h`` 490*6bf53999SMike Rapoport 491*6bf53999SMike RapoportNOTIFY_DONE and NOTIFY_OK have no effect on the further processing. 492*6bf53999SMike Rapoport 493*6bf53999SMike RapoportNOTIFY_BAD is used as response to the MEM_GOING_ONLINE, MEM_GOING_OFFLINE, 494*6bf53999SMike RapoportMEM_ONLINE, or MEM_OFFLINE action to cancel hotplugging. It stops 495*6bf53999SMike Rapoportfurther processing of the notification queue. 496*6bf53999SMike Rapoport 497*6bf53999SMike RapoportNOTIFY_STOP stops further processing of the notification queue. 498*6bf53999SMike Rapoport 499*6bf53999SMike RapoportFuture Work 500*6bf53999SMike Rapoport=========== 501*6bf53999SMike Rapoport 502*6bf53999SMike Rapoport - allowing memory hot-add to ZONE_MOVABLE. maybe we need some switch like 503*6bf53999SMike Rapoport sysctl or new control file. 504*6bf53999SMike Rapoport - showing memory block and physical device relationship. 505*6bf53999SMike Rapoport - test and make it better memory offlining. 506*6bf53999SMike Rapoport - support HugeTLB page migration and offlining. 507*6bf53999SMike Rapoport - memmap removing at memory offline. 508*6bf53999SMike Rapoport - physical remove memory. 509