blob: cbe9d0c66650452ba3e4a57b6af6c7f8f0940f56 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * Copyright 2013 Red Hat Inc.
4 *
David Brazdil0f672f62019-12-10 10:32:29 +00005 * Authors: Jérôme Glisse <jglisse@redhat.com>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006 */
7/*
8 * Refer to include/linux/hmm.h for information about heterogeneous memory
9 * management or HMM for short.
10 */
David Brazdil0f672f62019-12-10 10:32:29 +000011#include <linux/pagewalk.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000012#include <linux/hmm.h>
13#include <linux/init.h>
14#include <linux/rmap.h>
15#include <linux/swap.h>
16#include <linux/slab.h>
17#include <linux/sched.h>
18#include <linux/mmzone.h>
19#include <linux/pagemap.h>
20#include <linux/swapops.h>
21#include <linux/hugetlb.h>
22#include <linux/memremap.h>
David Brazdil0f672f62019-12-10 10:32:29 +000023#include <linux/sched/mm.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000024#include <linux/jump_label.h>
David Brazdil0f672f62019-12-10 10:32:29 +000025#include <linux/dma-mapping.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000026#include <linux/mmu_notifier.h>
27#include <linux/memory_hotplug.h>
28
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000029struct hmm_vma_walk {
30 struct hmm_range *range;
31 unsigned long last;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000032};
33
Olivier Deprez157378f2022-04-04 15:47:50 +020034enum {
35 HMM_NEED_FAULT = 1 << 0,
36 HMM_NEED_WRITE_FAULT = 1 << 1,
37 HMM_NEED_ALL_BITS = HMM_NEED_FAULT | HMM_NEED_WRITE_FAULT,
38};
39
40static int hmm_pfns_fill(unsigned long addr, unsigned long end,
41 struct hmm_range *range, unsigned long cpu_flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000042{
Olivier Deprez157378f2022-04-04 15:47:50 +020043 unsigned long i = (addr - range->start) >> PAGE_SHIFT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000044
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000045 for (; addr < end; addr += PAGE_SIZE, i++)
Olivier Deprez157378f2022-04-04 15:47:50 +020046 range->hmm_pfns[i] = cpu_flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000047 return 0;
48}
49
50/*
Olivier Deprez157378f2022-04-04 15:47:50 +020051 * hmm_vma_fault() - fault in a range lacking valid pmd or pte(s)
David Brazdil0f672f62019-12-10 10:32:29 +000052 * @addr: range virtual start address (inclusive)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000053 * @end: range virtual end address (exclusive)
Olivier Deprez157378f2022-04-04 15:47:50 +020054 * @required_fault: HMM_NEED_* flags
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000055 * @walk: mm_walk structure
Olivier Deprez157378f2022-04-04 15:47:50 +020056 * Return: -EBUSY after page fault, or page fault error
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000057 *
58 * This function will be called whenever pmd_none() or pte_none() returns true,
59 * or whenever there is no page directory covering the virtual address range.
60 */
Olivier Deprez157378f2022-04-04 15:47:50 +020061static int hmm_vma_fault(unsigned long addr, unsigned long end,
62 unsigned int required_fault, struct mm_walk *walk)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000063{
64 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Olivier Deprez157378f2022-04-04 15:47:50 +020065 struct vm_area_struct *vma = walk->vma;
66 unsigned int fault_flags = FAULT_FLAG_REMOTE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000067
Olivier Deprez157378f2022-04-04 15:47:50 +020068 WARN_ON_ONCE(!required_fault);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000069 hmm_vma_walk->last = addr;
David Brazdil0f672f62019-12-10 10:32:29 +000070
Olivier Deprez157378f2022-04-04 15:47:50 +020071 if (required_fault & HMM_NEED_WRITE_FAULT) {
72 if (!(vma->vm_flags & VM_WRITE))
73 return -EPERM;
74 fault_flags |= FAULT_FLAG_WRITE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000075 }
76
Olivier Deprez157378f2022-04-04 15:47:50 +020077 for (; addr < end; addr += PAGE_SIZE)
78 if (handle_mm_fault(vma, addr, fault_flags, NULL) &
79 VM_FAULT_ERROR)
80 return -EFAULT;
81 return -EBUSY;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000082}
83
Olivier Deprez157378f2022-04-04 15:47:50 +020084static unsigned int hmm_pte_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
85 unsigned long pfn_req_flags,
86 unsigned long cpu_flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000087{
88 struct hmm_range *range = hmm_vma_walk->range;
89
David Brazdil0f672f62019-12-10 10:32:29 +000090 /*
91 * So we not only consider the individual per page request we also
92 * consider the default flags requested for the range. The API can
93 * be used 2 ways. The first one where the HMM user coalesces
94 * multiple page faults into one request and sets flags per pfn for
95 * those faults. The second one where the HMM user wants to pre-
96 * fault a range with specific flags. For the latter one it is a
97 * waste to have the user pre-fill the pfn arrays with a default
98 * flags value.
99 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200100 pfn_req_flags &= range->pfn_flags_mask;
101 pfn_req_flags |= range->default_flags;
David Brazdil0f672f62019-12-10 10:32:29 +0000102
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000103 /* We aren't ask to do anything ... */
Olivier Deprez157378f2022-04-04 15:47:50 +0200104 if (!(pfn_req_flags & HMM_PFN_REQ_FAULT))
105 return 0;
106
107 /* Need to write fault ? */
108 if ((pfn_req_flags & HMM_PFN_REQ_WRITE) &&
109 !(cpu_flags & HMM_PFN_WRITE))
110 return HMM_NEED_FAULT | HMM_NEED_WRITE_FAULT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000111
112 /* If CPU page table is not valid then we need to fault */
Olivier Deprez157378f2022-04-04 15:47:50 +0200113 if (!(cpu_flags & HMM_PFN_VALID))
114 return HMM_NEED_FAULT;
115 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000116}
117
Olivier Deprez157378f2022-04-04 15:47:50 +0200118static unsigned int
119hmm_range_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
120 const unsigned long hmm_pfns[], unsigned long npages,
121 unsigned long cpu_flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000122{
Olivier Deprez157378f2022-04-04 15:47:50 +0200123 struct hmm_range *range = hmm_vma_walk->range;
124 unsigned int required_fault = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000125 unsigned long i;
126
Olivier Deprez157378f2022-04-04 15:47:50 +0200127 /*
128 * If the default flags do not request to fault pages, and the mask does
129 * not allow for individual pages to be faulted, then
130 * hmm_pte_need_fault() will always return 0.
131 */
132 if (!((range->default_flags | range->pfn_flags_mask) &
133 HMM_PFN_REQ_FAULT))
134 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000135
136 for (i = 0; i < npages; ++i) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200137 required_fault |= hmm_pte_need_fault(hmm_vma_walk, hmm_pfns[i],
138 cpu_flags);
139 if (required_fault == HMM_NEED_ALL_BITS)
140 return required_fault;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000141 }
Olivier Deprez157378f2022-04-04 15:47:50 +0200142 return required_fault;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000143}
144
145static int hmm_vma_walk_hole(unsigned long addr, unsigned long end,
Olivier Deprez157378f2022-04-04 15:47:50 +0200146 __always_unused int depth, struct mm_walk *walk)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000147{
148 struct hmm_vma_walk *hmm_vma_walk = walk->private;
149 struct hmm_range *range = hmm_vma_walk->range;
Olivier Deprez157378f2022-04-04 15:47:50 +0200150 unsigned int required_fault;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000151 unsigned long i, npages;
Olivier Deprez157378f2022-04-04 15:47:50 +0200152 unsigned long *hmm_pfns;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000153
154 i = (addr - range->start) >> PAGE_SHIFT;
155 npages = (end - addr) >> PAGE_SHIFT;
Olivier Deprez157378f2022-04-04 15:47:50 +0200156 hmm_pfns = &range->hmm_pfns[i];
157 required_fault =
158 hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0);
159 if (!walk->vma) {
160 if (required_fault)
161 return -EFAULT;
162 return hmm_pfns_fill(addr, end, range, HMM_PFN_ERROR);
163 }
164 if (required_fault)
165 return hmm_vma_fault(addr, end, required_fault, walk);
166 return hmm_pfns_fill(addr, end, range, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000167}
168
Olivier Deprez157378f2022-04-04 15:47:50 +0200169static inline unsigned long hmm_pfn_flags_order(unsigned long order)
170{
171 return order << HMM_PFN_ORDER_SHIFT;
172}
173
174static inline unsigned long pmd_to_hmm_pfn_flags(struct hmm_range *range,
175 pmd_t pmd)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000176{
177 if (pmd_protnone(pmd))
178 return 0;
Olivier Deprez157378f2022-04-04 15:47:50 +0200179 return (pmd_write(pmd) ? (HMM_PFN_VALID | HMM_PFN_WRITE) :
180 HMM_PFN_VALID) |
181 hmm_pfn_flags_order(PMD_SHIFT - PAGE_SHIFT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000182}
183
David Brazdil0f672f62019-12-10 10:32:29 +0000184#ifdef CONFIG_TRANSPARENT_HUGEPAGE
185static int hmm_vma_handle_pmd(struct mm_walk *walk, unsigned long addr,
Olivier Deprez157378f2022-04-04 15:47:50 +0200186 unsigned long end, unsigned long hmm_pfns[],
187 pmd_t pmd)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000188{
189 struct hmm_vma_walk *hmm_vma_walk = walk->private;
190 struct hmm_range *range = hmm_vma_walk->range;
191 unsigned long pfn, npages, i;
Olivier Deprez157378f2022-04-04 15:47:50 +0200192 unsigned int required_fault;
193 unsigned long cpu_flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000194
195 npages = (end - addr) >> PAGE_SHIFT;
196 cpu_flags = pmd_to_hmm_pfn_flags(range, pmd);
Olivier Deprez157378f2022-04-04 15:47:50 +0200197 required_fault =
198 hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, cpu_flags);
199 if (required_fault)
200 return hmm_vma_fault(addr, end, required_fault, walk);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000201
David Brazdil0f672f62019-12-10 10:32:29 +0000202 pfn = pmd_pfn(pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
Olivier Deprez157378f2022-04-04 15:47:50 +0200203 for (i = 0; addr < end; addr += PAGE_SIZE, i++, pfn++)
204 hmm_pfns[i] = pfn | cpu_flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000205 return 0;
206}
David Brazdil0f672f62019-12-10 10:32:29 +0000207#else /* CONFIG_TRANSPARENT_HUGEPAGE */
208/* stub to allow the code below to compile */
209int hmm_vma_handle_pmd(struct mm_walk *walk, unsigned long addr,
Olivier Deprez157378f2022-04-04 15:47:50 +0200210 unsigned long end, unsigned long hmm_pfns[], pmd_t pmd);
David Brazdil0f672f62019-12-10 10:32:29 +0000211#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000212
Olivier Deprez157378f2022-04-04 15:47:50 +0200213static inline bool hmm_is_device_private_entry(struct hmm_range *range,
214 swp_entry_t entry)
215{
216 return is_device_private_entry(entry) &&
217 device_private_entry_to_page(entry)->pgmap->owner ==
218 range->dev_private_owner;
219}
220
221static inline unsigned long pte_to_hmm_pfn_flags(struct hmm_range *range,
222 pte_t pte)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000223{
David Brazdil0f672f62019-12-10 10:32:29 +0000224 if (pte_none(pte) || !pte_present(pte) || pte_protnone(pte))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000225 return 0;
Olivier Deprez157378f2022-04-04 15:47:50 +0200226 return pte_write(pte) ? (HMM_PFN_VALID | HMM_PFN_WRITE) : HMM_PFN_VALID;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000227}
228
229static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,
230 unsigned long end, pmd_t *pmdp, pte_t *ptep,
Olivier Deprez157378f2022-04-04 15:47:50 +0200231 unsigned long *hmm_pfn)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000232{
233 struct hmm_vma_walk *hmm_vma_walk = walk->private;
234 struct hmm_range *range = hmm_vma_walk->range;
Olivier Deprez157378f2022-04-04 15:47:50 +0200235 unsigned int required_fault;
236 unsigned long cpu_flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000237 pte_t pte = *ptep;
Olivier Deprez157378f2022-04-04 15:47:50 +0200238 uint64_t pfn_req_flags = *hmm_pfn;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000239
240 if (pte_none(pte)) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200241 required_fault =
242 hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, 0);
243 if (required_fault)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000244 goto fault;
Olivier Deprez157378f2022-04-04 15:47:50 +0200245 *hmm_pfn = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000246 return 0;
247 }
248
249 if (!pte_present(pte)) {
250 swp_entry_t entry = pte_to_swp_entry(pte);
251
Olivier Deprez157378f2022-04-04 15:47:50 +0200252 /*
253 * Never fault in device private pages, but just report
254 * the PFN even if not present.
255 */
256 if (hmm_is_device_private_entry(range, entry)) {
257 cpu_flags = HMM_PFN_VALID;
258 if (is_write_device_private_entry(entry))
259 cpu_flags |= HMM_PFN_WRITE;
260 *hmm_pfn = device_private_entry_to_pfn(entry) |
261 cpu_flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000262 return 0;
263 }
264
Olivier Deprez157378f2022-04-04 15:47:50 +0200265 required_fault =
266 hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, 0);
267 if (!required_fault) {
268 *hmm_pfn = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000269 return 0;
270 }
271
Olivier Deprez157378f2022-04-04 15:47:50 +0200272 if (!non_swap_entry(entry))
273 goto fault;
274
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000275 if (is_migration_entry(entry)) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200276 pte_unmap(ptep);
277 hmm_vma_walk->last = addr;
278 migration_entry_wait(walk->mm, pmdp, addr);
279 return -EBUSY;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000280 }
281
282 /* Report error for everything else */
Olivier Deprez157378f2022-04-04 15:47:50 +0200283 pte_unmap(ptep);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000284 return -EFAULT;
285 }
286
Olivier Deprez157378f2022-04-04 15:47:50 +0200287 cpu_flags = pte_to_hmm_pfn_flags(range, pte);
288 required_fault =
289 hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, cpu_flags);
290 if (required_fault)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000291 goto fault;
292
Olivier Deprez157378f2022-04-04 15:47:50 +0200293 /*
294 * Bypass devmap pte such as DAX page when all pfn requested
295 * flags(pfn_req_flags) are fulfilled.
296 * Since each architecture defines a struct page for the zero page, just
297 * fall through and treat it like a normal page.
298 */
299 if (!vm_normal_page(walk->vma, addr, pte) &&
300 !pte_devmap(pte) &&
301 !is_zero_pfn(pte_pfn(pte))) {
302 if (hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, 0)) {
303 pte_unmap(ptep);
304 return -EFAULT;
305 }
306 *hmm_pfn = HMM_PFN_ERROR;
307 return 0;
David Brazdil0f672f62019-12-10 10:32:29 +0000308 }
309
Olivier Deprez157378f2022-04-04 15:47:50 +0200310 *hmm_pfn = pte_pfn(pte) | cpu_flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000311 return 0;
312
313fault:
314 pte_unmap(ptep);
315 /* Fault any virtual address we were asked to fault */
Olivier Deprez157378f2022-04-04 15:47:50 +0200316 return hmm_vma_fault(addr, end, required_fault, walk);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000317}
318
319static int hmm_vma_walk_pmd(pmd_t *pmdp,
320 unsigned long start,
321 unsigned long end,
322 struct mm_walk *walk)
323{
324 struct hmm_vma_walk *hmm_vma_walk = walk->private;
325 struct hmm_range *range = hmm_vma_walk->range;
Olivier Deprez157378f2022-04-04 15:47:50 +0200326 unsigned long *hmm_pfns =
327 &range->hmm_pfns[(start - range->start) >> PAGE_SHIFT];
328 unsigned long npages = (end - start) >> PAGE_SHIFT;
329 unsigned long addr = start;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000330 pte_t *ptep;
David Brazdil0f672f62019-12-10 10:32:29 +0000331 pmd_t pmd;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000332
333again:
David Brazdil0f672f62019-12-10 10:32:29 +0000334 pmd = READ_ONCE(*pmdp);
335 if (pmd_none(pmd))
Olivier Deprez157378f2022-04-04 15:47:50 +0200336 return hmm_vma_walk_hole(start, end, -1, walk);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000337
David Brazdil0f672f62019-12-10 10:32:29 +0000338 if (thp_migration_supported() && is_pmd_migration_entry(pmd)) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200339 if (hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000340 hmm_vma_walk->last = addr;
341 pmd_migration_entry_wait(walk->mm, pmdp);
342 return -EBUSY;
343 }
Olivier Deprez157378f2022-04-04 15:47:50 +0200344 return hmm_pfns_fill(start, end, range, 0);
345 }
346
347 if (!pmd_present(pmd)) {
348 if (hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0))
349 return -EFAULT;
350 return hmm_pfns_fill(start, end, range, HMM_PFN_ERROR);
351 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000352
David Brazdil0f672f62019-12-10 10:32:29 +0000353 if (pmd_devmap(pmd) || pmd_trans_huge(pmd)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000354 /*
David Brazdil0f672f62019-12-10 10:32:29 +0000355 * No need to take pmd_lock here, even if some other thread
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000356 * is splitting the huge pmd we will get that event through
357 * mmu_notifier callback.
358 *
David Brazdil0f672f62019-12-10 10:32:29 +0000359 * So just read pmd value and check again it's a transparent
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000360 * huge or device mapping one and compute corresponding pfn
361 * values.
362 */
363 pmd = pmd_read_atomic(pmdp);
364 barrier();
365 if (!pmd_devmap(pmd) && !pmd_trans_huge(pmd))
366 goto again;
367
Olivier Deprez157378f2022-04-04 15:47:50 +0200368 return hmm_vma_handle_pmd(walk, addr, end, hmm_pfns, pmd);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000369 }
370
David Brazdil0f672f62019-12-10 10:32:29 +0000371 /*
372 * We have handled all the valid cases above ie either none, migration,
373 * huge or transparent huge. At this point either it is a valid pmd
374 * entry pointing to pte directory or it is a bad pmd that will not
375 * recover.
376 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200377 if (pmd_bad(pmd)) {
378 if (hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0))
379 return -EFAULT;
380 return hmm_pfns_fill(start, end, range, HMM_PFN_ERROR);
381 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000382
383 ptep = pte_offset_map(pmdp, addr);
Olivier Deprez157378f2022-04-04 15:47:50 +0200384 for (; addr < end; addr += PAGE_SIZE, ptep++, hmm_pfns++) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000385 int r;
386
Olivier Deprez157378f2022-04-04 15:47:50 +0200387 r = hmm_vma_handle_pte(walk, addr, end, pmdp, ptep, hmm_pfns);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000388 if (r) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200389 /* hmm_vma_handle_pte() did pte_unmap() */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000390 return r;
391 }
392 }
393 pte_unmap(ptep - 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000394 return 0;
395}
396
David Brazdil0f672f62019-12-10 10:32:29 +0000397#if defined(CONFIG_ARCH_HAS_PTE_DEVMAP) && \
398 defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
Olivier Deprez157378f2022-04-04 15:47:50 +0200399static inline unsigned long pud_to_hmm_pfn_flags(struct hmm_range *range,
400 pud_t pud)
David Brazdil0f672f62019-12-10 10:32:29 +0000401{
402 if (!pud_present(pud))
403 return 0;
Olivier Deprez157378f2022-04-04 15:47:50 +0200404 return (pud_write(pud) ? (HMM_PFN_VALID | HMM_PFN_WRITE) :
405 HMM_PFN_VALID) |
406 hmm_pfn_flags_order(PUD_SHIFT - PAGE_SHIFT);
David Brazdil0f672f62019-12-10 10:32:29 +0000407}
408
409static int hmm_vma_walk_pud(pud_t *pudp, unsigned long start, unsigned long end,
410 struct mm_walk *walk)
411{
412 struct hmm_vma_walk *hmm_vma_walk = walk->private;
413 struct hmm_range *range = hmm_vma_walk->range;
Olivier Deprez157378f2022-04-04 15:47:50 +0200414 unsigned long addr = start;
David Brazdil0f672f62019-12-10 10:32:29 +0000415 pud_t pud;
Olivier Deprez157378f2022-04-04 15:47:50 +0200416 int ret = 0;
417 spinlock_t *ptl = pud_trans_huge_lock(pudp, walk->vma);
David Brazdil0f672f62019-12-10 10:32:29 +0000418
Olivier Deprez157378f2022-04-04 15:47:50 +0200419 if (!ptl)
420 return 0;
421
422 /* Normally we don't want to split the huge page */
423 walk->action = ACTION_CONTINUE;
424
David Brazdil0f672f62019-12-10 10:32:29 +0000425 pud = READ_ONCE(*pudp);
Olivier Deprez157378f2022-04-04 15:47:50 +0200426 if (pud_none(pud)) {
427 spin_unlock(ptl);
428 return hmm_vma_walk_hole(start, end, -1, walk);
429 }
David Brazdil0f672f62019-12-10 10:32:29 +0000430
431 if (pud_huge(pud) && pud_devmap(pud)) {
432 unsigned long i, npages, pfn;
Olivier Deprez157378f2022-04-04 15:47:50 +0200433 unsigned int required_fault;
434 unsigned long *hmm_pfns;
435 unsigned long cpu_flags;
David Brazdil0f672f62019-12-10 10:32:29 +0000436
Olivier Deprez157378f2022-04-04 15:47:50 +0200437 if (!pud_present(pud)) {
438 spin_unlock(ptl);
439 return hmm_vma_walk_hole(start, end, -1, walk);
440 }
David Brazdil0f672f62019-12-10 10:32:29 +0000441
442 i = (addr - range->start) >> PAGE_SHIFT;
443 npages = (end - addr) >> PAGE_SHIFT;
Olivier Deprez157378f2022-04-04 15:47:50 +0200444 hmm_pfns = &range->hmm_pfns[i];
David Brazdil0f672f62019-12-10 10:32:29 +0000445
446 cpu_flags = pud_to_hmm_pfn_flags(range, pud);
Olivier Deprez157378f2022-04-04 15:47:50 +0200447 required_fault = hmm_range_need_fault(hmm_vma_walk, hmm_pfns,
448 npages, cpu_flags);
449 if (required_fault) {
450 spin_unlock(ptl);
451 return hmm_vma_fault(addr, end, required_fault, walk);
452 }
David Brazdil0f672f62019-12-10 10:32:29 +0000453
454 pfn = pud_pfn(pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
Olivier Deprez157378f2022-04-04 15:47:50 +0200455 for (i = 0; i < npages; ++i, ++pfn)
456 hmm_pfns[i] = pfn | cpu_flags;
457 goto out_unlock;
David Brazdil0f672f62019-12-10 10:32:29 +0000458 }
459
Olivier Deprez157378f2022-04-04 15:47:50 +0200460 /* Ask for the PUD to be split */
461 walk->action = ACTION_SUBTREE;
David Brazdil0f672f62019-12-10 10:32:29 +0000462
Olivier Deprez157378f2022-04-04 15:47:50 +0200463out_unlock:
464 spin_unlock(ptl);
465 return ret;
David Brazdil0f672f62019-12-10 10:32:29 +0000466}
467#else
468#define hmm_vma_walk_pud NULL
469#endif
470
471#ifdef CONFIG_HUGETLB_PAGE
472static int hmm_vma_walk_hugetlb_entry(pte_t *pte, unsigned long hmask,
473 unsigned long start, unsigned long end,
474 struct mm_walk *walk)
475{
476 unsigned long addr = start, i, pfn;
477 struct hmm_vma_walk *hmm_vma_walk = walk->private;
478 struct hmm_range *range = hmm_vma_walk->range;
479 struct vm_area_struct *vma = walk->vma;
Olivier Deprez157378f2022-04-04 15:47:50 +0200480 unsigned int required_fault;
481 unsigned long pfn_req_flags;
482 unsigned long cpu_flags;
David Brazdil0f672f62019-12-10 10:32:29 +0000483 spinlock_t *ptl;
484 pte_t entry;
David Brazdil0f672f62019-12-10 10:32:29 +0000485
486 ptl = huge_pte_lock(hstate_vma(vma), walk->mm, pte);
487 entry = huge_ptep_get(pte);
488
489 i = (start - range->start) >> PAGE_SHIFT;
Olivier Deprez157378f2022-04-04 15:47:50 +0200490 pfn_req_flags = range->hmm_pfns[i];
491 cpu_flags = pte_to_hmm_pfn_flags(range, entry) |
492 hmm_pfn_flags_order(huge_page_order(hstate_vma(vma)));
493 required_fault =
494 hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, cpu_flags);
495 if (required_fault) {
496 spin_unlock(ptl);
497 return hmm_vma_fault(addr, end, required_fault, walk);
David Brazdil0f672f62019-12-10 10:32:29 +0000498 }
499
500 pfn = pte_pfn(entry) + ((start & ~hmask) >> PAGE_SHIFT);
501 for (; addr < end; addr += PAGE_SIZE, i++, pfn++)
Olivier Deprez157378f2022-04-04 15:47:50 +0200502 range->hmm_pfns[i] = pfn | cpu_flags;
David Brazdil0f672f62019-12-10 10:32:29 +0000503
David Brazdil0f672f62019-12-10 10:32:29 +0000504 spin_unlock(ptl);
Olivier Deprez157378f2022-04-04 15:47:50 +0200505 return 0;
David Brazdil0f672f62019-12-10 10:32:29 +0000506}
507#else
508#define hmm_vma_walk_hugetlb_entry NULL
509#endif /* CONFIG_HUGETLB_PAGE */
510
Olivier Deprez157378f2022-04-04 15:47:50 +0200511static int hmm_vma_walk_test(unsigned long start, unsigned long end,
512 struct mm_walk *walk)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000513{
Olivier Deprez157378f2022-04-04 15:47:50 +0200514 struct hmm_vma_walk *hmm_vma_walk = walk->private;
515 struct hmm_range *range = hmm_vma_walk->range;
516 struct vm_area_struct *vma = walk->vma;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000517
Olivier Deprez157378f2022-04-04 15:47:50 +0200518 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)) &&
519 vma->vm_flags & VM_READ)
520 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000521
Olivier Deprez157378f2022-04-04 15:47:50 +0200522 /*
523 * vma ranges that don't have struct page backing them or map I/O
524 * devices directly cannot be handled by hmm_range_fault().
525 *
526 * If the vma does not allow read access, then assume that it does not
527 * allow write access either. HMM does not support architectures that
528 * allow write without read.
529 *
530 * If a fault is requested for an unsupported range then it is a hard
531 * failure.
532 */
533 if (hmm_range_need_fault(hmm_vma_walk,
534 range->hmm_pfns +
535 ((start - range->start) >> PAGE_SHIFT),
536 (end - start) >> PAGE_SHIFT, 0))
David Brazdil0f672f62019-12-10 10:32:29 +0000537 return -EFAULT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000538
Olivier Deprez157378f2022-04-04 15:47:50 +0200539 hmm_pfns_fill(start, end, range, HMM_PFN_ERROR);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000540
Olivier Deprez157378f2022-04-04 15:47:50 +0200541 /* Skip this vma and continue processing the next vma. */
542 return 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000543}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000544
David Brazdil0f672f62019-12-10 10:32:29 +0000545static const struct mm_walk_ops hmm_walk_ops = {
546 .pud_entry = hmm_vma_walk_pud,
547 .pmd_entry = hmm_vma_walk_pmd,
548 .pte_hole = hmm_vma_walk_hole,
549 .hugetlb_entry = hmm_vma_walk_hugetlb_entry,
Olivier Deprez157378f2022-04-04 15:47:50 +0200550 .test_walk = hmm_vma_walk_test,
David Brazdil0f672f62019-12-10 10:32:29 +0000551};
552
553/**
554 * hmm_range_fault - try to fault some address in a virtual address range
Olivier Deprez157378f2022-04-04 15:47:50 +0200555 * @range: argument structure
David Brazdil0f672f62019-12-10 10:32:29 +0000556 *
Olivier Deprez157378f2022-04-04 15:47:50 +0200557 * Returns 0 on success or one of the following error codes:
David Brazdil0f672f62019-12-10 10:32:29 +0000558 *
559 * -EINVAL: Invalid arguments or mm or virtual address is in an invalid vma
560 * (e.g., device file vma).
561 * -ENOMEM: Out of memory.
562 * -EPERM: Invalid permission (e.g., asking for write and range is read
563 * only).
David Brazdil0f672f62019-12-10 10:32:29 +0000564 * -EBUSY: The range has been invalidated and the caller needs to wait for
565 * the invalidation to finish.
Olivier Deprez157378f2022-04-04 15:47:50 +0200566 * -EFAULT: A page was requested to be valid and could not be made valid
567 * ie it has no backing VMA or it is illegal to access
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000568 *
Olivier Deprez157378f2022-04-04 15:47:50 +0200569 * This is similar to get_user_pages(), except that it can read the page tables
570 * without mutating them (ie causing faults).
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000571 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200572int hmm_range_fault(struct hmm_range *range)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000573{
Olivier Deprez157378f2022-04-04 15:47:50 +0200574 struct hmm_vma_walk hmm_vma_walk = {
575 .range = range,
576 .last = range->start,
577 };
578 struct mm_struct *mm = range->notifier->mm;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000579 int ret;
580
Olivier Deprez157378f2022-04-04 15:47:50 +0200581 mmap_assert_locked(mm);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000582
583 do {
David Brazdil0f672f62019-12-10 10:32:29 +0000584 /* If range is no longer valid force retry. */
Olivier Deprez157378f2022-04-04 15:47:50 +0200585 if (mmu_interval_check_retry(range->notifier,
586 range->notifier_seq))
David Brazdil0f672f62019-12-10 10:32:29 +0000587 return -EBUSY;
Olivier Deprez157378f2022-04-04 15:47:50 +0200588 ret = walk_page_range(mm, hmm_vma_walk.last, range->end,
589 &hmm_walk_ops, &hmm_vma_walk);
David Brazdil0f672f62019-12-10 10:32:29 +0000590 /*
Olivier Deprez157378f2022-04-04 15:47:50 +0200591 * When -EBUSY is returned the loop restarts with
592 * hmm_vma_walk.last set to an address that has not been stored
593 * in pfns. All entries < last in the pfn array are set to their
594 * output, and all >= are still at their input values.
David Brazdil0f672f62019-12-10 10:32:29 +0000595 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200596 } while (ret == -EBUSY);
David Brazdil0f672f62019-12-10 10:32:29 +0000597 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000598}
Olivier Deprez157378f2022-04-04 15:47:50 +0200599EXPORT_SYMBOL(hmm_range_fault);