1 .\" Copyright (c) 2016, IBM Corporation.
2 .\" Written by Mike Rapoport <rppt@linux.vnet.ibm.com>
3 .\" and Copyright (C) 2017 Michael Kerrisk <mtk.manpages@gmail.com>
5 .\" %%%LICENSE_START(VERBATIM)
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date. The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein. The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
27 .TH USERFAULTFD 2 2017-09-15 "Linux" "Linux Programmer's Manual"
29 userfaultfd \- create a file descriptor for handling page faults in user space
32 .B #include <sys/types.h>
33 .B #include <linux/userfaultfd.h>
35 .BI "int userfaultfd(int " flags );
39 There is no glibc wrapper for this system call; see NOTES.
42 creates a new userfaultfd object that can be used for delegation of page-fault
43 handling to a user-space application,
44 and returns a file descriptor that refers to the new object.
45 The new userfaultfd object is configured using
48 Once the userfaultfd object is configured, the application can use
50 to receive userfaultfd notifications.
51 The reads from userfaultfd may be blocking or non-blocking,
52 depending on the value of
54 used for the creation of the userfaultfd or subsequent calls to
57 The following values may be bitwise ORed in
59 to change the behavior of
63 Enable the close-on-exec flag for the new userfaultfd file descriptor.
64 See the description of the
70 Enables non-blocking operation for the userfaultfd object.
71 See the description of the
76 When the last file descriptor referring to a userfaultfd object is closed,
77 all memory ranges that were registered with the object are unregistered
78 and unread events are flushed.
81 The userfaultfd mechanism is designed to allow a thread in a multithreaded
82 program to perform user-space paging for the other threads in the process.
83 When a page fault occurs for one of the regions registered
84 to the userfaultfd object,
85 the faulting thread is put to sleep and
86 an event is generated that can be read via the userfaultfd file descriptor.
87 The fault-handling thread reads events from this file descriptor and services
88 them using the operations described in
89 .BR ioctl_userfaultfd (2).
90 When servicing the page fault events,
91 the fault-handling thread can trigger a wake-up for the sleeping thread.
93 It is possible for the faulting threads and the fault-handling threads
94 to run in the context of different processes.
95 In this case, these threads may belong to different programs,
96 and the program that executes the faulting threads
97 will not necessarily cooperate with the program that handles the page faults.
98 In such non-cooperative mode,
99 the process that monitors userfaultfd and handles page faults
100 needs to be aware of the changes in the virtual memory layout
101 of the faulting process to avoid memory corruption.
103 Starting from Linux 4.11,
104 userfaultfd can also notify the fault-handling threads about changes
105 in the virtual memory layout of the faulting process.
106 In addition, if the faulting process invokes
108 the userfaultfd objects associated with the parent may be duplicated
109 into the child process and the userfaultfd monitor will be notified
113 about the file descriptor associated with the userfault objects
114 created for the child process,
115 which allows the userfaultfd monitor to perform user-space paging
116 for the child process.
117 Unlike page faults which have to be synchronous and require an
118 explicit or implicit wakeup,
119 all other events are delivered asynchronously and
120 the non-cooperative process resumes execution as
121 soon as the userfaultfd manager executes
123 The userfaultfd manager should carefully synchronize calls to
125 with the processing of events.
127 The current asynchronous model of the event delivery is optimal for
128 single threaded non-cooperative userfaultfd manager implementations.
129 .\" Regarding the preceding sentence, Mike Rapoport says:
130 .\" The major point here is that current events delivery model could be
131 .\" problematic for multi-threaded monitor. I even suspect that it would be
132 .\" impossible to ensure synchronization between page faults and non-page
133 .\" fault events in multi-threaded monitor.
135 .\" FIXME elaborate about non-cooperating mode, describe its limitations
136 .\" for kernels before 4.11, features added in 4.11
137 .\" and limitations remaining in 4.11
138 .\" Maybe it's worth adding a dedicated sub-section...
140 .SS Userfaultfd operation
141 After the userfaultfd object is created with
143 the application must enable it using the
147 This operation allows a handshake between the kernel and user space
148 to determine the API version and supported features.
149 This operation must be performed before any of the other
151 operations described below (or those operations fail with the
158 the application then registers memory address ranges using the
162 After successful completion of a
165 a page fault occurring in the requested memory range, and satisfying
166 the mode defined at the registration time, will be forwarded by the kernel to
167 the user-space application.
168 The application can then use the
173 operations to resolve the page fault.
175 Starting from Linux 4.14, if the application sets the
176 .B UFFD_FEATURE_SIGBUS
177 feature bit using the
180 no page-fault notification will be forwarded to user space.
183 signal is delivered to the faulting process.
185 userfaultfd can be used for robustness purposes to simply catch
186 any access to areas within the registered address range that do not
187 have pages allocated, without having to listen to userfaultfd events.
188 No userfaultfd monitor will be required for dealing with such memory
190 For example, this feature can be useful for applications that
191 want to prevent the kernel from automatically allocating pages and filling
192 holes in sparse files when the hole is accessed through a memory mapping.
195 .B UFFD_FEATURE_SIGBUS
196 feature is implicitly inherited through
198 if used in combination with
199 .BR UFFD_FEATURE_FORK .
201 Details of the various
203 operations can be found in
204 .BR ioctl_userfaultfd (2).
206 Since Linux 4.11, events other than page-fault may enabled during
211 userfaultfd can be used only with anonymous private memory mappings.
213 userfaultfd can be also used with hugetlbfs and shared memory mappings.
216 .SS Reading from the userfaultfd structure
219 from the userfaultfd file descriptor returns one or more
221 structures, each of which describes a page-fault event
222 or an event required for the non-cooperative userfaultfd usage:
227 __u8 event; /* Type of event */
231 __u64 flags; /* Flags describing fault */
232 __u64 address; /* Faulting address */
235 struct { /* Since Linux 4.11 */
236 __u32 ufd; /* Userfault file descriptor
237 of the child process */
240 struct { /* Since Linux 4.11 */
241 __u64 from; /* Old address of remapped area */
242 __u64 to; /* New address of remapped area */
243 __u64 len; /* Original mapping length */
246 struct { /* Since Linux 4.11 */
247 __u64 start; /* Start address of removed area */
248 __u64 end; /* End address of removed area */
253 /* Padding fields omitted */
258 If multiple events are available and the supplied buffer is large enough,
260 returns as many events as will fit in the supplied buffer.
261 If the buffer supplied to
263 is smaller than the size of the
270 The fields set in the
272 structure are as follows:
276 Depending of the event type,
277 different fields of the
279 union represent details required for the event processing.
280 The non-page-fault events are generated only when appropriate feature
281 is enabled during API handshake with
285 The following values can appear in the
290 .BR UFFD_EVENT_PAGEFAULT " (since Linux 4.3)"
292 The page-fault details are available in the
296 .BR UFFD_EVENT_FORK " (since Linux 4.11)"
297 Generated when the faulting process invokes
304 The event details are available in the
307 .\" FIXME describe duplication of userfault file descriptor during fork
309 .BR UFFD_EVENT_REMAP " (since Linux 4.11)"
310 Generated when the faulting process invokes
312 The event details are available in the
316 .BR UFFD_EVENT_REMOVE " (since Linux 4.11)"
317 Generated when the faulting process invokes
324 The event details are available in the
328 .BR UFFD_EVENT_UNMAP " (since Linux 4.11)"
329 Generated when the faulting process unmaps a memory range,
330 either explicitly using
336 The event details are available in the
342 The address that triggered the page fault.
345 A bit mask of flags that describe the event.
347 .BR UFFD_EVENT_PAGEFAULT ,
348 the following flag may appear:
351 .B UFFD_PAGEFAULT_FLAG_WRITE
352 If the address is in a range that was registered with the
353 .B UFFDIO_REGISTER_MODE_MISSING
355 .BR ioctl_userfaultfd (2))
356 and this flag is set, this a write fault;
357 otherwise it is a read fault.
359 .\" UFFD_PAGEFAULT_FLAG_WP is not yet supported.
363 The file descriptor associated with the userfault object
364 created for the child created by
368 The original address of the memory range that was remapped using
372 The new address of the memory range that was remapped using
376 The original length of the memory range that was remapped using
380 The start address of the memory range that was freed using
385 The end address of the memory range that was freed using
391 on a userfaultfd file descriptor can fail with the following errors:
394 The userfaultfd object has not yet been enabled using the
401 flag is enabled in the associated open file description,
402 the userfaultfd file descriptor can be monitored with
407 When events are available, the file descriptor indicates as readable.
410 flag is not enabled, then
412 (always) indicates the file as having a
416 indicates the file descriptor as both readable and writable.
417 .\" FIXME What is the reason for this seemingly odd behavior with respect
418 .\" to the O_NONBLOCK flag? (see userfaultfd_poll() in fs/userfaultfd.c).
419 .\" Something needs to be said about this.
423 returns a new file descriptor that refers to the userfaultfd object.
424 On error, \-1 is returned, and
426 is set appropriately.
430 An unsupported value was specified in
434 The per-process limit on the number of open file descriptors has been
438 The system-wide limit on the total number of open files has been
442 Insufficient kernel memory was available.
446 system call first appeared in Linux 4.3.
448 The support for hugetlbfs and shared memory areas and
449 non-page-fault events was added in Linux 4.11
452 is Linux-specific and should not be used in programs intended to be
455 Glibc does not provide a wrapper for this system call; call it using
458 The userfaultfd mechanism can be used as an alternative to
459 traditional user-space paging techniques based on the use of the
463 It can also be used to implement lazy restore
464 for checkpoint/restore mechanisms,
465 as well as post-copy migration to allow (nearly) uninterrupted execution
466 when transferring virtual machines and Linux containers
467 from one host to another.
470 .B UFFD_FEATURE_EVENT_FORK
471 is enabled and a system call from the
473 family is interrupted by a signal or failed, a stale userfaultfd descriptor
475 In this case, a spurious
477 will be delivered to the userfaultfd monitor.
479 The program below demonstrates the use of the userfaultfd mechanism.
480 The program creates two threads, one of which acts as the
481 page-fault handler for the process, for the pages in a demand-page zero
485 The program takes one command-line argument,
486 which is the number of pages that will be created in a mapping
487 whose page faults will be handled via userfaultfd.
488 After creating a userfaultfd object,
489 the program then creates an anonymous private mapping of the specified size
490 and registers the address range of that mapping using the
494 The program then creates a second thread that will perform the
495 task of handling page faults.
497 The main thread then walks through the pages of the mapping fetching
498 bytes from successive pages.
499 Because the pages have not yet been accessed,
500 the first access of a byte in each page will trigger a page-fault event
501 on the userfaultfd file descriptor.
503 Each of the page-fault events is handled by the second thread,
504 which sits in a loop processing input from the userfaultfd file descriptor.
505 In each loop iteration, the second thread first calls
507 to check the state of the file descriptor,
508 and then reads an event from the file descriptor.
509 All such events should be
510 .B UFFD_EVENT_PAGEFAULT
512 which the thread handles by copying a page of data into
513 the faulting region using the
518 The following is an example of what we see when running the program:
522 $ \fB./userfaultfd_demo 3\fP
523 Address returned by mmap() = 0x7fd30106c000
525 fault_handler_thread():
526 poll() returns: nready = 1; POLLIN = 1; POLLERR = 0
527 UFFD_EVENT_PAGEFAULT event: flags = 0; address = 7fd30106c00f
528 (uffdio_copy.copy returned 4096)
529 Read address 0x7fd30106c00f in main(): A
530 Read address 0x7fd30106c40f in main(): A
531 Read address 0x7fd30106c80f in main(): A
532 Read address 0x7fd30106cc0f in main(): A
534 fault_handler_thread():
535 poll() returns: nready = 1; POLLIN = 1; POLLERR = 0
536 UFFD_EVENT_PAGEFAULT event: flags = 0; address = 7fd30106d00f
537 (uffdio_copy.copy returned 4096)
538 Read address 0x7fd30106d00f in main(): B
539 Read address 0x7fd30106d40f in main(): B
540 Read address 0x7fd30106d80f in main(): B
541 Read address 0x7fd30106dc0f in main(): B
543 fault_handler_thread():
544 poll() returns: nready = 1; POLLIN = 1; POLLERR = 0
545 UFFD_EVENT_PAGEFAULT event: flags = 0; address = 7fd30106e00f
546 (uffdio_copy.copy returned 4096)
547 Read address 0x7fd30106e00f in main(): C
548 Read address 0x7fd30106e40f in main(): C
549 Read address 0x7fd30106e80f in main(): C
550 Read address 0x7fd30106ec0f in main(): C
556 /* userfaultfd_demo.c
558 Licensed under the GNU General Public License version 2 or later.
561 #include <sys/types.h>
563 #include <linux/userfaultfd.h>
572 #include <sys/mman.h>
573 #include <sys/syscall.h>
574 #include <sys/ioctl.h>
577 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\
580 static int page_size;
583 fault_handler_thread(void *arg)
585 static struct uffd_msg msg; /* Data read from userfaultfd */
586 static int fault_cnt = 0; /* Number of faults so far handled */
587 long uffd; /* userfaultfd file descriptor */
588 static char *page = NULL;
589 struct uffdio_copy uffdio_copy;
594 /* Create a page that will be copied into the faulting region */
597 page = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
598 MAP_PRIVATE | MAP_ANONYMOUS, \-1, 0);
599 if (page == MAP_FAILED)
603 /* Loop, handling incoming events on the userfaultfd
608 /* See what poll() tells us about the userfaultfd */
610 struct pollfd pollfd;
613 pollfd.events = POLLIN;
614 nready = poll(&pollfd, 1, \-1);
618 printf("\\nfault_handler_thread():\\n");
619 printf(" poll() returns: nready = %d; "
620 "POLLIN = %d; POLLERR = %d\\n", nready,
621 (pollfd.revents & POLLIN) != 0,
622 (pollfd.revents & POLLERR) != 0);
624 /* Read an event from the userfaultfd */
626 nread = read(uffd, &msg, sizeof(msg));
628 printf("EOF on userfaultfd!\\n");
635 /* We expect only one kind of event; verify that assumption */
637 if (msg.event != UFFD_EVENT_PAGEFAULT) {
638 fprintf(stderr, "Unexpected event on userfaultfd\\n");
642 /* Display info about the page\-fault event */
644 printf(" UFFD_EVENT_PAGEFAULT event: ");
645 printf("flags = %llx; ", msg.arg.pagefault.flags);
646 printf("address = %llx\\n", msg.arg.pagefault.address);
648 /* Copy the page pointed to by \(aqpage\(aq into the faulting
649 region. Vary the contents that are copied in, so that it
650 is more obvious that each fault is handled separately. */
652 memset(page, \(aqA\(aq + fault_cnt % 20, page_size);
655 uffdio_copy.src = (unsigned long) page;
657 /* We need to handle page faults in units of pages(!).
658 So, round faulting address down to page boundary */
660 uffdio_copy.dst = (unsigned long) msg.arg.pagefault.address &
662 uffdio_copy.len = page_size;
663 uffdio_copy.mode = 0;
664 uffdio_copy.copy = 0;
665 if (ioctl(uffd, UFFDIO_COPY, &uffdio_copy) == \-1)
666 errExit("ioctl\-UFFDIO_COPY");
668 printf(" (uffdio_copy.copy returned %lld)\\n",
674 main(int argc, char *argv[])
676 long uffd; /* userfaultfd file descriptor */
677 char *addr; /* Start of region handled by userfaultfd */
678 unsigned long len; /* Length of region handled by userfaultfd */
679 pthread_t thr; /* ID of thread that handles page faults */
680 struct uffdio_api uffdio_api;
681 struct uffdio_register uffdio_register;
685 fprintf(stderr, "Usage: %s num\-pages\\n", argv[0]);
689 page_size = sysconf(_SC_PAGE_SIZE);
690 len = strtoul(argv[1], NULL, 0) * page_size;
692 /* Create and enable userfaultfd object */
694 uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
696 errExit("userfaultfd");
698 uffdio_api.api = UFFD_API;
699 uffdio_api.features = 0;
700 if (ioctl(uffd, UFFDIO_API, &uffdio_api) == \-1)
701 errExit("ioctl\-UFFDIO_API");
703 /* Create a private anonymous mapping. The memory will be
704 demand\-zero paged\-\-that is, not yet allocated. When we
705 actually touch the memory, it will be allocated via
708 addr = mmap(NULL, len, PROT_READ | PROT_WRITE,
709 MAP_PRIVATE | MAP_ANONYMOUS, \-1, 0);
710 if (addr == MAP_FAILED)
713 printf("Address returned by mmap() = %p\\n", addr);
715 /* Register the memory range of the mapping we just created for
716 handling by the userfaultfd object. In mode, we request to track
717 missing pages (i.e., pages that have not yet been faulted in). */
719 uffdio_register.range.start = (unsigned long) addr;
720 uffdio_register.range.len = len;
721 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
722 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register) == \-1)
723 errExit("ioctl\-UFFDIO_REGISTER");
725 /* Create a thread that will process the userfaultfd events */
727 s = pthread_create(&thr, NULL, fault_handler_thread, (void *) uffd);
730 errExit("pthread_create");
733 /* Main thread now touches memory in the mapping, touching
734 locations 1024 bytes apart. This will trigger userfaultfd
735 events for all pages in the region. */
738 l = 0xf; /* Ensure that faulting address is not on a page
739 boundary, in order to test that we correctly
740 handle that case in fault_handling_thread() */
743 printf("Read address %p in main(): ", addr + l);
746 usleep(100000); /* Slow things down a little */
755 .BR ioctl_userfaultfd (2),
759 .IR Documentation/vm/userfaultfd.txt
760 in the Linux kernel source tree