On 6/3/2024 6:17 AM, Daniel P. Berrangé wrote:
On Wed, May 29, 2024 at 01:31:38PM -0400, Steven Sistare wrote:
On 5/28/2024 5:12 PM, Peter Xu wrote:
On Mon, Apr 29, 2024 at 08:55:26AM -0700, Steve Sistare wrote:
Allocate anonymous memory using memfd_create if the memfd-alloc machine
option is set.

Signed-off-by: Steve Sistare <steven.sist...@oracle.com>
---
   hw/core/machine.c   | 22 ++++++++++++++++++++++
   include/hw/boards.h |  1 +
   qemu-options.hx     |  6 ++++++
   system/memory.c     |  9 ++++++---
   system/physmem.c    | 18 +++++++++++++++++-
   system/trace-events |  1 +
   6 files changed, 53 insertions(+), 4 deletions(-)

diff --git a/qemu-options.hx b/qemu-options.hx
index cf61f6b..f0dfda5 100644
--- a/qemu-options.hx
+++ b/qemu-options.hx
@@ -32,6 +32,7 @@ DEF("machine", HAS_ARG, QEMU_OPTION_machine, \
       "                vmport=on|off|auto controls emulation of vmport (default: 
auto)\n"
       "                dump-guest-core=on|off include guest memory in a core dump 
(default=on)\n"
       "                mem-merge=on|off controls memory merge support (default: 
on)\n"
+    "                memfd-alloc=on|off controls allocating anonymous guest RAM 
using memfd_create (default: off)\n"
       "                aes-key-wrap=on|off controls support for AES key wrapping 
(default=on)\n"
       "                dea-key-wrap=on|off controls support for DEA key wrapping 
(default=on)\n"
       "                suppress-vmdesc=on|off disables self-describing migration 
(default=off)\n"
@@ -79,6 +80,11 @@ SRST
           supported by the host, de-duplicates identical memory pages
           among VMs instances (enabled by default).
+    ``memfd-alloc=on|off``
+        Enables or disables allocation of anonymous guest RAM using
+        memfd_create.  Any associated memory-backend objects are created with
+        share=on.  The memfd-alloc default is off.
+
       ``aes-key-wrap=on|off``
           Enables or disables AES key wrapping support on s390-ccw hosts.
           This feature controls whether AES wrapping keys will be created
diff --git a/system/memory.c b/system/memory.c
index 49f1cb2..ca04a0e 100644
--- a/system/memory.c
+++ b/system/memory.c
@@ -1552,8 +1552,9 @@ bool memory_region_init_ram_nomigrate(MemoryRegion *mr,
                                         uint64_t size,
                                         Error **errp)
   {
+    uint32_t flags = current_machine->memfd_alloc ? RAM_SHARED : 0;

If there's a machine option to "use memfd for allocations", then it's
shared mem... Hmm..

It is a bit confusing to me in quite a few levels:

    - Why memory allocation method will be defined by a machine property,
      even if we have memory-backend-* which should cover everything?

Some memory regions are implicitly created, and have no explicit representation
on the qemu command line.  memfd-alloc affects those.

More generally, memfd-alloc affects all ramblock allocations that are
not explicitly represented by memory-backend object.  Thus the simple
command line "qemu -m 1G" does not explicitly describe an object, so it
goes through the anonymous allocation path, and is affected by memfd-alloc.

Internally, create_default_memdev does create a memory-backend object.
That is what my doc comment above refers to:
   Any associated memory-backend objects are created with share=on

An explicit "qemu -object memory-backend-*" is not affected by memfd-alloc.

The qapi comments in patch "migration: cpr-exec mode" attempt to say all that:

+#     Memory backend objects must have the share=on attribute, and
+#     must be mmap'able in the new QEMU process.  For example,
+#     memory-backend-file is acceptable, but memory-backend-ram is
+#     not.
+#
+#     The VM must be started with the '-machine memfd-alloc=on'
+#     option.  This causes implicit ram blocks -- those not explicitly
+#     described by a memory-backend object -- to be allocated by
+#     mmap'ing a memfd.  Examples include VGA, ROM, and even guest
+#     RAM when it is specified without a memory-backend object.

    - Even if we have such a machine property, why setting "memfd" will
      always imply shared?  why not private?  After all it's not called
      "memfd-shared-alloc", and we can create private mappings using
      e.g. memory-backend-memfd,share=off.

There is no use case for memfd-alloc with share=off, so no point IMO in
making the option more verbose.  For cpr, the mapping with all its modifications
must be visible to new qemu when qemu mmaps it.


So IIUC, cpr doesn't care about the use of 'memfd' as the specific impl,
it only cares that the memory is share=on.

Rather than having a machine type option "memfd-alloc" which is named after
a Linux specific impl detail, how about having a machine type option
"mem-share=on", which just happens to trigger use of memfd internally on
Linux ? That gives us freedom to use non-memfd options if appropriate in
the future.

That would be fine.  Internally we still need a mechanism to preserve the
memory and name it so qemu can mmap it post-exec, but in theory we could
invent some other mechanism to do so, such as creating /dev/shm files with
canonical names.

- Steve

Reply via email to