From: "Marc-André Lureau" <marcandre.lureau@redhat.com>
To: qemu-devel@nongnu.org
Cc: pbonzini@redhat.com, "Marc-André Lureau" <marcandre.lureau@redhat.com>
Subject: [Qemu-devel] [PATCH v2 8/9] Add memfd based hostmem
Date: Wed, 11 Jan 2017 12:33:46 +0100 [thread overview]
Message-ID: <20170111113347.7577-9-marcandre.lureau@redhat.com> (raw)
In-Reply-To: <20170111113347.7577-1-marcandre.lureau@redhat.com>
Add 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
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
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
diff --git a/backends/hostmem-memfd.c b/backends/hostmem-memfd.c
new file mode 100644
index 0000000000..13d300d9ad
--- /dev/null
+++ b/backends/hostmem-memfd.c
@@ -0,0 +1,67 @@
+/*
+ * QEMU host memfd memory backend
+ *
+ * Copyright (C) 2016 Red Hat Inc
+ *
+ * Authors:
+ * Marc-André Lureau <marcandre.lureau@redhat.com>
+ *
+ * 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);
diff --git a/backends/Makefile.objs b/backends/Makefile.objs
index 18469980e6..1ed7e5b286 100644
--- a/backends/Makefile.objs
+++ b/backends/Makefile.objs
@@ -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
diff --git a/qemu-options.hx b/qemu-options.hx
index c534a2f7f9..d48463dd81 100644
--- a/qemu-options.hx
+++ b/qemu-options.hx
@@ -3807,6 +3807,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
--
2.11.0
next prev parent reply other threads:[~2017-01-11 11:34 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-01-11 11:33 [Qemu-devel] [PATCH v2 0/9] Add memfd memory backend Marc-André Lureau
2017-01-11 11:33 ` [Qemu-devel] [PATCH v2 1/9] exec: check kvm mmu notifiers earlier Marc-André Lureau
2017-01-11 11:33 ` [Qemu-devel] [PATCH v2 2/9] exec: split file_ram_alloc() Marc-André Lureau
2017-01-11 11:33 ` [Qemu-devel] [PATCH v2 3/9] exec: split qemu_ram_alloc_from_file() Marc-André Lureau
2017-01-11 11:33 ` [Qemu-devel] [PATCH v2 4/9] Add memory_region_init_ram_from_fd() Marc-André Lureau
2017-01-11 11:33 ` [Qemu-devel] [PATCH v2 5/9] ivshmem: use ram_from_fd() Marc-André Lureau
2017-01-11 11:33 ` [Qemu-devel] [PATCH v2 6/9] memory: remove memory_region_set_fd Marc-André Lureau
2017-01-11 11:33 ` [Qemu-devel] [PATCH v2 7/9] memfd: split qemu_memfd_alloc() Marc-André Lureau
2017-01-11 11:33 ` Marc-André Lureau [this message]
2017-01-11 11:33 ` [Qemu-devel] [PATCH v2 9/9] tests: use memfd in vhost-user-test Marc-André Lureau
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170111113347.7577-9-marcandre.lureau@redhat.com \
--to=marcandre.lureau@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).