In case a KVM_REQ_VM_DEAD request was sent to a VM, subsequent KVM ioctls will fail and cause test failure. This now happens with an aarch64 vgic test where the kvm_vm_free() fails. Let's add a new kvm_vm_dead_free() helper that does all the deallocation besides the KVM_SET_USER_MEMORY_REGION2 ioctl. Signed-off-by: Eric Auger <eric.auger@xxxxxxxxxx> --- .../testing/selftests/kvm/include/kvm_util.h | 1 + tools/testing/selftests/kvm/lib/kvm_util.c | 25 ++++++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h index 90424bfe33bd..8be893b2c6a2 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -437,6 +437,7 @@ void vm_enable_dirty_ring(struct kvm_vm *vm, uint32_t ring_size); const char *vm_guest_mode_string(uint32_t i); void kvm_vm_free(struct kvm_vm *vmp); +void kvm_vm_dead_free(struct kvm_vm *vmp); void kvm_vm_restart(struct kvm_vm *vmp); void kvm_vm_release(struct kvm_vm *vmp); void kvm_vm_elf_load(struct kvm_vm *vm, const char *filename); diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index a2b7df5f1d39..befbbe989d73 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -712,7 +712,8 @@ void kvm_vm_release(struct kvm_vm *vmp) } static void __vm_mem_region_delete(struct kvm_vm *vm, - struct userspace_mem_region *region) + struct userspace_mem_region *region, + bool dead) { int ret; @@ -720,8 +721,10 @@ static void __vm_mem_region_delete(struct kvm_vm *vm, rb_erase(®ion->hva_node, &vm->regions.hva_tree); hash_del(®ion->slot_node); - region->region.memory_size = 0; - vm_ioctl(vm, KVM_SET_USER_MEMORY_REGION2, ®ion->region); + if (!dead) { + region->region.memory_size = 0; + vm_ioctl(vm, KVM_SET_USER_MEMORY_REGION2, ®ion->region); + } sparsebit_free(®ion->unused_phy_pages); sparsebit_free(®ion->protected_phy_pages); @@ -742,7 +745,7 @@ static void __vm_mem_region_delete(struct kvm_vm *vm, /* * Destroys and frees the VM pointed to by vmp. */ -void kvm_vm_free(struct kvm_vm *vmp) +static void __kvm_vm_free(struct kvm_vm *vmp, bool dead) { int ctr; struct hlist_node *node; @@ -759,7 +762,7 @@ void kvm_vm_free(struct kvm_vm *vmp) /* Free userspace_mem_regions. */ hash_for_each_safe(vmp->regions.slot_hash, ctr, node, region, slot_node) - __vm_mem_region_delete(vmp, region); + __vm_mem_region_delete(vmp, region, dead); /* Free sparsebit arrays. */ sparsebit_free(&vmp->vpages_valid); @@ -771,6 +774,16 @@ void kvm_vm_free(struct kvm_vm *vmp) free(vmp); } +void kvm_vm_free(struct kvm_vm *vmp) +{ + __kvm_vm_free(vmp, false); +} + +void kvm_vm_dead_free(struct kvm_vm *vmp) +{ + __kvm_vm_free(vmp, true); +} + int kvm_memfd_alloc(size_t size, bool hugepages) { int memfd_flags = MFD_CLOEXEC; @@ -1197,7 +1210,7 @@ void vm_mem_region_move(struct kvm_vm *vm, uint32_t slot, uint64_t new_gpa) */ void vm_mem_region_delete(struct kvm_vm *vm, uint32_t slot) { - __vm_mem_region_delete(vm, memslot2region(vm, slot)); + __vm_mem_region_delete(vm, memslot2region(vm, slot), false); } void vm_guest_mem_fallocate(struct kvm_vm *vm, uint64_t base, uint64_t size, -- 2.41.0