Skip to contentSkip to conversation

userfaultfd: Add new test using UFFDIO_CONTINUE

PATCH Superseded Ricardo Branco 2 participants 4 messages Mar 30 – Apr 1

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 userfaultfd: Add new test using UFFDIO_CONTINUE

We test it with UFFD_PAGEFAULT_FLAG_MINOR.

---
 include/lapi/userfaultfd.h                    |   1 +
 runtest/syscalls                              |   1 +
 .../kernel/syscalls/userfaultfd/.gitignore    |   1 +
 .../kernel/syscalls/userfaultfd/Makefile      |   1 +
 .../syscalls/userfaultfd/userfaultfd07.c      | 161 ++++++++++++++++++
 5 files changed, 165 insertions(+)
 create mode 100644 testcases/kernel/syscalls/userfaultfd/userfaultfd07.c
include/lapi/userfaultfd.h +1 -0
index aab3890b7..c05b8dbf5 100644
@@ -171,6 +171,7 @@ struct uffdio_zeropage {
 #ifndef UFFD_PAGEFAULT_FLAG_MINOR
 #define UFFD_FEATURE_MINOR_HUGETLBFS		(1<<9)
 #define UFFDIO_REGISTER_MODE_MINOR	((__u64)1<<2)
+#define UFFD_PAGEFAULT_FLAG_MINOR	(1<<2)
 
 #define _UFFDIO_CONTINUE		(0x07)
 #define UFFDIO_CONTINUE		_IOWR(UFFDIO, _UFFDIO_CONTINUE,	\
runtest/syscalls +1 -0
index 6ba0227a8..534140f42 100644
@@ -1780,6 +1780,7 @@ userfaultfd03 userfaultfd03
 userfaultfd04 userfaultfd04
 userfaultfd05 userfaultfd05
 userfaultfd06 userfaultfd06
+userfaultfd07 userfaultfd07
 
 ustat01 ustat01
 ustat02 ustat02
testcases/kernel/syscalls/userfaultfd/.gitignore +1 -0
index bc32fdf3b..3478a162e 100644
@@ -4,3 +4,4 @@
 /userfaultfd04
 /userfaultfd05
 /userfaultfd06
+/userfaultfd07
testcases/kernel/syscalls/userfaultfd/Makefile +1 -0
index 3252e47df..257249e88 100644
@@ -17,3 +17,4 @@ userfaultfd03: CFLAGS += -pthread
 userfaultfd04: CFLAGS += -pthread
 userfaultfd05: CFLAGS += -pthread
 userfaultfd06: CFLAGS += -pthread
+userfaultfd07: CFLAGS += -pthread
testcases/kernel/syscalls/userfaultfd/userfaultfd07.c new file +161 -0

161 lines not on this page · raw message

Signature · 5 lines
2.53.0


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

Hi Ricardo,

Quoted 1 line

We test it with UFFD_PAGEFAULT_FLAG_MINOR.

Thanks, minor things below.

...

Quoted 26 lines
testcases/kernel/syscalls/userfaultfd/userfaultfd07.c new file +20 -0
new file mode 100644
index 000000000..644e7724e
@@ -0,0 +1,161 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2026 SUSE LLC
+ * Author: Ricardo Branco <[email protected]>
+ */
+
+/*\
+ * Force a pagefault event and handle it using :manpage:`userfaultfd(2)`
+ * from a different thread testing UFFDIO_CONTINUE.
+ */
+
+#include "config.h"
+#include <poll.h>
+#include <unistd.h>
+#include "tst_test.h"
+#include "tst_safe_macros.h"
+#include "tst_safe_prw.h"
+#include "tst_safe_pthread.h"
+#include "lapi/memfd.h"
+#include "lapi/userfaultfd.h"

testcases/kernel/syscalls/userfaultfd/userfaultfd07.c line 30

Quoted 10 lines
testcases/kernel/syscalls/userfaultfd/userfaultfd07.c new file +10 -0
+#include "lapi/syscalls.h"
+
+static long page_size;
+static char *page;
+static int uffd = -1;
+static int memfd = -1;
+
+static int sys_memfd_create(const char *name, unsigned int flags)
+{
+	return tst_syscall(__NR_memfd_create, name, flags);

We have this in testcases/kernel/syscalls/memfd_create/memfd_create_common.c. Moving it as static inline into include/lapi/memfd.h would help to DRY.

testcases/kernel/syscalls/userfaultfd/userfaultfd07.c line 37

Quoted 7 lines
testcases/kernel/syscalls/userfaultfd/userfaultfd07.c new file +7 -0
+}
+
+static void set_pages(void)
+{
+	char ch = 'A';
+
+	page_size = sysconf(_SC_PAGE_SIZE);

Maybe SAFE_SYSCONF(_SC_PAGE_SIZE) or because it should be safe:
getpagesize();

Quoted 16 lines
testcases/kernel/syscalls/userfaultfd/userfaultfd07.c new file +16 -0
+
+	memfd = sys_memfd_create("ltp-uffd-continue", MFD_CLOEXEC);
+	if (memfd < 0)
+		tst_brk(TBROK | TERRNO, "memfd_create failed");
+
+	SAFE_FTRUNCATE(memfd, page_size);
+
+	/*
+	 * Populate page cache so that after MADV_DONTNEED the next access
+	 * can generate a MINOR fault rather than a MISSING fault.
+	 */
+	SAFE_PWRITE(1, memfd, &ch, 1, 0);
+
+	page = SAFE_MMAP(NULL, page_size, PROT_READ, MAP_SHARED, memfd, 0);
+}
+

testcases/kernel/syscalls/userfaultfd/userfaultfd07.c line 63

Quoted 10 lines
testcases/kernel/syscalls/userfaultfd/userfaultfd07.c new file +10 -0
+static void reset_pages(void)
+{
+	if (page) {
+		SAFE_MUNMAP(page, page_size);
+		page = NULL;
+	}
+
+	if (memfd != -1) {
+		SAFE_CLOSE(memfd);
+		memfd = -1;

This is not needed, see include/tst_safe_macros.h

#define SAFE_CLOSE(fd) do { \
		safe_close(__FILE__, __LINE__, NULL, (fd)); \
		fd = -1; \
	} while (0)

testcases/kernel/syscalls/userfaultfd/userfaultfd07.c line 68

Quoted 5 lines
testcases/kernel/syscalls/userfaultfd/userfaultfd07.c new file +5 -0
+	}
+
+	if (uffd != -1) {
+		SAFE_CLOSE(uffd);
+		uffd = -1;

And here.

Quoted 37 lines
testcases/kernel/syscalls/userfaultfd/userfaultfd07.c new file +37 -0
+	}
+}
+
+static void *handle_thread(void *arg LTP_ATTRIBUTE_UNUSED)
+{
+	static struct uffd_msg msg;
+	struct uffdio_continue uffdio_continue = {};
+	struct pollfd pollfd;
+	int nready;
+	char z = 'Z';
+
+	pollfd.fd = uffd;
+	pollfd.events = POLLIN;
+	nready = poll(&pollfd, 1, -1);
+	if (nready == -1)
+		tst_brk(TBROK | TERRNO, "Error on poll");
+
+	SAFE_READ(1, uffd, &msg, sizeof(msg));
+
+	if (msg.event != UFFD_EVENT_PAGEFAULT)
+		tst_brk(TFAIL, "Received unexpected UFFD_EVENT %d", msg.event);
+
+	if (!(msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_MINOR)) {
+		tst_brk(TBROK, "expected MINOR fault, got flags=0x%llx",
+			(unsigned long long)msg.arg.pagefault.flags);
+	}
+
+	/* Update the shmem page in page cache before resuming the fault. */
+	SAFE_PWRITE(1, memfd, &z, 1, 0);
+
+	uffdio_continue.range.start =
+		msg.arg.pagefault.address & ~((unsigned long)page_size - 1);
+	uffdio_continue.range.len = page_size;
+
+	SAFE_IOCTL(uffd, UFFDIO_CONTINUE, &uffdio_continue);
+
+	SAFE_CLOSE(uffd);

testcases/kernel/syscalls/userfaultfd/userfaultfd07.c line 115

Quoted 10 lines
testcases/kernel/syscalls/userfaultfd/userfaultfd07.c new file +10 -0
+	return NULL;
+}
+
+static void run(void)
+{
+	pthread_t thr;
+	struct uffdio_api uffdio_api = {};
+	struct uffdio_register uffdio_register;
+
+	set_pages();

Maybe have it as setup function instead of calling it manually at the beginning of run()? I'm not sure if it justifies, but remember possible running -i1000.

THe rest LGTM.

Kind regards,
Petr

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

Maybe SAFE_SYSCONF(_SC_PAGE_SIZE) or because it should be safe:
getpagesize();

Opted for SAFE_SYSCONF as getpagesize() returns int.

Quoted 6 lines

testcases/kernel/syscalls/userfaultfd/userfaultfd07.c line 68

Quoted 5 lines
testcases/kernel/syscalls/userfaultfd/userfaultfd07.c new file +5 -0
+	}
+
+	if (uffd != -1) {
+		SAFE_CLOSE(uffd);
+		uffd = -1;

And here.

Andrea pointed me to this LLM suggestion that got it wrong:
https://github.com/acerv/ltp-agent/actions/runs/23788068990

It's a cool thing that we're reviewing code that I used as template to fix the userfaultfd tests in the other PR with fixes.

Best,
Ricardo.

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

Hi Ricardo,

Quoted 5 lines · Petr Vorel
Quoted 2 lines

Maybe SAFE_SYSCONF(_SC_PAGE_SIZE) or because it should be safe:
getpagesize();

Opted for SAFE_SYSCONF as getpagesize() returns int.

+1

Quoted 7 lines
Quoted 6 lines

testcases/kernel/syscalls/userfaultfd/userfaultfd07.c line 68

Quoted 5 lines
testcases/kernel/syscalls/userfaultfd/userfaultfd07.c new file +5 -0
+	}
+
+	if (uffd != -1) {
+		SAFE_CLOSE(uffd);
+		uffd = -1;

And here.

Quoted 5 lines

Andrea pointed me to this LLM suggestion that got it wrong:
https://github.com/acerv/ltp-agent/actions/runs/23788068990

It's a cool thing that we're reviewing code that I used as template to fix the userfaultfd tests in the other PR with fixes.

Yeah, I already asked him yesterday to teach agent to have look into includes as well :).

Kind regards,
Petr

Quoted 2 lines

Best,
Ricardo.

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