blob: 842e265992380c801d9f83d8749eca50251aa140 [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
Alistair Poppleb756a3b2021-06-30 18:54:25 -070029#include "internal.h"
30
Jérôme Glisse74eee182017-09-08 16:11:35 -070031struct hmm_vma_walk {
32 struct hmm_range *range;
33 unsigned long last;
Jérôme Glisse74eee182017-09-08 16:11:35 -070034};
35
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -030036enum {
37 HMM_NEED_FAULT = 1 << 0,
38 HMM_NEED_WRITE_FAULT = 1 << 1,
39 HMM_NEED_ALL_BITS = HMM_NEED_FAULT | HMM_NEED_WRITE_FAULT,
40};
41
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -080042static int hmm_pfns_fill(unsigned long addr, unsigned long end,
Jason Gunthorpe2733ea12020-05-01 15:20:48 -030043 struct hmm_range *range, unsigned long cpu_flags)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070044{
Jason Gunthorpe2733ea12020-05-01 15:20:48 -030045 unsigned long i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070046
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070047 for (; addr < end; addr += PAGE_SIZE, i++)
Jason Gunthorpe2733ea12020-05-01 15:20:48 -030048 range->hmm_pfns[i] = cpu_flags;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070049 return 0;
50}
51
Jérôme Glisse5504ed22018-04-10 16:28:46 -070052/*
Christoph Hellwigf8c888a2020-03-16 14:53:09 +010053 * hmm_vma_fault() - fault in a range lacking valid pmd or pte(s)
Ralph Campbelld2e8d552019-07-25 17:56:45 -070054 * @addr: range virtual start address (inclusive)
Jérôme Glisse5504ed22018-04-10 16:28:46 -070055 * @end: range virtual end address (exclusive)
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -030056 * @required_fault: HMM_NEED_* flags
Jérôme Glisse5504ed22018-04-10 16:28:46 -070057 * @walk: mm_walk structure
Christoph Hellwigf8c888a2020-03-16 14:53:09 +010058 * Return: -EBUSY after page fault, or page fault error
Jérôme Glisse5504ed22018-04-10 16:28:46 -070059 *
60 * This function will be called whenever pmd_none() or pte_none() returns true,
61 * or whenever there is no page directory covering the virtual address range.
62 */
Christoph Hellwigf8c888a2020-03-16 14:53:09 +010063static int hmm_vma_fault(unsigned long addr, unsigned long end,
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -030064 unsigned int required_fault, struct mm_walk *walk)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070065{
Jérôme Glisse74eee182017-09-08 16:11:35 -070066 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Christoph Hellwig5a0c38d2020-03-16 14:53:10 +010067 struct vm_area_struct *vma = walk->vma;
Christoph Hellwig5a0c38d2020-03-16 14:53:10 +010068 unsigned int fault_flags = FAULT_FLAG_REMOTE;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070069
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -030070 WARN_ON_ONCE(!required_fault);
Jérôme Glisse74eee182017-09-08 16:11:35 -070071 hmm_vma_walk->last = addr;
Jérôme Glisse63d50662019-05-13 17:20:18 -070072
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -030073 if (required_fault & HMM_NEED_WRITE_FAULT) {
Christoph Hellwig5a0c38d2020-03-16 14:53:10 +010074 if (!(vma->vm_flags & VM_WRITE))
75 return -EPERM;
76 fault_flags |= FAULT_FLAG_WRITE;
Jérôme Glisse74eee182017-09-08 16:11:35 -070077 }
78
Jason Gunthorpe53bfe17f2020-03-27 17:00:20 -030079 for (; addr < end; addr += PAGE_SIZE)
Peter Xubce617e2020-08-11 18:37:44 -070080 if (handle_mm_fault(vma, addr, fault_flags, NULL) &
81 VM_FAULT_ERROR)
Jason Gunthorpe53bfe17f2020-03-27 17:00:20 -030082 return -EFAULT;
Christoph Hellwigf8c888a2020-03-16 14:53:09 +010083 return -EBUSY;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -070084}
85
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -030086static unsigned int hmm_pte_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
Jason Gunthorpe2733ea12020-05-01 15:20:48 -030087 unsigned long pfn_req_flags,
88 unsigned long cpu_flags)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -070089{
Jérôme Glissef88a1e92018-04-10 16:29:06 -070090 struct hmm_range *range = hmm_vma_walk->range;
91
Jérôme Glisse023a0192019-05-13 17:20:05 -070092 /*
93 * So we not only consider the individual per page request we also
94 * consider the default flags requested for the range. The API can
Ralph Campbelld2e8d552019-07-25 17:56:45 -070095 * be used 2 ways. The first one where the HMM user coalesces
96 * multiple page faults into one request and sets flags per pfn for
97 * those faults. The second one where the HMM user wants to pre-
Jérôme Glisse023a0192019-05-13 17:20:05 -070098 * fault a range with specific flags. For the latter one it is a
99 * waste to have the user pre-fill the pfn arrays with a default
100 * flags value.
101 */
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300102 pfn_req_flags &= range->pfn_flags_mask;
103 pfn_req_flags |= range->default_flags;
Jérôme Glisse023a0192019-05-13 17:20:05 -0700104
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700105 /* We aren't ask to do anything ... */
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300106 if (!(pfn_req_flags & HMM_PFN_REQ_FAULT))
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300107 return 0;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700108
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700109 /* Need to write fault ? */
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300110 if ((pfn_req_flags & HMM_PFN_REQ_WRITE) &&
111 !(cpu_flags & HMM_PFN_WRITE))
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300112 return HMM_NEED_FAULT | HMM_NEED_WRITE_FAULT;
113
114 /* If CPU page table is not valid then we need to fault */
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300115 if (!(cpu_flags & HMM_PFN_VALID))
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300116 return HMM_NEED_FAULT;
117 return 0;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700118}
119
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300120static unsigned int
121hmm_range_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300122 const unsigned long hmm_pfns[], unsigned long npages,
123 unsigned long cpu_flags)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700124{
Jason Gunthorpe6bfef2f2020-03-27 17:00:16 -0300125 struct hmm_range *range = hmm_vma_walk->range;
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300126 unsigned int required_fault = 0;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700127 unsigned long i;
128
Jason Gunthorpe6bfef2f2020-03-27 17:00:16 -0300129 /*
130 * If the default flags do not request to fault pages, and the mask does
131 * not allow for individual pages to be faulted, then
132 * hmm_pte_need_fault() will always return 0.
133 */
134 if (!((range->default_flags | range->pfn_flags_mask) &
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300135 HMM_PFN_REQ_FAULT))
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300136 return 0;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700137
138 for (i = 0; i < npages; ++i) {
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300139 required_fault |= hmm_pte_need_fault(hmm_vma_walk, hmm_pfns[i],
140 cpu_flags);
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300141 if (required_fault == HMM_NEED_ALL_BITS)
142 return required_fault;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700143 }
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300144 return required_fault;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700145}
146
147static int hmm_vma_walk_hole(unsigned long addr, unsigned long end,
Steven Priceb7a16c72020-02-03 17:36:03 -0800148 __always_unused int depth, struct mm_walk *walk)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700149{
150 struct hmm_vma_walk *hmm_vma_walk = walk->private;
151 struct hmm_range *range = hmm_vma_walk->range;
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300152 unsigned int required_fault;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700153 unsigned long i, npages;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300154 unsigned long *hmm_pfns;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700155
156 i = (addr - range->start) >> PAGE_SHIFT;
157 npages = (end - addr) >> PAGE_SHIFT;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300158 hmm_pfns = &range->hmm_pfns[i];
159 required_fault =
160 hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0);
Jason Gunthorpebd5d3582020-03-27 17:00:21 -0300161 if (!walk->vma) {
162 if (required_fault)
163 return -EFAULT;
164 return hmm_pfns_fill(addr, end, range, HMM_PFN_ERROR);
165 }
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300166 if (required_fault)
167 return hmm_vma_fault(addr, end, required_fault, walk);
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300168 return hmm_pfns_fill(addr, end, range, 0);
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700169}
170
Ralph Campbell3b50a6e2020-07-01 15:53:49 -0700171static inline unsigned long hmm_pfn_flags_order(unsigned long order)
172{
173 return order << HMM_PFN_ORDER_SHIFT;
174}
175
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300176static inline unsigned long pmd_to_hmm_pfn_flags(struct hmm_range *range,
177 pmd_t pmd)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700178{
179 if (pmd_protnone(pmd))
180 return 0;
Ralph Campbell3b50a6e2020-07-01 15:53:49 -0700181 return (pmd_write(pmd) ? (HMM_PFN_VALID | HMM_PFN_WRITE) :
182 HMM_PFN_VALID) |
183 hmm_pfn_flags_order(PMD_SHIFT - PAGE_SHIFT);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700184}
185
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700186#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Christoph Hellwig9d3973d2019-08-06 19:05:49 +0300187static int hmm_vma_handle_pmd(struct mm_walk *walk, unsigned long addr,
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300188 unsigned long end, unsigned long hmm_pfns[],
189 pmd_t pmd)
Christoph Hellwig9d3973d2019-08-06 19:05:49 +0300190{
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700191 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700192 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700193 unsigned long pfn, npages, i;
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300194 unsigned int required_fault;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300195 unsigned long cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700196
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700197 npages = (end - addr) >> PAGE_SHIFT;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700198 cpu_flags = pmd_to_hmm_pfn_flags(range, pmd);
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300199 required_fault =
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300200 hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, cpu_flags);
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300201 if (required_fault)
202 return hmm_vma_fault(addr, end, required_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700203
Christoph Hellwig309f9a42019-08-06 19:05:47 +0300204 pfn = pmd_pfn(pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
Jason Gunthorpe068354a2020-03-27 17:00:13 -0300205 for (i = 0; addr < end; addr += PAGE_SIZE, i++, pfn++)
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300206 hmm_pfns[i] = pfn | cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700207 return 0;
208}
Christoph Hellwig9d3973d2019-08-06 19:05:49 +0300209#else /* CONFIG_TRANSPARENT_HUGEPAGE */
210/* stub to allow the code below to compile */
211int hmm_vma_handle_pmd(struct mm_walk *walk, unsigned long addr,
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300212 unsigned long end, unsigned long hmm_pfns[], pmd_t pmd);
Christoph Hellwig9d3973d2019-08-06 19:05:49 +0300213#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700214
Christoph Hellwig08ddddd2020-03-16 20:32:16 +0100215static inline bool hmm_is_device_private_entry(struct hmm_range *range,
216 swp_entry_t entry)
217{
218 return is_device_private_entry(entry) &&
Alistair Poppleaf5cdaf2021-06-30 18:54:06 -0700219 pfn_swap_entry_to_page(entry)->pgmap->owner ==
Christoph Hellwig08ddddd2020-03-16 20:32:16 +0100220 range->dev_private_owner;
221}
222
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300223static inline unsigned long pte_to_hmm_pfn_flags(struct hmm_range *range,
224 pte_t pte)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700225{
Philip Yang789c2af2019-05-23 16:32:31 -0400226 if (pte_none(pte) || !pte_present(pte) || pte_protnone(pte))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700227 return 0;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300228 return pte_write(pte) ? (HMM_PFN_VALID | HMM_PFN_WRITE) : HMM_PFN_VALID;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700229}
230
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700231static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,
232 unsigned long end, pmd_t *pmdp, pte_t *ptep,
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300233 unsigned long *hmm_pfn)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700234{
235 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700236 struct hmm_range *range = hmm_vma_walk->range;
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300237 unsigned int required_fault;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300238 unsigned long cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700239 pte_t pte = *ptep;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300240 uint64_t pfn_req_flags = *hmm_pfn;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700241
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700242 if (pte_none(pte)) {
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300243 required_fault =
244 hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, 0);
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300245 if (required_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700246 goto fault;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300247 *hmm_pfn = 0;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700248 return 0;
249 }
250
251 if (!pte_present(pte)) {
252 swp_entry_t entry = pte_to_swp_entry(pte);
253
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700254 /*
Randy Dunlap0cb80a22020-08-11 18:32:56 -0700255 * Never fault in device private pages, but just report
Christoph Hellwig17ffdc42020-03-16 20:32:15 +0100256 * the PFN even if not present.
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700257 */
Christoph Hellwig08ddddd2020-03-16 20:32:16 +0100258 if (hmm_is_device_private_entry(range, entry)) {
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300259 cpu_flags = HMM_PFN_VALID;
Alistair Popple4dd845b2021-06-30 18:54:09 -0700260 if (is_writable_device_private_entry(entry))
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300261 cpu_flags |= HMM_PFN_WRITE;
Alistair Poppleaf5cdaf2021-06-30 18:54:06 -0700262 *hmm_pfn = swp_offset(entry) | cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700263 return 0;
264 }
265
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300266 required_fault =
267 hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, 0);
Jason Gunthorpe846babe2020-03-27 17:00:19 -0300268 if (!required_fault) {
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300269 *hmm_pfn = 0;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700270 return 0;
Jason Gunthorpe846babe2020-03-27 17:00:19 -0300271 }
Jason Gunthorpe76612d62020-02-28 15:52:32 -0400272
273 if (!non_swap_entry(entry))
274 goto fault;
275
Alistair Poppleb756a3b2021-06-30 18:54:25 -0700276 if (is_device_exclusive_entry(entry))
277 goto fault;
278
Jason Gunthorpe76612d62020-02-28 15:52:32 -0400279 if (is_migration_entry(entry)) {
280 pte_unmap(ptep);
281 hmm_vma_walk->last = addr;
282 migration_entry_wait(walk->mm, pmdp, addr);
283 return -EBUSY;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700284 }
285
286 /* Report error for everything else */
Jason Gunthorpedfdc2202020-02-28 15:30:37 -0400287 pte_unmap(ptep);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700288 return -EFAULT;
289 }
290
Jason Gunthorpe76612d62020-02-28 15:52:32 -0400291 cpu_flags = pte_to_hmm_pfn_flags(range, pte);
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300292 required_fault =
293 hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, cpu_flags);
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300294 if (required_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700295 goto fault;
296
Jason Gunthorpe40550622020-03-05 14:27:20 -0400297 /*
Li Zhijian4b42fb22021-09-08 18:10:02 -0700298 * Bypass devmap pte such as DAX page when all pfn requested
299 * flags(pfn_req_flags) are fulfilled.
Jason Gunthorpe40550622020-03-05 14:27:20 -0400300 * Since each architecture defines a struct page for the zero page, just
301 * fall through and treat it like a normal page.
302 */
Li Zhijian4b42fb22021-09-08 18:10:02 -0700303 if (pte_special(pte) && !pte_devmap(pte) &&
304 !is_zero_pfn(pte_pfn(pte))) {
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300305 if (hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, 0)) {
Jason Gunthorpedfdc2202020-02-28 15:30:37 -0400306 pte_unmap(ptep);
Ralph Campbellac541f22019-10-23 12:55:14 -0700307 return -EFAULT;
308 }
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300309 *hmm_pfn = HMM_PFN_ERROR;
Jason Gunthorpe40550622020-03-05 14:27:20 -0400310 return 0;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700311 }
312
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300313 *hmm_pfn = pte_pfn(pte) | cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700314 return 0;
315
316fault:
317 pte_unmap(ptep);
318 /* Fault any virtual address we were asked to fault */
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300319 return hmm_vma_fault(addr, end, required_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700320}
321
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700322static int hmm_vma_walk_pmd(pmd_t *pmdp,
323 unsigned long start,
324 unsigned long end,
325 struct mm_walk *walk)
326{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700327 struct hmm_vma_walk *hmm_vma_walk = walk->private;
328 struct hmm_range *range = hmm_vma_walk->range;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300329 unsigned long *hmm_pfns =
330 &range->hmm_pfns[(start - range->start) >> PAGE_SHIFT];
Jason Gunthorpe2288a9a2020-03-05 15:26:33 -0400331 unsigned long npages = (end - start) >> PAGE_SHIFT;
332 unsigned long addr = start;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700333 pte_t *ptep;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700334 pmd_t pmd;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700335
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700336again:
Jérôme Glissed08faca2018-10-30 15:04:20 -0700337 pmd = READ_ONCE(*pmdp);
338 if (pmd_none(pmd))
Steven Priceb7a16c72020-02-03 17:36:03 -0800339 return hmm_vma_walk_hole(start, end, -1, walk);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700340
Jérôme Glissed08faca2018-10-30 15:04:20 -0700341 if (thp_migration_supported() && is_pmd_migration_entry(pmd)) {
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300342 if (hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0)) {
Jérôme Glissed08faca2018-10-30 15:04:20 -0700343 hmm_vma_walk->last = addr;
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700344 pmd_migration_entry_wait(walk->mm, pmdp);
Jérôme Glisse73231612019-05-13 17:19:58 -0700345 return -EBUSY;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700346 }
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300347 return hmm_pfns_fill(start, end, range, 0);
Jason Gunthorpe2288a9a2020-03-05 15:26:33 -0400348 }
349
350 if (!pmd_present(pmd)) {
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300351 if (hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0))
Jason Gunthorpe2288a9a2020-03-05 15:26:33 -0400352 return -EFAULT;
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800353 return hmm_pfns_fill(start, end, range, HMM_PFN_ERROR);
Jason Gunthorpe2288a9a2020-03-05 15:26:33 -0400354 }
Jérôme Glissed08faca2018-10-30 15:04:20 -0700355
356 if (pmd_devmap(pmd) || pmd_trans_huge(pmd)) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700357 /*
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700358 * No need to take pmd_lock here, even if some other thread
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700359 * is splitting the huge pmd we will get that event through
360 * mmu_notifier callback.
361 *
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700362 * So just read pmd value and check again it's a transparent
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700363 * huge or device mapping one and compute corresponding pfn
364 * values.
365 */
366 pmd = pmd_read_atomic(pmdp);
367 barrier();
368 if (!pmd_devmap(pmd) && !pmd_trans_huge(pmd))
369 goto again;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700370
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300371 return hmm_vma_handle_pmd(walk, addr, end, hmm_pfns, pmd);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700372 }
373
Jérôme Glissed08faca2018-10-30 15:04:20 -0700374 /*
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700375 * We have handled all the valid cases above ie either none, migration,
Jérôme Glissed08faca2018-10-30 15:04:20 -0700376 * huge or transparent huge. At this point either it is a valid pmd
377 * entry pointing to pte directory or it is a bad pmd that will not
378 * recover.
379 */
Jason Gunthorpe2288a9a2020-03-05 15:26:33 -0400380 if (pmd_bad(pmd)) {
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300381 if (hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0))
Jason Gunthorpe2288a9a2020-03-05 15:26:33 -0400382 return -EFAULT;
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800383 return hmm_pfns_fill(start, end, range, HMM_PFN_ERROR);
Jason Gunthorpe2288a9a2020-03-05 15:26:33 -0400384 }
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700385
386 ptep = pte_offset_map(pmdp, addr);
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300387 for (; addr < end; addr += PAGE_SIZE, ptep++, hmm_pfns++) {
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700388 int r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700389
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300390 r = hmm_vma_handle_pte(walk, addr, end, pmdp, ptep, hmm_pfns);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700391 if (r) {
Jason Gunthorpedfdc2202020-02-28 15:30:37 -0400392 /* hmm_vma_handle_pte() did pte_unmap() */
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700393 return r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700394 }
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700395 }
396 pte_unmap(ptep - 1);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700397 return 0;
398}
399
Christoph Hellwigf0b3c452019-08-06 19:05:48 +0300400#if defined(CONFIG_ARCH_HAS_PTE_DEVMAP) && \
401 defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300402static inline unsigned long pud_to_hmm_pfn_flags(struct hmm_range *range,
403 pud_t pud)
Christoph Hellwigf0b3c452019-08-06 19:05:48 +0300404{
405 if (!pud_present(pud))
406 return 0;
Ralph Campbell3b50a6e2020-07-01 15:53:49 -0700407 return (pud_write(pud) ? (HMM_PFN_VALID | HMM_PFN_WRITE) :
408 HMM_PFN_VALID) |
409 hmm_pfn_flags_order(PUD_SHIFT - PAGE_SHIFT);
Christoph Hellwigf0b3c452019-08-06 19:05:48 +0300410}
411
412static int hmm_vma_walk_pud(pud_t *pudp, unsigned long start, unsigned long end,
413 struct mm_walk *walk)
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700414{
415 struct hmm_vma_walk *hmm_vma_walk = walk->private;
416 struct hmm_range *range = hmm_vma_walk->range;
Steven Price3afc4232020-02-03 17:35:45 -0800417 unsigned long addr = start;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700418 pud_t pud;
Steven Price3afc4232020-02-03 17:35:45 -0800419 int ret = 0;
420 spinlock_t *ptl = pud_trans_huge_lock(pudp, walk->vma);
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700421
Steven Price3afc4232020-02-03 17:35:45 -0800422 if (!ptl)
423 return 0;
424
425 /* Normally we don't want to split the huge page */
426 walk->action = ACTION_CONTINUE;
427
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700428 pud = READ_ONCE(*pudp);
Steven Price3afc4232020-02-03 17:35:45 -0800429 if (pud_none(pud)) {
Jason Gunthorpe05fc1df2020-03-02 15:26:44 -0400430 spin_unlock(ptl);
431 return hmm_vma_walk_hole(start, end, -1, walk);
Steven Price3afc4232020-02-03 17:35:45 -0800432 }
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700433
434 if (pud_huge(pud) && pud_devmap(pud)) {
435 unsigned long i, npages, pfn;
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300436 unsigned int required_fault;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300437 unsigned long *hmm_pfns;
438 unsigned long cpu_flags;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700439
Steven Price3afc4232020-02-03 17:35:45 -0800440 if (!pud_present(pud)) {
Jason Gunthorpe05fc1df2020-03-02 15:26:44 -0400441 spin_unlock(ptl);
442 return hmm_vma_walk_hole(start, end, -1, walk);
Steven Price3afc4232020-02-03 17:35:45 -0800443 }
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700444
445 i = (addr - range->start) >> PAGE_SHIFT;
446 npages = (end - addr) >> PAGE_SHIFT;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300447 hmm_pfns = &range->hmm_pfns[i];
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700448
449 cpu_flags = pud_to_hmm_pfn_flags(range, pud);
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300450 required_fault = hmm_range_need_fault(hmm_vma_walk, hmm_pfns,
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300451 npages, cpu_flags);
452 if (required_fault) {
Jason Gunthorpe05fc1df2020-03-02 15:26:44 -0400453 spin_unlock(ptl);
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300454 return hmm_vma_fault(addr, end, required_fault, walk);
Steven Price3afc4232020-02-03 17:35:45 -0800455 }
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700456
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700457 pfn = pud_pfn(pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
Jason Gunthorpe068354a2020-03-27 17:00:13 -0300458 for (i = 0; i < npages; ++i, ++pfn)
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300459 hmm_pfns[i] = pfn | cpu_flags;
Steven Price3afc4232020-02-03 17:35:45 -0800460 goto out_unlock;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700461 }
462
Steven Price3afc4232020-02-03 17:35:45 -0800463 /* Ask for the PUD to be split */
464 walk->action = ACTION_SUBTREE;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700465
Steven Price3afc4232020-02-03 17:35:45 -0800466out_unlock:
467 spin_unlock(ptl);
468 return ret;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700469}
Christoph Hellwigf0b3c452019-08-06 19:05:48 +0300470#else
471#define hmm_vma_walk_pud NULL
472#endif
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700473
Christoph Hellwig251bbe52019-08-06 19:05:50 +0300474#ifdef CONFIG_HUGETLB_PAGE
Jérôme Glisse63d50662019-05-13 17:20:18 -0700475static int hmm_vma_walk_hugetlb_entry(pte_t *pte, unsigned long hmask,
476 unsigned long start, unsigned long end,
477 struct mm_walk *walk)
478{
Christoph Hellwig05c23af2019-08-06 19:05:46 +0300479 unsigned long addr = start, i, pfn;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700480 struct hmm_vma_walk *hmm_vma_walk = walk->private;
481 struct hmm_range *range = hmm_vma_walk->range;
482 struct vm_area_struct *vma = walk->vma;
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300483 unsigned int required_fault;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300484 unsigned long pfn_req_flags;
485 unsigned long cpu_flags;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700486 spinlock_t *ptl;
487 pte_t entry;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700488
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700489 ptl = huge_pte_lock(hstate_vma(vma), walk->mm, pte);
Jérôme Glisse63d50662019-05-13 17:20:18 -0700490 entry = huge_ptep_get(pte);
491
Christoph Hellwig7f082632019-08-06 19:05:45 +0300492 i = (start - range->start) >> PAGE_SHIFT;
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300493 pfn_req_flags = range->hmm_pfns[i];
Ralph Campbell3b50a6e2020-07-01 15:53:49 -0700494 cpu_flags = pte_to_hmm_pfn_flags(range, entry) |
495 hmm_pfn_flags_order(huge_page_order(hstate_vma(vma)));
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300496 required_fault =
497 hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, cpu_flags);
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300498 if (required_fault) {
Christoph Hellwig45050692020-03-16 14:53:08 +0100499 spin_unlock(ptl);
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300500 return hmm_vma_fault(addr, end, required_fault, walk);
Jérôme Glisse63d50662019-05-13 17:20:18 -0700501 }
502
Christoph Hellwig05c23af2019-08-06 19:05:46 +0300503 pfn = pte_pfn(entry) + ((start & ~hmask) >> PAGE_SHIFT);
Christoph Hellwig7f082632019-08-06 19:05:45 +0300504 for (; addr < end; addr += PAGE_SIZE, i++, pfn++)
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300505 range->hmm_pfns[i] = pfn | cpu_flags;
506
Jérôme Glisse63d50662019-05-13 17:20:18 -0700507 spin_unlock(ptl);
Christoph Hellwig45050692020-03-16 14:53:08 +0100508 return 0;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700509}
Christoph Hellwig251bbe52019-08-06 19:05:50 +0300510#else
511#define hmm_vma_walk_hugetlb_entry NULL
512#endif /* CONFIG_HUGETLB_PAGE */
Jérôme Glisse63d50662019-05-13 17:20:18 -0700513
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800514static int hmm_vma_walk_test(unsigned long start, unsigned long end,
515 struct mm_walk *walk)
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700516{
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800517 struct hmm_vma_walk *hmm_vma_walk = walk->private;
518 struct hmm_range *range = hmm_vma_walk->range;
519 struct vm_area_struct *vma = walk->vma;
520
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300521 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP | VM_MIXEDMAP)) &&
522 vma->vm_flags & VM_READ)
523 return 0;
524
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800525 /*
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300526 * vma ranges that don't have struct page backing them or map I/O
527 * devices directly cannot be handled by hmm_range_fault().
Jason Gunthorpec2579c92020-03-05 12:00:22 -0400528 *
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800529 * If the vma does not allow read access, then assume that it does not
Jason Gunthorpec2579c92020-03-05 12:00:22 -0400530 * allow write access either. HMM does not support architectures that
531 * allow write without read.
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300532 *
533 * If a fault is requested for an unsupported range then it is a hard
534 * failure.
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800535 */
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300536 if (hmm_range_need_fault(hmm_vma_walk,
Jason Gunthorpe2733ea12020-05-01 15:20:48 -0300537 range->hmm_pfns +
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300538 ((start - range->start) >> PAGE_SHIFT),
539 (end - start) >> PAGE_SHIFT, 0))
540 return -EFAULT;
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800541
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300542 hmm_pfns_fill(start, end, range, HMM_PFN_ERROR);
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800543
Jason Gunthorpea3eb13c2020-03-27 17:00:14 -0300544 /* Skip this vma and continue processing the next vma. */
545 return 1;
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700546}
547
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200548static const struct mm_walk_ops hmm_walk_ops = {
549 .pud_entry = hmm_vma_walk_pud,
550 .pmd_entry = hmm_vma_walk_pmd,
551 .pte_hole = hmm_vma_walk_hole,
552 .hugetlb_entry = hmm_vma_walk_hugetlb_entry,
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800553 .test_walk = hmm_vma_walk_test,
Christoph Hellwig7b86ac32019-08-28 16:19:54 +0200554};
555
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700556/**
557 * hmm_range_fault - try to fault some address in a virtual address range
Jason Gunthorpef970b972020-03-27 17:00:15 -0300558 * @range: argument structure
Jérôme Glisse73231612019-05-13 17:19:58 -0700559 *
Jason Gunthorpebe957c82020-05-01 15:20:45 -0300560 * Returns 0 on success or one of the following error codes:
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700561 *
562 * -EINVAL: Invalid arguments or mm or virtual address is in an invalid vma
563 * (e.g., device file vma).
564 * -ENOMEM: Out of memory.
565 * -EPERM: Invalid permission (e.g., asking for write and range is read
566 * only).
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700567 * -EBUSY: The range has been invalidated and the caller needs to wait for
568 * the invalidation to finish.
Jason Gunthorpef970b972020-03-27 17:00:15 -0300569 * -EFAULT: A page was requested to be valid and could not be made valid
570 * ie it has no backing VMA or it is illegal to access
Jérôme Glisse74eee182017-09-08 16:11:35 -0700571 *
Jason Gunthorpef970b972020-03-27 17:00:15 -0300572 * This is similar to get_user_pages(), except that it can read the page tables
573 * without mutating them (ie causing faults).
Jérôme Glisse74eee182017-09-08 16:11:35 -0700574 */
Jason Gunthorpebe957c82020-05-01 15:20:45 -0300575int hmm_range_fault(struct hmm_range *range)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700576{
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800577 struct hmm_vma_walk hmm_vma_walk = {
578 .range = range,
579 .last = range->start,
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800580 };
Jason Gunthorpea22dd502019-11-12 16:22:30 -0400581 struct mm_struct *mm = range->notifier->mm;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700582 int ret;
583
Michel Lespinasse42fc5412020-06-08 21:33:44 -0700584 mmap_assert_locked(mm);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700585
586 do {
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700587 /* If range is no longer valid force retry. */
Jason Gunthorpea22dd502019-11-12 16:22:30 -0400588 if (mmu_interval_check_retry(range->notifier,
589 range->notifier_seq))
Christoph Hellwig2bcbeae2019-07-24 08:52:52 +0200590 return -EBUSY;
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800591 ret = walk_page_range(mm, hmm_vma_walk.last, range->end,
592 &hmm_walk_ops, &hmm_vma_walk);
Jason Gunthorpebe957c82020-05-01 15:20:45 -0300593 /*
594 * When -EBUSY is returned the loop restarts with
595 * hmm_vma_walk.last set to an address that has not been stored
596 * in pfns. All entries < last in the pfn array are set to their
597 * output, and all >= are still at their input values.
598 */
Ralph Campbelld28c2c9a2019-11-04 14:21:40 -0800599 } while (ret == -EBUSY);
Jason Gunthorpebe957c82020-05-01 15:20:45 -0300600 return ret;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700601}
Jérôme Glisse73231612019-05-13 17:19:58 -0700602EXPORT_SYMBOL(hmm_range_fault);