From 4425c6730022df01c952139df97141201f709ebc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20B=2E=20Marli=C3=A8re=20=28SUSE=29?= Date: Wed, 14 Oct 2026 15:46:46 -0300 Subject: [PATCH] selftests/mm: mremap_test: Validate the preamble region before mapping it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit remap_region() checks that the destination region is free before mapping it, but not the preamble region placed right before it, so mapping the preamble can intermittently fail with EEXIST: # Failed to map dest preamble region: File exists not ok 15 5MB mremap - Source 1MB-aligned, Dest 1MB-aligned with 40MB Preamble This happens when the kernel's heap ASLR (arch_randomize_brk()) lands the process heap inside that unchecked range. Confirmed by correlating brk(NULL) at startup with pass/fail across 60 runs: every failure had brk land inside that range. Fix it by including the preamble in the free-region check. Link: https://lore.kernel.org/20261014-selftests-mm-mremap_preamble-v1-1-2e97f501cb56@marliere.net Signed-off-by: Ricardo B. Marlière (SUSE) Signed-off-by: Andrew Morton Reviewed-by: Sarthak Sharma Cc: David Hildenbrand Cc: Lorenzo Stoakes Cc: Liam R. Howlett Cc: Vlastimil Babka Cc: Mike Rapoport Cc: Suren Baghdasaryan Cc: Michal Hocko Cc: Shuah Khan --- tools/testing/selftests/mm/mremap_test.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/mm/mremap_test.c b/tools/testing/selftests/mm/mremap_test.c index 97abf4713cc5..31b484517bda 100644 --- a/tools/testing/selftests/mm/mremap_test.c +++ b/tools/testing/selftests/mm/mremap_test.c @@ -1006,8 +1006,10 @@ static long long remap_region(struct config c, unsigned int threshold_mb, if (!((unsigned long long) addr & c.dest_alignment)) addr = (void *) ((unsigned long long) addr | c.dest_alignment); - /* Don't destroy existing mappings unless expected to overlap */ - while (!is_remap_region_valid(addr, c.region_size) && !c.overlapping) { + /* Don't destroy existing mappings unless expected to overlap. */ + while (!is_remap_region_valid(addr - c.dest_preamble_size, + c.dest_preamble_size + c.region_size) && + !c.overlapping) { /* Check for unsigned overflow */ tmp_addr = addr + c.dest_alignment; if (tmp_addr < addr) {