From: Steve Sistare <steven.sistare@oracle.com>
To: qemu-devel@nongnu.org
Cc: "Daniel P. Berrange" <berrange@redhat.com>,
"Michael S. Tsirkin" <mst@redhat.com>,
"Jason Zeng" <jason.zeng@linux.intel.com>,
"Alex Bennée" <alex.bennee@linaro.org>,
"Juan Quintela" <quintela@redhat.com>,
"Dr. David Alan Gilbert" <dgilbert@redhat.com>,
"Markus Armbruster" <armbru@redhat.com>,
"Alex Williamson" <alex.williamson@redhat.com>,
"Steve Sistare" <steven.sistare@oracle.com>,
"Stefan Hajnoczi" <stefanha@redhat.com>,
"Marc-André Lureau" <marcandre.lureau@redhat.com>,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Philippe Mathieu-Daudé" <philmd@redhat.com>
Subject: [PATCH V2 05/22] vl: memfd-alloc option
Date: Tue, 5 Jan 2021 07:41:53 -0800 [thread overview]
Message-ID: <1609861330-129855-6-git-send-email-steven.sistare@oracle.com> (raw)
In-Reply-To: <1609861330-129855-1-git-send-email-steven.sistare@oracle.com>
Allocate anonymous memory using memfd_create if the memfd-alloc option is
set.
Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
---
exec.c | 38 ++++++++++++++++++++++++++++++--------
include/sysemu/sysemu.h | 1 +
qemu-options.hx | 11 +++++++++++
softmmu/vl.c | 4 ++++
trace-events | 1 +
5 files changed, 47 insertions(+), 8 deletions(-)
diff --git a/exec.c b/exec.c
index d1f31b4..6da6590 100644
--- a/exec.c
+++ b/exec.c
@@ -67,6 +67,7 @@
#include "exec/log.h"
#include "qemu/pmem.h"
+#include "qemu/memfd.h"
#include "migration/vmstate.h"
@@ -2231,34 +2232,55 @@ static void ram_block_add(RAMBlock *new_block, Error **errp, bool shared)
{
RAMBlock *block;
RAMBlock *last_block = NULL;
+ struct MemoryRegion *mr = new_block->mr;
ram_addr_t old_ram_size, new_ram_size;
Error *err = NULL;
+ const char *name;
+ void *addr = 0;
+ size_t maxlen;
old_ram_size = last_ram_page();
qemu_mutex_lock_ramlist();
- new_block->offset = find_ram_offset(new_block->max_length);
+ maxlen = new_block->max_length;
+ new_block->offset = find_ram_offset(maxlen);
if (!new_block->host) {
if (xen_enabled()) {
- xen_ram_alloc(new_block->offset, new_block->max_length,
- new_block->mr, &err);
+ xen_ram_alloc(new_block->offset, maxlen, new_block->mr, &err);
if (err) {
error_propagate(errp, err);
qemu_mutex_unlock_ramlist();
return;
}
} else {
- new_block->host = phys_mem_alloc(new_block->max_length,
- &new_block->mr->align, shared);
- if (!new_block->host) {
+ name = memory_region_name(new_block->mr);
+ if (memfd_alloc) {
+ int mfd = -1; /* placeholder until next patch */
+ mr->align = QEMU_VMALLOC_ALIGN;
+ if (mfd < 0) {
+ mfd = qemu_memfd_create(name, maxlen + mr->align,
+ 0, 0, 0, &err);
+ if (mfd < 0) {
+ return;
+ }
+ }
+ new_block->flags |= RAM_SHARED;
+ addr = file_ram_alloc(new_block, maxlen, mfd, false, errp);
+ trace_anon_memfd_alloc(name, maxlen, addr, mfd);
+ } else {
+ addr = phys_mem_alloc(maxlen, &mr->align, shared);
+ }
+
+ if (!addr) {
error_setg_errno(errp, errno,
"cannot set up guest memory '%s'",
- memory_region_name(new_block->mr));
+ name);
qemu_mutex_unlock_ramlist();
return;
}
- memory_try_enable_merging(new_block->host, new_block->max_length);
+ memory_try_enable_merging(addr, maxlen);
+ new_block->host = addr;
}
}
diff --git a/include/sysemu/sysemu.h b/include/sysemu/sysemu.h
index 4b6a5c4..408eb56 100644
--- a/include/sysemu/sysemu.h
+++ b/include/sysemu/sysemu.h
@@ -13,6 +13,7 @@ extern int only_migratable;
extern const char *qemu_name;
extern QemuUUID qemu_uuid;
extern bool qemu_uuid_set;
+extern bool memfd_alloc;
void qemu_add_data_dir(const char *path);
diff --git a/qemu-options.hx b/qemu-options.hx
index 708583b..455b43b7 100644
--- a/qemu-options.hx
+++ b/qemu-options.hx
@@ -4094,6 +4094,17 @@ SRST
an unmigratable state.
ERST
+#ifdef __linux__
+DEF("memfd-alloc", 0, QEMU_OPTION_memfd_alloc, \
+ "-memfd-alloc allocate anonymous memory using memfd_create\n",
+ QEMU_ARCH_ALL)
+#endif
+
+SRST
+``-memfd-alloc``
+ Allocate anonymous memory using memfd_create (Linux only).
+ERST
+
DEF("nodefaults", 0, QEMU_OPTION_nodefaults, \
"-nodefaults don't create default devices\n", QEMU_ARCH_ALL)
SRST
diff --git a/softmmu/vl.c b/softmmu/vl.c
index 4eb9d1f..5668e2b 100644
--- a/softmmu/vl.c
+++ b/softmmu/vl.c
@@ -164,6 +164,7 @@ bool boot_strict;
uint8_t *boot_splash_filedata;
int only_migratable; /* turn it off unless user states otherwise */
bool wakeup_suspend_enabled;
+bool memfd_alloc;
int icount_align_option;
@@ -3635,6 +3636,9 @@ void qemu_init(int argc, char **argv, char **envp)
case QEMU_OPTION_only_migratable:
only_migratable = 1;
break;
+ case QEMU_OPTION_memfd_alloc:
+ memfd_alloc = true;
+ break;
case QEMU_OPTION_nodefaults:
has_defaults = 0;
break;
diff --git a/trace-events b/trace-events
index 42107eb..ed46c4d 100644
--- a/trace-events
+++ b/trace-events
@@ -54,6 +54,7 @@ find_ram_offset_loop(uint64_t size, uint64_t candidate, uint64_t offset, uint64_
ram_block_discard_range(const char *rbname, void *hva, size_t length, bool need_madvise, bool need_fallocate, int ret) "%s@%p + 0x%zx: madvise: %d fallocate: %d ret: %d"
memory_notdirty_write_access(uint64_t vaddr, uint64_t ram_addr, unsigned size) "0x%" PRIx64 " ram_addr 0x%" PRIx64 " size %u"
memory_notdirty_set_dirty(uint64_t vaddr) "0x%" PRIx64
+anon_memfd_alloc(const char *name, size_t size, void *ptr, int fd) "%s size %zu ptr %p fd %d"
# memory.c
memory_region_ops_read(int cpu_index, void *mr, uint64_t addr, uint64_t value, unsigned size) "cpu %d mr %p addr 0x%"PRIx64" value 0x%"PRIx64" size %u"
--
1.8.3.1
next prev parent reply other threads:[~2021-01-05 16:13 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-01-05 15:41 [PATCH V2 00/22] Live Update Steve Sistare
2021-01-05 15:41 ` [PATCH V2 01/22] as_flat_walk Steve Sistare
2021-01-05 15:41 ` [PATCH V2 02/22] qemu_ram_volatile Steve Sistare
2021-01-05 15:41 ` [PATCH V2 03/22] oslib: qemu_clr_cloexec Steve Sistare
2021-01-05 15:41 ` [PATCH V2 04/22] util: env var helpers Steve Sistare
2021-01-05 15:41 ` Steve Sistare [this message]
2021-01-05 16:27 ` [PATCH V2 05/22] vl: memfd-alloc option Daniel P. Berrangé
2021-01-06 16:36 ` Steven Sistare
2021-01-06 20:10 ` Paolo Bonzini
2021-01-06 21:19 ` Steven Sistare
2021-01-05 15:41 ` [PATCH V2 06/22] vl: add helper to request re-exec Steve Sistare
2021-01-05 15:41 ` [PATCH V2 07/22] cpr Steve Sistare
2021-01-05 15:41 ` [PATCH V2 08/22] cpr: QMP interfaces Steve Sistare
2021-01-05 15:41 ` [PATCH V2 09/22] cpr: HMP interfaces Steve Sistare
2021-01-05 15:41 ` [PATCH V2 10/22] pci: export functions for cpr Steve Sistare
2021-01-05 15:41 ` [PATCH V2 11/22] vfio-pci: refactor " Steve Sistare
2021-01-05 15:42 ` [PATCH V2 12/22] vfio-pci: cpr Steve Sistare
2021-01-05 15:42 ` [PATCH V2 13/22] vhost: reset vhost devices upon cprsave Steve Sistare
2021-01-05 15:42 ` [PATCH V2 14/22] chardev: cpr framework Steve Sistare
2021-01-05 15:42 ` [PATCH V2 15/22] chardev: cpr for simple devices Steve Sistare
2021-01-05 15:42 ` [PATCH V2 16/22] chardev: cpr for pty Steve Sistare
2021-01-05 15:42 ` [PATCH V2 17/22] chardev: socket accept subroutine Steve Sistare
2021-01-05 15:42 ` [PATCH V2 18/22] chardev: cpr for sockets Steve Sistare
2021-01-05 16:22 ` Daniel P. Berrangé
2021-01-05 16:35 ` Steven Sistare
2021-01-05 15:42 ` [PATCH V2 19/22] monitor: cpr support Steve Sistare
2021-01-05 15:42 ` [PATCH V2 20/22] cpr: only-cpr-capable option Steve Sistare
2021-01-05 15:42 ` [PATCH V2 21/22] cpr: maintainers Steve Sistare
2021-01-05 15:42 ` [PATCH V2 22/22] simplify savevm Steve Sistare
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=1609861330-129855-6-git-send-email-steven.sistare@oracle.com \
--to=steven.sistare@oracle.com \
--cc=alex.bennee@linaro.org \
--cc=alex.williamson@redhat.com \
--cc=armbru@redhat.com \
--cc=berrange@redhat.com \
--cc=dgilbert@redhat.com \
--cc=jason.zeng@linux.intel.com \
--cc=marcandre.lureau@redhat.com \
--cc=mst@redhat.com \
--cc=pbonzini@redhat.com \
--cc=philmd@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=quintela@redhat.com \
--cc=stefanha@redhat.com \
/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).