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 application sets
176 .B UFFD_FEATURE_SIGBUS
180 no page fault notification will be forwarded to
181 the user-space, instead a
183 signal is delivered to the faulting process. With this feature,
184 userfaultfd can be used for robustness purpose to simply catch
185 any access to areas within the registered address range that do not
186 have pages allocated, without having to listen to userfaultfd events.
187 No userfaultfd monitor will be required for dealing with such memory
188 accesses. For example, this feature can be useful for applications that
189 want to prevent the kernel from automatically allocating pages and filling
190 holes in sparse files when the hole is accessed thru mapped address.
193 .B UFFD_FEATURE_SIGBUS
194 feature is implicitly inherited through fork() if used in combination with
195 .BR UFFD_FEATURE_FORK .
198 Details of the various
200 operations can be found in
201 .BR ioctl_userfaultfd (2).
203 Since Linux 4.11, events other than page-fault may enabled during
208 userfaultfd can be used only with anonymous private memory mappings.
210 userfaultfd can be also used with hugetlbfs and shared memory mappings.
213 .SS Reading from the userfaultfd structure
216 from the userfaultfd file descriptor returns one or more
218 structures, each of which describes a page-fault event
219 or an event required for the non-cooperative userfaultfd usage:
224 __u8 event; /* Type of event */
228 __u64 flags; /* Flags describing fault */
229 __u64 address; /* Faulting address */
232 struct { /* Since Linux 4.11 */
233 __u32 ufd; /* Userfault file descriptor
234 of the child process */
237 struct { /* Since Linux 4.11 */
238 __u64 from; /* Old address of remapped area */
239 __u64 to; /* New address of remapped area */
240 __u64 len; /* Original mapping length */
243 struct { /* Since Linux 4.11 */
244 __u64 start; /* Start address of removed area */
245 __u64 end; /* End address of removed area */
250 /* Padding fields omitted */
255 If multiple events are available and the supplied buffer is large enough,
257 returns as many events as will fit in the supplied buffer.
258 If the buffer supplied to
260 is smaller than the size of the
267 The fields set in the
269 structure are as follows:
273 Depending of the event type,
274 different fields of the
276 union represent details required for the event processing.
277 The non-page-fault events are generated only when appropriate feature
278 is enabled during API handshake with
282 The following values can appear in the
287 .BR UFFD_EVENT_PAGEFAULT " (since Linux 4.3)"
289 The page-fault details are available in the
293 .BR UFFD_EVENT_FORK " (since Linux 4.11)"
294 Generated when the faulting process invokes
301 The event details are available in the
304 .\" FIXME describe duplication of userfault file descriptor during fork
306 .BR UFFD_EVENT_REMAP " (since Linux 4.11)"
307 Generated when the faulting process invokes
309 The event details are available in the
313 .BR UFFD_EVENT_REMOVE " (since Linux 4.11)"
314 Generated when the faulting process invokes
321 The event details are available in the
325 .BR UFFD_EVENT_UNMAP " (since Linux 4.11)"
326 Generated when the faulting process unmaps a memory range,
327 either explicitly using
333 The event details are available in the
339 The address that triggered the page fault.
342 A bit mask of flags that describe the event.
344 .BR UFFD_EVENT_PAGEFAULT ,
345 the following flag may appear:
348 .B UFFD_PAGEFAULT_FLAG_WRITE
349 If the address is in a range that was registered with the
350 .B UFFDIO_REGISTER_MODE_MISSING
352 .BR ioctl_userfaultfd (2))
353 and this flag is set, this a write fault;
354 otherwise it is a read fault.
356 .\" UFFD_PAGEFAULT_FLAG_WP is not yet supported.
360 The file descriptor associated with the userfault object
361 created for the child created by
365 The original address of the memory range that was remapped using
369 The new address of the memory range that was remapped using
373 The original length of the memory range that was remapped using
377 The start address of the memory range that was freed using
382 The end address of the memory range that was freed using
388 on a userfaultfd file descriptor can fail with the following errors:
391 The userfaultfd object has not yet been enabled using the
398 flag is enabled in the associated open file description,
399 the userfaultfd file descriptor can be monitored with
404 When events are available, the file descriptor indicates as readable.
407 flag is not enabled, then
409 (always) indicates the file as having a
413 indicates the file descriptor as both readable and writable.
414 .\" FIXME What is the reason for this seemingly odd behavior with respect
415 .\" to the O_NONBLOCK flag? (see userfaultfd_poll() in fs/userfaultfd.c).
416 .\" Something needs to be said about this.
420 returns a new file descriptor that refers to the userfaultfd object.
421 On error, \-1 is returned, and
423 is set appropriately.
427 An unsupported value was specified in
431 The per-process limit on the number of open file descriptors has been
435 The system-wide limit on the total number of open files has been
439 Insufficient kernel memory was available.
443 system call first appeared in Linux 4.3.
445 The support for hugetlbfs and shared memory areas and
446 non-page-fault events was added in Linux 4.11
449 is Linux-specific and should not be used in programs intended to be
452 Glibc does not provide a wrapper for this system call; call it using
455 The userfaultfd mechanism can be used as an alternative to
456 traditional user-space paging techniques based on the use of the
460 It can also be used to implement lazy restore
461 for checkpoint/restore mechanisms,
462 as well as post-copy migration to allow (nearly) uninterrupted execution
463 when transferring virtual machines and Linux containers
464 from one host to another.
466 The program below demonstrates the use of the userfaultfd mechanism.
467 The program creates two threads, one of which acts as the
468 page-fault handler for the process, for the pages in a demand-page zero
472 The program takes one command-line argument,
473 which is the number of pages that will be created in a mapping
474 whose page faults will be handled via userfaultfd.
475 After creating a userfaultfd object,
476 the program then creates an anonymous private mapping of the specified size
477 and registers the address range of that mapping using the
481 The program then creates a second thread that will perform the
482 task of handling page faults.
484 The main thread then walks through the pages of the mapping fetching
485 bytes from successive pages.
486 Because the pages have not yet been accessed,
487 the first access of a byte in each page will trigger a page-fault event
488 on the userfaultfd file descriptor.
490 Each of the page-fault events is handled by the second thread,
491 which sits in a loop processing input from the userfaultfd file descriptor.
492 In each loop iteration, the second thread first calls
494 to check the state of the file descriptor,
495 and then reads an event from the file descriptor.
496 All such events should be
497 .B UFFD_EVENT_PAGEFAULT
499 which the thread handles by copying a page of data into
500 the faulting region using the
505 The following is an example of what we see when running the program:
509 $ \fB./userfaultfd_demo 3\fP
510 Address returned by mmap() = 0x7fd30106c000
512 fault_handler_thread():
513 poll() returns: nready = 1; POLLIN = 1; POLLERR = 0
514 UFFD_EVENT_PAGEFAULT event: flags = 0; address = 7fd30106c00f
515 (uffdio_copy.copy returned 4096)
516 Read address 0x7fd30106c00f in main(): A
517 Read address 0x7fd30106c40f in main(): A
518 Read address 0x7fd30106c80f in main(): A
519 Read address 0x7fd30106cc0f in main(): A
521 fault_handler_thread():
522 poll() returns: nready = 1; POLLIN = 1; POLLERR = 0
523 UFFD_EVENT_PAGEFAULT event: flags = 0; address = 7fd30106d00f
524 (uffdio_copy.copy returned 4096)
525 Read address 0x7fd30106d00f in main(): B
526 Read address 0x7fd30106d40f in main(): B
527 Read address 0x7fd30106d80f in main(): B
528 Read address 0x7fd30106dc0f in main(): B
530 fault_handler_thread():
531 poll() returns: nready = 1; POLLIN = 1; POLLERR = 0
532 UFFD_EVENT_PAGEFAULT event: flags = 0; address = 7fd30106e00f
533 (uffdio_copy.copy returned 4096)
534 Read address 0x7fd30106e00f in main(): C
535 Read address 0x7fd30106e40f in main(): C
536 Read address 0x7fd30106e80f in main(): C
537 Read address 0x7fd30106ec0f in main(): C
543 /* userfaultfd_demo.c
545 Licensed under the GNU General Public License version 2 or later.
548 #include <sys/types.h>
550 #include <linux/userfaultfd.h>
559 #include <sys/mman.h>
560 #include <sys/syscall.h>
561 #include <sys/ioctl.h>
564 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\
567 static int page_size;
570 fault_handler_thread(void *arg)
572 static struct uffd_msg msg; /* Data read from userfaultfd */
573 static int fault_cnt = 0; /* Number of faults so far handled */
574 long uffd; /* userfaultfd file descriptor */
575 static char *page = NULL;
576 struct uffdio_copy uffdio_copy;
581 /* Create a page that will be copied into the faulting region */
584 page = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
585 MAP_PRIVATE | MAP_ANONYMOUS, \-1, 0);
586 if (page == MAP_FAILED)
590 /* Loop, handling incoming events on the userfaultfd
595 /* See what poll() tells us about the userfaultfd */
597 struct pollfd pollfd;
600 pollfd.events = POLLIN;
601 nready = poll(&pollfd, 1, \-1);
605 printf("\\nfault_handler_thread():\\n");
606 printf(" poll() returns: nready = %d; "
607 "POLLIN = %d; POLLERR = %d\\n", nready,
608 (pollfd.revents & POLLIN) != 0,
609 (pollfd.revents & POLLERR) != 0);
611 /* Read an event from the userfaultfd */
613 nread = read(uffd, &msg, sizeof(msg));
615 printf("EOF on userfaultfd!\\n");
622 /* We expect only one kind of event; verify that assumption */
624 if (msg.event != UFFD_EVENT_PAGEFAULT) {
625 fprintf(stderr, "Unexpected event on userfaultfd\\n");
629 /* Display info about the page\-fault event */
631 printf(" UFFD_EVENT_PAGEFAULT event: ");
632 printf("flags = %llx; ", msg.arg.pagefault.flags);
633 printf("address = %llx\\n", msg.arg.pagefault.address);
635 /* Copy the page pointed to by \(aqpage\(aq into the faulting
636 region. Vary the contents that are copied in, so that it
637 is more obvious that each fault is handled separately. */
639 memset(page, \(aqA\(aq + fault_cnt % 20, page_size);
642 uffdio_copy.src = (unsigned long) page;
644 /* We need to handle page faults in units of pages(!).
645 So, round faulting address down to page boundary */
647 uffdio_copy.dst = (unsigned long) msg.arg.pagefault.address &
649 uffdio_copy.len = page_size;
650 uffdio_copy.mode = 0;
651 uffdio_copy.copy = 0;
652 if (ioctl(uffd, UFFDIO_COPY, &uffdio_copy) == \-1)
653 errExit("ioctl\-UFFDIO_COPY");
655 printf(" (uffdio_copy.copy returned %lld)\\n",
661 main(int argc, char *argv[])
663 long uffd; /* userfaultfd file descriptor */
664 char *addr; /* Start of region handled by userfaultfd */
665 unsigned long len; /* Length of region handled by userfaultfd */
666 pthread_t thr; /* ID of thread that handles page faults */
667 struct uffdio_api uffdio_api;
668 struct uffdio_register uffdio_register;
672 fprintf(stderr, "Usage: %s num\-pages\\n", argv[0]);
676 page_size = sysconf(_SC_PAGE_SIZE);
677 len = strtoul(argv[1], NULL, 0) * page_size;
679 /* Create and enable userfaultfd object */
681 uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
683 errExit("userfaultfd");
685 uffdio_api.api = UFFD_API;
686 uffdio_api.features = 0;
687 if (ioctl(uffd, UFFDIO_API, &uffdio_api) == \-1)
688 errExit("ioctl\-UFFDIO_API");
690 /* Create a private anonymous mapping. The memory will be
691 demand\-zero paged\-\-that is, not yet allocated. When we
692 actually touch the memory, it will be allocated via
695 addr = mmap(NULL, len, PROT_READ | PROT_WRITE,
696 MAP_PRIVATE | MAP_ANONYMOUS, \-1, 0);
697 if (addr == MAP_FAILED)
700 printf("Address returned by mmap() = %p\\n", addr);
702 /* Register the memory range of the mapping we just created for
703 handling by the userfaultfd object. In mode, we request to track
704 missing pages (i.e., pages that have not yet been faulted in). */
706 uffdio_register.range.start = (unsigned long) addr;
707 uffdio_register.range.len = len;
708 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
709 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register) == \-1)
710 errExit("ioctl\-UFFDIO_REGISTER");
712 /* Create a thread that will process the userfaultfd events */
714 s = pthread_create(&thr, NULL, fault_handler_thread, (void *) uffd);
717 errExit("pthread_create");
720 /* Main thread now touches memory in the mapping, touching
721 locations 1024 bytes apart. This will trigger userfaultfd
722 events for all pages in the region. */
725 l = 0xf; /* Ensure that faulting address is not on a page
726 boundary, in order to test that we correctly
727 handle that case in fault_handling_thread() */
730 printf("Read address %p in main(): ", addr + l);
733 usleep(100000); /* Slow things down a little */
742 .BR ioctl_userfaultfd (2),
746 .IR Documentation/vm/userfaultfd.txt
747 in the Linux kernel source tree