Skip to contentSkip to conversation

syscalls/munmap04: Stop using MAP_FIXED_NOREPLACE

PATCH Superseded Ricardo B. Marlière via ltp 2 participants 3 messages Jul 28, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 syscalls/munmap04: Stop using MAP_FIXED_NOREPLACE

From: Ricardo B. Marlière <[email protected]>

On some machines, mmap() call might return MAP_FAILED when passed the MAP_FIXED_NOREPLACE flag for reasons other than what the test expect: vma limit reached. While at it, add a sanity check if no regions were mapped.

---
 testcases/kernel/syscalls/munmap/munmap04.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
testcases/kernel/syscalls/munmap/munmap04.c +4 -4
index e046d17a75f8f75c5b11d569c33da41d7adc7d09..f55aa770a99d2facb53234fa593fa3f41a16cb00 100644
@@ -37,8 +37,7 @@ static void setup(void)
 
 	while (1) {
 		void *p = mmap((void *)(base + PAD * vma_size * map_count),
-			     vma_size, PROT_NONE,
-			     MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE,
+			     vma_size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
 			     -1, 0);
 		if (p == MAP_FAILED)
 			break;
@@ -46,8 +45,9 @@ static void setup(void)
 	}
 
 	if (map_count == MAP_MAX_COUNT)
-		tst_brk(TBROK, "Mapped all %d regions, expected less",
-			map_count);
+		tst_brk(TBROK, "Mapped all %d regions, expected less", map_count);
+	if (map_count == 0)
+		tst_brk(TBROK, "Mapped 0 regions");
 
 	tst_res(TINFO, "Mapped %d regions", map_count);
 }

---
base-commit: 6af619552ce7676171e47b88964ffb5b08499b36
change-id: 20250728-fixes-munmap04-02083c550c6b

Best regards,

Signature · 5 lines
Ricardo B. Marlière <[email protected]>


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

Hi!

Quoted 14 lines

On some machines, mmap() call might return MAP_FAILED when passed the MAP_FIXED_NOREPLACE flag for reasons other than what the test expect: vma limit reached. While at it, add a sanity check if no regions were mapped.

---
 testcases/kernel/syscalls/munmap/munmap04.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
testcases/kernel/syscalls/munmap/munmap04.c +0 -0
index e046d17a75f8f75c5b11d569c33da41d7adc7d09..f55aa770a99d2facb53234fa593fa3f41a16cb00 100644

testcases/kernel/syscalls/munmap/munmap04.c static void setup(void) · line 40

Quoted 6 lines
testcases/kernel/syscalls/munmap/munmap04.c +1 -2
@@ -37,8 +37,7 @@ static void setup(void)
 
 	while (1) {
 		void *p = mmap((void *)(base + PAD * vma_size * map_count),
-			     vma_size, PROT_NONE,
-			     MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE,
+			     vma_size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,

How do we avoid the adjacent mapping merges without the MAP_FIXED_NOREPLACE flag? With the flag we force particular addresses that have padding between then. Without the flag kernel is free to place the mappings anywhere it wants, e.g. create a big continous mapping in which case we would effectively have a signle vma in the kernel.

testcases/kernel/syscalls/munmap/munmap04.c static void setup(void) · line 43

Quoted 3 lines
testcases/kernel/syscalls/munmap/munmap04.c +0 -0
 			     -1, 0);
 		if (p == MAP_FAILED)
 			break;

Sholdn't it be easier to continue; here if errno == EEXIST? that would mean that we would not map over any mappings that exists in the process address space and continue retrying with increasing addresses:

	if (p == MAP_FAILED && errno == EEXIST)
		continue;

	if (p == MAP_FAILED)
		break;

	maps[map_count++] = p;
Quoted 24 lines
testcases/kernel/syscalls/munmap/munmap04.c +3 -3
@@ -46,8 +45,9 @@ static void setup(void)
 	}
 
 	if (map_count == MAP_MAX_COUNT)
-		tst_brk(TBROK, "Mapped all %d regions, expected less",
-			map_count);
+		tst_brk(TBROK, "Mapped all %d regions, expected less", map_count);
+	if (map_count == 0)
+		tst_brk(TBROK, "Mapped 0 regions");
 
 	tst_res(TINFO, "Mapped %d regions", map_count);
 }
 
---

base-commit: 6af619552ce7676171e47b88964ffb5b08499b36
change-id: 20250728-fixes-munmap04-02083c550c6b

Best regards,

Signature · 5 lines
Ricardo B. Marlière <[email protected]>


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw
Quoted 38 lines · Cyril Hrubis

Hi!

Quoted 14 lines

On some machines, mmap() call might return MAP_FAILED when passed the MAP_FIXED_NOREPLACE flag for reasons other than what the test expect: vma limit reached. While at it, add a sanity check if no regions were mapped.

---
 testcases/kernel/syscalls/munmap/munmap04.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
testcases/kernel/syscalls/munmap/munmap04.c +0 -0
index e046d17a75f8f75c5b11d569c33da41d7adc7d09..f55aa770a99d2facb53234fa593fa3f41a16cb00 100644

testcases/kernel/syscalls/munmap/munmap04.c static void setup(void) · line 40

Quoted 6 lines
testcases/kernel/syscalls/munmap/munmap04.c +1 -2
@@ -37,8 +37,7 @@ static void setup(void)
 
 	while (1) {
 		void *p = mmap((void *)(base + PAD * vma_size * map_count),
-			     vma_size, PROT_NONE,
-			     MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE,
+			     vma_size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,

How do we avoid the adjacent mapping merges without the MAP_FIXED_NOREPLACE flag? With the flag we force particular addresses that have padding between then. Without the flag kernel is free to place the mappings anywhere it wants, e.g. create a big continous mapping in which case we would effectively have a signle vma in the kernel.

testcases/kernel/syscalls/munmap/munmap04.c static void setup(void) · line 43

Quoted 3 lines
testcases/kernel/syscalls/munmap/munmap04.c +0 -0
 			     -1, 0);
 		if (p == MAP_FAILED)
 			break;

Sholdn't it be easier to continue; here if errno == EEXIST? that would mean that we would not map over any mappings that exists in the process address space and continue retrying with increasing addresses:

Quoted 7 lines
	if (p == MAP_FAILED && errno == EEXIST)
		continue;

	if (p == MAP_FAILED)
		break;

	maps[map_count++] = p;

Indeed, many thanks for the suggestion! Will send v2

Quoted 25 lines
Quoted 25 lines
testcases/kernel/syscalls/munmap/munmap04.c +3 -3
@@ -46,8 +45,9 @@ static void setup(void)
 	}
 
 	if (map_count == MAP_MAX_COUNT)
-		tst_brk(TBROK, "Mapped all %d regions, expected less",
-			map_count);
+		tst_brk(TBROK, "Mapped all %d regions, expected less", map_count);
+	if (map_count == 0)
+		tst_brk(TBROK, "Mapped 0 regions");
 
 	tst_res(TINFO, "Mapped %d regions", map_count);
 }
 
---

base-commit: 6af619552ce7676171e47b88964ffb5b08499b36
change-id: 20250728-fixes-munmap04-02083c550c6b

Best regards,

Signature · 5 lines
Ricardo B. Marlière <[email protected]>


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp