blob: b98f492984e5ab2be5bb42e742eccd2a49a01fb5 [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;
Jérôme Glisse992de9a2019-05-13 17:20:21 -070031 struct dev_pagemap *pgmap;
Jérôme Glisse74eee182017-09-08 16:11:35 -070032 unsigned long last;
Christoph Hellwig9a4903e2019-07-25 17:56:46 -070033 unsigned int flags;
Jérôme Glisse74eee182017-09-08 16:11:35 -070034};
35
Jérôme Glisse2aee09d2018-04-10 16:29:02 -070036static int hmm_vma_do_fault(struct mm_walk *walk, unsigned long addr,
37 bool write_fault, uint64_t *pfn)
Jérôme Glisse74eee182017-09-08 16:11:35 -070038{
Kuehling, Felix9b1ae602019-05-10 19:53:24 +000039 unsigned int flags = FAULT_FLAG_REMOTE;
Jérôme Glisse74eee182017-09-08 16:11:35 -070040 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -070041 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse74eee182017-09-08 16:11:35 -070042 struct vm_area_struct *vma = walk->vma;
Souptick Joarder50a7ca32018-08-17 15:44:47 -070043 vm_fault_t ret;
Jérôme Glisse74eee182017-09-08 16:11:35 -070044
Ralph Campbell6c64f2b2019-08-23 15:17:52 -070045 if (!vma)
46 goto err;
47
Christoph Hellwig9a4903e2019-07-25 17:56:46 -070048 if (hmm_vma_walk->flags & HMM_FAULT_ALLOW_RETRY)
49 flags |= FAULT_FLAG_ALLOW_RETRY;
50 if (write_fault)
51 flags |= FAULT_FLAG_WRITE;
52
Souptick Joarder50a7ca32018-08-17 15:44:47 -070053 ret = handle_mm_fault(vma, addr, flags);
Jason Gunthorpee709acc2019-07-24 08:52:58 +020054 if (ret & VM_FAULT_RETRY) {
55 /* Note, handle_mm_fault did up_read(&mm->mmap_sem)) */
Jérôme Glisse73231612019-05-13 17:19:58 -070056 return -EAGAIN;
Jason Gunthorpee709acc2019-07-24 08:52:58 +020057 }
Ralph Campbell6c64f2b2019-08-23 15:17:52 -070058 if (ret & VM_FAULT_ERROR)
59 goto err;
Jérôme Glisse74eee182017-09-08 16:11:35 -070060
Jérôme Glisse73231612019-05-13 17:19:58 -070061 return -EBUSY;
Ralph Campbell6c64f2b2019-08-23 15:17:52 -070062
63err:
64 *pfn = range->values[HMM_PFN_ERROR];
65 return -EFAULT;
Jérôme Glisse74eee182017-09-08 16:11:35 -070066}
67
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -080068static int hmm_pfns_fill(unsigned long addr, unsigned long end,
69 struct hmm_range *range, enum hmm_pfn_value_e value)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070070{
Jérôme Glisseff05c0c2018-04-10 16:28:38 -070071 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070072 unsigned long i;
73
74 i = (addr - range->start) >> PAGE_SHIFT;
75 for (; addr < end; addr += PAGE_SIZE, i++)
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -080076 pfns[i] = range->values[value];
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070077
78 return 0;
79}
80
Jérôme Glisse5504ed22018-04-10 16:28:46 -070081/*
Ralph Campbelld2e8d552019-07-25 17:56:45 -070082 * hmm_vma_walk_hole_() - handle a range lacking valid pmd or pte(s)
83 * @addr: range virtual start address (inclusive)
Jérôme Glisse5504ed22018-04-10 16:28:46 -070084 * @end: range virtual end address (exclusive)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -070085 * @fault: should we fault or not ?
86 * @write_fault: write fault ?
Jérôme Glisse5504ed22018-04-10 16:28:46 -070087 * @walk: mm_walk structure
Ralph Campbell085ea252019-05-06 16:29:39 -070088 * Return: 0 on success, -EBUSY after page fault, or page fault error
Jérôme Glisse5504ed22018-04-10 16:28:46 -070089 *
90 * This function will be called whenever pmd_none() or pte_none() returns true,
91 * or whenever there is no page directory covering the virtual address range.
92 */
Jérôme Glisse2aee09d2018-04-10 16:29:02 -070093static int hmm_vma_walk_hole_(unsigned long addr, unsigned long end,
94 bool fault, bool write_fault,
95 struct mm_walk *walk)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070096{
Jérôme Glisse74eee182017-09-08 16:11:35 -070097 struct hmm_vma_walk *hmm_vma_walk = walk->private;
98 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -070099 uint64_t *pfns = range->pfns;
Christoph Hellwig7f082632019-08-06 19:05:45 +0300100 unsigned long i;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700101
Jérôme Glisse74eee182017-09-08 16:11:35 -0700102 hmm_vma_walk->last = addr;
Christoph Hellwig7f082632019-08-06 19:05:45 +0300103 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700104
Ralph Campbellc18ce674d2019-08-23 15:17:53 -0700105 if (write_fault && walk->vma && !(walk->vma->vm_flags & VM_WRITE))
106 return -EPERM;
107
Christoph Hellwig7f082632019-08-06 19:05:45 +0300108 for (; addr < end; addr += PAGE_SIZE, i++) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700109 pfns[i] = range->values[HMM_PFN_NONE];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700110 if (fault || write_fault) {
Jérôme Glisse74eee182017-09-08 16:11:35 -0700111 int ret;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700112
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700113 ret = hmm_vma_do_fault(walk, addr, write_fault,
114 &pfns[i]);
Jérôme Glisse73231612019-05-13 17:19:58 -0700115 if (ret != -EBUSY)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700116 return ret;
117 }
118 }
119
Jérôme Glisse73231612019-05-13 17:19:58 -0700120 return (fault || write_fault) ? -EBUSY : 0;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700121}
122
123static inline void hmm_pte_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
124 uint64_t pfns, uint64_t cpu_flags,
125 bool *fault, bool *write_fault)
126{
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700127 struct hmm_range *range = hmm_vma_walk->range;
128
Christoph Hellwigd45d4642019-07-25 17:56:47 -0700129 if (hmm_vma_walk->flags & HMM_FAULT_SNAPSHOT)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700130 return;
131
Jérôme Glisse023a0192019-05-13 17:20:05 -0700132 /*
133 * So we not only consider the individual per page request we also
134 * consider the default flags requested for the range. The API can
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700135 * be used 2 ways. The first one where the HMM user coalesces
136 * multiple page faults into one request and sets flags per pfn for
137 * those faults. The second one where the HMM user wants to pre-
Jérôme Glisse023a0192019-05-13 17:20:05 -0700138 * fault a range with specific flags. For the latter one it is a
139 * waste to have the user pre-fill the pfn arrays with a default
140 * flags value.
141 */
142 pfns = (pfns & range->pfn_flags_mask) | range->default_flags;
143
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700144 /* We aren't ask to do anything ... */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700145 if (!(pfns & range->flags[HMM_PFN_VALID]))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700146 return;
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700147 /* If this is device memory then only fault if explicitly requested */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700148 if ((cpu_flags & range->flags[HMM_PFN_DEVICE_PRIVATE])) {
149 /* Do we fault on device memory ? */
150 if (pfns & range->flags[HMM_PFN_DEVICE_PRIVATE]) {
151 *write_fault = pfns & range->flags[HMM_PFN_WRITE];
152 *fault = true;
153 }
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700154 return;
155 }
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700156
157 /* If CPU page table is not valid then we need to fault */
158 *fault = !(cpu_flags & range->flags[HMM_PFN_VALID]);
159 /* Need to write fault ? */
160 if ((pfns & range->flags[HMM_PFN_WRITE]) &&
161 !(cpu_flags & range->flags[HMM_PFN_WRITE])) {
162 *write_fault = true;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700163 *fault = true;
164 }
165}
166
167static void hmm_range_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
168 const uint64_t *pfns, unsigned long npages,
169 uint64_t cpu_flags, bool *fault,
170 bool *write_fault)
171{
172 unsigned long i;
173
Christoph Hellwigd45d4642019-07-25 17:56:47 -0700174 if (hmm_vma_walk->flags & HMM_FAULT_SNAPSHOT) {
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700175 *fault = *write_fault = false;
176 return;
177 }
178
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700179 *fault = *write_fault = false;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700180 for (i = 0; i < npages; ++i) {
181 hmm_pte_need_fault(hmm_vma_walk, pfns[i], cpu_flags,
182 fault, write_fault);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700183 if ((*write_fault))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700184 return;
185 }
186}
187
188static int hmm_vma_walk_hole(unsigned long addr, unsigned long end,
Steven Priceb7a16c72020-02-03 17:36:03 -0800189 __always_unused int depth, struct mm_walk *walk)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700190{
191 struct hmm_vma_walk *hmm_vma_walk = walk->private;
192 struct hmm_range *range = hmm_vma_walk->range;
193 bool fault, write_fault;
194 unsigned long i, npages;
195 uint64_t *pfns;
196
197 i = (addr - range->start) >> PAGE_SHIFT;
198 npages = (end - addr) >> PAGE_SHIFT;
199 pfns = &range->pfns[i];
200 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
201 0, &fault, &write_fault);
202 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
203}
204
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700205static inline uint64_t pmd_to_hmm_pfn_flags(struct hmm_range *range, pmd_t pmd)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700206{
207 if (pmd_protnone(pmd))
208 return 0;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700209 return pmd_write(pmd) ? range->flags[HMM_PFN_VALID] |
210 range->flags[HMM_PFN_WRITE] :
211 range->flags[HMM_PFN_VALID];
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700212}
213
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700214#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Christoph Hellwig9d3973d2019-08-06 19:05:49 +0300215static int hmm_vma_handle_pmd(struct mm_walk *walk, unsigned long addr,
216 unsigned long end, uint64_t *pfns, pmd_t pmd)
217{
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700218 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700219 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700220 unsigned long pfn, npages, i;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700221 bool fault, write_fault;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700222 uint64_t cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700223
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700224 npages = (end - addr) >> PAGE_SHIFT;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700225 cpu_flags = pmd_to_hmm_pfn_flags(range, pmd);
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700226 hmm_range_need_fault(hmm_vma_walk, pfns, npages, cpu_flags,
227 &fault, &write_fault);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700228
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700229 if (pmd_protnone(pmd) || fault || write_fault)
230 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700231
Christoph Hellwig309f9a42019-08-06 19:05:47 +0300232 pfn = pmd_pfn(pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700233 for (i = 0; addr < end; addr += PAGE_SIZE, i++, pfn++) {
234 if (pmd_devmap(pmd)) {
235 hmm_vma_walk->pgmap = get_dev_pagemap(pfn,
236 hmm_vma_walk->pgmap);
237 if (unlikely(!hmm_vma_walk->pgmap))
238 return -EBUSY;
239 }
Jérôme Glisse391aab12019-05-13 17:20:31 -0700240 pfns[i] = hmm_device_entry_from_pfn(range, pfn) | cpu_flags;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700241 }
242 if (hmm_vma_walk->pgmap) {
243 put_dev_pagemap(hmm_vma_walk->pgmap);
244 hmm_vma_walk->pgmap = NULL;
245 }
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700246 hmm_vma_walk->last = end;
247 return 0;
248}
Christoph Hellwig9d3973d2019-08-06 19:05:49 +0300249#else /* CONFIG_TRANSPARENT_HUGEPAGE */
250/* stub to allow the code below to compile */
251int hmm_vma_handle_pmd(struct mm_walk *walk, unsigned long addr,
252 unsigned long end, uint64_t *pfns, pmd_t pmd);
253#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700254
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700255static inline uint64_t pte_to_hmm_pfn_flags(struct hmm_range *range, pte_t pte)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700256{
Philip Yang789c2af2019-05-23 16:32:31 -0400257 if (pte_none(pte) || !pte_present(pte) || pte_protnone(pte))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700258 return 0;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700259 return pte_write(pte) ? range->flags[HMM_PFN_VALID] |
260 range->flags[HMM_PFN_WRITE] :
261 range->flags[HMM_PFN_VALID];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700262}
263
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700264static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,
265 unsigned long end, pmd_t *pmdp, pte_t *ptep,
266 uint64_t *pfn)
267{
268 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700269 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700270 bool fault, write_fault;
271 uint64_t cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700272 pte_t pte = *ptep;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700273 uint64_t orig_pfn = *pfn;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700274
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700275 *pfn = range->values[HMM_PFN_NONE];
Jérôme Glisse73231612019-05-13 17:19:58 -0700276 fault = write_fault = false;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700277
278 if (pte_none(pte)) {
Jérôme Glisse73231612019-05-13 17:19:58 -0700279 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, 0,
280 &fault, &write_fault);
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700281 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700282 goto fault;
283 return 0;
284 }
285
286 if (!pte_present(pte)) {
287 swp_entry_t entry = pte_to_swp_entry(pte);
288
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700289 /*
290 * This is a special swap entry, ignore migration, use
291 * device and report anything else as error.
292 */
293 if (is_device_private_entry(entry)) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700294 cpu_flags = range->flags[HMM_PFN_VALID] |
295 range->flags[HMM_PFN_DEVICE_PRIVATE];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700296 cpu_flags |= is_write_device_private_entry(entry) ?
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700297 range->flags[HMM_PFN_WRITE] : 0;
298 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
299 &fault, &write_fault);
300 if (fault || write_fault)
301 goto fault;
Jérôme Glisse391aab12019-05-13 17:20:31 -0700302 *pfn = hmm_device_entry_from_pfn(range,
303 swp_offset(entry));
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700304 *pfn |= cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700305 return 0;
306 }
307
Jason Gunthorpe76612d62020-02-28 15:52:32 -0400308 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, 0, &fault,
309 &write_fault);
310 if (!fault && !write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700311 return 0;
Jason Gunthorpe76612d62020-02-28 15:52:32 -0400312
313 if (!non_swap_entry(entry))
314 goto fault;
315
316 if (is_migration_entry(entry)) {
317 pte_unmap(ptep);
318 hmm_vma_walk->last = addr;
319 migration_entry_wait(walk->mm, pmdp, addr);
320 return -EBUSY;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700321 }
322
323 /* Report error for everything else */
Jason Gunthorpedfdc2202020-02-28 15:30:37 -0400324 pte_unmap(ptep);
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700325 *pfn = range->values[HMM_PFN_ERROR];
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700326 return -EFAULT;
327 }
328
Jason Gunthorpe76612d62020-02-28 15:52:32 -0400329 cpu_flags = pte_to_hmm_pfn_flags(range, pte);
330 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags, &fault,
331 &write_fault);
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700332 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700333 goto fault;
334
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700335 if (pte_devmap(pte)) {
336 hmm_vma_walk->pgmap = get_dev_pagemap(pte_pfn(pte),
337 hmm_vma_walk->pgmap);
Jason Gunthorpedfdc2202020-02-28 15:30:37 -0400338 if (unlikely(!hmm_vma_walk->pgmap)) {
339 pte_unmap(ptep);
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700340 return -EBUSY;
Jason Gunthorpedfdc2202020-02-28 15:30:37 -0400341 }
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700342 } else if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) && pte_special(pte)) {
Ralph Campbellac541f22019-10-23 12:55:14 -0700343 if (!is_zero_pfn(pte_pfn(pte))) {
Jason Gunthorpedfdc2202020-02-28 15:30:37 -0400344 pte_unmap(ptep);
Ralph Campbellac541f22019-10-23 12:55:14 -0700345 *pfn = range->values[HMM_PFN_SPECIAL];
346 return -EFAULT;
347 }
348 /*
349 * Since each architecture defines a struct page for the zero
350 * page, just fall through and treat it like a normal page.
351 */
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700352 }
353
Jérôme Glisse391aab12019-05-13 17:20:31 -0700354 *pfn = hmm_device_entry_from_pfn(range, pte_pfn(pte)) | cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700355 return 0;
356
357fault:
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700358 if (hmm_vma_walk->pgmap) {
359 put_dev_pagemap(hmm_vma_walk->pgmap);
360 hmm_vma_walk->pgmap = NULL;
361 }
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700362 pte_unmap(ptep);
363 /* Fault any virtual address we were asked to fault */
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700364 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700365}
366
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700367static int hmm_vma_walk_pmd(pmd_t *pmdp,
368 unsigned long start,
369 unsigned long end,
370 struct mm_walk *walk)
371{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700372 struct hmm_vma_walk *hmm_vma_walk = walk->private;
373 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700374 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700375 unsigned long addr = start, i;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700376 pte_t *ptep;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700377 pmd_t pmd;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700378
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700379again:
Jérôme Glissed08faca2018-10-30 15:04:20 -0700380 pmd = READ_ONCE(*pmdp);
381 if (pmd_none(pmd))
Steven Priceb7a16c72020-02-03 17:36:03 -0800382 return hmm_vma_walk_hole(start, end, -1, walk);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700383
Jérôme Glissed08faca2018-10-30 15:04:20 -0700384 if (thp_migration_supported() && is_pmd_migration_entry(pmd)) {
385 bool fault, write_fault;
386 unsigned long npages;
387 uint64_t *pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700388
Jérôme Glissed08faca2018-10-30 15:04:20 -0700389 i = (addr - range->start) >> PAGE_SHIFT;
390 npages = (end - addr) >> PAGE_SHIFT;
391 pfns = &range->pfns[i];
392
393 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
394 0, &fault, &write_fault);
395 if (fault || write_fault) {
396 hmm_vma_walk->last = addr;
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700397 pmd_migration_entry_wait(walk->mm, pmdp);
Jérôme Glisse73231612019-05-13 17:19:58 -0700398 return -EBUSY;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700399 }
Jason Gunthorpe7d082982020-03-04 16:25:56 -0400400 return hmm_pfns_fill(start, end, range, HMM_PFN_NONE);
Jérôme Glissed08faca2018-10-30 15:04:20 -0700401 } else if (!pmd_present(pmd))
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800402 return hmm_pfns_fill(start, end, range, HMM_PFN_ERROR);
Jérôme Glissed08faca2018-10-30 15:04:20 -0700403
404 if (pmd_devmap(pmd) || pmd_trans_huge(pmd)) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700405 /*
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700406 * No need to take pmd_lock here, even if some other thread
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700407 * is splitting the huge pmd we will get that event through
408 * mmu_notifier callback.
409 *
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700410 * So just read pmd value and check again it's a transparent
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700411 * 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 Glisseda4c3c72017-09-08 16:11:31 -0700418
Jérôme Glissed08faca2018-10-30 15:04:20 -0700419 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700420 return hmm_vma_handle_pmd(walk, addr, end, &pfns[i], pmd);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700421 }
422
Jérôme Glissed08faca2018-10-30 15:04:20 -0700423 /*
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700424 * We have handled all the valid cases above ie either none, migration,
Jérôme Glissed08faca2018-10-30 15:04:20 -0700425 * huge or transparent huge. At this point either it is a valid pmd
426 * entry pointing to pte directory or it is a bad pmd that will not
427 * recover.
428 */
429 if (pmd_bad(pmd))
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800430 return hmm_pfns_fill(start, end, range, HMM_PFN_ERROR);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700431
432 ptep = pte_offset_map(pmdp, addr);
Jérôme Glissed08faca2018-10-30 15:04:20 -0700433 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700434 for (; addr < end; addr += PAGE_SIZE, ptep++, i++) {
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700435 int r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700436
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700437 r = hmm_vma_handle_pte(walk, addr, end, pmdp, ptep, &pfns[i]);
438 if (r) {
Jason Gunthorpedfdc2202020-02-28 15:30:37 -0400439 /* hmm_vma_handle_pte() did pte_unmap() */
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700440 hmm_vma_walk->last = addr;
441 return r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700442 }
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700443 }
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700444 if (hmm_vma_walk->pgmap) {
445 /*
446 * We do put_dev_pagemap() here and not in hmm_vma_handle_pte()
447 * so that we can leverage get_dev_pagemap() optimization which
448 * will not re-take a reference on a pgmap if we already have
449 * one.
450 */
451 put_dev_pagemap(hmm_vma_walk->pgmap);
452 hmm_vma_walk->pgmap = NULL;
453 }
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700454 pte_unmap(ptep - 1);
455
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700456 hmm_vma_walk->last = addr;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700457 return 0;
458}
459
Christoph Hellwigf0b3c452019-08-06 19:05:48 +0300460#if defined(CONFIG_ARCH_HAS_PTE_DEVMAP) && \
461 defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
462static inline uint64_t pud_to_hmm_pfn_flags(struct hmm_range *range, pud_t pud)
463{
464 if (!pud_present(pud))
465 return 0;
466 return pud_write(pud) ? range->flags[HMM_PFN_VALID] |
467 range->flags[HMM_PFN_WRITE] :
468 range->flags[HMM_PFN_VALID];
469}
470
471static int hmm_vma_walk_pud(pud_t *pudp, unsigned long start, unsigned long end,
472 struct mm_walk *walk)
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700473{
474 struct hmm_vma_walk *hmm_vma_walk = walk->private;
475 struct hmm_range *range = hmm_vma_walk->range;
Steven Price3afc4232020-02-03 17:35:45 -0800476 unsigned long addr = start;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700477 pud_t pud;
Steven Price3afc4232020-02-03 17:35:45 -0800478 int ret = 0;
479 spinlock_t *ptl = pud_trans_huge_lock(pudp, walk->vma);
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700480
Steven Price3afc4232020-02-03 17:35:45 -0800481 if (!ptl)
482 return 0;
483
484 /* Normally we don't want to split the huge page */
485 walk->action = ACTION_CONTINUE;
486
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700487 pud = READ_ONCE(*pudp);
Steven Price3afc4232020-02-03 17:35:45 -0800488 if (pud_none(pud)) {
Jason Gunthorpe05fc1df2020-03-02 15:26:44 -0400489 spin_unlock(ptl);
490 return hmm_vma_walk_hole(start, end, -1, walk);
Steven Price3afc4232020-02-03 17:35:45 -0800491 }
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700492
493 if (pud_huge(pud) && pud_devmap(pud)) {
494 unsigned long i, npages, pfn;
495 uint64_t *pfns, cpu_flags;
496 bool fault, write_fault;
497
Steven Price3afc4232020-02-03 17:35:45 -0800498 if (!pud_present(pud)) {
Jason Gunthorpe05fc1df2020-03-02 15:26:44 -0400499 spin_unlock(ptl);
500 return hmm_vma_walk_hole(start, end, -1, walk);
Steven Price3afc4232020-02-03 17:35:45 -0800501 }
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700502
503 i = (addr - range->start) >> PAGE_SHIFT;
504 npages = (end - addr) >> PAGE_SHIFT;
505 pfns = &range->pfns[i];
506
507 cpu_flags = pud_to_hmm_pfn_flags(range, pud);
508 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
509 cpu_flags, &fault, &write_fault);
Steven Price3afc4232020-02-03 17:35:45 -0800510 if (fault || write_fault) {
Jason Gunthorpe05fc1df2020-03-02 15:26:44 -0400511 spin_unlock(ptl);
512 return hmm_vma_walk_hole_(addr, end, fault, write_fault,
513 walk);
Steven Price3afc4232020-02-03 17:35:45 -0800514 }
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700515
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700516 pfn = pud_pfn(pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
517 for (i = 0; i < npages; ++i, ++pfn) {
518 hmm_vma_walk->pgmap = get_dev_pagemap(pfn,
519 hmm_vma_walk->pgmap);
Steven Price3afc4232020-02-03 17:35:45 -0800520 if (unlikely(!hmm_vma_walk->pgmap)) {
521 ret = -EBUSY;
522 goto out_unlock;
523 }
Jérôme Glisse391aab12019-05-13 17:20:31 -0700524 pfns[i] = hmm_device_entry_from_pfn(range, pfn) |
525 cpu_flags;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700526 }
527 if (hmm_vma_walk->pgmap) {
528 put_dev_pagemap(hmm_vma_walk->pgmap);
529 hmm_vma_walk->pgmap = NULL;
530 }
531 hmm_vma_walk->last = end;
Steven Price3afc4232020-02-03 17:35:45 -0800532 goto out_unlock;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700533 }
534
Steven Price3afc4232020-02-03 17:35:45 -0800535 /* Ask for the PUD to be split */
536 walk->action = ACTION_SUBTREE;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700537
Steven Price3afc4232020-02-03 17:35:45 -0800538out_unlock:
539 spin_unlock(ptl);
540 return ret;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700541}
Christoph Hellwigf0b3c452019-08-06 19:05:48 +0300542#else
543#define hmm_vma_walk_pud NULL
544#endif
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700545
Christoph Hellwig251bbe52019-08-06 19:05:50 +0300546#ifdef CONFIG_HUGETLB_PAGE
Jérôme Glisse63d50662019-05-13 17:20:18 -0700547static int hmm_vma_walk_hugetlb_entry(pte_t *pte, unsigned long hmask,
548 unsigned long start, unsigned long end,
549 struct mm_walk *walk)
550{
Christoph Hellwig05c23af2019-08-06 19:05:46 +0300551 unsigned long addr = start, i, pfn;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700552 struct hmm_vma_walk *hmm_vma_walk = walk->private;
553 struct hmm_range *range = hmm_vma_walk->range;
554 struct vm_area_struct *vma = walk->vma;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700555 uint64_t orig_pfn, cpu_flags;
556 bool fault, write_fault;
557 spinlock_t *ptl;
558 pte_t entry;
559 int ret = 0;
560
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700561 ptl = huge_pte_lock(hstate_vma(vma), walk->mm, pte);
Jérôme Glisse63d50662019-05-13 17:20:18 -0700562 entry = huge_ptep_get(pte);
563
Christoph Hellwig7f082632019-08-06 19:05:45 +0300564 i = (start - range->start) >> PAGE_SHIFT;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700565 orig_pfn = range->pfns[i];
566 range->pfns[i] = range->values[HMM_PFN_NONE];
567 cpu_flags = pte_to_hmm_pfn_flags(range, entry);
568 fault = write_fault = false;
569 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
570 &fault, &write_fault);
571 if (fault || write_fault) {
572 ret = -ENOENT;
573 goto unlock;
574 }
575
Christoph Hellwig05c23af2019-08-06 19:05:46 +0300576 pfn = pte_pfn(entry) + ((start & ~hmask) >> PAGE_SHIFT);
Christoph Hellwig7f082632019-08-06 19:05:45 +0300577 for (; addr < end; addr += PAGE_SIZE, i++, pfn++)
Jérôme Glisse391aab12019-05-13 17:20:31 -0700578 range->pfns[i] = hmm_device_entry_from_pfn(range, pfn) |
579 cpu_flags;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700580 hmm_vma_walk->last = end;
581
582unlock:
583 spin_unlock(ptl);
584
585 if (ret == -ENOENT)
586 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
587
588 return ret;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700589}
Christoph Hellwig251bbe52019-08-06 19:05:50 +0300590#else
591#define hmm_vma_walk_hugetlb_entry NULL
592#endif /* CONFIG_HUGETLB_PAGE */
Jérôme Glisse63d50662019-05-13 17:20:18 -0700593
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800594static int hmm_vma_walk_test(unsigned long start, unsigned long end,
595 struct mm_walk *walk)
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700596{
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800597 struct hmm_vma_walk *hmm_vma_walk = walk->private;
598 struct hmm_range *range = hmm_vma_walk->range;
599 struct vm_area_struct *vma = walk->vma;
600
601 /*
Jason Gunthorpec2579c92020-03-05 12:00:22 -0400602 * Skip vma ranges that don't have struct page backing them or map I/O
603 * devices directly.
604 *
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800605 * If the vma does not allow read access, then assume that it does not
Jason Gunthorpec2579c92020-03-05 12:00:22 -0400606 * allow write access either. HMM does not support architectures that
607 * allow write without read.
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800608 */
Jason Gunthorpec2579c92020-03-05 12:00:22 -0400609 if ((vma->vm_flags & (VM_IO | VM_PFNMAP | VM_MIXEDMAP)) ||
610 !(vma->vm_flags & VM_READ)) {
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800611 bool fault, write_fault;
612
613 /*
614 * Check to see if a fault is requested for any page in the
615 * range.
616 */
617 hmm_range_need_fault(hmm_vma_walk, range->pfns +
618 ((start - range->start) >> PAGE_SHIFT),
619 (end - start) >> PAGE_SHIFT,
620 0, &fault, &write_fault);
621 if (fault || write_fault)
622 return -EFAULT;
623
Jason Gunthorpec2579c92020-03-05 12:00:22 -0400624 hmm_pfns_fill(start, end, range, HMM_PFN_ERROR);
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800625 hmm_vma_walk->last = end;
626
627 /* Skip this vma and continue processing the next vma. */
628 return 1;
629 }
630
631 return 0;
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700632}
633
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200634static const struct mm_walk_ops hmm_walk_ops = {
635 .pud_entry = hmm_vma_walk_pud,
636 .pmd_entry = hmm_vma_walk_pmd,
637 .pte_hole = hmm_vma_walk_hole,
638 .hugetlb_entry = hmm_vma_walk_hugetlb_entry,
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800639 .test_walk = hmm_vma_walk_test,
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200640};
641
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700642/**
643 * hmm_range_fault - try to fault some address in a virtual address range
644 * @range: range being faulted
645 * @flags: HMM_FAULT_* flags
Jérôme Glisse73231612019-05-13 17:19:58 -0700646 *
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700647 * Return: the number of valid pages in range->pfns[] (from range start
648 * address), which may be zero. On error one of the following status codes
649 * can be returned:
650 *
651 * -EINVAL: Invalid arguments or mm or virtual address is in an invalid vma
652 * (e.g., device file vma).
653 * -ENOMEM: Out of memory.
654 * -EPERM: Invalid permission (e.g., asking for write and range is read
655 * only).
656 * -EAGAIN: A page fault needs to be retried and mmap_sem was dropped.
657 * -EBUSY: The range has been invalidated and the caller needs to wait for
658 * the invalidation to finish.
659 * -EFAULT: Invalid (i.e., either no valid vma or it is illegal to access
660 * that range) number of valid pages in range->pfns[] (from
661 * range start address).
Jérôme Glisse74eee182017-09-08 16:11:35 -0700662 *
663 * This is similar to a regular CPU page fault except that it will not trigger
Jérôme Glisse73231612019-05-13 17:19:58 -0700664 * any memory migration if the memory being faulted is not accessible by CPUs
665 * and caller does not ask for migration.
Jérôme Glisse74eee182017-09-08 16:11:35 -0700666 *
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700667 * On error, for one virtual address in the range, the function will mark the
668 * corresponding HMM pfn entry with an error flag.
Jérôme Glisse74eee182017-09-08 16:11:35 -0700669 */
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700670long hmm_range_fault(struct hmm_range *range, unsigned int flags)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700671{
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800672 struct hmm_vma_walk hmm_vma_walk = {
673 .range = range,
674 .last = range->start,
675 .flags = flags,
676 };
Jason Gunthorpea22dd502019-11-12 16:22:30 -0400677 struct mm_struct *mm = range->notifier->mm;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700678 int ret;
679
Jason Gunthorpe04ec32f2019-11-12 16:22:20 -0400680 lockdep_assert_held(&mm->mmap_sem);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700681
682 do {
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700683 /* If range is no longer valid force retry. */
Jason Gunthorpea22dd502019-11-12 16:22:30 -0400684 if (mmu_interval_check_retry(range->notifier,
685 range->notifier_seq))
Christoph Hellwig2bcbeae2019-07-24 08:52:52 +0200686 return -EBUSY;
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800687 ret = walk_page_range(mm, hmm_vma_walk.last, range->end,
688 &hmm_walk_ops, &hmm_vma_walk);
689 } while (ret == -EBUSY);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700690
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800691 if (ret)
692 return ret;
Jérôme Glisse73231612019-05-13 17:19:58 -0700693 return (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700694}
Jérôme Glisse73231612019-05-13 17:19:58 -0700695EXPORT_SYMBOL(hmm_range_fault);