Skip to content

Commit 475f2db

Browse files
ricardobranco777acerv
authored andcommitted
userfaultfd: Add test using UFFDIO_POISON
Signed-off-by: Ricardo Branco <rbranco@suse.de> Reviewed-by: Cyril Hrubis <chrubis@suse.cz> Acked-by: Andrea Cervesato <andrea.cervesato@suse.com>
1 parent dcf0c14 commit 475f2db

6 files changed

Lines changed: 161 additions & 0 deletions

File tree

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ AC_CHECK_TYPES([struct sockaddr_vm],,,[
279279
])
280280

281281
AC_CHECK_TYPES([struct uffdio_move],,,[#include <linux/userfaultfd.h>])
282+
AC_CHECK_TYPES([struct uffdio_poison],,,[#include <linux/userfaultfd.h>])
282283
AC_CHECK_TYPES([struct uffdio_writeprotect],,,[#include <linux/userfaultfd.h>])
283284

284285
# Tools knobs

include/lapi/userfaultfd.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,19 @@ struct uffdio_writeprotect {
233233
};
234234
#endif /* HAVE_STRUCT_UFFDIO_WRITEPROTECT */
235235

236+
#ifndef HAVE_STRUCT_UFFDIO_POISON
237+
#define UFFD_FEATURE_POISON (1<<14)
238+
#define _UFFDIO_POISON (0x08)
239+
#define UFFDIO_POISON _IOWR(UFFDIO, _UFFDIO_POISON, \
240+
struct uffdio_poison)
241+
struct uffdio_poison {
242+
struct uffdio_range range;
243+
#define UFFDIO_POISON_MODE_DONTWAKE ((__u64)1<<0)
244+
__u64 mode;
245+
__s64 updated;
246+
};
247+
#endif /* HAVE_STRUCT_UFFDIO_POISON */
248+
236249
#define SAFE_USERFAULTFD(flags, retry) \
237250
safe_userfaultfd(__FILE__, __LINE__, (flags), (retry))
238251

runtest/syscalls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,6 +1779,7 @@ userfaultfd02 userfaultfd02
17791779
userfaultfd03 userfaultfd03
17801780
userfaultfd04 userfaultfd04
17811781
userfaultfd05 userfaultfd05
1782+
userfaultfd06 userfaultfd06
17821783

17831784
ustat01 ustat01
17841785
ustat02 ustat02

testcases/kernel/syscalls/userfaultfd/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
/userfaultfd03
44
/userfaultfd04
55
/userfaultfd05
6+
/userfaultfd06

testcases/kernel/syscalls/userfaultfd/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ userfaultfd02: CFLAGS += -pthread
1616
userfaultfd03: CFLAGS += -pthread
1717
userfaultfd04: CFLAGS += -pthread
1818
userfaultfd05: CFLAGS += -pthread
19+
userfaultfd06: CFLAGS += -pthread
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* Copyright (c) 2026 SUSE LLC
4+
* Author: Ricardo Branco <rbranco@suse.com>
5+
*/
6+
7+
/*\
8+
* Force a pagefault event and handle it using :manpage:`userfaultfd(2)`
9+
* from a different thread testing UFFDIO_POISON.
10+
*/
11+
12+
#include "config.h"
13+
#include <poll.h>
14+
#include <setjmp.h>
15+
#include <signal.h>
16+
#include <unistd.h>
17+
#include "tst_test.h"
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 int uffd;
25+
static int poison_fault_seen;
26+
static volatile int sigbus_seen;
27+
static sigjmp_buf jmpbuf;
28+
29+
static void sigbus_handler(int sig)
30+
{
31+
if (sig == SIGBUS) {
32+
sigbus_seen = 1;
33+
siglongjmp(jmpbuf, 1);
34+
}
35+
}
36+
37+
static void setup(void)
38+
{
39+
struct sigaction sa = {};
40+
41+
sa.sa_handler = sigbus_handler;
42+
sigemptyset(&sa.sa_mask);
43+
SAFE_SIGACTION(SIGBUS, &sa, NULL);
44+
}
45+
46+
static void set_pages(void)
47+
{
48+
page_size = sysconf(_SC_PAGE_SIZE);
49+
page = SAFE_MMAP(NULL, page_size, PROT_READ | PROT_WRITE,
50+
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
51+
}
52+
53+
static void reset_pages(void)
54+
{
55+
if (page) {
56+
SAFE_MUNMAP(page, page_size);
57+
page = NULL;
58+
}
59+
}
60+
61+
static void *handle_thread(void)
62+
{
63+
static struct uffd_msg msg;
64+
struct uffdio_poison uffdio_poison = {};
65+
struct pollfd pollfd;
66+
int nready;
67+
68+
pollfd.fd = uffd;
69+
pollfd.events = POLLIN;
70+
nready = poll(&pollfd, 1, -1);
71+
if (nready == -1)
72+
tst_brk(TBROK | TERRNO, "Error on poll");
73+
74+
SAFE_READ(1, uffd, &msg, sizeof(msg));
75+
76+
if (msg.event != UFFD_EVENT_PAGEFAULT)
77+
tst_brk(TFAIL, "Received unexpected UFFD_EVENT %d", msg.event);
78+
79+
tst_atomic_store(1, &poison_fault_seen);
80+
81+
/* Poison the page that triggered the fault */
82+
uffdio_poison.range.start = msg.arg.pagefault.address & ~(page_size - 1);
83+
uffdio_poison.range.len = page_size;
84+
85+
SAFE_IOCTL(uffd, UFFDIO_POISON, &uffdio_poison);
86+
87+
close(uffd);
88+
return NULL;
89+
}
90+
91+
static void run(void)
92+
{
93+
pthread_t thr;
94+
struct uffdio_api uffdio_api = {};
95+
struct uffdio_register uffdio_register;
96+
char dummy;
97+
98+
poison_fault_seen = 0;
99+
sigbus_seen = 0;
100+
set_pages();
101+
102+
uffd = SAFE_USERFAULTFD(O_CLOEXEC | O_NONBLOCK, false);
103+
104+
uffdio_api.api = UFFD_API;
105+
uffdio_api.features = UFFD_FEATURE_POISON;
106+
107+
SAFE_IOCTL(uffd, UFFDIO_API, &uffdio_api);
108+
109+
if (!(uffdio_api.features & UFFD_FEATURE_POISON))
110+
tst_brk(TCONF, "UFFD_FEATURE_POISON not supported");
111+
112+
uffdio_register.range.start = (unsigned long) page;
113+
uffdio_register.range.len = page_size;
114+
uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
115+
116+
SAFE_IOCTL(uffd, UFFDIO_REGISTER, &uffdio_register);
117+
118+
SAFE_PTHREAD_CREATE(&thr, NULL, (void *) handle_thread, NULL);
119+
120+
/* Try to read from the page: should trigger fault, get poisoned, then SIGBUS */
121+
if (sigsetjmp(jmpbuf, 1) == 0) {
122+
LTP_VAR_USED(dummy) = page[0];
123+
}
124+
125+
SAFE_PTHREAD_JOIN(thr, NULL);
126+
reset_pages();
127+
128+
int poisoned = tst_atomic_load(&poison_fault_seen);
129+
130+
if (poisoned && sigbus_seen)
131+
tst_res(TPASS, "POISON successfully triggered SIGBUS");
132+
else if (poisoned && !sigbus_seen)
133+
tst_res(TFAIL, "POISON fault seen but no SIGBUS received");
134+
else if (!poisoned && sigbus_seen)
135+
tst_res(TFAIL, "SIGBUS received but no poison fault seen");
136+
else
137+
tst_res(TFAIL, "No poison fault or SIGBUS observed");
138+
}
139+
140+
static struct tst_test test = {
141+
.test_all = run,
142+
.setup = setup,
143+
.cleanup = reset_pages,
144+
};

0 commit comments

Comments
 (0)