mm/hmm: add default fault flags to avoid the need to pre-fill pfns arrays
[linux-block.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 *
f813f219 14 * Authors: Jérôme Glisse <jglisse@redhat.com>
133ff0ea
JG
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 IS_ENABLED(CONFIG_HMM_MIRROR)
c0b12405
JG
39static const struct mmu_notifier_ops hmm_mmu_notifier_ops;
40
704f3f2c
JG
41static inline struct hmm *mm_get_hmm(struct mm_struct *mm)
42{
43 struct hmm *hmm = READ_ONCE(mm->hmm);
44
45 if (hmm && kref_get_unless_zero(&hmm->kref))
46 return hmm;
47
48 return NULL;
49}
50
51/**
52 * hmm_get_or_create - register HMM against an mm (HMM internal)
133ff0ea
JG
53 *
54 * @mm: mm struct to attach to
704f3f2c
JG
55 * Returns: returns an HMM object, either by referencing the existing
56 * (per-process) object, or by creating a new one.
133ff0ea 57 *
704f3f2c
JG
58 * This is not intended to be used directly by device drivers. If mm already
59 * has an HMM struct then it get a reference on it and returns it. Otherwise
60 * it allocates an HMM struct, initializes it, associate it with the mm and
61 * returns it.
133ff0ea 62 */
704f3f2c 63static struct hmm *hmm_get_or_create(struct mm_struct *mm)
133ff0ea 64{
704f3f2c 65 struct hmm *hmm = mm_get_hmm(mm);
c0b12405 66 bool cleanup = false;
133ff0ea 67
c0b12405
JG
68 if (hmm)
69 return hmm;
70
71 hmm = kmalloc(sizeof(*hmm), GFP_KERNEL);
72 if (!hmm)
73 return NULL;
a3e0d41c 74 init_waitqueue_head(&hmm->wq);
c0b12405
JG
75 INIT_LIST_HEAD(&hmm->mirrors);
76 init_rwsem(&hmm->mirrors_sem);
c0b12405 77 hmm->mmu_notifier.ops = NULL;
da4c3c73 78 INIT_LIST_HEAD(&hmm->ranges);
a3e0d41c 79 mutex_init(&hmm->lock);
704f3f2c 80 kref_init(&hmm->kref);
a3e0d41c
JG
81 hmm->notifiers = 0;
82 hmm->dead = false;
c0b12405
JG
83 hmm->mm = mm;
84
c0b12405
JG
85 spin_lock(&mm->page_table_lock);
86 if (!mm->hmm)
87 mm->hmm = hmm;
88 else
89 cleanup = true;
90 spin_unlock(&mm->page_table_lock);
91
86a2d598
RC
92 if (cleanup)
93 goto error;
94
95 /*
96 * We should only get here if hold the mmap_sem in write mode ie on
97 * registration of first mirror through hmm_mirror_register()
98 */
99 hmm->mmu_notifier.ops = &hmm_mmu_notifier_ops;
100 if (__mmu_notifier_register(&hmm->mmu_notifier, mm))
101 goto error_mm;
c0b12405 102
704f3f2c 103 return hmm;
86a2d598
RC
104
105error_mm:
106 spin_lock(&mm->page_table_lock);
107 if (mm->hmm == hmm)
108 mm->hmm = NULL;
109 spin_unlock(&mm->page_table_lock);
110error:
111 kfree(hmm);
112 return NULL;
133ff0ea
JG
113}
114
704f3f2c
JG
115static void hmm_free(struct kref *kref)
116{
117 struct hmm *hmm = container_of(kref, struct hmm, kref);
118 struct mm_struct *mm = hmm->mm;
119
120 mmu_notifier_unregister_no_release(&hmm->mmu_notifier, mm);
121
122 spin_lock(&mm->page_table_lock);
123 if (mm->hmm == hmm)
124 mm->hmm = NULL;
125 spin_unlock(&mm->page_table_lock);
126
127 kfree(hmm);
128}
129
130static inline void hmm_put(struct hmm *hmm)
131{
132 kref_put(&hmm->kref, hmm_free);
133}
134
133ff0ea
JG
135void hmm_mm_destroy(struct mm_struct *mm)
136{
704f3f2c
JG
137 struct hmm *hmm;
138
139 spin_lock(&mm->page_table_lock);
140 hmm = mm_get_hmm(mm);
141 mm->hmm = NULL;
142 if (hmm) {
143 hmm->mm = NULL;
a3e0d41c 144 hmm->dead = true;
704f3f2c
JG
145 spin_unlock(&mm->page_table_lock);
146 hmm_put(hmm);
147 return;
148 }
149
150 spin_unlock(&mm->page_table_lock);
133ff0ea 151}
c0b12405 152
a3e0d41c 153static void hmm_release(struct mmu_notifier *mn, struct mm_struct *mm)
c0b12405 154{
a3e0d41c 155 struct hmm *hmm = mm_get_hmm(mm);
c0b12405 156 struct hmm_mirror *mirror;
da4c3c73
JG
157 struct hmm_range *range;
158
a3e0d41c
JG
159 /* Report this HMM as dying. */
160 hmm->dead = true;
da4c3c73 161
a3e0d41c
JG
162 /* Wake-up everyone waiting on any range. */
163 mutex_lock(&hmm->lock);
164 list_for_each_entry(range, &hmm->ranges, list) {
da4c3c73 165 range->valid = false;
da4c3c73 166 }
a3e0d41c
JG
167 wake_up_all(&hmm->wq);
168 mutex_unlock(&hmm->lock);
e1401513
RC
169
170 down_write(&hmm->mirrors_sem);
171 mirror = list_first_entry_or_null(&hmm->mirrors, struct hmm_mirror,
172 list);
173 while (mirror) {
174 list_del_init(&mirror->list);
175 if (mirror->ops->release) {
176 /*
177 * Drop mirrors_sem so callback can wait on any pending
178 * work that might itself trigger mmu_notifier callback
179 * and thus would deadlock with us.
180 */
181 up_write(&hmm->mirrors_sem);
182 mirror->ops->release(mirror);
183 down_write(&hmm->mirrors_sem);
184 }
185 mirror = list_first_entry_or_null(&hmm->mirrors,
186 struct hmm_mirror, list);
187 }
188 up_write(&hmm->mirrors_sem);
704f3f2c
JG
189
190 hmm_put(hmm);
e1401513
RC
191}
192
93065ac7 193static int hmm_invalidate_range_start(struct mmu_notifier *mn,
a3e0d41c 194 const struct mmu_notifier_range *nrange)
c0b12405 195{
a3e0d41c
JG
196 struct hmm *hmm = mm_get_hmm(nrange->mm);
197 struct hmm_mirror *mirror;
ec131b2d 198 struct hmm_update update;
a3e0d41c
JG
199 struct hmm_range *range;
200 int ret = 0;
c0b12405
JG
201
202 VM_BUG_ON(!hmm);
203
a3e0d41c
JG
204 update.start = nrange->start;
205 update.end = nrange->end;
ec131b2d 206 update.event = HMM_UPDATE_INVALIDATE;
a3e0d41c
JG
207 update.blockable = nrange->blockable;
208
209 if (nrange->blockable)
210 mutex_lock(&hmm->lock);
211 else if (!mutex_trylock(&hmm->lock)) {
212 ret = -EAGAIN;
213 goto out;
214 }
215 hmm->notifiers++;
216 list_for_each_entry(range, &hmm->ranges, list) {
217 if (update.end < range->start || update.start >= range->end)
218 continue;
219
220 range->valid = false;
221 }
222 mutex_unlock(&hmm->lock);
223
224 if (nrange->blockable)
225 down_read(&hmm->mirrors_sem);
226 else if (!down_read_trylock(&hmm->mirrors_sem)) {
227 ret = -EAGAIN;
228 goto out;
229 }
230 list_for_each_entry(mirror, &hmm->mirrors, list) {
231 int ret;
232
233 ret = mirror->ops->sync_cpu_device_pagetables(mirror, &update);
234 if (!update.blockable && ret == -EAGAIN) {
235 up_read(&hmm->mirrors_sem);
236 ret = -EAGAIN;
237 goto out;
238 }
239 }
240 up_read(&hmm->mirrors_sem);
241
242out:
704f3f2c
JG
243 hmm_put(hmm);
244 return ret;
c0b12405
JG
245}
246
247static void hmm_invalidate_range_end(struct mmu_notifier *mn,
a3e0d41c 248 const struct mmu_notifier_range *nrange)
c0b12405 249{
a3e0d41c 250 struct hmm *hmm = mm_get_hmm(nrange->mm);
c0b12405
JG
251
252 VM_BUG_ON(!hmm);
253
a3e0d41c
JG
254 mutex_lock(&hmm->lock);
255 hmm->notifiers--;
256 if (!hmm->notifiers) {
257 struct hmm_range *range;
258
259 list_for_each_entry(range, &hmm->ranges, list) {
260 if (range->valid)
261 continue;
262 range->valid = true;
263 }
264 wake_up_all(&hmm->wq);
265 }
266 mutex_unlock(&hmm->lock);
267
704f3f2c 268 hmm_put(hmm);
c0b12405
JG
269}
270
271static const struct mmu_notifier_ops hmm_mmu_notifier_ops = {
e1401513 272 .release = hmm_release,
c0b12405
JG
273 .invalidate_range_start = hmm_invalidate_range_start,
274 .invalidate_range_end = hmm_invalidate_range_end,
275};
276
277/*
278 * hmm_mirror_register() - register a mirror against an mm
279 *
280 * @mirror: new mirror struct to register
281 * @mm: mm to register against
282 *
283 * To start mirroring a process address space, the device driver must register
284 * an HMM mirror struct.
285 *
286 * THE mm->mmap_sem MUST BE HELD IN WRITE MODE !
287 */
288int hmm_mirror_register(struct hmm_mirror *mirror, struct mm_struct *mm)
289{
290 /* Sanity check */
291 if (!mm || !mirror || !mirror->ops)
292 return -EINVAL;
293
704f3f2c 294 mirror->hmm = hmm_get_or_create(mm);
c0b12405
JG
295 if (!mirror->hmm)
296 return -ENOMEM;
297
298 down_write(&mirror->hmm->mirrors_sem);
704f3f2c
JG
299 list_add(&mirror->list, &mirror->hmm->mirrors);
300 up_write(&mirror->hmm->mirrors_sem);
c0b12405
JG
301
302 return 0;
303}
304EXPORT_SYMBOL(hmm_mirror_register);
305
306/*
307 * hmm_mirror_unregister() - unregister a mirror
308 *
309 * @mirror: new mirror struct to register
310 *
311 * Stop mirroring a process address space, and cleanup.
312 */
313void hmm_mirror_unregister(struct hmm_mirror *mirror)
314{
704f3f2c 315 struct hmm *hmm = READ_ONCE(mirror->hmm);
c01cbba2 316
704f3f2c 317 if (hmm == NULL)
c01cbba2 318 return;
c0b12405
JG
319
320 down_write(&hmm->mirrors_sem);
e1401513 321 list_del_init(&mirror->list);
704f3f2c 322 /* To protect us against double unregister ... */
c01cbba2 323 mirror->hmm = NULL;
c0b12405 324 up_write(&hmm->mirrors_sem);
c01cbba2 325
704f3f2c 326 hmm_put(hmm);
c0b12405
JG
327}
328EXPORT_SYMBOL(hmm_mirror_unregister);
da4c3c73 329
74eee180
JG
330struct hmm_vma_walk {
331 struct hmm_range *range;
332 unsigned long last;
333 bool fault;
334 bool block;
74eee180
JG
335};
336
2aee09d8
JG
337static int hmm_vma_do_fault(struct mm_walk *walk, unsigned long addr,
338 bool write_fault, uint64_t *pfn)
74eee180
JG
339{
340 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_REMOTE;
341 struct hmm_vma_walk *hmm_vma_walk = walk->private;
f88a1e90 342 struct hmm_range *range = hmm_vma_walk->range;
74eee180 343 struct vm_area_struct *vma = walk->vma;
50a7ca3c 344 vm_fault_t ret;
74eee180
JG
345
346 flags |= hmm_vma_walk->block ? 0 : FAULT_FLAG_ALLOW_RETRY;
2aee09d8 347 flags |= write_fault ? FAULT_FLAG_WRITE : 0;
50a7ca3c
SJ
348 ret = handle_mm_fault(vma, addr, flags);
349 if (ret & VM_FAULT_RETRY)
73231612 350 return -EAGAIN;
50a7ca3c 351 if (ret & VM_FAULT_ERROR) {
f88a1e90 352 *pfn = range->values[HMM_PFN_ERROR];
74eee180
JG
353 return -EFAULT;
354 }
355
73231612 356 return -EBUSY;
74eee180
JG
357}
358
da4c3c73
JG
359static int hmm_pfns_bad(unsigned long addr,
360 unsigned long end,
361 struct mm_walk *walk)
362{
c719547f
JG
363 struct hmm_vma_walk *hmm_vma_walk = walk->private;
364 struct hmm_range *range = hmm_vma_walk->range;
ff05c0c6 365 uint64_t *pfns = range->pfns;
da4c3c73
JG
366 unsigned long i;
367
368 i = (addr - range->start) >> PAGE_SHIFT;
369 for (; addr < end; addr += PAGE_SIZE, i++)
f88a1e90 370 pfns[i] = range->values[HMM_PFN_ERROR];
da4c3c73
JG
371
372 return 0;
373}
374
5504ed29
JG
375/*
376 * hmm_vma_walk_hole() - handle a range lacking valid pmd or pte(s)
377 * @start: range virtual start address (inclusive)
378 * @end: range virtual end address (exclusive)
2aee09d8
JG
379 * @fault: should we fault or not ?
380 * @write_fault: write fault ?
5504ed29 381 * @walk: mm_walk structure
73231612 382 * Returns: 0 on success, -EBUSY after page fault, or page fault error
5504ed29
JG
383 *
384 * This function will be called whenever pmd_none() or pte_none() returns true,
385 * or whenever there is no page directory covering the virtual address range.
386 */
2aee09d8
JG
387static int hmm_vma_walk_hole_(unsigned long addr, unsigned long end,
388 bool fault, bool write_fault,
389 struct mm_walk *walk)
da4c3c73 390{
74eee180
JG
391 struct hmm_vma_walk *hmm_vma_walk = walk->private;
392 struct hmm_range *range = hmm_vma_walk->range;
ff05c0c6 393 uint64_t *pfns = range->pfns;
da4c3c73
JG
394 unsigned long i;
395
74eee180 396 hmm_vma_walk->last = addr;
da4c3c73 397 i = (addr - range->start) >> PAGE_SHIFT;
74eee180 398 for (; addr < end; addr += PAGE_SIZE, i++) {
f88a1e90 399 pfns[i] = range->values[HMM_PFN_NONE];
2aee09d8 400 if (fault || write_fault) {
74eee180 401 int ret;
da4c3c73 402
2aee09d8
JG
403 ret = hmm_vma_do_fault(walk, addr, write_fault,
404 &pfns[i]);
73231612 405 if (ret != -EBUSY)
74eee180
JG
406 return ret;
407 }
408 }
409
73231612 410 return (fault || write_fault) ? -EBUSY : 0;
2aee09d8
JG
411}
412
413static inline void hmm_pte_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
414 uint64_t pfns, uint64_t cpu_flags,
415 bool *fault, bool *write_fault)
416{
f88a1e90
JG
417 struct hmm_range *range = hmm_vma_walk->range;
418
2aee09d8
JG
419 if (!hmm_vma_walk->fault)
420 return;
421
023a019a
JG
422 /*
423 * So we not only consider the individual per page request we also
424 * consider the default flags requested for the range. The API can
425 * be use in 2 fashions. The first one where the HMM user coalesce
426 * multiple page fault into one request and set flags per pfns for
427 * of those faults. The second one where the HMM user want to pre-
428 * fault a range with specific flags. For the latter one it is a
429 * waste to have the user pre-fill the pfn arrays with a default
430 * flags value.
431 */
432 pfns = (pfns & range->pfn_flags_mask) | range->default_flags;
433
2aee09d8 434 /* We aren't ask to do anything ... */
f88a1e90 435 if (!(pfns & range->flags[HMM_PFN_VALID]))
2aee09d8 436 return;
f88a1e90
JG
437 /* If this is device memory than only fault if explicitly requested */
438 if ((cpu_flags & range->flags[HMM_PFN_DEVICE_PRIVATE])) {
439 /* Do we fault on device memory ? */
440 if (pfns & range->flags[HMM_PFN_DEVICE_PRIVATE]) {
441 *write_fault = pfns & range->flags[HMM_PFN_WRITE];
442 *fault = true;
443 }
2aee09d8
JG
444 return;
445 }
f88a1e90
JG
446
447 /* If CPU page table is not valid then we need to fault */
448 *fault = !(cpu_flags & range->flags[HMM_PFN_VALID]);
449 /* Need to write fault ? */
450 if ((pfns & range->flags[HMM_PFN_WRITE]) &&
451 !(cpu_flags & range->flags[HMM_PFN_WRITE])) {
452 *write_fault = true;
2aee09d8
JG
453 *fault = true;
454 }
455}
456
457static void hmm_range_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
458 const uint64_t *pfns, unsigned long npages,
459 uint64_t cpu_flags, bool *fault,
460 bool *write_fault)
461{
462 unsigned long i;
463
464 if (!hmm_vma_walk->fault) {
465 *fault = *write_fault = false;
466 return;
467 }
468
a3e0d41c 469 *fault = *write_fault = false;
2aee09d8
JG
470 for (i = 0; i < npages; ++i) {
471 hmm_pte_need_fault(hmm_vma_walk, pfns[i], cpu_flags,
472 fault, write_fault);
a3e0d41c 473 if ((*write_fault))
2aee09d8
JG
474 return;
475 }
476}
477
478static int hmm_vma_walk_hole(unsigned long addr, unsigned long end,
479 struct mm_walk *walk)
480{
481 struct hmm_vma_walk *hmm_vma_walk = walk->private;
482 struct hmm_range *range = hmm_vma_walk->range;
483 bool fault, write_fault;
484 unsigned long i, npages;
485 uint64_t *pfns;
486
487 i = (addr - range->start) >> PAGE_SHIFT;
488 npages = (end - addr) >> PAGE_SHIFT;
489 pfns = &range->pfns[i];
490 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
491 0, &fault, &write_fault);
492 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
493}
494
f88a1e90 495static inline uint64_t pmd_to_hmm_pfn_flags(struct hmm_range *range, pmd_t pmd)
2aee09d8
JG
496{
497 if (pmd_protnone(pmd))
498 return 0;
f88a1e90
JG
499 return pmd_write(pmd) ? range->flags[HMM_PFN_VALID] |
500 range->flags[HMM_PFN_WRITE] :
501 range->flags[HMM_PFN_VALID];
da4c3c73
JG
502}
503
53f5c3f4
JG
504static int hmm_vma_handle_pmd(struct mm_walk *walk,
505 unsigned long addr,
506 unsigned long end,
507 uint64_t *pfns,
508 pmd_t pmd)
509{
510 struct hmm_vma_walk *hmm_vma_walk = walk->private;
f88a1e90 511 struct hmm_range *range = hmm_vma_walk->range;
2aee09d8 512 unsigned long pfn, npages, i;
2aee09d8 513 bool fault, write_fault;
f88a1e90 514 uint64_t cpu_flags;
53f5c3f4 515
2aee09d8 516 npages = (end - addr) >> PAGE_SHIFT;
f88a1e90 517 cpu_flags = pmd_to_hmm_pfn_flags(range, pmd);
2aee09d8
JG
518 hmm_range_need_fault(hmm_vma_walk, pfns, npages, cpu_flags,
519 &fault, &write_fault);
53f5c3f4 520
2aee09d8
JG
521 if (pmd_protnone(pmd) || fault || write_fault)
522 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
53f5c3f4
JG
523
524 pfn = pmd_pfn(pmd) + pte_index(addr);
53f5c3f4 525 for (i = 0; addr < end; addr += PAGE_SIZE, i++, pfn++)
f88a1e90 526 pfns[i] = hmm_pfn_from_pfn(range, pfn) | cpu_flags;
53f5c3f4
JG
527 hmm_vma_walk->last = end;
528 return 0;
529}
530
f88a1e90 531static inline uint64_t pte_to_hmm_pfn_flags(struct hmm_range *range, pte_t pte)
2aee09d8
JG
532{
533 if (pte_none(pte) || !pte_present(pte))
534 return 0;
f88a1e90
JG
535 return pte_write(pte) ? range->flags[HMM_PFN_VALID] |
536 range->flags[HMM_PFN_WRITE] :
537 range->flags[HMM_PFN_VALID];
2aee09d8
JG
538}
539
53f5c3f4
JG
540static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,
541 unsigned long end, pmd_t *pmdp, pte_t *ptep,
542 uint64_t *pfn)
543{
544 struct hmm_vma_walk *hmm_vma_walk = walk->private;
f88a1e90 545 struct hmm_range *range = hmm_vma_walk->range;
53f5c3f4 546 struct vm_area_struct *vma = walk->vma;
2aee09d8
JG
547 bool fault, write_fault;
548 uint64_t cpu_flags;
53f5c3f4 549 pte_t pte = *ptep;
f88a1e90 550 uint64_t orig_pfn = *pfn;
53f5c3f4 551
f88a1e90 552 *pfn = range->values[HMM_PFN_NONE];
73231612 553 fault = write_fault = false;
53f5c3f4
JG
554
555 if (pte_none(pte)) {
73231612
JG
556 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, 0,
557 &fault, &write_fault);
2aee09d8 558 if (fault || write_fault)
53f5c3f4
JG
559 goto fault;
560 return 0;
561 }
562
563 if (!pte_present(pte)) {
564 swp_entry_t entry = pte_to_swp_entry(pte);
565
566 if (!non_swap_entry(entry)) {
2aee09d8 567 if (fault || write_fault)
53f5c3f4
JG
568 goto fault;
569 return 0;
570 }
571
572 /*
573 * This is a special swap entry, ignore migration, use
574 * device and report anything else as error.
575 */
576 if (is_device_private_entry(entry)) {
f88a1e90
JG
577 cpu_flags = range->flags[HMM_PFN_VALID] |
578 range->flags[HMM_PFN_DEVICE_PRIVATE];
2aee09d8 579 cpu_flags |= is_write_device_private_entry(entry) ?
f88a1e90
JG
580 range->flags[HMM_PFN_WRITE] : 0;
581 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
582 &fault, &write_fault);
583 if (fault || write_fault)
584 goto fault;
585 *pfn = hmm_pfn_from_pfn(range, swp_offset(entry));
586 *pfn |= cpu_flags;
53f5c3f4
JG
587 return 0;
588 }
589
590 if (is_migration_entry(entry)) {
2aee09d8 591 if (fault || write_fault) {
53f5c3f4
JG
592 pte_unmap(ptep);
593 hmm_vma_walk->last = addr;
594 migration_entry_wait(vma->vm_mm,
2aee09d8 595 pmdp, addr);
73231612 596 return -EBUSY;
53f5c3f4
JG
597 }
598 return 0;
599 }
600
601 /* Report error for everything else */
f88a1e90 602 *pfn = range->values[HMM_PFN_ERROR];
53f5c3f4 603 return -EFAULT;
73231612
JG
604 } else {
605 cpu_flags = pte_to_hmm_pfn_flags(range, pte);
606 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
607 &fault, &write_fault);
53f5c3f4
JG
608 }
609
2aee09d8 610 if (fault || write_fault)
53f5c3f4
JG
611 goto fault;
612
f88a1e90 613 *pfn = hmm_pfn_from_pfn(range, pte_pfn(pte)) | cpu_flags;
53f5c3f4
JG
614 return 0;
615
616fault:
617 pte_unmap(ptep);
618 /* Fault any virtual address we were asked to fault */
2aee09d8 619 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
53f5c3f4
JG
620}
621
da4c3c73
JG
622static int hmm_vma_walk_pmd(pmd_t *pmdp,
623 unsigned long start,
624 unsigned long end,
625 struct mm_walk *walk)
626{
74eee180
JG
627 struct hmm_vma_walk *hmm_vma_walk = walk->private;
628 struct hmm_range *range = hmm_vma_walk->range;
d08faca0 629 struct vm_area_struct *vma = walk->vma;
ff05c0c6 630 uint64_t *pfns = range->pfns;
da4c3c73 631 unsigned long addr = start, i;
da4c3c73 632 pte_t *ptep;
d08faca0 633 pmd_t pmd;
da4c3c73 634
da4c3c73
JG
635
636again:
d08faca0
JG
637 pmd = READ_ONCE(*pmdp);
638 if (pmd_none(pmd))
da4c3c73
JG
639 return hmm_vma_walk_hole(start, end, walk);
640
d08faca0 641 if (pmd_huge(pmd) && (range->vma->vm_flags & VM_HUGETLB))
da4c3c73
JG
642 return hmm_pfns_bad(start, end, walk);
643
d08faca0
JG
644 if (thp_migration_supported() && is_pmd_migration_entry(pmd)) {
645 bool fault, write_fault;
646 unsigned long npages;
647 uint64_t *pfns;
648
649 i = (addr - range->start) >> PAGE_SHIFT;
650 npages = (end - addr) >> PAGE_SHIFT;
651 pfns = &range->pfns[i];
652
653 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
654 0, &fault, &write_fault);
655 if (fault || write_fault) {
656 hmm_vma_walk->last = addr;
657 pmd_migration_entry_wait(vma->vm_mm, pmdp);
73231612 658 return -EBUSY;
d08faca0
JG
659 }
660 return 0;
661 } else if (!pmd_present(pmd))
662 return hmm_pfns_bad(start, end, walk);
da4c3c73 663
d08faca0 664 if (pmd_devmap(pmd) || pmd_trans_huge(pmd)) {
da4c3c73
JG
665 /*
666 * No need to take pmd_lock here, even if some other threads
667 * is splitting the huge pmd we will get that event through
668 * mmu_notifier callback.
669 *
670 * So just read pmd value and check again its a transparent
671 * huge or device mapping one and compute corresponding pfn
672 * values.
673 */
674 pmd = pmd_read_atomic(pmdp);
675 barrier();
676 if (!pmd_devmap(pmd) && !pmd_trans_huge(pmd))
677 goto again;
74eee180 678
d08faca0 679 i = (addr - range->start) >> PAGE_SHIFT;
53f5c3f4 680 return hmm_vma_handle_pmd(walk, addr, end, &pfns[i], pmd);
da4c3c73
JG
681 }
682
d08faca0
JG
683 /*
684 * We have handled all the valid case above ie either none, migration,
685 * huge or transparent huge. At this point either it is a valid pmd
686 * entry pointing to pte directory or it is a bad pmd that will not
687 * recover.
688 */
689 if (pmd_bad(pmd))
da4c3c73
JG
690 return hmm_pfns_bad(start, end, walk);
691
692 ptep = pte_offset_map(pmdp, addr);
d08faca0 693 i = (addr - range->start) >> PAGE_SHIFT;
da4c3c73 694 for (; addr < end; addr += PAGE_SIZE, ptep++, i++) {
53f5c3f4 695 int r;
74eee180 696
53f5c3f4
JG
697 r = hmm_vma_handle_pte(walk, addr, end, pmdp, ptep, &pfns[i]);
698 if (r) {
699 /* hmm_vma_handle_pte() did unmap pte directory */
700 hmm_vma_walk->last = addr;
701 return r;
74eee180 702 }
da4c3c73
JG
703 }
704 pte_unmap(ptep - 1);
705
53f5c3f4 706 hmm_vma_walk->last = addr;
da4c3c73
JG
707 return 0;
708}
709
f88a1e90
JG
710static void hmm_pfns_clear(struct hmm_range *range,
711 uint64_t *pfns,
33cd47dc
JG
712 unsigned long addr,
713 unsigned long end)
714{
715 for (; addr < end; addr += PAGE_SIZE, pfns++)
f88a1e90 716 *pfns = range->values[HMM_PFN_NONE];
33cd47dc
JG
717}
718
855ce7d2
JG
719static void hmm_pfns_special(struct hmm_range *range)
720{
721 unsigned long addr = range->start, i = 0;
722
723 for (; addr < range->end; addr += PAGE_SIZE, i++)
f88a1e90 724 range->pfns[i] = range->values[HMM_PFN_SPECIAL];
855ce7d2
JG
725}
726
da4c3c73 727/*
a3e0d41c 728 * hmm_range_register() - start tracking change to CPU page table over a range
25f23a0c 729 * @range: range
a3e0d41c
JG
730 * @mm: the mm struct for the range of virtual address
731 * @start: start virtual address (inclusive)
732 * @end: end virtual address (exclusive)
733 * Returns 0 on success, -EFAULT if the address space is no longer valid
25f23a0c 734 *
a3e0d41c 735 * Track updates to the CPU page table see include/linux/hmm.h
da4c3c73 736 */
a3e0d41c
JG
737int hmm_range_register(struct hmm_range *range,
738 struct mm_struct *mm,
739 unsigned long start,
740 unsigned long end)
da4c3c73 741{
a3e0d41c
JG
742 range->start = start & PAGE_MASK;
743 range->end = end & PAGE_MASK;
744 range->valid = false;
704f3f2c
JG
745 range->hmm = NULL;
746
a3e0d41c 747 if (range->start >= range->end)
da4c3c73
JG
748 return -EINVAL;
749
a3e0d41c
JG
750 range->start = start;
751 range->end = end;
752
753 range->hmm = hmm_get_or_create(mm);
754 if (!range->hmm)
755 return -EFAULT;
704f3f2c
JG
756
757 /* Check if hmm_mm_destroy() was call. */
a3e0d41c
JG
758 if (range->hmm->mm == NULL || range->hmm->dead) {
759 hmm_put(range->hmm);
760 return -EFAULT;
704f3f2c 761 }
da4c3c73 762
a3e0d41c
JG
763 /* Initialize range to track CPU page table update */
764 mutex_lock(&range->hmm->lock);
855ce7d2 765
a3e0d41c 766 list_add_rcu(&range->list, &range->hmm->ranges);
86586a41 767
704f3f2c 768 /*
a3e0d41c
JG
769 * If there are any concurrent notifiers we have to wait for them for
770 * the range to be valid (see hmm_range_wait_until_valid()).
704f3f2c 771 */
a3e0d41c
JG
772 if (!range->hmm->notifiers)
773 range->valid = true;
774 mutex_unlock(&range->hmm->lock);
775
776 return 0;
da4c3c73 777}
a3e0d41c 778EXPORT_SYMBOL(hmm_range_register);
da4c3c73
JG
779
780/*
a3e0d41c
JG
781 * hmm_range_unregister() - stop tracking change to CPU page table over a range
782 * @range: range
da4c3c73
JG
783 *
784 * Range struct is used to track updates to the CPU page table after a call to
a3e0d41c 785 * hmm_range_register(). See include/linux/hmm.h for how to use it.
da4c3c73 786 */
a3e0d41c 787void hmm_range_unregister(struct hmm_range *range)
da4c3c73 788{
704f3f2c 789 /* Sanity check this really should not happen. */
a3e0d41c
JG
790 if (range->hmm == NULL || range->end <= range->start)
791 return;
da4c3c73 792
a3e0d41c 793 mutex_lock(&range->hmm->lock);
da4c3c73 794 list_del_rcu(&range->list);
a3e0d41c 795 mutex_unlock(&range->hmm->lock);
da4c3c73 796
a3e0d41c
JG
797 /* Drop reference taken by hmm_range_register() */
798 range->valid = false;
704f3f2c
JG
799 hmm_put(range->hmm);
800 range->hmm = NULL;
da4c3c73 801}
a3e0d41c
JG
802EXPORT_SYMBOL(hmm_range_unregister);
803
804/*
805 * hmm_range_snapshot() - snapshot CPU page table for a range
806 * @range: range
807 * Returns: -EINVAL if invalid argument, -ENOMEM out of memory, -EPERM invalid
808 * permission (for instance asking for write and range is read only),
809 * -EAGAIN if you need to retry, -EFAULT invalid (ie either no valid
810 * vma or it is illegal to access that range), number of valid pages
811 * in range->pfns[] (from range start address).
812 *
813 * This snapshots the CPU page table for a range of virtual addresses. Snapshot
814 * validity is tracked by range struct. See in include/linux/hmm.h for example
815 * on how to use.
816 */
817long hmm_range_snapshot(struct hmm_range *range)
818{
819 unsigned long start = range->start, end;
820 struct hmm_vma_walk hmm_vma_walk;
821 struct hmm *hmm = range->hmm;
822 struct vm_area_struct *vma;
823 struct mm_walk mm_walk;
824
825 /* Check if hmm_mm_destroy() was call. */
826 if (hmm->mm == NULL || hmm->dead)
827 return -EFAULT;
828
829 do {
830 /* If range is no longer valid force retry. */
831 if (!range->valid)
832 return -EAGAIN;
833
834 vma = find_vma(hmm->mm, start);
835 if (vma == NULL || (vma->vm_flags & VM_SPECIAL))
836 return -EFAULT;
837
838 /* FIXME support hugetlb fs/dax */
839 if (is_vm_hugetlb_page(vma) || vma_is_dax(vma)) {
840 hmm_pfns_special(range);
841 return -EINVAL;
842 }
843
844 if (!(vma->vm_flags & VM_READ)) {
845 /*
846 * If vma do not allow read access, then assume that it
847 * does not allow write access, either. HMM does not
848 * support architecture that allow write without read.
849 */
850 hmm_pfns_clear(range, range->pfns,
851 range->start, range->end);
852 return -EPERM;
853 }
854
855 range->vma = vma;
856 hmm_vma_walk.last = start;
857 hmm_vma_walk.fault = false;
858 hmm_vma_walk.range = range;
859 mm_walk.private = &hmm_vma_walk;
860 end = min(range->end, vma->vm_end);
861
862 mm_walk.vma = vma;
863 mm_walk.mm = vma->vm_mm;
864 mm_walk.pte_entry = NULL;
865 mm_walk.test_walk = NULL;
866 mm_walk.hugetlb_entry = NULL;
867 mm_walk.pmd_entry = hmm_vma_walk_pmd;
868 mm_walk.pte_hole = hmm_vma_walk_hole;
869
870 walk_page_range(start, end, &mm_walk);
871 start = end;
872 } while (start < range->end);
873
874 return (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
875}
876EXPORT_SYMBOL(hmm_range_snapshot);
74eee180
JG
877
878/*
73231612 879 * hmm_range_fault() - try to fault some address in a virtual address range
08232a45 880 * @range: range being faulted
74eee180 881 * @block: allow blocking on fault (if true it sleeps and do not drop mmap_sem)
73231612
JG
882 * Returns: number of valid pages in range->pfns[] (from range start
883 * address). This may be zero. If the return value is negative,
884 * then one of the following values may be returned:
885 *
886 * -EINVAL invalid arguments or mm or virtual address are in an
887 * invalid vma (ie either hugetlbfs or device file vma).
888 * -ENOMEM: Out of memory.
889 * -EPERM: Invalid permission (for instance asking for write and
890 * range is read only).
891 * -EAGAIN: If you need to retry and mmap_sem was drop. This can only
892 * happens if block argument is false.
893 * -EBUSY: If the the range is being invalidated and you should wait
894 * for invalidation to finish.
895 * -EFAULT: Invalid (ie either no valid vma or it is illegal to access
896 * that range), number of valid pages in range->pfns[] (from
897 * range start address).
74eee180
JG
898 *
899 * This is similar to a regular CPU page fault except that it will not trigger
73231612
JG
900 * any memory migration if the memory being faulted is not accessible by CPUs
901 * and caller does not ask for migration.
74eee180 902 *
ff05c0c6
JG
903 * On error, for one virtual address in the range, the function will mark the
904 * corresponding HMM pfn entry with an error flag.
74eee180 905 */
73231612 906long hmm_range_fault(struct hmm_range *range, bool block)
74eee180 907{
a3e0d41c 908 unsigned long start = range->start, end;
74eee180 909 struct hmm_vma_walk hmm_vma_walk;
a3e0d41c
JG
910 struct hmm *hmm = range->hmm;
911 struct vm_area_struct *vma;
74eee180 912 struct mm_walk mm_walk;
74eee180
JG
913 int ret;
914
a3e0d41c
JG
915 /* Check if hmm_mm_destroy() was call. */
916 if (hmm->mm == NULL || hmm->dead)
917 return -EFAULT;
704f3f2c 918
a3e0d41c
JG
919 do {
920 /* If range is no longer valid force retry. */
921 if (!range->valid) {
922 up_read(&hmm->mm->mmap_sem);
923 return -EAGAIN;
924 }
74eee180 925
a3e0d41c
JG
926 vma = find_vma(hmm->mm, start);
927 if (vma == NULL || (vma->vm_flags & VM_SPECIAL))
928 return -EFAULT;
704f3f2c 929
a3e0d41c
JG
930 /* FIXME support hugetlb fs/dax */
931 if (is_vm_hugetlb_page(vma) || vma_is_dax(vma)) {
932 hmm_pfns_special(range);
933 return -EINVAL;
934 }
855ce7d2 935
a3e0d41c
JG
936 if (!(vma->vm_flags & VM_READ)) {
937 /*
938 * If vma do not allow read access, then assume that it
939 * does not allow write access, either. HMM does not
940 * support architecture that allow write without read.
941 */
942 hmm_pfns_clear(range, range->pfns,
943 range->start, range->end);
944 return -EPERM;
945 }
74eee180 946
a3e0d41c
JG
947 range->vma = vma;
948 hmm_vma_walk.last = start;
949 hmm_vma_walk.fault = true;
950 hmm_vma_walk.block = block;
951 hmm_vma_walk.range = range;
952 mm_walk.private = &hmm_vma_walk;
953 end = min(range->end, vma->vm_end);
954
955 mm_walk.vma = vma;
956 mm_walk.mm = vma->vm_mm;
957 mm_walk.pte_entry = NULL;
958 mm_walk.test_walk = NULL;
959 mm_walk.hugetlb_entry = NULL;
960 mm_walk.pmd_entry = hmm_vma_walk_pmd;
961 mm_walk.pte_hole = hmm_vma_walk_hole;
962
963 do {
964 ret = walk_page_range(start, end, &mm_walk);
965 start = hmm_vma_walk.last;
966
967 /* Keep trying while the range is valid. */
968 } while (ret == -EBUSY && range->valid);
969
970 if (ret) {
971 unsigned long i;
972
973 i = (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
974 hmm_pfns_clear(range, &range->pfns[i],
975 hmm_vma_walk.last, range->end);
976 return ret;
977 }
978 start = end;
74eee180 979
a3e0d41c 980 } while (start < range->end);
704f3f2c 981
73231612 982 return (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
74eee180 983}
73231612 984EXPORT_SYMBOL(hmm_range_fault);
c0b12405 985#endif /* IS_ENABLED(CONFIG_HMM_MIRROR) */
4ef589dc
JG
986
987
df6ad698 988#if IS_ENABLED(CONFIG_DEVICE_PRIVATE) || IS_ENABLED(CONFIG_DEVICE_PUBLIC)
4ef589dc
JG
989struct page *hmm_vma_alloc_locked_page(struct vm_area_struct *vma,
990 unsigned long addr)
991{
992 struct page *page;
993
994 page = alloc_page_vma(GFP_HIGHUSER, vma, addr);
995 if (!page)
996 return NULL;
997 lock_page(page);
998 return page;
999}
1000EXPORT_SYMBOL(hmm_vma_alloc_locked_page);
1001
1002
1003static void hmm_devmem_ref_release(struct percpu_ref *ref)
1004{
1005 struct hmm_devmem *devmem;
1006
1007 devmem = container_of(ref, struct hmm_devmem, ref);
1008 complete(&devmem->completion);
1009}
1010
1011static void hmm_devmem_ref_exit(void *data)
1012{
1013 struct percpu_ref *ref = data;
1014 struct hmm_devmem *devmem;
1015
1016 devmem = container_of(ref, struct hmm_devmem, ref);
bbecd94e 1017 wait_for_completion(&devmem->completion);
4ef589dc 1018 percpu_ref_exit(ref);
4ef589dc
JG
1019}
1020
bbecd94e 1021static void hmm_devmem_ref_kill(struct percpu_ref *ref)
4ef589dc 1022{
4ef589dc 1023 percpu_ref_kill(ref);
4ef589dc
JG
1024}
1025
b57e622e 1026static vm_fault_t hmm_devmem_fault(struct vm_area_struct *vma,
4ef589dc
JG
1027 unsigned long addr,
1028 const struct page *page,
1029 unsigned int flags,
1030 pmd_t *pmdp)
1031{
1032 struct hmm_devmem *devmem = page->pgmap->data;
1033
1034 return devmem->ops->fault(devmem, vma, addr, page, flags, pmdp);
1035}
1036
1037static void hmm_devmem_free(struct page *page, void *data)
1038{
1039 struct hmm_devmem *devmem = data;
1040
2fa147bd
DW
1041 page->mapping = NULL;
1042
4ef589dc
JG
1043 devmem->ops->free(devmem, page);
1044}
1045
4ef589dc
JG
1046/*
1047 * hmm_devmem_add() - hotplug ZONE_DEVICE memory for device memory
1048 *
1049 * @ops: memory event device driver callback (see struct hmm_devmem_ops)
1050 * @device: device struct to bind the resource too
1051 * @size: size in bytes of the device memory to add
1052 * Returns: pointer to new hmm_devmem struct ERR_PTR otherwise
1053 *
1054 * This function first finds an empty range of physical address big enough to
1055 * contain the new resource, and then hotplugs it as ZONE_DEVICE memory, which
1056 * in turn allocates struct pages. It does not do anything beyond that; all
1057 * events affecting the memory will go through the various callbacks provided
1058 * by hmm_devmem_ops struct.
1059 *
1060 * Device driver should call this function during device initialization and
1061 * is then responsible of memory management. HMM only provides helpers.
1062 */
1063struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops,
1064 struct device *device,
1065 unsigned long size)
1066{
1067 struct hmm_devmem *devmem;
1068 resource_size_t addr;
bbecd94e 1069 void *result;
4ef589dc
JG
1070 int ret;
1071
e7638488 1072 dev_pagemap_get_ops();
4ef589dc 1073
58ef15b7 1074 devmem = devm_kzalloc(device, sizeof(*devmem), GFP_KERNEL);
4ef589dc
JG
1075 if (!devmem)
1076 return ERR_PTR(-ENOMEM);
1077
1078 init_completion(&devmem->completion);
1079 devmem->pfn_first = -1UL;
1080 devmem->pfn_last = -1UL;
1081 devmem->resource = NULL;
1082 devmem->device = device;
1083 devmem->ops = ops;
1084
1085 ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release,
1086 0, GFP_KERNEL);
1087 if (ret)
58ef15b7 1088 return ERR_PTR(ret);
4ef589dc 1089
58ef15b7 1090 ret = devm_add_action_or_reset(device, hmm_devmem_ref_exit, &devmem->ref);
4ef589dc 1091 if (ret)
58ef15b7 1092 return ERR_PTR(ret);
4ef589dc
JG
1093
1094 size = ALIGN(size, PA_SECTION_SIZE);
1095 addr = min((unsigned long)iomem_resource.end,
1096 (1UL << MAX_PHYSMEM_BITS) - 1);
1097 addr = addr - size + 1UL;
1098
1099 /*
1100 * FIXME add a new helper to quickly walk resource tree and find free
1101 * range
1102 *
1103 * FIXME what about ioport_resource resource ?
1104 */
1105 for (; addr > size && addr >= iomem_resource.start; addr -= size) {
1106 ret = region_intersects(addr, size, 0, IORES_DESC_NONE);
1107 if (ret != REGION_DISJOINT)
1108 continue;
1109
1110 devmem->resource = devm_request_mem_region(device, addr, size,
1111 dev_name(device));
58ef15b7
DW
1112 if (!devmem->resource)
1113 return ERR_PTR(-ENOMEM);
4ef589dc
JG
1114 break;
1115 }
58ef15b7
DW
1116 if (!devmem->resource)
1117 return ERR_PTR(-ERANGE);
4ef589dc
JG
1118
1119 devmem->resource->desc = IORES_DESC_DEVICE_PRIVATE_MEMORY;
1120 devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
1121 devmem->pfn_last = devmem->pfn_first +
1122 (resource_size(devmem->resource) >> PAGE_SHIFT);
063a7d1d 1123 devmem->page_fault = hmm_devmem_fault;
4ef589dc 1124
bbecd94e
DW
1125 devmem->pagemap.type = MEMORY_DEVICE_PRIVATE;
1126 devmem->pagemap.res = *devmem->resource;
bbecd94e
DW
1127 devmem->pagemap.page_free = hmm_devmem_free;
1128 devmem->pagemap.altmap_valid = false;
1129 devmem->pagemap.ref = &devmem->ref;
1130 devmem->pagemap.data = devmem;
1131 devmem->pagemap.kill = hmm_devmem_ref_kill;
4ef589dc 1132
bbecd94e
DW
1133 result = devm_memremap_pages(devmem->device, &devmem->pagemap);
1134 if (IS_ERR(result))
1135 return result;
4ef589dc 1136 return devmem;
4ef589dc 1137}
02917e9f 1138EXPORT_SYMBOL_GPL(hmm_devmem_add);
4ef589dc 1139
d3df0a42
JG
1140struct hmm_devmem *hmm_devmem_add_resource(const struct hmm_devmem_ops *ops,
1141 struct device *device,
1142 struct resource *res)
1143{
1144 struct hmm_devmem *devmem;
bbecd94e 1145 void *result;
d3df0a42
JG
1146 int ret;
1147
1148 if (res->desc != IORES_DESC_DEVICE_PUBLIC_MEMORY)
1149 return ERR_PTR(-EINVAL);
1150
e7638488 1151 dev_pagemap_get_ops();
d3df0a42 1152
58ef15b7 1153 devmem = devm_kzalloc(device, sizeof(*devmem), GFP_KERNEL);
d3df0a42
JG
1154 if (!devmem)
1155 return ERR_PTR(-ENOMEM);
1156
1157 init_completion(&devmem->completion);
1158 devmem->pfn_first = -1UL;
1159 devmem->pfn_last = -1UL;
1160 devmem->resource = res;
1161 devmem->device = device;
1162 devmem->ops = ops;
1163
1164 ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release,
1165 0, GFP_KERNEL);
1166 if (ret)
58ef15b7 1167 return ERR_PTR(ret);
d3df0a42 1168
58ef15b7
DW
1169 ret = devm_add_action_or_reset(device, hmm_devmem_ref_exit,
1170 &devmem->ref);
d3df0a42 1171 if (ret)
58ef15b7 1172 return ERR_PTR(ret);
d3df0a42
JG
1173
1174 devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
1175 devmem->pfn_last = devmem->pfn_first +
1176 (resource_size(devmem->resource) >> PAGE_SHIFT);
063a7d1d 1177 devmem->page_fault = hmm_devmem_fault;
d3df0a42 1178
bbecd94e
DW
1179 devmem->pagemap.type = MEMORY_DEVICE_PUBLIC;
1180 devmem->pagemap.res = *devmem->resource;
bbecd94e
DW
1181 devmem->pagemap.page_free = hmm_devmem_free;
1182 devmem->pagemap.altmap_valid = false;
1183 devmem->pagemap.ref = &devmem->ref;
1184 devmem->pagemap.data = devmem;
1185 devmem->pagemap.kill = hmm_devmem_ref_kill;
d3df0a42 1186
bbecd94e
DW
1187 result = devm_memremap_pages(devmem->device, &devmem->pagemap);
1188 if (IS_ERR(result))
1189 return result;
d3df0a42 1190 return devmem;
d3df0a42 1191}
02917e9f 1192EXPORT_SYMBOL_GPL(hmm_devmem_add_resource);
d3df0a42 1193
858b54da
JG
1194/*
1195 * A device driver that wants to handle multiple devices memory through a
1196 * single fake device can use hmm_device to do so. This is purely a helper
1197 * and it is not needed to make use of any HMM functionality.
1198 */
1199#define HMM_DEVICE_MAX 256
1200
1201static DECLARE_BITMAP(hmm_device_mask, HMM_DEVICE_MAX);
1202static DEFINE_SPINLOCK(hmm_device_lock);
1203static struct class *hmm_device_class;
1204static dev_t hmm_device_devt;
1205
1206static void hmm_device_release(struct device *device)
1207{
1208 struct hmm_device *hmm_device;
1209
1210 hmm_device = container_of(device, struct hmm_device, device);
1211 spin_lock(&hmm_device_lock);
1212 clear_bit(hmm_device->minor, hmm_device_mask);
1213 spin_unlock(&hmm_device_lock);
1214
1215 kfree(hmm_device);
1216}
1217
1218struct hmm_device *hmm_device_new(void *drvdata)
1219{
1220 struct hmm_device *hmm_device;
1221
1222 hmm_device = kzalloc(sizeof(*hmm_device), GFP_KERNEL);
1223 if (!hmm_device)
1224 return ERR_PTR(-ENOMEM);
1225
1226 spin_lock(&hmm_device_lock);
1227 hmm_device->minor = find_first_zero_bit(hmm_device_mask, HMM_DEVICE_MAX);
1228 if (hmm_device->minor >= HMM_DEVICE_MAX) {
1229 spin_unlock(&hmm_device_lock);
1230 kfree(hmm_device);
1231 return ERR_PTR(-EBUSY);
1232 }
1233 set_bit(hmm_device->minor, hmm_device_mask);
1234 spin_unlock(&hmm_device_lock);
1235
1236 dev_set_name(&hmm_device->device, "hmm_device%d", hmm_device->minor);
1237 hmm_device->device.devt = MKDEV(MAJOR(hmm_device_devt),
1238 hmm_device->minor);
1239 hmm_device->device.release = hmm_device_release;
1240 dev_set_drvdata(&hmm_device->device, drvdata);
1241 hmm_device->device.class = hmm_device_class;
1242 device_initialize(&hmm_device->device);
1243
1244 return hmm_device;
1245}
1246EXPORT_SYMBOL(hmm_device_new);
1247
1248void hmm_device_put(struct hmm_device *hmm_device)
1249{
1250 put_device(&hmm_device->device);
1251}
1252EXPORT_SYMBOL(hmm_device_put);
1253
1254static int __init hmm_init(void)
1255{
1256 int ret;
1257
1258 ret = alloc_chrdev_region(&hmm_device_devt, 0,
1259 HMM_DEVICE_MAX,
1260 "hmm_device");
1261 if (ret)
1262 return ret;
1263
1264 hmm_device_class = class_create(THIS_MODULE, "hmm_device");
1265 if (IS_ERR(hmm_device_class)) {
1266 unregister_chrdev_region(hmm_device_devt, HMM_DEVICE_MAX);
1267 return PTR_ERR(hmm_device_class);
1268 }
1269 return 0;
1270}
1271
1272device_initcall(hmm_init);
df6ad698 1273#endif /* CONFIG_DEVICE_PRIVATE || CONFIG_DEVICE_PUBLIC */