All of lore.kernel.org
 help / color / mirror / Atom feed
From: Fabiano Rosas <farosas@suse.de>
To: qemu-devel@nongnu.org
Cc: Peter Xu <peterx@redhat.com>, Trieu Huynh <vikingtc4@gmail.com>
Subject: [PULL 23/43] migration: fix QIOChannelFile leak on error in file_connect_outgoing
Date: Thu, 23 Apr 2026 16:19:37 -0300	[thread overview]
Message-ID: <20260423191958.1440-24-farosas@suse.de> (raw)
In-Reply-To: <20260423191958.1440-1-farosas@suse.de>

From: Trieu Huynh <vikingtc4@gmail.com>

Commit 03a680c978 changed g_autoptr(QIOChannelFile) to a plain pointer
but failed to restore the necessary object_unref() calls on error paths.
Previously, these were handled implicitly by the g_autoptr cleanup
mechanism.

Two error paths currently leak the QIOChannelFile object and its
underlying file descriptor:

  1. When ftruncate() fails (e.g., on character or block devices).
  2. When qio_channel_io_seek() fails after the channel is created.

In environments that retry migration automatically (e.g., libvirt),
these FDs accumulate until QEMU hits RLIMIT_NOFILE and fails with
EMFILE (Too many open files).

Add the missing object_unref() calls to both error paths to ensure
resources are properly released.

Signed-off-by: Trieu Huynh <vikingtc4@gmail.com>
Reviewed-by: Peter Xu <peterx@redhat.com>
Link: https://lore.kernel.org/qemu-devel/20260328121215.159532-1-vikingtc4@gmail.com
Signed-off-by: Fabiano Rosas <farosas@suse.de>
---
 migration/file.c                   |  2 ++
 tests/qtest/migration/file-tests.c | 48 ++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+)

diff --git a/migration/file.c b/migration/file.c
index 5618aced49..962b4b3a1b 100644
--- a/migration/file.c
+++ b/migration/file.c
@@ -112,6 +112,7 @@ QIOChannel *file_connect_outgoing(MigrationState *s,
         error_setg_errno(errp, errno,
                          "failed to truncate migration file to offset %" PRIx64,
                          offset);
+        object_unref(OBJECT(fioc));
         goto out;
     }
 
@@ -119,6 +120,7 @@ QIOChannel *file_connect_outgoing(MigrationState *s,
 
     ioc = QIO_CHANNEL(fioc);
     if (offset && qio_channel_io_seek(ioc, offset, SEEK_SET, errp) < 0) {
+        object_unref(OBJECT(fioc));
         ioc = NULL;
         goto out;
     }
diff --git a/tests/qtest/migration/file-tests.c b/tests/qtest/migration/file-tests.c
index 5d1b861cf6..fef172068f 100644
--- a/tests/qtest/migration/file-tests.c
+++ b/tests/qtest/migration/file-tests.c
@@ -20,6 +20,51 @@
 
 static char *tmpfs;
 
+static int count_proc_fds(pid_t pid)
+{
+    g_autofree char *fddir = g_strdup_printf("/proc/%d/fd", (int)pid);
+    GDir *dir = g_dir_open(fddir, 0, NULL);
+    int count = 0;
+
+    if (!dir) {
+        return -1;
+    }
+    while (g_dir_read_name(dir)) {
+        count++;
+    }
+    g_dir_close(dir);
+    return count;
+}
+
+static void test_file_connect_outgoing_fd_leak(char *name, MigrateCommon *args)
+{
+    QTestState *from, *to;
+    int fd_before, fd_after;
+    const int retries = 5;
+    int i;
+    if (!g_file_test("/dev/full", G_FILE_TEST_EXISTS)) {
+        g_test_skip("/dev/full not available");
+        return;
+    }
+
+    args->listen_uri = "defer";
+    if (migrate_start(&from, &to, args->listen_uri, &args->start)) {
+        return;
+    }
+
+    fd_before = count_proc_fds(qtest_pid(from));
+    g_assert_cmpint(fd_before, >, 0);
+    for (i = 0; i < retries; i++) {
+        migrate_qmp_fail(from, "file:/dev/full", NULL, "{}");
+        migration_event_wait(from, "failed");
+    }
+
+    fd_after = count_proc_fds(qtest_pid(from));
+    g_assert_cmpint(fd_after, >, 0);
+    g_assert_cmpint(fd_after, ==, fd_before);
+    migrate_end(from, to, false);
+}
+
 static void test_precopy_file(char *name, MigrateCommon *args)
 {
     g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs,
@@ -314,6 +359,9 @@ void migration_test_add_file(MigrationTestEnv *env)
     migration_test_add("/migration/precopy/file/offset/bad",
                        test_precopy_file_offset_bad);
 
+    migration_test_add("/migration/precopy/file/connect-outgoing-fd-leak",
+                       test_file_connect_outgoing_fd_leak);
+
     migration_test_add("/migration/precopy/file/mapped-ram",
                        test_precopy_file_mapped_ram);
     migration_test_add("/migration/precopy/file/mapped-ram/live",
-- 
2.51.0



  parent reply	other threads:[~2026-04-23 19:22 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-23 19:19 [PULL 00/43] Migration patches for 2026-04-23 Fabiano Rosas
2026-04-23 19:19 ` [PULL 01/43] checkpatch: Allow spaces after all coroutine annotations Fabiano Rosas
2026-04-23 19:19 ` [PULL 02/43] tests/functional: Make socat wait longer in migration exec test Fabiano Rosas
2026-04-23 19:19 ` [PULL 03/43] migration: vmstate_save_state_v: fix double error_setg Fabiano Rosas
2026-04-23 19:19 ` [PULL 04/43] migration: make vmstate_save_state_v() static Fabiano Rosas
2026-04-23 19:19 ` [PULL 05/43] migration: make .post_save() a void function Fabiano Rosas
2026-04-23 19:19 ` [PULL 06/43] migration: vmstate_load_state(): add some newlines Fabiano Rosas
2026-04-23 19:19 ` [PULL 07/43] migration: vmstate_save/load_state(): refactor tracing errors Fabiano Rosas
2026-04-23 19:19 ` [PULL 08/43] migration: factor out vmstate_pre_save() from vmstate_save_state() Fabiano Rosas
2026-04-23 19:19 ` [PULL 09/43] migration: factor out vmstate_save_field() " Fabiano Rosas
2026-04-23 19:19 ` [PULL 10/43] migration: factor out vmstate_pre_load() from vmstate_load_state() Fabiano Rosas
2026-04-23 19:19 ` [PULL 11/43] migration: factor out vmstate_load_field() " Fabiano Rosas
2026-04-23 19:19 ` [PULL 12/43] migration: factor out vmstate_post_load() " Fabiano Rosas
2026-04-23 19:19 ` [PULL 13/43] migration: convert vmstate_subsection_save/load functions to bool Fabiano Rosas
2026-04-23 19:19 ` [PULL 14/43] migration: VMStateInfo: introduce new handlers with errp Fabiano Rosas
2026-04-23 19:19 ` [PULL 15/43] migration: introduce vmstate_load_vmsd() and vmstate_save_vmsd() Fabiano Rosas
2026-04-23 19:19 ` [PULL 16/43] migration/cpr: move to new migration APIs Fabiano Rosas
2026-04-23 19:19 ` [PULL 17/43] migration/savevm: " Fabiano Rosas
2026-04-23 19:19 ` [PULL 18/43] hw/s390x/css: drop use of .err_hint for vmstate Fabiano Rosas
2026-04-23 19:19 ` [PULL 19/43] migration: drop VMStateField.err_hint Fabiano Rosas
2026-04-23 19:19 ` [PULL 20/43] migration/vmstate-types: move to new migration APIs Fabiano Rosas
2026-04-23 19:19 ` [PULL 21/43] migration: Tweak description of migration property multifd-compression Fabiano Rosas
2026-04-23 19:19 ` [PULL 22/43] tests/qtest/migration: Add mapped-ram/postcopy validation test Fabiano Rosas
2026-04-23 19:19 ` Fabiano Rosas [this message]
2026-04-23 19:19 ` [PULL 24/43] vmstate: Pass in struct itself for VMSTATE_ARRAY_OF_POINTER Fabiano Rosas
2026-04-23 19:19 ` [PULL 25/43] vmstate: Pass in struct itself for VMSTATE_VARRAY_OF_POINTER_UINT32 Fabiano Rosas
2026-04-23 19:19 ` [PULL 26/43] vmstate: Do not set size for VMS_ARRAY_OF_POINTER Fabiano Rosas
2026-04-23 19:19 ` [PULL 27/43] vmstate: Update max_elems early and check field compressable once Fabiano Rosas
2026-04-23 19:19 ` [PULL 28/43] vmstate: Rename VMS_NULLPTR_MARKER to VMS_MARKER_PTR_NULL Fabiano Rosas
2026-04-23 19:19 ` [PULL 29/43] vmstate: Introduce vmstate_save_field_with_vmdesc() Fabiano Rosas
2026-04-23 19:19 ` [PULL 30/43] vmstate: Allow vmstate_info_nullptr to emit non-NULL markers Fabiano Rosas
2026-04-23 19:19 ` [PULL 31/43] vmstate: Implement load of ptr marker in vmstate core Fabiano Rosas
2026-04-23 19:19 ` [PULL 32/43] vmstate: Implement VMS_ARRAY_OF_POINTER_AUTO_ALLOC Fabiano Rosas
2026-04-23 19:19 ` [PULL 33/43] vmstate: Stop checking size for nullptr compression Fabiano Rosas
2026-04-23 19:19 ` [PULL 34/43] tests/unit/test-vmstate: add tests for VMS_ARRAY_OF_POINTER_AUTO_ALLOC Fabiano Rosas
2026-04-23 19:19 ` [PULL 35/43] migration: validate page_size in mapped-ram header before use Fabiano Rosas
2026-04-23 19:19 ` [PULL 36/43] io/channel: introduce qio_channel_pread{v, }_all{, _eof}() Fabiano Rosas
2026-04-23 19:19 ` [PULL 37/43] io/channel: introduce qio_channel_pwrite{v,}_all() Fabiano Rosas
2026-04-23 19:19 ` [PULL 38/43] migration/file: fix type mismatch and NULL deref in multifd_file_recv_data Fabiano Rosas
2026-04-23 19:19 ` [PULL 39/43] tests/unit: add pread/pwrite _all tests for io channel file Fabiano Rosas
2026-04-23 19:19 ` [PULL 40/43] tests/qtest/migration: fix fd leak in ufd_version_check Fabiano Rosas
2026-04-23 19:19 ` [PULL 41/43] migration/qemu-file: switch buffer_at functions to positioned I/O _all helpers Fabiano Rosas
2026-04-23 19:19 ` [PULL 42/43] migration/file: switch file_write_ramblock_iov to pwritev_all Fabiano Rosas
2026-04-23 19:19 ` [PULL 43/43] migration/qemu-file: drop incorrect const from qemu_get_buffer_at buf Fabiano Rosas
2026-04-25 16:58 ` [PULL 00/43] Migration patches for 2026-04-23 Stefan Hajnoczi

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=20260423191958.1440-24-farosas@suse.de \
    --to=farosas@suse.de \
    --cc=peterx@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=vikingtc4@gmail.com \
    /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.