Skip to contentSkip to conversation

In memory QEMUFile

PATCH Superseded Dr. David Alan Gilbert (git) 2 participants 6 messages Sep 17, 2014 – Sep 29, 2014

v4 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v4 cover In memory QEMUFile

From: "Dr. David Alan Gilbert" <[email protected]>

Hi,
This patch-pair adds the QEMUSizedBuffer based in-memory QEMUFile
written by Stefan Berger and Joel Schopp. I've made some
fixes and modified the existing test-vmstate to use it for some test cases.

While there's nothing other than test cases using it yet, I think
it's worth going in by itself, since I'm using it in two separate patchsets (postcopy and visitor/BER) and Sanidhya uses it in the periodic vmstate test world. In addition both microcheckpointing and COLO have similar but independent implementations (although they both have some extra-gotcha's so it might not be possible to reuse it), and there was another implementation of the same thing in the Yabusame Postcopy world. Thus it seems best to put in, if only to stop people writing yet another implementation.

Dave

v4:
Fix very silly mistake in qsb_grow ENOMEM check

v3:
Mostly addressing comments from Eric and Gonglei

rewrote qsb_grow to always use _try_ for memory allocation
also made it use MAX_CHUNK_SIZE as needed
made QSB_MAX_CHUNK_SIZE 16x rather than 10x CHUNK_SIZE

reworked qsb_get_buffer to always take an allocated buffer

qsb_create:
Cope with 0 length but passed buffer Rework so it can fail on memory allocation failures

qsb_write_at
return -EINVAL for invalid position
qsb_clone
Cope with qsb_write_at failing

Typo: 'of of' in comment
'withing'
Reword qsb_clone 'exact copy'->'deep copy'

Dr. David Alan Gilbert (2):
QEMUSizedBuffer based QEMUFile
Tests: QEMUSizedBuffer/QEMUBuffer

 include/migration/qemu-file.h |  28 +++
 include/qemu/typedefs.h       |   1 +
 qemu-file.c                   | 457 ++++++++++++++++++++++++++++++++++++++++++
 tests/Makefile                |   2 +-
 tests/test-vmstate.c          |  74 +++----
 5 files changed, 525 insertions(+), 37 deletions(-)
Signature · 1 line
1.9.3
#1 lore raw

PATCH v4 1/2 QEMUSizedBuffer based QEMUFile

From: "Dr. David Alan Gilbert" <[email protected]>

This is based on Stefan and Joel's patch that creates a QEMUFile that goes to a memory buffer; from:

http://lists.gnu.org/archive/html/qemu-devel/2013-03/msg05036.html

Using the QEMUFile interface, this patch adds support functions for operating on in-memory sized buffers that can be written to or read from.

For fixes/tweeks I've done:

---
 include/migration/qemu-file.h |  28 +++
 include/qemu/typedefs.h       |   1 +
 qemu-file.c                   | 457 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 486 insertions(+)
include/migration/qemu-file.h +28 -0
index c90f529..6ef8ebc 100644
@@ -25,6 +25,8 @@
 #define QEMU_FILE_H 1
 #include "exec/cpu-common.h"
 
+#include <stdint.h>
+
 /* This function writes a chunk of data to a file at the given position.
  * The pos argument can be ignored if the file is only being used for
  * streaming.  The handler should try to write all of the data it can.
@@ -94,11 +96,21 @@ typedef struct QEMUFileOps {
     QEMURamSaveFunc *save_page;
 } QEMUFileOps;
 
+struct QEMUSizedBuffer {
+    struct iovec *iov;
+    size_t n_iov;
+    size_t size; /* total allocated size in all iov's */
+    size_t used; /* number of used bytes */
+};
+
+typedef struct QEMUSizedBuffer QEMUSizedBuffer;
+
 QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops);
 QEMUFile *qemu_fopen(const char *filename, const char *mode);
 QEMUFile *qemu_fdopen(int fd, const char *mode);
 QEMUFile *qemu_fopen_socket(int fd, const char *mode);
 QEMUFile *qemu_popen_cmd(const char *command, const char *mode);
+QEMUFile *qemu_bufopen(const char *mode, QEMUSizedBuffer *input);
 int qemu_get_fd(QEMUFile *f);
 int qemu_fclose(QEMUFile *f);
 int64_t qemu_ftell(QEMUFile *f);
@@ -111,6 +123,22 @@ void qemu_put_byte(QEMUFile *f, int v);
 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, int size);
 bool qemu_file_mode_is_not_valid(const char *mode);
 
+QEMUSizedBuffer *qsb_create(const uint8_t *buffer, size_t len);
+QEMUSizedBuffer *qsb_clone(const QEMUSizedBuffer *);
+void qsb_free(QEMUSizedBuffer *);
+size_t qsb_set_length(QEMUSizedBuffer *qsb, size_t length);
+size_t qsb_get_length(const QEMUSizedBuffer *qsb);
+ssize_t qsb_get_buffer(const QEMUSizedBuffer *, off_t start, size_t count,
+                       uint8_t *buf);
+ssize_t qsb_write_at(QEMUSizedBuffer *qsb, const uint8_t *buf,
+                     off_t pos, size_t count);
+
+
+/*
+ * For use on files opened with qemu_bufopen
+ */
+const QEMUSizedBuffer *qemu_buf_get(QEMUFile *f);
+
 static inline void qemu_put_ubyte(QEMUFile *f, unsigned int v)
 {
     qemu_put_byte(f, (int)v);
include/qemu/typedefs.h +1 -0
index 5f20b0e..db1153a 100644
@@ -60,6 +60,7 @@ typedef struct PCIEAERLog PCIEAERLog;
 typedef struct PCIEAERErr PCIEAERErr;
 typedef struct PCIEPort PCIEPort;
 typedef struct PCIESlot PCIESlot;
+typedef struct QEMUSizedBuffer QEMUSizedBuffer;
 typedef struct MSIMessage MSIMessage;
 typedef struct SerialState SerialState;
 typedef struct PCMCIACardState PCMCIACardState;
qemu-file.c +457 -0

460 lines not on this page · raw message

Signature · 1 line
1.9.3
#2 lore raw

Re: [Qemu-devel] [PATCH v4 1/2] QEMUSizedBuffer based QEMUFile

Quoted 6 lines · Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

This is based on Stefan and Joel's patch that creates a QEMUFile that goes to a memory buffer; from:

http://lists.gnu.org/archive/html/qemu-devel/2013-03/msg05036.html

Sheesh - a year and a half ago, and still not ready to merge.

Quoted 19 lines

Using the QEMUFile interface, this patch adds support functions for operating on in-memory sized buffers that can be written to or read from.

For fixes/tweeks I've done:

---
 include/migration/qemu-file.h |  28 +++
 include/qemu/typedefs.h       |   1 +
 qemu-file.c                   | 457 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 486 insertions(+)
qemu-file.c +2 -0
+QEMUSizedBuffer *qsb_create(const uint8_t *buffer, size_t len)
 
+    for (i = 0; i < num_chunks; i++) {

qemu-file.c uint64_t qemu_get_be64(QEMUFile *f) · line 931

Quoted 10 lines
qemu-file.c +10 -0
+        qsb->iov[i].iov_base = g_try_malloc0(chunk_size);
+        if (!qsb->iov[i].iov_base) {
+            size_t j;
+
+            for (j = 0; j < i; j++) {
+                g_free(qsb->iov[j].iov_base);
+            }
+            g_free(qsb->iov);
+            g_free(qsb);
+            return NULL;

Rather than inlining all this cleanup, you could just call qsb_free(qsb). But I can live with this version too.

Quoted 3 lines

+/**
+ * Grow the QEMUSizedBuffer to the given size and allocated
+ * memory for it.

s/allocated/allocate/ ?

Quoted 5 lines

+ *
+ * @qsb: A QEMUSizedBuffer
+ * @new_size: The new size of the buffer
+ *
+ * Returns an error code in case of memory allocation failure

s/an error code/a negative error code/ ?

Quoted 7 lines

+ * or the new size of the buffer otherwise. The returned size
+ * may be greater or equal to @new_size.
+ */

qemu-file.c +4 -0
+static ssize_t qsb_grow(QEMUSizedBuffer *qsb, size_t new_size)
+{
+    size_t needed_chunks, i;
+

qemu-file.c uint64_t qemu_get_be64(QEMUFile *f) · line 1119

Quoted 10 lines
qemu-file.c +10 -0
+    if (qsb->size < new_size) {
+        struct iovec *new_iov;
+        size_t size_diff = new_size - qsb->size;
+        size_t chunk_size = (size_diff > QSB_MAX_CHUNK_SIZE)
+                             ? QSB_MAX_CHUNK_SIZE : QSB_CHUNK_SIZE;
+
+        needed_chunks = DIV_ROUND_UP(size_diff, chunk_size);
+
+        new_iov = g_try_malloc_n(qsb->n_iov + needed_chunks,
+                               sizeof(struct iovec));

Indentation is off.

qemu-file.c uint64_t qemu_get_be64(QEMUFile *f) · line 1293

Quoted 8 lines
qemu-file.c +8 -0
+
+const QEMUSizedBuffer *qemu_buf_get(QEMUFile *f)
+{
+    QEMUBuffer *p;
+
+    qemu_fflush(f);
+
+    p = (QEMUBuffer *)f->opaque;

Cast is not necessary (this is C, after all, not C++).

qemu-file.c uint64_t qemu_get_be64(QEMUFile *f) · line 1301

Quoted 8 lines
qemu-file.c +8 -0
+
+    return p->qsb;
+}
+
+static const QEMUFileOps buf_read_ops = {
+    .get_buffer = buf_get_buffer,
+    .close =      buf_close
+};

I think we prefer trailing commas, if only so that future additions don't have to modify existing lines.

qemu-file.c uint64_t qemu_get_be64(QEMUFile *f) · line 1306

Quoted 5 lines
qemu-file.c +5 -0
+
+static const QEMUFileOps buf_write_ops = {
+    .put_buffer = buf_put_buffer,
+    .close =      buf_close
+};

and again.

qemu-file.c uint64_t qemu_get_be64(QEMUFile *f) · line 1312

Quoted 6 lines
qemu-file.c +6 -0
+
+QEMUFile *qemu_bufopen(const char *mode, QEMUSizedBuffer *input)
+{
+    QEMUBuffer *s;
+
+    if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {

I prefer '\0' over 0 when comparing characters. Or shorthand such as '|| *mode[1]'. But it works as is.

Quoted 5 lines
qemu-file.c +5 -0
+        error_report("qemu_bufopen: Argument validity check failed");
+        return NULL;
+    }
+
+    s = g_malloc0(sizeof(QEMUBuffer));

qemu-file.c uint64_t qemu_get_be64(QEMUFile *f) · line 1327

Quoted 10 lines
qemu-file.c +10 -0
+    if (mode[0] == 'r') {
+        s->qsb = input;
+    }
+
+    if (s->qsb == NULL) {
+        s->qsb = qsb_create(NULL, 0);
+    }
+    if (!s->qsb) {
+        error_report("qemu_bufopen: qsb_create failed");
+        return NULL;

Memory leak of s.

qemu-file.c uint64_t qemu_get_be64(QEMUFile *f) · line 1337

Quoted 11 lines
qemu-file.c +10 -0
+    }
+
+
+    if (mode[0] == 'r') {
+        s->file = qemu_fopen_ops(s, &buf_read_ops);
+    } else {
+        s->file = qemu_fopen_ops(s, &buf_write_ops);
+    }
+    return s->file;
+}

Closer; most of my findings are minor, but the memleak needs fixing. If the only changes you make are in relation to my findings, then v5 can start life with Reviewed-by: Eric Blake <[email protected]>

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#3 lore raw

Re: [Qemu-devel] [PATCH v4 1/2] QEMUSizedBuffer based QEMUFile

Quoted 9 lines · * Eric Blake ([email protected]) wrote:
Quoted 6 lines · Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

This is based on Stefan and Joel's patch that creates a QEMUFile that goes to a memory buffer; from:

http://lists.gnu.org/archive/html/qemu-devel/2013-03/msg05036.html

Sheesh - a year and a half ago, and still not ready to merge.

Yes; I'm dragging it screaming, hopefully over the line.

Quoted 29 lines
Quoted 18 lines

Using the QEMUFile interface, this patch adds support functions for operating on in-memory sized buffers that can be written to or read from.

For fixes/tweeks I've done:

---
 include/migration/qemu-file.h |  28 +++
 include/qemu/typedefs.h       |   1 +
 qemu-file.c                   | 457 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 486 insertions(+)
qemu-file.c +2 -0
+QEMUSizedBuffer *qsb_create(const uint8_t *buffer, size_t len)
 
+    for (i = 0; i < num_chunks; i++) {

qemu-file.c uint64_t qemu_get_be64(QEMUFile *f) · line 931

Quoted 10 lines
qemu-file.c +10 -0
+        qsb->iov[i].iov_base = g_try_malloc0(chunk_size);
+        if (!qsb->iov[i].iov_base) {
+            size_t j;
+
+            for (j = 0; j < i; j++) {
+                g_free(qsb->iov[j].iov_base);
+            }
+            g_free(qsb->iov);
+            g_free(qsb);
+            return NULL;
Quoted 2 lines

Rather than inlining all this cleanup, you could just call qsb_free(qsb). But I can live with this version too.

Done; I was wary at first, but I checked and since qsb_free uses g_free, and g_free is defined to ignore NULL pointers, it's OK to be used at this point.

Quoted 5 lines
Quoted 3 lines

+/**
+ * Grow the QEMUSizedBuffer to the given size and allocated
+ * memory for it.

s/allocated/allocate/ ?

Fixed.

Quoted 8 lines
Quoted 6 lines

+ *
+ * @qsb: A QEMUSizedBuffer
+ * @new_size: The new size of the buffer
+ *
+ * Returns an error code in case of memory allocation failure

s/an error code/a negative error code/ ?

Done; reformatted that text to lay the 'or' out more clearly.

Quoted 11 lines
Quoted 11 lines

+ * or the new size of the buffer otherwise. The returned size
+ * may be greater or equal to @new_size.
+ */

qemu-file.c +8 -0
+static ssize_t qsb_grow(QEMUSizedBuffer *qsb, size_t new_size)
+{
+    size_t needed_chunks, i;
+
+    if (qsb->size < new_size) {
+        struct iovec *new_iov;
+        size_t size_diff = new_size - qsb->size;
+        size_t chunk_size = (size_diff > QSB_MAX_CHUNK_SIZE)
Quoted 8 lines

qemu-file.c uint64_t qemu_get_be64(QEMUFile *f) · line 1119

Quoted 6 lines
qemu-file.c +6 -0
+                             ? QSB_MAX_CHUNK_SIZE : QSB_CHUNK_SIZE;
+
+        needed_chunks = DIV_ROUND_UP(size_diff, chunk_size);
+
+        new_iov = g_try_malloc_n(qsb->n_iov + needed_chunks,
+                               sizeof(struct iovec));

Indentation is off.

Done.

Quoted 10 lines

qemu-file.c uint64_t qemu_get_be64(QEMUFile *f) · line 1293

Quoted 8 lines
qemu-file.c +8 -0
+
+const QEMUSizedBuffer *qemu_buf_get(QEMUFile *f)
+{
+    QEMUBuffer *p;
+
+    qemu_fflush(f);
+
+    p = (QEMUBuffer *)f->opaque;

Cast is not necessary (this is C, after all, not C++).

Done.

Quoted 12 lines

qemu-file.c uint64_t qemu_get_be64(QEMUFile *f) · line 1301

Quoted 8 lines
qemu-file.c +8 -0
+
+    return p->qsb;
+}
+
+static const QEMUFileOps buf_read_ops = {
+    .get_buffer = buf_get_buffer,
+    .close =      buf_close
+};

I think we prefer trailing commas, if only so that future additions don't have to modify existing lines.

Quoted 7 lines

qemu-file.c uint64_t qemu_get_be64(QEMUFile *f) · line 1306

Quoted 5 lines
qemu-file.c +5 -0
+
+static const QEMUFileOps buf_write_ops = {
+    .put_buffer = buf_put_buffer,
+    .close =      buf_close
+};

and again.

Done.

Quoted 10 lines

qemu-file.c uint64_t qemu_get_be64(QEMUFile *f) · line 1312

Quoted 7 lines
qemu-file.c +6 -0
+
+QEMUFile *qemu_bufopen(const char *mode, QEMUSizedBuffer *input)
+{
+    QEMUBuffer *s;
+
+    if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {

I prefer '\0' over 0 when comparing characters. Or shorthand such as '|| *mode[1]'. But it works as is.

Done as '\0'

Quoted 9 lines
Quoted 9 lines
qemu-file.c +9 -0
+        error_report("qemu_bufopen: Argument validity check failed");
+        return NULL;
+    }
+
+    s = g_malloc0(sizeof(QEMUBuffer));
+    if (mode[0] == 'r') {
+        s->qsb = input;
+    }
+
Quoted 8 lines

qemu-file.c uint64_t qemu_get_be64(QEMUFile *f) · line 1327

Quoted 6 lines
qemu-file.c +6 -0
+    if (s->qsb == NULL) {
+        s->qsb = qsb_create(NULL, 0);
+    }
+    if (!s->qsb) {
+        error_report("qemu_bufopen: qsb_create failed");
+        return NULL;

Memory leak of s.

Fixed.

Quoted 7 lines
Quoted 7 lines
qemu-file.c +7 -0
+    }
+
+
+    if (mode[0] == 'r') {
+        s->file = qemu_fopen_ops(s, &buf_read_ops);
+    } else {
+        s->file = qemu_fopen_ops(s, &buf_write_ops);
Quoted 8 lines

qemu-file.c uint64_t qemu_get_be64(QEMUFile *f) · line 1337

Quoted 4 lines
qemu-file.c +3 -0
+    }
+    return s->file;
+}

Closer; most of my findings are minor, but the memleak needs fixing. If the only changes you make are in relation to my findings, then v5 can start life with Reviewed-by: Eric Blake <[email protected]>

Thanks! I'll get that posted shortly, so please just check it over.

Dave

Quoted 4 lines
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

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

#4 lore raw

PATCH v4 2/2 Tests: QEMUSizedBuffer/QEMUBuffer

In reply to Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

Modify some of tests/test-vmstate.c to use the in memory file based on QEMUSizedBuffer to provide basic testing of QEMUSizedBuffer and the associated memory backed QEMUFile type.

Only some of the tests are changed so that the fd backed QEMUFile is still tested.

---
 tests/Makefile       |  2 +-
 tests/test-vmstate.c | 74 +++++++++++++++++++++++++++-------------------------
 2 files changed, 39 insertions(+), 37 deletions(-)
tests/Makefile +1 -1
index d5db97b..b661c4b 100644
@@ -258,7 +258,7 @@ tests/test-qdev-global-props$(EXESUF): tests/test-qdev-global-props.o \
 	libqemuutil.a libqemustub.a
 tests/test-vmstate$(EXESUF): tests/test-vmstate.o \
 	vmstate.o qemu-file.o \
-	libqemuutil.a
+	libqemuutil.a libqemustub.a
 
 tests/test-qapi-types.c tests/test-qapi-types.h :\
 $(SRC_PATH)/tests/qapi-schema/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-types.py
tests/test-vmstate.c +38 -36

143 lines not on this page · raw message

Signature · 1 line
1.9.3
Reviewed-by on v4 2/2
#5 lore raw

Re: [Qemu-devel] [PATCH v4 2/2] Tests: QEMUSizedBuffer/QEMUBuffer

Quoted 6 lines · Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

Modify some of tests/test-vmstate.c to use the in memory file based on QEMUSizedBuffer to provide basic testing of QEMUSizedBuffer and the associated memory backed QEMUFile type.

Quoted 9 lines

Only some of the tests are changed so that the fd backed QEMUFile is still tested.

---
 tests/Makefile       |  2 +-
 tests/test-vmstate.c | 74 +++++++++++++++++++++++++++-------------------------
 2 files changed, 39 insertions(+), 37 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org