blob: fe1cd87e49acc94641eaf7178dc07e5c4306e408 [file] [log] [blame]
Jérôme Glisse133ff0e2017-09-08 16:11:23 -07001/*
2 * Copyright 2013 Red Hat Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
Jérôme Glissef813f212018-10-30 15:04:06 -070014 * Authors: Jérôme Glisse <jglisse@redhat.com>
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070015 */
16/*
17 * Refer to include/linux/hmm.h for information about heterogeneous memory
18 * management or HMM for short.
19 */
20#include <linux/mm.h>
21#include <linux/hmm.h>
Jérôme Glisse858b54d2017-09-08 16:12:02 -070022#include <linux/init.h>
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070023#include <linux/rmap.h>
24#include <linux/swap.h>
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070025#include <linux/slab.h>
26#include <linux/sched.h>
Jérôme Glisse4ef589d2017-09-08 16:11:58 -070027#include <linux/mmzone.h>
28#include <linux/pagemap.h>
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070029#include <linux/swapops.h>
30#include <linux/hugetlb.h>
Jérôme Glisse4ef589d2017-09-08 16:11:58 -070031#include <linux/memremap.h>
Jérôme Glisse7b2d55d22017-09-08 16:11:46 -070032#include <linux/jump_label.h>
Jérôme Glissec0b12402017-09-08 16:11:27 -070033#include <linux/mmu_notifier.h>
Jérôme Glisse4ef589d2017-09-08 16:11:58 -070034#include <linux/memory_hotplug.h>
35
36#define PA_SECTION_SIZE (1UL << PA_SECTION_SHIFT)
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070037
Jérôme Glisse6b368cd2017-09-08 16:12:32 -070038#if IS_ENABLED(CONFIG_HMM_MIRROR)
Jérôme Glissec0b12402017-09-08 16:11:27 -070039static const struct mmu_notifier_ops hmm_mmu_notifier_ops;
40
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070041/*
42 * struct hmm - HMM per mm struct
43 *
44 * @mm: mm struct this HMM struct is bound to
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070045 * @lock: lock protecting ranges list
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070046 * @ranges: list of range being snapshotted
Jérôme Glissec0b12402017-09-08 16:11:27 -070047 * @mirrors: list of mirrors for this mm
48 * @mmu_notifier: mmu notifier to track updates to CPU page table
49 * @mirrors_sem: read/write semaphore protecting the mirrors list
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070050 */
51struct hmm {
52 struct mm_struct *mm;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070053 spinlock_t lock;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070054 struct list_head ranges;
Jérôme Glissec0b12402017-09-08 16:11:27 -070055 struct list_head mirrors;
56 struct mmu_notifier mmu_notifier;
57 struct rw_semaphore mirrors_sem;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070058};
59
60/*
61 * hmm_register - register HMM against an mm (HMM internal)
62 *
63 * @mm: mm struct to attach to
64 *
65 * This is not intended to be used directly by device drivers. It allocates an
66 * HMM struct if mm does not have one, and initializes it.
67 */
68static struct hmm *hmm_register(struct mm_struct *mm)
69{
Jérôme Glissec0b12402017-09-08 16:11:27 -070070 struct hmm *hmm = READ_ONCE(mm->hmm);
71 bool cleanup = false;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070072
73 /*
74 * The hmm struct can only be freed once the mm_struct goes away,
75 * hence we should always have pre-allocated an new hmm struct
76 * above.
77 */
Jérôme Glissec0b12402017-09-08 16:11:27 -070078 if (hmm)
79 return hmm;
80
81 hmm = kmalloc(sizeof(*hmm), GFP_KERNEL);
82 if (!hmm)
83 return NULL;
84 INIT_LIST_HEAD(&hmm->mirrors);
85 init_rwsem(&hmm->mirrors_sem);
Jérôme Glissec0b12402017-09-08 16:11:27 -070086 hmm->mmu_notifier.ops = NULL;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070087 INIT_LIST_HEAD(&hmm->ranges);
88 spin_lock_init(&hmm->lock);
Jérôme Glissec0b12402017-09-08 16:11:27 -070089 hmm->mm = mm;
90
Jérôme Glissec0b12402017-09-08 16:11:27 -070091 spin_lock(&mm->page_table_lock);
92 if (!mm->hmm)
93 mm->hmm = hmm;
94 else
95 cleanup = true;
96 spin_unlock(&mm->page_table_lock);
97
Ralph Campbell86a2d592018-10-30 15:04:14 -070098 if (cleanup)
99 goto error;
100
101 /*
102 * We should only get here if hold the mmap_sem in write mode ie on
103 * registration of first mirror through hmm_mirror_register()
104 */
105 hmm->mmu_notifier.ops = &hmm_mmu_notifier_ops;
106 if (__mmu_notifier_register(&hmm->mmu_notifier, mm))
107 goto error_mm;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700108
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700109 return mm->hmm;
Ralph Campbell86a2d592018-10-30 15:04:14 -0700110
111error_mm:
112 spin_lock(&mm->page_table_lock);
113 if (mm->hmm == hmm)
114 mm->hmm = NULL;
115 spin_unlock(&mm->page_table_lock);
116error:
117 kfree(hmm);
118 return NULL;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700119}
120
121void hmm_mm_destroy(struct mm_struct *mm)
122{
123 kfree(mm->hmm);
124}
Jérôme Glissec0b12402017-09-08 16:11:27 -0700125
Jérôme Glisseec131b22018-10-30 15:04:28 -0700126static int hmm_invalidate_range(struct hmm *hmm, bool device,
Jérôme Glisse44532d42018-10-30 15:04:24 -0700127 const struct hmm_update *update)
Jérôme Glissec0b12402017-09-08 16:11:27 -0700128{
129 struct hmm_mirror *mirror;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700130 struct hmm_range *range;
131
132 spin_lock(&hmm->lock);
133 list_for_each_entry(range, &hmm->ranges, list) {
134 unsigned long addr, idx, npages;
135
Jérôme Glisse44532d42018-10-30 15:04:24 -0700136 if (update->end < range->start || update->start >= range->end)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700137 continue;
138
139 range->valid = false;
Jérôme Glisse44532d42018-10-30 15:04:24 -0700140 addr = max(update->start, range->start);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700141 idx = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisse44532d42018-10-30 15:04:24 -0700142 npages = (min(range->end, update->end) - addr) >> PAGE_SHIFT;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700143 memset(&range->pfns[idx], 0, sizeof(*range->pfns) * npages);
144 }
145 spin_unlock(&hmm->lock);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700146
Jérôme Glisseec131b22018-10-30 15:04:28 -0700147 if (!device)
148 return 0;
149
Jérôme Glissec0b12402017-09-08 16:11:27 -0700150 down_read(&hmm->mirrors_sem);
Jérôme Glisse44532d42018-10-30 15:04:24 -0700151 list_for_each_entry(mirror, &hmm->mirrors, list) {
152 int ret;
153
154 ret = mirror->ops->sync_cpu_device_pagetables(mirror, update);
155 if (!update->blockable && ret == -EAGAIN) {
156 up_read(&hmm->mirrors_sem);
157 return -EAGAIN;
158 }
159 }
Jérôme Glissec0b12402017-09-08 16:11:27 -0700160 up_read(&hmm->mirrors_sem);
Jérôme Glisse44532d42018-10-30 15:04:24 -0700161
162 return 0;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700163}
164
Ralph Campbelle1401512018-04-10 16:28:19 -0700165static void hmm_release(struct mmu_notifier *mn, struct mm_struct *mm)
166{
167 struct hmm_mirror *mirror;
168 struct hmm *hmm = mm->hmm;
169
170 down_write(&hmm->mirrors_sem);
171 mirror = list_first_entry_or_null(&hmm->mirrors, struct hmm_mirror,
172 list);
173 while (mirror) {
174 list_del_init(&mirror->list);
175 if (mirror->ops->release) {
176 /*
177 * Drop mirrors_sem so callback can wait on any pending
178 * work that might itself trigger mmu_notifier callback
179 * and thus would deadlock with us.
180 */
181 up_write(&hmm->mirrors_sem);
182 mirror->ops->release(mirror);
183 down_write(&hmm->mirrors_sem);
184 }
185 mirror = list_first_entry_or_null(&hmm->mirrors,
186 struct hmm_mirror, list);
187 }
188 up_write(&hmm->mirrors_sem);
189}
190
Michal Hocko93065ac2018-08-21 21:52:33 -0700191static int hmm_invalidate_range_start(struct mmu_notifier *mn,
Jérôme Glisse5d6527a2018-12-28 00:38:05 -0800192 const struct mmu_notifier_range *range)
Jérôme Glissec0b12402017-09-08 16:11:27 -0700193{
Jérôme Glisseec131b22018-10-30 15:04:28 -0700194 struct hmm_update update;
Jérôme Glisse5d6527a2018-12-28 00:38:05 -0800195 struct hmm *hmm = range->mm->hmm;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700196
197 VM_BUG_ON(!hmm);
198
Jérôme Glisse5d6527a2018-12-28 00:38:05 -0800199 update.start = range->start;
200 update.end = range->end;
Jérôme Glisseec131b22018-10-30 15:04:28 -0700201 update.event = HMM_UPDATE_INVALIDATE;
Jérôme Glisse5d6527a2018-12-28 00:38:05 -0800202 update.blockable = range->blockable;
Jérôme Glisseec131b22018-10-30 15:04:28 -0700203 return hmm_invalidate_range(hmm, true, &update);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700204}
205
206static void hmm_invalidate_range_end(struct mmu_notifier *mn,
Jérôme Glisse5d6527a2018-12-28 00:38:05 -0800207 const struct mmu_notifier_range *range)
Jérôme Glissec0b12402017-09-08 16:11:27 -0700208{
Jérôme Glisse44532d42018-10-30 15:04:24 -0700209 struct hmm_update update;
Jérôme Glisse5d6527a2018-12-28 00:38:05 -0800210 struct hmm *hmm = range->mm->hmm;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700211
212 VM_BUG_ON(!hmm);
213
Jérôme Glisse5d6527a2018-12-28 00:38:05 -0800214 update.start = range->start;
215 update.end = range->end;
Jérôme Glisse44532d42018-10-30 15:04:24 -0700216 update.event = HMM_UPDATE_INVALIDATE;
217 update.blockable = true;
Jérôme Glisseec131b22018-10-30 15:04:28 -0700218 hmm_invalidate_range(hmm, false, &update);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700219}
220
221static const struct mmu_notifier_ops hmm_mmu_notifier_ops = {
Ralph Campbelle1401512018-04-10 16:28:19 -0700222 .release = hmm_release,
Jérôme Glissec0b12402017-09-08 16:11:27 -0700223 .invalidate_range_start = hmm_invalidate_range_start,
224 .invalidate_range_end = hmm_invalidate_range_end,
225};
226
227/*
228 * hmm_mirror_register() - register a mirror against an mm
229 *
230 * @mirror: new mirror struct to register
231 * @mm: mm to register against
232 *
233 * To start mirroring a process address space, the device driver must register
234 * an HMM mirror struct.
235 *
236 * THE mm->mmap_sem MUST BE HELD IN WRITE MODE !
237 */
238int hmm_mirror_register(struct hmm_mirror *mirror, struct mm_struct *mm)
239{
240 /* Sanity check */
241 if (!mm || !mirror || !mirror->ops)
242 return -EINVAL;
243
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700244again:
Jérôme Glissec0b12402017-09-08 16:11:27 -0700245 mirror->hmm = hmm_register(mm);
246 if (!mirror->hmm)
247 return -ENOMEM;
248
249 down_write(&mirror->hmm->mirrors_sem);
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700250 if (mirror->hmm->mm == NULL) {
251 /*
252 * A racing hmm_mirror_unregister() is about to destroy the hmm
253 * struct. Try again to allocate a new one.
254 */
255 up_write(&mirror->hmm->mirrors_sem);
256 mirror->hmm = NULL;
257 goto again;
258 } else {
259 list_add(&mirror->list, &mirror->hmm->mirrors);
260 up_write(&mirror->hmm->mirrors_sem);
261 }
Jérôme Glissec0b12402017-09-08 16:11:27 -0700262
263 return 0;
264}
265EXPORT_SYMBOL(hmm_mirror_register);
266
267/*
268 * hmm_mirror_unregister() - unregister a mirror
269 *
270 * @mirror: new mirror struct to register
271 *
272 * Stop mirroring a process address space, and cleanup.
273 */
274void hmm_mirror_unregister(struct hmm_mirror *mirror)
275{
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700276 bool should_unregister = false;
277 struct mm_struct *mm;
278 struct hmm *hmm;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700279
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700280 if (mirror->hmm == NULL)
281 return;
282
283 hmm = mirror->hmm;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700284 down_write(&hmm->mirrors_sem);
Ralph Campbelle1401512018-04-10 16:28:19 -0700285 list_del_init(&mirror->list);
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700286 should_unregister = list_empty(&hmm->mirrors);
287 mirror->hmm = NULL;
288 mm = hmm->mm;
289 hmm->mm = NULL;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700290 up_write(&hmm->mirrors_sem);
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700291
292 if (!should_unregister || mm == NULL)
293 return;
294
Ralph Campbell86a2d592018-10-30 15:04:14 -0700295 mmu_notifier_unregister_no_release(&hmm->mmu_notifier, mm);
296
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700297 spin_lock(&mm->page_table_lock);
298 if (mm->hmm == hmm)
299 mm->hmm = NULL;
300 spin_unlock(&mm->page_table_lock);
301
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700302 kfree(hmm);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700303}
304EXPORT_SYMBOL(hmm_mirror_unregister);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700305
Jérôme Glisse74eee182017-09-08 16:11:35 -0700306struct hmm_vma_walk {
307 struct hmm_range *range;
308 unsigned long last;
309 bool fault;
310 bool block;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700311};
312
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700313static int hmm_vma_do_fault(struct mm_walk *walk, unsigned long addr,
314 bool write_fault, uint64_t *pfn)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700315{
316 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_REMOTE;
317 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700318 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700319 struct vm_area_struct *vma = walk->vma;
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700320 vm_fault_t ret;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700321
322 flags |= hmm_vma_walk->block ? 0 : FAULT_FLAG_ALLOW_RETRY;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700323 flags |= write_fault ? FAULT_FLAG_WRITE : 0;
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700324 ret = handle_mm_fault(vma, addr, flags);
325 if (ret & VM_FAULT_RETRY)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700326 return -EBUSY;
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700327 if (ret & VM_FAULT_ERROR) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700328 *pfn = range->values[HMM_PFN_ERROR];
Jérôme Glisse74eee182017-09-08 16:11:35 -0700329 return -EFAULT;
330 }
331
332 return -EAGAIN;
333}
334
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700335static int hmm_pfns_bad(unsigned long addr,
336 unsigned long end,
337 struct mm_walk *walk)
338{
Jérôme Glissec7195472018-04-10 16:28:27 -0700339 struct hmm_vma_walk *hmm_vma_walk = walk->private;
340 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700341 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700342 unsigned long i;
343
344 i = (addr - range->start) >> PAGE_SHIFT;
345 for (; addr < end; addr += PAGE_SIZE, i++)
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700346 pfns[i] = range->values[HMM_PFN_ERROR];
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700347
348 return 0;
349}
350
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700351/*
352 * hmm_vma_walk_hole() - handle a range lacking valid pmd or pte(s)
353 * @start: range virtual start address (inclusive)
354 * @end: range virtual end address (exclusive)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700355 * @fault: should we fault or not ?
356 * @write_fault: write fault ?
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700357 * @walk: mm_walk structure
358 * Returns: 0 on success, -EAGAIN after page fault, or page fault error
359 *
360 * This function will be called whenever pmd_none() or pte_none() returns true,
361 * or whenever there is no page directory covering the virtual address range.
362 */
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700363static int hmm_vma_walk_hole_(unsigned long addr, unsigned long end,
364 bool fault, bool write_fault,
365 struct mm_walk *walk)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700366{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700367 struct hmm_vma_walk *hmm_vma_walk = walk->private;
368 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700369 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700370 unsigned long i;
371
Jérôme Glisse74eee182017-09-08 16:11:35 -0700372 hmm_vma_walk->last = addr;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700373 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700374 for (; addr < end; addr += PAGE_SIZE, i++) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700375 pfns[i] = range->values[HMM_PFN_NONE];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700376 if (fault || write_fault) {
Jérôme Glisse74eee182017-09-08 16:11:35 -0700377 int ret;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700378
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700379 ret = hmm_vma_do_fault(walk, addr, write_fault,
380 &pfns[i]);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700381 if (ret != -EAGAIN)
382 return ret;
383 }
384 }
385
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700386 return (fault || write_fault) ? -EAGAIN : 0;
387}
388
389static inline void hmm_pte_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
390 uint64_t pfns, uint64_t cpu_flags,
391 bool *fault, bool *write_fault)
392{
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700393 struct hmm_range *range = hmm_vma_walk->range;
394
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700395 *fault = *write_fault = false;
396 if (!hmm_vma_walk->fault)
397 return;
398
399 /* We aren't ask to do anything ... */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700400 if (!(pfns & range->flags[HMM_PFN_VALID]))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700401 return;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700402 /* If this is device memory than only fault if explicitly requested */
403 if ((cpu_flags & range->flags[HMM_PFN_DEVICE_PRIVATE])) {
404 /* Do we fault on device memory ? */
405 if (pfns & range->flags[HMM_PFN_DEVICE_PRIVATE]) {
406 *write_fault = pfns & range->flags[HMM_PFN_WRITE];
407 *fault = true;
408 }
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700409 return;
410 }
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700411
412 /* If CPU page table is not valid then we need to fault */
413 *fault = !(cpu_flags & range->flags[HMM_PFN_VALID]);
414 /* Need to write fault ? */
415 if ((pfns & range->flags[HMM_PFN_WRITE]) &&
416 !(cpu_flags & range->flags[HMM_PFN_WRITE])) {
417 *write_fault = true;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700418 *fault = true;
419 }
420}
421
422static void hmm_range_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
423 const uint64_t *pfns, unsigned long npages,
424 uint64_t cpu_flags, bool *fault,
425 bool *write_fault)
426{
427 unsigned long i;
428
429 if (!hmm_vma_walk->fault) {
430 *fault = *write_fault = false;
431 return;
432 }
433
434 for (i = 0; i < npages; ++i) {
435 hmm_pte_need_fault(hmm_vma_walk, pfns[i], cpu_flags,
436 fault, write_fault);
437 if ((*fault) || (*write_fault))
438 return;
439 }
440}
441
442static int hmm_vma_walk_hole(unsigned long addr, unsigned long end,
443 struct mm_walk *walk)
444{
445 struct hmm_vma_walk *hmm_vma_walk = walk->private;
446 struct hmm_range *range = hmm_vma_walk->range;
447 bool fault, write_fault;
448 unsigned long i, npages;
449 uint64_t *pfns;
450
451 i = (addr - range->start) >> PAGE_SHIFT;
452 npages = (end - addr) >> PAGE_SHIFT;
453 pfns = &range->pfns[i];
454 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
455 0, &fault, &write_fault);
456 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
457}
458
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700459static inline uint64_t pmd_to_hmm_pfn_flags(struct hmm_range *range, pmd_t pmd)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700460{
461 if (pmd_protnone(pmd))
462 return 0;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700463 return pmd_write(pmd) ? range->flags[HMM_PFN_VALID] |
464 range->flags[HMM_PFN_WRITE] :
465 range->flags[HMM_PFN_VALID];
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700466}
467
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700468static int hmm_vma_handle_pmd(struct mm_walk *walk,
469 unsigned long addr,
470 unsigned long end,
471 uint64_t *pfns,
472 pmd_t pmd)
473{
474 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700475 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700476 unsigned long pfn, npages, i;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700477 bool fault, write_fault;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700478 uint64_t cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700479
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700480 npages = (end - addr) >> PAGE_SHIFT;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700481 cpu_flags = pmd_to_hmm_pfn_flags(range, pmd);
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700482 hmm_range_need_fault(hmm_vma_walk, pfns, npages, cpu_flags,
483 &fault, &write_fault);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700484
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700485 if (pmd_protnone(pmd) || fault || write_fault)
486 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700487
488 pfn = pmd_pfn(pmd) + pte_index(addr);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700489 for (i = 0; addr < end; addr += PAGE_SIZE, i++, pfn++)
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700490 pfns[i] = hmm_pfn_from_pfn(range, pfn) | cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700491 hmm_vma_walk->last = end;
492 return 0;
493}
494
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700495static inline uint64_t pte_to_hmm_pfn_flags(struct hmm_range *range, pte_t pte)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700496{
497 if (pte_none(pte) || !pte_present(pte))
498 return 0;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700499 return pte_write(pte) ? range->flags[HMM_PFN_VALID] |
500 range->flags[HMM_PFN_WRITE] :
501 range->flags[HMM_PFN_VALID];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700502}
503
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700504static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,
505 unsigned long end, pmd_t *pmdp, pte_t *ptep,
506 uint64_t *pfn)
507{
508 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700509 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700510 struct vm_area_struct *vma = walk->vma;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700511 bool fault, write_fault;
512 uint64_t cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700513 pte_t pte = *ptep;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700514 uint64_t orig_pfn = *pfn;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700515
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700516 *pfn = range->values[HMM_PFN_NONE];
517 cpu_flags = pte_to_hmm_pfn_flags(range, pte);
518 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700519 &fault, &write_fault);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700520
521 if (pte_none(pte)) {
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700522 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700523 goto fault;
524 return 0;
525 }
526
527 if (!pte_present(pte)) {
528 swp_entry_t entry = pte_to_swp_entry(pte);
529
530 if (!non_swap_entry(entry)) {
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700531 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700532 goto fault;
533 return 0;
534 }
535
536 /*
537 * This is a special swap entry, ignore migration, use
538 * device and report anything else as error.
539 */
540 if (is_device_private_entry(entry)) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700541 cpu_flags = range->flags[HMM_PFN_VALID] |
542 range->flags[HMM_PFN_DEVICE_PRIVATE];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700543 cpu_flags |= is_write_device_private_entry(entry) ?
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700544 range->flags[HMM_PFN_WRITE] : 0;
545 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
546 &fault, &write_fault);
547 if (fault || write_fault)
548 goto fault;
549 *pfn = hmm_pfn_from_pfn(range, swp_offset(entry));
550 *pfn |= cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700551 return 0;
552 }
553
554 if (is_migration_entry(entry)) {
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700555 if (fault || write_fault) {
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700556 pte_unmap(ptep);
557 hmm_vma_walk->last = addr;
558 migration_entry_wait(vma->vm_mm,
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700559 pmdp, addr);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700560 return -EAGAIN;
561 }
562 return 0;
563 }
564
565 /* Report error for everything else */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700566 *pfn = range->values[HMM_PFN_ERROR];
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700567 return -EFAULT;
568 }
569
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700570 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700571 goto fault;
572
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700573 *pfn = hmm_pfn_from_pfn(range, pte_pfn(pte)) | cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700574 return 0;
575
576fault:
577 pte_unmap(ptep);
578 /* Fault any virtual address we were asked to fault */
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700579 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700580}
581
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700582static int hmm_vma_walk_pmd(pmd_t *pmdp,
583 unsigned long start,
584 unsigned long end,
585 struct mm_walk *walk)
586{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700587 struct hmm_vma_walk *hmm_vma_walk = walk->private;
588 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700589 struct vm_area_struct *vma = walk->vma;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700590 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700591 unsigned long addr = start, i;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700592 pte_t *ptep;
Jérôme Glissed08faca2018-10-30 15:04:20 -0700593 pmd_t pmd;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700594
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700595
596again:
Jérôme Glissed08faca2018-10-30 15:04:20 -0700597 pmd = READ_ONCE(*pmdp);
598 if (pmd_none(pmd))
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700599 return hmm_vma_walk_hole(start, end, walk);
600
Jérôme Glissed08faca2018-10-30 15:04:20 -0700601 if (pmd_huge(pmd) && (range->vma->vm_flags & VM_HUGETLB))
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700602 return hmm_pfns_bad(start, end, walk);
603
Jérôme Glissed08faca2018-10-30 15:04:20 -0700604 if (thp_migration_supported() && is_pmd_migration_entry(pmd)) {
605 bool fault, write_fault;
606 unsigned long npages;
607 uint64_t *pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700608
Jérôme Glissed08faca2018-10-30 15:04:20 -0700609 i = (addr - range->start) >> PAGE_SHIFT;
610 npages = (end - addr) >> PAGE_SHIFT;
611 pfns = &range->pfns[i];
612
613 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
614 0, &fault, &write_fault);
615 if (fault || write_fault) {
616 hmm_vma_walk->last = addr;
617 pmd_migration_entry_wait(vma->vm_mm, pmdp);
618 return -EAGAIN;
619 }
620 return 0;
621 } else if (!pmd_present(pmd))
622 return hmm_pfns_bad(start, end, walk);
623
624 if (pmd_devmap(pmd) || pmd_trans_huge(pmd)) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700625 /*
626 * No need to take pmd_lock here, even if some other threads
627 * is splitting the huge pmd we will get that event through
628 * mmu_notifier callback.
629 *
630 * So just read pmd value and check again its a transparent
631 * huge or device mapping one and compute corresponding pfn
632 * values.
633 */
634 pmd = pmd_read_atomic(pmdp);
635 barrier();
636 if (!pmd_devmap(pmd) && !pmd_trans_huge(pmd))
637 goto again;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700638
Jérôme Glissed08faca2018-10-30 15:04:20 -0700639 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700640 return hmm_vma_handle_pmd(walk, addr, end, &pfns[i], pmd);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700641 }
642
Jérôme Glissed08faca2018-10-30 15:04:20 -0700643 /*
644 * We have handled all the valid case above ie either none, migration,
645 * huge or transparent huge. At this point either it is a valid pmd
646 * entry pointing to pte directory or it is a bad pmd that will not
647 * recover.
648 */
649 if (pmd_bad(pmd))
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700650 return hmm_pfns_bad(start, end, walk);
651
652 ptep = pte_offset_map(pmdp, addr);
Jérôme Glissed08faca2018-10-30 15:04:20 -0700653 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700654 for (; addr < end; addr += PAGE_SIZE, ptep++, i++) {
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700655 int r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700656
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700657 r = hmm_vma_handle_pte(walk, addr, end, pmdp, ptep, &pfns[i]);
658 if (r) {
659 /* hmm_vma_handle_pte() did unmap pte directory */
660 hmm_vma_walk->last = addr;
661 return r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700662 }
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700663 }
664 pte_unmap(ptep - 1);
665
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700666 hmm_vma_walk->last = addr;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700667 return 0;
668}
669
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700670static void hmm_pfns_clear(struct hmm_range *range,
671 uint64_t *pfns,
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700672 unsigned long addr,
673 unsigned long end)
674{
675 for (; addr < end; addr += PAGE_SIZE, pfns++)
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700676 *pfns = range->values[HMM_PFN_NONE];
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700677}
678
Jérôme Glisse855ce7d2018-04-10 16:28:42 -0700679static void hmm_pfns_special(struct hmm_range *range)
680{
681 unsigned long addr = range->start, i = 0;
682
683 for (; addr < range->end; addr += PAGE_SIZE, i++)
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700684 range->pfns[i] = range->values[HMM_PFN_SPECIAL];
Jérôme Glisse855ce7d2018-04-10 16:28:42 -0700685}
686
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700687/*
688 * hmm_vma_get_pfns() - snapshot CPU page table for a range of virtual addresses
Jérôme Glisse08232a42018-04-10 16:28:30 -0700689 * @range: range being snapshotted
Jérôme Glisse86586a42018-04-10 16:28:34 -0700690 * Returns: -EINVAL if invalid argument, -ENOMEM out of memory, -EPERM invalid
691 * vma permission, 0 success
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700692 *
693 * This snapshots the CPU page table for a range of virtual addresses. Snapshot
694 * validity is tracked by range struct. See hmm_vma_range_done() for further
695 * information.
696 *
697 * The range struct is initialized here. It tracks the CPU page table, but only
698 * if the function returns success (0), in which case the caller must then call
699 * hmm_vma_range_done() to stop CPU page table update tracking on this range.
700 *
701 * NOT CALLING hmm_vma_range_done() IF FUNCTION RETURNS 0 WILL LEAD TO SERIOUS
702 * MEMORY CORRUPTION ! YOU HAVE BEEN WARNED !
703 */
Jérôme Glisse08232a42018-04-10 16:28:30 -0700704int hmm_vma_get_pfns(struct hmm_range *range)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700705{
Jérôme Glisse08232a42018-04-10 16:28:30 -0700706 struct vm_area_struct *vma = range->vma;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700707 struct hmm_vma_walk hmm_vma_walk;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700708 struct mm_walk mm_walk;
709 struct hmm *hmm;
710
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700711 /* Sanity check, this really should not happen ! */
Jérôme Glisse08232a42018-04-10 16:28:30 -0700712 if (range->start < vma->vm_start || range->start >= vma->vm_end)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700713 return -EINVAL;
Jérôme Glisse08232a42018-04-10 16:28:30 -0700714 if (range->end < vma->vm_start || range->end > vma->vm_end)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700715 return -EINVAL;
716
717 hmm = hmm_register(vma->vm_mm);
718 if (!hmm)
719 return -ENOMEM;
720 /* Caller must have registered a mirror, via hmm_mirror_register() ! */
721 if (!hmm->mmu_notifier.ops)
722 return -EINVAL;
723
Jérôme Glisse855ce7d2018-04-10 16:28:42 -0700724 /* FIXME support hugetlb fs */
Dave Jiange1fb4a02018-08-17 15:43:40 -0700725 if (is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_SPECIAL) ||
726 vma_is_dax(vma)) {
Jérôme Glisse855ce7d2018-04-10 16:28:42 -0700727 hmm_pfns_special(range);
728 return -EINVAL;
729 }
730
Jérôme Glisse86586a42018-04-10 16:28:34 -0700731 if (!(vma->vm_flags & VM_READ)) {
732 /*
733 * If vma do not allow read access, then assume that it does
734 * not allow write access, either. Architecture that allow
735 * write without read access are not supported by HMM, because
736 * operations such has atomic access would not work.
737 */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700738 hmm_pfns_clear(range, range->pfns, range->start, range->end);
Jérôme Glisse86586a42018-04-10 16:28:34 -0700739 return -EPERM;
740 }
741
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700742 /* Initialize range to track CPU page table update */
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700743 spin_lock(&hmm->lock);
744 range->valid = true;
745 list_add_rcu(&range->list, &hmm->ranges);
746 spin_unlock(&hmm->lock);
747
Jérôme Glisse74eee182017-09-08 16:11:35 -0700748 hmm_vma_walk.fault = false;
749 hmm_vma_walk.range = range;
750 mm_walk.private = &hmm_vma_walk;
751
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700752 mm_walk.vma = vma;
753 mm_walk.mm = vma->vm_mm;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700754 mm_walk.pte_entry = NULL;
755 mm_walk.test_walk = NULL;
756 mm_walk.hugetlb_entry = NULL;
757 mm_walk.pmd_entry = hmm_vma_walk_pmd;
758 mm_walk.pte_hole = hmm_vma_walk_hole;
759
Jérôme Glisse08232a42018-04-10 16:28:30 -0700760 walk_page_range(range->start, range->end, &mm_walk);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700761 return 0;
762}
763EXPORT_SYMBOL(hmm_vma_get_pfns);
764
765/*
766 * hmm_vma_range_done() - stop tracking change to CPU page table over a range
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700767 * @range: range being tracked
768 * Returns: false if range data has been invalidated, true otherwise
769 *
770 * Range struct is used to track updates to the CPU page table after a call to
771 * either hmm_vma_get_pfns() or hmm_vma_fault(). Once the device driver is done
772 * using the data, or wants to lock updates to the data it got from those
773 * functions, it must call the hmm_vma_range_done() function, which will then
774 * stop tracking CPU page table updates.
775 *
776 * Note that device driver must still implement general CPU page table update
777 * tracking either by using hmm_mirror (see hmm_mirror_register()) or by using
778 * the mmu_notifier API directly.
779 *
780 * CPU page table update tracking done through hmm_range is only temporary and
781 * to be used while trying to duplicate CPU page table contents for a range of
782 * virtual addresses.
783 *
784 * There are two ways to use this :
785 * again:
Jérôme Glisse08232a42018-04-10 16:28:30 -0700786 * hmm_vma_get_pfns(range); or hmm_vma_fault(...);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700787 * trans = device_build_page_table_update_transaction(pfns);
788 * device_page_table_lock();
Jérôme Glisse08232a42018-04-10 16:28:30 -0700789 * if (!hmm_vma_range_done(range)) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700790 * device_page_table_unlock();
791 * goto again;
792 * }
793 * device_commit_transaction(trans);
794 * device_page_table_unlock();
795 *
796 * Or:
Jérôme Glisse08232a42018-04-10 16:28:30 -0700797 * hmm_vma_get_pfns(range); or hmm_vma_fault(...);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700798 * device_page_table_lock();
Jérôme Glisse08232a42018-04-10 16:28:30 -0700799 * hmm_vma_range_done(range);
800 * device_update_page_table(range->pfns);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700801 * device_page_table_unlock();
802 */
Jérôme Glisse08232a42018-04-10 16:28:30 -0700803bool hmm_vma_range_done(struct hmm_range *range)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700804{
805 unsigned long npages = (range->end - range->start) >> PAGE_SHIFT;
806 struct hmm *hmm;
807
808 if (range->end <= range->start) {
809 BUG();
810 return false;
811 }
812
Jérôme Glisse08232a42018-04-10 16:28:30 -0700813 hmm = hmm_register(range->vma->vm_mm);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700814 if (!hmm) {
815 memset(range->pfns, 0, sizeof(*range->pfns) * npages);
816 return false;
817 }
818
819 spin_lock(&hmm->lock);
820 list_del_rcu(&range->list);
821 spin_unlock(&hmm->lock);
822
823 return range->valid;
824}
825EXPORT_SYMBOL(hmm_vma_range_done);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700826
827/*
828 * hmm_vma_fault() - try to fault some address in a virtual address range
Jérôme Glisse08232a42018-04-10 16:28:30 -0700829 * @range: range being faulted
Jérôme Glisse74eee182017-09-08 16:11:35 -0700830 * @block: allow blocking on fault (if true it sleeps and do not drop mmap_sem)
831 * Returns: 0 success, error otherwise (-EAGAIN means mmap_sem have been drop)
832 *
833 * This is similar to a regular CPU page fault except that it will not trigger
834 * any memory migration if the memory being faulted is not accessible by CPUs.
835 *
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700836 * On error, for one virtual address in the range, the function will mark the
837 * corresponding HMM pfn entry with an error flag.
Jérôme Glisse74eee182017-09-08 16:11:35 -0700838 *
839 * Expected use pattern:
840 * retry:
841 * down_read(&mm->mmap_sem);
842 * // Find vma and address device wants to fault, initialize hmm_pfn_t
843 * // array accordingly
Jérôme Glisse08232a42018-04-10 16:28:30 -0700844 * ret = hmm_vma_fault(range, write, block);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700845 * switch (ret) {
846 * case -EAGAIN:
Jérôme Glisse08232a42018-04-10 16:28:30 -0700847 * hmm_vma_range_done(range);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700848 * // You might want to rate limit or yield to play nicely, you may
849 * // also commit any valid pfn in the array assuming that you are
850 * // getting true from hmm_vma_range_monitor_end()
851 * goto retry;
852 * case 0:
853 * break;
Jérôme Glisse86586a42018-04-10 16:28:34 -0700854 * case -ENOMEM:
855 * case -EINVAL:
856 * case -EPERM:
Jérôme Glisse74eee182017-09-08 16:11:35 -0700857 * default:
858 * // Handle error !
859 * up_read(&mm->mmap_sem)
860 * return;
861 * }
862 * // Take device driver lock that serialize device page table update
863 * driver_lock_device_page_table_update();
Jérôme Glisse08232a42018-04-10 16:28:30 -0700864 * hmm_vma_range_done(range);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700865 * // Commit pfns we got from hmm_vma_fault()
866 * driver_unlock_device_page_table_update();
867 * up_read(&mm->mmap_sem)
868 *
869 * YOU MUST CALL hmm_vma_range_done() AFTER THIS FUNCTION RETURN SUCCESS (0)
870 * BEFORE FREEING THE range struct OR YOU WILL HAVE SERIOUS MEMORY CORRUPTION !
871 *
872 * YOU HAVE BEEN WARNED !
873 */
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700874int hmm_vma_fault(struct hmm_range *range, bool block)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700875{
Jérôme Glisse08232a42018-04-10 16:28:30 -0700876 struct vm_area_struct *vma = range->vma;
877 unsigned long start = range->start;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700878 struct hmm_vma_walk hmm_vma_walk;
879 struct mm_walk mm_walk;
880 struct hmm *hmm;
881 int ret;
882
883 /* Sanity check, this really should not happen ! */
Jérôme Glisse08232a42018-04-10 16:28:30 -0700884 if (range->start < vma->vm_start || range->start >= vma->vm_end)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700885 return -EINVAL;
Jérôme Glisse08232a42018-04-10 16:28:30 -0700886 if (range->end < vma->vm_start || range->end > vma->vm_end)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700887 return -EINVAL;
888
889 hmm = hmm_register(vma->vm_mm);
890 if (!hmm) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700891 hmm_pfns_clear(range, range->pfns, range->start, range->end);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700892 return -ENOMEM;
893 }
894 /* Caller must have registered a mirror using hmm_mirror_register() */
895 if (!hmm->mmu_notifier.ops)
896 return -EINVAL;
897
Jérôme Glisse855ce7d2018-04-10 16:28:42 -0700898 /* FIXME support hugetlb fs */
Dave Jiange1fb4a02018-08-17 15:43:40 -0700899 if (is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_SPECIAL) ||
900 vma_is_dax(vma)) {
Jérôme Glisse855ce7d2018-04-10 16:28:42 -0700901 hmm_pfns_special(range);
902 return -EINVAL;
903 }
904
Jérôme Glisse86586a42018-04-10 16:28:34 -0700905 if (!(vma->vm_flags & VM_READ)) {
906 /*
907 * If vma do not allow read access, then assume that it does
908 * not allow write access, either. Architecture that allow
909 * write without read access are not supported by HMM, because
910 * operations such has atomic access would not work.
911 */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700912 hmm_pfns_clear(range, range->pfns, range->start, range->end);
Jérôme Glisse86586a42018-04-10 16:28:34 -0700913 return -EPERM;
914 }
Jérôme Glisse74eee182017-09-08 16:11:35 -0700915
Jérôme Glisse86586a42018-04-10 16:28:34 -0700916 /* Initialize range to track CPU page table update */
917 spin_lock(&hmm->lock);
918 range->valid = true;
919 list_add_rcu(&range->list, &hmm->ranges);
920 spin_unlock(&hmm->lock);
921
Jérôme Glisse74eee182017-09-08 16:11:35 -0700922 hmm_vma_walk.fault = true;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700923 hmm_vma_walk.block = block;
924 hmm_vma_walk.range = range;
925 mm_walk.private = &hmm_vma_walk;
926 hmm_vma_walk.last = range->start;
927
928 mm_walk.vma = vma;
929 mm_walk.mm = vma->vm_mm;
930 mm_walk.pte_entry = NULL;
931 mm_walk.test_walk = NULL;
932 mm_walk.hugetlb_entry = NULL;
933 mm_walk.pmd_entry = hmm_vma_walk_pmd;
934 mm_walk.pte_hole = hmm_vma_walk_hole;
935
936 do {
Jérôme Glisse08232a42018-04-10 16:28:30 -0700937 ret = walk_page_range(start, range->end, &mm_walk);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700938 start = hmm_vma_walk.last;
939 } while (ret == -EAGAIN);
940
941 if (ret) {
942 unsigned long i;
943
944 i = (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700945 hmm_pfns_clear(range, &range->pfns[i], hmm_vma_walk.last,
946 range->end);
Jérôme Glisse08232a42018-04-10 16:28:30 -0700947 hmm_vma_range_done(range);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700948 }
949 return ret;
950}
951EXPORT_SYMBOL(hmm_vma_fault);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700952#endif /* IS_ENABLED(CONFIG_HMM_MIRROR) */
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700953
954
Jérôme Glissedf6ad692017-09-08 16:12:24 -0700955#if IS_ENABLED(CONFIG_DEVICE_PRIVATE) || IS_ENABLED(CONFIG_DEVICE_PUBLIC)
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700956struct page *hmm_vma_alloc_locked_page(struct vm_area_struct *vma,
957 unsigned long addr)
958{
959 struct page *page;
960
961 page = alloc_page_vma(GFP_HIGHUSER, vma, addr);
962 if (!page)
963 return NULL;
964 lock_page(page);
965 return page;
966}
967EXPORT_SYMBOL(hmm_vma_alloc_locked_page);
968
969
970static void hmm_devmem_ref_release(struct percpu_ref *ref)
971{
972 struct hmm_devmem *devmem;
973
974 devmem = container_of(ref, struct hmm_devmem, ref);
975 complete(&devmem->completion);
976}
977
978static void hmm_devmem_ref_exit(void *data)
979{
980 struct percpu_ref *ref = data;
981 struct hmm_devmem *devmem;
982
983 devmem = container_of(ref, struct hmm_devmem, ref);
Dan Williamsbbecd94e2018-12-28 00:35:11 -0800984 wait_for_completion(&devmem->completion);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700985 percpu_ref_exit(ref);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700986}
987
Dan Williamsbbecd94e2018-12-28 00:35:11 -0800988static void hmm_devmem_ref_kill(struct percpu_ref *ref)
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700989{
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700990 percpu_ref_kill(ref);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700991}
992
Souptick Joarderb57e622e62019-03-11 23:28:10 -0700993static vm_fault_t hmm_devmem_fault(struct vm_area_struct *vma,
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700994 unsigned long addr,
995 const struct page *page,
996 unsigned int flags,
997 pmd_t *pmdp)
998{
999 struct hmm_devmem *devmem = page->pgmap->data;
1000
1001 return devmem->ops->fault(devmem, vma, addr, page, flags, pmdp);
1002}
1003
1004static void hmm_devmem_free(struct page *page, void *data)
1005{
1006 struct hmm_devmem *devmem = data;
1007
Dan Williams2fa147b2018-07-13 21:50:01 -07001008 page->mapping = NULL;
1009
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001010 devmem->ops->free(devmem, page);
1011}
1012
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001013/*
1014 * hmm_devmem_add() - hotplug ZONE_DEVICE memory for device memory
1015 *
1016 * @ops: memory event device driver callback (see struct hmm_devmem_ops)
1017 * @device: device struct to bind the resource too
1018 * @size: size in bytes of the device memory to add
1019 * Returns: pointer to new hmm_devmem struct ERR_PTR otherwise
1020 *
1021 * This function first finds an empty range of physical address big enough to
1022 * contain the new resource, and then hotplugs it as ZONE_DEVICE memory, which
1023 * in turn allocates struct pages. It does not do anything beyond that; all
1024 * events affecting the memory will go through the various callbacks provided
1025 * by hmm_devmem_ops struct.
1026 *
1027 * Device driver should call this function during device initialization and
1028 * is then responsible of memory management. HMM only provides helpers.
1029 */
1030struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops,
1031 struct device *device,
1032 unsigned long size)
1033{
1034 struct hmm_devmem *devmem;
1035 resource_size_t addr;
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001036 void *result;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001037 int ret;
1038
Dan Williamse76384882018-05-16 11:46:08 -07001039 dev_pagemap_get_ops();
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001040
Dan Williams58ef15b2018-12-28 00:35:07 -08001041 devmem = devm_kzalloc(device, sizeof(*devmem), GFP_KERNEL);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001042 if (!devmem)
1043 return ERR_PTR(-ENOMEM);
1044
1045 init_completion(&devmem->completion);
1046 devmem->pfn_first = -1UL;
1047 devmem->pfn_last = -1UL;
1048 devmem->resource = NULL;
1049 devmem->device = device;
1050 devmem->ops = ops;
1051
1052 ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release,
1053 0, GFP_KERNEL);
1054 if (ret)
Dan Williams58ef15b2018-12-28 00:35:07 -08001055 return ERR_PTR(ret);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001056
Dan Williams58ef15b2018-12-28 00:35:07 -08001057 ret = devm_add_action_or_reset(device, hmm_devmem_ref_exit, &devmem->ref);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001058 if (ret)
Dan Williams58ef15b2018-12-28 00:35:07 -08001059 return ERR_PTR(ret);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001060
1061 size = ALIGN(size, PA_SECTION_SIZE);
1062 addr = min((unsigned long)iomem_resource.end,
1063 (1UL << MAX_PHYSMEM_BITS) - 1);
1064 addr = addr - size + 1UL;
1065
1066 /*
1067 * FIXME add a new helper to quickly walk resource tree and find free
1068 * range
1069 *
1070 * FIXME what about ioport_resource resource ?
1071 */
1072 for (; addr > size && addr >= iomem_resource.start; addr -= size) {
1073 ret = region_intersects(addr, size, 0, IORES_DESC_NONE);
1074 if (ret != REGION_DISJOINT)
1075 continue;
1076
1077 devmem->resource = devm_request_mem_region(device, addr, size,
1078 dev_name(device));
Dan Williams58ef15b2018-12-28 00:35:07 -08001079 if (!devmem->resource)
1080 return ERR_PTR(-ENOMEM);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001081 break;
1082 }
Dan Williams58ef15b2018-12-28 00:35:07 -08001083 if (!devmem->resource)
1084 return ERR_PTR(-ERANGE);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001085
1086 devmem->resource->desc = IORES_DESC_DEVICE_PRIVATE_MEMORY;
1087 devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
1088 devmem->pfn_last = devmem->pfn_first +
1089 (resource_size(devmem->resource) >> PAGE_SHIFT);
Dan Williams063a7d12018-12-28 00:39:46 -08001090 devmem->page_fault = hmm_devmem_fault;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001091
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001092 devmem->pagemap.type = MEMORY_DEVICE_PRIVATE;
1093 devmem->pagemap.res = *devmem->resource;
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001094 devmem->pagemap.page_free = hmm_devmem_free;
1095 devmem->pagemap.altmap_valid = false;
1096 devmem->pagemap.ref = &devmem->ref;
1097 devmem->pagemap.data = devmem;
1098 devmem->pagemap.kill = hmm_devmem_ref_kill;
Dan Williams58ef15b2018-12-28 00:35:07 -08001099
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001100 result = devm_memremap_pages(devmem->device, &devmem->pagemap);
1101 if (IS_ERR(result))
1102 return result;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001103 return devmem;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001104}
Dan Williams02917e92018-12-28 00:35:15 -08001105EXPORT_SYMBOL_GPL(hmm_devmem_add);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001106
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001107struct hmm_devmem *hmm_devmem_add_resource(const struct hmm_devmem_ops *ops,
1108 struct device *device,
1109 struct resource *res)
1110{
1111 struct hmm_devmem *devmem;
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001112 void *result;
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001113 int ret;
1114
1115 if (res->desc != IORES_DESC_DEVICE_PUBLIC_MEMORY)
1116 return ERR_PTR(-EINVAL);
1117
Dan Williamse76384882018-05-16 11:46:08 -07001118 dev_pagemap_get_ops();
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001119
Dan Williams58ef15b2018-12-28 00:35:07 -08001120 devmem = devm_kzalloc(device, sizeof(*devmem), GFP_KERNEL);
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001121 if (!devmem)
1122 return ERR_PTR(-ENOMEM);
1123
1124 init_completion(&devmem->completion);
1125 devmem->pfn_first = -1UL;
1126 devmem->pfn_last = -1UL;
1127 devmem->resource = res;
1128 devmem->device = device;
1129 devmem->ops = ops;
1130
1131 ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release,
1132 0, GFP_KERNEL);
1133 if (ret)
Dan Williams58ef15b2018-12-28 00:35:07 -08001134 return ERR_PTR(ret);
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001135
Dan Williams58ef15b2018-12-28 00:35:07 -08001136 ret = devm_add_action_or_reset(device, hmm_devmem_ref_exit,
1137 &devmem->ref);
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001138 if (ret)
Dan Williams58ef15b2018-12-28 00:35:07 -08001139 return ERR_PTR(ret);
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001140
1141 devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
1142 devmem->pfn_last = devmem->pfn_first +
1143 (resource_size(devmem->resource) >> PAGE_SHIFT);
Dan Williams063a7d12018-12-28 00:39:46 -08001144 devmem->page_fault = hmm_devmem_fault;
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001145
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001146 devmem->pagemap.type = MEMORY_DEVICE_PUBLIC;
1147 devmem->pagemap.res = *devmem->resource;
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001148 devmem->pagemap.page_free = hmm_devmem_free;
1149 devmem->pagemap.altmap_valid = false;
1150 devmem->pagemap.ref = &devmem->ref;
1151 devmem->pagemap.data = devmem;
1152 devmem->pagemap.kill = hmm_devmem_ref_kill;
Dan Williams58ef15b2018-12-28 00:35:07 -08001153
Dan Williamsbbecd94e2018-12-28 00:35:11 -08001154 result = devm_memremap_pages(devmem->device, &devmem->pagemap);
1155 if (IS_ERR(result))
1156 return result;
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001157 return devmem;
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001158}
Dan Williams02917e92018-12-28 00:35:15 -08001159EXPORT_SYMBOL_GPL(hmm_devmem_add_resource);
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001160
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001161/*
Jérôme Glisse858b54d2017-09-08 16:12:02 -07001162 * A device driver that wants to handle multiple devices memory through a
1163 * single fake device can use hmm_device to do so. This is purely a helper
1164 * and it is not needed to make use of any HMM functionality.
1165 */
1166#define HMM_DEVICE_MAX 256
1167
1168static DECLARE_BITMAP(hmm_device_mask, HMM_DEVICE_MAX);
1169static DEFINE_SPINLOCK(hmm_device_lock);
1170static struct class *hmm_device_class;
1171static dev_t hmm_device_devt;
1172
1173static void hmm_device_release(struct device *device)
1174{
1175 struct hmm_device *hmm_device;
1176
1177 hmm_device = container_of(device, struct hmm_device, device);
1178 spin_lock(&hmm_device_lock);
1179 clear_bit(hmm_device->minor, hmm_device_mask);
1180 spin_unlock(&hmm_device_lock);
1181
1182 kfree(hmm_device);
1183}
1184
1185struct hmm_device *hmm_device_new(void *drvdata)
1186{
1187 struct hmm_device *hmm_device;
1188
1189 hmm_device = kzalloc(sizeof(*hmm_device), GFP_KERNEL);
1190 if (!hmm_device)
1191 return ERR_PTR(-ENOMEM);
1192
1193 spin_lock(&hmm_device_lock);
1194 hmm_device->minor = find_first_zero_bit(hmm_device_mask, HMM_DEVICE_MAX);
1195 if (hmm_device->minor >= HMM_DEVICE_MAX) {
1196 spin_unlock(&hmm_device_lock);
1197 kfree(hmm_device);
1198 return ERR_PTR(-EBUSY);
1199 }
1200 set_bit(hmm_device->minor, hmm_device_mask);
1201 spin_unlock(&hmm_device_lock);
1202
1203 dev_set_name(&hmm_device->device, "hmm_device%d", hmm_device->minor);
1204 hmm_device->device.devt = MKDEV(MAJOR(hmm_device_devt),
1205 hmm_device->minor);
1206 hmm_device->device.release = hmm_device_release;
1207 dev_set_drvdata(&hmm_device->device, drvdata);
1208 hmm_device->device.class = hmm_device_class;
1209 device_initialize(&hmm_device->device);
1210
1211 return hmm_device;
1212}
1213EXPORT_SYMBOL(hmm_device_new);
1214
1215void hmm_device_put(struct hmm_device *hmm_device)
1216{
1217 put_device(&hmm_device->device);
1218}
1219EXPORT_SYMBOL(hmm_device_put);
1220
1221static int __init hmm_init(void)
1222{
1223 int ret;
1224
1225 ret = alloc_chrdev_region(&hmm_device_devt, 0,
1226 HMM_DEVICE_MAX,
1227 "hmm_device");
1228 if (ret)
1229 return ret;
1230
1231 hmm_device_class = class_create(THIS_MODULE, "hmm_device");
1232 if (IS_ERR(hmm_device_class)) {
1233 unregister_chrdev_region(hmm_device_devt, HMM_DEVICE_MAX);
1234 return PTR_ERR(hmm_device_class);
1235 }
1236 return 0;
1237}
1238
1239device_initcall(hmm_init);
Jérôme Glissedf6ad692017-09-08 16:12:24 -07001240#endif /* CONFIG_DEVICE_PRIVATE || CONFIG_DEVICE_PUBLIC */