Skip to content

Commit 720cfe2

Browse files
userfaultfd: Add test using UFFDIO_ZEROPAGE
Signed-off-by: Ricardo Branco <[email protected]>
1 parent 393d68f commit 720cfe2

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

runtest/syscalls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,6 +1773,7 @@ userfaultfd01 userfaultfd01
17731773
userfaultfd02 userfaultfd02
17741774
userfaultfd03 userfaultfd03
17751775
userfaultfd04 userfaultfd04
1776+
userfaultfd05 userfaultfd05
17761777

17771778
ustat01 ustat01
17781779
ustat02 ustat02

testcases/kernel/syscalls/userfaultfd/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
/userfaultfd02
33
/userfaultfd03
44
/userfaultfd04
5+
/userfaultfd05

testcases/kernel/syscalls/userfaultfd/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ userfaultfd01: CFLAGS += -pthread
1515
userfaultfd02: CFLAGS += -pthread
1616
userfaultfd03: CFLAGS += -pthread
1717
userfaultfd04: CFLAGS += -pthread
18+
userfaultfd05: CFLAGS += -pthread
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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 UFFDIO_ZEROPAGE
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 int uffd;
23+
24+
static int sys_userfaultfd(int flags)
25+
{
26+
return tst_syscall(__NR_userfaultfd, flags);
27+
}
28+
29+
static void set_pages(void)
30+
{
31+
page_size = sysconf(_SC_PAGE_SIZE);
32+
page = SAFE_MMAP(NULL, page_size, PROT_READ | PROT_WRITE,
33+
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
34+
}
35+
36+
static void handle_thread(void)
37+
{
38+
static struct uffd_msg msg;
39+
struct uffdio_zeropage uffdio_zeropage;
40+
41+
struct pollfd pollfd;
42+
int nready;
43+
44+
pollfd.fd = uffd;
45+
pollfd.events = POLLIN;
46+
nready = poll(&pollfd, 1, -1);
47+
if (nready == -1)
48+
tst_brk(TBROK | TERRNO, "Error on poll");
49+
50+
SAFE_READ(1, uffd, &msg, sizeof(msg));
51+
52+
if (msg.event != UFFD_EVENT_PAGEFAULT)
53+
tst_brk(TBROK | TERRNO, "Received unexpected UFFD_EVENT");
54+
55+
uffdio_zeropage.range.start = msg.arg.pagefault.address
56+
& ~(page_size - 1);
57+
uffdio_zeropage.range.len = page_size;
58+
uffdio_zeropage.mode = 0;
59+
60+
SAFE_IOCTL(uffd, UFFDIO_ZEROPAGE, &uffdio_zeropage);
61+
62+
close(uffd);
63+
}
64+
65+
static void run(void)
66+
{
67+
pthread_t thr;
68+
struct uffdio_api uffdio_api;
69+
struct uffdio_register uffdio_register;
70+
71+
set_pages();
72+
73+
TEST(sys_userfaultfd(O_CLOEXEC | O_NONBLOCK));
74+
75+
if (TST_RET == -1) {
76+
if (TST_ERR == EPERM) {
77+
tst_res(TCONF, "Hint: check /proc/sys/vm/unprivileged_userfaultfd");
78+
tst_brk(TCONF | TTERRNO,
79+
"userfaultfd() requires CAP_SYS_PTRACE on this system");
80+
} else
81+
tst_brk(TBROK | TTERRNO,
82+
"Could not create userfault file descriptor");
83+
}
84+
85+
uffd = TST_RET;
86+
uffdio_api.api = UFFD_API;
87+
uffdio_api.features = 0;
88+
SAFE_IOCTL(uffd, UFFDIO_API, &uffdio_api);
89+
90+
uffdio_register.range.start = (unsigned long) page;
91+
uffdio_register.range.len = page_size;
92+
uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
93+
94+
SAFE_IOCTL(uffd, UFFDIO_REGISTER, &uffdio_register);
95+
96+
SAFE_PTHREAD_CREATE(&thr, NULL,
97+
(void * (*)(void *)) handle_thread, NULL);
98+
99+
(void)page[0xf];
100+
101+
for (int i = 0; i < page_size; i++) {
102+
if (page[i] != '\0') {
103+
tst_res(TFAIL, "page[%d]=0x%x not zero", i, page[i]);
104+
return;
105+
}
106+
}
107+
108+
tst_res(TPASS, "Pagefault handled with UFFDIO_ZEROPAGE");
109+
110+
SAFE_PTHREAD_JOIN(thr, NULL);
111+
}
112+
113+
static struct tst_test test = {
114+
.test_all = run,
115+
.min_kver = "4.3",
116+
};

0 commit comments

Comments
 (0)