blob: 26916ff6c8df2dd4d4470ae36e4eb1090ab7f42a [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
Jason Gunthorpec7d8b782019-08-06 20:15:42 -030029static struct mmu_notifier *hmm_alloc_notifier(struct mm_struct *mm)
Jérôme Glisse704f3f22019-05-13 17:19:48 -070030{
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -030031 struct hmm *hmm;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070032
Jason Gunthorpec7d8b782019-08-06 20:15:42 -030033 hmm = kzalloc(sizeof(*hmm), GFP_KERNEL);
Jérôme Glissec0b12402017-09-08 16:11:27 -070034 if (!hmm)
Jason Gunthorpec7d8b782019-08-06 20:15:42 -030035 return ERR_PTR(-ENOMEM);
36
Jérôme Glissea3e0d412019-05-13 17:20:01 -070037 init_waitqueue_head(&hmm->wq);
Jérôme Glissec0b12402017-09-08 16:11:27 -070038 INIT_LIST_HEAD(&hmm->mirrors);
39 init_rwsem(&hmm->mirrors_sem);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070040 INIT_LIST_HEAD(&hmm->ranges);
Jason Gunthorpe5a136b42019-06-07 12:10:33 -030041 spin_lock_init(&hmm->ranges_lock);
Jérôme Glissea3e0d412019-05-13 17:20:01 -070042 hmm->notifiers = 0;
Jason Gunthorpec7d8b782019-08-06 20:15:42 -030043 return &hmm->mmu_notifier;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070044}
45
Jason Gunthorpec7d8b782019-08-06 20:15:42 -030046static void hmm_free_notifier(struct mmu_notifier *mn)
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -030047{
Jason Gunthorpec7d8b782019-08-06 20:15:42 -030048 struct hmm *hmm = container_of(mn, struct hmm, mmu_notifier);
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -030049
Jason Gunthorpec7d8b782019-08-06 20:15:42 -030050 WARN_ON(!list_empty(&hmm->ranges));
51 WARN_ON(!list_empty(&hmm->mirrors));
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070052 kfree(hmm);
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070053}
54
Jérôme Glissea3e0d412019-05-13 17:20:01 -070055static void hmm_release(struct mmu_notifier *mn, struct mm_struct *mm)
Jérôme Glissec0b12402017-09-08 16:11:27 -070056{
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -030057 struct hmm *hmm = container_of(mn, struct hmm, mmu_notifier);
Jérôme Glissec0b12402017-09-08 16:11:27 -070058 struct hmm_mirror *mirror;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070059
Jason Gunthorpe47f24592019-05-23 11:08:28 -030060 /*
61 * Since hmm_range_register() holds the mmget() lock hmm_release() is
62 * prevented as long as a range exists.
63 */
64 WARN_ON(!list_empty_careful(&hmm->ranges));
Ralph Campbelle1401512018-04-10 16:28:19 -070065
Jason Gunthorpe14331722019-05-24 12:14:08 -030066 down_read(&hmm->mirrors_sem);
Jérôme Glissea3e0d412019-05-13 17:20:01 -070067 list_for_each_entry(mirror, &hmm->mirrors, list) {
Jason Gunthorpe14331722019-05-24 12:14:08 -030068 /*
69 * Note: The driver is not allowed to trigger
70 * hmm_mirror_unregister() from this thread.
71 */
72 if (mirror->ops->release)
Ralph Campbelle1401512018-04-10 16:28:19 -070073 mirror->ops->release(mirror);
Jérôme Glissea3e0d412019-05-13 17:20:01 -070074 }
75 up_read(&hmm->mirrors_sem);
Jérôme Glissec0b12402017-09-08 16:11:27 -070076}
77
Jason Gunthorpe5a136b42019-06-07 12:10:33 -030078static void notifiers_decrement(struct hmm *hmm)
Jérôme Glissec0b12402017-09-08 16:11:27 -070079{
Jason Gunthorpe5a136b42019-06-07 12:10:33 -030080 unsigned long flags;
Jérôme Glissec0b12402017-09-08 16:11:27 -070081
Jason Gunthorpe5a136b42019-06-07 12:10:33 -030082 spin_lock_irqsave(&hmm->ranges_lock, flags);
Jérôme Glissea3e0d412019-05-13 17:20:01 -070083 hmm->notifiers--;
84 if (!hmm->notifiers) {
85 struct hmm_range *range;
86
87 list_for_each_entry(range, &hmm->ranges, list) {
88 if (range->valid)
89 continue;
90 range->valid = true;
91 }
92 wake_up_all(&hmm->wq);
93 }
Jason Gunthorpe5a136b42019-06-07 12:10:33 -030094 spin_unlock_irqrestore(&hmm->ranges_lock, flags);
95}
Jérôme Glissea3e0d412019-05-13 17:20:01 -070096
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070097static int hmm_invalidate_range_start(struct mmu_notifier *mn,
98 const struct mmu_notifier_range *nrange)
99{
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300100 struct hmm *hmm = container_of(mn, struct hmm, mmu_notifier);
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700101 struct hmm_mirror *mirror;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700102 struct hmm_range *range;
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300103 unsigned long flags;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700104 int ret = 0;
105
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300106 spin_lock_irqsave(&hmm->ranges_lock, flags);
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700107 hmm->notifiers++;
108 list_for_each_entry(range, &hmm->ranges, list) {
Ralph Campbell1f961802019-07-25 17:56:44 -0700109 if (nrange->end < range->start || nrange->start >= range->end)
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700110 continue;
111
112 range->valid = false;
113 }
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300114 spin_unlock_irqrestore(&hmm->ranges_lock, flags);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700115
116 if (mmu_notifier_range_blockable(nrange))
117 down_read(&hmm->mirrors_sem);
118 else if (!down_read_trylock(&hmm->mirrors_sem)) {
119 ret = -EAGAIN;
120 goto out;
121 }
Jérôme Glissec0b12402017-09-08 16:11:27 -0700122
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300123 list_for_each_entry(mirror, &hmm->mirrors, list) {
124 int rc;
125
Ralph Campbell1f961802019-07-25 17:56:44 -0700126 rc = mirror->ops->sync_cpu_device_pagetables(mirror, nrange);
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300127 if (rc) {
Ralph Campbell1f961802019-07-25 17:56:44 -0700128 if (WARN_ON(mmu_notifier_range_blockable(nrange) ||
129 rc != -EAGAIN))
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300130 continue;
131 ret = -EAGAIN;
Ralph Campbell085ea252019-05-06 16:29:39 -0700132 break;
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300133 }
Jérôme Glissec0b12402017-09-08 16:11:27 -0700134 }
135 up_read(&hmm->mirrors_sem);
136
137out:
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300138 if (ret)
139 notifiers_decrement(hmm);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700140 return ret;
141}
142
143static void hmm_invalidate_range_end(struct mmu_notifier *mn,
144 const struct mmu_notifier_range *nrange)
145{
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300146 struct hmm *hmm = container_of(mn, struct hmm, mmu_notifier);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700147
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300148 notifiers_decrement(hmm);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700149}
150
151static const struct mmu_notifier_ops hmm_mmu_notifier_ops = {
Ralph Campbelle1401512018-04-10 16:28:19 -0700152 .release = hmm_release,
Jérôme Glissec0b12402017-09-08 16:11:27 -0700153 .invalidate_range_start = hmm_invalidate_range_start,
154 .invalidate_range_end = hmm_invalidate_range_end,
Jason Gunthorpec7d8b782019-08-06 20:15:42 -0300155 .alloc_notifier = hmm_alloc_notifier,
156 .free_notifier = hmm_free_notifier,
Jérôme Glissec0b12402017-09-08 16:11:27 -0700157};
158
159/*
160 * hmm_mirror_register() - register a mirror against an mm
161 *
162 * @mirror: new mirror struct to register
163 * @mm: mm to register against
Ralph Campbell085ea252019-05-06 16:29:39 -0700164 * Return: 0 on success, -ENOMEM if no memory, -EINVAL if invalid arguments
Jérôme Glissec0b12402017-09-08 16:11:27 -0700165 *
166 * To start mirroring a process address space, the device driver must register
167 * an HMM mirror struct.
Jason Gunthorpec7d8b782019-08-06 20:15:42 -0300168 *
169 * The caller cannot unregister the hmm_mirror while any ranges are
170 * registered.
171 *
172 * Callers using this function must put a call to mmu_notifier_synchronize()
173 * in their module exit functions.
Jérôme Glissec0b12402017-09-08 16:11:27 -0700174 */
175int hmm_mirror_register(struct hmm_mirror *mirror, struct mm_struct *mm)
176{
Jason Gunthorpec7d8b782019-08-06 20:15:42 -0300177 struct mmu_notifier *mn;
178
Linus Torvaldsfec88ab2019-07-14 19:42:11 -0700179 lockdep_assert_held_write(&mm->mmap_sem);
Jason Gunthorpe8a1a0cd2019-05-23 11:23:30 -0300180
Jérôme Glissec0b12402017-09-08 16:11:27 -0700181 /* Sanity check */
182 if (!mm || !mirror || !mirror->ops)
183 return -EINVAL;
184
Jason Gunthorpec7d8b782019-08-06 20:15:42 -0300185 mn = mmu_notifier_get_locked(&hmm_mmu_notifier_ops, mm);
186 if (IS_ERR(mn))
187 return PTR_ERR(mn);
188 mirror->hmm = container_of(mn, struct hmm, mmu_notifier);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700189
190 down_write(&mirror->hmm->mirrors_sem);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700191 list_add(&mirror->list, &mirror->hmm->mirrors);
192 up_write(&mirror->hmm->mirrors_sem);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700193
194 return 0;
195}
196EXPORT_SYMBOL(hmm_mirror_register);
197
198/*
199 * hmm_mirror_unregister() - unregister a mirror
200 *
Ralph Campbell085ea252019-05-06 16:29:39 -0700201 * @mirror: mirror struct to unregister
Jérôme Glissec0b12402017-09-08 16:11:27 -0700202 *
203 * Stop mirroring a process address space, and cleanup.
204 */
205void hmm_mirror_unregister(struct hmm_mirror *mirror)
206{
Jason Gunthorpe187229c2019-05-23 11:31:45 -0300207 struct hmm *hmm = mirror->hmm;
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700208
Jérôme Glissec0b12402017-09-08 16:11:27 -0700209 down_write(&hmm->mirrors_sem);
Jason Gunthorpe14331722019-05-24 12:14:08 -0300210 list_del(&mirror->list);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700211 up_write(&hmm->mirrors_sem);
Jason Gunthorpec7d8b782019-08-06 20:15:42 -0300212 mmu_notifier_put(&hmm->mmu_notifier);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700213}
214EXPORT_SYMBOL(hmm_mirror_unregister);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700215
Jérôme Glisse74eee182017-09-08 16:11:35 -0700216struct hmm_vma_walk {
217 struct hmm_range *range;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700218 struct dev_pagemap *pgmap;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700219 unsigned long last;
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700220 unsigned int flags;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700221};
222
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700223static int hmm_vma_do_fault(struct mm_walk *walk, unsigned long addr,
224 bool write_fault, uint64_t *pfn)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700225{
Kuehling, Felix9b1ae602019-05-10 19:53:24 +0000226 unsigned int flags = FAULT_FLAG_REMOTE;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700227 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700228 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700229 struct vm_area_struct *vma = walk->vma;
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700230 vm_fault_t ret;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700231
Ralph Campbell6c64f2b2019-08-23 15:17:52 -0700232 if (!vma)
233 goto err;
234
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700235 if (hmm_vma_walk->flags & HMM_FAULT_ALLOW_RETRY)
236 flags |= FAULT_FLAG_ALLOW_RETRY;
237 if (write_fault)
238 flags |= FAULT_FLAG_WRITE;
239
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700240 ret = handle_mm_fault(vma, addr, flags);
Jason Gunthorpee709acc2019-07-24 08:52:58 +0200241 if (ret & VM_FAULT_RETRY) {
242 /* Note, handle_mm_fault did up_read(&mm->mmap_sem)) */
Jérôme Glisse73231612019-05-13 17:19:58 -0700243 return -EAGAIN;
Jason Gunthorpee709acc2019-07-24 08:52:58 +0200244 }
Ralph Campbell6c64f2b2019-08-23 15:17:52 -0700245 if (ret & VM_FAULT_ERROR)
246 goto err;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700247
Jérôme Glisse73231612019-05-13 17:19:58 -0700248 return -EBUSY;
Ralph Campbell6c64f2b2019-08-23 15:17:52 -0700249
250err:
251 *pfn = range->values[HMM_PFN_ERROR];
252 return -EFAULT;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700253}
254
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700255static int hmm_pfns_bad(unsigned long addr,
256 unsigned long end,
257 struct mm_walk *walk)
258{
Jérôme Glissec7195472018-04-10 16:28:27 -0700259 struct hmm_vma_walk *hmm_vma_walk = walk->private;
260 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700261 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700262 unsigned long i;
263
264 i = (addr - range->start) >> PAGE_SHIFT;
265 for (; addr < end; addr += PAGE_SIZE, i++)
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700266 pfns[i] = range->values[HMM_PFN_ERROR];
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700267
268 return 0;
269}
270
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700271/*
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700272 * hmm_vma_walk_hole_() - handle a range lacking valid pmd or pte(s)
273 * @addr: range virtual start address (inclusive)
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700274 * @end: range virtual end address (exclusive)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700275 * @fault: should we fault or not ?
276 * @write_fault: write fault ?
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700277 * @walk: mm_walk structure
Ralph Campbell085ea252019-05-06 16:29:39 -0700278 * Return: 0 on success, -EBUSY after page fault, or page fault error
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700279 *
280 * This function will be called whenever pmd_none() or pte_none() returns true,
281 * or whenever there is no page directory covering the virtual address range.
282 */
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700283static int hmm_vma_walk_hole_(unsigned long addr, unsigned long end,
284 bool fault, bool write_fault,
285 struct mm_walk *walk)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700286{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700287 struct hmm_vma_walk *hmm_vma_walk = walk->private;
288 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700289 uint64_t *pfns = range->pfns;
Christoph Hellwig7f082632019-08-06 19:05:45 +0300290 unsigned long i;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700291
Jérôme Glisse74eee182017-09-08 16:11:35 -0700292 hmm_vma_walk->last = addr;
Christoph Hellwig7f082632019-08-06 19:05:45 +0300293 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700294
Ralph Campbellc18ce674d2019-08-23 15:17:53 -0700295 if (write_fault && walk->vma && !(walk->vma->vm_flags & VM_WRITE))
296 return -EPERM;
297
Christoph Hellwig7f082632019-08-06 19:05:45 +0300298 for (; addr < end; addr += PAGE_SIZE, i++) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700299 pfns[i] = range->values[HMM_PFN_NONE];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700300 if (fault || write_fault) {
Jérôme Glisse74eee182017-09-08 16:11:35 -0700301 int ret;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700302
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700303 ret = hmm_vma_do_fault(walk, addr, write_fault,
304 &pfns[i]);
Jérôme Glisse73231612019-05-13 17:19:58 -0700305 if (ret != -EBUSY)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700306 return ret;
307 }
308 }
309
Jérôme Glisse73231612019-05-13 17:19:58 -0700310 return (fault || write_fault) ? -EBUSY : 0;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700311}
312
313static inline void hmm_pte_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
314 uint64_t pfns, uint64_t cpu_flags,
315 bool *fault, bool *write_fault)
316{
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700317 struct hmm_range *range = hmm_vma_walk->range;
318
Christoph Hellwigd45d4642019-07-25 17:56:47 -0700319 if (hmm_vma_walk->flags & HMM_FAULT_SNAPSHOT)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700320 return;
321
Jérôme Glisse023a0192019-05-13 17:20:05 -0700322 /*
323 * So we not only consider the individual per page request we also
324 * consider the default flags requested for the range. The API can
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700325 * be used 2 ways. The first one where the HMM user coalesces
326 * multiple page faults into one request and sets flags per pfn for
327 * those faults. The second one where the HMM user wants to pre-
Jérôme Glisse023a0192019-05-13 17:20:05 -0700328 * fault a range with specific flags. For the latter one it is a
329 * waste to have the user pre-fill the pfn arrays with a default
330 * flags value.
331 */
332 pfns = (pfns & range->pfn_flags_mask) | range->default_flags;
333
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700334 /* We aren't ask to do anything ... */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700335 if (!(pfns & range->flags[HMM_PFN_VALID]))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700336 return;
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700337 /* If this is device memory then only fault if explicitly requested */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700338 if ((cpu_flags & range->flags[HMM_PFN_DEVICE_PRIVATE])) {
339 /* Do we fault on device memory ? */
340 if (pfns & range->flags[HMM_PFN_DEVICE_PRIVATE]) {
341 *write_fault = pfns & range->flags[HMM_PFN_WRITE];
342 *fault = true;
343 }
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700344 return;
345 }
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700346
347 /* If CPU page table is not valid then we need to fault */
348 *fault = !(cpu_flags & range->flags[HMM_PFN_VALID]);
349 /* Need to write fault ? */
350 if ((pfns & range->flags[HMM_PFN_WRITE]) &&
351 !(cpu_flags & range->flags[HMM_PFN_WRITE])) {
352 *write_fault = true;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700353 *fault = true;
354 }
355}
356
357static void hmm_range_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
358 const uint64_t *pfns, unsigned long npages,
359 uint64_t cpu_flags, bool *fault,
360 bool *write_fault)
361{
362 unsigned long i;
363
Christoph Hellwigd45d4642019-07-25 17:56:47 -0700364 if (hmm_vma_walk->flags & HMM_FAULT_SNAPSHOT) {
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700365 *fault = *write_fault = false;
366 return;
367 }
368
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700369 *fault = *write_fault = false;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700370 for (i = 0; i < npages; ++i) {
371 hmm_pte_need_fault(hmm_vma_walk, pfns[i], cpu_flags,
372 fault, write_fault);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700373 if ((*write_fault))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700374 return;
375 }
376}
377
378static int hmm_vma_walk_hole(unsigned long addr, unsigned long end,
379 struct mm_walk *walk)
380{
381 struct hmm_vma_walk *hmm_vma_walk = walk->private;
382 struct hmm_range *range = hmm_vma_walk->range;
383 bool fault, write_fault;
384 unsigned long i, npages;
385 uint64_t *pfns;
386
387 i = (addr - range->start) >> PAGE_SHIFT;
388 npages = (end - addr) >> PAGE_SHIFT;
389 pfns = &range->pfns[i];
390 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
391 0, &fault, &write_fault);
392 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
393}
394
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700395static inline uint64_t pmd_to_hmm_pfn_flags(struct hmm_range *range, pmd_t pmd)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700396{
397 if (pmd_protnone(pmd))
398 return 0;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700399 return pmd_write(pmd) ? range->flags[HMM_PFN_VALID] |
400 range->flags[HMM_PFN_WRITE] :
401 range->flags[HMM_PFN_VALID];
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700402}
403
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700404#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Christoph Hellwig9d3973d2019-08-06 19:05:49 +0300405static int hmm_vma_handle_pmd(struct mm_walk *walk, unsigned long addr,
406 unsigned long end, uint64_t *pfns, pmd_t pmd)
407{
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700408 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700409 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700410 unsigned long pfn, npages, i;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700411 bool fault, write_fault;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700412 uint64_t cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700413
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700414 npages = (end - addr) >> PAGE_SHIFT;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700415 cpu_flags = pmd_to_hmm_pfn_flags(range, pmd);
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700416 hmm_range_need_fault(hmm_vma_walk, pfns, npages, cpu_flags,
417 &fault, &write_fault);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700418
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700419 if (pmd_protnone(pmd) || fault || write_fault)
420 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700421
Christoph Hellwig309f9a42019-08-06 19:05:47 +0300422 pfn = pmd_pfn(pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700423 for (i = 0; addr < end; addr += PAGE_SIZE, i++, pfn++) {
424 if (pmd_devmap(pmd)) {
425 hmm_vma_walk->pgmap = get_dev_pagemap(pfn,
426 hmm_vma_walk->pgmap);
427 if (unlikely(!hmm_vma_walk->pgmap))
428 return -EBUSY;
429 }
Jérôme Glisse391aab12019-05-13 17:20:31 -0700430 pfns[i] = hmm_device_entry_from_pfn(range, pfn) | cpu_flags;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700431 }
432 if (hmm_vma_walk->pgmap) {
433 put_dev_pagemap(hmm_vma_walk->pgmap);
434 hmm_vma_walk->pgmap = NULL;
435 }
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700436 hmm_vma_walk->last = end;
437 return 0;
438}
Christoph Hellwig9d3973d2019-08-06 19:05:49 +0300439#else /* CONFIG_TRANSPARENT_HUGEPAGE */
440/* stub to allow the code below to compile */
441int hmm_vma_handle_pmd(struct mm_walk *walk, unsigned long addr,
442 unsigned long end, uint64_t *pfns, pmd_t pmd);
443#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700444
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700445static inline uint64_t pte_to_hmm_pfn_flags(struct hmm_range *range, pte_t pte)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700446{
Philip Yang789c2af2019-05-23 16:32:31 -0400447 if (pte_none(pte) || !pte_present(pte) || pte_protnone(pte))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700448 return 0;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700449 return pte_write(pte) ? range->flags[HMM_PFN_VALID] |
450 range->flags[HMM_PFN_WRITE] :
451 range->flags[HMM_PFN_VALID];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700452}
453
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700454static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,
455 unsigned long end, pmd_t *pmdp, pte_t *ptep,
456 uint64_t *pfn)
457{
458 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700459 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700460 bool fault, write_fault;
461 uint64_t cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700462 pte_t pte = *ptep;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700463 uint64_t orig_pfn = *pfn;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700464
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700465 *pfn = range->values[HMM_PFN_NONE];
Jérôme Glisse73231612019-05-13 17:19:58 -0700466 fault = write_fault = false;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700467
468 if (pte_none(pte)) {
Jérôme Glisse73231612019-05-13 17:19:58 -0700469 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, 0,
470 &fault, &write_fault);
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700471 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700472 goto fault;
473 return 0;
474 }
475
476 if (!pte_present(pte)) {
477 swp_entry_t entry = pte_to_swp_entry(pte);
478
479 if (!non_swap_entry(entry)) {
Yang, Philipe3fe8e52019-08-15 20:52:56 +0000480 cpu_flags = pte_to_hmm_pfn_flags(range, pte);
481 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
482 &fault, &write_fault);
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700483 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700484 goto fault;
485 return 0;
486 }
487
488 /*
489 * This is a special swap entry, ignore migration, use
490 * device and report anything else as error.
491 */
492 if (is_device_private_entry(entry)) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700493 cpu_flags = range->flags[HMM_PFN_VALID] |
494 range->flags[HMM_PFN_DEVICE_PRIVATE];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700495 cpu_flags |= is_write_device_private_entry(entry) ?
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700496 range->flags[HMM_PFN_WRITE] : 0;
497 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
498 &fault, &write_fault);
499 if (fault || write_fault)
500 goto fault;
Jérôme Glisse391aab12019-05-13 17:20:31 -0700501 *pfn = hmm_device_entry_from_pfn(range,
502 swp_offset(entry));
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700503 *pfn |= cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700504 return 0;
505 }
506
507 if (is_migration_entry(entry)) {
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700508 if (fault || write_fault) {
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700509 pte_unmap(ptep);
510 hmm_vma_walk->last = addr;
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700511 migration_entry_wait(walk->mm, pmdp, addr);
Jérôme Glisse73231612019-05-13 17:19:58 -0700512 return -EBUSY;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700513 }
514 return 0;
515 }
516
517 /* Report error for everything else */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700518 *pfn = range->values[HMM_PFN_ERROR];
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700519 return -EFAULT;
Jérôme Glisse73231612019-05-13 17:19:58 -0700520 } else {
521 cpu_flags = pte_to_hmm_pfn_flags(range, pte);
522 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
523 &fault, &write_fault);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700524 }
525
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700526 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700527 goto fault;
528
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700529 if (pte_devmap(pte)) {
530 hmm_vma_walk->pgmap = get_dev_pagemap(pte_pfn(pte),
531 hmm_vma_walk->pgmap);
532 if (unlikely(!hmm_vma_walk->pgmap))
533 return -EBUSY;
534 } else if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) && pte_special(pte)) {
535 *pfn = range->values[HMM_PFN_SPECIAL];
536 return -EFAULT;
537 }
538
Jérôme Glisse391aab12019-05-13 17:20:31 -0700539 *pfn = hmm_device_entry_from_pfn(range, pte_pfn(pte)) | cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700540 return 0;
541
542fault:
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700543 if (hmm_vma_walk->pgmap) {
544 put_dev_pagemap(hmm_vma_walk->pgmap);
545 hmm_vma_walk->pgmap = NULL;
546 }
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700547 pte_unmap(ptep);
548 /* Fault any virtual address we were asked to fault */
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700549 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700550}
551
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700552static int hmm_vma_walk_pmd(pmd_t *pmdp,
553 unsigned long start,
554 unsigned long end,
555 struct mm_walk *walk)
556{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700557 struct hmm_vma_walk *hmm_vma_walk = walk->private;
558 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700559 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700560 unsigned long addr = start, i;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700561 pte_t *ptep;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700562 pmd_t pmd;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700563
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700564again:
Jérôme Glissed08faca2018-10-30 15:04:20 -0700565 pmd = READ_ONCE(*pmdp);
566 if (pmd_none(pmd))
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700567 return hmm_vma_walk_hole(start, end, walk);
568
Jérôme Glissed08faca2018-10-30 15:04:20 -0700569 if (thp_migration_supported() && is_pmd_migration_entry(pmd)) {
570 bool fault, write_fault;
571 unsigned long npages;
572 uint64_t *pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700573
Jérôme Glissed08faca2018-10-30 15:04:20 -0700574 i = (addr - range->start) >> PAGE_SHIFT;
575 npages = (end - addr) >> PAGE_SHIFT;
576 pfns = &range->pfns[i];
577
578 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
579 0, &fault, &write_fault);
580 if (fault || write_fault) {
581 hmm_vma_walk->last = addr;
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700582 pmd_migration_entry_wait(walk->mm, pmdp);
Jérôme Glisse73231612019-05-13 17:19:58 -0700583 return -EBUSY;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700584 }
585 return 0;
586 } else if (!pmd_present(pmd))
587 return hmm_pfns_bad(start, end, walk);
588
589 if (pmd_devmap(pmd) || pmd_trans_huge(pmd)) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700590 /*
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700591 * No need to take pmd_lock here, even if some other thread
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700592 * is splitting the huge pmd we will get that event through
593 * mmu_notifier callback.
594 *
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700595 * So just read pmd value and check again it's a transparent
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700596 * huge or device mapping one and compute corresponding pfn
597 * values.
598 */
599 pmd = pmd_read_atomic(pmdp);
600 barrier();
601 if (!pmd_devmap(pmd) && !pmd_trans_huge(pmd))
602 goto again;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700603
Jérôme Glissed08faca2018-10-30 15:04:20 -0700604 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700605 return hmm_vma_handle_pmd(walk, addr, end, &pfns[i], pmd);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700606 }
607
Jérôme Glissed08faca2018-10-30 15:04:20 -0700608 /*
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700609 * We have handled all the valid cases above ie either none, migration,
Jérôme Glissed08faca2018-10-30 15:04:20 -0700610 * huge or transparent huge. At this point either it is a valid pmd
611 * entry pointing to pte directory or it is a bad pmd that will not
612 * recover.
613 */
614 if (pmd_bad(pmd))
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700615 return hmm_pfns_bad(start, end, walk);
616
617 ptep = pte_offset_map(pmdp, addr);
Jérôme Glissed08faca2018-10-30 15:04:20 -0700618 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700619 for (; addr < end; addr += PAGE_SIZE, ptep++, i++) {
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700620 int r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700621
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700622 r = hmm_vma_handle_pte(walk, addr, end, pmdp, ptep, &pfns[i]);
623 if (r) {
624 /* hmm_vma_handle_pte() did unmap pte directory */
625 hmm_vma_walk->last = addr;
626 return r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700627 }
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700628 }
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700629 if (hmm_vma_walk->pgmap) {
630 /*
631 * We do put_dev_pagemap() here and not in hmm_vma_handle_pte()
632 * so that we can leverage get_dev_pagemap() optimization which
633 * will not re-take a reference on a pgmap if we already have
634 * one.
635 */
636 put_dev_pagemap(hmm_vma_walk->pgmap);
637 hmm_vma_walk->pgmap = NULL;
638 }
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700639 pte_unmap(ptep - 1);
640
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700641 hmm_vma_walk->last = addr;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700642 return 0;
643}
644
Christoph Hellwigf0b3c452019-08-06 19:05:48 +0300645#if defined(CONFIG_ARCH_HAS_PTE_DEVMAP) && \
646 defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
647static inline uint64_t pud_to_hmm_pfn_flags(struct hmm_range *range, pud_t pud)
648{
649 if (!pud_present(pud))
650 return 0;
651 return pud_write(pud) ? range->flags[HMM_PFN_VALID] |
652 range->flags[HMM_PFN_WRITE] :
653 range->flags[HMM_PFN_VALID];
654}
655
656static int hmm_vma_walk_pud(pud_t *pudp, unsigned long start, unsigned long end,
657 struct mm_walk *walk)
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700658{
659 struct hmm_vma_walk *hmm_vma_walk = walk->private;
660 struct hmm_range *range = hmm_vma_walk->range;
661 unsigned long addr = start, next;
662 pmd_t *pmdp;
663 pud_t pud;
664 int ret;
665
666again:
667 pud = READ_ONCE(*pudp);
668 if (pud_none(pud))
669 return hmm_vma_walk_hole(start, end, walk);
670
671 if (pud_huge(pud) && pud_devmap(pud)) {
672 unsigned long i, npages, pfn;
673 uint64_t *pfns, cpu_flags;
674 bool fault, write_fault;
675
676 if (!pud_present(pud))
677 return hmm_vma_walk_hole(start, end, walk);
678
679 i = (addr - range->start) >> PAGE_SHIFT;
680 npages = (end - addr) >> PAGE_SHIFT;
681 pfns = &range->pfns[i];
682
683 cpu_flags = pud_to_hmm_pfn_flags(range, pud);
684 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
685 cpu_flags, &fault, &write_fault);
686 if (fault || write_fault)
687 return hmm_vma_walk_hole_(addr, end, fault,
688 write_fault, walk);
689
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700690 pfn = pud_pfn(pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
691 for (i = 0; i < npages; ++i, ++pfn) {
692 hmm_vma_walk->pgmap = get_dev_pagemap(pfn,
693 hmm_vma_walk->pgmap);
694 if (unlikely(!hmm_vma_walk->pgmap))
695 return -EBUSY;
Jérôme Glisse391aab12019-05-13 17:20:31 -0700696 pfns[i] = hmm_device_entry_from_pfn(range, pfn) |
697 cpu_flags;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700698 }
699 if (hmm_vma_walk->pgmap) {
700 put_dev_pagemap(hmm_vma_walk->pgmap);
701 hmm_vma_walk->pgmap = NULL;
702 }
703 hmm_vma_walk->last = end;
704 return 0;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700705 }
706
707 split_huge_pud(walk->vma, pudp, addr);
708 if (pud_none(*pudp))
709 goto again;
710
711 pmdp = pmd_offset(pudp, addr);
712 do {
713 next = pmd_addr_end(addr, end);
714 ret = hmm_vma_walk_pmd(pmdp, addr, next, walk);
715 if (ret)
716 return ret;
717 } while (pmdp++, addr = next, addr != end);
718
719 return 0;
720}
Christoph Hellwigf0b3c452019-08-06 19:05:48 +0300721#else
722#define hmm_vma_walk_pud NULL
723#endif
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700724
Christoph Hellwig251bbe52019-08-06 19:05:50 +0300725#ifdef CONFIG_HUGETLB_PAGE
Jérôme Glisse63d50662019-05-13 17:20:18 -0700726static int hmm_vma_walk_hugetlb_entry(pte_t *pte, unsigned long hmask,
727 unsigned long start, unsigned long end,
728 struct mm_walk *walk)
729{
Christoph Hellwig05c23af2019-08-06 19:05:46 +0300730 unsigned long addr = start, i, pfn;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700731 struct hmm_vma_walk *hmm_vma_walk = walk->private;
732 struct hmm_range *range = hmm_vma_walk->range;
733 struct vm_area_struct *vma = walk->vma;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700734 uint64_t orig_pfn, cpu_flags;
735 bool fault, write_fault;
736 spinlock_t *ptl;
737 pte_t entry;
738 int ret = 0;
739
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700740 ptl = huge_pte_lock(hstate_vma(vma), walk->mm, pte);
Jérôme Glisse63d50662019-05-13 17:20:18 -0700741 entry = huge_ptep_get(pte);
742
Christoph Hellwig7f082632019-08-06 19:05:45 +0300743 i = (start - range->start) >> PAGE_SHIFT;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700744 orig_pfn = range->pfns[i];
745 range->pfns[i] = range->values[HMM_PFN_NONE];
746 cpu_flags = pte_to_hmm_pfn_flags(range, entry);
747 fault = write_fault = false;
748 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
749 &fault, &write_fault);
750 if (fault || write_fault) {
751 ret = -ENOENT;
752 goto unlock;
753 }
754
Christoph Hellwig05c23af2019-08-06 19:05:46 +0300755 pfn = pte_pfn(entry) + ((start & ~hmask) >> PAGE_SHIFT);
Christoph Hellwig7f082632019-08-06 19:05:45 +0300756 for (; addr < end; addr += PAGE_SIZE, i++, pfn++)
Jérôme Glisse391aab12019-05-13 17:20:31 -0700757 range->pfns[i] = hmm_device_entry_from_pfn(range, pfn) |
758 cpu_flags;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700759 hmm_vma_walk->last = end;
760
761unlock:
762 spin_unlock(ptl);
763
764 if (ret == -ENOENT)
765 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
766
767 return ret;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700768}
Christoph Hellwig251bbe52019-08-06 19:05:50 +0300769#else
770#define hmm_vma_walk_hugetlb_entry NULL
771#endif /* CONFIG_HUGETLB_PAGE */
Jérôme Glisse63d50662019-05-13 17:20:18 -0700772
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700773static void hmm_pfns_clear(struct hmm_range *range,
774 uint64_t *pfns,
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700775 unsigned long addr,
776 unsigned long end)
777{
778 for (; addr < end; addr += PAGE_SIZE, pfns++)
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700779 *pfns = range->values[HMM_PFN_NONE];
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700780}
781
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700782/*
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700783 * hmm_range_register() - start tracking change to CPU page table over a range
Jérôme Glisse25f23a02019-05-13 17:19:55 -0700784 * @range: range
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700785 * @mm: the mm struct for the range of virtual address
Christoph Hellwigfac555a2019-08-06 19:05:44 +0300786 *
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700787 * Return: 0 on success, -EFAULT if the address space is no longer valid
Jérôme Glisse25f23a02019-05-13 17:19:55 -0700788 *
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700789 * Track updates to the CPU page table see include/linux/hmm.h
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700790 */
Christoph Hellwigfac555a2019-08-06 19:05:44 +0300791int hmm_range_register(struct hmm_range *range, struct hmm_mirror *mirror)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700792{
Jason Gunthorpee36acfe2019-05-23 09:41:19 -0300793 struct hmm *hmm = mirror->hmm;
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300794 unsigned long flags;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700795
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700796 range->valid = false;
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700797 range->hmm = NULL;
798
Christoph Hellwig7f082632019-08-06 19:05:45 +0300799 if ((range->start & (PAGE_SIZE - 1)) || (range->end & (PAGE_SIZE - 1)))
Jérôme Glisse63d50662019-05-13 17:20:18 -0700800 return -EINVAL;
Christoph Hellwigfac555a2019-08-06 19:05:44 +0300801 if (range->start >= range->end)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700802 return -EINVAL;
803
Jason Gunthorpe47f24592019-05-23 11:08:28 -0300804 /* Prevent hmm_release() from running while the range is valid */
Jason Gunthorpec7d8b782019-08-06 20:15:42 -0300805 if (!mmget_not_zero(hmm->mmu_notifier.mm))
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700806 return -EFAULT;
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700807
Ralph Campbell085ea252019-05-06 16:29:39 -0700808 /* Initialize range to track CPU page table updates. */
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300809 spin_lock_irqsave(&hmm->ranges_lock, flags);
Jérôme Glisse86586a42018-04-10 16:28:34 -0700810
Ralph Campbell085ea252019-05-06 16:29:39 -0700811 range->hmm = hmm;
Jason Gunthorpe157816f2019-05-23 11:43:43 -0300812 list_add(&range->list, &hmm->ranges);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700813
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700814 /*
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700815 * If there are any concurrent notifiers we have to wait for them for
816 * the range to be valid (see hmm_range_wait_until_valid()).
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700817 */
Ralph Campbell085ea252019-05-06 16:29:39 -0700818 if (!hmm->notifiers)
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700819 range->valid = true;
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300820 spin_unlock_irqrestore(&hmm->ranges_lock, flags);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700821
822 return 0;
823}
824EXPORT_SYMBOL(hmm_range_register);
825
826/*
827 * hmm_range_unregister() - stop tracking change to CPU page table over a range
828 * @range: range
829 *
830 * Range struct is used to track updates to the CPU page table after a call to
831 * hmm_range_register(). See include/linux/hmm.h for how to use it.
832 */
833void hmm_range_unregister(struct hmm_range *range)
834{
Ralph Campbell085ea252019-05-06 16:29:39 -0700835 struct hmm *hmm = range->hmm;
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300836 unsigned long flags;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700837
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300838 spin_lock_irqsave(&hmm->ranges_lock, flags);
Jason Gunthorpe47f24592019-05-23 11:08:28 -0300839 list_del_init(&range->list);
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300840 spin_unlock_irqrestore(&hmm->ranges_lock, flags);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700841
842 /* Drop reference taken by hmm_range_register() */
Jason Gunthorpec7d8b782019-08-06 20:15:42 -0300843 mmput(hmm->mmu_notifier.mm);
Jason Gunthorpe2dcc3eb2019-05-23 11:40:24 -0300844
845 /*
846 * The range is now invalid and the ref on the hmm is dropped, so
847 * poison the pointer. Leave other fields in place, for the caller's
848 * use.
849 */
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700850 range->valid = false;
Jason Gunthorpe2dcc3eb2019-05-23 11:40:24 -0300851 memset(&range->hmm, POISON_INUSE, sizeof(range->hmm));
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700852}
853EXPORT_SYMBOL(hmm_range_unregister);
854
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700855/**
856 * hmm_range_fault - try to fault some address in a virtual address range
857 * @range: range being faulted
858 * @flags: HMM_FAULT_* flags
Jérôme Glisse73231612019-05-13 17:19:58 -0700859 *
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700860 * Return: the number of valid pages in range->pfns[] (from range start
861 * address), which may be zero. On error one of the following status codes
862 * can be returned:
863 *
864 * -EINVAL: Invalid arguments or mm or virtual address is in an invalid vma
865 * (e.g., device file vma).
866 * -ENOMEM: Out of memory.
867 * -EPERM: Invalid permission (e.g., asking for write and range is read
868 * only).
869 * -EAGAIN: A page fault needs to be retried and mmap_sem was dropped.
870 * -EBUSY: The range has been invalidated and the caller needs to wait for
871 * the invalidation to finish.
872 * -EFAULT: Invalid (i.e., either no valid vma or it is illegal to access
873 * that range) number of valid pages in range->pfns[] (from
874 * range start address).
Jérôme Glisse74eee182017-09-08 16:11:35 -0700875 *
876 * This is similar to a regular CPU page fault except that it will not trigger
Jérôme Glisse73231612019-05-13 17:19:58 -0700877 * any memory migration if the memory being faulted is not accessible by CPUs
878 * and caller does not ask for migration.
Jérôme Glisse74eee182017-09-08 16:11:35 -0700879 *
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700880 * On error, for one virtual address in the range, the function will mark the
881 * corresponding HMM pfn entry with an error flag.
Jérôme Glisse74eee182017-09-08 16:11:35 -0700882 */
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700883long hmm_range_fault(struct hmm_range *range, unsigned int flags)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700884{
Jérôme Glisse63d50662019-05-13 17:20:18 -0700885 const unsigned long device_vma = VM_IO | VM_PFNMAP | VM_MIXEDMAP;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700886 unsigned long start = range->start, end;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700887 struct hmm_vma_walk hmm_vma_walk;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700888 struct hmm *hmm = range->hmm;
889 struct vm_area_struct *vma;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700890 struct mm_walk mm_walk;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700891 int ret;
892
Jason Gunthorpec7d8b782019-08-06 20:15:42 -0300893 lockdep_assert_held(&hmm->mmu_notifier.mm->mmap_sem);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700894
895 do {
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700896 /* If range is no longer valid force retry. */
Christoph Hellwig2bcbeae2019-07-24 08:52:52 +0200897 if (!range->valid)
898 return -EBUSY;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700899
Jason Gunthorpec7d8b782019-08-06 20:15:42 -0300900 vma = find_vma(hmm->mmu_notifier.mm, start);
Jérôme Glisse63d50662019-05-13 17:20:18 -0700901 if (vma == NULL || (vma->vm_flags & device_vma))
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700902 return -EFAULT;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700903
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700904 if (!(vma->vm_flags & VM_READ)) {
905 /*
906 * If vma do not allow read access, then assume that it
907 * does not allow write access, either. HMM does not
908 * support architecture that allow write without read.
909 */
910 hmm_pfns_clear(range, range->pfns,
911 range->start, range->end);
912 return -EPERM;
913 }
914
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700915 hmm_vma_walk.pgmap = NULL;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700916 hmm_vma_walk.last = start;
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700917 hmm_vma_walk.flags = flags;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700918 hmm_vma_walk.range = range;
919 mm_walk.private = &hmm_vma_walk;
920 end = min(range->end, vma->vm_end);
921
922 mm_walk.vma = vma;
923 mm_walk.mm = vma->vm_mm;
924 mm_walk.pte_entry = NULL;
925 mm_walk.test_walk = NULL;
926 mm_walk.hugetlb_entry = NULL;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700927 mm_walk.pud_entry = hmm_vma_walk_pud;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700928 mm_walk.pmd_entry = hmm_vma_walk_pmd;
929 mm_walk.pte_hole = hmm_vma_walk_hole;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700930 mm_walk.hugetlb_entry = hmm_vma_walk_hugetlb_entry;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700931
932 do {
933 ret = walk_page_range(start, end, &mm_walk);
934 start = hmm_vma_walk.last;
935
936 /* Keep trying while the range is valid. */
937 } while (ret == -EBUSY && range->valid);
938
939 if (ret) {
940 unsigned long i;
941
942 i = (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
943 hmm_pfns_clear(range, &range->pfns[i],
944 hmm_vma_walk.last, range->end);
945 return ret;
946 }
947 start = end;
948
949 } while (start < range->end);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700950
Jérôme Glisse73231612019-05-13 17:19:58 -0700951 return (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700952}
Jérôme Glisse73231612019-05-13 17:19:58 -0700953EXPORT_SYMBOL(hmm_range_fault);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -0700954
955/**
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700956 * hmm_range_dma_map - hmm_range_fault() and dma map page all in one.
957 * @range: range being faulted
958 * @device: device to map page to
959 * @daddrs: array of dma addresses for the mapped pages
960 * @flags: HMM_FAULT_*
Jérôme Glisse55c0ece2019-05-13 17:20:28 -0700961 *
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700962 * Return: the number of pages mapped on success (including zero), or any
963 * status return from hmm_range_fault() otherwise.
Jérôme Glisse55c0ece2019-05-13 17:20:28 -0700964 */
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700965long hmm_range_dma_map(struct hmm_range *range, struct device *device,
966 dma_addr_t *daddrs, unsigned int flags)
Jérôme Glisse55c0ece2019-05-13 17:20:28 -0700967{
968 unsigned long i, npages, mapped;
969 long ret;
970
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700971 ret = hmm_range_fault(range, flags);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -0700972 if (ret <= 0)
973 return ret ? ret : -EBUSY;
974
975 npages = (range->end - range->start) >> PAGE_SHIFT;
976 for (i = 0, mapped = 0; i < npages; ++i) {
977 enum dma_data_direction dir = DMA_TO_DEVICE;
978 struct page *page;
979
980 /*
981 * FIXME need to update DMA API to provide invalid DMA address
982 * value instead of a function to test dma address value. This
983 * would remove lot of dumb code duplicated accross many arch.
984 *
985 * For now setting it to 0 here is good enough as the pfns[]
986 * value is what is use to check what is valid and what isn't.
987 */
988 daddrs[i] = 0;
989
Jérôme Glisse391aab12019-05-13 17:20:31 -0700990 page = hmm_device_entry_to_page(range, range->pfns[i]);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -0700991 if (page == NULL)
992 continue;
993
994 /* Check if range is being invalidated */
995 if (!range->valid) {
996 ret = -EBUSY;
997 goto unmap;
998 }
999
1000 /* If it is read and write than map bi-directional. */
1001 if (range->pfns[i] & range->flags[HMM_PFN_WRITE])
1002 dir = DMA_BIDIRECTIONAL;
1003
1004 daddrs[i] = dma_map_page(device, page, 0, PAGE_SIZE, dir);
1005 if (dma_mapping_error(device, daddrs[i])) {
1006 ret = -EFAULT;
1007 goto unmap;
1008 }
1009
1010 mapped++;
1011 }
1012
1013 return mapped;
1014
1015unmap:
1016 for (npages = i, i = 0; (i < npages) && mapped; ++i) {
1017 enum dma_data_direction dir = DMA_TO_DEVICE;
1018 struct page *page;
1019
Jérôme Glisse391aab12019-05-13 17:20:31 -07001020 page = hmm_device_entry_to_page(range, range->pfns[i]);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001021 if (page == NULL)
1022 continue;
1023
1024 if (dma_mapping_error(device, daddrs[i]))
1025 continue;
1026
1027 /* If it is read and write than map bi-directional. */
1028 if (range->pfns[i] & range->flags[HMM_PFN_WRITE])
1029 dir = DMA_BIDIRECTIONAL;
1030
1031 dma_unmap_page(device, daddrs[i], PAGE_SIZE, dir);
1032 mapped--;
1033 }
1034
1035 return ret;
1036}
1037EXPORT_SYMBOL(hmm_range_dma_map);
1038
1039/**
1040 * hmm_range_dma_unmap() - unmap range of that was map with hmm_range_dma_map()
1041 * @range: range being unmapped
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001042 * @device: device against which dma map was done
1043 * @daddrs: dma address of mapped pages
1044 * @dirty: dirty page if it had the write flag set
Ralph Campbell085ea252019-05-06 16:29:39 -07001045 * Return: number of page unmapped on success, -EINVAL otherwise
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001046 *
1047 * Note that caller MUST abide by mmu notifier or use HMM mirror and abide
1048 * to the sync_cpu_device_pagetables() callback so that it is safe here to
1049 * call set_page_dirty(). Caller must also take appropriate locks to avoid
1050 * concurrent mmu notifier or sync_cpu_device_pagetables() to make progress.
1051 */
1052long hmm_range_dma_unmap(struct hmm_range *range,
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001053 struct device *device,
1054 dma_addr_t *daddrs,
1055 bool dirty)
1056{
1057 unsigned long i, npages;
1058 long cpages = 0;
1059
1060 /* Sanity check. */
1061 if (range->end <= range->start)
1062 return -EINVAL;
1063 if (!daddrs)
1064 return -EINVAL;
1065 if (!range->pfns)
1066 return -EINVAL;
1067
1068 npages = (range->end - range->start) >> PAGE_SHIFT;
1069 for (i = 0; i < npages; ++i) {
1070 enum dma_data_direction dir = DMA_TO_DEVICE;
1071 struct page *page;
1072
Jérôme Glisse391aab12019-05-13 17:20:31 -07001073 page = hmm_device_entry_to_page(range, range->pfns[i]);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001074 if (page == NULL)
1075 continue;
1076
1077 /* If it is read and write than map bi-directional. */
1078 if (range->pfns[i] & range->flags[HMM_PFN_WRITE]) {
1079 dir = DMA_BIDIRECTIONAL;
1080
1081 /*
1082 * See comments in function description on why it is
1083 * safe here to call set_page_dirty()
1084 */
1085 if (dirty)
1086 set_page_dirty(page);
1087 }
1088
1089 /* Unmap and clear pfns/dma address */
1090 dma_unmap_page(device, daddrs[i], PAGE_SIZE, dir);
1091 range->pfns[i] = range->values[HMM_PFN_NONE];
1092 /* FIXME see comments in hmm_vma_dma_map() */
1093 daddrs[i] = 0;
1094 cpages++;
1095 }
1096
1097 return cpages;
1098}
1099EXPORT_SYMBOL(hmm_range_dma_unmap);