.\" the source, must acknowledge the copyright and authors of this work.
.\" %%%LICENSE_END
.\"
-.TH USERFAULTFD 2 2016-12-12 "Linux" "Linux Programmer's Manual"
+.TH USERFAULTFD 2 2017-09-15 "Linux" "Linux Programmer's Manual"
.SH NAME
userfaultfd \- create a file descriptor for handling page faults in user space
.SH SYNOPSIS
.nf
.B #include <sys/types.h>
.B #include <linux/userfaultfd.h>
-.sp
+.PP
.BI "int userfaultfd(int " flags );
.fi
.PP
and returns a file descriptor that refers to the new object.
The new userfaultfd object is configured using
.BR ioctl (2).
-
+.PP
Once the userfaultfd object is configured, the application can use
.BR read (2)
to receive userfaultfd notifications.
.I flags
used for the creation of the userfaultfd or subsequent calls to
.BR fcntl (2).
-
+.PP
The following values may be bitwise ORed in
.IR flags
to change the behavior of
.BR ioctl_userfaultfd (2).
When servicing the page fault events,
the fault-handling thread can trigger a wake-up for the sleeping thread.
-
+.PP
It is possible for the faulting threads and the fault-handling threads
to run in the context of different processes.
In this case, these threads may belong to different programs,
the process that monitors userfaultfd and handles page faults
needs to be aware of the changes in the virtual memory layout
of the faulting process to avoid memory corruption.
-
+.PP
Starting from Linux 4.11,
userfaultfd can also notify the fault-handling threads about changes
in the virtual memory layout of the faulting process.
.BR fork (2),
the userfaultfd objects associated with the parent may be duplicated
into the child process and the userfaultfd monitor will be notified
+(via the
+.B UFFD_EVENT_FORK
+described below)
about the file descriptor associated with the userfault objects
created for the child process,
-which allows userfaultfd monitor to perform user-space paging
+which allows the userfaultfd monitor to perform user-space paging
for the child process.
-
+Unlike page faults which have to be synchronous and require an
+explicit or implicit wakeup,
+all other events are delivered asynchronously and
+the non-cooperative process resumes execution as
+soon as the userfaultfd manager executes
+.BR read (2).
+The userfaultfd manager should carefully synchronize calls to
+.B UFFDIO_COPY
+with the processing of events.
+.PP
+The current asynchronous model of the event delivery is optimal for
+single threaded non-cooperative userfaultfd manager implementations.
+.\" Regarding the preceding sentence, Mike Rapoport says:
+.\" The major point here is that current events delivery model could be
+.\" problematic for multi-threaded monitor. I even suspect that it would be
+.\" impossible to ensure synchronization between page faults and non-page
+.\" fault events in multi-threaded monitor.
+.PP
.\" FIXME elaborate about non-cooperating mode, describe its limitations
.\" for kernels before 4.11, features added in 4.11
.\" and limitations remaining in 4.11
operations described below (or those operations fail with the
.BR EINVAL
error).
-
+.PP
After a successful
.B UFFDIO_API
operation,
The application can then use the
.B UFFDIO_COPY
or
-.B UFFDIO_ZERO
+.B UFFDIO_ZEROPAGE
.BR ioctl (2)
operations to resolve the page fault.
-
+.PP
+Starting from Linux 4.14, if the application sets the
+.B UFFD_FEATURE_SIGBUS
+feature bit using the
+.B UFFDIO_API
+.BR ioctl (2),
+no page-fault notification will be forwarded to user space.
+Instead a
+.B SIGBUS
+signal is delivered to the faulting process.
+With this feature,
+userfaultfd can be used for robustness purposes to simply catch
+any access to areas within the registered address range that do not
+have pages allocated, without having to listen to userfaultfd events.
+No userfaultfd monitor will be required for dealing with such memory
+accesses.
+For example, this feature can be useful for applications that
+want to prevent the kernel from automatically allocating pages and filling
+holes in sparse files when the hole is accessed through a memory mapping.
+.PP
+The
+.B UFFD_FEATURE_SIGBUS
+feature is implicitly inherited through
+.BR fork (2)
+if used in combination with
+.BR UFFD_FEATURE_FORK .
+.PP
Details of the various
.BR ioctl (2)
operations can be found in
.BR ioctl_userfaultfd (2).
-
+.PP
Since Linux 4.11, events other than page-fault may enabled during
.B UFFDIO_API
operation.
-
+.PP
Up to Linux 4.11,
userfaultfd can be used only with anonymous private memory mappings.
Since Linux 4.11,
userfaultfd can be also used with hugetlbfs and shared memory mappings.
-
+.PP
.\"
.SS Reading from the userfaultfd structure
Each
.I uffd_msg
structures, each of which describes a page-fault event
or an event required for the non-cooperative userfaultfd usage:
-
-.nf
+.PP
.in +4n
+.EX
struct uffd_msg {
__u8 event; /* Type of event */
...
/* Padding fields omitted */
} __packed;
+.EE
.in
-.fi
-
+.PP
If multiple events are available and the supplied buffer is large enough,
.BR read (2)
returns as many events as will fit in the supplied buffer.
.BR read (2)
fails with the error
.BR EINVAL .
-
+.PP
The fields set in the
.I uffd_msg
structure are as follows:
is enabled during API handshake with
.B UFFDIO_API
.BR ioctl (2).
-
+.IP
The following values can appear in the
.I event
field:
.TP
.BR UFFD_EVENT_FORK " (since Linux 4.11)"
Generated when the faulting process invokes
-.BR fork (2).
+.BR fork (2)
+(or
+.BR clone (2)
+without the
+.BR CLONE_VM
+flag).
The event details are available in the
.I fork
field.
.TP
.I fork.ufd
The file descriptor associated with the userfault object
-created for the child process
+created for the child created by
+.BR fork (2).
.TP
.I remap.from
The original address of the memory range that was remapped using
The
.BR userfaultfd ()
system call first appeared in Linux 4.3.
+.PP
+The support for hugetlbfs and shared memory areas and
+non-page-fault events was added in Linux 4.11
.SH CONFORMING TO
.BR userfaultfd ()
is Linux-specific and should not be used in programs intended to be
.SH NOTES
Glibc does not provide a wrapper for this system call; call it using
.BR syscall (2).
-
+.PP
The userfaultfd mechanism can be used as an alternative to
traditional user-space paging techniques based on the use of the
.BR SIGSEGV
as well as post-copy migration to allow (nearly) uninterrupted execution
when transferring virtual machines and Linux containers
from one host to another.
+.SH BUGS
+If the
+.B UFFD_FEATURE_EVENT_FORK
+is enabled and a system call from the
+.BR fork (2)
+family is interrupted by a signal or failed, a stale userfaultfd descriptor
+might be created.
+In this case, a spurious
+.B UFFD_EVENT_FORK
+will be delivered to the userfaultfd monitor.
.SH EXAMPLE
The program below demonstrates the use of the userfaultfd mechanism.
The program creates two threads, one of which acts as the
page-fault handler for the process, for the pages in a demand-page zero
region created using
.BR mmap (2).
-
+.PP
The program takes one command-line argument,
which is the number of pages that will be created in a mapping
whose page faults will be handled via userfaultfd.
operation.
The program then creates a second thread that will perform the
task of handling page faults.
-
+.PP
The main thread then walks through the pages of the mapping fetching
bytes from successive pages.
Because the pages have not yet been accessed,
the first access of a byte in each page will trigger a page-fault event
on the userfaultfd file descriptor.
-
+.PP
Each of the page-fault events is handled by the second thread,
which sits in a loop processing input from the userfaultfd file descriptor.
In each loop iteration, the second thread first calls
.B UFFDIO_COPY
.BR ioctl (2)
operation.
-
+.PP
The following is an example of what we see when running the program:
-
-.nf
+.PP
.in +4n
+.EX
$ \fB./userfaultfd_demo 3\fP
Address returned by mmap() = 0x7fd30106c000
Read address 0x7fd30106e40f in main(): C
Read address 0x7fd30106e80f in main(): C
Read address 0x7fd30106ec0f in main(): C
+.EE
.in
-.fi
.SS Program source
\&
-.nf
+.EX
/* userfaultfd_demo.c
Licensed under the GNU General Public License version 2 or later.
exit(EXIT_SUCCESS);
}
-.fi
+.EE
.SH SEE ALSO
.BR fcntl (2),
.BR ioctl (2),
.BR ioctl_userfaultfd (2),
.BR madvise (2),
.BR mmap (2)
-
-.IR Documentation/vm/userfaultfd.txt
+.PP
+.IR Documentation/admin-guide/mm/userfaultfd.rst
in the Linux kernel source tree
-
+.PP