blob: e1eedef129cf5c3425cb5d709b42910cb5409e43 [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
35 * Returns: returns an HMM object, either by referencing the existing
36 * (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;
168 struct hmm_update update;
169 struct hmm_range *range;
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300170 unsigned long flags;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700171 int ret = 0;
172
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300173 if (!kref_get_unless_zero(&hmm->kref))
174 return 0;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700175
176 update.start = nrange->start;
177 update.end = nrange->end;
178 update.event = HMM_UPDATE_INVALIDATE;
179 update.blockable = mmu_notifier_range_blockable(nrange);
180
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300181 spin_lock_irqsave(&hmm->ranges_lock, flags);
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700182 hmm->notifiers++;
183 list_for_each_entry(range, &hmm->ranges, list) {
184 if (update.end < range->start || update.start >= range->end)
185 continue;
186
187 range->valid = false;
188 }
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300189 spin_unlock_irqrestore(&hmm->ranges_lock, flags);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700190
191 if (mmu_notifier_range_blockable(nrange))
192 down_read(&hmm->mirrors_sem);
193 else if (!down_read_trylock(&hmm->mirrors_sem)) {
194 ret = -EAGAIN;
195 goto out;
196 }
Jérôme Glissec0b12402017-09-08 16:11:27 -0700197
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300198 list_for_each_entry(mirror, &hmm->mirrors, list) {
199 int rc;
200
201 rc = mirror->ops->sync_cpu_device_pagetables(mirror, &update);
202 if (rc) {
203 if (WARN_ON(update.blockable || rc != -EAGAIN))
204 continue;
205 ret = -EAGAIN;
Ralph Campbell085ea252019-05-06 16:29:39 -0700206 break;
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300207 }
Jérôme Glissec0b12402017-09-08 16:11:27 -0700208 }
209 up_read(&hmm->mirrors_sem);
210
211out:
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300212 if (ret)
213 notifiers_decrement(hmm);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700214 hmm_put(hmm);
215 return ret;
216}
217
218static void hmm_invalidate_range_end(struct mmu_notifier *mn,
219 const struct mmu_notifier_range *nrange)
220{
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300221 struct hmm *hmm = container_of(mn, struct hmm, mmu_notifier);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700222
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300223 if (!kref_get_unless_zero(&hmm->kref))
224 return;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700225
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300226 notifiers_decrement(hmm);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700227 hmm_put(hmm);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700228}
229
230static const struct mmu_notifier_ops hmm_mmu_notifier_ops = {
Ralph Campbelle1401512018-04-10 16:28:19 -0700231 .release = hmm_release,
Jérôme Glissec0b12402017-09-08 16:11:27 -0700232 .invalidate_range_start = hmm_invalidate_range_start,
233 .invalidate_range_end = hmm_invalidate_range_end,
234};
235
236/*
237 * hmm_mirror_register() - register a mirror against an mm
238 *
239 * @mirror: new mirror struct to register
240 * @mm: mm to register against
Ralph Campbell085ea252019-05-06 16:29:39 -0700241 * Return: 0 on success, -ENOMEM if no memory, -EINVAL if invalid arguments
Jérôme Glissec0b12402017-09-08 16:11:27 -0700242 *
243 * To start mirroring a process address space, the device driver must register
244 * an HMM mirror struct.
Jérôme Glissec0b12402017-09-08 16:11:27 -0700245 */
246int hmm_mirror_register(struct hmm_mirror *mirror, struct mm_struct *mm)
247{
Linus Torvaldsfec88ab2019-07-14 19:42:11 -0700248 lockdep_assert_held_write(&mm->mmap_sem);
Jason Gunthorpe8a1a0cd2019-05-23 11:23:30 -0300249
Jérôme Glissec0b12402017-09-08 16:11:27 -0700250 /* Sanity check */
251 if (!mm || !mirror || !mirror->ops)
252 return -EINVAL;
253
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700254 mirror->hmm = hmm_get_or_create(mm);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700255 if (!mirror->hmm)
256 return -ENOMEM;
257
258 down_write(&mirror->hmm->mirrors_sem);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700259 list_add(&mirror->list, &mirror->hmm->mirrors);
260 up_write(&mirror->hmm->mirrors_sem);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700261
262 return 0;
263}
264EXPORT_SYMBOL(hmm_mirror_register);
265
266/*
267 * hmm_mirror_unregister() - unregister a mirror
268 *
Ralph Campbell085ea252019-05-06 16:29:39 -0700269 * @mirror: mirror struct to unregister
Jérôme Glissec0b12402017-09-08 16:11:27 -0700270 *
271 * Stop mirroring a process address space, and cleanup.
272 */
273void hmm_mirror_unregister(struct hmm_mirror *mirror)
274{
Jason Gunthorpe187229c2019-05-23 11:31:45 -0300275 struct hmm *hmm = mirror->hmm;
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700276
Jérôme Glissec0b12402017-09-08 16:11:27 -0700277 down_write(&hmm->mirrors_sem);
Jason Gunthorpe14331722019-05-24 12:14:08 -0300278 list_del(&mirror->list);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700279 up_write(&hmm->mirrors_sem);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700280 hmm_put(hmm);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700281}
282EXPORT_SYMBOL(hmm_mirror_unregister);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700283
Jérôme Glisse74eee182017-09-08 16:11:35 -0700284struct hmm_vma_walk {
285 struct hmm_range *range;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700286 struct dev_pagemap *pgmap;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700287 unsigned long last;
288 bool fault;
289 bool block;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700290};
291
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700292static int hmm_vma_do_fault(struct mm_walk *walk, unsigned long addr,
293 bool write_fault, uint64_t *pfn)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700294{
Kuehling, Felix9b1ae602019-05-10 19:53:24 +0000295 unsigned int flags = FAULT_FLAG_REMOTE;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700296 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700297 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700298 struct vm_area_struct *vma = walk->vma;
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700299 vm_fault_t ret;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700300
301 flags |= hmm_vma_walk->block ? 0 : FAULT_FLAG_ALLOW_RETRY;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700302 flags |= write_fault ? FAULT_FLAG_WRITE : 0;
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700303 ret = handle_mm_fault(vma, addr, flags);
304 if (ret & VM_FAULT_RETRY)
Jérôme Glisse73231612019-05-13 17:19:58 -0700305 return -EAGAIN;
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/*
331 * hmm_vma_walk_hole() - handle a range lacking valid pmd or pte(s)
332 * @start: range virtual start address (inclusive)
333 * @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
382 * be use in 2 fashions. The first one where the HMM user coalesce
383 * multiple page fault into one request and set flags per pfns for
384 * of those faults. The second one where the HMM user want to pre-
385 * 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;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700394 /* If this is device memory than only fault if explicitly requested */
395 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
508 /* If THP is not enabled then we should never reach that code ! */
509 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 Glisse53f5c3f2018-04-10 16:28:59 -0700528 struct vm_area_struct *vma = walk->vma;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700529 bool fault, write_fault;
530 uint64_t cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700531 pte_t pte = *ptep;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700532 uint64_t orig_pfn = *pfn;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700533
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700534 *pfn = range->values[HMM_PFN_NONE];
Jérôme Glisse73231612019-05-13 17:19:58 -0700535 fault = write_fault = false;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700536
537 if (pte_none(pte)) {
Jérôme Glisse73231612019-05-13 17:19:58 -0700538 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, 0,
539 &fault, &write_fault);
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700540 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700541 goto fault;
542 return 0;
543 }
544
545 if (!pte_present(pte)) {
546 swp_entry_t entry = pte_to_swp_entry(pte);
547
548 if (!non_swap_entry(entry)) {
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700549 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700550 goto fault;
551 return 0;
552 }
553
554 /*
555 * This is a special swap entry, ignore migration, use
556 * device and report anything else as error.
557 */
558 if (is_device_private_entry(entry)) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700559 cpu_flags = range->flags[HMM_PFN_VALID] |
560 range->flags[HMM_PFN_DEVICE_PRIVATE];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700561 cpu_flags |= is_write_device_private_entry(entry) ?
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700562 range->flags[HMM_PFN_WRITE] : 0;
563 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
564 &fault, &write_fault);
565 if (fault || write_fault)
566 goto fault;
Jérôme Glisse391aab12019-05-13 17:20:31 -0700567 *pfn = hmm_device_entry_from_pfn(range,
568 swp_offset(entry));
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700569 *pfn |= cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700570 return 0;
571 }
572
573 if (is_migration_entry(entry)) {
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700574 if (fault || write_fault) {
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700575 pte_unmap(ptep);
576 hmm_vma_walk->last = addr;
577 migration_entry_wait(vma->vm_mm,
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700578 pmdp, addr);
Jérôme Glisse73231612019-05-13 17:19:58 -0700579 return -EBUSY;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700580 }
581 return 0;
582 }
583
584 /* Report error for everything else */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700585 *pfn = range->values[HMM_PFN_ERROR];
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700586 return -EFAULT;
Jérôme Glisse73231612019-05-13 17:19:58 -0700587 } else {
588 cpu_flags = pte_to_hmm_pfn_flags(range, pte);
589 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
590 &fault, &write_fault);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700591 }
592
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700593 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700594 goto fault;
595
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700596 if (pte_devmap(pte)) {
597 hmm_vma_walk->pgmap = get_dev_pagemap(pte_pfn(pte),
598 hmm_vma_walk->pgmap);
599 if (unlikely(!hmm_vma_walk->pgmap))
600 return -EBUSY;
601 } else if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) && pte_special(pte)) {
602 *pfn = range->values[HMM_PFN_SPECIAL];
603 return -EFAULT;
604 }
605
Jérôme Glisse391aab12019-05-13 17:20:31 -0700606 *pfn = hmm_device_entry_from_pfn(range, pte_pfn(pte)) | cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700607 return 0;
608
609fault:
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700610 if (hmm_vma_walk->pgmap) {
611 put_dev_pagemap(hmm_vma_walk->pgmap);
612 hmm_vma_walk->pgmap = NULL;
613 }
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700614 pte_unmap(ptep);
615 /* Fault any virtual address we were asked to fault */
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700616 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700617}
618
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700619static int hmm_vma_walk_pmd(pmd_t *pmdp,
620 unsigned long start,
621 unsigned long end,
622 struct mm_walk *walk)
623{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700624 struct hmm_vma_walk *hmm_vma_walk = walk->private;
625 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700626 struct vm_area_struct *vma = walk->vma;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700627 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700628 unsigned long addr = start, i;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700629 pte_t *ptep;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700630 pmd_t pmd;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700631
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700632
633again:
Jérôme Glissed08faca2018-10-30 15:04:20 -0700634 pmd = READ_ONCE(*pmdp);
635 if (pmd_none(pmd))
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700636 return hmm_vma_walk_hole(start, end, walk);
637
Jérôme Glissed08faca2018-10-30 15:04:20 -0700638 if (pmd_huge(pmd) && (range->vma->vm_flags & VM_HUGETLB))
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700639 return hmm_pfns_bad(start, end, walk);
640
Jérôme Glissed08faca2018-10-30 15:04:20 -0700641 if (thp_migration_supported() && is_pmd_migration_entry(pmd)) {
642 bool fault, write_fault;
643 unsigned long npages;
644 uint64_t *pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700645
Jérôme Glissed08faca2018-10-30 15:04:20 -0700646 i = (addr - range->start) >> PAGE_SHIFT;
647 npages = (end - addr) >> PAGE_SHIFT;
648 pfns = &range->pfns[i];
649
650 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
651 0, &fault, &write_fault);
652 if (fault || write_fault) {
653 hmm_vma_walk->last = addr;
654 pmd_migration_entry_wait(vma->vm_mm, pmdp);
Jérôme Glisse73231612019-05-13 17:19:58 -0700655 return -EBUSY;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700656 }
657 return 0;
658 } else if (!pmd_present(pmd))
659 return hmm_pfns_bad(start, end, walk);
660
661 if (pmd_devmap(pmd) || pmd_trans_huge(pmd)) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700662 /*
663 * No need to take pmd_lock here, even if some other threads
664 * is splitting the huge pmd we will get that event through
665 * mmu_notifier callback.
666 *
667 * So just read pmd value and check again its a transparent
668 * huge or device mapping one and compute corresponding pfn
669 * values.
670 */
671 pmd = pmd_read_atomic(pmdp);
672 barrier();
673 if (!pmd_devmap(pmd) && !pmd_trans_huge(pmd))
674 goto again;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700675
Jérôme Glissed08faca2018-10-30 15:04:20 -0700676 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700677 return hmm_vma_handle_pmd(walk, addr, end, &pfns[i], pmd);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700678 }
679
Jérôme Glissed08faca2018-10-30 15:04:20 -0700680 /*
681 * We have handled all the valid case above ie either none, migration,
682 * huge or transparent huge. At this point either it is a valid pmd
683 * entry pointing to pte directory or it is a bad pmd that will not
684 * recover.
685 */
686 if (pmd_bad(pmd))
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700687 return hmm_pfns_bad(start, end, walk);
688
689 ptep = pte_offset_map(pmdp, addr);
Jérôme Glissed08faca2018-10-30 15:04:20 -0700690 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700691 for (; addr < end; addr += PAGE_SIZE, ptep++, i++) {
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700692 int r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700693
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700694 r = hmm_vma_handle_pte(walk, addr, end, pmdp, ptep, &pfns[i]);
695 if (r) {
696 /* hmm_vma_handle_pte() did unmap pte directory */
697 hmm_vma_walk->last = addr;
698 return r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700699 }
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700700 }
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700701 if (hmm_vma_walk->pgmap) {
702 /*
703 * We do put_dev_pagemap() here and not in hmm_vma_handle_pte()
704 * so that we can leverage get_dev_pagemap() optimization which
705 * will not re-take a reference on a pgmap if we already have
706 * one.
707 */
708 put_dev_pagemap(hmm_vma_walk->pgmap);
709 hmm_vma_walk->pgmap = NULL;
710 }
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700711 pte_unmap(ptep - 1);
712
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700713 hmm_vma_walk->last = addr;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700714 return 0;
715}
716
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700717static int hmm_vma_walk_pud(pud_t *pudp,
718 unsigned long start,
719 unsigned long end,
720 struct mm_walk *walk)
721{
722 struct hmm_vma_walk *hmm_vma_walk = walk->private;
723 struct hmm_range *range = hmm_vma_walk->range;
724 unsigned long addr = start, next;
725 pmd_t *pmdp;
726 pud_t pud;
727 int ret;
728
729again:
730 pud = READ_ONCE(*pudp);
731 if (pud_none(pud))
732 return hmm_vma_walk_hole(start, end, walk);
733
734 if (pud_huge(pud) && pud_devmap(pud)) {
735 unsigned long i, npages, pfn;
736 uint64_t *pfns, cpu_flags;
737 bool fault, write_fault;
738
739 if (!pud_present(pud))
740 return hmm_vma_walk_hole(start, end, walk);
741
742 i = (addr - range->start) >> PAGE_SHIFT;
743 npages = (end - addr) >> PAGE_SHIFT;
744 pfns = &range->pfns[i];
745
746 cpu_flags = pud_to_hmm_pfn_flags(range, pud);
747 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
748 cpu_flags, &fault, &write_fault);
749 if (fault || write_fault)
750 return hmm_vma_walk_hole_(addr, end, fault,
751 write_fault, walk);
752
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700753 pfn = pud_pfn(pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
754 for (i = 0; i < npages; ++i, ++pfn) {
755 hmm_vma_walk->pgmap = get_dev_pagemap(pfn,
756 hmm_vma_walk->pgmap);
757 if (unlikely(!hmm_vma_walk->pgmap))
758 return -EBUSY;
Jérôme Glisse391aab12019-05-13 17:20:31 -0700759 pfns[i] = hmm_device_entry_from_pfn(range, pfn) |
760 cpu_flags;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700761 }
762 if (hmm_vma_walk->pgmap) {
763 put_dev_pagemap(hmm_vma_walk->pgmap);
764 hmm_vma_walk->pgmap = NULL;
765 }
766 hmm_vma_walk->last = end;
767 return 0;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700768 }
769
770 split_huge_pud(walk->vma, pudp, addr);
771 if (pud_none(*pudp))
772 goto again;
773
774 pmdp = pmd_offset(pudp, addr);
775 do {
776 next = pmd_addr_end(addr, end);
777 ret = hmm_vma_walk_pmd(pmdp, addr, next, walk);
778 if (ret)
779 return ret;
780 } while (pmdp++, addr = next, addr != end);
781
782 return 0;
783}
784
Jérôme Glisse63d50662019-05-13 17:20:18 -0700785static int hmm_vma_walk_hugetlb_entry(pte_t *pte, unsigned long hmask,
786 unsigned long start, unsigned long end,
787 struct mm_walk *walk)
788{
789#ifdef CONFIG_HUGETLB_PAGE
790 unsigned long addr = start, i, pfn, mask, size, pfn_inc;
791 struct hmm_vma_walk *hmm_vma_walk = walk->private;
792 struct hmm_range *range = hmm_vma_walk->range;
793 struct vm_area_struct *vma = walk->vma;
794 struct hstate *h = hstate_vma(vma);
795 uint64_t orig_pfn, cpu_flags;
796 bool fault, write_fault;
797 spinlock_t *ptl;
798 pte_t entry;
799 int ret = 0;
800
801 size = 1UL << huge_page_shift(h);
802 mask = size - 1;
803 if (range->page_shift != PAGE_SHIFT) {
804 /* Make sure we are looking at full page. */
805 if (start & mask)
806 return -EINVAL;
807 if (end < (start + size))
808 return -EINVAL;
809 pfn_inc = size >> PAGE_SHIFT;
810 } else {
811 pfn_inc = 1;
812 size = PAGE_SIZE;
813 }
814
815
816 ptl = huge_pte_lock(hstate_vma(walk->vma), walk->mm, pte);
817 entry = huge_ptep_get(pte);
818
819 i = (start - range->start) >> range->page_shift;
820 orig_pfn = range->pfns[i];
821 range->pfns[i] = range->values[HMM_PFN_NONE];
822 cpu_flags = pte_to_hmm_pfn_flags(range, entry);
823 fault = write_fault = false;
824 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
825 &fault, &write_fault);
826 if (fault || write_fault) {
827 ret = -ENOENT;
828 goto unlock;
829 }
830
831 pfn = pte_pfn(entry) + ((start & mask) >> range->page_shift);
832 for (; addr < end; addr += size, i++, pfn += pfn_inc)
Jérôme Glisse391aab12019-05-13 17:20:31 -0700833 range->pfns[i] = hmm_device_entry_from_pfn(range, pfn) |
834 cpu_flags;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700835 hmm_vma_walk->last = end;
836
837unlock:
838 spin_unlock(ptl);
839
840 if (ret == -ENOENT)
841 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
842
843 return ret;
844#else /* CONFIG_HUGETLB_PAGE */
845 return -EINVAL;
846#endif
847}
848
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700849static void hmm_pfns_clear(struct hmm_range *range,
850 uint64_t *pfns,
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700851 unsigned long addr,
852 unsigned long end)
853{
854 for (; addr < end; addr += PAGE_SIZE, pfns++)
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700855 *pfns = range->values[HMM_PFN_NONE];
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700856}
857
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700858/*
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700859 * hmm_range_register() - start tracking change to CPU page table over a range
Jérôme Glisse25f23a02019-05-13 17:19:55 -0700860 * @range: range
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700861 * @mm: the mm struct for the range of virtual address
862 * @start: start virtual address (inclusive)
863 * @end: end virtual address (exclusive)
Jérôme Glisse63d50662019-05-13 17:20:18 -0700864 * @page_shift: expect page shift for the range
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700865 * Returns 0 on success, -EFAULT if the address space is no longer valid
Jérôme Glisse25f23a02019-05-13 17:19:55 -0700866 *
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700867 * Track updates to the CPU page table see include/linux/hmm.h
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700868 */
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700869int hmm_range_register(struct hmm_range *range,
Jason Gunthorpee36acfe2019-05-23 09:41:19 -0300870 struct hmm_mirror *mirror,
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700871 unsigned long start,
Jérôme Glisse63d50662019-05-13 17:20:18 -0700872 unsigned long end,
873 unsigned page_shift)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700874{
Jérôme Glisse63d50662019-05-13 17:20:18 -0700875 unsigned long mask = ((1UL << page_shift) - 1UL);
Jason Gunthorpee36acfe2019-05-23 09:41:19 -0300876 struct hmm *hmm = mirror->hmm;
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300877 unsigned long flags;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700878
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700879 range->valid = false;
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700880 range->hmm = NULL;
881
Jérôme Glisse63d50662019-05-13 17:20:18 -0700882 if ((start & mask) || (end & mask))
883 return -EINVAL;
884 if (start >= end)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700885 return -EINVAL;
886
Jérôme Glisse63d50662019-05-13 17:20:18 -0700887 range->page_shift = page_shift;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700888 range->start = start;
889 range->end = end;
890
Jason Gunthorpe47f24592019-05-23 11:08:28 -0300891 /* Prevent hmm_release() from running while the range is valid */
892 if (!mmget_not_zero(hmm->mm))
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700893 return -EFAULT;
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700894
Ralph Campbell085ea252019-05-06 16:29:39 -0700895 /* Initialize range to track CPU page table updates. */
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300896 spin_lock_irqsave(&hmm->ranges_lock, flags);
Jérôme Glisse86586a42018-04-10 16:28:34 -0700897
Ralph Campbell085ea252019-05-06 16:29:39 -0700898 range->hmm = hmm;
Jason Gunthorpee36acfe2019-05-23 09:41:19 -0300899 kref_get(&hmm->kref);
Jason Gunthorpe157816f2019-05-23 11:43:43 -0300900 list_add(&range->list, &hmm->ranges);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700901
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700902 /*
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700903 * If there are any concurrent notifiers we have to wait for them for
904 * the range to be valid (see hmm_range_wait_until_valid()).
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700905 */
Ralph Campbell085ea252019-05-06 16:29:39 -0700906 if (!hmm->notifiers)
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700907 range->valid = true;
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300908 spin_unlock_irqrestore(&hmm->ranges_lock, flags);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700909
910 return 0;
911}
912EXPORT_SYMBOL(hmm_range_register);
913
914/*
915 * hmm_range_unregister() - stop tracking change to CPU page table over a range
916 * @range: range
917 *
918 * Range struct is used to track updates to the CPU page table after a call to
919 * hmm_range_register(). See include/linux/hmm.h for how to use it.
920 */
921void hmm_range_unregister(struct hmm_range *range)
922{
Ralph Campbell085ea252019-05-06 16:29:39 -0700923 struct hmm *hmm = range->hmm;
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300924 unsigned long flags;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700925
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300926 spin_lock_irqsave(&hmm->ranges_lock, flags);
Jason Gunthorpe47f24592019-05-23 11:08:28 -0300927 list_del_init(&range->list);
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300928 spin_unlock_irqrestore(&hmm->ranges_lock, flags);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700929
930 /* Drop reference taken by hmm_range_register() */
Jason Gunthorpe47f24592019-05-23 11:08:28 -0300931 mmput(hmm->mm);
Ralph Campbell085ea252019-05-06 16:29:39 -0700932 hmm_put(hmm);
Jason Gunthorpe2dcc3eb2019-05-23 11:40:24 -0300933
934 /*
935 * The range is now invalid and the ref on the hmm is dropped, so
936 * poison the pointer. Leave other fields in place, for the caller's
937 * use.
938 */
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700939 range->valid = false;
Jason Gunthorpe2dcc3eb2019-05-23 11:40:24 -0300940 memset(&range->hmm, POISON_INUSE, sizeof(range->hmm));
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700941}
942EXPORT_SYMBOL(hmm_range_unregister);
943
944/*
945 * hmm_range_snapshot() - snapshot CPU page table for a range
946 * @range: range
Ralph Campbell085ea252019-05-06 16:29:39 -0700947 * Return: -EINVAL if invalid argument, -ENOMEM out of memory, -EPERM invalid
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700948 * permission (for instance asking for write and range is read only),
949 * -EAGAIN if you need to retry, -EFAULT invalid (ie either no valid
950 * vma or it is illegal to access that range), number of valid pages
951 * in range->pfns[] (from range start address).
952 *
953 * This snapshots the CPU page table for a range of virtual addresses. Snapshot
954 * validity is tracked by range struct. See in include/linux/hmm.h for example
955 * on how to use.
956 */
957long hmm_range_snapshot(struct hmm_range *range)
958{
Jérôme Glisse63d50662019-05-13 17:20:18 -0700959 const unsigned long device_vma = VM_IO | VM_PFNMAP | VM_MIXEDMAP;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700960 unsigned long start = range->start, end;
961 struct hmm_vma_walk hmm_vma_walk;
962 struct hmm *hmm = range->hmm;
963 struct vm_area_struct *vma;
964 struct mm_walk mm_walk;
965
Jason Gunthorpe47f24592019-05-23 11:08:28 -0300966 lockdep_assert_held(&hmm->mm->mmap_sem);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700967 do {
968 /* If range is no longer valid force retry. */
969 if (!range->valid)
970 return -EAGAIN;
971
972 vma = find_vma(hmm->mm, start);
Jérôme Glisse63d50662019-05-13 17:20:18 -0700973 if (vma == NULL || (vma->vm_flags & device_vma))
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700974 return -EFAULT;
975
Jérôme Glisse63d50662019-05-13 17:20:18 -0700976 if (is_vm_hugetlb_page(vma)) {
Jason Gunthorpe1c2308f02019-05-27 17:02:21 -0300977 if (huge_page_shift(hstate_vma(vma)) !=
978 range->page_shift &&
Jérôme Glisse63d50662019-05-13 17:20:18 -0700979 range->page_shift != PAGE_SHIFT)
980 return -EINVAL;
981 } else {
982 if (range->page_shift != PAGE_SHIFT)
983 return -EINVAL;
984 }
985
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700986 if (!(vma->vm_flags & VM_READ)) {
987 /*
988 * If vma do not allow read access, then assume that it
989 * does not allow write access, either. HMM does not
990 * support architecture that allow write without read.
991 */
992 hmm_pfns_clear(range, range->pfns,
993 range->start, range->end);
994 return -EPERM;
995 }
996
997 range->vma = vma;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700998 hmm_vma_walk.pgmap = NULL;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700999 hmm_vma_walk.last = start;
1000 hmm_vma_walk.fault = false;
1001 hmm_vma_walk.range = range;
1002 mm_walk.private = &hmm_vma_walk;
1003 end = min(range->end, vma->vm_end);
1004
1005 mm_walk.vma = vma;
1006 mm_walk.mm = vma->vm_mm;
1007 mm_walk.pte_entry = NULL;
1008 mm_walk.test_walk = NULL;
1009 mm_walk.hugetlb_entry = NULL;
Jérôme Glisse992de9a2019-05-13 17:20:21 -07001010 mm_walk.pud_entry = hmm_vma_walk_pud;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001011 mm_walk.pmd_entry = hmm_vma_walk_pmd;
1012 mm_walk.pte_hole = hmm_vma_walk_hole;
Jérôme Glisse63d50662019-05-13 17:20:18 -07001013 mm_walk.hugetlb_entry = hmm_vma_walk_hugetlb_entry;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001014
1015 walk_page_range(start, end, &mm_walk);
1016 start = end;
1017 } while (start < range->end);
1018
Jérôme Glisse25f23a02019-05-13 17:19:55 -07001019 return (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -07001020}
Jérôme Glisse25f23a02019-05-13 17:19:55 -07001021EXPORT_SYMBOL(hmm_range_snapshot);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -07001022
1023/*
Jérôme Glisse73231612019-05-13 17:19:58 -07001024 * hmm_range_fault() - try to fault some address in a virtual address range
Jérôme Glisse08232a42018-04-10 16:28:30 -07001025 * @range: range being faulted
Jérôme Glisse74eee182017-09-08 16:11:35 -07001026 * @block: allow blocking on fault (if true it sleeps and do not drop mmap_sem)
Ralph Campbell085ea252019-05-06 16:29:39 -07001027 * Return: number of valid pages in range->pfns[] (from range start
Jérôme Glisse73231612019-05-13 17:19:58 -07001028 * address). This may be zero. If the return value is negative,
1029 * then one of the following values may be returned:
1030 *
1031 * -EINVAL invalid arguments or mm or virtual address are in an
Jérôme Glisse63d50662019-05-13 17:20:18 -07001032 * invalid vma (for instance device file vma).
Jérôme Glisse73231612019-05-13 17:19:58 -07001033 * -ENOMEM: Out of memory.
1034 * -EPERM: Invalid permission (for instance asking for write and
1035 * range is read only).
1036 * -EAGAIN: If you need to retry and mmap_sem was drop. This can only
1037 * happens if block argument is false.
1038 * -EBUSY: If the the range is being invalidated and you should wait
1039 * for invalidation to finish.
1040 * -EFAULT: Invalid (ie either no valid vma or it is illegal to access
1041 * that range), number of valid pages in range->pfns[] (from
1042 * range start address).
Jérôme Glisse74eee182017-09-08 16:11:35 -07001043 *
1044 * This is similar to a regular CPU page fault except that it will not trigger
Jérôme Glisse73231612019-05-13 17:19:58 -07001045 * any memory migration if the memory being faulted is not accessible by CPUs
1046 * and caller does not ask for migration.
Jérôme Glisse74eee182017-09-08 16:11:35 -07001047 *
Jérôme Glisseff05c0c2018-04-10 16:28:38 -07001048 * On error, for one virtual address in the range, the function will mark the
1049 * corresponding HMM pfn entry with an error flag.
Jérôme Glisse74eee182017-09-08 16:11:35 -07001050 */
Jérôme Glisse73231612019-05-13 17:19:58 -07001051long hmm_range_fault(struct hmm_range *range, bool block)
Jérôme Glisse74eee182017-09-08 16:11:35 -07001052{
Jérôme Glisse63d50662019-05-13 17:20:18 -07001053 const unsigned long device_vma = VM_IO | VM_PFNMAP | VM_MIXEDMAP;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001054 unsigned long start = range->start, end;
Jérôme Glisse74eee182017-09-08 16:11:35 -07001055 struct hmm_vma_walk hmm_vma_walk;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001056 struct hmm *hmm = range->hmm;
1057 struct vm_area_struct *vma;
Jérôme Glisse74eee182017-09-08 16:11:35 -07001058 struct mm_walk mm_walk;
Jérôme Glisse74eee182017-09-08 16:11:35 -07001059 int ret;
1060
Jason Gunthorpe47f24592019-05-23 11:08:28 -03001061 lockdep_assert_held(&hmm->mm->mmap_sem);
Jérôme Glisse74eee182017-09-08 16:11:35 -07001062
1063 do {
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001064 /* If range is no longer valid force retry. */
1065 if (!range->valid) {
1066 up_read(&hmm->mm->mmap_sem);
1067 return -EAGAIN;
1068 }
Jérôme Glisse74eee182017-09-08 16:11:35 -07001069
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001070 vma = find_vma(hmm->mm, start);
Jérôme Glisse63d50662019-05-13 17:20:18 -07001071 if (vma == NULL || (vma->vm_flags & device_vma))
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001072 return -EFAULT;
Jérôme Glisse74eee182017-09-08 16:11:35 -07001073
Jérôme Glisse63d50662019-05-13 17:20:18 -07001074 if (is_vm_hugetlb_page(vma)) {
1075 if (huge_page_shift(hstate_vma(vma)) !=
1076 range->page_shift &&
1077 range->page_shift != PAGE_SHIFT)
1078 return -EINVAL;
1079 } else {
1080 if (range->page_shift != PAGE_SHIFT)
1081 return -EINVAL;
1082 }
1083
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001084 if (!(vma->vm_flags & VM_READ)) {
1085 /*
1086 * If vma do not allow read access, then assume that it
1087 * does not allow write access, either. HMM does not
1088 * support architecture that allow write without read.
1089 */
1090 hmm_pfns_clear(range, range->pfns,
1091 range->start, range->end);
1092 return -EPERM;
1093 }
1094
1095 range->vma = vma;
Jérôme Glisse992de9a2019-05-13 17:20:21 -07001096 hmm_vma_walk.pgmap = NULL;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001097 hmm_vma_walk.last = start;
1098 hmm_vma_walk.fault = true;
1099 hmm_vma_walk.block = block;
1100 hmm_vma_walk.range = range;
1101 mm_walk.private = &hmm_vma_walk;
1102 end = min(range->end, vma->vm_end);
1103
1104 mm_walk.vma = vma;
1105 mm_walk.mm = vma->vm_mm;
1106 mm_walk.pte_entry = NULL;
1107 mm_walk.test_walk = NULL;
1108 mm_walk.hugetlb_entry = NULL;
Jérôme Glisse992de9a2019-05-13 17:20:21 -07001109 mm_walk.pud_entry = hmm_vma_walk_pud;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001110 mm_walk.pmd_entry = hmm_vma_walk_pmd;
1111 mm_walk.pte_hole = hmm_vma_walk_hole;
Jérôme Glisse63d50662019-05-13 17:20:18 -07001112 mm_walk.hugetlb_entry = hmm_vma_walk_hugetlb_entry;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001113
1114 do {
1115 ret = walk_page_range(start, end, &mm_walk);
1116 start = hmm_vma_walk.last;
1117
1118 /* Keep trying while the range is valid. */
1119 } while (ret == -EBUSY && range->valid);
1120
1121 if (ret) {
1122 unsigned long i;
1123
1124 i = (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
1125 hmm_pfns_clear(range, &range->pfns[i],
1126 hmm_vma_walk.last, range->end);
1127 return ret;
1128 }
1129 start = end;
1130
1131 } while (start < range->end);
Jérôme Glisse704f3f22019-05-13 17:19:48 -07001132
Jérôme Glisse73231612019-05-13 17:19:58 -07001133 return (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
Jérôme Glisse74eee182017-09-08 16:11:35 -07001134}
Jérôme Glisse73231612019-05-13 17:19:58 -07001135EXPORT_SYMBOL(hmm_range_fault);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001136
1137/**
1138 * hmm_range_dma_map() - hmm_range_fault() and dma map page all in one.
1139 * @range: range being faulted
1140 * @device: device against to dma map page to
1141 * @daddrs: dma address of mapped pages
1142 * @block: allow blocking on fault (if true it sleeps and do not drop mmap_sem)
Ralph Campbell085ea252019-05-06 16:29:39 -07001143 * Return: number of pages mapped on success, -EAGAIN if mmap_sem have been
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001144 * drop and you need to try again, some other error value otherwise
1145 *
1146 * Note same usage pattern as hmm_range_fault().
1147 */
1148long hmm_range_dma_map(struct hmm_range *range,
1149 struct device *device,
1150 dma_addr_t *daddrs,
1151 bool block)
1152{
1153 unsigned long i, npages, mapped;
1154 long ret;
1155
1156 ret = hmm_range_fault(range, block);
1157 if (ret <= 0)
1158 return ret ? ret : -EBUSY;
1159
1160 npages = (range->end - range->start) >> PAGE_SHIFT;
1161 for (i = 0, mapped = 0; i < npages; ++i) {
1162 enum dma_data_direction dir = DMA_TO_DEVICE;
1163 struct page *page;
1164
1165 /*
1166 * FIXME need to update DMA API to provide invalid DMA address
1167 * value instead of a function to test dma address value. This
1168 * would remove lot of dumb code duplicated accross many arch.
1169 *
1170 * For now setting it to 0 here is good enough as the pfns[]
1171 * value is what is use to check what is valid and what isn't.
1172 */
1173 daddrs[i] = 0;
1174
Jérôme Glisse391aab12019-05-13 17:20:31 -07001175 page = hmm_device_entry_to_page(range, range->pfns[i]);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001176 if (page == NULL)
1177 continue;
1178
1179 /* Check if range is being invalidated */
1180 if (!range->valid) {
1181 ret = -EBUSY;
1182 goto unmap;
1183 }
1184
1185 /* If it is read and write than map bi-directional. */
1186 if (range->pfns[i] & range->flags[HMM_PFN_WRITE])
1187 dir = DMA_BIDIRECTIONAL;
1188
1189 daddrs[i] = dma_map_page(device, page, 0, PAGE_SIZE, dir);
1190 if (dma_mapping_error(device, daddrs[i])) {
1191 ret = -EFAULT;
1192 goto unmap;
1193 }
1194
1195 mapped++;
1196 }
1197
1198 return mapped;
1199
1200unmap:
1201 for (npages = i, i = 0; (i < npages) && mapped; ++i) {
1202 enum dma_data_direction dir = DMA_TO_DEVICE;
1203 struct page *page;
1204
Jérôme Glisse391aab12019-05-13 17:20:31 -07001205 page = hmm_device_entry_to_page(range, range->pfns[i]);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001206 if (page == NULL)
1207 continue;
1208
1209 if (dma_mapping_error(device, daddrs[i]))
1210 continue;
1211
1212 /* If it is read and write than map bi-directional. */
1213 if (range->pfns[i] & range->flags[HMM_PFN_WRITE])
1214 dir = DMA_BIDIRECTIONAL;
1215
1216 dma_unmap_page(device, daddrs[i], PAGE_SIZE, dir);
1217 mapped--;
1218 }
1219
1220 return ret;
1221}
1222EXPORT_SYMBOL(hmm_range_dma_map);
1223
1224/**
1225 * hmm_range_dma_unmap() - unmap range of that was map with hmm_range_dma_map()
1226 * @range: range being unmapped
1227 * @vma: the vma against which the range (optional)
1228 * @device: device against which dma map was done
1229 * @daddrs: dma address of mapped pages
1230 * @dirty: dirty page if it had the write flag set
Ralph Campbell085ea252019-05-06 16:29:39 -07001231 * Return: number of page unmapped on success, -EINVAL otherwise
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001232 *
1233 * Note that caller MUST abide by mmu notifier or use HMM mirror and abide
1234 * to the sync_cpu_device_pagetables() callback so that it is safe here to
1235 * call set_page_dirty(). Caller must also take appropriate locks to avoid
1236 * concurrent mmu notifier or sync_cpu_device_pagetables() to make progress.
1237 */
1238long hmm_range_dma_unmap(struct hmm_range *range,
1239 struct vm_area_struct *vma,
1240 struct device *device,
1241 dma_addr_t *daddrs,
1242 bool dirty)
1243{
1244 unsigned long i, npages;
1245 long cpages = 0;
1246
1247 /* Sanity check. */
1248 if (range->end <= range->start)
1249 return -EINVAL;
1250 if (!daddrs)
1251 return -EINVAL;
1252 if (!range->pfns)
1253 return -EINVAL;
1254
1255 npages = (range->end - range->start) >> PAGE_SHIFT;
1256 for (i = 0; i < npages; ++i) {
1257 enum dma_data_direction dir = DMA_TO_DEVICE;
1258 struct page *page;
1259
Jérôme Glisse391aab12019-05-13 17:20:31 -07001260 page = hmm_device_entry_to_page(range, range->pfns[i]);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001261 if (page == NULL)
1262 continue;
1263
1264 /* If it is read and write than map bi-directional. */
1265 if (range->pfns[i] & range->flags[HMM_PFN_WRITE]) {
1266 dir = DMA_BIDIRECTIONAL;
1267
1268 /*
1269 * See comments in function description on why it is
1270 * safe here to call set_page_dirty()
1271 */
1272 if (dirty)
1273 set_page_dirty(page);
1274 }
1275
1276 /* Unmap and clear pfns/dma address */
1277 dma_unmap_page(device, daddrs[i], PAGE_SIZE, dir);
1278 range->pfns[i] = range->values[HMM_PFN_NONE];
1279 /* FIXME see comments in hmm_vma_dma_map() */
1280 daddrs[i] = 0;
1281 cpages++;
1282 }
1283
1284 return cpages;
1285}
1286EXPORT_SYMBOL(hmm_range_dma_unmap);