Add memfd memory backend
v3 cover Add memfd memory backend
Add a new Linux-specific memory backend, similar to hostmem-file, except that it doesn't need file path. It also try to enforce memory sealing if available. It is thus slightly easier and secure, and is compatible with transparent huge-pages since Linux 4.8.
The first 6 patches are mostly cleanups and could be considered regardless of memfd hostmem acceptability.
v3:
- make vhost-user-test use memfd only if possible
- rebased
v1->v2:
- make it linux-specific
- minor changes and commit message tweaks
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()
ivshmem: use ram_from_fd()
memory: remove memory_region_set_fd
memfd: split qemu_memfd_alloc()
Add memfd based hostmem
tests: use memfd in vhost-user-test
include/exec/memory.h | 31 ++++++++----- include/exec/ram_addr.h | 3 ++ include/qemu/memfd.h | 2 + backends/hostmem-memfd.c | 67 +++++++++++++++++++++++++++ exec.c | 116 ++++++++++++++++++++++++++++------------------- hw/misc/ivshmem.c | 14 +++--- memory.c | 26 +++++++---- tests/vhost-user-test.c | 68 +++++++++++++++++---------- util/memfd.c | 42 ++++++++++------- backends/Makefile.objs | 2 + qemu-options.hx | 11 +++++ 11 files changed, 266 insertions(+), 116 deletions(-) create mode 100644 backends/hostmem-memfd.c
Signature · 1 line
2.13.0.91.g00982b8dd
PATCH v3 1/9 exec: check kvm mmu notifiers earlier
Move kvm mmu notifiers check before calling file_ram_alloc(), with the other xen precondition. (file_ram_alloc() will be reused in other cases than -mem-path).
--- exec.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
exec.c +6 -6
@@ -1496,12 +1496,6 @@ static void *file_ram_alloc(RAMBlock *block,int fd =-1 ;int64_t file_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 ) {
@@ -1944,6 +1938,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 ; + } +if (phys_mem_alloc != qemu_anon_ram_alloc) {/* * file_ram_alloc() needs to allocate just like
Signature · 1 line
2.13.0.91.g00982b8dd
PATCH v3 2/9 exec: split file_ram_alloc()
In reply to Marc-André LureauMove file opening part in a seperate function, file_ram_open(). This allows for reuse of file_ram_alloc() with a given fd.
--- exec.c | 83 +++++++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 46 insertions(+), 37 deletions(-)
exec.c +46 -37
@@ -1483,19 +1483,17 @@ static int64_t get_file_size(int fd)return size; } -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 *c; -void *area =MAP_FAILED ;int fd =-1 ; -int64_t file_size; + *created = false;for (;;) { fd =open (path,O_RDWR );if (fd >=0 ) {
@@ -1506,13 +1504,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 ='_' ;
@@ -1535,7 +1533,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
@@ -1543,6 +1541,17 @@ static void *file_ram_alloc(RAMBlock *block,*/ } +return fd; +} + +static void *file_ram_alloc (RAMBlock *block, +ram_addr_t memory, +int fd, +bool truncate, +Error **errp) +{ +void *area; + block->page_size =qemu_fd_getpagesize (fd); block->mr->align = block->page_size;#if defined(__s390x__)
@@ -1551,20 +1560,11 @@ static void *file_ram_alloc(RAMBlock *block, }#endif - file_size =get_file_size (fd); -if (memory < block->page_size) {error_setg (errp,"memory size 0x" RAM_ADDR_FMT " must be equal to " "or larger than page size 0x%zx" , memory, block->page_size); - goto error; - } - -if (file_size >0 && file_size < memory) { -error_setg (errp,"backing store %s size 0x%" PRIx64 -" does not match 'size' option 0x" RAM_ADDR_FMT , - path, file_size, memory); - goto error; +return NULL ; } memory =ROUND_UP (memory, block->page_size);
@@ -1583,7 +1583,7 @@ static void *file_ram_alloc(RAMBlock *block,* those labels. Therefore, extending the non-empty backend file * is disabled as well. */ -if (!file_size &&ftruncate (fd, memory)) { +if (truncate &&ftruncate (fd, memory)) {perror ("ftruncate" ); }
@@ -1592,30 +1592,19 @@ 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) {os_mem_prealloc (fd, area, memory, smp_cpus, errp);if (errp && *errp) { - goto error; +qemu_ram_munmap (area, memory); +return NULL ; } } block->fd = fd; return area; - -error: -if (area !=MAP_FAILED ) { -qemu_ram_munmap (area, memory); - } -if (unlink_on_error) { -unlink (path); - } -if (fd !=-1 ) { -close (fd); - } - return NULL; }#endif
@@ -1932,6 +1921,9 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr, {RAMBlock *new_block;Error *local_err =NULL ; +int fd; +bool created; +int64_t file_size;if (xen_enabled ()) {error_setg (errp,"-mem-path not supported with Xen" );
@@ -1955,15 +1947,32 @@ 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); + file_size =get_file_size (fd); +if (file_size >0 && file_size < size) { +error_setg (errp,"backing store %s size 0x%" PRIx64 +" does not match 'size' option 0x" RAM_ADDR_FMT , + mem_path, file_size, size); +close (fd); +return NULL ; + } + 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, !file_size, errp);if (!new_block->host) { +if (created) { +unlink (mem_path); + } +close (fd);g_free (new_block);return NULL ; }
Signature · 1 line
2.13.0.91.g00982b8dd
PATCH v3 3/9 exec: split qemu_ram_alloc_from_file()
In reply to Marc-André LureauAdd qemu_ram_alloc_from_fd(), which can be use to allocate ramblock from fd only.
--- include/exec/ram_addr.h | 3 +++ exec.c | 45 ++++++++++++++++++++++++++++++--------------- 2 files changed, 33 insertions(+), 15 deletions(-)
include/exec/ram_addr.h +3 -0
@@ -65,6 +65,9 @@ unsigned long last_ram_page(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);
exec.c +30 -15
78 lines not on this page · raw message
Signature · 1 line
2.13.0.91.g00982b8dd
Re: [Qemu-devel] [PATCH v3 3/9] exec: split qemu_ram_alloc_from_file()
Quoted 46 lines · Marc-André Lureau
Add
qemu_ram_alloc_from_fd(), which can be use to allocate ramblock from fd only.Signed-off-by Marc-André Lureau <[email protected]>--- include/exec/ram_addr.h | 3 +++ exec.c | 45 ++++++++++++++++++++++++++++++--------------- 2 files changed, 33 insertions(+), 15 deletions(-)include/exec/ram_addr.h +3
-0@@ -65,6 +65,9 @@ unsigned long last_ram_page(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);exec.c +3
-5@@ -1915,14 +1915,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;int64_t file_size;if (xen_enabled ()) {
exec.c RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr, · line 1954
Quoted 8 lines
exec.c +0
-5@@ -1947,18 +1945,12 @@ 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 ; - } -
I like this, because it gets us one step closer to moving the file creation/opening logic in file_ram_open() to hostmem-file.c. Then both memfd and hostmem-file could use memory_region_init_ram_from_fd(), and we won't need memory_region_init_ram_from_file() anymore.
Quoted 58 lines
exec.c +27
-5size =HOST_PAGE_ALIGN (size); file_size =get_file_size (fd);if (file_size >0 && file_size < size) {error_setg (errp,"backing store %s size 0x%" PRIx64" does not match 'size' option 0x" RAM_ADDR_FMT , mem_path, file_size, size); -close (fd);return NULL ; }@@ -1969,10 +1961,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, !file_size, errp);if (!new_block->host) { -if (created) { -unlink (mem_path); - } -close (fd);g_free (new_block);return NULL ; }@@ -1984,6 +1972,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 Signature · 1 line
2.13.0.91.g00982b8dd
Signature · 1 line
Eduardo
PATCH v3 4/9 Add memory_region_init_ram_from_fd()
In reply to Marc-André LureauAdd a new function to initialize a RAM memory region with a file descriptor to be mmap-ed.
--- include/exec/memory.h | 20 ++++++++++++++++++++ memory.c | 16 ++++++++++++++++ 2 files changed, 36 insertions(+)
include/exec/memory.h +20 -0
@@ -458,6 +458,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
@@ -1397,6 +1397,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.13.0.91.g00982b8dd
PATCH v3 5/9 ivshmem: use ram_from_fd()
In reply to Marc-André LureauInstead of having its own mmap handling code, reuse the code from exec.c.
Note: memory_region_init_ram_from_fd() adds some restrictions (check for xen, kvm sync-mmu, etc) and changes (such as size alignment). This may actually be more correct.
--- hw/misc/ivshmem.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-)
hw/misc/ivshmem.c +6 -8
@@ -491,9 +491,9 @@ static void setup_interrupt(IVShmemState *s, int vector, Error **errp)static void process_msg_shmem (IVShmemState *s,int fd,Error **errp) { +Error *local_err =NULL ;struct stat buf;size_t size; -void *ptr;if (s->ivshmem_bar2) {error_setg (errp,"server sent unexpected shared memory message" );
@@ -522,15 +522,13 @@ 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, &local_err); +if (local_err) { +error_propagate (errp, local_err);return ; } -memory_region_init_ram_ptr (&s->server_bar2,OBJECT (s), -"ivshmem.bar2" , size, ptr); -memory_region_set_fd (&s->server_bar2, fd); + s->ivshmem_bar2 = &s->server_bar2; }
Signature · 1 line
2.13.0.91.g00982b8dd
Re: [Qemu-devel] [PATCH v3 5/9] ivshmem: use ram_from_fd()
Quoted 6 lines · Marc-André Lureau
Instead of having its own mmap handling code, reuse the code from
exec.c.Note:
memory_region_init_ram_from_fd()adds some restrictions (check for xen, kvm sync-mmu, etc) and changes (such as size alignment). This may actually be more correct.
I agree. I'm queuing patches 1-6 for now.
Paolo
PATCH v3 6/9 memory: remove memory_region_set_fd
In reply to Marc-André LureauNow unnecessary since ivshmem uses memory_region_init_ram_from_fd.
--- include/exec/memory.h | 11 ----------- memory.c | 10 ---------- 2 files changed, 21 deletions(-)
include/exec/memory.h +0 -11
@@ -827,17 +827,6 @@ static inline bool memory_region_is_rom(MemoryRegion *mr)int memory_region_get_fd (MemoryRegion *mr);/** -* memory_region_set_fd: Mark a RAM memory region as backed by a -* file descriptor. -* -* This function is typically used after memory_region_init_ram_ptr(). -* -* @mr: the memory region being queried. -* @fd: the file descriptor that backs @mr. -*/ -void memory_region_set_fd (MemoryRegion *mr,int fd); - -/** * memory_region_from_host: Convert a pointer into a RAM memory region * and an offset within it. *
memory.c +0 -10
@@ -1851,16 +1851,6 @@ int memory_region_get_fd(MemoryRegion *mr)return fd; } -void memory_region_set_fd (MemoryRegion *mr,int fd) -{ -rcu_read_lock (); -while (mr->alias) { - mr = mr->alias; - } - mr->ram_block->fd = fd; -rcu_read_unlock (); -} -void *memory_region_get_ram_ptr (MemoryRegion *mr) {void *ptr;
Signature · 1 line
2.13.0.91.g00982b8dd
PATCH v3 7/9 memfd: split qemu_memfd_alloc()
In reply to Marc-André LureauAdd a function to only create a memfd, without mmap. The function is used in the following memory backend.
--- include/qemu/memfd.h | 2 ++ util/memfd.c | 42 +++++++++++++++++++++++++----------------- 2 files changed, 27 insertions(+), 17 deletions(-)
include/qemu/memfd.h +2 -0
@@ -16,6 +16,8 @@#define F_SEAL_WRITE 0x0008/* prevent writes */ #endif +int qemu_memfd_create (const char *name,size_t size,unsigned int seals, +bool must_seal);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 +25 -17
70 lines not on this page · raw message
Signature · 1 line
2.13.0.91.g00982b8dd
PATCH v3 8/9 Add memfd based hostmem
In reply to Marc-André LureauAdd a new memory backend, similar to hostmem-file, except that it doesn't need to create files. It also enforces memory sealing.
This backend is mainly useful for sharing the memory with other processes.
Note that Linux supports transparent huge-pages of shmem/memfd memory since 4.8. It is relatively easier to set up THP than a dedicate hugepage mount point by using "madvise" in /sys/kernel/mm/transparent_hugepage/shmem_enabled.
Usage:
-object memory-backend-memfd,id=mem1,size=1G
--- backends/hostmem-memfd.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ backends/Makefile.objs | 2 ++ qemu-options.hx | 11 ++++++++ 3 files changed, 80 insertions(+) create mode 100644 backends/hostmem-memfd.c
backends/hostmem-memfd.c new file +67 -0
@@ -0,0 +1,67 @@ +/* +* 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" + +#define TYPE_MEMORY_BACKEND_MEMFD "memory-backend-memfd" + +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 ; + } + +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 , + true); +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); + } +} + +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, +}; + +static void register_types (void ) +{ +type_register_static (&memfd_backend_info); +} + +type_init (register_types);
backends/Makefile.objs +2 -0
@@ -12,3 +12,5 @@ common-obj-$(CONFIG_LINUX) += hostmem-file.o common-obj-y += cryptodev.o common-obj-y += cryptodev-builtin.o + +common-obj-$(CONFIG_LINUX) += hostmem-memfd.o
qemu-options.hx +11 -0
@@ -3974,6 +3974,17 @@ 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 +allocated with memfd and sealing. (Linux only) + +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.13.0.91.g00982b8dd
PATCH v3 9/9 tests: use memfd in vhost-user-test
In reply to Marc-André LureauThis 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 | 68 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 24 deletions(-)
tests/vhost-user-test.c +44 -24
@@ -16,6 +16,7 @@#include "qemu/option.h" #include "qemu/range.h" #include "qemu/sockets.h" +#include "qemu/memfd.h" #include "sysemu/char.h" #include "sysemu/sysemu.h" #include "libqos/libqos.h"
@@ -40,15 +41,14 @@#define HAVE_MONOTONIC_TIME #endif -#define QEMU_CMD_MEM " -m %d -object memory-backend-file,id=mem,size=%dM,"\ +#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_MEMFD " -m %d -object memory-backend-memfd,id=mem,size=%dM," \ + " -numa node,memdev=mem"#define QEMU_CMD_CHR " -chardev socket,id=%s,path=%s%s"#define QEMU_CMD_NETDEV " -netdev vhost-user,id=net0,chardev=%s,vhostforce"#define QEMU_CMD_NET " -device virtio-net-pci,netdev=net0" -#define QEMU_CMD QEMU_CMD_MEM QEMU_CMD_CHR \ - QEMU_CMD_NETDEV QEMU_CMD_NET -#define HUGETLBFS_MAGIC 0x958458f6/*********** FROM hw/virtio/vhost-user.c *************************************/
@@ -159,6 +159,22 @@ typedef struct TestServer {static const char *tmpfs;static const char *root; +static char *get_qemu_cmd (TestServer *s,int mem,const char *mem_path, +const char *chr_opts,const char *extra) +{ +if (qemu_memfd_check ()) { + return g_strdup_printf(QEMU_CMD_MEMFD QEMU_CMD_CHR + QEMU_CMD_NETDEV QEMU_CMD_NET "%s", mem, mem, + s->chr_name, s->socket_path, + chr_opts, s->chr_name, extra); + }else { + return g_strdup_printf(QEMU_CMD_MEM QEMU_CMD_CHR + QEMU_CMD_NETDEV QEMU_CMD_NET "%s", mem, mem, + mem_path, s->chr_name, s->socket_path, + chr_opts, s->chr_name, extra); + } +} +static void init_virtio_dev (TestServer *s) {QVirtioPCIDevice *dev;
@@ -477,14 +493,6 @@ static inline void test_server_connect(TestServer *server)test_server_create_chr (server,",reconnect=1" ); } -#define GET_QEMU_CMD (s) \ - g_strdup_printf(QEMU_CMD, 512, 512, (root), (s)->chr_name, \ - (s)->socket_path, "", (s)->chr_name) - -#define GET_QEMU_CMDE (s, mem, chr_opts, extra, ...) \ - g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name, \ - (s)->socket_path, (chr_opts), (s)->chr_name, ##__VA_ARGS__) -static gboolean _test_server_free (TestServer *server) {int i;
@@ -630,7 +638,7 @@ static void test_migrate(void)char *uri =g_strdup_printf ("%s%s" ,"unix:" , dest->mig_path);QTestState *global = global_qtest, *from, *to;GSource *source; -gchar *cmd; +gchar *cmd, *tmp;QDict *rsp;guint8 *log;guint64 size;
@@ -638,7 +646,7 @@ static void test_migrate(void)test_server_listen (s);test_server_listen (dest); - cmd =GET_QEMU_CMDE (s,2 ,"" ,"" ); + cmd =get_qemu_cmd (s,2 , root,"" ,"" ); from =qtest_start (cmd);g_free (cmd);
@@ -647,7 +655,9 @@ static void test_migrate(void) size =get_log_size (s); g_assert_cmpint(size, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE * 8)); - cmd =GET_QEMU_CMDE (dest,2 ,"" ," -incoming %s" , uri); + tmp =g_strdup_printf (" -incoming %s" , uri); + cmd =get_qemu_cmd (dest,2 , root,"" , tmp); +g_free (tmp); to =qtest_init (cmd);g_free (cmd);
@@ -753,7 +763,7 @@ static void test_reconnect_subprocess(void)char *cmd;g_thread_new ("connect" , connect_thread, s); - cmd =GET_QEMU_CMDE (s,2 ,",server" ,"" ); + cmd =get_qemu_cmd (s,2 , root,",server" ,"" );qtest_start (cmd);g_free (cmd);
@@ -789,7 +799,7 @@ static void test_connect_fail_subprocess(void) s->test_fail = true;g_thread_new ("connect" , connect_thread, s); - cmd =GET_QEMU_CMDE (s,2 ,",server" ,"" ); + cmd =get_qemu_cmd (s,2 , root,",server" ,"" );qtest_start (cmd);g_free (cmd);
@@ -817,7 +827,7 @@ static void test_flags_mismatch_subprocess(void) s->test_flags =TEST_FLAGS_DISCONNECT ;g_thread_new ("connect" , connect_thread, s); - cmd =GET_QEMU_CMDE (s,2 ,",server" ,"" ); + cmd =get_qemu_cmd (s,2 , root,",server" ,"" );qtest_start (cmd);g_free (cmd);
@@ -885,11 +895,21 @@ static void test_multiqueue(void) s->queues = queues;test_server_listen (s); - cmd = g_strdup_printf(QEMU_CMD_MEM QEMU_CMD_CHR QEMU_CMD_NETDEV ",queues=%d " -"-device virtio-net-pci,netdev=net0,mq=on,vectors=%d" , -512 ,512 , root, s->chr_name, - s->socket_path,"" , s->chr_name, - queues, queues *2 +2 ); +if (qemu_memfd_check ()) { + cmd =g_strdup_printf ( + QEMU_CMD_MEMFD QEMU_CMD_CHR QEMU_CMD_NETDEV ",queues=%d " +"-device virtio-net-pci,netdev=net0,mq=on,vectors=%d" , +512 ,512 , s->chr_name, + s->socket_path,"" , s->chr_name, + queues, queues *2 +2 ); + }else { + cmd =g_strdup_printf ( + QEMU_CMD_MEM QEMU_CMD_CHR QEMU_CMD_NETDEV ",queues=%d " +"-device virtio-net-pci,netdev=net0,mq=on,vectors=%d" , +512 ,512 , root, s->chr_name, + s->socket_path,"" , s->chr_name, + queues, queues *2 +2 ); + }qtest_start (cmd);g_free (cmd);
@@ -955,7 +975,7 @@ int main(int argc, char **argv)/* run the main loop thread so the chardev may operate */ thread =g_thread_new (NULL , thread_function, loop); - qemu_cmd =GET_QEMU_CMD (server); + qemu_cmd =get_qemu_cmd (server,512 , root,"" ,"" ); s =qtest_start (qemu_cmd);g_free (qemu_cmd);
Signature · 1 line
2.13.0.91.g00982b8dd
Quoted 4 lines · Marc-André Lureau
Add a new Linux-specific memory backend, similar to hostmem-file, except that it doesn't need file path. It also try to enforce memory sealing if available. It is thus slightly easier and secure, and is compatible with transparent huge-pages since Linux 4.8.
What about explicit huge pages ? They are the key scenario scenario where libvirt uses hostmem-file. AFAICT, there's no way to request explicit huge pages with memfd_create() syscall :-(
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 :|
Quoted 10 lines · Daniel P. Berrange
Quoted 4 lines · Marc-André Lureau
Add a new Linux-specific memory backend, similar to hostmem-file, except that it doesn't need file path. It also try to enforce memory sealing if available. It is thus slightly easier and secure, and is compatible with transparent huge-pages since Linux 4.8.
What about explicit huge pages ? They are the key scenario scenario where libvirt uses hostmem-file. AFAICT, there's no way to request explicit huge pages with
memfd_create()syscall :-(
Sadly, I don't think it's yet possible either.
Signature · 1 line
Marc-André Lureau