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