vhost: memslot handling improvements
v4 cover vhost: memslot handling improvements
Following up on my previous work to make virtio-mem consume multiple memslots dynamically [1] that requires precise accounting between used vs. reserved memslots, I realized that vhost makes this extra hard by filtering out some memory region sections (so they don't consume a memslot) in the vhost-user case, which messes up the whole memslot accounting.
This series fixes what I found to be broken and prepares for more work on [1]. Further, it cleanes up the merge checks that I consider unnecessary.
[1] https://lkml.kernel.org/r/[email protected]
v3 -> v4:
- Add RBs
- "vhost: Rework memslot filtering and fix "used_memslot" tracking" -- Fix check for free memslots (> 1 vs > 0)
v2 -> v3:
- Add ACKs
- "softmmu/physmem: Fixup qemu_ram_block_from_host() documentation" -- Fix typo in description
v1 -> v2:
- "vhost: Rework memslot filtering and fix "used_memslot" tracking" -- New approach: keep filtering, but make filtering less generic and track separately. This should keep any existing setups working.
- "softmmu/physmem: Fixup qemu_ram_block_from_host() documentation" -- As requested by Igor
David Hildenbrand (3):
vhost: Rework memslot filtering and fix "used_memslot" tracking
vhost: Remove vhost_backend_can_merge() callback softmmu/physmem: Fixup qemu_ram_block_from_host() documentation
hw/virtio/vhost-user.c | 21 ++--------- hw/virtio/vhost-vdpa.c | 1 - hw/virtio/vhost.c | 62 ++++++++++++++++++++++++------- include/exec/cpu-common.h | 15 ++++++++ include/hw/virtio/vhost-backend.h | 9 +---- softmmu/physmem.c | 17 --------- 6 files changed, 68 insertions(+), 57 deletions(-)
Signature · 1 line
2.40.1
PATCH v4 1/3 vhost: Rework memslot filtering and fix "used_memslot" tracking
Having multiple vhost devices, some filtering out fd-less memslots and some not, can mess up the "used_memslot" accounting. Consequently our "free memslot" checks become unreliable and we might run out of free memslots at runtime later.
An example sequence which can trigger a potential issue that involves different vhost backends (vhost-kernel and vhost-user) and hotplugged memory devices can be found at [1].
Let's make the filtering mechanism less generic and distinguish between backends that support private memslots (without a fd) and ones that only support shared memslots (with a fd). Track the used_memslots for both cases separately and use the corresponding value when required.
Note: Most probably we should filter out MAP_PRIVATE fd-based RAM regions (for example, via memory-backend-memfd,...,shared=off or as default with
memory-backend-file) as well. When not using MAP_SHARED, it might not work
as expected. Add a TODO for now.
[1] https://lkml.kernel.org/r/[email protected]
--- hw/virtio/vhost-user.c | 7 ++-- hw/virtio/vhost.c | 56 ++++++++++++++++++++++++++----- include/hw/virtio/vhost-backend.h | 5 ++- 3 files changed, 52 insertions(+), 16 deletions(-)
hw/virtio/vhost-user.c +3 -4
@@ -2443,10 +2443,9 @@ vhost_user_crypto_close_session(struct vhost_dev *dev, uint64_t session_id)return 0 ; } -static bool vhost_user_mem_section_filter (struct vhost_dev *dev, -MemoryRegionSection *section) +static bool vhost_user_no_private_memslots (struct vhost_dev *dev) { -return memory_region_get_fd (section->mr) >=0 ; +return true; } static int vhost_user_get_inflight_fd(struct vhost_dev *dev,
@@ -2689,6 +2688,7 @@ const VhostOps user_ops = { .vhost_backend_init = vhost_user_backend_init, .vhost_backend_cleanup = vhost_user_backend_cleanup, .vhost_backend_memslots_limit = vhost_user_memslots_limit, + .vhost_backend_no_private_memslots = vhost_user_no_private_memslots, .vhost_set_log_base = vhost_user_set_log_base, .vhost_set_mem_table = vhost_user_set_mem_table, .vhost_set_vring_addr = vhost_user_set_vring_addr,
@@ -2715,7 +2715,6 @@ const VhostOps user_ops = { .vhost_set_config = vhost_user_set_config, .vhost_crypto_create_session = vhost_user_crypto_create_session, .vhost_crypto_close_session = vhost_user_crypto_close_session, - .vhost_backend_mem_section_filter = vhost_user_mem_section_filter, .vhost_get_inflight_fd = vhost_user_get_inflight_fd, .vhost_set_inflight_fd = vhost_user_set_inflight_fd, .vhost_dev_start = vhost_user_dev_start,
hw/virtio/vhost.c +47 -9
103 lines not on this page · raw message
include/hw/virtio/vhost-backend.h +2 -3
@@ -108,8 +108,7 @@ typedef int (*vhost_crypto_create_session_op)(struct vhost_dev *dev,typedef int (*vhost_crypto_close_session_op )(struct vhost_dev *dev,uint64_t session_id); -typedef bool (*vhost_backend_mem_section_filter_op )(struct vhost_dev *dev, -MemoryRegionSection *section); +typedef bool (*vhost_backend_no_private_memslots_op )(struct vhost_dev *dev); typedef int (*vhost_get_inflight_fd_op)(struct vhost_dev *dev, uint16_t queue_size,
@@ -138,6 +137,7 @@ typedef struct VhostOps {vhost_backend_init vhost_backend_init;vhost_backend_cleanup vhost_backend_cleanup;vhost_backend_memslots_limit vhost_backend_memslots_limit; +vhost_backend_no_private_memslots_op vhost_backend_no_private_memslots;vhost_net_set_backend_op vhost_net_set_backend;vhost_net_set_mtu_op vhost_net_set_mtu;vhost_scsi_set_endpoint_op vhost_scsi_set_endpoint;
@@ -172,7 +172,6 @@ typedef struct VhostOps {vhost_set_config_op vhost_set_config;vhost_crypto_create_session_op vhost_crypto_create_session;vhost_crypto_close_session_op vhost_crypto_close_session; -vhost_backend_mem_section_filter_op vhost_backend_mem_section_filter;vhost_get_inflight_fd_op vhost_get_inflight_fd;vhost_set_inflight_fd_op vhost_set_inflight_fd;vhost_dev_start_op vhost_dev_start;
Signature · 1 line
2.40.1
Re: [PATCH v4 1/3] vhost: Rework memslot filtering and fix "used_memslot" tracking
Quoted 19 lines · David Hildenbrand
Having multiple vhost devices, some filtering out fd-less memslots and some not, can mess up the "
used_memslot" accounting. Consequently our "free memslot" checks become unreliable and we might run out of free memslots at runtime later.An example sequence which can trigger a potential issue that involves different vhost backends (vhost-kernel and vhost-user) and hotplugged memory devices can be found at [1].
Let's make the filtering mechanism less generic and distinguish between backends that support private memslots (without a fd) and ones that only support shared memslots (with a fd). Track the
used_memslotsfor both cases separately and use the corresponding value when required.Note: Most probably we should filter out
MAP_PRIVATEfd-based RAM regions (for example, via memory-backend-memfd,...,shared=off or as default with
memory-backend-file) as well. When not usingMAP_SHARED, it might not work
as expected. Add a TODO for now.
Quoted 6 lines
[1] https://lkml.kernel.org/r/[email protected]
Fixes 988a27754bbb ("vhost: allow backends to filter memory sections")Cc Tiwei Bie <[email protected]>Acked-by Igor Mammedov <[email protected]>Signed-off-by David Hildenbrand <[email protected]>
Signature · 1 line
Peter Xu
PATCH v4 2/3 vhost: Remove vhost_backend_can_merge() callback
In reply to David HildenbrandChecking whether the memory regions are equal is sufficient: if they are equal, then most certainly the contained fd is equal.
The whole vhost-user memslot handling is suboptimal and overly complicated. We shouldn't have to lookup a RAM memory regions we got notified about in vhost_user_get_mr_data() using a host pointer. But that requires a bigger rework -- especially an alternative vhost_set_mem_table() backend call that simply consumes MemoryRegionSections.
For now, let's just drop vhost_backend_can_merge().
--- hw/virtio/vhost-user.c | 14 -------------- hw/virtio/vhost-vdpa.c | 1 - hw/virtio/vhost.c | 6 +----- include/hw/virtio/vhost-backend.h | 4 ---- 4 files changed, 1 insertion(+), 24 deletions(-)
hw/virtio/vhost-user.c +0 -14
@@ -2185,19 +2185,6 @@ static int vhost_user_migration_done(struct vhost_dev *dev, char* mac_addr)return -ENOTSUP ; } -static bool vhost_user_can_merge (struct vhost_dev *dev, -uint64_t start1,uint64_t size1, -uint64_t start2,uint64_t size2) -{ -ram_addr_t offset; -int mfd, rfd; - - (void )vhost_user_get_mr_data (start1, &offset, &mfd); - (void )vhost_user_get_mr_data (start2, &offset, &rfd); - -return mfd == rfd; -} -static int vhost_user_net_set_mtu (struct vhost_dev *dev,uint16_t mtu) {VhostUserMsg msg;
@@ -2707,7 +2694,6 @@ const VhostOps user_ops = { .vhost_set_vring_enable = vhost_user_set_vring_enable, .vhost_requires_shm_log = vhost_user_requires_shm_log, .vhost_migration_done = vhost_user_migration_done, - .vhost_backend_can_merge = vhost_user_can_merge, .vhost_net_set_mtu = vhost_user_net_set_mtu, .vhost_set_iotlb_callback = vhost_user_set_iotlb_callback, .vhost_send_device_iotlb_msg = vhost_user_send_device_iotlb_msg,
hw/virtio/vhost-vdpa.c +0 -1
@@ -1505,7 +1505,6 @@ const VhostOps vdpa_ops = { .vhost_set_config = vhost_vdpa_set_config, .vhost_requires_shm_log = NULL, .vhost_migration_done = NULL, - .vhost_backend_can_merge = NULL, .vhost_net_set_mtu = NULL, .vhost_set_iotlb_callback = NULL, .vhost_send_device_iotlb_msg = NULL,
hw/virtio/vhost.c +1 -5
@@ -729,11 +729,7 @@ static void vhost_region_add_section(struct vhost_dev *dev,size_t offset = mrs_gpa - prev_gpa_start;if (prev_host_start + offset == mrs_host && - section->mr == prev_sec->mr && - (!dev->vhost_ops->vhost_backend_can_merge || - dev->vhost_ops->vhost_backend_can_merge (dev, - mrs_host, mrs_size, - prev_host_start, prev_size))) { + section->mr == prev_sec->mr) {uint64_t max_end =MAX (prev_host_end, mrs_host + mrs_size); need_add = false; prev_sec->offset_within_address_space =
include/hw/virtio/vhost-backend.h +0 -4
@@ -86,9 +86,6 @@ typedef int (*vhost_set_vring_enable_op)(struct vhost_dev *dev,typedef bool (*vhost_requires_shm_log_op )(struct vhost_dev *dev);typedef int (*vhost_migration_done_op )(struct vhost_dev *dev,char *mac_addr); -typedef bool (*vhost_backend_can_merge_op )(struct vhost_dev *dev, -uint64_t start1,uint64_t size1, -uint64_t start2,uint64_t size2);typedef int (*vhost_vsock_set_guest_cid_op )(struct vhost_dev *dev,uint64_t guest_cid);typedef int (*vhost_vsock_set_running_op )(struct vhost_dev *dev,int start);
@@ -163,7 +160,6 @@ typedef struct VhostOps {vhost_set_vring_enable_op vhost_set_vring_enable;vhost_requires_shm_log_op vhost_requires_shm_log;vhost_migration_done_op vhost_migration_done; -vhost_backend_can_merge_op vhost_backend_can_merge;vhost_vsock_set_guest_cid_op vhost_vsock_set_guest_cid;vhost_vsock_set_running_op vhost_vsock_set_running;vhost_set_iotlb_callback_op vhost_set_iotlb_callback;
Signature · 1 line
2.40.1
PATCH v4 3/3 softmmu/physmem: Fixup qemu_ram_block_from_host() documentation
In reply to David HildenbrandLet's fixup the documentation (e.g., removing traces of the ram_addr parameter that no longer exists) and move it to the header file while at it.
--- include/exec/cpu-common.h | 15 +++++++++++++++ softmmu/physmem.c | 17 ----------------- 2 files changed, 15 insertions(+), 17 deletions(-)
include/exec/cpu-common.h +15 -0
@@ -76,6 +76,21 @@ void qemu_ram_remap(ram_addr_t addr, ram_addr_t length);ram_addr_t qemu_ram_addr_from_host (void *ptr);ram_addr_t qemu_ram_addr_from_host_nofail (void *ptr);RAMBlock *qemu_ram_block_by_name (const char *name); + +/* +* Translates a host ptr back to a RAMBlock and an offset in that RAMBlock. +* +* @ptr: The host pointer to transalte. +* @round_offset: Whether to round the result offset down to a target page +* @offset: Will be set to the offset within the returned RAMBlock. +* +* Returns: RAMBlock (or NULL if not found) +* +* By the time this function returns, the returned pointer is not protected +* by RCU anymore. If the caller is not within an RCU critical section and +* does not hold the iothread lock, it must have other means of protecting the +* pointer, such as a reference to the memory region that owns the RAMBlock. +*/ RAMBlock *qemu_ram_block_from_host (void *ptr,bool round_offset,ram_addr_t *offset);ram_addr_t qemu_ram_block_host_offset (RAMBlock *rb,void *host);
softmmu/physmem.c +0 -17
@@ -2169,23 +2169,6 @@ ram_addr_t qemu_ram_block_host_offset(RAMBlock *rb, void *host)return res; } -/* -* Translates a host ptr back to a RAMBlock, a ram_addr and an offset -* in that RAMBlock. -* -* ptr: Host pointer to look up -* round_offset: If true round the result offset down to a page boundary -* *ram_addr: set to result ram_addr -* *offset: set to result offset within the RAMBlock -* -* Returns: RAMBlock (or NULL if not found) -* -* By the time this function returns, the returned pointer is not protected -* by RCU anymore. If the caller is not within an RCU critical section and -* does not hold the iothread lock, it must have other means of protecting the -* pointer, such as a reference to the region that includes the incoming -* ram_addr_t. -*/ RAMBlock *qemu_ram_block_from_host (void *ptr,bool round_offset,ram_addr_t *offset) {
Signature · 1 line
2.40.1
Re: [PATCH v4 3/3] softmmu/physmem: Fixup qemu_ram_block_from_host() documentation
Quoted 4 lines · David Hildenbrand
Let's fixup the documentation (e.g., removing traces of the
ram_addrparameter that no longer exists) and move it to the header file while at it.
Quoted 8 lines
Suggested-by Igor Mammedov <[email protected]>Acked-by Igor Mammedov <[email protected]>Reviewed-by Peter Xu <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- include/exec/cpu-common.h | 15 +++++++++++++++ softmmu/physmem.c | 17 ----------------- 2 files changed, 15 insertions(+), 17 deletions(-)
Quoted 10 lines · David Hildenbrand
Following up on my previous work to make virtio-mem consume multiple memslots dynamically [1] that requires precise accounting between used vs. reserved memslots, I realized that vhost makes this extra hard by filtering out some memory region sections (so they don't consume a memslot) in the vhost-user case, which messes up the whole memslot accounting.
This series fixes what I found to be broken and prepares for more work on [1]. Further, it cleanes up the merge checks that I consider unnecessary.
Quoted 9 lines
[1] https://lkml.kernel.org/r/[email protected]
Cc "Michael S. Tsirkin" <[email protected]>Cc Stefan Hajnoczi <[email protected]>Cc Igor Mammedov <[email protected]>Cc Paolo Bonzini <[email protected]>Cc Peter Xu <[email protected]>Cc "Philippe Mathieu-Daudé" <[email protected]>
Gentle ping that this wants to get picked up :)
Signature · 3 lines
Cheers, David / dhildenb