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