public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH] userfaultfd: Add test using UFFDIO_CONTINUE
@ 2026-03-21 19:58 Ricardo Branco
  2026-03-30  8:31 ` Andrea Cervesato via ltp
  0 siblings, 1 reply; 4+ messages in thread
From: Ricardo Branco @ 2026-03-21 19:58 UTC (permalink / raw)
  To: ltp

Signed-off-by: Ricardo Branco <rbranco@suse.de>
---
 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

diff --git a/include/lapi/userfaultfd.h b/include/lapi/userfaultfd.h
index aab3890b7..c05b8dbf5 100644
--- a/include/lapi/userfaultfd.h
+++ b/include/lapi/userfaultfd.h
@@ -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,	\
diff --git a/runtest/syscalls b/runtest/syscalls
index 6ba0227a8..534140f42 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -1780,6 +1780,7 @@ userfaultfd03 userfaultfd03
 userfaultfd04 userfaultfd04
 userfaultfd05 userfaultfd05
 userfaultfd06 userfaultfd06
+userfaultfd07 userfaultfd07
 
 ustat01 ustat01
 ustat02 ustat02
diff --git a/testcases/kernel/syscalls/userfaultfd/.gitignore b/testcases/kernel/syscalls/userfaultfd/.gitignore
index bc32fdf3b..3478a162e 100644
--- a/testcases/kernel/syscalls/userfaultfd/.gitignore
+++ b/testcases/kernel/syscalls/userfaultfd/.gitignore
@@ -4,3 +4,4 @@
 /userfaultfd04
 /userfaultfd05
 /userfaultfd06
+/userfaultfd07
diff --git a/testcases/kernel/syscalls/userfaultfd/Makefile b/testcases/kernel/syscalls/userfaultfd/Makefile
index 3252e47df..257249e88 100644
--- a/testcases/kernel/syscalls/userfaultfd/Makefile
+++ b/testcases/kernel/syscalls/userfaultfd/Makefile
@@ -17,3 +17,4 @@ userfaultfd03: CFLAGS += -pthread
 userfaultfd04: CFLAGS += -pthread
 userfaultfd05: CFLAGS += -pthread
 userfaultfd06: CFLAGS += -pthread
+userfaultfd07: CFLAGS += -pthread
diff --git a/testcases/kernel/syscalls/userfaultfd/userfaultfd07.c b/testcases/kernel/syscalls/userfaultfd/userfaultfd07.c
new file mode 100644
index 000000000..f2746d4ca
--- /dev/null
+++ b/testcases/kernel/syscalls/userfaultfd/userfaultfd07.c
@@ -0,0 +1,161 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2026 SUSE LLC
+ * Author: Ricardo Branco <rbranco@suse.com>
+ */
+
+/*\
+ * Force a pagefault event and handle it using :manpage:`userfaultfd(2)`
+ * from a different thread testing UFFDIO_CONTINUE.
+ */
+
+#define _GNU_SOURCE
+#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"
+#include "lapi/syscalls.h"
+
+static int page_size;
+static char *page;
+static int uffd;
+static int memfd = -1;
+
+static int sys_memfd_create(const char *name, unsigned int flags)
+{
+	return tst_syscall(__NR_memfd_create, name, flags);
+}
+
+static void set_pages(void)
+{
+	char ch = 'A';
+
+	page_size = sysconf(_SC_PAGE_SIZE);
+
+	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);
+}
+
+static void reset_pages(void)
+{
+	if (page) {
+		SAFE_MUNMAP(page, page_size);
+		page = NULL;
+	}
+
+	if (memfd >= 0) {
+		SAFE_CLOSE(memfd);
+		memfd = -1;
+	}
+}
+
+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 & ~(page_size - 1);
+	uffdio_continue.range.len = page_size;
+
+	SAFE_IOCTL(uffd, UFFDIO_CONTINUE, &uffdio_continue);
+
+	close(uffd);
+	return NULL;
+}
+
+static void run(void)
+{
+	pthread_t thr;
+	struct uffdio_api uffdio_api = {};
+	struct uffdio_register uffdio_register;
+
+	set_pages();
+
+	uffd = SAFE_USERFAULTFD(O_CLOEXEC | O_NONBLOCK, false);
+
+	uffdio_api.api = UFFD_API;
+	uffdio_api.features = UFFD_FEATURE_MINOR_SHMEM;
+
+	SAFE_IOCTL(uffd, UFFDIO_API, &uffdio_api);
+
+	if (!(uffdio_api.features & UFFD_FEATURE_MINOR_SHMEM))
+		tst_brk(TCONF, "UFFD_FEATURE_MINOR_SHMEM not supported");
+
+	uffdio_register.range.start = (unsigned long) page;
+	uffdio_register.range.len = page_size;
+	uffdio_register.mode = UFFDIO_REGISTER_MODE_MINOR;
+
+	SAFE_IOCTL(uffd, UFFDIO_REGISTER, &uffdio_register);
+
+	/*
+	 * Drop PTEs while retaining the cached shmem page so the next access
+	 * faults in MINOR mode.
+	 */
+	if (madvise(page, page_size, MADV_DONTNEED) < 0)
+		tst_brk(TBROK | TERRNO, "madvise MADV_DONTNEED failed");
+
+	SAFE_PTHREAD_CREATE(&thr, NULL, (void *) handle_thread, NULL);
+
+	char c = page[0];
+
+	if (c == 'Z')
+		tst_res(TPASS, "Pagefault handled via UFFDIO_CONTINUE");
+	else
+		tst_res(TFAIL, "pagefault not handled via UFFDIO_CONTINUE, got '%c'", c);
+
+	SAFE_PTHREAD_JOIN(thr, NULL);
+	reset_pages();
+}
+
+static struct tst_test test = {
+	.test_all = run,
+	/*
+	 * UFFDIO_CONTINUE is available since 5.13 but
+	 * UFFD_FEATURE_MINOR_SHMEM appeared in 5.14
+	 */
+	.min_kver = "5.14",
+	.needs_kconfigs = (const char *[]) {
+		"CONFIG_HAVE_ARCH_USERFAULTFD_WP=y",
+		NULL
+	},
+	.cleanup = reset_pages,
+};
-- 
2.53.0


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

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

* Re: [LTP] [PATCH] userfaultfd: Add test using UFFDIO_CONTINUE
  2026-03-21 19:58 [LTP] [PATCH] userfaultfd: Add test using UFFDIO_CONTINUE Ricardo Branco
@ 2026-03-30  8:31 ` Andrea Cervesato via ltp
  2026-03-30  9:24   ` Ricardo Branco
  0 siblings, 1 reply; 4+ messages in thread
From: Andrea Cervesato via ltp @ 2026-03-30  8:31 UTC (permalink / raw)
  To: Ricardo Branco; +Cc: ltp

Hi Ricardo,

The commit message needs a body explaining why this test is being added
(e.g. UFFDIO_CONTINUE with minor shmem faults was previously untested)
and that the missing UFFD_PAGEFAULT_FLAG_MINOR fallback define is also
added.

> +#define _GNU_SOURCE
> +#include "config.h"

_GNU_SOURCE is not needed here. None of the other userfaultfd tests use
it and nothing in this test requires GNU extensions.

[...]

> +static int page_size;
> +static char *page;
> +static int uffd;
> +static int memfd = -1;

uffd must be initialized to -1. Zero is a valid fd (stdin).

[...]

> +static void reset_pages(void)
> +{
> +	if (page) {
> +		SAFE_MUNMAP(page, page_size);
> +		page = NULL;
> +	}
> +
> +	if (memfd >= 0) {

Use `if (memfd != -1)` -- that is the LTP fd-check convention.

Also, reset_pages() is the .cleanup callback but never closes uffd. If
run() aborts after SAFE_USERFAULTFD() but before spawning the thread
(e.g. TCONF at the feature check), uffd leaks. Add:

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

[...]

> +	SAFE_IOCTL(uffd, UFFDIO_CONTINUE, &uffdio_continue);
> +
> +	close(uffd);
> +	return NULL;

Use SAFE_CLOSE(uffd) instead of close(uffd).

[...]

> +	uffd = SAFE_USERFAULTFD(O_CLOEXEC | O_NONBLOCK, false);

Consider passing true for the retry parameter. The test does not need
privileged userfaultfd -- UFFD_FEATURE_MINOR_SHMEM works fine with
UFFD_USER_MODE_ONLY. With false, the test unnecessarily TCONFs on
systems where unprivileged_userfaultfd is disabled.

[...]

> +	uffdio_continue.range.start =
> +		msg.arg.pagefault.address & ~(page_size - 1);

page_size is int, so ~(page_size - 1) produces a signed int mask that
gets sign-extended when applied to the __u64 address. Cast to avoid
relying on implementation-defined behavior:

	msg.arg.pagefault.address & ~((unsigned long)page_size - 1);

[...]

> +	.needs_kconfigs = (const char *[]) {
> +		"CONFIG_HAVE_ARCH_USERFAULTFD_WP=y",
> +		NULL
> +	},

This test uses UFFD_FEATURE_MINOR_SHMEM, not write-protect.
CONFIG_HAVE_ARCH_USERFAULTFD_WP will wrongly skip the test on
architectures that support minor faults but not WP. Use
CONFIG_USERFAULTFD=y instead. The version gate is already handled by
.min_kver = "5.14".

Regards,
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
andrea.cervesato@suse.com

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

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

* Re: [LTP] [PATCH] userfaultfd: Add test using UFFDIO_CONTINUE
  2026-03-30  8:31 ` Andrea Cervesato via ltp
@ 2026-03-30  9:24   ` Ricardo Branco
  2026-03-30  9:39     ` Andrea Cervesato via ltp
  0 siblings, 1 reply; 4+ messages in thread
From: Ricardo Branco @ 2026-03-30  9:24 UTC (permalink / raw)
  To: Andrea Cervesato; +Cc: ltp



On 3/30/26 10:31 AM, Andrea Cervesato wrote:
>> +static int page_size;
>> +static char *page;
>> +static int uffd;
>> +static int memfd = -1;
> uffd must be initialized to -1. Zero is a valid fd (stdin).

Will also fix the other tests in the "Minor fixes" patch.

> Also, reset_pages() is the .cleanup callback but never closes uffd. If
> run() aborts after SAFE_USERFAULTFD() but before spawning the thread
> (e.g. TCONF at the feature check), uffd leaks. Add:
>
> 	if (uffd != -1)
> 		SAFE_CLOSE(uffd);

Ditto.

>> +	SAFE_IOCTL(uffd, UFFDIO_CONTINUE, &uffdio_continue);
>> +
>> +	close(uffd);
>> +	return NULL;
> Use SAFE_CLOSE(uffd) instead of close(uffd).

Ditto here.

>> +	uffdio_continue.range.start =
>> +		msg.arg.pagefault.address & ~(page_size - 1);
> page_size is int, so ~(page_size - 1) produces a signed int mask that
> gets sign-extended when applied to the __u64 address. Cast to avoid
> relying on implementation-defined behavior:
>
> 	msg.arg.pagefault.address & ~((unsigned long)page_size - 1);

Shall we declare page_size as long because sysconf() returns long?

Also needs fixing the other tests.  I used userfaultfd01.c as template.

Best,
Ricardo.

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

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

* Re: [LTP] [PATCH] userfaultfd: Add test using UFFDIO_CONTINUE
  2026-03-30  9:24   ` Ricardo Branco
@ 2026-03-30  9:39     ` Andrea Cervesato via ltp
  0 siblings, 0 replies; 4+ messages in thread
From: Andrea Cervesato via ltp @ 2026-03-30  9:39 UTC (permalink / raw)
  To: Ricardo Branco; +Cc: ltp

> Shall we declare page_size as long because sysconf() returns long?

That's a good idea.

Regards,
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
andrea.cervesato@suse.com

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

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

end of thread, other threads:[~2026-03-30  9:40 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-21 19:58 [LTP] [PATCH] userfaultfd: Add test using UFFDIO_CONTINUE Ricardo Branco
2026-03-30  8:31 ` Andrea Cervesato via ltp
2026-03-30  9:24   ` Ricardo Branco
2026-03-30  9:39     ` Andrea Cervesato via ltp

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