]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - mm/hmm.c
mm/compaction.c: clear total_{migrate,free}_scanned before scanning a new zone
[mirror_ubuntu-bionic-kernel.git] / mm / hmm.c
CommitLineData
133ff0ea
JG
1/*
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>
858b54da 22#include <linux/init.h>
da4c3c73
JG
23#include <linux/rmap.h>
24#include <linux/swap.h>
133ff0ea
JG
25#include <linux/slab.h>
26#include <linux/sched.h>
4ef589dc
JG
27#include <linux/mmzone.h>
28#include <linux/pagemap.h>
da4c3c73
JG
29#include <linux/swapops.h>
30#include <linux/hugetlb.h>
4ef589dc 31#include <linux/memremap.h>
7b2d55d2 32#include <linux/jump_label.h>
c0b12405 33#include <linux/mmu_notifier.h>
4ef589dc
JG
34#include <linux/memory_hotplug.h>
35
36#define PA_SECTION_SIZE (1UL << PA_SECTION_SHIFT)
133ff0ea 37
6b368cd4 38#if defined(CONFIG_DEVICE_PRIVATE) || defined(CONFIG_DEVICE_PUBLIC)
7b2d55d2
JG
39/*
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);
6b368cd4 44#endif /* CONFIG_DEVICE_PRIVATE || CONFIG_DEVICE_PUBLIC */
7b2d55d2
JG
45
46
6b368cd4 47#if IS_ENABLED(CONFIG_HMM_MIRROR)
c0b12405
JG
48static const struct mmu_notifier_ops hmm_mmu_notifier_ops;
49
133ff0ea
JG
50/*
51 * struct hmm - HMM per mm struct
52 *
53 * @mm: mm struct this HMM struct is bound to
da4c3c73 54 * @lock: lock protecting ranges list
c0b12405 55 * @sequence: we track updates to the CPU page table with a sequence number
da4c3c73 56 * @ranges: list of range being snapshotted
c0b12405
JG
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
133ff0ea
JG
60 */
61struct hmm {
62 struct mm_struct *mm;
da4c3c73 63 spinlock_t lock;
c0b12405 64 atomic_t sequence;
da4c3c73 65 struct list_head ranges;
c0b12405
JG
66 struct list_head mirrors;
67 struct mmu_notifier mmu_notifier;
68 struct rw_semaphore mirrors_sem;
133ff0ea
JG
69};
70
71/*
72 * hmm_register - register HMM against an mm (HMM internal)
73 *
74 * @mm: mm struct to attach to
75 *
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.
78 */
79static struct hmm *hmm_register(struct mm_struct *mm)
80{
c0b12405
JG
81 struct hmm *hmm = READ_ONCE(mm->hmm);
82 bool cleanup = false;
133ff0ea
JG
83
84 /*
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.
88 */
c0b12405
JG
89 if (hmm)
90 return hmm;
91
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;
da4c3c73
JG
99 INIT_LIST_HEAD(&hmm->ranges);
100 spin_lock_init(&hmm->lock);
c0b12405
JG
101 hmm->mm = mm;
102
103 /*
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()
106 */
107 hmm->mmu_notifier.ops = &hmm_mmu_notifier_ops;
108 if (__mmu_notifier_register(&hmm->mmu_notifier, mm)) {
109 kfree(hmm);
110 return NULL;
111 }
112
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);
119
120 if (cleanup) {
121 mmu_notifier_unregister(&hmm->mmu_notifier, mm);
122 kfree(hmm);
123 }
124
133ff0ea
JG
125 return mm->hmm;
126}
127
128void hmm_mm_destroy(struct mm_struct *mm)
129{
130 kfree(mm->hmm);
131}
c0b12405 132
c0b12405
JG
133static void hmm_invalidate_range(struct hmm *hmm,
134 enum hmm_update_type action,
135 unsigned long start,
136 unsigned long end)
137{
138 struct hmm_mirror *mirror;
da4c3c73
JG
139 struct hmm_range *range;
140
141 spin_lock(&hmm->lock);
142 list_for_each_entry(range, &hmm->ranges, list) {
143 unsigned long addr, idx, npages;
144
145 if (end < range->start || start >= range->end)
146 continue;
147
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);
153 }
154 spin_unlock(&hmm->lock);
c0b12405
JG
155
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);
161}
162
163static void hmm_invalidate_range_start(struct mmu_notifier *mn,
164 struct mm_struct *mm,
165 unsigned long start,
166 unsigned long end)
167{
168 struct hmm *hmm = mm->hmm;
169
170 VM_BUG_ON(!hmm);
171
172 atomic_inc(&hmm->sequence);
173}
174
175static void hmm_invalidate_range_end(struct mmu_notifier *mn,
176 struct mm_struct *mm,
177 unsigned long start,
178 unsigned long end)
179{
180 struct hmm *hmm = mm->hmm;
181
182 VM_BUG_ON(!hmm);
183
184 hmm_invalidate_range(mm->hmm, HMM_UPDATE_INVALIDATE, start, end);
185}
186
187static 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,
190};
191
192/*
193 * hmm_mirror_register() - register a mirror against an mm
194 *
195 * @mirror: new mirror struct to register
196 * @mm: mm to register against
197 *
198 * To start mirroring a process address space, the device driver must register
199 * an HMM mirror struct.
200 *
201 * THE mm->mmap_sem MUST BE HELD IN WRITE MODE !
202 */
203int hmm_mirror_register(struct hmm_mirror *mirror, struct mm_struct *mm)
204{
205 /* Sanity check */
206 if (!mm || !mirror || !mirror->ops)
207 return -EINVAL;
208
209 mirror->hmm = hmm_register(mm);
210 if (!mirror->hmm)
211 return -ENOMEM;
212
213 down_write(&mirror->hmm->mirrors_sem);
214 list_add(&mirror->list, &mirror->hmm->mirrors);
215 up_write(&mirror->hmm->mirrors_sem);
216
217 return 0;
218}
219EXPORT_SYMBOL(hmm_mirror_register);
220
221/*
222 * hmm_mirror_unregister() - unregister a mirror
223 *
224 * @mirror: new mirror struct to register
225 *
226 * Stop mirroring a process address space, and cleanup.
227 */
228void hmm_mirror_unregister(struct hmm_mirror *mirror)
229{
230 struct hmm *hmm = mirror->hmm;
231
232 down_write(&hmm->mirrors_sem);
233 list_del(&mirror->list);
234 up_write(&hmm->mirrors_sem);
235}
236EXPORT_SYMBOL(hmm_mirror_unregister);
da4c3c73 237
74eee180
JG
238struct hmm_vma_walk {
239 struct hmm_range *range;
240 unsigned long last;
241 bool fault;
242 bool block;
243 bool write;
244};
245
246static int hmm_vma_do_fault(struct mm_walk *walk,
247 unsigned long addr,
248 hmm_pfn_t *pfn)
249{
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;
254
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;
263 }
264
265 return -EAGAIN;
266}
267
da4c3c73
JG
268static void hmm_pfns_special(hmm_pfn_t *pfns,
269 unsigned long addr,
270 unsigned long end)
271{
272 for (; addr < end; addr += PAGE_SIZE, pfns++)
273 *pfns = HMM_PFN_SPECIAL;
274}
275
276static int hmm_pfns_bad(unsigned long addr,
277 unsigned long end,
278 struct mm_walk *walk)
279{
4271c743
JG
280 struct hmm_vma_walk *hmm_vma_walk = walk->private;
281 struct hmm_range *range = hmm_vma_walk->range;
da4c3c73
JG
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
74eee180
JG
292static 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
da4c3c73
JG
300static int hmm_vma_walk_hole(unsigned long addr,
301 unsigned long end,
302 struct mm_walk *walk)
303{
74eee180
JG
304 struct hmm_vma_walk *hmm_vma_walk = walk->private;
305 struct hmm_range *range = hmm_vma_walk->range;
da4c3c73
JG
306 hmm_pfn_t *pfns = range->pfns;
307 unsigned long i;
308
74eee180 309 hmm_vma_walk->last = addr;
da4c3c73 310 i = (addr - range->start) >> PAGE_SHIFT;
74eee180 311 for (; addr < end; addr += PAGE_SIZE, i++) {
da4c3c73 312 pfns[i] = HMM_PFN_EMPTY;
74eee180
JG
313 if (hmm_vma_walk->fault) {
314 int ret;
da4c3c73 315
74eee180
JG
316 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;
da4c3c73
JG
323}
324
325static int hmm_vma_walk_clear(unsigned long addr,
326 unsigned long end,
327 struct mm_walk *walk)
328{
74eee180
JG
329 struct hmm_vma_walk *hmm_vma_walk = walk->private;
330 struct hmm_range *range = hmm_vma_walk->range;
da4c3c73
JG
331 hmm_pfn_t *pfns = range->pfns;
332 unsigned long i;
333
74eee180 334 hmm_vma_walk->last = addr;
da4c3c73 335 i = (addr - range->start) >> PAGE_SHIFT;
74eee180 336 for (; addr < end; addr += PAGE_SIZE, i++) {
da4c3c73 337 pfns[i] = 0;
74eee180
JG
338 if (hmm_vma_walk->fault) {
339 int ret;
da4c3c73 340
74eee180
JG
341 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;
da4c3c73
JG
348}
349
350static int hmm_vma_walk_pmd(pmd_t *pmdp,
351 unsigned long start,
352 unsigned long end,
353 struct mm_walk *walk)
354{
74eee180
JG
355 struct hmm_vma_walk *hmm_vma_walk = walk->private;
356 struct hmm_range *range = hmm_vma_walk->range;
da4c3c73
JG
357 struct vm_area_struct *vma = walk->vma;
358 hmm_pfn_t *pfns = range->pfns;
359 unsigned long addr = start, i;
74eee180 360 bool write_fault;
da4c3c73
JG
361 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;
74eee180 366 write_fault = hmm_vma_walk->fault & hmm_vma_walk->write;
da4c3c73
JG
367
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
f6f37321 395 if (write_fault && !pmd_write(pmd))
74eee180
JG
396 return hmm_vma_walk_clear(start, end, walk);
397
da4c3c73 398 pfn = pmd_pfn(pmd) + pte_index(addr);
f6f37321 399 flag |= pmd_write(pmd) ? HMM_PFN_WRITE : 0;
da4c3c73
JG
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
74eee180 414 if (pte_none(pte)) {
da4c3c73 415 pfns[i] = HMM_PFN_EMPTY;
74eee180
JG
416 if (hmm_vma_walk->fault)
417 goto fault;
da4c3c73
JG
418 continue;
419 }
420
74eee180
JG
421 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 */
4ef589dc
JG
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)) {
74eee180
JG
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;
451 }
452 continue;
453 } else {
454 /* Report error for everything else */
455 pfns[i] = HMM_PFN_ERROR;
456 }
457 continue;
458 }
459
f6f37321 460 if (write_fault && !pte_write(pte))
74eee180
JG
461 goto fault;
462
da4c3c73 463 pfns[i] = hmm_pfn_t_from_pfn(pte_pfn(pte)) | flag;
f6f37321 464 pfns[i] |= pte_write(pte) ? HMM_PFN_WRITE : 0;
74eee180
JG
465 continue;
466
467fault:
468 pte_unmap(ptep);
469 /* Fault all pages in range */
470 return hmm_vma_walk_clear(start, end, walk);
da4c3c73
JG
471 }
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{
74eee180 503 struct hmm_vma_walk hmm_vma_walk;
da4c3c73
JG
504 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
74eee180
JG
535 hmm_vma_walk.fault = false;
536 hmm_vma_walk.range = range;
537 mm_walk.private = &hmm_vma_walk;
538
da4c3c73
JG
539 mm_walk.vma = vma;
540 mm_walk.mm = vma->vm_mm;
da4c3c73
JG
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;
546
547 walk_page_range(start, end, &mm_walk);
da4c3c73
JG
548 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:
74eee180 574 * hmm_vma_get_pfns(vma, range, start, end, pfns); or hmm_vma_fault(...);
da4c3c73
JG
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;
580 * }
581 * device_commit_transaction(trans);
582 * device_page_table_unlock();
583 *
584 * Or:
74eee180 585 * hmm_vma_get_pfns(vma, range, start, end, pfns); or hmm_vma_fault(...);
da4c3c73
JG
586 * 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);
74eee180
JG
614
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);
c0b12405 737#endif /* IS_ENABLED(CONFIG_HMM_MIRROR) */
4ef589dc
JG
738
739
df6ad698 740#if IS_ENABLED(CONFIG_DEVICE_PRIVATE) || IS_ENABLED(CONFIG_DEVICE_PUBLIC)
4ef589dc
JG
741struct 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);
4ef589dc
JG
770}
771
772static void hmm_devmem_ref_kill(void *data)
773{
774 struct percpu_ref *ref = data;
775 struct hmm_devmem *devmem;
776
777 devmem = container_of(ref, struct hmm_devmem, ref);
778 percpu_ref_kill(ref);
779 wait_for_completion(&devmem->completion);
4ef589dc
JG
780}
781
782static 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)
787{
788 struct hmm_devmem *devmem = page->pgmap->data;
789
790 return devmem->ops->fault(devmem, vma, addr, page, flags, pmdp);
791}
792
793static void hmm_devmem_free(struct page *page, void *data)
794{
795 struct hmm_devmem *devmem = data;
796
0b756c49
DW
797 page->mapping = NULL;
798
4ef589dc
JG
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{
fec11bc0 807 resource_size_t key, align_start, align_size;
4ef589dc
JG
808
809 align_start = resource->start & ~(PA_SECTION_SIZE - 1);
810 align_size = ALIGN(resource_size(resource), PA_SECTION_SIZE);
4ef589dc
JG
811
812 mutex_lock(&hmm_devmem_lock);
813 for (key = resource->start;
814 key <= resource->end;
815 key += PA_SECTION_SIZE)
816 radix_tree_delete(&hmm_devmem_radix, key >> PA_SECTION_SHIFT);
817 mutex_unlock(&hmm_devmem_lock);
818}
819
483cb5e3 820static void hmm_devmem_release(void *data)
4ef589dc
JG
821{
822 struct hmm_devmem *devmem = data;
823 struct resource *resource = devmem->resource;
824 unsigned long start_pfn, npages;
825 struct zone *zone;
826 struct page *page;
827
4ef589dc
JG
828 /* pages are dead and unused, undo the arch mapping */
829 start_pfn = (resource->start & ~(PA_SECTION_SIZE - 1)) >> PAGE_SHIFT;
830 npages = ALIGN(resource_size(resource), PA_SECTION_SIZE) >> PAGE_SHIFT;
831
832 page = pfn_to_page(start_pfn);
833 zone = page_zone(page);
834
835 mem_hotplug_begin();
d3df0a42
JG
836 if (resource->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY)
837 __remove_pages(zone, start_pfn, npages);
838 else
839 arch_remove_memory(start_pfn << PAGE_SHIFT,
840 npages << PAGE_SHIFT);
4ef589dc
JG
841 mem_hotplug_done();
842
843 hmm_devmem_radix_release(resource);
844}
845
846static struct hmm_devmem *hmm_devmem_find(resource_size_t phys)
847{
848 WARN_ON_ONCE(!rcu_read_lock_held());
849
850 return radix_tree_lookup(&hmm_devmem_radix, phys >> PA_SECTION_SHIFT);
851}
852
853static int hmm_devmem_pages_create(struct hmm_devmem *devmem)
854{
855 resource_size_t key, align_start, align_size, align_end;
856 struct device *device = devmem->device;
857 int ret, nid, is_ram;
858 unsigned long pfn;
859
860 align_start = devmem->resource->start & ~(PA_SECTION_SIZE - 1);
861 align_size = ALIGN(devmem->resource->start +
862 resource_size(devmem->resource),
863 PA_SECTION_SIZE) - align_start;
864
865 is_ram = region_intersects(align_start, align_size,
866 IORESOURCE_SYSTEM_RAM,
867 IORES_DESC_NONE);
868 if (is_ram == REGION_MIXED) {
869 WARN_ONCE(1, "%s attempted on mixed region %pr\n",
870 __func__, devmem->resource);
871 return -ENXIO;
872 }
873 if (is_ram == REGION_INTERSECTS)
874 return -ENXIO;
875
d3df0a42
JG
876 if (devmem->resource->desc == IORES_DESC_DEVICE_PUBLIC_MEMORY)
877 devmem->pagemap.type = MEMORY_DEVICE_PUBLIC;
878 else
879 devmem->pagemap.type = MEMORY_DEVICE_PRIVATE;
880
4ef589dc
JG
881 devmem->pagemap.res = devmem->resource;
882 devmem->pagemap.page_fault = hmm_devmem_fault;
883 devmem->pagemap.page_free = hmm_devmem_free;
884 devmem->pagemap.dev = devmem->device;
885 devmem->pagemap.ref = &devmem->ref;
886 devmem->pagemap.data = devmem;
887
888 mutex_lock(&hmm_devmem_lock);
889 align_end = align_start + align_size - 1;
890 for (key = align_start; key <= align_end; key += PA_SECTION_SIZE) {
891 struct hmm_devmem *dup;
892
893 rcu_read_lock();
894 dup = hmm_devmem_find(key);
895 rcu_read_unlock();
896 if (dup) {
897 dev_err(device, "%s: collides with mapping for %s\n",
898 __func__, dev_name(dup->device));
899 mutex_unlock(&hmm_devmem_lock);
900 ret = -EBUSY;
901 goto error;
902 }
903 ret = radix_tree_insert(&hmm_devmem_radix,
904 key >> PA_SECTION_SHIFT,
905 devmem);
906 if (ret) {
907 dev_err(device, "%s: failed: %d\n", __func__, ret);
908 mutex_unlock(&hmm_devmem_lock);
909 goto error_radix;
910 }
911 }
912 mutex_unlock(&hmm_devmem_lock);
913
914 nid = dev_to_node(device);
915 if (nid < 0)
916 nid = numa_mem_id();
917
918 mem_hotplug_begin();
919 /*
920 * For device private memory we call add_pages() as we only need to
921 * allocate and initialize struct page for the device memory. More-
922 * over the device memory is un-accessible thus we do not want to
923 * create a linear mapping for the memory like arch_add_memory()
924 * would do.
d3df0a42
JG
925 *
926 * For device public memory, which is accesible by the CPU, we do
927 * want the linear mapping and thus use arch_add_memory().
4ef589dc 928 */
d3df0a42
JG
929 if (devmem->pagemap.type == MEMORY_DEVICE_PUBLIC)
930 ret = arch_add_memory(nid, align_start, align_size, false);
931 else
932 ret = add_pages(nid, align_start >> PAGE_SHIFT,
933 align_size >> PAGE_SHIFT, false);
4ef589dc
JG
934 if (ret) {
935 mem_hotplug_done();
936 goto error_add_memory;
937 }
938 move_pfn_range_to_zone(&NODE_DATA(nid)->node_zones[ZONE_DEVICE],
939 align_start >> PAGE_SHIFT,
940 align_size >> PAGE_SHIFT);
941 mem_hotplug_done();
942
943 for (pfn = devmem->pfn_first; pfn < devmem->pfn_last; pfn++) {
944 struct page *page = pfn_to_page(pfn);
945
946 page->pgmap = &devmem->pagemap;
947 }
948 return 0;
949
950error_add_memory:
951 untrack_pfn(NULL, PHYS_PFN(align_start), align_size);
952error_radix:
953 hmm_devmem_radix_release(devmem->resource);
954error:
955 return ret;
956}
957
4ef589dc
JG
958/*
959 * hmm_devmem_add() - hotplug ZONE_DEVICE memory for device memory
960 *
961 * @ops: memory event device driver callback (see struct hmm_devmem_ops)
962 * @device: device struct to bind the resource too
963 * @size: size in bytes of the device memory to add
964 * Returns: pointer to new hmm_devmem struct ERR_PTR otherwise
965 *
966 * This function first finds an empty range of physical address big enough to
967 * contain the new resource, and then hotplugs it as ZONE_DEVICE memory, which
968 * in turn allocates struct pages. It does not do anything beyond that; all
969 * events affecting the memory will go through the various callbacks provided
970 * by hmm_devmem_ops struct.
971 *
972 * Device driver should call this function during device initialization and
973 * is then responsible of memory management. HMM only provides helpers.
974 */
975struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops,
976 struct device *device,
977 unsigned long size)
978{
979 struct hmm_devmem *devmem;
980 resource_size_t addr;
981 int ret;
982
983 static_branch_enable(&device_private_key);
984
483cb5e3 985 devmem = devm_kzalloc(device, sizeof(*devmem), GFP_KERNEL);
4ef589dc
JG
986 if (!devmem)
987 return ERR_PTR(-ENOMEM);
988
989 init_completion(&devmem->completion);
990 devmem->pfn_first = -1UL;
991 devmem->pfn_last = -1UL;
992 devmem->resource = NULL;
993 devmem->device = device;
994 devmem->ops = ops;
995
996 ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release,
997 0, GFP_KERNEL);
998 if (ret)
483cb5e3 999 return ERR_PTR(ret);
4ef589dc 1000
483cb5e3 1001 ret = devm_add_action_or_reset(device, hmm_devmem_ref_exit, &devmem->ref);
4ef589dc 1002 if (ret)
483cb5e3 1003 return ERR_PTR(ret);
4ef589dc
JG
1004
1005 size = ALIGN(size, PA_SECTION_SIZE);
1006 addr = min((unsigned long)iomem_resource.end,
1007 (1UL << MAX_PHYSMEM_BITS) - 1);
1008 addr = addr - size + 1UL;
1009
1010 /*
1011 * FIXME add a new helper to quickly walk resource tree and find free
1012 * range
1013 *
1014 * FIXME what about ioport_resource resource ?
1015 */
1016 for (; addr > size && addr >= iomem_resource.start; addr -= size) {
1017 ret = region_intersects(addr, size, 0, IORES_DESC_NONE);
1018 if (ret != REGION_DISJOINT)
1019 continue;
1020
1021 devmem->resource = devm_request_mem_region(device, addr, size,
1022 dev_name(device));
483cb5e3
DW
1023 if (!devmem->resource)
1024 return ERR_PTR(-ENOMEM);
4ef589dc
JG
1025 break;
1026 }
483cb5e3
DW
1027 if (!devmem->resource)
1028 return ERR_PTR(-ERANGE);
4ef589dc
JG
1029
1030 devmem->resource->desc = IORES_DESC_DEVICE_PRIVATE_MEMORY;
1031 devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
1032 devmem->pfn_last = devmem->pfn_first +
1033 (resource_size(devmem->resource) >> PAGE_SHIFT);
1034
1035 ret = hmm_devmem_pages_create(devmem);
1036 if (ret)
483cb5e3 1037 return ERR_PTR(ret);
4ef589dc 1038
483cb5e3
DW
1039 ret = devm_add_action_or_reset(device, hmm_devmem_release, devmem);
1040 if (ret)
4ef589dc 1041 return ERR_PTR(ret);
4ef589dc
JG
1042
1043 return devmem;
4ef589dc 1044}
6a9ce13c 1045EXPORT_SYMBOL_GPL(hmm_devmem_add);
4ef589dc 1046
d3df0a42
JG
1047struct hmm_devmem *hmm_devmem_add_resource(const struct hmm_devmem_ops *ops,
1048 struct device *device,
1049 struct resource *res)
1050{
1051 struct hmm_devmem *devmem;
1052 int ret;
1053
1054 if (res->desc != IORES_DESC_DEVICE_PUBLIC_MEMORY)
1055 return ERR_PTR(-EINVAL);
1056
1057 static_branch_enable(&device_private_key);
1058
483cb5e3 1059 devmem = devm_kzalloc(device, sizeof(*devmem), GFP_KERNEL);
d3df0a42
JG
1060 if (!devmem)
1061 return ERR_PTR(-ENOMEM);
1062
1063 init_completion(&devmem->completion);
1064 devmem->pfn_first = -1UL;
1065 devmem->pfn_last = -1UL;
1066 devmem->resource = res;
1067 devmem->device = device;
1068 devmem->ops = ops;
1069
1070 ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release,
1071 0, GFP_KERNEL);
1072 if (ret)
483cb5e3 1073 return ERR_PTR(ret);
d3df0a42 1074
483cb5e3
DW
1075 ret = devm_add_action_or_reset(device, hmm_devmem_ref_exit,
1076 &devmem->ref);
d3df0a42 1077 if (ret)
483cb5e3 1078 return ERR_PTR(ret);
d3df0a42
JG
1079
1080 devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
1081 devmem->pfn_last = devmem->pfn_first +
1082 (resource_size(devmem->resource) >> PAGE_SHIFT);
1083
1084 ret = hmm_devmem_pages_create(devmem);
1085 if (ret)
483cb5e3 1086 return ERR_PTR(ret);
d3df0a42 1087
483cb5e3
DW
1088 ret = devm_add_action_or_reset(device, hmm_devmem_release, devmem);
1089 if (ret)
1090 return ERR_PTR(ret);
d3df0a42 1091
483cb5e3
DW
1092 ret = devm_add_action_or_reset(device, hmm_devmem_ref_kill,
1093 &devmem->ref);
1094 if (ret)
d3df0a42 1095 return ERR_PTR(ret);
d3df0a42
JG
1096
1097 return devmem;
d3df0a42 1098}
6a9ce13c 1099EXPORT_SYMBOL_GPL(hmm_devmem_add_resource);
d3df0a42 1100
858b54da
JG
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);
df6ad698 1180#endif /* CONFIG_DEVICE_PRIVATE || CONFIG_DEVICE_PUBLIC */