Skip to contentSkip to conversation

MSG_ZEROCOPY for multifd

PATCH Superseded Leonardo Bras 5 participants 35 messages Oct 9, 2021 – Apr 14, 2022

v4 of 13 Superseded Latest is v13 →

lore
Share & tools
lore
#0 lore raw

v4 cover MSG_ZEROCOPY for multifd

This patch series intends to enable MSG_ZEROCOPY in QIOChannel, and make use of it for multifd migration performance improvement.

Patch #1 creates new callbacks for QIOChannel, allowing the implementation of zerocopy writing.

Patch #2 implements writev_zerocopy and flush_zerocopy on QIOChannelSocket, making use of MSG_ZEROCOPY on Linux.

Patch #3 Makes use of QIOChannelSocket zerocopy implementation on nocomp multifd migration.

Results:
So far, the resource usage of __sys_sendmsg() reduced 15 times, and the overall migration took 13-22% less time, based in synthetic workload.

The objective is to reduce migration time in hosts with heavy cpu usage.

---
Changes since v3:
- QIOChannel interface names changed from io_async_{writev,flush} to io_{writev,flush}_zerocopy
- Instead of falling back in case zerocopy is not implemented, return error and abort operation.
- Flush now waits as long as needed, or return error in case anything goes wrong, aborting the operation.
- Zerocopy is now conditional in multifd, being set by parameter multifd-zerocopy
- Moves zerocopy_flush to multifd_send_sync_main() from multifd_save_cleanup so migration can abort if flush goes wrong.
- Several other small improvements

Changes since v2:
- Patch #1: One more fallback
- Patch #2: Fall back to sync if fails to lock buffer memory in MSG_ZEROCOPY send.

Changes since v1:
- Reimplemented the patchset using async_write + async_flush approach.
- Implemented a flush to be able to tell whenever all data was written.

Leonardo Bras (3):
QIOChannel: Add io_writev_zerocopy & io_flush_zerocopy callbacks
QIOChannelSocket: Implement io_writev_zerocopy & io_flush_zerocopy for
CONFIG_LINUX
multifd: Implement zerocopy write in multifd migration
(multifd-zerocopy)

 qapi/migration.json         |  18 ++++
 include/io/channel-socket.h |   2 +
 include/io/channel.h        | 104 +++++++++++++++++----
 migration/migration.h       |   1 +
 migration/multifd.h         |   2 +-
 io/channel-socket.c         | 180 ++++++++++++++++++++++++++++++++++--
 io/channel.c                |  74 +++++++++++----
 migration/migration.c       |  20 ++++
 migration/multifd.c         |  33 ++++++-
 migration/ram.c             |  20 ++--
 monitor/hmp-cmds.c          |   4 +
 11 files changed, 399 insertions(+), 59 deletions(-)
Signature · 1 line
2.33.0
#1 lore raw

PATCH v4 1/3 QIOChannel: Add io_writev_zerocopy & io_flush_zerocopy callbacks

Adds io_async_writev and io_async_flush as optional callback to QIOChannelClass, allowing the implementation of asynchronous writes by subclasses.

How to use them:
- Write data using qio_channel_writev_zerocopu(),
- Wait write completion with qio_channel_flush_zerocopy().

Notes:
As some zerocopy implementations work asynchronously, it's recommended to keep the write buffer untouched until the return of qio_channel_flush_zerocopy(), by the risk of sending an updated buffer instead of the one at the write.

As the new callbacks are optional, if a subclass does not implement them, then:
- io_async_writev will return -1,
- io_async_flush will return 0 without changing anything.

Also, some functions like qio_channel_writev_full_all() were adapted to receive a flag parameter. That allows shared code between zerocopy and non-zerocopy writev.

---
 include/io/channel.h | 103 +++++++++++++++++++++++++++++++++++--------
 io/channel.c         |  74 +++++++++++++++++++++++--------
 2 files changed, 141 insertions(+), 36 deletions(-)
include/io/channel.h +85 -18
index 88988979f8..e7d4e1521f 100644
@@ -32,12 +32,15 @@ OBJECT_DECLARE_TYPE(QIOChannel, QIOChannelClass,
 
 #define QIO_CHANNEL_ERR_BLOCK -2
 
+#define QIO_CHANNEL_WRITE_FLAG_ZEROCOPY 0x1
+
 typedef enum QIOChannelFeature QIOChannelFeature;
 
 enum QIOChannelFeature {
     QIO_CHANNEL_FEATURE_FD_PASS,
     QIO_CHANNEL_FEATURE_SHUTDOWN,
     QIO_CHANNEL_FEATURE_LISTEN,
+    QIO_CHANNEL_FEATURE_WRITE_ZEROCOPY,
 };
 
 
@@ -136,6 +139,12 @@ struct QIOChannelClass {
                                   IOHandler *io_read,
                                   IOHandler *io_write,
                                   void *opaque);
+    ssize_t (*io_writev_zerocopy)(QIOChannel *ioc,
+                                  const struct iovec *iov,
+                                  size_t niov,
+                                  Error **errp);
+    int (*io_flush_zerocopy)(QIOChannel *ioc,
+                              Error **errp);
 };
 
 /* General I/O handling functions */
@@ -222,12 +231,13 @@ ssize_t qio_channel_readv_full(QIOChannel *ioc,
 
 
 /**
- * qio_channel_writev_full:
+ * qio_channel_writev_full_flags:
  * @ioc: the channel object
  * @iov: the array of memory regions to write data from
  * @niov: the length of the @iov array
  * @fds: an array of file handles to send
  * @nfds: number of file handles in @fds
+ * @flags: write flags (QIO_CHANNEL_WRITE_FLAG_*)
  * @errp: pointer to a NULL-initialized error object
  *
  * Write data to the IO channel, reading it from the
@@ -242,6 +252,10 @@ ssize_t qio_channel_readv_full(QIOChannel *ioc,
  * guaranteed. If the channel is non-blocking and no
  * data can be sent, it will return QIO_CHANNEL_ERR_BLOCK
  *
+ * If flag QIO_CHANNEL_WRITE_FLAG_ZEROCOPY is passed,
+ * function will return once each buffer was queued for
+ * sending.
+ *
  * If there are file descriptors to send, the @fds
  * array should be non-NULL and provide the handles.
  * All file descriptors will be sent if at least one
@@ -255,12 +269,15 @@ ssize_t qio_channel_readv_full(QIOChannel *ioc,
  * or QIO_CHANNEL_ERR_BLOCK if no data is can be sent
  * and the channel is non-blocking
  */
-ssize_t qio_channel_writev_full(QIOChannel *ioc,
-                                const struct iovec *iov,
-                                size_t niov,
-                                int *fds,
-                                size_t nfds,
-                                Error **errp);
+ssize_t qio_channel_writev_full_flags(QIOChannel *ioc,
+                                      const struct iovec *iov,
+                                      size_t niov,
+                                      int *fds,
+                                      size_t nfds,
+                                      int flags,
+                                      Error **errp);
+#define qio_channel_writev_full(ioc, iov, niov, fds, nfds, errp) \
+    qio_channel_writev_full_flags(ioc, iov, niov, fds, nfds, 0, errp)
 
 /**
  * qio_channel_readv_all_eof:
@@ -321,10 +338,11 @@ int qio_channel_readv_all(QIOChannel *ioc,
 
 
 /**
- * qio_channel_writev_all:
+ * qio_channel_writev_all_flags:
  * @ioc: the channel object
  * @iov: the array of memory regions to write data from
  * @niov: the length of the @iov array
+ * @flags: write flags (QIO_CHANNEL_WRITE_FLAG_*)
  * @errp: pointer to a NULL-initialized error object
  *
  * Write data to the IO channel, reading it from the
@@ -339,10 +357,13 @@ int qio_channel_readv_all(QIOChannel *ioc,
  *
  * Returns: 0 if all bytes were written, or -1 on error
  */
-int qio_channel_writev_all(QIOChannel *ioc,
-                           const struct iovec *iov,
-                           size_t niov,
-                           Error **erp);
+int qio_channel_writev_all_flags(QIOChannel *ioc,
+                                 const struct iovec *iov,
+                                 size_t niov,
+                                 int flags,
+                                 Error **errp);
+#define qio_channel_writev_all(ioc, iov, niov, errp) \
+    qio_channel_writev_all_flags(ioc, iov, niov, 0, errp)
 
 /**
  * qio_channel_readv:
@@ -831,12 +852,13 @@ int qio_channel_readv_full_all(QIOChannel *ioc,
                                Error **errp);
 
 /**
- * qio_channel_writev_full_all:
+ * qio_channel_writev_full_all_flags:
  * @ioc: the channel object
  * @iov: the array of memory regions to write data from
  * @niov: the length of the @iov array
  * @fds: an array of file handles to send
  * @nfds: number of file handles in @fds
+ * @flags: write flags (QIO_CHANNEL_WRITE_FLAG_*)
  * @errp: pointer to a NULL-initialized error object
  *
  *
@@ -846,13 +868,58 @@ int qio_channel_readv_full_all(QIOChannel *ioc,
  * to be written, yielding from the current coroutine
  * if required.
  *
+ * If QIO_CHANNEL_WRITE_FLAG_ZEROCOPY is passed in flags,
+ * instead of waiting for all requested data to be written,
+ * this function will wait until it's all queued for writing.
+ *
  * Returns: 0 if all bytes were written, or -1 on error
  */
 
-int qio_channel_writev_full_all(QIOChannel *ioc,
-                                const struct iovec *iov,
-                                size_t niov,
-                                int *fds, size_t nfds,
-                                Error **errp);
+int qio_channel_writev_full_all_flags(QIOChannel *ioc,
+                                      const struct iovec *iov,
+                                      size_t niov,
+                                      int *fds, size_t nfds,
+                                      int flags, Error **errp);
+#define qio_channel_writev_full_all(ioc, iov, niov, fds, nfds, errp) \
+    qio_channel_writev_full_all_flags(ioc, iov, niov, fds, nfds, 0, errp)
+
+/**
+ * qio_channel_writev_zerocopy:
+ * @ioc: the channel object
+ * @iov: the array of memory regions to write data from
+ * @niov: the length of the @iov array
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * Behaves like qio_channel_writev_full_all_flags, but will write
+ * data asynchronously while avoiding unnecessary data copy.
+ * This function may return before any data is actually written,
+ * but should queue every buffer for writting.
+ *
+ * If at some point it's necessary wait for all data to be
+ * written, use qio_channel_flush_zerocopy().
+ *
+ * If zerocopy is not available, returns -1 and set errp.
+ */
+
+ssize_t qio_channel_writev_zerocopy(QIOChannel *ioc,
+                                    const struct iovec *iov,
+                                    size_t niov,
+                                    Error **errp);
+
+/**
+ * qio_channel_flush_zerocopy:
+ * @ioc: the channel object
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * Will lock until every packet queued with
+ * qio_channel_writev_zerocopy() is sent, or return
+ * in case of any error.
+ *
+ * Returns -1 if any error is found, 0 otherwise.
+ * If not implemented, returns 0 without changing anything.
+ */
+
+int qio_channel_flush_zerocopy(QIOChannel *ioc,
+                               Error **errp);
 
 #endif /* QIO_CHANNEL_H */
io/channel.c +56 -18

104 lines not on this page · raw message

Signature · 1 line
2.33.0
#2 lore raw

Re: [PATCH v4 1/3] QIOChannel: Add io_writev_zerocopy & io_flush_zerocopy callbacks

Quoted 1 line · Leonardo Bras

Adds io_async_writev and io_async_flush as optional callback to QIOChannelClass,

Are these names accurate?

Quoted 4 lines

allowing the implementation of asynchronous writes by subclasses.

How to use them:
- Write data using qio_channel_writev_zerocopu(),

s/copu/copy/

Quoted 6 lines

- Wait write completion with qio_channel_flush_zerocopy().

Notes:
As some zerocopy implementations work asynchronously, it's recommended to keep the write buffer untouched until the return of qio_channel_flush_zerocopy(), by the risk of sending an updated buffer

s/by/to avoid/

Quoted 5 lines

instead of the one at the write.

As the new callbacks are optional, if a subclass does not implement them, then:
- io_async_writev will return -1,
- io_async_flush will return 0 without changing anything.

Are these names accurate?

Quoted 32 lines

Also, some functions like qio_channel_writev_full_all() were adapted to receive a flag parameter. That allows shared code between zerocopy and non-zerocopy writev.

---
 include/io/channel.h | 103 +++++++++++++++++++++++++++++++++++--------
 io/channel.c         |  74 +++++++++++++++++++++++--------
 2 files changed, 141 insertions(+), 36 deletions(-)
include/io/channel.h +3 -0
index 88988979f8..e7d4e1521f 100644
@@ -32,12 +32,15 @@ OBJECT_DECLARE_TYPE(QIOChannel, QIOChannelClass,
 
 #define QIO_CHANNEL_ERR_BLOCK -2
 
+#define QIO_CHANNEL_WRITE_FLAG_ZEROCOPY 0x1
+
 typedef enum QIOChannelFeature QIOChannelFeature;
 
 enum QIOChannelFeature {
     QIO_CHANNEL_FEATURE_FD_PASS,
     QIO_CHANNEL_FEATURE_SHUTDOWN,
     QIO_CHANNEL_FEATURE_LISTEN,
+    QIO_CHANNEL_FEATURE_WRITE_ZEROCOPY,
 };
 
 

include/io/channel.h struct QIOChannelClass { · line 147

Quoted 9 lines
include/io/channel.h +6 -0
@@ -136,6 +139,12 @@ struct QIOChannelClass {
                                   IOHandler *io_read,
                                   IOHandler *io_write,
                                   void *opaque);
+    ssize_t (*io_writev_zerocopy)(QIOChannel *ioc,
+                                  const struct iovec *iov,
+                                  size_t niov,
+                                  Error **errp);
+    int (*io_flush_zerocopy)(QIOChannel *ioc,
+                              Error **errp);

Indentation is off by one.

Quoted 19 lines
include/io/channel.h +2 -1
 };
 
 /* General I/O handling functions */
@@ -222,12 +231,13 @@ ssize_t qio_channel_readv_full(QIOChannel *ioc,
 
 
 /**
- * qio_channel_writev_full:
+ * qio_channel_writev_full_flags:
  * @ioc: the channel object
  * @iov: the array of memory regions to write data from
  * @niov: the length of the @iov array
  * @fds: an array of file handles to send
  * @nfds: number of file handles in @fds
+ * @flags: write flags (QIO_CHANNEL_WRITE_FLAG_*)
  * @errp: pointer to a NULL-initialized error object
  *
  * Write data to the IO channel, reading it from the

include/io/channel.h ssize_t qio_channel_readv_full(QIOChannel *ioc, · line 257

Quoted 6 lines
include/io/channel.h +3 -0
@@ -242,6 +252,10 @@ ssize_t qio_channel_readv_full(QIOChannel *ioc,
  * guaranteed. If the channel is non-blocking and no
  * data can be sent, it will return QIO_CHANNEL_ERR_BLOCK
  *
+ * If flag QIO_CHANNEL_WRITE_FLAG_ZEROCOPY is passed,
+ * function will return once each buffer was queued for
+ * sending.

This would be a good place to document the requirement to keep the buffer unchanged until the zerocopy sequence completes.

Quoted 15 lines
include/io/channel.h +2 -1
                                Error **errp);
 
 /**
- * qio_channel_writev_full_all:
+ * qio_channel_writev_full_all_flags:
  * @ioc: the channel object
  * @iov: the array of memory regions to write data from
  * @niov: the length of the @iov array
  * @fds: an array of file handles to send
  * @nfds: number of file handles in @fds
+ * @flags: write flags (QIO_CHANNEL_WRITE_FLAG_*)
  * @errp: pointer to a NULL-initialized error object
  *
  *

include/io/channel.h int qio_channel_readv_full_all(QIOChannel *ioc, · line 873

Quoted 6 lines
include/io/channel.h +3 -0
@@ -846,13 +868,58 @@ int qio_channel_readv_full_all(QIOChannel *ioc,
  * to be written, yielding from the current coroutine
  * if required.
  *
+ * If QIO_CHANNEL_WRITE_FLAG_ZEROCOPY is passed in flags,
+ * instead of waiting for all requested data to be written,
+ * this function will wait until it's all queued for writing.

Another good place to document restrictions on buffer stability.

Quoted 18 lines
include/io/channel.h +10 -5
+ *
  * Returns: 0 if all bytes were written, or -1 on error
  */
 
-int qio_channel_writev_full_all(QIOChannel *ioc,
-                                const struct iovec *iov,
-                                size_t niov,
-                                int *fds, size_t nfds,
-                                Error **errp);
+int qio_channel_writev_full_all_flags(QIOChannel *ioc,
+                                      const struct iovec *iov,
+                                      size_t niov,
+                                      int *fds, size_t nfds,
+                                      int flags, Error **errp);
+#define qio_channel_writev_full_all(ioc, iov, niov, fds, nfds, errp) \
+    qio_channel_writev_full_all_flags(ioc, iov, niov, fds, nfds, 0, errp)
+
+/**

include/io/channel.h int qio_channel_readv_full_all(QIOChannel *ioc, · line 896

Quoted 10 lines
include/io/channel.h +10 -0
+ * qio_channel_writev_zerocopy:
+ * @ioc: the channel object
+ * @iov: the array of memory regions to write data from
+ * @niov: the length of the @iov array
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * Behaves like qio_channel_writev_full_all_flags, but will write
+ * data asynchronously while avoiding unnecessary data copy.
+ * This function may return before any data is actually written,
+ * but should queue every buffer for writting.

writing

Another place to document buffer stability considerations.

include/io/channel.h int qio_channel_readv_full_all(QIOChannel *ioc, · line 898

Quoted 2 lines
include/io/channel.h +2 -0
+ *
+ * If at some point it's necessary wait for all data to be

s/wait/to wait/

Quoted 6 lines
include/io/channel.h +6 -0
+ * written, use qio_channel_flush_zerocopy().
+ *
+ * If zerocopy is not available, returns -1 and set errp.
+ */
+
+ssize_t qio_channel_writev_zerocopy(QIOChannel *ioc,

include/io/channel.h int qio_channel_readv_full_all(QIOChannel *ioc, · line 914

Quoted 10 lines
include/io/channel.h +10 -0
+                                    const struct iovec *iov,
+                                    size_t niov,
+                                    Error **errp);
+
+/**
+ * qio_channel_flush_zerocopy:
+ * @ioc: the channel object
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * Will lock until every packet queued with

s/lock/block/

Quoted 15 lines
include/io/channel.h +9 -0
+ * qio_channel_writev_zerocopy() is sent, or return
+ * in case of any error.
+ *
+ * Returns -1 if any error is found, 0 otherwise.
+ * If not implemented, returns 0 without changing anything.
+ */
+
+int qio_channel_flush_zerocopy(QIOChannel *ioc,
+                               Error **errp);
 
 #endif /* QIO_CHANNEL_H */
io/channel.c +0 -0
index e8b019dc36..811c93ae23 100644

io/channel.c off_t qio_channel_io_seek(QIOChannel *ioc, · line 508

Quoted 9 lines
io/channel.c +8 -0
 
+int qio_channel_flush_zerocopy(QIOChannel *ioc,
+                               Error **errp)
+{
+    QIOChannelClass *klass = QIO_CHANNEL_GET_CLASS(ioc);
+
+    if (!klass->io_flush_zerocopy ||
+        !qio_channel_has_feature(ioc, QIO_CHANNEL_FEATURE_WRITE_ZEROCOPY)) {
+        return 0;

Matches your documentation, but an ideal app should not be trying to flush if the write failed in the first place. So wouldn't it be better to return -1 or even abort() on a coding error?

Quoted 12 lines
io/channel.c +6 -0
+    }
+
+    return klass->io_flush_zerocopy(ioc, errp);
+}
+
+
 static void qio_channel_restart_read(void *opaque)
 {
     QIOChannel *ioc = opaque;
Signature · 1 line
2.33.0
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#3 lore raw

Re: [PATCH v4 1/3] QIOChannel: Add io_writev_zerocopy & io_flush_zerocopy callbacks

Hello Eric, thank you for the feedback!

Quoted 5 lines · Eric Blake
Quoted 1 line · Leonardo Bras

Adds io_async_writev and io_async_flush as optional callback to QIOChannelClass,

Are these names accurate?

I am sorry, I think I missed some renaming before generating the patchset. As you suggested those names are incorrect (as they reflect a previous naming used in v3).
I will replace them for io_{writev,flush}_zerocopy in v5.

Quoted 7 lines
Quoted 5 lines

allowing the implementation of asynchronous writes by subclasses.

How to use them:
- Write data using qio_channel_writev_zerocopu(),

s/copu/copy/

Thanks, I will fix that.

Quoted 10 lines
Quoted 7 lines

- Wait write completion with qio_channel_flush_zerocopy().

Notes:
As some zerocopy implementations work asynchronously, it's recommended to keep the write buffer untouched until the return of qio_channel_flush_zerocopy(), by the risk of sending an updated buffer

s/by/to avoid/

Quoted 7 lines
Quoted 5 lines

instead of the one at the write.

As the new callbacks are optional, if a subclass does not implement them, then:
- io_async_writev will return -1,
- io_async_flush will return 0 without changing anything.

Are these names accurate?

They are not, I will replace them for io_{writev,flush}_zerocopy in v5.

Quoted 32 lines
Quoted 32 lines

Also, some functions like qio_channel_writev_full_all() were adapted to receive a flag parameter. That allows shared code between zerocopy and non-zerocopy writev.

---
 include/io/channel.h | 103 +++++++++++++++++++++++++++++++++++--------
 io/channel.c         |  74 +++++++++++++++++++++++--------
 2 files changed, 141 insertions(+), 36 deletions(-)
include/io/channel.h +3 -0
index 88988979f8..e7d4e1521f 100644
@@ -32,12 +32,15 @@ OBJECT_DECLARE_TYPE(QIOChannel, QIOChannelClass,
 
 #define QIO_CHANNEL_ERR_BLOCK -2
 
+#define QIO_CHANNEL_WRITE_FLAG_ZEROCOPY 0x1
+
 typedef enum QIOChannelFeature QIOChannelFeature;
 
 enum QIOChannelFeature {
     QIO_CHANNEL_FEATURE_FD_PASS,
     QIO_CHANNEL_FEATURE_SHUTDOWN,
     QIO_CHANNEL_FEATURE_LISTEN,
+    QIO_CHANNEL_FEATURE_WRITE_ZEROCOPY,
 };
 
 
Quoted 12 lines

include/io/channel.h struct QIOChannelClass { · line 147

Quoted 10 lines
include/io/channel.h +6 -0
@@ -136,6 +139,12 @@ struct QIOChannelClass {
                                   IOHandler *io_read,
                                   IOHandler *io_write,
                                   void *opaque);
+    ssize_t (*io_writev_zerocopy)(QIOChannel *ioc,
+                                  const struct iovec *iov,
+                                  size_t niov,
+                                  Error **errp);
+    int (*io_flush_zerocopy)(QIOChannel *ioc,
+                              Error **errp);

Indentation is off by one.

Thanks, I will fix that for v5.

Quoted 21 lines
Quoted 21 lines
include/io/channel.h +2 -1
 };
 
 /* General I/O handling functions */
@@ -222,12 +231,13 @@ ssize_t qio_channel_readv_full(QIOChannel *ioc,
 
 
 /**
- * qio_channel_writev_full:
+ * qio_channel_writev_full_flags:
  * @ioc: the channel object
  * @iov: the array of memory regions to write data from
  * @niov: the length of the @iov array
  * @fds: an array of file handles to send
  * @nfds: number of file handles in @fds
+ * @flags: write flags (QIO_CHANNEL_WRITE_FLAG_*)
  * @errp: pointer to a NULL-initialized error object
  *
  * Write data to the IO channel, reading it from the
@@ -242,6 +252,10 @@ ssize_t qio_channel_readv_full(QIOChannel *ioc,
  * guaranteed. If the channel is non-blocking and no
Quoted 8 lines

include/io/channel.h ssize_t qio_channel_readv_full(QIOChannel *ioc, · line 257

Quoted 5 lines
include/io/channel.h +3 -0
  * data can be sent, it will return QIO_CHANNEL_ERR_BLOCK
  *
+ * If flag QIO_CHANNEL_WRITE_FLAG_ZEROCOPY is passed,
+ * function will return once each buffer was queued for
+ * sending.

This would be a good place to document the requirement to keep the buffer unchanged until the zerocopy sequence completes.

That makes sense, even though that may be true for just some implementations, it makes sense to document it here.

Quoted 16 lines
Quoted 16 lines
include/io/channel.h +2 -1
                                Error **errp);
 
 /**
- * qio_channel_writev_full_all:
+ * qio_channel_writev_full_all_flags:
  * @ioc: the channel object
  * @iov: the array of memory regions to write data from
  * @niov: the length of the @iov array
  * @fds: an array of file handles to send
  * @nfds: number of file handles in @fds
+ * @flags: write flags (QIO_CHANNEL_WRITE_FLAG_*)
  * @errp: pointer to a NULL-initialized error object
  *
  *
@@ -846,13 +868,58 @@ int qio_channel_readv_full_all(QIOChannel *ioc,
Quoted 8 lines

include/io/channel.h int qio_channel_readv_full_all(QIOChannel *ioc, · line 873

Quoted 6 lines
include/io/channel.h +3 -0
  * to be written, yielding from the current coroutine
  * if required.
  *
+ * If QIO_CHANNEL_WRITE_FLAG_ZEROCOPY is passed in flags,
+ * instead of waiting for all requested data to be written,
+ * this function will wait until it's all queued for writing.

Another good place to document restrictions on buffer stability.

Ok :)

Quoted 32 lines
Quoted 19 lines
include/io/channel.h +10 -5
+ *
  * Returns: 0 if all bytes were written, or -1 on error
  */
 
-int qio_channel_writev_full_all(QIOChannel *ioc,
-                                const struct iovec *iov,
-                                size_t niov,
-                                int *fds, size_t nfds,
-                                Error **errp);
+int qio_channel_writev_full_all_flags(QIOChannel *ioc,
+                                      const struct iovec *iov,
+                                      size_t niov,
+                                      int *fds, size_t nfds,
+                                      int flags, Error **errp);
+#define qio_channel_writev_full_all(ioc, iov, niov, fds, nfds, errp) \
+    qio_channel_writev_full_all_flags(ioc, iov, niov, fds, nfds, 0, errp)
+
+/**

include/io/channel.h int qio_channel_readv_full_all(QIOChannel *ioc, · line 896

Quoted 10 lines
include/io/channel.h +10 -0
+ * qio_channel_writev_zerocopy:
+ * @ioc: the channel object
+ * @iov: the array of memory regions to write data from
+ * @niov: the length of the @iov array
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * Behaves like qio_channel_writev_full_all_flags, but will write
+ * data asynchronously while avoiding unnecessary data copy.
+ * This function may return before any data is actually written,
+ * but should queue every buffer for writting.

writing

Quoted 1 line

Another place to document buffer stability considerations.

Ok,
Is it enough to document it in a single one of the places suggested, or would you recommend documenting it in all suggested places?

Quoted 5 lines

include/io/channel.h int qio_channel_readv_full_all(QIOChannel *ioc, · line 898

Quoted 3 lines
include/io/channel.h +2 -0
+ *
+ * If at some point it's necessary wait for all data to be

s/wait/to wait/

I will fix that for v5, thanks!

Quoted 11 lines
Quoted 11 lines
include/io/channel.h +10 -0
+ * written, use qio_channel_flush_zerocopy().
+ *
+ * If zerocopy is not available, returns -1 and set errp.
+ */
+
+ssize_t qio_channel_writev_zerocopy(QIOChannel *ioc,
+                                    const struct iovec *iov,
+                                    size_t niov,
+                                    Error **errp);
+
Quoted 8 lines

include/io/channel.h int qio_channel_readv_full_all(QIOChannel *ioc, · line 914

Quoted 6 lines
include/io/channel.h +6 -0
+/**
+ * qio_channel_flush_zerocopy:
+ * @ioc: the channel object
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * Will lock until every packet queued with

s/lock/block/

Yeah, I should have fixed it in v4.
Thanks for pointing this out.

Quoted 17 lines
Quoted 16 lines
include/io/channel.h +9 -0
+ * qio_channel_writev_zerocopy() is sent, or return
+ * in case of any error.
+ *
+ * Returns -1 if any error is found, 0 otherwise.
+ * If not implemented, returns 0 without changing anything.
+ */
+
+int qio_channel_flush_zerocopy(QIOChannel *ioc,
+                               Error **errp);
 
 #endif /* QIO_CHANNEL_H */
io/channel.c +0 -0
index e8b019dc36..811c93ae23 100644
Quoted 12 lines

io/channel.c off_t qio_channel_io_seek(QIOChannel *ioc, · line 508

Quoted 8 lines
io/channel.c +8 -0
+int qio_channel_flush_zerocopy(QIOChannel *ioc,
+                               Error **errp)
+{
+    QIOChannelClass *klass = QIO_CHANNEL_GET_CLASS(ioc);
+
+    if (!klass->io_flush_zerocopy ||
+        !qio_channel_has_feature(ioc, QIO_CHANNEL_FEATURE_WRITE_ZEROCOPY)) {
+        return 0;

Matches your documentation, but an ideal app should not be trying to flush if the write failed in the first place. So wouldn't it be better to return -1 or even abort() on a coding error?

The point here is that any valid user of zrocopy_flush would have already used zerocopy_writev
at some point, and failed if not supported / enabled.

Having this not returning error can help the user keep a simpler approach when using
a setup in which the writev can happen in both zerocopy or default behavior.

I mean, the user will not need to check if zerocopy was or was not enabled, and just flush anyway.

But if it's not good behavior, or you guys think it's a better approach to fail here, I can also do that.

Quoted 10 lines
Quoted 10 lines
io/channel.c +6 -0
+    }
+
+    return klass->io_flush_zerocopy(ioc, errp);
+}
+
+
 static void qio_channel_restart_read(void *opaque)
 {
     QIOChannel *ioc = opaque;
Quoted 9 lines
Quoted 3 lines

--
2.33.0

--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization: qemu.org | libvirt.org

Thank you for reviewing!

Best regards,
Leo

#4 lore raw

Re: [PATCH v4 1/3] QIOChannel: Add io_writev_zerocopy & io_flush_zerocopy callbacks

Quoted 23 lines · Leonardo Bras Soares Passos
Quoted 14 lines
Quoted 14 lines

/**
- * qio_channel_writev_full:
+ * qio_channel_writev_full_flags:
* @ioc: the channel object
* @iov: the array of memory regions to write data from
* @niov: the length of the @iov array
* @fds: an array of file handles to send
* @nfds: number of file handles in @fds
+ * @flags: write flags (QIO_CHANNEL_WRITE_FLAG_*)
* @errp: pointer to a NULL-initialized error object *
* Write data to the IO channel, reading it from the

include/io/channel.h +0 -0
@@ -242,6 +252,10 @@ ssize_t qio_channel_readv_full(QIOChannel *ioc,
  * guaranteed. If the channel is non-blocking and no
Quoted 8 lines

include/io/channel.h ssize_t qio_channel_readv_full(QIOChannel *ioc, · line 257

Quoted 5 lines
include/io/channel.h +3 -0
  * data can be sent, it will return QIO_CHANNEL_ERR_BLOCK
  *
+ * If flag QIO_CHANNEL_WRITE_FLAG_ZEROCOPY is passed,
+ * function will return once each buffer was queued for
+ * sending.

This would be a good place to document the requirement to keep the buffer unchanged until the zerocopy sequence completes.

Quoted 8 lines

That makes sense, even though that may be true for just some implementations, it makes sense to document it here.

Ok,
Is it enough to document it in a single one of the places suggested, or would you recommend documenting it in all suggested places?

Ah, the curse of maintaining copy-and-paste. If you can find a way to say "see this other type for limitations" that sounds fine, it avoids the risk of later edits touching one but not all identical copies. But our current process for generating sphynx documentation from the qapi generator does not have cross-referencing abilities that I'm aware of. Markus or John, any thoughts?

Quoted 22 lines
Quoted 13 lines

io/channel.c off_t qio_channel_io_seek(QIOChannel *ioc, · line 508

Quoted 9 lines
io/channel.c +8 -0
+int qio_channel_flush_zerocopy(QIOChannel *ioc,
+                               Error **errp)
+{
+    QIOChannelClass *klass = QIO_CHANNEL_GET_CLASS(ioc);
+
+    if (!klass->io_flush_zerocopy ||
+        !qio_channel_has_feature(ioc, QIO_CHANNEL_FEATURE_WRITE_ZEROCOPY)) {
+        return 0;

Matches your documentation, but an ideal app should not be trying to flush if the write failed in the first place. So wouldn't it be better to return -1 or even abort() on a coding error?

The point here is that any valid user of zrocopy_flush would have already used zerocopy_writev
at some point, and failed if not supported / enabled.

Having this not returning error can help the user keep a simpler approach when using
a setup in which the writev can happen in both zerocopy or default behavior.

Quoted 5 lines

I mean, the user will not need to check if zerocopy was or was not enabled, and just flush anyway.

But if it's not good behavior, or you guys think it's a better approach to fail here, I can also do that.

Either flush is supposed to be a no-op when zerocopy fails (so returning 0 is okay), or should not be attempted unless zerocopy succeeded (in which case abort()ing seems like the best way to point out the programmer's error). But I wasn't clear from your documentation which of the two behaviors you had in mind.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#5 lore raw

Re: [PATCH v4 1/3] QIOChannel: Add io_writev_zerocopy & io_flush_zerocopy callbacks

Quoted 66 lines · Eric Blake
Quoted 23 lines · Leonardo Bras Soares Passos
Quoted 14 lines
Quoted 14 lines

/**
- * qio_channel_writev_full:
+ * qio_channel_writev_full_flags:
* @ioc: the channel object
* @iov: the array of memory regions to write data from
* @niov: the length of the @iov array
* @fds: an array of file handles to send
* @nfds: number of file handles in @fds
+ * @flags: write flags (QIO_CHANNEL_WRITE_FLAG_*)
* @errp: pointer to a NULL-initialized error object *
* Write data to the IO channel, reading it from the

include/io/channel.h +0 -0
@@ -242,6 +252,10 @@ ssize_t qio_channel_readv_full(QIOChannel *ioc,
  * guaranteed. If the channel is non-blocking and no
Quoted 8 lines

include/io/channel.h ssize_t qio_channel_readv_full(QIOChannel *ioc, · line 257

Quoted 5 lines
include/io/channel.h +3 -0
  * data can be sent, it will return QIO_CHANNEL_ERR_BLOCK
  *
+ * If flag QIO_CHANNEL_WRITE_FLAG_ZEROCOPY is passed,
+ * function will return once each buffer was queued for
+ * sending.

This would be a good place to document the requirement to keep the buffer unchanged until the zerocopy sequence completes.

Quoted 8 lines

That makes sense, even though that may be true for just some implementations, it makes sense to document it here.

Ok,
Is it enough to document it in a single one of the places suggested, or would you recommend documenting it in all suggested places?

Ah, the curse of maintaining copy-and-paste. If you can find a way to say "see this other type for limitations" that sounds fine, it avoids the risk of later edits touching one but not all identical copies. But our current process for generating sphynx documentation from the qapi generator does not have cross-referencing abilities that I'm aware of. Markus or John, any thoughts?

Quoted 25 lines
Quoted 13 lines

io/channel.c off_t qio_channel_io_seek(QIOChannel *ioc, · line 508

Quoted 9 lines
io/channel.c +8 -0
+int qio_channel_flush_zerocopy(QIOChannel *ioc,
+                               Error **errp)
+{
+    QIOChannelClass *klass = QIO_CHANNEL_GET_CLASS(ioc);
+
+    if (!klass->io_flush_zerocopy ||
+        !qio_channel_has_feature(ioc, QIO_CHANNEL_FEATURE_WRITE_ZEROCOPY)) {
+        return 0;

Matches your documentation, but an ideal app should not be trying to flush if the write failed in the first place. So wouldn't it be better to return -1 or even abort() on a coding error?

The point here is that any valid user of zrocopy_flush would have already used zerocopy_writev
at some point, and failed if not supported / enabled.

Having this not returning error can help the user keep a simpler approach when using
a setup in which the writev can happen in both zerocopy or default behavior.

I mean, the user will not need to check if zerocopy was or was not enabled, and just flush anyway.

Quoted 8 lines
Quoted 2 lines

But if it's not good behavior, or you guys think it's a better approach to fail here, I can also do that.

Either flush is supposed to be a no-op when zerocopy fails (so returning 0 is okay), or should not be attempted unless zerocopy succeeded (in which case abort()ing seems like the best way to point out the programmer's error). But I wasn't clear from your documentation which of the two behaviors you had in mind.

Oh, sorry about that.
Yeah, I intend to use it as a no-op.
If it's fine I will update the docs for v5.

Quoted 6 lines

--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization: qemu.org | libvirt.org

Thanks!

#6 lore raw

Re: [PATCH v4 1/3] QIOChannel: Add io_writev_zerocopy & io_flush_zerocopy callbacks

In reply to Leonardo Bras
Quoted 14 lines · Leonardo Bras
io/channel.c +5 -5
-int qio_channel_writev_full_all(QIOChannel *ioc,
-                                const struct iovec *iov,
-                                size_t niov,
-                                int *fds, size_t nfds,
-                                Error **errp)
+int qio_channel_writev_full_all_flags(QIOChannel *ioc,
+                                      const struct iovec *iov,
+                                      size_t niov,
+                                      int *fds, size_t nfds,
+                                      int flags, Error **errp)
 {
     int ret = -1;
     struct iovec *local_iov = g_new(struct iovec, niov);

io/channel.c int qio_channel_writev_full_all(QIOChannel *ioc, · line 254

Quoted 7 lines
io/channel.c +2 -2
@@ -237,8 +250,8 @@ int qio_channel_writev_full_all(QIOChannel *ioc,
 
     while (nlocal_iov > 0) {
         ssize_t len;
-        len = qio_channel_writev_full(ioc, local_iov, nlocal_iov, fds, nfds,
-                                      errp);
+        len = qio_channel_writev_full_flags(ioc, local_iov, nlocal_iov, fds, nfds,
+                                          flags, errp);

IMHO we can keep qio_channel_writev_full() untouched, as it is the wrapper for io_writev() hook right now (and it allows taking fds). Then here instead of adding a new flags into it, we can introduce qio_channel_writev_zerocopy_full() and directly call here:

           if (flags & ZEROCOPY) {
               assert(fds == NULL && nfds == 0);
               qio_channel_writev_zerocopy_full(...[no fds passing in]);
           } else {
               qio_channel_writev_full(...[with all parameters]);
           }

Then qio_channel_writev_zerocopy_full() should be simplely the wrapper for the new io_writev_zerocopy() hook.

Quoted 6 lines
io/channel.c +0 -0
         if (len == QIO_CHANNEL_ERR_BLOCK) {
             if (qemu_in_coroutine()) {
                 qio_channel_yield(ioc, G_IO_OUT);
@@ -474,6 +487,31 @@ off_t qio_channel_io_seek(QIOChannel *ioc,
 }
 

io/channel.c off_t qio_channel_io_seek(QIOChannel *ioc, · line 498

Quoted 10 lines
io/channel.c +9 -0
 
+ssize_t qio_channel_writev_zerocopy(QIOChannel *ioc,
+                                    const struct iovec *iov,
+                                    size_t niov,
+                                    Error **errp)
+{
+    return qio_channel_writev_full_flags(ioc, iov, niov, NULL, 0,
+                                         QIO_CHANNEL_WRITE_FLAG_ZEROCOPY,
+                                         errp);
+}

This function is never used, right? qio_channel_writev_all_flags() is used in patch 3, with proper flags passed in. Then IMHO we can drop this one.

The rest looks good, as long as with Eric's comment addressed.

Thanks,

Quoted 22 lines
io/channel.c +16 -0
+
+
+int qio_channel_flush_zerocopy(QIOChannel *ioc,
+                               Error **errp)
+{
+    QIOChannelClass *klass = QIO_CHANNEL_GET_CLASS(ioc);
+
+    if (!klass->io_flush_zerocopy ||
+        !qio_channel_has_feature(ioc, QIO_CHANNEL_FEATURE_WRITE_ZEROCOPY)) {
+        return 0;
+    }
+
+    return klass->io_flush_zerocopy(ioc, errp);
+}
+
+
 static void qio_channel_restart_read(void *opaque)
 {
     QIOChannel *ioc = opaque;
Signature · 1 line
2.33.0
Signature · 1 line
Peter Xu
#7 lore raw

Re: [PATCH v4 1/3] QIOChannel: Add io_writev_zerocopy & io_flush_zerocopy callbacks

Quoted 35 lines · Peter Xu
Quoted 14 lines · Leonardo Bras
io/channel.c +5 -5
-int qio_channel_writev_full_all(QIOChannel *ioc,
-                                const struct iovec *iov,
-                                size_t niov,
-                                int *fds, size_t nfds,
-                                Error **errp)
+int qio_channel_writev_full_all_flags(QIOChannel *ioc,
+                                      const struct iovec *iov,
+                                      size_t niov,
+                                      int *fds, size_t nfds,
+                                      int flags, Error **errp)
 {
     int ret = -1;
     struct iovec *local_iov = g_new(struct iovec, niov);

io/channel.c int qio_channel_writev_full_all(QIOChannel *ioc, · line 254

Quoted 7 lines
io/channel.c +2 -2
@@ -237,8 +250,8 @@ int qio_channel_writev_full_all(QIOChannel *ioc,
 
     while (nlocal_iov > 0) {
         ssize_t len;
-        len = qio_channel_writev_full(ioc, local_iov, nlocal_iov, fds, nfds,
-                                      errp);
+        len = qio_channel_writev_full_flags(ioc, local_iov, nlocal_iov, fds, nfds,
+                                          flags, errp);

IMHO we can keep qio_channel_writev_full() untouched, as it is the wrapper for io_writev() hook right now (and it allows taking fds). Then here instead of adding a new flags into it, we can introduce qio_channel_writev_zerocopy_full() and directly call here:

           if (flags & ZEROCOPY) {
               assert(fds == NULL && nfds == 0);
               qio_channel_writev_zerocopy_full(...[no fds passing in]);
           } else {
               qio_channel_writev_full(...[with all parameters]);
           }
Quoted 2 lines

Then qio_channel_writev_zerocopy_full() should be simplely the wrapper for the new io_writev_zerocopy() hook.

Sorry I think the name doesn't need to have "_full" - that should be used for io_writev() when we need to pass in fds. zerocopy doesn't have that, so I think we can just call it qio_channel_writev_zerocopy() as it simply does what writev() does.

Quoted 50 lines
Quoted 7 lines
io/channel.c +0 -0
         if (len == QIO_CHANNEL_ERR_BLOCK) {
             if (qemu_in_coroutine()) {
                 qio_channel_yield(ioc, G_IO_OUT);
@@ -474,6 +487,31 @@ off_t qio_channel_io_seek(QIOChannel *ioc,
 }
 

io/channel.c off_t qio_channel_io_seek(QIOChannel *ioc, · line 498

Quoted 10 lines
io/channel.c +9 -0
 
+ssize_t qio_channel_writev_zerocopy(QIOChannel *ioc,
+                                    const struct iovec *iov,
+                                    size_t niov,
+                                    Error **errp)
+{
+    return qio_channel_writev_full_flags(ioc, iov, niov, NULL, 0,
+                                         QIO_CHANNEL_WRITE_FLAG_ZEROCOPY,
+                                         errp);
+}

This function is never used, right? qio_channel_writev_all_flags() is used in patch 3, with proper flags passed in. Then IMHO we can drop this one.

The rest looks good, as long as with Eric's comment addressed.

Thanks,

Quoted 22 lines
io/channel.c +16 -0
+
+
+int qio_channel_flush_zerocopy(QIOChannel *ioc,
+                               Error **errp)
+{
+    QIOChannelClass *klass = QIO_CHANNEL_GET_CLASS(ioc);
+
+    if (!klass->io_flush_zerocopy ||
+        !qio_channel_has_feature(ioc, QIO_CHANNEL_FEATURE_WRITE_ZEROCOPY)) {
+        return 0;
+    }
+
+    return klass->io_flush_zerocopy(ioc, errp);
+}
+
+
 static void qio_channel_restart_read(void *opaque)
 {
     QIOChannel *ioc = opaque;
Signature · 1 line
2.33.0
Signature · 1 line
Peter Xu
Signature · 1 line
Peter Xu
#8 lore raw

Re: [PATCH v4 1/3] QIOChannel: Add io_writev_zerocopy & io_flush_zerocopy callbacks

Hello Peter, sorry for the delay.

Quoted 18 lines · Peter Xu
Quoted 16 lines · Peter Xu
Quoted 15 lines · Leonardo Bras
io/channel.c +5 -5
-int qio_channel_writev_full_all(QIOChannel *ioc,
-                                const struct iovec *iov,
-                                size_t niov,
-                                int *fds, size_t nfds,
-                                Error **errp)
+int qio_channel_writev_full_all_flags(QIOChannel *ioc,
+                                      const struct iovec *iov,
+                                      size_t niov,
+                                      int *fds, size_t nfds,
+                                      int flags, Error **errp)
 {
     int ret = -1;
     struct iovec *local_iov = g_new(struct iovec, niov);
@@ -237,8 +250,8 @@ int qio_channel_writev_full_all(QIOChannel *ioc,
 
Quoted 3 lines
Quoted 3 lines

io/channel.c int qio_channel_writev_full_all(QIOChannel *ioc,

Quoted 3 lines
io/channel.c +0 -1
     while (nlocal_iov > 0) {
         ssize_t len;
-        len = qio_channel_writev_full(ioc, local_iov, nlocal_iov,

fds, nfds,

Quoted 2 lines
Quoted 2 lines

Review int qio_channel_writev_full_all(QIOChannel *ioc, · line 253

Quoted 2 lines
(quoted diff) +1 -1
-                                      errp);
+        len = qio_channel_writev_full_flags(ioc, local_iov,

nlocal_iov, fds, nfds,

Quoted 3 lines
Quoted 3 lines

io/channel.c int qio_channel_writev_full_all(QIOChannel *ioc, · line 254

Quoted 1 line
io/channel.c +1 -0
+                                          flags, errp);

IMHO we can keep qio_channel_writev_full() untouched, as it is the

wrapper for

Quoted 1 line
Quoted 1 line

io_writev() hook right now (and it allows taking fds). Then here

instead of

Quoted 1 line
Quoted 1 line

adding a new flags into it, we can introduce

qio_channel_writev_zerocopy_full()

Quoted 1 line
Quoted 1 line

and directly call here:

Sure, it makes sense.

Quoted 3 lines
Quoted 3 lines
           if (flags & ZEROCOPY) {
               assert(fds == NULL && nfds == 0);

Quick question: Why is this assert needed?

Quoted 6 lines
Quoted 6 lines
               qio_channel_writev_zerocopy_full(...[no fds passing in]);
           } else {
               qio_channel_writev_full(...[with all parameters]);
           }

Then qio_channel_writev_zerocopy_full() should be simplely the wrapper

for the

Quoted 3 lines
Quoted 1 line

new io_writev_zerocopy() hook.

Sorry I think the name doesn't need to have "_full" - that should be used

for

Quoted 2 lines

io_writev() when we need to pass in fds. zerocopy doesn't have that, so I think we can just call it qio_channel_writev_zerocopy() as it simply does

what

Quoted 1 line

writev() does.

Ok, I will make these changes to v5.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
io/channel.c +0 -0
         if (len == QIO_CHANNEL_ERR_BLOCK) {
             if (qemu_in_coroutine()) {
                 qio_channel_yield(ioc, G_IO_OUT);
@@ -474,6 +487,31 @@ off_t qio_channel_io_seek(QIOChannel *ioc,
 }
 
 
Quoted 7 lines
Quoted 7 lines

io/channel.c off_t qio_channel_io_seek(QIOChannel *ioc, · line 499

Quoted 7 lines
io/channel.c +7 -0
+ssize_t qio_channel_writev_zerocopy(QIOChannel *ioc,
+                                    const struct iovec *iov,
+                                    size_t niov,
+                                    Error **errp)
+{
+    return qio_channel_writev_full_flags(ioc, iov, niov, NULL, 0,
+

QIO_CHANNEL_WRITE_FLAG_ZEROCOPY,

Quoted 4 lines
Quoted 4 lines

Review off_t qio_channel_io_seek(QIOChannel *ioc, · line 498

Quoted 2 lines
(quoted diff) +2 -0
+                                         errp);
+}

This function is never used, right? qio_channel_writev_all_flags() is

used in

Quoted 5 lines
Quoted 5 lines

patch 3, with proper flags passed in. Then IMHO we can drop this one.

The rest looks good, as long as with Eric's comment addressed.

Thanks,

IIUC, this was addressed by your reply above, is that correct?

Quoted 10 lines
Quoted 10 lines

io/channel.c off_t qio_channel_io_seek(QIOChannel *ioc,

Quoted 10 lines
io/channel.c +9 -0
+
+
+int qio_channel_flush_zerocopy(QIOChannel *ioc,
+                               Error **errp)
+{
+    QIOChannelClass *klass = QIO_CHANNEL_GET_CLASS(ioc);
+
+    if (!klass->io_flush_zerocopy ||
+        !qio_channel_has_feature(ioc,

QIO_CHANNEL_FEATURE_WRITE_ZEROCOPY)) {

Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
io/channel.c +7 -0
+        return 0;
+    }
+
+    return klass->io_flush_zerocopy(ioc, errp);
+}
+
+
 static void qio_channel_restart_read(void *opaque)
Quoted 8 lines
Quoted 5 lines
io/channel.c +0 -1
 {
     QIOChannel *ioc = opaque;
--

2.33.0

--
Peter Xu

Quoted 3 lines

--
Peter Xu

Best regards,
Leonardo Bras

#9 lore raw

Re: [PATCH v4 1/3] QIOChannel: Add io_writev_zerocopy & io_flush_zerocopy callbacks

Quoted 5 lines · Leonardo Bras Soares Passos
Quoted 3 lines
Quoted 3 lines
           if (flags & ZEROCOPY) {
               assert(fds == NULL && nfds == 0);

Quick question: Why is this assert needed?

Not required I think; just want to make sure no one passes in the fds when using zero copy mode.

Thanks,

Signature · 1 line
Peter Xu
#10 lore raw

Re: [PATCH v4 1/3] QIOChannel: Add io_writev_zerocopy & io_flush_zerocopy callbacks

Quoted 10 lines · Peter Xu
Quoted 5 lines · Leonardo Bras Soares Passos
Quoted 3 lines
Quoted 3 lines
           if (flags & ZEROCOPY) {
               assert(fds == NULL && nfds == 0);

Quick question: Why is this assert needed?

Not required I think; just want to make sure no one passes in the fds when using zero copy mode.

Ok, that makes sense.
I will add it then, thanks!

Quoted 6 lines

Thanks,

--
Peter Xu

Best regards,
Leo

#11 lore raw

PATCH v4 2/3 QIOChannelSocket: Implement io_writev_zerocopy & io_flush_zerocopy for CONFIG_LINUX

In reply to Leonardo Bras

For CONFIG_LINUX, implement the new optional callbacks io_write_zerocopy and io_flush_zerocopy on QIOChannelSocket, but enables it only when MSG_ZEROCOPY feature is available in the host kernel, which is checked on qio_channel_socket_connect_sync()

qio_channel_socket_writev() contents were moved to a helper function qio_channel_socket_writev_flags() which accepts an extra argument for flags. (This argument is passed directly to sendmsg().

The above helper function is used to implement qio_channel_socket_writev(), with flags = 0, keeping it's behavior unchanged, and qio_channel_socket_writev_zerocopy() with flags = MSG_ZEROCOPY.

qio_channel_socket_flush_zerocopy() was implemented by counting how many times sendmsg(...,MSG_ZEROCOPY) was sucessfully called, and then reading the socket's error queue, in order to find how many of them finished sending. Flush will loop until those counters are the same, or until some error ocurs.

A new function qio_channel_socket_poll() was also created in order to avoid busy-looping recvmsg() in qio_channel_socket_flush_zerocopy() while waiting for updates in socket's error queue.

Notes on using writev_zerocopy():
1: Buffer
- As MSG_ZEROCOPY tells the kernel to use the same user buffer to avoid copying, some caution is necessary to avoid overwriting any buffer before it's sent. If something like this happen, a newer version of the buffer may be sent instead.
- If this is a problem, it's recommended to call flush_zerocopy() before freeing or re-using the buffer.

2: Locked memory
- When using MSG_ZERCOCOPY, the buffer memory will be locked after queued, and unlocked after it's sent.
- Depending on the size of each buffer, and how often it's sent, it may require a larger amount of locked memory than usually available to non-root user.
- If the required amount of locked memory is not available, writev_zerocopy will return an error, which can abort an operation like migration,
- Because of this, when an user code wants to add zerocopy as a feature, it requires a mechanism to disable it, so it can still be acessible to less privileged users.

---
 include/io/channel-socket.h |   2 +
 include/io/channel.h        |   1 +
 io/channel-socket.c         | 180 ++++++++++++++++++++++++++++++++++--
 3 files changed, 173 insertions(+), 10 deletions(-)
include/io/channel-socket.h +2 -0
index e747e63514..81d04baa4c 100644
@@ -47,6 +47,8 @@ struct QIOChannelSocket {
     socklen_t localAddrLen;
     struct sockaddr_storage remoteAddr;
     socklen_t remoteAddrLen;
+    ssize_t zerocopy_queued;
+    ssize_t zerocopy_sent;
 };
 
 
include/io/channel.h +1 -0
index e7d4e1521f..9d74629226 100644
@@ -31,6 +31,7 @@ OBJECT_DECLARE_TYPE(QIOChannel, QIOChannelClass,
 
 
 #define QIO_CHANNEL_ERR_BLOCK -2
+#define QIO_CHANNEL_ERR_NOBUFS -3
 
 #define QIO_CHANNEL_WRITE_FLAG_ZEROCOPY 0x1
 
io/channel-socket.c +170 -10

238 lines not on this page · raw message

Signature · 1 line
2.33.0
#12 lore raw

Re: [PATCH v4 2/3] QIOChannelSocket: Implement io_writev_zerocopy & io_flush_zerocopy for CONFIG_LINUX

Quoted 11 lines · Leonardo Bras

For CONFIG_LINUX, implement the new optional callbacks io_write_zerocopy and io_flush_zerocopy on QIOChannelSocket, but enables it only when MSG_ZEROCOPY feature is available in the host kernel, which is checked on qio_channel_socket_connect_sync()

qio_channel_socket_writev() contents were moved to a helper function qio_channel_socket_writev_flags() which accepts an extra argument for flags. (This argument is passed directly to sendmsg().

The above helper function is used to implement qio_channel_socket_writev(), with flags = 0, keeping it's behavior unchanged, and

its (remember, "it's" is shorthand for "it is", which does not fit here)

Quoted 6 lines

qio_channel_socket_writev_zerocopy() with flags = MSG_ZEROCOPY.

qio_channel_socket_flush_zerocopy() was implemented by counting how many times sendmsg(...,MSG_ZEROCOPY) was sucessfully called, and then reading the socket's error queue, in order to find how many of them finished sending. Flush will loop until those counters are the same, or until some error ocurs.

occurs

Quoted 31 lines

A new function qio_channel_socket_poll() was also created in order to avoid busy-looping recvmsg() in qio_channel_socket_flush_zerocopy() while waiting for updates in socket's error queue.

Notes on using writev_zerocopy():
1: Buffer
- As MSG_ZEROCOPY tells the kernel to use the same user buffer to avoid copying, some caution is necessary to avoid overwriting any buffer before it's sent. If something like this happen, a newer version of the buffer may be sent instead.
- If this is a problem, it's recommended to call flush_zerocopy() before freeing or re-using the buffer.

2: Locked memory
- When using MSG_ZERCOCOPY, the buffer memory will be locked after queued, and unlocked after it's sent.
- Depending on the size of each buffer, and how often it's sent, it may require a larger amount of locked memory than usually available to non-root user.
- If the required amount of locked memory is not available, writev_zerocopy will return an error, which can abort an operation like migration,
- Because of this, when an user code wants to add zerocopy as a feature, it requires a mechanism to disable it, so it can still be acessible to less privileged users.

---
 include/io/channel-socket.h |   2 +
 include/io/channel.h        |   1 +
 io/channel-socket.c         | 180 ++++++++++++++++++++++++++++++++++--
 3 files changed, 173 insertions(+), 10 deletions(-)

io/channel-socket.c static ssize_t qio_channel_socket_writev(QIOChannel *ioc, · line 809

Quoted 6 lines
io/channel-socket.c +5 -0
+static int qio_channel_socket_flush_zerocopy(QIOChannel *ioc,
+                                             Error **errp)
+{
 
+
+        /* No errors, count sucessfully finished sendmsg()*/

Space before */

Quoted 11 lines
io/channel-socket.c +7 -0
+        sioc->zerocopy_sent += serr->ee_data - serr->ee_info + 1;
+    }
+    return 0;
+}
+
+#endif /* CONFIG_LINUX */
+
 static int
 qio_channel_socket_set_blocking(QIOChannel *ioc,
                                 bool enabled,

io/channel-socket.c static void qio_channel_socket_class_init(ObjectClass *klass, · line 950

Quoted 8 lines
io/channel-socket.c +4 -0
@@ -787,6 +943,10 @@ static void qio_channel_socket_class_init(ObjectClass *klass,
     ioc_klass->io_set_delay = qio_channel_socket_set_delay;
     ioc_klass->io_create_watch = qio_channel_socket_create_watch;
     ioc_klass->io_set_aio_fd_handler = qio_channel_socket_set_aio_fd_handler;
+#ifdef CONFIG_LINUX
+    ioc_klass->io_writev_zerocopy = qio_channel_socket_writev_zerocopy;
+    ioc_klass->io_flush_zerocopy = qio_channel_socket_flush_zerocopy;
+#endif
 }

I did a high-level look at the code, rather than an in-depth review of whether zero-copy was being used correctly.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#13 lore raw

Re: [PATCH v4 2/3] QIOChannelSocket: Implement io_writev_zerocopy & io_flush_zerocopy for CONFIG_LINUX

Hello Eric,

Quoted 16 lines · Eric Blake
Quoted 11 lines · Leonardo Bras

For CONFIG_LINUX, implement the new optional callbacks io_write_zerocopy and io_flush_zerocopy on QIOChannelSocket, but enables it only when MSG_ZEROCOPY feature is available in the host kernel, which is checked on qio_channel_socket_connect_sync()

qio_channel_socket_writev() contents were moved to a helper function qio_channel_socket_writev_flags() which accepts an extra argument for flags. (This argument is passed directly to sendmsg().

The above helper function is used to implement qio_channel_socket_writev(), with flags = 0, keeping it's behavior unchanged, and

its (remember, "it's" is shorthand for "it is", which does not fit here)

Quoted 8 lines
Quoted 6 lines

qio_channel_socket_writev_zerocopy() with flags = MSG_ZEROCOPY.

qio_channel_socket_flush_zerocopy() was implemented by counting how many times sendmsg(...,MSG_ZEROCOPY) was sucessfully called, and then reading the socket's error queue, in order to find how many of them finished sending. Flush will loop until those counters are the same, or until some error ocurs.

occurs

Thanks!
(I will check if my codespell is enabled in this setup)

Quoted 32 lines
Quoted 32 lines

A new function qio_channel_socket_poll() was also created in order to avoid busy-looping recvmsg() in qio_channel_socket_flush_zerocopy() while waiting for updates in socket's error queue.

Notes on using writev_zerocopy():
1: Buffer
- As MSG_ZEROCOPY tells the kernel to use the same user buffer to avoid copying, some caution is necessary to avoid overwriting any buffer before it's sent. If something like this happen, a newer version of the buffer may be sent instead.
- If this is a problem, it's recommended to call flush_zerocopy() before freeing or re-using the buffer.

2: Locked memory
- When using MSG_ZERCOCOPY, the buffer memory will be locked after queued, and unlocked after it's sent.
- Depending on the size of each buffer, and how often it's sent, it may require a larger amount of locked memory than usually available to non-root user.
- If the required amount of locked memory is not available, writev_zerocopy will return an error, which can abort an operation like migration,
- Because of this, when an user code wants to add zerocopy as a feature, it requires a mechanism to disable it, so it can still be acessible to less privileged users.

---
 include/io/channel-socket.h |   2 +
 include/io/channel.h        |   1 +
 io/channel-socket.c         | 180 ++++++++++++++++++++++++++++++++++--
 3 files changed, 173 insertions(+), 10 deletions(-)
Quoted 8 lines

io/channel-socket.c static ssize_t qio_channel_socket_writev(QIOChannel *ioc, · line 809

Quoted 6 lines
io/channel-socket.c +5 -0
+static int qio_channel_socket_flush_zerocopy(QIOChannel *ioc,
+                                             Error **errp)
+{
 
+
+        /* No errors, count sucessfully finished sendmsg()*/

Space before */

Thanks!
Also, typo on 'successfully'.

Quoted 15 lines
Quoted 15 lines
io/channel-socket.c +7 -0
+        sioc->zerocopy_sent += serr->ee_data - serr->ee_info + 1;
+    }
+    return 0;
+}
+
+#endif /* CONFIG_LINUX */
+
 static int
 qio_channel_socket_set_blocking(QIOChannel *ioc,
                                 bool enabled,
@@ -787,6 +943,10 @@ static void qio_channel_socket_class_init(ObjectClass *klass,
     ioc_klass->io_set_delay = qio_channel_socket_set_delay;
     ioc_klass->io_create_watch = qio_channel_socket_create_watch;
     ioc_klass->io_set_aio_fd_handler = qio_channel_socket_set_aio_fd_handler;
Quoted 8 lines

io/channel-socket.c static void qio_channel_socket_class_init(ObjectClass *klass, · line 950

Quoted 5 lines
io/channel-socket.c +4 -0
+#ifdef CONFIG_LINUX
+    ioc_klass->io_writev_zerocopy = qio_channel_socket_writev_zerocopy;
+    ioc_klass->io_flush_zerocopy = qio_channel_socket_flush_zerocopy;
+#endif
 }

I did a high-level look at the code, rather than an in-depth review of whether zero-copy was being used correctly.

It's so far been very helpful. Thanks!

Best regards,
Leo

#14 lore raw

Re: [PATCH v4 2/3] QIOChannelSocket: Implement io_writev_zerocopy & io_flush_zerocopy for CONFIG_LINUX

In reply to Leonardo Bras

io/channel-socket.c int qio_channel_socket_connect_sync(QIOChannelSocket *ioc, · line 172

Quoted 13 lines · Leonardo Bras
io/channel-socket.c +9 -0
@@ -154,6 +161,17 @@ int qio_channel_socket_connect_sync(QIOChannelSocket *ioc,
         return -1;
     }
 
+#ifdef CONFIG_LINUX
+    ret = qemu_setsockopt(fd, SOL_SOCKET, SO_ZEROCOPY, &v, sizeof(v));
+    if (ret < 0) {
+        /* Zerocopy not available on host */
+        return 0;
+    }
+
+    qio_channel_set_feature(QIO_CHANNEL(ioc),
+                            QIO_CHANNEL_FEATURE_WRITE_ZEROCOPY);

This is okay I think, but looks a bit weird. Maybe nicer to be written as:

#if LINUX
      ret = setsockopt();
      if (ret == 0) {
          qio_channel_set_feature(...);
      }
#endif
      return 0;

?

Quoted 4 lines
io/channel-socket.c +2 -0
+#endif
+
     return 0;
 }

[...]

io/channel-socket.c static ssize_t qio_channel_socket_writev(QIOChannel *ioc, · line 744

Quoted 12 lines
io/channel-socket.c +12 -0
+static ssize_t qio_channel_socket_writev_zerocopy(QIOChannel *ioc,
+                                                  const struct iovec *iov,
+                                                  size_t niov,
+                                                  Error **errp)
+{
+    QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
+    ssize_t ret;
+
+    ret = qio_channel_socket_writev_flags(ioc, iov, niov, NULL, 0,
+                                          MSG_ZEROCOPY, errp);
+    if (ret == QIO_CHANNEL_ERR_NOBUFS) {
+        if (errp && *errp) {

Hmm this seems wrong, *errp should be NULL in most cases, meanwhile I think error_setg*() takes care of errp==NULL too, so maybe we can drop this?

Quoted 9 lines
io/channel-socket.c +9 -0
+            error_setg_errno(errp, errno,
+                             "Process can't lock enough memory for using MSG_ZEROCOPY");
+        }
+        return -1;
+    }
+
+    sioc->zerocopy_queued++;
+    return ret;
+}
Signature · 1 line
Peter Xu
#15 lore raw

Re: [PATCH v4 2/3] QIOChannelSocket: Implement io_writev_zerocopy & io_flush_zerocopy for CONFIG_LINUX

Quoted 26 lines · Peter Xu

io/channel-socket.c int qio_channel_socket_connect_sync(QIOChannelSocket *ioc, · line 172

Quoted 13 lines · Leonardo Bras
io/channel-socket.c +9 -0
@@ -154,6 +161,17 @@ int qio_channel_socket_connect_sync(QIOChannelSocket *ioc,
         return -1;
     }
 
+#ifdef CONFIG_LINUX
+    ret = qemu_setsockopt(fd, SOL_SOCKET, SO_ZEROCOPY, &v, sizeof(v));
+    if (ret < 0) {
+        /* Zerocopy not available on host */
+        return 0;
+    }
+
+    qio_channel_set_feature(QIO_CHANNEL(ioc),
+                            QIO_CHANNEL_FEATURE_WRITE_ZEROCOPY);

This is okay I think, but looks a bit weird. Maybe nicer to be written as:

#if LINUX
      ret = setsockopt();
      if (ret == 0) {
          qio_channel_set_feature(...);
      }
#endif
      return 0;
Quoted 1 line

?

Yeah, I also questioned myself about this one.
At the time I ended up writing like this because the lines above used the behavior "if error, then exit/abort", and so I thought that this would be the better way to include this feature.
But I did not consider that this is not an error exit, but a 'maybe feature instead'.

So, I will change that like you suggested.

Quoted 8 lines
Quoted 5 lines
io/channel-socket.c +2 -0
+#endif
+
     return 0;
 }

[...]

Quoted 15 lines

io/channel-socket.c static ssize_t qio_channel_socket_writev(QIOChannel *ioc, · line 744

Quoted 12 lines
io/channel-socket.c +12 -0
+static ssize_t qio_channel_socket_writev_zerocopy(QIOChannel *ioc,
+                                                  const struct iovec *iov,
+                                                  size_t niov,
+                                                  Error **errp)
+{
+    QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
+    ssize_t ret;
+
+    ret = qio_channel_socket_writev_flags(ioc, iov, niov, NULL, 0,
+                                          MSG_ZEROCOPY, errp);
+    if (ret == QIO_CHANNEL_ERR_NOBUFS) {
+        if (errp && *errp) {

Hmm this seems wrong, *errp should be NULL in most cases, meanwhile I think error_setg*() takes care of errp==NULL too, so maybe we can drop this?

Yeah, you are correct.
I ended up confused about how to use err, thanks for making it more clear!

Quoted 14 lines

io/channel-socket.c static ssize_t qio_channel_socket_writev(QIOChannel *ioc, · line 753

Quoted 10 lines
io/channel-socket.c +9 -0
+            error_setg_errno(errp, errno,
+                             "Process can't lock enough memory for using MSG_ZEROCOPY");
+        }
+        return -1;
+    }
+
+    sioc->zerocopy_queued++;
+    return ret;
+}

--
Peter Xu

Best regards,
Leonardo Bras

#16 lore raw

Re: [PATCH v4 2/3] QIOChannelSocket: Implement io_writev_zerocopy & io_flush_zerocopy for CONFIG_LINUX

In reply to Leonardo Bras
Quoted 41 lines · Leonardo Bras

For CONFIG_LINUX, implement the new optional callbacks io_write_zerocopy and io_flush_zerocopy on QIOChannelSocket, but enables it only when MSG_ZEROCOPY feature is available in the host kernel, which is checked on qio_channel_socket_connect_sync()

qio_channel_socket_writev() contents were moved to a helper function qio_channel_socket_writev_flags() which accepts an extra argument for flags. (This argument is passed directly to sendmsg().

The above helper function is used to implement qio_channel_socket_writev(), with flags = 0, keeping it's behavior unchanged, and qio_channel_socket_writev_zerocopy() with flags = MSG_ZEROCOPY.

qio_channel_socket_flush_zerocopy() was implemented by counting how many times sendmsg(...,MSG_ZEROCOPY) was sucessfully called, and then reading the socket's error queue, in order to find how many of them finished sending. Flush will loop until those counters are the same, or until some error ocurs.

A new function qio_channel_socket_poll() was also created in order to avoid busy-looping recvmsg() in qio_channel_socket_flush_zerocopy() while waiting for updates in socket's error queue.

Notes on using writev_zerocopy():
1: Buffer
- As MSG_ZEROCOPY tells the kernel to use the same user buffer to avoid copying, some caution is necessary to avoid overwriting any buffer before it's sent. If something like this happen, a newer version of the buffer may be sent instead.
- If this is a problem, it's recommended to call flush_zerocopy() before freeing or re-using the buffer.

2: Locked memory
- When using MSG_ZERCOCOPY, the buffer memory will be locked after queued, and unlocked after it's sent.
- Depending on the size of each buffer, and how often it's sent, it may require a larger amount of locked memory than usually available to non-root user.
- If the required amount of locked memory is not available, writev_zerocopy will return an error, which can abort an operation like migration,
- Because of this, when an user code wants to add zerocopy as a feature, it requires a mechanism to disable it, so it can still be acessible to less privileged users.

Quoted 1 line

I think this patch would be easier to review if you split in:
- add the flags parameter left and right
- add the meat of what you do with the flags.

include/io/channel-socket.h struct QIOChannelSocket { · line 51

Quoted 7 lines

+++ b/include/io/channel-socket.h

include/io/channel-socket.h +2 -0
@@ -47,6 +47,8 @@ struct QIOChannelSocket {
     socklen_t localAddrLen;
     struct sockaddr_storage remoteAddr;
     socklen_t remoteAddrLen;
+    ssize_t zerocopy_queued;
+    ssize_t zerocopy_sent;

I am not sure if this is good/bad to put it inside

#ifdef CONFIG_LINUX

basically everything else uses it.

io/channel-socket.c int qio_channel_socket_connect_sync(QIOChannelSocket *ioc, · line 172

Quoted 9 lines
io/channel-socket.c +9 -0
+#ifdef CONFIG_LINUX
+    ret = qemu_setsockopt(fd, SOL_SOCKET, SO_ZEROCOPY, &v, sizeof(v));
+    if (ret < 0) {
+        /* Zerocopy not available on host */
+        return 0;
+    }
+
+    qio_channel_set_feature(QIO_CHANNEL(ioc),
+                            QIO_CHANNEL_FEATURE_WRITE_ZEROCOPY);

As Peter said, you shouldn't fail if the feature is not there.

But on the other hand, on patch 3, you don't check that this feature exist when you allow to enable multifd_zerocopy.

io/channel-socket.c int qio_channel_socket_connect_sync(QIOChannelSocket *ioc,

Quoted 8 lines
io/channel-socket.c +2 -0
+#endif
+
     return 0;
 }
 
 
         error_setg_errno(errp, errno,
                          "Unable to write to socket");

Why do you split this in two lines?

Yes, I know that this file is not consistent either on how the do with this, sometimes one line, otherwise more.

I don't know how ZEROCPY works at kernel level to comment on rest of the patch.

Later, Juan.

#17 lore raw

Re: [PATCH v4 2/3] QIOChannelSocket: Implement io_writev_zerocopy & io_flush_zerocopy for CONFIG_LINUX

Quoted 43 lines · Juan Quintela
Quoted 41 lines · Leonardo Bras

For CONFIG_LINUX, implement the new optional callbacks io_write_zerocopy and io_flush_zerocopy on QIOChannelSocket, but enables it only when MSG_ZEROCOPY feature is available in the host kernel, which is checked on qio_channel_socket_connect_sync()

qio_channel_socket_writev() contents were moved to a helper function qio_channel_socket_writev_flags() which accepts an extra argument for flags. (This argument is passed directly to sendmsg().

The above helper function is used to implement qio_channel_socket_writev(), with flags = 0, keeping it's behavior unchanged, and qio_channel_socket_writev_zerocopy() with flags = MSG_ZEROCOPY.

qio_channel_socket_flush_zerocopy() was implemented by counting how many times sendmsg(...,MSG_ZEROCOPY) was sucessfully called, and then reading the socket's error queue, in order to find how many of them finished sending. Flush will loop until those counters are the same, or until some error ocurs.

A new function qio_channel_socket_poll() was also created in order to avoid busy-looping recvmsg() in qio_channel_socket_flush_zerocopy() while waiting for updates in socket's error queue.

Notes on using writev_zerocopy():
1: Buffer
- As MSG_ZEROCOPY tells the kernel to use the same user buffer to avoid copying, some caution is necessary to avoid overwriting any buffer before it's sent. If something like this happen, a newer version of the buffer may be sent instead.
- If this is a problem, it's recommended to call flush_zerocopy() before freeing or re-using the buffer.

2: Locked memory
- When using MSG_ZERCOCOPY, the buffer memory will be locked after queued, and unlocked after it's sent.
- Depending on the size of each buffer, and how often it's sent, it may require a larger amount of locked memory than usually available to non-root user.
- If the required amount of locked memory is not available, writev_zerocopy will return an error, which can abort an operation like migration,
- Because of this, when an user code wants to add zerocopy as a feature, it requires a mechanism to disable it, so it can still be acessible to less privileged users.

Quoted 5 lines
Quoted 1 line

I think this patch would be easier to review if you split in:
- add the flags parameter left and right
- add the meat of what you do with the flags.

ok, I will try to split it like this.

Quoted 11 lines

include/io/channel-socket.h struct QIOChannelSocket { · line 51

Quoted 8 lines

+++ b/include/io/channel-socket.h

include/io/channel-socket.h +2 -0
@@ -47,6 +47,8 @@ struct QIOChannelSocket {
     socklen_t localAddrLen;
     struct sockaddr_storage remoteAddr;
     socklen_t remoteAddrLen;
+    ssize_t zerocopy_queued;
+    ssize_t zerocopy_sent;

I am not sure if this is good/bad to put it inside

Quoted 3 lines

#ifdef CONFIG_LINUX

basically everything else uses it.

I think it makes sense that zerocopy_{sent,queued} is inside CONFIG_LINUX as no one else is using zerocopy.

Quoted 13 lines

io/channel-socket.c int qio_channel_socket_connect_sync(QIOChannelSocket *ioc, · line 172

Quoted 10 lines
io/channel-socket.c +9 -0
+#ifdef CONFIG_LINUX
+    ret = qemu_setsockopt(fd, SOL_SOCKET, SO_ZEROCOPY, &v, sizeof(v));
+    if (ret < 0) {
+        /* Zerocopy not available on host */
+        return 0;
+    }
+
+    qio_channel_set_feature(QIO_CHANNEL(ioc),
+                            QIO_CHANNEL_FEATURE_WRITE_ZEROCOPY);

As Peter said, you shouldn't fail if the feature is not there.

Quoted 2 lines

But on the other hand, on patch 3, you don't check that this feature exist when you allow to enable multifd_zerocopy.

This had a major rework on v5, but I will make sure this suggestion is addressed before releasing it.

Quoted 7 lines
Quoted 5 lines
io/channel-socket.c +2 -0
+#endif
+
     return 0;
 }
Quoted 7 lines
Quoted 2 lines
         error_setg_errno(errp, errno,
                          "Unable to write to socket");

Why do you split this in two lines?

Yes, I know that this file is not consistent either on how the do with this, sometimes one line, otherwise more.

IIUC, this lines have no '+' in them, so they are not my addition.

Quoted 5 lines

I don't know how ZEROCPY works at kernel level to comment on rest of the patch.

Later, Juan.

Thanks for reviewing Juan.

Best regards,
Leo

#18 lore raw

PATCH v4 3/3 multifd: Implement zerocopy write in multifd migration (multifd-zerocopy)

In reply to Leonardo Bras

Implement zerocopy on nocomp_send_write(), by making use of QIOChannel zerocopy interface.

Change multifd_send_sync_main() so it can distinguish the last sync from the setup and per-iteration ones, so a flush_zerocopy() can be called at the last sync in order to make sure all RAM is sent before finishing the migration.

Also make it return -1 if flush_zerocopy() fails, in order to cancel the migration process, and avoid resuming the guest in the target host without receiving all current RAM.

This will work fine on RAM migration because the RAM pages are not usually freed, and there is no problem on changing the pages content between async_send() and the actual sending of the buffer, because this change will dirty the page and cause it to be re-sent on a next iteration anyway.

Given a lot of locked memory may be needed in order to use multid migration with zerocopy enabled, make it optional by creating a new parameter multifd-zerocopy on qapi, so low-privileged users can still perform multifd migrations.

---
 qapi/migration.json   | 18 ++++++++++++++++++
 migration/migration.h |  1 +
 migration/multifd.h   |  2 +-
 migration/migration.c | 20 ++++++++++++++++++++
 migration/multifd.c   | 33 ++++++++++++++++++++++++++++-----
 migration/ram.c       | 20 +++++++++++++-------
 monitor/hmp-cmds.c    |  4 ++++
 7 files changed, 85 insertions(+), 13 deletions(-)
qapi/migration.json +18 -0
index 88f07baedd..c4890cbb54 100644
@@ -724,6 +724,11 @@
 #                      will consume more CPU.
 #                      Defaults to 1. (Since 5.0)
 #
+# @multifd-zerocopy: Controls behavior on sending memory pages on multifd migration.
+#                    When true, enables a zerocopy mechanism for sending memory
+#                    pages, if host does support it.
+#                    Defaults to false. (Since 6.2)
+#
 # @block-bitmap-mapping: Maps block nodes and bitmaps on them to
 #                        aliases for the purpose of dirty bitmap migration.  Such
 #                        aliases may for example be the corresponding names on the
@@ -758,6 +763,7 @@
            'xbzrle-cache-size', 'max-postcopy-bandwidth',
            'max-cpu-throttle', 'multifd-compression',
            'multifd-zlib-level' ,'multifd-zstd-level',
+	   'multifd-zerocopy',
            'block-bitmap-mapping' ] }
 
 ##
@@ -884,6 +890,11 @@
 #                      will consume more CPU.
 #                      Defaults to 1. (Since 5.0)
 #
+# @multifd-zerocopy: Controls behavior on sending memory pages on multifd migration.
+#                    When true, enables a zerocopy mechanism for sending memory
+#                    pages, if host does support it.
+#                    Defaults to false. (Since 6.2)
+#
 # @block-bitmap-mapping: Maps block nodes and bitmaps on them to
 #                        aliases for the purpose of dirty bitmap migration.  Such
 #                        aliases may for example be the corresponding names on the
@@ -934,6 +945,7 @@
             '*multifd-compression': 'MultiFDCompression',
             '*multifd-zlib-level': 'uint8',
             '*multifd-zstd-level': 'uint8',
+	    '*multifd-zerocopy': 'bool',
             '*block-bitmap-mapping': [ 'BitmapMigrationNodeAlias' ] } }
 
 ##
@@ -1080,6 +1092,11 @@
 #                      will consume more CPU.
 #                      Defaults to 1. (Since 5.0)
 #
+# @multifd-zerocopy: Controls behavior on sending memory pages on multifd migration.
+#                    When true, enables a zerocopy mechanism for sending memory
+#                    pages, if host does support it.
+#                    Defaults to false. (Since 6.2)
+#
 # @block-bitmap-mapping: Maps block nodes and bitmaps on them to
 #                        aliases for the purpose of dirty bitmap migration.  Such
 #                        aliases may for example be the corresponding names on the
@@ -1128,6 +1145,7 @@
             '*multifd-compression': 'MultiFDCompression',
             '*multifd-zlib-level': 'uint8',
             '*multifd-zstd-level': 'uint8',
+	    '*multifd-zerocopy': 'bool',
             '*block-bitmap-mapping': [ 'BitmapMigrationNodeAlias' ] } }
 
 ##
migration/migration.h +1 -0
index 7a5aa8c2fd..860d83cc41 100644
@@ -338,6 +338,7 @@ int migrate_multifd_channels(void);
 MultiFDCompression migrate_multifd_compression(void);
 int migrate_multifd_zlib_level(void);
 int migrate_multifd_zstd_level(void);
+int migrate_multifd_zerocopy(void);
 
 int migrate_use_xbzrle(void);
 uint64_t migrate_xbzrle_cache_size(void);
migration/multifd.h +1 -1
index 8d6751f5ed..8f5c5a6953 100644
@@ -20,7 +20,7 @@ int multifd_load_cleanup(Error **errp);
 bool multifd_recv_all_channels_created(void);
 bool multifd_recv_new_channel(QIOChannel *ioc, Error **errp);
 void multifd_recv_sync_main(void);
-void multifd_send_sync_main(QEMUFile *f);
+int multifd_send_sync_main(QEMUFile *f, bool last_sync);
 int multifd_queue_page(QEMUFile *f, RAMBlock *block, ram_addr_t offset);
 
 /* Multifd Compression flags */
migration/migration.c +20 -0
index 6ac807ef3d..326f7c515f 100644
@@ -879,6 +879,8 @@ MigrationParameters *qmp_query_migrate_parameters(Error **errp)
     params->multifd_zlib_level = s->parameters.multifd_zlib_level;
     params->has_multifd_zstd_level = true;
     params->multifd_zstd_level = s->parameters.multifd_zstd_level;
+    params->has_multifd_zerocopy = true;
+    params->multifd_zerocopy = s->parameters.multifd_zerocopy;
     params->has_xbzrle_cache_size = true;
     params->xbzrle_cache_size = s->parameters.xbzrle_cache_size;
     params->has_max_postcopy_bandwidth = true;
@@ -1523,6 +1525,9 @@ static void migrate_params_test_apply(MigrateSetParameters *params,
     if (params->has_multifd_compression) {
         dest->multifd_compression = params->multifd_compression;
     }
+    if (params->has_multifd_zerocopy) {
+        dest->multifd_zerocopy = params->multifd_zerocopy;
+    }
     if (params->has_xbzrle_cache_size) {
         dest->xbzrle_cache_size = params->xbzrle_cache_size;
     }
@@ -1635,6 +1640,9 @@ static void migrate_params_apply(MigrateSetParameters *params, Error **errp)
     if (params->has_multifd_compression) {
         s->parameters.multifd_compression = params->multifd_compression;
     }
+    if (params->has_multifd_zerocopy) {
+        s->parameters.multifd_zerocopy = params->multifd_zerocopy;
+    }
     if (params->has_xbzrle_cache_size) {
         s->parameters.xbzrle_cache_size = params->xbzrle_cache_size;
         xbzrle_cache_resize(params->xbzrle_cache_size, errp);
@@ -2516,6 +2524,15 @@ int migrate_multifd_zstd_level(void)
     return s->parameters.multifd_zstd_level;
 }
 
+int migrate_multifd_zerocopy(void)
+{
+    MigrationState *s;
+
+    s = migrate_get_current();
+
+    return s->parameters.multifd_zerocopy;
+}
+
 int migrate_use_xbzrle(void)
 {
     MigrationState *s;
@@ -4164,6 +4181,8 @@ static Property migration_properties[] = {
     DEFINE_PROP_UINT8("multifd-zstd-level", MigrationState,
                       parameters.multifd_zstd_level,
                       DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL),
+    DEFINE_PROP_BOOL("multifd-zerocopy", MigrationState,
+                      parameters.multifd_zerocopy, false),
     DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState,
                       parameters.xbzrle_cache_size,
                       DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE),
@@ -4261,6 +4280,7 @@ static void migration_instance_init(Object *obj)
     params->has_multifd_compression = true;
     params->has_multifd_zlib_level = true;
     params->has_multifd_zstd_level = true;
+    params->has_multifd_zerocopy = true;
     params->has_xbzrle_cache_size = true;
     params->has_max_postcopy_bandwidth = true;
     params->has_max_cpu_throttle = true;
migration/multifd.c +28 -5
index 377da78f5b..17a7d90de3 100644
@@ -105,7 +105,13 @@ static int nocomp_send_prepare(MultiFDSendParams *p, uint32_t used,
  */
 static int nocomp_send_write(MultiFDSendParams *p, uint32_t used, Error **errp)
 {
-    return qio_channel_writev_all(p->c, p->pages->iov, used, errp);
+    int flags = 0;
+
+    if (migrate_multifd_zerocopy()) {
+        flags = QIO_CHANNEL_WRITE_FLAG_ZEROCOPY;
+    }
+
+    return qio_channel_writev_all_flags(p->c, p->pages->iov, used, flags, errp);
 }
 
 /**
@@ -575,19 +581,23 @@ void multifd_save_cleanup(void)
     multifd_send_state = NULL;
 }
 
-void multifd_send_sync_main(QEMUFile *f)
+int multifd_send_sync_main(QEMUFile *f, bool last_sync)
 {
     int i;
+    bool flush_zerocopy;
 
     if (!migrate_use_multifd()) {
-        return;
+        return 0;
     }
     if (multifd_send_state->pages->used) {
         if (multifd_send_pages(f) < 0) {
             error_report("%s: multifd_send_pages fail", __func__);
-            return;
+            return 0;
         }
     }
+
+    flush_zerocopy = last_sync && migrate_multifd_zerocopy();
+
     for (i = 0; i < migrate_multifd_channels(); i++) {
         MultiFDSendParams *p = &multifd_send_state->params[i];
 
@@ -598,7 +608,7 @@ void multifd_send_sync_main(QEMUFile *f)
         if (p->quit) {
             error_report("%s: channel %d has already quit", __func__, i);
             qemu_mutex_unlock(&p->mutex);
-            return;
+            return 0;
         }
 
         p->packet_num = multifd_send_state->packet_num++;
@@ -609,6 +619,17 @@ void multifd_send_sync_main(QEMUFile *f)
         ram_counters.transferred += p->packet_len;
         qemu_mutex_unlock(&p->mutex);
         qemu_sem_post(&p->sem);
+
+        if (flush_zerocopy) {
+            int ret;
+            Error *err = NULL;
+
+            ret = qio_channel_flush_zerocopy(p->c, &err);
+            if (ret < 0) {
+                error_report_err(err);
+                return -1;
+            }
+        }
     }
     for (i = 0; i < migrate_multifd_channels(); i++) {
         MultiFDSendParams *p = &multifd_send_state->params[i];
@@ -617,6 +638,8 @@ void multifd_send_sync_main(QEMUFile *f)
         qemu_sem_wait(&p->sem_sync);
     }
     trace_multifd_send_sync_main(multifd_send_state->packet_num);
+
+    return 0;
 }
 
 static void *multifd_send_thread(void *opaque)
migration/ram.c +13 -7
index 7a43bfd7af..ada57846a5 100644
@@ -2839,7 +2839,7 @@ static int ram_save_setup(QEMUFile *f, void *opaque)
     ram_control_before_iterate(f, RAM_CONTROL_SETUP);
     ram_control_after_iterate(f, RAM_CONTROL_SETUP);
 
-    multifd_send_sync_main(f);
+    multifd_send_sync_main(f, false);
     qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
     qemu_fflush(f);
 
@@ -2948,7 +2948,7 @@ static int ram_save_iterate(QEMUFile *f, void *opaque)
 out:
     if (ret >= 0
         && migration_is_setup_or_active(migrate_get_current()->state)) {
-        multifd_send_sync_main(rs->f);
+        multifd_send_sync_main(rs->f, false);
         qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
         qemu_fflush(f);
         ram_counters.transferred += 8;
@@ -3006,13 +3006,19 @@ static int ram_save_complete(QEMUFile *f, void *opaque)
         ram_control_after_iterate(f, RAM_CONTROL_FINISH);
     }
 
-    if (ret >= 0) {
-        multifd_send_sync_main(rs->f);
-        qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
-        qemu_fflush(f);
+    if (ret < 0) {
+        return ret;
     }
 
-    return ret;
+    ret = multifd_send_sync_main(rs->f, true);
+    if (ret < 0) {
+        return -1;
+    }
+
+    qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
+    qemu_fflush(f);
+
+    return 0;
 }
 
 static void ram_save_pending(QEMUFile *f, void *opaque, uint64_t max_size,
monitor/hmp-cmds.c +4 -0
index bcaa41350e..b04f14ec1e 100644
@@ -1364,6 +1364,10 @@ void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
         p->has_multifd_zstd_level = true;
         visit_type_uint8(v, param, &p->multifd_zstd_level, &err);
         break;
+    case MIGRATION_PARAMETER_MULTIFD_ZEROCOPY:
+        p->has_multifd_zerocopy = true;
+        visit_type_bool(v, param, &p->multifd_zerocopy, &err);
+        break;
     case MIGRATION_PARAMETER_XBZRLE_CACHE_SIZE:
         p->has_xbzrle_cache_size = true;
         if (!visit_type_size(v, param, &cache_size, &err)) {
Signature · 1 line
2.33.0
#19 lore raw

Re: [PATCH v4 3/3] multifd: Implement zerocopy write in multifd migration (multifd-zerocopy)

Quoted 38 lines · Leonardo Bras

Implement zerocopy on nocomp_send_write(), by making use of QIOChannel zerocopy interface.

Change multifd_send_sync_main() so it can distinguish the last sync from the setup and per-iteration ones, so a flush_zerocopy() can be called at the last sync in order to make sure all RAM is sent before finishing the migration.

Also make it return -1 if flush_zerocopy() fails, in order to cancel the migration process, and avoid resuming the guest in the target host without receiving all current RAM.

This will work fine on RAM migration because the RAM pages are not usually freed, and there is no problem on changing the pages content between async_send() and the actual sending of the buffer, because this change will dirty the page and cause it to be re-sent on a next iteration anyway.

Given a lot of locked memory may be needed in order to use multid migration with zerocopy enabled, make it optional by creating a new parameter multifd-zerocopy on qapi, so low-privileged users can still perform multifd migrations.

---
 qapi/migration.json   | 18 ++++++++++++++++++
 migration/migration.h |  1 +
 migration/multifd.h   |  2 +-
 migration/migration.c | 20 ++++++++++++++++++++
 migration/multifd.c   | 33 ++++++++++++++++++++++++++++-----
 migration/ram.c       | 20 +++++++++++++-------
 monitor/hmp-cmds.c    |  4 ++++
 7 files changed, 85 insertions(+), 13 deletions(-)
qapi/migration.json +0 -0
index 88f07baedd..c4890cbb54 100644

qapi/migration.json line 729

Quoted 6 lines
qapi/migration.json +3 -0
@@ -724,6 +724,11 @@
 #                      will consume more CPU.
 #                      Defaults to 1. (Since 5.0)
 #
+# @multifd-zerocopy: Controls behavior on sending memory pages on multifd migration.
+#                    When true, enables a zerocopy mechanism for sending memory
+#                    pages, if host does support it.

s/does support/supports/ (several instances this patch)

qapi/migration.json line 767

Quoted 11 lines
qapi/migration.json +3 -0
+#                    Defaults to false. (Since 6.2)
+#
 # @block-bitmap-mapping: Maps block nodes and bitmaps on them to
 #                        aliases for the purpose of dirty bitmap migration.  Such
 #                        aliases may for example be the corresponding names on the
@@ -758,6 +763,7 @@
            'xbzrle-cache-size', 'max-postcopy-bandwidth',
            'max-cpu-throttle', 'multifd-compression',
            'multifd-zlib-level' ,'multifd-zstd-level',
+	   'multifd-zerocopy',
            'block-bitmap-mapping' ] }

Should this feature be guarded with 'if':'CONFIG_LINUX', since that's the only platform where you even compile the code (even then, it can still fail if the kernel doesn't support it).

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#20 lore raw

Re: [PATCH v4 3/3] multifd: Implement zerocopy write in multifd migration (multifd-zerocopy)

Hello Eric,

Quoted 35 lines · Eric Blake
Quoted 33 lines · Leonardo Bras

Implement zerocopy on nocomp_send_write(), by making use of QIOChannel zerocopy interface.

Change multifd_send_sync_main() so it can distinguish the last sync from the setup and per-iteration ones, so a flush_zerocopy() can be called at the last sync in order to make sure all RAM is sent before finishing the migration.

Also make it return -1 if flush_zerocopy() fails, in order to cancel the migration process, and avoid resuming the guest in the target host without receiving all current RAM.

This will work fine on RAM migration because the RAM pages are not usually freed, and there is no problem on changing the pages content between async_send() and the actual sending of the buffer, because this change will dirty the page and cause it to be re-sent on a next iteration anyway.

Given a lot of locked memory may be needed in order to use multid migration with zerocopy enabled, make it optional by creating a new parameter multifd-zerocopy on qapi, so low-privileged users can still perform multifd migrations.

---
 qapi/migration.json   | 18 ++++++++++++++++++
 migration/migration.h |  1 +
 migration/multifd.h   |  2 +-
 migration/migration.c | 20 ++++++++++++++++++++
 migration/multifd.c   | 33 ++++++++++++++++++++++++++++-----
 migration/ram.c       | 20 +++++++++++++-------
 monitor/hmp-cmds.c    |  4 ++++
 7 files changed, 85 insertions(+), 13 deletions(-)
Quoted 13 lines

qapi/migration.json line 729

Quoted 11 lines
qapi/migration.json +3 -0
index 88f07baedd..c4890cbb54 100644
@@ -724,6 +724,11 @@
 #                      will consume more CPU.
 #                      Defaults to 1. (Since 5.0)
 #
+# @multifd-zerocopy: Controls behavior on sending memory pages on multifd migration.
+#                    When true, enables a zerocopy mechanism for sending memory
+#                    pages, if host does support it.

s/does support/supports/ (several instances this patch)

I will make sure to fix that in v5.

Quoted 16 lines

qapi/migration.json line 767

Quoted 12 lines
qapi/migration.json +3 -0
+#                    Defaults to false. (Since 6.2)
+#
 # @block-bitmap-mapping: Maps block nodes and bitmaps on them to
 #                        aliases for the purpose of dirty bitmap migration.  Such
 #                        aliases may for example be the corresponding names on the
@@ -758,6 +763,7 @@
            'xbzrle-cache-size', 'max-postcopy-bandwidth',
            'max-cpu-throttle', 'multifd-compression',
            'multifd-zlib-level' ,'multifd-zstd-level',
+        'multifd-zerocopy',
            'block-bitmap-mapping' ] }

Should this feature be guarded with 'if':'CONFIG_LINUX', since that's the only platform where you even compile the code (even then, it can still fail if the kernel doesn't support it).

I think it makes sense for the feature to always be available, even though it's not supported
outside linux > v4.14.

IMHO it makes more sense for the user to get an error when it starts migration, due to host
not supporting zerocopy, than the error happening in the config step, which may cause the user
to question if it was the right parameter.

The config message error here could also be ignored, and users can think zerocopy is working, while it's not.

For automated migrations, this feature should never be enabled for non-linux / older linux hosts anyway.

Is there a good argument I am missing for this feature being disabled on non-linux?

Quoted 6 lines

--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization: qemu.org | libvirt.org

Best regards,
Leo

#21 lore raw

Re: [PATCH v4 3/3] multifd: Implement zerocopy write in multifd migration (multifd-zerocopy)

Quoted 81 lines · Leonardo Bras Soares Passos

Hello Eric,

Quoted 35 lines · Eric Blake
Quoted 33 lines · Leonardo Bras

Implement zerocopy on nocomp_send_write(), by making use of QIOChannel zerocopy interface.

Change multifd_send_sync_main() so it can distinguish the last sync from the setup and per-iteration ones, so a flush_zerocopy() can be called at the last sync in order to make sure all RAM is sent before finishing the migration.

Also make it return -1 if flush_zerocopy() fails, in order to cancel the migration process, and avoid resuming the guest in the target host without receiving all current RAM.

This will work fine on RAM migration because the RAM pages are not usually freed, and there is no problem on changing the pages content between async_send() and the actual sending of the buffer, because this change will dirty the page and cause it to be re-sent on a next iteration anyway.

Given a lot of locked memory may be needed in order to use multid migration with zerocopy enabled, make it optional by creating a new parameter multifd-zerocopy on qapi, so low-privileged users can still perform multifd migrations.

---
 qapi/migration.json   | 18 ++++++++++++++++++
 migration/migration.h |  1 +
 migration/multifd.h   |  2 +-
 migration/migration.c | 20 ++++++++++++++++++++
 migration/multifd.c   | 33 ++++++++++++++++++++++++++++-----
 migration/ram.c       | 20 +++++++++++++-------
 monitor/hmp-cmds.c    |  4 ++++
 7 files changed, 85 insertions(+), 13 deletions(-)
Quoted 13 lines

qapi/migration.json line 729

Quoted 11 lines
qapi/migration.json +3 -0
index 88f07baedd..c4890cbb54 100644
@@ -724,6 +724,11 @@
 #                      will consume more CPU.
 #                      Defaults to 1. (Since 5.0)
 #
+# @multifd-zerocopy: Controls behavior on sending memory pages on multifd migration.
+#                    When true, enables a zerocopy mechanism for sending memory
+#                    pages, if host does support it.

s/does support/supports/ (several instances this patch)

I will make sure to fix that in v5.

Quoted 16 lines

qapi/migration.json line 767

Quoted 12 lines
qapi/migration.json +3 -0
+#                    Defaults to false. (Since 6.2)
+#
 # @block-bitmap-mapping: Maps block nodes and bitmaps on them to
 #                        aliases for the purpose of dirty bitmap migration.  Such
 #                        aliases may for example be the corresponding names on the
@@ -758,6 +763,7 @@
            'xbzrle-cache-size', 'max-postcopy-bandwidth',
            'max-cpu-throttle', 'multifd-compression',
            'multifd-zlib-level' ,'multifd-zstd-level',
+        'multifd-zerocopy',
            'block-bitmap-mapping' ] }

Should this feature be guarded with 'if':'CONFIG_LINUX', since that's the only platform where you even compile the code (even then, it can still fail if the kernel doesn't support it).

I think it makes sense for the feature to always be available, even though it's not supported
outside linux > v4.14.

IMHO it makes more sense for the user to get an error when it starts migration, due to host
not supporting zerocopy, than the error happening in the config step, which may cause the user
to question if it was the right parameter.

Quoted 8 lines

The config message error here could also be ignored, and users can think zerocopy is working, while it's not.

For automated migrations, this feature should never be enabled for non-linux / older linux hosts anyway.

Is there a good argument I am missing for this feature being disabled on non-linux?

The general argument for having QAPI schema 'if' mirror the C implementation's #if is introspection. Let me explain why that matters.

Consider a management application that supports a range of QEMU versions, say 5.0 to 6.2. Say it wants to use an QMP command that is new in QEMU 6.2. The sane way to do that is to probe for the command with query-qmp-schema. Same for command arguments, and anything else QMP.

If you doubt "sane", check out Part II of "QEMU interface introspection:
From hacks to solutions"[*].

The same technique works when a QMP command / argument / whatever is compile-time conditional ('if' in the schema). The code the management application needs anyway to deal with older QEMU now also deals with "compiled out". Nice.

Of course, a command or argument present in QEMU can still fail, and the management application still needs to handle failure. Distinguishing different failure modes can be bothersome and/or fragile.

By making the QAPI schema conditional mirror the C conditional, you squash the failure mode "this version of QEMU supports it, but this build of QEMU does not" into "this version of QEMU does not support it". Makes sense, doesn't it?

A minor additional advantage is less generated code.

[*] http://events17.linuxfoundation.org/sites/events/files/slides/armbru-qemu-introspection.pdf

#22 lore raw

Re: [PATCH v4 3/3] multifd: Implement zerocopy write in multifd migration (multifd-zerocopy)

Hello Markus!
(comments at the bottom)

Quoted 119 lines · Markus Armbruster
Quoted 81 lines · Leonardo Bras Soares Passos

Hello Eric,

Quoted 35 lines · Eric Blake
Quoted 33 lines · Leonardo Bras

Implement zerocopy on nocomp_send_write(), by making use of QIOChannel zerocopy interface.

Change multifd_send_sync_main() so it can distinguish the last sync from the setup and per-iteration ones, so a flush_zerocopy() can be called at the last sync in order to make sure all RAM is sent before finishing the migration.

Also make it return -1 if flush_zerocopy() fails, in order to cancel the migration process, and avoid resuming the guest in the target host without receiving all current RAM.

This will work fine on RAM migration because the RAM pages are not usually freed, and there is no problem on changing the pages content between async_send() and the actual sending of the buffer, because this change will dirty the page and cause it to be re-sent on a next iteration anyway.

Given a lot of locked memory may be needed in order to use multid migration with zerocopy enabled, make it optional by creating a new parameter multifd-zerocopy on qapi, so low-privileged users can still perform multifd migrations.

---
 qapi/migration.json   | 18 ++++++++++++++++++
 migration/migration.h |  1 +
 migration/multifd.h   |  2 +-
 migration/migration.c | 20 ++++++++++++++++++++
 migration/multifd.c   | 33 ++++++++++++++++++++++++++++-----
 migration/ram.c       | 20 +++++++++++++-------
 monitor/hmp-cmds.c    |  4 ++++
 7 files changed, 85 insertions(+), 13 deletions(-)
Quoted 13 lines

qapi/migration.json line 729

Quoted 11 lines
qapi/migration.json +3 -0
index 88f07baedd..c4890cbb54 100644
@@ -724,6 +724,11 @@
 #                      will consume more CPU.
 #                      Defaults to 1. (Since 5.0)
 #
+# @multifd-zerocopy: Controls behavior on sending memory pages on multifd migration.
+#                    When true, enables a zerocopy mechanism for sending memory
+#                    pages, if host does support it.

s/does support/supports/ (several instances this patch)

I will make sure to fix that in v5.

Quoted 16 lines

qapi/migration.json line 767

Quoted 12 lines
qapi/migration.json +3 -0
+#                    Defaults to false. (Since 6.2)
+#
 # @block-bitmap-mapping: Maps block nodes and bitmaps on them to
 #                        aliases for the purpose of dirty bitmap migration.  Such
 #                        aliases may for example be the corresponding names on the
@@ -758,6 +763,7 @@
            'xbzrle-cache-size', 'max-postcopy-bandwidth',
            'max-cpu-throttle', 'multifd-compression',
            'multifd-zlib-level' ,'multifd-zstd-level',
+        'multifd-zerocopy',
            'block-bitmap-mapping' ] }

Should this feature be guarded with 'if':'CONFIG_LINUX', since that's the only platform where you even compile the code (even then, it can still fail if the kernel doesn't support it).

I think it makes sense for the feature to always be available, even though it's not supported
outside linux > v4.14.

IMHO it makes more sense for the user to get an error when it starts migration, due to host
not supporting zerocopy, than the error happening in the config step, which may cause the user
to question if it was the right parameter.

Quoted 8 lines

The config message error here could also be ignored, and users can think zerocopy is working, while it's not.

For automated migrations, this feature should never be enabled for non-linux / older linux hosts anyway.

Is there a good argument I am missing for this feature being disabled on non-linux?

The general argument for having QAPI schema 'if' mirror the C implementation's #if is introspection. Let me explain why that matters.

Consider a management application that supports a range of QEMU versions, say 5.0 to 6.2. Say it wants to use an QMP command that is new in QEMU 6.2. The sane way to do that is to probe for the command with query-qmp-schema. Same for command arguments, and anything else QMP.

If you doubt "sane", check out Part II of "QEMU interface introspection:
From hacks to solutions"[*].

The same technique works when a QMP command / argument / whatever is compile-time conditional ('if' in the schema). The code the management application needs anyway to deal with older QEMU now also deals with "compiled out". Nice.

Of course, a command or argument present in QEMU can still fail, and the management application still needs to handle failure. Distinguishing different failure modes can be bothersome and/or fragile.

By making the QAPI schema conditional mirror the C conditional, you squash the failure mode "this version of QEMU supports it, but this build of QEMU does not" into "this version of QEMU does not support it". Makes sense, doesn't it?

Quoted 6 lines

A minor additional advantage is less generated code.

[*] http://events17.linuxfoundation.org/sites/events/files/slides/armbru-qemu-introspection.pdf

This was very informative, thanks!
I now understand the rationale about this choice.

TBH I am not very used to this syntax.
I did a take a peek at some other json files, and ended adding this lines in code, which compiled just fine:

for : enum MigrationParameter
{'name': 'multifd-zerocopy', 'if' : 'CONFIG_LINUX'},

for : struct MigrateSetParameters and struct MigrationParameters:
'*multifd-zerocopy': { 'type': 'bool', 'if': 'CONFIG_LINUX' },

Is that enough? Is there any other necessary change?

Thanks for reviewing and for helping out with this!

Best regards,
Leonardo Bras

#23 lore raw

Re: [PATCH v4 3/3] multifd: Implement zerocopy write in multifd migration (multifd-zerocopy)

Leonardo Bras Soares Passos <[email protected]> writes:

[...]

Quoted 40 lines
Quoted 26 lines

The general argument for having QAPI schema 'if' mirror the C implementation's #if is introspection. Let me explain why that matters.

Consider a management application that supports a range of QEMU versions, say 5.0 to 6.2. Say it wants to use an QMP command that is new in QEMU 6.2. The sane way to do that is to probe for the command with query-qmp-schema. Same for command arguments, and anything else QMP.

If you doubt "sane", check out Part II of "QEMU interface introspection:
From hacks to solutions"[*].

The same technique works when a QMP command / argument / whatever is compile-time conditional ('if' in the schema). The code the management application needs anyway to deal with older QEMU now also deals with "compiled out". Nice.

Of course, a command or argument present in QEMU can still fail, and the management application still needs to handle failure. Distinguishing different failure modes can be bothersome and/or fragile.

By making the QAPI schema conditional mirror the C conditional, you squash the failure mode "this version of QEMU supports it, but this build of QEMU does not" into "this version of QEMU does not support it". Makes sense, doesn't it?

Quoted 6 lines

A minor additional advantage is less generated code.

[*] http://events17.linuxfoundation.org/sites/events/files/slides/armbru-qemu-introspection.pdf

This was very informative, thanks!
I now understand the rationale about this choice.

TBH I am not very used to this syntax.
I did a take a peek at some other json files, and ended adding this lines in code, which compiled just fine:

Quoted 7 lines

for : enum MigrationParameter
{'name': 'multifd-zerocopy', 'if' : 'CONFIG_LINUX'},

for : struct MigrateSetParameters and struct MigrationParameters:
'*multifd-zerocopy': { 'type': 'bool', 'if': 'CONFIG_LINUX' },

Is that enough? Is there any other necessary change?

Looks good to me.

The QAPI schema language is documented in docs/devel/qapi-code-gen.rst.

If you're curious, you can diff code generated into qapi/ before and after adding the 'if'.

Quoted 1 line

Thanks for reviewing and for helping out with this!

My pleasure!

#24 lore raw

Re: [PATCH v4 3/3] multifd: Implement zerocopy write in multifd migration (multifd-zerocopy)

Quoted 48 lines · Markus Armbruster

Leonardo Bras Soares Passos <[email protected]> writes:

[...]

Quoted 43 lines
Quoted 26 lines

The general argument for having QAPI schema 'if' mirror the C implementation's #if is introspection. Let me explain why that matters.

Consider a management application that supports a range of QEMU versions, say 5.0 to 6.2. Say it wants to use an QMP command that is new in QEMU 6.2. The sane way to do that is to probe for the command with query-qmp-schema. Same for command arguments, and anything else QMP.

If you doubt "sane", check out Part II of "QEMU interface introspection:
From hacks to solutions"[*].

The same technique works when a QMP command / argument / whatever is compile-time conditional ('if' in the schema). The code the management application needs anyway to deal with older QEMU now also deals with "compiled out". Nice.

Of course, a command or argument present in QEMU can still fail, and the management application still needs to handle failure. Distinguishing different failure modes can be bothersome and/or fragile.

By making the QAPI schema conditional mirror the C conditional, you squash the failure mode "this version of QEMU supports it, but this build of QEMU does not" into "this version of QEMU does not support it". Makes sense, doesn't it?

Quoted 6 lines

A minor additional advantage is less generated code.

[*] http://events17.linuxfoundation.org/sites/events/files/slides/armbru-qemu-introspection.pdf

This was very informative, thanks!
I now understand the rationale about this choice.

TBH I am not very used to this syntax.
I did a take a peek at some other json files, and ended adding this lines in code, which compiled just fine:

for : enum MigrationParameter
{'name': 'multifd-zerocopy', 'if' : 'CONFIG_LINUX'},

Quoted 8 lines
Quoted 4 lines

for : struct MigrateSetParameters and struct MigrationParameters:
'*multifd-zerocopy': { 'type': 'bool', 'if': 'CONFIG_LINUX' },

Is that enough? Is there any other necessary change?

Looks good to me.

The QAPI schema language is documented in docs/devel/qapi-code-gen.rst.

Thanks for reviewing and for pointing this docs!

Quoted 3 lines

If you're curious, you can diff code generated into qapi/ before and after adding the 'if'.

Good idea!

Quoted 5 lines
Quoted 2 lines

Thanks for reviewing and for helping out with this!

My pleasure!

:)

Best regards,
Leo

#25 lore raw

Re: [PATCH v4 3/3] multifd: Implement zerocopy write in multifd migration (multifd-zerocopy)

In reply to Leonardo Bras

qapi/migration.json line 731

Quoted 13 lines · Leonardo Bras
qapi/migration.json +5 -0
index 88f07baedd..c4890cbb54 100644
@@ -724,6 +724,11 @@
 #                      will consume more CPU.
 #                      Defaults to 1. (Since 5.0)
 #
+# @multifd-zerocopy: Controls behavior on sending memory pages on multifd migration.
+#                    When true, enables a zerocopy mechanism for sending memory
+#                    pages, if host does support it.
+#                    Defaults to false. (Since 6.2)
+#

Shall we keep it named "@zerocopy"? Yes we have that dependency with multifd, but it's fine to just fail the configuration if multifd not set. The thing is we don't know whether that dependency will last forever, and we probably don't want to introduce yet another feature bit when we can remove the dependency.. as we can't remove the old one to be compatible.

Signature · 1 line
Peter Xu
#26 lore raw

Re: [PATCH v4 3/3] multifd: Implement zerocopy write in multifd migration (multifd-zerocopy)

Quoted 13 lines · Peter Xu
Quoted 11 lines · Leonardo Bras
qapi/migration.json +3 -0
index 88f07baedd..c4890cbb54 100644
@@ -724,6 +724,11 @@
 #                      will consume more CPU.
 #                      Defaults to 1. (Since 5.0)
 #
+# @multifd-zerocopy: Controls behavior on sending memory pages on multifd migration.
+#                    When true, enables a zerocopy mechanism for sending memory
+#                    pages, if host does support it.
Quoted 8 lines

qapi/migration.json line 731

Quoted 2 lines
qapi/migration.json +2 -0
+#                    Defaults to false. (Since 6.2)
+#

Shall we keep it named "@zerocopy"? Yes we have that dependency with multifd, but it's fine to just fail the configuration if multifd not set. The thing is we don't know whether that dependency will last forever, and we probably don't want to introduce yet another feature bit when we can remove the dependency.. as we can't remove the old one to be compatible.

It makes sense not wanting to create a new future bit in the future, but if we just add a
"@zerocopy' , wouldn't we need to fail every migration setup that don't support zerocopy?

(Thinking back, to stay as it is, it would also be necessary that I find a way to fail other multifd setups that don't support zerocopy, for v5)

Quoted 4 lines

--
Peter Xu

#27 lore raw

Re: [PATCH v4 3/3] multifd: Implement zerocopy write in multifd migration (multifd-zerocopy)

Quoted 23 lines · Leonardo Bras Soares Passos
Quoted 13 lines · Peter Xu
Quoted 11 lines · Leonardo Bras
qapi/migration.json +3 -0
index 88f07baedd..c4890cbb54 100644
@@ -724,6 +724,11 @@
 #                      will consume more CPU.
 #                      Defaults to 1. (Since 5.0)
 #
+# @multifd-zerocopy: Controls behavior on sending memory pages on multifd migration.
+#                    When true, enables a zerocopy mechanism for sending memory
+#                    pages, if host does support it.
Quoted 8 lines

qapi/migration.json line 731

Quoted 2 lines
qapi/migration.json +2 -0
+#                    Defaults to false. (Since 6.2)
+#

Shall we keep it named "@zerocopy"? Yes we have that dependency with multifd, but it's fine to just fail the configuration if multifd not set. The thing is we don't know whether that dependency will last forever, and we probably don't want to introduce yet another feature bit when we can remove the dependency.. as we can't remove the old one to be compatible.

Quoted 8 lines

It makes sense not wanting to create a new future bit in the future, but if we just add a
"@zerocopy' , wouldn't we need to fail every migration setup that don't support zerocopy?

(Thinking back, to stay as it is, it would also be necessary that I find a way to fail other multifd setups that don't support zerocopy, for v5)

Yes I think so; imho we can fail either whey applying the bit, or it's okay too to fail at the start of migration. Thanks,

Signature · 1 line
Peter Xu
#28 lore raw

Re: [PATCH v4 3/3] multifd: Implement zerocopy write in multifd migration (multifd-zerocopy)

In reply to Leonardo Bras

migration/multifd.c static int nocomp_send_prepare(MultiFDSendParams *p, uint32_t used, · line 115

Quoted 13 lines · Leonardo Bras
migration/multifd.c +7 -1
@@ -105,7 +105,13 @@ static int nocomp_send_prepare(MultiFDSendParams *p, uint32_t used,
  */
 static int nocomp_send_write(MultiFDSendParams *p, uint32_t used, Error **errp)
 {
-    return qio_channel_writev_all(p->c, p->pages->iov, used, errp);
+    int flags = 0;
+
+    if (migrate_multifd_zerocopy()) {
+        flags = QIO_CHANNEL_WRITE_FLAG_ZEROCOPY;
+    }
+
+    return qio_channel_writev_all_flags(p->c, p->pages->iov, used, flags, errp);
 }

What if the user specified ZEROCOPY+MULTIFD, but the kernel doesn't support it?

IIUC then the first call to qio_channel_writev_all_flags() above will fail, then we fail the migration.

It seems fine, but since you've introduced QIO_CHANNEL_FEATURE_WRITE_ZEROCOPY in the previous patch - I think the cleaner way is when start migration and after we setup the ioc, we sanity check on the capability and the ioc to make sure if ZEROCOPY+MULTIFD is specified, we should fail early if we're sure the ioc doesn't have QIO_CHANNEL_FEATURE_WRITE_ZEROCOPY bit set?

Signature · 1 line
Peter Xu
#29 lore raw

Re: [PATCH v4 3/3] multifd: Implement zerocopy write in multifd migration (multifd-zerocopy)

Quoted 21 lines · Peter Xu

migration/multifd.c static int nocomp_send_prepare(MultiFDSendParams *p, uint32_t used, · line 115

Quoted 13 lines · Leonardo Bras
migration/multifd.c +7 -1
@@ -105,7 +105,13 @@ static int nocomp_send_prepare(MultiFDSendParams *p, uint32_t used,
  */
 static int nocomp_send_write(MultiFDSendParams *p, uint32_t used, Error **errp)
 {
-    return qio_channel_writev_all(p->c, p->pages->iov, used, errp);
+    int flags = 0;
+
+    if (migrate_multifd_zerocopy()) {
+        flags = QIO_CHANNEL_WRITE_FLAG_ZEROCOPY;
+    }
+
+    return qio_channel_writev_all_flags(p->c, p->pages->iov, used, flags, errp);
 }

What if the user specified ZEROCOPY+MULTIFD, but the kernel doesn't support it?

IIUC then the first call to qio_channel_writev_all_flags() above will fail, then we fail the migration.

Quoted 9 lines

It seems fine, but since you've introduced QIO_CHANNEL_FEATURE_WRITE_ZEROCOPY in the previous patch - I think the cleaner way is when start migration and after we setup the ioc, we sanity check on the capability and the ioc to make sure if ZEROCOPY+MULTIFD is specified, we should fail early if we're sure the ioc doesn't have QIO_CHANNEL_FEATURE_WRITE_ZEROCOPY bit set?

--
Peter Xu

Failing earlier is always a good idea.
I will try to implement that.

Thanks Peter!

Best regards,
Leo

#30 lore raw

Re: [PATCH v4 3/3] multifd: Implement zerocopy write in multifd migration (multifd-zerocopy)

In reply to Leonardo Bras
Quoted 7 lines · Leonardo Bras

Implement zerocopy on nocomp_send_write(), by making use of QIOChannel zerocopy interface.

Change multifd_send_sync_main() so it can distinguish the last sync from the setup and per-iteration ones, so a flush_zerocopy() can be called at the last sync in order to make sure all RAM is sent before finishing the migration.

You need to do this after each iteration. Otherwise it can happen that:

channel 1: channel 2:

send page 11

next iteration
send page 11

this page arrives

now arrives this old copy.

After each iteration, one needs to be sure that no ram is inflight.

This means that I think you don't need the last_sync parameter at all, as you have to do the flush() in every iteration.

Quoted 9 lines

Also make it return -1 if flush_zerocopy() fails, in order to cancel the migration process, and avoid resuming the guest in the target host without receiving all current RAM.

This will work fine on RAM migration because the RAM pages are not usually freed, and there is no problem on changing the pages content between async_send() and the actual sending of the buffer, because this change will dirty the page and cause it to be re-sent on a next iteration anyway.

Quoted 6 lines

Given a lot of locked memory may be needed in order to use multid migration with zerocopy enabled, make it optional by creating a new parameter multifd-zerocopy on qapi, so low-privileged users can still perform multifd migrations.

I think it is better that you split this patch into two:

- Add the new parameter (it looks good to me, and can be reviewed-by)
- Implement the feature, here probably you need more changes/review

Review

Quoted 4 lines
             '*multifd-zlib-level': 'uint8',
             '*multifd-zstd-level': 'uint8',
(quoted diff) +1 -0
+	    '*multifd-zerocopy': 'bool',
             '*block-bitmap-mapping': [ 'BitmapMigrationNodeAlias' ] } }

Something weird here.

Review

Quoted 6 lines
             '*multifd-compression': 'MultiFDCompression',
             '*multifd-zlib-level': 'uint8',
             '*multifd-zstd-level': 'uint8',
(quoted diff) +1 -0
+	    '*multifd-zerocopy': 'bool',
             '*block-bitmap-mapping': [ 'BitmapMigrationNodeAlias' ] } }

Same here.

migration/multifd.c static int nocomp_send_prepare(MultiFDSendParams *p, uint32_t used, · line 112

Quoted 10 lines
migration/multifd.c +5 -1
@@ -105,7 +105,13 @@ static int nocomp_send_prepare(MultiFDSendParams *p, uint32_t used,
  */
 static int nocomp_send_write(MultiFDSendParams *p, uint32_t used, Error **errp)
 {
-    return qio_channel_writev_all(p->c, p->pages->iov, used, errp);
+    int flags = 0;
+
+    if (migrate_multifd_zerocopy()) {
+        flags = QIO_CHANNEL_WRITE_FLAG_ZEROCOPY;
+    }

You have added an if on each write, just add it during initialization.

There is already a uint32_t flags field in MultiFDRecvParams, but you can add a

int write_flags;

one and add it during initialization. That way you don't need any check here, just pass it.

migration/multifd.c void multifd_save_cleanup(void) · line 584

Quoted 5 lines
migration/multifd.c +2 -1
+    return qio_channel_writev_all_flags(p->c, p->pages->iov, used, flags, errp);
 
 
-void multifd_send_sync_main(QEMUFile *f)
+int multifd_send_sync_main(QEMUFile *f, bool last_sync)

As you need to check every round, you now have to check for errors on every multifd_send_sync_main() call. It really looked weird that you only need to check it sometimes.

Quoted 6 lines
migration/ram.c +0 -2
@@ -3006,13 +3006,19 @@ static int ram_save_complete(QEMUFile *f, void *opaque)
         ram_control_after_iterate(f, RAM_CONTROL_FINISH);
     }
 
-    if (ret >= 0) {
-        multifd_send_sync_main(rs->f);

migration/ram.c static int ram_save_complete(QEMUFile *f, void *opaque) · line 3015

Quoted 10 lines
migration/ram.c +5 -3
-        qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
-        qemu_fflush(f);
+    if (ret < 0) {
+        return ret;
     }
 
-    return ret;
+    ret = multifd_send_sync_main(rs->f, true);
+    if (ret < 0) {
+        return -1;

Why are you returning -1 instead of ret?

Callers of ram_save_complete(). We set qemu_error_file() with that error, so it is not a good idea to reset it.

Later, Juan.

#31 lore raw

Re: [PATCH v4 3/3] multifd: Implement zerocopy write in multifd migration (multifd-zerocopy)

Hello Juan,

Quoted 14 lines · Juan Quintela
Quoted 7 lines · Leonardo Bras

Implement zerocopy on nocomp_send_write(), by making use of QIOChannel zerocopy interface.

Change multifd_send_sync_main() so it can distinguish the last sync from the setup and per-iteration ones, so a flush_zerocopy() can be called at the last sync in order to make sure all RAM is sent before finishing the migration.

You need to do this after each iteration. Otherwise it can happen that:

channel 1: channel 2:

Quoted 8 lines

send page 11

next iteration
send page 11

this page arrives

now arrives this old copy.

Current multifd's sendmsg() will block until all data is sent, is that correct?

If that's the case, and supposing the network driver supports multiqueue, maybe there is a small chance for this to happen. I will add the flush at the end of each iteration, just to be sure.

Quoted 19 lines

After each iteration, one needs to be sure that no ram is inflight.

This means that I think you don't need the last_sync parameter at all, as you have to do the flush() in every iteration.

Quoted 13 lines

Also make it return -1 if flush_zerocopy() fails, in order to cancel the migration process, and avoid resuming the guest in the target host without receiving all current RAM.> >
This will work fine on RAM migration because the RAM pages are not usually freed, and there is no problem on changing the pages content between async_send() and the actual sending of the buffer, because this change will dirty the page and cause it to be re-sent on a next iteration anyway.

Given a lot of locked memory may be needed in order to use multid migration with zerocopy enabled, make it optional by creating a new parameter multifd-zerocopy on qapi, so low-privileged users can still perform multifd migrations.

Quoted 6 lines
Quoted 1 line

I think it is better that you split this patch into two:

- Add the new parameter (it looks good to me, and can be reviewed-by)
- Implement the feature, here probably you need more changes/review

Sure, I will try to divide the patch like this.

Quoted 9 lines
Quoted 6 lines

             '*multifd-zlib-level': 'uint8',
             '*multifd-zstd-level': 'uint8',
+         '*multifd-zerocopy': 'bool',
             '*block-bitmap-mapping': [ 'BitmapMigrationNodeAlias' ] } }

Something weird here.

Quoted 8 lines
Quoted 6 lines
             '*multifd-compression': 'MultiFDCompression',
             '*multifd-zlib-level': 'uint8',
             '*multifd-zstd-level': 'uint8',
+         '*multifd-zerocopy': 'bool',
             '*block-bitmap-mapping': [ 'BitmapMigrationNodeAlias' ] } }

Same here.

Could you please elaborate?

Quoted 18 lines

migration/multifd.c static int nocomp_send_prepare(MultiFDSendParams *p, uint32_t used, · line 112

Quoted 12 lines
migration/multifd.c +5 -1
@@ -105,7 +105,13 @@ static int nocomp_send_prepare(MultiFDSendParams *p, uint32_t used,
  */
 static int nocomp_send_write(MultiFDSendParams *p, uint32_t used, Error **errp)
 {
-    return qio_channel_writev_all(p->c, p->pages->iov, used, errp);
+    int flags = 0;
+
+    if (migrate_multifd_zerocopy()) {
+        flags = QIO_CHANNEL_WRITE_FLAG_ZEROCOPY;
+    }

You have added an if on each write, just add it during initialization.

Quoted 9 lines

There is already a uint32_t flags field in MultiFDRecvParams, but you can add a

int write_flags;

one and add it during initialization. That way you don't need any check here, just pass it.

Quoted 1 line
migration/multifd.c +1 -0
+    return qio_channel_writev_all_flags(p->c, p->pages->iov, used, flags, errp);

Ok, I will try to make it work with this suggestion.

Quoted 8 lines

migration/multifd.c void multifd_save_cleanup(void) · line 584

Quoted 4 lines
migration/multifd.c +1 -1
-void multifd_send_sync_main(QEMUFile *f)
+int multifd_send_sync_main(QEMUFile *f, bool last_sync)

As you need to check every round, you now have to check for errors on every multifd_send_sync_main() call. It really looked weird that you only need to check it sometimes.

Ok, I will work on that.

Quoted 20 lines
Quoted 7 lines
migration/ram.c +0 -2
@@ -3006,13 +3006,19 @@ static int ram_save_complete(QEMUFile *f, void *opaque)
         ram_control_after_iterate(f, RAM_CONTROL_FINISH);
     }
 
-    if (ret >= 0) {
-        multifd_send_sync_main(rs->f);

migration/ram.c static int ram_save_complete(QEMUFile *f, void *opaque) · line 3015

Quoted 10 lines
migration/ram.c +5 -3
-        qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
-        qemu_fflush(f);
+    if (ret < 0) {
+        return ret;
     }
 
-    return ret;
+    ret = multifd_send_sync_main(rs->f, true);
+    if (ret < 0) {
+        return -1;

Why are you returning -1 instead of ret?

Quoted 4 lines

Callers of ram_save_complete(). We set qemu_error_file() with that error, so it is not a good idea to reset it.

Ok, I will take a look on that.

Quoted 2 lines

Later, Juan.

Thanks,
Leo

#32 lore raw

Re: [PATCH v4 3/3] multifd: Implement zerocopy write in multifd migration (multifd-zerocopy)

Quoted 1 line · Leonardo Bras Soares Passos

Hello Juan,

hi

Quoted 11 lines

Current multifd's sendmsg() will block until all data is sent, is that correct?

If that's the case, and supposing the network driver supports multiqueue, maybe there is a small chance for this to happen. I will add the flush at the end of each iteration, just to be sure.

Quoted 5 lines

After each iteration, one needs to be sure that no ram is inflight.

This means that I think you don't need the last_sync parameter at all, as you have to do the flush() in every iteration.

It means guest memory corruption, it _needs_ to be there. That is the whole reason why multifd code has to wait after each iteration for all channels to finish. Probability of failure is low, but it exist, so it needs to be handled correctly.

Quoted 14 lines
Quoted 14 lines
Quoted 4 lines
             '*multifd-zlib-level': 'uint8',
             '*multifd-zstd-level': 'uint8',
+         '*multifd-zerocopy': 'bool',
             '*block-bitmap-mapping': [ 'BitmapMigrationNodeAlias' ] } }

Something weird here.

Quoted 6 lines
             '*multifd-compression': 'MultiFDCompression',
             '*multifd-zlib-level': 'uint8',
             '*multifd-zstd-level': 'uint8',
+         '*multifd-zerocopy': 'bool',
             '*block-bitmap-mapping': [ 'BitmapMigrationNodeAlias' ] } }
Quoted 3 lines
Quoted 1 line

Same here.

Could you please elaborate?

Indentation,

+ '*multifd-zerocopy': 'bool',

             '*block-bitmap-mapping': [ 'BitmapMigrationNodeAlias' ] } }

This is how it is seen here. space/tab?

Later, Juan.

#33 lore raw

Re: [PATCH v4 3/3] multifd: Implement zerocopy write in multifd migration (multifd-zerocopy)

Quoted 15 lines · Juan Quintela
Quoted 1 line · Leonardo Bras Soares Passos

Hello Juan,

hi

Quoted 9 lines

Current multifd's sendmsg() will block until all data is sent, is that correct?

If that's the case, and supposing the network driver supports multiqueue, maybe there is a small chance for this to happen. I will add the flush at the end of each iteration, just to be sure.

Quoted 3 lines

After each iteration, one needs to be sure that no ram is inflight.

Quoted 7 lines
Quoted 2 lines
Quoted 2 lines

This means that I think you don't need the last_sync parameter at all, as you have to do the flush() in every iteration.

It means guest memory corruption, it _needs_ to be there. That is the whole reason why multifd code has to wait after each iteration for all channels to finish. Probability of failure is low, but it exist, so it needs to be handled correctly.

Sure, I will add a flush after each iteration.

Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
Quoted 4 lines
             '*multifd-zlib-level': 'uint8',
             '*multifd-zstd-level': 'uint8',
+         '*multifd-zerocopy': 'bool',
             '*block-bitmap-mapping': [ 'BitmapMigrationNodeAlias' ] } }

Something weird here.

Quoted 6 lines
             '*multifd-compression': 'MultiFDCompression',
             '*multifd-zlib-level': 'uint8',
             '*multifd-zstd-level': 'uint8',
+         '*multifd-zerocopy': 'bool',
             '*block-bitmap-mapping': [ 'BitmapMigrationNodeAlias' ] } }

Same here.

Quoted 8 lines
Quoted 1 line

Could you please elaborate?

Indentation,

+ '*multifd-zerocopy': 'bool',

             '*block-bitmap-mapping': [ 'BitmapMigrationNodeAlias' ] } }

This is how it is seen here. space/tab?

It was supposed to be using only spaces.
I will make sure to fix that.

Quoted 3 lines

Later, Juan.

Thanks!

Best regards,
Leo

#34 lore raw

Re: [PATCH v4 3/3] multifd: Implement zerocopy write in multifd migration (multifd-zerocopy)

In reply to Juan Quintela

Hello Juan,

Sorry to go back that early in discussion, but I was reviewing for v9 and I am not sure If I am unable to recall the reason, or I missed an argument here.
Could you please help me with this?

Quoted 19 lines · Juan Quintela
Quoted 7 lines · Leonardo Bras

Implement zerocopy on nocomp_send_write(), by making use of QIOChannel zerocopy interface.

Change multifd_send_sync_main() so it can distinguish the last sync from the setup and per-iteration ones, so a flush_zerocopy() can be called at the last sync in order to make sure all RAM is sent before finishing the migration.

You need to do this after each iteration. Otherwise it can happen that:

channel 1: channel 2:

send page 11

next iteration
send page 11

Quoted 8 lines

this page arrives

now arrives this old copy.

After each iteration, one needs to be sure that no ram is inflight.

This means that I think you don't need the last_sync parameter at all, as you have to do the flush() in every iteration.

The flush command is used to guarantee every packet queued before flush is actually sent before flush returns.
I mean, flushing every iteration will not help with the situation above, where the pages are sent in order, but arrive at target in a different order.

There is a chance that in the above text you meant 'send page' as "queue page for sending", and 'page arrives' as "actually send the queued page".
It that is correct, then syncing every iteration should not be necessary:
- On page queue, Linux saves the page address and size for sending
- On actual send, Linux will send the current data in the page and send.

So, in this example, if page 11 from iteration 'i' happens to be 'actually sent' after page 11 from iteration 'i+1', it would not be an issue:
###
channel 1: channel 2:
Iteration i

queue page 11 (i)

iteration i+1
queue page 11 (i+1) actually send page 11 (i+1)

actually send page 11 (i)
###

That's because page 11 (i) will contain a newer version compared to page 11 (i+1)

tl;dr:
- The page content always depends on the send time, instead of queue time.
- The iteration count describes the queue time.
(on non-zerocopy it's the opposite: it will depend on queue time, because it copies the memory content during enqueue)

Quoted 1 line

[...]

Juan, could you please help me understand if I am missing a part of your argument up there?
Also, syncing every iteration is still necessary / recommended?

Best regards,
Leo