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