]> git.ipfire.org Git - thirdparty/man-pages.git/blobdiff - man2/userfaultfd.2
prctl.2: Fix mis-description of thread ID values in procfs
[thirdparty/man-pages.git] / man2 / userfaultfd.2
index 291dd10cfa5d715c5165c12926313fc65157a4cd..ba21ae29ba2da333ff7bda66f8fd55d84af7c13a 100644 (file)
 .\" 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 2020-02-09 "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
@@ -44,7 +44,7 @@ handling to a user-space application,
 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.
@@ -53,7 +53,7 @@ depending on the value of
 .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
@@ -75,7 +75,7 @@ flag in
 .PP
 When the last file descriptor referring to a userfaultfd object is closed,
 all memory ranges that were registered with the object are unregistered
-and unread page-fault events are flushed.
+and unread events are flushed.
 .\"
 .SS Usage
 The userfaultfd mechanism is designed to allow a thread in a multithreaded
@@ -89,18 +89,51 @@ them using the operations described in
 .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,
 and the program that executes the faulting threads
 will not necessarily cooperate with the program that handles the page faults.
 In such non-cooperative mode,
-the process that monitors userfaultfd and handles page faults,
+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.
+In addition, if the faulting process invokes
+.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 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 kerneles before 4.11, features added in 4.11
+.\" for kernels before 4.11, features added in 4.11
 .\" and limitations remaining in 4.11
 .\" Maybe it's worth adding a dedicated sub-section...
 .\"
@@ -118,7 +151,7 @@ This operation must be performed before any of the other
 operations described below (or those operations fail with the
 .BR EINVAL
 error).
-
+.PP
 After a successful
 .B UFFDIO_API
 operation,
@@ -135,47 +168,93 @@ the user-space application.
 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
 .BR read (2)
 from the userfaultfd file descriptor returns one or more
 .I uffd_msg
-structures, each of which describes a page-fault event:
-
-.nf
+structures, each of which describes a page-fault event
+or an event required for the non-cooperative userfaultfd usage:
+.PP
 .in +4n
+.EX
 struct uffd_msg {
-    __u8  event;                /* Type of event */
+    __u8  event;            /* Type of event */
     ...
     union {
         struct {
-            __u64 flags;        /* Flags describing fault */
-            __u64 address;      /* Faulting address */
+            __u64 flags;    /* Flags describing fault */
+            __u64 address;  /* Faulting address */
         } pagefault;
+
+        struct {            /* Since Linux 4.11 */
+            __u32 ufd;      /* Userfault file descriptor
+                               of the child process */
+        } fork;
+
+        struct {            /* Since Linux 4.11 */
+            __u64 from;     /* Old address of remapped area */
+            __u64 to;       /* New address of remapped area */
+            __u64 len;      /* Original mapping length */
+        } remap;
+
+        struct {            /* Since Linux 4.11 */
+            __u64 start;    /* Start address of removed area */
+            __u64 end;      /* End address of removed area */
+        } remove;
         ...
     } arg;
 
     /* 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.
@@ -187,21 +266,82 @@ structure, the
 .BR read (2)
 fails with the error
 .BR EINVAL .
-
+.PP
 The fields set in the
 .I uffd_msg
 structure are as follows:
 .TP
 .I event
 The type of event.
-Currently, only one value can appear in this field:
-.BR UFFD_EVENT_PAGEFAULT ,
-which indicates a page-fault event.
+Depending of the event type,
+different fields of the
+.I arg
+union represent details required for the event processing.
+The non-page-fault events are generated only when appropriate feature
+is enabled during API handshake with
+.B UFFDIO_API
+.BR ioctl (2).
+.IP
+The following values can appear in the
+.I event
+field:
+.RS
+.TP
+.BR UFFD_EVENT_PAGEFAULT " (since Linux 4.3)"
+A page-fault event.
+The page-fault details are available in the
+.I pagefault
+field.
+.TP
+.BR UFFD_EVENT_FORK " (since Linux 4.11)"
+Generated when the faulting process invokes
+.BR fork (2)
+(or
+.BR clone (2)
+without the
+.BR CLONE_VM
+flag).
+The event details are available in the
+.I fork
+field.
+.\" FIXME describe duplication of userfault file descriptor during fork
+.TP
+.BR UFFD_EVENT_REMAP " (since Linux 4.11)"
+Generated when the faulting process invokes
+.BR mremap (2).
+The event details are available in the
+.I remap
+field.
+.TP
+.BR UFFD_EVENT_REMOVE " (since Linux 4.11)"
+Generated when the faulting process invokes
+.BR madvise (2)
+with
+.BR MADV_DONTNEED
+or
+.BR MADV_REMOVE
+advice.
+The event details are available in the
+.I remove
+field.
+.TP
+.BR UFFD_EVENT_UNMAP " (since Linux 4.11)"
+Generated when the faulting process unmaps a memory range,
+either explicitly using
+.BR munmap (2)
+or implicitly during
+.BR mmap (2)
+or
+.BR mremap (2).
+The event details are available in the
+.I remove
+field.
+.RE
 .TP
-.I address
+.I pagefault.address
 The address that triggered the page fault.
 .TP
-.I flags
+.I pagefault.flags
 A bit mask of flags that describe the event.
 For
 .BR UFFD_EVENT_PAGEFAULT ,
@@ -218,6 +358,33 @@ otherwise it is a read fault.
 .\"
 .\" UFFD_PAGEFAULT_FLAG_WP is not yet supported.
 .RE
+.TP
+.I fork.ufd
+The file descriptor associated with the userfault object
+created for the child created by
+.BR fork (2).
+.TP
+.I remap.from
+The original address of the memory range that was remapped using
+.BR mremap (2).
+.TP
+.I remap.to
+The new address of the memory range that was remapped using
+.BR mremap (2).
+.TP
+.I remap.len
+The original length of the memory range that was remapped using
+.BR mremap (2).
+.TP
+.I remove.start
+The start address of the memory range that was freed using
+.BR madvise (2)
+or unmapped
+.TP
+.I remove.end
+The end address of the memory range that was freed using
+.BR madvise (2)
+or unmapped
 .PP
 A
 .BR read (2)
@@ -273,10 +440,21 @@ reached.
 .TP
 .B ENOMEM
 Insufficient kernel memory was available.
+.TP
+.BR EPERM " (since Linux 5.2)"
+.\" cefdca0a86be517bc390fc4541e3674b8e7803b0
+The caller is not privileged (does not have the
+.B CAP_SYS_PTRACE
+capability in the initial user namespace), and
+.I /proc/sys/vm/unprivileged_userfaultfd
+has the value 0.
 .SH VERSIONS
 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
@@ -284,7 +462,7 @@ portable.
 .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
@@ -295,13 +473,23 @@ for checkpoint/restore mechanisms,
 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.
@@ -313,13 +501,13 @@ and registers the address range of that mapping using the
 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
@@ -334,11 +522,11 @@ the faulting region using the
 .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
 
@@ -368,11 +556,11 @@ Read address 0x7fd30106e00f in main(): C
 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.
@@ -394,7 +582,7 @@ Read address 0x7fd30106ec0f in main(): C
 #include <sys/ioctl.h>
 #include <poll.h>
 
-#define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \\
+#define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \e
                         } while (0)
 
 static int page_size;
@@ -435,9 +623,9 @@ fault_handler_thread(void *arg)
         if (nready == \-1)
             errExit("poll");
 
-        printf("\\nfault_handler_thread():\\n");
+        printf("\enfault_handler_thread():\en");
         printf("    poll() returns: nready = %d; "
-                "POLLIN = %d; POLLERR = %d\\n", nready,
+                "POLLIN = %d; POLLERR = %d\en", nready,
                 (pollfd.revents & POLLIN) != 0,
                 (pollfd.revents & POLLERR) != 0);
 
@@ -445,7 +633,7 @@ fault_handler_thread(void *arg)
 
         nread = read(uffd, &msg, sizeof(msg));
         if (nread == 0) {
-            printf("EOF on userfaultfd!\\n");
+            printf("EOF on userfaultfd!\en");
             exit(EXIT_FAILURE);
         }
 
@@ -455,7 +643,7 @@ fault_handler_thread(void *arg)
         /* We expect only one kind of event; verify that assumption */
 
         if (msg.event != UFFD_EVENT_PAGEFAULT) {
-            fprintf(stderr, "Unexpected event on userfaultfd\\n");
+            fprintf(stderr, "Unexpected event on userfaultfd\en");
             exit(EXIT_FAILURE);
         }
 
@@ -463,7 +651,7 @@ fault_handler_thread(void *arg)
 
         printf("    UFFD_EVENT_PAGEFAULT event: ");
         printf("flags = %llx; ", msg.arg.pagefault.flags);
-        printf("address = %llx\\n", msg.arg.pagefault.address);
+        printf("address = %llx\en", msg.arg.pagefault.address);
 
         /* Copy the page pointed to by \(aqpage\(aq into the faulting
            region. Vary the contents that are copied in, so that it
@@ -485,7 +673,7 @@ fault_handler_thread(void *arg)
         if (ioctl(uffd, UFFDIO_COPY, &uffdio_copy) == \-1)
             errExit("ioctl\-UFFDIO_COPY");
 
-        printf("        (uffdio_copy.copy returned %lld)\\n",
+        printf("        (uffdio_copy.copy returned %lld)\en",
                 uffdio_copy.copy);
     }
 }
@@ -502,7 +690,7 @@ main(int argc, char *argv[])
     int s;
 
     if (argc != 2) {
-        fprintf(stderr, "Usage: %s num\-pages\\n", argv[0]);
+        fprintf(stderr, "Usage: %s num\-pages\en", argv[0]);
         exit(EXIT_FAILURE);
     }
 
@@ -530,7 +718,7 @@ main(int argc, char *argv[])
     if (addr == MAP_FAILED)
         errExit("mmap");
 
-    printf("Address returned by mmap() = %p\\n", addr);
+    printf("Address returned by mmap() = %p\en", addr);
 
     /* Register the memory range of the mapping we just created for
        handling by the userfaultfd object. In mode, we request to track
@@ -561,21 +749,21 @@ main(int argc, char *argv[])
     while (l < len) {
         char c = addr[l];
         printf("Read address %p in main(): ", addr + l);
-        printf("%c\\n", c);
+        printf("%c\en", c);
         l += 1024;
         usleep(100000);         /* Slow things down a little */
     }
 
     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