mm/execmem: only free ROX cache vm_struct when fully unused
Subsystems (3)
PATCH 1/1 mm/execmem: only free ROX cache vm_struct when fully unused
execmem_cache_clean() currently uses:
IS_ALIGNED(size, PMD_SIZE) && IS_ALIGNED(mas.index, PMD_SIZE)
as a proxy to determine whether a free range in execmem_cache.free_areas spans an entire unused vm_struct that can be returned to vmalloc.
This heuristic is broken in both directions:
1. Premature vfree() of multi-PMD vm_structs (Use-After-Free):
When execmem_cache_populate_alloc() services a request larger than PMD_SIZE (for example, a module with > 6 MiB of .text), alloc_size = round_up(size, PMD_SIZE) creates a single multi-PMD vm_struct (e.g., 8 MiB) whose remaining tail space is handed out to subsequent module allocations (e.g., kvm.ko). When a module inside that vm_struct unloads and leaves a PMD_SIZE-aligned free sub-range (such as a 2 MiB or 4 MiB chunk at the start of the 8 MiB vm_struct), execmem_cache_clean() passes the IS_ALIGNED() check and calls vfree(area), which unmaps and frees the entire enclosing multi-PMD vm_struct while sibling modules are still live:
BUG: unable to handle page fault for address: ffffffffc07dc030
#PF: supervisor instruction fetch in kernel mode
#PF: error_code(0x0010) - not-present page
PGD 2c33067 P4D 2c33067 PUD 2c35067 PMD 0
Oops: Oops: 0010 [#1] SMP NOPTI
RIP: 0010:dummy_exit+0x0/0xfd0 [tailm]2. Failure to reclaim fallback vm_structs:
After commit 3bd4e0ac61b2 ("execmem: add fallback for failures in vmalloc(VM_ALLOW_HUGE_VMAP)") and commit 4afb85f5e359 ("mm: remove PMD alignment constraint in execmem_vmalloc()"), ROX cache vm_structs allocated via the base-page or exact-size fallback paths are only PAGE_SIZE-aligned in address and/or size, so they never satisfy the PMD_SIZE alignment check even when completely unused.
Because vmalloc allocations include a trailing guard page, separate vm_struct allocations can never coalesce into a single contiguous entry in execmem_cache.free_areas. Replace the PMD_SIZE alignment check with an explicit check that the free range starts at vm->addr and spans the entire get_vm_area_size(vm).
--- mm/execmem.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
mm/execmem.c +2 -4
@@ -166,11 +166,9 @@ static void execmem_cache_clean(struct work_struct *work)mutex_lock (mutex);mas_for_each (&mas, area,ULONG_MAX ) {size_t size =mas_range_len (&mas); +struct vm_struct *vm =find_vm_area (area); -if (IS_ALIGNED (size,PMD_SIZE ) && -IS_ALIGNED (mas.index,PMD_SIZE )) { -struct vm_struct *vm =find_vm_area (area); - +if (vm->addr == area &&get_vm_area_size (vm) == size) {execmem_set_direct_map_valid (vm, true);mas_store_gfp (&mas,NULL ,GFP_KERNEL );vfree (area);
Signature · 1 line
2.47.0
Mike Rapoport
maintainerQuoted 3 lines · Arnav Kansal
execmem_cache_clean()currently uses:IS_ALIGNED(size, PMD_SIZE) && IS_ALIGNED(mas.index, PMD_SIZE)
This is not the case anymore:
Quoted 68 lines
as a proxy to determine whether a free range in execmem_cache.free_areas spans an entire unused
vm_structthat can be returned to vmalloc.This heuristic is broken in both directions:
1. Premature
vfree()of multi-PMDvm_structs(Use-After-Free):
Whenexecmem_cache_populate_alloc()services a request larger thanPMD_SIZE(for example, a module with > 6 MiB of .text),alloc_size=round_up(size, PMD_SIZE)creates a single multi-PMDvm_struct(e.g., 8 MiB) whose remaining tail space is handed out to subsequent module allocations (e.g., kvm.ko). When a module inside thatvm_structunloads and leaves a PMD_SIZE-aligned free sub-range (such as a 2 MiB or 4 MiB chunk at the start of the 8 MiBvm_struct),execmem_cache_clean()passes theIS_ALIGNED()check and calls vfree(area), which unmaps and frees the entire enclosing multi-PMDvm_structwhile sibling modules are still live:BUG: unable to handle page fault for address: ffffffffc07dc030 #PF: supervisor instruction fetch in kernel mode #PF: error_code(0x0010) - not-present page PGD 2c33067 P4D 2c33067 PUD 2c35067 PMD 0 Oops: Oops: 0010 [#1] SMP NOPTI RIP: 0010:dummy_exit+0x0/0xfd0 [tailm]2. Failure to reclaim fallback
vm_structs:
After commit3bd4e0ac61b2("execmem: add fallback for failures invmalloc(VM_ALLOW_HUGE_VMAP)") and commit4afb85f5e359("mm: remove PMD alignment constraint inexecmem_vmalloc()"), ROX cachevm_structsallocated via the base-page or exact-size fallback paths are only PAGE_SIZE-aligned in address and/or size, so they never satisfy thePMD_SIZEalignment check even when completely unused.Because vmalloc allocations include a trailing guard page, separate
vm_structallocations can never coalesce into a single contiguous entry in execmem_cache.free_areas. Replace thePMD_SIZEalignment check with an explicit check that the free range starts atvm->addrand spans the entireget_vm_area_size(vm).Fixes 2e45474ab14f ("execmem: add support for cache of large ROX pages")Assisted-by LLMCo-developed-by Rohan Kumar <[email protected]>Signed-off-by Rohan Kumar <[email protected]>Signed-off-by Arnav Kansal <[email protected]>--- mm/execmem.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)mm/execmem.c +2
-4@@ -166,11 +166,9 @@ static void execmem_cache_clean(struct work_struct *work)mutex_lock (mutex);mas_for_each (&mas, area,ULONG_MAX ) {size_t size =mas_range_len (&mas); +struct vm_struct *vm =find_vm_area (area); -if (IS_ALIGNED (size,PMD_SIZE ) && -IS_ALIGNED (mas.index,PMD_SIZE )) { -struct vm_struct *vm =find_vm_area (area); - +if (vm->addr == area &&get_vm_area_size (vm) == size) {execmem_set_direct_map_valid (vm, true);mas_store_gfp (&mas,NULL ,GFP_KERNEL );vfree (area);Signature · 1 line
2.47.0
Signature · 2 lines
Sincerely yours, Mike.
Quoted 8 lines · Mike Rapoport
Quoted 3 lines · Arnav Kansal
execmem_cache_clean()currently uses:IS_ALIGNED(size, PMD_SIZE) && IS_ALIGNED(mas.index, PMD_SIZE)This is not the case anymore:
Thanks for the pointer, Mike! We had checked Linus's tree (v7.3-rc6) and missed that your series ("mm/execmem: fixes and cleanups for the ROX cache") was already queued in akpm/mm.git.
Commit 52d95a55a011 ("mm/execmem: free ROX cache chunks only when they span an entire vm area") and 569db54d8b7a completely cover this, so please drop our patch.
As a data point, we hit the exact multi-PMD premature vfree() from 52d95a55a011 in production (on 6.18.y) when a large module (> 6 MiB of .text) failed initialization and unloaded after a smaller module (kvm.ko) had been allocated out of the tail of the same 8 MiB vm_struct, so having 52d95a55a011 tagged for stable is much appreciated.
Thanks,
Arnav