€•Œ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/mm/hmm”Œ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/mm/hmm”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒItalian”…”�”}”hhFsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ/translations/it_IT/mm/hmm”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒJapanese”…”�”}”hhZsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ/translations/ja_JP/mm/hmm”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒKorean”…”�”}”hhnsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ/translations/ko_KR/mm/hmm”Œ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/mm/hmm”Œ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/mm/hmm”Œ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Œ%Heterogeneous Memory Management (HMM)”h]”hŒ%Heterogeneous Memory Management (HMM)”…”�”}”(hh¼h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hºhh·h²hh³Œ4/var/lib/git/docbuild/linux/Documentation/mm/hmm.rst”h´KubhŒ paragraph”“”)�”}”(hŒýProvide infrastructure and helpers to integrate non-conventional memory (device memory like GPU on board memory) into regular kernel path, with the cornerstone of this being specialized struct page for such memory (see sections 5 to 7 of this document).”h]”hŒýProvide infrastructure and helpers to integrate non-conventional memory (device memory like GPU on board memory) into regular kernel path, with the cornerstone of this being specialized struct page for such memory (see sections 5 to 7 of this document).”…”�”}”(hhÍh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´Khh·h²hubhÌ)�”}”(hXšHMM also provides optional helpers for SVM (Share Virtual Memory), i.e., allowing a device to transparently access program addresses coherently with the CPU meaning that any valid pointer on the CPU is also a valid pointer for the device. This is becoming mandatory to simplify the use of advanced heterogeneous computing where GPU, DSP, or FPGA are used to perform various computations on behalf of a process.”h]”hXšHMM also provides optional helpers for SVM (Share Virtual Memory), i.e., allowing a device to transparently access program addresses coherently with the CPU meaning that any valid pointer on the CPU is also a valid pointer for the device. This is becoming mandatory to simplify the use of advanced heterogeneous computing where GPU, DSP, or FPGA are used to perform various computations on behalf of a process.”…”�”}”(hhÛh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´K hh·h²hubhÌ)�”}”(hX8This document is divided as follows: in the first section I expose the problems related to using device specific memory allocators. In the second section, I expose the hardware limitations that are inherent to many platforms. The third section gives an overview of the HMM design. The fourth section explains how CPU page-table mirroring works and the purpose of HMM in this context. The fifth section deals with how device memory is represented inside the kernel. Finally, the last section presents a new migration helper that allows leveraging the device DMA engine.”h]”hX8This document is divided as follows: in the first section I expose the problems related to using device specific memory allocators. In the second section, I expose the hardware limitations that are inherent to many platforms. The third section gives an overview of the HMM design. The fourth section explains how CPU page-table mirroring works and the purpose of HMM in this context. The fifth section deals with how device memory is represented inside the kernel. Finally, the last section presents a new migration helper that allows leveraging the device DMA engine.”…”�”}”(hhéh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´Khh·h²hubhŒtopic”“”)�”}”(hhh]”hŒ bullet_list”“”)�”}”(hhh]”(hŒ list_item”“”)�”}”(hhh]”hÌ)�”}”(hhh]”hŒ reference”“”)�”}”(hhh]”hŒ4Problems of using a device specific memory allocator”…”�”}”(hj h²hh³Nh´Nubah}”(h]”Œid1”ah ]”h"]”h$]”h&]”Œrefid”Œ4problems-of-using-a-device-specific-memory-allocator”uh1j hjubah}”(h]”h ]”h"]”h$]”h&]”uh1hËhjubah}”(h]”h ]”h"]”h$]”h&]”uh1jhhþubj)�”}”(hhh]”hÌ)�”}”(hhh]”j )�”}”(hhh]”hŒ&I/O bus, device memory characteristics”…”�”}”(hj-h²hh³Nh´Nubah}”(h]”Œid2”ah ]”h"]”h$]”h&]”Œrefid”Œ%i-o-bus-device-memory-characteristics”uh1j hj*ubah}”(h]”h ]”h"]”h$]”h&]”uh1hËhj'ubah}”(h]”h ]”h"]”h$]”h&]”uh1jhhþubj)�”}”(hhh]”hÌ)�”}”(hhh]”j )�”}”(hhh]”hŒ"Shared address space and migration”…”�”}”(hjOh²hh³Nh´Nubah}”(h]”Œid3”ah ]”h"]”h$]”h&]”Œrefid”Œ"shared-address-space-and-migration”uh1j hjLubah}”(h]”h ]”h"]”h$]”h&]”uh1hËhjIubah}”(h]”h ]”h"]”h$]”h&]”uh1jhhþubj)�”}”(hhh]”hÌ)�”}”(hhh]”j )�”}”(hhh]”hŒ.Address space mirroring implementation and API”…”�”}”(hjqh²hh³Nh´Nubah}”(h]”Œid4”ah ]”h"]”h$]”h&]”Œrefid”Œ.address-space-mirroring-implementation-and-api”uh1j hjnubah}”(h]”h ]”h"]”h$]”h&]”uh1hËhjkubah}”(h]”h ]”h"]”h$]”h&]”uh1jhhþubj)�”}”(hhh]”hÌ)�”}”(hhh]”j )�”}”(hhh]”hŒ'Holding the mmap lock across HMM faults”…”�”}”(hj“h²hh³Nh´Nubah}”(h]”Œid5”ah ]”h"]”h$]”h&]”Œrefid”Œ'holding-the-mmap-lock-across-hmm-faults”uh1j hj�ubah}”(h]”h ]”h"]”h$]”h&]”uh1hËhj�ubah}”(h]”h ]”h"]”h$]”h&]”uh1jhhþubj)�”}”(hhh]”hÌ)�”}”(hhh]”j )�”}”(hhh]”hŒ)Leverage default_flags and pfn_flags_mask”…”�”}”(hjµh²hh³Nh´Nubah}”(h]”Œid6”ah ]”h"]”h$]”h&]”Œrefid”Œ)leverage-default-flags-and-pfn-flags-mask”uh1j hj²ubah}”(h]”h ]”h"]”h$]”h&]”uh1hËhj¯ubah}”(h]”h ]”h"]”h$]”h&]”uh1jhhþubj)�”}”(hhh]”hÌ)�”}”(hhh]”j )�”}”(hhh]”hŒARepresent and manage device memory from core kernel point of view”…”�”}”(hj×h²hh³Nh´Nubah}”(h]”Œid7”ah ]”h"]”h$]”h&]”Œrefid”ŒArepresent-and-manage-device-memory-from-core-kernel-point-of-view”uh1j hjÔubah}”(h]”h ]”h"]”h$]”h&]”uh1hËhjÑubah}”(h]”h ]”h"]”h$]”h&]”uh1jhhþubj)�”}”(hhh]”hÌ)�”}”(hhh]”j )�”}”(hhh]”hŒ#Migration to and from device memory”…”�”}”(hjùh²hh³Nh´Nubah}”(h]”Œid8”ah ]”h"]”h$]”h&]”Œrefid”Œ#migration-to-and-from-device-memory”uh1j hjöubah}”(h]”h ]”h"]”h$]”h&]”uh1hËhjóubah}”(h]”h ]”h"]”h$]”h&]”uh1jhhþubj)�”}”(hhh]”hÌ)�”}”(hhh]”j )�”}”(hhh]”hŒExclusive access memory”…”�”}”(hjh²hh³Nh´Nubah}”(h]”Œid9”ah ]”h"]”h$]”h&]”Œrefid”Œexclusive-access-memory”uh1j hjubah}”(h]”h ]”h"]”h$]”h&]”uh1hËhjubah}”(h]”h ]”h"]”h$]”h&]”uh1jhhþubj)�”}”(hhh]”hÌ)�”}”(hhh]”j )�”}”(hhh]”hŒ(Memory cgroup (memcg) and rss accounting”…”�”}”(hj=h²hh³Nh´Nubah}”(h]”Œid10”ah ]”h"]”h$]”h&]”Œrefid”Œ&memory-cgroup-memcg-and-rss-accounting”uh1j hj:ubah}”(h]”h ]”h"]”h$]”h&]”uh1hËhj7ubah}”(h]”h ]”h"]”h$]”h&]”uh1jhhþubeh}”(h]”h ]”h"]”h$]”h&]”uh1hühhùh²hh³Nh´Nubah}”(h]”Œcontents”ah ]”(Œcontents”Œlocal”eh"]”Œcontents”ah$]”h&]”uh1h÷h³hÊh´Khh·h²hubh¶)�”}”(hhh]”(h»)�”}”(hŒ4Problems of using a device specific memory allocator”h]”hŒ4Problems of using a device specific memory allocator”…”�”}”(hjlh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”Œrefid”juh1hºhjih²hh³hÊh´KubhÌ)�”}”(hX'Devices with a large amount of on board memory (several gigabytes) like GPUs have historically managed their memory through dedicated driver specific APIs. This creates a disconnect between memory allocated and managed by a device driver and regular application memory (private anonymous, shared memory, or regular file backed memory). From here on I will refer to this aspect as split address space. I use shared address space to refer to the opposite situation: i.e., one in which any application memory region can be used by a device transparently.”h]”hX'Devices with a large amount of on board memory (several gigabytes) like GPUs have historically managed their memory through dedicated driver specific APIs. This creates a disconnect between memory allocated and managed by a device driver and regular application memory (private anonymous, shared memory, or regular file backed memory). From here on I will refer to this aspect as split address space. I use shared address space to refer to the opposite situation: i.e., one in which any application memory region can be used by a device transparently.”…”�”}”(hj{h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´Khjih²hubhÌ)�”}”(hXSplit address space happens because devices can only access memory allocated through a device specific API. This implies that all memory objects in a program are not equal from the device point of view which complicates large programs that rely on a wide set of libraries.”h]”hXSplit address space happens because devices can only access memory allocated through a device specific API. This implies that all memory objects in a program are not equal from the device point of view which complicates large programs that rely on a wide set of libraries.”…”�”}”(hj‰h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´K(hjih²hubhÌ)�”}”(hXConcretely, this means that code that wants to leverage devices like GPUs needs to copy objects between generically allocated memory (malloc, mmap private, mmap share) and memory allocated through the device driver API (this still ends up with an mmap but of the device file).”h]”hXConcretely, this means that code that wants to leverage devices like GPUs needs to copy objects between generically allocated memory (malloc, mmap private, mmap share) and memory allocated through the device driver API (this still ends up with an mmap but of the device file).”…”�”}”(hj—h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´K-hjih²hubhÌ)�”}”(hX\For flat data sets (array, grid, image, ...) this isn't too hard to achieve but for complex data sets (list, tree, ...) it's hard to get right. Duplicating a complex data set needs to re-map all the pointer relations between each of its elements. This is error prone and programs get harder to debug because of the duplicate data set and addresses.”h]”hX`For flat data sets (array, grid, image, ...) this isn’t too hard to achieve but for complex data sets (list, tree, ...) it’s hard to get right. Duplicating a complex data set needs to re-map all the pointer relations between each of its elements. This is error prone and programs get harder to debug because of the duplicate data set and addresses.”…”�”}”(hj¥h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´K2hjih²hubhÌ)�”}”(hXLSplit address space also means that libraries cannot transparently use data they are getting from the core program or another library and thus each library might have to duplicate its input data set using the device specific memory allocator. Large projects suffer from this and waste resources because of the various memory copies.”h]”hXLSplit address space also means that libraries cannot transparently use data they are getting from the core program or another library and thus each library might have to duplicate its input data set using the device specific memory allocator. Large projects suffer from this and waste resources because of the various memory copies.”…”�”}”(hj³h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´K8hjih²hubhÌ)�”}”(hŒÌDuplicating each library API to accept as input or output memory allocated by each device specific allocator is not a viable option. It would lead to a combinatorial explosion in the library entry points.”h]”hŒÌDuplicating each library API to accept as input or output memory allocated by each device specific allocator is not a viable option. It would lead to a combinatorial explosion in the library entry points.”…”�”}”(hjÁh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´K>hjih²hubhÌ)�”}”(hXdFinally, with the advance of high level language constructs (in C++ but in other languages too) it is now possible for the compiler to leverage GPUs and other devices without programmer knowledge. Some compiler identified patterns are only doable with a shared address space. It is also more reasonable to use a shared address space for all other patterns.”h]”hXdFinally, with the advance of high level language constructs (in C++ but in other languages too) it is now possible for the compiler to leverage GPUs and other devices without programmer knowledge. Some compiler identified patterns are only doable with a shared address space. It is also more reasonable to use a shared address space for all other patterns.”…”�”}”(hjÏh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´KBhjih²hubeh}”(h]”jah ]”h"]”Œ4problems of using a device specific memory allocator”ah$]”h&]”uh1hµhh·h²hh³hÊh´Kubh¶)�”}”(hhh]”(h»)�”}”(hŒ&I/O bus, device memory characteristics”h]”hŒ&I/O bus, device memory characteristics”…”�”}”(hjçh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”jzj6uh1hºhjäh²hh³hÊh´KJubhÌ)�”}”(hXI/O buses cripple shared address spaces due to a few limitations. Most I/O buses only allow basic memory access from device to main memory; even cache coherency is often optional. Access to device memory from a CPU is even more limited. More often than not, it is not cache coherent.”h]”hXI/O buses cripple shared address spaces due to a few limitations. Most I/O buses only allow basic memory access from device to main memory; even cache coherency is often optional. Access to device memory from a CPU is even more limited. More often than not, it is not cache coherent.”…”�”}”(hjõh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´KLhjäh²hubhÌ)�”}”(hXÐIf we only consider the PCIE bus, then a device can access main memory (often through an IOMMU) and be cache coherent with the CPUs. However, it only allows a limited set of atomic operations from the device on main memory. This is worse in the other direction: the CPU can only access a limited range of the device memory and cannot perform atomic operations on it. Thus device memory cannot be considered the same as regular memory from the kernel point of view.”h]”hXÐIf we only consider the PCIE bus, then a device can access main memory (often through an IOMMU) and be cache coherent with the CPUs. However, it only allows a limited set of atomic operations from the device on main memory. This is worse in the other direction: the CPU can only access a limited range of the device memory and cannot perform atomic operations on it. Thus device memory cannot be considered the same as regular memory from the kernel point of view.”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´KQhjäh²hubhÌ)�”}”(hX9Another crippling factor is the limited bandwidth (~32GBytes/s with PCIE 4.0 and 16 lanes). This is 33 times less than the fastest GPU memory (1 TBytes/s). The final limitation is latency. Access to main memory from the device has an order of magnitude higher latency than when the device accesses its own memory.”h]”hX9Another crippling factor is the limited bandwidth (~32GBytes/s with PCIE 4.0 and 16 lanes). This is 33 times less than the fastest GPU memory (1 TBytes/s). The final limitation is latency. Access to main memory from the device has an order of magnitude higher latency than when the device accesses its own memory.”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´KXhjäh²hubhÌ)�”}”(hX‡Some platforms are developing new I/O buses or additions/modifications to PCIE to address some of these limitations (OpenCAPI, CCIX). They mainly allow two-way cache coherency between CPU and device and allow all atomic operations the architecture supports. Sadly, not all platforms are following this trend and some major architectures are left without hardware solutions to these problems.”h]”hX‡Some platforms are developing new I/O buses or additions/modifications to PCIE to address some of these limitations (OpenCAPI, CCIX). They mainly allow two-way cache coherency between CPU and device and allow all atomic operations the architecture supports. Sadly, not all platforms are following this trend and some major architectures are left without hardware solutions to these problems.”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´K]hjäh²hubhÌ)�”}”(hŒæSo for shared address space to make sense, not only must we allow devices to access any memory but we must also permit any memory to be migrated to device memory while the device is using it (blocking CPU access while it happens).”h]”hŒæSo for shared address space to make sense, not only must we allow devices to access any memory but we must also permit any memory to be migrated to device memory while the device is using it (blocking CPU access while it happens).”…”�”}”(hj-h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´Kchjäh²hubeh}”(h]”j<ah ]”h"]”Œ&i/o bus, device memory characteristics”ah$]”h&]”uh1hµhh·h²hh³hÊh´KJubh¶)�”}”(hhh]”(h»)�”}”(hŒ"Shared address space and migration”h]”hŒ"Shared address space and migration”…”�”}”(hjEh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”jzjXuh1hºhjBh²hh³hÊh´KiubhÌ)�”}”(hXHMM intends to provide two main features. The first one is to share the address space by duplicating the CPU page table in the device page table so the same address points to the same physical memory for any valid main memory address in the process address space.”h]”hXHMM intends to provide two main features. The first one is to share the address space by duplicating the CPU page table in the device page table so the same address points to the same physical memory for any valid main memory address in the process address space.”…”�”}”(hjSh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´KkhjBh²hubhÌ)�”}”(hXPTo achieve this, HMM offers a set of helpers to populate the device page table while keeping track of CPU page table updates. Device page table updates are not as easy as CPU page table updates. To update the device page table, you must allocate a buffer (or use a pool of pre-allocated buffers) and write GPU specific commands in it to perform the update (unmap, cache invalidations, and flush, ...). This cannot be done through common code for all devices. Hence why HMM provides helpers to factor out everything that can be while leaving the hardware specific details to the device driver.”h]”hXPTo achieve this, HMM offers a set of helpers to populate the device page table while keeping track of CPU page table updates. Device page table updates are not as easy as CPU page table updates. To update the device page table, you must allocate a buffer (or use a pool of pre-allocated buffers) and write GPU specific commands in it to perform the update (unmap, cache invalidations, and flush, ...). This cannot be done through common code for all devices. Hence why HMM provides helpers to factor out everything that can be while leaving the hardware specific details to the device driver.”…”�”}”(hjah²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´KphjBh²hubhÌ)�”}”(hX¨The second mechanism HMM provides is a new kind of ZONE_DEVICE memory that allows allocating a struct page for each page of device memory. Those pages are special because the CPU cannot map them. However, they allow migrating main memory to device memory using existing migration mechanisms and everything looks like a page that is swapped out to disk from the CPU point of view. Using a struct page gives the easiest and cleanest integration with existing mm mechanisms. Here again, HMM only provides helpers, first to hotplug new ZONE_DEVICE memory for the device memory and second to perform migration. Policy decisions of what and when to migrate is left to the device driver.”h]”hX¨The second mechanism HMM provides is a new kind of ZONE_DEVICE memory that allows allocating a struct page for each page of device memory. Those pages are special because the CPU cannot map them. However, they allow migrating main memory to device memory using existing migration mechanisms and everything looks like a page that is swapped out to disk from the CPU point of view. Using a struct page gives the easiest and cleanest integration with existing mm mechanisms. Here again, HMM only provides helpers, first to hotplug new ZONE_DEVICE memory for the device memory and second to perform migration. Policy decisions of what and when to migrate is left to the device driver.”…”�”}”(hjoh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´KyhjBh²hubhÌ)�”}”(hX7Note that any CPU access to a device page triggers a page fault and a migration back to main memory. For example, when a page backing a given CPU address A is migrated from a main memory page to a device page, then any CPU access to address A triggers a page fault and initiates a migration back to main memory.”h]”hX7Note that any CPU access to a device page triggers a page fault and a migration back to main memory. For example, when a page backing a given CPU address A is migrated from a main memory page to a device page, then any CPU access to address A triggers a page fault and initiates a migration back to main memory.”…”�”}”(hj}h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´KƒhjBh²hubhÌ)�”}”(hŒÿWith these two features, HMM not only allows a device to mirror process address space and keeps both CPU and device page tables synchronized, but also leverages device memory by migrating the part of the data set that is actively being used by the device.”h]”hŒÿWith these two features, HMM not only allows a device to mirror process address space and keeps both CPU and device page tables synchronized, but also leverages device memory by migrating the part of the data set that is actively being used by the device.”…”�”}”(hj‹h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´KˆhjBh²hubeh}”(h]”j^ah ]”h"]”Œ"shared address space and migration”ah$]”h&]”uh1hµhh·h²hh³hÊh´Kiubh¶)�”}”(hhh]”(h»)�”}”(hŒ.Address space mirroring implementation and API”h]”hŒ.Address space mirroring implementation and API”…”�”}”(hj£h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”jzjzuh1hºhj h²hh³hÊh´K�ubhÌ)�”}”(hXAddress space mirroring's main objective is to allow duplication of a range of CPU page table into a device page table; HMM helps keep both synchronized. A device driver that wants to mirror a process address space must start with the registration of a mmu_interval_notifier::”h]”hXAddress space mirroring’s main objective is to allow duplication of a range of CPU page table into a device page table; HMM helps keep both synchronized. A device driver that wants to mirror a process address space must start with the registration of a mmu_interval_notifier:”…”�”}”(hj±h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´K‘hj h²hubhŒ literal_block”“”)�”}”(hXint mmu_interval_notifier_insert(struct mmu_interval_notifier *interval_sub, struct mm_struct *mm, unsigned long start, unsigned long length, const struct mmu_interval_notifier_ops *ops);”h]”hXint mmu_interval_notifier_insert(struct mmu_interval_notifier *interval_sub, struct mm_struct *mm, unsigned long start, unsigned long length, const struct mmu_interval_notifier_ops *ops);”…”�”}”hjÁsbah}”(h]”h ]”h"]”h$]”h&]”Œ xml:space”Œpreserve”uh1j¿h³hÊh´K–hj h²hubhÌ)�”}”(hŒÙDuring the ops->invalidate() callback the device driver must perform the update action to the range (mark range read only, or fully unmap, etc.). The device must complete the update before the driver callback returns.”h]”hŒÙDuring the ops->invalidate() callback the device driver must perform the update action to the range (mark range read only, or fully unmap, etc.). The device must complete the update before the driver callback returns.”…”�”}”(hjÑh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´K›hj h²hubhÌ)�”}”(hŒ`When the device driver wants to populate a range of virtual addresses, the normal interface is::”h]”hŒ_When the device driver wants to populate a range of virtual addresses, the normal interface is:”…”�”}”(hjßh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´KŸhj h²hubjÀ)�”}”(hŒzint hmm_range_fault_unlocked_timeout(struct hmm_range *range, unsigned long timeout);”h]”hŒzint hmm_range_fault_unlocked_timeout(struct hmm_range *range, unsigned long timeout);”…”�”}”hjísbah}”(h]”h ]”h"]”h$]”h&]”jÏjÐuh1j¿h³hÊh´K¢hj h²hubhÌ)�”}”(hŒ¶It will trigger a page fault on missing or read-only entries if write access is requested (see below). Page faults use the generic mm page fault code path just like a CPU page fault.”h]”hŒ¶It will trigger a page fault on missing or read-only entries if write access is requested (see below). Page faults use the generic mm page fault code path just like a CPU page fault.”…”�”}”(hjûh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´K¥hj h²hubhÌ)�”}”(hXDThe caller must not hold ``mmap_read_lock`` before the call. ``hmm_range_fault_unlocked_timeout()`` takes the mmap read lock internally and allows ``handle_mm_fault()`` to drop it during fault handling. This is required for VMAs whose fault handlers may release the mmap lock, for example regions managed by ``userfaultfd``.”h]”(hŒThe caller must not hold ”…”�”}”(hj h²hh³Nh´NubhŒliteral”“”)�”}”(hŒ``mmap_read_lock``”h]”hŒmmap_read_lock”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj ubhŒ before the call. ”…”�”}”(hj h²hh³Nh´Nubj)�”}”(hŒ&``hmm_range_fault_unlocked_timeout()``”h]”hŒ"hmm_range_fault_unlocked_timeout()”…”�”}”(hj%h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj ubhŒ0 takes the mmap read lock internally and allows ”…”�”}”(hj h²hh³Nh´Nubj)�”}”(hŒ``handle_mm_fault()``”h]”hŒhandle_mm_fault()”…”�”}”(hj7h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj ubhŒŒ to drop it during fault handling. This is required for VMAs whose fault handlers may release the mmap lock, for example regions managed by ”…”�”}”(hj h²hh³Nh´Nubj)�”}”(hŒ``userfaultfd``”h]”hŒ userfaultfd”…”�”}”(hjIh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj ubhŒ.”…”�”}”(hj h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´K©hj h²hubhÌ)�”}”(hŒüIf the mmap lock is dropped or the range is invalidated, the function refreshes ``range->notifier_seq`` and restarts the walk internally. ``-EINTR`` is returned if mmap lock acquisition is interrupted or a fatal signal is pending during retry handling.”h]”(hŒPIf the mmap lock is dropped or the range is invalidated, the function refreshes ”…”�”}”(hjah²hh³Nh´Nubj)�”}”(hŒ``range->notifier_seq``”h]”hŒrange->notifier_seq”…”�”}”(hjih²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjaubhŒ# and restarts the walk internally. ”…”�”}”(hjah²hh³Nh´Nubj)�”}”(hŒ ``-EINTR``”h]”hŒ-EINTR”…”�”}”(hj{h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjaubhŒh is returned if mmap lock acquisition is interrupted or a fatal signal is pending during retry handling.”…”�”}”(hjah²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´K¯hj h²hubhÌ)�”}”(hXgThe timeout is specified in jiffies; passing ``0`` means retry indefinitely. The timeout exists to preserve caller policy for repeated mmu-notifier invalidation and is checked between retry attempts. HMM does not interrupt page fault handling when the timeout expires, but returns ``-EBUSY`` if the retry budget is exhausted before a stable range is obtained.”h]”(hŒ-The timeout is specified in jiffies; passing ”…”�”}”(hj“h²hh³Nh´Nubj)�”}”(hŒ``0``”h]”hŒ0”…”�”}”(hj›h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj“ubhŒç means retry indefinitely. The timeout exists to preserve caller policy for repeated mmu-notifier invalidation and is checked between retry attempts. HMM does not interrupt page fault handling when the timeout expires, but returns ”…”�”}”(hj“h²hh³Nh´Nubj)�”}”(hŒ ``-EBUSY``”h]”hŒ-EBUSY”…”�”}”(hj­h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj“ubhŒD if the retry budget is exhausted before a stable range is obtained.”…”�”}”(hj“h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´K´hj h²hubhÌ)�”}”(hŒThe usage pattern is::”h]”hŒThe usage pattern is:”…”�”}”(hjÅh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´Kºhj h²hubjÀ)�”}”(hX3int driver_populate_range(...) { struct hmm_range range; unsigned long timeout; ... timeout = msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT); range.notifier = &interval_sub; range.start = ...; range.end = ...; range.hmm_pfns = ...; if (!mmget_not_zero(interval_sub.mm)) return -EFAULT; again: ret = hmm_range_fault_unlocked_timeout(&range, timeout); if (ret) goto out_put; take_lock(driver->update); if (mmu_interval_read_retry(range.notifier, range.notifier_seq)) { release_lock(driver->update); goto again; } /* Use pfns array content to update device page table, * under the update lock */ release_lock(driver->update); ret = 0; out_put: mmput(interval_sub.mm); return ret; }”h]”hX3int driver_populate_range(...) { struct hmm_range range; unsigned long timeout; ... timeout = msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT); range.notifier = &interval_sub; range.start = ...; range.end = ...; range.hmm_pfns = ...; if (!mmget_not_zero(interval_sub.mm)) return -EFAULT; again: ret = hmm_range_fault_unlocked_timeout(&range, timeout); if (ret) goto out_put; take_lock(driver->update); if (mmu_interval_read_retry(range.notifier, range.notifier_seq)) { release_lock(driver->update); goto again; } /* Use pfns array content to update device page table, * under the update lock */ release_lock(driver->update); ret = 0; out_put: mmput(interval_sub.mm); return ret; }”…”�”}”hjÓsbah}”(h]”h ]”h"]”h$]”h&]”jÏjÐuh1j¿h³hÊh´K¼hj h²hubhÌ)�”}”(hXYThe driver->update lock is the same lock that the driver takes inside its invalidate() callback. That lock must be held before calling mmu_interval_read_retry() to avoid any race with a concurrent CPU page table update. The retry check must use the same notifier and sequence number stored in ``range`` by ``hmm_range_fault_unlocked_timeout()``.”h]”(hX%The driver->update lock is the same lock that the driver takes inside its invalidate() callback. That lock must be held before calling mmu_interval_read_retry() to avoid any race with a concurrent CPU page table update. The retry check must use the same notifier and sequence number stored in ”…”�”}”(hjáh²hh³Nh´Nubj)�”}”(hŒ ``range``”h]”hŒrange”…”�”}”(hjéh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjáubhŒ by ”…”�”}”(hjáh²hh³Nh´Nubj)�”}”(hŒ&``hmm_range_fault_unlocked_timeout()``”h]”hŒ"hmm_range_fault_unlocked_timeout()”…”�”}”(hjûh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjáubhŒ.”…”�”}”(hjáh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´Káhj h²hubeh}”(h]”j€ah ]”h"]”Œ.address space mirroring implementation and api”ah$]”h&]”uh1hµhh·h²hh³hÊh´K�ubh¶)�”}”(hhh]”(h»)�”}”(hŒ'Holding the mmap lock across HMM faults”h]”hŒ'Holding the mmap lock across HMM faults”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”jzjœuh1hºhjh²hh³hÊh´KèubhÌ)�”}”(hŒ”Most callers should use ``hmm_range_fault_unlocked_timeout()``. If a driver really needs to hold the mmap lock across work outside HMM, it can use::”h]”(hŒMost callers should use ”…”�”}”(hj+h²hh³Nh´Nubj)�”}”(hŒ&``hmm_range_fault_unlocked_timeout()``”h]”hŒ"hmm_range_fault_unlocked_timeout()”…”�”}”(hj3h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj+ubhŒU. If a driver really needs to hold the mmap lock across work outside HMM, it can use:”…”�”}”(hj+h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´Kêhjh²hubjÀ)�”}”(hŒ-int hmm_range_fault(struct hmm_range *range);”h]”hŒ-int hmm_range_fault(struct hmm_range *range);”…”�”}”hjKsbah}”(h]”h ]”h"]”h$]”h&]”jÏjÐuh1j¿h³hÊh´Kíhjh²hubhÌ)�”}”(hX1The mmap lock must be held by the caller and will remain held on return. This interface cannot support VMAs whose fault handlers need to drop the mmap lock. New callers should prefer ``hmm_range_fault_unlocked_timeout()`` unless they have a specific requirement to keep the mmap lock held across the call.”h]”(hŒ·The mmap lock must be held by the caller and will remain held on return. This interface cannot support VMAs whose fault handlers need to drop the mmap lock. New callers should prefer ”…”�”}”(hjYh²hh³Nh´Nubj)�”}”(hŒ&``hmm_range_fault_unlocked_timeout()``”h]”hŒ"hmm_range_fault_unlocked_timeout()”…”�”}”(hjah²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjYubhŒT unless they have a specific requirement to keep the mmap lock held across the call.”…”�”}”(hjYh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´Kïhjh²hubeh}”(h]”j¢ah ]”h"]”Œ'holding the mmap lock across hmm faults”ah$]”h&]”uh1hµhh·h²hh³hÊh´Kèubh¶)�”}”(hhh]”(h»)�”}”(hŒ)Leverage default_flags and pfn_flags_mask”h]”hŒ)Leverage default_flags and pfn_flags_mask”…”�”}”(hjƒh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”jzj¾uh1hºhj€h²hh³hÊh´KõubhÌ)�”}”(hŒ¾The hmm_range struct has 2 fields, default_flags and pfn_flags_mask, that specify fault or snapshot policy for the whole range instead of having to set them for each entry in the pfns array.”h]”hŒ¾The hmm_range struct has 2 fields, default_flags and pfn_flags_mask, that specify fault or snapshot policy for the whole range instead of having to set them for each entry in the pfns array.”…”�”}”(hj‘h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´K÷hj€h²hubhÌ)�”}”(hŒbFor instance if the device driver wants pages for a range with at least read permission, it sets::”h]”hŒaFor instance if the device driver wants pages for a range with at least read permission, it sets:”…”�”}”(hjŸh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´Kûhj€h²hubjÀ)�”}”(hŒDrange->default_flags = HMM_PFN_REQ_FAULT; range->pfn_flags_mask = 0;”h]”hŒDrange->default_flags = HMM_PFN_REQ_FAULT; range->pfn_flags_mask = 0;”…”�”}”hj­sbah}”(h]”h ]”h"]”h$]”h&]”jÏjÐuh1j¿h³hÊh´Kþhj€h²hubhÌ)�”}”(hŒ~and calls the HMM range fault helper as described above. This will fault all pages in the range with at least read permission.”h]”hŒ~and calls the HMM range fault helper as described above. This will fault all pages in the range with at least read permission.”…”�”}”(hj»h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´Mhj€h²hubhÌ)�”}”(hŒŒNow let's say the driver wants to do the same except for one page in the range for which it wants to have write permission. Now driver set::”h]”hŒ�Now let’s say the driver wants to do the same except for one page in the range for which it wants to have write permission. Now driver set:”…”�”}”(hjÉh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´Mhj€h²hubjÀ)�”}”(hŒ…range->default_flags = HMM_PFN_REQ_FAULT; range->pfn_flags_mask = HMM_PFN_REQ_WRITE; range->pfns[index_of_write] = HMM_PFN_REQ_WRITE;”h]”hŒ…range->default_flags = HMM_PFN_REQ_FAULT; range->pfn_flags_mask = HMM_PFN_REQ_WRITE; range->pfns[index_of_write] = HMM_PFN_REQ_WRITE;”…”�”}”hj×sbah}”(h]”h ]”h"]”h$]”h&]”jÏjÐuh1j¿h³hÊh´Mhj€h²hubhÌ)�”}”(hXWith this, HMM will fault in all pages with at least read (i.e., valid) and for the address == range->start + (index_of_write << PAGE_SHIFT) it will fault with write permission i.e., if the CPU pte does not have write permission set then HMM will call handle_mm_fault().”h]”hXWith this, HMM will fault in all pages with at least read (i.e., valid) and for the address == range->start + (index_of_write << PAGE_SHIFT) it will fault with write permission i.e., if the CPU pte does not have write permission set then HMM will call handle_mm_fault().”…”�”}”(hjåh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´M hj€h²hubhÌ)�”}”(hŒ¯After the HMM range fault helper completes the flag bits are set to the current state of the page tables, ie HMM_PFN_VALID | HMM_PFN_WRITE will be set if the page is writable.”h]”hŒ¯After the HMM range fault helper completes the flag bits are set to the current state of the page tables, ie HMM_PFN_VALID | HMM_PFN_WRITE will be set if the page is writable.”…”�”}”(hjóh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´Mhj€h²hubeh}”(h]”jÄah ]”h"]”Œ)leverage default_flags and pfn_flags_mask”ah$]”h&]”uh1hµhh·h²hh³hÊh´Kõubh¶)�”}”(hhh]”(h»)�”}”(hŒARepresent and manage device memory from core kernel point of view”h]”hŒARepresent and manage device memory from core kernel point of view”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”jzjàuh1hºhjh²hh³hÊh´MubhÌ)�”}”(hX¾Several different designs were tried to support device memory. The first one used a device specific data structure to keep information about migrated memory and HMM hooked itself in various places of mm code to handle any access to addresses that were backed by device memory. It turns out that this ended up replicating most of the fields of struct page and also needed many kernel code paths to be updated to understand this new kind of memory.”h]”hX¾Several different designs were tried to support device memory. The first one used a device specific data structure to keep information about migrated memory and HMM hooked itself in various places of mm code to handle any access to addresses that were backed by device memory. It turns out that this ended up replicating most of the fields of struct page and also needed many kernel code paths to be updated to understand this new kind of memory.”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´Mhjh²hubhÌ)�”}”(hXOMost kernel code paths never try to access the memory behind a page but only care about struct page contents. Because of this, HMM switched to directly using struct page for device memory which left most kernel code paths unaware of the difference. We only need to make sure that no one ever tries to map those pages from the CPU side.”h]”hXOMost kernel code paths never try to access the memory behind a page but only care about struct page contents. Because of this, HMM switched to directly using struct page for device memory which left most kernel code paths unaware of the difference. We only need to make sure that no one ever tries to map those pages from the CPU side.”…”�”}”(hj'h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´Mhjh²hubeh}”(h]”jæah ]”h"]”ŒArepresent and manage device memory from core kernel point of view”ah$]”h&]”uh1hµhh·h²hh³hÊh´Mubh¶)�”}”(hhh]”(h»)�”}”(hŒ#Migration to and from device memory”h]”hŒ#Migration to and from device memory”…”�”}”(hj?h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”jzjuh1hºhj<h²hh³hÊh´M&ubhÌ)�”}”(hXGBecause the CPU cannot access device memory directly, the device driver must use hardware DMA or device specific load/store instructions to migrate data. The migrate_vma_setup(), migrate_vma_pages(), and migrate_vma_finalize() functions are designed to make drivers easier to write and to centralize common code across drivers.”h]”hXGBecause the CPU cannot access device memory directly, the device driver must use hardware DMA or device specific load/store instructions to migrate data. The migrate_vma_setup(), migrate_vma_pages(), and migrate_vma_finalize() functions are designed to make drivers easier to write and to centralize common code across drivers.”…”�”}”(hjMh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´M(hj<h²hubhÌ)�”}”(hXBefore migrating pages to device private memory, special device private ``struct page`` needs to be created. These will be used as special "swap" page table entries so that a CPU process will fault if it tries to access a page that has been migrated to device private memory.”h]”(hŒHBefore migrating pages to device private memory, special device private ”…”�”}”(hj[h²hh³Nh´Nubj)�”}”(hŒ``struct page``”h]”hŒ struct page”…”�”}”(hjch²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj[ubhŒÀ needs to be created. These will be used as special “swapâ€� page table entries so that a CPU process will fault if it tries to access a page that has been migrated to device private memory.”…”�”}”(hj[h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´M.hj<h²hubhÌ)�”}”(hŒ'These can be allocated and freed with::”h]”hŒ&These can be allocated and freed with:”…”�”}”(hj{h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´M3hj<h²hubjÀ)�”}”(hXÙstruct resource *res; struct dev_pagemap pagemap; res = request_free_mem_region(&iomem_resource, /* number of bytes */, "name of driver resource"); pagemap.type = MEMORY_DEVICE_PRIVATE; pagemap.range.start = res->start; pagemap.range.end = res->end; pagemap.nr_range = 1; pagemap.ops = &device_devmem_ops; memremap_pages(&pagemap, numa_node_id()); memunmap_pages(&pagemap); release_mem_region(pagemap.range.start, range_len(&pagemap.range));”h]”hXÙstruct resource *res; struct dev_pagemap pagemap; res = request_free_mem_region(&iomem_resource, /* number of bytes */, "name of driver resource"); pagemap.type = MEMORY_DEVICE_PRIVATE; pagemap.range.start = res->start; pagemap.range.end = res->end; pagemap.nr_range = 1; pagemap.ops = &device_devmem_ops; memremap_pages(&pagemap, numa_node_id()); memunmap_pages(&pagemap); release_mem_region(pagemap.range.start, range_len(&pagemap.range));”…”�”}”hj‰sbah}”(h]”h ]”h"]”h$]”h&]”jÏjÐuh1j¿h³hÊh´M5hj<h²hubhÌ)�”}”(hŒ±There are also devm_request_free_mem_region(), devm_memremap_pages(), devm_memunmap_pages(), and devm_release_mem_region() when the resources can be tied to a ``struct device``.”h]”(hŒŸThere are also devm_request_free_mem_region(), devm_memremap_pages(), devm_memunmap_pages(), and devm_release_mem_region() when the resources can be tied to a ”…”�”}”(hj—h²hh³Nh´Nubj)�”}”(hŒ``struct device``”h]”hŒ struct device”…”�”}”(hjŸh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj—ubhŒ.”…”�”}”(hj—h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´MDhj<h²hubhÌ)�”}”(hŒÒThe overall migration steps are similar to migrating NUMA pages within system memory (see Documentation/mm/page_migration.rst) but the steps are split between device driver specific code and shared common code:”h]”hŒÒThe overall migration steps are similar to migrating NUMA pages within system memory (see Documentation/mm/page_migration.rst) but the steps are split between device driver specific code and shared common code:”…”�”}”(hj·h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´MHhj<h²hubhŒenumerated_list”“”)�”}”(hhh]”(j)�”}”(hŒÈ``mmap_read_lock()`` The device driver has to pass a ``struct vm_area_struct`` to migrate_vma_setup() so the mmap_read_lock() or mmap_write_lock() needs to be held for the duration of the migration. ”h]”(hÌ)�”}”(hŒ``mmap_read_lock()``”h]”j)�”}”(hjÐh]”hŒmmap_read_lock()”…”�”}”(hjÒh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjÎubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´MLhjÊubhÌ)�”}”(hŒ±The device driver has to pass a ``struct vm_area_struct`` to migrate_vma_setup() so the mmap_read_lock() or mmap_write_lock() needs to be held for the duration of the migration.”h]”(hŒ The device driver has to pass a ”…”�”}”(hjåh²hh³Nh´Nubj)�”}”(hŒ``struct vm_area_struct``”h]”hŒstruct vm_area_struct”…”�”}”(hjíh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjåubhŒx to migrate_vma_setup() so the mmap_read_lock() or mmap_write_lock() needs to be held for the duration of the migration.”…”�”}”(hjåh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´MNhjÊubeh}”(h]”h ]”h"]”h$]”h&]”uh1jhjÇh²hh³hÊh´Nubj)�”}”(hXã``migrate_vma_setup(struct migrate_vma *args)`` The device driver initializes the ``struct migrate_vma`` fields and passes the pointer to migrate_vma_setup(). The ``args->flags`` field is used to filter which source pages should be migrated. For example, setting ``MIGRATE_VMA_SELECT_SYSTEM`` will only migrate system memory and ``MIGRATE_VMA_SELECT_DEVICE_PRIVATE`` will only migrate pages residing in device private memory. If the latter flag is set, the ``args->pgmap_owner`` field is used to identify device private pages owned by the driver. This avoids trying to migrate device private pages residing in other devices. Currently only anonymous private VMA ranges can be migrated to or from system memory and device private memory. One of the first steps migrate_vma_setup() does is to invalidate other device's MMUs with the ``mmu_notifier_invalidate_range_start()`` and ``mmu_notifier_invalidate_range_end()`` calls around the page table walks to fill in the ``args->src`` array with PFNs to be migrated. The ``invalidate_range_start()`` callback is passed a ``struct mmu_notifier_range`` with the ``event`` field set to ``MMU_NOTIFY_MIGRATE`` and the ``owner`` field set to the ``args->pgmap_owner`` field passed to migrate_vma_setup(). This allows the device driver to skip the invalidation callback and only invalidate device private MMU mappings that are actually migrating. This is explained more in the next section. While walking the page tables, a ``pte_none()`` or ``is_zero_pfn()`` entry results in a valid "zero" PFN stored in the ``args->src`` array. This lets the driver allocate device private memory and clear it instead of copying a page of zeros. Valid PTE entries to system memory or device private struct pages will be locked with ``lock_page()``, isolated from the LRU (if system memory since device private pages are not on the LRU), unmapped from the process, and a special migration PTE is inserted in place of the original PTE. migrate_vma_setup() also clears the ``args->dst`` array. ”h]”(hÌ)�”}”(hŒ/``migrate_vma_setup(struct migrate_vma *args)``”h]”j)�”}”(hjh]”hŒ+migrate_vma_setup(struct migrate_vma *args)”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´MRhj ubhÌ)�”}”(hX°The device driver initializes the ``struct migrate_vma`` fields and passes the pointer to migrate_vma_setup(). The ``args->flags`` field is used to filter which source pages should be migrated. For example, setting ``MIGRATE_VMA_SELECT_SYSTEM`` will only migrate system memory and ``MIGRATE_VMA_SELECT_DEVICE_PRIVATE`` will only migrate pages residing in device private memory. If the latter flag is set, the ``args->pgmap_owner`` field is used to identify device private pages owned by the driver. This avoids trying to migrate device private pages residing in other devices. Currently only anonymous private VMA ranges can be migrated to or from system memory and device private memory.”h]”(hŒ"The device driver initializes the ”…”�”}”(hj&h²hh³Nh´Nubj)�”}”(hŒ``struct migrate_vma``”h]”hŒstruct migrate_vma”…”�”}”(hj.h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj&ubhŒ; fields and passes the pointer to migrate_vma_setup(). The ”…”�”}”(hj&h²hh³Nh´Nubj)�”}”(hŒ``args->flags``”h]”hŒ args->flags”…”�”}”(hj@h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj&ubhŒU field is used to filter which source pages should be migrated. For example, setting ”…”�”}”(hj&h²hh³Nh´Nubj)�”}”(hŒ``MIGRATE_VMA_SELECT_SYSTEM``”h]”hŒMIGRATE_VMA_SELECT_SYSTEM”…”�”}”(hjRh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj&ubhŒ% will only migrate system memory and ”…”�”}”(hj&h²hh³Nh´Nubj)�”}”(hŒ%``MIGRATE_VMA_SELECT_DEVICE_PRIVATE``”h]”hŒ!MIGRATE_VMA_SELECT_DEVICE_PRIVATE”…”�”}”(hjdh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj&ubhŒ[ will only migrate pages residing in device private memory. If the latter flag is set, the ”…”�”}”(hj&h²hh³Nh´Nubj)�”}”(hŒ``args->pgmap_owner``”h]”hŒargs->pgmap_owner”…”�”}”(hjvh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj&ubhX field is used to identify device private pages owned by the driver. This avoids trying to migrate device private pages residing in other devices. Currently only anonymous private VMA ranges can be migrated to or from system memory and device private memory.”…”�”}”(hj&h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´MThj ubhÌ)�”}”(hX´One of the first steps migrate_vma_setup() does is to invalidate other device's MMUs with the ``mmu_notifier_invalidate_range_start()`` and ``mmu_notifier_invalidate_range_end()`` calls around the page table walks to fill in the ``args->src`` array with PFNs to be migrated. The ``invalidate_range_start()`` callback is passed a ``struct mmu_notifier_range`` with the ``event`` field set to ``MMU_NOTIFY_MIGRATE`` and the ``owner`` field set to the ``args->pgmap_owner`` field passed to migrate_vma_setup(). This allows the device driver to skip the invalidation callback and only invalidate device private MMU mappings that are actually migrating. This is explained more in the next section.”h]”(hŒ`One of the first steps migrate_vma_setup() does is to invalidate other device’s MMUs with the ”…”�”}”(hjŽh²hh³Nh´Nubj)�”}”(hŒ)``mmu_notifier_invalidate_range_start()``”h]”hŒ%mmu_notifier_invalidate_range_start()”…”�”}”(hj–h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjŽubhŒ and ”…”�”}”(hjŽh²hh³Nh´Nubj)�”}”(hŒ'``mmu_notifier_invalidate_range_end()``”h]”hŒ#mmu_notifier_invalidate_range_end()”…”�”}”(hj¨h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjŽubhŒ2 calls around the page table walks to fill in the ”…”�”}”(hjŽh²hh³Nh´Nubj)�”}”(hŒ ``args->src``”h]”hŒ args->src”…”�”}”(hjºh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjŽubhŒ% array with PFNs to be migrated. The ”…”�”}”(hjŽh²hh³Nh´Nubj)�”}”(hŒ``invalidate_range_start()``”h]”hŒinvalidate_range_start()”…”�”}”(hjÌh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjŽubhŒ callback is passed a ”…”�”}”(hjŽh²hh³Nh´Nubj)�”}”(hŒ``struct mmu_notifier_range``”h]”hŒstruct mmu_notifier_range”…”�”}”(hjÞh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjŽubhŒ with the ”…”�”}”(hjŽh²hh³Nh´Nubj)�”}”(hŒ ``event``”h]”hŒevent”…”�”}”(hjðh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjŽubhŒ field set to ”…”�”}”(hjŽh²hh³Nh´Nubj)�”}”(hŒ``MMU_NOTIFY_MIGRATE``”h]”hŒMMU_NOTIFY_MIGRATE”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjŽubhŒ and the ”…”�”}”(hjŽh²hh³Nh´Nubj)�”}”(hŒ ``owner``”h]”hŒowner”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjŽubhŒ field set to the ”…”�”}”(hjŽh²hh³Nh´Nubj)�”}”(hŒ``args->pgmap_owner``”h]”hŒargs->pgmap_owner”…”�”}”(hj&h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjŽubhŒÞ field passed to migrate_vma_setup(). This allows the device driver to skip the invalidation callback and only invalidate device private MMU mappings that are actually migrating. This is explained more in the next section.”…”�”}”(hjŽh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´M_hj ubhÌ)�”}”(hXIWhile walking the page tables, a ``pte_none()`` or ``is_zero_pfn()`` entry results in a valid "zero" PFN stored in the ``args->src`` array. This lets the driver allocate device private memory and clear it instead of copying a page of zeros. Valid PTE entries to system memory or device private struct pages will be locked with ``lock_page()``, isolated from the LRU (if system memory since device private pages are not on the LRU), unmapped from the process, and a special migration PTE is inserted in place of the original PTE. migrate_vma_setup() also clears the ``args->dst`` array.”h]”(hŒ!While walking the page tables, a ”…”�”}”(hj>h²hh³Nh´Nubj)�”}”(hŒ``pte_none()``”h]”hŒ pte_none()”…”�”}”(hjFh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj>ubhŒ or ”…”�”}”(hj>h²hh³Nh´Nubj)�”}”(hŒ``is_zero_pfn()``”h]”hŒ is_zero_pfn()”…”�”}”(hjXh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj>ubhŒ7 entry results in a valid “zeroâ€� PFN stored in the ”…”�”}”(hj>h²hh³Nh´Nubj)�”}”(hŒ ``args->src``”h]”hŒ args->src”…”�”}”(hjjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj>ubhŒÃ array. This lets the driver allocate device private memory and clear it instead of copying a page of zeros. Valid PTE entries to system memory or device private struct pages will be locked with ”…”�”}”(hj>h²hh³Nh´Nubj)�”}”(hŒ``lock_page()``”h]”hŒ lock_page()”…”�”}”(hj|h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj>ubhŒß, isolated from the LRU (if system memory since device private pages are not on the LRU), unmapped from the process, and a special migration PTE is inserted in place of the original PTE. migrate_vma_setup() also clears the ”…”�”}”(hj>h²hh³Nh´Nubj)�”}”(hŒ ``args->dst``”h]”hŒ args->dst”…”�”}”(hjŽh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj>ubhŒ array.”…”�”}”(hj>h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´Mkhj ubeh}”(h]”h ]”h"]”h$]”h&]”uh1jhjÇh²hh³hÊh´Nubj)�”}”(hXoThe device driver allocates destination pages and copies source pages to destination pages. The driver checks each ``src`` entry to see if the ``MIGRATE_PFN_MIGRATE`` bit is set and skips entries that are not migrating. The device driver can also choose to skip migrating a page by not filling in the ``dst`` array for that page. The driver then allocates either a device private struct page or a system memory page, locks the page with ``lock_page()``, and fills in the ``dst`` array entry with:: dst[i] = migrate_pfn(page_to_pfn(dpage)); Now that the driver knows that this page is being migrated, it can invalidate device private MMU mappings and copy device private memory to system memory or another device private page. The core Linux kernel handles CPU page table invalidations so the device driver only has to invalidate its own MMU mappings. The driver can use ``migrate_pfn_to_page(src[i])`` to get the ``struct page`` of the source and either copy the source page to the destination or clear the destination device private memory if the pointer is ``NULL`` meaning the source page was not populated in system memory. ”h]”(hÌ)�”}”(hŒ[The device driver allocates destination pages and copies source pages to destination pages.”h]”hŒ[The device driver allocates destination pages and copies source pages to destination pages.”…”�”}”(hj°h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´Muhj¬ubhÌ)�”}”(hŒíThe driver checks each ``src`` entry to see if the ``MIGRATE_PFN_MIGRATE`` bit is set and skips entries that are not migrating. The device driver can also choose to skip migrating a page by not filling in the ``dst`` array for that page.”h]”(hŒThe driver checks each ”…”�”}”(hj¾h²hh³Nh´Nubj)�”}”(hŒ``src``”h]”hŒsrc”…”�”}”(hjÆh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj¾ubhŒ entry to see if the ”…”�”}”(hj¾h²hh³Nh´Nubj)�”}”(hŒ``MIGRATE_PFN_MIGRATE``”h]”hŒMIGRATE_PFN_MIGRATE”…”�”}”(hjØh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj¾ubhŒ‡ bit is set and skips entries that are not migrating. The device driver can also choose to skip migrating a page by not filling in the ”…”�”}”(hj¾h²hh³Nh´Nubj)�”}”(hŒ``dst``”h]”hŒdst”…”�”}”(hjêh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj¾ubhŒ array for that page.”…”�”}”(hj¾h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´Mxhj¬ubhÌ)�”}”(hŒ§The driver then allocates either a device private struct page or a system memory page, locks the page with ``lock_page()``, and fills in the ``dst`` array entry with::”h]”(hŒkThe driver then allocates either a device private struct page or a system memory page, locks the page with ”…”�”}”(hj h²hh³Nh´Nubj)�”}”(hŒ``lock_page()``”h]”hŒ lock_page()”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj ubhŒ, and fills in the ”…”�”}”(hj h²hh³Nh´Nubj)�”}”(hŒ``dst``”h]”hŒdst”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj ubhŒ array entry with:”…”�”}”(hj h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´M}hj¬ubjÀ)�”}”(hŒ)dst[i] = migrate_pfn(page_to_pfn(dpage));”h]”hŒ)dst[i] = migrate_pfn(page_to_pfn(dpage));”…”�”}”hj4 sbah}”(h]”h ]”h"]”h$]”h&]”jÏjÐuh1j¿h³hÊh´M�hj¬ubhÌ)�”}”(hX6Now that the driver knows that this page is being migrated, it can invalidate device private MMU mappings and copy device private memory to system memory or another device private page. The core Linux kernel handles CPU page table invalidations so the device driver only has to invalidate its own MMU mappings.”h]”hX6Now that the driver knows that this page is being migrated, it can invalidate device private MMU mappings and copy device private memory to system memory or another device private page. The core Linux kernel handles CPU page table invalidations so the device driver only has to invalidate its own MMU mappings.”…”�”}”(hjB h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´Mƒhj¬ubhÌ)�”}”(hXThe driver can use ``migrate_pfn_to_page(src[i])`` to get the ``struct page`` of the source and either copy the source page to the destination or clear the destination device private memory if the pointer is ``NULL`` meaning the source page was not populated in system memory.”h]”(hŒThe driver can use ”…”�”}”(hjP h²hh³Nh´Nubj)�”}”(hŒ``migrate_pfn_to_page(src[i])``”h]”hŒmigrate_pfn_to_page(src[i])”…”�”}”(hjX h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjP ubhŒ to get the ”…”�”}”(hjP h²hh³Nh´Nubj)�”}”(hŒ``struct page``”h]”hŒ struct page”…”�”}”(hjj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjP ubhŒƒ of the source and either copy the source page to the destination or clear the destination device private memory if the pointer is ”…”�”}”(hjP h²hh³Nh´Nubj)�”}”(hŒ``NULL``”h]”hŒNULL”…”�”}”(hj| h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjP ubhŒ< meaning the source page was not populated in system memory.”…”�”}”(hjP h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´M‰hj¬ubeh}”(h]”h ]”h"]”h$]”h&]”uh1jhjÇh²hh³hÊh´Nubj)�”}”(hX|``migrate_vma_pages()`` This step is where the migration is actually "committed". If the source page was a ``pte_none()`` or ``is_zero_pfn()`` page, this is where the newly allocated page is inserted into the CPU's page table. This can fail if a CPU thread faults on the same page. However, the page table is locked and only one of the new pages will be inserted. The device driver will see that the ``MIGRATE_PFN_MIGRATE`` bit is cleared if it loses the race. If the source page was locked, isolated, etc. the source ``struct page`` information is now copied to destination ``struct page`` finalizing the migration on the CPU side. ”h]”(hÌ)�”}”(hŒ``migrate_vma_pages()``”h]”j)�”}”(hj  h]”hŒmigrate_vma_pages()”…”�”}”(hj¢ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjž ubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´MŽhjš ubhÌ)�”}”(hŒ9This step is where the migration is actually "committed".”h]”hŒ=This step is where the migration is actually “committedâ€�.”…”�”}”(hjµ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´M�hjš ubhÌ)�”}”(hXzIf the source page was a ``pte_none()`` or ``is_zero_pfn()`` page, this is where the newly allocated page is inserted into the CPU's page table. This can fail if a CPU thread faults on the same page. However, the page table is locked and only one of the new pages will be inserted. The device driver will see that the ``MIGRATE_PFN_MIGRATE`` bit is cleared if it loses the race.”h]”(hŒIf the source page was a ”…”�”}”(hjà h²hh³Nh´Nubj)�”}”(hŒ``pte_none()``”h]”hŒ pte_none()”…”�”}”(hjË h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjà ubhŒ or ”…”�”}”(hjà h²hh³Nh´Nubj)�”}”(hŒ``is_zero_pfn()``”h]”hŒ is_zero_pfn()”…”�”}”(hjÝ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjà ubhX page, this is where the newly allocated page is inserted into the CPU’s page table. This can fail if a CPU thread faults on the same page. However, the page table is locked and only one of the new pages will be inserted. The device driver will see that the ”…”�”}”(hjà h²hh³Nh´Nubj)�”}”(hŒ``MIGRATE_PFN_MIGRATE``”h]”hŒMIGRATE_PFN_MIGRATE”…”�”}”(hjï h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjà ubhŒ% bit is cleared if it loses the race.”…”�”}”(hjà h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´M’hjš ubhÌ)�”}”(hŒ«If the source page was locked, isolated, etc. the source ``struct page`` information is now copied to destination ``struct page`` finalizing the migration on the CPU side.”h]”(hŒ9If the source page was locked, isolated, etc. the source ”…”�”}”(hj h²hh³Nh´Nubj)�”}”(hŒ``struct page``”h]”hŒ struct page”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj ubhŒ* information is now copied to destination ”…”�”}”(hj h²hh³Nh´Nubj)�”}”(hŒ``struct page``”h]”hŒ struct page”…”�”}”(hj! h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj ubhŒ* finalizing the migration on the CPU side.”…”�”}”(hj h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´M™hjš ubeh}”(h]”h ]”h"]”h$]”h&]”uh1jhjÇh²hh³hÊh´Nubj)�”}”(hXDevice driver updates device MMU page tables for pages still migrating, rolling back pages not migrating. If the ``src`` entry still has ``MIGRATE_PFN_MIGRATE`` bit set, the device driver can update the device MMU and set the write enable bit if the ``MIGRATE_PFN_WRITE`` bit is set. ”h]”(hÌ)�”}”(hŒiDevice driver updates device MMU page tables for pages still migrating, rolling back pages not migrating.”h]”hŒiDevice driver updates device MMU page tables for pages still migrating, rolling back pages not migrating.”…”�”}”(hjC h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´M�hj? ubhÌ)�”}”(hŒ±If the ``src`` entry still has ``MIGRATE_PFN_MIGRATE`` bit set, the device driver can update the device MMU and set the write enable bit if the ``MIGRATE_PFN_WRITE`` bit is set.”h]”(hŒIf the ”…”�”}”(hjQ h²hh³Nh´Nubj)�”}”(hŒ``src``”h]”hŒsrc”…”�”}”(hjY h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjQ ubhŒ entry still has ”…”�”}”(hjQ h²hh³Nh´Nubj)�”}”(hŒ``MIGRATE_PFN_MIGRATE``”h]”hŒMIGRATE_PFN_MIGRATE”…”�”}”(hjk h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjQ ubhŒZ bit set, the device driver can update the device MMU and set the write enable bit if the ”…”�”}”(hjQ h²hh³Nh´Nubj)�”}”(hŒ``MIGRATE_PFN_WRITE``”h]”hŒMIGRATE_PFN_WRITE”…”�”}”(hj} h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjQ ubhŒ bit is set.”…”�”}”(hjQ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´M hj? ubeh}”(h]”h ]”h"]”h$]”h&]”uh1jhjÇh²hh³hÊh´Nubj)�”}”(hŒÅ``migrate_vma_finalize()`` This step replaces the special migration page table entry with the new page's page table entry and releases the reference to the source and destination ``struct page``. ”h]”(hÌ)�”}”(hŒ``migrate_vma_finalize()``”h]”j)�”}”(hj¡ h]”hŒmigrate_vma_finalize()”…”�”}”(hj£ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjŸ ubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´M¤hj› ubhÌ)�”}”(hŒ¨This step replaces the special migration page table entry with the new page's page table entry and releases the reference to the source and destination ``struct page``.”h]”(hŒšThis step replaces the special migration page table entry with the new page’s page table entry and releases the reference to the source and destination ”…”�”}”(hj¶ h²hh³Nh´Nubj)�”}”(hŒ``struct page``”h]”hŒ struct page”…”�”}”(hj¾ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj¶ ubhŒ.”…”�”}”(hj¶ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´M¦hj› ubeh}”(h]”h ]”h"]”h$]”h&]”uh1jhjÇh²hh³hÊh´Nubj)�”}”(•ž#hŒ6``mmap_read_unlock()`` The lock can now be released. ”h]”(hÌ)�”}”(hŒ``mmap_read_unlock()``”h]”j)�”}”(hjâ h]”hŒmmap_read_unlock()”…”�”}”(hjä h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjà ubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´MªhjÜ ubhÌ)�”}”(hŒThe lock can now be released.”h]”hŒThe lock can now be released.”…”�”}”(hj÷ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´M¬hjÜ ubeh}”(h]”h ]”h"]”h$]”h&]”uh1jhjÇh²hh³hÊh´Nubeh}”(h]”h ]”h"]”h$]”h&]”Œenumtype”Œarabic”Œprefix”hŒsuffix”Œ.”uh1jÅhj<h²hh³hÊh´MLubeh}”(h]”jah ]”h"]”Œ#migration to and from device memory”ah$]”h&]”uh1hµhh·h²hh³hÊh´M&ubh¶)�”}”(hhh]”(h»)�”}”(hŒExclusive access memory”h]”hŒExclusive access memory”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”jzj$uh1hºhj h²hh³hÊh´M¯ubhÌ)�”}”(hXwSome devices have features such as atomic PTE bits that can be used to implement atomic access to system memory. To support atomic operations to a shared virtual memory page such a device needs access to that page which is exclusive of any userspace access from the CPU. The ``make_device_exclusive()`` function can be used to make a memory range inaccessible from userspace.”h]”(hXSome devices have features such as atomic PTE bits that can be used to implement atomic access to system memory. To support atomic operations to a shared virtual memory page such a device needs access to that page which is exclusive of any userspace access from the CPU. The ”…”�”}”(hj. h²hh³Nh´Nubj)�”}”(hŒ``make_device_exclusive()``”h]”hŒmake_device_exclusive()”…”�”}”(hj6 h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj. ubhŒI function can be used to make a memory range inaccessible from userspace.”…”�”}”(hj. h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´M±hj h²hubhÌ)�”}”(hXThis replaces all mappings for pages in the given range with special swap entries. Any attempt to access the swap entry results in a fault which is resolved by replacing the entry with the original mapping. A driver gets notified that the mapping has been changed by MMU notifiers, after which point it will no longer have exclusive access to the page. Exclusive access is guaranteed to last until the driver drops the page lock and page reference, at which point any CPU faults on the page may proceed as described.”h]”hXThis replaces all mappings for pages in the given range with special swap entries. Any attempt to access the swap entry results in a fault which is resolved by replacing the entry with the original mapping. A driver gets notified that the mapping has been changed by MMU notifiers, after which point it will no longer have exclusive access to the page. Exclusive access is guaranteed to last until the driver drops the page lock and page reference, at which point any CPU faults on the page may proceed as described.”…”�”}”(hjN h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´M·hj h²hubeh}”(h]”j*ah ]”h"]”Œexclusive access memory”ah$]”h&]”uh1hµhh·h²hh³hÊh´M¯ubh¶)�”}”(hhh]”(h»)�”}”(hŒ(Memory cgroup (memcg) and rss accounting”h]”hŒ(Memory cgroup (memcg) and rss accounting”…”�”}”(hjf h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”jzjFuh1hºhjc h²hh³hÊh´MÀubhÌ)�”}”(hXsFor now, device memory is accounted as any regular page in rss counters (either anonymous if device page is used for anonymous, file if device page is used for file backed page, or shmem if device page is used for shared memory). This is a deliberate choice to keep existing applications, that might start using device memory without knowing about it, running unimpacted.”h]”hXsFor now, device memory is accounted as any regular page in rss counters (either anonymous if device page is used for anonymous, file if device page is used for file backed page, or shmem if device page is used for shared memory). This is a deliberate choice to keep existing applications, that might start using device memory without knowing about it, running unimpacted.”…”�”}”(hjt h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´MÂhjc h²hubhÌ)�”}”(hXgA drawback is that the OOM killer might kill an application using a lot of device memory and not a lot of regular system memory and thus not freeing much system memory. We want to gather more real world experience on how applications and system react under memory pressure in the presence of device memory before deciding to account device memory differently.”h]”hXgA drawback is that the OOM killer might kill an application using a lot of device memory and not a lot of regular system memory and thus not freeing much system memory. We want to gather more real world experience on how applications and system react under memory pressure in the presence of device memory before deciding to account device memory differently.”…”�”}”(hj‚ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´MÈhjc h²hubhÌ)�”}”(hXÒSame decision was made for memory cgroup. Device memory pages are accounted against same memory cgroup a regular page would be accounted to. This does simplify migration to and from device memory. This also means that migration back from device memory to regular memory cannot fail because it would go above memory cgroup limit. We might revisit this choice later on once we get more experience in how device memory is used and its impact on memory resource control.”h]”hXÒSame decision was made for memory cgroup. Device memory pages are accounted against same memory cgroup a regular page would be accounted to. This does simplify migration to and from device memory. This also means that migration back from device memory to regular memory cannot fail because it would go above memory cgroup limit. We might revisit this choice later on once we get more experience in how device memory is used and its impact on memory resource control.”…”�”}”(hj� h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´MÏhjc h²hubhÌ)�”}”(hŒ×Note that device memory can never be pinned by a device driver nor through GUP and thus such memory is always free upon process exit. Or when last reference is dropped in case of shared memory or file backed memory.”h]”hŒ×Note that device memory can never be pinned by a device driver nor through GUP and thus such memory is always free upon process exit. Or when last reference is dropped in case of shared memory or file backed memory.”…”�”}”(hjž h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hËh³hÊh´MØhjc h²hubeh}”(h]”jLah ]”h"]”Œ(memory cgroup (memcg) and rss accounting”ah$]”h&]”uh1hµhh·h²hh³hÊh´MÀubeh}”(h]”Œ#heterogeneous-memory-management-hmm”ah ]”h"]”Œ%heterogeneous memory management (hmm)”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”}”Œrefids”}”Œnameids”}”(j¸ jµ jfjajájj?j<j�j^jj€j}j¢jjÄj9jæj jj` j*j° jLuŒ nametypes”}”(j¸ ‰jf‰já‰j?‰j�‰j‰j}‰j‰j9‰j ‰j` ‰j° ‰uh}”(jµ h·jahùjjij<jäj^jBj€j j¢jjÄj€jæjjj<j*j jLjc jj j6j-jXjOjzjqjœj“j¾jµjàj×jjùj$jjFj=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”“”}”jì K s…”R”Œparse_messages”]”Œtransform_messages”]”Œ transformer”NŒ include_log”]”Œ decoration”Nh²hub.