1f9451df2SMike Rapoport.. _userfaultfd: 225edd8bfSAndrea Arcangeli 3f9451df2SMike Rapoport=========== 4f9451df2SMike RapoportUserfaultfd 5f9451df2SMike Rapoport=========== 6f9451df2SMike Rapoport 7f9451df2SMike RapoportObjective 8f9451df2SMike Rapoport========= 925edd8bfSAndrea Arcangeli 1025edd8bfSAndrea ArcangeliUserfaults allow the implementation of on-demand paging from userland 1125edd8bfSAndrea Arcangeliand more generally they allow userland to take control of various 1225edd8bfSAndrea Arcangelimemory page faults, something otherwise only the kernel code could do. 1325edd8bfSAndrea Arcangeli 1425edd8bfSAndrea ArcangeliFor example userfaults allows a proper and more optimal implementation 1514a7e51fSMauro Carvalho Chehabof the ``PROT_NONE+SIGSEGV`` trick. 1625edd8bfSAndrea Arcangeli 17f9451df2SMike RapoportDesign 18f9451df2SMike Rapoport====== 1925edd8bfSAndrea Arcangeli 2014a7e51fSMauro Carvalho ChehabUserfaults are delivered and resolved through the ``userfaultfd`` syscall. 2125edd8bfSAndrea Arcangeli 2214a7e51fSMauro Carvalho ChehabThe ``userfaultfd`` (aside from registering and unregistering virtual 2325edd8bfSAndrea Arcangelimemory ranges) provides two primary functionalities: 2425edd8bfSAndrea Arcangeli 2514a7e51fSMauro Carvalho Chehab1) ``read/POLLIN`` protocol to notify a userland thread of the faults 2625edd8bfSAndrea Arcangeli happening 2725edd8bfSAndrea Arcangeli 2814a7e51fSMauro Carvalho Chehab2) various ``UFFDIO_*`` ioctls that can manage the virtual memory regions 2914a7e51fSMauro Carvalho Chehab registered in the ``userfaultfd`` that allows userland to efficiently 3025edd8bfSAndrea Arcangeli resolve the userfaults it receives via 1) or to manage the virtual 3125edd8bfSAndrea Arcangeli memory in the background 3225edd8bfSAndrea Arcangeli 3325edd8bfSAndrea ArcangeliThe real advantage of userfaults if compared to regular virtual memory 3425edd8bfSAndrea Arcangelimanagement of mremap/mprotect is that the userfaults in all their 3525edd8bfSAndrea Arcangelioperations never involve heavyweight structures like vmas (in fact the 36c1e8d7c6SMichel Lespinasse``userfaultfd`` runtime load never takes the mmap_lock for writing). 3725edd8bfSAndrea Arcangeli 3825edd8bfSAndrea ArcangeliVmas are not suitable for page- (or hugepage) granular fault tracking 3925edd8bfSAndrea Arcangeliwhen dealing with virtual address spaces that could span 4025edd8bfSAndrea ArcangeliTerabytes. Too many vmas would be needed for that. 4125edd8bfSAndrea Arcangeli 4214a7e51fSMauro Carvalho ChehabThe ``userfaultfd`` once opened by invoking the syscall, can also be 4325edd8bfSAndrea Arcangelipassed using unix domain sockets to a manager process, so the same 4425edd8bfSAndrea Arcangelimanager process could handle the userfaults of a multitude of 4525edd8bfSAndrea Arcangelidifferent processes without them being aware about what is going on 4614a7e51fSMauro Carvalho Chehab(well of course unless they later try to use the ``userfaultfd`` 4725edd8bfSAndrea Arcangelithemselves on the same region the manager is already tracking, which 4814a7e51fSMauro Carvalho Chehabis a corner case that would currently return ``-EBUSY``). 4925edd8bfSAndrea Arcangeli 50f9451df2SMike RapoportAPI 51f9451df2SMike Rapoport=== 5225edd8bfSAndrea Arcangeli 5314a7e51fSMauro Carvalho ChehabWhen first opened the ``userfaultfd`` must be enabled invoking the 5414a7e51fSMauro Carvalho Chehab``UFFDIO_API`` ioctl specifying a ``uffdio_api.api`` value set to ``UFFD_API`` (or 5514a7e51fSMauro Carvalho Chehaba later API version) which will specify the ``read/POLLIN`` protocol 5614a7e51fSMauro Carvalho Chehabuserland intends to speak on the ``UFFD`` and the ``uffdio_api.features`` 5714a7e51fSMauro Carvalho Chehabuserland requires. The ``UFFDIO_API`` ioctl if successful (i.e. if the 5814a7e51fSMauro Carvalho Chehabrequested ``uffdio_api.api`` is spoken also by the running kernel and the 59a9b85f94SAndrea Arcangelirequested features are going to be enabled) will return into 6014a7e51fSMauro Carvalho Chehab``uffdio_api.features`` and ``uffdio_api.ioctls`` two 64bit bitmasks of 61a9b85f94SAndrea Arcangelirespectively all the available features of the read(2) protocol and 62a9b85f94SAndrea Arcangelithe generic ioctl available. 6325edd8bfSAndrea Arcangeli 6414a7e51fSMauro Carvalho ChehabThe ``uffdio_api.features`` bitmask returned by the ``UFFDIO_API`` ioctl 6514a7e51fSMauro Carvalho Chehabdefines what memory types are supported by the ``userfaultfd`` and what 66b8da5cd4SAxel Rasmussenevents, except page fault notifications, may be generated: 675a02026dSMike Rapoport 68b8da5cd4SAxel Rasmussen- The ``UFFD_FEATURE_EVENT_*`` flags indicate that various other events 69b8da5cd4SAxel Rasmussen other than page faults are supported. These events are described in more 70b8da5cd4SAxel Rasmussen detail below in the `Non-cooperative userfaultfd`_ section. 715a02026dSMike Rapoport 72b8da5cd4SAxel Rasmussen- ``UFFD_FEATURE_MISSING_HUGETLBFS`` and ``UFFD_FEATURE_MISSING_SHMEM`` 73b8da5cd4SAxel Rasmussen indicate that the kernel supports ``UFFDIO_REGISTER_MODE_MISSING`` 74b8da5cd4SAxel Rasmussen registrations for hugetlbfs and shared memory (covering all shmem APIs, 75b8da5cd4SAxel Rasmussen i.e. tmpfs, ``IPCSHM``, ``/dev/zero``, ``MAP_SHARED``, ``memfd_create``, 76b8da5cd4SAxel Rasmussen etc) virtual memory areas, respectively. 775a02026dSMike Rapoport 78b8da5cd4SAxel Rasmussen- ``UFFD_FEATURE_MINOR_HUGETLBFS`` indicates that the kernel supports 79b8da5cd4SAxel Rasmussen ``UFFDIO_REGISTER_MODE_MINOR`` registration for hugetlbfs virtual memory 80*964ab004SAxel Rasmussen areas. ``UFFD_FEATURE_MINOR_SHMEM`` is the analogous feature indicating 81*964ab004SAxel Rasmussen support for shmem virtual memory areas. 825a02026dSMike Rapoport 83b8da5cd4SAxel RasmussenThe userland application should set the feature flags it intends to use 84b8da5cd4SAxel Rasmussenwhen invoking the ``UFFDIO_API`` ioctl, to request that those features be 85b8da5cd4SAxel Rasmussenenabled if supported. 86b8da5cd4SAxel Rasmussen 87b8da5cd4SAxel RasmussenOnce the ``userfaultfd`` API has been enabled the ``UFFDIO_REGISTER`` 88b8da5cd4SAxel Rasmussenioctl should be invoked (if present in the returned ``uffdio_api.ioctls`` 89b8da5cd4SAxel Rasmussenbitmask) to register a memory range in the ``userfaultfd`` by setting the 9014a7e51fSMauro Carvalho Chehabuffdio_register structure accordingly. The ``uffdio_register.mode`` 9125edd8bfSAndrea Arcangelibitmask will specify to the kernel which kind of faults to track for 92b8da5cd4SAxel Rasmussenthe range. The ``UFFDIO_REGISTER`` ioctl will return the 9314a7e51fSMauro Carvalho Chehab``uffdio_register.ioctls`` bitmask of ioctls that are suitable to resolve 9425edd8bfSAndrea Arcangeliuserfaults on the range registered. Not all ioctls will necessarily be 95b8da5cd4SAxel Rasmussensupported for all memory types (e.g. anonymous memory vs. shmem vs. 96b8da5cd4SAxel Rasmussenhugetlbfs), or all types of intercepted faults. 9725edd8bfSAndrea Arcangeli 9814a7e51fSMauro Carvalho ChehabUserland can use the ``uffdio_register.ioctls`` to manage the virtual 9925edd8bfSAndrea Arcangeliaddress space in the background (to add or potentially also remove 10014a7e51fSMauro Carvalho Chehabmemory from the ``userfaultfd`` registered range). This means a userfault 10125edd8bfSAndrea Arcangelicould be triggering just before userland maps in the background the 10225edd8bfSAndrea Arcangeliuser-faulted page. 10325edd8bfSAndrea Arcangeli 104b8da5cd4SAxel RasmussenResolving Userfaults 105b8da5cd4SAxel Rasmussen-------------------- 106b8da5cd4SAxel Rasmussen 107b8da5cd4SAxel RasmussenThere are three basic ways to resolve userfaults: 108b8da5cd4SAxel Rasmussen 109b8da5cd4SAxel Rasmussen- ``UFFDIO_COPY`` atomically copies some existing page contents from 110b8da5cd4SAxel Rasmussen userspace. 111b8da5cd4SAxel Rasmussen 112b8da5cd4SAxel Rasmussen- ``UFFDIO_ZEROPAGE`` atomically zeros the new page. 113b8da5cd4SAxel Rasmussen 114b8da5cd4SAxel Rasmussen- ``UFFDIO_CONTINUE`` maps an existing, previously-populated page. 115b8da5cd4SAxel Rasmussen 116b8da5cd4SAxel RasmussenThese operations are atomic in the sense that they guarantee nothing can 117b8da5cd4SAxel Rasmussensee a half-populated page, since readers will keep userfaulting until the 118b8da5cd4SAxel Rasmussenoperation has finished. 119b8da5cd4SAxel Rasmussen 120b8da5cd4SAxel RasmussenBy default, these wake up userfaults blocked on the range in question. 121b8da5cd4SAxel RasmussenThey support a ``UFFDIO_*_MODE_DONTWAKE`` ``mode`` flag, which indicates 122b8da5cd4SAxel Rasmussenthat waking will be done separately at some later time. 123b8da5cd4SAxel Rasmussen 124b8da5cd4SAxel RasmussenWhich ioctl to choose depends on the kind of page fault, and what we'd 125b8da5cd4SAxel Rasmussenlike to do to resolve it: 126b8da5cd4SAxel Rasmussen 127b8da5cd4SAxel Rasmussen- For ``UFFDIO_REGISTER_MODE_MISSING`` faults, the fault needs to be 128b8da5cd4SAxel Rasmussen resolved by either providing a new page (``UFFDIO_COPY``), or mapping 129b8da5cd4SAxel Rasmussen the zero page (``UFFDIO_ZEROPAGE``). By default, the kernel would map 130b8da5cd4SAxel Rasmussen the zero page for a missing fault. With userfaultfd, userspace can 131b8da5cd4SAxel Rasmussen decide what content to provide before the faulting thread continues. 132b8da5cd4SAxel Rasmussen 133b8da5cd4SAxel Rasmussen- For ``UFFDIO_REGISTER_MODE_MINOR`` faults, there is an existing page (in 134b8da5cd4SAxel Rasmussen the page cache). Userspace has the option of modifying the page's 135b8da5cd4SAxel Rasmussen contents before resolving the fault. Once the contents are correct 136b8da5cd4SAxel Rasmussen (modified or not), userspace asks the kernel to map the page and let the 137b8da5cd4SAxel Rasmussen faulting thread continue with ``UFFDIO_CONTINUE``. 13825edd8bfSAndrea Arcangeli 13957e5d4f2SMartin CracauerNotes: 14057e5d4f2SMartin Cracauer 141b8da5cd4SAxel Rasmussen- You can tell which kind of fault occurred by examining 142b8da5cd4SAxel Rasmussen ``pagefault.flags`` within the ``uffd_msg``, checking for the 143b8da5cd4SAxel Rasmussen ``UFFD_PAGEFAULT_FLAG_*`` flags. 14457e5d4f2SMartin Cracauer 14557e5d4f2SMartin Cracauer- None of the page-delivering ioctls default to the range that you 14657e5d4f2SMartin Cracauer registered with. You must fill in all fields for the appropriate 14757e5d4f2SMartin Cracauer ioctl struct including the range. 14857e5d4f2SMartin Cracauer 14957e5d4f2SMartin Cracauer- You get the address of the access that triggered the missing page 15057e5d4f2SMartin Cracauer event out of a struct uffd_msg that you read in the thread from the 151b8da5cd4SAxel Rasmussen uffd. You can supply as many pages as you want with these IOCTLs. 152b8da5cd4SAxel Rasmussen Keep in mind that unless you used DONTWAKE then the first of any of 153b8da5cd4SAxel Rasmussen those IOCTLs wakes up the faulting thread. 15457e5d4f2SMartin Cracauer 15514a7e51fSMauro Carvalho Chehab- Be sure to test for all errors including 15614a7e51fSMauro Carvalho Chehab (``pollfd[0].revents & POLLERR``). This can happen, e.g. when ranges 15714a7e51fSMauro Carvalho Chehab supplied were incorrect. 15857e5d4f2SMartin Cracauer 15957e5d4f2SMartin CracauerWrite Protect Notifications 16057e5d4f2SMartin Cracauer--------------------------- 16157e5d4f2SMartin Cracauer 16257e5d4f2SMartin CracauerThis is equivalent to (but faster than) using mprotect and a SIGSEGV 16357e5d4f2SMartin Cracauersignal handler. 16457e5d4f2SMartin Cracauer 16514a7e51fSMauro Carvalho ChehabFirstly you need to register a range with ``UFFDIO_REGISTER_MODE_WP``. 16614a7e51fSMauro Carvalho ChehabInstead of using mprotect(2) you use 16714a7e51fSMauro Carvalho Chehab``ioctl(uffd, UFFDIO_WRITEPROTECT, struct *uffdio_writeprotect)`` 16814a7e51fSMauro Carvalho Chehabwhile ``mode = UFFDIO_WRITEPROTECT_MODE_WP`` 16957e5d4f2SMartin Cracauerin the struct passed in. The range does not default to and does not 17057e5d4f2SMartin Cracauerhave to be identical to the range you registered with. You can write 17157e5d4f2SMartin Cracauerprotect as many ranges as you like (inside the registered range). 17257e5d4f2SMartin CracauerThen, in the thread reading from uffd the struct will have 17314a7e51fSMauro Carvalho Chehab``msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WP`` set. Now you send 17414a7e51fSMauro Carvalho Chehab``ioctl(uffd, UFFDIO_WRITEPROTECT, struct *uffdio_writeprotect)`` 17514a7e51fSMauro Carvalho Chehabagain while ``pagefault.mode`` does not have ``UFFDIO_WRITEPROTECT_MODE_WP`` 17614a7e51fSMauro Carvalho Chehabset. This wakes up the thread which will continue to run with writes. This 17757e5d4f2SMartin Cracauerallows you to do the bookkeeping about the write in the uffd reading 17857e5d4f2SMartin Cracauerthread before the ioctl. 17957e5d4f2SMartin Cracauer 18014a7e51fSMauro Carvalho ChehabIf you registered with both ``UFFDIO_REGISTER_MODE_MISSING`` and 18114a7e51fSMauro Carvalho Chehab``UFFDIO_REGISTER_MODE_WP`` then you need to think about the sequence in 18257e5d4f2SMartin Cracauerwhich you supply a page and undo write protect. Note that there is a 18357e5d4f2SMartin Cracauerdifference between writes into a WP area and into a !WP area. The 18414a7e51fSMauro Carvalho Chehabformer will have ``UFFD_PAGEFAULT_FLAG_WP`` set, the latter 18514a7e51fSMauro Carvalho Chehab``UFFD_PAGEFAULT_FLAG_WRITE``. The latter did not fail on protection but 18614a7e51fSMauro Carvalho Chehabyou still need to supply a page when ``UFFDIO_REGISTER_MODE_MISSING`` was 18757e5d4f2SMartin Cracauerused. 18857e5d4f2SMartin Cracauer 189f9451df2SMike RapoportQEMU/KVM 190f9451df2SMike Rapoport======== 19125edd8bfSAndrea Arcangeli 19214a7e51fSMauro Carvalho ChehabQEMU/KVM is using the ``userfaultfd`` syscall to implement postcopy live 19325edd8bfSAndrea Arcangelimigration. Postcopy live migration is one form of memory 19425edd8bfSAndrea Arcangeliexternalization consisting of a virtual machine running with part or 19525edd8bfSAndrea Arcangeliall of its memory residing on a different node in the cloud. The 19614a7e51fSMauro Carvalho Chehab``userfaultfd`` abstraction is generic enough that not a single line of 19725edd8bfSAndrea ArcangeliKVM kernel code had to be modified in order to add postcopy live 19825edd8bfSAndrea Arcangelimigration to QEMU. 19925edd8bfSAndrea Arcangeli 20014a7e51fSMauro Carvalho ChehabGuest async page faults, ``FOLL_NOWAIT`` and all other ``GUP*`` features work 20125edd8bfSAndrea Arcangelijust fine in combination with userfaults. Userfaults trigger async 20225edd8bfSAndrea Arcangelipage faults in the guest scheduler so those guest processes that 20325edd8bfSAndrea Arcangeliaren't waiting for userfaults (i.e. network bound) can keep running in 20425edd8bfSAndrea Arcangelithe guest vcpus. 20525edd8bfSAndrea Arcangeli 20625edd8bfSAndrea ArcangeliIt is generally beneficial to run one pass of precopy live migration 20725edd8bfSAndrea Arcangelijust before starting postcopy live migration, in order to avoid 20825edd8bfSAndrea Arcangeligenerating userfaults for readonly guest regions. 20925edd8bfSAndrea Arcangeli 21025edd8bfSAndrea ArcangeliThe implementation of postcopy live migration currently uses one 21125edd8bfSAndrea Arcangelisingle bidirectional socket but in the future two different sockets 21225edd8bfSAndrea Arcangeliwill be used (to reduce the latency of the userfaults to the minimum 21314a7e51fSMauro Carvalho Chehabpossible without having to decrease ``/proc/sys/net/ipv4/tcp_wmem``). 21425edd8bfSAndrea Arcangeli 21525edd8bfSAndrea ArcangeliThe QEMU in the source node writes all pages that it knows are missing 21625edd8bfSAndrea Arcangeliin the destination node, into the socket, and the migration thread of 21714a7e51fSMauro Carvalho Chehabthe QEMU running in the destination node runs ``UFFDIO_COPY|ZEROPAGE`` 21814a7e51fSMauro Carvalho Chehabioctls on the ``userfaultfd`` in order to map the received pages into the 21914a7e51fSMauro Carvalho Chehabguest (``UFFDIO_ZEROCOPY`` is used if the source page was a zero page). 22025edd8bfSAndrea Arcangeli 22125edd8bfSAndrea ArcangeliA different postcopy thread in the destination node listens with 22214a7e51fSMauro Carvalho Chehabpoll() to the ``userfaultfd`` in parallel. When a ``POLLIN`` event is 22325edd8bfSAndrea Arcangeligenerated after a userfault triggers, the postcopy thread read() from 22414a7e51fSMauro Carvalho Chehabthe ``userfaultfd`` and receives the fault address (or ``-EAGAIN`` in case the 22514a7e51fSMauro Carvalho Chehabuserfault was already resolved and waken by a ``UFFDIO_COPY|ZEROPAGE`` run 22625edd8bfSAndrea Arcangeliby the parallel QEMU migration thread). 22725edd8bfSAndrea Arcangeli 22825edd8bfSAndrea ArcangeliAfter the QEMU postcopy thread (running in the destination node) gets 22925edd8bfSAndrea Arcangelithe userfault address it writes the information about the missing page 23025edd8bfSAndrea Arcangeliinto the socket. The QEMU source node receives the information and 23125edd8bfSAndrea Arcangeliroughly "seeks" to that page address and continues sending all 23225edd8bfSAndrea Arcangeliremaining missing pages from that new page offset. Soon after that 23325edd8bfSAndrea Arcangeli(just the time to flush the tcp_wmem queue through the network) the 23425edd8bfSAndrea Arcangelimigration thread in the QEMU running in the destination node will 23525edd8bfSAndrea Arcangelireceive the page that triggered the userfault and it'll map it as 23614a7e51fSMauro Carvalho Chehabusual with the ``UFFDIO_COPY|ZEROPAGE`` (without actually knowing if it 23725edd8bfSAndrea Arcangeliwas spontaneously sent by the source or if it was an urgent page 2389332ef9dSMasahiro Yamadarequested through a userfault). 23925edd8bfSAndrea Arcangeli 24025edd8bfSAndrea ArcangeliBy the time the userfaults start, the QEMU in the destination node 24125edd8bfSAndrea Arcangelidoesn't need to keep any per-page state bitmap relative to the live 24225edd8bfSAndrea Arcangelimigration around and a single per-page bitmap has to be maintained in 24325edd8bfSAndrea Arcangelithe QEMU running in the source node to know which pages are still 24425edd8bfSAndrea Arcangelimissing in the destination node. The bitmap in the source node is 24525edd8bfSAndrea Arcangelichecked to find which missing pages to send in round robin and we seek 24625edd8bfSAndrea Arcangeliover it when receiving incoming userfaults. After sending each page of 24725edd8bfSAndrea Arcangelicourse the bitmap is updated accordingly. It's also useful to avoid 24825edd8bfSAndrea Arcangelisending the same page twice (in case the userfault is read by the 24914a7e51fSMauro Carvalho Chehabpostcopy thread just before ``UFFDIO_COPY|ZEROPAGE`` runs in the migration 25025edd8bfSAndrea Arcangelithread). 2515a02026dSMike Rapoport 252f9451df2SMike RapoportNon-cooperative userfaultfd 253f9451df2SMike Rapoport=========================== 2545a02026dSMike Rapoport 25514a7e51fSMauro Carvalho ChehabWhen the ``userfaultfd`` is monitored by an external manager, the manager 2565a02026dSMike Rapoportmust be able to track changes in the process virtual memory 2575a02026dSMike Rapoportlayout. Userfaultfd can notify the manager about such changes using 2585a02026dSMike Rapoportthe same read(2) protocol as for the page fault notifications. The 2595a02026dSMike Rapoportmanager has to explicitly enable these events by setting appropriate 26014a7e51fSMauro Carvalho Chehabbits in ``uffdio_api.features`` passed to ``UFFDIO_API`` ioctl: 2615a02026dSMike Rapoport 26214a7e51fSMauro Carvalho Chehab``UFFD_FEATURE_EVENT_FORK`` 26314a7e51fSMauro Carvalho Chehab enable ``userfaultfd`` hooks for fork(). When this feature is 26414a7e51fSMauro Carvalho Chehab enabled, the ``userfaultfd`` context of the parent process is 265f9451df2SMike Rapoport duplicated into the newly created process. The manager 26614a7e51fSMauro Carvalho Chehab receives ``UFFD_EVENT_FORK`` with file descriptor of the new 26714a7e51fSMauro Carvalho Chehab ``userfaultfd`` context in the ``uffd_msg.fork``. 2685a02026dSMike Rapoport 26914a7e51fSMauro Carvalho Chehab``UFFD_FEATURE_EVENT_REMAP`` 270f9451df2SMike Rapoport enable notifications about mremap() calls. When the 271f9451df2SMike Rapoport non-cooperative process moves a virtual memory area to a 272f9451df2SMike Rapoport different location, the manager will receive 27314a7e51fSMauro Carvalho Chehab ``UFFD_EVENT_REMAP``. The ``uffd_msg.remap`` will contain the old and 274f9451df2SMike Rapoport new addresses of the area and its original length. 2755a02026dSMike Rapoport 27614a7e51fSMauro Carvalho Chehab``UFFD_FEATURE_EVENT_REMOVE`` 277f9451df2SMike Rapoport enable notifications about madvise(MADV_REMOVE) and 27814a7e51fSMauro Carvalho Chehab madvise(MADV_DONTNEED) calls. The event ``UFFD_EVENT_REMOVE`` will 27914a7e51fSMauro Carvalho Chehab be generated upon these calls to madvise(). The ``uffd_msg.remove`` 280f9451df2SMike Rapoport will contain start and end addresses of the removed area. 2815a02026dSMike Rapoport 28214a7e51fSMauro Carvalho Chehab``UFFD_FEATURE_EVENT_UNMAP`` 283f9451df2SMike Rapoport enable notifications about memory unmapping. The manager will 28414a7e51fSMauro Carvalho Chehab get ``UFFD_EVENT_UNMAP`` with ``uffd_msg.remove`` containing start and 285f9451df2SMike Rapoport end addresses of the unmapped area. 2865a02026dSMike Rapoport 28714a7e51fSMauro Carvalho ChehabAlthough the ``UFFD_FEATURE_EVENT_REMOVE`` and ``UFFD_FEATURE_EVENT_UNMAP`` 2885a02026dSMike Rapoportare pretty similar, they quite differ in the action expected from the 28914a7e51fSMauro Carvalho Chehab``userfaultfd`` manager. In the former case, the virtual memory is 2905a02026dSMike Rapoportremoved, but the area is not, the area remains monitored by the 29114a7e51fSMauro Carvalho Chehab``userfaultfd``, and if a page fault occurs in that area it will be 2925a02026dSMike Rapoportdelivered to the manager. The proper resolution for such page fault is 2935a02026dSMike Rapoportto zeromap the faulting address. However, in the latter case, when an 2945a02026dSMike Rapoportarea is unmapped, either explicitly (with munmap() system call), or 2955a02026dSMike Rapoportimplicitly (e.g. during mremap()), the area is removed and in turn the 29614a7e51fSMauro Carvalho Chehab``userfaultfd`` context for such area disappears too and the manager will 2975a02026dSMike Rapoportnot get further userland page faults from the removed area. Still, the 2985a02026dSMike Rapoportnotification is required in order to prevent manager from using 29914a7e51fSMauro Carvalho Chehab``UFFDIO_COPY`` on the unmapped area. 3005a02026dSMike Rapoport 3015a02026dSMike RapoportUnlike userland page faults which have to be synchronous and require 3025a02026dSMike Rapoportexplicit or implicit wakeup, all the events are delivered 3035a02026dSMike Rapoportasynchronously and the non-cooperative process resumes execution as 30414a7e51fSMauro Carvalho Chehabsoon as manager executes read(). The ``userfaultfd`` manager should 30514a7e51fSMauro Carvalho Chehabcarefully synchronize calls to ``UFFDIO_COPY`` with the events 30614a7e51fSMauro Carvalho Chehabprocessing. To aid the synchronization, the ``UFFDIO_COPY`` ioctl will 30714a7e51fSMauro Carvalho Chehabreturn ``-ENOSPC`` when the monitored process exits at the time of 30814a7e51fSMauro Carvalho Chehab``UFFDIO_COPY``, and ``-ENOENT``, when the non-cooperative process has changed 30914a7e51fSMauro Carvalho Chehabits virtual memory layout simultaneously with outstanding ``UFFDIO_COPY`` 3105a02026dSMike Rapoportoperation. 3115a02026dSMike Rapoport 3125a02026dSMike RapoportThe current asynchronous model of the event delivery is optimal for 31314a7e51fSMauro Carvalho Chehabsingle threaded non-cooperative ``userfaultfd`` manager implementations. A 3145a02026dSMike Rapoportsynchronous event delivery model can be added later as a new 31514a7e51fSMauro Carvalho Chehab``userfaultfd`` feature to facilitate multithreading enhancements of the 31614a7e51fSMauro Carvalho Chehabnon cooperative manager, for example to allow ``UFFDIO_COPY`` ioctls to 3175a02026dSMike Rapoportrun in parallel to the event reception. Single threaded 3185a02026dSMike Rapoportimplementations should continue to use the current async event 3195a02026dSMike Rapoportdelivery model instead. 320