All of lore.kernel.org
 help / color / mirror / Atom feed
From: Gokul K <gokul02k@gmail.com>
To: Sean Christopherson <seanjc@google.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	kvm@vger.kernel.org
Cc: Shuah Khan <shuah@kernel.org>,
	linux-kselftest@vger.kernel.org, Peter Gonda <pgonda@google.com>,
	Jing Zhang <jingzhangos@google.com>,
	linux-kernel@vger.kernel.org
Subject: [PATCH 2/3] KVM: selftests: Close the SEV fd opened for each KVM_SEV_INIT2 ioctl
Date: Tue,  1 Dec 2026 14:36:31 +0530	[thread overview]
Message-ID: <20261201090632.2482795-3-gokul02k@gmail.com> (raw)
In-Reply-To: <20261201090632.2482795-1-gokul02k@gmail.com>

__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



  parent reply	other threads:[~2026-12-01  9:06 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201090632.2482795-3-gokul02k@gmail.com \
    --to=gokul02k@gmail.com \
    --cc=jingzhangos@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=pgonda@google.com \
    --cc=seanjc@google.com \
    --cc=shuah@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.