mm/hmm: factor out pte and pmd handling to simplify hmm_vma_walk_pmd()
[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 *
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
e1401513
RC
163static void hmm_release(struct mmu_notifier *mn, struct mm_struct *mm)
164{
165 struct hmm_mirror *mirror;
166 struct hmm *hmm = mm->hmm;
167
168 down_write(&hmm->mirrors_sem);
169 mirror = list_first_entry_or_null(&hmm->mirrors, struct hmm_mirror,
170 list);
171 while (mirror) {
172 list_del_init(&mirror->list);
173 if (mirror->ops->release) {
174 /*
175 * Drop mirrors_sem so callback can wait on any pending
176 * work that might itself trigger mmu_notifier callback
177 * and thus would deadlock with us.
178 */
179 up_write(&hmm->mirrors_sem);
180 mirror->ops->release(mirror);
181 down_write(&hmm->mirrors_sem);
182 }
183 mirror = list_first_entry_or_null(&hmm->mirrors,
184 struct hmm_mirror, list);
185 }
186 up_write(&hmm->mirrors_sem);
187}
188
c0b12405
JG
189static void hmm_invalidate_range_start(struct mmu_notifier *mn,
190 struct mm_struct *mm,
191 unsigned long start,
192 unsigned long end)
193{
194 struct hmm *hmm = mm->hmm;
195
196 VM_BUG_ON(!hmm);
197
198 atomic_inc(&hmm->sequence);
199}
200
201static void hmm_invalidate_range_end(struct mmu_notifier *mn,
202 struct mm_struct *mm,
203 unsigned long start,
204 unsigned long end)
205{
206 struct hmm *hmm = mm->hmm;
207
208 VM_BUG_ON(!hmm);
209
210 hmm_invalidate_range(mm->hmm, HMM_UPDATE_INVALIDATE, start, end);
211}
212
213static const struct mmu_notifier_ops hmm_mmu_notifier_ops = {
e1401513 214 .release = hmm_release,
c0b12405
JG
215 .invalidate_range_start = hmm_invalidate_range_start,
216 .invalidate_range_end = hmm_invalidate_range_end,
217};
218
219/*
220 * hmm_mirror_register() - register a mirror against an mm
221 *
222 * @mirror: new mirror struct to register
223 * @mm: mm to register against
224 *
225 * To start mirroring a process address space, the device driver must register
226 * an HMM mirror struct.
227 *
228 * THE mm->mmap_sem MUST BE HELD IN WRITE MODE !
229 */
230int hmm_mirror_register(struct hmm_mirror *mirror, struct mm_struct *mm)
231{
232 /* Sanity check */
233 if (!mm || !mirror || !mirror->ops)
234 return -EINVAL;
235
c01cbba2 236again:
c0b12405
JG
237 mirror->hmm = hmm_register(mm);
238 if (!mirror->hmm)
239 return -ENOMEM;
240
241 down_write(&mirror->hmm->mirrors_sem);
c01cbba2
JG
242 if (mirror->hmm->mm == NULL) {
243 /*
244 * A racing hmm_mirror_unregister() is about to destroy the hmm
245 * struct. Try again to allocate a new one.
246 */
247 up_write(&mirror->hmm->mirrors_sem);
248 mirror->hmm = NULL;
249 goto again;
250 } else {
251 list_add(&mirror->list, &mirror->hmm->mirrors);
252 up_write(&mirror->hmm->mirrors_sem);
253 }
c0b12405
JG
254
255 return 0;
256}
257EXPORT_SYMBOL(hmm_mirror_register);
258
259/*
260 * hmm_mirror_unregister() - unregister a mirror
261 *
262 * @mirror: new mirror struct to register
263 *
264 * Stop mirroring a process address space, and cleanup.
265 */
266void hmm_mirror_unregister(struct hmm_mirror *mirror)
267{
c01cbba2
JG
268 bool should_unregister = false;
269 struct mm_struct *mm;
270 struct hmm *hmm;
271
272 if (mirror->hmm == NULL)
273 return;
c0b12405 274
c01cbba2 275 hmm = mirror->hmm;
c0b12405 276 down_write(&hmm->mirrors_sem);
e1401513 277 list_del_init(&mirror->list);
c01cbba2
JG
278 should_unregister = list_empty(&hmm->mirrors);
279 mirror->hmm = NULL;
280 mm = hmm->mm;
281 hmm->mm = NULL;
c0b12405 282 up_write(&hmm->mirrors_sem);
c01cbba2
JG
283
284 if (!should_unregister || mm == NULL)
285 return;
286
287 spin_lock(&mm->page_table_lock);
288 if (mm->hmm == hmm)
289 mm->hmm = NULL;
290 spin_unlock(&mm->page_table_lock);
291
292 mmu_notifier_unregister_no_release(&hmm->mmu_notifier, mm);
293 kfree(hmm);
c0b12405
JG
294}
295EXPORT_SYMBOL(hmm_mirror_unregister);
da4c3c73 296
74eee180
JG
297struct hmm_vma_walk {
298 struct hmm_range *range;
299 unsigned long last;
300 bool fault;
301 bool block;
302 bool write;
303};
304
305static int hmm_vma_do_fault(struct mm_walk *walk,
306 unsigned long addr,
ff05c0c6 307 uint64_t *pfn)
74eee180
JG
308{
309 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_REMOTE;
310 struct hmm_vma_walk *hmm_vma_walk = walk->private;
311 struct vm_area_struct *vma = walk->vma;
312 int r;
313
314 flags |= hmm_vma_walk->block ? 0 : FAULT_FLAG_ALLOW_RETRY;
315 flags |= hmm_vma_walk->write ? FAULT_FLAG_WRITE : 0;
316 r = handle_mm_fault(vma, addr, flags);
317 if (r & VM_FAULT_RETRY)
318 return -EBUSY;
319 if (r & VM_FAULT_ERROR) {
320 *pfn = HMM_PFN_ERROR;
321 return -EFAULT;
322 }
323
324 return -EAGAIN;
325}
326
da4c3c73
JG
327static int hmm_pfns_bad(unsigned long addr,
328 unsigned long end,
329 struct mm_walk *walk)
330{
c719547f
JG
331 struct hmm_vma_walk *hmm_vma_walk = walk->private;
332 struct hmm_range *range = hmm_vma_walk->range;
ff05c0c6 333 uint64_t *pfns = range->pfns;
da4c3c73
JG
334 unsigned long i;
335
336 i = (addr - range->start) >> PAGE_SHIFT;
337 for (; addr < end; addr += PAGE_SIZE, i++)
338 pfns[i] = HMM_PFN_ERROR;
339
340 return 0;
341}
342
5504ed29
JG
343/*
344 * hmm_vma_walk_hole() - handle a range lacking valid pmd or pte(s)
345 * @start: range virtual start address (inclusive)
346 * @end: range virtual end address (exclusive)
347 * @walk: mm_walk structure
348 * Returns: 0 on success, -EAGAIN after page fault, or page fault error
349 *
350 * This function will be called whenever pmd_none() or pte_none() returns true,
351 * or whenever there is no page directory covering the virtual address range.
352 */
da4c3c73
JG
353static int hmm_vma_walk_hole(unsigned long addr,
354 unsigned long end,
355 struct mm_walk *walk)
356{
74eee180
JG
357 struct hmm_vma_walk *hmm_vma_walk = walk->private;
358 struct hmm_range *range = hmm_vma_walk->range;
ff05c0c6 359 uint64_t *pfns = range->pfns;
da4c3c73
JG
360 unsigned long i;
361
74eee180 362 hmm_vma_walk->last = addr;
da4c3c73 363 i = (addr - range->start) >> PAGE_SHIFT;
74eee180 364 for (; addr < end; addr += PAGE_SIZE, i++) {
da4c3c73 365 pfns[i] = 0;
74eee180
JG
366 if (hmm_vma_walk->fault) {
367 int ret;
da4c3c73 368
74eee180
JG
369 ret = hmm_vma_do_fault(walk, addr, &pfns[i]);
370 if (ret != -EAGAIN)
371 return ret;
372 }
373 }
374
375 return hmm_vma_walk->fault ? -EAGAIN : 0;
da4c3c73
JG
376}
377
53f5c3f4
JG
378static int hmm_vma_handle_pmd(struct mm_walk *walk,
379 unsigned long addr,
380 unsigned long end,
381 uint64_t *pfns,
382 pmd_t pmd)
383{
384 struct hmm_vma_walk *hmm_vma_walk = walk->private;
385 unsigned long pfn, i;
386 uint64_t flag = 0;
387
388 if (pmd_protnone(pmd))
389 return hmm_vma_walk_hole(addr, end, walk);
390
391 if ((hmm_vma_walk->fault & hmm_vma_walk->write) && !pmd_write(pmd))
392 return hmm_vma_walk_hole(addr, end, walk);
393
394 pfn = pmd_pfn(pmd) + pte_index(addr);
395 flag |= pmd_write(pmd) ? HMM_PFN_WRITE : 0;
396 for (i = 0; addr < end; addr += PAGE_SIZE, i++, pfn++)
397 pfns[i] = hmm_pfn_from_pfn(pfn) | flag;
398 hmm_vma_walk->last = end;
399 return 0;
400}
401
402static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,
403 unsigned long end, pmd_t *pmdp, pte_t *ptep,
404 uint64_t *pfn)
405{
406 struct hmm_vma_walk *hmm_vma_walk = walk->private;
407 struct vm_area_struct *vma = walk->vma;
408 pte_t pte = *ptep;
409
410 *pfn = 0;
411
412 if (pte_none(pte)) {
413 *pfn = 0;
414 if (hmm_vma_walk->fault)
415 goto fault;
416 return 0;
417 }
418
419 if (!pte_present(pte)) {
420 swp_entry_t entry = pte_to_swp_entry(pte);
421
422 if (!non_swap_entry(entry)) {
423 if (hmm_vma_walk->fault)
424 goto fault;
425 return 0;
426 }
427
428 /*
429 * This is a special swap entry, ignore migration, use
430 * device and report anything else as error.
431 */
432 if (is_device_private_entry(entry)) {
433 *pfn = hmm_pfn_from_pfn(swp_offset(entry));
434 if (is_write_device_private_entry(entry)) {
435 *pfn |= HMM_PFN_WRITE;
436 } else if ((hmm_vma_walk->fault & hmm_vma_walk->write))
437 goto fault;
438 *pfn |= HMM_PFN_DEVICE_PRIVATE;
439 return 0;
440 }
441
442 if (is_migration_entry(entry)) {
443 if (hmm_vma_walk->fault) {
444 pte_unmap(ptep);
445 hmm_vma_walk->last = addr;
446 migration_entry_wait(vma->vm_mm,
447 pmdp, addr);
448 return -EAGAIN;
449 }
450 return 0;
451 }
452
453 /* Report error for everything else */
454 *pfn = HMM_PFN_ERROR;
455 return -EFAULT;
456 }
457
458 if ((hmm_vma_walk->fault & hmm_vma_walk->write) && !pte_write(pte))
459 goto fault;
460
461 *pfn = hmm_pfn_from_pfn(pte_pfn(pte));
462 *pfn |= pte_write(pte) ? HMM_PFN_WRITE : 0;
463 return 0;
464
465fault:
466 pte_unmap(ptep);
467 /* Fault any virtual address we were asked to fault */
468 return hmm_vma_walk_hole(addr, end, walk);
469}
470
da4c3c73
JG
471static int hmm_vma_walk_pmd(pmd_t *pmdp,
472 unsigned long start,
473 unsigned long end,
474 struct mm_walk *walk)
475{
74eee180
JG
476 struct hmm_vma_walk *hmm_vma_walk = walk->private;
477 struct hmm_range *range = hmm_vma_walk->range;
ff05c0c6 478 uint64_t *pfns = range->pfns;
da4c3c73 479 unsigned long addr = start, i;
da4c3c73
JG
480 pte_t *ptep;
481
482 i = (addr - range->start) >> PAGE_SHIFT;
da4c3c73
JG
483
484again:
485 if (pmd_none(*pmdp))
486 return hmm_vma_walk_hole(start, end, walk);
487
53f5c3f4 488 if (pmd_huge(*pmdp) && (range->vma->vm_flags & VM_HUGETLB))
da4c3c73
JG
489 return hmm_pfns_bad(start, end, walk);
490
491 if (pmd_devmap(*pmdp) || pmd_trans_huge(*pmdp)) {
da4c3c73
JG
492 pmd_t pmd;
493
494 /*
495 * No need to take pmd_lock here, even if some other threads
496 * is splitting the huge pmd we will get that event through
497 * mmu_notifier callback.
498 *
499 * So just read pmd value and check again its a transparent
500 * huge or device mapping one and compute corresponding pfn
501 * values.
502 */
503 pmd = pmd_read_atomic(pmdp);
504 barrier();
505 if (!pmd_devmap(pmd) && !pmd_trans_huge(pmd))
506 goto again;
74eee180 507
53f5c3f4 508 return hmm_vma_handle_pmd(walk, addr, end, &pfns[i], pmd);
da4c3c73
JG
509 }
510
511 if (pmd_bad(*pmdp))
512 return hmm_pfns_bad(start, end, walk);
513
514 ptep = pte_offset_map(pmdp, addr);
515 for (; addr < end; addr += PAGE_SIZE, ptep++, i++) {
53f5c3f4 516 int r;
74eee180 517
53f5c3f4
JG
518 r = hmm_vma_handle_pte(walk, addr, end, pmdp, ptep, &pfns[i]);
519 if (r) {
520 /* hmm_vma_handle_pte() did unmap pte directory */
521 hmm_vma_walk->last = addr;
522 return r;
74eee180 523 }
da4c3c73
JG
524 }
525 pte_unmap(ptep - 1);
526
53f5c3f4 527 hmm_vma_walk->last = addr;
da4c3c73
JG
528 return 0;
529}
530
33cd47dc
JG
531static void hmm_pfns_clear(uint64_t *pfns,
532 unsigned long addr,
533 unsigned long end)
534{
535 for (; addr < end; addr += PAGE_SIZE, pfns++)
536 *pfns = 0;
537}
538
855ce7d2
JG
539static void hmm_pfns_special(struct hmm_range *range)
540{
541 unsigned long addr = range->start, i = 0;
542
543 for (; addr < range->end; addr += PAGE_SIZE, i++)
544 range->pfns[i] = HMM_PFN_SPECIAL;
545}
546
da4c3c73
JG
547/*
548 * hmm_vma_get_pfns() - snapshot CPU page table for a range of virtual addresses
08232a45 549 * @range: range being snapshotted
86586a41
JG
550 * Returns: -EINVAL if invalid argument, -ENOMEM out of memory, -EPERM invalid
551 * vma permission, 0 success
da4c3c73
JG
552 *
553 * This snapshots the CPU page table for a range of virtual addresses. Snapshot
554 * validity is tracked by range struct. See hmm_vma_range_done() for further
555 * information.
556 *
557 * The range struct is initialized here. It tracks the CPU page table, but only
558 * if the function returns success (0), in which case the caller must then call
559 * hmm_vma_range_done() to stop CPU page table update tracking on this range.
560 *
561 * NOT CALLING hmm_vma_range_done() IF FUNCTION RETURNS 0 WILL LEAD TO SERIOUS
562 * MEMORY CORRUPTION ! YOU HAVE BEEN WARNED !
563 */
08232a45 564int hmm_vma_get_pfns(struct hmm_range *range)
da4c3c73 565{
08232a45 566 struct vm_area_struct *vma = range->vma;
74eee180 567 struct hmm_vma_walk hmm_vma_walk;
da4c3c73
JG
568 struct mm_walk mm_walk;
569 struct hmm *hmm;
570
da4c3c73 571 /* Sanity check, this really should not happen ! */
08232a45 572 if (range->start < vma->vm_start || range->start >= vma->vm_end)
da4c3c73 573 return -EINVAL;
08232a45 574 if (range->end < vma->vm_start || range->end > vma->vm_end)
da4c3c73
JG
575 return -EINVAL;
576
577 hmm = hmm_register(vma->vm_mm);
578 if (!hmm)
579 return -ENOMEM;
580 /* Caller must have registered a mirror, via hmm_mirror_register() ! */
581 if (!hmm->mmu_notifier.ops)
582 return -EINVAL;
583
855ce7d2
JG
584 /* FIXME support hugetlb fs */
585 if (is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_SPECIAL)) {
586 hmm_pfns_special(range);
587 return -EINVAL;
588 }
589
86586a41
JG
590 if (!(vma->vm_flags & VM_READ)) {
591 /*
592 * If vma do not allow read access, then assume that it does
593 * not allow write access, either. Architecture that allow
594 * write without read access are not supported by HMM, because
595 * operations such has atomic access would not work.
596 */
597 hmm_pfns_clear(range->pfns, range->start, range->end);
598 return -EPERM;
599 }
600
da4c3c73 601 /* Initialize range to track CPU page table update */
da4c3c73
JG
602 spin_lock(&hmm->lock);
603 range->valid = true;
604 list_add_rcu(&range->list, &hmm->ranges);
605 spin_unlock(&hmm->lock);
606
74eee180
JG
607 hmm_vma_walk.fault = false;
608 hmm_vma_walk.range = range;
609 mm_walk.private = &hmm_vma_walk;
610
da4c3c73
JG
611 mm_walk.vma = vma;
612 mm_walk.mm = vma->vm_mm;
da4c3c73
JG
613 mm_walk.pte_entry = NULL;
614 mm_walk.test_walk = NULL;
615 mm_walk.hugetlb_entry = NULL;
616 mm_walk.pmd_entry = hmm_vma_walk_pmd;
617 mm_walk.pte_hole = hmm_vma_walk_hole;
618
08232a45 619 walk_page_range(range->start, range->end, &mm_walk);
da4c3c73
JG
620 return 0;
621}
622EXPORT_SYMBOL(hmm_vma_get_pfns);
623
624/*
625 * hmm_vma_range_done() - stop tracking change to CPU page table over a range
da4c3c73
JG
626 * @range: range being tracked
627 * Returns: false if range data has been invalidated, true otherwise
628 *
629 * Range struct is used to track updates to the CPU page table after a call to
630 * either hmm_vma_get_pfns() or hmm_vma_fault(). Once the device driver is done
631 * using the data, or wants to lock updates to the data it got from those
632 * functions, it must call the hmm_vma_range_done() function, which will then
633 * stop tracking CPU page table updates.
634 *
635 * Note that device driver must still implement general CPU page table update
636 * tracking either by using hmm_mirror (see hmm_mirror_register()) or by using
637 * the mmu_notifier API directly.
638 *
639 * CPU page table update tracking done through hmm_range is only temporary and
640 * to be used while trying to duplicate CPU page table contents for a range of
641 * virtual addresses.
642 *
643 * There are two ways to use this :
644 * again:
08232a45 645 * hmm_vma_get_pfns(range); or hmm_vma_fault(...);
da4c3c73
JG
646 * trans = device_build_page_table_update_transaction(pfns);
647 * device_page_table_lock();
08232a45 648 * if (!hmm_vma_range_done(range)) {
da4c3c73
JG
649 * device_page_table_unlock();
650 * goto again;
651 * }
652 * device_commit_transaction(trans);
653 * device_page_table_unlock();
654 *
655 * Or:
08232a45 656 * hmm_vma_get_pfns(range); or hmm_vma_fault(...);
da4c3c73 657 * device_page_table_lock();
08232a45
JG
658 * hmm_vma_range_done(range);
659 * device_update_page_table(range->pfns);
da4c3c73
JG
660 * device_page_table_unlock();
661 */
08232a45 662bool hmm_vma_range_done(struct hmm_range *range)
da4c3c73
JG
663{
664 unsigned long npages = (range->end - range->start) >> PAGE_SHIFT;
665 struct hmm *hmm;
666
667 if (range->end <= range->start) {
668 BUG();
669 return false;
670 }
671
08232a45 672 hmm = hmm_register(range->vma->vm_mm);
da4c3c73
JG
673 if (!hmm) {
674 memset(range->pfns, 0, sizeof(*range->pfns) * npages);
675 return false;
676 }
677
678 spin_lock(&hmm->lock);
679 list_del_rcu(&range->list);
680 spin_unlock(&hmm->lock);
681
682 return range->valid;
683}
684EXPORT_SYMBOL(hmm_vma_range_done);
74eee180
JG
685
686/*
687 * hmm_vma_fault() - try to fault some address in a virtual address range
08232a45 688 * @range: range being faulted
74eee180
JG
689 * @write: is it a write fault
690 * @block: allow blocking on fault (if true it sleeps and do not drop mmap_sem)
691 * Returns: 0 success, error otherwise (-EAGAIN means mmap_sem have been drop)
692 *
693 * This is similar to a regular CPU page fault except that it will not trigger
694 * any memory migration if the memory being faulted is not accessible by CPUs.
695 *
ff05c0c6
JG
696 * On error, for one virtual address in the range, the function will mark the
697 * corresponding HMM pfn entry with an error flag.
74eee180
JG
698 *
699 * Expected use pattern:
700 * retry:
701 * down_read(&mm->mmap_sem);
702 * // Find vma and address device wants to fault, initialize hmm_pfn_t
703 * // array accordingly
08232a45 704 * ret = hmm_vma_fault(range, write, block);
74eee180
JG
705 * switch (ret) {
706 * case -EAGAIN:
08232a45 707 * hmm_vma_range_done(range);
74eee180
JG
708 * // You might want to rate limit or yield to play nicely, you may
709 * // also commit any valid pfn in the array assuming that you are
710 * // getting true from hmm_vma_range_monitor_end()
711 * goto retry;
712 * case 0:
713 * break;
86586a41
JG
714 * case -ENOMEM:
715 * case -EINVAL:
716 * case -EPERM:
74eee180
JG
717 * default:
718 * // Handle error !
719 * up_read(&mm->mmap_sem)
720 * return;
721 * }
722 * // Take device driver lock that serialize device page table update
723 * driver_lock_device_page_table_update();
08232a45 724 * hmm_vma_range_done(range);
74eee180
JG
725 * // Commit pfns we got from hmm_vma_fault()
726 * driver_unlock_device_page_table_update();
727 * up_read(&mm->mmap_sem)
728 *
729 * YOU MUST CALL hmm_vma_range_done() AFTER THIS FUNCTION RETURN SUCCESS (0)
730 * BEFORE FREEING THE range struct OR YOU WILL HAVE SERIOUS MEMORY CORRUPTION !
731 *
732 * YOU HAVE BEEN WARNED !
733 */
08232a45 734int hmm_vma_fault(struct hmm_range *range, bool write, bool block)
74eee180 735{
08232a45
JG
736 struct vm_area_struct *vma = range->vma;
737 unsigned long start = range->start;
74eee180
JG
738 struct hmm_vma_walk hmm_vma_walk;
739 struct mm_walk mm_walk;
740 struct hmm *hmm;
741 int ret;
742
743 /* Sanity check, this really should not happen ! */
08232a45 744 if (range->start < vma->vm_start || range->start >= vma->vm_end)
74eee180 745 return -EINVAL;
08232a45 746 if (range->end < vma->vm_start || range->end > vma->vm_end)
74eee180
JG
747 return -EINVAL;
748
749 hmm = hmm_register(vma->vm_mm);
750 if (!hmm) {
08232a45 751 hmm_pfns_clear(range->pfns, range->start, range->end);
74eee180
JG
752 return -ENOMEM;
753 }
754 /* Caller must have registered a mirror using hmm_mirror_register() */
755 if (!hmm->mmu_notifier.ops)
756 return -EINVAL;
757
855ce7d2
JG
758 /* FIXME support hugetlb fs */
759 if (is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_SPECIAL)) {
760 hmm_pfns_special(range);
761 return -EINVAL;
762 }
763
86586a41
JG
764 if (!(vma->vm_flags & VM_READ)) {
765 /*
766 * If vma do not allow read access, then assume that it does
767 * not allow write access, either. Architecture that allow
768 * write without read access are not supported by HMM, because
769 * operations such has atomic access would not work.
770 */
771 hmm_pfns_clear(range->pfns, range->start, range->end);
772 return -EPERM;
773 }
74eee180 774
86586a41
JG
775 /* Initialize range to track CPU page table update */
776 spin_lock(&hmm->lock);
777 range->valid = true;
778 list_add_rcu(&range->list, &hmm->ranges);
779 spin_unlock(&hmm->lock);
780
74eee180
JG
781 hmm_vma_walk.fault = true;
782 hmm_vma_walk.write = write;
783 hmm_vma_walk.block = block;
784 hmm_vma_walk.range = range;
785 mm_walk.private = &hmm_vma_walk;
786 hmm_vma_walk.last = range->start;
787
788 mm_walk.vma = vma;
789 mm_walk.mm = vma->vm_mm;
790 mm_walk.pte_entry = NULL;
791 mm_walk.test_walk = NULL;
792 mm_walk.hugetlb_entry = NULL;
793 mm_walk.pmd_entry = hmm_vma_walk_pmd;
794 mm_walk.pte_hole = hmm_vma_walk_hole;
795
796 do {
08232a45 797 ret = walk_page_range(start, range->end, &mm_walk);
74eee180
JG
798 start = hmm_vma_walk.last;
799 } while (ret == -EAGAIN);
800
801 if (ret) {
802 unsigned long i;
803
804 i = (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
08232a45
JG
805 hmm_pfns_clear(&range->pfns[i], hmm_vma_walk.last, range->end);
806 hmm_vma_range_done(range);
74eee180
JG
807 }
808 return ret;
809}
810EXPORT_SYMBOL(hmm_vma_fault);
c0b12405 811#endif /* IS_ENABLED(CONFIG_HMM_MIRROR) */
4ef589dc
JG
812
813
df6ad698 814#if IS_ENABLED(CONFIG_DEVICE_PRIVATE) || IS_ENABLED(CONFIG_DEVICE_PUBLIC)
4ef589dc
JG
815struct page *hmm_vma_alloc_locked_page(struct vm_area_struct *vma,
816 unsigned long addr)
817{
818 struct page *page;
819
820 page = alloc_page_vma(GFP_HIGHUSER, vma, addr);
821 if (!page)
822 return NULL;
823 lock_page(page);
824 return page;
825}
826EXPORT_SYMBOL(hmm_vma_alloc_locked_page);
827
828
829static void hmm_devmem_ref_release(struct percpu_ref *ref)
830{
831 struct hmm_devmem *devmem;
832
833 devmem = container_of(ref, struct hmm_devmem, ref);
834 complete(&devmem->completion);
835}
836
837static void hmm_devmem_ref_exit(void *data)
838{
839 struct percpu_ref *ref = data;
840 struct hmm_devmem *devmem;
841
842 devmem = container_of(ref, struct hmm_devmem, ref);
843 percpu_ref_exit(ref);
844 devm_remove_action(devmem->device, &hmm_devmem_ref_exit, data);
845}
846
847static void hmm_devmem_ref_kill(void *data)
848{
849 struct percpu_ref *ref = data;
850 struct hmm_devmem *devmem;
851
852 devmem = container_of(ref, struct hmm_devmem, ref);
853 percpu_ref_kill(ref);
854 wait_for_completion(&devmem->completion);
855 devm_remove_action(devmem->device, &hmm_devmem_ref_kill, data);
856}
857
858static int hmm_devmem_fault(struct vm_area_struct *vma,
859 unsigned long addr,
860 const struct page *page,
861 unsigned int flags,
862 pmd_t *pmdp)
863{
864 struct hmm_devmem *devmem = page->pgmap->data;
865
866 return devmem->ops->fault(devmem, vma, addr, page, flags, pmdp);
867}
868
869static void hmm_devmem_free(struct page *page, void *data)
870{
871 struct hmm_devmem *devmem = data;
872
873 devmem->ops->free(devmem, page);
874}
875
876static DEFINE_MUTEX(hmm_devmem_lock);
877static RADIX_TREE(hmm_devmem_radix, GFP_KERNEL);
878
879static void hmm_devmem_radix_release(struct resource *resource)
880{
fec11bc0 881 resource_size_t key, align_start, align_size;
4ef589dc
JG
882
883 align_start = resource->start & ~(PA_SECTION_SIZE - 1);
884 align_size = ALIGN(resource_size(resource), PA_SECTION_SIZE);
4ef589dc
JG
885
886 mutex_lock(&hmm_devmem_lock);
887 for (key = resource->start;
888 key <= resource->end;
889 key += PA_SECTION_SIZE)
890 radix_tree_delete(&hmm_devmem_radix, key >> PA_SECTION_SHIFT);
891 mutex_unlock(&hmm_devmem_lock);
892}
893
894static void hmm_devmem_release(struct device *dev, void *data)
895{
896 struct hmm_devmem *devmem = data;
897 struct resource *resource = devmem->resource;
898 unsigned long start_pfn, npages;
899 struct zone *zone;
900 struct page *page;
901
902 if (percpu_ref_tryget_live(&devmem->ref)) {
903 dev_WARN(dev, "%s: page mapping is still live!\n", __func__);
904 percpu_ref_put(&devmem->ref);
905 }
906
907 /* pages are dead and unused, undo the arch mapping */
908 start_pfn = (resource->start & ~(PA_SECTION_SIZE - 1)) >> PAGE_SHIFT;
909 npages = ALIGN(resource_size(resource), PA_SECTION_SIZE) >> PAGE_SHIFT;
910
911 page = pfn_to_page(start_pfn);
912 zone = page_zone(page);
913
914 mem_hotplug_begin();
d3df0a42 915 if (resource->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY)
da024512 916 __remove_pages(zone, start_pfn, npages, NULL);
d3df0a42
JG
917 else
918 arch_remove_memory(start_pfn << PAGE_SHIFT,
da024512 919 npages << PAGE_SHIFT, NULL);
4ef589dc
JG
920 mem_hotplug_done();
921
922 hmm_devmem_radix_release(resource);
923}
924
925static struct hmm_devmem *hmm_devmem_find(resource_size_t phys)
926{
927 WARN_ON_ONCE(!rcu_read_lock_held());
928
929 return radix_tree_lookup(&hmm_devmem_radix, phys >> PA_SECTION_SHIFT);
930}
931
932static int hmm_devmem_pages_create(struct hmm_devmem *devmem)
933{
934 resource_size_t key, align_start, align_size, align_end;
935 struct device *device = devmem->device;
936 int ret, nid, is_ram;
937 unsigned long pfn;
938
939 align_start = devmem->resource->start & ~(PA_SECTION_SIZE - 1);
940 align_size = ALIGN(devmem->resource->start +
941 resource_size(devmem->resource),
942 PA_SECTION_SIZE) - align_start;
943
944 is_ram = region_intersects(align_start, align_size,
945 IORESOURCE_SYSTEM_RAM,
946 IORES_DESC_NONE);
947 if (is_ram == REGION_MIXED) {
948 WARN_ONCE(1, "%s attempted on mixed region %pr\n",
949 __func__, devmem->resource);
950 return -ENXIO;
951 }
952 if (is_ram == REGION_INTERSECTS)
953 return -ENXIO;
954
d3df0a42
JG
955 if (devmem->resource->desc == IORES_DESC_DEVICE_PUBLIC_MEMORY)
956 devmem->pagemap.type = MEMORY_DEVICE_PUBLIC;
957 else
958 devmem->pagemap.type = MEMORY_DEVICE_PRIVATE;
959
e7744aa2 960 devmem->pagemap.res = *devmem->resource;
4ef589dc
JG
961 devmem->pagemap.page_fault = hmm_devmem_fault;
962 devmem->pagemap.page_free = hmm_devmem_free;
963 devmem->pagemap.dev = devmem->device;
964 devmem->pagemap.ref = &devmem->ref;
965 devmem->pagemap.data = devmem;
966
967 mutex_lock(&hmm_devmem_lock);
968 align_end = align_start + align_size - 1;
969 for (key = align_start; key <= align_end; key += PA_SECTION_SIZE) {
970 struct hmm_devmem *dup;
971
972 rcu_read_lock();
973 dup = hmm_devmem_find(key);
974 rcu_read_unlock();
975 if (dup) {
976 dev_err(device, "%s: collides with mapping for %s\n",
977 __func__, dev_name(dup->device));
978 mutex_unlock(&hmm_devmem_lock);
979 ret = -EBUSY;
980 goto error;
981 }
982 ret = radix_tree_insert(&hmm_devmem_radix,
983 key >> PA_SECTION_SHIFT,
984 devmem);
985 if (ret) {
986 dev_err(device, "%s: failed: %d\n", __func__, ret);
987 mutex_unlock(&hmm_devmem_lock);
988 goto error_radix;
989 }
990 }
991 mutex_unlock(&hmm_devmem_lock);
992
993 nid = dev_to_node(device);
994 if (nid < 0)
995 nid = numa_mem_id();
996
997 mem_hotplug_begin();
998 /*
999 * For device private memory we call add_pages() as we only need to
1000 * allocate and initialize struct page for the device memory. More-
1001 * over the device memory is un-accessible thus we do not want to
1002 * create a linear mapping for the memory like arch_add_memory()
1003 * would do.
d3df0a42
JG
1004 *
1005 * For device public memory, which is accesible by the CPU, we do
1006 * want the linear mapping and thus use arch_add_memory().
4ef589dc 1007 */
d3df0a42 1008 if (devmem->pagemap.type == MEMORY_DEVICE_PUBLIC)
24e6d5a5
CH
1009 ret = arch_add_memory(nid, align_start, align_size, NULL,
1010 false);
d3df0a42
JG
1011 else
1012 ret = add_pages(nid, align_start >> PAGE_SHIFT,
24e6d5a5 1013 align_size >> PAGE_SHIFT, NULL, false);
4ef589dc
JG
1014 if (ret) {
1015 mem_hotplug_done();
1016 goto error_add_memory;
1017 }
1018 move_pfn_range_to_zone(&NODE_DATA(nid)->node_zones[ZONE_DEVICE],
1019 align_start >> PAGE_SHIFT,
a99583e7 1020 align_size >> PAGE_SHIFT, NULL);
4ef589dc
JG
1021 mem_hotplug_done();
1022
1023 for (pfn = devmem->pfn_first; pfn < devmem->pfn_last; pfn++) {
1024 struct page *page = pfn_to_page(pfn);
1025
1026 page->pgmap = &devmem->pagemap;
1027 }
1028 return 0;
1029
1030error_add_memory:
1031 untrack_pfn(NULL, PHYS_PFN(align_start), align_size);
1032error_radix:
1033 hmm_devmem_radix_release(devmem->resource);
1034error:
1035 return ret;
1036}
1037
1038static int hmm_devmem_match(struct device *dev, void *data, void *match_data)
1039{
1040 struct hmm_devmem *devmem = data;
1041
1042 return devmem->resource == match_data;
1043}
1044
1045static void hmm_devmem_pages_remove(struct hmm_devmem *devmem)
1046{
1047 devres_release(devmem->device, &hmm_devmem_release,
1048 &hmm_devmem_match, devmem->resource);
1049}
1050
1051/*
1052 * hmm_devmem_add() - hotplug ZONE_DEVICE memory for device memory
1053 *
1054 * @ops: memory event device driver callback (see struct hmm_devmem_ops)
1055 * @device: device struct to bind the resource too
1056 * @size: size in bytes of the device memory to add
1057 * Returns: pointer to new hmm_devmem struct ERR_PTR otherwise
1058 *
1059 * This function first finds an empty range of physical address big enough to
1060 * contain the new resource, and then hotplugs it as ZONE_DEVICE memory, which
1061 * in turn allocates struct pages. It does not do anything beyond that; all
1062 * events affecting the memory will go through the various callbacks provided
1063 * by hmm_devmem_ops struct.
1064 *
1065 * Device driver should call this function during device initialization and
1066 * is then responsible of memory management. HMM only provides helpers.
1067 */
1068struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops,
1069 struct device *device,
1070 unsigned long size)
1071{
1072 struct hmm_devmem *devmem;
1073 resource_size_t addr;
1074 int ret;
1075
1076 static_branch_enable(&device_private_key);
1077
1078 devmem = devres_alloc_node(&hmm_devmem_release, sizeof(*devmem),
1079 GFP_KERNEL, dev_to_node(device));
1080 if (!devmem)
1081 return ERR_PTR(-ENOMEM);
1082
1083 init_completion(&devmem->completion);
1084 devmem->pfn_first = -1UL;
1085 devmem->pfn_last = -1UL;
1086 devmem->resource = NULL;
1087 devmem->device = device;
1088 devmem->ops = ops;
1089
1090 ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release,
1091 0, GFP_KERNEL);
1092 if (ret)
1093 goto error_percpu_ref;
1094
1095 ret = devm_add_action(device, hmm_devmem_ref_exit, &devmem->ref);
1096 if (ret)
1097 goto error_devm_add_action;
1098
1099 size = ALIGN(size, PA_SECTION_SIZE);
1100 addr = min((unsigned long)iomem_resource.end,
1101 (1UL << MAX_PHYSMEM_BITS) - 1);
1102 addr = addr - size + 1UL;
1103
1104 /*
1105 * FIXME add a new helper to quickly walk resource tree and find free
1106 * range
1107 *
1108 * FIXME what about ioport_resource resource ?
1109 */
1110 for (; addr > size && addr >= iomem_resource.start; addr -= size) {
1111 ret = region_intersects(addr, size, 0, IORES_DESC_NONE);
1112 if (ret != REGION_DISJOINT)
1113 continue;
1114
1115 devmem->resource = devm_request_mem_region(device, addr, size,
1116 dev_name(device));
1117 if (!devmem->resource) {
1118 ret = -ENOMEM;
1119 goto error_no_resource;
1120 }
1121 break;
1122 }
1123 if (!devmem->resource) {
1124 ret = -ERANGE;
1125 goto error_no_resource;
1126 }
1127
1128 devmem->resource->desc = IORES_DESC_DEVICE_PRIVATE_MEMORY;
1129 devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
1130 devmem->pfn_last = devmem->pfn_first +
1131 (resource_size(devmem->resource) >> PAGE_SHIFT);
1132
1133 ret = hmm_devmem_pages_create(devmem);
1134 if (ret)
1135 goto error_pages;
1136
1137 devres_add(device, devmem);
1138
1139 ret = devm_add_action(device, hmm_devmem_ref_kill, &devmem->ref);
1140 if (ret) {
1141 hmm_devmem_remove(devmem);
1142 return ERR_PTR(ret);
1143 }
1144
1145 return devmem;
1146
1147error_pages:
1148 devm_release_mem_region(device, devmem->resource->start,
1149 resource_size(devmem->resource));
1150error_no_resource:
1151error_devm_add_action:
1152 hmm_devmem_ref_kill(&devmem->ref);
1153 hmm_devmem_ref_exit(&devmem->ref);
1154error_percpu_ref:
1155 devres_free(devmem);
1156 return ERR_PTR(ret);
1157}
1158EXPORT_SYMBOL(hmm_devmem_add);
1159
d3df0a42
JG
1160struct hmm_devmem *hmm_devmem_add_resource(const struct hmm_devmem_ops *ops,
1161 struct device *device,
1162 struct resource *res)
1163{
1164 struct hmm_devmem *devmem;
1165 int ret;
1166
1167 if (res->desc != IORES_DESC_DEVICE_PUBLIC_MEMORY)
1168 return ERR_PTR(-EINVAL);
1169
1170 static_branch_enable(&device_private_key);
1171
1172 devmem = devres_alloc_node(&hmm_devmem_release, sizeof(*devmem),
1173 GFP_KERNEL, dev_to_node(device));
1174 if (!devmem)
1175 return ERR_PTR(-ENOMEM);
1176
1177 init_completion(&devmem->completion);
1178 devmem->pfn_first = -1UL;
1179 devmem->pfn_last = -1UL;
1180 devmem->resource = res;
1181 devmem->device = device;
1182 devmem->ops = ops;
1183
1184 ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release,
1185 0, GFP_KERNEL);
1186 if (ret)
1187 goto error_percpu_ref;
1188
1189 ret = devm_add_action(device, hmm_devmem_ref_exit, &devmem->ref);
1190 if (ret)
1191 goto error_devm_add_action;
1192
1193
1194 devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
1195 devmem->pfn_last = devmem->pfn_first +
1196 (resource_size(devmem->resource) >> PAGE_SHIFT);
1197
1198 ret = hmm_devmem_pages_create(devmem);
1199 if (ret)
1200 goto error_devm_add_action;
1201
1202 devres_add(device, devmem);
1203
1204 ret = devm_add_action(device, hmm_devmem_ref_kill, &devmem->ref);
1205 if (ret) {
1206 hmm_devmem_remove(devmem);
1207 return ERR_PTR(ret);
1208 }
1209
1210 return devmem;
1211
1212error_devm_add_action:
1213 hmm_devmem_ref_kill(&devmem->ref);
1214 hmm_devmem_ref_exit(&devmem->ref);
1215error_percpu_ref:
1216 devres_free(devmem);
1217 return ERR_PTR(ret);
1218}
1219EXPORT_SYMBOL(hmm_devmem_add_resource);
1220
4ef589dc
JG
1221/*
1222 * hmm_devmem_remove() - remove device memory (kill and free ZONE_DEVICE)
1223 *
1224 * @devmem: hmm_devmem struct use to track and manage the ZONE_DEVICE memory
1225 *
1226 * This will hot-unplug memory that was hotplugged by hmm_devmem_add on behalf
1227 * of the device driver. It will free struct page and remove the resource that
1228 * reserved the physical address range for this device memory.
1229 */
1230void hmm_devmem_remove(struct hmm_devmem *devmem)
1231{
1232 resource_size_t start, size;
1233 struct device *device;
d3df0a42 1234 bool cdm = false;
4ef589dc
JG
1235
1236 if (!devmem)
1237 return;
1238
1239 device = devmem->device;
1240 start = devmem->resource->start;
1241 size = resource_size(devmem->resource);
1242
d3df0a42 1243 cdm = devmem->resource->desc == IORES_DESC_DEVICE_PUBLIC_MEMORY;
4ef589dc
JG
1244 hmm_devmem_ref_kill(&devmem->ref);
1245 hmm_devmem_ref_exit(&devmem->ref);
1246 hmm_devmem_pages_remove(devmem);
1247
d3df0a42
JG
1248 if (!cdm)
1249 devm_release_mem_region(device, start, size);
4ef589dc
JG
1250}
1251EXPORT_SYMBOL(hmm_devmem_remove);
858b54da
JG
1252
1253/*
1254 * A device driver that wants to handle multiple devices memory through a
1255 * single fake device can use hmm_device to do so. This is purely a helper
1256 * and it is not needed to make use of any HMM functionality.
1257 */
1258#define HMM_DEVICE_MAX 256
1259
1260static DECLARE_BITMAP(hmm_device_mask, HMM_DEVICE_MAX);
1261static DEFINE_SPINLOCK(hmm_device_lock);
1262static struct class *hmm_device_class;
1263static dev_t hmm_device_devt;
1264
1265static void hmm_device_release(struct device *device)
1266{
1267 struct hmm_device *hmm_device;
1268
1269 hmm_device = container_of(device, struct hmm_device, device);
1270 spin_lock(&hmm_device_lock);
1271 clear_bit(hmm_device->minor, hmm_device_mask);
1272 spin_unlock(&hmm_device_lock);
1273
1274 kfree(hmm_device);
1275}
1276
1277struct hmm_device *hmm_device_new(void *drvdata)
1278{
1279 struct hmm_device *hmm_device;
1280
1281 hmm_device = kzalloc(sizeof(*hmm_device), GFP_KERNEL);
1282 if (!hmm_device)
1283 return ERR_PTR(-ENOMEM);
1284
1285 spin_lock(&hmm_device_lock);
1286 hmm_device->minor = find_first_zero_bit(hmm_device_mask, HMM_DEVICE_MAX);
1287 if (hmm_device->minor >= HMM_DEVICE_MAX) {
1288 spin_unlock(&hmm_device_lock);
1289 kfree(hmm_device);
1290 return ERR_PTR(-EBUSY);
1291 }
1292 set_bit(hmm_device->minor, hmm_device_mask);
1293 spin_unlock(&hmm_device_lock);
1294
1295 dev_set_name(&hmm_device->device, "hmm_device%d", hmm_device->minor);
1296 hmm_device->device.devt = MKDEV(MAJOR(hmm_device_devt),
1297 hmm_device->minor);
1298 hmm_device->device.release = hmm_device_release;
1299 dev_set_drvdata(&hmm_device->device, drvdata);
1300 hmm_device->device.class = hmm_device_class;
1301 device_initialize(&hmm_device->device);
1302
1303 return hmm_device;
1304}
1305EXPORT_SYMBOL(hmm_device_new);
1306
1307void hmm_device_put(struct hmm_device *hmm_device)
1308{
1309 put_device(&hmm_device->device);
1310}
1311EXPORT_SYMBOL(hmm_device_put);
1312
1313static int __init hmm_init(void)
1314{
1315 int ret;
1316
1317 ret = alloc_chrdev_region(&hmm_device_devt, 0,
1318 HMM_DEVICE_MAX,
1319 "hmm_device");
1320 if (ret)
1321 return ret;
1322
1323 hmm_device_class = class_create(THIS_MODULE, "hmm_device");
1324 if (IS_ERR(hmm_device_class)) {
1325 unregister_chrdev_region(hmm_device_devt, HMM_DEVICE_MAX);
1326 return PTR_ERR(hmm_device_class);
1327 }
1328 return 0;
1329}
1330
1331device_initcall(hmm_init);
df6ad698 1332#endif /* CONFIG_DEVICE_PRIVATE || CONFIG_DEVICE_PUBLIC */