virtio-mem: paravirtualized memory
Subsystems (11)
v4 cover virtio-mem: paravirtualized memory
This series is based on latest linux-next. The patches are located at:
https://github.com/davidhildenbrand/linux.git virtio-mem-rfc-v4
The basic idea of virtio-mem is to provide a flexible, cross-architecture memory hot(un)plug solution that avoids many limitations imposed by existing technologies, architectures, and interfaces. More details can be found below and in linked material.
This RFC is limited to x86-64, however, should theoretically work on any architecture that supports virtio and implements memory hot(un)plug under Linux - like s390x, powerpc64 and arm64. On x86-64, it is currently possible to add/remove memory to the system in >= 4MB granularity. Memory hotplug works very reliably. For memory unplug, there are no guarantees how much memory can actually get unplugged, it depends on the setup (especially: fragmentation of (unmovable) memory). I have plans to improve that in the future.
--------------------------------------------------------------------------
1. virtio-mem
--------------------------------------------------------------------------
The basic idea behind virtio-mem was presented at KVM Forum 2018. The slides can be found at [1]. The previous RFC can be found at [2]. The first RFC can be found at [3]. However, the concept evolved over time. The KVM Forum slides roughly match the current design.
Patch #2 ("virtio-mem: Paravirtualized memory hotplug") contains quite some information, especially in "include/uapi/linux/virtio_mem.h":
Each virtio-mem device manages a dedicated region in physical address space. Each device can belong to a single NUMA node, multiple devices for a single NUMA node are possible. A virtio-mem device is like a "resizable DIMM" consisting of small memory blocks that can be plugged or unplugged. The device driver is responsible for (un)plugging memory blocks on demand.
Virtio-mem devices can only operate on their assigned memory region in order to (un)plug memory. A device cannot (un)plug memory belonging to other devices.
The "region_size" corresponds to the maximum amount of memory that can be provided by a device. The "size" corresponds to the amount of memory that is currently plugged. "requested_size" corresponds to a request from the device to the device driver to (un)plug blocks. The device driver should try to (un)plug blocks in order to reach the "requested_size". It is impossible to plug more memory than requested.
The "usable_region_size" represents the memory region that can actually be used to (un)plug memory. It is always at least as big as the "requested_size" and will grow dynamically. It will only shrink when explicitly triggered (VIRTIO_MEM_REQ_UNPLUG).
Memory in the usable region can usually be read, however, there are no guarantees. It can happen that the device cannot process a request, because it is busy. The device driver has to retry later.
Usually, during system resets all memory will get unplugged, so the device driver can start with a clean state. However, in specific scenarios (if the device is busy) it can happen that the device still has memory plugged. The device driver can request to unplug all memory (VIRTIO_MEM_REQ_UNPLUG) - which might take a while to succeed if the device is busy.
--------------------------------------------------------------------------
2. Linux Implementation
--------------------------------------------------------------------------
This RFC reuses quite some existing MM infrastructure, however, has to expose some additional functionality.
Memory blocks (e.g., 128MB) are added/removed on demand. Within these memory blocks, subblocks (e.g., 4MB) are plugged/unplugged. The sizes depend on the target architecture, MAX_ORDER + pageblock_order, and the block size of a virtio-mem device.
add_memory()/try_remove_memory() is used to add/remove memory blocks. virtio-mem will not online memory blocks itself. This has to be done by user space, or configured into the kernel (CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE). virtio-mem will only unplug memory that was online to the ZONE_NORMAL. Memory is suggested to be onlined to the ZONE_NORMAL for now.
The memory hotplug notifier is used to properly synchronize against onlining/offlining of memory blocks and to track the states of memory blocks (including the zone memory blocks are onlined to).
The set_online_page() callback is used to keep unplugged subblocks of a memory block fake-offline when onlining the memory block. generic_online_page() is used to fake-online plugged subblocks. This handling is similar to the Hyper-V balloon driver.
PG_offline is used to mark unplugged subblocks as offline, so e.g., dumping tools (makedumpfile) will skip these pages. This is similar to other balloon drivers like virtio-balloon and Hyper-V.
Memory offlining code is extended to allow drivers to drop their reference to PG_offline pages when MEM_GOING_OFFLINE, so these pages can be skipped when offlining memory blocks. This allows to offline memory blocks that have partially unplugged (allocated e.g., via alloc_contig_range()) subblocks - or are completely unplugged.
alloc_contig_range()/free_contig_range() [now exposed] is used to unplug/plug subblocks of memory blocks the are already exposed to Linux.
offline_and_remove_memory() [new] is used to offline a fully unplugged memory block and remove it from Linux.
A lot of additional information can be found in the separate patches and as comments in the code itself.
--------------------------------------------------------------------------
3. Changes RFC v2 -> v3
--------------------------------------------------------------------------
A lot of things changed, especially also on the QEMU + virtio side. The biggest changes on the Linux driver side are:
- Onlining/offlining of subblocks is now emulated on top of memory blocks. set_online_page()+alloc_contig_range()+free_contig_range() is now used for that. Core MM does not have to be modified and will continue to online/offline full memory blocks.
- Onlining/offlining of memory blocks is no longer performed by virtio-mem.
- Pg_offline is upstream and can be used. It is also used to allow offlining of partially unplugged memory blocks.
- Memory block states + subblocks are now tracked more space-efficient.
- Proper kexec(), kdump(), driver unload, driver reload, ZONE_MOVABLE, ... handling.
--------------------------------------------------------------------------
4. Changes RFC v3 -> RFC v4
--------------------------------------------------------------------------
Only minor things changed, especially nothing on the QEMU + virtio side. Interresting changes on the Linux driver side are:
- "mm: Allow to offline unmovable PageOffline() pages via MEM_GOING_OFFLINE"
-- Rework to Michals suggestion (allow to isolate all PageOffline() pages
by skipping all PageOffline() pages in has_unmovable_pages(). Fail offlining later if the pages cannot be offlined/migrated).
- "virtio-mem: Allow to offline partially unplugged memory blocks" -- Adapt to Michals suggestion on core-mm part.
- "virtio-mem: Better retry handling"
-- Optimize retry intervals
- "virtio-mem: Drop slab objects when unplug continues to fail" -- Call drop_slab()/drop_slab_node() when unplug keeps failing for a longer time.
- Multiple cleanups and fixes.
--------------------------------------------------------------------------
5. Future work
--------------------------------------------------------------------------
The separate patches contain a lot of future work items. One of the next steps is to make memory unplug more likely to succeed - currently, there are no guarantees on how much memory can get unplugged again. I have various ideas on how to limit fragmentation of all memory blocks that virtio-mem added.
Memory hotplug:
- Reduce the amount of memory resources if that turnes out to be an issue. Or try to speed up relevant code paths to deal with many resources.
- Allocate the vmemmap from the added memory. Makes hotplug more likely to succeed, the vmemmap is stored on the same NUMA node and that unmovable memory will later not hinder unplug.
Memory hotunplug:
- Performance improvements:
-- Sense (lockless) if it make sense to try alloc_contig_range() at all
before directly trying to isolate and taking locks.
-- Try to unplug bigger chunks if possible first.
-- Identify free areas first, that don't have to be evacuated.
- Make unplug more likely to succeed:
-- There are various idea to limit fragmentation on memory block
granularity. (e.g., ZONE_PREFER_MOVABLE and smart balancing)
-- Allocate memmap from added memory. This way, less unmovable data can
end up on the memory blocks.
- OOM handling, e.g., via an OOM handler.
- Defragmentation
-- Will require a new virtio-mem CMD to exchange plugged<->unplugged blocks
--------------------------------------------------------------------------
6. Example Usage
--------------------------------------------------------------------------
A very basic QEMU prototype (kept updated) is available at:
https://github.com/davidhildenbrand/qemu.git virtio-mem
It lacks various features, however, works to test the guest driver side:
- No support for resizable memory regions / memory backends yet
- No protection of unplugged memory (esp., userfaultfd-wp) yet
- No dump/migration/XXX optimizations to skip unplugged memory (and avoid touching it)
Start QEMU with two virtio-mem devices (one per NUMA node):
$ qemu-system-x86_64 -m 4G,maxmem=20G \ -smp sockets=2,cores=2 \ -numa node,nodeid=0,cpus=0-1 -numa node,nodeid=1,cpus=2-3 \ [...] -object memory-backend-ram,id=mem0,size=8G \ -device virtio-mem-pci,id=vm0,memdev=mem0,node=0,requested-size=128M \ -object memory-backend-ram,id=mem1,size=8G \ -device virtio-mem-pci,id=vm1,memdev=mem1,node=1,requested-size=80M
Query the configuration:
QEMU 4.1.95 monitor - type 'help' for more information (qemu) info memory-devices
Memory device [virtio-mem]: "vm0"
memaddr: 0x140000000
node: 0
requested-size: 134217728
size: 134217728
max-size: 8589934592
block-size: 2097152
memdev: /objects/mem0
Memory device [virtio-mem]: "vm1"
memaddr: 0x340000000
node: 1
requested-size: 83886080
size: 83886080
max-size: 8589934592
block-size: 2097152
memdev: /objects/mem1
Add some memory to node 1:
QEMU 4.1.95 monitor - type 'help' for more information (qemu) qom-set vm1 requested-size 1G
Remove some memory from node 0:
QEMU 4.1.95 monitor - type 'help' for more information (qemu) qom-set vm0 requested-size 64M
Query the configuration again:
(qemu) info memory-devices
Memory device [virtio-mem]: "vm0"
memaddr: 0x140000000
node: 0
requested-size: 67108864
size: 67108864
max-size: 8589934592
block-size: 2097152
memdev: /objects/mem0
Memory device [virtio-mem]: "vm1"
memaddr: 0x340000000
node: 1
requested-size: 1073741824
size: 1073741824
max-size: 8589934592
block-size: 2097152
memdev: /objects/mem1
--------------------------------------------------------------------------
7. Q/A
--------------------------------------------------------------------------
Q: Why add/remove parts ("subblocks") of memory blocks/sections?
A: Flexibility (section size depends on the architecture) - e.g., some
architectures have a section size of 2GB. Also, the memory block size is variable (e.g., on x86-64). I want to avoid any such restrictions. Some use cases want to add/remove memory in smaller granularities to a VM (e.g., the Hyper-V balloon also implements this) - especially smaller VMs like used for kata containers. Also, on memory unplug, it is more reliable to free-up and unplug multiple small chunks instead of one big chunk. E.g., if one page of a DIMM is either unmovable or pinned, the DIMM can't get unplugged. This approach is basically a compromise between DIMM-based memory hot(un)plug and balloon inflation/deflation, which works mostly on page granularity.
Q: Why care about memory blocks?
A: They are the way to tell user space about new memory. This way,
memory can get onlined/offlined by user space. Also, e.g., kdump relies on udev events to reload kexec when memory blocks are onlined/offlined. Memory blocks are the "real" memory hot(un)plug granularity. Everything that's smaller has to be emulated "on top".
Q: Won't memory unplug of subblocks fragment memory?
A: Yes and no. Unplugging e.g., >=4MB subblocks on x86-64 will not really
fragment memory like unplugging random pages like a balloon driver does. Buddy merging will not be limited. However, any allocation that requires bigger consecutive memory chunks (e.g., gigantic pages) might observe the fragmentation. Possible solutions: Allocate gigantic huge pages before unplugging memory, don't unplug memory, combine virtio-mem with DIMM based memory or bigger initial memory. Remember, a virtio-mem device will only unplug on the memory range it manages, not on other DIMMs. Unplug of single memory blocks will result in similar fragmentation in respect to gigantic huge pages. I ahve plans for a virtio-mem defragmentation feature in the future.
Q: How reliable is memory unplug?
A: There are no guarantees on how much memory can get unplugged
again. However, it is more likely to find 4MB chunks to unplug than e.g., 128MB chunks. If memory is terribly fragmented, there is nothing we can do - for now. I consider memory hotplug the first primary use of virtio-mem. Memory unplug might usually work, but we want to improve the performance and the amount of memory we can actually unplug later.
Q: Why not unplug from the ZONE_MOVABLE?
A: Unplugged memory chunks are unmovable. Unmovable data must not end up
on the ZONE_MOVABLE - similar to gigantic pages - they will never be allocated from ZONE_MOVABLE. virtio-mem added memory can be onlined to the ZONE_MOVABLE, but subblocks will not get unplugged from it.
Q: How big should the initial (!virtio-mem) memory of a VM be?
A: virtio-mem memory will not go to the DMA zones. So to avoid running out
of DMA memory, I suggest something like 2-3GB on x86-64. But many VMs can most probably deal with less DMA memory - depends on the use case.
[1] https://events.linuxfoundation.org/wp-content/uploads/2017/12/virtio-mem-Paravirtualized-Memory-David-Hildenbrand-Red-Hat-1.pdf
[2] https://lkml.kernel.org/r/[email protected]
[3] https://lkml.kernel.org/r/[email protected]
David Hildenbrand (13):
ACPI: NUMA: export pxm_to_node
virtio-mem: Paravirtualized memory hotplug
virtio-mem: Paravirtualized memory hotunplug part 1
mm: Export alloc_contig_range() / free_contig_range()
virtio-mem: Paravirtualized memory hotunplug part 2
mm: Allow to offline unmovable PageOffline() pages via
MEM_GOING_OFFLINE
virtio-mem: Allow to offline partially unplugged memory blocks mm/memory_hotplug: Introduce offline_and_remove_memory()
virtio-mem: Offline and remove completely unplugged memory blocks
virtio-mem: Better retry handling
mm/vmscan: Move count_vm_event(DROP_SLAB) into drop_slab() mm/vmscan: Export drop_slab() and drop_slab_node()
virtio-mem: Drop slab objects when unplug continues to fail
drivers/acpi/numa/srat.c | 1 + drivers/virtio/Kconfig | 18 + drivers/virtio/Makefile | 1 + drivers/virtio/virtio_mem.c | 1939 +++++++++++++++++++++++++++++++ fs/drop_caches.c | 4 +- include/linux/memory_hotplug.h | 1 + include/linux/mm.h | 4 +- include/linux/page-flags.h | 10 + include/uapi/linux/virtio_ids.h | 1 + include/uapi/linux/virtio_mem.h | 204 ++++ mm/memory_hotplug.c | 76 +- mm/page_alloc.c | 26 + mm/page_isolation.c | 9 + mm/vmscan.c | 3 + 14 files changed, 2282 insertions(+), 15 deletions(-) create mode 100644 drivers/virtio/virtio_mem.c create mode 100644 include/uapi/linux/virtio_mem.h
Signature · 6 lines
2.23.0 --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
RFC PATCH v4 1/13 ACPI: NUMA: export pxm_to_node
Will be needed by virtio-mem to identify the node from a pxm.
--- drivers/acpi/numa/srat.c | 1 + 1 file changed, 1 insertion(+)
drivers/acpi/numa/srat.c +1 -0
@@ -35,6 +35,7 @@ int pxm_to_node(int pxm)return NUMA_NO_NODE ;return pxm_to_node_map[pxm]; } +EXPORT_SYMBOL (pxm_to_node);int node_to_pxm (int node) {
Signature · 1 line
2.23.0
RFC PATCH v4 1/13 ACPI: NUMA: export pxm_to_node
In reply to David HildenbrandWill be needed by virtio-mem to identify the node from a pxm.
--- drivers/acpi/numa/srat.c | 1 + 1 file changed, 1 insertion(+)
drivers/acpi/numa/srat.c +1 -0
@@ -35,6 +35,7 @@ int pxm_to_node(int pxm)return NUMA_NO_NODE ;return pxm_to_node_map[pxm]; } +EXPORT_SYMBOL (pxm_to_node);int node_to_pxm (int node) {
Signature · 6 lines
2.23.0 --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
Re: [PATCH RFC v4 01/13] ACPI: NUMA: export pxm_to_node
Quoted 16 lines · CET David Hildenbrand
Will be needed by virtio-mem to identify the node from a pxm.
Cc "Rafael J. Wysocki" <[email protected]>Cc Len Brown <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- drivers/acpi/numa/srat.c | 1 + 1 file changed, 1 insertion(+)drivers/acpi/numa/srat.c +0
-0
drivers/acpi/numa/srat.c int pxm_to_node(int pxm) · line 41
Quoted 7 lines
drivers/acpi/numa/srat.c +1
-0@@ -35,6 +35,7 @@ int pxm_to_node(int pxm)return NUMA_NO_NODE ;return pxm_to_node_map[pxm]; } +EXPORT_SYMBOL (pxm_to_node);int node_to_pxm (int node) {
This is fine by me FWIW.
Re: [PATCH RFC v4 01/13] ACPI: NUMA: export pxm_to_node
Quoted 21 lines · Rafael J. Wysocki
Quoted 20 lines · CET David Hildenbrand
Will be needed by virtio-mem to identify the node from a pxm.
Cc "Rafael J. Wysocki" <[email protected]>Cc Len Brown <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- drivers/acpi/numa/srat.c | 1 + 1 file changed, 1 insertion(+)drivers/acpi/numa/srat.c +1
-0@@ -35,6 +35,7 @@ int pxm_to_node(int pxm)return NUMA_NO_NODE ;return pxm_to_node_map[pxm]; } +EXPORT_SYMBOL (pxm_to_node);
Quoted 5 lines
drivers/acpi/numa/srat.c int pxm_to_node(int pxm) · line 41
Quoted 3 lines
drivers/acpi/numa/srat.c +0
-0int node_to_pxm (int node) {This is fine by me FWIW.
Can I count that as an Acked-by and carry it along? Thanks!
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH RFC v4 01/13] ACPI: NUMA: export pxm_to_node
In reply to Rafael J. WysockiQuoted 21 lines · Rafael J. Wysocki
Quoted 20 lines · CET David Hildenbrand
Will be needed by virtio-mem to identify the node from a pxm.
Cc "Rafael J. Wysocki" <[email protected]>Cc Len Brown <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- drivers/acpi/numa/srat.c | 1 + 1 file changed, 1 insertion(+)drivers/acpi/numa/srat.c +1
-0@@ -35,6 +35,7 @@ int pxm_to_node(int pxm)return NUMA_NO_NODE ;return pxm_to_node_map[pxm]; } +EXPORT_SYMBOL (pxm_to_node);
Quoted 5 lines
drivers/acpi/numa/srat.c int pxm_to_node(int pxm) · line 41
Quoted 3 lines
drivers/acpi/numa/srat.c +0
-0int node_to_pxm (int node) {This is fine by me FWIW.
Can I count that as an Acked-by and carry it along? Thanks!
Signature · 8 lines
Thanks, David / dhildenb --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
Rafael J. Wysocki
maintainerRe: [PATCH RFC v4 01/13] ACPI: NUMA: export pxm_to_node
Quoted 23 lines · David Hildenbrand
Quoted 21 lines · Rafael J. Wysocki
Quoted 20 lines · CET David Hildenbrand
Will be needed by virtio-mem to identify the node from a pxm.
Cc "Rafael J. Wysocki" <[email protected]>Cc Len Brown <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- drivers/acpi/numa/srat.c | 1 + 1 file changed, 1 insertion(+)drivers/acpi/numa/srat.c +1
-0@@ -35,6 +35,7 @@ int pxm_to_node(int pxm)return NUMA_NO_NODE ;return pxm_to_node_map[pxm]; } +EXPORT_SYMBOL (pxm_to_node);
Quoted 7 lines
Quoted 5 lines
drivers/acpi/numa/srat.c int pxm_to_node(int pxm) · line 41
Quoted 3 lines
drivers/acpi/numa/srat.c +0
-0int node_to_pxm (int node) {This is fine by me FWIW.
Can I count that as an Acked-by and carry it along? Thanks!
Yes, please.
Rafael J. Wysocki
maintainerRe: [PATCH RFC v4 01/13] ACPI: NUMA: export pxm_to_node
In reply to David HildenbrandQuoted 23 lines · David Hildenbrand
Quoted 21 lines · Rafael J. Wysocki
Quoted 20 lines · CET David Hildenbrand
Will be needed by virtio-mem to identify the node from a pxm.
Cc "Rafael J. Wysocki" <[email protected]>Cc Len Brown <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- drivers/acpi/numa/srat.c | 1 + 1 file changed, 1 insertion(+)drivers/acpi/numa/srat.c +1
-0@@ -35,6 +35,7 @@ int pxm_to_node(int pxm)return NUMA_NO_NODE ;return pxm_to_node_map[pxm]; } +EXPORT_SYMBOL (pxm_to_node);
Quoted 7 lines
Quoted 5 lines
drivers/acpi/numa/srat.c int pxm_to_node(int pxm) · line 41
Quoted 3 lines
drivers/acpi/numa/srat.c +0
-0int node_to_pxm (int node) {This is fine by me FWIW.
Can I count that as an Acked-by and carry it along? Thanks!
Yes, please.
RFC PATCH v4 2/13 virtio-mem: Paravirtualized memory hotplug
In reply to David HildenbrandEach virtio-mem device owns exactly one memory region. It is responsible for adding/removing memory from that memory region on request.
When the device driver starts up, the requested amount of memory is queried and then plugged to Linux. On request, further memory can be plugged or unplugged. This patch only implements the plugging part.
On x86-64, memory can currently be plugged in 4MB ("subblock") granularity. When required, a new memory block will be added (e.g., usually 128MB on x86-64) in order to plug more subblocks. Only x86-64 was tested for now.
The online_page callback is used to keep unplugged subblocks offline when onlining memory - similar to the Hyper-V balloon driver. Unplugged pages are marked PG_offline, to tell dump tools (e.g., makedumpfile) to skip them.
User space is usually responsible for onlining the added memory. The memory hotplug notifier is used to synchronize virtio-mem activity against memory onlining/offlining.
Each virtio-mem device can belong to a NUMA node, which allows us to easily add/remove small chunks of memory to/from a specific NUMA node by using multiple virtio-mem devices. Something that works even when the guest has no idea about the NUMA topology.
One way to view virtio-mem is as a "resizable DIMM" or a DIMM with many "sub-DIMMS".
This patch directly introduces the basic infrastructure to implement memory unplug. Especially the memory block states and subblock bitmaps will be heavily used there.
Notes:
- Memory blocks that are partally unplugged must not be onlined to the MOVABLE_ZONE. They contain unmovable parts and might e.g., result in warnings when trying to offline them.
- In the kdump kernel, we don't want to load the driver, to not mess with memory to be dumped.
- In case memory is to be onlined by user space, we limit the amount of offline memory blocks, to not run out of memory.
- Suspend/Hibernate is not supported due to the way virtio-mem devices behave. Limited support might be possible in the future.
- Reloading the device driver is not supported.
- When unloading the driver, we have to remove partially plugged offline memory blocks. Otherwise they could get fully onlined later on, when we no longer have control over via the online_page callback.
- As the hypervisor might suddenly be busy (during reboot, in-between requests, when adding of memory fails), we have to take care of some corner cases - especially virtio_mem_unplug_pending_mb() and virtio_mem_send_unplug_all_request(). The hypervisor could for example be busy if it is currently migrating the guest.
Future work:
- Reduce the amount of memory resources if that turnes out to be an issue. Or try to speed up relevant code paths to deal with many resources.
- Allocate the vmemmap from the added memory. Makes hotplug more likely to succeed, the vmemmap is stored on the same NUMA node and that unmovable memory will later not hinder unplug.
--- drivers/virtio/Kconfig | 17 + drivers/virtio/Makefile | 1 + drivers/virtio/virtio_mem.c | 1569 +++++++++++++++++++++++++++++++ include/uapi/linux/virtio_ids.h | 1 + include/uapi/linux/virtio_mem.h | 204 ++++ 5 files changed, 1792 insertions(+) create mode 100644 drivers/virtio/virtio_mem.c create mode 100644 include/uapi/linux/virtio_mem.h
drivers/virtio/Kconfig +17 -0
@@ -64,6 +64,23 @@ config VIRTIO_BALLOON If unsure, say M. +config VIRTIO_MEM + tristate "Virtio mem driver" + default m + depends on X86_64 + depends on VIRTIO + depends on MEMORY_HOTPLUG_SPARSE + depends on MEMORY_HOTREMOVE + help + This driver provides access to virtio-mem paravirtualized memory + devices, allowing to hotplug and hotunplug memory. + + This driver is was only tested under x86-64, but should + theoretically work on all architectures that support memory + hotplug and hotremove. + + If unsure, say M. + config VIRTIO_INPUT tristate "Virtio input driver" depends on VIRTIO
drivers/virtio/Makefile +1 -0
@@ -6,3 +6,4 @@ virtio_pci-y := virtio_pci_modern.o virtio_pci_common.o virtio_pci-$(CONFIG_VIRTIO_PCI_LEGACY) += virtio_pci_legacy.o obj-$(CONFIG_VIRTIO_BALLOON) += virtio_balloon.o obj-$(CONFIG_VIRTIO_INPUT) += virtio_input.o +obj-$(CONFIG_VIRTIO_MEM) += virtio_mem.o
drivers/virtio/virtio_mem.c new file +1569 -0
1,569 lines not on this page · raw message
include/uapi/linux/virtio_ids.h +1 -0
@@ -44,6 +44,7 @@#define VIRTIO_ID_VSOCK 19/* virtio vsock transport */ #define VIRTIO_ID_CRYPTO 20/* virtio crypto */ #define VIRTIO_ID_IOMMU 23/* virtio IOMMU */ +#define VIRTIO_ID_MEM 24/* virtio mem */ #define VIRTIO_ID_FS 26/* virtio filesystem */ #define VIRTIO_ID_PMEM 27/* virtio pmem */
include/uapi/linux/virtio_mem.h new file +204 -0
204 lines not on this page · raw message
Signature · 1 line
2.23.0
RFC PATCH v4 2/13 virtio-mem: Paravirtualized memory hotplug
In reply to David HildenbrandEach virtio-mem device owns exactly one memory region. It is responsible for adding/removing memory from that memory region on request.
When the device driver starts up, the requested amount of memory is queried and then plugged to Linux. On request, further memory can be plugged or unplugged. This patch only implements the plugging part.
On x86-64, memory can currently be plugged in 4MB ("subblock") granularity. When required, a new memory block will be added (e.g., usually 128MB on x86-64) in order to plug more subblocks. Only x86-64 was tested for now.
The online_page callback is used to keep unplugged subblocks offline when onlining memory - similar to the Hyper-V balloon driver. Unplugged pages are marked PG_offline, to tell dump tools (e.g., makedumpfile) to skip them.
User space is usually responsible for onlining the added memory. The memory hotplug notifier is used to synchronize virtio-mem activity against memory onlining/offlining.
Each virtio-mem device can belong to a NUMA node, which allows us to easily add/remove small chunks of memory to/from a specific NUMA node by using multiple virtio-mem devices. Something that works even when the guest has no idea about the NUMA topology.
One way to view virtio-mem is as a "resizable DIMM" or a DIMM with many "sub-DIMMS".
This patch directly introduces the basic infrastructure to implement memory unplug. Especially the memory block states and subblock bitmaps will be heavily used there.
Notes:
- Memory blocks that are partally unplugged must not be onlined to the MOVABLE_ZONE. They contain unmovable parts and might e.g., result in warnings when trying to offline them.
- In the kdump kernel, we don't want to load the driver, to not mess with memory to be dumped.
- In case memory is to be onlined by user space, we limit the amount of offline memory blocks, to not run out of memory.
- Suspend/Hibernate is not supported due to the way virtio-mem devices behave. Limited support might be possible in the future.
- Reloading the device driver is not supported.
- When unloading the driver, we have to remove partially plugged offline memory blocks. Otherwise they could get fully onlined later on, when we no longer have control over via the online_page callback.
- As the hypervisor might suddenly be busy (during reboot, in-between requests, when adding of memory fails), we have to take care of some corner cases - especially virtio_mem_unplug_pending_mb() and virtio_mem_send_unplug_all_request(). The hypervisor could for example be busy if it is currently migrating the guest.
Future work:
- Reduce the amount of memory resources if that turnes out to be an issue. Or try to speed up relevant code paths to deal with many resources.
- Allocate the vmemmap from the added memory. Makes hotplug more likely to succeed, the vmemmap is stored on the same NUMA node and that unmovable memory will later not hinder unplug.
--- drivers/virtio/Kconfig | 17 + drivers/virtio/Makefile | 1 + drivers/virtio/virtio_mem.c | 1569 +++++++++++++++++++++++++++++++ include/uapi/linux/virtio_ids.h | 1 + include/uapi/linux/virtio_mem.h | 204 ++++ 5 files changed, 1792 insertions(+) create mode 100644 drivers/virtio/virtio_mem.c create mode 100644 include/uapi/linux/virtio_mem.h
drivers/virtio/Kconfig +17 -0
@@ -64,6 +64,23 @@ config VIRTIO_BALLOON If unsure, say M. +config VIRTIO_MEM + tristate "Virtio mem driver" + default m + depends on X86_64 + depends on VIRTIO + depends on MEMORY_HOTPLUG_SPARSE + depends on MEMORY_HOTREMOVE + help + This driver provides access to virtio-mem paravirtualized memory + devices, allowing to hotplug and hotunplug memory. + + This driver is was only tested under x86-64, but should + theoretically work on all architectures that support memory + hotplug and hotremove. + + If unsure, say M. + config VIRTIO_INPUT tristate "Virtio input driver" depends on VIRTIO
drivers/virtio/Makefile +1 -0
@@ -6,3 +6,4 @@ virtio_pci-y := virtio_pci_modern.o virtio_pci_common.o virtio_pci-$(CONFIG_VIRTIO_PCI_LEGACY) += virtio_pci_legacy.o obj-$(CONFIG_VIRTIO_BALLOON) += virtio_balloon.o obj-$(CONFIG_VIRTIO_INPUT) += virtio_input.o +obj-$(CONFIG_VIRTIO_MEM) += virtio_mem.o
drivers/virtio/virtio_mem.c new file +1569 -0
1,569 lines not on this page · raw message
include/uapi/linux/virtio_ids.h +1 -0
@@ -44,6 +44,7 @@#define VIRTIO_ID_VSOCK 19/* virtio vsock transport */ #define VIRTIO_ID_CRYPTO 20/* virtio crypto */ #define VIRTIO_ID_IOMMU 23/* virtio IOMMU */ +#define VIRTIO_ID_MEM 24/* virtio mem */ #define VIRTIO_ID_FS 26/* virtio filesystem */ #define VIRTIO_ID_PMEM 27/* virtio pmem */
include/uapi/linux/virtio_mem.h new file +204 -0
204 lines not on this page · raw message
Signature · 6 lines
2.23.0 --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
RFC PATCH v4 3/13 virtio-mem: Paravirtualized memory hotunplug part 1
In reply to David HildenbrandUnplugging subblocks of memory blocks that are offline is easy. All we have to do is watch out for concurrent onlining acticity.
--- drivers/virtio/virtio_mem.c | 109 +++++++++++++++++++++++++++++++++++- 1 file changed, 107 insertions(+), 2 deletions(-)
drivers/virtio/virtio_mem.c +107 -2
@@ -119,7 +119,7 @@ struct virtio_mem {* * When this lock is held the pointers can't change, ONLINE and * OFFLINE blocks can't change the state and no subblocks will get -* plugged. +* plugged/unplugged. */ struct mutex hotplug_mutex;bool hotplug_active;
@@ -321,6 +321,19 @@ static bool virtio_mem_mb_test_sb_plugged(struct virtio_mem *vm, bit + count; } +/* +* Test if all selected subblocks are unplugged. +*/ +static bool virtio_mem_mb_test_sb_unplugged (struct virtio_mem *vm, +unsigned long mb_id,int sb_id, +int count) +{ +const int bit = (mb_id - vm->first_mb_id) * vm->nb_sb_per_mb + sb_id; + +/* TODO: Helper similar to bitmap_set() */ +return find_next_bit (vm->sb_bitmap, bit + count, bit) >= bit + count; +} +/* * Find the first plugged subblock. Returns vm->nb_sb_per_mb in case there is * none.
@@ -511,6 +524,9 @@ static void virtio_mem_notify_offline(struct virtio_mem *vm,BUG ();break ; } + +/* trigger the workqueue, maybe we can now unplug memory. */ +virtio_mem_retry (vm); } static void virtio_mem_notify_online(struct virtio_mem *vm, unsigned long mb_id,
@@ -1123,6 +1139,93 @@ static int virtio_mem_plug_request(struct virtio_mem *vm, uint64_t diff)return rc; } +/* +* Unplug the desired number of plugged subblocks of an offline memory block. +* Will fail if any subblock cannot get unplugged (instead of skipping it). +* +* Will modify the state of the memory block. Might temporarily drop the +* hotplug_mutex. +* +* Note: Can fail after some subblocks were successfully unplugged. +*/ +static int virtio_mem_mb_unplug_any_sb_offline (struct virtio_mem *vm, +unsigned long mb_id, +uint64_t *nb_sb) +{ +int rc; + + rc =virtio_mem_mb_unplug_any_sb (vm, mb_id, nb_sb); + +/* some subblocks might have been unplugged even on failure */ +if (!virtio_mem_mb_test_sb_plugged (vm, mb_id,0 , vm->nb_sb_per_mb)) +virtio_mem_mb_set_state (vm, mb_id, +VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL ); +if (rc) +return rc; + +if (virtio_mem_mb_test_sb_unplugged (vm, mb_id,0 , vm->nb_sb_per_mb)) { +/* +* Remove the block from Linux - this should never fail. +* Hinder the block from getting onlined by marking it +* unplugged. Temporarily drop the mutex, so +* any pending GOING_ONLINE requests can be serviced/rejected. +*/ +virtio_mem_mb_set_state (vm, mb_id, +VIRTIO_MEM_MB_STATE_UNUSED ); + +mutex_unlock (&vm->hotplug_mutex); + rc =virtio_mem_mb_remove (vm, mb_id); +BUG_ON (rc); +mutex_lock (&vm->hotplug_mutex); + } +return 0 ; +} + +/* +* Try to unplug the requested amount of memory. +*/ +static int virtio_mem_unplug_request (struct virtio_mem *vm,uint64_t diff) +{ +uint64_t nb_sb = diff / vm->subblock_size; +unsigned long mb_id; +int rc; + +if (!nb_sb) +return 0 ; + +/* +* We'll drop the mutex a couple of times when it is safe to do so. +* This might result in some blocks switching the state (online/offline) +* and we could miss them in this run - we will retry again later. +*/ +mutex_lock (&vm->hotplug_mutex); + +/* Try to unplug subblocks of partially plugged offline blocks. */ +virtio_mem_for_each_mb_state (vm, mb_id, +VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL ) { + rc =virtio_mem_mb_unplug_any_sb_offline (vm, mb_id, + &nb_sb); +if (rc || !nb_sb) + goto out_unlock; +cond_resched (); + } + +/* Try to unplug subblocks of plugged offline blocks. */ +virtio_mem_for_each_mb_state (vm, mb_id,VIRTIO_MEM_MB_STATE_OFFLINE ) { + rc =virtio_mem_mb_unplug_any_sb_offline (vm, mb_id, + &nb_sb); +if (rc || !nb_sb) + goto out_unlock; +cond_resched (); + } + +mutex_unlock (&vm->hotplug_mutex); +return 0 ; +out_unlock: +mutex_unlock (&vm->hotplug_mutex); +return rc; +} +/* * Try to unplug all blocks that couldn't be unplugged before, for example, * because the hypervisor was busy.
@@ -1203,8 +1306,10 @@ static void virtio_mem_run_wq(struct work_struct *work)if (vm->requested_size > vm->plugged_size) { diff = vm->requested_size - vm->plugged_size; rc =virtio_mem_plug_request (vm, diff); + }else { + diff = vm->plugged_size - vm->requested_size; + rc =virtio_mem_unplug_request (vm, diff); } -/* TODO: try to unplug memory */ }switch (rc) {
Signature · 1 line
2.23.0
RFC PATCH v4 3/13 virtio-mem: Paravirtualized memory hotunplug part 1
In reply to David HildenbrandUnplugging subblocks of memory blocks that are offline is easy. All we have to do is watch out for concurrent onlining acticity.
--- drivers/virtio/virtio_mem.c | 109 +++++++++++++++++++++++++++++++++++- 1 file changed, 107 insertions(+), 2 deletions(-)
drivers/virtio/virtio_mem.c +107 -2
@@ -119,7 +119,7 @@ struct virtio_mem {* * When this lock is held the pointers can't change, ONLINE and * OFFLINE blocks can't change the state and no subblocks will get -* plugged. +* plugged/unplugged. */ struct mutex hotplug_mutex;bool hotplug_active;
@@ -321,6 +321,19 @@ static bool virtio_mem_mb_test_sb_plugged(struct virtio_mem *vm, bit + count; } +/* +* Test if all selected subblocks are unplugged. +*/ +static bool virtio_mem_mb_test_sb_unplugged (struct virtio_mem *vm, +unsigned long mb_id,int sb_id, +int count) +{ +const int bit = (mb_id - vm->first_mb_id) * vm->nb_sb_per_mb + sb_id; + +/* TODO: Helper similar to bitmap_set() */ +return find_next_bit (vm->sb_bitmap, bit + count, bit) >= bit + count; +} +/* * Find the first plugged subblock. Returns vm->nb_sb_per_mb in case there is * none.
@@ -511,6 +524,9 @@ static void virtio_mem_notify_offline(struct virtio_mem *vm,BUG ();break ; } + +/* trigger the workqueue, maybe we can now unplug memory. */ +virtio_mem_retry (vm); } static void virtio_mem_notify_online(struct virtio_mem *vm, unsigned long mb_id,
@@ -1123,6 +1139,93 @@ static int virtio_mem_plug_request(struct virtio_mem *vm, uint64_t diff)return rc; } +/* +* Unplug the desired number of plugged subblocks of an offline memory block. +* Will fail if any subblock cannot get unplugged (instead of skipping it). +* +* Will modify the state of the memory block. Might temporarily drop the +* hotplug_mutex. +* +* Note: Can fail after some subblocks were successfully unplugged. +*/ +static int virtio_mem_mb_unplug_any_sb_offline (struct virtio_mem *vm, +unsigned long mb_id, +uint64_t *nb_sb) +{ +int rc; + + rc =virtio_mem_mb_unplug_any_sb (vm, mb_id, nb_sb); + +/* some subblocks might have been unplugged even on failure */ +if (!virtio_mem_mb_test_sb_plugged (vm, mb_id,0 , vm->nb_sb_per_mb)) +virtio_mem_mb_set_state (vm, mb_id, +VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL ); +if (rc) +return rc; + +if (virtio_mem_mb_test_sb_unplugged (vm, mb_id,0 , vm->nb_sb_per_mb)) { +/* +* Remove the block from Linux - this should never fail. +* Hinder the block from getting onlined by marking it +* unplugged. Temporarily drop the mutex, so +* any pending GOING_ONLINE requests can be serviced/rejected. +*/ +virtio_mem_mb_set_state (vm, mb_id, +VIRTIO_MEM_MB_STATE_UNUSED ); + +mutex_unlock (&vm->hotplug_mutex); + rc =virtio_mem_mb_remove (vm, mb_id); +BUG_ON (rc); +mutex_lock (&vm->hotplug_mutex); + } +return 0 ; +} + +/* +* Try to unplug the requested amount of memory. +*/ +static int virtio_mem_unplug_request (struct virtio_mem *vm,uint64_t diff) +{ +uint64_t nb_sb = diff / vm->subblock_size; +unsigned long mb_id; +int rc; + +if (!nb_sb) +return 0 ; + +/* +* We'll drop the mutex a couple of times when it is safe to do so. +* This might result in some blocks switching the state (online/offline) +* and we could miss them in this run - we will retry again later. +*/ +mutex_lock (&vm->hotplug_mutex); + +/* Try to unplug subblocks of partially plugged offline blocks. */ +virtio_mem_for_each_mb_state (vm, mb_id, +VIRTIO_MEM_MB_STATE_OFFLINE_PARTIAL ) { + rc =virtio_mem_mb_unplug_any_sb_offline (vm, mb_id, + &nb_sb); +if (rc || !nb_sb) + goto out_unlock; +cond_resched (); + } + +/* Try to unplug subblocks of plugged offline blocks. */ +virtio_mem_for_each_mb_state (vm, mb_id,VIRTIO_MEM_MB_STATE_OFFLINE ) { + rc =virtio_mem_mb_unplug_any_sb_offline (vm, mb_id, + &nb_sb); +if (rc || !nb_sb) + goto out_unlock; +cond_resched (); + } + +mutex_unlock (&vm->hotplug_mutex); +return 0 ; +out_unlock: +mutex_unlock (&vm->hotplug_mutex); +return rc; +} +/* * Try to unplug all blocks that couldn't be unplugged before, for example, * because the hypervisor was busy.
@@ -1203,8 +1306,10 @@ static void virtio_mem_run_wq(struct work_struct *work)if (vm->requested_size > vm->plugged_size) { diff = vm->requested_size - vm->plugged_size; rc =virtio_mem_plug_request (vm, diff); + }else { + diff = vm->plugged_size - vm->requested_size; + rc =virtio_mem_unplug_request (vm, diff); } -/* TODO: try to unplug memory */ }switch (rc) {
Signature · 1 line
2.23.0
RFC PATCH v4 4/13 mm: Export alloc_contig_range() / free_contig_range()
In reply to David HildenbrandA virtio-mem device wants to allocate memory from the memory region it manages in order to unplug it in the hypervisor - similar to a balloon driver. Also, it might want to plug previously unplugged (allocated) memory and give it back to Linux. alloc_contig_range() / free_contig_range() seem to be the perfect interface for this task.
In contrast to existing balloon devices, a virtio-mem device operates on bigger chunks (e.g., 4MB) and only on physical memory it manages. It tracks which chunks (subblocks) are still plugged, so it can go ahead and try to alloc_contig_range()+unplug them on unplug request, or plug+free_contig_range() unplugged chunks on plug requests.
A virtio-mem device will use alloc_contig_range() / free_contig_range() only on ranges that belong to the same node/zone in at least MAX(MAX_ORDER - 1, pageblock_order) order granularity - e.g., 4MB on x86-64. The virtio-mem device added that memory, so the memory exists and does not contain any holes. virtio-mem will only try to allocate on ZONE_NORMAL, never on ZONE_MOVABLE, just like when allocating gigantic pages (we don't put unmovable data into the movable zone).
--- mm/page_alloc.c | 2 ++ 1 file changed, 2 insertions(+)
mm/page_alloc.c +2 -0
@@ -8494,6 +8494,7 @@ int alloc_contig_range(unsigned long start, unsigned long end, pfn_max_align_up(end), migratetype);return ret; } +EXPORT_SYMBOL (alloc_contig_range);static int __alloc_contig_pages (unsigned long start_pfn,unsigned long nr_pages,gfp_t gfp_mask)
@@ -8609,6 +8610,7 @@ void free_contig_range(unsigned long pfn, unsigned int nr_pages) }WARN (count !=0 ,"%d pages are still in use!\n" , count); } +EXPORT_SYMBOL (free_contig_range);/* * The zone indicated has a new number of managed_pages; batch sizes and percpu
Signature · 1 line
2.23.0
RFC PATCH v4 4/13 mm: Export alloc_contig_range() / free_contig_range()
In reply to David HildenbrandA virtio-mem device wants to allocate memory from the memory region it manages in order to unplug it in the hypervisor - similar to a balloon driver. Also, it might want to plug previously unplugged (allocated) memory and give it back to Linux. alloc_contig_range() / free_contig_range() seem to be the perfect interface for this task.
In contrast to existing balloon devices, a virtio-mem device operates on bigger chunks (e.g., 4MB) and only on physical memory it manages. It tracks which chunks (subblocks) are still plugged, so it can go ahead and try to alloc_contig_range()+unplug them on unplug request, or plug+free_contig_range() unplugged chunks on plug requests.
A virtio-mem device will use alloc_contig_range() / free_contig_range() only on ranges that belong to the same node/zone in at least MAX(MAX_ORDER - 1, pageblock_order) order granularity - e.g., 4MB on x86-64. The virtio-mem device added that memory, so the memory exists and does not contain any holes. virtio-mem will only try to allocate on ZONE_NORMAL, never on ZONE_MOVABLE, just like when allocating gigantic pages (we don't put unmovable data into the movable zone).
--- mm/page_alloc.c | 2 ++ 1 file changed, 2 insertions(+)
mm/page_alloc.c +2 -0
@@ -8494,6 +8494,7 @@ int alloc_contig_range(unsigned long start, unsigned long end, pfn_max_align_up(end), migratetype);return ret; } +EXPORT_SYMBOL (alloc_contig_range);static int __alloc_contig_pages (unsigned long start_pfn,unsigned long nr_pages,gfp_t gfp_mask)
@@ -8609,6 +8610,7 @@ void free_contig_range(unsigned long pfn, unsigned int nr_pages) }WARN (count !=0 ,"%d pages are still in use!\n" , count); } +EXPORT_SYMBOL (free_contig_range);/* * The zone indicated has a new number of managed_pages; batch sizes and percpu
Signature · 1 line
2.23.0
RFC PATCH v4 5/13 virtio-mem: Paravirtualized memory hotunplug part 2
In reply to David HildenbrandWe can use alloc_contig_range() to try to unplug subblocks. Unplugged blocks will be marked PG_offline, however, don't have the PG_reserved flag set. This way, we can differentiate these allocated subblocks from subblocks that were never onlined and handle them properly in virtio_mem_fake_online(). free_contig_range() is used to hand back subblocks to Linux.
It is worth noting that there are no guarantess on how much memory can actually get unplugged again. All device memory might completely be fragmented with unmovable data, such that no subblock can get unplugged. We might want to improve the unplugging capability in the future.
We are not touching the ZONE_MOVABLE. If memory is onlined to the ZONE_MOVABLE, it can only get unplugged after that memory was offlined manually by user space. In normal operation, virtio-mem memory is suggested to be onlined to ZONE_NORMAL. In the future, we will try to make unplug more likely to succeed.
Add a module parameter to control if online memory shall be touched.
Future work:
- Performance improvements:
-- Sense (lockless) if it make sense to try alloc_contig_range() at all
before directly trying to isolate and taking locks.
-- Try to unplug bigger chunks if possible first.
-- Identify free areas first, that don't have to be evacuated.
- Make unplug more likely to succeed:
-- There are various idea to limit fragmentation on memory block
granularity. (e.g., ZONE_PREFER_MOVABLE and smart balancing)
-- Allocate memmap from added memory. This way, less unmovable data can
end up on the memory blocks.
- OOM handling, e.g., via an OOM handler.
- Defragmentation
-- Will require a new virtio-mem CMD to exchange plugged<->unplugged blocks
--- drivers/virtio/Kconfig | 1 + drivers/virtio/virtio_mem.c | 156 ++++++++++++++++++++++++++++++++---- 2 files changed, 143 insertions(+), 14 deletions(-)
drivers/virtio/Kconfig +1 -0
@@ -71,6 +71,7 @@ config VIRTIO_MEM depends on VIRTIO depends on MEMORY_HOTPLUG_SPARSE depends on MEMORY_HOTREMOVE + select CONTIG_ALLOC help This driver provides access to virtio-mem paravirtualized memory devices, allowing to hotplug and hotunplug memory.
drivers/virtio/virtio_mem.c +142 -14
209 lines not on this page · raw message
Signature · 1 line
2.23.0
RFC PATCH v4 5/13 virtio-mem: Paravirtualized memory hotunplug part 2
In reply to David HildenbrandWe can use alloc_contig_range() to try to unplug subblocks. Unplugged blocks will be marked PG_offline, however, don't have the PG_reserved flag set. This way, we can differentiate these allocated subblocks from subblocks that were never onlined and handle them properly in virtio_mem_fake_online(). free_contig_range() is used to hand back subblocks to Linux.
It is worth noting that there are no guarantess on how much memory can actually get unplugged again. All device memory might completely be fragmented with unmovable data, such that no subblock can get unplugged. We might want to improve the unplugging capability in the future.
We are not touching the ZONE_MOVABLE. If memory is onlined to the ZONE_MOVABLE, it can only get unplugged after that memory was offlined manually by user space. In normal operation, virtio-mem memory is suggested to be onlined to ZONE_NORMAL. In the future, we will try to make unplug more likely to succeed.
Add a module parameter to control if online memory shall be touched.
Future work:
- Performance improvements:
-- Sense (lockless) if it make sense to try alloc_contig_range() at all
before directly trying to isolate and taking locks.
-- Try to unplug bigger chunks if possible first.
-- Identify free areas first, that don't have to be evacuated.
- Make unplug more likely to succeed:
-- There are various idea to limit fragmentation on memory block
granularity. (e.g., ZONE_PREFER_MOVABLE and smart balancing)
-- Allocate memmap from added memory. This way, less unmovable data can
end up on the memory blocks.
- OOM handling, e.g., via an OOM handler.
- Defragmentation
-- Will require a new virtio-mem CMD to exchange plugged<->unplugged blocks
--- drivers/virtio/Kconfig | 1 + drivers/virtio/virtio_mem.c | 156 ++++++++++++++++++++++++++++++++---- 2 files changed, 143 insertions(+), 14 deletions(-)
drivers/virtio/Kconfig +1 -0
@@ -71,6 +71,7 @@ config VIRTIO_MEM depends on VIRTIO depends on MEMORY_HOTPLUG_SPARSE depends on MEMORY_HOTREMOVE + select CONTIG_ALLOC help This driver provides access to virtio-mem paravirtualized memory devices, allowing to hotplug and hotunplug memory.
drivers/virtio/virtio_mem.c +142 -14
209 lines not on this page · raw message
Signature · 6 lines
2.23.0 --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
RFC PATCH v4 6/13 mm: Allow to offline unmovable PageOffline() pages via MEM_GOING_OFFLINE
In reply to David Hildenbrandvirtio-mem wants to allow to offline memory blocks of which some parts were unplugged (allocated via alloc_contig_range()), especially, to later offline and remove completely unplugged memory blocks. The important part is that PageOffline() has to remain set until the section is offline, so these pages will never get accessed (e.g., when dumping). The pages should not be handed back to the buddy (which would require clearing PageOffline() and result in issues if offlining fails and the pages are suddenly in the buddy).
Let's allow to do that by allowing to isolate any PageOffline() page when offlining. This way, we can reach the memory hotplug notifier MEM_GOING_OFFLINE, where the driver can signal that he is fine with offlining this page by dropping its reference count. PageOffline() pages with a reference count of 0 can then be skipped when offlining the pages (like if they were free, however they are not in the buddy).
Anybody who uses PageOffline() pages and does not agree to offline them (e.g., Hyper-V balloon, XEN balloon, VMWare balloon for 2MB pages) will not decrement the reference count and make offlining fail when trying to migrate such an unmovable page. So there should be no observerable change. Same applies to balloon compaction users (movable PageOffline() pages), the pages will simply be migrated.
Note 1: If offlining fails, a driver has to increment the reference
count again in MEM_CANCEL_OFFLINE.
Note 2: A driver that makes use of this has to be aware that re-onlining
the memory block has to be handled by hooking into onlining code (online_page_callback_t), resetting the page PageOffline() and not giving them to the buddy.
--- include/linux/page-flags.h | 10 ++++++++++ mm/memory_hotplug.c | 41 ++++++++++++++++++++++++++++---------- mm/page_alloc.c | 24 ++++++++++++++++++++++ mm/page_isolation.c | 9 +++++++++ 4 files changed, 74 insertions(+), 10 deletions(-)
include/linux/page-flags.h +10 -0
@@ -761,6 +761,16 @@ PAGE_TYPE_OPS(Buddy, buddy)* not onlined when onlining the section). * The content of these pages is effectively stale. Such pages should not * be touched (read/write/dump/save) except by their owner. +* +* If a driver wants to allow to offline unmovable PageOffline() pages without +* putting them back to the buddy, it can do so via the memory notifier by +* decrementing the reference count in MEM_GOING_OFFLINE and incrementing the +* reference count in MEM_CANCEL_OFFLINE. When offlining, the PageOffline() +* pages (now with a reference count of zero) are treated like free pages, +* allowing the containing memory block to get offlined. A driver that +* relies on this feature is aware that re-onlining the memory block will +* require to re-set the pages PageOffline() and not giving them to the +* buddy via online_page_callback_t. */ PAGE_TYPE_OPS (Offline, offline)
mm/memory_hotplug.c +31 -10
81 lines not on this page · raw message
mm/page_alloc.c +24 -0
@@ -8256,6 +8256,19 @@ bool has_unmovable_pages(struct zone *zone, struct page *page, int count,if ((flags &MEMORY_OFFLINE ) &&PageHWPoison (page))continue ; +/* +* We treat all PageOffline() pages as movable when offlining +* to give drivers a chance to decrement their reference count +* in MEM_GOING_OFFLINE in order to signalize that these pages +* can be offlined as there are no direct references anymore. +* For actually unmovable PageOffline() where the driver does +* not support this, we will fail later when trying to actually +* move these pages that still have a reference count > 0. +* (false negatives in this function only) +*/ +if ((flags &MEMORY_OFFLINE ) &&PageOffline (page)) +continue ; +if (__PageMovable (page))continue ;
@@ -8683,6 +8696,17 @@ __offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn) offlined_pages++;continue ; } +/* +* At this point all remaining PageOffline() pages have a +* reference count of 0 and can simply be skipped. +*/ +if (PageOffline (page)) { +BUG_ON (page_count (page)); +BUG_ON (PageBuddy (page)); + pfn++; + offlined_pages++; +continue ; + }BUG_ON (page_count (page));BUG_ON (!PageBuddy (page));
mm/page_isolation.c +9 -0
@@ -170,6 +170,7 @@ __first_valid_page(unsigned long pfn, unsigned long nr_pages)* a bit mask) * MEMORY_OFFLINE - isolate to offline (!allocate) memory * e.g., skip over PageHWPoison() pages +* and PageOffline() pages. * REPORT_FAILURE - report details about the failure to * isolate the range *
@@ -278,6 +279,14 @@ __test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn,else if ((flags &MEMORY_OFFLINE ) &&PageHWPoison (page))/* A HWPoisoned page cannot be also PageBuddy */ pfn++; +else if ((flags &MEMORY_OFFLINE ) &&PageOffline (page) && + !page_count (page)) +/* +* The responsible driver agreed to offline +* PageOffline() pages by dropping its reference in +* MEM_GOING_OFFLINE. +*/ + pfn++;else break ; }
Signature · 1 line
2.23.0
RFC PATCH v4 6/13 mm: Allow to offline unmovable PageOffline() pages via MEM_GOING_OFFLINE
In reply to David Hildenbrandvirtio-mem wants to allow to offline memory blocks of which some parts were unplugged (allocated via alloc_contig_range()), especially, to later offline and remove completely unplugged memory blocks. The important part is that PageOffline() has to remain set until the section is offline, so these pages will never get accessed (e.g., when dumping). The pages should not be handed back to the buddy (which would require clearing PageOffline() and result in issues if offlining fails and the pages are suddenly in the buddy).
Let's allow to do that by allowing to isolate any PageOffline() page when offlining. This way, we can reach the memory hotplug notifier MEM_GOING_OFFLINE, where the driver can signal that he is fine with offlining this page by dropping its reference count. PageOffline() pages with a reference count of 0 can then be skipped when offlining the pages (like if they were free, however they are not in the buddy).
Anybody who uses PageOffline() pages and does not agree to offline them (e.g., Hyper-V balloon, XEN balloon, VMWare balloon for 2MB pages) will not decrement the reference count and make offlining fail when trying to migrate such an unmovable page. So there should be no observerable change. Same applies to balloon compaction users (movable PageOffline() pages), the pages will simply be migrated.
Note 1: If offlining fails, a driver has to increment the reference
count again in MEM_CANCEL_OFFLINE.
Note 2: A driver that makes use of this has to be aware that re-onlining
the memory block has to be handled by hooking into onlining code (online_page_callback_t), resetting the page PageOffline() and not giving them to the buddy.
--- include/linux/page-flags.h | 10 ++++++++++ mm/memory_hotplug.c | 41 ++++++++++++++++++++++++++++---------- mm/page_alloc.c | 24 ++++++++++++++++++++++ mm/page_isolation.c | 9 +++++++++ 4 files changed, 74 insertions(+), 10 deletions(-)
include/linux/page-flags.h +10 -0
@@ -761,6 +761,16 @@ PAGE_TYPE_OPS(Buddy, buddy)* not onlined when onlining the section). * The content of these pages is effectively stale. Such pages should not * be touched (read/write/dump/save) except by their owner. +* +* If a driver wants to allow to offline unmovable PageOffline() pages without +* putting them back to the buddy, it can do so via the memory notifier by +* decrementing the reference count in MEM_GOING_OFFLINE and incrementing the +* reference count in MEM_CANCEL_OFFLINE. When offlining, the PageOffline() +* pages (now with a reference count of zero) are treated like free pages, +* allowing the containing memory block to get offlined. A driver that +* relies on this feature is aware that re-onlining the memory block will +* require to re-set the pages PageOffline() and not giving them to the +* buddy via online_page_callback_t. */ PAGE_TYPE_OPS (Offline, offline)
mm/memory_hotplug.c +31 -10
81 lines not on this page · raw message
mm/page_alloc.c +24 -0
@@ -8256,6 +8256,19 @@ bool has_unmovable_pages(struct zone *zone, struct page *page, int count,if ((flags &MEMORY_OFFLINE ) &&PageHWPoison (page))continue ; +/* +* We treat all PageOffline() pages as movable when offlining +* to give drivers a chance to decrement their reference count +* in MEM_GOING_OFFLINE in order to signalize that these pages +* can be offlined as there are no direct references anymore. +* For actually unmovable PageOffline() where the driver does +* not support this, we will fail later when trying to actually +* move these pages that still have a reference count > 0. +* (false negatives in this function only) +*/ +if ((flags &MEMORY_OFFLINE ) &&PageOffline (page)) +continue ; +if (__PageMovable (page))continue ;
@@ -8683,6 +8696,17 @@ __offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn) offlined_pages++;continue ; } +/* +* At this point all remaining PageOffline() pages have a +* reference count of 0 and can simply be skipped. +*/ +if (PageOffline (page)) { +BUG_ON (page_count (page)); +BUG_ON (PageBuddy (page)); + pfn++; + offlined_pages++; +continue ; + }BUG_ON (page_count (page));BUG_ON (!PageBuddy (page));
mm/page_isolation.c +9 -0
@@ -170,6 +170,7 @@ __first_valid_page(unsigned long pfn, unsigned long nr_pages)* a bit mask) * MEMORY_OFFLINE - isolate to offline (!allocate) memory * e.g., skip over PageHWPoison() pages +* and PageOffline() pages. * REPORT_FAILURE - report details about the failure to * isolate the range *
@@ -278,6 +279,14 @@ __test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn,else if ((flags &MEMORY_OFFLINE ) &&PageHWPoison (page))/* A HWPoisoned page cannot be also PageBuddy */ pfn++; +else if ((flags &MEMORY_OFFLINE ) &&PageOffline (page) && + !page_count (page)) +/* +* The responsible driver agreed to offline +* PageOffline() pages by dropping its reference in +* MEM_GOING_OFFLINE. +*/ + pfn++;else break ; }
Signature · 6 lines
2.23.0 --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
Re: [PATCH RFC v4 06/13] mm: Allow to offline unmovable PageOffline() pages via MEM_GOING_OFFLINE
Quoted 107 lines · David Hildenbrand
virtio-mem wants to allow to offline memory blocks of which some parts were unplugged (allocated via
alloc_contig_range()), especially, to later offline and remove completely unplugged memory blocks. The important part is thatPageOffline()has to remain set until the section is offline, so these pages will never get accessed (e.g., when dumping). The pages should not be handed back to the buddy (which would require clearingPageOffline()and result in issues if offlining fails and the pages are suddenly in the buddy).Let's allow to do that by allowing to isolate any
PageOffline()page when offlining. This way, we can reach the memory hotplug notifierMEM_GOING_OFFLINE, where the driver can signal that he is fine with offlining this page by dropping its reference count.PageOffline()pages with a reference count of 0 can then be skipped when offlining the pages (like if they were free, however they are not in the buddy).Anybody who uses
PageOffline()pages and does not agree to offline them (e.g., Hyper-V balloon, XEN balloon, VMWare balloon for 2MB pages) will not decrement the reference count and make offlining fail when trying to migrate such an unmovable page. So there should be no observerable change. Same applies to balloon compaction users (movablePageOffline()pages), the pages will simply be migrated.Note 1: If offlining fails, a driver has to increment the reference
count again inMEM_CANCEL_OFFLINE.Note 2: A driver that makes use of this has to be aware that re-onlining
the memory block has to be handled by hooking into onlining code (online_page_callback_t), resetting the pagePageOffline()and not giving them to the buddy.Cc Andrew Morton <[email protected]>Cc Juergen Gross <[email protected]>Cc Konrad Rzeszutek Wilk <[email protected]>Cc Pavel Tatashin <[email protected]>Cc Alexander Duyck <[email protected]>Cc Vlastimil Babka <[email protected]>Cc Johannes Weiner <[email protected]>Cc Anthony Yznaga <[email protected]>Cc Michal Hocko <[email protected]>Cc Oscar Salvador <[email protected]>Cc Mel Gorman <[email protected]>Cc Mike Rapoport <[email protected]>Cc Dan Williams <[email protected]>Cc Anshuman Khandual <[email protected]>Cc Qian Cai <[email protected]>Cc Pingfan Liu <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- include/linux/page-flags.h | 10 ++++++++++ mm/memory_hotplug.c | 41 ++++++++++++++++++++++++++++---------- mm/page_alloc.c | 24 ++++++++++++++++++++++ mm/page_isolation.c | 9 +++++++++ 4 files changed, 74 insertions(+), 10 deletions(-)include/linux/page-flags.h +10
-0@@ -761,6 +761,16 @@ PAGE_TYPE_OPS(Buddy, buddy)* not onlined when onlining the section). * The content of these pages is effectively stale. Such pages should not * be touched (read/write/dump/save) except by their owner. +* +* If a driver wants to allow to offline unmovable PageOffline() pages without +* putting them back to the buddy, it can do so via the memory notifier by +* decrementing the reference count in MEM_GOING_OFFLINE and incrementing the +* reference count in MEM_CANCEL_OFFLINE. When offlining, the PageOffline() +* pages (now with a reference count of zero) are treated like free pages, +* allowing the containing memory block to get offlined. A driver that +* relies on this feature is aware that re-onlining the memory block will +* require to re-set the pages PageOffline() and not giving them to the +* buddy via online_page_callback_t. */ PAGE_TYPE_OPS (Offline, offline)mm/memory_hotplug.c +9
-5@@ -1231,11 +1231,15 @@ int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn,/* * Scan pfn range [start,end) to find movable/migratable pages (LRU pages, -* non-lru movable pages and hugepages). We scan pfn because it's much -* easier than scanning over linked list. This function returns the pfn -* of the first found movable page if it's found, otherwise 0. +* non-lru movable pages and hugepages). +* +* Returns: +* 0 in case a movable page is found and movable_pfn was updated. +* -ENOENT in case no movable page was found. +* -EBUSY in case a definetly unmovable page was found. */ -static unsigned long scan_movable_pages (unsigned long start,unsigned long end) +static int scan_movable_pages (unsigned long start,unsigned long end, +unsigned long *movable_pfn) {unsigned long pfn;@@ -1247,18 +1251,29 @@ static unsigned long scan_movable_pages(unsigned long start, unsigned long end)continue ; page =pfn_to_page (pfn);if (PageLRU (page)) -return pfn; + goto found;if (__PageMovable (page))
mm/memory_hotplug.c static unsigned long scan_movable_pages(unsigned long start, unsigned long end) · line 1264
Quoted 10 lines
mm/memory_hotplug.c +9
-1-return pfn; + goto found; + +/* +* Unmovable PageOffline() pages where somebody still holds +* a reference count (after MEM_GOING_OFFLINE) can definetly +* not be offlined. +*/ +if (PageOffline (page) &&page_count (page)) +return -EBUSY ;
So the comment confused me a bit because technically this function isn't about offlining memory, it is about finding movable pages. I had to do a bit of digging to find the only consumer is __offline_pages, but if we are going to talk about "offlining" instead of "moving" in this function it might make sense to rename it.
Quoted 5 lines
mm/memory_hotplug.c +0
-0if (!PageHuge (page))continue ; head =compound_head (page);if (page_huge_active (head))
mm/memory_hotplug.c static unsigned long scan_movable_pages(unsigned long start, unsigned long end) · line 1278
Quoted 10 lines
mm/memory_hotplug.c +4
-1-return pfn; + goto found; skip =compound_nr (head) - (page - head); pfn += skip -1 ; } +return -ENOENT ; +found: + *movable_pfn = pfn;return 0 ; }
So I am looking at this function and it seems like your change completely changes the behavior. The code before would walk the entire range and if at least 1 page was available to move you would return the PFN of that page. Now what seems to happen is that you will return -EBUSY as soon as you encounter an offline page with a page count. I would think that would slow down the offlining process since you have made the Unmovable PageOffline() page a head of line blocker that you have to wait to get around.
Would it perhaps make more sense to add a return value initialized to ENOENT, and if you encounter one of these offline pages you change the return value to EBUSY, and then if you walk through the entire list without finding a movable page you just return the value?
Otherwise you might want to add a comment explaining why the function should stall instead of skipping over the unmovable section that will hopefully become movable later.
Quoted 37 lines
mm/memory_hotplug.c +9
-3@@ -1528,7 +1543,8 @@ static int __ref __offline_pages(unsigned long start_pfn, } do { - for (pfn = start_pfn; pfn;) { + pfn = start_pfn; + do { if (signal_pending(current)) { ret = -EINTR; reason = "signal backoff";@@ -1538,14 +1554,19 @@ static int __ref __offline_pages(unsigned long start_pfn,cond_resched ();lru_add_drain_all (); - pfn =scan_movable_pages (pfn, end_pfn); -if (pfn) { + ret =scan_movable_pages (pfn, end_pfn, &pfn); +if (!ret) {/* * TODO: fatal migration failures should bail * out */ do_migrate_range (pfn, end_pfn); } + }while (!ret); + +if (ret != -ENOENT ) { + reason ="unmovable page" ; + goto failed_removal_isolated; }/* mm/page_alloc.c +0
-0
mm/page_alloc.c bool has_unmovable_pages(struct zone *zone, struct page *page, int count, · line 8262
Quoted 7 lines
mm/page_alloc.c +4
-0@@ -8256,6 +8256,19 @@ bool has_unmovable_pages(struct zone *zone, struct page *page, int count,if ((flags &MEMORY_OFFLINE ) &&PageHWPoison (page))continue ; +/* +* We treat all PageOffline() pages as movable when offlining +* to give drivers a chance to decrement their reference count +* in MEM_GOING_OFFLINE in order to signalize that these pages
You can probably just use "signal" or "indicate" instead of "signalize".
Quoted 57 lines
mm/page_alloc.c +20
-0+* can be offlined as there are no direct references anymore. +* For actually unmovable PageOffline() where the driver does +* not support this, we will fail later when trying to actually +* move these pages that still have a reference count > 0. +* (false negatives in this function only) +*/ +if ((flags &MEMORY_OFFLINE ) &&PageOffline (page)) +continue ; +if (__PageMovable (page))continue ;@@ -8683,6 +8696,17 @@ __offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn) offlined_pages++;continue ; } +/* +* At this point all remaining PageOffline() pages have a +* reference count of 0 and can simply be skipped. +*/ +if (PageOffline (page)) { +BUG_ON (page_count (page)); +BUG_ON (PageBuddy (page)); + pfn++; + offlined_pages++; +continue ; + }BUG_ON (page_count (page));BUG_ON (!PageBuddy (page));mm/page_isolation.c +9
-0@@ -170,6 +170,7 @@ __first_valid_page(unsigned long pfn, unsigned long nr_pages)* a bit mask) * MEMORY_OFFLINE - isolate to offline (!allocate) memory * e.g., skip over PageHWPoison() pages +* and PageOffline() pages. * REPORT_FAILURE - report details about the failure to * isolate the range * @@ -278,6 +279,14 @@ __test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn,else if ((flags &MEMORY_OFFLINE ) &&PageHWPoison (page))/* A HWPoisoned page cannot be also PageBuddy */ pfn++; +else if ((flags &MEMORY_OFFLINE ) &&PageOffline (page) && + !page_count (page)) +/* +* The responsible driver agreed to offline +* PageOffline() pages by dropping its reference in +* MEM_GOING_OFFLINE. +*/ + pfn++;else break ; }
Re: [PATCH RFC v4 06/13] mm: Allow to offline unmovable PageOffline() pages via MEM_GOING_OFFLINE
Quoted 33 lines
Quoted 33 lines
/*
* Scan pfn range [start,end) to find movable/migratable pages (LRU pages,
- * non-lru movable pages and hugepages). We scan pfn because it's much
- * easier than scanning over linked list. This function returns the pfn
- * of the first found movable page if it's found, otherwise 0.
+ * non-lru movable pages and hugepages).
+ *
+ * Returns:
+ * 0 in case a movable page is found andmovable_pfnwas updated.
+ *-ENOENTin case no movable page was found.
+ *-EBUSYin case a definetly unmovable page was found.
*/mm/memory_hotplug.c +10
-3-static unsigned long scan_movable_pages (unsigned long start,unsigned long end) +static int scan_movable_pages (unsigned long start,unsigned long end, +unsigned long *movable_pfn) {unsigned long pfn;@@ -1247,18 +1251,29 @@ static unsigned long scan_movable_pages(unsigned long start, unsigned long end)continue ; page =pfn_to_page (pfn);if (PageLRU (page)) -return pfn; + goto found;if (__PageMovable (page)) -return pfn; + goto found; + +/* +* Unmovable PageOffline() pages where somebody still holds +* a reference count (after MEM_GOING_OFFLINE) can definetly +* not be offlined. +*/
Quoted 8 lines
mm/memory_hotplug.c static unsigned long scan_movable_pages(unsigned long start, unsigned long end) · line 1264
Quoted 2 lines
mm/memory_hotplug.c +2
-0+if (PageOffline (page) &&page_count (page)) +return -EBUSY ;So the comment confused me a bit because technically this function isn't about offlining memory, it is about finding movable pages. I had to do a bit of digging to find the only consumer is
__offline_pages, but if we are going to talk about "offlining" instead of "moving" in this function it might make sense to rename it.
Well, it's contained in memory_hotplug.c, and the only user of moving pages around in there is offlining code :) And it's job is to locate movable pages, skip over some (temporary? unmovable ones) and (now) indicate definitely unmovable ones.
Any idea for a better name? scan_movable_pages_and_stop_on_definitely_unmovable() is not so nice :)
Quoted 17 lines
Quoted 6 lines
mm/memory_hotplug.c +0
-0if (!PageHuge (page))continue ; head =compound_head (page);if (page_huge_active (head))mm/memory_hotplug.c static unsigned long scan_movable_pages(unsigned long start, unsigned long end) · line 1278
Quoted 10 lines
mm/memory_hotplug.c +4
-1-return pfn; + goto found; skip =compound_nr (head) - (page - head); pfn += skip -1 ; } +return -ENOENT ; +found: + *movable_pfn = pfn;return 0 ; }
Quoted 8 lines
So I am looking at this function and it seems like your change completely changes the behavior. The code before would walk the entire range and if at least 1 page was available to move you would return the PFN of that page. Now what seems to happen is that you will return
-EBUSYas soon as you encounter an offline page with a page count. I would think that would slow down the offlining process since you have made the UnmovablePageOffline()page a head of line blocker that you have to wait to get around.
So, the comment says "Unmovable PageOffline() pages where somebody still holds a reference count (after MEM_GOING_OFFLINE) can definitely not be offlined". And the doc "-EBUSY in case a definitely unmovable page was found."
So why would this make offlining slow? Offlining will be aborted, because offlining is not possible.
Please note that this is the exact old behavior, where isolating the page range would have failed directly and offlining would have been aborted early. The old offlining failure in the case in the offlining path would have been "failure to isolate range".
Also, note that the users of PageOffline() with unmovable pages are very rare (only balloon drivers for now).
Quoted 5 lines
Would it perhaps make more sense to add a return value initialized to ENOENT, and if you encounter one of these offline pages you change the return value to EBUSY, and then if you walk through the entire list without finding a movable page you just return the value?
Did you have a look in which context this function is used, especially
[1] and [2]?
Quoted 4 lines
Otherwise you might want to add a comment explaining why the function should stall instead of skipping over the unmovable section that will hopefully become movable later.
So we have "-EBUSY in case a definitely unmovable page was found.". Do you have a better suggestion?
Quoted 15 lines
Quoted 15 lines
mm/memory_hotplug.c +2
-1@@ -1528,7 +1543,8 @@ static int __ref __offline_pages(unsigned long start_pfn, } do { - for (pfn = start_pfn; pfn;) { + pfn = start_pfn; + do { if (signal_pending(current)) { ret = -EINTR; reason = "signal backoff";@@ -1538,14 +1554,19 @@ static int __ref __offline_pages(unsigned long start_pfn,cond_resched ();lru_add_drain_all ();
Quoted 10 lines
mm/memory_hotplug.c static int __ref __offline_pages(unsigned long start_pfn, · line 1564
Quoted 10 lines
mm/memory_hotplug.c +2
-2- pfn =scan_movable_pages (pfn, end_pfn); -if (pfn) { + ret =scan_movable_pages (pfn, end_pfn, &pfn); +if (!ret) {/* * TODO: fatal migration failures should bail * out */ do_migrate_range (pfn, end_pfn); }
[1] we detect a definite offlining blocker and
Quoted 4 lines
mm/memory_hotplug.c static int __ref __offline_pages(unsigned long start_pfn, · line 1568
Quoted 4 lines
mm/memory_hotplug.c +4
-0+ }while (!ret); + +if (ret != -ENOENT ) { + reason ="unmovable page" ;
[2] we abort offlining
Quoted 12 lines
Quoted 12 lines
mm/memory_hotplug.c +1
-0+ goto failed_removal_isolated; }/* mm/page_alloc.c +0
-0@@ -8256,6 +8256,19 @@ bool has_unmovable_pages(struct zone *zone, struct page *page, int count,if ((flags &MEMORY_OFFLINE ) &&PageHWPoison (page))continue ;
Quoted 6 lines
mm/page_alloc.c bool has_unmovable_pages(struct zone *zone, struct page *page, int count, · line 8262
Quoted 4 lines
mm/page_alloc.c +4
-0+/* +* We treat all PageOffline() pages as movable when offlining +* to give drivers a chance to decrement their reference count +* in MEM_GOING_OFFLINE in order to signalize that these pages You can probably just use "signal" or "indicate" instead of "signalize".
Makes sense, "indicate" it is!
Thanks!
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH RFC v4 06/13] mm: Allow to offline unmovable PageOffline() pages via MEM_GOING_OFFLINE
Quoted 42 lines · David Hildenbrand
Quoted 33 lines
Quoted 33 lines
/*
* Scan pfn range [start,end) to find movable/migratable pages (LRU pages,
- * non-lru movable pages and hugepages). We scan pfn because it's much
- * easier than scanning over linked list. This function returns the pfn
- * of the first found movable page if it's found, otherwise 0.
+ * non-lru movable pages and hugepages).
+ *
+ * Returns:
+ * 0 in case a movable page is found andmovable_pfnwas updated.
+ *-ENOENTin case no movable page was found.
+ *-EBUSYin case a definetly unmovable page was found.
*/mm/memory_hotplug.c +10
-3-static unsigned long scan_movable_pages (unsigned long start,unsigned long end) +static int scan_movable_pages (unsigned long start,unsigned long end, +unsigned long *movable_pfn) {unsigned long pfn;@@ -1247,18 +1251,29 @@ static unsigned long scan_movable_pages(unsigned long start, unsigned long end)continue ; page =pfn_to_page (pfn);if (PageLRU (page)) -return pfn; + goto found;if (__PageMovable (page)) -return pfn; + goto found; + +/* +* Unmovable PageOffline() pages where somebody still holds +* a reference count (after MEM_GOING_OFFLINE) can definetly +* not be offlined. +*/ Quoted 8 lines
mm/memory_hotplug.c static unsigned long scan_movable_pages(unsigned long start, unsigned long end) · line 1264
Quoted 2 lines
mm/memory_hotplug.c +2
-0+if (PageOffline (page) &&page_count (page)) +return -EBUSY ;So the comment confused me a bit because technically this function isn't about offlining memory, it is about finding movable pages. I had to do a bit of digging to find the only consumer is
__offline_pages, but if we are going to talk about "offlining" instead of "moving" in this function it might make sense to rename it.
Quoted 7 lines
Well, it's contained in
memory_hotplug.c, and the only user of moving pages around in there is offlining code :) And it's job is to locate movable pages, skip over some (temporary? unmovable ones) and (now) indicate definitely unmovable ones.Any idea for a better name?
scan_movable_pages_and_stop_on_definitely_unmovable()is not so nice :)
I dunno. What I was getting at is that the wording here would make it clearer if you simply stated that these pages "can definately not be moved". Saying you cannot offline a page that is PageOffline seems kind of redundant, then again calling it an Unmovable and then saying it cannot be moves is also redundant I suppose. In the end you don't move them, but they can be switched to offline if the page count hits 0. When that happens you simply end up skipping over them in the code for __test_page_isolated_in_pageblock and __offline_isolated_pages.
Quoted 25 lines
Quoted 16 lines
Quoted 5 lines
mm/memory_hotplug.c +0
-0if (!PageHuge (page))continue ; head =compound_head (page);if (page_huge_active (head))mm/memory_hotplug.c static unsigned long scan_movable_pages(unsigned long start, unsigned long end) · line 1278
Quoted 10 lines
mm/memory_hotplug.c +4
-1-return pfn; + goto found; skip =compound_nr (head) - (page - head); pfn += skip -1 ; } +return -ENOENT ; +found: + *movable_pfn = pfn;return 0 ; }Quoted 8 lines
So I am looking at this function and it seems like your change completely changes the behavior. The code before would walk the entire range and if at least 1 page was available to move you would return the PFN of that page. Now what seems to happen is that you will return
-EBUSYas soon as you encounter an offline page with a page count. I would think that would slow down the offlining process since you have made the UnmovablePageOffline()page a head of line blocker that you have to wait to get around.
Quoted 7 lines
So, the comment says "Unmovable
PageOffline()pages where somebody still holds a reference count (afterMEM_GOING_OFFLINE) can definitely not be offlined". And the doc "-EBUSYin case a definitely unmovable page was found."So why would this make offlining slow? Offlining will be aborted, because offlining is not possible.
Okay, my reading of the code was a bit off. In my mind I was thinking you were effectively treating it almost like an EAGAIN and continuing the loop. I didn't catch the part where you had rewritten the for loop as a do-while in __offline_pages.
Quoted 57 lines
Please note that this is the exact old behavior, where isolating the page range would have failed directly and offlining would have been aborted early. The old offlining failure in the case in the offlining path would have been "failure to isolate range".
Also, note that the users of
PageOffline()with unmovable pages are very rare (only balloon drivers for now).Quoted 4 lines
Would it perhaps make more sense to add a return value initialized to ENOENT, and if you encounter one of these offline pages you change the return value to EBUSY, and then if you walk through the entire list without finding a movable page you just return the value?
Did you have a look in which context this function is used, especially
[1] and [2]?Quoted 3 lines
Otherwise you might want to add a comment explaining why the function should stall instead of skipping over the unmovable section that will hopefully become movable later.
So we have "
-EBUSYin case a definitely unmovable page was found.". Do you have a better suggestion?Quoted 14 lines
Quoted 14 lines
mm/memory_hotplug.c +2
-1@@ -1528,7 +1543,8 @@ static int __ref __offline_pages(unsigned long start_pfn, } do { - for (pfn = start_pfn; pfn;) { + pfn = start_pfn; + do { if (signal_pending(current)) { ret = -EINTR; reason = "signal backoff";@@ -1538,14 +1554,19 @@ static int __ref __offline_pages(unsigned long start_pfn,cond_resched ();lru_add_drain_all ();Quoted 10 lines
mm/memory_hotplug.c static int __ref __offline_pages(unsigned long start_pfn, · line 1564
Quoted 10 lines
mm/memory_hotplug.c +2
-2- pfn =scan_movable_pages (pfn, end_pfn); -if (pfn) { + ret =scan_movable_pages (pfn, end_pfn, &pfn); +if (!ret) {/* * TODO: fatal migration failures should bail * out */ do_migrate_range (pfn, end_pfn); }[1] we detect a definite offlining blocker and
Quoted 4 lines
mm/memory_hotplug.c static int __ref __offline_pages(unsigned long start_pfn, · line 1568
Quoted 4 lines
mm/memory_hotplug.c +4
-0+ }while (!ret); + +if (ret != -ENOENT ) { + reason ="unmovable page" ;[2] we abort offlining
Quoted 4 lines
Quoted 4 lines
mm/memory_hotplug.c static int __ref __offline_pages(unsigned long start_pfn, · line 1572
Quoted 4 lines
mm/memory_hotplug.c +1
-0+ goto failed_removal_isolated; }/*
Yeah, this is the piece I misread. I knew the loop this was in previously was looping when returning -ENOENT so for some reason I had it in my head that you were still looping on -EBUSY.
So the one question I would have is if at this point are we guaranteed that the balloon drivers have already taken care of the page count for all the pages they set to PageOffline? Based on the patch description I was thinking that this was going to be looping for a while waiting for the driver to clear the pages and then walking through them at the end of the loop via check_pages_isolated_cb.
Quoted 15 lines
Quoted 8 lines
Quoted 8 lines
mm/page_alloc.c +0
-0@@ -8256,6 +8256,19 @@ bool has_unmovable_pages(struct zone *zone, struct page *page, int count,if ((flags &MEMORY_OFFLINE ) &&PageHWPoison (page))continue ;Quoted 6 lines
mm/page_alloc.c bool has_unmovable_pages(struct zone *zone, struct page *page, int count, · line 8262
Quoted 4 lines
mm/page_alloc.c +4
-0+/* +* We treat all PageOffline() pages as movable when offlining +* to give drivers a chance to decrement their reference count +* in MEM_GOING_OFFLINE in order to signalize that these pages You can probably just use "signal" or "indicate" instead of "signalize".
Quoted 4 lines
Makes sense, "indicate" it is!
Thanks!
No problem.
Re: [PATCH RFC v4 06/13] mm: Allow to offline unmovable PageOffline() pages via MEM_GOING_OFFLINE
Quoted 48 lines · Alexander Duyck
Quoted 47 lines · David Hildenbrand
Quoted 33 lines
Quoted 33 lines
/*
* Scan pfn range [start,end) to find movable/migratable pages (LRU pages,
- * non-lru movable pages and hugepages). We scan pfn because it's much
- * easier than scanning over linked list. This function returns the pfn
- * of the first found movable page if it's found, otherwise 0.
+ * non-lru movable pages and hugepages).
+ *
+ * Returns:
+ * 0 in case a movable page is found andmovable_pfnwas updated.
+ *-ENOENTin case no movable page was found.
+ *-EBUSYin case a definetly unmovable page was found.
*/mm/memory_hotplug.c +10
-3-static unsigned long scan_movable_pages (unsigned long start,unsigned long end) +static int scan_movable_pages (unsigned long start,unsigned long end, +unsigned long *movable_pfn) {unsigned long pfn;@@ -1247,18 +1251,29 @@ static unsigned long scan_movable_pages(unsigned long start, unsigned long end)continue ; page =pfn_to_page (pfn);if (PageLRU (page)) -return pfn; + goto found;if (__PageMovable (page)) -return pfn; + goto found; + +/* +* Unmovable PageOffline() pages where somebody still holds +* a reference count (after MEM_GOING_OFFLINE) can definetly +* not be offlined. +*/ Quoted 8 lines
mm/memory_hotplug.c static unsigned long scan_movable_pages(unsigned long start, unsigned long end) · line 1264
Quoted 2 lines
mm/memory_hotplug.c +2
-0+if (PageOffline (page) &&page_count (page)) +return -EBUSY ;So the comment confused me a bit because technically this function isn't about offlining memory, it is about finding movable pages. I had to do a bit of digging to find the only consumer is
__offline_pages, but if we are going to talk about "offlining" instead of "moving" in this function it might make sense to rename it.Well, it's contained in
memory_hotplug.c, and the only user of moving pages around in there is offlining code :) And it's job is to locate movable pages, skip over some (temporary? unmovable ones) and (now) indicate definitely unmovable ones.
Quoted 8 lines
Quoted 2 lines
Any idea for a better name?
scan_movable_pages_and_stop_on_definitely_unmovable()is not so nice :)I dunno. What I was getting at is that the wording here would make it clearer if you simply stated that these pages "can definately not be moved". Saying you cannot offline a page that is PageOffline seems kind of redundant, then again calling it an Unmovable and then saying it cannot be moves is also redundant I suppose. In the end you don't move them, but
So, in summary, there are
- PageOffline() pages that are movable (balloon compaction).
- PageOffline() pages that cannot be moved and cannot be offlined (e.g., no balloon compaction enabled, XEN, HyperV, ...) . page_count(page) >= 0
- PageOffline() pages that cannot be moved, but can be offlined. page_count(page) == 0.
Quoted 3 lines
they can be switched to offline if the page count hits 0. When that happens you simply end up skipping over them in the code for
__test_page_isolated_in_pageblockand__offline_isolated_pages.
Yes. The thing with the wording is that pages with (PageOffline(page) && !page_count(page)) can also not really be moved, but they can be skipped when offlining. If we call that "moving them to /dev/null", then yes, they can be moved to some degree :)
I can certainly do here e.g.,
/*
* PageOffline() pages that are not marked __PageMovable() and have a
* reference count > 0 (after MEM_GOING_OFFLINE) are definitely
* unmovable. If their reference count would be 0, they could be skipped
* when offlining memory sections.
*/
And maybe I'll add to the function doc, that unmovable pages that are skipped in this function can include pages that can be skipped when offlining (moving them to nirvana).
Other suggestions?
[...]
Quoted 10 lines
Quoted 10 lines
[1] we detect a definite offlining blocker and
Quoted 4 lines
mm/memory_hotplug.c static int __ref __offline_pages(unsigned long start_pfn, · line 1568
Quoted 4 lines
mm/memory_hotplug.c +4
-0+ }while (!ret); + +if (ret != -ENOENT ) { + reason ="unmovable page" ;[2] we abort offlining
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
mm/memory_hotplug.c static int __ref __offline_pages(unsigned long start_pfn, · line 1572
Quoted 4 lines
mm/memory_hotplug.c +1
-0+ goto failed_removal_isolated; }/* Yeah, this is the piece I misread. I knew the loop this was in previously was looping when returning
-ENOENTso for some reason I had it in my head that you were still looping on-EBUSY.
Ah okay, I see. Yeah, that wouldn't make sense for the use case I have :)
Quoted 7 lines
So the one question I would have is if at this point are we guaranteed that the balloon drivers have already taken care of the page count for all the pages they set to PageOffline? Based on the patch description I was thinking that this was going to be looping for a while waiting for the driver to clear the pages and then walking through them at the end of the loop via
check_pages_isolated_cb.
So, e.g., the patch description states
"Let's allow to do that by allowing to isolate any PageOffline() page when offlining. This way, we can reach the memory hotplug notifier MEM_GOING_OFFLINE, where the driver can signal that he is fine with offlining this page by dropping its reference count."
Any balloon driver that does not allow offlining (e.g., XEN, HyperV, virtio-balloon), will always have a refcount of (at least) 1. Drivers that want to make use of that (esp. virtio-mem, but eventually also HyperV), will drop their refcount via the MEM_GOING_OFFLINE call.
So yes, at this point, all applicable users were notified via MEM_GOING_OFFLINE and had their chance to decrement the refcount. If they didn't, offlining will be aborted.
Thanks again!
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH RFC v4 06/13] mm: Allow to offline unmovable PageOffline() pages via MEM_GOING_OFFLINE
Quoted 85 lines · David Hildenbrand
Quoted 48 lines · Alexander Duyck
Quoted 47 lines · David Hildenbrand
Quoted 33 lines
Quoted 33 lines
/*
* Scan pfn range [start,end) to find movable/migratable pages (LRU pages,
- * non-lru movable pages and hugepages). We scan pfn because it's much
- * easier than scanning over linked list. This function returns the pfn
- * of the first found movable page if it's found, otherwise 0.
+ * non-lru movable pages and hugepages).
+ *
+ * Returns:
+ * 0 in case a movable page is found andmovable_pfnwas updated.
+ *-ENOENTin case no movable page was found.
+ *-EBUSYin case a definetly unmovable page was found.
*/mm/memory_hotplug.c +10
-3-static unsigned long scan_movable_pages (unsigned long start,unsigned long end) +static int scan_movable_pages (unsigned long start,unsigned long end, +unsigned long *movable_pfn) {unsigned long pfn;@@ -1247,18 +1251,29 @@ static unsigned long scan_movable_pages(unsigned long start, unsigned long end)continue ; page =pfn_to_page (pfn);if (PageLRU (page)) -return pfn; + goto found;if (__PageMovable (page)) -return pfn; + goto found; + +/* +* Unmovable PageOffline() pages where somebody still holds +* a reference count (after MEM_GOING_OFFLINE) can definetly +* not be offlined. +*/ Quoted 8 lines
mm/memory_hotplug.c static unsigned long scan_movable_pages(unsigned long start, unsigned long end) · line 1264
Quoted 2 lines
mm/memory_hotplug.c +2
-0+if (PageOffline (page) &&page_count (page)) +return -EBUSY ;So the comment confused me a bit because technically this function isn't about offlining memory, it is about finding movable pages. I had to do a bit of digging to find the only consumer is
__offline_pages, but if we are going to talk about "offlining" instead of "moving" in this function it might make sense to rename it.Well, it's contained in
memory_hotplug.c, and the only user of moving pages around in there is offlining code :) And it's job is to locate movable pages, skip over some (temporary? unmovable ones) and (now) indicate definitely unmovable ones.Quoted 8 lines
Quoted 2 lines
Any idea for a better name?
scan_movable_pages_and_stop_on_definitely_unmovable()is not so nice :)I dunno. What I was getting at is that the wording here would make it clearer if you simply stated that these pages "can definately not be moved". Saying you cannot offline a page that is PageOffline seems kind of redundant, then again calling it an Unmovable and then saying it cannot be moves is also redundant I suppose. In the end you don't move them, but
So, in summary, there are
-PageOffline()pages that are movable (balloon compaction).
-PageOffline()pages that cannot be moved and cannot be offlined (e.g., no balloon compaction enabled, XEN, HyperV, ...) .page_count(page)>= 0
-PageOffline()pages that cannot be moved, but can be offlined.page_count(page)== 0.Quoted 3 lines
they can be switched to offline if the page count hits 0. When that happens you simply end up skipping over them in the code for
__test_page_isolated_in_pageblockand__offline_isolated_pages.Yes. The thing with the wording is that pages with (PageOffline(page) && !page_count(page)) can also not really be moved, but they can be skipped when offlining. If we call that "moving them to
/dev/null", then yes, they can be moved to some degree :)I can certainly do here e.g.,
/*
*PageOffline()pages that are not marked__PageMovable()and have a
* reference count > 0 (afterMEM_GOING_OFFLINE) are definitely
* unmovable. If their reference count would be 0, they could be skipped
* when offlining memory sections.
*/
Quoted 5 lines
And maybe I'll add to the function doc, that unmovable pages that are skipped in this function can include pages that can be skipped when offlining (moving them to nirvana).
Other suggestions?
No, this sounds good and makes it much clearer.
Quoted 41 lines
[...]
Quoted 9 lines
Quoted 9 lines
[1] we detect a definite offlining blocker and
Quoted 4 lines
mm/memory_hotplug.c static int __ref __offline_pages(unsigned long start_pfn, · line 1568
Quoted 4 lines
mm/memory_hotplug.c +4
-0+ }while (!ret); + +if (ret != -ENOENT ) { + reason ="unmovable page" ;[2] we abort offlining
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
mm/memory_hotplug.c static int __ref __offline_pages(unsigned long start_pfn, · line 1572
Quoted 4 lines
mm/memory_hotplug.c +1
-0+ goto failed_removal_isolated; }/* Yeah, this is the piece I misread. I knew the loop this was in previously was looping when returning
-ENOENTso for some reason I had it in my head that you were still looping on-EBUSY.Ah okay, I see. Yeah, that wouldn't make sense for the use case I have :)
Quoted 6 lines
So the one question I would have is if at this point are we guaranteed that the balloon drivers have already taken care of the page count for all the pages they set to PageOffline? Based on the patch description I was thinking that this was going to be looping for a while waiting for the driver to clear the pages and then walking through them at the end of the loop via
check_pages_isolated_cb.So, e.g., the patch description states
"Let's allow to do that by allowing to isolate any
PageOffline()page when offlining. This way, we can reach the memory hotplug notifierMEM_GOING_OFFLINE, where the driver can signal that he is fine with offlining this page by dropping its reference count."Any balloon driver that does not allow offlining (e.g., XEN, HyperV, virtio-balloon), will always have a refcount of (at least) 1. Drivers that want to make use of that (esp. virtio-mem, but eventually also HyperV), will drop their refcount via the
MEM_GOING_OFFLINEcall.
Quoted 5 lines
So yes, at this point, all applicable users were notified via
MEM_GOING_OFFLINEand had their chance to decrement the refcount. If they didn't, offlining will be aborted.Thanks again!
Thank you as well. I'm still getting up to speed on the inner workings of much of this and so discussions such as this usually prove to be quite beneficial for me.
RFC PATCH v4 7/13 virtio-mem: Allow to offline partially unplugged memory blocks
In reply to David HildenbrandDropping the reference count of PageOffline() pages allows offlining code to skip them. However, we also have to convert PG_reserved to another flag - let's use PG_dirty - so has_unmovable_pages() will properly handle them. PG_reserved pages get detected as unmovable right away.
We need the flag to see if we are onlining pages the first time, or if we allocated them via alloc_contig_range().
Properly take care of offlining code also modifying the stats and special handling in case the driver gets unloaded.
--- drivers/virtio/virtio_mem.c | 64 ++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-)
drivers/virtio/virtio_mem.c +63 -1
@@ -564,6 +564,53 @@ static void virtio_mem_notify_online(struct virtio_mem *vm, unsigned long mb_id,virtio_mem_retry (vm); } +static void virtio_mem_notify_going_offline (struct virtio_mem *vm, +unsigned long mb_id) +{ +const unsigned long nr_pages =PFN_DOWN (vm->subblock_size); +unsigned long pfn; +int sb_id, i; + +for (sb_id =0 ; sb_id < vm->nb_sb_per_mb; sb_id++) { +if (virtio_mem_mb_test_sb_plugged (vm, mb_id, sb_id,1 )) +continue ; +/* +* Drop our reference to the pages so the memory can get +* offlined and add the unplugged pages to the managed +* page counters (so offlining code can correctly subtract +* them again). +*/ + pfn =PFN_DOWN (virtio_mem_mb_id_to_phys (mb_id) + + sb_id * vm->subblock_size); +adjust_managed_page_count (pfn_to_page (pfn), nr_pages); +for (i =0 ; i < nr_pages; i++) +page_ref_dec (pfn_to_page (pfn + i)); + } +} + +static void virtio_mem_notify_cancel_offline (struct virtio_mem *vm, +unsigned long mb_id) +{ +const unsigned long nr_pages =PFN_DOWN (vm->subblock_size); +unsigned long pfn; +int sb_id, i; + +for (sb_id =0 ; sb_id < vm->nb_sb_per_mb; sb_id++) { +if (virtio_mem_mb_test_sb_plugged (vm, mb_id, sb_id,1 )) +continue ; +/* +* Get the reference we dropped when going offline and +* subtract the unplugged pages from the managed page +* counters. +*/ + pfn =PFN_DOWN (virtio_mem_mb_id_to_phys (mb_id) + + sb_id * vm->subblock_size); +adjust_managed_page_count (pfn_to_page (pfn), -nr_pages); +for (i =0 ; i < nr_pages; i++) +page_ref_inc (pfn_to_page (pfn + i)); + } +} +/* * This callback will either be called synchonously from add_memory() or * asynchronously (e.g., triggered via user space). We have to be careful
@@ -611,6 +658,7 @@ static int virtio_mem_memory_notifier_cb(struct notifier_block *nb,break ;mutex_lock (&vm->hotplug_mutex); vm->hotplug_active = true; +virtio_mem_notify_going_offline (vm, mb_id);break ;case MEM_GOING_ONLINE :spin_lock_irq (&vm->removal_lock);
@@ -636,6 +684,12 @@ static int virtio_mem_memory_notifier_cb(struct notifier_block *nb,mutex_unlock (&vm->hotplug_mutex);break ;case MEM_CANCEL_OFFLINE : +if (!vm->hotplug_active) +break ; +virtio_mem_notify_cancel_offline (vm, mb_id); + vm->hotplug_active = false; +mutex_unlock (&vm->hotplug_mutex); +break ;case MEM_CANCEL_ONLINE :if (!vm->hotplug_active)break ;
@@ -660,8 +714,11 @@ static void virtio_mem_set_fake_offline(unsigned long pfn,struct page *page =pfn_to_page (pfn);__SetPageOffline (page); -if (!onlined) +if (!onlined) {SetPageDirty (page); +/* FIXME: remove after cleanups */ +ClearPageReserved (page); + } } }
@@ -1719,6 +1776,11 @@ static void virtio_mem_remove(struct virtio_device *vdev) rc =virtio_mem_mb_remove (vm, mb_id);BUG_ON (rc); } +/* +* After we unregistered our callbacks, user space can no longer +* offline partially plugged online memory blocks. No need to worry +* about them. +*/ /* unregister callbacks */ unregister_virtio_mem_device (vm);
Signature · 1 line
2.23.0
RFC PATCH v4 7/13 virtio-mem: Allow to offline partially unplugged memory blocks
In reply to David HildenbrandDropping the reference count of PageOffline() pages allows offlining code to skip them. However, we also have to convert PG_reserved to another flag - let's use PG_dirty - so has_unmovable_pages() will properly handle them. PG_reserved pages get detected as unmovable right away.
We need the flag to see if we are onlining pages the first time, or if we allocated them via alloc_contig_range().
Properly take care of offlining code also modifying the stats and special handling in case the driver gets unloaded.
--- drivers/virtio/virtio_mem.c | 64 ++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-)
drivers/virtio/virtio_mem.c +63 -1
@@ -564,6 +564,53 @@ static void virtio_mem_notify_online(struct virtio_mem *vm, unsigned long mb_id,virtio_mem_retry (vm); } +static void virtio_mem_notify_going_offline (struct virtio_mem *vm, +unsigned long mb_id) +{ +const unsigned long nr_pages =PFN_DOWN (vm->subblock_size); +unsigned long pfn; +int sb_id, i; + +for (sb_id =0 ; sb_id < vm->nb_sb_per_mb; sb_id++) { +if (virtio_mem_mb_test_sb_plugged (vm, mb_id, sb_id,1 )) +continue ; +/* +* Drop our reference to the pages so the memory can get +* offlined and add the unplugged pages to the managed +* page counters (so offlining code can correctly subtract +* them again). +*/ + pfn =PFN_DOWN (virtio_mem_mb_id_to_phys (mb_id) + + sb_id * vm->subblock_size); +adjust_managed_page_count (pfn_to_page (pfn), nr_pages); +for (i =0 ; i < nr_pages; i++) +page_ref_dec (pfn_to_page (pfn + i)); + } +} + +static void virtio_mem_notify_cancel_offline (struct virtio_mem *vm, +unsigned long mb_id) +{ +const unsigned long nr_pages =PFN_DOWN (vm->subblock_size); +unsigned long pfn; +int sb_id, i; + +for (sb_id =0 ; sb_id < vm->nb_sb_per_mb; sb_id++) { +if (virtio_mem_mb_test_sb_plugged (vm, mb_id, sb_id,1 )) +continue ; +/* +* Get the reference we dropped when going offline and +* subtract the unplugged pages from the managed page +* counters. +*/ + pfn =PFN_DOWN (virtio_mem_mb_id_to_phys (mb_id) + + sb_id * vm->subblock_size); +adjust_managed_page_count (pfn_to_page (pfn), -nr_pages); +for (i =0 ; i < nr_pages; i++) +page_ref_inc (pfn_to_page (pfn + i)); + } +} +/* * This callback will either be called synchonously from add_memory() or * asynchronously (e.g., triggered via user space). We have to be careful
@@ -611,6 +658,7 @@ static int virtio_mem_memory_notifier_cb(struct notifier_block *nb,break ;mutex_lock (&vm->hotplug_mutex); vm->hotplug_active = true; +virtio_mem_notify_going_offline (vm, mb_id);break ;case MEM_GOING_ONLINE :spin_lock_irq (&vm->removal_lock);
@@ -636,6 +684,12 @@ static int virtio_mem_memory_notifier_cb(struct notifier_block *nb,mutex_unlock (&vm->hotplug_mutex);break ;case MEM_CANCEL_OFFLINE : +if (!vm->hotplug_active) +break ; +virtio_mem_notify_cancel_offline (vm, mb_id); + vm->hotplug_active = false; +mutex_unlock (&vm->hotplug_mutex); +break ;case MEM_CANCEL_ONLINE :if (!vm->hotplug_active)break ;
@@ -660,8 +714,11 @@ static void virtio_mem_set_fake_offline(unsigned long pfn,struct page *page =pfn_to_page (pfn);__SetPageOffline (page); -if (!onlined) +if (!onlined) {SetPageDirty (page); +/* FIXME: remove after cleanups */ +ClearPageReserved (page); + } } }
@@ -1719,6 +1776,11 @@ static void virtio_mem_remove(struct virtio_device *vdev) rc =virtio_mem_mb_remove (vm, mb_id);BUG_ON (rc); } +/* +* After we unregistered our callbacks, user space can no longer +* offline partially plugged online memory blocks. No need to worry +* about them. +*/ /* unregister callbacks */ unregister_virtio_mem_device (vm);
Signature · 6 lines
2.23.0 --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
RFC PATCH v4 8/13 mm/memory_hotplug: Introduce offline_and_remove_memory()
In reply to David Hildenbrandvirtio-mem wants to offline and remove a memory block once it unplugged all subblocks (e.g., using alloc_contig_range()). Let's provide an interface to do that from a driver. virtio-mem already supports to offline partially unplugged memory blocks. Offlining a fully unplugged memory block will not require to migrate any pages. All unplugged subblocks are PageOffline() and have a reference count of 0 - so offlining code will simply skip them.
All we need an interface to trigger the "offlining" and the removing in a single operation - to make sure the memory block cannot get onlined by user space again before it gets removed.
To keep things simple, allow to only work on a single memory block.
--- include/linux/memory_hotplug.h | 1 + mm/memory_hotplug.c | 35 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+)
include/linux/memory_hotplug.h +1 -0
@@ -310,6 +310,7 @@ extern void try_offline_node(int nid);extern int offline_pages (unsigned long start_pfn,unsigned long nr_pages);extern int remove_memory (int nid,u64 start,u64 size);extern void __remove_memory (int nid,u64 start,u64 size); +extern int offline_and_remove_memory (int nid,u64 start,u64 size);#else static inline bool is_mem_section_removable(unsigned long pfn,
mm/memory_hotplug.c +35 -0
@@ -1825,4 +1825,39 @@ int remove_memory(int nid, u64 start, u64 size)return rc; }EXPORT_SYMBOL_GPL (remove_memory); + +/* +* Try to offline and remove a memory block. Might take a long time to +* finish in case memory is still in use. Primarily useful for memory devices +* that logically unplugged all memory (so it's no longer in use) and want to +* offline + remove the memory block. +*/ +int offline_and_remove_memory (int nid,u64 start,u64 size) +{ +struct memory_block *mem; +int rc = -EINVAL ; + +if (!IS_ALIGNED (start,memory_block_size_bytes ()) || + size !=memory_block_size_bytes ()) +return rc; + +lock_device_hotplug (); + mem =find_memory_block (__pfn_to_section (PFN_DOWN (start))); +if (mem) + rc =device_offline (&mem->dev); +/* Ignore if the device is already offline. */ +if (rc >0 ) + rc =0 ; + +/* +* In case we succeeded to offline the memory block, remove it. +* This cannot fail as it cannot get onlined in the meantime. +*/ +if (!rc &&try_remove_memory (nid, start, size)) +BUG (); +unlock_device_hotplug (); + +return rc; +} +EXPORT_SYMBOL_GPL (offline_and_remove_memory);#endif /* CONFIG_MEMORY_HOTREMOVE */
Signature · 1 line
2.23.0
RFC PATCH v4 8/13 mm/memory_hotplug: Introduce offline_and_remove_memory()
In reply to David Hildenbrandvirtio-mem wants to offline and remove a memory block once it unplugged all subblocks (e.g., using alloc_contig_range()). Let's provide an interface to do that from a driver. virtio-mem already supports to offline partially unplugged memory blocks. Offlining a fully unplugged memory block will not require to migrate any pages. All unplugged subblocks are PageOffline() and have a reference count of 0 - so offlining code will simply skip them.
All we need an interface to trigger the "offlining" and the removing in a single operation - to make sure the memory block cannot get onlined by user space again before it gets removed.
To keep things simple, allow to only work on a single memory block.
--- include/linux/memory_hotplug.h | 1 + mm/memory_hotplug.c | 35 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+)
include/linux/memory_hotplug.h +1 -0
@@ -310,6 +310,7 @@ extern void try_offline_node(int nid);extern int offline_pages (unsigned long start_pfn,unsigned long nr_pages);extern int remove_memory (int nid,u64 start,u64 size);extern void __remove_memory (int nid,u64 start,u64 size); +extern int offline_and_remove_memory (int nid,u64 start,u64 size);#else static inline bool is_mem_section_removable(unsigned long pfn,
mm/memory_hotplug.c +35 -0
@@ -1825,4 +1825,39 @@ int remove_memory(int nid, u64 start, u64 size)return rc; }EXPORT_SYMBOL_GPL (remove_memory); + +/* +* Try to offline and remove a memory block. Might take a long time to +* finish in case memory is still in use. Primarily useful for memory devices +* that logically unplugged all memory (so it's no longer in use) and want to +* offline + remove the memory block. +*/ +int offline_and_remove_memory (int nid,u64 start,u64 size) +{ +struct memory_block *mem; +int rc = -EINVAL ; + +if (!IS_ALIGNED (start,memory_block_size_bytes ()) || + size !=memory_block_size_bytes ()) +return rc; + +lock_device_hotplug (); + mem =find_memory_block (__pfn_to_section (PFN_DOWN (start))); +if (mem) + rc =device_offline (&mem->dev); +/* Ignore if the device is already offline. */ +if (rc >0 ) + rc =0 ; + +/* +* In case we succeeded to offline the memory block, remove it. +* This cannot fail as it cannot get onlined in the meantime. +*/ +if (!rc &&try_remove_memory (nid, start, size)) +BUG (); +unlock_device_hotplug (); + +return rc; +} +EXPORT_SYMBOL_GPL (offline_and_remove_memory);#endif /* CONFIG_MEMORY_HOTREMOVE */
Signature · 1 line
2.23.0
Michal Hocko
reviewerRe: [PATCH RFC v4 08/13] mm/memory_hotplug: Introduce offline_and_remove_memory()
Quoted 11 lines · David Hildenbrand
virtio-mem wants to offline and remove a memory block once it unplugged all subblocks (e.g., using
alloc_contig_range()). Let's provide an interface to do that from a driver. virtio-mem already supports to offline partially unplugged memory blocks. Offlining a fully unplugged memory block will not require to migrate any pages. All unplugged subblocks arePageOffline()and have a reference count of 0 - so offlining code will simply skip them.All we need an interface to trigger the "offlining" and the removing in a single operation - to make sure the memory block cannot get onlined by user space again before it gets removed.
Why does that matter? Is it really likely that the userspace would interfere? What would be the scenario?
Or is still mostly about not requiring callers to open code this general patter?
Quoted 74 lines
To keep things simple, allow to only work on a single memory block.
Cc Andrew Morton <[email protected]>Cc David Hildenbrand <[email protected]>Cc Oscar Salvador <[email protected]>Cc Michal Hocko <[email protected]>Cc Pavel Tatashin <[email protected]>Cc Wei Yang <[email protected]>Cc Dan Williams <[email protected]>Cc Qian Cai <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- include/linux/memory_hotplug.h | 1 + mm/memory_hotplug.c | 35 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+)include/linux/memory_hotplug.h +1
-0@@ -310,6 +310,7 @@ extern void try_offline_node(int nid);extern int offline_pages (unsigned long start_pfn,unsigned long nr_pages);extern int remove_memory (int nid,u64 start,u64 size);extern void __remove_memory (int nid,u64 start,u64 size); +extern int offline_and_remove_memory (int nid,u64 start,u64 size);#else static inline bool is_mem_section_removable(unsigned long pfn,mm/memory_hotplug.c +35
-0@@ -1825,4 +1825,39 @@ int remove_memory(int nid, u64 start, u64 size)return rc; }EXPORT_SYMBOL_GPL (remove_memory); + +/* +* Try to offline and remove a memory block. Might take a long time to +* finish in case memory is still in use. Primarily useful for memory devices +* that logically unplugged all memory (so it's no longer in use) and want to +* offline + remove the memory block. +*/ +int offline_and_remove_memory (int nid,u64 start,u64 size) +{ +struct memory_block *mem; +int rc = -EINVAL ; + +if (!IS_ALIGNED (start,memory_block_size_bytes ()) || + size !=memory_block_size_bytes ()) +return rc; + +lock_device_hotplug (); + mem =find_memory_block (__pfn_to_section (PFN_DOWN (start))); +if (mem) + rc =device_offline (&mem->dev); +/* Ignore if the device is already offline. */ +if (rc >0 ) + rc =0 ; + +/* +* In case we succeeded to offline the memory block, remove it. +* This cannot fail as it cannot get onlined in the meantime. +*/ +if (!rc &&try_remove_memory (nid, start, size)) +BUG (); +unlock_device_hotplug (); + +return rc; +} +EXPORT_SYMBOL_GPL (offline_and_remove_memory);#endif /* CONFIG_MEMORY_HOTREMOVE */ Signature · 1 line
2.23.0
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [PATCH RFC v4 08/13] mm/memory_hotplug: Introduce offline_and_remove_memory()
Quoted 9 lines · Michal Hocko
Quoted 8 lines · David Hildenbrand
virtio-mem wants to offline and remove a memory block once it unplugged all subblocks (e.g., using
alloc_contig_range()). Let's provide an interface to do that from a driver. virtio-mem already supports to offline partially unplugged memory blocks. Offlining a fully unplugged memory block will not require to migrate any pages. All unplugged subblocks arePageOffline()and have a reference count of 0 - so offlining code will simply skip them.
Quoted 6 lines
Quoted 3 lines
All we need an interface to trigger the "offlining" and the removing in a single operation - to make sure the memory block cannot get onlined by user space again before it gets removed.
Why does that matter? Is it really likely that the userspace would interfere? What would be the scenario?
I guess it's not that relevant after all (I think this comment dates back to the times where we didn't have try_remove_memory() and could actually BUG_ON() in remove_memory() if there would have been a race). Can drop that part.
Quoted 3 lines
Or is still mostly about not requiring callers to open code this general patter?
From kernel module context, I cannot get access to the actual memory block device (find_memory_block()) and call the device_unregister().
Especially, also the device hotplug lock is not exported. So this is a clean helper function to be used from kernel module context. (e.g., also hyper-v showed interest for using that)
Signature · 3 lines
Thanks, David / dhildenb
Michal Hocko
reviewerRe: [PATCH RFC v4 08/13] mm/memory_hotplug: Introduce offline_and_remove_memory()
Quoted 26 lines · David Hildenbrand
Quoted 9 lines · Michal Hocko
Quoted 8 lines · David Hildenbrand
virtio-mem wants to offline and remove a memory block once it unplugged all subblocks (e.g., using
alloc_contig_range()). Let's provide an interface to do that from a driver. virtio-mem already supports to offline partially unplugged memory blocks. Offlining a fully unplugged memory block will not require to migrate any pages. All unplugged subblocks arePageOffline()and have a reference count of 0 - so offlining code will simply skip them.Quoted 6 lines
Quoted 3 lines
All we need an interface to trigger the "offlining" and the removing in a single operation - to make sure the memory block cannot get onlined by user space again before it gets removed.
Why does that matter? Is it really likely that the userspace would interfere? What would be the scenario?
I guess it's not that relevant after all (I think this comment dates back to the times where we didn't have
try_remove_memory()and could actuallyBUG_ON()inremove_memory()if there would have been a race). Can drop that part.Quoted 3 lines
Or is still mostly about not requiring callers to open code this general patter?
Quoted 6 lines
From kernel module context, I cannot get access to the actual memory block device (
find_memory_block()) and call thedevice_unregister().Especially, also the device hotplug lock is not exported. So this is a clean helper function to be used from kernel module context. (e.g., also hyper-v showed interest for using that)
Fair enough.
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [PATCH RFC v4 08/13] mm/memory_hotplug: Introduce offline_and_remove_memory()
Quoted 27 lines · Michal Hocko
Quoted 26 lines · David Hildenbrand
Quoted 9 lines · Michal Hocko
Quoted 8 lines · David Hildenbrand
virtio-mem wants to offline and remove a memory block once it unplugged all subblocks (e.g., using
alloc_contig_range()). Let's provide an interface to do that from a driver. virtio-mem already supports to offline partially unplugged memory blocks. Offlining a fully unplugged memory block will not require to migrate any pages. All unplugged subblocks arePageOffline()and have a reference count of 0 - so offlining code will simply skip them.Quoted 6 lines
Quoted 3 lines
All we need an interface to trigger the "offlining" and the removing in a single operation - to make sure the memory block cannot get onlined by user space again before it gets removed.
Why does that matter? Is it really likely that the userspace would interfere? What would be the scenario?
I guess it's not that relevant after all (I think this comment dates back to the times where we didn't have
try_remove_memory()and could actuallyBUG_ON()inremove_memory()if there would have been a race). Can drop that part.Quoted 3 lines
Or is still mostly about not requiring callers to open code this general patter?
Quoted 9 lines
Quoted 6 lines
From kernel module context, I cannot get access to the actual memory block device (
find_memory_block()) and call thedevice_unregister().Especially, also the device hotplug lock is not exported. So this is a clean helper function to be used from kernel module context. (e.g., also hyper-v showed interest for using that)
Fair enough.
I'll send a v1 shortly, I rephrased the description to make this clear. Thanks!
Signature · 3 lines
Thanks, David / dhildenb
RFC PATCH v4 9/13 virtio-mem: Offline and remove completely unplugged memory blocks
In reply to David HildenbrandLet's offline+remove memory blocks once all subblocks are unplugged. We can use the new Linux MM interface for that. As no memory is in use anymore, this shouldn't take a long time and shouldn't fail. There might be corner cases where the offlining could still fail (especially, if another notifier NACKs the offlining request).
--- drivers/virtio/virtio_mem.c | 47 +++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-)
drivers/virtio/virtio_mem.c +43 -4
@@ -436,6 +436,28 @@ static int virtio_mem_mb_remove(struct virtio_mem *vm, unsigned long mb_id)return remove_memory (nid, addr,memory_block_size_bytes ()); } +/* +* Try to offline and remove a memory block from Linux. +* +* Must not be called with the vm->hotplug_mutex held (possible deadlock with +* onlining code). +* +* Will not modify the state of the memory block. +*/ +static int virtio_mem_mb_offline_and_remove (struct virtio_mem *vm, +unsigned long mb_id) +{ +const uint64_t addr =virtio_mem_mb_id_to_phys (mb_id); +int nid = vm->nid; + +if (nid ==NUMA_NO_NODE ) + nid =memory_add_physaddr_to_nid (addr); + +dev_dbg (&vm->vdev->dev,"offlining and removing memory block: %lu\n" , + mb_id); +return offline_and_remove_memory (nid, addr,memory_block_size_bytes ()); +} +/* * Trigger the workqueue so the device can perform its magic. */
@@ -529,7 +551,13 @@ static void virtio_mem_notify_offline(struct virtio_mem *vm,break ; } -/* trigger the workqueue, maybe we can now unplug memory. */ +/* +* Trigger the workqueue, maybe we can now unplug memory. Also, +* when we offline and remove a memory block, this will re-trigger +* us immediately - which is often nice because the removal of +* the memory block (e.g., memmap) might have freed up memory +* on other memory blocks we manage. +*/ virtio_mem_retry (vm); }
@@ -1275,7 +1303,8 @@ static int virtio_mem_mb_unplug_any_sb_offline(struct virtio_mem *vm,* Unplug the desired number of plugged subblocks of an online memory block. * Will skip subblock that are busy. * -* Will modify the state of the memory block. +* Will modify the state of the memory block. Might temporarily drop the +* hotplug_mutex. * * Note: Can fail after some subblocks were successfully unplugged. Can * return 0 even if subblocks were busy and could not get unplugged.
@@ -1331,9 +1360,19 @@ static int virtio_mem_mb_unplug_any_sb_online(struct virtio_mem *vm, }/* -* TODO: Once all subblocks of a memory block were unplugged, we want -* to offline the memory block and remove it. +* Once all subblocks of a memory block were unplugged, offline and +* remove it. This will usually not fail, as no memory is in use +* anymore - however some other notifiers might NACK the request. */ +if (virtio_mem_mb_test_sb_unplugged (vm, mb_id,0 , vm->nb_sb_per_mb)) { +mutex_unlock (&vm->hotplug_mutex); + rc =virtio_mem_mb_offline_and_remove (vm, mb_id); +mutex_lock (&vm->hotplug_mutex); +if (!rc) +virtio_mem_mb_set_state (vm, mb_id, +VIRTIO_MEM_MB_STATE_UNUSED ); + } +return 0 ; }
Signature · 1 line
2.23.0
RFC PATCH v4 9/13 virtio-mem: Offline and remove completely unplugged memory blocks
In reply to David HildenbrandLet's offline+remove memory blocks once all subblocks are unplugged. We can use the new Linux MM interface for that. As no memory is in use anymore, this shouldn't take a long time and shouldn't fail. There might be corner cases where the offlining could still fail (especially, if another notifier NACKs the offlining request).
--- drivers/virtio/virtio_mem.c | 47 +++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-)
drivers/virtio/virtio_mem.c +43 -4
@@ -436,6 +436,28 @@ static int virtio_mem_mb_remove(struct virtio_mem *vm, unsigned long mb_id)return remove_memory (nid, addr,memory_block_size_bytes ()); } +/* +* Try to offline and remove a memory block from Linux. +* +* Must not be called with the vm->hotplug_mutex held (possible deadlock with +* onlining code). +* +* Will not modify the state of the memory block. +*/ +static int virtio_mem_mb_offline_and_remove (struct virtio_mem *vm, +unsigned long mb_id) +{ +const uint64_t addr =virtio_mem_mb_id_to_phys (mb_id); +int nid = vm->nid; + +if (nid ==NUMA_NO_NODE ) + nid =memory_add_physaddr_to_nid (addr); + +dev_dbg (&vm->vdev->dev,"offlining and removing memory block: %lu\n" , + mb_id); +return offline_and_remove_memory (nid, addr,memory_block_size_bytes ()); +} +/* * Trigger the workqueue so the device can perform its magic. */
@@ -529,7 +551,13 @@ static void virtio_mem_notify_offline(struct virtio_mem *vm,break ; } -/* trigger the workqueue, maybe we can now unplug memory. */ +/* +* Trigger the workqueue, maybe we can now unplug memory. Also, +* when we offline and remove a memory block, this will re-trigger +* us immediately - which is often nice because the removal of +* the memory block (e.g., memmap) might have freed up memory +* on other memory blocks we manage. +*/ virtio_mem_retry (vm); }
@@ -1275,7 +1303,8 @@ static int virtio_mem_mb_unplug_any_sb_offline(struct virtio_mem *vm,* Unplug the desired number of plugged subblocks of an online memory block. * Will skip subblock that are busy. * -* Will modify the state of the memory block. +* Will modify the state of the memory block. Might temporarily drop the +* hotplug_mutex. * * Note: Can fail after some subblocks were successfully unplugged. Can * return 0 even if subblocks were busy and could not get unplugged.
@@ -1331,9 +1360,19 @@ static int virtio_mem_mb_unplug_any_sb_online(struct virtio_mem *vm, }/* -* TODO: Once all subblocks of a memory block were unplugged, we want -* to offline the memory block and remove it. +* Once all subblocks of a memory block were unplugged, offline and +* remove it. This will usually not fail, as no memory is in use +* anymore - however some other notifiers might NACK the request. */ +if (virtio_mem_mb_test_sb_unplugged (vm, mb_id,0 , vm->nb_sb_per_mb)) { +mutex_unlock (&vm->hotplug_mutex); + rc =virtio_mem_mb_offline_and_remove (vm, mb_id); +mutex_lock (&vm->hotplug_mutex); +if (!rc) +virtio_mem_mb_set_state (vm, mb_id, +VIRTIO_MEM_MB_STATE_UNUSED ); + } +return 0 ; }
Signature · 6 lines
2.23.0 --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
RFC PATCH v4 10/13 virtio-mem: Better retry handling
In reply to David HildenbrandLet's start with a retry interval of 30 seconds and double the time until we reach 30 minutes, in case we keep getting errors. Reset the retry interval in case we succeeded.
--- drivers/virtio/virtio_mem.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/virtio/virtio_mem.c +9 -3
@@ -137,7 +137,9 @@ struct virtio_mem {/* Timer for retrying to plug/unplug memory. */ struct hrtimer retry_timer; -#define VIRTIO_MEM_RETRY_TIMER_MS 30000 +unsigned int retry_timer_ms; +#define VIRTIO_MEM_RETRY_TIMER_MIN_MS 30000 +#define VIRTIO_MEM_RETRY_TIMER_MAX_MS 1800000/* Memory notifier (online/offline events). */ struct notifier_block memory_notifier;
@@ -1537,6 +1539,7 @@ static void virtio_mem_run_wq(struct work_struct *work)switch (rc) {case 0 : + vm->retry_timer_ms =VIRTIO_MEM_RETRY_TIMER_MIN_MS ;break ;case -ENOSPC :/*
@@ -1552,8 +1555,7 @@ static void virtio_mem_run_wq(struct work_struct *work)*/ case -ENOMEM :/* Out of memory, try again later. */ -hrtimer_start (&vm->retry_timer, -ms_to_ktime (VIRTIO_MEM_RETRY_TIMER_MS ), +hrtimer_start (&vm->retry_timer,ms_to_ktime (vm->retry_timer_ms),HRTIMER_MODE_REL );break ;case -EAGAIN :
@@ -1573,6 +1575,9 @@ static enum hrtimer_restart virtio_mem_timer_expired(struct hrtimer *timer) retry_timer);virtio_mem_retry (vm); +/* Racy (with reset in virtio_mem_run_wq), we ignore that for now. */ + vm->retry_timer_ms = min_t(unsigned int, vm->retry_timer_ms * 2, +VIRTIO_MEM_RETRY_TIMER_MAX_MS );return HRTIMER_NORESTART ; }
@@ -1746,6 +1751,7 @@ static int virtio_mem_probe(struct virtio_device *vdev)spin_lock_init (&vm->removal_lock);hrtimer_init (&vm->retry_timer,CLOCK_MONOTONIC ,HRTIMER_MODE_REL ); vm->retry_timer.function = virtio_mem_timer_expired; + vm->retry_timer_ms =VIRTIO_MEM_RETRY_TIMER_MIN_MS ;/* register the virtqueue */ rc =virtio_mem_init_vq (vm);
Signature · 1 line
2.23.0
RFC PATCH v4 10/13 virtio-mem: Better retry handling
In reply to David HildenbrandLet's start with a retry interval of 30 seconds and double the time until we reach 30 minutes, in case we keep getting errors. Reset the retry interval in case we succeeded.
--- drivers/virtio/virtio_mem.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/virtio/virtio_mem.c +9 -3
@@ -137,7 +137,9 @@ struct virtio_mem {/* Timer for retrying to plug/unplug memory. */ struct hrtimer retry_timer; -#define VIRTIO_MEM_RETRY_TIMER_MS 30000 +unsigned int retry_timer_ms; +#define VIRTIO_MEM_RETRY_TIMER_MIN_MS 30000 +#define VIRTIO_MEM_RETRY_TIMER_MAX_MS 1800000/* Memory notifier (online/offline events). */ struct notifier_block memory_notifier;
@@ -1537,6 +1539,7 @@ static void virtio_mem_run_wq(struct work_struct *work)switch (rc) {case 0 : + vm->retry_timer_ms =VIRTIO_MEM_RETRY_TIMER_MIN_MS ;break ;case -ENOSPC :/*
@@ -1552,8 +1555,7 @@ static void virtio_mem_run_wq(struct work_struct *work)*/ case -ENOMEM :/* Out of memory, try again later. */ -hrtimer_start (&vm->retry_timer, -ms_to_ktime (VIRTIO_MEM_RETRY_TIMER_MS ), +hrtimer_start (&vm->retry_timer,ms_to_ktime (vm->retry_timer_ms),HRTIMER_MODE_REL );break ;case -EAGAIN :
@@ -1573,6 +1575,9 @@ static enum hrtimer_restart virtio_mem_timer_expired(struct hrtimer *timer) retry_timer);virtio_mem_retry (vm); +/* Racy (with reset in virtio_mem_run_wq), we ignore that for now. */ + vm->retry_timer_ms = min_t(unsigned int, vm->retry_timer_ms * 2, +VIRTIO_MEM_RETRY_TIMER_MAX_MS );return HRTIMER_NORESTART ; }
@@ -1746,6 +1751,7 @@ static int virtio_mem_probe(struct virtio_device *vdev)spin_lock_init (&vm->removal_lock);hrtimer_init (&vm->retry_timer,CLOCK_MONOTONIC ,HRTIMER_MODE_REL ); vm->retry_timer.function = virtio_mem_timer_expired; + vm->retry_timer_ms =VIRTIO_MEM_RETRY_TIMER_MIN_MS ;/* register the virtqueue */ rc =virtio_mem_init_vq (vm);
Signature · 6 lines
2.23.0 --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
RFC PATCH v4 11/13 mm/vmscan: Move count_vm_event(DROP_SLAB) into drop_slab()
In reply to David HildenbrandLet's count within the function itself, so every invocation (of future users) will be counted.
--- fs/drop_caches.c | 4 +--- mm/vmscan.c | 1 + 2 files changed, 2 insertions(+), 3 deletions(-)
fs/drop_caches.c +1 -3
@@ -61,10 +61,8 @@ int drop_caches_sysctl_handler(struct ctl_table *table, int write,iterate_supers (drop_pagecache_sb,NULL );count_vm_event (DROP_PAGECACHE ); } -if (sysctl_drop_caches &2 ) { +if (sysctl_drop_caches &2 )drop_slab (); -count_vm_event (DROP_SLAB ); - }if (!stfu) { pr_info("%s (%d): drop_caches: %d\n", current->comm, task_pid_nr(current),
mm/vmscan.c +1 -0
@@ -726,6 +726,7 @@ void drop_slab(void)for_each_online_node (nid)drop_slab_node (nid); +count_vm_event (DROP_SLAB ); }static inline int is_page_cache_freeable (struct page *page)
Signature · 1 line
2.23.0
RFC PATCH v4 11/13 mm/vmscan: Move count_vm_event(DROP_SLAB) into drop_slab()
In reply to David HildenbrandLet's count within the function itself, so every invocation (of future users) will be counted.
--- fs/drop_caches.c | 4 +--- mm/vmscan.c | 1 + 2 files changed, 2 insertions(+), 3 deletions(-)
fs/drop_caches.c +1 -3
@@ -61,10 +61,8 @@ int drop_caches_sysctl_handler(struct ctl_table *table, int write,iterate_supers (drop_pagecache_sb,NULL );count_vm_event (DROP_PAGECACHE ); } -if (sysctl_drop_caches &2 ) { +if (sysctl_drop_caches &2 )drop_slab (); -count_vm_event (DROP_SLAB ); - }if (!stfu) { pr_info("%s (%d): drop_caches: %d\n", current->comm, task_pid_nr(current),
mm/vmscan.c +1 -0
@@ -726,6 +726,7 @@ void drop_slab(void)for_each_online_node (nid)drop_slab_node (nid); +count_vm_event (DROP_SLAB ); }static inline int is_page_cache_freeable (struct page *page)
Signature · 6 lines
2.23.0 --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
Re: [PATCH RFC v4 11/13] mm/vmscan: Move count_vm_event(DROP_SLAB) into drop_slab()
Quoted 7 lines · David Hildenbrand
Let's count within the function itself, so every invocation (of future users) will be counted.
Cc Alexander Viro <[email protected]>Cc Andrew Morton <[email protected]>Signed-off-by David Hildenbrand <[email protected]>
Slight inconsistency with the page cache droppint but nothing earth shattering.
Quoted 35 lines
--- fs/drop_caches.c | 4 +--- mm/vmscan.c | 1 + 2 files changed, 2 insertions(+), 3 deletions(-)fs/drop_caches.c +1
-3@@ -61,10 +61,8 @@ int drop_caches_sysctl_handler(struct ctl_table *table, int write,iterate_supers (drop_pagecache_sb,NULL );count_vm_event (DROP_PAGECACHE ); } -if (sysctl_drop_caches &2 ) { +if (sysctl_drop_caches &2 )drop_slab (); -count_vm_event (DROP_SLAB ); - }if (!stfu) { pr_info("%s (%d): drop_caches: %d\n", current->comm, task_pid_nr(current),mm/vmscan.c +1
-0@@ -726,6 +726,7 @@ void drop_slab(void)for_each_online_node (nid)drop_slab_node (nid); +count_vm_event (DROP_SLAB ); }static inline int is_page_cache_freeable (struct page *page)Signature · 1 line
2.23.0
Signature · 2 lines
Michal Hocko SUSE Labs
RFC PATCH v4 12/13 mm/vmscan: Export drop_slab() and drop_slab_node()
In reply to David HildenbrandWe already have a way to trigger reclaiming of all reclaimable slab objects from user space (echo 2 > /proc/sys/vm/drop_caches). Let's allow drivers to also trigger this when they really want to make progress and know what they are doing.
virtio-mem wants to use these functions when it failed to unplug memory for quite some time (e.g., after 30 minutes). It will then try to free up reclaimable objects by dropping the slab caches every now and then (e.g., every 30 minutes) as long as necessary. There will be a way to disable this feature and info messages will be logged.
In the future, we want to have a drop_slab_range() functionality instead. Memory offlining code has similar demands and also other alloc_contig_range() users (e.g., gigantic pages) could make good use of this feature. Adding it, however, requires more work/thought.
--- include/linux/mm.h | 4 ++-- mm/vmscan.c | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-)
include/linux/mm.h +2 -2
@@ -2706,8 +2706,8 @@ int drop_caches_sysctl_handler(struct ctl_table *, int, void __user *, size_t *, loff_t *);#endif -void drop_slab (void ); -void drop_slab_node (int nid); +extern void drop_slab (void ); +extern void drop_slab_node (int nid);#ifndef CONFIG_MMU #define randomize_va_space 0
mm/vmscan.c +2 -0
@@ -719,6 +719,7 @@ void drop_slab_node(int nid) }while ((memcg =mem_cgroup_iter (NULL , memcg,NULL )) !=NULL ); }while (freed >10 ); } +EXPORT_SYMBOL (drop_slab_node);void drop_slab (void ) {
@@ -728,6 +729,7 @@ void drop_slab(void)drop_slab_node (nid);count_vm_event (DROP_SLAB ); } +EXPORT_SYMBOL (drop_slab);static inline int is_page_cache_freeable (struct page *page) {
Signature · 1 line
2.23.0
RFC PATCH v4 12/13 mm/vmscan: Export drop_slab() and drop_slab_node()
In reply to David HildenbrandWe already have a way to trigger reclaiming of all reclaimable slab objects from user space (echo 2 > /proc/sys/vm/drop_caches). Let's allow drivers to also trigger this when they really want to make progress and know what they are doing.
virtio-mem wants to use these functions when it failed to unplug memory for quite some time (e.g., after 30 minutes). It will then try to free up reclaimable objects by dropping the slab caches every now and then (e.g., every 30 minutes) as long as necessary. There will be a way to disable this feature and info messages will be logged.
In the future, we want to have a drop_slab_range() functionality instead. Memory offlining code has similar demands and also other alloc_contig_range() users (e.g., gigantic pages) could make good use of this feature. Adding it, however, requires more work/thought.
--- include/linux/mm.h | 4 ++-- mm/vmscan.c | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-)
include/linux/mm.h +2 -2
@@ -2706,8 +2706,8 @@ int drop_caches_sysctl_handler(struct ctl_table *, int, void __user *, size_t *, loff_t *);#endif -void drop_slab (void ); -void drop_slab_node (int nid); +extern void drop_slab (void ); +extern void drop_slab_node (int nid);#ifndef CONFIG_MMU #define randomize_va_space 0
mm/vmscan.c +2 -0
@@ -719,6 +719,7 @@ void drop_slab_node(int nid) }while ((memcg =mem_cgroup_iter (NULL , memcg,NULL )) !=NULL ); }while (freed >10 ); } +EXPORT_SYMBOL (drop_slab_node);void drop_slab (void ) {
@@ -728,6 +729,7 @@ void drop_slab(void)drop_slab_node (nid);count_vm_event (DROP_SLAB ); } +EXPORT_SYMBOL (drop_slab);static inline int is_page_cache_freeable (struct page *page) {
Signature · 6 lines
2.23.0 --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
Michal Hocko
reviewerRe: [PATCH RFC v4 12/13] mm/vmscan: Export drop_slab() and drop_slab_node()
Quoted 4 lines · David Hildenbrand
We already have a way to trigger reclaiming of all reclaimable slab objects from user space (echo 2 >
/proc/sys/vm/drop_caches). Let's allow drivers to also trigger this when they really want to make progress and know what they are doing.
I cannot say I would be fan of this. This is a global action with user visible performance impact. I am worried that we will find out that all sorts of drivers have a very good idea that dropping slab caches is going to help their problem whatever it is. We have seen the same patter in the userspace already and that is the reason we are logging the usage to the log and count invocations in the counter.
Quoted 10 lines
virtio-mem wants to use these functions when it failed to unplug memory for quite some time (e.g., after 30 minutes). It will then try to free up reclaimable objects by dropping the slab caches every now and then (e.g., every 30 minutes) as long as necessary. There will be a way to disable this feature and info messages will be logged.
In the future, we want to have a
drop_slab_range()functionality instead. Memory offlining code has similar demands and also otheralloc_contig_range()users (e.g., gigantic pages) could make good use of this feature. Adding it, however, requires more work/thought.
We already do have a memory_notify(MEM_GOING_OFFLINE) for that purpose and slab allocator implements a callback (slab_mem_going_offline_callback). The callback is quite dumb and it doesn't really try to free objects from the given memory range or even try to drop active objects which might turn out to be hard but this sounds like a more robust way to achieve what you want.
Quoted 45 lines
Cc Andrew Morton <[email protected]>Cc Michal Hocko <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- include/linux/mm.h | 4 ++-- mm/vmscan.c | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-)include/linux/mm.h +2
-2@@ -2706,8 +2706,8 @@ int drop_caches_sysctl_handler(struct ctl_table *, int, void __user *, size_t *, loff_t *);#endif -void drop_slab (void ); -void drop_slab_node (int nid); +extern void drop_slab (void ); +extern void drop_slab_node (int nid);#ifndef CONFIG_MMU #define randomize_va_space 0mm/vmscan.c +2
-0@@ -719,6 +719,7 @@ void drop_slab_node(int nid) }while ((memcg =mem_cgroup_iter (NULL , memcg,NULL )) !=NULL ); }while (freed >10 ); } +EXPORT_SYMBOL (drop_slab_node);void drop_slab (void ) {@@ -728,6 +729,7 @@ void drop_slab(void)drop_slab_node (nid);count_vm_event (DROP_SLAB ); } +EXPORT_SYMBOL (drop_slab);static inline int is_page_cache_freeable (struct page *page) {Signature · 1 line
2.23.0
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [PATCH RFC v4 12/13] mm/vmscan: Export drop_slab() and drop_slab_node()
Quoted 6 lines · Michal Hocko
Quoted 4 lines · David Hildenbrand
We already have a way to trigger reclaiming of all reclaimable slab objects from user space (echo 2 >
/proc/sys/vm/drop_caches). Let's allow drivers to also trigger this when they really want to make progress and know what they are doing.
Quoted 6 lines
I cannot say I would be fan of this. This is a global action with user visible performance impact. I am worried that we will find out that all sorts of drivers have a very good idea that dropping slab caches is going to help their problem whatever it is. We have seen the same patter in the userspace already and that is the reason we are logging the usage to the log and count invocations in the counter.
Yeah, I decided to hold back patch 11-13 for the v1 (which I am planning to post in March after more testing). What we really want is to make memory offlining an alloc_contig_range() work better with reclaimable objects.
Quoted 12 lines
Quoted 11 lines
virtio-mem wants to use these functions when it failed to unplug memory for quite some time (e.g., after 30 minutes). It will then try to free up reclaimable objects by dropping the slab caches every now and then (e.g., every 30 minutes) as long as necessary. There will be a way to disable this feature and info messages will be logged.
In the future, we want to have a
drop_slab_range()functionality instead. Memory offlining code has similar demands and also otheralloc_contig_range()users (e.g., gigantic pages) could make good use of this feature. Adding it, however, requires more work/thought.
Quoted 6 lines
We already do have a
memory_notify(MEM_GOING_OFFLINE)for that purpose and slab allocator implements a callback (slab_mem_going_offline_callback). The callback is quite dumb and it doesn't really try to free objects from the given memory range or even try to drop active objects which might turn out to be hard but this sounds like a more robust way to achieve what you want.
Two things:
1. memory_notify(MEM_GOING_OFFLINE) is called after trying to isolate the page range and checking if we only have movable pages. Won't help much I guess.
2. alloc_contig_range() won't benefit from that.
Something like drop_slab_range() would be better, and calling it from the right spots in the core (e.g., set_migratetype_isolate() see below).
Especially, have a look at mm/page_isolation.c:set_migratetype_isolate()
"FIXME: Now, memory hotplug doesn't call shrink_slab() by itself. We just check MOVABLE pages."
Signature · 3 lines
Thanks, David / dhildenb
Michal Hocko
reviewerRe: [PATCH RFC v4 12/13] mm/vmscan: Export drop_slab() and drop_slab_node()
Quoted 38 lines · David Hildenbrand
Quoted 6 lines · Michal Hocko
Quoted 4 lines · David Hildenbrand
We already have a way to trigger reclaiming of all reclaimable slab objects from user space (echo 2 >
/proc/sys/vm/drop_caches). Let's allow drivers to also trigger this when they really want to make progress and know what they are doing.Quoted 6 lines
I cannot say I would be fan of this. This is a global action with user visible performance impact. I am worried that we will find out that all sorts of drivers have a very good idea that dropping slab caches is going to help their problem whatever it is. We have seen the same patter in the userspace already and that is the reason we are logging the usage to the log and count invocations in the counter.
Yeah, I decided to hold back patch 11-13 for the v1 (which I am planning to post in March after more testing). What we really want is to make memory offlining an
alloc_contig_range()work better with reclaimable objects.Quoted 12 lines
Quoted 11 lines
virtio-mem wants to use these functions when it failed to unplug memory for quite some time (e.g., after 30 minutes). It will then try to free up reclaimable objects by dropping the slab caches every now and then (e.g., every 30 minutes) as long as necessary. There will be a way to disable this feature and info messages will be logged.
In the future, we want to have a
drop_slab_range()functionality instead. Memory offlining code has similar demands and also otheralloc_contig_range()users (e.g., gigantic pages) could make good use of this feature. Adding it, however, requires more work/thought.Quoted 6 lines
We already do have a
memory_notify(MEM_GOING_OFFLINE)for that purpose and slab allocator implements a callback (slab_mem_going_offline_callback). The callback is quite dumb and it doesn't really try to free objects from the given memory range or even try to drop active objects which might turn out to be hard but this sounds like a more robust way to achieve what you want.
Quoted 5 lines
Two things:
1.
memory_notify(MEM_GOING_OFFLINE)is called after trying to isolate the page range and checking if we only have movable pages. Won't help much I guess.
You are right, I have missed that. Can we reorder those two calls?
Quoted 1 line
2.
alloc_contig_range()won't benefit from that.
True.
Quoted 7 lines
Something like
drop_slab_range()would be better, and calling it from the right spots in the core (e.g.,set_migratetype_isolate()see below).Especially, have a look at mm/page_isolation.c:set_migratetype_isolate()
"FIXME: Now, memory hotplug doesn't call
shrink_slab()by itself. We just check MOVABLE pages."
shrink_slab is really a large hammer for this purpose. The notifier mechanism sounds more appropriate to me. If that means to move it outside of its current position then let's try to experiment with that. But there is a long route to have per pfn range reclaim.
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [PATCH RFC v4 12/13] mm/vmscan: Export drop_slab() and drop_slab_node()
Quoted 39 lines · Michal Hocko
Quoted 38 lines · David Hildenbrand
Quoted 6 lines · Michal Hocko
Quoted 4 lines · David Hildenbrand
We already have a way to trigger reclaiming of all reclaimable slab objects from user space (echo 2 >
/proc/sys/vm/drop_caches). Let's allow drivers to also trigger this when they really want to make progress and know what they are doing.Quoted 6 lines
I cannot say I would be fan of this. This is a global action with user visible performance impact. I am worried that we will find out that all sorts of drivers have a very good idea that dropping slab caches is going to help their problem whatever it is. We have seen the same patter in the userspace already and that is the reason we are logging the usage to the log and count invocations in the counter.
Yeah, I decided to hold back patch 11-13 for the v1 (which I am planning to post in March after more testing). What we really want is to make memory offlining an
alloc_contig_range()work better with reclaimable objects.Quoted 18 lines
Quoted 11 lines
virtio-mem wants to use these functions when it failed to unplug memory for quite some time (e.g., after 30 minutes). It will then try to free up reclaimable objects by dropping the slab caches every now and then (e.g., every 30 minutes) as long as necessary. There will be a way to disable this feature and info messages will be logged.
In the future, we want to have a
drop_slab_range()functionality instead. Memory offlining code has similar demands and also otheralloc_contig_range()users (e.g., gigantic pages) could make good use of this feature. Adding it, however, requires more work/thought.We already do have a
memory_notify(MEM_GOING_OFFLINE)for that purpose and slab allocator implements a callback (slab_mem_going_offline_callback). The callback is quite dumb and it doesn't really try to free objects from the given memory range or even try to drop active objects which might turn out to be hard but this sounds like a more robust way to achieve what you want.
Quoted 7 lines
Quoted 5 lines
Two things:
1.
memory_notify(MEM_GOING_OFFLINE)is called after trying to isolate the page range and checking if we only have movable pages. Won't help much I guess.You are right, I have missed that. Can we reorder those two calls?
AFAIK no (would have to look up the details, but there was a good reason for the order, e.g., avoid races with other users of page isolation like alloc_contig_range()).
Especially, "[PATCH RFC v4 06/13] mm: Allow to offline unmovable PageOffline() pages via MEM_GOING_OFFLINE" (which is still impatiently waiting for an ACK ;) ) also works around that ordering issue in a way we discussed back then.
Signature · 3 lines
Thanks, David / dhildenb
RFC PATCH v4 13/13 virtio-mem: Drop slab objects when unplug continues to fail
In reply to David HildenbrandStart dropping slab objects after 30 minutes and repeat every 30 minutes in case we can't unplug more memory using alloc_contig_range(). Log messages and make it configurable. Enable dropping slab objects as default (especially, reclaimable slab objects that are not movable).
In the future, we might want to implement+use drop_slab_range(), which will also come in handy for other users (e.g., offlining, gigantic huge pages).
--- drivers/virtio/virtio_mem.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+)
drivers/virtio/virtio_mem.c +30 -0
@@ -25,6 +25,11 @@ static bool unplug_online = true;module_param (unplug_online, bool,0644 );MODULE_PARM_DESC (unplug_online,"Try to unplug online memory" ); +static bool drop_slab_objects = true; +module_param (drop_slab_objects, bool,0644 ); +MODULE_PARM_DESC (drop_slab_objects, +"Drop slab objects when unplug continues to fail" ); +enum virtio_mem_mb_state {/* Unplugged, not added to Linux. Can be reused later. */ VIRTIO_MEM_MB_STATE_UNUSED =0 ,
@@ -1384,6 +1389,7 @@ static int virtio_mem_mb_unplug_any_sb_online(struct virtio_mem *vm,static int virtio_mem_unplug_request (struct virtio_mem *vm,uint64_t diff) {uint64_t nb_sb = diff / vm->subblock_size; +bool retried = false;unsigned long mb_id;int rc;
@@ -1421,6 +1427,7 @@ static int virtio_mem_unplug_request(struct virtio_mem *vm, uint64_t diff)return 0 ; } +retry_locked:/* Try to unplug subblocks of partially plugged online blocks. */ virtio_mem_for_each_mb_state (vm, mb_id,VIRTIO_MEM_MB_STATE_ONLINE_PARTIAL ) {
@@ -1445,6 +1452,29 @@ static int virtio_mem_unplug_request(struct virtio_mem *vm, uint64_t diff) }mutex_unlock (&vm->hotplug_mutex); + +/* +* If we can't unplug the requested amount of memory for a long time, +* start freeing up memory in caches. This might harm performance, +* is configurable, and we log a message. Retry imemdiately a second +* time - then wait another VIRTIO_MEM_RETRY_TIMER_MAX_MS. +*/ +if (nb_sb && !retried && drop_slab_objects && + vm->retry_timer_ms ==VIRTIO_MEM_RETRY_TIMER_MAX_MS ) { +if (vm->nid ==NUMA_NO_NODE ) { +dev_info (&vm->vdev->dev,"dropping all slab objects\n" ); +drop_slab (); + }else { +dev_info (&vm->vdev->dev, +"dropping all slab objects on node=%d\n" , + vm->nid); +drop_slab_node (vm->nid); + } + retried = true; +mutex_lock (&vm->hotplug_mutex); + goto retry_locked; + } +return nb_sb ? -EBUSY :0 ; out_unlock:mutex_unlock (&vm->hotplug_mutex);
Signature · 1 line
2.23.0
RFC PATCH v4 13/13 virtio-mem: Drop slab objects when unplug continues to fail
In reply to David HildenbrandStart dropping slab objects after 30 minutes and repeat every 30 minutes in case we can't unplug more memory using alloc_contig_range(). Log messages and make it configurable. Enable dropping slab objects as default (especially, reclaimable slab objects that are not movable).
In the future, we might want to implement+use drop_slab_range(), which will also come in handy for other users (e.g., offlining, gigantic huge pages).
--- drivers/virtio/virtio_mem.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+)
drivers/virtio/virtio_mem.c +30 -0
@@ -25,6 +25,11 @@ static bool unplug_online = true;module_param (unplug_online, bool,0644 );MODULE_PARM_DESC (unplug_online,"Try to unplug online memory" ); +static bool drop_slab_objects = true; +module_param (drop_slab_objects, bool,0644 ); +MODULE_PARM_DESC (drop_slab_objects, +"Drop slab objects when unplug continues to fail" ); +enum virtio_mem_mb_state {/* Unplugged, not added to Linux. Can be reused later. */ VIRTIO_MEM_MB_STATE_UNUSED =0 ,
@@ -1384,6 +1389,7 @@ static int virtio_mem_mb_unplug_any_sb_online(struct virtio_mem *vm,static int virtio_mem_unplug_request (struct virtio_mem *vm,uint64_t diff) {uint64_t nb_sb = diff / vm->subblock_size; +bool retried = false;unsigned long mb_id;int rc;
@@ -1421,6 +1427,7 @@ static int virtio_mem_unplug_request(struct virtio_mem *vm, uint64_t diff)return 0 ; } +retry_locked:/* Try to unplug subblocks of partially plugged online blocks. */ virtio_mem_for_each_mb_state (vm, mb_id,VIRTIO_MEM_MB_STATE_ONLINE_PARTIAL ) {
@@ -1445,6 +1452,29 @@ static int virtio_mem_unplug_request(struct virtio_mem *vm, uint64_t diff) }mutex_unlock (&vm->hotplug_mutex); + +/* +* If we can't unplug the requested amount of memory for a long time, +* start freeing up memory in caches. This might harm performance, +* is configurable, and we log a message. Retry imemdiately a second +* time - then wait another VIRTIO_MEM_RETRY_TIMER_MAX_MS. +*/ +if (nb_sb && !retried && drop_slab_objects && + vm->retry_timer_ms ==VIRTIO_MEM_RETRY_TIMER_MAX_MS ) { +if (vm->nid ==NUMA_NO_NODE ) { +dev_info (&vm->vdev->dev,"dropping all slab objects\n" ); +drop_slab (); + }else { +dev_info (&vm->vdev->dev, +"dropping all slab objects on node=%d\n" , + vm->nid); +drop_slab_node (vm->nid); + } + retried = true; +mutex_lock (&vm->hotplug_mutex); + goto retry_locked; + } +return nb_sb ? -EBUSY :0 ; out_unlock:mutex_unlock (&vm->hotplug_mutex);
Signature · 1 line
2.23.0
Quoted 2 lines · David Hildenbrand
This series is based on latest linux-next. The patches are located at:
https://github.com/davidhildenbrand/linux.git virtio-mem-rfc-v4
Heya!
Would there be by any chance a virtio-spec git tree somewhere?
..snip..
Quoted 7 lines
--------------------------------------------------------------------------
5. Future work
--------------------------------------------------------------------------The separate patches contain a lot of future work items. One of the next steps is to make memory unplug more likely to succeed - currently, there are no guarantees on how much memory can get unplugged again. I have
Or perhaps tell the caller why we can't and let them sort it out? For example: "Application XYZ is mlocked. Can't offload'.
Quoted 4 lines · Konrad Rzeszutek Wilk
Quoted 2 lines · David Hildenbrand
This series is based on latest linux-next. The patches are located at:
https://github.com/davidhildenbrand/linux.git virtio-mem-rfc-v4Heya!
Hi Konrad!
Quoted 2 lines
Would there be by any chance a virtio-spec git tree somewhere?
I haven't started working on a spec yet - it's on my todo list but has low priority (one-man-team). I'll focus on the QEMU pieces next, once the kernel part is in an acceptable state.
The uapi file contains quite some documentation - if somebody wants to start hacking on an alternative hypervisor implementation, I'm happy to answer questions until I have a spec ready.
Quoted 6 lines
..snip..
Quoted 4 lines
--------------------------------------------------------------------------
5. Future work
--------------------------------------------------------------------------
Quoted 7 lines
Quoted 3 lines
The separate patches contain a lot of future work items. One of the next steps is to make memory unplug more likely to succeed - currently, there are no guarantees on how much memory can get unplugged again. I have
Or perhaps tell the caller why we can't and let them sort it out? For example: "Application XYZ is mlocked. Can't offload'.
Yes, it might in general be interesting for the guest to indicate persistent errors, both when hotplugging and hotunplugging memory. Indicating why unplugging is not able to succeed in that detail is, however, non-trivial.
The hypervisor sets the requested size can can watch over the actual size of a virtio-mem device. Right now, after it updated the requested size, it can wait some time (e.g., 1-5 minutes). If the requested size was not reached after that time, it knows there is a persistent issue limiting plug/unplug. In the future, this could be extended by a rough or detailed root cause indication. In the worst case, the guest crashed and is no longer able to respond (not even with an error indication).
One interesting piece of the current hypervisor (QEMU) design is that the maximum memory size a VM can consume is always known and QEMU will send QMP events to upper layers whenever that size changes. This means that you can e.g., reliably charge a customer how much memory a VM is actually able to consume over time (independent of hotplug/unplug errors). But yeah, the QEMU bits are still in a very early stage.
Signature · 3 lines
Thanks, David / dhildenb
Quoted 4 lines · Konrad Rzeszutek Wilk
Quoted 2 lines · David Hildenbrand
This series is based on latest linux-next. The patches are located at:
https://github.com/davidhildenbrand/linux.git virtio-mem-rfc-v4Heya!
Hi Konrad!
Quoted 2 lines
Would there be by any chance a virtio-spec git tree somewhere?
I haven't started working on a spec yet - it's on my todo list but has low priority (one-man-team). I'll focus on the QEMU pieces next, once the kernel part is in an acceptable state.
The uapi file contains quite some documentation - if somebody wants to start hacking on an alternative hypervisor implementation, I'm happy to answer questions until I have a spec ready.
Quoted 6 lines
..snip..
Quoted 4 lines
--------------------------------------------------------------------------
5. Future work
--------------------------------------------------------------------------
Quoted 7 lines
Quoted 3 lines
The separate patches contain a lot of future work items. One of the next steps is to make memory unplug more likely to succeed - currently, there are no guarantees on how much memory can get unplugged again. I have
Or perhaps tell the caller why we can't and let them sort it out? For example: "Application XYZ is mlocked. Can't offload'.
Yes, it might in general be interesting for the guest to indicate persistent errors, both when hotplugging and hotunplugging memory. Indicating why unplugging is not able to succeed in that detail is, however, non-trivial.
The hypervisor sets the requested size can can watch over the actual size of a virtio-mem device. Right now, after it updated the requested size, it can wait some time (e.g., 1-5 minutes). If the requested size was not reached after that time, it knows there is a persistent issue limiting plug/unplug. In the future, this could be extended by a rough or detailed root cause indication. In the worst case, the guest crashed and is no longer able to respond (not even with an error indication).
One interesting piece of the current hypervisor (QEMU) design is that the maximum memory size a VM can consume is always known and QEMU will send QMP events to upper layers whenever that size changes. This means that you can e.g., reliably charge a customer how much memory a VM is actually able to consume over time (independent of hotplug/unplug errors). But yeah, the QEMU bits are still in a very early stage.
Signature · 3 lines
Thanks, David / dhildenb
Hi David,
Thanks for your work.
I Got following build fail if X86_64_ACPI_NUMA is n with rfc3 and rfc4:
make -j8 bzImage
GEN Makefile
DESCEND objtool
CALL /home/teawater/kernel/linux-upstream3/scripts/atomic/check-atomics.sh
CALL /home/teawater/kernel/linux-upstream3/scripts/checksyscalls.sh
CHK include/generated/compile.h
CC drivers/virtio/virtio_mem.o
/home/teawater/kernel/linux-upstream3/drivers/virtio/virtio_mem.c: In function ‘virtio_mem_translate_node_id’:
/home/teawater/kernel/linux-upstream3/drivers/virtio/virtio_mem.c:478:10: error: implicit declaration of function ‘pxm_to_node’ [-Werror=implicit-function-declaration]
node = pxm_to_node(node_id);
^~~~~~~~~~~cc1: some warnings being treated as errors /home/teawater/kernel/linux-upstream3/scripts/Makefile.build:265: recipe for target 'drivers/virtio/virtio_mem.o' failed make[3]: *** [drivers/virtio/virtio_mem.o] Error 1 /home/teawater/kernel/linux-upstream3/scripts/Makefile.build:503: recipe for target 'drivers/virtio' failed make[2]: *** [drivers/virtio] Error 2/home/teawater/kernel/linux-upstream3/Makefile:1649: recipe for target 'drivers' failed
make[1]: *** [drivers] Error 2/home/teawater/kernel/linux-upstream3/Makefile:179: recipe for target 'sub-make' failed
make: *** [sub-make] Error 2
Best,
Hui
Quoted 355 lines · 在 2019年12月13日,01:11,David Hildenbrand <[email protected]> 写道:
This series is based on latest linux-next. The patches are located at:
https://github.com/davidhildenbrand/linux.git virtio-mem-rfc-v4The basic idea of virtio-mem is to provide a flexible, cross-architecture memory hot(un)plug solution that avoids many limitations imposed by existing technologies, architectures, and interfaces. More details can be found below and in linked material.
This RFC is limited to x86-64, however, should theoretically work on any architecture that supports virtio and implements memory hot(un)plug under Linux - like s390x, powerpc64 and arm64. On x86-64, it is currently possible to add/remove memory to the system in >= 4MB granularity. Memory hotplug works very reliably. For memory unplug, there are no guarantees how much memory can actually get unplugged, it depends on the setup (especially: fragmentation of (unmovable) memory). I have plans to improve that in the future.
--------------------------------------------------------------------------
1. virtio-mem
--------------------------------------------------------------------------The basic idea behind virtio-mem was presented at KVM Forum 2018. The slides can be found at [1]. The previous RFC can be found at [2]. The first RFC can be found at [3]. However, the concept evolved over time. The KVM Forum slides roughly match the current design.
Patch #2 ("virtio-mem: Paravirtualized memory hotplug") contains quite some information, especially in "
include/uapi/linux/virtio_mem.h":Each virtio-mem device manages a dedicated region in physical address space. Each device can belong to a single NUMA node, multiple devices for a single NUMA node are possible. A virtio-mem device is like a "resizable DIMM" consisting of small memory blocks that can be plugged or unplugged. The device driver is responsible for (un)plugging memory blocks on demand.
Virtio-mem devices can only operate on their assigned memory region in order to (un)plug memory. A device cannot (un)plug memory belonging to other devices.
The "
region_size" corresponds to the maximum amount of memory that can be provided by a device. The "size" corresponds to the amount of memory that is currently plugged. "requested_size" corresponds to a request from the device to the device driver to (un)plug blocks. The device driver should try to (un)plug blocks in order to reach the "requested_size". It is impossible to plug more memory than requested.The "
usable_region_size" represents the memory region that can actually be used to (un)plug memory. It is always at least as big as the "requested_size" and will grow dynamically. It will only shrink when explicitly triggered (VIRTIO_MEM_REQ_UNPLUG).Memory in the usable region can usually be read, however, there are no guarantees. It can happen that the device cannot process a request, because it is busy. The device driver has to retry later.
Usually, during system resets all memory will get unplugged, so the device driver can start with a clean state. However, in specific scenarios (if the device is busy) it can happen that the device still has memory plugged. The device driver can request to unplug all memory (
VIRTIO_MEM_REQ_UNPLUG) - which might take a while to succeed if the device is busy.--------------------------------------------------------------------------
2. Linux Implementation
--------------------------------------------------------------------------This RFC reuses quite some existing MM infrastructure, however, has to expose some additional functionality.
Memory blocks (e.g., 128MB) are added/removed on demand. Within these memory blocks, subblocks (e.g., 4MB) are plugged/unplugged. The sizes depend on the target architecture,
MAX_ORDER+pageblock_order, and the block size of a virtio-mem device.add_memory()/try_remove_memory() is used to add/remove memory blocks. virtio-mem will not online memory blocks itself. This has to be done by user space, or configured into the kernel (
CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE). virtio-mem will only unplug memory that was online to theZONE_NORMAL. Memory is suggested to be onlined to theZONE_NORMALfor now.The memory hotplug notifier is used to properly synchronize against onlining/offlining of memory blocks and to track the states of memory blocks (including the zone memory blocks are onlined to).
The
set_online_page()callback is used to keep unplugged subblocks of a memory block fake-offline when onlining the memory block.generic_online_page()is used to fake-online plugged subblocks. This handling is similar to the Hyper-V balloon driver.
PG_offlineis used to mark unplugged subblocks as offline, so e.g., dumping tools (makedumpfile) will skip these pages. This is similar to other balloon drivers like virtio-balloon and Hyper-V.Memory offlining code is extended to allow drivers to drop their reference to
PG_offlinepages whenMEM_GOING_OFFLINE, so these pages can be skipped when offlining memory blocks. This allows to offline memory blocks that have partially unplugged (allocated e.g., viaalloc_contig_range()) subblocks - or are completely unplugged.alloc_contig_range()/free_contig_range() [now exposed] is used to unplug/plug subblocks of memory blocks the are already exposed to Linux.
offline_and_remove_memory()[new] is used to offline a fully unplugged memory block and remove it from Linux.A lot of additional information can be found in the separate patches and as comments in the code itself.
--------------------------------------------------------------------------
3. Changes RFC v2 -> v3
--------------------------------------------------------------------------A lot of things changed, especially also on the QEMU + virtio side. The biggest changes on the Linux driver side are:
- Onlining/offlining of subblocks is now emulated on top of memory blocks. set_online_page()+alloc_contig_range()+free_contig_range() is now used for that. Core MM does not have to be modified and will continue to online/offline full memory blocks.
- Onlining/offlining of memory blocks is no longer performed by virtio-mem.
-Pg_offlineis upstream and can be used. It is also used to allow offlining of partially unplugged memory blocks.
- Memory block states + subblocks are now tracked more space-efficient.
- Properkexec(),kdump(), driver unload, driver reload,ZONE_MOVABLE, ... handling.--------------------------------------------------------------------------
4. Changes RFC v3 -> RFC v4
--------------------------------------------------------------------------Only minor things changed, especially nothing on the QEMU + virtio side. Interresting changes on the Linux driver side are:
- "mm: Allow to offline unmovablePageOffline()pages viaMEM_GOING_OFFLINE"
-- Rework to Michals suggestion (allow to isolate allPageOffline()pages
by skipping allPageOffline()pages inhas_unmovable_pages(). Fail offlining later if the pages cannot be offlined/migrated).
- "virtio-mem: Allow to offline partially unplugged memory blocks" -- Adapt to Michals suggestion on core-mm part.
- "virtio-mem: Better retry handling"
-- Optimize retry intervals
- "virtio-mem: Drop slab objects when unplug continues to fail" -- Call drop_slab()/drop_slab_node() when unplug keeps failing for a longer time.
- Multiple cleanups and fixes.--------------------------------------------------------------------------
5. Future work
--------------------------------------------------------------------------The separate patches contain a lot of future work items. One of the next steps is to make memory unplug more likely to succeed - currently, there are no guarantees on how much memory can get unplugged again. I have various ideas on how to limit fragmentation of all memory blocks that virtio-mem added.
Memory hotplug:
- Reduce the amount of memory resources if that turnes out to be an issue. Or try to speed up relevant code paths to deal with many resources.
- Allocate the vmemmap from the added memory. Makes hotplug more likely to succeed, the vmemmap is stored on the same NUMA node and that unmovable memory will later not hinder unplug.Memory hotunplug:
- Performance improvements:
-- Sense (lockless) if it make sense to tryalloc_contig_range()at all
before directly trying to isolate and taking locks.
-- Try to unplug bigger chunks if possible first.
-- Identify free areas first, that don't have to be evacuated.
- Make unplug more likely to succeed:
-- There are various idea to limit fragmentation on memory block
granularity. (e.g.,ZONE_PREFER_MOVABLEand smart balancing)
-- Allocate memmap from added memory. This way, less unmovable data can
end up on the memory blocks.
- OOM handling, e.g., via an OOM handler.
- Defragmentation
-- Will require a new virtio-mem CMD to exchange plugged<->unplugged blocks--------------------------------------------------------------------------
6. Example Usage
--------------------------------------------------------------------------A very basic QEMU prototype (kept updated) is available at:
https://github.com/davidhildenbrand/qemu.git virtio-memIt lacks various features, however, works to test the guest driver side:
- No support for resizable memory regions / memory backends yet
- No protection of unplugged memory (esp., userfaultfd-wp) yet
- No dump/migration/XXX optimizations to skip unplugged memory (and avoid touching it)Start QEMU with two virtio-mem devices (one per NUMA node):
$ qemu-system-x86_64 -m 4G,maxmem=20G \ -smp sockets=2,cores=2 \ -numa node,nodeid=0,cpus=0-1 -numa node,nodeid=1,cpus=2-3 \ [...] -object memory-backend-ram,id=mem0,size=8G \ -device virtio-mem-pci,id=vm0,memdev=mem0,node=0,requested-size=128M \ -object memory-backend-ram,id=mem1,size=8G \ -device virtio-mem-pci,id=vm1,memdev=mem1,node=1,requested-size=80MQuery the configuration:
QEMU 4.1.95 monitor - type 'help' for more information (qemu) info memory-devices
Memory device [virtio-mem]: "vm0"
memaddr:0x140000000
node: 0
requested-size: 134217728
size: 134217728
max-size: 8589934592
block-size: 2097152
memdev: /objects/mem0
Memory device [virtio-mem]: "vm1"
memaddr:0x340000000
node: 1
requested-size: 83886080
size: 83886080
max-size: 8589934592
block-size: 2097152
memdev: /objects/mem1Add some memory to node 1:
QEMU 4.1.95 monitor - type 'help' for more information (qemu) qom-set vm1 requested-size 1GRemove some memory from node 0:
QEMU 4.1.95 monitor - type 'help' for more information (qemu) qom-set vm0 requested-size 64MQuery the configuration again:
(qemu) info memory-devices
Memory device [virtio-mem]: "vm0"
memaddr:0x140000000
node: 0
requested-size: 67108864
size: 67108864
max-size: 8589934592
block-size: 2097152
memdev: /objects/mem0
Memory device [virtio-mem]: "vm1"
memaddr:0x340000000
node: 1
requested-size: 1073741824
size: 1073741824
max-size: 8589934592
block-size: 2097152
memdev: /objects/mem1--------------------------------------------------------------------------
7. Q/A
--------------------------------------------------------------------------Q: Why add/remove parts ("subblocks") of memory blocks/sections?
A: Flexibility (section size depends on the architecture) - e.g., some
architectures have a section size of 2GB. Also, the memory block size is variable (e.g., on x86-64). I want to avoid any such restrictions. Some use cases want to add/remove memory in smaller granularities to a VM (e.g., the Hyper-V balloon also implements this) - especially smaller VMs like used for kata containers. Also, on memory unplug, it is more reliable to free-up and unplug multiple small chunks instead of one big chunk. E.g., if one page of a DIMM is either unmovable or pinned, the DIMM can't get unplugged. This approach is basically a compromise between DIMM-based memory hot(un)plug and balloon inflation/deflation, which works mostly on page granularity.Q: Why care about memory blocks?
A: They are the way to tell user space about new memory. This way,
memory can get onlined/offlined by user space. Also, e.g., kdump relies on udev events to reload kexec when memory blocks are onlined/offlined. Memory blocks are the "real" memory hot(un)plug granularity. Everything that's smaller has to be emulated "on top".Q: Won't memory unplug of subblocks fragment memory?
A: Yes and no. Unplugging e.g., >=4MB subblocks on x86-64 will not really
fragment memory like unplugging random pages like a balloon driver does. Buddy merging will not be limited. However, any allocation that requires bigger consecutive memory chunks (e.g., gigantic pages) might observe the fragmentation. Possible solutions: Allocate gigantic huge pages before unplugging memory, don't unplug memory, combine virtio-mem with DIMM based memory or bigger initial memory. Remember, a virtio-mem device will only unplug on the memory range it manages, not on other DIMMs. Unplug of single memory blocks will result in similar fragmentation in respect to gigantic huge pages. I ahve plans for a virtio-mem defragmentation feature in the future.Q: How reliable is memory unplug?
A: There are no guarantees on how much memory can get unplugged
again. However, it is more likely to find 4MB chunks to unplug than e.g., 128MB chunks. If memory is terribly fragmented, there is nothing we can do - for now. I consider memory hotplug the first primary use of virtio-mem. Memory unplug might usually work, but we want to improve the performance and the amount of memory we can actually unplug later.Q: Why not unplug from the
ZONE_MOVABLE?
A: Unplugged memory chunks are unmovable. Unmovable data must not end up
on theZONE_MOVABLE- similar to gigantic pages - they will never be allocated fromZONE_MOVABLE. virtio-mem added memory can be onlined to theZONE_MOVABLE, but subblocks will not get unplugged from it.Q: How big should the initial (!virtio-mem) memory of a VM be?
A: virtio-mem memory will not go to the DMA zones. So to avoid running out
of DMA memory, I suggest something like 2-3GB on x86-64. But many VMs can most probably deal with less DMA memory - depends on the use case.[1] https://events.linuxfoundation.org/wp-content/uploads/2017/12/virtio-mem-Paravirtualized-Memory-David-Hildenbrand-Red-Hat-1.pdf
[2] https://lkml.kernel.org/r/[email protected]
[3] https://lkml.kernel.org/r/[email protected]Cc Sebastien Boeuf <[email protected]>Cc Samuel Ortiz <[email protected]>Cc Robert Bradford <[email protected]>Cc Luiz Capitulino <[email protected]>David Hildenbrand (13):
ACPI: NUMA: exportpxm_to_node
virtio-mem: Paravirtualized memory hotplug
virtio-mem: Paravirtualized memory hotunplug part 1
mm: Exportalloc_contig_range()/free_contig_range()
virtio-mem: Paravirtualized memory hotunplug part 2
mm: Allow to offline unmovablePageOffline()pages via
MEM_GOING_OFFLINE
virtio-mem: Allow to offline partially unplugged memory blocksmm/memory_hotplug: Introduceoffline_and_remove_memory()
virtio-mem: Offline and remove completely unplugged memory blocks
virtio-mem: Better retry handling
mm/vmscan: Movecount_vm_event(DROP_SLAB)intodrop_slab()mm/vmscan: Exportdrop_slab()anddrop_slab_node()
virtio-mem: Drop slab objects when unplug continues to faildrivers/acpi/numa/srat.c | 1 + drivers/virtio/Kconfig | 18 + drivers/virtio/Makefile | 1 + drivers/virtio/virtio_mem.c | 1939 +++++++++++++++++++++++++++++++ fs/drop_caches.c | 4 +- include/linux/memory_hotplug.h | 1 + include/linux/mm.h | 4 +- include/linux/page-flags.h | 10 + include/uapi/linux/virtio_ids.h | 1 + include/uapi/linux/virtio_mem.h | 204 ++++ mm/memory_hotplug.c | 76 +- mm/page_alloc.c | 26 + mm/page_isolation.c | 9 + mm/vmscan.c | 3 + 14 files changed, 2282 insertions(+), 15 deletions(-)create mode 100644
drivers/virtio/virtio_mem.ccreate mode 100644include/uapi/linux/virtio_mem.hSignature · 1 line
2.23.0
Quoted 20 lines
Am 24.12.2019 um 08:04 schrieb teawater <[email protected]>:
Hi David,
Thanks for your work.
I Got following build fail if
X86_64_ACPI_NUMAis n with rfc3 and rfc4:
make -j8 bzImage
GENMakefile
DESCEND objtoolCALL /home/teawater/kernel/linux-upstream3/scripts/atomic/check-atomics.sh CALL /home/teawater/kernel/linux-upstream3/scripts/checksyscalls.sh CHK include/generated/compile.h CC drivers/virtio/virtio_mem.o /home/teawater/kernel/linux-upstream3/drivers/virtio/virtio_mem.c: In function ‘virtio_mem_translate_node_id’: /home/teawater/kernel/linux-upstream3/drivers/virtio/virtio_mem.c:478:10: error: implicit declaration of function ‘pxm_to_node’ [-Werror=implicit-function-declaration] node = pxm_to_node(node_id); ^~~~~~~~~~~
Quoted 10 lines
cc1: some warnings being treated as errors
/home/teawater/kernel/linux-upstream3/scripts/Makefile.build:265: recipe for target 'drivers/virtio/virtio_mem.o' failed make[3]: *** [drivers/virtio/virtio_mem.o] Error 1/home/teawater/kernel/linux-upstream3/scripts/Makefile.build:503: recipe for target 'drivers/virtio' failed make[2]: *** [drivers/virtio] Error 2/home/teawater/kernel/linux-upstream3/Makefile:1649: recipe for target 'drivers' failed
make[1]: *** [drivers] Error 2/home/teawater/kernel/linux-upstream3/Makefile:179: recipe for target 'sub-make' failed
make: *** [sub-make] Error 2
Thanks Hui,
So it has to be wrapped in an ifdef, thanks!
Cheers!
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 20 lines
Am 24.12.2019 um 08:04 schrieb teawater <[email protected]>:
Hi David,
Thanks for your work.
I Got following build fail if
X86_64_ACPI_NUMAis n with rfc3 and rfc4:
make -j8 bzImage
GENMakefile
DESCEND objtoolCALL /home/teawater/kernel/linux-upstream3/scripts/atomic/check-atomics.sh CALL /home/teawater/kernel/linux-upstream3/scripts/checksyscalls.sh CHK include/generated/compile.h CC drivers/virtio/virtio_mem.o /home/teawater/kernel/linux-upstream3/drivers/virtio/virtio_mem.c: In function ‘virtio_mem_translate_node_id’: /home/teawater/kernel/linux-upstream3/drivers/virtio/virtio_mem.c:478:10: error: implicit declaration of function ‘pxm_to_node’ [-Werror=implicit-function-declaration] node = pxm_to_node(node_id); ^~~~~~~~~~~
Quoted 10 lines
cc1: some warnings being treated as errors
/home/teawater/kernel/linux-upstream3/scripts/Makefile.build:265: recipe for target 'drivers/virtio/virtio_mem.o' failed make[3]: *** [drivers/virtio/virtio_mem.o] Error 1/home/teawater/kernel/linux-upstream3/scripts/Makefile.build:503: recipe for target 'drivers/virtio' failed make[2]: *** [drivers/virtio] Error 2/home/teawater/kernel/linux-upstream3/Makefile:1649: recipe for target 'drivers' failed
make[1]: *** [drivers] Error 2/home/teawater/kernel/linux-upstream3/Makefile:179: recipe for target 'sub-make' failed
make: *** [sub-make] Error 2
Thanks Hui,
So it has to be wrapped in an ifdef, thanks!
Cheers!
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
Quoted 317 lines · David Hildenbrand
This series is based on latest linux-next. The patches are located at:
https://github.com/davidhildenbrand/linux.git virtio-mem-rfc-v4The basic idea of virtio-mem is to provide a flexible, cross-architecture memory hot(un)plug solution that avoids many limitations imposed by existing technologies, architectures, and interfaces. More details can be found below and in linked material.
This RFC is limited to x86-64, however, should theoretically work on any architecture that supports virtio and implements memory hot(un)plug under Linux - like s390x, powerpc64 and arm64. On x86-64, it is currently possible to add/remove memory to the system in >= 4MB granularity. Memory hotplug works very reliably. For memory unplug, there are no guarantees how much memory can actually get unplugged, it depends on the setup (especially: fragmentation of (unmovable) memory). I have plans to improve that in the future.
--------------------------------------------------------------------------
1. virtio-mem
--------------------------------------------------------------------------The basic idea behind virtio-mem was presented at KVM Forum 2018. The slides can be found at [1]. The previous RFC can be found at [2]. The first RFC can be found at [3]. However, the concept evolved over time. The KVM Forum slides roughly match the current design.
Patch #2 ("virtio-mem: Paravirtualized memory hotplug") contains quite some information, especially in "
include/uapi/linux/virtio_mem.h":Each virtio-mem device manages a dedicated region in physical address space. Each device can belong to a single NUMA node, multiple devices for a single NUMA node are possible. A virtio-mem device is like a "resizable DIMM" consisting of small memory blocks that can be plugged or unplugged. The device driver is responsible for (un)plugging memory blocks on demand.
Virtio-mem devices can only operate on their assigned memory region in order to (un)plug memory. A device cannot (un)plug memory belonging to other devices.
The "
region_size" corresponds to the maximum amount of memory that can be provided by a device. The "size" corresponds to the amount of memory that is currently plugged. "requested_size" corresponds to a request from the device to the device driver to (un)plug blocks. The device driver should try to (un)plug blocks in order to reach the "requested_size". It is impossible to plug more memory than requested.The "
usable_region_size" represents the memory region that can actually be used to (un)plug memory. It is always at least as big as the "requested_size" and will grow dynamically. It will only shrink when explicitly triggered (VIRTIO_MEM_REQ_UNPLUG).Memory in the usable region can usually be read, however, there are no guarantees. It can happen that the device cannot process a request, because it is busy. The device driver has to retry later.
Usually, during system resets all memory will get unplugged, so the device driver can start with a clean state. However, in specific scenarios (if the device is busy) it can happen that the device still has memory plugged. The device driver can request to unplug all memory (
VIRTIO_MEM_REQ_UNPLUG) - which might take a while to succeed if the device is busy.--------------------------------------------------------------------------
2. Linux Implementation
--------------------------------------------------------------------------This RFC reuses quite some existing MM infrastructure, however, has to expose some additional functionality.
Memory blocks (e.g., 128MB) are added/removed on demand. Within these memory blocks, subblocks (e.g., 4MB) are plugged/unplugged. The sizes depend on the target architecture,
MAX_ORDER+pageblock_order, and the block size of a virtio-mem device.add_memory()/try_remove_memory() is used to add/remove memory blocks. virtio-mem will not online memory blocks itself. This has to be done by user space, or configured into the kernel (
CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE). virtio-mem will only unplug memory that was online to theZONE_NORMAL. Memory is suggested to be onlined to theZONE_NORMALfor now.The memory hotplug notifier is used to properly synchronize against onlining/offlining of memory blocks and to track the states of memory blocks (including the zone memory blocks are onlined to).
The
set_online_page()callback is used to keep unplugged subblocks of a memory block fake-offline when onlining the memory block.generic_online_page()is used to fake-online plugged subblocks. This handling is similar to the Hyper-V balloon driver.
PG_offlineis used to mark unplugged subblocks as offline, so e.g., dumping tools (makedumpfile) will skip these pages. This is similar to other balloon drivers like virtio-balloon and Hyper-V.Memory offlining code is extended to allow drivers to drop their reference to
PG_offlinepages whenMEM_GOING_OFFLINE, so these pages can be skipped when offlining memory blocks. This allows to offline memory blocks that have partially unplugged (allocated e.g., viaalloc_contig_range()) subblocks - or are completely unplugged.alloc_contig_range()/free_contig_range() [now exposed] is used to unplug/plug subblocks of memory blocks the are already exposed to Linux.
offline_and_remove_memory()[new] is used to offline a fully unplugged memory block and remove it from Linux.A lot of additional information can be found in the separate patches and as comments in the code itself.
--------------------------------------------------------------------------
3. Changes RFC v2 -> v3
--------------------------------------------------------------------------A lot of things changed, especially also on the QEMU + virtio side. The biggest changes on the Linux driver side are:
- Onlining/offlining of subblocks is now emulated on top of memory blocks. set_online_page()+alloc_contig_range()+free_contig_range() is now used for that. Core MM does not have to be modified and will continue to online/offline full memory blocks.
- Onlining/offlining of memory blocks is no longer performed by virtio-mem.
-Pg_offlineis upstream and can be used. It is also used to allow offlining of partially unplugged memory blocks.
- Memory block states + subblocks are now tracked more space-efficient.
- Properkexec(),kdump(), driver unload, driver reload,ZONE_MOVABLE, ... handling.--------------------------------------------------------------------------
4. Changes RFC v3 -> RFC v4
--------------------------------------------------------------------------Only minor things changed, especially nothing on the QEMU + virtio side. Interresting changes on the Linux driver side are:
- "mm: Allow to offline unmovablePageOffline()pages viaMEM_GOING_OFFLINE"
-- Rework to Michals suggestion (allow to isolate allPageOffline()pages
by skipping allPageOffline()pages inhas_unmovable_pages(). Fail offlining later if the pages cannot be offlined/migrated).
- "virtio-mem: Allow to offline partially unplugged memory blocks" -- Adapt to Michals suggestion on core-mm part.
- "virtio-mem: Better retry handling"
-- Optimize retry intervals
- "virtio-mem: Drop slab objects when unplug continues to fail" -- Call drop_slab()/drop_slab_node() when unplug keeps failing for a longer time.
- Multiple cleanups and fixes.--------------------------------------------------------------------------
5. Future work
--------------------------------------------------------------------------The separate patches contain a lot of future work items. One of the next steps is to make memory unplug more likely to succeed - currently, there are no guarantees on how much memory can get unplugged again. I have various ideas on how to limit fragmentation of all memory blocks that virtio-mem added.
Memory hotplug:
- Reduce the amount of memory resources if that turnes out to be an issue. Or try to speed up relevant code paths to deal with many resources.
- Allocate the vmemmap from the added memory. Makes hotplug more likely to succeed, the vmemmap is stored on the same NUMA node and that unmovable memory will later not hinder unplug.Memory hotunplug:
- Performance improvements:
-- Sense (lockless) if it make sense to tryalloc_contig_range()at all
before directly trying to isolate and taking locks.
-- Try to unplug bigger chunks if possible first.
-- Identify free areas first, that don't have to be evacuated.
- Make unplug more likely to succeed:
-- There are various idea to limit fragmentation on memory block
granularity. (e.g.,ZONE_PREFER_MOVABLEand smart balancing)
-- Allocate memmap from added memory. This way, less unmovable data can
end up on the memory blocks.
- OOM handling, e.g., via an OOM handler.
- Defragmentation
-- Will require a new virtio-mem CMD to exchange plugged<->unplugged blocks--------------------------------------------------------------------------
6. Example Usage
--------------------------------------------------------------------------A very basic QEMU prototype (kept updated) is available at:
https://github.com/davidhildenbrand/qemu.git virtio-memIt lacks various features, however, works to test the guest driver side:
- No support for resizable memory regions / memory backends yet
- No protection of unplugged memory (esp., userfaultfd-wp) yet
- No dump/migration/XXX optimizations to skip unplugged memory (and avoid touching it)Start QEMU with two virtio-mem devices (one per NUMA node):
$ qemu-system-x86_64 -m 4G,maxmem=20G \ -smp sockets=2,cores=2 \ -numa node,nodeid=0,cpus=0-1 -numa node,nodeid=1,cpus=2-3 \ [...] -object memory-backend-ram,id=mem0,size=8G \ -device virtio-mem-pci,id=vm0,memdev=mem0,node=0,requested-size=128M \ -object memory-backend-ram,id=mem1,size=8G \ -device virtio-mem-pci,id=vm1,memdev=mem1,node=1,requested-size=80MQuery the configuration:
QEMU 4.1.95 monitor - type 'help' for more information (qemu) info memory-devices
Memory device [virtio-mem]: "vm0"
memaddr:0x140000000
node: 0
requested-size: 134217728
size: 134217728
max-size: 8589934592
block-size: 2097152
memdev: /objects/mem0
Memory device [virtio-mem]: "vm1"
memaddr:0x340000000
node: 1
requested-size: 83886080
size: 83886080
max-size: 8589934592
block-size: 2097152
memdev: /objects/mem1Add some memory to node 1:
QEMU 4.1.95 monitor - type 'help' for more information (qemu) qom-set vm1 requested-size 1GRemove some memory from node 0:
QEMU 4.1.95 monitor - type 'help' for more information (qemu) qom-set vm0 requested-size 64MQuery the configuration again:
(qemu) info memory-devices
Memory device [virtio-mem]: "vm0"
memaddr:0x140000000
node: 0
requested-size: 67108864
size: 67108864
max-size: 8589934592
block-size: 2097152
memdev: /objects/mem0
Memory device [virtio-mem]: "vm1"
memaddr:0x340000000
node: 1
requested-size: 1073741824
size: 1073741824
max-size: 8589934592
block-size: 2097152
memdev: /objects/mem1--------------------------------------------------------------------------
7. Q/A
--------------------------------------------------------------------------Q: Why add/remove parts ("subblocks") of memory blocks/sections?
A: Flexibility (section size depends on the architecture) - e.g., some
architectures have a section size of 2GB. Also, the memory block size is variable (e.g., on x86-64). I want to avoid any such restrictions. Some use cases want to add/remove memory in smaller granularities to a VM (e.g., the Hyper-V balloon also implements this) - especially smaller VMs like used for kata containers. Also, on memory unplug, it is more reliable to free-up and unplug multiple small chunks instead of one big chunk. E.g., if one page of a DIMM is either unmovable or pinned, the DIMM can't get unplugged. This approach is basically a compromise between DIMM-based memory hot(un)plug and balloon inflation/deflation, which works mostly on page granularity.Q: Why care about memory blocks?
A: They are the way to tell user space about new memory. This way,
memory can get onlined/offlined by user space. Also, e.g., kdump relies on udev events to reload kexec when memory blocks are onlined/offlined. Memory blocks are the "real" memory hot(un)plug granularity. Everything that's smaller has to be emulated "on top".Q: Won't memory unplug of subblocks fragment memory?
A: Yes and no. Unplugging e.g., >=4MB subblocks on x86-64 will not really
fragment memory like unplugging random pages like a balloon driver does. Buddy merging will not be limited. However, any allocation that requires bigger consecutive memory chunks (e.g., gigantic pages) might observe the fragmentation. Possible solutions: Allocate gigantic huge pages before unplugging memory, don't unplug memory, combine virtio-mem with DIMM based memory or bigger initial memory. Remember, a virtio-mem device will only unplug on the memory range it manages, not on other DIMMs. Unplug of single memory blocks will result in similar fragmentation in respect to gigantic huge pages. I ahve plans for a virtio-mem defragmentation feature in the future.Q: How reliable is memory unplug?
A: There are no guarantees on how much memory can get unplugged
again. However, it is more likely to find 4MB chunks to unplug than e.g., 128MB chunks. If memory is terribly fragmented, there is nothing we can do - for now. I consider memory hotplug the first primary use of virtio-mem. Memory unplug might usually work, but we want to improve the performance and the amount of memory we can actually unplug later.Q: Why not unplug from the
ZONE_MOVABLE?
A: Unplugged memory chunks are unmovable. Unmovable data must not end up
on theZONE_MOVABLE- similar to gigantic pages - they will never be allocated fromZONE_MOVABLE. virtio-mem added memory can be onlined to theZONE_MOVABLE, but subblocks will not get unplugged from it.Q: How big should the initial (!virtio-mem) memory of a VM be?
A: virtio-mem memory will not go to the DMA zones. So to avoid running out
of DMA memory, I suggest something like 2-3GB on x86-64. But many VMs can most probably deal with less DMA memory - depends on the use case.[1] https://events.linuxfoundation.org/wp-content/uploads/2017/12/virtio-mem-Paravirtualized-Memory-David-Hildenbrand-Red-Hat-1.pdf
[2] https://lkml.kernel.org/r/[email protected]
[3] https://lkml.kernel.org/r/[email protected]Cc Sebastien Boeuf <[email protected]>Cc Samuel Ortiz <[email protected]>Cc Robert Bradford <[email protected]>Cc Luiz Capitulino <[email protected]>
Quoted 15 lines
David Hildenbrand (13):
ACPI: NUMA: exportpxm_to_node
virtio-mem: Paravirtualized memory hotplug
virtio-mem: Paravirtualized memory hotunplug part 1
mm: Exportalloc_contig_range()/free_contig_range()
virtio-mem: Paravirtualized memory hotunplug part 2
mm: Allow to offline unmovablePageOffline()pages via
MEM_GOING_OFFLINE
virtio-mem: Allow to offline partially unplugged memory blocksmm/memory_hotplug: Introduceoffline_and_remove_memory()
virtio-mem: Offline and remove completely unplugged memory blocks
virtio-mem: Better retry handling
mm/vmscan: Movecount_vm_event(DROP_SLAB)intodrop_slab()mm/vmscan: Exportdrop_slab()anddrop_slab_node()
virtio-mem: Drop slab objects when unplug continues to fail
Ping,
I'd love to get some feedback on
a) The remaining MM bits from MM folks (especially, patch #6 and #8).
b) The general virtio infrastructure (esp. uapi in patch #2) from virtio folks.
I'm planning to send a proper v1 (!RFC) once I have all necessary MM acks. In the meanwhile, I will do more testing and minor reworks (e.g., fix !CONFIG_NUMA compilation).
Signature · 3 lines
Thanks, David / dhildenb
Quoted 317 lines · David Hildenbrand
This series is based on latest linux-next. The patches are located at:
https://github.com/davidhildenbrand/linux.git virtio-mem-rfc-v4The basic idea of virtio-mem is to provide a flexible, cross-architecture memory hot(un)plug solution that avoids many limitations imposed by existing technologies, architectures, and interfaces. More details can be found below and in linked material.
This RFC is limited to x86-64, however, should theoretically work on any architecture that supports virtio and implements memory hot(un)plug under Linux - like s390x, powerpc64 and arm64. On x86-64, it is currently possible to add/remove memory to the system in >= 4MB granularity. Memory hotplug works very reliably. For memory unplug, there are no guarantees how much memory can actually get unplugged, it depends on the setup (especially: fragmentation of (unmovable) memory). I have plans to improve that in the future.
--------------------------------------------------------------------------
1. virtio-mem
--------------------------------------------------------------------------The basic idea behind virtio-mem was presented at KVM Forum 2018. The slides can be found at [1]. The previous RFC can be found at [2]. The first RFC can be found at [3]. However, the concept evolved over time. The KVM Forum slides roughly match the current design.
Patch #2 ("virtio-mem: Paravirtualized memory hotplug") contains quite some information, especially in "
include/uapi/linux/virtio_mem.h":Each virtio-mem device manages a dedicated region in physical address space. Each device can belong to a single NUMA node, multiple devices for a single NUMA node are possible. A virtio-mem device is like a "resizable DIMM" consisting of small memory blocks that can be plugged or unplugged. The device driver is responsible for (un)plugging memory blocks on demand.
Virtio-mem devices can only operate on their assigned memory region in order to (un)plug memory. A device cannot (un)plug memory belonging to other devices.
The "
region_size" corresponds to the maximum amount of memory that can be provided by a device. The "size" corresponds to the amount of memory that is currently plugged. "requested_size" corresponds to a request from the device to the device driver to (un)plug blocks. The device driver should try to (un)plug blocks in order to reach the "requested_size". It is impossible to plug more memory than requested.The "
usable_region_size" represents the memory region that can actually be used to (un)plug memory. It is always at least as big as the "requested_size" and will grow dynamically. It will only shrink when explicitly triggered (VIRTIO_MEM_REQ_UNPLUG).Memory in the usable region can usually be read, however, there are no guarantees. It can happen that the device cannot process a request, because it is busy. The device driver has to retry later.
Usually, during system resets all memory will get unplugged, so the device driver can start with a clean state. However, in specific scenarios (if the device is busy) it can happen that the device still has memory plugged. The device driver can request to unplug all memory (
VIRTIO_MEM_REQ_UNPLUG) - which might take a while to succeed if the device is busy.--------------------------------------------------------------------------
2. Linux Implementation
--------------------------------------------------------------------------This RFC reuses quite some existing MM infrastructure, however, has to expose some additional functionality.
Memory blocks (e.g., 128MB) are added/removed on demand. Within these memory blocks, subblocks (e.g., 4MB) are plugged/unplugged. The sizes depend on the target architecture,
MAX_ORDER+pageblock_order, and the block size of a virtio-mem device.add_memory()/try_remove_memory() is used to add/remove memory blocks. virtio-mem will not online memory blocks itself. This has to be done by user space, or configured into the kernel (
CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE). virtio-mem will only unplug memory that was online to theZONE_NORMAL. Memory is suggested to be onlined to theZONE_NORMALfor now.The memory hotplug notifier is used to properly synchronize against onlining/offlining of memory blocks and to track the states of memory blocks (including the zone memory blocks are onlined to).
The
set_online_page()callback is used to keep unplugged subblocks of a memory block fake-offline when onlining the memory block.generic_online_page()is used to fake-online plugged subblocks. This handling is similar to the Hyper-V balloon driver.
PG_offlineis used to mark unplugged subblocks as offline, so e.g., dumping tools (makedumpfile) will skip these pages. This is similar to other balloon drivers like virtio-balloon and Hyper-V.Memory offlining code is extended to allow drivers to drop their reference to
PG_offlinepages whenMEM_GOING_OFFLINE, so these pages can be skipped when offlining memory blocks. This allows to offline memory blocks that have partially unplugged (allocated e.g., viaalloc_contig_range()) subblocks - or are completely unplugged.alloc_contig_range()/free_contig_range() [now exposed] is used to unplug/plug subblocks of memory blocks the are already exposed to Linux.
offline_and_remove_memory()[new] is used to offline a fully unplugged memory block and remove it from Linux.A lot of additional information can be found in the separate patches and as comments in the code itself.
--------------------------------------------------------------------------
3. Changes RFC v2 -> v3
--------------------------------------------------------------------------A lot of things changed, especially also on the QEMU + virtio side. The biggest changes on the Linux driver side are:
- Onlining/offlining of subblocks is now emulated on top of memory blocks. set_online_page()+alloc_contig_range()+free_contig_range() is now used for that. Core MM does not have to be modified and will continue to online/offline full memory blocks.
- Onlining/offlining of memory blocks is no longer performed by virtio-mem.
-Pg_offlineis upstream and can be used. It is also used to allow offlining of partially unplugged memory blocks.
- Memory block states + subblocks are now tracked more space-efficient.
- Properkexec(),kdump(), driver unload, driver reload,ZONE_MOVABLE, ... handling.--------------------------------------------------------------------------
4. Changes RFC v3 -> RFC v4
--------------------------------------------------------------------------Only minor things changed, especially nothing on the QEMU + virtio side. Interresting changes on the Linux driver side are:
- "mm: Allow to offline unmovablePageOffline()pages viaMEM_GOING_OFFLINE"
-- Rework to Michals suggestion (allow to isolate allPageOffline()pages
by skipping allPageOffline()pages inhas_unmovable_pages(). Fail offlining later if the pages cannot be offlined/migrated).
- "virtio-mem: Allow to offline partially unplugged memory blocks" -- Adapt to Michals suggestion on core-mm part.
- "virtio-mem: Better retry handling"
-- Optimize retry intervals
- "virtio-mem: Drop slab objects when unplug continues to fail" -- Call drop_slab()/drop_slab_node() when unplug keeps failing for a longer time.
- Multiple cleanups and fixes.--------------------------------------------------------------------------
5. Future work
--------------------------------------------------------------------------The separate patches contain a lot of future work items. One of the next steps is to make memory unplug more likely to succeed - currently, there are no guarantees on how much memory can get unplugged again. I have various ideas on how to limit fragmentation of all memory blocks that virtio-mem added.
Memory hotplug:
- Reduce the amount of memory resources if that turnes out to be an issue. Or try to speed up relevant code paths to deal with many resources.
- Allocate the vmemmap from the added memory. Makes hotplug more likely to succeed, the vmemmap is stored on the same NUMA node and that unmovable memory will later not hinder unplug.Memory hotunplug:
- Performance improvements:
-- Sense (lockless) if it make sense to tryalloc_contig_range()at all
before directly trying to isolate and taking locks.
-- Try to unplug bigger chunks if possible first.
-- Identify free areas first, that don't have to be evacuated.
- Make unplug more likely to succeed:
-- There are various idea to limit fragmentation on memory block
granularity. (e.g.,ZONE_PREFER_MOVABLEand smart balancing)
-- Allocate memmap from added memory. This way, less unmovable data can
end up on the memory blocks.
- OOM handling, e.g., via an OOM handler.
- Defragmentation
-- Will require a new virtio-mem CMD to exchange plugged<->unplugged blocks--------------------------------------------------------------------------
6. Example Usage
--------------------------------------------------------------------------A very basic QEMU prototype (kept updated) is available at:
https://github.com/davidhildenbrand/qemu.git virtio-memIt lacks various features, however, works to test the guest driver side:
- No support for resizable memory regions / memory backends yet
- No protection of unplugged memory (esp., userfaultfd-wp) yet
- No dump/migration/XXX optimizations to skip unplugged memory (and avoid touching it)Start QEMU with two virtio-mem devices (one per NUMA node):
$ qemu-system-x86_64 -m 4G,maxmem=20G \ -smp sockets=2,cores=2 \ -numa node,nodeid=0,cpus=0-1 -numa node,nodeid=1,cpus=2-3 \ [...] -object memory-backend-ram,id=mem0,size=8G \ -device virtio-mem-pci,id=vm0,memdev=mem0,node=0,requested-size=128M \ -object memory-backend-ram,id=mem1,size=8G \ -device virtio-mem-pci,id=vm1,memdev=mem1,node=1,requested-size=80MQuery the configuration:
QEMU 4.1.95 monitor - type 'help' for more information (qemu) info memory-devices
Memory device [virtio-mem]: "vm0"
memaddr:0x140000000
node: 0
requested-size: 134217728
size: 134217728
max-size: 8589934592
block-size: 2097152
memdev: /objects/mem0
Memory device [virtio-mem]: "vm1"
memaddr:0x340000000
node: 1
requested-size: 83886080
size: 83886080
max-size: 8589934592
block-size: 2097152
memdev: /objects/mem1Add some memory to node 1:
QEMU 4.1.95 monitor - type 'help' for more information (qemu) qom-set vm1 requested-size 1GRemove some memory from node 0:
QEMU 4.1.95 monitor - type 'help' for more information (qemu) qom-set vm0 requested-size 64MQuery the configuration again:
(qemu) info memory-devices
Memory device [virtio-mem]: "vm0"
memaddr:0x140000000
node: 0
requested-size: 67108864
size: 67108864
max-size: 8589934592
block-size: 2097152
memdev: /objects/mem0
Memory device [virtio-mem]: "vm1"
memaddr:0x340000000
node: 1
requested-size: 1073741824
size: 1073741824
max-size: 8589934592
block-size: 2097152
memdev: /objects/mem1--------------------------------------------------------------------------
7. Q/A
--------------------------------------------------------------------------Q: Why add/remove parts ("subblocks") of memory blocks/sections?
A: Flexibility (section size depends on the architecture) - e.g., some
architectures have a section size of 2GB. Also, the memory block size is variable (e.g., on x86-64). I want to avoid any such restrictions. Some use cases want to add/remove memory in smaller granularities to a VM (e.g., the Hyper-V balloon also implements this) - especially smaller VMs like used for kata containers. Also, on memory unplug, it is more reliable to free-up and unplug multiple small chunks instead of one big chunk. E.g., if one page of a DIMM is either unmovable or pinned, the DIMM can't get unplugged. This approach is basically a compromise between DIMM-based memory hot(un)plug and balloon inflation/deflation, which works mostly on page granularity.Q: Why care about memory blocks?
A: They are the way to tell user space about new memory. This way,
memory can get onlined/offlined by user space. Also, e.g., kdump relies on udev events to reload kexec when memory blocks are onlined/offlined. Memory blocks are the "real" memory hot(un)plug granularity. Everything that's smaller has to be emulated "on top".Q: Won't memory unplug of subblocks fragment memory?
A: Yes and no. Unplugging e.g., >=4MB subblocks on x86-64 will not really
fragment memory like unplugging random pages like a balloon driver does. Buddy merging will not be limited. However, any allocation that requires bigger consecutive memory chunks (e.g., gigantic pages) might observe the fragmentation. Possible solutions: Allocate gigantic huge pages before unplugging memory, don't unplug memory, combine virtio-mem with DIMM based memory or bigger initial memory. Remember, a virtio-mem device will only unplug on the memory range it manages, not on other DIMMs. Unplug of single memory blocks will result in similar fragmentation in respect to gigantic huge pages. I ahve plans for a virtio-mem defragmentation feature in the future.Q: How reliable is memory unplug?
A: There are no guarantees on how much memory can get unplugged
again. However, it is more likely to find 4MB chunks to unplug than e.g., 128MB chunks. If memory is terribly fragmented, there is nothing we can do - for now. I consider memory hotplug the first primary use of virtio-mem. Memory unplug might usually work, but we want to improve the performance and the amount of memory we can actually unplug later.Q: Why not unplug from the
ZONE_MOVABLE?
A: Unplugged memory chunks are unmovable. Unmovable data must not end up
on theZONE_MOVABLE- similar to gigantic pages - they will never be allocated fromZONE_MOVABLE. virtio-mem added memory can be onlined to theZONE_MOVABLE, but subblocks will not get unplugged from it.Q: How big should the initial (!virtio-mem) memory of a VM be?
A: virtio-mem memory will not go to the DMA zones. So to avoid running out
of DMA memory, I suggest something like 2-3GB on x86-64. But many VMs can most probably deal with less DMA memory - depends on the use case.[1] https://events.linuxfoundation.org/wp-content/uploads/2017/12/virtio-mem-Paravirtualized-Memory-David-Hildenbrand-Red-Hat-1.pdf
[2] https://lkml.kernel.org/r/[email protected]
[3] https://lkml.kernel.org/r/[email protected]Cc Sebastien Boeuf <[email protected]>Cc Samuel Ortiz <[email protected]>Cc Robert Bradford <[email protected]>Cc Luiz Capitulino <[email protected]>
Quoted 15 lines
David Hildenbrand (13):
ACPI: NUMA: exportpxm_to_node
virtio-mem: Paravirtualized memory hotplug
virtio-mem: Paravirtualized memory hotunplug part 1
mm: Exportalloc_contig_range()/free_contig_range()
virtio-mem: Paravirtualized memory hotunplug part 2
mm: Allow to offline unmovablePageOffline()pages via
MEM_GOING_OFFLINE
virtio-mem: Allow to offline partially unplugged memory blocksmm/memory_hotplug: Introduceoffline_and_remove_memory()
virtio-mem: Offline and remove completely unplugged memory blocks
virtio-mem: Better retry handling
mm/vmscan: Movecount_vm_event(DROP_SLAB)intodrop_slab()mm/vmscan: Exportdrop_slab()anddrop_slab_node()
virtio-mem: Drop slab objects when unplug continues to fail
Ping,
I'd love to get some feedback on
a) The remaining MM bits from MM folks (especially, patch #6 and #8).
b) The general virtio infrastructure (esp. uapi in patch #2) from virtio folks.
I'm planning to send a proper v1 (!RFC) once I have all necessary MM acks. In the meanwhile, I will do more testing and minor reworks (e.g., fix !CONFIG_NUMA compilation).
Signature · 8 lines
Thanks, David / dhildenb --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
Quoted 334 lines · David Hildenbrand
Quoted 317 lines · David Hildenbrand
This series is based on latest linux-next. The patches are located at:
https://github.com/davidhildenbrand/linux.git virtio-mem-rfc-v4The basic idea of virtio-mem is to provide a flexible, cross-architecture memory hot(un)plug solution that avoids many limitations imposed by existing technologies, architectures, and interfaces. More details can be found below and in linked material.
This RFC is limited to x86-64, however, should theoretically work on any architecture that supports virtio and implements memory hot(un)plug under Linux - like s390x, powerpc64 and arm64. On x86-64, it is currently possible to add/remove memory to the system in >= 4MB granularity. Memory hotplug works very reliably. For memory unplug, there are no guarantees how much memory can actually get unplugged, it depends on the setup (especially: fragmentation of (unmovable) memory). I have plans to improve that in the future.
--------------------------------------------------------------------------
1. virtio-mem
--------------------------------------------------------------------------The basic idea behind virtio-mem was presented at KVM Forum 2018. The slides can be found at [1]. The previous RFC can be found at [2]. The first RFC can be found at [3]. However, the concept evolved over time. The KVM Forum slides roughly match the current design.
Patch #2 ("virtio-mem: Paravirtualized memory hotplug") contains quite some information, especially in "
include/uapi/linux/virtio_mem.h":Each virtio-mem device manages a dedicated region in physical address space. Each device can belong to a single NUMA node, multiple devices for a single NUMA node are possible. A virtio-mem device is like a "resizable DIMM" consisting of small memory blocks that can be plugged or unplugged. The device driver is responsible for (un)plugging memory blocks on demand.
Virtio-mem devices can only operate on their assigned memory region in order to (un)plug memory. A device cannot (un)plug memory belonging to other devices.
The "
region_size" corresponds to the maximum amount of memory that can be provided by a device. The "size" corresponds to the amount of memory that is currently plugged. "requested_size" corresponds to a request from the device to the device driver to (un)plug blocks. The device driver should try to (un)plug blocks in order to reach the "requested_size". It is impossible to plug more memory than requested.The "
usable_region_size" represents the memory region that can actually be used to (un)plug memory. It is always at least as big as the "requested_size" and will grow dynamically. It will only shrink when explicitly triggered (VIRTIO_MEM_REQ_UNPLUG).Memory in the usable region can usually be read, however, there are no guarantees. It can happen that the device cannot process a request, because it is busy. The device driver has to retry later.
Usually, during system resets all memory will get unplugged, so the device driver can start with a clean state. However, in specific scenarios (if the device is busy) it can happen that the device still has memory plugged. The device driver can request to unplug all memory (
VIRTIO_MEM_REQ_UNPLUG) - which might take a while to succeed if the device is busy.--------------------------------------------------------------------------
2. Linux Implementation
--------------------------------------------------------------------------This RFC reuses quite some existing MM infrastructure, however, has to expose some additional functionality.
Memory blocks (e.g., 128MB) are added/removed on demand. Within these memory blocks, subblocks (e.g., 4MB) are plugged/unplugged. The sizes depend on the target architecture,
MAX_ORDER+pageblock_order, and the block size of a virtio-mem device.add_memory()/try_remove_memory() is used to add/remove memory blocks. virtio-mem will not online memory blocks itself. This has to be done by user space, or configured into the kernel (
CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE). virtio-mem will only unplug memory that was online to theZONE_NORMAL. Memory is suggested to be onlined to theZONE_NORMALfor now.The memory hotplug notifier is used to properly synchronize against onlining/offlining of memory blocks and to track the states of memory blocks (including the zone memory blocks are onlined to).
The
set_online_page()callback is used to keep unplugged subblocks of a memory block fake-offline when onlining the memory block.generic_online_page()is used to fake-online plugged subblocks. This handling is similar to the Hyper-V balloon driver.
PG_offlineis used to mark unplugged subblocks as offline, so e.g., dumping tools (makedumpfile) will skip these pages. This is similar to other balloon drivers like virtio-balloon and Hyper-V.Memory offlining code is extended to allow drivers to drop their reference to
PG_offlinepages whenMEM_GOING_OFFLINE, so these pages can be skipped when offlining memory blocks. This allows to offline memory blocks that have partially unplugged (allocated e.g., viaalloc_contig_range()) subblocks - or are completely unplugged.alloc_contig_range()/free_contig_range() [now exposed] is used to unplug/plug subblocks of memory blocks the are already exposed to Linux.
offline_and_remove_memory()[new] is used to offline a fully unplugged memory block and remove it from Linux.A lot of additional information can be found in the separate patches and as comments in the code itself.
--------------------------------------------------------------------------
3. Changes RFC v2 -> v3
--------------------------------------------------------------------------A lot of things changed, especially also on the QEMU + virtio side. The biggest changes on the Linux driver side are:
- Onlining/offlining of subblocks is now emulated on top of memory blocks. set_online_page()+alloc_contig_range()+free_contig_range() is now used for that. Core MM does not have to be modified and will continue to online/offline full memory blocks.
- Onlining/offlining of memory blocks is no longer performed by virtio-mem.
-Pg_offlineis upstream and can be used. It is also used to allow offlining of partially unplugged memory blocks.
- Memory block states + subblocks are now tracked more space-efficient.
- Properkexec(),kdump(), driver unload, driver reload,ZONE_MOVABLE, ... handling.--------------------------------------------------------------------------
4. Changes RFC v3 -> RFC v4
--------------------------------------------------------------------------Only minor things changed, especially nothing on the QEMU + virtio side. Interresting changes on the Linux driver side are:
- "mm: Allow to offline unmovablePageOffline()pages viaMEM_GOING_OFFLINE"
-- Rework to Michals suggestion (allow to isolate allPageOffline()pages
by skipping allPageOffline()pages inhas_unmovable_pages(). Fail offlining later if the pages cannot be offlined/migrated).
- "virtio-mem: Allow to offline partially unplugged memory blocks" -- Adapt to Michals suggestion on core-mm part.
- "virtio-mem: Better retry handling"
-- Optimize retry intervals
- "virtio-mem: Drop slab objects when unplug continues to fail" -- Call drop_slab()/drop_slab_node() when unplug keeps failing for a longer time.
- Multiple cleanups and fixes.--------------------------------------------------------------------------
5. Future work
--------------------------------------------------------------------------The separate patches contain a lot of future work items. One of the next steps is to make memory unplug more likely to succeed - currently, there are no guarantees on how much memory can get unplugged again. I have various ideas on how to limit fragmentation of all memory blocks that virtio-mem added.
Memory hotplug:
- Reduce the amount of memory resources if that turnes out to be an issue. Or try to speed up relevant code paths to deal with many resources.
- Allocate the vmemmap from the added memory. Makes hotplug more likely to succeed, the vmemmap is stored on the same NUMA node and that unmovable memory will later not hinder unplug.Memory hotunplug:
- Performance improvements:
-- Sense (lockless) if it make sense to tryalloc_contig_range()at all
before directly trying to isolate and taking locks.
-- Try to unplug bigger chunks if possible first.
-- Identify free areas first, that don't have to be evacuated.
- Make unplug more likely to succeed:
-- There are various idea to limit fragmentation on memory block
granularity. (e.g.,ZONE_PREFER_MOVABLEand smart balancing)
-- Allocate memmap from added memory. This way, less unmovable data can
end up on the memory blocks.
- OOM handling, e.g., via an OOM handler.
- Defragmentation
-- Will require a new virtio-mem CMD to exchange plugged<->unplugged blocks--------------------------------------------------------------------------
6. Example Usage
--------------------------------------------------------------------------A very basic QEMU prototype (kept updated) is available at:
https://github.com/davidhildenbrand/qemu.git virtio-memIt lacks various features, however, works to test the guest driver side:
- No support for resizable memory regions / memory backends yet
- No protection of unplugged memory (esp., userfaultfd-wp) yet
- No dump/migration/XXX optimizations to skip unplugged memory (and avoid touching it)Start QEMU with two virtio-mem devices (one per NUMA node):
$ qemu-system-x86_64 -m 4G,maxmem=20G \ -smp sockets=2,cores=2 \ -numa node,nodeid=0,cpus=0-1 -numa node,nodeid=1,cpus=2-3 \ [...] -object memory-backend-ram,id=mem0,size=8G \ -device virtio-mem-pci,id=vm0,memdev=mem0,node=0,requested-size=128M \ -object memory-backend-ram,id=mem1,size=8G \ -device virtio-mem-pci,id=vm1,memdev=mem1,node=1,requested-size=80MQuery the configuration:
QEMU 4.1.95 monitor - type 'help' for more information (qemu) info memory-devices
Memory device [virtio-mem]: "vm0"
memaddr:0x140000000
node: 0
requested-size: 134217728
size: 134217728
max-size: 8589934592
block-size: 2097152
memdev: /objects/mem0
Memory device [virtio-mem]: "vm1"
memaddr:0x340000000
node: 1
requested-size: 83886080
size: 83886080
max-size: 8589934592
block-size: 2097152
memdev: /objects/mem1Add some memory to node 1:
QEMU 4.1.95 monitor - type 'help' for more information (qemu) qom-set vm1 requested-size 1GRemove some memory from node 0:
QEMU 4.1.95 monitor - type 'help' for more information (qemu) qom-set vm0 requested-size 64MQuery the configuration again:
(qemu) info memory-devices
Memory device [virtio-mem]: "vm0"
memaddr:0x140000000
node: 0
requested-size: 67108864
size: 67108864
max-size: 8589934592
block-size: 2097152
memdev: /objects/mem0
Memory device [virtio-mem]: "vm1"
memaddr:0x340000000
node: 1
requested-size: 1073741824
size: 1073741824
max-size: 8589934592
block-size: 2097152
memdev: /objects/mem1--------------------------------------------------------------------------
7. Q/A
--------------------------------------------------------------------------Q: Why add/remove parts ("subblocks") of memory blocks/sections?
A: Flexibility (section size depends on the architecture) - e.g., some
architectures have a section size of 2GB. Also, the memory block size is variable (e.g., on x86-64). I want to avoid any such restrictions. Some use cases want to add/remove memory in smaller granularities to a VM (e.g., the Hyper-V balloon also implements this) - especially smaller VMs like used for kata containers. Also, on memory unplug, it is more reliable to free-up and unplug multiple small chunks instead of one big chunk. E.g., if one page of a DIMM is either unmovable or pinned, the DIMM can't get unplugged. This approach is basically a compromise between DIMM-based memory hot(un)plug and balloon inflation/deflation, which works mostly on page granularity.Q: Why care about memory blocks?
A: They are the way to tell user space about new memory. This way,
memory can get onlined/offlined by user space. Also, e.g., kdump relies on udev events to reload kexec when memory blocks are onlined/offlined. Memory blocks are the "real" memory hot(un)plug granularity. Everything that's smaller has to be emulated "on top".Q: Won't memory unplug of subblocks fragment memory?
A: Yes and no. Unplugging e.g., >=4MB subblocks on x86-64 will not really
fragment memory like unplugging random pages like a balloon driver does. Buddy merging will not be limited. However, any allocation that requires bigger consecutive memory chunks (e.g., gigantic pages) might observe the fragmentation. Possible solutions: Allocate gigantic huge pages before unplugging memory, don't unplug memory, combine virtio-mem with DIMM based memory or bigger initial memory. Remember, a virtio-mem device will only unplug on the memory range it manages, not on other DIMMs. Unplug of single memory blocks will result in similar fragmentation in respect to gigantic huge pages. I ahve plans for a virtio-mem defragmentation feature in the future.Q: How reliable is memory unplug?
A: There are no guarantees on how much memory can get unplugged
again. However, it is more likely to find 4MB chunks to unplug than e.g., 128MB chunks. If memory is terribly fragmented, there is nothing we can do - for now. I consider memory hotplug the first primary use of virtio-mem. Memory unplug might usually work, but we want to improve the performance and the amount of memory we can actually unplug later.Q: Why not unplug from the
ZONE_MOVABLE?
A: Unplugged memory chunks are unmovable. Unmovable data must not end up
on theZONE_MOVABLE- similar to gigantic pages - they will never be allocated fromZONE_MOVABLE. virtio-mem added memory can be onlined to theZONE_MOVABLE, but subblocks will not get unplugged from it.Q: How big should the initial (!virtio-mem) memory of a VM be?
A: virtio-mem memory will not go to the DMA zones. So to avoid running out
of DMA memory, I suggest something like 2-3GB on x86-64. But many VMs can most probably deal with less DMA memory - depends on the use case.[1] https://events.linuxfoundation.org/wp-content/uploads/2017/12/virtio-mem-Paravirtualized-Memory-David-Hildenbrand-Red-Hat-1.pdf
[2] https://lkml.kernel.org/r/[email protected]
[3] https://lkml.kernel.org/r/[email protected]Cc Sebastien Boeuf <[email protected]>Cc Samuel Ortiz <[email protected]>Cc Robert Bradford <[email protected]>Cc Luiz Capitulino <[email protected]>Quoted 15 lines
David Hildenbrand (13):
ACPI: NUMA: exportpxm_to_node
virtio-mem: Paravirtualized memory hotplug
virtio-mem: Paravirtualized memory hotunplug part 1
mm: Exportalloc_contig_range()/free_contig_range()
virtio-mem: Paravirtualized memory hotunplug part 2
mm: Allow to offline unmovablePageOffline()pages via
MEM_GOING_OFFLINE
virtio-mem: Allow to offline partially unplugged memory blocksmm/memory_hotplug: Introduceoffline_and_remove_memory()
virtio-mem: Offline and remove completely unplugged memory blocks
virtio-mem: Better retry handling
mm/vmscan: Movecount_vm_event(DROP_SLAB)intodrop_slab()mm/vmscan: Exportdrop_slab()anddrop_slab_node()
virtio-mem: Drop slab objects when unplug continues to fail
Quoted 5 lines
Ping,
I'd love to get some feedback on
a) The remaining MM bits from MM folks (especially, patch #6 and #8).
Friendly ping again:
Can I get some feedback on the two important MM changes in this series
"[PATCH RFC v4 06/13] mm: Allow to offline unmovable PageOffline() pages via MEM_GOING_OFFLINE"
and
"[PATCH RFC v4 08/13] mm/memory_hotplug: Introduce offline_and_remove_memory()"
Signature · 3 lines
Thanks, David / dhildenb
Quoted 345 lines · David Hildenbrand
Quoted 334 lines · David Hildenbrand
Quoted 317 lines · David Hildenbrand
This series is based on latest linux-next. The patches are located at:
https://github.com/davidhildenbrand/linux.git virtio-mem-rfc-v4The basic idea of virtio-mem is to provide a flexible, cross-architecture memory hot(un)plug solution that avoids many limitations imposed by existing technologies, architectures, and interfaces. More details can be found below and in linked material.
This RFC is limited to x86-64, however, should theoretically work on any architecture that supports virtio and implements memory hot(un)plug under Linux - like s390x, powerpc64 and arm64. On x86-64, it is currently possible to add/remove memory to the system in >= 4MB granularity. Memory hotplug works very reliably. For memory unplug, there are no guarantees how much memory can actually get unplugged, it depends on the setup (especially: fragmentation of (unmovable) memory). I have plans to improve that in the future.
--------------------------------------------------------------------------
1. virtio-mem
--------------------------------------------------------------------------The basic idea behind virtio-mem was presented at KVM Forum 2018. The slides can be found at [1]. The previous RFC can be found at [2]. The first RFC can be found at [3]. However, the concept evolved over time. The KVM Forum slides roughly match the current design.
Patch #2 ("virtio-mem: Paravirtualized memory hotplug") contains quite some information, especially in "
include/uapi/linux/virtio_mem.h":Each virtio-mem device manages a dedicated region in physical address space. Each device can belong to a single NUMA node, multiple devices for a single NUMA node are possible. A virtio-mem device is like a "resizable DIMM" consisting of small memory blocks that can be plugged or unplugged. The device driver is responsible for (un)plugging memory blocks on demand.
Virtio-mem devices can only operate on their assigned memory region in order to (un)plug memory. A device cannot (un)plug memory belonging to other devices.
The "
region_size" corresponds to the maximum amount of memory that can be provided by a device. The "size" corresponds to the amount of memory that is currently plugged. "requested_size" corresponds to a request from the device to the device driver to (un)plug blocks. The device driver should try to (un)plug blocks in order to reach the "requested_size". It is impossible to plug more memory than requested.The "
usable_region_size" represents the memory region that can actually be used to (un)plug memory. It is always at least as big as the "requested_size" and will grow dynamically. It will only shrink when explicitly triggered (VIRTIO_MEM_REQ_UNPLUG).Memory in the usable region can usually be read, however, there are no guarantees. It can happen that the device cannot process a request, because it is busy. The device driver has to retry later.
Usually, during system resets all memory will get unplugged, so the device driver can start with a clean state. However, in specific scenarios (if the device is busy) it can happen that the device still has memory plugged. The device driver can request to unplug all memory (
VIRTIO_MEM_REQ_UNPLUG) - which might take a while to succeed if the device is busy.--------------------------------------------------------------------------
2. Linux Implementation
--------------------------------------------------------------------------This RFC reuses quite some existing MM infrastructure, however, has to expose some additional functionality.
Memory blocks (e.g., 128MB) are added/removed on demand. Within these memory blocks, subblocks (e.g., 4MB) are plugged/unplugged. The sizes depend on the target architecture,
MAX_ORDER+pageblock_order, and the block size of a virtio-mem device.add_memory()/try_remove_memory() is used to add/remove memory blocks. virtio-mem will not online memory blocks itself. This has to be done by user space, or configured into the kernel (
CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE). virtio-mem will only unplug memory that was online to theZONE_NORMAL. Memory is suggested to be onlined to theZONE_NORMALfor now.The memory hotplug notifier is used to properly synchronize against onlining/offlining of memory blocks and to track the states of memory blocks (including the zone memory blocks are onlined to).
The
set_online_page()callback is used to keep unplugged subblocks of a memory block fake-offline when onlining the memory block.generic_online_page()is used to fake-online plugged subblocks. This handling is similar to the Hyper-V balloon driver.
PG_offlineis used to mark unplugged subblocks as offline, so e.g., dumping tools (makedumpfile) will skip these pages. This is similar to other balloon drivers like virtio-balloon and Hyper-V.Memory offlining code is extended to allow drivers to drop their reference to
PG_offlinepages whenMEM_GOING_OFFLINE, so these pages can be skipped when offlining memory blocks. This allows to offline memory blocks that have partially unplugged (allocated e.g., viaalloc_contig_range()) subblocks - or are completely unplugged.alloc_contig_range()/free_contig_range() [now exposed] is used to unplug/plug subblocks of memory blocks the are already exposed to Linux.
offline_and_remove_memory()[new] is used to offline a fully unplugged memory block and remove it from Linux.A lot of additional information can be found in the separate patches and as comments in the code itself.
--------------------------------------------------------------------------
3. Changes RFC v2 -> v3
--------------------------------------------------------------------------A lot of things changed, especially also on the QEMU + virtio side. The biggest changes on the Linux driver side are:
- Onlining/offlining of subblocks is now emulated on top of memory blocks. set_online_page()+alloc_contig_range()+free_contig_range() is now used for that. Core MM does not have to be modified and will continue to online/offline full memory blocks.
- Onlining/offlining of memory blocks is no longer performed by virtio-mem.
-Pg_offlineis upstream and can be used. It is also used to allow offlining of partially unplugged memory blocks.
- Memory block states + subblocks are now tracked more space-efficient.
- Properkexec(),kdump(), driver unload, driver reload,ZONE_MOVABLE, ... handling.--------------------------------------------------------------------------
4. Changes RFC v3 -> RFC v4
--------------------------------------------------------------------------Only minor things changed, especially nothing on the QEMU + virtio side. Interresting changes on the Linux driver side are:
- "mm: Allow to offline unmovablePageOffline()pages viaMEM_GOING_OFFLINE"
-- Rework to Michals suggestion (allow to isolate allPageOffline()pages
by skipping allPageOffline()pages inhas_unmovable_pages(). Fail offlining later if the pages cannot be offlined/migrated).
- "virtio-mem: Allow to offline partially unplugged memory blocks" -- Adapt to Michals suggestion on core-mm part.
- "virtio-mem: Better retry handling"
-- Optimize retry intervals
- "virtio-mem: Drop slab objects when unplug continues to fail" -- Call drop_slab()/drop_slab_node() when unplug keeps failing for a longer time.
- Multiple cleanups and fixes.--------------------------------------------------------------------------
5. Future work
--------------------------------------------------------------------------The separate patches contain a lot of future work items. One of the next steps is to make memory unplug more likely to succeed - currently, there are no guarantees on how much memory can get unplugged again. I have various ideas on how to limit fragmentation of all memory blocks that virtio-mem added.
Memory hotplug:
- Reduce the amount of memory resources if that turnes out to be an issue. Or try to speed up relevant code paths to deal with many resources.
- Allocate the vmemmap from the added memory. Makes hotplug more likely to succeed, the vmemmap is stored on the same NUMA node and that unmovable memory will later not hinder unplug.Memory hotunplug:
- Performance improvements:
-- Sense (lockless) if it make sense to tryalloc_contig_range()at all
before directly trying to isolate and taking locks.
-- Try to unplug bigger chunks if possible first.
-- Identify free areas first, that don't have to be evacuated.
- Make unplug more likely to succeed:
-- There are various idea to limit fragmentation on memory block
granularity. (e.g.,ZONE_PREFER_MOVABLEand smart balancing)
-- Allocate memmap from added memory. This way, less unmovable data can
end up on the memory blocks.
- OOM handling, e.g., via an OOM handler.
- Defragmentation
-- Will require a new virtio-mem CMD to exchange plugged<->unplugged blocks--------------------------------------------------------------------------
6. Example Usage
--------------------------------------------------------------------------A very basic QEMU prototype (kept updated) is available at:
https://github.com/davidhildenbrand/qemu.git virtio-memIt lacks various features, however, works to test the guest driver side:
- No support for resizable memory regions / memory backends yet
- No protection of unplugged memory (esp., userfaultfd-wp) yet
- No dump/migration/XXX optimizations to skip unplugged memory (and avoid touching it)Start QEMU with two virtio-mem devices (one per NUMA node):
$ qemu-system-x86_64 -m 4G,maxmem=20G \ -smp sockets=2,cores=2 \ -numa node,nodeid=0,cpus=0-1 -numa node,nodeid=1,cpus=2-3 \ [...] -object memory-backend-ram,id=mem0,size=8G \ -device virtio-mem-pci,id=vm0,memdev=mem0,node=0,requested-size=128M \ -object memory-backend-ram,id=mem1,size=8G \ -device virtio-mem-pci,id=vm1,memdev=mem1,node=1,requested-size=80MQuery the configuration:
QEMU 4.1.95 monitor - type 'help' for more information (qemu) info memory-devices
Memory device [virtio-mem]: "vm0"
memaddr:0x140000000
node: 0
requested-size: 134217728
size: 134217728
max-size: 8589934592
block-size: 2097152
memdev: /objects/mem0
Memory device [virtio-mem]: "vm1"
memaddr:0x340000000
node: 1
requested-size: 83886080
size: 83886080
max-size: 8589934592
block-size: 2097152
memdev: /objects/mem1Add some memory to node 1:
QEMU 4.1.95 monitor - type 'help' for more information (qemu) qom-set vm1 requested-size 1GRemove some memory from node 0:
QEMU 4.1.95 monitor - type 'help' for more information (qemu) qom-set vm0 requested-size 64MQuery the configuration again:
(qemu) info memory-devices
Memory device [virtio-mem]: "vm0"
memaddr:0x140000000
node: 0
requested-size: 67108864
size: 67108864
max-size: 8589934592
block-size: 2097152
memdev: /objects/mem0
Memory device [virtio-mem]: "vm1"
memaddr:0x340000000
node: 1
requested-size: 1073741824
size: 1073741824
max-size: 8589934592
block-size: 2097152
memdev: /objects/mem1--------------------------------------------------------------------------
7. Q/A
--------------------------------------------------------------------------Q: Why add/remove parts ("subblocks") of memory blocks/sections?
A: Flexibility (section size depends on the architecture) - e.g., some
architectures have a section size of 2GB. Also, the memory block size is variable (e.g., on x86-64). I want to avoid any such restrictions. Some use cases want to add/remove memory in smaller granularities to a VM (e.g., the Hyper-V balloon also implements this) - especially smaller VMs like used for kata containers. Also, on memory unplug, it is more reliable to free-up and unplug multiple small chunks instead of one big chunk. E.g., if one page of a DIMM is either unmovable or pinned, the DIMM can't get unplugged. This approach is basically a compromise between DIMM-based memory hot(un)plug and balloon inflation/deflation, which works mostly on page granularity.Q: Why care about memory blocks?
A: They are the way to tell user space about new memory. This way,
memory can get onlined/offlined by user space. Also, e.g., kdump relies on udev events to reload kexec when memory blocks are onlined/offlined. Memory blocks are the "real" memory hot(un)plug granularity. Everything that's smaller has to be emulated "on top".Q: Won't memory unplug of subblocks fragment memory?
A: Yes and no. Unplugging e.g., >=4MB subblocks on x86-64 will not really
fragment memory like unplugging random pages like a balloon driver does. Buddy merging will not be limited. However, any allocation that requires bigger consecutive memory chunks (e.g., gigantic pages) might observe the fragmentation. Possible solutions: Allocate gigantic huge pages before unplugging memory, don't unplug memory, combine virtio-mem with DIMM based memory or bigger initial memory. Remember, a virtio-mem device will only unplug on the memory range it manages, not on other DIMMs. Unplug of single memory blocks will result in similar fragmentation in respect to gigantic huge pages. I ahve plans for a virtio-mem defragmentation feature in the future.Q: How reliable is memory unplug?
A: There are no guarantees on how much memory can get unplugged
again. However, it is more likely to find 4MB chunks to unplug than e.g., 128MB chunks. If memory is terribly fragmented, there is nothing we can do - for now. I consider memory hotplug the first primary use of virtio-mem. Memory unplug might usually work, but we want to improve the performance and the amount of memory we can actually unplug later.Q: Why not unplug from the
ZONE_MOVABLE?
A: Unplugged memory chunks are unmovable. Unmovable data must not end up
on theZONE_MOVABLE- similar to gigantic pages - they will never be allocated fromZONE_MOVABLE. virtio-mem added memory can be onlined to theZONE_MOVABLE, but subblocks will not get unplugged from it.Q: How big should the initial (!virtio-mem) memory of a VM be?
A: virtio-mem memory will not go to the DMA zones. So to avoid running out
of DMA memory, I suggest something like 2-3GB on x86-64. But many VMs can most probably deal with less DMA memory - depends on the use case.[1] https://events.linuxfoundation.org/wp-content/uploads/2017/12/virtio-mem-Paravirtualized-Memory-David-Hildenbrand-Red-Hat-1.pdf
[2] https://lkml.kernel.org/r/[email protected]
[3] https://lkml.kernel.org/r/[email protected]Cc Sebastien Boeuf <[email protected]>Cc Samuel Ortiz <[email protected]>Cc Robert Bradford <[email protected]>Cc Luiz Capitulino <[email protected]>Quoted 15 lines
David Hildenbrand (13):
ACPI: NUMA: exportpxm_to_node
virtio-mem: Paravirtualized memory hotplug
virtio-mem: Paravirtualized memory hotunplug part 1
mm: Exportalloc_contig_range()/free_contig_range()
virtio-mem: Paravirtualized memory hotunplug part 2
mm: Allow to offline unmovablePageOffline()pages via
MEM_GOING_OFFLINE
virtio-mem: Allow to offline partially unplugged memory blocksmm/memory_hotplug: Introduceoffline_and_remove_memory()
virtio-mem: Offline and remove completely unplugged memory blocks
virtio-mem: Better retry handling
mm/vmscan: Movecount_vm_event(DROP_SLAB)intodrop_slab()mm/vmscan: Exportdrop_slab()anddrop_slab_node()
virtio-mem: Drop slab objects when unplug continues to failQuoted 5 lines
Ping,
I'd love to get some feedback on
a) The remaining MM bits from MM folks (especially, patch #6 and #8).
Friendly ping again:
Can I get some feedback on the two important MM changes in this series
Quoted 8 lines
"[PATCH RFC v4 06/13] mm: Allow to offline unmovable
PageOffline()pages viaMEM_GOING_OFFLINE"and
"[PATCH RFC v4 08/13]
mm/memory_hotplug: Introduceoffline_and_remove_memory()"
Yet another ping.
Signature · 3 lines
Thanks, David / dhildenb
Quoted 11 lines · David Hildenbrand
Ping,
I'd love to get some feedback on
a) The remaining MM bits from MM folks (especially, patch #6 and #8).
b) The general virtio infrastructure (esp. uapi in patch #2) from virtio folks.I'm planning to send a proper v1 (!RFC) once I have all necessary MM acks. In the meanwhile, I will do more testing and minor reworks (e.g., fix !CONFIG_NUMA compilation).
Hi David,
Thanks for your work!
I am trying your https://github.com/davidhildenbrand/linux.git virtio-mem-v5 which works fine for me, but just a 'DMA error' happens when a vm start with less than 2GB memory, Do I missed sth?
Thanks
Alex
(qemu) qom-set vm0 requested-size 1g
(qemu) [ 26.560026] virtio_mem virtio0: plugged size: 0x0
[ 26.560648] virtio_mem virtio0: requested size: 0x40000000 [ 26.561730] systemd-journald[167]: no db file to read /run/udev/data/+virtio:virtio0: No such file or directory [ 26.563138] systemd-journald[167]: no db file to read /run/udev/data/+virtio:virtio0: No such file or directory [ 26.569122] Built 1 zonelists, mobility grouping on. Total pages: 513141 [ 26.570039] Policy zone: Normal
(qemu) [ 32.175838] e1000 0000:00:03.0: swiotlb buffer is full (sz: 81 bytes), total 0 (slots), used 0 (slots)
[ 32.176922] e1000 0000:00:03.0: TX DMA map failed [ 32.177488] e1000 0000:00:03.0: swiotlb buffer is full (sz: 81 bytes), total 0 (slots), used 0 (slots) [ 32.178535] e1000 0000:00:03.0: TX DMA map failed
my qemu command is like this:
qemu-system-x86_64 --enable-kvm \ -m 2G,maxmem=16G -kernel /root/linux-next/$1/arch/x86/boot/bzImage \ -smp 4 \ -append "earlyprintk=ttyS0 root=/dev/sda1 console=ttyS0 debug psi=1 nokaslr ignore_loglevel" \ -hda /root/CentOS-7-x86_64-Azure-1703.qcow2 \ -net user,hostfwd=tcp::2222-:22 -net nic -s \
-object memory-backend-ram,id=mem0,size=3G \
-device virtio-mem-pci,id=vm0,memdev=mem0,node=0,requested-size=0M \
--nographic
Quoted 16 lines · Alex Shi
Quoted 11 lines · David Hildenbrand
Ping,
I'd love to get some feedback on
a) The remaining MM bits from MM folks (especially, patch #6 and #8).
b) The general virtio infrastructure (esp. uapi in patch #2) from virtio folks.I'm planning to send a proper v1 (!RFC) once I have all necessary MM acks. In the meanwhile, I will do more testing and minor reworks (e.g., fix !CONFIG_NUMA compilation).
Quoted 7 lines
Hi David,
Thanks for your work!
I am trying your https://github.com/davidhildenbrand/linux.git virtio-mem-v5 which works fine for me, but just a 'DMA error' happens when a vm start with less than 2GB memory, Do I missed sth?
Please use the virtio-mem-v4 branch for now, v5 is still under construction (and might be scrapped completely if v4 goes upstream as is).
Looks like a DMA issue. Your're hotplugging 1GB, which should not really eat too much memory. There was a similar issue reported by Hui in [1], which boiled down to wrong usage of the swiotlb parameter.
In such cases you should always try to reproduce with hotplug of a sam-sized DIMM. E.g., hotplugging a 1GB DIMM should result in the same issue.
What does your .config specify for CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE?
I'll try to reproduce with v4 briefly.
[1]
https://lkml.kernel.org/r/[email protected]
Quoted 30 lines
Thanks
Alex(qemu) qom-set vm0 requested-size 1g
(qemu) [ 26.560026]virtio_memvirtio0: plugged size:0x0[ 26.560648] virtio_mem virtio0: requested size: 0x40000000 [ 26.561730] systemd-journald[167]: no db file to read /run/udev/data/+virtio:virtio0: No such file or directory [ 26.563138] systemd-journald[167]: no db file to read /run/udev/data/+virtio:virtio0: No such file or directory [ 26.569122] Built 1 zonelists, mobility grouping on. Total pages: 513141 [ 26.570039] Policy zone: Normal(qemu) [ 32.175838] e1000 0000:00:03.0: swiotlb buffer is full (sz: 81 bytes), total 0 (slots), used 0 (slots)
[ 32.176922] e1000 0000:00:03.0: TX DMA map failed [ 32.177488] e1000 0000:00:03.0: swiotlb buffer is full (sz: 81 bytes), total 0 (slots), used 0 (slots) [ 32.178535] e1000 0000:00:03.0: TX DMA map failedmy qemu command is like this:
qemu-system-x86_64 --enable-kvm \ -m 2G,maxmem=16G -kernel /root/linux-next/$1/arch/x86/boot/bzImage \ -smp 4 \ -append "earlyprintk=ttyS0 root=/dev/sda1 console=ttyS0 debug psi=1 nokaslr ignore_loglevel" \ -hda /root/CentOS-7-x86_64-Azure-1703.qcow2 \ -net user,hostfwd=tcp::2222-:22 -net nic -s \-object memory-backend-ram,id=mem0,size=3G \
-device virtio-mem-pci,id=vm0,memdev=mem0,node=0,requested-size=0M \
--nographic
Signature · 3 lines
Thanks, David / dhildenb
Quoted 32 lines · David Hildenbrand
Quoted 16 lines · Alex Shi
Quoted 11 lines · David Hildenbrand
Ping,
I'd love to get some feedback on
a) The remaining MM bits from MM folks (especially, patch #6 and #8).
b) The general virtio infrastructure (esp. uapi in patch #2) from virtio folks.I'm planning to send a proper v1 (!RFC) once I have all necessary MM acks. In the meanwhile, I will do more testing and minor reworks (e.g., fix !CONFIG_NUMA compilation).
Quoted 7 lines
Hi David,
Thanks for your work!
I am trying your https://github.com/davidhildenbrand/linux.git virtio-mem-v5 which works fine for me, but just a 'DMA error' happens when a vm start with less than 2GB memory, Do I missed sth?
Please use the virtio-mem-v4 branch for now, v5 is still under construction (and might be scrapped completely if v4 goes upstream as is).
Looks like a DMA issue. Your're hotplugging 1GB, which should not really eat too much memory. There was a similar issue reported by Hui in [1], which boiled down to wrong usage of the swiotlb parameter.
Quoted 7 lines
In such cases you should always try to reproduce with hotplug of a sam-sized DIMM. E.g., hotplugging a 1GB DIMM should result in the same issue.
What does your .config specify for
CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE?I'll try to reproduce with v4 briefly.
I guess I know what's happening here. In case we only have DMA memory when booting, we don't reserve swiotlb buffers. Once we hotplug memory and online ZONE_NORMAL, we don't have any swiotlb DMA bounce buffers to map such PFNs (total 0 (slots), used 0 (slots)).
Can you try with "swiotlb=force" on the kernel cmdline?
Signature · 3 lines
Thanks, David / dhildenb
Quoted 39 lines · David Hildenbrand
Quoted 38 lines · David Hildenbrand
Quoted 16 lines · Alex Shi
Quoted 11 lines · David Hildenbrand
Ping,
I'd love to get some feedback on
a) The remaining MM bits from MM folks (especially, patch #6 and #8).
b) The general virtio infrastructure (esp. uapi in patch #2) from virtio folks.I'm planning to send a proper v1 (!RFC) once I have all necessary MM acks. In the meanwhile, I will do more testing and minor reworks (e.g., fix !CONFIG_NUMA compilation).
Quoted 7 lines
Hi David,
Thanks for your work!
I am trying your https://github.com/davidhildenbrand/linux.git virtio-mem-v5 which works fine for me, but just a 'DMA error' happens when a vm start with less than 2GB memory, Do I missed sth?
Please use the virtio-mem-v4 branch for now, v5 is still under construction (and might be scrapped completely if v4 goes upstream as is).
Looks like a DMA issue. Your're hotplugging 1GB, which should not really eat too much memory. There was a similar issue reported by Hui in [1], which boiled down to wrong usage of the swiotlb parameter.
In such cases you should always try to reproduce with hotplug of a sam-sized DIMM. E.g., hotplugging a 1GB DIMM should result in the same issue.
What does your .config specify for
CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE?
Quoted 8 lines
Quoted 1 line
I'll try to reproduce with v4 briefly.
I guess I know what's happening here. In case we only have DMA memory when booting, we don't reserve swiotlb buffers. Once we hotplug memory and online
ZONE_NORMAL, we don't have any swiotlb DMA bounce buffers to map such PFNs (total 0 (slots), used 0 (slots)).Can you try with "swiotlb=force" on the kernel cmdline?
Alternative, looks like you can specify "-m 2G,maxmem=16G,slots=1", to create proper ACPI tables that indicate hotpluggable memory. (I'll have to look into QEMU to figure out to always indicate hotpluggable memory that way).
Signature · 3 lines
Thanks, David / dhildenb
Quoted 11 lines · David Hildenbrand
Quoted 6 lines
I guess I know what's happening here. In case we only have DMA memory when booting, we don't reserve swiotlb buffers. Once we hotplug memory and online
ZONE_NORMAL, we don't have any swiotlb DMA bounce buffers to map such PFNs (total 0 (slots), used 0 (slots)).Can you try with "swiotlb=force" on the kernel cmdline?
Alternative, looks like you can specify "-m 2G,maxmem=16G,slots=1", to create proper ACPI tables that indicate hotpluggable memory. (I'll have to look into QEMU to figure out to always indicate hotpluggable memory that way).
That works too. Yes, better resolved in qemu, maybe. :)
Thanks!
Quoted 9 lines · Alex Shi
Quoted 6 lines · David Hildenbrand
Quoted 6 lines
I guess I know what's happening here. In case we only have DMA memory when booting, we don't reserve swiotlb buffers. Once we hotplug memory and online
ZONE_NORMAL, we don't have any swiotlb DMA bounce buffers to map such PFNs (total 0 (slots), used 0 (slots)).Can you try with "swiotlb=force" on the kernel cmdline?
Quoted 9 lines
Quoted 5 lines
Alternative, looks like you can specify "-m 2G,maxmem=16G,slots=1", to create proper ACPI tables that indicate hotpluggable memory. (I'll have to look into QEMU to figure out to always indicate hotpluggable memory that way).
That works too. Yes, better resolved in qemu, maybe. :)
You can checkout
[email protected]:davidhildenbrand/qemu.git virtio-mem-v4
(prone to change before officially sent), which will create srat tables also if no "slots" parameter was defined (and no -numa config was specified).
Your original example should work with that.
Signature · 3 lines
Thanks, David / dhildenb
Quoted 17 lines · David Hildenbrand
Quoted 16 lines · Alex Shi
Quoted 11 lines · David Hildenbrand
Quoted 6 lines
I guess I know what's happening here. In case we only have DMA memory when booting, we don't reserve swiotlb buffers. Once we hotplug memory and online
ZONE_NORMAL, we don't have any swiotlb DMA bounce buffers to map such PFNs (total 0 (slots), used 0 (slots)).Can you try with "swiotlb=force" on the kernel cmdline?
Alternative, looks like you can specify "-m 2G,maxmem=16G,slots=1", to create proper ACPI tables that indicate hotpluggable memory. (I'll have to look into QEMU to figure out to always indicate hotpluggable memory that way).
Quoted 6 lines
Quoted 2 lines
That works too. Yes, better resolved in qemu, maybe. :)
You can checkout
[email protected]:davidhildenbrand/qemu.git virtio-mem-v4
yes, it works for me. Thanks!
Quoted 7 lines
(prone to change before officially sent), which will create srat tables also if no "slots" parameter was defined (and no -numa config was specified).
Your original example should work with that.
Quoted 6 lines · David Hildenbrand
I guess I know what's happening here. In case we only have DMA memory when booting, we don't reserve swiotlb buffers. Once we hotplug memory and online
ZONE_NORMAL, we don't have any swiotlb DMA bounce buffers to map such PFNs (total 0 (slots), used 0 (slots)).Can you try with "swiotlb=force" on the kernel cmdline?
Yes, it works fine with this cmdline. problems gone,
Quoted 6 lines · David Hildenbrand
Please use the virtio-mem-v4 branch for now, v5 is still under construction (and might be scrapped completely if v4 goes upstream as is).
Looks like a DMA issue. Your're hotplugging 1GB, which should not really eat too much memory. There was a similar issue reported by Hui in [1], which boiled down to wrong usage of the swiotlb parameter.
I have no swiotbl=noforce set, and sometime no swiotlb error reported, like
(qemu) [ 41.591308] e1000 0000:00:03.0: dma_direct_map_page: overflow 0x000000011fd470da+54 of device mask ffffffff
[ 41.592431] e1000 0000:00:03.0: TX DMA map failed [ 41.593031] e1000 0000:00:03.0: dma_direct_map_page: overflow 0x000000011fd474da+54 of device mask ffffff ... [ 63.049464] ata_piix 0000:00:01.1: dma_direct_map_sg: overflow 0x0000000107db2000+4096 of device mask ffffffff [ 63.068297] ata1.00: exception Emask 0x0 SAct 0x0 SErr 0x0 action 0x0 [ 63.069057] ata1.00: failed command: READ DMA [ 63.069580] ata1.00: cmd c8/00:20:40:bd:d2/00:00:00:00:00/e0 tag 0 dma 16384 in [ 63.069580] res 50/00:00:3f:30:80/00:00:00:00:00/a0 Emask 0x40 (internal error)
Quoted 6 lines
In such cases you should always try to reproduce with hotplug of a sam-sized DIMM. E.g., hotplugging a 1GB DIMM should result in the same issue.
What does your .config specify for
CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE?
Yes, it's set.
I had tried the v2/v4 version, which has the same issue. Is this related with virtio-mem start address too low?
Thanks a lot!
Quoted 5 lines
I'll try to reproduce with v4 briefly.