Skip to contentSkip to conversation

RFC: add memfd memory backend

PATCH Superseded [email protected] 3 participants 12 messages Apr 12, 2016 Arm STM32

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

cover RFC: add memfd memory backend

From: Marc-André Lureau <[email protected]>

Add a new memory backend, similar to hostmem-file, except that it doesn't need a file path, or hugepages directory permissions. It also try to enforce memory sealing if available.

This backend is mainly useful for easily sharing the memory with other processes, for example with vhost-user backend, when hugepages aren't available (for security reasons or other limitations).

Marc-André Lureau (9):
exec: check kvm mmu notifiers earlier
exec: split file_ram_alloc()
exec: split qemu_ram_alloc_from_file()
Add memory_region_init_ram_from_fd()
RFC: ivshmem: use ram_from_fd()
exec: remove qemu_set_ram_fd()
memfd: split qemu_memfd_alloc()
Add memfd based hostmem
tests: use memfd in vhost-user-test

 backends/Makefile.objs   |   1 +
 backends/hostmem-memfd.c |  85 ++++++++++++++++++++++++++++++++++++++
 exec.c                   | 103 +++++++++++++++++++++++++++--------------------
 hw/misc/ivshmem.c        |  13 +++---
 include/exec/memory.h    |  20 +++++++++
 include/exec/ram_addr.h  |   4 +-
 include/qemu/memfd.h     |   1 +
 memory.c                 |  16 ++++++++
 qemu-options.hx          |  12 ++++++
 tests/vhost-user-test.c  |  12 +++---
 util/memfd.c             |  19 ++++++---
 11 files changed, 222 insertions(+), 64 deletions(-)
 create mode 100644 backends/hostmem-memfd.c
Signature · 1 line
2.5.5
#1 lore raw

PATCH 1/9 exec: check kvm mmu notifiers earlier

From: Marc-André Lureau <[email protected]>

Move kvm mmu notifiers check before calling file_ram_alloc(). This shouldn't change the validation logic.

---
 exec.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
exec.c +6 -6
index c4f9036..dcc6632 100644
@@ -1242,12 +1242,6 @@ static void *file_ram_alloc(RAMBlock *block,
     int fd = -1;
     int64_t page_size;
 
-    if (kvm_enabled() && !kvm_has_sync_mmu()) {
-        error_setg(errp,
-                   "host lacks kvm mmu notifiers, -mem-path unsupported");
-        return NULL;
-    }
-
     for (;;) {
         fd = open(path, O_RDWR);
         if (fd >= 0) {
@@ -1675,6 +1669,12 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr,
         return NULL;
     }
 
+    if (kvm_enabled() && !kvm_has_sync_mmu()) {
+        error_setg(errp,
+                   "host lacks kvm mmu notifiers, -mem-path unsupported");
+        return NULL;
+    }
+
     size = HOST_PAGE_ALIGN(size);
     new_block = g_malloc0(sizeof(*new_block));
     new_block->mr = mr;
Signature · 1 line
2.5.5
#2 lore raw

PATCH 2/9 exec: split file_ram_alloc()

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

Move file opening part in a seperate function file_ram_open(). This allows for reuse of file_ram_alloc() with only a fd.

---
 exec.c | 58 ++++++++++++++++++++++++++++++++++------------------------
 1 file changed, 34 insertions(+), 24 deletions(-)
exec.c +34 -24
index dcc6632..422ea3e 100644
@@ -1229,19 +1229,17 @@ void qemu_mutex_unlock_ramlist(void)
 }
 
 #ifdef __linux__
-static void *file_ram_alloc(RAMBlock *block,
-                            ram_addr_t memory,
-                            const char *path,
-                            Error **errp)
+static int file_ram_open(const char *path,
+                         const char *region_name,
+                         bool *created,
+                         Error **errp)
 {
-    bool unlink_on_error = false;
-    char *filename;
     char *sanitized_name;
+    char *filename;
     char *c;
-    void *area;
     int fd = -1;
-    int64_t page_size;
 
+    *created = false;
     for (;;) {
         fd = open(path, O_RDWR);
         if (fd >= 0) {
@@ -1252,13 +1250,13 @@ static void *file_ram_alloc(RAMBlock *block,
             /* @path names a file that doesn't exist, create it */
             fd = open(path, O_RDWR | O_CREAT | O_EXCL, 0644);
             if (fd >= 0) {
-                unlink_on_error = true;
+                *created = true;
                 break;
             }
         } else if (errno == EISDIR) {
             /* @path names a directory, create a file there */
             /* Make name safe to use with mkstemp by replacing '/' with '_'. */
-            sanitized_name = g_strdup(memory_region_name(block->mr));
+            sanitized_name = g_strdup(region_name);
             for (c = sanitized_name; *c != '\0'; c++) {
                 if (*c == '/') {
                     *c = '_';
@@ -1281,7 +1279,7 @@ static void *file_ram_alloc(RAMBlock *block,
             error_setg_errno(errp, errno,
                              "can't open backing store %s for guest RAM",
                              path);
-            goto error;
+            return -1;
         }
         /*
          * Try again on EINTR and EEXIST.  The latter happens when
@@ -1289,6 +1287,17 @@ static void *file_ram_alloc(RAMBlock *block,
          */
     }
 
+    return fd;
+}
+
+static void *file_ram_alloc(RAMBlock *block,
+                            ram_addr_t memory,
+                            int fd,
+                            Error **errp)
+{
+    void *area;
+    int64_t page_size;
+
     page_size = qemu_fd_getpagesize(fd);
     block->mr->align = page_size;
 
@@ -1296,7 +1305,7 @@ static void *file_ram_alloc(RAMBlock *block,
         error_setg(errp, "memory size 0x" RAM_ADDR_FMT " must be equal to "
                    "or larger than page size 0x%" PRIx64,
                    memory, page_size);
-        goto error;
+        return NULL;
     }
 
     memory = ROUND_UP(memory, page_size);
@@ -1315,7 +1324,7 @@ static void *file_ram_alloc(RAMBlock *block,
     if (area == MAP_FAILED) {
         error_setg_errno(errp, errno,
                          "unable to map backing store for guest RAM");
-        goto error;
+        return NULL;
     }
 
     if (mem_prealloc) {
@@ -1324,15 +1333,6 @@ static void *file_ram_alloc(RAMBlock *block,
 
     block->fd = fd;
     return area;
-
-error:
-    if (unlink_on_error) {
-        unlink(path);
-    }
-    if (fd != -1) {
-        close(fd);
-    }
-    return NULL;
 }
 #endif
 
@@ -1652,6 +1652,8 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr,
 {
     RAMBlock *new_block;
     Error *local_err = NULL;
+    int fd;
+    bool created;
 
     if (xen_enabled()) {
         error_setg(errp, "-mem-path not supported with Xen");
@@ -1675,15 +1677,23 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr,
         return NULL;
     }
 
+    fd = file_ram_open(mem_path, memory_region_name(mr), &created, errp);
+    if (fd < 0) {
+        return NULL;
+    }
+
     size = HOST_PAGE_ALIGN(size);
     new_block = g_malloc0(sizeof(*new_block));
     new_block->mr = mr;
     new_block->used_length = size;
     new_block->max_length = size;
     new_block->flags = share ? RAM_SHARED : 0;
-    new_block->host = file_ram_alloc(new_block, size,
-                                     mem_path, errp);
+    new_block->host = file_ram_alloc(new_block, size, fd, errp);
     if (!new_block->host) {
+        if (created) {
+            unlink(mem_path);
+        }
+        close(fd);
         g_free(new_block);
         return NULL;
     }
Signature · 1 line
2.5.5
#3 lore raw

PATCH 3/9 exec: split qemu_ram_alloc_from_file()

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

Add qemu_ram_alloc_from_fd(), which can be use to allocate ramblock from fd only.

---
 exec.c                  | 45 ++++++++++++++++++++++++++++++---------------
 include/exec/ram_addr.h |  3 +++
 2 files changed, 33 insertions(+), 15 deletions(-)
exec.c +30 -15
index 422ea3e..8675b5e 100644
@@ -1646,14 +1646,12 @@ static void ram_block_add(RAMBlock *new_block, Error **errp)
 }
 
 #ifdef __linux__
-RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr,
-                                   bool share, const char *mem_path,
-                                   Error **errp)
+RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr,
+                                 bool share, int fd,
+                                 Error **errp)
 {
     RAMBlock *new_block;
     Error *local_err = NULL;
-    int fd;
-    bool created;
 
     if (xen_enabled()) {
         error_setg(errp, "-mem-path not supported with Xen");
@@ -1677,11 +1675,6 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr,
         return NULL;
     }
 
-    fd = file_ram_open(mem_path, memory_region_name(mr), &created, errp);
-    if (fd < 0) {
-        return NULL;
-    }
-
     size = HOST_PAGE_ALIGN(size);
     new_block = g_malloc0(sizeof(*new_block));
     new_block->mr = mr;
@@ -1690,11 +1683,6 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr,
     new_block->flags = share ? RAM_SHARED : 0;
     new_block->host = file_ram_alloc(new_block, size, fd, errp);
     if (!new_block->host) {
-        if (created) {
-            unlink(mem_path);
-        }
-        close(fd);
-        g_free(new_block);
         return NULL;
     }
 
@@ -1705,6 +1693,33 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr,
         return NULL;
     }
     return new_block;
+
+}
+
+
+RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr,
+                                   bool share, const char *mem_path,
+                                   Error **errp)
+{
+    int fd;
+    bool created;
+    RAMBlock *block;
+
+    fd = file_ram_open(mem_path, memory_region_name(mr), &created, errp);
+    if (fd < 0) {
+        return NULL;
+    }
+
+    block = qemu_ram_alloc_from_fd(size, mr, share, fd, errp);
+    if (!block) {
+        if (created) {
+            unlink(mem_path);
+        }
+        close(fd);
+        return NULL;
+    }
+
+    return block;
 }
 #endif
 
include/exec/ram_addr.h +3 -0
index 5adf7a4..153f1b2 100644
@@ -97,6 +97,9 @@ void qemu_mutex_unlock_ramlist(void);
 RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr,
                                    bool share, const char *mem_path,
                                    Error **errp);
+RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr,
+                                 bool share, int fd,
+                                 Error **errp);
 RAMBlock *qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
                                   MemoryRegion *mr, Error **errp);
 RAMBlock *qemu_ram_alloc(ram_addr_t size, MemoryRegion *mr, Error **errp);
Signature · 1 line
2.5.5
#4 lore raw

PATCH 4/9 Add memory_region_init_ram_from_fd()

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

Add a new function to Initialize a RAM memory region with a mmap-ed file descriptor.

---
 include/exec/memory.h | 20 ++++++++++++++++++++
 memory.c              | 16 ++++++++++++++++
 2 files changed, 36 insertions(+)
include/exec/memory.h +20 -0
index e2a3e99..017bfdd 100644
@@ -399,6 +399,26 @@ void memory_region_init_ram_from_file(MemoryRegion *mr,
                                       bool share,
                                       const char *path,
                                       Error **errp);
+
+/**
+ * memory_region_init_ram_from_fd:  Initialize RAM memory region with a
+ *                                  mmap-ed backend.
+ *
+ * @mr: the #MemoryRegion to be initialized.
+ * @owner: the object that tracks the region's reference count
+ * @name: the name of the region.
+ * @size: size of the region.
+ * @share: %true if memory must be mmaped with the MAP_SHARED flag
+ * @fd: the fd to mmap.
+ * @errp: pointer to Error*, to store an error if it happens.
+ */
+void memory_region_init_ram_from_fd(MemoryRegion *mr,
+                                    struct Object *owner,
+                                    const char *name,
+                                    uint64_t size,
+                                    bool share,
+                                    int fd,
+                                    Error **errp);
 #endif
 
 /**
memory.c +16 -0
index f76f85d..dad99a9 100644
@@ -1351,6 +1351,22 @@ void memory_region_init_ram_from_file(MemoryRegion *mr,
     mr->ram_block = qemu_ram_alloc_from_file(size, mr, share, path, errp);
     mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
 }
+
+void memory_region_init_ram_from_fd(MemoryRegion *mr,
+                                    struct Object *owner,
+                                    const char *name,
+                                    uint64_t size,
+                                    bool share,
+                                    int fd,
+                                    Error **errp)
+{
+    memory_region_init(mr, owner, name, size);
+    mr->ram = true;
+    mr->terminates = true;
+    mr->destructor = memory_region_destructor_ram;
+    mr->ram_block = qemu_ram_alloc_from_fd(size, mr, share, fd, errp);
+    mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
+}
 #endif
 
 void memory_region_init_ram_ptr(MemoryRegion *mr,
Signature · 1 line
2.5.5
#5 lore raw

PATCH 5/9 RFC: ivshmem: use ram_from_fd()

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

Instead of writing our own mmap handling code, reuse the code from exec.c.

RFC because memory_region_init_ram_from_fd() adds some restrictions (check for xen, kvm sync-mmu, etc) and changes (such as size alignment). In practice, this doesn't seem to hurt though.

---
 hw/misc/ivshmem.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
hw/misc/ivshmem.c +5 -8
index 2eb8668..08bf5c6 100644
@@ -494,9 +494,9 @@ static void setup_interrupt(IVShmemState *s, int vector, Error **errp)
 
 static void process_msg_shmem(IVShmemState *s, int fd, Error **errp)
 {
+    Error *err = NULL;
     struct stat buf;
     size_t size;
-    void *ptr;
 
     if (s->ivshmem_bar2) {
         error_setg(errp, "server sent unexpected shared memory message");
@@ -525,15 +525,12 @@ static void process_msg_shmem(IVShmemState *s, int fd, Error **errp)
     }
 
     /* mmap the region and map into the BAR2 */
-    ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
-    if (ptr == MAP_FAILED) {
-        error_setg_errno(errp, errno, "Failed to mmap shared memory");
-        close(fd);
+    memory_region_init_ram_from_fd(&s->server_bar2, OBJECT(s),
+                                   "ivshmem.bar2", size, true, fd, &err);
+    if (err) {
+        error_propagate(errp, err);
         return;
     }
-    memory_region_init_ram_ptr(&s->server_bar2, OBJECT(s),
-                               "ivshmem.bar2", size, ptr);
-    qemu_set_ram_fd(memory_region_get_ram_addr(&s->server_bar2), fd);
     s->ivshmem_bar2 = &s->server_bar2;
 }
 
Signature · 1 line
2.5.5
#6 lore raw

PATCH 6/9 exec: remove qemu_set_ram_fd()

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

If the previous ivshmem patch is applied, qemu_set_ram_fd() is no longer used.

---
 exec.c                  | 10 ----------
 include/exec/ram_addr.h |  1 -
 2 files changed, 11 deletions(-)
exec.c +0 -10
index 8675b5e..ec72064 100644
@@ -1873,16 +1873,6 @@ int qemu_get_ram_fd(ram_addr_t addr)
     return fd;
 }
 
-void qemu_set_ram_fd(ram_addr_t addr, int fd)
-{
-    RAMBlock *block;
-
-    rcu_read_lock();
-    block = qemu_get_ram_block(addr);
-    block->fd = fd;
-    rcu_read_unlock();
-}
-
 void *qemu_get_ram_block_host_ptr(ram_addr_t addr)
 {
     RAMBlock *block;
include/exec/ram_addr.h +0 -1
index 153f1b2..3a21aee 100644
@@ -109,7 +109,6 @@ RAMBlock *qemu_ram_alloc_resizeable(ram_addr_t size, ram_addr_t max_size,
                                                     void *host),
                                     MemoryRegion *mr, Error **errp);
 int qemu_get_ram_fd(ram_addr_t addr);
-void qemu_set_ram_fd(ram_addr_t addr, int fd);
 void *qemu_get_ram_block_host_ptr(ram_addr_t addr);
 void qemu_ram_free(RAMBlock *block);
 
Signature · 1 line
2.5.5
#7 lore raw

PATCH 7/9 memfd: split qemu_memfd_alloc()

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

---
 include/qemu/memfd.h |  1 +
 util/memfd.c         | 19 ++++++++++++++-----
 2 files changed, 15 insertions(+), 5 deletions(-)
include/qemu/memfd.h +1 -0
index 745a8c5..41c24d8 100644
@@ -16,6 +16,7 @@
 #define F_SEAL_WRITE    0x0008  /* prevent writes */
 #endif
 
+int qemu_memfd_create(const char *name, size_t size, unsigned int seals);
 void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals,
                        int *fd);
 void qemu_memfd_free(void *ptr, size_t size, int fd);
util/memfd.c +14 -5
index 7c40691..31aaced 100644
@@ -64,14 +64,10 @@ static int memfd_create(const char *name, unsigned int flags)
  * memfd with sealing, but may fallback on other methods without
  * sealing.
  */
-void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals,
-                       int *fd)
+int qemu_memfd_create(const char *name, size_t size, unsigned int seals)
 {
-    void *ptr;
     int mfd = -1;
 
-    *fd = -1;
-
 #ifdef CONFIG_LINUX
     if (seals) {
         mfd = memfd_create(name, MFD_ALLOW_SEALING | MFD_CLOEXEC);
@@ -117,6 +113,19 @@ void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals,
         }
     }
 
+    return mfd;
+}
+
+void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals,
+                       int *fd)
+{
+    void *ptr;
+    int mfd = qemu_memfd_create(name, size, seals);
+
+    if (mfd == -1) {
+        return NULL;
+    }
+
     ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0);
     if (ptr == MAP_FAILED) {
         perror("mmap");
Signature · 1 line
2.5.5
#8 lore raw

PATCH 8/9 Add memfd based hostmem

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

Add a new memory backend, similar to hostmem-file, except that it doesn't need a file path, or hugepages directory permissions. It also try to enforce memory sealing if available.

This backend is mainly useful for easily sharing the memory with other processes, for example with vhost-user backend, when hugepages aren't available (for security reasons and other limitations).

Usage:
-object memory-backend-memfd,id=mem1,size=1G

---
 backends/Makefile.objs   |  1 +
 backends/hostmem-memfd.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++
 qemu-options.hx          | 12 +++++++
 3 files changed, 98 insertions(+)
 create mode 100644 backends/hostmem-memfd.c
backends/Makefile.objs +1 -0
index 31a3a89..32cfcdb 100644
@@ -9,3 +9,4 @@ common-obj-$(CONFIG_TPM) += tpm.o
 
 common-obj-y += hostmem.o hostmem-ram.o
 common-obj-$(CONFIG_LINUX) += hostmem-file.o
+common-obj-$(CONFIG_POSIX) += hostmem-memfd.o
backends/hostmem-memfd.c new file +85 -0

85 lines not on this page · raw message

qemu-options.hx +12 -0
index 587de8f..a54af0f 100644
@@ -3756,6 +3756,18 @@ The @option{share} boolean option determines whether the memory
 region is marked as private to QEMU, or shared. The latter allows
 a co-operating external process to access the QEMU memory region.
 
+@item -object memory-backend-memfd,id=@var{id},size=@var{size}
+
+Creates an anonymous memory file backend object, which can be used to
+share the memory with a co-operating external process. The memory is
+created with memfd and sealing if possible, but has fallback mechanism
+for systems that lack it.
+
+The @option{id} parameter is a unique ID that will be used to
+reference this memory region when configuring the @option{-numa}
+argument. The @option{size} option provides the size of the memory
+region, and accepts common suffixes, eg @option{500M}.
+
 @item -object rng-random,id=@var{id},filename=@var{/dev/random}
 
 Creates a random number generator backend which obtains entropy from
Signature · 1 line
2.5.5
#9 lore raw

Re: [Qemu-devel] [PATCH 8/9] Add memfd based hostmem

Quoted 6 lines · On Tue, 12 Apr 2016 13:55:28 +0200 [email protected] wrote:

From: Marc-André Lureau <[email protected]>

Add a new memory backend, similar to hostmem-file, except that it doesn't need a file path, or hugepages directory permissions. It also try to enforce memory sealing if available.

Quoted 6 lines

This backend is mainly useful for easily sharing the memory with other processes, for example with vhost-user backend, when hugepages aren't available (for security reasons and other limitations).

Usage:
-object memory-backend-memfd,id=mem1,size=1G

Does it make sense if it's possible to reuse/extend memory-backend-file, i.e. make 'mem-path' optional and behave like this patch
and also add 'memfd' property so that parent process could pass its handle to QEMU as well?

Quoted 132 lines
---
 backends/Makefile.objs   |  1 +
 backends/hostmem-memfd.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++
 qemu-options.hx          | 12 +++++++
 3 files changed, 98 insertions(+)
 create mode 100644 backends/hostmem-memfd.c
backends/Makefile.objs +1 -0
index 31a3a89..32cfcdb 100644
@@ -9,3 +9,4 @@ common-obj-$(CONFIG_TPM) += tpm.o
 
 common-obj-y += hostmem.o hostmem-ram.o
 common-obj-$(CONFIG_LINUX) += hostmem-file.o
+common-obj-$(CONFIG_POSIX) += hostmem-memfd.o
backends/hostmem-memfd.c new file +85 -0
new file mode 100644
index 0000000..e0e18d7
@@ -0,0 +1,85 @@
+/*
+ * QEMU host memfd memory backend
+ *
+ * Copyright (C) 2016 Red Hat Inc
+ *
+ * Authors:
+ *   Marc-André Lureau <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+#include "qemu/osdep.h"
+#include "qemu-common.h"
+#include "sysemu/hostmem.h"
+#include "sysemu/sysemu.h"
+#include "qom/object_interfaces.h"
+#include "qemu/memfd.h"
+#include "qapi/error.h"
+
+/**
+ * @TYPE_MEMORY_BACKEND_MEMFD:
+ * name of the memory backend that uses memfd mmap
+ */
+#define TYPE_MEMORY_BACKEND_MEMFD "memory-backend-memfd"
+
+#define MEMORY_BACKEND_MEMFD(obj) \
+    OBJECT_CHECK(HostMemoryBackendMemfd, (obj), TYPE_MEMORY_BACKEND_MEMFD)
+
+typedef struct HostMemoryBackendMemfd HostMemoryBackendMemfd;
+
+struct HostMemoryBackendMemfd {
+    HostMemoryBackend parent_obj;
+
+    int fd;
+};
+
+static void
+memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
+{
+    int fd;
+
+    if (!backend->size) {
+        error_setg(errp, "can't create backend with size 0");
+        return;
+    }
+#ifndef CONFIG_LINUX
+    error_setg(errp, "memfd not supported on this host");
+#else
+    if (!memory_region_size(&backend->mr)) {
+        backend->force_prealloc = mem_prealloc;
+        fd = qemu_memfd_create(TYPE_MEMORY_BACKEND_MEMFD,
+                               backend->size,
+                               F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL);
+        if (fd == -1) {
+            error_setg(errp, "can't allocate memfd backend");
+            return;
+        }
+        memory_region_init_ram_from_fd(&backend->mr, OBJECT(backend),
+            object_get_canonical_path(OBJECT(backend)),
+            backend->size, true, fd, errp);
+    }
+#endif
+}
+
+static void
+memfd_backend_class_init(ObjectClass *oc, void *data)
+{
+    HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
+
+    bc->alloc = memfd_backend_memory_alloc;
+}
+
+static const TypeInfo memfd_backend_info = {
+    .name = TYPE_MEMORY_BACKEND_MEMFD,
+    .parent = TYPE_MEMORY_BACKEND,
+    .class_init = memfd_backend_class_init,
+    .instance_size = sizeof(HostMemoryBackendMemfd),
+};
+
+static void register_types(void)
+{
+    type_register_static(&memfd_backend_info);
+}
+
+type_init(register_types);
qemu-options.hx +12 -0
index 587de8f..a54af0f 100644
@@ -3756,6 +3756,18 @@ The @option{share} boolean option determines whether the memory
 region is marked as private to QEMU, or shared. The latter allows
 a co-operating external process to access the QEMU memory region.
 
+@item -object memory-backend-memfd,id=@var{id},size=@var{size}
+
+Creates an anonymous memory file backend object, which can be used to
+share the memory with a co-operating external process. The memory is
+created with memfd and sealing if possible, but has fallback mechanism
+for systems that lack it.
+
+The @option{id} parameter is a unique ID that will be used to
+reference this memory region when configuring the @option{-numa}
+argument. The @option{size} option provides the size of the memory
+region, and accepts common suffixes, eg @option{500M}.
+
 @item -object rng-random,id=@var{id},filename=@var{/dev/random}
 
 Creates a random number generator backend which obtains entropy from
#10 lore raw

Re: [Qemu-devel] [PATCH 8/9] Add memfd based hostmem

----- Original Message -----

Quoted 13 lines
Quoted 10 lines · On Tue, 12 Apr 2016 13:55:28 +0200 [email protected] wrote:

From: Marc-André Lureau <[email protected]>

Add a new memory backend, similar to hostmem-file, except that it doesn't need a file path, or hugepages directory permissions. It also try to enforce memory sealing if available.

This backend is mainly useful for easily sharing the memory with other processes, for example with vhost-user backend, when hugepages aren't available (for security reasons and other limitations).

Quoted 6 lines
Quoted 2 lines

Usage:
-object memory-backend-memfd,id=mem1,size=1G

Does it make sense if it's possible to reuse/extend memory-backend-file, i.e. make 'mem-path' optional and behave like this patch
and also add 'memfd' property so that parent process could pass its handle to QEMU as well?

I don't mind, I would be a bit worried by the combination of options and the expected result though.

Quoted 137 lines
Quoted 135 lines
---
 backends/Makefile.objs   |  1 +
 backends/hostmem-memfd.c | 85
 ++++++++++++++++++++++++++++++++++++++++++++++++
 qemu-options.hx          | 12 +++++++
 3 files changed, 98 insertions(+)
 create mode 100644 backends/hostmem-memfd.c
backends/Makefile.objs +1 -0
index 31a3a89..32cfcdb 100644
@@ -9,3 +9,4 @@ common-obj-$(CONFIG_TPM) += tpm.o
 
 common-obj-y += hostmem.o hostmem-ram.o
 common-obj-$(CONFIG_LINUX) += hostmem-file.o
+common-obj-$(CONFIG_POSIX) += hostmem-memfd.o
backends/hostmem-memfd.c new file +9 -0
new file mode 100644
index 0000000..e0e18d7
@@ -0,0 +1,85 @@
+/*
+ * QEMU host memfd memory backend
+ *
+ * Copyright (C) 2016 Red Hat Inc
+ *
+ * Authors:
+ *   Marc-André Lureau <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or

later.
+ * See the COPYING file in the top-level directory.
+ */

backends/hostmem-memfd.c new file +74 -0
+#include "qemu/osdep.h"
+#include "qemu-common.h"
+#include "sysemu/hostmem.h"
+#include "sysemu/sysemu.h"
+#include "qom/object_interfaces.h"
+#include "qemu/memfd.h"
+#include "qapi/error.h"
+
+/**
+ * @TYPE_MEMORY_BACKEND_MEMFD:
+ * name of the memory backend that uses memfd mmap
+ */
+#define TYPE_MEMORY_BACKEND_MEMFD "memory-backend-memfd"
+
+#define MEMORY_BACKEND_MEMFD(obj) \
+    OBJECT_CHECK(HostMemoryBackendMemfd, (obj), TYPE_MEMORY_BACKEND_MEMFD)
+
+typedef struct HostMemoryBackendMemfd HostMemoryBackendMemfd;
+
+struct HostMemoryBackendMemfd {
+    HostMemoryBackend parent_obj;
+
+    int fd;
+};
+
+static void
+memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
+{
+    int fd;
+
+    if (!backend->size) {
+        error_setg(errp, "can't create backend with size 0");
+        return;
+    }
+#ifndef CONFIG_LINUX
+    error_setg(errp, "memfd not supported on this host");
+#else
+    if (!memory_region_size(&backend->mr)) {
+        backend->force_prealloc = mem_prealloc;
+        fd = qemu_memfd_create(TYPE_MEMORY_BACKEND_MEMFD,
+                               backend->size,
+                               F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL);
+        if (fd == -1) {
+            error_setg(errp, "can't allocate memfd backend");
+            return;
+        }
+        memory_region_init_ram_from_fd(&backend->mr, OBJECT(backend),
+            object_get_canonical_path(OBJECT(backend)),
+            backend->size, true, fd, errp);
+    }
+#endif
+}
+
+static void
+memfd_backend_class_init(ObjectClass *oc, void *data)
+{
+    HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
+
+    bc->alloc = memfd_backend_memory_alloc;
+}
+
+static const TypeInfo memfd_backend_info = {
+    .name = TYPE_MEMORY_BACKEND_MEMFD,
+    .parent = TYPE_MEMORY_BACKEND,
+    .class_init = memfd_backend_class_init,
+    .instance_size = sizeof(HostMemoryBackendMemfd),
+};
+
+static void register_types(void)
+{
+    type_register_static(&memfd_backend_info);
+}
+
+type_init(register_types);
qemu-options.hx +0 -0
index 587de8f..a54af0f 100644
@@ -3756,6 +3756,18 @@ The @option{share} boolean option determines whether

the memory
region is marked as private to QEMU, or shared. The latter allows a co-operating external process to access the QEMU memory region.

+@item -object memory-backend-memfd,id=@var{id},size=@var{size}

qemu-options.hx +11 -0
+
+Creates an anonymous memory file backend object, which can be used to
+share the memory with a co-operating external process. The memory is
+created with memfd and sealing if possible, but has fallback mechanism
+for systems that lack it.
+
+The @option{id} parameter is a unique ID that will be used to
+reference this memory region when configuring the @option{-numa}
+argument. The @option{size} option provides the size of the memory
+region, and accepts common suffixes, eg @option{500M}.
+
 @item -object rng-random,id=@var{id},filename=@var{/dev/random}
 
 Creates a random number generator backend which obtains entropy from
#11 lore raw

PATCH 9/9 tests: use memfd in vhost-user-test

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

This will exercise the memfd memory backend and should generally be better for testing than memory-backend-file (thanks to anonymous files and sealing).

---
 tests/vhost-user-test.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
tests/vhost-user-test.c +6 -6
index 6961596..6ef6c48 100644
@@ -32,8 +32,8 @@
 #endif
 
 #define QEMU_CMD_ACCEL  " -machine accel=tcg"
-#define QEMU_CMD_MEM    " -m %d -object memory-backend-file,id=mem,size=%dM,"\
-                        "mem-path=%s,share=on -numa node,memdev=mem"
+#define QEMU_CMD_MEM    " -m %d -object memory-backend-memfd,id=mem,size=%dM"\
+                        " -numa node,memdev=mem"
 #define QEMU_CMD_CHR    " -chardev socket,id=%s,path=%s"
 #define QEMU_CMD_NETDEV " -netdev vhost-user,id=net0,chardev=%s,vhostforce"
 #define QEMU_CMD_NET    " -device virtio-net-pci,netdev=net0,romfile=./pc-bios/pxe-virtio.rom"
@@ -383,12 +383,12 @@ static TestServer *test_server_new(const gchar *name)
     return server;
 }
 
-#define GET_QEMU_CMD(s)                                                        \
-    g_strdup_printf(QEMU_CMD, 512, 512, (root), (s)->chr_name,                 \
+#define GET_QEMU_CMD(s)                                 \
+    g_strdup_printf(QEMU_CMD, 512, 512, (s)->chr_name,  \
                     (s)->socket_path, (s)->chr_name)
 
-#define GET_QEMU_CMDE(s, mem, extra, ...)                                      \
-    g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name,       \
+#define GET_QEMU_CMDE(s, mem, extra, ...)                               \
+    g_strdup_printf(QEMU_CMD extra, (mem), (mem), (s)->chr_name,        \
                     (s)->socket_path, (s)->chr_name, ##__VA_ARGS__)
 
 static gboolean _test_server_free(TestServer *server)
Signature · 1 line
2.5.5