Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2013 Red Hat Inc. |
| 4 | * |
Jérôme Glisse | f813f21 | 2018-10-30 15:04:06 -0700 | [diff] [blame] | 5 | * Authors: Jérôme Glisse <jglisse@redhat.com> |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 6 | */ |
| 7 | /* |
| 8 | * Refer to include/linux/hmm.h for information about heterogeneous memory |
| 9 | * management or HMM for short. |
| 10 | */ |
Christoph Hellwig | a520110 | 2019-08-28 16:19:53 +0200 | [diff] [blame] | 11 | #include <linux/pagewalk.h> |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 12 | #include <linux/hmm.h> |
Jérôme Glisse | 858b54d | 2017-09-08 16:12:02 -0700 | [diff] [blame] | 13 | #include <linux/init.h> |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 14 | #include <linux/rmap.h> |
| 15 | #include <linux/swap.h> |
Jérôme Glisse | 133ff0e | 2017-09-08 16:11:23 -0700 | [diff] [blame] | 16 | #include <linux/slab.h> |
| 17 | #include <linux/sched.h> |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 18 | #include <linux/mmzone.h> |
| 19 | #include <linux/pagemap.h> |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 20 | #include <linux/swapops.h> |
| 21 | #include <linux/hugetlb.h> |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 22 | #include <linux/memremap.h> |
Jason Gunthorpe | c8a53b2 | 2019-05-23 10:36:46 -0300 | [diff] [blame] | 23 | #include <linux/sched/mm.h> |
Jérôme Glisse | 7b2d55d2 | 2017-09-08 16:11:46 -0700 | [diff] [blame] | 24 | #include <linux/jump_label.h> |
Jérôme Glisse | 55c0ece | 2019-05-13 17:20:28 -0700 | [diff] [blame] | 25 | #include <linux/dma-mapping.h> |
Jérôme Glisse | c0b1240 | 2017-09-08 16:11:27 -0700 | [diff] [blame] | 26 | #include <linux/mmu_notifier.h> |
Jérôme Glisse | 4ef589d | 2017-09-08 16:11:58 -0700 | [diff] [blame] | 27 | #include <linux/memory_hotplug.h> |
| 28 | |
Alistair Popple | b756a3b | 2021-06-30 18:54:25 -0700 | [diff] [blame] | 29 | #include "internal.h" |
| 30 | |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 31 | struct hmm_vma_walk { |
| 32 | struct hmm_range *range; |
| 33 | unsigned long last; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 34 | }; |
| 35 | |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 36 | enum { |
| 37 | HMM_NEED_FAULT = 1 << 0, |
| 38 | HMM_NEED_WRITE_FAULT = 1 << 1, |
| 39 | HMM_NEED_ALL_BITS = HMM_NEED_FAULT | HMM_NEED_WRITE_FAULT, |
| 40 | }; |
| 41 | |
Ralph Campbell | d28c2c9a | 2019-11-04 14:21:40 -0800 | [diff] [blame] | 42 | static int hmm_pfns_fill(unsigned long addr, unsigned long end, |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 43 | struct hmm_range *range, unsigned long cpu_flags) |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 44 | { |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 45 | unsigned long i = (addr - range->start) >> PAGE_SHIFT; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 46 | |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 47 | for (; addr < end; addr += PAGE_SIZE, i++) |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 48 | range->hmm_pfns[i] = cpu_flags; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 49 | return 0; |
| 50 | } |
| 51 | |
Jérôme Glisse | 5504ed2 | 2018-04-10 16:28:46 -0700 | [diff] [blame] | 52 | /* |
Christoph Hellwig | f8c888a | 2020-03-16 14:53:09 +0100 | [diff] [blame] | 53 | * hmm_vma_fault() - fault in a range lacking valid pmd or pte(s) |
Ralph Campbell | d2e8d55 | 2019-07-25 17:56:45 -0700 | [diff] [blame] | 54 | * @addr: range virtual start address (inclusive) |
Jérôme Glisse | 5504ed2 | 2018-04-10 16:28:46 -0700 | [diff] [blame] | 55 | * @end: range virtual end address (exclusive) |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 56 | * @required_fault: HMM_NEED_* flags |
Jérôme Glisse | 5504ed2 | 2018-04-10 16:28:46 -0700 | [diff] [blame] | 57 | * @walk: mm_walk structure |
Christoph Hellwig | f8c888a | 2020-03-16 14:53:09 +0100 | [diff] [blame] | 58 | * Return: -EBUSY after page fault, or page fault error |
Jérôme Glisse | 5504ed2 | 2018-04-10 16:28:46 -0700 | [diff] [blame] | 59 | * |
| 60 | * This function will be called whenever pmd_none() or pte_none() returns true, |
| 61 | * or whenever there is no page directory covering the virtual address range. |
| 62 | */ |
Christoph Hellwig | f8c888a | 2020-03-16 14:53:09 +0100 | [diff] [blame] | 63 | static int hmm_vma_fault(unsigned long addr, unsigned long end, |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 64 | unsigned int required_fault, struct mm_walk *walk) |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 65 | { |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 66 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
Christoph Hellwig | 5a0c38d | 2020-03-16 14:53:10 +0100 | [diff] [blame] | 67 | struct vm_area_struct *vma = walk->vma; |
Christoph Hellwig | 5a0c38d | 2020-03-16 14:53:10 +0100 | [diff] [blame] | 68 | unsigned int fault_flags = FAULT_FLAG_REMOTE; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 69 | |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 70 | WARN_ON_ONCE(!required_fault); |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 71 | hmm_vma_walk->last = addr; |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 72 | |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 73 | if (required_fault & HMM_NEED_WRITE_FAULT) { |
Christoph Hellwig | 5a0c38d | 2020-03-16 14:53:10 +0100 | [diff] [blame] | 74 | if (!(vma->vm_flags & VM_WRITE)) |
| 75 | return -EPERM; |
| 76 | fault_flags |= FAULT_FLAG_WRITE; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Jason Gunthorpe | 53bfe17f | 2020-03-27 17:00:20 -0300 | [diff] [blame] | 79 | for (; addr < end; addr += PAGE_SIZE) |
Peter Xu | bce617e | 2020-08-11 18:37:44 -0700 | [diff] [blame] | 80 | if (handle_mm_fault(vma, addr, fault_flags, NULL) & |
| 81 | VM_FAULT_ERROR) |
Jason Gunthorpe | 53bfe17f | 2020-03-27 17:00:20 -0300 | [diff] [blame] | 82 | return -EFAULT; |
Christoph Hellwig | f8c888a | 2020-03-16 14:53:09 +0100 | [diff] [blame] | 83 | return -EBUSY; |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 86 | static unsigned int hmm_pte_need_fault(const struct hmm_vma_walk *hmm_vma_walk, |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 87 | unsigned long pfn_req_flags, |
| 88 | unsigned long cpu_flags) |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 89 | { |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 90 | struct hmm_range *range = hmm_vma_walk->range; |
| 91 | |
Jérôme Glisse | 023a019 | 2019-05-13 17:20:05 -0700 | [diff] [blame] | 92 | /* |
| 93 | * So we not only consider the individual per page request we also |
| 94 | * consider the default flags requested for the range. The API can |
Ralph Campbell | d2e8d55 | 2019-07-25 17:56:45 -0700 | [diff] [blame] | 95 | * be used 2 ways. The first one where the HMM user coalesces |
| 96 | * multiple page faults into one request and sets flags per pfn for |
| 97 | * those faults. The second one where the HMM user wants to pre- |
Jérôme Glisse | 023a019 | 2019-05-13 17:20:05 -0700 | [diff] [blame] | 98 | * fault a range with specific flags. For the latter one it is a |
| 99 | * waste to have the user pre-fill the pfn arrays with a default |
| 100 | * flags value. |
| 101 | */ |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 102 | pfn_req_flags &= range->pfn_flags_mask; |
| 103 | pfn_req_flags |= range->default_flags; |
Jérôme Glisse | 023a019 | 2019-05-13 17:20:05 -0700 | [diff] [blame] | 104 | |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 105 | /* We aren't ask to do anything ... */ |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 106 | if (!(pfn_req_flags & HMM_PFN_REQ_FAULT)) |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 107 | return 0; |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 108 | |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 109 | /* Need to write fault ? */ |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 110 | if ((pfn_req_flags & HMM_PFN_REQ_WRITE) && |
| 111 | !(cpu_flags & HMM_PFN_WRITE)) |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 112 | return HMM_NEED_FAULT | HMM_NEED_WRITE_FAULT; |
| 113 | |
| 114 | /* If CPU page table is not valid then we need to fault */ |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 115 | if (!(cpu_flags & HMM_PFN_VALID)) |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 116 | return HMM_NEED_FAULT; |
| 117 | return 0; |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 120 | static unsigned int |
| 121 | hmm_range_need_fault(const struct hmm_vma_walk *hmm_vma_walk, |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 122 | const unsigned long hmm_pfns[], unsigned long npages, |
| 123 | unsigned long cpu_flags) |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 124 | { |
Jason Gunthorpe | 6bfef2f | 2020-03-27 17:00:16 -0300 | [diff] [blame] | 125 | struct hmm_range *range = hmm_vma_walk->range; |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 126 | unsigned int required_fault = 0; |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 127 | unsigned long i; |
| 128 | |
Jason Gunthorpe | 6bfef2f | 2020-03-27 17:00:16 -0300 | [diff] [blame] | 129 | /* |
| 130 | * If the default flags do not request to fault pages, and the mask does |
| 131 | * not allow for individual pages to be faulted, then |
| 132 | * hmm_pte_need_fault() will always return 0. |
| 133 | */ |
| 134 | if (!((range->default_flags | range->pfn_flags_mask) & |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 135 | HMM_PFN_REQ_FAULT)) |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 136 | return 0; |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 137 | |
| 138 | for (i = 0; i < npages; ++i) { |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 139 | required_fault |= hmm_pte_need_fault(hmm_vma_walk, hmm_pfns[i], |
| 140 | cpu_flags); |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 141 | if (required_fault == HMM_NEED_ALL_BITS) |
| 142 | return required_fault; |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 143 | } |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 144 | return required_fault; |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static int hmm_vma_walk_hole(unsigned long addr, unsigned long end, |
Steven Price | b7a16c7 | 2020-02-03 17:36:03 -0800 | [diff] [blame] | 148 | __always_unused int depth, struct mm_walk *walk) |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 149 | { |
| 150 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
| 151 | struct hmm_range *range = hmm_vma_walk->range; |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 152 | unsigned int required_fault; |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 153 | unsigned long i, npages; |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 154 | unsigned long *hmm_pfns; |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 155 | |
| 156 | i = (addr - range->start) >> PAGE_SHIFT; |
| 157 | npages = (end - addr) >> PAGE_SHIFT; |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 158 | hmm_pfns = &range->hmm_pfns[i]; |
| 159 | required_fault = |
| 160 | hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0); |
Jason Gunthorpe | bd5d358 | 2020-03-27 17:00:21 -0300 | [diff] [blame] | 161 | if (!walk->vma) { |
| 162 | if (required_fault) |
| 163 | return -EFAULT; |
| 164 | return hmm_pfns_fill(addr, end, range, HMM_PFN_ERROR); |
| 165 | } |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 166 | if (required_fault) |
| 167 | return hmm_vma_fault(addr, end, required_fault, walk); |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 168 | return hmm_pfns_fill(addr, end, range, 0); |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Ralph Campbell | 3b50a6e | 2020-07-01 15:53:49 -0700 | [diff] [blame] | 171 | static inline unsigned long hmm_pfn_flags_order(unsigned long order) |
| 172 | { |
| 173 | return order << HMM_PFN_ORDER_SHIFT; |
| 174 | } |
| 175 | |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 176 | static inline unsigned long pmd_to_hmm_pfn_flags(struct hmm_range *range, |
| 177 | pmd_t pmd) |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 178 | { |
| 179 | if (pmd_protnone(pmd)) |
| 180 | return 0; |
Ralph Campbell | 3b50a6e | 2020-07-01 15:53:49 -0700 | [diff] [blame] | 181 | return (pmd_write(pmd) ? (HMM_PFN_VALID | HMM_PFN_WRITE) : |
| 182 | HMM_PFN_VALID) | |
| 183 | hmm_pfn_flags_order(PMD_SHIFT - PAGE_SHIFT); |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 186 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
Christoph Hellwig | 9d3973d | 2019-08-06 19:05:49 +0300 | [diff] [blame] | 187 | static int hmm_vma_handle_pmd(struct mm_walk *walk, unsigned long addr, |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 188 | unsigned long end, unsigned long hmm_pfns[], |
| 189 | pmd_t pmd) |
Christoph Hellwig | 9d3973d | 2019-08-06 19:05:49 +0300 | [diff] [blame] | 190 | { |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 191 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 192 | struct hmm_range *range = hmm_vma_walk->range; |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 193 | unsigned long pfn, npages, i; |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 194 | unsigned int required_fault; |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 195 | unsigned long cpu_flags; |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 196 | |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 197 | npages = (end - addr) >> PAGE_SHIFT; |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 198 | cpu_flags = pmd_to_hmm_pfn_flags(range, pmd); |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 199 | required_fault = |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 200 | hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, cpu_flags); |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 201 | if (required_fault) |
| 202 | return hmm_vma_fault(addr, end, required_fault, walk); |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 203 | |
Christoph Hellwig | 309f9a4 | 2019-08-06 19:05:47 +0300 | [diff] [blame] | 204 | pfn = pmd_pfn(pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT); |
Jason Gunthorpe | 068354a | 2020-03-27 17:00:13 -0300 | [diff] [blame] | 205 | for (i = 0; addr < end; addr += PAGE_SIZE, i++, pfn++) |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 206 | hmm_pfns[i] = pfn | cpu_flags; |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 207 | return 0; |
| 208 | } |
Christoph Hellwig | 9d3973d | 2019-08-06 19:05:49 +0300 | [diff] [blame] | 209 | #else /* CONFIG_TRANSPARENT_HUGEPAGE */ |
| 210 | /* stub to allow the code below to compile */ |
| 211 | int hmm_vma_handle_pmd(struct mm_walk *walk, unsigned long addr, |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 212 | unsigned long end, unsigned long hmm_pfns[], pmd_t pmd); |
Christoph Hellwig | 9d3973d | 2019-08-06 19:05:49 +0300 | [diff] [blame] | 213 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 214 | |
Christoph Hellwig | 08ddddd | 2020-03-16 20:32:16 +0100 | [diff] [blame] | 215 | static inline bool hmm_is_device_private_entry(struct hmm_range *range, |
| 216 | swp_entry_t entry) |
| 217 | { |
| 218 | return is_device_private_entry(entry) && |
Alistair Popple | af5cdaf | 2021-06-30 18:54:06 -0700 | [diff] [blame] | 219 | pfn_swap_entry_to_page(entry)->pgmap->owner == |
Christoph Hellwig | 08ddddd | 2020-03-16 20:32:16 +0100 | [diff] [blame] | 220 | range->dev_private_owner; |
| 221 | } |
| 222 | |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 223 | static inline unsigned long pte_to_hmm_pfn_flags(struct hmm_range *range, |
| 224 | pte_t pte) |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 225 | { |
Philip Yang | 789c2af | 2019-05-23 16:32:31 -0400 | [diff] [blame] | 226 | if (pte_none(pte) || !pte_present(pte) || pte_protnone(pte)) |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 227 | return 0; |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 228 | return pte_write(pte) ? (HMM_PFN_VALID | HMM_PFN_WRITE) : HMM_PFN_VALID; |
Jérôme Glisse | 2aee09d | 2018-04-10 16:29:02 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 231 | static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr, |
| 232 | unsigned long end, pmd_t *pmdp, pte_t *ptep, |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 233 | unsigned long *hmm_pfn) |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 234 | { |
| 235 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
Jérôme Glisse | f88a1e9 | 2018-04-10 16:29:06 -0700 | [diff] [blame] | 236 | struct hmm_range *range = hmm_vma_walk->range; |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 237 | unsigned int required_fault; |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 238 | unsigned long cpu_flags; |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 239 | pte_t pte = *ptep; |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 240 | uint64_t pfn_req_flags = *hmm_pfn; |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 241 | |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 242 | if (pte_none(pte)) { |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 243 | required_fault = |
| 244 | hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, 0); |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 245 | if (required_fault) |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 246 | goto fault; |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 247 | *hmm_pfn = 0; |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | if (!pte_present(pte)) { |
| 252 | swp_entry_t entry = pte_to_swp_entry(pte); |
| 253 | |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 254 | /* |
Randy Dunlap | 0cb80a2 | 2020-08-11 18:32:56 -0700 | [diff] [blame] | 255 | * Never fault in device private pages, but just report |
Christoph Hellwig | 17ffdc4 | 2020-03-16 20:32:15 +0100 | [diff] [blame] | 256 | * the PFN even if not present. |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 257 | */ |
Christoph Hellwig | 08ddddd | 2020-03-16 20:32:16 +0100 | [diff] [blame] | 258 | if (hmm_is_device_private_entry(range, entry)) { |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 259 | cpu_flags = HMM_PFN_VALID; |
Alistair Popple | 4dd845b | 2021-06-30 18:54:09 -0700 | [diff] [blame] | 260 | if (is_writable_device_private_entry(entry)) |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 261 | cpu_flags |= HMM_PFN_WRITE; |
Alistair Popple | af5cdaf | 2021-06-30 18:54:06 -0700 | [diff] [blame] | 262 | *hmm_pfn = swp_offset(entry) | cpu_flags; |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 263 | return 0; |
| 264 | } |
| 265 | |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 266 | required_fault = |
| 267 | hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, 0); |
Jason Gunthorpe | 846babe | 2020-03-27 17:00:19 -0300 | [diff] [blame] | 268 | if (!required_fault) { |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 269 | *hmm_pfn = 0; |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 270 | return 0; |
Jason Gunthorpe | 846babe | 2020-03-27 17:00:19 -0300 | [diff] [blame] | 271 | } |
Jason Gunthorpe | 76612d6 | 2020-02-28 15:52:32 -0400 | [diff] [blame] | 272 | |
| 273 | if (!non_swap_entry(entry)) |
| 274 | goto fault; |
| 275 | |
Alistair Popple | b756a3b | 2021-06-30 18:54:25 -0700 | [diff] [blame] | 276 | if (is_device_exclusive_entry(entry)) |
| 277 | goto fault; |
| 278 | |
Jason Gunthorpe | 76612d6 | 2020-02-28 15:52:32 -0400 | [diff] [blame] | 279 | if (is_migration_entry(entry)) { |
| 280 | pte_unmap(ptep); |
| 281 | hmm_vma_walk->last = addr; |
| 282 | migration_entry_wait(walk->mm, pmdp, addr); |
| 283 | return -EBUSY; |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | /* Report error for everything else */ |
Jason Gunthorpe | dfdc220 | 2020-02-28 15:30:37 -0400 | [diff] [blame] | 287 | pte_unmap(ptep); |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 288 | return -EFAULT; |
| 289 | } |
| 290 | |
Jason Gunthorpe | 76612d6 | 2020-02-28 15:52:32 -0400 | [diff] [blame] | 291 | cpu_flags = pte_to_hmm_pfn_flags(range, pte); |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 292 | required_fault = |
| 293 | hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, cpu_flags); |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 294 | if (required_fault) |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 295 | goto fault; |
| 296 | |
Jason Gunthorpe | 4055062 | 2020-03-05 14:27:20 -0400 | [diff] [blame] | 297 | /* |
Li Zhijian | 4b42fb2 | 2021-09-08 18:10:02 -0700 | [diff] [blame] | 298 | * Bypass devmap pte such as DAX page when all pfn requested |
| 299 | * flags(pfn_req_flags) are fulfilled. |
Jason Gunthorpe | 4055062 | 2020-03-05 14:27:20 -0400 | [diff] [blame] | 300 | * Since each architecture defines a struct page for the zero page, just |
| 301 | * fall through and treat it like a normal page. |
| 302 | */ |
Alistair Popple | 87c01d5 | 2022-01-14 14:09:31 -0800 | [diff] [blame] | 303 | if (!vm_normal_page(walk->vma, addr, pte) && |
| 304 | !pte_devmap(pte) && |
Li Zhijian | 4b42fb2 | 2021-09-08 18:10:02 -0700 | [diff] [blame] | 305 | !is_zero_pfn(pte_pfn(pte))) { |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 306 | if (hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, 0)) { |
Jason Gunthorpe | dfdc220 | 2020-02-28 15:30:37 -0400 | [diff] [blame] | 307 | pte_unmap(ptep); |
Ralph Campbell | ac541f2 | 2019-10-23 12:55:14 -0700 | [diff] [blame] | 308 | return -EFAULT; |
| 309 | } |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 310 | *hmm_pfn = HMM_PFN_ERROR; |
Jason Gunthorpe | 4055062 | 2020-03-05 14:27:20 -0400 | [diff] [blame] | 311 | return 0; |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 314 | *hmm_pfn = pte_pfn(pte) | cpu_flags; |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 315 | return 0; |
| 316 | |
| 317 | fault: |
| 318 | pte_unmap(ptep); |
| 319 | /* Fault any virtual address we were asked to fault */ |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 320 | return hmm_vma_fault(addr, end, required_fault, walk); |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 323 | static int hmm_vma_walk_pmd(pmd_t *pmdp, |
| 324 | unsigned long start, |
| 325 | unsigned long end, |
| 326 | struct mm_walk *walk) |
| 327 | { |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 328 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
| 329 | struct hmm_range *range = hmm_vma_walk->range; |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 330 | unsigned long *hmm_pfns = |
| 331 | &range->hmm_pfns[(start - range->start) >> PAGE_SHIFT]; |
Jason Gunthorpe | 2288a9a | 2020-03-05 15:26:33 -0400 | [diff] [blame] | 332 | unsigned long npages = (end - start) >> PAGE_SHIFT; |
| 333 | unsigned long addr = start; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 334 | pte_t *ptep; |
Jérôme Glisse | d08faca | 2018-10-30 15:04:20 -0700 | [diff] [blame] | 335 | pmd_t pmd; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 336 | |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 337 | again: |
Jérôme Glisse | d08faca | 2018-10-30 15:04:20 -0700 | [diff] [blame] | 338 | pmd = READ_ONCE(*pmdp); |
| 339 | if (pmd_none(pmd)) |
Steven Price | b7a16c7 | 2020-02-03 17:36:03 -0800 | [diff] [blame] | 340 | return hmm_vma_walk_hole(start, end, -1, walk); |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 341 | |
Jérôme Glisse | d08faca | 2018-10-30 15:04:20 -0700 | [diff] [blame] | 342 | if (thp_migration_supported() && is_pmd_migration_entry(pmd)) { |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 343 | if (hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0)) { |
Jérôme Glisse | d08faca | 2018-10-30 15:04:20 -0700 | [diff] [blame] | 344 | hmm_vma_walk->last = addr; |
Ralph Campbell | d2e8d55 | 2019-07-25 17:56:45 -0700 | [diff] [blame] | 345 | pmd_migration_entry_wait(walk->mm, pmdp); |
Jérôme Glisse | 7323161 | 2019-05-13 17:19:58 -0700 | [diff] [blame] | 346 | return -EBUSY; |
Jérôme Glisse | d08faca | 2018-10-30 15:04:20 -0700 | [diff] [blame] | 347 | } |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 348 | return hmm_pfns_fill(start, end, range, 0); |
Jason Gunthorpe | 2288a9a | 2020-03-05 15:26:33 -0400 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | if (!pmd_present(pmd)) { |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 352 | if (hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0)) |
Jason Gunthorpe | 2288a9a | 2020-03-05 15:26:33 -0400 | [diff] [blame] | 353 | return -EFAULT; |
Ralph Campbell | d28c2c9a | 2019-11-04 14:21:40 -0800 | [diff] [blame] | 354 | return hmm_pfns_fill(start, end, range, HMM_PFN_ERROR); |
Jason Gunthorpe | 2288a9a | 2020-03-05 15:26:33 -0400 | [diff] [blame] | 355 | } |
Jérôme Glisse | d08faca | 2018-10-30 15:04:20 -0700 | [diff] [blame] | 356 | |
| 357 | if (pmd_devmap(pmd) || pmd_trans_huge(pmd)) { |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 358 | /* |
Ralph Campbell | d2e8d55 | 2019-07-25 17:56:45 -0700 | [diff] [blame] | 359 | * No need to take pmd_lock here, even if some other thread |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 360 | * is splitting the huge pmd we will get that event through |
| 361 | * mmu_notifier callback. |
| 362 | * |
Ralph Campbell | d2e8d55 | 2019-07-25 17:56:45 -0700 | [diff] [blame] | 363 | * So just read pmd value and check again it's a transparent |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 364 | * huge or device mapping one and compute corresponding pfn |
| 365 | * values. |
| 366 | */ |
| 367 | pmd = pmd_read_atomic(pmdp); |
| 368 | barrier(); |
| 369 | if (!pmd_devmap(pmd) && !pmd_trans_huge(pmd)) |
| 370 | goto again; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 371 | |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 372 | return hmm_vma_handle_pmd(walk, addr, end, hmm_pfns, pmd); |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Jérôme Glisse | d08faca | 2018-10-30 15:04:20 -0700 | [diff] [blame] | 375 | /* |
Ralph Campbell | d2e8d55 | 2019-07-25 17:56:45 -0700 | [diff] [blame] | 376 | * We have handled all the valid cases above ie either none, migration, |
Jérôme Glisse | d08faca | 2018-10-30 15:04:20 -0700 | [diff] [blame] | 377 | * huge or transparent huge. At this point either it is a valid pmd |
| 378 | * entry pointing to pte directory or it is a bad pmd that will not |
| 379 | * recover. |
| 380 | */ |
Jason Gunthorpe | 2288a9a | 2020-03-05 15:26:33 -0400 | [diff] [blame] | 381 | if (pmd_bad(pmd)) { |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 382 | if (hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0)) |
Jason Gunthorpe | 2288a9a | 2020-03-05 15:26:33 -0400 | [diff] [blame] | 383 | return -EFAULT; |
Ralph Campbell | d28c2c9a | 2019-11-04 14:21:40 -0800 | [diff] [blame] | 384 | return hmm_pfns_fill(start, end, range, HMM_PFN_ERROR); |
Jason Gunthorpe | 2288a9a | 2020-03-05 15:26:33 -0400 | [diff] [blame] | 385 | } |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 386 | |
| 387 | ptep = pte_offset_map(pmdp, addr); |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 388 | for (; addr < end; addr += PAGE_SIZE, ptep++, hmm_pfns++) { |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 389 | int r; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 390 | |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 391 | r = hmm_vma_handle_pte(walk, addr, end, pmdp, ptep, hmm_pfns); |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 392 | if (r) { |
Jason Gunthorpe | dfdc220 | 2020-02-28 15:30:37 -0400 | [diff] [blame] | 393 | /* hmm_vma_handle_pte() did pte_unmap() */ |
Jérôme Glisse | 53f5c3f | 2018-04-10 16:28:59 -0700 | [diff] [blame] | 394 | return r; |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 395 | } |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 396 | } |
| 397 | pte_unmap(ptep - 1); |
Jérôme Glisse | da4c3c7 | 2017-09-08 16:11:31 -0700 | [diff] [blame] | 398 | return 0; |
| 399 | } |
| 400 | |
Christoph Hellwig | f0b3c45 | 2019-08-06 19:05:48 +0300 | [diff] [blame] | 401 | #if defined(CONFIG_ARCH_HAS_PTE_DEVMAP) && \ |
| 402 | defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 403 | static inline unsigned long pud_to_hmm_pfn_flags(struct hmm_range *range, |
| 404 | pud_t pud) |
Christoph Hellwig | f0b3c45 | 2019-08-06 19:05:48 +0300 | [diff] [blame] | 405 | { |
| 406 | if (!pud_present(pud)) |
| 407 | return 0; |
Ralph Campbell | 3b50a6e | 2020-07-01 15:53:49 -0700 | [diff] [blame] | 408 | return (pud_write(pud) ? (HMM_PFN_VALID | HMM_PFN_WRITE) : |
| 409 | HMM_PFN_VALID) | |
| 410 | hmm_pfn_flags_order(PUD_SHIFT - PAGE_SHIFT); |
Christoph Hellwig | f0b3c45 | 2019-08-06 19:05:48 +0300 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | static int hmm_vma_walk_pud(pud_t *pudp, unsigned long start, unsigned long end, |
| 414 | struct mm_walk *walk) |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 415 | { |
| 416 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
| 417 | struct hmm_range *range = hmm_vma_walk->range; |
Steven Price | 3afc423 | 2020-02-03 17:35:45 -0800 | [diff] [blame] | 418 | unsigned long addr = start; |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 419 | pud_t pud; |
Steven Price | 3afc423 | 2020-02-03 17:35:45 -0800 | [diff] [blame] | 420 | int ret = 0; |
| 421 | spinlock_t *ptl = pud_trans_huge_lock(pudp, walk->vma); |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 422 | |
Steven Price | 3afc423 | 2020-02-03 17:35:45 -0800 | [diff] [blame] | 423 | if (!ptl) |
| 424 | return 0; |
| 425 | |
| 426 | /* Normally we don't want to split the huge page */ |
| 427 | walk->action = ACTION_CONTINUE; |
| 428 | |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 429 | pud = READ_ONCE(*pudp); |
Steven Price | 3afc423 | 2020-02-03 17:35:45 -0800 | [diff] [blame] | 430 | if (pud_none(pud)) { |
Jason Gunthorpe | 05fc1df | 2020-03-02 15:26:44 -0400 | [diff] [blame] | 431 | spin_unlock(ptl); |
| 432 | return hmm_vma_walk_hole(start, end, -1, walk); |
Steven Price | 3afc423 | 2020-02-03 17:35:45 -0800 | [diff] [blame] | 433 | } |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 434 | |
| 435 | if (pud_huge(pud) && pud_devmap(pud)) { |
| 436 | unsigned long i, npages, pfn; |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 437 | unsigned int required_fault; |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 438 | unsigned long *hmm_pfns; |
| 439 | unsigned long cpu_flags; |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 440 | |
Steven Price | 3afc423 | 2020-02-03 17:35:45 -0800 | [diff] [blame] | 441 | if (!pud_present(pud)) { |
Jason Gunthorpe | 05fc1df | 2020-03-02 15:26:44 -0400 | [diff] [blame] | 442 | spin_unlock(ptl); |
| 443 | return hmm_vma_walk_hole(start, end, -1, walk); |
Steven Price | 3afc423 | 2020-02-03 17:35:45 -0800 | [diff] [blame] | 444 | } |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 445 | |
| 446 | i = (addr - range->start) >> PAGE_SHIFT; |
| 447 | npages = (end - addr) >> PAGE_SHIFT; |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 448 | hmm_pfns = &range->hmm_pfns[i]; |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 449 | |
| 450 | cpu_flags = pud_to_hmm_pfn_flags(range, pud); |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 451 | required_fault = hmm_range_need_fault(hmm_vma_walk, hmm_pfns, |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 452 | npages, cpu_flags); |
| 453 | if (required_fault) { |
Jason Gunthorpe | 05fc1df | 2020-03-02 15:26:44 -0400 | [diff] [blame] | 454 | spin_unlock(ptl); |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 455 | return hmm_vma_fault(addr, end, required_fault, walk); |
Steven Price | 3afc423 | 2020-02-03 17:35:45 -0800 | [diff] [blame] | 456 | } |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 457 | |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 458 | pfn = pud_pfn(pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT); |
Jason Gunthorpe | 068354a | 2020-03-27 17:00:13 -0300 | [diff] [blame] | 459 | for (i = 0; i < npages; ++i, ++pfn) |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 460 | hmm_pfns[i] = pfn | cpu_flags; |
Steven Price | 3afc423 | 2020-02-03 17:35:45 -0800 | [diff] [blame] | 461 | goto out_unlock; |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Steven Price | 3afc423 | 2020-02-03 17:35:45 -0800 | [diff] [blame] | 464 | /* Ask for the PUD to be split */ |
| 465 | walk->action = ACTION_SUBTREE; |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 466 | |
Steven Price | 3afc423 | 2020-02-03 17:35:45 -0800 | [diff] [blame] | 467 | out_unlock: |
| 468 | spin_unlock(ptl); |
| 469 | return ret; |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 470 | } |
Christoph Hellwig | f0b3c45 | 2019-08-06 19:05:48 +0300 | [diff] [blame] | 471 | #else |
| 472 | #define hmm_vma_walk_pud NULL |
| 473 | #endif |
Jérôme Glisse | 992de9a | 2019-05-13 17:20:21 -0700 | [diff] [blame] | 474 | |
Christoph Hellwig | 251bbe5 | 2019-08-06 19:05:50 +0300 | [diff] [blame] | 475 | #ifdef CONFIG_HUGETLB_PAGE |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 476 | static int hmm_vma_walk_hugetlb_entry(pte_t *pte, unsigned long hmask, |
| 477 | unsigned long start, unsigned long end, |
| 478 | struct mm_walk *walk) |
| 479 | { |
Christoph Hellwig | 05c23af | 2019-08-06 19:05:46 +0300 | [diff] [blame] | 480 | unsigned long addr = start, i, pfn; |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 481 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
| 482 | struct hmm_range *range = hmm_vma_walk->range; |
| 483 | struct vm_area_struct *vma = walk->vma; |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 484 | unsigned int required_fault; |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 485 | unsigned long pfn_req_flags; |
| 486 | unsigned long cpu_flags; |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 487 | spinlock_t *ptl; |
| 488 | pte_t entry; |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 489 | |
Ralph Campbell | d2e8d55 | 2019-07-25 17:56:45 -0700 | [diff] [blame] | 490 | ptl = huge_pte_lock(hstate_vma(vma), walk->mm, pte); |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 491 | entry = huge_ptep_get(pte); |
| 492 | |
Christoph Hellwig | 7f08263 | 2019-08-06 19:05:45 +0300 | [diff] [blame] | 493 | i = (start - range->start) >> PAGE_SHIFT; |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 494 | pfn_req_flags = range->hmm_pfns[i]; |
Ralph Campbell | 3b50a6e | 2020-07-01 15:53:49 -0700 | [diff] [blame] | 495 | cpu_flags = pte_to_hmm_pfn_flags(range, entry) | |
| 496 | hmm_pfn_flags_order(huge_page_order(hstate_vma(vma))); |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 497 | required_fault = |
| 498 | hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, cpu_flags); |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 499 | if (required_fault) { |
Christoph Hellwig | 4505069 | 2020-03-16 14:53:08 +0100 | [diff] [blame] | 500 | spin_unlock(ptl); |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 501 | return hmm_vma_fault(addr, end, required_fault, walk); |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Christoph Hellwig | 05c23af | 2019-08-06 19:05:46 +0300 | [diff] [blame] | 504 | pfn = pte_pfn(entry) + ((start & ~hmask) >> PAGE_SHIFT); |
Christoph Hellwig | 7f08263 | 2019-08-06 19:05:45 +0300 | [diff] [blame] | 505 | for (; addr < end; addr += PAGE_SIZE, i++, pfn++) |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 506 | range->hmm_pfns[i] = pfn | cpu_flags; |
| 507 | |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 508 | spin_unlock(ptl); |
Christoph Hellwig | 4505069 | 2020-03-16 14:53:08 +0100 | [diff] [blame] | 509 | return 0; |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 510 | } |
Christoph Hellwig | 251bbe5 | 2019-08-06 19:05:50 +0300 | [diff] [blame] | 511 | #else |
| 512 | #define hmm_vma_walk_hugetlb_entry NULL |
| 513 | #endif /* CONFIG_HUGETLB_PAGE */ |
Jérôme Glisse | 63d5066 | 2019-05-13 17:20:18 -0700 | [diff] [blame] | 514 | |
Ralph Campbell | d28c2c9a | 2019-11-04 14:21:40 -0800 | [diff] [blame] | 515 | static int hmm_vma_walk_test(unsigned long start, unsigned long end, |
| 516 | struct mm_walk *walk) |
Jérôme Glisse | 33cd47d | 2018-04-10 16:28:54 -0700 | [diff] [blame] | 517 | { |
Ralph Campbell | d28c2c9a | 2019-11-04 14:21:40 -0800 | [diff] [blame] | 518 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
| 519 | struct hmm_range *range = hmm_vma_walk->range; |
| 520 | struct vm_area_struct *vma = walk->vma; |
| 521 | |
Alistair Popple | 87c01d5 | 2022-01-14 14:09:31 -0800 | [diff] [blame] | 522 | if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)) && |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 523 | vma->vm_flags & VM_READ) |
| 524 | return 0; |
| 525 | |
Ralph Campbell | d28c2c9a | 2019-11-04 14:21:40 -0800 | [diff] [blame] | 526 | /* |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 527 | * vma ranges that don't have struct page backing them or map I/O |
| 528 | * devices directly cannot be handled by hmm_range_fault(). |
Jason Gunthorpe | c2579c9 | 2020-03-05 12:00:22 -0400 | [diff] [blame] | 529 | * |
Ralph Campbell | d28c2c9a | 2019-11-04 14:21:40 -0800 | [diff] [blame] | 530 | * If the vma does not allow read access, then assume that it does not |
Jason Gunthorpe | c2579c9 | 2020-03-05 12:00:22 -0400 | [diff] [blame] | 531 | * allow write access either. HMM does not support architectures that |
| 532 | * allow write without read. |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 533 | * |
| 534 | * If a fault is requested for an unsupported range then it is a hard |
| 535 | * failure. |
Ralph Campbell | d28c2c9a | 2019-11-04 14:21:40 -0800 | [diff] [blame] | 536 | */ |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 537 | if (hmm_range_need_fault(hmm_vma_walk, |
Jason Gunthorpe | 2733ea1 | 2020-05-01 15:20:48 -0300 | [diff] [blame] | 538 | range->hmm_pfns + |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 539 | ((start - range->start) >> PAGE_SHIFT), |
| 540 | (end - start) >> PAGE_SHIFT, 0)) |
| 541 | return -EFAULT; |
Ralph Campbell | d28c2c9a | 2019-11-04 14:21:40 -0800 | [diff] [blame] | 542 | |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 543 | hmm_pfns_fill(start, end, range, HMM_PFN_ERROR); |
Ralph Campbell | d28c2c9a | 2019-11-04 14:21:40 -0800 | [diff] [blame] | 544 | |
Jason Gunthorpe | a3eb13c | 2020-03-27 17:00:14 -0300 | [diff] [blame] | 545 | /* Skip this vma and continue processing the next vma. */ |
| 546 | return 1; |
Jérôme Glisse | 33cd47d | 2018-04-10 16:28:54 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 549 | static const struct mm_walk_ops hmm_walk_ops = { |
| 550 | .pud_entry = hmm_vma_walk_pud, |
| 551 | .pmd_entry = hmm_vma_walk_pmd, |
| 552 | .pte_hole = hmm_vma_walk_hole, |
| 553 | .hugetlb_entry = hmm_vma_walk_hugetlb_entry, |
Ralph Campbell | d28c2c9a | 2019-11-04 14:21:40 -0800 | [diff] [blame] | 554 | .test_walk = hmm_vma_walk_test, |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 555 | }; |
| 556 | |
Christoph Hellwig | 9a4903e | 2019-07-25 17:56:46 -0700 | [diff] [blame] | 557 | /** |
| 558 | * hmm_range_fault - try to fault some address in a virtual address range |
Jason Gunthorpe | f970b97 | 2020-03-27 17:00:15 -0300 | [diff] [blame] | 559 | * @range: argument structure |
Jérôme Glisse | 7323161 | 2019-05-13 17:19:58 -0700 | [diff] [blame] | 560 | * |
Jason Gunthorpe | be957c8 | 2020-05-01 15:20:45 -0300 | [diff] [blame] | 561 | * Returns 0 on success or one of the following error codes: |
Christoph Hellwig | 9a4903e | 2019-07-25 17:56:46 -0700 | [diff] [blame] | 562 | * |
| 563 | * -EINVAL: Invalid arguments or mm or virtual address is in an invalid vma |
| 564 | * (e.g., device file vma). |
| 565 | * -ENOMEM: Out of memory. |
| 566 | * -EPERM: Invalid permission (e.g., asking for write and range is read |
| 567 | * only). |
Christoph Hellwig | 9a4903e | 2019-07-25 17:56:46 -0700 | [diff] [blame] | 568 | * -EBUSY: The range has been invalidated and the caller needs to wait for |
| 569 | * the invalidation to finish. |
Jason Gunthorpe | f970b97 | 2020-03-27 17:00:15 -0300 | [diff] [blame] | 570 | * -EFAULT: A page was requested to be valid and could not be made valid |
| 571 | * ie it has no backing VMA or it is illegal to access |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 572 | * |
Jason Gunthorpe | f970b97 | 2020-03-27 17:00:15 -0300 | [diff] [blame] | 573 | * This is similar to get_user_pages(), except that it can read the page tables |
| 574 | * without mutating them (ie causing faults). |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 575 | */ |
Jason Gunthorpe | be957c8 | 2020-05-01 15:20:45 -0300 | [diff] [blame] | 576 | int hmm_range_fault(struct hmm_range *range) |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 577 | { |
Ralph Campbell | d28c2c9a | 2019-11-04 14:21:40 -0800 | [diff] [blame] | 578 | struct hmm_vma_walk hmm_vma_walk = { |
| 579 | .range = range, |
| 580 | .last = range->start, |
Ralph Campbell | d28c2c9a | 2019-11-04 14:21:40 -0800 | [diff] [blame] | 581 | }; |
Jason Gunthorpe | a22dd50 | 2019-11-12 16:22:30 -0400 | [diff] [blame] | 582 | struct mm_struct *mm = range->notifier->mm; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 583 | int ret; |
| 584 | |
Michel Lespinasse | 42fc541 | 2020-06-08 21:33:44 -0700 | [diff] [blame] | 585 | mmap_assert_locked(mm); |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 586 | |
| 587 | do { |
Jérôme Glisse | a3e0d41 | 2019-05-13 17:20:01 -0700 | [diff] [blame] | 588 | /* If range is no longer valid force retry. */ |
Jason Gunthorpe | a22dd50 | 2019-11-12 16:22:30 -0400 | [diff] [blame] | 589 | if (mmu_interval_check_retry(range->notifier, |
| 590 | range->notifier_seq)) |
Christoph Hellwig | 2bcbeae | 2019-07-24 08:52:52 +0200 | [diff] [blame] | 591 | return -EBUSY; |
Ralph Campbell | d28c2c9a | 2019-11-04 14:21:40 -0800 | [diff] [blame] | 592 | ret = walk_page_range(mm, hmm_vma_walk.last, range->end, |
| 593 | &hmm_walk_ops, &hmm_vma_walk); |
Jason Gunthorpe | be957c8 | 2020-05-01 15:20:45 -0300 | [diff] [blame] | 594 | /* |
| 595 | * When -EBUSY is returned the loop restarts with |
| 596 | * hmm_vma_walk.last set to an address that has not been stored |
| 597 | * in pfns. All entries < last in the pfn array are set to their |
| 598 | * output, and all >= are still at their input values. |
| 599 | */ |
Ralph Campbell | d28c2c9a | 2019-11-04 14:21:40 -0800 | [diff] [blame] | 600 | } while (ret == -EBUSY); |
Jason Gunthorpe | be957c8 | 2020-05-01 15:20:45 -0300 | [diff] [blame] | 601 | return ret; |
Jérôme Glisse | 74eee18 | 2017-09-08 16:11:35 -0700 | [diff] [blame] | 602 | } |
Jérôme Glisse | 7323161 | 2019-05-13 17:19:58 -0700 | [diff] [blame] | 603 | EXPORT_SYMBOL(hmm_range_fault); |