]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - mm/hmm.c
mm/hmm: return -EFAULT when setting HMM_PFN_ERROR on requested valid pages
[mirror_ubuntu-hirsute-kernel.git] / mm / hmm.c
CommitLineData
c942fddf 1// SPDX-License-Identifier: GPL-2.0-or-later
133ff0ea
JG
2/*
3 * Copyright 2013 Red Hat Inc.
4 *
f813f219 5 * Authors: Jérôme Glisse <jglisse@redhat.com>
133ff0ea
JG
6 */
7/*
8 * Refer to include/linux/hmm.h for information about heterogeneous memory
9 * management or HMM for short.
10 */
a520110e 11#include <linux/pagewalk.h>
133ff0ea 12#include <linux/hmm.h>
858b54da 13#include <linux/init.h>
da4c3c73
JG
14#include <linux/rmap.h>
15#include <linux/swap.h>
133ff0ea
JG
16#include <linux/slab.h>
17#include <linux/sched.h>
4ef589dc
JG
18#include <linux/mmzone.h>
19#include <linux/pagemap.h>
da4c3c73
JG
20#include <linux/swapops.h>
21#include <linux/hugetlb.h>
4ef589dc 22#include <linux/memremap.h>
c8a53b2d 23#include <linux/sched/mm.h>
7b2d55d2 24#include <linux/jump_label.h>
55c0ece8 25#include <linux/dma-mapping.h>
c0b12405 26#include <linux/mmu_notifier.h>
4ef589dc
JG
27#include <linux/memory_hotplug.h>
28
74eee180
JG
29struct hmm_vma_walk {
30 struct hmm_range *range;
992de9a8 31 struct dev_pagemap *pgmap;
74eee180 32 unsigned long last;
9a4903e4 33 unsigned int flags;
74eee180
JG
34};
35
2aee09d8
JG
36static int hmm_vma_do_fault(struct mm_walk *walk, unsigned long addr,
37 bool write_fault, uint64_t *pfn)
74eee180 38{
9b1ae605 39 unsigned int flags = FAULT_FLAG_REMOTE;
74eee180 40 struct hmm_vma_walk *hmm_vma_walk = walk->private;
f88a1e90 41 struct hmm_range *range = hmm_vma_walk->range;
74eee180 42 struct vm_area_struct *vma = walk->vma;
50a7ca3c 43 vm_fault_t ret;
74eee180 44
6c64f2bb
RC
45 if (!vma)
46 goto err;
47
9a4903e4
CH
48 if (hmm_vma_walk->flags & HMM_FAULT_ALLOW_RETRY)
49 flags |= FAULT_FLAG_ALLOW_RETRY;
50 if (write_fault)
51 flags |= FAULT_FLAG_WRITE;
52
50a7ca3c 53 ret = handle_mm_fault(vma, addr, flags);
e709accc
JG
54 if (ret & VM_FAULT_RETRY) {
55 /* Note, handle_mm_fault did up_read(&mm->mmap_sem)) */
73231612 56 return -EAGAIN;
e709accc 57 }
6c64f2bb
RC
58 if (ret & VM_FAULT_ERROR)
59 goto err;
74eee180 60
73231612 61 return -EBUSY;
6c64f2bb
RC
62
63err:
64 *pfn = range->values[HMM_PFN_ERROR];
65 return -EFAULT;
74eee180
JG
66}
67
d28c2c9a
RC
68static int hmm_pfns_fill(unsigned long addr, unsigned long end,
69 struct hmm_range *range, enum hmm_pfn_value_e value)
da4c3c73 70{
ff05c0c6 71 uint64_t *pfns = range->pfns;
da4c3c73
JG
72 unsigned long i;
73
74 i = (addr - range->start) >> PAGE_SHIFT;
75 for (; addr < end; addr += PAGE_SIZE, i++)
d28c2c9a 76 pfns[i] = range->values[value];
da4c3c73
JG
77
78 return 0;
79}
80
5504ed29 81/*
d2e8d551
RC
82 * hmm_vma_walk_hole_() - handle a range lacking valid pmd or pte(s)
83 * @addr: range virtual start address (inclusive)
5504ed29 84 * @end: range virtual end address (exclusive)
2aee09d8
JG
85 * @fault: should we fault or not ?
86 * @write_fault: write fault ?
5504ed29 87 * @walk: mm_walk structure
085ea250 88 * Return: 0 on success, -EBUSY after page fault, or page fault error
5504ed29
JG
89 *
90 * This function will be called whenever pmd_none() or pte_none() returns true,
91 * or whenever there is no page directory covering the virtual address range.
92 */
2aee09d8
JG
93static int hmm_vma_walk_hole_(unsigned long addr, unsigned long end,
94 bool fault, bool write_fault,
95 struct mm_walk *walk)
da4c3c73 96{
74eee180
JG
97 struct hmm_vma_walk *hmm_vma_walk = walk->private;
98 struct hmm_range *range = hmm_vma_walk->range;
ff05c0c6 99 uint64_t *pfns = range->pfns;
7f08263d 100 unsigned long i;
da4c3c73 101
74eee180 102 hmm_vma_walk->last = addr;
7f08263d 103 i = (addr - range->start) >> PAGE_SHIFT;
63d5066f 104
c18ce674
RC
105 if (write_fault && walk->vma && !(walk->vma->vm_flags & VM_WRITE))
106 return -EPERM;
107
7f08263d 108 for (; addr < end; addr += PAGE_SIZE, i++) {
f88a1e90 109 pfns[i] = range->values[HMM_PFN_NONE];
2aee09d8 110 if (fault || write_fault) {
74eee180 111 int ret;
da4c3c73 112
2aee09d8
JG
113 ret = hmm_vma_do_fault(walk, addr, write_fault,
114 &pfns[i]);
73231612 115 if (ret != -EBUSY)
74eee180
JG
116 return ret;
117 }
118 }
119
73231612 120 return (fault || write_fault) ? -EBUSY : 0;
2aee09d8
JG
121}
122
123static inline void hmm_pte_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
124 uint64_t pfns, uint64_t cpu_flags,
125 bool *fault, bool *write_fault)
126{
f88a1e90
JG
127 struct hmm_range *range = hmm_vma_walk->range;
128
d45d464b 129 if (hmm_vma_walk->flags & HMM_FAULT_SNAPSHOT)
2aee09d8
JG
130 return;
131
023a019a
JG
132 /*
133 * So we not only consider the individual per page request we also
134 * consider the default flags requested for the range. The API can
d2e8d551
RC
135 * be used 2 ways. The first one where the HMM user coalesces
136 * multiple page faults into one request and sets flags per pfn for
137 * those faults. The second one where the HMM user wants to pre-
023a019a
JG
138 * fault a range with specific flags. For the latter one it is a
139 * waste to have the user pre-fill the pfn arrays with a default
140 * flags value.
141 */
142 pfns = (pfns & range->pfn_flags_mask) | range->default_flags;
143
2aee09d8 144 /* We aren't ask to do anything ... */
f88a1e90 145 if (!(pfns & range->flags[HMM_PFN_VALID]))
2aee09d8 146 return;
d2e8d551 147 /* If this is device memory then only fault if explicitly requested */
f88a1e90
JG
148 if ((cpu_flags & range->flags[HMM_PFN_DEVICE_PRIVATE])) {
149 /* Do we fault on device memory ? */
150 if (pfns & range->flags[HMM_PFN_DEVICE_PRIVATE]) {
151 *write_fault = pfns & range->flags[HMM_PFN_WRITE];
152 *fault = true;
153 }
2aee09d8
JG
154 return;
155 }
f88a1e90
JG
156
157 /* If CPU page table is not valid then we need to fault */
158 *fault = !(cpu_flags & range->flags[HMM_PFN_VALID]);
159 /* Need to write fault ? */
160 if ((pfns & range->flags[HMM_PFN_WRITE]) &&
161 !(cpu_flags & range->flags[HMM_PFN_WRITE])) {
162 *write_fault = true;
2aee09d8
JG
163 *fault = true;
164 }
165}
166
167static void hmm_range_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
168 const uint64_t *pfns, unsigned long npages,
169 uint64_t cpu_flags, bool *fault,
170 bool *write_fault)
171{
172 unsigned long i;
173
d45d464b 174 if (hmm_vma_walk->flags & HMM_FAULT_SNAPSHOT) {
2aee09d8
JG
175 *fault = *write_fault = false;
176 return;
177 }
178
a3e0d41c 179 *fault = *write_fault = false;
2aee09d8
JG
180 for (i = 0; i < npages; ++i) {
181 hmm_pte_need_fault(hmm_vma_walk, pfns[i], cpu_flags,
182 fault, write_fault);
a3e0d41c 183 if ((*write_fault))
2aee09d8
JG
184 return;
185 }
186}
187
188static int hmm_vma_walk_hole(unsigned long addr, unsigned long end,
b7a16c7a 189 __always_unused int depth, struct mm_walk *walk)
2aee09d8
JG
190{
191 struct hmm_vma_walk *hmm_vma_walk = walk->private;
192 struct hmm_range *range = hmm_vma_walk->range;
193 bool fault, write_fault;
194 unsigned long i, npages;
195 uint64_t *pfns;
196
197 i = (addr - range->start) >> PAGE_SHIFT;
198 npages = (end - addr) >> PAGE_SHIFT;
199 pfns = &range->pfns[i];
200 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
201 0, &fault, &write_fault);
202 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
203}
204
f88a1e90 205static inline uint64_t pmd_to_hmm_pfn_flags(struct hmm_range *range, pmd_t pmd)
2aee09d8
JG
206{
207 if (pmd_protnone(pmd))
208 return 0;
f88a1e90
JG
209 return pmd_write(pmd) ? range->flags[HMM_PFN_VALID] |
210 range->flags[HMM_PFN_WRITE] :
211 range->flags[HMM_PFN_VALID];
da4c3c73
JG
212}
213
992de9a8 214#ifdef CONFIG_TRANSPARENT_HUGEPAGE
9d3973d6
CH
215static int hmm_vma_handle_pmd(struct mm_walk *walk, unsigned long addr,
216 unsigned long end, uint64_t *pfns, pmd_t pmd)
217{
53f5c3f4 218 struct hmm_vma_walk *hmm_vma_walk = walk->private;
f88a1e90 219 struct hmm_range *range = hmm_vma_walk->range;
2aee09d8 220 unsigned long pfn, npages, i;
2aee09d8 221 bool fault, write_fault;
f88a1e90 222 uint64_t cpu_flags;
53f5c3f4 223
2aee09d8 224 npages = (end - addr) >> PAGE_SHIFT;
f88a1e90 225 cpu_flags = pmd_to_hmm_pfn_flags(range, pmd);
2aee09d8
JG
226 hmm_range_need_fault(hmm_vma_walk, pfns, npages, cpu_flags,
227 &fault, &write_fault);
53f5c3f4 228
2aee09d8
JG
229 if (pmd_protnone(pmd) || fault || write_fault)
230 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
53f5c3f4 231
309f9a4f 232 pfn = pmd_pfn(pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
992de9a8
JG
233 for (i = 0; addr < end; addr += PAGE_SIZE, i++, pfn++) {
234 if (pmd_devmap(pmd)) {
235 hmm_vma_walk->pgmap = get_dev_pagemap(pfn,
236 hmm_vma_walk->pgmap);
237 if (unlikely(!hmm_vma_walk->pgmap))
238 return -EBUSY;
239 }
391aab11 240 pfns[i] = hmm_device_entry_from_pfn(range, pfn) | cpu_flags;
992de9a8
JG
241 }
242 if (hmm_vma_walk->pgmap) {
243 put_dev_pagemap(hmm_vma_walk->pgmap);
244 hmm_vma_walk->pgmap = NULL;
245 }
53f5c3f4
JG
246 hmm_vma_walk->last = end;
247 return 0;
248}
9d3973d6
CH
249#else /* CONFIG_TRANSPARENT_HUGEPAGE */
250/* stub to allow the code below to compile */
251int hmm_vma_handle_pmd(struct mm_walk *walk, unsigned long addr,
252 unsigned long end, uint64_t *pfns, pmd_t pmd);
253#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
53f5c3f4 254
f88a1e90 255static inline uint64_t pte_to_hmm_pfn_flags(struct hmm_range *range, pte_t pte)
2aee09d8 256{
789c2af8 257 if (pte_none(pte) || !pte_present(pte) || pte_protnone(pte))
2aee09d8 258 return 0;
f88a1e90
JG
259 return pte_write(pte) ? range->flags[HMM_PFN_VALID] |
260 range->flags[HMM_PFN_WRITE] :
261 range->flags[HMM_PFN_VALID];
2aee09d8
JG
262}
263
53f5c3f4
JG
264static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,
265 unsigned long end, pmd_t *pmdp, pte_t *ptep,
266 uint64_t *pfn)
267{
268 struct hmm_vma_walk *hmm_vma_walk = walk->private;
f88a1e90 269 struct hmm_range *range = hmm_vma_walk->range;
2aee09d8
JG
270 bool fault, write_fault;
271 uint64_t cpu_flags;
53f5c3f4 272 pte_t pte = *ptep;
f88a1e90 273 uint64_t orig_pfn = *pfn;
53f5c3f4 274
f88a1e90 275 *pfn = range->values[HMM_PFN_NONE];
73231612 276 fault = write_fault = false;
53f5c3f4
JG
277
278 if (pte_none(pte)) {
73231612
JG
279 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, 0,
280 &fault, &write_fault);
2aee09d8 281 if (fault || write_fault)
53f5c3f4
JG
282 goto fault;
283 return 0;
284 }
285
286 if (!pte_present(pte)) {
287 swp_entry_t entry = pte_to_swp_entry(pte);
288
53f5c3f4
JG
289 /*
290 * This is a special swap entry, ignore migration, use
291 * device and report anything else as error.
292 */
293 if (is_device_private_entry(entry)) {
f88a1e90
JG
294 cpu_flags = range->flags[HMM_PFN_VALID] |
295 range->flags[HMM_PFN_DEVICE_PRIVATE];
2aee09d8 296 cpu_flags |= is_write_device_private_entry(entry) ?
f88a1e90
JG
297 range->flags[HMM_PFN_WRITE] : 0;
298 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
299 &fault, &write_fault);
300 if (fault || write_fault)
301 goto fault;
391aab11
JG
302 *pfn = hmm_device_entry_from_pfn(range,
303 swp_offset(entry));
f88a1e90 304 *pfn |= cpu_flags;
53f5c3f4
JG
305 return 0;
306 }
307
76612d6c
JG
308 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, 0, &fault,
309 &write_fault);
310 if (!fault && !write_fault)
53f5c3f4 311 return 0;
76612d6c
JG
312
313 if (!non_swap_entry(entry))
314 goto fault;
315
316 if (is_migration_entry(entry)) {
317 pte_unmap(ptep);
318 hmm_vma_walk->last = addr;
319 migration_entry_wait(walk->mm, pmdp, addr);
320 return -EBUSY;
53f5c3f4
JG
321 }
322
323 /* Report error for everything else */
dfdc2207 324 pte_unmap(ptep);
f88a1e90 325 *pfn = range->values[HMM_PFN_ERROR];
53f5c3f4
JG
326 return -EFAULT;
327 }
328
76612d6c
JG
329 cpu_flags = pte_to_hmm_pfn_flags(range, pte);
330 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags, &fault,
331 &write_fault);
2aee09d8 332 if (fault || write_fault)
53f5c3f4
JG
333 goto fault;
334
992de9a8
JG
335 if (pte_devmap(pte)) {
336 hmm_vma_walk->pgmap = get_dev_pagemap(pte_pfn(pte),
337 hmm_vma_walk->pgmap);
dfdc2207
JG
338 if (unlikely(!hmm_vma_walk->pgmap)) {
339 pte_unmap(ptep);
992de9a8 340 return -EBUSY;
dfdc2207 341 }
992de9a8 342 } else if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) && pte_special(pte)) {
ac541f25 343 if (!is_zero_pfn(pte_pfn(pte))) {
dfdc2207 344 pte_unmap(ptep);
ac541f25
RC
345 *pfn = range->values[HMM_PFN_SPECIAL];
346 return -EFAULT;
347 }
348 /*
349 * Since each architecture defines a struct page for the zero
350 * page, just fall through and treat it like a normal page.
351 */
992de9a8
JG
352 }
353
391aab11 354 *pfn = hmm_device_entry_from_pfn(range, pte_pfn(pte)) | cpu_flags;
53f5c3f4
JG
355 return 0;
356
357fault:
992de9a8
JG
358 if (hmm_vma_walk->pgmap) {
359 put_dev_pagemap(hmm_vma_walk->pgmap);
360 hmm_vma_walk->pgmap = NULL;
361 }
53f5c3f4
JG
362 pte_unmap(ptep);
363 /* Fault any virtual address we were asked to fault */
2aee09d8 364 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
53f5c3f4
JG
365}
366
da4c3c73
JG
367static int hmm_vma_walk_pmd(pmd_t *pmdp,
368 unsigned long start,
369 unsigned long end,
370 struct mm_walk *walk)
371{
74eee180
JG
372 struct hmm_vma_walk *hmm_vma_walk = walk->private;
373 struct hmm_range *range = hmm_vma_walk->range;
2288a9a6
JG
374 uint64_t *pfns = &range->pfns[(start - range->start) >> PAGE_SHIFT];
375 unsigned long npages = (end - start) >> PAGE_SHIFT;
376 unsigned long addr = start;
377 bool fault, write_fault;
da4c3c73 378 pte_t *ptep;
d08faca0 379 pmd_t pmd;
da4c3c73 380
da4c3c73 381again:
d08faca0
JG
382 pmd = READ_ONCE(*pmdp);
383 if (pmd_none(pmd))
b7a16c7a 384 return hmm_vma_walk_hole(start, end, -1, walk);
da4c3c73 385
d08faca0 386 if (thp_migration_supported() && is_pmd_migration_entry(pmd)) {
d08faca0
JG
387 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
388 0, &fault, &write_fault);
389 if (fault || write_fault) {
390 hmm_vma_walk->last = addr;
d2e8d551 391 pmd_migration_entry_wait(walk->mm, pmdp);
73231612 392 return -EBUSY;
d08faca0 393 }
7d082987 394 return hmm_pfns_fill(start, end, range, HMM_PFN_NONE);
2288a9a6
JG
395 }
396
397 if (!pmd_present(pmd)) {
398 hmm_range_need_fault(hmm_vma_walk, pfns, npages, 0, &fault,
399 &write_fault);
400 if (fault || write_fault)
401 return -EFAULT;
d28c2c9a 402 return hmm_pfns_fill(start, end, range, HMM_PFN_ERROR);
2288a9a6 403 }
da4c3c73 404
d08faca0 405 if (pmd_devmap(pmd) || pmd_trans_huge(pmd)) {
da4c3c73 406 /*
d2e8d551 407 * No need to take pmd_lock here, even if some other thread
da4c3c73
JG
408 * is splitting the huge pmd we will get that event through
409 * mmu_notifier callback.
410 *
d2e8d551 411 * So just read pmd value and check again it's a transparent
da4c3c73
JG
412 * huge or device mapping one and compute corresponding pfn
413 * values.
414 */
415 pmd = pmd_read_atomic(pmdp);
416 barrier();
417 if (!pmd_devmap(pmd) && !pmd_trans_huge(pmd))
418 goto again;
74eee180 419
2288a9a6 420 return hmm_vma_handle_pmd(walk, addr, end, pfns, pmd);
da4c3c73
JG
421 }
422
d08faca0 423 /*
d2e8d551 424 * We have handled all the valid cases above ie either none, migration,
d08faca0
JG
425 * huge or transparent huge. At this point either it is a valid pmd
426 * entry pointing to pte directory or it is a bad pmd that will not
427 * recover.
428 */
2288a9a6
JG
429 if (pmd_bad(pmd)) {
430 hmm_range_need_fault(hmm_vma_walk, pfns, npages, 0, &fault,
431 &write_fault);
432 if (fault || write_fault)
433 return -EFAULT;
d28c2c9a 434 return hmm_pfns_fill(start, end, range, HMM_PFN_ERROR);
2288a9a6 435 }
da4c3c73
JG
436
437 ptep = pte_offset_map(pmdp, addr);
2288a9a6 438 for (; addr < end; addr += PAGE_SIZE, ptep++, pfns++) {
53f5c3f4 439 int r;
74eee180 440
2288a9a6 441 r = hmm_vma_handle_pte(walk, addr, end, pmdp, ptep, pfns);
53f5c3f4 442 if (r) {
dfdc2207 443 /* hmm_vma_handle_pte() did pte_unmap() */
53f5c3f4
JG
444 hmm_vma_walk->last = addr;
445 return r;
74eee180 446 }
da4c3c73 447 }
992de9a8
JG
448 if (hmm_vma_walk->pgmap) {
449 /*
450 * We do put_dev_pagemap() here and not in hmm_vma_handle_pte()
451 * so that we can leverage get_dev_pagemap() optimization which
452 * will not re-take a reference on a pgmap if we already have
453 * one.
454 */
455 put_dev_pagemap(hmm_vma_walk->pgmap);
456 hmm_vma_walk->pgmap = NULL;
457 }
da4c3c73
JG
458 pte_unmap(ptep - 1);
459
53f5c3f4 460 hmm_vma_walk->last = addr;
da4c3c73
JG
461 return 0;
462}
463
f0b3c45c
CH
464#if defined(CONFIG_ARCH_HAS_PTE_DEVMAP) && \
465 defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
466static inline uint64_t pud_to_hmm_pfn_flags(struct hmm_range *range, pud_t pud)
467{
468 if (!pud_present(pud))
469 return 0;
470 return pud_write(pud) ? range->flags[HMM_PFN_VALID] |
471 range->flags[HMM_PFN_WRITE] :
472 range->flags[HMM_PFN_VALID];
473}
474
475static int hmm_vma_walk_pud(pud_t *pudp, unsigned long start, unsigned long end,
476 struct mm_walk *walk)
992de9a8
JG
477{
478 struct hmm_vma_walk *hmm_vma_walk = walk->private;
479 struct hmm_range *range = hmm_vma_walk->range;
3afc4236 480 unsigned long addr = start;
992de9a8 481 pud_t pud;
3afc4236
SP
482 int ret = 0;
483 spinlock_t *ptl = pud_trans_huge_lock(pudp, walk->vma);
484
485 if (!ptl)
486 return 0;
487
488 /* Normally we don't want to split the huge page */
489 walk->action = ACTION_CONTINUE;
992de9a8 490
992de9a8 491 pud = READ_ONCE(*pudp);
3afc4236 492 if (pud_none(pud)) {
05fc1df9
JG
493 spin_unlock(ptl);
494 return hmm_vma_walk_hole(start, end, -1, walk);
3afc4236 495 }
992de9a8
JG
496
497 if (pud_huge(pud) && pud_devmap(pud)) {
498 unsigned long i, npages, pfn;
499 uint64_t *pfns, cpu_flags;
500 bool fault, write_fault;
501
3afc4236 502 if (!pud_present(pud)) {
05fc1df9
JG
503 spin_unlock(ptl);
504 return hmm_vma_walk_hole(start, end, -1, walk);
3afc4236 505 }
992de9a8
JG
506
507 i = (addr - range->start) >> PAGE_SHIFT;
508 npages = (end - addr) >> PAGE_SHIFT;
509 pfns = &range->pfns[i];
510
511 cpu_flags = pud_to_hmm_pfn_flags(range, pud);
512 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
513 cpu_flags, &fault, &write_fault);
3afc4236 514 if (fault || write_fault) {
05fc1df9
JG
515 spin_unlock(ptl);
516 return hmm_vma_walk_hole_(addr, end, fault, write_fault,
517 walk);
3afc4236 518 }
992de9a8 519
992de9a8
JG
520 pfn = pud_pfn(pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
521 for (i = 0; i < npages; ++i, ++pfn) {
522 hmm_vma_walk->pgmap = get_dev_pagemap(pfn,
523 hmm_vma_walk->pgmap);
3afc4236
SP
524 if (unlikely(!hmm_vma_walk->pgmap)) {
525 ret = -EBUSY;
526 goto out_unlock;
527 }
391aab11
JG
528 pfns[i] = hmm_device_entry_from_pfn(range, pfn) |
529 cpu_flags;
992de9a8
JG
530 }
531 if (hmm_vma_walk->pgmap) {
532 put_dev_pagemap(hmm_vma_walk->pgmap);
533 hmm_vma_walk->pgmap = NULL;
534 }
535 hmm_vma_walk->last = end;
3afc4236 536 goto out_unlock;
992de9a8
JG
537 }
538
3afc4236
SP
539 /* Ask for the PUD to be split */
540 walk->action = ACTION_SUBTREE;
992de9a8 541
3afc4236
SP
542out_unlock:
543 spin_unlock(ptl);
544 return ret;
992de9a8 545}
f0b3c45c
CH
546#else
547#define hmm_vma_walk_pud NULL
548#endif
992de9a8 549
251bbe59 550#ifdef CONFIG_HUGETLB_PAGE
63d5066f
JG
551static int hmm_vma_walk_hugetlb_entry(pte_t *pte, unsigned long hmask,
552 unsigned long start, unsigned long end,
553 struct mm_walk *walk)
554{
05c23af4 555 unsigned long addr = start, i, pfn;
63d5066f
JG
556 struct hmm_vma_walk *hmm_vma_walk = walk->private;
557 struct hmm_range *range = hmm_vma_walk->range;
558 struct vm_area_struct *vma = walk->vma;
63d5066f
JG
559 uint64_t orig_pfn, cpu_flags;
560 bool fault, write_fault;
561 spinlock_t *ptl;
562 pte_t entry;
563 int ret = 0;
564
d2e8d551 565 ptl = huge_pte_lock(hstate_vma(vma), walk->mm, pte);
63d5066f
JG
566 entry = huge_ptep_get(pte);
567
7f08263d 568 i = (start - range->start) >> PAGE_SHIFT;
63d5066f
JG
569 orig_pfn = range->pfns[i];
570 range->pfns[i] = range->values[HMM_PFN_NONE];
571 cpu_flags = pte_to_hmm_pfn_flags(range, entry);
572 fault = write_fault = false;
573 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
574 &fault, &write_fault);
575 if (fault || write_fault) {
576 ret = -ENOENT;
577 goto unlock;
578 }
579
05c23af4 580 pfn = pte_pfn(entry) + ((start & ~hmask) >> PAGE_SHIFT);
7f08263d 581 for (; addr < end; addr += PAGE_SIZE, i++, pfn++)
391aab11
JG
582 range->pfns[i] = hmm_device_entry_from_pfn(range, pfn) |
583 cpu_flags;
63d5066f
JG
584 hmm_vma_walk->last = end;
585
586unlock:
587 spin_unlock(ptl);
588
589 if (ret == -ENOENT)
590 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
591
592 return ret;
63d5066f 593}
251bbe59
CH
594#else
595#define hmm_vma_walk_hugetlb_entry NULL
596#endif /* CONFIG_HUGETLB_PAGE */
63d5066f 597
d28c2c9a
RC
598static int hmm_vma_walk_test(unsigned long start, unsigned long end,
599 struct mm_walk *walk)
33cd47dc 600{
d28c2c9a
RC
601 struct hmm_vma_walk *hmm_vma_walk = walk->private;
602 struct hmm_range *range = hmm_vma_walk->range;
603 struct vm_area_struct *vma = walk->vma;
604
605 /*
c2579c9c
JG
606 * Skip vma ranges that don't have struct page backing them or map I/O
607 * devices directly.
608 *
d28c2c9a 609 * If the vma does not allow read access, then assume that it does not
c2579c9c
JG
610 * allow write access either. HMM does not support architectures that
611 * allow write without read.
d28c2c9a 612 */
c2579c9c
JG
613 if ((vma->vm_flags & (VM_IO | VM_PFNMAP | VM_MIXEDMAP)) ||
614 !(vma->vm_flags & VM_READ)) {
d28c2c9a
RC
615 bool fault, write_fault;
616
617 /*
618 * Check to see if a fault is requested for any page in the
619 * range.
620 */
621 hmm_range_need_fault(hmm_vma_walk, range->pfns +
622 ((start - range->start) >> PAGE_SHIFT),
623 (end - start) >> PAGE_SHIFT,
624 0, &fault, &write_fault);
625 if (fault || write_fault)
626 return -EFAULT;
627
c2579c9c 628 hmm_pfns_fill(start, end, range, HMM_PFN_ERROR);
d28c2c9a
RC
629 hmm_vma_walk->last = end;
630
631 /* Skip this vma and continue processing the next vma. */
632 return 1;
633 }
634
635 return 0;
33cd47dc
JG
636}
637
7b86ac33
CH
638static const struct mm_walk_ops hmm_walk_ops = {
639 .pud_entry = hmm_vma_walk_pud,
640 .pmd_entry = hmm_vma_walk_pmd,
641 .pte_hole = hmm_vma_walk_hole,
642 .hugetlb_entry = hmm_vma_walk_hugetlb_entry,
d28c2c9a 643 .test_walk = hmm_vma_walk_test,
7b86ac33
CH
644};
645
9a4903e4
CH
646/**
647 * hmm_range_fault - try to fault some address in a virtual address range
648 * @range: range being faulted
649 * @flags: HMM_FAULT_* flags
650 *
651 * Return: the number of valid pages in range->pfns[] (from range start
652 * address), which may be zero. On error one of the following status codes
653 * can be returned:
73231612 654 *
9a4903e4
CH
655 * -EINVAL: Invalid arguments or mm or virtual address is in an invalid vma
656 * (e.g., device file vma).
657 * -ENOMEM: Out of memory.
658 * -EPERM: Invalid permission (e.g., asking for write and range is read
659 * only).
660 * -EAGAIN: A page fault needs to be retried and mmap_sem was dropped.
661 * -EBUSY: The range has been invalidated and the caller needs to wait for
662 * the invalidation to finish.
663 * -EFAULT: Invalid (i.e., either no valid vma or it is illegal to access
664 * that range) number of valid pages in range->pfns[] (from
665 * range start address).
74eee180
JG
666 *
667 * This is similar to a regular CPU page fault except that it will not trigger
73231612
JG
668 * any memory migration if the memory being faulted is not accessible by CPUs
669 * and caller does not ask for migration.
74eee180 670 *
ff05c0c6
JG
671 * On error, for one virtual address in the range, the function will mark the
672 * corresponding HMM pfn entry with an error flag.
74eee180 673 */
9a4903e4 674long hmm_range_fault(struct hmm_range *range, unsigned int flags)
74eee180 675{
d28c2c9a
RC
676 struct hmm_vma_walk hmm_vma_walk = {
677 .range = range,
678 .last = range->start,
679 .flags = flags,
680 };
a22dd506 681 struct mm_struct *mm = range->notifier->mm;
74eee180
JG
682 int ret;
683
04ec32fb 684 lockdep_assert_held(&mm->mmap_sem);
704f3f2c 685
a3e0d41c
JG
686 do {
687 /* If range is no longer valid force retry. */
a22dd506
JG
688 if (mmu_interval_check_retry(range->notifier,
689 range->notifier_seq))
2bcbeaef 690 return -EBUSY;
d28c2c9a
RC
691 ret = walk_page_range(mm, hmm_vma_walk.last, range->end,
692 &hmm_walk_ops, &hmm_vma_walk);
693 } while (ret == -EBUSY);
74eee180 694
d28c2c9a
RC
695 if (ret)
696 return ret;
73231612 697 return (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
74eee180 698}
73231612 699EXPORT_SYMBOL(hmm_range_fault);