arch, mm/execmem: resolve confusion about set_direct_map_valid_noflush()
Subsystems (13)
Mike Rapoport (Microsoft)
maintainerv3 cover arch, mm/execmem: resolve confusion about set_direct_map_valid_noflush()
Recent discussion about implementation of execmem's ROX caches on arm64 revealed a confusion about how set_direct_map_valid_noflush() implemented on different architectures.
On arm64 it sets or clears the PTE_VALID bit marking a PTE as present or not present.
On other architectures it's a range version of set_direct_map_invalid_noflush() and set_direct_map_default_noflush()
Unlike arm64::set_direct_map_valid_noflush(), set_direct_map_default_noflush() not only marks PTE as present, but also sets its default protection mode.
Other than that, initial design of execmem ROX caches didn't rely on restoration of large mappings that's now available on x86, but completely removed the memory allocated for the ROX cache from the direct map to ensure that large mappings are not split. This precluded usage of VM_FLUSH_RESET_PERMS for the ROX cache allocations and required execmem to implement manipulation of the direct map alias.
Current implementation of ROX caches does not remove the direct map alias but simply calls set_memory_rox() that updates the permissions in both vmalloc address space and the direct map and relies on collapse_large_pages() in x86 CPA to keep large mappings.
This allow using VM_FLUSH_RESET_PERMS for execmem ROX cache allocations with small adjustments to set_direct_map APIs and vmalloc::reset_perms()
behaviour: adding number of pages parameter to set_direct_map APIs and making resetting of the direct map permissions in vmalloc VMAP_HUGE friendly.
Implement these adjustments, make execmem always use VM_FLUSH_RESET_PERMS and revert set_direct_map_valid_noflush() changes.
This applies on the current mm-unstable with v2 reverted, the base-commit b4 lists is bogus.
[1] https://lore.kernel.org/all/[email protected]
---
This version does not address sashiko comments about the preexiting issues, it only changes how VM_FLUSH_RESET_PERMS is set for ROX cache areas.
The fixes for pre-exising issues sashiko found are coming soon :)
v3 changes:
* set VM_FLUSH_RESET_PERMS to allocated area just before flipping the permissions rather than at allocation time
v2: https://lore.kernel.org/all/[email protected]
* move setting of VM area order back to __vmalloc_area_node()
* add tags, thanks!
v1: https://patch.msgid.link/[email protected]
---
Mike Rapoport (Microsoft) (6):
set_memory: add number of pages parameter to set_direct_map APIs mm/vmalloc: set area's page_order after allocation succeeds mm/vmalloc: constify vm parameter of get_vm_area_page_order() mm/vmalloc: make set_area_direct_map HUGE_VMAP friendly mm/execmem: use VM_FLUSH_RESET_PERMS for ROX cache allocations Revert "arch: introduce set_direct_map_valid_noflush()"
arch/arm64/include/asm/set_memory.h | 5 ++--- arch/arm64/mm/pageattr.c | 24 ++++---------------- arch/loongarch/include/asm/set_memory.h | 5 ++--- arch/loongarch/mm/pageattr.c | 27 ++++------------------ arch/riscv/include/asm/set_memory.h | 5 ++--- arch/riscv/mm/pageattr.c | 23 ++++--------------- arch/s390/include/asm/set_memory.h | 5 ++--- arch/s390/mm/pageattr.c | 20 ++++------------- arch/x86/include/asm/set_memory.h | 5 ++--- arch/x86/mm/pat/set_memory.c | 16 ++++--------- include/linux/set_memory.h | 12 ++++------ kernel/power/snapshot.c | 4 ++-- mm/execmem.c | 40 ++++++--------------------------- mm/secretmem.c | 6 ++--- mm/vmalloc.c | 26 +++++++++++---------- mm/vmalloc.h | 2 +- 16 files changed, 61 insertions(+), 164 deletions(-)
---
base-commit: d7bc5ce72091180aef05d001dc05c90f6b14c18d
change-id: 20260816-execmem-set-vm-perms-v0-2-bae847a4f64f
--
Sincerely yours,
Mike.
Mike Rapoport (Microsoft)
maintainerPATCH v3 1/6 set_memory: add number of pages parameter to set_direct_map APIs
When set_direct_map APIs were introduced by the commit d253ca0c3865 ("x86/mm/cpa: Add set_direct_map_*() functions") the single page parameter made sense because the initial callers (vmalloc and hibernation) had sets of unsorted struct pages that required changes of their mappings in the direct map.
Since there is an increasing demand for direct map manipulation and it is also desirable to be able to update larger physically contiguous mappings, for example an entire large folio, extend set_direct_map APIs to receive number of pages parameter.
As there is still only a handful of callers, change the existing functions directly and update all the call sites rather than adding wrappers for single page case.
--- arch/arm64/include/asm/set_memory.h | 4 ++-- arch/arm64/mm/pageattr.c | 8 ++++---- arch/loongarch/include/asm/set_memory.h | 4 ++-- arch/loongarch/mm/pageattr.c | 8 ++++---- arch/riscv/include/asm/set_memory.h | 4 ++-- arch/riscv/mm/pageattr.c | 8 ++++---- arch/s390/include/asm/set_memory.h | 4 ++-- arch/s390/mm/pageattr.c | 8 ++++---- arch/x86/include/asm/set_memory.h | 4 ++-- arch/x86/mm/pat/set_memory.c | 8 ++++---- include/linux/set_memory.h | 6 ++++-- kernel/power/snapshot.c | 4 ++-- mm/secretmem.c | 6 +++--- mm/vmalloc.c | 5 +++-- 14 files changed, 42 insertions(+), 39 deletions(-)
arch/arm64/include/asm/set_memory.h +2 -2
@@ -11,8 +11,8 @@ 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_invalid_noflush (struct page *page,unsigned int numpages); +int set_direct_map_default_noflush (struct page *page,unsigned int numpages);int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid);bool kernel_page_present (struct page *page);
arch/arm64/mm/pageattr.c +4 -4
@@ -251,7 +251,7 @@ int set_memory_valid(unsigned long addr, int numpages, int enable) __pgprot(PTE_PRESENT_VALID_KERNEL)); } -int set_direct_map_invalid_noflush (struct page *page) +int set_direct_map_invalid_noflush (struct page *page,unsigned int numpages) {pgprot_t clear_mask =__pgprot (PTE_PRESENT_VALID_KERNEL );pgprot_t set_mask =__pgprot (PTE_PRESENT_INVALID );
@@ -260,10 +260,10 @@ int set_direct_map_invalid_noflush(struct page *page)return 0 ;return update_range_prot ((unsigned long )page_address (page), -PAGE_SIZE , set_mask, clear_mask); +PAGE_SIZE * numpages, set_mask, clear_mask); } -int set_direct_map_default_noflush (struct page *page) +int set_direct_map_default_noflush (struct page *page,unsigned int numpages) {pgprot_t set_mask =__pgprot (PTE_PRESENT_VALID_KERNEL |PTE_WRITE );pgprot_t clear_mask =__pgprot (PTE_PRESENT_INVALID |PTE_RDONLY );
@@ -272,7 +272,7 @@ int set_direct_map_default_noflush(struct page *page)return 0 ;return update_range_prot ((unsigned long )page_address (page), -PAGE_SIZE , set_mask, clear_mask); +PAGE_SIZE * numpages, set_mask, clear_mask); } static int __set_memory_enc_dec(unsigned long addr,
arch/loongarch/include/asm/set_memory.h +2 -2
@@ -15,8 +15,8 @@ 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_default_noflush (struct page *page,unsigned int nr); +int set_direct_map_invalid_noflush (struct page *page,unsigned int nr);int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid);#endif /* _ASM_LOONGARCH_SET_MEMORY_H */
arch/loongarch/mm/pageattr.c +4 -4
@@ -198,24 +198,24 @@ 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 (struct page *page,unsigned int nr) {unsigned long addr = (unsigned long )page_address (page);if (addr < vm_map_base)return 0 ; -return __set_memory (addr,1 ,PAGE_KERNEL ,__pgprot (0 )); +return __set_memory (addr, nr,PAGE_KERNEL ,__pgprot (0 )); } -int set_direct_map_invalid_noflush (struct page *page) +int set_direct_map_invalid_noflush (struct page *page,unsigned int nr) {unsigned long addr = (unsigned long )page_address (page);if (addr < vm_map_base)return 0 ; -return __set_memory (addr,1 ,__pgprot (0 ),__pgprot (_PAGE_PRESENT | _PAGE_VALID)); +return __set_memory (addr, nr,__pgprot (0 ),__pgprot (_PAGE_PRESENT | _PAGE_VALID)); }int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid)
arch/riscv/include/asm/set_memory.h +2 -2
@@ -40,8 +40,8 @@ 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_invalid_noflush (struct page *page,unsigned int nr); +int set_direct_map_default_noflush (struct page *page,unsigned int nr);int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid);bool kernel_page_present (struct page *page);
arch/riscv/mm/pageattr.c +4 -4
@@ -374,15 +374,15 @@ 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 (struct page *page,unsigned int nr) { -return __set_memory ((unsigned long )page_address (page),1 , +return __set_memory ((unsigned long )page_address (page), nr,__pgprot (0 ),__pgprot (_PAGE_PRESENT)); } -int set_direct_map_default_noflush (struct page *page) +int set_direct_map_default_noflush (struct page *page,unsigned int nr) { -return __set_memory ((unsigned long )page_address (page),1 , +return __set_memory ((unsigned long )page_address (page), nr,PAGE_KERNEL ,__pgprot (_PAGE_EXEC)); }
arch/s390/include/asm/set_memory.h +2 -2
@@ -60,8 +60,8 @@ __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_invalid_noflush (struct page *page,unsigned int nr); +int set_direct_map_default_noflush (struct page *page,unsigned int nr);int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid);bool kernel_page_present (struct page *page);
arch/s390/mm/pageattr.c +4 -4
@@ -382,14 +382,14 @@ 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 (struct page *page,unsigned int nr) { -return __set_memory ((unsigned long )page_to_virt (page),1 ,SET_MEMORY_INV ); +return __set_memory ((unsigned long )page_to_virt (page), nr,SET_MEMORY_INV ); } -int set_direct_map_default_noflush (struct page *page) +int set_direct_map_default_noflush (struct page *page,unsigned int nr) { -return __set_memory ((unsigned long )page_to_virt (page),1 ,SET_MEMORY_DEF ); +return __set_memory ((unsigned long )page_to_virt (page), nr,SET_MEMORY_DEF ); }int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid)
arch/x86/include/asm/set_memory.h +2 -2
@@ -86,8 +86,8 @@ 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_invalid_noflush (struct page *page,unsigned int nr); +int set_direct_map_default_noflush (struct page *page,unsigned int nr);int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid);bool kernel_page_present (struct page *page);
arch/x86/mm/pat/set_memory.c +4 -4
@@ -2656,14 +2656,14 @@ static int __set_pages_np(struct page *page, int numpages, unsigned int cpa_flagreturn __change_page_attr_set_clr (&cpa,1 ); } -int set_direct_map_invalid_noflush (struct page *page) +int set_direct_map_invalid_noflush (struct page *page,unsigned int nr) { -return __set_pages_np (page,1 ,0 ); +return __set_pages_np (page, nr,0 ); } -int set_direct_map_default_noflush (struct page *page) +int set_direct_map_default_noflush (struct page *page,unsigned int nr) { -return __set_pages_p (page,1 ,0 ); +return __set_pages_p (page, nr,0 ); }int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid)
include/linux/set_memory.h +4 -2
@@ -25,11 +25,13 @@ 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 (struct page *page, +unsigned int nr) {return 0 ; } -static inline int set_direct_map_default_noflush (struct page *page) +static inline int set_direct_map_default_noflush (struct page *page, +unsigned int nr) {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,1 );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,1 );if (ret)pr_warn_once ("Failed to remap page\n" );
mm/secretmem.c +3 -3
@@ -139,7 +139,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_page (folio,0 ),1 );if (err) {secretmem_unaccount_folio (state, folio);folio_put (folio);
@@ -156,7 +156,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_page (folio,0 ),1 );folio_put (folio);if (err == -EEXIST ) goto retry;
@@ -228,7 +228,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_page (folio,0 ),1 );folio_zero_segment (folio,0 ,folio_size (folio)); }
mm/vmalloc.c +3 -2
@@ -3364,14 +3364,15 @@ 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)(struct page *page, +unsigned int nr)) {unsigned long 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]); +set_direct_map (area->pages[i],1 ); }/*
Signature · 1 line
2.53.0
Re: [PATCH v3 1/6] set_memory: add number of pages parameter to set_direct_map APIs
Quoted 11 lines · Mike Rapoport (Microsoft)
When
set_direct_mapAPIs were introduced by the commitd253ca0c3865("x86/mm/cpa: Add set_direct_map_*() functions") the single page parameter made sense because the initial callers (vmalloc and hibernation) had sets of unsorted struct pages that required changes of their mappings in the direct map.Since there is an increasing demand for direct map manipulation and it is also desirable to be able to update larger physically contiguous mappings, for example an entire large folio, extend
set_direct_mapAPIs to receive number of pages parameter.
Quoted 6 lines
As there is still only a handful of callers, change the existing functions directly and update all the call sites rather than adding wrappers for single page case.
Signed-off-by Mike Rapoport (Microsoft) <[email protected]>---
In general, LGTM.
But regarding semantics, is it well defined what happens when an update fails halfway through an operation?
I'd assume such a case cannot currently get triggered, but there is no documentation on what's supported and what's not. Or is there?
Imagine someone performing an update on an area that partially spans two PMDs. While splitting and updating the first PMD could succeed, splitting the second PMD could fail. What would be the end result? Rollback? Does the caller have to clean up?
I'd appreciate if we could add proper documentation with expected semantics.
Signature · 3 lines
Cheers, David
Mike Rapoport
maintainerRe: [PATCH v3 1/6] set_memory: add number of pages parameter to set_direct_map APIs
Quoted 16 lines · David Hildenbrand (Arm)
Quoted 15 lines · Mike Rapoport (Microsoft)
When
set_direct_mapAPIs were introduced by the commitd253ca0c3865("x86/mm/cpa: Add set_direct_map_*() functions") the single page parameter made sense because the initial callers (vmalloc and hibernation) had sets of unsorted struct pages that required changes of their mappings in the direct map.Since there is an increasing demand for direct map manipulation and it is also desirable to be able to update larger physically contiguous mappings, for example an entire large folio, extend
set_direct_mapAPIs to receive number of pages parameter.As there is still only a handful of callers, change the existing functions directly and update all the call sites rather than adding wrappers for single page case.
Quoted 6 lines
Quoted 2 lines
Signed-off-by Mike Rapoport (Microsoft) <[email protected]>---
In general, LGTM.
But regarding semantics, is it well defined what happens when an update fails halfway through an operation?
Just like other set_memory, it bails out in the middle if anything bad happens.
Quoted 7 lines
I'd assume such a case cannot currently get triggered, but there is no documentation on what's supported and what's not. Or is there?
Imagine someone performing an update on an area that partially spans two PMDs. While splitting and updating the first PMD could succeed, splitting the second PMD could fail. What would be the end result? Rollback? Does the caller have to clean up?
There is no rollback in any of set_memory implementations. It's up to the callers to cleanup the mess afterwards.
Quoted 1 line
I'd appreciate if we could add proper documentation with expected semantics.
I believe it's more in the scope of "arch, mm: create a generic set_memory/change_page_attr core" [1] work than this patchset.
I can write (or maybe even slop ;) ) a doc about set_memory/set_direct_map, but I wouldn't want to hold this because there's not set_memory documentation.
[1] https://lore.kernel.org/all/[email protected]
Quoted 4 lines
Signature · 3 lines
Cheers, David
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH v3 1/6] set_memory: add number of pages parameter to set_direct_map APIs
Quoted 10 lines
Quoted 7 lines
I'd assume such a case cannot currently get triggered, but there is no documentation on what's supported and what's not. Or is there?
Imagine someone performing an update on an area that partially spans two PMDs. While splitting and updating the first PMD could succeed, splitting the second PMD could fail. What would be the end result? Rollback? Does the caller have to clean up?
There is no rollback in any of
set_memoryimplementations. It's up to the callers to cleanup the mess afterwards.
Good to know, let's document that ;)
Quoted 7 lines
Quoted 2 lines
I'd appreciate if we could add proper documentation with expected semantics.
I believe it's more in the scope of "arch, mm: create a generic set_memory/change_page_attr core" [1] work than this patchset.
I can write (or maybe even slop ;) ) a doc about set_memory/set_direct_map,
I prefer well minimal and accurate docs ;) Who knows, maybe we'll even realize while writing the docs that some semantics are weird and should be changed (some architecture implementations adjusted to not be weird).
(I'm pretty sure we'll do, so schlop is not a good idea)
Quoted 2 lines
but I wouldn't want to hold this because there's not
set_memorydocumentation.
I'm fine with documenting it separately, but we *really* should start documenting the semantics as we touch+cleanup this mess.
Signature · 3 lines
Cheers, David
Re: [PATCH v3 1/6] set_memory: add number of pages parameter to set_direct_map APIs
In reply to Mike Rapoport (Microsoft)Quoted 11 lines · Mike Rapoport (Microsoft)
When
set_direct_mapAPIs were introduced by the commitd253ca0c3865("x86/mm/cpa: Add set_direct_map_*() functions") the single page parameter made sense because the initial callers (vmalloc and hibernation) had sets of unsorted struct pages that required changes of their mappings in the direct map.Since there is an increasing demand for direct map manipulation and it is also desirable to be able to update larger physically contiguous mappings, for example an entire large folio, extend
set_direct_mapAPIs to receive number of pages parameter.
Quoted 6 lines
As there is still only a handful of callers, change the existing functions directly and update all the call sites rather than adding wrappers for single page case.
Signed-off-by Mike Rapoport (Microsoft) <[email protected]>---
Signature · 3 lines
Cheers, David
Re: [PATCH v3 1/6] set_memory: add number of pages parameter to set_direct_map APIs
In reply to Mike Rapoport (Microsoft)Quoted 38 lines · Mike Rapoport (Microsoft)
When
set_direct_mapAPIs were introduced by the commitd253ca0c3865("x86/mm/cpa: Add set_direct_map_*() functions") the single page parameter made sense because the initial callers (vmalloc and hibernation) had sets of unsorted struct pages that required changes of their mappings in the direct map.Since there is an increasing demand for direct map manipulation and it is also desirable to be able to update larger physically contiguous mappings, for example an entire large folio, extend
set_direct_mapAPIs to receive number of pages parameter.As there is still only a handful of callers, change the existing functions directly and update all the call sites rather than adding wrappers for single page case.
Signed-off-by Mike Rapoport (Microsoft) <[email protected]>--- arch/arm64/include/asm/set_memory.h | 4 ++-- arch/arm64/mm/pageattr.c | 8 ++++---- arch/loongarch/include/asm/set_memory.h | 4 ++-- arch/loongarch/mm/pageattr.c | 8 ++++---- arch/riscv/include/asm/set_memory.h | 4 ++-- arch/riscv/mm/pageattr.c | 8 ++++---- arch/s390/include/asm/set_memory.h | 4 ++-- arch/s390/mm/pageattr.c | 8 ++++---- arch/x86/include/asm/set_memory.h | 4 ++-- arch/x86/mm/pat/set_memory.c | 8 ++++---- include/linux/set_memory.h | 6 ++++-- kernel/power/snapshot.c | 4 ++-- mm/secretmem.c | 6 +++--- mm/vmalloc.c | 5 +++-- 14 files changed, 42 insertions(+), 39 deletions(-)arch/arm64/include/asm/set_memory.h +0
-0
arch/arm64/include/asm/set_memory.h bool can_set_direct_map(void); · line 15
Quoted 7 lines
arch/arm64/include/asm/set_memory.h +2
-2@@ -11,8 +11,8 @@ 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_invalid_noflush (struct page *page,unsigned int numpages); +int set_direct_map_default_noflush (struct page *page,unsigned int numpages);
I don't have a strong opinion on whether to call the argument nr or numpages, but we might as well be consistent across archs :)
Aside from that:
Mike Rapoport
maintainerRe: [PATCH v3 1/6] set_memory: add number of pages parameter to set_direct_map APIs
Quoted 42 lines · Kevin Brodsky
Quoted 41 lines · Mike Rapoport (Microsoft)
When
set_direct_mapAPIs were introduced by the commitd253ca0c3865("x86/mm/cpa: Add set_direct_map_*() functions") the single page parameter made sense because the initial callers (vmalloc and hibernation) had sets of unsorted struct pages that required changes of their mappings in the direct map.Since there is an increasing demand for direct map manipulation and it is also desirable to be able to update larger physically contiguous mappings, for example an entire large folio, extend
set_direct_mapAPIs to receive number of pages parameter.As there is still only a handful of callers, change the existing functions directly and update all the call sites rather than adding wrappers for single page case.
Signed-off-by Mike Rapoport (Microsoft) <[email protected]>--- arch/arm64/include/asm/set_memory.h | 4 ++-- arch/arm64/mm/pageattr.c | 8 ++++---- arch/loongarch/include/asm/set_memory.h | 4 ++-- arch/loongarch/mm/pageattr.c | 8 ++++---- arch/riscv/include/asm/set_memory.h | 4 ++-- arch/riscv/mm/pageattr.c | 8 ++++---- arch/s390/include/asm/set_memory.h | 4 ++-- arch/s390/mm/pageattr.c | 8 ++++---- arch/x86/include/asm/set_memory.h | 4 ++-- arch/x86/mm/pat/set_memory.c | 8 ++++---- include/linux/set_memory.h | 6 ++++-- kernel/power/snapshot.c | 4 ++-- mm/secretmem.c | 6 +++--- mm/vmalloc.c | 5 +++-- 14 files changed, 42 insertions(+), 39 deletions(-)arch/arm64/include/asm/set_memory.h +0
-0@@ -11,8 +11,8 @@ bool can_set_direct_map(void);int set_memory_valid (unsigned long addr,int numpages,int enable);
Quoted 7 lines
arch/arm64/include/asm/set_memory.h bool can_set_direct_map(void); · line 15
Quoted 4 lines
arch/arm64/include/asm/set_memory.h +2
-2-int set_direct_map_invalid_noflush (struct page *page); -int set_direct_map_default_noflush (struct page *page); +int set_direct_map_invalid_noflush (struct page *page,unsigned int numpages); +int set_direct_map_default_noflush (struct page *page,unsigned int numpages);I don't have a strong opinion on whether to call the argument nr or numpages, but we might as well be consistent across archs :)
Well, it's either consistent across archs or consistent within each arch :) Or add a churny patch that renames it everywhere to nr_pages :)
Quoted 3 lines
Aside from that:
Reviewed-by Kevin Brodsky <[email protected]>
Thanks!
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH v3 1/6] set_memory: add number of pages parameter to set_direct_map APIs
Quoted 43 lines · Mike Rapoport
Quoted 42 lines · Kevin Brodsky
Quoted 41 lines · Mike Rapoport (Microsoft)
When
set_direct_mapAPIs were introduced by the commitd253ca0c3865("x86/mm/cpa: Add set_direct_map_*() functions") the single page parameter made sense because the initial callers (vmalloc and hibernation) had sets of unsorted struct pages that required changes of their mappings in the direct map.Since there is an increasing demand for direct map manipulation and it is also desirable to be able to update larger physically contiguous mappings, for example an entire large folio, extend
set_direct_mapAPIs to receive number of pages parameter.As there is still only a handful of callers, change the existing functions directly and update all the call sites rather than adding wrappers for single page case.
Signed-off-by Mike Rapoport (Microsoft) <[email protected]>--- arch/arm64/include/asm/set_memory.h | 4 ++-- arch/arm64/mm/pageattr.c | 8 ++++---- arch/loongarch/include/asm/set_memory.h | 4 ++-- arch/loongarch/mm/pageattr.c | 8 ++++---- arch/riscv/include/asm/set_memory.h | 4 ++-- arch/riscv/mm/pageattr.c | 8 ++++---- arch/s390/include/asm/set_memory.h | 4 ++-- arch/s390/mm/pageattr.c | 8 ++++---- arch/x86/include/asm/set_memory.h | 4 ++-- arch/x86/mm/pat/set_memory.c | 8 ++++---- include/linux/set_memory.h | 6 ++++-- kernel/power/snapshot.c | 4 ++-- mm/secretmem.c | 6 +++--- mm/vmalloc.c | 5 +++-- 14 files changed, 42 insertions(+), 39 deletions(-)arch/arm64/include/asm/set_memory.h +0
-0@@ -11,8 +11,8 @@ bool can_set_direct_map(void);int set_memory_valid (unsigned long addr,int numpages,int enable);
Quoted 8 lines
Quoted 6 lines
arch/arm64/include/asm/set_memory.h bool can_set_direct_map(void); · line 15
Quoted 4 lines
arch/arm64/include/asm/set_memory.h +2
-2-int set_direct_map_invalid_noflush (struct page *page); -int set_direct_map_default_noflush (struct page *page); +int set_direct_map_invalid_noflush (struct page *page,unsigned int numpages); +int set_direct_map_default_noflush (struct page *page,unsigned int numpages);I don't have a strong opinion on whether to call the argument nr or numpages, but we might as well be consistent across archs :)
Well, it's either consistent across archs or consistent within each arch :) Or add a churny patch that renames it everywhere to
nr_pages:)
Eh fair enough... Clearly not the biggest inconsistency in the set_direct_map API anyway!
- Kevin
Mike Rapoport (Microsoft)
maintainerPATCH v3 2/6 mm/vmalloc: set area's page_order after allocation succeeds
In reply to Mike Rapoport (Microsoft)__vmalloc_area_node() calls set_vm_area_page_order() to set area's page_order before actually allocating pages to populate the area.
If allocation of large pages in HUGE_VMAP case fails midway, this leaves the area with elevated page_order throughout the cleanup path.
There is no actual issue with this because the only place that currently relies on area->page_order on the cleanup path is the loop calculating the direct map alias range in vm_reset_perms() and it anyway skips unpopulated pages.
But having set_vm_area_page_order() in the middle of __vmalloc_area_node() makes things very obscure, hard to reason about and error prone against future changes of the cleanup path.
Move the call to set_vm_area_page_order() just before the successful return from __vmalloc_area_node() where page order is guaranteed.
While on it, initialize local page_order variable with its declaration.
--- mm/vmalloc.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
mm/vmalloc.c +2 -4
@@ -3879,7 +3879,7 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,unsigned long size =get_vm_area_size (area);unsigned long array_size;unsigned long nr_small_pages = size >>PAGE_SHIFT ; -unsigned int page_order; +unsigned int page_order = page_shift -PAGE_SHIFT ;unsigned int flags;int ret;
@@ -3907,9 +3907,6 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, goto fail; } -set_vm_area_page_order (area, page_shift -PAGE_SHIFT ); - page_order =vm_area_page_order (area); -/* * High-order nofail allocations are really expensive and * potentially dangerous (pre-mature OOM, disruptive reclaim
@@ -3964,6 +3961,7 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, goto fail; } +set_vm_area_page_order (area, page_order);return area->addr; fail:
Signature · 1 line
2.53.0
Re: [PATCH v3 2/6] mm/vmalloc: set area's page_order after allocation succeeds
Quoted 40 lines · Mike Rapoport (Microsoft)
__vmalloc_area_node()callsset_vm_area_page_order()to set area'spage_orderbefore actually allocating pages to populate the area.If allocation of large pages in
HUGE_VMAPcase fails midway, this leaves the area with elevatedpage_orderthroughout the cleanup path.There is no actual issue with this because the only place that currently relies on
area->page_orderon the cleanup path is the loop calculating the direct map alias range invm_reset_perms()and it anyway skips unpopulated pages.But having
set_vm_area_page_order()in the middle of__vmalloc_area_node()makes things very obscure, hard to reason about and error prone against future changes of the cleanup path.Move the call to
set_vm_area_page_order()just before the successful return from__vmalloc_area_node()where page order is guaranteed.While on it, initialize local
page_ordervariable with its declaration.Reviewed-by Uladzislau Rezki (Sony) <[email protected]>Signed-off-by Mike Rapoport (Microsoft) <[email protected]>--- mm/vmalloc.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)mm/vmalloc.c +1
-1@@ -3879,7 +3879,7 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,unsigned long size =get_vm_area_size (area);unsigned long array_size;unsigned long nr_small_pages = size >>PAGE_SHIFT ; -unsigned int page_order; +unsigned int page_order = page_shift -PAGE_SHIFT ;unsigned int flags;int ret;
mm/vmalloc.c static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, · line 3911
Quoted 5 lines
mm/vmalloc.c +0
-2@@ -3907,9 +3907,6 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, goto fail; } -set_vm_area_page_order (area, page_shift -PAGE_SHIFT ); - page_order =vm_area_page_order (area);
vm_area_page_order() returns zero in case !CONFIG_HAVE_ARCH_HUGE_VMALLOC. So it may happen that after this patch the page_shift passed to the page-allocator is > PAGE_SHIFT but that got rejected by vm_area_page_order.
But that won't happen because for page_shift > PAGE_SHIFT to be passed to __vmalloc_area_node(), we need "vmap_huge" to be true, which needs CONFIG_HAVE_ARCH_HUGE_VMALLOC.
Writing this out because CONFIG_HAVE_ARCH_HUGE_VMALLOC and CONFIG_HAVE_ARCH_HUGE_VMAP along with mixing variable names with vmap is extremely confusing.
Quoted 13 lines
mm/vmalloc.c +1
-1-/* * High-order nofail allocations are really expensive and * potentially dangerous (pre-mature OOM, disruptive reclaim @@ -3964,6 +3961,7 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, goto fail; } +set_vm_area_page_order (area, page_order);return area->addr; fail:
Mike Rapoport (Microsoft)
maintainerPATCH v3 3/6 mm/vmalloc: constify vm parameter of get_vm_area_page_order()
In reply to Mike Rapoport (Microsoft)get_vm_area_page_order() and vm_area_page_order() do not need to modify struct vm_struct passed to them.
Constify the parameter.
--- mm/vmalloc.c | 4 ++-- mm/vmalloc.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)
mm/vmalloc.c +2 -2
@@ -3130,7 +3130,7 @@ EXPORT_SYMBOL(vm_map_ram);static struct vm_struct *vmlist__initdata ; -static inline unsigned int vm_area_page_order (struct vm_struct *vm) +static inline unsigned int vm_area_page_order (const struct vm_struct *vm) {#ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC return vm->page_order;
@@ -3139,7 +3139,7 @@ static inline unsigned int vm_area_page_order(struct vm_struct *vm)#endif } -unsigned int get_vm_area_page_order (struct vm_struct *vm) +unsigned int get_vm_area_page_order (const struct vm_struct *vm) {return vm_area_page_order (vm); }
mm/vmalloc.h +1 -1
@@ -12,7 +12,7 @@ void __init vmalloc_init(void);int __must_check vmap_pages_range_noflush (unsigned long addr,unsigned long end,pgprot_t prot,struct page **pages,unsigned int page_shift,gfp_t gfp_mask); -unsigned int get_vm_area_page_order (struct vm_struct *vm); +unsigned int get_vm_area_page_order (const struct vm_struct *vm);#else static inline void vmalloc_init (void ) {}
Signature · 1 line
2.53.0
Re: [PATCH v3 3/6] mm/vmalloc: constify vm parameter of get_vm_area_page_order()
Quoted 8 lines · Mike Rapoport (Microsoft)
get_vm_area_page_order()andvm_area_page_order()do not need to modify structvm_structpassed to them.Constify the parameter.
Reviewed-by Uladzislau Rezki (Sony) <[email protected]>Signed-off-by Mike Rapoport (Microsoft) <[email protected]>---
Quoted 40 lines
mm/vmalloc.c | 4 ++-- mm/vmalloc.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)mm/vmalloc.c +2
-2@@ -3130,7 +3130,7 @@ EXPORT_SYMBOL(vm_map_ram);static struct vm_struct *vmlist__initdata ; -static inline unsigned int vm_area_page_order (struct vm_struct *vm) +static inline unsigned int vm_area_page_order (const struct vm_struct *vm) {#ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC return vm->page_order;@@ -3139,7 +3139,7 @@ static inline unsigned int vm_area_page_order(struct vm_struct *vm)#endif } -unsigned int get_vm_area_page_order (struct vm_struct *vm) +unsigned int get_vm_area_page_order (const struct vm_struct *vm) {return vm_area_page_order (vm); }mm/vmalloc.h +1
-1@@ -12,7 +12,7 @@ void __init vmalloc_init(void);int __must_check vmap_pages_range_noflush (unsigned long addr,unsigned long end,pgprot_t prot,struct page **pages,unsigned int page_shift,gfp_t gfp_mask); -unsigned int get_vm_area_page_order (struct vm_struct *vm); +unsigned int get_vm_area_page_order (const struct vm_struct *vm);#else static inline void vmalloc_init (void ) {}
Re: [PATCH v3 3/6] mm/vmalloc: constify vm parameter of get_vm_area_page_order()
In reply to Mike Rapoport (Microsoft)Quoted 8 lines · Mike Rapoport (Microsoft)
get_vm_area_page_order()andvm_area_page_order()do not need to modify structvm_structpassed to them.Constify the parameter.
Reviewed-by Uladzislau Rezki (Sony) <[email protected]>Signed-off-by Mike Rapoport (Microsoft) <[email protected]>---
Signature · 3 lines
Cheers, David
Re: [PATCH v3 3/6] mm/vmalloc: constify vm parameter of get_vm_area_page_order()
In reply to Mike Rapoport (Microsoft)Quoted 7 lines · Mike Rapoport (Microsoft)
get_vm_area_page_order()andvm_area_page_order()do not need to modify structvm_structpassed to them.Constify the parameter.
Reviewed-by Uladzislau Rezki (Sony) <[email protected]>Signed-off-by Mike Rapoport (Microsoft) <[email protected]>
Mike Rapoport (Microsoft)
maintainerPATCH v3 4/6 mm/vmalloc: make set_area_direct_map HUGE_VMAP friendly
In reply to Mike Rapoport (Microsoft)set_area_direct_map() always updates direct map alias permissions in single page increments.
For HUGE_VMAP areas it's suboptimal. Not only the loop in set_area_direct_map() needlessly has more iterations (e.g times 512 on x86), but it also causes fragmentation of the direct map that could be avoided for the HUGE_VMAP areas populated with large pages.
All pages in an area are always of the same order: either same-order large pages when VM_ALLOW_HUGE_VMAP is set and all huge pages were successfully allocated, or order-0 page when VM_ALLOW_HUGE_VMAP is cleared or when huge pages allocation fails and fallback path is taken.
Instead of updating the direct map permissions for every order-0 page in an area, use the area's page_order as the loop increment and update the large pages in one call to set_direct_map_{invalid,default}_noflush().
--- mm/vmalloc.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
mm/vmalloc.c +8 -5
@@ -3367,12 +3367,15 @@ static inline void set_area_direct_map(const struct vm_struct *area,int (*set_direct_map)(struct page *page, unsigned int nr)) { -unsigned long i; +unsigned int nr = (1U <<vm_area_page_order (area)); + +for (unsigned long i =0 ; i < area->nr_pages; i += nr) { +if (page_address (area->pages[i])) { +int err =set_direct_map (area->pages[i], nr); -/* 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],1 ); +WARN_ON_ONCE (err); + } + } }/*
Signature · 1 line
2.53.0
Re: [PATCH v3 4/6] mm/vmalloc: make set_area_direct_map HUGE_VMAP friendly
Quoted 7 lines · Mike Rapoport (Microsoft)
set_area_direct_map()always updates direct map alias permissions in single page increments.For
HUGE_VMAPareas it's suboptimal. Not only the loop inset_area_direct_map()needlessly has more iterations (e.g times 512 on x86), but it also causes fragmentation of the direct map that could be avoided for theHUGE_VMAPareas populated with large pages.
Correct, this would also help us on arm64 once we have ROX caches.
Quoted 6 lines
All pages in an area are always of the same order: either same-order large pages when
VM_ALLOW_HUGE_VMAPis set and all huge pages were successfully allocated, or order-0 page whenVM_ALLOW_HUGE_VMAPis cleared or when huge pages allocation fails and fallback path is taken.
Quoted 6 lines
Instead of updating the direct map permissions for every order-0 page in an area, use the area's
page_orderas the loop increment and update the large pages in one call to set_direct_map_{invalid,default}_noflush().Signed-off-by Mike Rapoport (Microsoft) <[email protected]>---
LGTM:
Quoted 29 lines
mm/vmalloc.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)mm/vmalloc.c +8
-5@@ -3367,12 +3367,15 @@ static inline void set_area_direct_map(const struct vm_struct *area,int (*set_direct_map)(struct page *page, unsigned int nr)) { -unsigned long i; +unsigned int nr = (1U <<vm_area_page_order (area)); + +for (unsigned long i =0 ; i < area->nr_pages; i += nr) { +if (page_address (area->pages[i])) { +int err =set_direct_map (area->pages[i], nr); -/* 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],1 ); +WARN_ON_ONCE (err); + } + } }/*
Re: [PATCH v3 4/6] mm/vmalloc: make set_area_direct_map HUGE_VMAP friendly
In reply to Mike Rapoport (Microsoft)Quoted 27 lines · Mike Rapoport (Microsoft)
set_area_direct_map()always updates direct map alias permissions in single page increments.For
HUGE_VMAPareas it's suboptimal. Not only the loop inset_area_direct_map()needlessly has more iterations (e.g times 512 on x86), but it also causes fragmentation of the direct map that could be avoided for theHUGE_VMAPareas populated with large pages.All pages in an area are always of the same order: either same-order large pages when
VM_ALLOW_HUGE_VMAPis set and all huge pages were successfully allocated, or order-0 page whenVM_ALLOW_HUGE_VMAPis cleared or when huge pages allocation fails and fallback path is taken.Instead of updating the direct map permissions for every order-0 page in an area, use the area's
page_orderas the loop increment and update the large pages in one call to set_direct_map_{invalid,default}_noflush().Signed-off-by Mike Rapoport (Microsoft) <[email protected]>--- mm/vmalloc.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)mm/vmalloc.c +0
-0
mm/vmalloc.c static inline void set_area_direct_map(const struct vm_struct *area, · line 3374
Quoted 9 lines
mm/vmalloc.c +5
-1@@ -3367,12 +3367,15 @@ static inline void set_area_direct_map(const struct vm_struct *area,int (*set_direct_map)(struct page *page, unsigned int nr)) { -unsigned long i; +unsigned int nr = (1U <<vm_area_page_order (area)); + +for (unsigned long i =0 ; i < area->nr_pages; i += nr) { +if (page_address (area->pages[i])) { +int err =set_direct_map (area->pages[i], nr);
Do we really expect some pages to have a directmap and others not?
Signature · 3 lines
Cheers, David
Mike Rapoport
maintainerRe: [PATCH v3 4/6] mm/vmalloc: make set_area_direct_map HUGE_VMAP friendly
Quoted 23 lines · David Hildenbrand (Arm)
Quoted 22 lines · Mike Rapoport (Microsoft)
set_area_direct_map()always updates direct map alias permissions in single page increments.For
HUGE_VMAPareas it's suboptimal. Not only the loop inset_area_direct_map()needlessly has more iterations (e.g times 512 on x86), but it also causes fragmentation of the direct map that could be avoided for theHUGE_VMAPareas populated with large pages.All pages in an area are always of the same order: either same-order large pages when
VM_ALLOW_HUGE_VMAPis set and all huge pages were successfully allocated, or order-0 page whenVM_ALLOW_HUGE_VMAPis cleared or when huge pages allocation fails and fallback path is taken.Instead of updating the direct map permissions for every order-0 page in an area, use the area's
page_orderas the loop increment and update the large pages in one call to set_direct_map_{invalid,default}_noflush().Signed-off-by Mike Rapoport (Microsoft) <[email protected]>--- mm/vmalloc.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
Quoted 16 lines
Quoted 5 lines
mm/vmalloc.c +0
-0mm/vmalloc.c static inline void set_area_direct_map(const struct vm_struct *area, · line 3374
Quoted 9 lines
mm/vmalloc.c +5
-1@@ -3367,12 +3367,15 @@ static inline void set_area_direct_map(const struct vm_struct *area,int (*set_direct_map)(struct page *page, unsigned int nr)) { -unsigned long i; +unsigned int nr = (1U <<vm_area_page_order (area)); + +for (unsigned long i =0 ; i < area->nr_pages; i += nr) { +if (page_address (area->pages[i])) { +int err =set_direct_map (area->pages[i], nr);Do we really expect some pages to have a directmap and others not?
Every page in the area can be allocated separately and __vmalloc_area_node() has
if (!(gfp_mask & (GFP_DMA | GFP_DMA32))) gfp_mask |= __GFP_HIGHMEM;
so generally speaking yes :)
Quoted 4 lines
Signature · 3 lines
Cheers, David
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH v3 4/6] mm/vmalloc: make set_area_direct_map HUGE_VMAP friendly
Quoted 41 lines · Mike Rapoport
Quoted 23 lines · David Hildenbrand (Arm)
Quoted 22 lines · Mike Rapoport (Microsoft)
set_area_direct_map()always updates direct map alias permissions in single page increments.For
HUGE_VMAPareas it's suboptimal. Not only the loop inset_area_direct_map()needlessly has more iterations (e.g times 512 on x86), but it also causes fragmentation of the direct map that could be avoided for theHUGE_VMAPareas populated with large pages.All pages in an area are always of the same order: either same-order large pages when
VM_ALLOW_HUGE_VMAPis set and all huge pages were successfully allocated, or order-0 page whenVM_ALLOW_HUGE_VMAPis cleared or when huge pages allocation fails and fallback path is taken.Instead of updating the direct map permissions for every order-0 page in an area, use the area's
page_orderas the loop increment and update the large pages in one call to set_direct_map_{invalid,default}_noflush().Signed-off-by Mike Rapoport (Microsoft) <[email protected]>--- mm/vmalloc.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)Quoted 16 lines
Quoted 5 lines
mm/vmalloc.c +0
-0mm/vmalloc.c static inline void set_area_direct_map(const struct vm_struct *area, · line 3374
Quoted 9 lines
mm/vmalloc.c +5
-1@@ -3367,12 +3367,15 @@ static inline void set_area_direct_map(const struct vm_struct *area,int (*set_direct_map)(struct page *page, unsigned int nr)) { -unsigned long i; +unsigned int nr = (1U <<vm_area_page_order (area)); + +for (unsigned long i =0 ; i < area->nr_pages; i += nr) { +if (page_address (area->pages[i])) { +int err =set_direct_map (area->pages[i], nr);Do we really expect some pages to have a directmap and others not?
Quoted 7 lines
Every page in the area can be allocated separately and
__vmalloc_area_node()hasif (!(gfp_mask & (GFP_DMA | GFP_DMA32))) gfp_mask |= __GFP_HIGHMEM;so generally speaking yes :)
Ah, I was thinking in terms of HUGE_VMAP, because there we sure would expect all-or-nothing, right?
Signature · 3 lines
Cheers, David
Mike Rapoport
maintainerRe: [PATCH v3 4/6] mm/vmalloc: make set_area_direct_map HUGE_VMAP friendly
Quoted 45 lines · David Hildenbrand (Arm)
Quoted 44 lines · Mike Rapoport
Quoted 23 lines · David Hildenbrand (Arm)
Quoted 22 lines · Mike Rapoport (Microsoft)
set_area_direct_map()always updates direct map alias permissions in single page increments.For
HUGE_VMAPareas it's suboptimal. Not only the loop inset_area_direct_map()needlessly has more iterations (e.g times 512 on x86), but it also causes fragmentation of the direct map that could be avoided for theHUGE_VMAPareas populated with large pages.All pages in an area are always of the same order: either same-order large pages when
VM_ALLOW_HUGE_VMAPis set and all huge pages were successfully allocated, or order-0 page whenVM_ALLOW_HUGE_VMAPis cleared or when huge pages allocation fails and fallback path is taken.Instead of updating the direct map permissions for every order-0 page in an area, use the area's
page_orderas the loop increment and update the large pages in one call to set_direct_map_{invalid,default}_noflush().Signed-off-by Mike Rapoport (Microsoft) <[email protected]>--- mm/vmalloc.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)Quoted 16 lines
Quoted 5 lines
mm/vmalloc.c +0
-0mm/vmalloc.c static inline void set_area_direct_map(const struct vm_struct *area, · line 3374
Quoted 9 lines
mm/vmalloc.c +5
-1@@ -3367,12 +3367,15 @@ static inline void set_area_direct_map(const struct vm_struct *area,int (*set_direct_map)(struct page *page, unsigned int nr)) { -unsigned long i; +unsigned int nr = (1U <<vm_area_page_order (area)); + +for (unsigned long i =0 ; i < area->nr_pages; i += nr) { +if (page_address (area->pages[i])) { +int err =set_direct_map (area->pages[i], nr);Do we really expect some pages to have a directmap and others not?
Every page in the area can be allocated separately and
__vmalloc_area_node()has
Quoted 7 lines
Quoted 4 lines
if (!(gfp_mask & (GFP_DMA | GFP_DMA32))) gfp_mask |= __GFP_HIGHMEM;so generally speaking yes :)
Ah, I was thinking in terms of
HUGE_VMAP, because there we sure would expect all-or-nothing, right?
Even with VM_ALLOW_HUGE_VMAP an area may be backed by highmem pages. When huge allocations are disabled either because an architecture does not support them or nohugevmalloc=no, vmalloc(VM_ALLOW_HUGE_VMAP) silently falls back to order-0 allocations.
Quoted 4 lines
Signature · 3 lines
Cheers, David
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH v3 4/6] mm/vmalloc: make set_area_direct_map HUGE_VMAP friendly
In reply to Mike Rapoport (Microsoft)Quoted 13 lines · Mike Rapoport (Microsoft)
set_area_direct_map()always updates direct map alias permissions in single page increments.For
HUGE_VMAPareas it's suboptimal. Not only the loop inset_area_direct_map()needlessly has more iterations (e.g times 512 on x86), but it also causes fragmentation of the direct map that could be avoided for theHUGE_VMAPareas populated with large pages.All pages in an area are always of the same order: either same-order large pages when
VM_ALLOW_HUGE_VMAPis set and all huge pages were successfully allocated, or order-0 page whenVM_ALLOW_HUGE_VMAPis cleared or when huge pages allocation fails and fallback path is taken.
Quoted 5 lines
Instead of updating the direct map permissions for every order-0 page in an area, use the area's
page_orderas the loop increment and update the large pages in one call to set_direct_map_{invalid,default}_noflush().Signed-off-by Mike Rapoport (Microsoft) <[email protected]>
Quoted 30 lines
--- mm/vmalloc.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)mm/vmalloc.c +8
-5@@ -3367,12 +3367,15 @@ static inline void set_area_direct_map(const struct vm_struct *area,int (*set_direct_map)(struct page *page, unsigned int nr)) { -unsigned long i; +unsigned int nr = (1U <<vm_area_page_order (area)); + +for (unsigned long i =0 ; i < area->nr_pages; i += nr) { +if (page_address (area->pages[i])) { +int err =set_direct_map (area->pages[i], nr); -/* 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],1 ); +WARN_ON_ONCE (err); + } + } }/*
Mike Rapoport (Microsoft)
maintainerPATCH v3 5/6 mm/execmem: use VM_FLUSH_RESET_PERMS for ROX cache allocations
In reply to Mike Rapoport (Microsoft)Initially execmem completely removed direct map alias for the memory allocated for the ROX cache in PMD_SIZE chunks. When that memory was freed, its direct map was restored also in PMD_SIZE chunks to avoid fragmentation of the direct map caused by vmalloc::vm_reset_perms().
This required execmem to implement the wrappers for set_direct_map APIs for proper sequencing of removal and restoration of the direct map aliases.
Since then x86's CPA gained support for collapsing the direct map page tables for ROX pages and execmem switched from removing ROX caches from the direct map to making them ROX there, so execmem only needs to update direct map alias permissions when freeing the ROX cache memory.
vmalloc already handles those updates for areas with VM_FLUSH_RESET_PERMS set and vmalloc::vm_reset_perms() does not force split of the direct map for PMD_SIZE chunks.
Set the area permissions with set_vm_flush_reset_perms() when populating the execmem cache just before flipping the area to ROX.
This way freeing an allocated area on an error path won't incur two updates of the direct map alias of that area and TLB flushing in vm_reset_perms().
--- mm/execmem.c | 40 +++++++--------------------------------- 1 file changed, 7 insertions(+), 33 deletions(-)
mm/execmem.c +7 -33
@@ -113,28 +113,6 @@ static inline unsigned long mas_range_len(struct ma_state *mas)return mas->last - mas->index +1 ; } -static int execmem_set_direct_map_valid (struct vm_struct *vm,bool valid) -{ -unsigned int nr = (1 <<get_vm_area_page_order (vm)); -unsigned int updated =0 ; -int err =0 ; - -for (int i =0 ; i < vm->nr_pages; i += nr) { - err =set_direct_map_valid_noflush (vm->pages[i], nr, valid); -if (err) - goto err_restore; - updated += nr; - } - -return 0 ; - -err_restore: -for (int i =0 ; i < updated; i += nr) -set_direct_map_valid_noflush (vm->pages[i], nr, !valid); - -return err; -} -static int execmem_force_rw (void *ptr,size_t size) {unsigned int nr =PAGE_ALIGN (size) >>PAGE_SHIFT ;
@@ -169,9 +147,6 @@ static void execmem_cache_clean(struct work_struct *work)if (IS_ALIGNED (size,PMD_SIZE ) &&IS_ALIGNED (mas.index,PMD_SIZE )) { -struct vm_struct *vm =find_vm_area (area); - -execmem_set_direct_map_valid (vm, true);mas_store_gfp (&mas,NULL ,GFP_KERNEL );vfree (area); }
@@ -301,6 +276,8 @@ static void *execmem_cache_populate_alloc(struct execmem_range *range, size_t si/* fill memory with instructions that will trap */ execmem_fill_trapping_insns (p, alloc_size); +set_vm_flush_reset_perms (p); + err =set_memory_rox ((unsigned long )p, vm->nr_pages);if (err) goto err_free_mem;
@@ -312,18 +289,15 @@ static void *execmem_cache_populate_alloc(struct execmem_range *range, size_t si*/ mutex_lock (mutex); err =execmem_cache_add_locked (p, alloc_size,GFP_KERNEL ); -if (err) - goto err_reset_direct_map; - - p =execmem_cache_alloc_locked (range, size); - +if (!err) + p =execmem_cache_alloc_locked (range, size);mutex_unlock (mutex); +if (err) + goto err_free_mem; +return p; -err_reset_direct_map: -mutex_unlock (mutex); -execmem_set_direct_map_valid (vm, true); err_free_mem:vfree (p);return NULL ;
Signature · 1 line
2.53.0
Re: [PATCH v3 5/6] mm/execmem: use VM_FLUSH_RESET_PERMS for ROX cache allocations
Quoted 17 lines · Mike Rapoport (Microsoft)
Initially execmem completely removed direct map alias for the memory allocated for the ROX cache in
PMD_SIZEchunks. When that memory was freed, its direct map was restored also inPMD_SIZEchunks to avoid fragmentation of the direct map caused byvmalloc::vm_reset_perms().This required execmem to implement the wrappers for
set_direct_mapAPIs for proper sequencing of removal and restoration of the direct map aliases.Since then x86's CPA gained support for collapsing the direct map page tables for ROX pages and execmem switched from removing ROX caches from the direct map to making them ROX there, so execmem only needs to update direct map alias permissions when freeing the ROX cache memory.
vmalloc already handles those updates for areas with
VM_FLUSH_RESET_PERMSset andvmalloc::vm_reset_perms()does not force split of the direct map forPMD_SIZEchunks.
Quoted 8 lines
Set the area permissions with
set_vm_flush_reset_perms()when populating the execmem cache just before flipping the area to ROX.This way freeing an allocated area on an error path won't incur two updates of the direct map alias of that area and TLB flushing in
vm_reset_perms().Signed-off-by Mike Rapoport (Microsoft) <[email protected]>
Mike Rapoport (Microsoft)
maintainerPATCH v3 6/6 Revert "arch: introduce set_direct_map_valid_noflush()"
In reply to Mike Rapoport (Microsoft)Commit 0c6378a71574 ("arch: introduce set_direct_map_valid_noflush()") added set_direct_map_valid_noflush() to allow updating the direct map for a physically contiguous range in execmem.
As Brendan recently pointed out [1], this API is confusing because on arm64 it means that is sets VALID bit in ptes, while on other architectures it is an analog of set_direct_map_default_noflush().
The only user of set_direct_map_valid_noflush() was execmem's ROX cache freeing path and it was switched to utilize VM_FLUSH_RESET_PERMS for resetting permissions of the direct map alias.
With the last user gone and with set_direct_map_{invalid,default}_noflush() accepting number of pages as a parameter, set_direct_map_valid_noflush() become a copy of set_memory_valid() on arm64 and a duplicate of set_direct_map_{invalid,default}_noflush() on other architecture, it is safe to remove set_direct_map_valid_noflush().
Also drop a stale comment in arm64::__kernel_map_pages() that Linus bothered to add when merging changes containing set_direct_map_valid_noflush() to his tree.
This reverts commit 0c6378a71574daa6cd1534ad42a956e3262756c7.
[1] https://lore.kernel.org/all/[email protected]
--- arch/arm64/include/asm/set_memory.h | 1 - arch/arm64/mm/pageattr.c | 16 ---------------- arch/loongarch/include/asm/set_memory.h | 1 - arch/loongarch/mm/pageattr.c | 19 ------------------- arch/riscv/include/asm/set_memory.h | 1 - arch/riscv/mm/pageattr.c | 15 --------------- arch/s390/include/asm/set_memory.h | 1 - arch/s390/mm/pageattr.c | 12 ------------ arch/x86/include/asm/set_memory.h | 1 - arch/x86/mm/pat/set_memory.c | 8 -------- include/linux/set_memory.h | 6 ------ 11 files changed, 81 deletions(-)
arch/arm64/include/asm/set_memory.h +0 -1
@@ -13,7 +13,6 @@ int set_memory_valid(unsigned long addr, int numpages, int enable);int set_direct_map_invalid_noflush (struct page *page,unsigned int numpages);int set_direct_map_default_noflush (struct page *page,unsigned int numpages); -int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid);bool kernel_page_present (struct page *page);int set_memory_encrypted (unsigned long addr,int numpages);
arch/arm64/mm/pageattr.c +0 -16
@@ -355,23 +355,7 @@ 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) -{ -unsigned long addr = (unsigned long )page_address (page); - -if (!can_set_direct_map ()) -return 0 ; - -return set_memory_valid (addr, nr, valid); -} -#ifdef CONFIG_DEBUG_PAGEALLOC -/* -* This is - apart from the return value - doing the same -* thing as the new set_direct_map_valid_noflush() function. -* -* Unify? Explain the conceptual differences? -*/ void __kernel_map_pages (struct page *page,int numpages,int enable) {if (!can_set_direct_map ())
arch/loongarch/include/asm/set_memory.h +0 -1
@@ -17,6 +17,5 @@ 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,unsigned int nr);int set_direct_map_invalid_noflush (struct page *page,unsigned int nr); -int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid);#endif /* _ASM_LOONGARCH_SET_MEMORY_H */
arch/loongarch/mm/pageattr.c +0 -19
@@ -217,22 +217,3 @@ int set_direct_map_invalid_noflush(struct page *page, unsigned int nr)return __set_memory (addr, nr,__pgprot (0 ),__pgprot (_PAGE_PRESENT | _PAGE_VALID)); } - -int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid) -{ -unsigned long addr = (unsigned long )page_address (page); -pgprot_t set, clear; - -if (addr < vm_map_base) -return 0 ; - -if (valid) { - set =PAGE_KERNEL ; - clear =__pgprot (0 ); - }else { - set =__pgprot (0 ); - clear =__pgprot (_PAGE_PRESENT | _PAGE_VALID); - } - -return __set_memory (addr, nr, set, clear); -}
arch/riscv/include/asm/set_memory.h +0 -1
@@ -42,7 +42,6 @@ static inline int set_kernel_memory(char *startp, char *endp,int set_direct_map_invalid_noflush (struct page *page,unsigned int nr);int set_direct_map_default_noflush (struct page *page,unsigned int nr); -int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid);bool kernel_page_present (struct page *page);#endif /* __ASSEMBLER__ */
arch/riscv/mm/pageattr.c +0 -15
@@ -386,21 +386,6 @@ int set_direct_map_default_noflush(struct page *page, unsigned int nr) PAGE_KERNEL, __pgprot(_PAGE_EXEC)); } -int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid) -{ -pgprot_t set, clear; - -if (valid) { - set =PAGE_KERNEL ; - clear =__pgprot (_PAGE_EXEC); - }else { - set =__pgprot (0 ); - clear =__pgprot (_PAGE_PRESENT); - } - -return __set_memory ((unsigned long )page_address (page), nr, set, clear); -} -#ifdef CONFIG_DEBUG_PAGEALLOC static int debug_pagealloc_set_page (pte_t *pte,unsigned long addr,void *data) {
arch/s390/include/asm/set_memory.h +0 -1
@@ -62,7 +62,6 @@ __SET_MEMORY_FUNC(set_memory_4k, SET_MEMORY_4K)int set_direct_map_invalid_noflush (struct page *page,unsigned int nr);int set_direct_map_default_noflush (struct page *page,unsigned int nr); -int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid);bool kernel_page_present (struct page *page);#endif
arch/s390/mm/pageattr.c +0 -12
@@ -392,18 +392,6 @@ int set_direct_map_default_noflush(struct page *page, unsigned int nr)return __set_memory ((unsigned long )page_to_virt (page), nr,SET_MEMORY_DEF ); } -int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid) -{ -unsigned long flags; - -if (valid) - flags =SET_MEMORY_DEF ; -else - flags =SET_MEMORY_INV ; - -return __set_memory ((unsigned long )page_to_virt (page), nr, flags); -} -bool kernel_page_present (struct page *page) {unsigned long addr;
arch/x86/include/asm/set_memory.h +0 -1
@@ -88,7 +88,6 @@ int set_pages_rw(struct page *page, int numpages);int set_direct_map_invalid_noflush (struct page *page,unsigned int nr);int set_direct_map_default_noflush (struct page *page,unsigned int nr); -int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid);bool kernel_page_present (struct page *page);extern int kernel_set_to_readonly;
arch/x86/mm/pat/set_memory.c +0 -8
@@ -2666,14 +2666,6 @@ int set_direct_map_default_noflush(struct page *page, unsigned int nr)return __set_pages_p (page, nr,0 ); } -int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid) -{ -if (valid) -return __set_pages_p (page, nr,0 ); - -return __set_pages_np (page, nr,0 ); -} -#ifdef CONFIG_DEBUG_PAGEALLOC void __kernel_map_pages (struct page *page,int numpages,int enable) {
include/linux/set_memory.h +0 -6
@@ -36,12 +36,6 @@ static inline int set_direct_map_default_noflush(struct page *page,return 0 ; } -static inline int set_direct_map_valid_noflush (struct page *page, -unsigned nr,bool valid) -{ -return 0 ; -} -static inline bool kernel_page_present (struct page *page) {return true;
Signature · 1 line
2.53.0
Re: [PATCH v3 6/6] Revert "arch: introduce set_direct_map_valid_noflush()"
Quoted 22 lines · Mike Rapoport (Microsoft)
Commit
0c6378a71574("arch: introduceset_direct_map_valid_noflush()") addedset_direct_map_valid_noflush()to allow updating the direct map for a physically contiguous range in execmem.As Brendan recently pointed out [1], this API is confusing because on arm64 it means that is sets VALID bit in ptes, while on other architectures it is an analog of
set_direct_map_default_noflush().The only user of
set_direct_map_valid_noflush()was execmem's ROX cache freeing path and it was switched to utilizeVM_FLUSH_RESET_PERMSfor resetting permissions of the direct map alias.With the last user gone and with set_direct_map_{invalid,default}_noflush() accepting number of pages as a parameter,
set_direct_map_valid_noflush()become a copy ofset_memory_valid()on arm64 and a duplicate of set_direct_map_{invalid,default}_noflush() on other architecture, it is safe to removeset_direct_map_valid_noflush().Also drop a stale comment in
arm64::__kernel_map_pages()that Linus bothered to add when merging changes containingset_direct_map_valid_noflush()to his tree.
Quoted 7 lines
This reverts commit
0c6378a71574daa6cd1534ad42a956e3262756c7.[1] https://lore.kernel.org/all/[email protected]
Reviewed-by Brendan Jackman <[email protected]>Signed-off-by Mike Rapoport (Microsoft) <[email protected]>---
Thanks!
Signature · 3 lines
Cheers, David
Re: [PATCH v3 6/6] Revert "arch: introduce set_direct_map_valid_noflush()"
In reply to Mike Rapoport (Microsoft)Quoted 22 lines · Mike Rapoport (Microsoft)
Commit
0c6378a71574("arch: introduceset_direct_map_valid_noflush()") addedset_direct_map_valid_noflush()to allow updating the direct map for a physically contiguous range in execmem.As Brendan recently pointed out [1], this API is confusing because on arm64 it means that is sets VALID bit in ptes, while on other architectures it is an analog of
set_direct_map_default_noflush().The only user of
set_direct_map_valid_noflush()was execmem's ROX cache freeing path and it was switched to utilizeVM_FLUSH_RESET_PERMSfor resetting permissions of the direct map alias.With the last user gone and with set_direct_map_{invalid,default}_noflush() accepting number of pages as a parameter,
set_direct_map_valid_noflush()become a copy ofset_memory_valid()on arm64 and a duplicate of set_direct_map_{invalid,default}_noflush() on other architecture, it is safe to removeset_direct_map_valid_noflush().Also drop a stale comment in
arm64::__kernel_map_pages()that Linus bothered to add when merging changes containingset_direct_map_valid_noflush()to his tree.
Quoted 6 lines
This reverts commit
0c6378a71574daa6cd1534ad42a956e3262756c7.[1] https://lore.kernel.org/all/[email protected]
Reviewed-by Brendan Jackman <[email protected]>Signed-off-by Mike Rapoport (Microsoft) <[email protected]>
It's a great feeling to see the set_direct_map API get a little streamlined :D
Andrew Morton
maintainerQuoted 9 lines · On Thu, 03 Sep 2026 12:28:26 +0300 "Mike Rapoport (Microsoft)"…
Recent discussion about implementation of execmem's ROX caches on arm64 revealed a confusion about how
set_direct_map_valid_noflush()implemented on different architectures....
Implement these adjustments, make execmem always use
VM_FLUSH_RESET_PERMSand revertset_direct_map_valid_noflush()changes.
Thanks, I updated mm-unstable to this version.
Quoted 5 lines
v3 changes:
* setVM_FLUSH_RESET_PERMSto allocated area just before flipping the permissions rather than at allocation time
Here's how v3 altered mm.git:
mm/execmem.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
mm/execmem.c renamed from mm/execmem.c~b +4 -2
@@ -36,7 +36,6 @@ static void *execmem_vmalloc(struct execunsigned long end = range->end;void *p; - vm_flags |=VM_FLUSH_RESET_PERMS ;if (kasan) vm_flags |=VM_DEFER_KMEMLEAK ;
@@ -277,6 +276,8 @@ static void *execmem_cache_populate_allo/* fill memory with instructions that will trap */ execmem_fill_trapping_insns (p, alloc_size); +set_vm_flush_reset_perms (p); + err =set_memory_rox ((unsigned long )p, vm->nr_pages);if (err) goto err_free_mem;
@@ -439,6 +440,7 @@ void *execmem_alloc(enum execmem_type ty {struct execmem_range *range = &execmem_info->ranges[type];bool use_cache = range->flags &EXECMEM_ROX_CACHE ; +unsigned long vm_flags =VM_FLUSH_RESET_PERMS ;pgprot_t pgprot = range->pgprot;void *p =NULL ;
@@ -447,7 +449,7 @@ void *execmem_alloc(enum execmem_type tyif (use_cache) p =execmem_cache_alloc (range, size);else - p =execmem_vmalloc (range, size, pgprot,0 ); + p =execmem_vmalloc (range, size, pgprot, vm_flags);return kasan_reset_tag (p); }
_
This series was applied to riscv/linux.git (fixes) by Andrew Morton <[email protected]>:
Hello:
This series was applied to riscv/linux.git (fixes) by Andrew Morton <[email protected]>:
Quoted 8 lines · On Thu, 03 Sep 2026 12:28:26 +0300 you wrote:
Recent discussion about implementation of execmem's ROX caches on arm64 revealed a confusion about how
set_direct_map_valid_noflush()implemented on different architectures.On arm64 it sets or clears the
PTE_VALIDbit marking a PTE as present or not present.[...]
Here is the summary with links:
- [v3,1/6] set_memory: add number of pages parameter to set_direct_map APIs
https://git.kernel.org/riscv/c/be7dacb9c1d4 - [v3,2/6] mm/vmalloc: set area's page_order after allocation succeeds https://git.kernel.org/riscv/c/40702f168a9e - [v3,3/6] mm/vmalloc: constify vm parameter of get_vm_area_page_order() https://git.kernel.org/riscv/c/1f73dfdeb9c2 - [v3,4/6] mm/vmalloc: make set_area_direct_map HUGE_VMAP friendly https://git.kernel.org/riscv/c/86dc0280b807 - [v3,5/6] mm/execmem: use VM_FLUSH_RESET_PERMS for ROX cache allocations https://git.kernel.org/riscv/c/4c4b2ed5b783 - [v3,6/6] Revert "arch: introduce set_direct_map_valid_noflush()" https://git.kernel.org/riscv/c/0e774fdb2ed6
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html