Unmapping guest_memfd from Direct Map
Subsystems (6)
cover Unmapping guest_memfd from Direct Map
Hey all,
This RFC series is a rough draft adding support for running non-confidential compute VMs in guest_memfd, based on prior discussions with Sean [1]. Our specific usecase for this is the ability to unmap guest memory from the host kernel's direct map, as a mitigation against a large class of speculative execution issues.
=== Implementation ===
This patch series introduces a new flag to the KVM_CREATE_GUEST_MEMFD to remove its pages from the direct map when they are allocated. When trying to run a guest from such a VM, we now face the problem that without either userspace or kernelspace mappings of guest_memfd, KVM cannot access guest memory to, for example, do MMIO emulation of access memory used to guest/host communication. We have multiple options for solving this when running non-CoCo VMs: (1) implement a TDX-light solution, where the guest shares memory that KVM needs to access, and relies on paravirtual solutions where this is not possible (e.g. MMIO),
(2) have KVM use userspace mappings of guest_memfd (e.g. a memfd_secret-style solution), or (3) dynamically reinsert pages into the direct map whenever KVM wants to access them.
This RFC goes for option (3). Option (1) is a lot of overhead for very little gain, since we are not actually constrained by a physical inability to access guest memory (e.g. we are not in a TDX context where accesses to guest memory cause a #MC). Option (2) has previously been rejected [1].
In this patch series, we make sufficient parts of KVM gmem-aware to be able to boot a Linux initrd from private memory on x86. These include KVM's MMIO emulation (including guest page table walking) and kvm-clock. For VM types which do not allow accessing gmem, we return -EFAULT and attempt to prepare a KVM_EXIT_MEMORY_FAULT.
Additionally, this patch series adds support for "restricted" userspace mappings of guest_memfd, which work similar to memfd_secret (e.g. disallow get_user_pages), which allows handling I/O and loading the guest kernel in a simple way. Support for this is completely independent of the rest of the functionality introduced in this patch series. However, it is required to build a minimal hypervisor PoC that actually allows booting a VM from a disk.
=== Performance ===
We have run some preliminary performance benchmarks to assess the impact of on-the-fly direct map manipulations. We were mainly interested in the impact of manipulating the direct map for MMIO emulation on virtio-mmio. Particularly, we were worried about the impact of the TLB and L1/2/3 Cache flushes that set_memory_[n]p entails.
In our setup, we have taken a modified Firecracker VMM, spawned a Linux guest with 1 vCPU, and used fio to stress a virtio_blk device. We found that the cache flushes caused throughput to drop from around 600MB/s to ~50MB/s (~90%) for both reads and writes (on a Intel(R) Xeon(R) Platinum 8375C CPU with 64 cores). We then converted our prototype to use set_direct_map_{invalid,default}_noflush instead of set_memory_[n]p and found that without cache flushes the pure impact of the direct map manipulation is indistinguishable from noise. This is why we use set_direct_map_{invalid,default}_noflush instead of set_memory_[n]p in this RFC.
Note that in this comparison, both the baseline, as well as the guest_memfd-supporting version of Firecracker were made to bounce I/O buffers in VMM userspace. As GUP is disabled for the guest_memfd VMAs, the virtio stack cannot directly pass guest buffers to read/write syscalls.
=== Security ===
We want to use unmapping guest memory from the host kernel as a security mitigation against transient execution attacks. Temporarily restoring direct map entries whenever KVM requires access to guest memory leaves a gap in this mitigation. We believe this to be acceptable for the above cases, since pages used for paravirtual guest/host communication (e.g. kvm-clock) and guest page tables do not contain sensitive data. MMIO emulation will only end up reading pages containing privileged instructions (e.g. guest kernel code).
=== Summary ===
Patches 1-4 are about hot-patching various points inside of KVM that access guest memory to correctly handle the case where memory happens to be guest-private. This means either handling the access as a memory error, or simply accessing the memslot's guest_memfd instead of looking at the userspace provided VMA if the VM type allows these kind of accesses. Patches 5-6 add a flag to KVM_CREATE_GUEST_MEMFD that will make it remove its pages from the kernel's direct map. Whenever KVM wants to access guest-private memory, it will temporarily re-insert the relevant pages. Patches 7-8 allow for restricted userspace mappings (e.g. get_user_pages paths are disabled like for memfd_secret) of guest_memfd, so that userspace has an easy path for loading the guest kernel and handling I/O-buffers.
=== ToDos / Limitations ===
There are still a few rough edges that need to be addressed before dropping the "RFC" tag, e.g.
* Handle errors of set_direct_map_default_not_flush in kvm_gmem_invalidate_folio instead of calling BUG_ON
* Lift the limitation of "at most one gfn_to_pfn_cache for each gfn/pfn" in e1c61f0a7963 ("kvm: gmem: Temporarily restore direct map entries when needed"). It currently means that guests with more than 1 vcpu fail to boot, because multiple vcpus can put their kvm-clock PV structures into the same page (gfn)
* Write selftests, particularly around hole punching, direct map removal, and mmap.
Lastly, there's the question of nested virtualization which Sean brought up in previous discussions, which runs into similar problems as MMIO. I have looked at it very briefly. On Intel, KVM uses various gfn->uhva caches, which run in similar problems as the gfn_to_hva_caches dealt with in 200834b15dda ("kvm: use slowpath in gfn_to_hva_cache if memory is private"). However, previous attempts at just converting this to gfn_to_pfn_cache (which would make them work with guest_memfd) proved complicated [2]. I suppose initially, we should probably disallow nested virtualization in VMs that have their memory removed from the direct map.
Best,
Patrick
[1]: https://lore.kernel.org/linux-mm/[email protected]/
[2]: https://lore.kernel.org/kvm/[email protected]/
Patrick Roy (8):
kvm: Allow reading/writing gmem using kvm_{read,write}_guest
kvm: use slowpath in gfn_to_hva_cache if memory is private
kvm: pfncache: enlighten about gmem
kvm: x86: support walking guest page tables in gmem
kvm: gmem: add option to remove guest private memory from direct map
kvm: gmem: Temporarily restore direct map entries when needed
mm: secretmem: use AS_INACCESSIBLE to prohibit GUP
kvm: gmem: Allow restricted userspace mappings
arch/x86/kvm/mmu/paging_tmpl.h | 94 +++++++++++++++++++----- include/linux/kvm_host.h | 5 ++ include/linux/kvm_types.h | 1 + include/linux/secretmem.h | 13 +++- include/uapi/linux/kvm.h | 2 + mm/secretmem.c | 6 +- virt/kvm/guest_memfd.c | 83 +++++++++++++++++++-- virt/kvm/kvm_main.c | 112 +++++++++++++++++++++++++++- virt/kvm/pfncache.c | 130 +++++++++++++++++++++++++++++---- 9 files changed, 399 insertions(+), 47 deletions(-)
base-commit: 890a64810d59b1a58ed26efc28cfd821fc068e84
Signature · 1 line
2.45.2
RFC PATCH 1/8 kvm: Allow reading/writing gmem using kvm_{read,write}_guest
If KVM can access guest-private memory without causing a host-kernel panic (e.g. currently only if the vm type is KVM_SW_PROTECTED_VM), allow kvm_{read,write}_guest to access gfns that are set to "private". If KVM cannot access guest-private memory (say, because it is running a TDX VM), prepare a KVM_EXIT_MEMORY_FAULT (if possible) and return -EFAULT.
KVM can only prepare the memory fault exit inside the kvm_vcpu_{read,write}_guest variant, as it needs a vcpu reference to assign the exit reason to.
KVM accesses guest-private memory via kernel virtual addresses/the direct map. In the special case of guest_memfd, it does not have to worry about gfn->pfn mappings being invalidated, since guest_memfd pages are immovable.
--- include/linux/kvm_host.h | 5 +++ virt/kvm/kvm_main.c | 85 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+)
include/linux/kvm_host.h +5 -0
@@ -2407,6 +2407,11 @@ static inline void kvm_prepare_memory_fault_exit(struct kvm_vcpu *vcpu, vcpu->run->memory_fault.flags |=KVM_MEMORY_EXIT_FLAG_PRIVATE ; } +static inline bool kvm_can_access_gmem (struct kvm *kvm) +{ +return kvm->arch.vm_type ==KVM_X86_SW_PROTECTED_VM ; +} +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES static inline unsigned long kvm_get_memory_attributes (struct kvm *kvm,gfn_t gfn) {
virt/kvm/kvm_main.c +85 -0
119 lines not on this page · raw message
base-commit: 771df9ffadb8204e61d3e98f36c5067102aab78f
Signature · 1 line
2.45.2
RFC PATCH 2/8 kvm: use slowpath in gfn_to_hva_cache if memory is private
In reply to Patrick RoyCurrently, KVM uses gfn_to_hva_caches to cache gfn->memslot->userspace host virtual address (uhva) translations. If a gfn is backed by guest_memfd however, there is no uhva-equivalent item we could possible cache, since accesses go through a file descriptor instead of a VMA. Thus, we effectively disable gfn_to_hva_caches in the case where gfns are gmem-backed, and instead do a gfn->pfn translation on the fly by calling kvm_{read,write}_guest inside kvm_{read,write}_guest_cached.
--- virt/kvm/kvm_main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
virt/kvm/kvm_main.c +2 -2
@@ -3603,7 +3603,7 @@ int kvm_write_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,if (kvm_is_error_hva (ghc->hva))return -EFAULT ; -if (unlikely (!ghc->memslot)) +if (unlikely (!ghc->memslot ||kvm_mem_is_private (kvm,gpa_to_gfn (gpa))))return kvm_write_guest (kvm, gpa, data, len); r =__copy_to_user ((void __user *)ghc->hva + offset, data, len);
@@ -3641,7 +3641,7 @@ int kvm_read_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,if (kvm_is_error_hva (ghc->hva))return -EFAULT ; -if (unlikely (!ghc->memslot)) +if (unlikely (!ghc->memslot ||kvm_mem_is_private (kvm,gpa_to_gfn (gpa))))return kvm_read_guest (kvm, gpa, data, len); r =__copy_from_user (data, (void __user *)ghc->hva + offset, len);
Signature · 1 line
2.45.2
RFC PATCH 3/8 kvm: pfncache: enlighten about gmem
In reply to Patrick RoyKVM uses gfn_to_pfn_caches to cache translations from gfn all the way to the pfn (for example, kvm-clock caches the page storing the page used for guest/host communication this way). Unlike the gfn_to_hva_cache, where no equivalent caching semantics were possible to gmem-backed gfns (see also 858e8068a750 ("kvm: pfncache: enlighten about gmem")), here it is possible to simply cache the pfn returned by kvm_gmem_get_pfn.
Additionally, gfn_to_pfn_caches now invalidate whenever a cached gfn's attributes are flipped from shared to private (or vice-versa).
--- include/linux/kvm_types.h | 1 + virt/kvm/pfncache.c | 41 +++++++++++++++++++++++++++++++++------ 2 files changed, 36 insertions(+), 6 deletions(-)
include/linux/kvm_types.h +1 -0
@@ -70,6 +70,7 @@ struct gfn_to_pfn_cache {kvm_pfn_t pfn;bool active;bool valid; +bool is_private; };#ifdef KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE
virt/kvm/pfncache.c +35 -6
86 lines not on this page · raw message
Signature · 1 line
2.45.2
Re: [RFC PATCH 3/8] kvm: pfncache: enlighten about gmem
Quoted 11 lines · Patrick Roy
KVM uses
gfn_to_pfn_cachesto cache translations from gfn all the way to the pfn (for example, kvm-clock caches the page storing the page used for guest/host communication this way). Unlike thegfn_to_hva_cache, where no equivalent caching semantics were possible to gmem-backed gfns (see also858e8068a750("kvm: pfncache: enlighten about gmem")), here it is possible to simply cache the pfn returned bykvm_gmem_get_pfn.Additionally,
gfn_to_pfn_cachesnow invalidate whenever a cached gfn's attributes are flipped from shared to private (or vice-versa).Signed-off-by Patrick Roy <[email protected]>
I can't see how this is safe from race conditions.
When the GPC is invalidated from gfn_to_pfn_cache_invalidate_start() its *write* lock is taken and gpc->valid is set to false.
In parallel, any code using the GPC to access guest memory will take the *read* lock, call kvm_gpc_check(), and then go ahead and use the pointer to its heart's content until eventually dropping the read lock.
Since invalidation takes the write lock, it has to wait until the GPC is no longer in active use, and the pointer cannot be being dereferenced.
How does this work for the kvm_mem_is_private() check. You've added a check in kvm_gpc_check(), but what if the pfn is made private immediately *after* that check? Unless the code path which makes the pfn private also takes the write lock, how is it safe?
Re: [RFC PATCH 3/8] kvm: pfncache: enlighten about gmem
Quoted 22 lines · David Woodhouse
Quoted 11 lines · Patrick Roy
KVM uses
gfn_to_pfn_cachesto cache translations from gfn all the way to the pfn (for example, kvm-clock caches the page storing the page used for guest/host communication this way). Unlike thegfn_to_hva_cache, where no equivalent caching semantics were possible to gmem-backed gfns (see also858e8068a750("kvm: pfncache: enlighten about gmem")), here it is possible to simply cache the pfn returned bykvm_gmem_get_pfn.Additionally,
gfn_to_pfn_cachesnow invalidate whenever a cached gfn's attributes are flipped from shared to private (or vice-versa).Signed-off-by Patrick Roy <[email protected]>I can't see how this is safe from race conditions.
When the GPC is invalidated from
gfn_to_pfn_cache_invalidate_start()its *write* lock is taken andgpc->validis set to false.In parallel, any code using the GPC to access guest memory will take the *read* lock, call
kvm_gpc_check(), and then go ahead and use the pointer to its heart's content until eventually dropping the read lock.
Quoted 8 lines
Since invalidation takes the write lock, it has to wait until the GPC is no longer in active use, and the pointer cannot be being dereferenced.
How does this work for the
kvm_mem_is_private()check. You've added a check inkvm_gpc_check(), but what if the pfn is made private immediately *after* that check? Unless the code path which makes the pfn private also takes the write lock, how is it safe?
Ah, you're right - I did in fact overlook this. I do think that it works out though: kvm_vm_set_mem_attributes, which is used for flipping between shared/private, registers the range which had its attributes changed for invalidation, and thus gfn_to_pfn_cache_invalidate_start should get called for it (although I have to admit I do not immediately see what the exact callstack for this looks like, so maybe I am misunderstanding something about invalidation here?).
Re: [RFC PATCH 3/8] kvm: pfncache: enlighten about gmem
Quoted 1 line · Patrick Roy
On 7/9/24 15:36, David Woodhouse wrote:
I did? It isn't September yet, surely?
Quoted 31 lines
Quoted 22 lines · Patrick Roy
Quoted 11 lines
KVM uses
gfn_to_pfn_cachesto cache translations from gfn all the way to the pfn (for example, kvm-clock caches the page storing the page used for guest/host communication this way). Unlike thegfn_to_hva_cache, where no equivalent caching semantics were possible to gmem-backed gfns (see also858e8068a750("kvm: pfncache: enlighten about gmem")), here it is possible to simply cache the pfn returned bykvm_gmem_get_pfn.Additionally,
gfn_to_pfn_cachesnow invalidate whenever a cached gfn's attributes are flipped from shared to private (or vice-versa).Signed-off-by Patrick Roy <[email protected]>I can't see how this is safe from race conditions.
When the GPC is invalidated from
gfn_to_pfn_cache_invalidate_start()its *write* lock is taken andgpc->validis set to false.In parallel, any code using the GPC to access guest memory will take the *read* lock, call
kvm_gpc_check(), and then go ahead and use the pointer to its heart's content until eventually dropping the read lock.Quoted 8 lines
Since invalidation takes the write lock, it has to wait until the GPC is no longer in active use, and the pointer cannot be being dereferenced.
How does this work for the
kvm_mem_is_private()check. You've added a check inkvm_gpc_check(), but what if the pfn is made private immediately *after* that check? Unless the code path which makes the pfn private also takes the write lock, how is it safe?
Quoted 7 lines
Ah, you're right - I did in fact overlook this. I do think that it works out though:
kvm_vm_set_mem_attributes, which is used for flipping between shared/private, registers the range which had its attributes changed for invalidation, and thusgfn_to_pfn_cache_invalidate_startshould get called for it (although I have to admit I do not immediately see what the exact callstack for this looks like, so maybe I am misunderstanding something about invalidation here?).
In that case, wouldn't that mean the explicit checks on gpc->is_private matching kvm_mem_is_private() would be redundant and you can remove them because you can trust that gpc->valid would be cleared?
Re: [RFC PATCH 3/8] kvm: pfncache: enlighten about gmem
Quoted 4 lines · David Woodhouse
Quoted 1 line · Patrick Roy
On 7/9/24 15:36, David Woodhouse wrote:
I did? It isn't September yet, surely?
Argh, thanks for letting me know, I think I've whacked some sense into my mail client now :)
Quoted 39 lines
Quoted 31 lines
Quoted 22 lines · Patrick Roy
Quoted 11 lines
KVM uses
gfn_to_pfn_cachesto cache translations from gfn all the way to the pfn (for example, kvm-clock caches the page storing the page used for guest/host communication this way). Unlike thegfn_to_hva_cache, where no equivalent caching semantics were possible to gmem-backed gfns (see also858e8068a750("kvm: pfncache: enlighten about gmem")), here it is possible to simply cache the pfn returned bykvm_gmem_get_pfn.Additionally,
gfn_to_pfn_cachesnow invalidate whenever a cached gfn's attributes are flipped from shared to private (or vice-versa).Signed-off-by Patrick Roy <[email protected]>I can't see how this is safe from race conditions.
When the GPC is invalidated from
gfn_to_pfn_cache_invalidate_start()its *write* lock is taken andgpc->validis set to false.In parallel, any code using the GPC to access guest memory will take the *read* lock, call
kvm_gpc_check(), and then go ahead and use the pointer to its heart's content until eventually dropping the read lock.Quoted 8 lines
Since invalidation takes the write lock, it has to wait until the GPC is no longer in active use, and the pointer cannot be being dereferenced.
How does this work for the
kvm_mem_is_private()check. You've added a check inkvm_gpc_check(), but what if the pfn is made private immediately *after* that check? Unless the code path which makes the pfn private also takes the write lock, how is it safe?Quoted 7 lines
Ah, you're right - I did in fact overlook this. I do think that it works out though:
kvm_vm_set_mem_attributes, which is used for flipping between shared/private, registers the range which had its attributes changed for invalidation, and thusgfn_to_pfn_cache_invalidate_startshould get called for it (although I have to admit I do not immediately see what the exact callstack for this looks like, so maybe I am misunderstanding something about invalidation here?).
Quoted 4 lines
In that case, wouldn't that mean the explicit checks on
gpc->is_privatematchingkvm_mem_is_private()would be redundant and you can remove them because you can trust thatgpc->validwould be cleared?
Right, yes, it would indeed mean that. I'll double-check my assumption about the whole invalidation thing and adjust the code for the next iteration!
Re: [RFC PATCH 3/8] kvm: pfncache: enlighten about gmem
Quoted 9 lines · Patrick Roy
Quoted 4 lines · David Woodhouse
In that case, wouldn't that mean the explicit checks on
gpc->is_privatematchingkvm_mem_is_private()would be redundant and you can remove them because you can trust thatgpc->validwould be cleared?Right, yes, it would indeed mean that. I'll double-check my assumption about the whole invalidation thing and adjust the code for the next iteration!
I was going to suggest that you take the check you'd added to kvm_gpc_check() and move it down below the ->valid check, and turn it into a BUG_ON() to check that assertion.
You *might* get false positives with that though, if the result of kvm_mem_is_private() becomes true before the flush actually *happens*?
RFC PATCH 4/8 kvm: x86: support walking guest page tables in gmem
In reply to Patrick RoyUpdate the logic in paging_tmpl.h to work with guest_private memory. If KVM cannot access gmem and the guest's page tables are in gfns marked as private, then error out.
Let the guest page table walker access gmem by making it use gfn_to_pfn_caches, which are already gmem aware, and will later also handle on-demand mapping of gmem once it supports being removed from the direct map. We re-use the gfn_to_pfn_cache here to avoid implementing yet another remapping solution to support the cmpxchg used to set the "accessed" bit on guest PTEs. The only case that now needs some special handling is page tables in read-only memslots, as gfn_to_pfn_caches cannot be used for readonly memory. In this case, use kvm_vcpu_read_guest (which is also gmem aware), as there is no need to cache the gfn->pfn translation in this case (there is no need to do a cmpxchg on the PTE as the walker does not set the accessed bit for read-only ptes).
--- arch/x86/kvm/mmu/paging_tmpl.h | 94 ++++++++++++++++++++++++++++------ 1 file changed, 77 insertions(+), 17 deletions(-)
arch/x86/kvm/mmu/paging_tmpl.h +77 -17
@@ -84,7 +84,7 @@ struct guest_walker {pt_element_t ptes[PT_MAX_FULL_LEVELS ];pt_element_t prefetch_ptes[PTE_PREFETCH_NUM ];gpa_t pte_gpa[PT_MAX_FULL_LEVELS ]; -pt_element_t __user *ptep_user[PT_MAX_FULL_LEVELS ]; +struct gfn_to_pfn_cache ptep_caches[PT_MAX_FULL_LEVELS ];bool pte_writable[PT_MAX_FULL_LEVELS ];unsigned int pt_access[PT_MAX_FULL_LEVELS ];unsigned int pte_access;
@@ -201,7 +201,7 @@ static int FNAME(update_accessed_dirty_bits)(struct kvm_vcpu *vcpu, {unsigned level, index;pt_element_t pte, orig_pte; -pt_element_t __user *ptep_user; +struct gfn_to_pfn_cache *pte_cache;gfn_t table_gfn;int ret;
@@ -210,10 +210,12 @@ static int FNAME(update_accessed_dirty_bits)(struct kvm_vcpu *vcpu,return 0 ;for (level = walker->max_level; level >= walker->level; --level) { +unsigned long flags; + pte = orig_pte = walker->ptes[level -1 ]; table_gfn = walker->table_gfn[level -1 ]; - ptep_user = walker->ptep_user[level -1 ]; - index =offset_in_page (ptep_user) /sizeof (pt_element_t); + pte_cache = &walker->ptep_caches[level -1 ]; + index =offset_in_page (pte_cache->khva) /sizeof (pt_element_t);if (!(pte &PT_GUEST_ACCESSED_MASK )) {trace_kvm_mmu_set_accessed_bit (table_gfn, index,sizeof (pte)); pte |=PT_GUEST_ACCESSED_MASK ;
@@ -246,11 +248,26 @@ static int FNAME(update_accessed_dirty_bits)(struct kvm_vcpu *vcpu,if (unlikely (!walker->pte_writable[level -1 ]))continue ; - ret =__try_cmpxchg_user (ptep_user, &orig_pte, pte, fault); +read_lock_irqsave (&pte_cache->lock, flags); +while (!kvm_gpc_check (pte_cache,sizeof (pte))) { +read_unlock_irqrestore (&pte_cache->lock, flags); + + ret =kvm_gpc_refresh (pte_cache,sizeof (pte)); +if (ret) +return ret; + +read_lock_irqsave (&pte_cache->lock, flags); + } + ret =__try_cmpxchg ((pt_element_t *)pte_cache->khva, &orig_pte, pte,sizeof (pte)); + +if (!ret) +kvm_gpc_mark_dirty_in_slot (pte_cache); + +read_unlock_irqrestore (&pte_cache->lock, flags); +if (ret)return ret; -kvm_vcpu_mark_page_dirty (vcpu, table_gfn); walker->ptes[level -1 ] = pte; }return 0 ;
@@ -296,6 +313,12 @@ static inline bool FNAME(is_last_gpte)(struct kvm_mmu *mmu,return gpte &PT_PAGE_SIZE_MASK ; } + +static void FNAME (walk_deactivate_gpcs )(struct guest_walker *walker) { +for (unsigned int level =0 ; level <PT_MAX_FULL_LEVELS ; ++level) +kvm_gpc_deactivate (&walker->ptep_caches[level]); +} +/* * Fetch a guest pte for a guest virtual address, or for an L2's GPA. */
@@ -305,7 +328,6 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker, {int ret;pt_element_t pte; -pt_element_t __user *ptep_user;gfn_t table_gfn;u64 pt_access, pte_access;unsigned index, accessed_dirty, pte_pkey;
@@ -320,8 +342,17 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,u16 errcode =0 ;gpa_t real_gpa;gfn_t gfn; +struct gfn_to_pfn_cache *pte_cache;trace_kvm_mmu_pagetable_walk (addr, access); + +for (unsigned int level =0 ; level <PT_MAX_FULL_LEVELS ; ++level) { + pte_cache = &walker->ptep_caches[level]; + +memset (pte_cache,0 ,sizeof (*pte_cache)); +kvm_gpc_init (pte_cache, vcpu->kvm); + } + retry_walk: walker->level = mmu->cpu_role.base.level; pte =kvm_mmu_get_guest_pgd (vcpu, mmu);
@@ -362,11 +393,13 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker, do { struct kvm_memory_slot *slot; - unsigned long host_addr; + unsigned long flags; pt_access = pte_access; --walker->level; + pte_cache = &walker->ptep_caches[walker->level - 1]; + index = PT_INDEX(addr, walker->level); table_gfn = gpte_to_gfn(pte); offset = index * sizeof(pt_element_t);
@@ -396,15 +429,36 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,if (!kvm_is_visible_memslot (slot)) goto error; - host_addr =gfn_to_hva_memslot_prot (slot,gpa_to_gfn (real_gpa), - &walker->pte_writable[walker->level -1 ]); -if (unlikely (kvm_is_error_hva (host_addr))) - goto error; +/* +* gfn_to_pfn_cache expects the memory to be writable. However, +* if the memory is not writable, we do not need caching in the +* first place, as we only need it to later potentially write +* the access bit (which we cannot do anyway if the memory is +* readonly). +*/ +if (slot->flags &KVM_MEM_READONLY ) { +if (kvm_vcpu_read_guest (vcpu, real_gpa + offset, &pte,sizeof (pte))) + goto error; + }else { +if (kvm_gpc_activate (pte_cache, real_gpa + offset, +sizeof (pte))) + goto error; - ptep_user = (pt_element_t __user *)((void *)host_addr + offset); -if (unlikely (__get_user (pte, ptep_user))) - goto error; - walker->ptep_user[walker->level -1 ] = ptep_user; +read_lock_irqsave (&pte_cache->lock, flags); +while (!kvm_gpc_check (pte_cache,sizeof (pte))) { +read_unlock_irqrestore (&pte_cache->lock, flags); + +if (kvm_gpc_refresh (pte_cache,sizeof (pte))) + goto error; + +read_lock_irqsave (&pte_cache->lock, flags); + } + + pte = *(pt_element_t *)pte_cache->khva; +read_unlock_irqrestore (&pte_cache->lock, flags); + + walker->pte_writable[walker->level -1 ] = true; + }trace_kvm_mmu_paging_element (pte, walker->level);
@@ -467,13 +521,19 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker, addr, write_fault);if (unlikely (ret <0 )) goto error; -else if (ret) +else if (ret) { +FNAME (walk_deactivate_gpcs)(walker); goto retry_walk; + } } +FNAME (walk_deactivate_gpcs)(walker); +return 1 ; error: +FNAME (walk_deactivate_gpcs)(walker); + errcode |= write_fault | user_fault;if (fetch_fault && (is_efer_nx (mmu) ||is_cr4_smep (mmu))) errcode |=PFERR_FETCH_MASK ;
Signature · 1 line
2.45.2
RFC PATCH 5/8 kvm: gmem: add option to remove guest private memory from direct map
In reply to Patrick RoyWhile guest_memfd is not available to be mapped by userspace, it is still accessible through the kernel's direct map. This means that in scenarios where guest-private memory is not hardware protected, it can be speculatively read and its contents potentially leaked through hardware side-channels. Removing guest-private memory from the direct map, thus mitigates a large class of speculative execution issues [1, Table 1].
This patch adds a flag to the KVM_CREATE_GUEST_MEMFD which, if set, removes the struct pages backing guest-private memory from the direct map. Should CONFIG_HAVE_KVM_GMEM_{INVALIDATE, PREPARE} be set, pages are removed after preparation and before invalidation, so that the prepare/invalidate routines do not have to worry about potentially absent direct map entries.
Direct map removal do not reuse the KVM_GMEM_PREPARE machinery, since prepare can be called multiple time, and it is the responsibility of the preparation routine to not "prepare" the same folio twice [2]. Thus, instead explicitly check if filemap_grab_folio allocated a new folio, and
remove the returned folio from the direct map only if this was the case.
The patch uses release_folio instead of free_folio to reinsert pages back into the direct map as by the time free_folio is called, folio->mapping can already be NULL. This means that a call to folio_inode inside free_folio might deference a NULL pointer, leaving no way to access the inode which stores the flags that allow determining whether the page was removed from the direct map in the first place.
Lastly, the patch uses set_direct_map_{invalid,default}_noflush instead of set_memory_[n]p to avoid expensive flushes of TLBs and the L*-cache hierarchy. This is especially important once KVM restores direct map entries on-demand in later patches, where simple FIO benchmarks of a virtio-blk device have shown that TLB flushes on a Intel(R) Xeon(R) Platinum 8375C CPU @ 2.90GHz resulted in 80% degradation in throughput compared to a non-flushing solution.
Not flushing the TLB means that until TLB entries for temporarily restored direct map entries get naturally evicted, they can be used during speculative execution, and effectively "unhide" the memory for longer than intended. We consider this acceptable, as the only pages that are temporarily reinserted into the direct map like this will either hold PV data structures (kvm-clock, asyncpf, etc), or pages containing privileged instructions inside the guest kernel image (in the MMIO emulation case).
[1]: https://download.vusec.net/papers/quarantine_raid23.pdf
--- include/uapi/linux/kvm.h | 2 ++ virt/kvm/guest_memfd.c | 52 ++++++++++++++++++++++++++++++++++------ 2 files changed, 47 insertions(+), 7 deletions(-)
include/uapi/linux/kvm.h +2 -0
@@ -1563,4 +1563,6 @@ struct kvm_create_guest_memfd { __u64 reserved[6]; }; +#define KVM_GMEM_NO_DIRECT_MAP (1ULL << 0) +#endif /* __LINUX_KVM_H */
virt/kvm/guest_memfd.c +45 -7
99 lines not on this page · raw message
Signature · 1 line
2.45.2
Mike Rapoport
maintainerRe: [RFC PATCH 5/8] kvm: gmem: add option to remove guest private memory from direct map
Quoted 94 lines · Patrick Roy
While
guest_memfdis not available to be mapped by userspace, it is still accessible through the kernel's direct map. This means that in scenarios where guest-private memory is not hardware protected, it can be speculatively read and its contents potentially leaked through hardware side-channels. Removing guest-private memory from the direct map, thus mitigates a large class of speculative execution issues [1, Table 1].This patch adds a flag to the
KVM_CREATE_GUEST_MEMFDwhich, if set, removes the struct pages backing guest-private memory from the direct map. ShouldCONFIG_HAVE_KVM_GMEM_{INVALIDATE, PREPARE}be set, pages are removed after preparation and before invalidation, so that the prepare/invalidate routines do not have to worry about potentially absent direct map entries.Direct map removal do not reuse the
KVM_GMEM_PREPAREmachinery, sincepreparecan be called multiple time, and it is the responsibility of the preparation routine to not "prepare" the same folio twice [2]. Thus, instead explicitly check iffilemap_grab_folioallocated a new folio, and
remove the returned folio from the direct map only if this was the case.The patch uses
release_folioinstead offree_folioto reinsert pages back into the direct map as by the timefree_foliois called,folio->mappingcan already be NULL. This means that a call tofolio_inodeinsidefree_foliomight deference a NULL pointer, leaving no way to access the inode which stores the flags that allow determining whether the page was removed from the direct map in the first place.Lastly, the patch uses set_direct_map_{invalid,default}_noflush instead of
set_memory_[n]pto avoid expensive flushes of TLBs and the L*-cache hierarchy. This is especially important once KVM restores direct map entries on-demand in later patches, where simple FIO benchmarks of a virtio-blk device have shown that TLB flushes on a Intel(R) Xeon(R) Platinum 8375C CPU @ 2.90GHz resulted in 80% degradation in throughput compared to a non-flushing solution.Not flushing the TLB means that until TLB entries for temporarily restored direct map entries get naturally evicted, they can be used during speculative execution, and effectively "unhide" the memory for longer than intended. We consider this acceptable, as the only pages that are temporarily reinserted into the direct map like this will either hold PV data structures (kvm-clock, asyncpf, etc), or pages containing privileged instructions inside the guest kernel image (in the MMIO emulation case).
[1]: https://download.vusec.net/papers/quarantine_raid23.pdf
Signed-off-by Patrick Roy <[email protected]>--- include/uapi/linux/kvm.h | 2 ++ virt/kvm/guest_memfd.c | 52 ++++++++++++++++++++++++++++++++++------ 2 files changed, 47 insertions(+), 7 deletions(-)include/uapi/linux/kvm.h +2
-0@@ -1563,4 +1563,6 @@ struct kvm_create_guest_memfd { __u64 reserved[6]; }; +#define KVM_GMEM_NO_DIRECT_MAP (1ULL << 0) +#endif /* __LINUX_KVM_H */ virt/kvm/guest_memfd.c +8
-0@@ -4,6 +4,7 @@#include <linux/kvm_host.h> #include <linux/pagemap.h> #include <linux/anon_inodes.h> +#include <linux/set_memory.h> #include "kvm_mm.h" @@ -49,9 +50,16 @@ static int kvm_gmem_prepare_folio(struct inode *inode, pgoff_t index, struct folreturn 0 ; } +static bool kvm_gmem_not_present (struct inode *inode) +{ +return ((unsigned long )inode->i_private &KVM_GMEM_NO_DIRECT_MAP ) !=0 ; +} +static struct folio *kvm_gmem_get_folio (struct inode *inode,pgoff_t index,bool prepare) {struct folio *folio; +bool zap_direct_map = false; +int r;/* TODO: Support huge pages. */ folio =filemap_grab_folio (inode->i_mapping, index);
virt/kvm/guest_memfd.c static struct folio *kvm_gmem_get_folio(struct inode *inode, pgoff_t index, bool · line 86
Quoted 5 lines
virt/kvm/guest_memfd.c +2
-0@@ -74,16 +82,30 @@ static struct folio *kvm_gmem_get_folio(struct inode *inode, pgoff_t index, boolfor (i =0 ; i < nr_pages; i++)clear_highpage (folio_page (folio, i)); +// We need to clear the folio before calling kvm_gmem_prepare_folio, +// but can only remove it from the direct map _after_ preparation is done.
No C++ comments please
Quoted 9 lines
virt/kvm/guest_memfd.c +2
-3+ zap_direct_map =kvm_gmem_not_present (inode); +folio_mark_uptodate (folio); }if (prepare) { -int r =kvm_gmem_prepare_folio (inode, index, folio); -if (r <0 ) { -folio_unlock (folio);
virt/kvm/guest_memfd.c static struct folio *kvm_gmem_get_folio(struct inode *inode, pgoff_t index, bool · line 99
Quoted 10 lines
virt/kvm/guest_memfd.c +7
-3-folio_put (folio); -return ERR_PTR (r); - } + r =kvm_gmem_prepare_folio (inode, index, folio); +if (r <0 ) + goto out_err; + } + +if (zap_direct_map) { + r =set_direct_map_invalid_noflush (&folio->page);
It's not future proof to presume that folio is a single page here. You should loop over folio pages and add a TLB flush after the loop.
Quoted 24 lines
virt/kvm/guest_memfd.c +13
-0+if (r <0 ) + goto out_err; + +// We use the private flag to track whether the folio has been removed +// from the direct map. This is because inside of ->free_folio, +// we do not have access to the address_space anymore, meaning we +// cannot check folio_inode(folio)->i_private to determine whether +// KVM_GMEM_NO_DIRECT_MAP was set. +folio_set_private (folio); }/* @@ -91,6 +113,10 @@ static struct folio *kvm_gmem_get_folio(struct inode *inode, pgoff_t index, bool* unevictable and there is no storage to write back to. */ return folio; +out_err: +folio_unlock (folio); +folio_put (folio); +return ERR_PTR (r); } static void kvm_gmem_invalidate_begin(struct kvm_gmem *gmem, pgoff_t start,
virt/kvm/guest_memfd.c static void kvm_gmem_free_folio(struct folio *folio) · line 388
Quoted 9 lines
virt/kvm/guest_memfd.c +6
-0@@ -354,10 +380,22 @@ static void kvm_gmem_free_folio(struct folio *folio) }#endif +static void kvm_gmem_invalidate_folio (struct folio *folio,size_t start,size_t end) +{ +if (start ==0 && end ==PAGE_SIZE ) { +// We only get here if PG_private is set, which only happens if kvm_gmem_not_present +// returned true in kvm_gmem_get_folio. Thus no need to do that check again. +BUG_ON (set_direct_map_default_noflush (&folio->page));
Ditto.
Quoted 25 lines
virt/kvm/guest_memfd.c +7
-1+ +folio_clear_private (folio); + } +} +static const struct address_space_operations kvm_gmem_aops = { .dirty_folio = noop_dirty_folio, .migrate_folio = kvm_gmem_migrate_folio, .error_remove_folio = kvm_gmem_error_folio, + .invalidate_folio = kvm_gmem_invalidate_folio, #ifdef CONFIG_HAVE_KVM_GMEM_INVALIDATE .free_folio = kvm_gmem_free_folio, #endif@@ -443,7 +481,7 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args) {loff_t size = args->size;u64 flags = args->flags; -u64 valid_flags =0 ; +u64 valid_flags =KVM_GMEM_NO_DIRECT_MAP ;if (flags & ~valid_flags)return -EINVAL ;Signature · 1 line
2.45.2
Signature · 2 lines
Sincerely yours, Mike.
Re: [RFC PATCH 5/8] kvm: gmem: add option to remove guest private memory from direct map
Quoted 102 lines · Mike Rapoport
CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.
Quoted 97 lines · Patrick Roy
While
guest_memfdis not available to be mapped by userspace, it is still accessible through the kernel's direct map. This means that in scenarios where guest-private memory is not hardware protected, it can be speculatively read and its contents potentially leaked through hardware side-channels. Removing guest-private memory from the direct map, thus mitigates a large class of speculative execution issues [1, Table 1].This patch adds a flag to the
KVM_CREATE_GUEST_MEMFDwhich, if set, removes the struct pages backing guest-private memory from the direct map. ShouldCONFIG_HAVE_KVM_GMEM_{INVALIDATE, PREPARE}be set, pages are removed after preparation and before invalidation, so that the prepare/invalidate routines do not have to worry about potentially absent direct map entries.Direct map removal do not reuse the
KVM_GMEM_PREPAREmachinery, sincepreparecan be called multiple time, and it is the responsibility of the preparation routine to not "prepare" the same folio twice [2]. Thus, instead explicitly check iffilemap_grab_folioallocated a new folio, and
remove the returned folio from the direct map only if this was the case.The patch uses
release_folioinstead offree_folioto reinsert pages back into the direct map as by the timefree_foliois called,folio->mappingcan already be NULL. This means that a call tofolio_inodeinsidefree_foliomight deference a NULL pointer, leaving no way to access the inode which stores the flags that allow determining whether the page was removed from the direct map in the first place.Lastly, the patch uses set_direct_map_{invalid,default}_noflush instead of
set_memory_[n]pto avoid expensive flushes of TLBs and the L*-cache hierarchy. This is especially important once KVM restores direct map entries on-demand in later patches, where simple FIO benchmarks of a virtio-blk device have shown that TLB flushes on a Intel(R) Xeon(R) Platinum 8375C CPU @ 2.90GHz resulted in 80% degradation in throughput compared to a non-flushing solution.Not flushing the TLB means that until TLB entries for temporarily restored direct map entries get naturally evicted, they can be used during speculative execution, and effectively "unhide" the memory for longer than intended. We consider this acceptable, as the only pages that are temporarily reinserted into the direct map like this will either hold PV data structures (kvm-clock, asyncpf, etc), or pages containing privileged instructions inside the guest kernel image (in the MMIO emulation case).
[1]: https://download.vusec.net/papers/quarantine_raid23.pdf
Signed-off-by Patrick Roy <[email protected]>--- include/uapi/linux/kvm.h | 2 ++ virt/kvm/guest_memfd.c | 52 ++++++++++++++++++++++++++++++++++------ 2 files changed, 47 insertions(+), 7 deletions(-)include/uapi/linux/kvm.h +2
-0@@ -1563,4 +1563,6 @@ struct kvm_create_guest_memfd { __u64 reserved[6]; }; +#define KVM_GMEM_NO_DIRECT_MAP (1ULL << 0) +#endif /* __LINUX_KVM_H */ virt/kvm/guest_memfd.c +8
-0@@ -4,6 +4,7 @@#include <linux/kvm_host.h> #include <linux/pagemap.h> #include <linux/anon_inodes.h> +#include <linux/set_memory.h> #include "kvm_mm.h" @@ -49,9 +50,16 @@ static int kvm_gmem_prepare_folio(struct inode *inode, pgoff_t index, struct folreturn 0 ; } +static bool kvm_gmem_not_present (struct inode *inode) +{ +return ((unsigned long )inode->i_private &KVM_GMEM_NO_DIRECT_MAP ) !=0 ; +} +static struct folio *kvm_gmem_get_folio (struct inode *inode,pgoff_t index,bool prepare) {struct folio *folio; +bool zap_direct_map = false; +int r;/* TODO: Support huge pages. */ folio =filemap_grab_folio (inode->i_mapping, index);@@ -74,16 +82,30 @@ static struct folio *kvm_gmem_get_folio(struct inode *inode, pgoff_t index, boolfor (i =0 ; i < nr_pages; i++)clear_highpage (folio_page (folio, i));
Quoted 5 lines
virt/kvm/guest_memfd.c static struct folio *kvm_gmem_get_folio(struct inode *inode, pgoff_t index, bool · line 86
Quoted 2 lines
virt/kvm/guest_memfd.c +2
-0+// We need to clear the folio before calling kvm_gmem_prepare_folio, +// but can only remove it from the direct map _after_ preparation is done. No C++ comments please
Ack, sorry, will avoid in the future!
Quoted 14 lines
Quoted 14 lines
virt/kvm/guest_memfd.c +4
-6+ zap_direct_map =kvm_gmem_not_present (inode); +folio_mark_uptodate (folio); }if (prepare) { -int r =kvm_gmem_prepare_folio (inode, index, folio); -if (r <0 ) { -folio_unlock (folio); -folio_put (folio); -return ERR_PTR (r); - } + r =kvm_gmem_prepare_folio (inode, index, folio); +if (r <0 )
Quoted 9 lines
virt/kvm/guest_memfd.c static struct folio *kvm_gmem_get_folio(struct inode *inode, pgoff_t index, bool · line 99
Quoted 5 lines
virt/kvm/guest_memfd.c +5
-0+ goto out_err; + } + +if (zap_direct_map) { + r =set_direct_map_invalid_noflush (&folio->page);It's not future proof to presume that folio is a single page here. You should loop over folio pages and add a TLB flush after the loop.
Right, will do the folio iteration thing (and same for all other places I call the direct_map* functions in this RFC)!
I'll also have a look at the TLB flush. I specifically avoided using set_memory_np here because the flushes it did (TLB + L1/2/3) had significant performance impact (see cover letter). I'll have to rerun my benchmark with set_direct_map_invalid_noflush + flush_tlb_kernel_range instead, but if the result is similar, and we really need the flush here for correctness, I might have to go back to the drawing board about this whole on-demand mapping approach :(
Quoted 65 lines
Quoted 24 lines
virt/kvm/guest_memfd.c +13
-0+if (r <0 ) + goto out_err; + +// We use the private flag to track whether the folio has been removed +// from the direct map. This is because inside of ->free_folio, +// we do not have access to the address_space anymore, meaning we +// cannot check folio_inode(folio)->i_private to determine whether +// KVM_GMEM_NO_DIRECT_MAP was set. +folio_set_private (folio); }/* @@ -91,6 +113,10 @@ static struct folio *kvm_gmem_get_folio(struct inode *inode, pgoff_t index, bool* unevictable and there is no storage to write back to. */ return folio; +out_err: +folio_unlock (folio); +folio_put (folio); +return ERR_PTR (r); } static void kvm_gmem_invalidate_begin(struct kvm_gmem *gmem, pgoff_t start,virt/kvm/guest_memfd.c static void kvm_gmem_free_folio(struct folio *folio) · line 388
Quoted 9 lines
virt/kvm/guest_memfd.c +6
-0@@ -354,10 +380,22 @@ static void kvm_gmem_free_folio(struct folio *folio) }#endif +static void kvm_gmem_invalidate_folio (struct folio *folio,size_t start,size_t end) +{ +if (start ==0 && end ==PAGE_SIZE ) { +// We only get here if PG_private is set, which only happens if kvm_gmem_not_present +// returned true in kvm_gmem_get_folio. Thus no need to do that check again. +BUG_ON (set_direct_map_default_noflush (&folio->page));Ditto.
Quoted 23 lines
virt/kvm/guest_memfd.c +7
-2+ +folio_clear_private (folio); + } +} +static const struct address_space_operations kvm_gmem_aops = { .dirty_folio = noop_dirty_folio, .migrate_folio = kvm_gmem_migrate_folio, .error_remove_folio = kvm_gmem_error_folio, + .invalidate_folio = kvm_gmem_invalidate_folio, #ifdef CONFIG_HAVE_KVM_GMEM_INVALIDATE .free_folio = kvm_gmem_free_folio, #endif@@ -443,7 +481,7 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args) {loff_t size = args->size;u64 flags = args->flags; -u64 valid_flags =0 ; +u64 valid_flags =KVM_GMEM_NO_DIRECT_MAP ;if (flags & ~valid_flags)return -EINVAL ; --Quoted 2 lines
2.45.2
--
Sincerely yours,
Mike.
RFC PATCH 6/8 kvm: gmem: Temporarily restore direct map entries when needed
In reply to Patrick RoyIf KVM_GMEM_NO_DIRECT_MAP is set, and KVM tries to internally access guest-private memory inside kvm_{read,write}_guest, or via a gfn_to_pfn_cache, temporarily restore the direct map entry.
To avoid race conditions between two threads restoring or zapping direct map entries for the same page and potentially interfering with each other (e.g. unfortune interweavings of map->read->unmap in the form of map(A)->map(B)->read(A)->unmap(A)->read(B) [BOOM]), the following invariant is upheld in this patch:
- Only a single gfn_to_pfn_cache can exist for any given pfn, and
- All non-gfn_to_pfn_cache code paths that temporarily restore direct map entries complete the entire map->access->unmap critical section
while holding the folio lock.
To remember whether a given folio currently has a direct map entry, use the PG_private flag. If this flag is set, then the folio is removed from the direct map, otherwise it is present in the direct map. Modifications of this flag, together with the corresponding direct map manipulations, must happen while holding the folio's lock.
A gfn_to_pfn_cache cannot hold the folio lock for its entire lifetime, so it operates as follows: In gpc_map, under folio lock, restore the direct map entry and set PG_private to 0. In gpc_unmap, zap the direct map entry again and set PG_private back to 1.
If inside gpc_map the cache finds a folio that has PG_private set to 0, it knows that another gfn_to_pfn_cache is currently active for the given pfn (as this is the only scenario in which PG_private can be 0 without the folio lock being held), and so it returns -EINVAL.
The only other interesting scenario is then if kvm_{read,write}_guest is called for a gfn whose translation is currently cached inside a gfn_to_pfn_cache. In this case, kvm_{read,write}_guest notices that PG_private is 0 and skips all direct map manipulations. Since it is holding the folio lock, it can be sure that gpc_unmap cannot concurrently zap the direct map entries while kvm_{read,write}_guest still needs them.
Note that this implementation is slightly too restrictive, as sometimes multiple gfn_to_pfn_caches need to be active for the same gfn (for example, each vCPU has its own kvm-clock structure, which they all try to put into the same gfn).
--- virt/kvm/kvm_main.c | 59 +++++++++++++++++++++--------- virt/kvm/pfncache.c | 89 +++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 123 insertions(+), 25 deletions(-)
virt/kvm/kvm_main.c +41 -18
@@ -52,6 +52,7 @@#include <asm/processor.h> #include <asm/ioctl.h> +#include <asm/set_memory.h> #include <linux/uaccess.h> #include "coalesced_mmio.h"
@@ -3291,8 +3292,8 @@ static int __kvm_read_guest_private_page(struct kvm *kvm, void *data, int offset, int len) { kvm_pfn_t pfn; -int r; -struct page *page; +int r =0 ; +struct folio *folio;void *kaddr;if (!kvm_can_access_gmem (kvm))
@@ -3303,13 +3304,24 @@ static int __kvm_read_guest_private_page(struct kvm *kvm,if (r <0 )return -EFAULT ; - page =pfn_to_page (pfn); -lock_page (page); - kaddr =page_address (page) + offset; -memcpy (data, kaddr, len); -unlock_page (page); -put_page (page); -return 0 ; + folio =pfn_folio (pfn); +folio_lock (folio); + kaddr =folio_address (folio); +if (folio_test_private (folio)) { + r =set_direct_map_default_noflush (&folio->page); +if (r) + goto out_unlock; + } +memcpy (data, kaddr + offset, len); +if (folio_test_private (folio)) { + r =set_direct_map_invalid_noflush (&folio->page); +if (r) + goto out_unlock; + } +out_unlock: +folio_unlock (folio); +folio_put (folio); +return r; } static int __kvm_vcpu_read_guest_private_page(struct kvm_vcpu *vcpu,
@@ -3437,8 +3449,8 @@ static int __kvm_write_guest_private_page(struct kvm *kvm, const void *data, int offset, int len) { kvm_pfn_t pfn; -int r; -struct page *page; +int r =0 ; +struct folio *folio;void *kaddr;if (!kvm_can_access_gmem (kvm))
@@ -3449,14 +3461,25 @@ static int __kvm_write_guest_private_page(struct kvm *kvm,if (r <0 )return -EFAULT ; - page =pfn_to_page (pfn); -lock_page (page); - kaddr =page_address (page) + offset; -memcpy (kaddr, data, len); -unlock_page (page); -put_page (page); + folio =pfn_folio (pfn); +folio_lock (folio); + kaddr =folio_address (folio); +if (folio_test_private (folio)) { + r =set_direct_map_default_noflush (&folio->page); +if (r) + goto out_unlock; + } +memcpy (kaddr + offset, data, len); +if (folio_test_private (folio)) { + r =set_direct_map_invalid_noflush (&folio->page); +if (r) + goto out_unlock; + } -return 0 ; +out_unlock: +folio_unlock (folio); +folio_put (folio); +return r; } static int __kvm_vcpu_write_guest_private_page(struct kvm_vcpu *vcpu,
virt/kvm/pfncache.c +82 -7
138 lines not on this page · raw message
Signature · 1 line
2.45.2
Paolo Bonzini
maintainerRe: [RFC PATCH 6/8] kvm: gmem: Temporarily restore direct map entries when needed
Quoted 11 lines · Patrick Roy
If
KVM_GMEM_NO_DIRECT_MAPis set, and KVM tries to internally access guest-private memory inside kvm_{read,write}_guest, or via agfn_to_pfn_cache, temporarily restore the direct map entry.To avoid race conditions between two threads restoring or zapping direct map entries for the same page and potentially interfering with each other (e.g. unfortune interweavings of
map->read->unmapin the form of map(A)->map(B)->read(A)->unmap(A)->read(B) [BOOM]), the following invariant is upheld in this patch:- Only a single
gfn_to_pfn_cachecan exist for any given pfn, and
I think this is not ensured. You can however use set_page_private()/page_private() to count the number of references.
Paolo
Quoted 3 lines
- All non-gfn_to_pfn_cache code paths that temporarily restore direct map entries complete the entire
map->access->unmapcritical section
while holding the folio lock.
RFC PATCH 7/8 mm: secretmem: use AS_INACCESSIBLE to prohibit GUP
In reply to Patrick RoyInside of vma_is_secretmem and secretmem_mapping, instead of checking whether a vm_area_struct/address_space has the secretmem ops structure attached to it, check whether the address_space has the AS_INACCESSIBLE bit set. Then set the AS_INACCESSIBLE flag for secretmem's address_space.
This means that get_user_pages and friends are disables for all adress_spaces that set AS_INACCESIBLE. The AS_INACCESSIBLE flag was introduced in commit c72ceafbd12c ("mm: Introduce AS_INACCESSIBLE for encrypted/confidential memory") specifically for guest_memfd to indicate that no reads and writes should ever be done to guest_memfd address_spaces. Disallowing gup seems like a reasonable semantic extension, and means that potential future mmaps of guest_memfd cannot be GUP'd.
--- include/linux/secretmem.h | 13 +++++++++++-- mm/secretmem.c | 6 +----- 2 files changed, 12 insertions(+), 7 deletions(-)
include/linux/secretmem.h +11 -2
@@ -8,10 +8,19 @@ extern const struct address_space_operations secretmem_aops;static inline bool secretmem_mapping (struct address_space *mapping) { -return mapping->a_ops == &secretmem_aops; +return mapping->flags &AS_INACCESSIBLE ; +} + +static inline bool vma_is_secretmem (struct vm_area_struct *vma) +{ +struct file *file = vma->vm_file; + +if (!file) +return false; + +return secretmem_mapping (file->f_inode->i_mapping); } -bool vma_is_secretmem (struct vm_area_struct *vma);bool secretmem_active (void );#else
mm/secretmem.c +1 -5
@@ -136,11 +136,6 @@ static int secretmem_mmap(struct file *file, struct vm_area_struct *vma)return 0 ; } -bool vma_is_secretmem (struct vm_area_struct *vma) -{ -return vma->vm_ops == &secretmem_vm_ops; -} -static const struct file_operations secretmem_fops = { .release = secretmem_release, .mmap = secretmem_mmap,
@@ -218,6 +213,7 @@ static struct file *secretmem_file_create(unsigned long flags) inode->i_op = &secretmem_iops; inode->i_mapping->a_ops = &secretmem_aops; + inode->i_mapping->flags |=AS_INACCESSIBLE ;/* pretend we are a normal file with zero size */ inode->i_mode |=S_IFREG ;
Signature · 1 line
2.45.2
Re: [RFC PATCH 7/8] mm: secretmem: use AS_INACCESSIBLE to prohibit GUP
Quoted 32 lines · Patrick Roy
Inside of
vma_is_secretmemandsecretmem_mapping, instead of checking whether a vm_area_struct/address_space has the secretmem ops structure attached to it, check whether theaddress_spacehas theAS_INACCESSIBLEbit set. Then set theAS_INACCESSIBLEflag for secretmem'saddress_space.This means that
get_user_pagesand friends are disables for alladress_spacesthat setAS_INACCESIBLE. TheAS_INACCESSIBLEflag was introduced in commitc72ceafbd12c("mm: IntroduceAS_INACCESSIBLEfor encrypted/confidential memory") specifically forguest_memfdto indicate that no reads and writes should ever be done toguest_memfdaddress_spaces. Disallowing gup seems like a reasonable semantic extension, and means that potential future mmaps ofguest_memfdcannot be GUP'd.Signed-off-by Patrick Roy <[email protected]>--- include/linux/secretmem.h | 13 +++++++++++-- mm/secretmem.c | 6 +----- 2 files changed, 12 insertions(+), 7 deletions(-)include/linux/secretmem.h +2
-1@@ -8,10 +8,19 @@ extern const struct address_space_operations secretmem_aops;static inline bool secretmem_mapping (struct address_space *mapping) { -return mapping->a_ops == &secretmem_aops; +return mapping->flags &AS_INACCESSIBLE ; +}
include/linux/secretmem.h extern const struct address_space_operations secretmem_aops; · line 22
Quoted 10 lines
include/linux/secretmem.h +9
-0+ +static inline bool vma_is_secretmem (struct vm_area_struct *vma) +{ +struct file *file = vma->vm_file; + +if (!file) +return false; + +return secretmem_mapping (file->f_inode->i_mapping); }
That sounds wrong. You should leave *secretmem alone and instead have something like inaccessible_mapping that is used where appropriate.
vma_is_secretmem() should not suddenly succeed on something that is not mm/secretmem.c
Signature · 3 lines
Cheers, David / dhildenb
Mike Rapoport
maintainerRe: [RFC PATCH 7/8] mm: secretmem: use AS_INACCESSIBLE to prohibit GUP
Quoted 46 lines · David Hildenbrand
Quoted 31 lines · Patrick Roy
Inside of
vma_is_secretmemandsecretmem_mapping, instead of checking whether a vm_area_struct/address_space has the secretmem ops structure attached to it, check whether theaddress_spacehas theAS_INACCESSIBLEbit set. Then set theAS_INACCESSIBLEflag for secretmem'saddress_space.This means that
get_user_pagesand friends are disables for alladress_spacesthat setAS_INACCESIBLE. TheAS_INACCESSIBLEflag was introduced in commitc72ceafbd12c("mm: IntroduceAS_INACCESSIBLEfor encrypted/confidential memory") specifically forguest_memfdto indicate that no reads and writes should ever be done toguest_memfdaddress_spaces. Disallowing gup seems like a reasonable semantic extension, and means that potential future mmaps ofguest_memfdcannot be GUP'd.Signed-off-by Patrick Roy <[email protected]>--- include/linux/secretmem.h | 13 +++++++++++-- mm/secretmem.c | 6 +----- 2 files changed, 12 insertions(+), 7 deletions(-)include/linux/secretmem.h +2
-1@@ -8,10 +8,19 @@ extern const struct address_space_operations secretmem_aops;static inline bool secretmem_mapping (struct address_space *mapping) { -return mapping->a_ops == &secretmem_aops; +return mapping->flags &AS_INACCESSIBLE ; +}include/linux/secretmem.h extern const struct address_space_operations secretmem_aops; · line 21
Quoted 10 lines
include/linux/secretmem.h +9
-0+ +static inline bool vma_is_secretmem (struct vm_area_struct *vma) +{ +struct file *file = vma->vm_file; + +if (!file) +return false; + +return secretmem_mapping (file->f_inode->i_mapping); }That sounds wrong. You should leave *secretmem alone and instead have something like
inaccessible_mappingthat is used where appropriate.
Quoted 2 lines
vma_is_secretmem()should not suddenly succeed on something that is notmm/secretmem.c
I'm with David here.
Quoted 5 lines
Signature · 3 lines
Cheers, David / dhildenb
Signature · 2 lines
Sincerely yours, Mike.
Re: [RFC PATCH 7/8] mm: secretmem: use AS_INACCESSIBLE to prohibit GUP
Quoted 51 lines · Mike Rapoport
CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.
Quoted 46 lines · David Hildenbrand
Quoted 31 lines · Patrick Roy
Inside of
vma_is_secretmemandsecretmem_mapping, instead of checking whether a vm_area_struct/address_space has the secretmem ops structure attached to it, check whether theaddress_spacehas theAS_INACCESSIBLEbit set. Then set theAS_INACCESSIBLEflag for secretmem'saddress_space.This means that
get_user_pagesand friends are disables for alladress_spacesthat setAS_INACCESIBLE. TheAS_INACCESSIBLEflag was introduced in commitc72ceafbd12c("mm: IntroduceAS_INACCESSIBLEfor encrypted/confidential memory") specifically forguest_memfdto indicate that no reads and writes should ever be done toguest_memfdaddress_spaces. Disallowing gup seems like a reasonable semantic extension, and means that potential future mmaps ofguest_memfdcannot be GUP'd.Signed-off-by Patrick Roy <[email protected]>--- include/linux/secretmem.h | 13 +++++++++++-- mm/secretmem.c | 6 +----- 2 files changed, 12 insertions(+), 7 deletions(-)include/linux/secretmem.h +2
-1@@ -8,10 +8,19 @@ extern const struct address_space_operations secretmem_aops;static inline bool secretmem_mapping (struct address_space *mapping) { -return mapping->a_ops == &secretmem_aops; +return mapping->flags &AS_INACCESSIBLE ; +}include/linux/secretmem.h extern const struct address_space_operations secretmem_aops; · line 21
Quoted 10 lines
include/linux/secretmem.h +9
-0+ +static inline bool vma_is_secretmem (struct vm_area_struct *vma) +{ +struct file *file = vma->vm_file; + +if (!file) +return false; + +return secretmem_mapping (file->f_inode->i_mapping); }That sounds wrong. You should leave *secretmem alone and instead have something like
inaccessible_mappingthat is used where appropriate.
Quoted 5 lines
Quoted 2 lines
vma_is_secretmem()should not suddenly succeed on something that is notmm/secretmem.cI'm with David here.
Right, that makes sense. My thinking here was that if memfd_secret and potential mappings of guest_memfd have the same behavior wrt GUP, then it makes sense to just have them rely on the same checks. But I guess I didn't follow that thought to its logical conclusion of renaming the "secretmem" checks into "inaccessible" checks and moving them out of secretmem.h.
Or do you mean to just leave secretmem untouched and add separate "inaccessible" checks? But then we'd have two different ways of disabling GUP for specific VMAs that both rely on checks in exactly the same places :/
Quoted 9 lines
Quoted 5 lines
--
Cheers,David / dhildenb
--
Sincerely yours,
Mike.
Re: [RFC PATCH 7/8] mm: secretmem: use AS_INACCESSIBLE to prohibit GUP
Quoted 67 lines · Patrick Roy
Quoted 51 lines · Mike Rapoport
CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.
Quoted 46 lines · David Hildenbrand
Quoted 31 lines · Patrick Roy
Inside of
vma_is_secretmemandsecretmem_mapping, instead of checking whether a vm_area_struct/address_space has the secretmem ops structure attached to it, check whether theaddress_spacehas theAS_INACCESSIBLEbit set. Then set theAS_INACCESSIBLEflag for secretmem'saddress_space.This means that
get_user_pagesand friends are disables for alladress_spacesthat setAS_INACCESIBLE. TheAS_INACCESSIBLEflag was introduced in commitc72ceafbd12c("mm: IntroduceAS_INACCESSIBLEfor encrypted/confidential memory") specifically forguest_memfdto indicate that no reads and writes should ever be done toguest_memfdaddress_spaces. Disallowing gup seems like a reasonable semantic extension, and means that potential future mmaps ofguest_memfdcannot be GUP'd.Signed-off-by Patrick Roy <[email protected]>--- include/linux/secretmem.h | 13 +++++++++++-- mm/secretmem.c | 6 +----- 2 files changed, 12 insertions(+), 7 deletions(-)include/linux/secretmem.h +2
-1@@ -8,10 +8,19 @@ extern const struct address_space_operations secretmem_aops;static inline bool secretmem_mapping (struct address_space *mapping) { -return mapping->a_ops == &secretmem_aops; +return mapping->flags &AS_INACCESSIBLE ; +}include/linux/secretmem.h extern const struct address_space_operations secretmem_aops; · line 21
Quoted 10 lines
include/linux/secretmem.h +9
-0+ +static inline bool vma_is_secretmem (struct vm_area_struct *vma) +{ +struct file *file = vma->vm_file; + +if (!file) +return false; + +return secretmem_mapping (file->f_inode->i_mapping); }That sounds wrong. You should leave *secretmem alone and instead have something like
inaccessible_mappingthat is used where appropriate.Quoted 5 lines
Quoted 2 lines
vma_is_secretmem()should not suddenly succeed on something that is notmm/secretmem.cI'm with David here.
Right, that makes sense. My thinking here was that if
memfd_secretand potential mappings ofguest_memfdhave the same behavior wrt GUP, then it makes sense to just have them rely on the same checks. But I guess I didn't follow that thought to its logical conclusion of renaming the "secretmem" checks into "inaccessible" checks and moving them out ofsecretmem.h.
Quoted 4 lines
Or do you mean to just leave secretmem untouched and add separate "inaccessible" checks? But then we'd have two different ways of disabling GUP for specific VMAs that both rely on checks in exactly the same places :/
You can just replace the vma_is_secretmem in relevant places by checks if inaccessible address spaces. No need for the additional vma_is_secretmem check then.
BUT, as raised in my other reply, I wonder if adding support for secretmem in KVM (I assume) would be simpler+cleaner.
Quoted 11 lines
Quoted 10 lines
Quoted 6 lines
--
Cheers,David / dhildenb
--
Sincerely yours,
Mike.
Signature · 3 lines
Cheers, David / dhildenb
RFC PATCH 8/8 kvm: gmem: Allow restricted userspace mappings
In reply to Patrick RoyAllow mapping guest_memfd into userspace. Since AS_INACCESSIBLE is set on the underlying address_space struct, no GUP of guest_memfd will be possible.
--- virt/kvm/guest_memfd.c | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-)
virt/kvm/guest_memfd.c +30 -1
@@ -319,7 +319,37 @@ static inline struct file *kvm_gmem_get_file(struct kvm_memory_slot *slot)return get_file_active (&slot->gmem.file); } +static vm_fault_t kvm_gmem_fault (struct vm_fault *vmf) +{ +struct folio *folio; + + folio =kvm_gmem_get_folio (file_inode (vmf->vma->vm_file), vmf->pgoff, true); + +if (!folio) +return VM_FAULT_SIGBUS ; + + vmf->page =folio_file_page (folio, vmf->pgoff); + +return VM_FAULT_LOCKED ; +} + +static const struct vm_operations_struct kvm_gmem_vm_ops = { + .fault = kvm_gmem_fault +}; + +static int kvm_gmem_mmap (struct file *file,struct vm_area_struct *vma) +{ +if ((vma->vm_flags & (VM_SHARED |VM_MAYSHARE )) ==0 ) +return -EINVAL ; + +vm_flags_set (vma,VM_DONTDUMP ); + vma->vm_ops = &kvm_gmem_vm_ops; + +return 0 ; +} +static struct file_operations kvm_gmem_fops = { + .mmap = kvm_gmem_mmap, .open = generic_file_open, .release = kvm_gmem_release, .fallocate = kvm_gmem_fallocate,
@@ -594,7 +624,6 @@ static int __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot,return -EFAULT ; } - gmem = file->private_data;if (xa_load (&gmem->bindings, index) != slot) {WARN_ON_ONCE (xa_load (&gmem->bindings, index));return -EIO ;
Signature · 1 line
2.45.2
Re: [RFC PATCH 8/8] kvm: gmem: Allow restricted userspace mappings
Hi Patrick,
Quoted 4 lines · Patrick Roy
Allow mapping
guest_memfdinto userspace. SinceAS_INACCESSIBLEis set on the underlyingaddress_spacestruct, no GUP ofguest_memfdwill be possible.
This patch allows mapping guest_memfd() unconditionally. Even if it's not guppable, there are other reasons why you wouldn't want to allow this. Maybe a config flag to gate it? e.g.,
https://lore.kernel.org/all/[email protected]/
Quoted 49 lines
Signed-off-by Patrick Roy <[email protected]>--- virt/kvm/guest_memfd.c | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-)virt/kvm/guest_memfd.c +30
-0@@ -319,7 +319,37 @@ static inline struct file *kvm_gmem_get_file(struct kvm_memory_slot *slot)return get_file_active (&slot->gmem.file); } +static vm_fault_t kvm_gmem_fault (struct vm_fault *vmf) +{ +struct folio *folio; + + folio =kvm_gmem_get_folio (file_inode (vmf->vma->vm_file), vmf->pgoff, true); + +if (!folio) +return VM_FAULT_SIGBUS ; + + vmf->page =folio_file_page (folio, vmf->pgoff); + +return VM_FAULT_LOCKED ; +} + +static const struct vm_operations_struct kvm_gmem_vm_ops = { + .fault = kvm_gmem_fault +}; + +static int kvm_gmem_mmap (struct file *file,struct vm_area_struct *vma) +{ +if ((vma->vm_flags & (VM_SHARED |VM_MAYSHARE )) ==0 ) +return -EINVAL ; + +vm_flags_set (vma,VM_DONTDUMP ); + vma->vm_ops = &kvm_gmem_vm_ops; + +return 0 ; +} +static struct file_operations kvm_gmem_fops = { + .mmap = kvm_gmem_mmap, .open = generic_file_open, .release = kvm_gmem_release, .fallocate = kvm_gmem_fallocate,
virt/kvm/guest_memfd.c static int __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot, · line 597
Quoted 4 lines
virt/kvm/guest_memfd.c +0
-1@@ -594,7 +624,6 @@ static int __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot,return -EFAULT ; } - gmem = file->private_data;
Is this intentional?
Cheers,
/fuad
Quoted 6 lines
virt/kvm/guest_memfd.c +0
-1if (xa_load (&gmem->bindings, index) != slot) {WARN_ON_ONCE (xa_load (&gmem->bindings, index));return -EIO ; --2.45.2
Re: [RFC PATCH 8/8] kvm: gmem: Allow restricted userspace mappings
Quoted 11 lines · Fuad Tabba
Hi Patrick,
Quoted 4 lines · Patrick Roy
Allow mapping
guest_memfdinto userspace. SinceAS_INACCESSIBLEis set on the underlyingaddress_spacestruct, no GUP ofguest_memfdwill be possible.This patch allows mapping
guest_memfd()unconditionally. Even if it's not guppable, there are other reasons why you wouldn't want to allow this. Maybe a config flag to gate it? e.g.,
As discussed with Jason, maybe not the direction we want to take with guest_memfd. If it's private memory, it shall not be mapped. Also not via magic config options.
We'll likely discuss some of that in the meeting MM tomorrow I guess (having both shared and private memory in guest_memfd).
Note that just from staring at this commit, I don't understand the motivation *why* we would want to do that.
Signature · 3 lines
Cheers, David / dhildenb
Re: [RFC PATCH 8/8] kvm: gmem: Allow restricted userspace mappings
Quoted 18 lines · David Hildenbrand
CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.
Quoted 11 lines · Fuad Tabba
Hi Patrick,
Quoted 4 lines · Patrick Roy
Allow mapping
guest_memfdinto userspace. SinceAS_INACCESSIBLEis set on the underlyingaddress_spacestruct, no GUP ofguest_memfdwill be possible.This patch allows mapping
guest_memfd()unconditionally. Even if it's not guppable, there are other reasons why you wouldn't want to allow this. Maybe a config flag to gate it? e.g.,
Quoted 7 lines
As discussed with Jason, maybe not the direction we want to take with
guest_memfd.
If it's private memory, it shall not be mapped. Also not via magic config options.We'll likely discuss some of that in the meeting MM tomorrow I guess (having both shared and private memory in
guest_memfd).
Oh, nice. I'm assuming you mean this meeting:
https://lore.kernel.org/linux-mm/[email protected]/T/?
Would it be okay if I also attend? I see it also mentions huge pages, which is another thing we are interested in, actually :)
Quoted 2 lines
Note that just from staring at this commit, I don't understand the motivation *why* we would want to do that.
Fair - I admittedly didn't get into that as much as I probably should have. In our usecase, we do not have anything that pKVM would (I think) call "guest-private" memory. I think our memory can be better described as guest-owned, but always shared with the VMM (e.g. userspace), but ideally never shared with the host kernel. This model lets us do a lot of simplifying assumptions: Things like I/O can be handled in userspace without the guest explicitly sharing I/O buffers (which is not exactly what we would want long-term anyway, as sharing in the guest_memfd context means sharing with the host kernel), we can easily do VM snapshotting without needing things like TDX's TDH.EXPORT.MEM APIs, etc.
Quoted 5 lines
Signature · 3 lines
Cheers, David / dhildenb
Re: [RFC PATCH 8/8] kvm: gmem: Allow restricted userspace mappings
Quoted 26 lines · Patrick Roy
Quoted 23 lines · David Hildenbrand
CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.
Quoted 11 lines · Fuad Tabba
Hi Patrick,
Quoted 4 lines · Patrick Roy
Allow mapping
guest_memfdinto userspace. SinceAS_INACCESSIBLEis set on the underlyingaddress_spacestruct, no GUP ofguest_memfdwill be possible.This patch allows mapping
guest_memfd()unconditionally. Even if it's not guppable, there are other reasons why you wouldn't want to allow this. Maybe a config flag to gate it? e.g.,As discussed with Jason, maybe not the direction we want to take with
guest_memfd.
If it's private memory, it shall not be mapped. Also not via magic config options.
Quoted 7 lines
Quoted 2 lines
We'll likely discuss some of that in the meeting MM tomorrow I guess (having both shared and private memory in
guest_memfd).Oh, nice. I'm assuming you mean this meeting:
https://lore.kernel.org/linux-mm/[email protected]/T/?
Would it be okay if I also attend? I see it also mentions huge pages, which is another thing we are interested in, actually :)
Hi,
sorry for the late reply. Yes, you could have joined .... too late. There will be a summary posted soon. So far the agreement is that we're planning on allowing shared memory as part guest_memfd, and will allow that to get mapped and pinned. Private memory is not going to get mapped and pinned.
If we have to disallow pinning of shared memory on top for some use cases (i.e., no directmap), I assume that could be added.
Quoted 4 lines
Quoted 3 lines
Note that just from staring at this commit, I don't understand the motivation *why* we would want to do that.
Quoted 10 lines
Fair - I admittedly didn't get into that as much as I probably should have. In our usecase, we do not have anything that pKVM would (I think) call "guest-private" memory. I think our memory can be better described as guest-owned, but always shared with the VMM (e.g. userspace), but ideally never shared with the host kernel. This model lets us do a lot of simplifying assumptions: Things like I/O can be handled in userspace without the guest explicitly sharing I/O buffers (which is not exactly what we would want long-term anyway, as sharing in the
guest_memfdcontext means sharing with the host kernel), we can easily do VM snapshotting without needing things like TDX's TDH.EXPORT.MEM APIs, etc.
Okay, so essentially you would want to use guest_memfd to only contain shard memory and disallow any pinning like for secretmem.
If so, I wonder if it wouldn't be better to simply add KVM support to consume *real* secretmem memory? IIRC so far there was only demand to probably remove the directmap of private memory in guest_memfd, not of shared memory.
Signature · 3 lines
Cheers, David / dhildenb
Sean Christopherson
maintainerRe: [RFC PATCH 8/8] kvm: gmem: Allow restricted userspace mappings
Quoted 16 lines · David Hildenbrand
Quoted 4 lines · Patrick Roy
Quoted 2 lines · David Hildenbrand
Note that just from staring at this commit, I don't understand the motivation *why* we would want to do that.
Quoted 10 lines
Fair - I admittedly didn't get into that as much as I probably should have. In our usecase, we do not have anything that pKVM would (I think) call "guest-private" memory. I think our memory can be better described as guest-owned, but always shared with the VMM (e.g. userspace), but ideally never shared with the host kernel. This model lets us do a lot of simplifying assumptions: Things like I/O can be handled in userspace without the guest explicitly sharing I/O buffers (which is not exactly what we would want long-term anyway, as sharing in the
guest_memfdcontext means sharing with the host kernel), we can easily do VM snapshotting without needing things like TDX's TDH.EXPORT.MEM APIs, etc.
Quoted 7 lines
Okay, so essentially you would want to use
guest_memfdto only contain shard memory and disallow any pinning like for secretmem.If so, I wonder if it wouldn't be better to simply add KVM support to consume *real* secretmem memory? IIRC so far there was only demand to probably remove the directmap of private memory in
guest_memfd, not of shared memory.
It's also desirable to remove shared memory from the directmap, e.g. to prevent using the directmap in a cross-VM attack.
I don't think we want to allow KVM to consume secretmem. That would require letting KVM gup() secretmem, which AIUI defeats the entire purpose of secretmem, and I don't think KVM should be special.
Re: [RFC PATCH 8/8] kvm: gmem: Allow restricted userspace mappings
Quoted 25 lines · Sean Christopherson
Quoted 16 lines · David Hildenbrand
Quoted 4 lines · Patrick Roy
Quoted 2 lines · David Hildenbrand
Note that just from staring at this commit, I don't understand the motivation *why* we would want to do that.
Quoted 10 lines
Fair - I admittedly didn't get into that as much as I probably should have. In our usecase, we do not have anything that pKVM would (I think) call "guest-private" memory. I think our memory can be better described as guest-owned, but always shared with the VMM (e.g. userspace), but ideally never shared with the host kernel. This model lets us do a lot of simplifying assumptions: Things like I/O can be handled in userspace without the guest explicitly sharing I/O buffers (which is not exactly what we would want long-term anyway, as sharing in the
guest_memfdcontext means sharing with the host kernel), we can easily do VM snapshotting without needing things like TDX's TDH.EXPORT.MEM APIs, etc.Quoted 7 lines
Okay, so essentially you would want to use
guest_memfdto only contain shard memory and disallow any pinning like for secretmem.If so, I wonder if it wouldn't be better to simply add KVM support to consume *real* secretmem memory? IIRC so far there was only demand to probably remove the directmap of private memory in
guest_memfd, not of shared memory.
Quoted 6 lines
It's also desirable to remove shared memory from the directmap, e.g. to prevent using the directmap in a cross-VM attack.
I don't think we want to allow KVM to consume secretmem. That would require letting KVM
gup()secretmem, which AIUI defeats the entire purpose of secretmem, and I don't think KVM should be special.
I would mean consuming secretmem via the fd, *not* via page tables / gup.
But if we also want to have the option of directmap modifications for shared memory in guest_memfd, we could make that indeed a guest_memfd feature.
Signature · 3 lines
Cheers, David / dhildenb
Re: [RFC PATCH 8/8] kvm: gmem: Allow restricted userspace mappings
In reply to David HildenbrandHey,
Quoted 26 lines · David Hildenbrand
Quoted 25 lines · Patrick Roy
Quoted 21 lines · David Hildenbrand
Quoted 11 lines · Fuad Tabba
Hi Patrick,
Quoted 4 lines · Patrick Roy
Allow mapping
guest_memfdinto userspace. SinceAS_INACCESSIBLEis set on the underlyingaddress_spacestruct, no GUP ofguest_memfdwill be possible.This patch allows mapping
guest_memfd()unconditionally. Even if it's not guppable, there are other reasons why you wouldn't want to allow this. Maybe a config flag to gate it? e.g.,As discussed with Jason, maybe not the direction we want to take with
guest_memfd.
If it's private memory, it shall not be mapped. Also not via magic config options.We'll likely discuss some of that in the meeting MM tomorrow I guess (having both shared and private memory in
guest_memfd).
Quoted 8 lines
Quoted 4 lines
Oh, nice. I'm assuming you mean this meeting:
https://lore.kernel.org/linux-mm/[email protected]/T/?
Would it be okay if I also attend? I see it also mentions huge pages, which is another thing we are interested in, actually :)Hi,
sorry for the late reply. Yes, you could have joined .... too late.
No worries, I did end up joining to listen in to y'all's discussion anyway :)
Quoted 23 lines
There will be a summary posted soon. So far the agreement is that we're planning on allowing shared memory as part
guest_memfd, and will allow that to get mapped and pinned. Private memory is not going to get mapped and pinned.If we have to disallow pinning of shared memory on top for some use cases (i.e., no directmap), I assume that could be added.
Quoted 4 lines
Quoted 3 lines
Note that just from staring at this commit, I don't understand the motivation *why* we would want to do that.
Quoted 10 lines
Fair - I admittedly didn't get into that as much as I probably should have. In our usecase, we do not have anything that pKVM would (I think) call "guest-private" memory. I think our memory can be better described as guest-owned, but always shared with the VMM (e.g. userspace), but ideally never shared with the host kernel. This model lets us do a lot of simplifying assumptions: Things like I/O can be handled in userspace without the guest explicitly sharing I/O buffers (which is not exactly what we would want long-term anyway, as sharing in the
guest_memfdcontext means sharing with the host kernel), we can easily do VM snapshotting without needing things like TDX's TDH.EXPORT.MEM APIs, etc.
Quoted 2 lines
Okay, so essentially you would want to use
guest_memfdto only contain shard memory and disallow any pinning like for secretmem.
Yeah, this is pretty much what I thought we wanted before listening in on Wednesday.
I've actually be thinking about this some more since then though. With hugepages, if the VM is backed by, say, 2M pages, our on-demand direct map insertion approach runs into the same problem that CoCo VMs have when they're backed by hugepages: How to deal with the guest only sharing a 4K range in a hugepage? If we want to restore the direct map for e.g. the page containing kvm-clock data, then we can't simply go ahead and restore the direct map for the entire 2M page, because there very well might be stuff in the other 511 small guest pages that we really do not want in the direct map. And we can't even take the approach of letting the guest deal with the problem, because here "sharing" is driven by the host, not the guest, so the guest cannot possibly know that it maybe should avoid putting stuff it doesn't want shared into those remaining 511 pages! To me that sounds a lot like the whole "breaking down huge folios to allow GUP to only some parts of it" thing mentioned on Wednesday.
Now, if we instead treat "guest memory without direct map entries" as "private", and "guest memory with direct map entries" as "shared", then the above will be solved by whatever mechanism allows gupping/mapping of only the "shared" parts of huge folios, IIUC. The fact that GUP is then also allowed for the "shared" parts is not actually a problem for us - we went down the route of disabling GUP altogether here because based on
[1] it sounded like GUP for anything gmem related would never happen. But after something is re-inserted into the direct map, we don't very much care if it can be GUP-ed or not. In fact, allowing GUP for the shared parts probably makes some things easier for us, as we can then do I/O without bounce buffers by just in-place converting I/O-buffers to shared, and then treating that shared slice of guest_memfd the same way we treat traditional guest memory today. In a very far-off future, we'd like to be able to do I/O without ever reinserting pages into the direct map, but I don't think adopting this private/shared model for gmem would block us from doing that?
Although all of this does hinge on us being able to do the in-place shared/private conversion without any guest involvement. Do you envision that to be possible?
Quoted 8 lines
If so, I wonder if it wouldn't be better to simply add KVM support to consume *real* secretmem memory? IIRC so far there was only demand to probably remove the directmap of private memory in
guest_memfd, not of shared memory.Signature · 3 lines
Cheers, David / dhildenb
Best,
Patrick
Re: [RFC PATCH 8/8] kvm: gmem: Allow restricted userspace mappings
Quoted 6 lines
Quoted 3 lines
Hi,
sorry for the late reply. Yes, you could have joined .... too late.
No worries, I did end up joining to listen in to y'all's discussion anyway :)
Sorry for the late reply :(
Quoted 30 lines
Quoted 24 lines
There will be a summary posted soon. So far the agreement is that we're planning on allowing shared memory as part
guest_memfd, and will allow that to get mapped and pinned. Private memory is not going to get mapped and pinned.If we have to disallow pinning of shared memory on top for some use cases (i.e., no directmap), I assume that could be added.
Quoted 4 lines
Quoted 3 lines
Note that just from staring at this commit, I don't understand the motivation *why* we would want to do that.
Quoted 10 lines
Fair - I admittedly didn't get into that as much as I probably should have. In our usecase, we do not have anything that pKVM would (I think) call "guest-private" memory. I think our memory can be better described as guest-owned, but always shared with the VMM (e.g. userspace), but ideally never shared with the host kernel. This model lets us do a lot of simplifying assumptions: Things like I/O can be handled in userspace without the guest explicitly sharing I/O buffers (which is not exactly what we would want long-term anyway, as sharing in the
guest_memfdcontext means sharing with the host kernel), we can easily do VM snapshotting without needing things like TDX's TDH.EXPORT.MEM APIs, etc.Quoted 2 lines
Okay, so essentially you would want to use
guest_memfdto only contain shard memory and disallow any pinning like for secretmem.Yeah, this is pretty much what I thought we wanted before listening in on Wednesday.
Quoted 9 lines
I've actually be thinking about this some more since then though. With hugepages, if the VM is backed by, say, 2M pages, our on-demand direct map insertion approach runs into the same problem that CoCo VMs have when they're backed by hugepages: How to deal with the guest only sharing a 4K range in a hugepage? If we want to restore the direct map for e.g. the page containing kvm-clock data, then we can't simply go ahead and restore the direct map for the entire 2M page, because there very well might be stuff in the other 511 small guest pages that we really do not want in the direct map. And we can't even take the
Right, you'd only want to restore the direct map for a fragment. Or dynamically map that fragment using kmap where required (as raised by Vlastimil).
Quoted 6 lines
approach of letting the guest deal with the problem, because here "sharing" is driven by the host, not the guest, so the guest cannot possibly know that it maybe should avoid putting stuff it doesn't want shared into those remaining 511 pages! To me that sounds a lot like the whole "breaking down huge folios to allow GUP to only some parts of it" thing mentioned on Wednesday.
Yes. While it would be one logical huge page, it would be exposed to the remainder of the kernel as 512 individual pages.
Quoted 8 lines
Now, if we instead treat "guest memory without direct map entries" as "private", and "guest memory with direct map entries" as "shared", then the above will be solved by whatever mechanism allows gupping/mapping of only the "shared" parts of huge folios, IIUC. The fact that GUP is then also allowed for the "shared" parts is not actually a problem for us - we went down the route of disabling GUP altogether here because based on
[1] it sounded like GUP for anything gmem related would never happen.
Right. Might there also be a case for removing the directmap for shared memory or is that not really a requirement so far?
Quoted 6 lines
But after something is re-inserted into the direct map, we don't very much care if it can be GUP-ed or not. In fact, allowing GUP for the shared parts probably makes some things easier for us, as we can then do I/O without bounce buffers by just in-place converting I/O-buffers to shared, and then treating that shared slice of
guest_memfdthe same way we treat traditional guest memory today.
Yes.
Quoted 4 lines
In a very far-off future, we'd
like to be able to do I/O without ever reinserting pages into the direct map, but I don't think adopting this private/shared model for gmem would block us from doing that?
How would that I/O get triggered? GUP would require the directmap.
Quoted 4 lines
Although all of this does hinge on us being able to do the in-place shared/private conversion without any guest involvement. Do you envision that to be possible?
Who would trigger the conversion and how? I don't see a reason why -- for your use case -- user space shouldn't be able to trigger conversion private <-> shared. At least nothing fundamental comes to mind that would prohibit that.
Signature · 3 lines
Cheers, David / dhildenb
Re: [RFC PATCH 8/8] kvm: gmem: Allow restricted userspace mappings
Quoted 8 lines · David Hildenbrand
Quoted 6 lines
Quoted 3 lines
Hi,
sorry for the late reply. Yes, you could have joined .... too late.
No worries, I did end up joining to listen in to y'all's discussion anyway :)
Sorry for the late reply :(
No worries :)
Quoted 40 lines
Quoted 30 lines
Quoted 24 lines
There will be a summary posted soon. So far the agreement is that we're planning on allowing shared memory as part
guest_memfd, and will allow that to get mapped and pinned. Private memory is not going to get mapped and pinned.If we have to disallow pinning of shared memory on top for some use cases (i.e., no directmap), I assume that could be added.
Quoted 4 lines
Quoted 3 lines
Note that just from staring at this commit, I don't understand the motivation *why* we would want to do that.
Quoted 10 lines
Fair - I admittedly didn't get into that as much as I probably should have. In our usecase, we do not have anything that pKVM would (I think) call "guest-private" memory. I think our memory can be better described as guest-owned, but always shared with the VMM (e.g. userspace), but ideally never shared with the host kernel. This model lets us do a lot of simplifying assumptions: Things like I/O can be handled in userspace without the guest explicitly sharing I/O buffers (which is not exactly what we would want long-term anyway, as sharing in the
guest_memfdcontext means sharing with the host kernel), we can easily do VM snapshotting without needing things like TDX's TDH.EXPORT.MEM APIs, etc.Quoted 2 lines
Okay, so essentially you would want to use
guest_memfdto only contain shard memory and disallow any pinning like for secretmem.Yeah, this is pretty much what I thought we wanted before listening in on Wednesday.
Quoted 9 lines
I've actually be thinking about this some more since then though. With hugepages, if the VM is backed by, say, 2M pages, our on-demand direct map insertion approach runs into the same problem that CoCo VMs have when they're backed by hugepages: How to deal with the guest only sharing a 4K range in a hugepage? If we want to restore the direct map for e.g. the page containing kvm-clock data, then we can't simply go ahead and restore the direct map for the entire 2M page, because there very well might be stuff in the other 511 small guest pages that we really do not want in the direct map. And we can't even take the
Quoted 3 lines
Right, you'd only want to restore the direct map for a fragment. Or dynamically map that fragment using kmap where required (as raised by Vlastimil).
Can the kmap approach work if the memory is supposed to be GUP-able?
Quoted 19 lines
Quoted 6 lines
approach of letting the guest deal with the problem, because here "sharing" is driven by the host, not the guest, so the guest cannot possibly know that it maybe should avoid putting stuff it doesn't want shared into those remaining 511 pages! To me that sounds a lot like the whole "breaking down huge folios to allow GUP to only some parts of it" thing mentioned on Wednesday.
Yes. While it would be one logical huge page, it would be exposed to the remainder of the kernel as 512 individual pages.
Quoted 8 lines
Now, if we instead treat "guest memory without direct map entries" as "private", and "guest memory with direct map entries" as "shared", then the above will be solved by whatever mechanism allows gupping/mapping of only the "shared" parts of huge folios, IIUC. The fact that GUP is then also allowed for the "shared" parts is not actually a problem for us - we went down the route of disabling GUP altogether here because based on
[1] it sounded like GUP for anything gmem related would never happen.
Quoted 2 lines
Right. Might there also be a case for removing the directmap for shared memory or is that not really a requirement so far?
No, not really - we would only mark as "shared" memory that _needs_ to be in the direct map for functional reasons (e.g. MMIO instruction emulation, etc.).
Quoted 7 lines
Quoted 6 lines
But after something is re-inserted into the direct map, we don't very much care if it can be GUP-ed or not. In fact, allowing GUP for the shared parts probably makes some things easier for us, as we can then do I/O without bounce buffers by just in-place converting I/O-buffers to shared, and then treating that shared slice of
guest_memfdthe same way we treat traditional guest memory today.
Quoted 8 lines
Yes.
Quoted 4 lines
In a very far-off future, we'd
like to be able to do I/O without ever reinserting pages into the direct map, but I don't think adopting this private/shared model for gmem would block us from doing that?How would that I/O get triggered? GUP would require the directmap.
I was hoping that this "phyr" thing Matthew has been talking about [1] would allow somehow doing I/O without direct map entries/GUP, but maybe I am misunderstanding something.
Quoted 9 lines
Quoted 4 lines
Although all of this does hinge on us being able to do the in-place shared/private conversion without any guest involvement. Do you envision that to be possible?
Who would trigger the conversion and how? I don't see a reason why -- for your use case -- user space shouldn't be able to trigger conversion private <-> shared. At least nothing fundamental comes to mind that would prohibit that.
Either KVM itself would trigger the conversions whenever it wants to access gmem (e.g. each place in this series where there is a set_direct_map_{invalid,default} it would do a shared/private conversion), or userspace would do it via some syscall/ioctl (the one place I can think of right now is I/O, where the VMM receives a virtio buffer from the guest and converts it from private to shared in-place. Although I guess 2 syscalls for each I/O operation aren't great perf-wise, so maybe swiotlb still wins out here?).
I actually see that Fuad just posted an RFC series that implements the basic shared/private handling [2], so will probably also comment about this over there after I had a closer look :)
Quoted 4 lines
Signature · 3 lines
Cheers, David / dhildenb
Best,
Patrick
[1]: https://lore.kernel.org/netdev/Yd0IeK5s%[email protected]/T/
[2]: https://lore.kernel.org/kvm/[email protected]/T/#t
Quoted 23 lines · Patrick Roy
Hey all,
This RFC series is a rough draft adding support for running non-confidential compute VMs in
guest_memfd, based on prior discussions with Sean [1]. Our specific usecase for this is the ability to unmap guest memory from the host kernel's direct map, as a mitigation against a large class of speculative execution issues.=== Implementation ===
This patch series introduces a new flag to the
KVM_CREATE_GUEST_MEMFDto remove its pages from the direct map when they are allocated. When trying to run a guest from such a VM, we now face the problem that without either userspace or kernelspace mappings ofguest_memfd, KVM cannot access guest memory to, for example, do MMIO emulation of access memory used to guest/host communication. We have multiple options for solving this when running non-CoCo VMs: (1) implement a TDX-light solution, where the guest shares memory that KVM needs to access, and relies on paravirtual solutions where this is not possible (e.g. MMIO),
(2) have KVM use userspace mappings ofguest_memfd(e.g. a memfd_secret-style solution), or (3) dynamically reinsert pages into the direct map whenever KVM wants to access them.
Quoted 5 lines
This RFC goes for option (3). Option (1) is a lot of overhead for very little gain, since we are not actually constrained by a physical inability to access guest memory (e.g. we are not in a TDX context where accesses to guest memory cause a #MC). Option (2) has previously been rejected [1].
Do the pages have to have the same address when they are temporarily mapped? Wouldn't it be easier to do something similar to kmap_local_page() used for HIMEM? I.e. you get a temporary kernel mapping to do what's needed, but it doesn't have to alter the shared directmap.
Maybe that was already discussed somewhere as unsuitable but didn't spot it here.
Quoted 120 lines
In this patch series, we make sufficient parts of KVM gmem-aware to be able to boot a Linux initrd from private memory on x86. These include KVM's MMIO emulation (including guest page table walking) and kvm-clock. For VM types which do not allow accessing gmem, we return
-EFAULTand attempt to prepare aKVM_EXIT_MEMORY_FAULT.Additionally, this patch series adds support for "restricted" userspace mappings of
guest_memfd, which work similar tomemfd_secret(e.g. disallowget_user_pages), which allows handling I/O and loading the guest kernel in a simple way. Support for this is completely independent of the rest of the functionality introduced in this patch series. However, it is required to build a minimal hypervisor PoC that actually allows booting a VM from a disk.=== Performance ===
We have run some preliminary performance benchmarks to assess the impact of on-the-fly direct map manipulations. We were mainly interested in the impact of manipulating the direct map for MMIO emulation on virtio-mmio. Particularly, we were worried about the impact of the TLB and L1/2/3 Cache flushes that set_memory_[n]p entails.
In our setup, we have taken a modified Firecracker VMM, spawned a Linux guest with 1 vCPU, and used fio to stress a
virtio_blkdevice. We found that the cache flushes caused throughput to drop from around 600MB/s to ~50MB/s (~90%) for both reads and writes (on a Intel(R) Xeon(R) Platinum 8375C CPU with 64 cores). We then converted our prototype to use set_direct_map_{invalid,default}_noflush instead of set_memory_[n]p and found that without cache flushes the pure impact of the direct map manipulation is indistinguishable from noise. This is why we use set_direct_map_{invalid,default}_noflush instead of set_memory_[n]p in this RFC.Note that in this comparison, both the baseline, as well as the guest_memfd-supporting version of Firecracker were made to bounce I/O buffers in VMM userspace. As GUP is disabled for the
guest_memfdVMAs, the virtio stack cannot directly pass guest buffers to read/write syscalls.=== Security ===
We want to use unmapping guest memory from the host kernel as a security mitigation against transient execution attacks. Temporarily restoring direct map entries whenever KVM requires access to guest memory leaves a gap in this mitigation. We believe this to be acceptable for the above cases, since pages used for paravirtual guest/host communication (e.g. kvm-clock) and guest page tables do not contain sensitive data. MMIO emulation will only end up reading pages containing privileged instructions (e.g. guest kernel code).
=== Summary ===
Patches 1-4 are about hot-patching various points inside of KVM that access guest memory to correctly handle the case where memory happens to be guest-private. This means either handling the access as a memory error, or simply accessing the memslot's
guest_memfdinstead of looking at the userspace provided VMA if the VM type allows these kind of accesses. Patches 5-6 add a flag toKVM_CREATE_GUEST_MEMFDthat will make it remove its pages from the kernel's direct map. Whenever KVM wants to access guest-private memory, it will temporarily re-insert the relevant pages. Patches 7-8 allow for restricted userspace mappings (e.g.get_user_pagespaths are disabled like formemfd_secret) ofguest_memfd, so that userspace has an easy path for loading the guest kernel and handling I/O-buffers.=== ToDos / Limitations ===
There are still a few rough edges that need to be addressed before dropping the "RFC" tag, e.g.
* Handle errors of
set_direct_map_default_not_flushinkvm_gmem_invalidate_folioinstead of callingBUG_ON
* Lift the limitation of "at most onegfn_to_pfn_cachefor each gfn/pfn" ine1c61f0a7963("kvm: gmem: Temporarily restore direct map entries when needed"). It currently means that guests with more than 1 vcpu fail to boot, because multiple vcpus can put their kvm-clock PV structures into the same page (gfn)
* Write selftests, particularly around hole punching, direct map removal, and mmap.Lastly, there's the question of nested virtualization which Sean brought up in previous discussions, which runs into similar problems as MMIO. I have looked at it very briefly. On Intel, KVM uses various
gfn->uhvacaches, which run in similar problems as thegfn_to_hva_cachesdealt with in200834b15dda("kvm: use slowpath ingfn_to_hva_cacheif memory is private"). However, previous attempts at just converting this togfn_to_pfn_cache(which would make them work withguest_memfd) proved complicated [2]. I suppose initially, we should probably disallow nested virtualization in VMs that have their memory removed from the direct map.Best,
Patrick[1]: https://lore.kernel.org/linux-mm/[email protected]/
[2]: https://lore.kernel.org/kvm/[email protected]/Patrick Roy (8):
kvm: Allow reading/writing gmem using kvm_{read,write}_guest
kvm: use slowpath ingfn_to_hva_cacheif memory is private
kvm: pfncache: enlighten about gmem
kvm: x86: support walking guest page tables in gmem
kvm: gmem: add option to remove guest private memory from direct map
kvm: gmem: Temporarily restore direct map entries when needed
mm: secretmem: useAS_INACCESSIBLEto prohibit GUP
kvm: gmem: Allow restricted userspace mappingsarch/x86/kvm/mmu/paging_tmpl.h | 94 +++++++++++++++++++----- include/linux/kvm_host.h | 5 ++ include/linux/kvm_types.h | 1 + include/linux/secretmem.h | 13 +++- include/uapi/linux/kvm.h | 2 + mm/secretmem.c | 6 +- virt/kvm/guest_memfd.c | 83 +++++++++++++++++++-- virt/kvm/kvm_main.c | 112 +++++++++++++++++++++++++++- virt/kvm/pfncache.c | 130 +++++++++++++++++++++++++++++---- 9 files changed, 399 insertions(+), 47 deletions(-)base-commit:
890a64810d59b1a58ed26efc28cfd821fc068e84
Quoted 21 lines · Vlastimil Babka (SUSE)
Quoted 15 lines
=== Implementation ===
This patch series introduces a new flag to the
KVM_CREATE_GUEST_MEMFDto remove its pages from the direct map when they are allocated. When trying to run a guest from such a VM, we now face the problem that without either userspace or kernelspace mappings ofguest_memfd, KVM cannot access guest memory to, for example, do MMIO emulation of access memory used to guest/host communication. We have multiple options for solving this when running non-CoCo VMs: (1) implement a TDX-light solution, where the guest shares memory that KVM needs to access, and relies on paravirtual solutions where this is not possible (e.g. MMIO),
(2) have KVM use userspace mappings ofguest_memfd(e.g. a memfd_secret-style solution), or (3) dynamically reinsert pages into the direct map whenever KVM wants to access them.Quoted 5 lines
This RFC goes for option (3). Option (1) is a lot of overhead for very little gain, since we are not actually constrained by a physical inability to access guest memory (e.g. we are not in a TDX context where accesses to guest memory cause a #MC). Option (2) has previously been rejected [1].
Quoted 7 lines
Do the pages have to have the same address when they are temporarily mapped? Wouldn't it be easier to do something similar to
kmap_local_page()used for HIMEM? I.e. you get a temporary kernel mapping to do what's needed, but it doesn't have to alter the shared directmap.Maybe that was already discussed somewhere as unsuitable but didn't spot it here.
For what I had prototyped here, there's no requirement to have the pages mapped at the same address (I remember briefly looking at memremap to achieve the temporary mappings, but since that doesnt work for normal memory, I gave up on that path). However, I think guest_memfd is moving into a direction where ranges marked as "in-place shared" (e.g. those that are temporarily reinserted into the direct map in this RFC) should be able to be GUP'd [1]. I think for that the direct map entries would need to be present, right?
Quoted 6 lines
Quoted 6 lines
In this patch series, we make sufficient parts of KVM gmem-aware to be able to boot a Linux initrd from private memory on x86. These include KVM's MMIO emulation (including guest page table walking) and kvm-clock. For VM types which do not allow accessing gmem, we return
-EFAULTand attempt to prepare aKVM_EXIT_MEMORY_FAULT.
Quoted 7 lines
Quoted 7 lines
Additionally, this patch series adds support for "restricted" userspace mappings of
guest_memfd, which work similar tomemfd_secret(e.g. disallowget_user_pages), which allows handling I/O and loading the guest kernel in a simple way. Support for this is completely independent of the rest of the functionality introduced in this patch series. However, it is required to build a minimal hypervisor PoC that actually allows booting a VM from a disk.
[1]: https://lore.kernel.org/kvm/[email protected]/T/#mc944a6fdcd20a35f654c2be99f9c91a117c1bed4
Quoted 32 lines · Patrick Roy
Quoted 21 lines · Vlastimil Babka (SUSE)
Quoted 15 lines
=== Implementation ===
This patch series introduces a new flag to the
KVM_CREATE_GUEST_MEMFDto remove its pages from the direct map when they are allocated. When trying to run a guest from such a VM, we now face the problem that without either userspace or kernelspace mappings ofguest_memfd, KVM cannot access guest memory to, for example, do MMIO emulation of access memory used to guest/host communication. We have multiple options for solving this when running non-CoCo VMs: (1) implement a TDX-light solution, where the guest shares memory that KVM needs to access, and relies on paravirtual solutions where this is not possible (e.g. MMIO),
(2) have KVM use userspace mappings ofguest_memfd(e.g. a memfd_secret-style solution), or (3) dynamically reinsert pages into the direct map whenever KVM wants to access them.Quoted 5 lines
This RFC goes for option (3). Option (1) is a lot of overhead for very little gain, since we are not actually constrained by a physical inability to access guest memory (e.g. we are not in a TDX context where accesses to guest memory cause a #MC). Option (2) has previously been rejected [1].
Quoted 7 lines
Do the pages have to have the same address when they are temporarily mapped? Wouldn't it be easier to do something similar to
kmap_local_page()used for HIMEM? I.e. you get a temporary kernel mapping to do what's needed, but it doesn't have to alter the shared directmap.Maybe that was already discussed somewhere as unsuitable but didn't spot it here.
Quoted 8 lines
For what I had prototyped here, there's no requirement to have the pages mapped at the same address (I remember briefly looking at memremap to achieve the temporary mappings, but since that doesnt work for normal memory, I gave up on that path). However, I think
guest_memfdis moving into a direction where ranges marked as "in-place shared" (e.g. those that are temporarily reinserted into the direct map in this RFC) should be able to be GUP'd [1]. I think for that the direct map entries would need to be present, right?
Yes, we'd allow GUP. Of course, one could think of a similar extension like secretmem that would allow shared memory to get mapped into user page tables but would disallow any GUP on (shared) guest_memfd memory.
Signature · 3 lines
Cheers, David / dhildenb
Quoted 8 lines · Patrick Roy
Hey all,
This RFC series is a rough draft adding support for running non-confidential compute VMs in
guest_memfd, based on prior discussions with Sean [1]. Our specific usecase for this is the ability to unmap guest memory from the host kernel's direct map, as a mitigation against a large class of speculative execution issues.
Not to sound like a salesman, but did you happen to come across the RFC for ASI?
https://lore.kernel.org/lkml/[email protected]/
The current implementation considers userspace allocations as sensitive, so when a VM is running with ASI, the memory of other VMs is unmapped from the direct map (i.e. in the restricted address space). It also incorporates a mechanism to map this memory on-demand when needed (i.e. switch to the unrestricted address space), and running mitigations at this point to make sure it isn't exploited.
In theory, it should be a more generic approach because it should apply to VMs that do not use guest_memfd as well, and it should be extensible to protect other parts of memory (e.g. sensitive kernel allocations).
I understand that unmapping guest_memfd memory from the direct map in general could still be favorable, and for other reasons beyond mitigating speculative execution attacks. Just thought you may be interested in looking at ASI.