xref: /OK3568_Linux_fs/kernel/Documentation/admin-guide/mm/memory-hotplug.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun.. _admin_guide_memory_hotplug:
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun==============
4*4882a593SmuzhiyunMemory Hotplug
5*4882a593Smuzhiyun==============
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun:Created:							Jul 28 2007
8*4882a593Smuzhiyun:Updated: Add some details about locking internals:		Aug 20 2018
9*4882a593Smuzhiyun
10*4882a593SmuzhiyunThis document is about memory hotplug including how-to-use and current status.
11*4882a593SmuzhiyunBecause Memory Hotplug is still under development, contents of this text will
12*4882a593Smuzhiyunbe changed often.
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun.. contents:: :local:
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun.. note::
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun    (1) x86_64's has special implementation for memory hotplug.
19*4882a593Smuzhiyun        This text does not describe it.
20*4882a593Smuzhiyun    (2) This text assumes that sysfs is mounted at ``/sys``.
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun
23*4882a593SmuzhiyunIntroduction
24*4882a593Smuzhiyun============
25*4882a593Smuzhiyun
26*4882a593SmuzhiyunPurpose of memory hotplug
27*4882a593Smuzhiyun-------------------------
28*4882a593Smuzhiyun
29*4882a593SmuzhiyunMemory Hotplug allows users to increase/decrease the amount of memory.
30*4882a593SmuzhiyunGenerally, there are two purposes.
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun(A) For changing the amount of memory.
33*4882a593Smuzhiyun    This is to allow a feature like capacity on demand.
34*4882a593Smuzhiyun(B) For installing/removing DIMMs or NUMA-nodes physically.
35*4882a593Smuzhiyun    This is to exchange DIMMs/NUMA-nodes, reduce power consumption, etc.
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun(A) is required by highly virtualized environments and (B) is required by
38*4882a593Smuzhiyunhardware which supports memory power management.
39*4882a593Smuzhiyun
40*4882a593SmuzhiyunLinux memory hotplug is designed for both purpose.
41*4882a593Smuzhiyun
42*4882a593SmuzhiyunPhases of memory hotplug
43*4882a593Smuzhiyun------------------------
44*4882a593Smuzhiyun
45*4882a593SmuzhiyunThere are 2 phases in Memory Hotplug:
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun  1) Physical Memory Hotplug phase
48*4882a593Smuzhiyun  2) Logical Memory Hotplug phase.
49*4882a593Smuzhiyun
50*4882a593SmuzhiyunThe First phase is to communicate hardware/firmware and make/erase
51*4882a593Smuzhiyunenvironment for hotplugged memory. Basically, this phase is necessary
52*4882a593Smuzhiyunfor the purpose (B), but this is good phase for communication between
53*4882a593Smuzhiyunhighly virtualized environments too.
54*4882a593Smuzhiyun
55*4882a593SmuzhiyunWhen memory is hotplugged, the kernel recognizes new memory, makes new memory
56*4882a593Smuzhiyunmanagement tables, and makes sysfs files for new memory's operation.
57*4882a593Smuzhiyun
58*4882a593SmuzhiyunIf firmware supports notification of connection of new memory to OS,
59*4882a593Smuzhiyunthis phase is triggered automatically. ACPI can notify this event. If not,
60*4882a593Smuzhiyun"probe" operation by system administration is used instead.
61*4882a593Smuzhiyun(see :ref:`memory_hotplug_physical_mem`).
62*4882a593Smuzhiyun
63*4882a593SmuzhiyunLogical Memory Hotplug phase is to change memory state into
64*4882a593Smuzhiyunavailable/unavailable for users. Amount of memory from user's view is
65*4882a593Smuzhiyunchanged by this phase. The kernel makes all memory in it as free pages
66*4882a593Smuzhiyunwhen a memory range is available.
67*4882a593Smuzhiyun
68*4882a593SmuzhiyunIn this document, this phase is described as online/offline.
69*4882a593Smuzhiyun
70*4882a593SmuzhiyunLogical Memory Hotplug phase is triggered by write of sysfs file by system
71*4882a593Smuzhiyunadministrator. For the hot-add case, it must be executed after Physical Hotplug
72*4882a593Smuzhiyunphase by hand.
73*4882a593Smuzhiyun(However, if you writes udev's hotplug scripts for memory hotplug, these
74*4882a593Smuzhiyunphases can be execute in seamless way.)
75*4882a593Smuzhiyun
76*4882a593SmuzhiyunUnit of Memory online/offline operation
77*4882a593Smuzhiyun---------------------------------------
78*4882a593Smuzhiyun
79*4882a593SmuzhiyunMemory hotplug uses SPARSEMEM memory model which allows memory to be divided
80*4882a593Smuzhiyuninto chunks of the same size. These chunks are called "sections". The size of
81*4882a593Smuzhiyuna memory section is architecture dependent. For example, power uses 16MiB, ia64
82*4882a593Smuzhiyunuses 1GiB.
83*4882a593Smuzhiyun
84*4882a593SmuzhiyunMemory sections are combined into chunks referred to as "memory blocks". The
85*4882a593Smuzhiyunsize of a memory block is architecture dependent and represents the logical
86*4882a593Smuzhiyununit upon which memory online/offline operations are to be performed. The
87*4882a593Smuzhiyundefault size of a memory block is the same as memory section size unless an
88*4882a593Smuzhiyunarchitecture specifies otherwise. (see :ref:`memory_hotplug_sysfs_files`.)
89*4882a593Smuzhiyun
90*4882a593SmuzhiyunTo determine the size (in bytes) of a memory block please read this file::
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun  /sys/devices/system/memory/block_size_bytes
93*4882a593Smuzhiyun
94*4882a593SmuzhiyunKernel Configuration
95*4882a593Smuzhiyun====================
96*4882a593Smuzhiyun
97*4882a593SmuzhiyunTo use memory hotplug feature, kernel must be compiled with following
98*4882a593Smuzhiyunconfig options.
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun- For all memory hotplug:
101*4882a593Smuzhiyun    - Memory model -> Sparse Memory  (``CONFIG_SPARSEMEM``)
102*4882a593Smuzhiyun    - Allow for memory hot-add       (``CONFIG_MEMORY_HOTPLUG``)
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun- To enable memory removal, the following are also necessary:
105*4882a593Smuzhiyun    - Allow for memory hot remove    (``CONFIG_MEMORY_HOTREMOVE``)
106*4882a593Smuzhiyun    - Page Migration                 (``CONFIG_MIGRATION``)
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun- For ACPI memory hotplug, the following are also necessary:
109*4882a593Smuzhiyun    - Memory hotplug (under ACPI Support menu) (``CONFIG_ACPI_HOTPLUG_MEMORY``)
110*4882a593Smuzhiyun    - This option can be kernel module.
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun- As a related configuration, if your box has a feature of NUMA-node hotplug
113*4882a593Smuzhiyun  via ACPI, then this option is necessary too.
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun    - ACPI0004,PNP0A05 and PNP0A06 Container Driver (under ACPI Support menu)
116*4882a593Smuzhiyun      (``CONFIG_ACPI_CONTAINER``).
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun     This option can be kernel module too.
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun.. _memory_hotplug_sysfs_files:
122*4882a593Smuzhiyun
123*4882a593Smuzhiyunsysfs files for memory hotplug
124*4882a593Smuzhiyun==============================
125*4882a593Smuzhiyun
126*4882a593SmuzhiyunAll memory blocks have their device information in sysfs.  Each memory block
127*4882a593Smuzhiyunis described under ``/sys/devices/system/memory`` as::
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun	/sys/devices/system/memory/memoryXXX
130*4882a593Smuzhiyun
131*4882a593Smuzhiyunwhere XXX is the memory block id.
132*4882a593Smuzhiyun
133*4882a593SmuzhiyunFor the memory block covered by the sysfs directory.  It is expected that all
134*4882a593Smuzhiyunmemory sections in this range are present and no memory holes exist in the
135*4882a593Smuzhiyunrange. Currently there is no way to determine if there is a memory hole, but
136*4882a593Smuzhiyunthe existence of one should not affect the hotplug capabilities of the memory
137*4882a593Smuzhiyunblock.
138*4882a593Smuzhiyun
139*4882a593SmuzhiyunFor example, assume 1GiB memory block size. A device for a memory starting at
140*4882a593Smuzhiyun0x100000000 is ``/sys/device/system/memory/memory4``::
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun	(0x100000000 / 1Gib = 4)
143*4882a593Smuzhiyun
144*4882a593SmuzhiyunThis device covers address range [0x100000000 ... 0x140000000)
145*4882a593Smuzhiyun
146*4882a593SmuzhiyunUnder each memory block, you can see 5 files:
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun- ``/sys/devices/system/memory/memoryXXX/phys_index``
149*4882a593Smuzhiyun- ``/sys/devices/system/memory/memoryXXX/phys_device``
150*4882a593Smuzhiyun- ``/sys/devices/system/memory/memoryXXX/state``
151*4882a593Smuzhiyun- ``/sys/devices/system/memory/memoryXXX/removable``
152*4882a593Smuzhiyun- ``/sys/devices/system/memory/memoryXXX/valid_zones``
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun=================== ============================================================
155*4882a593Smuzhiyun``phys_index``      read-only and contains memory block id, same as XXX.
156*4882a593Smuzhiyun``state``           read-write
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun                    - at read:  contains online/offline state of memory.
159*4882a593Smuzhiyun                    - at write: user can specify "online_kernel",
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun                    "online_movable", "online", "offline" command
162*4882a593Smuzhiyun                    which will be performed on all sections in the block.
163*4882a593Smuzhiyun``phys_device``	    read-only: legacy interface only ever used on s390x to
164*4882a593Smuzhiyun		    expose the covered storage increment.
165*4882a593Smuzhiyun``removable``       read-only: contains an integer value indicating
166*4882a593Smuzhiyun                    whether the memory block is removable or not
167*4882a593Smuzhiyun                    removable.  A value of 1 indicates that the memory
168*4882a593Smuzhiyun                    block is removable and a value of 0 indicates that
169*4882a593Smuzhiyun                    it is not removable. A memory block is removable only if
170*4882a593Smuzhiyun                    every section in the block is removable.
171*4882a593Smuzhiyun``valid_zones``     read-only: designed to show which zones this memory block
172*4882a593Smuzhiyun		    can be onlined to.
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun		    The first column shows it`s default zone.
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun		    "memory6/valid_zones: Normal Movable" shows this memoryblock
177*4882a593Smuzhiyun		    can be onlined to ZONE_NORMAL by default and to ZONE_MOVABLE
178*4882a593Smuzhiyun		    by online_movable.
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun		    "memory7/valid_zones: Movable Normal" shows this memoryblock
181*4882a593Smuzhiyun		    can be onlined to ZONE_MOVABLE by default and to ZONE_NORMAL
182*4882a593Smuzhiyun		    by online_kernel.
183*4882a593Smuzhiyun=================== ============================================================
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun.. note::
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun  These directories/files appear after physical memory hotplug phase.
188*4882a593Smuzhiyun
189*4882a593SmuzhiyunIf CONFIG_NUMA is enabled the memoryXXX/ directories can also be accessed
190*4882a593Smuzhiyunvia symbolic links located in the ``/sys/devices/system/node/node*`` directories.
191*4882a593Smuzhiyun
192*4882a593SmuzhiyunFor example::
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun	/sys/devices/system/node/node0/memory9 -> ../../memory/memory9
195*4882a593Smuzhiyun
196*4882a593SmuzhiyunA backlink will also be created::
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun	/sys/devices/system/memory/memory9/node0 -> ../../node/node0
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun.. _memory_hotplug_physical_mem:
201*4882a593Smuzhiyun
202*4882a593SmuzhiyunPhysical memory hot-add phase
203*4882a593Smuzhiyun=============================
204*4882a593Smuzhiyun
205*4882a593SmuzhiyunHardware(Firmware) Support
206*4882a593Smuzhiyun--------------------------
207*4882a593Smuzhiyun
208*4882a593SmuzhiyunOn x86_64/ia64 platform, memory hotplug by ACPI is supported.
209*4882a593Smuzhiyun
210*4882a593SmuzhiyunIn general, the firmware (ACPI) which supports memory hotplug defines
211*4882a593Smuzhiyunmemory class object of _HID "PNP0C80". When a notify is asserted to PNP0C80,
212*4882a593SmuzhiyunLinux's ACPI handler does hot-add memory to the system and calls a hotplug udev
213*4882a593Smuzhiyunscript. This will be done automatically.
214*4882a593Smuzhiyun
215*4882a593SmuzhiyunBut scripts for memory hotplug are not contained in generic udev package(now).
216*4882a593SmuzhiyunYou may have to write it by yourself or online/offline memory by hand.
217*4882a593SmuzhiyunPlease see :ref:`memory_hotplug_how_to_online_memory` and
218*4882a593Smuzhiyun:ref:`memory_hotplug_how_to_offline_memory`.
219*4882a593Smuzhiyun
220*4882a593SmuzhiyunIf firmware supports NUMA-node hotplug, and defines an object _HID "ACPI0004",
221*4882a593Smuzhiyun"PNP0A05", or "PNP0A06", notification is asserted to it, and ACPI handler
222*4882a593Smuzhiyuncalls hotplug code for all of objects which are defined in it.
223*4882a593SmuzhiyunIf memory device is found, memory hotplug code will be called.
224*4882a593Smuzhiyun
225*4882a593SmuzhiyunNotify memory hot-add event by hand
226*4882a593Smuzhiyun-----------------------------------
227*4882a593Smuzhiyun
228*4882a593SmuzhiyunOn some architectures, the firmware may not notify the kernel of a memory
229*4882a593Smuzhiyunhotplug event.  Therefore, the memory "probe" interface is supported to
230*4882a593Smuzhiyunexplicitly notify the kernel.  This interface depends on
231*4882a593SmuzhiyunCONFIG_ARCH_MEMORY_PROBE and can be configured on powerpc, sh, and x86
232*4882a593Smuzhiyunif hotplug is supported, although for x86 this should be handled by ACPI
233*4882a593Smuzhiyunnotification.
234*4882a593Smuzhiyun
235*4882a593SmuzhiyunProbe interface is located at::
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun	/sys/devices/system/memory/probe
238*4882a593Smuzhiyun
239*4882a593SmuzhiyunYou can tell the physical address of new memory to the kernel by::
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun	% echo start_address_of_new_memory > /sys/devices/system/memory/probe
242*4882a593Smuzhiyun
243*4882a593SmuzhiyunThen, [start_address_of_new_memory, start_address_of_new_memory +
244*4882a593Smuzhiyunmemory_block_size] memory range is hot-added. In this case, hotplug script is
245*4882a593Smuzhiyunnot called (in current implementation). You'll have to online memory by
246*4882a593Smuzhiyunyourself.  Please see :ref:`memory_hotplug_how_to_online_memory`.
247*4882a593Smuzhiyun
248*4882a593SmuzhiyunLogical Memory hot-add phase
249*4882a593Smuzhiyun============================
250*4882a593Smuzhiyun
251*4882a593SmuzhiyunState of memory
252*4882a593Smuzhiyun---------------
253*4882a593Smuzhiyun
254*4882a593SmuzhiyunTo see (online/offline) state of a memory block, read 'state' file::
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun	% cat /sys/device/system/memory/memoryXXX/state
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun- If the memory block is online, you'll read "online".
260*4882a593Smuzhiyun- If the memory block is offline, you'll read "offline".
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun.. _memory_hotplug_how_to_online_memory:
264*4882a593Smuzhiyun
265*4882a593SmuzhiyunHow to online memory
266*4882a593Smuzhiyun--------------------
267*4882a593Smuzhiyun
268*4882a593SmuzhiyunWhen the memory is hot-added, the kernel decides whether or not to "online"
269*4882a593Smuzhiyunit according to the policy which can be read from "auto_online_blocks" file::
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun	% cat /sys/devices/system/memory/auto_online_blocks
272*4882a593Smuzhiyun
273*4882a593SmuzhiyunThe default depends on the CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE kernel config
274*4882a593Smuzhiyunoption. If it is disabled the default is "offline" which means the newly added
275*4882a593Smuzhiyunmemory is not in a ready-to-use state and you have to "online" the newly added
276*4882a593Smuzhiyunmemory blocks manually. Automatic onlining can be requested by writing "online"
277*4882a593Smuzhiyunto "auto_online_blocks" file::
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun	% echo online > /sys/devices/system/memory/auto_online_blocks
280*4882a593Smuzhiyun
281*4882a593SmuzhiyunThis sets a global policy and impacts all memory blocks that will subsequently
282*4882a593Smuzhiyunbe hotplugged. Currently offline blocks keep their state. It is possible, under
283*4882a593Smuzhiyuncertain circumstances, that some memory blocks will be added but will fail to
284*4882a593Smuzhiyunonline. User space tools can check their "state" files
285*4882a593Smuzhiyun(``/sys/devices/system/memory/memoryXXX/state``) and try to online them manually.
286*4882a593Smuzhiyun
287*4882a593SmuzhiyunIf the automatic onlining wasn't requested, failed, or some memory block was
288*4882a593Smuzhiyunofflined it is possible to change the individual block's state by writing to the
289*4882a593Smuzhiyun"state" file::
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun	% echo online > /sys/devices/system/memory/memoryXXX/state
292*4882a593Smuzhiyun
293*4882a593SmuzhiyunThis onlining will not change the ZONE type of the target memory block,
294*4882a593SmuzhiyunIf the memory block doesn't belong to any zone an appropriate kernel zone
295*4882a593Smuzhiyun(usually ZONE_NORMAL) will be used unless movable_node kernel command line
296*4882a593Smuzhiyunoption is specified when ZONE_MOVABLE will be used.
297*4882a593Smuzhiyun
298*4882a593SmuzhiyunYou can explicitly request to associate it with ZONE_MOVABLE by::
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun	% echo online_movable > /sys/devices/system/memory/memoryXXX/state
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun.. note:: current limit: this memory block must be adjacent to ZONE_MOVABLE
303*4882a593Smuzhiyun
304*4882a593SmuzhiyunOr you can explicitly request a kernel zone (usually ZONE_NORMAL) by::
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun	% echo online_kernel > /sys/devices/system/memory/memoryXXX/state
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun.. note:: current limit: this memory block must be adjacent to ZONE_NORMAL
309*4882a593Smuzhiyun
310*4882a593SmuzhiyunAn explicit zone onlining can fail (e.g. when the range is already within
311*4882a593Smuzhiyunand existing and incompatible zone already).
312*4882a593Smuzhiyun
313*4882a593SmuzhiyunAfter this, memory block XXX's state will be 'online' and the amount of
314*4882a593Smuzhiyunavailable memory will be increased.
315*4882a593Smuzhiyun
316*4882a593SmuzhiyunThis may be changed in future.
317*4882a593Smuzhiyun
318*4882a593SmuzhiyunLogical memory remove
319*4882a593Smuzhiyun=====================
320*4882a593Smuzhiyun
321*4882a593SmuzhiyunMemory offline and ZONE_MOVABLE
322*4882a593Smuzhiyun-------------------------------
323*4882a593Smuzhiyun
324*4882a593SmuzhiyunMemory offlining is more complicated than memory online. Because memory offline
325*4882a593Smuzhiyunhas to make the whole memory block be unused, memory offline can fail if
326*4882a593Smuzhiyunthe memory block includes memory which cannot be freed.
327*4882a593Smuzhiyun
328*4882a593SmuzhiyunIn general, memory offline can use 2 techniques.
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun(1) reclaim and free all memory in the memory block.
331*4882a593Smuzhiyun(2) migrate all pages in the memory block.
332*4882a593Smuzhiyun
333*4882a593SmuzhiyunIn the current implementation, Linux's memory offline uses method (2), freeing
334*4882a593Smuzhiyunall  pages in the memory block by page migration. But not all pages are
335*4882a593Smuzhiyunmigratable. Under current Linux, migratable pages are anonymous pages and
336*4882a593Smuzhiyunpage caches. For offlining a memory block by migration, the kernel has to
337*4882a593Smuzhiyunguarantee that the memory block contains only migratable pages.
338*4882a593Smuzhiyun
339*4882a593SmuzhiyunNow, a boot option for making a memory block which consists of migratable pages
340*4882a593Smuzhiyunis supported. By specifying "kernelcore=" or "movablecore=" boot option, you can
341*4882a593Smuzhiyuncreate ZONE_MOVABLE...a zone which is just used for movable pages.
342*4882a593Smuzhiyun(See also Documentation/admin-guide/kernel-parameters.rst)
343*4882a593Smuzhiyun
344*4882a593SmuzhiyunAssume the system has "TOTAL" amount of memory at boot time, this boot option
345*4882a593Smuzhiyuncreates ZONE_MOVABLE as following.
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun1) When kernelcore=YYYY boot option is used,
348*4882a593Smuzhiyun   Size of memory not for movable pages (not for offline) is YYYY.
349*4882a593Smuzhiyun   Size of memory for movable pages (for offline) is TOTAL-YYYY.
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun2) When movablecore=ZZZZ boot option is used,
352*4882a593Smuzhiyun   Size of memory not for movable pages (not for offline) is TOTAL - ZZZZ.
353*4882a593Smuzhiyun   Size of memory for movable pages (for offline) is ZZZZ.
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun.. note::
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun   Unfortunately, there is no information to show which memory block belongs
358*4882a593Smuzhiyun   to ZONE_MOVABLE. This is TBD.
359*4882a593Smuzhiyun
360*4882a593Smuzhiyun.. _memory_hotplug_how_to_offline_memory:
361*4882a593Smuzhiyun
362*4882a593SmuzhiyunHow to offline memory
363*4882a593Smuzhiyun---------------------
364*4882a593Smuzhiyun
365*4882a593SmuzhiyunYou can offline a memory block by using the same sysfs interface that was used
366*4882a593Smuzhiyunin memory onlining::
367*4882a593Smuzhiyun
368*4882a593Smuzhiyun	% echo offline > /sys/devices/system/memory/memoryXXX/state
369*4882a593Smuzhiyun
370*4882a593SmuzhiyunIf offline succeeds, the state of the memory block is changed to be "offline".
371*4882a593SmuzhiyunIf it fails, some error core (like -EBUSY) will be returned by the kernel.
372*4882a593SmuzhiyunEven if a memory block does not belong to ZONE_MOVABLE, you can try to offline
373*4882a593Smuzhiyunit.  If it doesn't contain 'unmovable' memory, you'll get success.
374*4882a593Smuzhiyun
375*4882a593SmuzhiyunA memory block under ZONE_MOVABLE is considered to be able to be offlined
376*4882a593Smuzhiyuneasily.  But under some busy state, it may return -EBUSY. Even if a memory
377*4882a593Smuzhiyunblock cannot be offlined due to -EBUSY, you can retry offlining it and may be
378*4882a593Smuzhiyunable to offline it (or not). (For example, a page is referred to by some kernel
379*4882a593Smuzhiyuninternal call and released soon.)
380*4882a593Smuzhiyun
381*4882a593SmuzhiyunConsideration:
382*4882a593Smuzhiyun  Memory hotplug's design direction is to make the possibility of memory
383*4882a593Smuzhiyun  offlining higher and to guarantee unplugging memory under any situation. But
384*4882a593Smuzhiyun  it needs more work. Returning -EBUSY under some situation may be good because
385*4882a593Smuzhiyun  the user can decide to retry more or not by himself. Currently, memory
386*4882a593Smuzhiyun  offlining code does some amount of retry with 120 seconds timeout.
387*4882a593Smuzhiyun
388*4882a593SmuzhiyunPhysical memory remove
389*4882a593Smuzhiyun======================
390*4882a593Smuzhiyun
391*4882a593SmuzhiyunNeed more implementation yet....
392*4882a593Smuzhiyun - Notification completion of remove works by OS to firmware.
393*4882a593Smuzhiyun - Guard from remove if not yet.
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun
396*4882a593SmuzhiyunLocking Internals
397*4882a593Smuzhiyun=================
398*4882a593Smuzhiyun
399*4882a593SmuzhiyunWhen adding/removing memory that uses memory block devices (i.e. ordinary RAM),
400*4882a593Smuzhiyunthe device_hotplug_lock should be held to:
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun- synchronize against online/offline requests (e.g. via sysfs). This way, memory
403*4882a593Smuzhiyun  block devices can only be accessed (.online/.state attributes) by user
404*4882a593Smuzhiyun  space once memory has been fully added. And when removing memory, we
405*4882a593Smuzhiyun  know nobody is in critical sections.
406*4882a593Smuzhiyun- synchronize against CPU hotplug and similar (e.g. relevant for ACPI and PPC)
407*4882a593Smuzhiyun
408*4882a593SmuzhiyunEspecially, there is a possible lock inversion that is avoided using
409*4882a593Smuzhiyundevice_hotplug_lock when adding memory and user space tries to online that
410*4882a593Smuzhiyunmemory faster than expected:
411*4882a593Smuzhiyun
412*4882a593Smuzhiyun- device_online() will first take the device_lock(), followed by
413*4882a593Smuzhiyun  mem_hotplug_lock
414*4882a593Smuzhiyun- add_memory_resource() will first take the mem_hotplug_lock, followed by
415*4882a593Smuzhiyun  the device_lock() (while creating the devices, during bus_add_device()).
416*4882a593Smuzhiyun
417*4882a593SmuzhiyunAs the device is visible to user space before taking the device_lock(), this
418*4882a593Smuzhiyuncan result in a lock inversion.
419*4882a593Smuzhiyun
420*4882a593Smuzhiyunonlining/offlining of memory should be done via device_online()/
421*4882a593Smuzhiyundevice_offline() - to make sure it is properly synchronized to actions
422*4882a593Smuzhiyunvia sysfs. Holding device_hotplug_lock is advised (to e.g. protect online_type)
423*4882a593Smuzhiyun
424*4882a593SmuzhiyunWhen adding/removing/onlining/offlining memory or adding/removing
425*4882a593Smuzhiyunheterogeneous/device memory, we should always hold the mem_hotplug_lock in
426*4882a593Smuzhiyunwrite mode to serialise memory hotplug (e.g. access to global/zone
427*4882a593Smuzhiyunvariables).
428*4882a593Smuzhiyun
429*4882a593SmuzhiyunIn addition, mem_hotplug_lock (in contrast to device_hotplug_lock) in read
430*4882a593Smuzhiyunmode allows for a quite efficient get_online_mems/put_online_mems
431*4882a593Smuzhiyunimplementation, so code accessing memory can protect from that memory
432*4882a593Smuzhiyunvanishing.
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun
435*4882a593SmuzhiyunFuture Work
436*4882a593Smuzhiyun===========
437*4882a593Smuzhiyun
438*4882a593Smuzhiyun  - allowing memory hot-add to ZONE_MOVABLE. maybe we need some switch like
439*4882a593Smuzhiyun    sysctl or new control file.
440*4882a593Smuzhiyun  - showing memory block and physical device relationship.
441*4882a593Smuzhiyun  - test and make it better memory offlining.
442*4882a593Smuzhiyun  - support HugeTLB page migration and offlining.
443*4882a593Smuzhiyun  - memmap removing at memory offline.
444*4882a593Smuzhiyun  - physical remove memory.
445