Skip to contentSkip to conversation

mremap07.c: New case check mremap with MREMAP_DONTUNMAP

PATCH Superseded Wei Gao via ltp 4 participants 23 messages Aug 27, 2025 – Apr 22

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mremap07.c: New case check mremap with MREMAP_DONTUNMAP

This case test mremap() with MREMAP_DONTUNMAP and use userfaultfd verifies fault which triggered by accessing old memory region.

Fixes #1168
---
 runtest/syscalls                            |   1 +
 testcases/kernel/syscalls/mremap/.gitignore |   1 +
 testcases/kernel/syscalls/mremap/mremap07.c | 154 ++++++++++++++++++++
 3 files changed, 156 insertions(+)
 create mode 100644 testcases/kernel/syscalls/mremap/mremap07.c
runtest/syscalls +1 -0
index f15331da3..14ceb7696 100644
@@ -916,6 +916,7 @@ mremap03 mremap03
 mremap04 mremap04
 mremap05 mremap05
 mremap06 mremap06
+mremap07 mremap07
 
 mseal01 mseal01
 mseal02 mseal02
testcases/kernel/syscalls/mremap/.gitignore +1 -0
index ec15a19cd..292899e03 100644
@@ -4,3 +4,4 @@
 /mremap04
 /mremap05
 /mremap06
+/mremap07
testcases/kernel/syscalls/mremap/mremap07.c new file +154 -0

154 lines not on this page · raw message

Signature · 5 lines
2.43.0


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

PATCH v2 1/1 mremap07.c: New case check mremap with MREMAP_DONTUNMAP

This case test mremap() with MREMAP_DONTUNMAP and use userfaultfd verifies fault which triggered by accessing old memory region.

Fixes #1168
---
 configure.ac                                |   1 +
 runtest/syscalls                            |   1 +
 testcases/kernel/syscalls/mremap/.gitignore |   1 +
 testcases/kernel/syscalls/mremap/Makefile   |   1 +
 testcases/kernel/syscalls/mremap/mremap07.c | 159 ++++++++++++++++++++
 5 files changed, 163 insertions(+)
 create mode 100644 testcases/kernel/syscalls/mremap/mremap07.c
configure.ac +1 -0
index 0480f46ca..df842c9e4 100644
@@ -46,6 +46,7 @@ AC_CHECK_DECLS([MADV_MERGEABLE],,,[#include <sys/mman.h>])
 AC_CHECK_DECLS([NFTA_CHAIN_ID, NFTA_VERDICT_CHAIN_ID],,,[#include <linux/netfilter/nf_tables.h>])
 AC_CHECK_DECLS([PR_CAPBSET_DROP, PR_CAPBSET_READ],,,[#include <sys/prctl.h>])
 AC_CHECK_DECLS([SEM_STAT_ANY],,,[#include <sys/sem.h>])
+AC_CHECK_DECLS([MREMAP_DONTUNMAP],,,[#include <linux/mman.h>])
 
 AC_CHECK_HEADERS_ONCE([ \
     aio.h \
runtest/syscalls +1 -0
index 4b284f279..6d9a4f1d2 100644
@@ -920,6 +920,7 @@ mremap03 mremap03
 mremap04 mremap04
 mremap05 mremap05
 mremap06 mremap06
+mremap07 mremap07
 
 mseal01 mseal01
 mseal02 mseal02
testcases/kernel/syscalls/mremap/.gitignore +1 -0
index ec15a19cd..292899e03 100644
@@ -4,3 +4,4 @@
 /mremap04
 /mremap05
 /mremap06
+/mremap07
testcases/kernel/syscalls/mremap/Makefile +1 -0
index 9f5aca9ec..8811b887e 100644
@@ -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
testcases/kernel/syscalls/mremap/mremap07.c new file +159 -0

159 lines not on this page · raw message

Signature · 5 lines
2.51.0


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

Hi Wei,

...

testcases/kernel/syscalls/mremap/mremap07.c line 122

Quoted 8 lines
testcases/kernel/syscalls/mremap/mremap07.c new file +8 -0
+static void run(void)
+{
+	pthread_t handler_thread;
+
+	SAFE_PTHREAD_CREATE(&handler_thread, NULL,
+		(void * (*)(void *))fault_handler_thread, NULL);
+
+#if HAVE_DECL_MREMAP_DONTUNMAP

This is a compile check, whole test should be wrapped with #ifdef and fallback using TST_TEST_TCONF() macro.

#include "config.h"
#include "tst_test.h"
...
#ifdef HAVE_DECL_MREMAP_DONTUNMAP
// other includes

#else
TST_TEST_TCONF("Missing MREMAP_DONTUNMAP in <linux/mman.h>"); #endif

This quits tests early.
And please use #ifdef.

Kind regards,
Petr

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

Hi Wei,

...

testcases/kernel/syscalls/mremap/mremap07.c line 122

Quoted 8 lines
testcases/kernel/syscalls/mremap/mremap07.c new file +8 -0
+static void run(void)
+{
+	pthread_t handler_thread;
+
+	SAFE_PTHREAD_CREATE(&handler_thread, NULL,
+		(void * (*)(void *))fault_handler_thread, NULL);
+
+#if HAVE_DECL_MREMAP_DONTUNMAP

This is a compile check, whole test should be wrapped with #ifdef and fallback using TST_TEST_TCONF() macro.

#include "config.h"
#include "tst_test.h"
...
#ifdef HAVE_DECL_MREMAP_DONTUNMAP
// other includes

Quoted 5 lines

#else
TST_TEST_TCONF("Missing MREMAP_DONTUNMAP in <linux/mman.h>"); #endif

This quits tests early.

Good.

Quoted 1 line

And please use #ifdef.

When MREMAP_DONTUNMAP can not be found config.h will contain:
#define HAVE_DECL_MREMAP_DONTUNMAP 0
So i suppose we should use #if instead of #ifdef otherwise compile issue will happen on old platform.
If you agree then i will prepare new patch.
Thanks.

Quoted 3 lines

Kind regards,
Petr

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

Hi Wei,

...

Quoted 6 lines
Quoted 1 line

And please use #ifdef.

When MREMAP_DONTUNMAP can not be found config.h will contain:
#define HAVE_DECL_MREMAP_DONTUNMAP 0

So i suppose we should use #if instead of #ifdef otherwise compile issue will happen on old platform.

Indeed declarations are really defined (instead of commented out undef). I'm sorry for a wrong hint.

Kind regards,
Petr

Quoted 5 lines

If you agree then i will prepare new patch.
Thanks.

Quoted 2 lines

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw
Quoted 4 lines · Petr Vorel

Hi Wei,

...

Quoted 1 line
Quoted 1 line

And please use #ifdef.

Quoted 8 lines
Quoted 5 lines

When MREMAP_DONTUNMAP can not be found config.h will contain:
#define HAVE_DECL_MREMAP_DONTUNMAP 0

So i suppose we should use #if instead of #ifdef otherwise compile issue will happen on old platform.

Indeed declarations are really defined (instead of commented out undef). I'm sorry for a wrong hint.

No worry at all, thanks for confirming the correct way. That's help :)

Quoted 9 lines

Kind regards,
Petr

Quoted 5 lines

If you agree then i will prepare new patch.
Thanks.

Quoted 2 lines

Kind regards,
Petr

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

PATCH v3 1/1 mremap07.c: New case check mremap with MREMAP_DONTUNMAP

In reply to Wei Gao via ltp

This case test mremap() with MREMAP_DONTUNMAP and use userfaultfd verifies fault which triggered by accessing old memory region.

Fixes #1168
---
 configure.ac                                |   1 +
 runtest/syscalls                            |   1 +
 testcases/kernel/syscalls/mremap/.gitignore |   1 +
 testcases/kernel/syscalls/mremap/Makefile   |   1 +
 testcases/kernel/syscalls/mremap/mremap07.c | 161 ++++++++++++++++++++
 5 files changed, 165 insertions(+)
 create mode 100644 testcases/kernel/syscalls/mremap/mremap07.c
configure.ac +1 -0
index 0480f46ca..df842c9e4 100644
@@ -46,6 +46,7 @@ AC_CHECK_DECLS([MADV_MERGEABLE],,,[#include <sys/mman.h>])
 AC_CHECK_DECLS([NFTA_CHAIN_ID, NFTA_VERDICT_CHAIN_ID],,,[#include <linux/netfilter/nf_tables.h>])
 AC_CHECK_DECLS([PR_CAPBSET_DROP, PR_CAPBSET_READ],,,[#include <sys/prctl.h>])
 AC_CHECK_DECLS([SEM_STAT_ANY],,,[#include <sys/sem.h>])
+AC_CHECK_DECLS([MREMAP_DONTUNMAP],,,[#include <linux/mman.h>])
 
 AC_CHECK_HEADERS_ONCE([ \
     aio.h \
runtest/syscalls +1 -0
index 4b284f279..6d9a4f1d2 100644
@@ -920,6 +920,7 @@ mremap03 mremap03
 mremap04 mremap04
 mremap05 mremap05
 mremap06 mremap06
+mremap07 mremap07
 
 mseal01 mseal01
 mseal02 mseal02
testcases/kernel/syscalls/mremap/.gitignore +1 -0
index ec15a19cd..292899e03 100644
@@ -4,3 +4,4 @@
 /mremap04
 /mremap05
 /mremap06
+/mremap07
testcases/kernel/syscalls/mremap/Makefile +1 -0
index 9f5aca9ec..8811b887e 100644
@@ -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
testcases/kernel/syscalls/mremap/mremap07.c new file +161 -0

161 lines not on this page · raw message

Signature · 5 lines
2.51.0


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

Hi Wei,

Quoted 2 lines

This case test mremap() with MREMAP_DONTUNMAP and use userfaultfd verifies fault which triggered by accessing old memory region.

nit: Having a changelog would help reviewing.

...

configure.ac AC_CHECK_DECLS([MADV_MERGEABLE],,,[#include <sys/mman.h>]) · line 49

Quoted 6 lines

+++ b/configure.ac

configure.ac +1 -0
@@ -46,6 +46,7 @@ AC_CHECK_DECLS([MADV_MERGEABLE],,,[#include <sys/mman.h>])
 AC_CHECK_DECLS([NFTA_CHAIN_ID, NFTA_VERDICT_CHAIN_ID],,,[#include <linux/netfilter/nf_tables.h>])
 AC_CHECK_DECLS([PR_CAPBSET_DROP, PR_CAPBSET_READ],,,[#include <sys/prctl.h>])
 AC_CHECK_DECLS([SEM_STAT_ANY],,,[#include <sys/sem.h>])
+AC_CHECK_DECLS([MREMAP_DONTUNMAP],,,[#include <linux/mman.h>])

Obviously AC_CHECK_DECLS() does not require to define _GNU_SOURCE, interesting.

...

testcases/kernel/syscalls/mremap/mremap07.c line 11

Quoted 13 lines
testcases/kernel/syscalls/mremap/mremap07.c new file +12 -0
+++ b/testcases/kernel/syscalls/mremap/mremap07.c
@@ -0,0 +1,161 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2025 Wei Gao <[email protected]>
+ */
+
+/*\
+ * LTP test case for mremap() with MREMAP_DONTUNMAP and userfaultfd.
+ *
+ * This test verifies a fault is triggered on the old memory region
+ * and is then correctly handled by a userfaultfd handler.
+ */

You should include config.h if you want to check for HAVE_DECL_MREMAP_DONTUNMAP:
#include "config.h"

This works just because some header already do it, but that can change in the future.

testcases/kernel/syscalls/mremap/mremap07.c line 21

Quoted 9 lines
testcases/kernel/syscalls/mremap/mremap07.c new file +9 -0
+#define _GNU_SOURCE
+#include <poll.h>
+#include <pthread.h>
+
+#include "tst_test.h"
+#include "tst_safe_pthread.h"
+#include "lapi/userfaultfd.h"
+
+#if HAVE_DECL_MREMAP_DONTUNMAP

Interesting, you don't include <linux/mman.h>, which should have MREMAP_DONTUNMAP, but the check works as expected. But I would still prefer to include <linux/mman.h>.

testcases/kernel/syscalls/mremap/mremap07.c line 33

Quoted 11 lines
testcases/kernel/syscalls/mremap/mremap07.c new file +11 -0
+static int page_size;
+static int uffd;
+static char *fault_addr;
+static char *new_remap_addr;
+
+static const char *test_string = "Hello, world! This is a test string that fills up a page.";
+
+static int sys_userfaultfd(int flags)
+{
+	return tst_syscall(__NR_userfaultfd, flags);
+}

This is copy pasted from userfaultfd01.c. I factored it out to include/lapi/userfaultfd.h, could you please use it in the next version?

https://lore.kernel.org/ltp/[email protected]/
https://patchwork.ozlabs.org/project/ltp/patch/[email protected]/

testcases/kernel/syscalls/mremap/mremap07.c line 47

Quoted 13 lines
testcases/kernel/syscalls/mremap/mremap07.c new file +13 -0
+static void fault_handler_thread(void)
+{
+	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);

Interesting, we still don't have safe_poll().

testcases/kernel/syscalls/mremap/mremap07.c line 49

Quoted 1 line
testcases/kernel/syscalls/mremap/mremap07.c new file +1 -0
+	if (nready <= 0)

man poll(2) says:
return value of zero indicates that the system call timed out before any file descriptors became ready. userfaultfd01.c checks only for nready == -1, I'm not sure maybe it should also check for 0. But if you also check for 0, maybe printing nready would be useful (OTOH TERRNO
prints SUCCESS(0)).

testcases/kernel/syscalls/mremap/mremap07.c line 50

Quoted 1 line
testcases/kernel/syscalls/mremap/mremap07.c new file +1 -0
+		tst_brk(TBROK | TERRNO, "Poll on uffd failed");

Maybe just poll() failed?

Quoted 6 lines
testcases/kernel/syscalls/mremap/mremap07.c new file +6 -0
+
+	SAFE_READ(1, uffd, &msg, sizeof(msg));
+
+	if (msg.event != UFFD_EVENT_PAGEFAULT)
+		tst_brk(TBROK, "Received unexpected UFFD_EVENT: %d", msg.event);
+

testcases/kernel/syscalls/mremap/mremap07.c line 66

Quoted 10 lines
testcases/kernel/syscalls/mremap/mremap07.c new file +10 -0
+	if ((char *)msg.arg.pagefault.address != fault_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_remap_addr;
+	uffdio_copy.dst = (unsigned long)fault_addr;
+	uffdio_copy.len = page_size;
+	uffdio_copy.mode = 0;
+	uffdio_copy.copy = 0;

Most of this code is the same as in userfaultfd01.c, but I don't see a way to factor it out more than what I sent in my patch.

testcases/kernel/syscalls/mremap/mremap07.c line 69

Quoted 3 lines
testcases/kernel/syscalls/mremap/mremap07.c new file +3 -0
+
+	SAFE_IOCTL(uffd, UFFDIO_COPY, &uffdio_copy);
+	tst_res(TPASS, "Userfaultfd handler successfully handled the fault.");

very nit: please avoid '.' at the end.

Quoted 7 lines
testcases/kernel/syscalls/mremap/mremap07.c new file +7 -0
+}
+
+static void setup(void)
+{
+	page_size = getpagesize();
+	struct uffdio_api uffdio_api;
+	struct uffdio_register uffdio_register;

testcases/kernel/syscalls/mremap/mremap07.c line 86

Quoted 10 lines
testcases/kernel/syscalls/mremap/mremap07.c new file +10 -0
+
+	TEST(sys_userfaultfd(O_CLOEXEC | O_NONBLOCK));
+	if (TST_RET == -1) {
+		if (TST_ERR == EPERM) {
+			tst_res(TCONF, "Hint: check /proc/sys/vm/unprivileged_userfaultfd");
+			tst_brk(TCONF | TTERRNO, "userfaultfd() requires CAP_SYS_PTRACE on this system");
+		} else {
+			tst_brk(TBROK | TTERRNO, "Could not create userfault file descriptor");
+		}
+	}

This would be also replaced by my patch, only this would be used:
uffd = SAFE_USERFAULTFD(O_CLOEXEC | O_NONBLOCK, true);

testcases/kernel/syscalls/mremap/mremap07.c line 93

Quoted 7 lines
testcases/kernel/syscalls/mremap/mremap07.c new file +7 -0
+
+	uffd = TST_RET;
+	uffdio_api.api = UFFD_API;
+	uffdio_api.features = 0;
+	SAFE_IOCTL(uffd, UFFDIO_API, &uffdio_api);
+
+	fault_addr = SAFE_MMAP(NULL, page_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);

nit: maybe split long line?

Quoted 21 lines
testcases/kernel/syscalls/mremap/mremap07.c new file +21 -0
+
+	tst_res(TINFO, "Original mapping created at %p", (void *)fault_addr);
+
+	strcpy(fault_addr, "ABCD");
+
+	uffdio_register.range.start = (unsigned long)fault_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_remap_addr != NULL)
+		SAFE_MUNMAP(new_remap_addr, page_size);
+
+	if (fault_addr != NULL)
+		SAFE_MUNMAP(fault_addr, page_size);
+
+	if (uffd != -1)
+		SAFE_CLOSE(uffd);

testcases/kernel/syscalls/mremap/mremap07.c line 124

Quoted 10 lines
testcases/kernel/syscalls/mremap/mremap07.c new file +10 -0
+}
+
+static void run(void)
+{
+	pthread_t handler_thread;
+
+	SAFE_PTHREAD_CREATE(&handler_thread, NULL,
+		(void * (*)(void *))fault_handler_thread, NULL);
+
+	new_remap_addr = mremap(fault_addr, page_size, page_size, MREMAP_DONTUNMAP | MREMAP_MAYMOVE);

nit: long line.

testcases/kernel/syscalls/mremap/mremap07.c line 136

Quoted 12 lines
testcases/kernel/syscalls/mremap/mremap07.c new file +12 -0
+
+	if (new_remap_addr == MAP_FAILED)
+		tst_brk(TBROK | TTERRNO, "mremap failed");
+
+	tst_res(TINFO, "New mapping created at %p", (void *)new_remap_addr);
+
+	strcpy(new_remap_addr, test_string);
+
+	TST_CHECKPOINT_WAKE(0);
+
+	tst_res(TINFO, "Main thread accessing old address %p to trigger fault. Access Content is \"%s\"",
+			(void *)fault_addr, fault_addr);

nit: could we remove "Access Content is \"%s\"" ? It's not important for the result if it pass (you print it also on the failure) and line is too long.

testcases/kernel/syscalls/mremap/mremap07.c line 141

Quoted 5 lines
testcases/kernel/syscalls/mremap/mremap07.c new file +5 -0
+
+	SAFE_PTHREAD_JOIN(handler_thread, NULL);
+
+	if (strcmp(fault_addr, test_string) != 0)
+		tst_res(TFAIL, "Verification failed: Content at old address is '%s', expected '%s'", fault_addr, test_string);

Maybe s/at/of the/ ?

testcases/kernel/syscalls/mremap/mremap07.c line 143

Quoted 2 lines
testcases/kernel/syscalls/mremap/mremap07.c new file +2 -0
+	else
+		tst_res(TPASS, "Verification passed: Content at old address is correct after fault handling.");

nit: '.' at the end.

testcases/kernel/syscalls/mremap/mremap07.c line 156

Quoted 13 lines
testcases/kernel/syscalls/mremap/mremap07.c new file +13 -0
+}
+
+static struct tst_test test = {
+	.test_all = run,
+	.setup = setup,
+	.needs_checkpoints = 1,
+	.cleanup = cleanup,
+	.needs_root = 1,
+	.needs_kconfigs = (const char *[]) {
+		"CONFIG_USERFAULTFD=y",
+		NULL,
+	},
+	.min_kver = "5.7",

I wonder if we check for MREMAP_DONTUNMAP whether we need to have also explicit check for MREMAP_DONTUNMAP. But sure, it's safer to have runtime kernel check and check that it was not compiled with an old headers.

Kind regards,
Petr

Quoted 5 lines
testcases/kernel/syscalls/mremap/mremap07.c new file +5 -0
+};
+
+#else
+TST_TEST_TCONF("Missing MREMAP_DONTUNMAP in <linux/mman.h>");
+#endif
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#8 lore raw

PATCH v4 1/1 mremap07.c: New case check mremap with MREMAP_DONTUNMAP

In reply to Wei Gao via ltp

This case test mremap() with MREMAP_DONTUNMAP and use userfaultfd verifies fault which triggered by accessing old memory region.

changelog v4->v3:
1) Add explicit volatile memory access to trigger page fault.
2) Use SAFE_USERFAULTFD and add config.h/linux/mman.h.
3) Logic: Distinguish poll() timeout from failure.
4) Style: Fix long lines.

Fixes #1168
---
 configure.ac                                |   1 +
 runtest/syscalls                            |   1 +
 testcases/kernel/syscalls/mremap/.gitignore |   1 +
 testcases/kernel/syscalls/mremap/Makefile   |   1 +
 testcases/kernel/syscalls/mremap/mremap07.c | 158 ++++++++++++++++++++
 5 files changed, 162 insertions(+)
 create mode 100644 testcases/kernel/syscalls/mremap/mremap07.c
configure.ac +1 -0
index 7fa614dcb..c836ef7f3 100644
@@ -46,6 +46,7 @@ AC_CHECK_DECLS([MADV_MERGEABLE],,,[#include <sys/mman.h>])
 AC_CHECK_DECLS([NFTA_CHAIN_ID, NFTA_VERDICT_CHAIN_ID],,,[#include <linux/netfilter/nf_tables.h>])
 AC_CHECK_DECLS([PR_CAPBSET_DROP, PR_CAPBSET_READ],,,[#include <sys/prctl.h>])
 AC_CHECK_DECLS([SEM_STAT_ANY],,,[#include <sys/sem.h>])
+AC_CHECK_DECLS([MREMAP_DONTUNMAP],,,[#include <linux/mman.h>])
 
 AC_CHECK_HEADERS_ONCE([ \
     aio.h \
runtest/syscalls +1 -0
index 2f629e4e4..9d7c6c32e 100644
@@ -925,6 +925,7 @@ mremap03 mremap03
 mremap04 mremap04
 mremap05 mremap05
 mremap06 mremap06
+mremap07 mremap07
 
 mseal01 mseal01
 mseal02 mseal02
testcases/kernel/syscalls/mremap/.gitignore +1 -0
index ec15a19cd..292899e03 100644
@@ -4,3 +4,4 @@
 /mremap04
 /mremap05
 /mremap06
+/mremap07
testcases/kernel/syscalls/mremap/Makefile +1 -0
index 9f5aca9ec..8811b887e 100644
@@ -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
testcases/kernel/syscalls/mremap/mremap07.c new file +158 -0

158 lines not on this page · raw message

Signature · 5 lines
2.52.0


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

Hi Wei,

The overall structure looks good, but there are a few bugs that need fixing before this can be merged.

Quoted 1 line

+static int uffd;

This is zero-initialized by the compiler, but fd 0 is a valid file descriptor (stdin). cleanup() guards with if (uffd != -1), so if setup() fails before SAFE_USERFAULTFD assigns a real fd, cleanup() will call SAFE_CLOSE(0) and close stdin. Initialize it to -1:

  static int uffd = -1;
Quoted 1 line

+static void fault_handler_thread(void)

pthread start routines must have the signature void *(*)(void *). Casting a void (*)(void) function to that type and calling it through the incompatible pointer is undefined behavior. Please use the correct signature:

  static void *fault_handler_thread(void *arg LTP_ATTRIBUTE_UNUSED)
  {
          ...
          return NULL;
  }

testcases/kernel/syscalls/mremap/mremap07.c line 103

Quoted 5 lines
testcases/kernel/syscalls/mremap/mremap07.c new file +5 -0
+	if (new_remap_addr != NULL)
+		SAFE_MUNMAP(new_remap_addr, page_size);
+
+	if (fault_addr != NULL)
+		SAFE_MUNMAP(fault_addr, page_size);

Kernel coding style: drop the != NULL, write if (new_remap_addr) and if (fault_addr). checkpatch also flags these.

testcases/kernel/syscalls/mremap/mremap07.c line 120

Quoted 8 lines
testcases/kernel/syscalls/mremap/mremap07.c new file +8 -0
+	SAFE_PTHREAD_CREATE(&handler_thread, NULL,
+		(void * (*)(void *))fault_handler_thread, NULL);
+
+	new_remap_addr = mremap(fault_addr, page_size, page_size,
+		MREMAP_DONTUNMAP | MREMAP_MAYMOVE);
+
+	if (new_remap_addr == MAP_FAILED)
+		tst_brk(TBROK | TTERRNO, "mremap failed");

Two issues here:

1. TTERRNO prints TST_ERR, which is the errno captured by the TEST() macro. mremap() is called directly, so TST_ERR is stale. Use TERRNO (system errno) instead.

2. new_remap_addr is a static variable that is set here but only freed in cleanup(). When the test is run with -i N for N > 1, each iteration overwrites new_remap_addr with a fresh mremap result, and the previous iteration's mapping is leaked. Add an explicit munmap at the end of run() and reset the pointer:

     SAFE_MUNMAP(new_remap_addr, page_size);
     new_remap_addr = NULL;

testcases/kernel/syscalls/mremap/mremap07.c line 148

Quoted 1 line
testcases/kernel/syscalls/mremap/mremap07.c new file +1 -0
+	.needs_root = 1,

This is not needed. SAFE_USERFAULTFD() is called with retry=true, which automatically retries with UFFD_USER_MODE_ONLY when EPERM is returned. Everything this test does (mmap/mremap, UFFDIO_REGISTER, UFFDIO_COPY on user pages) works without root on kernels >= 5.11. On 5.7--5.10 the retry will result in TCONF, which is the correct behavior. Please drop .needs_root.

Regards,
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

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

PATCH v5 1/1 mremap07.c: New case check mremap with MREMAP_DONTUNMAP

In reply to Wei Gao via ltp

This case test mremap() with MREMAP_DONTUNMAP and use userfaultfd verifies fault which triggered by accessing old memory region.

Fixes #1168

---
v4->v5:
1)Added NULL as the 5th argument to mremap(), otherwise mremap will return EINVAL, latest kernel will check new address is aligned or not. 2)Initialized uffd to -1
3)Updated fault_handler_thread to return void *
4)Enhanced the cleanup() function to verify that pointers are not MAP_FAILED
5)Switched from TTERRNO to TERRNO in the mremap check 6)Removed .needs_root = 1

 configure.ac                                |   1 +
 runtest/syscalls                            |   1 +
 testcases/kernel/syscalls/mremap/.gitignore |   1 +
 testcases/kernel/syscalls/mremap/Makefile   |   1 +
 testcases/kernel/syscalls/mremap/mremap07.c | 160 ++++++++++++++++++++
 5 files changed, 164 insertions(+)
 create mode 100644 testcases/kernel/syscalls/mremap/mremap07.c
configure.ac +1 -0
index 7fa614dcb..c836ef7f3 100644
@@ -46,6 +46,7 @@ AC_CHECK_DECLS([MADV_MERGEABLE],,,[#include <sys/mman.h>])
 AC_CHECK_DECLS([NFTA_CHAIN_ID, NFTA_VERDICT_CHAIN_ID],,,[#include <linux/netfilter/nf_tables.h>])
 AC_CHECK_DECLS([PR_CAPBSET_DROP, PR_CAPBSET_READ],,,[#include <sys/prctl.h>])
 AC_CHECK_DECLS([SEM_STAT_ANY],,,[#include <sys/sem.h>])
+AC_CHECK_DECLS([MREMAP_DONTUNMAP],,,[#include <linux/mman.h>])
 
 AC_CHECK_HEADERS_ONCE([ \
     aio.h \
runtest/syscalls +1 -0
index 2f629e4e4..9d7c6c32e 100644
@@ -925,6 +925,7 @@ mremap03 mremap03
 mremap04 mremap04
 mremap05 mremap05
 mremap06 mremap06
+mremap07 mremap07
 
 mseal01 mseal01
 mseal02 mseal02
testcases/kernel/syscalls/mremap/.gitignore +1 -0
index ec15a19cd..292899e03 100644
@@ -4,3 +4,4 @@
 /mremap04
 /mremap05
 /mremap06
+/mremap07
testcases/kernel/syscalls/mremap/Makefile +1 -0
index 9f5aca9ec..8811b887e 100644
@@ -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
testcases/kernel/syscalls/mremap/mremap07.c new file +160 -0

160 lines not on this page · raw message

Signature · 5 lines
2.52.0


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

Hi Wei,

we are almost there to merge. A few comments below..

Quoted 2 lines

This case test mremap() with MREMAP_DONTUNMAP and use userfaultfd verifies fault which triggered by accessing old memory region.

English can be improved a bit here:

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.

Review

Quoted 2 lines
(quoted diff) +2 -0
+static void *fault_handler_thread(void *arg LTP_ATTRIBUTE_UNUSED)
+{

Missing return NULL; in this function.

testcases/kernel/syscalls/mremap/mremap07.c line 141

Quoted 7 lines
testcases/kernel/syscalls/mremap/mremap07.c new file +7 -0
+	if (strcmp(fault_addr, test_string) != 0)
+		tst_res(TFAIL, "Verification failed: Content of the old "
+			"address is '%s', expected '%s'", fault_addr, test_string);
+	else
+		tst_res(TPASS, "Verification passed: Content of the old "
+			"address is correct after fault handling");
+

We have TST_EXP_EQ_STR(fault_addr, test_string).

The rest is ok.

Regards,
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

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

PATCH v6 1/1 mremap07.c: New case check mremap with MREMAP_DONTUNMAP

In reply to Wei Gao via ltp

This case test mremap() with MREMAP_DONTUNMAP and use userfaultfd verifies fault which triggered by accessing old memory region.

---
v5->v6:
- Refined the test description header
- Added a missing return NULL to the fault_handler_thread
- Replaced a manual strcmp and tst_res conditional block with the TST_EXP_EQ_STR macro

 configure.ac                                |   1 +
 runtest/syscalls                            |   1 +
 testcases/kernel/syscalls/mremap/.gitignore |   1 +
 testcases/kernel/syscalls/mremap/Makefile   |   1 +
 testcases/kernel/syscalls/mremap/mremap07.c | 158 ++++++++++++++++++++
 5 files changed, 162 insertions(+)
 create mode 100644 testcases/kernel/syscalls/mremap/mremap07.c
configure.ac +1 -0
index 812f17d8b..211aaa8ad 100644
@@ -46,6 +46,7 @@ AC_CHECK_DECLS([MADV_MERGEABLE],,,[#include <sys/mman.h>])
 AC_CHECK_DECLS([NFTA_CHAIN_ID, NFTA_VERDICT_CHAIN_ID],,,[#include <linux/netfilter/nf_tables.h>])
 AC_CHECK_DECLS([PR_CAPBSET_DROP, PR_CAPBSET_READ],,,[#include <sys/prctl.h>])
 AC_CHECK_DECLS([SEM_STAT_ANY],,,[#include <sys/sem.h>])
+AC_CHECK_DECLS([MREMAP_DONTUNMAP],,,[#include <linux/mman.h>])
 
 AC_CHECK_HEADERS_ONCE([ \
     aio.h \
runtest/syscalls +1 -0
index d72fceb5e..b9a9ce5fe 100644
@@ -927,6 +927,7 @@ mremap03 mremap03
 mremap04 mremap04
 mremap05 mremap05
 mremap06 mremap06
+mremap07 mremap07
 
 mseal01 mseal01
 mseal02 mseal02
testcases/kernel/syscalls/mremap/.gitignore +1 -0
index ec15a19cd..292899e03 100644
@@ -4,3 +4,4 @@
 /mremap04
 /mremap05
 /mremap06
+/mremap07
testcases/kernel/syscalls/mremap/Makefile +1 -0
index 9f5aca9ec..8811b887e 100644
@@ -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
testcases/kernel/syscalls/mremap/mremap07.c new file +158 -0

158 lines not on this page · raw message

Signature · 5 lines
2.52.0


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

PATCH v7 1/1 mremap07.c: New test for mremap() with MREMAP_DONTUNMAP

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.

---
v6->v7:
- Update commit description
- Update year of copyright

 configure.ac                                |   1 +
 runtest/syscalls                            |   1 +
 testcases/kernel/syscalls/mremap/.gitignore |   1 +
 testcases/kernel/syscalls/mremap/Makefile   |   1 +
 testcases/kernel/syscalls/mremap/mremap07.c | 158 ++++++++++++++++++++
 5 files changed, 162 insertions(+)
 create mode 100644 testcases/kernel/syscalls/mremap/mremap07.c
configure.ac +1 -0
index 812f17d8b..211aaa8ad 100644
@@ -46,6 +46,7 @@ AC_CHECK_DECLS([MADV_MERGEABLE],,,[#include <sys/mman.h>])
 AC_CHECK_DECLS([NFTA_CHAIN_ID, NFTA_VERDICT_CHAIN_ID],,,[#include <linux/netfilter/nf_tables.h>])
 AC_CHECK_DECLS([PR_CAPBSET_DROP, PR_CAPBSET_READ],,,[#include <sys/prctl.h>])
 AC_CHECK_DECLS([SEM_STAT_ANY],,,[#include <sys/sem.h>])
+AC_CHECK_DECLS([MREMAP_DONTUNMAP],,,[#include <linux/mman.h>])
 
 AC_CHECK_HEADERS_ONCE([ \
     aio.h \
runtest/syscalls +1 -0
index d72fceb5e..b9a9ce5fe 100644
@@ -927,6 +927,7 @@ mremap03 mremap03
 mremap04 mremap04
 mremap05 mremap05
 mremap06 mremap06
+mremap07 mremap07
 
 mseal01 mseal01
 mseal02 mseal02
testcases/kernel/syscalls/mremap/.gitignore +1 -0
index ec15a19cd..292899e03 100644
@@ -4,3 +4,4 @@
 /mremap04
 /mremap05
 /mremap06
+/mremap07
testcases/kernel/syscalls/mremap/Makefile +1 -0
index 9f5aca9ec..8811b887e 100644
@@ -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
testcases/kernel/syscalls/mremap/mremap07.c new file +158 -0

158 lines not on this page · raw message

Signature · 5 lines
2.52.0


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

Re: [LTP] mremap07.c: New test for mremap() with MREMAP_DONTUNMAP

Hi Wei,

Quoted 3 lines · Wei Gao

mremap07.c: New test for mremap() with MREMAP_DONTUNMAP

Use Closes: for GitHub issue references. Fixes: is reserved for commit SHA references in the form: Fixes: <hash> ("<subject>").

[...]

testcases/kernel/syscalls/mremap/mremap07.c line 110

Quoted 11 lines
testcases/kernel/syscalls/mremap/mremap07.c new file +11 -0
+static void cleanup(void)
+{
+	if (new_remap_addr && new_remap_addr != MAP_FAILED)
+		SAFE_MUNMAP(new_remap_addr, page_size);
+
+	if (fault_addr && fault_addr != MAP_FAILED)
+		SAFE_MUNMAP(fault_addr, page_size);
+
+	if (uffd != -1)
+		SAFE_CLOSE(uffd);
+}

SAFE_MUNMAP does not clear the pointer. If setup() fails on a subsequent iteration before SAFE_MMAP reassigns fault_addr, cleanup() will double-munmap the stale pointer. Add fault_addr = NULL; after the SAFE_MUNMAP call.

---
Note:

Our agent completed the review of the patch. The full review can be found at: https://github.com/linux-test-project/ltp-agent/actions/runs/24554018117

The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer

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

Re: [LTP] mremap07.c: New test for mremap() with MREMAP_DONTUNMAP

Quoted 4 lines

SAFE_MUNMAP does not clear the pointer. If setup() fails on a subsequent iteration before SAFE_MMAP reassigns fault_addr, cleanup() will double-munmap the stale pointer. Add fault_addr = NULL; after the SAFE_MUNMAP call.

The SAFE_MUNMAP() call already set the pointer to NULL, it didn't catch this error. I will fix it in the agent config.

--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

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

PATCH v8 1/1 mremap07.c: New test for mremap() with MREMAP_DONTUNMAP

In reply to Wei Gao via 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.

---
v7 -> v8:
- Change Fixes: tag to Closes: for GitHub issue reference.

 configure.ac                                |   1 +
 runtest/syscalls                            |   1 +
 testcases/kernel/syscalls/mremap/.gitignore |   1 +
 testcases/kernel/syscalls/mremap/Makefile   |   1 +
 testcases/kernel/syscalls/mremap/mremap07.c | 158 ++++++++++++++++++++
 5 files changed, 162 insertions(+)
 create mode 100644 testcases/kernel/syscalls/mremap/mremap07.c
configure.ac +1 -0
index 812f17d8b..211aaa8ad 100644
@@ -46,6 +46,7 @@ AC_CHECK_DECLS([MADV_MERGEABLE],,,[#include <sys/mman.h>])
 AC_CHECK_DECLS([NFTA_CHAIN_ID, NFTA_VERDICT_CHAIN_ID],,,[#include <linux/netfilter/nf_tables.h>])
 AC_CHECK_DECLS([PR_CAPBSET_DROP, PR_CAPBSET_READ],,,[#include <sys/prctl.h>])
 AC_CHECK_DECLS([SEM_STAT_ANY],,,[#include <sys/sem.h>])
+AC_CHECK_DECLS([MREMAP_DONTUNMAP],,,[#include <linux/mman.h>])
 
 AC_CHECK_HEADERS_ONCE([ \
     aio.h \
runtest/syscalls +1 -0
index d72fceb5e..b9a9ce5fe 100644
@@ -927,6 +927,7 @@ mremap03 mremap03
 mremap04 mremap04
 mremap05 mremap05
 mremap06 mremap06
+mremap07 mremap07
 
 mseal01 mseal01
 mseal02 mseal02
testcases/kernel/syscalls/mremap/.gitignore +1 -0
index ec15a19cd..292899e03 100644
@@ -4,3 +4,4 @@
 /mremap04
 /mremap05
 /mremap06
+/mremap07
testcases/kernel/syscalls/mremap/Makefile +1 -0
index 9f5aca9ec..8811b887e 100644
@@ -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
testcases/kernel/syscalls/mremap/mremap07.c new file +158 -0

158 lines not on this page · raw message

Signature · 5 lines
2.52.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by LTP AI Reviewer on v8 1/1
#17 lore raw

Re: [LTP] mremap07.c: New test for mremap() with MREMAP_DONTUNMAP

Hi Wei,

testcases/kernel/syscalls/mremap/mremap07.c line 45

Quoted 12 lines · Wei Gao

mremap07.c: New test for mremap() with MREMAP_DONTUNMAP

testcases/kernel/syscalls/mremap/mremap07.c new file +10 -0
+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;
+
+	int nready = poll(&pollfd, 1, -1);

Nit: declare all variables at the top of the block, before any statements (struct pollfd and int nready should move above TST_CHECKPOINT_WAIT(0)).

[...]

---
Note:

Our agent completed the review of the patch. The full review can be found at: <review_url>

The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer

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

Re: [LTP] mremap07.c: New test for mremap() with MREMAP_DONTUNMAP

Quoted 5 lines · On Fri, Apr 17, 2026 at 01:26:49PM +0000, [email protected]…

Hi Wei,

Quoted 1 line · Wei Gao

mremap07.c: New test for mremap() with MREMAP_DONTUNMAP

Quoted 13 lines

testcases/kernel/syscalls/mremap/mremap07.c line 45

Quoted 10 lines
testcases/kernel/syscalls/mremap/mremap07.c new file +10 -0
+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;
+
+	int nready = poll(&pollfd, 1, -1);

Nit: declare all variables at the top of the block, before any statements (struct pollfd and int nready should move above TST_CHECKPOINT_WAIT(0)).

Unless the LTP project strictly mandates a "declarations at the top" policy for tests, I would prefer to keep the current implementation.

Quoted 17 lines

[...]

---
Note:

Our agent completed the review of the patch. The full review can be found at: <review_url>

The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer

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

Re: [LTP] [PATCH v8] mremap07.c: New test for mremap() with MREMAP_DONTUNMAP

In reply to Wei Gao via ltp

Hi Wei,

This is an excellent test design, but still few places need refactoring in my opinion.

configure.ac AC_CHECK_DECLS([MADV_MERGEABLE],,,[#include <sys/mman.h>]) · line 49

Quoted 7 lines · Wei Gao via ltp
configure.ac +1 -0
@@ -46,6 +46,7 @@ AC_CHECK_DECLS([MADV_MERGEABLE],,,[#include <sys/mman.h>])
 AC_CHECK_DECLS([NFTA_CHAIN_ID, NFTA_VERDICT_CHAIN_ID],,,[#include <linux/netfilter/nf_tables.h>])
 AC_CHECK_DECLS([PR_CAPBSET_DROP, PR_CAPBSET_READ],,,[#include <sys/prctl.h>])
 AC_CHECK_DECLS([SEM_STAT_ANY],,,[#include <sys/sem.h>])
+AC_CHECK_DECLS([MREMAP_DONTUNMAP],,,[#include <linux/mman.h>])

I didn't go through all the previous discussion, but can we define MREMAP_DONTUNMAP in the lapi/mmap.h then remove the .min_kver and TST_TEST_TCONF?

Quoted 1 line
configure.ac +1 -0
+++ b/testcases/kernel/syscalls/mremap/mremap07.c

...

Quoted 7 lines
configure.ac +7 -0
+#define _GNU_SOURCE
+#include <poll.h>
+#include <pthread.h>
+#include <linux/mman.h>
+
+#include "tst_test.h"
+#include "tst_safe_pthread.h"

configure.ac

Quoted 10 lines
configure.ac +9 -0
+#include "lapi/userfaultfd.h"
+#include "config.h"
+
+#if HAVE_DECL_MREMAP_DONTUNMAP
+
+static int page_size;
+static int uffd = -1;
 
+static char *fault_addr;
+static char *new_remap_addr;

The two variables are not clear enough, I would suggest using old_addr, new_addr directly.

Quoted 1 line

+static const char *test_string = "Hello, world! This is a test string that fills up a page.";

We can just define in macro:

    #define TEST_STRING_A "ABCD"
    #define TEST_STRING_B "EFGH"
Quoted 4 lines
configure.ac +4 -0
+static void setup(void)
+{
+       page_size = getpagesize();
+       struct uffdio_api uffdio_api;

configure.ac

Quoted 10 lines
configure.ac +10 -0
+       struct uffdio_register uffdio_register;
+
+       uffd = SAFE_USERFAULTFD(O_CLOEXEC | O_NONBLOCK, true);
+
+       uffdio_api.api = UFFD_API;
+       uffdio_api.features = 0;
+       SAFE_IOCTL(uffd, UFFDIO_API, &uffdio_api);
+
+       fault_addr = SAFE_MMAP(NULL, page_size,
+               PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);

When we need indentation it's best to format it via "=" (if you use vim:). And make the newline start from the first "(" position in a function.

Quoted 11 lines
configure.ac +11 -0
+
+       tst_res(TINFO, "Original mapping created at %p", (void *)fault_addr);
+
+       strcpy(fault_addr, "ABCD");
+
+       uffdio_register.range.start = (unsigned long)fault_addr;
+       uffdio_register.range.len = page_size;
+       uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
+       SAFE_IOCTL(uffd, UFFDIO_REGISTER, &uffdio_register);
+}
+

...

Quoted 5 lines
configure.ac +5 -0
+static void run(void)
+{
+       pthread_t handler_thread;
+
+       SAFE_PTHREAD_CREATE(&handler_thread, NULL,

configure.ac

Quoted 10 lines
configure.ac +10 -0
+               fault_handler_thread, NULL);
+
+       new_remap_addr = mremap(fault_addr, page_size, page_size,
+               MREMAP_DONTUNMAP | MREMAP_MAYMOVE, NULL);
+
+       if (new_remap_addr == MAP_FAILED)
+               tst_brk(TBROK | TERRNO, "mremap failed");
+
+       tst_res(TINFO, "New mapping created at %p", (void *)new_remap_addr);
+

I strongly suggest verifying content of thenew_addr right after the page migration
to ensure the PTEs were moved correctly without data loss.

    TST_EXP_EQ_STR(new_addr, "ABCD");
Quoted 4 lines
configure.ac +4 -0
+       strcpy(new_remap_addr, test_string);
+
+       TST_CHECKPOINT_WAKE(0);
+

configure.ac

Quoted 10 lines
configure.ac +10 -0
+       tst_res(TINFO, "Main thread accessing old address %p to trigger fault",
+                       (void *)fault_addr);
+
+       (void)*(volatile char *)fault_addr;
+
+       SAFE_PTHREAD_JOIN(handler_thread, NULL);
+
+       TST_EXP_EQ_STR(fault_addr, test_string);
+
+       SAFE_MUNMAP(new_remap_addr, page_size);

Add strcpy(old_addr, TEST_STRING_A); to support -i N.

Below is a diff version base on your patch:

testcases/kernel/syscalls/mremap/mremap07.c +2 -3
@@ -25,10 +25,11 @@
 
 static int page_size;
 static int uffd = -1;
-static char *fault_addr;
-static char *new_remap_addr;
+static char *old_addr;
+static char *new_addr;
 
-static const char *test_string = "Hello, world! This is a test string

that fills up a page.";
+#define TEST_STRING_A "ABCD"
+#define TEST_STRING_B "EFGH"

static void *fault_handler_thread(void *arg LTP_ATTRIBUTE_UNUSED) {
@@ -55,13 +56,13 @@ static void *fault_handler_thread(void *arg LTP_ATTRIBUTE_UNUSED)

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

- if ((char *)msg.arg.pagefault.address != fault_addr)
+ 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_remap_addr;
-       uffdio_copy.dst = (unsigned long)fault_addr;
+       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;

@@ -74,24 +75,26 @@ static void *fault_handler_thread(void *arg LTP_ATTRIBUTE_UNUSED)

static void setup(void)
{

-       page_size = getpagesize();
        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);

- fault_addr = SAFE_MMAP(NULL, page_size,
- PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ 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 *)fault_addr);
+ tst_res(TINFO, "Original mapping created at %p", (void *)old_addr);

- strcpy(fault_addr, "ABCD");
+ strcpy(old_addr, TEST_STRING_A);

- uffdio_register.range.start = (unsigned long)fault_addr;

+       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);

@@ -99,11 +102,11 @@ static void setup(void)

static void cleanup(void)
{
- if (new_remap_addr && new_remap_addr != MAP_FAILED)
- SAFE_MUNMAP(new_remap_addr, page_size);
+ if (new_addr && new_addr != MAP_FAILED)
+ SAFE_MUNMAP(new_addr, page_size);

- if (fault_addr && fault_addr != MAP_FAILED)
- SAFE_MUNMAP(fault_addr, page_size);
+ if (old_addr && old_addr != MAP_FAILED)
+ SAFE_MUNMAP(old_addr, page_size);

        if (uffd != -1)
                SAFE_CLOSE(uffd);

@@ -111,34 +114,36 @@ static void cleanup(void)

static void run(void)
{

+       new_addr = NULL;
        pthread_t handler_thread;

        SAFE_PTHREAD_CREATE(&handler_thread, NULL,

- fault_handler_thread, NULL);
+ fault_handler_thread, NULL);

- new_remap_addr = mremap(fault_addr, page_size, page_size,
- MREMAP_DONTUNMAP | MREMAP_MAYMOVE, NULL);
+ new_addr = mremap(old_addr, page_size, page_size,
+ MREMAP_DONTUNMAP | MREMAP_MAYMOVE, NULL);

- if (new_remap_addr == MAP_FAILED)
+ if (new_addr == MAP_FAILED)

                tst_brk(TBROK | TERRNO, "mremap failed");

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

-       strcpy(new_remap_addr, test_string);
+       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 *)fault_addr);
+ (void *)old_addr);

- (void)*(volatile char *)fault_addr;
+ (void)*(volatile char *)old_addr;

        SAFE_PTHREAD_JOIN(handler_thread, NULL);

- TST_EXP_EQ_STR(fault_addr, test_string);
+ TST_EXP_EQ_STR(old_addr, TEST_STRING_B);

-       SAFE_MUNMAP(new_remap_addr, page_size);
-       new_remap_addr = NULL;
+       SAFE_MUNMAP(new_addr, page_size);
+       strcpy(old_addr, TEST_STRING_A);

}

static struct tst_test test = {

Signature · 5 lines
Regards,
Li Wang

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

Re: [LTP] [PATCH v8] mremap07.c: New test for mremap() with MREMAP_DONTUNMAP

Quoted 7 lines · Li Wang

Hi Wei,

This is an excellent test design, but still few places need refactoring in my opinion.

Wei Gao via ltp <[email protected]> wrote:

Quoted 11 lines

configure.ac AC_CHECK_DECLS([MADV_MERGEABLE],,,[#include <sys/mman.h>]) · line 49

Quoted 7 lines
configure.ac +1 -0
@@ -46,6 +46,7 @@ AC_CHECK_DECLS([MADV_MERGEABLE],,,[#include <sys/mman.h>])
 AC_CHECK_DECLS([NFTA_CHAIN_ID, NFTA_VERDICT_CHAIN_ID],,,[#include <linux/netfilter/nf_tables.h>])
 AC_CHECK_DECLS([PR_CAPBSET_DROP, PR_CAPBSET_READ],,,[#include <sys/prctl.h>])
 AC_CHECK_DECLS([SEM_STAT_ANY],,,[#include <sys/sem.h>])
+AC_CHECK_DECLS([MREMAP_DONTUNMAP],,,[#include <linux/mman.h>])

I didn't go through all the previous discussion, but can we define MREMAP_DONTUNMAP in the lapi/mmap.h then remove the .min_kver and TST_TEST_TCONF?

To handle older kernels without using .min_kver and TST_TEST_TCONF, we'd need to implement extra logic. Would it look something like the following?

- if (new_remap_addr == MAP_FAILED)
+ if (new_remap_addr == MAP_FAILED) {
+ if (errno == EINVAL)
+ tst_brk(TCONF | TERRNO, "mremap() with MREMAP_DONTUNMAP not supported");
+
tst_brk(TBROK | TERRNO, "mremap failed");
+ }

Test on old kernel if not using .min_kver/TST_TEST_TCONF?.

susetest:~/ltp/testcases/kernel/syscalls/mremap # uname -r
4.12.14-122.231-default
susetest:~/ltp/testcases/kernel/syscalls/mremap # ./mremap07
tst_kconfig.c:90: TINFO: Parsing kernel config '/proc/config.gz'
tst_tmpdir.c:309: TINFO: Using /tmp/LTP_mreVVHzVk as tmpdir (btrfs filesystem)
tst_test.c:2059: TINFO: LTP version: 20260130-147-g19c9b694c
tst_test.c:2062: TINFO: Tested kernel: 4.12.14-122.231-default #1 SMP Wed Oct 2 17:30:21 UTC 2024 (5f0ddca) x86_64 tst_kconfig.c:90: TINFO: Parsing kernel config '/proc/config.gz'
tst_kconfig.c:714: TINFO: CONFIG_FAULT_INJECTION kernel option detected which might slow the execution tst_test.c:1889: TINFO: Overall timeout per run is 0h 02m 00s
mremap07.c:88: TINFO: Original mapping created at 0x7f19f2edb000
mremap07.c:121: TBROK: mremap failed: EINVAL (22)

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

Re: [LTP] [PATCH v8] mremap07.c: New test for mremap() with MREMAP_DONTUNMAP

Quoted 23 lines · Wei Gao
Quoted 7 lines · Li Wang

Hi Wei,

This is an excellent test design, but still few places need refactoring in my opinion.

Wei Gao via ltp <[email protected]> wrote:

Quoted 11 lines

configure.ac AC_CHECK_DECLS([MADV_MERGEABLE],,,[#include <sys/mman.h>]) · line 49

Quoted 7 lines
configure.ac +1 -0
@@ -46,6 +46,7 @@ AC_CHECK_DECLS([MADV_MERGEABLE],,,[#include <sys/mman.h>])
 AC_CHECK_DECLS([NFTA_CHAIN_ID, NFTA_VERDICT_CHAIN_ID],,,[#include <linux/netfilter/nf_tables.h>])
 AC_CHECK_DECLS([PR_CAPBSET_DROP, PR_CAPBSET_READ],,,[#include <sys/prctl.h>])
 AC_CHECK_DECLS([SEM_STAT_ANY],,,[#include <sys/sem.h>])
+AC_CHECK_DECLS([MREMAP_DONTUNMAP],,,[#include <linux/mman.h>])

I didn't go through all the previous discussion, but can we define MREMAP_DONTUNMAP in the lapi/mmap.h then remove the .min_kver and TST_TEST_TCONF?

To handle older kernels without using .min_kver and TST_TEST_TCONF, we'd need to implement extra logic. Would it look something like the following?

Review

Quoted 7 lines
(quoted diff) +5 -1
-       if (new_remap_addr == MAP_FAILED)
+       if (new_remap_addr == MAP_FAILED) {
+               if (errno == EINVAL)
+                       tst_brk(TCONF | TERRNO, "mremap() with MREMAP_DONTUNMAP not supported");
+
                tst_brk(TBROK | TERRNO, "mremap failed");
+       }

Yes, that's usually how it should be. Could you post the complete code snippet verifying this function?

Reference what you did in the past:

    b697cf0bd4ea275053288f18c093551e99571ea0
Signature · 5 lines
Regards,
Li Wang

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

PATCH v9 1/1 mremap07.c: New test for mremap() with MREMAP_DONTUNMAP

In reply to Wei Gao via 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.

---
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
include/lapi/mmap.h +4 -0
index 248b64564..3908310f4 100644
@@ -91,6 +91,10 @@
 # define MAP_DROPPABLE 0x08
 #endif
 
+#ifndef MREMAP_DONTUNMAP
+# define MREMAP_DONTUNMAP 4
+#endif
+
 #ifndef MAP_FIXED_NOREPLACE
 
 #ifdef __alpha__
runtest/syscalls +1 -0
index 5025b259f..df5dc02b5 100644
@@ -927,6 +927,7 @@ mremap03 mremap03
 mremap04 mremap04
 mremap05 mremap05
 mremap06 mremap06
+mremap07 mremap07
 
 mseal01 mseal01
 mseal02 mseal02
testcases/kernel/syscalls/mremap/.gitignore +1 -0
index ec15a19cd..292899e03 100644
@@ -4,3 +4,4 @@
 /mremap04
 /mremap05
 /mremap06
+/mremap07
testcases/kernel/syscalls/mremap/Makefile +1 -0
index 9f5aca9ec..8811b887e 100644
@@ -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
testcases/kernel/syscalls/mremap/mremap07.c new file +164 -0

164 lines not on this page · raw message

Signature · 5 lines
2.52.0


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