mm/execmem: fixes and cleanups for the ROX cache
Subsystems (5)
Mike Rapoport (Microsoft)
maintainercover mm/execmem: fixes and cleanups for the ROX cache
Sashiko review of the ROX cache refill path asked what happens when the PMD_SIZE allocation fails there. Pulling that thread uncovered a bit more than the fallback.
Patch 1 fixes a real bug, although rare bug: execmem_cache_clean() can free a chunk that is still partially in use, because a PMD sized and PMD aligned free range in the middle of a larger chunk looks exactly like a chunk that nobody uses.
Patch 2 handles maple tree allocation failures in the cache. They are unlikely, but silently dropping an area from the tree is not a great way to deal with them.
Patch 3 deals with the fallback that started all this. The cache exists to keep the direct map free of unnecessary splits, and the fallback happily filled it with base page mapped areas that could never be freed from the cache again. Ask vmalloc for a huge mapping or nothing, and serve whatever does not fit outside the cache.
Patches 4 and 5 convert the ROX cache to scope based cleanup.
The series was tested on x86 with module load/unload cycles, with PTDUMP confirming that the cache is using 2M mappings, and with nohugevmalloc to exercise the uncached fallback.
---
Mike Rapoport (Microsoft) (5):
mm/execmem: free ROX cache chunks only when they span an entire vm area mm/execmem: handle potential allocation errors in the maple tree mm/execmem: make sure ROX cache always contains multiples of PMD_SIZE mm/vmalloc: add DEFINE_FREE() for vfree()
mm/execmem: use cleanup infrastructure in ROX cache functions
drivers/hid/hid-core.c | 4 +- include/linux/vmalloc.h | 4 ++ mm/execmem.c | 124 ++++++++++++++++++++++++++++-------------------- mm/vmalloc.c | 15 +++++- 4 files changed, 92 insertions(+), 55 deletions(-)
---
base-commit: 8499b4645c7f199707510461779f82d85732e742
change-id: 20260903-execmem-rox-cache-pmd-v1-ab97da2c4364
prerequisite-change-id: 20260816-execmem-set-vm-perms-v0-2-bae847a4f64f:v3
prerequisite-patch-id: 4d8c7989581fd5f4aaa2e315bfc903a9f0aadedc
prerequisite-patch-id: 211c002f2501d5aad7dea733fc6273c76bed910f
prerequisite-patch-id: a328539ba621866e4a63b39ab72a54dbfb3ed0a1
prerequisite-patch-id: 991f843051a913188e0cb671f3e8768682f895e5
prerequisite-patch-id: c05688baf37b3e80a57fe85d351f997f017acea2
prerequisite-patch-id: 36038f3f55748416fe56dc990d3836f70e851f59
--
Sincerely yours,
Mike.
Mike Rapoport (Microsoft)
maintainerPATCH 1/5 mm/execmem: free ROX cache chunks only when they span an entire vm area
When execmem refills the ROX cache, it vmalloc()s multiples of PMD_SIZE aligned to PMD_SIZE. For every such allocation vmalloc creates a vm area.
The first part of the vmalloc()ed chunk is returned to the allocation that triggered the cache refill and the remaining part is added to the cache and handed out for subsequent allocations with execmem_alloc().
When only the first part is freed, the entire vm area remains in the ROX cache and can be handed out again.
In the case when the first allocation is larger than PMD_SIZE and the second allocation from the freed first part of the chunk is exactly PMD_SIZE, execmem_cache_clean() will free the entire chunk while part of it is still in use.
For example:
/* * vmalloc(4M), return p0 to the caller * add [p0 + 3M, p0 + 4M) to the cache */ p0 = execmem_alloc(3M); /* return p0 + 3M from the cache to the caller */ p1 = execmem_alloc(1M); /* put [p0, p0 + 3M) back into the cache */ execmem_free(p0); /* return p0 from the cache to the caller */ p2 = execmem_alloc(2M); /* return p0 + 2M from the cache to the caller */ p3 = execmem_alloc(1M); /* bah! execmem_cache_clean() frees the entire 4M chunk */ execmem_free(p2);
Make sure that the ranges that execmem_cache_clean() frees cover the entire vm area.
--- mm/execmem.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
mm/execmem.c +3 -1
@@ -143,9 +143,11 @@ static void execmem_cache_clean(struct work_struct *work)mutex_lock (mutex);mas_for_each (&mas, area,ULONG_MAX ) { +struct vm_struct *vm =find_vm_area (area);size_t size =mas_range_len (&mas); -if (IS_ALIGNED (size,PMD_SIZE ) && +if (vm &&get_vm_area_size (vm) == size && +IS_ALIGNED (size,PMD_SIZE ) &&IS_ALIGNED (mas.index,PMD_SIZE )) {mas_store_gfp (&mas,NULL ,GFP_KERNEL );vfree (area);
Signature · 1 line
2.53.0
Mike Rapoport (Microsoft)
maintainerPATCH 2/5 mm/execmem: handle potential allocation errors in the maple tree
In reply to Mike Rapoport (Microsoft)execmem_cache_clean() and execmem_cache_alloc_locked() ignore potential allocation failures in mas_store_gfp().
While in practice they are unlikely to happen, it's better to handle those errors and ensure the integrity of the ROX cache.
Preallocate the maple tree nodes for the stores that must not fail and order the maple tree updates so that there won't be any failures once a tree has been modified.
--- mm/execmem.c | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-)
mm/execmem.c +22 -10
@@ -149,7 +149,15 @@ static void execmem_cache_clean(struct work_struct *work)if (vm &&get_vm_area_size (vm) == size &&IS_ALIGNED (size,PMD_SIZE ) &&IS_ALIGNED (mas.index,PMD_SIZE )) { -mas_store_gfp (&mas,NULL ,GFP_KERNEL ); +/* +* Preallocate to ensure mas_store does not fail +* If there is no memory for the tree update, bail out, +* next execmem_free() might be more lucky +*/ +if (mas_preallocate (&mas,NULL ,GFP_KERNEL )) +break ; + +mas_store_prealloc (&mas,NULL );vfree (area); } }
@@ -219,30 +227,34 @@ static void *execmem_cache_alloc_locked(struct execmem_range *range, size_t size addr = mas_free.index; last = mas_free.last; +mas_set_range (&mas_free, addr, addr + size -1 ); +if (mas_preallocate (&mas_free,NULL ,GFP_KERNEL )) +return NULL ; +/* insert allocated size to busy_areas at range [addr, addr + size) */ mas_set_range (&mas_busy, addr, addr + size -1 ); err =mas_store_gfp (&mas_busy, (void *)addr,GFP_KERNEL );if (err) -return NULL ; + goto err_destroy_mas_free; -mas_store_gfp (&mas_free,NULL ,GFP_KERNEL ); +mas_store_prealloc (&mas_free,NULL );if (area_size > size) { -void *ptr = (void *)(addr + size); -/* * re-insert remaining free size to free_areas at range * [addr + size, last] +* the range matches an existing entry, so this cannot allocate */ + ptr = (void *)(addr + size);mas_set_range (&mas_free, addr + size, last); - err =mas_store_gfp (&mas_free, ptr,GFP_KERNEL ); -if (err) { -mas_store_gfp (&mas_busy,NULL ,GFP_KERNEL ); -return NULL ; - } +mas_store_gfp (&mas_free, ptr,GFP_KERNEL ); } ptr = (void *)addr;return ptr; + +err_destroy_mas_free: +mas_destroy (&mas_free); +return NULL ; }static void *__execmem_cache_alloc (struct execmem_range *range,size_t size)
Signature · 1 line
2.53.0
Mike Rapoport (Microsoft)
maintainerPATCH 3/5 mm/execmem: make sure ROX cache always contains multiples of PMD_SIZE
In reply to Mike Rapoport (Microsoft)The ROX cache relies on its chunks being PMD mapped. For a PMD mapped chunk, set_memory_rox() updates the direct map alias one PMD at a time and the large mappings there survive.
When execmem refills the cache, it rounds up the requested allocation size to PMD_SIZE and tries to allocate that with vmalloc(VM_ALLOW_HUGE_VMAP). If that allocation fails, execmem falls back to vmalloc() of the original size.
There are two issues with this approach:
* If huge pages are not available, __vmalloc_node_range() silently falls back to base pages. The area execmem gets is virtually contiguous, but it is backed by 512 scattered base pages.
Permission updates on such areas split large mappings in the direct map that contain those base pages, up to 512 PMD splits in the worst case.
* execmem's own fallback adds a small base page mapped area to the cache. This adds the overhead of cache management to these allocations with no benefit of reducing fragmentation either in the vmalloc/modules address space or in the direct map.
Worse, these areas are never freed from the cache, because execmem_cache_clean() releases only chunks that are a multiple of PMD_SIZE and aligned to PMD_SIZE, exactly to minimize the number of base page mappings.
Add VM_REQUIRE_HUGE_VMAP option to vmalloc that fails if the allocation of huge pages fails or if such an allocation is not possible because huge page allocations in vmalloc were disabled or the architecture does not support them.
Use this option when populating the ROX cache. If vmalloc(VM_REQUIRE_HUGE_VMAP) fails or vmalloc of huge pages is unavailable, handle the memory allocation outside the ROX cache with plain vmalloc().
Since the fallback allocation has to return ROX memory, add an execmem_alloc_rox() helper and use it for both populating the ROX cache and dealing with a fallback allocation in a ROX execmem_range.
With that, the cache only ever contains PMD aligned chunks sized as a multiple of PMD_SIZE, and the PMD checks in execmem_cache_clean() become a VM_WARN_ON_ONCE() to ensure that the PMD mapping invariant does not change.
--- include/linux/vmalloc.h | 1 + mm/execmem.c | 76 ++++++++++++++++++++++++++++++------------------- mm/vmalloc.c | 15 +++++++++- 3 files changed, 62 insertions(+), 30 deletions(-)
include/linux/vmalloc.h +1 -0
@@ -38,6 +38,7 @@ struct iov_iter; /* in uio.h */#define VM_DEFER_KMEMLEAK 0#endif #define VM_SPARSE 0x00001000/* sparse vm_area. not all pages are present. */ +#define VM_REQUIRE_HUGE_VMAP 0x00002000/* huge page mapping or nothing */ /* bits [20..32] reserved for arch specific ioremap internals */
mm/execmem.c +47 -29
130 lines not on this page · raw message
mm/vmalloc.c +14 -1
@@ -4027,6 +4027,12 @@ static gfp_t vmalloc_fix_flags(gfp_t flags)* %__GFP_SKIP_KASAN can be used to skip unpoisoning of mapped pages * (when prot=%PAGE_KERNEL). * +* %VM_ALLOW_HUGE_VMAP allocates huge pages when possible and falls back to +* base pages if huge page allocation fails. +* +* %VM_REQUIRE_HUGE_VMAP implies %VM_ALLOW_HUGE_VMAP and fails instead of +* silently falling back to base pages. +* * Can not be called from interrupt nor NMI contexts. * Return: the address of the area or %NULL on failure */
@@ -4052,6 +4058,10 @@ void *__vmalloc_node_range_noprof(unsigned long size, unsigned long align,return NULL ; } +/* VM_REQUIRE_HUGE_VMAP implies VM_ALLOW_HUGE_VMAP */ +if (vm_flags &VM_REQUIRE_HUGE_VMAP ) + vm_flags |=VM_ALLOW_HUGE_VMAP ; +if (vmap_allow_huge && (vm_flags &VM_ALLOW_HUGE_VMAP )) {/* * Try huge pages. Only try for PAGE_KERNEL allocations,
@@ -4068,6 +4078,9 @@ void *__vmalloc_node_range_noprof(unsigned long size, unsigned long align, align =max (original_align,1UL << shift); } +if ((vm_flags &VM_REQUIRE_HUGE_VMAP ) && shift ==PAGE_SHIFT ) +return NULL ; + again: area = __get_vm_area_node(size, align, shift, VM_ALLOC | VM_UNINITIALIZED | vm_flags, start, end, node,
@@ -4142,7 +4155,7 @@ void *__vmalloc_node_range_noprof(unsigned long size, unsigned long align,return area->addr; fail: -if (shift >PAGE_SHIFT ) { +if (shift >PAGE_SHIFT && !(vm_flags &VM_REQUIRE_HUGE_VMAP )) { shift =PAGE_SHIFT ; align = original_align; goto again;
Signature · 1 line
2.53.0
Mike Rapoport (Microsoft)
maintainerPATCH 4/5 mm/vmalloc: add DEFINE_FREE() for vfree()
In reply to Mike Rapoport (Microsoft)... and use it in hid-core, the only place with a cleanup for a vmalloc() allocation.
--- drivers/hid/hid-core.c | 4 ++-- include/linux/vmalloc.h | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-)
drivers/hid/hid-core.c +2 -2
@@ -944,7 +944,7 @@ static int hid_scan_report(struct hid_device *hid)hid_parser_reserved }; - struct hid_parser *parser __free(kvfree) = vzalloc(sizeof(*parser)); + struct hid_parser *parser __free(vfree) = vzalloc(sizeof(*parser));if (!parser)return -ENOMEM ;
@@ -1265,7 +1265,7 @@ static int hid_parse_collections(struct hid_device *device)hid_parser_reserved }; - struct hid_parser *parser __free(kvfree) = vzalloc(sizeof(*parser)); + struct hid_parser *parser __free(vfree) = vzalloc(sizeof(*parser));if (!parser)return -ENOMEM ;
include/linux/vmalloc.h +3 -0
@@ -3,6 +3,7 @@#define _LINUX_VMALLOC_H#include <linux/alloc_tag.h> +#include <linux/cleanup.h> #include <linux/sched.h> #include <linux/spinlock.h> #include <linux/init.h>
@@ -215,6 +216,8 @@ void *__must_check vrealloc_node_align_noprof(const void *p, size_t size,extern void vfree (const void *addr);extern void vfree_atomic (const void *addr); +DEFINE_FREE(vfree, void *, if (!IS_ERR_OR_NULL(_T)) vfree(_T)) +extern void *vmap (struct page **pages,unsigned int count,unsigned long flags,pgprot_t prot);void *vmap_pfn (unsigned long *pfns,unsigned int count,pgprot_t prot);
Signature · 1 line
2.53.0
Mike Rapoport (Microsoft)
maintainerPATCH 5/5 mm/execmem: use cleanup infrastructure in ROX cache functions
In reply to Mike Rapoport (Microsoft)After splitting out execmem_alloc_rox() from execmem_cache_populate_alloc(), the error paths of both functions became less complex and can be easily switched to use the cleanup infrastructure.
Use __free(vfree) to free allocated memory on the error paths and guard(mutex) for synchronization in ROX cache functions.
--- mm/execmem.c | 32 ++++++++++---------------------- 1 file changed, 10 insertions(+), 22 deletions(-)
mm/execmem.c +10 -22
@@ -138,11 +138,10 @@ int execmem_restore_rox(void *ptr, size_t size)static void execmem_cache_clean (struct work_struct *work) {struct maple_tree *free_areas = &execmem_cache.free_areas; -struct mutex *mutex = &execmem_cache.mutex;MA_STATE (mas, free_areas,0 ,ULONG_MAX );void *area; -mutex_lock (mutex); +guard (mutex)(&execmem_cache.mutex);mas_for_each (&mas, area,ULONG_MAX ) {struct vm_struct *vm =find_vm_area (area);size_t size =mas_range_len (&mas);
@@ -163,7 +162,6 @@ static void execmem_cache_clean(struct work_struct *work)vfree (area); } } -mutex_unlock (mutex); } static DECLARE_WORK(execmem_cache_clean_work, execmem_cache_clean);
@@ -269,7 +267,7 @@ static void *__execmem_cache_alloc(struct execmem_range *range, size_t size)static void *execmem_vmalloc_rox (struct execmem_range *range,size_t size,unsigned long vm_flags) { -void *p =execmem_vmalloc (range, size,PAGE_KERNEL , vm_flags); + void *p __free(vfree) = execmem_vmalloc(range, size, PAGE_KERNEL, vm_flags);int err;if (!p)
@@ -280,22 +278,17 @@ static void *execmem_vmalloc_rox(struct execmem_range *range, size_t size,set_vm_flush_reset_perms (p); err =set_memory_rox ((unsigned long )p, size >>PAGE_SHIFT );if (err) - goto err_free_mem; - -return p; +return NULL ; -err_free_mem: -vfree (p); -return NULL ; +return no_free_ptr (p); }static void *execmem_cache_populate_alloc (struct execmem_range *range,size_t size) {unsigned long vm_flags =VM_REQUIRE_HUGE_VMAP ;size_t alloc_size =round_up (size,PMD_SIZE ); -struct mutex *mutex = &execmem_cache.mutex; + void *p __free(vfree) = NULL;int err; -void *p; p =execmem_vmalloc_rox (range, alloc_size, vm_flags);if (!p)
@@ -306,20 +299,15 @@ static void *execmem_cache_populate_alloc(struct execmem_range *range, size_t si* as an atomic operation, otherwise they may be consumed * by a parallel call to the execmem_cache_alloc function. */ -mutex_lock (mutex); +guard (mutex)(&execmem_cache.mutex); err =execmem_cache_add_locked (p, alloc_size,GFP_KERNEL ); -if (!err) - p =execmem_cache_alloc_locked (range, size); -mutex_unlock (mutex); -if (err) - goto err_free_mem; +return NULL ; -return p; +/* the chunk belongs to the cache now */ +retain_and_null_ptr (p); -err_free_mem: -vfree (p); -return NULL ; +return execmem_cache_alloc_locked (range, size); }static void *execmem_alloc_rox (struct execmem_range *range,size_t size)
Signature · 1 line
2.53.0