blob: b31d56662202c7c2dbda0d934e7a9cb0edcf30d3 [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 *
14 * Authors: Jérôme Glisse <jglisse@redhat.com>
15 */
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
38
Jérôme Glisse7b2d55d22017-09-08 16:11:46 -070039/*
40 * Device private memory see HMM (Documentation/vm/hmm.txt) or hmm.h
41 */
42DEFINE_STATIC_KEY_FALSE(device_private_key);
43EXPORT_SYMBOL(device_private_key);
44
45
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070046#ifdef CONFIG_HMM
Jérôme Glissec0b12402017-09-08 16:11:27 -070047static const struct mmu_notifier_ops hmm_mmu_notifier_ops;
48
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070049/*
50 * struct hmm - HMM per mm struct
51 *
52 * @mm: mm struct this HMM struct is bound to
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070053 * @lock: lock protecting ranges list
Jérôme Glissec0b12402017-09-08 16:11:27 -070054 * @sequence: we track updates to the CPU page table with a sequence number
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070055 * @ranges: list of range being snapshotted
Jérôme Glissec0b12402017-09-08 16:11:27 -070056 * @mirrors: list of mirrors for this mm
57 * @mmu_notifier: mmu notifier to track updates to CPU page table
58 * @mirrors_sem: read/write semaphore protecting the mirrors list
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070059 */
60struct hmm {
61 struct mm_struct *mm;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070062 spinlock_t lock;
Jérôme Glissec0b12402017-09-08 16:11:27 -070063 atomic_t sequence;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070064 struct list_head ranges;
Jérôme Glissec0b12402017-09-08 16:11:27 -070065 struct list_head mirrors;
66 struct mmu_notifier mmu_notifier;
67 struct rw_semaphore mirrors_sem;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070068};
69
70/*
71 * hmm_register - register HMM against an mm (HMM internal)
72 *
73 * @mm: mm struct to attach to
74 *
75 * This is not intended to be used directly by device drivers. It allocates an
76 * HMM struct if mm does not have one, and initializes it.
77 */
78static struct hmm *hmm_register(struct mm_struct *mm)
79{
Jérôme Glissec0b12402017-09-08 16:11:27 -070080 struct hmm *hmm = READ_ONCE(mm->hmm);
81 bool cleanup = false;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070082
83 /*
84 * The hmm struct can only be freed once the mm_struct goes away,
85 * hence we should always have pre-allocated an new hmm struct
86 * above.
87 */
Jérôme Glissec0b12402017-09-08 16:11:27 -070088 if (hmm)
89 return hmm;
90
91 hmm = kmalloc(sizeof(*hmm), GFP_KERNEL);
92 if (!hmm)
93 return NULL;
94 INIT_LIST_HEAD(&hmm->mirrors);
95 init_rwsem(&hmm->mirrors_sem);
96 atomic_set(&hmm->sequence, 0);
97 hmm->mmu_notifier.ops = NULL;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070098 INIT_LIST_HEAD(&hmm->ranges);
99 spin_lock_init(&hmm->lock);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700100 hmm->mm = mm;
101
102 /*
103 * We should only get here if hold the mmap_sem in write mode ie on
104 * registration of first mirror through hmm_mirror_register()
105 */
106 hmm->mmu_notifier.ops = &hmm_mmu_notifier_ops;
107 if (__mmu_notifier_register(&hmm->mmu_notifier, mm)) {
108 kfree(hmm);
109 return NULL;
110 }
111
112 spin_lock(&mm->page_table_lock);
113 if (!mm->hmm)
114 mm->hmm = hmm;
115 else
116 cleanup = true;
117 spin_unlock(&mm->page_table_lock);
118
119 if (cleanup) {
120 mmu_notifier_unregister(&hmm->mmu_notifier, mm);
121 kfree(hmm);
122 }
123
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700124 return mm->hmm;
125}
126
127void hmm_mm_destroy(struct mm_struct *mm)
128{
129 kfree(mm->hmm);
130}
131#endif /* CONFIG_HMM */
Jérôme Glissec0b12402017-09-08 16:11:27 -0700132
133#if IS_ENABLED(CONFIG_HMM_MIRROR)
134static void hmm_invalidate_range(struct hmm *hmm,
135 enum hmm_update_type action,
136 unsigned long start,
137 unsigned long end)
138{
139 struct hmm_mirror *mirror;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700140 struct hmm_range *range;
141
142 spin_lock(&hmm->lock);
143 list_for_each_entry(range, &hmm->ranges, list) {
144 unsigned long addr, idx, npages;
145
146 if (end < range->start || start >= range->end)
147 continue;
148
149 range->valid = false;
150 addr = max(start, range->start);
151 idx = (addr - range->start) >> PAGE_SHIFT;
152 npages = (min(range->end, end) - addr) >> PAGE_SHIFT;
153 memset(&range->pfns[idx], 0, sizeof(*range->pfns) * npages);
154 }
155 spin_unlock(&hmm->lock);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700156
157 down_read(&hmm->mirrors_sem);
158 list_for_each_entry(mirror, &hmm->mirrors, list)
159 mirror->ops->sync_cpu_device_pagetables(mirror, action,
160 start, end);
161 up_read(&hmm->mirrors_sem);
162}
163
164static void hmm_invalidate_range_start(struct mmu_notifier *mn,
165 struct mm_struct *mm,
166 unsigned long start,
167 unsigned long end)
168{
169 struct hmm *hmm = mm->hmm;
170
171 VM_BUG_ON(!hmm);
172
173 atomic_inc(&hmm->sequence);
174}
175
176static void hmm_invalidate_range_end(struct mmu_notifier *mn,
177 struct mm_struct *mm,
178 unsigned long start,
179 unsigned long end)
180{
181 struct hmm *hmm = mm->hmm;
182
183 VM_BUG_ON(!hmm);
184
185 hmm_invalidate_range(mm->hmm, HMM_UPDATE_INVALIDATE, start, end);
186}
187
188static const struct mmu_notifier_ops hmm_mmu_notifier_ops = {
189 .invalidate_range_start = hmm_invalidate_range_start,
190 .invalidate_range_end = hmm_invalidate_range_end,
191};
192
193/*
194 * hmm_mirror_register() - register a mirror against an mm
195 *
196 * @mirror: new mirror struct to register
197 * @mm: mm to register against
198 *
199 * To start mirroring a process address space, the device driver must register
200 * an HMM mirror struct.
201 *
202 * THE mm->mmap_sem MUST BE HELD IN WRITE MODE !
203 */
204int hmm_mirror_register(struct hmm_mirror *mirror, struct mm_struct *mm)
205{
206 /* Sanity check */
207 if (!mm || !mirror || !mirror->ops)
208 return -EINVAL;
209
210 mirror->hmm = hmm_register(mm);
211 if (!mirror->hmm)
212 return -ENOMEM;
213
214 down_write(&mirror->hmm->mirrors_sem);
215 list_add(&mirror->list, &mirror->hmm->mirrors);
216 up_write(&mirror->hmm->mirrors_sem);
217
218 return 0;
219}
220EXPORT_SYMBOL(hmm_mirror_register);
221
222/*
223 * hmm_mirror_unregister() - unregister a mirror
224 *
225 * @mirror: new mirror struct to register
226 *
227 * Stop mirroring a process address space, and cleanup.
228 */
229void hmm_mirror_unregister(struct hmm_mirror *mirror)
230{
231 struct hmm *hmm = mirror->hmm;
232
233 down_write(&hmm->mirrors_sem);
234 list_del(&mirror->list);
235 up_write(&hmm->mirrors_sem);
236}
237EXPORT_SYMBOL(hmm_mirror_unregister);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700238
Jérôme Glisse74eee182017-09-08 16:11:35 -0700239struct hmm_vma_walk {
240 struct hmm_range *range;
241 unsigned long last;
242 bool fault;
243 bool block;
244 bool write;
245};
246
247static int hmm_vma_do_fault(struct mm_walk *walk,
248 unsigned long addr,
249 hmm_pfn_t *pfn)
250{
251 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_REMOTE;
252 struct hmm_vma_walk *hmm_vma_walk = walk->private;
253 struct vm_area_struct *vma = walk->vma;
254 int r;
255
256 flags |= hmm_vma_walk->block ? 0 : FAULT_FLAG_ALLOW_RETRY;
257 flags |= hmm_vma_walk->write ? FAULT_FLAG_WRITE : 0;
258 r = handle_mm_fault(vma, addr, flags);
259 if (r & VM_FAULT_RETRY)
260 return -EBUSY;
261 if (r & VM_FAULT_ERROR) {
262 *pfn = HMM_PFN_ERROR;
263 return -EFAULT;
264 }
265
266 return -EAGAIN;
267}
268
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700269static void hmm_pfns_special(hmm_pfn_t *pfns,
270 unsigned long addr,
271 unsigned long end)
272{
273 for (; addr < end; addr += PAGE_SIZE, pfns++)
274 *pfns = HMM_PFN_SPECIAL;
275}
276
277static int hmm_pfns_bad(unsigned long addr,
278 unsigned long end,
279 struct mm_walk *walk)
280{
281 struct hmm_range *range = walk->private;
282 hmm_pfn_t *pfns = range->pfns;
283 unsigned long i;
284
285 i = (addr - range->start) >> PAGE_SHIFT;
286 for (; addr < end; addr += PAGE_SIZE, i++)
287 pfns[i] = HMM_PFN_ERROR;
288
289 return 0;
290}
291
Jérôme Glisse74eee182017-09-08 16:11:35 -0700292static void hmm_pfns_clear(hmm_pfn_t *pfns,
293 unsigned long addr,
294 unsigned long end)
295{
296 for (; addr < end; addr += PAGE_SIZE, pfns++)
297 *pfns = 0;
298}
299
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700300static int hmm_vma_walk_hole(unsigned long addr,
301 unsigned long end,
302 struct mm_walk *walk)
303{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700304 struct hmm_vma_walk *hmm_vma_walk = walk->private;
305 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700306 hmm_pfn_t *pfns = range->pfns;
307 unsigned long i;
308
Jérôme Glisse74eee182017-09-08 16:11:35 -0700309 hmm_vma_walk->last = addr;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700310 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700311 for (; addr < end; addr += PAGE_SIZE, i++) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700312 pfns[i] = HMM_PFN_EMPTY;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700313 if (hmm_vma_walk->fault) {
314 int ret;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700315
Jérôme Glisse74eee182017-09-08 16:11:35 -0700316 ret = hmm_vma_do_fault(walk, addr, &pfns[i]);
317 if (ret != -EAGAIN)
318 return ret;
319 }
320 }
321
322 return hmm_vma_walk->fault ? -EAGAIN : 0;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700323}
324
325static int hmm_vma_walk_clear(unsigned long addr,
326 unsigned long end,
327 struct mm_walk *walk)
328{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700329 struct hmm_vma_walk *hmm_vma_walk = walk->private;
330 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700331 hmm_pfn_t *pfns = range->pfns;
332 unsigned long i;
333
Jérôme Glisse74eee182017-09-08 16:11:35 -0700334 hmm_vma_walk->last = addr;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700335 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700336 for (; addr < end; addr += PAGE_SIZE, i++) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700337 pfns[i] = 0;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700338 if (hmm_vma_walk->fault) {
339 int ret;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700340
Jérôme Glisse74eee182017-09-08 16:11:35 -0700341 ret = hmm_vma_do_fault(walk, addr, &pfns[i]);
342 if (ret != -EAGAIN)
343 return ret;
344 }
345 }
346
347 return hmm_vma_walk->fault ? -EAGAIN : 0;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700348}
349
350static int hmm_vma_walk_pmd(pmd_t *pmdp,
351 unsigned long start,
352 unsigned long end,
353 struct mm_walk *walk)
354{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700355 struct hmm_vma_walk *hmm_vma_walk = walk->private;
356 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700357 struct vm_area_struct *vma = walk->vma;
358 hmm_pfn_t *pfns = range->pfns;
359 unsigned long addr = start, i;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700360 bool write_fault;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700361 hmm_pfn_t flag;
362 pte_t *ptep;
363
364 i = (addr - range->start) >> PAGE_SHIFT;
365 flag = vma->vm_flags & VM_READ ? HMM_PFN_READ : 0;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700366 write_fault = hmm_vma_walk->fault & hmm_vma_walk->write;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700367
368again:
369 if (pmd_none(*pmdp))
370 return hmm_vma_walk_hole(start, end, walk);
371
372 if (pmd_huge(*pmdp) && vma->vm_flags & VM_HUGETLB)
373 return hmm_pfns_bad(start, end, walk);
374
375 if (pmd_devmap(*pmdp) || pmd_trans_huge(*pmdp)) {
376 unsigned long pfn;
377 pmd_t pmd;
378
379 /*
380 * No need to take pmd_lock here, even if some other threads
381 * is splitting the huge pmd we will get that event through
382 * mmu_notifier callback.
383 *
384 * So just read pmd value and check again its a transparent
385 * huge or device mapping one and compute corresponding pfn
386 * values.
387 */
388 pmd = pmd_read_atomic(pmdp);
389 barrier();
390 if (!pmd_devmap(pmd) && !pmd_trans_huge(pmd))
391 goto again;
392 if (pmd_protnone(pmd))
393 return hmm_vma_walk_clear(start, end, walk);
394
Jérôme Glisse74eee182017-09-08 16:11:35 -0700395 if (write_fault && !pmd_write(pmd))
396 return hmm_vma_walk_clear(start, end, walk);
397
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700398 pfn = pmd_pfn(pmd) + pte_index(addr);
399 flag |= pmd_write(pmd) ? HMM_PFN_WRITE : 0;
400 for (; addr < end; addr += PAGE_SIZE, i++, pfn++)
401 pfns[i] = hmm_pfn_t_from_pfn(pfn) | flag;
402 return 0;
403 }
404
405 if (pmd_bad(*pmdp))
406 return hmm_pfns_bad(start, end, walk);
407
408 ptep = pte_offset_map(pmdp, addr);
409 for (; addr < end; addr += PAGE_SIZE, ptep++, i++) {
410 pte_t pte = *ptep;
411
412 pfns[i] = 0;
413
Jérôme Glisse74eee182017-09-08 16:11:35 -0700414 if (pte_none(pte)) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700415 pfns[i] = HMM_PFN_EMPTY;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700416 if (hmm_vma_walk->fault)
417 goto fault;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700418 continue;
419 }
420
Jérôme Glisse74eee182017-09-08 16:11:35 -0700421 if (!pte_present(pte)) {
422 swp_entry_t entry;
423
424 if (!non_swap_entry(entry)) {
425 if (hmm_vma_walk->fault)
426 goto fault;
427 continue;
428 }
429
430 entry = pte_to_swp_entry(pte);
431
432 /*
433 * This is a special swap entry, ignore migration, use
434 * device and report anything else as error.
435 */
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700436 if (is_device_private_entry(entry)) {
437 pfns[i] = hmm_pfn_t_from_pfn(swp_offset(entry));
438 if (is_write_device_private_entry(entry)) {
439 pfns[i] |= HMM_PFN_WRITE;
440 } else if (write_fault)
441 goto fault;
442 pfns[i] |= HMM_PFN_DEVICE_UNADDRESSABLE;
443 pfns[i] |= flag;
444 } else if (is_migration_entry(entry)) {
Jérôme Glisse74eee182017-09-08 16:11:35 -0700445 if (hmm_vma_walk->fault) {
446 pte_unmap(ptep);
447 hmm_vma_walk->last = addr;
448 migration_entry_wait(vma->vm_mm,
449 pmdp, addr);
450 return -EAGAIN;
451 }
452 continue;
453 } else {
454 /* Report error for everything else */
455 pfns[i] = HMM_PFN_ERROR;
456 }
457 continue;
458 }
459
460 if (write_fault && !pte_write(pte))
461 goto fault;
462
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700463 pfns[i] = hmm_pfn_t_from_pfn(pte_pfn(pte)) | flag;
464 pfns[i] |= pte_write(pte) ? HMM_PFN_WRITE : 0;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700465 continue;
466
467fault:
468 pte_unmap(ptep);
469 /* Fault all pages in range */
470 return hmm_vma_walk_clear(start, end, walk);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700471 }
472 pte_unmap(ptep - 1);
473
474 return 0;
475}
476
477/*
478 * hmm_vma_get_pfns() - snapshot CPU page table for a range of virtual addresses
479 * @vma: virtual memory area containing the virtual address range
480 * @range: used to track snapshot validity
481 * @start: range virtual start address (inclusive)
482 * @end: range virtual end address (exclusive)
483 * @entries: array of hmm_pfn_t: provided by the caller, filled in by function
484 * Returns: -EINVAL if invalid argument, -ENOMEM out of memory, 0 success
485 *
486 * This snapshots the CPU page table for a range of virtual addresses. Snapshot
487 * validity is tracked by range struct. See hmm_vma_range_done() for further
488 * information.
489 *
490 * The range struct is initialized here. It tracks the CPU page table, but only
491 * if the function returns success (0), in which case the caller must then call
492 * hmm_vma_range_done() to stop CPU page table update tracking on this range.
493 *
494 * NOT CALLING hmm_vma_range_done() IF FUNCTION RETURNS 0 WILL LEAD TO SERIOUS
495 * MEMORY CORRUPTION ! YOU HAVE BEEN WARNED !
496 */
497int hmm_vma_get_pfns(struct vm_area_struct *vma,
498 struct hmm_range *range,
499 unsigned long start,
500 unsigned long end,
501 hmm_pfn_t *pfns)
502{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700503 struct hmm_vma_walk hmm_vma_walk;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700504 struct mm_walk mm_walk;
505 struct hmm *hmm;
506
507 /* FIXME support hugetlb fs */
508 if (is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_SPECIAL)) {
509 hmm_pfns_special(pfns, start, end);
510 return -EINVAL;
511 }
512
513 /* Sanity check, this really should not happen ! */
514 if (start < vma->vm_start || start >= vma->vm_end)
515 return -EINVAL;
516 if (end < vma->vm_start || end > vma->vm_end)
517 return -EINVAL;
518
519 hmm = hmm_register(vma->vm_mm);
520 if (!hmm)
521 return -ENOMEM;
522 /* Caller must have registered a mirror, via hmm_mirror_register() ! */
523 if (!hmm->mmu_notifier.ops)
524 return -EINVAL;
525
526 /* Initialize range to track CPU page table update */
527 range->start = start;
528 range->pfns = pfns;
529 range->end = end;
530 spin_lock(&hmm->lock);
531 range->valid = true;
532 list_add_rcu(&range->list, &hmm->ranges);
533 spin_unlock(&hmm->lock);
534
Jérôme Glisse74eee182017-09-08 16:11:35 -0700535 hmm_vma_walk.fault = false;
536 hmm_vma_walk.range = range;
537 mm_walk.private = &hmm_vma_walk;
538
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700539 mm_walk.vma = vma;
540 mm_walk.mm = vma->vm_mm;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700541 mm_walk.pte_entry = NULL;
542 mm_walk.test_walk = NULL;
543 mm_walk.hugetlb_entry = NULL;
544 mm_walk.pmd_entry = hmm_vma_walk_pmd;
545 mm_walk.pte_hole = hmm_vma_walk_hole;
546
547 walk_page_range(start, end, &mm_walk);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700548 return 0;
549}
550EXPORT_SYMBOL(hmm_vma_get_pfns);
551
552/*
553 * hmm_vma_range_done() - stop tracking change to CPU page table over a range
554 * @vma: virtual memory area containing the virtual address range
555 * @range: range being tracked
556 * Returns: false if range data has been invalidated, true otherwise
557 *
558 * Range struct is used to track updates to the CPU page table after a call to
559 * either hmm_vma_get_pfns() or hmm_vma_fault(). Once the device driver is done
560 * using the data, or wants to lock updates to the data it got from those
561 * functions, it must call the hmm_vma_range_done() function, which will then
562 * stop tracking CPU page table updates.
563 *
564 * Note that device driver must still implement general CPU page table update
565 * tracking either by using hmm_mirror (see hmm_mirror_register()) or by using
566 * the mmu_notifier API directly.
567 *
568 * CPU page table update tracking done through hmm_range is only temporary and
569 * to be used while trying to duplicate CPU page table contents for a range of
570 * virtual addresses.
571 *
572 * There are two ways to use this :
573 * again:
Jérôme Glisse74eee182017-09-08 16:11:35 -0700574 * hmm_vma_get_pfns(vma, range, start, end, pfns); or hmm_vma_fault(...);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700575 * trans = device_build_page_table_update_transaction(pfns);
576 * device_page_table_lock();
577 * if (!hmm_vma_range_done(vma, range)) {
578 * device_page_table_unlock();
579 * goto again;
580 * }
581 * device_commit_transaction(trans);
582 * device_page_table_unlock();
583 *
584 * Or:
Jérôme Glisse74eee182017-09-08 16:11:35 -0700585 * hmm_vma_get_pfns(vma, range, start, end, pfns); or hmm_vma_fault(...);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700586 * device_page_table_lock();
587 * hmm_vma_range_done(vma, range);
588 * device_update_page_table(pfns);
589 * device_page_table_unlock();
590 */
591bool hmm_vma_range_done(struct vm_area_struct *vma, struct hmm_range *range)
592{
593 unsigned long npages = (range->end - range->start) >> PAGE_SHIFT;
594 struct hmm *hmm;
595
596 if (range->end <= range->start) {
597 BUG();
598 return false;
599 }
600
601 hmm = hmm_register(vma->vm_mm);
602 if (!hmm) {
603 memset(range->pfns, 0, sizeof(*range->pfns) * npages);
604 return false;
605 }
606
607 spin_lock(&hmm->lock);
608 list_del_rcu(&range->list);
609 spin_unlock(&hmm->lock);
610
611 return range->valid;
612}
613EXPORT_SYMBOL(hmm_vma_range_done);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700614
615/*
616 * hmm_vma_fault() - try to fault some address in a virtual address range
617 * @vma: virtual memory area containing the virtual address range
618 * @range: use to track pfns array content validity
619 * @start: fault range virtual start address (inclusive)
620 * @end: fault range virtual end address (exclusive)
621 * @pfns: array of hmm_pfn_t, only entry with fault flag set will be faulted
622 * @write: is it a write fault
623 * @block: allow blocking on fault (if true it sleeps and do not drop mmap_sem)
624 * Returns: 0 success, error otherwise (-EAGAIN means mmap_sem have been drop)
625 *
626 * This is similar to a regular CPU page fault except that it will not trigger
627 * any memory migration if the memory being faulted is not accessible by CPUs.
628 *
629 * On error, for one virtual address in the range, the function will set the
630 * hmm_pfn_t error flag for the corresponding pfn entry.
631 *
632 * Expected use pattern:
633 * retry:
634 * down_read(&mm->mmap_sem);
635 * // Find vma and address device wants to fault, initialize hmm_pfn_t
636 * // array accordingly
637 * ret = hmm_vma_fault(vma, start, end, pfns, allow_retry);
638 * switch (ret) {
639 * case -EAGAIN:
640 * hmm_vma_range_done(vma, range);
641 * // You might want to rate limit or yield to play nicely, you may
642 * // also commit any valid pfn in the array assuming that you are
643 * // getting true from hmm_vma_range_monitor_end()
644 * goto retry;
645 * case 0:
646 * break;
647 * default:
648 * // Handle error !
649 * up_read(&mm->mmap_sem)
650 * return;
651 * }
652 * // Take device driver lock that serialize device page table update
653 * driver_lock_device_page_table_update();
654 * hmm_vma_range_done(vma, range);
655 * // Commit pfns we got from hmm_vma_fault()
656 * driver_unlock_device_page_table_update();
657 * up_read(&mm->mmap_sem)
658 *
659 * YOU MUST CALL hmm_vma_range_done() AFTER THIS FUNCTION RETURN SUCCESS (0)
660 * BEFORE FREEING THE range struct OR YOU WILL HAVE SERIOUS MEMORY CORRUPTION !
661 *
662 * YOU HAVE BEEN WARNED !
663 */
664int hmm_vma_fault(struct vm_area_struct *vma,
665 struct hmm_range *range,
666 unsigned long start,
667 unsigned long end,
668 hmm_pfn_t *pfns,
669 bool write,
670 bool block)
671{
672 struct hmm_vma_walk hmm_vma_walk;
673 struct mm_walk mm_walk;
674 struct hmm *hmm;
675 int ret;
676
677 /* Sanity check, this really should not happen ! */
678 if (start < vma->vm_start || start >= vma->vm_end)
679 return -EINVAL;
680 if (end < vma->vm_start || end > vma->vm_end)
681 return -EINVAL;
682
683 hmm = hmm_register(vma->vm_mm);
684 if (!hmm) {
685 hmm_pfns_clear(pfns, start, end);
686 return -ENOMEM;
687 }
688 /* Caller must have registered a mirror using hmm_mirror_register() */
689 if (!hmm->mmu_notifier.ops)
690 return -EINVAL;
691
692 /* Initialize range to track CPU page table update */
693 range->start = start;
694 range->pfns = pfns;
695 range->end = end;
696 spin_lock(&hmm->lock);
697 range->valid = true;
698 list_add_rcu(&range->list, &hmm->ranges);
699 spin_unlock(&hmm->lock);
700
701 /* FIXME support hugetlb fs */
702 if (is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_SPECIAL)) {
703 hmm_pfns_special(pfns, start, end);
704 return 0;
705 }
706
707 hmm_vma_walk.fault = true;
708 hmm_vma_walk.write = write;
709 hmm_vma_walk.block = block;
710 hmm_vma_walk.range = range;
711 mm_walk.private = &hmm_vma_walk;
712 hmm_vma_walk.last = range->start;
713
714 mm_walk.vma = vma;
715 mm_walk.mm = vma->vm_mm;
716 mm_walk.pte_entry = NULL;
717 mm_walk.test_walk = NULL;
718 mm_walk.hugetlb_entry = NULL;
719 mm_walk.pmd_entry = hmm_vma_walk_pmd;
720 mm_walk.pte_hole = hmm_vma_walk_hole;
721
722 do {
723 ret = walk_page_range(start, end, &mm_walk);
724 start = hmm_vma_walk.last;
725 } while (ret == -EAGAIN);
726
727 if (ret) {
728 unsigned long i;
729
730 i = (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
731 hmm_pfns_clear(&pfns[i], hmm_vma_walk.last, end);
732 hmm_vma_range_done(vma, range);
733 }
734 return ret;
735}
736EXPORT_SYMBOL(hmm_vma_fault);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700737#endif /* IS_ENABLED(CONFIG_HMM_MIRROR) */
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700738
739
Jérôme Glissedf6ad692017-09-08 16:12:24 -0700740#if IS_ENABLED(CONFIG_DEVICE_PRIVATE) || IS_ENABLED(CONFIG_DEVICE_PUBLIC)
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700741struct page *hmm_vma_alloc_locked_page(struct vm_area_struct *vma,
742 unsigned long addr)
743{
744 struct page *page;
745
746 page = alloc_page_vma(GFP_HIGHUSER, vma, addr);
747 if (!page)
748 return NULL;
749 lock_page(page);
750 return page;
751}
752EXPORT_SYMBOL(hmm_vma_alloc_locked_page);
753
754
755static void hmm_devmem_ref_release(struct percpu_ref *ref)
756{
757 struct hmm_devmem *devmem;
758
759 devmem = container_of(ref, struct hmm_devmem, ref);
760 complete(&devmem->completion);
761}
762
763static void hmm_devmem_ref_exit(void *data)
764{
765 struct percpu_ref *ref = data;
766 struct hmm_devmem *devmem;
767
768 devmem = container_of(ref, struct hmm_devmem, ref);
769 percpu_ref_exit(ref);
770 devm_remove_action(devmem->device, &hmm_devmem_ref_exit, data);
771}
772
773static void hmm_devmem_ref_kill(void *data)
774{
775 struct percpu_ref *ref = data;
776 struct hmm_devmem *devmem;
777
778 devmem = container_of(ref, struct hmm_devmem, ref);
779 percpu_ref_kill(ref);
780 wait_for_completion(&devmem->completion);
781 devm_remove_action(devmem->device, &hmm_devmem_ref_kill, data);
782}
783
784static int hmm_devmem_fault(struct vm_area_struct *vma,
785 unsigned long addr,
786 const struct page *page,
787 unsigned int flags,
788 pmd_t *pmdp)
789{
790 struct hmm_devmem *devmem = page->pgmap->data;
791
792 return devmem->ops->fault(devmem, vma, addr, page, flags, pmdp);
793}
794
795static void hmm_devmem_free(struct page *page, void *data)
796{
797 struct hmm_devmem *devmem = data;
798
799 devmem->ops->free(devmem, page);
800}
801
802static DEFINE_MUTEX(hmm_devmem_lock);
803static RADIX_TREE(hmm_devmem_radix, GFP_KERNEL);
804
805static void hmm_devmem_radix_release(struct resource *resource)
806{
807 resource_size_t key, align_start, align_size, align_end;
808
809 align_start = resource->start & ~(PA_SECTION_SIZE - 1);
810 align_size = ALIGN(resource_size(resource), PA_SECTION_SIZE);
811 align_end = align_start + align_size - 1;
812
813 mutex_lock(&hmm_devmem_lock);
814 for (key = resource->start;
815 key <= resource->end;
816 key += PA_SECTION_SIZE)
817 radix_tree_delete(&hmm_devmem_radix, key >> PA_SECTION_SHIFT);
818 mutex_unlock(&hmm_devmem_lock);
819}
820
821static void hmm_devmem_release(struct device *dev, void *data)
822{
823 struct hmm_devmem *devmem = data;
824 struct resource *resource = devmem->resource;
825 unsigned long start_pfn, npages;
826 struct zone *zone;
827 struct page *page;
828
829 if (percpu_ref_tryget_live(&devmem->ref)) {
830 dev_WARN(dev, "%s: page mapping is still live!\n", __func__);
831 percpu_ref_put(&devmem->ref);
832 }
833
834 /* pages are dead and unused, undo the arch mapping */
835 start_pfn = (resource->start & ~(PA_SECTION_SIZE - 1)) >> PAGE_SHIFT;
836 npages = ALIGN(resource_size(resource), PA_SECTION_SIZE) >> PAGE_SHIFT;
837
838 page = pfn_to_page(start_pfn);
839 zone = page_zone(page);
840
841 mem_hotplug_begin();
842 __remove_pages(zone, start_pfn, npages);
843 mem_hotplug_done();
844
845 hmm_devmem_radix_release(resource);
846}
847
848static struct hmm_devmem *hmm_devmem_find(resource_size_t phys)
849{
850 WARN_ON_ONCE(!rcu_read_lock_held());
851
852 return radix_tree_lookup(&hmm_devmem_radix, phys >> PA_SECTION_SHIFT);
853}
854
855static int hmm_devmem_pages_create(struct hmm_devmem *devmem)
856{
857 resource_size_t key, align_start, align_size, align_end;
858 struct device *device = devmem->device;
859 int ret, nid, is_ram;
860 unsigned long pfn;
861
862 align_start = devmem->resource->start & ~(PA_SECTION_SIZE - 1);
863 align_size = ALIGN(devmem->resource->start +
864 resource_size(devmem->resource),
865 PA_SECTION_SIZE) - align_start;
866
867 is_ram = region_intersects(align_start, align_size,
868 IORESOURCE_SYSTEM_RAM,
869 IORES_DESC_NONE);
870 if (is_ram == REGION_MIXED) {
871 WARN_ONCE(1, "%s attempted on mixed region %pr\n",
872 __func__, devmem->resource);
873 return -ENXIO;
874 }
875 if (is_ram == REGION_INTERSECTS)
876 return -ENXIO;
877
878 devmem->pagemap.type = MEMORY_DEVICE_PRIVATE;
879 devmem->pagemap.res = devmem->resource;
880 devmem->pagemap.page_fault = hmm_devmem_fault;
881 devmem->pagemap.page_free = hmm_devmem_free;
882 devmem->pagemap.dev = devmem->device;
883 devmem->pagemap.ref = &devmem->ref;
884 devmem->pagemap.data = devmem;
885
886 mutex_lock(&hmm_devmem_lock);
887 align_end = align_start + align_size - 1;
888 for (key = align_start; key <= align_end; key += PA_SECTION_SIZE) {
889 struct hmm_devmem *dup;
890
891 rcu_read_lock();
892 dup = hmm_devmem_find(key);
893 rcu_read_unlock();
894 if (dup) {
895 dev_err(device, "%s: collides with mapping for %s\n",
896 __func__, dev_name(dup->device));
897 mutex_unlock(&hmm_devmem_lock);
898 ret = -EBUSY;
899 goto error;
900 }
901 ret = radix_tree_insert(&hmm_devmem_radix,
902 key >> PA_SECTION_SHIFT,
903 devmem);
904 if (ret) {
905 dev_err(device, "%s: failed: %d\n", __func__, ret);
906 mutex_unlock(&hmm_devmem_lock);
907 goto error_radix;
908 }
909 }
910 mutex_unlock(&hmm_devmem_lock);
911
912 nid = dev_to_node(device);
913 if (nid < 0)
914 nid = numa_mem_id();
915
916 mem_hotplug_begin();
917 /*
918 * For device private memory we call add_pages() as we only need to
919 * allocate and initialize struct page for the device memory. More-
920 * over the device memory is un-accessible thus we do not want to
921 * create a linear mapping for the memory like arch_add_memory()
922 * would do.
923 */
924 ret = add_pages(nid, align_start >> PAGE_SHIFT,
925 align_size >> PAGE_SHIFT, false);
926 if (ret) {
927 mem_hotplug_done();
928 goto error_add_memory;
929 }
930 move_pfn_range_to_zone(&NODE_DATA(nid)->node_zones[ZONE_DEVICE],
931 align_start >> PAGE_SHIFT,
932 align_size >> PAGE_SHIFT);
933 mem_hotplug_done();
934
935 for (pfn = devmem->pfn_first; pfn < devmem->pfn_last; pfn++) {
936 struct page *page = pfn_to_page(pfn);
937
938 page->pgmap = &devmem->pagemap;
939 }
940 return 0;
941
942error_add_memory:
943 untrack_pfn(NULL, PHYS_PFN(align_start), align_size);
944error_radix:
945 hmm_devmem_radix_release(devmem->resource);
946error:
947 return ret;
948}
949
950static int hmm_devmem_match(struct device *dev, void *data, void *match_data)
951{
952 struct hmm_devmem *devmem = data;
953
954 return devmem->resource == match_data;
955}
956
957static void hmm_devmem_pages_remove(struct hmm_devmem *devmem)
958{
959 devres_release(devmem->device, &hmm_devmem_release,
960 &hmm_devmem_match, devmem->resource);
961}
962
963/*
964 * hmm_devmem_add() - hotplug ZONE_DEVICE memory for device memory
965 *
966 * @ops: memory event device driver callback (see struct hmm_devmem_ops)
967 * @device: device struct to bind the resource too
968 * @size: size in bytes of the device memory to add
969 * Returns: pointer to new hmm_devmem struct ERR_PTR otherwise
970 *
971 * This function first finds an empty range of physical address big enough to
972 * contain the new resource, and then hotplugs it as ZONE_DEVICE memory, which
973 * in turn allocates struct pages. It does not do anything beyond that; all
974 * events affecting the memory will go through the various callbacks provided
975 * by hmm_devmem_ops struct.
976 *
977 * Device driver should call this function during device initialization and
978 * is then responsible of memory management. HMM only provides helpers.
979 */
980struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops,
981 struct device *device,
982 unsigned long size)
983{
984 struct hmm_devmem *devmem;
985 resource_size_t addr;
986 int ret;
987
988 static_branch_enable(&device_private_key);
989
990 devmem = devres_alloc_node(&hmm_devmem_release, sizeof(*devmem),
991 GFP_KERNEL, dev_to_node(device));
992 if (!devmem)
993 return ERR_PTR(-ENOMEM);
994
995 init_completion(&devmem->completion);
996 devmem->pfn_first = -1UL;
997 devmem->pfn_last = -1UL;
998 devmem->resource = NULL;
999 devmem->device = device;
1000 devmem->ops = ops;
1001
1002 ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release,
1003 0, GFP_KERNEL);
1004 if (ret)
1005 goto error_percpu_ref;
1006
1007 ret = devm_add_action(device, hmm_devmem_ref_exit, &devmem->ref);
1008 if (ret)
1009 goto error_devm_add_action;
1010
1011 size = ALIGN(size, PA_SECTION_SIZE);
1012 addr = min((unsigned long)iomem_resource.end,
1013 (1UL << MAX_PHYSMEM_BITS) - 1);
1014 addr = addr - size + 1UL;
1015
1016 /*
1017 * FIXME add a new helper to quickly walk resource tree and find free
1018 * range
1019 *
1020 * FIXME what about ioport_resource resource ?
1021 */
1022 for (; addr > size && addr >= iomem_resource.start; addr -= size) {
1023 ret = region_intersects(addr, size, 0, IORES_DESC_NONE);
1024 if (ret != REGION_DISJOINT)
1025 continue;
1026
1027 devmem->resource = devm_request_mem_region(device, addr, size,
1028 dev_name(device));
1029 if (!devmem->resource) {
1030 ret = -ENOMEM;
1031 goto error_no_resource;
1032 }
1033 break;
1034 }
1035 if (!devmem->resource) {
1036 ret = -ERANGE;
1037 goto error_no_resource;
1038 }
1039
1040 devmem->resource->desc = IORES_DESC_DEVICE_PRIVATE_MEMORY;
1041 devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
1042 devmem->pfn_last = devmem->pfn_first +
1043 (resource_size(devmem->resource) >> PAGE_SHIFT);
1044
1045 ret = hmm_devmem_pages_create(devmem);
1046 if (ret)
1047 goto error_pages;
1048
1049 devres_add(device, devmem);
1050
1051 ret = devm_add_action(device, hmm_devmem_ref_kill, &devmem->ref);
1052 if (ret) {
1053 hmm_devmem_remove(devmem);
1054 return ERR_PTR(ret);
1055 }
1056
1057 return devmem;
1058
1059error_pages:
1060 devm_release_mem_region(device, devmem->resource->start,
1061 resource_size(devmem->resource));
1062error_no_resource:
1063error_devm_add_action:
1064 hmm_devmem_ref_kill(&devmem->ref);
1065 hmm_devmem_ref_exit(&devmem->ref);
1066error_percpu_ref:
1067 devres_free(devmem);
1068 return ERR_PTR(ret);
1069}
1070EXPORT_SYMBOL(hmm_devmem_add);
1071
1072/*
1073 * hmm_devmem_remove() - remove device memory (kill and free ZONE_DEVICE)
1074 *
1075 * @devmem: hmm_devmem struct use to track and manage the ZONE_DEVICE memory
1076 *
1077 * This will hot-unplug memory that was hotplugged by hmm_devmem_add on behalf
1078 * of the device driver. It will free struct page and remove the resource that
1079 * reserved the physical address range for this device memory.
1080 */
1081void hmm_devmem_remove(struct hmm_devmem *devmem)
1082{
1083 resource_size_t start, size;
1084 struct device *device;
1085
1086 if (!devmem)
1087 return;
1088
1089 device = devmem->device;
1090 start = devmem->resource->start;
1091 size = resource_size(devmem->resource);
1092
1093 hmm_devmem_ref_kill(&devmem->ref);
1094 hmm_devmem_ref_exit(&devmem->ref);
1095 hmm_devmem_pages_remove(devmem);
1096
1097 devm_release_mem_region(device, start, size);
1098}
1099EXPORT_SYMBOL(hmm_devmem_remove);
Jérôme Glisse858b54d2017-09-08 16:12:02 -07001100
1101/*
1102 * A device driver that wants to handle multiple devices memory through a
1103 * single fake device can use hmm_device to do so. This is purely a helper
1104 * and it is not needed to make use of any HMM functionality.
1105 */
1106#define HMM_DEVICE_MAX 256
1107
1108static DECLARE_BITMAP(hmm_device_mask, HMM_DEVICE_MAX);
1109static DEFINE_SPINLOCK(hmm_device_lock);
1110static struct class *hmm_device_class;
1111static dev_t hmm_device_devt;
1112
1113static void hmm_device_release(struct device *device)
1114{
1115 struct hmm_device *hmm_device;
1116
1117 hmm_device = container_of(device, struct hmm_device, device);
1118 spin_lock(&hmm_device_lock);
1119 clear_bit(hmm_device->minor, hmm_device_mask);
1120 spin_unlock(&hmm_device_lock);
1121
1122 kfree(hmm_device);
1123}
1124
1125struct hmm_device *hmm_device_new(void *drvdata)
1126{
1127 struct hmm_device *hmm_device;
1128
1129 hmm_device = kzalloc(sizeof(*hmm_device), GFP_KERNEL);
1130 if (!hmm_device)
1131 return ERR_PTR(-ENOMEM);
1132
1133 spin_lock(&hmm_device_lock);
1134 hmm_device->minor = find_first_zero_bit(hmm_device_mask, HMM_DEVICE_MAX);
1135 if (hmm_device->minor >= HMM_DEVICE_MAX) {
1136 spin_unlock(&hmm_device_lock);
1137 kfree(hmm_device);
1138 return ERR_PTR(-EBUSY);
1139 }
1140 set_bit(hmm_device->minor, hmm_device_mask);
1141 spin_unlock(&hmm_device_lock);
1142
1143 dev_set_name(&hmm_device->device, "hmm_device%d", hmm_device->minor);
1144 hmm_device->device.devt = MKDEV(MAJOR(hmm_device_devt),
1145 hmm_device->minor);
1146 hmm_device->device.release = hmm_device_release;
1147 dev_set_drvdata(&hmm_device->device, drvdata);
1148 hmm_device->device.class = hmm_device_class;
1149 device_initialize(&hmm_device->device);
1150
1151 return hmm_device;
1152}
1153EXPORT_SYMBOL(hmm_device_new);
1154
1155void hmm_device_put(struct hmm_device *hmm_device)
1156{
1157 put_device(&hmm_device->device);
1158}
1159EXPORT_SYMBOL(hmm_device_put);
1160
1161static int __init hmm_init(void)
1162{
1163 int ret;
1164
1165 ret = alloc_chrdev_region(&hmm_device_devt, 0,
1166 HMM_DEVICE_MAX,
1167 "hmm_device");
1168 if (ret)
1169 return ret;
1170
1171 hmm_device_class = class_create(THIS_MODULE, "hmm_device");
1172 if (IS_ERR(hmm_device_class)) {
1173 unregister_chrdev_region(hmm_device_devt, HMM_DEVICE_MAX);
1174 return PTR_ERR(hmm_device_class);
1175 }
1176 return 0;
1177}
1178
1179device_initcall(hmm_init);
Jérôme Glissedf6ad692017-09-08 16:12:24 -07001180#endif /* CONFIG_DEVICE_PRIVATE || CONFIG_DEVICE_PUBLIC */