Skip to contentSkip to conversation

userfaultfd: Add test using UFFDIO_CONTINUE

PATCH Superseded Ricardo Branco 2 participants 4 messages Mar 21 – Mar 30

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 userfaultfd: Add test using UFFDIO_CONTINUE

---
 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,

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.

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

Quoted 2 lines
testcases/kernel/syscalls/userfaultfd/userfaultfd07.c new file +2 -0
+#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.

[...]

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

Quoted 4 lines
testcases/kernel/syscalls/userfaultfd/userfaultfd07.c new file +4 -0
+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).

[...]

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

Quoted 8 lines
testcases/kernel/syscalls/userfaultfd/userfaultfd07.c new file +8 -0
+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);

[...]

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

Quoted 4 lines
testcases/kernel/syscalls/userfaultfd/userfaultfd07.c new file +4 -0
+	SAFE_IOCTL(uffd, UFFDIO_CONTINUE, &uffdio_continue);
+
+	close(uffd);
+	return NULL;

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

[...]

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

Quoted 1 line
testcases/kernel/syscalls/userfaultfd/userfaultfd07.c new file +1 -0
+	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.

[...]

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

Quoted 2 lines
testcases/kernel/syscalls/userfaultfd/userfaultfd07.c new file +2 -0
+	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);

[...]

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

Quoted 4 lines
testcases/kernel/syscalls/userfaultfd/userfaultfd07.c new file +4 -0
+	.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
[email protected]

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

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

Quoted 4 lines
testcases/kernel/syscalls/userfaultfd/userfaultfd07.c new file +4 -0
+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.

Quoted 6 lines

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.

Quoted 5 lines

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

Quoted 4 lines
testcases/kernel/syscalls/userfaultfd/userfaultfd07.c new file +4 -0
+	SAFE_IOCTL(uffd, UFFDIO_CONTINUE, &uffdio_continue);
+
+	close(uffd);
+	return NULL;

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

Ditto here.

Quoted 7 lines

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

Quoted 2 lines
testcases/kernel/syscalls/userfaultfd/userfaultfd07.c new file +2 -0
+	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.

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

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

That's a good idea.

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

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