blob: 84f2791d3510214886fbac1eecd548dde82eed83 [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;
283 bool fault;
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700284 unsigned int flags;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700285};
286
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700287static int hmm_vma_do_fault(struct mm_walk *walk, unsigned long addr,
288 bool write_fault, uint64_t *pfn)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700289{
Kuehling, Felix9b1ae602019-05-10 19:53:24 +0000290 unsigned int flags = FAULT_FLAG_REMOTE;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700291 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700292 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700293 struct vm_area_struct *vma = walk->vma;
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700294 vm_fault_t ret;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700295
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700296 if (hmm_vma_walk->flags & HMM_FAULT_ALLOW_RETRY)
297 flags |= FAULT_FLAG_ALLOW_RETRY;
298 if (write_fault)
299 flags |= FAULT_FLAG_WRITE;
300
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700301 ret = handle_mm_fault(vma, addr, flags);
Jason Gunthorpee709acc2019-07-24 08:52:58 +0200302 if (ret & VM_FAULT_RETRY) {
303 /* Note, handle_mm_fault did up_read(&mm->mmap_sem)) */
Jérôme Glisse73231612019-05-13 17:19:58 -0700304 return -EAGAIN;
Jason Gunthorpee709acc2019-07-24 08:52:58 +0200305 }
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700306 if (ret & VM_FAULT_ERROR) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700307 *pfn = range->values[HMM_PFN_ERROR];
Jérôme Glisse74eee182017-09-08 16:11:35 -0700308 return -EFAULT;
309 }
310
Jérôme Glisse73231612019-05-13 17:19:58 -0700311 return -EBUSY;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700312}
313
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700314static int hmm_pfns_bad(unsigned long addr,
315 unsigned long end,
316 struct mm_walk *walk)
317{
Jérôme Glissec7195472018-04-10 16:28:27 -0700318 struct hmm_vma_walk *hmm_vma_walk = walk->private;
319 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700320 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700321 unsigned long i;
322
323 i = (addr - range->start) >> PAGE_SHIFT;
324 for (; addr < end; addr += PAGE_SIZE, i++)
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700325 pfns[i] = range->values[HMM_PFN_ERROR];
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700326
327 return 0;
328}
329
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700330/*
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700331 * hmm_vma_walk_hole_() - handle a range lacking valid pmd or pte(s)
332 * @addr: range virtual start address (inclusive)
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700333 * @end: range virtual end address (exclusive)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700334 * @fault: should we fault or not ?
335 * @write_fault: write fault ?
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700336 * @walk: mm_walk structure
Ralph Campbell085ea252019-05-06 16:29:39 -0700337 * Return: 0 on success, -EBUSY after page fault, or page fault error
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700338 *
339 * This function will be called whenever pmd_none() or pte_none() returns true,
340 * or whenever there is no page directory covering the virtual address range.
341 */
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700342static int hmm_vma_walk_hole_(unsigned long addr, unsigned long end,
343 bool fault, bool write_fault,
344 struct mm_walk *walk)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700345{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700346 struct hmm_vma_walk *hmm_vma_walk = walk->private;
347 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700348 uint64_t *pfns = range->pfns;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700349 unsigned long i, page_size;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700350
Jérôme Glisse74eee182017-09-08 16:11:35 -0700351 hmm_vma_walk->last = addr;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700352 page_size = hmm_range_page_size(range);
353 i = (addr - range->start) >> range->page_shift;
354
355 for (; addr < end; addr += page_size, i++) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700356 pfns[i] = range->values[HMM_PFN_NONE];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700357 if (fault || write_fault) {
Jérôme Glisse74eee182017-09-08 16:11:35 -0700358 int ret;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700359
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700360 ret = hmm_vma_do_fault(walk, addr, write_fault,
361 &pfns[i]);
Jérôme Glisse73231612019-05-13 17:19:58 -0700362 if (ret != -EBUSY)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700363 return ret;
364 }
365 }
366
Jérôme Glisse73231612019-05-13 17:19:58 -0700367 return (fault || write_fault) ? -EBUSY : 0;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700368}
369
370static inline void hmm_pte_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
371 uint64_t pfns, uint64_t cpu_flags,
372 bool *fault, bool *write_fault)
373{
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700374 struct hmm_range *range = hmm_vma_walk->range;
375
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700376 if (!hmm_vma_walk->fault)
377 return;
378
Jérôme Glisse023a0192019-05-13 17:20:05 -0700379 /*
380 * So we not only consider the individual per page request we also
381 * consider the default flags requested for the range. The API can
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700382 * be used 2 ways. The first one where the HMM user coalesces
383 * multiple page faults into one request and sets flags per pfn for
384 * those faults. The second one where the HMM user wants to pre-
Jérôme Glisse023a0192019-05-13 17:20:05 -0700385 * fault a range with specific flags. For the latter one it is a
386 * waste to have the user pre-fill the pfn arrays with a default
387 * flags value.
388 */
389 pfns = (pfns & range->pfn_flags_mask) | range->default_flags;
390
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700391 /* We aren't ask to do anything ... */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700392 if (!(pfns & range->flags[HMM_PFN_VALID]))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700393 return;
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700394 /* If this is device memory then only fault if explicitly requested */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700395 if ((cpu_flags & range->flags[HMM_PFN_DEVICE_PRIVATE])) {
396 /* Do we fault on device memory ? */
397 if (pfns & range->flags[HMM_PFN_DEVICE_PRIVATE]) {
398 *write_fault = pfns & range->flags[HMM_PFN_WRITE];
399 *fault = true;
400 }
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700401 return;
402 }
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700403
404 /* If CPU page table is not valid then we need to fault */
405 *fault = !(cpu_flags & range->flags[HMM_PFN_VALID]);
406 /* Need to write fault ? */
407 if ((pfns & range->flags[HMM_PFN_WRITE]) &&
408 !(cpu_flags & range->flags[HMM_PFN_WRITE])) {
409 *write_fault = true;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700410 *fault = true;
411 }
412}
413
414static void hmm_range_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
415 const uint64_t *pfns, unsigned long npages,
416 uint64_t cpu_flags, bool *fault,
417 bool *write_fault)
418{
419 unsigned long i;
420
421 if (!hmm_vma_walk->fault) {
422 *fault = *write_fault = false;
423 return;
424 }
425
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700426 *fault = *write_fault = false;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700427 for (i = 0; i < npages; ++i) {
428 hmm_pte_need_fault(hmm_vma_walk, pfns[i], cpu_flags,
429 fault, write_fault);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700430 if ((*write_fault))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700431 return;
432 }
433}
434
435static int hmm_vma_walk_hole(unsigned long addr, unsigned long end,
436 struct mm_walk *walk)
437{
438 struct hmm_vma_walk *hmm_vma_walk = walk->private;
439 struct hmm_range *range = hmm_vma_walk->range;
440 bool fault, write_fault;
441 unsigned long i, npages;
442 uint64_t *pfns;
443
444 i = (addr - range->start) >> PAGE_SHIFT;
445 npages = (end - addr) >> PAGE_SHIFT;
446 pfns = &range->pfns[i];
447 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
448 0, &fault, &write_fault);
449 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
450}
451
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700452static inline uint64_t pmd_to_hmm_pfn_flags(struct hmm_range *range, pmd_t pmd)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700453{
454 if (pmd_protnone(pmd))
455 return 0;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700456 return pmd_write(pmd) ? range->flags[HMM_PFN_VALID] |
457 range->flags[HMM_PFN_WRITE] :
458 range->flags[HMM_PFN_VALID];
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700459}
460
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700461static inline uint64_t pud_to_hmm_pfn_flags(struct hmm_range *range, pud_t pud)
462{
463 if (!pud_present(pud))
464 return 0;
465 return pud_write(pud) ? range->flags[HMM_PFN_VALID] |
466 range->flags[HMM_PFN_WRITE] :
467 range->flags[HMM_PFN_VALID];
468}
469
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700470static int hmm_vma_handle_pmd(struct mm_walk *walk,
471 unsigned long addr,
472 unsigned long end,
473 uint64_t *pfns,
474 pmd_t pmd)
475{
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700476#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700477 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700478 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700479 unsigned long pfn, npages, i;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700480 bool fault, write_fault;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700481 uint64_t cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700482
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700483 npages = (end - addr) >> PAGE_SHIFT;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700484 cpu_flags = pmd_to_hmm_pfn_flags(range, pmd);
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700485 hmm_range_need_fault(hmm_vma_walk, pfns, npages, cpu_flags,
486 &fault, &write_fault);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700487
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700488 if (pmd_protnone(pmd) || fault || write_fault)
489 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700490
491 pfn = pmd_pfn(pmd) + pte_index(addr);
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700492 for (i = 0; addr < end; addr += PAGE_SIZE, i++, pfn++) {
493 if (pmd_devmap(pmd)) {
494 hmm_vma_walk->pgmap = get_dev_pagemap(pfn,
495 hmm_vma_walk->pgmap);
496 if (unlikely(!hmm_vma_walk->pgmap))
497 return -EBUSY;
498 }
Jérôme Glisse391aab12019-05-13 17:20:31 -0700499 pfns[i] = hmm_device_entry_from_pfn(range, pfn) | cpu_flags;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700500 }
501 if (hmm_vma_walk->pgmap) {
502 put_dev_pagemap(hmm_vma_walk->pgmap);
503 hmm_vma_walk->pgmap = NULL;
504 }
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700505 hmm_vma_walk->last = end;
506 return 0;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700507#else
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700508 /* If THP is not enabled then we should never reach this code ! */
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700509 return -EINVAL;
510#endif
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700511}
512
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700513static inline uint64_t pte_to_hmm_pfn_flags(struct hmm_range *range, pte_t pte)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700514{
Philip Yang789c2af2019-05-23 16:32:31 -0400515 if (pte_none(pte) || !pte_present(pte) || pte_protnone(pte))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700516 return 0;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700517 return pte_write(pte) ? range->flags[HMM_PFN_VALID] |
518 range->flags[HMM_PFN_WRITE] :
519 range->flags[HMM_PFN_VALID];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700520}
521
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700522static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,
523 unsigned long end, pmd_t *pmdp, pte_t *ptep,
524 uint64_t *pfn)
525{
526 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700527 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700528 bool fault, write_fault;
529 uint64_t cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700530 pte_t pte = *ptep;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700531 uint64_t orig_pfn = *pfn;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700532
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700533 *pfn = range->values[HMM_PFN_NONE];
Jérôme Glisse73231612019-05-13 17:19:58 -0700534 fault = write_fault = false;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700535
536 if (pte_none(pte)) {
Jérôme Glisse73231612019-05-13 17:19:58 -0700537 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, 0,
538 &fault, &write_fault);
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700539 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700540 goto fault;
541 return 0;
542 }
543
544 if (!pte_present(pte)) {
545 swp_entry_t entry = pte_to_swp_entry(pte);
546
547 if (!non_swap_entry(entry)) {
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700548 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700549 goto fault;
550 return 0;
551 }
552
553 /*
554 * This is a special swap entry, ignore migration, use
555 * device and report anything else as error.
556 */
557 if (is_device_private_entry(entry)) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700558 cpu_flags = range->flags[HMM_PFN_VALID] |
559 range->flags[HMM_PFN_DEVICE_PRIVATE];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700560 cpu_flags |= is_write_device_private_entry(entry) ?
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700561 range->flags[HMM_PFN_WRITE] : 0;
562 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
563 &fault, &write_fault);
564 if (fault || write_fault)
565 goto fault;
Jérôme Glisse391aab12019-05-13 17:20:31 -0700566 *pfn = hmm_device_entry_from_pfn(range,
567 swp_offset(entry));
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700568 *pfn |= cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700569 return 0;
570 }
571
572 if (is_migration_entry(entry)) {
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700573 if (fault || write_fault) {
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700574 pte_unmap(ptep);
575 hmm_vma_walk->last = addr;
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700576 migration_entry_wait(walk->mm, pmdp, addr);
Jérôme Glisse73231612019-05-13 17:19:58 -0700577 return -EBUSY;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700578 }
579 return 0;
580 }
581
582 /* Report error for everything else */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700583 *pfn = range->values[HMM_PFN_ERROR];
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700584 return -EFAULT;
Jérôme Glisse73231612019-05-13 17:19:58 -0700585 } else {
586 cpu_flags = pte_to_hmm_pfn_flags(range, pte);
587 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
588 &fault, &write_fault);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700589 }
590
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700591 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700592 goto fault;
593
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700594 if (pte_devmap(pte)) {
595 hmm_vma_walk->pgmap = get_dev_pagemap(pte_pfn(pte),
596 hmm_vma_walk->pgmap);
597 if (unlikely(!hmm_vma_walk->pgmap))
598 return -EBUSY;
599 } else if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) && pte_special(pte)) {
600 *pfn = range->values[HMM_PFN_SPECIAL];
601 return -EFAULT;
602 }
603
Jérôme Glisse391aab12019-05-13 17:20:31 -0700604 *pfn = hmm_device_entry_from_pfn(range, pte_pfn(pte)) | cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700605 return 0;
606
607fault:
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700608 if (hmm_vma_walk->pgmap) {
609 put_dev_pagemap(hmm_vma_walk->pgmap);
610 hmm_vma_walk->pgmap = NULL;
611 }
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700612 pte_unmap(ptep);
613 /* Fault any virtual address we were asked to fault */
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700614 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700615}
616
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700617static int hmm_vma_walk_pmd(pmd_t *pmdp,
618 unsigned long start,
619 unsigned long end,
620 struct mm_walk *walk)
621{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700622 struct hmm_vma_walk *hmm_vma_walk = walk->private;
623 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700624 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700625 unsigned long addr = start, i;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700626 pte_t *ptep;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700627 pmd_t pmd;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700628
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700629again:
Jérôme Glissed08faca2018-10-30 15:04:20 -0700630 pmd = READ_ONCE(*pmdp);
631 if (pmd_none(pmd))
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700632 return hmm_vma_walk_hole(start, end, walk);
633
Jérôme Glissed08faca2018-10-30 15:04:20 -0700634 if (pmd_huge(pmd) && (range->vma->vm_flags & VM_HUGETLB))
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700635 return hmm_pfns_bad(start, end, walk);
636
Jérôme Glissed08faca2018-10-30 15:04:20 -0700637 if (thp_migration_supported() && is_pmd_migration_entry(pmd)) {
638 bool fault, write_fault;
639 unsigned long npages;
640 uint64_t *pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700641
Jérôme Glissed08faca2018-10-30 15:04:20 -0700642 i = (addr - range->start) >> PAGE_SHIFT;
643 npages = (end - addr) >> PAGE_SHIFT;
644 pfns = &range->pfns[i];
645
646 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
647 0, &fault, &write_fault);
648 if (fault || write_fault) {
649 hmm_vma_walk->last = addr;
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700650 pmd_migration_entry_wait(walk->mm, pmdp);
Jérôme Glisse73231612019-05-13 17:19:58 -0700651 return -EBUSY;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700652 }
653 return 0;
654 } else if (!pmd_present(pmd))
655 return hmm_pfns_bad(start, end, walk);
656
657 if (pmd_devmap(pmd) || pmd_trans_huge(pmd)) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700658 /*
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700659 * No need to take pmd_lock here, even if some other thread
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700660 * is splitting the huge pmd we will get that event through
661 * mmu_notifier callback.
662 *
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700663 * So just read pmd value and check again it's a transparent
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700664 * huge or device mapping one and compute corresponding pfn
665 * values.
666 */
667 pmd = pmd_read_atomic(pmdp);
668 barrier();
669 if (!pmd_devmap(pmd) && !pmd_trans_huge(pmd))
670 goto again;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700671
Jérôme Glissed08faca2018-10-30 15:04:20 -0700672 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700673 return hmm_vma_handle_pmd(walk, addr, end, &pfns[i], pmd);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700674 }
675
Jérôme Glissed08faca2018-10-30 15:04:20 -0700676 /*
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700677 * We have handled all the valid cases above ie either none, migration,
Jérôme Glissed08faca2018-10-30 15:04:20 -0700678 * huge or transparent huge. At this point either it is a valid pmd
679 * entry pointing to pte directory or it is a bad pmd that will not
680 * recover.
681 */
682 if (pmd_bad(pmd))
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700683 return hmm_pfns_bad(start, end, walk);
684
685 ptep = pte_offset_map(pmdp, addr);
Jérôme Glissed08faca2018-10-30 15:04:20 -0700686 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700687 for (; addr < end; addr += PAGE_SIZE, ptep++, i++) {
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700688 int r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700689
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700690 r = hmm_vma_handle_pte(walk, addr, end, pmdp, ptep, &pfns[i]);
691 if (r) {
692 /* hmm_vma_handle_pte() did unmap pte directory */
693 hmm_vma_walk->last = addr;
694 return r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700695 }
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700696 }
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700697 if (hmm_vma_walk->pgmap) {
698 /*
699 * We do put_dev_pagemap() here and not in hmm_vma_handle_pte()
700 * so that we can leverage get_dev_pagemap() optimization which
701 * will not re-take a reference on a pgmap if we already have
702 * one.
703 */
704 put_dev_pagemap(hmm_vma_walk->pgmap);
705 hmm_vma_walk->pgmap = NULL;
706 }
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700707 pte_unmap(ptep - 1);
708
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700709 hmm_vma_walk->last = addr;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700710 return 0;
711}
712
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700713static int hmm_vma_walk_pud(pud_t *pudp,
714 unsigned long start,
715 unsigned long end,
716 struct mm_walk *walk)
717{
718 struct hmm_vma_walk *hmm_vma_walk = walk->private;
719 struct hmm_range *range = hmm_vma_walk->range;
720 unsigned long addr = start, next;
721 pmd_t *pmdp;
722 pud_t pud;
723 int ret;
724
725again:
726 pud = READ_ONCE(*pudp);
727 if (pud_none(pud))
728 return hmm_vma_walk_hole(start, end, walk);
729
730 if (pud_huge(pud) && pud_devmap(pud)) {
731 unsigned long i, npages, pfn;
732 uint64_t *pfns, cpu_flags;
733 bool fault, write_fault;
734
735 if (!pud_present(pud))
736 return hmm_vma_walk_hole(start, end, walk);
737
738 i = (addr - range->start) >> PAGE_SHIFT;
739 npages = (end - addr) >> PAGE_SHIFT;
740 pfns = &range->pfns[i];
741
742 cpu_flags = pud_to_hmm_pfn_flags(range, pud);
743 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
744 cpu_flags, &fault, &write_fault);
745 if (fault || write_fault)
746 return hmm_vma_walk_hole_(addr, end, fault,
747 write_fault, walk);
748
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700749 pfn = pud_pfn(pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
750 for (i = 0; i < npages; ++i, ++pfn) {
751 hmm_vma_walk->pgmap = get_dev_pagemap(pfn,
752 hmm_vma_walk->pgmap);
753 if (unlikely(!hmm_vma_walk->pgmap))
754 return -EBUSY;
Jérôme Glisse391aab12019-05-13 17:20:31 -0700755 pfns[i] = hmm_device_entry_from_pfn(range, pfn) |
756 cpu_flags;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700757 }
758 if (hmm_vma_walk->pgmap) {
759 put_dev_pagemap(hmm_vma_walk->pgmap);
760 hmm_vma_walk->pgmap = NULL;
761 }
762 hmm_vma_walk->last = end;
763 return 0;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700764 }
765
766 split_huge_pud(walk->vma, pudp, addr);
767 if (pud_none(*pudp))
768 goto again;
769
770 pmdp = pmd_offset(pudp, addr);
771 do {
772 next = pmd_addr_end(addr, end);
773 ret = hmm_vma_walk_pmd(pmdp, addr, next, walk);
774 if (ret)
775 return ret;
776 } while (pmdp++, addr = next, addr != end);
777
778 return 0;
779}
780
Jérôme Glisse63d50662019-05-13 17:20:18 -0700781static int hmm_vma_walk_hugetlb_entry(pte_t *pte, unsigned long hmask,
782 unsigned long start, unsigned long end,
783 struct mm_walk *walk)
784{
785#ifdef CONFIG_HUGETLB_PAGE
786 unsigned long addr = start, i, pfn, mask, size, pfn_inc;
787 struct hmm_vma_walk *hmm_vma_walk = walk->private;
788 struct hmm_range *range = hmm_vma_walk->range;
789 struct vm_area_struct *vma = walk->vma;
790 struct hstate *h = hstate_vma(vma);
791 uint64_t orig_pfn, cpu_flags;
792 bool fault, write_fault;
793 spinlock_t *ptl;
794 pte_t entry;
795 int ret = 0;
796
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700797 size = huge_page_size(h);
Jérôme Glisse63d50662019-05-13 17:20:18 -0700798 mask = size - 1;
799 if (range->page_shift != PAGE_SHIFT) {
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700800 /* Make sure we are looking at a full page. */
Jérôme Glisse63d50662019-05-13 17:20:18 -0700801 if (start & mask)
802 return -EINVAL;
803 if (end < (start + size))
804 return -EINVAL;
805 pfn_inc = size >> PAGE_SHIFT;
806 } else {
807 pfn_inc = 1;
808 size = PAGE_SIZE;
809 }
810
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700811 ptl = huge_pte_lock(hstate_vma(vma), walk->mm, pte);
Jérôme Glisse63d50662019-05-13 17:20:18 -0700812 entry = huge_ptep_get(pte);
813
814 i = (start - range->start) >> range->page_shift;
815 orig_pfn = range->pfns[i];
816 range->pfns[i] = range->values[HMM_PFN_NONE];
817 cpu_flags = pte_to_hmm_pfn_flags(range, entry);
818 fault = write_fault = false;
819 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
820 &fault, &write_fault);
821 if (fault || write_fault) {
822 ret = -ENOENT;
823 goto unlock;
824 }
825
826 pfn = pte_pfn(entry) + ((start & mask) >> range->page_shift);
827 for (; addr < end; addr += size, i++, pfn += pfn_inc)
Jérôme Glisse391aab12019-05-13 17:20:31 -0700828 range->pfns[i] = hmm_device_entry_from_pfn(range, pfn) |
829 cpu_flags;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700830 hmm_vma_walk->last = end;
831
832unlock:
833 spin_unlock(ptl);
834
835 if (ret == -ENOENT)
836 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
837
838 return ret;
839#else /* CONFIG_HUGETLB_PAGE */
840 return -EINVAL;
841#endif
842}
843
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700844static void hmm_pfns_clear(struct hmm_range *range,
845 uint64_t *pfns,
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700846 unsigned long addr,
847 unsigned long end)
848{
849 for (; addr < end; addr += PAGE_SIZE, pfns++)
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700850 *pfns = range->values[HMM_PFN_NONE];
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700851}
852
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700853/*
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700854 * hmm_range_register() - start tracking change to CPU page table over a range
Jérôme Glisse25f23a02019-05-13 17:19:55 -0700855 * @range: range
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700856 * @mm: the mm struct for the range of virtual address
857 * @start: start virtual address (inclusive)
858 * @end: end virtual address (exclusive)
Jérôme Glisse63d50662019-05-13 17:20:18 -0700859 * @page_shift: expect page shift for the range
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700860 * Return: 0 on success, -EFAULT if the address space is no longer valid
Jérôme Glisse25f23a02019-05-13 17:19:55 -0700861 *
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700862 * Track updates to the CPU page table see include/linux/hmm.h
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700863 */
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700864int hmm_range_register(struct hmm_range *range,
Jason Gunthorpee36acfe2019-05-23 09:41:19 -0300865 struct hmm_mirror *mirror,
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700866 unsigned long start,
Jérôme Glisse63d50662019-05-13 17:20:18 -0700867 unsigned long end,
868 unsigned page_shift)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700869{
Jérôme Glisse63d50662019-05-13 17:20:18 -0700870 unsigned long mask = ((1UL << page_shift) - 1UL);
Jason Gunthorpee36acfe2019-05-23 09:41:19 -0300871 struct hmm *hmm = mirror->hmm;
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300872 unsigned long flags;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700873
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700874 range->valid = false;
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700875 range->hmm = NULL;
876
Jérôme Glisse63d50662019-05-13 17:20:18 -0700877 if ((start & mask) || (end & mask))
878 return -EINVAL;
879 if (start >= end)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700880 return -EINVAL;
881
Jérôme Glisse63d50662019-05-13 17:20:18 -0700882 range->page_shift = page_shift;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700883 range->start = start;
884 range->end = end;
885
Jason Gunthorpe47f24592019-05-23 11:08:28 -0300886 /* Prevent hmm_release() from running while the range is valid */
887 if (!mmget_not_zero(hmm->mm))
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700888 return -EFAULT;
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700889
Ralph Campbell085ea252019-05-06 16:29:39 -0700890 /* Initialize range to track CPU page table updates. */
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300891 spin_lock_irqsave(&hmm->ranges_lock, flags);
Jérôme Glisse86586a42018-04-10 16:28:34 -0700892
Ralph Campbell085ea252019-05-06 16:29:39 -0700893 range->hmm = hmm;
Jason Gunthorpee36acfe2019-05-23 09:41:19 -0300894 kref_get(&hmm->kref);
Jason Gunthorpe157816f2019-05-23 11:43:43 -0300895 list_add(&range->list, &hmm->ranges);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700896
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700897 /*
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700898 * If there are any concurrent notifiers we have to wait for them for
899 * the range to be valid (see hmm_range_wait_until_valid()).
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700900 */
Ralph Campbell085ea252019-05-06 16:29:39 -0700901 if (!hmm->notifiers)
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700902 range->valid = true;
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300903 spin_unlock_irqrestore(&hmm->ranges_lock, flags);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700904
905 return 0;
906}
907EXPORT_SYMBOL(hmm_range_register);
908
909/*
910 * hmm_range_unregister() - stop tracking change to CPU page table over a range
911 * @range: range
912 *
913 * Range struct is used to track updates to the CPU page table after a call to
914 * hmm_range_register(). See include/linux/hmm.h for how to use it.
915 */
916void hmm_range_unregister(struct hmm_range *range)
917{
Ralph Campbell085ea252019-05-06 16:29:39 -0700918 struct hmm *hmm = range->hmm;
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300919 unsigned long flags;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700920
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300921 spin_lock_irqsave(&hmm->ranges_lock, flags);
Jason Gunthorpe47f24592019-05-23 11:08:28 -0300922 list_del_init(&range->list);
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300923 spin_unlock_irqrestore(&hmm->ranges_lock, flags);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700924
925 /* Drop reference taken by hmm_range_register() */
Jason Gunthorpe47f24592019-05-23 11:08:28 -0300926 mmput(hmm->mm);
Ralph Campbell085ea252019-05-06 16:29:39 -0700927 hmm_put(hmm);
Jason Gunthorpe2dcc3eb2019-05-23 11:40:24 -0300928
929 /*
930 * The range is now invalid and the ref on the hmm is dropped, so
931 * poison the pointer. Leave other fields in place, for the caller's
932 * use.
933 */
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700934 range->valid = false;
Jason Gunthorpe2dcc3eb2019-05-23 11:40:24 -0300935 memset(&range->hmm, POISON_INUSE, sizeof(range->hmm));
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700936}
937EXPORT_SYMBOL(hmm_range_unregister);
938
939/*
940 * hmm_range_snapshot() - snapshot CPU page table for a range
941 * @range: range
Ralph Campbell085ea252019-05-06 16:29:39 -0700942 * Return: -EINVAL if invalid argument, -ENOMEM out of memory, -EPERM invalid
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700943 * permission (for instance asking for write and range is read only),
Christoph Hellwig2bcbeae2019-07-24 08:52:52 +0200944 * -EBUSY if you need to retry, -EFAULT invalid (ie either no valid
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700945 * vma or it is illegal to access that range), number of valid pages
946 * in range->pfns[] (from range start address).
947 *
948 * This snapshots the CPU page table for a range of virtual addresses. Snapshot
949 * validity is tracked by range struct. See in include/linux/hmm.h for example
950 * on how to use.
951 */
952long hmm_range_snapshot(struct hmm_range *range)
953{
Jérôme Glisse63d50662019-05-13 17:20:18 -0700954 const unsigned long device_vma = VM_IO | VM_PFNMAP | VM_MIXEDMAP;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700955 unsigned long start = range->start, end;
956 struct hmm_vma_walk hmm_vma_walk;
957 struct hmm *hmm = range->hmm;
958 struct vm_area_struct *vma;
959 struct mm_walk mm_walk;
960
Jason Gunthorpe47f24592019-05-23 11:08:28 -0300961 lockdep_assert_held(&hmm->mm->mmap_sem);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700962 do {
963 /* If range is no longer valid force retry. */
964 if (!range->valid)
Christoph Hellwig2bcbeae2019-07-24 08:52:52 +0200965 return -EBUSY;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700966
967 vma = find_vma(hmm->mm, start);
Jérôme Glisse63d50662019-05-13 17:20:18 -0700968 if (vma == NULL || (vma->vm_flags & device_vma))
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700969 return -EFAULT;
970
Jérôme Glisse63d50662019-05-13 17:20:18 -0700971 if (is_vm_hugetlb_page(vma)) {
Jason Gunthorpe1c2308f02019-05-27 17:02:21 -0300972 if (huge_page_shift(hstate_vma(vma)) !=
973 range->page_shift &&
Jérôme Glisse63d50662019-05-13 17:20:18 -0700974 range->page_shift != PAGE_SHIFT)
975 return -EINVAL;
976 } else {
977 if (range->page_shift != PAGE_SHIFT)
978 return -EINVAL;
979 }
980
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700981 if (!(vma->vm_flags & VM_READ)) {
982 /*
983 * If vma do not allow read access, then assume that it
984 * does not allow write access, either. HMM does not
985 * support architecture that allow write without read.
986 */
987 hmm_pfns_clear(range, range->pfns,
988 range->start, range->end);
989 return -EPERM;
990 }
991
992 range->vma = vma;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700993 hmm_vma_walk.pgmap = NULL;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700994 hmm_vma_walk.last = start;
995 hmm_vma_walk.fault = false;
996 hmm_vma_walk.range = range;
997 mm_walk.private = &hmm_vma_walk;
998 end = min(range->end, vma->vm_end);
999
1000 mm_walk.vma = vma;
1001 mm_walk.mm = vma->vm_mm;
1002 mm_walk.pte_entry = NULL;
1003 mm_walk.test_walk = NULL;
1004 mm_walk.hugetlb_entry = NULL;
Jérôme Glisse992de9a2019-05-13 17:20:21 -07001005 mm_walk.pud_entry = hmm_vma_walk_pud;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001006 mm_walk.pmd_entry = hmm_vma_walk_pmd;
1007 mm_walk.pte_hole = hmm_vma_walk_hole;
Jérôme Glisse63d50662019-05-13 17:20:18 -07001008 mm_walk.hugetlb_entry = hmm_vma_walk_hugetlb_entry;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001009
1010 walk_page_range(start, end, &mm_walk);
1011 start = end;
1012 } while (start < range->end);
1013
Jérôme Glisse25f23a02019-05-13 17:19:55 -07001014 return (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -07001015}
Jérôme Glisse25f23a02019-05-13 17:19:55 -07001016EXPORT_SYMBOL(hmm_range_snapshot);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -07001017
Christoph Hellwig9a4903e2019-07-25 17:56:46 -07001018/**
1019 * hmm_range_fault - try to fault some address in a virtual address range
1020 * @range: range being faulted
1021 * @flags: HMM_FAULT_* flags
Jérôme Glisse73231612019-05-13 17:19:58 -07001022 *
Christoph Hellwig9a4903e2019-07-25 17:56:46 -07001023 * Return: the number of valid pages in range->pfns[] (from range start
1024 * address), which may be zero. On error one of the following status codes
1025 * can be returned:
1026 *
1027 * -EINVAL: Invalid arguments or mm or virtual address is in an invalid vma
1028 * (e.g., device file vma).
1029 * -ENOMEM: Out of memory.
1030 * -EPERM: Invalid permission (e.g., asking for write and range is read
1031 * only).
1032 * -EAGAIN: A page fault needs to be retried and mmap_sem was dropped.
1033 * -EBUSY: The range has been invalidated and the caller needs to wait for
1034 * the invalidation to finish.
1035 * -EFAULT: Invalid (i.e., either no valid vma or it is illegal to access
1036 * that range) number of valid pages in range->pfns[] (from
1037 * range start address).
Jérôme Glisse74eee182017-09-08 16:11:35 -07001038 *
1039 * This is similar to a regular CPU page fault except that it will not trigger
Jérôme Glisse73231612019-05-13 17:19:58 -07001040 * any memory migration if the memory being faulted is not accessible by CPUs
1041 * and caller does not ask for migration.
Jérôme Glisse74eee182017-09-08 16:11:35 -07001042 *
Jérôme Glisseff05c0c2018-04-10 16:28:38 -07001043 * On error, for one virtual address in the range, the function will mark the
1044 * corresponding HMM pfn entry with an error flag.
Jérôme Glisse74eee182017-09-08 16:11:35 -07001045 */
Christoph Hellwig9a4903e2019-07-25 17:56:46 -07001046long hmm_range_fault(struct hmm_range *range, unsigned int flags)
Jérôme Glisse74eee182017-09-08 16:11:35 -07001047{
Jérôme Glisse63d50662019-05-13 17:20:18 -07001048 const unsigned long device_vma = VM_IO | VM_PFNMAP | VM_MIXEDMAP;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001049 unsigned long start = range->start, end;
Jérôme Glisse74eee182017-09-08 16:11:35 -07001050 struct hmm_vma_walk hmm_vma_walk;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001051 struct hmm *hmm = range->hmm;
1052 struct vm_area_struct *vma;
Jérôme Glisse74eee182017-09-08 16:11:35 -07001053 struct mm_walk mm_walk;
Jérôme Glisse74eee182017-09-08 16:11:35 -07001054 int ret;
1055
Jason Gunthorpe47f24592019-05-23 11:08:28 -03001056 lockdep_assert_held(&hmm->mm->mmap_sem);
Jérôme Glisse74eee182017-09-08 16:11:35 -07001057
1058 do {
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001059 /* If range is no longer valid force retry. */
Christoph Hellwig2bcbeae2019-07-24 08:52:52 +02001060 if (!range->valid)
1061 return -EBUSY;
Jérôme Glisse74eee182017-09-08 16:11:35 -07001062
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001063 vma = find_vma(hmm->mm, start);
Jérôme Glisse63d50662019-05-13 17:20:18 -07001064 if (vma == NULL || (vma->vm_flags & device_vma))
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001065 return -EFAULT;
Jérôme Glisse74eee182017-09-08 16:11:35 -07001066
Jérôme Glisse63d50662019-05-13 17:20:18 -07001067 if (is_vm_hugetlb_page(vma)) {
1068 if (huge_page_shift(hstate_vma(vma)) !=
1069 range->page_shift &&
1070 range->page_shift != PAGE_SHIFT)
1071 return -EINVAL;
1072 } else {
1073 if (range->page_shift != PAGE_SHIFT)
1074 return -EINVAL;
1075 }
1076
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001077 if (!(vma->vm_flags & VM_READ)) {
1078 /*
1079 * If vma do not allow read access, then assume that it
1080 * does not allow write access, either. HMM does not
1081 * support architecture that allow write without read.
1082 */
1083 hmm_pfns_clear(range, range->pfns,
1084 range->start, range->end);
1085 return -EPERM;
1086 }
1087
1088 range->vma = vma;
Jérôme Glisse992de9a2019-05-13 17:20:21 -07001089 hmm_vma_walk.pgmap = NULL;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001090 hmm_vma_walk.last = start;
1091 hmm_vma_walk.fault = true;
Christoph Hellwig9a4903e2019-07-25 17:56:46 -07001092 hmm_vma_walk.flags = flags;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001093 hmm_vma_walk.range = range;
1094 mm_walk.private = &hmm_vma_walk;
1095 end = min(range->end, vma->vm_end);
1096
1097 mm_walk.vma = vma;
1098 mm_walk.mm = vma->vm_mm;
1099 mm_walk.pte_entry = NULL;
1100 mm_walk.test_walk = NULL;
1101 mm_walk.hugetlb_entry = NULL;
Jérôme Glisse992de9a2019-05-13 17:20:21 -07001102 mm_walk.pud_entry = hmm_vma_walk_pud;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001103 mm_walk.pmd_entry = hmm_vma_walk_pmd;
1104 mm_walk.pte_hole = hmm_vma_walk_hole;
Jérôme Glisse63d50662019-05-13 17:20:18 -07001105 mm_walk.hugetlb_entry = hmm_vma_walk_hugetlb_entry;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001106
1107 do {
1108 ret = walk_page_range(start, end, &mm_walk);
1109 start = hmm_vma_walk.last;
1110
1111 /* Keep trying while the range is valid. */
1112 } while (ret == -EBUSY && range->valid);
1113
1114 if (ret) {
1115 unsigned long i;
1116
1117 i = (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
1118 hmm_pfns_clear(range, &range->pfns[i],
1119 hmm_vma_walk.last, range->end);
1120 return ret;
1121 }
1122 start = end;
1123
1124 } while (start < range->end);
Jérôme Glisse704f3f22019-05-13 17:19:48 -07001125
Jérôme Glisse73231612019-05-13 17:19:58 -07001126 return (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
Jérôme Glisse74eee182017-09-08 16:11:35 -07001127}
Jérôme Glisse73231612019-05-13 17:19:58 -07001128EXPORT_SYMBOL(hmm_range_fault);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001129
1130/**
Christoph Hellwig9a4903e2019-07-25 17:56:46 -07001131 * hmm_range_dma_map - hmm_range_fault() and dma map page all in one.
1132 * @range: range being faulted
1133 * @device: device to map page to
1134 * @daddrs: array of dma addresses for the mapped pages
1135 * @flags: HMM_FAULT_*
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001136 *
Christoph Hellwig9a4903e2019-07-25 17:56:46 -07001137 * Return: the number of pages mapped on success (including zero), or any
1138 * status return from hmm_range_fault() otherwise.
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001139 */
Christoph Hellwig9a4903e2019-07-25 17:56:46 -07001140long hmm_range_dma_map(struct hmm_range *range, struct device *device,
1141 dma_addr_t *daddrs, unsigned int flags)
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001142{
1143 unsigned long i, npages, mapped;
1144 long ret;
1145
Christoph Hellwig9a4903e2019-07-25 17:56:46 -07001146 ret = hmm_range_fault(range, flags);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001147 if (ret <= 0)
1148 return ret ? ret : -EBUSY;
1149
1150 npages = (range->end - range->start) >> PAGE_SHIFT;
1151 for (i = 0, mapped = 0; i < npages; ++i) {
1152 enum dma_data_direction dir = DMA_TO_DEVICE;
1153 struct page *page;
1154
1155 /*
1156 * FIXME need to update DMA API to provide invalid DMA address
1157 * value instead of a function to test dma address value. This
1158 * would remove lot of dumb code duplicated accross many arch.
1159 *
1160 * For now setting it to 0 here is good enough as the pfns[]
1161 * value is what is use to check what is valid and what isn't.
1162 */
1163 daddrs[i] = 0;
1164
Jérôme Glisse391aab12019-05-13 17:20:31 -07001165 page = hmm_device_entry_to_page(range, range->pfns[i]);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001166 if (page == NULL)
1167 continue;
1168
1169 /* Check if range is being invalidated */
1170 if (!range->valid) {
1171 ret = -EBUSY;
1172 goto unmap;
1173 }
1174
1175 /* If it is read and write than map bi-directional. */
1176 if (range->pfns[i] & range->flags[HMM_PFN_WRITE])
1177 dir = DMA_BIDIRECTIONAL;
1178
1179 daddrs[i] = dma_map_page(device, page, 0, PAGE_SIZE, dir);
1180 if (dma_mapping_error(device, daddrs[i])) {
1181 ret = -EFAULT;
1182 goto unmap;
1183 }
1184
1185 mapped++;
1186 }
1187
1188 return mapped;
1189
1190unmap:
1191 for (npages = i, i = 0; (i < npages) && mapped; ++i) {
1192 enum dma_data_direction dir = DMA_TO_DEVICE;
1193 struct page *page;
1194
Jérôme Glisse391aab12019-05-13 17:20:31 -07001195 page = hmm_device_entry_to_page(range, range->pfns[i]);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001196 if (page == NULL)
1197 continue;
1198
1199 if (dma_mapping_error(device, daddrs[i]))
1200 continue;
1201
1202 /* If it is read and write than map bi-directional. */
1203 if (range->pfns[i] & range->flags[HMM_PFN_WRITE])
1204 dir = DMA_BIDIRECTIONAL;
1205
1206 dma_unmap_page(device, daddrs[i], PAGE_SIZE, dir);
1207 mapped--;
1208 }
1209
1210 return ret;
1211}
1212EXPORT_SYMBOL(hmm_range_dma_map);
1213
1214/**
1215 * hmm_range_dma_unmap() - unmap range of that was map with hmm_range_dma_map()
1216 * @range: range being unmapped
1217 * @vma: the vma against which the range (optional)
1218 * @device: device against which dma map was done
1219 * @daddrs: dma address of mapped pages
1220 * @dirty: dirty page if it had the write flag set
Ralph Campbell085ea252019-05-06 16:29:39 -07001221 * Return: number of page unmapped on success, -EINVAL otherwise
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001222 *
1223 * Note that caller MUST abide by mmu notifier or use HMM mirror and abide
1224 * to the sync_cpu_device_pagetables() callback so that it is safe here to
1225 * call set_page_dirty(). Caller must also take appropriate locks to avoid
1226 * concurrent mmu notifier or sync_cpu_device_pagetables() to make progress.
1227 */
1228long hmm_range_dma_unmap(struct hmm_range *range,
1229 struct vm_area_struct *vma,
1230 struct device *device,
1231 dma_addr_t *daddrs,
1232 bool dirty)
1233{
1234 unsigned long i, npages;
1235 long cpages = 0;
1236
1237 /* Sanity check. */
1238 if (range->end <= range->start)
1239 return -EINVAL;
1240 if (!daddrs)
1241 return -EINVAL;
1242 if (!range->pfns)
1243 return -EINVAL;
1244
1245 npages = (range->end - range->start) >> PAGE_SHIFT;
1246 for (i = 0; i < npages; ++i) {
1247 enum dma_data_direction dir = DMA_TO_DEVICE;
1248 struct page *page;
1249
Jérôme Glisse391aab12019-05-13 17:20:31 -07001250 page = hmm_device_entry_to_page(range, range->pfns[i]);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001251 if (page == NULL)
1252 continue;
1253
1254 /* If it is read and write than map bi-directional. */
1255 if (range->pfns[i] & range->flags[HMM_PFN_WRITE]) {
1256 dir = DMA_BIDIRECTIONAL;
1257
1258 /*
1259 * See comments in function description on why it is
1260 * safe here to call set_page_dirty()
1261 */
1262 if (dirty)
1263 set_page_dirty(page);
1264 }
1265
1266 /* Unmap and clear pfns/dma address */
1267 dma_unmap_page(device, daddrs[i], PAGE_SIZE, dir);
1268 range->pfns[i] = range->values[HMM_PFN_NONE];
1269 /* FIXME see comments in hmm_vma_dma_map() */
1270 daddrs[i] = 0;
1271 cpages++;
1272 }
1273
1274 return cpages;
1275}
1276EXPORT_SYMBOL(hmm_range_dma_unmap);