virtio-mem: paravirtualized memory
Subsystems (8)
v4 cover virtio-mem: paravirtualized memory
This series is based on v5.7-rc4. The patches are located at:
https://github.com/davidhildenbrand/linux.git virtio-mem-v4
This is basically a resend of v3 [1], now based on v5.7-rc4 and restested. One patch was reshuffled and two ACKs I missed to add were added. The rebase did not require any modifications to patches.
Details about virtio-mem can be found in the cover letter of v2 [2]. A basic QEMU implementation was posted yesterday [3].
[1] https://lkml.kernel.org/r/[email protected]
[2] https://lkml.kernel.org/r/[email protected]
[3] https://lkml.kernel.org/r/[email protected]
v3 -> v4:
- Move "MAINTAINERS: Add myself as virtio-mem maintainer" to #2
- Add two ACKs from Andrew (in reply to v2)
-- "mm: Allow to offline unmovable PageOffline() pages via ..." -- "mm/memory_hotplug: Introduce offline_and_remove_memory()"
v2 -> v3:
- "virtio-mem: Paravirtualized memory hotplug"
-- Include "linux/slab.h" to fix build issues
-- Remember the "region_size", helpful for patch #11
-- Minor simplifaction in virtio_mem_overlaps_range()
-- Use notifier_from_errno() instead of notifier_to_errno() in notifier -- More reliable check for added memory when unloading the driver
- "virtio-mem: Allow to specify an ACPI PXM as nid"
-- Also print the nid
- Added patch #11-#15
David Hildenbrand (15):
virtio-mem: Paravirtualized memory hotplug
MAINTAINERS: Add myself as virtio-mem maintainer
virtio-mem: Allow to specify an ACPI PXM as nid
virtio-mem: Paravirtualized memory hotunplug part 1
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
virtio-mem: Add parent resource for all added "System RAM"
virtio-mem: Drop manual check for already present memory
virtio-mem: Unplug subblocks right-to-left
virtio-mem: Use -ETXTBSY as error code if the device is busy
virtio-mem: Try to unplug the complete online memory block first
MAINTAINERS | 7 + drivers/acpi/numa/srat.c | 1 + drivers/virtio/Kconfig | 17 + drivers/virtio/Makefile | 1 + drivers/virtio/virtio_mem.c | 1962 +++++++++++++++++++++++++++++++ include/linux/memory_hotplug.h | 1 + include/linux/page-flags.h | 10 + include/uapi/linux/virtio_ids.h | 1 + include/uapi/linux/virtio_mem.h | 208 ++++ mm/memory_hotplug.c | 81 +- mm/page_alloc.c | 26 + mm/page_isolation.c | 9 + 12 files changed, 2314 insertions(+), 10 deletions(-) create mode 100644 drivers/virtio/virtio_mem.c create mode 100644 include/uapi/linux/virtio_mem.h
Signature · 1 line
2.25.3
PATCH v4 1/15 virtio-mem: Paravirtualized memory hotplug
Each 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:
- In case memory is to be onlined by user space, we limit the amount of offline memory blocks, to not run out of memory. This is esp. an issue if memory is added faster than it is getting onlined.
- 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.
--- drivers/virtio/Kconfig | 16 + drivers/virtio/Makefile | 1 + drivers/virtio/virtio_mem.c | 1533 +++++++++++++++++++++++++++++++ include/uapi/linux/virtio_ids.h | 1 + include/uapi/linux/virtio_mem.h | 200 ++++ 5 files changed, 1751 insertions(+) create mode 100644 drivers/virtio/virtio_mem.c create mode 100644 include/uapi/linux/virtio_mem.h
drivers/virtio/Kconfig +16 -0
@@ -78,6 +78,22 @@ 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 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
@@ -7,3 +7,4 @@ 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_VDPA) += virtio_vdpa.o +obj-$(CONFIG_VIRTIO_MEM) += virtio_mem.o
drivers/virtio/virtio_mem.c new file +1533 -0
1,533 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 */ #define VIRTIO_ID_MAC80211_HWSIM 29/* virtio mac80211-hwsim */
include/uapi/linux/virtio_mem.h new file +200 -0
200 lines not on this page · raw message
Signature · 1 line
2.25.3
PATCH v4 2/15 MAINTAINERS: Add myself as virtio-mem maintainer
In reply to David HildenbrandLet's make sure patches/bug reports find the right person.
--- MAINTAINERS | 7 +++++++ 1 file changed, 7 insertions(+)
MAINTAINERS +7 -0
@@ -17972,6 +17972,13 @@ S: Maintained F: drivers/iommu/virtio-iommu.c F: include/uapi/linux/virtio_iommu.h +VIRTIO MEM DRIVER +M: David Hildenbrand <[email protected]> +L: [email protected] +S: Maintained +F: drivers/virtio/virtio_mem.c +F: include/uapi/linux/virtio_mem.h + VIRTUAL BOX GUEST DEVICE DRIVER M: Hans de Goede <[email protected]> M: Arnd Bergmann <[email protected]>
Signature · 1 line
2.25.3
PATCH v4 3/15 virtio-mem: Allow to specify an ACPI PXM as nid
In reply to David HildenbrandWe want to allow to specify (similar as for a DIMM), to which node a virtio-mem device (and, therefore, its memory) belongs. Add a new virtio-mem feature flag and export pxm_to_node, so it can be used in kernel module context.
--- drivers/acpi/numa/srat.c | 1 + drivers/virtio/virtio_mem.c | 39 +++++++++++++++++++++++++++++++-- include/uapi/linux/virtio_mem.h | 10 ++++++++- 3 files changed, 47 insertions(+), 3 deletions(-)
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) {
drivers/virtio/virtio_mem.c +37 -2
@@ -21,6 +21,8 @@#include <linux/bitmap.h> #include <linux/lockdep.h> +#include <acpi/acpi_numa.h> +enum virtio_mem_mb_state {/* Unplugged, not added to Linux. Can be reused later. */ VIRTIO_MEM_MB_STATE_UNUSED =0 ,
@@ -72,6 +74,8 @@ struct virtio_mem {/* The device block size (for communicating with the device). */ uint32_t device_block_size; +/* The translated node id. NUMA_NO_NODE in case not specified. */ +int nid;/* Physical start address of the memory region. */ uint64_t addr;/* Maximum region size in bytes. */
@@ -389,7 +393,10 @@ static int virtio_mem_sb_bitmap_prepare_next_mb(struct virtio_mem *vm)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 =memory_add_physaddr_to_nid (addr); +int nid = vm->nid; + +if (nid ==NUMA_NO_NODE ) + nid =memory_add_physaddr_to_nid (addr);dev_dbg (&vm->vdev->dev,"adding memory block: %lu\n" , mb_id);return add_memory (nid, addr,memory_block_size_bytes ());
@@ -407,7 +414,10 @@ static int virtio_mem_mb_add(struct virtio_mem *vm, unsigned long mb_id)static int virtio_mem_mb_remove (struct virtio_mem *vm,unsigned long mb_id) {const uint64_t addr =virtio_mem_mb_id_to_phys (mb_id); -int nid =memory_add_physaddr_to_nid (addr); +int nid = vm->nid; + +if (nid ==NUMA_NO_NODE ) + nid =memory_add_physaddr_to_nid (addr);dev_dbg (&vm->vdev->dev,"removing memory block: %lu\n" , mb_id);return remove_memory (nid, addr,memory_block_size_bytes ());
@@ -426,6 +436,17 @@ static void virtio_mem_retry(struct virtio_mem *vm)spin_unlock_irqrestore (&vm->removal_lock, flags); } +static int virtio_mem_translate_node_id (struct virtio_mem *vm,uint16_t node_id) +{ +int node =NUMA_NO_NODE ; + +#if defined(CONFIG_ACPI_NUMA ) +if (virtio_has_feature (vm->vdev,VIRTIO_MEM_F_ACPI_PXM )) + node =pxm_to_node (node_id); +#endif +return node; +} +/* * Test if a virtio-mem device overlaps with the given range. Can be called * from (notifier) callbacks lockless.
@@ -1267,6 +1288,7 @@ static bool virtio_mem_any_memory_present(unsigned long start,static int virtio_mem_init (struct virtio_mem *vm) {const uint64_t phys_limit =1UL <<MAX_PHYSMEM_BITS ; +uint16_t node_id;if (!vm->vdev->config->get) {dev_err (&vm->vdev->dev,"config access disabled\n" );
@@ -1287,6 +1309,9 @@ static int virtio_mem_init(struct virtio_mem *vm) &vm->plugged_size); virtio_cread(vm->vdev, struct virtio_mem_config, block_size, &vm->device_block_size); + virtio_cread(vm->vdev, struct virtio_mem_config, node_id, + &node_id); + vm->nid =virtio_mem_translate_node_id (vm, node_id); virtio_cread(vm->vdev, struct virtio_mem_config, addr, &vm->addr); virtio_cread(vm->vdev, struct virtio_mem_config, region_size, &vm->region_size);
@@ -1365,6 +1390,8 @@ static int virtio_mem_init(struct virtio_mem *vm) memory_block_size_bytes());dev_info (&vm->vdev->dev,"subblock size: 0x%x" , vm->subblock_size); +if (vm->nid !=NUMA_NO_NODE ) +dev_info (&vm->vdev->dev,"nid: %d" , vm->nid);return 0 ; }
@@ -1508,12 +1535,20 @@ static int virtio_mem_restore(struct virtio_device *vdev) }#endif +static unsigned int virtio_mem_features[] = { +#if defined(CONFIG_NUMA ) && defined(CONFIG_ACPI_NUMA ) + VIRTIO_MEM_F_ACPI_PXM, +#endif +}; +static struct virtio_device_id virtio_mem_id_table[] = { {VIRTIO_ID_MEM ,VIRTIO_DEV_ANY_ID }, {0 }, };static struct virtio_driver virtio_mem_driver = { + .feature_table = virtio_mem_features, + .feature_table_size =ARRAY_SIZE (virtio_mem_features), .driver.name =KBUILD_MODNAME , .driver.owner =THIS_MODULE , .id_table = virtio_mem_id_table,
include/uapi/linux/virtio_mem.h +9 -1
@@ -83,6 +83,12 @@* device is busy. */ +/* --- virtio-mem: feature bits --- */ + +/* node_id is an ACPI PXM and is valid */ +#define VIRTIO_MEM_F_ACPI_PXM 0 + +/* --- virtio-mem: guest -> host requests --- */ /* request to plug memory blocks */
@@ -177,7 +183,9 @@ struct virtio_mem_resp {struct virtio_mem_config {/* Block size and alignment. Cannot change. */ __u32 block_size; -__u32 padding; +/* Valid with VIRTIO_MEM_F_ACPI_PXM. Cannot change. */ +__u16 node_id; +__u16 padding;/* Start address of the memory region. Cannot change. */ __u64 addr;/* Region size (maximum). Cannot change. */
Signature · 1 line
2.25.3
PATCH v4 4/15 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 activity.
--- drivers/virtio/virtio_mem.c | 116 +++++++++++++++++++++++++++++++++++- 1 file changed, 114 insertions(+), 2 deletions(-)
drivers/virtio/virtio_mem.c +114 -2
@@ -123,7 +123,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;
@@ -280,6 +280,12 @@ static int virtio_mem_mb_state_prepare_next_mb(struct virtio_mem *vm) _mb_id++) \ if (virtio_mem_mb_get_state(_vm, _mb_id) == _state) +#define virtio_mem_for_each_mb_state_rev (_vm, _mb_id, _state) \ + for (_mb_id = _vm->next_mb_id - 1; \ + _mb_id >= _vm->first_mb_id && _vm->nb_mb_state[_state]; \ + _mb_id--) \ + if (virtio_mem_mb_get_state(_vm, _mb_id) == _state) +/* * Mark all selected subblocks plugged. *
@@ -325,6 +331,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.
@@ -513,6 +532,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,
@@ -1122,6 +1144,94 @@ 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_rev (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_rev (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.
@@ -1204,8 +1314,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.25.3
PATCH v4 5/15 virtio-mem: Paravirtualized memory hotunplug part 2
In reply to David HildenbrandWe also want to unplug online memory (contained in online memory blocks and, therefore, managed by the buddy), and eventually replug it later.
When requested to unplug memory, we use alloc_contig_range() to allocate subblocks in online memory blocks (so we are the owner) and send them to our hypervisor. When requested to plug memory, we can replug such memory using free_contig_range() after asking our hypervisor.
We also want to mark all allocated pages PG_offline, so nobody will touch them. To differentiate pages that were never onlined when onlining the memory block from pages allocated via alloc_contig_range(), we use PageDirty(). Based on this flag, virtio_mem_fake_online() can either online the pages for the first time or use free_contig_range().
It is worth noting that there are no guarantees 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 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.
As we want to access alloc_contig_range()/free_contig_range() from kernel module context, export the symbols.
Note: Whenever virtio-mem uses alloc_contig_range(), all affected pages are on the same node, in the same zone, and contain no holes.
--- drivers/virtio/Kconfig | 1 + drivers/virtio/virtio_mem.c | 157 ++++++++++++++++++++++++++++++++---- mm/page_alloc.c | 2 + 3 files changed, 146 insertions(+), 14 deletions(-)
drivers/virtio/Kconfig +1 -0
@@ -85,6 +85,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 +143 -14
210 lines not on this page · raw message
mm/page_alloc.c +2 -0
@@ -8594,6 +8594,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)
@@ -8709,6 +8710,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.25.3
PATCH v4 6/15 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 observable 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 | 44 +++++++++++++++++++++++++++++--------- mm/page_alloc.c | 24 +++++++++++++++++++++ mm/page_isolation.c | 9 ++++++++ 4 files changed, 77 insertions(+), 10 deletions(-)
include/linux/page-flags.h +10 -0
@@ -777,6 +777,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 +34 -10
84 lines not on this page · raw message
mm/page_alloc.c +24 -0
@@ -8363,6 +8363,19 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,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 indicate 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) ||PageLRU (page))continue ;
@@ -8783,6 +8796,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
@@ -151,6 +151,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 *
@@ -259,6 +260,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 skip PageOffline() +* pages when offlining memory by dropping its +* reference in MEM_GOING_OFFLINE. +*/ + pfn++;else break ; }
Signature · 1 line
2.25.3
PATCH v4 7/15 virtio-mem: Allow to offline partially unplugged memory blocks
In reply to David HildenbrandDropping the reference count of PageOffline() pages during MEM_GOING_ONLINE allows offlining code to skip them. However, we also have to clear PG_reserved, because PG_reserved pages get detected as unmovable right away. Take care of restoring the reference count when offlining is canceled.
Clarify why we don't have to perform any action when unloading the driver. Also, let's add a warning if anybody is still holding a reference to unplugged pages when offlining.
--- drivers/virtio/virtio_mem.c | 68 ++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-)
drivers/virtio/virtio_mem.c +67 -1
@@ -572,6 +572,57 @@ 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); +struct page *page; +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 =pfn_to_page (pfn + i); +if (WARN_ON (!page_ref_dec_and_test (page))) +dump_page (page,"unplugged page referenced" ); + } + } +} + +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 synchronously from add_memory() or * asynchronously (e.g., triggered via user space). We have to be careful
@@ -618,6 +669,7 @@ static int virtio_mem_memory_notifier_cb(struct notifier_block *nb,break ; } vm->hotplug_active = true; +virtio_mem_notify_going_offline (vm, mb_id);break ;case MEM_GOING_ONLINE :mutex_lock (&vm->hotplug_mutex);
@@ -642,6 +694,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 ;
@@ -668,8 +726,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); + } } }
@@ -1722,6 +1783,11 @@ static void virtio_mem_remove(struct virtio_device *vdev)BUG_ON (rc);virtio_mem_mb_set_state (vm, mb_id,VIRTIO_MEM_MB_STATE_UNUSED ); } +/* +* 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.25.3
PATCH v4 8/15 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 is an interface to offline and remove the memory from kernel module context, where we don't have access to the memory block devices (esp. find_memory_block() and device_offline()) and the device hotplug lock.
To keep things simple, allow to only work on a single memory block.
--- include/linux/memory_hotplug.h | 1 + mm/memory_hotplug.c | 37 ++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+)
include/linux/memory_hotplug.h +1 -0
@@ -319,6 +319,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 +37 -0
@@ -1821,4 +1821,41 @@ 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) { + rc =try_remove_memory (nid, start, size); +WARN_ON_ONCE (rc); + } +unlock_device_hotplug (); + +return rc; +} +EXPORT_SYMBOL_GPL (offline_and_remove_memory);#endif /* CONFIG_MEMORY_HOTREMOVE */
Signature · 1 line
2.25.3
PATCH v4 9/15 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
@@ -446,6 +446,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. */
@@ -537,7 +559,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); }
@@ -1284,7 +1312,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.
@@ -1340,9 +1369,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.25.3
PATCH v4 10/15 virtio-mem: Better retry handling
In reply to David HildenbrandLet's start with a retry interval of 5 seconds and double the time until we reach 5 minutes, in case we keep getting errors. Reset the retry interval in case we succeeded.
The two main reasons for having to retry are
- The hypervisor is busy and cannot process our request
- We cannot reach the desired requested_size (esp., not enough memory can get unplugged because we can't allocate any subblocks).
--- drivers/virtio/virtio_mem.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/virtio/virtio_mem.c +8 -3
@@ -141,7 +141,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 50000 +#define VIRTIO_MEM_RETRY_TIMER_MAX_MS 300000/* Memory notifier (online/offline events). */ struct notifier_block memory_notifier;
@@ -1550,6 +1552,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 :/*
@@ -1565,8 +1568,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 :
@@ -1586,6 +1588,8 @@ static enum hrtimer_restart virtio_mem_timer_expired(struct hrtimer *timer) retry_timer);virtio_mem_retry (vm); + vm->retry_timer_ms = min_t(unsigned int, vm->retry_timer_ms * 2, +VIRTIO_MEM_RETRY_TIMER_MAX_MS );return HRTIMER_NORESTART ; }
@@ -1754,6 +1758,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.25.3
PATCH v4 11/15 virtio-mem: Add parent resource for all added "System RAM"
In reply to David HildenbrandLet's add a parent resource, named after the virtio device (inspired by drivers/dax/kmem.c). This allows user space to identify which memory belongs to which virtio-mem device.
With this change and two virtio-mem devices:
:/# cat /proc/iomem 00000000-00000fff : Reserved 00001000-0009fbff : System RAM [...] 140000000-333ffffff : virtio0 140000000-147ffffff : System RAM 148000000-14fffffff : System RAM 150000000-157ffffff : System RAM [...] 334000000-3033ffffff : virtio1 338000000-33fffffff : System RAM 340000000-347ffffff : System RAM 348000000-34fffffff : System RAM [...]
--- drivers/virtio/virtio_mem.c | 52 ++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-)
drivers/virtio/virtio_mem.c +51 -1
@@ -99,6 +99,9 @@ struct virtio_mem {/* Id of the next memory bock to prepare when needed. */ unsigned long next_mb_id; +/* The parent resource for all memory added via this device. */ +struct resource *parent_resource; +/* Summary of all memory block states. */ unsigned long nb_mb_state[VIRTIO_MEM_MB_STATE_COUNT ];#define VIRTIO_MEM_NB_OFFLINE_THRESHOLD 10
@@ -1741,6 +1744,44 @@ static int virtio_mem_init(struct virtio_mem *vm)return 0 ; } +static int virtio_mem_create_resource (struct virtio_mem *vm) +{ +/* +* When force-unloading the driver and removing the device, we +* could have a garbage pointer. Duplicate the string. +*/ +const char *name =kstrdup (dev_name (&vm->vdev->dev),GFP_KERNEL ); + +if (!name) +return -ENOMEM ; + + vm->parent_resource =__request_mem_region (vm->addr, vm->region_size, + name,IORESOURCE_SYSTEM_RAM ); +if (!vm->parent_resource) { +kfree (name); +dev_warn (&vm->vdev->dev,"could not reserve device region\n" ); +return -EBUSY ; + } + +/* The memory is not actually busy - make add_memory() work. */ + vm->parent_resource->flags &= ~IORESOURCE_BUSY ; +return 0 ; +} + +static void virtio_mem_delete_resource (struct virtio_mem *vm) +{ +const char *name; + +if (!vm->parent_resource) +return ; + + name = vm->parent_resource->name; +release_resource (vm->parent_resource); +kfree (vm->parent_resource); +kfree (name); + vm->parent_resource =NULL ; +} +static int virtio_mem_probe (struct virtio_device *vdev) {struct virtio_mem *vm;
@@ -1770,11 +1811,16 @@ static int virtio_mem_probe(struct virtio_device *vdev)if (rc) goto out_del_vq; +/* create the parent resource for all memory */ + rc =virtio_mem_create_resource (vm); +if (rc) + goto out_del_vq; +/* register callbacks */ vm->memory_notifier.notifier_call = virtio_mem_memory_notifier_cb; rc =register_memory_notifier (&vm->memory_notifier);if (rc) - goto out_del_vq; + goto out_del_resource; rc =register_virtio_mem_device (vm);if (rc) goto out_unreg_mem;
@@ -1788,6 +1834,8 @@ static int virtio_mem_probe(struct virtio_device *vdev)return 0 ; out_unreg_mem:unregister_memory_notifier (&vm->memory_notifier); +out_del_resource: +virtio_mem_delete_resource (vm); out_del_vq: vdev->config->del_vqs (vdev); out_free_vm:
@@ -1848,6 +1896,8 @@ static void virtio_mem_remove(struct virtio_device *vdev) vm->nb_mb_state[VIRTIO_MEM_MB_STATE_ONLINE_PARTIAL] || vm->nb_mb_state[VIRTIO_MEM_MB_STATE_ONLINE_MOVABLE])dev_warn (&vdev->dev,"device still has system memory added\n" ); + else +virtio_mem_delete_resource (vm );/* remove all tracking data - no locking needed */ vfree (vm->mb_state);
Signature · 1 line
2.25.3
Re: [PATCH v4 11/15] virtio-mem: Add parent resource for all added "System RAM"
Quoted 114 lines
Let's add a parent resource, named after the virtio device (inspired by
drivers/dax/kmem.c). This allows user space to identify which memory belongs to which virtio-mem device.With this change and two virtio-mem devices:
:/# cat /proc/iomem 00000000-00000fff : Reserved 00001000-0009fbff : System RAM [...] 140000000-333ffffff : virtio0 140000000-147ffffff : System RAM 148000000-14fffffff : System RAM 150000000-157ffffff : System RAM [...] 334000000-3033ffffff : virtio1 338000000-33fffffff : System RAM 340000000-347ffffff : System RAM 348000000-34fffffff : System RAM [...]Cc "Michael S. Tsirkin" <[email protected]>Cc Pankaj Gupta <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- drivers/virtio/virtio_mem.c | 52 ++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-)drivers/virtio/virtio_mem.c +49
-1@@ -99,6 +99,9 @@ struct virtio_mem {/* Id of the next memory bock to prepare when needed. */ unsigned long next_mb_id; +/* The parent resource for all memory added via this device. */ +struct resource *parent_resource; +/* Summary of all memory block states. */ unsigned long nb_mb_state[VIRTIO_MEM_MB_STATE_COUNT ];#define VIRTIO_MEM_NB_OFFLINE_THRESHOLD 10@@ -1741,6 +1744,44 @@ static int virtio_mem_init(struct virtio_mem *vm)return 0 ; } +static int virtio_mem_create_resource (struct virtio_mem *vm) +{ +/* +* When force-unloading the driver and removing the device, we +* could have a garbage pointer. Duplicate the string. +*/ +const char *name =kstrdup (dev_name (&vm->vdev->dev),GFP_KERNEL ); + +if (!name) +return -ENOMEM ; + + vm->parent_resource =__request_mem_region (vm->addr, vm->region_size, + name,IORESOURCE_SYSTEM_RAM ); +if (!vm->parent_resource) { +kfree (name); +dev_warn (&vm->vdev->dev,"could not reserve device region\n" ); +return -EBUSY ; + } + +/* The memory is not actually busy - make add_memory() work. */ + vm->parent_resource->flags &= ~IORESOURCE_BUSY ; +return 0 ; +} + +static void virtio_mem_delete_resource (struct virtio_mem *vm) +{ +const char *name; + +if (!vm->parent_resource) +return ; + + name = vm->parent_resource->name; +release_resource (vm->parent_resource); +kfree (vm->parent_resource); +kfree (name); + vm->parent_resource =NULL ; +} +static int virtio_mem_probe (struct virtio_device *vdev) {struct virtio_mem *vm;@@ -1770,11 +1811,16 @@ static int virtio_mem_probe(struct virtio_device *vdev)if (rc) goto out_del_vq; +/* create the parent resource for all memory */ + rc =virtio_mem_create_resource (vm); +if (rc) + goto out_del_vq; +/* register callbacks */ vm->memory_notifier.notifier_call = virtio_mem_memory_notifier_cb; rc =register_memory_notifier (&vm->memory_notifier);if (rc) - goto out_del_vq; + goto out_del_resource; rc =register_virtio_mem_device (vm);if (rc) goto out_unreg_mem;@@ -1788,6 +1834,8 @@ static int virtio_mem_probe(struct virtio_device *vdev)return 0 ; out_unreg_mem:unregister_memory_notifier (&vm->memory_notifier); +out_del_resource: +virtio_mem_delete_resource (vm); out_del_vq: vdev->config->del_vqs (vdev); out_free_vm:
drivers/virtio/virtio_mem.c static void virtio_mem_remove(struct virtio_device *vdev)
Quoted 9 lines
drivers/virtio/virtio_mem.c +2
-1@@ -1848,6 +1896,8 @@ static void virtio_mem_remove(struct virtio_device *vdev) vm->nb_mb_state[VIRTIO_MEM_MB_STATE_ONLINE_PARTIAL] || vm->nb_mb_state[VIRTIO_MEM_MB_STATE_ONLINE_MOVABLE])dev_warn (&vdev->dev,"device still has system memory added\n" ); + else +virtio_mem_delete_resource (vm );/* remove all tracking data - no locking needed */ vfree (vm->mb_state); --
PATCH v4 12/15 virtio-mem: Drop manual check for already present memory
In reply to David HildenbrandRegistering our parent resource will fail if any memory is still present (e.g., because somebody unloaded the driver and tries to reload it). No need for the manual check.
Move our "unplug all" handling to after registering the resource.
--- drivers/virtio/virtio_mem.c | 55 ++++++++----------------------------- 1 file changed, 12 insertions(+), 43 deletions(-)
drivers/virtio/virtio_mem.c +12 -43
@@ -1616,23 +1616,6 @@ static int virtio_mem_init_vq(struct virtio_mem *vm)return 0 ; } -/* -* Test if any memory in the range is present in Linux. -*/ -static bool virtio_mem_any_memory_present (unsigned long start, -unsigned long size) -{ -const unsigned long start_pfn =PFN_DOWN (start); -const unsigned long end_pfn =PFN_UP (start + size); -unsigned long pfn; - -for (pfn = start_pfn; pfn != end_pfn; pfn++) -if (present_section_nr (pfn_to_section_nr (pfn))) -return true; - -return false; -} -static int virtio_mem_init (struct virtio_mem *vm) {const uint64_t phys_limit =1UL <<MAX_PHYSMEM_BITS ;
@@ -1664,32 +1647,6 @@ static int virtio_mem_init(struct virtio_mem *vm) virtio_cread(vm->vdev, struct virtio_mem_config, region_size, &vm->region_size); -/* -* If we still have memory plugged, we might have to unplug all -* memory first. However, if somebody simply unloaded the driver -* we would have to reinitialize the old state - something we don't -* support yet. Detect if we have any memory in the area present. -*/ -if (vm->plugged_size) { -uint64_t usable_region_size; - - virtio_cread(vm->vdev, struct virtio_mem_config, - usable_region_size, &usable_region_size); - -if (virtio_mem_any_memory_present (vm->addr, - usable_region_size)) { -dev_err (&vm->vdev->dev, -"reloading the driver is not supported\n" ); -return -EINVAL ; - } -/* -* Note: it might happen that the device is busy and -* unplugging all memory might take some time. -*/ -dev_info (&vm->vdev->dev,"unplugging all memory required\n" ); - vm->unplug_all_required =1 ; - } -/* * We always hotplug memory in memory block granularity. This way, * we have to wait for exactly one memory block to online.
@@ -1760,6 +1717,8 @@ static int virtio_mem_create_resource(struct virtio_mem *vm)if (!vm->parent_resource) {kfree (name);dev_warn (&vm->vdev->dev,"could not reserve device region\n" ); +dev_info (&vm->vdev->dev, +"reloading the driver is not supported\n" );return -EBUSY ; }
@@ -1816,6 +1775,16 @@ static int virtio_mem_probe(struct virtio_device *vdev)if (rc) goto out_del_vq; +/* +* If we still have memory plugged, we have to unplug all memory first. +* Registering our parent resource makes sure that this memory isn't +* actually in use (e.g., trying to reload the driver). +*/ +if (vm->plugged_size) { + vm->unplug_all_required =1 ; +dev_info (&vm->vdev->dev,"unplugging all memory is required\n" ); + } +/* register callbacks */ vm->memory_notifier.notifier_call = virtio_mem_memory_notifier_cb; rc =register_memory_notifier (&vm->memory_notifier);
Signature · 1 line
2.25.3
PATCH v4 13/15 virtio-mem: Unplug subblocks right-to-left
In reply to David HildenbrandWe unplug blocks right-to-left, let's also unplug subblocks within a block right-to-left.
--- drivers/virtio/virtio_mem.c | 38 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 22 deletions(-)
drivers/virtio/virtio_mem.c +16 -22
@@ -353,18 +353,6 @@ static bool virtio_mem_mb_test_sb_unplugged(struct virtio_mem *vm,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. -*/ -static int virtio_mem_mb_first_plugged_sb (struct virtio_mem *vm, -unsigned long mb_id) -{ -const int bit = (mb_id - vm->first_mb_id) * vm->nb_sb_per_mb; - -return find_next_bit (vm->sb_bitmap, bit + vm->nb_sb_per_mb, bit) - bit; -} -/* * Find the first unplugged subblock. Returns vm->nb_sb_per_mb in case there is * none.
@@ -1016,21 +1004,27 @@ static int virtio_mem_mb_unplug_any_sb(struct virtio_mem *vm,int sb_id, count;int rc; + sb_id = vm->nb_sb_per_mb -1 ;while (*nb_sb) { - sb_id =virtio_mem_mb_first_plugged_sb (vm, mb_id); -if (sb_id >= vm->nb_sb_per_mb) +/* Find the next candidate subblock */ +while (sb_id >=0 && +virtio_mem_mb_test_sb_unplugged (vm, mb_id, sb_id,1 )) + sb_id--; +if (sb_id <0 )break ; +/* Try to unplug multiple subblocks at a time */ count =1 ; -while (count < *nb_sb && - sb_id + count < vm->nb_sb_per_mb && -virtio_mem_mb_test_sb_plugged (vm, mb_id, sb_id + count, -1 )) +while (count < *nb_sb && sb_id >0 && +virtio_mem_mb_test_sb_plugged (vm, mb_id, sb_id -1 ,1 )) { count++; + sb_id--; + } rc =virtio_mem_mb_unplug_sb (vm, mb_id, sb_id, count);if (rc)return rc; *nb_sb -= count; + sb_id--; }return 0 ;
@@ -1337,12 +1331,12 @@ static int virtio_mem_mb_unplug_any_sb_online(struct virtio_mem *vm,* we should sense via something like is_mem_section_removable() * first if it makes sense to go ahead any try to allocate. */ -for (sb_id =0 ; sb_id < vm->nb_sb_per_mb && *nb_sb; sb_id++) { +for (sb_id = vm->nb_sb_per_mb -1 ; sb_id >=0 && *nb_sb; sb_id--) {/* Find the next candidate subblock */ -while (sb_id < vm->nb_sb_per_mb && +while (sb_id >=0 && !virtio_mem_mb_test_sb_plugged (vm, mb_id, sb_id,1 )) - sb_id++; -if (sb_id >= vm->nb_sb_per_mb) + sb_id--; +if (sb_id <0 )break ; start_pfn = PFN_DOWN(virtio_mem_mb_id_to_phys(mb_id) +
Signature · 1 line
2.25.3
PATCH v4 14/15 virtio-mem: Use -ETXTBSY as error code if the device is busy
In reply to David HildenbrandLet's be able to distinguish if the device or if memory is busy.
--- drivers/virtio/virtio_mem.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
drivers/virtio/virtio_mem.c +10 -6
@@ -893,7 +893,7 @@ static int virtio_mem_send_plug_request(struct virtio_mem *vm, uint64_t addr,case VIRTIO_MEM_RESP_NACK :return -EAGAIN ;case VIRTIO_MEM_RESP_BUSY : -return -EBUSY ; +return -ETXTBSY ;case VIRTIO_MEM_RESP_ERROR :return -EINVAL ;default :
@@ -919,7 +919,7 @@ static int virtio_mem_send_unplug_request(struct virtio_mem *vm, uint64_t addr, vm->plugged_size -= size;return 0 ;case VIRTIO_MEM_RESP_BUSY : -return -EBUSY ; +return -ETXTBSY ;case VIRTIO_MEM_RESP_ERROR :return -EINVAL ;default :
@@ -941,7 +941,7 @@ static int virtio_mem_send_unplug_all_request(struct virtio_mem *vm)atomic_set (&vm->config_changed,1 );return 0 ;case VIRTIO_MEM_RESP_BUSY : -return -EBUSY ; +return -ETXTBSY ;default :return -ENOMEM ; }
@@ -1557,11 +1557,15 @@ static void virtio_mem_run_wq(struct work_struct *work)* or we have too many offline memory blocks. */ break ; -case -EBUSY : +case -ETXTBSY :/* * The hypervisor cannot process our request right now -* (e.g., out of memory, migrating) or we cannot free up -* any memory to unplug it (all plugged memory is busy). +* (e.g., out of memory, migrating); +*/ +case -EBUSY : +/* +* We cannot free up any memory to unplug it (all plugged memory +* is busy). */ case -ENOMEM :/* Out of memory, try again later. */
Signature · 1 line
2.25.3
PATCH v4 15/15 virtio-mem: Try to unplug the complete online memory block first
In reply to David HildenbrandRight now, we always try to unplug single subblocks when processing an online memory block. Let's try to unplug the complete online memory block first, in case it is fully plugged and the unplug request is large enough. Fallback to single subblocks in case the memory block cannot get unplugged as a whole.
--- drivers/virtio/virtio_mem.c | 88 ++++++++++++++++++++++++------------- 1 file changed, 57 insertions(+), 31 deletions(-)
drivers/virtio/virtio_mem.c +57 -31
@@ -1307,6 +1307,46 @@ static int virtio_mem_mb_unplug_any_sb_offline(struct virtio_mem *vm,return 0 ; } +/* +* Unplug the given plugged subblocks of an online memory block. +* +* Will modify the state of the memory block. +*/ +static int virtio_mem_mb_unplug_sb_online (struct virtio_mem *vm, +unsigned long mb_id,int sb_id, +int count) +{ +const unsigned long nr_pages =PFN_DOWN (vm->subblock_size) * count; +unsigned long start_pfn; +int rc; + + start_pfn =PFN_DOWN (virtio_mem_mb_id_to_phys (mb_id) + + sb_id * vm->subblock_size); + rc =alloc_contig_range (start_pfn, start_pfn + nr_pages, +MIGRATE_MOVABLE ,GFP_KERNEL ); +if (rc == -ENOMEM ) +/* whoops, out of memory */ +return rc; +if (rc) +return -EBUSY ; + +/* Mark it as fake-offline before unplugging it */ +virtio_mem_set_fake_offline (start_pfn, nr_pages, true); +adjust_managed_page_count (pfn_to_page (start_pfn), -nr_pages); + +/* Try to unplug the allocated memory */ + rc =virtio_mem_mb_unplug_sb (vm, mb_id, sb_id, count); +if (rc) { +/* Return the memory to the buddy. */ +virtio_mem_fake_online (start_pfn, nr_pages); +return rc; + } + +virtio_mem_mb_set_state (vm, mb_id, +VIRTIO_MEM_MB_STATE_ONLINE_PARTIAL ); +return 0 ; +} +/* * Unplug the desired number of plugged subblocks of an online memory block. * Will skip subblock that are busy.
@@ -1321,16 +1361,21 @@ static int virtio_mem_mb_unplug_any_sb_online(struct virtio_mem *vm,unsigned long mb_id, uint64_t *nb_sb) { -const unsigned long nr_pages =PFN_DOWN (vm->subblock_size); -unsigned long start_pfn; int rc, sb_id; -/* -* TODO: To increase the performance we want to try bigger, consecutive -* subblocks first before falling back to single subblocks. Also, -* we should sense via something like is_mem_section_removable() -* first if it makes sense to go ahead any try to allocate. -*/ +/* If possible, try to unplug the complete block in one shot. */ +if (*nb_sb >= vm->nb_sb_per_mb && +virtio_mem_mb_test_sb_plugged (vm, mb_id,0 , vm->nb_sb_per_mb)) { + rc =virtio_mem_mb_unplug_sb_online (vm, mb_id,0 , + vm->nb_sb_per_mb); +if (!rc) { + *nb_sb -= vm->nb_sb_per_mb; + goto unplugged; + }else if (rc != -EBUSY ) +return rc; + } + +/* Fallback to single subblocks. */ for (sb_id = vm->nb_sb_per_mb -1 ; sb_id >=0 && *nb_sb; sb_id--) {/* Find the next candidate subblock */ while (sb_id >= 0 &&
@@ -1339,34 +1384,15 @@ static int virtio_mem_mb_unplug_any_sb_online(struct virtio_mem *vm,if (sb_id <0 )break ; - start_pfn =PFN_DOWN (virtio_mem_mb_id_to_phys (mb_id) + - sb_id * vm->subblock_size); - rc =alloc_contig_range (start_pfn, start_pfn + nr_pages, -MIGRATE_MOVABLE ,GFP_KERNEL ); -if (rc == -ENOMEM ) -/* whoops, out of memory */ -return rc; -if (rc) -/* memory busy, we can't unplug this chunk */ + rc =virtio_mem_mb_unplug_sb_online (vm, mb_id, sb_id,1 ); +if (rc == -EBUSY )continue ; - -/* Mark it as fake-offline before unplugging it */ -virtio_mem_set_fake_offline (start_pfn, nr_pages, true); -adjust_managed_page_count (pfn_to_page (start_pfn), -nr_pages); - -/* Try to unplug the allocated memory */ - rc =virtio_mem_mb_unplug_sb (vm, mb_id, sb_id,1 ); -if (rc) { -/* Return the memory to the buddy. */ -virtio_mem_fake_online (start_pfn, nr_pages); +else if (rc)return rc; - } - -virtio_mem_mb_set_state (vm, mb_id, -VIRTIO_MEM_MB_STATE_ONLINE_PARTIAL ); *nb_sb -=1 ; } +unplugged:/* * Once all subblocks of a memory block were unplugged, offline and * remove it. This will usually not fail, as no memory is in use
Signature · 1 line
2.25.3
[PATCH v4 16/15] virtio-mem: Don't rely on implicit compiler padding for requests
In reply to David HildenbrandThe compiler will add padding after the last member, make that explicit. The size of a request is always 24 bytes. The size of a response always 10 bytes. Add compile-time checks.
---
Something I noticed while working on the spec (which proves that writing a virtio-spec makes sense :) ).
--- drivers/virtio/virtio_mem.c | 3 +++ include/uapi/linux/virtio_mem.h | 3 +++ 2 files changed, 6 insertions(+)
drivers/virtio/virtio_mem.c +3 -0
@@ -1770,6 +1770,9 @@ static int virtio_mem_probe(struct virtio_device *vdev)struct virtio_mem *vm;int rc = -EINVAL ; +BUILD_BUG_ON (sizeof (struct virtio_mem_req ) !=24 ); +BUILD_BUG_ON (sizeof (struct virtio_mem_resp ) !=10 ); + vdev->priv = vm =kzalloc (sizeof (*vm),GFP_KERNEL );if (!vm)return -ENOMEM ;
include/uapi/linux/virtio_mem.h +3 -0
@@ -103,16 +103,19 @@struct virtio_mem_req_plug {__virtio64 addr;__virtio16 nb_blocks; +__virtio16 padding[3 ]; };struct virtio_mem_req_unplug {__virtio64 addr;__virtio16 nb_blocks; +__virtio16 padding[3 ]; };struct virtio_mem_req_state {__virtio64 addr;__virtio16 nb_blocks; +__virtio16 padding[3 ]; };struct virtio_mem_req {
Signature · 1 line
2.25.4
Hi David,
Thanks for your work.
I tried this version with cloud-hypervisor master. It worked very well.
Best,
Hui
Quoted 69 lines
2020年5月7日 22:01,David Hildenbrand <[email protected]> 写道:
This series is based on v5.7-rc4. The patches are located at:
https://github.com/davidhildenbrand/linux.git virtio-mem-v4This is basically a resend of v3 [1], now based on v5.7-rc4 and restested. One patch was reshuffled and two ACKs I missed to add were added. The rebase did not require any modifications to patches.
Details about virtio-mem can be found in the cover letter of v2 [2]. A basic QEMU implementation was posted yesterday [3].
[1] https://lkml.kernel.org/r/[email protected]
[2] https://lkml.kernel.org/r/[email protected]
[3] https://lkml.kernel.org/r/[email protected]v3 -> v4:
- Move "MAINTAINERS: Add myself as virtio-mem maintainer" to #2
- Add two ACKs from Andrew (in reply to v2)
-- "mm: Allow to offline unmovablePageOffline()pages via ..." -- "mm/memory_hotplug: Introduceoffline_and_remove_memory()"v2 -> v3:
- "virtio-mem: Paravirtualized memory hotplug"
-- Include "linux/slab.h" to fix build issues
-- Remember the "region_size", helpful for patch #11
-- Minor simplifaction invirtio_mem_overlaps_range()
-- Usenotifier_from_errno()instead ofnotifier_to_errno()in notifier -- More reliable check for added memory when unloading the driver
- "virtio-mem: Allow to specify an ACPI PXM as nid"
-- Also print the nid
- Added patch #11-#15David Hildenbrand (15):
virtio-mem: Paravirtualized memory hotplug
MAINTAINERS: Add myself as virtio-mem maintainer
virtio-mem: Allow to specify an ACPI PXM as nid
virtio-mem: Paravirtualized memory hotunplug part 1
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
virtio-mem: Add parent resource for all added "System RAM"
virtio-mem: Drop manual check for already present memory
virtio-mem: Unplug subblocks right-to-left
virtio-mem: Use-ETXTBSYas error code if the device is busy
virtio-mem: Try to unplug the complete online memory block firstMAINTAINERS | 7 + drivers/acpi/numa/srat.c | 1 + drivers/virtio/Kconfig | 17 + drivers/virtio/Makefile | 1 + drivers/virtio/virtio_mem.c | 1962 +++++++++++++++++++++++++++++++ include/linux/memory_hotplug.h | 1 + include/linux/page-flags.h | 10 + include/uapi/linux/virtio_ids.h | 1 + include/uapi/linux/virtio_mem.h | 208 ++++ mm/memory_hotplug.c | 81 +- mm/page_alloc.c | 26 + mm/page_isolation.c | 9 + 12 files changed, 2314 insertions(+), 10 deletions(-)create mode 100644
drivers/virtio/virtio_mem.ccreate mode 100644include/uapi/linux/virtio_mem.hSignature · 1 line
2.25.3
Quoted 7 lines · teawater
Hi David,
Thanks for your work.
I tried this version with cloud-hypervisor master. It worked very well.Best,
Hui
Hi Hui,
thanks for testing!
Cheers!
Signature · 3 lines
Thanks, David / dhildenb
Quoted 20 lines · David Hildenbrand
This series is based on v5.7-rc4. The patches are located at:
https://github.com/davidhildenbrand/linux.git virtio-mem-v4This is basically a resend of v3 [1], now based on v5.7-rc4 and restested. One patch was reshuffled and two ACKs I missed to add were added. The rebase did not require any modifications to patches.
Details about virtio-mem can be found in the cover letter of v2 [2]. A basic QEMU implementation was posted yesterday [3].
[1] https://lkml.kernel.org/r/[email protected]
[2] https://lkml.kernel.org/r/[email protected]
[3] https://lkml.kernel.org/r/[email protected]v3 -> v4:
- Move "MAINTAINERS: Add myself as virtio-mem maintainer" to #2
- Add two ACKs from Andrew (in reply to v2)
-- "mm: Allow to offline unmovablePageOffline()pages via ..." -- "mm/memory_hotplug: Introduceoffline_and_remove_memory()"
Quoted 10 lines
v2 -> v3:
- "virtio-mem: Paravirtualized memory hotplug"
-- Include "linux/slab.h" to fix build issues
-- Remember the "region_size", helpful for patch #11
-- Minor simplifaction invirtio_mem_overlaps_range()
-- Usenotifier_from_errno()instead ofnotifier_to_errno()in notifier -- More reliable check for added memory when unloading the driver
- "virtio-mem: Allow to specify an ACPI PXM as nid"
-- Also print the nid
- Added patch #11-#15
@MST ping, v5.7 has been released
Signature · 3 lines
Thanks, David / dhildenb