Kernel KVM virtualization development
 help / color / mirror / Atom feed
* [PATCH 0/3] KVM: selftests: Fix three leaked resources
@ 2026-12-01  9:06 Gokul K
  2026-12-01  9:06 ` [PATCH 1/3] KVM: selftests: Close /dev/sev when an SEV VM is released Gokul K
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ 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

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(-)

-- 
2.51.0



^ permalink raw reply	[flat|nested] 5+ messages in thread

* [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; 5+ 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 related	[flat|nested] 5+ 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; 5+ 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 related	[flat|nested] 5+ 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
  2026-12-01  9:12   ` sashiko-bot
  2 siblings, 1 reply; 5+ 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 related	[flat|nested] 5+ messages in thread

* Re: [PATCH 3/3] KVM: selftests: Free the stats id string when no stats are defined
  2026-12-01  9:06 ` [PATCH 3/3] KVM: selftests: Free the stats id string when no stats are defined Gokul K
@ 2026-12-01  9:12   ` sashiko-bot
  0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-12-01  9:12 UTC (permalink / raw)
  To: Gokul K, kvm

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 <gokul02k@gmail.com>

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.

> diff --git a/tools/testing/selftests/kvm/kvm_binary_stats_test.c b/tools/testing/selftests/kvm/kvm_binary_stats_test.c
> index b7dbde9c08436..89ae011ffae9f 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;
>  	}

[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.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261201090632.2482795-1-gokul02k@gmail.com?part=3

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-12-01  9:12 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH 3/3] KVM: selftests: Free the stats id string when no stats are defined Gokul K
2026-12-01  9:12   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox