Skip to contentSkip to conversation

io/channel: complete pread/pwrite_all API and fix multifd_file_recv_data

PATCH In review Junjie Cao 6 participants 18 messages Apr 13 – Apr 29

lore
Share & tools
lore

Junjie Cao

author
#0 lore raw

v3 cover io/channel: complete pread/pwrite_all API and fix multifd_file_recv_data

This series fixes two bugs in multifd_file_recv_data() and completes the positioned read/write "all" API set for QIO channels.

Bug summary:
- size_t variable storing ssize_t return: -1 wraps to SIZE_MAX
- error_prepend() called without ERRP_GUARD(): NULL deref on short read

Changes since v2:
- Added qio_channel_pread_all_eof() to complete the API set (Daniel)
- Added pwrite_all / pwritev_all variants (Daniel)
- Reworked doc comments to match existing API style (Daniel)
- Added Fixes tag, expanded _eof test coverage, added CONFIG_PREADV guard

Changes since v1:
- Used approach (b): add pread_all API instead of local retry loop (Daniel, Peter)

v2: https://lore.kernel.org/qemu-devel/[email protected]/
v1: https://lore.kernel.org/qemu-devel/[email protected]/

Junjie Cao (4):
io/channel: introduce qio_channel_pread{v,}_all{,_eof}() io/channel: introduce qio_channel_pwrite{v,}_all() migration/file: fix type mismatch and NULL deref in
multifd_file_recv_data
tests/unit: add pread/pwrite _all tests for io channel file

 include/io/channel.h              | 133 +++++++++++++++++++
 io/channel.c                      | 139 ++++++++++++++++++++
 migration/file.c                  |  13 +-
 tests/unit/test-io-channel-file.c | 207 ++++++++++++++++++++++++++++++
 4 files changed, 486 insertions(+), 6 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v3 1/4 io/channel: introduce qio_channel_pread{v, }_all{, _eof}()

qio_channel_pread() and qio_channel_preadv() perform a single positioned read and may return a short result. Callers that need all bytes currently have to open-code a retry loop or simply treat a short read as an error.

Introduce four new helpers following the existing read_all / readv_all pattern:

qio_channel_preadv_all_eof() -- retry loop; returns 1 on success,
0 on clean EOF, -1 on error.
qio_channel_preadv_all() -- wraps _eof; treats early EOF as

                                    error; returns 0 / -1.

qio_channel_pread_all_eof() -- single-buffer convenience wrapper
around preadv_all_eof().
qio_channel_pread_all() -- single-buffer convenience wrapper
around preadv_all().

These advance the file offset internally after each partial read. All four are marked coroutine_mixed_fn, consistent with the existing _all helpers.

---
 include/io/channel.h | 92 ++++++++++++++++++++++++++++++++++++++++++++
 io/channel.c         | 91 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 183 insertions(+)
include/io/channel.h +92 -0
index 1b02350437..47af409ede 100644
@@ -634,6 +634,98 @@ ssize_t qio_channel_preadv(QIOChannel *ioc, const struct iovec *iov,
 ssize_t qio_channel_pread(QIOChannel *ioc, void *buf, size_t buflen,
                           off_t offset, Error **errp);
 
+/**
+ * qio_channel_preadv_all_eof:
+ * @ioc: the channel object
+ * @iov: the array of memory regions to read data into
+ * @niov: the length of the @iov array
+ * @offset: the starting offset in the channel to read from
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * Reads @iov, possibly blocking or (if the channel is non-blocking)
+ * yielding from the current coroutine multiple times until the entire
+ * content is read.  If end-of-file occurs immediately it is not an
+ * error, but if it occurs after data has been read it will return
+ * an error rather than a short-read.  Otherwise behaves as
+ * qio_channel_preadv().
+ *
+ * Returns: 1 if all bytes were read, 0 if end-of-file occurs
+ *          without data, or -1 on error
+ */
+int coroutine_mixed_fn qio_channel_preadv_all_eof(QIOChannel *ioc,
+                                                  const struct iovec *iov,
+                                                  size_t niov,
+                                                  off_t offset,
+                                                  Error **errp);
+
+/**
+ * qio_channel_preadv_all:
+ * @ioc: the channel object
+ * @iov: the array of memory regions to read data into
+ * @niov: the length of the @iov array
+ * @offset: the starting offset in the channel to read from
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * Reads @iov, possibly blocking or (if the channel is non-blocking)
+ * yielding from the current coroutine multiple times until the entire
+ * content is read.  If end-of-file occurs before all requested data
+ * has been read, an error will be reported.  Otherwise behaves as
+ * qio_channel_preadv().
+ *
+ * Returns: 0 if all bytes were read, or -1 on error
+ */
+int coroutine_mixed_fn qio_channel_preadv_all(QIOChannel *ioc,
+                                              const struct iovec *iov,
+                                              size_t niov,
+                                              off_t offset,
+                                              Error **errp);
+
+/**
+ * qio_channel_pread_all_eof:
+ * @ioc: the channel object
+ * @buf: the memory region to read data into
+ * @buflen: the number of bytes to read into @buf
+ * @offset: the starting offset in the channel to read from
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * Reads @buflen bytes, possibly blocking or (if the channel is
+ * non-blocking) yielding from the current coroutine multiple times
+ * until the entire content is read.  If end-of-file occurs
+ * immediately it is not an error, but if it occurs after data has
+ * been read it will return an error rather than a short-read.
+ * Otherwise behaves as qio_channel_pread().
+ *
+ * Returns: 1 if all bytes were read, 0 if end-of-file occurs
+ *          without data, or -1 on error
+ */
+int coroutine_mixed_fn qio_channel_pread_all_eof(QIOChannel *ioc,
+                                                 void *buf,
+                                                 size_t buflen,
+                                                 off_t offset,
+                                                 Error **errp);
+
+/**
+ * qio_channel_pread_all:
+ * @ioc: the channel object
+ * @buf: the memory region to read data into
+ * @buflen: the number of bytes to read into @buf
+ * @offset: the starting offset in the channel to read from
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * Reads @buflen bytes, possibly blocking or (if the channel is
+ * non-blocking) yielding from the current coroutine multiple times
+ * until the entire content is read.  If end-of-file occurs before
+ * all requested data has been read, an error will be reported.
+ * Otherwise behaves as qio_channel_pread().
+ *
+ * Returns: 0 if all bytes were read, or -1 on error
+ */
+int coroutine_mixed_fn qio_channel_pread_all(QIOChannel *ioc,
+                                             void *buf,
+                                             size_t buflen,
+                                             off_t offset,
+                                             Error **errp);
+
 /**
  * qio_channel_shutdown:
  * @ioc: the channel object
io/channel.c +91 -0

97 lines not on this page · raw message

Signature · 1 line
2.43.0
Reviewed-by on v3 1/4
#2 lore raw

Re: [PATCH v3 1/4] io/channel: introduce qio_channel_pread{v, }_all{, _eof}()

Quoted 223 lines · Junjie Cao

qio_channel_pread() and qio_channel_preadv() perform a single positioned read and may return a short result. Callers that need all bytes currently have to open-code a retry loop or simply treat a short read as an error.

Introduce four new helpers following the existing read_all / readv_all pattern:

qio_channel_preadv_all_eof() -- retry loop; returns 1 on success,
0 on clean EOF, -1 on error.
qio_channel_preadv_all() -- wraps _eof; treats early EOF as

                                    error; returns 0 / -1.

qio_channel_pread_all_eof() -- single-buffer convenience wrapper
around preadv_all_eof().
qio_channel_pread_all() -- single-buffer convenience wrapper
around preadv_all().

These advance the file offset internally after each partial read. All four are marked coroutine_mixed_fn, consistent with the existing _all helpers.

---
 include/io/channel.h | 92 ++++++++++++++++++++++++++++++++++++++++++++
 io/channel.c         | 91 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 183 insertions(+)
include/io/channel.h +92 -0
index 1b02350437..47af409ede 100644
@@ -634,6 +634,98 @@ ssize_t qio_channel_preadv(QIOChannel *ioc, const struct iovec *iov,
 ssize_t qio_channel_pread(QIOChannel *ioc, void *buf, size_t buflen,
                           off_t offset, Error **errp);
 
+/**
+ * qio_channel_preadv_all_eof:
+ * @ioc: the channel object
+ * @iov: the array of memory regions to read data into
+ * @niov: the length of the @iov array
+ * @offset: the starting offset in the channel to read from
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * Reads @iov, possibly blocking or (if the channel is non-blocking)
+ * yielding from the current coroutine multiple times until the entire
+ * content is read.  If end-of-file occurs immediately it is not an
+ * error, but if it occurs after data has been read it will return
+ * an error rather than a short-read.  Otherwise behaves as
+ * qio_channel_preadv().
+ *
+ * Returns: 1 if all bytes were read, 0 if end-of-file occurs
+ *          without data, or -1 on error
+ */
+int coroutine_mixed_fn qio_channel_preadv_all_eof(QIOChannel *ioc,
+                                                  const struct iovec *iov,
+                                                  size_t niov,
+                                                  off_t offset,
+                                                  Error **errp);
+
+/**
+ * qio_channel_preadv_all:
+ * @ioc: the channel object
+ * @iov: the array of memory regions to read data into
+ * @niov: the length of the @iov array
+ * @offset: the starting offset in the channel to read from
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * Reads @iov, possibly blocking or (if the channel is non-blocking)
+ * yielding from the current coroutine multiple times until the entire
+ * content is read.  If end-of-file occurs before all requested data
+ * has been read, an error will be reported.  Otherwise behaves as
+ * qio_channel_preadv().
+ *
+ * Returns: 0 if all bytes were read, or -1 on error
+ */
+int coroutine_mixed_fn qio_channel_preadv_all(QIOChannel *ioc,
+                                              const struct iovec *iov,
+                                              size_t niov,
+                                              off_t offset,
+                                              Error **errp);
+
+/**
+ * qio_channel_pread_all_eof:
+ * @ioc: the channel object
+ * @buf: the memory region to read data into
+ * @buflen: the number of bytes to read into @buf
+ * @offset: the starting offset in the channel to read from
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * Reads @buflen bytes, possibly blocking or (if the channel is
+ * non-blocking) yielding from the current coroutine multiple times
+ * until the entire content is read.  If end-of-file occurs
+ * immediately it is not an error, but if it occurs after data has
+ * been read it will return an error rather than a short-read.
+ * Otherwise behaves as qio_channel_pread().
+ *
+ * Returns: 1 if all bytes were read, 0 if end-of-file occurs
+ *          without data, or -1 on error
+ */
+int coroutine_mixed_fn qio_channel_pread_all_eof(QIOChannel *ioc,
+                                                 void *buf,
+                                                 size_t buflen,
+                                                 off_t offset,
+                                                 Error **errp);
+
+/**
+ * qio_channel_pread_all:
+ * @ioc: the channel object
+ * @buf: the memory region to read data into
+ * @buflen: the number of bytes to read into @buf
+ * @offset: the starting offset in the channel to read from
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * Reads @buflen bytes, possibly blocking or (if the channel is
+ * non-blocking) yielding from the current coroutine multiple times
+ * until the entire content is read.  If end-of-file occurs before
+ * all requested data has been read, an error will be reported.
+ * Otherwise behaves as qio_channel_pread().
+ *
+ * Returns: 0 if all bytes were read, or -1 on error
+ */
+int coroutine_mixed_fn qio_channel_pread_all(QIOChannel *ioc,
+                                             void *buf,
+                                             size_t buflen,
+                                             off_t offset,
+                                             Error **errp);
+
 /**
  * qio_channel_shutdown:
  * @ioc: the channel object
io/channel.c +84 -0
index cc02d997a4..52c1abfcbc 100644
@@ -507,6 +507,97 @@ ssize_t qio_channel_pread(QIOChannel *ioc, void *buf, size_t buflen,
     return qio_channel_preadv(ioc, &iov, 1, offset, errp);
 }
 
+int coroutine_mixed_fn qio_channel_preadv_all_eof(QIOChannel *ioc,
+                                                  const struct iovec *iov,
+                                                  size_t niov,
+                                                  off_t offset,
+                                                  Error **errp)
+{
+    int ret = -1;
+    struct iovec *local_iov = g_new(struct iovec, niov);
+    struct iovec *local_iov_head = local_iov;
+    unsigned int nlocal_iov = niov;
+    bool partial = false;
+
+    nlocal_iov = iov_copy(local_iov, nlocal_iov,
+                          iov, niov,
+                          0, iov_size(iov, niov));
+
+    while (nlocal_iov > 0) {
+        ssize_t len;
+        len = qio_channel_preadv(ioc, local_iov, nlocal_iov, offset, errp);
+
+        if (len == QIO_CHANNEL_ERR_BLOCK) {
+            qio_channel_wait_cond(ioc, G_IO_IN);
+            continue;
+        }
+
+        if (len == 0) {
+            if (!partial) {
+                ret = 0;
+                goto cleanup;
+            }
+            error_setg(errp,
+                       "Unexpected end-of-file before all data were read");
+            goto cleanup;
+        }
+
+        if (len < 0) {
+            goto cleanup;
+        }
+
+        partial = true;
+        offset += len;
+        iov_discard_front(&local_iov, &nlocal_iov, len);
+    }
+
+    ret = 1;
+
+ cleanup:
+    g_free(local_iov_head);
+    return ret;
+}
+
+int coroutine_mixed_fn qio_channel_preadv_all(QIOChannel *ioc,
+                                              const struct iovec *iov,
+                                              size_t niov,
+                                              off_t offset,
+                                              Error **errp)
+{
+    int ret = qio_channel_preadv_all_eof(ioc, iov, niov, offset, errp);
+
+    if (ret == 0) {
+        error_setg(errp,
+                   "Unexpected end-of-file before all data were read");
+        return -1;
+    }
+    if (ret == 1) {
+        return 0;
+    }
+
+    return ret;
+}
+
+int coroutine_mixed_fn qio_channel_pread_all_eof(QIOChannel *ioc,
+                                                 void *buf,
+                                                 size_t buflen,
+                                                 off_t offset,
+                                                 Error **errp)
+{
+    struct iovec iov = { .iov_base = buf, .iov_len = buflen };
+    return qio_channel_preadv_all_eof(ioc, &iov, 1, offset, errp);
+}
+
+int coroutine_mixed_fn qio_channel_pread_all(QIOChannel *ioc,
+                                             void *buf,
+                                             size_t buflen,

io/channel.c ssize_t qio_channel_pread(QIOChannel *ioc, void *buf, size_t buflen, · line 603

Quoted 10 lines
io/channel.c +7 -0
+                                             off_t offset,
+                                             Error **errp)
+{
+    struct iovec iov = { .iov_base = buf, .iov_len = buflen };
+    return qio_channel_preadv_all(ioc, &iov, 1, offset, errp);
+}
+
 int qio_channel_shutdown(QIOChannel *ioc,
                          QIOChannelShutdown how,
                          Error **errp)
Reviewed-by Acked-by on v3 1/4
#3 lore raw

Re: [PATCH v3 1/4] io/channel: introduce qio_channel_pread{v,}_all{,_eof}()

In reply to Junjie Cao
Quoted 21 lines · Junjie Cao

qio_channel_pread() and qio_channel_preadv() perform a single positioned read and may return a short result. Callers that need all bytes currently have to open-code a retry loop or simply treat a short read as an error.

Introduce four new helpers following the existing read_all / readv_all pattern:

qio_channel_preadv_all_eof() -- retry loop; returns 1 on success,
0 on clean EOF, -1 on error.
qio_channel_preadv_all() -- wraps _eof; treats early EOF as

                                    error; returns 0 / -1.

qio_channel_pread_all_eof() -- single-buffer convenience wrapper
around preadv_all_eof().
qio_channel_pread_all() -- single-buffer convenience wrapper
around preadv_all().

These advance the file offset internally after each partial read. All four are marked coroutine_mixed_fn, consistent with the existing _all helpers.

Quoted 6 lines
---
 include/io/channel.h | 92 ++++++++++++++++++++++++++++++++++++++++++++
 io/channel.c         | 91 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 183 insertions(+)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
author
#4 lore raw

PATCH v3 2/4 io/channel: introduce qio_channel_pwrite{v,}_all()

In reply to Junjie Cao

Add positioned write helpers that retry on short writes, matching the pread_all family from the previous patch.

qio_channel_pwritev_all() -- retry loop; returns 0 on success,
-1 on error.
qio_channel_pwrite_all() -- single-buffer convenience wrapper.

---
 include/io/channel.h | 41 +++++++++++++++++++++++++++++++++++++
 io/channel.c         | 48 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 89 insertions(+)
include/io/channel.h +41 -0
index 47af409ede..287d10cd6f 100644
@@ -598,6 +598,47 @@ ssize_t qio_channel_pwritev(QIOChannel *ioc, const struct iovec *iov,
 ssize_t qio_channel_pwrite(QIOChannel *ioc, void *buf, size_t buflen,
                            off_t offset, Error **errp);
 
+/**
+ * qio_channel_pwritev_all:
+ * @ioc: the channel object
+ * @iov: the array of memory regions to write data from
+ * @niov: the length of the @iov array
+ * @offset: the starting offset in the channel to write to
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * Writes @iov, possibly blocking or (if the channel is non-blocking)
+ * yielding from the current coroutine multiple times until the entire
+ * content is written.  Otherwise behaves as qio_channel_pwritev().
+ *
+ * Returns: 0 if all bytes were written, or -1 on error
+ */
+int coroutine_mixed_fn qio_channel_pwritev_all(QIOChannel *ioc,
+                                               const struct iovec *iov,
+                                               size_t niov,
+                                               off_t offset,
+                                               Error **errp);
+
+/**
+ * qio_channel_pwrite_all:
+ * @ioc: the channel object
+ * @buf: the memory region to write data from
+ * @buflen: the number of bytes to write from @buf
+ * @offset: the starting offset in the channel to write to
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * Writes @buflen bytes from @buf, possibly blocking or (if the
+ * channel is non-blocking) yielding from the current coroutine
+ * multiple times until the entire content is written.  Otherwise
+ * behaves as qio_channel_pwrite().
+ *
+ * Returns: 0 if all bytes were written, or -1 on error
+ */
+int coroutine_mixed_fn qio_channel_pwrite_all(QIOChannel *ioc,
+                                              const void *buf,
+                                              size_t buflen,
+                                              off_t offset,
+                                              Error **errp);
+
 /**
  * qio_channel_preadv
  * @ioc: the channel object
io/channel.c +48 -0

54 lines not on this page · raw message

Signature · 1 line
2.43.0
Reviewed-by on v3 2/4
#5 lore raw

Re: [PATCH v3 2/4] io/channel: introduce qio_channel_pwrite{v, }_all()

Quoted 114 lines · Junjie Cao

Add positioned write helpers that retry on short writes, matching the pread_all family from the previous patch.

qio_channel_pwritev_all() -- retry loop; returns 0 on success,
-1 on error.
qio_channel_pwrite_all() -- single-buffer convenience wrapper.

---
 include/io/channel.h | 41 +++++++++++++++++++++++++++++++++++++
 io/channel.c         | 48 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 89 insertions(+)
include/io/channel.h +41 -0
index 47af409ede..287d10cd6f 100644
@@ -598,6 +598,47 @@ ssize_t qio_channel_pwritev(QIOChannel *ioc, const struct iovec *iov,
 ssize_t qio_channel_pwrite(QIOChannel *ioc, void *buf, size_t buflen,
                            off_t offset, Error **errp);
 
+/**
+ * qio_channel_pwritev_all:
+ * @ioc: the channel object
+ * @iov: the array of memory regions to write data from
+ * @niov: the length of the @iov array
+ * @offset: the starting offset in the channel to write to
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * Writes @iov, possibly blocking or (if the channel is non-blocking)
+ * yielding from the current coroutine multiple times until the entire
+ * content is written.  Otherwise behaves as qio_channel_pwritev().
+ *
+ * Returns: 0 if all bytes were written, or -1 on error
+ */
+int coroutine_mixed_fn qio_channel_pwritev_all(QIOChannel *ioc,
+                                               const struct iovec *iov,
+                                               size_t niov,
+                                               off_t offset,
+                                               Error **errp);
+
+/**
+ * qio_channel_pwrite_all:
+ * @ioc: the channel object
+ * @buf: the memory region to write data from
+ * @buflen: the number of bytes to write from @buf
+ * @offset: the starting offset in the channel to write to
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * Writes @buflen bytes from @buf, possibly blocking or (if the
+ * channel is non-blocking) yielding from the current coroutine
+ * multiple times until the entire content is written.  Otherwise
+ * behaves as qio_channel_pwrite().
+ *
+ * Returns: 0 if all bytes were written, or -1 on error
+ */
+int coroutine_mixed_fn qio_channel_pwrite_all(QIOChannel *ioc,
+                                              const void *buf,
+                                              size_t buflen,
+                                              off_t offset,
+                                              Error **errp);
+
 /**
  * qio_channel_preadv
  * @ioc: the channel object
io/channel.c +41 -0
index 52c1abfcbc..2853dadb68 100644
@@ -478,6 +478,54 @@ ssize_t qio_channel_pwrite(QIOChannel *ioc, void *buf, size_t buflen,
     return qio_channel_pwritev(ioc, &iov, 1, offset, errp);
 }
 
+int coroutine_mixed_fn qio_channel_pwritev_all(QIOChannel *ioc,
+                                               const struct iovec *iov,
+                                               size_t niov,
+                                               off_t offset,
+                                               Error **errp)
+{
+    int ret = -1;
+    struct iovec *local_iov = g_new(struct iovec, niov);
+    struct iovec *local_iov_head = local_iov;
+    unsigned int nlocal_iov = niov;
+
+    nlocal_iov = iov_copy(local_iov, nlocal_iov,
+                          iov, niov,
+                          0, iov_size(iov, niov));
+
+    while (nlocal_iov > 0) {
+        ssize_t len;
+
+        len = qio_channel_pwritev(ioc, local_iov, nlocal_iov, offset, errp);
+
+        if (len == QIO_CHANNEL_ERR_BLOCK) {
+            qio_channel_wait_cond(ioc, G_IO_OUT);
+            continue;
+        }
+        if (len < 0) {
+            goto cleanup;
+        }
+
+        offset += len;
+        iov_discard_front(&local_iov, &nlocal_iov, len);
+    }
+
+    ret = 0;
+ cleanup:
+    g_free(local_iov_head);
+    return ret;
+}
+
+int coroutine_mixed_fn qio_channel_pwrite_all(QIOChannel *ioc,
+                                              const void *buf,
+                                              size_t buflen,

io/channel.c ssize_t qio_channel_pwrite(QIOChannel *ioc, void *buf, size_t buflen, · line 531

Quoted 10 lines
io/channel.c +7 -0
+                                              off_t offset,
+                                              Error **errp)
+{
+    struct iovec iov = { .iov_base = (char *)buf, .iov_len = buflen };
+    return qio_channel_pwritev_all(ioc, &iov, 1, offset, errp);
+}
+
 ssize_t qio_channel_preadv(QIOChannel *ioc, const struct iovec *iov,
                            size_t niov, off_t offset, Error **errp)
 {
Reviewed-by Daniel P. Berrangé Acked-by Daniel P. Berrangé on v3 2/4
#6 lore raw

Re: [PATCH v3 2/4] io/channel: introduce qio_channel_pwrite{v, }_all()

In reply to Junjie Cao
Quoted 7 lines · Junjie Cao

Add positioned write helpers that retry on short writes, matching the pread_all family from the previous patch.

qio_channel_pwritev_all() -- retry loop; returns 0 on success,
-1 on error.
qio_channel_pwrite_all() -- single-buffer convenience wrapper.

Quoted 5 lines
---
 include/io/channel.h | 41 +++++++++++++++++++++++++++++++++++++
 io/channel.c         | 48 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 89 insertions(+)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
author
#7 lore raw

PATCH v3 3/4 migration/file: fix type mismatch and NULL deref in multifd_file_recv_data

In reply to Junjie Cao

multifd_file_recv_data() stores the return value of qio_channel_pread() (ssize_t) in a size_t variable. On I/O error the -1 return value wraps to SIZE_MAX, producing a nonsensical read size in the error message.

More critically, a short read (0 <= ret < data->size) is possible when the migration file is truncated. In that case qio_channel_pread() returns a non-negative value without setting *errp. The function then calls error_prepend(errp, ...) which dereferences *errp -- a NULL pointer -- crashing QEMU.

Fix both issues by switching to qio_channel_pread_all() introduced in a previous patch, which retries on short reads and treats end-of-file as an error, so the caller no longer needs to check the byte count manually. Add ERRP_GUARD() so that error_prepend() works correctly even when errp is &error_fatal or NULL.

Fixes a49d15a38d3d ("migration/multifd: Support incoming mapped-ram stream format")
---
 migration/file.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
migration/file.c +7 -6
index 5618aced49..0853188601 100644
@@ -254,15 +254,16 @@ int file_write_ramblock_iov(QIOChannel *ioc, const struct iovec *iov,
 
 int multifd_file_recv_data(MultiFDRecvParams *p, Error **errp)
 {
+    ERRP_GUARD();
     MultiFDRecvData *data = p->data;
-    size_t ret;
+    int ret;
 
-    ret = qio_channel_pread(p->c, (char *) data->opaque,
-                            data->size, data->file_offset, errp);
-    if (ret != data->size) {
+    ret = qio_channel_pread_all(p->c, (char *) data->opaque,
+                                data->size, data->file_offset, errp);
+    if (ret != 0) {
         error_prepend(errp,
-                      "multifd recv (%u): read 0x%zx, expected 0x%zx",
-                      p->id, ret, data->size);
+                      "multifd recv (%u): ",
+                      p->id);
         return -1;
     }
 
Signature · 1 line
2.43.0
Reviewed-by on v3 3/4
#8 lore raw

Re: [PATCH v3 3/4] migration/file: fix type mismatch and NULL deref in multifd_file_recv_data

Quoted 40 lines · Junjie Cao

multifd_file_recv_data() stores the return value of qio_channel_pread() (ssize_t) in a size_t variable. On I/O error the -1 return value wraps to SIZE_MAX, producing a nonsensical read size in the error message.

More critically, a short read (0 <= ret < data->size) is possible when the migration file is truncated. In that case qio_channel_pread() returns a non-negative value without setting *errp. The function then calls error_prepend(errp, ...) which dereferences *errp -- a NULL pointer -- crashing QEMU.

Fix both issues by switching to qio_channel_pread_all() introduced in a previous patch, which retries on short reads and treats end-of-file as an error, so the caller no longer needs to check the byte count manually. Add ERRP_GUARD() so that error_prepend() works correctly even when errp is &error_fatal or NULL.

Fixes a49d15a38d3d ("migration/multifd: Support incoming mapped-ram stream format")
---
 migration/file.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
migration/file.c +2 -4
index 5618aced49..0853188601 100644
@@ -254,15 +254,16 @@ int file_write_ramblock_iov(QIOChannel *ioc, const struct iovec *iov,
 
 int multifd_file_recv_data(MultiFDRecvParams *p, Error **errp)
 {
+    ERRP_GUARD();
     MultiFDRecvData *data = p->data;
-    size_t ret;
+    int ret;
 
-    ret = qio_channel_pread(p->c, (char *) data->opaque,
-                            data->size, data->file_offset, errp);
-    if (ret != data->size) {

migration/file.c int file_write_ramblock_iov(QIOChannel *ioc, const struct iovec *iov, · line 268

Quoted 10 lines
migration/file.c +5 -2
+    ret = qio_channel_pread_all(p->c, (char *) data->opaque,
+                                data->size, data->file_offset, errp);
+    if (ret != 0) {
         error_prepend(errp,
-                      "multifd recv (%u): read 0x%zx, expected 0x%zx",
-                      p->id, ret, data->size);
+                      "multifd recv (%u): ",
+                      p->id);
         return -1;
     }
author
#9 lore raw

PATCH v3 4/4 tests/unit: add pread/pwrite _all tests for io channel file

In reply to Junjie Cao

Add unit tests for the new qio_channel_pread{v,}_all{,_eof}() and qio_channel_pwrite{v,}_all() APIs.

The basic tests write data to a file channel, then read it back at various offsets using both the single-buffer and iovec variants to make sure the round-trip produces identical content. The _eof tests verify all three return cases -- full read (1), clean EOF (0), and partial-then-EOF (-1 with error set) -- and check that the strict wrappers (preadv_all / pread_all) treat a clean EOF as an error.

All tests are guarded by CONFIG_PREADV since the underlying channel methods require preadv(2).

---
 tests/unit/test-io-channel-file.c | 207 ++++++++++++++++++++++++++++++
 1 file changed, 207 insertions(+)
tests/unit/test-io-channel-file.c +207 -0
index 1977006ce9..b597350dca 100644
@@ -102,6 +102,203 @@ static void test_io_channel_fd(void)
 }
 
 
+#ifdef CONFIG_PREADV
+static void test_io_channel_pread_all(void)
+{
+    QIOChannel *ioc;
+    char write_buf[] = "Hello World, pread_all";
+    char read_buf[sizeof(write_buf)] = {0};
+    int ret;
+
+    unlink(TEST_FILE);
+    ioc = QIO_CHANNEL(qio_channel_file_new_path(
+                          TEST_FILE,
+                          O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
+                          TEST_MASK,
+                          &error_abort));
+
+    ret = qio_channel_pwrite_all(ioc, write_buf, sizeof(write_buf),
+                                 0, &error_abort);
+    g_assert_cmpint(ret, ==, 0);
+
+    /* Read back at offset 0 */
+    ret = qio_channel_pread_all(ioc, read_buf, sizeof(read_buf),
+                                0, &error_abort);
+    g_assert_cmpint(ret, ==, 0);
+    g_assert_cmpmem(write_buf, sizeof(write_buf),
+                    read_buf, sizeof(read_buf));
+
+    /* Read at a non-zero offset */
+    memset(read_buf, 0, sizeof(read_buf));
+    ret = qio_channel_pread_all(ioc, read_buf, sizeof(write_buf) - 7,
+                                7, &error_abort);
+    g_assert_cmpint(ret, ==, 0);
+    g_assert_cmpmem(write_buf + 7, sizeof(write_buf) - 7,
+                    read_buf, sizeof(write_buf) - 7);
+
+    unlink(TEST_FILE);
+    object_unref(OBJECT(ioc));
+}
+
+static void test_io_channel_preadv_all(void)
+{
+    QIOChannel *ioc;
+    char write_buf[256];
+    char read_buf[256] = {0};
+    struct iovec write_iov[2];
+    struct iovec read_iov[2];
+    int ret;
+    size_t i;
+
+    for (i = 0; i < sizeof(write_buf); i++) {
+        write_buf[i] = i & 0xff;
+    }
+
+    unlink(TEST_FILE);
+    ioc = QIO_CHANNEL(qio_channel_file_new_path(
+                          TEST_FILE,
+                          O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
+                          TEST_MASK,
+                          &error_abort));
+
+    /* Write using pwritev_all with 2 iovecs */
+    write_iov[0].iov_base = write_buf;
+    write_iov[0].iov_len = 128;
+    write_iov[1].iov_base = write_buf + 128;
+    write_iov[1].iov_len = 128;
+    ret = qio_channel_pwritev_all(ioc, write_iov, 2, 0, &error_abort);
+    g_assert_cmpint(ret, ==, 0);
+
+    /* Read back using preadv_all with 2 iovecs */
+    read_iov[0].iov_base = read_buf;
+    read_iov[0].iov_len = 128;
+    read_iov[1].iov_base = read_buf + 128;
+    read_iov[1].iov_len = 128;
+    ret = qio_channel_preadv_all(ioc, read_iov, 2, 0, &error_abort);
+    g_assert_cmpint(ret, ==, 0);
+
+    g_assert_cmpmem(write_buf, sizeof(write_buf),
+                    read_buf, sizeof(read_buf));
+
+    /* Read at non-zero offset with preadv_all */
+    memset(read_buf, 0, sizeof(read_buf));
+    read_iov[0].iov_base = read_buf;
+    read_iov[0].iov_len = 64;
+    read_iov[1].iov_base = read_buf + 64;
+    read_iov[1].iov_len = 64;
+    ret = qio_channel_preadv_all(ioc, read_iov, 2, 128, &error_abort);
+    g_assert_cmpint(ret, ==, 0);
+
+    g_assert_cmpmem(write_buf + 128, 128,
+                    read_buf, 128);
+
+    unlink(TEST_FILE);
+    object_unref(OBJECT(ioc));
+}
+
+static void test_io_channel_preadv_all_eof(void)
+{
+    QIOChannel *ioc;
+    char write_buf[] = "Hello World, preadv_all_eof";
+    char read_buf[sizeof(write_buf)] = {0};
+    struct iovec iov;
+    int ret;
+    Error *err = NULL;
+
+    unlink(TEST_FILE);
+    ioc = QIO_CHANNEL(qio_channel_file_new_path(
+                          TEST_FILE,
+                          O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
+                          TEST_MASK,
+                          &error_abort));
+
+    ret = qio_channel_pwrite_all(ioc, write_buf, sizeof(write_buf),
+                                 0, &error_abort);
+    g_assert_cmpint(ret, ==, 0);
+
+    /* Full read succeeds: should return 1 */
+    iov.iov_base = read_buf;
+    iov.iov_len = sizeof(read_buf);
+    ret = qio_channel_preadv_all_eof(ioc, &iov, 1, 0, &error_abort);
+    g_assert_cmpint(ret, ==, 1);
+    g_assert_cmpmem(write_buf, sizeof(write_buf),
+                    read_buf, sizeof(read_buf));
+
+    /* Clean EOF: offset at file end, should return 0 */
+    iov.iov_base = read_buf;
+    iov.iov_len = 1;
+    ret = qio_channel_preadv_all_eof(ioc, &iov, 1,
+                                     sizeof(write_buf), &err);
+    g_assert_cmpint(ret, ==, 0);
+    g_assert_null(err);
+
+    /* Partial EOF: start before end, request extends past */
+    iov.iov_base = read_buf;
+    iov.iov_len = 8;
+    ret = qio_channel_preadv_all_eof(ioc, &iov, 1,
+                                     sizeof(write_buf) - 4, &err);
+    g_assert_cmpint(ret, ==, -1);
+    g_assert_nonnull(err);
+    error_free(err);
+    err = NULL;
+
+    /* Strict wrapper (preadv_all) treats clean EOF as error */
+    iov.iov_base = read_buf;
+    iov.iov_len = 1;
+    ret = qio_channel_preadv_all(ioc, &iov, 1,
+                                 sizeof(write_buf), &err);
+    g_assert_cmpint(ret, ==, -1);
+    g_assert_nonnull(err);
+    error_free(err);
+
+    unlink(TEST_FILE);
+    object_unref(OBJECT(ioc));
+}
+
+static void test_io_channel_pread_all_eof(void)
+{
+    QIOChannel *ioc;
+    char write_buf[] = "Hello World, pread_all_eof";
+    char read_buf[sizeof(write_buf)] = {0};
+    int ret;
+    Error *err = NULL;
+
+    unlink(TEST_FILE);
+    ioc = QIO_CHANNEL(qio_channel_file_new_path(
+                          TEST_FILE,
+                          O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
+                          TEST_MASK,
+                          &error_abort));
+
+    ret = qio_channel_pwrite_all(ioc, write_buf, sizeof(write_buf),
+                                 0, &error_abort);
+    g_assert_cmpint(ret, ==, 0);
+
+    /* Full read succeeds: should return 1 */
+    ret = qio_channel_pread_all_eof(ioc, read_buf, sizeof(read_buf),
+                                    0, &error_abort);
+    g_assert_cmpint(ret, ==, 1);
+    g_assert_cmpmem(write_buf, sizeof(write_buf),
+                    read_buf, sizeof(read_buf));
+
+    /* Clean EOF: should return 0 */
+    ret = qio_channel_pread_all_eof(ioc, read_buf, 1,
+                                    sizeof(write_buf), &err);
+    g_assert_cmpint(ret, ==, 0);
+    g_assert_null(err);
+
+    /* Partial EOF: should return -1 */
+    ret = qio_channel_pread_all_eof(ioc, read_buf, 8,
+                                    sizeof(write_buf) - 4, &err);
+    g_assert_cmpint(ret, ==, -1);
+    g_assert_nonnull(err);
+    error_free(err);
+
+    unlink(TEST_FILE);
+    object_unref(OBJECT(ioc));
+}
+#endif /* CONFIG_PREADV */
+
 #ifndef _WIN32
 static void test_io_channel_pipe(bool async)
 {
@@ -147,6 +344,16 @@ int main(int argc, char **argv)
     g_test_add_func("/io/channel/file", test_io_channel_file);
     g_test_add_func("/io/channel/file/rdwr", test_io_channel_file_rdwr);
     g_test_add_func("/io/channel/file/fd", test_io_channel_fd);
+#ifdef CONFIG_PREADV
+    g_test_add_func("/io/channel/file/pread-all",
+                    test_io_channel_pread_all);
+    g_test_add_func("/io/channel/file/preadv-all",
+                    test_io_channel_preadv_all);
+    g_test_add_func("/io/channel/file/preadv-all-eof",
+                    test_io_channel_preadv_all_eof);
+    g_test_add_func("/io/channel/file/pread-all-eof",
+                    test_io_channel_pread_all_eof);
+#endif
 #ifndef _WIN32
     g_test_add_func("/io/channel/pipe/sync", test_io_channel_pipe_sync);
     g_test_add_func("/io/channel/pipe/async", test_io_channel_pipe_async);
Signature · 1 line
2.43.0
Reviewed-by on v3 4/4
#10 lore raw

Re: [PATCH v3 4/4] tests/unit: add pread/pwrite _all tests for io channel file

Quoted 233 lines · Junjie Cao

Add unit tests for the new qio_channel_pread{v,}_all{,_eof}() and qio_channel_pwrite{v,}_all() APIs.

The basic tests write data to a file channel, then read it back at various offsets using both the single-buffer and iovec variants to make sure the round-trip produces identical content. The _eof tests verify all three return cases -- full read (1), clean EOF (0), and partial-then-EOF (-1 with error set) -- and check that the strict wrappers (preadv_all / pread_all) treat a clean EOF as an error.

All tests are guarded by CONFIG_PREADV since the underlying channel methods require preadv(2).

---
 tests/unit/test-io-channel-file.c | 207 ++++++++++++++++++++++++++++++
 1 file changed, 207 insertions(+)
tests/unit/test-io-channel-file.c +200 -0
index 1977006ce9..b597350dca 100644
@@ -102,6 +102,203 @@ static void test_io_channel_fd(void)
 }
 
 
+#ifdef CONFIG_PREADV
+static void test_io_channel_pread_all(void)
+{
+    QIOChannel *ioc;
+    char write_buf[] = "Hello World, pread_all";
+    char read_buf[sizeof(write_buf)] = {0};
+    int ret;
+
+    unlink(TEST_FILE);
+    ioc = QIO_CHANNEL(qio_channel_file_new_path(
+                          TEST_FILE,
+                          O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
+                          TEST_MASK,
+                          &error_abort));
+
+    ret = qio_channel_pwrite_all(ioc, write_buf, sizeof(write_buf),
+                                 0, &error_abort);
+    g_assert_cmpint(ret, ==, 0);
+
+    /* Read back at offset 0 */
+    ret = qio_channel_pread_all(ioc, read_buf, sizeof(read_buf),
+                                0, &error_abort);
+    g_assert_cmpint(ret, ==, 0);
+    g_assert_cmpmem(write_buf, sizeof(write_buf),
+                    read_buf, sizeof(read_buf));
+
+    /* Read at a non-zero offset */
+    memset(read_buf, 0, sizeof(read_buf));
+    ret = qio_channel_pread_all(ioc, read_buf, sizeof(write_buf) - 7,
+                                7, &error_abort);
+    g_assert_cmpint(ret, ==, 0);
+    g_assert_cmpmem(write_buf + 7, sizeof(write_buf) - 7,
+                    read_buf, sizeof(write_buf) - 7);
+
+    unlink(TEST_FILE);
+    object_unref(OBJECT(ioc));
+}
+
+static void test_io_channel_preadv_all(void)
+{
+    QIOChannel *ioc;
+    char write_buf[256];
+    char read_buf[256] = {0};
+    struct iovec write_iov[2];
+    struct iovec read_iov[2];
+    int ret;
+    size_t i;
+
+    for (i = 0; i < sizeof(write_buf); i++) {
+        write_buf[i] = i & 0xff;
+    }
+
+    unlink(TEST_FILE);
+    ioc = QIO_CHANNEL(qio_channel_file_new_path(
+                          TEST_FILE,
+                          O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
+                          TEST_MASK,
+                          &error_abort));
+
+    /* Write using pwritev_all with 2 iovecs */
+    write_iov[0].iov_base = write_buf;
+    write_iov[0].iov_len = 128;
+    write_iov[1].iov_base = write_buf + 128;
+    write_iov[1].iov_len = 128;
+    ret = qio_channel_pwritev_all(ioc, write_iov, 2, 0, &error_abort);
+    g_assert_cmpint(ret, ==, 0);
+
+    /* Read back using preadv_all with 2 iovecs */
+    read_iov[0].iov_base = read_buf;
+    read_iov[0].iov_len = 128;
+    read_iov[1].iov_base = read_buf + 128;
+    read_iov[1].iov_len = 128;
+    ret = qio_channel_preadv_all(ioc, read_iov, 2, 0, &error_abort);
+    g_assert_cmpint(ret, ==, 0);
+
+    g_assert_cmpmem(write_buf, sizeof(write_buf),
+                    read_buf, sizeof(read_buf));
+
+    /* Read at non-zero offset with preadv_all */
+    memset(read_buf, 0, sizeof(read_buf));
+    read_iov[0].iov_base = read_buf;
+    read_iov[0].iov_len = 64;
+    read_iov[1].iov_base = read_buf + 64;
+    read_iov[1].iov_len = 64;
+    ret = qio_channel_preadv_all(ioc, read_iov, 2, 128, &error_abort);
+    g_assert_cmpint(ret, ==, 0);
+
+    g_assert_cmpmem(write_buf + 128, 128,
+                    read_buf, 128);
+
+    unlink(TEST_FILE);
+    object_unref(OBJECT(ioc));
+}
+
+static void test_io_channel_preadv_all_eof(void)
+{
+    QIOChannel *ioc;
+    char write_buf[] = "Hello World, preadv_all_eof";
+    char read_buf[sizeof(write_buf)] = {0};
+    struct iovec iov;
+    int ret;
+    Error *err = NULL;
+
+    unlink(TEST_FILE);
+    ioc = QIO_CHANNEL(qio_channel_file_new_path(
+                          TEST_FILE,
+                          O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
+                          TEST_MASK,
+                          &error_abort));
+
+    ret = qio_channel_pwrite_all(ioc, write_buf, sizeof(write_buf),
+                                 0, &error_abort);
+    g_assert_cmpint(ret, ==, 0);
+
+    /* Full read succeeds: should return 1 */
+    iov.iov_base = read_buf;
+    iov.iov_len = sizeof(read_buf);
+    ret = qio_channel_preadv_all_eof(ioc, &iov, 1, 0, &error_abort);
+    g_assert_cmpint(ret, ==, 1);
+    g_assert_cmpmem(write_buf, sizeof(write_buf),
+                    read_buf, sizeof(read_buf));
+
+    /* Clean EOF: offset at file end, should return 0 */
+    iov.iov_base = read_buf;
+    iov.iov_len = 1;
+    ret = qio_channel_preadv_all_eof(ioc, &iov, 1,
+                                     sizeof(write_buf), &err);
+    g_assert_cmpint(ret, ==, 0);
+    g_assert_null(err);
+
+    /* Partial EOF: start before end, request extends past */
+    iov.iov_base = read_buf;
+    iov.iov_len = 8;
+    ret = qio_channel_preadv_all_eof(ioc, &iov, 1,
+                                     sizeof(write_buf) - 4, &err);
+    g_assert_cmpint(ret, ==, -1);
+    g_assert_nonnull(err);
+    error_free(err);
+    err = NULL;
+
+    /* Strict wrapper (preadv_all) treats clean EOF as error */
+    iov.iov_base = read_buf;
+    iov.iov_len = 1;
+    ret = qio_channel_preadv_all(ioc, &iov, 1,
+                                 sizeof(write_buf), &err);
+    g_assert_cmpint(ret, ==, -1);
+    g_assert_nonnull(err);
+    error_free(err);
+
+    unlink(TEST_FILE);
+    object_unref(OBJECT(ioc));
+}
+
+static void test_io_channel_pread_all_eof(void)
+{
+    QIOChannel *ioc;
+    char write_buf[] = "Hello World, pread_all_eof";
+    char read_buf[sizeof(write_buf)] = {0};
+    int ret;
+    Error *err = NULL;
+
+    unlink(TEST_FILE);
+    ioc = QIO_CHANNEL(qio_channel_file_new_path(
+                          TEST_FILE,
+                          O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
+                          TEST_MASK,
+                          &error_abort));
+
+    ret = qio_channel_pwrite_all(ioc, write_buf, sizeof(write_buf),
+                                 0, &error_abort);
+    g_assert_cmpint(ret, ==, 0);
+
+    /* Full read succeeds: should return 1 */
+    ret = qio_channel_pread_all_eof(ioc, read_buf, sizeof(read_buf),
+                                    0, &error_abort);
+    g_assert_cmpint(ret, ==, 1);
+    g_assert_cmpmem(write_buf, sizeof(write_buf),
+                    read_buf, sizeof(read_buf));
+
+    /* Clean EOF: should return 0 */
+    ret = qio_channel_pread_all_eof(ioc, read_buf, 1,
+                                    sizeof(write_buf), &err);
+    g_assert_cmpint(ret, ==, 0);
+    g_assert_null(err);
+
+    /* Partial EOF: should return -1 */
+    ret = qio_channel_pread_all_eof(ioc, read_buf, 8,
+                                    sizeof(write_buf) - 4, &err);
+    g_assert_cmpint(ret, ==, -1);
+    g_assert_nonnull(err);
+    error_free(err);
+
+    unlink(TEST_FILE);
+    object_unref(OBJECT(ioc));
+}
+#endif /* CONFIG_PREADV */
+
 #ifndef _WIN32
 static void test_io_channel_pipe(bool async)
 {
@@ -147,6 +344,16 @@ int main(int argc, char **argv)
     g_test_add_func("/io/channel/file", test_io_channel_file);
     g_test_add_func("/io/channel/file/rdwr", test_io_channel_file_rdwr);
     g_test_add_func("/io/channel/file/fd", test_io_channel_fd);
+#ifdef CONFIG_PREADV
+    g_test_add_func("/io/channel/file/pread-all",
+                    test_io_channel_pread_all);

tests/unit/test-io-channel-file.c int main(int argc, char **argv) · line 359

Quoted 10 lines
tests/unit/test-io-channel-file.c +7 -0
+    g_test_add_func("/io/channel/file/preadv-all",
+                    test_io_channel_preadv_all);
+    g_test_add_func("/io/channel/file/preadv-all-eof",
+                    test_io_channel_preadv_all_eof);
+    g_test_add_func("/io/channel/file/pread-all-eof",
+                    test_io_channel_pread_all_eof);
+#endif
 #ifndef _WIN32
     g_test_add_func("/io/channel/pipe/sync", test_io_channel_pipe_sync);
     g_test_add_func("/io/channel/pipe/async", test_io_channel_pipe_async);
Reviewed-by Acked-by on v3 4/4
#11 lore raw

Re: [PATCH v3 4/4] tests/unit: add pread/pwrite _all tests for io channel file

In reply to Junjie Cao
Quoted 10 lines · Junjie Cao

Add unit tests for the new qio_channel_pread{v,}_all{,_eof}() and qio_channel_pwrite{v,}_all() APIs.

The basic tests write data to a file channel, then read it back at various offsets using both the single-buffer and iovec variants to make sure the round-trip produces identical content. The _eof tests verify all three return cases -- full read (1), clean EOF (0), and partial-then-EOF (-1 with error set) -- and check that the strict wrappers (preadv_all / pread_all) treat a clean EOF as an error.

Quoted 7 lines

All tests are guarded by CONFIG_PREADV since the underlying channel methods require preadv(2).

---
 tests/unit/test-io-channel-file.c | 207 ++++++++++++++++++++++++++++++
 1 file changed, 207 insertions(+)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
author
#12 lore raw
In reply to Junjie Cao

Hi Daniel, Peter,

Just checking in on this series -- does v3 look alright to you?

I have a follow-up series that depends on this one to fix the remaining positioned I/O call sites in migration, so it would be great to get this moving.

Many thanks,
Junjie

#13 lore raw
Quoted 7 lines · Junjie Cao

Hi Daniel, Peter,

Just checking in on this series -- does v3 look alright to you?

I have a follow-up series that depends on this one to fix the remaining positioned I/O call sites in migration, so it would be great to get this moving.

I've ackd the io subsystem patches, so Peter / Fabiano can just include them in a migration pull request.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#14 lore raw
Quoted 11 lines · Daniel P. Berrangé
Quoted 7 lines · Junjie Cao

Hi Daniel, Peter,

Just checking in on this series -- does v3 look alright to you?

I have a follow-up series that depends on this one to fix the remaining positioned I/O call sites in migration, so it would be great to get this moving.

I've ackd the io subsystem patches, so Peter / Fabiano can just include them in a migration pull request.

Junjie, sorry for the late response. It looks all good here, thanks for your work!

I bet Fabiano will pick this up for his first pull when 11.1 dev window opens.

Signature · 1 line
Peter Xu
#15 lore raw
In reply to Junjie Cao
Quoted 13 lines · Junjie Cao

This series fixes two bugs in multifd_file_recv_data() and completes the positioned read/write "all" API set for QIO channels.

Bug summary:
- size_t variable storing ssize_t return: -1 wraps to SIZE_MAX
- error_prepend() called without ERRP_GUARD(): NULL deref on short read
Junjie Cao (4):
io/channel: introduce qio_channel_pread{v,}_all{,_eof}() io/channel: introduce qio_channel_pwrite{v,}_all() migration/file: fix type mismatch and NULL deref in
multifd_file_recv_data
tests/unit: add pread/pwrite _all tests for io channel file

Quoted 5 lines
  include/io/channel.h              | 133 +++++++++++++++++++
  io/channel.c                      | 139 ++++++++++++++++++++
  migration/file.c                  |  13 +-
  tests/unit/test-io-channel-file.c | 207 ++++++++++++++++++++++++++++++
  4 files changed, 486 insertions(+), 6 deletions(-)

Hi!

Is this a qemu-stable material? The amount of new code this series introduces is somewhat large, but it fixes real bugs in the end. On the other hand, these bugs doesn't seem to be very serious, so.. I'm not sure :)

How do you think, should we pick this up for the current stable releases? It applies cleanly to 10.0.x (the earliest stable series currently supported).

Thanks,

/mjt

#16 lore raw
Quoted 22 lines · Michael Tokarev
Quoted 13 lines · Junjie Cao

This series fixes two bugs in multifd_file_recv_data() and completes the positioned read/write "all" API set for QIO channels.

Bug summary:
   - size_t variable storing ssize_t return: -1 wraps to SIZE_MAX
   - error_prepend() called without ERRP_GUARD(): NULL deref on short read Junjie Cao (4):
   io/channel: introduce qio_channel_pread{v,}_all{,_eof}() io/channel: introduce qio_channel_pwrite{v,}_all() migration/file: fix type mismatch and NULL deref in multifd_file_recv_data
   tests/unit: add pread/pwrite _all tests for io channel file

Quoted 5 lines

  include/io/channel.h              | 133 +++++++++++++++++++ io/channel.c                      | 139 ++++++++++++++++++++ migration/file.c                  |  13 +- tests/unit/test-io-channel-file.c | 207 ++++++++++++++++++++++++++++++

  4 files changed, 486 insertions(+), 6 deletions(-)

Hi!

Quoted 8 lines

Is this a qemu-stable material?  The amount of new code this series introduces is somewhat large, but it fixes real bugs in the end.  On the other hand, these bugs doesn't seem to be very serious, so.. I'm not sure :)

How do you think, should we pick this up for the current stable releases? It applies cleanly to 10.0.x (the earliest stable series currently supported).

For 10.0.x, it also needs two more changes to work:

a5bc1ccca95 migration: simplify error reporting after channel read 1edf0df2840 io: Add qio_channel_wait_cond() helper

Thanks,

/mjt

#17 lore raw
Quoted 33 lines · Michael Tokarev
Quoted 23 lines · Michael Tokarev
Quoted 14 lines · Junjie Cao

This series fixes two bugs in multifd_file_recv_data() and completes the positioned read/write "all" API set for QIO channels.

Bug summary:
   - size_t variable storing ssize_t return: -1 wraps to SIZE_MAX
   - error_prepend() called without ERRP_GUARD(): NULL deref on short read
Junjie Cao (4):
   io/channel: introduce qio_channel_pread{v,}_all{,_eof}() io/channel: introduce qio_channel_pwrite{v,}_all() migration/file: fix type mismatch and NULL deref in multifd_file_recv_data
   tests/unit: add pread/pwrite _all tests for io channel file

Quoted 5 lines

  include/io/channel.h              | 133 +++++++++++++++++++ io/channel.c                      | 139 ++++++++++++++++++++ migration/file.c                  |  13 +- tests/unit/test-io-channel-file.c | 207 ++++++++++++++++++++++++++++++

  4 files changed, 486 insertions(+), 6 deletions(-)

Hi!

Quoted 8 lines

Is this a qemu-stable material?  The amount of new code this series introduces is somewhat large, but it fixes real bugs in the end.  On the other hand, these bugs doesn't seem to be very serious, so.. I'm not sure :)

How do you think, should we pick this up for the current stable releases? It applies cleanly to 10.0.x (the earliest stable series currently supported).

Quoted 6 lines

For 10.0.x, it also needs two more changes to work:

a5bc1ccca95 migration: simplify error reporting after channel read 1edf0df2840 io: Add qio_channel_wait_cond() helper

Thanks,

Hi! Sorry for the delay. I don't think we need this series for stable. It's a fairly unlikely issue.