[PATCH 2/3] KVM: selftests: Close the SEV fd opened for each KVM_SEV_INIT2 ioctl

From: Gokul K

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


__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@xxxxxxxxx>
---
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