Linux 4.14.212
[linux/fpc-iii.git] / mm / hmm.c
bloba5def9f343857275f117cc2437013873a8034f3e
1 /*
2 * Copyright 2013 Red Hat Inc.
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.
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.
14 * Authors: Jérôme Glisse <jglisse@redhat.com>
17 * Refer to include/linux/hmm.h for information about heterogeneous memory
18 * management or HMM for short.
20 #include <linux/mm.h>
21 #include <linux/hmm.h>
22 #include <linux/init.h>
23 #include <linux/rmap.h>
24 #include <linux/swap.h>
25 #include <linux/slab.h>
26 #include <linux/sched.h>
27 #include <linux/mmzone.h>
28 #include <linux/pagemap.h>
29 #include <linux/swapops.h>
30 #include <linux/hugetlb.h>
31 #include <linux/memremap.h>
32 #include <linux/jump_label.h>
33 #include <linux/mmu_notifier.h>
34 #include <linux/memory_hotplug.h>
36 #define PA_SECTION_SIZE (1UL << PA_SECTION_SHIFT)
38 #if defined(CONFIG_DEVICE_PRIVATE) || defined(CONFIG_DEVICE_PUBLIC)
40 * Device private memory see HMM (Documentation/vm/hmm.txt) or hmm.h
42 DEFINE_STATIC_KEY_FALSE(device_private_key);
43 EXPORT_SYMBOL(device_private_key);
44 #endif /* CONFIG_DEVICE_PRIVATE || CONFIG_DEVICE_PUBLIC */
47 #if IS_ENABLED(CONFIG_HMM_MIRROR)
48 static const struct mmu_notifier_ops hmm_mmu_notifier_ops;
51 * struct hmm - HMM per mm struct
53 * @mm: mm struct this HMM struct is bound to
54 * @lock: lock protecting ranges list
55 * @sequence: we track updates to the CPU page table with a sequence number
56 * @ranges: list of range being snapshotted
57 * @mirrors: list of mirrors for this mm
58 * @mmu_notifier: mmu notifier to track updates to CPU page table
59 * @mirrors_sem: read/write semaphore protecting the mirrors list
61 struct hmm {
62 struct mm_struct *mm;
63 spinlock_t lock;
64 atomic_t sequence;
65 struct list_head ranges;
66 struct list_head mirrors;
67 struct mmu_notifier mmu_notifier;
68 struct rw_semaphore mirrors_sem;
72 * hmm_register - register HMM against an mm (HMM internal)
74 * @mm: mm struct to attach to
76 * This is not intended to be used directly by device drivers. It allocates an
77 * HMM struct if mm does not have one, and initializes it.
79 static struct hmm *hmm_register(struct mm_struct *mm)
81 struct hmm *hmm = READ_ONCE(mm->hmm);
82 bool cleanup = false;
85 * The hmm struct can only be freed once the mm_struct goes away,
86 * hence we should always have pre-allocated an new hmm struct
87 * above.
89 if (hmm)
90 return hmm;
92 hmm = kmalloc(sizeof(*hmm), GFP_KERNEL);
93 if (!hmm)
94 return NULL;
95 INIT_LIST_HEAD(&hmm->mirrors);
96 init_rwsem(&hmm->mirrors_sem);
97 atomic_set(&hmm->sequence, 0);
98 hmm->mmu_notifier.ops = NULL;
99 INIT_LIST_HEAD(&hmm->ranges);
100 spin_lock_init(&hmm->lock);
101 hmm->mm = mm;
104 * We should only get here if hold the mmap_sem in write mode ie on
105 * registration of first mirror through hmm_mirror_register()
107 hmm->mmu_notifier.ops = &hmm_mmu_notifier_ops;
108 if (__mmu_notifier_register(&hmm->mmu_notifier, mm)) {
109 kfree(hmm);
110 return NULL;
113 spin_lock(&mm->page_table_lock);
114 if (!mm->hmm)
115 mm->hmm = hmm;
116 else
117 cleanup = true;
118 spin_unlock(&mm->page_table_lock);
120 if (cleanup) {
121 mmu_notifier_unregister(&hmm->mmu_notifier, mm);
122 kfree(hmm);
125 return mm->hmm;
128 void hmm_mm_destroy(struct mm_struct *mm)
130 kfree(mm->hmm);
133 static void hmm_invalidate_range(struct hmm *hmm,
134 enum hmm_update_type action,
135 unsigned long start,
136 unsigned long end)
138 struct hmm_mirror *mirror;
139 struct hmm_range *range;
141 spin_lock(&hmm->lock);
142 list_for_each_entry(range, &hmm->ranges, list) {
143 unsigned long addr, idx, npages;
145 if (end < range->start || start >= range->end)
146 continue;
148 range->valid = false;
149 addr = max(start, range->start);
150 idx = (addr - range->start) >> PAGE_SHIFT;
151 npages = (min(range->end, end) - addr) >> PAGE_SHIFT;
152 memset(&range->pfns[idx], 0, sizeof(*range->pfns) * npages);
154 spin_unlock(&hmm->lock);
156 down_read(&hmm->mirrors_sem);
157 list_for_each_entry(mirror, &hmm->mirrors, list)
158 mirror->ops->sync_cpu_device_pagetables(mirror, action,
159 start, end);
160 up_read(&hmm->mirrors_sem);
163 static void hmm_invalidate_range_start(struct mmu_notifier *mn,
164 struct mm_struct *mm,
165 unsigned long start,
166 unsigned long end)
168 struct hmm *hmm = mm->hmm;
170 VM_BUG_ON(!hmm);
172 atomic_inc(&hmm->sequence);
175 static void hmm_invalidate_range_end(struct mmu_notifier *mn,
176 struct mm_struct *mm,
177 unsigned long start,
178 unsigned long end)
180 struct hmm *hmm = mm->hmm;
182 VM_BUG_ON(!hmm);
184 hmm_invalidate_range(mm->hmm, HMM_UPDATE_INVALIDATE, start, end);
187 static const struct mmu_notifier_ops hmm_mmu_notifier_ops = {
188 .invalidate_range_start = hmm_invalidate_range_start,
189 .invalidate_range_end = hmm_invalidate_range_end,
193 * hmm_mirror_register() - register a mirror against an mm
195 * @mirror: new mirror struct to register
196 * @mm: mm to register against
198 * To start mirroring a process address space, the device driver must register
199 * an HMM mirror struct.
201 * THE mm->mmap_sem MUST BE HELD IN WRITE MODE !
203 int hmm_mirror_register(struct hmm_mirror *mirror, struct mm_struct *mm)
205 /* Sanity check */
206 if (!mm || !mirror || !mirror->ops)
207 return -EINVAL;
209 mirror->hmm = hmm_register(mm);
210 if (!mirror->hmm)
211 return -ENOMEM;
213 down_write(&mirror->hmm->mirrors_sem);
214 list_add(&mirror->list, &mirror->hmm->mirrors);
215 up_write(&mirror->hmm->mirrors_sem);
217 return 0;
219 EXPORT_SYMBOL(hmm_mirror_register);
222 * hmm_mirror_unregister() - unregister a mirror
224 * @mirror: new mirror struct to register
226 * Stop mirroring a process address space, and cleanup.
228 void hmm_mirror_unregister(struct hmm_mirror *mirror)
230 struct hmm *hmm = mirror->hmm;
232 down_write(&hmm->mirrors_sem);
233 list_del(&mirror->list);
234 up_write(&hmm->mirrors_sem);
236 EXPORT_SYMBOL(hmm_mirror_unregister);
238 struct hmm_vma_walk {
239 struct hmm_range *range;
240 unsigned long last;
241 bool fault;
242 bool block;
243 bool write;
246 static int hmm_vma_do_fault(struct mm_walk *walk,
247 unsigned long addr,
248 hmm_pfn_t *pfn)
250 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_REMOTE;
251 struct hmm_vma_walk *hmm_vma_walk = walk->private;
252 struct vm_area_struct *vma = walk->vma;
253 int r;
255 flags |= hmm_vma_walk->block ? 0 : FAULT_FLAG_ALLOW_RETRY;
256 flags |= hmm_vma_walk->write ? FAULT_FLAG_WRITE : 0;
257 r = handle_mm_fault(vma, addr, flags);
258 if (r & VM_FAULT_RETRY)
259 return -EBUSY;
260 if (r & VM_FAULT_ERROR) {
261 *pfn = HMM_PFN_ERROR;
262 return -EFAULT;
265 return -EAGAIN;
268 static void hmm_pfns_special(hmm_pfn_t *pfns,
269 unsigned long addr,
270 unsigned long end)
272 for (; addr < end; addr += PAGE_SIZE, pfns++)
273 *pfns = HMM_PFN_SPECIAL;
276 static int hmm_pfns_bad(unsigned long addr,
277 unsigned long end,
278 struct mm_walk *walk)
280 struct hmm_vma_walk *hmm_vma_walk = walk->private;
281 struct hmm_range *range = hmm_vma_walk->range;
282 hmm_pfn_t *pfns = range->pfns;
283 unsigned long i;
285 i = (addr - range->start) >> PAGE_SHIFT;
286 for (; addr < end; addr += PAGE_SIZE, i++)
287 pfns[i] = HMM_PFN_ERROR;
289 return 0;
292 static void hmm_pfns_clear(hmm_pfn_t *pfns,
293 unsigned long addr,
294 unsigned long end)
296 for (; addr < end; addr += PAGE_SIZE, pfns++)
297 *pfns = 0;
300 static int hmm_vma_walk_hole(unsigned long addr,
301 unsigned long end,
302 struct mm_walk *walk)
304 struct hmm_vma_walk *hmm_vma_walk = walk->private;
305 struct hmm_range *range = hmm_vma_walk->range;
306 hmm_pfn_t *pfns = range->pfns;
307 unsigned long i;
309 hmm_vma_walk->last = addr;
310 i = (addr - range->start) >> PAGE_SHIFT;
311 for (; addr < end; addr += PAGE_SIZE, i++) {
312 pfns[i] = HMM_PFN_EMPTY;
313 if (hmm_vma_walk->fault) {
314 int ret;
316 ret = hmm_vma_do_fault(walk, addr, &pfns[i]);
317 if (ret != -EAGAIN)
318 return ret;
322 return hmm_vma_walk->fault ? -EAGAIN : 0;
325 static int hmm_vma_walk_clear(unsigned long addr,
326 unsigned long end,
327 struct mm_walk *walk)
329 struct hmm_vma_walk *hmm_vma_walk = walk->private;
330 struct hmm_range *range = hmm_vma_walk->range;
331 hmm_pfn_t *pfns = range->pfns;
332 unsigned long i;
334 hmm_vma_walk->last = addr;
335 i = (addr - range->start) >> PAGE_SHIFT;
336 for (; addr < end; addr += PAGE_SIZE, i++) {
337 pfns[i] = 0;
338 if (hmm_vma_walk->fault) {
339 int ret;
341 ret = hmm_vma_do_fault(walk, addr, &pfns[i]);
342 if (ret != -EAGAIN)
343 return ret;
347 return hmm_vma_walk->fault ? -EAGAIN : 0;
350 static int hmm_vma_walk_pmd(pmd_t *pmdp,
351 unsigned long start,
352 unsigned long end,
353 struct mm_walk *walk)
355 struct hmm_vma_walk *hmm_vma_walk = walk->private;
356 struct hmm_range *range = hmm_vma_walk->range;
357 struct vm_area_struct *vma = walk->vma;
358 hmm_pfn_t *pfns = range->pfns;
359 unsigned long addr = start, i;
360 bool write_fault;
361 hmm_pfn_t flag;
362 pte_t *ptep;
364 i = (addr - range->start) >> PAGE_SHIFT;
365 flag = vma->vm_flags & VM_READ ? HMM_PFN_READ : 0;
366 write_fault = hmm_vma_walk->fault & hmm_vma_walk->write;
368 again:
369 if (pmd_none(*pmdp))
370 return hmm_vma_walk_hole(start, end, walk);
372 if (pmd_huge(*pmdp) && vma->vm_flags & VM_HUGETLB)
373 return hmm_pfns_bad(start, end, walk);
375 if (pmd_devmap(*pmdp) || pmd_trans_huge(*pmdp)) {
376 unsigned long pfn;
377 pmd_t pmd;
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.
384 * So just read pmd value and check again its a transparent
385 * huge or device mapping one and compute corresponding pfn
386 * values.
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);
395 if (write_fault && !pmd_write(pmd))
396 return hmm_vma_walk_clear(start, end, walk);
398 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;
405 if (pmd_bad(*pmdp))
406 return hmm_pfns_bad(start, end, walk);
408 ptep = pte_offset_map(pmdp, addr);
409 for (; addr < end; addr += PAGE_SIZE, ptep++, i++) {
410 pte_t pte = *ptep;
412 pfns[i] = 0;
414 if (pte_none(pte)) {
415 pfns[i] = HMM_PFN_EMPTY;
416 if (hmm_vma_walk->fault)
417 goto fault;
418 continue;
421 if (!pte_present(pte)) {
422 swp_entry_t entry;
424 if (!non_swap_entry(entry)) {
425 if (hmm_vma_walk->fault)
426 goto fault;
427 continue;
430 entry = pte_to_swp_entry(pte);
433 * This is a special swap entry, ignore migration, use
434 * device and report anything else as error.
436 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)) {
445 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;
452 continue;
453 } else {
454 /* Report error for everything else */
455 pfns[i] = HMM_PFN_ERROR;
457 continue;
460 if (write_fault && !pte_write(pte))
461 goto fault;
463 pfns[i] = hmm_pfn_t_from_pfn(pte_pfn(pte)) | flag;
464 pfns[i] |= pte_write(pte) ? HMM_PFN_WRITE : 0;
465 continue;
467 fault:
468 pte_unmap(ptep);
469 /* Fault all pages in range */
470 return hmm_vma_walk_clear(start, end, walk);
472 pte_unmap(ptep - 1);
474 return 0;
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
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.
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.
494 * NOT CALLING hmm_vma_range_done() IF FUNCTION RETURNS 0 WILL LEAD TO SERIOUS
495 * MEMORY CORRUPTION ! YOU HAVE BEEN WARNED !
497 int 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)
503 struct hmm_vma_walk hmm_vma_walk;
504 struct mm_walk mm_walk;
505 struct hmm *hmm;
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;
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;
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;
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);
535 hmm_vma_walk.fault = false;
536 hmm_vma_walk.range = range;
537 mm_walk.private = &hmm_vma_walk;
539 mm_walk.vma = vma;
540 mm_walk.mm = vma->vm_mm;
541 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;
547 walk_page_range(start, end, &mm_walk);
548 return 0;
550 EXPORT_SYMBOL(hmm_vma_get_pfns);
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
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.
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.
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.
572 * There are two ways to use this :
573 * again:
574 * hmm_vma_get_pfns(vma, range, start, end, pfns); or hmm_vma_fault(...);
575 * 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;
581 * device_commit_transaction(trans);
582 * device_page_table_unlock();
584 * Or:
585 * hmm_vma_get_pfns(vma, range, start, end, pfns); or hmm_vma_fault(...);
586 * device_page_table_lock();
587 * hmm_vma_range_done(vma, range);
588 * device_update_page_table(pfns);
589 * device_page_table_unlock();
591 bool hmm_vma_range_done(struct vm_area_struct *vma, struct hmm_range *range)
593 unsigned long npages = (range->end - range->start) >> PAGE_SHIFT;
594 struct hmm *hmm;
596 if (range->end <= range->start) {
597 BUG();
598 return false;
601 hmm = hmm_register(vma->vm_mm);
602 if (!hmm) {
603 memset(range->pfns, 0, sizeof(*range->pfns) * npages);
604 return false;
607 spin_lock(&hmm->lock);
608 list_del_rcu(&range->list);
609 spin_unlock(&hmm->lock);
611 return range->valid;
613 EXPORT_SYMBOL(hmm_vma_range_done);
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)
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.
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.
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;
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)
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 !
662 * YOU HAVE BEEN WARNED !
664 int 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)
672 struct hmm_vma_walk hmm_vma_walk;
673 struct mm_walk mm_walk;
674 struct hmm *hmm;
675 int ret;
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;
683 hmm = hmm_register(vma->vm_mm);
684 if (!hmm) {
685 hmm_pfns_clear(pfns, start, end);
686 return -ENOMEM;
688 /* Caller must have registered a mirror using hmm_mirror_register() */
689 if (!hmm->mmu_notifier.ops)
690 return -EINVAL;
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);
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;
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;
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;
722 do {
723 ret = walk_page_range(start, end, &mm_walk);
724 start = hmm_vma_walk.last;
725 } while (ret == -EAGAIN);
727 if (ret) {
728 unsigned long i;
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);
734 return ret;
736 EXPORT_SYMBOL(hmm_vma_fault);
737 #endif /* IS_ENABLED(CONFIG_HMM_MIRROR) */
740 #if IS_ENABLED(CONFIG_DEVICE_PRIVATE) || IS_ENABLED(CONFIG_DEVICE_PUBLIC)
741 struct page *hmm_vma_alloc_locked_page(struct vm_area_struct *vma,
742 unsigned long addr)
744 struct page *page;
746 page = alloc_page_vma(GFP_HIGHUSER, vma, addr);
747 if (!page)
748 return NULL;
749 lock_page(page);
750 return page;
752 EXPORT_SYMBOL(hmm_vma_alloc_locked_page);
755 static void hmm_devmem_ref_release(struct percpu_ref *ref)
757 struct hmm_devmem *devmem;
759 devmem = container_of(ref, struct hmm_devmem, ref);
760 complete(&devmem->completion);
763 static void hmm_devmem_ref_exit(void *data)
765 struct percpu_ref *ref = data;
766 struct hmm_devmem *devmem;
768 devmem = container_of(ref, struct hmm_devmem, ref);
769 percpu_ref_exit(ref);
772 static void hmm_devmem_ref_kill(void *data)
774 struct percpu_ref *ref = data;
775 struct hmm_devmem *devmem;
777 devmem = container_of(ref, struct hmm_devmem, ref);
778 percpu_ref_kill(ref);
779 wait_for_completion(&devmem->completion);
782 static int hmm_devmem_fault(struct vm_area_struct *vma,
783 unsigned long addr,
784 const struct page *page,
785 unsigned int flags,
786 pmd_t *pmdp)
788 struct hmm_devmem *devmem = page->pgmap->data;
790 return devmem->ops->fault(devmem, vma, addr, page, flags, pmdp);
793 static void hmm_devmem_free(struct page *page, void *data)
795 struct hmm_devmem *devmem = data;
797 devmem->ops->free(devmem, page);
800 static DEFINE_MUTEX(hmm_devmem_lock);
801 static RADIX_TREE(hmm_devmem_radix, GFP_KERNEL);
803 static void hmm_devmem_radix_release(struct resource *resource)
805 resource_size_t key, align_start, align_size, align_end;
807 align_start = resource->start & ~(PA_SECTION_SIZE - 1);
808 align_size = ALIGN(resource_size(resource), PA_SECTION_SIZE);
809 align_end = align_start + align_size - 1;
811 mutex_lock(&hmm_devmem_lock);
812 for (key = resource->start;
813 key <= resource->end;
814 key += PA_SECTION_SIZE)
815 radix_tree_delete(&hmm_devmem_radix, key >> PA_SECTION_SHIFT);
816 mutex_unlock(&hmm_devmem_lock);
819 static void hmm_devmem_release(void *data)
821 struct hmm_devmem *devmem = data;
822 struct resource *resource = devmem->resource;
823 unsigned long start_pfn, npages;
824 struct zone *zone;
825 struct page *page;
827 /* pages are dead and unused, undo the arch mapping */
828 start_pfn = (resource->start & ~(PA_SECTION_SIZE - 1)) >> PAGE_SHIFT;
829 npages = ALIGN(resource_size(resource), PA_SECTION_SIZE) >> PAGE_SHIFT;
831 page = pfn_to_page(start_pfn);
832 zone = page_zone(page);
834 mem_hotplug_begin();
835 if (resource->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY)
836 __remove_pages(zone, start_pfn, npages);
837 else
838 arch_remove_memory(start_pfn << PAGE_SHIFT,
839 npages << PAGE_SHIFT);
840 mem_hotplug_done();
842 hmm_devmem_radix_release(resource);
845 static struct hmm_devmem *hmm_devmem_find(resource_size_t phys)
847 WARN_ON_ONCE(!rcu_read_lock_held());
849 return radix_tree_lookup(&hmm_devmem_radix, phys >> PA_SECTION_SHIFT);
852 static int hmm_devmem_pages_create(struct hmm_devmem *devmem)
854 resource_size_t key, align_start, align_size, align_end;
855 struct device *device = devmem->device;
856 int ret, nid, is_ram;
857 unsigned long pfn;
859 align_start = devmem->resource->start & ~(PA_SECTION_SIZE - 1);
860 align_size = ALIGN(devmem->resource->start +
861 resource_size(devmem->resource),
862 PA_SECTION_SIZE) - align_start;
864 is_ram = region_intersects(align_start, align_size,
865 IORESOURCE_SYSTEM_RAM,
866 IORES_DESC_NONE);
867 if (is_ram == REGION_MIXED) {
868 WARN_ONCE(1, "%s attempted on mixed region %pr\n",
869 __func__, devmem->resource);
870 return -ENXIO;
872 if (is_ram == REGION_INTERSECTS)
873 return -ENXIO;
875 if (devmem->resource->desc == IORES_DESC_DEVICE_PUBLIC_MEMORY)
876 devmem->pagemap.type = MEMORY_DEVICE_PUBLIC;
877 else
878 devmem->pagemap.type = MEMORY_DEVICE_PRIVATE;
880 devmem->pagemap.res = devmem->resource;
881 devmem->pagemap.page_fault = hmm_devmem_fault;
882 devmem->pagemap.page_free = hmm_devmem_free;
883 devmem->pagemap.dev = devmem->device;
884 devmem->pagemap.ref = &devmem->ref;
885 devmem->pagemap.data = devmem;
887 mutex_lock(&hmm_devmem_lock);
888 align_end = align_start + align_size - 1;
889 for (key = align_start; key <= align_end; key += PA_SECTION_SIZE) {
890 struct hmm_devmem *dup;
892 rcu_read_lock();
893 dup = hmm_devmem_find(key);
894 rcu_read_unlock();
895 if (dup) {
896 dev_err(device, "%s: collides with mapping for %s\n",
897 __func__, dev_name(dup->device));
898 mutex_unlock(&hmm_devmem_lock);
899 ret = -EBUSY;
900 goto error;
902 ret = radix_tree_insert(&hmm_devmem_radix,
903 key >> PA_SECTION_SHIFT,
904 devmem);
905 if (ret) {
906 dev_err(device, "%s: failed: %d\n", __func__, ret);
907 mutex_unlock(&hmm_devmem_lock);
908 goto error_radix;
911 mutex_unlock(&hmm_devmem_lock);
913 nid = dev_to_node(device);
914 if (nid < 0)
915 nid = numa_mem_id();
917 mem_hotplug_begin();
919 * For device private memory we call add_pages() as we only need to
920 * allocate and initialize struct page for the device memory. More-
921 * over the device memory is un-accessible thus we do not want to
922 * create a linear mapping for the memory like arch_add_memory()
923 * would do.
925 * For device public memory, which is accesible by the CPU, we do
926 * want the linear mapping and thus use arch_add_memory().
928 if (devmem->pagemap.type == MEMORY_DEVICE_PUBLIC)
929 ret = arch_add_memory(nid, align_start, align_size, false);
930 else
931 ret = add_pages(nid, align_start >> PAGE_SHIFT,
932 align_size >> PAGE_SHIFT, false);
933 if (ret) {
934 mem_hotplug_done();
935 goto error_add_memory;
937 move_pfn_range_to_zone(&NODE_DATA(nid)->node_zones[ZONE_DEVICE],
938 align_start >> PAGE_SHIFT,
939 align_size >> PAGE_SHIFT);
940 mem_hotplug_done();
942 for (pfn = devmem->pfn_first; pfn < devmem->pfn_last; pfn++) {
943 struct page *page = pfn_to_page(pfn);
945 page->pgmap = &devmem->pagemap;
947 return 0;
949 error_add_memory:
950 untrack_pfn(NULL, PHYS_PFN(align_start), align_size);
951 error_radix:
952 hmm_devmem_radix_release(devmem->resource);
953 error:
954 return ret;
958 * hmm_devmem_add() - hotplug ZONE_DEVICE memory for device memory
960 * @ops: memory event device driver callback (see struct hmm_devmem_ops)
961 * @device: device struct to bind the resource too
962 * @size: size in bytes of the device memory to add
963 * Returns: pointer to new hmm_devmem struct ERR_PTR otherwise
965 * This function first finds an empty range of physical address big enough to
966 * contain the new resource, and then hotplugs it as ZONE_DEVICE memory, which
967 * in turn allocates struct pages. It does not do anything beyond that; all
968 * events affecting the memory will go through the various callbacks provided
969 * by hmm_devmem_ops struct.
971 * Device driver should call this function during device initialization and
972 * is then responsible of memory management. HMM only provides helpers.
974 struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops,
975 struct device *device,
976 unsigned long size)
978 struct hmm_devmem *devmem;
979 resource_size_t addr;
980 int ret;
982 static_branch_enable(&device_private_key);
984 devmem = devm_kzalloc(device, sizeof(*devmem), GFP_KERNEL);
985 if (!devmem)
986 return ERR_PTR(-ENOMEM);
988 init_completion(&devmem->completion);
989 devmem->pfn_first = -1UL;
990 devmem->pfn_last = -1UL;
991 devmem->resource = NULL;
992 devmem->device = device;
993 devmem->ops = ops;
995 ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release,
996 0, GFP_KERNEL);
997 if (ret)
998 return ERR_PTR(ret);
1000 ret = devm_add_action_or_reset(device, hmm_devmem_ref_exit, &devmem->ref);
1001 if (ret)
1002 return ERR_PTR(ret);
1004 size = ALIGN(size, PA_SECTION_SIZE);
1005 addr = min((unsigned long)iomem_resource.end,
1006 (1UL << MAX_PHYSMEM_BITS) - 1);
1007 addr = addr - size + 1UL;
1010 * FIXME add a new helper to quickly walk resource tree and find free
1011 * range
1013 * FIXME what about ioport_resource resource ?
1015 for (; addr > size && addr >= iomem_resource.start; addr -= size) {
1016 ret = region_intersects(addr, size, 0, IORES_DESC_NONE);
1017 if (ret != REGION_DISJOINT)
1018 continue;
1020 devmem->resource = devm_request_mem_region(device, addr, size,
1021 dev_name(device));
1022 if (!devmem->resource)
1023 return ERR_PTR(-ENOMEM);
1024 break;
1026 if (!devmem->resource)
1027 return ERR_PTR(-ERANGE);
1029 devmem->resource->desc = IORES_DESC_DEVICE_PRIVATE_MEMORY;
1030 devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
1031 devmem->pfn_last = devmem->pfn_first +
1032 (resource_size(devmem->resource) >> PAGE_SHIFT);
1034 ret = hmm_devmem_pages_create(devmem);
1035 if (ret)
1036 return ERR_PTR(ret);
1038 ret = devm_add_action_or_reset(device, hmm_devmem_release, devmem);
1039 if (ret)
1040 return ERR_PTR(ret);
1042 return devmem;
1044 EXPORT_SYMBOL_GPL(hmm_devmem_add);
1046 struct hmm_devmem *hmm_devmem_add_resource(const struct hmm_devmem_ops *ops,
1047 struct device *device,
1048 struct resource *res)
1050 struct hmm_devmem *devmem;
1051 int ret;
1053 if (res->desc != IORES_DESC_DEVICE_PUBLIC_MEMORY)
1054 return ERR_PTR(-EINVAL);
1056 static_branch_enable(&device_private_key);
1058 devmem = devm_kzalloc(device, sizeof(*devmem), GFP_KERNEL);
1059 if (!devmem)
1060 return ERR_PTR(-ENOMEM);
1062 init_completion(&devmem->completion);
1063 devmem->pfn_first = -1UL;
1064 devmem->pfn_last = -1UL;
1065 devmem->resource = res;
1066 devmem->device = device;
1067 devmem->ops = ops;
1069 ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release,
1070 0, GFP_KERNEL);
1071 if (ret)
1072 return ERR_PTR(ret);
1074 ret = devm_add_action_or_reset(device, hmm_devmem_ref_exit,
1075 &devmem->ref);
1076 if (ret)
1077 return ERR_PTR(ret);
1079 devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
1080 devmem->pfn_last = devmem->pfn_first +
1081 (resource_size(devmem->resource) >> PAGE_SHIFT);
1083 ret = hmm_devmem_pages_create(devmem);
1084 if (ret)
1085 return ERR_PTR(ret);
1087 ret = devm_add_action_or_reset(device, hmm_devmem_release, devmem);
1088 if (ret)
1089 return ERR_PTR(ret);
1091 ret = devm_add_action_or_reset(device, hmm_devmem_ref_kill,
1092 &devmem->ref);
1093 if (ret)
1094 return ERR_PTR(ret);
1096 return devmem;
1098 EXPORT_SYMBOL_GPL(hmm_devmem_add_resource);
1101 * A device driver that wants to handle multiple devices memory through a
1102 * single fake device can use hmm_device to do so. This is purely a helper
1103 * and it is not needed to make use of any HMM functionality.
1105 #define HMM_DEVICE_MAX 256
1107 static DECLARE_BITMAP(hmm_device_mask, HMM_DEVICE_MAX);
1108 static DEFINE_SPINLOCK(hmm_device_lock);
1109 static struct class *hmm_device_class;
1110 static dev_t hmm_device_devt;
1112 static void hmm_device_release(struct device *device)
1114 struct hmm_device *hmm_device;
1116 hmm_device = container_of(device, struct hmm_device, device);
1117 spin_lock(&hmm_device_lock);
1118 clear_bit(hmm_device->minor, hmm_device_mask);
1119 spin_unlock(&hmm_device_lock);
1121 kfree(hmm_device);
1124 struct hmm_device *hmm_device_new(void *drvdata)
1126 struct hmm_device *hmm_device;
1128 hmm_device = kzalloc(sizeof(*hmm_device), GFP_KERNEL);
1129 if (!hmm_device)
1130 return ERR_PTR(-ENOMEM);
1132 spin_lock(&hmm_device_lock);
1133 hmm_device->minor = find_first_zero_bit(hmm_device_mask, HMM_DEVICE_MAX);
1134 if (hmm_device->minor >= HMM_DEVICE_MAX) {
1135 spin_unlock(&hmm_device_lock);
1136 kfree(hmm_device);
1137 return ERR_PTR(-EBUSY);
1139 set_bit(hmm_device->minor, hmm_device_mask);
1140 spin_unlock(&hmm_device_lock);
1142 dev_set_name(&hmm_device->device, "hmm_device%d", hmm_device->minor);
1143 hmm_device->device.devt = MKDEV(MAJOR(hmm_device_devt),
1144 hmm_device->minor);
1145 hmm_device->device.release = hmm_device_release;
1146 dev_set_drvdata(&hmm_device->device, drvdata);
1147 hmm_device->device.class = hmm_device_class;
1148 device_initialize(&hmm_device->device);
1150 return hmm_device;
1152 EXPORT_SYMBOL(hmm_device_new);
1154 void hmm_device_put(struct hmm_device *hmm_device)
1156 put_device(&hmm_device->device);
1158 EXPORT_SYMBOL(hmm_device_put);
1160 static int __init hmm_init(void)
1162 int ret;
1164 ret = alloc_chrdev_region(&hmm_device_devt, 0,
1165 HMM_DEVICE_MAX,
1166 "hmm_device");
1167 if (ret)
1168 return ret;
1170 hmm_device_class = class_create(THIS_MODULE, "hmm_device");
1171 if (IS_ERR(hmm_device_class)) {
1172 unregister_chrdev_region(hmm_device_devt, HMM_DEVICE_MAX);
1173 return PTR_ERR(hmm_device_class);
1175 return 0;
1178 device_initcall(hmm_init);
1179 #endif /* CONFIG_DEVICE_PRIVATE || CONFIG_DEVICE_PUBLIC */