Skip to contentSkip to conversation

mm/execmem: keep the ROX cache huge-page only

PATCH New Gyokhan Kochmarla 1 participant 3 messages Nov 20

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover mm/execmem: keep the ROX cache huge-page only

Hi,

Please consider these two mainline commits for 7.3.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 7.3.4 as 8c60564f03e0. The same two commits are posted separately for 6.18.y, and this version keeps 7.3.y covered for that request.

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.

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.

7.3.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 [ 7.3: ] 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.

Both patches apply cleanly to v7.3.5, and mm/execmem.o and mm/vmalloc.o build with W=1 on x86_64. The code changes are the same as in the 6.18.y version, which was tested on c8i.metal-96xl: 20 and 29 warnings without the patches, none with them, and no failed module loads in either case. I have not run the 7.3.y version on hardware.

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: 988117cc1bb17338c586a4e21ade88b5397eac33

Signature · 1 line
2.50.1
#1 lore raw

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.

Assisted-by copilot:claude-opus-5
Cc Benjamin Tissoires <[email protected]>
Cc Jiri Kosina <[email protected]>
Cc Luis Chamberalin <[email protected]>
Cc "Uladzislau Rezki (Sony)" <[email protected]>

[ 7.3: 4c4b2ed5b783 ("mm/execmem: use VM_FLUSH_RESET_PERMS for ROX cache
allocations") is not in 7.3.y, so execmem_cache_clean() still calls execmem_set_direct_map_valid(). Keep the call and move it after the new mas_preallocate() check, so a failed preallocation leaves the chunk unchanged. ]

---
 mm/execmem.c | 32 ++++++++++++++++++++++----------
 1 file changed, 22 insertions(+), 10 deletions(-)
mm/execmem.c +22 -10
index 33e4e68f655f..3857955e701b 100644
@@ -173,8 +173,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);
 		}
 	}
@@ -244,30 +252,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
#2 lore raw

PATCH 2/2 mm/execmem: make sure ROX cache always contains multiples of PMD_SIZE

In reply to Gyokhan Kochmarla

From: "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.

Assisted-by copilot:claude-opus-5
Cc Benjamin Tissoires <[email protected]>
Cc Jiri Kosina <[email protected]>
Cc Luis Chamberalin <[email protected]>
Cc "Uladzislau Rezki (Sony)" <[email protected]>

[ 7.3: 4c4b2ed5b783 is not in 7.3.y, so cache chunks keep the
execmem_set_direct_map_valid() handling: execmem_vmalloc_rox() does not call set_vm_flush_reset_perms(), and the populate error path restores the direct map before vfree(). The uncached fallback is freed with plain vfree(), so execmem_alloc_rox() passes VM_FLUSH_RESET_PERMS. ]

---
 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
index aed121d729b0..6e555e31e622 100644
@@ -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
index bea9f76ed7e7..3ab7eff25565 100644
@@ -4019,6 +4019,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
  */
@@ -4044,6 +4050,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,
@@ -4060,6 +4070,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,
@@ -4134,7 +4147,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