Direct Map Removal for guest_memfd
Subsystems (14)
v4 cover Direct Map Removal for guest_memfd
Unmapping virtual machine guest memory from the host kernel's direct map is a successful mitigation against Spectre-style transient execution
issues: If the kernel page tables do not contain entries pointing to guest memory, then any attempted speculative read through the direct map will necessarily be blocked by the MMU before any observable microarchitectural side-effects happen. This means that Spectre-gadgets and similar cannot be used to target virtual machine memory. Roughly 60% of speculative execution issues fall into this category [1, Table 1].
This patch series extends guest_memfd with the ability to remove its memory from the host kernel's direct map, to be able to attain the above protection for KVM guests running inside guest_memfd.
=== Changes to RFC v3 ===
- Settle relationship between direct map removal and shared/private memory in guest_memfd (David H.)
- Omit TLB flushes upon direct map removal again
- Settle uABI for how KVM accesses guest memory in non-CoCo guest_memfd VMs (upstream guest_memfd calls)
- Add selftests that exercise the codepaths of non-CoCo guest_memfd VMs
Lastly, this series is rebased on top of Fuad's v4 for shared mapping of guest_memfd [2]. The KVM parts should also apply on top of 0ad2507d5d93 ("Linux 6.14-rc3"), but the selftest patches need Fuad's series as base.
=== Overview ===
guest_memfd should be usable for "non-CoCo" VMs - virtual machines where host userspace is trusted (e.g. can have access to all of guest memory), but which should still be hardened against speculative execution attacks (Spectre, etc.) staged through potentially existing gadgets in the host kernel.
To attain this hardening, unmap guest memory from the host kernels address space (e.g. zap direct map entries), while allowing KVM to continue accessing guest memory through userspace mappings. This works because KVM already almost always uses userspace mappings whenever KVM needs to access guest memory - the only parts that require direct map entries (because they use GUP) are KVM's MMU, and kvm-clock on x86.
Building on top of guest_memfd sidesteps the MMU problem, as for memslots with KVM_MEM_GUEST_MEMFD set, the MMU consumes fd + offset directly without going through any VMAs. kvm-clock on the other hand is not strictly needed (guests boot fine without it), so ignore it for now.
=== Implementation ===
Make KVM_CREATE_GUEST_MEMFD accept a flag (KVM_GMEM_NO_DIRECT_MAP) that instructs it to remove newly allocated folios from the host kernels direct map immediately after preparation.
Nothing further is needed to make non-CoCo VMs work - particularly, KVM does not need to be taught any special ways of accessing guest memory if it is in guest_memfd. Userspace can simply mmap guest_memfd (via KVM_GMEM_SHARED_MEM added in Fuad's series), and set the memslot's userspace_addr to this userspace mapping of guest_memfd.
=== Open Questions ===
In this patch series, stale TLB entries do not get flushed after direct map entries are marked as not present. This is fine from a functional point of view (as the mapping is still valid, it's just temporarily not supposed to be used), but pokes a theoretical hole into the speculation
protection: Something could try to keep alive stale TLB entries for specific pages until the guest starts using them for sensitive information, and then stage a Spectre attack on that memory, despite it being unmapped. In practice, this would require knowing in advance, at gmem fault-time, which pages will eventually contain information of interest, and then preventing these specific TLB entries from getting naturally evicted (where the number of pages that can be targeted like this is limited by the size of the TLB). These seem to be fairly difficult requisites to fulfill, but we were wondering what the community thinks.
=== Summary ===
Patch 1 adds a struct address_space flag that indices that folios in a mapping are direct map removed, and threads it through mm code to ensure direct map removed folios don't end up in places where they can cause mayhem (particularly, we reject them in get_user_pages). Since these checks end up being duplicates of already existing checks for secretmem folios, patch 2 unifies the two by using the new address_space flag for secretmem mappings. Patches 3 through 5 are about support for direct map removal in guest_memfd, while patches 6 through 12 are about testing the non-CoCo setup in KVM selftests, with patches 6 through 9 being preparatory, and patches 10 through 12 adding the actual test cases.
[1]: https://download.vusec.net/papers/quarantine_raid23.pdf
[2]: https://lore.kernel.org/kvm/[email protected]/
[RFC v1]: https://lore.kernel.org/kvm/[email protected]/
[RFC v2]: https://lore.kernel.org/kvm/[email protected]/
[RFC v3]: https://lore.kernel.org/kvm/[email protected]/
Patrick Roy (12):
mm: introduce AS_NO_DIRECT_MAP
mm/secretmem: set AS_NO_DIRECT_MAP instead of special-casing
KVM: guest_memfd: Add flag to remove from direct map
KVM: Add capability to discover KVM_GMEM_NO_DIRECT_MAP support
KVM: Documentation: document KVM_GMEM_NO_DIRECT_MAP flag
KVM: selftests: load elf via bounce buffer
KVM: selftests: set KVM_MEM_GUEST_MEMFD in vm_mem_add() if guest_memfd
!= -1
KVM: selftests: Add guest_memfd based vm_mem_backing_src_types
KVM: selftests: stuff vm_mem_backing_src_type into vm_shape
KVM: selftests: adjust test_create_guest_memfd_invalid
KVM: selftests: set KVM_GMEM_NO_DIRECT_MAP in mem conversion tests
KVM: selftests: Test guest execution from direct map removed gmem
Documentation/virt/kvm/api.rst | 13 ++++ include/linux/pagemap.h | 16 +++++ include/linux/secretmem.h | 18 ------ include/uapi/linux/kvm.h | 3 + lib/buildid.c | 4 +- mm/gup.c | 14 +--- mm/mlock.c | 2 +- mm/secretmem.c | 6 +- .../testing/selftests/kvm/guest_memfd_test.c | 2 +- .../testing/selftests/kvm/include/kvm_util.h | 29 ++++++--- .../testing/selftests/kvm/include/test_util.h | 8 +++ tools/testing/selftests/kvm/lib/elf.c | 8 +-- tools/testing/selftests/kvm/lib/io.c | 23 +++++++ tools/testing/selftests/kvm/lib/kvm_util.c | 64 +++++++++++-------- tools/testing/selftests/kvm/lib/test_util.c | 8 +++ tools/testing/selftests/kvm/lib/x86/sev.c | 1 + .../selftests/kvm/pre_fault_memory_test.c | 1 + .../selftests/kvm/set_memory_region_test.c | 40 ++++++++++++ .../kvm/x86/private_mem_conversions_test.c | 7 +- virt/kvm/guest_memfd.c | 24 ++++++- virt/kvm/kvm_main.c | 5 ++ 21 files changed, 214 insertions(+), 82 deletions(-)
base-commit: da40655874b54a2b563f8ceb3ed839c6cd38e0b4
Signature · 1 line
2.48.1
PATCH v4 1/12 mm: introduce AS_NO_DIRECT_MAP
Add AS_NO_DIRECT_MAP for mappings where direct map entries of folios are set to not present . Currently, mappings that match this description are secretmem mappings (memfd_secret()). Later, some guest_memfd configurations will also fall into this category.
Reject this new type of mappings in all locations that currently reject secretmem mappings, on the assumption that if secretmem mappings are rejected somewhere, it is precisely because of an inability to deal with folios without direct map entries.
Use a new flag instead of overloading AS_INACCESSIBLE (which is already set by guest_memfd) because not all guest_memfd mappings will end up being direct map removed (e.g. in pKVM setups, parts of guest_memfd that can be mapped to userspace should also be GUP-able, and generally not have restrictions on who can access it).
--- include/linux/pagemap.h | 16 ++++++++++++++++ lib/buildid.c | 4 ++-- mm/gup.c | 6 +++++- mm/mlock.c | 3 ++- 4 files changed, 25 insertions(+), 4 deletions(-)
include/linux/pagemap.h +16 -0
@@ -210,6 +210,7 @@ enum mapping_flags {AS_STABLE_WRITES =7 ,/* must wait for writeback before modifying folio contents */ AS_INACCESSIBLE =8 ,/* Do not attempt direct R/W access to the mapping */ +AS_NO_DIRECT_MAP =9 ,/* Folios in the mapping are not in the direct map */ /* Bits 16-25 are used for FOLIO_ORDER */ AS_FOLIO_ORDER_BITS =5 , AS_FOLIO_ORDER_MIN = 16,
@@ -335,6 +336,21 @@ static inline bool mapping_inaccessible(struct address_space *mapping)return test_bit (AS_INACCESSIBLE , &mapping->flags); } +static inline void mapping_set_no_direct_map (struct address_space *mapping) +{ +set_bit (AS_NO_DIRECT_MAP , &mapping->flags); +} + +static inline bool mapping_no_direct_map (struct address_space *mapping) +{ +return test_bit (AS_NO_DIRECT_MAP , &mapping->flags); +} + +static inline bool vma_is_no_direct_map (const struct vm_area_struct *vma) +{ +return vma->vm_file &&mapping_no_direct_map (vma->vm_file->f_mapping); +} +static inline gfp_t mapping_gfp_mask (struct address_space * mapping) {return mapping->gfp_mask;
lib/buildid.c +2 -2
@@ -65,8 +65,8 @@ static int freader_get_folio(struct freader *r, loff_t file_off)freader_put_folio (r); -/* reject secretmem folios created with memfd_secret() */ -if (secretmem_mapping (r->file->f_mapping)) +/* reject secretmem folios created with memfd_secret() or guest_memfd() */ +if (secretmem_mapping (r->file->f_mapping) ||mapping_no_direct_map (r->file->f_mapping))return -EFAULT ; r->folio =filemap_get_folio (r->file->f_mapping, file_off >>PAGE_SHIFT );
mm/gup.c +5 -1
@@ -1283,7 +1283,7 @@ static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)if ((gup_flags &FOLL_LONGTERM ) &&vma_is_fsdax (vma))return -EOPNOTSUPP ; -if (vma_is_secretmem (vma)) +if (vma_is_secretmem (vma) ||vma_is_no_direct_map (vma))return -EFAULT ;if (write) {
@@ -2849,6 +2849,10 @@ static bool gup_fast_folio_allowed(struct folio *folio, unsigned int flags)*/ if (check_secretmem &&secretmem_mapping (mapping))return false; + +if (mapping_no_direct_map (mapping)) +return false; +/* The only remaining allowed file system is shmem. */ return !reject_file_backed ||shmem_mapping (mapping); }
mm/mlock.c +2 -1
@@ -474,7 +474,8 @@ static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma,if (newflags == oldflags || (oldflags &VM_SPECIAL ) ||is_vm_hugetlb_page (vma) || vma ==get_gate_vma (current->mm) || -vma_is_dax (vma) ||vma_is_secretmem (vma) || (oldflags &VM_DROPPABLE )) +vma_is_dax (vma) ||vma_is_secretmem (vma) ||vma_is_no_direct_map (vma) || + (oldflags &VM_DROPPABLE ))/* don't set VM_LOCKED or VM_LOCKONFAULT and don't count */ goto out;
base-commit: da40655874b54a2b563f8ceb3ed839c6cd38e0b4
Signature · 1 line
2.48.1
Re: [PATCH v4 01/12] mm: introduce AS_NO_DIRECT_MAP
Quoted 18 lines · Patrick Roy
Add
AS_NO_DIRECT_MAPfor mappings where direct map entries of folios are set to not present . Currently, mappings that match this description are secretmem mappings (memfd_secret()). Later, someguest_memfdconfigurations will also fall into this category.Reject this new type of mappings in all locations that currently reject secretmem mappings, on the assumption that if secretmem mappings are rejected somewhere, it is precisely because of an inability to deal with folios without direct map entries.
Use a new flag instead of overloading
AS_INACCESSIBLE(which is already set byguest_memfd) because not allguest_memfdmappings will end up being direct map removed (e.g. in pKVM setups, parts ofguest_memfdthat can be mapped to userspace should also be GUP-able, and generally not have restrictions on who can access it).Signed-off-by Patrick Roy <[email protected]>---
...
Quoted 8 lines
static inline gfp_t mapping_gfp_mask(struct address_space * mapping) { return mapping->gfp_mask;lib/buildid.c +0
-0
lib/buildid.c static int freader_get_folio(struct freader *r, loff_t file_off) · line 70
Quoted 8 lines
lib/buildid.c +2
-2@@ -65,8 +65,8 @@ static int freader_get_folio(struct freader *r, loff_t file_off)freader_put_folio (r); -/* reject secretmem folios created with memfd_secret() */ -if (secretmem_mapping (r->file->f_mapping)) +/* reject secretmem folios created with memfd_secret() or guest_memfd() */ +if (secretmem_mapping (r->file->f_mapping) ||mapping_no_direct_map (r->file->f_mapping))return -EFAULT ;
Maybe I'm missing it, but why do we have to special-case secretmem with that at all anymore?
Couldn't we just let secretmem set AS_NO_DIRECT_MAP as well, and convert all/most secretmem specific stuff to check AS_NO_DIRECT_MAP as well?
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v4 01/12] mm: introduce AS_NO_DIRECT_MAP
Quoted 42 lines · David Hildenbrand
Quoted 18 lines · Patrick Roy
Add
AS_NO_DIRECT_MAPfor mappings where direct map entries of folios are set to not present . Currently, mappings that match this description are secretmem mappings (memfd_secret()). Later, someguest_memfdconfigurations will also fall into this category.Reject this new type of mappings in all locations that currently reject secretmem mappings, on the assumption that if secretmem mappings are rejected somewhere, it is precisely because of an inability to deal with folios without direct map entries.
Use a new flag instead of overloading
AS_INACCESSIBLE(which is already set byguest_memfd) because not allguest_memfdmappings will end up being direct map removed (e.g. in pKVM setups, parts ofguest_memfdthat can be mapped to userspace should also be GUP-able, and generally not have restrictions on who can access it).Signed-off-by Patrick Roy <[email protected]>---
...
Quoted 8 lines
static inline gfp_t mapping_gfp_mask(struct address_space * mapping) { return mapping->gfp_mask;lib/buildid.c +0
-0lib/buildid.c static int freader_get_folio(struct freader *r, loff_t file_off) · line 70
Quoted 8 lines
lib/buildid.c +2
-2@@ -65,8 +65,8 @@ static int freader_get_folio(struct freader *r, loff_t file_off)freader_put_folio (r); -/* reject secretmem folios created with memfd_secret() */ -if (secretmem_mapping (r->file->f_mapping)) +/* reject secretmem folios created with memfd_secret() or guest_memfd() */ +if (secretmem_mapping (r->file->f_mapping) ||mapping_no_direct_map (r->file->f_mapping))return -EFAULT ;Maybe I'm missing it, but why do we have to special-case secretmem with that at all anymore?
Quoted 2 lines
Couldn't we just let secretmem set
AS_NO_DIRECT_MAPas well, and convert all/most secretmem specific stuff to checkAS_NO_DIRECT_MAPas well?
That's done in patch 02. But yeah, squashing them together would reduce some churn. I guess because it removes some !IS_ENABLED(CONFIG_SECRETMEM) optimizations, a separate change for review was preferred.
PATCH v4 2/12 mm/secretmem: set AS_NO_DIRECT_MAP instead of special-casing
In reply to Patrick RoyMake secretmem set AS_NO_DIRECT_MAP on its struct address_space, to drop all the vma_is_secretmem()/secretmem_mapping() checks that are based on checking explicitly for the secretmem ops structures.
This drops a optimization in gup_fast_folio_allowed() where secretmem_mapping() was only called if CONFIG_SECRETMEM=y. secretmem is enabled by default since commit b758fe6df50d ("mm/secretmem: make it on by default"), so the secretmem check did not actually end up elided in most cases anymore anyway.
--- include/linux/secretmem.h | 18 ------------------ lib/buildid.c | 2 +- mm/gup.c | 14 +------------- mm/mlock.c | 3 +-- mm/secretmem.c | 6 +----- 5 files changed, 4 insertions(+), 39 deletions(-)
include/linux/secretmem.h +0 -18
@@ -4,28 +4,10 @@#ifdef CONFIG_SECRETMEM -extern const struct address_space_operations secretmem_aops; - -static inline bool secretmem_mapping (struct address_space *mapping) -{ -return mapping->a_ops == &secretmem_aops; -} - -bool vma_is_secretmem (struct vm_area_struct *vma);bool secretmem_active (void );#else -static inline bool vma_is_secretmem (struct vm_area_struct *vma) -{ -return false; -} - -static inline bool secretmem_mapping (struct address_space *mapping) -{ -return false; -} -static inline bool secretmem_active (void ) {return false;
lib/buildid.c +1 -1
@@ -66,7 +66,7 @@ static int freader_get_folio(struct freader *r, loff_t file_off)freader_put_folio (r);/* reject secretmem folios created with memfd_secret() or guest_memfd() */ -if (secretmem_mapping (r->file->f_mapping) ||mapping_no_direct_map (r->file->f_mapping)) +if (mapping_no_direct_map (r->file->f_mapping))return -EFAULT ; r->folio =filemap_get_folio (r->file->f_mapping, file_off >>PAGE_SHIFT );
mm/gup.c +1 -13
@@ -1283,7 +1283,7 @@ static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)if ((gup_flags &FOLL_LONGTERM ) &&vma_is_fsdax (vma))return -EOPNOTSUPP ; -if (vma_is_secretmem (vma) ||vma_is_no_direct_map (vma)) +if (vma_is_no_direct_map (vma))return -EFAULT ;if (write) {
@@ -2786,7 +2786,6 @@ static bool gup_fast_folio_allowed(struct folio *folio, unsigned int flags) {bool reject_file_backed = false;struct address_space *mapping; -bool check_secretmem = false;unsigned long mapping_flags;/*
@@ -2798,14 +2797,6 @@ static bool gup_fast_folio_allowed(struct folio *folio, unsigned int flags) reject_file_backed = true;/* We hold a folio reference, so we can safely access folio fields. */ - -/* secretmem folios are always order-0 folios. */ -if (IS_ENABLED (CONFIG_SECRETMEM ) && !folio_test_large (folio)) - check_secretmem = true; - -if (!reject_file_backed && !check_secretmem) -return true; -if (WARN_ON_ONCE (folio_test_slab (folio)))return false;
@@ -2847,9 +2838,6 @@ static bool gup_fast_folio_allowed(struct folio *folio, unsigned int flags)* At this point, we know the mapping is non-null and points to an * address_space object. */ -if (check_secretmem &&secretmem_mapping (mapping)) -return false; -if (mapping_no_direct_map (mapping))return false;
mm/mlock.c +1 -2
@@ -474,8 +474,7 @@ static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma,if (newflags == oldflags || (oldflags &VM_SPECIAL ) ||is_vm_hugetlb_page (vma) || vma ==get_gate_vma (current->mm) || -vma_is_dax (vma) ||vma_is_secretmem (vma) ||vma_is_no_direct_map (vma) || - (oldflags &VM_DROPPABLE )) +vma_is_dax (vma) ||vma_is_no_direct_map (vma) || (oldflags &VM_DROPPABLE ))/* don't set VM_LOCKED or VM_LOCKONFAULT and don't count */ goto out;
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,
@@ -214,6 +209,7 @@ static struct file *secretmem_file_create(unsigned long flags)mapping_set_gfp_mask (inode->i_mapping,GFP_HIGHUSER );mapping_set_unevictable (inode->i_mapping); +mapping_set_no_direct_map (inode->i_mapping); inode->i_op = &secretmem_iops; inode->i_mapping->a_ops = &secretmem_aops;
Signature · 1 line
2.48.1
Re: [PATCH v4 02/12] mm/secretmem: set AS_NO_DIRECT_MAP instead of special-casing
Quoted 4 lines · Patrick Roy
Make secretmem set
AS_NO_DIRECT_MAPon its structaddress_space, to drop all the vma_is_secretmem()/secretmem_mapping() checks that are based on checking explicitly for the secretmem ops structures.
Quoted 8 lines
This drops a optimization in
gup_fast_folio_allowed()wheresecretmem_mapping()was only called ifCONFIG_SECRETMEM=y. secretmem is enabled by default since commitb758fe6df50d("mm/secretmem: make it on by default"), so the secretmem check did not actually end up elided in most cases anymore anyway.Signed-off-by Patrick Roy <[email protected]>---
Ah, there it is. Can both patches somehow be squashed?
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v4 02/12] mm/secretmem: set AS_NO_DIRECT_MAP instead of special-casing
Quoted 10 lines · Patrick Roy
Quoted 10 lines
Make secretmem set
AS_NO_DIRECT_MAPon its structaddress_space, to drop all the vma_is_secretmem()/secretmem_mapping() checks that are based on checking explicitly for the secretmem ops structures.This drops a optimization in
gup_fast_folio_allowed()wheresecretmem_mapping()was only called ifCONFIG_SECRETMEM=y. secretmem is enabled by default since commitb758fe6df50d("mm/secretmem: make it on by default"), so the secretmem check did not actually end up elided in most cases anymore anyway.
Quoted 4 lines
Quoted 2 lines
Signed-off-by Patrick Roy <[email protected]>---
Ah, there it is. Can both patches somehow be squashed?
Yeah, I'm happy to squash them. I separated them out based on feedback on the v2, but checking back I realized that I actually just misunderstood/misremembered what you and Mike were telling me. Sorry about that, they'll be squashed together in the next posting :)
Quoted 5 lines
Signature · 3 lines
Cheers, David / dhildenb
Best,
Patrick
PATCH v4 3/12 KVM: guest_memfd: Add flag to remove from direct map
In reply to Patrick RoyAdd KVM_GMEM_NO_DIRECT_MAP flag for KVM_CREATE_GUEST_MEMFD() ioctl. When set, guest_memfd folios will be removed from the direct map after preparation, with direct map entries only restored when the folios are freed.
To ensure these folios do not end up in places where the kernel cannot deal with them, set AS_NO_DIRECT_MAP on the guest_memfd's struct address_space if KVM_GMEM_NO_DIRECT_MAP is requested.
Note that this flag causes removal of direct map entries for all guest_memfd folios independent of whether they are "shared" or "private" (although current guest_memfd only supports either all folios in the "shared" state, or all folios in the "private" state if !IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM)). The usecase for removing direct map entries of also the shared parts of guest_memfd are a special type of non-CoCo VM where, host userspace is trusted to have access to all of guest memory, but where Spectre-style transient execution attacks through the host kernel's direct map should still be mitigated.
Note that KVM retains access to guest memory via userspace mappings of guest_memfd, which are reflected back into KVM's memslots via userspace_addr. This is needed for things like MMIO emulation on x86_64 to work. Previous iterations attempted to instead have KVM temporarily restore direct map entries whenever such an access to guest memory was needed, but this turned out to have a significant performance impact, as well as additional complexity due to needing to refcount direct map reinsertion operations and making them play nicely with gmem truncations.
This iteration also doesn't have KVM perform TLB flushes after direct map manipulations. This is because TLB flushes resulted in a up to 40x elongation of page faults in guest_memfd (scaling with the number of CPU cores), or a 5x elongation of memory population. On the one hand, TLB flushes are not needed for functional correctness (the virt->phys mapping technically stays "correct", the kernel should simply to not it for a while), so this is a correct optimization to make. On the other hand, it means that the desired protection from Spectre-style attacks is not perfect, as an attacker could try to prevent a stale TLB entry from getting evicted, keeping it alive until the page it refers to is used by the guest for some sensitive data, and then targeting it using a spectre-gadget.
--- include/uapi/linux/kvm.h | 2 ++ virt/kvm/guest_memfd.c | 28 +++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-)
include/uapi/linux/kvm.h +2 -0
@@ -1573,6 +1573,8 @@ struct kvm_create_guest_memfd {__u64 reserved[6 ]; }; +#define KVM_GMEM_NO_DIRECT_MAP (1ULL << 0) +#define KVM_PRE_FAULT_MEMORY _IOWR(KVMIO, 0xd5, struct kvm_pre_fault_memory)struct kvm_pre_fault_memory {
virt/kvm/guest_memfd.c +27 -1
@@ -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"
@@ -42,8 +43,23 @@ static int __kvm_gmem_prepare_folio(struct kvm *kvm, struct kvm_memory_slot *sloreturn 0 ; } +static bool kvm_gmem_test_no_direct_map (struct inode *inode) +{ +return ((unsigned long ) inode->i_private) &KVM_GMEM_NO_DIRECT_MAP ; +} +static inline void kvm_gmem_mark_prepared (struct folio *folio) { +struct inode *inode =folio_inode (folio); + +if (kvm_gmem_test_no_direct_map (inode)) { +int r =set_direct_map_valid_noflush (folio_page (folio,0 ),folio_nr_pages (folio), + false); + +if (!r) +folio_set_private (folio); + } +folio_mark_uptodate (folio); }
@@ -479,6 +495,10 @@ static void kvm_gmem_free_folio(struct folio *folio)kvm_pfn_t pfn =page_to_pfn (page);int order =folio_order (folio); +if (folio_test_private (folio)) +WARN_ON_ONCE (set_direct_map_valid_noflush (folio_page (folio,0 ), +folio_nr_pages (folio), true)); + kvm_arch_gmem_invalidate(pfn, pfn + (1ul << order)); }#endif
@@ -552,6 +572,9 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags)/* Unmovable mappings are supposed to be marked unevictable as well. */ WARN_ON_ONCE (!mapping_unevictable (inode->i_mapping)); +if (flags &KVM_GMEM_NO_DIRECT_MAP ) +mapping_set_no_direct_map (inode->i_mapping); +kvm_get_kvm (kvm); gmem->kvm = kvm;xa_init (&gmem->bindings);
@@ -571,7 +594,10 @@ 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 (!can_set_direct_map ()) + valid_flags &= ~KVM_GMEM_NO_DIRECT_MAP ;if (flags & ~valid_flags)return -EINVAL ;
Signature · 1 line
2.48.1
Re: [PATCH v4 03/12] KVM: guest_memfd: Add flag to remove from direct map
Quoted 43 lines · Patrick Roy
Add
KVM_GMEM_NO_DIRECT_MAPflag forKVM_CREATE_GUEST_MEMFD()ioctl. When set,guest_memfdfolios will be removed from the direct map after preparation, with direct map entries only restored when the folios are freed.To ensure these folios do not end up in places where the kernel cannot deal with them, set
AS_NO_DIRECT_MAPon the guest_memfd's structaddress_spaceifKVM_GMEM_NO_DIRECT_MAPis requested.Note that this flag causes removal of direct map entries for all
guest_memfdfolios independent of whether they are "shared" or "private" (although currentguest_memfdonly supports either all folios in the "shared" state, or all folios in the "private" state if !IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM)). The usecase for removing direct map entries of also the shared parts ofguest_memfdare a special type of non-CoCo VM where, host userspace is trusted to have access to all of guest memory, but where Spectre-style transient execution attacks through the host kernel's direct map should still be mitigated.Note that KVM retains access to guest memory via userspace mappings of
guest_memfd, which are reflected back into KVM's memslots viauserspace_addr. This is needed for things like MMIO emulation onx86_64to work. Previous iterations attempted to instead have KVM temporarily restore direct map entries whenever such an access to guest memory was needed, but this turned out to have a significant performance impact, as well as additional complexity due to needing to refcount direct map reinsertion operations and making them play nicely with gmem truncations.This iteration also doesn't have KVM perform TLB flushes after direct map manipulations. This is because TLB flushes resulted in a up to 40x elongation of page faults in
guest_memfd(scaling with the number of CPU cores), or a 5x elongation of memory population. On the one hand, TLB flushes are not needed for functional correctness (thevirt->physmapping technically stays "correct", the kernel should simply to not it for a while), so this is a correct optimization to make. On the other hand, it means that the desired protection from Spectre-style attacks is not perfect, as an attacker could try to prevent a stale TLB entry from getting evicted, keeping it alive until the page it refers to is used by the guest for some sensitive data, and then targeting it using a spectre-gadget.Signed-off-by Patrick Roy <[email protected]>
...
virt/kvm/guest_memfd.c static int __kvm_gmem_prepare_folio(struct kvm *kvm, struct kvm_memory_slot *slo · line 57
Quoted 13 lines
virt/kvm/guest_memfd.c +10
-0+static bool kvm_gmem_test_no_direct_map (struct inode *inode) +{ +return ((unsigned long ) inode->i_private) &KVM_GMEM_NO_DIRECT_MAP ; +} +static inline void kvm_gmem_mark_prepared (struct folio *folio) { +struct inode *inode =folio_inode (folio); + +if (kvm_gmem_test_no_direct_map (inode)) { +int r =set_direct_map_valid_noflush (folio_page (folio,0 ),folio_nr_pages (folio), + false);
Will this work if KVM is built as a module, or is this another good reason why we might want guest_memfd core part of core-mm?
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v4 03/12] KVM: guest_memfd: Add flag to remove from direct map
Quoted 47 lines · Patrick Roy
Quoted 43 lines
Add
KVM_GMEM_NO_DIRECT_MAPflag forKVM_CREATE_GUEST_MEMFD()ioctl. When set,guest_memfdfolios will be removed from the direct map after preparation, with direct map entries only restored when the folios are freed.To ensure these folios do not end up in places where the kernel cannot deal with them, set
AS_NO_DIRECT_MAPon the guest_memfd's structaddress_spaceifKVM_GMEM_NO_DIRECT_MAPis requested.Note that this flag causes removal of direct map entries for all
guest_memfdfolios independent of whether they are "shared" or "private" (although currentguest_memfdonly supports either all folios in the "shared" state, or all folios in the "private" state if !IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM)). The usecase for removing direct map entries of also the shared parts ofguest_memfdare a special type of non-CoCo VM where, host userspace is trusted to have access to all of guest memory, but where Spectre-style transient execution attacks through the host kernel's direct map should still be mitigated.Note that KVM retains access to guest memory via userspace mappings of
guest_memfd, which are reflected back into KVM's memslots viauserspace_addr. This is needed for things like MMIO emulation onx86_64to work. Previous iterations attempted to instead have KVM temporarily restore direct map entries whenever such an access to guest memory was needed, but this turned out to have a significant performance impact, as well as additional complexity due to needing to refcount direct map reinsertion operations and making them play nicely with gmem truncations.This iteration also doesn't have KVM perform TLB flushes after direct map manipulations. This is because TLB flushes resulted in a up to 40x elongation of page faults in
guest_memfd(scaling with the number of CPU cores), or a 5x elongation of memory population. On the one hand, TLB flushes are not needed for functional correctness (thevirt->physmapping technically stays "correct", the kernel should simply to not it for a while), so this is a correct optimization to make. On the other hand, it means that the desired protection from Spectre-style attacks is not perfect, as an attacker could try to prevent a stale TLB entry from getting evicted, keeping it alive until the page it refers to is used by the guest for some sensitive data, and then targeting it using a spectre-gadget.Signed-off-by Patrick Roy <[email protected]>...
Quoted 1 line
Quoted 15 lines
virt/kvm/guest_memfd.c static int __kvm_gmem_prepare_folio(struct kvm *kvm, struct kvm_memory_slot *slo · line 57
Quoted 12 lines
virt/kvm/guest_memfd.c +10
-0+static bool kvm_gmem_test_no_direct_map (struct inode *inode) +{ +return ((unsigned long ) inode->i_private) &KVM_GMEM_NO_DIRECT_MAP ; +} +static inline void kvm_gmem_mark_prepared (struct folio *folio) { +struct inode *inode =folio_inode (folio); + +if (kvm_gmem_test_no_direct_map (inode)) { +int r =set_direct_map_valid_noflush (folio_page (folio,0 ),folio_nr_pages (folio), + false);Will this work if KVM is built as a module, or is this another good reason why we might want
guest_memfdcore part of core-mm?
mh, I'm admittedly not too familiar with the differences that would come from building KVM as a module vs not. I do remember something about the direct map accessors not being available for modules, so this would indeed not work. Does that mean moving gmem into core-mm will be a pre-requisite for the direct map removal stuff?
Quoted 5 lines
Signature · 3 lines
Cheers, David / dhildenb
Best,
Patrick
Re: [PATCH v4 03/12] KVM: guest_memfd: Add flag to remove from direct map
Quoted 66 lines · Patrick Roy
Quoted 47 lines · Patrick Roy
Quoted 43 lines
Add
KVM_GMEM_NO_DIRECT_MAPflag forKVM_CREATE_GUEST_MEMFD()ioctl. When set,guest_memfdfolios will be removed from the direct map after preparation, with direct map entries only restored when the folios are freed.To ensure these folios do not end up in places where the kernel cannot deal with them, set
AS_NO_DIRECT_MAPon the guest_memfd's structaddress_spaceifKVM_GMEM_NO_DIRECT_MAPis requested.Note that this flag causes removal of direct map entries for all
guest_memfdfolios independent of whether they are "shared" or "private" (although currentguest_memfdonly supports either all folios in the "shared" state, or all folios in the "private" state if !IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM)). The usecase for removing direct map entries of also the shared parts ofguest_memfdare a special type of non-CoCo VM where, host userspace is trusted to have access to all of guest memory, but where Spectre-style transient execution attacks through the host kernel's direct map should still be mitigated.Note that KVM retains access to guest memory via userspace mappings of
guest_memfd, which are reflected back into KVM's memslots viauserspace_addr. This is needed for things like MMIO emulation onx86_64to work. Previous iterations attempted to instead have KVM temporarily restore direct map entries whenever such an access to guest memory was needed, but this turned out to have a significant performance impact, as well as additional complexity due to needing to refcount direct map reinsertion operations and making them play nicely with gmem truncations.This iteration also doesn't have KVM perform TLB flushes after direct map manipulations. This is because TLB flushes resulted in a up to 40x elongation of page faults in
guest_memfd(scaling with the number of CPU cores), or a 5x elongation of memory population. On the one hand, TLB flushes are not needed for functional correctness (thevirt->physmapping technically stays "correct", the kernel should simply to not it for a while), so this is a correct optimization to make. On the other hand, it means that the desired protection from Spectre-style attacks is not perfect, as an attacker could try to prevent a stale TLB entry from getting evicted, keeping it alive until the page it refers to is used by the guest for some sensitive data, and then targeting it using a spectre-gadget.Signed-off-by Patrick Roy <[email protected]>...
Quoted 1 line
Quoted 15 lines
virt/kvm/guest_memfd.c static int __kvm_gmem_prepare_folio(struct kvm *kvm, struct kvm_memory_slot *slo · line 57
Quoted 12 lines
virt/kvm/guest_memfd.c +10
-0+static bool kvm_gmem_test_no_direct_map (struct inode *inode) +{ +return ((unsigned long ) inode->i_private) &KVM_GMEM_NO_DIRECT_MAP ; +} +static inline void kvm_gmem_mark_prepared (struct folio *folio) { +struct inode *inode =folio_inode (folio); + +if (kvm_gmem_test_no_direct_map (inode)) { +int r =set_direct_map_valid_noflush (folio_page (folio,0 ),folio_nr_pages (folio), + false);Will this work if KVM is built as a module, or is this another good reason why we might want
guest_memfdcore part of core-mm?
Quoted 5 lines
mh, I'm admittedly not too familiar with the differences that would come from building KVM as a module vs not. I do remember something about the direct map accessors not being available for modules, so this would indeed not work. Does that mean moving gmem into core-mm will be a pre-requisite for the direct map removal stuff?
Likely, we'd need some shim.
Maybe for the time being it could be fenced using #if IS_BUILTIN() ... but that sure won't win in a beauty contest.
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v4 03/12] KVM: guest_memfd: Add flag to remove from direct map
Quoted 73 lines · David Hildenbrand
Quoted 66 lines · Patrick Roy
Quoted 47 lines · Patrick Roy
Quoted 43 lines
Add
KVM_GMEM_NO_DIRECT_MAPflag forKVM_CREATE_GUEST_MEMFD()ioctl. When set,guest_memfdfolios will be removed from the direct map after preparation, with direct map entries only restored when the folios are freed.To ensure these folios do not end up in places where the kernel cannot deal with them, set
AS_NO_DIRECT_MAPon the guest_memfd's structaddress_spaceifKVM_GMEM_NO_DIRECT_MAPis requested.Note that this flag causes removal of direct map entries for all
guest_memfdfolios independent of whether they are "shared" or "private" (although currentguest_memfdonly supports either all folios in the "shared" state, or all folios in the "private" state if !IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM)). The usecase for removing direct map entries of also the shared parts ofguest_memfdare a special type of non-CoCo VM where, host userspace is trusted to have access to all of guest memory, but where Spectre-style transient execution attacks through the host kernel's direct map should still be mitigated.Note that KVM retains access to guest memory via userspace mappings of
guest_memfd, which are reflected back into KVM's memslots viauserspace_addr. This is needed for things like MMIO emulation onx86_64to work. Previous iterations attempted to instead have KVM temporarily restore direct map entries whenever such an access to guest memory was needed, but this turned out to have a significant performance impact, as well as additional complexity due to needing to refcount direct map reinsertion operations and making them play nicely with gmem truncations.This iteration also doesn't have KVM perform TLB flushes after direct map manipulations. This is because TLB flushes resulted in a up to 40x elongation of page faults in
guest_memfd(scaling with the number of CPU cores), or a 5x elongation of memory population. On the one hand, TLB flushes are not needed for functional correctness (thevirt->physmapping technically stays "correct", the kernel should simply to not it for a while), so this is a correct optimization to make. On the other hand, it means that the desired protection from Spectre-style attacks is not perfect, as an attacker could try to prevent a stale TLB entry from getting evicted, keeping it alive until the page it refers to is used by the guest for some sensitive data, and then targeting it using a spectre-gadget.Signed-off-by Patrick Roy <[email protected]>...
Quoted 1 line
Quoted 15 lines
virt/kvm/guest_memfd.c static int __kvm_gmem_prepare_folio(struct kvm *kvm, struct kvm_memory_slot *slo · line 57
Quoted 12 lines
virt/kvm/guest_memfd.c +10
-0+static bool kvm_gmem_test_no_direct_map (struct inode *inode) +{ +return ((unsigned long ) inode->i_private) &KVM_GMEM_NO_DIRECT_MAP ; +} +static inline void kvm_gmem_mark_prepared (struct folio *folio) { +struct inode *inode =folio_inode (folio); + +if (kvm_gmem_test_no_direct_map (inode)) { +int r =set_direct_map_valid_noflush (folio_page (folio,0 ),folio_nr_pages (folio), + false);Will this work if KVM is built as a module, or is this another good reason why we might want
guest_memfdcore part of core-mm?Quoted 5 lines
mh, I'm admittedly not too familiar with the differences that would come from building KVM as a module vs not. I do remember something about the direct map accessors not being available for modules, so this would indeed not work. Does that mean moving gmem into core-mm will be a pre-requisite for the direct map removal stuff?
Quoted 4 lines
Likely, we'd need some shim.
Maybe for the time being it could be fenced using #if
IS_BUILTIN()... but that sure won't win in a beauty contest.
Is anyone working on such a shim at the moment? Otherwise, would it make sense for me to look into it? (although I'll probably need a pointer or two for what is actually needed)
I saw your comment on Fuad's series [1] indicating that he'll also need some shim, so probably makes sense to tackle it anyway instead of hacking around it with #if-ery.
[1]: https://lore.kernel.org/kvm/[email protected]/
Quoted 5 lines
Signature · 3 lines
Cheers, David / dhildenb
Best,
Patrick
Re: [PATCH v4 03/12] KVM: guest_memfd: Add flag to remove from direct map
Quoted 81 lines · Patrick Roy
Quoted 73 lines · David Hildenbrand
Quoted 66 lines · Patrick Roy
Quoted 47 lines · Patrick Roy
Quoted 43 lines
Add
KVM_GMEM_NO_DIRECT_MAPflag forKVM_CREATE_GUEST_MEMFD()ioctl. When set,guest_memfdfolios will be removed from the direct map after preparation, with direct map entries only restored when the folios are freed.To ensure these folios do not end up in places where the kernel cannot deal with them, set
AS_NO_DIRECT_MAPon the guest_memfd's structaddress_spaceifKVM_GMEM_NO_DIRECT_MAPis requested.Note that this flag causes removal of direct map entries for all
guest_memfdfolios independent of whether they are "shared" or "private" (although currentguest_memfdonly supports either all folios in the "shared" state, or all folios in the "private" state if !IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM)). The usecase for removing direct map entries of also the shared parts ofguest_memfdare a special type of non-CoCo VM where, host userspace is trusted to have access to all of guest memory, but where Spectre-style transient execution attacks through the host kernel's direct map should still be mitigated.Note that KVM retains access to guest memory via userspace mappings of
guest_memfd, which are reflected back into KVM's memslots viauserspace_addr. This is needed for things like MMIO emulation onx86_64to work. Previous iterations attempted to instead have KVM temporarily restore direct map entries whenever such an access to guest memory was needed, but this turned out to have a significant performance impact, as well as additional complexity due to needing to refcount direct map reinsertion operations and making them play nicely with gmem truncations.This iteration also doesn't have KVM perform TLB flushes after direct map manipulations. This is because TLB flushes resulted in a up to 40x elongation of page faults in
guest_memfd(scaling with the number of CPU cores), or a 5x elongation of memory population. On the one hand, TLB flushes are not needed for functional correctness (thevirt->physmapping technically stays "correct", the kernel should simply to not it for a while), so this is a correct optimization to make. On the other hand, it means that the desired protection from Spectre-style attacks is not perfect, as an attacker could try to prevent a stale TLB entry from getting evicted, keeping it alive until the page it refers to is used by the guest for some sensitive data, and then targeting it using a spectre-gadget.Signed-off-by Patrick Roy <[email protected]>...
Quoted 1 line
Quoted 15 lines
virt/kvm/guest_memfd.c static int __kvm_gmem_prepare_folio(struct kvm *kvm, struct kvm_memory_slot *slo · line 57
Quoted 12 lines
virt/kvm/guest_memfd.c +10
-0+static bool kvm_gmem_test_no_direct_map (struct inode *inode) +{ +return ((unsigned long ) inode->i_private) &KVM_GMEM_NO_DIRECT_MAP ; +} +static inline void kvm_gmem_mark_prepared (struct folio *folio) { +struct inode *inode =folio_inode (folio); + +if (kvm_gmem_test_no_direct_map (inode)) { +int r =set_direct_map_valid_noflush (folio_page (folio,0 ),folio_nr_pages (folio), + false);Will this work if KVM is built as a module, or is this another good reason why we might want
guest_memfdcore part of core-mm?Quoted 5 lines
mh, I'm admittedly not too familiar with the differences that would come from building KVM as a module vs not. I do remember something about the direct map accessors not being available for modules, so this would indeed not work. Does that mean moving gmem into core-mm will be a pre-requisite for the direct map removal stuff?
Quoted 4 lines
Likely, we'd need some shim.
Maybe for the time being it could be fenced using #if
IS_BUILTIN()... but that sure won't win in a beauty contest.
Quoted 7 lines
Is anyone working on such a shim at the moment? Otherwise, would it make sense for me to look into it? (although I'll probably need a pointer or two for what is actually needed)
I saw your comment on Fuad's series [1] indicating that he'll also need some shim, so probably makes sense to tackle it anyway instead of hacking around it with #if-ery.
Elliot (CC) was working on "guestmem library" project [1], but it was unclear what we could factor out into the core.
Looks like a simple shim for such stuff might be a good starting point, although not the final idea of encapsulating more in the library.
@Elliot, are you currently still looking into this?
[1] https://lore.kernel.org/all/[email protected]/T/#u
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v4 03/12] KVM: guest_memfd: Add flag to remove from direct map
Hi David!
Quoted 90 lines · David Hildenbrand
Quoted 81 lines · Patrick Roy
Quoted 73 lines · David Hildenbrand
Quoted 66 lines · Patrick Roy
Quoted 47 lines · Patrick Roy
Quoted 43 lines
Add
KVM_GMEM_NO_DIRECT_MAPflag forKVM_CREATE_GUEST_MEMFD()ioctl. When set,guest_memfdfolios will be removed from the direct map after preparation, with direct map entries only restored when the folios are freed.To ensure these folios do not end up in places where the kernel cannot deal with them, set
AS_NO_DIRECT_MAPon the guest_memfd's structaddress_spaceifKVM_GMEM_NO_DIRECT_MAPis requested.Note that this flag causes removal of direct map entries for all
guest_memfdfolios independent of whether they are "shared" or "private" (although currentguest_memfdonly supports either all folios in the "shared" state, or all folios in the "private" state if !IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM)). The usecase for removing direct map entries of also the shared parts ofguest_memfdare a special type of non-CoCo VM where, host userspace is trusted to have access to all of guest memory, but where Spectre-style transient execution attacks through the host kernel's direct map should still be mitigated.Note that KVM retains access to guest memory via userspace mappings of
guest_memfd, which are reflected back into KVM's memslots viauserspace_addr. This is needed for things like MMIO emulation onx86_64to work. Previous iterations attempted to instead have KVM temporarily restore direct map entries whenever such an access to guest memory was needed, but this turned out to have a significant performance impact, as well as additional complexity due to needing to refcount direct map reinsertion operations and making them play nicely with gmem truncations.This iteration also doesn't have KVM perform TLB flushes after direct map manipulations. This is because TLB flushes resulted in a up to 40x elongation of page faults in
guest_memfd(scaling with the number of CPU cores), or a 5x elongation of memory population. On the one hand, TLB flushes are not needed for functional correctness (thevirt->physmapping technically stays "correct", the kernel should simply to not it for a while), so this is a correct optimization to make. On the other hand, it means that the desired protection from Spectre-style attacks is not perfect, as an attacker could try to prevent a stale TLB entry from getting evicted, keeping it alive until the page it refers to is used by the guest for some sensitive data, and then targeting it using a spectre-gadget.Signed-off-by Patrick Roy <[email protected]>...
Quoted 1 line
Quoted 15 lines
virt/kvm/guest_memfd.c static int __kvm_gmem_prepare_folio(struct kvm *kvm, struct kvm_memory_slot *slo · line 57
Quoted 12 lines
virt/kvm/guest_memfd.c +10
-0+static bool kvm_gmem_test_no_direct_map (struct inode *inode) +{ +return ((unsigned long ) inode->i_private) &KVM_GMEM_NO_DIRECT_MAP ; +} +static inline void kvm_gmem_mark_prepared (struct folio *folio) { +struct inode *inode =folio_inode (folio); + +if (kvm_gmem_test_no_direct_map (inode)) { +int r =set_direct_map_valid_noflush (folio_page (folio,0 ),folio_nr_pages (folio), + false);Will this work if KVM is built as a module, or is this another good reason why we might want
guest_memfdcore part of core-mm?Quoted 5 lines
mh, I'm admittedly not too familiar with the differences that would come from building KVM as a module vs not. I do remember something about the direct map accessors not being available for modules, so this would indeed not work. Does that mean moving gmem into core-mm will be a pre-requisite for the direct map removal stuff?
Quoted 4 lines
Likely, we'd need some shim.
Maybe for the time being it could be fenced using #if
IS_BUILTIN()... but that sure won't win in a beauty contest.Quoted 7 lines
Is anyone working on such a shim at the moment? Otherwise, would it make sense for me to look into it? (although I'll probably need a pointer or two for what is actually needed)
I saw your comment on Fuad's series [1] indicating that he'll also need some shim, so probably makes sense to tackle it anyway instead of hacking around it with #if-ery.
Quoted 5 lines
Elliot (CC) was working on "guestmem library" project [1], but it was unclear what we could factor out into the core.
Looks like a simple shim for such stuff might be a good starting point, although not the final idea of encapsulating more in the library.
So I started looking into this based on what we talked about at the last guest_memfd sync. I tried to sort of go the way you hinted at when this topic of "direct map removal from modules" came up in the past [1], and hide it behind some sort of "alloc/free" abstraction. E.g. have the library/shim expose gmem_get_folio(struct inode *inode, pgoff_t index) that is a sorta equivalent of today __kvm_gmem_get_pfn(), which grabs a new folio from the filemap, prepares it via a callback provided by KVM, and then direct map removes it before returning it proper. But then, that could still be "abused" by module code to just remove arbitrary folios from the direct map, if a caller messed up any old struct inode to look sufficiently like a gmem inode for the purposes of gmem_get_folio(). But I also couldn't really come up with anything that _wouldn't_ allow something like this. What're your thoughts on this? Do we need to find a way to prevent this sort of stuff, and is that even possible? I checked some of Elliot's old submissions that contain direct map removal as part of the library and they run into the same problem.
Best,
Patrick
[1]: https://lore.kernel.org/all/[email protected]/
Quoted 10 lines
@Elliot, are you currently still looking into this?
[1]
https://lore.kernel.org/all/[email protected]/T/#uSignature · 3 lines
Cheers, David / dhildenb
PATCH v4 4/12 KVM: Add capability to discover KVM_GMEM_NO_DIRECT_MAP support
In reply to Patrick RoyAdd a capability to let userspace discover whether guest_memfd supports removing its folios from the direct map. Support depends on guest_memfd itself being supported, but also on whether KVM can manipulate the direct map at page granularity at all (possible most of the time, just arm64 is a notable outlier where its impossible if the direct map has been setup using hugepages, as arm64 cannot break these apart due to break-before-make semantics).
--- include/uapi/linux/kvm.h | 1 + virt/kvm/kvm_main.c | 5 +++++ 2 files changed, 6 insertions(+)
include/uapi/linux/kvm.h +1 -0
@@ -930,6 +930,7 @@ struct kvm_enable_cap {#define KVM_CAP_X86_APIC_BUS_CYCLES_NS 237#define KVM_CAP_X86_GUEST_MODE 238#define KVM_CAP_GMEM_SHARED_MEM 239 +#define KVM_CAP_GMEM_NO_DIRECT_MAP 240struct kvm_irq_routing_irqchip {__u32 irqchip;
virt/kvm/kvm_main.c +5 -0
@@ -65,6 +65,7 @@#include <trace/events/kvm.h> #include <linux/kvm_dirty_ring.h> +#include <linux/set_memory.h> /* Worst case buffer size needed for holding an integer. */
@@ -4823,6 +4824,10 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)return kvm_supported_mem_attributes (kvm);#endif #ifdef CONFIG_KVM_PRIVATE_MEM +case KVM_CAP_GMEM_NO_DIRECT_MAP : +if (!can_set_direct_map ()) +return false; + fallthrough;case KVM_CAP_GUEST_MEMFD :return !kvm ||kvm_arch_has_private_mem (kvm);#endif
Signature · 1 line
2.48.1
Re: [PATCH v4 04/12] KVM: Add capability to discover KVM_GMEM_NO_DIRECT_MAP support
Quoted 10 lines · Patrick Roy
Add a capability to let userspace discover whether
guest_memfdsupports removing its folios from the direct map. Support depends onguest_memfditself being supported, but also on whether KVM can manipulate the direct map at page granularity at all (possible most of the time, just arm64 is a notable outlier where its impossible if the direct map has been setup using hugepages, as arm64 cannot break these apart due to break-before-make semantics).Signed-off-by Patrick Roy <[email protected]>---
Not sure how KVM folks handle that, but I suspect we would just want to squash that into the previous commit,
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v4 04/12] KVM: Add capability to discover KVM_GMEM_NO_DIRECT_MAP support
Quoted 9 lines · David Hildenbrand
Quoted 8 lines · Patrick Roy
Add a capability to let userspace discover whether
guest_memfdsupports removing its folios from the direct map. Support depends onguest_memfditself being supported, but also on whether KVM can manipulate the direct map at page granularity at all (possible most of the time, just arm64 is a notable outlier where its impossible if the direct map has been setup using hugepages, as arm64 cannot break these apart due to break-before-make semantics).
Quoted 11 lines
Quoted 2 lines
Signed-off-by Patrick Roy <[email protected]>---
Not sure how KVM folks handle that, but I suspect we would just want to squash that into the previous commit,
Signature · 3 lines
Cheers, David / dhildenb
Ah, yeah, I just had a look at the commit history in this file and indeed these have seem to usually be squashed. Will do so too, thanks!
Best,
Patrick
PATCH v4 5/12 KVM: Documentation: document KVM_GMEM_NO_DIRECT_MAP flag
In reply to Patrick Roy--- Documentation/virt/kvm/api.rst | 13 +++++++++++++ 1 file changed, 13 insertions(+)
Documentation/virt/kvm/api.rst +13 -0
@@ -6388,6 +6388,19 @@ a single guest_memfd file, but the bound ranges must not overlap). See KVM_SET_USER_MEMORY_REGION2 for additional details. +The following flags are defined: + +KVM_GMEM_NO_DIRECT_MAP + Ensure memory backing this guest_memfd inode is unmapped from the kernel's + address space. Check KVM_CAP_GMEM_NO_DIRECT_MAP for support. + +Errors: + + ========== =============================================================== + EINVAL The specified `flags` were invalid or not supported. + ========== =============================================================== + + 4.143 KVM_PRE_FAULT_MEMORY ---------------------------
Signature · 1 line
2.48.1
PATCH v4 6/12 KVM: selftests: load elf via bounce buffer
In reply to Patrick RoyIf guest memory is backed using a VMA that does not allow GUP (e.g. a userspace mapping of guest_memfd when the fd was allocated using KVM_GMEM_NO_DIRECT_MAP), then directly loading the test ELF binary into it via read(2) potentially does not work. To nevertheless support loading binaries in this cases, do the read(2) syscall using a bounce buffer, and then memcpy from the bounce buffer into guest memory.
--- .../testing/selftests/kvm/include/test_util.h | 1 + tools/testing/selftests/kvm/lib/elf.c | 8 +++---- tools/testing/selftests/kvm/lib/io.c | 23 +++++++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-)
tools/testing/selftests/kvm/include/test_util.h +1 -0
@@ -46,6 +46,7 @@ do { \ssize_t test_write (int fd,const void *buf,size_t count);ssize_t test_read (int fd,void *buf,size_t count); +ssize_t test_read_bounce (int fd,void *buf,size_t count);int test_seq_read (const char *path,char **bufp,size_t *sizep); void __printf(5, 6) test_assert(bool exp, const char *exp_str,
tools/testing/selftests/kvm/lib/elf.c +4 -4
@@ -31,7 +31,7 @@ static void elfhdr_get(const char *filename, Elf64_Ehdr *hdrp)* the real size of the ELF header. */ unsigned char ident[EI_NIDENT ]; -test_read (fd, ident,sizeof (ident)); +test_read_bounce (fd, ident,sizeof (ident));TEST_ASSERT ((ident[EI_MAG0 ] ==ELFMAG0 ) && (ident[EI_MAG1 ] ==ELFMAG1 ) && (ident[EI_MAG2 ] ==ELFMAG2 ) && (ident[EI_MAG3 ] ==ELFMAG3 ), "ELF MAGIC Mismatch,\n"
@@ -79,7 +79,7 @@ static void elfhdr_get(const char *filename, Elf64_Ehdr *hdrp) offset_rv =lseek (fd,0 ,SEEK_SET );TEST_ASSERT (offset_rv ==0 ,"Seek to ELF header failed,\n" " rv: %zi expected: %i" , offset_rv,0 ); -test_read (fd, hdrp,sizeof (*hdrp)); +test_read_bounce (fd, hdrp,sizeof (*hdrp));TEST_ASSERT (hdrp->e_phentsize ==sizeof (Elf64_Phdr),"Unexpected physical header size,\n" " hdrp->e_phentsize: %x\n"
@@ -146,7 +146,7 @@ void kvm_vm_elf_load(struct kvm_vm *vm, const char *filename)/* Read in the program header. */ Elf64_Phdr phdr; -test_read (fd, &phdr,sizeof (phdr)); +test_read_bounce (fd, &phdr,sizeof (phdr));/* Skip if this header doesn't describe a loadable segment. */ if (phdr.p_type !=PT_LOAD )
@@ -187,7 +187,7 @@ void kvm_vm_elf_load(struct kvm_vm *vm, const char *filename)" expected: 0x%jx" , n1, errno, (intmax_t ) offset_rv, (intmax_t) phdr.p_offset); -test_read (fd,addr_gva2hva (vm, phdr.p_vaddr), +test_read_bounce (fd,addr_gva2hva (vm, phdr.p_vaddr), phdr.p_filesz); } }
tools/testing/selftests/kvm/lib/io.c +23 -0
@@ -155,3 +155,26 @@ ssize_t test_read(int fd, void *buf, size_t count)return num_read; } + +/* Test read via intermediary buffer +* +* Same as test_read, except read(2)s happen into a bounce buffer that is memcpy'd +* to buf. For use with buffers that cannot be GUP'd (e.g. guest_memfd VMAs if +* guest_memfd was allocated with KVM_GMEM_NO_DIRECT_MAP). +*/ +ssize_t test_read_bounce (int fd,void *buf,size_t count) +{ +void *bounce_buffer; +ssize_t num_read; + +TEST_ASSERT (count >=0 ,"Unexpected count, count: %li" , count); + + bounce_buffer =malloc (count); +TEST_ASSERT (bounce_buffer !=NULL ,"Failed to allocate bounce buffer" ); + + num_read =test_read (fd, bounce_buffer, count); +memcpy (buf, bounce_buffer, num_read); +free (bounce_buffer); + +return num_read; +}
Signature · 1 line
2.48.1
PATCH v4 7/12 KVM: selftests: set KVM_MEM_GUEST_MEMFD in vm_mem_add() if guest_memfd != -1
In reply to Patrick RoyHave vm_mem_add() always set KVM_MEM_GUEST_MEMFD in the memslot flags if a guest_memfd is passed in as an argument. This eliminates the possibility where a guest_memfd instance is passed to vm_mem_add(), but it ends up being ignored because the flags argument does not specify KVM_MEM_GUEST_MEMFD at the same time.
This makes it easy to support more scenarios in which no vm_mem_add() is not passed a guest_memfd instance, but is expected to allocate one. Currently, this only happens if guest_memfd == -1 but flags & KVM_MEM_GUEST_MEMFD != 0, but later vm_mem_add() will gain support for loading the test code itself into guest_memfd (via KVM_GMEM_SHARED_MEM) if requested via a special vm_mem_backing_src_type, at which point having to make sure the src_type and flags are in-sync becomes cumbersome.
--- tools/testing/selftests/kvm/lib/kvm_util.c | 26 +++++++++++++--------- 1 file changed, 15 insertions(+), 11 deletions(-)
tools/testing/selftests/kvm/lib/kvm_util.c +15 -11
@@ -1017,22 +1017,26 @@ void vm_mem_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type, region->backing_src_type = src_type; -if (flags &KVM_MEM_GUEST_MEMFD ) { -if (guest_memfd <0 ) { +if (guest_memfd <0 ) { +if (flags &KVM_MEM_GUEST_MEMFD ) {uint32_t guest_memfd_flags =0 ;TEST_ASSERT (!guest_memfd_offset,"Offset must be zero when creating new guest_memfd" ); guest_memfd =vm_create_guest_memfd (vm, mem_size, guest_memfd_flags); - }else { -/* -* Install a unique fd for each memslot so that the fd -* can be closed when the region is deleted without -* needing to track if the fd is owned by the framework -* or by the caller. -*/ - guest_memfd =dup (guest_memfd); -TEST_ASSERT (guest_memfd >=0 ,__KVM_SYSCALL_ERROR ("dup()" , guest_memfd)); } + }else { +/* +* Install a unique fd for each memslot so that the fd +* can be closed when the region is deleted without +* needing to track if the fd is owned by the framework +* or by the caller. +*/ + guest_memfd =dup (guest_memfd); +TEST_ASSERT (guest_memfd >=0 ,__KVM_SYSCALL_ERROR ("dup()" , guest_memfd)); + } + +if (guest_memfd >0 ) { + flags |=KVM_MEM_GUEST_MEMFD ; region->region.guest_memfd = guest_memfd; region->region.guest_memfd_offset = guest_memfd_offset;
Signature · 1 line
2.48.1
PATCH v4 8/12 KVM: selftests: Add guest_memfd based vm_mem_backing_src_types
In reply to Patrick RoyAllow selftests to configure their memslots such that userspace_addr is set to a MAP_SHARED mapping of the guest_memfd that's associated with the memslot. This setup is the configuration for non-CoCo VMs, where all guest memory is backed by a guest_memfd whose folios are all marked shared, but KVM is still able to access guest memory to provide functionality such as MMIO emulation on x86.
Add backing types for normal guest_memfd, as well as direct map removed guest_memfd.
If KVM_CAP_MEMORY_ATTRIBUTES is available, explicitly set gmem-enabled memslots to private, as otherwise guest page faults will be resolved by GUP-ing the guest_memfd VMA (instead of using the special VMA-less guest_memfd fault code in the KVM MMU), but this is not always supported (e.g. if direct map entries are not available).
--- .../testing/selftests/kvm/include/kvm_util.h | 10 +++ .../testing/selftests/kvm/include/test_util.h | 7 ++ tools/testing/selftests/kvm/lib/kvm_util.c | 67 ++++++++++--------- tools/testing/selftests/kvm/lib/test_util.c | 8 +++ 4 files changed, 62 insertions(+), 30 deletions(-)
tools/testing/selftests/kvm/include/kvm_util.h +10 -0
@@ -544,6 +544,16 @@ static inline uint64_t vm_get_stat(struct kvm_vm *vm, const char *stat_name)void vm_create_irqchip (struct kvm_vm *vm); +static inline bool backing_src_guest_memfd_flags (enum vm_mem_backing_src_type t) +{ +switch (t) { +case VM_MEM_SRC_GUEST_MEMFD_NO_DIRECT_MAP : +return KVM_GMEM_NO_DIRECT_MAP ; +default : +return 0 ; + } +} +static inline int __vm_create_guest_memfd (struct kvm_vm *vm,uint64_t size,uint64_t flags) {
tools/testing/selftests/kvm/include/test_util.h +7 -0
@@ -133,6 +133,8 @@ enum vm_mem_backing_src_type {VM_MEM_SRC_ANONYMOUS_HUGETLB_16GB ,VM_MEM_SRC_SHMEM ,VM_MEM_SRC_SHARED_HUGETLB , +VM_MEM_SRC_GUEST_MEMFD , +VM_MEM_SRC_GUEST_MEMFD_NO_DIRECT_MAP , NUM_SRC_TYPES, };
@@ -164,6 +166,11 @@ static inline bool backing_src_is_shared(enum vm_mem_backing_src_type t)return vm_mem_backing_src_alias (t)->flag &MAP_SHARED ; } +static inline bool backing_src_is_guest_memfd (enum vm_mem_backing_src_type t) +{ +return t ==VM_MEM_SRC_GUEST_MEMFD || t ==VM_MEM_SRC_GUEST_MEMFD_NO_DIRECT_MAP ; +} +static inline bool backing_src_can_be_huge (enum vm_mem_backing_src_type t) {return t !=VM_MEM_SRC_ANONYMOUS && t !=VM_MEM_SRC_SHMEM ;
tools/testing/selftests/kvm/lib/kvm_util.c +37 -30
93 lines not on this page · raw message
tools/testing/selftests/kvm/lib/test_util.c +8 -0
@@ -279,6 +279,14 @@ const struct vm_mem_backing_src_alias *vm_mem_backing_src_alias(uint32_t i)*/ .flag = MAP_SHARED, }, + [VM_MEM_SRC_GUEST_MEMFD] = { + .name = "guest_memfd", + .flag = MAP_SHARED, + }, + [VM_MEM_SRC_GUEST_MEMFD_NO_DIRECT_MAP] = { + .name = "guest_memfd_no_direct_map", + .flag = MAP_SHARED, + } };_Static_assert (ARRAY_SIZE (aliases) ==NUM_SRC_TYPES ,"Missing new backing src types?" );
Signature · 1 line
2.48.1
Re: [PATCH v4 08/12] KVM: selftests: Add guest_memfd based vm_mem_backing_src_types
On Fri, 2025-02-21 at 16:07 +0000, Patrick Roy wrote:
...
tools/testing/selftests/kvm/lib/kvm_util.c void vm_mem_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type, · line 1018
Quoted 9 lines
tools/testing/selftests/kvm/lib/kvm_util.c +3
-2@@ -985,10 +1013,13 @@ void vm_mem_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type,if (alignment >1 ) region->mmap_size += alignment; - region->fd =-1 ; -if (backing_src_is_shared (src_type)) +if (backing_src_is_guest_memfd (src_type)) + region->fd = guest_memfd; +else if (backing_src_is_guest_memfd (src_type))
Argh, this is nonsense. Should be
+ else if (backing_src_is_shared(src_type))
instead.
Quoted 7 lines
tools/testing/selftests/kvm/lib/kvm_util.c +2
-0region->fd =kvm_memfd_alloc (region->mmap_size, src_type ==VM_MEM_SRC_SHARED_HUGETLB ); + else + region->fd = -1; region->mmap_start = mmap(NULL, region->mmap_size, PROT_READ | PROT_WRITE,
...
PATCH v4 9/12 KVM: selftests: stuff vm_mem_backing_src_type into vm_shape
In reply to Patrick RoyUse one of the padding fields in struct vm_shape to carry an enum vm_mem_backing_src_type value, to give the option to overwrite the default of VM_MEM_SRC_ANONYMOUS in __vm_create().
Overwriting this default will allow tests to create VMs where the test code is backed by mmap'd guest_memfd instead of anonymous memory.
--- .../testing/selftests/kvm/include/kvm_util.h | 19 ++++++++++--------- tools/testing/selftests/kvm/lib/kvm_util.c | 2 +- tools/testing/selftests/kvm/lib/x86/sev.c | 1 + .../selftests/kvm/pre_fault_memory_test.c | 1 + 4 files changed, 13 insertions(+), 10 deletions(-)
tools/testing/selftests/kvm/include/kvm_util.h +10 -9
@@ -180,7 +180,7 @@ enum vm_guest_mode {struct vm_shape {uint32_t type;uint8_t mode; -uint8_t pad0; +uint8_t src_type;uint16_t pad1; };
@@ -188,14 +188,15 @@ kvm_static_assert(sizeof(struct vm_shape) == sizeof(uint64_t));#define VM_TYPE_DEFAULT 0 -#define VM_SHAPE (__mode) \ -({ \ - struct vm_shape shape = { \ - .mode = (__mode), \ - .type = VM_TYPE_DEFAULT \ - }; \ - \ - shape; \ +#define VM_SHAPE (__mode) \ +({ \ + struct vm_shape shape = { \ + .mode = (__mode), \ + .type = VM_TYPE_DEFAULT, \ + .src_type = VM_MEM_SRC_ANONYMOUS \ + }; \ + \ + shape; \ })#if defined(__aarch64__)
tools/testing/selftests/kvm/lib/kvm_util.c +1 -1
@@ -420,7 +420,7 @@ struct kvm_vm *__vm_create(struct vm_shape shape, uint32_t nr_runnable_vcpus, vm =____vm_create (shape); -vm_userspace_mem_region_add (vm,VM_MEM_SRC_ANONYMOUS ,0 ,0 , nr_pages,0 ); +vm_userspace_mem_region_add (vm, shape.src_type,0 ,0 , nr_pages,0 );for (i =0 ; i <NR_MEM_REGIONS ; i++) vm->memslots[i] =0 ;
tools/testing/selftests/kvm/lib/x86/sev.c +1 -0
@@ -118,6 +118,7 @@ struct kvm_vm *vm_sev_create_with_one_vcpu(uint32_t type, void *guest_code,struct vm_shape shape = { .mode =VM_MODE_DEFAULT , .type = type, + .src_type =VM_MEM_SRC_ANONYMOUS , };struct kvm_vm *vm;struct kvm_vcpu *cpus[1 ];
tools/testing/selftests/kvm/pre_fault_memory_test.c +1 -0
@@ -68,6 +68,7 @@ static void __test_pre_fault_memory(unsigned long vm_type, bool private)const struct vm_shape shape = { .mode =VM_MODE_DEFAULT , .type = vm_type, + .src_type =VM_MEM_SRC_ANONYMOUS , };struct kvm_vcpu *vcpu;struct kvm_run *run;
Signature · 1 line
2.48.1
PATCH v4 10/12 KVM: selftests: adjust test_create_guest_memfd_invalid
In reply to Patrick RoyBIT(0) is now a valid flag, corresponding to KVM_GMEM_NO_DIRECT_MAP, so adjust test_create_guest_memfd_invalid to no longer assert it as an invalid flag.
--- tools/testing/selftests/kvm/guest_memfd_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/kvm/guest_memfd_test.c +1 -1
@@ -170,7 +170,7 @@ static void test_create_guest_memfd_invalid(struct kvm_vm *vm) size); } -for (flag =BIT (0 ); flag; flag <<=1 ) { +for (flag =BIT (1 ); flag; flag <<=1 ) { fd =__vm_create_guest_memfd (vm, page_size, flag); TEST_ASSERT(fd == -1 && errno == EINVAL, "guest_memfd() with flag '0x%lx' should fail with EINVAL",
Signature · 1 line
2.48.1
PATCH v4 11/12 KVM: selftests: set KVM_GMEM_NO_DIRECT_MAP in mem conversion tests
In reply to Patrick RoyCover the scenario that the guest can fault in and write gmem-backed guest memory even if its direct map removed.
--- .../selftests/kvm/x86/private_mem_conversions_test.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/x86/private_mem_conversions_test.c +4 -3
@@ -367,7 +367,7 @@ static void *__test_mem_conversions(void *__vcpu) }static void test_mem_conversions (enum vm_mem_backing_src_type src_type,uint32_t nr_vcpus, -uint32_t nr_memslots) +uint32_t nr_memslots,uint64_t gmem_flags) {/* * Allocate enough memory so that each vCPU's chunk of memory can be
@@ -394,7 +394,7 @@ static void test_mem_conversions(enum vm_mem_backing_src_type src_type, uint32_tvm_enable_cap (vm,KVM_CAP_EXIT_HYPERCALL , (1 <<KVM_HC_MAP_GPA_RANGE )); - memfd =vm_create_guest_memfd (vm, memfd_size,0 ); + memfd =vm_create_guest_memfd (vm, memfd_size, gmem_flags);for (i =0 ; i < nr_memslots; i++) vm_mem_add(vm, src_type, BASE_DATA_GPA + slot_size * i,
@@ -477,7 +477,8 @@ int main(int argc, char *argv[]) } } -test_mem_conversions (src_type, nr_vcpus, nr_memslots); +test_mem_conversions (src_type, nr_vcpus, nr_memslots,0 ); +test_mem_conversions (src_type, nr_vcpus, nr_memslots,KVM_GMEM_NO_DIRECT_MAP );return 0 ; }
Signature · 1 line
2.48.1
PATCH v4 12/12 KVM: selftests: Test guest execution from direct map removed gmem
In reply to Patrick RoyAdd a selftest that loads itself into guest_memfd (via KVM_GMEM_SHARED_MEM) and triggers an MMIO exit when executed. This exercises x86 MMIO emulation code inside KVM for guest_memfd-backed memslots where the guest_memfd folios are direct map removed. Particularly, it validates that x86 MMIO emulation code (guest page table walks + instruction fetch) correctly accesses gmem through the VMA that's been reflected into the memslot's userspace_addr field (instead of trying to do direct map accesses).
--- .../selftests/kvm/set_memory_region_test.c | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+)
tools/testing/selftests/kvm/set_memory_region_test.c +40 -0
@@ -603,6 +603,42 @@ static void test_mmio_during_vectoring(void)kvm_vm_free (vm); } + +static void guest_code_trigger_mmio (void ) +{ +/* +* Read some GPA that is not backed by a memslot. KVM consider this +* as MMIO and tell userspace to emulate the read. +*/ +READ_ONCE (*((uint64_t *)MEM_REGION_GPA )); + +GUEST_DONE (); +} + +static void test_guest_memfd_mmio (void ) +{ +struct kvm_vm *vm; +struct kvm_vcpu *vcpu; +struct vm_shape shape = { + .mode =VM_MODE_DEFAULT , + .type =KVM_X86_SW_PROTECTED_VM , + .src_type =VM_MEM_SRC_GUEST_MEMFD_NO_DIRECT_MAP , + }; +pthread_t vcpu_thread; + +pr_info ("Testing MMIO emulation for instructions in gmem\n" ); + + vm =__vm_create_shape_with_one_vcpu (shape, &vcpu,0 , guest_code_trigger_mmio); + +virt_map (vm,MEM_REGION_GPA ,MEM_REGION_GPA ,1 ); + +pthread_create (&vcpu_thread,NULL , vcpu_worker, vcpu); + +/* If the MMIO read was successfully emulated, the vcpu thread will exit */ +pthread_join (vcpu_thread,NULL ); + +kvm_vm_free (vm); +}#endif int main (int argc,char *argv[])
@@ -630,6 +666,10 @@ int main(int argc, char *argv[]) (kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SW_PROTECTED_VM))) {test_add_private_memory_region ();test_add_overlapping_private_memory_regions (); +if (kvm_has_cap (KVM_CAP_GMEM_SHARED_MEM ) &&kvm_has_cap (KVM_CAP_GMEM_NO_DIRECT_MAP )) +test_guest_memfd_mmio (); +else +pr_info ("Skipping tests requiring KVM_CAP_GMEM_SHARED_MEM | KVM_CAP_GMEM_NO_DIRECT_MAP" ); } else {pr_info ("Skipping tests for KVM_MEM_GUEST_MEMFD memory regions\n" ); }
Signature · 1 line
2.48.1