blob: cbe9d0c66650452ba3e4a57b6af6c7f8f0940f56 [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
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -080040static int hmm_pfns_fill(unsigned long addr, unsigned long end,
Jason Gunthorpe2733ea12020-05-01 15:20:48 -030041 struct hmm_range *range, unsigned long cpu_flags)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070042{
Jason Gunthorpe2733ea12020-05-01 15:20:48 -030043 unsigned long i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070044
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070045 for (; addr < end; addr += PAGE_SIZE, i++)
Jason Gunthorpe2733ea12020-05-01 15:20:48 -030046 range->hmm_pfns[i] = cpu_flags;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070047 return 0;
48}
49
Jérôme Glisse5504ed22018-04-10 16:28:46 -070050/*
Christoph Hellwigf8c888a2020-03-16 14:53:09 +010051 * hmm_vma_fault() - fault in a range lacking valid pmd or pte(s)
Ralph Campbelld2e8d552019-07-25 17:56:45 -070052 * @addr: range virtual start address (inclusive)
Jérôme Glisse5504ed22018-04-10 16:28:46 -070053 * @end: range virtual end address (exclusive)
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -030054 * @required_fault: HMM_NEED_* flags
Jérôme Glisse5504ed22018-04-10 16:28:46 -070055 * @walk: mm_walk structure
Christoph Hellwigf8c888a2020-03-16 14:53:09 +010056 * Return: -EBUSY after page fault, or page fault error
Jérôme Glisse5504ed22018-04-10 16:28:46 -070057 *
58 * This function will be called whenever pmd_none() or pte_none() returns true,
59 * or whenever there is no page directory covering the virtual address range.
60 */
Christoph Hellwigf8c888a2020-03-16 14:53:09 +010061static int hmm_vma_fault(unsigned long addr, unsigned long end,
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -030062 unsigned int required_fault, struct mm_walk *walk)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070063{
Jérôme Glisse74eee182017-09-08 16:11:35 -070064 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Christoph Hellwig5a0c38d2020-03-16 14:53:10 +010065 struct vm_area_struct *vma = walk->vma;
Christoph Hellwig5a0c38d2020-03-16 14:53:10 +010066 unsigned int fault_flags = FAULT_FLAG_REMOTE;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070067
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -030068 WARN_ON_ONCE(!required_fault);
Jérôme Glisse74eee182017-09-08 16:11:35 -070069 hmm_vma_walk->last = addr;
Jérôme Glisse63d50662019-05-13 17:20:18 -070070
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -030071 if (required_fault & HMM_NEED_WRITE_FAULT) {
Christoph Hellwig5a0c38d2020-03-16 14:53:10 +010072 if (!(vma->vm_flags & VM_WRITE))
73 return -EPERM;
74 fault_flags |= FAULT_FLAG_WRITE;
Jérôme Glisse74eee182017-09-08 16:11:35 -070075 }
76
Jason Gunthorpe53bfe17f2020-03-27 17:00:20 -030077 for (; addr < end; addr += PAGE_SIZE)
Peter Xubce617e2020-08-11 18:37:44 -070078 if (handle_mm_fault(vma, addr, fault_flags, NULL) &
79 VM_FAULT_ERROR)
Jason Gunthorpe53bfe17f2020-03-27 17:00:20 -030080 return -EFAULT;
Christoph Hellwigf8c888a2020-03-16 14:53:09 +010081 return -EBUSY;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -070082}
83
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -030084static unsigned int hmm_pte_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
Jason Gunthorpe2733ea12020-05-01 15:20:48 -030085 unsigned long pfn_req_flags,
86 unsigned long cpu_flags)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -070087{
Jérôme Glissef88a1e92018-04-10 16:29:06 -070088 struct hmm_range *range = hmm_vma_walk->range;
89
Jérôme Glisse023a0192019-05-13 17:20:05 -070090 /*
91 * So we not only consider the individual per page request we also
92 * consider the default flags requested for the range. The API can
Ralph Campbelld2e8d552019-07-25 17:56:45 -070093 * be used 2 ways. The first one where the HMM user coalesces
94 * multiple page faults into one request and sets flags per pfn for
95 * those faults. The second one where the HMM user wants to pre-
Jérôme Glisse023a0192019-05-13 17:20:05 -070096 * fault a range with specific flags. For the latter one it is a
97 * waste to have the user pre-fill the pfn arrays with a default
98 * flags value.
99 */
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300100 pfn_req_flags &= range->pfn_flags_mask;
101 pfn_req_flags |= range->default_flags;
Jérôme Glisse023a0192019-05-13 17:20:05 -0700102
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700103 /* We aren't ask to do anything ... */
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300104 if (!(pfn_req_flags & HMM_PFN_REQ_FAULT))
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300105 return 0;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700106
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700107 /* Need to write fault ? */
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300108 if ((pfn_req_flags & HMM_PFN_REQ_WRITE) &&
109 !(cpu_flags & HMM_PFN_WRITE))
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300110 return HMM_NEED_FAULT | HMM_NEED_WRITE_FAULT;
111
112 /* If CPU page table is not valid then we need to fault */
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300113 if (!(cpu_flags & HMM_PFN_VALID))
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300114 return HMM_NEED_FAULT;
115 return 0;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700116}
117
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300118static unsigned int
119hmm_range_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300120 const unsigned long hmm_pfns[], unsigned long npages,
121 unsigned long cpu_flags)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700122{
Jason Gunthorpe6bfef2f2020-03-27 17:00:16 -0300123 struct hmm_range *range = hmm_vma_walk->range;
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300124 unsigned int required_fault = 0;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700125 unsigned long i;
126
Jason Gunthorpe6bfef2f2020-03-27 17:00:16 -0300127 /*
128 * If the default flags do not request to fault pages, and the mask does
129 * not allow for individual pages to be faulted, then
130 * hmm_pte_need_fault() will always return 0.
131 */
132 if (!((range->default_flags | range->pfn_flags_mask) &
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300133 HMM_PFN_REQ_FAULT))
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300134 return 0;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700135
136 for (i = 0; i < npages; ++i) {
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300137 required_fault |= hmm_pte_need_fault(hmm_vma_walk, hmm_pfns[i],
138 cpu_flags);
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300139 if (required_fault == HMM_NEED_ALL_BITS)
140 return required_fault;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700141 }
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300142 return required_fault;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700143}
144
145static int hmm_vma_walk_hole(unsigned long addr, unsigned long end,
Steven Priceb7a16c72020-02-03 17:36:03 -0800146 __always_unused int depth, struct mm_walk *walk)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700147{
148 struct hmm_vma_walk *hmm_vma_walk = walk->private;
149 struct hmm_range *range = hmm_vma_walk->range;
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300150 unsigned int required_fault;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700151 unsigned long i, npages;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300152 unsigned long *hmm_pfns;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700153
154 i = (addr - range->start) >> PAGE_SHIFT;
155 npages = (end - addr) >> PAGE_SHIFT;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300156 hmm_pfns = &range->hmm_pfns[i];
157 required_fault =
158 hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0);
Jason Gunthorpebd5d3582020-03-27 17:00:21 -0300159 if (!walk->vma) {
160 if (required_fault)
161 return -EFAULT;
162 return hmm_pfns_fill(addr, end, range, HMM_PFN_ERROR);
163 }
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300164 if (required_fault)
165 return hmm_vma_fault(addr, end, required_fault, walk);
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300166 return hmm_pfns_fill(addr, end, range, 0);
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700167}
168
Ralph Campbell3b50a6e2020-07-01 15:53:49 -0700169static inline unsigned long hmm_pfn_flags_order(unsigned long order)
170{
171 return order << HMM_PFN_ORDER_SHIFT;
172}
173
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300174static inline unsigned long pmd_to_hmm_pfn_flags(struct hmm_range *range,
175 pmd_t pmd)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700176{
177 if (pmd_protnone(pmd))
178 return 0;
Ralph Campbell3b50a6e2020-07-01 15:53:49 -0700179 return (pmd_write(pmd) ? (HMM_PFN_VALID | HMM_PFN_WRITE) :
180 HMM_PFN_VALID) |
181 hmm_pfn_flags_order(PMD_SHIFT - PAGE_SHIFT);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700182}
183
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700184#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Christoph Hellwig9d3973d2019-08-06 19:05:49 +0300185static int hmm_vma_handle_pmd(struct mm_walk *walk, unsigned long addr,
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300186 unsigned long end, unsigned long hmm_pfns[],
187 pmd_t pmd)
Christoph Hellwig9d3973d2019-08-06 19:05:49 +0300188{
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700189 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700190 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700191 unsigned long pfn, npages, i;
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300192 unsigned int required_fault;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300193 unsigned long cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700194
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700195 npages = (end - addr) >> PAGE_SHIFT;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700196 cpu_flags = pmd_to_hmm_pfn_flags(range, pmd);
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300197 required_fault =
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300198 hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, cpu_flags);
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300199 if (required_fault)
200 return hmm_vma_fault(addr, end, required_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700201
Christoph Hellwig309f9a42019-08-06 19:05:47 +0300202 pfn = pmd_pfn(pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
Jason Gunthorpe068354a2020-03-27 17:00:13 -0300203 for (i = 0; addr < end; addr += PAGE_SIZE, i++, pfn++)
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300204 hmm_pfns[i] = pfn | cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700205 return 0;
206}
Christoph Hellwig9d3973d2019-08-06 19:05:49 +0300207#else /* CONFIG_TRANSPARENT_HUGEPAGE */
208/* stub to allow the code below to compile */
209int hmm_vma_handle_pmd(struct mm_walk *walk, unsigned long addr,
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300210 unsigned long end, unsigned long hmm_pfns[], pmd_t pmd);
Christoph Hellwig9d3973d2019-08-06 19:05:49 +0300211#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700212
Christoph Hellwig08ddddd2020-03-16 20:32:16 +0100213static inline bool hmm_is_device_private_entry(struct hmm_range *range,
214 swp_entry_t entry)
215{
216 return is_device_private_entry(entry) &&
217 device_private_entry_to_page(entry)->pgmap->owner ==
218 range->dev_private_owner;
219}
220
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300221static inline unsigned long pte_to_hmm_pfn_flags(struct hmm_range *range,
222 pte_t pte)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700223{
Philip Yang789c2af2019-05-23 16:32:31 -0400224 if (pte_none(pte) || !pte_present(pte) || pte_protnone(pte))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700225 return 0;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300226 return pte_write(pte) ? (HMM_PFN_VALID | HMM_PFN_WRITE) : HMM_PFN_VALID;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700227}
228
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700229static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,
230 unsigned long end, pmd_t *pmdp, pte_t *ptep,
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300231 unsigned long *hmm_pfn)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700232{
233 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700234 struct hmm_range *range = hmm_vma_walk->range;
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300235 unsigned int required_fault;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300236 unsigned long cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700237 pte_t pte = *ptep;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300238 uint64_t pfn_req_flags = *hmm_pfn;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700239
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700240 if (pte_none(pte)) {
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300241 required_fault =
242 hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, 0);
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300243 if (required_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700244 goto fault;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300245 *hmm_pfn = 0;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700246 return 0;
247 }
248
249 if (!pte_present(pte)) {
250 swp_entry_t entry = pte_to_swp_entry(pte);
251
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700252 /*
Randy Dunlap0cb80a22020-08-11 18:32:56 -0700253 * Never fault in device private pages, but just report
Christoph Hellwig17ffdc42020-03-16 20:32:15 +0100254 * the PFN even if not present.
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700255 */
Christoph Hellwig08ddddd2020-03-16 20:32:16 +0100256 if (hmm_is_device_private_entry(range, entry)) {
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300257 cpu_flags = HMM_PFN_VALID;
Christoph Hellwig17ffdc42020-03-16 20:32:15 +0100258 if (is_write_device_private_entry(entry))
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300259 cpu_flags |= HMM_PFN_WRITE;
260 *hmm_pfn = device_private_entry_to_pfn(entry) |
261 cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700262 return 0;
263 }
264
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300265 required_fault =
266 hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, 0);
Jason Gunthorpe846babe2020-03-27 17:00:19 -0300267 if (!required_fault) {
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300268 *hmm_pfn = 0;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700269 return 0;
Jason Gunthorpe846babe2020-03-27 17:00:19 -0300270 }
Jason Gunthorpe76612d62020-02-28 15:52:32 -0400271
272 if (!non_swap_entry(entry))
273 goto fault;
274
275 if (is_migration_entry(entry)) {
276 pte_unmap(ptep);
277 hmm_vma_walk->last = addr;
278 migration_entry_wait(walk->mm, pmdp, addr);
279 return -EBUSY;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700280 }
281
282 /* Report error for everything else */
Jason Gunthorpedfdc2202020-02-28 15:30:37 -0400283 pte_unmap(ptep);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700284 return -EFAULT;
285 }
286
Jason Gunthorpe76612d62020-02-28 15:52:32 -0400287 cpu_flags = pte_to_hmm_pfn_flags(range, pte);
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300288 required_fault =
289 hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, cpu_flags);
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300290 if (required_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700291 goto fault;
292
Jason Gunthorpe40550622020-03-05 14:27:20 -0400293 /*
Li Zhijiance75a6b2021-09-08 18:10:02 -0700294 * Bypass devmap pte such as DAX page when all pfn requested
295 * flags(pfn_req_flags) are fulfilled.
Jason Gunthorpe40550622020-03-05 14:27:20 -0400296 * Since each architecture defines a struct page for the zero page, just
297 * fall through and treat it like a normal page.
298 */
Alistair Popple62925032022-01-14 14:09:31 -0800299 if (!vm_normal_page(walk->vma, addr, pte) &&
300 !pte_devmap(pte) &&
Li Zhijiance75a6b2021-09-08 18:10:02 -0700301 !is_zero_pfn(pte_pfn(pte))) {
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300302 if (hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, 0)) {
Jason Gunthorpedfdc2202020-02-28 15:30:37 -0400303 pte_unmap(ptep);
Ralph Campbellac541f22019-10-23 12:55:14 -0700304 return -EFAULT;
305 }
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300306 *hmm_pfn = HMM_PFN_ERROR;
Jason Gunthorpe40550622020-03-05 14:27:20 -0400307 return 0;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700308 }
309
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300310 *hmm_pfn = pte_pfn(pte) | cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700311 return 0;
312
313fault:
314 pte_unmap(ptep);
315 /* Fault any virtual address we were asked to fault */
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300316 return hmm_vma_fault(addr, end, required_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700317}
318
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700319static int hmm_vma_walk_pmd(pmd_t *pmdp,
320 unsigned long start,
321 unsigned long end,
322 struct mm_walk *walk)
323{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700324 struct hmm_vma_walk *hmm_vma_walk = walk->private;
325 struct hmm_range *range = hmm_vma_walk->range;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300326 unsigned long *hmm_pfns =
327 &range->hmm_pfns[(start - range->start) >> PAGE_SHIFT];
Jason Gunthorpe2288a9a2020-03-05 15:26:33 -0400328 unsigned long npages = (end - start) >> PAGE_SHIFT;
329 unsigned long addr = start;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700330 pte_t *ptep;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700331 pmd_t pmd;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700332
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700333again:
Jérôme Glissed08faca2018-10-30 15:04:20 -0700334 pmd = READ_ONCE(*pmdp);
335 if (pmd_none(pmd))
Steven Priceb7a16c72020-02-03 17:36:03 -0800336 return hmm_vma_walk_hole(start, end, -1, walk);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700337
Jérôme Glissed08faca2018-10-30 15:04:20 -0700338 if (thp_migration_supported() && is_pmd_migration_entry(pmd)) {
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300339 if (hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0)) {
Jérôme Glissed08faca2018-10-30 15:04:20 -0700340 hmm_vma_walk->last = addr;
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700341 pmd_migration_entry_wait(walk->mm, pmdp);
Jérôme Glisse73231612019-05-13 17:19:58 -0700342 return -EBUSY;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700343 }
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300344 return hmm_pfns_fill(start, end, range, 0);
Jason Gunthorpe2288a9a2020-03-05 15:26:33 -0400345 }
346
347 if (!pmd_present(pmd)) {
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300348 if (hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0))
Jason Gunthorpe2288a9a2020-03-05 15:26:33 -0400349 return -EFAULT;
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800350 return hmm_pfns_fill(start, end, range, HMM_PFN_ERROR);
Jason Gunthorpe2288a9a2020-03-05 15:26:33 -0400351 }
Jérôme Glissed08faca2018-10-30 15:04:20 -0700352
353 if (pmd_devmap(pmd) || pmd_trans_huge(pmd)) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700354 /*
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700355 * No need to take pmd_lock here, even if some other thread
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700356 * is splitting the huge pmd we will get that event through
357 * mmu_notifier callback.
358 *
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700359 * So just read pmd value and check again it's a transparent
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700360 * huge or device mapping one and compute corresponding pfn
361 * values.
362 */
363 pmd = pmd_read_atomic(pmdp);
364 barrier();
365 if (!pmd_devmap(pmd) && !pmd_trans_huge(pmd))
366 goto again;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700367
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300368 return hmm_vma_handle_pmd(walk, addr, end, hmm_pfns, pmd);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700369 }
370
Jérôme Glissed08faca2018-10-30 15:04:20 -0700371 /*
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700372 * We have handled all the valid cases above ie either none, migration,
Jérôme Glissed08faca2018-10-30 15:04:20 -0700373 * huge or transparent huge. At this point either it is a valid pmd
374 * entry pointing to pte directory or it is a bad pmd that will not
375 * recover.
376 */
Jason Gunthorpe2288a9a2020-03-05 15:26:33 -0400377 if (pmd_bad(pmd)) {
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300378 if (hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0))
Jason Gunthorpe2288a9a2020-03-05 15:26:33 -0400379 return -EFAULT;
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800380 return hmm_pfns_fill(start, end, range, HMM_PFN_ERROR);
Jason Gunthorpe2288a9a2020-03-05 15:26:33 -0400381 }
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700382
383 ptep = pte_offset_map(pmdp, addr);
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300384 for (; addr < end; addr += PAGE_SIZE, ptep++, hmm_pfns++) {
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700385 int r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700386
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300387 r = hmm_vma_handle_pte(walk, addr, end, pmdp, ptep, hmm_pfns);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700388 if (r) {
Jason Gunthorpedfdc2202020-02-28 15:30:37 -0400389 /* hmm_vma_handle_pte() did pte_unmap() */
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700390 return r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700391 }
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700392 }
393 pte_unmap(ptep - 1);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700394 return 0;
395}
396
Christoph Hellwigf0b3c452019-08-06 19:05:48 +0300397#if defined(CONFIG_ARCH_HAS_PTE_DEVMAP) && \
398 defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300399static inline unsigned long pud_to_hmm_pfn_flags(struct hmm_range *range,
400 pud_t pud)
Christoph Hellwigf0b3c452019-08-06 19:05:48 +0300401{
402 if (!pud_present(pud))
403 return 0;
Ralph Campbell3b50a6e2020-07-01 15:53:49 -0700404 return (pud_write(pud) ? (HMM_PFN_VALID | HMM_PFN_WRITE) :
405 HMM_PFN_VALID) |
406 hmm_pfn_flags_order(PUD_SHIFT - PAGE_SHIFT);
Christoph Hellwigf0b3c452019-08-06 19:05:48 +0300407}
408
409static int hmm_vma_walk_pud(pud_t *pudp, unsigned long start, unsigned long end,
410 struct mm_walk *walk)
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700411{
412 struct hmm_vma_walk *hmm_vma_walk = walk->private;
413 struct hmm_range *range = hmm_vma_walk->range;
Steven Price3afc4232020-02-03 17:35:45 -0800414 unsigned long addr = start;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700415 pud_t pud;
Steven Price3afc4232020-02-03 17:35:45 -0800416 int ret = 0;
417 spinlock_t *ptl = pud_trans_huge_lock(pudp, walk->vma);
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700418
Steven Price3afc4232020-02-03 17:35:45 -0800419 if (!ptl)
420 return 0;
421
422 /* Normally we don't want to split the huge page */
423 walk->action = ACTION_CONTINUE;
424
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700425 pud = READ_ONCE(*pudp);
Steven Price3afc4232020-02-03 17:35:45 -0800426 if (pud_none(pud)) {
Jason Gunthorpe05fc1df2020-03-02 15:26:44 -0400427 spin_unlock(ptl);
428 return hmm_vma_walk_hole(start, end, -1, walk);
Steven Price3afc4232020-02-03 17:35:45 -0800429 }
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700430
431 if (pud_huge(pud) && pud_devmap(pud)) {
432 unsigned long i, npages, pfn;
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300433 unsigned int required_fault;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300434 unsigned long *hmm_pfns;
435 unsigned long cpu_flags;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700436
Steven Price3afc4232020-02-03 17:35:45 -0800437 if (!pud_present(pud)) {
Jason Gunthorpe05fc1df2020-03-02 15:26:44 -0400438 spin_unlock(ptl);
439 return hmm_vma_walk_hole(start, end, -1, walk);
Steven Price3afc4232020-02-03 17:35:45 -0800440 }
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700441
442 i = (addr - range->start) >> PAGE_SHIFT;
443 npages = (end - addr) >> PAGE_SHIFT;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300444 hmm_pfns = &range->hmm_pfns[i];
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700445
446 cpu_flags = pud_to_hmm_pfn_flags(range, pud);
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300447 required_fault = hmm_range_need_fault(hmm_vma_walk, hmm_pfns,
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300448 npages, cpu_flags);
449 if (required_fault) {
Jason Gunthorpe05fc1df2020-03-02 15:26:44 -0400450 spin_unlock(ptl);
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300451 return hmm_vma_fault(addr, end, required_fault, walk);
Steven Price3afc4232020-02-03 17:35:45 -0800452 }
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700453
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700454 pfn = pud_pfn(pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
Jason Gunthorpe068354a2020-03-27 17:00:13 -0300455 for (i = 0; i < npages; ++i, ++pfn)
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300456 hmm_pfns[i] = pfn | cpu_flags;
Steven Price3afc4232020-02-03 17:35:45 -0800457 goto out_unlock;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700458 }
459
Steven Price3afc4232020-02-03 17:35:45 -0800460 /* Ask for the PUD to be split */
461 walk->action = ACTION_SUBTREE;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700462
Steven Price3afc4232020-02-03 17:35:45 -0800463out_unlock:
464 spin_unlock(ptl);
465 return ret;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700466}
Christoph Hellwigf0b3c452019-08-06 19:05:48 +0300467#else
468#define hmm_vma_walk_pud NULL
469#endif
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700470
Christoph Hellwig251bbe52019-08-06 19:05:50 +0300471#ifdef CONFIG_HUGETLB_PAGE
Jérôme Glisse63d50662019-05-13 17:20:18 -0700472static int hmm_vma_walk_hugetlb_entry(pte_t *pte, unsigned long hmask,
473 unsigned long start, unsigned long end,
474 struct mm_walk *walk)
475{
Christoph Hellwig05c23af2019-08-06 19:05:46 +0300476 unsigned long addr = start, i, pfn;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700477 struct hmm_vma_walk *hmm_vma_walk = walk->private;
478 struct hmm_range *range = hmm_vma_walk->range;
479 struct vm_area_struct *vma = walk->vma;
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300480 unsigned int required_fault;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300481 unsigned long pfn_req_flags;
482 unsigned long cpu_flags;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700483 spinlock_t *ptl;
484 pte_t entry;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700485
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700486 ptl = huge_pte_lock(hstate_vma(vma), walk->mm, pte);
Jérôme Glisse63d50662019-05-13 17:20:18 -0700487 entry = huge_ptep_get(pte);
488
Christoph Hellwig7f082632019-08-06 19:05:45 +0300489 i = (start - range->start) >> PAGE_SHIFT;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300490 pfn_req_flags = range->hmm_pfns[i];
Ralph Campbell3b50a6e2020-07-01 15:53:49 -0700491 cpu_flags = pte_to_hmm_pfn_flags(range, entry) |
492 hmm_pfn_flags_order(huge_page_order(hstate_vma(vma)));
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300493 required_fault =
494 hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, cpu_flags);
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300495 if (required_fault) {
Christoph Hellwig45050692020-03-16 14:53:08 +0100496 spin_unlock(ptl);
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300497 return hmm_vma_fault(addr, end, required_fault, walk);
Jérôme Glisse63d50662019-05-13 17:20:18 -0700498 }
499
Christoph Hellwig05c23af2019-08-06 19:05:46 +0300500 pfn = pte_pfn(entry) + ((start & ~hmask) >> PAGE_SHIFT);
Christoph Hellwig7f082632019-08-06 19:05:45 +0300501 for (; addr < end; addr += PAGE_SIZE, i++, pfn++)
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300502 range->hmm_pfns[i] = pfn | cpu_flags;
503
Jérôme Glisse63d50662019-05-13 17:20:18 -0700504 spin_unlock(ptl);
Christoph Hellwig45050692020-03-16 14:53:08 +0100505 return 0;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700506}
Christoph Hellwig251bbe52019-08-06 19:05:50 +0300507#else
508#define hmm_vma_walk_hugetlb_entry NULL
509#endif /* CONFIG_HUGETLB_PAGE */
Jérôme Glisse63d50662019-05-13 17:20:18 -0700510
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800511static int hmm_vma_walk_test(unsigned long start, unsigned long end,
512 struct mm_walk *walk)
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700513{
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800514 struct hmm_vma_walk *hmm_vma_walk = walk->private;
515 struct hmm_range *range = hmm_vma_walk->range;
516 struct vm_area_struct *vma = walk->vma;
517
Alistair Popple62925032022-01-14 14:09:31 -0800518 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)) &&
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300519 vma->vm_flags & VM_READ)
520 return 0;
521
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800522 /*
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300523 * vma ranges that don't have struct page backing them or map I/O
524 * devices directly cannot be handled by hmm_range_fault().
Jason Gunthorpec2579c92020-03-05 12:00:22 -0400525 *
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800526 * If the vma does not allow read access, then assume that it does not
Jason Gunthorpec2579c92020-03-05 12:00:22 -0400527 * allow write access either. HMM does not support architectures that
528 * allow write without read.
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300529 *
530 * If a fault is requested for an unsupported range then it is a hard
531 * failure.
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800532 */
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300533 if (hmm_range_need_fault(hmm_vma_walk,
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300534 range->hmm_pfns +
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300535 ((start - range->start) >> PAGE_SHIFT),
536 (end - start) >> PAGE_SHIFT, 0))
537 return -EFAULT;
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800538
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300539 hmm_pfns_fill(start, end, range, HMM_PFN_ERROR);
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800540
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300541 /* Skip this vma and continue processing the next vma. */
542 return 1;
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700543}
544
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200545static const struct mm_walk_ops hmm_walk_ops = {
546 .pud_entry = hmm_vma_walk_pud,
547 .pmd_entry = hmm_vma_walk_pmd,
548 .pte_hole = hmm_vma_walk_hole,
549 .hugetlb_entry = hmm_vma_walk_hugetlb_entry,
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800550 .test_walk = hmm_vma_walk_test,
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200551};
552
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700553/**
554 * hmm_range_fault - try to fault some address in a virtual address range
Jason Gunthorpef970b972020-03-27 17:00:15 -0300555 * @range: argument structure
Jérôme Glisse73231612019-05-13 17:19:58 -0700556 *
Jason Gunthorpebe957c82020-05-01 15:20:45 -0300557 * Returns 0 on success or one of the following error codes:
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700558 *
559 * -EINVAL: Invalid arguments or mm or virtual address is in an invalid vma
560 * (e.g., device file vma).
561 * -ENOMEM: Out of memory.
562 * -EPERM: Invalid permission (e.g., asking for write and range is read
563 * only).
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700564 * -EBUSY: The range has been invalidated and the caller needs to wait for
565 * the invalidation to finish.
Jason Gunthorpef970b972020-03-27 17:00:15 -0300566 * -EFAULT: A page was requested to be valid and could not be made valid
567 * ie it has no backing VMA or it is illegal to access
Jérôme Glisse74eee182017-09-08 16:11:35 -0700568 *
Jason Gunthorpef970b972020-03-27 17:00:15 -0300569 * This is similar to get_user_pages(), except that it can read the page tables
570 * without mutating them (ie causing faults).
Jérôme Glisse74eee182017-09-08 16:11:35 -0700571 */
Jason Gunthorpebe957c82020-05-01 15:20:45 -0300572int hmm_range_fault(struct hmm_range *range)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700573{
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800574 struct hmm_vma_walk hmm_vma_walk = {
575 .range = range,
576 .last = range->start,
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800577 };
Jason Gunthorpea22dd502019-11-12 16:22:30 -0400578 struct mm_struct *mm = range->notifier->mm;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700579 int ret;
580
Michel Lespinasse42fc5412020-06-08 21:33:44 -0700581 mmap_assert_locked(mm);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700582
583 do {
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700584 /* If range is no longer valid force retry. */
Jason Gunthorpea22dd502019-11-12 16:22:30 -0400585 if (mmu_interval_check_retry(range->notifier,
586 range->notifier_seq))
Christoph Hellwig2bcbeae2019-07-24 08:52:52 +0200587 return -EBUSY;
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800588 ret = walk_page_range(mm, hmm_vma_walk.last, range->end,
589 &hmm_walk_ops, &hmm_vma_walk);
Jason Gunthorpebe957c82020-05-01 15:20:45 -0300590 /*
591 * When -EBUSY is returned the loop restarts with
592 * hmm_vma_walk.last set to an address that has not been stored
593 * in pfns. All entries < last in the pfn array are set to their
594 * output, and all >= are still at their input values.
595 */
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800596 } while (ret == -EBUSY);
Jason Gunthorpebe957c82020-05-01 15:20:45 -0300597 return ret;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700598}
Jérôme Glisse73231612019-05-13 17:19:58 -0700599EXPORT_SYMBOL(hmm_range_fault);