guest_memfd: support in-place memory conversion
Subsystems (4)
cover guest_memfd: support in-place memory conversion
This patchset is also available at:
https://github.com/amdese/qemu/commits/snp-inplace-rfc1
which is in turn based on the following series:
[PATCH 0/4] "guest_memfd: Fix handling for conversions of MMIO ranges"
https://lists.gnu.org/archive/html/qemu-devel/2026-05/msg07547.html
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 v7 00/42] guest_memfd: In-place conversion support"
https://lore.kernel.org/kvm/[email protected]/
That series also introduces a new 'vm_memory_attributes' 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
------
A new dedicated memory backend, memory-backend-guest-memfd, allocates its memory via a guest_memfd file descriptor obtained from KVM with the GUEST_MEMFD_FLAG_MMAP | GUEST_MEMFD_FLAG_INIT_SHARED flags. The fd is mmap()ed so userspace can access pages directly while they are in the shared state. For a normal/non-confidential VM, this backend can be used in a similar fashion as the existing memory-backend-memfd.
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 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 is well (which is currently what block->guest_memfd point to). This allows the prior non-in-place handling around block->guest_memfd to be kept mostly unchanged.
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.
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-section. 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 that gets heavily re-used here.
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-guest-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-guest-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
----------
- the CPR handling to support resetting of confidential VMs is currently disabled when in-place conversion is enabled.
- 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/[email protected]/
[2] https://lore.kernel.org/kvm/[email protected]/
[3] https://www.youtube.com/watch?v=MMfAGNW9RVg
[4] 1GB hugetlb v2
Thoughts, feedback, and testing are very much appreciated.
Thanks,
Mike
----------------------------------------------------------------
Michael Roth (12):
accel/kvm: Decouple guest_memfd checks from memory attribute checks
hostmem: Introduce dedicated memory backend for guest_memfd
linux-headers: Update headers for v7 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 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
accel/kvm/kvm-all.c | 286 +++++++++++-- accel/stubs/kvm-stub.c | 9 +- backends/confidential-guest-support.c | 25 ++ backends/hostmem-guest-memfd.c | 93 +++++ backends/meson.build | 1 + include/standard-headers/drm/drm_fourcc.h | 28 +- include/standard-headers/linux/const.h | 18 + include/standard-headers/linux/ethtool.h | 28 +- include/standard-headers/linux/input-event-codes.h | 13 + include/standard-headers/linux/pci_regs.h | 71 +++- include/standard-headers/linux/typelimits.h | 8 + include/standard-headers/linux/virtio_ring.h | 5 +- include/standard-headers/linux/virtio_rtc.h | 237 +++++++++++ include/standard-headers/linux/vmclock-abi.h | 20 + include/system/confidential-guest-support.h | 14 + include/system/hostmem.h | 1 + include/system/kvm.h | 3 +- include/system/memory.h | 8 +- linux-headers/asm-arm64/kvm.h | 1 + linux-headers/asm-arm64/unistd_64.h | 1 + linux-headers/asm-generic/unistd.h | 5 +- linux-headers/asm-loongarch/kvm.h | 5 + linux-headers/asm-loongarch/kvm_para.h | 1 + linux-headers/asm-loongarch/unistd_64.h | 2 + linux-headers/asm-mips/unistd_n32.h | 1 + linux-headers/asm-mips/unistd_n64.h | 1 + linux-headers/asm-mips/unistd_o32.h | 1 + linux-headers/asm-powerpc/unistd_32.h | 1 + linux-headers/asm-powerpc/unistd_64.h | 1 + linux-headers/asm-riscv/kvm.h | 11 +- linux-headers/asm-riscv/ptrace.h | 37 ++ linux-headers/asm-riscv/unistd_32.h | 1 + linux-headers/asm-riscv/unistd_64.h | 1 + linux-headers/asm-s390/unistd_32.h | 446 --------------------- linux-headers/asm-s390/unistd_64.h | 1 + linux-headers/asm-x86/kvm.h | 21 +- linux-headers/asm-x86/unistd_32.h | 1 + linux-headers/asm-x86/unistd_64.h | 1 + linux-headers/asm-x86/unistd_x32.h | 1 + linux-headers/linux/const.h | 18 + linux-headers/linux/iommufd.h | 48 +++ linux-headers/linux/kvm.h | 62 ++- linux-headers/linux/mshv.h | 4 +- linux-headers/linux/psp-sev.h | 2 +- linux-headers/linux/stddef.h | 4 + linux-headers/linux/vduse.h | 85 +++- linux-headers/linux/vfio.h | 30 +- qapi/qom.json | 35 +- qemu-options.hx | 5 + system/memory.c | 22 +- system/physmem.c | 50 ++- target/i386/sev.c | 12 +- 52 files changed, 1253 insertions(+), 533 deletions(-) create mode 100644 backends/hostmem-guest-memfd.c create mode 100644 include/standard-headers/linux/typelimits.h create mode 100644 include/standard-headers/linux/virtio_rtc.h delete mode 100644 linux-headers/asm-s390/unistd_32.h
RFC PATCH 1/12 accel/kvm: Decouple guest_memfd checks from memory attribute checks
Currently QEMU supports using guest_memfd internally (separately from user-specified memory backends) to handle private memory for confidential VMs, and as a result has checks for guest_memfd support merged with checks to see if KVM can handle mapping private memory (as determined by KVM_MEMORY_ATTRIBUTE_PRIVATE).
Future QEMU support will allow using guest_memfd not just for private memory, but as mmap()'able memory that can be used by non-confidential guests as well.
In prep for this, split the checks for guest_memfd out from the check for KVM_MEMORY_ATTRIBUTE_PRIVATE, and rename the current kvm_create_guest_memfd() to kvm_create_guest_memfd_private() to self-document current behavior/expectations and disambiguate from future helpers intended for creating a guest_memfd to handle non-private/shared memory. While there, fix up the missing error_setg() handling in the stub functions.
--- accel/kvm/kvm-all.c | 20 +++++++++++++++++--- accel/stubs/kvm-stub.c | 3 ++- include/system/kvm.h | 2 +- include/system/memory.h | 5 +++-- system/physmem.c | 8 ++++---- 5 files changed, 27 insertions(+), 11 deletions(-)
accel/kvm/kvm-all.c +17 -3
@@ -795,6 +795,11 @@ static int kvm_mem_flags(MemoryRegion *mr) }if (memory_region_has_guest_memfd (mr)) {assert (kvm_guest_memfd_supported); +/* +* memory_region_has_guest_memfd() is specifically pertaining to +* using guest_memfd to handle private memory use cases. +*/ +assert (kvm_supported_memory_attributes &KVM_MEMORY_ATTRIBUTE_PRIVATE ); flags |=KVM_MEM_GUEST_MEMFD ; }return flags;
@@ -3066,8 +3071,7 @@ static int kvm_init(AccelState *as, MachineState *ms) kvm_supported_memory_attributes =kvm_vm_check_extension (s,KVM_CAP_MEMORY_ATTRIBUTES ); kvm_guest_memfd_supported =kvm_vm_check_extension (s,KVM_CAP_GUEST_MEMFD ) && -kvm_vm_check_extension (s,KVM_CAP_USER_MEMORY2 ) && - (kvm_supported_memory_attributes &KVM_MEMORY_ATTRIBUTE_PRIVATE ); +kvm_vm_check_extension (s,KVM_CAP_USER_MEMORY2 ); kvm_pre_fault_memory_supported =kvm_vm_check_extension (s,KVM_CAP_PRE_FAULT_MEMORY );if (s->kernel_irqchip_split ==ON_OFF_AUTO_AUTO ) {
@@ -4854,7 +4858,7 @@ void kvm_mark_guest_state_protected(void) kvm_state->guest_state_protected = true; } -int kvm_create_guest_memfd (uint64_t size,uint64_t flags,Error **errp) +static int kvm_create_guest_memfd (uint64_t size,uint64_t flags,Error **errp) {int fd;struct kvm_create_guest_memfd guest_memfd = {
@@ -4875,3 +4879,13 @@ int kvm_create_guest_memfd(uint64_t size, uint64_t flags, Error **errp)return fd; } + +int kvm_create_guest_memfd_private (uint64_t size,Error **errp) +{ +if (!(kvm_supported_memory_attributes &KVM_MEMORY_ATTRIBUTE_PRIVATE )) { +error_setg (errp,"KVM does not support using guest_memfd for private memory" ); +return -1 ; + } + +return kvm_create_guest_memfd (size,0 , errp); +}
accel/stubs/kvm-stub.c +2 -1
@@ -139,7 +139,8 @@ bool kvm_hwpoisoned_mem(void)return false; } -int kvm_create_guest_memfd (uint64_t size,uint64_t flags,Error **errp) +int kvm_create_guest_memfd_private (uint64_t size,Error **errp) { +error_setg (errp,"guest_memfd is not supported for this configuration" );return -ENOSYS ; }
include/system/kvm.h +1 -1
@@ -561,7 +561,7 @@ void kvm_mark_guest_state_protected(void);*/ bool kvm_hwpoisoned_mem (void ); -int kvm_create_guest_memfd (uint64_t size,uint64_t flags,Error **errp); +int kvm_create_guest_memfd_private (uint64_t size,Error **errp);int kvm_set_memory_attributes_private (hwaddr start,uint64_t size);int kvm_set_memory_attributes_shared (hwaddr start,uint64_t size);
include/system/memory.h +3 -2
@@ -1745,9 +1745,10 @@ bool memory_region_is_protected(const MemoryRegion *mr);/** * memory_region_has_guest_memfd: check whether a memory region has guest_memfd -* associated +* associated with it for handling private memory * -* Returns %true if a memory region's ram_block has valid guest_memfd assigned. +* Returns %true if a memory region's ram_block has valid guest_memfd assigned +* for handling private memory. * * @mr: the memory region being queried */
system/physmem.c +4 -4
@@ -2202,8 +2202,8 @@ static void ram_block_add(RAMBlock *new_block, Error **errp) goto out_free; } - new_block->guest_memfd =kvm_create_guest_memfd (new_block->max_length, -0 , errp); + new_block->guest_memfd =kvm_create_guest_memfd_private (new_block->max_length, + errp);if (new_block->guest_memfd <0 ) {qemu_mutex_unlock_ramlist (); goto out_free;
@@ -2835,8 +2835,8 @@ int ram_block_rebind(Error **errp)if (block->guest_memfd >=0 ) {close (block->guest_memfd); } - block->guest_memfd =kvm_create_guest_memfd (block->max_length, -0 , errp); + block->guest_memfd =kvm_create_guest_memfd_private (block->max_length, + errp);if (block->guest_memfd <0 ) {qemu_mutex_unlock_ramlist ();return -1 ;
Signature · 1 line
2.43.0
RFC PATCH 2/12 hostmem: Introduce dedicated memory backend for guest_memfd
In reply to Michael RothIn the initial implementation of guest_memfd in the linux kernel, it was not possible to map memory into userspace for direct access; instead the memory provided by the memory backend would be used for cases where a confidential VM wants to access normal/unprotected/unencrypted memory that can be used for shared memory use cases, and for access to private memory a guest_memfd could be associated with the same memslot. A memory 'private' attribute set via KVM_SET_MEMORY_ATTRIBUTES could then be used to have KVM route to the approprate backing memory.
In that model, it didn't make sense to introduce a specific backend for guest_memfd, since there was always a generally need to have a separate backend type to handle shared memory access/allocation. Instead, QEMU configures the guest_memfd support for the associated memslots internally for cases where it is running a confidential VM.
However, with recent changes in guest_memfd kernel support, it is now possible to mmap() a guest_memfd FD into userspace and use it for shared memory, as well as continue to use the same physical pages for the same GPA ranges after they are converted to private ("in-place conversion").
To enable the use of this mmap()-able/guest_memfd-provided memory to be used for normal/shared memory instead of just for private memory, introduce a dedicated guest_memfd memory backend that can be used both for confidential VMs that wish to make use of in-place conversion, as well as for non-confidential VMs that just want to make use of guest_memfd for normal memory (which can be useful both for testing as well as a stepping stone to things like software-protected VMs where the host can be trusted to provided some additional degree of isolation for the VM independently of hardware support).
--- accel/kvm/kvm-all.c | 15 ++++++ accel/stubs/kvm-stub.c | 6 +++ backends/hostmem-guest-memfd.c | 92 ++++++++++++++++++++++++++++++++++ backends/meson.build | 1 + include/system/hostmem.h | 1 + include/system/kvm.h | 1 + qapi/qom.json | 19 ++++++- qemu-options.hx | 5 ++ 8 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 backends/hostmem-guest-memfd.c
accel/kvm/kvm-all.c +15 -0
@@ -108,6 +108,7 @@ static bool kvm_has_guest_debug;static int kvm_sstep_flags;static bool kvm_immediate_exit;static uint64_t kvm_supported_memory_attributes; +static uint64_t kvm_supported_guest_memfd_flags;static bool kvm_guest_memfd_supported;static hwaddr kvm_max_slot_size = ~0 ;
@@ -3069,6 +3070,7 @@ static int kvm_init(AccelState *as, MachineState *ms) } kvm_supported_memory_attributes =kvm_vm_check_extension (s,KVM_CAP_MEMORY_ATTRIBUTES ); + kvm_supported_guest_memfd_flags =kvm_vm_check_extension (s,KVM_CAP_GUEST_MEMFD_FLAGS ); kvm_guest_memfd_supported =kvm_vm_check_extension (s,KVM_CAP_GUEST_MEMFD ) &&kvm_vm_check_extension (s,KVM_CAP_USER_MEMORY2 );
@@ -4889,3 +4891,16 @@ int kvm_create_guest_memfd_private(uint64_t size, Error **errp)return kvm_create_guest_memfd (size,0 , errp); } + +int kvm_create_guest_memfd_shared (uint64_t size,Error **errp) +{ +if (!(kvm_supported_guest_memfd_flags &GUEST_MEMFD_FLAG_MMAP ) || + !(kvm_supported_guest_memfd_flags &GUEST_MEMFD_FLAG_INIT_SHARED )) { +error_setg (errp,"KVM does not support using guest_memfd for shared memory" ); +return -1 ; + } + +return kvm_create_guest_memfd (size, +GUEST_MEMFD_FLAG_MMAP |GUEST_MEMFD_FLAG_INIT_SHARED , + errp); +}
accel/stubs/kvm-stub.c +6 -0
@@ -144,3 +144,9 @@ int kvm_create_guest_memfd_private(uint64_t size, Error **errp)error_setg (errp,"guest_memfd is not supported for this configuration" );return -ENOSYS ; } + +int kvm_create_guest_memfd_shared (uint64_t size,Error **errp) +{ +error_setg (errp,"guest_memfd is not supported for this configuration" ); +return -ENOSYS ; +}
backends/hostmem-guest-memfd.c new file +92 -0
92 lines not on this page · raw message
backends/meson.build +1 -0
@@ -20,6 +20,7 @@ endif if host_os == 'linux' system_ss.add(files('hostmem-memfd.c')) system_ss.add(files('host_iommu_device.c')) + system_ss.add(files('hostmem-guest-memfd.c')) endif if keyutils.found() system_ss.add(keyutils, files('cryptodev-lkcf.c'))
include/system/hostmem.h +1 -0
@@ -41,6 +41,7 @@ OBJECT_DECLARE_TYPE(HostMemoryBackend, HostMemoryBackendClass,#define TYPE_MEMORY_BACKEND_MEMFD "memory-backend-memfd" +#define TYPE_MEMORY_BACKEND_GUEST_MEMFD "memory-backend-guest-memfd"/** * HostMemoryBackendClass:
include/system/kvm.h +1 -0
@@ -562,6 +562,7 @@ void kvm_mark_guest_state_protected(void);bool kvm_hwpoisoned_mem (void );int kvm_create_guest_memfd_private (uint64_t size,Error **errp); +int kvm_create_guest_memfd_shared (uint64_t size,Error **errp);int kvm_set_memory_attributes_private (hwaddr start,uint64_t size);int kvm_set_memory_attributes_shared (hwaddr start,uint64_t size);
qapi/qom.json +18 -1
@@ -661,7 +661,8 @@ # @share: if false, the memory is private to QEMU; if true, it is # shared (default false for backends memory-backend-file and # memory-backend-ram, true for backends memory-backend-epc, -# memory-backend-memfd, and memory-backend-shm) +# memory-backend-memfd, memory-backend-shm, and +# memory-backend-guest-memfd) # # @reserve: if true, reserve swap space (or huge pages) if applicable # (default: true) (since 6.1)
@@ -780,6 +781,18 @@ '*seal': 'bool' }, 'if': 'CONFIG_LINUX' } +## +# @MemoryBackendGuestMemfdProperties: +# +# Properties for memory-backend-guest-memfd objects. +# +# Since: 11.1 +## +{ 'struct': 'MemoryBackendGuestMemfdProperties', + 'base': 'MemoryBackendProperties', + 'data': {}, + 'if': 'CONFIG_LINUX' } + ## # @MemoryBackendShmProperties: #
@@ -1234,6 +1247,8 @@ 'memory-backend-file', { 'name': 'memory-backend-memfd', 'if': 'CONFIG_LINUX' }, + { 'name': 'memory-backend-guest-memfd', + 'if': 'CONFIG_LINUX' }, 'memory-backend-ram', { 'name': 'memory-backend-shm', 'if': 'CONFIG_POSIX' },
@@ -1312,6 +1327,8 @@ 'memory-backend-file': 'MemoryBackendFileProperties', 'memory-backend-memfd': { 'type': 'MemoryBackendMemfdProperties', 'if': 'CONFIG_LINUX' }, + 'memory-backend-guest-memfd': { 'type': 'MemoryBackendGuestMemfdProperties', + 'if': 'CONFIG_LINUX' }, 'memory-backend-ram': 'MemoryBackendProperties', 'memory-backend-shm': { 'type': 'MemoryBackendShmProperties', 'if': 'CONFIG_POSIX' },
qemu-options.hx +5 -0
@@ -5858,6 +5858,11 @@ SRST off will cause a failure during allocation because it is not supported by this backend. + ``-object memory-backend-guest-memfd,id=id,prealloc=on|off,size=size,host-nodes=host-nodes,policy=default|preferred|bind|interleave`` + Creates an anonymous memory file backend object that has similar + semantics to memfd, but is also usable as private memory when + running as a confidential VM. (Linux only) + ``-object iommufd,id=id[,fd=fd]`` Creates an iommufd backend which allows control of DMA mapping through the ``/dev/iommu`` device.
Signature · 1 line
2.43.0
Re: [PATCH RFC 02/12] hostmem: Introduce dedicated memory backend for guest_memfd
Quoted 11 lines · Michael Roth
In the initial implementation of
guest_memfdin the linux kernel, it was not possible to map memory into userspace for direct access; instead the memory provided by the memory backend would be used for cases where a confidential VM wants to access normal/unprotected/unencrypted memory that can be used for shared memory use cases, and for access to private memory aguest_memfdcould be associated with the same memslot. A memory 'private' attribute set viaKVM_SET_MEMORY_ATTRIBUTEScould then be used to have KVM route to the approprate backing memory.In that model, it didn't make sense to introduce a specific backend for
guest_memfd, since there was always a generally need to have a separate
a general need?
Quoted 20 lines
backend type to handle shared memory access/allocation. Instead, QEMU configures the
guest_memfdsupport for the associated memslots internally for cases where it is running a confidential VM.However, with recent changes in
guest_memfdkernel support, it is now possible tommap()aguest_memfdFD into userspace and use it for shared memory, as well as continue to use the same physical pages for the same GPA ranges after they are converted to private ("in-place conversion").To enable the use of this mmap()-able/guest_memfd-provided memory to be used for normal/shared memory instead of just for private memory, introduce a dedicated
guest_memfdmemory backend that can be used both for confidential VMs that wish to make use of in-place conversion, as well as for non-confidential VMs that just want to make use ofguest_memfdfor normal memory (which can be useful both for testing as well as a stepping stone to things like software-protected VMs where the host can be trusted to provided some additional degree of isolation for the VM independently of hardware support).Signed-off-by Michael Roth <[email protected]>
[...]
Quoted 20 lines
qapi/qom.json +4
-1@@ -661,7 +661,8 @@ # @share: if false, the memory is private to QEMU; if true, it is # shared (default false for backends memory-backend-file and # memory-backend-ram, true for backends memory-backend-epc, -# memory-backend-memfd, and memory-backend-shm) +# memory-backend-memfd, memory-backend-shm, and +# memory-backend-guest-memfd) # # @reserve: if true, reserve swap space (or huge pages) if applicable # (default: true) (since 6.1)@@ -780,6 +781,18 @@ '*seal': 'bool' }, 'if': 'CONFIG_LINUX' } +## +# @MemoryBackendGuestMemfdProperties:
qapi/qom.json line 795
Quoted 10 lines
qapi/qom.json +10
-0+# +# Properties for memory-backend-guest-memfd objects. +# +# Since: 11.1 +## +{ 'struct': 'MemoryBackendGuestMemfdProperties', + 'base': 'MemoryBackendProperties', + 'data': {}, + 'if': 'CONFIG_LINUX' } +
Identical to MemoryBackendProperties so far.
Quoted 13 lines
qapi/qom.json +2
-0## # @MemoryBackendShmProperties: #@@ -1234,6 +1247,8 @@ 'memory-backend-file', { 'name': 'memory-backend-memfd', 'if': 'CONFIG_LINUX' }, + { 'name': 'memory-backend-guest-memfd', + 'if': 'CONFIG_LINUX' }, 'memory-backend-ram', { 'name': 'memory-backend-shm', 'if': 'CONFIG_POSIX' },
qapi/qom.json line 1331
Quoted 5 lines
qapi/qom.json +2
-0@@ -1312,6 +1327,8 @@ 'memory-backend-file': 'MemoryBackendFileProperties', 'memory-backend-memfd': { 'type': 'MemoryBackendMemfdProperties', 'if': 'CONFIG_LINUX' }, + 'memory-backend-guest-memfd': { 'type': 'MemoryBackendGuestMemfdProperties', + 'if': 'CONFIG_LINUX' },
You could use MemoryBackendProperties here, and drop MemoryBackendGuestMemfdProperties, similar to how memory-backend-ram is done.
qapi/qom.json line 1334
Quoted 3 lines
qapi/qom.json +0
-0'memory-backend-ram': 'MemoryBackendProperties', 'memory-backend-shm': { 'type': 'MemoryBackendShmProperties', 'if': 'CONFIG_POSIX' },
Should we provide guidance on when to use which memory backend? The commit message provides some clues...
qemu-options.hx SRST · line 5864
Quoted 12 lines
qemu-options.hx +4
-0@@ -5858,6 +5858,11 @@ SRST off will cause a failure during allocation because it is not supported by this backend. + ``-object memory-backend-guest-memfd,id=id,prealloc=on|off,size=size,host-nodes=host-nodes,policy=default|preferred|bind|interleave`` + Creates an anonymous memory file backend object that has similar + semantics to memfd, but is also usable as private memory when + running as a confidential VM. (Linux only)
There is no object type "memfd". Do you mean "memory-backend-memfd"?
If yes, that one has additional properties @hugetlb, @hugetlbsize, and @seal. Why are they not needed for memory-backend-guest-memfd?
Quoted 4 lines
qemu-options.hx +1
-0+ ``-object iommufd,id=id[,fd=fd]`` Creates an iommufd backend which allows control of DMA mapping through the ``/dev/iommu`` device.
Re: [PATCH RFC 02/12] hostmem: Introduce dedicated memory backend for guest_memfd
Quoted 11 lines · Markus Armbruster
Quoted 9 lines · Michael Roth
In the initial implementation of
guest_memfdin the linux kernel, it was not possible to map memory into userspace for direct access; instead the memory provided by the memory backend would be used for cases where a confidential VM wants to access normal/unprotected/unencrypted memory that can be used for shared memory use cases, and for access to private memory aguest_memfdcould be associated with the same memslot. A memory 'private' attribute set viaKVM_SET_MEMORY_ATTRIBUTEScould then be used to have KVM route to the approprate backing memory.
Quoted 4 lines
Quoted 2 lines
In that model, it didn't make sense to introduce a specific backend for
guest_memfd, since there was always a generally need to have a separatea general need?
Much nicer :)
Quoted 71 lines
Quoted 21 lines
backend type to handle shared memory access/allocation. Instead, QEMU configures the
guest_memfdsupport for the associated memslots internally for cases where it is running a confidential VM.However, with recent changes in
guest_memfdkernel support, it is now possible tommap()aguest_memfdFD into userspace and use it for shared memory, as well as continue to use the same physical pages for the same GPA ranges after they are converted to private ("in-place conversion").To enable the use of this mmap()-able/guest_memfd-provided memory to be used for normal/shared memory instead of just for private memory, introduce a dedicated
guest_memfdmemory backend that can be used both for confidential VMs that wish to make use of in-place conversion, as well as for non-confidential VMs that just want to make use ofguest_memfdfor normal memory (which can be useful both for testing as well as a stepping stone to things like software-protected VMs where the host can be trusted to provided some additional degree of isolation for the VM independently of hardware support).Signed-off-by Michael Roth <[email protected]>[...]
Quoted 20 lines
qapi/qom.json +4
-1@@ -661,7 +661,8 @@ # @share: if false, the memory is private to QEMU; if true, it is # shared (default false for backends memory-backend-file and # memory-backend-ram, true for backends memory-backend-epc, -# memory-backend-memfd, and memory-backend-shm) +# memory-backend-memfd, memory-backend-shm, and +# memory-backend-guest-memfd) # # @reserve: if true, reserve swap space (or huge pages) if applicable # (default: true) (since 6.1)@@ -780,6 +781,18 @@ '*seal': 'bool' }, 'if': 'CONFIG_LINUX' } +## +# @MemoryBackendGuestMemfdProperties:qapi/qom.json line 795
Quoted 10 lines
qapi/qom.json +10
-0+# +# Properties for memory-backend-guest-memfd objects. +# +# Since: 11.1 +## +{ 'struct': 'MemoryBackendGuestMemfdProperties', + 'base': 'MemoryBackendProperties', + 'data': {}, + 'if': 'CONFIG_LINUX' } +Identical to MemoryBackendProperties so far.
Quoted 14 lines
qapi/qom.json +2
-0## # @MemoryBackendShmProperties: #@@ -1234,6 +1247,8 @@ 'memory-backend-file', { 'name': 'memory-backend-memfd', 'if': 'CONFIG_LINUX' }, + { 'name': 'memory-backend-guest-memfd', + 'if': 'CONFIG_LINUX' }, 'memory-backend-ram', { 'name': 'memory-backend-shm', 'if': 'CONFIG_POSIX' },@@ -1312,6 +1327,8 @@ 'memory-backend-file': 'MemoryBackendFileProperties',
Quoted 8 lines
qapi/qom.json line 1331
Quoted 4 lines
qapi/qom.json +2
-0'memory-backend-memfd': { 'type': 'MemoryBackendMemfdProperties', 'if': 'CONFIG_LINUX' }, + 'memory-backend-guest-memfd': { 'type': 'MemoryBackendGuestMemfdProperties', + 'if': 'CONFIG_LINUX' },You could use MemoryBackendProperties here, and drop MemoryBackendGuestMemfdProperties, similar to how memory-backend-ram is done.
That's true. I think I was anticipating it being warranted at some point, but that doesn't need to happen here.
Quoted 7 lines
qapi/qom.json line 1334
Quoted 4 lines
qapi/qom.json +0
-0'memory-backend-ram': 'MemoryBackendProperties', 'memory-backend-shm': { 'type': 'MemoryBackendShmProperties', 'if': 'CONFIG_POSIX' },Should we provide guidance on when to use which memory backend? The commit message provides some clues...
Were you thinking from a schema perspective, or something more user-facing?
Either way, docs/system/confidential-guest-support.rst could definitely use some sprucing up as part of this series, so I can cover this aspect there as well.
Quoted 9 lines
Quoted 9 lines
qemu-options.hx +0
-0@@ -5858,6 +5858,11 @@ SRST off will cause a failure during allocation because it is not supported by this backend.
Quoted 6 lines
qemu-options.hx SRST · line 5864
Quoted 4 lines
qemu-options.hx +4
-0+ ``-object memory-backend-guest-memfd,id=id,prealloc=on|off,size=size,host-nodes=host-nodes,policy=default|preferred|bind|interleave`` + Creates an anonymous memory file backend object that has similar + semantics to memfd, but is also usable as private memory when + running as a confidential VM. (Linux only)There is no object type "memfd". Do you mean "memory-backend-memfd"?
Yes, will update.
Quoted 3 lines
If yes, that one has additional properties @hugetlb, @hugetlbsize, and @seal. Why are they not needed for memory-backend-guest-memfd?
ATM, hugetlb is not enabled for guest_memfd in the kernel. It's likely the same set of options will apply, but there are also efforts to do things like plumb DAX memory through guest_memfd for confidential VMs where maybe we end up needing to be a bit more flexible/creative... not sure, but it seemed like a good idea to give ourselves a clean slate since the support isn't there yet anyway.
For seal, I'm not aware of any plan to support that for guest_memfd, so it seems like unecessary baggage to pull in.
Thanks,
Mike
Quoted 6 lines
Quoted 5 lines
qemu-options.hx +1
-0+ ``-object iommufd,id=id[,fd=fd]`` Creates an iommufd backend which allows control of DMA mapping through the ``/dev/iommu`` device.
Re: [PATCH RFC 02/12] hostmem: Introduce dedicated memory backend for guest_memfd
Quoted 107 lines · Michael Roth
Quoted 11 lines · Markus Armbruster
Quoted 9 lines · Michael Roth
In the initial implementation of
guest_memfdin the linux kernel, it was not possible to map memory into userspace for direct access; instead the memory provided by the memory backend would be used for cases where a confidential VM wants to access normal/unprotected/unencrypted memory that can be used for shared memory use cases, and for access to private memory aguest_memfdcould be associated with the same memslot. A memory 'private' attribute set viaKVM_SET_MEMORY_ATTRIBUTEScould then be used to have KVM route to the approprate backing memory.Quoted 4 lines
Quoted 2 lines
In that model, it didn't make sense to introduce a specific backend for
guest_memfd, since there was always a generally need to have a separatea general need?
Much nicer :)
Quoted 71 lines
Quoted 21 lines
backend type to handle shared memory access/allocation. Instead, QEMU configures the
guest_memfdsupport for the associated memslots internally for cases where it is running a confidential VM.However, with recent changes in
guest_memfdkernel support, it is now possible tommap()aguest_memfdFD into userspace and use it for shared memory, as well as continue to use the same physical pages for the same GPA ranges after they are converted to private ("in-place conversion").To enable the use of this mmap()-able/guest_memfd-provided memory to be used for normal/shared memory instead of just for private memory, introduce a dedicated
guest_memfdmemory backend that can be used both for confidential VMs that wish to make use of in-place conversion, as well as for non-confidential VMs that just want to make use ofguest_memfdfor normal memory (which can be useful both for testing as well as a stepping stone to things like software-protected VMs where the host can be trusted to provided some additional degree of isolation for the VM independently of hardware support).Signed-off-by Michael Roth <[email protected]>[...]
Quoted 20 lines
qapi/qom.json +4
-1@@ -661,7 +661,8 @@ # @share: if false, the memory is private to QEMU; if true, it is # shared (default false for backends memory-backend-file and # memory-backend-ram, true for backends memory-backend-epc, -# memory-backend-memfd, and memory-backend-shm) +# memory-backend-memfd, memory-backend-shm, and +# memory-backend-guest-memfd) # # @reserve: if true, reserve swap space (or huge pages) if applicable # (default: true) (since 6.1)@@ -780,6 +781,18 @@ '*seal': 'bool' }, 'if': 'CONFIG_LINUX' } +## +# @MemoryBackendGuestMemfdProperties:qapi/qom.json line 795
Quoted 10 lines
qapi/qom.json +10
-0+# +# Properties for memory-backend-guest-memfd objects. +# +# Since: 11.1 +## +{ 'struct': 'MemoryBackendGuestMemfdProperties', + 'base': 'MemoryBackendProperties', + 'data': {}, + 'if': 'CONFIG_LINUX' } +Identical to MemoryBackendProperties so far.
Quoted 14 lines
qapi/qom.json +2
-0## # @MemoryBackendShmProperties: #@@ -1234,6 +1247,8 @@ 'memory-backend-file', { 'name': 'memory-backend-memfd', 'if': 'CONFIG_LINUX' }, + { 'name': 'memory-backend-guest-memfd', + 'if': 'CONFIG_LINUX' }, 'memory-backend-ram', { 'name': 'memory-backend-shm', 'if': 'CONFIG_POSIX' },@@ -1312,6 +1327,8 @@ 'memory-backend-file': 'MemoryBackendFileProperties',Quoted 8 lines
qapi/qom.json line 1331
Quoted 4 lines
qapi/qom.json +2
-0'memory-backend-memfd': { 'type': 'MemoryBackendMemfdProperties', 'if': 'CONFIG_LINUX' }, + 'memory-backend-guest-memfd': { 'type': 'MemoryBackendGuestMemfdProperties', + 'if': 'CONFIG_LINUX' },You could use MemoryBackendProperties here, and drop MemoryBackendGuestMemfdProperties, similar to how memory-backend-ram is done.
That's true. I think I was anticipating it being warranted at some point, but that doesn't need to happen here.
Quoted 5 lines
Quoted 4 lines
qapi/qom.json +0
-0'memory-backend-ram': 'MemoryBackendProperties', 'memory-backend-shm': { 'type': 'MemoryBackendShmProperties', 'if': 'CONFIG_POSIX' },
Quoted 5 lines
Quoted 2 lines
Should we provide guidance on when to use which memory backend? The commit message provides some clues...
Were you thinking from a schema perspective, or something more user-facing?
The QAPI schema doc comments become the QEMU QMP Reference Manual, which I believe is the first stop for "how do I use this?"
Sometimes, a full answer just doesn't fit there comfortably. So we put it elsewhere, and point to it from the QMP Reference.
Quoted 26 lines
Either way,
docs/system/confidential-guest-support.rstcould definitely use some sprucing up as part of this series, so I can cover this aspect there as well.Quoted 9 lines
Quoted 9 lines
qemu-options.hx +0
-0@@ -5858,6 +5858,11 @@ SRST off will cause a failure during allocation because it is not supported by this backend.Quoted 6 lines
qemu-options.hx SRST · line 5864
Quoted 4 lines
qemu-options.hx +4
-0+ ``-object memory-backend-guest-memfd,id=id,prealloc=on|off,size=size,host-nodes=host-nodes,policy=default|preferred|bind|interleave`` + Creates an anonymous memory file backend object that has similar + semantics to memfd, but is also usable as private memory when + running as a confidential VM. (Linux only)There is no object type "memfd". Do you mean "memory-backend-memfd"?
Yes, will update.
Quoted 3 lines
If yes, that one has additional properties @hugetlb, @hugetlbsize, and @seal. Why are they not needed for memory-backend-guest-memfd?
Quoted 6 lines
ATM, hugetlb is not enabled for
guest_memfdin the kernel. It's likely the same set of options will apply, but there are also efforts to do things like plumb DAX memory throughguest_memfdfor confidential VMs where maybe we end up needing to be a bit more flexible/creative... not sure, but it seemed like a good idea to give ourselves a clean slate since the support isn't there yet anyway.
I gather these properties cannot work today. I agree we shouldn't add them until they do.
Quoted 2 lines
For seal, I'm not aware of any plan to support that for
guest_memfd, so it seems like unecessary baggage to pull in.
Likewise.
Quoted 10 lines
Thanks,
Mike
Quoted 6 lines
Quoted 5 lines
qemu-options.hx +1
-0+ ``-object iommufd,id=id[,fd=fd]`` Creates an iommufd backend which allows control of DMA mapping through the ``/dev/iommu`` device.
Re: [PATCH RFC 02/12] hostmem: Introduce dedicated memory backend for guest_memfd
Quoted 112 lines · Markus Armbruster
Quoted 110 lines · Michael Roth
Quoted 11 lines · Markus Armbruster
Quoted 9 lines · Michael Roth
In the initial implementation of
guest_memfdin the linux kernel, it was not possible to map memory into userspace for direct access; instead the memory provided by the memory backend would be used for cases where a confidential VM wants to access normal/unprotected/unencrypted memory that can be used for shared memory use cases, and for access to private memory aguest_memfdcould be associated with the same memslot. A memory 'private' attribute set viaKVM_SET_MEMORY_ATTRIBUTEScould then be used to have KVM route to the approprate backing memory.Quoted 4 lines
Quoted 2 lines
In that model, it didn't make sense to introduce a specific backend for
guest_memfd, since there was always a generally need to have a separatea general need?
Much nicer :)
Quoted 71 lines
Quoted 21 lines
backend type to handle shared memory access/allocation. Instead, QEMU configures the
guest_memfdsupport for the associated memslots internally for cases where it is running a confidential VM.However, with recent changes in
guest_memfdkernel support, it is now possible tommap()aguest_memfdFD into userspace and use it for shared memory, as well as continue to use the same physical pages for the same GPA ranges after they are converted to private ("in-place conversion").To enable the use of this mmap()-able/guest_memfd-provided memory to be used for normal/shared memory instead of just for private memory, introduce a dedicated
guest_memfdmemory backend that can be used both for confidential VMs that wish to make use of in-place conversion, as well as for non-confidential VMs that just want to make use ofguest_memfdfor normal memory (which can be useful both for testing as well as a stepping stone to things like software-protected VMs where the host can be trusted to provided some additional degree of isolation for the VM independently of hardware support).Signed-off-by Michael Roth <[email protected]>[...]
Quoted 20 lines
qapi/qom.json +4
-1@@ -661,7 +661,8 @@ # @share: if false, the memory is private to QEMU; if true, it is # shared (default false for backends memory-backend-file and # memory-backend-ram, true for backends memory-backend-epc, -# memory-backend-memfd, and memory-backend-shm) +# memory-backend-memfd, memory-backend-shm, and +# memory-backend-guest-memfd) # # @reserve: if true, reserve swap space (or huge pages) if applicable # (default: true) (since 6.1)@@ -780,6 +781,18 @@ '*seal': 'bool' }, 'if': 'CONFIG_LINUX' } +## +# @MemoryBackendGuestMemfdProperties:qapi/qom.json line 795
Quoted 10 lines
qapi/qom.json +10
-0+# +# Properties for memory-backend-guest-memfd objects. +# +# Since: 11.1 +## +{ 'struct': 'MemoryBackendGuestMemfdProperties', + 'base': 'MemoryBackendProperties', + 'data': {}, + 'if': 'CONFIG_LINUX' } +Identical to MemoryBackendProperties so far.
Quoted 14 lines
qapi/qom.json +2
-0## # @MemoryBackendShmProperties: #@@ -1234,6 +1247,8 @@ 'memory-backend-file', { 'name': 'memory-backend-memfd', 'if': 'CONFIG_LINUX' }, + { 'name': 'memory-backend-guest-memfd', + 'if': 'CONFIG_LINUX' }, 'memory-backend-ram', { 'name': 'memory-backend-shm', 'if': 'CONFIG_POSIX' },@@ -1312,6 +1327,8 @@ 'memory-backend-file': 'MemoryBackendFileProperties',Quoted 8 lines
qapi/qom.json line 1331
Quoted 4 lines
qapi/qom.json +2
-0'memory-backend-memfd': { 'type': 'MemoryBackendMemfdProperties', 'if': 'CONFIG_LINUX' }, + 'memory-backend-guest-memfd': { 'type': 'MemoryBackendGuestMemfdProperties', + 'if': 'CONFIG_LINUX' },You could use MemoryBackendProperties here, and drop MemoryBackendGuestMemfdProperties, similar to how memory-backend-ram is done.
That's true. I think I was anticipating it being warranted at some point, but that doesn't need to happen here.
Quoted 7 lines
qapi/qom.json line 1334
Quoted 4 lines
qapi/qom.json +0
-0'memory-backend-ram': 'MemoryBackendProperties', 'memory-backend-shm': { 'type': 'MemoryBackendShmProperties', 'if': 'CONFIG_POSIX' },Should we provide guidance on when to use which memory backend? The commit message provides some clues...
Quoted 8 lines
Quoted 2 lines
Were you thinking from a schema perspective, or something more user-facing?
The QAPI schema doc comments become the QEMU QMP Reference Manual, which I believe is the first stop for "how do I use this?"
Sometimes, a full answer just doesn't fit there comfortably. So we put it elsewhere, and point to it from the QMP Reference.
Makes sense, I'll cross reference the documentation and provide some background on how the backends / options are used.
Thanks,
Mike
Quoted 34 lines
Quoted 27 lines
Either way,
docs/system/confidential-guest-support.rstcould definitely use some sprucing up as part of this series, so I can cover this aspect there as well.Quoted 9 lines
Quoted 9 lines
qemu-options.hx +0
-0@@ -5858,6 +5858,11 @@ SRST off will cause a failure during allocation because it is not supported by this backend.Quoted 6 lines
qemu-options.hx SRST · line 5864
Quoted 4 lines
qemu-options.hx +4
-0+ ``-object memory-backend-guest-memfd,id=id,prealloc=on|off,size=size,host-nodes=host-nodes,policy=default|preferred|bind|interleave`` + Creates an anonymous memory file backend object that has similar + semantics to memfd, but is also usable as private memory when + running as a confidential VM. (Linux only)There is no object type "memfd". Do you mean "memory-backend-memfd"?
Yes, will update.
Quoted 3 lines
If yes, that one has additional properties @hugetlb, @hugetlbsize, and @seal. Why are they not needed for memory-backend-guest-memfd?
Quoted 6 lines
ATM, hugetlb is not enabled for
guest_memfdin the kernel. It's likely the same set of options will apply, but there are also efforts to do things like plumb DAX memory throughguest_memfdfor confidential VMs where maybe we end up needing to be a bit more flexible/creative... not sure, but it seemed like a good idea to give ourselves a clean slate since the support isn't there yet anyway.
Quoted 7 lines
I gather these properties cannot work today. I agree we shouldn't add them until they do.
Quoted 2 lines
For seal, I'm not aware of any plan to support that for
guest_memfd, so it seems like unecessary baggage to pull in.Likewise.
Sounds good, though I'm sort of now leaning more toward the memory-backend-memfd,guest_memfd=on approach that Peter implemented[1] since it requires less assumptions about what we'll need to do later (i.e. if we want to introduce a backend specifically for guest_memfd we'll still have the option, but if we do it now, but decide to go back to re-using the existin *-memfd/*-file/*-etc backends because the option format seems more familiar to QEMU users, then the dedicated backend is a little bit more of a pain to turn around and try to deprecate.
Not sure yet what we'll end up doing though, but hopefully for v2 we'll have a plan for what to do initially at least.
Thanks,
Mike
[1] https://lore.kernel.org/qemu-devel/[email protected]/
Quoted 12 lines
Quoted 11 lines
Thanks,
Mike
Quoted 6 lines
Quoted 5 lines
qemu-options.hx +1
-0+ ``-object iommufd,id=id[,fd=fd]`` Creates an iommufd backend which allows control of DMA mapping through the ``/dev/iommu`` device.
RFC PATCH 3/12 linux-headers: Update headers for v7 of in-place conversion kernel support
In reply to Michael RothThis will also pull in kernel 7.1.0-rc2 definitions.
--- include/standard-headers/drm/drm_fourcc.h | 28 +- include/standard-headers/linux/const.h | 18 + include/standard-headers/linux/ethtool.h | 28 +- .../linux/input-event-codes.h | 13 + include/standard-headers/linux/pci_regs.h | 71 ++- include/standard-headers/linux/typelimits.h | 8 + include/standard-headers/linux/virtio_ring.h | 5 +- include/standard-headers/linux/virtio_rtc.h | 237 ++++++++++ include/standard-headers/linux/vmclock-abi.h | 20 + linux-headers/asm-arm64/kvm.h | 1 + linux-headers/asm-arm64/unistd_64.h | 1 + linux-headers/asm-generic/unistd.h | 5 +- linux-headers/asm-loongarch/kvm.h | 5 + linux-headers/asm-loongarch/kvm_para.h | 1 + linux-headers/asm-loongarch/unistd_64.h | 2 + linux-headers/asm-mips/unistd_n32.h | 1 + linux-headers/asm-mips/unistd_n64.h | 1 + linux-headers/asm-mips/unistd_o32.h | 1 + linux-headers/asm-powerpc/unistd_32.h | 1 + linux-headers/asm-powerpc/unistd_64.h | 1 + linux-headers/asm-riscv/kvm.h | 11 +- linux-headers/asm-riscv/ptrace.h | 37 ++ linux-headers/asm-riscv/unistd_32.h | 1 + linux-headers/asm-riscv/unistd_64.h | 1 + linux-headers/asm-s390/unistd_32.h | 446 ------------------ linux-headers/asm-s390/unistd_64.h | 1 + linux-headers/asm-x86/kvm.h | 21 +- linux-headers/asm-x86/unistd_32.h | 1 + linux-headers/asm-x86/unistd_64.h | 1 + linux-headers/asm-x86/unistd_x32.h | 1 + linux-headers/linux/const.h | 18 + linux-headers/linux/iommufd.h | 48 ++ linux-headers/linux/kvm.h | 62 ++- linux-headers/linux/mshv.h | 4 +- linux-headers/linux/psp-sev.h | 2 +- linux-headers/linux/stddef.h | 4 + linux-headers/linux/vduse.h | 85 +++- linux-headers/linux/vfio.h | 30 +- 38 files changed, 729 insertions(+), 493 deletions(-) create mode 100644 include/standard-headers/linux/typelimits.h create mode 100644 include/standard-headers/linux/virtio_rtc.h delete mode 100644 linux-headers/asm-s390/unistd_32.h
include/standard-headers/drm/drm_fourcc.h +22 -6
@@ -400,8 +400,8 @@ extern "C" {* implementation can multiply the values by 2^6=64. For that reason the padding * must only contain zeros. * index 0 = Y plane, [15:0] z:Y [6:10] little endian -* index 1 = Cr plane, [15:0] z:Cr [6:10] little endian -* index 2 = Cb plane, [15:0] z:Cb [6:10] little endian +* index 1 = Cb plane, [15:0] z:Cb [6:10] little endian +* index 2 = Cr plane, [15:0] z:Cr [6:10] little endian */ #define DRM_FORMAT_S010 fourcc_code('S', '0', '1', '0')/* 2x2 subsampled Cb (1) and Cr (2) planes 10 bits per channel */ #define DRM_FORMAT_S210 fourcc_code('S', '2', '1', '0')/* 2x1 subsampled Cb (1) and Cr (2) planes 10 bits per channel */
@@ -413,8 +413,8 @@ extern "C" {* implementation can multiply the values by 2^4=16. For that reason the padding * must only contain zeros. * index 0 = Y plane, [15:0] z:Y [4:12] little endian -* index 1 = Cr plane, [15:0] z:Cr [4:12] little endian -* index 2 = Cb plane, [15:0] z:Cb [4:12] little endian +* index 1 = Cb plane, [15:0] z:Cb [4:12] little endian +* index 2 = Cr plane, [15:0] z:Cr [4:12] little endian */ #define DRM_FORMAT_S012 fourcc_code('S', '0', '1', '2')/* 2x2 subsampled Cb (1) and Cr (2) planes 12 bits per channel */ #define DRM_FORMAT_S212 fourcc_code('S', '2', '1', '2')/* 2x1 subsampled Cb (1) and Cr (2) planes 12 bits per channel */
@@ -423,8 +423,8 @@ extern "C" {/* * 3 plane YCbCr * index 0 = Y plane, [15:0] Y little endian -* index 1 = Cr plane, [15:0] Cr little endian -* index 2 = Cb plane, [15:0] Cb little endian +* index 1 = Cb plane, [15:0] Cb little endian +* index 2 = Cr plane, [15:0] Cr little endian */ #define DRM_FORMAT_S016 fourcc_code('S', '0', '1', '6')/* 2x2 subsampled Cb (1) and Cr (2) planes 16 bits per channel */ #define DRM_FORMAT_S216 fourcc_code('S', '2', '1', '6')/* 2x1 subsampled Cb (1) and Cr (2) planes 16 bits per channel */
@@ -1421,6 +1421,22 @@ drm_fourcc_canonicalize_nvidia_format_mod(uint64_t modifier)#define DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED \ DRM_FORMAT_MOD_ARM_CODE(DRM_FORMAT_MOD_ARM_TYPE_MISC, 1ULL) +/* +* ARM 64k interleaved modifier +* +* This is used by ARM Mali v10+ GPUs. With this modifier, the plane is divided +* into 64k byte 1:1 or 2:1 -sided tiles. The 64k tiles are laid out linearly. +* Each 64k tile is divided into blocks of 16x16 texel blocks, which are +* themselves laid out linearly within a 64k tile. Then within each 16x16 +* block, texel blocks are laid out according to U order, similar to +* 16X16_BLOCK_U_INTERLEAVED. +* +* Note that unlike 16X16_BLOCK_U_INTERLEAVED, the layout does not change +* depending on whether a format is compressed or not. +*/ +#define DRM_FORMAT_MOD_ARM_INTERLEAVED_64K \ + DRM_FORMAT_MOD_ARM_CODE(DRM_FORMAT_MOD_ARM_TYPE_MISC, 2ULL) +/* * Allwinner tiled modifier *
include/standard-headers/linux/const.h +18 -0
@@ -50,4 +50,22 @@#define __KERNEL_DIV_ROUND_UP (n, d) (((n) + (d) - 1) / (d)) +/* +* Divide positive or negative dividend by positive or negative divisor +* and round to closest integer. Result is undefined for negative +* divisors if the dividend variable type is unsigned and for negative +* dividends if the divisor variable type is unsigned. +*/ +#define __KERNEL_DIV_ROUND_CLOSEST (x, divisor) \ +({ \ + __typeof__(x) __x = x; \ + __typeof__(divisor) __d = divisor; \ + \ + (((__typeof__(x))-1) > 0 || \ + ((__typeof__(divisor))-1) > 0 || \ + (((__x) > 0) == ((__d) > 0))) ? \ + (((__x) + ((__d) / 2)) / (__d)) : \ + (((__x) - ((__d) / 2)) / (__d)); \ +}) +#endif /* _LINUX_CONST_H */
include/standard-headers/linux/ethtool.h +22 -6
@@ -17,11 +17,10 @@#include "net/eth.h" #include "standard-headers/linux/const.h" +#include "standard-headers/linux/typelimits.h" #include "standard-headers/linux/types.h" #include "standard-headers/linux/if_ether.h" -#include <limits.h> /* for INT_MAX */ -/* All structures exposed to userland should be defined such that they * have the same layout for 32-bit and 64-bit userland. */
@@ -228,7 +227,7 @@ enum tunable_id {ETHTOOL_ID_UNSPEC ,ETHTOOL_RX_COPYBREAK ,ETHTOOL_TX_COPYBREAK , -ETHTOOL_PFC_PREVENTION_TOUT ,/* timeout in msecs */ +ETHTOOL_PFC_PREVENTION_TOUT ,/* both pause and pfc, see man ethtool */ ETHTOOL_TX_COPYBREAK_BUF_SIZE ,/* * Add your fresh new tunable attribute above and remember to update
@@ -603,6 +602,8 @@ enum ethtool_link_ext_state {ETHTOOL_LINK_EXT_STATE_POWER_BUDGET_EXCEEDED ,ETHTOOL_LINK_EXT_STATE_OVERHEAT ,ETHTOOL_LINK_EXT_STATE_MODULE , +ETHTOOL_LINK_EXT_STATE_OTP_SPEED_VIOLATION , + ETHTOOL_LINK_EXT_STATE_BMC_REQUEST_DOWN, };/* More information in addition to ETHTOOL_LINK_EXT_STATE_AUTONEG. */
@@ -1094,13 +1095,20 @@ enum ethtool_module_fw_flash_status {* struct ethtool_gstrings - string set for data tagging * @cmd: Command number = %ETHTOOL_GSTRINGS * @string_set: String set ID; one of &enum ethtool_stringset -* @len: On return, the number of strings in the string set +* @len: Number of strings in the string set * @data: Buffer for strings. Each string is null-padded to a size of * %ETH_GSTRING_LEN. * * Users must use %ETHTOOL_GSSET_INFO to find the number of strings in * the string set. They must allocate a buffer of the appropriate * size immediately following this structure. +* +* Setting @len on input is optional (though preferred), but must be zeroed +* otherwise. +* When set, @len will return the requested count if it matches the actual +* count; otherwise, it will be zero. +* This prevents issues when the number of strings is different than the +* userspace allocation. */ struct ethtool_gstrings {uint32_t cmd;
@@ -1177,13 +1185,20 @@ struct ethtool_test {/** * struct ethtool_stats - device-specific statistics * @cmd: Command number = %ETHTOOL_GSTATS -* @n_stats: On return, the number of statistics +* @n_stats: Number of statistics * @data: Array of statistics * * Users must use %ETHTOOL_GSSET_INFO or %ETHTOOL_GDRVINFO to find the * number of statistics that will be returned. They must allocate a * buffer of the appropriate size (8 * number of statistics) * immediately following this structure. +* +* Setting @n_stats on input is optional (though preferred), but must be zeroed +* otherwise. +* When set, @n_stats will return the requested count if it matches the actual +* count; otherwise, it will be zero. +* This prevents issues when the number of stats is different than the +* userspace allocation. */ struct ethtool_stats {uint32_t cmd;
@@ -2190,6 +2205,7 @@ enum ethtool_link_mode_bit_indices {#define SPEED_40000 40000#define SPEED_50000 50000#define SPEED_56000 56000 +#define SPEED_80000 80000#define SPEED_100000 100000#define SPEED_200000 200000#define SPEED_400000 400000
@@ -2200,7 +2216,7 @@ enum ethtool_link_mode_bit_indices {static inline int ethtool_validate_speed (uint32_t speed) { -return speed <=INT_MAX || speed == (uint32_t )SPEED_UNKNOWN ; +return speed <= __KERNEL_INT_MAX || speed == (uint32_t )SPEED_UNKNOWN ; }/* Duplex, half or full. */
include/standard-headers/linux/input-event-codes.h +13 -0
@@ -643,6 +643,10 @@#define KEY_EPRIVACY_SCREEN_ON 0x252#define KEY_EPRIVACY_SCREEN_OFF 0x253 +#define KEY_ACTION_ON_SELECTION 0x254/* AL Action on Selection (HUTRR119) */ +#define KEY_CONTEXTUAL_INSERT 0x255/* AL Contextual Insertion (HUTRR119) */ +#define KEY_CONTEXTUAL_QUERY 0x256/* AL Contextual Query (HUTRR119) */ +#define KEY_KBDINPUTASSIST_PREV 0x260#define KEY_KBDINPUTASSIST_NEXT 0x261#define KEY_KBDINPUTASSIST_PREVGROUP 0x262
@@ -891,6 +895,7 @@#define ABS_VOLUME 0x20#define ABS_PROFILE 0x21 +#define ABS_SND_PROFILE 0x22#define ABS_MISC 0x28
@@ -1000,4 +1005,12 @@#define SND_MAX 0x07#define SND_CNT (SND_MAX+1) +/* +* ABS_SND_PROFILE values +*/ + +#define SND_PROFILE_SILENT 0x00 +#define SND_PROFILE_VIBRATE 0x01 +#define SND_PROFILE_RING 0x02 +#endif
include/standard-headers/linux/pci_regs.h +65 -6
93 lines not on this page · raw message
include/standard-headers/linux/typelimits.h new file +8 -0
@@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +#ifndef _LINUX_TYPELIMITS_H +#define _LINUX_TYPELIMITS_H + +#define __KERNEL_INT_MAX ((int)(~0U >> 1)) +#define __KERNEL_INT_MIN (-__KERNEL_INT_MAX - 1) + +#endif /* _LINUX_TYPELIMITS_H */
include/standard-headers/linux/virtio_ring.h +3 -2
@@ -1,5 +1,7 @@#ifndef _LINUX_VIRTIO_RING_H#define _LINUX_VIRTIO_RING_H + +#define VIRTIO_RING_NO_LEGACY /* An interface for efficient virtio implementation, currently for use by KVM, * but hopefully others soon. Do NOT change this since it will * break existing servers and clients.
@@ -31,7 +33,6 @@* SUCH DAMAGE. * * Copyright Rusty Russell IBM Corporation 2007. */ -#include <stdint.h> #include "standard-headers/linux/types.h" #include "standard-headers/linux/virtio_types.h"
@@ -200,7 +201,7 @@ static inline void vring_init(struct vring *vr, unsigned int num, void *p, vr->num = num; vr->desc = p; vr->avail = (struct vring_avail *)((char *)p + num *sizeof (struct vring_desc )); - vr->used = (void *)(((uintptr_t )&vr->avail->ring[num] +sizeof (__virtio16) + vr->used = (void *)(((unsigned long )&vr->avail->ring[num] +sizeof (__virtio16) + align-1 ) & ~(align -1 )); }
include/standard-headers/linux/virtio_rtc.h new file +237 -0
237 lines not on this page · raw message
include/standard-headers/linux/vmclock-abi.h +20 -0
@@ -115,6 +115,17 @@ struct vmclock_abi {* bit again after the update, using the about-to-be-valid fields. */ #define VMCLOCK_FLAG_TIME_MONOTONIC (1 << 7) +/* +* If the VM_GEN_COUNTER_PRESENT flag is set, the hypervisor will +* bump the vm_generation_counter field every time the guest is +* loaded from some save state (restored from a snapshot). +*/ +#define VMCLOCK_FLAG_VM_GEN_COUNTER_PRESENT (1 << 8) +/* +* If the NOTIFICATION_PRESENT flag is set, the hypervisor will send +* a notification every time it updates seq_count to a new even number. +*/ +#define VMCLOCK_FLAG_NOTIFICATION_PRESENT (1 << 9)uint8_t pad[2 ];uint8_t clock_status;
@@ -177,6 +188,15 @@ struct vmclock_abi {uint64_t time_frac_sec;/* Units of 1/2^64 of a second */ uint64_t time_esterror_nanosec;uint64_t time_maxerror_nanosec; + +/* +* This field changes to another non-repeating value when the guest +* has been loaded from a snapshot. In addition to handling a +* disruption in time (which will also be signalled through the +* disruption_marker field), a guest may wish to discard UUIDs, +* reset network connections, reseed entropy, etc. +*/ + uint64_t vm_generation_counter; };#endif /* __VMCLOCK_ABI_H__ */
linux-headers/asm-arm64/kvm.h +1 -0
@@ -416,6 +416,7 @@ enum {#define KVM_DEV_ARM_ITS_RESTORE_TABLES 2#define KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES 3#define KVM_DEV_ARM_ITS_CTRL_RESET 4 +#define KVM_DEV_ARM_VGIC_USERSPACE_PPIS 5/* Device Control API on vcpu fd */ #define KVM_ARM_VCPU_PMU_V3_CTRL 0
linux-headers/asm-arm64/unistd_64.h +1 -0
@@ -327,6 +327,7 @@#define __NR_file_getattr 468#define __NR_file_setattr 469#define __NR_listns 470 +#define __NR_rseq_slice_yield 471#endif /* _ASM_UNISTD_64_H */
linux-headers/asm-generic/unistd.h +4 -1
@@ -860,8 +860,11 @@ __SYSCALL(__NR_file_setattr, sys_file_setattr)#define __NR_listns 470 __SYSCALL(__NR_listns, sys_listns) +#define __NR_rseq_slice_yield 471 +__SYSCALL(__NR_rseq_slice_yield, sys_rseq_slice_yield) +#undef __NR_syscalls -#define __NR_syscalls 471 +#define __NR_syscalls 472/* * 32 bit systems traditionally used different
linux-headers/asm-loongarch/kvm.h +5 -0
@@ -105,6 +105,7 @@ struct kvm_fpu {#define KVM_LOONGARCH_VM_FEAT_PV_STEALTIME 7#define KVM_LOONGARCH_VM_FEAT_PTW 8#define KVM_LOONGARCH_VM_FEAT_MSGINT 9 +#define KVM_LOONGARCH_VM_FEAT_PV_PREEMPT 10/* Device Control API on vcpu fd */ #define KVM_LOONGARCH_VCPU_CPUCFG 0
@@ -154,4 +155,8 @@ struct kvm_iocsr_entry {#define KVM_DEV_LOONGARCH_PCH_PIC_GRP_CTRL 0x40000006#define KVM_DEV_LOONGARCH_PCH_PIC_CTRL_INIT 0 +#define KVM_DEV_LOONGARCH_DMSINTC_GRP_CTRL 0x40000007 +#define KVM_DEV_LOONGARCH_DMSINTC_MSG_ADDR_BASE 0x0 +#define KVM_DEV_LOONGARCH_DMSINTC_MSG_ADDR_SIZE 0x1 +#endif /* __UAPI_ASM_LOONGARCH_KVM_H */
linux-headers/asm-loongarch/kvm_para.h +1 -0
@@ -15,6 +15,7 @@#define CPUCFG_KVM_FEATURE (CPUCFG_KVM_BASE + 4)#define KVM_FEATURE_IPI 1#define KVM_FEATURE_STEAL_TIME 2 +#define KVM_FEATURE_PREEMPT 3/* BIT 24 - 31 are features configurable by user space vmm */ #define KVM_FEATURE_VIRT_EXTIOI 24#define KVM_FEATURE_USER_HCALL 25
linux-headers/asm-loongarch/unistd_64.h +2 -0
@@ -300,6 +300,7 @@#define __NR_landlock_create_ruleset 444#define __NR_landlock_add_rule 445#define __NR_landlock_restrict_self 446 +#define __NR_memfd_secret 447#define __NR_process_mrelease 448#define __NR_futex_waitv 449#define __NR_set_mempolicy_home_node 450
@@ -323,6 +324,7 @@#define __NR_file_getattr 468#define __NR_file_setattr 469#define __NR_listns 470 +#define __NR_rseq_slice_yield 471#endif /* _ASM_UNISTD_64_H */
linux-headers/asm-mips/unistd_n32.h +1 -0
@@ -399,5 +399,6 @@#define __NR_file_getattr (__NR_Linux + 468)#define __NR_file_setattr (__NR_Linux + 469)#define __NR_listns (__NR_Linux + 470) +#define __NR_rseq_slice_yield (__NR_Linux + 471)#endif /* _ASM_UNISTD_N32_H */
linux-headers/asm-mips/unistd_n64.h +1 -0
@@ -375,5 +375,6 @@#define __NR_file_getattr (__NR_Linux + 468)#define __NR_file_setattr (__NR_Linux + 469)#define __NR_listns (__NR_Linux + 470) +#define __NR_rseq_slice_yield (__NR_Linux + 471)#endif /* _ASM_UNISTD_N64_H */
linux-headers/asm-mips/unistd_o32.h +1 -0
@@ -445,5 +445,6 @@#define __NR_file_getattr (__NR_Linux + 468)#define __NR_file_setattr (__NR_Linux + 469)#define __NR_listns (__NR_Linux + 470) +#define __NR_rseq_slice_yield (__NR_Linux + 471)#endif /* _ASM_UNISTD_O32_H */
linux-headers/asm-powerpc/unistd_32.h +1 -0
@@ -452,6 +452,7 @@#define __NR_file_getattr 468#define __NR_file_setattr 469#define __NR_listns 470 +#define __NR_rseq_slice_yield 471#endif /* _ASM_UNISTD_32_H */
linux-headers/asm-powerpc/unistd_64.h +1 -0
@@ -424,6 +424,7 @@#define __NR_file_getattr 468#define __NR_file_setattr 469#define __NR_listns 470 +#define __NR_rseq_slice_yield 471#endif /* _ASM_UNISTD_64_H */
linux-headers/asm-riscv/kvm.h +7 -4
@@ -110,6 +110,10 @@ struct kvm_riscv_timer {__u64 state; }; +/* Possible states for kvm_riscv_timer */ +#define KVM_RISCV_TIMER_STATE_OFF 0 +#define KVM_RISCV_TIMER_STATE_ON 1 +/* * ISA extension IDs specific to KVM. This is not the same as the host ISA * extension IDs as that is internal to the host and should not be exposed
@@ -192,6 +196,9 @@ enum KVM_RISCV_ISA_EXT_ID {KVM_RISCV_ISA_EXT_ZFBFMIN ,KVM_RISCV_ISA_EXT_ZVFBFMIN ,KVM_RISCV_ISA_EXT_ZVFBFWMA , +KVM_RISCV_ISA_EXT_ZCLSD , +KVM_RISCV_ISA_EXT_ZILSD , +KVM_RISCV_ISA_EXT_ZALASR , KVM_RISCV_ISA_EXT_MAX, };
@@ -235,10 +242,6 @@ struct kvm_riscv_sbi_fwft {struct kvm_riscv_sbi_fwft_feature pointer_masking; }; -/* Possible states for kvm_riscv_timer */ -#define KVM_RISCV_TIMER_STATE_OFF 0 -#define KVM_RISCV_TIMER_STATE_ON 1 -/* If you need to interpret the index values, here is the key: */ #define KVM_REG_RISCV_TYPE_MASK 0x00000000FF000000#define KVM_REG_RISCV_TYPE_SHIFT 24
linux-headers/asm-riscv/ptrace.h +37 -0
@@ -9,6 +9,7 @@#ifndef __ASSEMBLER__#include <linux/types.h> +#include <linux/const.h> #define PTRACE_GETFDPIC 33
@@ -127,6 +128,42 @@ struct __riscv_v_regset_state {*/ #define RISCV_MAX_VLENB (8192) +struct __sc_riscv_cfi_state { +unsigned long ss_ptr;/* shadow stack pointer */ +}; + +#define PTRACE_CFI_BRANCH_LANDING_PAD_EN_BIT 0 +#define PTRACE_CFI_BRANCH_LANDING_PAD_LOCK_BIT 1 +#define PTRACE_CFI_BRANCH_EXPECTED_LANDING_PAD_BIT 2 +#define PTRACE_CFI_SHADOW_STACK_EN_BIT 3 +#define PTRACE_CFI_SHADOW_STACK_LOCK_BIT 4 +#define PTRACE_CFI_SHADOW_STACK_PTR_BIT 5 + +#define PTRACE_CFI_BRANCH_LANDING_PAD_EN_STATE _BITUL(PTRACE_CFI_BRANCH_LANDING_PAD_EN_BIT) +#define PTRACE_CFI_BRANCH_LANDING_PAD_LOCK_STATE \ + _BITUL(PTRACE_CFI_BRANCH_LANDING_PAD_LOCK_BIT) +#define PTRACE_CFI_BRANCH_EXPECTED_LANDING_PAD_STATE \ + _BITUL(PTRACE_CFI_BRANCH_EXPECTED_LANDING_PAD_BIT) +#define PTRACE_CFI_SHADOW_STACK_EN_STATE _BITUL(PTRACE_CFI_SHADOW_STACK_EN_BIT) +#define PTRACE_CFI_SHADOW_STACK_LOCK_STATE _BITUL(PTRACE_CFI_SHADOW_STACK_LOCK_BIT) +#define PTRACE_CFI_SHADOW_STACK_PTR_STATE _BITUL(PTRACE_CFI_SHADOW_STACK_PTR_BIT) + +#define PTRACE_CFI_STATE_INVALID_MASK ~(PTRACE_CFI_BRANCH_LANDING_PAD_EN_STATE | \ + PTRACE_CFI_BRANCH_LANDING_PAD_LOCK_STATE | \ + PTRACE_CFI_BRANCH_EXPECTED_LANDING_PAD_STATE | \ + PTRACE_CFI_SHADOW_STACK_EN_STATE | \ + PTRACE_CFI_SHADOW_STACK_LOCK_STATE | \ + PTRACE_CFI_SHADOW_STACK_PTR_STATE) + +struct __cfi_status { +__u64 cfi_state; +}; + +struct user_cfi_state { +struct __cfi_status cfi_status; +__u64 shstk_ptr; +}; +#endif /* __ASSEMBLER__ */ #endif /* _ASM_RISCV_PTRACE_H */
linux-headers/asm-riscv/unistd_32.h +1 -0
@@ -318,6 +318,7 @@#define __NR_file_getattr 468#define __NR_file_setattr 469#define __NR_listns 470 +#define __NR_rseq_slice_yield 471#endif /* _ASM_UNISTD_32_H */
linux-headers/asm-riscv/unistd_64.h +1 -0
@@ -328,6 +328,7 @@#define __NR_file_getattr 468#define __NR_file_setattr 469#define __NR_listns 470 +#define __NR_rseq_slice_yield 471#endif /* _ASM_UNISTD_64_H */
linux-headers/asm-s390/unistd_32.h deleted +0 -446
446 lines not on this page · raw message
linux-headers/asm-s390/unistd_64.h +1 -0
@@ -390,6 +390,7 @@#define __NR_file_getattr 468#define __NR_file_setattr 469#define __NR_listns 470 +#define __NR_rseq_slice_yield 471#endif /* _ASM_UNISTD_64_H */
linux-headers/asm-x86/kvm.h +13 -8
@@ -197,13 +197,13 @@ struct kvm_msrs {__u32 nmsrs;/* number of msrs in entries */ __u32 pad; -struct kvm_msr_entry entries[]; + __DECLARE_FLEX_ARRAY(struct kvm_msr_entry, entries); };/* for KVM_GET_MSR_INDEX_LIST */ struct kvm_msr_list {__u32 nmsrs;/* number of msrs in entries */ -__u32 indices[]; + __DECLARE_FLEX_ARRAY(__u32, indices); };/* Maximum size of any access bitmap in bytes */
@@ -243,7 +243,7 @@ struct kvm_cpuid_entry {struct kvm_cpuid {__u32 nent;__u32 padding; -struct kvm_cpuid_entry entries[]; + __DECLARE_FLEX_ARRAY(struct kvm_cpuid_entry, entries); };struct kvm_cpuid_entry2 {
@@ -265,7 +265,7 @@ struct kvm_cpuid_entry2 {struct kvm_cpuid2 {__u32 nent;__u32 padding; -struct kvm_cpuid_entry2 entries[]; + __DECLARE_FLEX_ARRAY(struct kvm_cpuid_entry2, entries); };/* for KVM_GET_PIT and KVM_SET_PIT */
@@ -396,7 +396,7 @@ struct kvm_xsave {* the contents of CPUID leaf 0xD on the host. */ __u32 region[1024 ]; -__u32 extra[]; +__DECLARE_FLEX_ARRAY (__u32, extra); };#define KVM_MAX_XCRS 16
@@ -474,6 +474,7 @@ struct kvm_sync_regs {#define KVM_X86_QUIRK_SLOT_ZAP_ALL (1 << 7)#define KVM_X86_QUIRK_STUFF_FEATURE_MSRS (1 << 8)#define KVM_X86_QUIRK_IGNORE_GUEST_PAT (1 << 9) +#define KVM_X86_QUIRK_VMCS12_ALLOW_FREEZE_IN_SMM (1 << 10)#define KVM_STATE_NESTED_FORMAT_VMX 0#define KVM_STATE_NESTED_FORMAT_SVM 1
@@ -501,6 +502,7 @@ struct kvm_sync_regs {#define KVM_X86_GRP_SEV 1# define KVM_X86_SEV_VMSA_FEATURES 0# define KVM_X86_SNP_POLICY_BITS 1 +# define KVM_X86_SEV_SNP_REQ_CERTS 2struct kvm_vmx_nested_state_data {__u8 vmcs12[KVM_STATE_NESTED_VMX_VMCS_SIZE ];
@@ -562,7 +564,7 @@ struct kvm_pmu_event_filter {__u32 fixed_counter_bitmap;__u32 flags;__u32 pad[4 ]; -__u64 events[]; +__DECLARE_FLEX_ARRAY (__u64, events); };#define KVM_PMU_EVENT_ALLOW 0
@@ -741,6 +743,7 @@ enum sev_cmd_id {KVM_SEV_SNP_LAUNCH_START =100 ,KVM_SEV_SNP_LAUNCH_UPDATE ,KVM_SEV_SNP_LAUNCH_FINISH , +KVM_SEV_SNP_ENABLE_REQ_CERTS , KVM_SEV_NR_MAX, };
@@ -912,8 +915,10 @@ struct kvm_sev_snp_launch_finish {__u64 pad1[4 ]; }; -#define KVM_X2APIC_API_USE_32BIT_IDS (1ULL << 0) -#define KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK (1ULL << 1) +#define KVM_X2APIC_API_USE_32BIT_IDS _BITULL(0) +#define KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK _BITULL(1) +#define KVM_X2APIC_ENABLE_SUPPRESS_EOI_BROADCAST _BITULL(2) +#define KVM_X2APIC_DISABLE_SUPPRESS_EOI_BROADCAST _BITULL(3)struct kvm_hyperv_eventfd {__u32 conn_id;
linux-headers/asm-x86/unistd_32.h +1 -0
@@ -461,6 +461,7 @@#define __NR_file_getattr 468#define __NR_file_setattr 469#define __NR_listns 470 +#define __NR_rseq_slice_yield 471#endif /* _ASM_UNISTD_32_H */
linux-headers/asm-x86/unistd_64.h +1 -0
@@ -385,6 +385,7 @@#define __NR_file_getattr 468#define __NR_file_setattr 469#define __NR_listns 470 +#define __NR_rseq_slice_yield 471#endif /* _ASM_UNISTD_64_H */
linux-headers/asm-x86/unistd_x32.h +1 -0
@@ -338,6 +338,7 @@#define __NR_file_getattr (__X32_SYSCALL_BIT + 468)#define __NR_file_setattr (__X32_SYSCALL_BIT + 469)#define __NR_listns (__X32_SYSCALL_BIT + 470) +#define __NR_rseq_slice_yield (__X32_SYSCALL_BIT + 471)#define __NR_rt_sigaction (__X32_SYSCALL_BIT + 512)#define __NR_rt_sigreturn (__X32_SYSCALL_BIT + 513)#define __NR_ioctl (__X32_SYSCALL_BIT + 514)
linux-headers/linux/const.h +18 -0
@@ -50,4 +50,22 @@#define __KERNEL_DIV_ROUND_UP (n, d) (((n) + (d) - 1) / (d)) +/* +* Divide positive or negative dividend by positive or negative divisor +* and round to closest integer. Result is undefined for negative +* divisors if the dividend variable type is unsigned and for negative +* dividends if the divisor variable type is unsigned. +*/ +#define __KERNEL_DIV_ROUND_CLOSEST (x, divisor) \ +({ \ + __typeof__(x) __x = x; \ + __typeof__(divisor) __d = divisor; \ + \ + (((__typeof__(x))-1) > 0 || \ + ((__typeof__(divisor))-1) > 0 || \ + (((__x) > 0) == ((__d) > 0))) ? \ + (((__x) + ((__d) / 2)) / (__d)) : \ + (((__x) - ((__d) / 2)) / (__d)); \ +}) +#endif /* _LINUX_CONST_H */
linux-headers/linux/iommufd.h +48 -0
99 lines not on this page · raw message
linux-headers/linux/kvm.h +56 -6
161 lines not on this page · raw message
linux-headers/linux/mshv.h +3 -1
@@ -27,6 +27,8 @@ enum {MSHV_PT_BIT_X2APIC ,MSHV_PT_BIT_GPA_SUPER_PAGES ,MSHV_PT_BIT_CPU_AND_XSAVE_FEATURES , +MSHV_PT_BIT_NESTED_VIRTUALIZATION , +MSHV_PT_BIT_SMT_ENABLED_GUEST , MSHV_PT_BIT_COUNT, };
@@ -355,7 +357,7 @@ struct mshv_vtl_sint_post_msg {struct mshv_vtl_ram_disposition {__u64 start_pfn; -__u64 last_pfn; +__u64 last_pfn;/* last_pfn is excluded from the range [start_pfn, last_pfn) */ };struct mshv_vtl_set_poll_file {
linux-headers/linux/psp-sev.h +1 -1
@@ -277,7 +277,7 @@ struct sev_user_data_snp_wrapped_vlek_hashstick {* struct sev_issue_cmd - SEV ioctl parameters * * @cmd: SEV commands to execute -* @opaque: pointer to the command structure +* @data: pointer to the command structure * @error: SEV FW return code on failure */ struct sev_issue_cmd {
linux-headers/linux/stddef.h +4 -0
@@ -69,6 +69,10 @@#define __counted_by_be (m)#endif +#ifndef __counted_by_ptr +#define __counted_by_ptr (m) +#endif +#define __kernel_nonstring#endif /* _LINUX_STDDEF_H */
linux-headers/linux/vduse.h +80 -5
180 lines not on this page · raw message
linux-headers/linux/vfio.h +29 -1
@@ -141,7 +141,7 @@ struct vfio_info_cap_header {* * Retrieve information about the group. Fills in provided * struct vfio_group_info. Caller sets argsz. -* Return: 0 on succes, -errno on failure. +* Return: 0 on success, -errno on failure. * Availability: Always */ struct vfio_group_status {
@@ -964,6 +964,10 @@ struct vfio_device_bind_iommufd {* hwpt corresponding to the given pt_id. * * Return: 0 on success, -errno on failure. +* +* When a device is resetting, -EBUSY will be returned to reject any concurrent +* attachment to the resetting device itself or any sibling device in the IOMMU +* group having the resetting device. */ struct vfio_device_attach_iommufd_pt {__u32 argsz;
@@ -1262,6 +1266,19 @@ enum vfio_device_mig_state {* The initial_bytes field indicates the amount of initial precopy * data available from the device. This field should have a non-zero initial * value and decrease as migration data is read from the device. +* The presence of the VFIO_PRECOPY_INFO_REINIT output flag indicates +* that new initial data is present on the stream. +* The new initial data may result, for example, from device reconfiguration +* during migration that requires additional initialization data. +* In that case initial_bytes may report a non-zero value irrespective of +* any previously reported values, which progresses towards zero as precopy +* data is read from the data stream. dirty_bytes is also reset +* to zero and represents the state change of the device relative to the new +* initial_bytes. +* VFIO_PRECOPY_INFO_REINIT can be reported only after userspace opts in to +* VFIO_DEVICE_FEATURE_MIG_PRECOPY_INFOv2. Without this opt-in, the flags field +* of struct vfio_precopy_info is reserved for bug-compatibility reasons. +* * It is recommended to leave PRE_COPY for STOP_COPY only after this field * reaches zero. Leaving PRE_COPY earlier might make things slower. *
@@ -1297,6 +1314,7 @@ enum vfio_device_mig_state {struct vfio_precopy_info {__u32 argsz;__u32 flags; +#define VFIO_PRECOPY_INFO_REINIT (1 << 0)/* output - new initial data is present */ __aligned_u64 initial_bytes;__aligned_u64 dirty_bytes; };
@@ -1506,6 +1524,16 @@ struct vfio_device_feature_dma_buf { struct vfio_region_dma_range dma_ranges[] __counted_by(nr_ranges); }; +/* +* Enables the migration precopy_info_v2 behaviour. +* +* VFIO_DEVICE_FEATURE_MIG_PRECOPY_INFOv2. +* +* On SET, enables the v2 pre_copy_info behaviour, where the +* vfio_precopy_info.flags is a valid output field. +*/ +#define VFIO_DEVICE_FEATURE_MIG_PRECOPY_INFOv2 12 +/* -------- API for Type1 VFIO IOMMU -------- */ /**
Signature · 1 line
2.43.0
RFC PATCH 4/12 accel/kvm: Add CGS option to control in-place conversion support
In reply to Michael RothFor confidential guests, guest_memfd is currently used only for private guest memory, and normal guest memory comes from the configured memory backend just as it does for a non-confidential guest. It is now possible to use the same physical memory to back a particular GPA regardless of whether it is in a shared or private state. This avoids the need to rely on discarding memory between shared/private conversions (to avoid doubled memory usage), and is intended to be the primary mode of using guest_memfd for confidential guests moving forward, and future features like hugepage support will likely require it.
Add an option to enable this support. Since ConfidentialGuestSupport is already used to track some guest_memfd-related functionality (e.g. whether it is required for the configured machine), similarly introduce this option as a property of ConfidentialGuestSupport.
Also add the KVM-specific checks to enable this support, but leave the option disabled until other required changes are implemented for CGS variants that intend to make use of KVM's in-place conversion support.
--- accel/kvm/kvm-all.c | 21 +++++++++++++++++ backends/confidential-guest-support.c | 25 +++++++++++++++++++++ include/system/confidential-guest-support.h | 14 ++++++++++++ qapi/qom.json | 16 +++++++++++++ 4 files changed, 76 insertions(+)
accel/kvm/kvm-all.c +21 -0
@@ -52,6 +52,7 @@#include "kvm-cpus.h" #include "system/dirtylimit.h" #include "qemu/range.h" +#include "system/confidential-guest-support.h" #include "hw/core/boards.h" #include "system/stats.h"
@@ -2901,6 +2902,7 @@ static int kvm_reset_vmfd(MachineState *ms)static int kvm_init (AccelState *as,MachineState *ms) {MachineClass *mc =MACHINE_GET_CLASS (ms); +ConfidentialGuestSupport *cgs = ms->cgs;static const char upgrade_note[] ="Please upgrade to at least kernel 4.5.\n" ; const struct {
@@ -3076,6 +3078,25 @@ static int kvm_init(AccelState *as, MachineState *ms)kvm_vm_check_extension (s,KVM_CAP_USER_MEMORY2 ); kvm_pre_fault_memory_supported =kvm_vm_check_extension (s,KVM_CAP_PRE_FAULT_MEMORY ); +if (cgs && cgs->convert_in_place) { +uint64_t guest_memfd_supported_memory_attributes; + + guest_memfd_supported_memory_attributes = +kvm_vm_check_extension (s,KVM_CAP_GUEST_MEMFD_MEMORY_ATTRIBUTES ); + +if (!(guest_memfd_supported_memory_attributes &KVM_MEMORY_ATTRIBUTE_PRIVATE )) { + ret = -EINVAL ; +error_report ("In-place conversion is only supported if private " +"memory attributes can be set via guest_memfd. " +"Please ensure the 'vm_memory_attributes' KVM module " +"parameter is set to 0." ); + goto err; + } + +assert (kvm_guest_memfd_supported); + kvm_supported_memory_attributes = guest_memfd_supported_memory_attributes; + } +if (s->kernel_irqchip_split ==ON_OFF_AUTO_AUTO ) { s->kernel_irqchip_split = mc->default_kernel_irqchip_split ?ON_OFF_AUTO_ON :ON_OFF_AUTO_OFF ; }
backends/confidential-guest-support.c +25 -0
@@ -21,6 +21,24 @@ OBJECT_DEFINE_ABSTRACT_TYPE(ConfidentialGuestSupport, CONFIDENTIAL_GUEST_SUPPORT, OBJECT) +static bool +cgs_get_convert_in_place (Object *obj,Error **errp) +{ +return CONFIDENTIAL_GUEST_SUPPORT (obj)->convert_in_place; +} + +static void +cgs_set_convert_in_place (Object *obj,bool value,Error **errp) +{ +ConfidentialGuestSupport *cgs =CONFIDENTIAL_GUEST_SUPPORT (obj); + +if (!cgs->allow_convert_in_place && value) { +error_setg (errp,"In-place conversion support is not supported for this guest configuration." ); + } + + cgs->convert_in_place = value; +} +static bool check_support (ConfidentialGuestPlatformType platform,uint16_t platform_version,uint8_t highest_vtl,uint64_t shared_gpa_boundary)
@@ -70,6 +88,13 @@ static void confidential_guest_support_class_init(ObjectClass *oc,static void confidential_guest_support_init (Object *obj) { +ConfidentialGuestSupport *cgs =CONFIDENTIAL_GUEST_SUPPORT (obj); + +object_property_add_bool (obj,"convert-in-place" , cgs_get_convert_in_place, + cgs_set_convert_in_place); + + cgs->convert_in_place = false; + cgs->allow_convert_in_place = false; }static void confidential_guest_support_finalize (Object *obj)
include/system/confidential-guest-support.h +14 -0
@@ -20,6 +20,7 @@#include "qom/object.h" #include "exec/hwaddr.h" +#include "qapi/qapi-visit-qom.h" #define TYPE_CONFIDENTIAL_GUEST_SUPPORT "confidential-guest-support" OBJECT_DECLARE_TYPE(ConfidentialGuestSupport,
@@ -92,6 +93,19 @@ struct ConfidentialGuestSupport {* so 'ready' is not set, we'll abort. */ bool ready; + +/* +* True if the machine re-uses physical pages when converting +* between shared/private (as opposed to using different +* physical pages depending on the access type). +*/ +bool convert_in_place; + +/* +* CGS implementations will use this to indicate whether or not +* in-place conversion can be enabled by users. +*/ +bool allow_convert_in_place; }; typedef struct ConfidentialGuestSupportClass {
qapi/qom.json +16 -0
@@ -1014,6 +1014,21 @@ 'if': 'CONFIG_IGVM', 'data': { 'file': 'str' } } +## +# @ConfidentialGuestSupportProperties: +# +# Properties for ConfidentialGuestSupport base class. +# +# @convert-in-place: If true, the same physical pages are reused +# when memory is converted between shared and private states. +# If false (default), separate allocations are used depending +# on whether the page is private or shared. +# +# Since: 11.1 +## +{ 'struct': 'ConfidentialGuestSupportProperties', + 'data': { '*convert-in-place': 'bool' } } + ## # @SevCommonProperties: #
@@ -1038,6 +1053,7 @@ # Since: 9.1 ## { 'struct': 'SevCommonProperties', + 'base': 'ConfidentialGuestSupportProperties', 'data': { '*sev-device': 'str', '*cbitpos': 'uint32', 'reduced-phys-bits': 'uint32',
Signature · 1 line
2.43.0
Re: [PATCH RFC 04/12] accel/kvm: Add CGS option to control in-place conversion support
Quoted 21 lines · Michael Roth
For confidential guests,
guest_memfdis currently used only for private guest memory, and normal guest memory comes from the configured memory backend just as it does for a non-confidential guest. It is now possible to use the same physical memory to back a particular GPA regardless of whether it is in a shared or private state. This avoids the need to rely on discarding memory between shared/private conversions (to avoid doubled memory usage), and is intended to be the primary mode of usingguest_memfdfor confidential guests moving forward, and future features like hugepage support will likely require it.Add an option to enable this support. Since ConfidentialGuestSupport is already used to track some guest_memfd-related functionality (e.g. whether it is required for the configured machine), similarly introduce this option as a property of ConfidentialGuestSupport.
Also add the KVM-specific checks to enable this support, but leave the option disabled until other required changes are implemented for CGS variants that intend to make use of KVM's in-place conversion support.
Signed-off-by Michael Roth <[email protected]>
[...]
Quoted 27 lines
qapi/qom.json +15
-0@@ -1014,6 +1014,21 @@ 'if': 'CONFIG_IGVM', 'data': { 'file': 'str' } } +## +# @ConfidentialGuestSupportProperties: +# +# Properties for ConfidentialGuestSupport base class. +# +# @convert-in-place: If true, the same physical pages are reused +# when memory is converted between shared and private states. +# If false (default), separate allocations are used depending +# on whether the page is private or shared. +# +# Since: 11.1 +## +{ 'struct': 'ConfidentialGuestSupportProperties', + 'data': { '*convert-in-place': 'bool' } } + ## # @SevCommonProperties: #
qapi/qom.json line 1059
Quoted 7 lines
qapi/qom.json +1
-0@@ -1038,6 +1053,7 @@ # Since: 9.1 ## { 'struct': 'SevCommonProperties', + 'base': 'ConfidentialGuestSupportProperties', 'data': { '*sev-device': 'str', '*cbitpos': 'uint32', 'reduced-phys-bits': 'uint32',
Why use a base type instead of simply adding @convert-in-place to SevCommonProperties?
Re: [PATCH RFC 04/12] accel/kvm: Add CGS option to control in-place conversion support
Quoted 55 lines · Markus Armbruster
Quoted 21 lines · Michael Roth
For confidential guests,
guest_memfdis currently used only for private guest memory, and normal guest memory comes from the configured memory backend just as it does for a non-confidential guest. It is now possible to use the same physical memory to back a particular GPA regardless of whether it is in a shared or private state. This avoids the need to rely on discarding memory between shared/private conversions (to avoid doubled memory usage), and is intended to be the primary mode of usingguest_memfdfor confidential guests moving forward, and future features like hugepage support will likely require it.Add an option to enable this support. Since ConfidentialGuestSupport is already used to track some guest_memfd-related functionality (e.g. whether it is required for the configured machine), similarly introduce this option as a property of ConfidentialGuestSupport.
Also add the KVM-specific checks to enable this support, but leave the option disabled until other required changes are implemented for CGS variants that intend to make use of KVM's in-place conversion support.
Signed-off-by Michael Roth <[email protected]>[...]
Quoted 29 lines
qapi/qom.json +15
-0@@ -1014,6 +1014,21 @@ 'if': 'CONFIG_IGVM', 'data': { 'file': 'str' } } +## +# @ConfidentialGuestSupportProperties: +# +# Properties for ConfidentialGuestSupport base class. +# +# @convert-in-place: If true, the same physical pages are reused +# when memory is converted between shared and private states. +# If false (default), separate allocations are used depending +# on whether the page is private or shared. +# +# Since: 11.1 +## +{ 'struct': 'ConfidentialGuestSupportProperties', + 'data': { '*convert-in-place': 'bool' } } + ## # @SevCommonProperties: #@@ -1038,6 +1053,7 @@ # Since: 9.1 ##
Quoted 9 lines
qapi/qom.json line 1059
Quoted 5 lines
qapi/qom.json +1
-0{ 'struct': 'SevCommonProperties', + 'base': 'ConfidentialGuestSupportProperties', 'data': { '*sev-device': 'str', '*cbitpos': 'uint32', 'reduced-phys-bits': 'uint32',Why use a base type instead of simply adding @convert-in-place to SevCommonProperties?
My thinking was that TDX and other implementations would similarly enable this through their CGS implementation, so I went ahead and carved out a set of common properties that ConfidentialGuestSupport implementations could use the same ,convert-in-place=true option (or set it by default for newer implementations)
It is sort of tied to the 'allow_convert_in_place' flag that is part of the common ConfidentialGuestSupport object struct, so the property handling is sort of tied to the common ConfidentialGuestSupport base class as well rather than something implementation-specific.
Not sure if there are better ways to handle all that though.
Thanks,
Mike
Re: [PATCH RFC 04/12] accel/kvm: Add CGS option to control in-place conversion support
Quoted 66 lines · Michael Roth
Quoted 55 lines · Markus Armbruster
Quoted 21 lines · Michael Roth
For confidential guests,
guest_memfdis currently used only for private guest memory, and normal guest memory comes from the configured memory backend just as it does for a non-confidential guest. It is now possible to use the same physical memory to back a particular GPA regardless of whether it is in a shared or private state. This avoids the need to rely on discarding memory between shared/private conversions (to avoid doubled memory usage), and is intended to be the primary mode of usingguest_memfdfor confidential guests moving forward, and future features like hugepage support will likely require it.Add an option to enable this support. Since ConfidentialGuestSupport is already used to track some guest_memfd-related functionality (e.g. whether it is required for the configured machine), similarly introduce this option as a property of ConfidentialGuestSupport.
Also add the KVM-specific checks to enable this support, but leave the option disabled until other required changes are implemented for CGS variants that intend to make use of KVM's in-place conversion support.
Signed-off-by Michael Roth <[email protected]>[...]
Quoted 29 lines
qapi/qom.json +15
-0@@ -1014,6 +1014,21 @@ 'if': 'CONFIG_IGVM', 'data': { 'file': 'str' } } +## +# @ConfidentialGuestSupportProperties: +# +# Properties for ConfidentialGuestSupport base class. +# +# @convert-in-place: If true, the same physical pages are reused +# when memory is converted between shared and private states. +# If false (default), separate allocations are used depending +# on whether the page is private or shared. +# +# Since: 11.1 +## +{ 'struct': 'ConfidentialGuestSupportProperties', + 'data': { '*convert-in-place': 'bool' } } + ## # @SevCommonProperties: #@@ -1038,6 +1053,7 @@ # Since: 9.1 ##Quoted 9 lines
qapi/qom.json line 1059
Quoted 5 lines
qapi/qom.json +1
-0{ 'struct': 'SevCommonProperties', + 'base': 'ConfidentialGuestSupportProperties', 'data': { '*sev-device': 'str', '*cbitpos': 'uint32', 'reduced-phys-bits': 'uint32',Why use a base type instead of simply adding @convert-in-place to SevCommonProperties?
Quoted 5 lines
My thinking was that TDX and other implementations would similarly enable this through their CGS implementation, so I went ahead and carved out a set of common properties that ConfidentialGuestSupport implementations could use the same ,convert-in-place=true option (or set it by default for newer implementations)
How confident are we in future reuse by TDX and others?
If there are doubts, refactoring for reuse when reuse happens would be smarter. The refactoring would be a bit of churn, but not all that much.
If it's something like "pretty much inevitable", preparing the reuse now saves us that churn, and makes sense.
Judgement call, i.e. you decide.
Quoted 4 lines
It is sort of tied to the '
allow_convert_in_place' flag that is part of the common ConfidentialGuestSupport object struct, so the property handling is sort of tied to the common ConfidentialGuestSupport base class as well rather than something implementation-specific.
Valid point. Not sure how much weight to assign to it, though.
Quoted 1 line
Not sure if there are better ways to handle all that though.
Work your rationale into the commit message, please.
Re: [PATCH RFC 04/12] accel/kvm: Add CGS option to control in-place conversion support
Quoted 76 lines · Markus Armbruster
Quoted 66 lines · Michael Roth
Quoted 55 lines · Markus Armbruster
Quoted 21 lines · Michael Roth
For confidential guests,
guest_memfdis currently used only for private guest memory, and normal guest memory comes from the configured memory backend just as it does for a non-confidential guest. It is now possible to use the same physical memory to back a particular GPA regardless of whether it is in a shared or private state. This avoids the need to rely on discarding memory between shared/private conversions (to avoid doubled memory usage), and is intended to be the primary mode of usingguest_memfdfor confidential guests moving forward, and future features like hugepage support will likely require it.Add an option to enable this support. Since ConfidentialGuestSupport is already used to track some guest_memfd-related functionality (e.g. whether it is required for the configured machine), similarly introduce this option as a property of ConfidentialGuestSupport.
Also add the KVM-specific checks to enable this support, but leave the option disabled until other required changes are implemented for CGS variants that intend to make use of KVM's in-place conversion support.
Signed-off-by Michael Roth <[email protected]>[...]
Quoted 29 lines
qapi/qom.json +15
-0@@ -1014,6 +1014,21 @@ 'if': 'CONFIG_IGVM', 'data': { 'file': 'str' } } +## +# @ConfidentialGuestSupportProperties: +# +# Properties for ConfidentialGuestSupport base class. +# +# @convert-in-place: If true, the same physical pages are reused +# when memory is converted between shared and private states. +# If false (default), separate allocations are used depending +# on whether the page is private or shared. +# +# Since: 11.1 +## +{ 'struct': 'ConfidentialGuestSupportProperties', + 'data': { '*convert-in-place': 'bool' } } + ## # @SevCommonProperties: #@@ -1038,6 +1053,7 @@ # Since: 9.1 ##Quoted 9 lines
qapi/qom.json line 1059
Quoted 5 lines
qapi/qom.json +1
-0{ 'struct': 'SevCommonProperties', + 'base': 'ConfidentialGuestSupportProperties', 'data': { '*sev-device': 'str', '*cbitpos': 'uint32', 'reduced-phys-bits': 'uint32',Why use a base type instead of simply adding @convert-in-place to SevCommonProperties?
Quoted 5 lines
My thinking was that TDX and other implementations would similarly enable this through their CGS implementation, so I went ahead and carved out a set of common properties that ConfidentialGuestSupport implementations could use the same ,convert-in-place=true option (or set it by default for newer implementations)
How confident are we in future reuse by TDX and others?
Quoted 8 lines
If there are doubts, refactoring for reuse when reuse happens would be smarter. The refactoring would be a bit of churn, but not all that much.
If it's something like "pretty much inevitable", preparing the reuse now saves us that churn, and makes sense.
Judgement call, i.e. you decide.
Hoping to hear back from the TDX folks on whether there's anything missing to switch things on there too, at which point maybe it won't be premature to have a common type. But yah, until then I can plan to keep the option specific to SEV/SevCommonProperties. As you said, not a big deal to move it out to a common base after-the-fact.
Quoted 7 lines
Quoted 5 lines
It is sort of tied to the '
allow_convert_in_place' flag that is part of the common ConfidentialGuestSupport object struct, so the property handling is sort of tied to the common ConfidentialGuestSupport base class as well rather than something implementation-specific.Valid point. Not sure how much weight to assign to it, though.
Yah, I was purposely trying to make it easy for other platforms to switch it on, but if we do for the time being keep it limited to SNP, then there's probably other ways to go about it.
Quoted 5 lines
Quoted 2 lines
Not sure if there are better ways to handle all that though.
Work your rationale into the commit message, please.
Will do!
Thanks,
Mike
RFC PATCH 5/12 system/memory: Re-use memory-backend-guest-memfd inode for private memory
In reply to Michael RothWhen convert-in-place=true, the shared memory allocated/provided by the guest-memfd memory backend should also be used internally for private memory. Do this by dup()'ing the guest_memfd FD so separate cleanup paths for shared vs. private FDs can be managed in the same way they are currently for convert-in-place=false (where shared memory comes from some other backend like memory-backend-memfd).
Since it only currently makes sense to allow a memory-backend-guest-memfd FD to be used for private memory, introduce a new RAM_GUEST_MEMFD_SHARED flag that can be used to limit dup()'ing to specific backend types like memory-backend-guest-memfd.
--- backends/hostmem-guest-memfd.c | 1 + include/system/memory.h | 3 +++ system/physmem.c | 46 +++++++++++++++++++++++++++++++--- 3 files changed, 47 insertions(+), 3 deletions(-)
backends/hostmem-guest-memfd.c +1 -0
@@ -56,6 +56,7 @@ have_fd: ram_flags = backend->share ?RAM_SHARED :RAM_PRIVATE ; ram_flags |= backend->reserve ?0 :RAM_NORESERVE ; ram_flags |= backend->guest_memfd ?RAM_GUEST_MEMFD :0 ; + ram_flags |=RAM_GUEST_MEMFD_SHARED ;return memory_region_init_ram_from_fd (&backend->mr,OBJECT (backend), name, backend->size, ram_flags, fd,0 , errp); }
include/system/memory.h +3 -0
@@ -282,6 +282,9 @@ typedef struct IOMMUTLBEvent {*/ #define RAM_PRIVATE (1 << 13) +/* RAM can be shared that has kvm guest memfd backend */ +#define RAM_GUEST_MEMFD_SHARED (1 << 14) + static inline void iommu_notifier_init(IOMMUNotifier *n, IOMMUNotify fn, IOMMUNotifierFlag flags, hwaddr start, hwaddr end,
system/physmem.c +43 -3
81 lines not on this page · raw message
Signature · 1 line
2.43.0
RFC PATCH 6/12 system/memory: Default to guest_memfd for RAM for in-place conversion
In reply to Michael Rothmemory_region_init_ram_guest_memfd() is called in some cases (legacy BIOS regions / IGVM regions) to allocate a new RAM region with a guest_memfd FD under the covers to handle private memory since the GPA range can be converted between shared/private guest RAM.
When in-place conversion is enabled, the conversions happen with the guest_memfd inode itself, so the same inode must be used for both shared and private memory. Handle this accordingly when convert-in-place=true.
--- system/memory.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-)
system/memory.c +19 -3
@@ -35,6 +35,7 @@#include "hw/core/boards.h" #include "migration/vmstate.h" #include "system/address-spaces.h" +#include "system/confidential-guest-support.h" #include "memory-internal.h"
@@ -3674,10 +3675,25 @@ bool memory_region_init_ram_guest_memfd(MemoryRegion *mr, Object *owner, const char *name, uint64_t size, Error **errp) { - if (!memory_region_init_ram_flags_nomigrate(mr, owner, name, size, - RAM_GUEST_MEMFD, errp)) { - return false; + if (current_machine->cgs && current_machine->cgs->convert_in_place) { + int fd = kvm_create_guest_memfd_shared(size, errp); +if (fd <0 ) { +return false; + } + +if (!memory_region_init_ram_from_fd (mr, owner, name, size, +RAM_SHARED |RAM_GUEST_MEMFD | +RAM_GUEST_MEMFD_SHARED , + fd,0 , errp)) { +return false; + } + } else { +if (!memory_region_init_ram_flags_nomigrate (mr, owner, name, size, +RAM_GUEST_MEMFD , errp)) { +return false; + } } +memory_region_register_ram (mr, owner);return true; }
Signature · 1 line
2.43.0
RFC PATCH 7/12 accel/kvm: Move post-conversion updates to a separate helper
In reply to Michael RothCurrently memory attribute conversions are followed up by other bookkeeping tasks like discarding unused memory or issuing iommufd notifications. Move these tasks to a separate post-conversions helper to better compartmentalize and track these tasks, and in doing so lay the groundwork for a pre-conversion helper which will be needed in the future.
--- accel/kvm/kvm-all.c | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-)
accel/kvm/kvm-all.c +21 -9
@@ -3445,20 +3445,26 @@ static int kvm_convert_section(MemoryRegionSection *section, bool to_private) {hwaddr start = section->offset_within_address_space;hwaddr size =int128_get64 (section->size); -MemoryRegion *mr = section->mr; -ram_addr_t offset; -RAMBlock *rb; -void *addr; -int ret = -EINVAL ; +int ret;if (to_private) { ret =kvm_set_memory_attributes_private (start, size); }else { ret =kvm_set_memory_attributes_shared (start, size); } -if (ret) { -return ret; - } + +return ret; +} + +static int kvm_post_convert_section (MemoryRegionSection *section,bool to_private) +{ +hwaddr start = section->offset_within_address_space; +hwaddr size =int128_get64 (section->size); +MemoryRegion *mr = section->mr; +ram_addr_t offset; +RAMBlock *rb; +void *addr; +int ret; addr =memory_region_get_ram_ptr (mr) + section->offset_within_region; rb =qemu_ram_block_from_host (addr, false, &offset);
@@ -3485,7 +3491,7 @@ static int kvm_convert_section(MemoryRegionSection *section, bool to_private) ret =ram_block_discard_guest_memfd_range (rb, offset, size); } -return ret; +return 0 ; }int kvm_convert_memory (hwaddr start,hwaddr size,bool to_private)
@@ -3533,6 +3539,12 @@ int kvm_convert_memory(hwaddr start, hwaddr size, bool to_private) } ret =kvm_convert_section (§ion, to_private); +if (ret) { +memory_region_unref (section.mr); +break ; + } + + ret =kvm_post_convert_section (§ion, to_private);memory_region_unref (section.mr);if (ret) {
Signature · 1 line
2.43.0
RFC PATCH 8/12 accel/kvm: Re-order attribute notifications for in-place conversion
In reply to Michael Rothram-block-attribute update notifications are currently sent after conversions from/to private pages to trigger DMA maps/unmaps of shared GPA ranges (respectively). However, with in-place conversion additional requirements on the kernel side come into play which require this behavior to be adjusted.
For shared->private conversions: the attributes need to be set to private *after* the notification, since when using VFIO it may not be possible to update the attribute while it remains pinned due to the IOMMU mapping, so issue the notification first to ensure unmappings are done in advance.
For private->shared conversions: the attributes need to be set to shared *before* the notification, since it will possibly result in the page being mapped into an IOMMU and trigger guest_memfd's fault handler, which will expect the page to have its attributes set to shared or otherwise SIGBUS.
Implement this to enable passthrough support for CoCo guests with in-place conversion support enabled. For non-inplace conversion, pages mapped into the IOMMU are not the same physical pages as the one used for private accesses by the guest, so neither order risks DMA accesses to private memory and that path can be consolidated to use the same handling as well.
--- accel/kvm/kvm-all.c | 70 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 63 insertions(+), 7 deletions(-)
accel/kvm/kvm-all.c +63 -7
@@ -3456,6 +3456,47 @@ static int kvm_convert_section(MemoryRegionSection *section, bool to_private)return ret; } +static int kvm_pre_convert_section (MemoryRegionSection *section,bool to_private) +{ +hwaddr start = section->offset_within_address_space; +hwaddr size =int128_get64 (section->size); +MemoryRegion *mr = section->mr; +ram_addr_t offset; +RAMBlock *rb; +void *addr; +int ret; + + addr =memory_region_get_ram_ptr (mr) + section->offset_within_region; + rb =qemu_ram_block_from_host (addr, false, &offset); + +/* +* The attributes need to be set to private *after* the notification +* of a shared->private conversion, since when using VFIO it may not +* be possible to update the attribute while it remains pinned due +* to the IOMMU mapping, so issue the notification first to ensure +* unmappings are done in advance. +* +* There is an asymmetry here in that if the subsequent memory +* attribute update fails, this notification is out of sync with the +* state as tracked by guest_memfd, which isn't ideal, but memory +* attribute failures are not expected to be recoverable any way so +* there it would be a waste of time to roll back the notification and +* re-trigger things like mapping the page via iommufd. +*/ +if (to_private) { + ret =ram_block_attributes_state_change (rb->attributes, + offset, size, to_private); +if (ret) { +error_report ("Failed to notify the listener the state change of " +"(0x%" HWADDR_PRIx" + 0x%" HWADDR_PRIx") to %s, ret %d" , + start, size, to_private ?"private" :"shared" , ret); +return ret; + } + } + +return 0 ; +} +static int kvm_post_convert_section (MemoryRegionSection *section,bool to_private) {hwaddr start = section->offset_within_address_space;
@@ -3469,13 +3510,22 @@ static int kvm_post_convert_section(MemoryRegionSection *section, bool to_privat addr =memory_region_get_ram_ptr (mr) + section->offset_within_region; rb =qemu_ram_block_from_host (addr, false, &offset); - ret =ram_block_attributes_state_change (rb->attributes, - offset, size, to_private); -if (ret) { -error_report ("Failed to notify the listener the state change of " -"(0x%" HWADDR_PRIx" + 0x%" HWADDR_PRIx") to %s, ret %d" , - start, size, to_private ?"private" :"shared" , ret); -return ret; +/* +* The attributes need to have been set to shared *before* the notification +* of a private->shared conversion, since it will possibly result in the +* page being mapped into an IOMMU when using VFIO and trigger +* guest_memfd's fault handler, which will expect the page to have its +* attributes set to shared. +*/ +if (!to_private) { + ret =ram_block_attributes_state_change (rb->attributes, + offset, size, to_private); +if (ret) { +error_report ("Failed to notify the listener the state change of " +"(0x%" HWADDR_PRIx" + 0x%" HWADDR_PRIx") to %s, ret %d" , + start, size, to_private ?"private" :"shared" , ret); +return ret; + } }if (to_private) {
@@ -3538,6 +3588,12 @@ int kvm_convert_memory(hwaddr start, hwaddr size, bool to_private)continue ; } + ret =kvm_pre_convert_section (§ion, to_private); +if (ret) { +memory_region_unref (section.mr); +break ; + } + ret =kvm_convert_section (§ion, to_private);if (ret) {memory_region_unref (section.mr);
Signature · 1 line
2.43.0
RFC PATCH 9/12 accel/kvm: Support shared/private conversions via guest_memfd ioctls
In reply to Michael RothWhen using guest_memfd with support for shared memory / in-place conversion, it is necessary to use the guest_memfd ioctls to handle conversions instead of KVM ioctls. Implement support for this by looping through all the sections within a converison range. Implement everything in terms of the kvm_convert_memory() loop, which already deals with some special considerations regarding various holes / region types that might be encountered.
Also update kvm_set_memory_attributes_*() to use the same common path when convert-in-place=false. This potentially results in a small change in behavior due to the additional MMIO checks/skips now being applied in that case (generally qemu-triggered during setup) rather than only for kvm_convert_memory() (generally guest-triggered), but this is arguably safer, and it provides similar behavior between convert-in-place=false vs. convert-in-place=true, the latter of which *must* skip MMIO holes because the regions (and associated guest_memfds) themselves track shared/private state internally and passing the whole conversion range through to KVM is not an option in that case.
--- accel/kvm/kvm-all.c | 131 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 114 insertions(+), 17 deletions(-)
accel/kvm/kvm-all.c +114 -17
@@ -1626,14 +1626,78 @@ static int kvm_set_memory_attributes(hwaddr start, uint64_t size, uint64_t attr)return r; } -int kvm_set_memory_attributes_private (hwaddr start,uint64_t size) +static int kvm_gmem_ioctl (int guest_memfd,unsigned long type, ...) { -return kvm_set_memory_attributes (start, size,KVM_MEMORY_ATTRIBUTE_PRIVATE ); +int ret; +void *arg; +va_list ap; + +va_start (ap, type); + arg = va_arg(ap, void *); +va_end (ap); + + ret =ioctl (guest_memfd, type, arg); +if (ret ==-1 ) { + ret = -errno; + } +return ret; } -int kvm_set_memory_attributes_shared (hwaddr start,uint64_t size) +static int guest_memfd_set_memory_attributes_fd (int guest_memfd,hwaddr offset, +uint64_t size,uint64_t attr) { -return kvm_set_memory_attributes (start, size,0 ); +struct kvm_memory_attributes2 attrs; +int r; + +assert ((attr & kvm_supported_memory_attributes) == attr); + attrs.attributes = attr; + attrs.offset = offset; + attrs.size = size; + attrs.flags =0 ; + +/* +* guest_memfd may need to delay conversion requests due to +* the memory being in-use by the kernel. In most cases these +* will be transient uses. In some cases, userspace itself may +* be the cause of the memory being considered in-use, though +* QEMU currently takes steps to avoid this (e.g. via +* RamBlockAttributes). On that basis, this code loops +* indefinitely with the assumption that only transient cases +* will block, and that those will be for relatively short +* periods vs. the overall conversion path. +* If those assumptions at some point prove false, most likely +* this will manifest as guest-side lockups on their conversion +* path, which seems like the appropriate way to surface this +* situation to the guest owner rather than some hard timeout. +*/ +do { + r =kvm_gmem_ioctl (guest_memfd,KVM_SET_MEMORY_ATTRIBUTES2 , &attrs); + }while (r == -EAGAIN ); + +if (r) { +error_report ("failed to set memory (0x%" HWADDR_PRIx"+0x%" PRIx64") " +"with attr 0x%" PRIx64" error '%s'" , + offset, size, attr,strerror (-r)); + } +return r; +} + +static int guest_memfd_set_memory_section_attributes (MemoryRegionSection *section,uint64_t attr) +{ +hwaddr convert_offset, convert_size; +MemoryRegion *mr = section->mr; +RAMBlock *rb; + +assert (mr); + rb = mr->ram_block; +assert (rb->guest_memfd); + convert_offset = section->offset_within_region; + convert_size =int128_get64 (section->size); + +return guest_memfd_set_memory_attributes_fd (rb->guest_memfd, + convert_offset, + convert_size, + attr); }/* Called with KVMMemoryListener.slots_lock held */
@@ -3447,10 +3511,18 @@ static int kvm_convert_section(MemoryRegionSection *section, bool to_private)hwaddr size =int128_get64 (section->size);int ret; -if (to_private) { - ret =kvm_set_memory_attributes_private (start, size); +if (current_machine->cgs && current_machine->cgs->convert_in_place) { + ret =guest_memfd_set_memory_section_attributes (section, + to_private ?KVM_MEMORY_ATTRIBUTE_PRIVATE + :0 ); }else { - ret =kvm_set_memory_attributes_shared (start, size); +/* +* Without in-place conversion, attribute-tracking is handled by KVM +* across all guest memory rather than on a per-section/slot basis. +*/ + ret =kvm_set_memory_attributes (start, size, + to_private ?KVM_MEMORY_ATTRIBUTE_PRIVATE + :0 ); }return ret;
@@ -3544,7 +3616,8 @@ static int kvm_post_convert_section(MemoryRegionSection *section, bool to_privatreturn 0 ; } -int kvm_convert_memory (hwaddr start,hwaddr size,bool to_private) +static int kvm_convert_memory_full (hwaddr start,hwaddr size,bool to_private, +bool pre_hooks,bool post_hooks) {int ret = -EINVAL ;
@@ -3588,10 +3661,12 @@ int kvm_convert_memory(hwaddr start, hwaddr size, bool to_private)continue ; } - ret =kvm_pre_convert_section (§ion, to_private); -if (ret) { -memory_region_unref (section.mr); -break ; +if (pre_hooks) { + ret =kvm_pre_convert_section (§ion, to_private); +if (ret) { +memory_region_unref (section.mr); +break ; + } } ret =kvm_convert_section (§ion, to_private);
@@ -3600,13 +3675,15 @@ int kvm_convert_memory(hwaddr start, hwaddr size, bool to_private)break ; } - ret =kvm_post_convert_section (§ion, to_private); -memory_region_unref (section.mr); - -if (ret) { -break ; +if (post_hooks) { + ret =kvm_post_convert_section (§ion, to_private); +if (ret) { +memory_region_unref (section.mr); +break ; + } } +memory_region_unref (section.mr); size -= section_end - start; start = section_end; }
@@ -3614,6 +3691,26 @@ int kvm_convert_memory(hwaddr start, hwaddr size, bool to_private)return ret; } +int kvm_convert_memory (hwaddr start,hwaddr size,bool to_private) +{ +return kvm_convert_memory_full (start, size, to_private, true, true); +} + +static int kvm_convert_memory_attributes (hwaddr start,hwaddr size,bool to_private) +{ +return kvm_convert_memory_full (start, size, to_private, false, false); +} + +int kvm_set_memory_attributes_private (hwaddr start,uint64_t size) +{ +return kvm_convert_memory_attributes (start, size,KVM_MEMORY_ATTRIBUTE_PRIVATE ); +} + +int kvm_set_memory_attributes_shared (hwaddr start,uint64_t size) +{ +return kvm_convert_memory_attributes (start, size,0 ); +} +int kvm_cpu_exec (CPUState *cpu) {struct kvm_run *run = cpu->kvm_run;
Signature · 1 line
2.43.0
Re: [PATCH RFC 09/12] accel/kvm: Support shared/private conversions via guest_memfd ioctls
Quoted 48 lines
When using
guest_memfdwith support for shared memory / in-place conversion, it is necessary to use theguest_memfdioctls to handle conversions instead of KVM ioctls. Implement support for this by looping through all the sections within a converison range. Implement everything in terms of thekvm_convert_memory()loop, which already deals with some special considerations regarding various holes / region types that might be encountered.Also update kvm_set_memory_attributes_*() to use the same common path when convert-in-place=false. This potentially results in a small change in behavior due to the additional MMIO checks/skips now being applied in that case (generally qemu-triggered during setup) rather than only for
kvm_convert_memory()(generally guest-triggered), but this is arguably safer, and it provides similar behavior between convert-in-place=false vs. convert-in-place=true, the latter of which *must* skip MMIO holes because the regions (and associatedguest_memfds) themselves track shared/private state internally and passing the whole conversion range through to KVM is not an option in that case.Signed-off-by Michael Roth <[email protected]>--- accel/kvm/kvm-all.c | 131 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 114 insertions(+), 17 deletions(-)accel/kvm/kvm-all.c +12
-2@@ -1626,14 +1626,78 @@ static int kvm_set_memory_attributes(hwaddr start, uint64_t size, uint64_t attr)return r; } -int kvm_set_memory_attributes_private (hwaddr start,uint64_t size) +static int kvm_gmem_ioctl (int guest_memfd,unsigned long type, ...) { -return kvm_set_memory_attributes (start, size,KVM_MEMORY_ATTRIBUTE_PRIVATE ); +int ret; +void *arg; +va_list ap; + +va_start (ap, type); + arg = va_arg(ap, void *); +va_end (ap); + + ret =ioctl (guest_memfd, type, arg); +if (ret ==-1 ) { + ret = -errno;
accel/kvm/kvm-all.c static int kvm_set_memory_attributes(hwaddr start, uint64_t size, uint64_t attr) · line 1649
Quoted 10 lines
accel/kvm/kvm-all.c +5
-2+ } +return ret; } -int kvm_set_memory_attributes_shared (hwaddr start,uint64_t size) +static int guest_memfd_set_memory_attributes_fd (int guest_memfd,hwaddr offset, +uint64_t size,uint64_t attr) { -return kvm_set_memory_attributes (start, size,0 ); +struct kvm_memory_attributes2 attrs;
- struct kvm_memory_attributes2 attrs;
+ struct kvm_memory_attributes2 attrs = {0};
Zero initializing 'attrs' fixed a '-EINVAL' error, caused because of kernel 'attrs.reserved' check failed in 'kvm_gmem_set_attributes()'.
Thanks,
Pankaj
Quoted 151 lines
accel/kvm/kvm-all.c +97
-13+int r; + +assert ((attr & kvm_supported_memory_attributes) == attr); + attrs.attributes = attr; + attrs.offset = offset; + attrs.size = size; + attrs.flags =0 ; + +/* +* guest_memfd may need to delay conversion requests due to +* the memory being in-use by the kernel. In most cases these +* will be transient uses. In some cases, userspace itself may +* be the cause of the memory being considered in-use, though +* QEMU currently takes steps to avoid this (e.g. via +* RamBlockAttributes). On that basis, this code loops +* indefinitely with the assumption that only transient cases +* will block, and that those will be for relatively short +* periods vs. the overall conversion path. +* If those assumptions at some point prove false, most likely +* this will manifest as guest-side lockups on their conversion +* path, which seems like the appropriate way to surface this +* situation to the guest owner rather than some hard timeout. +*/ +do { + r =kvm_gmem_ioctl (guest_memfd,KVM_SET_MEMORY_ATTRIBUTES2 , &attrs); + }while (r == -EAGAIN ); + +if (r) { +error_report ("failed to set memory (0x%" HWADDR_PRIx"+0x%" PRIx64") " +"with attr 0x%" PRIx64" error '%s'" , + offset, size, attr,strerror (-r)); + } +return r; +} + +static int guest_memfd_set_memory_section_attributes (MemoryRegionSection *section,uint64_t attr) +{ +hwaddr convert_offset, convert_size; +MemoryRegion *mr = section->mr; +RAMBlock *rb; + +assert (mr); + rb = mr->ram_block; +assert (rb->guest_memfd); + convert_offset = section->offset_within_region; + convert_size =int128_get64 (section->size); + +return guest_memfd_set_memory_attributes_fd (rb->guest_memfd, + convert_offset, + convert_size, + attr); }/* Called with KVMMemoryListener.slots_lock held */ @@ -3447,10 +3511,18 @@ static int kvm_convert_section(MemoryRegionSection *section, bool to_private)hwaddr size =int128_get64 (section->size);int ret; -if (to_private) { - ret =kvm_set_memory_attributes_private (start, size); +if (current_machine->cgs && current_machine->cgs->convert_in_place) { + ret =guest_memfd_set_memory_section_attributes (section, + to_private ?KVM_MEMORY_ATTRIBUTE_PRIVATE + :0 ); }else { - ret =kvm_set_memory_attributes_shared (start, size); +/* +* Without in-place conversion, attribute-tracking is handled by KVM +* across all guest memory rather than on a per-section/slot basis. +*/ + ret =kvm_set_memory_attributes (start, size, + to_private ?KVM_MEMORY_ATTRIBUTE_PRIVATE + :0 ); }return ret;@@ -3544,7 +3616,8 @@ static int kvm_post_convert_section(MemoryRegionSection *section, bool to_privatreturn 0 ; } -int kvm_convert_memory (hwaddr start,hwaddr size,bool to_private) +static int kvm_convert_memory_full (hwaddr start,hwaddr size,bool to_private, +bool pre_hooks,bool post_hooks) {int ret = -EINVAL ;@@ -3588,10 +3661,12 @@ int kvm_convert_memory(hwaddr start, hwaddr size, bool to_private)continue ; } - ret =kvm_pre_convert_section (§ion, to_private); -if (ret) { -memory_region_unref (section.mr); -break ; +if (pre_hooks) { + ret =kvm_pre_convert_section (§ion, to_private); +if (ret) { +memory_region_unref (section.mr); +break ; + } } ret =kvm_convert_section (§ion, to_private);@@ -3600,13 +3675,15 @@ int kvm_convert_memory(hwaddr start, hwaddr size, bool to_private)break ; } - ret =kvm_post_convert_section (§ion, to_private); -memory_region_unref (section.mr); - -if (ret) { -break ; +if (post_hooks) { + ret =kvm_post_convert_section (§ion, to_private); +if (ret) { +memory_region_unref (section.mr); +break ; + } } +memory_region_unref (section.mr); size -= section_end - start; start = section_end; }@@ -3614,6 +3691,26 @@ int kvm_convert_memory(hwaddr start, hwaddr size, bool to_private)return ret; } +int kvm_convert_memory (hwaddr start,hwaddr size,bool to_private) +{ +return kvm_convert_memory_full (start, size, to_private, true, true); +} + +static int kvm_convert_memory_attributes (hwaddr start,hwaddr size,bool to_private) +{ +return kvm_convert_memory_full (start, size, to_private, false, false); +} + +int kvm_set_memory_attributes_private (hwaddr start,uint64_t size) +{ +return kvm_convert_memory_attributes (start, size,KVM_MEMORY_ATTRIBUTE_PRIVATE ); +} + +int kvm_set_memory_attributes_shared (hwaddr start,uint64_t size) +{ +return kvm_convert_memory_attributes (start, size,0 ); +} +int kvm_cpu_exec (CPUState *cpu) {struct kvm_run *run = cpu->kvm_run;
Re: [PATCH RFC 09/12] accel/kvm: Support shared/private conversions via guest_memfd ioctls
Quoted 60 lines · Pankaj
Quoted 46 lines
When using
guest_memfdwith support for shared memory / in-place conversion, it is necessary to use theguest_memfdioctls to handle conversions instead of KVM ioctls. Implement support for this by looping through all the sections within a converison range. Implement everything in terms of thekvm_convert_memory()loop, which already deals with some special considerations regarding various holes / region types that might be encountered.Also update kvm_set_memory_attributes_*() to use the same common path when convert-in-place=false. This potentially results in a small change in behavior due to the additional MMIO checks/skips now being applied in that case (generally qemu-triggered during setup) rather than only for
kvm_convert_memory()(generally guest-triggered), but this is arguably safer, and it provides similar behavior between convert-in-place=false vs. convert-in-place=true, the latter of which *must* skip MMIO holes because the regions (and associatedguest_memfds) themselves track shared/private state internally and passing the whole conversion range through to KVM is not an option in that case.Signed-off-by Michael Roth <[email protected]>--- accel/kvm/kvm-all.c | 131 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 114 insertions(+), 17 deletions(-)accel/kvm/kvm-all.c +11
-2@@ -1626,14 +1626,78 @@ static int kvm_set_memory_attributes(hwaddr start, uint64_t size, uint64_t attr)return r; } -int kvm_set_memory_attributes_private (hwaddr start,uint64_t size) +static int kvm_gmem_ioctl (int guest_memfd,unsigned long type, ...) { -return kvm_set_memory_attributes (start, size,KVM_MEMORY_ATTRIBUTE_PRIVATE ); +int ret; +void *arg; +va_list ap; + +va_start (ap, type); + arg = va_arg(ap, void *); +va_end (ap); + + ret =ioctl (guest_memfd, type, arg); +if (ret ==-1 ) {accel/kvm/kvm-all.c static int kvm_set_memory_attributes(hwaddr start, uint64_t size, uint64_t attr) · line 1647
Quoted 10 lines
accel/kvm/kvm-all.c +6
-2+ ret = -errno; + } +return ret; } -int kvm_set_memory_attributes_shared (hwaddr start,uint64_t size) +static int guest_memfd_set_memory_attributes_fd (int guest_memfd,hwaddr offset, +uint64_t size,uint64_t attr) { -return kvm_set_memory_attributes (start, size,0 ); +struct kvm_memory_attributes2 attrs;- struct
kvm_memory_attributes2attrs;
+ structkvm_memory_attributes2attrs = {0};
Quoted 2 lines
Zero initializing 'attrs' fixed a '
-EINVAL' error, caused because of kernel 'attrs.reserved' check failed in 'kvm_gmem_set_attributes()'.
Indeed, thanks for the catch!
-Mike
Quoted 146 lines
Thanks,
Pankaj
Quoted 141 lines
accel/kvm/kvm-all.c +97
-13+int r; + +assert ((attr & kvm_supported_memory_attributes) == attr); + attrs.attributes = attr; + attrs.offset = offset; + attrs.size = size; + attrs.flags =0 ; + +/* +* guest_memfd may need to delay conversion requests due to +* the memory being in-use by the kernel. In most cases these +* will be transient uses. In some cases, userspace itself may +* be the cause of the memory being considered in-use, though +* QEMU currently takes steps to avoid this (e.g. via +* RamBlockAttributes). On that basis, this code loops +* indefinitely with the assumption that only transient cases +* will block, and that those will be for relatively short +* periods vs. the overall conversion path. +* If those assumptions at some point prove false, most likely +* this will manifest as guest-side lockups on their conversion +* path, which seems like the appropriate way to surface this +* situation to the guest owner rather than some hard timeout. +*/ +do { + r =kvm_gmem_ioctl (guest_memfd,KVM_SET_MEMORY_ATTRIBUTES2 , &attrs); + }while (r == -EAGAIN ); + +if (r) { +error_report ("failed to set memory (0x%" HWADDR_PRIx"+0x%" PRIx64") " +"with attr 0x%" PRIx64" error '%s'" , + offset, size, attr,strerror (-r)); + } +return r; +} + +static int guest_memfd_set_memory_section_attributes (MemoryRegionSection *section,uint64_t attr) +{ +hwaddr convert_offset, convert_size; +MemoryRegion *mr = section->mr; +RAMBlock *rb; + +assert (mr); + rb = mr->ram_block; +assert (rb->guest_memfd); + convert_offset = section->offset_within_region; + convert_size =int128_get64 (section->size); + +return guest_memfd_set_memory_attributes_fd (rb->guest_memfd, + convert_offset, + convert_size, + attr); }/* Called with KVMMemoryListener.slots_lock held */ @@ -3447,10 +3511,18 @@ static int kvm_convert_section(MemoryRegionSection *section, bool to_private)hwaddr size =int128_get64 (section->size);int ret; -if (to_private) { - ret =kvm_set_memory_attributes_private (start, size); +if (current_machine->cgs && current_machine->cgs->convert_in_place) { + ret =guest_memfd_set_memory_section_attributes (section, + to_private ?KVM_MEMORY_ATTRIBUTE_PRIVATE + :0 ); }else { - ret =kvm_set_memory_attributes_shared (start, size); +/* +* Without in-place conversion, attribute-tracking is handled by KVM +* across all guest memory rather than on a per-section/slot basis. +*/ + ret =kvm_set_memory_attributes (start, size, + to_private ?KVM_MEMORY_ATTRIBUTE_PRIVATE + :0 ); }return ret;@@ -3544,7 +3616,8 @@ static int kvm_post_convert_section(MemoryRegionSection *section, bool to_privatreturn 0 ; } -int kvm_convert_memory (hwaddr start,hwaddr size,bool to_private) +static int kvm_convert_memory_full (hwaddr start,hwaddr size,bool to_private, +bool pre_hooks,bool post_hooks) {int ret = -EINVAL ;@@ -3588,10 +3661,12 @@ int kvm_convert_memory(hwaddr start, hwaddr size, bool to_private)continue ; } - ret =kvm_pre_convert_section (§ion, to_private); -if (ret) { -memory_region_unref (section.mr); -break ; +if (pre_hooks) { + ret =kvm_pre_convert_section (§ion, to_private); +if (ret) { +memory_region_unref (section.mr); +break ; + } } ret =kvm_convert_section (§ion, to_private);@@ -3600,13 +3675,15 @@ int kvm_convert_memory(hwaddr start, hwaddr size, bool to_private)break ; } - ret =kvm_post_convert_section (§ion, to_private); -memory_region_unref (section.mr); - -if (ret) { -break ; +if (post_hooks) { + ret =kvm_post_convert_section (§ion, to_private); +if (ret) { +memory_region_unref (section.mr); +break ; + } } +memory_region_unref (section.mr); size -= section_end - start; start = section_end; }@@ -3614,6 +3691,26 @@ int kvm_convert_memory(hwaddr start, hwaddr size, bool to_private)return ret; } +int kvm_convert_memory (hwaddr start,hwaddr size,bool to_private) +{ +return kvm_convert_memory_full (start, size, to_private, true, true); +} + +static int kvm_convert_memory_attributes (hwaddr start,hwaddr size,bool to_private) +{ +return kvm_convert_memory_full (start, size, to_private, false, false); +} + +int kvm_set_memory_attributes_private (hwaddr start,uint64_t size) +{ +return kvm_convert_memory_attributes (start, size,KVM_MEMORY_ATTRIBUTE_PRIVATE ); +} + +int kvm_set_memory_attributes_shared (hwaddr start,uint64_t size) +{ +return kvm_convert_memory_attributes (start, size,0 ); +} +int kvm_cpu_exec (CPUState *cpu) {struct kvm_run *run = cpu->kvm_run;
RFC PATCH 10/12 accel/kvm: Don't default to private attributes for in-place conversion
In reply to Michael RothWithout in-place conversion, QEMU can still access shared memory to load initial state into guest memory prior to launch even if the GPA's memory attributes default to private, since userspace is accessing a completely separate pool of memory. With in-place conversion, all these accesses would need to first be converted to shared, then back to private, since the memory all comes from guest_memfd and only shared memory can be accessed by userspace.
To avoid sprinkling these differences in behavior throughout QEMU when in-place conversion is enabled, just default to shared. This does not compromise guest security, since Confidential VMs will necessarily enforce this via trusted entities, and simply generate implicit page state changes if their default expectations don't match KVM's. However, in most cases a guest will explicitly convert memory to a particular state before actually using it, so even these implicit conversion requests should be rare.
--- accel/kvm/kvm-all.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-)
accel/kvm/kvm-all.c +20 -1
@@ -1808,7 +1808,26 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml,abort (); } -if (memory_region_has_guest_memfd (mr)) { +/* +* Without in-place conversion, QEMU can still access shared memory +* to load initial state into guest memory prior to launch even if +* the GPA's memory attributes default to private, since userspace +* is accessing a completely separate pool of memory. With in-place +* conversion, all these accesses would need to first be converted +* to shared, then back to private, since the memory all comes from +* guest_memfd and only shared memory can be accessed by userspace. +* +* To avoid sprinkling these differences in behavior throughout QEMU +* when in-place conversion is enabled, just default to shared. This +* does not compromise guest security, since Confidential VMs will +* necessarily enforce this via trusted entities, and simply generate +* implicit page state changes if their default expectations don't +* match KVM's. However, in most cases a guest will explicitly +* convert memory to a particular state before actually using it, so +* even these implicit conversion requests should be rare. +*/ +if (memory_region_has_guest_memfd (mr) && + !(current_machine->cgs && current_machine->cgs->convert_in_place)) { err =kvm_set_memory_attributes_private (start_addr, slot_size);if (err) { error_report("%s: failed to set memory attribute private: %s",
Signature · 1 line
2.43.0
Re: [PATCH RFC 10/12] accel/kvm: Don't default to private attributes for in-place conversion
Quoted 8 lines · Michael Roth
Without in-place conversion, QEMU can still access shared memory to load initial state into guest memory prior to launch even if the GPA's memory attributes default to private, since userspace is accessing a completely separate pool of memory. With in-place conversion, all these accesses would need to first be converted to shared, then back to private, since the memory all comes from
guest_memfdand only shared memory can be accessed by userspace.
Quoted 8 lines
To avoid sprinkling these differences in behavior throughout QEMU when in-place conversion is enabled, just default to shared. This does not compromise guest security, since Confidential VMs will necessarily enforce this via trusted entities, and simply generate implicit page state changes if their default expectations don't match KVM's. However, in most cases a guest will explicitly convert memory to a particular state before actually using it, so even these implicit conversion requests should be rare.
I assume you want to prevent peppering code with convert-in-place conditional paths where, if we default to private, certain mem copies to guest memory would fault.
Flip side: we default to shared, we leave the current code paths unchanged (that assume double backing) but we end up (eg on CCA) populating guest memory twice, once with a copy to shared guest memfd memory (that is basically useless because populating in CCA is destructive) and then populating the guest memory region using CCA KVM interface (KVM_ARM_RMI_POPULATE).
I am just thinking aloud and sharing what I have noticed so far.
Lorenzo
Quoted 40 lines
Signed-off-by Michael Roth <[email protected]>--- accel/kvm/kvm-all.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-)accel/kvm/kvm-all.c +20
-1@@ -1808,7 +1808,26 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml,abort (); } -if (memory_region_has_guest_memfd (mr)) { +/* +* Without in-place conversion, QEMU can still access shared memory +* to load initial state into guest memory prior to launch even if +* the GPA's memory attributes default to private, since userspace +* is accessing a completely separate pool of memory. With in-place +* conversion, all these accesses would need to first be converted +* to shared, then back to private, since the memory all comes from +* guest_memfd and only shared memory can be accessed by userspace. +* +* To avoid sprinkling these differences in behavior throughout QEMU +* when in-place conversion is enabled, just default to shared. This +* does not compromise guest security, since Confidential VMs will +* necessarily enforce this via trusted entities, and simply generate +* implicit page state changes if their default expectations don't +* match KVM's. However, in most cases a guest will explicitly +* convert memory to a particular state before actually using it, so +* even these implicit conversion requests should be rare. +*/ +if (memory_region_has_guest_memfd (mr) && + !(current_machine->cgs && current_machine->cgs->convert_in_place)) { err =kvm_set_memory_attributes_private (start_addr, slot_size);if (err) { error_report("%s: failed to set memory attribute private: %s",Signature · 1 line
2.43.0
RFC PATCH 11/12 i386/sev: Update SNP_LAUNCH_UPDATE for in-place conversion
In reply to Michael RothFor in-place conversion, the source pointer is expected to be NULL since the data has already been written directly to guest memory and doesn't need to be copied in prior to encrypting it in-place for initial guest memory payload.
--- target/i386/sev.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
target/i386/sev.c +10 -1
@@ -1186,6 +1186,8 @@ sev_snp_launch_update(SevSnpGuestState *sev_snp_guest,int ret, fw_error;SnpCpuidInfo snp_cpuid_info;struct kvm_sev_snp_launch_update update = {0 }; +ConfidentialGuestSupport *cgs = +CONFIDENTIAL_GUEST_SUPPORT (OBJECT (sev_snp_guest));if (!data->hva || !data->len) { error_report("SNP_LAUNCH_UPDATE called with invalid address"
@@ -1199,7 +1201,14 @@ sev_snp_launch_update(SevSnpGuestState *sev_snp_guest,memcpy (&snp_cpuid_info, data->hva,sizeof (snp_cpuid_info)); } - update.uaddr = (__u64 )(unsigned long )data->hva; +/* +* For in-place conversion, the source pointer is expected to be NULL +* since the data has already been written directly to guest memory +* and only needs to be encrypted in-place for secure access. +*/ +if (!cgs->convert_in_place) { + update.uaddr = (__u64 )(unsigned long )data->hva; + } update.gfn_start = data->gpa >>TARGET_PAGE_BITS ; update.len = data->len; update.type = data->type;
Signature · 1 line
2.43.0
RFC PATCH 12/12 i386/sev: Allow in-place conversion for SEV-SNP guests
In reply to Michael RothAll the necessary changes are now in place for an SNP guest to be able to leverage in-place conversion support. Allow it to be switched on by users. KVM-specific checks will still gate whether or not the option is ultimately allowed, this just allows the option to be set via command-line.
--- target/i386/sev.c | 1 + 1 file changed, 1 insertion(+)
target/i386/sev.c +1 -0
@@ -3198,6 +3198,7 @@ sev_snp_guest_instance_init(Object *obj)SevSnpGuestState *sev_snp_guest =SEV_SNP_GUEST (obj); cgs->require_guest_memfd = true; + cgs->allow_convert_in_place = true;/* default init/start/finish params for kvm */ sev_snp_guest->kvm_start_conf.policy =DEFAULT_SEV_SNP_POLICY ;
Signature · 1 line
2.43.0
Quoted 61 lines · Michael Roth
This patchset is also available at:
https://github.com/amdese/qemu/commits/snp-inplace-rfc1
which is in turn based on the following series:
[PATCH 0/4] "
guest_memfd: Fix handling for conversions of MMIO ranges"
https://lists.gnu.org/archive/html/qemu-devel/2026-05/msg07547.htmlOVERVIEW
--------This series adds
guest_memfdsupport 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()-ableguest_memfdinstances[1], which allow it to be used for shared memory, and the following patchset[2], which adds additionalguest_memfdinterfaces to allow it to be used to perform in-place conversion:"[PATCH v7 00/42]
guest_memfd: In-place conversion support"
https://lore.kernel.org/kvm/[email protected]/That series also introduces a new '
vm_memory_attributes' 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-allocatedguest_memfdfile descriptor for private pages.KVM_SET_MEMORY_ATTRIBUTESflips 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_memfdwill 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.
Quoted 5 lines
Recent kernel work[2] makes
guest_memfdmmap()-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.
+ Peter,
Peter had the series[*] to enable the mmap() of guest memfd and allow it serve as unencrypted memory for VMs. I believe there are some overlapped efforts.
[*] https://lore.kernel.org/qemu-devel/[email protected]/
Quoted 7 lines
DESIGN
------A new dedicated memory backend, memory-backend-guest-memfd, allocates its memory via a
guest_memfdfile descriptor obtained from KVM with theGUEST_MEMFD_FLAG_MMAP|GUEST_MEMFD_FLAG_INIT_SHAREDflags.
A quick feedback:
The design choice from Peter's series was to extend the current hostmem-memfd backend to support guest-memfd instead of a new dedicated backend. I think we need to evaluate the pros and cons of each other, and make a choice.
(I will go read the other part later and provide more feedback)
Quoted 68 lines · Xiaoyao Li
Quoted 61 lines · Michael Roth
This patchset is also available at:
https://github.com/amdese/qemu/commits/snp-inplace-rfc1
which is in turn based on the following series:
[PATCH 0/4] "
guest_memfd: Fix handling for conversions of MMIO ranges"
https://lists.gnu.org/archive/html/qemu-devel/2026-05/msg07547.htmlOVERVIEW
--------This series adds
guest_memfdsupport 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()-ableguest_memfdinstances[1], which allow it to be used for shared memory, and the following patchset[2], which adds additionalguest_memfdinterfaces to allow it to be used to perform in-place conversion:"[PATCH v7 00/42]
guest_memfd: In-place conversion support"
https://lore.kernel.org/kvm/[email protected]/That series also introduces a new '
vm_memory_attributes' 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-allocatedguest_memfdfile descriptor for private pages.KVM_SET_MEMORY_ATTRIBUTESflips 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_memfdwill 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.Quoted 5 lines
Recent kernel work[2] makes
guest_memfdmmap()-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.
Quoted 7 lines
+ Peter,
Peter had the series[*] to enable the
mmap()of guest memfd and allow it serve as unencrypted memory for VMs. I believe there are some overlapped efforts.
Thanks, I wasn't aware of that series but it definitely seems like a good idea to take that for base mmapable guest_memfd support for normal VMs and then rebase my inplace-conversion / confidential VM patches on top.
I do think it would be a good idea to introduce a dedicated backend however. I brought up the discussion in that thread, but I think that mostly only calls patch #2 from this series into question and most of the other patches still seem like they'll be needed for confidential VMs.
Thanks for pointing this out.
-Mike
Quoted 18 lines
Quoted 8 lines
DESIGN
------A new dedicated memory backend, memory-backend-guest-memfd, allocates its memory via a
guest_memfdfile descriptor obtained from KVM with theGUEST_MEMFD_FLAG_MMAP|GUEST_MEMFD_FLAG_INIT_SHAREDflags.A quick feedback:
The design choice from Peter's series was to extend the current hostmem-memfd backend to support guest-memfd instead of a new dedicated backend.
I think we need to evaluate the pros and cons of each other, and make a choice.(I will go read the other part later and provide more feedback)
Quoted 35 lines · Michael Roth
This patchset is also available at:
https://github.com/amdese/qemu/commits/snp-inplace-rfc1
which is in turn based on the following series:
[PATCH 0/4] "
guest_memfd: Fix handling for conversions of MMIO ranges"
https://lists.gnu.org/archive/html/qemu-devel/2026-05/msg07547.htmlOVERVIEW
--------This series adds
guest_memfdsupport 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()-ableguest_memfdinstances[1], which allow it to be used for shared memory, and the following patchset[2], which adds additionalguest_memfdinterfaces to allow it to be used to perform in-place conversion:"[PATCH v7 00/42]
guest_memfd: In-place conversion support"
https://lore.kernel.org/kvm/[email protected]/That series also introduces a new '
vm_memory_attributes' 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
----------
Quoted 7 lines
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-allocatedguest_memfdfile descriptor for private pages.KVM_SET_MEMORY_ATTRIBUTESflips 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.
Hi Michael,
I am giving this a go on Arm CCA on top of Ackerley's KVM patches.
When convert-in-place is switched on I think that the post conversion hook should not trigger discard+hole-punch since now guest-memfd _is_ the memory back-end but it looks like there is no guard in place against that (I noticed that ram_block_discard_range() triggers a hole-punch in kvm_post_convert_section() - when the CCA guest first requests a KVM_EXIT_MEMORY_FAULT to convert to private).
It is a question really.
Thanks,
Lorenzo
Quoted 191 lines
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_memfdwill 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_memfdmmap()-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
------A new dedicated memory backend, memory-backend-guest-memfd, allocates its memory via a
guest_memfdfile descriptor obtained from KVM with theGUEST_MEMFD_FLAG_MMAP|GUEST_MEMFD_FLAG_INIT_SHAREDflags. The fd is mmap()ed so userspace can access pages directly while they are in the shared state. For a normal/non-confidential VM, this backend can be used in a similar fashion as the existing memory-backend-memfd.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_SHAREDRAMBlock flag is added to track/enforce the dependency. Additionally, QEMU is modified to use mmap()-ableguest_memfdand set this flag for other cases where it allocates RAM internally. As a result,block->fdwill generally always aguest_memfd, and whenRAM_GUEST_MEMFD_SHAREDis set then thatblock->fdwill be qemu_dup()'d as the FD handle for private memory is well (which is currently whatblock->guest_memfdpoint to). This allows the prior non-in-place handling aroundblock->guest_memfdto be kept mostly unchanged.When running with convert-in-place=true, shared/private conversions are no longer handled directly by KVM, but instead by a new
guest_memfdioctl,KVM_SET_MEMORY_ATTRIBUTES2, which purposely provides similar naming/implementation to theKVM_SET_MEMORY_ATTRIBUTESKVM 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.Since
guest_memfdioctls need to be called against the specificguest_memfdinode associated with each memory slot/region, some refactoring is needed to handle conversions on a per-section. 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 that gets heavily re-used here.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-guest-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-guest-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_memfdas a general-purpose backend for non-confidential VMs, so it is intended to work in this manner as well.NOTES/TODO
----------- the CPR handling to support resetting of confidential VMs is currently disabled when in-place conversion is enabled.
- 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 upstreamREFERENCES
----------[1] https://lore.kernel.org/kvm/[email protected]/
[2] https://lore.kernel.org/kvm/[email protected]/
[3] https://www.youtube.com/watch?v=MMfAGNW9RVg
[4] 1GB hugetlb v2Thoughts, feedback, and testing are very much appreciated.
Thanks,
Mike
----------------------------------------------------------------
Michael Roth (12):
accel/kvm: Decoupleguest_memfdchecks from memory attribute checks
hostmem: Introduce dedicated memory backend forguest_memfd
linux-headers: Update headers for v7 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 system/memory: Default toguest_memfdfor 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 viaguest_memfdioctls accel/kvm: Don't default to private attributes for in-place conversion i386/sev: UpdateSNP_LAUNCH_UPDATEfor in-place conversion i386/sev: Allow in-place conversion for SEV-SNP guestsaccel/kvm/kvm-all.c | 286 +++++++++++-- accel/stubs/kvm-stub.c | 9 +- backends/confidential-guest-support.c | 25 ++ backends/hostmem-guest-memfd.c | 93 +++++ backends/meson.build | 1 + include/standard-headers/drm/drm_fourcc.h | 28 +- include/standard-headers/linux/const.h | 18 + include/standard-headers/linux/ethtool.h | 28 +- include/standard-headers/linux/input-event-codes.h | 13 + include/standard-headers/linux/pci_regs.h | 71 +++- include/standard-headers/linux/typelimits.h | 8 + include/standard-headers/linux/virtio_ring.h | 5 +- include/standard-headers/linux/virtio_rtc.h | 237 +++++++++++ include/standard-headers/linux/vmclock-abi.h | 20 + include/system/confidential-guest-support.h | 14 + include/system/hostmem.h | 1 + include/system/kvm.h | 3 +- include/system/memory.h | 8 +- linux-headers/asm-arm64/kvm.h | 1 + linux-headers/asm-arm64/unistd_64.h | 1 + linux-headers/asm-generic/unistd.h | 5 +- linux-headers/asm-loongarch/kvm.h | 5 + linux-headers/asm-loongarch/kvm_para.h | 1 + linux-headers/asm-loongarch/unistd_64.h | 2 + linux-headers/asm-mips/unistd_n32.h | 1 + linux-headers/asm-mips/unistd_n64.h | 1 + linux-headers/asm-mips/unistd_o32.h | 1 + linux-headers/asm-powerpc/unistd_32.h | 1 + linux-headers/asm-powerpc/unistd_64.h | 1 + linux-headers/asm-riscv/kvm.h | 11 +- linux-headers/asm-riscv/ptrace.h | 37 ++ linux-headers/asm-riscv/unistd_32.h | 1 + linux-headers/asm-riscv/unistd_64.h | 1 + linux-headers/asm-s390/unistd_32.h | 446 --------------------- linux-headers/asm-s390/unistd_64.h | 1 + linux-headers/asm-x86/kvm.h | 21 +- linux-headers/asm-x86/unistd_32.h | 1 + linux-headers/asm-x86/unistd_64.h | 1 + linux-headers/asm-x86/unistd_x32.h | 1 + linux-headers/linux/const.h | 18 + linux-headers/linux/iommufd.h | 48 +++ linux-headers/linux/kvm.h | 62 ++- linux-headers/linux/mshv.h | 4 +- linux-headers/linux/psp-sev.h | 2 +- linux-headers/linux/stddef.h | 4 + linux-headers/linux/vduse.h | 85 +++- linux-headers/linux/vfio.h | 30 +- qapi/qom.json | 35 +- qemu-options.hx | 5 + system/memory.c | 22 +- system/physmem.c | 50 ++- target/i386/sev.c | 12 +- 52 files changed, 1253 insertions(+), 533 deletions(-) create mode 100644 backends/hostmem-guest-memfd.c create mode 100644 include/standard-headers/linux/typelimits.h create mode 100644 include/standard-headers/linux/virtio_rtc.h delete mode 100644 linux-headers/asm-s390/unistd_32.h
Quoted 44 lines · Lorenzo Pieralisi
Quoted 35 lines · Michael Roth
This patchset is also available at:
https://github.com/amdese/qemu/commits/snp-inplace-rfc1
which is in turn based on the following series:
[PATCH 0/4] "
guest_memfd: Fix handling for conversions of MMIO ranges"
https://lists.gnu.org/archive/html/qemu-devel/2026-05/msg07547.htmlOVERVIEW
--------This series adds
guest_memfdsupport 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()-ableguest_memfdinstances[1], which allow it to be used for shared memory, and the following patchset[2], which adds additionalguest_memfdinterfaces to allow it to be used to perform in-place conversion:"[PATCH v7 00/42]
guest_memfd: In-place conversion support"
https://lore.kernel.org/kvm/[email protected]/That series also introduces a new '
vm_memory_attributes' 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
----------Quoted 7 lines
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-allocatedguest_memfdfile descriptor for private pages.KVM_SET_MEMORY_ATTRIBUTESflips 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.
Quoted 1 line
Hi Michael,
Hi Lorenzo,
Quoted 2 lines
I am giving this a go on Arm CCA on top of Ackerley's KVM patches.
Nice!
Quoted 9 lines
When convert-in-place is switched on I think that the post conversion hook should not trigger discard+hole-punch since now guest-memfd _is_ the memory back-end but it looks like there is no guard in place against that (I noticed that
ram_block_discard_range()triggers a hole-punch inkvm_post_convert_section()- when the CCA guest first requests aKVM_EXIT_MEMORY_FAULTto convert to private).It is a question really.
Yes, I agree that it does not make much sense to try to hole-punch for in-place conversion, it's just uncessary churn on the gmem side. In my internal branches I generally disabled this path but for some reason left that out of this series. I've tested with the below change and it should do the trick though; I'll roll something similar into v2 of the series.
diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c
index 5840daa7c8..2faec929b5 100644
--- a/accel/kvm/kvm-all.c
+++ b/accel/kvm/kvm-all.c
@@ -3619,17 +3619,19 @@ static int kvm_post_convert_section(MemoryRegionSection *section, bool to_privat
}
} - if (to_private) {
- if (rb->page_size != qemu_real_host_page_size()) {
- /*
- * shared memory is backed by hugetlb, which is supposed to be
- * pre-allocated and doesn't need to be discarded
- */
- return 0;
+ if (current_machine->cgs && current_machine->cgs->convert_in_place) {
+ if (to_private) {
+ if (rb->page_size != qemu_real_host_page_size()) {
+ /*
+ * shared memory is backed by hugetlb, which is supposed to be
+ * pre-allocated and doesn't need to be discarded
+ */
+ return 0;
+ }
+ ret = ram_block_discard_range(rb, offset, size);
+ } else {
+ ret = ram_block_discard_guest_memfd_range(rb, offset, size);
}
- ret = ram_block_discard_range(rb, offset, size);
- } else {
- ret = ram_block_discard_guest_memfd_range(rb, offset, size);
}
return 0;Thanks!
-Mike
Quoted 195 lines
Thanks,
LorenzoQuoted 191 lines
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_memfdwill 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_memfdmmap()-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
------A new dedicated memory backend, memory-backend-guest-memfd, allocates its memory via a
guest_memfdfile descriptor obtained from KVM with theGUEST_MEMFD_FLAG_MMAP|GUEST_MEMFD_FLAG_INIT_SHAREDflags. The fd is mmap()ed so userspace can access pages directly while they are in the shared state. For a normal/non-confidential VM, this backend can be used in a similar fashion as the existing memory-backend-memfd.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_SHAREDRAMBlock flag is added to track/enforce the dependency. Additionally, QEMU is modified to use mmap()-ableguest_memfdand set this flag for other cases where it allocates RAM internally. As a result,block->fdwill generally always aguest_memfd, and whenRAM_GUEST_MEMFD_SHAREDis set then thatblock->fdwill be qemu_dup()'d as the FD handle for private memory is well (which is currently whatblock->guest_memfdpoint to). This allows the prior non-in-place handling aroundblock->guest_memfdto be kept mostly unchanged.When running with convert-in-place=true, shared/private conversions are no longer handled directly by KVM, but instead by a new
guest_memfdioctl,KVM_SET_MEMORY_ATTRIBUTES2, which purposely provides similar naming/implementation to theKVM_SET_MEMORY_ATTRIBUTESKVM 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.Since
guest_memfdioctls need to be called against the specificguest_memfdinode associated with each memory slot/region, some refactoring is needed to handle conversions on a per-section. 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 that gets heavily re-used here.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-guest-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-guest-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_memfdas a general-purpose backend for non-confidential VMs, so it is intended to work in this manner as well.NOTES/TODO
----------- the CPR handling to support resetting of confidential VMs is currently disabled when in-place conversion is enabled.
- 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 upstreamREFERENCES
----------[1] https://lore.kernel.org/kvm/[email protected]/
[2] https://lore.kernel.org/kvm/[email protected]/
[3] https://www.youtube.com/watch?v=MMfAGNW9RVg
[4] 1GB hugetlb v2Thoughts, feedback, and testing are very much appreciated.
Thanks,
Mike
----------------------------------------------------------------
Michael Roth (12):
accel/kvm: Decoupleguest_memfdchecks from memory attribute checks
hostmem: Introduce dedicated memory backend forguest_memfd
linux-headers: Update headers for v7 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 system/memory: Default toguest_memfdfor 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 viaguest_memfdioctls accel/kvm: Don't default to private attributes for in-place conversion i386/sev: UpdateSNP_LAUNCH_UPDATEfor in-place conversion i386/sev: Allow in-place conversion for SEV-SNP guestsaccel/kvm/kvm-all.c | 286 +++++++++++-- accel/stubs/kvm-stub.c | 9 +- backends/confidential-guest-support.c | 25 ++ backends/hostmem-guest-memfd.c | 93 +++++ backends/meson.build | 1 + include/standard-headers/drm/drm_fourcc.h | 28 +- include/standard-headers/linux/const.h | 18 + include/standard-headers/linux/ethtool.h | 28 +- include/standard-headers/linux/input-event-codes.h | 13 + include/standard-headers/linux/pci_regs.h | 71 +++- include/standard-headers/linux/typelimits.h | 8 + include/standard-headers/linux/virtio_ring.h | 5 +- include/standard-headers/linux/virtio_rtc.h | 237 +++++++++++ include/standard-headers/linux/vmclock-abi.h | 20 + include/system/confidential-guest-support.h | 14 + include/system/hostmem.h | 1 + include/system/kvm.h | 3 +- include/system/memory.h | 8 +- linux-headers/asm-arm64/kvm.h | 1 + linux-headers/asm-arm64/unistd_64.h | 1 + linux-headers/asm-generic/unistd.h | 5 +- linux-headers/asm-loongarch/kvm.h | 5 + linux-headers/asm-loongarch/kvm_para.h | 1 + linux-headers/asm-loongarch/unistd_64.h | 2 + linux-headers/asm-mips/unistd_n32.h | 1 + linux-headers/asm-mips/unistd_n64.h | 1 + linux-headers/asm-mips/unistd_o32.h | 1 + linux-headers/asm-powerpc/unistd_32.h | 1 + linux-headers/asm-powerpc/unistd_64.h | 1 + linux-headers/asm-riscv/kvm.h | 11 +- linux-headers/asm-riscv/ptrace.h | 37 ++ linux-headers/asm-riscv/unistd_32.h | 1 + linux-headers/asm-riscv/unistd_64.h | 1 + linux-headers/asm-s390/unistd_32.h | 446 --------------------- linux-headers/asm-s390/unistd_64.h | 1 + linux-headers/asm-x86/kvm.h | 21 +- linux-headers/asm-x86/unistd_32.h | 1 + linux-headers/asm-x86/unistd_64.h | 1 + linux-headers/asm-x86/unistd_x32.h | 1 + linux-headers/linux/const.h | 18 + linux-headers/linux/iommufd.h | 48 +++ linux-headers/linux/kvm.h | 62 ++- linux-headers/linux/mshv.h | 4 +- linux-headers/linux/psp-sev.h | 2 +- linux-headers/linux/stddef.h | 4 + linux-headers/linux/vduse.h | 85 +++- linux-headers/linux/vfio.h | 30 +- qapi/qom.json | 35 +- qemu-options.hx | 5 + system/memory.c | 22 +- system/physmem.c | 50 ++- target/i386/sev.c | 12 +- 52 files changed, 1253 insertions(+), 533 deletions(-) create mode 100644 backends/hostmem-guest-memfd.c create mode 100644 include/standard-headers/linux/typelimits.h create mode 100644 include/standard-headers/linux/virtio_rtc.h delete mode 100644 linux-headers/asm-s390/unistd_32.h
Quoted 62 lines · Michael Roth
Quoted 44 lines · Lorenzo Pieralisi
Quoted 35 lines · Michael Roth
This patchset is also available at:
https://github.com/amdese/qemu/commits/snp-inplace-rfc1
which is in turn based on the following series:
[PATCH 0/4] "
guest_memfd: Fix handling for conversions of MMIO ranges"
https://lists.gnu.org/archive/html/qemu-devel/2026-05/msg07547.htmlOVERVIEW
--------This series adds
guest_memfdsupport 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()-ableguest_memfdinstances[1], which allow it to be used for shared memory, and the following patchset[2], which adds additionalguest_memfdinterfaces to allow it to be used to perform in-place conversion:"[PATCH v7 00/42]
guest_memfd: In-place conversion support"
https://lore.kernel.org/kvm/[email protected]/That series also introduces a new '
vm_memory_attributes' 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
----------Quoted 7 lines
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-allocatedguest_memfdfile descriptor for private pages.KVM_SET_MEMORY_ATTRIBUTESflips 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.Quoted 1 line
Hi Michael,
Hi Lorenzo,
Quoted 2 lines
I am giving this a go on Arm CCA on top of Ackerley's KVM patches.
Nice!
Quoted 8 lines
When convert-in-place is switched on I think that the post conversion hook should not trigger discard+hole-punch since now guest-memfd _is_ the memory back-end but it looks like there is no guard in place against that (I noticed that
ram_block_discard_range()triggers a hole-punch inkvm_post_convert_section()- when the CCA guest first requests aKVM_EXIT_MEMORY_FAULTto convert to private).
Quoted 4 lines
Quoted 1 line
It is a question really.
Yes, I agree that it does not make much sense to try to hole-punch for in-place conversion, it's just uncessary churn on the gmem side. In my
FWIW - on CCA I am not sure it is just "churn" especially if it is done on transition to private when guest-memfd pages are already private.
Quoted 4 lines
internal branches I generally disabled this path but for some reason left that out of this series. I've tested with the below change and it should do the trick though; I'll roll something similar into v2 of the series.
Thanks a lot, I will give it a spin.
Thanks !
Lorenzo
Quoted 234 lines
diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index 5840daa7c8..2faec929b5 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -3619,17 +3619,19 @@ static int kvm_post_convert_section(MemoryRegionSection *section, bool to_privat } }- if (
to_private) {
- if (rb->page_size!=qemu_real_host_page_size()) {
- /*- * shared memory is backed by hugetlb, which is supposed to be - * pre-allocated and doesn't need to be discarded - */ - return 0; + if (current_machine->cgs && current_machine->cgs->convert_in_place) { + if (to_private) { + if (rb->page_size != qemu_real_host_page_size()) { + /* + * shared memory is backed by hugetlb, which is supposed to be + * pre-allocated and doesn't need to be discarded + */ + return 0; + } + ret = ram_block_discard_range(rb, offset, size); + } else { + ret = ram_block_discard_guest_memfd_range(rb, offset, size); } - ret = ram_block_discard_range(rb, offset, size); - } else { - ret = ram_block_discard_guest_memfd_range(rb, offset, size); } return 0;Thanks!
-Mike
Quoted 195 lines
Thanks,
LorenzoQuoted 191 lines
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_memfdwill 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_memfdmmap()-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
------A new dedicated memory backend, memory-backend-guest-memfd, allocates its memory via a
guest_memfdfile descriptor obtained from KVM with theGUEST_MEMFD_FLAG_MMAP|GUEST_MEMFD_FLAG_INIT_SHAREDflags. The fd is mmap()ed so userspace can access pages directly while they are in the shared state. For a normal/non-confidential VM, this backend can be used in a similar fashion as the existing memory-backend-memfd.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_SHAREDRAMBlock flag is added to track/enforce the dependency. Additionally, QEMU is modified to use mmap()-ableguest_memfdand set this flag for other cases where it allocates RAM internally. As a result,block->fdwill generally always aguest_memfd, and whenRAM_GUEST_MEMFD_SHAREDis set then thatblock->fdwill be qemu_dup()'d as the FD handle for private memory is well (which is currently whatblock->guest_memfdpoint to). This allows the prior non-in-place handling aroundblock->guest_memfdto be kept mostly unchanged.When running with convert-in-place=true, shared/private conversions are no longer handled directly by KVM, but instead by a new
guest_memfdioctl,KVM_SET_MEMORY_ATTRIBUTES2, which purposely provides similar naming/implementation to theKVM_SET_MEMORY_ATTRIBUTESKVM 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.Since
guest_memfdioctls need to be called against the specificguest_memfdinode associated with each memory slot/region, some refactoring is needed to handle conversions on a per-section. 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 that gets heavily re-used here.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-guest-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-guest-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_memfdas a general-purpose backend for non-confidential VMs, so it is intended to work in this manner as well.NOTES/TODO
----------- the CPR handling to support resetting of confidential VMs is currently disabled when in-place conversion is enabled.
- 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 upstreamREFERENCES
----------[1] https://lore.kernel.org/kvm/[email protected]/
[2] https://lore.kernel.org/kvm/[email protected]/
[3] https://www.youtube.com/watch?v=MMfAGNW9RVg
[4] 1GB hugetlb v2Thoughts, feedback, and testing are very much appreciated.
Thanks,
Mike
----------------------------------------------------------------
Michael Roth (12):
accel/kvm: Decoupleguest_memfdchecks from memory attribute checks
hostmem: Introduce dedicated memory backend forguest_memfd
linux-headers: Update headers for v7 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 system/memory: Default toguest_memfdfor 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 viaguest_memfdioctls accel/kvm: Don't default to private attributes for in-place conversion i386/sev: UpdateSNP_LAUNCH_UPDATEfor in-place conversion i386/sev: Allow in-place conversion for SEV-SNP guestsaccel/kvm/kvm-all.c | 286 +++++++++++-- accel/stubs/kvm-stub.c | 9 +- backends/confidential-guest-support.c | 25 ++ backends/hostmem-guest-memfd.c | 93 +++++ backends/meson.build | 1 + include/standard-headers/drm/drm_fourcc.h | 28 +- include/standard-headers/linux/const.h | 18 + include/standard-headers/linux/ethtool.h | 28 +- include/standard-headers/linux/input-event-codes.h | 13 + include/standard-headers/linux/pci_regs.h | 71 +++- include/standard-headers/linux/typelimits.h | 8 + include/standard-headers/linux/virtio_ring.h | 5 +- include/standard-headers/linux/virtio_rtc.h | 237 +++++++++++ include/standard-headers/linux/vmclock-abi.h | 20 + include/system/confidential-guest-support.h | 14 + include/system/hostmem.h | 1 + include/system/kvm.h | 3 +- include/system/memory.h | 8 +- linux-headers/asm-arm64/kvm.h | 1 + linux-headers/asm-arm64/unistd_64.h | 1 + linux-headers/asm-generic/unistd.h | 5 +- linux-headers/asm-loongarch/kvm.h | 5 + linux-headers/asm-loongarch/kvm_para.h | 1 + linux-headers/asm-loongarch/unistd_64.h | 2 + linux-headers/asm-mips/unistd_n32.h | 1 + linux-headers/asm-mips/unistd_n64.h | 1 + linux-headers/asm-mips/unistd_o32.h | 1 + linux-headers/asm-powerpc/unistd_32.h | 1 + linux-headers/asm-powerpc/unistd_64.h | 1 + linux-headers/asm-riscv/kvm.h | 11 +- linux-headers/asm-riscv/ptrace.h | 37 ++ linux-headers/asm-riscv/unistd_32.h | 1 + linux-headers/asm-riscv/unistd_64.h | 1 + linux-headers/asm-s390/unistd_32.h | 446 --------------------- linux-headers/asm-s390/unistd_64.h | 1 + linux-headers/asm-x86/kvm.h | 21 +- linux-headers/asm-x86/unistd_32.h | 1 + linux-headers/asm-x86/unistd_64.h | 1 + linux-headers/asm-x86/unistd_x32.h | 1 + linux-headers/linux/const.h | 18 + linux-headers/linux/iommufd.h | 48 +++ linux-headers/linux/kvm.h | 62 ++- linux-headers/linux/mshv.h | 4 +- linux-headers/linux/psp-sev.h | 2 +- linux-headers/linux/stddef.h | 4 + linux-headers/linux/vduse.h | 85 +++- linux-headers/linux/vfio.h | 30 +- qapi/qom.json | 35 +- qemu-options.hx | 5 + system/memory.c | 22 +- system/physmem.c | 50 ++- target/i386/sev.c | 12 +- 52 files changed, 1253 insertions(+), 533 deletions(-) create mode 100644 backends/hostmem-guest-memfd.c create mode 100644 include/standard-headers/linux/typelimits.h create mode 100644 include/standard-headers/linux/virtio_rtc.h delete mode 100644 linux-headers/asm-s390/unistd_32.h
Quoted 79 lines · Michael Roth
Quoted 44 lines · Lorenzo Pieralisi
Quoted 35 lines · Michael Roth
This patchset is also available at:
https://github.com/amdese/qemu/commits/snp-inplace-rfc1
which is in turn based on the following series:
[PATCH 0/4] "
guest_memfd: Fix handling for conversions of MMIO ranges"
https://lists.gnu.org/archive/html/qemu-devel/2026-05/msg07547.htmlOVERVIEW
--------This series adds
guest_memfdsupport 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()-ableguest_memfdinstances[1], which allow it to be used for shared memory, and the following patchset[2], which adds additionalguest_memfdinterfaces to allow it to be used to perform in-place conversion:"[PATCH v7 00/42]
guest_memfd: In-place conversion support"
https://lore.kernel.org/kvm/[email protected]/That series also introduces a new '
vm_memory_attributes' 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
----------Quoted 7 lines
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-allocatedguest_memfdfile descriptor for private pages.KVM_SET_MEMORY_ATTRIBUTESflips 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.Quoted 1 line
Hi Michael,
Hi Lorenzo,
Quoted 2 lines
I am giving this a go on Arm CCA on top of Ackerley's KVM patches.
Nice!
Quoted 9 lines
When convert-in-place is switched on I think that the post conversion hook should not trigger discard+hole-punch since now guest-memfd _is_ the memory back-end but it looks like there is no guard in place against that (I noticed that
ram_block_discard_range()triggers a hole-punch inkvm_post_convert_section()- when the CCA guest first requests aKVM_EXIT_MEMORY_FAULTto convert to private).It is a question really.
Yes, I agree that it does not make much sense to try to hole-punch for in-place conversion, it's just uncessary churn on the gmem side. In my internal branches I generally disabled this path but for some reason left that out of this series. I've tested with the below change and it should do the trick though; I'll roll something similar into v2 of the series.
diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index 5840daa7c8..2faec929b5 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -3619,17 +3619,19 @@ static int kvm_post_convert_section(MemoryRegionSection *section, bool to_privat } }
Quoted 8 lines
- if (
to_private) {
- if (rb->page_size!=qemu_real_host_page_size()) {
- /*- * shared memory is backed by hugetlb, which is supposed to be - * pre-allocated and doesn't need to be discarded - */- return 0;
+ if (current_machine->cgs&¤t_machine->cgs->convert_in_place) {
Shouldn't we execute the discards only if (!cgs || !cgs->convert_in_place)
Or flipped around, just return if (cgs && cgs->convert_in_place) ?
Thanks,
Lorenzo
Quoted 218 lines
+ if (
to_private) {
+ if (rb->page_size!=qemu_real_host_page_size()) {
+ /*+ * shared memory is backed by hugetlb, which is supposed to be + * pre-allocated and doesn't need to be discarded + */ + return 0; + } + ret = ram_block_discard_range(rb, offset, size); + } else { + ret = ram_block_discard_guest_memfd_range(rb, offset, size); } - ret = ram_block_discard_range(rb, offset, size); - } else { - ret = ram_block_discard_guest_memfd_range(rb, offset, size); } return 0;Thanks!
-Mike
Quoted 195 lines
Thanks,
LorenzoQuoted 191 lines
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_memfdwill 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_memfdmmap()-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
------A new dedicated memory backend, memory-backend-guest-memfd, allocates its memory via a
guest_memfdfile descriptor obtained from KVM with theGUEST_MEMFD_FLAG_MMAP|GUEST_MEMFD_FLAG_INIT_SHAREDflags. The fd is mmap()ed so userspace can access pages directly while they are in the shared state. For a normal/non-confidential VM, this backend can be used in a similar fashion as the existing memory-backend-memfd.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_SHAREDRAMBlock flag is added to track/enforce the dependency. Additionally, QEMU is modified to use mmap()-ableguest_memfdand set this flag for other cases where it allocates RAM internally. As a result,block->fdwill generally always aguest_memfd, and whenRAM_GUEST_MEMFD_SHAREDis set then thatblock->fdwill be qemu_dup()'d as the FD handle for private memory is well (which is currently whatblock->guest_memfdpoint to). This allows the prior non-in-place handling aroundblock->guest_memfdto be kept mostly unchanged.When running with convert-in-place=true, shared/private conversions are no longer handled directly by KVM, but instead by a new
guest_memfdioctl,KVM_SET_MEMORY_ATTRIBUTES2, which purposely provides similar naming/implementation to theKVM_SET_MEMORY_ATTRIBUTESKVM 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.Since
guest_memfdioctls need to be called against the specificguest_memfdinode associated with each memory slot/region, some refactoring is needed to handle conversions on a per-section. 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 that gets heavily re-used here.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-guest-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-guest-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_memfdas a general-purpose backend for non-confidential VMs, so it is intended to work in this manner as well.NOTES/TODO
----------- the CPR handling to support resetting of confidential VMs is currently disabled when in-place conversion is enabled.
- 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 upstreamREFERENCES
----------[1] https://lore.kernel.org/kvm/[email protected]/
[2] https://lore.kernel.org/kvm/[email protected]/
[3] https://www.youtube.com/watch?v=MMfAGNW9RVg
[4] 1GB hugetlb v2Thoughts, feedback, and testing are very much appreciated.
Thanks,
Mike
----------------------------------------------------------------
Michael Roth (12):
accel/kvm: Decoupleguest_memfdchecks from memory attribute checks
hostmem: Introduce dedicated memory backend forguest_memfd
linux-headers: Update headers for v7 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 system/memory: Default toguest_memfdfor 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 viaguest_memfdioctls accel/kvm: Don't default to private attributes for in-place conversion i386/sev: UpdateSNP_LAUNCH_UPDATEfor in-place conversion i386/sev: Allow in-place conversion for SEV-SNP guestsaccel/kvm/kvm-all.c | 286 +++++++++++-- accel/stubs/kvm-stub.c | 9 +- backends/confidential-guest-support.c | 25 ++ backends/hostmem-guest-memfd.c | 93 +++++ backends/meson.build | 1 + include/standard-headers/drm/drm_fourcc.h | 28 +- include/standard-headers/linux/const.h | 18 + include/standard-headers/linux/ethtool.h | 28 +- include/standard-headers/linux/input-event-codes.h | 13 + include/standard-headers/linux/pci_regs.h | 71 +++- include/standard-headers/linux/typelimits.h | 8 + include/standard-headers/linux/virtio_ring.h | 5 +- include/standard-headers/linux/virtio_rtc.h | 237 +++++++++++ include/standard-headers/linux/vmclock-abi.h | 20 + include/system/confidential-guest-support.h | 14 + include/system/hostmem.h | 1 + include/system/kvm.h | 3 +- include/system/memory.h | 8 +- linux-headers/asm-arm64/kvm.h | 1 + linux-headers/asm-arm64/unistd_64.h | 1 + linux-headers/asm-generic/unistd.h | 5 +- linux-headers/asm-loongarch/kvm.h | 5 + linux-headers/asm-loongarch/kvm_para.h | 1 + linux-headers/asm-loongarch/unistd_64.h | 2 + linux-headers/asm-mips/unistd_n32.h | 1 + linux-headers/asm-mips/unistd_n64.h | 1 + linux-headers/asm-mips/unistd_o32.h | 1 + linux-headers/asm-powerpc/unistd_32.h | 1 + linux-headers/asm-powerpc/unistd_64.h | 1 + linux-headers/asm-riscv/kvm.h | 11 +- linux-headers/asm-riscv/ptrace.h | 37 ++ linux-headers/asm-riscv/unistd_32.h | 1 + linux-headers/asm-riscv/unistd_64.h | 1 + linux-headers/asm-s390/unistd_32.h | 446 --------------------- linux-headers/asm-s390/unistd_64.h | 1 + linux-headers/asm-x86/kvm.h | 21 +- linux-headers/asm-x86/unistd_32.h | 1 + linux-headers/asm-x86/unistd_64.h | 1 + linux-headers/asm-x86/unistd_x32.h | 1 + linux-headers/linux/const.h | 18 + linux-headers/linux/iommufd.h | 48 +++ linux-headers/linux/kvm.h | 62 ++- linux-headers/linux/mshv.h | 4 +- linux-headers/linux/psp-sev.h | 2 +- linux-headers/linux/stddef.h | 4 + linux-headers/linux/vduse.h | 85 +++- linux-headers/linux/vfio.h | 30 +- qapi/qom.json | 35 +- qemu-options.hx | 5 + system/memory.c | 22 +- system/physmem.c | 50 ++- target/i386/sev.c | 12 +- 52 files changed, 1253 insertions(+), 533 deletions(-) create mode 100644 backends/hostmem-guest-memfd.c create mode 100644 include/standard-headers/linux/typelimits.h create mode 100644 include/standard-headers/linux/virtio_rtc.h delete mode 100644 linux-headers/asm-s390/unistd_32.h
On Thu, Jun 25, 2026 at 04:21:52PM -0500, Michael Roth wrote:
[...]
Quoted 7 lines
Quoted 7 lines
When convert-in-place is switched on I think that the post conversion hook should not trigger discard+hole-punch since now guest-memfd _is_ the memory back-end but it looks like there is no guard in place against that (I noticed that
ram_block_discard_range()triggers a hole-punch inkvm_post_convert_section()- when the CCA guest first requests aKVM_EXIT_MEMORY_FAULTto convert to private).
Quoted 8 lines
Quoted 1 line
It is a question really.
Yes, I agree that it does not make much sense to try to hole-punch for in-place conversion, it's just uncessary churn on the gmem side. In my internal branches I generally disabled this path but for some reason left that out of this series. I've tested with the below change and it should do the trick though; I'll roll something similar into v2 of the series.
Hi Michael,
have you planned posting a v2 rebased against a more recent codebase ?
Thanks,
Lorenzo
Quoted 235 lines
diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index 5840daa7c8..2faec929b5 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -3619,17 +3619,19 @@ static int kvm_post_convert_section(MemoryRegionSection *section, bool to_privat } }- if (
to_private) {
- if (rb->page_size!=qemu_real_host_page_size()) {
- /*- * shared memory is backed by hugetlb, which is supposed to be - * pre-allocated and doesn't need to be discarded - */ - return 0; + if (current_machine->cgs && current_machine->cgs->convert_in_place) { + if (to_private) { + if (rb->page_size != qemu_real_host_page_size()) { + /* + * shared memory is backed by hugetlb, which is supposed to be + * pre-allocated and doesn't need to be discarded + */ + return 0; + } + ret = ram_block_discard_range(rb, offset, size); + } else { + ret = ram_block_discard_guest_memfd_range(rb, offset, size); } - ret = ram_block_discard_range(rb, offset, size); - } else { - ret = ram_block_discard_guest_memfd_range(rb, offset, size); } return 0;Thanks!
-Mike
Quoted 195 lines
Thanks,
LorenzoQuoted 191 lines
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_memfdwill 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_memfdmmap()-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
------A new dedicated memory backend, memory-backend-guest-memfd, allocates its memory via a
guest_memfdfile descriptor obtained from KVM with theGUEST_MEMFD_FLAG_MMAP|GUEST_MEMFD_FLAG_INIT_SHAREDflags. The fd is mmap()ed so userspace can access pages directly while they are in the shared state. For a normal/non-confidential VM, this backend can be used in a similar fashion as the existing memory-backend-memfd.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_SHAREDRAMBlock flag is added to track/enforce the dependency. Additionally, QEMU is modified to use mmap()-ableguest_memfdand set this flag for other cases where it allocates RAM internally. As a result,block->fdwill generally always aguest_memfd, and whenRAM_GUEST_MEMFD_SHAREDis set then thatblock->fdwill be qemu_dup()'d as the FD handle for private memory is well (which is currently whatblock->guest_memfdpoint to). This allows the prior non-in-place handling aroundblock->guest_memfdto be kept mostly unchanged.When running with convert-in-place=true, shared/private conversions are no longer handled directly by KVM, but instead by a new
guest_memfdioctl,KVM_SET_MEMORY_ATTRIBUTES2, which purposely provides similar naming/implementation to theKVM_SET_MEMORY_ATTRIBUTESKVM 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.Since
guest_memfdioctls need to be called against the specificguest_memfdinode associated with each memory slot/region, some refactoring is needed to handle conversions on a per-section. 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 that gets heavily re-used here.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-guest-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-guest-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_memfdas a general-purpose backend for non-confidential VMs, so it is intended to work in this manner as well.NOTES/TODO
----------- the CPR handling to support resetting of confidential VMs is currently disabled when in-place conversion is enabled.
- 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 upstreamREFERENCES
----------[1] https://lore.kernel.org/kvm/[email protected]/
[2] https://lore.kernel.org/kvm/[email protected]/
[3] https://www.youtube.com/watch?v=MMfAGNW9RVg
[4] 1GB hugetlb v2Thoughts, feedback, and testing are very much appreciated.
Thanks,
Mike
----------------------------------------------------------------
Michael Roth (12):
accel/kvm: Decoupleguest_memfdchecks from memory attribute checks
hostmem: Introduce dedicated memory backend forguest_memfd
linux-headers: Update headers for v7 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 system/memory: Default toguest_memfdfor 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 viaguest_memfdioctls accel/kvm: Don't default to private attributes for in-place conversion i386/sev: UpdateSNP_LAUNCH_UPDATEfor in-place conversion i386/sev: Allow in-place conversion for SEV-SNP guestsaccel/kvm/kvm-all.c | 286 +++++++++++-- accel/stubs/kvm-stub.c | 9 +- backends/confidential-guest-support.c | 25 ++ backends/hostmem-guest-memfd.c | 93 +++++ backends/meson.build | 1 + include/standard-headers/drm/drm_fourcc.h | 28 +- include/standard-headers/linux/const.h | 18 + include/standard-headers/linux/ethtool.h | 28 +- include/standard-headers/linux/input-event-codes.h | 13 + include/standard-headers/linux/pci_regs.h | 71 +++- include/standard-headers/linux/typelimits.h | 8 + include/standard-headers/linux/virtio_ring.h | 5 +- include/standard-headers/linux/virtio_rtc.h | 237 +++++++++++ include/standard-headers/linux/vmclock-abi.h | 20 + include/system/confidential-guest-support.h | 14 + include/system/hostmem.h | 1 + include/system/kvm.h | 3 +- include/system/memory.h | 8 +- linux-headers/asm-arm64/kvm.h | 1 + linux-headers/asm-arm64/unistd_64.h | 1 + linux-headers/asm-generic/unistd.h | 5 +- linux-headers/asm-loongarch/kvm.h | 5 + linux-headers/asm-loongarch/kvm_para.h | 1 + linux-headers/asm-loongarch/unistd_64.h | 2 + linux-headers/asm-mips/unistd_n32.h | 1 + linux-headers/asm-mips/unistd_n64.h | 1 + linux-headers/asm-mips/unistd_o32.h | 1 + linux-headers/asm-powerpc/unistd_32.h | 1 + linux-headers/asm-powerpc/unistd_64.h | 1 + linux-headers/asm-riscv/kvm.h | 11 +- linux-headers/asm-riscv/ptrace.h | 37 ++ linux-headers/asm-riscv/unistd_32.h | 1 + linux-headers/asm-riscv/unistd_64.h | 1 + linux-headers/asm-s390/unistd_32.h | 446 --------------------- linux-headers/asm-s390/unistd_64.h | 1 + linux-headers/asm-x86/kvm.h | 21 +- linux-headers/asm-x86/unistd_32.h | 1 + linux-headers/asm-x86/unistd_64.h | 1 + linux-headers/asm-x86/unistd_x32.h | 1 + linux-headers/linux/const.h | 18 + linux-headers/linux/iommufd.h | 48 +++ linux-headers/linux/kvm.h | 62 ++- linux-headers/linux/mshv.h | 4 +- linux-headers/linux/psp-sev.h | 2 +- linux-headers/linux/stddef.h | 4 + linux-headers/linux/vduse.h | 85 +++- linux-headers/linux/vfio.h | 30 +- qapi/qom.json | 35 +- qemu-options.hx | 5 + system/memory.c | 22 +- system/physmem.c | 50 ++- target/i386/sev.c | 12 +- 52 files changed, 1253 insertions(+), 533 deletions(-) create mode 100644 backends/hostmem-guest-memfd.c create mode 100644 include/standard-headers/linux/typelimits.h create mode 100644 include/standard-headers/linux/virtio_rtc.h delete mode 100644 linux-headers/asm-s390/unistd_32.h