Skip to contentSkip to conversation

mmap_24-1: Change vm.max_map_count if needed

PATCH In review Martin Doucha 3 participants 7 messages May 9, 2025 – May 12, 2025

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 mmap_24-1: Change vm.max_map_count if needed

If vm.max_map_count system parameter is too high, mmap24-1 may get killed by OOM. Set the parameter to a reasonable low value so that mmap() quickly fails as expected.

---

Changes since v1:
- Print warning if procfile cannot be opened
- Use fopen(..., "r") instead of "r+" in main()

 .../conformance/interfaces/mmap/24-1.c        | 37 ++++++++++++++++++-
 1 file changed, 36 insertions(+), 1 deletion(-)
testcases/open_posix_testsuite/conformance/interfaces/mmap/24-1.c +36 -1
index 6cc8349e1..49673d603 100644
@@ -31,12 +31,42 @@
 #include <stdint.h>
 #include "posixtest.h"
 
+#define MAX_MAP_COUNT_PATH "/proc/sys/vm/max_map_count"
+#define MAP_COUNT_LIMIT 65530
+
+void proc_write_val(const char *path, size_t val)
+{
+	FILE *procfile;
+
+	procfile = fopen(path, "r+");
+
+	if (!procfile) {
+		printf("Warning: Could not open %s\n", path);
+		return;
+	}
+
+	fprintf(procfile, "%zu", val);
+	fclose(procfile);
+}
+
 int main(void)
 {
 	char tmpfname[256];
 	void *pa;
-	size_t len;
+	size_t len, max_map_count = 0;
 	int fd;
+	FILE *procfile;
+
+	/* Change vm.max_map_count to avoid OOM */
+	procfile = fopen(MAX_MAP_COUNT_PATH, "r");
+
+	if (procfile) {
+		fscanf(procfile, "%zu", &max_map_count);
+		fclose(procfile);
+	}
+
+	if (max_map_count > MAP_COUNT_LIMIT)
+		proc_write_val(MAX_MAP_COUNT_PATH, MAP_COUNT_LIMIT);
 
 	/* Size of the shared memory object */
 	size_t shm_size = 1024;
@@ -78,5 +108,10 @@ int main(void)
 
 	close(fd);
 	printf("Test FAILED: Did not get ENOMEM as expected\n");
+
+	/* Restore original vm.max_map_count */
+	if (max_map_count > MAP_COUNT_LIMIT)
+		proc_write_val(MAX_MAP_COUNT_PATH, max_map_count);
+
 	return PTS_FAIL;
 }
Signature · 5 lines
2.49.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by Li Wang on v2 1/1
#1 lore raw

testcases/open_posix_testsuite/conformance/interfaces/mmap/24-1.c

Quoted 21 lines · Martin Doucha

If vm.max_map_count system parameter is too high, mmap24-1 may get killed by OOM. Set the parameter to a reasonable low value so that mmap() quickly fails as expected.

---

Changes since v1:
- Print warning if procfile cannot be opened
- Use fopen(..., "r") instead of "r+" in main()

 .../conformance/interfaces/mmap/24-1.c        | 37 ++++++++++++++++++-
 1 file changed, 36 insertions(+), 1 deletion(-)
testcases/open_posix_testsuite/conformance/interfaces/mmap/24-1.c +0 -0
index 6cc8349e1..49673d603 100644

As I pointed in the previous thread, we should update the code comments to explicitly state that the failure is caused by exceeding the maximum number of memory mappings (vm.max_map_count), rather than overall memory exhaustion.

Without this clarification, readers may be misled by the current comments and mistakenly assume that the code is intended to test total memory exhaustion, which is not the case.

Anyway, patch itself looks good.
And I vote for merging this before a new release.

Quoted 62 lines
testcases/open_posix_testsuite/conformance/interfaces/mmap/24-1.c +36 -2
@@ -31,12 +31,42 @@
 #include <stdint.h>
 #include "posixtest.h"
 
+#define MAX_MAP_COUNT_PATH "/proc/sys/vm/max_map_count"
+#define MAP_COUNT_LIMIT 65530
+
+void proc_write_val(const char *path, size_t val)
+{
+       FILE *procfile;
+
+       procfile = fopen(path, "r+");
+
+       if (!procfile) {
+               printf("Warning: Could not open %s\n", path);
+               return;
+       }
+
+       fprintf(procfile, "%zu", val);
+       fclose(procfile);
+}
+
 int main(void)
 {
        char tmpfname[256];
        void *pa;
-       size_t len;
+       size_t len, max_map_count = 0;
        int fd;
+       FILE *procfile;
+
+       /* Change vm.max_map_count to avoid OOM */
+       procfile = fopen(MAX_MAP_COUNT_PATH, "r");
+
+       if (procfile) {
+               fscanf(procfile, "%zu", &max_map_count);
+               fclose(procfile);
+       }
+
+       if (max_map_count > MAP_COUNT_LIMIT)
+               proc_write_val(MAX_MAP_COUNT_PATH, MAP_COUNT_LIMIT);
 
        /* Size of the shared memory object */
        size_t shm_size = 1024;
@@ -78,5 +108,10 @@ int main(void)
 
        close(fd);
        printf("Test FAILED: Did not get ENOMEM as expected\n");
+
+       /* Restore original vm.max_map_count */
+       if (max_map_count > MAP_COUNT_LIMIT)
+               proc_write_val(MAX_MAP_COUNT_PATH, max_map_count);
+
        return PTS_FAIL;
 }
--

2.49.0

--
Mailing list info: https://lists.linux.it/listinfo/ltp

Signature · 6 lines
Regards,
Li Wang


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

Hi all,

merged, thank you!
@Li, please merge your follow up fixing comment.

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

PATCH 1/1 mmap_24-1: update code comments

In reply to Martin Doucha

Follow-up: mmap_24-1: Change vm.max_map_count if needed

Cc Martin Doucha <[email protected]>

---

Notes:
This patch is based on Martin's vm.max_map_count change, to make everything is clear enough from the code to comments.

 .../conformance/interfaces/mmap/24-1.c        | 20 +++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)
testcases/open_posix_testsuite/conformance/interfaces/mmap/24-1.c +14 -6
index 49673d603..8aebdba61 100644
@@ -7,15 +7,23 @@
  * source tree.
  *
  * The mmap() function shall fail if:
- * [ENOMEM] MAP_FIXED was specified, and the range [addr,addr+len)
+ * [ENOMEM] MAP_FIXED was specified, and the range [addr, addr+len)
  * exceeds that allowed for the address space of a process;
- * or, if MAP_FIXED was not specified and
- * there is insufficient room in the address space to effect the mapping.
+ * or, if MAP_FIXED was not specified and there is insufficient room
+ * in the address space to effect the mapping;
+ * or, if the process exceeds the maximum number of allowed memory mappings
+ * (as defined by /proc/sys/vm/max_map_count).
  *
  * Test Steps:
- * 1. In a very long loop, keep mapping a shared memory object,
- *    until there this insufficient room in the address space;
- * 3. Should get ENOMEM.
+ * 1. In a very long loop, continuously map a shared memory object without
+ *    unmapping previous ones.
+ * 2. The loop continues until mmap() fails with ENOMEM.
+ *
+ * Note:
+ * This failure may occur due to either exhausting the process's
+ * virtual address space, or hitting the system-wide limit on
+ * the number of memory mappings (especially on systems with large RAM).
+ *
  */
 
 #include <stdio.h>
Signature · 5 lines
2.49.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author Reviewed-by on v1 1/1
#4 lore raw

Re: [LTP] [PATCH] mmap_24-1: update code comments

Hi,
thank you.

Quoted 46 lines · Li Wang

Follow-up: mmap_24-1: Change vm.max_map_count if needed

Cc Martin Doucha <[email protected]>

---

Notes:
This patch is based on Martin's vm.max_map_count change, to make everything is clear enough from the code to comments.

  .../conformance/interfaces/mmap/24-1.c        | 20 +++++++++++++------
  1 file changed, 14 insertions(+), 6 deletions(-)
testcases/open_posix_testsuite/conformance/interfaces/mmap/24-1.c +14 -6
index 49673d603..8aebdba61 100644
@@ -7,15 +7,23 @@
   * source tree.
   *
   * The mmap() function shall fail if:
- * [ENOMEM] MAP_FIXED was specified, and the range [addr,addr+len)
+ * [ENOMEM] MAP_FIXED was specified, and the range [addr, addr+len)
   * exceeds that allowed for the address space of a process;
- * or, if MAP_FIXED was not specified and
- * there is insufficient room in the address space to effect the mapping.
+ * or, if MAP_FIXED was not specified and there is insufficient room
+ * in the address space to effect the mapping;
+ * or, if the process exceeds the maximum number of allowed memory mappings
+ * (as defined by /proc/sys/vm/max_map_count).
   *
   * Test Steps:
- * 1. In a very long loop, keep mapping a shared memory object,
- *    until there this insufficient room in the address space;
- * 3. Should get ENOMEM.
+ * 1. In a very long loop, continuously map a shared memory object without
+ *    unmapping previous ones.
+ * 2. The loop continues until mmap() fails with ENOMEM.
+ *
+ * Note:
+ * This failure may occur due to either exhausting the process's
+ * virtual address space, or hitting the system-wide limit on
+ * the number of memory mappings (especially on systems with large RAM).
+ *
   */
 
  #include <stdio.h>
Signature · 10 lines
Martin Doucha   [email protected]
SW Quality Engineer
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v1 1/1
#5 lore raw

Re: [LTP] [PATCH] mmap_24-1: update code comments

In reply to Li Wang via ltp

Hi Li,

Quoted 1 line

Follow-up: mmap_24-1: Change vm.max_map_count if needed

Thanks!
...

Quoted 18 lines

+++ b/testcases/open_posix_testsuite/conformance/interfaces/mmap/24-1.c

testcases/open_posix_testsuite/conformance/interfaces/mmap/24-1.c +5 -5
@@ -7,15 +7,23 @@
  * source tree.
  *
  * The mmap() function shall fail if:
- * [ENOMEM] MAP_FIXED was specified, and the range [addr,addr+len)
+ * [ENOMEM] MAP_FIXED was specified, and the range [addr, addr+len)
  * exceeds that allowed for the address space of a process;
- * or, if MAP_FIXED was not specified and
- * there is insufficient room in the address space to effect the mapping.
+ * or, if MAP_FIXED was not specified and there is insufficient room
+ * in the address space to effect the mapping;
+ * or, if the process exceeds the maximum number of allowed memory mappings
+ * (as defined by /proc/sys/vm/max_map_count).
  *
  * Test Steps:
- * 1. In a very long loop, keep mapping a shared memory object,
- *    until there this insufficient room in the address space;

testcases/open_posix_testsuite/conformance/interfaces/mmap/24-1.c line 26

Quoted 10 lines
testcases/open_posix_testsuite/conformance/interfaces/mmap/24-1.c +9 -1
- * 3. Should get ENOMEM.
+ * 1. In a very long loop, continuously map a shared memory object without
+ *    unmapping previous ones.
+ * 2. The loop continues until mmap() fails with ENOMEM.
+ *
+ * Note:
+ * This failure may occur due to either exhausting the process's
+ * virtual address space, or hitting the system-wide limit on
+ * the number of memory mappings (especially on systems with large RAM).
+ *

nit: please remove before commit this blank line.

Kind regards,
Petr

Quoted 1 line
(quoted diff) +0 -0
  */
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#6 lore raw

Re: [LTP] [PATCH] mmap_24-1: update code comments

Quoted 37 lines · Petr Vorel

Hi Li,

Quoted 1 line

Follow-up: mmap_24-1: Change vm.max_map_count if needed

Thanks!
...

testcases/open_posix_testsuite/conformance/interfaces/mmap/24-1.c line 13

Quoted 10 lines

+++ b/testcases/open_posix_testsuite/conformance/interfaces/mmap/24-1.c

testcases/open_posix_testsuite/conformance/interfaces/mmap/24-1.c +1 -3
@@ -7,15 +7,23 @@
  * source tree.
  *
  * The mmap() function shall fail if:
- * [ENOMEM] MAP_FIXED was specified, and the range [addr,addr+len)
+ * [ENOMEM] MAP_FIXED was specified, and the range [addr, addr+len)
  * exceeds that allowed for the address space of a process;
- * or, if MAP_FIXED was not specified and
- * there is insufficient room in the address space to effect the

mapping.

testcases/open_posix_testsuite/conformance/interfaces/mmap/24-1.c

Quoted 3 lines
testcases/open_posix_testsuite/conformance/interfaces/mmap/24-1.c +3 -0
+ * or, if MAP_FIXED was not specified and there is insufficient room
+ * in the address space to effect the mapping;
+ * or, if the process exceeds the maximum number of allowed memory

mappings

testcases/open_posix_testsuite/conformance/interfaces/mmap/24-1.c

Quoted 7 lines
testcases/open_posix_testsuite/conformance/interfaces/mmap/24-1.c +2 -3
+ * (as defined by /proc/sys/vm/max_map_count).
  *
  * Test Steps:
- * 1. In a very long loop, keep mapping a shared memory object,
- *    until there this insufficient room in the address space;
- * 3. Should get ENOMEM.
+ * 1. In a very long loop, continuously map a shared memory object

without

testcases/open_posix_testsuite/conformance/interfaces/mmap/24-1.c line 26

Quoted 8 lines
testcases/open_posix_testsuite/conformance/interfaces/mmap/24-1.c +8 -0
+ *    unmapping previous ones.
+ * 2. The loop continues until mmap() fails with ENOMEM.
+ *
+ * Note:
+ * This failure may occur due to either exhausting the process's
+ * virtual address space, or hitting the system-wide limit on
+ * the number of memory mappings (especially on systems with large RAM).
+ *
Quoted 2 lines

nit: please remove before commit this blank line.

Ah sure, merged as you suggested.
Thanks!

Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp