kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 00/19] guest_memfd: support in-place memory conversion
@ 2026-09-08 20:48 Michael Roth
  2026-09-08 20:48 ` [PATCH v2 01/19] accel/kvm: Add helper for handling conversions of MMIO holes Michael Roth
                   ` (18 more replies)
  0 siblings, 19 replies; 24+ messages in thread
From: Michael Roth @ 2026-09-08 20:48 UTC (permalink / raw)
  To: qemu-devel
  Cc: kvm, pbonzini, berrange, armbru, pankaj.gupta, isaku.yamahata,
	xiaoyao.li, chao.p.peng, david, ashish.kalra, ackerleytng,
	lpieralisi

v1: https://lore.kernel.org/qemu-devel/20260528000416.8161-1-michael.roth@amd.com/
v2:
 - reuse memory-backend-memfd instead of introducing a new
   guest-memfd-specific backend (Peter)
 - pull bug-fix-related refactorings/dependencies back into the beginning of
   this series for more visibility/context (patches 1-4, can be applied
   independently), originally submitted as:
   https://lore.kernel.org/qemu-devel/20260527223036.4614-1-michael.roth@amd.com/
 - move gmem flag specification to KVM-internal code to allow for better
   control of likely-global policies (like INIT_SHARED, which some confidential
   VM types might prefer to leave unset) (Lorenzo)
 - zero-initialize the kvm_memory_attributes2 struct to avoid garbage values
   ending up in reserved fields (Pankaj)
 - add a helper to check for in-place conversion instead of accessing CGS
   directly
 - add reasoning for introducing ConfidentialGuestSupportProperties base class
   now instead of deferring (Markus)
 - minor fixups/refactorings


This patchset is also available at:

  https://github.com/amdese/qemu/commits/snp-inplace-v2

which is in turn based on the following series:

  [PATCH v5 00/12] KVM/hostmem: Support init-shared guest-memfd as VM backends
  https://lore.kernel.org/qemu-devel/20260908133151.836685-1-michael.roth@amd.com/


OVERVIEW
--------

This series adds guest_memfd support for in-place conversion of memory
between private/shared, and enables it for SEV-SNP guests. It is based
on recently-added kernel support for mmap()-able guest_memfd
instances[1], which allow it to be used for shared memory, and the
following patchset[2], which adds additional guest_memfd interfaces to
allow it to be used to perform in-place conversion:

  [PATCH v12 00/45] guest_memfd: In-place conversion support
  https://lore.kernel.org/kvm/20260830-gmem-inplace-conversion-v12-0-85e5fd25252a@google.com/

That series also introduces a new 'gmem_in_place_conversion' KVM
module option, which sets whether memory attributes are tracked
VM-wide by KVM (vm_memory_attributes=1: the existing 'legacy' mode),
or per-guest_memfd instance (vm_memory_attributes=0: the new mode
which allows for in-place conversion). The latter is intended to
eventually deprecate the legacy mode, at which point in-place
conversion would become the primarily-supported mode.


MOTIVATION
----------

Today, SEV-SNP guests (and other CoCo VM types using guest_memfd) keep
shared and private memory on separate physical backings: a userspace
memory-backend object for shared pages, and a kernel-allocated
guest_memfd file descriptor for private pages. KVM_SET_MEMORY_ATTRIBUTES
flips which backing the guest sees for a given GPA range, and the old
backing is typically discarded / hole-punched on conversion to avoid
doubled memory usage.

That model works, but has a number of downsides that impact certain
use-cases:

  - Each conversion involves discarding pages on one side and faulting
    them in on the other, which incurs allocation overheads in the
    host kernel for every conversion.

  - Some use-cases, like pKVM[3], rely on memory isolation rather than
    encryption and rely on in-place conversion to pass through things
    like secured framebuffer memory without needing to bounce data
    through separate shared/private HPAs, which would introduce
    unacceptable latency for that sort of workload.

  - Hugetlb support[4] for guest_memfd will rely on it, since things like
    1GB hugepages with a mix of shared/private sub-ranges would generally
    require 2 1GB hugetlb pages to remain available to handle shared vs.
    private accesses, which quickly causes doubling of guest memory usage.

Recent kernel work[2] makes guest_memfd mmap()-able and lets the *same*
physical pages be used for both shared and private states for a given
GPA range, allowing the above pitfalls to be naturally avoided.

This series wires that support up in QEMU.


DESIGN
------

For confidential VMs, a new 'convert-in-place' flag is added to switch
on in-place conversion support. When running in this mode, the user
*MUST* use memory-backend-guest-memfd for backing guest RAM. A new
RAM_GUEST_MEMFD_SHARED RAMBlock flag is added to track/enforce the
dependency. Additionally, QEMU is modified to use mmap()-able
guest_memfd and set this flag for other cases where it allocates RAM
internally. As a result, block->fd will generally always be a
guest_memfd, and when RAM_GUEST_MEMFD_SHARED is set then that block->fd
will be qemu_dup()'d as the FD handle for private memory as well (which
is currently what block->guest_memfd points to). This allows the prior
non-in-place handling around block->guest_memfd to be kept mostly
unchanged for the in-place case.

When running with convert-in-place=true, shared/private conversions
are no longer handled directly by KVM, but instead by a new guest_memfd
ioctl, KVM_SET_MEMORY_ATTRIBUTES2, which purposely provides similar
naming/implementation to the KVM_SET_MEMORY_ATTRIBUTES KVM ioctl that
it replaces. This series adds handling to route conversion requests to
the appropriate ioctls based on whether or not in-place conversion is
enabled.

This support also relies on the memory-backend-memfd,guest-memfd=on
option, since it is necessary to use guest_memfd for both the shared
and private memory. This is set automatically based on whether or
not in-place conversion is enabled.

Since guest_memfd ioctls need to be called against the specific
guest_memfd inode associated with each memory slot/region, some
refactoring is needed to handle conversions on a per-region basis. Much
of that is inherited from the bugfix series this patchset is based on
top of, which adds the initial logic for handling multiple sections
within a range.


USAGE
-----

After applying this series against a kernel with the RFC patches above
present, an SEV-SNP guest can be started with in-place conversion via:

    qemu-system-x86_64 \
        -machine q35,confidential-guest-support=sev0,memory-backend=ram0 \
        -object memory-backend-memfd,id=ram0,size=8G,share=on \
        -object sev-snp-guest,id=sev0,cbitpos=51,reduced-phys-bits=1,\
                convert-in-place=on \
        ...

The new memory-backend-guest-memfd can also be used by normal VMs:

    qemu-system-x86_64 \
        -machine q35,memory-backend=ram0 \
        -object memory-backend-memfd,id=ram0,size=8G,share=on \
        ...

This is mainly only useful atm for testing, but in the future there may
be more use-cases around using guest_memfd as a general-purpose backend
for non-confidential VMs, so it is intended to work in this manner as
well.


NOTES/TODO
----------

  - TDX testing would be great, in theory it can be enabled with this
    series (similarly to the top patch) but I'm not sure if there are
    other special requirements before we can switch it on.
  - kernel patches are still in-flight, but fairly mature at this point
    and nearing upstream


REFERENCES
----------

[1] https://lore.kernel.org/kvm/20250729225455.670324-1-seanjc@google.com/
[2] https://lore.kernel.org/kvm/20260830-gmem-inplace-conversion-v12-0-85e5fd25252a@google.com/
[3] https://www.youtube.com/watch?v=MMfAGNW9RVg
[4] https://lore.kernel.org/kvm/cover.1747264138.git.ackerleytng@google.com/


Thoughts, feedback, and testing are very much appreciated.

Thanks,

Mike


----------------------------------------------------------------
Ashish Kalra (1):
      accel/kvm: Fix kvm_convert_memory() calls crossing memory regions

Michael Roth (18):
      accel/kvm: Add helper for handling conversions of MMIO holes
      accel/kvm: Fix handling of MMIO holes at start of conversion ranges
      accel/kvm: Fix handling of conversion ranges with multiple MMIO holes
      accel/kvm: Use dedicated helper for creating private-only gmem instances
      linux-headers: Update headers for v12 of in-place conversion kernel support
      accel/kvm: Add CGS option to control in-place conversion support
      system/memory: Re-use memory-backend-guest-memfd inode for private memory
      accel/kvm: Handle guest_memfd flags internally when creating instances
      system/memory: Default to guest_memfd for RAM for in-place conversion
      accel/kvm: Move post-conversion updates to a separate helper
      accel/kvm: Re-order attribute notifications for in-place conversion
      accel/kvm: Support shared/private conversions via guest_memfd ioctls
      accel/kvm: Don't default to private attributes for in-place conversion
      i386/sev: Update SNP_LAUNCH_UPDATE for in-place conversion
      i386/sev: Allow in-place conversion for SEV-SNP guests
      i386/sev: Update CPUID failure handling for in-place conversion
      accel/kvm: Disable discard for in-place conversion
      hostmem: Automatically select set guest-memfd=on for in-place conversion

 accel/kvm/kvm-all.c                         | 449 ++++++++++++++++++++++++----
 accel/stubs/kvm-stub.c                      |   8 +-
 backends/confidential-guest-support.c       |  26 ++
 backends/hostmem-memfd.c                    |  41 ++-
 hw/core/machine.c                           |   5 +
 include/hw/core/boards.h                    |   1 +
 include/system/confidential-guest-support.h |  14 +
 include/system/kvm.h                        |   3 +-
 include/system/memory.h                     |   3 +
 linux-headers/linux/kvm.h                   |  16 +
 qapi/qom.json                               |  21 +-
 system/memory.c                             |  23 +-
 system/physmem.c                            |  52 +++-
 target/i386/sev.c                           |  22 +-
 14 files changed, 593 insertions(+), 91 deletions(-)


^ permalink raw reply	[flat|nested] 24+ messages in thread

end of thread, other threads:[~2026-09-10  8:39 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-08 20:48 [PATCH v2 00/19] guest_memfd: support in-place memory conversion Michael Roth
2026-09-08 20:48 ` [PATCH v2 01/19] accel/kvm: Add helper for handling conversions of MMIO holes Michael Roth
2026-09-08 20:48 ` [PATCH v2 02/19] accel/kvm: Fix kvm_convert_memory() calls crossing memory regions Michael Roth
2026-09-08 20:48 ` [PATCH v2 03/19] accel/kvm: Fix handling of MMIO holes at start of conversion ranges Michael Roth
2026-09-08 20:48 ` [PATCH v2 04/19] accel/kvm: Fix handling of conversion ranges with multiple MMIO holes Michael Roth
2026-09-08 20:48 ` [PATCH v2 05/19] accel/kvm: Use dedicated helper for creating private-only gmem instances Michael Roth
2026-09-08 20:48 ` [PATCH v2 06/19] linux-headers: Update headers for v12 of in-place conversion kernel support Michael Roth
2026-09-08 20:48 ` [PATCH v2 07/19] accel/kvm: Add CGS option to control in-place conversion support Michael Roth
2026-09-09  6:20   ` Markus Armbruster
2026-09-08 20:48 ` [PATCH v2 08/19] system/memory: Re-use memory-backend-guest-memfd inode for private memory Michael Roth
2026-09-10  8:39   ` David Hildenbrand
2026-09-08 20:48 ` [PATCH v2 09/19] accel/kvm: Handle guest_memfd flags internally when creating instances Michael Roth
2026-09-08 20:48 ` [PATCH v2 10/19] system/memory: Default to guest_memfd for RAM for in-place conversion Michael Roth
2026-09-08 20:48 ` [PATCH v2 11/19] accel/kvm: Move post-conversion updates to a separate helper Michael Roth
2026-09-08 20:48 ` [PATCH v2 12/19] accel/kvm: Re-order attribute notifications for in-place conversion Michael Roth
2026-09-08 20:48 ` [PATCH v2 13/19] accel/kvm: Support shared/private conversions via guest_memfd ioctls Michael Roth
2026-09-08 20:48 ` [PATCH v2 14/19] accel/kvm: Don't default to private attributes for in-place conversion Michael Roth
2026-09-08 20:48 ` [PATCH v2 15/19] i386/sev: Update SNP_LAUNCH_UPDATE " Michael Roth
2026-09-08 20:48 ` [PATCH v2 16/19] i386/sev: Allow in-place conversion for SEV-SNP guests Michael Roth
2026-09-08 20:48 ` [PATCH v2 17/19] i386/sev: Update CPUID failure handling for in-place conversion Michael Roth
2026-09-08 20:48 ` [PATCH v2 18/19] accel/kvm: Disable discard " Michael Roth
2026-09-08 22:16   ` Michael Roth
2026-09-08 20:48 ` [PATCH v2 19/19] hostmem: Automatically select set guest-memfd=on " Michael Roth
2026-09-09  6:30   ` Markus Armbruster

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).