public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH v9] mremap07.c: New test for mremap() with MREMAP_DONTUNMAP
  2026-04-17 12:27 [LTP] [PATCH v8] " Wei Gao via ltp
@ 2026-04-22  2:30 ` Wei Gao via ltp
  0 siblings, 0 replies; 2+ messages in thread
From: Wei Gao via ltp @ 2026-04-22  2:30 UTC (permalink / raw)
  To: ltp

This test verifies the mremap() syscall with the MREMAP_DONTUNMAP flag.
It uses userfaultfd to verify that accessing the old memory region
correctly triggers a page fault after the mapping has been moved.

MREMAP_DONTUNMAP behavior with userfaultfd was not covered by existing
mremap tests. This test provides coverage for the feature requested in
the linked issue.

Closes: https://github.com/linux-test-project/ltp/issues/1168
Signed-off-by: Wei Gao <wegao@suse.com>
---
v8->v9:
- Removed `MREMAP_DONTUNMAP` check from `configure.ac` and moved fallback to `include/lapi/mmap.h`.
- Renamed address variables to `old_addr` and `new_addr` for clarity.
- Replaced hardcoded strings with `TEST_STRING_A` and `TEST_STRING_B` macros.
- Improved code formatting and indentation.
- Removed `.min_kver = "5.7"` in favor of dynamic feature detection.
- Added runtime `EINVAL` check to trigger `TCONF` on unsupported kernels.
- Added verification logic for data content at `new_addr`.
- Fixed stability issues for multiple test iterations (`-i` option).

 include/lapi/mmap.h                         |   4 +
 runtest/syscalls                            |   1 +
 testcases/kernel/syscalls/mremap/.gitignore |   1 +
 testcases/kernel/syscalls/mremap/Makefile   |   1 +
 testcases/kernel/syscalls/mremap/mremap07.c | 164 ++++++++++++++++++++
 5 files changed, 171 insertions(+)
 create mode 100644 testcases/kernel/syscalls/mremap/mremap07.c

diff --git a/include/lapi/mmap.h b/include/lapi/mmap.h
index 248b64564..3908310f4 100644
--- a/include/lapi/mmap.h
+++ b/include/lapi/mmap.h
@@ -91,6 +91,10 @@
 # define MAP_DROPPABLE 0x08
 #endif
 
+#ifndef MREMAP_DONTUNMAP
+# define MREMAP_DONTUNMAP 4
+#endif
+
 #ifndef MAP_FIXED_NOREPLACE
 
 #ifdef __alpha__
diff --git a/runtest/syscalls b/runtest/syscalls
index 5025b259f..df5dc02b5 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -927,6 +927,7 @@ mremap03 mremap03
 mremap04 mremap04
 mremap05 mremap05
 mremap06 mremap06
+mremap07 mremap07
 
 mseal01 mseal01
 mseal02 mseal02
diff --git a/testcases/kernel/syscalls/mremap/.gitignore b/testcases/kernel/syscalls/mremap/.gitignore
index ec15a19cd..292899e03 100644
--- a/testcases/kernel/syscalls/mremap/.gitignore
+++ b/testcases/kernel/syscalls/mremap/.gitignore
@@ -4,3 +4,4 @@
 /mremap04
 /mremap05
 /mremap06
+/mremap07
diff --git a/testcases/kernel/syscalls/mremap/Makefile b/testcases/kernel/syscalls/mremap/Makefile
index 9f5aca9ec..8811b887e 100644
--- a/testcases/kernel/syscalls/mremap/Makefile
+++ b/testcases/kernel/syscalls/mremap/Makefile
@@ -8,5 +8,6 @@ LTPLIBS = ipc
 include $(top_srcdir)/include/mk/testcases.mk
 
 mremap04: LTPLDLIBS  = -lltpipc
+mremap07: LDLIBS  += -lpthread
 
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/syscalls/mremap/mremap07.c b/testcases/kernel/syscalls/mremap/mremap07.c
new file mode 100644
index 000000000..ecd8319f9
--- /dev/null
+++ b/testcases/kernel/syscalls/mremap/mremap07.c
@@ -0,0 +1,164 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2026 Wei Gao <wegao@suse.com>
+ */
+
+/*\
+ * LTP test case for mremap() with MREMAP_DONTUNMAP and userfaultfd.
+ *
+ * Test mremap() with MREMAP_DONTUNMAP and verify that accessing the
+ * old memory region triggers a page fault, which is then correctly
+ * handled by a userfaultfd handler.
+ */
+
+#define _GNU_SOURCE
+#include <poll.h>
+#include <pthread.h>
+
+#include "tst_test.h"
+#include "tst_safe_pthread.h"
+#include "lapi/userfaultfd.h"
+#include "lapi/mmap.h"
+#include "config.h"
+
+static int page_size;
+static int uffd = -1;
+static char *old_addr;
+static char *new_addr;
+
+#define TEST_STRING_A "ABCD"
+#define TEST_STRING_B "EFGH"
+
+static void *fault_handler_thread(void *arg LTP_ATTRIBUTE_UNUSED)
+{
+	struct uffd_msg msg;
+	struct uffdio_copy uffdio_copy;
+
+	TST_CHECKPOINT_WAIT(0);
+
+	struct pollfd pollfd;
+
+	pollfd.fd = uffd;
+	pollfd.events = POLLIN;
+
+	int nready = poll(&pollfd, 1, -1);
+
+	if (nready == -1)
+		tst_brk(TBROK | TERRNO, "poll() failed");
+
+	if (nready == 0)
+		tst_brk(TBROK, "poll() timed out unexpectedly");
+
+	SAFE_READ(1, uffd, &msg, sizeof(msg));
+
+	if (msg.event != UFFD_EVENT_PAGEFAULT)
+		tst_brk(TBROK, "Received unexpected UFFD_EVENT: %d", msg.event);
+
+	if ((char *)msg.arg.pagefault.address != old_addr)
+		tst_brk(TBROK, "Page fault on unexpected address: %p",
+			(void *)msg.arg.pagefault.address);
+
+	tst_res(TINFO, "Userfaultfd handler caught a page fault at %p",
+		(void *)msg.arg.pagefault.address);
+
+	uffdio_copy.src = (unsigned long)new_addr;
+	uffdio_copy.dst = (unsigned long)old_addr;
+	uffdio_copy.len = page_size;
+	uffdio_copy.mode = 0;
+	uffdio_copy.copy = 0;
+
+	SAFE_IOCTL(uffd, UFFDIO_COPY, &uffdio_copy);
+	tst_res(TPASS, "Userfaultfd handler successfully handled the fault");
+
+	return NULL;
+}
+
+static void setup(void)
+{
+	struct uffdio_api uffdio_api;
+	struct uffdio_register uffdio_register;
+
+	page_size = getpagesize();
+	uffd = SAFE_USERFAULTFD(O_CLOEXEC | O_NONBLOCK, true);
+
+	uffdio_api.api = UFFD_API;
+	uffdio_api.features = 0;
+	SAFE_IOCTL(uffd, UFFDIO_API, &uffdio_api);
+
+	old_addr = SAFE_MMAP(NULL, page_size,
+			     PROT_READ | PROT_WRITE,
+			     MAP_PRIVATE | MAP_ANONYMOUS,
+			     -1, 0);
+
+	tst_res(TINFO, "Original mapping created at %p", (void *)old_addr);
+
+	strcpy(old_addr, TEST_STRING_A);
+
+	uffdio_register.range.start = (unsigned long)old_addr;
+	uffdio_register.range.len = page_size;
+	uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
+	SAFE_IOCTL(uffd, UFFDIO_REGISTER, &uffdio_register);
+}
+
+static void cleanup(void)
+{
+	if (new_addr && new_addr != MAP_FAILED)
+		SAFE_MUNMAP(new_addr, page_size);
+
+	if (old_addr && old_addr != MAP_FAILED)
+		SAFE_MUNMAP(old_addr, page_size);
+
+	if (uffd != -1)
+		SAFE_CLOSE(uffd);
+}
+
+static void run(void)
+{
+	new_addr = NULL;
+	pthread_t handler_thread;
+
+	SAFE_PTHREAD_CREATE(&handler_thread, NULL,
+			    fault_handler_thread, NULL);
+
+	new_addr = mremap(old_addr, page_size, page_size,
+			  MREMAP_DONTUNMAP | MREMAP_MAYMOVE, NULL);
+
+	if (new_addr == MAP_FAILED) {
+		if (errno == EINVAL) {
+			tst_brk(TCONF | TERRNO,
+				"mremap with MREMAP_DONTUNMAP not supported?");
+		}
+		tst_brk(TBROK | TERRNO, "mremap failed");
+	}
+
+	tst_res(TINFO, "New mapping created at %p", (void *)new_addr);
+
+	TST_EXP_EQ_STR(new_addr, TEST_STRING_A);
+	strcpy(new_addr, TEST_STRING_B);
+
+	TST_CHECKPOINT_WAKE(0);
+
+	tst_res(TINFO, "Main thread accessing old address %p to trigger fault",
+		(void *)old_addr);
+
+	(void)*(volatile char *)old_addr;
+
+	SAFE_PTHREAD_JOIN(handler_thread, NULL);
+
+	TST_EXP_EQ_STR(old_addr, TEST_STRING_B);
+
+	SAFE_MUNMAP(new_addr, page_size);
+	new_addr = NULL;
+	strcpy(old_addr, TEST_STRING_A);
+}
+
+static struct tst_test test = {
+	.test_all = run,
+	.setup = setup,
+	.needs_checkpoints = 1,
+	.cleanup = cleanup,
+	.needs_kconfigs = (const char *[]) {
+		"CONFIG_USERFAULTFD=y",
+		NULL,
+	},
+};
-- 
2.52.0


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

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [LTP] [PATCH v9] mremap07.c: New test for mremap() with MREMAP_DONTUNMAP
@ 2026-04-22  5:14 Li Wang
  0 siblings, 0 replies; 2+ messages in thread
From: Li Wang @ 2026-04-22  5:14 UTC (permalink / raw)
  To: Wei Gao; +Cc: ltp

Hi Wei,

Wei Gao wrote:

> This test verifies the mremap() syscall with the MREMAP_DONTUNMAP flag.
> It uses userfaultfd to verify that accessing the old memory region
> correctly triggers a page fault after the mapping has been moved.
> 
> MREMAP_DONTUNMAP behavior with userfaultfd was not covered by existing
> mremap tests. This test provides coverage for the feature requested in
> the linked issue.
> 
> Closes: https://github.com/linux-test-project/ltp/issues/1168
> Signed-off-by: Wei Gao <wegao@suse.com>
> ---
> v8->v9:
> - Removed `MREMAP_DONTUNMAP` check from `configure.ac` and moved fallback to `include/lapi/mmap.h`.
> - Renamed address variables to `old_addr` and `new_addr` for clarity.
> - Replaced hardcoded strings with `TEST_STRING_A` and `TEST_STRING_B` macros.
> - Improved code formatting and indentation.
> - Removed `.min_kver = "5.7"` in favor of dynamic feature detection.
> - Added runtime `EINVAL` check to trigger `TCONF` on unsupported kernels.
> - Added verification logic for data content at `new_addr`.
> - Fixed stability issues for multiple test iterations (`-i` option).

Patch merged with some improvements (for better readability and reliability):

--- a/testcases/kernel/syscalls/mremap/mremap07.c
+++ b/testcases/kernel/syscalls/mremap/mremap07.c
@@ -19,7 +19,6 @@
 #include "tst_safe_pthread.h"
 #include "lapi/userfaultfd.h"
 #include "lapi/mmap.h"
-#include "config.h"

 static int page_size;
 static int uffd = -1;
@@ -33,33 +32,24 @@ static void *fault_handler_thread(void *arg LTP_ATTRIBUTE_UNUSED)
 {
        struct uffd_msg msg;
        struct uffdio_copy uffdio_copy;
+       struct pollfd pollfd = { .fd = uffd, .events = POLLIN };

        TST_CHECKPOINT_WAIT(0);

-       struct pollfd pollfd;
-
-       pollfd.fd = uffd;
-       pollfd.events = POLLIN;
-
-       int nready = poll(&pollfd, 1, -1);
-
-       if (nready == -1)
+       if (poll(&pollfd, 1, -1) == -1)
                tst_brk(TBROK | TERRNO, "poll() failed");

-       if (nready == 0)
-               tst_brk(TBROK, "poll() timed out unexpectedly");
-
        SAFE_READ(1, uffd, &msg, sizeof(msg));

        if (msg.event != UFFD_EVENT_PAGEFAULT)
                tst_brk(TBROK, "Received unexpected UFFD_EVENT: %d", msg.event);

-       if ((char *)msg.arg.pagefault.address != old_addr)
+       if (msg.arg.pagefault.address != (unsigned long)old_addr)
                tst_brk(TBROK, "Page fault on unexpected address: %p",
-                       (void *)msg.arg.pagefault.address);
+                               (void *)msg.arg.pagefault.address);

        tst_res(TINFO, "Userfaultfd handler caught a page fault at %p",
-               (void *)msg.arg.pagefault.address);
+                       (void *)msg.arg.pagefault.address);

        uffdio_copy.src = (unsigned long)new_addr;
        uffdio_copy.dst = (unsigned long)old_addr;
@@ -73,12 +63,32 @@ static void *fault_handler_thread(void *arg LTP_ATTRIBUTE_UNUSED)
        return NULL;
 }

+static void check_mremap_dontunmap(void)
+{
+       char *test = SAFE_MMAP(NULL, page_size, PROT_READ | PROT_WRITE,
+                               MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+
+       char *tmp = mremap(test, page_size, page_size,
+                               MREMAP_DONTUNMAP | MREMAP_MAYMOVE, NULL);
+
+       if (tmp == MAP_FAILED) {
+               if (errno == EINVAL)
+                       tst_brk(TCONF | TERRNO,
+                                       "MREMAP_DONTUNMAP not supported");
+               tst_brk(TBROK | TERRNO, "mremap failed");
+       }
+
+       SAFE_MUNMAP(tmp, page_size);
+       SAFE_MUNMAP(test, page_size);
+}
+
 static void setup(void)
 {
        struct uffdio_api uffdio_api;
        struct uffdio_register uffdio_register;

        page_size = getpagesize();
+       check_mremap_dontunmap();
        uffd = SAFE_USERFAULTFD(O_CLOEXEC | O_NONBLOCK, true);

        uffdio_api.api = UFFD_API;
@@ -92,7 +102,7 @@ static void setup(void)

        tst_res(TINFO, "Original mapping created at %p", (void *)old_addr);

-       strcpy(old_addr, TEST_STRING_A);
+       memcpy(old_addr, TEST_STRING_A, sizeof(TEST_STRING_A));

        uffdio_register.range.start = (unsigned long)old_addr;
        uffdio_register.range.len = page_size;
@@ -118,23 +128,17 @@ static void run(void)
        pthread_t handler_thread;

        SAFE_PTHREAD_CREATE(&handler_thread, NULL,
-                           fault_handler_thread, NULL);
+                               fault_handler_thread, NULL);

        new_addr = mremap(old_addr, page_size, page_size,
-                         MREMAP_DONTUNMAP | MREMAP_MAYMOVE, NULL);
-
-       if (new_addr == MAP_FAILED) {
-               if (errno == EINVAL) {
-                       tst_brk(TCONF | TERRNO,
-                               "mremap with MREMAP_DONTUNMAP not supported?");
-               }
+                               MREMAP_DONTUNMAP | MREMAP_MAYMOVE, NULL);
+       if (new_addr == MAP_FAILED)
                tst_brk(TBROK | TERRNO, "mremap failed");
-       }

        tst_res(TINFO, "New mapping created at %p", (void *)new_addr);

        TST_EXP_EQ_STR(new_addr, TEST_STRING_A);
-       strcpy(new_addr, TEST_STRING_B);
+       memcpy(new_addr, TEST_STRING_B, sizeof(TEST_STRING_B));

        TST_CHECKPOINT_WAKE(0);

@@ -148,8 +152,7 @@ static void run(void)
        TST_EXP_EQ_STR(old_addr, TEST_STRING_B);

        SAFE_MUNMAP(new_addr, page_size);
-       new_addr = NULL;
-       strcpy(old_addr, TEST_STRING_A);
+       memcpy(old_addr, TEST_STRING_A, sizeof(TEST_STRING_A));
 }

 static struct tst_test test = {

-- 
Regards,
Li Wang

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-04-22  5:15 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-22  5:14 [LTP] [PATCH v9] mremap07.c: New test for mremap() with MREMAP_DONTUNMAP Li Wang
  -- strict thread matches above, loose matches on Subject: below --
2026-04-17 12:27 [LTP] [PATCH v8] " Wei Gao via ltp
2026-04-22  2:30 ` [LTP] [PATCH v9] " Wei Gao via ltp

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox