mm/memory_hotplug: selective merging of memory resources
Subsystems (7)
cover mm/memory_hotplug: selective merging of memory resources
Some add_memory*() users add memory in small, contiguous memory blocks. Examples include virtio-mem, hyper-v balloon, and the XEN balloon.
This can quickly result in a lot of memory resources, whereby the actual resource boundaries are not of interest (e.g., it might be relevant for DIMMs, exposed via /proc/iomem to user space). We really want to merge added resources in this scenario where possible.
Resources are effectively stored in a list-based tree. Having a lot of resources not only wastes memory, it also makes traversing that tree more expensive, and makes /proc/iomem explode in size (e.g., requiring kexec-tools to manually merge resources when creating a kdump header. The current kexec-tools resource count limit does not allow more than ~100GB of memory with a memory block size of 128MB on x86-64).
Let's allow to selectively merge resources, speciyfing a parent node and a resource idendifier string. The memory unplug path will properly split up merged resources again.
Patch #3 contains a /proc/iomem example. Only tested with virtio-mem.
Note: This gets the job done and is comparably simple. More complicated approaches would require introducing IORESOURCE_MERGEABLE and extending our add_memory*() interfaces with a flag, specifying that merging after adding succeeded is acceptable. I'd like to avoid that complexity and code churn for now.
David Hildenbrand (5):
kernel/resource: make release_mem_region_adjustable() never fail kernel/resource: merge_child_mem_resources() to merge memory resources
after adding succeeded
virtio-mem: try to merge "System RAM (virtio_mem)" resources xen/balloon: try to merge "System RAM" resources hv_balloon:: try to merge "System RAM" resources
drivers/hv/hv_balloon.c | 3 ++ drivers/virtio/virtio_mem.c | 14 ++++- drivers/xen/balloon.c | 4 ++ include/linux/ioport.h | 7 ++- kernel/resource.c | 105 ++++++++++++++++++++++++++++-------- mm/memory_hotplug.c | 22 +------- 6 files changed, 109 insertions(+), 46 deletions(-)
Signature · 1 line
2.26.2
RFC PATCH 1/5 kernel/resource: make release_mem_region_adjustable() never fail
Let's make sure splitting a resource on memory hotunplug will never fail. This will become more relevant once we merge selected system ram resources - then, we'll trigger that case more often un memory unplug.
In general, this function is already unlikely to fail. When we remove memory, we free up quite a lot of metadata (memmap, page tables, memory block device, etc.).
All other error cases inside release_mem_region_adjustable() seem to be sanity checks if the function would be abused in different context - let's add WARN_ON_ONCE().
--- include/linux/ioport.h | 4 ++-- kernel/resource.c | 49 ++++++++++++++++++++++++------------------ mm/memory_hotplug.c | 22 +------------------ 3 files changed, 31 insertions(+), 44 deletions(-)
include/linux/ioport.h +2 -2
@@ -248,8 +248,8 @@ extern struct resource * __request_region(struct resource *,extern void __release_region (struct resource *,resource_size_t ,resource_size_t );#ifdef CONFIG_MEMORY_HOTREMOVE -extern int release_mem_region_adjustable (struct resource *,resource_size_t , -resource_size_t ); +extern void release_mem_region_adjustable (struct resource *,resource_size_t , +resource_size_t );#endif /* Wrappers for managed devices */
kernel/resource.c +28 -21
92 lines not on this page · raw message
mm/memory_hotplug.c +1 -21
@@ -1709,26 +1709,6 @@ void try_offline_node(int nid) }EXPORT_SYMBOL (try_offline_node); -static void __release_memory_resource (resource_size_t start, -resource_size_t size) -{ -int ret; - -/* -* When removing memory in the same granularity as it was added, -* this function never fails. It might only fail if resources -* have to be adjusted or split. We'll ignore the error, as -* removing of memory cannot fail. -*/ - ret =release_mem_region_adjustable (&iomem_resource, start, size); -if (ret) { -resource_size_t endres = start + size -1 ; - -pr_warn ("Unable to release resource <%pa-%pa> (%d)\n" , - &start, &endres, ret); - } -} -static int __ref try_remove_memory (int nid,u64 start,u64 size) {int rc =0 ;
@@ -1762,7 +1742,7 @@ static int __ref try_remove_memory(int nid, u64 start, u64 size)memblock_remove (start, size); } -__release_memory_resource (start, size); +release_mem_region_adjustable (&iomem_resource, start, size);try_offline_node (nid);
Signature · 1 line
2.26.2
RFC PATCH 2/5 kernel/resource: merge_child_mem_resources() to merge memory resources after adding succeeded
In reply to David HildenbrandSome add_memory*() users add memory in small, contiguous memory blocks. Examples include virtio-mem, hyper-v balloon, and the XEN balloon.
This can quickly result in a lot of memory resources, whereby the actual resource boundaries are not of interest (e.g., it might be relevant for DIMMs, exposed via /proc/iomem to user space). We really want to merge added resources in this scenario where possible.
Let's provide an interface to trigger merging of applicable child resources. It will be, for example, used by virtio-mem to trigger merging of memory resources it added (via add_memory_driver() managed) to its resource container.
Note: We really want to merge after the whole operation succeeded, not directly when adding a resource to the resource tree (it would break add_memory_resource() and require splitting resources again when the operation failed - e.g., due to -ENOMEM).
--- include/linux/ioport.h | 3 +++ kernel/resource.c | 56 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+)
include/linux/ioport.h +3 -0
@@ -251,6 +251,9 @@ extern void __release_region(struct resource *, resource_size_t,extern void release_mem_region_adjustable (struct resource *,resource_size_t ,resource_size_t );#endif +#ifdef CONFIG_MEMORY_HOTPLUG +extern void merge_child_mem_resources (struct resource *res,const char *name); +#endif /* Wrappers for managed devices */ struct device ;
kernel/resource.c +56 -0
62 lines not on this page · raw message
Signature · 1 line
2.26.2
RFC PATCH 3/5 virtio-mem: try to merge "System RAM (virtio_mem)" resources
In reply to David Hildenbrandvirtio-mem adds memory in memory block granularity, to be able to remove it in the same granularity again later, and to grow slowly on demand. This, however, results in quite a lot of resources when adding a lot of memory. Resources are effectively stored in a list-based tree. Having a lot of resources not only wastes memory, it also makes traversing that tree more expensive, and makes /proc/iomem explode in size (e.g., requiring kexec-tools to manually merge resources later when e.g., trying to create a kdump header).
Before this patch, we get (/proc/iomem) when hotplugging 2G via virtio-mem on x86-64:
[...]
100000000-13fffffff : System RAM
140000000-33fffffff : virtio0
140000000-147ffffff : System RAM (virtio_mem)
148000000-14fffffff : System RAM (virtio_mem)
150000000-157ffffff : System RAM (virtio_mem)
158000000-15fffffff : System RAM (virtio_mem)
160000000-167ffffff : System RAM (virtio_mem)
168000000-16fffffff : System RAM (virtio_mem)
170000000-177ffffff : System RAM (virtio_mem)
178000000-17fffffff : System RAM (virtio_mem)
180000000-187ffffff : System RAM (virtio_mem)
188000000-18fffffff : System RAM (virtio_mem)
190000000-197ffffff : System RAM (virtio_mem)
198000000-19fffffff : System RAM (virtio_mem)
1a0000000-1a7ffffff : System RAM (virtio_mem)
1a8000000-1afffffff : System RAM (virtio_mem)
1b0000000-1b7ffffff : System RAM (virtio_mem)
1b8000000-1bfffffff : System RAM (virtio_mem)
3280000000-32ffffffff : PCI Bus 0000:00With this patch, we get (/proc/iomem):
[...]
fffc0000-ffffffff : Reserved
100000000-13fffffff : System RAM
140000000-33fffffff : virtio0
140000000-1bfffffff : System RAM (virtio_mem)
3280000000-32ffffffff : PCI Bus 0000:00Of course, with more hotplugged memory, it gets worse. When unplugging memory blocks again, try_remove_memory() (via offline_and_remove_memory()) will properly split the resource up again.
--- drivers/virtio/virtio_mem.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
drivers/virtio/virtio_mem.c +12 -2
@@ -415,6 +415,7 @@ static int virtio_mem_mb_add(struct virtio_mem *vm, unsigned long mb_id) {const uint64_t addr =virtio_mem_mb_id_to_phys (mb_id);int nid = vm->nid; +int rc;if (nid ==NUMA_NO_NODE ) nid =memory_add_physaddr_to_nid (addr);
@@ -431,8 +432,17 @@ static int virtio_mem_mb_add(struct virtio_mem *vm, unsigned long mb_id) }dev_dbg (&vm->vdev->dev,"adding memory block: %lu\n" , mb_id); -return add_memory_driver_managed (nid, addr,memory_block_size_bytes (), - vm->resource_name); + rc =add_memory_driver_managed (nid, addr,memory_block_size_bytes (), + vm->resource_name); +if (!rc) { +/* +* Try to reduce the number of resources by merging them. The +* memory removal path will properly split them up again. +*/ +merge_child_mem_resources (vm->parent_resource, + vm->resource_name); + } +return rc; }/*
Signature · 1 line
2.26.2
Re: [PATCH RFCv1 3/5] virtio-mem: try to merge "System RAM (virtio_mem)" resources
On 31.07.20 11:18, David Hildenbrand wrote:
Grml, forgot to add cc: list for this patch, ccing the right people.
Quoted 82 lines
virtio-mem adds memory in memory block granularity, to be able to remove it in the same granularity again later, and to grow slowly on demand. This, however, results in quite a lot of resources when adding a lot of memory. Resources are effectively stored in a list-based tree. Having a lot of resources not only wastes memory, it also makes traversing that tree more expensive, and makes
/proc/iomemexplode in size (e.g., requiring kexec-tools to manually merge resources later when e.g., trying to create a kdump header).Before this patch, we get (
/proc/iomem) when hotplugging 2G via virtio-mem on x86-64:[...] 100000000-13fffffff : System RAM 140000000-33fffffff : virtio0 140000000-147ffffff : System RAM (virtio_mem) 148000000-14fffffff : System RAM (virtio_mem) 150000000-157ffffff : System RAM (virtio_mem) 158000000-15fffffff : System RAM (virtio_mem) 160000000-167ffffff : System RAM (virtio_mem) 168000000-16fffffff : System RAM (virtio_mem) 170000000-177ffffff : System RAM (virtio_mem) 178000000-17fffffff : System RAM (virtio_mem) 180000000-187ffffff : System RAM (virtio_mem) 188000000-18fffffff : System RAM (virtio_mem) 190000000-197ffffff : System RAM (virtio_mem) 198000000-19fffffff : System RAM (virtio_mem) 1a0000000-1a7ffffff : System RAM (virtio_mem) 1a8000000-1afffffff : System RAM (virtio_mem) 1b0000000-1b7ffffff : System RAM (virtio_mem) 1b8000000-1bfffffff : System RAM (virtio_mem) 3280000000-32ffffffff : PCI Bus 0000:00With this patch, we get (
/proc/iomem):[...] fffc0000-ffffffff : Reserved 100000000-13fffffff : System RAM 140000000-33fffffff : virtio0 140000000-1bfffffff : System RAM (virtio_mem) 3280000000-32ffffffff : PCI Bus 0000:00Of course, with more hotplugged memory, it gets worse. When unplugging memory blocks again,
try_remove_memory()(viaoffline_and_remove_memory()) will properly split the resource up again.Signed-off-by David Hildenbrand <[email protected]>--- drivers/virtio/virtio_mem.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)drivers/virtio/virtio_mem.c +12
-2@@ -415,6 +415,7 @@ static int virtio_mem_mb_add(struct virtio_mem *vm, unsigned long mb_id) {const uint64_t addr =virtio_mem_mb_id_to_phys (mb_id);int nid = vm->nid; +int rc;if (nid ==NUMA_NO_NODE ) nid =memory_add_physaddr_to_nid (addr);@@ -431,8 +432,17 @@ static int virtio_mem_mb_add(struct virtio_mem *vm, unsigned long mb_id) }dev_dbg (&vm->vdev->dev,"adding memory block: %lu\n" , mb_id); -return add_memory_driver_managed (nid, addr,memory_block_size_bytes (), - vm->resource_name); + rc =add_memory_driver_managed (nid, addr,memory_block_size_bytes (), + vm->resource_name); +if (!rc) { +/* +* Try to reduce the number of resources by merging them. The +* memory removal path will properly split them up again. +*/ +merge_child_mem_resources (vm->parent_resource, + vm->resource_name); + } +return rc; }/*
Signature · 3 lines
Thanks, David / dhildenb
RFC PATCH 4/5 xen/balloon: try to merge "System RAM" resources
In reply to David HildenbrandLet's reuse the new mechanism to merge "System RAM" resources below the root. We are the only one hotplugging "System RAM" and DIMMs don't apply, so this is safe to use.
--- drivers/xen/balloon.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/xen/balloon.c +4 -0
@@ -353,6 +353,10 @@ static enum bp_state reserve_additional_memory(void)if (rc) {pr_warn ("Cannot add additional memory (%i)\n" , rc); goto err; + }else { + resource =NULL ; +/* Try to reduce the number of "System RAM" resources. */ +merge_child_mem_resources (&iomem_resource,"System RAM" ); } balloon_stats.total_pages += balloon_hotplug;
Signature · 1 line
2.26.2
RFC PATCH 5/5 hv_balloon:: try to merge "System RAM" resources
In reply to David HildenbrandLet's reuse the new mechanism to merge "System RAM" resources below the root. We are the only one hotplugging "System RAM" and DIMMs don't apply, so this is safe to use.
--- drivers/hv/hv_balloon.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/hv/hv_balloon.c +3 -0
@@ -745,6 +745,9 @@ static void hv_mem_hot_add(unsigned long start, unsigned long size, has->covered_end_pfn -= processed_pfn;spin_unlock_irqrestore (&dm_device.ha_lock, flags);break ; + }else { +/* Try to reduce the number of "System RAM" resources. */ +merge_child_mem_resources (&iomem_resource,"System RAM" ); }/*
Signature · 1 line
2.26.2