本文将介绍在QEMU KVM 环境下,memory hot(un)plug的使用。

1. Kernel Configuration

To use the memory hotplug feature, CONFIG_ACPI_HOTPLUG_MEMORY should be selected.

1
2
$ cat /boot/config-5.4.0-92-generic | grep CONFIG_ACPI_HOTPLUG_MEMORY
CONFIG_ACPI_HOTPLUG_MEMORY=y

2. QEMU launch script setting

In order to be able to hotplug memory, QEMU has to be told how many hotpluggable memory slots to create and what is the maximum amount of memory the guest can grow. This is done at startup time by means of the -m command-line option, which has the following format:

1
-m [size=]megs[,slots=n,maxmem=size]

Where,

  • “megs” is the startup RAM. It is the RAM the guest will boot with
  • “slots” is the number of hotpluggable memory slots
  • “maxmem” is the maximum RAM size the guest can have

For example, the following command-line:

1
qemu [...] -m 1G,slots=3,maxmem=4G

Creates a guest with 1GB of memory and three hotpluggable memory slots. The hotpluggable memory slots are empty when the guest is booted, so all memory the guest will see after boot is 1GB. The maximum memory the guest can reach is 4GB. This means that three additional gigabytes can be hotplugged by using any combination of the available memory slots.

3. Hotplug

Before running device_add, run the following command lines.

1
2
3
4
5
6
7
8
9
10
11
12
$ cat /proc/meminfo | head -2
MemTotal: 727172 kB
MemFree: 59700 kB
$ ls /sys/devices/system/memory/ | grep memory
memory0
memory1
memory2
memory3
memory4
memory5
memory6
memory7

1
2
(qemu) object_add memory-backend-ram,id=mem1,size=1G
(qemu) device_add pc-dimm,id=dimm1,memdev=mem1

After running device_add, run the following command lines.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$ cat /proc/meminfo | head -2
MemTotal: 1785560 kB
MemFree: 1115004 kB
$ ls /sys/devices/system/memory/ | grep memory
memory0
memory1
memory2
memory3
memory32
memory33
memory34
memory35
memory36
memory37
memory38
memory39
memory4
memory5
memory6
memory7


$ cat /sys/devices/system/memory/memory33/state
online

4. Hotunplug

You may need to add movable_node in guest kernel command line firstly!

In order to be able to hot unplug pc-dimm device, QEMU has to be told the ids of pc-dimm device and memory backend object. The ids were assigned when you hot plugged memory.

Two monitor commands are used to hot unplug memory:

1
2
- "device_del": deletes a front-end pc-dimm device
- "object_del": deletes a memory backend object

For example, assuming that the pc-dimm device with id “dimm1” exists, and its memory backend is “mem1”, the following commands tries to remove it.

1
2
(qemu) device_del dimm1
(qemu) object_del mem1

After running device_del, run the following command lines.

1
2
3
4
5
6
7
8
9
10
11
12
$ cat /proc/meminfo | head -2
MemTotal: 736984 kB
MemFree: 87368 kB
$ ls /sys/devices/system/memory/ | grep memory
memory0
memory1
memory2
memory3
memory4
memory5
memory6
memory7


参考资料:

  1. QEMU memory hotplug
  2. kernel doc Memory Hot(Un)Plug
  3. Memory Hot-unplug fails to remove DIMM