Skip to contentSkip to conversation

migration: mapped-ram fixes

PATCH In review Fabiano Rosas 3 participants 7 messages Mar 11, 2024 – Mar 12, 2024

lore
Share & tools
lore
#0 lore raw

cover migration: mapped-ram fixes

Hi,

Here are the fixes for the dup() issues found by Coverity.

@Peter Xu, I fixed the leak you found, but I'm holding on to that patch because I noticed we're not freeing the ioc in the error paths. I'll need to add some infrastructure to be able to cancel the glib polling (qio_channel_add_watch_full) when the channel creation fails before the source has connnected.

CI run: https://gitlab.com/farosas/qemu/-/pipelines/1209529440

Fabiano Rosas (2):
io: Introduce qio_channel_file_new_dupfd
migration: Fix error handling after dup in file migration

 include/io/channel-file.h | 18 ++++++++++++++++++
 io/channel-file.c         | 12 ++++++++++++
 migration/fd.c            |  9 ++++-----
 migration/file.c          | 14 +++++++-------
 4 files changed, 41 insertions(+), 12 deletions(-)
Signature · 1 line
2.35.3
#1 lore raw

PATCH 1/2 io: Introduce qio_channel_file_new_dupfd

Add a new helper function for creating a QIOChannelFile channel with a duplicated file descriptor. This saves the calling code from having to do error checking on the dup() call.

---
 include/io/channel-file.h | 18 ++++++++++++++++++
 io/channel-file.c         | 12 ++++++++++++
 2 files changed, 30 insertions(+)
include/io/channel-file.h +18 -0
index 50e8eb1138..d373a4e44d 100644
@@ -68,6 +68,24 @@ struct QIOChannelFile {
 QIOChannelFile *
 qio_channel_file_new_fd(int fd);
 
+/**
+ * qio_channel_file_new_dupfd:
+ * @fd: the file descriptor
+ * @errp: pointer to initialized error object
+ *
+ * Create a new IO channel object for a file represented by the @fd
+ * parameter. Like qio_channel_file_new_fd(), but the @fd is first
+ * duplicated with dup().
+ *
+ * The channel will own the duplicated file descriptor and will take
+ * responsibility for closing it, the original FD is owned by the
+ * caller.
+ *
+ * Returns: the new channel object
+ */
+QIOChannelFile *
+qio_channel_file_new_dupfd(int fd, Error **errp);
+
 /**
  * qio_channel_file_new_path:
  * @path: the file path
io/channel-file.c +12 -0
index d4706fa592..cbdd03bf21 100644
@@ -45,6 +45,18 @@ qio_channel_file_new_fd(int fd)
     return ioc;
 }
 
+QIOChannelFile *
+qio_channel_file_new_dupfd(int fd, Error **errp)
+{
+    int newfd = dup(fd);
+
+    if (newfd < 0) {
+        error_setg_errno(errp, errno, "Could not dup FD %d", fd);
+        return NULL;
+    }
+
+    return qio_channel_file_new_fd(newfd);
+}
 
 QIOChannelFile *
 qio_channel_file_new_path(const char *path,
Signature · 1 line
2.35.3
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] io: Introduce qio_channel_file_new_dupfd

Quoted 10 lines · Fabiano Rosas

Add a new helper function for creating a QIOChannelFile channel with a duplicated file descriptor. This saves the calling code from having to do error checking on the dup() call.

---
 include/io/channel-file.h | 18 ++++++++++++++++++
 io/channel-file.c         | 12 ++++++++++++
 2 files changed, 30 insertions(+)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#3 lore raw

PATCH 2/2 migration: Fix error handling after dup in file migration

In reply to Fabiano Rosas

The file migration code was allowing a possible -1 from a failed call to dup() to propagate into the new QIOFileChannel::fd before checking for validity. Coverity doesn't like that, possibly due to the the lseek(-1, ...) call that would ensue before returning from the channel creation routine.

Use the newly introduced qio_channel_file_dupfd() to properly check the return of dup() before proceeding.

Fixes CID 1539961
Fixes CID 1539965
Fixes CID 1539960
Fixes 2dd7ee7a51 ("migration/multifd: Add incoming QIOChannelFile support")
Fixes decdc76772 ("migration/multifd: Add mapped-ram support to fd: URI")
---
 migration/fd.c   |  9 ++++-----
 migration/file.c | 14 +++++++-------
 2 files changed, 11 insertions(+), 12 deletions(-)
migration/fd.c +4 -5
index d4ae72d132..4e2a63a73d 100644
@@ -80,6 +80,7 @@ static gboolean fd_accept_incoming_migration(QIOChannel *ioc,
 void fd_start_incoming_migration(const char *fdname, Error **errp)
 {
     QIOChannel *ioc;
+    QIOChannelFile *fioc;
     int fd = monitor_fd_param(monitor_cur(), fdname, errp);
     if (fd == -1) {
         return;
@@ -103,15 +104,13 @@ void fd_start_incoming_migration(const char *fdname, Error **errp)
         int channels = migrate_multifd_channels();
 
         while (channels--) {
-            ioc = QIO_CHANNEL(qio_channel_file_new_fd(dup(fd)));
-
-            if (QIO_CHANNEL_FILE(ioc)->fd == -1) {
-                error_setg(errp, "Failed to duplicate fd %d", fd);
+            fioc = qio_channel_file_new_dupfd(fd, errp);
+            if (!fioc) {
                 return;
             }
 
             qio_channel_set_name(ioc, "migration-fd-incoming");
-            qio_channel_add_watch_full(ioc, G_IO_IN,
+            qio_channel_add_watch_full(QIO_CHANNEL(fioc), G_IO_IN,
                                        fd_accept_incoming_migration,
                                        NULL, NULL,
                                        g_main_context_get_thread_default());
migration/file.c +7 -7
index 164b079966..d458f48269 100644
@@ -58,12 +58,13 @@ bool file_send_channel_create(gpointer opaque, Error **errp)
     int fd = fd_args_get_fd();
 
     if (fd && fd != -1) {
-        ioc = qio_channel_file_new_fd(dup(fd));
+        ioc = qio_channel_file_new_dupfd(fd, errp);
     } else {
         ioc = qio_channel_file_new_path(outgoing_args.fname, flags, 0, errp);
-        if (!ioc) {
-            goto out;
-        }
+    }
+
+    if (!ioc) {
+        goto out;
     }
 
     multifd_channel_connect(opaque, QIO_CHANNEL(ioc));
@@ -147,10 +148,9 @@ void file_start_incoming_migration(FileMigrationArgs *file_args, Error **errp)
                                    NULL, NULL,
                                    g_main_context_get_thread_default());
 
-        fioc = qio_channel_file_new_fd(dup(fioc->fd));
+        fioc = qio_channel_file_new_dupfd(fioc->fd, errp);
 
-        if (!fioc || fioc->fd == -1) {
-            error_setg(errp, "Error creating migration incoming channel");
+        if (!fioc) {
             break;
         }
     } while (++i < channels);
Signature · 1 line
2.35.3
Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] migration: Fix error handling after dup in file migration

Quoted 42 lines · Fabiano Rosas

The file migration code was allowing a possible -1 from a failed call to dup() to propagate into the new QIOFileChannel::fd before checking for validity. Coverity doesn't like that, possibly due to the the lseek(-1, ...) call that would ensue before returning from the channel creation routine.

Use the newly introduced qio_channel_file_dupfd() to properly check the return of dup() before proceeding.

Fixes CID 1539961
Fixes CID 1539965
Fixes CID 1539960
Fixes 2dd7ee7a51 ("migration/multifd: Add incoming QIOChannelFile support")
Fixes decdc76772 ("migration/multifd: Add mapped-ram support to fd: URI")
---
 migration/fd.c   |  9 ++++-----
 migration/file.c | 14 +++++++-------
 2 files changed, 11 insertions(+), 12 deletions(-)
migration/fd.c +2 -4
index d4ae72d132..4e2a63a73d 100644
@@ -80,6 +80,7 @@ static gboolean fd_accept_incoming_migration(QIOChannel *ioc,
 void fd_start_incoming_migration(const char *fdname, Error **errp)
 {
     QIOChannel *ioc;
+    QIOChannelFile *fioc;
     int fd = monitor_fd_param(monitor_cur(), fdname, errp);
     if (fd == -1) {
         return;
@@ -103,15 +104,13 @@ void fd_start_incoming_migration(const char *fdname, Error **errp)
         int channels = migrate_multifd_channels();
 
         while (channels--) {
-            ioc = QIO_CHANNEL(qio_channel_file_new_fd(dup(fd)));
-
-            if (QIO_CHANNEL_FILE(ioc)->fd == -1) {
-                error_setg(errp, "Failed to duplicate fd %d", fd);
+            fioc = qio_channel_file_new_dupfd(fd, errp);

migration/fd.c void fd_start_incoming_migration(const char *fdname, Error **errp) · line 116

Quoted 10 lines
migration/fd.c +2 -1
+            if (!fioc) {
                 return;
             }
 
             qio_channel_set_name(ioc, "migration-fd-incoming");
-            qio_channel_add_watch_full(ioc, G_IO_IN,
+            qio_channel_add_watch_full(QIO_CHANNEL(fioc), G_IO_IN,
                                        fd_accept_incoming_migration,
                                        NULL, NULL,
                                        g_main_context_get_thread_default());

Nothing is free'ing the already created channels, if this while() loop fails on the 2nd or later iterations.

Quoted 25 lines
migration/file.c +5 -4
index 164b079966..d458f48269 100644
@@ -58,12 +58,13 @@ bool file_send_channel_create(gpointer opaque, Error **errp)
     int fd = fd_args_get_fd();
 
     if (fd && fd != -1) {
-        ioc = qio_channel_file_new_fd(dup(fd));
+        ioc = qio_channel_file_new_dupfd(fd, errp);
     } else {
         ioc = qio_channel_file_new_path(outgoing_args.fname, flags, 0, errp);
-        if (!ioc) {
-            goto out;
-        }
+    }
+
+    if (!ioc) {
+        goto out;
     }
 
     multifd_channel_connect(opaque, QIO_CHANNEL(ioc));
@@ -147,10 +148,9 @@ void file_start_incoming_migration(FileMigrationArgs *file_args, Error **errp)
                                    NULL, NULL,
                                    g_main_context_get_thread_default());

migration/file.c void file_start_incoming_migration(FileMigrationArgs *file_args, Error **errp) · line 156

Quoted 10 lines
migration/file.c +2 -3
 
-        fioc = qio_channel_file_new_fd(dup(fioc->fd));
+        fioc = qio_channel_file_new_dupfd(fioc->fd, errp);
 
-        if (!fioc || fioc->fd == -1) {
-            error_setg(errp, "Error creating migration incoming channel");
+        if (!fioc) {
             break;
         }
     } while (++i < channels);

Again, nothing is free'ing when the loops fails on 2nd or later iterations.

So a weak

on the basis that it fixes the bugs that it claims to fix, but there are more bugs that still need fixing here.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#5 lore raw

Re: [PATCH 2/2] migration: Fix error handling after dup in file migration

Quoted 86 lines · Daniel P. Berrangé
Quoted 42 lines · Fabiano Rosas

The file migration code was allowing a possible -1 from a failed call to dup() to propagate into the new QIOFileChannel::fd before checking for validity. Coverity doesn't like that, possibly due to the the lseek(-1, ...) call that would ensue before returning from the channel creation routine.

Use the newly introduced qio_channel_file_dupfd() to properly check the return of dup() before proceeding.

Fixes CID 1539961
Fixes CID 1539965
Fixes CID 1539960
Fixes 2dd7ee7a51 ("migration/multifd: Add incoming QIOChannelFile support")
Fixes decdc76772 ("migration/multifd: Add mapped-ram support to fd: URI")
---
 migration/fd.c   |  9 ++++-----
 migration/file.c | 14 +++++++-------
 2 files changed, 11 insertions(+), 12 deletions(-)
migration/fd.c +2 -4
index d4ae72d132..4e2a63a73d 100644
@@ -80,6 +80,7 @@ static gboolean fd_accept_incoming_migration(QIOChannel *ioc,
 void fd_start_incoming_migration(const char *fdname, Error **errp)
 {
     QIOChannel *ioc;
+    QIOChannelFile *fioc;
     int fd = monitor_fd_param(monitor_cur(), fdname, errp);
     if (fd == -1) {
         return;
@@ -103,15 +104,13 @@ void fd_start_incoming_migration(const char *fdname, Error **errp)
         int channels = migrate_multifd_channels();
 
         while (channels--) {
-            ioc = QIO_CHANNEL(qio_channel_file_new_fd(dup(fd)));
-
-            if (QIO_CHANNEL_FILE(ioc)->fd == -1) {
-                error_setg(errp, "Failed to duplicate fd %d", fd);
+            fioc = qio_channel_file_new_dupfd(fd, errp);

migration/fd.c void fd_start_incoming_migration(const char *fdname, Error **errp) · line 116

Quoted 10 lines
migration/fd.c +2 -1
+            if (!fioc) {
                 return;
             }
 
             qio_channel_set_name(ioc, "migration-fd-incoming");
-            qio_channel_add_watch_full(ioc, G_IO_IN,
+            qio_channel_add_watch_full(QIO_CHANNEL(fioc), G_IO_IN,
                                        fd_accept_incoming_migration,
                                        NULL, NULL,
                                        g_main_context_get_thread_default());

Nothing is free'ing the already created channels, if this while() loop fails on the 2nd or later iterations.

Quoted 29 lines
migration/file.c +6 -5
index 164b079966..d458f48269 100644
@@ -58,12 +58,13 @@ bool file_send_channel_create(gpointer opaque, Error **errp)
     int fd = fd_args_get_fd();
 
     if (fd && fd != -1) {
-        ioc = qio_channel_file_new_fd(dup(fd));
+        ioc = qio_channel_file_new_dupfd(fd, errp);
     } else {
         ioc = qio_channel_file_new_path(outgoing_args.fname, flags, 0, errp);
-        if (!ioc) {
-            goto out;
-        }
+    }
+
+    if (!ioc) {
+        goto out;
     }
 
     multifd_channel_connect(opaque, QIO_CHANNEL(ioc));
@@ -147,10 +148,9 @@ void file_start_incoming_migration(FileMigrationArgs *file_args, Error **errp)
                                    NULL, NULL,
                                    g_main_context_get_thread_default());
 
-        fioc = qio_channel_file_new_fd(dup(fioc->fd));
+        fioc = qio_channel_file_new_dupfd(fioc->fd, errp);
 
Quoted 9 lines

migration/file.c void file_start_incoming_migration(FileMigrationArgs *file_args, Error **errp) · line 156

Quoted 6 lines
migration/file.c +1 -2
-        if (!fioc || fioc->fd == -1) {
-            error_setg(errp, "Error creating migration incoming channel");
+        if (!fioc) {
             break;
         }
     } while (++i < channels);

Again, nothing is free'ing when the loops fails on 2nd or later iterations.

For this one, I think it constantly leak one IOC even if no failure triggers..

Quoted 7 lines

So a weak

on the basis that it fixes the bugs that it claims to fix, but there are more bugs that still need fixing here.

For the other issue, Fabiano - I think there's one easy way to workaround and avoid bothering with "how to remove a registered IO watch" is we create the IOCs in a loop first, register the IO watches only if all succeeded.

I'll queue the series first to fix the reported issue.

Thanks,

Signature · 1 line
Peter Xu
#6 lore raw

Re: [PATCH 2/2] migration: Fix error handling after dup in file migration

Quoted 103 lines · Peter Xu
Quoted 86 lines · Daniel P. Berrangé
Quoted 42 lines · Fabiano Rosas

The file migration code was allowing a possible -1 from a failed call to dup() to propagate into the new QIOFileChannel::fd before checking for validity. Coverity doesn't like that, possibly due to the the lseek(-1, ...) call that would ensue before returning from the channel creation routine.

Use the newly introduced qio_channel_file_dupfd() to properly check the return of dup() before proceeding.

Fixes CID 1539961
Fixes CID 1539965
Fixes CID 1539960
Fixes 2dd7ee7a51 ("migration/multifd: Add incoming QIOChannelFile support")
Fixes decdc76772 ("migration/multifd: Add mapped-ram support to fd: URI")
---
 migration/fd.c   |  9 ++++-----
 migration/file.c | 14 +++++++-------
 2 files changed, 11 insertions(+), 12 deletions(-)
migration/fd.c +2 -4
index d4ae72d132..4e2a63a73d 100644
@@ -80,6 +80,7 @@ static gboolean fd_accept_incoming_migration(QIOChannel *ioc,
 void fd_start_incoming_migration(const char *fdname, Error **errp)
 {
     QIOChannel *ioc;
+    QIOChannelFile *fioc;
     int fd = monitor_fd_param(monitor_cur(), fdname, errp);
     if (fd == -1) {
         return;
@@ -103,15 +104,13 @@ void fd_start_incoming_migration(const char *fdname, Error **errp)
         int channels = migrate_multifd_channels();
 
         while (channels--) {
-            ioc = QIO_CHANNEL(qio_channel_file_new_fd(dup(fd)));
-
-            if (QIO_CHANNEL_FILE(ioc)->fd == -1) {
-                error_setg(errp, "Failed to duplicate fd %d", fd);
+            fioc = qio_channel_file_new_dupfd(fd, errp);

migration/fd.c void fd_start_incoming_migration(const char *fdname, Error **errp) · line 116

Quoted 10 lines
migration/fd.c +2 -1
+            if (!fioc) {
                 return;
             }
 
             qio_channel_set_name(ioc, "migration-fd-incoming");
-            qio_channel_add_watch_full(ioc, G_IO_IN,
+            qio_channel_add_watch_full(QIO_CHANNEL(fioc), G_IO_IN,
                                        fd_accept_incoming_migration,
                                        NULL, NULL,
                                        g_main_context_get_thread_default());

Nothing is free'ing the already created channels, if this while() loop fails on the 2nd or later iterations.

Quoted 29 lines
migration/file.c +6 -5
index 164b079966..d458f48269 100644
@@ -58,12 +58,13 @@ bool file_send_channel_create(gpointer opaque, Error **errp)
     int fd = fd_args_get_fd();
 
     if (fd && fd != -1) {
-        ioc = qio_channel_file_new_fd(dup(fd));
+        ioc = qio_channel_file_new_dupfd(fd, errp);
     } else {
         ioc = qio_channel_file_new_path(outgoing_args.fname, flags, 0, errp);
-        if (!ioc) {
-            goto out;
-        }
+    }
+
+    if (!ioc) {
+        goto out;
     }
 
     multifd_channel_connect(opaque, QIO_CHANNEL(ioc));
@@ -147,10 +148,9 @@ void file_start_incoming_migration(FileMigrationArgs *file_args, Error **errp)
                                    NULL, NULL,
                                    g_main_context_get_thread_default());
 
-        fioc = qio_channel_file_new_fd(dup(fioc->fd));
+        fioc = qio_channel_file_new_dupfd(fioc->fd, errp);
 
Quoted 9 lines

migration/file.c void file_start_incoming_migration(FileMigrationArgs *file_args, Error **errp) · line 156

Quoted 6 lines
migration/file.c +1 -2
-        if (!fioc || fioc->fd == -1) {
-            error_setg(errp, "Error creating migration incoming channel");
+        if (!fioc) {
             break;
         }
     } while (++i < channels);

Again, nothing is free'ing when the loops fails on 2nd or later iterations.

For this one, I think it constantly leak one IOC even if no failure triggers..

Quoted 3 lines

So a weak

Quoted 8 lines
Quoted 4 lines

on the basis that it fixes the bugs that it claims to fix, but there are more bugs that still need fixing here.

For the other issue, Fabiano - I think there's one easy way to workaround and avoid bothering with "how to remove a registered IO watch" is we create the IOCs in a loop first, register the IO watches only if all succeeded.

Yes, that makes sense as an approach.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|