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-05-03 "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 Details of the various
177 operations can be found in
178 .BR ioctl_userfaultfd (2).
180 Since Linux 4.11, events other than page-fault may enabled during
185 userfaultfd can be used only with anonymous private memory mappings.
187 userfaultfd can be also used with hugetlbfs and shared memory mappings.
190 .SS Reading from the userfaultfd structure
193 from the userfaultfd file descriptor returns one or more
195 structures, each of which describes a page-fault event
196 or an event required for the non-cooperative userfaultfd usage:
201 __u8 event; /* Type of event */
205 __u64 flags; /* Flags describing fault */
206 __u64 address; /* Faulting address */
209 struct { /* Since Linux 4.11 */
210 __u32 ufd; /* Userfault file descriptor
211 of the child process */
214 struct { /* Since Linux 4.11 */
215 __u64 from; /* Old address of remapped area */
216 __u64 to; /* New address of remapped area */
217 __u64 len; /* Original mapping length */
220 struct { /* Since Linux 4.11 */
221 __u64 start; /* Start address of removed area */
222 __u64 end; /* End address of removed area */
227 /* Padding fields omitted */
232 If multiple events are available and the supplied buffer is large enough,
234 returns as many events as will fit in the supplied buffer.
235 If the buffer supplied to
237 is smaller than the size of the
244 The fields set in the
246 structure are as follows:
250 Depending of the event type,
251 different fields of the
253 union represent details required for the event processing.
254 The non-page-fault events are generated only when appropriate feature
255 is enabled during API handshake with
259 The following values can appear in the
264 .BR UFFD_EVENT_PAGEFAULT " (since Linux 4.3)"
266 The page-fault details are available in the
270 .BR UFFD_EVENT_FORK " (since Linux 4.11)"
271 Generated when the faulting process invokes
278 The event details are available in the
281 .\" FIXME describe duplication of userfault file descriptor during fork
283 .BR UFFD_EVENT_REMAP " (since Linux 4.11)"
284 Generated when the faulting process invokes
286 The event details are available in the
290 .BR UFFD_EVENT_REMOVE " (since Linux 4.11)"
291 Generated when the faulting process invokes
298 The event details are available in the
302 .BR UFFD_EVENT_UNMAP " (since Linux 4.11)"
303 Generated when the faulting process unmaps a memory range,
304 either explicitly using
310 The event details are available in the
316 The address that triggered the page fault.
319 A bit mask of flags that describe the event.
321 .BR UFFD_EVENT_PAGEFAULT ,
322 the following flag may appear:
325 .B UFFD_PAGEFAULT_FLAG_WRITE
326 If the address is in a range that was registered with the
327 .B UFFDIO_REGISTER_MODE_MISSING
329 .BR ioctl_userfaultfd (2))
330 and this flag is set, this a write fault;
331 otherwise it is a read fault.
333 .\" UFFD_PAGEFAULT_FLAG_WP is not yet supported.
337 The file descriptor associated with the userfault object
338 created for the child created by
342 The original address of the memory range that was remapped using
346 The new address of the memory range that was remapped using
350 The original length of the memory range that was remapped using
354 The start address of the memory range that was freed using
359 The end address of the memory range that was freed using
365 on a userfaultfd file descriptor can fail with the following errors:
368 The userfaultfd object has not yet been enabled using the
375 flag is enabled in the associated open file description,
376 the userfaultfd file descriptor can be monitored with
381 When events are available, the file descriptor indicates as readable.
384 flag is not enabled, then
386 (always) indicates the file as having a
390 indicates the file descriptor as both readable and writable.
391 .\" FIXME What is the reason for this seemingly odd behavior with respect
392 .\" to the O_NONBLOCK flag? (see userfaultfd_poll() in fs/userfaultfd.c).
393 .\" Something needs to be said about this.
397 returns a new file descriptor that refers to the userfaultfd object.
398 On error, \-1 is returned, and
400 is set appropriately.
404 An unsupported value was specified in
408 The per-process limit on the number of open file descriptors has been
412 The system-wide limit on the total number of open files has been
416 Insufficient kernel memory was available.
420 system call first appeared in Linux 4.3.
422 The support for hugetlbfs and shared memory areas and
423 non-page-fault events was added in Linux 4.11
426 is Linux-specific and should not be used in programs intended to be
429 Glibc does not provide a wrapper for this system call; call it using
432 The userfaultfd mechanism can be used as an alternative to
433 traditional user-space paging techniques based on the use of the
437 It can also be used to implement lazy restore
438 for checkpoint/restore mechanisms,
439 as well as post-copy migration to allow (nearly) uninterrupted execution
440 when transferring virtual machines and Linux containers
441 from one host to another.
443 The program below demonstrates the use of the userfaultfd mechanism.
444 The program creates two threads, one of which acts as the
445 page-fault handler for the process, for the pages in a demand-page zero
449 The program takes one command-line argument,
450 which is the number of pages that will be created in a mapping
451 whose page faults will be handled via userfaultfd.
452 After creating a userfaultfd object,
453 the program then creates an anonymous private mapping of the specified size
454 and registers the address range of that mapping using the
458 The program then creates a second thread that will perform the
459 task of handling page faults.
461 The main thread then walks through the pages of the mapping fetching
462 bytes from successive pages.
463 Because the pages have not yet been accessed,
464 the first access of a byte in each page will trigger a page-fault event
465 on the userfaultfd file descriptor.
467 Each of the page-fault events is handled by the second thread,
468 which sits in a loop processing input from the userfaultfd file descriptor.
469 In each loop iteration, the second thread first calls
471 to check the state of the file descriptor,
472 and then reads an event from the file descriptor.
473 All such events should be
474 .B UFFD_EVENT_PAGEFAULT
476 which the thread handles by copying a page of data into
477 the faulting region using the
482 The following is an example of what we see when running the program:
486 $ \fB./userfaultfd_demo 3\fP
487 Address returned by mmap() = 0x7fd30106c000
489 fault_handler_thread():
490 poll() returns: nready = 1; POLLIN = 1; POLLERR = 0
491 UFFD_EVENT_PAGEFAULT event: flags = 0; address = 7fd30106c00f
492 (uffdio_copy.copy returned 4096)
493 Read address 0x7fd30106c00f in main(): A
494 Read address 0x7fd30106c40f in main(): A
495 Read address 0x7fd30106c80f in main(): A
496 Read address 0x7fd30106cc0f in main(): A
498 fault_handler_thread():
499 poll() returns: nready = 1; POLLIN = 1; POLLERR = 0
500 UFFD_EVENT_PAGEFAULT event: flags = 0; address = 7fd30106d00f
501 (uffdio_copy.copy returned 4096)
502 Read address 0x7fd30106d00f in main(): B
503 Read address 0x7fd30106d40f in main(): B
504 Read address 0x7fd30106d80f in main(): B
505 Read address 0x7fd30106dc0f in main(): B
507 fault_handler_thread():
508 poll() returns: nready = 1; POLLIN = 1; POLLERR = 0
509 UFFD_EVENT_PAGEFAULT event: flags = 0; address = 7fd30106e00f
510 (uffdio_copy.copy returned 4096)
511 Read address 0x7fd30106e00f in main(): C
512 Read address 0x7fd30106e40f in main(): C
513 Read address 0x7fd30106e80f in main(): C
514 Read address 0x7fd30106ec0f in main(): C
520 /* userfaultfd_demo.c
522 Licensed under the GNU General Public License version 2 or later.
525 #include <sys/types.h>
527 #include <linux/userfaultfd.h>
536 #include <sys/mman.h>
537 #include <sys/syscall.h>
538 #include <sys/ioctl.h>
541 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\
544 static int page_size;
547 fault_handler_thread(void *arg)
549 static struct uffd_msg msg; /* Data read from userfaultfd */
550 static int fault_cnt = 0; /* Number of faults so far handled */
551 long uffd; /* userfaultfd file descriptor */
552 static char *page = NULL;
553 struct uffdio_copy uffdio_copy;
558 /* Create a page that will be copied into the faulting region */
561 page = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
562 MAP_PRIVATE | MAP_ANONYMOUS, \-1, 0);
563 if (page == MAP_FAILED)
567 /* Loop, handling incoming events on the userfaultfd
572 /* See what poll() tells us about the userfaultfd */
574 struct pollfd pollfd;
577 pollfd.events = POLLIN;
578 nready = poll(&pollfd, 1, \-1);
582 printf("\\nfault_handler_thread():\\n");
583 printf(" poll() returns: nready = %d; "
584 "POLLIN = %d; POLLERR = %d\\n", nready,
585 (pollfd.revents & POLLIN) != 0,
586 (pollfd.revents & POLLERR) != 0);
588 /* Read an event from the userfaultfd */
590 nread = read(uffd, &msg, sizeof(msg));
592 printf("EOF on userfaultfd!\\n");
599 /* We expect only one kind of event; verify that assumption */
601 if (msg.event != UFFD_EVENT_PAGEFAULT) {
602 fprintf(stderr, "Unexpected event on userfaultfd\\n");
606 /* Display info about the page\-fault event */
608 printf(" UFFD_EVENT_PAGEFAULT event: ");
609 printf("flags = %llx; ", msg.arg.pagefault.flags);
610 printf("address = %llx\\n", msg.arg.pagefault.address);
612 /* Copy the page pointed to by \(aqpage\(aq into the faulting
613 region. Vary the contents that are copied in, so that it
614 is more obvious that each fault is handled separately. */
616 memset(page, \(aqA\(aq + fault_cnt % 20, page_size);
619 uffdio_copy.src = (unsigned long) page;
621 /* We need to handle page faults in units of pages(!).
622 So, round faulting address down to page boundary */
624 uffdio_copy.dst = (unsigned long) msg.arg.pagefault.address &
626 uffdio_copy.len = page_size;
627 uffdio_copy.mode = 0;
628 uffdio_copy.copy = 0;
629 if (ioctl(uffd, UFFDIO_COPY, &uffdio_copy) == \-1)
630 errExit("ioctl\-UFFDIO_COPY");
632 printf(" (uffdio_copy.copy returned %lld)\\n",
638 main(int argc, char *argv[])
640 long uffd; /* userfaultfd file descriptor */
641 char *addr; /* Start of region handled by userfaultfd */
642 unsigned long len; /* Length of region handled by userfaultfd */
643 pthread_t thr; /* ID of thread that handles page faults */
644 struct uffdio_api uffdio_api;
645 struct uffdio_register uffdio_register;
649 fprintf(stderr, "Usage: %s num\-pages\\n", argv[0]);
653 page_size = sysconf(_SC_PAGE_SIZE);
654 len = strtoul(argv[1], NULL, 0) * page_size;
656 /* Create and enable userfaultfd object */
658 uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
660 errExit("userfaultfd");
662 uffdio_api.api = UFFD_API;
663 uffdio_api.features = 0;
664 if (ioctl(uffd, UFFDIO_API, &uffdio_api) == \-1)
665 errExit("ioctl\-UFFDIO_API");
667 /* Create a private anonymous mapping. The memory will be
668 demand\-zero paged\-\-that is, not yet allocated. When we
669 actually touch the memory, it will be allocated via
672 addr = mmap(NULL, len, PROT_READ | PROT_WRITE,
673 MAP_PRIVATE | MAP_ANONYMOUS, \-1, 0);
674 if (addr == MAP_FAILED)
677 printf("Address returned by mmap() = %p\\n", addr);
679 /* Register the memory range of the mapping we just created for
680 handling by the userfaultfd object. In mode, we request to track
681 missing pages (i.e., pages that have not yet been faulted in). */
683 uffdio_register.range.start = (unsigned long) addr;
684 uffdio_register.range.len = len;
685 uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING;
686 if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register) == \-1)
687 errExit("ioctl\-UFFDIO_REGISTER");
689 /* Create a thread that will process the userfaultfd events */
691 s = pthread_create(&thr, NULL, fault_handler_thread, (void *) uffd);
694 errExit("pthread_create");
697 /* Main thread now touches memory in the mapping, touching
698 locations 1024 bytes apart. This will trigger userfaultfd
699 events for all pages in the region. */
702 l = 0xf; /* Ensure that faulting address is not on a page
703 boundary, in order to test that we correctly
704 handle that case in fault_handling_thread() */
707 printf("Read address %p in main(): ", addr + l);
710 usleep(100000); /* Slow things down a little */
719 .BR ioctl_userfaultfd (2),
723 .IR Documentation/vm/userfaultfd.txt
724 in the Linux kernel source tree