Skip to contentSkip to conversation

userfaultfd: Add helper for checking UFFD feature support

PATCH In review Ricardo Branco 4 participants 11 messages May 7

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/3 userfaultfd: Add helper for checking UFFD feature support

Add CHECK_UFFD_FEATURE() to perform the initial UFFDIO_API handshake without requesting optional features and verify that the kernel reports the requested feature bit.

---
 include/lapi/userfaultfd.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
include/lapi/userfaultfd.h +20 -0
index aab3890b7..170dfadd4 100644
@@ -277,4 +277,24 @@ retry:
 	return ret;
 }
 
+#define CHECK_UFFD_FEATURE(feature)	check_uffd_feature(feature, #feature)
+
+static inline void check_uffd_feature(uint64_t feature, const char *name)
+{
+	struct uffdio_api uffdio_api = {};
+	int uffd;
+
+	uffd = SAFE_USERFAULTFD(O_CLOEXEC | O_NONBLOCK, false);
+
+	uffdio_api.api = UFFD_API;
+	SAFE_IOCTL(uffd, UFFDIO_API, &uffdio_api);
+
+	if (!(uffdio_api.features & feature)) {
+		SAFE_CLOSE(uffd);
+		tst_brk(TCONF, "%s not supported", name);
+	}
+
+	SAFE_CLOSE(uffd);
+}
+
 #endif /* LAPI_USERFAULTFD_H__ */
Signature · 5 lines
2.54.0


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

PATCH v4 2/3 userfaultfd: Use two-step handshake to probe features

userfaultfd05 and userfaultfd06 assume that UFFD_FEATURE_PAGEFAULT_FLAG_WP and UFFD_FEATURE_POISON are available when setting up userfaultfd.

On kernels where either feature is unavailable at runtime, UFFDIO_API fails with EINVAL and the tests end up as TBROK instead of being skipped.

Fix this by using the required two-step handshake and skip with TCONF if the required feature bit is absent.

Fixes 1840ee23d172b5ab04cca7c2acfa48755b041911
---
 testcases/kernel/syscalls/userfaultfd/userfaultfd05.c | 8 +++++++-
 testcases/kernel/syscalls/userfaultfd/userfaultfd06.c | 6 +++---
 2 files changed, 10 insertions(+), 4 deletions(-)
testcases/kernel/syscalls/userfaultfd/userfaultfd05.c +7 -1
index 59956d85d..a42ac09bd 100644
@@ -22,6 +22,11 @@ static char *page;
 static int uffd = -1;
 static volatile int wp_fault_seen;
 
+static void setup(void)
+{
+	CHECK_UFFD_FEATURE(UFFD_FEATURE_PAGEFAULT_FLAG_WP);
+}
+
 static void set_pages(void)
 {
 	page_size = SAFE_SYSCONF(_SC_PAGE_SIZE);
@@ -87,7 +92,7 @@ static void *handle_thread(void *arg LTP_ATTRIBUTE_UNUSED)
 static void run(void)
 {
 	pthread_t thr;
-	struct uffdio_api uffdio_api;
+	struct uffdio_api uffdio_api = {};
 	struct uffdio_register uffdio_register;
 	struct uffdio_writeprotect uffdio_writeprotect;
 
@@ -127,6 +132,7 @@ static void run(void)
 }
 
 static struct tst_test test = {
+	.setup = setup,
 	.test_all = run,
 	.min_kver = "5.7",
 	.needs_kconfigs = (const char *[]) {
testcases/kernel/syscalls/userfaultfd/userfaultfd06.c +3 -3
index ec93d8ad6..1ab288e64 100644
@@ -38,6 +38,8 @@ static void setup(void)
 {
 	struct sigaction sa = {};
 
+	CHECK_UFFD_FEATURE(UFFD_FEATURE_POISON);
+
 	sa.sa_handler = sigbus_handler;
 	sigemptyset(&sa.sa_mask);
 	SAFE_SIGACTION(SIGBUS, &sa, NULL);
@@ -99,6 +101,7 @@ static void run(void)
 
 	poison_fault_seen = 0;
 	sigbus_seen = 0;
+
 	set_pages();
 
 	uffd = SAFE_USERFAULTFD(O_CLOEXEC | O_NONBLOCK, false);
@@ -108,9 +111,6 @@ static void run(void)
 
 	SAFE_IOCTL(uffd, UFFDIO_API, &uffdio_api);
 
-	if (!(uffdio_api.features & UFFD_FEATURE_POISON))
-		tst_brk(TCONF, "UFFD_FEATURE_POISON not supported");
-
 	uffdio_register.range.start = (unsigned long) page;
 	uffdio_register.range.len = page_size;
 	uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
Signature · 5 lines
2.54.0


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

PATCH v4 3/3 userfaultfd: Reset wp_fault_seen on each run()

In reply to Ricardo Branco

wp_fault_seen was never reset to 0 at the top of run(); so on multiple runs (-i N) the second iteration could falsely pass.

---
 testcases/kernel/syscalls/userfaultfd/userfaultfd05.c | 1 +
 1 file changed, 1 insertion(+)
testcases/kernel/syscalls/userfaultfd/userfaultfd05.c +1 -0
index a42ac09bd..e36f9c321 100644
@@ -97,6 +97,7 @@ static void run(void)
 	struct uffdio_writeprotect uffdio_writeprotect;
 
 	set_pages();
+	wp_fault_seen = 0;
 
 	uffd = SAFE_USERFAULTFD(O_CLOEXEC | O_NONBLOCK, false);
 
Signature · 5 lines
2.54.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#7 lore raw
In reply to Ricardo Branco

Hi Ricardo,

--- [PATCH 1/3] ---

include/lapi/userfaultfd.h retry: · line 290

Quoted 6 lines · Ricardo Branco

[PATCH 1/3] userfaultfd: Add helper for checking UFFD feature support

include/lapi/userfaultfd.h +4 -0
+	uffd = SAFE_USERFAULTFD(O_CLOEXEC | O_NONBLOCK, false);
+
+	uffdio_api.api = UFFD_API;
+	SAFE_IOCTL(uffd, UFFDIO_API, &uffdio_api);

If SAFE_IOCTL calls tst_brk(TBROK) here, the local uffd is not closed before the test runner takes over; the cleanup functions only track the global uffd. Close it before the SAFE_IOCTL call, or restructure to guarantee SAFE_CLOSE on all exit paths.

--- [PATCH 2/3] ---

Quoted 3 lines · Ricardo Branco

[PATCH 2/3] userfaultfd: Use two-step handshake to probe features

Fixes 1840ee23d172b5ab04cca7c2acfa48755b041911

This commit is not reachable in the local git history (grafted shallow clone). Please verify the Fixes: tag resolves correctly against the upstream LTP tree.

Pre-existing issues noticed in the surrounding code (not introduced by this patch):

- testcases/kernel/syscalls/userfaultfd/userfaultfd06.c:142 — no .min_kver set; UFFD_FEATURE_POISON requires Linux 6.6.

---
Note:

Our agent completed the review of the patch.

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
#8 lore raw

Hi!

Quoted 9 lines

include/lapi/userfaultfd.h retry: · line 290

Quoted 4 lines
include/lapi/userfaultfd.h +4 -0
+	uffd = SAFE_USERFAULTFD(O_CLOEXEC | O_NONBLOCK, false);
+
+	uffdio_api.api = UFFD_API;
+	SAFE_IOCTL(uffd, UFFDIO_API, &uffdio_api);

If SAFE_IOCTL calls tst_brk(TBROK) here, the local uffd is not closed before the test runner takes over; the cleanup functions only track the global uffd. Close it before the SAFE_IOCTL call, or restructure to guarantee SAFE_CLOSE on all exit paths.

This is not necessarily wrong, but since TBROK is called, fd is closed with the process. We need to instruct the agent because I seen this logical concept being missed a few times.

Quoted 11 lines

--- [PATCH 2/3] ---

Quoted 3 lines · Ricardo Branco

[PATCH 2/3] userfaultfd: Use two-step handshake to probe features

Fixes 1840ee23d172b5ab04cca7c2acfa48755b041911

This commit is not reachable in the local git history (grafted shallow clone). Please verify the Fixes: tag resolves correctly against the upstream LTP tree.

Can you please double check this? Maybe we have an another issue with the agent.

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

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

Hi!

Quoted 5 lines
Quoted 5 lines

--- [PATCH 2/3] ---

Quoted 1 line · Ricardo Branco

[PATCH 2/3] userfaultfd: Use two-step handshake to probe features

Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Fixes 1840ee23d172b5ab04cca7c2acfa48755b041911

This commit is not reachable in the local git history (grafted shallow clone). Please verify the Fixes: tag resolves correctly against the upstream LTP tree.

Can you please double check this? Maybe we have an another issue with the agent.

The commit is there:

commit 1840ee23d172b5ab04cca7c2acfa48755b041911

Author: Li Wang <[email protected]>
Date:   Fri Jan 23 19:54:19 2026 +0800

userfaultfd05: require CONFIG_HAVE_ARCH_USERFAULTFD_WP

Signature · 5 lines
Cyril Hrubis
[email protected]

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

The commit is there:

commit 1840ee23d172b5ab04cca7c2acfa48755b041911

Author: Li Wang <[email protected]>
Date:   Fri Jan 23 19:54:19 2026 +0800

userfaultfd05: require CONFIG_HAVE_ARCH_USERFAULTFD_WP

sigh. Im wondering why it didn't catch it, this is the basics.

I think we need to separate the logical review from everything that can be done with a linter. I will explore this possibility in the next weeks when I will have more time.

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

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