[PATCH 1/3] KVM: selftests: Close /dev/sev when an SEV VM is released

From: Gokul K

Date: Tue Dec 01 2026 - 04:07:00 EST


Every SEV VM opens /dev/sev and stores the descriptor in
vm->arch.sev_fd: kvm_init_vm_address_properties() does it for the SEV VM
types, and sev_vm_init() and sev_es_vm_init() for SEV VMs of the default
type. Nothing ever closes it. kvm_vm_release() does call
kvm_arch_vm_release(), however x86 never provided one: the __weak stub in
lib/kvm_util.c is empty, and arm64 is currently the only architecture
that overrides it, to close its GIC fd.

The descriptor therefore stays open for the lifetime of the process, so a
test that creates and frees SEV VMs in a loop accumulates one per VM.

Add an x86 kvm_arch_vm_release() that closes it, mirroring arm64.
vm->arch.sev_fd is -1 unless one was opened, so guard on that.

Fixes: ae20eef5323c ("KVM: selftests: Add library for creating and interacting with SEV guests")
Signed-off-by: Gokul K <gokul02k@xxxxxxxxx>
---
tools/testing/selftests/kvm/lib/x86/processor.c | 6 ++++++
1 file changed, 6 insertions(+)

diff --git a/tools/testing/selftests/kvm/lib/x86/processor.c b/tools/testing/selftests/kvm/lib/x86/processor.c
index ee8a1715d844..b716c53b6721 100644
--- a/tools/testing/selftests/kvm/lib/x86/processor.c
+++ b/tools/testing/selftests/kvm/lib/x86/processor.c
@@ -1347,6 +1347,12 @@ void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits)
}
}

+void kvm_arch_vm_release(struct kvm_vm *vm)
+{
+ if (vm->arch.sev_fd >= 0)
+ kvm_free_fd(vm->arch.sev_fd);
+}
+
void kvm_init_vm_address_properties(struct kvm_vm *vm)
{
u32 gpa_bits = kvm_cpu_property(X86_PROPERTY_GUEST_MAX_PHY_ADDR);
--
2.54.0