All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1 1/1] QIOChannelSocket: Fix zero-copy send so socket flush works
@ 2022-06-08 18:18 Leonardo Bras
  2022-06-08 18:46 ` Daniel P. Berrangé
  2022-06-08 20:26 ` Peter Xu
  0 siblings, 2 replies; 7+ messages in thread
From: Leonardo Bras @ 2022-06-08 18:18 UTC (permalink / raw)
  To: Daniel P. Berrangé
  Cc: Leonardo Bras, qemu-devel, Peter Xu, 徐闯

Somewhere between v6 and v7 the of the zero-copy-send patchset a crucial
part of the flushing mechanism got missing: incrementing zero_copy_queued.

Without that, the flushing interface becomes a no-op, and there is no
garantee the buffer is really sent.

This can go as bad as causing a corruption in RAM during migration.

Fixes: 2bc58ffc2926 ("QIOChannelSocket: Implement io_writev zero copy flag & io_flush for CONFIG_LINUX")
Reported-by: 徐闯 <xuchuangxclwt@bytedance.com>
Signed-off-by: Leonardo Bras <leobras@redhat.com>
---
 io/channel-socket.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/io/channel-socket.c b/io/channel-socket.c
index dc9c165de1..ca4cae930f 100644
--- a/io/channel-socket.c
+++ b/io/channel-socket.c
@@ -554,6 +554,7 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
     size_t fdsize = sizeof(int) * nfds;
     struct cmsghdr *cmsg;
     int sflags = 0;
+    bool zero_copy_enabled = false;
 
     memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
 
@@ -581,6 +582,7 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
 #ifdef QEMU_MSG_ZEROCOPY
     if (flags & QIO_CHANNEL_WRITE_FLAG_ZERO_COPY) {
         sflags = MSG_ZEROCOPY;
+        zero_copy_enabled = true;
     }
 #endif
 
@@ -592,21 +594,24 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
             return QIO_CHANNEL_ERR_BLOCK;
         case EINTR:
             goto retry;
-#ifdef QEMU_MSG_ZEROCOPY
         case ENOBUFS:
-            if (sflags & MSG_ZEROCOPY) {
+            if (zero_copy_enabled) {
                 error_setg_errno(errp, errno,
                                  "Process can't lock enough memory for using MSG_ZEROCOPY");
                 return -1;
             }
             break;
-#endif
         }
 
         error_setg_errno(errp, errno,
                          "Unable to write to socket");
         return -1;
     }
+
+    if (zero_copy_enabled) {
+        sioc->zero_copy_queued++;
+    }
+
     return ret;
 }
 #else /* WIN32 */
-- 
2.36.1



^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v1 1/1] QIOChannelSocket: Fix zero-copy send so socket flush works
  2022-06-08 18:18 [PATCH v1 1/1] QIOChannelSocket: Fix zero-copy send so socket flush works Leonardo Bras
@ 2022-06-08 18:46 ` Daniel P. Berrangé
  2022-06-08 20:25   ` Peter Xu
  2022-06-08 20:48   ` Leonardo Bras Soares Passos
  2022-06-08 20:26 ` Peter Xu
  1 sibling, 2 replies; 7+ messages in thread
From: Daniel P. Berrangé @ 2022-06-08 18:46 UTC (permalink / raw)
  To: Leonardo Bras; +Cc: qemu-devel, Peter Xu, 徐闯

On Wed, Jun 08, 2022 at 03:18:09PM -0300, Leonardo Bras wrote:
> Somewhere between v6 and v7 the of the zero-copy-send patchset a crucial
> part of the flushing mechanism got missing: incrementing zero_copy_queued.
> 
> Without that, the flushing interface becomes a no-op, and there is no
> garantee the buffer is really sent.
> 
> This can go as bad as causing a corruption in RAM during migration.
> 
> Fixes: 2bc58ffc2926 ("QIOChannelSocket: Implement io_writev zero copy flag & io_flush for CONFIG_LINUX")
> Reported-by: 徐闯 <xuchuangxclwt@bytedance.com>
> Signed-off-by: Leonardo Bras <leobras@redhat.com>
> ---
>  io/channel-socket.c | 11 ++++++++---
>  1 file changed, 8 insertions(+), 3 deletions(-)
> 
> diff --git a/io/channel-socket.c b/io/channel-socket.c
> index dc9c165de1..ca4cae930f 100644
> --- a/io/channel-socket.c
> +++ b/io/channel-socket.c
> @@ -554,6 +554,7 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
>      size_t fdsize = sizeof(int) * nfds;
>      struct cmsghdr *cmsg;
>      int sflags = 0;
> +    bool zero_copy_enabled = false;
>  
>      memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
>  
> @@ -581,6 +582,7 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
>  #ifdef QEMU_MSG_ZEROCOPY
>      if (flags & QIO_CHANNEL_WRITE_FLAG_ZERO_COPY) {
>          sflags = MSG_ZEROCOPY;
> +        zero_copy_enabled = true;
>      }
>  #endif
>  
> @@ -592,21 +594,24 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
>              return QIO_CHANNEL_ERR_BLOCK;
>          case EINTR:
>              goto retry;
> -#ifdef QEMU_MSG_ZEROCOPY

Removing this ifdef appears incidental to the change. If this is
redundant just remove it in its own patch.

>          case ENOBUFS:
> -            if (sflags & MSG_ZEROCOPY) {
> +            if (zero_copy_enabled) {
>                  error_setg_errno(errp, errno,
>                                   "Process can't lock enough memory for using MSG_ZEROCOPY");
>                  return -1;
>              }
>              break;
> -#endif
>          }
>  
>          error_setg_errno(errp, errno,
>                           "Unable to write to socket");
>          return -1;
>      }
> +
> +    if (zero_copy_enabled) {

What's wrong with

   if (flags & QIO_CHANNEL_WRITE_FLAG_ZERO_COPY) {
        sioc->zero_copy_queued++;
    }


Introducing another local variable doesn't really add value IMHO.

With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v1 1/1] QIOChannelSocket: Fix zero-copy send so socket flush works
  2022-06-08 18:46 ` Daniel P. Berrangé
@ 2022-06-08 20:25   ` Peter Xu
  2022-06-08 20:48   ` Leonardo Bras Soares Passos
  1 sibling, 0 replies; 7+ messages in thread
From: Peter Xu @ 2022-06-08 20:25 UTC (permalink / raw)
  To: Daniel P. Berrangé; +Cc: Leonardo Bras, qemu-devel, 徐闯

On Wed, Jun 08, 2022 at 07:46:43PM +0100, Daniel P. Berrangé wrote:
> On Wed, Jun 08, 2022 at 03:18:09PM -0300, Leonardo Bras wrote:
> > Somewhere between v6 and v7 the of the zero-copy-send patchset a crucial
> > part of the flushing mechanism got missing: incrementing zero_copy_queued.
> > 
> > Without that, the flushing interface becomes a no-op, and there is no
> > garantee the buffer is really sent.
> > 
> > This can go as bad as causing a corruption in RAM during migration.
> > 
> > Fixes: 2bc58ffc2926 ("QIOChannelSocket: Implement io_writev zero copy flag & io_flush for CONFIG_LINUX")
> > Reported-by: 徐闯 <xuchuangxclwt@bytedance.com>
> > Signed-off-by: Leonardo Bras <leobras@redhat.com>
> > ---
> >  io/channel-socket.c | 11 ++++++++---
> >  1 file changed, 8 insertions(+), 3 deletions(-)
> > 
> > diff --git a/io/channel-socket.c b/io/channel-socket.c
> > index dc9c165de1..ca4cae930f 100644
> > --- a/io/channel-socket.c
> > +++ b/io/channel-socket.c
> > @@ -554,6 +554,7 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
> >      size_t fdsize = sizeof(int) * nfds;
> >      struct cmsghdr *cmsg;
> >      int sflags = 0;
> > +    bool zero_copy_enabled = false;
> >  
> >      memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
> >  
> > @@ -581,6 +582,7 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
> >  #ifdef QEMU_MSG_ZEROCOPY
> >      if (flags & QIO_CHANNEL_WRITE_FLAG_ZERO_COPY) {
> >          sflags = MSG_ZEROCOPY;
> > +        zero_copy_enabled = true;
> >      }
> >  #endif
> >  
> > @@ -592,21 +594,24 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
> >              return QIO_CHANNEL_ERR_BLOCK;
> >          case EINTR:
> >              goto retry;
> > -#ifdef QEMU_MSG_ZEROCOPY
> 
> Removing this ifdef appears incidental to the change. If this is
> redundant just remove it in its own patch.
> 
> >          case ENOBUFS:
> > -            if (sflags & MSG_ZEROCOPY) {
> > +            if (zero_copy_enabled) {
> >                  error_setg_errno(errp, errno,
> >                                   "Process can't lock enough memory for using MSG_ZEROCOPY");
> >                  return -1;
> >              }
> >              break;
> > -#endif
> >          }
> >  
> >          error_setg_errno(errp, errno,
> >                           "Unable to write to socket");
> >          return -1;
> >      }
> > +
> > +    if (zero_copy_enabled) {
> 
> What's wrong with
> 
>    if (flags & QIO_CHANNEL_WRITE_FLAG_ZERO_COPY) {
>         sioc->zero_copy_queued++;
>     }
> 
> 
> Introducing another local variable doesn't really add value IMHO.

One benefit of having that variable is we setup zero_copy_enabled once in
the #ifdef and the rest code can avoid wrapping with the macro.  From that
pov the patch looks okay to me.  Thanks,

-- 
Peter Xu



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v1 1/1] QIOChannelSocket: Fix zero-copy send so socket flush works
  2022-06-08 18:18 [PATCH v1 1/1] QIOChannelSocket: Fix zero-copy send so socket flush works Leonardo Bras
  2022-06-08 18:46 ` Daniel P. Berrangé
@ 2022-06-08 20:26 ` Peter Xu
  2022-06-08 20:55   ` Peter Xu
  1 sibling, 1 reply; 7+ messages in thread
From: Peter Xu @ 2022-06-08 20:26 UTC (permalink / raw)
  To: Leonardo Bras
  Cc: Daniel P. Berrangé, qemu-devel, 徐闯,
	Juan Quintela, Dr. David Alan Gilbert

On Wed, Jun 08, 2022 at 03:18:09PM -0300, Leonardo Bras wrote:
> Somewhere between v6 and v7 the of the zero-copy-send patchset a crucial
> part of the flushing mechanism got missing: incrementing zero_copy_queued.
> 
> Without that, the flushing interface becomes a no-op, and there is no
> garantee the buffer is really sent.
> 
> This can go as bad as causing a corruption in RAM during migration.
> 
> Fixes: 2bc58ffc2926 ("QIOChannelSocket: Implement io_writev zero copy flag & io_flush for CONFIG_LINUX")
> Reported-by: 徐闯 <xuchuangxclwt@bytedance.com>
> Signed-off-by: Leonardo Bras <leobras@redhat.com>

Copy Dave/Juan; Leo please remember to do so in the next posts, or no one
will be picking this up. :)

-- 
Peter Xu



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v1 1/1] QIOChannelSocket: Fix zero-copy send so socket flush works
  2022-06-08 18:46 ` Daniel P. Berrangé
  2022-06-08 20:25   ` Peter Xu
@ 2022-06-08 20:48   ` Leonardo Bras Soares Passos
  1 sibling, 0 replies; 7+ messages in thread
From: Leonardo Bras Soares Passos @ 2022-06-08 20:48 UTC (permalink / raw)
  To: Daniel P. Berrangé
  Cc: qemu-devel, Peter Xu, 徐闯, David Gilbert, Juan Quintela

Hello Daniel,

On Wed, Jun 8, 2022 at 3:46 PM Daniel P. Berrangé <berrange@redhat.com> wrote:
>
> On Wed, Jun 08, 2022 at 03:18:09PM -0300, Leonardo Bras wrote:
> > Somewhere between v6 and v7 the of the zero-copy-send patchset a crucial
> > part of the flushing mechanism got missing: incrementing zero_copy_queued.
> >
> > Without that, the flushing interface becomes a no-op, and there is no
> > garantee the buffer is really sent.
> >
> > This can go as bad as causing a corruption in RAM during migration.
> >
> > Fixes: 2bc58ffc2926 ("QIOChannelSocket: Implement io_writev zero copy flag & io_flush for CONFIG_LINUX")
> > Reported-by: 徐闯 <xuchuangxclwt@bytedance.com>
> > Signed-off-by: Leonardo Bras <leobras@redhat.com>
> > ---
> >  io/channel-socket.c | 11 ++++++++---
> >  1 file changed, 8 insertions(+), 3 deletions(-)
> >
> > diff --git a/io/channel-socket.c b/io/channel-socket.c
> > index dc9c165de1..ca4cae930f 100644
> > --- a/io/channel-socket.c
> > +++ b/io/channel-socket.c
> > @@ -554,6 +554,7 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
> >      size_t fdsize = sizeof(int) * nfds;
> >      struct cmsghdr *cmsg;
> >      int sflags = 0;
> > +    bool zero_copy_enabled = false;
> >
> >      memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
> >
> > @@ -581,6 +582,7 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
> >  #ifdef QEMU_MSG_ZEROCOPY
> >      if (flags & QIO_CHANNEL_WRITE_FLAG_ZERO_COPY) {
> >          sflags = MSG_ZEROCOPY;
> > +        zero_copy_enabled = true;
> >      }
> >  #endif
> >
> > @@ -592,21 +594,24 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
> >              return QIO_CHANNEL_ERR_BLOCK;
> >          case EINTR:
> >              goto retry;
> > -#ifdef QEMU_MSG_ZEROCOPY
>
> Removing this ifdef appears incidental to the change. If this is
> redundant just remove it in its own patch.

The idea is to reduce the amount of #ifdefs as Peter suggested,
because adding another ifdef here
would introduce extra noise. But sure, I see no problem adding this
change as a previous patch.

>
> >          case ENOBUFS:
> > -            if (sflags & MSG_ZEROCOPY) {
> > +            if (zero_copy_enabled) {
> >                  error_setg_errno(errp, errno,
> >                                   "Process can't lock enough memory for using MSG_ZEROCOPY");
> >                  return -1;
> >              }
> >              break;
> > -#endif
> >          }
> >
> >          error_setg_errno(errp, errno,
> >                           "Unable to write to socket");
> >          return -1;
> >      }
> > +
> > +    if (zero_copy_enabled) {
>
> What's wrong with
>
>    if (flags & QIO_CHANNEL_WRITE_FLAG_ZERO_COPY) {
>         sioc->zero_copy_queued++;
>     }

There is nothing wrong with it, but using zero_copy_enabled as
presented here will
compile-out this 'if()'  block if the user does not support MSG_ZEROCOPY.

Best regards,
Leo

>
>
> Introducing another local variable doesn't really add value IMHO.
>
> With regards,
> Daniel
> --
> |: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
> |: https://libvirt.org         -o-            https://fstop138.berrange.com :|
> |: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
>



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v1 1/1] QIOChannelSocket: Fix zero-copy send so socket flush works
  2022-06-08 20:26 ` Peter Xu
@ 2022-06-08 20:55   ` Peter Xu
  2022-06-08 21:00     ` Leonardo Bras Soares Passos
  0 siblings, 1 reply; 7+ messages in thread
From: Peter Xu @ 2022-06-08 20:55 UTC (permalink / raw)
  To: Leonardo Bras
  Cc: Daniel P. Berrangé, qemu-devel, 徐闯,
	Juan Quintela, Dr. David Alan Gilbert

On Wed, Jun 08, 2022 at 04:26:10PM -0400, Peter Xu wrote:
> On Wed, Jun 08, 2022 at 03:18:09PM -0300, Leonardo Bras wrote:
> > Somewhere between v6 and v7 the of the zero-copy-send patchset a crucial
> > part of the flushing mechanism got missing: incrementing zero_copy_queued.
> > 
> > Without that, the flushing interface becomes a no-op, and there is no
> > garantee the buffer is really sent.
> > 
> > This can go as bad as causing a corruption in RAM during migration.
> > 
> > Fixes: 2bc58ffc2926 ("QIOChannelSocket: Implement io_writev zero copy flag & io_flush for CONFIG_LINUX")
> > Reported-by: 徐闯 <xuchuangxclwt@bytedance.com>
> > Signed-off-by: Leonardo Bras <leobras@redhat.com>
> 
> Copy Dave/Juan; Leo please remember to do so in the next posts, or no one
> will be picking this up. :)

My fault, it's an io channel patch.  But still good to copy relevant
developers..

-- 
Peter Xu



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v1 1/1] QIOChannelSocket: Fix zero-copy send so socket flush works
  2022-06-08 20:55   ` Peter Xu
@ 2022-06-08 21:00     ` Leonardo Bras Soares Passos
  0 siblings, 0 replies; 7+ messages in thread
From: Leonardo Bras Soares Passos @ 2022-06-08 21:00 UTC (permalink / raw)
  To: Peter Xu
  Cc: Daniel P. Berrangé, qemu-devel, 徐闯,
	Juan Quintela, Dr. David Alan Gilbert

On Wed, Jun 8, 2022 at 5:55 PM Peter Xu <peterx@redhat.com> wrote:
>
> On Wed, Jun 08, 2022 at 04:26:10PM -0400, Peter Xu wrote:
> > On Wed, Jun 08, 2022 at 03:18:09PM -0300, Leonardo Bras wrote:
> > > Somewhere between v6 and v7 the of the zero-copy-send patchset a crucial
> > > part of the flushing mechanism got missing: incrementing zero_copy_queued.
> > >
> > > Without that, the flushing interface becomes a no-op, and there is no
> > > garantee the buffer is really sent.
> > >
> > > This can go as bad as causing a corruption in RAM during migration.
> > >
> > > Fixes: 2bc58ffc2926 ("QIOChannelSocket: Implement io_writev zero copy flag & io_flush for CONFIG_LINUX")
> > > Reported-by: 徐闯 <xuchuangxclwt@bytedance.com>
> > > Signed-off-by: Leonardo Bras <leobras@redhat.com>
> >
> > Copy Dave/Juan; Leo please remember to do so in the next posts, or no one
> > will be picking this up. :)
>

Thanks for letting me know.

> My fault, it's an io channel patch.  But still good to copy relevant
> developers..

Np. Sure, I will keep in mind to add them in the next version.

Oh, BTW: I will be sending a v2 shortly.

>
> --
> Peter Xu
>



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-06-08 21:02 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-08 18:18 [PATCH v1 1/1] QIOChannelSocket: Fix zero-copy send so socket flush works Leonardo Bras
2022-06-08 18:46 ` Daniel P. Berrangé
2022-06-08 20:25   ` Peter Xu
2022-06-08 20:48   ` Leonardo Bras Soares Passos
2022-06-08 20:26 ` Peter Xu
2022-06-08 20:55   ` Peter Xu
2022-06-08 21:00     ` Leonardo Bras Soares Passos

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.