blob: 8d56a43426240c6f79e86147388167f77e5d9e4b [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 */
11#include <linux/mm.h>
12#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 Glissec0b12402017-09-08 16:11:27 -070029static const struct mmu_notifier_ops hmm_mmu_notifier_ops;
30
Jérôme Glisse704f3f22019-05-13 17:19:48 -070031/**
32 * hmm_get_or_create - register HMM against an mm (HMM internal)
33 *
34 * @mm: mm struct to attach to
Ralph Campbelld2e8d552019-07-25 17:56:45 -070035 * Return: an HMM object, either by referencing the existing
Jérôme Glisse704f3f22019-05-13 17:19:48 -070036 * (per-process) object, or by creating a new one.
37 *
38 * This is not intended to be used directly by device drivers. If mm already
39 * has an HMM struct then it get a reference on it and returns it. Otherwise
40 * it allocates an HMM struct, initializes it, associate it with the mm and
41 * returns it.
42 */
43static struct hmm *hmm_get_or_create(struct mm_struct *mm)
44{
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -030045 struct hmm *hmm;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070046
Linus Torvaldsfec88ab2019-07-14 19:42:11 -070047 lockdep_assert_held_write(&mm->mmap_sem);
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -030048
49 /* Abuse the page_table_lock to also protect mm->hmm. */
50 spin_lock(&mm->page_table_lock);
51 hmm = mm->hmm;
52 if (mm->hmm && kref_get_unless_zero(&mm->hmm->kref))
53 goto out_unlock;
54 spin_unlock(&mm->page_table_lock);
Jérôme Glissec0b12402017-09-08 16:11:27 -070055
56 hmm = kmalloc(sizeof(*hmm), GFP_KERNEL);
57 if (!hmm)
58 return NULL;
Jérôme Glissea3e0d412019-05-13 17:20:01 -070059 init_waitqueue_head(&hmm->wq);
Jérôme Glissec0b12402017-09-08 16:11:27 -070060 INIT_LIST_HEAD(&hmm->mirrors);
61 init_rwsem(&hmm->mirrors_sem);
Jérôme Glissec0b12402017-09-08 16:11:27 -070062 hmm->mmu_notifier.ops = NULL;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070063 INIT_LIST_HEAD(&hmm->ranges);
Jason Gunthorpe5a136b42019-06-07 12:10:33 -030064 spin_lock_init(&hmm->ranges_lock);
Jérôme Glisse704f3f22019-05-13 17:19:48 -070065 kref_init(&hmm->kref);
Jérôme Glissea3e0d412019-05-13 17:20:01 -070066 hmm->notifiers = 0;
Jérôme Glissec0b12402017-09-08 16:11:27 -070067 hmm->mm = mm;
68
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -030069 hmm->mmu_notifier.ops = &hmm_mmu_notifier_ops;
70 if (__mmu_notifier_register(&hmm->mmu_notifier, mm)) {
71 kfree(hmm);
72 return NULL;
73 }
Jérôme Glissec0b12402017-09-08 16:11:27 -070074
Jason Gunthorpec8a53b22019-05-23 10:36:46 -030075 mmgrab(hmm->mm);
Ralph Campbell86a2d592018-10-30 15:04:14 -070076
77 /*
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -030078 * We hold the exclusive mmap_sem here so we know that mm->hmm is
79 * still NULL or 0 kref, and is safe to update.
Ralph Campbell86a2d592018-10-30 15:04:14 -070080 */
Ralph Campbell86a2d592018-10-30 15:04:14 -070081 spin_lock(&mm->page_table_lock);
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -030082 mm->hmm = hmm;
83
84out_unlock:
Ralph Campbell86a2d592018-10-30 15:04:14 -070085 spin_unlock(&mm->page_table_lock);
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -030086 return hmm;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070087}
88
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -030089static void hmm_free_rcu(struct rcu_head *rcu)
90{
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -030091 struct hmm *hmm = container_of(rcu, struct hmm, rcu);
92
93 mmdrop(hmm->mm);
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070094 kfree(hmm);
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070095}
96
Jérôme Glisse704f3f22019-05-13 17:19:48 -070097static void hmm_free(struct kref *kref)
98{
99 struct hmm *hmm = container_of(kref, struct hmm, kref);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700100
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -0300101 spin_lock(&hmm->mm->page_table_lock);
102 if (hmm->mm->hmm == hmm)
103 hmm->mm->hmm = NULL;
104 spin_unlock(&hmm->mm->page_table_lock);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700105
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -0300106 mmu_notifier_unregister_no_release(&hmm->mmu_notifier, hmm->mm);
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300107 mmu_notifier_call_srcu(&hmm->rcu, hmm_free_rcu);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700108}
109
110static inline void hmm_put(struct hmm *hmm)
111{
112 kref_put(&hmm->kref, hmm_free);
113}
114
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700115static void hmm_release(struct mmu_notifier *mn, struct mm_struct *mm)
Jérôme Glissec0b12402017-09-08 16:11:27 -0700116{
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300117 struct hmm *hmm = container_of(mn, struct hmm, mmu_notifier);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700118 struct hmm_mirror *mirror;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700119
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300120 /* Bail out if hmm is in the process of being freed */
121 if (!kref_get_unless_zero(&hmm->kref))
122 return;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700123
Jason Gunthorpe47f24592019-05-23 11:08:28 -0300124 /*
125 * Since hmm_range_register() holds the mmget() lock hmm_release() is
126 * prevented as long as a range exists.
127 */
128 WARN_ON(!list_empty_careful(&hmm->ranges));
Ralph Campbelle1401512018-04-10 16:28:19 -0700129
Jason Gunthorpe14331722019-05-24 12:14:08 -0300130 down_read(&hmm->mirrors_sem);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700131 list_for_each_entry(mirror, &hmm->mirrors, list) {
Jason Gunthorpe14331722019-05-24 12:14:08 -0300132 /*
133 * Note: The driver is not allowed to trigger
134 * hmm_mirror_unregister() from this thread.
135 */
136 if (mirror->ops->release)
Ralph Campbelle1401512018-04-10 16:28:19 -0700137 mirror->ops->release(mirror);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700138 }
139 up_read(&hmm->mirrors_sem);
140
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700141 hmm_put(hmm);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700142}
143
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300144static void notifiers_decrement(struct hmm *hmm)
Jérôme Glissec0b12402017-09-08 16:11:27 -0700145{
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300146 unsigned long flags;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700147
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300148 spin_lock_irqsave(&hmm->ranges_lock, flags);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700149 hmm->notifiers--;
150 if (!hmm->notifiers) {
151 struct hmm_range *range;
152
153 list_for_each_entry(range, &hmm->ranges, list) {
154 if (range->valid)
155 continue;
156 range->valid = true;
157 }
158 wake_up_all(&hmm->wq);
159 }
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300160 spin_unlock_irqrestore(&hmm->ranges_lock, flags);
161}
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700162
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700163static int hmm_invalidate_range_start(struct mmu_notifier *mn,
164 const struct mmu_notifier_range *nrange)
165{
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300166 struct hmm *hmm = container_of(mn, struct hmm, mmu_notifier);
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700167 struct hmm_mirror *mirror;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700168 struct hmm_range *range;
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300169 unsigned long flags;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700170 int ret = 0;
171
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300172 if (!kref_get_unless_zero(&hmm->kref))
173 return 0;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700174
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300175 spin_lock_irqsave(&hmm->ranges_lock, flags);
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700176 hmm->notifiers++;
177 list_for_each_entry(range, &hmm->ranges, list) {
Ralph Campbell1f961802019-07-25 17:56:44 -0700178 if (nrange->end < range->start || nrange->start >= range->end)
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700179 continue;
180
181 range->valid = false;
182 }
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300183 spin_unlock_irqrestore(&hmm->ranges_lock, flags);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700184
185 if (mmu_notifier_range_blockable(nrange))
186 down_read(&hmm->mirrors_sem);
187 else if (!down_read_trylock(&hmm->mirrors_sem)) {
188 ret = -EAGAIN;
189 goto out;
190 }
Jérôme Glissec0b12402017-09-08 16:11:27 -0700191
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300192 list_for_each_entry(mirror, &hmm->mirrors, list) {
193 int rc;
194
Ralph Campbell1f961802019-07-25 17:56:44 -0700195 rc = mirror->ops->sync_cpu_device_pagetables(mirror, nrange);
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300196 if (rc) {
Ralph Campbell1f961802019-07-25 17:56:44 -0700197 if (WARN_ON(mmu_notifier_range_blockable(nrange) ||
198 rc != -EAGAIN))
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300199 continue;
200 ret = -EAGAIN;
Ralph Campbell085ea252019-05-06 16:29:39 -0700201 break;
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300202 }
Jérôme Glissec0b12402017-09-08 16:11:27 -0700203 }
204 up_read(&hmm->mirrors_sem);
205
206out:
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300207 if (ret)
208 notifiers_decrement(hmm);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700209 hmm_put(hmm);
210 return ret;
211}
212
213static void hmm_invalidate_range_end(struct mmu_notifier *mn,
214 const struct mmu_notifier_range *nrange)
215{
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300216 struct hmm *hmm = container_of(mn, struct hmm, mmu_notifier);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700217
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300218 if (!kref_get_unless_zero(&hmm->kref))
219 return;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700220
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300221 notifiers_decrement(hmm);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700222 hmm_put(hmm);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700223}
224
225static const struct mmu_notifier_ops hmm_mmu_notifier_ops = {
Ralph Campbelle1401512018-04-10 16:28:19 -0700226 .release = hmm_release,
Jérôme Glissec0b12402017-09-08 16:11:27 -0700227 .invalidate_range_start = hmm_invalidate_range_start,
228 .invalidate_range_end = hmm_invalidate_range_end,
229};
230
231/*
232 * hmm_mirror_register() - register a mirror against an mm
233 *
234 * @mirror: new mirror struct to register
235 * @mm: mm to register against
Ralph Campbell085ea252019-05-06 16:29:39 -0700236 * Return: 0 on success, -ENOMEM if no memory, -EINVAL if invalid arguments
Jérôme Glissec0b12402017-09-08 16:11:27 -0700237 *
238 * To start mirroring a process address space, the device driver must register
239 * an HMM mirror struct.
Jérôme Glissec0b12402017-09-08 16:11:27 -0700240 */
241int hmm_mirror_register(struct hmm_mirror *mirror, struct mm_struct *mm)
242{
Linus Torvaldsfec88ab2019-07-14 19:42:11 -0700243 lockdep_assert_held_write(&mm->mmap_sem);
Jason Gunthorpe8a1a0cd2019-05-23 11:23:30 -0300244
Jérôme Glissec0b12402017-09-08 16:11:27 -0700245 /* Sanity check */
246 if (!mm || !mirror || !mirror->ops)
247 return -EINVAL;
248
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700249 mirror->hmm = hmm_get_or_create(mm);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700250 if (!mirror->hmm)
251 return -ENOMEM;
252
253 down_write(&mirror->hmm->mirrors_sem);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700254 list_add(&mirror->list, &mirror->hmm->mirrors);
255 up_write(&mirror->hmm->mirrors_sem);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700256
257 return 0;
258}
259EXPORT_SYMBOL(hmm_mirror_register);
260
261/*
262 * hmm_mirror_unregister() - unregister a mirror
263 *
Ralph Campbell085ea252019-05-06 16:29:39 -0700264 * @mirror: mirror struct to unregister
Jérôme Glissec0b12402017-09-08 16:11:27 -0700265 *
266 * Stop mirroring a process address space, and cleanup.
267 */
268void hmm_mirror_unregister(struct hmm_mirror *mirror)
269{
Jason Gunthorpe187229c2019-05-23 11:31:45 -0300270 struct hmm *hmm = mirror->hmm;
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700271
Jérôme Glissec0b12402017-09-08 16:11:27 -0700272 down_write(&hmm->mirrors_sem);
Jason Gunthorpe14331722019-05-24 12:14:08 -0300273 list_del(&mirror->list);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700274 up_write(&hmm->mirrors_sem);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700275 hmm_put(hmm);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700276}
277EXPORT_SYMBOL(hmm_mirror_unregister);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700278
Jérôme Glisse74eee182017-09-08 16:11:35 -0700279struct hmm_vma_walk {
280 struct hmm_range *range;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700281 struct dev_pagemap *pgmap;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700282 unsigned long last;
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700283 unsigned int flags;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700284};
285
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700286static int hmm_vma_do_fault(struct mm_walk *walk, unsigned long addr,
287 bool write_fault, uint64_t *pfn)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700288{
Kuehling, Felix9b1ae602019-05-10 19:53:24 +0000289 unsigned int flags = FAULT_FLAG_REMOTE;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700290 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700291 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700292 struct vm_area_struct *vma = walk->vma;
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700293 vm_fault_t ret;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700294
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700295 if (hmm_vma_walk->flags & HMM_FAULT_ALLOW_RETRY)
296 flags |= FAULT_FLAG_ALLOW_RETRY;
297 if (write_fault)
298 flags |= FAULT_FLAG_WRITE;
299
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700300 ret = handle_mm_fault(vma, addr, flags);
Jason Gunthorpee709acc2019-07-24 08:52:58 +0200301 if (ret & VM_FAULT_RETRY) {
302 /* Note, handle_mm_fault did up_read(&mm->mmap_sem)) */
Jérôme Glisse73231612019-05-13 17:19:58 -0700303 return -EAGAIN;
Jason Gunthorpee709acc2019-07-24 08:52:58 +0200304 }
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700305 if (ret & VM_FAULT_ERROR) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700306 *pfn = range->values[HMM_PFN_ERROR];
Jérôme Glisse74eee182017-09-08 16:11:35 -0700307 return -EFAULT;
308 }
309
Jérôme Glisse73231612019-05-13 17:19:58 -0700310 return -EBUSY;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700311}
312
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700313static int hmm_pfns_bad(unsigned long addr,
314 unsigned long end,
315 struct mm_walk *walk)
316{
Jérôme Glissec7195472018-04-10 16:28:27 -0700317 struct hmm_vma_walk *hmm_vma_walk = walk->private;
318 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700319 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700320 unsigned long i;
321
322 i = (addr - range->start) >> PAGE_SHIFT;
323 for (; addr < end; addr += PAGE_SIZE, i++)
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700324 pfns[i] = range->values[HMM_PFN_ERROR];
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700325
326 return 0;
327}
328
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700329/*
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700330 * hmm_vma_walk_hole_() - handle a range lacking valid pmd or pte(s)
331 * @addr: range virtual start address (inclusive)
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700332 * @end: range virtual end address (exclusive)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700333 * @fault: should we fault or not ?
334 * @write_fault: write fault ?
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700335 * @walk: mm_walk structure
Ralph Campbell085ea252019-05-06 16:29:39 -0700336 * Return: 0 on success, -EBUSY after page fault, or page fault error
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700337 *
338 * This function will be called whenever pmd_none() or pte_none() returns true,
339 * or whenever there is no page directory covering the virtual address range.
340 */
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700341static int hmm_vma_walk_hole_(unsigned long addr, unsigned long end,
342 bool fault, bool write_fault,
343 struct mm_walk *walk)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700344{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700345 struct hmm_vma_walk *hmm_vma_walk = walk->private;
346 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700347 uint64_t *pfns = range->pfns;
Christoph Hellwig7f082632019-08-06 19:05:45 +0300348 unsigned long i;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700349
Jérôme Glisse74eee182017-09-08 16:11:35 -0700350 hmm_vma_walk->last = addr;
Christoph Hellwig7f082632019-08-06 19:05:45 +0300351 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700352
Christoph Hellwig7f082632019-08-06 19:05:45 +0300353 for (; addr < end; addr += PAGE_SIZE, i++) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700354 pfns[i] = range->values[HMM_PFN_NONE];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700355 if (fault || write_fault) {
Jérôme Glisse74eee182017-09-08 16:11:35 -0700356 int ret;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700357
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700358 ret = hmm_vma_do_fault(walk, addr, write_fault,
359 &pfns[i]);
Jérôme Glisse73231612019-05-13 17:19:58 -0700360 if (ret != -EBUSY)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700361 return ret;
362 }
363 }
364
Jérôme Glisse73231612019-05-13 17:19:58 -0700365 return (fault || write_fault) ? -EBUSY : 0;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700366}
367
368static inline void hmm_pte_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
369 uint64_t pfns, uint64_t cpu_flags,
370 bool *fault, bool *write_fault)
371{
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700372 struct hmm_range *range = hmm_vma_walk->range;
373
Christoph Hellwigd45d4642019-07-25 17:56:47 -0700374 if (hmm_vma_walk->flags & HMM_FAULT_SNAPSHOT)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700375 return;
376
Jérôme Glisse023a0192019-05-13 17:20:05 -0700377 /*
378 * So we not only consider the individual per page request we also
379 * consider the default flags requested for the range. The API can
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700380 * be used 2 ways. The first one where the HMM user coalesces
381 * multiple page faults into one request and sets flags per pfn for
382 * those faults. The second one where the HMM user wants to pre-
Jérôme Glisse023a0192019-05-13 17:20:05 -0700383 * fault a range with specific flags. For the latter one it is a
384 * waste to have the user pre-fill the pfn arrays with a default
385 * flags value.
386 */
387 pfns = (pfns & range->pfn_flags_mask) | range->default_flags;
388
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700389 /* We aren't ask to do anything ... */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700390 if (!(pfns & range->flags[HMM_PFN_VALID]))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700391 return;
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700392 /* If this is device memory then only fault if explicitly requested */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700393 if ((cpu_flags & range->flags[HMM_PFN_DEVICE_PRIVATE])) {
394 /* Do we fault on device memory ? */
395 if (pfns & range->flags[HMM_PFN_DEVICE_PRIVATE]) {
396 *write_fault = pfns & range->flags[HMM_PFN_WRITE];
397 *fault = true;
398 }
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700399 return;
400 }
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700401
402 /* If CPU page table is not valid then we need to fault */
403 *fault = !(cpu_flags & range->flags[HMM_PFN_VALID]);
404 /* Need to write fault ? */
405 if ((pfns & range->flags[HMM_PFN_WRITE]) &&
406 !(cpu_flags & range->flags[HMM_PFN_WRITE])) {
407 *write_fault = true;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700408 *fault = true;
409 }
410}
411
412static void hmm_range_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
413 const uint64_t *pfns, unsigned long npages,
414 uint64_t cpu_flags, bool *fault,
415 bool *write_fault)
416{
417 unsigned long i;
418
Christoph Hellwigd45d4642019-07-25 17:56:47 -0700419 if (hmm_vma_walk->flags & HMM_FAULT_SNAPSHOT) {
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700420 *fault = *write_fault = false;
421 return;
422 }
423
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700424 *fault = *write_fault = false;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700425 for (i = 0; i < npages; ++i) {
426 hmm_pte_need_fault(hmm_vma_walk, pfns[i], cpu_flags,
427 fault, write_fault);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700428 if ((*write_fault))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700429 return;
430 }
431}
432
433static int hmm_vma_walk_hole(unsigned long addr, unsigned long end,
434 struct mm_walk *walk)
435{
436 struct hmm_vma_walk *hmm_vma_walk = walk->private;
437 struct hmm_range *range = hmm_vma_walk->range;
438 bool fault, write_fault;
439 unsigned long i, npages;
440 uint64_t *pfns;
441
442 i = (addr - range->start) >> PAGE_SHIFT;
443 npages = (end - addr) >> PAGE_SHIFT;
444 pfns = &range->pfns[i];
445 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
446 0, &fault, &write_fault);
447 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
448}
449
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700450static inline uint64_t pmd_to_hmm_pfn_flags(struct hmm_range *range, pmd_t pmd)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700451{
452 if (pmd_protnone(pmd))
453 return 0;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700454 return pmd_write(pmd) ? range->flags[HMM_PFN_VALID] |
455 range->flags[HMM_PFN_WRITE] :
456 range->flags[HMM_PFN_VALID];
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700457}
458
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700459static inline uint64_t pud_to_hmm_pfn_flags(struct hmm_range *range, pud_t pud)
460{
461 if (!pud_present(pud))
462 return 0;
463 return pud_write(pud) ? range->flags[HMM_PFN_VALID] |
464 range->flags[HMM_PFN_WRITE] :
465 range->flags[HMM_PFN_VALID];
466}
467
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700468static int hmm_vma_handle_pmd(struct mm_walk *walk,
469 unsigned long addr,
470 unsigned long end,
471 uint64_t *pfns,
472 pmd_t pmd)
473{
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700474#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700475 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700476 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700477 unsigned long pfn, npages, i;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700478 bool fault, write_fault;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700479 uint64_t cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700480
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700481 npages = (end - addr) >> PAGE_SHIFT;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700482 cpu_flags = pmd_to_hmm_pfn_flags(range, pmd);
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700483 hmm_range_need_fault(hmm_vma_walk, pfns, npages, cpu_flags,
484 &fault, &write_fault);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700485
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700486 if (pmd_protnone(pmd) || fault || write_fault)
487 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700488
Christoph Hellwig309f9a42019-08-06 19:05:47 +0300489 pfn = pmd_pfn(pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700490 for (i = 0; addr < end; addr += PAGE_SIZE, i++, pfn++) {
491 if (pmd_devmap(pmd)) {
492 hmm_vma_walk->pgmap = get_dev_pagemap(pfn,
493 hmm_vma_walk->pgmap);
494 if (unlikely(!hmm_vma_walk->pgmap))
495 return -EBUSY;
496 }
Jérôme Glisse391aab12019-05-13 17:20:31 -0700497 pfns[i] = hmm_device_entry_from_pfn(range, pfn) | cpu_flags;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700498 }
499 if (hmm_vma_walk->pgmap) {
500 put_dev_pagemap(hmm_vma_walk->pgmap);
501 hmm_vma_walk->pgmap = NULL;
502 }
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700503 hmm_vma_walk->last = end;
504 return 0;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700505#else
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700506 /* If THP is not enabled then we should never reach this code ! */
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700507 return -EINVAL;
508#endif
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700509}
510
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700511static inline uint64_t pte_to_hmm_pfn_flags(struct hmm_range *range, pte_t pte)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700512{
Philip Yang789c2af2019-05-23 16:32:31 -0400513 if (pte_none(pte) || !pte_present(pte) || pte_protnone(pte))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700514 return 0;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700515 return pte_write(pte) ? range->flags[HMM_PFN_VALID] |
516 range->flags[HMM_PFN_WRITE] :
517 range->flags[HMM_PFN_VALID];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700518}
519
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700520static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,
521 unsigned long end, pmd_t *pmdp, pte_t *ptep,
522 uint64_t *pfn)
523{
524 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700525 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700526 bool fault, write_fault;
527 uint64_t cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700528 pte_t pte = *ptep;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700529 uint64_t orig_pfn = *pfn;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700530
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700531 *pfn = range->values[HMM_PFN_NONE];
Jérôme Glisse73231612019-05-13 17:19:58 -0700532 fault = write_fault = false;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700533
534 if (pte_none(pte)) {
Jérôme Glisse73231612019-05-13 17:19:58 -0700535 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, 0,
536 &fault, &write_fault);
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700537 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700538 goto fault;
539 return 0;
540 }
541
542 if (!pte_present(pte)) {
543 swp_entry_t entry = pte_to_swp_entry(pte);
544
545 if (!non_swap_entry(entry)) {
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700546 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700547 goto fault;
548 return 0;
549 }
550
551 /*
552 * This is a special swap entry, ignore migration, use
553 * device and report anything else as error.
554 */
555 if (is_device_private_entry(entry)) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700556 cpu_flags = range->flags[HMM_PFN_VALID] |
557 range->flags[HMM_PFN_DEVICE_PRIVATE];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700558 cpu_flags |= is_write_device_private_entry(entry) ?
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700559 range->flags[HMM_PFN_WRITE] : 0;
560 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
561 &fault, &write_fault);
562 if (fault || write_fault)
563 goto fault;
Jérôme Glisse391aab12019-05-13 17:20:31 -0700564 *pfn = hmm_device_entry_from_pfn(range,
565 swp_offset(entry));
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700566 *pfn |= cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700567 return 0;
568 }
569
570 if (is_migration_entry(entry)) {
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700571 if (fault || write_fault) {
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700572 pte_unmap(ptep);
573 hmm_vma_walk->last = addr;
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700574 migration_entry_wait(walk->mm, pmdp, addr);
Jérôme Glisse73231612019-05-13 17:19:58 -0700575 return -EBUSY;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700576 }
577 return 0;
578 }
579
580 /* Report error for everything else */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700581 *pfn = range->values[HMM_PFN_ERROR];
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700582 return -EFAULT;
Jérôme Glisse73231612019-05-13 17:19:58 -0700583 } else {
584 cpu_flags = pte_to_hmm_pfn_flags(range, pte);
585 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
586 &fault, &write_fault);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700587 }
588
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700589 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700590 goto fault;
591
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700592 if (pte_devmap(pte)) {
593 hmm_vma_walk->pgmap = get_dev_pagemap(pte_pfn(pte),
594 hmm_vma_walk->pgmap);
595 if (unlikely(!hmm_vma_walk->pgmap))
596 return -EBUSY;
597 } else if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) && pte_special(pte)) {
598 *pfn = range->values[HMM_PFN_SPECIAL];
599 return -EFAULT;
600 }
601
Jérôme Glisse391aab12019-05-13 17:20:31 -0700602 *pfn = hmm_device_entry_from_pfn(range, pte_pfn(pte)) | cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700603 return 0;
604
605fault:
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700606 if (hmm_vma_walk->pgmap) {
607 put_dev_pagemap(hmm_vma_walk->pgmap);
608 hmm_vma_walk->pgmap = NULL;
609 }
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700610 pte_unmap(ptep);
611 /* Fault any virtual address we were asked to fault */
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700612 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700613}
614
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700615static int hmm_vma_walk_pmd(pmd_t *pmdp,
616 unsigned long start,
617 unsigned long end,
618 struct mm_walk *walk)
619{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700620 struct hmm_vma_walk *hmm_vma_walk = walk->private;
621 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700622 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700623 unsigned long addr = start, i;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700624 pte_t *ptep;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700625 pmd_t pmd;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700626
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700627again:
Jérôme Glissed08faca2018-10-30 15:04:20 -0700628 pmd = READ_ONCE(*pmdp);
629 if (pmd_none(pmd))
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700630 return hmm_vma_walk_hole(start, end, walk);
631
Jérôme Glissed08faca2018-10-30 15:04:20 -0700632 if (thp_migration_supported() && is_pmd_migration_entry(pmd)) {
633 bool fault, write_fault;
634 unsigned long npages;
635 uint64_t *pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700636
Jérôme Glissed08faca2018-10-30 15:04:20 -0700637 i = (addr - range->start) >> PAGE_SHIFT;
638 npages = (end - addr) >> PAGE_SHIFT;
639 pfns = &range->pfns[i];
640
641 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
642 0, &fault, &write_fault);
643 if (fault || write_fault) {
644 hmm_vma_walk->last = addr;
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700645 pmd_migration_entry_wait(walk->mm, pmdp);
Jérôme Glisse73231612019-05-13 17:19:58 -0700646 return -EBUSY;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700647 }
648 return 0;
649 } else if (!pmd_present(pmd))
650 return hmm_pfns_bad(start, end, walk);
651
652 if (pmd_devmap(pmd) || pmd_trans_huge(pmd)) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700653 /*
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700654 * No need to take pmd_lock here, even if some other thread
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700655 * is splitting the huge pmd we will get that event through
656 * mmu_notifier callback.
657 *
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700658 * So just read pmd value and check again it's a transparent
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700659 * huge or device mapping one and compute corresponding pfn
660 * values.
661 */
662 pmd = pmd_read_atomic(pmdp);
663 barrier();
664 if (!pmd_devmap(pmd) && !pmd_trans_huge(pmd))
665 goto again;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700666
Jérôme Glissed08faca2018-10-30 15:04:20 -0700667 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700668 return hmm_vma_handle_pmd(walk, addr, end, &pfns[i], pmd);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700669 }
670
Jérôme Glissed08faca2018-10-30 15:04:20 -0700671 /*
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700672 * We have handled all the valid cases above ie either none, migration,
Jérôme Glissed08faca2018-10-30 15:04:20 -0700673 * huge or transparent huge. At this point either it is a valid pmd
674 * entry pointing to pte directory or it is a bad pmd that will not
675 * recover.
676 */
677 if (pmd_bad(pmd))
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700678 return hmm_pfns_bad(start, end, walk);
679
680 ptep = pte_offset_map(pmdp, addr);
Jérôme Glissed08faca2018-10-30 15:04:20 -0700681 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700682 for (; addr < end; addr += PAGE_SIZE, ptep++, i++) {
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700683 int r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700684
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700685 r = hmm_vma_handle_pte(walk, addr, end, pmdp, ptep, &pfns[i]);
686 if (r) {
687 /* hmm_vma_handle_pte() did unmap pte directory */
688 hmm_vma_walk->last = addr;
689 return r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700690 }
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700691 }
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700692 if (hmm_vma_walk->pgmap) {
693 /*
694 * We do put_dev_pagemap() here and not in hmm_vma_handle_pte()
695 * so that we can leverage get_dev_pagemap() optimization which
696 * will not re-take a reference on a pgmap if we already have
697 * one.
698 */
699 put_dev_pagemap(hmm_vma_walk->pgmap);
700 hmm_vma_walk->pgmap = NULL;
701 }
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700702 pte_unmap(ptep - 1);
703
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700704 hmm_vma_walk->last = addr;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700705 return 0;
706}
707
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700708static int hmm_vma_walk_pud(pud_t *pudp,
709 unsigned long start,
710 unsigned long end,
711 struct mm_walk *walk)
712{
713 struct hmm_vma_walk *hmm_vma_walk = walk->private;
714 struct hmm_range *range = hmm_vma_walk->range;
715 unsigned long addr = start, next;
716 pmd_t *pmdp;
717 pud_t pud;
718 int ret;
719
720again:
721 pud = READ_ONCE(*pudp);
722 if (pud_none(pud))
723 return hmm_vma_walk_hole(start, end, walk);
724
725 if (pud_huge(pud) && pud_devmap(pud)) {
726 unsigned long i, npages, pfn;
727 uint64_t *pfns, cpu_flags;
728 bool fault, write_fault;
729
730 if (!pud_present(pud))
731 return hmm_vma_walk_hole(start, end, walk);
732
733 i = (addr - range->start) >> PAGE_SHIFT;
734 npages = (end - addr) >> PAGE_SHIFT;
735 pfns = &range->pfns[i];
736
737 cpu_flags = pud_to_hmm_pfn_flags(range, pud);
738 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
739 cpu_flags, &fault, &write_fault);
740 if (fault || write_fault)
741 return hmm_vma_walk_hole_(addr, end, fault,
742 write_fault, walk);
743
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700744 pfn = pud_pfn(pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
745 for (i = 0; i < npages; ++i, ++pfn) {
746 hmm_vma_walk->pgmap = get_dev_pagemap(pfn,
747 hmm_vma_walk->pgmap);
748 if (unlikely(!hmm_vma_walk->pgmap))
749 return -EBUSY;
Jérôme Glisse391aab12019-05-13 17:20:31 -0700750 pfns[i] = hmm_device_entry_from_pfn(range, pfn) |
751 cpu_flags;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700752 }
753 if (hmm_vma_walk->pgmap) {
754 put_dev_pagemap(hmm_vma_walk->pgmap);
755 hmm_vma_walk->pgmap = NULL;
756 }
757 hmm_vma_walk->last = end;
758 return 0;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700759 }
760
761 split_huge_pud(walk->vma, pudp, addr);
762 if (pud_none(*pudp))
763 goto again;
764
765 pmdp = pmd_offset(pudp, addr);
766 do {
767 next = pmd_addr_end(addr, end);
768 ret = hmm_vma_walk_pmd(pmdp, addr, next, walk);
769 if (ret)
770 return ret;
771 } while (pmdp++, addr = next, addr != end);
772
773 return 0;
774}
775
Jérôme Glisse63d50662019-05-13 17:20:18 -0700776static int hmm_vma_walk_hugetlb_entry(pte_t *pte, unsigned long hmask,
777 unsigned long start, unsigned long end,
778 struct mm_walk *walk)
779{
780#ifdef CONFIG_HUGETLB_PAGE
Christoph Hellwig05c23af2019-08-06 19:05:46 +0300781 unsigned long addr = start, i, pfn;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700782 struct hmm_vma_walk *hmm_vma_walk = walk->private;
783 struct hmm_range *range = hmm_vma_walk->range;
784 struct vm_area_struct *vma = walk->vma;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700785 uint64_t orig_pfn, cpu_flags;
786 bool fault, write_fault;
787 spinlock_t *ptl;
788 pte_t entry;
789 int ret = 0;
790
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700791 ptl = huge_pte_lock(hstate_vma(vma), walk->mm, pte);
Jérôme Glisse63d50662019-05-13 17:20:18 -0700792 entry = huge_ptep_get(pte);
793
Christoph Hellwig7f082632019-08-06 19:05:45 +0300794 i = (start - range->start) >> PAGE_SHIFT;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700795 orig_pfn = range->pfns[i];
796 range->pfns[i] = range->values[HMM_PFN_NONE];
797 cpu_flags = pte_to_hmm_pfn_flags(range, entry);
798 fault = write_fault = false;
799 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
800 &fault, &write_fault);
801 if (fault || write_fault) {
802 ret = -ENOENT;
803 goto unlock;
804 }
805
Christoph Hellwig05c23af2019-08-06 19:05:46 +0300806 pfn = pte_pfn(entry) + ((start & ~hmask) >> PAGE_SHIFT);
Christoph Hellwig7f082632019-08-06 19:05:45 +0300807 for (; addr < end; addr += PAGE_SIZE, i++, pfn++)
Jérôme Glisse391aab12019-05-13 17:20:31 -0700808 range->pfns[i] = hmm_device_entry_from_pfn(range, pfn) |
809 cpu_flags;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700810 hmm_vma_walk->last = end;
811
812unlock:
813 spin_unlock(ptl);
814
815 if (ret == -ENOENT)
816 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
817
818 return ret;
819#else /* CONFIG_HUGETLB_PAGE */
820 return -EINVAL;
821#endif
822}
823
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700824static void hmm_pfns_clear(struct hmm_range *range,
825 uint64_t *pfns,
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700826 unsigned long addr,
827 unsigned long end)
828{
829 for (; addr < end; addr += PAGE_SIZE, pfns++)
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700830 *pfns = range->values[HMM_PFN_NONE];
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700831}
832
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700833/*
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700834 * hmm_range_register() - start tracking change to CPU page table over a range
Jérôme Glisse25f23a02019-05-13 17:19:55 -0700835 * @range: range
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700836 * @mm: the mm struct for the range of virtual address
Christoph Hellwigfac555a2019-08-06 19:05:44 +0300837 *
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700838 * Return: 0 on success, -EFAULT if the address space is no longer valid
Jérôme Glisse25f23a02019-05-13 17:19:55 -0700839 *
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700840 * Track updates to the CPU page table see include/linux/hmm.h
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700841 */
Christoph Hellwigfac555a2019-08-06 19:05:44 +0300842int hmm_range_register(struct hmm_range *range, struct hmm_mirror *mirror)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700843{
Jason Gunthorpee36acfe2019-05-23 09:41:19 -0300844 struct hmm *hmm = mirror->hmm;
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300845 unsigned long flags;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700846
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700847 range->valid = false;
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700848 range->hmm = NULL;
849
Christoph Hellwig7f082632019-08-06 19:05:45 +0300850 if ((range->start & (PAGE_SIZE - 1)) || (range->end & (PAGE_SIZE - 1)))
Jérôme Glisse63d50662019-05-13 17:20:18 -0700851 return -EINVAL;
Christoph Hellwigfac555a2019-08-06 19:05:44 +0300852 if (range->start >= range->end)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700853 return -EINVAL;
854
Jason Gunthorpe47f24592019-05-23 11:08:28 -0300855 /* Prevent hmm_release() from running while the range is valid */
856 if (!mmget_not_zero(hmm->mm))
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700857 return -EFAULT;
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700858
Ralph Campbell085ea252019-05-06 16:29:39 -0700859 /* Initialize range to track CPU page table updates. */
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300860 spin_lock_irqsave(&hmm->ranges_lock, flags);
Jérôme Glisse86586a42018-04-10 16:28:34 -0700861
Ralph Campbell085ea252019-05-06 16:29:39 -0700862 range->hmm = hmm;
Jason Gunthorpee36acfe2019-05-23 09:41:19 -0300863 kref_get(&hmm->kref);
Jason Gunthorpe157816f2019-05-23 11:43:43 -0300864 list_add(&range->list, &hmm->ranges);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700865
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700866 /*
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700867 * If there are any concurrent notifiers we have to wait for them for
868 * the range to be valid (see hmm_range_wait_until_valid()).
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700869 */
Ralph Campbell085ea252019-05-06 16:29:39 -0700870 if (!hmm->notifiers)
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700871 range->valid = true;
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300872 spin_unlock_irqrestore(&hmm->ranges_lock, flags);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700873
874 return 0;
875}
876EXPORT_SYMBOL(hmm_range_register);
877
878/*
879 * hmm_range_unregister() - stop tracking change to CPU page table over a range
880 * @range: range
881 *
882 * Range struct is used to track updates to the CPU page table after a call to
883 * hmm_range_register(). See include/linux/hmm.h for how to use it.
884 */
885void hmm_range_unregister(struct hmm_range *range)
886{
Ralph Campbell085ea252019-05-06 16:29:39 -0700887 struct hmm *hmm = range->hmm;
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300888 unsigned long flags;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700889
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300890 spin_lock_irqsave(&hmm->ranges_lock, flags);
Jason Gunthorpe47f24592019-05-23 11:08:28 -0300891 list_del_init(&range->list);
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300892 spin_unlock_irqrestore(&hmm->ranges_lock, flags);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700893
894 /* Drop reference taken by hmm_range_register() */
Jason Gunthorpe47f24592019-05-23 11:08:28 -0300895 mmput(hmm->mm);
Ralph Campbell085ea252019-05-06 16:29:39 -0700896 hmm_put(hmm);
Jason Gunthorpe2dcc3eb2019-05-23 11:40:24 -0300897
898 /*
899 * The range is now invalid and the ref on the hmm is dropped, so
900 * poison the pointer. Leave other fields in place, for the caller's
901 * use.
902 */
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700903 range->valid = false;
Jason Gunthorpe2dcc3eb2019-05-23 11:40:24 -0300904 memset(&range->hmm, POISON_INUSE, sizeof(range->hmm));
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700905}
906EXPORT_SYMBOL(hmm_range_unregister);
907
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700908/**
909 * hmm_range_fault - try to fault some address in a virtual address range
910 * @range: range being faulted
911 * @flags: HMM_FAULT_* flags
Jérôme Glisse73231612019-05-13 17:19:58 -0700912 *
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700913 * Return: the number of valid pages in range->pfns[] (from range start
914 * address), which may be zero. On error one of the following status codes
915 * can be returned:
916 *
917 * -EINVAL: Invalid arguments or mm or virtual address is in an invalid vma
918 * (e.g., device file vma).
919 * -ENOMEM: Out of memory.
920 * -EPERM: Invalid permission (e.g., asking for write and range is read
921 * only).
922 * -EAGAIN: A page fault needs to be retried and mmap_sem was dropped.
923 * -EBUSY: The range has been invalidated and the caller needs to wait for
924 * the invalidation to finish.
925 * -EFAULT: Invalid (i.e., either no valid vma or it is illegal to access
926 * that range) number of valid pages in range->pfns[] (from
927 * range start address).
Jérôme Glisse74eee182017-09-08 16:11:35 -0700928 *
929 * This is similar to a regular CPU page fault except that it will not trigger
Jérôme Glisse73231612019-05-13 17:19:58 -0700930 * any memory migration if the memory being faulted is not accessible by CPUs
931 * and caller does not ask for migration.
Jérôme Glisse74eee182017-09-08 16:11:35 -0700932 *
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700933 * On error, for one virtual address in the range, the function will mark the
934 * corresponding HMM pfn entry with an error flag.
Jérôme Glisse74eee182017-09-08 16:11:35 -0700935 */
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700936long hmm_range_fault(struct hmm_range *range, unsigned int flags)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700937{
Jérôme Glisse63d50662019-05-13 17:20:18 -0700938 const unsigned long device_vma = VM_IO | VM_PFNMAP | VM_MIXEDMAP;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700939 unsigned long start = range->start, end;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700940 struct hmm_vma_walk hmm_vma_walk;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700941 struct hmm *hmm = range->hmm;
942 struct vm_area_struct *vma;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700943 struct mm_walk mm_walk;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700944 int ret;
945
Jason Gunthorpe47f24592019-05-23 11:08:28 -0300946 lockdep_assert_held(&hmm->mm->mmap_sem);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700947
948 do {
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700949 /* If range is no longer valid force retry. */
Christoph Hellwig2bcbeae2019-07-24 08:52:52 +0200950 if (!range->valid)
951 return -EBUSY;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700952
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700953 vma = find_vma(hmm->mm, start);
Jérôme Glisse63d50662019-05-13 17:20:18 -0700954 if (vma == NULL || (vma->vm_flags & device_vma))
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700955 return -EFAULT;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700956
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700957 if (!(vma->vm_flags & VM_READ)) {
958 /*
959 * If vma do not allow read access, then assume that it
960 * does not allow write access, either. HMM does not
961 * support architecture that allow write without read.
962 */
963 hmm_pfns_clear(range, range->pfns,
964 range->start, range->end);
965 return -EPERM;
966 }
967
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700968 hmm_vma_walk.pgmap = NULL;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700969 hmm_vma_walk.last = start;
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700970 hmm_vma_walk.flags = flags;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700971 hmm_vma_walk.range = range;
972 mm_walk.private = &hmm_vma_walk;
973 end = min(range->end, vma->vm_end);
974
975 mm_walk.vma = vma;
976 mm_walk.mm = vma->vm_mm;
977 mm_walk.pte_entry = NULL;
978 mm_walk.test_walk = NULL;
979 mm_walk.hugetlb_entry = NULL;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700980 mm_walk.pud_entry = hmm_vma_walk_pud;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700981 mm_walk.pmd_entry = hmm_vma_walk_pmd;
982 mm_walk.pte_hole = hmm_vma_walk_hole;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700983 mm_walk.hugetlb_entry = hmm_vma_walk_hugetlb_entry;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700984
985 do {
986 ret = walk_page_range(start, end, &mm_walk);
987 start = hmm_vma_walk.last;
988
989 /* Keep trying while the range is valid. */
990 } while (ret == -EBUSY && range->valid);
991
992 if (ret) {
993 unsigned long i;
994
995 i = (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
996 hmm_pfns_clear(range, &range->pfns[i],
997 hmm_vma_walk.last, range->end);
998 return ret;
999 }
1000 start = end;
1001
1002 } while (start < range->end);
Jérôme Glisse704f3f22019-05-13 17:19:48 -07001003
Jérôme Glisse73231612019-05-13 17:19:58 -07001004 return (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
Jérôme Glisse74eee182017-09-08 16:11:35 -07001005}
Jérôme Glisse73231612019-05-13 17:19:58 -07001006EXPORT_SYMBOL(hmm_range_fault);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001007
1008/**
Christoph Hellwig9a4903e2019-07-25 17:56:46 -07001009 * hmm_range_dma_map - hmm_range_fault() and dma map page all in one.
1010 * @range: range being faulted
1011 * @device: device to map page to
1012 * @daddrs: array of dma addresses for the mapped pages
1013 * @flags: HMM_FAULT_*
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001014 *
Christoph Hellwig9a4903e2019-07-25 17:56:46 -07001015 * Return: the number of pages mapped on success (including zero), or any
1016 * status return from hmm_range_fault() otherwise.
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001017 */
Christoph Hellwig9a4903e2019-07-25 17:56:46 -07001018long hmm_range_dma_map(struct hmm_range *range, struct device *device,
1019 dma_addr_t *daddrs, unsigned int flags)
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001020{
1021 unsigned long i, npages, mapped;
1022 long ret;
1023
Christoph Hellwig9a4903e2019-07-25 17:56:46 -07001024 ret = hmm_range_fault(range, flags);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001025 if (ret <= 0)
1026 return ret ? ret : -EBUSY;
1027
1028 npages = (range->end - range->start) >> PAGE_SHIFT;
1029 for (i = 0, mapped = 0; i < npages; ++i) {
1030 enum dma_data_direction dir = DMA_TO_DEVICE;
1031 struct page *page;
1032
1033 /*
1034 * FIXME need to update DMA API to provide invalid DMA address
1035 * value instead of a function to test dma address value. This
1036 * would remove lot of dumb code duplicated accross many arch.
1037 *
1038 * For now setting it to 0 here is good enough as the pfns[]
1039 * value is what is use to check what is valid and what isn't.
1040 */
1041 daddrs[i] = 0;
1042
Jérôme Glisse391aab12019-05-13 17:20:31 -07001043 page = hmm_device_entry_to_page(range, range->pfns[i]);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001044 if (page == NULL)
1045 continue;
1046
1047 /* Check if range is being invalidated */
1048 if (!range->valid) {
1049 ret = -EBUSY;
1050 goto unmap;
1051 }
1052
1053 /* If it is read and write than map bi-directional. */
1054 if (range->pfns[i] & range->flags[HMM_PFN_WRITE])
1055 dir = DMA_BIDIRECTIONAL;
1056
1057 daddrs[i] = dma_map_page(device, page, 0, PAGE_SIZE, dir);
1058 if (dma_mapping_error(device, daddrs[i])) {
1059 ret = -EFAULT;
1060 goto unmap;
1061 }
1062
1063 mapped++;
1064 }
1065
1066 return mapped;
1067
1068unmap:
1069 for (npages = i, i = 0; (i < npages) && mapped; ++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 (dma_mapping_error(device, daddrs[i]))
1078 continue;
1079
1080 /* If it is read and write than map bi-directional. */
1081 if (range->pfns[i] & range->flags[HMM_PFN_WRITE])
1082 dir = DMA_BIDIRECTIONAL;
1083
1084 dma_unmap_page(device, daddrs[i], PAGE_SIZE, dir);
1085 mapped--;
1086 }
1087
1088 return ret;
1089}
1090EXPORT_SYMBOL(hmm_range_dma_map);
1091
1092/**
1093 * hmm_range_dma_unmap() - unmap range of that was map with hmm_range_dma_map()
1094 * @range: range being unmapped
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001095 * @device: device against which dma map was done
1096 * @daddrs: dma address of mapped pages
1097 * @dirty: dirty page if it had the write flag set
Ralph Campbell085ea252019-05-06 16:29:39 -07001098 * Return: number of page unmapped on success, -EINVAL otherwise
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001099 *
1100 * Note that caller MUST abide by mmu notifier or use HMM mirror and abide
1101 * to the sync_cpu_device_pagetables() callback so that it is safe here to
1102 * call set_page_dirty(). Caller must also take appropriate locks to avoid
1103 * concurrent mmu notifier or sync_cpu_device_pagetables() to make progress.
1104 */
1105long hmm_range_dma_unmap(struct hmm_range *range,
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001106 struct device *device,
1107 dma_addr_t *daddrs,
1108 bool dirty)
1109{
1110 unsigned long i, npages;
1111 long cpages = 0;
1112
1113 /* Sanity check. */
1114 if (range->end <= range->start)
1115 return -EINVAL;
1116 if (!daddrs)
1117 return -EINVAL;
1118 if (!range->pfns)
1119 return -EINVAL;
1120
1121 npages = (range->end - range->start) >> PAGE_SHIFT;
1122 for (i = 0; i < npages; ++i) {
1123 enum dma_data_direction dir = DMA_TO_DEVICE;
1124 struct page *page;
1125
Jérôme Glisse391aab12019-05-13 17:20:31 -07001126 page = hmm_device_entry_to_page(range, range->pfns[i]);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001127 if (page == NULL)
1128 continue;
1129
1130 /* If it is read and write than map bi-directional. */
1131 if (range->pfns[i] & range->flags[HMM_PFN_WRITE]) {
1132 dir = DMA_BIDIRECTIONAL;
1133
1134 /*
1135 * See comments in function description on why it is
1136 * safe here to call set_page_dirty()
1137 */
1138 if (dirty)
1139 set_page_dirty(page);
1140 }
1141
1142 /* Unmap and clear pfns/dma address */
1143 dma_unmap_page(device, daddrs[i], PAGE_SIZE, dir);
1144 range->pfns[i] = range->values[HMM_PFN_NONE];
1145 /* FIXME see comments in hmm_vma_dma_map() */
1146 daddrs[i] = 0;
1147 cpages++;
1148 }
1149
1150 return cpages;
1151}
1152EXPORT_SYMBOL(hmm_range_dma_unmap);