blob: e9a545751108cd165dd61a4c67589967bf9e8602 [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)
Christoph Hellwig5a0c38d2020-03-16 14:53:10 +010078 if (handle_mm_fault(vma, addr, fault_flags) & VM_FAULT_ERROR)
Jason Gunthorpe53bfe17f2020-03-27 17:00:20 -030079 return -EFAULT;
Christoph Hellwigf8c888a2020-03-16 14:53:09 +010080 return -EBUSY;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -070081}
82
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -030083static unsigned int hmm_pte_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
Jason Gunthorpe2733ea12020-05-01 15:20:48 -030084 unsigned long pfn_req_flags,
85 unsigned long cpu_flags)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -070086{
Jérôme Glissef88a1e92018-04-10 16:29:06 -070087 struct hmm_range *range = hmm_vma_walk->range;
88
Jérôme Glisse023a0192019-05-13 17:20:05 -070089 /*
90 * So we not only consider the individual per page request we also
91 * consider the default flags requested for the range. The API can
Ralph Campbelld2e8d552019-07-25 17:56:45 -070092 * be used 2 ways. The first one where the HMM user coalesces
93 * multiple page faults into one request and sets flags per pfn for
94 * those faults. The second one where the HMM user wants to pre-
Jérôme Glisse023a0192019-05-13 17:20:05 -070095 * fault a range with specific flags. For the latter one it is a
96 * waste to have the user pre-fill the pfn arrays with a default
97 * flags value.
98 */
Jason Gunthorpe2733ea12020-05-01 15:20:48 -030099 pfn_req_flags &= range->pfn_flags_mask;
100 pfn_req_flags |= range->default_flags;
Jérôme Glisse023a0192019-05-13 17:20:05 -0700101
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700102 /* We aren't ask to do anything ... */
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300103 if (!(pfn_req_flags & HMM_PFN_REQ_FAULT))
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300104 return 0;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700105
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700106 /* Need to write fault ? */
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300107 if ((pfn_req_flags & HMM_PFN_REQ_WRITE) &&
108 !(cpu_flags & HMM_PFN_WRITE))
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300109 return HMM_NEED_FAULT | HMM_NEED_WRITE_FAULT;
110
111 /* If CPU page table is not valid then we need to fault */
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300112 if (!(cpu_flags & HMM_PFN_VALID))
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300113 return HMM_NEED_FAULT;
114 return 0;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700115}
116
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300117static unsigned int
118hmm_range_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300119 const unsigned long hmm_pfns[], unsigned long npages,
120 unsigned long cpu_flags)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700121{
Jason Gunthorpe6bfef2f2020-03-27 17:00:16 -0300122 struct hmm_range *range = hmm_vma_walk->range;
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300123 unsigned int required_fault = 0;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700124 unsigned long i;
125
Jason Gunthorpe6bfef2f2020-03-27 17:00:16 -0300126 /*
127 * If the default flags do not request to fault pages, and the mask does
128 * not allow for individual pages to be faulted, then
129 * hmm_pte_need_fault() will always return 0.
130 */
131 if (!((range->default_flags | range->pfn_flags_mask) &
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300132 HMM_PFN_REQ_FAULT))
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300133 return 0;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700134
135 for (i = 0; i < npages; ++i) {
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300136 required_fault |= hmm_pte_need_fault(hmm_vma_walk, hmm_pfns[i],
137 cpu_flags);
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300138 if (required_fault == HMM_NEED_ALL_BITS)
139 return required_fault;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700140 }
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300141 return required_fault;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700142}
143
144static int hmm_vma_walk_hole(unsigned long addr, unsigned long end,
Steven Priceb7a16c72020-02-03 17:36:03 -0800145 __always_unused int depth, struct mm_walk *walk)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700146{
147 struct hmm_vma_walk *hmm_vma_walk = walk->private;
148 struct hmm_range *range = hmm_vma_walk->range;
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300149 unsigned int required_fault;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700150 unsigned long i, npages;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300151 unsigned long *hmm_pfns;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700152
153 i = (addr - range->start) >> PAGE_SHIFT;
154 npages = (end - addr) >> PAGE_SHIFT;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300155 hmm_pfns = &range->hmm_pfns[i];
156 required_fault =
157 hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0);
Jason Gunthorpebd5d3582020-03-27 17:00:21 -0300158 if (!walk->vma) {
159 if (required_fault)
160 return -EFAULT;
161 return hmm_pfns_fill(addr, end, range, HMM_PFN_ERROR);
162 }
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300163 if (required_fault)
164 return hmm_vma_fault(addr, end, required_fault, walk);
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300165 return hmm_pfns_fill(addr, end, range, 0);
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700166}
167
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300168static inline unsigned long pmd_to_hmm_pfn_flags(struct hmm_range *range,
169 pmd_t pmd)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700170{
171 if (pmd_protnone(pmd))
172 return 0;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300173 return pmd_write(pmd) ? (HMM_PFN_VALID | HMM_PFN_WRITE) : HMM_PFN_VALID;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700174}
175
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700176#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Christoph Hellwig9d3973d2019-08-06 19:05:49 +0300177static int hmm_vma_handle_pmd(struct mm_walk *walk, unsigned long addr,
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300178 unsigned long end, unsigned long hmm_pfns[],
179 pmd_t pmd)
Christoph Hellwig9d3973d2019-08-06 19:05:49 +0300180{
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700181 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700182 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700183 unsigned long pfn, npages, i;
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300184 unsigned int required_fault;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300185 unsigned long cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700186
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700187 npages = (end - addr) >> PAGE_SHIFT;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700188 cpu_flags = pmd_to_hmm_pfn_flags(range, pmd);
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300189 required_fault =
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300190 hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, cpu_flags);
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300191 if (required_fault)
192 return hmm_vma_fault(addr, end, required_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700193
Christoph Hellwig309f9a42019-08-06 19:05:47 +0300194 pfn = pmd_pfn(pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
Jason Gunthorpe068354a2020-03-27 17:00:13 -0300195 for (i = 0; addr < end; addr += PAGE_SIZE, i++, pfn++)
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300196 hmm_pfns[i] = pfn | cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700197 return 0;
198}
Christoph Hellwig9d3973d2019-08-06 19:05:49 +0300199#else /* CONFIG_TRANSPARENT_HUGEPAGE */
200/* stub to allow the code below to compile */
201int hmm_vma_handle_pmd(struct mm_walk *walk, unsigned long addr,
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300202 unsigned long end, unsigned long hmm_pfns[], pmd_t pmd);
Christoph Hellwig9d3973d2019-08-06 19:05:49 +0300203#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700204
Christoph Hellwig08ddddd2020-03-16 20:32:16 +0100205static inline bool hmm_is_device_private_entry(struct hmm_range *range,
206 swp_entry_t entry)
207{
208 return is_device_private_entry(entry) &&
209 device_private_entry_to_page(entry)->pgmap->owner ==
210 range->dev_private_owner;
211}
212
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300213static inline unsigned long pte_to_hmm_pfn_flags(struct hmm_range *range,
214 pte_t pte)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700215{
Philip Yang789c2af2019-05-23 16:32:31 -0400216 if (pte_none(pte) || !pte_present(pte) || pte_protnone(pte))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700217 return 0;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300218 return pte_write(pte) ? (HMM_PFN_VALID | HMM_PFN_WRITE) : HMM_PFN_VALID;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700219}
220
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700221static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,
222 unsigned long end, pmd_t *pmdp, pte_t *ptep,
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300223 unsigned long *hmm_pfn)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700224{
225 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700226 struct hmm_range *range = hmm_vma_walk->range;
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300227 unsigned int required_fault;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300228 unsigned long cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700229 pte_t pte = *ptep;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300230 uint64_t pfn_req_flags = *hmm_pfn;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700231
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700232 if (pte_none(pte)) {
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300233 required_fault =
234 hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, 0);
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300235 if (required_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700236 goto fault;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300237 *hmm_pfn = 0;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700238 return 0;
239 }
240
241 if (!pte_present(pte)) {
242 swp_entry_t entry = pte_to_swp_entry(pte);
243
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700244 /*
Christoph Hellwig17ffdc42020-03-16 20:32:15 +0100245 * Never fault in device private pages pages, but just report
246 * the PFN even if not present.
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700247 */
Christoph Hellwig08ddddd2020-03-16 20:32:16 +0100248 if (hmm_is_device_private_entry(range, entry)) {
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300249 cpu_flags = HMM_PFN_VALID;
Christoph Hellwig17ffdc42020-03-16 20:32:15 +0100250 if (is_write_device_private_entry(entry))
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300251 cpu_flags |= HMM_PFN_WRITE;
252 *hmm_pfn = device_private_entry_to_pfn(entry) |
253 cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700254 return 0;
255 }
256
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300257 required_fault =
258 hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, 0);
Jason Gunthorpe846babe2020-03-27 17:00:19 -0300259 if (!required_fault) {
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300260 *hmm_pfn = 0;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700261 return 0;
Jason Gunthorpe846babe2020-03-27 17:00:19 -0300262 }
Jason Gunthorpe76612d62020-02-28 15:52:32 -0400263
264 if (!non_swap_entry(entry))
265 goto fault;
266
267 if (is_migration_entry(entry)) {
268 pte_unmap(ptep);
269 hmm_vma_walk->last = addr;
270 migration_entry_wait(walk->mm, pmdp, addr);
271 return -EBUSY;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700272 }
273
274 /* Report error for everything else */
Jason Gunthorpedfdc2202020-02-28 15:30:37 -0400275 pte_unmap(ptep);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700276 return -EFAULT;
277 }
278
Jason Gunthorpe76612d62020-02-28 15:52:32 -0400279 cpu_flags = pte_to_hmm_pfn_flags(range, pte);
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300280 required_fault =
281 hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, cpu_flags);
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300282 if (required_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700283 goto fault;
284
Jason Gunthorpe40550622020-03-05 14:27:20 -0400285 /*
286 * Since each architecture defines a struct page for the zero page, just
287 * fall through and treat it like a normal page.
288 */
289 if (pte_special(pte) && !is_zero_pfn(pte_pfn(pte))) {
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300290 if (hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, 0)) {
Jason Gunthorpedfdc2202020-02-28 15:30:37 -0400291 pte_unmap(ptep);
Ralph Campbellac541f22019-10-23 12:55:14 -0700292 return -EFAULT;
293 }
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300294 *hmm_pfn = HMM_PFN_ERROR;
Jason Gunthorpe40550622020-03-05 14:27:20 -0400295 return 0;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700296 }
297
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300298 *hmm_pfn = pte_pfn(pte) | cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700299 return 0;
300
301fault:
302 pte_unmap(ptep);
303 /* Fault any virtual address we were asked to fault */
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300304 return hmm_vma_fault(addr, end, required_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700305}
306
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700307static int hmm_vma_walk_pmd(pmd_t *pmdp,
308 unsigned long start,
309 unsigned long end,
310 struct mm_walk *walk)
311{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700312 struct hmm_vma_walk *hmm_vma_walk = walk->private;
313 struct hmm_range *range = hmm_vma_walk->range;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300314 unsigned long *hmm_pfns =
315 &range->hmm_pfns[(start - range->start) >> PAGE_SHIFT];
Jason Gunthorpe2288a9a2020-03-05 15:26:33 -0400316 unsigned long npages = (end - start) >> PAGE_SHIFT;
317 unsigned long addr = start;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700318 pte_t *ptep;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700319 pmd_t pmd;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700320
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700321again:
Jérôme Glissed08faca2018-10-30 15:04:20 -0700322 pmd = READ_ONCE(*pmdp);
323 if (pmd_none(pmd))
Steven Priceb7a16c72020-02-03 17:36:03 -0800324 return hmm_vma_walk_hole(start, end, -1, walk);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700325
Jérôme Glissed08faca2018-10-30 15:04:20 -0700326 if (thp_migration_supported() && is_pmd_migration_entry(pmd)) {
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300327 if (hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0)) {
Jérôme Glissed08faca2018-10-30 15:04:20 -0700328 hmm_vma_walk->last = addr;
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700329 pmd_migration_entry_wait(walk->mm, pmdp);
Jérôme Glisse73231612019-05-13 17:19:58 -0700330 return -EBUSY;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700331 }
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300332 return hmm_pfns_fill(start, end, range, 0);
Jason Gunthorpe2288a9a2020-03-05 15:26:33 -0400333 }
334
335 if (!pmd_present(pmd)) {
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300336 if (hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0))
Jason Gunthorpe2288a9a2020-03-05 15:26:33 -0400337 return -EFAULT;
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800338 return hmm_pfns_fill(start, end, range, HMM_PFN_ERROR);
Jason Gunthorpe2288a9a2020-03-05 15:26:33 -0400339 }
Jérôme Glissed08faca2018-10-30 15:04:20 -0700340
341 if (pmd_devmap(pmd) || pmd_trans_huge(pmd)) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700342 /*
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700343 * No need to take pmd_lock here, even if some other thread
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700344 * is splitting the huge pmd we will get that event through
345 * mmu_notifier callback.
346 *
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700347 * So just read pmd value and check again it's a transparent
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700348 * huge or device mapping one and compute corresponding pfn
349 * values.
350 */
351 pmd = pmd_read_atomic(pmdp);
352 barrier();
353 if (!pmd_devmap(pmd) && !pmd_trans_huge(pmd))
354 goto again;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700355
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300356 return hmm_vma_handle_pmd(walk, addr, end, hmm_pfns, pmd);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700357 }
358
Jérôme Glissed08faca2018-10-30 15:04:20 -0700359 /*
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700360 * We have handled all the valid cases above ie either none, migration,
Jérôme Glissed08faca2018-10-30 15:04:20 -0700361 * huge or transparent huge. At this point either it is a valid pmd
362 * entry pointing to pte directory or it is a bad pmd that will not
363 * recover.
364 */
Jason Gunthorpe2288a9a2020-03-05 15:26:33 -0400365 if (pmd_bad(pmd)) {
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300366 if (hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0))
Jason Gunthorpe2288a9a2020-03-05 15:26:33 -0400367 return -EFAULT;
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800368 return hmm_pfns_fill(start, end, range, HMM_PFN_ERROR);
Jason Gunthorpe2288a9a2020-03-05 15:26:33 -0400369 }
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700370
371 ptep = pte_offset_map(pmdp, addr);
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300372 for (; addr < end; addr += PAGE_SIZE, ptep++, hmm_pfns++) {
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700373 int r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700374
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300375 r = hmm_vma_handle_pte(walk, addr, end, pmdp, ptep, hmm_pfns);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700376 if (r) {
Jason Gunthorpedfdc2202020-02-28 15:30:37 -0400377 /* hmm_vma_handle_pte() did pte_unmap() */
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700378 return r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700379 }
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700380 }
381 pte_unmap(ptep - 1);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700382 return 0;
383}
384
Christoph Hellwigf0b3c452019-08-06 19:05:48 +0300385#if defined(CONFIG_ARCH_HAS_PTE_DEVMAP) && \
386 defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300387static inline unsigned long pud_to_hmm_pfn_flags(struct hmm_range *range,
388 pud_t pud)
Christoph Hellwigf0b3c452019-08-06 19:05:48 +0300389{
390 if (!pud_present(pud))
391 return 0;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300392 return pud_write(pud) ? (HMM_PFN_VALID | HMM_PFN_WRITE) : HMM_PFN_VALID;
Christoph Hellwigf0b3c452019-08-06 19:05:48 +0300393}
394
395static int hmm_vma_walk_pud(pud_t *pudp, unsigned long start, unsigned long end,
396 struct mm_walk *walk)
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700397{
398 struct hmm_vma_walk *hmm_vma_walk = walk->private;
399 struct hmm_range *range = hmm_vma_walk->range;
Steven Price3afc4232020-02-03 17:35:45 -0800400 unsigned long addr = start;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700401 pud_t pud;
Steven Price3afc4232020-02-03 17:35:45 -0800402 int ret = 0;
403 spinlock_t *ptl = pud_trans_huge_lock(pudp, walk->vma);
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700404
Steven Price3afc4232020-02-03 17:35:45 -0800405 if (!ptl)
406 return 0;
407
408 /* Normally we don't want to split the huge page */
409 walk->action = ACTION_CONTINUE;
410
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700411 pud = READ_ONCE(*pudp);
Steven Price3afc4232020-02-03 17:35:45 -0800412 if (pud_none(pud)) {
Jason Gunthorpe05fc1df2020-03-02 15:26:44 -0400413 spin_unlock(ptl);
414 return hmm_vma_walk_hole(start, end, -1, walk);
Steven Price3afc4232020-02-03 17:35:45 -0800415 }
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700416
417 if (pud_huge(pud) && pud_devmap(pud)) {
418 unsigned long i, npages, pfn;
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300419 unsigned int required_fault;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300420 unsigned long *hmm_pfns;
421 unsigned long cpu_flags;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700422
Steven Price3afc4232020-02-03 17:35:45 -0800423 if (!pud_present(pud)) {
Jason Gunthorpe05fc1df2020-03-02 15:26:44 -0400424 spin_unlock(ptl);
425 return hmm_vma_walk_hole(start, end, -1, walk);
Steven Price3afc4232020-02-03 17:35:45 -0800426 }
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700427
428 i = (addr - range->start) >> PAGE_SHIFT;
429 npages = (end - addr) >> PAGE_SHIFT;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300430 hmm_pfns = &range->hmm_pfns[i];
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700431
432 cpu_flags = pud_to_hmm_pfn_flags(range, pud);
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300433 required_fault = hmm_range_need_fault(hmm_vma_walk, hmm_pfns,
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300434 npages, cpu_flags);
435 if (required_fault) {
Jason Gunthorpe05fc1df2020-03-02 15:26:44 -0400436 spin_unlock(ptl);
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300437 return hmm_vma_fault(addr, end, required_fault, walk);
Steven Price3afc4232020-02-03 17:35:45 -0800438 }
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700439
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700440 pfn = pud_pfn(pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
Jason Gunthorpe068354a2020-03-27 17:00:13 -0300441 for (i = 0; i < npages; ++i, ++pfn)
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300442 hmm_pfns[i] = pfn | cpu_flags;
Steven Price3afc4232020-02-03 17:35:45 -0800443 goto out_unlock;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700444 }
445
Steven Price3afc4232020-02-03 17:35:45 -0800446 /* Ask for the PUD to be split */
447 walk->action = ACTION_SUBTREE;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700448
Steven Price3afc4232020-02-03 17:35:45 -0800449out_unlock:
450 spin_unlock(ptl);
451 return ret;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700452}
Christoph Hellwigf0b3c452019-08-06 19:05:48 +0300453#else
454#define hmm_vma_walk_pud NULL
455#endif
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700456
Christoph Hellwig251bbe52019-08-06 19:05:50 +0300457#ifdef CONFIG_HUGETLB_PAGE
Jérôme Glisse63d50662019-05-13 17:20:18 -0700458static int hmm_vma_walk_hugetlb_entry(pte_t *pte, unsigned long hmask,
459 unsigned long start, unsigned long end,
460 struct mm_walk *walk)
461{
Christoph Hellwig05c23af2019-08-06 19:05:46 +0300462 unsigned long addr = start, i, pfn;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700463 struct hmm_vma_walk *hmm_vma_walk = walk->private;
464 struct hmm_range *range = hmm_vma_walk->range;
465 struct vm_area_struct *vma = walk->vma;
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300466 unsigned int required_fault;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300467 unsigned long pfn_req_flags;
468 unsigned long cpu_flags;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700469 spinlock_t *ptl;
470 pte_t entry;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700471
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700472 ptl = huge_pte_lock(hstate_vma(vma), walk->mm, pte);
Jérôme Glisse63d50662019-05-13 17:20:18 -0700473 entry = huge_ptep_get(pte);
474
Christoph Hellwig7f082632019-08-06 19:05:45 +0300475 i = (start - range->start) >> PAGE_SHIFT;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300476 pfn_req_flags = range->hmm_pfns[i];
Jérôme Glisse63d50662019-05-13 17:20:18 -0700477 cpu_flags = pte_to_hmm_pfn_flags(range, entry);
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300478 required_fault =
479 hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, cpu_flags);
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300480 if (required_fault) {
Christoph Hellwig45050692020-03-16 14:53:08 +0100481 spin_unlock(ptl);
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300482 return hmm_vma_fault(addr, end, required_fault, walk);
Jérôme Glisse63d50662019-05-13 17:20:18 -0700483 }
484
Christoph Hellwig05c23af2019-08-06 19:05:46 +0300485 pfn = pte_pfn(entry) + ((start & ~hmask) >> PAGE_SHIFT);
Christoph Hellwig7f082632019-08-06 19:05:45 +0300486 for (; addr < end; addr += PAGE_SIZE, i++, pfn++)
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300487 range->hmm_pfns[i] = pfn | cpu_flags;
488
Jérôme Glisse63d50662019-05-13 17:20:18 -0700489 spin_unlock(ptl);
Christoph Hellwig45050692020-03-16 14:53:08 +0100490 return 0;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700491}
Christoph Hellwig251bbe52019-08-06 19:05:50 +0300492#else
493#define hmm_vma_walk_hugetlb_entry NULL
494#endif /* CONFIG_HUGETLB_PAGE */
Jérôme Glisse63d50662019-05-13 17:20:18 -0700495
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800496static int hmm_vma_walk_test(unsigned long start, unsigned long end,
497 struct mm_walk *walk)
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700498{
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800499 struct hmm_vma_walk *hmm_vma_walk = walk->private;
500 struct hmm_range *range = hmm_vma_walk->range;
501 struct vm_area_struct *vma = walk->vma;
502
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300503 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP | VM_MIXEDMAP)) &&
504 vma->vm_flags & VM_READ)
505 return 0;
506
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800507 /*
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300508 * vma ranges that don't have struct page backing them or map I/O
509 * devices directly cannot be handled by hmm_range_fault().
Jason Gunthorpec2579c92020-03-05 12:00:22 -0400510 *
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800511 * If the vma does not allow read access, then assume that it does not
Jason Gunthorpec2579c92020-03-05 12:00:22 -0400512 * allow write access either. HMM does not support architectures that
513 * allow write without read.
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300514 *
515 * If a fault is requested for an unsupported range then it is a hard
516 * failure.
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800517 */
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300518 if (hmm_range_need_fault(hmm_vma_walk,
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300519 range->hmm_pfns +
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300520 ((start - range->start) >> PAGE_SHIFT),
521 (end - start) >> PAGE_SHIFT, 0))
522 return -EFAULT;
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800523
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300524 hmm_pfns_fill(start, end, range, HMM_PFN_ERROR);
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800525
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300526 /* Skip this vma and continue processing the next vma. */
527 return 1;
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700528}
529
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200530static const struct mm_walk_ops hmm_walk_ops = {
531 .pud_entry = hmm_vma_walk_pud,
532 .pmd_entry = hmm_vma_walk_pmd,
533 .pte_hole = hmm_vma_walk_hole,
534 .hugetlb_entry = hmm_vma_walk_hugetlb_entry,
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800535 .test_walk = hmm_vma_walk_test,
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200536};
537
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700538/**
539 * hmm_range_fault - try to fault some address in a virtual address range
Jason Gunthorpef970b972020-03-27 17:00:15 -0300540 * @range: argument structure
Jérôme Glisse73231612019-05-13 17:19:58 -0700541 *
Jason Gunthorpebe957c82020-05-01 15:20:45 -0300542 * Returns 0 on success or one of the following error codes:
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700543 *
544 * -EINVAL: Invalid arguments or mm or virtual address is in an invalid vma
545 * (e.g., device file vma).
546 * -ENOMEM: Out of memory.
547 * -EPERM: Invalid permission (e.g., asking for write and range is read
548 * only).
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700549 * -EBUSY: The range has been invalidated and the caller needs to wait for
550 * the invalidation to finish.
Jason Gunthorpef970b972020-03-27 17:00:15 -0300551 * -EFAULT: A page was requested to be valid and could not be made valid
552 * ie it has no backing VMA or it is illegal to access
Jérôme Glisse74eee182017-09-08 16:11:35 -0700553 *
Jason Gunthorpef970b972020-03-27 17:00:15 -0300554 * This is similar to get_user_pages(), except that it can read the page tables
555 * without mutating them (ie causing faults).
Jérôme Glisse74eee182017-09-08 16:11:35 -0700556 */
Jason Gunthorpebe957c82020-05-01 15:20:45 -0300557int hmm_range_fault(struct hmm_range *range)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700558{
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800559 struct hmm_vma_walk hmm_vma_walk = {
560 .range = range,
561 .last = range->start,
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800562 };
Jason Gunthorpea22dd502019-11-12 16:22:30 -0400563 struct mm_struct *mm = range->notifier->mm;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700564 int ret;
565
Michel Lespinasse42fc5412020-06-08 21:33:44 -0700566 mmap_assert_locked(mm);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700567
568 do {
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700569 /* If range is no longer valid force retry. */
Jason Gunthorpea22dd502019-11-12 16:22:30 -0400570 if (mmu_interval_check_retry(range->notifier,
571 range->notifier_seq))
Christoph Hellwig2bcbeae2019-07-24 08:52:52 +0200572 return -EBUSY;
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800573 ret = walk_page_range(mm, hmm_vma_walk.last, range->end,
574 &hmm_walk_ops, &hmm_vma_walk);
Jason Gunthorpebe957c82020-05-01 15:20:45 -0300575 /*
576 * When -EBUSY is returned the loop restarts with
577 * hmm_vma_walk.last set to an address that has not been stored
578 * in pfns. All entries < last in the pfn array are set to their
579 * output, and all >= are still at their input values.
580 */
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800581 } while (ret == -EBUSY);
Jason Gunthorpebe957c82020-05-01 15:20:45 -0300582 return ret;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700583}
Jérôme Glisse73231612019-05-13 17:19:58 -0700584EXPORT_SYMBOL(hmm_range_fault);