1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2019 SUSE LLC
4 * Author: Christian Amann <camann@suse.com>
5 *
6 * Test userfaultfd
7 *
8 * Force a pagefault event and handle it using userfaultfd
9 * from a different thread
10 */
11
12 #include "config.h"
13 #include "tst_test.h"
14
15 #ifdef HAVE_LINUX_USERFAULTFD_H
16 #include <linux/userfaultfd.h>
17 #include <poll.h>
18
19 #include "tst_safe_macros.h"
20 #include "tst_safe_pthread.h"
21 #include "lapi/syscalls.h"
22
23 static int page_size;
24 static char *page;
25 static void *copy_page;
26 static int uffd;
27
sys_userfaultfd(int flags)28 static int sys_userfaultfd(int flags)
29 {
30 return tst_syscall(__NR_userfaultfd, flags);
31 }
32
set_pages(void)33 static void set_pages(void)
34 {
35 page_size = sysconf(_SC_PAGE_SIZE);
36 page = SAFE_MMAP(NULL, page_size, PROT_READ | PROT_WRITE,
37 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
38 copy_page = SAFE_MMAP(NULL, page_size, PROT_READ | PROT_WRITE,
39 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
40 }
41
handle_thread(void)42 static void handle_thread(void)
43 {
44 static struct uffd_msg msg;
45 struct uffdio_copy uffdio_copy;
46
47 struct pollfd pollfd;
48 int nready;
49
50 pollfd.fd = uffd;
51 pollfd.events = POLLIN;
52 nready = poll(&pollfd, 1, -1);
53 if (nready == -1)
54 tst_brk(TBROK | TERRNO,
55 "Error on poll");
56
57 SAFE_READ(1, uffd, &msg, sizeof(msg));
58
59 if (msg.event != UFFD_EVENT_PAGEFAULT)
60 tst_brk(TBROK | TERRNO,
61 "Received unexpected UFFD_EVENT");
62
63 memset(copy_page, 'X', page_size);
64
65 uffdio_copy.src = (unsigned long) copy_page;
66
67 uffdio_copy.dst = (unsigned long) msg.arg.pagefault.address
68 & ~(page_size - 1);
69 uffdio_copy.len = page_size;
70 uffdio_copy.mode = 0;
71 uffdio_copy.copy = 0;
72 SAFE_IOCTL(uffd, UFFDIO_COPY, &uffdio_copy);
73
74 close(uffd);
75 }
76
run(void)77 static void run(void)
78 {
79 pthread_t thr;
80 struct uffdio_api uffdio_api;
81 struct uffdio_register uffdio_register;
82
83 set_pages();
84
85 TEST(sys_userfaultfd(O_CLOEXEC | O_NONBLOCK));
86
87 if (TST_RET == -1) {
88 if (TST_ERR == EPERM) {
89 tst_res(TCONF, "Hint: check /proc/sys/vm/unprivileged_userfaultfd");
90 tst_brk(TCONF | TTERRNO,
91 "userfaultfd() requires CAP_SYS_PTRACE on this system");
92 } else
93 tst_brk(TBROK | TTERRNO,
94 "Could not create userfault file descriptor");
95 }
96
97 uffd = TST_RET;
98 uffdio_api.api = UFFD_API;
99 uffdio_api.features = 0;
100 SAFE_IOCTL(uffd, UFFDIO_API, &uffdio_api);
101
102 uffdio_register.range.start = (unsigned long) page;
103 uffdio_register.range.len = page_size;
104 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
105
106 SAFE_IOCTL(uffd, UFFDIO_REGISTER, &uffdio_register);
107
108 SAFE_PTHREAD_CREATE(&thr, NULL,
109 (void * (*)(void *)) handle_thread, NULL);
110
111 char c = page[0xf];
112
113 if (c == 'X')
114 tst_res(TPASS, "Pagefault handled!");
115 else
116 tst_res(TFAIL, "Pagefault not handled!");
117
118 SAFE_PTHREAD_JOIN(thr, NULL);
119 }
120
121 static struct tst_test test = {
122 .test_all = run,
123 .min_kver = "4.3",
124 .timeout = 20
125 };
126
127 #else
128 TST_TEST_TCONF("This system does not provide userfaultfd support");
129 #endif
130