[RAMEN9610-21208]coredump: fix race condition between mmget_not_zero()/get_task_mm...
[GitHub/MotorolaMobilityLLC/kernel-slsi.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{
28cb085f
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
74eee180
JG
395 if (write_fault && !pmd_write(pmd))
396 return hmm_vma_walk_clear(start, end, walk);
397
da4c3c73
JG
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;
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
460 if (write_fault && !pte_write(pte))
461 goto fault;
462
da4c3c73
JG
463 pfns[i] = hmm_pfn_t_from_pfn(pte_pfn(pte)) | flag;
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
797 devmem->ops->free(devmem, page);
798}
799
800static DEFINE_MUTEX(hmm_devmem_lock);
801static RADIX_TREE(hmm_devmem_radix, GFP_KERNEL);
802
803static void hmm_devmem_radix_release(struct resource *resource)
804{
805 resource_size_t key, align_start, align_size, align_end;
806
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;
810
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);
817}
818
465c5cf0 819static void hmm_devmem_release(void *data)
4ef589dc
JG
820{
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;
826
4ef589dc
JG
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;
830
831 page = pfn_to_page(start_pfn);
832 zone = page_zone(page);
833
834 mem_hotplug_begin();
d3df0a42
JG
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);
4ef589dc
JG
840 mem_hotplug_done();
841
842 hmm_devmem_radix_release(resource);
843}
844
845static struct hmm_devmem *hmm_devmem_find(resource_size_t phys)
846{
847 WARN_ON_ONCE(!rcu_read_lock_held());
848
849 return radix_tree_lookup(&hmm_devmem_radix, phys >> PA_SECTION_SHIFT);
850}
851
852static int hmm_devmem_pages_create(struct hmm_devmem *devmem)
853{
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;
858
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;
863
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;
871 }
872 if (is_ram == REGION_INTERSECTS)
873 return -ENXIO;
874
d3df0a42
JG
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;
879
4ef589dc
JG
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;
886
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;
891
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;
901 }
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;
909 }
910 }
911 mutex_unlock(&hmm_devmem_lock);
912
913 nid = dev_to_node(device);
914 if (nid < 0)
915 nid = numa_mem_id();
916
917 mem_hotplug_begin();
918 /*
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.
d3df0a42
JG
924 *
925 * For device public memory, which is accesible by the CPU, we do
926 * want the linear mapping and thus use arch_add_memory().
4ef589dc 927 */
d3df0a42
JG
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);
4ef589dc
JG
933 if (ret) {
934 mem_hotplug_done();
935 goto error_add_memory;
936 }
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();
941
942 for (pfn = devmem->pfn_first; pfn < devmem->pfn_last; pfn++) {
943 struct page *page = pfn_to_page(pfn);
944
945 page->pgmap = &devmem->pagemap;
946 }
947 return 0;
948
949error_add_memory:
950 untrack_pfn(NULL, PHYS_PFN(align_start), align_size);
951error_radix:
952 hmm_devmem_radix_release(devmem->resource);
953error:
954 return ret;
955}
956
4ef589dc
JG
957/*
958 * hmm_devmem_add() - hotplug ZONE_DEVICE memory for device memory
959 *
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
964 *
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.
970 *
971 * Device driver should call this function during device initialization and
972 * is then responsible of memory management. HMM only provides helpers.
973 */
974struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops,
975 struct device *device,
976 unsigned long size)
977{
978 struct hmm_devmem *devmem;
979 resource_size_t addr;
980 int ret;
981
982 static_branch_enable(&device_private_key);
983
465c5cf0 984 devmem = devm_kzalloc(device, sizeof(*devmem), GFP_KERNEL);
4ef589dc
JG
985 if (!devmem)
986 return ERR_PTR(-ENOMEM);
987
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;
994
995 ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release,
996 0, GFP_KERNEL);
997 if (ret)
465c5cf0 998 return ERR_PTR(ret);
4ef589dc 999
465c5cf0 1000 ret = devm_add_action_or_reset(device, hmm_devmem_ref_exit, &devmem->ref);
4ef589dc 1001 if (ret)
465c5cf0 1002 return ERR_PTR(ret);
4ef589dc
JG
1003
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;
1008
1009 /*
1010 * FIXME add a new helper to quickly walk resource tree and find free
1011 * range
1012 *
1013 * FIXME what about ioport_resource resource ?
1014 */
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;
1019
1020 devmem->resource = devm_request_mem_region(device, addr, size,
1021 dev_name(device));
465c5cf0
DW
1022 if (!devmem->resource)
1023 return ERR_PTR(-ENOMEM);
4ef589dc
JG
1024 break;
1025 }
465c5cf0
DW
1026 if (!devmem->resource)
1027 return ERR_PTR(-ERANGE);
4ef589dc
JG
1028
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);
1033
1034 ret = hmm_devmem_pages_create(devmem);
1035 if (ret)
465c5cf0 1036 return ERR_PTR(ret);
4ef589dc 1037
465c5cf0
DW
1038 ret = devm_add_action_or_reset(device, hmm_devmem_release, devmem);
1039 if (ret)
4ef589dc 1040 return ERR_PTR(ret);
4ef589dc
JG
1041
1042 return devmem;
4ef589dc 1043}
c5a2c79d 1044EXPORT_SYMBOL_GPL(hmm_devmem_add);
4ef589dc 1045
d3df0a42
JG
1046struct hmm_devmem *hmm_devmem_add_resource(const struct hmm_devmem_ops *ops,
1047 struct device *device,
1048 struct resource *res)
1049{
1050 struct hmm_devmem *devmem;
1051 int ret;
1052
1053 if (res->desc != IORES_DESC_DEVICE_PUBLIC_MEMORY)
1054 return ERR_PTR(-EINVAL);
1055
1056 static_branch_enable(&device_private_key);
1057
465c5cf0 1058 devmem = devm_kzalloc(device, sizeof(*devmem), GFP_KERNEL);
d3df0a42
JG
1059 if (!devmem)
1060 return ERR_PTR(-ENOMEM);
1061
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;
1068
1069 ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release,
1070 0, GFP_KERNEL);
1071 if (ret)
465c5cf0 1072 return ERR_PTR(ret);
d3df0a42 1073
465c5cf0
DW
1074 ret = devm_add_action_or_reset(device, hmm_devmem_ref_exit,
1075 &devmem->ref);
d3df0a42 1076 if (ret)
465c5cf0 1077 return ERR_PTR(ret);
d3df0a42
JG
1078
1079 devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
1080 devmem->pfn_last = devmem->pfn_first +
1081 (resource_size(devmem->resource) >> PAGE_SHIFT);
1082
1083 ret = hmm_devmem_pages_create(devmem);
1084 if (ret)
465c5cf0 1085 return ERR_PTR(ret);
d3df0a42 1086
465c5cf0
DW
1087 ret = devm_add_action_or_reset(device, hmm_devmem_release, devmem);
1088 if (ret)
1089 return ERR_PTR(ret);
d3df0a42 1090
465c5cf0
DW
1091 ret = devm_add_action_or_reset(device, hmm_devmem_ref_kill,
1092 &devmem->ref);
1093 if (ret)
d3df0a42 1094 return ERR_PTR(ret);
d3df0a42
JG
1095
1096 return devmem;
d3df0a42 1097}
c5a2c79d 1098EXPORT_SYMBOL_GPL(hmm_devmem_add_resource);
d3df0a42 1099
858b54da
JG
1100/*
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.
1104 */
1105#define HMM_DEVICE_MAX 256
1106
1107static DECLARE_BITMAP(hmm_device_mask, HMM_DEVICE_MAX);
1108static DEFINE_SPINLOCK(hmm_device_lock);
1109static struct class *hmm_device_class;
1110static dev_t hmm_device_devt;
1111
1112static void hmm_device_release(struct device *device)
1113{
1114 struct hmm_device *hmm_device;
1115
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);
1120
1121 kfree(hmm_device);
1122}
1123
1124struct hmm_device *hmm_device_new(void *drvdata)
1125{
1126 struct hmm_device *hmm_device;
1127
1128 hmm_device = kzalloc(sizeof(*hmm_device), GFP_KERNEL);
1129 if (!hmm_device)
1130 return ERR_PTR(-ENOMEM);
1131
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);
1138 }
1139 set_bit(hmm_device->minor, hmm_device_mask);
1140 spin_unlock(&hmm_device_lock);
1141
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);
1149
1150 return hmm_device;
1151}
1152EXPORT_SYMBOL(hmm_device_new);
1153
1154void hmm_device_put(struct hmm_device *hmm_device)
1155{
1156 put_device(&hmm_device->device);
1157}
1158EXPORT_SYMBOL(hmm_device_put);
1159
1160static int __init hmm_init(void)
1161{
1162 int ret;
1163
1164 ret = alloc_chrdev_region(&hmm_device_devt, 0,
1165 HMM_DEVICE_MAX,
1166 "hmm_device");
1167 if (ret)
1168 return ret;
1169
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);
1174 }
1175 return 0;
1176}
1177
1178device_initcall(hmm_init);
df6ad698 1179#endif /* CONFIG_DEVICE_PRIVATE || CONFIG_DEVICE_PUBLIC */