Skip to content

Commit df452c8

Browse files
userfaultfd: Add test using UFFD_USER_MODE_ONLY
Signed-off-by: Ricardo Branco <[email protected]>
1 parent 451623a commit df452c8

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed

runtest/syscalls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,6 +1771,7 @@ umount2_02 umount2_02
17711771

17721772
userfaultfd01 userfaultfd01
17731773
userfaultfd02 userfaultfd02
1774+
userfaultfd03 userfaultfd03
17741775

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

testcases/kernel/syscalls/userfaultfd/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ include $(top_srcdir)/include/mk/generic_leaf_target.mk
1313

1414
userfaultfd01: CFLAGS += -pthread
1515
userfaultfd02: CFLAGS += -pthread
16+
userfaultfd03: CFLAGS += -pthread
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
* Test userfaultfd using UFFD_USER_MODE_ONLY
8+
*
9+
* Force a pagefault event and handle it using userfaultfd
10+
* from a different thread
11+
*/
12+
13+
#include "config.h"
14+
#include "tst_test.h"
15+
16+
#include <poll.h>
17+
18+
#include "tst_safe_macros.h"
19+
#include "tst_safe_pthread.h"
20+
#include "lapi/userfaultfd.h"
21+
22+
static int page_size;
23+
static char *page;
24+
static void *copy_page;
25+
static int uffd;
26+
27+
static int sys_userfaultfd(int flags)
28+
{
29+
return tst_syscall(__NR_userfaultfd, flags);
30+
}
31+
32+
static void set_pages(void)
33+
{
34+
page_size = sysconf(_SC_PAGE_SIZE);
35+
page = SAFE_MMAP(NULL, page_size, PROT_READ | PROT_WRITE,
36+
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
37+
copy_page = SAFE_MMAP(NULL, page_size, PROT_READ | PROT_WRITE,
38+
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
39+
}
40+
41+
static void handle_thread(void)
42+
{
43+
static struct uffd_msg msg;
44+
struct uffdio_copy uffdio_copy;
45+
46+
struct pollfd pollfd;
47+
int nready;
48+
49+
pollfd.fd = uffd;
50+
pollfd.events = POLLIN;
51+
nready = poll(&pollfd, 1, -1);
52+
if (nready == -1)
53+
tst_brk(TBROK | TERRNO,
54+
"Error on poll");
55+
56+
SAFE_READ(1, uffd, &msg, sizeof(msg));
57+
58+
if (msg.event != UFFD_EVENT_PAGEFAULT)
59+
tst_brk(TBROK | TERRNO,
60+
"Received unexpected UFFD_EVENT");
61+
62+
memset(copy_page, 'X', page_size);
63+
64+
uffdio_copy.src = (unsigned long) copy_page;
65+
66+
uffdio_copy.dst = (unsigned long) msg.arg.pagefault.address
67+
& ~(page_size - 1);
68+
uffdio_copy.len = page_size;
69+
uffdio_copy.mode = 0;
70+
uffdio_copy.copy = 0;
71+
SAFE_IOCTL(uffd, UFFDIO_COPY, &uffdio_copy);
72+
73+
close(uffd);
74+
}
75+
76+
static void run(void)
77+
{
78+
pthread_t thr;
79+
struct uffdio_api uffdio_api;
80+
struct uffdio_register uffdio_register;
81+
82+
set_pages();
83+
84+
TEST(sys_userfaultfd(O_CLOEXEC | O_NONBLOCK | UFFD_USER_MODE_ONLY));
85+
86+
if (TST_RET == -1) {
87+
tst_brk(TBROK | TTERRNO,
88+
"Could not create userfault file descriptor");
89+
}
90+
91+
uffd = TST_RET;
92+
uffdio_api.api = UFFD_API;
93+
uffdio_api.features = 0;
94+
SAFE_IOCTL(uffd, UFFDIO_API, &uffdio_api);
95+
96+
uffdio_register.range.start = (unsigned long) page;
97+
uffdio_register.range.len = page_size;
98+
uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
99+
100+
SAFE_IOCTL(uffd, UFFDIO_REGISTER, &uffdio_register);
101+
102+
SAFE_PTHREAD_CREATE(&thr, NULL,
103+
(void * (*)(void *)) handle_thread, NULL);
104+
105+
char c = page[0xf];
106+
107+
if (c == 'X')
108+
tst_res(TPASS, "Pagefault handled in user-mode!");
109+
else
110+
tst_res(TFAIL, "Pagefault not handled in user-mode!");
111+
112+
SAFE_PTHREAD_JOIN(thr, NULL);
113+
}
114+
115+
static struct tst_test test = {
116+
.test_all = run,
117+
.min_kver = "5.11",
118+
};

0 commit comments

Comments
 (0)