Revert "Bluetooth: btusb: Fix quirk for Atheros 1525/QCA6174"
[linux/fpc-iii.git] / mm / hmm.c
blob91d3f062dd2885f1d76fe54a994f7df7f4d061f4
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 = pte_to_swp_entry(pte);
424 if (!non_swap_entry(entry)) {
425 if (hmm_vma_walk->fault)
426 goto fault;
427 continue;
431 * This is a special swap entry, ignore migration, use
432 * device and report anything else as error.
434 if (is_device_private_entry(entry)) {
435 pfns[i] = hmm_pfn_t_from_pfn(swp_offset(entry));
436 if (is_write_device_private_entry(entry)) {
437 pfns[i] |= HMM_PFN_WRITE;
438 } else if (write_fault)
439 goto fault;
440 pfns[i] |= HMM_PFN_DEVICE_UNADDRESSABLE;
441 pfns[i] |= flag;
442 } else if (is_migration_entry(entry)) {
443 if (hmm_vma_walk->fault) {
444 pte_unmap(ptep);
445 hmm_vma_walk->last = addr;
446 migration_entry_wait(vma->vm_mm,
447 pmdp, addr);
448 return -EAGAIN;
450 continue;
451 } else {
452 /* Report error for everything else */
453 pfns[i] = HMM_PFN_ERROR;
455 continue;
458 if (write_fault && !pte_write(pte))
459 goto fault;
461 pfns[i] = hmm_pfn_t_from_pfn(pte_pfn(pte)) | flag;
462 pfns[i] |= pte_write(pte) ? HMM_PFN_WRITE : 0;
463 continue;
465 fault:
466 pte_unmap(ptep);
467 /* Fault all pages in range */
468 return hmm_vma_walk_clear(start, end, walk);
470 pte_unmap(ptep - 1);
472 return 0;
476 * hmm_vma_get_pfns() - snapshot CPU page table for a range of virtual addresses
477 * @vma: virtual memory area containing the virtual address range
478 * @range: used to track snapshot validity
479 * @start: range virtual start address (inclusive)
480 * @end: range virtual end address (exclusive)
481 * @entries: array of hmm_pfn_t: provided by the caller, filled in by function
482 * Returns: -EINVAL if invalid argument, -ENOMEM out of memory, 0 success
484 * This snapshots the CPU page table for a range of virtual addresses. Snapshot
485 * validity is tracked by range struct. See hmm_vma_range_done() for further
486 * information.
488 * The range struct is initialized here. It tracks the CPU page table, but only
489 * if the function returns success (0), in which case the caller must then call
490 * hmm_vma_range_done() to stop CPU page table update tracking on this range.
492 * NOT CALLING hmm_vma_range_done() IF FUNCTION RETURNS 0 WILL LEAD TO SERIOUS
493 * MEMORY CORRUPTION ! YOU HAVE BEEN WARNED !
495 int hmm_vma_get_pfns(struct vm_area_struct *vma,
496 struct hmm_range *range,
497 unsigned long start,
498 unsigned long end,
499 hmm_pfn_t *pfns)
501 struct hmm_vma_walk hmm_vma_walk;
502 struct mm_walk mm_walk;
503 struct hmm *hmm;
505 /* FIXME support hugetlb fs */
506 if (is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_SPECIAL)) {
507 hmm_pfns_special(pfns, start, end);
508 return -EINVAL;
511 /* Sanity check, this really should not happen ! */
512 if (start < vma->vm_start || start >= vma->vm_end)
513 return -EINVAL;
514 if (end < vma->vm_start || end > vma->vm_end)
515 return -EINVAL;
517 hmm = hmm_register(vma->vm_mm);
518 if (!hmm)
519 return -ENOMEM;
520 /* Caller must have registered a mirror, via hmm_mirror_register() ! */
521 if (!hmm->mmu_notifier.ops)
522 return -EINVAL;
524 /* Initialize range to track CPU page table update */
525 range->start = start;
526 range->pfns = pfns;
527 range->end = end;
528 spin_lock(&hmm->lock);
529 range->valid = true;
530 list_add_rcu(&range->list, &hmm->ranges);
531 spin_unlock(&hmm->lock);
533 hmm_vma_walk.fault = false;
534 hmm_vma_walk.range = range;
535 mm_walk.private = &hmm_vma_walk;
537 mm_walk.vma = vma;
538 mm_walk.mm = vma->vm_mm;
539 mm_walk.pte_entry = NULL;
540 mm_walk.test_walk = NULL;
541 mm_walk.hugetlb_entry = NULL;
542 mm_walk.pmd_entry = hmm_vma_walk_pmd;
543 mm_walk.pte_hole = hmm_vma_walk_hole;
545 walk_page_range(start, end, &mm_walk);
546 return 0;
548 EXPORT_SYMBOL(hmm_vma_get_pfns);
551 * hmm_vma_range_done() - stop tracking change to CPU page table over a range
552 * @vma: virtual memory area containing the virtual address range
553 * @range: range being tracked
554 * Returns: false if range data has been invalidated, true otherwise
556 * Range struct is used to track updates to the CPU page table after a call to
557 * either hmm_vma_get_pfns() or hmm_vma_fault(). Once the device driver is done
558 * using the data, or wants to lock updates to the data it got from those
559 * functions, it must call the hmm_vma_range_done() function, which will then
560 * stop tracking CPU page table updates.
562 * Note that device driver must still implement general CPU page table update
563 * tracking either by using hmm_mirror (see hmm_mirror_register()) or by using
564 * the mmu_notifier API directly.
566 * CPU page table update tracking done through hmm_range is only temporary and
567 * to be used while trying to duplicate CPU page table contents for a range of
568 * virtual addresses.
570 * There are two ways to use this :
571 * again:
572 * hmm_vma_get_pfns(vma, range, start, end, pfns); or hmm_vma_fault(...);
573 * trans = device_build_page_table_update_transaction(pfns);
574 * device_page_table_lock();
575 * if (!hmm_vma_range_done(vma, range)) {
576 * device_page_table_unlock();
577 * goto again;
579 * device_commit_transaction(trans);
580 * device_page_table_unlock();
582 * Or:
583 * hmm_vma_get_pfns(vma, range, start, end, pfns); or hmm_vma_fault(...);
584 * device_page_table_lock();
585 * hmm_vma_range_done(vma, range);
586 * device_update_page_table(pfns);
587 * device_page_table_unlock();
589 bool hmm_vma_range_done(struct vm_area_struct *vma, struct hmm_range *range)
591 unsigned long npages = (range->end - range->start) >> PAGE_SHIFT;
592 struct hmm *hmm;
594 if (range->end <= range->start) {
595 BUG();
596 return false;
599 hmm = hmm_register(vma->vm_mm);
600 if (!hmm) {
601 memset(range->pfns, 0, sizeof(*range->pfns) * npages);
602 return false;
605 spin_lock(&hmm->lock);
606 list_del_rcu(&range->list);
607 spin_unlock(&hmm->lock);
609 return range->valid;
611 EXPORT_SYMBOL(hmm_vma_range_done);
614 * hmm_vma_fault() - try to fault some address in a virtual address range
615 * @vma: virtual memory area containing the virtual address range
616 * @range: use to track pfns array content validity
617 * @start: fault range virtual start address (inclusive)
618 * @end: fault range virtual end address (exclusive)
619 * @pfns: array of hmm_pfn_t, only entry with fault flag set will be faulted
620 * @write: is it a write fault
621 * @block: allow blocking on fault (if true it sleeps and do not drop mmap_sem)
622 * Returns: 0 success, error otherwise (-EAGAIN means mmap_sem have been drop)
624 * This is similar to a regular CPU page fault except that it will not trigger
625 * any memory migration if the memory being faulted is not accessible by CPUs.
627 * On error, for one virtual address in the range, the function will set the
628 * hmm_pfn_t error flag for the corresponding pfn entry.
630 * Expected use pattern:
631 * retry:
632 * down_read(&mm->mmap_sem);
633 * // Find vma and address device wants to fault, initialize hmm_pfn_t
634 * // array accordingly
635 * ret = hmm_vma_fault(vma, start, end, pfns, allow_retry);
636 * switch (ret) {
637 * case -EAGAIN:
638 * hmm_vma_range_done(vma, range);
639 * // You might want to rate limit or yield to play nicely, you may
640 * // also commit any valid pfn in the array assuming that you are
641 * // getting true from hmm_vma_range_monitor_end()
642 * goto retry;
643 * case 0:
644 * break;
645 * default:
646 * // Handle error !
647 * up_read(&mm->mmap_sem)
648 * return;
650 * // Take device driver lock that serialize device page table update
651 * driver_lock_device_page_table_update();
652 * hmm_vma_range_done(vma, range);
653 * // Commit pfns we got from hmm_vma_fault()
654 * driver_unlock_device_page_table_update();
655 * up_read(&mm->mmap_sem)
657 * YOU MUST CALL hmm_vma_range_done() AFTER THIS FUNCTION RETURN SUCCESS (0)
658 * BEFORE FREEING THE range struct OR YOU WILL HAVE SERIOUS MEMORY CORRUPTION !
660 * YOU HAVE BEEN WARNED !
662 int hmm_vma_fault(struct vm_area_struct *vma,
663 struct hmm_range *range,
664 unsigned long start,
665 unsigned long end,
666 hmm_pfn_t *pfns,
667 bool write,
668 bool block)
670 struct hmm_vma_walk hmm_vma_walk;
671 struct mm_walk mm_walk;
672 struct hmm *hmm;
673 int ret;
675 /* Sanity check, this really should not happen ! */
676 if (start < vma->vm_start || start >= vma->vm_end)
677 return -EINVAL;
678 if (end < vma->vm_start || end > vma->vm_end)
679 return -EINVAL;
681 hmm = hmm_register(vma->vm_mm);
682 if (!hmm) {
683 hmm_pfns_clear(pfns, start, end);
684 return -ENOMEM;
686 /* Caller must have registered a mirror using hmm_mirror_register() */
687 if (!hmm->mmu_notifier.ops)
688 return -EINVAL;
690 /* Initialize range to track CPU page table update */
691 range->start = start;
692 range->pfns = pfns;
693 range->end = end;
694 spin_lock(&hmm->lock);
695 range->valid = true;
696 list_add_rcu(&range->list, &hmm->ranges);
697 spin_unlock(&hmm->lock);
699 /* FIXME support hugetlb fs */
700 if (is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_SPECIAL)) {
701 hmm_pfns_special(pfns, start, end);
702 return 0;
705 hmm_vma_walk.fault = true;
706 hmm_vma_walk.write = write;
707 hmm_vma_walk.block = block;
708 hmm_vma_walk.range = range;
709 mm_walk.private = &hmm_vma_walk;
710 hmm_vma_walk.last = range->start;
712 mm_walk.vma = vma;
713 mm_walk.mm = vma->vm_mm;
714 mm_walk.pte_entry = NULL;
715 mm_walk.test_walk = NULL;
716 mm_walk.hugetlb_entry = NULL;
717 mm_walk.pmd_entry = hmm_vma_walk_pmd;
718 mm_walk.pte_hole = hmm_vma_walk_hole;
720 do {
721 ret = walk_page_range(start, end, &mm_walk);
722 start = hmm_vma_walk.last;
723 } while (ret == -EAGAIN);
725 if (ret) {
726 unsigned long i;
728 i = (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
729 hmm_pfns_clear(&pfns[i], hmm_vma_walk.last, end);
730 hmm_vma_range_done(vma, range);
732 return ret;
734 EXPORT_SYMBOL(hmm_vma_fault);
735 #endif /* IS_ENABLED(CONFIG_HMM_MIRROR) */
738 #if IS_ENABLED(CONFIG_DEVICE_PRIVATE) || IS_ENABLED(CONFIG_DEVICE_PUBLIC)
739 struct page *hmm_vma_alloc_locked_page(struct vm_area_struct *vma,
740 unsigned long addr)
742 struct page *page;
744 page = alloc_page_vma(GFP_HIGHUSER, vma, addr);
745 if (!page)
746 return NULL;
747 lock_page(page);
748 return page;
750 EXPORT_SYMBOL(hmm_vma_alloc_locked_page);
753 static void hmm_devmem_ref_release(struct percpu_ref *ref)
755 struct hmm_devmem *devmem;
757 devmem = container_of(ref, struct hmm_devmem, ref);
758 complete(&devmem->completion);
761 static void hmm_devmem_ref_exit(void *data)
763 struct percpu_ref *ref = data;
764 struct hmm_devmem *devmem;
766 devmem = container_of(ref, struct hmm_devmem, ref);
767 percpu_ref_exit(ref);
768 devm_remove_action(devmem->device, &hmm_devmem_ref_exit, data);
771 static void hmm_devmem_ref_kill(void *data)
773 struct percpu_ref *ref = data;
774 struct hmm_devmem *devmem;
776 devmem = container_of(ref, struct hmm_devmem, ref);
777 percpu_ref_kill(ref);
778 wait_for_completion(&devmem->completion);
779 devm_remove_action(devmem->device, &hmm_devmem_ref_kill, data);
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;
807 align_start = resource->start & ~(PA_SECTION_SIZE - 1);
808 align_size = ALIGN(resource_size(resource), PA_SECTION_SIZE);
810 mutex_lock(&hmm_devmem_lock);
811 for (key = resource->start;
812 key <= resource->end;
813 key += PA_SECTION_SIZE)
814 radix_tree_delete(&hmm_devmem_radix, key >> PA_SECTION_SHIFT);
815 mutex_unlock(&hmm_devmem_lock);
818 static void hmm_devmem_release(struct device *dev, void *data)
820 struct hmm_devmem *devmem = data;
821 struct resource *resource = devmem->resource;
822 unsigned long start_pfn, npages;
823 struct zone *zone;
824 struct page *page;
826 if (percpu_ref_tryget_live(&devmem->ref)) {
827 dev_WARN(dev, "%s: page mapping is still live!\n", __func__);
828 percpu_ref_put(&devmem->ref);
831 /* pages are dead and unused, undo the arch mapping */
832 start_pfn = (resource->start & ~(PA_SECTION_SIZE - 1)) >> PAGE_SHIFT;
833 npages = ALIGN(resource_size(resource), PA_SECTION_SIZE) >> PAGE_SHIFT;
835 page = pfn_to_page(start_pfn);
836 zone = page_zone(page);
838 mem_hotplug_begin();
839 if (resource->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY)
840 __remove_pages(zone, start_pfn, npages, NULL);
841 else
842 arch_remove_memory(start_pfn << PAGE_SHIFT,
843 npages << PAGE_SHIFT, NULL);
844 mem_hotplug_done();
846 hmm_devmem_radix_release(resource);
849 static struct hmm_devmem *hmm_devmem_find(resource_size_t phys)
851 WARN_ON_ONCE(!rcu_read_lock_held());
853 return radix_tree_lookup(&hmm_devmem_radix, phys >> PA_SECTION_SHIFT);
856 static int hmm_devmem_pages_create(struct hmm_devmem *devmem)
858 resource_size_t key, align_start, align_size, align_end;
859 struct device *device = devmem->device;
860 int ret, nid, is_ram;
861 unsigned long pfn;
863 align_start = devmem->resource->start & ~(PA_SECTION_SIZE - 1);
864 align_size = ALIGN(devmem->resource->start +
865 resource_size(devmem->resource),
866 PA_SECTION_SIZE) - align_start;
868 is_ram = region_intersects(align_start, align_size,
869 IORESOURCE_SYSTEM_RAM,
870 IORES_DESC_NONE);
871 if (is_ram == REGION_MIXED) {
872 WARN_ONCE(1, "%s attempted on mixed region %pr\n",
873 __func__, devmem->resource);
874 return -ENXIO;
876 if (is_ram == REGION_INTERSECTS)
877 return -ENXIO;
879 if (devmem->resource->desc == IORES_DESC_DEVICE_PUBLIC_MEMORY)
880 devmem->pagemap.type = MEMORY_DEVICE_PUBLIC;
881 else
882 devmem->pagemap.type = MEMORY_DEVICE_PRIVATE;
884 devmem->pagemap.res = *devmem->resource;
885 devmem->pagemap.page_fault = hmm_devmem_fault;
886 devmem->pagemap.page_free = hmm_devmem_free;
887 devmem->pagemap.dev = devmem->device;
888 devmem->pagemap.ref = &devmem->ref;
889 devmem->pagemap.data = devmem;
891 mutex_lock(&hmm_devmem_lock);
892 align_end = align_start + align_size - 1;
893 for (key = align_start; key <= align_end; key += PA_SECTION_SIZE) {
894 struct hmm_devmem *dup;
896 rcu_read_lock();
897 dup = hmm_devmem_find(key);
898 rcu_read_unlock();
899 if (dup) {
900 dev_err(device, "%s: collides with mapping for %s\n",
901 __func__, dev_name(dup->device));
902 mutex_unlock(&hmm_devmem_lock);
903 ret = -EBUSY;
904 goto error;
906 ret = radix_tree_insert(&hmm_devmem_radix,
907 key >> PA_SECTION_SHIFT,
908 devmem);
909 if (ret) {
910 dev_err(device, "%s: failed: %d\n", __func__, ret);
911 mutex_unlock(&hmm_devmem_lock);
912 goto error_radix;
915 mutex_unlock(&hmm_devmem_lock);
917 nid = dev_to_node(device);
918 if (nid < 0)
919 nid = numa_mem_id();
921 mem_hotplug_begin();
923 * For device private memory we call add_pages() as we only need to
924 * allocate and initialize struct page for the device memory. More-
925 * over the device memory is un-accessible thus we do not want to
926 * create a linear mapping for the memory like arch_add_memory()
927 * would do.
929 * For device public memory, which is accesible by the CPU, we do
930 * want the linear mapping and thus use arch_add_memory().
932 if (devmem->pagemap.type == MEMORY_DEVICE_PUBLIC)
933 ret = arch_add_memory(nid, align_start, align_size, NULL,
934 false);
935 else
936 ret = add_pages(nid, align_start >> PAGE_SHIFT,
937 align_size >> PAGE_SHIFT, NULL, false);
938 if (ret) {
939 mem_hotplug_done();
940 goto error_add_memory;
942 move_pfn_range_to_zone(&NODE_DATA(nid)->node_zones[ZONE_DEVICE],
943 align_start >> PAGE_SHIFT,
944 align_size >> PAGE_SHIFT, NULL);
945 mem_hotplug_done();
947 for (pfn = devmem->pfn_first; pfn < devmem->pfn_last; pfn++) {
948 struct page *page = pfn_to_page(pfn);
950 page->pgmap = &devmem->pagemap;
952 return 0;
954 error_add_memory:
955 untrack_pfn(NULL, PHYS_PFN(align_start), align_size);
956 error_radix:
957 hmm_devmem_radix_release(devmem->resource);
958 error:
959 return ret;
962 static int hmm_devmem_match(struct device *dev, void *data, void *match_data)
964 struct hmm_devmem *devmem = data;
966 return devmem->resource == match_data;
969 static void hmm_devmem_pages_remove(struct hmm_devmem *devmem)
971 devres_release(devmem->device, &hmm_devmem_release,
972 &hmm_devmem_match, devmem->resource);
976 * hmm_devmem_add() - hotplug ZONE_DEVICE memory for device memory
978 * @ops: memory event device driver callback (see struct hmm_devmem_ops)
979 * @device: device struct to bind the resource too
980 * @size: size in bytes of the device memory to add
981 * Returns: pointer to new hmm_devmem struct ERR_PTR otherwise
983 * This function first finds an empty range of physical address big enough to
984 * contain the new resource, and then hotplugs it as ZONE_DEVICE memory, which
985 * in turn allocates struct pages. It does not do anything beyond that; all
986 * events affecting the memory will go through the various callbacks provided
987 * by hmm_devmem_ops struct.
989 * Device driver should call this function during device initialization and
990 * is then responsible of memory management. HMM only provides helpers.
992 struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops,
993 struct device *device,
994 unsigned long size)
996 struct hmm_devmem *devmem;
997 resource_size_t addr;
998 int ret;
1000 static_branch_enable(&device_private_key);
1002 devmem = devres_alloc_node(&hmm_devmem_release, sizeof(*devmem),
1003 GFP_KERNEL, dev_to_node(device));
1004 if (!devmem)
1005 return ERR_PTR(-ENOMEM);
1007 init_completion(&devmem->completion);
1008 devmem->pfn_first = -1UL;
1009 devmem->pfn_last = -1UL;
1010 devmem->resource = NULL;
1011 devmem->device = device;
1012 devmem->ops = ops;
1014 ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release,
1015 0, GFP_KERNEL);
1016 if (ret)
1017 goto error_percpu_ref;
1019 ret = devm_add_action(device, hmm_devmem_ref_exit, &devmem->ref);
1020 if (ret)
1021 goto error_devm_add_action;
1023 size = ALIGN(size, PA_SECTION_SIZE);
1024 addr = min((unsigned long)iomem_resource.end,
1025 (1UL << MAX_PHYSMEM_BITS) - 1);
1026 addr = addr - size + 1UL;
1029 * FIXME add a new helper to quickly walk resource tree and find free
1030 * range
1032 * FIXME what about ioport_resource resource ?
1034 for (; addr > size && addr >= iomem_resource.start; addr -= size) {
1035 ret = region_intersects(addr, size, 0, IORES_DESC_NONE);
1036 if (ret != REGION_DISJOINT)
1037 continue;
1039 devmem->resource = devm_request_mem_region(device, addr, size,
1040 dev_name(device));
1041 if (!devmem->resource) {
1042 ret = -ENOMEM;
1043 goto error_no_resource;
1045 break;
1047 if (!devmem->resource) {
1048 ret = -ERANGE;
1049 goto error_no_resource;
1052 devmem->resource->desc = IORES_DESC_DEVICE_PRIVATE_MEMORY;
1053 devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
1054 devmem->pfn_last = devmem->pfn_first +
1055 (resource_size(devmem->resource) >> PAGE_SHIFT);
1057 ret = hmm_devmem_pages_create(devmem);
1058 if (ret)
1059 goto error_pages;
1061 devres_add(device, devmem);
1063 ret = devm_add_action(device, hmm_devmem_ref_kill, &devmem->ref);
1064 if (ret) {
1065 hmm_devmem_remove(devmem);
1066 return ERR_PTR(ret);
1069 return devmem;
1071 error_pages:
1072 devm_release_mem_region(device, devmem->resource->start,
1073 resource_size(devmem->resource));
1074 error_no_resource:
1075 error_devm_add_action:
1076 hmm_devmem_ref_kill(&devmem->ref);
1077 hmm_devmem_ref_exit(&devmem->ref);
1078 error_percpu_ref:
1079 devres_free(devmem);
1080 return ERR_PTR(ret);
1082 EXPORT_SYMBOL(hmm_devmem_add);
1084 struct hmm_devmem *hmm_devmem_add_resource(const struct hmm_devmem_ops *ops,
1085 struct device *device,
1086 struct resource *res)
1088 struct hmm_devmem *devmem;
1089 int ret;
1091 if (res->desc != IORES_DESC_DEVICE_PUBLIC_MEMORY)
1092 return ERR_PTR(-EINVAL);
1094 static_branch_enable(&device_private_key);
1096 devmem = devres_alloc_node(&hmm_devmem_release, sizeof(*devmem),
1097 GFP_KERNEL, dev_to_node(device));
1098 if (!devmem)
1099 return ERR_PTR(-ENOMEM);
1101 init_completion(&devmem->completion);
1102 devmem->pfn_first = -1UL;
1103 devmem->pfn_last = -1UL;
1104 devmem->resource = res;
1105 devmem->device = device;
1106 devmem->ops = ops;
1108 ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release,
1109 0, GFP_KERNEL);
1110 if (ret)
1111 goto error_percpu_ref;
1113 ret = devm_add_action(device, hmm_devmem_ref_exit, &devmem->ref);
1114 if (ret)
1115 goto error_devm_add_action;
1118 devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
1119 devmem->pfn_last = devmem->pfn_first +
1120 (resource_size(devmem->resource) >> PAGE_SHIFT);
1122 ret = hmm_devmem_pages_create(devmem);
1123 if (ret)
1124 goto error_devm_add_action;
1126 devres_add(device, devmem);
1128 ret = devm_add_action(device, hmm_devmem_ref_kill, &devmem->ref);
1129 if (ret) {
1130 hmm_devmem_remove(devmem);
1131 return ERR_PTR(ret);
1134 return devmem;
1136 error_devm_add_action:
1137 hmm_devmem_ref_kill(&devmem->ref);
1138 hmm_devmem_ref_exit(&devmem->ref);
1139 error_percpu_ref:
1140 devres_free(devmem);
1141 return ERR_PTR(ret);
1143 EXPORT_SYMBOL(hmm_devmem_add_resource);
1146 * hmm_devmem_remove() - remove device memory (kill and free ZONE_DEVICE)
1148 * @devmem: hmm_devmem struct use to track and manage the ZONE_DEVICE memory
1150 * This will hot-unplug memory that was hotplugged by hmm_devmem_add on behalf
1151 * of the device driver. It will free struct page and remove the resource that
1152 * reserved the physical address range for this device memory.
1154 void hmm_devmem_remove(struct hmm_devmem *devmem)
1156 resource_size_t start, size;
1157 struct device *device;
1158 bool cdm = false;
1160 if (!devmem)
1161 return;
1163 device = devmem->device;
1164 start = devmem->resource->start;
1165 size = resource_size(devmem->resource);
1167 cdm = devmem->resource->desc == IORES_DESC_DEVICE_PUBLIC_MEMORY;
1168 hmm_devmem_ref_kill(&devmem->ref);
1169 hmm_devmem_ref_exit(&devmem->ref);
1170 hmm_devmem_pages_remove(devmem);
1172 if (!cdm)
1173 devm_release_mem_region(device, start, size);
1175 EXPORT_SYMBOL(hmm_devmem_remove);
1178 * A device driver that wants to handle multiple devices memory through a
1179 * single fake device can use hmm_device to do so. This is purely a helper
1180 * and it is not needed to make use of any HMM functionality.
1182 #define HMM_DEVICE_MAX 256
1184 static DECLARE_BITMAP(hmm_device_mask, HMM_DEVICE_MAX);
1185 static DEFINE_SPINLOCK(hmm_device_lock);
1186 static struct class *hmm_device_class;
1187 static dev_t hmm_device_devt;
1189 static void hmm_device_release(struct device *device)
1191 struct hmm_device *hmm_device;
1193 hmm_device = container_of(device, struct hmm_device, device);
1194 spin_lock(&hmm_device_lock);
1195 clear_bit(hmm_device->minor, hmm_device_mask);
1196 spin_unlock(&hmm_device_lock);
1198 kfree(hmm_device);
1201 struct hmm_device *hmm_device_new(void *drvdata)
1203 struct hmm_device *hmm_device;
1205 hmm_device = kzalloc(sizeof(*hmm_device), GFP_KERNEL);
1206 if (!hmm_device)
1207 return ERR_PTR(-ENOMEM);
1209 spin_lock(&hmm_device_lock);
1210 hmm_device->minor = find_first_zero_bit(hmm_device_mask, HMM_DEVICE_MAX);
1211 if (hmm_device->minor >= HMM_DEVICE_MAX) {
1212 spin_unlock(&hmm_device_lock);
1213 kfree(hmm_device);
1214 return ERR_PTR(-EBUSY);
1216 set_bit(hmm_device->minor, hmm_device_mask);
1217 spin_unlock(&hmm_device_lock);
1219 dev_set_name(&hmm_device->device, "hmm_device%d", hmm_device->minor);
1220 hmm_device->device.devt = MKDEV(MAJOR(hmm_device_devt),
1221 hmm_device->minor);
1222 hmm_device->device.release = hmm_device_release;
1223 dev_set_drvdata(&hmm_device->device, drvdata);
1224 hmm_device->device.class = hmm_device_class;
1225 device_initialize(&hmm_device->device);
1227 return hmm_device;
1229 EXPORT_SYMBOL(hmm_device_new);
1231 void hmm_device_put(struct hmm_device *hmm_device)
1233 put_device(&hmm_device->device);
1235 EXPORT_SYMBOL(hmm_device_put);
1237 static int __init hmm_init(void)
1239 int ret;
1241 ret = alloc_chrdev_region(&hmm_device_devt, 0,
1242 HMM_DEVICE_MAX,
1243 "hmm_device");
1244 if (ret)
1245 return ret;
1247 hmm_device_class = class_create(THIS_MODULE, "hmm_device");
1248 if (IS_ERR(hmm_device_class)) {
1249 unregister_chrdev_region(hmm_device_devt, HMM_DEVICE_MAX);
1250 return PTR_ERR(hmm_device_class);
1252 return 0;
1255 device_initcall(hmm_init);
1256 #endif /* CONFIG_DEVICE_PRIVATE || CONFIG_DEVICE_PUBLIC */