Enable shared device assignment
Subsystems (2)
cover Enable shared device assignment
Commit 852f0048f3 ("RAMBlock: make guest_memfd require uncoordinated discard") effectively disables device assignment with guest_memfd. guest_memfd is required for confidential guests, so device assignment to confidential guests is disabled. A supporting assumption for disabling device-assignment was that TEE I/O (SEV-TIO, TDX Connect, COVE-IO etc...) solves the confidential-guest device-assignment problem [1]. That turns out not to be the case because TEE I/O depends on being able to operate devices against "shared"/untrusted memory for device initialization and error recovery scenarios.
This series utilizes an existing framework named RamDiscardManager to notify VFIO of page conversions. However, there's still one concern related to the semantics of RamDiscardManager which is used to manage the memory plug/unplug state. This is a little different from the memory shared/private in our requirement. See the "Open" section below for more details.
Background
==========
Confidential VMs have two classes of memory: shared and private memory. Shared memory is accessible from the host/VMM while private memory is not. Confidential VMs can decide which memory is shared/private and convert memory between shared/private at runtime.
"guest_memfd" is a new kind of fd whose primary goal is to serve guest private memory. The key differences between guest_memfd and normal memfd are that guest_memfd is spawned by a KVM ioctl, bound to its owner VM and cannot be mapped, read or written by userspace.
In QEMU's implementation, shared memory is allocated with normal methods (e.g. mmap or fallocate) while private memory is allocated from guest_memfd. When a VM performs memory conversions, QEMU frees pages via madvise() or via PUNCH_HOLE on memfd or guest_memfd from one side and allocates new pages from the other side.
Problem
=======
Device assignment in QEMU is implemented via VFIO system. In the normal VM, VM memory is pinned at the beginning of time by VFIO. In the confidential VM, the VM can convert memory and when that happens nothing currently tells VFIO that its mappings are stale. This means that page conversion leaks memory and leaves stale IOMMU mappings. For example, sequence like the following can result in stale IOMMU mappings:
1. allocate shared page
2. convert page shared->private
3. discard shared page
4. convert page private->shared
5. allocate shared page
6. issue DMA operations against that shared page
After step 3, VFIO is still pinning the page. However, DMA operations in step 6 will hit the old mapping that was allocated in step 1, which causes the device to access the invalid data.
Currently, the commit 852f0048f3 ("RAMBlock: make guest_memfd require uncoordinated discard") has blocked the device assignment with guest_memfd to avoid this problem.
Solution
========
The key to enable shared device assignment is to solve the stale IOMMU mappings problem.
Given the constraints and assumptions here is a solution that satisfied the use cases. RamDiscardManager, an existing interface currently utilized by virtio-mem, offers a means to modify IOMMU mappings in accordance with VM page assignment. Page conversion is similar to hot-removing a page in one mode and adding it back in the other.
This series implements a RamDiscardManager for confidential VMs and utilizes its infrastructure to notify VFIO of page conversions.
Another possible attempt [2] was to not discard shared pages in step 3 above. This was an incomplete band-aid because guests would consume twice the memory since shared pages wouldn't be freed even after they were converted to private.
Open
====
Implementing a RamDiscardManager to notify VFIO of page conversions causes changes in semantics: private memory is treated as discarded (or hot-removed) memory. This isn't aligned with the expectation of current RamDiscardManager users (e.g. VFIO or live migration) who really expect that discarded memory is hot-removed and thus can be skipped when the users are processing guest memory. Treating private memory as discarded won't work in future if VFIO or live migration needs to handle private memory. e.g. VFIO may need to map private memory to support Trusted IO and live migration for confidential VMs need to migrate private memory.
There are two possible ways to mitigate the semantics changes.
1. Develop a new mechanism to notify the page conversions between private and shared. For example, utilize the notifier_list in QEMU. VFIO registers its own handler and gets notified upon page conversions. This is a clean approach which only touches the notifier workflow. A challenge is that for device hotplug, existing shared memory should be mapped in IOMMU. This will need additional changes.
2. Extend the existing RamDiscardManager interface to manage not only the discarded/populated status of guest memory but also the shared/private status. RamDiscardManager users like VFIO will be notified with one more argument indicating what change is happening and can take action accordingly. It also has challenges e.g. QEMU allows only one RamDiscardManager, how to support virtio-mem for confidential VMs would be a problem. And some APIs like .is_populated() exposed by RamDiscardManager are meaningless to shared/private memory. So they may need some adjustments.
Testing
=======
This patch series is tested based on the internal TDX KVM/QEMU tree.
To facilitate shared device assignment with the NIC, employ the legacy type1 VFIO with the QEMU command:
qemu-system-x86_64 [...]
-device vfio-pci,host=XX:XX.X
The parameter of dma_entry_limit needs to be adjusted. For example, a 16GB guest needs to adjust the parameter like vfio_iommu_type1.dma_entry_limit=4194304.
If use the iommufd-backed VFIO with the qemu command:
qemu-system-x86_64 [...]
-object iommufd,id=iommufd0 \
-device vfio-pci,host=XX:XX.X,iommufd=iommufd0No additional adjustment required.
Following the bootup of the TD guest, the guest's IP address becomes visible, and iperf is able to successfully send and receive data.
Related link
============
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
Chenyi Qiang (6):
guest_memfd: Introduce an object to manage the guest-memfd with
RamDiscardManager
guest_memfd: Introduce a helper to notify the shared/private state
change
KVM: Notify the state change via RamDiscardManager helper during
shared/private conversion
memory: Register the RamDiscardManager instance upon guest_memfd
creation
guest-memfd: Default to discarded (private) in guest_memfd_manager
RAMBlock: make guest_memfd require coordinate discard
accel/kvm/kvm-all.c | 7 + include/sysemu/guest-memfd-manager.h | 49 +++ system/guest-memfd-manager.c | 425 +++++++++++++++++++++++++++ system/meson.build | 1 + system/physmem.c | 11 +- 5 files changed, 492 insertions(+), 1 deletion(-) create mode 100644 include/sysemu/guest-memfd-manager.h create mode 100644 system/guest-memfd-manager.c
base-commit: 900536d3e97aed7fdd9cb4dadd3bf7023360e819
Signature · 1 line
2.43.5
RFC PATCH 1/6 guest_memfd: Introduce an object to manage the guest-memfd with RamDiscardManager
As the commit 852f0048f3 ("RAMBlock: make guest_memfd require uncoordinated discard") highlighted, some subsystems like VFIO might disable ram block discard. However, guest_memfd relies on the discard operation to perform page conversion between private and shared memory. This can lead to stale IOMMU mapping issue when assigning a hardware device to a confidential guest via shared memory (unprotected memory pages). Blocking shared page discard can solve this problem, but it could cause guests to consume twice the memory with VFIO, which is not acceptable in some cases. An alternative solution is to convey other systems like VFIO to refresh its outdated IOMMU mappings.
RamDiscardManager is an existing concept (used by virtio-mem) to adjust VFIO mappings in relation to VM page assignement. Effectively page conversion is similar to hot-removing a page in one mode and adding it back in the other, so the similar work that needs to happen in response to virtio-mem changes needs to happen for page conversion events. Introduce the RamDiscardManager to guest_memfd to achieve it.
However, Implementing the RamDiscardManager interface poses a challenge as guest_memfd is not an object, instead, it is contained within RamBlock and is indicated by a RAM_GUEST_MEMFD flag upon creation.
One option is to implement the interface in HostMemoryBackend. Any guest_memfd-backed host memory backend can register itself in the target MemoryRegion. However, this solution doesn't cover the scenario where a guest_memfd MemoryRegion doesn't belong to the HostMemoryBackend, e.g. the virtual BIOS MemoryRegion.
Thus, implement the second option, which involves defining an object type named guest_memfd_manager with the RamDiscardManager interface. Upon creation of guest_memfd, a new guest_memfd_manager object can be instantiated and registered to the managed guest_memfd MemoryRegion to handle the page conversion events.
In the context of guest_memfd, the discarded state signifies that the page is private, while the populated state indicated that the page is shared. The state of the memory is tracked at the granularity of the host page size (i.e. block_size), as the minimum conversion size can be one page per request. In addition, VFIO expects the DMA mapping for a specific iova to be mapped and unmapped with the same granularity. However, there's no guarantee that the confidential guest won't partially convert the pages. For instance the confidential guest may flip a 2M page from private to shared and later flip the first 4K sub-range from shared to private. To prevent such invalid cases, all operations are performed with a 4K granularity.
--- include/sysemu/guest-memfd-manager.h | 46 +++++ system/guest-memfd-manager.c | 283 +++++++++++++++++++++++++++ system/meson.build | 1 + 3 files changed, 330 insertions(+) create mode 100644 include/sysemu/guest-memfd-manager.h create mode 100644 system/guest-memfd-manager.c
include/sysemu/guest-memfd-manager.h new file +46 -0
@@ -0,0 +1,46 @@ +/* +* QEMU guest memfd manager +* +* Copyright Intel +* +* Author: +* Chenyi Qiang <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory +* +*/ + +#ifndef SYSEMU_GUEST_MEMFD_MANAGER_H +#define SYSEMU_GUEST_MEMFD_MANAGER_H + +#include "sysemu/hostmem.h" + +#define TYPE_GUEST_MEMFD_MANAGER "guest-memfd-manager" + +OBJECT_DECLARE_TYPE(GuestMemfdManager, GuestMemfdManagerClass, GUEST_MEMFD_MANAGER) + +struct GuestMemfdManager { +Object parent; + +/* Managed memory region. */ +MemoryRegion *mr; + +/* bitmap used to track discard (private) memory */ +int32_t discard_bitmap_size; +unsigned long *discard_bitmap; + +/* block size and alignment */ +uint64_t block_size; + +/* listeners to notify on populate/discard activity. */ + QLIST_HEAD(, RamDiscardListener) rdl_list; +}; + +struct GuestMemfdManagerClass { +ObjectClass parent_class; + +void (*realize)(Object *gmm,MemoryRegion *mr,uint64_t region_size); +}; + +#endif
system/guest-memfd-manager.c new file +283 -0
283 lines not on this page · raw message
system/meson.build +1 -0
@@ -16,6 +16,7 @@ system_ss.add(files( 'dirtylimit.c', 'dma-helpers.c', 'globals.c', + 'guest-memfd-manager.c', 'memory_mapping.c', 'qdev-monitor.c', 'qtest.c',
Signature · 1 line
2.43.5
RFC PATCH 2/6 guest_memfd: Introduce a helper to notify the shared/private state change
In reply to Chenyi QiangIntroduce a helper function within RamDiscardManager to efficiently notify all registered RamDiscardListeners, including VFIO listeners about the memory conversion events between shared and private in guest_memfd. The existing VFIO listener can dynamically DMA map/unmap the shared pages based on the conversion type:
- For conversions from shared to private, the VFIO system ensures the discarding of shared mapping from the IOMMU.
- For conversions from private to shared, it triggers the population of the shared mapping into the IOMMU.
Additionally, there could be some special conversion requests:
- When a conversion request is made for a page already in the desired state (either private or shared), the helper simply returns success.
- For requests involving a range partially in the desired state, only the necessary segments are converted, ensuring the entire range complies with the request efficiently.
- In scenarios where a conversion request is declined by other systems, such as a failure from VFIO during notify_populate(), the helper will roll back the request, maintaining consistency.
--- include/sysemu/guest-memfd-manager.h | 3 + system/guest-memfd-manager.c | 141 +++++++++++++++++++++++++++ 2 files changed, 144 insertions(+)
include/sysemu/guest-memfd-manager.h +3 -0
@@ -43,4 +43,7 @@ struct GuestMemfdManagerClass { void (*realize)(Object *gmm, MemoryRegion *mr, uint64_t region_size); }; +int guest_memfd_state_change (GuestMemfdManager *gmm,uint64_t offset,uint64_t size, +bool shared_to_private); +#endif
system/guest-memfd-manager.c +141 -0
147 lines not on this page · raw message
Signature · 1 line
2.43.5
RFC PATCH 3/6 KVM: Notify the state change via RamDiscardManager helper during shared/private conversion
In reply to Chenyi QiangOnce exit to userspace to convert the page from private to shared or vice versa at runtime, notify the state change via the guest_memfd_state_change() helper so that other registered subsystems like VFIO can be notified.
--- accel/kvm/kvm-all.c | 7 +++++++ 1 file changed, 7 insertions(+)
accel/kvm/kvm-all.c +7 -0
@@ -48,6 +48,7 @@#include "kvm-cpus.h" #include "sysemu/dirtylimit.h" #include "qemu/range.h" +#include "sysemu/guest-memfd-manager.h" #include "hw/boards.h" #include "sysemu/stats.h"
@@ -2852,6 +2853,7 @@ int kvm_convert_memory(hwaddr start, hwaddr size, bool to_private)RAMBlock *rb;void *addr;int ret =-1 ; +GuestMemfdManager *gmm;trace_kvm_convert_memory (start, size, to_private ?"shared_to_private" :"private_to_shared" );
@@ -2914,6 +2916,11 @@ int kvm_convert_memory(hwaddr start, hwaddr size, bool to_private) addr =memory_region_get_ram_ptr (mr) + section.offset_within_region; rb =qemu_ram_block_from_host (addr, false, &offset); + gmm =GUEST_MEMFD_MANAGER (mr->rdm); +if (gmm) { +guest_memfd_state_change (gmm, offset, size, to_private); + } +if (to_private) {if (rb->page_size !=qemu_real_host_page_size ()) {/*
Signature · 1 line
2.43.5
RFC PATCH 4/6 memory: Register the RamDiscardManager instance upon guest_memfd creation
In reply to Chenyi QiangInstantiate a new guest_memfd_manager object and register it in the target MemoryRegion. From this point, other subsystems such as VFIO can register their listeners in guest_memfd_manager and receive conversion events through RamDiscardManager.
--- system/physmem.c | 9 +++++++++ 1 file changed, 9 insertions(+)
system/physmem.c +9 -0
@@ -53,6 +53,7 @@#include "sysemu/hostmem.h" #include "sysemu/hw_accel.h" #include "sysemu/xen-mapcache.h" +#include "sysemu/guest-memfd-manager.h" #include "trace/trace-root.h" #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
@@ -1861,6 +1862,12 @@ static void ram_block_add(RAMBlock *new_block, Error **errp)qemu_mutex_unlock_ramlist (); goto out_free; } + +GuestMemfdManager *gmm =GUEST_MEMFD_MANAGER (object_new (TYPE_GUEST_MEMFD_MANAGER )); +GuestMemfdManagerClass *gmmc =GUEST_MEMFD_MANAGER_GET_CLASS (gmm); +g_assert (new_block->mr); + gmmc->realize (OBJECT (gmm), new_block->mr, new_block->mr->size); +memory_region_set_ram_discard_manager (gmm->mr,RAM_DISCARD_MANAGER (gmm)); } new_ram_size = MAX(old_ram_size,
@@ -2118,6 +2125,8 @@ static void reclaim_ramblock(RAMBlock *block)if (block->guest_memfd >=0 ) {close (block->guest_memfd); +g_assert (block->mr); +object_unref (OBJECT (block->mr->rdm));ram_block_discard_require (false); }
Signature · 1 line
2.43.5
RFC PATCH 5/6 guest-memfd: Default to discarded (private) in guest_memfd_manager
In reply to Chenyi Qiangguest_memfd was initially set to shared until the commit bd3bcf6962 ("kvm/memory: Make memory type private by default if it has guest memfd backend"). To align with this change, the default state in guest_memfd_manager is set to discarded.
One concern raised by this commit is the handling of the virtual BIOS. The virtual BIOS loads its image into the shared memory of guest_memfd. However, during the region_commit() stage, the memory attribute is set to private while its shared memory remains valid. This mismatch persists until the shared content is copied to the private region. Fortunately, this interval only exits during setup stage and currently, only the guest_memfd_manager is concerned with the state of the guest_memfd at that stage. For simplicity, the default bitmap in guest_memfd_manager is set to discarded (private). This is feasible because the shared content of the virtual BIOS will eventually be discarded and there are no requests to DMA access to this shared part during this period.
Additionally, setting the default to private can also reduce the overhead of mapping shared pages into IOMMU by VFIO at the bootup stage.
--- system/guest-memfd-manager.c | 1 + 1 file changed, 1 insertion(+)
system/guest-memfd-manager.c +1 -0
@@ -393,6 +393,7 @@ static void guest_memfd_manager_realize(Object *obj, MemoryRegion *mr, gmm->mr = mr; gmm->discard_bitmap_size = bitmap_size; gmm->discard_bitmap =bitmap_new (bitmap_size); +bitmap_fill (gmm->discard_bitmap, bitmap_size); }static void guest_memfd_manager_init (Object *obj)
Signature · 1 line
2.43.5
RFC PATCH 6/6 RAMBlock: make guest_memfd require coordinate discard
In reply to Chenyi QiangAs guest_memfd is now managed by guest_memfd_manager with RamDiscardManager, only block uncoordinated discard.
--- system/physmem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
system/physmem.c +1 -1
@@ -1849,7 +1849,7 @@ static void ram_block_add(RAMBlock *new_block, Error **errp)assert (kvm_enabled ());assert (new_block->guest_memfd <0 ); -if (ram_block_discard_require (true) <0 ) { +if (ram_block_coordinated_discard_require (true) <0 ) {error_setg_errno (errp, errno,"cannot set up private guest memory: discard currently blocked" );error_append_hint (errp,"Are you using assigned devices?\n" );
Signature · 1 line
2.43.5
Quoted 4 lines
Open
====
Implementing a RamDiscardManager to notify VFIO of page conversions causes changes in semantics: private memory is treated as discarded (or
Quoted 8 lines
hot-removed) memory. This isn't aligned with the expectation of current RamDiscardManager users (e.g. VFIO or live migration) who really expect that discarded memory is hot-removed and thus can be skipped when the users are processing guest memory. Treating private memory as discarded won't work in future if VFIO or live migration needs to handle private memory. e.g. VFIO may need to map private memory to support Trusted IO and live migration for confidential VMs need to migrate private memory.
"VFIO may need to map private memory to support Trusted IO"
I've been told that the way we handle shared memory won't be the way this is going to work with guest_memfd. KVM will coordinate directly with VFIO or $whatever and update the IOMMU tables itself right in the kernel; the pages are pinned/owned by guest_memfd, so that will just work. So I don't consider that currently a concern. guest_memfd private memory is not mapped into user page tables and as it currently seems it never will be.
Similarly: live migration. We cannot simply migrate that memory the traditional way. We even have to track the dirty state differently.
So IMHO, treating both memory as discarded == don't touch it the usual way might actually be a feature not a bug ;)
Quoted 9 lines
There are two possible ways to mitigate the semantics changes.
1. Develop a new mechanism to notify the page conversions between private and shared. For example, utilize thenotifier_listin QEMU. VFIO registers its own handler and gets notified upon page conversions. This is a clean approach which only touches the notifier workflow. A challenge is that for device hotplug, existing shared memory should be mapped in IOMMU. This will need additional changes.
Quoted 9 lines
2. Extend the existing RamDiscardManager interface to manage not only the discarded/populated status of guest memory but also the shared/private status. RamDiscardManager users like VFIO will be notified with one more argument indicating what change is happening and can take action accordingly. It also has challenges e.g. QEMU allows only one RamDiscardManager, how to support virtio-mem for confidential VMs would be a problem. And some APIs like .is_populated() exposed by RamDiscardManager are meaningless to shared/private memory. So they may need some adjustments.
Think of all of that in terms of "shared memory is populated, private memory is some inaccessible stuff that needs very special way and other means for device assignment, live migration, etc.". Then it actually quite makes sense to use of RamDiscardManager (AFAIKS :) ).
Quoted 8 lines
Testing
=======
This patch series is tested based on the internal TDX KVM/QEMU tree.To facilitate shared device assignment with the NIC, employ the legacy type1 VFIO with the QEMU command:
Quoted 6 lines
qemu-system-x86_64 [...]
-device vfio-pci,host=XX:XX.XThe parameter of
dma_entry_limitneeds to be adjusted. For example, a 16GB guest needs to adjust the parameter like vfio_iommu_type1.dma_entry_limit=4194304.
But here you note the biggest real issue I see (not related to RAMDiscardManager, but that we have to prepare for conversion of each possible private page to shared and back): we need a single IOMMU mapping for each 4 KiB page.
Doesn't that mean that we limit shared memory to 4194304*4096 == 16 GiB. Does it even scale then?
There is the alternative of having in-place private/shared conversion when we also let guest_memfd manage some shared memory. It has plenty of downsides, but for the problem at hand it would mean that we don't discard on shared/private conversion.
But whenever we want to convert memory shared->private we would similarly have to from IOMMU page tables via VFIO. (the in-place conversion will only be allowed if any additional references on a page are gone -- when it is inaccessible by userspace/kernel).
Again, if IOMMU page tables would be managed by KVM in the kernel without user space intervention/vfio this would work with device assignment just fine. But I guess it will take a while until we actually have that option.
Signature · 3 lines
Cheers, David / dhildenb
Quoted 19 lines
From: David Hildenbrand <[email protected]>
Sent: Thursday, July 25, 2024 10:04 PMQuoted 7 lines
Open
====
Implementing a RamDiscardManager to notify VFIO of page conversions causes changes in semantics: private memory is treated as discarded (or hot-removed) memory. This isn't aligned with the expectation of current RamDiscardManager users (e.g. VFIO or live migration) who really expect that discarded memory is hot-removed and thus can be skippedwhen
Quoted 5 lines
the users are processing guest memory. Treating private memory as discarded won't work in future if VFIO or live migration needs to handle private memory. e.g. VFIO may need to map private memory to support Trusted IO and live migration for confidential VMs need to migrate private memory.
"VFIO may need to map private memory to support Trusted IO"
Quoted 7 lines
I've been told that the way we handle shared memory won't be the way this is going to work with
guest_memfd. KVM will coordinate directly with VFIO or $whatever and update the IOMMU tables itself right in the kernel; the pages are pinned/owned byguest_memfd, so that will just work. So I don't consider that currently a concern.guest_memfdprivate memory is not mapped into user page tables and as it currently seems it never will be.
Or could extend MAP_DMA to accept guest_memfd+offset in place of 'vaddr' and have VFIO/IOMMUFD call guest_memfd helpers to retrieve the pinned pfn.
IMHO it's more the TIO arch deciding whether VFIO/IOMMUFD needs to manage the mapping of the private memory instead of the use of guest_memfd.
e.g. SEV-TIO, iiuc, introduces a new-layer page ownership tracker (RMP) to check the HPA after the IOMMU walks the existing I/O page tables. So reasonably VFIO/IOMMUFD could continue to manage those I/O page tables including both private and shared memory, with a hint to know where to find the pfn (host page table or guest_memfd).
But TDX Connect introduces a new I/O page table format (same as secure EPT) for mapping the private memory and further requires sharing the secure-EPT between CPU/IOMMU for private. Then it appears to be a different story.
Quoted 27 lines · Kevin
Quoted 19 lines
From: David Hildenbrand <[email protected]>
Sent: Thursday, July 25, 2024 10:04 PMQuoted 7 lines
Open
====
Implementing a RamDiscardManager to notify VFIO of page conversions causes changes in semantics: private memory is treated as discarded (or hot-removed) memory. This isn't aligned with the expectation of current RamDiscardManager users (e.g. VFIO or live migration) who really expect that discarded memory is hot-removed and thus can be skippedwhen
Quoted 5 lines
the users are processing guest memory. Treating private memory as discarded won't work in future if VFIO or live migration needs to handle private memory. e.g. VFIO may need to map private memory to support Trusted IO and live migration for confidential VMs need to migrate private memory.
"VFIO may need to map private memory to support Trusted IO"
Quoted 7 lines
I've been told that the way we handle shared memory won't be the way this is going to work with
guest_memfd. KVM will coordinate directly with VFIO or $whatever and update the IOMMU tables itself right in the kernel; the pages are pinned/owned byguest_memfd, so that will just work. So I don't consider that currently a concern.guest_memfdprivate memory is not mapped into user page tables and as it currently seems it never will be.
Quoted 3 lines
Or could extend
MAP_DMAto accept guest_memfd+offset in place of 'vaddr' and have VFIO/IOMMUFD callguest_memfdhelpers to retrieve the pinned pfn.
In theory yes, and I've been thinking of the same for a while. Until people told me that it is unlikely that it will work that way in the future.
Quoted 11 lines
IMHO it's more the TIO arch deciding whether VFIO/IOMMUFD needs to manage the mapping of the private memory instead of the use of
guest_memfd.e.g. SEV-TIO, iiuc, introduces a new-layer page ownership tracker (RMP) to check the HPA after the IOMMU walks the existing I/O page tables. So reasonably VFIO/IOMMUFD could continue to manage those I/O page tables including both private and shared memory, with a hint to know where to find the pfn (host page table or
guest_memfd).
Quoted 4 lines
But TDX Connect introduces a new I/O page table format (same as secure EPT) for mapping the private memory and further requires sharing the secure-EPT between CPU/IOMMU for private. Then it appears to be a different story.
Yes. This seems to be the future and more in-line with in-place/in-kernel conversion as e.g., pKVM wants to have it. If you want to avoid user space altogether when doing shared<->private conversions, then letting user space manage the IOMMUs is not going to work.
If we ever have to go down that path (MAP_DMA of guest_memfd), we could have two RAMDiscardManager for a RAM region, just like we have two memory backends: one for shared memory populate/discard (what this series tries to achieve), one for private memory populate/discard.
The thing is, that private memory will always have to be special-cased all over the place either way, unfortunately.
Signature · 3 lines
Cheers, David / dhildenb
Quoted 28 lines · David Hildenbrand
Quoted 27 lines · Kevin
Quoted 26 lines
From: David Hildenbrand <[email protected]>
Sent: Thursday, July 25, 2024 10:04 PMQuoted 7 lines
Open
====
Implementing a RamDiscardManager to notify VFIO of page conversions causes changes in semantics: private memory is treated as discarded (or hot-removed) memory. This isn't aligned with the expectation of current RamDiscardManager users (e.g. VFIO or live migration) who really expect that discarded memory is hot-removed and thus can be skippedwhen
Quoted 5 lines
the users are processing guest memory. Treating private memory as discarded won't work in future if VFIO or live migration needs to handle private memory. e.g. VFIO may need to map private memory to support Trusted IO and live migration for confidential VMs need to migrate private memory.
"VFIO may need to map private memory to support Trusted IO"
I've been told that the way we handle shared memory won't be the way this is going to work with
guest_memfd. KVM will coordinate directly with VFIO or $whatever and update the IOMMU tables itself right in the kernel; the pages are pinned/owned byguest_memfd, so that will just work. So I don't consider that currently a concern.guest_memfdprivate memory is not mapped into user page tables and as it currently seems it never will be.
Quoted 1 line
Quoted 1 line
Or could extend
MAP_DMAto accept guest_memfd+offset in place of
With TIO, I can imagine several buffer sharing requirements: KVM maps VFIO owned private MMIO, IOMMU maps gmem owned private memory, IOMMU maps VFIO owned private MMIO. These buffers cannot be found by user page table anymore. I'm wondering it would be messy to have specific PFN finding methods for each FD type. Is it possible we have a unified way for buffer sharing and PFN finding, is dma-buf a candidate?
Quoted 5 lines
Quoted 2 lines
'vaddr' and have VFIO/IOMMUFD call
guest_memfdhelpers to retrieve the pinned pfn.In theory yes, and I've been thinking of the same for a while. Until people told me that it is unlikely that it will work that way in the future.
Could you help specify why it won't work? As Kevin mentioned below, SEV-TIO may still allow userspace to manage the IOMMU mapping for private. I'm not sure how they map private memory for IOMMU without touching gmemfd.
Thanks,
Yilun
Quoted 37 lines
Quoted 12 lines
IMHO it's more the TIO arch deciding whether VFIO/IOMMUFD needs to manage the mapping of the private memory instead of the use of
guest_memfd.e.g. SEV-TIO, iiuc, introduces a new-layer page ownership tracker (RMP) to check the HPA after the IOMMU walks the existing I/O page tables. So reasonably VFIO/IOMMUFD could continue to manage those I/O page tables including both private and shared memory, with a hint to know where to find the pfn (host page table or
guest_memfd).Quoted 4 lines
But TDX Connect introduces a new I/O page table format (same as secure EPT) for mapping the private memory and further requires sharing the secure-EPT between CPU/IOMMU for private. Then it appears to be a different story.
Yes. This seems to be the future and more in-line with in-place/in-kernel conversion as e.g., pKVM wants to have it. If you want to avoid user space altogether when doing shared<->private conversions, then letting user space manage the IOMMUs is not going to work.
If we ever have to go down that path (
MAP_DMAofguest_memfd), we could have two RAMDiscardManager for a RAM region, just like we have two memory
backends: one for shared memory populate/discard (what this series tries to achieve), one for private memory populate/discard.The thing is, that private memory will always have to be special-cased all over the place either way, unfortunately.
Signature · 3 lines
Cheers, David / dhildenb
Quoted 29 lines · Xu Yilun
Quoted 28 lines · David Hildenbrand
Quoted 27 lines · Kevin
Quoted 26 lines
From: David Hildenbrand <[email protected]>
Sent: Thursday, July 25, 2024 10:04 PMQuoted 7 lines
Open
====
Implementing a RamDiscardManager to notify VFIO of page conversions causes changes in semantics: private memory is treated as discarded (or hot-removed) memory. This isn't aligned with the expectation of current RamDiscardManager users (e.g. VFIO or live migration) who really expect that discarded memory is hot-removed and thus can be skippedwhen
Quoted 5 lines
the users are processing guest memory. Treating private memory as discarded won't work in future if VFIO or live migration needs to handle private memory. e.g. VFIO may need to map private memory to support Trusted IO and live migration for confidential VMs need to migrate private memory.
"VFIO may need to map private memory to support Trusted IO"
I've been told that the way we handle shared memory won't be the way this is going to work with
guest_memfd. KVM will coordinate directly with VFIO or $whatever and update the IOMMU tables itself right in the kernel; the pages are pinned/owned byguest_memfd, so that will just work. So I don't consider that currently a concern.guest_memfdprivate memory is not mapped into user page tables and as it currently seems it never will be.
Quoted 8 lines
Quoted 1 line
Quoted 1 line
Or could extend
MAP_DMAto accept guest_memfd+offset in place ofWith TIO, I can imagine several buffer sharing requirements: KVM maps VFIO owned private MMIO, IOMMU maps gmem owned private memory, IOMMU maps VFIO owned private MMIO. These buffers cannot be found by user page table anymore. I'm wondering it would be messy to have specific PFN finding methods for each FD type. Is it possible we have a unified way for buffer sharing and PFN finding, is dma-buf a candidate?
No expert on that, so I'm afraid I can't help.
Quoted 10 lines
Quoted 6 lines
Quoted 3 lines
'vaddr' and have VFIO/IOMMUFD call
guest_memfdhelpers to retrieve the pinned pfn.In theory yes, and I've been thinking of the same for a while. Until people told me that it is unlikely that it will work that way in the future.
Could you help specify why it won't work? As Kevin mentioned below, SEV-TIO may still allow userspace to manage the IOMMU mapping for private. I'm not sure how they map private memory for IOMMU without touching gmemfd.
I raised that question in [1]:
"How would the device be able to grab/access "private memory", if not via the user page tables?"
Jason summarized it as "The approaches I'm aware of require the secure world to own the IOMMU and generate the IOMMU page tables. So we will not use a GUP approach with VFIO today as the kernel will not have any reason to generate a page table in the first place. Instead we will say "this PCI device translates through the secure world" and walk away."
I think for some cVM approaches it really cannot work without letting KVM/secure world handle the IOMMU (e.g., sharing of page tables between IOMMU and KVM).
For your use case it *might* work, but I am wondering if this is how it should be done, and if there are better alternatives.
[1] https://lkml.org/lkml/2024/6/20/920
Signature · 3 lines
Cheers, David / dhildenb
Quoted 15 lines · David Hildenbrand
Quoted 4 lines
Open
====
Implementing a RamDiscardManager to notify VFIO of page conversions causes changes in semantics: private memory is treated as discarded (orQuoted 8 lines
hot-removed) memory. This isn't aligned with the expectation of current RamDiscardManager users (e.g. VFIO or live migration) who really expect that discarded memory is hot-removed and thus can be skipped when the users are processing guest memory. Treating private memory as discarded won't work in future if VFIO or live migration needs to handle private memory. e.g. VFIO may need to map private memory to support Trusted IO and live migration for confidential VMs need to migrate private memory.
"VFIO may need to map private memory to support Trusted IO"
Quoted 7 lines
I've been told that the way we handle shared memory won't be the way this is going to work with
guest_memfd. KVM will coordinate directly with VFIO or $whatever and update the IOMMU tables itself right in the kernel; the pages are pinned/owned byguest_memfd, so that will just work. So I don't consider that currently a concern.guest_memfdprivate memory is not mapped into user page tables and as it currently seems it never will be.
That's correct. AFAIK, some TEE IO solution like TDX Connect would let kernel coordinate and update private mapping in IOMMU tables. Here, It mentions that VFIO "may" need map private memory. I want to make this more generic to account for potential future TEE IO solutions that may require such functionality. :)
Quoted 6 lines
Similarly: live migration. We cannot simply migrate that memory the traditional way. We even have to track the dirty state differently.
So IMHO, treating both memory as discarded == don't touch it the usual way might actually be a feature not a bug ;)
Do you mean treating the private memory in both VFIO and live migration as discarded? That is what this patch series does. And as you mentioned, these RDM users cannot follow the traditional RDM way. Because of this, we also considered whether we should use RDM or a more generic mechanism like notifier_list below.
Quoted 20 lines
Quoted 10 lines
There are two possible ways to mitigate the semantics changes.
1. Develop a new mechanism to notify the page conversions between private and shared. For example, utilize thenotifier_listin QEMU. VFIO registers its own handler and gets notified upon page conversions. This is a clean approach which only touches the notifier workflow. A challenge is that for device hotplug, existing shared memory should be mapped in IOMMU. This will need additional changes.Quoted 9 lines
2. Extend the existing RamDiscardManager interface to manage not only the discarded/populated status of guest memory but also the shared/private status. RamDiscardManager users like VFIO will be notified with one more argument indicating what change is happening and can take action accordingly. It also has challenges e.g. QEMU allows only one RamDiscardManager, how to support virtio-mem for confidential VMs would be a problem. And some APIs like .is_populated() exposed by RamDiscardManager are meaningless to shared/private memory. So they may need some adjustments.
Quoted 4 lines
Think of all of that in terms of "shared memory is populated, private memory is some inaccessible stuff that needs very special way and other means for device assignment, live migration, etc.". Then it actually quite makes sense to use of RamDiscardManager (AFAIKS :) ).
Yes, such notification mechanism is what we want. But for the users of RDM, it would require additional change accordingly. Current users just skip inaccessible stuff, but in private memory case, it can't be simply skipped. Maybe renaming RamDiscardManager to RamStateManager is more accurate then. :)
Quoted 16 lines
Quoted 9 lines
Testing
=======
This patch series is tested based on the internal TDX KVM/QEMU tree.To facilitate shared device assignment with the NIC, employ the legacy type1 VFIO with the QEMU command:
Quoted 6 lines
qemu-system-x86_64 [...]
-device vfio-pci,host=XX:XX.XThe parameter of
dma_entry_limitneeds to be adjusted. For example, a 16GB guest needs to adjust the parameter like vfio_iommu_type1.dma_entry_limit=4194304.
Quoted 7 lines
But here you note the biggest real issue I see (not related to RAMDiscardManager, but that we have to prepare for conversion of each possible private page to shared and back): we need a single IOMMU mapping for each 4 KiB page.
Doesn't that mean that we limit shared memory to 4194304*4096 == 16 GiB. Does it even scale then?
The entry limitation needs to be increased as the guest memory size increases. For this issue, are you concerned that having too many entries might bring some performance issue? Maybe we could introduce some PV mechanism to coordinate with guest to convert memory only in 2M granularity. This may help mitigate the problem.
Quoted 10 lines
There is the alternative of having in-place private/shared conversion when we also let
guest_memfdmanage some shared memory. It has plenty of downsides, but for the problem at hand it would mean that we don't discard on shared/private conversion.>
But whenever we want to convert memoryshared->privatewe would similarly have to from IOMMU page tables via VFIO. (the in-place conversion will only be allowed if any additional references on a page are gone -- when it is inaccessible by userspace/kernel).
I'm not clear about this in-place private/shared conversion. Can you elaborate a little bit? It seems this alternative changes private and shared management in current guest_memfd?
Quoted 6 lines
Again, if IOMMU page tables would be managed by KVM in the kernel without user space intervention/vfio this would work with device assignment just fine. But I guess it will take a while until we actually have that option.
Quoted 26 lines · Chenyi Qiang
Quoted 15 lines · David Hildenbrand
Quoted 4 lines
Open
====
Implementing a RamDiscardManager to notify VFIO of page conversions causes changes in semantics: private memory is treated as discarded (orQuoted 8 lines
hot-removed) memory. This isn't aligned with the expectation of current RamDiscardManager users (e.g. VFIO or live migration) who really expect that discarded memory is hot-removed and thus can be skipped when the users are processing guest memory. Treating private memory as discarded won't work in future if VFIO or live migration needs to handle private memory. e.g. VFIO may need to map private memory to support Trusted IO and live migration for confidential VMs need to migrate private memory.
"VFIO may need to map private memory to support Trusted IO"
Quoted 7 lines
I've been told that the way we handle shared memory won't be the way this is going to work with
guest_memfd. KVM will coordinate directly with VFIO or $whatever and update the IOMMU tables itself right in the kernel; the pages are pinned/owned byguest_memfd, so that will just work. So I don't consider that currently a concern.guest_memfdprivate memory is not mapped into user page tables and as it currently seems it never will be.
Quoted 5 lines
That's correct. AFAIK, some TEE IO solution like TDX Connect would let kernel coordinate and update private mapping in IOMMU tables. Here, It mentions that VFIO "may" need map private memory. I want to make this more generic to account for potential future TEE IO solutions that may require such functionality. :)
Careful to not over-enginner something that is not even real or close-to-be-real yet, though. :) Nobody really knows who that will look like, besides that we know for Intel that we won't need that.
Quoted 5 lines
Quoted 5 lines
Similarly: live migration. We cannot simply migrate that memory the traditional way. We even have to track the dirty state differently.
Quoted 8 lines
Quoted 2 lines
So IMHO, treating both memory as discarded == don't touch it the usual way might actually be a feature not a bug ;)
Do you mean treating the private memory in both VFIO and live migration as discarded? That is what this patch series does. And as you mentioned, these RDM users cannot follow the traditional RDM way. Because of this, we also considered whether we should use RDM or a more generic mechanism like
notifier_listbelow.
Yes, the shared memory is logically discarded. At the same time we *might* get private memory effectively populated. See my reply to Kevin that there might be ways of having shared vs. private populate/discard in the future, if required. Just some idea, though.
Quoted 26 lines
Quoted 21 lines
Quoted 11 lines
There are two possible ways to mitigate the semantics changes.
1. Develop a new mechanism to notify the page conversions between private and shared. For example, utilize thenotifier_listin QEMU. VFIO registers its own handler and gets notified upon page conversions. This is a clean approach which only touches the notifier workflow. A challenge is that for device hotplug, existing shared memory should be mapped in IOMMU. This will need additional changes.Quoted 9 lines
2. Extend the existing RamDiscardManager interface to manage not only the discarded/populated status of guest memory but also the shared/private status. RamDiscardManager users like VFIO will be notified with one more argument indicating what change is happening and can take action accordingly. It also has challenges e.g. QEMU allows only one RamDiscardManager, how to support virtio-mem for confidential VMs would be a problem. And some APIs like .is_populated() exposed by RamDiscardManager are meaningless to shared/private memory. So they may need some adjustments.
Quoted 4 lines
Think of all of that in terms of "shared memory is populated, private memory is some inaccessible stuff that needs very special way and other means for device assignment, live migration, etc.". Then it actually quite makes sense to use of RamDiscardManager (AFAIKS :) ).
Quoted 5 lines
Yes, such notification mechanism is what we want. But for the users of RDM, it would require additional change accordingly. Current users just skip inaccessible stuff, but in private memory case, it can't be simply skipped. Maybe renaming RamDiscardManager to RamStateManager is more accurate then. :)
Current users must skip it, yes. How private memory would have to be handled, and who would handle it, is rather unclear.
Again, maybe we'd want separate RamDiscardManager for private and shared memory (after all, these are two separate memory backends).
Not sure that "RamStateManager" terminology would be reasonable in that approach.
Quoted 22 lines
Quoted 22 lines
Quoted 10 lines
Testing
=======
This patch series is tested based on the internal TDX KVM/QEMU tree.To facilitate shared device assignment with the NIC, employ the legacy type1 VFIO with the QEMU command:
Quoted 6 lines
qemu-system-x86_64 [...]
-device vfio-pci,host=XX:XX.XThe parameter of
dma_entry_limitneeds to be adjusted. For example, a 16GB guest needs to adjust the parameter like vfio_iommu_type1.dma_entry_limit=4194304.But here you note the biggest real issue I see (not related to RAMDiscardManager, but that we have to prepare for conversion of each possible private page to shared and back): we need a single IOMMU mapping for each 4 KiB page.
Quoted 8 lines
Quoted 2 lines
Doesn't that mean that we limit shared memory to 4194304*4096 == 16 GiB. Does it even scale then?
The entry limitation needs to be increased as the guest memory size increases. For this issue, are you concerned that having too many entries might bring some performance issue? Maybe we could introduce some PV mechanism to coordinate with guest to convert memory only in 2M granularity. This may help mitigate the problem.
I've had this talk with Intel, because the 4K granularity is a pain. I was told that ship has sailed ... and we have to cope with random 4K conversions :(
The many mappings will likely add both memory and runtime overheads in the kernel. But we only know once we measure.
Key point is that even 4194304 "only" allows for 16 GiB. Imagine 1 TiB of shared memory :/
Quoted 12 lines
Quoted 11 lines
There is the alternative of having in-place private/shared conversion when we also let
guest_memfdmanage some shared memory. It has plenty of downsides, but for the problem at hand it would mean that we don't discard on shared/private conversion.>
But whenever we want to convert memoryshared->privatewe would similarly have to from IOMMU page tables via VFIO. (the in-place conversion will only be allowed if any additional references on a page are gone -- when it is inaccessible by userspace/kernel).
Quoted 3 lines
I'm not clear about this in-place private/shared conversion. Can you elaborate a little bit? It seems this alternative changes private and shared management in current
guest_memfd?
Yes, there have been discussions about that, also in the context of supporting huge pages while allowing for the guest to still convert individual 4K chunks ...
A summary is here [1]. Likely more things will be covered at Linux Plumbers.
[1] https://lore.kernel.org/kvm/[email protected]/
Signature · 3 lines
Cheers, David / dhildenb
Quoted 35 lines · David Hildenbrand
Quoted 28 lines · Chenyi Qiang
Quoted 17 lines · David Hildenbrand
Quoted 6 lines
Open
====
Implementing a RamDiscardManager to notify VFIO of page conversions causes changes in semantics: private memory is treated as discarded (or hot-removed) memory. This isn't aligned with the expectation of current RamDiscardManager users (e.g. VFIO or live migration) who reallyQuoted 8 lines
expect that discarded memory is hot-removed and thus can be skipped when
the users are processing guest memory. Treating private memory as discarded won't work in future if VFIO or live migration needs to handle
private memory. e.g. VFIO may need to map private memory to support Trusted IO and live migration for confidential VMs need to migrate private memory."VFIO may need to map private memory to support Trusted IO"
Quoted 7 lines
I've been told that the way we handle shared memory won't be the way this is going to work with
guest_memfd. KVM will coordinate directly with VFIO or $whatever and update the IOMMU tables itself right in the kernel; the pages are pinned/owned byguest_memfd, so that will just work. So I don't consider that currently a concern.guest_memfdprivate memory is not mapped into user page tables and as it currently seems it never will be.Quoted 5 lines
That's correct. AFAIK, some TEE IO solution like TDX Connect would let kernel coordinate and update private mapping in IOMMU tables. Here, It mentions that VFIO "may" need map private memory. I want to make this more generic to account for potential future TEE IO solutions that may require such functionality. :)
Quoted 3 lines
Careful to not over-enginner something that is not even real or close-to-be-real yet, though. :) Nobody really knows who that will look like, besides that we know for Intel that we won't need that.
OK, Thanks for the reminder!
Quoted 53 lines
Quoted 6 lines
Quoted 6 lines
Similarly: live migration. We cannot simply migrate that memory the traditional way. We even have to track the dirty state differently.
Quoted 8 lines
Quoted 2 lines
So IMHO, treating both memory as discarded == don't touch it the usual way might actually be a feature not a bug ;)
Do you mean treating the private memory in both VFIO and live migration as discarded? That is what this patch series does. And as you mentioned, these RDM users cannot follow the traditional RDM way. Because of this, we also considered whether we should use RDM or a more generic mechanism like
notifier_listbelow.Yes, the shared memory is logically discarded. At the same time we *might* get private memory effectively populated. See my reply to Kevin that there might be ways of having shared vs. private populate/discard in the future, if required. Just some idea, though.
Quoted 27 lines
Quoted 22 lines
Quoted 12 lines
There are two possible ways to mitigate the semantics changes.
1. Develop a new mechanism to notify the page conversions between private and shared. For example, utilize thenotifier_listin QEMU. VFIO
registers its own handler and gets notified upon page conversions. This is a clean approach which only touches the notifier workflow. A challenge is that for device hotplug, existing shared memory should be mapped in IOMMU. This will need additional changes.Quoted 9 lines
2. Extend the existing RamDiscardManager interface to manage not only the discarded/populated status of guest memory but also the shared/private status. RamDiscardManager users like VFIO will be notified with one more argument indicating what change is happening and can take action accordingly. It also has challenges e.g. QEMU allows only one RamDiscardManager, how to support virtio-mem for confidential VMs would be a problem. And some APIs like .is_populated() exposed by RamDiscardManager are meaningless to shared/private memory. So they may need some adjustments.
Quoted 4 lines
Think of all of that in terms of "shared memory is populated, private memory is some inaccessible stuff that needs very special way and other means for device assignment, live migration, etc.". Then it actually quite makes sense to use of RamDiscardManager (AFAIKS :) ).
Quoted 5 lines
Yes, such notification mechanism is what we want. But for the users of RDM, it would require additional change accordingly. Current users just skip inaccessible stuff, but in private memory case, it can't be simply skipped. Maybe renaming RamDiscardManager to RamStateManager is more accurate then. :)
Quoted 5 lines
Current users must skip it, yes. How private memory would have to be handled, and who would handle it, is rather unclear.
Again, maybe we'd want separate RamDiscardManager for private and shared memory (after all, these are two separate memory backends).
We also considered distinguishing the populate and discard operation for private and shared memory separately. As in method 2 above, we mentioned to add a new argument to indicate the memory attribute to operate on. They seem to have a similar idea.
Quoted 35 lines
Not sure that "RamStateManager" terminology would be reasonable in that approach.
Quoted 22 lines
Quoted 22 lines
Quoted 10 lines
Testing
=======
This patch series is tested based on the internal TDX KVM/QEMU tree.To facilitate shared device assignment with the NIC, employ the legacy type1 VFIO with the QEMU command:
Quoted 6 lines
qemu-system-x86_64 [...]
-device vfio-pci,host=XX:XX.XThe parameter of
dma_entry_limitneeds to be adjusted. For example, a 16GB guest needs to adjust the parameter like vfio_iommu_type1.dma_entry_limit=4194304.But here you note the biggest real issue I see (not related to RAMDiscardManager, but that we have to prepare for conversion of each possible private page to shared and back): we need a single IOMMU mapping for each 4 KiB page.
Quoted 8 lines
Quoted 2 lines
Doesn't that mean that we limit shared memory to 4194304*4096 == 16 GiB. Does it even scale then?
The entry limitation needs to be increased as the guest memory size increases. For this issue, are you concerned that having too many entries might bring some performance issue? Maybe we could introduce some PV mechanism to coordinate with guest to convert memory only in 2M granularity. This may help mitigate the problem.
Quoted 6 lines
I've had this talk with Intel, because the 4K granularity is a pain. I was told that ship has sailed ... and we have to cope with random 4K conversions :(
The many mappings will likely add both memory and runtime overheads in the kernel. But we only know once we measure.
In the normal case, the main runtime overhead comes from private<->shared flip in SWIOTLB, which defaults to 6% of memory with a maximum of 1Gbyte. I think this overhead is acceptable. In non-default case, e.g. dynamic allocated DMA buffer, the runtime overhead will increase. As for the memory overheads, It is indeed unavoidable.
Will these performance issues be a deal breaker for enabling shared device assignment in this way?
Quoted 24 lines
Key point is that even 4194304 "only" allows for 16 GiB. Imagine 1 TiB of shared memory :/
Quoted 12 lines
Quoted 11 lines
There is the alternative of having in-place private/shared conversion when we also let
guest_memfdmanage some shared memory. It has plenty of downsides, but for the problem at hand it would mean that we don't discard on shared/private conversion.>
But whenever we want to convert memoryshared->privatewe would similarly have to from IOMMU page tables via VFIO. (the in-place conversion will only be allowed if any additional references on a page are gone -- when it is inaccessible by userspace/kernel).Quoted 3 lines
I'm not clear about this in-place private/shared conversion. Can you elaborate a little bit? It seems this alternative changes private and shared management in current
guest_memfd?Yes, there have been discussions about that, also in the context of supporting huge pages while allowing for the guest to still convert individual 4K chunks ...
Quoted 7 lines
A summary is here [1]. Likely more things will be covered at Linux Plumbers.
Thanks for your sharing.
Sorry for the late reply!
Quoted 10 lines
Quoted 5 lines
Current users must skip it, yes. How private memory would have to be handled, and who would handle it, is rather unclear.
Again, maybe we'd want separate RamDiscardManager for private and shared memory (after all, these are two separate memory backends).
We also considered distinguishing the populate and discard operation for private and shared memory separately. As in method 2 above, we mentioned to add a new argument to indicate the memory attribute to operate on. They seem to have a similar idea.
Yes. Likely it's just some implementation detail. I think the following states would be possible:
* Discarded in shared + discarded in private (not populated)
* Discarded in shared + populated in private (private populated)
* Populated in shared + discarded in private (shared populated)
One could map these to states discarded/private/shared indeed.
[...]
Quoted 7 lines
Quoted 6 lines
I've had this talk with Intel, because the 4K granularity is a pain. I was told that ship has sailed ... and we have to cope with random 4K conversions :(
The many mappings will likely add both memory and runtime overheads in the kernel. But we only know once we measure.
Quoted 8 lines
In the normal case, the main runtime overhead comes from private<->shared flip in SWIOTLB, which defaults to 6% of memory with a maximum of 1Gbyte. I think this overhead is acceptable. In non-default case, e.g. dynamic allocated DMA buffer, the runtime overhead will increase. As for the memory overheads, It is indeed unavoidable.
Will these performance issues be a deal breaker for enabling shared device assignment in this way?
I see the most problematic part being the dma_entry_limit and all of these individual MAP/UNMAP calls on 4KiB granularity.
dma_entry_limit is "unsigned int", and defaults to U16_MAX. So the possible maximum should be 4294967296, and the default is 65535.
So we should be able to have a maximum of 16 TiB shared memory all in 4KiB chunks.
sizeof(struct vfio_dma) is probably something like <= 96 bytes, implying a per-page overhead of ~2.4%, excluding the actual rbtree.
Tree lookup/modifications with that many nodes might also get a bit slower, but likely still tolerable as you note.
Deal breaker? Not sure. Rather "suboptimal" :) ... but maybe unavoidable for your use case?
Signature · 3 lines
Cheers, David / dhildenb
Quoted 13 lines · David Hildenbrand
Sorry for the late reply!
Quoted 10 lines
Quoted 5 lines
Current users must skip it, yes. How private memory would have to be handled, and who would handle it, is rather unclear.
Again, maybe we'd want separate RamDiscardManager for private and shared memory (after all, these are two separate memory backends).
We also considered distinguishing the populate and discard operation for private and shared memory separately. As in method 2 above, we mentioned to add a new argument to indicate the memory attribute to operate on. They seem to have a similar idea.
Quoted 8 lines
Yes. Likely it's just some implementation detail. I think the following states would be possible:
* Discarded in shared + discarded in private (not populated)
* Discarded in shared + populated in private (private populated)
* Populated in shared + discarded in private (shared populated)One could map these to states discarded/private/shared indeed.
Make sense. We can follow this if the mechanism of RamDiscardManager is acceptable and no other concerns.
Quoted 28 lines
[...]
Quoted 7 lines
Quoted 6 lines
I've had this talk with Intel, because the 4K granularity is a pain. I was told that ship has sailed ... and we have to cope with random 4K conversions :(
The many mappings will likely add both memory and runtime overheads in the kernel. But we only know once we measure.
Quoted 8 lines
In the normal case, the main runtime overhead comes from private<->shared flip in SWIOTLB, which defaults to 6% of memory with a maximum of 1Gbyte. I think this overhead is acceptable. In non-default case, e.g. dynamic allocated DMA buffer, the runtime overhead will increase. As for the memory overheads, It is indeed unavoidable.
Will these performance issues be a deal breaker for enabling shared device assignment in this way?
I see the most problematic part being the
dma_entry_limitand all of these individual MAP/UNMAP calls on 4KiB granularity.
dma_entry_limitis "unsigned int", and defaults toU16_MAX. So the possible maximum should be 4294967296, and the default is 65535.So we should be able to have a maximum of 16 TiB shared memory all in 4KiB chunks.
Quoted 8 lines
sizeof(struct vfio_dma)is probably something like <= 96 bytes, implying a per-page overhead of ~2.4%, excluding the actual rbtree.Tree lookup/modifications with that many nodes might also get a bit slower, but likely still tolerable as you note.
Deal breaker? Not sure. Rather "suboptimal" :) ... but maybe unavoidable for your use case?
Yes. We can't guarantee the behavior of guest, so the overhead would be uncertain and unavoidable.
Quoted 1 line
Hi David,
Quoted 6 lines · David Hildenbrand
Yes, there have been discussions about that, also in the context of supporting huge pages while allowing for the guest to still convert individual 4K chunks ...
A summary is here [1]. Likely more things will be covered at Linux Plumbers.
This is a very valuable link. Thanks a lot for sharing.
Aaron and I are particular interesting to the huge page (both hugetlb and THP) support for gmem_fd (per our testing, at least 10%+ performance gain with it with TDX for many workloads). We will monitor the linux-mm for such kind of discussion. I am wondering whether it's possible that you can involve Aaron and me if the discussion is still open but not on the mailing list (I suppose you will be included always for such kind of discussion). Thanks.
Regards
Yin, Fengwei
Hi Paolo,
Hope to draw your attention. As TEE I/O would depend on shared device assignment and we introduce this RDM solution in QEMU. Now, Observe the in-place private/shared conversion option mentioned by David, do you think we should continue to add pass-thru support for this in-qemu page conversion method? Or wait for the option discussion to see if it will change to in-kernel conversion.
Thanks
Chenyi
Quoted 162 lines · Chenyi Qiang
Commit
852f0048f3("RAMBlock: makeguest_memfdrequire uncoordinated discard") effectively disables device assignment withguest_memfd.guest_memfdis required for confidential guests, so device assignment to confidential guests is disabled. A supporting assumption for disabling device-assignment was that TEE I/O (SEV-TIO, TDX Connect, COVE-IO etc...) solves the confidential-guest device-assignment problem [1]. That turns out not to be the case because TEE I/O depends on being able to operate devices against "shared"/untrusted memory for device initialization and error recovery scenarios.This series utilizes an existing framework named RamDiscardManager to notify VFIO of page conversions. However, there's still one concern related to the semantics of RamDiscardManager which is used to manage the memory plug/unplug state. This is a little different from the memory shared/private in our requirement. See the "Open" section below for more details.
Background
==========
Confidential VMs have two classes of memory: shared and private memory. Shared memory is accessible from the host/VMM while private memory is not. Confidential VMs can decide which memory is shared/private and convert memory between shared/private at runtime."
guest_memfd" is a new kind of fd whose primary goal is to serve guest private memory. The key differences betweenguest_memfdand normal memfd are thatguest_memfdis spawned by a KVM ioctl, bound to its owner VM and cannot be mapped, read or written by userspace.In QEMU's implementation, shared memory is allocated with normal methods (e.g. mmap or fallocate) while private memory is allocated from
guest_memfd. When a VM performs memory conversions, QEMU frees pages viamadvise()or viaPUNCH_HOLEon memfd orguest_memfdfrom one side and allocates new pages from the other side.Problem
=======
Device assignment in QEMU is implemented via VFIO system. In the normal VM, VM memory is pinned at the beginning of time by VFIO. In the confidential VM, the VM can convert memory and when that happens nothing currently tells VFIO that its mappings are stale. This means that page conversion leaks memory and leaves stale IOMMU mappings. For example, sequence like the following can result in stale IOMMU mappings:1. allocate shared page
2. convert pageshared->private
3. discard shared page
4. convert pageprivate->shared
5. allocate shared page
6. issue DMA operations against that shared pageAfter step 3, VFIO is still pinning the page. However, DMA operations in step 6 will hit the old mapping that was allocated in step 1, which causes the device to access the invalid data.
Currently, the commit
852f0048f3("RAMBlock: makeguest_memfdrequire uncoordinated discard") has blocked the device assignment withguest_memfdto avoid this problem.Solution
========
The key to enable shared device assignment is to solve the stale IOMMU mappings problem.Given the constraints and assumptions here is a solution that satisfied the use cases. RamDiscardManager, an existing interface currently utilized by virtio-mem, offers a means to modify IOMMU mappings in accordance with VM page assignment. Page conversion is similar to hot-removing a page in one mode and adding it back in the other.
This series implements a RamDiscardManager for confidential VMs and utilizes its infrastructure to notify VFIO of page conversions.
Another possible attempt [2] was to not discard shared pages in step 3 above. This was an incomplete band-aid because guests would consume twice the memory since shared pages wouldn't be freed even after they were converted to private.
Open
====
Implementing a RamDiscardManager to notify VFIO of page conversions causes changes in semantics: private memory is treated as discarded (or hot-removed) memory. This isn't aligned with the expectation of current RamDiscardManager users (e.g. VFIO or live migration) who really expect that discarded memory is hot-removed and thus can be skipped when the users are processing guest memory. Treating private memory as discarded won't work in future if VFIO or live migration needs to handle private memory. e.g. VFIO may need to map private memory to support Trusted IO and live migration for confidential VMs need to migrate private memory.There are two possible ways to mitigate the semantics changes.
1. Develop a new mechanism to notify the page conversions between private and shared. For example, utilize thenotifier_listin QEMU. VFIO registers its own handler and gets notified upon page conversions. This is a clean approach which only touches the notifier workflow. A challenge is that for device hotplug, existing shared memory should be mapped in IOMMU. This will need additional changes.2. Extend the existing RamDiscardManager interface to manage not only the discarded/populated status of guest memory but also the shared/private status. RamDiscardManager users like VFIO will be notified with one more argument indicating what change is happening and can take action accordingly. It also has challenges e.g. QEMU allows only one RamDiscardManager, how to support virtio-mem for confidential VMs would be a problem. And some APIs like .is_populated() exposed by RamDiscardManager are meaningless to shared/private memory. So they may need some adjustments.
Testing
=======
This patch series is tested based on the internal TDX KVM/QEMU tree.To facilitate shared device assignment with the NIC, employ the legacy type1 VFIO with the QEMU command:
qemu-system-x86_64 [...]
-device vfio-pci,host=XX:XX.XThe parameter of
dma_entry_limitneeds to be adjusted. For example, a 16GB guest needs to adjust the parameter like vfio_iommu_type1.dma_entry_limit=4194304.If use the iommufd-backed VFIO with the qemu command:
qemu-system-x86_64 [...]
-object iommufd,id=iommufd0 \ -device vfio-pci,host=XX:XX.X,iommufd=iommufd0No additional adjustment required.
Following the bootup of the TD guest, the guest's IP address becomes visible, and iperf is able to successfully send and receive data.
Related link
============
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/Chenyi Qiang (6):
guest_memfd: Introduce an object to manage the guest-memfd with
RamDiscardManager
guest_memfd: Introduce a helper to notify the shared/private state
change
KVM: Notify the state change via RamDiscardManager helper during
shared/private conversion
memory: Register the RamDiscardManager instance uponguest_memfd
creation
guest-memfd: Default to discarded (private) inguest_memfd_manager
RAMBlock: makeguest_memfdrequire coordinate discardaccel/kvm/kvm-all.c | 7 + include/sysemu/guest-memfd-manager.h | 49 +++ system/guest-memfd-manager.c | 425 +++++++++++++++++++++++++++ system/meson.build | 1 + system/physmem.c | 11 +- 5 files changed, 492 insertions(+), 1 deletion(-) create mode 100644 include/sysemu/guest-memfd-manager.h create mode 100644 system/guest-memfd-manager.cbase-commit:
900536d3e97aed7fdd9cb4dadd3bf7023360e819
Hi Paolo,
Kindly ping for this thread. The in-place page conversion is discussed at Linux Plumbers. Does it give some direction for shared device assignment enabling work?
Thanks
Chenyi
Quoted 175 lines · Chenyi Qiang
Hi Paolo,
Hope to draw your attention. As TEE I/O would depend on shared device assignment and we introduce this RDM solution in QEMU. Now, Observe the in-place private/shared conversion option mentioned by David, do you think we should continue to add pass-thru support for this in-qemu page conversion method? Or wait for the option discussion to see if it will change to in-kernel conversion.
Thanks
ChenyiQuoted 162 lines · Chenyi Qiang
Commit
852f0048f3("RAMBlock: makeguest_memfdrequire uncoordinated discard") effectively disables device assignment withguest_memfd.guest_memfdis required for confidential guests, so device assignment to confidential guests is disabled. A supporting assumption for disabling device-assignment was that TEE I/O (SEV-TIO, TDX Connect, COVE-IO etc...) solves the confidential-guest device-assignment problem [1]. That turns out not to be the case because TEE I/O depends on being able to operate devices against "shared"/untrusted memory for device initialization and error recovery scenarios.This series utilizes an existing framework named RamDiscardManager to notify VFIO of page conversions. However, there's still one concern related to the semantics of RamDiscardManager which is used to manage the memory plug/unplug state. This is a little different from the memory shared/private in our requirement. See the "Open" section below for more details.
Background
==========
Confidential VMs have two classes of memory: shared and private memory. Shared memory is accessible from the host/VMM while private memory is not. Confidential VMs can decide which memory is shared/private and convert memory between shared/private at runtime."
guest_memfd" is a new kind of fd whose primary goal is to serve guest private memory. The key differences betweenguest_memfdand normal memfd are thatguest_memfdis spawned by a KVM ioctl, bound to its owner VM and cannot be mapped, read or written by userspace.In QEMU's implementation, shared memory is allocated with normal methods (e.g. mmap or fallocate) while private memory is allocated from
guest_memfd. When a VM performs memory conversions, QEMU frees pages viamadvise()or viaPUNCH_HOLEon memfd orguest_memfdfrom one side and allocates new pages from the other side.Problem
=======
Device assignment in QEMU is implemented via VFIO system. In the normal VM, VM memory is pinned at the beginning of time by VFIO. In the confidential VM, the VM can convert memory and when that happens nothing currently tells VFIO that its mappings are stale. This means that page conversion leaks memory and leaves stale IOMMU mappings. For example, sequence like the following can result in stale IOMMU mappings:1. allocate shared page
2. convert pageshared->private
3. discard shared page
4. convert pageprivate->shared
5. allocate shared page
6. issue DMA operations against that shared pageAfter step 3, VFIO is still pinning the page. However, DMA operations in step 6 will hit the old mapping that was allocated in step 1, which causes the device to access the invalid data.
Currently, the commit
852f0048f3("RAMBlock: makeguest_memfdrequire uncoordinated discard") has blocked the device assignment withguest_memfdto avoid this problem.Solution
========
The key to enable shared device assignment is to solve the stale IOMMU mappings problem.Given the constraints and assumptions here is a solution that satisfied the use cases. RamDiscardManager, an existing interface currently utilized by virtio-mem, offers a means to modify IOMMU mappings in accordance with VM page assignment. Page conversion is similar to hot-removing a page in one mode and adding it back in the other.
This series implements a RamDiscardManager for confidential VMs and utilizes its infrastructure to notify VFIO of page conversions.
Another possible attempt [2] was to not discard shared pages in step 3 above. This was an incomplete band-aid because guests would consume twice the memory since shared pages wouldn't be freed even after they were converted to private.
Open
====
Implementing a RamDiscardManager to notify VFIO of page conversions causes changes in semantics: private memory is treated as discarded (or hot-removed) memory. This isn't aligned with the expectation of current RamDiscardManager users (e.g. VFIO or live migration) who really expect that discarded memory is hot-removed and thus can be skipped when the users are processing guest memory. Treating private memory as discarded won't work in future if VFIO or live migration needs to handle private memory. e.g. VFIO may need to map private memory to support Trusted IO and live migration for confidential VMs need to migrate private memory.There are two possible ways to mitigate the semantics changes.
1. Develop a new mechanism to notify the page conversions between private and shared. For example, utilize thenotifier_listin QEMU. VFIO registers its own handler and gets notified upon page conversions. This is a clean approach which only touches the notifier workflow. A challenge is that for device hotplug, existing shared memory should be mapped in IOMMU. This will need additional changes.2. Extend the existing RamDiscardManager interface to manage not only the discarded/populated status of guest memory but also the shared/private status. RamDiscardManager users like VFIO will be notified with one more argument indicating what change is happening and can take action accordingly. It also has challenges e.g. QEMU allows only one RamDiscardManager, how to support virtio-mem for confidential VMs would be a problem. And some APIs like .is_populated() exposed by RamDiscardManager are meaningless to shared/private memory. So they may need some adjustments.
Testing
=======
This patch series is tested based on the internal TDX KVM/QEMU tree.To facilitate shared device assignment with the NIC, employ the legacy type1 VFIO with the QEMU command:
qemu-system-x86_64 [...]
-device vfio-pci,host=XX:XX.XThe parameter of
dma_entry_limitneeds to be adjusted. For example, a 16GB guest needs to adjust the parameter like vfio_iommu_type1.dma_entry_limit=4194304.If use the iommufd-backed VFIO with the qemu command:
qemu-system-x86_64 [...]
-object iommufd,id=iommufd0 \ -device vfio-pci,host=XX:XX.X,iommufd=iommufd0No additional adjustment required.
Following the bootup of the TD guest, the guest's IP address becomes visible, and iperf is able to successfully send and receive data.
Related link
============
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/Chenyi Qiang (6):
guest_memfd: Introduce an object to manage the guest-memfd with
RamDiscardManager
guest_memfd: Introduce a helper to notify the shared/private state
change
KVM: Notify the state change via RamDiscardManager helper during
shared/private conversion
memory: Register the RamDiscardManager instance uponguest_memfd
creation
guest-memfd: Default to discarded (private) inguest_memfd_manager
RAMBlock: makeguest_memfdrequire coordinate discardaccel/kvm/kvm-all.c | 7 + include/sysemu/guest-memfd-manager.h | 49 +++ system/guest-memfd-manager.c | 425 +++++++++++++++++++++++++++ system/meson.build | 1 + system/physmem.c | 11 +- 5 files changed, 492 insertions(+), 1 deletion(-) create mode 100644 include/sysemu/guest-memfd-manager.h create mode 100644 system/guest-memfd-manager.cbase-commit:
900536d3e97aed7fdd9cb4dadd3bf7023360e819
Quoted 6 lines · Chenyi Qiang
Hi Paolo,
Kindly ping for this thread. The in-place page conversion is discussed at Linux Plumbers. Does it give some direction for shared device assignment enabling work?
Hi everybody.
Our NVIDIA GPUs currently support this shared-memory/bounce-buffer method to provide AI acceleration within TEE CVMs. We require passing though the GPU via VFIO stubbing, which means that we are impacted by the absence of an API to inform VFIO about page conversions.
The CSPs have enough kernel engineers who handle this process in their own host kernels, but we have several enterprise customers who are eager to begin using this solution in the upstream. AMD has successfully ported enough of the SEV-SNP support into 6.11 and our initial testing shows successful operation, but only by disabling discard via these two QEMU patches:
- https://github.com/AMDESE/qemu/commit/0c9ae28d3e199de9a40876a492e0f03a11c6f5d8
- https://github.com/AMDESE/qemu/commit/5256c41fb3055961ea7ac368acc0b86a6632d095
This "workaround" is a bit of a hack, as it effectively requires greater than double the amount of host memory than as to be allocated to the guest CVM. The proposal here appears to be a promising workaround; are there other solutions that are recommended for this use case?
This configuration is in GA right now and NVIDIA is committed to support and test this bounce-buffer mailbox solution for many years into the future, so we're highly invested in seeing a converged solution in the upstream.
Thanks,
Rob
Quoted 181 lines
Thanks
ChenyiQuoted 175 lines · Chenyi Qiang
Hi Paolo,
Hope to draw your attention. As TEE I/O would depend on shared device assignment and we introduce this RDM solution in QEMU. Now, Observe the in-place private/shared conversion option mentioned by David, do you think we should continue to add pass-thru support for this in-qemu page conversion method? Or wait for the option discussion to see if it will change to in-kernel conversion.
Thanks
ChenyiQuoted 162 lines · Chenyi Qiang
Commit
852f0048f3("RAMBlock: makeguest_memfdrequire uncoordinated discard") effectively disables device assignment withguest_memfd.guest_memfdis required for confidential guests, so device assignment to confidential guests is disabled. A supporting assumption for disabling device-assignment was that TEE I/O (SEV-TIO, TDX Connect, COVE-IO etc...) solves the confidential-guest device-assignment problem [1]. That turns out not to be the case because TEE I/O depends on being able to operate devices against "shared"/untrusted memory for device initialization and error recovery scenarios.This series utilizes an existing framework named RamDiscardManager to notify VFIO of page conversions. However, there's still one concern related to the semantics of RamDiscardManager which is used to manage the memory plug/unplug state. This is a little different from the memory shared/private in our requirement. See the "Open" section below for more details.
Background
==========
Confidential VMs have two classes of memory: shared and private memory. Shared memory is accessible from the host/VMM while private memory is not. Confidential VMs can decide which memory is shared/private and convert memory between shared/private at runtime."
guest_memfd" is a new kind of fd whose primary goal is to serve guest private memory. The key differences betweenguest_memfdand normal memfd are thatguest_memfdis spawned by a KVM ioctl, bound to its owner VM and cannot be mapped, read or written by userspace.In QEMU's implementation, shared memory is allocated with normal methods (e.g. mmap or fallocate) while private memory is allocated from
guest_memfd. When a VM performs memory conversions, QEMU frees pages viamadvise()or viaPUNCH_HOLEon memfd orguest_memfdfrom one side and allocates new pages from the other side.Problem
=======
Device assignment in QEMU is implemented via VFIO system. In the normal VM, VM memory is pinned at the beginning of time by VFIO. In the confidential VM, the VM can convert memory and when that happens nothing currently tells VFIO that its mappings are stale. This means that page conversion leaks memory and leaves stale IOMMU mappings. For example, sequence like the following can result in stale IOMMU mappings:1. allocate shared page
2. convert pageshared->private
3. discard shared page
4. convert pageprivate->shared
5. allocate shared page
6. issue DMA operations against that shared pageAfter step 3, VFIO is still pinning the page. However, DMA operations in step 6 will hit the old mapping that was allocated in step 1, which causes the device to access the invalid data.
Currently, the commit
852f0048f3("RAMBlock: makeguest_memfdrequire uncoordinated discard") has blocked the device assignment withguest_memfdto avoid this problem.Solution
========
The key to enable shared device assignment is to solve the stale IOMMU mappings problem.Given the constraints and assumptions here is a solution that satisfied the use cases. RamDiscardManager, an existing interface currently utilized by virtio-mem, offers a means to modify IOMMU mappings in accordance with VM page assignment. Page conversion is similar to hot-removing a page in one mode and adding it back in the other.
This series implements a RamDiscardManager for confidential VMs and utilizes its infrastructure to notify VFIO of page conversions.
Another possible attempt [2] was to not discard shared pages in step 3 above. This was an incomplete band-aid because guests would consume twice the memory since shared pages wouldn't be freed even after they were converted to private.
Open
====
Implementing a RamDiscardManager to notify VFIO of page conversions causes changes in semantics: private memory is treated as discarded (or hot-removed) memory. This isn't aligned with the expectation of current RamDiscardManager users (e.g. VFIO or live migration) who really expect that discarded memory is hot-removed and thus can be skipped when the users are processing guest memory. Treating private memory as discarded won't work in future if VFIO or live migration needs to handle private memory. e.g. VFIO may need to map private memory to support Trusted IO and live migration for confidential VMs need to migrate private memory.There are two possible ways to mitigate the semantics changes.
1. Develop a new mechanism to notify the page conversions between private and shared. For example, utilize thenotifier_listin QEMU. VFIO registers its own handler and gets notified upon page conversions. This is a clean approach which only touches the notifier workflow. A challenge is that for device hotplug, existing shared memory should be mapped in IOMMU. This will need additional changes.2. Extend the existing RamDiscardManager interface to manage not only the discarded/populated status of guest memory but also the shared/private status. RamDiscardManager users like VFIO will be notified with one more argument indicating what change is happening and can take action accordingly. It also has challenges e.g. QEMU allows only one RamDiscardManager, how to support virtio-mem for confidential VMs would be a problem. And some APIs like .is_populated() exposed by RamDiscardManager are meaningless to shared/private memory. So they may need some adjustments.
Testing
=======
This patch series is tested based on the internal TDX KVM/QEMU tree.To facilitate shared device assignment with the NIC, employ the legacy type1 VFIO with the QEMU command:
qemu-system-x86_64 [...]
-device vfio-pci,host=XX:XX.XThe parameter of
dma_entry_limitneeds to be adjusted. For example, a 16GB guest needs to adjust the parameter like vfio_iommu_type1.dma_entry_limit=4194304.If use the iommufd-backed VFIO with the qemu command:
qemu-system-x86_64 [...]
-object iommufd,id=iommufd0 \ -device vfio-pci,host=XX:XX.X,iommufd=iommufd0No additional adjustment required.
Following the bootup of the TD guest, the guest's IP address becomes visible, and iperf is able to successfully send and receive data.
Related link
============
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/Chenyi Qiang (6):
guest_memfd: Introduce an object to manage the guest-memfd with
RamDiscardManager
guest_memfd: Introduce a helper to notify the shared/private state
change
KVM: Notify the state change via RamDiscardManager helper during
shared/private conversion
memory: Register the RamDiscardManager instance uponguest_memfd
creation
guest-memfd: Default to discarded (private) inguest_memfd_manager
RAMBlock: makeguest_memfdrequire coordinate discardaccel/kvm/kvm-all.c | 7 + include/sysemu/guest-memfd-manager.h | 49 +++ system/guest-memfd-manager.c | 425 +++++++++++++++++++++++++++ system/meson.build | 1 + system/physmem.c | 11 +- 5 files changed, 492 insertions(+), 1 deletion(-) create mode 100644 include/sysemu/guest-memfd-manager.h create mode 100644 system/guest-memfd-manager.cbase-commit:
900536d3e97aed7fdd9cb4dadd3bf7023360e819
Quoted 8 lines · Rob Nertney
Quoted 6 lines · Chenyi Qiang
Hi Paolo,
Kindly ping for this thread. The in-place page conversion is discussed at Linux Plumbers. Does it give some direction for shared device assignment enabling work?
Hi everybody.
Hi,
Quoted 14 lines
Our NVIDIA GPUs currently support this shared-memory/bounce-buffer method to provide AI acceleration within TEE CVMs. We require passing though the GPU via VFIO stubbing, which means that we are impacted by the absence of an API to inform VFIO about page conversions.
The CSPs have enough kernel engineers who handle this process in their own host kernels, but we have several enterprise customers who are eager to begin using this solution in the upstream. AMD has successfully ported enough of the SEV-SNP support into 6.11 and our initial testing shows successful operation, but only by disabling discard via these two QEMU patches:
- https://github.com/AMDESE/qemu/commit/0c9ae28d3e199de9a40876a492e0f03a11c6f5d8
- https://github.com/AMDESE/qemu/commit/5256c41fb3055961ea7ac368acc0b86a6632d095
Quoted 4 lines
This "workaround" is a bit of a hack, as it effectively requires greater than double the amount of host memory than as to be allocated to the guest CVM. The proposal here appears to be a promising workaround; are there other solutions that are recommended for this use case?
What people we are working on is supporting private and shared memory in guest_memfd, and allowing an in-place conversion between shared and private: this avoids discards + reallocation and consequently any double memory allocation.
To get stuff into VFIO, we must only map the currently shared pages (VFIO will pin + map them), and unmap them (VFIO will unmap + unpin them) before converting them to private.
This series should likely achieve the unmap-before-conversion-to-private, and map-after-conversion-to-shared, such that it could be compatible with guest_memfd.
QEMU would simply mmap the guest_memfd to obtain a user space mapping, from which it can pass address ranges to VFIO like we already do. This user space mapping only allows for shared pages to be faulted in. Currently private pages cannot be faulted in (inaccessible -> SIGBUS). So far the theory.
I'll note that this is likely not the most elegant solution, but something that would achieve in a reasonable timeframe one solution to the problem.
Cheers!
Signature · 3 lines
Cheers, David / dhildenb