blob: ab22fdb655c8e3bd4fbb7804212c0af13e21321a [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Jérôme Glisse133ff0e2017-09-08 16:11:23 -07002/*
3 * Copyright 2013 Red Hat Inc.
4 *
Jérôme Glissef813f212018-10-30 15:04:06 -07005 * Authors: Jérôme Glisse <jglisse@redhat.com>
Jérôme Glisse133ff0e2017-09-08 16:11:23 -07006 */
7/*
8 * Refer to include/linux/hmm.h for information about heterogeneous memory
9 * management or HMM for short.
10 */
11#include <linux/mm.h>
12#include <linux/hmm.h>
Jérôme Glisse858b54d2017-09-08 16:12:02 -070013#include <linux/init.h>
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070014#include <linux/rmap.h>
15#include <linux/swap.h>
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070016#include <linux/slab.h>
17#include <linux/sched.h>
Jérôme Glisse4ef589d2017-09-08 16:11:58 -070018#include <linux/mmzone.h>
19#include <linux/pagemap.h>
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070020#include <linux/swapops.h>
21#include <linux/hugetlb.h>
Jérôme Glisse4ef589d2017-09-08 16:11:58 -070022#include <linux/memremap.h>
Jason Gunthorpec8a53b22019-05-23 10:36:46 -030023#include <linux/sched/mm.h>
Jérôme Glisse7b2d55d22017-09-08 16:11:46 -070024#include <linux/jump_label.h>
Jérôme Glisse55c0ece2019-05-13 17:20:28 -070025#include <linux/dma-mapping.h>
Jérôme Glissec0b12402017-09-08 16:11:27 -070026#include <linux/mmu_notifier.h>
Jérôme Glisse4ef589d2017-09-08 16:11:58 -070027#include <linux/memory_hotplug.h>
28
Jérôme Glissec0b12402017-09-08 16:11:27 -070029static const struct mmu_notifier_ops hmm_mmu_notifier_ops;
30
Jérôme Glisse704f3f22019-05-13 17:19:48 -070031/**
32 * hmm_get_or_create - register HMM against an mm (HMM internal)
33 *
34 * @mm: mm struct to attach to
Ralph Campbelld2e8d552019-07-25 17:56:45 -070035 * Return: an HMM object, either by referencing the existing
Jérôme Glisse704f3f22019-05-13 17:19:48 -070036 * (per-process) object, or by creating a new one.
37 *
38 * This is not intended to be used directly by device drivers. If mm already
39 * has an HMM struct then it get a reference on it and returns it. Otherwise
40 * it allocates an HMM struct, initializes it, associate it with the mm and
41 * returns it.
42 */
43static struct hmm *hmm_get_or_create(struct mm_struct *mm)
44{
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -030045 struct hmm *hmm;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070046
Linus Torvaldsfec88ab2019-07-14 19:42:11 -070047 lockdep_assert_held_write(&mm->mmap_sem);
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -030048
49 /* Abuse the page_table_lock to also protect mm->hmm. */
50 spin_lock(&mm->page_table_lock);
51 hmm = mm->hmm;
52 if (mm->hmm && kref_get_unless_zero(&mm->hmm->kref))
53 goto out_unlock;
54 spin_unlock(&mm->page_table_lock);
Jérôme Glissec0b12402017-09-08 16:11:27 -070055
56 hmm = kmalloc(sizeof(*hmm), GFP_KERNEL);
57 if (!hmm)
58 return NULL;
Jérôme Glissea3e0d412019-05-13 17:20:01 -070059 init_waitqueue_head(&hmm->wq);
Jérôme Glissec0b12402017-09-08 16:11:27 -070060 INIT_LIST_HEAD(&hmm->mirrors);
61 init_rwsem(&hmm->mirrors_sem);
Jérôme Glissec0b12402017-09-08 16:11:27 -070062 hmm->mmu_notifier.ops = NULL;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070063 INIT_LIST_HEAD(&hmm->ranges);
Jason Gunthorpe5a136b42019-06-07 12:10:33 -030064 spin_lock_init(&hmm->ranges_lock);
Jérôme Glisse704f3f22019-05-13 17:19:48 -070065 kref_init(&hmm->kref);
Jérôme Glissea3e0d412019-05-13 17:20:01 -070066 hmm->notifiers = 0;
Jérôme Glissec0b12402017-09-08 16:11:27 -070067 hmm->mm = mm;
68
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -030069 hmm->mmu_notifier.ops = &hmm_mmu_notifier_ops;
70 if (__mmu_notifier_register(&hmm->mmu_notifier, mm)) {
71 kfree(hmm);
72 return NULL;
73 }
Jérôme Glissec0b12402017-09-08 16:11:27 -070074
Jason Gunthorpec8a53b22019-05-23 10:36:46 -030075 mmgrab(hmm->mm);
Ralph Campbell86a2d592018-10-30 15:04:14 -070076
77 /*
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -030078 * We hold the exclusive mmap_sem here so we know that mm->hmm is
79 * still NULL or 0 kref, and is safe to update.
Ralph Campbell86a2d592018-10-30 15:04:14 -070080 */
Ralph Campbell86a2d592018-10-30 15:04:14 -070081 spin_lock(&mm->page_table_lock);
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -030082 mm->hmm = hmm;
83
84out_unlock:
Ralph Campbell86a2d592018-10-30 15:04:14 -070085 spin_unlock(&mm->page_table_lock);
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -030086 return hmm;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070087}
88
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -030089static void hmm_free_rcu(struct rcu_head *rcu)
90{
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -030091 struct hmm *hmm = container_of(rcu, struct hmm, rcu);
92
93 mmdrop(hmm->mm);
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070094 kfree(hmm);
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070095}
96
Jérôme Glisse704f3f22019-05-13 17:19:48 -070097static void hmm_free(struct kref *kref)
98{
99 struct hmm *hmm = container_of(kref, struct hmm, kref);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700100
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -0300101 spin_lock(&hmm->mm->page_table_lock);
102 if (hmm->mm->hmm == hmm)
103 hmm->mm->hmm = NULL;
104 spin_unlock(&hmm->mm->page_table_lock);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700105
Jason Gunthorpe8a9320b2019-05-23 10:24:13 -0300106 mmu_notifier_unregister_no_release(&hmm->mmu_notifier, hmm->mm);
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300107 mmu_notifier_call_srcu(&hmm->rcu, hmm_free_rcu);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700108}
109
110static inline void hmm_put(struct hmm *hmm)
111{
112 kref_put(&hmm->kref, hmm_free);
113}
114
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700115static void hmm_release(struct mmu_notifier *mn, struct mm_struct *mm)
Jérôme Glissec0b12402017-09-08 16:11:27 -0700116{
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300117 struct hmm *hmm = container_of(mn, struct hmm, mmu_notifier);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700118 struct hmm_mirror *mirror;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700119
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300120 /* Bail out if hmm is in the process of being freed */
121 if (!kref_get_unless_zero(&hmm->kref))
122 return;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700123
Jason Gunthorpe47f24592019-05-23 11:08:28 -0300124 /*
125 * Since hmm_range_register() holds the mmget() lock hmm_release() is
126 * prevented as long as a range exists.
127 */
128 WARN_ON(!list_empty_careful(&hmm->ranges));
Ralph Campbelle1401512018-04-10 16:28:19 -0700129
Jason Gunthorpe14331722019-05-24 12:14:08 -0300130 down_read(&hmm->mirrors_sem);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700131 list_for_each_entry(mirror, &hmm->mirrors, list) {
Jason Gunthorpe14331722019-05-24 12:14:08 -0300132 /*
133 * Note: The driver is not allowed to trigger
134 * hmm_mirror_unregister() from this thread.
135 */
136 if (mirror->ops->release)
Ralph Campbelle1401512018-04-10 16:28:19 -0700137 mirror->ops->release(mirror);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700138 }
139 up_read(&hmm->mirrors_sem);
140
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700141 hmm_put(hmm);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700142}
143
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300144static void notifiers_decrement(struct hmm *hmm)
Jérôme Glissec0b12402017-09-08 16:11:27 -0700145{
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300146 unsigned long flags;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700147
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300148 spin_lock_irqsave(&hmm->ranges_lock, flags);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700149 hmm->notifiers--;
150 if (!hmm->notifiers) {
151 struct hmm_range *range;
152
153 list_for_each_entry(range, &hmm->ranges, list) {
154 if (range->valid)
155 continue;
156 range->valid = true;
157 }
158 wake_up_all(&hmm->wq);
159 }
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300160 spin_unlock_irqrestore(&hmm->ranges_lock, flags);
161}
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700162
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700163static int hmm_invalidate_range_start(struct mmu_notifier *mn,
164 const struct mmu_notifier_range *nrange)
165{
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300166 struct hmm *hmm = container_of(mn, struct hmm, mmu_notifier);
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700167 struct hmm_mirror *mirror;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700168 struct hmm_range *range;
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300169 unsigned long flags;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700170 int ret = 0;
171
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300172 if (!kref_get_unless_zero(&hmm->kref))
173 return 0;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700174
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300175 spin_lock_irqsave(&hmm->ranges_lock, flags);
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700176 hmm->notifiers++;
177 list_for_each_entry(range, &hmm->ranges, list) {
Ralph Campbell1f961802019-07-25 17:56:44 -0700178 if (nrange->end < range->start || nrange->start >= range->end)
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700179 continue;
180
181 range->valid = false;
182 }
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300183 spin_unlock_irqrestore(&hmm->ranges_lock, flags);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700184
185 if (mmu_notifier_range_blockable(nrange))
186 down_read(&hmm->mirrors_sem);
187 else if (!down_read_trylock(&hmm->mirrors_sem)) {
188 ret = -EAGAIN;
189 goto out;
190 }
Jérôme Glissec0b12402017-09-08 16:11:27 -0700191
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300192 list_for_each_entry(mirror, &hmm->mirrors, list) {
193 int rc;
194
Ralph Campbell1f961802019-07-25 17:56:44 -0700195 rc = mirror->ops->sync_cpu_device_pagetables(mirror, nrange);
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300196 if (rc) {
Ralph Campbell1f961802019-07-25 17:56:44 -0700197 if (WARN_ON(mmu_notifier_range_blockable(nrange) ||
198 rc != -EAGAIN))
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300199 continue;
200 ret = -EAGAIN;
Ralph Campbell085ea252019-05-06 16:29:39 -0700201 break;
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300202 }
Jérôme Glissec0b12402017-09-08 16:11:27 -0700203 }
204 up_read(&hmm->mirrors_sem);
205
206out:
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300207 if (ret)
208 notifiers_decrement(hmm);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700209 hmm_put(hmm);
210 return ret;
211}
212
213static void hmm_invalidate_range_end(struct mmu_notifier *mn,
214 const struct mmu_notifier_range *nrange)
215{
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300216 struct hmm *hmm = container_of(mn, struct hmm, mmu_notifier);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700217
Jason Gunthorpe6d7c3cd2019-05-22 16:52:52 -0300218 if (!kref_get_unless_zero(&hmm->kref))
219 return;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700220
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300221 notifiers_decrement(hmm);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700222 hmm_put(hmm);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700223}
224
225static const struct mmu_notifier_ops hmm_mmu_notifier_ops = {
Ralph Campbelle1401512018-04-10 16:28:19 -0700226 .release = hmm_release,
Jérôme Glissec0b12402017-09-08 16:11:27 -0700227 .invalidate_range_start = hmm_invalidate_range_start,
228 .invalidate_range_end = hmm_invalidate_range_end,
229};
230
231/*
232 * hmm_mirror_register() - register a mirror against an mm
233 *
234 * @mirror: new mirror struct to register
235 * @mm: mm to register against
Ralph Campbell085ea252019-05-06 16:29:39 -0700236 * Return: 0 on success, -ENOMEM if no memory, -EINVAL if invalid arguments
Jérôme Glissec0b12402017-09-08 16:11:27 -0700237 *
238 * To start mirroring a process address space, the device driver must register
239 * an HMM mirror struct.
Jérôme Glissec0b12402017-09-08 16:11:27 -0700240 */
241int hmm_mirror_register(struct hmm_mirror *mirror, struct mm_struct *mm)
242{
Linus Torvaldsfec88ab2019-07-14 19:42:11 -0700243 lockdep_assert_held_write(&mm->mmap_sem);
Jason Gunthorpe8a1a0cd2019-05-23 11:23:30 -0300244
Jérôme Glissec0b12402017-09-08 16:11:27 -0700245 /* Sanity check */
246 if (!mm || !mirror || !mirror->ops)
247 return -EINVAL;
248
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700249 mirror->hmm = hmm_get_or_create(mm);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700250 if (!mirror->hmm)
251 return -ENOMEM;
252
253 down_write(&mirror->hmm->mirrors_sem);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700254 list_add(&mirror->list, &mirror->hmm->mirrors);
255 up_write(&mirror->hmm->mirrors_sem);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700256
257 return 0;
258}
259EXPORT_SYMBOL(hmm_mirror_register);
260
261/*
262 * hmm_mirror_unregister() - unregister a mirror
263 *
Ralph Campbell085ea252019-05-06 16:29:39 -0700264 * @mirror: mirror struct to unregister
Jérôme Glissec0b12402017-09-08 16:11:27 -0700265 *
266 * Stop mirroring a process address space, and cleanup.
267 */
268void hmm_mirror_unregister(struct hmm_mirror *mirror)
269{
Jason Gunthorpe187229c2019-05-23 11:31:45 -0300270 struct hmm *hmm = mirror->hmm;
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700271
Jérôme Glissec0b12402017-09-08 16:11:27 -0700272 down_write(&hmm->mirrors_sem);
Jason Gunthorpe14331722019-05-24 12:14:08 -0300273 list_del(&mirror->list);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700274 up_write(&hmm->mirrors_sem);
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700275 hmm_put(hmm);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700276}
277EXPORT_SYMBOL(hmm_mirror_unregister);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700278
Jérôme Glisse74eee182017-09-08 16:11:35 -0700279struct hmm_vma_walk {
280 struct hmm_range *range;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700281 struct dev_pagemap *pgmap;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700282 unsigned long last;
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700283 unsigned int flags;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700284};
285
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700286static int hmm_vma_do_fault(struct mm_walk *walk, unsigned long addr,
287 bool write_fault, uint64_t *pfn)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700288{
Kuehling, Felix9b1ae602019-05-10 19:53:24 +0000289 unsigned int flags = FAULT_FLAG_REMOTE;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700290 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700291 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700292 struct vm_area_struct *vma = walk->vma;
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700293 vm_fault_t ret;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700294
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700295 if (hmm_vma_walk->flags & HMM_FAULT_ALLOW_RETRY)
296 flags |= FAULT_FLAG_ALLOW_RETRY;
297 if (write_fault)
298 flags |= FAULT_FLAG_WRITE;
299
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700300 ret = handle_mm_fault(vma, addr, flags);
Jason Gunthorpee709acc2019-07-24 08:52:58 +0200301 if (ret & VM_FAULT_RETRY) {
302 /* Note, handle_mm_fault did up_read(&mm->mmap_sem)) */
Jérôme Glisse73231612019-05-13 17:19:58 -0700303 return -EAGAIN;
Jason Gunthorpee709acc2019-07-24 08:52:58 +0200304 }
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700305 if (ret & VM_FAULT_ERROR) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700306 *pfn = range->values[HMM_PFN_ERROR];
Jérôme Glisse74eee182017-09-08 16:11:35 -0700307 return -EFAULT;
308 }
309
Jérôme Glisse73231612019-05-13 17:19:58 -0700310 return -EBUSY;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700311}
312
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700313static int hmm_pfns_bad(unsigned long addr,
314 unsigned long end,
315 struct mm_walk *walk)
316{
Jérôme Glissec7195472018-04-10 16:28:27 -0700317 struct hmm_vma_walk *hmm_vma_walk = walk->private;
318 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700319 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700320 unsigned long i;
321
322 i = (addr - range->start) >> PAGE_SHIFT;
323 for (; addr < end; addr += PAGE_SIZE, i++)
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700324 pfns[i] = range->values[HMM_PFN_ERROR];
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700325
326 return 0;
327}
328
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700329/*
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700330 * hmm_vma_walk_hole_() - handle a range lacking valid pmd or pte(s)
331 * @addr: range virtual start address (inclusive)
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700332 * @end: range virtual end address (exclusive)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700333 * @fault: should we fault or not ?
334 * @write_fault: write fault ?
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700335 * @walk: mm_walk structure
Ralph Campbell085ea252019-05-06 16:29:39 -0700336 * Return: 0 on success, -EBUSY after page fault, or page fault error
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700337 *
338 * This function will be called whenever pmd_none() or pte_none() returns true,
339 * or whenever there is no page directory covering the virtual address range.
340 */
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700341static int hmm_vma_walk_hole_(unsigned long addr, unsigned long end,
342 bool fault, bool write_fault,
343 struct mm_walk *walk)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700344{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700345 struct hmm_vma_walk *hmm_vma_walk = walk->private;
346 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700347 uint64_t *pfns = range->pfns;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700348 unsigned long i, page_size;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700349
Jérôme Glisse74eee182017-09-08 16:11:35 -0700350 hmm_vma_walk->last = addr;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700351 page_size = hmm_range_page_size(range);
352 i = (addr - range->start) >> range->page_shift;
353
354 for (; addr < end; addr += page_size, i++) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700355 pfns[i] = range->values[HMM_PFN_NONE];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700356 if (fault || write_fault) {
Jérôme Glisse74eee182017-09-08 16:11:35 -0700357 int ret;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700358
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700359 ret = hmm_vma_do_fault(walk, addr, write_fault,
360 &pfns[i]);
Jérôme Glisse73231612019-05-13 17:19:58 -0700361 if (ret != -EBUSY)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700362 return ret;
363 }
364 }
365
Jérôme Glisse73231612019-05-13 17:19:58 -0700366 return (fault || write_fault) ? -EBUSY : 0;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700367}
368
369static inline void hmm_pte_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
370 uint64_t pfns, uint64_t cpu_flags,
371 bool *fault, bool *write_fault)
372{
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700373 struct hmm_range *range = hmm_vma_walk->range;
374
Christoph Hellwigd45d4642019-07-25 17:56:47 -0700375 if (hmm_vma_walk->flags & HMM_FAULT_SNAPSHOT)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700376 return;
377
Jérôme Glisse023a0192019-05-13 17:20:05 -0700378 /*
379 * So we not only consider the individual per page request we also
380 * consider the default flags requested for the range. The API can
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700381 * be used 2 ways. The first one where the HMM user coalesces
382 * multiple page faults into one request and sets flags per pfn for
383 * those faults. The second one where the HMM user wants to pre-
Jérôme Glisse023a0192019-05-13 17:20:05 -0700384 * fault a range with specific flags. For the latter one it is a
385 * waste to have the user pre-fill the pfn arrays with a default
386 * flags value.
387 */
388 pfns = (pfns & range->pfn_flags_mask) | range->default_flags;
389
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700390 /* We aren't ask to do anything ... */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700391 if (!(pfns & range->flags[HMM_PFN_VALID]))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700392 return;
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700393 /* If this is device memory then only fault if explicitly requested */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700394 if ((cpu_flags & range->flags[HMM_PFN_DEVICE_PRIVATE])) {
395 /* Do we fault on device memory ? */
396 if (pfns & range->flags[HMM_PFN_DEVICE_PRIVATE]) {
397 *write_fault = pfns & range->flags[HMM_PFN_WRITE];
398 *fault = true;
399 }
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700400 return;
401 }
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700402
403 /* If CPU page table is not valid then we need to fault */
404 *fault = !(cpu_flags & range->flags[HMM_PFN_VALID]);
405 /* Need to write fault ? */
406 if ((pfns & range->flags[HMM_PFN_WRITE]) &&
407 !(cpu_flags & range->flags[HMM_PFN_WRITE])) {
408 *write_fault = true;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700409 *fault = true;
410 }
411}
412
413static void hmm_range_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
414 const uint64_t *pfns, unsigned long npages,
415 uint64_t cpu_flags, bool *fault,
416 bool *write_fault)
417{
418 unsigned long i;
419
Christoph Hellwigd45d4642019-07-25 17:56:47 -0700420 if (hmm_vma_walk->flags & HMM_FAULT_SNAPSHOT) {
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700421 *fault = *write_fault = false;
422 return;
423 }
424
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700425 *fault = *write_fault = false;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700426 for (i = 0; i < npages; ++i) {
427 hmm_pte_need_fault(hmm_vma_walk, pfns[i], cpu_flags,
428 fault, write_fault);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700429 if ((*write_fault))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700430 return;
431 }
432}
433
434static int hmm_vma_walk_hole(unsigned long addr, unsigned long end,
435 struct mm_walk *walk)
436{
437 struct hmm_vma_walk *hmm_vma_walk = walk->private;
438 struct hmm_range *range = hmm_vma_walk->range;
439 bool fault, write_fault;
440 unsigned long i, npages;
441 uint64_t *pfns;
442
443 i = (addr - range->start) >> PAGE_SHIFT;
444 npages = (end - addr) >> PAGE_SHIFT;
445 pfns = &range->pfns[i];
446 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
447 0, &fault, &write_fault);
448 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
449}
450
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700451static inline uint64_t pmd_to_hmm_pfn_flags(struct hmm_range *range, pmd_t pmd)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700452{
453 if (pmd_protnone(pmd))
454 return 0;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700455 return pmd_write(pmd) ? range->flags[HMM_PFN_VALID] |
456 range->flags[HMM_PFN_WRITE] :
457 range->flags[HMM_PFN_VALID];
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700458}
459
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700460static inline uint64_t pud_to_hmm_pfn_flags(struct hmm_range *range, pud_t pud)
461{
462 if (!pud_present(pud))
463 return 0;
464 return pud_write(pud) ? range->flags[HMM_PFN_VALID] |
465 range->flags[HMM_PFN_WRITE] :
466 range->flags[HMM_PFN_VALID];
467}
468
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700469static int hmm_vma_handle_pmd(struct mm_walk *walk,
470 unsigned long addr,
471 unsigned long end,
472 uint64_t *pfns,
473 pmd_t pmd)
474{
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700475#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700476 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700477 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700478 unsigned long pfn, npages, i;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700479 bool fault, write_fault;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700480 uint64_t cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700481
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700482 npages = (end - addr) >> PAGE_SHIFT;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700483 cpu_flags = pmd_to_hmm_pfn_flags(range, pmd);
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700484 hmm_range_need_fault(hmm_vma_walk, pfns, npages, cpu_flags,
485 &fault, &write_fault);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700486
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700487 if (pmd_protnone(pmd) || fault || write_fault)
488 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700489
490 pfn = pmd_pfn(pmd) + pte_index(addr);
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700491 for (i = 0; addr < end; addr += PAGE_SIZE, i++, pfn++) {
492 if (pmd_devmap(pmd)) {
493 hmm_vma_walk->pgmap = get_dev_pagemap(pfn,
494 hmm_vma_walk->pgmap);
495 if (unlikely(!hmm_vma_walk->pgmap))
496 return -EBUSY;
497 }
Jérôme Glisse391aab12019-05-13 17:20:31 -0700498 pfns[i] = hmm_device_entry_from_pfn(range, pfn) | cpu_flags;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700499 }
500 if (hmm_vma_walk->pgmap) {
501 put_dev_pagemap(hmm_vma_walk->pgmap);
502 hmm_vma_walk->pgmap = NULL;
503 }
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700504 hmm_vma_walk->last = end;
505 return 0;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700506#else
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700507 /* If THP is not enabled then we should never reach this code ! */
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700508 return -EINVAL;
509#endif
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700510}
511
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700512static inline uint64_t pte_to_hmm_pfn_flags(struct hmm_range *range, pte_t pte)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700513{
Philip Yang789c2af2019-05-23 16:32:31 -0400514 if (pte_none(pte) || !pte_present(pte) || pte_protnone(pte))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700515 return 0;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700516 return pte_write(pte) ? range->flags[HMM_PFN_VALID] |
517 range->flags[HMM_PFN_WRITE] :
518 range->flags[HMM_PFN_VALID];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700519}
520
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700521static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,
522 unsigned long end, pmd_t *pmdp, pte_t *ptep,
523 uint64_t *pfn)
524{
525 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700526 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700527 bool fault, write_fault;
528 uint64_t cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700529 pte_t pte = *ptep;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700530 uint64_t orig_pfn = *pfn;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700531
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700532 *pfn = range->values[HMM_PFN_NONE];
Jérôme Glisse73231612019-05-13 17:19:58 -0700533 fault = write_fault = false;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700534
535 if (pte_none(pte)) {
Jérôme Glisse73231612019-05-13 17:19:58 -0700536 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, 0,
537 &fault, &write_fault);
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700538 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700539 goto fault;
540 return 0;
541 }
542
543 if (!pte_present(pte)) {
544 swp_entry_t entry = pte_to_swp_entry(pte);
545
546 if (!non_swap_entry(entry)) {
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700547 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700548 goto fault;
549 return 0;
550 }
551
552 /*
553 * This is a special swap entry, ignore migration, use
554 * device and report anything else as error.
555 */
556 if (is_device_private_entry(entry)) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700557 cpu_flags = range->flags[HMM_PFN_VALID] |
558 range->flags[HMM_PFN_DEVICE_PRIVATE];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700559 cpu_flags |= is_write_device_private_entry(entry) ?
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700560 range->flags[HMM_PFN_WRITE] : 0;
561 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
562 &fault, &write_fault);
563 if (fault || write_fault)
564 goto fault;
Jérôme Glisse391aab12019-05-13 17:20:31 -0700565 *pfn = hmm_device_entry_from_pfn(range,
566 swp_offset(entry));
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700567 *pfn |= cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700568 return 0;
569 }
570
571 if (is_migration_entry(entry)) {
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700572 if (fault || write_fault) {
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700573 pte_unmap(ptep);
574 hmm_vma_walk->last = addr;
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700575 migration_entry_wait(walk->mm, pmdp, addr);
Jérôme Glisse73231612019-05-13 17:19:58 -0700576 return -EBUSY;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700577 }
578 return 0;
579 }
580
581 /* Report error for everything else */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700582 *pfn = range->values[HMM_PFN_ERROR];
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700583 return -EFAULT;
Jérôme Glisse73231612019-05-13 17:19:58 -0700584 } else {
585 cpu_flags = pte_to_hmm_pfn_flags(range, pte);
586 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
587 &fault, &write_fault);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700588 }
589
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700590 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700591 goto fault;
592
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700593 if (pte_devmap(pte)) {
594 hmm_vma_walk->pgmap = get_dev_pagemap(pte_pfn(pte),
595 hmm_vma_walk->pgmap);
596 if (unlikely(!hmm_vma_walk->pgmap))
597 return -EBUSY;
598 } else if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) && pte_special(pte)) {
599 *pfn = range->values[HMM_PFN_SPECIAL];
600 return -EFAULT;
601 }
602
Jérôme Glisse391aab12019-05-13 17:20:31 -0700603 *pfn = hmm_device_entry_from_pfn(range, pte_pfn(pte)) | cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700604 return 0;
605
606fault:
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700607 if (hmm_vma_walk->pgmap) {
608 put_dev_pagemap(hmm_vma_walk->pgmap);
609 hmm_vma_walk->pgmap = NULL;
610 }
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700611 pte_unmap(ptep);
612 /* Fault any virtual address we were asked to fault */
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700613 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700614}
615
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700616static int hmm_vma_walk_pmd(pmd_t *pmdp,
617 unsigned long start,
618 unsigned long end,
619 struct mm_walk *walk)
620{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700621 struct hmm_vma_walk *hmm_vma_walk = walk->private;
622 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700623 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700624 unsigned long addr = start, i;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700625 pte_t *ptep;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700626 pmd_t pmd;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700627
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700628again:
Jérôme Glissed08faca2018-10-30 15:04:20 -0700629 pmd = READ_ONCE(*pmdp);
630 if (pmd_none(pmd))
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700631 return hmm_vma_walk_hole(start, end, walk);
632
Jérôme Glissed08faca2018-10-30 15:04:20 -0700633 if (thp_migration_supported() && is_pmd_migration_entry(pmd)) {
634 bool fault, write_fault;
635 unsigned long npages;
636 uint64_t *pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700637
Jérôme Glissed08faca2018-10-30 15:04:20 -0700638 i = (addr - range->start) >> PAGE_SHIFT;
639 npages = (end - addr) >> PAGE_SHIFT;
640 pfns = &range->pfns[i];
641
642 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
643 0, &fault, &write_fault);
644 if (fault || write_fault) {
645 hmm_vma_walk->last = addr;
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700646 pmd_migration_entry_wait(walk->mm, pmdp);
Jérôme Glisse73231612019-05-13 17:19:58 -0700647 return -EBUSY;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700648 }
649 return 0;
650 } else if (!pmd_present(pmd))
651 return hmm_pfns_bad(start, end, walk);
652
653 if (pmd_devmap(pmd) || pmd_trans_huge(pmd)) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700654 /*
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700655 * No need to take pmd_lock here, even if some other thread
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700656 * is splitting the huge pmd we will get that event through
657 * mmu_notifier callback.
658 *
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700659 * So just read pmd value and check again it's a transparent
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700660 * huge or device mapping one and compute corresponding pfn
661 * values.
662 */
663 pmd = pmd_read_atomic(pmdp);
664 barrier();
665 if (!pmd_devmap(pmd) && !pmd_trans_huge(pmd))
666 goto again;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700667
Jérôme Glissed08faca2018-10-30 15:04:20 -0700668 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700669 return hmm_vma_handle_pmd(walk, addr, end, &pfns[i], pmd);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700670 }
671
Jérôme Glissed08faca2018-10-30 15:04:20 -0700672 /*
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700673 * We have handled all the valid cases above ie either none, migration,
Jérôme Glissed08faca2018-10-30 15:04:20 -0700674 * huge or transparent huge. At this point either it is a valid pmd
675 * entry pointing to pte directory or it is a bad pmd that will not
676 * recover.
677 */
678 if (pmd_bad(pmd))
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700679 return hmm_pfns_bad(start, end, walk);
680
681 ptep = pte_offset_map(pmdp, addr);
Jérôme Glissed08faca2018-10-30 15:04:20 -0700682 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700683 for (; addr < end; addr += PAGE_SIZE, ptep++, i++) {
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700684 int r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700685
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700686 r = hmm_vma_handle_pte(walk, addr, end, pmdp, ptep, &pfns[i]);
687 if (r) {
688 /* hmm_vma_handle_pte() did unmap pte directory */
689 hmm_vma_walk->last = addr;
690 return r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700691 }
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700692 }
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700693 if (hmm_vma_walk->pgmap) {
694 /*
695 * We do put_dev_pagemap() here and not in hmm_vma_handle_pte()
696 * so that we can leverage get_dev_pagemap() optimization which
697 * will not re-take a reference on a pgmap if we already have
698 * one.
699 */
700 put_dev_pagemap(hmm_vma_walk->pgmap);
701 hmm_vma_walk->pgmap = NULL;
702 }
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700703 pte_unmap(ptep - 1);
704
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700705 hmm_vma_walk->last = addr;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700706 return 0;
707}
708
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700709static int hmm_vma_walk_pud(pud_t *pudp,
710 unsigned long start,
711 unsigned long end,
712 struct mm_walk *walk)
713{
714 struct hmm_vma_walk *hmm_vma_walk = walk->private;
715 struct hmm_range *range = hmm_vma_walk->range;
716 unsigned long addr = start, next;
717 pmd_t *pmdp;
718 pud_t pud;
719 int ret;
720
721again:
722 pud = READ_ONCE(*pudp);
723 if (pud_none(pud))
724 return hmm_vma_walk_hole(start, end, walk);
725
726 if (pud_huge(pud) && pud_devmap(pud)) {
727 unsigned long i, npages, pfn;
728 uint64_t *pfns, cpu_flags;
729 bool fault, write_fault;
730
731 if (!pud_present(pud))
732 return hmm_vma_walk_hole(start, end, walk);
733
734 i = (addr - range->start) >> PAGE_SHIFT;
735 npages = (end - addr) >> PAGE_SHIFT;
736 pfns = &range->pfns[i];
737
738 cpu_flags = pud_to_hmm_pfn_flags(range, pud);
739 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
740 cpu_flags, &fault, &write_fault);
741 if (fault || write_fault)
742 return hmm_vma_walk_hole_(addr, end, fault,
743 write_fault, walk);
744
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700745 pfn = pud_pfn(pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
746 for (i = 0; i < npages; ++i, ++pfn) {
747 hmm_vma_walk->pgmap = get_dev_pagemap(pfn,
748 hmm_vma_walk->pgmap);
749 if (unlikely(!hmm_vma_walk->pgmap))
750 return -EBUSY;
Jérôme Glisse391aab12019-05-13 17:20:31 -0700751 pfns[i] = hmm_device_entry_from_pfn(range, pfn) |
752 cpu_flags;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700753 }
754 if (hmm_vma_walk->pgmap) {
755 put_dev_pagemap(hmm_vma_walk->pgmap);
756 hmm_vma_walk->pgmap = NULL;
757 }
758 hmm_vma_walk->last = end;
759 return 0;
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700760 }
761
762 split_huge_pud(walk->vma, pudp, addr);
763 if (pud_none(*pudp))
764 goto again;
765
766 pmdp = pmd_offset(pudp, addr);
767 do {
768 next = pmd_addr_end(addr, end);
769 ret = hmm_vma_walk_pmd(pmdp, addr, next, walk);
770 if (ret)
771 return ret;
772 } while (pmdp++, addr = next, addr != end);
773
774 return 0;
775}
776
Jérôme Glisse63d50662019-05-13 17:20:18 -0700777static int hmm_vma_walk_hugetlb_entry(pte_t *pte, unsigned long hmask,
778 unsigned long start, unsigned long end,
779 struct mm_walk *walk)
780{
781#ifdef CONFIG_HUGETLB_PAGE
782 unsigned long addr = start, i, pfn, mask, size, pfn_inc;
783 struct hmm_vma_walk *hmm_vma_walk = walk->private;
784 struct hmm_range *range = hmm_vma_walk->range;
785 struct vm_area_struct *vma = walk->vma;
786 struct hstate *h = hstate_vma(vma);
787 uint64_t orig_pfn, cpu_flags;
788 bool fault, write_fault;
789 spinlock_t *ptl;
790 pte_t entry;
791 int ret = 0;
792
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700793 size = huge_page_size(h);
Jérôme Glisse63d50662019-05-13 17:20:18 -0700794 mask = size - 1;
795 if (range->page_shift != PAGE_SHIFT) {
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700796 /* Make sure we are looking at a full page. */
Jérôme Glisse63d50662019-05-13 17:20:18 -0700797 if (start & mask)
798 return -EINVAL;
799 if (end < (start + size))
800 return -EINVAL;
801 pfn_inc = size >> PAGE_SHIFT;
802 } else {
803 pfn_inc = 1;
804 size = PAGE_SIZE;
805 }
806
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700807 ptl = huge_pte_lock(hstate_vma(vma), walk->mm, pte);
Jérôme Glisse63d50662019-05-13 17:20:18 -0700808 entry = huge_ptep_get(pte);
809
810 i = (start - range->start) >> range->page_shift;
811 orig_pfn = range->pfns[i];
812 range->pfns[i] = range->values[HMM_PFN_NONE];
813 cpu_flags = pte_to_hmm_pfn_flags(range, entry);
814 fault = write_fault = false;
815 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
816 &fault, &write_fault);
817 if (fault || write_fault) {
818 ret = -ENOENT;
819 goto unlock;
820 }
821
822 pfn = pte_pfn(entry) + ((start & mask) >> range->page_shift);
823 for (; addr < end; addr += size, i++, pfn += pfn_inc)
Jérôme Glisse391aab12019-05-13 17:20:31 -0700824 range->pfns[i] = hmm_device_entry_from_pfn(range, pfn) |
825 cpu_flags;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700826 hmm_vma_walk->last = end;
827
828unlock:
829 spin_unlock(ptl);
830
831 if (ret == -ENOENT)
832 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
833
834 return ret;
835#else /* CONFIG_HUGETLB_PAGE */
836 return -EINVAL;
837#endif
838}
839
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700840static void hmm_pfns_clear(struct hmm_range *range,
841 uint64_t *pfns,
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700842 unsigned long addr,
843 unsigned long end)
844{
845 for (; addr < end; addr += PAGE_SIZE, pfns++)
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700846 *pfns = range->values[HMM_PFN_NONE];
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700847}
848
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700849/*
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700850 * hmm_range_register() - start tracking change to CPU page table over a range
Jérôme Glisse25f23a02019-05-13 17:19:55 -0700851 * @range: range
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700852 * @mm: the mm struct for the range of virtual address
Christoph Hellwigfac555a2019-08-06 19:05:44 +0300853 *
Ralph Campbelld2e8d552019-07-25 17:56:45 -0700854 * Return: 0 on success, -EFAULT if the address space is no longer valid
Jérôme Glisse25f23a02019-05-13 17:19:55 -0700855 *
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700856 * Track updates to the CPU page table see include/linux/hmm.h
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700857 */
Christoph Hellwigfac555a2019-08-06 19:05:44 +0300858int hmm_range_register(struct hmm_range *range, struct hmm_mirror *mirror)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700859{
Christoph Hellwigfac555a2019-08-06 19:05:44 +0300860 unsigned long mask = ((1UL << range->page_shift) - 1UL);
Jason Gunthorpee36acfe2019-05-23 09:41:19 -0300861 struct hmm *hmm = mirror->hmm;
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300862 unsigned long flags;
Jérôme Glisse63d50662019-05-13 17:20:18 -0700863
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700864 range->valid = false;
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700865 range->hmm = NULL;
866
Christoph Hellwigfac555a2019-08-06 19:05:44 +0300867 if ((range->start & mask) || (range->end & mask))
Jérôme Glisse63d50662019-05-13 17:20:18 -0700868 return -EINVAL;
Christoph Hellwigfac555a2019-08-06 19:05:44 +0300869 if (range->start >= range->end)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700870 return -EINVAL;
871
Jason Gunthorpe47f24592019-05-23 11:08:28 -0300872 /* Prevent hmm_release() from running while the range is valid */
873 if (!mmget_not_zero(hmm->mm))
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700874 return -EFAULT;
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700875
Ralph Campbell085ea252019-05-06 16:29:39 -0700876 /* Initialize range to track CPU page table updates. */
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300877 spin_lock_irqsave(&hmm->ranges_lock, flags);
Jérôme Glisse86586a42018-04-10 16:28:34 -0700878
Ralph Campbell085ea252019-05-06 16:29:39 -0700879 range->hmm = hmm;
Jason Gunthorpee36acfe2019-05-23 09:41:19 -0300880 kref_get(&hmm->kref);
Jason Gunthorpe157816f2019-05-23 11:43:43 -0300881 list_add(&range->list, &hmm->ranges);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700882
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700883 /*
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700884 * If there are any concurrent notifiers we have to wait for them for
885 * the range to be valid (see hmm_range_wait_until_valid()).
Jérôme Glisse704f3f22019-05-13 17:19:48 -0700886 */
Ralph Campbell085ea252019-05-06 16:29:39 -0700887 if (!hmm->notifiers)
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700888 range->valid = true;
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300889 spin_unlock_irqrestore(&hmm->ranges_lock, flags);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700890
891 return 0;
892}
893EXPORT_SYMBOL(hmm_range_register);
894
895/*
896 * hmm_range_unregister() - stop tracking change to CPU page table over a range
897 * @range: range
898 *
899 * Range struct is used to track updates to the CPU page table after a call to
900 * hmm_range_register(). See include/linux/hmm.h for how to use it.
901 */
902void hmm_range_unregister(struct hmm_range *range)
903{
Ralph Campbell085ea252019-05-06 16:29:39 -0700904 struct hmm *hmm = range->hmm;
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300905 unsigned long flags;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700906
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300907 spin_lock_irqsave(&hmm->ranges_lock, flags);
Jason Gunthorpe47f24592019-05-23 11:08:28 -0300908 list_del_init(&range->list);
Jason Gunthorpe5a136b42019-06-07 12:10:33 -0300909 spin_unlock_irqrestore(&hmm->ranges_lock, flags);
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700910
911 /* Drop reference taken by hmm_range_register() */
Jason Gunthorpe47f24592019-05-23 11:08:28 -0300912 mmput(hmm->mm);
Ralph Campbell085ea252019-05-06 16:29:39 -0700913 hmm_put(hmm);
Jason Gunthorpe2dcc3eb2019-05-23 11:40:24 -0300914
915 /*
916 * The range is now invalid and the ref on the hmm is dropped, so
917 * poison the pointer. Leave other fields in place, for the caller's
918 * use.
919 */
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700920 range->valid = false;
Jason Gunthorpe2dcc3eb2019-05-23 11:40:24 -0300921 memset(&range->hmm, POISON_INUSE, sizeof(range->hmm));
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700922}
923EXPORT_SYMBOL(hmm_range_unregister);
924
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700925/**
926 * hmm_range_fault - try to fault some address in a virtual address range
927 * @range: range being faulted
928 * @flags: HMM_FAULT_* flags
Jérôme Glisse73231612019-05-13 17:19:58 -0700929 *
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700930 * Return: the number of valid pages in range->pfns[] (from range start
931 * address), which may be zero. On error one of the following status codes
932 * can be returned:
933 *
934 * -EINVAL: Invalid arguments or mm or virtual address is in an invalid vma
935 * (e.g., device file vma).
936 * -ENOMEM: Out of memory.
937 * -EPERM: Invalid permission (e.g., asking for write and range is read
938 * only).
939 * -EAGAIN: A page fault needs to be retried and mmap_sem was dropped.
940 * -EBUSY: The range has been invalidated and the caller needs to wait for
941 * the invalidation to finish.
942 * -EFAULT: Invalid (i.e., either no valid vma or it is illegal to access
943 * that range) number of valid pages in range->pfns[] (from
944 * range start address).
Jérôme Glisse74eee182017-09-08 16:11:35 -0700945 *
946 * This is similar to a regular CPU page fault except that it will not trigger
Jérôme Glisse73231612019-05-13 17:19:58 -0700947 * any memory migration if the memory being faulted is not accessible by CPUs
948 * and caller does not ask for migration.
Jérôme Glisse74eee182017-09-08 16:11:35 -0700949 *
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700950 * On error, for one virtual address in the range, the function will mark the
951 * corresponding HMM pfn entry with an error flag.
Jérôme Glisse74eee182017-09-08 16:11:35 -0700952 */
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700953long hmm_range_fault(struct hmm_range *range, unsigned int flags)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700954{
Jérôme Glisse63d50662019-05-13 17:20:18 -0700955 const unsigned long device_vma = VM_IO | VM_PFNMAP | VM_MIXEDMAP;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700956 unsigned long start = range->start, end;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700957 struct hmm_vma_walk hmm_vma_walk;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700958 struct hmm *hmm = range->hmm;
959 struct vm_area_struct *vma;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700960 struct mm_walk mm_walk;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700961 int ret;
962
Jason Gunthorpe47f24592019-05-23 11:08:28 -0300963 lockdep_assert_held(&hmm->mm->mmap_sem);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700964
965 do {
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700966 /* If range is no longer valid force retry. */
Christoph Hellwig2bcbeae2019-07-24 08:52:52 +0200967 if (!range->valid)
968 return -EBUSY;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700969
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700970 vma = find_vma(hmm->mm, start);
Jérôme Glisse63d50662019-05-13 17:20:18 -0700971 if (vma == NULL || (vma->vm_flags & device_vma))
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700972 return -EFAULT;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700973
Jérôme Glisse63d50662019-05-13 17:20:18 -0700974 if (is_vm_hugetlb_page(vma)) {
975 if (huge_page_shift(hstate_vma(vma)) !=
976 range->page_shift &&
977 range->page_shift != PAGE_SHIFT)
978 return -EINVAL;
979 } else {
980 if (range->page_shift != PAGE_SHIFT)
981 return -EINVAL;
982 }
983
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700984 if (!(vma->vm_flags & VM_READ)) {
985 /*
986 * If vma do not allow read access, then assume that it
987 * does not allow write access, either. HMM does not
988 * support architecture that allow write without read.
989 */
990 hmm_pfns_clear(range, range->pfns,
991 range->start, range->end);
992 return -EPERM;
993 }
994
Jérôme Glisse992de9a2019-05-13 17:20:21 -0700995 hmm_vma_walk.pgmap = NULL;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700996 hmm_vma_walk.last = start;
Christoph Hellwig9a4903e2019-07-25 17:56:46 -0700997 hmm_vma_walk.flags = flags;
Jérôme Glissea3e0d412019-05-13 17:20:01 -0700998 hmm_vma_walk.range = range;
999 mm_walk.private = &hmm_vma_walk;
1000 end = min(range->end, vma->vm_end);
1001
1002 mm_walk.vma = vma;
1003 mm_walk.mm = vma->vm_mm;
1004 mm_walk.pte_entry = NULL;
1005 mm_walk.test_walk = NULL;
1006 mm_walk.hugetlb_entry = NULL;
Jérôme Glisse992de9a2019-05-13 17:20:21 -07001007 mm_walk.pud_entry = hmm_vma_walk_pud;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001008 mm_walk.pmd_entry = hmm_vma_walk_pmd;
1009 mm_walk.pte_hole = hmm_vma_walk_hole;
Jérôme Glisse63d50662019-05-13 17:20:18 -07001010 mm_walk.hugetlb_entry = hmm_vma_walk_hugetlb_entry;
Jérôme Glissea3e0d412019-05-13 17:20:01 -07001011
1012 do {
1013 ret = walk_page_range(start, end, &mm_walk);
1014 start = hmm_vma_walk.last;
1015
1016 /* Keep trying while the range is valid. */
1017 } while (ret == -EBUSY && range->valid);
1018
1019 if (ret) {
1020 unsigned long i;
1021
1022 i = (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
1023 hmm_pfns_clear(range, &range->pfns[i],
1024 hmm_vma_walk.last, range->end);
1025 return ret;
1026 }
1027 start = end;
1028
1029 } while (start < range->end);
Jérôme Glisse704f3f22019-05-13 17:19:48 -07001030
Jérôme Glisse73231612019-05-13 17:19:58 -07001031 return (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
Jérôme Glisse74eee182017-09-08 16:11:35 -07001032}
Jérôme Glisse73231612019-05-13 17:19:58 -07001033EXPORT_SYMBOL(hmm_range_fault);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001034
1035/**
Christoph Hellwig9a4903e2019-07-25 17:56:46 -07001036 * hmm_range_dma_map - hmm_range_fault() and dma map page all in one.
1037 * @range: range being faulted
1038 * @device: device to map page to
1039 * @daddrs: array of dma addresses for the mapped pages
1040 * @flags: HMM_FAULT_*
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001041 *
Christoph Hellwig9a4903e2019-07-25 17:56:46 -07001042 * Return: the number of pages mapped on success (including zero), or any
1043 * status return from hmm_range_fault() otherwise.
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001044 */
Christoph Hellwig9a4903e2019-07-25 17:56:46 -07001045long hmm_range_dma_map(struct hmm_range *range, struct device *device,
1046 dma_addr_t *daddrs, unsigned int flags)
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001047{
1048 unsigned long i, npages, mapped;
1049 long ret;
1050
Christoph Hellwig9a4903e2019-07-25 17:56:46 -07001051 ret = hmm_range_fault(range, flags);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001052 if (ret <= 0)
1053 return ret ? ret : -EBUSY;
1054
1055 npages = (range->end - range->start) >> PAGE_SHIFT;
1056 for (i = 0, mapped = 0; i < npages; ++i) {
1057 enum dma_data_direction dir = DMA_TO_DEVICE;
1058 struct page *page;
1059
1060 /*
1061 * FIXME need to update DMA API to provide invalid DMA address
1062 * value instead of a function to test dma address value. This
1063 * would remove lot of dumb code duplicated accross many arch.
1064 *
1065 * For now setting it to 0 here is good enough as the pfns[]
1066 * value is what is use to check what is valid and what isn't.
1067 */
1068 daddrs[i] = 0;
1069
Jérôme Glisse391aab12019-05-13 17:20:31 -07001070 page = hmm_device_entry_to_page(range, range->pfns[i]);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001071 if (page == NULL)
1072 continue;
1073
1074 /* Check if range is being invalidated */
1075 if (!range->valid) {
1076 ret = -EBUSY;
1077 goto unmap;
1078 }
1079
1080 /* If it is read and write than map bi-directional. */
1081 if (range->pfns[i] & range->flags[HMM_PFN_WRITE])
1082 dir = DMA_BIDIRECTIONAL;
1083
1084 daddrs[i] = dma_map_page(device, page, 0, PAGE_SIZE, dir);
1085 if (dma_mapping_error(device, daddrs[i])) {
1086 ret = -EFAULT;
1087 goto unmap;
1088 }
1089
1090 mapped++;
1091 }
1092
1093 return mapped;
1094
1095unmap:
1096 for (npages = i, i = 0; (i < npages) && mapped; ++i) {
1097 enum dma_data_direction dir = DMA_TO_DEVICE;
1098 struct page *page;
1099
Jérôme Glisse391aab12019-05-13 17:20:31 -07001100 page = hmm_device_entry_to_page(range, range->pfns[i]);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001101 if (page == NULL)
1102 continue;
1103
1104 if (dma_mapping_error(device, daddrs[i]))
1105 continue;
1106
1107 /* If it is read and write than map bi-directional. */
1108 if (range->pfns[i] & range->flags[HMM_PFN_WRITE])
1109 dir = DMA_BIDIRECTIONAL;
1110
1111 dma_unmap_page(device, daddrs[i], PAGE_SIZE, dir);
1112 mapped--;
1113 }
1114
1115 return ret;
1116}
1117EXPORT_SYMBOL(hmm_range_dma_map);
1118
1119/**
1120 * hmm_range_dma_unmap() - unmap range of that was map with hmm_range_dma_map()
1121 * @range: range being unmapped
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001122 * @device: device against which dma map was done
1123 * @daddrs: dma address of mapped pages
1124 * @dirty: dirty page if it had the write flag set
Ralph Campbell085ea252019-05-06 16:29:39 -07001125 * Return: number of page unmapped on success, -EINVAL otherwise
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001126 *
1127 * Note that caller MUST abide by mmu notifier or use HMM mirror and abide
1128 * to the sync_cpu_device_pagetables() callback so that it is safe here to
1129 * call set_page_dirty(). Caller must also take appropriate locks to avoid
1130 * concurrent mmu notifier or sync_cpu_device_pagetables() to make progress.
1131 */
1132long hmm_range_dma_unmap(struct hmm_range *range,
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001133 struct device *device,
1134 dma_addr_t *daddrs,
1135 bool dirty)
1136{
1137 unsigned long i, npages;
1138 long cpages = 0;
1139
1140 /* Sanity check. */
1141 if (range->end <= range->start)
1142 return -EINVAL;
1143 if (!daddrs)
1144 return -EINVAL;
1145 if (!range->pfns)
1146 return -EINVAL;
1147
1148 npages = (range->end - range->start) >> PAGE_SHIFT;
1149 for (i = 0; i < npages; ++i) {
1150 enum dma_data_direction dir = DMA_TO_DEVICE;
1151 struct page *page;
1152
Jérôme Glisse391aab12019-05-13 17:20:31 -07001153 page = hmm_device_entry_to_page(range, range->pfns[i]);
Jérôme Glisse55c0ece2019-05-13 17:20:28 -07001154 if (page == NULL)
1155 continue;
1156
1157 /* If it is read and write than map bi-directional. */
1158 if (range->pfns[i] & range->flags[HMM_PFN_WRITE]) {
1159 dir = DMA_BIDIRECTIONAL;
1160
1161 /*
1162 * See comments in function description on why it is
1163 * safe here to call set_page_dirty()
1164 */
1165 if (dirty)
1166 set_page_dirty(page);
1167 }
1168
1169 /* Unmap and clear pfns/dma address */
1170 dma_unmap_page(device, daddrs[i], PAGE_SIZE, dir);
1171 range->pfns[i] = range->values[HMM_PFN_NONE];
1172 /* FIXME see comments in hmm_vma_dma_map() */
1173 daddrs[i] = 0;
1174 cpages++;
1175 }
1176
1177 return cpages;
1178}
1179EXPORT_SYMBOL(hmm_range_dma_unmap);