€•\Œsphinx.addnodes”Œdocument”“”)�”}”(Œ rawsource”Œ”Œchildren”]”(Œ translations”Œ LanguagesNode”“”)�”}”(hhh]”(hŒ pending_xref”“”)�”}”(hhh]”Œdocutils.nodes”ŒText”“”ŒChinese (Simplified)”…”�”}”Œparent”hsbaŒ attributes”}”(Œids”]”Œclasses”]”Œnames”]”Œdupnames”]”Œbackrefs”]”Œ refdomain”Œstd”Œreftype”Œdoc”Œ reftarget”Œ./translations/zh_CN/admin-guide/mm/userfaultfd”Œmodname”NŒ classname”NŒ refexplicit”ˆuŒtagname”hhh ubh)�”}”(hhh]”hŒChinese (Traditional)”…”�”}”hh2sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ./translations/zh_TW/admin-guide/mm/userfaultfd”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒItalian”…”�”}”hhFsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ./translations/it_IT/admin-guide/mm/userfaultfd”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒJapanese”…”�”}”hhZsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ./translations/ja_JP/admin-guide/mm/userfaultfd”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒKorean”…”�”}”hhnsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ./translations/ko_KR/admin-guide/mm/userfaultfd”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒPortuguese (Brazilian)”…”�”}”hh‚sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ./translations/pt_BR/admin-guide/mm/userfaultfd”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒSpanish”…”�”}”hh–sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ./translations/sp_SP/admin-guide/mm/userfaultfd”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubeh}”(h]”h ]”h"]”h$]”h&]”Œcurrent_language”ŒEnglish”uh1h hhŒ _document”hŒsource”NŒline”NubhŒsection”“”)�”}”(hhh]”(hŒtitle”“”)�”}”(hŒ Userfaultfd”h]”hŒ Userfaultfd”…”�”}”(hh¼h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hºhh·h²hh³ŒH/var/lib/git/docbuild/linux/Documentation/admin-guide/mm/userfaultfd.rst”h´Kubh¶)�”}”(hhh]”(h»)�”}”(hŒ Objective”h]”hŒ Objective”…”�”}”(hhÎh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hºhhËh²hh³hÊh´KubhŒ paragraph”“”)�”}”(hŒÎUserfaults 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]”hŒÎUserfaults 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.”…”�”}”(hhÞh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´KhhËh²hubhÝ)�”}”(hŒjFor example userfaults allows a proper and more optimal implementation of the ``PROT_NONE+SIGSEGV`` trick.”h]”(hŒNFor example userfaults allows a proper and more optimal implementation of the ”…”�”}”(hhìh²hh³Nh´NubhŒliteral”“”)�”}”(hŒ``PROT_NONE+SIGSEGV``”h]”hŒPROT_NONE+SIGSEGV”…”�”}”(hhöh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhhìubhŒ trick.”…”�”}”(hhìh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K hhËh²hubeh}”(h]”Œ objective”ah ]”h"]”Œ objective”ah$]”h&]”uh1hµhh·h²hh³hÊh´Kubh¶)�”}”(hhh]”(h»)�”}”(hŒDesign”h]”hŒDesign”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hºhjh²hh³hÊh´KubhÝ)�”}”(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'h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´Khjh²hubhÝ)�”}”(hŒzThe ``userfaultfd`` (aside from registering and unregistering virtual memory ranges) provides two primary functionalities:”h]”(hŒThe ”…”�”}”(hj5h²hh³Nh´Nubhõ)�”}”(hŒ``userfaultfd``”h]”hŒ userfaultfd”…”�”}”(hj=h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj5ubhŒg (aside from registering and unregistering virtual memory ranges) provides two primary functionalities:”…”�”}”(hj5h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´Khjh²hubhŒenumerated_list”“”)�”}”(hhh]”(hŒ list_item”“”)�”}”(hŒM``read/POLLIN`` protocol to notify a userland thread of the faults happening ”h]”hÝ)�”}”(hŒL``read/POLLIN`` protocol to notify a userland thread of the faults happening”h]”(hõ)�”}”(hŒ``read/POLLIN``”h]”hŒ read/POLLIN”…”�”}”(hjdh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj`ubhŒ= protocol to notify a userland thread of the faults happening”…”�”}”(hj`h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´Khj\ubah}”(h]”h ]”h"]”h$]”h&]”uh1jZhjWh²hh³hÊh´Nubj[)�”}”(hŒévarious ``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Ý)�”}”(hŒèvarious ``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Œvarious ”…”�”}”(hj†h²hh³Nh´Nubhõ)�”}”(hŒ ``UFFDIO_*``”h]”hŒUFFDIO_*”…”�”}”(hjŽh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj†ubhŒE ioctls that can manage the virtual memory regions registered in the ”…”�”}”(hj†h²hh³Nh´Nubhõ)�”}”(hŒ``userfaultfd``”h]”hŒ userfaultfd”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj†ubhŒ€ that allows userland to efficiently resolve the userfaults it receives via 1) or to manage the virtual memory in the background”…”�”}”(hj†h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´Khj‚ubah}”(h]”h ]”h"]”h$]”h&]”uh1jZhjWh²hh³hÊh´Nubeh}”(h]”h ]”h"]”h$]”h&]”Œenumtype”Œarabic”Œprefix”hŒsuffix”Œ)”uh1jUhjh²hh³hÊh´KubhÝ)�”}”(hXÆThe 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]”(hŒÐThe 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 ”…”�”}”(hjÉh²hh³Nh´Nubhõ)�”}”(hŒ``userfaultfd``”h]”hŒ userfaultfd”…”�”}”(hjÑh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjÉubhŒç 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.”…”�”}”(hjÉh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K"hjh²hubhÝ)�”}”(hX³The ``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]”(hŒThe ”…”�”}”(hjéh²hh³Nh´Nubhõ)�”}”(hŒ``userfaultfd``”h]”hŒ userfaultfd”…”�”}”(hjñh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjéubhX, 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 ”…”�”}”(hjéh²hh³Nh´Nubhõ)�”}”(hŒ``userfaultfd``”h]”hŒ userfaultfd”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjéubhŒs themselves on the same region the manager is already tracking, which is a corner case that would currently return ”…”�”}”(hjéh²hh³Nh´Nubhõ)�”}”(hŒ ``-EBUSY``”h]”hŒ-EBUSY”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjéubhŒ).”…”�”}”(hjéh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K*hjh²hubeh}”(h]”Œdesign”ah ]”h"]”Œdesign”ah$]”h&]”uh1hµhh·h²hh³hÊh´Kubh¶)�”}”(hhh]”(h»)�”}”(hŒAPI”h]”hŒAPI”…”�”}”(hj8h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hºhj5h²hh³hÊh´K3ubh¶)�”}”(hhh]”(h»)�”}”(hŒCreating a userfaultfd”h]”hŒCreating a userfaultfd”…”�”}”(hjIh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hºhjFh²hh³hÊh´K6ubhÝ)�”}”(hŒèThere 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]”hŒèThere 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).”…”�”}”(hjWh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K8hjFh²hubhÝ)�”}”(hŒ‡The first way, supported since userfaultfd was introduced, is the userfaultfd(2) syscall. Access to this is controlled in several ways:”h]”hŒ‡The first way, supported since userfaultfd was introduced, is the userfaultfd(2) syscall. Access to this is controlled in several ways:”…”�”}”(hjeh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K atomically copies some existing page contents from userspace.”…”�”}”(hj¹h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K›hjµubah}”(h]”h ]”h"]”h$]”h&]”uh1jZhj²h²hh³hÊh´Nubj[)�”}”(hŒ3``UFFDIO_ZEROPAGE`` atomically zeros the new page. ”h]”hÝ)�”}”(hŒ2``UFFDIO_ZEROPAGE`` atomically zeros the new page.”h]”(hõ)�”}”(hŒ``UFFDIO_ZEROPAGE``”h]”hŒUFFDIO_ZEROPAGE”…”�”}”(hjãh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjßubhŒ atomically zeros the new page.”…”�”}”(hjßh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´KžhjÛubah}”(h]”h ]”h"]”h$]”h&]”uh1jZhj²h²hh³hÊh´Nubj[)�”}”(hŒA``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]”hŒUFFDIO_CONTINUE”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjubhŒ- maps an existing, previously-populated page.”…”�”}”(hjh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K hjubah}”(h]”h ]”h"]”h$]”h&]”uh1jZhj²h²hh³hÊh´Nubeh}”(h]”h ]”h"]”h$]”h&]”j®j¯uh1jsh³hÊh´K›hj“h²hubhÝ)�”}”(hŒªThese 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]”hŒªThese 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-h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K¢hj“h²hubhÝ)�”}”(hŒÇBy 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]”(hŒVBy default, these wake up userfaults blocked on the range in question. They support a ”…”�”}”(hj;h²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_*_MODE_DONTWAKE``”h]”hŒUFFDIO_*_MODE_DONTWAKE”…”�”}”(hjCh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj;ubhŒ ”…”�”}”(hj;h²hh³Nh´Nubhõ)�”}”(hŒ``mode``”h]”hŒmode”…”�”}”(hjUh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj;ubhŒN flag, which indicates that waking will be done separately at some later time.”…”�”}”(hj;h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K¦hj“h²hubhÝ)�”}”(hŒ`Which ioctl to choose depends on the kind of page fault, and what we'd like to do to resolve it:”h]”hŒbWhich ioctl to choose depends on the kind of page fault, and what we’d like to do to resolve it:”…”�”}”(hjmh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´Kªhj“h²hubjt)�”}”(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]”(hŒFor ”…”�”}”(hj‚h²hh³Nh´Nubhõ)�”}”(hŒ ``UFFDIO_REGISTER_MODE_MISSING``”h]”hŒUFFDIO_REGISTER_MODE_MISSING”…”�”}”(hjŠh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj‚ubhŒH faults, the fault needs to be resolved by either providing a new page (”…”�”}”(hj‚h²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_COPY``”h]”hŒ UFFDIO_COPY”…”�”}”(hjœh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj‚ubhŒ), or mapping the zero page (”…”�”}”(hj‚h²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_ZEROPAGE``”h]”hŒUFFDIO_ZEROPAGE”…”�”}”(hj®h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj‚ubhŒ«). 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.”…”�”}”(hj‚h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K­hj~ubah}”(h]”h ]”h"]”h$]”h&]”uh1jZhj{h²hh³hÊh´Nubj[)�”}”(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]”(hŒFor ”…”�”}”(hjÐh²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_REGISTER_MODE_MINOR``”h]”hŒUFFDIO_REGISTER_MODE_MINOR”…”�”}”(hjØh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjÐubhX 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 ”…”�”}”(hjÐh²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_CONTINUE``”h]”hŒUFFDIO_CONTINUE”…”�”}”(hjêh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjÐubhŒ.”…”�”}”(hjÐh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K³hjÌubah}”(h]”h ]”h"]”h$]”h&]”uh1jZhj{h²hh³hÊh´Nubeh}”(h]”h ]”h"]”h$]”h&]”j®j¯uh1jsh³hÊh´K­hj“h²hubhÝ)�”}”(hŒNotes:”h]”hŒNotes:”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K¹hj“h²hubjt)�”}”(hhh]”(j[)�”}”(hŒ–You can tell which kind of fault occurred by examining ``pagefault.flags`` within the ``uffd_msg``, checking for the ``UFFD_PAGEFAULT_FLAG_*`` flags. ”h]”hÝ)�”}”(hŒ•You can tell which kind of fault occurred by examining ``pagefault.flags`` within the ``uffd_msg``, checking for the ``UFFD_PAGEFAULT_FLAG_*`` flags.”h]”(hŒ7You can tell which kind of fault occurred by examining ”…”�”}”(hj#h²hh³Nh´Nubhõ)�”}”(hŒ``pagefault.flags``”h]”hŒpagefault.flags”…”�”}”(hj+h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj#ubhŒ within the ”…”�”}”(hj#h²hh³Nh´Nubhõ)�”}”(hŒ ``uffd_msg``”h]”hŒuffd_msg”…”�”}”(hj=h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj#ubhŒ, checking for the ”…”�”}”(hj#h²hh³Nh´Nubhõ)�”}”(hŒ``UFFD_PAGEFAULT_FLAG_*``”h]”hŒUFFD_PAGEFAULT_FLAG_*”…”�”}”(hjOh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj#ubhŒ flags.”…”�”}”(hj#h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K»hjubah}”(h]”h ]”h"]”h$]”h&]”uh1jZhjh²hh³hÊh´Nubj[)�”}”(hŒ¥None 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Ý)�”}”(hŒ¤None 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Œ¤None 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.”…”�”}”(hjqh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K¿hjmubah}”(h]”h ]”h"]”h$]”h&]”uh1jZhjh²hh³hÊh´Nubj[)�”}”(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.”…”�”}”(hj‰h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´KÃhj…ubah}”(h]”h ]”h"]”h$]”h&]”uh1jZhjh²hh³hÊh´Nubj[)�”}”(hŒˆBe sure to test for all errors including (``pollfd[0].revents & POLLERR``). This can happen, e.g. when ranges supplied were incorrect. ”h]”hÝ)�”}”(hŒ‡Be 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 (”…”�”}”(hj¡h²hh³Nh´Nubhõ)�”}”(hŒ``pollfd[0].revents & POLLERR``”h]”hŒpollfd[0].revents & POLLERR”…”�”}”(hj©h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj¡ubhŒ>). This can happen, e.g. when ranges supplied were incorrect.”…”�”}”(hj¡h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´KÉhj�ubah}”(h]”h ]”h"]”h$]”h&]”uh1jZhjh²hh³hÊh´Nubeh}”(h]”h ]”h"]”h$]”h&]”j®j¯uh1jsh³hÊh´K»hj“h²hubeh}”(h]”Œresolving-userfaults”ah ]”h"]”Œresolving userfaults”ah$]”h&]”uh1hµhj5h²hh³hÊh´K—ubh¶)�”}”(hhh]”(h»)�”}”(hŒWrite Protect Notifications”h]”hŒWrite Protect Notifications”…”�”}”(hjØh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hºhjÕh²hh³hÊh´KÎubhÝ)�”}”(hŒTThis is equivalent to (but faster than) using mprotect and a SIGSEGV signal handler.”h]”hŒTThis is equivalent to (but faster than) using mprotect and a SIGSEGV signal handler.”…”�”}”(hjæh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´KÐhjÕh²hubhÝ)�”}”(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 ”…”�”}”(hjôh²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_REGISTER_MODE_WP``”h]”hŒUFFDIO_REGISTER_MODE_WP”…”�”}”(hjüh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjôubhŒ'. Instead of using mprotect(2) you use ”…”�”}”(hjôh²hh³Nh´Nubhõ)�”}”(hŒA``ioctl(uffd, UFFDIO_WRITEPROTECT, struct *uffdio_writeprotect)``”h]”hŒ=ioctl(uffd, UFFDIO_WRITEPROTECT, struct *uffdio_writeprotect)”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjôubhŒ while ”…”�”}”(hjôh²hh³Nh´Nubhõ)�”}”(hŒ&``mode = UFFDIO_WRITEPROTECT_MODE_WP``”h]”hŒ"mode = UFFDIO_WRITEPROTECT_MODE_WP”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjôubhX  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 ”…”�”}”(hjôh²hh³Nh´Nubhõ)�”}”(hŒ4``msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WP``”h]”hŒ0msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WP”…”�”}”(hj2 h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjôubhŒ set. Now you send ”…”�”}”(hjôh²hh³Nh´Nubhõ)�”}”(hŒA``ioctl(uffd, UFFDIO_WRITEPROTECT, struct *uffdio_writeprotect)``”h]”hŒ=ioctl(uffd, UFFDIO_WRITEPROTECT, struct *uffdio_writeprotect)”…”�”}”(hjD h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjôubhŒ again while ”…”�”}”(hjôh²hh³Nh´Nubhõ)�”}”(hŒ``pagefault.mode``”h]”hŒpagefault.mode”…”�”}”(hjV h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjôubhŒ does not have ”…”�”}”(hjôh²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_WRITEPROTECT_MODE_WP``”h]”hŒUFFDIO_WRITEPROTECT_MODE_WP”…”�”}”(hjh h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjôubhŒ© 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.”…”�”}”(hjôh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´KÓhjÕh²hubhÝ)�”}”(hXèIf 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]”(hŒIf you registered with both ”…”�”}”(hj€ h²hh³Nh´Nubhõ)�”}”(hŒ ``UFFDIO_REGISTER_MODE_MISSING``”h]”hŒUFFDIO_REGISTER_MODE_MISSING”…”�”}”(hjˆ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj€ ubhŒ and ”…”�”}”(hj€ h²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_REGISTER_MODE_WP``”h]”hŒUFFDIO_REGISTER_MODE_WP”…”�”}”(hjš h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj€ 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€ h²hh³Nh´Nubhõ)�”}”(hŒ``UFFD_PAGEFAULT_FLAG_WP``”h]”hŒUFFD_PAGEFAULT_FLAG_WP”…”�”}”(hj¬ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj€ ubhŒ set, the latter ”…”�”}”(hj€ h²hh³Nh´Nubhõ)�”}”(hŒ``UFFD_PAGEFAULT_FLAG_WRITE``”h]”hŒUFFD_PAGEFAULT_FLAG_WRITE”…”�”}”(hj¾ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj€ ubhŒR. The latter did not fail on protection but you still need to supply a page when ”…”�”}”(hj€ h²hh³Nh´Nubhõ)�”}”(hŒ ``UFFDIO_REGISTER_MODE_MISSING``”h]”hŒUFFDIO_REGISTER_MODE_MISSING”…”�”}”(hjÐ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj€ ubhŒ was used.”…”�”}”(hj€ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´KâhjÕh²hubhÝ)�”}”(hŒ†Userfaultfd write-protect mode currently behave differently on none ptes (when e.g. page is missing) over different types of memories.”h]”hŒ†Userfaultfd write-protect mode currently behave differently on none ptes (when e.g. page is missing) over different types of memories.”…”�”}”(hjè h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´KëhjÕh²hubhÝ)�”}”(hXêFor 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]”(hŒFor anonymous memory, ”…”�”}”(hjö h²hh³Nh´Nubhõ)�”}”(hŒ``ioctl(UFFDIO_WRITEPROTECT)``”h]”hŒioctl(UFFDIO_WRITEPROTECT)”…”�”}”(hjþ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjö 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ö h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´KîhjÕh²hubhÝ)�”}”(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 h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´KöhjÕh²hubhÝ)�”}”(hXçWhen 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$ h²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_REGISTER_MODE_WP``”h]”hŒUFFDIO_REGISTER_MODE_WP”…”�”}”(hj, h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj$ ubhŒ in combination with either ”…”�”}”(hj$ h²hh³Nh´Nubhõ)�”}”(hŒ ``UFFDIO_REGISTER_MODE_MISSING``”h]”hŒUFFDIO_REGISTER_MODE_MISSING”…”�”}”(hj> h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj$ ubhŒ or ”…”�”}”(hj$ h²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_REGISTER_MODE_MINOR``”h]”hŒUFFDIO_REGISTER_MODE_MINOR”…”�”}”(hjP h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj$ ubhŒ-, when resolving missing / minor faults with ”…”�”}”(hj$ h²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_COPY``”h]”hŒ UFFDIO_COPY”…”�”}”(hjb h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj$ ubhŒ or ”…”�”}”hj$ sbhõ)�”}”(hŒ``UFFDIO_CONTINUE``”h]”hŒUFFDIO_CONTINUE”…”�”}”(hjt h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj$ 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$ h²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_COPY_MODE_WP``”h]”hŒUFFDIO_COPY_MODE_WP”…”�”}”(hj† h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj$ ubhŒ or ”…”�”}”hj$ sbhõ)�”}”(hŒ``UFFDIO_CONTINUE_MODE_WP``”h]”hŒUFFDIO_CONTINUE_MODE_WP”…”�”}”(hj˜ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj$ ubhŒ1 respectively) to configure the mapping this way.”…”�”}”(hj$ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´KühjÕh²hubhÝ)�”}”(hŒ­If 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]”(hŒIf the userfaultfd context has ”…”�”}”(hj° h²hh³Nh´Nubhõ)�”}”(hŒ``UFFD_FEATURE_WP_ASYNC``”h]”hŒUFFD_FEATURE_WP_ASYNC”…”�”}”(hj¸ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj° ubhŒu feature bit set, any vma registered with write-protection will work in async mode rather than the default sync mode.”…”�”}”(hj° h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MhjÕh²hubhÝ)�”}”(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Ð h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MhjÕh²hubhŒ 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[)�”}”(hŒvThe dirty result will not be affected by vma changes (e.g. vma merging) because the dirty is only tracked by the pte. ”h]”hÝ)�”}”(hŒuThe dirty result will not be affected by vma changes (e.g. vma merging) because the dirty is only tracked by the pte.”h]”hŒuThe dirty result will not be affected by vma changes (e.g. vma merging) because the dirty is only tracked by the pte.”…”�”}”(hjë h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M hjç ubah}”(h]”h ]”h"]”h$]”h&]”uh1jZhjä ubj[)�”}”(hŒtIt supports range operations by default, so one can enable tracking on any range of memory as long as page aligned. ”h]”hÝ)�”}”(hŒsIt supports range operations by default, so one can enable tracking on any range of memory as long as page aligned.”h]”hŒsIt supports range operations by default, so one can enable tracking on any range of memory as long as page aligned.”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´Mhjÿ ubah}”(h]”h ]”h"]”h$]”h&]”uh1jZhjä ubj[)�”}”(hŒ‡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). ”h]”hÝ)�”}”(hŒ†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).”h]”hŒ†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).”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´Mhj 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]”(hŒ¾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: ”…”�”}”(hj3 h²hh³Nh´Nubhõ)�”}”(hŒ``MADV_DONTNEED``”h]”hŒ MADV_DONTNEED”…”�”}”(hj; h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj3 ubhŒ on anonymous (or ”…”�”}”(hj3 h²hh³Nh´Nubhõ)�”}”(hŒ``MADV_REMOVE``”h]”hŒ MADV_REMOVE”…”�”}”(hjM h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj3 ubhŒh on a file mapping) will be treated as dirtying of memory by dropping the uffd bit during the procedure.”…”�”}”(hj3 h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´Mhj/ ubah}”(h]”h ]”h"]”h$]”h&]”uh1jZhjä ubeh}”(h]”h ]”h"]”h$]”h&]”j®j¯uh1jsh³hÊh´M hjà ubah}”(h]”h ]”h"]”h$]”h&]”uh1jÞ h³hÊh´M hjÕh²hubhÝ)�”}”(hŒThe user app can collect the "written/dirty" status by looking up the uffd bit for the pages being interested in /proc/pagemap.”h]”hŒƒThe user app can collect the “written/dirtyâ€� status by looking up the uffd bit for the pages being interested in /proc/pagemap.”…”�”}”(hjw h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MhjÕh²hubhÝ)�”}”(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]”(hŒnThe page will not be under track of userfaultfd-wp async mode until the page is explicitly write-protected by ”…”�”}”(hj… h²hh³Nh´Nubhõ)�”}”(hŒ``ioctl(UFFDIO_WRITEPROTECT)``”h]”hŒioctl(UFFDIO_WRITEPROTECT)”…”�”}”(hj� h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj… ubhŒ with the mode flag ”…”�”}”(hj… h²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_WRITEPROTECT_MODE_WP``”h]”hŒUFFDIO_WRITEPROTECT_MODE_WP”…”�”}”(hjŸ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj… ubhŒ_ set. Trying to resolve a page fault that was tracked by async mode userfaultfd-wp is invalid.”…”�”}”(hj… h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MhjÕh²hubhÝ)�”}”(hŒWWhen userfaultfd-wp async mode is used alone, it can be applied to all kinds of memory.”h]”hŒWWhen userfaultfd-wp async mode is used alone, it can be applied to all kinds of memory.”…”�”}”(hj· h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M$hjÕh²hubeh}”(h]”Œwrite-protect-notifications”ah ]”h"]”Œwrite protect notifications”ah$]”h&]”uh1hµhj5h²hh³hÊh´KÎubh¶)�”}”(hhh]”(h»)�”}”(hŒMemory Poisioning Emulation”h]”hŒMemory Poisioning Emulation”…”�”}”(hjÐ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hºhjÍ h²hh³hÊh´M(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]”(hŒqIn response to a fault (either missing or minor), an action userspace can take to “resolveâ€� it is to issue a ”…”�”}”(hjÞ h²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_POISON``”h]”hŒ UFFDIO_POISON”…”�”}”(hjæ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjÞ 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Þ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M*hjÍ h²hubhÝ)�”}”(hX¬This 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]”hX°This 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þ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M/hjÍ h²hubeh}”(h]”Œmemory-poisioning-emulation”ah ]”h"]”Œmemory poisioning emulation”ah$]”h&]”uh1hµhj5h²hh³hÊh´M(ubh¶)�”}”(hhh]”(h»)�”}”(hŒRead-Write Protection”h]”hŒRead-Write Protection”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hºhj h²hh³hÊh´M7ubhÝ)�”}”(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]”hŒUFFDIO_REGISTER_MODE_RWP”…”�”}”(hj) h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj% ubhŒ^ enables read-write protection tracking on a memory range. It is similar to (but faster than) ”…”�”}”(hj% h²hh³Nh´Nubhõ)�”}”(hŒ``mprotect(PROT_NONE)``”h]”hŒmprotect(PROT_NONE)”…”�”}”(hj; h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj% ubhŒ( combined with a signal handler; unlike ”…”�”}”(hj% h²hh³Nh´Nubhõ)�”}”(hŒ``mprotect(PROT_NONE)``”h]”hŒmprotect(PROT_NONE)”…”�”}”(hjM h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj% ubhŒ, RWP only traps accesses to ”…”�”}”(hj% h²hh³Nh´NubhŒemphasis”“”)�”}”(hŒ *present*”h]”hŒpresent”…”�”}”(hja h²hh³Nh´Nubah}”(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% h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M9hj h²hubhÝ)�”}”(hŒ�RWP 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]”hŒ�RWP 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 h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MBhj h²hubhÝ)�”}”(hŒ **Setup:**”h]”hŒstrong”“”)�”}”(hj‰ h]”hŒSetup:”…”�”}”(hj� h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j‹ hj‡ ubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MEhj h²hubjV)�”}”(hhh]”(j[)�”}”(hŒÍOpen 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Ý)�”}”(hŒÌOpen 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ŒOpen a userfaultfd and enable ”…”�”}”(hj§ h²hh³Nh´Nubhõ)�”}”(hŒ``UFFD_FEATURE_RWP``”h]”hŒUFFD_FEATURE_RWP”…”�”}”(hj¯ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj§ ubhŒ via ”…”�”}”(hj§ h²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_API``”h]”hŒ UFFDIO_API”…”�”}”(hjÁ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj§ ubhŒ. Optionally request ”…”�”}”(hj§ h²hh³Nh´Nubhõ)�”}”(hŒ``UFFD_FEATURE_RWP_ASYNC``”h]”hŒUFFD_FEATURE_RWP_ASYNC”…”�”}”(hjÓ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj§ ubhŒ as well — it requires ”…”�”}”(hj§ h²hh³Nh´Nubhõ)�”}”(hŒ``UFFD_FEATURE_RWP``”h]”hŒUFFD_FEATURE_RWP”…”�”}”(hjå h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj§ ubhŒ to be set in the same ”…”�”}”(hj§ h²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_API``”h]”hŒ UFFDIO_API”…”�”}”(hj÷ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj§ ubhŒ call.”…”�”}”(hj§ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MGhj£ ubah}”(h]”h ]”h"]”h$]”h&]”uh1jZhj  h²hh³hÊh´Nubj[)�”}”(hŒ¥Register 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Ý)�”}”(hŒ¤Register 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 h²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_REGISTER_MODE_RWP``”h]”hŒUFFDIO_REGISTER_MODE_RWP”…”�”}”(hj! h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj ubhŒ (and ”…”�”}”(hj h²hh³Nh´Nubhõ)�”}”(hŒ ``UFFDIO_REGISTER_MODE_MISSING``”h]”hŒUFFDIO_REGISTER_MODE_MISSING”…”�”}”(hj3 h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj ubhŒ= if evicted pages will need to be fetched back from storage).”…”�”}”(hj h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MKhj ubah}”(h]”h ]”h"]”h$]”h&]”uh1jZhj  h²hh³hÊh´Nubeh}”(h]”h ]”h"]”h$]”h&]”jÄjÅjÆhjÇŒ.”uh1jUhj h²hh³hÊh´MGubhÝ)�”}”(hŒ**Feature availability:**”h]”jŒ )�”}”(hjZ h]”hŒFeature availability:”…”�”}”(hj\ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j‹ hjX ubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MOhj h²hubhÝ)�”}”(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]”(hŒTRWP is built on top of two kernel primitives: a spare PTE bit owned by userfaultfd (”…”�”}”(hjo h²hh³Nh´Nubhõ)�”}”(hŒ#``CONFIG_HAVE_ARCH_USERFAULTFD_WP``”h]”hŒCONFIG_HAVE_ARCH_USERFAULTFD_WP”…”�”}”(hjw h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjo ubhŒ>) and architecture support for present-but-inaccessible PTEs (”…”�”}”(hjo h²hh³Nh´Nubhõ)�”}”(hŒ ``CONFIG_ARCH_HAS_PTE_PROTNONE``”h]”hŒCONFIG_ARCH_HAS_PTE_PROTNONE”…”�”}”(hj‰ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjo ubhŒA). When both are available on a 64-bit kernel, the build selects ”…”�”}”(hjo h²hh³Nh´Nubhõ)�”}”(hŒ``CONFIG_USERFAULTFD_RWP=y``”h]”hŒCONFIG_USERFAULTFD_RWP=y”…”�”}”(hj› h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjo ubhŒ and the ”…”�”}”(hjo h²hh³Nh´Nubhõ)�”}”(hŒ``VM_UFFD_RWP``”h]”hŒ VM_UFFD_RWP”…”�”}”(hj­ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjo ubhŒ VMA flag becomes available.”…”�”}”(hjo h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MQhj h²hubhÝ)�”}”(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]”hŒUFFD_FEATURE_RWP”…”�”}”(hjÉ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjÅ ubhŒ and ”…”�”}”(hjÅ h²hh³Nh´Nubhõ)�”}”(hŒ``UFFD_FEATURE_RWP_ASYNC``”h]”hŒUFFD_FEATURE_RWP_ASYNC”…”�”}”(hjÛ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjÅ ubhŒu are unavailable when the running kernel or architecture does not support them — for example 32-bit kernels (where ”…”�”}”(hjÅ h²hh³Nh´Nubhõ)�”}”(hŒ``VM_UFFD_RWP``”h]”hŒ VM_UFFD_RWP”…”�”}”(hjí h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjÅ ubhŒ( is unavailable), kernels built without ”…”�”}”(hjÅ h²hh³Nh´Nubhõ)�”}”(hŒ``CONFIG_USERFAULTFD_RWP``”h]”hŒCONFIG_USERFAULTFD_RWP”…”�”}”(hjÿ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjÅ ubhŒ\, and architectures whose ptes cannot carry the uffd bit at runtime (e.g. riscv without the ”…”�”•úÙ}”(hjÅ h²hh³Nh´Nubhõ)�”}”(hŒ``SVRSW60T59B``”h]”hŒ SVRSW60T59B”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjÅ ubhŒ2 extension). Requesting an unsupported feature in ”…”�”}”(hjÅ h²hh³Nh´Nubhõ)�”}”(hŒ``uffdio_api.features``”h]”hŒuffdio_api.features”…”�”}”(hj#h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjÅ ubhŒ makes ”…”�”}”(hjÅ h²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_API``”h]”hŒ UFFDIO_API”…”�”}”(hj5h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjÅ ubhŒ fail with ”…”�”}”(hjÅ h²hh³Nh´Nubhõ)�”}”(hŒ ``EINVAL``”h]”hŒEINVAL”…”�”}”(hjGh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjÅ 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Å h²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_API``”h]”hŒ UFFDIO_API”…”�”}”(hjYh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjÅ ubhŒ once with ”…”�”}”(hjÅ h²hh³Nh´Nubhõ)�”}”(hŒ``features = 0``”h]”hŒ features = 0”…”�”}”(hjkh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjÅ ubhŒO, inspect the returned bitmask, close that fd, then open the real one and call ”…”�”}”(hjÅ h²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_API``”h]”hŒ UFFDIO_API”…”�”}”(hj}h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjÅ ubhŒ, again with only the supported features set.”…”�”}”(hjÅ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MXhj h²hubhÝ)�”}”(hŒ **Protecting and Unprotecting:**”h]”jŒ )�”}”(hj—h]”hŒProtecting and Unprotecting:”…”�”}”(hj™h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j‹ hj•ubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´Mfhj h²hubhÝ)�”}”(hŒkUse ``UFFDIO_RWPROTECT`` to protect or unprotect a range, mirroring the ``UFFDIO_WRITEPROTECT`` interface::”h]”(hŒUse ”…”�”}”(hj¬h²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_RWPROTECT``”h]”hŒUFFDIO_RWPROTECT”…”�”}”(hj´h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj¬ubhŒ0 to protect or unprotect a range, mirroring the ”…”�”}”(hj¬h²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_WRITEPROTECT``”h]”hŒUFFDIO_WRITEPROTECT”…”�”}”(hjÆh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj¬ubhŒ interface:”…”�”}”(hj¬h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´Mhhj h²hubhŒ literal_block”“”)�”}”(hŒ©struct uffdio_rwprotect rwp = { .range = { .start = addr, .len = len }, .mode = UFFDIO_RWPROTECT_MODE_RWP, /* protect */ }; ioctl(uffd, UFFDIO_RWPROTECT, &rwp);”h]”hŒ©struct uffdio_rwprotect rwp = { .range = { .start = addr, .len = len }, .mode = UFFDIO_RWPROTECT_MODE_RWP, /* protect */ }; ioctl(uffd, UFFDIO_RWPROTECT, &rwp);”…”�”}”hjàsbah}”(h]”h ]”h"]”h$]”h&]”Œ xml:space”Œpreserve”uh1jÞh³hÊh´Mkhj h²hubhÝ)�”}”(hŒ·Setting ``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]”(hŒSetting ”…”�”}”(hjðh²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_RWPROTECT_MODE_RWP``”h]”hŒUFFDIO_RWPROTECT_MODE_RWP”…”�”}”(hjøh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjðubhŒ’ sets PROT_NONE on present PTEs in the range. Pages stay resident and their physical frames are preserved — only access permissions are removed.”…”�”}”(hjðh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´Mqhj h²hubhÝ)�”}”(hŒ™Clearing ``UFFDIO_RWPROTECT_MODE_RWP`` restores normal VMA permissions and wakes any faulting threads (unless ``UFFDIO_RWPROTECT_MODE_DONTWAKE`` is set).”h]”(hŒ Clearing ”…”�”}”(hjh²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_RWPROTECT_MODE_RWP``”h]”hŒUFFDIO_RWPROTECT_MODE_RWP”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjubhŒH restores normal VMA permissions and wakes any faulting threads (unless ”…”�”}”(hjh²hh³Nh´Nubhõ)�”}”(hŒ"``UFFDIO_RWPROTECT_MODE_DONTWAKE``”h]”hŒUFFDIO_RWPROTECT_MODE_DONTWAKE”…”�”}”(hj*h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjubhŒ is set).”…”�”}”(hjh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´Muhj h²hubhÝ)�”}”(hŒ**Scope of protection:**”h]”jŒ )�”}”(hjDh]”hŒScope of protection:”…”�”}”(hjFh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j‹ hjBubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´Mxhj h²hubhÝ)�”}”(hX¼RWP 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 ”…”�”}”(hjYh²hh³Nh´Nubj` )�”}”(hŒ *present*”h]”hŒpresent”…”�”}”(hjah²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j_ hjYubhŒ PTEs. ”…”�”}”(hjYh²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_RWPROTECT``”h]”hŒUFFDIO_RWPROTECT”…”�”}”(hjsh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjYubhŒ­ only affects entries that are already populated. Unpopulated addresses within the range remain unpopulated; when first accessed they fault through the normal missing path (”…”�”}”(hjYh²hh³Nh´Nubhõ)�”}”(hŒ``do_anonymous_page()``”h]”hŒdo_anonymous_page()”…”�”}”(hj…h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjYubhŒ, ”…”�”}”(hjYh²hh³Nh´Nubhõ)�”}”(hŒ``do_swap_page()``”h]”hŒdo_swap_page()”…”�”}”(hj—h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjYubhŒ, ”…”�”}”(hjYh²hh³Nh´Nubhõ)�”}”(hŒ``finish_fault()``”h]”hŒfinish_fault()”…”�”}”(hj©h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjYubhŒk) and the resulting PTE is not RWP-protected. To observe the population itself, co-register the range with ”…”�”}”(hjYh²hh³Nh´Nubhõ)�”}”(hŒ ``UFFDIO_REGISTER_MODE_MISSING``”h]”hŒUFFDIO_REGISTER_MODE_MISSING”…”�”}”(hj»h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjYubhŒ.”…”�”}”(hjYh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´Mzhj h²hubhÝ)�”}”(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.”…”�”}”(hjÓh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M‚hj h²hubhÝ)�”}”(hX¨Operations 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 — ”…”�”}”(hjáh²hh³Nh´Nubhõ)�”}”(hŒ``MADV_DONTNEED``”h]”hŒ MADV_DONTNEED”…”�”}”(hjéh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjáubhŒ¿ 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 ”…”�”}”(hjáh²hh³Nh´Nubhõ)�”}”(hŒ``PTE_MARKER_UFFD_WP``”h]”hŒPTE_MARKER_UFFD_WP”…”�”}”(hjûh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjáubhŒT), there is no persistent RWP marker today. The user needs to re-arm the range with ”…”�”}”(hjáh²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_RWPROTECT``”h]”hŒUFFDIO_RWPROTECT”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjáubhŒ0 after any operation that explicitly frees PTEs.”…”�”}”(hjáh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M‡hj h²hubhÝ)�”}”(hŒ**Fault Handling:**”h]”jŒ )�”}”(hj'h]”hŒFault Handling:”…”�”}”(hj)h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j‹ hj%ubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MŽhj h²hubhÝ)�”}”(hŒ"When a protected page is accessed:”h]”hŒ"When a protected page is accessed:”…”�”}”(hj<h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M�hj h²hubjt)�”}”(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”…”�”}”(hjUh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j‹ hjQubhŒ- (default): The faulting thread blocks and a ”…”�”}”(hjQh²hh³Nh´Nubhõ)�”}”(hŒ``UFFD_PAGEFAULT_FLAG_RWP``”h]”hŒUFFD_PAGEFAULT_FLAG_RWP”…”�”}”(hjgh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjQubhŒV message is delivered to the userfaultfd handler. The handler resolves the fault with ”…”�”}”(hjQh²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_RWPROTECT``”h]”hŒUFFDIO_RWPROTECT”…”�”}”(hjyh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjQubhŒ (clearing ”…”�”}”(hjQh²hh³Nh´Nubhõ)�”}”(hŒ ``MODE_RWP``”h]”hŒMODE_RWP”…”�”}”(hj‹h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjQubhŒD), which restores the PTE permissions and wakes the faulting thread.”…”�”}”(hjQh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M’hjMubah}”(h]”h ]”h"]”h$]”h&]”uh1jZhjJh²hh³hÊh´Nubj[)�”}”(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”…”�”}”(hj±h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j‹ hj­ubhŒ (”…”�”}”(hj­h²hh³Nh´Nubhõ)�”}”(hŒ``UFFD_FEATURE_RWP_ASYNC``”h]”hŒUFFD_FEATURE_RWP_ASYNC”…”�”}”(hjÃh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj­ubhŒ‡): The kernel automatically restores PTE permissions and the thread continues without blocking. No message is delivered to the handler.”…”�”}”(hj­h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M˜hj©ubah}”(h]”h ]”h"]”h$]”h&]”uh1jZhjJh²hh³hÊh´Nubeh}”(h]”h ]”h"]”h$]”h&]”j®j¯uh1jsh³hÊh´M’hj h²hubhÝ)�”}”(hŒ**Runtime Mode Switching:**”h]”jŒ )�”}”(hjéh]”hŒRuntime Mode Switching:”…”�”}”(hjëh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j‹ hjçubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´Mœhj h²hubhÝ)�”}”(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]”hŒUFFDIO_SET_MODE”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjþubhŒ toggles ”…”�”}”(hjþh²hh³Nh´Nubhõ)�”}”(hŒ``UFFD_FEATURE_RWP_ASYNC``”h]”hŒUFFD_FEATURE_RWP_ASYNC”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjþubhŒŒ at runtime, allowing the VMM to switch between lightweight async detection and safe sync eviction without re-registering. The toggle takes ”…”�”}”(hjþh²hh³Nh´Nubhõ)�”}”(hŒ``mmap_write_lock()``”h]”hŒmmap_write_lock()”…”�”}”(hj&h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjþubhŒ and calls ”…”�”}”(hjþh²hh³Nh´Nubhõ)�”}”(hŒ``vma_start_write()``”h]”hŒvma_start_write()”…”�”}”(hj8h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjþubhŒc on each UFFD-armed VMA, draining in-flight per-VMA-locked faults before the new mode takes effect.”…”�”}”(hjþh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´Mžhj h²hubhÝ)�”}”(hŒ,**Working-set detection with PAGEMAP_SCAN:**”h]”jŒ )�”}”(hjRh]”hŒ(Working-set detection with PAGEMAP_SCAN:”…”�”}”(hjTh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j‹ hjPubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M¤hj h²hubhÝ)�”}”(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]”(hŒjRWP-protected PTEs carry the uffd PTE bit; an access (and, in async mode, its auto-resolution) clears it. ”…”�”}”(hjgh²hh³Nh´Nubhõ)�”}”(hŒ``PAGEMAP_SCAN``”h]”hŒ PAGEMAP_SCAN”…”�”}”(hjoh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjgubhŒ reports ”…”�”}”(hjgh²hh³Nh´Nubhõ)�”}”(hŒ``PAGE_IS_ACCESSED``”h]”hŒPAGE_IS_ACCESSED”…”�”}”(hj�h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjgubhŒ once the bit is clear on a ”…”�”}”(hjgh²hh³Nh´Nubhõ)�”}”(hŒ``VM_UFFD_RWP``”h]”hŒ VM_UFFD_RWP”…”�”}”(hj“h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjgubhŒ VMA, so a ”…”�”}”(hjgh²hh³Nh´Nubj` )�”}”(hŒ*non-inverted*”h]”hŒ non-inverted”…”�”}”(hj¥h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j_ hjgubhŒM scan reports the pages that were touched during the interval -- the hot set:”…”�”}”(hjgh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M¦hj h²hubjß)�”}”(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);”…”�”}”hj½sbah}”(h]”h ]”h"]”h$]”h&]”jîjïuh1jÞh³hÊh´M«hj h²hubhÝ)�”}”(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 ”…”�”}”(hjËh²hh³Nh´Nubhõ)�”}”(hŒ``page_region``”h]”hŒ page_region”…”�”}”(hjÓh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjËubhŒ array lists the hot ranges. ”…”�”}”(hjËh²hh³Nh´Nubhõ)�”}”(hŒ``PAGE_IS_ACCESSED``”h]”hŒPAGE_IS_ACCESSED”…”�”}”(hjåh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjËubhŒp is set on an accessed page whether it is still present or has since been swapped out, so the hot scan needs no ”…”�”}”(hjËh²hh³Nh´Nubhõ)�”}”(hŒ``PAGE_IS_PRESENT``”h]”hŒPAGE_IS_PRESENT”…”�”}”(hj÷h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjËubhŒM filter -- unpopulated holes carry neither bit and are excluded on their own.”…”�”}”(hjËh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M¶hj h²hubhÝ)�”}”(hX¸Track 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]”(hŒaTrack the hot set and reclaim everything else from the backing file (see the workflow below). Do ”…”�”}”(hjh²hh³Nh´NubjŒ )�”}”(hŒ**not**”h]”hŒnot”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j‹ hjubhŒ[ invert the scan to enumerate “coldâ€� pages directly: an inverted scan reports only the ”…”�”}”(hjh²hh³Nh´Nubhõ)�”}”(hŒ``VM_UFFD_RWP``”h]”hŒ VM_UFFD_RWP”…”�”}”(hj)h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjubhŒ= PTEs that are still protected, i.e. the resident portion of ”…”�”}”(hjh²hh³Nh´Nubj` )�”}”(hŒ*this*”h]”hŒthis”…”�”}”(hj;h²hh³Nh´Nubah}”(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 ”…”�”}”(hjh²hh³Nh´Nubhõ)�”}”(hŒ ``pte_none``”h]”hŒpte_none”…”�”}”(hjMh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjubhŒò 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 ”…”�”}”(hjh²hh³Nh´Nubj` )�”}”(hŒ*first*”h]”hŒfirst”…”�”}”(hj_h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j_ hjubhŒf access to a cached but unmapped page (e.g. pre-populated content) as hot, co-register the range with ”…”�”}”(hjh²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_REGISTER_MODE_MINOR``”h]”hŒUFFDIO_REGISTER_MODE_MINOR”…”�”}”(hjqh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjubhŒP: such accesses then fault as minor faults instead of mapping the page silently.”…”�”}”(hjh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M»hj h²hubhÝ)�”}”(hŒ **Cleanup:**”h]”jŒ )�”}”(hj‹h]”hŒCleanup:”…”�”}”(hj�h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j‹ hj‰ubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MÉhj h²hubhÝ)�”}”(hŒÇWhen 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]”hŒÇWhen 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.”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MËhj h²hubhÝ)�”}”(hŒ&**VMM Working Set Tracking Workflow:**”h]”jŒ )�”}”(hj°h]”hŒ"VMM Working Set Tracking Workflow:”…”�”}”(hj²h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j‹ hj®ubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MÏhj h²hubhÝ)�”}”(hXƒA 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]”(hŒA typical VMM lifecycle for cold page eviction to tiered storage. Two mappings of the same shmem (or hugetlbfs) file are used: ”…”�”}”(hjÅh²hh³Nh´Nubhõ)�”}”(hŒ ``guest_mem``”h]”hŒ guest_mem”…”�”}”(hjÍh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjÅubhŒ> is the RWP-registered mapping that vCPUs access through, and ”…”�”}”(hjÅh²hh³Nh´Nubhõ)�”}”(hŒ ``io_mem``”h]”hŒio_mem”…”�”}”(hjßh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjÅubhŒ0 is a private mapping for VMM-side I/O. Reading ”…”�”}”(hjÅh²hh³Nh´Nubhõ)�”}”(hŒ ``io_mem``”h]”hŒio_mem”…”�”}”(hjñh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjÅubhŒ/ does not go through the RWP-protected PTEs of ”…”�”}”(hjÅh²hh³Nh´Nubhõ)�”}”(hŒ ``guest_mem``”h]”hŒ guest_mem”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjÅubhŒ, so the VMM’s own ”…”�”}”(hjÅh²hh³Nh´Nubhõ)�”}”(hŒ ``pwrite()``”h]”hŒpwrite()”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjÅubhŒ never traps on its own”…”�”}”(hjÅh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MÑhj h²hubjß)�”}”(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&]”jîjïuh1jÞh³hÊh´MØhj h²hubhÝ)�”}”(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;h²hh³Nh´Nubhõ)�”}”(hŒ ``guest_mem``”h]”hŒ guest_mem”…”�”}”(hjCh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj;ubhŒ$ offset being evicted blocks with a ”…”�”}”(hj;h²hh³Nh´Nubhõ)�”}”(hŒ``UFFD_PAGEFAULT_FLAG_RWP``”h]”hŒUFFD_PAGEFAULT_FLAG_RWP”…”�”}”(hjUh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj;ubhŒ0 fault while the eviction is in progress. After ”…”�”}”(hj;h²hh³Nh´Nubhõ)�”}”(hŒ``fallocate()``”h]”hŒ fallocate()”…”�”}”(hjgh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj;ubhŒ punches the page out and ”…”�”}”(hj;h²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_WAKE``”h]”hŒ UFFDIO_WAKE”…”�”}”(hjyh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj;ubhŒ/ fires, the vCPU retries the access, faults as ”…”�”}”(hj;h²hh³Nh´Nubhõ)�”}”(hŒ ``MISSING``”h]”hŒMISSING”…”�”}”(hj‹h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj;ubhŒ#, and the handler resolves it with ”…”�”}”(hj;h²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_COPY``”h]”hŒ UFFDIO_COPY”…”�”}”(hj�h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj;ubhŒ from storage.”…”�”}”(hj;h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M"hj h²hubhÝ)�”}”(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]”(hŒBThis workflow targets shmem and hugetlbfs (both support a private ”…”�”}”(hjµh²hh³Nh´Nubhõ)�”}”(hŒ ``io_mem``”h]”hŒio_mem”…”�”}”(hj½h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjµubhŒ· 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.”…”�”}”(hjµh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M(hj h²hubeh}”(h]”Œread-write-protection”ah ]”h"]”Œread-write protection”ah$]”h&]”uh1hµhj5h²hh³hÊh´M7ubeh}”(h]”Œapi”ah ]”h"]”Œapi”ah$]”h&]”uh1hµhh·h²hh³hÊh´K3ubh¶)�”}”(hhh]”(h»)�”}”(hŒQEMU/KVM”h]”hŒQEMU/KVM”…”�”}”(hjèh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hºhjåh²hh³hÊh´M.ubhÝ)�”}”(hX£QEMU/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]”(hŒQEMU/KVM is using the ”…”�”}”(hjöh²hh³Nh´Nubhõ)�”}”(hŒ``userfaultfd``”h]”hŒ userfaultfd”…”�”}”(hjþh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjöubhŒã 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 ”…”�”}”(hjöh²hh³Nh´Nubhõ)�”}”(hŒ``userfaultfd``”h]”hŒ userfaultfd”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjöubhŒŒ 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.”…”�”}”(hjöh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M0hjåh²hubhÝ)�”}”(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]”(hŒGuest async page faults, ”…”�”}”(hj(h²hh³Nh´Nubhõ)�”}”(hŒ``FOLL_NOWAIT``”h]”hŒ FOLL_NOWAIT”…”�”}”(hj0h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj(ubhŒ and all other ”…”�”}”(hj(h²hh³Nh´Nubhõ)�”}”(hŒ``GUP*``”h]”hŒGUP*”…”�”}”(hjBh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj(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(h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M8hjåh²hubhÝ)�”}”(hŒ¶It 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]”hŒ¶It 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.”…”�”}”(hjZh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M>hjåh²hubhÝ)�”}”(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]”(hŒîThe 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 ”…”�”}”(hjhh²hh³Nh´Nubhõ)�”}”(hŒ``/proc/sys/net/ipv4/tcp_wmem``”h]”hŒ/proc/sys/net/ipv4/tcp_wmem”…”�”}”(hjph²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjhubhŒ).”…”�”}”(hjhh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MBhjåh²hubhÝ)�”}”(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]”(hŒ»The 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 ”…”�”}”(hjˆh²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_COPY|ZEROPAGE``”h]”hŒUFFDIO_COPY|ZEROPAGE”…”�”}”(hj�h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjˆubhŒ ioctls on the ”…”�”}”(hjˆh²hh³Nh´Nubhõ)�”}”(hŒ``userfaultfd``”h]”hŒ userfaultfd”…”�”}”(hj¢h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjˆubhŒ4 in order to map the received pages into the guest (”…”�”}”(hjˆh²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_ZEROCOPY``”h]”hŒUFFDIO_ZEROCOPY”…”�”}”(hj´h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjˆubhŒ- is used if the source page was a zero page).”…”�”}”(hjˆh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MGhjåh²hubhÝ)�”}”(hXŽA 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]”(hŒOA different postcopy thread in the destination node listens with poll() to the ”…”�”}”(hjÌh²hh³Nh´Nubhõ)�”}”(hŒ``userfaultfd``”h]”hŒ userfaultfd”…”�”}”(hjÔh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjÌubhŒ in parallel. When a ”…”�”}”(hjÌh²hh³Nh´Nubhõ)�”}”(hŒ ``POLLIN``”h]”hŒPOLLIN”…”�”}”(hjæh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjÌubhŒT event is generated after a userfault triggers, the postcopy thread read() from the ”…”�”}”(hjÌh²hh³Nh´Nubhõ)�”}”(hŒ``userfaultfd``”h]”hŒ userfaultfd”…”�”}”(hjøh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjÌubhŒ$ and receives the fault address (or ”…”�”}”(hjÌh²hh³Nh´Nubhõ)�”}”(hŒ ``-EAGAIN``”h]”hŒ-EAGAIN”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjÌubhŒ; in case the userfault was already resolved and waken by a ”…”�”}”(hjÌh²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_COPY|ZEROPAGE``”h]”hŒUFFDIO_COPY|ZEROPAGE”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjÌubhŒ, run by the parallel QEMU migration thread).”…”�”}”(hjÌh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MMhjåh²hubhÝ)�”}”(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 ``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 ”…”�”}”(hj4h²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_COPY|ZEROPAGE``”h]”hŒUFFDIO_COPY|ZEROPAGE”…”�”}”(hj<h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj4ubhŒ� (without actually knowing if it was spontaneously sent by the source or if it was an urgent page requested through a userfault).”…”�”}”(hj4h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MThjåh²hubhÝ)�”}”(hX¸By 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]”(hX…By 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 ”…”�”}”(hjTh²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_COPY|ZEROPAGE``”h]”hŒUFFDIO_COPY|ZEROPAGE”…”�”}”(hj\h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjTubhŒ runs in the migration thread).”…”�”}”(hjTh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M`hjåh²hubeh}”(h]”Œqemu-kvm”ah ]”h"]”Œqemu/kvm”ah$]”h&]”uh1hµhh·h²hh³hÊh´M.ubh¶)�”}”(hhh]”(h»)�”}”(hŒNon-cooperative userfaultfd”h]”hŒNon-cooperative userfaultfd”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hºhj|h²hh³hÊh´MmubhÝ)�”}”(hX‘When 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 ”…”�”}”(hj�h²hh³Nh´Nubhõ)�”}”(hŒ``userfaultfd``”h]”hŒ userfaultfd”…”�”}”(hj•h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj�ubhXB 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 ”…”�”}”(hj�h²hh³Nh´Nubhõ)�”}”(hŒ``uffdio_api.features``”h]”hŒuffdio_api.features”…”�”}”(hj§h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj�ubhŒ passed to ”…”�”}”(hj�h²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_API``”h]”hŒ UFFDIO_API”…”�”}”(hj¹h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj�ubhŒ ioctl:”…”�”}”(hj�h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´Mohj|h²hubhŒdefinition_list”“”)�”}”(hhh]”(hŒdefinition_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]”(hŒterm”“”)�”}”(hŒ``UFFD_FEATURE_EVENT_FORK``”h]”hõ)�”}”(hjàh]”hŒUFFD_FEATURE_EVENT_FORK”…”�”}”(hjâh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjÞubah}”(h]”h ]”h"]”h$]”h&]”uh1jÜh³hÊh´M{hjØubhŒ 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]”(hŒenable ”…”�”}”(hjúh²hh³Nh´Nubhõ)�”}”(hŒ``userfaultfd``”h]”hŒ userfaultfd”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjúubhŒ5 hooks for fork(). When this feature is enabled, the ”…”�”}”(hjúh²hh³Nh´Nubhõ)�”}”(hŒ``userfaultfd``”h]”hŒ userfaultfd”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjúubhŒb context of the parent process is duplicated into the newly created process. The manager receives ”…”�”}”(hjúh²hh³Nh´Nubhõ)�”}”(hŒ``UFFD_EVENT_FORK``”h]”hŒUFFD_EVENT_FORK”…”�”}”(hj&h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjúubhŒ! with file descriptor of the new ”…”�”}”(hjúh²hh³Nh´Nubhõ)�”}”(hŒ``userfaultfd``”h]”hŒ userfaultfd”…”�”}”(hj8h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjúubhŒ context in the ”…”�”}”(hjúh²hh³Nh´Nubhõ)�”}”(hŒ``uffd_msg.fork``”h]”hŒ uffd_msg.fork”…”�”}”(hjJh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjúubhŒ.”…”�”}”(hjúh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´Mwhj÷ubah}”(h]”h ]”h"]”h$]”h&]”uh1jõhjØubeh}”(h]”h ]”h"]”h$]”h&]”uh1jÖh³hÊh´M{hjÓubj×)�”}”(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]”hŒUFFD_FEATURE_EVENT_REMAP”…”�”}”(hjvh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjrubah}”(h]”h ]”h"]”h$]”h&]”uh1jÜh³hÊh´M‚hjnubjö)�”}”(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]”(hŒšenable notifications about mremap() calls. When the non-cooperative process moves a virtual memory area to a different location, the manager will receive ”…”�”}”(hjŒh²hh³Nh´Nubhõ)�”}”(hŒ``UFFD_EVENT_REMAP``”h]”hŒUFFD_EVENT_REMAP”…”�”}”(hj”h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjŒubhŒ. The ”…”�”}”(hjŒh²hh³Nh´Nubhõ)�”}”(hŒ``uffd_msg.remap``”h]”hŒuffd_msg.remap”…”�”}”(hj¦h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjŒubhŒL will contain the old and new addresses of the area and its original length.”…”�”}”(hjŒh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M~hj‰ubah}”(h]”h ]”h"]”h$]”h&]”uh1jõhjnubeh}”(h]”h ]”h"]”h$]”h&]”uh1jÖh³hÊh´M‚hjÓh²hubj×)�”}”(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õ)�”}”(hjÐh]”hŒUFFD_FEATURE_EVENT_REMOVE”…”�”}”(hjÒh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjÎubah}”(h]”h ]”h"]”h$]”h&]”uh1jÜh³hÊh´MˆhjÊubjö)�”}”(hhh]”hÝ)�”}”(hŒô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]”(hŒ\enable notifications about madvise(MADV_REMOVE) and madvise(MADV_DONTNEED) calls. The event ”…”�”}”(hjèh²hh³Nh´Nubhõ)�”}”(hŒ``UFFD_EVENT_REMOVE``”h]”hŒUFFD_EVENT_REMOVE”…”�”}”(hjðh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjèubhŒ6 will be generated upon these calls to madvise(). The ”…”�”}”(hjèh²hh³Nh´Nubhõ)�”}”(hŒ``uffd_msg.remove``”h]”hŒuffd_msg.remove”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjèubhŒ: will contain start and end addresses of the removed area.”…”�”}”(hjèh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M…hjåubah}”(h]”h ]”h"]”h$]”h&]”uh1jõhjÊubeh}”(h]”h ]”h"]”h$]”h&]”uh1jÖh³hÊh´MˆhjÓh²hubj×)�”}”(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]”hŒUFFD_FEATURE_EVENT_UNMAP”…”�”}”(hj.h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhj*ubah}”(h]”h ]”h"]”h$]”h&]”uh1jÜh³hÊh´M�hj&ubjö)�”}”(hhh]”hÝ)�”}”(hŒ¨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]”(hŒBenable notifications about memory unmapping. The manager will get ”…”�”}”(hjDh²hh³Nh´Nubhõ)�”}”(hŒ``UFFD_EVENT_UNMAP``”h]”hŒUFFD_EVENT_UNMAP”…”�”}”(hjLh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjDubhŒ with ”…”�”}”(hjDh²hh³Nh´Nubhõ)�”}”(hŒ``uffd_msg.remove``”h]”hŒuffd_msg.remove”…”�”}”(hj^h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjDubhŒ9 containing start and end addresses of the unmapped area.”…”�”}”(hjDh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M‹hjAubah}”(h]”h ]”h"]”h$]”h&]”uh1jõhj&ubeh}”(h]”h ]”h"]”h$]”h&]”uh1jÖh³hÊh´M�hjÓh²hubeh}”(h]”h ]”h"]”h$]”h&]”uh1jÑhj|h²hh³hÊh´NubhÝ)�”}”(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 ”…”�”}”(hjˆh²hh³Nh´Nubhõ)�”}”(hŒ``UFFD_FEATURE_EVENT_REMOVE``”h]”hŒUFFD_FEATURE_EVENT_REMOVE”…”�”}”(hj�h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjˆubhŒ and ”…”�”}”(hjˆh²hh³Nh´Nubhõ)�”}”(hŒ``UFFD_FEATURE_EVENT_UNMAP``”h]”hŒUFFD_FEATURE_EVENT_UNMAP”…”�”}”(hj¢h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjˆubhŒG are pretty similar, they quite differ in the action expected from the ”…”�”}”(hjˆh²hh³Nh´Nubhõ)�”}”(hŒ``userfaultfd``”h]”hŒ userfaultfd”…”�”}”(hj´h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjˆubhŒt manager. In the former case, the virtual memory is removed, but the area is not, the area remains monitored by the ”…”�”}”(hjˆh²hh³Nh´Nubhõ)�”}”(hŒ``userfaultfd``”h]”hŒ userfaultfd”…”�”}”(hjÆh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjˆubhXM, 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 ”…”�”}”(hjˆh²hh³Nh´Nubhõ)�”}”(hŒ``userfaultfd``”h]”hŒ userfaultfd”…”�”}”(hjØh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjˆubhŒÂ 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 ”…”�”}”(hjˆh²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_COPY``”h]”hŒ UFFDIO_COPY”…”�”}”(hjêh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjˆubhŒ on the unmapped area.”…”�”}”(hjˆh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M�hj|h²hubhÝ)�”}”(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]”(hŒèUnlike 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 ”…”�”}”(hjh²hh³Nh´Nubhõ)�”}”(hŒ``userfaultfd``”h]”hŒ userfaultfd”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjubhŒ/ manager should carefully synchronize calls to ”…”�”}”(hjh²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_COPY``”h]”hŒ UFFDIO_COPY”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjubhŒ= with the events processing. To aid the synchronization, the ”…”�”}”(hjh²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_COPY``”h]”hŒ UFFDIO_COPY”…”�”}”(hj.h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjubhŒ ioctl will return ”…”�”}”(hjh²hh³Nh´Nubhõ)�”}”(hŒ ``-ENOSPC``”h]”hŒ-ENOSPC”…”�”}”(hj@h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjubhŒ1 when the monitored process exits at the time of ”…”�”}”(hjh²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_COPY``”h]”hŒ UFFDIO_COPY”…”�”}”(hjRh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjubhŒ, and ”…”�”}”(hjh²hh³Nh´Nubhõ)�”}”(hŒ ``-ENOENT``”h]”hŒ-ENOENT”…”�”}”(hjdh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjubhŒi, when the non-cooperative process has changed its virtual memory layout simultaneously with outstanding ”…”�”}”(hjh²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_COPY``”h]”hŒ UFFDIO_COPY”…”�”}”(hjvh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjubhŒ operation.”…”�”}”(hjh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M�hj|h²hubhÝ)�”}”(hXëThe 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]”(hŒdThe current asynchronous model of the event delivery is optimal for single threaded non-cooperative ”…”�”}”(hjŽh²hh³Nh´Nubhõ)�”}”(hŒ``userfaultfd``”h]”hŒ userfaultfd”…”�”}”(hj–h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjŽubhŒY manager implementations. A synchronous event delivery model can be added later as a new ”…”�”}”(hjŽh²hh³Nh´Nubhõ)�”}”(hŒ``userfaultfd``”h]”hŒ userfaultfd”…”�”}”(hj¨h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjŽubhŒh feature to facilitate multithreading enhancements of the non cooperative manager, for example to allow ”…”�”}”(hjŽh²hh³Nh´Nubhõ)�”}”(hŒ``UFFDIO_COPY``”h]”hŒ UFFDIO_COPY”…”�”}”(hjºh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hôhjŽubhŒ™ ioctls to run in parallel to the event reception. Single threaded implementations should continue to use the current async event delivery model instead.”…”�”}”(hjŽh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M¨hj|h²hubeh}”(h]”j1ah ]”h"]”Œnon-cooperative userfaultfd”ah$]”h&]”uh1hµhh·h²hh³hÊh´MmŒ referenced”Kubeh}”(h]”Œ userfaultfd”ah ]”h"]”Œ userfaultfd”ah$]”h&]”uh1hµhhh²hh³hÊh´Kubeh}”(h]”h ]”h"]”h$]”h&]”Œsource”hÊuh1hŒcurrent_source”NŒ current_line”NŒsettings”Œdocutils.frontend”ŒValues”“”)�”}”(hºNŒ generator”NŒ datestamp”NŒ source_link”NŒ source_url”NŒ toc_backlinks”Œentry”Œfootnote_backlinks”KŒ sectnum_xform”KŒstrip_comments”NŒstrip_elements_with_classes”NŒ strip_classes”NŒ report_level”KŒ halt_level”KŒexit_status_level”KŒdebug”NŒwarning_stream”NŒ traceback”ˆŒinput_encoding”Œ utf-8-sig”Œinput_encoding_error_handler”Œstrict”Œoutput_encoding”Œutf-8”Œoutput_encoding_error_handler”jŒerror_encoding”Œutf-8”Œerror_encoding_error_handler”Œbackslashreplace”Œ language_code”Œen”Œrecord_dependencies”NŒconfig”NŒ id_prefix”hŒauto_id_prefix”Œid”Œ dump_settings”NŒdump_internals”NŒdump_transforms”NŒdump_pseudo_xml”NŒexpose_internals”NŒstrict_visitor”NŒ_disable_config”NŒ_source”hÊŒ _destination”NŒ _config_files”]”Œ7/var/lib/git/docbuild/linux/Documentation/docutils.conf”aŒfile_insertion_enabled”ˆŒ raw_enabled”KŒline_length_limit”M'Œpep_references”NŒ pep_base_url”Œhttps://peps.python.org/”Œpep_file_url_template”Œpep-%04d”Œrfc_references”NŒ rfc_base_url”Œ&https://datatracker.ietf.org/doc/html/”Œ tab_width”KŒtrim_footnote_reference_space”‰Œsyntax_highlight”Œlong”Œ smart_quotes”ˆŒsmartquotes_locales”]”Œcharacter_level_inline_markup”‰Œdoctitle_xform”‰Œ docinfo_xform”KŒsectsubtitle_xform”‰Œ image_loading”Œlink”Œembed_stylesheet”‰Œcloak_email_addresses”ˆŒsection_self_link”‰Œenv”NubŒreporter”NŒindirect_targets”]”Œsubstitution_defs”}”Œsubstitution_names”}”Œrefnames”}”Œnon-cooperative userfaultfd”]”j asŒrefids”}”Œnameids”}”(jßjÜjjj2j/jâjßjÑjÎj�j�jÒjÏjÊ jÇ j j jÚj×jyjvjÖj1uŒ nametypes”}”(j߉j‰j2‰jâ‰jщj�‰jÒ‰jÊ ‰j ‰jÚ‰jy‰jÖ‰uh}”(jÜh·jhËj/jjßj5jÎjFj�jÔjÏj“jÇ jÕj jÍ j×j jvjåj1j|uŒ footnote_refs”}”Œ citation_refs”}”Œ autofootnotes”]”Œautofootnote_refs”]”Œsymbol_footnotes”]”Œsymbol_footnote_refs”]”Œ footnotes”]”Œ citations”]”Œautofootnote_start”KŒsymbol_footnote_start”KŒ id_counter”Œ collections”ŒCounter”“”}”…”R”Œparse_messages”]”Œtransform_messages”]”Œ transformer”NŒ include_log”]”Œ decoration”Nh²hub.