Skip to content

Commit d06f420

Browse files
userfaultfd: Add test using UFFD_USER_MODE_ONLY
Signed-off-by: Ricardo Branco <[email protected]>
1 parent 9daa401 commit d06f420

File tree

4 files changed

+117
-0
lines changed

4 files changed

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

0 commit comments

Comments
 (0)