Skip to contentSkip to conversation

userfaultfd: Add tests for UFFDIO_RWPROTECT and UFFDIO_SET_MODE

PATCH Superseded Ricardo Branco 3 participants 4 messages Nov 24 – Nov 25

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 userfaultfd: Add tests for UFFDIO_RWPROTECT and UFFDIO_SET_MODE

Add userfaultfd08 with two variants to exercise UFFDIO_RWPROTECT and UFFDIO_SET_MODE.

The first variant registers a page with UFFDIO_REGISTER_MODE_RWP, protects it with UFFDIO_RWPROTECT_MODE_RWP, and verifies that a plain read from a second thread raises a UFFD_PAGEFAULT_FLAG_RWP fault (without UFFD_PAGEFAULT_FLAG_WRITE) that is resolved by clearing the RWP mode, preserving page contents.

The second variant exercises UFFDIO_SET_MODE toggling UFFD_FEATURE_RWP_ASYNC at runtime: with async mode enabled at UFFDIO_API time, a protected page is accessed and auto-resolved with no fault message delivered; UFFDIO_SET_MODE then disables async mode, and the same access on a freshly protected page now blocks and is delivered as a fault, handled from a second thread as above.

Add the UFFDIO_RWPROTECT and UFFDIO_SET_MODE ioctls, their associated structs, and the UFFD_FEATURE_RWP/UFFD_FEATURE_RWP_ASYNC feature bits to lapi/userfaultfd.h for systems whose kernel headers predate them.

---
 configure.ac                                  |   2 +
 include/lapi/userfaultfd.h                    |  28 +++
 runtest/syscalls                              |   1 +
 .../kernel/syscalls/userfaultfd/.gitignore    |   1 +
 .../kernel/syscalls/userfaultfd/Makefile      |   1 +
 .../syscalls/userfaultfd/userfaultfd08.c      | 183 ++++++++++++++++++
 6 files changed, 216 insertions(+)
 create mode 100644 testcases/kernel/syscalls/userfaultfd/userfaultfd08.c
configure.ac +2 -0
index d75e94162..359a584f5 100644
@@ -298,6 +298,8 @@ AC_CHECK_TYPES([struct sockaddr_vm],,,[
 
 AC_CHECK_TYPES([struct uffdio_move],,,[#include <linux/userfaultfd.h>])
 AC_CHECK_TYPES([struct uffdio_poison],,,[#include <linux/userfaultfd.h>])
+AC_CHECK_TYPES([struct uffdio_rwprotect],,,[#include <linux/userfaultfd.h>])
+AC_CHECK_TYPES([struct uffdio_set_mode],,,[#include <linux/userfaultfd.h>])
 AC_CHECK_TYPES([struct uffdio_writeprotect],,,[#include <linux/userfaultfd.h>])
 
 AC_CHECK_TYPES([struct keyctl_dh_params, struct keyctl_kdf_params,
include/lapi/userfaultfd.h +28 -0
index 4a25e7338..a4b3d0763 100644
@@ -247,6 +247,34 @@ struct uffdio_poison {
 };
 #endif	/* HAVE_STRUCT_UFFDIO_POISON */
 
+#ifndef HAVE_STRUCT_UFFDIO_RWPROTECT
+#define UFFD_FEATURE_RWP			(1<<17)
+#define UFFD_FEATURE_RWP_ASYNC			(1<<18)
+#define UFFD_PAGEFAULT_FLAG_RWP			(1<<3)
+#define UFFDIO_REGISTER_MODE_RWP		((__u64)1<<3)
+#define _UFFDIO_RWPROTECT			(0x09)
+#define UFFDIO_RWPROTECT	_IOWR(UFFDIO, _UFFDIO_RWPROTECT, \
+				      struct uffdio_rwprotect)
+
+struct uffdio_rwprotect {
+	struct uffdio_range range;
+#define UFFDIO_RWPROTECT_MODE_RWP		((__u64)1<<0)
+#define UFFDIO_RWPROTECT_MODE_DONTWAKE		((__u64)1<<1)
+	__u64 mode;
+};
+#endif	/* HAVE_STRUCT_UFFDIO_RWPROTECT */
+
+#ifndef HAVE_STRUCT_UFFDIO_SET_MODE
+#define _UFFDIO_SET_MODE			(0x0A)
+#define UFFDIO_SET_MODE	_IOW(UFFDIO, _UFFDIO_SET_MODE, \
+				     struct uffdio_set_mode)
+
+struct uffdio_set_mode {
+	__u64 enable;
+	__u64 disable;
+};
+#endif	/* HAVE_STRUCT_UFFDIO_SET_MODE */
+
 #define SAFE_USERFAULTFD(flags, retry) \
 	safe_userfaultfd(__FILE__, __LINE__, (flags), (retry))
 
runtest/syscalls +1 -0
index 7b6a7d376..dadf076f8 100644
@@ -1849,6 +1849,7 @@ userfaultfd04 userfaultfd04
 userfaultfd05 userfaultfd05
 userfaultfd06 userfaultfd06
 userfaultfd07 userfaultfd07
+userfaultfd08 userfaultfd08
 
 ustat01 ustat01
 ustat02 ustat02
testcases/kernel/syscalls/userfaultfd/.gitignore +1 -0
index 3478a162e..d5734bb68 100644
@@ -5,3 +5,4 @@
 /userfaultfd05
 /userfaultfd06
 /userfaultfd07
+/userfaultfd08
testcases/kernel/syscalls/userfaultfd/Makefile +1 -0
index 257249e88..8c757c030 100644
@@ -18,3 +18,4 @@ userfaultfd04: CFLAGS += -pthread
 userfaultfd05: CFLAGS += -pthread
 userfaultfd06: CFLAGS += -pthread
 userfaultfd07: CFLAGS += -pthread
+userfaultfd08: CFLAGS += -pthread
testcases/kernel/syscalls/userfaultfd/userfaultfd08.c new file +183 -0

183 lines not on this page · raw message

Signature · 5 lines
2.56.1


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

Hi Ricardo,

Quoted 1 line · Ricardo Branco

userfaultfd: Add tests for UFFDIO_RWPROTECT and UFFDIO_SET_MODE

Verdict - Reviewed

---
Note:

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
#2 lore raw
In reply to Ricardo Branco

Hi!

Quoted 62 lines
testcases/kernel/syscalls/userfaultfd/userfaultfd08.c new file +62 -0
+static void run(unsigned int n)
+{
+	pthread_t thr;
+	struct uffdio_api uffdio_api = { .api = UFFD_API };
+	struct uffdio_register uffdio_register;
+	struct uffdio_set_mode uffdio_set_mode = {
+		.disable = UFFD_FEATURE_RWP_ASYNC,
+	};
+	struct pollfd pollfd;
+	unsigned char val;
+
+	uffdio_api.features = UFFD_FEATURE_RWP;
+	if (n) {
+		CHECK_UFFD_FEATURE(UFFD_FEATURE_RWP_ASYNC);
+		uffdio_api.features |= UFFD_FEATURE_RWP_ASYNC;
+	}
+
+	set_page();
+	rwp_fault_seen = 0;
+
+	uffd = SAFE_USERFAULTFD(O_CLOEXEC | O_NONBLOCK, false);
+
+	SAFE_IOCTL(uffd, UFFDIO_API, &uffdio_api);
+
+	uffdio_register.range.start = (unsigned long)page;
+	uffdio_register.range.len = page_size;
+	uffdio_register.mode = UFFDIO_REGISTER_MODE_RWP;
+
+	SAFE_IOCTL(uffd, UFFDIO_REGISTER, &uffdio_register);
+
+	if (n) {
+		/* Async mode is active, access must auto-resolve. */
+		rwprotect((unsigned long)page, UFFDIO_RWPROTECT_MODE_RWP);
+
+		val = page[0];
+
+		if (val != PAGE_BYTE) {
+			tst_res(TFAIL, "Async access corrupted content: got 0x%02x, expected 0x%02x",
+				val, PAGE_BYTE);
+			goto out;
+		}
+
+		pollfd.fd = uffd;
+		pollfd.events = POLLIN;
+		if (SAFE_POLL(&pollfd, 1, 0) != 0) {
+			tst_res(TFAIL, "Unexpected pagefault message in async mode");
+			goto out;
+		}
+
+		/* Switch to sync mode via UFFDIO_SET_MODE. */
+		SAFE_IOCTL(uffd, UFFDIO_SET_MODE, &uffdio_set_mode);
+	}
+
+	rwprotect((unsigned long)page, UFFDIO_RWPROTECT_MODE_RWP);
+
+	SAFE_PTHREAD_CREATE(&thr, NULL, handle_thread, NULL);
+
+	/* Try to read the RWP-protected page. */
+	val = page[0];
+
+	SAFE_PTHREAD_JOIN(thr, NULL);
+

testcases/kernel/syscalls/userfaultfd/userfaultfd08.c line 169

Quoted 10 lines
testcases/kernel/syscalls/userfaultfd/userfaultfd08.c new file +10 -0
+	if (!rwp_fault_seen) {
+		tst_res(TFAIL, "No RWPROTECT pagefault observed");
+		goto out;
+	}
+
+	if (val != PAGE_BYTE) {
+		tst_res(TFAIL, "Page content corrupted: got 0x%02x, expected 0x%02x",
+			val, PAGE_BYTE);
+		goto out;
+	}

Shouldn't this part be in else branch? Otherwise we run both tests for n = 1.

I suppose that we would have to run the handle_thread in the first if branch as well, but not as a thread, since it's asynchronous we can run it as a normal function.

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw
Quoted 2 lines · Cyril Hrubis

Shouldn't this part be in else branch? Otherwise we run both tests for n = 1.

  That's intentional. For n = 1 the thing under test is UFFDIO_SET_MODE:
  after disabling RWP_ASYNC, the only way to verify the switch took effect is to show that the next access on a re-protected page blocks and is delivered as a fault. Without that second half we'd only be checking that the ioctl returns 0.

Quoted 3 lines

I suppose that we would have to run the handle_thread in the first if branch as well, but not as a thread, since it's asynchronous we can run it as a normal function.

  With UFFD_FEATURE_RWP_ASYNC the kernel resolves the access itself and no fault message is queued at all, so handle_thread() would block in poll() forever. The async branch actually asserts that: after the access it checks that poll() on the uffd with a zero timeout returns 0.

  In v5 I'll split the two phases into check_async_access() and check_sync_fault() helpers with a comment in run(), so the flow is
  explicit: n = 0 runs only the sync check, n = 1 runs the async check, then UFFDIO_SET_MODE, then the sync check.

Best,
Ricardo

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