blob: 54b3a4162ae9494302d30894fa27c56427fa1dfa [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);
Jason Gunthorpee709acc2019-07-24 08:52:58 +0200304 if (ret & VM_FAULT_RETRY) {
305 /* Note, handle_mm_fault did up_read(&mm->mmap_sem)) */
Jérôme Glisse73231612019-05-13 17:19:58 -0700306 return -EAGAIN;
Jason Gunthorpee709acc2019-07-24 08:52:58 +0200307 }
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700308 if (ret & VM_FAULT_ERROR) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700309 *pfn = range->values[HMM_PFN_ERROR];
Jérôme Glisse74eee182017-09-08 16:11:35 -0700310 return -EFAULT;
311 }
312
Jérôme Glisse73231612019-05-13 17:19:58 -0700313 return -EBUSY;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700314}
315
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700316static int hmm_pfns_bad(unsigned long addr,
317 unsigned long end,
318 struct mm_walk *walk)
319{
Jérôme Glissec7195472018-04-10 16:28:27 -0700320 struct hmm_vma_walk *hmm_vma_walk = walk->private;
321 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700322 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700323 unsigned long i;
324
325 i = (addr - range->start) >> PAGE_SHIFT;
326 for (; addr < end; addr += PAGE_SIZE, i++)
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700327 pfns[i] = range->values[HMM_PFN_ERROR];
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700328
329 return 0;
330}
331
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700332/*
333 * hmm_vma_walk_hole() - handle a range lacking valid pmd or pte(s)
334 * @start: range virtual start address (inclusive)
335 * @end: range virtual end address (exclusive)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700336 * @fault: should we fault or not ?
337 * @write_fault: write fault ?
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700338 * @walk: mm_walk structure
Ralph Campbell085ea252019-05-06 16:29:39 -0700339 * Return: 0 on success, -EBUSY after page fault, or page fault error
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700340 *
341 * This function will be called whenever pmd_none() or pte_none() returns true,
342 * or whenever there is no page directory covering the virtual address range.
343 */
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700344static int hmm_vma_walk_hole_(unsigned long addr, unsigned long end,
345 bool fault, bool write_fault,
346 struct mm_walk *walk)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700347{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700348 struct hmm_vma_walk *hmm_vma_walk = walk->private;
349 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700350 uint64_t *pfns = range->pfns;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700351 unsigned long i, page_size;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700352
Jérôme Glisse74eee182017-09-08 16:11:35 -0700353 hmm_vma_walk->last = addr;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700354 page_size = hmm_range_page_size(range);
355 i = (addr - range->start) >> range->page_shift;
356
357 for (; addr < end; addr += page_size, i++) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700358 pfns[i] = range->values[HMM_PFN_NONE];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700359 if (fault || write_fault) {
Jérôme Glisse74eee182017-09-08 16:11:35 -0700360 int ret;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700361
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700362 ret = hmm_vma_do_fault(walk, addr, write_fault,
363 &pfns[i]);
Jérôme Glisse73231612019-05-13 17:19:58 -0700364 if (ret != -EBUSY)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700365 return ret;
366 }
367 }
368
Jérôme Glisse73231612019-05-13 17:19:58 -0700369 return (fault || write_fault) ? -EBUSY : 0;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700370}
371
372static inline void hmm_pte_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
373 uint64_t pfns, uint64_t cpu_flags,
374 bool *fault, bool *write_fault)
375{
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700376 struct hmm_range *range = hmm_vma_walk->range;
377
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700378 if (!hmm_vma_walk->fault)
379 return;
380
Jérôme Glisse023a0192019-05-13 17:20:05 -0700381 /*
382 * So we not only consider the individual per page request we also
383 * consider the default flags requested for the range. The API can
384 * be use in 2 fashions. The first one where the HMM user coalesce
385 * multiple page fault into one request and set flags per pfns for
386 * of those faults. The second one where the HMM user want to pre-
387 * fault a range with specific flags. For the latter one it is a
388 * waste to have the user pre-fill the pfn arrays with a default
389 * flags value.
390 */
391 pfns = (pfns & range->pfn_flags_mask) | range->default_flags;
392
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700393 /* We aren't ask to do anything ... */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700394 if (!(pfns & range->flags[HMM_PFN_VALID]))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700395 return;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700396 /* If this is device memory than only fault if explicitly requested */
397 if ((cpu_flags & range->flags[HMM_PFN_DEVICE_PRIVATE])) {
398 /* Do we fault on device memory ? */
399 if (pfns & range->flags[HMM_PFN_DEVICE_PRIVATE]) {
400 *write_fault = pfns & range->flags[HMM_PFN_WRITE];
401 *fault = true;
402 }
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700403 return;
404 }
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700405
406 /* If CPU page table is not valid then we need to fault */
407 *fault = !(cpu_flags & range->flags[HMM_PFN_VALID]);
408 /* Need to write fault ? */
409 if ((pfns & range->flags[HMM_PFN_WRITE]) &&
410 !(cpu_flags & range->flags[HMM_PFN_WRITE])) {
411 *write_fault = true;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700412 *fault = true;
413 }
414}
415
416static void hmm_range_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
417 const uint64_t *pfns, unsigned long npages,
418 uint64_t cpu_flags, bool *fault,
419 bool *write_fault)
420{
421 unsigned long i;
422
423 if (!hmm_vma_walk->fault) {
424 *fault = *write_fault = false;
425 return;
426 }
427
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700428 *fault = *write_fault = false;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700429 for (i = 0; i < npages; ++i) {
430 hmm_pte_need_fault(hmm_vma_walk, pfns[i], cpu_flags,
431 fault, write_fault);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700432 if ((*write_fault))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700433 return;
434 }
435}
436
437static int hmm_vma_walk_hole(unsigned long addr, unsigned long end,
438 struct mm_walk *walk)
439{
440 struct hmm_vma_walk *hmm_vma_walk = walk->private;
441 struct hmm_range *range = hmm_vma_walk->range;
442 bool fault, write_fault;
443 unsigned long i, npages;
444 uint64_t *pfns;
445
446 i = (addr - range->start) >> PAGE_SHIFT;
447 npages = (end - addr) >> PAGE_SHIFT;
448 pfns = &range->pfns[i];
449 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
450 0, &fault, &write_fault);
451 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
452}
453
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700454static inline uint64_t pmd_to_hmm_pfn_flags(struct hmm_range *range, pmd_t pmd)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700455{
456 if (pmd_protnone(pmd))
457 return 0;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700458 return pmd_write(pmd) ? range->flags[HMM_PFN_VALID] |
459 range->flags[HMM_PFN_WRITE] :
460 range->flags[HMM_PFN_VALID];
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700461}
462
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700463static inline uint64_t pud_to_hmm_pfn_flags(struct hmm_range *range, pud_t pud)
464{
465 if (!pud_present(pud))
466 return 0;
467 return pud_write(pud) ? range->flags[HMM_PFN_VALID] |
468 range->flags[HMM_PFN_WRITE] :
469 range->flags[HMM_PFN_VALID];
470}
471
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700472static int hmm_vma_handle_pmd(struct mm_walk *walk,
473 unsigned long addr,
474 unsigned long end,
475 uint64_t *pfns,
476 pmd_t pmd)
477{
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700478#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700479 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700480 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700481 unsigned long pfn, npages, i;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700482 bool fault, write_fault;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700483 uint64_t cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700484
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700485 npages = (end - addr) >> PAGE_SHIFT;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700486 cpu_flags = pmd_to_hmm_pfn_flags(range, pmd);
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700487 hmm_range_need_fault(hmm_vma_walk, pfns, npages, cpu_flags,
488 &fault, &write_fault);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700489
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700490 if (pmd_protnone(pmd) || fault || write_fault)
491 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700492
493 pfn = pmd_pfn(pmd) + pte_index(addr);
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700494 for (i = 0; addr < end; addr += PAGE_SIZE, i++, pfn++) {
495 if (pmd_devmap(pmd)) {
496 hmm_vma_walk->pgmap = get_dev_pagemap(pfn,
497 hmm_vma_walk->pgmap);
498 if (unlikely(!hmm_vma_walk->pgmap))
499 return -EBUSY;
500 }
Jérôme Glisse391aab12019-05-13 17:20:31 -0700501 pfns[i] = hmm_device_entry_from_pfn(range, pfn) | cpu_flags;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700502 }
503 if (hmm_vma_walk->pgmap) {
504 put_dev_pagemap(hmm_vma_walk->pgmap);
505 hmm_vma_walk->pgmap = NULL;
506 }
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700507 hmm_vma_walk->last = end;
508 return 0;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700509#else
510 /* If THP is not enabled then we should never reach that code ! */
511 return -EINVAL;
512#endif
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700513}
514
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700515static inline uint64_t pte_to_hmm_pfn_flags(struct hmm_range *range, pte_t pte)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700516{
Philip Yang789c2af2019-05-23 16:32:31 -0400517 if (pte_none(pte) || !pte_present(pte) || pte_protnone(pte))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700518 return 0;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700519 return pte_write(pte) ? range->flags[HMM_PFN_VALID] |
520 range->flags[HMM_PFN_WRITE] :
521 range->flags[HMM_PFN_VALID];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700522}
523
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700524static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,
525 unsigned long end, pmd_t *pmdp, pte_t *ptep,
526 uint64_t *pfn)
527{
528 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700529 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700530 struct vm_area_struct *vma = walk->vma;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700531 bool fault, write_fault;
532 uint64_t cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700533 pte_t pte = *ptep;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700534 uint64_t orig_pfn = *pfn;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700535
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700536 *pfn = range->values[HMM_PFN_NONE];
Jérôme Glisse73231612019-05-13 17:19:58 -0700537 fault = write_fault = false;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700538
539 if (pte_none(pte)) {
Jérôme Glisse73231612019-05-13 17:19:58 -0700540 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, 0,
541 &fault, &write_fault);
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700542 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700543 goto fault;
544 return 0;
545 }
546
547 if (!pte_present(pte)) {
548 swp_entry_t entry = pte_to_swp_entry(pte);
549
550 if (!non_swap_entry(entry)) {
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700551 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700552 goto fault;
553 return 0;
554 }
555
556 /*
557 * This is a special swap entry, ignore migration, use
558 * device and report anything else as error.
559 */
560 if (is_device_private_entry(entry)) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700561 cpu_flags = range->flags[HMM_PFN_VALID] |
562 range->flags[HMM_PFN_DEVICE_PRIVATE];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700563 cpu_flags |= is_write_device_private_entry(entry) ?
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700564 range->flags[HMM_PFN_WRITE] : 0;
565 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
566 &fault, &write_fault);
567 if (fault || write_fault)
568 goto fault;
Jérôme Glisse391aab12019-05-13 17:20:31 -0700569 *pfn = hmm_device_entry_from_pfn(range,
570 swp_offset(entry));
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700571 *pfn |= cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700572 return 0;
573 }
574
575 if (is_migration_entry(entry)) {
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700576 if (fault || write_fault) {
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700577 pte_unmap(ptep);
578 hmm_vma_walk->last = addr;
579 migration_entry_wait(vma->vm_mm,
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700580 pmdp, addr);
Jérôme Glisse73231612019-05-13 17:19:58 -0700581 return -EBUSY;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700582 }
583 return 0;
584 }
585
586 /* Report error for everything else */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700587 *pfn = range->values[HMM_PFN_ERROR];
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700588 return -EFAULT;
Jérôme Glisse73231612019-05-13 17:19:58 -0700589 } else {
590 cpu_flags = pte_to_hmm_pfn_flags(range, pte);
591 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
592 &fault, &write_fault);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700593 }
594
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700595 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700596 goto fault;
597
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700598 if (pte_devmap(pte)) {
599 hmm_vma_walk->pgmap = get_dev_pagemap(pte_pfn(pte),
600 hmm_vma_walk->pgmap);
601 if (unlikely(!hmm_vma_walk->pgmap))
602 return -EBUSY;
603 } else if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) && pte_special(pte)) {
604 *pfn = range->values[HMM_PFN_SPECIAL];
605 return -EFAULT;
606 }
607
Jérôme Glisse391aab12019-05-13 17:20:31 -0700608 *pfn = hmm_device_entry_from_pfn(range, pte_pfn(pte)) | cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700609 return 0;
610
611fault:
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700612 if (hmm_vma_walk->pgmap) {
613 put_dev_pagemap(hmm_vma_walk->pgmap);
614 hmm_vma_walk->pgmap = NULL;
615 }
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700616 pte_unmap(ptep);
617 /* Fault any virtual address we were asked to fault */
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700618 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700619}
620
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700621static int hmm_vma_walk_pmd(pmd_t *pmdp,
622 unsigned long start,
623 unsigned long end,
624 struct mm_walk *walk)
625{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700626 struct hmm_vma_walk *hmm_vma_walk = walk->private;
627 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700628 struct vm_area_struct *vma = walk->vma;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700629 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700630 unsigned long addr = start, i;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700631 pte_t *ptep;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700632 pmd_t pmd;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700633
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700634
635again:
Jérôme Glissed08faca2018-10-30 15:04:20 -0700636 pmd = READ_ONCE(*pmdp);
637 if (pmd_none(pmd))
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700638 return hmm_vma_walk_hole(start, end, walk);
639
Jérôme Glissed08faca2018-10-30 15:04:20 -0700640 if (pmd_huge(pmd) && (range->vma->vm_flags & VM_HUGETLB))
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700641 return hmm_pfns_bad(start, end, walk);
642
Jérôme Glissed08faca2018-10-30 15:04:20 -0700643 if (thp_migration_supported() && is_pmd_migration_entry(pmd)) {
644 bool fault, write_fault;
645 unsigned long npages;
646 uint64_t *pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700647
Jérôme Glissed08faca2018-10-30 15:04:20 -0700648 i = (addr - range->start) >> PAGE_SHIFT;
649 npages = (end - addr) >> PAGE_SHIFT;
650 pfns = &range->pfns[i];
651
652 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
653 0, &fault, &write_fault);
654 if (fault || write_fault) {
655 hmm_vma_walk->last = addr;
656 pmd_migration_entry_wait(vma->vm_mm, pmdp);
Jérôme Glisse73231612019-05-13 17:19:58 -0700657 return -EBUSY;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700658 }
659 return 0;
660 } else if (!pmd_present(pmd))
661 return hmm_pfns_bad(start, end, walk);
662
663 if (pmd_devmap(pmd) || pmd_trans_huge(pmd)) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700664 /*
665 * No need to take pmd_lock here, even if some other threads
666 * is splitting the huge pmd we will get that event through
667 * mmu_notifier callback.
668 *
669 * So just read pmd value and check again its a transparent
670 * huge or device mapping one and compute corresponding pfn
671 * values.
672 */
673 pmd = pmd_read_atomic(pmdp);
674 barrier();
675 if (!pmd_devmap(pmd) && !pmd_trans_huge(pmd))
676 goto again;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700677
Jérôme Glissed08faca2018-10-30 15:04:20 -0700678 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700679 return hmm_vma_handle_pmd(walk, addr, end, &pfns[i], pmd);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700680 }
681
Jérôme Glissed08faca2018-10-30 15:04:20 -0700682 /*
683 * We have handled all the valid case above ie either none, migration,
684 * huge or transparent huge. At this point either it is a valid pmd
685 * entry pointing to pte directory or it is a bad pmd that will not
686 * recover.
687 */
688 if (pmd_bad(pmd))
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700689 return hmm_pfns_bad(start, end, walk);
690
691 ptep = pte_offset_map(pmdp, addr);
Jérôme Glissed08faca2018-10-30 15:04:20 -0700692 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700693 for (; addr < end; addr += PAGE_SIZE, ptep++, i++) {
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700694 int r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700695
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700696 r = hmm_vma_handle_pte(walk, addr, end, pmdp, ptep, &pfns[i]);
697 if (r) {
698 /* hmm_vma_handle_pte() did unmap pte directory */
699 hmm_vma_walk->last = addr;
700 return r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700701 }
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700702 }
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700703 if (hmm_vma_walk->pgmap) {
704 /*
705 * We do put_dev_pagemap() here and not in hmm_vma_handle_pte()
706 * so that we can leverage get_dev_pagemap() optimization which
707 * will not re-take a reference on a pgmap if we already have
708 * one.
709 */
710 put_dev_pagemap(hmm_vma_walk->pgmap);
711 hmm_vma_walk->pgmap = NULL;
712 }
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700713 pte_unmap(ptep - 1);
714
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700715 hmm_vma_walk->last = addr;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700716 return 0;
717}
718
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700719static int hmm_vma_walk_pud(pud_t *pudp,
720 unsigned long start,
721 unsigned long end,
722 struct mm_walk *walk)
723{
724 struct hmm_vma_walk *hmm_vma_walk = walk->private;
725 struct hmm_range *range = hmm_vma_walk->range;
726 unsigned long addr = start, next;
727 pmd_t *pmdp;
728 pud_t pud;
729 int ret;
730
731again:
732 pud = READ_ONCE(*pudp);
733 if (pud_none(pud))
734 return hmm_vma_walk_hole(start, end, walk);
735
736 if (pud_huge(pud) && pud_devmap(pud)) {
737 unsigned long i, npages, pfn;
738 uint64_t *pfns, cpu_flags;
739 bool fault, write_fault;
740
741 if (!pud_present(pud))
742 return hmm_vma_walk_hole(start, end, walk);
743
744 i = (addr - range->start) >> PAGE_SHIFT;
745 npages = (end - addr) >> PAGE_SHIFT;
746 pfns = &range->pfns[i];
747
748 cpu_flags = pud_to_hmm_pfn_flags(range, pud);
749 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
750 cpu_flags, &fault, &write_fault);
751 if (fault || write_fault)
752 return hmm_vma_walk_hole_(addr, end, fault,
753 write_fault, walk);
754
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700755 pfn = pud_pfn(pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
756 for (i = 0; i < npages; ++i, ++pfn) {
757 hmm_vma_walk->pgmap = get_dev_pagemap(pfn,
758 hmm_vma_walk->pgmap);
759 if (unlikely(!hmm_vma_walk->pgmap))
760 return -EBUSY;
Jérôme Glisse391aab12019-05-13 17:20:31 -0700761 pfns[i] = hmm_device_entry_from_pfn(range, pfn) |
762 cpu_flags;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700763 }
764 if (hmm_vma_walk->pgmap) {
765 put_dev_pagemap(hmm_vma_walk->pgmap);
766 hmm_vma_walk->pgmap = NULL;
767 }
768 hmm_vma_walk->last = end;
769 return 0;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700770 }
771
772 split_huge_pud(walk->vma, pudp, addr);
773 if (pud_none(*pudp))
774 goto again;
775
776 pmdp = pmd_offset(pudp, addr);
777 do {
778 next = pmd_addr_end(addr, end);
779 ret = hmm_vma_walk_pmd(pmdp, addr, next, walk);
780 if (ret)
781 return ret;
782 } while (pmdp++, addr = next, addr != end);
783
784 return 0;
785}
786
Jérôme Glisse63d50662019-05-13 17:20:18 -0700787static int hmm_vma_walk_hugetlb_entry(pte_t *pte, unsigned long hmask,
788 unsigned long start, unsigned long end,
789 struct mm_walk *walk)
790{
791#ifdef CONFIG_HUGETLB_PAGE
792 unsigned long addr = start, i, pfn, mask, size, pfn_inc;
793 struct hmm_vma_walk *hmm_vma_walk = walk->private;
794 struct hmm_range *range = hmm_vma_walk->range;
795 struct vm_area_struct *vma = walk->vma;
796 struct hstate *h = hstate_vma(vma);
797 uint64_t orig_pfn, cpu_flags;
798 bool fault, write_fault;
799 spinlock_t *ptl;
800 pte_t entry;
801 int ret = 0;
802
803 size = 1UL << huge_page_shift(h);
804 mask = size - 1;
805 if (range->page_shift != PAGE_SHIFT) {
806 /* Make sure we are looking at full page. */
807 if (start & mask)
808 return -EINVAL;
809 if (end < (start + size))
810 return -EINVAL;
811 pfn_inc = size >> PAGE_SHIFT;
812 } else {
813 pfn_inc = 1;
814 size = PAGE_SIZE;
815 }
816
817
818 ptl = huge_pte_lock(hstate_vma(walk->vma), walk->mm, pte);
819 entry = huge_ptep_get(pte);
820
821 i = (start - range->start) >> range->page_shift;
822 orig_pfn = range->pfns[i];
823 range->pfns[i] = range->values[HMM_PFN_NONE];
824 cpu_flags = pte_to_hmm_pfn_flags(range, entry);
825 fault = write_fault = false;
826 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
827 &fault, &write_fault);
828 if (fault || write_fault) {
829 ret = -ENOENT;
830 goto unlock;
831 }
832
833 pfn = pte_pfn(entry) + ((start & mask) >> range->page_shift);
834 for (; addr < end; addr += size, i++, pfn += pfn_inc)
Jérôme Glisse391aab12019-05-13 17:20:31 -0700835 range->pfns[i] = hmm_device_entry_from_pfn(range, pfn) |
836 cpu_flags;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700837 hmm_vma_walk->last = end;
838
839unlock:
840 spin_unlock(ptl);
841
842 if (ret == -ENOENT)
843 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
844
845 return ret;
846#else /* CONFIG_HUGETLB_PAGE */
847 return -EINVAL;
848#endif
849}
850
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700851static void hmm_pfns_clear(struct hmm_range *range,
852 uint64_t *pfns,
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700853 unsigned long addr,
854 unsigned long end)
855{
856 for (; addr < end; addr += PAGE_SIZE, pfns++)
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700857 *pfns = range->values[HMM_PFN_NONE];
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700858}
859
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700860/*
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700861 * hmm_range_register() - start tracking change to CPU page table over a range
Jérôme Glisse25f23a02019-05-13 17:19:55 -0700862 * @range: range
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700863 * @mm: the mm struct for the range of virtual address
864 * @start: start virtual address (inclusive)
865 * @end: end virtual address (exclusive)
Jérôme Glisse63d50662019-05-13 17:20:18 -0700866 * @page_shift: expect page shift for the range
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700867 * Returns 0 on success, -EFAULT if the address space is no longer valid
Jérôme Glisse25f23a02019-05-13 17:19:55 -0700868 *
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700869 * Track updates to the CPU page table see include/linux/hmm.h
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700870 */
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700871int hmm_range_register(struct hmm_range *range,
Jason Gunthorpee36acfe2019-05-23 09:41:19 -0300872 struct hmm_mirror *mirror,
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700873 unsigned long start,
Jérôme Glisse63d50662019-05-13 17:20:18 -0700874 unsigned long end,
875 unsigned page_shift)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700876{
Jérôme Glisse63d50662019-05-13 17:20:18 -0700877 unsigned long mask = ((1UL << page_shift) - 1UL);
Jason Gunthorpee36acfe2019-05-23 09:41:19 -0300878 struct hmm *hmm = mirror->hmm;
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300879 unsigned long flags;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700880
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700881 range->valid = false;
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700882 range->hmm = NULL;
883
Jérôme Glisse63d50662019-05-13 17:20:18 -0700884 if ((start & mask) || (end & mask))
885 return -EINVAL;
886 if (start >= end)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700887 return -EINVAL;
888
Jérôme Glisse63d50662019-05-13 17:20:18 -0700889 range->page_shift = page_shift;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700890 range->start = start;
891 range->end = end;
892
Jason Gunthorpe47f24592019-05-23 11:08:28 -0300893 /* Prevent hmm_release() from running while the range is valid */
894 if (!mmget_not_zero(hmm->mm))
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700895 return -EFAULT;
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700896
Ralph Campbell085ea252019-05-06 16:29:39 -0700897 /* Initialize range to track CPU page table updates. */
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300898 spin_lock_irqsave(&hmm->ranges_lock, flags);
Jérôme Glisse86586a42018-04-10 16:28:34 -0700899
Ralph Campbell085ea252019-05-06 16:29:39 -0700900 range->hmm = hmm;
Jason Gunthorpee36acfe2019-05-23 09:41:19 -0300901 kref_get(&hmm->kref);
Jason Gunthorpe157816f2019-05-23 11:43:43 -0300902 list_add(&range->list, &hmm->ranges);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700903
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700904 /*
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700905 * If there are any concurrent notifiers we have to wait for them for
906 * the range to be valid (see hmm_range_wait_until_valid()).
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700907 */
Ralph Campbell085ea252019-05-06 16:29:39 -0700908 if (!hmm->notifiers)
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700909 range->valid = true;
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300910 spin_unlock_irqrestore(&hmm->ranges_lock, flags);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700911
912 return 0;
913}
914EXPORT_SYMBOL(hmm_range_register);
915
916/*
917 * hmm_range_unregister() - stop tracking change to CPU page table over a range
918 * @range: range
919 *
920 * Range struct is used to track updates to the CPU page table after a call to
921 * hmm_range_register(). See include/linux/hmm.h for how to use it.
922 */
923void hmm_range_unregister(struct hmm_range *range)
924{
Ralph Campbell085ea252019-05-06 16:29:39 -0700925 struct hmm *hmm = range->hmm;
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300926 unsigned long flags;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700927
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300928 spin_lock_irqsave(&hmm->ranges_lock, flags);
Jason Gunthorpe47f24592019-05-23 11:08:28 -0300929 list_del_init(&range->list);
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300930 spin_unlock_irqrestore(&hmm->ranges_lock, flags);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700931
932 /* Drop reference taken by hmm_range_register() */
Jason Gunthorpe47f24592019-05-23 11:08:28 -0300933 mmput(hmm->mm);
Ralph Campbell085ea252019-05-06 16:29:39 -0700934 hmm_put(hmm);
Jason Gunthorpe2dcc3eb2019-05-23 11:40:24 -0300935
936 /*
937 * The range is now invalid and the ref on the hmm is dropped, so
938 * poison the pointer. Leave other fields in place, for the caller's
939 * use.
940 */
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700941 range->valid = false;
Jason Gunthorpe2dcc3eb2019-05-23 11:40:24 -0300942 memset(&range->hmm, POISON_INUSE, sizeof(range->hmm));
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700943}
944EXPORT_SYMBOL(hmm_range_unregister);
945
946/*
947 * hmm_range_snapshot() - snapshot CPU page table for a range
948 * @range: range
Ralph Campbell085ea252019-05-06 16:29:39 -0700949 * Return: -EINVAL if invalid argument, -ENOMEM out of memory, -EPERM invalid
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700950 * permission (for instance asking for write and range is read only),
Christoph Hellwig2bcbeae2019-07-24 08:52:52 +0200951 * -EBUSY if you need to retry, -EFAULT invalid (ie either no valid
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700952 * vma or it is illegal to access that range), number of valid pages
953 * in range->pfns[] (from range start address).
954 *
955 * This snapshots the CPU page table for a range of virtual addresses. Snapshot
956 * validity is tracked by range struct. See in include/linux/hmm.h for example
957 * on how to use.
958 */
959long hmm_range_snapshot(struct hmm_range *range)
960{
Jérôme Glisse63d50662019-05-13 17:20:18 -0700961 const unsigned long device_vma = VM_IO | VM_PFNMAP | VM_MIXEDMAP;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700962 unsigned long start = range->start, end;
963 struct hmm_vma_walk hmm_vma_walk;
964 struct hmm *hmm = range->hmm;
965 struct vm_area_struct *vma;
966 struct mm_walk mm_walk;
967
Jason Gunthorpe47f24592019-05-23 11:08:28 -0300968 lockdep_assert_held(&hmm->mm->mmap_sem);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700969 do {
970 /* If range is no longer valid force retry. */
971 if (!range->valid)
Christoph Hellwig2bcbeae2019-07-24 08:52:52 +0200972 return -EBUSY;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700973
974 vma = find_vma(hmm->mm, start);
Jérôme Glisse63d50662019-05-13 17:20:18 -0700975 if (vma == NULL || (vma->vm_flags & device_vma))
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700976 return -EFAULT;
977
Jérôme Glisse63d50662019-05-13 17:20:18 -0700978 if (is_vm_hugetlb_page(vma)) {
Jason Gunthorpe1c2308f02019-05-27 17:02:21 -0300979 if (huge_page_shift(hstate_vma(vma)) !=
980 range->page_shift &&
Jérôme Glisse63d50662019-05-13 17:20:18 -0700981 range->page_shift != PAGE_SHIFT)
982 return -EINVAL;
983 } else {
984 if (range->page_shift != PAGE_SHIFT)
985 return -EINVAL;
986 }
987
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700988 if (!(vma->vm_flags & VM_READ)) {
989 /*
990 * If vma do not allow read access, then assume that it
991 * does not allow write access, either. HMM does not
992 * support architecture that allow write without read.
993 */
994 hmm_pfns_clear(range, range->pfns,
995 range->start, range->end);
996 return -EPERM;
997 }
998
999 range->vma = vma;
Jérôme Glisse992de9a2019-05-13 17:20:21 -07001000 hmm_vma_walk.pgmap = NULL;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001001 hmm_vma_walk.last = start;
1002 hmm_vma_walk.fault = false;
1003 hmm_vma_walk.range = range;
1004 mm_walk.private = &hmm_vma_walk;
1005 end = min(range->end, vma->vm_end);
1006
1007 mm_walk.vma = vma;
1008 mm_walk.mm = vma->vm_mm;
1009 mm_walk.pte_entry = NULL;
1010 mm_walk.test_walk = NULL;
1011 mm_walk.hugetlb_entry = NULL;
Jérôme Glisse992de9a2019-05-13 17:20:21 -07001012 mm_walk.pud_entry = hmm_vma_walk_pud;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001013 mm_walk.pmd_entry = hmm_vma_walk_pmd;
1014 mm_walk.pte_hole = hmm_vma_walk_hole;
Jérôme Glisse63d50662019-05-13 17:20:18 -07001015 mm_walk.hugetlb_entry = hmm_vma_walk_hugetlb_entry;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001016
1017 walk_page_range(start, end, &mm_walk);
1018 start = end;
1019 } while (start < range->end);
1020
Jérôme Glisse25f23a02019-05-13 17:19:55 -07001021 return (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -07001022}
Jérôme Glisse25f23a02019-05-13 17:19:55 -07001023EXPORT_SYMBOL(hmm_range_snapshot);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -07001024
1025/*
Jérôme Glisse73231612019-05-13 17:19:58 -07001026 * hmm_range_fault() - try to fault some address in a virtual address range
Jérôme Glisse08232a42018-04-10 16:28:30 -07001027 * @range: range being faulted
Jérôme Glisse74eee182017-09-08 16:11:35 -07001028 * @block: allow blocking on fault (if true it sleeps and do not drop mmap_sem)
Ralph Campbell085ea252019-05-06 16:29:39 -07001029 * Return: number of valid pages in range->pfns[] (from range start
Jérôme Glisse73231612019-05-13 17:19:58 -07001030 * address). This may be zero. If the return value is negative,
1031 * then one of the following values may be returned:
1032 *
1033 * -EINVAL invalid arguments or mm or virtual address are in an
Jérôme Glisse63d50662019-05-13 17:20:18 -07001034 * invalid vma (for instance device file vma).
Jérôme Glisse73231612019-05-13 17:19:58 -07001035 * -ENOMEM: Out of memory.
1036 * -EPERM: Invalid permission (for instance asking for write and
1037 * range is read only).
1038 * -EAGAIN: If you need to retry and mmap_sem was drop. This can only
1039 * happens if block argument is false.
1040 * -EBUSY: If the the range is being invalidated and you should wait
1041 * for invalidation to finish.
1042 * -EFAULT: Invalid (ie either no valid vma or it is illegal to access
1043 * that range), number of valid pages in range->pfns[] (from
1044 * range start address).
Jérôme Glisse74eee182017-09-08 16:11:35 -07001045 *
1046 * This is similar to a regular CPU page fault except that it will not trigger
Jérôme Glisse73231612019-05-13 17:19:58 -07001047 * any memory migration if the memory being faulted is not accessible by CPUs
1048 * and caller does not ask for migration.
Jérôme Glisse74eee182017-09-08 16:11:35 -07001049 *
Jérôme Glisseff05c0c2018-04-10 16:28:38 -07001050 * On error, for one virtual address in the range, the function will mark the
1051 * corresponding HMM pfn entry with an error flag.
Jérôme Glisse74eee182017-09-08 16:11:35 -07001052 */
Jérôme Glisse73231612019-05-13 17:19:58 -07001053long hmm_range_fault(struct hmm_range *range, bool block)
Jérôme Glisse74eee182017-09-08 16:11:35 -07001054{
Jérôme Glisse63d50662019-05-13 17:20:18 -07001055 const unsigned long device_vma = VM_IO | VM_PFNMAP | VM_MIXEDMAP;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001056 unsigned long start = range->start, end;
Jérôme Glisse74eee182017-09-08 16:11:35 -07001057 struct hmm_vma_walk hmm_vma_walk;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001058 struct hmm *hmm = range->hmm;
1059 struct vm_area_struct *vma;
Jérôme Glisse74eee182017-09-08 16:11:35 -07001060 struct mm_walk mm_walk;
Jérôme Glisse74eee182017-09-08 16:11:35 -07001061 int ret;
1062
Jason Gunthorpe47f24592019-05-23 11:08:28 -03001063 lockdep_assert_held(&hmm->mm->mmap_sem);
Jérôme Glisse74eee182017-09-08 16:11:35 -07001064
1065 do {
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001066 /* If range is no longer valid force retry. */
Christoph Hellwig2bcbeae2019-07-24 08:52:52 +02001067 if (!range->valid)
1068 return -EBUSY;
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);