blob: f06bcac948a79bf80871935f36e96388f823e46f [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Jérôme Glisse133ff0e2017-09-08 16:11:23 -07002/*
3 * Copyright 2013 Red Hat Inc.
4 *
Jérôme Glissef813f212018-10-30 15:04:06 -07005 * Authors: Jérôme Glisse <jglisse@redhat.com>
Jérôme Glisse133ff0e2017-09-08 16:11:23 -07006 */
7/*
8 * Refer to include/linux/hmm.h for information about heterogeneous memory
9 * management or HMM for short.
10 */
Christoph Hellwiga5201102019-08-28 16:19:53 +020011#include <linux/pagewalk.h>
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070012#include <linux/hmm.h>
Jérôme Glisse858b54d2017-09-08 16:12:02 -070013#include <linux/init.h>
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070014#include <linux/rmap.h>
15#include <linux/swap.h>
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070016#include <linux/slab.h>
17#include <linux/sched.h>
Jérôme Glisse4ef589d2017-09-08 16:11:58 -070018#include <linux/mmzone.h>
19#include <linux/pagemap.h>
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070020#include <linux/swapops.h>
21#include <linux/hugetlb.h>
Jérôme Glisse4ef589d2017-09-08 16:11:58 -070022#include <linux/memremap.h>
Jason Gunthorpec8a53b22019-05-23 10:36:46 -030023#include <linux/sched/mm.h>
Jérôme Glisse7b2d55d22017-09-08 16:11:46 -070024#include <linux/jump_label.h>
Jérôme Glisse55c0ece2019-05-13 17:20:28 -070025#include <linux/dma-mapping.h>
Jérôme Glissec0b12402017-09-08 16:11:27 -070026#include <linux/mmu_notifier.h>
Jérôme Glisse4ef589d2017-09-08 16:11:58 -070027#include <linux/memory_hotplug.h>
28
Jérôme Glisse74eee182017-09-08 16:11:35 -070029struct hmm_vma_walk {
30 struct hmm_range *range;
31 unsigned long last;
Jérôme Glisse74eee182017-09-08 16:11:35 -070032};
33
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -030034enum {
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 Gunthorpef970b972020-03-27 17:00:15 -030040/*
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 */
46static 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 Campbelld28c2c9a2019-11-04 14:21:40 -080052static int hmm_pfns_fill(unsigned long addr, unsigned long end,
53 struct hmm_range *range, enum hmm_pfn_value_e value)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070054{
Jérôme Glisseff05c0c2018-04-10 16:28:38 -070055 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070056 unsigned long i;
57
58 i = (addr - range->start) >> PAGE_SHIFT;
59 for (; addr < end; addr += PAGE_SIZE, i++)
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -080060 pfns[i] = range->values[value];
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070061
62 return 0;
63}
64
Jérôme Glisse5504ed22018-04-10 16:28:46 -070065/*
Christoph Hellwigf8c888a2020-03-16 14:53:09 +010066 * hmm_vma_fault() - fault in a range lacking valid pmd or pte(s)
Ralph Campbelld2e8d552019-07-25 17:56:45 -070067 * @addr: range virtual start address (inclusive)
Jérôme Glisse5504ed22018-04-10 16:28:46 -070068 * @end: range virtual end address (exclusive)
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -030069 * @required_fault: HMM_NEED_* flags
Jérôme Glisse5504ed22018-04-10 16:28:46 -070070 * @walk: mm_walk structure
Christoph Hellwigf8c888a2020-03-16 14:53:09 +010071 * Return: -EBUSY after page fault, or page fault error
Jérôme Glisse5504ed22018-04-10 16:28:46 -070072 *
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 Hellwigf8c888a2020-03-16 14:53:09 +010076static int hmm_vma_fault(unsigned long addr, unsigned long end,
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -030077 unsigned int required_fault, struct mm_walk *walk)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070078{
Jérôme Glisse74eee182017-09-08 16:11:35 -070079 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Christoph Hellwig5a0c38d2020-03-16 14:53:10 +010080 struct vm_area_struct *vma = walk->vma;
Christoph Hellwig5a0c38d2020-03-16 14:53:10 +010081 unsigned int fault_flags = FAULT_FLAG_REMOTE;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070082
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -030083 WARN_ON_ONCE(!required_fault);
Jérôme Glisse74eee182017-09-08 16:11:35 -070084 hmm_vma_walk->last = addr;
Jérôme Glisse63d50662019-05-13 17:20:18 -070085
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -030086 if (required_fault & HMM_NEED_WRITE_FAULT) {
Christoph Hellwig5a0c38d2020-03-16 14:53:10 +010087 if (!(vma->vm_flags & VM_WRITE))
88 return -EPERM;
89 fault_flags |= FAULT_FLAG_WRITE;
Jérôme Glisse74eee182017-09-08 16:11:35 -070090 }
91
Jason Gunthorpe53bfe17f2020-03-27 17:00:20 -030092 for (; addr < end; addr += PAGE_SIZE)
Christoph Hellwig5a0c38d2020-03-16 14:53:10 +010093 if (handle_mm_fault(vma, addr, fault_flags) & VM_FAULT_ERROR)
Jason Gunthorpe53bfe17f2020-03-27 17:00:20 -030094 return -EFAULT;
Christoph Hellwigf8c888a2020-03-16 14:53:09 +010095 return -EBUSY;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -070096}
97
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -030098static unsigned int hmm_pte_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
99 uint64_t pfns, uint64_t cpu_flags)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700100{
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700101 struct hmm_range *range = hmm_vma_walk->range;
102
Jérôme Glisse023a0192019-05-13 17:20:05 -0700103 /*
104 * So we not only consider the individual per page request we also
105 * consider the default flags requested for the range. The API can
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700106 * be used 2 ways. The first one where the HMM user coalesces
107 * multiple page faults into one request and sets flags per pfn for
108 * those faults. The second one where the HMM user wants to pre-
Jérôme Glisse023a0192019-05-13 17:20:05 -0700109 * fault a range with specific flags. For the latter one it is a
110 * waste to have the user pre-fill the pfn arrays with a default
111 * flags value.
112 */
113 pfns = (pfns & range->pfn_flags_mask) | range->default_flags;
114
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700115 /* We aren't ask to do anything ... */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700116 if (!(pfns & range->flags[HMM_PFN_VALID]))
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300117 return 0;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700118
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700119 /* Need to write fault ? */
120 if ((pfns & range->flags[HMM_PFN_WRITE]) &&
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300121 !(cpu_flags & range->flags[HMM_PFN_WRITE]))
122 return HMM_NEED_FAULT | HMM_NEED_WRITE_FAULT;
123
124 /* If CPU page table is not valid then we need to fault */
125 if (!(cpu_flags & range->flags[HMM_PFN_VALID]))
126 return HMM_NEED_FAULT;
127 return 0;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700128}
129
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300130static unsigned int
131hmm_range_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
132 const uint64_t *pfns, unsigned long npages,
133 uint64_t cpu_flags)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700134{
Jason Gunthorpe6bfef2f2020-03-27 17:00:16 -0300135 struct hmm_range *range = hmm_vma_walk->range;
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300136 unsigned int required_fault = 0;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700137 unsigned long i;
138
Jason Gunthorpe6bfef2f2020-03-27 17:00:16 -0300139 /*
140 * If the default flags do not request to fault pages, and the mask does
141 * not allow for individual pages to be faulted, then
142 * hmm_pte_need_fault() will always return 0.
143 */
144 if (!((range->default_flags | range->pfn_flags_mask) &
145 range->flags[HMM_PFN_VALID]))
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300146 return 0;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700147
148 for (i = 0; i < npages; ++i) {
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300149 required_fault |=
150 hmm_pte_need_fault(hmm_vma_walk, pfns[i], cpu_flags);
151 if (required_fault == HMM_NEED_ALL_BITS)
152 return required_fault;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700153 }
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300154 return required_fault;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700155}
156
157static int hmm_vma_walk_hole(unsigned long addr, unsigned long end,
Steven Priceb7a16c72020-02-03 17:36:03 -0800158 __always_unused int depth, struct mm_walk *walk)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700159{
160 struct hmm_vma_walk *hmm_vma_walk = walk->private;
161 struct hmm_range *range = hmm_vma_walk->range;
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300162 unsigned int required_fault;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700163 unsigned long i, npages;
164 uint64_t *pfns;
165
166 i = (addr - range->start) >> PAGE_SHIFT;
167 npages = (end - addr) >> PAGE_SHIFT;
168 pfns = &range->pfns[i];
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300169 required_fault = hmm_range_need_fault(hmm_vma_walk, pfns, npages, 0);
Jason Gunthorpebd5d3582020-03-27 17:00:21 -0300170 if (!walk->vma) {
171 if (required_fault)
172 return -EFAULT;
173 return hmm_pfns_fill(addr, end, range, HMM_PFN_ERROR);
174 }
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300175 if (required_fault)
176 return hmm_vma_fault(addr, end, required_fault, walk);
Christoph Hellwigf8c888a2020-03-16 14:53:09 +0100177 return hmm_pfns_fill(addr, end, range, HMM_PFN_NONE);
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700178}
179
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700180static inline uint64_t pmd_to_hmm_pfn_flags(struct hmm_range *range, pmd_t pmd)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700181{
182 if (pmd_protnone(pmd))
183 return 0;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700184 return pmd_write(pmd) ? range->flags[HMM_PFN_VALID] |
185 range->flags[HMM_PFN_WRITE] :
186 range->flags[HMM_PFN_VALID];
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700187}
188
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700189#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Christoph Hellwig9d3973d2019-08-06 19:05:49 +0300190static int hmm_vma_handle_pmd(struct mm_walk *walk, unsigned long addr,
191 unsigned long end, uint64_t *pfns, pmd_t pmd)
192{
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700193 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700194 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700195 unsigned long pfn, npages, i;
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300196 unsigned int required_fault;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700197 uint64_t cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700198
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700199 npages = (end - addr) >> PAGE_SHIFT;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700200 cpu_flags = pmd_to_hmm_pfn_flags(range, pmd);
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300201 required_fault =
202 hmm_range_need_fault(hmm_vma_walk, pfns, npages, cpu_flags);
203 if (required_fault)
204 return hmm_vma_fault(addr, end, required_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700205
Christoph Hellwig309f9a42019-08-06 19:05:47 +0300206 pfn = pmd_pfn(pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
Jason Gunthorpe068354a2020-03-27 17:00:13 -0300207 for (i = 0; addr < end; addr += PAGE_SIZE, i++, pfn++)
Jérôme Glisse391aab12019-05-13 17:20:31 -0700208 pfns[i] = hmm_device_entry_from_pfn(range, pfn) | cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700209 return 0;
210}
Christoph Hellwig9d3973d2019-08-06 19:05:49 +0300211#else /* CONFIG_TRANSPARENT_HUGEPAGE */
212/* stub to allow the code below to compile */
213int hmm_vma_handle_pmd(struct mm_walk *walk, unsigned long addr,
214 unsigned long end, uint64_t *pfns, pmd_t pmd);
215#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700216
Christoph Hellwig08ddddd2020-03-16 20:32:16 +0100217static inline bool hmm_is_device_private_entry(struct hmm_range *range,
218 swp_entry_t entry)
219{
220 return is_device_private_entry(entry) &&
221 device_private_entry_to_page(entry)->pgmap->owner ==
222 range->dev_private_owner;
223}
224
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700225static inline uint64_t pte_to_hmm_pfn_flags(struct hmm_range *range, pte_t pte)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700226{
Philip Yang789c2af2019-05-23 16:32:31 -0400227 if (pte_none(pte) || !pte_present(pte) || pte_protnone(pte))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700228 return 0;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700229 return pte_write(pte) ? range->flags[HMM_PFN_VALID] |
230 range->flags[HMM_PFN_WRITE] :
231 range->flags[HMM_PFN_VALID];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700232}
233
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700234static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,
235 unsigned long end, pmd_t *pmdp, pte_t *ptep,
236 uint64_t *pfn)
237{
238 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700239 struct hmm_range *range = hmm_vma_walk->range;
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300240 unsigned int required_fault;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700241 uint64_t cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700242 pte_t pte = *ptep;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700243 uint64_t orig_pfn = *pfn;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700244
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700245 if (pte_none(pte)) {
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300246 required_fault = hmm_pte_need_fault(hmm_vma_walk, orig_pfn, 0);
247 if (required_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700248 goto fault;
Jason Gunthorpe846babe2020-03-27 17:00:19 -0300249 *pfn = range->values[HMM_PFN_NONE];
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700250 return 0;
251 }
252
253 if (!pte_present(pte)) {
254 swp_entry_t entry = pte_to_swp_entry(pte);
255
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700256 /*
Christoph Hellwig17ffdc42020-03-16 20:32:15 +0100257 * Never fault in device private pages pages, but just report
258 * the PFN even if not present.
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700259 */
Christoph Hellwig08ddddd2020-03-16 20:32:16 +0100260 if (hmm_is_device_private_entry(range, entry)) {
Jérôme Glisse391aab12019-05-13 17:20:31 -0700261 *pfn = hmm_device_entry_from_pfn(range,
Jason Gunthorpef66c9a32020-03-27 17:00:18 -0300262 device_private_entry_to_pfn(entry));
Christoph Hellwig17ffdc42020-03-16 20:32:15 +0100263 *pfn |= range->flags[HMM_PFN_VALID];
264 if (is_write_device_private_entry(entry))
265 *pfn |= range->flags[HMM_PFN_WRITE];
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700266 return 0;
267 }
268
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300269 required_fault = hmm_pte_need_fault(hmm_vma_walk, orig_pfn, 0);
Jason Gunthorpe846babe2020-03-27 17:00:19 -0300270 if (!required_fault) {
271 *pfn = range->values[HMM_PFN_NONE];
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700272 return 0;
Jason Gunthorpe846babe2020-03-27 17:00:19 -0300273 }
Jason Gunthorpe76612d62020-02-28 15:52:32 -0400274
275 if (!non_swap_entry(entry))
276 goto fault;
277
278 if (is_migration_entry(entry)) {
279 pte_unmap(ptep);
280 hmm_vma_walk->last = addr;
281 migration_entry_wait(walk->mm, pmdp, addr);
282 return -EBUSY;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700283 }
284
285 /* Report error for everything else */
Jason Gunthorpedfdc2202020-02-28 15:30:37 -0400286 pte_unmap(ptep);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700287 return -EFAULT;
288 }
289
Jason Gunthorpe76612d62020-02-28 15:52:32 -0400290 cpu_flags = pte_to_hmm_pfn_flags(range, pte);
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300291 required_fault = hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags);
292 if (required_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700293 goto fault;
294
Jason Gunthorpe40550622020-03-05 14:27:20 -0400295 /*
296 * Since each architecture defines a struct page for the zero page, just
297 * fall through and treat it like a normal page.
298 */
299 if (pte_special(pte) && !is_zero_pfn(pte_pfn(pte))) {
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300300 if (hmm_pte_need_fault(hmm_vma_walk, orig_pfn, 0)) {
Jason Gunthorpedfdc2202020-02-28 15:30:37 -0400301 pte_unmap(ptep);
Ralph Campbellac541f22019-10-23 12:55:14 -0700302 return -EFAULT;
303 }
Jason Gunthorpe40550622020-03-05 14:27:20 -0400304 *pfn = range->values[HMM_PFN_SPECIAL];
305 return 0;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700306 }
307
Jérôme Glisse391aab12019-05-13 17:20:31 -0700308 *pfn = hmm_device_entry_from_pfn(range, pte_pfn(pte)) | cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700309 return 0;
310
311fault:
312 pte_unmap(ptep);
313 /* Fault any virtual address we were asked to fault */
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300314 return hmm_vma_fault(addr, end, required_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700315}
316
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700317static int hmm_vma_walk_pmd(pmd_t *pmdp,
318 unsigned long start,
319 unsigned long end,
320 struct mm_walk *walk)
321{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700322 struct hmm_vma_walk *hmm_vma_walk = walk->private;
323 struct hmm_range *range = hmm_vma_walk->range;
Jason Gunthorpe2288a9a2020-03-05 15:26:33 -0400324 uint64_t *pfns = &range->pfns[(start - range->start) >> PAGE_SHIFT];
325 unsigned long npages = (end - start) >> PAGE_SHIFT;
326 unsigned long addr = start;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700327 pte_t *ptep;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700328 pmd_t pmd;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700329
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700330again:
Jérôme Glissed08faca2018-10-30 15:04:20 -0700331 pmd = READ_ONCE(*pmdp);
332 if (pmd_none(pmd))
Steven Priceb7a16c72020-02-03 17:36:03 -0800333 return hmm_vma_walk_hole(start, end, -1, walk);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700334
Jérôme Glissed08faca2018-10-30 15:04:20 -0700335 if (thp_migration_supported() && is_pmd_migration_entry(pmd)) {
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300336 if (hmm_range_need_fault(hmm_vma_walk, pfns, npages, 0)) {
Jérôme Glissed08faca2018-10-30 15:04:20 -0700337 hmm_vma_walk->last = addr;
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700338 pmd_migration_entry_wait(walk->mm, pmdp);
Jérôme Glisse73231612019-05-13 17:19:58 -0700339 return -EBUSY;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700340 }
Jason Gunthorpe7d082982020-03-04 16:25:56 -0400341 return hmm_pfns_fill(start, end, range, HMM_PFN_NONE);
Jason Gunthorpe2288a9a2020-03-05 15:26:33 -0400342 }
343
344 if (!pmd_present(pmd)) {
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300345 if (hmm_range_need_fault(hmm_vma_walk, pfns, npages, 0))
Jason Gunthorpe2288a9a2020-03-05 15:26:33 -0400346 return -EFAULT;
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800347 return hmm_pfns_fill(start, end, range, HMM_PFN_ERROR);
Jason Gunthorpe2288a9a2020-03-05 15:26:33 -0400348 }
Jérôme Glissed08faca2018-10-30 15:04:20 -0700349
350 if (pmd_devmap(pmd) || pmd_trans_huge(pmd)) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700351 /*
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700352 * No need to take pmd_lock here, even if some other thread
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700353 * is splitting the huge pmd we will get that event through
354 * mmu_notifier callback.
355 *
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700356 * So just read pmd value and check again it's a transparent
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700357 * huge or device mapping one and compute corresponding pfn
358 * values.
359 */
360 pmd = pmd_read_atomic(pmdp);
361 barrier();
362 if (!pmd_devmap(pmd) && !pmd_trans_huge(pmd))
363 goto again;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700364
Jason Gunthorpe2288a9a2020-03-05 15:26:33 -0400365 return hmm_vma_handle_pmd(walk, addr, end, pfns, pmd);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700366 }
367
Jérôme Glissed08faca2018-10-30 15:04:20 -0700368 /*
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700369 * We have handled all the valid cases above ie either none, migration,
Jérôme Glissed08faca2018-10-30 15:04:20 -0700370 * huge or transparent huge. At this point either it is a valid pmd
371 * entry pointing to pte directory or it is a bad pmd that will not
372 * recover.
373 */
Jason Gunthorpe2288a9a2020-03-05 15:26:33 -0400374 if (pmd_bad(pmd)) {
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300375 if (hmm_range_need_fault(hmm_vma_walk, pfns, npages, 0))
Jason Gunthorpe2288a9a2020-03-05 15:26:33 -0400376 return -EFAULT;
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800377 return hmm_pfns_fill(start, end, range, HMM_PFN_ERROR);
Jason Gunthorpe2288a9a2020-03-05 15:26:33 -0400378 }
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700379
380 ptep = pte_offset_map(pmdp, addr);
Jason Gunthorpe2288a9a2020-03-05 15:26:33 -0400381 for (; addr < end; addr += PAGE_SIZE, ptep++, pfns++) {
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700382 int r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700383
Jason Gunthorpe2288a9a2020-03-05 15:26:33 -0400384 r = hmm_vma_handle_pte(walk, addr, end, pmdp, ptep, pfns);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700385 if (r) {
Jason Gunthorpedfdc2202020-02-28 15:30:37 -0400386 /* hmm_vma_handle_pte() did pte_unmap() */
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700387 return r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700388 }
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700389 }
390 pte_unmap(ptep - 1);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700391 return 0;
392}
393
Christoph Hellwigf0b3c452019-08-06 19:05:48 +0300394#if defined(CONFIG_ARCH_HAS_PTE_DEVMAP) && \
395 defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
396static inline uint64_t pud_to_hmm_pfn_flags(struct hmm_range *range, pud_t pud)
397{
398 if (!pud_present(pud))
399 return 0;
400 return pud_write(pud) ? range->flags[HMM_PFN_VALID] |
401 range->flags[HMM_PFN_WRITE] :
402 range->flags[HMM_PFN_VALID];
403}
404
405static int hmm_vma_walk_pud(pud_t *pudp, unsigned long start, unsigned long end,
406 struct mm_walk *walk)
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700407{
408 struct hmm_vma_walk *hmm_vma_walk = walk->private;
409 struct hmm_range *range = hmm_vma_walk->range;
Steven Price3afc4232020-02-03 17:35:45 -0800410 unsigned long addr = start;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700411 pud_t pud;
Steven Price3afc4232020-02-03 17:35:45 -0800412 int ret = 0;
413 spinlock_t *ptl = pud_trans_huge_lock(pudp, walk->vma);
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700414
Steven Price3afc4232020-02-03 17:35:45 -0800415 if (!ptl)
416 return 0;
417
418 /* Normally we don't want to split the huge page */
419 walk->action = ACTION_CONTINUE;
420
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700421 pud = READ_ONCE(*pudp);
Steven Price3afc4232020-02-03 17:35:45 -0800422 if (pud_none(pud)) {
Jason Gunthorpe05fc1df2020-03-02 15:26:44 -0400423 spin_unlock(ptl);
424 return hmm_vma_walk_hole(start, end, -1, walk);
Steven Price3afc4232020-02-03 17:35:45 -0800425 }
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700426
427 if (pud_huge(pud) && pud_devmap(pud)) {
428 unsigned long i, npages, pfn;
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300429 unsigned int required_fault;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700430 uint64_t *pfns, cpu_flags;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700431
Steven Price3afc4232020-02-03 17:35:45 -0800432 if (!pud_present(pud)) {
Jason Gunthorpe05fc1df2020-03-02 15:26:44 -0400433 spin_unlock(ptl);
434 return hmm_vma_walk_hole(start, end, -1, walk);
Steven Price3afc4232020-02-03 17:35:45 -0800435 }
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700436
437 i = (addr - range->start) >> PAGE_SHIFT;
438 npages = (end - addr) >> PAGE_SHIFT;
439 pfns = &range->pfns[i];
440
441 cpu_flags = pud_to_hmm_pfn_flags(range, pud);
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300442 required_fault = hmm_range_need_fault(hmm_vma_walk, pfns,
443 npages, cpu_flags);
444 if (required_fault) {
Jason Gunthorpe05fc1df2020-03-02 15:26:44 -0400445 spin_unlock(ptl);
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300446 return hmm_vma_fault(addr, end, required_fault, walk);
Steven Price3afc4232020-02-03 17:35:45 -0800447 }
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700448
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700449 pfn = pud_pfn(pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
Jason Gunthorpe068354a2020-03-27 17:00:13 -0300450 for (i = 0; i < npages; ++i, ++pfn)
Jérôme Glisse391aab12019-05-13 17:20:31 -0700451 pfns[i] = hmm_device_entry_from_pfn(range, pfn) |
452 cpu_flags;
Steven Price3afc4232020-02-03 17:35:45 -0800453 goto out_unlock;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700454 }
455
Steven Price3afc4232020-02-03 17:35:45 -0800456 /* Ask for the PUD to be split */
457 walk->action = ACTION_SUBTREE;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700458
Steven Price3afc4232020-02-03 17:35:45 -0800459out_unlock:
460 spin_unlock(ptl);
461 return ret;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700462}
Christoph Hellwigf0b3c452019-08-06 19:05:48 +0300463#else
464#define hmm_vma_walk_pud NULL
465#endif
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700466
Christoph Hellwig251bbe52019-08-06 19:05:50 +0300467#ifdef CONFIG_HUGETLB_PAGE
Jérôme Glisse63d50662019-05-13 17:20:18 -0700468static int hmm_vma_walk_hugetlb_entry(pte_t *pte, unsigned long hmask,
469 unsigned long start, unsigned long end,
470 struct mm_walk *walk)
471{
Christoph Hellwig05c23af2019-08-06 19:05:46 +0300472 unsigned long addr = start, i, pfn;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700473 struct hmm_vma_walk *hmm_vma_walk = walk->private;
474 struct hmm_range *range = hmm_vma_walk->range;
475 struct vm_area_struct *vma = walk->vma;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700476 uint64_t orig_pfn, cpu_flags;
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300477 unsigned int required_fault;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700478 spinlock_t *ptl;
479 pte_t entry;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700480
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700481 ptl = huge_pte_lock(hstate_vma(vma), walk->mm, pte);
Jérôme Glisse63d50662019-05-13 17:20:18 -0700482 entry = huge_ptep_get(pte);
483
Christoph Hellwig7f082632019-08-06 19:05:45 +0300484 i = (start - range->start) >> PAGE_SHIFT;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700485 orig_pfn = range->pfns[i];
Jérôme Glisse63d50662019-05-13 17:20:18 -0700486 cpu_flags = pte_to_hmm_pfn_flags(range, entry);
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300487 required_fault = hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags);
488 if (required_fault) {
Christoph Hellwig45050692020-03-16 14:53:08 +0100489 spin_unlock(ptl);
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300490 return hmm_vma_fault(addr, end, required_fault, walk);
Jérôme Glisse63d50662019-05-13 17:20:18 -0700491 }
492
Christoph Hellwig05c23af2019-08-06 19:05:46 +0300493 pfn = pte_pfn(entry) + ((start & ~hmask) >> PAGE_SHIFT);
Christoph Hellwig7f082632019-08-06 19:05:45 +0300494 for (; addr < end; addr += PAGE_SIZE, i++, pfn++)
Jérôme Glisse391aab12019-05-13 17:20:31 -0700495 range->pfns[i] = hmm_device_entry_from_pfn(range, pfn) |
496 cpu_flags;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700497 spin_unlock(ptl);
Christoph Hellwig45050692020-03-16 14:53:08 +0100498 return 0;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700499}
Christoph Hellwig251bbe52019-08-06 19:05:50 +0300500#else
501#define hmm_vma_walk_hugetlb_entry NULL
502#endif /* CONFIG_HUGETLB_PAGE */
Jérôme Glisse63d50662019-05-13 17:20:18 -0700503
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800504static int hmm_vma_walk_test(unsigned long start, unsigned long end,
505 struct mm_walk *walk)
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700506{
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800507 struct hmm_vma_walk *hmm_vma_walk = walk->private;
508 struct hmm_range *range = hmm_vma_walk->range;
509 struct vm_area_struct *vma = walk->vma;
510
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300511 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP | VM_MIXEDMAP)) &&
512 vma->vm_flags & VM_READ)
513 return 0;
514
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800515 /*
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300516 * vma ranges that don't have struct page backing them or map I/O
517 * devices directly cannot be handled by hmm_range_fault().
Jason Gunthorpec2579c92020-03-05 12:00:22 -0400518 *
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800519 * If the vma does not allow read access, then assume that it does not
Jason Gunthorpec2579c92020-03-05 12:00:22 -0400520 * allow write access either. HMM does not support architectures that
521 * allow write without read.
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300522 *
523 * If a fault is requested for an unsupported range then it is a hard
524 * failure.
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800525 */
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300526 if (hmm_range_need_fault(hmm_vma_walk,
527 range->pfns +
528 ((start - range->start) >> PAGE_SHIFT),
529 (end - start) >> PAGE_SHIFT, 0))
530 return -EFAULT;
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800531
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300532 hmm_pfns_fill(start, end, range, HMM_PFN_ERROR);
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800533
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300534 /* Skip this vma and continue processing the next vma. */
535 return 1;
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700536}
537
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200538static const struct mm_walk_ops hmm_walk_ops = {
539 .pud_entry = hmm_vma_walk_pud,
540 .pmd_entry = hmm_vma_walk_pmd,
541 .pte_hole = hmm_vma_walk_hole,
542 .hugetlb_entry = hmm_vma_walk_hugetlb_entry,
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800543 .test_walk = hmm_vma_walk_test,
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200544};
545
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700546/**
547 * hmm_range_fault - try to fault some address in a virtual address range
Jason Gunthorpef970b972020-03-27 17:00:15 -0300548 * @range: argument structure
Jérôme Glisse73231612019-05-13 17:19:58 -0700549 *
Jason Gunthorpebe957c82020-05-01 15:20:45 -0300550 * Returns 0 on success or one of the following error codes:
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700551 *
552 * -EINVAL: Invalid arguments or mm or virtual address is in an invalid vma
553 * (e.g., device file vma).
554 * -ENOMEM: Out of memory.
555 * -EPERM: Invalid permission (e.g., asking for write and range is read
556 * only).
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700557 * -EBUSY: The range has been invalidated and the caller needs to wait for
558 * the invalidation to finish.
Jason Gunthorpef970b972020-03-27 17:00:15 -0300559 * -EFAULT: A page was requested to be valid and could not be made valid
560 * ie it has no backing VMA or it is illegal to access
Jérôme Glisse74eee182017-09-08 16:11:35 -0700561 *
Jason Gunthorpef970b972020-03-27 17:00:15 -0300562 * This is similar to get_user_pages(), except that it can read the page tables
563 * without mutating them (ie causing faults).
Jérôme Glisse74eee182017-09-08 16:11:35 -0700564 */
Jason Gunthorpebe957c82020-05-01 15:20:45 -0300565int hmm_range_fault(struct hmm_range *range)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700566{
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800567 struct hmm_vma_walk hmm_vma_walk = {
568 .range = range,
569 .last = range->start,
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800570 };
Jason Gunthorpea22dd502019-11-12 16:22:30 -0400571 struct mm_struct *mm = range->notifier->mm;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700572 int ret;
573
Jason Gunthorpe04ec32f2019-11-12 16:22:20 -0400574 lockdep_assert_held(&mm->mmap_sem);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700575
576 do {
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700577 /* If range is no longer valid force retry. */
Jason Gunthorpea22dd502019-11-12 16:22:30 -0400578 if (mmu_interval_check_retry(range->notifier,
579 range->notifier_seq))
Christoph Hellwig2bcbeae2019-07-24 08:52:52 +0200580 return -EBUSY;
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800581 ret = walk_page_range(mm, hmm_vma_walk.last, range->end,
582 &hmm_walk_ops, &hmm_vma_walk);
Jason Gunthorpebe957c82020-05-01 15:20:45 -0300583 /*
584 * When -EBUSY is returned the loop restarts with
585 * hmm_vma_walk.last set to an address that has not been stored
586 * in pfns. All entries < last in the pfn array are set to their
587 * output, and all >= are still at their input values.
588 */
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800589 } while (ret == -EBUSY);
Jason Gunthorpebe957c82020-05-01 15:20:45 -0300590 return ret;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700591}
Jérôme Glisse73231612019-05-13 17:19:58 -0700592EXPORT_SYMBOL(hmm_range_fault);