2 * Linux UFFD-WP support
4 * Copyright Virtuozzo GmbH, 2020
7 * Andrey Gruzdev <andrey.gruzdev@virtuozzo.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or
10 * later. See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "qemu/bitops.h"
15 #include "qemu/error-report.h"
16 #include "qemu/userfaultfd.h"
19 #include <sys/syscall.h>
20 #include <sys/ioctl.h>
23 UFFD_UNINITIALIZED
= 0,
28 int uffd_open(int flags
)
30 #if defined(__NR_userfaultfd)
31 static uffd_open_mode open_mode
;
34 /* Detect how to generate uffd desc when run the 1st time */
35 if (open_mode
== UFFD_UNINITIALIZED
) {
37 * Make /dev/userfaultfd the default approach because it has better
38 * permission controls, meanwhile allows kernel faults without any
39 * privilege requirement (e.g. SYS_CAP_PTRACE).
41 uffd_dev
= open("/dev/userfaultfd", O_RDWR
| O_CLOEXEC
);
43 open_mode
= UFFD_USE_DEV_PATH
;
45 /* Fallback to the system call */
46 open_mode
= UFFD_USE_SYSCALL
;
48 trace_uffd_detect_open_mode(open_mode
);
51 if (open_mode
== UFFD_USE_DEV_PATH
) {
52 assert(uffd_dev
>= 0);
53 return ioctl(uffd_dev
, USERFAULTFD_IOC_NEW
, flags
);
56 return syscall(__NR_userfaultfd
, flags
);
63 * uffd_query_features: query UFFD features
65 * Returns: 0 on success, negative value in case of an error
67 * @features: parameter to receive 'uffdio_api.features'
69 int uffd_query_features(uint64_t *features
)
72 struct uffdio_api api_struct
= { 0 };
75 uffd_fd
= uffd_open(O_CLOEXEC
);
77 trace_uffd_query_features_nosys(errno
);
81 api_struct
.api
= UFFD_API
;
82 api_struct
.features
= 0;
84 if (ioctl(uffd_fd
, UFFDIO_API
, &api_struct
)) {
85 trace_uffd_query_features_api_failed(errno
);
88 *features
= api_struct
.features
;
97 * uffd_create_fd: create UFFD file descriptor
99 * Returns non-negative file descriptor or negative value in case of an error
101 * @features: UFFD features to request
102 * @non_blocking: create UFFD file descriptor for non-blocking operation
104 int uffd_create_fd(uint64_t features
, bool non_blocking
)
108 struct uffdio_api api_struct
= { 0 };
109 uint64_t ioctl_mask
= BIT(_UFFDIO_REGISTER
) | BIT(_UFFDIO_UNREGISTER
);
111 flags
= O_CLOEXEC
| (non_blocking
? O_NONBLOCK
: 0);
112 uffd_fd
= uffd_open(flags
);
114 trace_uffd_create_fd_nosys(errno
);
118 api_struct
.api
= UFFD_API
;
119 api_struct
.features
= features
;
120 if (ioctl(uffd_fd
, UFFDIO_API
, &api_struct
)) {
121 trace_uffd_create_fd_api_failed(errno
);
124 if ((api_struct
.ioctls
& ioctl_mask
) != ioctl_mask
) {
125 trace_uffd_create_fd_api_noioctl(ioctl_mask
, api_struct
.ioctls
);
137 * uffd_close_fd: close UFFD file descriptor
139 * @uffd_fd: UFFD file descriptor
141 void uffd_close_fd(int uffd_fd
)
143 assert(uffd_fd
>= 0);
148 * uffd_register_memory: register memory range via UFFD-IO
150 * Returns 0 in case of success, negative value in case of an error
152 * @uffd_fd: UFFD file descriptor
153 * @addr: base address of memory range
154 * @length: length of memory range
155 * @mode: UFFD register mode (UFFDIO_REGISTER_MODE_MISSING, ...)
156 * @ioctls: optional pointer to receive supported IOCTL mask
158 int uffd_register_memory(int uffd_fd
, void *addr
, uint64_t length
,
159 uint64_t mode
, uint64_t *ioctls
)
161 struct uffdio_register uffd_register
;
163 uffd_register
.range
.start
= (uintptr_t) addr
;
164 uffd_register
.range
.len
= length
;
165 uffd_register
.mode
= mode
;
167 if (ioctl(uffd_fd
, UFFDIO_REGISTER
, &uffd_register
)) {
168 trace_uffd_register_memory_failed(addr
, length
, mode
, errno
);
172 *ioctls
= uffd_register
.ioctls
;
179 * uffd_unregister_memory: un-register memory range with UFFD-IO
181 * Returns 0 in case of success, negative value in case of an error
183 * @uffd_fd: UFFD file descriptor
184 * @addr: base address of memory range
185 * @length: length of memory range
187 int uffd_unregister_memory(int uffd_fd
, void *addr
, uint64_t length
)
189 struct uffdio_range uffd_range
;
191 uffd_range
.start
= (uintptr_t) addr
;
192 uffd_range
.len
= length
;
194 if (ioctl(uffd_fd
, UFFDIO_UNREGISTER
, &uffd_range
)) {
195 trace_uffd_unregister_memory_failed(addr
, length
, errno
);
203 * uffd_change_protection: protect/un-protect memory range for writes via UFFD-IO
205 * Returns 0 on success, negative value in case of error
207 * @uffd_fd: UFFD file descriptor
208 * @addr: base address of memory range
209 * @length: length of memory range
210 * @wp: write-protect/unprotect
211 * @dont_wake: do not wake threads waiting on wr-protected page
213 int uffd_change_protection(int uffd_fd
, void *addr
, uint64_t length
,
214 bool wp
, bool dont_wake
)
216 struct uffdio_writeprotect uffd_writeprotect
;
218 uffd_writeprotect
.range
.start
= (uintptr_t) addr
;
219 uffd_writeprotect
.range
.len
= length
;
220 if (!wp
&& dont_wake
) {
221 /* DONTWAKE is meaningful only on protection release */
222 uffd_writeprotect
.mode
= UFFDIO_WRITEPROTECT_MODE_DONTWAKE
;
224 uffd_writeprotect
.mode
= (wp
? UFFDIO_WRITEPROTECT_MODE_WP
: 0);
227 if (ioctl(uffd_fd
, UFFDIO_WRITEPROTECT
, &uffd_writeprotect
)) {
228 error_report("uffd_change_protection() failed: addr=%p len=%" PRIu64
229 " mode=%" PRIx64
" errno=%i", addr
, length
,
230 (uint64_t) uffd_writeprotect
.mode
, errno
);
238 * uffd_copy_page: copy range of pages to destination via UFFD-IO
240 * Copy range of source pages to the destination to resolve
241 * missing page fault somewhere in the destination range.
243 * Returns 0 on success, negative value in case of an error
245 * @uffd_fd: UFFD file descriptor
246 * @dst_addr: destination base address
247 * @src_addr: source base address
248 * @length: length of the range to copy
249 * @dont_wake: do not wake threads waiting on missing page
251 int uffd_copy_page(int uffd_fd
, void *dst_addr
, void *src_addr
,
252 uint64_t length
, bool dont_wake
)
254 struct uffdio_copy uffd_copy
;
256 uffd_copy
.dst
= (uintptr_t) dst_addr
;
257 uffd_copy
.src
= (uintptr_t) src_addr
;
258 uffd_copy
.len
= length
;
259 uffd_copy
.mode
= dont_wake
? UFFDIO_COPY_MODE_DONTWAKE
: 0;
261 if (ioctl(uffd_fd
, UFFDIO_COPY
, &uffd_copy
)) {
262 error_report("uffd_copy_page() failed: dst_addr=%p src_addr=%p length=%" PRIu64
263 " mode=%" PRIx64
" errno=%i", dst_addr
, src_addr
,
264 length
, (uint64_t) uffd_copy
.mode
, errno
);
272 * uffd_zero_page: fill range of pages with zeroes via UFFD-IO
274 * Fill range pages with zeroes to resolve missing page fault within the range.
276 * Returns 0 on success, negative value in case of an error
278 * @uffd_fd: UFFD file descriptor
279 * @addr: base address
280 * @length: length of the range to fill with zeroes
281 * @dont_wake: do not wake threads waiting on missing page
283 int uffd_zero_page(int uffd_fd
, void *addr
, uint64_t length
, bool dont_wake
)
285 struct uffdio_zeropage uffd_zeropage
;
287 uffd_zeropage
.range
.start
= (uintptr_t) addr
;
288 uffd_zeropage
.range
.len
= length
;
289 uffd_zeropage
.mode
= dont_wake
? UFFDIO_ZEROPAGE_MODE_DONTWAKE
: 0;
291 if (ioctl(uffd_fd
, UFFDIO_ZEROPAGE
, &uffd_zeropage
)) {
292 error_report("uffd_zero_page() failed: addr=%p length=%" PRIu64
293 " mode=%" PRIx64
" errno=%i", addr
, length
,
294 (uint64_t) uffd_zeropage
.mode
, errno
);
302 * uffd_wakeup: wake up threads waiting on page UFFD-managed page fault resolution
304 * Wake up threads waiting on any page/pages from the designated range.
305 * The main use case is when during some period, page faults are resolved
306 * via UFFD-IO IOCTLs with MODE_DONTWAKE flag set, then after that all waits
307 * for the whole memory range are satisfied in a single call to uffd_wakeup().
309 * Returns 0 on success, negative value in case of an error
311 * @uffd_fd: UFFD file descriptor
312 * @addr: base address
313 * @length: length of the range
315 int uffd_wakeup(int uffd_fd
, void *addr
, uint64_t length
)
317 struct uffdio_range uffd_range
;
319 uffd_range
.start
= (uintptr_t) addr
;
320 uffd_range
.len
= length
;
322 if (ioctl(uffd_fd
, UFFDIO_WAKE
, &uffd_range
)) {
323 error_report("uffd_wakeup() failed: addr=%p length=%" PRIu64
" errno=%i",
324 addr
, length
, errno
);
332 * uffd_read_events: read pending UFFD events
334 * Returns number of fetched messages, 0 if non is available or
335 * negative value in case of an error
337 * @uffd_fd: UFFD file descriptor
338 * @msgs: pointer to message buffer
339 * @count: number of messages that can fit in the buffer
341 int uffd_read_events(int uffd_fd
, struct uffd_msg
*msgs
, int count
)
345 res
= read(uffd_fd
, msgs
, count
* sizeof(struct uffd_msg
));
346 } while (res
< 0 && errno
== EINTR
);
348 if ((res
< 0 && errno
== EAGAIN
)) {
352 error_report("uffd_read_events() failed: errno=%i", errno
);
356 return (int) (res
/ sizeof(struct uffd_msg
));
360 * uffd_poll_events: poll UFFD file descriptor for read
362 * Returns true if events are available for read, false otherwise
364 * @uffd_fd: UFFD file descriptor
365 * @tmo: timeout value
367 bool uffd_poll_events(int uffd_fd
, int tmo
)
370 struct pollfd poll_fd
= { .fd
= uffd_fd
, .events
= POLLIN
, .revents
= 0 };
373 res
= poll(&poll_fd
, 1, tmo
);
374 } while (res
< 0 && errno
== EINTR
);
380 error_report("uffd_poll_events() failed: errno=%i", errno
);
384 return (poll_fd
.revents
& POLLIN
) != 0;