xref: /linux/mm/hmm.c (revision 2f0f6b0773be0a1ec475097ae54848eea42adc7d)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright 2013 Red Hat Inc.
4  *
5  * Authors: Jérôme Glisse <jglisse@redhat.com>
6  */
7 /*
8  * Refer to include/linux/hmm.h for information about heterogeneous memory
9  * management or HMM for short.
10  */
11 #include <linux/pagewalk.h>
12 #include <linux/hmm.h>
13 #include <linux/hmm-dma.h>
14 #include <linux/init.h>
15 #include <linux/rmap.h>
16 #include <linux/swap.h>
17 #include <linux/slab.h>
18 #include <linux/sched.h>
19 #include <linux/mmzone.h>
20 #include <linux/oom.h>
21 #include <linux/pagemap.h>
22 #include <linux/leafops.h>
23 #include <linux/hugetlb.h>
24 #include <linux/memremap.h>
25 #include <linux/sched/mm.h>
26 #include <linux/jump_label.h>
27 #include <linux/dma-mapping.h>
28 #include <linux/pci-p2pdma.h>
29 #include <linux/mmu_notifier.h>
30 #include <linux/memory_hotplug.h>
31 
32 #include "internal.h"
33 
34 struct hmm_vma_walk {
35 	struct hmm_range	*range;
36 	bool			*locked;
37 	unsigned long		last;
38 	unsigned long		end;
39 	unsigned int		required_fault;
40 };
41 
42 /*
43  * Internal sentinel returned by walk callbacks when they need a page fault.
44  * The callback stores end/required_fault in hmm_vma_walk; the outer loop
45  * consumes the sentinel and never propagates it to the caller.
46  */
47 #define HMM_FAULT_PENDING	-EAGAIN
48 
49 /*
50  * Internal sentinel returned by hmm_do_fault() when handle_mm_fault()
51  * completes a page fault with the mmap lock dropped. hmm_do_fault() sets
52  * *locked = false; the outer loop consumes the sentinel and never propagates
53  * it to the caller.
54  */
55 #define HMM_FAULT_UNLOCKED	-ENOLCK
56 
57 enum {
58 	HMM_NEED_FAULT = 1 << 0,
59 	HMM_NEED_WRITE_FAULT = 1 << 1,
60 	HMM_NEED_ALL_BITS = HMM_NEED_FAULT | HMM_NEED_WRITE_FAULT,
61 };
62 
63 enum {
64 	/* These flags are carried from input-to-output */
65 	HMM_PFN_INOUT_FLAGS = HMM_PFN_DMA_MAPPED | HMM_PFN_P2PDMA |
66 			      HMM_PFN_P2PDMA_BUS,
67 };
68 
69 static int hmm_pfns_fill(unsigned long addr, unsigned long end,
70 			 struct hmm_range *range, unsigned long cpu_flags)
71 {
72 	unsigned long i = (addr - range->start) >> PAGE_SHIFT;
73 
74 	for (; addr < end; addr += PAGE_SIZE, i++) {
75 		range->hmm_pfns[i] &= HMM_PFN_INOUT_FLAGS;
76 		range->hmm_pfns[i] |= cpu_flags;
77 	}
78 	return 0;
79 }
80 
81 /*
82  * hmm_record_fault() - record a range that needs to be faulted in
83  *
84  * Called by the walk callbacks when they discover that part of the range
85  * needs a page fault.  The callback records what to fault and returns
86  * HMM_FAULT_PENDING; the outer loop in hmm_range_fault_locked() drops
87  * back out of walk_page_range() and invokes handle_mm_fault() from a context
88  * where no page-table or hugetlb_vma_lock is held.
89  */
90 static int hmm_record_fault(unsigned long addr, unsigned long end,
91 			    unsigned int required_fault,
92 			    struct mm_walk *walk)
93 {
94 	struct hmm_vma_walk *hmm_vma_walk = walk->private;
95 
96 	WARN_ON_ONCE(!required_fault);
97 	hmm_vma_walk->last = addr;
98 	hmm_vma_walk->end = end;
99 	hmm_vma_walk->required_fault = required_fault;
100 	return HMM_FAULT_PENDING;
101 }
102 
103 static unsigned int hmm_pte_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
104 				       unsigned long pfn_req_flags,
105 				       unsigned long cpu_flags)
106 {
107 	struct hmm_range *range = hmm_vma_walk->range;
108 
109 	/*
110 	 * So we not only consider the individual per page request we also
111 	 * consider the default flags requested for the range. The API can
112 	 * be used 2 ways. The first one where the HMM user coalesces
113 	 * multiple page faults into one request and sets flags per pfn for
114 	 * those faults. The second one where the HMM user wants to pre-
115 	 * fault a range with specific flags. For the latter one it is a
116 	 * waste to have the user pre-fill the pfn arrays with a default
117 	 * flags value.
118 	 */
119 	pfn_req_flags &= range->pfn_flags_mask;
120 	pfn_req_flags |= range->default_flags;
121 
122 	/* We aren't ask to do anything ... */
123 	if (!(pfn_req_flags & HMM_PFN_REQ_FAULT))
124 		return 0;
125 
126 	/* Need to write fault ? */
127 	if ((pfn_req_flags & HMM_PFN_REQ_WRITE) &&
128 	    !(cpu_flags & HMM_PFN_WRITE))
129 		return HMM_NEED_FAULT | HMM_NEED_WRITE_FAULT;
130 
131 	/* If CPU page table is not valid then we need to fault */
132 	if (!(cpu_flags & HMM_PFN_VALID))
133 		return HMM_NEED_FAULT;
134 	return 0;
135 }
136 
137 static unsigned int
138 hmm_range_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
139 		     const unsigned long hmm_pfns[], unsigned long npages,
140 		     unsigned long cpu_flags)
141 {
142 	struct hmm_range *range = hmm_vma_walk->range;
143 	unsigned int required_fault = 0;
144 	unsigned long i;
145 
146 	/*
147 	 * If the default flags do not request to fault pages, and the mask does
148 	 * not allow for individual pages to be faulted, then
149 	 * hmm_pte_need_fault() will always return 0.
150 	 */
151 	if (!((range->default_flags | range->pfn_flags_mask) &
152 	      HMM_PFN_REQ_FAULT))
153 		return 0;
154 
155 	for (i = 0; i < npages; ++i) {
156 		required_fault |= hmm_pte_need_fault(hmm_vma_walk, hmm_pfns[i],
157 						     cpu_flags);
158 		if (required_fault == HMM_NEED_ALL_BITS)
159 			return required_fault;
160 	}
161 	return required_fault;
162 }
163 
164 static int hmm_vma_walk_hole(unsigned long addr, unsigned long end,
165 			     __always_unused int depth, struct mm_walk *walk)
166 {
167 	struct hmm_vma_walk *hmm_vma_walk = walk->private;
168 	struct hmm_range *range = hmm_vma_walk->range;
169 	unsigned int required_fault;
170 	unsigned long i, npages;
171 	unsigned long *hmm_pfns;
172 
173 	i = (addr - range->start) >> PAGE_SHIFT;
174 	npages = (end - addr) >> PAGE_SHIFT;
175 	hmm_pfns = &range->hmm_pfns[i];
176 	required_fault =
177 		hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0);
178 	if (!walk->vma) {
179 		if (required_fault)
180 			return -EFAULT;
181 		return hmm_pfns_fill(addr, end, range, HMM_PFN_ERROR);
182 	}
183 	if (required_fault)
184 		return hmm_record_fault(addr, end, required_fault, walk);
185 	return hmm_pfns_fill(addr, end, range, 0);
186 }
187 
188 static inline unsigned long hmm_pfn_flags_order(unsigned long order)
189 {
190 	return order << HMM_PFN_ORDER_SHIFT;
191 }
192 
193 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
194 static inline unsigned long pmd_to_hmm_pfn_flags(struct hmm_range *range,
195 						 pmd_t pmd)
196 {
197 	if (pmd_protnone(pmd))
198 		return 0;
199 	return (pmd_write(pmd) ? (HMM_PFN_VALID | HMM_PFN_WRITE) :
200 				 HMM_PFN_VALID) |
201 	       hmm_pfn_flags_order(PMD_SHIFT - PAGE_SHIFT);
202 }
203 
204 static int hmm_vma_handle_pmd(struct mm_walk *walk, unsigned long addr,
205 			      unsigned long end, unsigned long hmm_pfns[],
206 			      pmd_t pmd)
207 {
208 	struct hmm_vma_walk *hmm_vma_walk = walk->private;
209 	struct hmm_range *range = hmm_vma_walk->range;
210 	unsigned long pfn, npages, i;
211 	unsigned int required_fault;
212 	unsigned long cpu_flags;
213 
214 	npages = (end - addr) >> PAGE_SHIFT;
215 	cpu_flags = pmd_to_hmm_pfn_flags(range, pmd);
216 	required_fault =
217 		hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, cpu_flags);
218 	if (required_fault)
219 		return hmm_record_fault(addr, end, required_fault, walk);
220 
221 	pfn = pmd_pfn(pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
222 	for (i = 0; addr < end; addr += PAGE_SIZE, i++, pfn++) {
223 		hmm_pfns[i] &= HMM_PFN_INOUT_FLAGS;
224 		hmm_pfns[i] |= pfn | cpu_flags;
225 	}
226 	return 0;
227 }
228 #else /* CONFIG_TRANSPARENT_HUGEPAGE */
229 /* stub to allow the code below to compile */
230 int hmm_vma_handle_pmd(struct mm_walk *walk, unsigned long addr,
231 		unsigned long end, unsigned long hmm_pfns[], pmd_t pmd);
232 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
233 
234 static inline unsigned long pte_to_hmm_pfn_flags(struct hmm_range *range,
235 						 pte_t pte)
236 {
237 	if (pte_none(pte) || !pte_present(pte) || pte_protnone(pte))
238 		return 0;
239 	return pte_write(pte) ? (HMM_PFN_VALID | HMM_PFN_WRITE) : HMM_PFN_VALID;
240 }
241 
242 static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,
243 			      unsigned long end, pmd_t *pmdp, pte_t *ptep,
244 			      unsigned long *hmm_pfn)
245 {
246 	struct hmm_vma_walk *hmm_vma_walk = walk->private;
247 	struct hmm_range *range = hmm_vma_walk->range;
248 	unsigned int required_fault;
249 	unsigned long cpu_flags;
250 	pte_t pte = ptep_get(ptep);
251 	uint64_t pfn_req_flags = *hmm_pfn;
252 	uint64_t new_pfn_flags = 0;
253 
254 	/*
255 	 * Any other marker than a UFFD WP marker will result in a fault error
256 	 * that will be correctly handled, so we need only check for UFFD WP
257 	 * here.
258 	 */
259 	if (pte_none(pte) || pte_is_uffd_wp_marker(pte)) {
260 		required_fault =
261 			hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, 0);
262 		if (required_fault)
263 			goto fault;
264 		goto out;
265 	}
266 
267 	if (!pte_present(pte)) {
268 		const softleaf_t entry = softleaf_from_pte(pte);
269 
270 		/*
271 		 * Don't fault in device private pages owned by the caller,
272 		 * just report the PFN.
273 		 */
274 		if (softleaf_is_device_private(entry) &&
275 		    page_pgmap(softleaf_to_page(entry))->owner ==
276 		    range->dev_private_owner) {
277 			cpu_flags = HMM_PFN_VALID;
278 			if (softleaf_is_device_private_write(entry))
279 				cpu_flags |= HMM_PFN_WRITE;
280 			new_pfn_flags = softleaf_to_pfn(entry) | cpu_flags;
281 			goto out;
282 		}
283 
284 		required_fault =
285 			hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, 0);
286 		if (!required_fault)
287 			goto out;
288 
289 		if (softleaf_is_swap(entry))
290 			goto fault;
291 
292 		if (softleaf_is_device_private(entry))
293 			goto fault;
294 
295 		if (softleaf_is_device_exclusive(entry))
296 			goto fault;
297 
298 		if (softleaf_is_migration(entry)) {
299 			pte_unmap(ptep);
300 			hmm_vma_walk->last = addr;
301 			migration_entry_wait(walk->mm, pmdp, addr);
302 			return -EBUSY;
303 		}
304 
305 		/* Report error for everything else */
306 		pte_unmap(ptep);
307 		return -EFAULT;
308 	}
309 
310 	cpu_flags = pte_to_hmm_pfn_flags(range, pte);
311 	required_fault =
312 		hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, cpu_flags);
313 	if (required_fault)
314 		goto fault;
315 
316 	/*
317 	 * Since each architecture defines a struct page for the zero page, just
318 	 * fall through and treat it like a normal page.
319 	 */
320 	if (!vm_normal_page(walk->vma, addr, pte) &&
321 	    !is_zero_pfn(pte_pfn(pte))) {
322 		if (hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, 0)) {
323 			pte_unmap(ptep);
324 			return -EFAULT;
325 		}
326 		new_pfn_flags = HMM_PFN_ERROR;
327 		goto out;
328 	}
329 
330 	new_pfn_flags = pte_pfn(pte) | cpu_flags;
331 out:
332 	*hmm_pfn = (*hmm_pfn & HMM_PFN_INOUT_FLAGS) | new_pfn_flags;
333 	return 0;
334 
335 fault:
336 	pte_unmap(ptep);
337 	/* Fault any virtual address we were asked to fault */
338 	return hmm_record_fault(addr, end, required_fault, walk);
339 }
340 
341 #ifdef CONFIG_ARCH_HAS_PMD_SOFTLEAVES
342 static int hmm_vma_handle_absent_pmd(struct mm_walk *walk, unsigned long start,
343 				     unsigned long end, unsigned long *hmm_pfns,
344 				     pmd_t pmd)
345 {
346 	struct hmm_vma_walk *hmm_vma_walk = walk->private;
347 	struct hmm_range *range = hmm_vma_walk->range;
348 	unsigned long npages = (end - start) >> PAGE_SHIFT;
349 	const softleaf_t entry = softleaf_from_pmd(pmd);
350 	unsigned long addr = start;
351 	unsigned int required_fault;
352 
353 	if (softleaf_is_device_private(entry) &&
354 	    softleaf_to_folio(entry)->pgmap->owner ==
355 	    range->dev_private_owner) {
356 		unsigned long cpu_flags = HMM_PFN_VALID |
357 			hmm_pfn_flags_order(PMD_SHIFT - PAGE_SHIFT);
358 		unsigned long pfn = softleaf_to_pfn(entry);
359 		unsigned long i;
360 
361 		if (softleaf_is_device_private_write(entry))
362 			cpu_flags |= HMM_PFN_WRITE;
363 
364 		/*
365 		 * Fully populate the PFN list though subsequent PFNs could be
366 		 * inferred, because drivers which are not yet aware of large
367 		 * folios probably do not support sparsely populated PFN lists.
368 		 */
369 		for (i = 0; addr < end; addr += PAGE_SIZE, i++, pfn++) {
370 			hmm_pfns[i] &= HMM_PFN_INOUT_FLAGS;
371 			hmm_pfns[i] |= pfn | cpu_flags;
372 		}
373 
374 		return 0;
375 	}
376 
377 	required_fault = hmm_range_need_fault(hmm_vma_walk, hmm_pfns,
378 					      npages, 0);
379 	if (required_fault) {
380 		if (softleaf_is_device_private(entry))
381 			return hmm_record_fault(addr, end, required_fault, walk);
382 		else
383 			return -EFAULT;
384 	}
385 
386 	return hmm_pfns_fill(start, end, range, HMM_PFN_ERROR);
387 }
388 #else
389 static int hmm_vma_handle_absent_pmd(struct mm_walk *walk, unsigned long start,
390 				     unsigned long end, unsigned long *hmm_pfns,
391 				     pmd_t pmd)
392 {
393 	struct hmm_vma_walk *hmm_vma_walk = walk->private;
394 	struct hmm_range *range = hmm_vma_walk->range;
395 	unsigned long npages = (end - start) >> PAGE_SHIFT;
396 
397 	if (hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0))
398 		return -EFAULT;
399 	return hmm_pfns_fill(start, end, range, HMM_PFN_ERROR);
400 }
401 #endif  /* CONFIG_ARCH_HAS_PMD_SOFTLEAVES */
402 
403 static int hmm_vma_walk_pmd(pmd_t *pmdp,
404 			    unsigned long start,
405 			    unsigned long end,
406 			    struct mm_walk *walk)
407 {
408 	struct hmm_vma_walk *hmm_vma_walk = walk->private;
409 	struct hmm_range *range = hmm_vma_walk->range;
410 	unsigned long *hmm_pfns =
411 		&range->hmm_pfns[(start - range->start) >> PAGE_SHIFT];
412 	unsigned long npages = (end - start) >> PAGE_SHIFT;
413 	unsigned long addr = start;
414 	pte_t *ptep;
415 	pmd_t pmd;
416 
417 again:
418 	pmd = pmdp_get_lockless(pmdp);
419 	if (pmd_none(pmd))
420 		return hmm_vma_walk_hole(start, end, -1, walk);
421 
422 	if (thp_migration_supported() && pmd_is_migration_entry(pmd)) {
423 		if (hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0)) {
424 			hmm_vma_walk->last = addr;
425 			pmd_migration_entry_wait(walk->mm, pmdp);
426 			return -EBUSY;
427 		}
428 		return hmm_pfns_fill(start, end, range, 0);
429 	}
430 
431 	if (!pmd_present(pmd))
432 		return hmm_vma_handle_absent_pmd(walk, start, end, hmm_pfns,
433 						 pmd);
434 
435 	if (pmd_trans_huge(pmd)) {
436 		/*
437 		 * No need to take pmd_lock here, even if some other thread
438 		 * is splitting the huge pmd we will get that event through
439 		 * mmu_notifier callback.
440 		 *
441 		 * So just read pmd value and check again it's a transparent
442 		 * huge or device mapping one and compute corresponding pfn
443 		 * values.
444 		 */
445 		pmd = pmdp_get_lockless(pmdp);
446 		if (!pmd_trans_huge(pmd))
447 			goto again;
448 
449 		return hmm_vma_handle_pmd(walk, addr, end, hmm_pfns, pmd);
450 	}
451 
452 	/*
453 	 * We have handled all the valid cases above ie either none, migration,
454 	 * huge or transparent huge. At this point either it is a valid pmd
455 	 * entry pointing to pte directory or it is a bad pmd that will not
456 	 * recover.
457 	 */
458 	if (pmd_bad(pmd)) {
459 		if (hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0))
460 			return -EFAULT;
461 		return hmm_pfns_fill(start, end, range, HMM_PFN_ERROR);
462 	}
463 
464 	ptep = pte_offset_map(pmdp, addr);
465 	if (!ptep)
466 		goto again;
467 	for (; addr < end; addr += PAGE_SIZE, ptep++, hmm_pfns++) {
468 		int r;
469 
470 		r = hmm_vma_handle_pte(walk, addr, end, pmdp, ptep, hmm_pfns);
471 		if (r) {
472 			/* hmm_vma_handle_pte() did pte_unmap() */
473 			return r;
474 		}
475 	}
476 	pte_unmap(ptep - 1);
477 	return 0;
478 }
479 
480 #if defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
481 static inline unsigned long pud_to_hmm_pfn_flags(struct hmm_range *range,
482 						 pud_t pud)
483 {
484 	if (!pud_present(pud))
485 		return 0;
486 	return (pud_write(pud) ? (HMM_PFN_VALID | HMM_PFN_WRITE) :
487 				 HMM_PFN_VALID) |
488 	       hmm_pfn_flags_order(PUD_SHIFT - PAGE_SHIFT);
489 }
490 
491 static int hmm_vma_walk_pud(pud_t *pudp, unsigned long start, unsigned long end,
492 		struct mm_walk *walk)
493 {
494 	struct hmm_vma_walk *hmm_vma_walk = walk->private;
495 	struct hmm_range *range = hmm_vma_walk->range;
496 	unsigned long addr = start;
497 	pud_t pud;
498 	spinlock_t *ptl = pud_trans_huge_lock(pudp, walk->vma);
499 
500 	if (!ptl)
501 		return 0;
502 
503 	/* Normally we don't want to split the huge page */
504 	walk->action = ACTION_CONTINUE;
505 
506 	pud = pudp_get(pudp);
507 	if (!pud_present(pud)) {
508 		spin_unlock(ptl);
509 		return hmm_vma_walk_hole(start, end, -1, walk);
510 	}
511 
512 	if (pud_leaf(pud)) {
513 		unsigned long i, npages, pfn;
514 		unsigned int required_fault;
515 		unsigned long *hmm_pfns;
516 		unsigned long cpu_flags;
517 
518 		i = (addr - range->start) >> PAGE_SHIFT;
519 		npages = (end - addr) >> PAGE_SHIFT;
520 		hmm_pfns = &range->hmm_pfns[i];
521 
522 		cpu_flags = pud_to_hmm_pfn_flags(range, pud);
523 		required_fault = hmm_range_need_fault(hmm_vma_walk, hmm_pfns,
524 						      npages, cpu_flags);
525 		if (required_fault) {
526 			spin_unlock(ptl);
527 			return hmm_record_fault(addr, end, required_fault, walk);
528 		}
529 
530 		pfn = pud_pfn(pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
531 		for (i = 0; i < npages; ++i, ++pfn) {
532 			hmm_pfns[i] &= HMM_PFN_INOUT_FLAGS;
533 			hmm_pfns[i] |= pfn | cpu_flags;
534 		}
535 		goto out_unlock;
536 	}
537 
538 	/* Ask for the PUD to be split */
539 	walk->action = ACTION_SUBTREE;
540 
541 out_unlock:
542 	spin_unlock(ptl);
543 	return 0;
544 }
545 #else
546 #define hmm_vma_walk_pud	NULL
547 #endif
548 
549 #ifdef CONFIG_HUGETLB_PAGE
550 static int hmm_vma_walk_hugetlb_entry(pte_t *pte, unsigned long hmask,
551 				      unsigned long start, unsigned long end,
552 				      struct mm_walk *walk)
553 {
554 	unsigned long addr = start, i, pfn;
555 	struct hmm_vma_walk *hmm_vma_walk = walk->private;
556 	struct hmm_range *range = hmm_vma_walk->range;
557 	struct vm_area_struct *vma = walk->vma;
558 	unsigned int required_fault;
559 	unsigned long pfn_req_flags;
560 	unsigned long cpu_flags;
561 	spinlock_t *ptl;
562 	pte_t entry;
563 
564 	ptl = huge_pte_lock(hstate_vma(vma), walk->mm, pte);
565 	entry = huge_ptep_get(walk->mm, addr, pte);
566 
567 	i = (start - range->start) >> PAGE_SHIFT;
568 	pfn_req_flags = range->hmm_pfns[i];
569 	cpu_flags = pte_to_hmm_pfn_flags(range, entry) |
570 		    hmm_pfn_flags_order(huge_page_order(hstate_vma(vma)));
571 	required_fault =
572 		hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, cpu_flags);
573 	if (required_fault) {
574 		spin_unlock(ptl);
575 		return hmm_record_fault(addr, end, required_fault, walk);
576 	}
577 
578 	pfn = pte_pfn(entry) + ((start & ~hmask) >> PAGE_SHIFT);
579 	for (; addr < end; addr += PAGE_SIZE, i++, pfn++) {
580 		range->hmm_pfns[i] &= HMM_PFN_INOUT_FLAGS;
581 		range->hmm_pfns[i] |= pfn | cpu_flags;
582 	}
583 
584 	spin_unlock(ptl);
585 	return 0;
586 }
587 #else
588 #define hmm_vma_walk_hugetlb_entry NULL
589 #endif /* CONFIG_HUGETLB_PAGE */
590 
591 static int hmm_vma_walk_test(unsigned long start, unsigned long end,
592 			     struct mm_walk *walk)
593 {
594 	struct hmm_vma_walk *hmm_vma_walk = walk->private;
595 	struct hmm_range *range = hmm_vma_walk->range;
596 	struct vm_area_struct *vma = walk->vma;
597 
598 	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)) &&
599 	    vma->vm_flags & VM_READ)
600 		return 0;
601 
602 	/*
603 	 * vma ranges that don't have struct page backing them or map I/O
604 	 * devices directly cannot be handled by hmm_range_fault().
605 	 *
606 	 * If the vma does not allow read access, then assume that it does not
607 	 * allow write access either. HMM does not support architectures that
608 	 * allow write without read.
609 	 *
610 	 * If a fault is requested for an unsupported range then it is a hard
611 	 * failure.
612 	 */
613 	if (hmm_range_need_fault(hmm_vma_walk,
614 				 range->hmm_pfns +
615 					 ((start - range->start) >> PAGE_SHIFT),
616 				 (end - start) >> PAGE_SHIFT, 0))
617 		return -EFAULT;
618 
619 	hmm_pfns_fill(start, end, range, HMM_PFN_ERROR);
620 
621 	/* Skip this vma and continue processing the next vma. */
622 	return 1;
623 }
624 
625 static const struct mm_walk_ops hmm_walk_ops = {
626 	.pud_entry	= hmm_vma_walk_pud,
627 	.pmd_entry	= hmm_vma_walk_pmd,
628 	.pte_hole	= hmm_vma_walk_hole,
629 	.hugetlb_entry	= hmm_vma_walk_hugetlb_entry,
630 	.test_walk	= hmm_vma_walk_test,
631 	.walk_lock	= PGWALK_RDLOCK,
632 };
633 
634 /*
635  * hmm_do_fault - fault in a range recorded by a walk callback
636  *
637  * Called from the outer loop in hmm_range_fault_locked() after a callback
638  * returned HMM_FAULT_PENDING.  At this point we hold only mmap_lock;
639  * the page-table spinlock and any hugetlb_vma_lock acquired by the walk
640  * framework have already been released by the unwind.
641  *
642  * Returns -EBUSY on success (all pages faulted, caller should re-walk).
643  * Returns a negative errno on failure.
644  */
645 static int hmm_do_fault(struct mm_struct *mm,
646 			struct hmm_vma_walk *hmm_vma_walk)
647 {
648 	unsigned long addr = hmm_vma_walk->last;
649 	unsigned long end = hmm_vma_walk->end;
650 	unsigned int required_fault = hmm_vma_walk->required_fault;
651 	unsigned int fault_flags = FAULT_FLAG_REMOTE;
652 	struct vm_area_struct *vma;
653 
654 	if (hmm_vma_walk->locked)
655 		fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
656 
657 	vma = vma_lookup(mm, addr);
658 	if (!vma)
659 		return -EFAULT;
660 
661 	if (required_fault & HMM_NEED_WRITE_FAULT) {
662 		if (!(vma->vm_flags & VM_WRITE))
663 			return -EPERM;
664 		fault_flags |= FAULT_FLAG_WRITE;
665 	}
666 
667 	for (; addr < end; addr += PAGE_SIZE) {
668 		vm_fault_t ret;
669 
670 		ret = handle_mm_fault(vma, addr, fault_flags, NULL);
671 
672 		if (ret & (VM_FAULT_COMPLETED | VM_FAULT_RETRY)) {
673 			*hmm_vma_walk->locked = false;
674 			return HMM_FAULT_UNLOCKED;
675 		}
676 
677 		if (ret & VM_FAULT_ERROR) {
678 			int err = vm_fault_to_errno(ret, 0);
679 
680 			if (WARN_ON(!err))
681 				err = -EINVAL;
682 
683 			return err;
684 		}
685 	}
686 
687 	return -EBUSY;
688 }
689 
690 static int hmm_range_fault_locked(struct hmm_range *range, bool *locked)
691 {
692 	struct hmm_vma_walk hmm_vma_walk = {
693 		.range = range,
694 		.locked = locked,
695 		.last = range->start,
696 	};
697 	struct mm_struct *mm = range->notifier->mm;
698 	int ret;
699 
700 	mmap_assert_locked(mm);
701 
702 	do {
703 		/* If range is no longer valid force retry. */
704 		if (mmu_interval_check_retry(range->notifier,
705 					     range->notifier_seq))
706 			return -EBUSY;
707 		ret = walk_page_range(mm, hmm_vma_walk.last, range->end,
708 				      &hmm_walk_ops, &hmm_vma_walk);
709 		/*
710 		 * When HMM_FAULT_PENDING is returned a walk callback
711 		 * recorded a range that needs handle_mm_fault();
712 		 * hmm_do_fault() runs the fault outside walk_page_range()
713 		 * (so no page-table or hugetlb_vma_lock is held) and
714 		 * returns -EBUSY so the loop re-walks and picks up the
715 		 * now-present entries.
716 		 */
717 		if (ret == HMM_FAULT_PENDING) {
718 			ret = hmm_do_fault(mm, &hmm_vma_walk);
719 			if (ret == HMM_FAULT_UNLOCKED) {
720 				if (fatal_signal_pending(current))
721 					return -EINTR;
722 				return -EBUSY;
723 			}
724 		}
725 		/*
726 		 * When -EBUSY is returned the loop restarts with
727 		 * hmm_vma_walk.last set to an address that has not been stored
728 		 * in pfns. All entries < last in the pfn array are set to their
729 		 * output, and all >= are still at their input values.
730 		 */
731 	} while (ret == -EBUSY);
732 	return ret;
733 }
734 
735 /**
736  * hmm_range_fault - try to fault some address in a virtual address range
737  * @range:	argument structure
738  *
739  * Returns 0 on success or one of the following error codes:
740  *
741  * -EINVAL:	Invalid arguments or mm or virtual address is in an invalid vma
742  *		(e.g., device file vma).
743  * -ENOMEM:	Out of memory.
744  * -EPERM:	Invalid permission (e.g., asking for write and range is read
745  *		only).
746  * -EBUSY:	The range has been invalidated and the caller needs to wait for
747  *		the invalidation to finish.
748  * -EFAULT:     A page was requested to be valid and could not be made valid
749  *              ie it has no backing VMA or it is illegal to access
750  *
751  * This is similar to get_user_pages(), except that it can read the page tables
752  * without mutating them (ie causing faults).
753  *
754  * The mmap lock must be held by the caller and will remain held on return.
755  * New users should prefer hmm_range_fault_unlocked_timeout() unless they
756  * specifically need to keep the mmap lock held across the call. This helper
757  * cannot support VMAs whose fault handlers need to drop the mmap lock.
758  */
759 int hmm_range_fault(struct hmm_range *range)
760 {
761 	return hmm_range_fault_locked(range, NULL);
762 }
763 EXPORT_SYMBOL(hmm_range_fault);
764 
765 /**
766  * hmm_range_fault_unlocked_timeout - fault in a range with a retry timeout
767  * @range:	argument structure
768  * @timeout:	timeout in jiffies for internal -EBUSY retries, or 0 to retry
769  *		indefinitely
770  *
771  * The caller must not hold the mmap lock. The function takes the mmap read
772  * lock internally and allows handle_mm_fault() to drop it during faults. If
773  * the mmap lock is dropped or the range is invalidated, the function refreshes
774  * range->notifier_seq and restarts the walk internally.
775  *
776  * Passing 0 for @timeout retries indefinitely. A non-zero @timeout is a caller
777  * policy limit for repeated mmu-notifier invalidation retries. HMM does not
778  * interrupt page fault handling when the timeout expires, but returns -EBUSY
779  * if the retry budget is exhausted before a stable range is obtained.
780  *
781  * Returns 0 on success or one of the error codes documented for
782  * hmm_range_fault(). -EINTR is returned if mmap_lock acquisition is
783  * interrupted or a fatal signal is pending during retry handling.
784  */
785 int hmm_range_fault_unlocked_timeout(struct hmm_range *range,
786 				     unsigned long timeout)
787 {
788 	struct mm_struct *mm = range->notifier->mm;
789 	unsigned long deadline = 0;
790 	bool locked = false;
791 	int ret;
792 
793 	do {
794 		/*
795 		 * If the previous fault dropped mmap_lock, then the fault
796 		 * handler made progress. Restart the retry timeout in that
797 		 * case, but keep the existing deadline for ordinary -EBUSY
798 		 * retries.
799 		 */
800 		if (timeout && !locked)
801 			deadline = jiffies + timeout;
802 
803 		range->notifier_seq =
804 			mmu_interval_read_begin(range->notifier);
805 
806 		ret = mmap_read_lock_killable(mm);
807 		if (ret)
808 			return ret;
809 
810 		if (check_stable_address_space(mm)) {
811 			mmap_read_unlock(mm);
812 			return -EFAULT;
813 		}
814 
815 		if (timeout && time_after(jiffies, deadline)) {
816 			mmap_read_unlock(mm);
817 			return -EBUSY;
818 		}
819 
820 		locked = true;
821 		ret = hmm_range_fault_locked(range, &locked);
822 		if (locked)
823 			mmap_read_unlock(mm);
824 	} while (ret == -EBUSY);
825 
826 	return ret;
827 }
828 EXPORT_SYMBOL(hmm_range_fault_unlocked_timeout);
829 
830 /**
831  * hmm_dma_map_alloc - Allocate HMM map structure
832  * @dev: device to allocate structure for
833  * @map: HMM map to allocate
834  * @nr_entries: number of entries in the map
835  * @dma_entry_size: size of the DMA entry in the map
836  *
837  * Allocate the HMM map structure and all the lists it contains.
838  * Return 0 on success, -ENOMEM on failure.
839  */
840 int hmm_dma_map_alloc(struct device *dev, struct hmm_dma_map *map,
841 		      size_t nr_entries, size_t dma_entry_size)
842 {
843 	bool dma_need_sync = false;
844 	bool use_iova;
845 
846 	WARN_ON_ONCE(!(nr_entries * PAGE_SIZE / dma_entry_size));
847 
848 	/*
849 	 * The HMM API violates our normal DMA buffer ownership rules and can't
850 	 * transfer buffer ownership.  The dma_addressing_limited() check is a
851 	 * best approximation to ensure no swiotlb buffering happens.
852 	 */
853 #ifdef CONFIG_DMA_NEED_SYNC
854 	dma_need_sync = !dev_dma_skip_sync(dev);
855 #endif /* CONFIG_DMA_NEED_SYNC */
856 	if (dma_need_sync || dma_addressing_limited(dev))
857 		return -EOPNOTSUPP;
858 
859 	map->dma_entry_size = dma_entry_size;
860 	map->pfn_list = kvcalloc(nr_entries, sizeof(*map->pfn_list),
861 				 GFP_KERNEL | __GFP_NOWARN);
862 	if (!map->pfn_list)
863 		return -ENOMEM;
864 
865 	use_iova = dma_iova_try_alloc(dev, &map->state, 0,
866 			nr_entries * PAGE_SIZE);
867 	if (!use_iova && dma_need_unmap(dev)) {
868 		map->dma_list = kvzalloc_objs(*map->dma_list, nr_entries,
869 					      GFP_KERNEL | __GFP_NOWARN);
870 		if (!map->dma_list)
871 			goto err_dma;
872 	}
873 	return 0;
874 
875 err_dma:
876 	kvfree(map->pfn_list);
877 	return -ENOMEM;
878 }
879 EXPORT_SYMBOL_GPL(hmm_dma_map_alloc);
880 
881 /**
882  * hmm_dma_map_free - iFree HMM map structure
883  * @dev: device to free structure from
884  * @map: HMM map containing the various lists and state
885  *
886  * Free the HMM map structure and all the lists it contains.
887  */
888 void hmm_dma_map_free(struct device *dev, struct hmm_dma_map *map)
889 {
890 	if (dma_use_iova(&map->state))
891 		dma_iova_free(dev, &map->state);
892 	kvfree(map->pfn_list);
893 	kvfree(map->dma_list);
894 }
895 EXPORT_SYMBOL_GPL(hmm_dma_map_free);
896 
897 /**
898  * hmm_dma_map_pfn - Map a physical HMM page to DMA address
899  * @dev: Device to map the page for
900  * @map: HMM map
901  * @idx: Index into the PFN and dma address arrays
902  * @p2pdma_state: PCI P2P state.
903  *
904  * dma_alloc_iova() allocates IOVA based on the size specified by their use in
905  * iova->size. Call this function after IOVA allocation to link whole @page
906  * to get the DMA address. Note that very first call to this function
907  * will have @offset set to 0 in the IOVA space allocated from
908  * dma_alloc_iova(). For subsequent calls to this function on same @iova,
909  * @offset needs to be advanced by the caller with the size of previous
910  * page that was linked + DMA address returned for the previous page that was
911  * linked by this function.
912  */
913 dma_addr_t hmm_dma_map_pfn(struct device *dev, struct hmm_dma_map *map,
914 			   size_t idx,
915 			   struct pci_p2pdma_map_state *p2pdma_state)
916 {
917 	struct dma_iova_state *state = &map->state;
918 	dma_addr_t *dma_addrs = map->dma_list;
919 	unsigned long *pfns = map->pfn_list;
920 	struct page *page = hmm_pfn_to_page(pfns[idx]);
921 	phys_addr_t paddr = hmm_pfn_to_phys(pfns[idx]);
922 	size_t offset = idx * map->dma_entry_size;
923 	unsigned long attrs = DMA_ATTR_REQUIRE_COHERENT;
924 	dma_addr_t dma_addr;
925 	int ret;
926 
927 	if ((pfns[idx] & HMM_PFN_DMA_MAPPED) &&
928 	    !(pfns[idx] & HMM_PFN_P2PDMA_BUS)) {
929 		/*
930 		 * We are in this flow when there is a need to resync flags,
931 		 * for example when page was already linked in prefetch call
932 		 * with READ flag and now we need to add WRITE flag
933 		 *
934 		 * This page was already programmed to HW and we don't want/need
935 		 * to unlink and link it again just to resync flags.
936 		 */
937 		if (dma_use_iova(state))
938 			return state->addr + offset;
939 
940 		/*
941 		 * Without dma_need_unmap, the dma_addrs array is NULL, thus we
942 		 * need to regenerate the address below even if there already
943 		 * was a mapping. But !dma_need_unmap implies that the
944 		 * mapping stateless, so this is fine.
945 		 */
946 		if (dma_need_unmap(dev))
947 			return dma_addrs[idx];
948 
949 		/* Continue to remapping */
950 	}
951 
952 	switch (pci_p2pdma_state(p2pdma_state, dev, page)) {
953 	case PCI_P2PDMA_MAP_NONE:
954 		break;
955 	case PCI_P2PDMA_MAP_THRU_HOST_BRIDGE:
956 		attrs |= DMA_ATTR_MMIO;
957 		pfns[idx] |= HMM_PFN_P2PDMA;
958 		break;
959 	case PCI_P2PDMA_MAP_BUS_ADDR:
960 		pfns[idx] |= HMM_PFN_P2PDMA_BUS | HMM_PFN_DMA_MAPPED;
961 		return pci_p2pdma_bus_addr_map(p2pdma_state->mem, paddr);
962 	default:
963 		return DMA_MAPPING_ERROR;
964 	}
965 
966 	if (dma_use_iova(state)) {
967 		ret = dma_iova_link(dev, state, paddr, offset,
968 				    map->dma_entry_size, DMA_BIDIRECTIONAL,
969 				    attrs);
970 		if (ret)
971 			goto error;
972 
973 		ret = dma_iova_sync(dev, state, offset, map->dma_entry_size);
974 		if (ret) {
975 			dma_iova_unlink(dev, state, offset, map->dma_entry_size,
976 					DMA_BIDIRECTIONAL, attrs);
977 			goto error;
978 		}
979 
980 		dma_addr = state->addr + offset;
981 	} else {
982 		if (WARN_ON_ONCE(dma_need_unmap(dev) && !dma_addrs))
983 			goto error;
984 
985 		dma_addr = dma_map_phys(dev, paddr, map->dma_entry_size,
986 					DMA_BIDIRECTIONAL, attrs);
987 		if (dma_mapping_error(dev, dma_addr))
988 			goto error;
989 
990 		if (dma_need_unmap(dev))
991 			dma_addrs[idx] = dma_addr;
992 	}
993 	pfns[idx] |= HMM_PFN_DMA_MAPPED;
994 	return dma_addr;
995 error:
996 	pfns[idx] &= ~HMM_PFN_P2PDMA;
997 	return DMA_MAPPING_ERROR;
998 
999 }
1000 EXPORT_SYMBOL_GPL(hmm_dma_map_pfn);
1001 
1002 /**
1003  * hmm_dma_unmap_pfn - Unmap a physical HMM page from DMA address
1004  * @dev: Device to unmap the page from
1005  * @map: HMM map
1006  * @idx: Index of the PFN to unmap
1007  *
1008  * Returns true if the PFN was mapped and has been unmapped, false otherwise.
1009  */
1010 bool hmm_dma_unmap_pfn(struct device *dev, struct hmm_dma_map *map, size_t idx)
1011 {
1012 	const unsigned long valid_dma = HMM_PFN_VALID | HMM_PFN_DMA_MAPPED;
1013 	struct dma_iova_state *state = &map->state;
1014 	dma_addr_t *dma_addrs = map->dma_list;
1015 	unsigned long *pfns = map->pfn_list;
1016 	unsigned long attrs = DMA_ATTR_REQUIRE_COHERENT;
1017 
1018 	if ((pfns[idx] & valid_dma) != valid_dma)
1019 		return false;
1020 
1021 	if (pfns[idx] & HMM_PFN_P2PDMA)
1022 		attrs |= DMA_ATTR_MMIO;
1023 
1024 	if (pfns[idx] & HMM_PFN_P2PDMA_BUS)
1025 		; /* no need to unmap bus address P2P mappings */
1026 	else if (dma_use_iova(state))
1027 		dma_iova_unlink(dev, state, idx * map->dma_entry_size,
1028 				map->dma_entry_size, DMA_BIDIRECTIONAL, attrs);
1029 	else if (dma_need_unmap(dev))
1030 		dma_unmap_phys(dev, dma_addrs[idx], map->dma_entry_size,
1031 			       DMA_BIDIRECTIONAL, attrs);
1032 
1033 	pfns[idx] &=
1034 		~(HMM_PFN_DMA_MAPPED | HMM_PFN_P2PDMA | HMM_PFN_P2PDMA_BUS);
1035 	return true;
1036 }
1037 EXPORT_SYMBOL_GPL(hmm_dma_unmap_pfn);
1038