vhost: check if vhost has capacity for hotplugged memory
cover vhost: check if vhost has capacity for hotplugged memory
it's defensive patchset which helps to avoid QEMU crashing at memory hotplug time by checking that vhost has free capacity for an additional memory slot.
Igor Mammedov (2):
vhost: add vhost_has_free_slot() interface
pc-dimm: add vhost slots limit check before commiting to hotplug
hw/mem/pc-dimm.c | 7 +++++++ hw/virtio/vhost-backend.c | 23 ++++++++++++++++++++++- hw/virtio/vhost-user.c | 8 +++++++- hw/virtio/vhost.c | 21 +++++++++++++++++++++ include/hw/virtio/vhost-backend.h | 2 ++ include/hw/virtio/vhost.h | 1 + stubs/Makefile.objs | 1 + stubs/vhost.c | 6 ++++++ 8 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 stubs/vhost.c
Signature · 1 line
1.8.3.1
PATCH 1/2 vhost: add vhost_has_free_slot() interface
it will allow for other parts of QEMU check if it's safe to map memory region during hotplug/runtime. That way hotplug path will have a chance to cancel hotplug operation instead of crashing in vhost_commit().
--- hw/virtio/vhost-backend.c | 23 ++++++++++++++++++++++- hw/virtio/vhost-user.c | 8 +++++++- hw/virtio/vhost.c | 21 +++++++++++++++++++++ include/hw/virtio/vhost-backend.h | 2 ++ include/hw/virtio/vhost.h | 1 + stubs/Makefile.objs | 1 + stubs/vhost.c | 6 ++++++ 7 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 stubs/vhost.c
hw/virtio/vhost-backend.c +22 -1
@@ -11,6 +11,7 @@#include "hw/virtio/vhost.h" #include "hw/virtio/vhost-backend.h" #include "qemu/error-report.h" +#include "linux/vhost.h" #include <sys/ioctl.h>
@@ -42,11 +43,31 @@ static int vhost_kernel_cleanup(struct vhost_dev *dev)return close (fd); } +static int vhost_kernel_memslots_limit (struct vhost_dev *dev) +{ +int limit; +int s = offsetof(struct vhost_memory , regions); +struct vhost_memory *mem =g_malloc0 (s); + +assert (dev->mem->nregions); +do { + s +=sizeof mem->regions[0 ]; + mem =g_realloc (mem, s); + mem->regions[mem->nregions] = dev->mem->regions[0 ]; + mem->nregions++; + }while (vhost_kernel_call (dev,VHOST_SET_MEM_TABLE , mem) !=-1 ); + limit = mem->nregions -1 >0 ? mem->nregions -1 :0 ; +g_free (mem); + +return limit; +} +static const VhostOps kernel_ops = { .backend_type =VHOST_BACKEND_TYPE_KERNEL , .vhost_call = vhost_kernel_call, .vhost_backend_init = vhost_kernel_init, - .vhost_backend_cleanup = vhost_kernel_cleanup + .vhost_backend_cleanup = vhost_kernel_cleanup, + .vhost_backend_memslots_limit = vhost_kernel_memslots_limit };int vhost_set_backend_type (struct vhost_dev *dev,VhostBackendType backend_type)
hw/virtio/vhost-user.c +7 -1
@@ -352,9 +352,15 @@ static int vhost_user_cleanup(struct vhost_dev *dev)return 0 ; } +static int vhost_user_memslots_limit (struct vhost_dev *dev) +{ +return VHOST_MEMORY_MAX_NREGIONS ; +} +const VhostOps user_ops = { .backend_type =VHOST_BACKEND_TYPE_USER , .vhost_call = vhost_user_call, .vhost_backend_init = vhost_user_init, - .vhost_backend_cleanup = vhost_user_cleanup + .vhost_backend_cleanup = vhost_user_cleanup, + .vhost_backend_memslots_limit = vhost_user_memslots_limit };
hw/virtio/vhost.c +21 -0
@@ -26,6 +26,18 @@static struct vhost_log *vhost_log; +static int used_memslots; +static int memslots_limit =-1 ; + +bool vhost_has_free_slot (void ) +{ +if (memslots_limit >=0 ) { +return memslots_limit > used_memslots; + } + +return true; +} + static void vhost_dev_sync_region(struct vhost_dev *dev, MemoryRegionSection *section, uint64_t mfirst, uint64_t mlast,
@@ -457,6 +469,7 @@ static void vhost_set_memory(MemoryListener *listener, dev->mem_changed_start_addr =MIN (dev->mem_changed_start_addr, start_addr); dev->mem_changed_end_addr =MAX (dev->mem_changed_end_addr, start_addr + size -1 ); dev->memory_changed = true; + used_memslots = dev->mem->nregions; }static bool vhost_section (MemoryRegionSection *section)
@@ -1119,6 +1132,14 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev)if (r <0 ) { goto fail_features; } + + r = hdev->vhost_ops->vhost_backend_memslots_limit (hdev); +if (memslots_limit >0 ) { + memslots_limit =MIN (memslots_limit, r); + }else { + memslots_limit = r; + } + r = hdev->vhost_ops->vhost_call (hdev,VHOST_SET_MEM_TABLE , hdev->mem);if (r <0 ) { r = -errno;
include/hw/virtio/vhost-backend.h +2 -0
@@ -24,12 +24,14 @@ typedef int (*vhost_call)(struct vhost_dev *dev, unsigned long int request, void *arg);typedef int (*vhost_backend_init )(struct vhost_dev *dev,void *opaque);typedef int (*vhost_backend_cleanup )(struct vhost_dev *dev); +typedef int (*vhost_backend_memslots_limit )(struct vhost_dev *dev);typedef struct VhostOps {VhostBackendType backend_type;vhost_call vhost_call;vhost_backend_init vhost_backend_init;vhost_backend_cleanup vhost_backend_cleanup; +vhost_backend_memslots_limit vhost_backend_memslots_limit; }VhostOps ;extern const VhostOps user_ops;
include/hw/virtio/vhost.h +1 -0
@@ -81,4 +81,5 @@ uint64_t vhost_get_features(struct vhost_dev *hdev, const int *feature_bits, uint64_t features);void vhost_ack_features (struct vhost_dev *hdev,const int *feature_bits,uint64_t features); +bool vhost_has_free_slot (void );#endif
stubs/Makefile.objs +1 -0
@@ -38,3 +38,4 @@ stub-obj-$(CONFIG_WIN32) += fd-register.o stub-obj-y += cpus.o stub-obj-y += kvm.o stub-obj-y += qmp_pc_dimm_device_list.o +stub-obj-y += vhost.o
stubs/vhost.c new file +6 -0
@@ -0,0 +1,6 @@ +#include "hw/virtio/vhost.h" + +bool vhost_has_free_slot (void ) +{ +return true; +}
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH 1/2] vhost: add vhost_has_free_slot() interface
Quoted 42 lines · Igor Mammedov
it will allow for other parts of QEMU check if it's safe to map memory region during hotplug/runtime. That way hotplug path will have a chance to cancel hotplug operation instead of crashing in
vhost_commit().Signed-off-by Igor Mammedov <[email protected]>--- hw/virtio/vhost-backend.c | 23 ++++++++++++++++++++++- hw/virtio/vhost-user.c | 8 +++++++- hw/virtio/vhost.c | 21 +++++++++++++++++++++ include/hw/virtio/vhost-backend.h | 2 ++ include/hw/virtio/vhost.h | 1 + stubs/Makefile.objs | 1 + stubs/vhost.c | 6 ++++++ 7 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 stubs/vhost.chw/virtio/vhost-backend.c +10
-0@@ -11,6 +11,7 @@#include "hw/virtio/vhost.h" #include "hw/virtio/vhost-backend.h" #include "qemu/error-report.h" +#include "linux/vhost.h" #include <sys/ioctl.h> @@ -42,11 +43,31 @@ static int vhost_kernel_cleanup(struct vhost_dev *dev)return close (fd); } +static int vhost_kernel_memslots_limit (struct vhost_dev *dev) +{ +int limit; +int s = offsetof(struct vhost_memory , regions); +struct vhost_memory *mem =g_malloc0 (s); + +assert (dev->mem->nregions); + do { + s += sizeof mem->regions[0];
hw/virtio/vhost-backend.c static int vhost_kernel_cleanup(struct vhost_dev *dev) · line 64
Quoted 10 lines
hw/virtio/vhost-backend.c +10
-0+ mem =g_realloc (mem, s); + mem->regions[mem->nregions] = dev->mem->regions[0 ]; + mem->nregions++; + }while (vhost_kernel_call (dev,VHOST_SET_MEM_TABLE , mem) !=-1 ); + limit = mem->nregions -1 >0 ? mem->nregions -1 :0 ; +g_free (mem); + +return limit; +} +
I don't like this probing much: if one clocks the size up significantly, this will slow down the box trying to allocate kernel memory for no real reason.
I'd rather check /sys/modules/vhost/parameters/max_mem_regions. If not there, assume 64.
Quoted 129 lines
hw/virtio/vhost-backend.c +2
-1static const VhostOps kernel_ops = { .backend_type =VHOST_BACKEND_TYPE_KERNEL , .vhost_call = vhost_kernel_call, .vhost_backend_init = vhost_kernel_init, - .vhost_backend_cleanup = vhost_kernel_cleanup + .vhost_backend_cleanup = vhost_kernel_cleanup, + .vhost_backend_memslots_limit = vhost_kernel_memslots_limit };int vhost_set_backend_type (struct vhost_dev *dev,VhostBackendType backend_type)hw/virtio/vhost-user.c +7
-1@@ -352,9 +352,15 @@ static int vhost_user_cleanup(struct vhost_dev *dev)return 0 ; } +static int vhost_user_memslots_limit (struct vhost_dev *dev) +{ +return VHOST_MEMORY_MAX_NREGIONS ; +} +const VhostOps user_ops = { .backend_type =VHOST_BACKEND_TYPE_USER , .vhost_call = vhost_user_call, .vhost_backend_init = vhost_user_init, - .vhost_backend_cleanup = vhost_user_cleanup + .vhost_backend_cleanup = vhost_user_cleanup, + .vhost_backend_memslots_limit = vhost_user_memslots_limit };hw/virtio/vhost.c +21
-0@@ -26,6 +26,18 @@static struct vhost_log *vhost_log; +static int used_memslots; +static int memslots_limit =-1 ; + +bool vhost_has_free_slot (void ) +{ +if (memslots_limit >=0 ) { +return memslots_limit > used_memslots; + } + +return true; +} + static void vhost_dev_sync_region(struct vhost_dev *dev, MemoryRegionSection *section, uint64_t mfirst, uint64_t mlast,@@ -457,6 +469,7 @@ static void vhost_set_memory(MemoryListener *listener, dev->mem_changed_start_addr =MIN (dev->mem_changed_start_addr, start_addr); dev->mem_changed_end_addr =MAX (dev->mem_changed_end_addr, start_addr + size -1 ); dev->memory_changed = true; + used_memslots = dev->mem->nregions; }static bool vhost_section (MemoryRegionSection *section)@@ -1119,6 +1132,14 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev)if (r <0 ) { goto fail_features; } + + r = hdev->vhost_ops->vhost_backend_memslots_limit (hdev); +if (memslots_limit >0 ) { + memslots_limit =MIN (memslots_limit, r); + }else { + memslots_limit = r; + } + r = hdev->vhost_ops->vhost_call (hdev,VHOST_SET_MEM_TABLE , hdev->mem);if (r <0 ) { r = -errno;include/hw/virtio/vhost-backend.h +2
-0@@ -24,12 +24,14 @@ typedef int (*vhost_call)(struct vhost_dev *dev, unsigned long int request, void *arg);typedef int (*vhost_backend_init )(struct vhost_dev *dev,void *opaque);typedef int (*vhost_backend_cleanup )(struct vhost_dev *dev); +typedef int (*vhost_backend_memslots_limit )(struct vhost_dev *dev);typedef struct VhostOps {VhostBackendType backend_type;vhost_call vhost_call;vhost_backend_init vhost_backend_init;vhost_backend_cleanup vhost_backend_cleanup; +vhost_backend_memslots_limit vhost_backend_memslots_limit; }VhostOps ;extern const VhostOps user_ops;include/hw/virtio/vhost.h +1
-0@@ -81,4 +81,5 @@ uint64_t vhost_get_features(struct vhost_dev *hdev, const int *feature_bits, uint64_t features);void vhost_ack_features (struct vhost_dev *hdev,const int *feature_bits,uint64_t features); +bool vhost_has_free_slot (void );#endif stubs/Makefile.objs +1
-0@@ -38,3 +38,4 @@ stub-obj-$(CONFIG_WIN32) += fd-register.o stub-obj-y += cpus.o stub-obj-y += kvm.o stub-obj-y += qmp_pc_dimm_device_list.o +stub-obj-y += vhost.ostubs/vhost.c new file +6
-0@@ -0,0 +1,6 @@ +#include "hw/virtio/vhost.h" + +bool vhost_has_free_slot (void ) +{ +return true; +}Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH 1/2] vhost: add vhost_has_free_slot() interface
Quoted 58 lines · On Wed, 29 Jul 2015 13:20:24 +0300 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 42 lines · Igor Mammedov
it will allow for other parts of QEMU check if it's safe to map memory region during hotplug/runtime. That way hotplug path will have a chance to cancel hotplug operation instead of crashing in
vhost_commit().Signed-off-by Igor Mammedov <[email protected]>--- hw/virtio/vhost-backend.c | 23 ++++++++++++++++++++++- hw/virtio/vhost-user.c | 8 +++++++- hw/virtio/vhost.c | 21 +++++++++++++++++++++ include/hw/virtio/vhost-backend.h | 2 ++ include/hw/virtio/vhost.h | 1 + stubs/Makefile.objs | 1 + stubs/vhost.c | 6 ++++++ 7 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 stubs/vhost.chw/virtio/vhost-backend.c +10
-0@@ -11,6 +11,7 @@#include "hw/virtio/vhost.h" #include "hw/virtio/vhost-backend.h" #include "qemu/error-report.h" +#include "linux/vhost.h" #include <sys/ioctl.h> @@ -42,11 +43,31 @@ static int vhost_kernel_cleanup(struct vhost_dev *dev)return close (fd); } +static int vhost_kernel_memslots_limit (struct vhost_dev *dev) +{ +int limit; +int s = offsetof(struct vhost_memory , regions); +struct vhost_memory *mem =g_malloc0 (s); + +assert (dev->mem->nregions); + do { + s += sizeof mem->regions[0];hw/virtio/vhost-backend.c static int vhost_kernel_cleanup(struct vhost_dev *dev) · line 64
Quoted 10 lines
hw/virtio/vhost-backend.c +10
-0+ mem =g_realloc (mem, s); + mem->regions[mem->nregions] = dev->mem->regions[0 ]; + mem->nregions++; + }while (vhost_kernel_call (dev,VHOST_SET_MEM_TABLE , mem) !=-1 ); + limit = mem->nregions -1 >0 ? mem->nregions -1 :0 ; +g_free (mem); + +return limit; +} +I don't like this probing much: if one clocks the size up significantly, this will slow down the box trying to allocate kernel memory for no real reason.
Quoted 2 lines
I'd rather check
/sys/modules/vhost/parameters/max_mem_regions. If not there, assume 64.
I'll respin with this suggestion even if it's a bit ugly. In addition to it we can add IOCTL interface to detect limit later when kernel side would be ready for it.
Quoted 131 lines
Quoted 130 lines
hw/virtio/vhost-backend.c +2
-1static const VhostOps kernel_ops = { .backend_type =VHOST_BACKEND_TYPE_KERNEL , .vhost_call = vhost_kernel_call, .vhost_backend_init = vhost_kernel_init, - .vhost_backend_cleanup = vhost_kernel_cleanup + .vhost_backend_cleanup = vhost_kernel_cleanup, + .vhost_backend_memslots_limit = vhost_kernel_memslots_limit };int vhost_set_backend_type (struct vhost_dev *dev,VhostBackendType backend_type)hw/virtio/vhost-user.c +7
-1@@ -352,9 +352,15 @@ static int vhost_user_cleanup(struct vhost_dev *dev)return 0 ; } +static int vhost_user_memslots_limit (struct vhost_dev *dev) +{ +return VHOST_MEMORY_MAX_NREGIONS ; +} +const VhostOps user_ops = { .backend_type =VHOST_BACKEND_TYPE_USER , .vhost_call = vhost_user_call, .vhost_backend_init = vhost_user_init, - .vhost_backend_cleanup = vhost_user_cleanup + .vhost_backend_cleanup = vhost_user_cleanup, + .vhost_backend_memslots_limit = vhost_user_memslots_limit };hw/virtio/vhost.c +21
-0@@ -26,6 +26,18 @@static struct vhost_log *vhost_log; +static int used_memslots; +static int memslots_limit =-1 ; + +bool vhost_has_free_slot (void ) +{ +if (memslots_limit >=0 ) { +return memslots_limit > used_memslots; + } + +return true; +} + static void vhost_dev_sync_region(struct vhost_dev *dev, MemoryRegionSection *section, uint64_t mfirst, uint64_t mlast,@@ -457,6 +469,7 @@ static void vhost_set_memory(MemoryListener *listener, dev->mem_changed_start_addr =MIN (dev->mem_changed_start_addr, start_addr); dev->mem_changed_end_addr =MAX (dev->mem_changed_end_addr, start_addr + size -1 ); dev->memory_changed = true; + used_memslots = dev->mem->nregions; }static bool vhost_section (MemoryRegionSection *section)@@ -1119,6 +1132,14 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev)if (r <0 ) { goto fail_features; } + + r = hdev->vhost_ops->vhost_backend_memslots_limit (hdev); +if (memslots_limit >0 ) { + memslots_limit =MIN (memslots_limit, r); + }else { + memslots_limit = r; + } + r = hdev->vhost_ops->vhost_call (hdev,VHOST_SET_MEM_TABLE , hdev->mem);if (r <0 ) { r = -errno;include/hw/virtio/vhost-backend.h +2
-0@@ -24,12 +24,14 @@ typedef int (*vhost_call)(struct vhost_dev *dev, unsigned long int request, void *arg);typedef int (*vhost_backend_init )(struct vhost_dev *dev,void *opaque);typedef int (*vhost_backend_cleanup )(struct vhost_dev *dev); +typedef int (*vhost_backend_memslots_limit )(struct vhost_dev *dev);typedef struct VhostOps {VhostBackendType backend_type;vhost_call vhost_call;vhost_backend_init vhost_backend_init;vhost_backend_cleanup vhost_backend_cleanup; +vhost_backend_memslots_limit vhost_backend_memslots_limit; }VhostOps ;extern const VhostOps user_ops;include/hw/virtio/vhost.h +1
-0@@ -81,4 +81,5 @@ uint64_t vhost_get_features(struct vhost_dev *hdev, const int *feature_bits, uint64_t features);void vhost_ack_features (struct vhost_dev *hdev,const int *feature_bits,uint64_t features); +bool vhost_has_free_slot (void );#endif stubs/Makefile.objs +1
-0@@ -38,3 +38,4 @@ stub-obj-$(CONFIG_WIN32) += fd-register.o stub-obj-y += cpus.o stub-obj-y += kvm.o stub-obj-y += qmp_pc_dimm_device_list.o +stub-obj-y += vhost.ostubs/vhost.c new file +6
-0@@ -0,0 +1,6 @@ +#include "hw/virtio/vhost.h" + +bool vhost_has_free_slot (void ) +{ +return true; +}Signature · 1 line
1.8.3.1
PATCH 2/2 pc-dimm: add vhost slots limit check before commiting to hotplug
In reply to Igor Mammedovit allows safely cancel memory hotplug if vhost backend doesn't support necessary amount of memory slots and prevents QEMU crashing in vhost due to hitting vhost limit on amount of supported memory ranges.
--- hw/mem/pc-dimm.c | 7 +++++++ 1 file changed, 7 insertions(+)
hw/mem/pc-dimm.c +7 -0
@@ -25,6 +25,7 @@#include "sysemu/numa.h" #include "sysemu/kvm.h" #include "trace.h" +#include "hw/virtio/vhost.h" typedef struct pc_dimms_capacity { uint64_t size;
@@ -95,6 +96,12 @@ void pc_dimm_memory_plug(DeviceState *dev, MemoryHotplugState *hpms, goto out; } +if (!vhost_has_free_slot ()) { +error_setg (&local_err,"a used vhost backend has no free" +" memory slots left" ); + goto out; + } +memory_region_add_subregion (&hpms->mr, addr - hpms->base, mr);vmstate_register_ram (mr, dev);numa_set_mem_node_id (addr,memory_region_size (mr), dimm->node);
Signature · 1 line
1.8.3.1
Quoted 8 lines · On Mon, 20 Jul 2015 15:49:28 +0200 Igor Mammedov <[email protected]> wrote:
it's defensive patchset which helps to avoid QEMU crashing at memory hotplug time by checking that vhost has free capacity for an additional memory slot.
Igor Mammedov (2):
vhost: addvhost_has_free_slot()interface
pc-dimm: add vhost slots limit check before commiting to hotplug
Quoted 11 lines
hw/mem/pc-dimm.c | 7 +++++++ hw/virtio/vhost-backend.c | 23 ++++++++++++++++++++++- hw/virtio/vhost-user.c | 8 +++++++- hw/virtio/vhost.c | 21 +++++++++++++++++++++ include/hw/virtio/vhost-backend.h | 2 ++ include/hw/virtio/vhost.h | 1 + stubs/Makefile.objs | 1 + stubs/vhost.c | 6 ++++++ 8 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 stubs/vhost.c
ping,
patches are not invasive and fix QEMU crash during memory hotplug when running on kernels that have lower vhost memory slots limit. Pls consider applying them for 2.4.
Quoted 11 lines · Igor Mammedov
Quoted 11 lines
Quoted 11 lines
hw/mem/pc-dimm.c | 7 +++++++ hw/virtio/vhost-backend.c | 23 ++++++++++++++++++++++- hw/virtio/vhost-user.c | 8 +++++++- hw/virtio/vhost.c | 21 +++++++++++++++++++++ include/hw/virtio/vhost-backend.h | 2 ++ include/hw/virtio/vhost.h | 1 + stubs/Makefile.objs | 1 + stubs/vhost.c | 6 ++++++ 8 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 stubs/vhost.c
Quoted 5 lines
ping,
patches are not invasive and fix QEMU crash during memory hotplug when running on kernels that have lower vhost memory slots limit. Pls consider applying them for 2.4.
I like the approach. For 2.5 we could consider using a
NotifierWithReturn: it can be easily extended to cover KVM as well and removes the need for the stub.
Paolo