* [PATCH 1/3] KVM: selftests: Close /dev/sev when an SEV VM is released
2026-12-01 9:06 [PATCH 0/3] KVM: selftests: Fix three leaked resources Gokul K
@ 2026-12-01 9:06 ` Gokul K
2026-12-01 9:06 ` [PATCH 2/3] KVM: selftests: Close the SEV fd opened for each KVM_SEV_INIT2 ioctl Gokul K
2026-12-01 9:06 ` [PATCH 3/3] KVM: selftests: Free the stats id string when no stats are defined Gokul K
2 siblings, 0 replies; 4+ messages in thread
From: Gokul K @ 2026-12-01 9:06 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini, kvm
Cc: Shuah Khan, linux-kselftest, Peter Gonda, Jing Zhang, linux-kernel
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@gmail.com>
---
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
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH 2/3] KVM: selftests: Close the SEV fd opened for each KVM_SEV_INIT2 ioctl
2026-12-01 9:06 [PATCH 0/3] KVM: selftests: Fix three leaked resources Gokul K
2026-12-01 9:06 ` [PATCH 1/3] KVM: selftests: Close /dev/sev when an SEV VM is released Gokul K
@ 2026-12-01 9:06 ` Gokul K
2026-12-01 9:06 ` [PATCH 3/3] KVM: selftests: Free the stats id string when no stats are defined Gokul K
2 siblings, 0 replies; 4+ messages in thread
From: Gokul K @ 2026-12-01 9:06 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini, kvm
Cc: Shuah Khan, linux-kselftest, Peter Gonda, Jing Zhang, linux-kernel
__sev_ioctl() opens /dev/sev to fill in kvm_sev_cmd.sev_fd and then drops
the descriptor on the floor. open_sev_dev_path_or_exit() opens the device
afresh on every call, so the leak is one descriptor per ioctl, not one for
the test.
The test issues a lot of them. test_flags() runs 32 iterations and
test_features() runs 64, each for up to three VM types, so a machine that
supports SEV, SEV-ES and SEV-SNP gets through nearly three hundred calls.
Hold the descriptor in a local and close it once the ioctl is done. Every
other caller of open_sev_dev_path_or_exit() stores the fd in
vm->arch.sev_fd, which the previous patch makes kvm_vm_release() close;
this is the only caller that owns the fd itself.
Fixes: dfc083a181ba ("selftests: kvm: add tests for KVM_SEV_INIT2")
Signed-off-by: Gokul K <gokul02k@gmail.com>
---
tools/testing/selftests/kvm/x86/sev_init2_tests.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/kvm/x86/sev_init2_tests.c b/tools/testing/selftests/kvm/x86/sev_init2_tests.c
index 87bff4fbb7ed..09c8d9620bd1 100644
--- a/tools/testing/selftests/kvm/x86/sev_init2_tests.c
+++ b/tools/testing/selftests/kvm/x86/sev_init2_tests.c
@@ -32,10 +32,11 @@ bool have_snp;
static int __sev_ioctl(int vm_fd, int cmd_id, void *data)
{
+ int sev_fd = open_sev_dev_path_or_exit();
struct kvm_sev_cmd cmd = {
.id = cmd_id,
.data = (u64)data,
- .sev_fd = open_sev_dev_path_or_exit(),
+ .sev_fd = sev_fd,
};
int ret;
@@ -44,6 +45,8 @@ static int __sev_ioctl(int vm_fd, int cmd_id, void *data)
"%d failed: fw error: %d\n",
cmd_id, cmd.error);
+ kvm_free_fd(sev_fd);
+
return ret;
}
--
2.54.0
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH 3/3] KVM: selftests: Free the stats id string when no stats are defined
2026-12-01 9:06 [PATCH 0/3] KVM: selftests: Fix three leaked resources Gokul K
2026-12-01 9:06 ` [PATCH 1/3] KVM: selftests: Close /dev/sev when an SEV VM is released Gokul K
2026-12-01 9:06 ` [PATCH 2/3] KVM: selftests: Close the SEV fd opened for each KVM_SEV_INIT2 ioctl Gokul K
@ 2026-12-01 9:06 ` Gokul K
2 siblings, 0 replies; 4+ messages in thread
From: Gokul K @ 2026-12-01 9:06 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini, kvm
Cc: Shuah Khan, linux-kselftest, Peter Gonda, Jing Zhang, linux-kernel
stats_test() allocates the id string before it sanity checks the header,
then returns early if the kernel reports no stats descriptors. The normal
path frees id along with stats_desc and stats_data, but this return does
not, so the allocation is lost.
A goto would be wrong here: stats_desc and stats_data are still
uninitialised at this point, and the common exit path frees both. Free the
one live allocation instead.
Fixes: 0b45d58738cd ("KVM: selftests: Add selftest for KVM statistics data binary interface")
Signed-off-by: Gokul K <gokul02k@gmail.com>
---
tools/testing/selftests/kvm/kvm_binary_stats_test.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/tools/testing/selftests/kvm/kvm_binary_stats_test.c b/tools/testing/selftests/kvm/kvm_binary_stats_test.c
index b7dbde9c0843..89ae011ffae9 100644
--- a/tools/testing/selftests/kvm/kvm_binary_stats_test.c
+++ b/tools/testing/selftests/kvm/kvm_binary_stats_test.c
@@ -53,6 +53,7 @@ static void stats_test(int stats_fd)
/* Sanity check for other fields in header */
if (header.num_desc == 0) {
ksft_print_msg("No KVM stats defined!\n");
+ free(id);
return;
}
/*
--
2.54.0
^ permalink raw reply [flat|nested] 4+ messages in thread