Skip to content

Commit 393d68f

Browse files
userfaultfd: Add test using /dev/userfaultfd instead of syscall
Signed-off-by: Ricardo Branco <[email protected]>
1 parent d06f420 commit 393d68f

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

runtest/syscalls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,6 +1772,7 @@ umount2_02 umount2_02
17721772
userfaultfd01 userfaultfd01
17731773
userfaultfd02 userfaultfd02
17741774
userfaultfd03 userfaultfd03
1775+
userfaultfd04 userfaultfd04
17751776

17761777
ustat01 ustat01
17771778
ustat02 ustat02
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/userfaultfd01
22
/userfaultfd02
33
/userfaultfd03
4+
/userfaultfd04

testcases/kernel/syscalls/userfaultfd/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ include $(top_srcdir)/include/mk/generic_leaf_target.mk
1414
userfaultfd01: CFLAGS += -pthread
1515
userfaultfd02: CFLAGS += -pthread
1616
userfaultfd03: CFLAGS += -pthread
17+
userfaultfd04: CFLAGS += -pthread
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* Copyright (c) 2025 SUSE LLC
4+
* Author: Christian Amann <[email protected]>
5+
* Author: Ricardo Branco <[email protected]>
6+
*/
7+
8+
/*\
9+
* Force a pagefault event and handle it using :man2:`userfaultfd`
10+
* from a different thread using /dev/userfaultfd instead of syscall.
11+
*/
12+
13+
#include "config.h"
14+
#include <poll.h>
15+
#include "tst_test.h"
16+
#include "tst_safe_macros.h"
17+
#include "tst_safe_pthread.h"
18+
#include "lapi/userfaultfd.h"
19+
20+
static int page_size;
21+
static char *page;
22+
static void *copy_page;
23+
static int uffd;
24+
25+
static int open_userfaultfd(int flags)
26+
{
27+
int fd, fd2;
28+
29+
fd = SAFE_OPEN("/dev/userfaultfd", O_RDWR);
30+
31+
fd2 = SAFE_IOCTL(fd, USERFAULTFD_IOC_NEW, flags);
32+
33+
SAFE_CLOSE(fd);
34+
35+
return fd2;
36+
}
37+
38+
static void set_pages(void)
39+
{
40+
page_size = sysconf(_SC_PAGE_SIZE);
41+
page = SAFE_MMAP(NULL, page_size, PROT_READ | PROT_WRITE,
42+
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
43+
copy_page = SAFE_MMAP(NULL, page_size, PROT_READ | PROT_WRITE,
44+
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
45+
}
46+
47+
static void handle_thread(void)
48+
{
49+
static struct uffd_msg msg;
50+
struct uffdio_copy uffdio_copy;
51+
52+
struct pollfd pollfd;
53+
int nready;
54+
55+
pollfd.fd = uffd;
56+
pollfd.events = POLLIN;
57+
nready = poll(&pollfd, 1, -1);
58+
if (nready == -1)
59+
tst_brk(TBROK | TERRNO, "Error on poll");
60+
61+
SAFE_READ(1, uffd, &msg, sizeof(msg));
62+
63+
if (msg.event != UFFD_EVENT_PAGEFAULT)
64+
tst_brk(TBROK | TERRNO, "Received unexpected UFFD_EVENT");
65+
66+
memset(copy_page, 'X', page_size);
67+
68+
uffdio_copy.src = (unsigned long) copy_page;
69+
70+
uffdio_copy.dst = (unsigned long) msg.arg.pagefault.address
71+
& ~(page_size - 1);
72+
uffdio_copy.len = page_size;
73+
uffdio_copy.mode = 0;
74+
uffdio_copy.copy = 0;
75+
SAFE_IOCTL(uffd, UFFDIO_COPY, &uffdio_copy);
76+
77+
close(uffd);
78+
}
79+
80+
static void run(void)
81+
{
82+
pthread_t thr;
83+
struct uffdio_api uffdio_api;
84+
struct uffdio_register uffdio_register;
85+
86+
set_pages();
87+
88+
TEST(open_userfaultfd(O_CLOEXEC | O_NONBLOCK));
89+
90+
if (TST_RET == -1) {
91+
if (TST_ERR == EPERM) {
92+
tst_brk(TCONF, "Hint: check /dev/userfaultfd permissions");
93+
} else
94+
tst_brk(TBROK | TTERRNO,
95+
"Could not create userfault file descriptor");
96+
}
97+
98+
uffd = TST_RET;
99+
uffdio_api.api = UFFD_API;
100+
uffdio_api.features = 0;
101+
SAFE_IOCTL(uffd, UFFDIO_API, &uffdio_api);
102+
103+
uffdio_register.range.start = (unsigned long) page;
104+
uffdio_register.range.len = page_size;
105+
uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
106+
107+
SAFE_IOCTL(uffd, UFFDIO_REGISTER, &uffdio_register);
108+
109+
SAFE_PTHREAD_CREATE(&thr, NULL,
110+
(void * (*)(void *)) handle_thread, NULL);
111+
112+
char c = page[0xf];
113+
114+
if (c == 'X')
115+
tst_res(TPASS, "Pagefault handled via /dev/userfaultfd");
116+
else
117+
tst_res(TFAIL, "Pagefault not handled via /dev/userfaultfd");
118+
119+
SAFE_PTHREAD_JOIN(thr, NULL);
120+
}
121+
122+
static struct tst_test test = {
123+
.test_all = run,
124+
.min_kver = "5.11",
125+
};

0 commit comments

Comments
 (0)