\sphinx.addnodesdocument)}( rawsourcechildren]( translations LanguagesNode)}(hhh](h pending_xref)}(hhh]docutils.nodesTextChinese (Simplified)}parenthsba attributes}(ids]classes]names]dupnames]backrefs] refdomainstdreftypedoc reftarget./translations/zh_CN/admin-guide/mm/userfaultfdmodnameN classnameN refexplicitutagnamehhh ubh)}(hhh]hChinese (Traditional)}hh2sbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget./translations/zh_TW/admin-guide/mm/userfaultfdmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hItalian}hhFsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget./translations/it_IT/admin-guide/mm/userfaultfdmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hJapanese}hhZsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget./translations/ja_JP/admin-guide/mm/userfaultfdmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hKorean}hhnsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget./translations/ko_KR/admin-guide/mm/userfaultfdmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hPortuguese (Brazilian)}hhsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget./translations/pt_BR/admin-guide/mm/userfaultfdmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hSpanish}hhsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget./translations/sp_SP/admin-guide/mm/userfaultfdmodnameN classnameN refexplicituh1hhh ubeh}(h]h ]h"]h$]h&]current_languageEnglishuh1h hh _documenthsourceNlineNubhsection)}(hhh](htitle)}(h Userfaultfdh]h Userfaultfd}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhhH/var/lib/git/docbuild/linux/Documentation/admin-guide/mm/userfaultfd.rsthKubh)}(hhh](h)}(h Objectiveh]h Objective}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhhhhKubh paragraph)}(hUserfaults allow the implementation of on-demand paging from userland and more generally they allow userland to take control of various memory page faults, something otherwise only the kernel code could do.h]hUserfaults allow the implementation of on-demand paging from userland and more generally they allow userland to take control of various memory page faults, something otherwise only the kernel code could do.}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhhhhubh)}(hjFor example userfaults allows a proper and more optimal implementation of the ``PROT_NONE+SIGSEGV`` trick.h](hNFor example userfaults allows a proper and more optimal implementation of the }(hhhhhNhNubhliteral)}(h``PROT_NONE+SIGSEGV``h]hPROT_NONE+SIGSEGV}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhubh trick.}(hhhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK hhhhubeh}(h] objectiveah ]h"] objectiveah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(hDesignh]hDesign}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubh)}(hXUserspace creates a new userfaultfd, initializes it, and registers one or more regions of virtual memory with it. Then, any page faults which occur within the region(s) result in a message being delivered to the userfaultfd, notifying userspace of the fault.h]hXUserspace creates a new userfaultfd, initializes it, and registers one or more regions of virtual memory with it. Then, any page faults which occur within the region(s) result in a message being delivered to the userfaultfd, notifying userspace of the fault.}(hj'hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hzThe ``userfaultfd`` (aside from registering and unregistering virtual memory ranges) provides two primary functionalities:h](hThe }(hj5hhhNhNubh)}(h``userfaultfd``h]h userfaultfd}(hj=hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj5ubhg (aside from registering and unregistering virtual memory ranges) provides two primary functionalities:}(hj5hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubhenumerated_list)}(hhh](h list_item)}(hM``read/POLLIN`` protocol to notify a userland thread of the faults happening h]h)}(hL``read/POLLIN`` protocol to notify a userland thread of the faults happeningh](h)}(h``read/POLLIN``h]h read/POLLIN}(hjdhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj`ubh= protocol to notify a userland thread of the faults happening}(hj`hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj\ubah}(h]h ]h"]h$]h&]uh1jZhjWhhhhhNubj[)}(hvarious ``UFFDIO_*`` ioctls that can manage the virtual memory regions registered in the ``userfaultfd`` that allows userland to efficiently resolve the userfaults it receives via 1) or to manage the virtual memory in the background h]h)}(hvarious ``UFFDIO_*`` ioctls that can manage the virtual memory regions registered in the ``userfaultfd`` that allows userland to efficiently resolve the userfaults it receives via 1) or to manage the virtual memory in the backgroundh](hvarious }(hjhhhNhNubh)}(h ``UFFDIO_*``h]hUFFDIO_*}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhE ioctls that can manage the virtual memory regions registered in the }(hjhhhNhNubh)}(h``userfaultfd``h]h userfaultfd}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh that allows userland to efficiently resolve the userfaults it receives via 1) or to manage the virtual memory in the background}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jZhjWhhhhhNubeh}(h]h ]h"]h$]h&]enumtypearabicprefixhsuffix)uh1jUhjhhhhhKubh)}(hXThe real advantage of userfaults if compared to regular virtual memory management of mremap/mprotect is that the userfaults in all their operations never involve heavyweight structures like vmas (in fact the ``userfaultfd`` runtime load never takes the mmap_lock for writing). Vmas are not suitable for page- (or hugepage) granular fault tracking when dealing with virtual address spaces that could span Terabytes. Too many vmas would be needed for that.h](hThe real advantage of userfaults if compared to regular virtual memory management of mremap/mprotect is that the userfaults in all their operations never involve heavyweight structures like vmas (in fact the }(hjhhhNhNubh)}(h``userfaultfd``h]h userfaultfd}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh runtime load never takes the mmap_lock for writing). Vmas are not suitable for page- (or hugepage) granular fault tracking when dealing with virtual address spaces that could span Terabytes. Too many vmas would be needed for that.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK"hjhhubh)}(hXThe ``userfaultfd``, once created, can also be passed using unix domain sockets to a manager process, so the same manager process could handle the userfaults of a multitude of different processes without them being aware about what is going on (well of course unless they later try to use the ``userfaultfd`` themselves on the same region the manager is already tracking, which is a corner case that would currently return ``-EBUSY``).h](hThe }(hjhhhNhNubh)}(h``userfaultfd``h]h userfaultfd}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhX, once created, can also be passed using unix domain sockets to a manager process, so the same manager process could handle the userfaults of a multitude of different processes without them being aware about what is going on (well of course unless they later try to use the }(hjhhhNhNubh)}(h``userfaultfd``h]h userfaultfd}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhs themselves on the same region the manager is already tracking, which is a corner case that would currently return }(hjhhhNhNubh)}(h ``-EBUSY``h]h-EBUSY}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh).}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK*hjhhubeh}(h]designah ]h"]designah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(hAPIh]hAPI}(hj8hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj5hhhhhK3ubh)}(hhh](h)}(hCreating a userfaultfdh]hCreating a userfaultfd}(hjIhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjFhhhhhK6ubh)}(hThere are two ways to create a new userfaultfd, each of which provide ways to restrict access to this functionality (since historically userfaultfds which handle kernel page faults have been a useful tool for exploiting the kernel).h]hThere are two ways to create a new userfaultfd, each of which provide ways to restrict access to this functionality (since historically userfaultfds which handle kernel page faults have been a useful tool for exploiting the kernel).}(hjWhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK8hjFhhubh)}(hThe first way, supported since userfaultfd was introduced, is the userfaultfd(2) syscall. Access to this is controlled in several ways:h]hThe first way, supported since userfaultfd was introduced, is the userfaultfd(2) syscall. Access to this is controlled in several ways:}(hjehhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK atomically copies some existing page contents from userspace.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jZhjhhhhhNubj[)}(h3``UFFDIO_ZEROPAGE`` atomically zeros the new page. h]h)}(h2``UFFDIO_ZEROPAGE`` atomically zeros the new page.h](h)}(h``UFFDIO_ZEROPAGE``h]hUFFDIO_ZEROPAGE}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh atomically zeros the new page.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jZhjhhhhhNubj[)}(hA``UFFDIO_CONTINUE`` maps an existing, previously-populated page. h]h)}(h@``UFFDIO_CONTINUE`` maps an existing, previously-populated page.h](h)}(h``UFFDIO_CONTINUE``h]hUFFDIO_CONTINUE}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh- maps an existing, previously-populated page.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jZhjhhhhhNubeh}(h]h ]h"]h$]h&]jjuh1jshhhKhjhhubh)}(hThese operations are atomic in the sense that they guarantee nothing can see a half-populated page, since readers will keep userfaulting until the operation has finished.h]hThese operations are atomic in the sense that they guarantee nothing can see a half-populated page, since readers will keep userfaulting until the operation has finished.}(hj-hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hBy default, these wake up userfaults blocked on the range in question. They support a ``UFFDIO_*_MODE_DONTWAKE`` ``mode`` flag, which indicates that waking will be done separately at some later time.h](hVBy default, these wake up userfaults blocked on the range in question. They support a }(hj;hhhNhNubh)}(h``UFFDIO_*_MODE_DONTWAKE``h]hUFFDIO_*_MODE_DONTWAKE}(hjChhhNhNubah}(h]h ]h"]h$]h&]uh1hhj;ubh }(hj;hhhNhNubh)}(h``mode``h]hmode}(hjUhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj;ubhN flag, which indicates that waking will be done separately at some later time.}(hj;hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(h`Which ioctl to choose depends on the kind of page fault, and what we'd like to do to resolve it:h]hbWhich ioctl to choose depends on the kind of page fault, and what we’d like to do to resolve it:}(hjmhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubjt)}(hhh](j[)}(hXWFor ``UFFDIO_REGISTER_MODE_MISSING`` faults, the fault needs to be resolved by either providing a new page (``UFFDIO_COPY``), or mapping the zero page (``UFFDIO_ZEROPAGE``). By default, the kernel would map the zero page for a missing fault. With userfaultfd, userspace can decide what content to provide before the faulting thread continues. h]h)}(hXVFor ``UFFDIO_REGISTER_MODE_MISSING`` faults, the fault needs to be resolved by either providing a new page (``UFFDIO_COPY``), or mapping the zero page (``UFFDIO_ZEROPAGE``). By default, the kernel would map the zero page for a missing fault. With userfaultfd, userspace can decide what content to provide before the faulting thread continues.h](hFor }(hjhhhNhNubh)}(h ``UFFDIO_REGISTER_MODE_MISSING``h]hUFFDIO_REGISTER_MODE_MISSING}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhH faults, the fault needs to be resolved by either providing a new page (}(hjhhhNhNubh)}(h``UFFDIO_COPY``h]h UFFDIO_COPY}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh), or mapping the zero page (}(hjhhhNhNubh)}(h``UFFDIO_ZEROPAGE``h]hUFFDIO_ZEROPAGE}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh). By default, the kernel would map the zero page for a missing fault. With userfaultfd, userspace can decide what content to provide before the faulting thread continues.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj~ubah}(h]h ]h"]h$]h&]uh1jZhj{hhhhhNubj[)}(hXJFor ``UFFDIO_REGISTER_MODE_MINOR`` faults, there is an existing page (in the page cache). Userspace has the option of modifying the page's contents before resolving the fault. Once the contents are correct (modified or not), userspace asks the kernel to map the page and let the faulting thread continue with ``UFFDIO_CONTINUE``. h]h)}(hXIFor ``UFFDIO_REGISTER_MODE_MINOR`` faults, there is an existing page (in the page cache). Userspace has the option of modifying the page's contents before resolving the fault. Once the contents are correct (modified or not), userspace asks the kernel to map the page and let the faulting thread continue with ``UFFDIO_CONTINUE``.h](hFor }(hjhhhNhNubh)}(h``UFFDIO_REGISTER_MODE_MINOR``h]hUFFDIO_REGISTER_MODE_MINOR}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhX faults, there is an existing page (in the page cache). Userspace has the option of modifying the page’s contents before resolving the fault. Once the contents are correct (modified or not), userspace asks the kernel to map the page and let the faulting thread continue with }(hjhhhNhNubh)}(h``UFFDIO_CONTINUE``h]hUFFDIO_CONTINUE}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jZhj{hhhhhNubeh}(h]h ]h"]h$]h&]jjuh1jshhhKhjhhubh)}(hNotes:h]hNotes:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubjt)}(hhh](j[)}(hYou can tell which kind of fault occurred by examining ``pagefault.flags`` within the ``uffd_msg``, checking for the ``UFFD_PAGEFAULT_FLAG_*`` flags. h]h)}(hYou can tell which kind of fault occurred by examining ``pagefault.flags`` within the ``uffd_msg``, checking for the ``UFFD_PAGEFAULT_FLAG_*`` flags.h](h7You can tell which kind of fault occurred by examining }(hj#hhhNhNubh)}(h``pagefault.flags``h]hpagefault.flags}(hj+hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj#ubh within the }(hj#hhhNhNubh)}(h ``uffd_msg``h]huffd_msg}(hj=hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj#ubh, checking for the }(hj#hhhNhNubh)}(h``UFFD_PAGEFAULT_FLAG_*``h]hUFFD_PAGEFAULT_FLAG_*}(hjOhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj#ubh flags.}(hj#hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jZhjhhhhhNubj[)}(hNone of the page-delivering ioctls default to the range that you registered with. You must fill in all fields for the appropriate ioctl struct including the range. h]h)}(hNone of the page-delivering ioctls default to the range that you registered with. You must fill in all fields for the appropriate ioctl struct including the range.h]hNone of the page-delivering ioctls default to the range that you registered with. You must fill in all fields for the appropriate ioctl struct including the range.}(hjqhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjmubah}(h]h ]h"]h$]h&]uh1jZhjhhhhhNubj[)}(hX8You get the address of the access that triggered the missing page event out of a struct uffd_msg that you read in the thread from the uffd. You can supply as many pages as you want with these IOCTLs. Keep in mind that unless you used DONTWAKE then the first of any of those IOCTLs wakes up the faulting thread. h]h)}(hX7You get the address of the access that triggered the missing page event out of a struct uffd_msg that you read in the thread from the uffd. You can supply as many pages as you want with these IOCTLs. Keep in mind that unless you used DONTWAKE then the first of any of those IOCTLs wakes up the faulting thread.h]hX7You get the address of the access that triggered the missing page event out of a struct uffd_msg that you read in the thread from the uffd. You can supply as many pages as you want with these IOCTLs. Keep in mind that unless you used DONTWAKE then the first of any of those IOCTLs wakes up the faulting thread.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jZhjhhhhhNubj[)}(hBe sure to test for all errors including (``pollfd[0].revents & POLLERR``). This can happen, e.g. when ranges supplied were incorrect. h]h)}(hBe sure to test for all errors including (``pollfd[0].revents & POLLERR``). This can happen, e.g. when ranges supplied were incorrect.h](h*Be sure to test for all errors including (}(hjhhhNhNubh)}(h``pollfd[0].revents & POLLERR``h]hpollfd[0].revents & POLLERR}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh>). This can happen, e.g. when ranges supplied were incorrect.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jZhjhhhhhNubeh}(h]h ]h"]h$]h&]jjuh1jshhhKhjhhubeh}(h]resolving-userfaultsah ]h"]resolving userfaultsah$]h&]uh1hhj5hhhhhKubh)}(hhh](h)}(hWrite Protect Notificationsh]hWrite Protect Notifications}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubh)}(hTThis is equivalent to (but faster than) using mprotect and a SIGSEGV signal handler.h]hTThis is equivalent to (but faster than) using mprotect and a SIGSEGV signal handler.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hXaFirstly you need to register a range with ``UFFDIO_REGISTER_MODE_WP``. Instead of using mprotect(2) you use ``ioctl(uffd, UFFDIO_WRITEPROTECT, struct *uffdio_writeprotect)`` while ``mode = UFFDIO_WRITEPROTECT_MODE_WP`` in the struct passed in. The range does not default to and does not have to be identical to the range you registered with. You can write protect as many ranges as you like (inside the registered range). Then, in the thread reading from uffd the struct will have ``msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WP`` set. Now you send ``ioctl(uffd, UFFDIO_WRITEPROTECT, struct *uffdio_writeprotect)`` again while ``pagefault.mode`` does not have ``UFFDIO_WRITEPROTECT_MODE_WP`` set. This wakes up the thread which will continue to run with writes. This allows you to do the bookkeeping about the write in the uffd reading thread before the ioctl.h](h*Firstly you need to register a range with }(hjhhhNhNubh)}(h``UFFDIO_REGISTER_MODE_WP``h]hUFFDIO_REGISTER_MODE_WP}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh'. Instead of using mprotect(2) you use }(hjhhhNhNubh)}(hA``ioctl(uffd, UFFDIO_WRITEPROTECT, struct *uffdio_writeprotect)``h]h=ioctl(uffd, UFFDIO_WRITEPROTECT, struct *uffdio_writeprotect)}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh while }(hjhhhNhNubh)}(h&``mode = UFFDIO_WRITEPROTECT_MODE_WP``h]h"mode = UFFDIO_WRITEPROTECT_MODE_WP}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhX  in the struct passed in. The range does not default to and does not have to be identical to the range you registered with. You can write protect as many ranges as you like (inside the registered range). Then, in the thread reading from uffd the struct will have }(hjhhhNhNubh)}(h4``msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WP``h]h0msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WP}(hj2 hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh set. Now you send }(hjhhhNhNubh)}(hA``ioctl(uffd, UFFDIO_WRITEPROTECT, struct *uffdio_writeprotect)``h]h=ioctl(uffd, UFFDIO_WRITEPROTECT, struct *uffdio_writeprotect)}(hjD hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh again while }(hjhhhNhNubh)}(h``pagefault.mode``h]hpagefault.mode}(hjV hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh does not have }(hjhhhNhNubh)}(h``UFFDIO_WRITEPROTECT_MODE_WP``h]hUFFDIO_WRITEPROTECT_MODE_WP}(hjh hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh set. This wakes up the thread which will continue to run with writes. This allows you to do the bookkeeping about the write in the uffd reading thread before the ioctl.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hXIf you registered with both ``UFFDIO_REGISTER_MODE_MISSING`` and ``UFFDIO_REGISTER_MODE_WP`` then you need to think about the sequence in which you supply a page and undo write protect. Note that there is a difference between writes into a WP area and into a !WP area. The former will have ``UFFD_PAGEFAULT_FLAG_WP`` set, the latter ``UFFD_PAGEFAULT_FLAG_WRITE``. The latter did not fail on protection but you still need to supply a page when ``UFFDIO_REGISTER_MODE_MISSING`` was used.h](hIf you registered with both }(hj hhhNhNubh)}(h ``UFFDIO_REGISTER_MODE_MISSING``h]hUFFDIO_REGISTER_MODE_MISSING}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh and }(hj hhhNhNubh)}(h``UFFDIO_REGISTER_MODE_WP``h]hUFFDIO_REGISTER_MODE_WP}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh then you need to think about the sequence in which you supply a page and undo write protect. Note that there is a difference between writes into a WP area and into a !WP area. The former will have }(hj hhhNhNubh)}(h``UFFD_PAGEFAULT_FLAG_WP``h]hUFFD_PAGEFAULT_FLAG_WP}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh set, the latter }(hj hhhNhNubh)}(h``UFFD_PAGEFAULT_FLAG_WRITE``h]hUFFD_PAGEFAULT_FLAG_WRITE}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubhR. The latter did not fail on protection but you still need to supply a page when }(hj hhhNhNubh)}(h ``UFFDIO_REGISTER_MODE_MISSING``h]hUFFDIO_REGISTER_MODE_MISSING}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh was used.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hUserfaultfd write-protect mode currently behave differently on none ptes (when e.g. page is missing) over different types of memories.h]hUserfaultfd write-protect mode currently behave differently on none ptes (when e.g. page is missing) over different types of memories.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hXFor anonymous memory, ``ioctl(UFFDIO_WRITEPROTECT)`` will ignore none ptes (e.g. when pages are missing and not populated). For file-backed memories like shmem and hugetlbfs, none ptes will be write protected just like a present pte. In other words, there will be a userfaultfd write fault message generated when writing to a missing page on file typed memories, as long as the page range was write-protected before. Such a message will not be generated on anonymous memories by default.h](hFor anonymous memory, }(hj hhhNhNubh)}(h``ioctl(UFFDIO_WRITEPROTECT)``h]hioctl(UFFDIO_WRITEPROTECT)}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubhX will ignore none ptes (e.g. when pages are missing and not populated). For file-backed memories like shmem and hugetlbfs, none ptes will be write protected just like a present pte. In other words, there will be a userfaultfd write fault message generated when writing to a missing page on file typed memories, as long as the page range was write-protected before. Such a message will not be generated on anonymous memories by default.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hXSIf the application wants to be able to write protect none ptes on anonymous memory, one can pre-populate the memory with e.g. MADV_POPULATE_READ. On newer kernels, one can also detect the feature UFFD_FEATURE_WP_UNPOPULATED and set the feature bit in advance to make sure none ptes will also be write protected even upon anonymous memory.h]hXSIf the application wants to be able to write protect none ptes on anonymous memory, one can pre-populate the memory with e.g. MADV_POPULATE_READ. On newer kernels, one can also detect the feature UFFD_FEATURE_WP_UNPOPULATED and set the feature bit in advance to make sure none ptes will also be write protected even upon anonymous memory.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hXWhen using ``UFFDIO_REGISTER_MODE_WP`` in combination with either ``UFFDIO_REGISTER_MODE_MISSING`` or ``UFFDIO_REGISTER_MODE_MINOR``, when resolving missing / minor faults with ``UFFDIO_COPY`` or ``UFFDIO_CONTINUE`` respectively, it may be desirable for the new page / mapping to be write-protected (so future writes will also result in a WP fault). These ioctls support a mode flag (``UFFDIO_COPY_MODE_WP`` or ``UFFDIO_CONTINUE_MODE_WP`` respectively) to configure the mapping this way.h](h When using }(hj$ hhhNhNubh)}(h``UFFDIO_REGISTER_MODE_WP``h]hUFFDIO_REGISTER_MODE_WP}(hj, hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj$ ubh in combination with either }(hj$ hhhNhNubh)}(h ``UFFDIO_REGISTER_MODE_MISSING``h]hUFFDIO_REGISTER_MODE_MISSING}(hj> hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj$ ubh or }(hj$ hhhNhNubh)}(h``UFFDIO_REGISTER_MODE_MINOR``h]hUFFDIO_REGISTER_MODE_MINOR}(hjP hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj$ ubh-, when resolving missing / minor faults with }(hj$ hhhNhNubh)}(h``UFFDIO_COPY``h]h UFFDIO_COPY}(hjb hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj$ ubh or }hj$ sbh)}(h``UFFDIO_CONTINUE``h]hUFFDIO_CONTINUE}(hjt hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj$ ubh respectively, it may be desirable for the new page / mapping to be write-protected (so future writes will also result in a WP fault). These ioctls support a mode flag (}(hj$ hhhNhNubh)}(h``UFFDIO_COPY_MODE_WP``h]hUFFDIO_COPY_MODE_WP}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj$ ubh or }hj$ sbh)}(h``UFFDIO_CONTINUE_MODE_WP``h]hUFFDIO_CONTINUE_MODE_WP}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj$ ubh1 respectively) to configure the mapping this way.}(hj$ hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hIf the userfaultfd context has ``UFFD_FEATURE_WP_ASYNC`` feature bit set, any vma registered with write-protection will work in async mode rather than the default sync mode.h](hIf the userfaultfd context has }(hj hhhNhNubh)}(h``UFFD_FEATURE_WP_ASYNC``h]hUFFD_FEATURE_WP_ASYNC}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubhu feature bit set, any vma registered with write-protection will work in async mode rather than the default sync mode.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hXIn async mode, there will be no message generated when a write operation happens, meanwhile the write-protection will be resolved automatically by the kernel. It can be seen as a more accurate version of soft-dirty tracking and it can be different in a few ways:h]hXIn async mode, there will be no message generated when a write operation happens, meanwhile the write-protection will be resolved automatically by the kernel. It can be seen as a more accurate version of soft-dirty tracking and it can be different in a few ways:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh block_quote)}(hX- The dirty result will not be affected by vma changes (e.g. vma merging) because the dirty is only tracked by the pte. - It supports range operations by default, so one can enable tracking on any range of memory as long as page aligned. - Dirty information will not get lost if the pte was zapped due to various reasons (e.g. during split of a shmem transparent huge page). - Due to a reverted meaning of soft-dirty (page clean when the uffd bit is set; dirty when the uffd bit is cleared), it has different semantics on some of the memory operations. For example: ``MADV_DONTNEED`` on anonymous (or ``MADV_REMOVE`` on a file mapping) will be treated as dirtying of memory by dropping the uffd bit during the procedure. h]jt)}(hhh](j[)}(hvThe dirty result will not be affected by vma changes (e.g. vma merging) because the dirty is only tracked by the pte. h]h)}(huThe dirty result will not be affected by vma changes (e.g. vma merging) because the dirty is only tracked by the pte.h]huThe dirty result will not be affected by vma changes (e.g. vma merging) because the dirty is only tracked by the pte.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hj ubah}(h]h ]h"]h$]h&]uh1jZhj ubj[)}(htIt supports range operations by default, so one can enable tracking on any range of memory as long as page aligned. h]h)}(hsIt supports range operations by default, so one can enable tracking on any range of memory as long as page aligned.h]hsIt supports range operations by default, so one can enable tracking on any range of memory as long as page aligned.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj ubah}(h]h ]h"]h$]h&]uh1jZhj ubj[)}(hDirty information will not get lost if the pte was zapped due to various reasons (e.g. during split of a shmem transparent huge page). h]h)}(hDirty information will not get lost if the pte was zapped due to various reasons (e.g. during split of a shmem transparent huge page).h]hDirty information will not get lost if the pte was zapped due to various reasons (e.g. during split of a shmem transparent huge page).}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj ubah}(h]h ]h"]h$]h&]uh1jZhj ubj[)}(hXYDue to a reverted meaning of soft-dirty (page clean when the uffd bit is set; dirty when the uffd bit is cleared), it has different semantics on some of the memory operations. For example: ``MADV_DONTNEED`` on anonymous (or ``MADV_REMOVE`` on a file mapping) will be treated as dirtying of memory by dropping the uffd bit during the procedure. h]h)}(hXXDue to a reverted meaning of soft-dirty (page clean when the uffd bit is set; dirty when the uffd bit is cleared), it has different semantics on some of the memory operations. For example: ``MADV_DONTNEED`` on anonymous (or ``MADV_REMOVE`` on a file mapping) will be treated as dirtying of memory by dropping the uffd bit during the procedure.h](hDue to a reverted meaning of soft-dirty (page clean when the uffd bit is set; dirty when the uffd bit is cleared), it has different semantics on some of the memory operations. For example: }(hj3 hhhNhNubh)}(h``MADV_DONTNEED``h]h MADV_DONTNEED}(hj; hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj3 ubh on anonymous (or }(hj3 hhhNhNubh)}(h``MADV_REMOVE``h]h MADV_REMOVE}(hjM hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj3 ubhh on a file mapping) will be treated as dirtying of memory by dropping the uffd bit during the procedure.}(hj3 hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj/ ubah}(h]h ]h"]h$]h&]uh1jZhj ubeh}(h]h ]h"]h$]h&]jjuh1jshhhM hj ubah}(h]h ]h"]h$]h&]uh1j hhhM hjhhubh)}(hThe user app can collect the "written/dirty" status by looking up the uffd bit for the pages being interested in /proc/pagemap.h]hThe user app can collect the “written/dirty” status by looking up the uffd bit for the pages being interested in /proc/pagemap.}(hjw hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hXThe page will not be under track of userfaultfd-wp async mode until the page is explicitly write-protected by ``ioctl(UFFDIO_WRITEPROTECT)`` with the mode flag ``UFFDIO_WRITEPROTECT_MODE_WP`` set. Trying to resolve a page fault that was tracked by async mode userfaultfd-wp is invalid.h](hnThe page will not be under track of userfaultfd-wp async mode until the page is explicitly write-protected by }(hj hhhNhNubh)}(h``ioctl(UFFDIO_WRITEPROTECT)``h]hioctl(UFFDIO_WRITEPROTECT)}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh with the mode flag }(hj hhhNhNubh)}(h``UFFDIO_WRITEPROTECT_MODE_WP``h]hUFFDIO_WRITEPROTECT_MODE_WP}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh_ set. Trying to resolve a page fault that was tracked by async mode userfaultfd-wp is invalid.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hWWhen userfaultfd-wp async mode is used alone, it can be applied to all kinds of memory.h]hWWhen userfaultfd-wp async mode is used alone, it can be applied to all kinds of memory.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM$hjhhubeh}(h]write-protect-notificationsah ]h"]write protect notificationsah$]h&]uh1hhj5hhhhhKubh)}(hhh](h)}(hMemory Poisioning Emulationh]hMemory Poisioning Emulation}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj hhhhhM(ubh)}(hXIn response to a fault (either missing or minor), an action userspace can take to "resolve" it is to issue a ``UFFDIO_POISON``. This will cause any future faulters to either get a SIGBUS, or in KVM's case the guest will receive an MCE as if there were hardware memory poisoning.h](hqIn response to a fault (either missing or minor), an action userspace can take to “resolve” it is to issue a }(hj hhhNhNubh)}(h``UFFDIO_POISON``h]h UFFDIO_POISON}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh. This will cause any future faulters to either get a SIGBUS, or in KVM’s case the guest will receive an MCE as if there were hardware memory poisoning.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM*hj hhubh)}(hXThis is used to emulate hardware memory poisoning. Imagine a VM running on a machine which experiences a real hardware memory error. Later, we live migrate the VM to another physical machine. Since we want the migration to be transparent to the guest, we want that same address range to act as if it was still poisoned, even though it's on a new physical host which ostensibly doesn't have a memory error in the exact same spot.h]hXThis is used to emulate hardware memory poisoning. Imagine a VM running on a machine which experiences a real hardware memory error. Later, we live migrate the VM to another physical machine. Since we want the migration to be transparent to the guest, we want that same address range to act as if it was still poisoned, even though it’s on a new physical host which ostensibly doesn’t have a memory error in the exact same spot.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM/hj hhubeh}(h]memory-poisioning-emulationah ]h"]memory poisioning emulationah$]h&]uh1hhj5hhhhhM(ubh)}(hhh](h)}(hRead-Write Protectionh]hRead-Write Protection}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj hhhhhM7ubh)}(hX``UFFDIO_REGISTER_MODE_RWP`` enables read-write protection tracking on a memory range. It is similar to (but faster than) ``mprotect(PROT_NONE)`` combined with a signal handler; unlike ``mprotect(PROT_NONE)``, RWP only traps accesses to *present* PTEs, so accesses to unpopulated addresses in a protected range fall through to the normal missing-page path. It uses the PROT_NONE hinting mechanism (same as NUMA balancing) to make pages inaccessible while keeping them resident in memory. Works on anonymous, shmem, and hugetlbfs memory.h](h)}(h``UFFDIO_REGISTER_MODE_RWP``h]hUFFDIO_REGISTER_MODE_RWP}(hj) hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj% ubh^ enables read-write protection tracking on a memory range. It is similar to (but faster than) }(hj% hhhNhNubh)}(h``mprotect(PROT_NONE)``h]hmprotect(PROT_NONE)}(hj; hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj% ubh( combined with a signal handler; unlike }(hj% hhhNhNubh)}(h``mprotect(PROT_NONE)``h]hmprotect(PROT_NONE)}(hjM hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj% ubh, RWP only traps accesses to }(hj% hhhNhNubhemphasis)}(h *present*h]hpresent}(hja hhhNhNubah}(h]h ]h"]h$]h&]uh1j_ hj% ubhX" PTEs, so accesses to unpopulated addresses in a protected range fall through to the normal missing-page path. It uses the PROT_NONE hinting mechanism (same as NUMA balancing) to make pages inaccessible while keeping them resident in memory. Works on anonymous, shmem, and hugetlbfs memory.}(hj% hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM9hj hhubh)}(hRWP is designed for VM memory managers that need to track the working set of guest memory for cold page eviction to tiered or remote storage.h]hRWP is designed for VM memory managers that need to track the working set of guest memory for cold page eviction to tiered or remote storage.}(hjy hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMBhj hhubh)}(h **Setup:**h]hstrong)}(hj h]hSetup:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1j hj ubah}(h]h ]h"]h$]h&]uh1hhhhMEhj hhubjV)}(hhh](j[)}(hOpen a userfaultfd and enable ``UFFD_FEATURE_RWP`` via ``UFFDIO_API``. Optionally request ``UFFD_FEATURE_RWP_ASYNC`` as well — it requires ``UFFD_FEATURE_RWP`` to be set in the same ``UFFDIO_API`` call. h]h)}(hOpen a userfaultfd and enable ``UFFD_FEATURE_RWP`` via ``UFFDIO_API``. Optionally request ``UFFD_FEATURE_RWP_ASYNC`` as well — it requires ``UFFD_FEATURE_RWP`` to be set in the same ``UFFDIO_API`` call.h](hOpen a userfaultfd and enable }(hj hhhNhNubh)}(h``UFFD_FEATURE_RWP``h]hUFFD_FEATURE_RWP}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh via }(hj hhhNhNubh)}(h``UFFDIO_API``h]h UFFDIO_API}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh. Optionally request }(hj hhhNhNubh)}(h``UFFD_FEATURE_RWP_ASYNC``h]hUFFD_FEATURE_RWP_ASYNC}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh as well — it requires }(hj hhhNhNubh)}(h``UFFD_FEATURE_RWP``h]hUFFD_FEATURE_RWP}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh to be set in the same }(hj hhhNhNubh)}(h``UFFDIO_API``h]h UFFDIO_API}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh call.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMGhj ubah}(h]h ]h"]h$]h&]uh1jZhj hhhhhNubj[)}(hRegister the guest memory range with ``UFFDIO_REGISTER_MODE_RWP`` (and ``UFFDIO_REGISTER_MODE_MISSING`` if evicted pages will need to be fetched back from storage). h]h)}(hRegister the guest memory range with ``UFFDIO_REGISTER_MODE_RWP`` (and ``UFFDIO_REGISTER_MODE_MISSING`` if evicted pages will need to be fetched back from storage).h](h%Register the guest memory range with }(hj hhhNhNubh)}(h``UFFDIO_REGISTER_MODE_RWP``h]hUFFDIO_REGISTER_MODE_RWP}(hj! hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh (and }(hj hhhNhNubh)}(h ``UFFDIO_REGISTER_MODE_MISSING``h]hUFFDIO_REGISTER_MODE_MISSING}(hj3 hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh= if evicted pages will need to be fetched back from storage).}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMKhj ubah}(h]h ]h"]h$]h&]uh1jZhj hhhhhNubeh}(h]h ]h"]h$]h&]jjjhj.uh1jUhj hhhhhMGubh)}(h**Feature availability:**h]j )}(hjZ h]hFeature availability:}(hj\ hhhNhNubah}(h]h ]h"]h$]h&]uh1j hjX ubah}(h]h ]h"]h$]h&]uh1hhhhMOhj hhubh)}(hXfRWP is built on top of two kernel primitives: a spare PTE bit owned by userfaultfd (``CONFIG_HAVE_ARCH_USERFAULTFD_WP``) and architecture support for present-but-inaccessible PTEs (``CONFIG_ARCH_HAS_PTE_PROTNONE``). When both are available on a 64-bit kernel, the build selects ``CONFIG_USERFAULTFD_RWP=y`` and the ``VM_UFFD_RWP`` VMA flag becomes available.h](hTRWP is built on top of two kernel primitives: a spare PTE bit owned by userfaultfd (}(hjo hhhNhNubh)}(h#``CONFIG_HAVE_ARCH_USERFAULTFD_WP``h]hCONFIG_HAVE_ARCH_USERFAULTFD_WP}(hjw hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjo ubh>) and architecture support for present-but-inaccessible PTEs (}(hjo hhhNhNubh)}(h ``CONFIG_ARCH_HAS_PTE_PROTNONE``h]hCONFIG_ARCH_HAS_PTE_PROTNONE}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjo ubhA). When both are available on a 64-bit kernel, the build selects }(hjo hhhNhNubh)}(h``CONFIG_USERFAULTFD_RWP=y``h]hCONFIG_USERFAULTFD_RWP=y}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjo ubh and the }(hjo hhhNhNubh)}(h``VM_UFFD_RWP``h]h VM_UFFD_RWP}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjo ubh VMA flag becomes available.}(hjo hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMQhj hhubh)}(hXw``UFFD_FEATURE_RWP`` and ``UFFD_FEATURE_RWP_ASYNC`` are unavailable when the running kernel or architecture does not support them — for example 32-bit kernels (where ``VM_UFFD_RWP`` is unavailable), kernels built without ``CONFIG_USERFAULTFD_RWP``, and architectures whose ptes cannot carry the uffd bit at runtime (e.g. riscv without the ``SVRSW60T59B`` extension). Requesting an unsupported feature in ``uffdio_api.features`` makes ``UFFDIO_API`` fail with ``EINVAL`` and leaves the userfaultfd context uninitialized; the structure is returned zeroed, so the error path cannot be used to discover what the kernel supports. The recommended probe sequence is therefore to open a throwaway userfaultfd, call ``UFFDIO_API`` once with ``features = 0``, inspect the returned bitmask, close that fd, then open the real one and call ``UFFDIO_API`` again with only the supported features set.h](h)}(h``UFFD_FEATURE_RWP``h]hUFFD_FEATURE_RWP}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh and }(hj hhhNhNubh)}(h``UFFD_FEATURE_RWP_ASYNC``h]hUFFD_FEATURE_RWP_ASYNC}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubhu are unavailable when the running kernel or architecture does not support them — for example 32-bit kernels (where }(hj hhhNhNubh)}(h``VM_UFFD_RWP``h]h VM_UFFD_RWP}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh( is unavailable), kernels built without }(hj hhhNhNubh)}(h``CONFIG_USERFAULTFD_RWP``h]hCONFIG_USERFAULTFD_RWP}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh\, and architectures whose ptes cannot carry the uffd bit at runtime (e.g. riscv without the }(hj hhhNhNubh)}(h``SVRSW60T59B``h]h SVRSW60T59B}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh2 extension). Requesting an unsupported feature in }(hj hhhNhNubh)}(h``uffdio_api.features``h]huffdio_api.features}(hj#hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh makes }(hj hhhNhNubh)}(h``UFFDIO_API``h]h UFFDIO_API}(hj5hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh fail with }(hj hhhNhNubh)}(h ``EINVAL``h]hEINVAL}(hjGhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh and leaves the userfaultfd context uninitialized; the structure is returned zeroed, so the error path cannot be used to discover what the kernel supports. The recommended probe sequence is therefore to open a throwaway userfaultfd, call }(hj hhhNhNubh)}(h``UFFDIO_API``h]h UFFDIO_API}(hjYhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh once with }(hj hhhNhNubh)}(h``features = 0``h]h features = 0}(hjkhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubhO, inspect the returned bitmask, close that fd, then open the real one and call }(hj hhhNhNubh)}(h``UFFDIO_API``h]h UFFDIO_API}(hj}hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh, again with only the supported features set.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMXhj hhubh)}(h **Protecting and Unprotecting:**h]j )}(hjh]hProtecting and Unprotecting:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j hjubah}(h]h ]h"]h$]h&]uh1hhhhMfhj hhubh)}(hkUse ``UFFDIO_RWPROTECT`` to protect or unprotect a range, mirroring the ``UFFDIO_WRITEPROTECT`` interface::h](hUse }(hjhhhNhNubh)}(h``UFFDIO_RWPROTECT``h]hUFFDIO_RWPROTECT}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh0 to protect or unprotect a range, mirroring the }(hjhhhNhNubh)}(h``UFFDIO_WRITEPROTECT``h]hUFFDIO_WRITEPROTECT}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh interface:}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhhj hhubh literal_block)}(hstruct uffdio_rwprotect rwp = { .range = { .start = addr, .len = len }, .mode = UFFDIO_RWPROTECT_MODE_RWP, /* protect */ }; ioctl(uffd, UFFDIO_RWPROTECT, &rwp);h]hstruct uffdio_rwprotect rwp = { .range = { .start = addr, .len = len }, .mode = UFFDIO_RWPROTECT_MODE_RWP, /* protect */ }; ioctl(uffd, UFFDIO_RWPROTECT, &rwp);}hjsbah}(h]h ]h"]h$]h&] xml:spacepreserveuh1jhhhMkhj hhubh)}(hSetting ``UFFDIO_RWPROTECT_MODE_RWP`` sets PROT_NONE on present PTEs in the range. Pages stay resident and their physical frames are preserved — only access permissions are removed.h](hSetting }(hjhhhNhNubh)}(h``UFFDIO_RWPROTECT_MODE_RWP``h]hUFFDIO_RWPROTECT_MODE_RWP}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh sets PROT_NONE on present PTEs in the range. Pages stay resident and their physical frames are preserved — only access permissions are removed.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMqhj hhubh)}(hClearing ``UFFDIO_RWPROTECT_MODE_RWP`` restores normal VMA permissions and wakes any faulting threads (unless ``UFFDIO_RWPROTECT_MODE_DONTWAKE`` is set).h](h Clearing }(hjhhhNhNubh)}(h``UFFDIO_RWPROTECT_MODE_RWP``h]hUFFDIO_RWPROTECT_MODE_RWP}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhH restores normal VMA permissions and wakes any faulting threads (unless }(hjhhhNhNubh)}(h"``UFFDIO_RWPROTECT_MODE_DONTWAKE``h]hUFFDIO_RWPROTECT_MODE_DONTWAKE}(hj*hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh is set).}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMuhj hhubh)}(h**Scope of protection:**h]j )}(hjDh]hScope of protection:}(hjFhhhNhNubah}(h]h ]h"]h$]h&]uh1j hjBubah}(h]h ]h"]h$]h&]uh1hhhhMxhj hhubh)}(hXRWP protection is a property of *present* PTEs. ``UFFDIO_RWPROTECT`` only affects entries that are already populated. Unpopulated addresses within the range remain unpopulated; when first accessed they fault through the normal missing path (``do_anonymous_page()``, ``do_swap_page()``, ``finish_fault()``) and the resulting PTE is not RWP-protected. To observe the population itself, co-register the range with ``UFFDIO_REGISTER_MODE_MISSING``.h](h RWP protection is a property of }(hjYhhhNhNubj` )}(h *present*h]hpresent}(hjahhhNhNubah}(h]h ]h"]h$]h&]uh1j_ hjYubh PTEs. }(hjYhhhNhNubh)}(h``UFFDIO_RWPROTECT``h]hUFFDIO_RWPROTECT}(hjshhhNhNubah}(h]h ]h"]h$]h&]uh1hhjYubh only affects entries that are already populated. Unpopulated addresses within the range remain unpopulated; when first accessed they fault through the normal missing path (}(hjYhhhNhNubh)}(h``do_anonymous_page()``h]hdo_anonymous_page()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjYubh, }(hjYhhhNhNubh)}(h``do_swap_page()``h]hdo_swap_page()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjYubh, }(hjYhhhNhNubh)}(h``finish_fault()``h]hfinish_fault()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjYubhk) and the resulting PTE is not RWP-protected. To observe the population itself, co-register the range with }(hjYhhhNhNubh)}(h ``UFFDIO_REGISTER_MODE_MISSING``h]hUFFDIO_REGISTER_MODE_MISSING}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjYubh.}(hjYhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMzhj hhubh)}(hXProtection is preserved across page reclaim: a page swapped out while RWP-protected carries the marker on its swap entry, and swap-in restores the PROT_NONE state so the first access after swap-in still faults. The same applies to pages temporarily replaced by migration entries.h]hXProtection is preserved across page reclaim: a page swapped out while RWP-protected carries the marker on its swap entry, and swap-in restores the PROT_NONE state so the first access after swap-in still faults. The same applies to pages temporarily replaced by migration entries.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubh)}(hXOperations that drop the PTE entirely — ``MADV_DONTNEED`` on anonymous memory, hole-punch on shmem, truncation of a file mapping — also drop the RWP marker: the next access re-populates the range without protection. Unlike WP (which persists via ``PTE_MARKER_UFFD_WP``), there is no persistent RWP marker today. The user needs to re-arm the range with ``UFFDIO_RWPROTECT`` after any operation that explicitly frees PTEs.h](h*Operations that drop the PTE entirely — }(hjhhhNhNubh)}(h``MADV_DONTNEED``h]h MADV_DONTNEED}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh on anonymous memory, hole-punch on shmem, truncation of a file mapping — also drop the RWP marker: the next access re-populates the range without protection. Unlike WP (which persists via }(hjhhhNhNubh)}(h``PTE_MARKER_UFFD_WP``h]hPTE_MARKER_UFFD_WP}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhT), there is no persistent RWP marker today. The user needs to re-arm the range with }(hjhhhNhNubh)}(h``UFFDIO_RWPROTECT``h]hUFFDIO_RWPROTECT}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh0 after any operation that explicitly frees PTEs.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj hhubh)}(h**Fault Handling:**h]j )}(hj'h]hFault Handling:}(hj)hhhNhNubah}(h]h ]h"]h$]h&]uh1j hj%ubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubh)}(h"When a protected page is accessed:h]h"When a protected page is accessed:}(hj<hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubjt)}(hhh](j[)}(hX**Sync mode** (default): The faulting thread blocks and a ``UFFD_PAGEFAULT_FLAG_RWP`` message is delivered to the userfaultfd handler. The handler resolves the fault with ``UFFDIO_RWPROTECT`` (clearing ``MODE_RWP``), which restores the PTE permissions and wakes the faulting thread. h]h)}(hX**Sync mode** (default): The faulting thread blocks and a ``UFFD_PAGEFAULT_FLAG_RWP`` message is delivered to the userfaultfd handler. The handler resolves the fault with ``UFFDIO_RWPROTECT`` (clearing ``MODE_RWP``), which restores the PTE permissions and wakes the faulting thread.h](j )}(h **Sync mode**h]h Sync mode}(hjUhhhNhNubah}(h]h ]h"]h$]h&]uh1j hjQubh- (default): The faulting thread blocks and a }(hjQhhhNhNubh)}(h``UFFD_PAGEFAULT_FLAG_RWP``h]hUFFD_PAGEFAULT_FLAG_RWP}(hjghhhNhNubah}(h]h ]h"]h$]h&]uh1hhjQubhV message is delivered to the userfaultfd handler. The handler resolves the fault with }(hjQhhhNhNubh)}(h``UFFDIO_RWPROTECT``h]hUFFDIO_RWPROTECT}(hjyhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjQubh (clearing }(hjQhhhNhNubh)}(h ``MODE_RWP``h]hMODE_RWP}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjQubhD), which restores the PTE permissions and wakes the faulting thread.}(hjQhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjMubah}(h]h ]h"]h$]h&]uh1jZhjJhhhhhNubj[)}(h**Async mode** (``UFFD_FEATURE_RWP_ASYNC``): The kernel automatically restores PTE permissions and the thread continues without blocking. No message is delivered to the handler. h]h)}(h**Async mode** (``UFFD_FEATURE_RWP_ASYNC``): The kernel automatically restores PTE permissions and the thread continues without blocking. No message is delivered to the handler.h](j )}(h**Async mode**h]h Async mode}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j hjubh (}(hjhhhNhNubh)}(h``UFFD_FEATURE_RWP_ASYNC``h]hUFFD_FEATURE_RWP_ASYNC}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh): The kernel automatically restores PTE permissions and the thread continues without blocking. No message is delivered to the handler.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1jZhjJhhhhhNubeh}(h]h ]h"]h$]h&]jjuh1jshhhMhj hhubh)}(h**Runtime Mode Switching:**h]j )}(hjh]hRuntime Mode Switching:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j hjubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubh)}(hXZ``UFFDIO_SET_MODE`` toggles ``UFFD_FEATURE_RWP_ASYNC`` at runtime, allowing the VMM to switch between lightweight async detection and safe sync eviction without re-registering. The toggle takes ``mmap_write_lock()`` and calls ``vma_start_write()`` on each UFFD-armed VMA, draining in-flight per-VMA-locked faults before the new mode takes effect.h](h)}(h``UFFDIO_SET_MODE``h]hUFFDIO_SET_MODE}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh toggles }(hjhhhNhNubh)}(h``UFFD_FEATURE_RWP_ASYNC``h]hUFFD_FEATURE_RWP_ASYNC}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh at runtime, allowing the VMM to switch between lightweight async detection and safe sync eviction without re-registering. The toggle takes }(hjhhhNhNubh)}(h``mmap_write_lock()``h]hmmap_write_lock()}(hj&hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh and calls }(hjhhhNhNubh)}(h``vma_start_write()``h]hvma_start_write()}(hj8hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhc on each UFFD-armed VMA, draining in-flight per-VMA-locked faults before the new mode takes effect.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj hhubh)}(h,**Working-set detection with PAGEMAP_SCAN:**h]j )}(hjRh]h(Working-set detection with PAGEMAP_SCAN:}(hjThhhNhNubah}(h]h ]h"]h$]h&]uh1j hjPubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubh)}(hX)RWP-protected PTEs carry the uffd PTE bit; an access (and, in async mode, its auto-resolution) clears it. ``PAGEMAP_SCAN`` reports ``PAGE_IS_ACCESSED`` once the bit is clear on a ``VM_UFFD_RWP`` VMA, so a *non-inverted* scan reports the pages that were touched during the interval -- the hot set::h](hjRWP-protected PTEs carry the uffd PTE bit; an access (and, in async mode, its auto-resolution) clears it. }(hjghhhNhNubh)}(h``PAGEMAP_SCAN``h]h PAGEMAP_SCAN}(hjohhhNhNubah}(h]h ]h"]h$]h&]uh1hhjgubh reports }(hjghhhNhNubh)}(h``PAGE_IS_ACCESSED``h]hPAGE_IS_ACCESSED}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjgubh once the bit is clear on a }(hjghhhNhNubh)}(h``VM_UFFD_RWP``h]h VM_UFFD_RWP}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjgubh VMA, so a }(hjghhhNhNubj` )}(h*non-inverted*h]h non-inverted}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j_ hjgubhM scan reports the pages that were touched during the interval -- the hot set:}(hjghhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj hhubj)}(hX$struct pm_scan_arg arg = { .size = sizeof(arg), .start = guest_mem_start, .end = guest_mem_end, .vec = (uint64_t)regions, .vec_len = regions_len, .category_mask = PAGE_IS_ACCESSED, .return_mask = PAGE_IS_ACCESSED, }; long n = ioctl(pagemap_fd, PAGEMAP_SCAN, &arg);h]hX$struct pm_scan_arg arg = { .size = sizeof(arg), .start = guest_mem_start, .end = guest_mem_end, .vec = (uint64_t)regions, .vec_len = regions_len, .category_mask = PAGE_IS_ACCESSED, .return_mask = PAGE_IS_ACCESSED, }; long n = ioctl(pagemap_fd, PAGEMAP_SCAN, &arg);}hjsbah}(h]h ]h"]h$]h&]jjuh1jhhhMhj hhubh)}(hXThe returned ``page_region`` array lists the hot ranges. ``PAGE_IS_ACCESSED`` is set on an accessed page whether it is still present or has since been swapped out, so the hot scan needs no ``PAGE_IS_PRESENT`` filter -- unpopulated holes carry neither bit and are excluded on their own.h](h The returned }(hjhhhNhNubh)}(h``page_region``h]h page_region}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh array lists the hot ranges. }(hjhhhNhNubh)}(h``PAGE_IS_ACCESSED``h]hPAGE_IS_ACCESSED}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhp is set on an accessed page whether it is still present or has since been swapped out, so the hot scan needs no }(hjhhhNhNubh)}(h``PAGE_IS_PRESENT``h]hPAGE_IS_PRESENT}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhM filter -- unpopulated holes carry neither bit and are excluded on their own.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj hhubh)}(hXTrack the hot set and reclaim everything else from the backing file (see the workflow below). Do **not** invert the scan to enumerate "cold" pages directly: an inverted scan reports only the ``VM_UFFD_RWP`` PTEs that are still protected, i.e. the resident portion of *this* VMA. For a file mapping the working set spans the whole file -- pages that live in the page cache but are not mapped into this VMA (a pre-populated tmpfs file, or memory populated through another mapping) are ``pte_none`` here, never appear in the scan, and would never be considered for eviction even though they occupy memory. Driving eviction from "file offsets minus the hot set" avoids that blind spot; a cold PTE scan cannot. To additionally record the *first* access to a cached but unmapped page (e.g. pre-populated content) as hot, co-register the range with ``UFFDIO_REGISTER_MODE_MINOR``: such accesses then fault as minor faults instead of mapping the page silently.h](haTrack the hot set and reclaim everything else from the backing file (see the workflow below). Do }(hjhhhNhNubj )}(h**not**h]hnot}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j hjubh[ invert the scan to enumerate “cold” pages directly: an inverted scan reports only the }(hjhhhNhNubh)}(h``VM_UFFD_RWP``h]h VM_UFFD_RWP}(hj)hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh= PTEs that are still protected, i.e. the resident portion of }(hjhhhNhNubj` )}(h*this*h]hthis}(hj;hhhNhNubah}(h]h ]h"]h$]h&]uh1j_ hjubh VMA. For a file mapping the working set spans the whole file -- pages that live in the page cache but are not mapped into this VMA (a pre-populated tmpfs file, or memory populated through another mapping) are }(hjhhhNhNubh)}(h ``pte_none``h]hpte_none}(hjMhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh here, never appear in the scan, and would never be considered for eviction even though they occupy memory. Driving eviction from “file offsets minus the hot set” avoids that blind spot; a cold PTE scan cannot. To additionally record the }(hjhhhNhNubj` )}(h*first*h]hfirst}(hj_hhhNhNubah}(h]h ]h"]h$]h&]uh1j_ hjubhf access to a cached but unmapped page (e.g. pre-populated content) as hot, co-register the range with }(hjhhhNhNubh)}(h``UFFDIO_REGISTER_MODE_MINOR``h]hUFFDIO_REGISTER_MODE_MINOR}(hjqhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhP: such accesses then fault as minor faults instead of mapping the page silently.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj hhubh)}(h **Cleanup:**h]j )}(hjh]hCleanup:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j hjubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubh)}(hWhen the userfaultfd is closed or the range is unregistered, all PROT_NONE PTEs are automatically restored to their normal VMA permissions. This prevents pages from becoming permanently inaccessible.h]hWhen the userfaultfd is closed or the range is unregistered, all PROT_NONE PTEs are automatically restored to their normal VMA permissions. This prevents pages from becoming permanently inaccessible.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubh)}(h&**VMM Working Set Tracking Workflow:**h]j )}(hjh]h"VMM Working Set Tracking Workflow:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j hjubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubh)}(hXA typical VMM lifecycle for cold page eviction to tiered storage. Two mappings of the same shmem (or hugetlbfs) file are used: ``guest_mem`` is the RWP-registered mapping that vCPUs access through, and ``io_mem`` is a private mapping for VMM-side I/O. Reading ``io_mem`` does not go through the RWP-protected PTEs of ``guest_mem``, so the VMM's own ``pwrite()`` never traps on its own ::h](hA typical VMM lifecycle for cold page eviction to tiered storage. Two mappings of the same shmem (or hugetlbfs) file are used: }(hjhhhNhNubh)}(h ``guest_mem``h]h guest_mem}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh> is the RWP-registered mapping that vCPUs access through, and }(hjhhhNhNubh)}(h ``io_mem``h]hio_mem}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh0 is a private mapping for VMM-side I/O. Reading }(hjhhhNhNubh)}(h ``io_mem``h]hio_mem}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh/ does not go through the RWP-protected PTEs of }(hjhhhNhNubh)}(h ``guest_mem``h]h guest_mem}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh, so the VMM’s own }(hjhhhNhNubh)}(h ``pwrite()``h]hpwrite()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh never traps on its own}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj hhubj)}(hX /* One-time setup */ fd = memfd_create("guest", MFD_CLOEXEC); ftruncate(fd, guest_size); guest_mem = mmap(NULL, guest_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); /* vCPU view, RWP-registered */ io_mem = mmap(NULL, guest_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); /* VMM I/O view, unprotected */ uffd = userfaultfd(O_CLOEXEC | O_NONBLOCK); struct uffdio_api api = { .api = UFFD_API, .features = UFFD_FEATURE_RWP | UFFD_FEATURE_RWP_ASYNC, }; ioctl(uffd, UFFDIO_API, &api); if (!(api.features & UFFD_FEATURE_RWP)) /* RWP unavailable on this kernel/arch -- fall back. */ ioctl(uffd, UFFDIO_REGISTER, &(struct uffdio_register){ .range = { guest_mem, guest_size }, .mode = UFFDIO_REGISTER_MODE_RWP | UFFDIO_REGISTER_MODE_MISSING, }); /* Tracking loop */ while (vm_running) { /* 1. Detection phase (async -- no vCPU stalls) */ ioctl(uffd, UFFDIO_RWPROTECT, &(struct uffdio_rwprotect){ .range = full_range, .mode = UFFDIO_RWPROTECT_MODE_RWP }); sleep(tracking_interval); /* * 2. Switch to sync BEFORE scanning. In async mode a vCPU * access races eviction: it would auto-resolve and mark the * page hot just as the VMM writes it out and punches it, * losing the update. Sync mode makes such accesses block and * be delivered, freezing the hot snapshot for the rest of the * iteration. */ ioctl(uffd, UFFDIO_SET_MODE, &(struct uffdio_set_mode){ .disable = UFFD_FEATURE_RWP_ASYNC }); /* 3. Read the hot set: pages touched this interval. */ ioctl(pagemap_fd, PAGEMAP_SCAN, &(struct pm_scan_arg){ .category_mask = PAGE_IS_ACCESSED, .return_mask = PAGE_IS_ACCESSED, ... }); /* * 4. Reclaim the file offsets that are NOT in the hot set. * Driving this from the file's offset space (rather than from a * cold PTE scan) also reclaims pages that are cached but not * mapped into guest_mem, e.g. pre-populated content. */ for each non-hot offset range: /* Read from io_mem -- bypasses RWP, no fault. */ pwrite(storage_fd, (char *)io_mem + off, len, off); /* Drop the page from the shared file. */ fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, off, len); /* * Wake any vCPU blocked on the RWP fault for this range: * fallocate() does not iterate ctx->fault_pending_wqh. */ ioctl(uffd, UFFDIO_WAKE, &(struct uffdio_range){ .start = (uintptr_t)guest_mem + off, .len = len }); /* 5. Resume async tracking */ ioctl(uffd, UFFDIO_SET_MODE, &(struct uffdio_set_mode){ .enable = UFFD_FEATURE_RWP_ASYNC }); }h]hX /* One-time setup */ fd = memfd_create("guest", MFD_CLOEXEC); ftruncate(fd, guest_size); guest_mem = mmap(NULL, guest_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); /* vCPU view, RWP-registered */ io_mem = mmap(NULL, guest_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); /* VMM I/O view, unprotected */ uffd = userfaultfd(O_CLOEXEC | O_NONBLOCK); struct uffdio_api api = { .api = UFFD_API, .features = UFFD_FEATURE_RWP | UFFD_FEATURE_RWP_ASYNC, }; ioctl(uffd, UFFDIO_API, &api); if (!(api.features & UFFD_FEATURE_RWP)) /* RWP unavailable on this kernel/arch -- fall back. */ ioctl(uffd, UFFDIO_REGISTER, &(struct uffdio_register){ .range = { guest_mem, guest_size }, .mode = UFFDIO_REGISTER_MODE_RWP | UFFDIO_REGISTER_MODE_MISSING, }); /* Tracking loop */ while (vm_running) { /* 1. Detection phase (async -- no vCPU stalls) */ ioctl(uffd, UFFDIO_RWPROTECT, &(struct uffdio_rwprotect){ .range = full_range, .mode = UFFDIO_RWPROTECT_MODE_RWP }); sleep(tracking_interval); /* * 2. Switch to sync BEFORE scanning. In async mode a vCPU * access races eviction: it would auto-resolve and mark the * page hot just as the VMM writes it out and punches it, * losing the update. Sync mode makes such accesses block and * be delivered, freezing the hot snapshot for the rest of the * iteration. */ ioctl(uffd, UFFDIO_SET_MODE, &(struct uffdio_set_mode){ .disable = UFFD_FEATURE_RWP_ASYNC }); /* 3. Read the hot set: pages touched this interval. */ ioctl(pagemap_fd, PAGEMAP_SCAN, &(struct pm_scan_arg){ .category_mask = PAGE_IS_ACCESSED, .return_mask = PAGE_IS_ACCESSED, ... }); /* * 4. Reclaim the file offsets that are NOT in the hot set. * Driving this from the file's offset space (rather than from a * cold PTE scan) also reclaims pages that are cached but not * mapped into guest_mem, e.g. pre-populated content. */ for each non-hot offset range: /* Read from io_mem -- bypasses RWP, no fault. */ pwrite(storage_fd, (char *)io_mem + off, len, off); /* Drop the page from the shared file. */ fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, off, len); /* * Wake any vCPU blocked on the RWP fault for this range: * fallocate() does not iterate ctx->fault_pending_wqh. */ ioctl(uffd, UFFDIO_WAKE, &(struct uffdio_range){ .start = (uintptr_t)guest_mem + off, .len = len }); /* 5. Resume async tracking */ ioctl(uffd, UFFDIO_SET_MODE, &(struct uffdio_set_mode){ .enable = UFFD_FEATURE_RWP_ASYNC }); }}hj-sbah}(h]h ]h"]h$]h&]jjuh1jhhhMhj hhubh)}(hXTDuring step 4, a vCPU that accesses a ``guest_mem`` offset being evicted blocks with a ``UFFD_PAGEFAULT_FLAG_RWP`` fault while the eviction is in progress. After ``fallocate()`` punches the page out and ``UFFDIO_WAKE`` fires, the vCPU retries the access, faults as ``MISSING``, and the handler resolves it with ``UFFDIO_COPY`` from storage.h](h&During step 4, a vCPU that accesses a }(hj;hhhNhNubh)}(h ``guest_mem``h]h guest_mem}(hjChhhNhNubah}(h]h ]h"]h$]h&]uh1hhj;ubh$ offset being evicted blocks with a }(hj;hhhNhNubh)}(h``UFFD_PAGEFAULT_FLAG_RWP``h]hUFFD_PAGEFAULT_FLAG_RWP}(hjUhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj;ubh0 fault while the eviction is in progress. After }(hj;hhhNhNubh)}(h``fallocate()``h]h fallocate()}(hjghhhNhNubah}(h]h ]h"]h$]h&]uh1hhj;ubh punches the page out and }(hj;hhhNhNubh)}(h``UFFDIO_WAKE``h]h UFFDIO_WAKE}(hjyhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj;ubh/ fires, the vCPU retries the access, faults as }(hj;hhhNhNubh)}(h ``MISSING``h]hMISSING}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj;ubh#, and the handler resolves it with }(hj;hhhNhNubh)}(h``UFFDIO_COPY``h]h UFFDIO_COPY}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj;ubh from storage.}(hj;hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM"hj hhubh)}(hXThis workflow targets shmem and hugetlbfs (both support a private ``io_mem`` mapping over the same fd). Anonymous-memory backings need a different inner-loop strategy because the VMM has no way to read the page without going through the RWP-protected mapping.h](hBThis workflow targets shmem and hugetlbfs (both support a private }(hjhhhNhNubh)}(h ``io_mem``h]hio_mem}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh mapping over the same fd). Anonymous-memory backings need a different inner-loop strategy because the VMM has no way to read the page without going through the RWP-protected mapping.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM(hj hhubeh}(h]read-write-protectionah ]h"]read-write protectionah$]h&]uh1hhj5hhhhhM7ubeh}(h]apiah ]h"]apiah$]h&]uh1hhhhhhhhK3ubh)}(hhh](h)}(hQEMU/KVMh]hQEMU/KVM}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhM.ubh)}(hXQEMU/KVM is using the ``userfaultfd`` syscall to implement postcopy live migration. Postcopy live migration is one form of memory externalization consisting of a virtual machine running with part or all of its memory residing on a different node in the cloud. The ``userfaultfd`` abstraction is generic enough that not a single line of KVM kernel code had to be modified in order to add postcopy live migration to QEMU.h](hQEMU/KVM is using the }(hjhhhNhNubh)}(h``userfaultfd``h]h userfaultfd}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh syscall to implement postcopy live migration. Postcopy live migration is one form of memory externalization consisting of a virtual machine running with part or all of its memory residing on a different node in the cloud. The }(hjhhhNhNubh)}(h``userfaultfd``h]h userfaultfd}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh abstraction is generic enough that not a single line of KVM kernel code had to be modified in order to add postcopy live migration to QEMU.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM0hjhhubh)}(hX)Guest async page faults, ``FOLL_NOWAIT`` and all other ``GUP*`` features work just fine in combination with userfaults. Userfaults trigger async page faults in the guest scheduler so those guest processes that aren't waiting for userfaults (i.e. network bound) can keep running in the guest vcpus.h](hGuest async page faults, }(hj(hhhNhNubh)}(h``FOLL_NOWAIT``h]h FOLL_NOWAIT}(hj0hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj(ubh and all other }(hj(hhhNhNubh)}(h``GUP*``h]hGUP*}(hjBhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj(ubh features work just fine in combination with userfaults. Userfaults trigger async page faults in the guest scheduler so those guest processes that aren’t waiting for userfaults (i.e. network bound) can keep running in the guest vcpus.}(hj(hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM8hjhhubh)}(hIt is generally beneficial to run one pass of precopy live migration just before starting postcopy live migration, in order to avoid generating userfaults for readonly guest regions.h]hIt is generally beneficial to run one pass of precopy live migration just before starting postcopy live migration, in order to avoid generating userfaults for readonly guest regions.}(hjZhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM>hjhhubh)}(hXThe implementation of postcopy live migration currently uses one single bidirectional socket but in the future two different sockets will be used (to reduce the latency of the userfaults to the minimum possible without having to decrease ``/proc/sys/net/ipv4/tcp_wmem``).h](hThe implementation of postcopy live migration currently uses one single bidirectional socket but in the future two different sockets will be used (to reduce the latency of the userfaults to the minimum possible without having to decrease }(hjhhhhNhNubh)}(h``/proc/sys/net/ipv4/tcp_wmem``h]h/proc/sys/net/ipv4/tcp_wmem}(hjphhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhubh).}(hjhhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMBhjhhubh)}(hXeThe QEMU in the source node writes all pages that it knows are missing in the destination node, into the socket, and the migration thread of the QEMU running in the destination node runs ``UFFDIO_COPY|ZEROPAGE`` ioctls on the ``userfaultfd`` in order to map the received pages into the guest (``UFFDIO_ZEROCOPY`` is used if the source page was a zero page).h](hThe QEMU in the source node writes all pages that it knows are missing in the destination node, into the socket, and the migration thread of the QEMU running in the destination node runs }(hjhhhNhNubh)}(h``UFFDIO_COPY|ZEROPAGE``h]hUFFDIO_COPY|ZEROPAGE}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh ioctls on the }(hjhhhNhNubh)}(h``userfaultfd``h]h userfaultfd}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh4 in order to map the received pages into the guest (}(hjhhhNhNubh)}(h``UFFDIO_ZEROCOPY``h]hUFFDIO_ZEROCOPY}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh- is used if the source page was a zero page).}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMGhjhhubh)}(hXA different postcopy thread in the destination node listens with poll() to the ``userfaultfd`` in parallel. When a ``POLLIN`` event is generated after a userfault triggers, the postcopy thread read() from the ``userfaultfd`` and receives the fault address (or ``-EAGAIN`` in case the userfault was already resolved and waken by a ``UFFDIO_COPY|ZEROPAGE`` run by the parallel QEMU migration thread).h](hOA different postcopy thread in the destination node listens with poll() to the }(hjhhhNhNubh)}(h``userfaultfd``h]h userfaultfd}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh in parallel. When a }(hjhhhNhNubh)}(h ``POLLIN``h]hPOLLIN}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhT event is generated after a userfault triggers, the postcopy thread read() from the }(hjhhhNhNubh)}(h``userfaultfd``h]h userfaultfd}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh$ and receives the fault address (or }(hjhhhNhNubh)}(h ``-EAGAIN``h]h-EAGAIN}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh; in case the userfault was already resolved and waken by a }(hjhhhNhNubh)}(h``UFFDIO_COPY|ZEROPAGE``h]hUFFDIO_COPY|ZEROPAGE}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh, run by the parallel QEMU migration thread).}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMMhjhhubh)}(hXAfter the QEMU postcopy thread (running in the destination node) gets the userfault address it writes the information about the missing page into the socket. The QEMU source node receives the information and roughly "seeks" to that page address and continues sending all remaining missing pages from that new page offset. Soon after that (just the time to flush the tcp_wmem queue through the network) the migration thread in the QEMU running in the destination node will receive the page that triggered the userfault and it'll map it as usual with the ``UFFDIO_COPY|ZEROPAGE`` (without actually knowing if it was spontaneously sent by the source or if it was an urgent page requested through a userfault).h](hX/After the QEMU postcopy thread (running in the destination node) gets the userfault address it writes the information about the missing page into the socket. The QEMU source node receives the information and roughly “seeks” to that page address and continues sending all remaining missing pages from that new page offset. Soon after that (just the time to flush the tcp_wmem queue through the network) the migration thread in the QEMU running in the destination node will receive the page that triggered the userfault and it’ll map it as usual with the }(hj4hhhNhNubh)}(h``UFFDIO_COPY|ZEROPAGE``h]hUFFDIO_COPY|ZEROPAGE}(hj<hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj4ubh (without actually knowing if it was spontaneously sent by the source or if it was an urgent page requested through a userfault).}(hj4hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMThjhhubh)}(hXBy the time the userfaults start, the QEMU in the destination node doesn't need to keep any per-page state bitmap relative to the live migration around and a single per-page bitmap has to be maintained in the QEMU running in the source node to know which pages are still missing in the destination node. The bitmap in the source node is checked to find which missing pages to send in round robin and we seek over it when receiving incoming userfaults. After sending each page of course the bitmap is updated accordingly. It's also useful to avoid sending the same page twice (in case the userfault is read by the postcopy thread just before ``UFFDIO_COPY|ZEROPAGE`` runs in the migration thread).h](hXBy the time the userfaults start, the QEMU in the destination node doesn’t need to keep any per-page state bitmap relative to the live migration around and a single per-page bitmap has to be maintained in the QEMU running in the source node to know which pages are still missing in the destination node. The bitmap in the source node is checked to find which missing pages to send in round robin and we seek over it when receiving incoming userfaults. After sending each page of course the bitmap is updated accordingly. It’s also useful to avoid sending the same page twice (in case the userfault is read by the postcopy thread just before }(hjThhhNhNubh)}(h``UFFDIO_COPY|ZEROPAGE``h]hUFFDIO_COPY|ZEROPAGE}(hj\hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjTubh runs in the migration thread).}(hjThhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM`hjhhubeh}(h]qemu-kvmah ]h"]qemu/kvmah$]h&]uh1hhhhhhhhM.ubh)}(hhh](h)}(hNon-cooperative userfaultfdh]hNon-cooperative userfaultfd}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj|hhhhhMmubh)}(hXWhen the ``userfaultfd`` is monitored by an external manager, the manager must be able to track changes in the process virtual memory layout. Userfaultfd can notify the manager about such changes using the same read(2) protocol as for the page fault notifications. The manager has to explicitly enable these events by setting appropriate bits in ``uffdio_api.features`` passed to ``UFFDIO_API`` ioctl:h](h When the }(hjhhhNhNubh)}(h``userfaultfd``h]h userfaultfd}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhXB is monitored by an external manager, the manager must be able to track changes in the process virtual memory layout. Userfaultfd can notify the manager about such changes using the same read(2) protocol as for the page fault notifications. The manager has to explicitly enable these events by setting appropriate bits in }(hjhhhNhNubh)}(h``uffdio_api.features``h]huffdio_api.features}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh passed to }(hjhhhNhNubh)}(h``UFFDIO_API``h]h UFFDIO_API}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh ioctl:}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMohj|hhubhdefinition_list)}(hhh](hdefinition_list_item)}(hX>``UFFD_FEATURE_EVENT_FORK`` enable ``userfaultfd`` hooks for fork(). When this feature is enabled, the ``userfaultfd`` context of the parent process is duplicated into the newly created process. The manager receives ``UFFD_EVENT_FORK`` with file descriptor of the new ``userfaultfd`` context in the ``uffd_msg.fork``. h](hterm)}(h``UFFD_FEATURE_EVENT_FORK``h]h)}(hjh]hUFFD_FEATURE_EVENT_FORK}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubah}(h]h ]h"]h$]h&]uh1jhhhM{hjubh definition)}(hhh]h)}(hX!enable ``userfaultfd`` hooks for fork(). When this feature is enabled, the ``userfaultfd`` context of the parent process is duplicated into the newly created process. The manager receives ``UFFD_EVENT_FORK`` with file descriptor of the new ``userfaultfd`` context in the ``uffd_msg.fork``.h](henable }(hjhhhNhNubh)}(h``userfaultfd``h]h userfaultfd}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh5 hooks for fork(). When this feature is enabled, the }(hjhhhNhNubh)}(h``userfaultfd``h]h userfaultfd}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhb context of the parent process is duplicated into the newly created process. The manager receives }(hjhhhNhNubh)}(h``UFFD_EVENT_FORK``h]hUFFD_EVENT_FORK}(hj&hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh! with file descriptor of the new }(hjhhhNhNubh)}(h``userfaultfd``h]h userfaultfd}(hj8hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh context in the }(hjhhhNhNubh)}(h``uffd_msg.fork``h]h uffd_msg.fork}(hjJhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMwhjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhhhM{hjubj)}(hX0``UFFD_FEATURE_EVENT_REMAP`` enable notifications about mremap() calls. When the non-cooperative process moves a virtual memory area to a different location, the manager will receive ``UFFD_EVENT_REMAP``. The ``uffd_msg.remap`` will contain the old and new addresses of the area and its original length. h](j)}(h``UFFD_FEATURE_EVENT_REMAP``h]h)}(hjth]hUFFD_FEATURE_EVENT_REMAP}(hjvhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjrubah}(h]h ]h"]h$]h&]uh1jhhhMhjnubj)}(hhh]h)}(hXenable notifications about mremap() calls. When the non-cooperative process moves a virtual memory area to a different location, the manager will receive ``UFFD_EVENT_REMAP``. The ``uffd_msg.remap`` will contain the old and new addresses of the area and its original length.h](henable notifications about mremap() calls. When the non-cooperative process moves a virtual memory area to a different location, the manager will receive }(hjhhhNhNubh)}(h``UFFD_EVENT_REMAP``h]hUFFD_EVENT_REMAP}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh. The }(hjhhhNhNubh)}(h``uffd_msg.remap``h]huffd_msg.remap}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhL will contain the old and new addresses of the area and its original length.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM~hjubah}(h]h ]h"]h$]h&]uh1jhjnubeh}(h]h ]h"]h$]h&]uh1jhhhMhjhhubj)}(hX``UFFD_FEATURE_EVENT_REMOVE`` enable notifications about madvise(MADV_REMOVE) and madvise(MADV_DONTNEED) calls. The event ``UFFD_EVENT_REMOVE`` will be generated upon these calls to madvise(). The ``uffd_msg.remove`` will contain start and end addresses of the removed area. h](j)}(h``UFFD_FEATURE_EVENT_REMOVE``h]h)}(hjh]hUFFD_FEATURE_EVENT_REMOVE}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubah}(h]h ]h"]h$]h&]uh1jhhhMhjubj)}(hhh]h)}(henable notifications about madvise(MADV_REMOVE) and madvise(MADV_DONTNEED) calls. The event ``UFFD_EVENT_REMOVE`` will be generated upon these calls to madvise(). The ``uffd_msg.remove`` will contain start and end addresses of the removed area.h](h\enable notifications about madvise(MADV_REMOVE) and madvise(MADV_DONTNEED) calls. The event }(hjhhhNhNubh)}(h``UFFD_EVENT_REMOVE``h]hUFFD_EVENT_REMOVE}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh6 will be generated upon these calls to madvise(). The }(hjhhhNhNubh)}(h``uffd_msg.remove``h]huffd_msg.remove}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh: will contain start and end addresses of the removed area.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhhhMhjhhubj)}(h``UFFD_FEATURE_EVENT_UNMAP`` enable notifications about memory unmapping. The manager will get ``UFFD_EVENT_UNMAP`` with ``uffd_msg.remove`` containing start and end addresses of the unmapped area. h](j)}(h``UFFD_FEATURE_EVENT_UNMAP``h]h)}(hj,h]hUFFD_FEATURE_EVENT_UNMAP}(hj.hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj*ubah}(h]h ]h"]h$]h&]uh1jhhhMhj&ubj)}(hhh]h)}(henable notifications about memory unmapping. The manager will get ``UFFD_EVENT_UNMAP`` with ``uffd_msg.remove`` containing start and end addresses of the unmapped area.h](hBenable notifications about memory unmapping. The manager will get }(hjDhhhNhNubh)}(h``UFFD_EVENT_UNMAP``h]hUFFD_EVENT_UNMAP}(hjLhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjDubh with }(hjDhhhNhNubh)}(h``uffd_msg.remove``h]huffd_msg.remove}(hj^hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjDubh9 containing start and end addresses of the unmapped area.}(hjDhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjAubah}(h]h ]h"]h$]h&]uh1jhj&ubeh}(h]h ]h"]h$]h&]uh1jhhhMhjhhubeh}(h]h ]h"]h$]h&]uh1jhj|hhhhhNubh)}(hXgAlthough the ``UFFD_FEATURE_EVENT_REMOVE`` and ``UFFD_FEATURE_EVENT_UNMAP`` are pretty similar, they quite differ in the action expected from the ``userfaultfd`` manager. In the former case, the virtual memory is removed, but the area is not, the area remains monitored by the ``userfaultfd``, and if a page fault occurs in that area it will be delivered to the manager. The proper resolution for such page fault is to zeromap the faulting address. However, in the latter case, when an area is unmapped, either explicitly (with munmap() system call), or implicitly (e.g. during mremap()), the area is removed and in turn the ``userfaultfd`` context for such area disappears too and the manager will not get further userland page faults from the removed area. Still, the notification is required in order to prevent manager from using ``UFFDIO_COPY`` on the unmapped area.h](h Although the }(hjhhhNhNubh)}(h``UFFD_FEATURE_EVENT_REMOVE``h]hUFFD_FEATURE_EVENT_REMOVE}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh and }(hjhhhNhNubh)}(h``UFFD_FEATURE_EVENT_UNMAP``h]hUFFD_FEATURE_EVENT_UNMAP}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhG are pretty similar, they quite differ in the action expected from the }(hjhhhNhNubh)}(h``userfaultfd``h]h userfaultfd}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubht manager. In the former case, the virtual memory is removed, but the area is not, the area remains monitored by the }(hjhhhNhNubh)}(h``userfaultfd``h]h userfaultfd}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhXM, and if a page fault occurs in that area it will be delivered to the manager. The proper resolution for such page fault is to zeromap the faulting address. However, in the latter case, when an area is unmapped, either explicitly (with munmap() system call), or implicitly (e.g. during mremap()), the area is removed and in turn the }(hjhhhNhNubh)}(h``userfaultfd``h]h userfaultfd}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh context for such area disappears too and the manager will not get further userland page faults from the removed area. Still, the notification is required in order to prevent manager from using }(hjhhhNhNubh)}(h``UFFDIO_COPY``h]h UFFDIO_COPY}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh on the unmapped area.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj|hhubh)}(hXsUnlike userland page faults which have to be synchronous and require explicit or implicit wakeup, all the events are delivered asynchronously and the non-cooperative process resumes execution as soon as manager executes read(). The ``userfaultfd`` manager should carefully synchronize calls to ``UFFDIO_COPY`` with the events processing. To aid the synchronization, the ``UFFDIO_COPY`` ioctl will return ``-ENOSPC`` when the monitored process exits at the time of ``UFFDIO_COPY``, and ``-ENOENT``, when the non-cooperative process has changed its virtual memory layout simultaneously with outstanding ``UFFDIO_COPY`` operation.h](hUnlike userland page faults which have to be synchronous and require explicit or implicit wakeup, all the events are delivered asynchronously and the non-cooperative process resumes execution as soon as manager executes read(). The }(hjhhhNhNubh)}(h``userfaultfd``h]h userfaultfd}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh/ manager should carefully synchronize calls to }(hjhhhNhNubh)}(h``UFFDIO_COPY``h]h UFFDIO_COPY}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh= with the events processing. To aid the synchronization, the }(hjhhhNhNubh)}(h``UFFDIO_COPY``h]h UFFDIO_COPY}(hj.hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh ioctl will return }(hjhhhNhNubh)}(h ``-ENOSPC``h]h-ENOSPC}(hj@hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh1 when the monitored process exits at the time of }(hjhhhNhNubh)}(h``UFFDIO_COPY``h]h UFFDIO_COPY}(hjRhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh, and }(hjhhhNhNubh)}(h ``-ENOENT``h]h-ENOENT}(hjdhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhi, when the non-cooperative process has changed its virtual memory layout simultaneously with outstanding }(hjhhhNhNubh)}(h``UFFDIO_COPY``h]h UFFDIO_COPY}(hjvhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh operation.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj|hhubh)}(hXThe current asynchronous model of the event delivery is optimal for single threaded non-cooperative ``userfaultfd`` manager implementations. A synchronous event delivery model can be added later as a new ``userfaultfd`` feature to facilitate multithreading enhancements of the non cooperative manager, for example to allow ``UFFDIO_COPY`` ioctls to run in parallel to the event reception. Single threaded implementations should continue to use the current async event delivery model instead.h](hdThe current asynchronous model of the event delivery is optimal for single threaded non-cooperative }(hjhhhNhNubh)}(h``userfaultfd``h]h userfaultfd}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhY manager implementations. A synchronous event delivery model can be added later as a new }(hjhhhNhNubh)}(h``userfaultfd``h]h userfaultfd}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhh feature to facilitate multithreading enhancements of the non cooperative manager, for example to allow }(hjhhhNhNubh)}(h``UFFDIO_COPY``h]h UFFDIO_COPY}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh ioctls to run in parallel to the event reception. Single threaded implementations should continue to use the current async event delivery model instead.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj|hhubeh}(h]j1ah ]h"]non-cooperative userfaultfdah$]h&]uh1hhhhhhhhMm referencedKubeh}(h] userfaultfdah ]h"] userfaultfdah$]h&]uh1hhhhhhhhKubeh}(h]h ]h"]h$]h&]sourcehuh1hcurrent_sourceN current_lineNsettingsdocutils.frontendValues)}(hN generatorN datestampN source_linkN source_urlN toc_backlinksentryfootnote_backlinksK sectnum_xformKstrip_commentsNstrip_elements_with_classesN strip_classesN report_levelK halt_levelKexit_status_levelKdebugNwarning_streamN tracebackinput_encoding utf-8-siginput_encoding_error_handlerstrictoutput_encodingutf-8output_encoding_error_handlerjerror_encodingutf-8error_encoding_error_handlerbackslashreplace language_codeenrecord_dependenciesNconfigN id_prefixhauto_id_prefixid dump_settingsNdump_internalsNdump_transformsNdump_pseudo_xmlNexpose_internalsNstrict_visitorN_disable_configN_sourcehʌ _destinationN _config_files]7/var/lib/git/docbuild/linux/Documentation/docutils.confafile_insertion_enabled raw_enabledKline_length_limitM'pep_referencesN pep_base_urlhttps://peps.python.org/pep_file_url_templatepep-%04drfc_referencesN rfc_base_url&https://datatracker.ietf.org/doc/html/ tab_widthKtrim_footnote_reference_spacesyntax_highlightlong smart_quotessmartquotes_locales]character_level_inline_markupdoctitle_xform docinfo_xformKsectsubtitle_xform image_loadinglinkembed_stylesheetcloak_email_addressessection_self_linkenvNubreporterNindirect_targets]substitution_defs}substitution_names}refnames}non-cooperative userfaultfd]j asrefids}nameids}(jjjjj2j/jjjjjjjjj j j j jjjyjvjj1u nametypes}(jjj2jjjjj j jjyjuh}(jhjhj/jjj5jjFjjjjj jj j jj jvjj1j|u footnote_refs} citation_refs} autofootnotes]autofootnote_refs]symbol_footnotes]symbol_footnote_refs] footnotes] citations]autofootnote_startKsymbol_footnote_startK id_counter collectionsCounter}Rparse_messages]transform_messages] transformerN include_log] decorationNhhub.