mm/execmem: keep the ROX cache huge-page only
Subsystems (2)
cover mm/execmem: keep the ROX cache huge-page only
Hi,
Please consider these two mainline commits for 6.18.y:
d80773cedde2 ("mm/execmem: handle potential allocation errors in the maple tree") 569db54d8b7a ("mm/execmem: make sure ROX cache always contains multiples of PMD_SIZE")
They complete the series whose first patch, 52d95a55a011 ("mm/execmem:
free ROX cache chunks only when they span an entire vm area"), is already in 6.18.63 as e4dcde9709a2. The same two commits are posted separately for 7.3.y.
On x86 systems with 384 CPUs, udev coldplug runs several hundred concurrent init_module() calls for the same modules (kvm, kvm_intel, irqbypass, x86_pkg_temp_thermal). On a ROX cache miss, execmem_cache_populate_alloc() reserves a 2 MiB area before it takes the cache mutex, so these reservations briefly exhaust the module address range. Each failed attempt prints "execmem: unable to allocate memory", although the exact-size retry succeeds and every module loads. The exact-size areas then stay in the cache and are never freed. On AWS *.metal-96xl instances this shows up on about 1% of boots.
With 569db54d8b7a, the cache refill only accepts a huge-page backed, PMD-sized area and fails quietly otherwise. The allocation then falls back to an exact-size ROX area outside the cache. d80773cedde2 is a prerequisite.
6.18.y does not have 4c4b2ed5b783 ("mm/execmem: use VM_FLUSH_RESET_PERMS for ROX cache allocations"), so both patches keep the existing execmem_set_direct_map_valid() handling for cache chunks. The uncached fallback is freed with plain vfree(), so it uses VM_FLUSH_RESET_PERMS like the non-ROX path. Each patch documents the change in a [ 6.18: ] note. Mike, if you would rather have 4c4b2ed5b783 backported as well, I can send that version instead.
569db54d8b7a is above the usual 100-line limit. A large part of it moves the ROX setup into a helper that the cache refill and the fallback share.
Testing was done on c8i.metal-96xl with an Amazon Linux 6.18.62 kernel, without and with all three patches (e4dcde9709a2 plus these two). Both kernels also carried a test-only patch that keeps the execmem helpers traceable. Each run did 20 udev coldplug cycles of the modules above, with two runs per kernel:
without with "unable to allocate memory" 20, 29 0, 0 execmem_alloc() returned NULL 0, 0 0, 0 module load -ENOMEM 0, 0 0, 0 uncached fallbacks / allocations n/a 709 / 88288, 346 / 87832 median coldplug cycle (ms) 5496, 5777 5845, 5986
Both patches apply cleanly to v6.18.64, and mm/execmem.o and mm/vmalloc.o build with W=1 on x86_64.
Gyokhan
Mike Rapoport (Microsoft) (2):
mm/execmem: handle potential allocation errors in the maple tree mm/execmem: make sure ROX cache always contains multiples of PMD_SIZE
include/linux/vmalloc.h | 1 + mm/execmem.c | 109 +++++++++++++++++++++++++--------------- mm/vmalloc.c | 15 +++++- 3 files changed, 84 insertions(+), 41 deletions(-)
base-commit: fd7cfd39c88596fec816639ad83cbf3e9d316372
Signature · 1 line
2.50.1
PATCH 1/2 mm/execmem: handle potential allocation errors in the maple tree
From: "Mike Rapoport (Microsoft)" <[email protected]>
commit d80773cedde2eb66e5702d383b5eff0190e7693d upstream.
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.
[ 6.18: 6.18.y still restores the direct map with execmem_set_direct_map_valid()
in execmem_cache_clean() because 4c4b2ed5b783 ("mm/execmem: use VM_FLUSH_RESET_PERMS for ROX cache allocations") is absent. Keep that call and move it after the new mas_preallocate() check, so a preallocation failure leaves the chunk in the cache unchanged. ]
--- mm/execmem.c | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-)
mm/execmem.c +22 -10
@@ -172,8 +172,16 @@ static void execmem_cache_clean(struct work_struct *work) IS_ALIGNED(mas.index, PMD_SIZE)) {struct vm_struct *vm =find_vm_area (area); +/* +* 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 ; +execmem_set_direct_map_valid (vm, true); -mas_store_gfp (&mas,NULL ,GFP_KERNEL ); +mas_store_prealloc (&mas,NULL );vfree (area); } }
@@ -243,30 +251,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.50.1
PATCH 2/2 mm/execmem: make sure ROX cache always contains multiples of PMD_SIZE
In reply to Gyokhan KochmarlaFrom: "Mike Rapoport (Microsoft)" <[email protected]>
commit 569db54d8b7a4ec463a62c444de0d6f5f10e9dc1 upstream.
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.
[ 6.18: 4c4b2ed5b783 ("mm/execmem: use VM_FLUSH_RESET_PERMS for ROX cache
allocations") and its prerequisite set_area_direct_map() changes are absent, so 6.18 still restores the direct map of cache chunks with execmem_set_direct_map_valid(). Keep that handling for cache chunks:
execmem_vmalloc_rox() does not call set_vm_flush_reset_perms(), and the populate error path still restores the direct map before vfree(). The uncached fallback is freed by plain vfree(), so execmem_alloc_rox() passes VM_FLUSH_RESET_PERMS, matching the non-ROX execmem_vmalloc() path. In mm/vmalloc.c only the kerneldoc context differs; the code hunks are identical to upstream. ]
--- include/linux/vmalloc.h | 1 + mm/execmem.c | 77 +++++++++++++++++++++++++---------------- mm/vmalloc.c | 15 +++++++- 3 files changed, 62 insertions(+), 31 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 -30
135 lines not on this page · raw message
mm/vmalloc.c +14 -1
@@ -3844,6 +3844,12 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,* Map them into contiguous kernel virtual space, using a pagetable * protection of @prot. * +* %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. +* * Return: the address of the area or %NULL on failure */ void *__vmalloc_node_range_noprof(unsigned long size, unsigned long align,
@@ -3867,6 +3873,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,
@@ -3883,6 +3893,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,
@@ -3956,7 +3969,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.50.1