Direct Map Removal Support for guest_memfd
Subsystems (27)
v12 cover Direct Map Removal Support for guest_memfd
From: Nikita Kalyazin <[email protected]>
[ based on kvm/next ]
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.
Additionally, a Firecracker branch with support for these VMs can be found on GitHub [2].
For more details, please refer to the v5 cover letter. No substantial changes in design have taken place since.
See also related write() syscall support in guest_memfd [3] where the interoperation between the two features is described.
Changes since v11:
- Ackerley/Sashiko: fix previously missed __set_pages_* argument update in __kernel_map_pages (patch 1)
- David: disallow large folios in folio_zap_direct_map (patch 2)
- David/Sashiko: check for folio_is_zone_device if mapping is NULL in gup_fast_folio_allowed (patch 4)
- Ackerley/Sashiko: kvm_arch_gmem_supports_no_direct_map to return false for SEV-SNP (patch 8).
- David: replace a redundant check for GUEST_MEMFD_FLAG_NO_DIRECT_MAP with a WARN_ON_ONCE (patch 10)
- David: assert the folio is locked when zapping direct map (patch 10)
- Ackerley/Sashiko: reorder operations to "zap then prepare" and "invalidate then restore" (patch 10)
v11: https://lore.kernel.org/kvm/[email protected]
v10: https://lore.kernel.org/kvm/[email protected]
v9: https://lore.kernel.org/kvm/[email protected]
v8: https://lore.kernel.org/kvm/[email protected]
v7: https://lore.kernel.org/kvm/[email protected]
v6: https://lore.kernel.org/kvm/[email protected]
v5: https://lore.kernel.org/kvm/[email protected]
v4: https://lore.kernel.org/kvm/[email protected]
RFCv3: https://lore.kernel.org/kvm/[email protected]
RFCv2: https://lore.kernel.org/kvm/[email protected]
RFCv1: https://lore.kernel.org/kvm/[email protected]
[1] https://download.vusec.net/papers/quarantine_raid23.pdf
[2] https://github.com/firecracker-microvm/firecracker/tree/feature/secret-hiding
[3] https://lore.kernel.org/kvm/[email protected]
Nikita Kalyazin (4):
set_memory: set_direct_map_* to take address
set_memory: add folio_{zap,restore}_direct_map helpers mm/secretmem: make use of folio_{zap,restore}_direct_map mm/gup: drop local variable in gup_fast_folio_allowed
Patrick Roy (12):
mm/gup: drop secretmem optimization from gup_fast_folio_allowed
mm: introduce AS_NO_DIRECT_MAP
KVM: guest_memfd: Add stub for kvm_arch_gmem_invalidate
KVM: x86: define kvm_arch_gmem_supports_no_direct_map()
KVM: arm64: define kvm_arch_gmem_supports_no_direct_map()
KVM: guest_memfd: Add flag to remove from direct map
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: cover GUEST_MEMFD_FLAG_NO_DIRECT_MAP in existing
selftests
KVM: selftests: stuff vm_mem_backing_src_type into vm_shape
KVM: selftests: Test guest execution from direct map removed gmem
Documentation/virt/kvm/api.rst | 21 +++--- arch/arm64/include/asm/kvm_host.h | 13 ++++ arch/arm64/include/asm/set_memory.h | 7 +- arch/arm64/mm/pageattr.c | 19 +++-- arch/loongarch/include/asm/set_memory.h | 7 +- arch/loongarch/mm/pageattr.c | 25 +++---- arch/riscv/include/asm/set_memory.h | 7 +- arch/riscv/mm/pageattr.c | 17 +++-- arch/s390/include/asm/set_memory.h | 7 +- arch/s390/mm/pageattr.c | 13 ++-- arch/x86/include/asm/kvm_host.h | 6 ++ arch/x86/include/asm/set_memory.h | 7 +- arch/x86/kvm/x86.c | 7 ++ arch/x86/mm/pat/set_memory.c | 27 +++---- include/linux/kvm_host.h | 14 ++++ include/linux/pagemap.h | 16 ++++ include/linux/secretmem.h | 18 ----- include/linux/set_memory.h | 22 +++++- include/uapi/linux/kvm.h | 1 + kernel/power/snapshot.c | 4 +- lib/buildid.c | 8 +- mm/execmem.c | 6 +- mm/gup.c | 47 ++++++------ mm/memory.c | 45 +++++++++++ mm/mlock.c | 2 +- mm/secretmem.c | 18 ++--- mm/vmalloc.c | 11 ++- .../testing/selftests/kvm/guest_memfd_test.c | 17 ++++- .../testing/selftests/kvm/include/kvm_util.h | 37 ++++++--- .../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 | 59 ++++++++------- 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 | 52 ++++++++++++- .../kvm/x86/private_mem_conversions_test.c | 7 +- virt/kvm/guest_memfd.c | 75 +++++++++++++++++-- 39 files changed, 489 insertions(+), 202 deletions(-)
base-commit: 24f9515de8778410e4b84c85b196c9850d2c1e18
Signature · 1 line
2.50.1
PATCH v12 1/16 set_memory: set_direct_map_* to take address
From: Nikita Kalyazin <[email protected]>
Let's convert set_direct_map_*() to take an address instead of a page to prepare for adding helpers that operate on folios; it will be more efficient to convert from a folio directly to an address without going through a page first.
--- arch/arm64/include/asm/set_memory.h | 7 ++++--- arch/arm64/mm/pageattr.c | 19 +++++++++-------- arch/loongarch/include/asm/set_memory.h | 7 ++++--- arch/loongarch/mm/pageattr.c | 25 ++++++++++------------- arch/riscv/include/asm/set_memory.h | 7 ++++--- arch/riscv/mm/pageattr.c | 17 ++++++++-------- arch/s390/include/asm/set_memory.h | 7 ++++--- arch/s390/mm/pageattr.c | 13 ++++++------ arch/x86/include/asm/set_memory.h | 7 ++++--- arch/x86/mm/pat/set_memory.c | 27 +++++++++++++------------ include/linux/set_memory.h | 9 +++++---- kernel/power/snapshot.c | 4 ++-- mm/execmem.c | 6 ++++-- mm/secretmem.c | 6 +++--- mm/vmalloc.c | 11 ++++++---- 15 files changed, 91 insertions(+), 81 deletions(-)
arch/arm64/include/asm/set_memory.h +4 -3
@@ -11,9 +11,10 @@ bool can_set_direct_map(void);int set_memory_valid (unsigned long addr,int numpages,int enable); -int set_direct_map_invalid_noflush (struct page *page); -int set_direct_map_default_noflush (struct page *page); -int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid); +int set_direct_map_invalid_noflush (const void *addr); +int set_direct_map_default_noflush (const void *addr); +int set_direct_map_valid_noflush (const void *addr,unsigned long numpages, +bool valid);bool kernel_page_present (struct page *page);int set_memory_encrypted (unsigned long addr,int numpages);
arch/arm64/mm/pageattr.c +9 -10
@@ -245,7 +245,7 @@ int set_memory_valid(unsigned long addr, int numpages, int enable) __pgprot(PTE_VALID)); } -int set_direct_map_invalid_noflush (struct page *page) +int set_direct_map_invalid_noflush (const void *addr) {pgprot_t clear_mask =__pgprot (PTE_VALID );pgprot_t set_mask =__pgprot (0 );
@@ -253,11 +253,11 @@ int set_direct_map_invalid_noflush(struct page *page)if (!can_set_direct_map ())return 0 ; -return update_range_prot ((unsigned long )page_address (page), -PAGE_SIZE , set_mask, clear_mask); +return update_range_prot ((unsigned long )addr,PAGE_SIZE , set_mask, + clear_mask); } -int set_direct_map_default_noflush (struct page *page) +int set_direct_map_default_noflush (const void *addr) {pgprot_t set_mask =__pgprot (PTE_VALID |PTE_WRITE );pgprot_t clear_mask =__pgprot (PTE_RDONLY );
@@ -265,8 +265,8 @@ int set_direct_map_default_noflush(struct page *page)if (!can_set_direct_map ())return 0 ; -return update_range_prot ((unsigned long )page_address (page), -PAGE_SIZE , set_mask, clear_mask); +return update_range_prot ((unsigned long )addr,PAGE_SIZE , set_mask, + clear_mask); } static int __set_memory_enc_dec(unsigned long addr,
@@ -349,14 +349,13 @@ int realm_register_memory_enc_ops(void)return arm64_mem_crypt_ops_register (&realm_crypt_ops); } -int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid) +int set_direct_map_valid_noflush (const void *addr,unsigned long numpages, +bool valid) { -unsigned long addr = (unsigned long )page_address (page); -if (!can_set_direct_map ())return 0 ; -return set_memory_valid (addr, nr, valid); +return set_memory_valid ((unsigned long )addr, numpages, valid); }#ifdef CONFIG_DEBUG_PAGEALLOC
arch/loongarch/include/asm/set_memory.h +4 -3
@@ -15,8 +15,9 @@ int set_memory_ro(unsigned long addr, int numpages);int set_memory_rw (unsigned long addr,int numpages);bool kernel_page_present (struct page *page); -int set_direct_map_default_noflush (struct page *page); -int set_direct_map_invalid_noflush (struct page *page); -int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid); +int set_direct_map_invalid_noflush (const void *addr); +int set_direct_map_default_noflush (const void *addr); +int set_direct_map_valid_noflush (const void *addr,unsigned long numpages, +bool valid);#endif /* _ASM_LOONGARCH_SET_MEMORY_H */
arch/loongarch/mm/pageattr.c +11 -14
@@ -198,32 +198,29 @@ bool kernel_page_present(struct page *page)return pte_present (ptep_get (pte)); } -int set_direct_map_default_noflush (struct page *page) +int set_direct_map_default_noflush (const void *addr) { -unsigned long addr = (unsigned long )page_address (page); - -if (addr < vm_map_base) +if ((unsigned long )addr < vm_map_base)return 0 ; -return __set_memory (addr,1 ,PAGE_KERNEL ,__pgprot (0 )); +return __set_memory ((unsigned long )addr,1 ,PAGE_KERNEL ,__pgprot (0 )); } -int set_direct_map_invalid_noflush (struct page *page) +int set_direct_map_invalid_noflush (const void *addr) { -unsigned long addr = (unsigned long )page_address (page); - -if (addr < vm_map_base) +if ((unsigned long )addr < vm_map_base)return 0 ; -return __set_memory (addr,1 ,__pgprot (0 ),__pgprot (_PAGE_PRESENT | _PAGE_VALID)); +return __set_memory ((unsigned long )addr,1 ,__pgprot (0 ), +__pgprot (_PAGE_PRESENT | _PAGE_VALID)); } -int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid) +int set_direct_map_valid_noflush (const void *addr,unsigned long numpages, +bool valid) { -unsigned long addr = (unsigned long )page_address (page);pgprot_t set, clear; -if (addr < vm_map_base) +if ((unsigned long )addr < vm_map_base)return 0 ;if (valid) {
@@ -234,5 +231,5 @@ int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool valid) clear =__pgprot (_PAGE_PRESENT | _PAGE_VALID); } -return __set_memory (addr,1 , set, clear); +return __set_memory ((unsigned long )addr,1 , set, clear); }
arch/riscv/include/asm/set_memory.h +4 -3
@@ -40,9 +40,10 @@ static inline int set_kernel_memory(char *startp, char *endp, }#endif -int set_direct_map_invalid_noflush (struct page *page); -int set_direct_map_default_noflush (struct page *page); -int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid); +int set_direct_map_invalid_noflush (const void *addr); +int set_direct_map_default_noflush (const void *addr); +int set_direct_map_valid_noflush (const void *addr,unsigned long numpages, +bool valid);bool kernel_page_present (struct page *page);#endif /* __ASSEMBLER__ */
arch/riscv/mm/pageattr.c +9 -8
@@ -374,19 +374,20 @@ int set_memory_nx(unsigned long addr, int numpages)return __set_memory (addr, numpages,__pgprot (0 ),__pgprot (_PAGE_EXEC)); } -int set_direct_map_invalid_noflush (struct page *page) +int set_direct_map_invalid_noflush (const void *addr) { -return __set_memory ((unsigned long )page_address (page),1 , -__pgprot (0 ),__pgprot (_PAGE_PRESENT)); +return __set_memory ((unsigned long )addr,1 ,__pgprot (0 ), +__pgprot (_PAGE_PRESENT)); } -int set_direct_map_default_noflush (struct page *page) +int set_direct_map_default_noflush (const void *addr) { -return __set_memory ((unsigned long )page_address (page),1 , -PAGE_KERNEL ,__pgprot (_PAGE_EXEC)); +return __set_memory ((unsigned long )addr,1 ,PAGE_KERNEL , +__pgprot (_PAGE_EXEC)); } -int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid) +int set_direct_map_valid_noflush (const void *addr,unsigned long numpages, +bool valid) {pgprot_t set, clear;
@@ -398,7 +399,7 @@ int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool valid) clear =__pgprot (_PAGE_PRESENT); } -return __set_memory ((unsigned long )page_address (page), nr, set, clear); +return __set_memory ((unsigned long )addr, numpages, set, clear); }#ifdef CONFIG_DEBUG_PAGEALLOC
arch/s390/include/asm/set_memory.h +4 -3
@@ -60,9 +60,10 @@ __SET_MEMORY_FUNC(set_memory_rox, SET_MEMORY_RO | SET_MEMORY_X) __SET_MEMORY_FUNC(set_memory_rwnx, SET_MEMORY_RW | SET_MEMORY_NX) __SET_MEMORY_FUNC(set_memory_4k, SET_MEMORY_4K) -int set_direct_map_invalid_noflush (struct page *page); -int set_direct_map_default_noflush (struct page *page); -int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid); +int set_direct_map_invalid_noflush (const void *addr); +int set_direct_map_default_noflush (const void *addr); +int set_direct_map_valid_noflush (const void *addr,unsigned long numpages, +bool valid);bool kernel_page_present (struct page *page);#endif
arch/s390/mm/pageattr.c +7 -6
@@ -383,17 +383,18 @@ int __set_memory(unsigned long addr, unsigned long numpages, unsigned long flagsreturn rc; } -int set_direct_map_invalid_noflush (struct page *page) +int set_direct_map_invalid_noflush (const void *addr) { -return __set_memory ((unsigned long )page_to_virt (page),1 ,SET_MEMORY_INV ); +return __set_memory ((unsigned long )addr,1 ,SET_MEMORY_INV ); } -int set_direct_map_default_noflush (struct page *page) +int set_direct_map_default_noflush (const void *addr) { -return __set_memory ((unsigned long )page_to_virt (page),1 ,SET_MEMORY_DEF ); +return __set_memory ((unsigned long )addr,1 ,SET_MEMORY_DEF ); } -int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid) +int set_direct_map_valid_noflush (const void *addr,unsigned long numpages, +bool valid) {unsigned long flags;
@@ -402,7 +403,7 @@ int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool valid)else flags =SET_MEMORY_INV ; -return __set_memory ((unsigned long )page_to_virt (page), nr, flags); +return __set_memory ((unsigned long )addr, numpages, flags); }bool kernel_page_present (struct page *page)
arch/x86/include/asm/set_memory.h +4 -3
@@ -86,9 +86,10 @@ int set_pages_wb(struct page *page, int numpages);int set_pages_ro (struct page *page,int numpages);int set_pages_rw (struct page *page,int numpages); -int set_direct_map_invalid_noflush (struct page *page); -int set_direct_map_default_noflush (struct page *page); -int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid); +int set_direct_map_invalid_noflush (const void *addr); +int set_direct_map_default_noflush (const void *addr); +int set_direct_map_valid_noflush (const void *addr,unsigned long numpages, +bool valid);bool kernel_page_present (struct page *page);extern int kernel_set_to_readonly;
arch/x86/mm/pat/set_memory.c +14 -13
@@ -2587,9 +2587,9 @@ int set_pages_rw(struct page *page, int numpages)return set_memory_rw (addr, numpages); } -static int __set_pages_p (struct page *page,int numpages) +static int __set_pages_p (const void *addr,int numpages) { -unsigned long tempaddr = (unsigned long )page_address (page); +unsigned long tempaddr = (unsigned long )addr;struct cpa_data cpa = { .vaddr = &tempaddr, .pgd =NULL , .numpages = numpages,
@@ -2606,9 +2606,9 @@ static int __set_pages_p(struct page *page, int numpages)return __change_page_attr_set_clr (&cpa,1 ); } -static int __set_pages_np (struct page *page,int numpages) +static int __set_pages_np (const void *addr,int numpages) { -unsigned long tempaddr = (unsigned long )page_address (page); +unsigned long tempaddr = (unsigned long )addr;struct cpa_data cpa = { .vaddr = &tempaddr, .pgd =NULL , .numpages = numpages,
@@ -2625,22 +2625,23 @@ static int __set_pages_np(struct page *page, int numpages)return __change_page_attr_set_clr (&cpa,1 ); } -int set_direct_map_invalid_noflush (struct page *page) +int set_direct_map_invalid_noflush (const void *addr) { -return __set_pages_np (page,1 ); +return __set_pages_np (addr,1 ); } -int set_direct_map_default_noflush (struct page *page) +int set_direct_map_default_noflush (const void *addr) { -return __set_pages_p (page,1 ); +return __set_pages_p (addr,1 ); } -int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid) +int set_direct_map_valid_noflush (const void *addr,unsigned long numpages, +bool valid) {if (valid) -return __set_pages_p (page, nr); +return __set_pages_p (addr, numpages); -return __set_pages_np (page, nr); +return __set_pages_np (addr, numpages); }#ifdef CONFIG_DEBUG_PAGEALLOC
@@ -2659,9 +2660,9 @@ void __kernel_map_pages(struct page *page, int numpages, int enable)* and hence no memory allocations during large page split. */ if (enable) -__set_pages_p (page, numpages); +__set_pages_p (page_address (page), numpages);else -__set_pages_np (page, numpages); +__set_pages_np (page_address (page), numpages);/* * We should perform an IPI and flush all tlbs,
include/linux/set_memory.h +5 -4
@@ -25,17 +25,18 @@ static inline int set_memory_rox(unsigned long addr, int numpages)#endif #ifndef CONFIG_ARCH_HAS_SET_DIRECT_MAP -static inline int set_direct_map_invalid_noflush (struct page *page) +static inline int set_direct_map_invalid_noflush (const void *addr) {return 0 ; } -static inline int set_direct_map_default_noflush (struct page *page) +static inline int set_direct_map_default_noflush (const void *addr) {return 0 ; } -static inline int set_direct_map_valid_noflush (struct page *page, -unsigned nr,bool valid) +static inline int set_direct_map_valid_noflush (const void *addr, +unsigned long numpages, +bool valid) {return 0 ; }
kernel/power/snapshot.c +2 -2
@@ -88,7 +88,7 @@ static inline int hibernate_restore_unprotect_page(void *page_address) {return 0static inline void hibernate_map_page (struct page *page) {if (IS_ENABLED (CONFIG_ARCH_HAS_SET_DIRECT_MAP )) { -int ret =set_direct_map_default_noflush (page); +int ret =set_direct_map_default_noflush (page_address (page));if (ret)pr_warn_once ("Failed to remap page\n" );
@@ -101,7 +101,7 @@ static inline void hibernate_unmap_page(struct page *page) {if (IS_ENABLED (CONFIG_ARCH_HAS_SET_DIRECT_MAP )) {unsigned long addr = (unsigned long )page_address (page); -int ret =set_direct_map_invalid_noflush (page); +int ret =set_direct_map_invalid_noflush (page_address (page));if (ret)pr_warn_once ("Failed to remap page\n" );
mm/execmem.c +4 -2
@@ -119,7 +119,8 @@ static int execmem_set_direct_map_valid(struct vm_struct *vm, bool valid)int err =0 ;for (int i =0 ; i < vm->nr_pages; i += nr) { - err =set_direct_map_valid_noflush (vm->pages[i], nr, valid); + err =set_direct_map_valid_noflush (page_address (vm->pages[i]), + nr, valid);if (err) goto err_restore; updated += nr;
@@ -129,7 +130,8 @@ static int execmem_set_direct_map_valid(struct vm_struct *vm, bool valid) err_restore:for (int i =0 ; i < updated; i += nr) -set_direct_map_valid_noflush (vm->pages[i], nr, !valid); +set_direct_map_valid_noflush (page_address (vm->pages[i]), nr, + !valid);return err; }
mm/secretmem.c +3 -3
@@ -72,7 +72,7 @@ static vm_fault_t secretmem_fault(struct vm_fault *vmf) goto out; } - err =set_direct_map_invalid_noflush (folio_page (folio,0 )); + err =set_direct_map_invalid_noflush (folio_address (folio));if (err) {folio_put (folio); ret =vmf_error (err);
@@ -87,7 +87,7 @@ static vm_fault_t secretmem_fault(struct vm_fault *vmf)* already happened when we marked the page invalid * which guarantees that this call won't fail */ -set_direct_map_default_noflush (folio_page (folio,0 )); +set_direct_map_default_noflush (folio_address (folio));folio_put (folio);if (err == -EEXIST ) goto retry;
@@ -151,7 +151,7 @@ static int secretmem_migrate_folio(struct address_space *mapping,static void secretmem_free_folio (struct folio *folio) { -set_direct_map_default_noflush (folio_page (folio,0 )); +set_direct_map_default_noflush (folio_address (folio));folio_zero_segment (folio,0 ,folio_size (folio)); }
mm/vmalloc.c +7 -4
@@ -3342,14 +3342,17 @@ struct vm_struct *remove_vm_area(const void *addr) }static inline void set_area_direct_map (const struct vm_struct *area, -int (*set_direct_map)(struct page *page)) +int (*set_direct_map)(const void *addr)) {int i;/* HUGE_VMALLOC passes small pages to set_direct_map */ -for (i =0 ; i < area->nr_pages; i++) -if (page_address (area->pages[i])) -set_direct_map (area->pages[i]); +for (i =0 ; i < area->nr_pages; i++) { +const void *addr =page_address (area->pages[i]); + +if (addr) +set_direct_map (addr); + } }/*
Signature · 1 line
2.50.1
Lorenzo Stoakes
maintainerRe: [PATCH v12 01/16] set_memory: set_direct_map_* to take address
Quoted 6 lines · Nikita
From: Nikita Kalyazin <[email protected]>
Let's convert set_direct_map_*() to take an address instead of a page to prepare for adding helpers that operate on folios; it will be more efficient to convert from a folio directly to an address without going through a page first.
Hmm presumably these addresses have to be page-aligned? But now they might not be _in theory_?
Is it clear to callers that they should pass page-aligned pointers? Might we have an issue in future with somebody passing something unaligned?
Quoted 3 lines
Signed-off-by Nikita Kalyazin <[email protected]>
The logic looks good to me, just various stylistic issues and concerns about alignment for future callers.
Quoted 23 lines
--- arch/arm64/include/asm/set_memory.h | 7 ++++--- arch/arm64/mm/pageattr.c | 19 +++++++++-------- arch/loongarch/include/asm/set_memory.h | 7 ++++--- arch/loongarch/mm/pageattr.c | 25 ++++++++++------------- arch/riscv/include/asm/set_memory.h | 7 ++++--- arch/riscv/mm/pageattr.c | 17 ++++++++-------- arch/s390/include/asm/set_memory.h | 7 ++++--- arch/s390/mm/pageattr.c | 13 ++++++------ arch/x86/include/asm/set_memory.h | 7 ++++--- arch/x86/mm/pat/set_memory.c | 27 +++++++++++++------------ include/linux/set_memory.h | 9 +++++---- kernel/power/snapshot.c | 4 ++-- mm/execmem.c | 6 ++++-- mm/secretmem.c | 6 +++--- mm/vmalloc.c | 11 ++++++---- 15 files changed, 91 insertions(+), 81 deletions(-)arch/arm64/include/asm/set_memory.h +0
-0
arch/arm64/include/asm/set_memory.h bool can_set_direct_map(void); · line 17
Quoted 10 lines
arch/arm64/include/asm/set_memory.h +4
-3@@ -11,9 +11,10 @@ bool can_set_direct_map(void);int set_memory_valid (unsigned long addr,int numpages,int enable); -int set_direct_map_invalid_noflush (struct page *page); -int set_direct_map_default_noflush (struct page *page); -int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid); +int set_direct_map_invalid_noflush (const void *addr); +int set_direct_map_default_noflush (const void *addr); +int set_direct_map_valid_noflush (const void *addr,unsigned long numpages, +bool valid);
I mean it's nitty, but it's weird having these being called 'addr' when they're not giving you an address, they're giving you pointers to something.
E.g. if I have a 'struct vm_area_struct *vma' and I do *vma I get a VMA. If I do a *addr here I get a... hmm :)
This looks to be a long-standing issue with page_address() (and probably, necessarily, folio_address()). But still...
It causes you issues later, somewhat trivial ones but still, where you have something like:
unsigned long addr = (unsigned long)page_address(page);
And now have a void *addr...
Maybe 'page_start' or something like that makes it clear that these need to be page-aligned. Or plain 'ptr' works too? 'page_ptr'? Naming's hard :)
Would it be overly difficult to simply pass the addr as an unsigned long?
Also a const void * is iffy from a type safety point of view, because somebody might not have got the memo, adds some code to that hands a struct page * and there'd be no compiler issues. Obv. the same with unaligned stuff.
Using an unsigned long would avoid that (not the alignment issue though obviously, but could imply that with param name maybe).
Quoted 43 lines
arch/arm64/include/asm/set_memory.h +0
-0bool kernel_page_present (struct page *page);int set_memory_encrypted (unsigned long addr,int numpages);arch/arm64/mm/pageattr.c +6
-6@@ -245,7 +245,7 @@ int set_memory_valid(unsigned long addr, int numpages, int enable) __pgprot(PTE_VALID)); } -int set_direct_map_invalid_noflush (struct page *page) +int set_direct_map_invalid_noflush (const void *addr) {pgprot_t clear_mask =__pgprot (PTE_VALID );pgprot_t set_mask =__pgprot (0 );@@ -253,11 +253,11 @@ int set_direct_map_invalid_noflush(struct page *page)if (!can_set_direct_map ())return 0 ; -return update_range_prot ((unsigned long )page_address (page), -PAGE_SIZE , set_mask, clear_mask); +return update_range_prot ((unsigned long )addr,PAGE_SIZE , set_mask, + clear_mask); } -int set_direct_map_default_noflush (struct page *page) +int set_direct_map_default_noflush (const void *addr) {pgprot_t set_mask =__pgprot (PTE_VALID |PTE_WRITE );pgprot_t clear_mask =__pgprot (PTE_RDONLY );@@ -265,8 +265,8 @@ int set_direct_map_default_noflush(struct page *page)if (!can_set_direct_map ())return 0 ; -return update_range_prot ((unsigned long )page_address (page), -PAGE_SIZE , set_mask, clear_mask); +return update_range_prot ((unsigned long )addr,PAGE_SIZE , set_mask, + clear_mask); } static int __set_memory_enc_dec(unsigned long addr,
arch/arm64/mm/pageattr.c int realm_register_memory_enc_ops(void) · line 354
Quoted 8 lines
arch/arm64/mm/pageattr.c +2
-2@@ -349,14 +349,13 @@ int realm_register_memory_enc_ops(void)return arm64_mem_crypt_ops_register (&realm_crypt_ops); } -int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid) +int set_direct_map_valid_noflush (const void *addr,unsigned long numpages, +bool valid) { -unsigned long addr = (unsigned long )page_address (page);
NIT, but maybe less churny to keep this and make it (unsigned long)addr?
OTOH is more consistent with other stuff to do the way you have, so optional.
I kinda like spelling things out like this though.
arch/arm64/mm/pageattr.c int realm_register_memory_enc_ops(void) · line 358
Quoted 6 lines
arch/arm64/mm/pageattr.c +1
-2-if (!can_set_direct_map ())return 0 ; -return set_memory_valid (addr, nr, valid); +return set_memory_valid ((unsigned long )addr, numpages, valid);
Yeah I mean (unsigned long)addr looks really weird to me, nearly always 'addr' references an address value like unsigned long in mm so :)
Quoted 25 lines
arch/arm64/mm/pageattr.c +0
-0}#ifdef CONFIG_DEBUG_PAGEALLOC arch/loongarch/include/asm/set_memory.h +4
-3@@ -15,8 +15,9 @@ int set_memory_ro(unsigned long addr, int numpages);int set_memory_rw (unsigned long addr,int numpages);bool kernel_page_present (struct page *page); -int set_direct_map_default_noflush (struct page *page); -int set_direct_map_invalid_noflush (struct page *page); -int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid); +int set_direct_map_invalid_noflush (const void *addr); +int set_direct_map_default_noflush (const void *addr); +int set_direct_map_valid_noflush (const void *addr,unsigned long numpages, +bool valid);#endif /* _ASM_LOONGARCH_SET_MEMORY_H */ arch/loongarch/mm/pageattr.c +0
-0
arch/loongarch/mm/pageattr.c bool kernel_page_present(struct page *page) · line 203
Quoted 7 lines
arch/loongarch/mm/pageattr.c +1
-2@@ -198,32 +198,29 @@ bool kernel_page_present(struct page *page)return pte_present (ptep_get (pte)); } -int set_direct_map_default_noflush (struct page *page) +int set_direct_map_default_noflush (const void *addr) { -unsigned long addr = (unsigned long )page_address (page);
OK we definitely need to de-duplicate here, repeatedly doing (unsigned long)addr is a bit silly.
Also if you do, please const-ify the value.
Quoted 11 lines
arch/loongarch/mm/pageattr.c +3
-4- -if (addr < vm_map_base) +if ((unsigned long )addr < vm_map_base)return 0 ; -return __set_memory (addr,1 ,PAGE_KERNEL ,__pgprot (0 )); +return __set_memory ((unsigned long )addr,1 ,PAGE_KERNEL ,__pgprot (0 )); } -int set_direct_map_invalid_noflush (struct page *page) +int set_direct_map_invalid_noflush (const void *addr)
arch/loongarch/mm/pageattr.c bool kernel_page_present(struct page *page) · line 215
Quoted 10 lines
arch/loongarch/mm/pageattr.c +3
-4{ -unsigned long addr = (unsigned long )page_address (page); - -if (addr < vm_map_base) +if ((unsigned long )addr < vm_map_base)return 0 ; -return __set_memory (addr,1 ,__pgprot (0 ),__pgprot (_PAGE_PRESENT | _PAGE_VALID)); +return __set_memory ((unsigned long )addr,1 ,__pgprot (0 ), +__pgprot (_PAGE_PRESENT | _PAGE_VALID));
Same here... We could avoid most of the churn here altogether by saying ptr instead of addr and just changing the addr = bit :)
Quoted 15 lines
arch/loongarch/mm/pageattr.c +3
-3} -int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid) +int set_direct_map_valid_noflush (const void *addr,unsigned long numpages, +bool valid) { -unsigned long addr = (unsigned long )page_address (page);pgprot_t set, clear; -if (addr < vm_map_base) +if ((unsigned long )addr < vm_map_base)return 0 ;if (valid) {
arch/loongarch/mm/pageattr.c int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool valid) · line 234
Quoted 5 lines
arch/loongarch/mm/pageattr.c +1
-1@@ -234,5 +231,5 @@ int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool valid) clear =__pgprot (_PAGE_PRESENT | _PAGE_VALID); } -return __set_memory (addr,1 , set, clear); +return __set_memory ((unsigned long )addr,1 , set, clear);
Obv. same comments here and below, I will stop duplicating them, just assume they apply to the whole thing :)
Quoted 92 lines
arch/loongarch/mm/pageattr.c +0
-0}arch/riscv/include/asm/set_memory.h +4
-3@@ -40,9 +40,10 @@ static inline int set_kernel_memory(char *startp, char *endp, }#endif -int set_direct_map_invalid_noflush (struct page *page); -int set_direct_map_default_noflush (struct page *page); -int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid); +int set_direct_map_invalid_noflush (const void *addr); +int set_direct_map_default_noflush (const void *addr); +int set_direct_map_valid_noflush (const void *addr,unsigned long numpages, +bool valid);bool kernel_page_present (struct page *page);#endif /* __ASSEMBLER__ */ arch/riscv/mm/pageattr.c +9
-8@@ -374,19 +374,20 @@ int set_memory_nx(unsigned long addr, int numpages)return __set_memory (addr, numpages,__pgprot (0 ),__pgprot (_PAGE_EXEC)); } -int set_direct_map_invalid_noflush (struct page *page) +int set_direct_map_invalid_noflush (const void *addr) { -return __set_memory ((unsigned long )page_address (page),1 , -__pgprot (0 ),__pgprot (_PAGE_PRESENT)); +return __set_memory ((unsigned long )addr,1 ,__pgprot (0 ), +__pgprot (_PAGE_PRESENT)); } -int set_direct_map_default_noflush (struct page *page) +int set_direct_map_default_noflush (const void *addr) { -return __set_memory ((unsigned long )page_address (page),1 , -PAGE_KERNEL ,__pgprot (_PAGE_EXEC)); +return __set_memory ((unsigned long )addr,1 ,PAGE_KERNEL , +__pgprot (_PAGE_EXEC)); } -int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid) +int set_direct_map_valid_noflush (const void *addr,unsigned long numpages, +bool valid) {pgprot_t set, clear;@@ -398,7 +399,7 @@ int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool valid) clear =__pgprot (_PAGE_PRESENT); } -return __set_memory ((unsigned long )page_address (page), nr, set, clear); +return __set_memory ((unsigned long )addr, numpages, set, clear); }#ifdef CONFIG_DEBUG_PAGEALLOC arch/s390/include/asm/set_memory.h +4
-3@@ -60,9 +60,10 @@ __SET_MEMORY_FUNC(set_memory_rox, SET_MEMORY_RO | SET_MEMORY_X) __SET_MEMORY_FUNC(set_memory_rwnx, SET_MEMORY_RW | SET_MEMORY_NX) __SET_MEMORY_FUNC(set_memory_4k, SET_MEMORY_4K) -int set_direct_map_invalid_noflush (struct page *page); -int set_direct_map_default_noflush (struct page *page); -int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid); +int set_direct_map_invalid_noflush (const void *addr); +int set_direct_map_default_noflush (const void *addr); +int set_direct_map_valid_noflush (const void *addr,unsigned long numpages, +bool valid);bool kernel_page_present (struct page *page);#endif arch/s390/mm/pageattr.c +2
-2@@ -383,17 +383,18 @@ int __set_memory(unsigned long addr, unsigned long numpages, unsigned long flagsreturn rc; } -int set_direct_map_invalid_noflush (struct page *page) +int set_direct_map_invalid_noflush (const void *addr) { -return __set_memory ((unsigned long )page_to_virt (page),1 ,SET_MEMORY_INV ); +return __set_memory ((unsigned long )addr,1 ,SET_MEMORY_INV ); }
arch/s390/mm/pageattr.c int __set_memory(unsigned long addr, unsigned long numpages, unsigned long flags · line 396
Quoted 10 lines
arch/s390/mm/pageattr.c +3
-3-int set_direct_map_default_noflush (struct page *page) +int set_direct_map_default_noflush (const void *addr) { -return __set_memory ((unsigned long )page_to_virt (page),1 ,SET_MEMORY_DEF ); +return __set_memory ((unsigned long )addr,1 ,SET_MEMORY_DEF ); } -int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid) +int set_direct_map_valid_noflush(const void *addr, unsigned long numpages,
Why changing to numpages? I guess because it's not obvious what it's a count of...
This really makes me think we're kinda making this interface a bit worse, because before it had to be 'nr' pages, but now it's 'numpages of err something starting at addr which isn't an address but a pointer and might not be aligned'.
If you called this 'page_ptr' then nr would be implicit and could be kept the same I guess?
Quoted 48 lines
arch/s390/mm/pageattr.c +2
-1+ bool valid) {unsigned long flags;@@ -402,7 +403,7 @@ int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool valid)else flags =SET_MEMORY_INV ; -return __set_memory ((unsigned long )page_to_virt (page), nr, flags); +return __set_memory ((unsigned long )addr, numpages, flags); }bool kernel_page_present (struct page *page)arch/x86/include/asm/set_memory.h +4
-3@@ -86,9 +86,10 @@ int set_pages_wb(struct page *page, int numpages);int set_pages_ro (struct page *page,int numpages);int set_pages_rw (struct page *page,int numpages); -int set_direct_map_invalid_noflush (struct page *page); -int set_direct_map_default_noflush (struct page *page); -int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid); +int set_direct_map_invalid_noflush (const void *addr); +int set_direct_map_default_noflush (const void *addr); +int set_direct_map_valid_noflush (const void *addr,unsigned long numpages, +bool valid);bool kernel_page_present (struct page *page);extern int kernel_set_to_readonly;arch/x86/mm/pat/set_memory.c +2
-2@@ -2587,9 +2587,9 @@ int set_pages_rw(struct page *page, int numpages)return set_memory_rw (addr, numpages); } -static int __set_pages_p (struct page *page,int numpages) +static int __set_pages_p (const void *addr,int numpages) { -unsigned long tempaddr = (unsigned long )page_address (page); +unsigned long tempaddr = (unsigned long )addr;struct cpa_data cpa = { .vaddr = &tempaddr, .pgd =NULL , .numpages = numpages,
arch/x86/mm/pat/set_memory.c static int __set_pages_p(struct page *page, int numpages) · line 2611
Quoted 8 lines
arch/x86/mm/pat/set_memory.c +2
-2@@ -2606,9 +2606,9 @@ static int __set_pages_p(struct page *page, int numpages)return __change_page_attr_set_clr (&cpa,1 ); } -static int __set_pages_np (struct page *page,int numpages) +static int __set_pages_np (const void *addr,int numpages) { -unsigned long tempaddr = (unsigned long )page_address (page); +unsigned long tempaddr = (unsigned long )addr;
This is even more horrid, tempaddr and addr, but different types...
Also let's please const-ify these kinds of local vars.
Quoted 177 lines
arch/x86/mm/pat/set_memory.c +10
-9struct cpa_data cpa = { .vaddr = &tempaddr, .pgd =NULL , .numpages = numpages,@@ -2625,22 +2625,23 @@ static int __set_pages_np(struct page *page, int numpages)return __change_page_attr_set_clr (&cpa,1 ); } -int set_direct_map_invalid_noflush (struct page *page) +int set_direct_map_invalid_noflush (const void *addr) { -return __set_pages_np (page,1 ); +return __set_pages_np (addr,1 ); } -int set_direct_map_default_noflush (struct page *page) +int set_direct_map_default_noflush (const void *addr) { -return __set_pages_p (page,1 ); +return __set_pages_p (addr,1 ); } -int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid) +int set_direct_map_valid_noflush (const void *addr,unsigned long numpages, +bool valid) {if (valid) -return __set_pages_p (page, nr); +return __set_pages_p (addr, numpages); -return __set_pages_np (page, nr); +return __set_pages_np (addr, numpages); }#ifdef CONFIG_DEBUG_PAGEALLOC @@ -2659,9 +2660,9 @@ void __kernel_map_pages(struct page *page, int numpages, int enable)* and hence no memory allocations during large page split. */ if (enable) -__set_pages_p (page, numpages); +__set_pages_p (page_address (page), numpages);else -__set_pages_np (page, numpages); +__set_pages_np (page_address (page), numpages);/* * We should perform an IPI and flush all tlbs, include/linux/set_memory.h +5
-4@@ -25,17 +25,18 @@ static inline int set_memory_rox(unsigned long addr, int numpages)#endif #ifndef CONFIG_ARCH_HAS_SET_DIRECT_MAP -static inline int set_direct_map_invalid_noflush (struct page *page) +static inline int set_direct_map_invalid_noflush (const void *addr) {return 0 ; } -static inline int set_direct_map_default_noflush (struct page *page) +static inline int set_direct_map_default_noflush (const void *addr) {return 0 ; } -static inline int set_direct_map_valid_noflush (struct page *page, -unsigned nr,bool valid) +static inline int set_direct_map_valid_noflush (const void *addr, +unsigned long numpages, +bool valid) {return 0 ; }kernel/power/snapshot.c +2
-2@@ -88,7 +88,7 @@ static inline int hibernate_restore_unprotect_page(void *page_address) {return 0static inline void hibernate_map_page (struct page *page) {if (IS_ENABLED (CONFIG_ARCH_HAS_SET_DIRECT_MAP )) { -int ret =set_direct_map_default_noflush (page); +int ret =set_direct_map_default_noflush (page_address (page));if (ret)pr_warn_once ("Failed to remap page\n" );@@ -101,7 +101,7 @@ static inline void hibernate_unmap_page(struct page *page) {if (IS_ENABLED (CONFIG_ARCH_HAS_SET_DIRECT_MAP )) {unsigned long addr = (unsigned long )page_address (page); -int ret =set_direct_map_invalid_noflush (page); +int ret =set_direct_map_invalid_noflush (page_address (page));if (ret)pr_warn_once ("Failed to remap page\n" );mm/execmem.c +4
-2@@ -119,7 +119,8 @@ static int execmem_set_direct_map_valid(struct vm_struct *vm, bool valid)int err =0 ;for (int i =0 ; i < vm->nr_pages; i += nr) { - err =set_direct_map_valid_noflush (vm->pages[i], nr, valid); + err =set_direct_map_valid_noflush (page_address (vm->pages[i]), + nr, valid);if (err) goto err_restore; updated += nr;@@ -129,7 +130,8 @@ static int execmem_set_direct_map_valid(struct vm_struct *vm, bool valid) err_restore:for (int i =0 ; i < updated; i += nr) -set_direct_map_valid_noflush (vm->pages[i], nr, !valid); +set_direct_map_valid_noflush (page_address (vm->pages[i]), nr, + !valid);return err; }mm/secretmem.c +3
-3@@ -72,7 +72,7 @@ static vm_fault_t secretmem_fault(struct vm_fault *vmf) goto out; } - err =set_direct_map_invalid_noflush (folio_page (folio,0 )); + err =set_direct_map_invalid_noflush (folio_address (folio));if (err) {folio_put (folio); ret =vmf_error (err);@@ -87,7 +87,7 @@ static vm_fault_t secretmem_fault(struct vm_fault *vmf)* already happened when we marked the page invalid * which guarantees that this call won't fail */ -set_direct_map_default_noflush (folio_page (folio,0 )); +set_direct_map_default_noflush (folio_address (folio));folio_put (folio);if (err == -EEXIST ) goto retry;@@ -151,7 +151,7 @@ static int secretmem_migrate_folio(struct address_space *mapping,static void secretmem_free_folio (struct folio *folio) { -set_direct_map_default_noflush (folio_page (folio,0 )); +set_direct_map_default_noflush (folio_address (folio));folio_zero_segment (folio,0 ,folio_size (folio)); }mm/vmalloc.c +7
-5@@ -3342,14 +3342,17 @@ struct vm_struct *remove_vm_area(const void *addr) }static inline void set_area_direct_map (const struct vm_struct *area, -int (*set_direct_map)(struct page *page)) +int (*set_direct_map)(const void *addr)) {int i;/* HUGE_VMALLOC passes small pages to set_direct_map */ -for (i =0 ; i < area->nr_pages; i++) -if (page_address (area->pages[i])) -set_direct_map (area->pages[i]); +for (i =0 ; i < area->nr_pages; i++) { +const void *addr =page_address (area->pages[i]); + +if (addr) +set_direct_map (addr); + } }/* --
Quoted 2 lines
2.50.1
Cheers, Lorenzo
Re: [PATCH v12 01/16] set_memory: set_direct_map_* to take address
Quoted 7 lines · Lorenzo Stoakes
Quoted 6 lines · Nikita
From: Nikita Kalyazin <[email protected]>
Let's convert set_direct_map_*() to take an address instead of a page to prepare for adding helpers that operate on folios; it will be more efficient to convert from a folio directly to an address without going through a page first.
Why is this more efficient? Isn't it a purely compile-time conversion?
Indeed in the current implementation folio_address() is page_address(&folio->page) so it still goes through a page anyway, no?
I might be missing context here about how this will look in the memdesc future.
David Hildenbrand (Arm)
maintainerRe: [PATCH v12 01/16] set_memory: set_direct_map_* to take address
Quoted 9 lines · Brendan Jackman
Quoted 7 lines · Lorenzo Stoakes
Quoted 6 lines · Nikita
From: Nikita Kalyazin <[email protected]>
Let's convert set_direct_map_*() to take an address instead of a page to prepare for adding helpers that operate on folios; it will be more efficient to convert from a folio directly to an address without going through a page first.
Quoted 7 lines
Why is this more efficient? Isn't it a purely compile-time conversion?
Indeed in the current implementation
folio_address()ispage_address(&folio->page)so it still goes through a page anyway, no?I might be missing context here about how this will look in the memdesc future.
Good question. page_address() is really only complicated for highmem. For non-highmem stuff it's simply derived from the page pfn.
I suspect going page -> pfn will remain as efficient as it currently is (with vmemmap). For odd cases (SPARSEMEM without VMEMMAP) going through folio->pfn [1] might be slighty faster.
[1] https://kernelnewbies.org/MatthewWilcox/Memdescs/Path
Signature · 3 lines
Cheers, David
Re: [PATCH v12 01/16] set_memory: set_direct_map_* to take address
Quoted 18 lines · David Hildenbrand (Arm)
Quoted 9 lines · Brendan Jackman
Quoted 7 lines · Lorenzo Stoakes
Quoted 6 lines · Nikita
From: Nikita Kalyazin <[email protected]>
Let's convert set_direct_map_*() to take an address instead of a page to prepare for adding helpers that operate on folios; it will be more efficient to convert from a folio directly to an address without going through a page first.
Quoted 7 lines
Why is this more efficient? Isn't it a purely compile-time conversion?
Indeed in the current implementation
folio_address()ispage_address(&folio->page)so it still goes through a page anyway, no?I might be missing context here about how this will look in the memdesc future.
Quoted 8 lines
Good question.
page_address()is really only complicated for highmem. For non-highmem stuff it's simply derived from the page pfn.I suspect going page -> pfn will remain as efficient as it currently is (with vmemmap). For odd cases (SPARSEMEM without VMEMMAP) going through
folio->pfn[1] might be slighty faster.
Ah sorry I raced with you and replied to myself:
https://lore.kernel.org/all/[email protected]/
The above makes me realise that if we really do want to avoid needing a struct page here, the "right" thing would be to make this API accept a PFN.
The problem would be that our favourite programming language we use doesn't know the difference between unsigned long addr and unsigned long pfn... but I thiiiiink its unlikely those bugs will be latent, I've made that mistake a few times and my computer usually let me know about it.
Still, I'm not really convinced we need to change anything here yet (modulo fuzziness about memdesc etc).
Re: [PATCH v12 01/16] set_memory: set_direct_map_* to take address
In reply to Brendan JackmanQuoted 9 lines · Brendan Jackman
Quoted 7 lines · Lorenzo Stoakes
Quoted 6 lines · Nikita
From: Nikita Kalyazin <[email protected]>
Let's convert set_direct_map_*() to take an address instead of a page to prepare for adding helpers that operate on folios; it will be more efficient to convert from a folio directly to an address without going through a page first.
Quoted 7 lines
Why is this more efficient? Isn't it a purely compile-time conversion?
Indeed in the current implementation
folio_address()ispage_address(&folio->page)so it still goes through a page anyway, no?I might be missing context here about how this will look in the memdesc future.
Sorry I failed to do the history search here, this was suggested by Willy during the review here:
https://lore.kernel.org/kvm/[email protected]/
Quoted 4 lines · Matthew Wilcox
The implementation isn't the greatest. None of the implementations of
set_direct_map_valid_noflush()actually do anything with the struct page; they all callpage_address()orpage_to_virt()(fundamentally the same thing).
But IMO the struct page isn't there to carry runtime information, it's a type hint that this API operates on physical pages. Yes this is already implied by the name but it also gives us a certain amount of safety since it avoids unaligned or non-physmap addresses at compile time.
Quoted 1 line
So converting
folio->page->addressis a bit inefficient.
... but yeah now I see this comes from Willy I definitely suspect I'm missing memdesc insight here.
Quoted 3 lines
It feels like we should change
set_direct_map_valid_noflush()to take a const void * and pass eitherpage_address()orfolio_address(), depending whether the caller has a page or a folio. What do you think?
Mike Rapoport
maintainerRe: [PATCH v12 01/16] set_memory: set_direct_map_* to take address
Quoted 29 lines · Brendan Jackman
Quoted 9 lines · Brendan Jackman
Quoted 7 lines · Lorenzo Stoakes
Quoted 6 lines · Nikita
From: Nikita Kalyazin <[email protected]>
Let's convert set_direct_map_*() to take an address instead of a page to prepare for adding helpers that operate on folios; it will be more efficient to convert from a folio directly to an address without going through a page first.
Quoted 7 lines
Why is this more efficient? Isn't it a purely compile-time conversion?
Indeed in the current implementation
folio_address()ispage_address(&folio->page)so it still goes through a page anyway, no?I might be missing context here about how this will look in the memdesc future.
Sorry I failed to do the history search here, this was suggested by Willy during the review here:
https://lore.kernel.org/kvm/[email protected]/
Quoted 4 lines · Matthew Wilcox
The implementation isn't the greatest. None of the implementations of
set_direct_map_valid_noflush()actually do anything with the struct page; they all callpage_address()orpage_to_virt()(fundamentally the same thing).
Quoted 4 lines
But IMO the struct page isn't there to carry runtime information, it's a type hint that this API operates on physical pages. Yes this is already implied by the name but it also gives us a certain amount of safety since it avoids unaligned or non-physmap addresses at compile time.
I think it's more historical thingy, the initial user was vmalloc::set_area_direct_map() that passed each page from vm->pages[] to set_direct_map functions.
And yes, having a page there let's the implementation to go directly from the page to it's address in the direct map, even for highmem ...
Quoted 8 lines
Quoted 1 line
So converting
folio->page->addressis a bit inefficient.... but yeah now I see this comes from Willy I definitely suspect I'm missing memdesc insight here.
Quoted 3 lines
It feels like we should change
set_direct_map_valid_noflush()to take a const void * and pass eitherpage_address()orfolio_address(), depending whether the caller has a page or a folio. What do you think?
... while with void * it's not necessarily one-to-one because void * can be a vmalloc address.
Signature · 2 lines
Sincerely yours, Mike.
PATCH v12 2/16 set_memory: add folio_{zap,restore}_direct_map helpers
In reply to Kalyazin, NikitaFrom: Nikita Kalyazin <[email protected]>
Let's provide folio_{zap,restore}_direct_map helpers as preparation for supporting removal of the direct map for guest_memfd folios. In folio_zap_direct_map(), flush TLB to make sure the data is not accessible. On some architectures, there may be a double TLB flush issued because set_direct_map_valid_noflush already performs a flush internally.
The new helpers need to be accessible to KVM on architectures that support guest_memfd (x86 and arm64).
Direct map removal gives guest_memfd the same protection that memfd_secret does, such as hardening against Spectre-like attacks through in-kernel gadgets.
--- include/linux/set_memory.h | 13 +++++++++++ mm/memory.c | 45 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+)
include/linux/set_memory.h +13 -0
@@ -41,6 +41,15 @@ static inline int set_direct_map_valid_noflush(const void *addr,return 0 ; } +static inline int folio_zap_direct_map (struct folio *folio) +{ +return 0 ; +} + +static inline void folio_restore_direct_map (struct folio *folio) +{ +} +static inline bool kernel_page_present (struct page *page) {return true;
@@ -57,6 +66,10 @@ static inline bool can_set_direct_map(void) }#define can_set_direct_map can_set_direct_map#endif + +int folio_zap_direct_map (struct folio *folio); +void folio_restore_direct_map (struct folio *folio); +#endif /* CONFIG_ARCH_HAS_SET_DIRECT_MAP */ #ifdef CONFIG_X86_64
mm/memory.c +45 -0
54 lines not on this page · raw message
Signature · 1 line
2.50.1
Re: [PATCH v12 02/16] set_memory: add folio_{zap,restore}_direct_map helpers
Quoted 86 lines · Nikita Kalyazin
From: Nikita Kalyazin <[email protected]>
Let's provide folio_{zap,restore}_direct_map helpers as preparation for supporting removal of the direct map for
guest_memfdfolios. Infolio_zap_direct_map(), flush TLB to make sure the data is not accessible. On some architectures, there may be a double TLB flush issued becauseset_direct_map_valid_noflushalready performs a flush internally.The new helpers need to be accessible to KVM on architectures that support
guest_memfd(x86 and arm64).Direct map removal gives
guest_memfdthe same protection thatmemfd_secretdoes, such as hardening against Spectre-like attacks through in-kernel gadgets.Signed-off-by Nikita Kalyazin <[email protected]>--- include/linux/set_memory.h | 13 +++++++++++ mm/memory.c | 45 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+)include/linux/set_memory.h +13
-0@@ -41,6 +41,15 @@ static inline int set_direct_map_valid_noflush(const void *addr,return 0 ; } +static inline int folio_zap_direct_map (struct folio *folio) +{ +return 0 ; +} + +static inline void folio_restore_direct_map (struct folio *folio) +{ +} +static inline bool kernel_page_present (struct page *page) {return true;@@ -57,6 +66,10 @@ static inline bool can_set_direct_map(void) }#define can_set_direct_map can_set_direct_map#endif + +int folio_zap_direct_map (struct folio *folio); +void folio_restore_direct_map (struct folio *folio); +#endif /* CONFIG_ARCH_HAS_SET_DIRECT_MAP */ #ifdef CONFIG_X86_64 mm/memory.c +17
-0@@ -78,6 +78,7 @@#include <linux/sched/sysctl.h> #include <linux/pgalloc.h> #include <linux/uaccess.h> +#include <linux/set_memory.h> #include <trace/events/kmem.h> @@ -7479,3 +7480,47 @@ void vma_pgtable_walk_end(struct vm_area_struct *vma)if (is_vm_hugetlb_page (vma))hugetlb_vma_unlock_read (vma); } + +#ifdef CONFIG_ARCH_HAS_SET_DIRECT_MAP +/** +* folio_zap_direct_map - remove a folio from the kernel direct map +* @folio: folio to remove from the direct map +* +* Removes the folio from the kernel direct map and flushes the TLB. This may +* require splitting huge pages in the direct map, which can fail due to memory +* allocation. So far, only order-0 folios are supported. +* +* Return: 0 on success, or a negative error code on failure. +*/ +int folio_zap_direct_map (struct folio *folio) +{ +const void *addr =folio_address (folio); +int ret;
mm/memory.c void vma_pgtable_walk_end(struct vm_area_struct *vma) · line 7508
Quoted 10 lines
mm/memory.c +10
-0+ +if (folio_test_large (folio)) +return -EINVAL ; + + ret =set_direct_map_valid_noflush (addr,folio_nr_pages (folio), false); +flush_tlb_kernel_range ((unsigned long )addr, + (unsigned long )addr +folio_size (folio)); + +return ret; +}
My local Sashiko run pointed out that this is broken for highmem.
There's no highmem for guest_memfd but there is for secretmem.
... but this isn't actually an issue with the patch, it's currently broken in Linus' master:
Su[ 30.071284] ------------[ cut here ]------------
ccessfully allocated and mapped 2097152000 bytes at 0x3a449000
Populating memor[ 30.074614] CPA: called for zero pte. vaddr = 0 cpa->vaddr = 0
y...
[ 30.078636] WARNING: arch/x86/mm/pat/set_memory.c:1840 at __cpa_process_fault+0x34d/0x360, CPU#5: allocate_secret/570 [ 30.084789] CPU: 5 UID: 0 PID: 570 Comm: allocate_secret Not tainted 7.1.0-14063-g4edcdefd4083-dirty #10 PREEMPTLAZY [ 30.090937] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.17.0-0-gb52ca86e094d-prebuilt.qemu.org 04/01/2014 [ 30.097543] EIP: __cpa_process_fault+0x34d/0x360 [ 30.100514] Code: ff ff 85 c0 0f 89 7d fe ff ff e9 3d fe ff ff 8b 03 8b 00 c7 04 24 c8 ff 64 c1 89 44 24 08 8b 45 e8 89 44 24 04 e8 53 7a 00 00 <0f> 0b c7 45 f0 f2 ff ff ff e9 fc fc ff ff 90 8d 74 26 00 55 25 00 [ 30.110829] EAX: 00000000 EBX: f64afe98 ECX: 00000000 EDX: 00000000 [ 30.114799] ESI: 00000000 EDI: f64afe98 EBP: f64afe04 ESP: f64afdcc [ 30.118785] DS: 007b ES: 007b FS: 00d8 GS: 0033 SS: 0068 EFLAGS: 00010246 [ 30.123020] CR0: 80050033 CR2: 46c48ffc CR3: 038c8000 CR4: 00000690 [ 30.127010] Call Trace: [ 30.129078] __change_page_attr_set_clr+0x5e7/0x870 [ 30.132275] ? console_unlock+0x99/0x130 [ 30.135069] ? irq_work_queue+0x36/0x70 [ 30.137853] ? page_address+0xd3/0xf0 [ 30.140421] set_direct_map_invalid_noflush+0x52/0x60 [ 30.143782] secretmem_fault+0x128/0x210 [ 30.146560] __do_fault+0x25/0x90 [ 30.149053] handle_mm_fault+0x6d1/0xcb0 [ 30.151759] exc_page_fault+0x135/0x3b0 [ 30.154487] ? doublefault_shim+0x150/0x150 [ 30.157416] handle_exception+0x130/0x130 [ 30.160137] EIP: 0x804d29f [ 30.162307] Code: 89 54 08 e1 89 54 08 e5 89 54 08 e9 89 54 08 ed c3 0f b6 44 24 08 89 7c 24 0c 69 c0 01 01 01 01 8b 7c 24 04 f7 c7 0f 00 00 00 <89> 44 0f fc 75 0e c1 e9 02 f3 ab 8b 44 24 04 8b 7c 24 0c c3 31 d2 [ 30.172936] EAX: 5a5a5a5a EBX: 00000000 ECX: 0c800000 EDX: 3a449000 [ 30.176927] ESI: 00000000 EDI: 3a449000 EBP: bfbbae18 ESP: bfbbadac [ 30.180897] DS: 007b ES: 007b FS: 0000 GS: 0033 SS: 007b EFLAGS: 00010246 [ 30.185161] ? doublefault_shim+0x150/0x150 [ 30.187979] ---[ end trace 0000000000000000 ]---
Bus error (core dumped) ./allocate_secret_i686 2000M
Fixing this directly in secretmem.c is kinda yucky but if we can just make folio_zap_direct_map() a NOP for highmem folios it's nice and easy so I propose to just fix it as a followup to this series.
Alternatively, we maybe should disable secretmem for highmem systems since it evidently doesn't have any users.
Mike Rapoport
maintainerRe: [PATCH v12 02/16] set_memory: add folio_{zap,restore}_direct_map helpers
Quoted 41 lines · Brendan Jackman
On Fri Apr 10, 2026 at 3:18 PM UTC, Nikita Kalyazin wrote:
My local Sashiko run pointed out that this is broken for highmem.
There's no highmem for
guest_memfdbut there is for secretmem.... but this isn't actually an issue with the patch, it's currently broken in Linus' master:
Su[ 30.071284] ------------[ cut here ]------------
ccessfully allocated and mapped 2097152000 bytes at0x3a449000
Populating memor[ 30.074614] CPA: called for zero pte. vaddr = 0cpa->vaddr= 0
y...[ 30.078636] WARNING: arch/x86/mm/pat/set_memory.c:1840 at __cpa_process_fault+0x34d/0x360, CPU#5: allocate_secret/570 [ 30.084789] CPU: 5 UID: 0 PID: 570 Comm: allocate_secret Not tainted 7.1.0-14063-g4edcdefd4083-dirty #10 PREEMPTLAZY [ 30.090937] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.17.0-0-gb52ca86e094d-prebuilt.qemu.org 04/01/2014 [ 30.097543] EIP: __cpa_process_fault+0x34d/0x360 [ 30.100514] Code: ff ff 85 c0 0f 89 7d fe ff ff e9 3d fe ff ff 8b 03 8b 00 c7 04 24 c8 ff 64 c1 89 44 24 08 8b 45 e8 89 44 24 04 e8 53 7a 00 00 <0f> 0b c7 45 f0 f2 ff ff ff e9 fc fc ff ff 90 8d 74 26 00 55 25 00 [ 30.110829] EAX: 00000000 EBX: f64afe98 ECX: 00000000 EDX: 00000000 [ 30.114799] ESI: 00000000 EDI: f64afe98 EBP: f64afe04 ESP: f64afdcc [ 30.118785] DS: 007b ES: 007b FS: 00d8 GS: 0033 SS: 0068 EFLAGS: 00010246 [ 30.123020] CR0: 80050033 CR2: 46c48ffc CR3: 038c8000 CR4: 00000690 [ 30.127010] Call Trace: [ 30.129078] __change_page_attr_set_clr+0x5e7/0x870 [ 30.132275] ? console_unlock+0x99/0x130 [ 30.135069] ? irq_work_queue+0x36/0x70 [ 30.137853] ? page_address+0xd3/0xf0 [ 30.140421] set_direct_map_invalid_noflush+0x52/0x60 [ 30.143782] secretmem_fault+0x128/0x210 [ 30.146560] __do_fault+0x25/0x90 [ 30.149053] handle_mm_fault+0x6d1/0xcb0 [ 30.151759] exc_page_fault+0x135/0x3b0 [ 30.154487] ? doublefault_shim+0x150/0x150 [ 30.157416] handle_exception+0x130/0x130 [ 30.160137] EIP: 0x804d29f [ 30.162307] Code: 89 54 08 e1 89 54 08 e5 89 54 08 e9 89 54 08 ed c3 0f b6 44 24 08 89 7c 24 0c 69 c0 01 01 01 01 8b 7c 24 04 f7 c7 0f 00 00 00 <89> 44 0f fc 75 0e c1 e9 02 f3 ab 8b 44 24 04 8b 7c 24 0c c3 31 d2 [ 30.172936] EAX: 5a5a5a5a EBX: 00000000 ECX: 0c800000 EDX: 3a449000 [ 30.176927] ESI: 00000000 EDI: 3a449000 EBP: bfbbae18 ESP: bfbbadac [ 30.180897] DS: 007b ES: 007b FS: 0000 GS: 0033 SS: 007b EFLAGS: 00010246 [ 30.185161] ? doublefault_shim+0x150/0x150 [ 30.187979] ---[ end trace 0000000000000000 ]---
Quoted 8 lines
Bus error (core dumped) ./allocate_secret_i686 2000M
Fixing this directly in
secretmem.cis kinda yucky but if we can just makefolio_zap_direct_map()a NOP for highmem folios it's nice and easy so I propose to just fix it as a followup to this series.Alternatively, we maybe should disable secretmem for highmem systems since it evidently doesn't have any users.
Yes, please :)
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH v12 02/16] set_memory: add folio_{zap,restore}_direct_map helpers
Quoted 1 line · Mike Rapoport
On Fri, Jul 03, 2026 at 10:19:10AM +0000, Brendan Jackman wrote:
...
Quoted 4 lines
Quoted 2 lines
Alternatively, we maybe should disable secretmem for highmem systems since it evidently doesn't have any users.
Yes, please :)
PATCH v12 3/16 mm/secretmem: make use of folio_{zap,restore}_direct_map
In reply to Kalyazin, NikitaFrom: Nikita Kalyazin <[email protected]>
Replace set_direct_map_*_noflush with newly available folio_zap_direct_map calls that take folio's address internally. A side effect is even if filemap_add_folio fails, the TLB is still flushed, which is not expected to be on the hot path.
--- mm/secretmem.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
mm/secretmem.c +2 -6
@@ -53,7 +53,6 @@ static vm_fault_t secretmem_fault(struct vm_fault *vmf)struct inode *inode =file_inode (vmf->vma->vm_file);pgoff_t offset = vmf->pgoff;gfp_t gfp = vmf->gfp_mask; -unsigned long addr;struct folio *folio;vm_fault_t ret;int err;
@@ -72,7 +71,7 @@ static vm_fault_t secretmem_fault(struct vm_fault *vmf) goto out; } - err =set_direct_map_invalid_noflush (folio_address (folio)); + err =folio_zap_direct_map (folio);if (err) {folio_put (folio); ret =vmf_error (err);
@@ -87,7 +86,7 @@ static vm_fault_t secretmem_fault(struct vm_fault *vmf)* already happened when we marked the page invalid * which guarantees that this call won't fail */ -set_direct_map_default_noflush (folio_address (folio)); +folio_restore_direct_map (folio);folio_put (folio);if (err == -EEXIST ) goto retry;
@@ -95,9 +94,6 @@ static vm_fault_t secretmem_fault(struct vm_fault *vmf) ret =vmf_error (err); goto out; } - - addr = (unsigned long )folio_address (folio); -flush_tlb_kernel_range (addr, addr +PAGE_SIZE ); } vmf->page =folio_file_page (folio, vmf->pgoff);
Signature · 1 line
2.50.1
PATCH v12 4/16 mm/gup: drop secretmem optimization from gup_fast_folio_allowed
In reply to Kalyazin, NikitaFrom: Patrick Roy <[email protected]>
This drops an 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.
To make sure the fast path for ZONE_DEVICE pages (like Device DAX and PCI P2PDMA) is still allowed, check for folio_is_zone_device() if mapping is NULL.
This is in preparation of the generalization of handling 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.
--- mm/gup.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-)
mm/gup.c +6 -11
@@ -2739,7 +2739,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;/*
@@ -2751,14 +2750,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;
@@ -2787,9 +2778,13 @@ static bool gup_fast_folio_allowed(struct folio *folio, unsigned int flags)* The mapping may have been truncated, in any case we cannot determine * if this mapping is safe - fall back to slow path to determine how to * proceed. +* +* ZONE_DEVICE folios (e.g. Device DAX, PCI P2PDMA) may legitimately +* have a NULL mapping. They are never secretmem/no-direct-map folios, +* so let them through. */ if (!mapping) -return false; +return folio_is_zone_device (folio);/* Anonymous folios pose no problem. */ mapping_flags = (unsigned long )mapping &FOLIO_MAPPING_FLAGS ;
@@ -2800,7 +2795,7 @@ 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)) +if (secretmem_mapping (mapping))return false;/* The only remaining allowed file system is shmem. */ return !reject_file_backed ||shmem_mapping (mapping);
Signature · 1 line
2.50.1
PATCH v12 5/16 mm/gup: drop local variable in gup_fast_folio_allowed
In reply to Kalyazin, NikitaFrom: Nikita Kalyazin <[email protected]>
Move the check for pinning closer to where the result is used. No functional changes.
--- mm/gup.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-)
mm/gup.c +12 -11
@@ -2737,18 +2737,9 @@ EXPORT_SYMBOL(get_user_pages_unlocked);*/ static bool gup_fast_folio_allowed (struct folio *folio,unsigned int flags) { -bool reject_file_backed = false;struct address_space *mapping;unsigned long mapping_flags; -/* -* If we aren't pinning then no problematic write can occur. A long term -* pin is the most egregious case so this is the one we disallow. -*/ -if ((flags & (FOLL_PIN |FOLL_LONGTERM |FOLL_WRITE )) == - (FOLL_PIN |FOLL_LONGTERM |FOLL_WRITE )) - reject_file_backed = true; -/* We hold a folio reference, so we can safely access folio fields. */ if (WARN_ON_ONCE (folio_test_slab (folio)))return false;
@@ -2797,8 +2788,18 @@ static bool gup_fast_folio_allowed(struct folio *folio, unsigned int flags)*/ if (secretmem_mapping (mapping))return false; -/* The only remaining allowed file system is shmem. */ -return !reject_file_backed ||shmem_mapping (mapping); + +/* +* If we aren't pinning then no problematic write can occur. A writable +* long term pin is the most egregious case, so this is the one we +* allow only for ... +*/ +if ((flags & (FOLL_PIN |FOLL_LONGTERM |FOLL_WRITE )) != + (FOLL_PIN |FOLL_LONGTERM |FOLL_WRITE )) +return true; + +/* ... hugetlb (which we allowed above already) and shared memory. */ +return shmem_mapping (mapping); }#ifdef CONFIG_ARCH_HAS_PTE_SPECIAL
Signature · 1 line
2.50.1
[email protected]
AI reviewRe: [PATCH v12 05/16] mm/gup: drop local variable in gup_fast_folio_allowed
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines
From: Nikita Kalyazin <[email protected]>
Move the check for pinning closer to where the result is used. No functional changes.
Signed-off-by Nikita Kalyazin <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
PATCH v12 6/16 mm: introduce AS_NO_DIRECT_MAP
In reply to Kalyazin, NikitaFrom: Patrick Roy <[email protected]>
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, and then make memfd_secret() use AS_NO_DIRECT_MAP on its address_space to drop its special vma_is_secretmem()/secretmem_mapping() checks.
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 ++++++++++++++++ include/linux/secretmem.h | 18 ------------------ lib/buildid.c | 8 ++++++-- mm/gup.c | 9 ++++----- mm/mlock.c | 2 +- mm/secretmem.c | 8 ++------ 6 files changed, 29 insertions(+), 32 deletions(-)
include/linux/pagemap.h +16 -0
@@ -211,6 +211,7 @@ enum mapping_flags {AS_KERNEL_FILE =10 ,/* mapping for a fake kernel file that shouldn't account usage to user cgroups */ AS_NO_DATA_INTEGRITY =11 ,/* no data integrity guarantees */ +AS_NO_DIRECT_MAP =12 ,/* 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,
@@ -356,6 +357,21 @@ static inline bool mapping_no_data_integrity(const struct address_space *mappingreturn test_bit (AS_NO_DATA_INTEGRITY , &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 (const struct address_space *mapping) +{ +return test_bit (AS_NO_DIRECT_MAP , &mapping->flags); +} + +static inline bool vma_has_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 (const struct address_space *mapping) {return mapping->gfp_mask;
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 +6 -2
@@ -47,6 +47,10 @@ static int freader_get_folio(struct freader *r, loff_t file_off)freader_put_folio (r); +/* reject folios without direct map entries (e.g. from memfd_secret() or guest_memfd()) */ +if (mapping_no_direct_map (r->file->f_mapping)) +return -EFAULT ; +/* only use page cache lookup - fail if not already cached */ r->folio =filemap_get_folio (r->file->f_mapping, file_off >>PAGE_SHIFT );
@@ -87,8 +91,8 @@ const void *freader_fetch(struct freader *r, loff_t file_off, size_t sz)return r->data + file_off; } -/* reject secretmem folios created with memfd_secret() */ -if (secretmem_mapping (r->file->f_mapping)) { +/* reject folios without direct map entries (e.g. from memfd_secret() or guest_memfd()) */ +if (mapping_no_direct_map (r->file->f_mapping)) { r->err = -EFAULT ;return NULL ; }
mm/gup.c +4 -5
@@ -11,7 +11,6 @@#include <linux/rmap.h> #include <linux/swap.h> #include <linux/swapops.h> -#include <linux/secretmem.h> #include <linux/sched/signal.h> #include <linux/rwsem.h>
@@ -1216,7 +1215,7 @@ static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)if ((gup_flags &FOLL_SPLIT_PMD ) &&is_vm_hugetlb_page (vma))return -EOPNOTSUPP ; -if (vma_is_secretmem (vma)) +if (vma_has_no_direct_map (vma))return -EFAULT ;if (write) {
@@ -2724,7 +2723,7 @@ EXPORT_SYMBOL(get_user_pages_unlocked);* This call assumes the caller has pinned the folio, that the lowest page table * level still points to this folio, and that interrupts have been disabled. * -* GUP-fast must reject all secretmem folios. +* GUP-fast must reject all folios without direct map entries (such as secretmem). * * Writing to pinned file-backed dirty tracked folios is inherently problematic * (see comment describing the writable_file_mapping_allowed() function). We
@@ -2744,7 +2743,7 @@ static bool gup_fast_folio_allowed(struct folio *folio, unsigned int flags)if (WARN_ON_ONCE (folio_test_slab (folio)))return false; -/* hugetlb neither requires dirty-tracking nor can be secretmem. */ +/* hugetlb neither requires dirty-tracking nor can be without direct map. */ if (folio_test_hugetlb (folio))return true;
@@ -2786,7 +2785,7 @@ 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 (secretmem_mapping (mapping)) +if (mapping_no_direct_map (mapping))return false;/*
mm/mlock.c +1 -1
@@ -474,7 +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) || (oldflags &VM_DROPPABLE )) +vma_is_dax (vma) ||vma_has_no_direct_map (vma) || (oldflags &VM_DROPPABLE ))/* don't set VM_LOCKED or VM_LOCKONFAULT and don't count */ goto out;
mm/secretmem.c +2 -6
@@ -129,11 +129,6 @@ static int secretmem_mmap_prepare(struct vm_area_desc *desc)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_prepare = secretmem_mmap_prepare,
@@ -151,7 +146,7 @@ static void secretmem_free_folio(struct folio *folio)folio_zero_segment (folio,0 ,folio_size (folio)); } -const struct address_space_operations secretmem_aops = { +static const struct address_space_operations secretmem_aops = { .dirty_folio = noop_dirty_folio, .free_folio = secretmem_free_folio, .migrate_folio = secretmem_migrate_folio,
@@ -200,6 +195,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.50.1
PATCH v12 7/16 KVM: guest_memfd: Add stub for kvm_arch_gmem_invalidate
In reply to Kalyazin, NikitaFrom: Patrick Roy <[email protected]>
Add a no-op stub for kvm_arch_gmem_invalidate if CONFIG_HAVE_KVM_ARCH_GMEM_INVALIDATE=n. This allows defining kvm_gmem_free_folio without ifdef-ery, which allows more cleanly using guest_memfd's free_folio callback for non-arch-invalidation related code.
--- include/linux/kvm_host.h | 2 ++ virt/kvm/guest_memfd.c | 4 ---- 2 files changed, 2 insertions(+), 4 deletions(-)
include/linux/kvm_host.h +2 -0
@@ -2587,6 +2587,8 @@ long kvm_gmem_populate(struct kvm *kvm, gfn_t gfn, void __user *src, long npages#ifdef CONFIG_HAVE_KVM_ARCH_GMEM_INVALIDATE void kvm_arch_gmem_invalidate (kvm_pfn_t start,kvm_pfn_t end); +#else +static inline void kvm_arch_gmem_invalidate (kvm_pfn_t start,kvm_pfn_t end) { }#endif #ifdef CONFIG_KVM_GENERIC_PRE_FAULT_MEMORY
virt/kvm/guest_memfd.c +0 -4
@@ -522,7 +522,6 @@ static int kvm_gmem_error_folio(struct address_space *mapping, struct folio *folreturn MF_DELAYED ; } -#ifdef CONFIG_HAVE_KVM_ARCH_GMEM_INVALIDATE static void kvm_gmem_free_folio (struct folio *folio) {struct page *page =folio_page (folio,0 );
@@ -531,15 +530,12 @@ static void kvm_gmem_free_folio(struct folio *folio)kvm_arch_gmem_invalidate (pfn, pfn + (1ul << order)); } -#endif 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, -#ifdef CONFIG_HAVE_KVM_ARCH_GMEM_INVALIDATE .free_folio = kvm_gmem_free_folio, -#endif }; static int kvm_gmem_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
Signature · 1 line
2.50.1
PATCH v12 8/16 KVM: x86: define kvm_arch_gmem_supports_no_direct_map()
In reply to Kalyazin, NikitaFrom: Patrick Roy <[email protected]>
x86 supports GUEST_MEMFD_FLAG_NO_DIRECT_MAP whenever direct map modifications are possible. Exclude TDX and SEV-SNP as they access pages via direct map in certain operations, such as population.
--- arch/x86/include/asm/kvm_host.h | 6 ++++++ arch/x86/kvm/x86.c | 7 +++++++ include/linux/kvm_host.h | 9 +++++++++ 3 files changed, 22 insertions(+)
arch/x86/include/asm/kvm_host.h +6 -0
@@ -28,6 +28,7 @@#include <linux/sched/vhost_task.h> #include <linux/call_once.h> #include <linux/atomic.h> +#include <linux/set_memory.h> #include <asm/apic.h> #include <asm/pvclock-abi.h>
@@ -2504,4 +2505,9 @@ static inline bool kvm_arch_has_irq_bypass(void) return enable_device_posted_irqs; } +#ifdef CONFIG_KVM_GUEST_MEMFD +bool kvm_arch_gmem_supports_no_direct_map (struct kvm *kvm); +#define kvm_arch_gmem_supports_no_direct_map kvm_arch_gmem_supports_no_direct_map +#endif /* CONFIG_KVM_GUEST_MEMFD */ +#endif /* _ASM_X86_KVM_HOST_H */
arch/x86/kvm/x86.c +7 -0
@@ -14079,6 +14079,13 @@ void kvm_arch_gmem_invalidate(kvm_pfn_t start, kvm_pfn_t end)kvm_x86_call (gmem_invalidate)(start, end); }#endif + +bool kvm_arch_gmem_supports_no_direct_map (struct kvm *kvm) +{ +return can_set_direct_map () && + kvm->arch.vm_type !=KVM_X86_TDX_VM && + kvm->arch.vm_type !=KVM_X86_SNP_VM ; +}#endif int kvm_spec_ctrl_test_value (u64 value)
include/linux/kvm_host.h +9 -0
@@ -742,6 +742,15 @@ static inline u64 kvm_gmem_get_supported_flags(struct kvm *kvm) }#endif +#ifdef CONFIG_KVM_GUEST_MEMFD +#ifndef kvm_arch_gmem_supports_no_direct_map +static inline bool kvm_arch_gmem_supports_no_direct_map (struct kvm *kvm) +{ +return false; +} +#endif +#endif /* CONFIG_KVM_GUEST_MEMFD */ +#ifndef kvm_arch_has_readonly_memstatic inline bool kvm_arch_has_readonly_mem (struct kvm *kvm) {
Signature · 1 line
2.50.1
PATCH v12 9/16 KVM: arm64: define kvm_arch_gmem_supports_no_direct_map()
In reply to Kalyazin, NikitaFrom: Patrick Roy <[email protected]>
Support for GUEST_MEMFD_FLAG_NO_DIRECT_MAP on arm64 depends on 1) direct map manipulations at 4k granularity being possible, and 2) FEAT_S2FWB.
1) is met whenever the direct map is set up at 4k granularity (e.g. not with huge/gigantic pages) at boottime, as due to ARM's
break-before-make semantics, breaking huge mappings into 4k mappings in the direct map is not possible (BBM would require temporary invalidation of the entire huge mapping, even if only a 4k subrange should be zapped, which will probably crash the kernel). However, the current default for rodata_full is true, which forces a 4k direct map.
2) is required to allow KVM to elide cache coherency operations when installing stage 2 page tables, which require the direct map to be entry for the newly mapped memory to be present (which it will not be, as guest_memfd would have removed direct map entries in kvm_gmem_get_pfn()).
--- arch/arm64/include/asm/kvm_host.h | 13 +++++++++++++ 1 file changed, 13 insertions(+)
arch/arm64/include/asm/kvm_host.h +13 -0
@@ -19,6 +19,7 @@#include <linux/maple_tree.h> #include <linux/percpu.h> #include <linux/psci.h> +#include <linux/set_memory.h> #include <asm/arch_gicv3.h> #include <asm/barrier.h> #include <asm/cpufeature.h>
@@ -1682,6 +1683,18 @@ static __always_inline enum fgt_group_id __fgt_reg_to_group_id(enum vcpu_sysreg \ p; \ }) +#ifdef CONFIG_KVM_GUEST_MEMFD +static inline bool kvm_arch_gmem_supports_no_direct_map (struct kvm *kvm) +{ +/* +* Without FWB, direct map access is needed in kvm_pgtable_stage2_map(), +* as it calls dcache_clean_inval_poc(). +*/ +return can_set_direct_map () &&cpus_have_final_cap (ARM64_HAS_STAGE2_FWB ); +} +#define kvm_arch_gmem_supports_no_direct_map kvm_arch_gmem_supports_no_direct_map +#endif /* CONFIG_KVM_GUEST_MEMFD */ +long kvm_get_cap_for_kvm_ioctl (unsigned int ioctl,long *ext);
Signature · 1 line
2.50.1
Marc Zyngier
maintainerRe: [PATCH v12 09/16] KVM: arm64: define kvm_arch_gmem_supports_no_direct_map()
Quoted 6 lines · Nikita
From: Patrick Roy <[email protected]>
Support for
GUEST_MEMFD_FLAG_NO_DIRECT_MAPon arm64 depends on 1) direct map manipulations at 4k granularity being possible, and 2)FEAT_S2FWB.
Quoted 7 lines
1) is met whenever the direct map is set up at 4k granularity (e.g. not with huge/gigantic pages) at boottime, as due to ARM's
break-before-make semantics, breaking huge mappings into 4k mappings in the direct map is not possible (BBM would require temporary invalidation of the entire huge mapping, even if only a 4k subrange should be zapped, which will probably crash the kernel). However, the current default forrodata_fullis true, which forces a 4k direct map.
Where is this 4kB requirement enforced? Or is it that you means "PAGE_SIZE"?
Quoted 28 lines
2) is required to allow KVM to elide cache coherency operations when installing stage 2 page tables, which require the direct map to be entry for the newly mapped memory to be present (which it will not be, as
guest_memfdwould have removed direct map entries inkvm_gmem_get_pfn()).Cc Will Deacon <[email protected]>Signed-off-by Patrick Roy <[email protected]>Reviewed-by David Hildenbrand (Arm) <[email protected]>Signed-off-by Nikita Kalyazin <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 13 +++++++++++++ 1 file changed, 13 insertions(+)arch/arm64/include/asm/kvm_host.h +1
-0@@ -19,6 +19,7 @@#include <linux/maple_tree.h> #include <linux/percpu.h> #include <linux/psci.h> +#include <linux/set_memory.h> #include <asm/arch_gicv3.h> #include <asm/barrier.h> #include <asm/cpufeature.h>
arch/arm64/include/asm/kvm_host.h static __always_inline enum fgt_group_id __fgt_reg_to_group_id(enum vcpu_sysreg · line 1686
Quoted 4 lines
arch/arm64/include/asm/kvm_host.h +1
-0@@ -1682,6 +1683,18 @@ static __always_inline enum fgt_group_id __fgt_reg_to_group_id(enum vcpu_sysreg \ p; \ }) +#ifdef CONFIG_KVM_GUEST_MEMFD
KVM/arm64 has:
select KVM_GUEST_MEMFD
arch/arm64/include/asm/kvm_host.h static __always_inline enum fgt_group_id __fgt_reg_to_group_id(enum vcpu_sysreg · line 1696
Quoted 10 lines
arch/arm64/include/asm/kvm_host.h +10
-0+static inline bool kvm_arch_gmem_supports_no_direct_map (struct kvm *kvm) +{ +/* +* Without FWB, direct map access is needed in kvm_pgtable_stage2_map(), +* as it calls dcache_clean_inval_poc(). +*/ +return can_set_direct_map () &&cpus_have_final_cap (ARM64_HAS_STAGE2_FWB ); +} +#define kvm_arch_gmem_supports_no_direct_map kvm_arch_gmem_supports_no_direct_map +#endif /* CONFIG_KVM_GUEST_MEMFD */
Why is it an inline function? Given that it takes a kvm parameter, I fully expect that you'll have to evaluate this at some point.
But since struct kvm cannot be dereferenced in asm/kvm_host.h, it will have to move. How about doing it right now?
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Re: [PATCH v12 09/16] KVM: arm64: define kvm_arch_gmem_supports_no_direct_map()
Quoted 16 lines · Marc Zyngier
Quoted 6 lines · Nikita
From: Patrick Roy <[email protected]>
Support for
GUEST_MEMFD_FLAG_NO_DIRECT_MAPon arm64 depends on 1) direct map manipulations at 4k granularity being possible, and 2)FEAT_S2FWB.Quoted 7 lines
1) is met whenever the direct map is set up at 4k granularity (e.g. not with huge/gigantic pages) at boottime, as due to ARM's
break-before-make semantics, breaking huge mappings into 4k mappings in the direct map is not possible (BBM would require temporary invalidation of the entire huge mapping, even if only a 4k subrange should be zapped, which will probably crash the kernel). However, the current default forrodata_fullis true, which forces a 4k direct map.
Quoted 2 lines
Where is this 4kB requirement enforced? Or is it that you means "
PAGE_SIZE"?
Yeah I believe this means PAGE_SIZE and that it's effectively enforced by checking can_set_direct_map() in kvm_arch_gmem_supports_no_direct_map().
Re: [PATCH v12 09/16] KVM: arm64: define kvm_arch_gmem_supports_no_direct_map()
Apologies for the delayed response, and thank you for the review.
Quoted 10 lines · Brendan Jackman
Quoted 5 lines · Marc Zyngier
Support for
GUEST_MEMFD_FLAG_NO_DIRECT_MAPon arm64 depends on 1) direct map manipulations at 4k granularity being possible, and 2)FEAT_S2FWB.Where is this 4kB requirement enforced? Or is it that you means "
PAGE_SIZE"?Yeah I believe this means
PAGE_SIZEand that it's effectively enforced by checkingcan_set_direct_map()inkvm_arch_gmem_supports_no_direct_map().
Yes, this should say PAGE_SIZE rather than 4k.
If I understand the arm64 MM code correctly, can_set_direct_map() is the capability gate, but it does not require the linear map to be built with page-sized PTEs from boot. Without BBML2_NOABORT, force_pte_mapping() builds the linear map with PAGE_SIZE PTEs. With BBML2_NOABORT, update_range_prot() can use split_kernel_leaf_mapping() to safely split larger mappings on demand before changing a PAGE_SIZE range.
I'll update the v13 commit message to describe this.
Quoted 3 lines · Marc Zyngier
KVM/arm64 has:
select
KVM_GUEST_MEMFD
I also confirmed that KVM/arm64 selects KVM_GUEST_MEMFD, so I'll drop the redundant #ifdef in v13.
Quoted 5 lines
Why is it an inline function? Given that it takes a kvm parameter, I fully expect that you'll have to evaluate this at some point.
But since struct kvm cannot be dereferenced in
asm/kvm_host.h, it will have to move. How about doing it right now?
Agreed. Although the current implementation does not dereference kvm, it makes sense to move it out of asm/kvm_host.h now. I'll move the implementation to an arm64 KVM .c file in v13.
PATCH v12 10/16 KVM: guest_memfd: Add flag to remove from direct map
In reply to Kalyazin, NikitaFrom: Patrick Roy <[email protected]>
Add GUEST_MEMFD_FLAG_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 GUEST_MEMFD_FLAG_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 GUEST_MEMFD_FLAG_MMAP is not set). 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. In this setup, 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.
Direct map entries are zapped right before guest or userspace mappings of gmem folios are set up, e.g. in kvm_gmem_fault_user_mapping() or kvm_gmem_get_pfn() [called from the KVM MMU code]. At present, direct map removal is not supported on platforms that support kvm_gmem_populate(). In case such support is added in the future, the following ordering is maintained: zap then prepare, invalidate then restore, to avoid having guest-owned pages being temporarily mapped on by host. This assumes that preparation or invalidation code does not access the page content.
--- Documentation/virt/kvm/api.rst | 21 +++++----- include/linux/kvm_host.h | 3 ++ include/uapi/linux/kvm.h | 1 + virt/kvm/guest_memfd.c | 71 ++++++++++++++++++++++++++++++++-- 4 files changed, 83 insertions(+), 13 deletions(-)
Documentation/virt/kvm/api.rst +12 -9
@@ -6439,15 +6439,18 @@ a single guest_memfd file, but the bound ranges must not overlap). The capability KVM_CAP_GUEST_MEMFD_FLAGS enumerates the `flags` that can be specified via KVM_CREATE_GUEST_MEMFD. Currently defined flags: - ============================ ================================================ - GUEST_MEMFD_FLAG_MMAP Enable using mmap() on the guest_memfd file - descriptor. - GUEST_MEMFD_FLAG_INIT_SHARED Make all memory in the file shared during - KVM_CREATE_GUEST_MEMFD (memory files created - without INIT_SHARED will be marked private). - Shared memory can be faulted into host userspace - page tables. Private memory cannot. - ============================ ================================================ + ============================== ================================================ + GUEST_MEMFD_FLAG_MMAP Enable using mmap() on the guest_memfd file + descriptor. + GUEST_MEMFD_FLAG_INIT_SHARED Make all memory in the file shared during + KVM_CREATE_GUEST_MEMFD (memory files created + without INIT_SHARED will be marked private). + Shared memory can be faulted into host userspace + page tables. Private memory cannot. + GUEST_MEMFD_FLAG_NO_DIRECT_MAP The guest_memfd instance will unmap the memory + backing it from the kernel's address space + before passing it off to userspace or the guest. + ============================== ================================================ When the KVM MMU performs a PFN lookup to service a guest fault and the backing guest_memfd has the GUEST_MEMFD_FLAG_MMAP set, then the fault will always be
include/linux/kvm_host.h +3 -0
@@ -738,6 +738,9 @@ static inline u64 kvm_gmem_get_supported_flags(struct kvm *kvm)if (!kvm ||kvm_arch_supports_gmem_init_shared (kvm)) flags |=GUEST_MEMFD_FLAG_INIT_SHARED ; +if (!kvm ||kvm_arch_gmem_supports_no_direct_map (kvm)) + flags |=GUEST_MEMFD_FLAG_NO_DIRECT_MAP ; + return flags; }#endif
include/uapi/linux/kvm.h +1 -0
@@ -1642,6 +1642,7 @@ struct kvm_memory_attributes {#define KVM_CREATE_GUEST_MEMFD _IOWR(KVMIO, 0xd4, struct kvm_create_guest_memfd)#define GUEST_MEMFD_FLAG_MMAP (1ULL << 0)#define GUEST_MEMFD_FLAG_INIT_SHARED (1ULL << 1) +#define GUEST_MEMFD_FLAG_NO_DIRECT_MAP (1ULL << 2)struct kvm_create_guest_memfd {__u64 size;
virt/kvm/guest_memfd.c +67 -4
122 lines not on this page · raw message
Signature · 1 line
2.50.1
Sean Christopherson
maintainerRe: [PATCH v12 10/16] KVM: guest_memfd: Add flag to remove from direct map
Quoted 28 lines · Nikita Kalyazin
From: Patrick Roy <[email protected]>
Add
GUEST_MEMFD_FLAG_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_spaceifGUEST_MEMFD_FLAG_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 ifGUEST_MEMFD_FLAG_MMAPis not set). 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. In this setup, KVM retains access to guest memory via userspace mappings ofguest_memfd, which are reflected back into KVM's memslots viauserspace_addr. This is needed for things like MMIO emulation onx86_64to work.Direct map entries are zapped right before guest or userspace mappings of gmem folios are set up, e.g. in
kvm_gmem_fault_user_mapping()orkvm_gmem_get_pfn()[called from the KVM MMU code].
...
Quoted 22 lines
virt/kvm/guest_memfd.c +22
-0+#define KVM_GMEM_FOLIO_NO_DIRECT_MAP BIT(0) + +static bool kvm_gmem_folio_no_direct_map (struct folio *folio) +{ +return ((u64 )folio->private) &KVM_GMEM_FOLIO_NO_DIRECT_MAP ; +} + +static int kvm_gmem_folio_zap_direct_map (struct folio *folio) +{ +int r =0 ; + +VM_WARN_ON_FOLIO (!folio_test_locked (folio), folio); + +if (WARN_ON_ONCE (!(GMEM_I (folio_inode (folio))->flags &GUEST_MEMFD_FLAG_NO_DIRECT_MAP ))) +return -EINVAL ; + +if (kvm_gmem_folio_no_direct_map (folio)) + goto out; + + r =folio_zap_direct_map (folio); +if (!r) + folio->private = (void *)((u64 )folio->private |KVM_GMEM_FOLIO_NO_DIRECT_MAP );
virt/kvm/guest_memfd.c static int __kvm_gmem_prepare_folio(struct kvm *kvm, struct kvm_memory_slot *slo · line 111
Quoted 10 lines
virt/kvm/guest_memfd.c +10
-0+ +out: +return r; +} + +static void kvm_gmem_folio_restore_direct_map (struct folio *folio) +{ +folio_restore_direct_map (folio); + folio->private = (void *)((u64 )folio->private & ~KVM_GMEM_FOLIO_NO_DIRECT_MAP ); +}
Making guest_memfd responsible for zapping and restoring the direct map on a per- folio basis feels wrong given the addition of AS_NO_DIRECT_MAP. I especially don't like that the "rules" for when an AS_NO_DIRECT_MAP folio has a direct map will vary based on the owner, and even within an owner (e.g. guest_memfd) will be ad hoc.
E.g. as per the series to add guest_memfd write() support[*]:
When direct map removal is implemented [2]
- write() will not be allowed to access pages that have already been removed from direct map
- on completion, write() will remove the populated pages from direct map
That's pretty gross ABI, because with KVM_GMEM_FOLIO_NO_DIRECT_MAP, userspace can write() exactly once. To re-write memory, I assume userspace would need to do a PUNCH_HOLE or truncate.
What's preventing us from handling this automagically in e.g. filemap_add_folio() and filemap_remove_folio()? Then the usage rules are pretty straightforward: the kernel must *always* assume the direct map is invalid for folios from AS_NO_DIRECT_MAP mappings.
Then if KVM needs to utilize a kernel mapping, e.g. in kvm_gmem_populate(), KVM could use dedicated variants of kmap_local_xxx() to deal with a local mapping for a folio/page without a direct map. Or, KVM could simply disallow the specific sequence that would require KVM to do the memcpy (I'm pretty sure we can do that with in-place shared=>private conversion support).
I realize that could throw a big wrench into write() performance, but IMO, before merging either series, we need a complete story for exactly how this will all fit together, in a maintainable fashion and with sane ABI.
Re: [PATCH v12 10/16] KVM: guest_memfd: Add flag to remove from direct map
Quoted 94 lines · Sean Christopherson
Quoted 28 lines · Nikita Kalyazin
From: Patrick Roy <[email protected]>
Add
GUEST_MEMFD_FLAG_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_spaceifGUEST_MEMFD_FLAG_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 ifGUEST_MEMFD_FLAG_MMAPis not set). 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. In this setup, KVM retains access to guest memory via userspace mappings ofguest_memfd, which are reflected back into KVM's memslots viauserspace_addr. This is needed for things like MMIO emulation onx86_64to work.Direct map entries are zapped right before guest or userspace mappings of gmem folios are set up, e.g. in
kvm_gmem_fault_user_mapping()orkvm_gmem_get_pfn()[called from the KVM MMU code]....
Quoted 22 lines
virt/kvm/guest_memfd.c +22
-0+#define KVM_GMEM_FOLIO_NO_DIRECT_MAP BIT(0) + +static bool kvm_gmem_folio_no_direct_map (struct folio *folio) +{ +return ((u64 )folio->private) &KVM_GMEM_FOLIO_NO_DIRECT_MAP ; +} + +static int kvm_gmem_folio_zap_direct_map (struct folio *folio) +{ +int r =0 ; + +VM_WARN_ON_FOLIO (!folio_test_locked (folio), folio); + +if (WARN_ON_ONCE (!(GMEM_I (folio_inode (folio))->flags &GUEST_MEMFD_FLAG_NO_DIRECT_MAP ))) +return -EINVAL ; + +if (kvm_gmem_folio_no_direct_map (folio)) + goto out; + + r =folio_zap_direct_map (folio); +if (!r) + folio->private = (void *)((u64 )folio->private |KVM_GMEM_FOLIO_NO_DIRECT_MAP );virt/kvm/guest_memfd.c static int __kvm_gmem_prepare_folio(struct kvm *kvm, struct kvm_memory_slot *slo · line 111
Quoted 10 lines
virt/kvm/guest_memfd.c +10
-0+ +out: +return r; +} + +static void kvm_gmem_folio_restore_direct_map (struct folio *folio) +{ +folio_restore_direct_map (folio); + folio->private = (void *)((u64 )folio->private & ~KVM_GMEM_FOLIO_NO_DIRECT_MAP ); +}Making
guest_memfdresponsible for zapping and restoring the direct map on a per- folio basis feels wrong given the addition ofAS_NO_DIRECT_MAP. I especially don't like that the "rules" for when anAS_NO_DIRECT_MAPfolio has a direct map will vary based on the owner, and even within an owner (e.g.guest_memfd) will be ad hoc.E.g. as per the series to add
guest_memfdwrite()support[*]:When direct map removal is implemented [2]
-write()will not be allowed to access pages that have already been removed from direct map
- on completion,write()will remove the populated pages from direct mapThat's pretty gross ABI, because with
KVM_GMEM_FOLIO_NO_DIRECT_MAP, userspace canwrite()exactly once. To re-write memory, I assume userspace would need to do aPUNCH_HOLEor truncate.What's preventing us from handling this automagically in e.g.
filemap_add_folio()andfilemap_remove_folio()? Then the usage rules are pretty straightforward: the kernel must *always* assume the direct map is invalid for folios fromAS_NO_DIRECT_MAPmappings.Then if KVM needs to utilize a kernel mapping, e.g. in
kvm_gmem_populate(), KVM could use dedicated variants ofkmap_local_xxx()to deal with a local mapping for a folio/page without a direct map. Or, KVM could simply disallow the specific sequence that would require KVM to do the memcpy (I'm pretty sure we can do that with in-place shared=>private conversion support).
Quoted 6 lines
I realize that could throw a big wrench into
write()performance, but IMO, before merging either series, we need a complete story for exactly how this will all fit together, in a maintainable fashion and with sane ABI.
I agree with this - this approach would also allow for memory that was never in the direct map to begin with, or has been taken out already (for which I happen to have a use case :-)). guest_memfd and other code can then assume that AS_NO_DIRECT_MAP means they have to take explicit action to map it if needed. It's a clean, simple ABI.
With the current set of patches, it seems like this couldn't be done in a clean manner.
- Frank
Re: [PATCH v12 10/16] KVM: guest_memfd: Add flag to remove from direct map
Hi Sean, Frank, Lorenzo,
Quoted 33 lines · Frank van der Linden
Quoted 28 lines · Sean Christopherson
Making
guest_memfdresponsible for zapping and restoring the direct map on a per- folio basis feels wrong given the addition ofAS_NO_DIRECT_MAP. I especially don't like that the "rules" for when anAS_NO_DIRECT_MAPfolio has a direct map will vary based on the owner, and even within an owner (e.g.guest_memfd) will be ad hoc.E.g. as per the series to add
guest_memfdwrite()support[*]:When direct map removal is implemented [2]
-write()will not be allowed to access pages that have already been removed from direct map
- on completion,write()will remove the populated pages from direct mapThat's pretty gross ABI, because with
KVM_GMEM_FOLIO_NO_DIRECT_MAP, userspace canwrite()exactly once. To re-write memory, I assume userspace would need to do aPUNCH_HOLEor truncate.What's preventing us from handling this automagically in e.g.
filemap_add_folio()andfilemap_remove_folio()? Then the usage rules are pretty straightforward: the kernel must *always* assume the direct map is invalid for folios fromAS_NO_DIRECT_MAPmappings.Then if KVM needs to utilize a kernel mapping, e.g. in
kvm_gmem_populate(), KVM could use dedicated variants ofkmap_local_xxx()to deal with a local mapping for a folio/page without a direct map. Or, KVM could simply disallow the specific sequence that would require KVM to do the memcpy (I'm pretty sure we can do that with in-place shared=>private conversion support).Quoted 3 lines
I realize that could throw a big wrench into
write()performance, but IMO, before merging either series, we need a complete story for exactly how this will all fit together, in a maintainable fashion and with sane ABI.
Quoted 8 lines
I agree with this - this approach would also allow for memory that was never in the direct map to begin with, or has been taken out already (for which I happen to have a use case :-)).
guest_memfdand other code can then assume thatAS_NO_DIRECT_MAPmeans they have to take explicit action to map it if needed. It's a clean, simple ABI.With the current set of patches, it seems like this couldn't be done in a clean manner.
Agreed with both of you. I'll adopt the filemap-level approach:
- Move the zap/restore hooks from guest_memfd into filemap_add_folio() / filemap_remove_folio().
- Tighten AS_NO_DIRECT_MAP semantics so that, for folios in such a mapping, the direct map is invalid for the entire time the folio resides in the page cache.
- Drop the per-folio KVM_GMEM_FOLIO_NO_DIRECT_MAP bookkeeping in folio->private, since the existence of the folio in the mapping is itself the state.
On each guest memory population path,
- memcpy-based population from userspace goes through the userspace mapping of guest_memfd, not through the kernel direct map, so the filemap-level invariant doesn't affect it. But this is slow, which is what motivated the write() syscall support.
- write(): meant to speed up the userspace-memcpy case above by doing the copy in the kernel. I believe Brendan's __GFP_UNMAPPED/mermap work [1] would give us a low-overhead way to get temporary kernel access to an AS_NO_DIRECT_MAP. Landing mermap may take a while, but this series does not introduce the write() path, so mermap is not a blocker for now.
- kvm_gmem_populate(): this is a TDX/SNP-only path, and NO_DIRECT_MAP is not available on those VM types — kvm_arch_gmem_supports_no_direct_map() returns false for KVM_X86_TDX_VM and KVM_X86_SNP_VM, which are its only callers today. So it doesn't interact with the filemap invariant IIUC.
So, unless I'm missing any path, adopting the filemap-level approach in this series should be fine.
I'd like to consult with you folks on how to proceed in advance. In a separate reply on the cover letter thread [2], Lorenzo and Sean suggested that the mm pieces should go through the mm subsystem:
Quoted 4 lines · Sean Christopherson
Yeah, when the time comes, the mm pieces definitely need to go through the mm tree. Ideally, I think this would be merged in two separate parts, with all mm changes going through the mm tree, and then the KVM changes through the KVM tree using a stable topic branch/tag from Andrew.
I see two reasonable paths to get there, and would appreciate your input on which you prefer:
Path A — validate on KVM side first, then split:
- Post v13 as a single series on the KVM list, gather feedback and make sure the design is acceptable to KVM reviewers.
- Once v13 looks good ("the time comes"), do the MM/KVM split, rebase the MM part onto the appropriate MM branch, and post the MM part to linux-mm to build consensus with MM maintainers.
Path B — split early and seek MM consensus in parallel:
- With the filemap rework already in place, do the MM/KVM split now and post the MM part to linux-mm directly. The KVM part follows on top of a stable topic from MM.
Which of the two would you rather see? Happy to go either way.
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
Takahiro
Re: [PATCH v12 10/16] KVM: guest_memfd: Add flag to remove from direct map
Quoted 3 lines · Takahiro Itazuri
[...snip...]
Brought this topic up on the guest_memfd biweekly today!
Quoted 26 lines
Agreed with both of you. I'll adopt the filemap-level approach:
- Move the zap/restore hooks from
guest_memfdintofilemap_add_folio()/filemap_remove_folio().
- TightenAS_NO_DIRECT_MAPsemantics so that, for folios in such a mapping, the direct map is invalid for the entire time the folio resides in the page cache.
- Drop the per-folioKVM_GMEM_FOLIO_NO_DIRECT_MAPbookkeeping infolio->private, since the existence of the folio in the mapping is itself the state.On each guest memory population path,
- memcpy-based population from userspace goes through the userspace mapping of
guest_memfd, not through the kernel direct map, so the filemap-level invariant doesn't affect it. But this is slow, which is what motivated thewrite()syscall support.-
write(): meant to speed up the userspace-memcpy case above by doing the copy in the kernel. I believe Brendan's __GFP_UNMAPPED/mermap work [1] would give us a low-overhead way to get temporary kernel access to anAS_NO_DIRECT_MAP. Landing mermap may take a while, but this series does not introduce thewrite()path, so mermap is not a blocker for now.
Quoted 6 lines
-
kvm_gmem_populate(): this is a TDX/SNP-only path, andNO_DIRECT_MAPis not available on those VM types —kvm_arch_gmem_supports_no_direct_map()returns false forKVM_X86_TDX_VMandKVM_X86_SNP_VM, which are its only callers today. So it doesn't interact with the filemap invariant IIUC.
I'm a little bit uncomfortable this statement since it seems to say TDX and SNP aren't taken care of. Would just like to discuss (for a line of sight to SNP and TDX support):
For non-in-place population where the source physical page is different from the destination physical page,
+ TDX: the TDX module does the population and works with physical addresses, so no issue with populate? Other parts of TDX may have trouble though, but that can be handled later.
+ SNP: sev_gmem_post_populate() does a memcpy() after using kmap_local_page()
Would mermap be a drop in replacement for kmap_local_page() here? Would guest_memfd need to force a TLB flush after mermap+memcpy?
Quoted 24 lines
So, unless I'm missing any path, adopting the filemap-level approach in this series should be fine.
I'd like to consult with you folks on how to proceed in advance. In a separate reply on the cover letter thread [2], Lorenzo and Sean suggested that the mm pieces should go through the mm subsystem:
Quoted 4 lines · Sean Christopherson
Yeah, when the time comes, the mm pieces definitely need to go through the mm tree. Ideally, I think this would be merged in two separate parts, with all mm changes going through the mm tree, and then the KVM changes through the KVM tree using a stable topic branch/tag from Andrew.
I see two reasonable paths to get there, and would appreciate your input on which you prefer:
Path A — validate on KVM side first, then split:
- Post v13 as a single series on the KVM list, gather feedback and make sure the design is acceptable to KVM reviewers.
- Once v13 looks good ("the time comes"), do the MM/KVM split, rebase the MM part onto the appropriate MM branch, and post the MM part to linux-mm to build consensus with MM maintainers.
Quoted 7 lines
Path B — split early and seek MM consensus in parallel:
- With the filemap rework already in place, do the MM/KVM split now and post the MM part to linux-mm directly. The KVM part follows on top of a stable topic from MM.Which of the two would you rather see? Happy to go either way.
Vlastimil pointed out that there's a temporary limitation now that the mm-tree cannot do stable branches shared between trees now.
I think it depends on how quickly you plan to refresh this series, but Path A wouldn't be blocked by the temporary limitation.
My opinion would be to go ahead with a new revision (Path A) to fully address comments before splitting the series. Any Reviewed-bys can be carried over to the split series anyway :)
Alternatively you could wait till conversion lands :P Either one of us will need to do more work for conversion wrt direct map removal.
Quoted 5 lines
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/Takahiro
Re: [PATCH v12 10/16] KVM: guest_memfd: Add flag to remove from direct map
Alright I think I'm finally getting a bit more up to speed on the important questions here.
Quoted 19 lines · Ackerley Tng
Quoted 3 lines · Takahiro Itazuri
[...snip...]
Brought this topic up on the
guest_memfdbiweekly today!Quoted 11 lines
Agreed with both of you. I'll adopt the filemap-level approach:
- Move the zap/restore hooks from
guest_memfdintofilemap_add_folio()/filemap_remove_folio().
- TightenAS_NO_DIRECT_MAPsemantics so that, for folios in such a mapping, the direct map is invalid for the entire time the folio resides in the page cache.
- Drop the per-folioKVM_GMEM_FOLIO_NO_DIRECT_MAPbookkeeping infolio->private, since the existence of the folio in the mapping is itself the state.
Yeah so I protoyped this and I think it's fine.. except for zeroing.
Quoted 14 lines
Quoted 14 lines
On each guest memory population path,
- memcpy-based population from userspace goes through the userspace mapping of
guest_memfd, not through the kernel direct map, so the filemap-level invariant doesn't affect it. But this is slow, which is what motivated thewrite()syscall support.-
write(): meant to speed up the userspace-memcpy case above by doing the copy in the kernel. I believe Brendan's __GFP_UNMAPPED/mermap work [1] would give us a low-overhead way to get temporary kernel access to anAS_NO_DIRECT_MAP. Landing mermap may take a while, but this series does not introduce thewrite()path, so mermap is not a blocker for now.
Quoted 5 lines
Quoted 5 lines
-
kvm_gmem_populate(): this is a TDX/SNP-only path, andNO_DIRECT_MAPis not available on those VM types —kvm_arch_gmem_supports_no_direct_map()returns false forKVM_X86_TDX_VMandKVM_X86_SNP_VM, which are its only callers today. So it doesn't interact with the filemap invariant IIUC.
There are also the fault paths though; if the pages are nonpresent in the direct map for the duration of their life in the page cache (and I think they should be) then by the time we get to kvm_mmu_faultin_pfn_gmem() or kvm_gmem_fault_user_mapping() we lost the ability to zero them.
My original answer for this was "that's fine, we'll use __GFP_ZERO (which will probably use the mermap under the hood)", but now I've realised there's a good reason we don't set __GFP_ZERO at the moment, namely that it's wasted if we end up doing kvm_gmem_populate()
(Continued below...)
Quoted 3 lines
I'm a little bit uncomfortable this statement since it seems to say TDX and SNP aren't taken care of. Would just like to discuss (for a line of sight to SNP and TDX support):
Are you saying we need NO_DIRECT_MAP support for TDX/SNP? I think that would be doable but what's the value? So that we can get a #PF instead of #MCE if we screw up?
Quoted 10 lines
For non-in-place population where the source physical page is different from the destination physical page,
+ TDX: the TDX module does the population and works with physical addresses, so no issue with populate? Other parts of TDX may have trouble though, but that can be handled later.
+ SNP:sev_gmem_post_populate()does amemcpy()after usingkmap_local_page()Would mermap be a drop in replacement for
kmap_local_page()here?
Yeah basically.
Quoted 1 line
Would
guest_memfdneed to force a TLB flush after mermap+memcpy?
It's not required for correctness, no (mermap does those flushes internally). For security, I dunno, this comes back to my confusion above about why we'd want NO_DIRECT_MAP for TDX at all, maybe best to chat face-to-face about that and then follow up here with a summary.
====
ANYWAY, here is how I would ultimately see all of this working, at least for non-CoCo cases:
- AS_NO_DIRECT_MAP causes filemap.c to set ALLOC_UNMAPPED (that's what the next iteration of __GFP_UNMAPPED will be called) so you get pages directly from the page allocator that are already fully zapped.
- Where guest_memfd.c currently does clear_highpage(), it now isntead does something a bit like clear_page_mermap() from
https://lore.kernel.org/all/[email protected]/
- The write() path does something similar with the mermap.
- Those mermap operations would leave behind stale TLB entries that could be exploited by the VMM for CPU vulns. To prevent that we need to force a TLB flush before freeing the physical pages they point to. Luckily now that all the folio allocations are pushed into mm/filemap.c we can just do that in kvm_gmem_free_folio(), preventing bugs like the one I had here (bottom of the mail):
https://lore.kernel.org/all/[email protected]/
Note there's no need for the page allocator to suport ALLOC_UNMAPPED with __GFP_ZERO in this design, which is nice.
====
NOW, the thing I'm stuck on (again lol) is the patchset-fu. Here's all the parts we need, with dependencies indented:
0. efficient GUEST_MEMFD_FLAG_NO_DIRECT_MAP
1. AS_NO_DIRECT_MAP
2. ALLOC_UNMAPPED (formerly known as __GFP_UNMAPPED)
3. alloc_flags arg to the page allocator (I'm sneakily introducing this in [1])
4. freetype_t
5. The mermap
6. The mm-local region
I originally posted all of those in [0], except part 3. Doing all of that together in one series would be a bit too much though. Approaches I can see to avoid that:
Approach X:
- Do parts 1, 2 and 4 as a standalone series. The only beneficiary of AS_NO_DIRECT_MAP would be secretmem.
- Then another series that fills in 0, 5 and 6.
Approach Y:
- One series that does parts 0, 1, 5, and 6. AS_NO_DIRECT_MAP is implemented by having filemap.c itself call folio_zap_direct_map(), then guest_memfd.c zeroes it via the mermap. It works but it's really slow.
- Then another series that fills in parts 2 and 4, switches filemap.c over from manual folio_zap_direct_map() to ALLOC_UNMAPPED, making things fast.
Approach X seems natural from a code progression perspective but leaves us with an interim phase where we have a bunch of complexity just to "optimise secretmem" which nobody cares about.
Approach Y seems natural from a feature progression perspective but leaves us with an interim phase where we expensively zap a page, only to then immediately do this complex mermap dance to access it right afterwards.
Any thoughts / other ideas? Personally I think I prefer X.
[0] https://lore.kernel.org/all/[email protected]/
[1] https://lore.kernel.org/all/[email protected]/
Apologies for the Friday night essay :D
Brendan
Sean Christopherson
maintainerRe: [PATCH v12 10/16] KVM: guest_memfd: Add flag to remove from direct map
Quoted 10 lines · Brendan Jackman
There are also the fault paths though; if the pages are nonpresent in the direct map for the duration of their life in the page cache (and I think they should be) then by the time we get to
kvm_mmu_faultin_pfn_gmem()orkvm_gmem_fault_user_mapping()we lost the ability to zero them.My original answer for this was "that's fine, we'll use
__GFP_ZERO(which will probably use the mermap under the hood)", but now I've realised there's a good reason we don't set__GFP_ZEROat the moment, namely that it's wasted if we end up doingkvm_gmem_populate()
Eh, don't worry about the wasted cycles for populate(). The overhead of zeroing a page is dwarfed by the overhead of adding (and in most cases, measuring) the page.
We (KVM folks) discussed this in the context of in-place conversions, and long story short, the consensus is that shaving cycles by eliding the zeroing in the host isn't worth the complexity.
Quoted 8 lines
(Continued below...)
Quoted 3 lines
I'm a little bit uncomfortable this statement since it seems to say TDX and SNP aren't taken care of. Would just like to discuss (for a line of sight to SNP and TDX support):
Are you saying we need
NO_DIRECT_MAPsupport for TDX/SNP? I think that would be doable but what's the value?
Hardening against consumption of shared memory? Yes, the guest has explicitly shared the memory so there are (very) reduced expectations around confidentiality and integrity, but defense in depth and all that.
For me, the main thing is that I don't want to completely punt on the interaction of the two things, and end up with an unworkable mess if there's ever a strong reason for CoCo VMs to support NO_DIRECT_MAP.
Quoted 1 line
So that we can get a #PF instead of #MCE if we screw up?
Note, SNP gets a #PF either way.
Quoted 34 lines
NOW, the thing I'm stuck on (again lol) is the patchset-fu. Here's all the parts we need, with dependencies indented:
0. efficient
GUEST_MEMFD_FLAG_NO_DIRECT_MAP
1.AS_NO_DIRECT_MAP
2.ALLOC_UNMAPPED(formerly known as__GFP_UNMAPPED)
3.alloc_flagsarg to the page allocator (I'm sneakily introducing this in [1])
4.freetype_t
5. The mermap
6. The mm-local regionI originally posted all of those in [0], except part 3. Doing all of that together in one series would be a bit too much though. Approaches I can see to avoid that:
Approach X:
- Do parts 1, 2 and 4 as a standalone series. The only beneficiary ofAS_NO_DIRECT_MAPwould be secretmem.
- Then another series that fills in 0, 5 and 6.Approach Y:
- One series that does parts 0, 1, 5, and 6.AS_NO_DIRECT_MAPis implemented by havingfilemap.citself callfolio_zap_direct_map(), thenguest_memfd.czeroes it via the mermap. It works but it's really slow.
- Then another series that fills in parts 2 and 4, switchesfilemap.cover from manualfolio_zap_direct_map()toALLOC_UNMAPPED, making things fast.Approach X seems natural from a code progression perspective but leaves us with an interim phase where we have a bunch of complexity just to "optimise secretmem" which nobody cares about.
Quoted 6 lines
Approach Y seems natural from a feature progression perspective but leaves us with an interim phase where we expensively zap a page, only to then immediately do this complex mermap dance to access it right afterwards.
Any thoughts / other ideas? Personally I think I prefer X.
I don't have preference between those options, mostly because I don't appreciate the difference. My overarching preference is to separate the mm/ work from the guest_memfd as much as possible. Beyond that, I probably don't care all that much?
Re: [PATCH v12 10/16] KVM: guest_memfd: Add flag to remove from direct map
Quoted 12 lines · Sean Christopherson
Quoted 10 lines · Brendan Jackman
There are also the fault paths though; if the pages are nonpresent in the direct map for the duration of their life in the page cache (and I think they should be) then by the time we get to
kvm_mmu_faultin_pfn_gmem()orkvm_gmem_fault_user_mapping()we lost the ability to zero them.My original answer for this was "that's fine, we'll use
__GFP_ZERO(which will probably use the mermap under the hood)", but now I've realised there's a good reason we don't set__GFP_ZEROat the moment, namely that it's wasted if we end up doingkvm_gmem_populate()
Quoted 7 lines
Eh, don't worry about the wasted cycles for
populate(). The overhead of zeroing a page is dwarfed by the overhead of adding (and in most cases, measuring) the page.We (KVM folks) discussed this in the context of in-place conversions, and long story short, the consensus is that shaving cycles by eliding the zeroing in the host isn't worth the complexity.
OK thanks, this is useful. Nonetheless because it leads to a less confusing dishgn, I think it still makes sense to just disallow __GFP_ZERO for the unmapped pages.
(At least, at the page allocator level. We might want to allow it in address_space.gfp_mask or something and have filemap.c deal with it. But, overloading a GFP flag like this is yucky, if we need a flag for that I think it's likely neater to make it an AS_ flag or something).
Quoted 6 lines
Quoted 6 lines
(Continued below...)
Quoted 3 lines
I'm a little bit uncomfortable this statement since it seems to say TDX and SNP aren't taken care of. Would just like to discuss (for a line of sight to SNP and TDX support):
Quoted 6 lines
Quoted 2 lines
Are you saying we need
NO_DIRECT_MAPsupport for TDX/SNP? I think that would be doable but what's the value?Hardening against consumption of shared memory? Yes, the guest has explicitly shared the memory so there are (very) reduced expectations around confidentiality and integrity, but defense in depth and all that.
Ah yeah, it did not occur to me that there was memory being set up via CoCo paths that is not encrypted. So yeah that makes perfect sense. Eventually we'll want to be able to lean on this to protect that shared memory against attacks by other VMs etc.
Quoted 3 lines
For me, the main thing is that I don't want to completely punt on the interaction of the two things, and end up with an unworkable mess if there's ever a strong reason for CoCo VMs to support
NO_DIRECT_MAP.
Yeah that makes sense.
Quoted 43 lines
Quoted 1 line
So that we can get a #PF instead of #MCE if we screw up?
Note, SNP gets a #PF either way.
Quoted 39 lines
NOW, the thing I'm stuck on (again lol) is the patchset-fu. Here's all the parts we need, with dependencies indented:
0. efficient
GUEST_MEMFD_FLAG_NO_DIRECT_MAP
1.AS_NO_DIRECT_MAP
2.ALLOC_UNMAPPED(formerly known as__GFP_UNMAPPED)
3.alloc_flagsarg to the page allocator (I'm sneakily introducing this in [1])
4.freetype_t
5. The mermap
6. The mm-local regionI originally posted all of those in [0], except part 3. Doing all of that together in one series would be a bit too much though. Approaches I can see to avoid that:
Approach X:
- Do parts 1, 2 and 4 as a standalone series. The only beneficiary ofAS_NO_DIRECT_MAPwould be secretmem.
- Then another series that fills in 0, 5 and 6.Approach Y:
- One series that does parts 0, 1, 5, and 6.AS_NO_DIRECT_MAPis implemented by havingfilemap.citself callfolio_zap_direct_map(), thenguest_memfd.czeroes it via the mermap. It works but it's really slow.
- Then another series that fills in parts 2 and 4, switchesfilemap.cover from manualfolio_zap_direct_map()toALLOC_UNMAPPED, making things fast.Approach X seems natural from a code progression perspective but leaves us with an interim phase where we have a bunch of complexity just to "optimise secretmem" which nobody cares about.
Approach Y seems natural from a feature progression perspective but leaves us with an interim phase where we expensively zap a page, only to then immediately do this complex mermap dance to access it right afterwards.
Quoted 5 lines
Quoted 1 line
Any thoughts / other ideas? Personally I think I prefer X.
I don't have preference between those options, mostly because I don't appreciate the difference. My overarching preference is to separate the
mm/work from theguest_memfdas much as possible. Beyond that, I probably don't care all that much?
Cool thanks, shrugs are fine here.
Main thing I'm trying to do here is generate some awareness that the decision is being made, so that I don't forge ahead with X and then have people going "ugh, why haven't you considered Y?" If nobody cares either way then nothing to worry about.
Lorenzo Stoakes
maintainerRe: [PATCH v12 10/16] KVM: guest_memfd: Add flag to remove from direct map
Quoted 103 lines · Brendan Jackman
Quoted 12 lines · Sean Christopherson
Quoted 10 lines · Brendan Jackman
There are also the fault paths though; if the pages are nonpresent in the direct map for the duration of their life in the page cache (and I think they should be) then by the time we get to
kvm_mmu_faultin_pfn_gmem()orkvm_gmem_fault_user_mapping()we lost the ability to zero them.My original answer for this was "that's fine, we'll use
__GFP_ZERO(which will probably use the mermap under the hood)", but now I've realised there's a good reason we don't set__GFP_ZEROat the moment, namely that it's wasted if we end up doingkvm_gmem_populate()Quoted 7 lines
Eh, don't worry about the wasted cycles for
populate(). The overhead of zeroing a page is dwarfed by the overhead of adding (and in most cases, measuring) the page.We (KVM folks) discussed this in the context of in-place conversions, and long story short, the consensus is that shaving cycles by eliding the zeroing in the host isn't worth the complexity.
OK thanks, this is useful. Nonetheless because it leads to a less confusing dishgn, I think it still makes sense to just disallow
__GFP_ZEROfor the unmapped pages.(At least, at the page allocator level. We might want to allow it in address_space.gfp_mask or something and have
filemap.cdeal with it. But, overloading a GFP flag like this is yucky, if we need a flag for that I think it's likely neater to make it anAS_flag or something).Quoted 6 lines
Quoted 6 lines
(Continued below...)
Quoted 3 lines
I'm a little bit uncomfortable this statement since it seems to say TDX and SNP aren't taken care of. Would just like to discuss (for a line of sight to SNP and TDX support):
Quoted 6 lines
Quoted 2 lines
Are you saying we need
NO_DIRECT_MAPsupport for TDX/SNP? I think that would be doable but what's the value?Hardening against consumption of shared memory? Yes, the guest has explicitly shared the memory so there are (very) reduced expectations around confidentiality and integrity, but defense in depth and all that.
Ah yeah, it did not occur to me that there was memory being set up via CoCo paths that is not encrypted. So yeah that makes perfect sense. Eventually we'll want to be able to lean on this to protect that shared memory against attacks by other VMs etc.
Quoted 3 lines
For me, the main thing is that I don't want to completely punt on the interaction of the two things, and end up with an unworkable mess if there's ever a strong reason for CoCo VMs to support
NO_DIRECT_MAP.Yeah that makes sense.
Quoted 43 lines
Quoted 1 line
So that we can get a #PF instead of #MCE if we screw up?
Note, SNP gets a #PF either way.
Quoted 39 lines
NOW, the thing I'm stuck on (again lol) is the patchset-fu. Here's all the parts we need, with dependencies indented:
0. efficient
GUEST_MEMFD_FLAG_NO_DIRECT_MAP
1.AS_NO_DIRECT_MAP
2.ALLOC_UNMAPPED(formerly known as__GFP_UNMAPPED)
3.alloc_flagsarg to the page allocator (I'm sneakily introducing this in [1])
4.freetype_t
5. The mermap
6. The mm-local regionI originally posted all of those in [0], except part 3. Doing all of that together in one series would be a bit too much though. Approaches I can see to avoid that:
Approach X:
- Do parts 1, 2 and 4 as a standalone series. The only beneficiary ofAS_NO_DIRECT_MAPwould be secretmem.
- Then another series that fills in 0, 5 and 6.Approach Y:
- One series that does parts 0, 1, 5, and 6.AS_NO_DIRECT_MAPis implemented by havingfilemap.citself callfolio_zap_direct_map(), thenguest_memfd.czeroes it via the mermap. It works but it's really slow.
- Then another series that fills in parts 2 and 4, switchesfilemap.cover from manualfolio_zap_direct_map()toALLOC_UNMAPPED, making things fast.Approach X seems natural from a code progression perspective but leaves us with an interim phase where we have a bunch of complexity just to "optimise secretmem" which nobody cares about.
Approach Y seems natural from a feature progression perspective but leaves us with an interim phase where we expensively zap a page, only to then immediately do this complex mermap dance to access it right afterwards.
Quoted 5 lines
Quoted 1 line
Any thoughts / other ideas? Personally I think I prefer X.
I don't have preference between those options, mostly because I don't appreciate the difference. My overarching preference is to separate the
mm/work from theguest_memfdas much as possible. Beyond that, I probably don't care all that much?
Quoted 6 lines
Cool thanks, shrugs are fine here.
Main thing I'm trying to do here is generate some awareness that the decision is being made, so that I don't forge ahead with X and then have people going "ugh, why haven't you considered Y?" If nobody cares either way then nothing to worry about.
X seems fine to me and that seems to be the consensus here. And it seems moot if Y relies upon mm stable branches which are not here yet :)
Cheers, Lorenzo
PATCH v12 11/16 KVM: selftests: load elf via bounce buffer
In reply to Kalyazin, NikitaFrom: Patrick Roy <[email protected]>
If 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 GUEST_MEMFD_FLAG_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
@@ -48,6 +48,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 created with GUEST_MEMFD_FLAG_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: %zu" , 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.50.1
PATCH v12 12/16 KVM: selftests: set KVM_MEM_GUEST_MEMFD in vm_mem_add() if guest_memfd != -1
In reply to Kalyazin, NikitaFrom: Patrick Roy <[email protected]>
Have 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 GUEST_MEMFD_FLAG_MMAP) 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 | 24 +++++++++++++--------- 1 file changed, 14 insertions(+), 10 deletions(-)
tools/testing/selftests/kvm/lib/kvm_util.c +14 -10
@@ -1090,21 +1090,25 @@ 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 =kvm_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 =kvm_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.50.1
PATCH v12 13/16 KVM: selftests: Add guest_memfd based vm_mem_backing_src_types
In reply to Kalyazin, NikitaFrom: Patrick Roy <[email protected]>
Allow 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.
--- .../testing/selftests/kvm/include/kvm_util.h | 18 ++++++ .../testing/selftests/kvm/include/test_util.h | 7 +++ tools/testing/selftests/kvm/lib/kvm_util.c | 61 ++++++++++--------- tools/testing/selftests/kvm/lib/test_util.c | 8 +++ 4 files changed, 65 insertions(+), 29 deletions(-)
tools/testing/selftests/kvm/include/kvm_util.h +18 -0
@@ -664,6 +664,24 @@ static inline bool is_smt_on(void)void vm_create_irqchip (struct kvm_vm *vm); +static inline uint32_t backing_src_guest_memfd_flags (enum vm_mem_backing_src_type t) +{ +uint32_t flags =0 ; + +switch (t) { +case VM_MEM_SRC_GUEST_MEMFD_NO_DIRECT_MAP : + flags |=GUEST_MEMFD_FLAG_NO_DIRECT_MAP ; + fallthrough; +case VM_MEM_SRC_GUEST_MEMFD : + flags |=GUEST_MEMFD_FLAG_MMAP |GUEST_MEMFD_FLAG_INIT_SHARED ; +break ; +default : +break ; + } + +return flags; +} +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
@@ -152,6 +152,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, };
@@ -184,6 +186,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 +32 -29
81 lines not on this page · raw message
tools/testing/selftests/kvm/lib/test_util.c +8 -0
@@ -306,6 +306,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.50.1
Re: [PATCH v12 13/16] KVM: selftests: Add guest_memfd based vm_mem_backing_src_types
Quoted 114 lines · Nikita Kalyazin
From: Patrick Roy <[email protected]>
Allow selftests to configure their memslots such that
userspace_addris set to aMAP_SHAREDmapping of theguest_memfdthat's associated with the memslot. This setup is the configuration for non-CoCo VMs, where all guest memory is backed by aguest_memfdwhose 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 removedguest_memfd.Signed-off-by Patrick Roy <[email protected]>Signed-off-by Nikita Kalyazin <[email protected]>--- .../testing/selftests/kvm/include/kvm_util.h | 18 ++++++ .../testing/selftests/kvm/include/test_util.h | 7 +++ tools/testing/selftests/kvm/lib/kvm_util.c | 61 ++++++++++--------- tools/testing/selftests/kvm/lib/test_util.c | 8 +++ 4 files changed, 65 insertions(+), 29 deletions(-)tools/testing/selftests/kvm/include/kvm_util.h +18
-0@@ -664,6 +664,24 @@ static inline bool is_smt_on(void)void vm_create_irqchip (struct kvm_vm *vm); +static inline uint32_t backing_src_guest_memfd_flags (enum vm_mem_backing_src_type t) +{ +uint32_t flags =0 ; + +switch (t) { +case VM_MEM_SRC_GUEST_MEMFD_NO_DIRECT_MAP : + flags |=GUEST_MEMFD_FLAG_NO_DIRECT_MAP ; + fallthrough; +case VM_MEM_SRC_GUEST_MEMFD : + flags |=GUEST_MEMFD_FLAG_MMAP |GUEST_MEMFD_FLAG_INIT_SHARED ; +break ; +default : +break ; + } + +return flags; +} +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@@ -152,6 +152,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, };@@ -184,6 +186,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 +27
-0@@ -1046,6 +1046,33 @@ void vm_mem_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type, alignment =1 ;#endif +if (guest_memfd <0 ) { +if ((flags &KVM_MEM_GUEST_MEMFD ) ||backing_src_is_guest_memfd (src_type)) { +uint32_t guest_memfd_flags =backing_src_guest_memfd_flags (src_type); + +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 =kvm_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; + }else { + region->region.guest_memfd =-1 ; + } +/* * When using THP mmap is not guaranteed to returned a hugepage aligned * address so we have to pad the mmap. Padding is not needed for HugeTLB
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 1092
Quoted 7 lines
tools/testing/selftests/kvm/lib/kvm_util.c +2
-2@@ -1061,10 +1088,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;
This seems to cause a double-close in __vm_mem_region_delete() - it was fine when this patch was written but now we have kvm_free_fd() which crashes the test when this happens.
AFAICS it's easy to fix we just need to enlighten __vm_mem_region_delete() that they might be the same FD.
PATCH v12 14/16 KVM: selftests: cover GUEST_MEMFD_FLAG_NO_DIRECT_MAP in existing selftests
In reply to Kalyazin, NikitaFrom: Patrick Roy <[email protected]>
Extend mem conversion selftests to cover the scenario that the guest can fault in and write gmem-backed guest memory even if its direct map removed. Also cover the new flag in guest_memfd_test.c tests.
--- tools/testing/selftests/kvm/guest_memfd_test.c | 17 ++++++++++++++++- .../kvm/x86/private_mem_conversions_test.c | 7 ++++--- 2 files changed, 20 insertions(+), 4 deletions(-)
tools/testing/selftests/kvm/guest_memfd_test.c +16 -1
@@ -403,6 +403,17 @@ static void test_guest_memfd(unsigned long vm_type)__test_guest_memfd (vm,GUEST_MEMFD_FLAG_MMAP |GUEST_MEMFD_FLAG_INIT_SHARED ); +if (flags &GUEST_MEMFD_FLAG_NO_DIRECT_MAP ) { +__test_guest_memfd (vm,GUEST_MEMFD_FLAG_NO_DIRECT_MAP ); +if (flags &GUEST_MEMFD_FLAG_MMAP ) +__test_guest_memfd (vm,GUEST_MEMFD_FLAG_NO_DIRECT_MAP | +GUEST_MEMFD_FLAG_MMAP ); +if (flags &GUEST_MEMFD_FLAG_INIT_SHARED ) +__test_guest_memfd (vm,GUEST_MEMFD_FLAG_NO_DIRECT_MAP | +GUEST_MEMFD_FLAG_MMAP | +GUEST_MEMFD_FLAG_INIT_SHARED ); + } +kvm_vm_free (vm); }
@@ -445,10 +456,14 @@ static void test_guest_memfd_guest(void)TEST_ASSERT (vm_check_cap (vm,KVM_CAP_GUEST_MEMFD_FLAGS ) &GUEST_MEMFD_FLAG_INIT_SHARED ,"Default VM type should support INIT_SHARED, supported flags = 0x%x" ,vm_check_cap (vm,KVM_CAP_GUEST_MEMFD_FLAGS )); +TEST_ASSERT (vm_check_cap (vm,KVM_CAP_GUEST_MEMFD_FLAGS ) &GUEST_MEMFD_FLAG_NO_DIRECT_MAP , +"Default VM type should support NO_DIRECT_MAP, supported flags = 0x%x" , +vm_check_cap (vm,KVM_CAP_GUEST_MEMFD_FLAGS )); size = vm->page_size; fd =vm_create_guest_memfd (vm, size,GUEST_MEMFD_FLAG_MMAP | -GUEST_MEMFD_FLAG_INIT_SHARED ); +GUEST_MEMFD_FLAG_INIT_SHARED | +GUEST_MEMFD_FLAG_NO_DIRECT_MAP );vm_set_user_memory_region2 (vm, slot,KVM_MEM_GUEST_MEMFD , gpa, size,NULL , fd,0 ); mem =kvm_mmap (size,PROT_READ |PROT_WRITE ,MAP_SHARED , fd);
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,
@@ -474,7 +474,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,GUEST_MEMFD_FLAG_NO_DIRECT_MAP );return 0 ; }
Signature · 1 line
2.50.1
PATCH v12 15/16 KVM: selftests: stuff vm_mem_backing_src_type into vm_shape
In reply to Kalyazin, NikitaFrom: Patrick Roy <[email protected]>
Use 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
@@ -215,7 +215,7 @@ enum vm_guest_mode {struct vm_shape {uint32_t type;uint8_t mode; -uint8_t pad0; +uint8_t src_type;uint16_t pad1; };
@@ -223,14 +223,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; \ })extern enum vm_guest_mode vm_mode_default;
tools/testing/selftests/kvm/lib/kvm_util.c +1 -1
@@ -500,7 +500,7 @@ struct kvm_vm *__vm_create(struct vm_shape shape, uint32_t nr_runnable_vcpus,if (is_guest_memfd_required (shape)) flags |=KVM_MEM_GUEST_MEMFD ; -vm_userspace_mem_region_add (vm,VM_MEM_SRC_ANONYMOUS ,0 ,0 , nr_pages, flags); +vm_userspace_mem_region_add (vm, shape.src_type,0 ,0 , nr_pages, flags);for (i =0 ; i <NR_MEM_REGIONS ; i++) vm->memslots[i] =0 ;
tools/testing/selftests/kvm/lib/x86/sev.c +1 -0
@@ -164,6 +164,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
@@ -165,6 +165,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.50.1
PATCH v12 16/16 KVM: selftests: Test guest execution from direct map removed gmem
In reply to Kalyazin, NikitaFrom: Patrick Roy <[email protected]>
Add a selftest that loads itself into guest_memfd (via GUEST_MEMFD_FLAG_MMAP) 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 | 52 +++++++++++++++++-- 1 file changed, 48 insertions(+), 4 deletions(-)
tools/testing/selftests/kvm/set_memory_region_test.c +48 -4
@@ -602,6 +602,41 @@ 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 , + .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[])
@@ -625,10 +660,19 @@ int main(int argc, char *argv[])test_add_max_memory_regions ();#ifdef __x86_64__ -if (kvm_has_cap (KVM_CAP_GUEST_MEMFD ) && - (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_GUEST_MEMFD )) { +uint64_t valid_flags =kvm_check_cap (KVM_CAP_GUEST_MEMFD_FLAGS ); + +if (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 ((valid_flags &GUEST_MEMFD_FLAG_MMAP ) && + (valid_flags &GUEST_MEMFD_FLAG_NO_DIRECT_MAP )) +test_guest_memfd_mmio (); +else +pr_info ("Skipping tests requiring GUEST_MEMFD_FLAG_MMAP | GUEST_MEMFD_FLAG_NO_DIRECT_MAP" ); }else {pr_info ("Skipping tests for KVM_MEM_GUEST_MEMFD memory regions\n" ); }
Signature · 1 line
2.50.1
Lorenzo Stoakes
maintainerQuoted 3 lines · Nikita
From: Nikita Kalyazin <[email protected]>
[ based on kvm/next ]
Hm, given this touches a fair bit of mm, I wonder if we shouldn't try to do this through the mm tree?
In any case, we definitely need a rebase on something not-next :) if not mm then Linus's tree at least maybe?
I'm seeing a lot of conflicts against mm-unstable, it can't b4 shazam even patch 1 and in Linus's tree it's failing at an mm patch (mm: introduce AS_NO_DIRECT_MAP).
That and 'KVM: selftests: Add guest_memfd based vm_mem_backing_src_types' won't apply, but that one's trivial at least.
Obviously we're in the merge window right now so maybe best to do any rebasing a little after 7.1-rc1?
Cheers, Lorenzo
Quoted 122 lines
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_memfdwith 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 insideguest_memfd.Additionally, a Firecracker branch with support for these VMs can be found on GitHub [2].
For more details, please refer to the v5 cover letter. No substantial changes in design have taken place since.
See also related
write()syscall support inguest_memfd[3] where the interoperation between the two features is described.Changes since v11:
- Ackerley/Sashiko: fix previously missed __set_pages_* argument update in__kernel_map_pages(patch 1)
- David: disallow large folios infolio_zap_direct_map(patch 2)
- David/Sashiko: check forfolio_is_zone_deviceif mapping is NULL ingup_fast_folio_allowed(patch 4)
- Ackerley/Sashiko:kvm_arch_gmem_supports_no_direct_mapto return false for SEV-SNP (patch 8).
- David: replace a redundant check forGUEST_MEMFD_FLAG_NO_DIRECT_MAPwith aWARN_ON_ONCE(patch 10)
- David: assert the folio is locked when zapping direct map (patch 10)
- Ackerley/Sashiko: reorder operations to "zap then prepare" and "invalidate then restore" (patch 10)v11: https://lore.kernel.org/kvm/[email protected]
v10: https://lore.kernel.org/kvm/[email protected]
v9: https://lore.kernel.org/kvm/[email protected]
v8: https://lore.kernel.org/kvm/[email protected]
v7: https://lore.kernel.org/kvm/[email protected]
v6: https://lore.kernel.org/kvm/[email protected]
v5: https://lore.kernel.org/kvm/[email protected]
v4: https://lore.kernel.org/kvm/[email protected]
RFCv3: https://lore.kernel.org/kvm/[email protected]
RFCv2: https://lore.kernel.org/kvm/[email protected]
RFCv1: https://lore.kernel.org/kvm/[email protected][1] https://download.vusec.net/papers/quarantine_raid23.pdf
[2] https://github.com/firecracker-microvm/firecracker/tree/feature/secret-hiding
[3] https://lore.kernel.org/kvm/[email protected]Nikita Kalyazin (4):
set_memory: set_direct_map_* to take address
set_memory: add folio_{zap,restore}_direct_map helpers mm/secretmem: make use of folio_{zap,restore}_direct_map mm/gup: drop local variable ingup_fast_folio_allowedPatrick Roy (12):
mm/gup: drop secretmem optimization fromgup_fast_folio_allowed
mm: introduceAS_NO_DIRECT_MAP
KVM:guest_memfd: Add stub forkvm_arch_gmem_invalidate
KVM: x86: definekvm_arch_gmem_supports_no_direct_map()
KVM: arm64: definekvm_arch_gmem_supports_no_direct_map()
KVM:guest_memfd: Add flag to remove from direct map
KVM: selftests: load elf via bounce buffer
KVM: selftests: setKVM_MEM_GUEST_MEMFDinvm_mem_add()ifguest_memfd!= -1KVM: selftests: Add
guest_memfdbasedvm_mem_backing_src_types
KVM: selftests: coverGUEST_MEMFD_FLAG_NO_DIRECT_MAPin existing
selftests
KVM: selftests: stuffvm_mem_backing_src_typeintovm_shape
KVM: selftests: Test guest execution from direct map removed gmemDocumentation/virt/kvm/api.rst | 21 +++--- arch/arm64/include/asm/kvm_host.h | 13 ++++ arch/arm64/include/asm/set_memory.h | 7 +- arch/arm64/mm/pageattr.c | 19 +++-- arch/loongarch/include/asm/set_memory.h | 7 +- arch/loongarch/mm/pageattr.c | 25 +++---- arch/riscv/include/asm/set_memory.h | 7 +- arch/riscv/mm/pageattr.c | 17 +++-- arch/s390/include/asm/set_memory.h | 7 +- arch/s390/mm/pageattr.c | 13 ++-- arch/x86/include/asm/kvm_host.h | 6 ++ arch/x86/include/asm/set_memory.h | 7 +- arch/x86/kvm/x86.c | 7 ++ arch/x86/mm/pat/set_memory.c | 27 +++---- include/linux/kvm_host.h | 14 ++++ include/linux/pagemap.h | 16 ++++ include/linux/secretmem.h | 18 ----- include/linux/set_memory.h | 22 +++++- include/uapi/linux/kvm.h | 1 + kernel/power/snapshot.c | 4 +- lib/buildid.c | 8 +- mm/execmem.c | 6 +- mm/gup.c | 47 ++++++------ mm/memory.c | 45 +++++++++++ mm/mlock.c | 2 +- mm/secretmem.c | 18 ++--- mm/vmalloc.c | 11 ++- .../testing/selftests/kvm/guest_memfd_test.c | 17 ++++- .../testing/selftests/kvm/include/kvm_util.h | 37 ++++++--- .../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 | 59 ++++++++------- 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 | 52 ++++++++++++- .../kvm/x86/private_mem_conversions_test.c | 7 +- virt/kvm/guest_memfd.c | 75 +++++++++++++++++-- 39 files changed, 489 insertions(+), 202 deletions(-)base-commit:
24f9515de8778410e4b84c85b196c9850d2c1e18--
2.50.1
Sean Christopherson
maintainerQuoted 7 lines · Lorenzo Stoakes
Quoted 3 lines · Nikita
From: Nikita Kalyazin <[email protected]>
[ based on kvm/next ]
Hm, given this touches a fair bit of mm, I wonder if we shouldn't try to do this through the mm tree?
Yeah, when the time comes, the mm pieces definitely need to go through the mm tree. Ideally, I think this would be merged in two separate parts, with all mm changes going through the mm tree, and then the KVM changes through the KVM tree using a stable topic branch/tag from Andrew.
Hi Lorenzo and Sean,
Apologies for the delayed reply — Nikita is leaving Amazon, and I'm taking over this series going forward. Thanks for your patience.
Quoted 2 lines · Lorenzo Stoakes
Hm, given this touches a fair bit of mm, I wonder if we shouldn't try to do this through the mm tree?
Quoted 4 lines · Sean Christopherson
Yeah, when the time comes, the mm pieces definitely need to go through the mm tree. Ideally, I think this would be merged in two separate parts, with all mm changes going through the mm tree, and then the KVM changes through the KVM tree using a stable topic branch/tag from Andrew.
Thanks for the guidance. The split makes sense to me; I'm planning to follow this approach with patches 1-6 (mm) going through the mm tree and patches 7-16 (KVM) through the KVM tree on top of a stable branch/tag from mm. I'll confirm the exact boundary and coordination details as I prepare the repost.
Quoted 6 lines · Lorenzo Stoakes
In any case, we definitely need a rebase on something not-next :) if not mm then Linus's tree at least maybe?
I'm seeing a lot of conflicts against mm-unstable, it can't b4 shazam even patch 1 and in Linus's tree it's failing at an mm patch (mm: introduce
AS_NO_DIRECT_MAP).
I'll rebase onto v7.1-rc1 and resolve the conflicts as part of the split.
Takahiro
Lorenzo Stoakes
maintainerQuoted 4 lines · Takahiro Itazuri
Hi Lorenzo and Sean,
Apologies for the delayed reply — Nikita is leaving Amazon, and I'm taking over this series going forward. Thanks for your patience.
Hey,
Thanks for letting us know! And thanks to Nikita for all his work, will keep an eye out for the respin!
Cheers, Lorenzo
Quoted 29 lines · Lorenzo Stoakes
Quoted 2 lines
Hm, given this touches a fair bit of mm, I wonder if we shouldn't try to do this through the mm tree?
Quoted 4 lines · Sean Christopherson
Yeah, when the time comes, the mm pieces definitely need to go through the mm tree. Ideally, I think this would be merged in two separate parts, with all mm changes going through the mm tree, and then the KVM changes through the KVM tree using a stable topic branch/tag from Andrew.
Thanks for the guidance. The split makes sense to me; I'm planning to follow this approach with patches 1-6 (mm) going through the mm tree and patches 7-16 (KVM) through the KVM tree on top of a stable branch/tag from mm. I'll confirm the exact boundary and coordination details as I prepare the repost.
Quoted 6 lines · Lorenzo Stoakes
In any case, we definitely need a rebase on something not-next :) if not mm then Linus's tree at least maybe?
I'm seeing a lot of conflicts against mm-unstable, it can't b4 shazam even patch 1 and in Linus's tree it's failing at an mm patch (mm: introduce
AS_NO_DIRECT_MAP).I'll rebase onto v7.1-rc1 and resolve the conflicts as part of the split.
Takahiro
Quoted 15 lines · Takahiro Itazuri
Hi Lorenzo and Sean,
Apologies for the delayed reply — Nikita is leaving Amazon, and I'm taking over this series going forward. Thanks for your patience.
Quoted 2 lines · Lorenzo Stoakes
Hm, given this touches a fair bit of mm, I wonder if we shouldn't try to do this through the mm tree?
Quoted 4 lines · Sean Christopherson
Yeah, when the time comes, the mm pieces definitely need to go through the mm tree. Ideally, I think this would be merged in two separate parts, with all mm changes going through the mm tree, and then the KVM changes through the KVM tree using a stable topic branch/tag from Andrew.
Quoted 14 lines
Thanks for the guidance. The split makes sense to me; I'm planning to follow this approach with patches 1-6 (mm) going through the mm tree and patches 7-16 (KVM) through the KVM tree on top of a stable branch/tag from mm. I'll confirm the exact boundary and coordination details as I prepare the repost.
Quoted 6 lines · Lorenzo Stoakes
In any case, we definitely need a rebase on something not-next :) if not mm then Linus's tree at least maybe?
I'm seeing a lot of conflicts against mm-unstable, it can't b4 shazam even patch 1 and in Linus's tree it's failing at an mm patch (mm: introduce
AS_NO_DIRECT_MAP).
Just as an FYI, I am gonna look at trying to move this forward a bit while Takahiro ramps up on taking over (I spoke to him about this off list).
My ulterior motive is that this would give me an excuse to add ALLOC_UNMAPPED (formerly __GFP_UNMAPPED) and the mermap [0] which unblocks various security nonsense including ASI [1]. (But also, this feature is a good idea).
[0] https://lore.kernel.org/all/[email protected]/
[1] https://linuxasi.dev