Add memfd memory backend
v7 cover Add memfd memory backend
Hi,
This series adds 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.
Since 4.14, memfd allows to set hugetlb requirement explicitly.
Pending for merge in 4.16 is memfd sealing support for hugetlb backed memory.
Usage:
-object memory-backend-memfd,id=mem1,size=1G
v7:
- fix a small memory leak
- update documentation and commit messages
- rebased
v6:
- add hugetlbsize option
- run vhost-user read-guest-mem test with memfile & memfd backend
v5:
- add memfd hugetlb support
- add seal and hugeltb options to memfd memory backend
- update qemu-option to explain sharing works with vhost-user for now
- rebased, misc code changes
v4:
- rebased, now that preliminary patches are merged
v3:
- make vhost-user-test use memfd only if possible
- rebased
v2:
- make it linux-specific
- minor changes and commit message tweaks
Marc-André Lureau (7):
memfd: add error argument, instead of perror()
memfd: add hugetlb support
memfd: add hugetlbsize argument
Add memfd based hostmem
tests: keep compiling failing vhost-user tests
vhost-user-test: make read-guest-mem setup its own qemu
tests: use memfd in vhost-user-test
include/qemu/memfd.h | 5 +- backends/hostmem-memfd.c | 170 +++++++++++++++++++++++++++++++++++++++++++++++ hw/virtio/vhost.c | 8 ++- tests/vhost-user-test.c | 128 +++++++++++++++++++++++------------ util/memfd.c | 78 ++++++++++++++-------- backends/Makefile.objs | 2 + qemu-options.hx | 22 ++++++ 7 files changed, 340 insertions(+), 73 deletions(-) create mode 100644 backends/hostmem-memfd.c
Signature · 1 line
2.16.0.rc1.1.gef27df75a1
PATCH v7 1/7 memfd: add error argument, instead of perror()
This will allow callers to silence error report when the call is allowed to failed.
--- include/qemu/memfd.h | 5 +++-- hw/virtio/vhost.c | 8 +++++++- util/memfd.c | 57 +++++++++++++++++++++++++++------------------------- 3 files changed, 40 insertions(+), 30 deletions(-)
include/qemu/memfd.h +3 -2
@@ -16,9 +16,10 @@#define F_SEAL_WRITE 0x0008/* prevent writes */ #endif -int qemu_memfd_create (const char *name,size_t size,unsigned int seals); +int qemu_memfd_create (const char *name,size_t size,unsigned int seals, +Error **errp);void *qemu_memfd_alloc (const char *name,size_t size,unsigned int seals, -int *fd); +int *fd,Error **errp);void qemu_memfd_free (void *ptr,size_t size,int fd);bool qemu_memfd_check (void );
hw/virtio/vhost.c +7 -1
@@ -329,6 +329,7 @@ static uint64_t vhost_get_log_size(struct vhost_dev *dev)static struct vhost_log *vhost_log_alloc (uint64_t size,bool share) { +Error *err =NULL ;struct vhost_log *log;uint64_t logsize = size *sizeof (*(log->log));int fd =-1 ;
@@ -337,7 +338,12 @@ static struct vhost_log *vhost_log_alloc(uint64_t size, bool share)if (share) { log->log =qemu_memfd_alloc ("vhost-log" , logsize,F_SEAL_GROW |F_SEAL_SHRINK |F_SEAL_SEAL , - &fd); + &fd, &err); +if (err) { +error_report_err (err); +g_free (log); +return NULL ; + }memset (log->log,0 , logsize); }else { log->log =g_malloc0 (logsize);
util/memfd.c +30 -27
118 lines not on this page · raw message
Signature · 1 line
2.16.0.rc1.1.gef27df75a1
PATCH v7 2/7 memfd: add hugetlb support
In reply to Marc-André LureauLinux commit 749df87bd7bee5a79cef073f5d032ddb2b211de8 (v4.14-rc1) added a new flag MFD_HUGETLB to memfd_create() that specify the file to be created resides in the hugetlbfs filesystem. This is the generic hugetlbfs filesystem not associated with any specific mount point.
hugetlbfs does not support sealing operations in v4.14, therefore specifying MFD_ALLOW_SEALING with MFD_HUGETLB will result in EINVAL.
However, I added sealing support in "[PATCH v3 0/9] memfd: add sealing to hugetlb-backed memory" series, queued in -mm tree for v4.16.
--- include/qemu/memfd.h | 4 ++-- util/memfd.c | 13 ++++++++++--- 2 files changed, 12 insertions(+), 5 deletions(-)
include/qemu/memfd.h +2 -2
@@ -16,8 +16,8 @@#define F_SEAL_WRITE 0x0008/* prevent writes */ #endif -int qemu_memfd_create (const char *name,size_t size,unsigned int seals, -Error **errp); +int qemu_memfd_create (const char *name,size_t size,bool hugetlb, +unsigned int seals,Error **errp);void *qemu_memfd_alloc (const char *name,size_t size,unsigned int seals,int *fd,Error **errp);void qemu_memfd_free (void *ptr,size_t size,int fd);
util/memfd.c +10 -3
@@ -52,7 +52,11 @@ static int memfd_create(const char *name, unsigned int flags)#define MFD_ALLOW_SEALING 0x0002U#endif -int qemu_memfd_create (const char *name,size_t size, +#ifndef MFD_HUGETLB +#define MFD_HUGETLB 0x0004U +#endif + +int qemu_memfd_create (const char *name,size_t size,bool hugetlb,unsigned int seals,Error **errp) {#ifdef CONFIG_LINUX
@@ -62,6 +66,9 @@ int qemu_memfd_create(const char *name, size_t size,if (seals) { flags |=MFD_ALLOW_SEALING ; } +if (hugetlb) { + flags |=MFD_HUGETLB ; + } mfd =memfd_create (name, flags);if (mfd <0 ) {
@@ -97,11 +104,11 @@ void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals, int *fd, Error **errp) { void *ptr; -int mfd =qemu_memfd_create (name, size, seals,NULL ); +int mfd =qemu_memfd_create (name, size, false, seals,NULL );/* some systems have memfd without sealing */ if (mfd ==-1 ) { - mfd =qemu_memfd_create (name, size,0 ,NULL ); + mfd =qemu_memfd_create (name, size, false,0 ,NULL ); }if (mfd ==-1 ) {
Signature · 1 line
2.16.0.rc1.1.gef27df75a1
PATCH v7 3/7 memfd: add hugetlbsize argument
In reply to Marc-André LureauLearn to specificy hugetlb size as qemu_memfd_create() argument.
--- include/qemu/memfd.h | 2 +- util/memfd.c | 22 ++++++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-)
include/qemu/memfd.h +1 -1
@@ -17,7 +17,7 @@#endif int qemu_memfd_create (const char *name,size_t size,bool hugetlb, -unsigned int seals,Error **errp); +uint64_t hugetlbsize,unsigned int seals,Error **errp);void *qemu_memfd_alloc (const char *name,size_t size,unsigned int seals,int *fd,Error **errp);void qemu_memfd_free (void *ptr,size_t size,int fd);
util/memfd.c +18 -4
@@ -29,6 +29,7 @@#include "qapi/error.h" #include "qemu/memfd.h" +#include "qemu/host-utils.h" #if definedCONFIG_LINUX && !definedCONFIG_MEMFD #include <sys/syscall.h>
@@ -56,9 +57,22 @@ static int memfd_create(const char *name, unsigned int flags)#define MFD_HUGETLB 0x0004U#endif +#ifndef MFD_HUGE_SHIFT +#define MFD_HUGE_SHIFT 26 +#endif +int qemu_memfd_create (const char *name,size_t size,bool hugetlb, -unsigned int seals,Error **errp) +uint64_t hugetlbsize,unsigned int seals,Error **errp) { +int htsize = hugetlbsize ?ctz64 (hugetlbsize) :0 ; + +if (htsize &&1 << htsize != hugetlbsize) { +error_setg (errp,"Hugepage size must be a power of 2" ); +return -1 ; + } + + htsize = htsize <<MFD_HUGE_SHIFT ; +#ifdef CONFIG_LINUX int mfd =-1 ;unsigned int flags =MFD_CLOEXEC ;
@@ -68,8 +82,8 @@ int qemu_memfd_create(const char *name, size_t size, bool hugetlb, }if (hugetlb) { flags |=MFD_HUGETLB ; + flags |= htsize; } - mfd =memfd_create (name, flags);if (mfd <0 ) { goto err;
@@ -104,11 +118,11 @@ void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals, int *fd, Error **errp) { void *ptr; -int mfd =qemu_memfd_create (name, size, false, seals,NULL ); +int mfd =qemu_memfd_create (name, size, false,0 , seals,NULL );/* some systems have memfd without sealing */ if (mfd ==-1 ) { - mfd =qemu_memfd_create (name, size, false,0 ,NULL ); + mfd =qemu_memfd_create (name, size, false,0 ,0 ,NULL ); }if (mfd ==-1 ) {
Signature · 1 line
2.16.0.rc1.1.gef27df75a1
PATCH v7 4/7 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.
Since 4.14, memfd allows to set hugetlb requirement explicitly.
Pending for merge in 4.16 is memfd sealing support for hugetlb backed memory.
Usage:
-object memory-backend-memfd,id=mem1,size=1G
--- backends/hostmem-memfd.c | 170 +++++++++++++++++++++++++++++++++++++++++++++++ backends/Makefile.objs | 2 + qemu-options.hx | 22 ++++++ 3 files changed, 194 insertions(+) create mode 100644 backends/hostmem-memfd.c
backends/hostmem-memfd.c new file +170 -0
@@ -0,0 +1,170 @@ +/* +* QEMU host memfd memory backend +* +* Copyright (C) 2018 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" + +#define MEMORY_BACKEND_MEMFD (obj) \ + OBJECT_CHECK(HostMemoryBackendMemfd, (obj), TYPE_MEMORY_BACKEND_MEMFD) + +typedef struct HostMemoryBackendMemfd HostMemoryBackendMemfd ; + +struct HostMemoryBackendMemfd { +HostMemoryBackend parent_obj; + +bool hugetlb; +uint64_t hugetlbsize; +bool seal; +}; + +static void +memfd_backend_memory_alloc (HostMemoryBackend *backend,Error **errp) +{ +HostMemoryBackendMemfd *m =MEMORY_BACKEND_MEMFD (backend); +char *name; +int fd; + +if (!backend->size) { +error_setg (errp,"can't create backend with size 0" ); +return ; + } + +if (host_memory_backend_mr_inited (backend)) { +return ; + } + + backend->force_prealloc = mem_prealloc; + fd =qemu_memfd_create (TYPE_MEMORY_BACKEND_MEMFD , backend->size, + m->hugetlb, m->hugetlbsize, m->seal ? +F_SEAL_GROW |F_SEAL_SHRINK |F_SEAL_SEAL :0 , + errp); +if (fd ==-1 ) { +return ; + } + + name =object_get_canonical_path (OBJECT (backend)); +memory_region_init_ram_from_fd (&backend->mr,OBJECT (backend), + name, backend->size, true, fd, errp); +g_free (name); +} + +static bool +memfd_backend_get_hugetlb (Object *o,Error **errp) +{ +return MEMORY_BACKEND_MEMFD (o)->hugetlb; +} + +static void +memfd_backend_set_hugetlb (Object *o,bool value,Error **errp) +{ +MEMORY_BACKEND_MEMFD (o)->hugetlb = value; +} + +static void +memfd_backend_set_hugetlbsize (Object *obj,Visitor *v,const char *name, +void *opaque,Error **errp) +{ +HostMemoryBackendMemfd *m =MEMORY_BACKEND_MEMFD (obj); +Error *local_err =NULL ; +uint64_t value; + +if (host_memory_backend_mr_inited (MEMORY_BACKEND (obj))) { +error_setg (&local_err,"cannot change property value" ); + goto out; + } + +visit_type_size (v, name, &value, &local_err); +if (local_err) { + goto out; + } +if (!value) { +error_setg (&local_err,"Property '%s.%s' doesn't take value '%" + PRIu64"'" ,object_get_typename (obj), name, value); + goto out; + } + m->hugetlbsize = value; +out: +error_propagate (errp, local_err); +} + +static void +memfd_backend_get_hugetlbsize (Object *obj,Visitor *v,const char *name, +void *opaque,Error **errp) +{ +HostMemoryBackendMemfd *m =MEMORY_BACKEND_MEMFD (obj); +uint64_t value = m->hugetlbsize; + +visit_type_size (v, name, &value, errp); +} + +static bool +memfd_backend_get_seal (Object *o,Error **errp) +{ +return MEMORY_BACKEND_MEMFD (o)->seal; +} + +static void +memfd_backend_set_seal (Object *o,bool value,Error **errp) +{ +MEMORY_BACKEND_MEMFD (o)->seal = value; +} + +static void +memfd_backend_instance_init (Object *obj) +{ +HostMemoryBackendMemfd *m =MEMORY_BACKEND_MEMFD (obj); + +/* default to sealed file */ + m->seal = true; +} + +static void +memfd_backend_class_init (ObjectClass *oc,void *data) +{ +HostMemoryBackendClass *bc =MEMORY_BACKEND_CLASS (oc); + + bc->alloc = memfd_backend_memory_alloc; + +object_class_property_add_bool (oc,"hugetlb" , + memfd_backend_get_hugetlb, + memfd_backend_set_hugetlb, + &error_abort); +object_class_property_add (oc,"hugetlbsize" ,"int" , + memfd_backend_get_hugetlbsize, + memfd_backend_set_hugetlbsize, +NULL ,NULL , &error_abort); +object_class_property_add_bool (oc,"seal" , + memfd_backend_get_seal, + memfd_backend_set_seal, + &error_abort); +} + +static const TypeInfo memfd_backend_info = { + .name =TYPE_MEMORY_BACKEND_MEMFD , + .parent =TYPE_MEMORY_BACKEND , + .instance_init = memfd_backend_instance_init, + .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);
backends/Makefile.objs +2 -0
@@ -8,3 +8,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 +22 -0
@@ -4024,6 +4024,28 @@ Memory backend objects offer more control than the @option{-m} option that is traditionally used to define guest RAM. Please refer to @option{memory-backend-file} for a description of the options. +@item -object memory-backend-memfd,id=@var{id},merge=@var{on|off},dump=@var{on|off},prealloc=@var{on|off},size=@var{size},host-nodes=@var{host-nodes},policy=@var{default|preferred|bind|interleave},seal=@var{on|off},hugetlb=@var{on|off},hugetlbsize=@var{size} + +Creates an anonymous memory file backend object, which allows QEMU to +share the memory with an external process (e.g. when using +vhost-user). The memory is allocated with memfd and optional +sealing. (Linux only) + +The @option{seal} option creates a sealed-file, that will block +further resizing the memory ('on' by default). + +The @option{hugetlb} option specify the file to be created resides in +the hugetlbfs filesystem (since Linux 4.14). Used in conjunction with +the @option{hugetlb} option, the @option{hugetlbsize} option specify +the hugetlb page size on systems that support multiple hugetlb page +sizes (it must be a power of 2 value supported by the system). + +In some versions of Linux, the @option{hugetlb} option is incompatible +with the @option{seal} option (requires at least Linux 4.16). + +Please refer to @option{memory-backend-file} for a description of the +other options. + @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.16.0.rc1.1.gef27df75a1
PATCH v7 5/7 tests: keep compiling failing vhost-user tests
In reply to Marc-André LureauLet's protect the failing tests under a QTEST_VHOST_USER_FIXME environment variable, so we keep compiling the tests and we can easily run them.
--- tests/vhost-user-test.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-)
tests/vhost-user-test.c +14 -13
@@ -30,8 +30,6 @@#include <linux/virtio_net.h> #include <sys/vfs.h> -#define VHOST_USER_NET_TESTS_WORKING 0/* broken as of 2.10.0 */ -/* GLIB version compatibility flags */ #if !GLIB_CHECK_VERSION (2 ,26 ,0 )#define G_TIME_SPAN_SECOND (G_GINT64_CONSTANT(1000000))
@@ -765,7 +763,7 @@ static void wait_for_rings_started(TestServer *s, size_t count)g_mutex_unlock (&s->data_mutex); } -#if VHOST_USER_NET_TESTS_WORKING && defined(CONFIG_HAS_GLIB_SUBPROCESS_TESTS ) +#if defined(CONFIG_HAS_GLIB_SUBPROCESS_TESTS )static inline void test_server_connect (TestServer *server) {test_server_create_chr (server,",reconnect=1" );
@@ -956,16 +954,19 @@ int main(int argc, char **argv)qtest_add_func ("/vhost-user/migrate" , test_migrate);qtest_add_func ("/vhost-user/multiqueue" , test_multiqueue); -#if VHOST_USER_NET_TESTS_WORKING && defined(CONFIG_HAS_GLIB_SUBPROCESS_TESTS ) -qtest_add_func ("/vhost-user/reconnect/subprocess" , - test_reconnect_subprocess); -qtest_add_func ("/vhost-user/reconnect" , test_reconnect); -qtest_add_func ("/vhost-user/connect-fail/subprocess" , - test_connect_fail_subprocess); -qtest_add_func ("/vhost-user/connect-fail" , test_connect_fail); -qtest_add_func ("/vhost-user/flags-mismatch/subprocess" , - test_flags_mismatch_subprocess); -qtest_add_func ("/vhost-user/flags-mismatch" , test_flags_mismatch); +#if defined(CONFIG_HAS_GLIB_SUBPROCESS_TESTS ) +/* keeps failing on build-system since Aug 15 2017 */ +if (getenv ("QTEST_VHOST_USER_FIXME" )) { +qtest_add_func ("/vhost-user/reconnect/subprocess" , + test_reconnect_subprocess); +qtest_add_func ("/vhost-user/reconnect" , test_reconnect); +qtest_add_func ("/vhost-user/connect-fail/subprocess" , + test_connect_fail_subprocess); +qtest_add_func ("/vhost-user/connect-fail" , test_connect_fail); +qtest_add_func ("/vhost-user/flags-mismatch/subprocess" , + test_flags_mismatch_subprocess); +qtest_add_func ("/vhost-user/flags-mismatch" , test_flags_mismatch); + }#endif ret =g_test_run ();
Signature · 1 line
2.16.0.rc1.1.gef27df75a1
PATCH v7 6/7 vhost-user-test: make read-guest-mem setup its own qemu
In reply to Marc-André Lureau--- tests/vhost-user-test.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-)
tests/vhost-user-test.c +15 -12
@@ -48,6 +48,14 @@#define QEMU_CMD QEMU_CMD_MEM QEMU_CMD_CHR \ QEMU_CMD_NETDEV QEMU_CMD_NET +#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__) +#define HUGETLBFS_MAGIC 0x958458f6/*********** FROM hw/virtio/vhost-user.c *************************************/
@@ -159,6 +167,10 @@ typedef struct TestServer {QGuestAllocator *alloc; } TestServer; +static TestServer *test_server_new (const gchar *name); +static void test_server_free (TestServer *server); +static void test_server_listen (TestServer *server); +static const char *tmpfs;static const char *root;
@@ -225,9 +237,8 @@ static void wait_for_fds(TestServer *s)g_mutex_unlock (&s->data_mutex); } -static void read_guest_mem (const void *data) +static void read_guest_mem_server (TestServer *s) { -TestServer *s = (void *)data;uint32_t *guest_mem;int i, j;size_t size;
@@ -492,14 +503,6 @@ static void test_server_listen(TestServer *server)test_server_create_chr (server,",server,nowait" ); } -#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;
@@ -652,7 +655,7 @@ static void test_read_guest_mem(void)init_virtio_dev (server,1u <<VIRTIO_NET_F_MAC ); -read_guest_mem (server); +read_guest_mem_server (server);uninit_virtio_dev (server);
@@ -730,7 +733,7 @@ static void test_migrate(void) global_qtest = to;qmp_eventwait ("RESUME" ); -read_guest_mem (dest); +read_guest_mem_server (dest);uninit_virtio_dev (s);
Signature · 1 line
2.16.0.rc1.1.gef27df75a1
PATCH v7 7/7 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).
If memfd is available, it is preferred.
However, in order to check that file & memfd backends both work correctly, the read-guest-mem test is checked explicitly for each.
--- tests/vhost-user-test.c | 90 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 64 insertions(+), 26 deletions(-)
tests/vhost-user-test.c +64 -26
@@ -17,6 +17,7 @@#include "qemu/range.h" #include "qemu/sockets.h" #include "chardev/char-fe.h" +#include "qemu/memfd.h" #include "sysemu/sysemu.h" #include "libqos/libqos.h" #include "libqos/pci-pc.h"
@@ -39,23 +40,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 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__) -#define HUGETLBFS_MAGIC 0x958458f6/*********** FROM hw/virtio/vhost-user.c *************************************/
@@ -174,6 +166,33 @@ static void test_server_listen(TestServer *server);static const char *tmpfs;static const char *root; +enum test_memfd { +TEST_MEMFD_AUTO , +TEST_MEMFD_YES , +TEST_MEMFD_NO , +}; + +static char *get_qemu_cmd (TestServer *s, +int mem,enum test_memfd memfd,const char *mem_path, +const char *chr_opts,const char *extra) +{ +if (memfd ==TEST_MEMFD_AUTO &&qemu_memfd_check ()) { + memfd =TEST_MEMFD_YES ; + } + +if (memfd ==TEST_MEMFD_YES ) { + 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,uint32_t features_mask) {uint32_t features;
@@ -639,8 +658,9 @@ GSourceFuncs test_migrate_source_funcs = { .check = test_migrate_source_check, }; -static void test_read_guest_mem (void ) +static void test_read_guest_mem (const void *arg) { +enum test_memfd memfd =GPOINTER_TO_INT (arg);TestServer *server =NULL ;char *qemu_cmd =NULL ;QTestState *s =NULL ;
@@ -648,7 +668,7 @@ static void test_read_guest_mem(void) server =test_server_new ("test" );test_server_listen (server); - qemu_cmd =GET_QEMU_CMD (server); + qemu_cmd =get_qemu_cmd (server,512 , memfd, root,"" ,"" ); s =qtest_start (qemu_cmd);g_free (qemu_cmd);
@@ -670,7 +690,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;
@@ -678,7 +698,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 ,TEST_MEMFD_AUTO , root,"" ,"" ); from =qtest_start (cmd);g_free (cmd);
@@ -687,7 +707,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 ,TEST_MEMFD_AUTO , root,"" , tmp); +g_free (tmp); to =qtest_init (cmd);g_free (cmd);
@@ -800,7 +822,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 ,TEST_MEMFD_AUTO , root,",server" ,"" );qtest_start (cmd);g_free (cmd);
@@ -838,7 +860,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 ,TEST_MEMFD_AUTO , root,",server" ,"" );qtest_start (cmd);g_free (cmd);
@@ -868,7 +890,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 ,TEST_MEMFD_AUTO , root,",server" ,"" );qtest_start (cmd);g_free (cmd);
@@ -903,11 +925,21 @@ static void test_multiqueue(void) s->queues =2 ;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, - s->queues, s->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, + s->queues, s->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, + s->queues, s->queues *2 +2 ); + }qtest_start (cmd);g_free (cmd);
@@ -953,7 +985,13 @@ int main(int argc, char **argv)/* run the main loop thread so the chardev may operate */ thread =g_thread_new (NULL , thread_function, loop); -qtest_add_func ("/vhost-user/read-guest-mem" , test_read_guest_mem); +if (qemu_memfd_check ()) { +qtest_add_data_func ("/vhost-user/read-guest-mem/memfd" , +GINT_TO_POINTER (TEST_MEMFD_YES ), + test_read_guest_mem); + } +qtest_add_data_func ("/vhost-user/read-guest-mem/memfile" , +GINT_TO_POINTER (TEST_MEMFD_NO ), test_read_guest_mem);qtest_add_func ("/vhost-user/migrate" , test_migrate);qtest_add_func ("/vhost-user/multiqueue" , test_multiqueue);
Signature · 1 line
2.16.0.rc1.1.gef27df75a1