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.

Signed-off-by: Ricardo B. Marlière (SUSE) <[email protected]>
---
Hi, this sporadic issue has been found in openSUSE CI:

https://openqa.opensuse.org/tests/6270334/logfile?filename=serial_terminal.txt#line-6760
---
 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 131d9d6db867..8d4fa3025a9d 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) {

---
base-commit: 0fcb05b72344f40806cbe810de8872f1698bf053
change-id: 20261014-selftests-mm-mremap_preamble-5f98ad625f93

Best regards,
--  
Ricardo B. Marlière (SUSE) <[email protected]>


Reply via email to