[LTP] [PATCH v3] mmap22.c: Test for new MAP_DROPPABLE flag for mmap
Wei Gao
wegao@suse.com
Wed Apr 16 21:16:44 CEST 2025
Signed-off-by: Wei Gao <wegao@suse.com>
---
include/lapi/mmap.h | 4 +
runtest/syscalls | 1 +
testcases/kernel/syscalls/mmap/.gitignore | 1 +
testcases/kernel/syscalls/mmap/mmap22.c | 95 +++++++++++++++++++++++
4 files changed, 101 insertions(+)
create mode 100644 testcases/kernel/syscalls/mmap/mmap22.c
diff --git a/include/lapi/mmap.h b/include/lapi/mmap.h
index ea9730586..248b64564 100644
--- a/include/lapi/mmap.h
+++ b/include/lapi/mmap.h
@@ -87,6 +87,10 @@
# define MADV_PAGEOUT 21
#endif
+#ifndef MAP_DROPPABLE
+# define MAP_DROPPABLE 0x08
+#endif
+
#ifndef MAP_FIXED_NOREPLACE
#ifdef __alpha__
diff --git a/runtest/syscalls b/runtest/syscalls
index 932b7030c..e369536ea 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -844,6 +844,7 @@ mmap19 mmap19
mmap20 mmap20
mmap21_01 mmap21 -m 1
mmap21_02 mmap21
+mmap22 mmap22
modify_ldt01 modify_ldt01
modify_ldt02 modify_ldt02
diff --git a/testcases/kernel/syscalls/mmap/.gitignore b/testcases/kernel/syscalls/mmap/.gitignore
index 850284d86..075be933d 100644
--- a/testcases/kernel/syscalls/mmap/.gitignore
+++ b/testcases/kernel/syscalls/mmap/.gitignore
@@ -18,3 +18,4 @@
/mmap19
/mmap20
/mmap21
+/mmap22
diff --git a/testcases/kernel/syscalls/mmap/mmap22.c b/testcases/kernel/syscalls/mmap/mmap22.c
new file mode 100644
index 000000000..31414896c
--- /dev/null
+++ b/testcases/kernel/syscalls/mmap/mmap22.c
@@ -0,0 +1,95 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2025 Wei Gao <wegao@suse.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Test mmap(2) with MAP_DROPPABLE flag.
+ *
+ * Test base on kernel selftests/mm/droppable.c
+ */
+
+#define _GNU_SOURCE
+#include <errno.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include "tst_test.h"
+#include "lapi/mmap.h"
+
+#define MEM_LIMIT (256 * TST_MB)
+#define ALLOC_SIZE (128 * TST_MB)
+
+static struct tst_cg_group *cg_child;
+
+static void test_mmap(void)
+{
+ size_t alloc_size = ALLOC_SIZE;
+ size_t page_size = getpagesize();
+ void *alloc;
+ pid_t child;
+
+ cg_child = tst_cg_group_mk(tst_cg, "child");
+ SAFE_CG_PRINTF(tst_cg, "memory.max", "%d", MEM_LIMIT);
+ SAFE_CG_PRINTF(cg_child, "cgroup.procs", "%d", getpid());
+
+ alloc = SAFE_MMAP(0, alloc_size, PROT_READ | PROT_WRITE,
+ MAP_ANONYMOUS | MAP_DROPPABLE, -1, 0);
+
+ memset(alloc, 'A', alloc_size);
+ for (size_t i = 0; i < alloc_size; i += page_size) {
+ if (*(char *)(alloc + i) != 'A')
+ tst_res(TFAIL, "memset failed");
+ }
+
+ child = SAFE_FORK();
+ if (!child) {
+ for (;;)
+ *(char *)malloc(page_size) = 'B';
+ }
+
+ while (1) {
+ for (size_t i = 0; i < alloc_size; i += page_size) {
+ if (!tst_remaining_runtime())
+ tst_brk(TBROK, "MAP_DROPPABLE did not drop memory within the timeout period.");
+ if (!*(uint8_t *)(alloc + i)) {
+ tst_res(TPASS, "MAP_DROPPABLE test pass.");
+ goto kill_child;
+ }
+ }
+ }
+
+kill_child:
+ SAFE_KILL(child, SIGKILL);
+ SAFE_WAITPID(child, NULL, 0);
+ SAFE_MUNMAP(alloc, alloc_size);
+}
+
+static void setup(void)
+{
+ void *addr = mmap(0, 1, PROT_READ | PROT_WRITE,
+ MAP_ANONYMOUS | MAP_DROPPABLE, -1, 0);
+ if (addr == MAP_FAILED && errno == EINVAL)
+ tst_brk(TCONF, "MAP_DROPPABLE not support");
+}
+
+static void cleanup(void)
+{
+ if (cg_child) {
+ SAFE_CG_PRINTF(tst_cg_drain, "cgroup.procs", "%d", getpid());
+ cg_child = tst_cg_group_rm(cg_child);
+ }
+}
+
+static struct tst_test test = {
+ .test_all = test_mmap,
+ .needs_tmpdir = 1,
+ .forks_child = 1,
+ .needs_cgroup_ctrls = (const char *const []){ "memory", NULL },
+ .needs_root = 1,
+ .cleanup = cleanup,
+ .setup = setup,
+ .runtime = 30,
+ .min_mem_avail = 300,
+};
--
2.49.0
More information about the ltp
mailing list