[LTP] [PATCH] mincore05: Add test for per-page residency accuracy
Pavithra
pavrampu@linux.ibm.com
Thu Nov 5 05:41:03 CET 2026
Test that mincore() correctly reports per-page residency
for a partially faulted anonymous mapping. Even-indexed
pages are written and mlocked; odd-indexed pages are left
untouched. The vec[] bitmap is verified to match exactly.
Signed-off-by: Pavithra <pavrampu@linux.ibm.com>
---
runtest/syscalls | 1 +
testcases/kernel/syscalls/mincore/.gitignore | 1 +
testcases/kernel/syscalls/mincore/mincore05.c | 94 +++++++++++++++++++
3 files changed, 96 insertions(+)
create mode 100644 testcases/kernel/syscalls/mincore/mincore05.c
diff --git a/runtest/syscalls b/runtest/syscalls
index ca190dd97..28abac41c 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -1045,6 +1045,7 @@ mincore01 mincore01
mincore02 mincore02
mincore03 mincore03
mincore04 mincore04
+mincore05 mincore05
madvise01 madvise01
madvise02 madvise02
diff --git a/testcases/kernel/syscalls/mincore/.gitignore b/testcases/kernel/syscalls/mincore/.gitignore
index 25a5e8ab1..406b47749 100644
--- a/testcases/kernel/syscalls/mincore/.gitignore
+++ b/testcases/kernel/syscalls/mincore/.gitignore
@@ -2,3 +2,4 @@
/mincore02
/mincore03
/mincore04
+/mincore05
diff --git a/testcases/kernel/syscalls/mincore/mincore05.c b/testcases/kernel/syscalls/mincore/mincore05.c
new file mode 100644
index 000000000..9aa5072d4
--- /dev/null
+++ b/testcases/kernel/syscalls/mincore/mincore05.c
@@ -0,0 +1,94 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) Linux Test Project, 2026
+ */
+
+/*\
+ * Verify that mincore() reports per-page residency accurately for a
+ * partially faulted anonymous mapping.
+ *
+ * A MAP_PRIVATE | MAP_ANONYMOUS region of NUM_PAGES pages is created.
+ * Only even-indexed pages (0, 2, 4) are written to and individually
+ * mlocked. The odd-indexed pages (1, 3, 5) are left untouched.
+ *
+ * After calling mincore() the vec[] bitmap must satisfy:
+ * even pages -> bit 0 set (resident)
+ * odd pages -> bit 0 clear (not resident)
+ */
+
+#include <sys/mman.h>
+#include <unistd.h>
+#include "tst_test.h"
+
+#define NUM_PAGES 6
+
+static int page_size;
+static int map_size;
+static char *map_addr;
+
+static void setup(void)
+{
+ page_size = getpagesize();
+ map_size = page_size * NUM_PAGES;
+}
+
+static void cleanup(void)
+{
+ if (map_addr) {
+ SAFE_MUNMAP(map_addr, map_size);
+ map_addr = NULL;
+ }
+}
+
+static void test_mincore(void)
+{
+ unsigned char vec[NUM_PAGES];
+ int i, fail = 0;
+
+ map_addr = SAFE_MMAP(NULL, map_size,
+ PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS,
+ -1, 0);
+
+ for (i = 0; i < NUM_PAGES; i += 2) {
+ map_addr[i * page_size] = 1;
+ SAFE_MLOCK(map_addr + i * page_size, page_size);
+ }
+
+ if (mincore(map_addr, map_size, vec) == -1)
+ tst_brk(TBROK | TERRNO, "mincore() failed");
+
+ for (i = 0; i < NUM_PAGES; i += 2)
+ SAFE_MUNLOCK(map_addr + i * page_size, page_size);
+
+ for (i = 0; i < NUM_PAGES; i++) {
+ int resident = vec[i] & 1;
+ int expected = (i % 2 == 0) ? 1 : 0;
+
+ if (resident != expected) {
+ tst_res(TFAIL,
+ "page %d: expected %s but mincore() reports %s",
+ i,
+ expected ? "resident" : "not resident",
+ resident ? "resident" : "not resident");
+ fail = 1;
+ }
+ }
+
+ if (!fail) {
+ tst_res(TPASS,
+ "mincore() correctly reports even pages resident "
+ "and odd pages not resident");
+ }
+
+ SAFE_MUNMAP(map_addr, map_size);
+ map_addr = NULL;
+}
+
+static struct tst_test test = {
+ .setup = setup,
+ .cleanup = cleanup,
+ .test_all = test_mincore,
+};
--
2.55.0
More information about the ltp
mailing list