KVM: selftests: Fix three leaked resources
cover KVM: selftests: Fix three leaked resources
Three resource leaks found while auditing the selftests, all on paths that run normally rather than on error paths.
Patches 1 and 2 are both /dev/sev descriptors. The first is the one that
matters: every SEV VM opens /dev/sev into vm->arch.sev_fd and nothing
ever closes it, because x86 never provided a kvm_arch_vm_release(). arm64 is the only architecture that overrides that hook today. The second is sev_init2_tests.c opening
the device once per KVM_SEV_INIT2 ioctl and keeping none of them.
Patch 3 is unrelated to SEV: kvm_binary_stats_test.c loses the id string when the kernel reports no stats descriptors.
Compile-tested and run on an AMD host without SEV, so patches 1 and 2 are compile-tested only - the paths they touch need an SEV-capable machine. kvm_binary_stats_test passes 4/4 before and after patch 3. Nine x86 selftests were run with and without patch 1, which touches the library every x86 test links, and gave identical results:set_memory_region_test and x86/state_test fail both ways, as this host's kernel is older than the tree, and x86/kvm_clock_test skips.
Gokul K (3):
KVM: selftests: Close /dev/sev when an SEV VM is released
KVM: selftests: Close the SEV fd opened for each KVM_SEV_INIT2 ioctl
KVM: selftests: Free the stats id string when no stats are defined
tools/testing/selftests/kvm/kvm_binary_stats_test.c | 1 + tools/testing/selftests/kvm/lib/x86/processor.c | 6 ++++++ tools/testing/selftests/kvm/x86/sev_init2_tests.c | 5 ++++- 3 files changed, 11 insertions(+), 1 deletion(-)
Signature · 1 line
2.51.0
PATCH 1/3 KVM: selftests: Close /dev/sev when an SEV VM is released
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.
--- tools/testing/selftests/kvm/lib/x86/processor.c | 6 ++++++ 1 file changed, 6 insertions(+)
tools/testing/selftests/kvm/lib/x86/processor.c +6 -0
@@ -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 );
Signature · 1 line
2.54.0
PATCH 2/3 KVM: selftests: Close the SEV fd opened for each KVM_SEV_INIT2 ioctl
In reply to Gokul K__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.
--- tools/testing/selftests/kvm/x86/sev_init2_tests.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/x86/sev_init2_tests.c +4 -1
@@ -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; }
Signature · 1 line
2.54.0
PATCH 3/3 KVM: selftests: Free the stats id string when no stats are defined
In reply to Gokul Kstats_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.
--- tools/testing/selftests/kvm/kvm_binary_stats_test.c | 1 + 1 file changed, 1 insertion(+)
tools/testing/selftests/kvm/kvm_binary_stats_test.c +1 -0
@@ -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 ; }/*
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH 3/3] KVM: selftests: Free the stats id string when no stats are defined
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] KVM: selftests: stats_fd leak in stats_test() when header.num_desc == 0 --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] KVM: selftests: stats_fd leak in stats_test() when header.num_desc == 0
--
commit 38dd872f0f6bd2de7be54a91cb51a47a844eca82
Author: Gokul K <[email protected]>
KVM: selftests: Free the stats id string when no stats are defined
This commit frees the allocated id string in stats_test() before returning early when header.num_desc is zero.
tools/testing/selftests/kvm/kvm_binary_stats_test.c static void stats_test(int stats_fd) · line 58
Quoted 11 lines
tools/testing/selftests/kvm/kvm_binary_stats_test.c +1
-0@@ -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 ; }
[Severity: Low]
Does this early return still leak stats_fd?
main() passes newly opened or duplicated file descriptors directly to stats_test(), such as stats_test(kvm_dup(vm_stats_fds)) and stats_test(vm_get_stats_fd(vms[i])), and relies on the tail of stats_test() to close them:
tools/testing/selftests/kvm/kvm_binary_stats_test.c:stats_test() {
...
free(stats_data);
free(stats_desc);
free(id);
close(stats_fd);
TEST_ASSERT(fcntl(stats_fd, F_GETFD) == -1, "Stats fd not freed");
}Placing a label right before free(id) at the end of stats_test() and jumping to it here would free id and close stats_fd without touching the uninitialized stats_data and stats_desc pointers.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3