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