Skip to contentSkip to conversation

migration/qemu-file: cleanup and refine qemu-file

PATCH In review Wei Yang 4 participants 13 messages Jul 31, 2019 – Sep 11, 2019

lore
Share & tools
lore

Wei Yang

author
#0 lore raw

cover migration/qemu-file: cleanup and refine qemu-file

Two cleanup:

Patch #1 make code consistent on calling add_to_iovec Patch #2 refine the code to handle the case when buf already flushed

Wei Yang (2):
migration/qemu-file: remove check on writev_buffer in
qemu_put_compression_data
migration/qemu-file: fix potential buf waste for extra buf_index
adjustment

 migration/qemu-file.c | 44 +++++++++++++++++++++++++++----------------
 1 file changed, 28 insertions(+), 16 deletions(-)
Signature · 1 line
2.17.1
author
#1 lore raw

PATCH 1/2 migration/qemu-file: remove check on writev_buffer in qemu_put_compression_data

The check of writev_buffer is in qemu_fflush, which means it is not harmful if it is NULL.

And removing it will make the code consistent since all other add_to_iovec() is called without the check.

---
 migration/qemu-file.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
migration/qemu-file.c +1 -3
index 0431585502..35c22605dd 100644
@@ -717,9 +717,7 @@ ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream,
     }
 
     qemu_put_be32(f, blen);
-    if (f->ops->writev_buffer) {
-        add_to_iovec(f, f->buf + f->buf_index, blen, false);
-    }
+    add_to_iovec(f, f->buf + f->buf_index, blen, false);
     f->buf_index += blen;
     if (f->buf_index == IO_BUF_SIZE) {
         qemu_fflush(f);
Signature · 1 line
2.17.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] migration/qemu-file: remove check on writev_buffer in qemu_put_compression_data

Quoted 7 lines · * Wei Yang ([email protected]) wrote:

The check of writev_buffer is in qemu_fflush, which means it is not harmful if it is NULL.

And removing it will make the code consistent since all other add_to_iovec() is called without the check.

So I think the real reason here is commit 11808bb which removed support for non-iovec writers.

Quoted 22 lines
---
 migration/qemu-file.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
migration/qemu-file.c +1 -3
index 0431585502..35c22605dd 100644
@@ -717,9 +717,7 @@ ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream,
     }
 
     qemu_put_be32(f, blen);
-    if (f->ops->writev_buffer) {
-        add_to_iovec(f, f->buf + f->buf_index, blen, false);
-    }
+    add_to_iovec(f, f->buf + f->buf_index, blen, false);
     f->buf_index += blen;
     if (f->buf_index == IO_BUF_SIZE) {
         qemu_fflush(f);
Signature · 1 line
2.17.1

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

author
#3 lore raw

PATCH 2/2 migration/qemu-file: fix potential buf waste for extra buf_index adjustment

In reply to Wei Yang

In add_to_iovec(), qemu_fflush() will be called if iovec is full. If this happens, buf_index is reset. Currently, this is not checked and buf_index would always been adjust with buf size.

This is not harmful, but will waste some space in file buffer.

This patch make add_to_iovec() return 1 when it has flushed the file. Then the caller could check the return value to see whether it is necessary to adjust the buf_index any more.

---
 migration/qemu-file.c | 42 ++++++++++++++++++++++++++++--------------
 1 file changed, 28 insertions(+), 14 deletions(-)
migration/qemu-file.c +28 -14
index 35c22605dd..05d9f42ddb 100644
@@ -343,8 +343,16 @@ int qemu_fclose(QEMUFile *f)
     return ret;
 }
 
-static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
-                         bool may_free)
+/*
+ * Add buf to iovec. Do flush if iovec is full.
+ *
+ * Return values:
+ * 1 iovec is full and flushed
+ * 0 iovec is not flushed
+ *
+ */
+static int add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
+                        bool may_free)
 {
     /* check for adjacent buffer and coalesce them */
     if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
@@ -362,7 +370,10 @@ static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
 
     if (f->iovcnt >= MAX_IOV_SIZE) {
         qemu_fflush(f);
+        return 1;
     }
+
+    return 0;
 }
 
 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size,
@@ -391,10 +402,11 @@ void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size)
         }
         memcpy(f->buf + f->buf_index, buf, l);
         f->bytes_xfer += l;
-        add_to_iovec(f, f->buf + f->buf_index, l, false);
-        f->buf_index += l;
-        if (f->buf_index == IO_BUF_SIZE) {
-            qemu_fflush(f);
+        if (!add_to_iovec(f, f->buf + f->buf_index, l, false)) {
+            f->buf_index += l;
+            if (f->buf_index == IO_BUF_SIZE) {
+                qemu_fflush(f);
+            }
         }
         if (qemu_file_get_error(f)) {
             break;
@@ -412,10 +424,11 @@ void qemu_put_byte(QEMUFile *f, int v)
 
     f->buf[f->buf_index] = v;
     f->bytes_xfer++;
-    add_to_iovec(f, f->buf + f->buf_index, 1, false);
-    f->buf_index++;
-    if (f->buf_index == IO_BUF_SIZE) {
-        qemu_fflush(f);
+    if (!add_to_iovec(f, f->buf + f->buf_index, 1, false)) {
+        f->buf_index++;
+        if (f->buf_index == IO_BUF_SIZE) {
+            qemu_fflush(f);
+        }
     }
 }
 
@@ -717,10 +730,11 @@ ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream,
     }
 
     qemu_put_be32(f, blen);
-    add_to_iovec(f, f->buf + f->buf_index, blen, false);
-    f->buf_index += blen;
-    if (f->buf_index == IO_BUF_SIZE) {
-        qemu_fflush(f);
+    if (!add_to_iovec(f, f->buf + f->buf_index, blen, false)) {
+        f->buf_index += blen;
+        if (f->buf_index == IO_BUF_SIZE) {
+            qemu_fflush(f);
+        }
     }
     return blen + sizeof(int32_t);
 }
Signature · 1 line
2.17.1
Reviewed-by on v1 2/2
#4 lore raw

Re: [Qemu-devel] [PATCH 2/2] migration/qemu-file: fix potential buf waste for extra buf_index adjustment

(Copying Dan in)

Quoted 5 lines · * Wei Yang ([email protected]) wrote:

In add_to_iovec(), qemu_fflush() will be called if iovec is full. If this happens, buf_index is reset. Currently, this is not checked and buf_index would always been adjust with buf size.

This is not harmful, but will waste some space in file buffer.

That's a nice find.

Quoted 5 lines

This patch make add_to_iovec() return 1 when it has flushed the file. Then the caller could check the return value to see whether it is necessary to adjust the buf_index any more.

(I wonder if there's a way to wrap that little add_to_iovec, check, add to index, flush in a little wrapper).

Dave

Quoted 89 lines
---
 migration/qemu-file.c | 42 ++++++++++++++++++++++++++++--------------
 1 file changed, 28 insertions(+), 14 deletions(-)
migration/qemu-file.c +28 -14
index 35c22605dd..05d9f42ddb 100644
@@ -343,8 +343,16 @@ int qemu_fclose(QEMUFile *f)
     return ret;
 }
 
-static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
-                         bool may_free)
+/*
+ * Add buf to iovec. Do flush if iovec is full.
+ *
+ * Return values:
+ * 1 iovec is full and flushed
+ * 0 iovec is not flushed
+ *
+ */
+static int add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
+                        bool may_free)
 {
     /* check for adjacent buffer and coalesce them */
     if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
@@ -362,7 +370,10 @@ static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
 
     if (f->iovcnt >= MAX_IOV_SIZE) {
         qemu_fflush(f);
+        return 1;
     }
+
+    return 0;
 }
 
 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size,
@@ -391,10 +402,11 @@ void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size)
         }
         memcpy(f->buf + f->buf_index, buf, l);
         f->bytes_xfer += l;
-        add_to_iovec(f, f->buf + f->buf_index, l, false);
-        f->buf_index += l;
-        if (f->buf_index == IO_BUF_SIZE) {
-            qemu_fflush(f);
+        if (!add_to_iovec(f, f->buf + f->buf_index, l, false)) {
+            f->buf_index += l;
+            if (f->buf_index == IO_BUF_SIZE) {
+                qemu_fflush(f);
+            }
         }
         if (qemu_file_get_error(f)) {
             break;
@@ -412,10 +424,11 @@ void qemu_put_byte(QEMUFile *f, int v)
 
     f->buf[f->buf_index] = v;
     f->bytes_xfer++;
-    add_to_iovec(f, f->buf + f->buf_index, 1, false);
-    f->buf_index++;
-    if (f->buf_index == IO_BUF_SIZE) {
-        qemu_fflush(f);
+    if (!add_to_iovec(f, f->buf + f->buf_index, 1, false)) {
+        f->buf_index++;
+        if (f->buf_index == IO_BUF_SIZE) {
+            qemu_fflush(f);
+        }
     }
 }
 
@@ -717,10 +730,11 @@ ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream,
     }
 
     qemu_put_be32(f, blen);
-    add_to_iovec(f, f->buf + f->buf_index, blen, false);
-    f->buf_index += blen;
-    if (f->buf_index == IO_BUF_SIZE) {
-        qemu_fflush(f);
+    if (!add_to_iovec(f, f->buf + f->buf_index, blen, false)) {
+        f->buf_index += blen;
+        if (f->buf_index == IO_BUF_SIZE) {
+            qemu_fflush(f);
+        }
     }
     return blen + sizeof(int32_t);
 }
Signature · 1 line
2.17.1

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#5 lore raw

Re: [Qemu-devel] [PATCH 2/2] migration/qemu-file: fix potential buf waste for extra buf_index adjustment

Quoted 15 lines · Dr. David Alan Gilbert

(Copying Dan in)

Quoted 5 lines · * Wei Yang ([email protected]) wrote:

In add_to_iovec(), qemu_fflush() will be called if iovec is full. If this happens, buf_index is reset. Currently, this is not checked and buf_index would always been adjust with buf size.

This is not harmful, but will waste some space in file buffer.

That's a nice find.

Quoted 4 lines

This patch make add_to_iovec() return 1 when it has flushed the file. Then the caller could check the return value to see whether it is necessary to adjust the buf_index any more.

Quoted 6 lines
Quoted 1 line

(I wonder if there's a way to wrap that little add_to_iovec, check, add to index, flush in a little wrapper).

Given the name "add_to_iovec" I think it is pretty surprising that it calls "qemu_flush" at all.

It is also pretty wierd that we're checking two different conditions in two different places.

Right now the code is essentially doing this:

     if (f->iovcnt >= MAX_IOV_SIZE) {
        qemu_fflush(f);
     }
     if (f->buf_index == IO_BUF_SIZE) {
        qemu_fflush(f);
     }

Except that in the qemu_put_buffer_async() case, we're only doing the first of these two checks. This feels very odd indeed - I would have thought either it should do both, or do neither.

Assuming doing both flushs is ok for qemu_put_buffer_async then I'd suggest renaming 'add_to_iovec' to 'queue_buffer' and have that method do both of these qemu_fflush() calls.

Quoted 83 lines
Quoted 83 lines
---
 migration/qemu-file.c | 42 ++++++++++++++++++++++++++++--------------
 1 file changed, 28 insertions(+), 14 deletions(-)
migration/qemu-file.c +28 -14
index 35c22605dd..05d9f42ddb 100644
@@ -343,8 +343,16 @@ int qemu_fclose(QEMUFile *f)
     return ret;
 }
 
-static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
-                         bool may_free)
+/*
+ * Add buf to iovec. Do flush if iovec is full.
+ *
+ * Return values:
+ * 1 iovec is full and flushed
+ * 0 iovec is not flushed
+ *
+ */
+static int add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
+                        bool may_free)
 {
     /* check for adjacent buffer and coalesce them */
     if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
@@ -362,7 +370,10 @@ static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
 
     if (f->iovcnt >= MAX_IOV_SIZE) {
         qemu_fflush(f);
+        return 1;
     }
+
+    return 0;
 }
 
 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size,
@@ -391,10 +402,11 @@ void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size)
         }
         memcpy(f->buf + f->buf_index, buf, l);
         f->bytes_xfer += l;
-        add_to_iovec(f, f->buf + f->buf_index, l, false);
-        f->buf_index += l;
-        if (f->buf_index == IO_BUF_SIZE) {
-            qemu_fflush(f);
+        if (!add_to_iovec(f, f->buf + f->buf_index, l, false)) {
+            f->buf_index += l;
+            if (f->buf_index == IO_BUF_SIZE) {
+                qemu_fflush(f);
+            }
         }
         if (qemu_file_get_error(f)) {
             break;
@@ -412,10 +424,11 @@ void qemu_put_byte(QEMUFile *f, int v)
 
     f->buf[f->buf_index] = v;
     f->bytes_xfer++;
-    add_to_iovec(f, f->buf + f->buf_index, 1, false);
-    f->buf_index++;
-    if (f->buf_index == IO_BUF_SIZE) {
-        qemu_fflush(f);
+    if (!add_to_iovec(f, f->buf + f->buf_index, 1, false)) {
+        f->buf_index++;
+        if (f->buf_index == IO_BUF_SIZE) {
+            qemu_fflush(f);
+        }
     }
 }
 
@@ -717,10 +730,11 @@ ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream,
     }
 
     qemu_put_be32(f, blen);
-    add_to_iovec(f, f->buf + f->buf_index, blen, false);
-    f->buf_index += blen;
-    if (f->buf_index == IO_BUF_SIZE) {
-        qemu_fflush(f);
+    if (!add_to_iovec(f, f->buf + f->buf_index, blen, false)) {
+        f->buf_index += blen;
+        if (f->buf_index == IO_BUF_SIZE) {
+            qemu_fflush(f);
+        }
Quoted 9 lines
Quoted 6 lines
migration/qemu-file.c +0 -0
     }
     return blen + sizeof(int32_t);
 }
Signature · 1 line
2.17.1

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

Regards,
Daniel

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

Re: [Qemu-devel] [PATCH 2/2] migration/qemu-file: fix potential buf waste for extra buf_index adjustment

Quoted 38 lines · * Daniel P. Berrangé ([email protected]) wrote:
Quoted 15 lines · Dr. David Alan Gilbert

(Copying Dan in)

Quoted 5 lines · * Wei Yang ([email protected]) wrote:

In add_to_iovec(), qemu_fflush() will be called if iovec is full. If this happens, buf_index is reset. Currently, this is not checked and buf_index would always been adjust with buf size.

This is not harmful, but will waste some space in file buffer.

That's a nice find.

Quoted 4 lines

This patch make add_to_iovec() return 1 when it has flushed the file. Then the caller could check the return value to see whether it is necessary to adjust the buf_index any more.

Quoted 6 lines
Quoted 1 line

(I wonder if there's a way to wrap that little add_to_iovec, check, add to index, flush in a little wrapper).

Given the name "add_to_iovec" I think it is pretty surprising that it calls "qemu_flush" at all.

It is also pretty wierd that we're checking two different conditions in two different places.

Right now the code is essentially doing this:

     if (f->iovcnt >= MAX_IOV_SIZE) {
        qemu_fflush(f);
     }
     if (f->buf_index == IO_BUF_SIZE) {
        qemu_fflush(f);
     }
Quoted 4 lines

Except that in the qemu_put_buffer_async() case, we're only doing the first of these two checks. This feels very odd indeed - I would have thought either it should do both, or do neither.

No, there's two separate types of buffers.

There's f->buf which is a single allocated buffer in the QEMUFile with an offset buf_index, and there are arbitrary RAM pages added typically via qemu_put_buffer_async.

The check for >= IO_BUF_SIZE is only done when adding to the f->buf, where as the check on f->iovcnt is done when you add an element to the iovec and that can happen potentially in either case.

Dave

Quoted 97 lines

Assuming doing both flushs is ok for qemu_put_buffer_async then I'd suggest renaming 'add_to_iovec' to 'queue_buffer' and have that method do both of these qemu_fflush() calls.

Quoted 83 lines
Quoted 83 lines
---
 migration/qemu-file.c | 42 ++++++++++++++++++++++++++++--------------
 1 file changed, 28 insertions(+), 14 deletions(-)
migration/qemu-file.c +28 -14
index 35c22605dd..05d9f42ddb 100644
@@ -343,8 +343,16 @@ int qemu_fclose(QEMUFile *f)
     return ret;
 }
 
-static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
-                         bool may_free)
+/*
+ * Add buf to iovec. Do flush if iovec is full.
+ *
+ * Return values:
+ * 1 iovec is full and flushed
+ * 0 iovec is not flushed
+ *
+ */
+static int add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
+                        bool may_free)
 {
     /* check for adjacent buffer and coalesce them */
     if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
@@ -362,7 +370,10 @@ static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
 
     if (f->iovcnt >= MAX_IOV_SIZE) {
         qemu_fflush(f);
+        return 1;
     }
+
+    return 0;
 }
 
 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size,
@@ -391,10 +402,11 @@ void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size)
         }
         memcpy(f->buf + f->buf_index, buf, l);
         f->bytes_xfer += l;
-        add_to_iovec(f, f->buf + f->buf_index, l, false);
-        f->buf_index += l;
-        if (f->buf_index == IO_BUF_SIZE) {
-            qemu_fflush(f);
+        if (!add_to_iovec(f, f->buf + f->buf_index, l, false)) {
+            f->buf_index += l;
+            if (f->buf_index == IO_BUF_SIZE) {
+                qemu_fflush(f);
+            }
         }
         if (qemu_file_get_error(f)) {
             break;
@@ -412,10 +424,11 @@ void qemu_put_byte(QEMUFile *f, int v)
 
     f->buf[f->buf_index] = v;
     f->bytes_xfer++;
-    add_to_iovec(f, f->buf + f->buf_index, 1, false);
-    f->buf_index++;
-    if (f->buf_index == IO_BUF_SIZE) {
-        qemu_fflush(f);
+    if (!add_to_iovec(f, f->buf + f->buf_index, 1, false)) {
+        f->buf_index++;
+        if (f->buf_index == IO_BUF_SIZE) {
+            qemu_fflush(f);
+        }
     }
 }
 
@@ -717,10 +730,11 @@ ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream,
     }
 
     qemu_put_be32(f, blen);
-    add_to_iovec(f, f->buf + f->buf_index, blen, false);
-    f->buf_index += blen;
-    if (f->buf_index == IO_BUF_SIZE) {
-        qemu_fflush(f);
+    if (!add_to_iovec(f, f->buf + f->buf_index, blen, false)) {
+        f->buf_index += blen;
+        if (f->buf_index == IO_BUF_SIZE) {
+            qemu_fflush(f);
+        }
Quoted 9 lines
Quoted 6 lines
migration/qemu-file.c +0 -0
     }
     return blen + sizeof(int32_t);
 }
Signature · 1 line
2.17.1

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

Quoted 6 lines

Regards,
Daniel

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

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#7 lore raw

Re: [Qemu-devel] [PATCH 2/2] migration/qemu-file: fix potential buf waste for extra buf_index adjustment

Quoted 44 lines · Dr. David Alan Gilbert
Quoted 38 lines · * Daniel P. Berrang? ([email protected]) wrote:
Quoted 15 lines · Dr. David Alan Gilbert

(Copying Dan in)

Quoted 5 lines · * Wei Yang ([email protected]) wrote:

In add_to_iovec(), qemu_fflush() will be called if iovec is full. If this happens, buf_index is reset. Currently, this is not checked and buf_index would always been adjust with buf size.

This is not harmful, but will waste some space in file buffer.

That's a nice find.

Quoted 4 lines

This patch make add_to_iovec() return 1 when it has flushed the file. Then the caller could check the return value to see whether it is necessary to adjust the buf_index any more.

Quoted 6 lines
Quoted 1 line

(I wonder if there's a way to wrap that little add_to_iovec, check, add to index, flush in a little wrapper).

Given the name "add_to_iovec" I think it is pretty surprising that it calls "qemu_flush" at all.

It is also pretty wierd that we're checking two different conditions in two different places.

Right now the code is essentially doing this:

     if (f->iovcnt >= MAX_IOV_SIZE) {
        qemu_fflush(f);
     }
     if (f->buf_index == IO_BUF_SIZE) {
        qemu_fflush(f);
     }
Quoted 4 lines

Except that in the qemu_put_buffer_async() case, we're only doing the first of these two checks. This feels very odd indeed - I would have thought either it should do both, or do neither.

Quoted 6 lines

No, there's two separate types of buffers.

There's f->buf which is a single allocated buffer in the QEMUFile with an offset buf_index, and there are arbitrary RAM pages added typically via qemu_put_buffer_async.

qemu_put_buffer_async is the only one which put a range not in f->buf into the iovec.

And one thing confused me is even its name is async, add_to_iovec still would call qemu_fflush when iovec is full. So it is not a always async function. From the function name, it is a little difficult to differentiate qemu_put_buffer and qemu_put_buffer_async.

Quoted 111 lines

The check for >= IO_BUF_SIZE is only done when adding to the f->buf, where as the check on f->iovcnt is done when you add an element to the iovec and that can happen potentially in either case.

Dave

Quoted 97 lines

Assuming doing both flushs is ok for qemu_put_buffer_async then I'd suggest renaming 'add_to_iovec' to 'queue_buffer' and have that method do both of these qemu_fflush() calls.

Quoted 83 lines
Quoted 83 lines
---
 migration/qemu-file.c | 42 ++++++++++++++++++++++++++++--------------
 1 file changed, 28 insertions(+), 14 deletions(-)
migration/qemu-file.c +28 -14
index 35c22605dd..05d9f42ddb 100644
@@ -343,8 +343,16 @@ int qemu_fclose(QEMUFile *f)
     return ret;
 }
 
-static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
-                         bool may_free)
+/*
+ * Add buf to iovec. Do flush if iovec is full.
+ *
+ * Return values:
+ * 1 iovec is full and flushed
+ * 0 iovec is not flushed
+ *
+ */
+static int add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
+                        bool may_free)
 {
     /* check for adjacent buffer and coalesce them */
     if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
@@ -362,7 +370,10 @@ static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
 
     if (f->iovcnt >= MAX_IOV_SIZE) {
         qemu_fflush(f);
+        return 1;
     }
+
+    return 0;
 }
 
 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size,
@@ -391,10 +402,11 @@ void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size)
         }
         memcpy(f->buf + f->buf_index, buf, l);
         f->bytes_xfer += l;
-        add_to_iovec(f, f->buf + f->buf_index, l, false);
-        f->buf_index += l;
-        if (f->buf_index == IO_BUF_SIZE) {
-            qemu_fflush(f);
+        if (!add_to_iovec(f, f->buf + f->buf_index, l, false)) {
+            f->buf_index += l;
+            if (f->buf_index == IO_BUF_SIZE) {
+                qemu_fflush(f);
+            }
         }
         if (qemu_file_get_error(f)) {
             break;
@@ -412,10 +424,11 @@ void qemu_put_byte(QEMUFile *f, int v)
 
     f->buf[f->buf_index] = v;
     f->bytes_xfer++;
-    add_to_iovec(f, f->buf + f->buf_index, 1, false);
-    f->buf_index++;
-    if (f->buf_index == IO_BUF_SIZE) {
-        qemu_fflush(f);
+    if (!add_to_iovec(f, f->buf + f->buf_index, 1, false)) {
+        f->buf_index++;
+        if (f->buf_index == IO_BUF_SIZE) {
+            qemu_fflush(f);
+        }
     }
 }
 
@@ -717,10 +730,11 @@ ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream,
     }
 
     qemu_put_be32(f, blen);
-    add_to_iovec(f, f->buf + f->buf_index, blen, false);
-    f->buf_index += blen;
-    if (f->buf_index == IO_BUF_SIZE) {
-        qemu_fflush(f);
+    if (!add_to_iovec(f, f->buf + f->buf_index, blen, false)) {
+        f->buf_index += blen;
+        if (f->buf_index == IO_BUF_SIZE) {
+            qemu_fflush(f);
+        }
Quoted 9 lines
Quoted 6 lines
migration/qemu-file.c +0 -0
     }
     return blen + sizeof(int32_t);
 }
Signature · 1 line
2.17.1

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

Quoted 6 lines

Regards,
Daniel

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

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

Signature · 2 lines
Wei Yang
Help you, Help me
#8 lore raw

Re: [Qemu-devel] [PATCH 2/2] migration/qemu-file: fix potential buf waste for extra buf_index adjustment

Quoted 52 lines · * Wei Yang ([email protected]) wrote:
Quoted 44 lines · Dr. David Alan Gilbert
Quoted 38 lines · * Daniel P. Berrang? ([email protected]) wrote:
Quoted 15 lines · Dr. David Alan Gilbert

(Copying Dan in)

Quoted 5 lines · * Wei Yang ([email protected]) wrote:

In add_to_iovec(), qemu_fflush() will be called if iovec is full. If this happens, buf_index is reset. Currently, this is not checked and buf_index would always been adjust with buf size.

This is not harmful, but will waste some space in file buffer.

That's a nice find.

Quoted 4 lines

This patch make add_to_iovec() return 1 when it has flushed the file. Then the caller could check the return value to see whether it is necessary to adjust the buf_index any more.

Quoted 6 lines
Quoted 1 line

(I wonder if there's a way to wrap that little add_to_iovec, check, add to index, flush in a little wrapper).

Given the name "add_to_iovec" I think it is pretty surprising that it calls "qemu_flush" at all.

It is also pretty wierd that we're checking two different conditions in two different places.

Right now the code is essentially doing this:

     if (f->iovcnt >= MAX_IOV_SIZE) {
        qemu_fflush(f);
     }
     if (f->buf_index == IO_BUF_SIZE) {
        qemu_fflush(f);
     }
Quoted 4 lines

Except that in the qemu_put_buffer_async() case, we're only doing the first of these two checks. This feels very odd indeed - I would have thought either it should do both, or do neither.

Quoted 6 lines

No, there's two separate types of buffers.

There's f->buf which is a single allocated buffer in the QEMUFile with an offset buf_index, and there are arbitrary RAM pages added typically via qemu_put_buffer_async.

Quoted 7 lines

qemu_put_buffer_async is the only one which put a range not in f->buf into the iovec.

And one thing confused me is even its name is async, add_to_iovec still would call qemu_fflush when iovec is full. So it is not a always async function. From the function name, it is a little difficult to differentiate qemu_put_buffer and qemu_put_buffer_async.

It's a bit confusing, but I think it is the right name; the 'async' refers to the fact that the data is read from the buffer we pass sometime later, not synchronously before the return of the qemu_put_buffer_async call.

Dave

Quoted 103 lines
Quoted 103 lines

The check for >= IO_BUF_SIZE is only done when adding to the f->buf, where as the check on f->iovcnt is done when you add an element to the iovec and that can happen potentially in either case.

Dave

Quoted 97 lines

Assuming doing both flushs is ok for qemu_put_buffer_async then I'd suggest renaming 'add_to_iovec' to 'queue_buffer' and have that method do both of these qemu_fflush() calls.

Quoted 92 lines
Quoted 89 lines
---
 migration/qemu-file.c | 42 ++++++++++++++++++++++++++++--------------
 1 file changed, 28 insertions(+), 14 deletions(-)
migration/qemu-file.c +28 -14
index 35c22605dd..05d9f42ddb 100644
@@ -343,8 +343,16 @@ int qemu_fclose(QEMUFile *f)
     return ret;
 }
 
-static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
-                         bool may_free)
+/*
+ * Add buf to iovec. Do flush if iovec is full.
+ *
+ * Return values:
+ * 1 iovec is full and flushed
+ * 0 iovec is not flushed
+ *
+ */
+static int add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
+                        bool may_free)
 {
     /* check for adjacent buffer and coalesce them */
     if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
@@ -362,7 +370,10 @@ static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
 
     if (f->iovcnt >= MAX_IOV_SIZE) {
         qemu_fflush(f);
+        return 1;
     }
+
+    return 0;
 }
 
 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size,
@@ -391,10 +402,11 @@ void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size)
         }
         memcpy(f->buf + f->buf_index, buf, l);
         f->bytes_xfer += l;
-        add_to_iovec(f, f->buf + f->buf_index, l, false);
-        f->buf_index += l;
-        if (f->buf_index == IO_BUF_SIZE) {
-            qemu_fflush(f);
+        if (!add_to_iovec(f, f->buf + f->buf_index, l, false)) {
+            f->buf_index += l;
+            if (f->buf_index == IO_BUF_SIZE) {
+                qemu_fflush(f);
+            }
         }
         if (qemu_file_get_error(f)) {
             break;
@@ -412,10 +424,11 @@ void qemu_put_byte(QEMUFile *f, int v)
 
     f->buf[f->buf_index] = v;
     f->bytes_xfer++;
-    add_to_iovec(f, f->buf + f->buf_index, 1, false);
-    f->buf_index++;
-    if (f->buf_index == IO_BUF_SIZE) {
-        qemu_fflush(f);
+    if (!add_to_iovec(f, f->buf + f->buf_index, 1, false)) {
+        f->buf_index++;
+        if (f->buf_index == IO_BUF_SIZE) {
+            qemu_fflush(f);
+        }
     }
 }
 
@@ -717,10 +730,11 @@ ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream,
     }
 
     qemu_put_be32(f, blen);
-    add_to_iovec(f, f->buf + f->buf_index, blen, false);
-    f->buf_index += blen;
-    if (f->buf_index == IO_BUF_SIZE) {
-        qemu_fflush(f);
+    if (!add_to_iovec(f, f->buf + f->buf_index, blen, false)) {
+        f->buf_index += blen;
+        if (f->buf_index == IO_BUF_SIZE) {
+            qemu_fflush(f);
+        }
     }
     return blen + sizeof(int32_t);
 }
Signature · 1 line
2.17.1

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

Quoted 12 lines
Quoted 8 lines
Quoted 6 lines

Regards,
Daniel

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

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

Signature · 2 lines
Wei Yang
Help you, Help me

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#9 lore raw

Re: [Qemu-devel] [PATCH 2/2] migration/qemu-file: fix potential buf waste for extra buf_index adjustment

In reply to Dr. David Alan Gilbert
Quoted 61 lines · Dr. David Alan Gilbert

(Copying Dan in)

Quoted 5 lines · * Wei Yang ([email protected]) wrote:

In add_to_iovec(), qemu_fflush() will be called if iovec is full. If this happens, buf_index is reset. Currently, this is not checked and buf_index would always been adjust with buf size.

This is not harmful, but will waste some space in file buffer.

That's a nice find.

Quoted 5 lines

This patch make add_to_iovec() return 1 when it has flushed the file. Then the caller could check the return value to see whether it is necessary to adjust the buf_index any more.

(I wonder if there's a way to wrap that little add_to_iovec, check, add to index, flush in a little wrapper).

Dave

Quoted 37 lines
---
 migration/qemu-file.c | 42 ++++++++++++++++++++++++++++--------------
 1 file changed, 28 insertions(+), 14 deletions(-)
migration/qemu-file.c +13 -2
index 35c22605dd..05d9f42ddb 100644
@@ -343,8 +343,16 @@ int qemu_fclose(QEMUFile *f)
     return ret;
 }
 
-static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
-                         bool may_free)
+/*
+ * Add buf to iovec. Do flush if iovec is full.
+ *
+ * Return values:
+ * 1 iovec is full and flushed
+ * 0 iovec is not flushed
+ *
+ */
+static int add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
+                        bool may_free)
 {
     /* check for adjacent buffer and coalesce them */
     if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
@@ -362,7 +370,10 @@ static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
 
     if (f->iovcnt >= MAX_IOV_SIZE) {
         qemu_fflush(f);
+        return 1;
     }
+
+    return 0;
 }
 
Quoted 14 lines
Quoted 4 lines
migration/qemu-file.c +0 -0
 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size,
@@ -391,10 +402,11 @@ void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size)
         }
         memcpy(f->buf + f->buf_index, buf, l);

migration/qemu-file.c void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size) · line 409

Quoted 10 lines
migration/qemu-file.c +5 -4
         f->bytes_xfer += l;
-        add_to_iovec(f, f->buf + f->buf_index, l, false);
-        f->buf_index += l;
-        if (f->buf_index == IO_BUF_SIZE) {
-            qemu_fflush(f);
+        if (!add_to_iovec(f, f->buf + f->buf_index, l, false)) {
+            f->buf_index += l;
+            if (f->buf_index == IO_BUF_SIZE) {
+                qemu_fflush(f);
+            }

You mean put these four lines into a wrapper?

Name it as add_buf_to_iovec?

Quoted 40 lines
Quoted 38 lines
migration/qemu-file.c +10 -8
         }
         if (qemu_file_get_error(f)) {
             break;
@@ -412,10 +424,11 @@ void qemu_put_byte(QEMUFile *f, int v)
 
     f->buf[f->buf_index] = v;
     f->bytes_xfer++;
-    add_to_iovec(f, f->buf + f->buf_index, 1, false);
-    f->buf_index++;
-    if (f->buf_index == IO_BUF_SIZE) {
-        qemu_fflush(f);
+    if (!add_to_iovec(f, f->buf + f->buf_index, 1, false)) {
+        f->buf_index++;
+        if (f->buf_index == IO_BUF_SIZE) {
+            qemu_fflush(f);
+        }
     }
 }
 
@@ -717,10 +730,11 @@ ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream,
     }
 
     qemu_put_be32(f, blen);
-    add_to_iovec(f, f->buf + f->buf_index, blen, false);
-    f->buf_index += blen;
-    if (f->buf_index == IO_BUF_SIZE) {
-        qemu_fflush(f);
+    if (!add_to_iovec(f, f->buf + f->buf_index, blen, false)) {
+        f->buf_index += blen;
+        if (f->buf_index == IO_BUF_SIZE) {
+            qemu_fflush(f);
+        }
     }
     return blen + sizeof(int32_t);
 }
Signature · 1 line
2.17.1

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

Signature · 2 lines
Wei Yang
Help you, Help me
#10 lore raw

Re: [Qemu-devel] [PATCH 2/2] migration/qemu-file: fix potential buf waste for extra buf_index adjustment

Quoted 79 lines · * Wei Yang ([email protected]) wrote:
Quoted 61 lines · Dr. David Alan Gilbert

(Copying Dan in)

Quoted 5 lines · * Wei Yang ([email protected]) wrote:

In add_to_iovec(), qemu_fflush() will be called if iovec is full. If this happens, buf_index is reset. Currently, this is not checked and buf_index would always been adjust with buf size.

This is not harmful, but will waste some space in file buffer.

That's a nice find.

Quoted 5 lines

This patch make add_to_iovec() return 1 when it has flushed the file. Then the caller could check the return value to see whether it is necessary to adjust the buf_index any more.

(I wonder if there's a way to wrap that little add_to_iovec, check, add to index, flush in a little wrapper).

Dave

Quoted 37 lines
---
 migration/qemu-file.c | 42 ++++++++++++++++++++++++++++--------------
 1 file changed, 28 insertions(+), 14 deletions(-)
migration/qemu-file.c +13 -2
index 35c22605dd..05d9f42ddb 100644
@@ -343,8 +343,16 @@ int qemu_fclose(QEMUFile *f)
     return ret;
 }
 
-static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
-                         bool may_free)
+/*
+ * Add buf to iovec. Do flush if iovec is full.
+ *
+ * Return values:
+ * 1 iovec is full and flushed
+ * 0 iovec is not flushed
+ *
+ */
+static int add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
+                        bool may_free)
 {
     /* check for adjacent buffer and coalesce them */
     if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
@@ -362,7 +370,10 @@ static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
 
     if (f->iovcnt >= MAX_IOV_SIZE) {
         qemu_fflush(f);
+        return 1;
     }
+
+    return 0;
 }
 
Quoted 14 lines
Quoted 4 lines
migration/qemu-file.c +0 -0
 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size,
@@ -391,10 +402,11 @@ void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size)
         }
         memcpy(f->buf + f->buf_index, buf, l);

migration/qemu-file.c void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size) · line 409

Quoted 10 lines
migration/qemu-file.c +5 -4
         f->bytes_xfer += l;
-        add_to_iovec(f, f->buf + f->buf_index, l, false);
-        f->buf_index += l;
-        if (f->buf_index == IO_BUF_SIZE) {
-            qemu_fflush(f);
+        if (!add_to_iovec(f, f->buf + f->buf_index, l, false)) {
+            f->buf_index += l;
+            if (f->buf_index == IO_BUF_SIZE) {
+                qemu_fflush(f);
+            }

You mean put these four lines into a wrapper?

Quoted 1 line

Name it as add_buf_to_iovec?

Yes.

Dave

Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
migration/qemu-file.c +10 -8
         }
         if (qemu_file_get_error(f)) {
             break;
@@ -412,10 +424,11 @@ void qemu_put_byte(QEMUFile *f, int v)
 
     f->buf[f->buf_index] = v;
     f->bytes_xfer++;
-    add_to_iovec(f, f->buf + f->buf_index, 1, false);
-    f->buf_index++;
-    if (f->buf_index == IO_BUF_SIZE) {
-        qemu_fflush(f);
+    if (!add_to_iovec(f, f->buf + f->buf_index, 1, false)) {
+        f->buf_index++;
+        if (f->buf_index == IO_BUF_SIZE) {
+            qemu_fflush(f);
+        }
     }
 }
 
@@ -717,10 +730,11 @@ ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream,
     }
 
     qemu_put_be32(f, blen);
-    add_to_iovec(f, f->buf + f->buf_index, blen, false);
-    f->buf_index += blen;
-    if (f->buf_index == IO_BUF_SIZE) {
-        qemu_fflush(f);
+    if (!add_to_iovec(f, f->buf + f->buf_index, blen, false)) {
+        f->buf_index += blen;
+        if (f->buf_index == IO_BUF_SIZE) {
+            qemu_fflush(f);
+        }
Quoted 12 lines
Quoted 8 lines
Quoted 6 lines
migration/qemu-file.c +0 -0
     }
     return blen + sizeof(int32_t);
 }
Signature · 1 line
2.17.1

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

Signature · 2 lines
Wei Yang
Help you, Help me

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#11 lore raw

Re: [Qemu-devel] [PATCH 2/2] migration/qemu-file: fix potential buf waste for extra buf_index adjustment

Quoted 80 lines · Dr. David Alan Gilbert
Quoted 79 lines · * Wei Yang ([email protected]) wrote:
Quoted 61 lines · Dr. David Alan Gilbert

(Copying Dan in)

Quoted 5 lines · * Wei Yang ([email protected]) wrote:

In add_to_iovec(), qemu_fflush() will be called if iovec is full. If this happens, buf_index is reset. Currently, this is not checked and buf_index would always been adjust with buf size.

This is not harmful, but will waste some space in file buffer.

That's a nice find.

Quoted 5 lines

This patch make add_to_iovec() return 1 when it has flushed the file. Then the caller could check the return value to see whether it is necessary to adjust the buf_index any more.

(I wonder if there's a way to wrap that little add_to_iovec, check, add to index, flush in a little wrapper).

Dave

Quoted 37 lines
---
 migration/qemu-file.c | 42 ++++++++++++++++++++++++++++--------------
 1 file changed, 28 insertions(+), 14 deletions(-)
migration/qemu-file.c +13 -2
index 35c22605dd..05d9f42ddb 100644
@@ -343,8 +343,16 @@ int qemu_fclose(QEMUFile *f)
     return ret;
 }
 
-static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
-                         bool may_free)
+/*
+ * Add buf to iovec. Do flush if iovec is full.
+ *
+ * Return values:
+ * 1 iovec is full and flushed
+ * 0 iovec is not flushed
+ *
+ */
+static int add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
+                        bool may_free)
 {
     /* check for adjacent buffer and coalesce them */
     if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
@@ -362,7 +370,10 @@ static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
 
     if (f->iovcnt >= MAX_IOV_SIZE) {
         qemu_fflush(f);
+        return 1;
     }
+
+    return 0;
 }
 
Quoted 14 lines
Quoted 4 lines
migration/qemu-file.c +0 -0
 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size,
@@ -391,10 +402,11 @@ void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size)
         }
         memcpy(f->buf + f->buf_index, buf, l);

migration/qemu-file.c void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size) · line 409

Quoted 10 lines
migration/qemu-file.c +5 -4
         f->bytes_xfer += l;
-        add_to_iovec(f, f->buf + f->buf_index, l, false);
-        f->buf_index += l;
-        if (f->buf_index == IO_BUF_SIZE) {
-            qemu_fflush(f);
+        if (!add_to_iovec(f, f->buf + f->buf_index, l, false)) {
+            f->buf_index += l;
+            if (f->buf_index == IO_BUF_SIZE) {
+                qemu_fflush(f);
+            }

You mean put these four lines into a wrapper?

Quoted 3 lines
Quoted 1 line

Name it as add_buf_to_iovec?

Yes.

Sure, Let me prepare v2 with this.

Signature · 2 lines
Wei Yang
Help you, Help me
author
#12 lore raw
In reply to Wei Yang
Quoted 5 lines · Wei Yang

Two cleanup:

Patch #1 make code consistent on calling add_to_iovec Patch #2 refine the code to handle the case when buf already flushed

Ping~

Quoted 11 lines

Wei Yang (2):
migration/qemu-file: remove check on writev_buffer in
qemu_put_compression_data
migration/qemu-file: fix potential buf waste for extra buf_index
adjustment

migration/qemu-file.c | 44 +++++++++++++++++++++++++++----------------

1 file changed, 28 insertions(+), 16 deletions(-)
Signature · 1 line
2.17.1
Signature · 2 lines
Wei Yang
Help you, Help me