All of lore.kernel.org
 help / color / mirror / Atom feed
From: Trieu Huynh <vikingtc4@gmail.com>
To: qemu-devel@nongnu.org
Cc: Trieu Huynh <vikingtc4@gmail.com>, Peter Xu <peterx@redhat.com>,
	Fabiano Rosas <farosas@suse.de>,
	Laurent Vivier <lvivier@redhat.com>,
	Paolo Bonzini <pbonzini@redhat.com>
Subject: [PATCH] tests/qtest/migration: fix fd leak in ufd_version_check
Date: Sun, 19 Apr 2026 18:03:04 +0700	[thread overview]
Message-ID: <20260419110304.8661-1-viking4@gmail.com> (raw)

From: Trieu Huynh <vikingtc4@gmail.com>

ufd_version_check() opens a userfaultfd via uffd_open() but never closes
it on any path where the open succeeded: the UFFDIO_API failure path,
the missing-ioctls path, and the success path all returned without
calling close(ufd).

Convert to a goto-out pattern consistent with uffd_open() used in
util/userfaultfd.c and migration/postcopy-ram.c, ensuring the fd is
always closed before returning.

Signed-off-by: Trieu Huynh <vikingtc4@gmail.com>
---
 tests/qtest/migration/migration-util.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/tests/qtest/migration/migration-util.c b/tests/qtest/migration/migration-util.c
index 2648ad7f61..db66d8b24f 100644
--- a/tests/qtest/migration/migration-util.c
+++ b/tests/qtest/migration/migration-util.c
@@ -343,6 +343,7 @@ bool ufd_version_check(bool *uffd_feature_thread_id)
 {
     struct uffdio_api api_struct;
     uint64_t ioctl_mask;
+    bool ret = false;
 
     int ufd = uffd_open(O_CLOEXEC);
 
@@ -355,7 +356,7 @@ bool ufd_version_check(bool *uffd_feature_thread_id)
     api_struct.features = 0;
     if (ioctl(ufd, UFFDIO_API, &api_struct)) {
         g_test_message("Skipping test: UFFDIO_API failed");
-        return false;
+        goto release_ufd;
     }
 
     if (uffd_feature_thread_id) {
@@ -366,10 +367,13 @@ bool ufd_version_check(bool *uffd_feature_thread_id)
                   1ULL << _UFFDIO_UNREGISTER);
     if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) {
         g_test_message("Skipping test: Missing userfault feature");
-        return false;
+        goto release_ufd;
     }
 
-    return true;
+    ret = true;
+release_ufd:
+    close(ufd);
+    return ret;
 }
 #else
 bool ufd_version_check(bool *uffd_feature_thread_id)
-- 
2.43.0



             reply	other threads:[~2026-04-19 11:04 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-19 11:03 Trieu Huynh [this message]
2026-04-20 19:57 ` [PATCH] tests/qtest/migration: fix fd leak in ufd_version_check Peter Xu

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=20260419110304.8661-1-viking4@gmail.com \
    --to=vikingtc4@gmail.com \
    --cc=farosas@suse.de \
    --cc=lvivier@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peterx@redhat.com \
    --cc=qemu-devel@nongnu.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.