blob: 320fdc87f064364a4f8d2835cf430a1c3be58915 [file] [log] [blame]
Jérôme Glisse133ff0e2017-09-08 16:11:23 -07001/*
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>
Jérôme Glisse858b54d2017-09-08 16:12:02 -070022#include <linux/init.h>
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070023#include <linux/rmap.h>
24#include <linux/swap.h>
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070025#include <linux/slab.h>
26#include <linux/sched.h>
Jérôme Glisse4ef589d2017-09-08 16:11:58 -070027#include <linux/mmzone.h>
28#include <linux/pagemap.h>
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070029#include <linux/swapops.h>
30#include <linux/hugetlb.h>
Jérôme Glisse4ef589d2017-09-08 16:11:58 -070031#include <linux/memremap.h>
Jérôme Glisse7b2d55d22017-09-08 16:11:46 -070032#include <linux/jump_label.h>
Jérôme Glissec0b12402017-09-08 16:11:27 -070033#include <linux/mmu_notifier.h>
Jérôme Glisse4ef589d2017-09-08 16:11:58 -070034#include <linux/memory_hotplug.h>
35
36#define PA_SECTION_SIZE (1UL << PA_SECTION_SHIFT)
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070037
Jérôme Glisse6b368cd2017-09-08 16:12:32 -070038#if defined(CONFIG_DEVICE_PRIVATE) || defined(CONFIG_DEVICE_PUBLIC)
Jérôme Glisse7b2d55d22017-09-08 16:11:46 -070039/*
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);
Jérôme Glisse6b368cd2017-09-08 16:12:32 -070044#endif /* CONFIG_DEVICE_PRIVATE || CONFIG_DEVICE_PUBLIC */
Jérôme Glisse7b2d55d22017-09-08 16:11:46 -070045
46
Jérôme Glisse6b368cd2017-09-08 16:12:32 -070047#if IS_ENABLED(CONFIG_HMM_MIRROR)
Jérôme Glissec0b12402017-09-08 16:11:27 -070048static const struct mmu_notifier_ops hmm_mmu_notifier_ops;
49
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070050/*
51 * struct hmm - HMM per mm struct
52 *
53 * @mm: mm struct this HMM struct is bound to
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070054 * @lock: lock protecting ranges list
Jérôme Glissec0b12402017-09-08 16:11:27 -070055 * @sequence: we track updates to the CPU page table with a sequence number
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070056 * @ranges: list of range being snapshotted
Jérôme Glissec0b12402017-09-08 16:11:27 -070057 * @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
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070060 */
61struct hmm {
62 struct mm_struct *mm;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070063 spinlock_t lock;
Jérôme Glissec0b12402017-09-08 16:11:27 -070064 atomic_t sequence;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070065 struct list_head ranges;
Jérôme Glissec0b12402017-09-08 16:11:27 -070066 struct list_head mirrors;
67 struct mmu_notifier mmu_notifier;
68 struct rw_semaphore mirrors_sem;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070069};
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{
Jérôme Glissec0b12402017-09-08 16:11:27 -070081 struct hmm *hmm = READ_ONCE(mm->hmm);
82 bool cleanup = false;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070083
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 */
Jérôme Glissec0b12402017-09-08 16:11:27 -070089 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;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070099 INIT_LIST_HEAD(&hmm->ranges);
100 spin_lock_init(&hmm->lock);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700101 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
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700125 return mm->hmm;
126}
127
128void hmm_mm_destroy(struct mm_struct *mm)
129{
130 kfree(mm->hmm);
131}
Jérôme Glissec0b12402017-09-08 16:11:27 -0700132
Jérôme Glissec0b12402017-09-08 16:11:27 -0700133static 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;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700139 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);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700155
156 down_read(&hmm->mirrors_sem);
157 list_for_each_entry(mirror, &hmm->mirrors, list)
158 mirror->ops->sync_cpu_device_pagetables(mirror, action,
159 start, end);
160 up_read(&hmm->mirrors_sem);
161}
162
163static void hmm_invalidate_range_start(struct mmu_notifier *mn,
164 struct mm_struct *mm,
165 unsigned long start,
166 unsigned long end)
167{
168 struct hmm *hmm = mm->hmm;
169
170 VM_BUG_ON(!hmm);
171
172 atomic_inc(&hmm->sequence);
173}
174
175static void hmm_invalidate_range_end(struct mmu_notifier *mn,
176 struct mm_struct *mm,
177 unsigned long start,
178 unsigned long end)
179{
180 struct hmm *hmm = mm->hmm;
181
182 VM_BUG_ON(!hmm);
183
184 hmm_invalidate_range(mm->hmm, HMM_UPDATE_INVALIDATE, start, end);
185}
186
187static const struct mmu_notifier_ops hmm_mmu_notifier_ops = {
188 .invalidate_range_start = hmm_invalidate_range_start,
189 .invalidate_range_end = hmm_invalidate_range_end,
190};
191
192/*
193 * hmm_mirror_register() - register a mirror against an mm
194 *
195 * @mirror: new mirror struct to register
196 * @mm: mm to register against
197 *
198 * To start mirroring a process address space, the device driver must register
199 * an HMM mirror struct.
200 *
201 * THE mm->mmap_sem MUST BE HELD IN WRITE MODE !
202 */
203int hmm_mirror_register(struct hmm_mirror *mirror, struct mm_struct *mm)
204{
205 /* Sanity check */
206 if (!mm || !mirror || !mirror->ops)
207 return -EINVAL;
208
209 mirror->hmm = hmm_register(mm);
210 if (!mirror->hmm)
211 return -ENOMEM;
212
213 down_write(&mirror->hmm->mirrors_sem);
214 list_add(&mirror->list, &mirror->hmm->mirrors);
215 up_write(&mirror->hmm->mirrors_sem);
216
217 return 0;
218}
219EXPORT_SYMBOL(hmm_mirror_register);
220
221/*
222 * hmm_mirror_unregister() - unregister a mirror
223 *
224 * @mirror: new mirror struct to register
225 *
226 * Stop mirroring a process address space, and cleanup.
227 */
228void hmm_mirror_unregister(struct hmm_mirror *mirror)
229{
230 struct hmm *hmm = mirror->hmm;
231
232 down_write(&hmm->mirrors_sem);
233 list_del(&mirror->list);
234 up_write(&hmm->mirrors_sem);
235}
236EXPORT_SYMBOL(hmm_mirror_unregister);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700237
Jérôme Glisse74eee182017-09-08 16:11:35 -0700238struct hmm_vma_walk {
239 struct hmm_range *range;
240 unsigned long last;
241 bool fault;
242 bool block;
243 bool write;
244};
245
246static int hmm_vma_do_fault(struct mm_walk *walk,
247 unsigned long addr,
248 hmm_pfn_t *pfn)
249{
250 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_REMOTE;
251 struct hmm_vma_walk *hmm_vma_walk = walk->private;
252 struct vm_area_struct *vma = walk->vma;
253 int r;
254
255 flags |= hmm_vma_walk->block ? 0 : FAULT_FLAG_ALLOW_RETRY;
256 flags |= hmm_vma_walk->write ? FAULT_FLAG_WRITE : 0;
257 r = handle_mm_fault(vma, addr, flags);
258 if (r & VM_FAULT_RETRY)
259 return -EBUSY;
260 if (r & VM_FAULT_ERROR) {
261 *pfn = HMM_PFN_ERROR;
262 return -EFAULT;
263 }
264
265 return -EAGAIN;
266}
267
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700268static void hmm_pfns_special(hmm_pfn_t *pfns,
269 unsigned long addr,
270 unsigned long end)
271{
272 for (; addr < end; addr += PAGE_SIZE, pfns++)
273 *pfns = HMM_PFN_SPECIAL;
274}
275
276static int hmm_pfns_bad(unsigned long addr,
277 unsigned long end,
278 struct mm_walk *walk)
279{
280 struct hmm_range *range = walk->private;
281 hmm_pfn_t *pfns = range->pfns;
282 unsigned long i;
283
284 i = (addr - range->start) >> PAGE_SHIFT;
285 for (; addr < end; addr += PAGE_SIZE, i++)
286 pfns[i] = HMM_PFN_ERROR;
287
288 return 0;
289}
290
Jérôme Glisse74eee182017-09-08 16:11:35 -0700291static void hmm_pfns_clear(hmm_pfn_t *pfns,
292 unsigned long addr,
293 unsigned long end)
294{
295 for (; addr < end; addr += PAGE_SIZE, pfns++)
296 *pfns = 0;
297}
298
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700299static int hmm_vma_walk_hole(unsigned long addr,
300 unsigned long end,
301 struct mm_walk *walk)
302{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700303 struct hmm_vma_walk *hmm_vma_walk = walk->private;
304 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700305 hmm_pfn_t *pfns = range->pfns;
306 unsigned long i;
307
Jérôme Glisse74eee182017-09-08 16:11:35 -0700308 hmm_vma_walk->last = addr;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700309 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700310 for (; addr < end; addr += PAGE_SIZE, i++) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700311 pfns[i] = HMM_PFN_EMPTY;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700312 if (hmm_vma_walk->fault) {
313 int ret;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700314
Jérôme Glisse74eee182017-09-08 16:11:35 -0700315 ret = hmm_vma_do_fault(walk, addr, &pfns[i]);
316 if (ret != -EAGAIN)
317 return ret;
318 }
319 }
320
321 return hmm_vma_walk->fault ? -EAGAIN : 0;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700322}
323
324static int hmm_vma_walk_clear(unsigned long addr,
325 unsigned long end,
326 struct mm_walk *walk)
327{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700328 struct hmm_vma_walk *hmm_vma_walk = walk->private;
329 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700330 hmm_pfn_t *pfns = range->pfns;
331 unsigned long i;
332
Jérôme Glisse74eee182017-09-08 16:11:35 -0700333 hmm_vma_walk->last = addr;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700334 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700335 for (; addr < end; addr += PAGE_SIZE, i++) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700336 pfns[i] = 0;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700337 if (hmm_vma_walk->fault) {
338 int ret;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700339
Jérôme Glisse74eee182017-09-08 16:11:35 -0700340 ret = hmm_vma_do_fault(walk, addr, &pfns[i]);
341 if (ret != -EAGAIN)
342 return ret;
343 }
344 }
345
346 return hmm_vma_walk->fault ? -EAGAIN : 0;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700347}
348
349static int hmm_vma_walk_pmd(pmd_t *pmdp,
350 unsigned long start,
351 unsigned long end,
352 struct mm_walk *walk)
353{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700354 struct hmm_vma_walk *hmm_vma_walk = walk->private;
355 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700356 struct vm_area_struct *vma = walk->vma;
357 hmm_pfn_t *pfns = range->pfns;
358 unsigned long addr = start, i;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700359 bool write_fault;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700360 hmm_pfn_t flag;
361 pte_t *ptep;
362
363 i = (addr - range->start) >> PAGE_SHIFT;
364 flag = vma->vm_flags & VM_READ ? HMM_PFN_READ : 0;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700365 write_fault = hmm_vma_walk->fault & hmm_vma_walk->write;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700366
367again:
368 if (pmd_none(*pmdp))
369 return hmm_vma_walk_hole(start, end, walk);
370
371 if (pmd_huge(*pmdp) && vma->vm_flags & VM_HUGETLB)
372 return hmm_pfns_bad(start, end, walk);
373
374 if (pmd_devmap(*pmdp) || pmd_trans_huge(*pmdp)) {
375 unsigned long pfn;
376 pmd_t pmd;
377
378 /*
379 * No need to take pmd_lock here, even if some other threads
380 * is splitting the huge pmd we will get that event through
381 * mmu_notifier callback.
382 *
383 * So just read pmd value and check again its a transparent
384 * huge or device mapping one and compute corresponding pfn
385 * values.
386 */
387 pmd = pmd_read_atomic(pmdp);
388 barrier();
389 if (!pmd_devmap(pmd) && !pmd_trans_huge(pmd))
390 goto again;
391 if (pmd_protnone(pmd))
392 return hmm_vma_walk_clear(start, end, walk);
393
Linus Torvaldsf6f37322017-12-15 18:53:22 -0800394 if (write_fault && !pmd_write(pmd))
Jérôme Glisse74eee182017-09-08 16:11:35 -0700395 return hmm_vma_walk_clear(start, end, walk);
396
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700397 pfn = pmd_pfn(pmd) + pte_index(addr);
Linus Torvaldsf6f37322017-12-15 18:53:22 -0800398 flag |= pmd_write(pmd) ? HMM_PFN_WRITE : 0;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700399 for (; addr < end; addr += PAGE_SIZE, i++, pfn++)
400 pfns[i] = hmm_pfn_t_from_pfn(pfn) | flag;
401 return 0;
402 }
403
404 if (pmd_bad(*pmdp))
405 return hmm_pfns_bad(start, end, walk);
406
407 ptep = pte_offset_map(pmdp, addr);
408 for (; addr < end; addr += PAGE_SIZE, ptep++, i++) {
409 pte_t pte = *ptep;
410
411 pfns[i] = 0;
412
Jérôme Glisse74eee182017-09-08 16:11:35 -0700413 if (pte_none(pte)) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700414 pfns[i] = HMM_PFN_EMPTY;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700415 if (hmm_vma_walk->fault)
416 goto fault;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700417 continue;
418 }
419
Jérôme Glisse74eee182017-09-08 16:11:35 -0700420 if (!pte_present(pte)) {
421 swp_entry_t entry;
422
423 if (!non_swap_entry(entry)) {
424 if (hmm_vma_walk->fault)
425 goto fault;
426 continue;
427 }
428
429 entry = pte_to_swp_entry(pte);
430
431 /*
432 * This is a special swap entry, ignore migration, use
433 * device and report anything else as error.
434 */
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700435 if (is_device_private_entry(entry)) {
436 pfns[i] = hmm_pfn_t_from_pfn(swp_offset(entry));
437 if (is_write_device_private_entry(entry)) {
438 pfns[i] |= HMM_PFN_WRITE;
439 } else if (write_fault)
440 goto fault;
441 pfns[i] |= HMM_PFN_DEVICE_UNADDRESSABLE;
442 pfns[i] |= flag;
443 } else if (is_migration_entry(entry)) {
Jérôme Glisse74eee182017-09-08 16:11:35 -0700444 if (hmm_vma_walk->fault) {
445 pte_unmap(ptep);
446 hmm_vma_walk->last = addr;
447 migration_entry_wait(vma->vm_mm,
448 pmdp, addr);
449 return -EAGAIN;
450 }
451 continue;
452 } else {
453 /* Report error for everything else */
454 pfns[i] = HMM_PFN_ERROR;
455 }
456 continue;
457 }
458
Linus Torvaldsf6f37322017-12-15 18:53:22 -0800459 if (write_fault && !pte_write(pte))
Jérôme Glisse74eee182017-09-08 16:11:35 -0700460 goto fault;
461
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700462 pfns[i] = hmm_pfn_t_from_pfn(pte_pfn(pte)) | flag;
Linus Torvaldsf6f37322017-12-15 18:53:22 -0800463 pfns[i] |= pte_write(pte) ? HMM_PFN_WRITE : 0;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700464 continue;
465
466fault:
467 pte_unmap(ptep);
468 /* Fault all pages in range */
469 return hmm_vma_walk_clear(start, end, walk);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700470 }
471 pte_unmap(ptep - 1);
472
473 return 0;
474}
475
476/*
477 * hmm_vma_get_pfns() - snapshot CPU page table for a range of virtual addresses
478 * @vma: virtual memory area containing the virtual address range
479 * @range: used to track snapshot validity
480 * @start: range virtual start address (inclusive)
481 * @end: range virtual end address (exclusive)
482 * @entries: array of hmm_pfn_t: provided by the caller, filled in by function
483 * Returns: -EINVAL if invalid argument, -ENOMEM out of memory, 0 success
484 *
485 * This snapshots the CPU page table for a range of virtual addresses. Snapshot
486 * validity is tracked by range struct. See hmm_vma_range_done() for further
487 * information.
488 *
489 * The range struct is initialized here. It tracks the CPU page table, but only
490 * if the function returns success (0), in which case the caller must then call
491 * hmm_vma_range_done() to stop CPU page table update tracking on this range.
492 *
493 * NOT CALLING hmm_vma_range_done() IF FUNCTION RETURNS 0 WILL LEAD TO SERIOUS
494 * MEMORY CORRUPTION ! YOU HAVE BEEN WARNED !
495 */
496int hmm_vma_get_pfns(struct vm_area_struct *vma,
497 struct hmm_range *range,
498 unsigned long start,
499 unsigned long end,
500 hmm_pfn_t *pfns)
501{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700502 struct hmm_vma_walk hmm_vma_walk;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700503 struct mm_walk mm_walk;
504 struct hmm *hmm;
505
506 /* FIXME support hugetlb fs */
507 if (is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_SPECIAL)) {
508 hmm_pfns_special(pfns, start, end);
509 return -EINVAL;
510 }
511
512 /* Sanity check, this really should not happen ! */
513 if (start < vma->vm_start || start >= vma->vm_end)
514 return -EINVAL;
515 if (end < vma->vm_start || end > vma->vm_end)
516 return -EINVAL;
517
518 hmm = hmm_register(vma->vm_mm);
519 if (!hmm)
520 return -ENOMEM;
521 /* Caller must have registered a mirror, via hmm_mirror_register() ! */
522 if (!hmm->mmu_notifier.ops)
523 return -EINVAL;
524
525 /* Initialize range to track CPU page table update */
526 range->start = start;
527 range->pfns = pfns;
528 range->end = end;
529 spin_lock(&hmm->lock);
530 range->valid = true;
531 list_add_rcu(&range->list, &hmm->ranges);
532 spin_unlock(&hmm->lock);
533
Jérôme Glisse74eee182017-09-08 16:11:35 -0700534 hmm_vma_walk.fault = false;
535 hmm_vma_walk.range = range;
536 mm_walk.private = &hmm_vma_walk;
537
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700538 mm_walk.vma = vma;
539 mm_walk.mm = vma->vm_mm;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700540 mm_walk.pte_entry = NULL;
541 mm_walk.test_walk = NULL;
542 mm_walk.hugetlb_entry = NULL;
543 mm_walk.pmd_entry = hmm_vma_walk_pmd;
544 mm_walk.pte_hole = hmm_vma_walk_hole;
545
546 walk_page_range(start, end, &mm_walk);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700547 return 0;
548}
549EXPORT_SYMBOL(hmm_vma_get_pfns);
550
551/*
552 * hmm_vma_range_done() - stop tracking change to CPU page table over a range
553 * @vma: virtual memory area containing the virtual address range
554 * @range: range being tracked
555 * Returns: false if range data has been invalidated, true otherwise
556 *
557 * Range struct is used to track updates to the CPU page table after a call to
558 * either hmm_vma_get_pfns() or hmm_vma_fault(). Once the device driver is done
559 * using the data, or wants to lock updates to the data it got from those
560 * functions, it must call the hmm_vma_range_done() function, which will then
561 * stop tracking CPU page table updates.
562 *
563 * Note that device driver must still implement general CPU page table update
564 * tracking either by using hmm_mirror (see hmm_mirror_register()) or by using
565 * the mmu_notifier API directly.
566 *
567 * CPU page table update tracking done through hmm_range is only temporary and
568 * to be used while trying to duplicate CPU page table contents for a range of
569 * virtual addresses.
570 *
571 * There are two ways to use this :
572 * again:
Jérôme Glisse74eee182017-09-08 16:11:35 -0700573 * hmm_vma_get_pfns(vma, range, start, end, pfns); or hmm_vma_fault(...);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700574 * trans = device_build_page_table_update_transaction(pfns);
575 * device_page_table_lock();
576 * if (!hmm_vma_range_done(vma, range)) {
577 * device_page_table_unlock();
578 * goto again;
579 * }
580 * device_commit_transaction(trans);
581 * device_page_table_unlock();
582 *
583 * Or:
Jérôme Glisse74eee182017-09-08 16:11:35 -0700584 * hmm_vma_get_pfns(vma, range, start, end, pfns); or hmm_vma_fault(...);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700585 * device_page_table_lock();
586 * hmm_vma_range_done(vma, range);
587 * device_update_page_table(pfns);
588 * device_page_table_unlock();
589 */
590bool hmm_vma_range_done(struct vm_area_struct *vma, struct hmm_range *range)
591{
592 unsigned long npages = (range->end - range->start) >> PAGE_SHIFT;
593 struct hmm *hmm;
594
595 if (range->end <= range->start) {
596 BUG();
597 return false;
598 }
599
600 hmm = hmm_register(vma->vm_mm);
601 if (!hmm) {
602 memset(range->pfns, 0, sizeof(*range->pfns) * npages);
603 return false;
604 }
605
606 spin_lock(&hmm->lock);
607 list_del_rcu(&range->list);
608 spin_unlock(&hmm->lock);
609
610 return range->valid;
611}
612EXPORT_SYMBOL(hmm_vma_range_done);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700613
614/*
615 * hmm_vma_fault() - try to fault some address in a virtual address range
616 * @vma: virtual memory area containing the virtual address range
617 * @range: use to track pfns array content validity
618 * @start: fault range virtual start address (inclusive)
619 * @end: fault range virtual end address (exclusive)
620 * @pfns: array of hmm_pfn_t, only entry with fault flag set will be faulted
621 * @write: is it a write fault
622 * @block: allow blocking on fault (if true it sleeps and do not drop mmap_sem)
623 * Returns: 0 success, error otherwise (-EAGAIN means mmap_sem have been drop)
624 *
625 * This is similar to a regular CPU page fault except that it will not trigger
626 * any memory migration if the memory being faulted is not accessible by CPUs.
627 *
628 * On error, for one virtual address in the range, the function will set the
629 * hmm_pfn_t error flag for the corresponding pfn entry.
630 *
631 * Expected use pattern:
632 * retry:
633 * down_read(&mm->mmap_sem);
634 * // Find vma and address device wants to fault, initialize hmm_pfn_t
635 * // array accordingly
636 * ret = hmm_vma_fault(vma, start, end, pfns, allow_retry);
637 * switch (ret) {
638 * case -EAGAIN:
639 * hmm_vma_range_done(vma, range);
640 * // You might want to rate limit or yield to play nicely, you may
641 * // also commit any valid pfn in the array assuming that you are
642 * // getting true from hmm_vma_range_monitor_end()
643 * goto retry;
644 * case 0:
645 * break;
646 * default:
647 * // Handle error !
648 * up_read(&mm->mmap_sem)
649 * return;
650 * }
651 * // Take device driver lock that serialize device page table update
652 * driver_lock_device_page_table_update();
653 * hmm_vma_range_done(vma, range);
654 * // Commit pfns we got from hmm_vma_fault()
655 * driver_unlock_device_page_table_update();
656 * up_read(&mm->mmap_sem)
657 *
658 * YOU MUST CALL hmm_vma_range_done() AFTER THIS FUNCTION RETURN SUCCESS (0)
659 * BEFORE FREEING THE range struct OR YOU WILL HAVE SERIOUS MEMORY CORRUPTION !
660 *
661 * YOU HAVE BEEN WARNED !
662 */
663int hmm_vma_fault(struct vm_area_struct *vma,
664 struct hmm_range *range,
665 unsigned long start,
666 unsigned long end,
667 hmm_pfn_t *pfns,
668 bool write,
669 bool block)
670{
671 struct hmm_vma_walk hmm_vma_walk;
672 struct mm_walk mm_walk;
673 struct hmm *hmm;
674 int ret;
675
676 /* Sanity check, this really should not happen ! */
677 if (start < vma->vm_start || start >= vma->vm_end)
678 return -EINVAL;
679 if (end < vma->vm_start || end > vma->vm_end)
680 return -EINVAL;
681
682 hmm = hmm_register(vma->vm_mm);
683 if (!hmm) {
684 hmm_pfns_clear(pfns, start, end);
685 return -ENOMEM;
686 }
687 /* Caller must have registered a mirror using hmm_mirror_register() */
688 if (!hmm->mmu_notifier.ops)
689 return -EINVAL;
690
691 /* Initialize range to track CPU page table update */
692 range->start = start;
693 range->pfns = pfns;
694 range->end = end;
695 spin_lock(&hmm->lock);
696 range->valid = true;
697 list_add_rcu(&range->list, &hmm->ranges);
698 spin_unlock(&hmm->lock);
699
700 /* FIXME support hugetlb fs */
701 if (is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_SPECIAL)) {
702 hmm_pfns_special(pfns, start, end);
703 return 0;
704 }
705
706 hmm_vma_walk.fault = true;
707 hmm_vma_walk.write = write;
708 hmm_vma_walk.block = block;
709 hmm_vma_walk.range = range;
710 mm_walk.private = &hmm_vma_walk;
711 hmm_vma_walk.last = range->start;
712
713 mm_walk.vma = vma;
714 mm_walk.mm = vma->vm_mm;
715 mm_walk.pte_entry = NULL;
716 mm_walk.test_walk = NULL;
717 mm_walk.hugetlb_entry = NULL;
718 mm_walk.pmd_entry = hmm_vma_walk_pmd;
719 mm_walk.pte_hole = hmm_vma_walk_hole;
720
721 do {
722 ret = walk_page_range(start, end, &mm_walk);
723 start = hmm_vma_walk.last;
724 } while (ret == -EAGAIN);
725
726 if (ret) {
727 unsigned long i;
728
729 i = (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
730 hmm_pfns_clear(&pfns[i], hmm_vma_walk.last, end);
731 hmm_vma_range_done(vma, range);
732 }
733 return ret;
734}
735EXPORT_SYMBOL(hmm_vma_fault);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700736#endif /* IS_ENABLED(CONFIG_HMM_MIRROR) */
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700737
738
Jérôme Glissedf6ad692017-09-08 16:12:24 -0700739#if IS_ENABLED(CONFIG_DEVICE_PRIVATE) || IS_ENABLED(CONFIG_DEVICE_PUBLIC)
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700740struct page *hmm_vma_alloc_locked_page(struct vm_area_struct *vma,
741 unsigned long addr)
742{
743 struct page *page;
744
745 page = alloc_page_vma(GFP_HIGHUSER, vma, addr);
746 if (!page)
747 return NULL;
748 lock_page(page);
749 return page;
750}
751EXPORT_SYMBOL(hmm_vma_alloc_locked_page);
752
753
754static void hmm_devmem_ref_release(struct percpu_ref *ref)
755{
756 struct hmm_devmem *devmem;
757
758 devmem = container_of(ref, struct hmm_devmem, ref);
759 complete(&devmem->completion);
760}
761
762static void hmm_devmem_ref_exit(void *data)
763{
764 struct percpu_ref *ref = data;
765 struct hmm_devmem *devmem;
766
767 devmem = container_of(ref, struct hmm_devmem, ref);
768 percpu_ref_exit(ref);
769 devm_remove_action(devmem->device, &hmm_devmem_ref_exit, data);
770}
771
772static void hmm_devmem_ref_kill(void *data)
773{
774 struct percpu_ref *ref = data;
775 struct hmm_devmem *devmem;
776
777 devmem = container_of(ref, struct hmm_devmem, ref);
778 percpu_ref_kill(ref);
779 wait_for_completion(&devmem->completion);
780 devm_remove_action(devmem->device, &hmm_devmem_ref_kill, data);
781}
782
783static int hmm_devmem_fault(struct vm_area_struct *vma,
784 unsigned long addr,
785 const struct page *page,
786 unsigned int flags,
787 pmd_t *pmdp)
788{
789 struct hmm_devmem *devmem = page->pgmap->data;
790
791 return devmem->ops->fault(devmem, vma, addr, page, flags, pmdp);
792}
793
794static void hmm_devmem_free(struct page *page, void *data)
795{
796 struct hmm_devmem *devmem = data;
797
798 devmem->ops->free(devmem, page);
799}
800
801static DEFINE_MUTEX(hmm_devmem_lock);
802static RADIX_TREE(hmm_devmem_radix, GFP_KERNEL);
803
804static void hmm_devmem_radix_release(struct resource *resource)
805{
Colin Ian Kingfec11bc2017-11-15 17:38:52 -0800806 resource_size_t key, align_start, align_size;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700807
808 align_start = resource->start & ~(PA_SECTION_SIZE - 1);
809 align_size = ALIGN(resource_size(resource), PA_SECTION_SIZE);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700810
811 mutex_lock(&hmm_devmem_lock);
812 for (key = resource->start;
813 key <= resource->end;
814 key += PA_SECTION_SIZE)
815 radix_tree_delete(&hmm_devmem_radix, key >> PA_SECTION_SHIFT);
816 mutex_unlock(&hmm_devmem_lock);
817}
818
819static void hmm_devmem_release(struct device *dev, void *data)
820{
821 struct hmm_devmem *devmem = data;
822 struct resource *resource = devmem->resource;
823 unsigned long start_pfn, npages;
824 struct zone *zone;
825 struct page *page;
826
827 if (percpu_ref_tryget_live(&devmem->ref)) {
828 dev_WARN(dev, "%s: page mapping is still live!\n", __func__);
829 percpu_ref_put(&devmem->ref);
830 }
831
832 /* pages are dead and unused, undo the arch mapping */
833 start_pfn = (resource->start & ~(PA_SECTION_SIZE - 1)) >> PAGE_SHIFT;
834 npages = ALIGN(resource_size(resource), PA_SECTION_SIZE) >> PAGE_SHIFT;
835
836 page = pfn_to_page(start_pfn);
837 zone = page_zone(page);
838
839 mem_hotplug_begin();
Jérôme Glissed3df0a42017-09-08 16:12:28 -0700840 if (resource->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY)
Christoph Hellwigda024512017-12-29 08:53:55 +0100841 __remove_pages(zone, start_pfn, npages, NULL);
Jérôme Glissed3df0a42017-09-08 16:12:28 -0700842 else
843 arch_remove_memory(start_pfn << PAGE_SHIFT,
Christoph Hellwigda024512017-12-29 08:53:55 +0100844 npages << PAGE_SHIFT, NULL);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700845 mem_hotplug_done();
846
847 hmm_devmem_radix_release(resource);
848}
849
850static struct hmm_devmem *hmm_devmem_find(resource_size_t phys)
851{
852 WARN_ON_ONCE(!rcu_read_lock_held());
853
854 return radix_tree_lookup(&hmm_devmem_radix, phys >> PA_SECTION_SHIFT);
855}
856
857static int hmm_devmem_pages_create(struct hmm_devmem *devmem)
858{
859 resource_size_t key, align_start, align_size, align_end;
860 struct device *device = devmem->device;
861 int ret, nid, is_ram;
862 unsigned long pfn;
863
864 align_start = devmem->resource->start & ~(PA_SECTION_SIZE - 1);
865 align_size = ALIGN(devmem->resource->start +
866 resource_size(devmem->resource),
867 PA_SECTION_SIZE) - align_start;
868
869 is_ram = region_intersects(align_start, align_size,
870 IORESOURCE_SYSTEM_RAM,
871 IORES_DESC_NONE);
872 if (is_ram == REGION_MIXED) {
873 WARN_ONCE(1, "%s attempted on mixed region %pr\n",
874 __func__, devmem->resource);
875 return -ENXIO;
876 }
877 if (is_ram == REGION_INTERSECTS)
878 return -ENXIO;
879
Jérôme Glissed3df0a42017-09-08 16:12:28 -0700880 if (devmem->resource->desc == IORES_DESC_DEVICE_PUBLIC_MEMORY)
881 devmem->pagemap.type = MEMORY_DEVICE_PUBLIC;
882 else
883 devmem->pagemap.type = MEMORY_DEVICE_PRIVATE;
884
Logan Gunthorpee7744aa2017-12-29 08:54:04 +0100885 devmem->pagemap.res = *devmem->resource;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700886 devmem->pagemap.page_fault = hmm_devmem_fault;
887 devmem->pagemap.page_free = hmm_devmem_free;
888 devmem->pagemap.dev = devmem->device;
889 devmem->pagemap.ref = &devmem->ref;
890 devmem->pagemap.data = devmem;
891
892 mutex_lock(&hmm_devmem_lock);
893 align_end = align_start + align_size - 1;
894 for (key = align_start; key <= align_end; key += PA_SECTION_SIZE) {
895 struct hmm_devmem *dup;
896
897 rcu_read_lock();
898 dup = hmm_devmem_find(key);
899 rcu_read_unlock();
900 if (dup) {
901 dev_err(device, "%s: collides with mapping for %s\n",
902 __func__, dev_name(dup->device));
903 mutex_unlock(&hmm_devmem_lock);
904 ret = -EBUSY;
905 goto error;
906 }
907 ret = radix_tree_insert(&hmm_devmem_radix,
908 key >> PA_SECTION_SHIFT,
909 devmem);
910 if (ret) {
911 dev_err(device, "%s: failed: %d\n", __func__, ret);
912 mutex_unlock(&hmm_devmem_lock);
913 goto error_radix;
914 }
915 }
916 mutex_unlock(&hmm_devmem_lock);
917
918 nid = dev_to_node(device);
919 if (nid < 0)
920 nid = numa_mem_id();
921
922 mem_hotplug_begin();
923 /*
924 * For device private memory we call add_pages() as we only need to
925 * allocate and initialize struct page for the device memory. More-
926 * over the device memory is un-accessible thus we do not want to
927 * create a linear mapping for the memory like arch_add_memory()
928 * would do.
Jérôme Glissed3df0a42017-09-08 16:12:28 -0700929 *
930 * For device public memory, which is accesible by the CPU, we do
931 * want the linear mapping and thus use arch_add_memory().
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700932 */
Jérôme Glissed3df0a42017-09-08 16:12:28 -0700933 if (devmem->pagemap.type == MEMORY_DEVICE_PUBLIC)
Christoph Hellwig24e6d5a2017-12-29 08:53:53 +0100934 ret = arch_add_memory(nid, align_start, align_size, NULL,
935 false);
Jérôme Glissed3df0a42017-09-08 16:12:28 -0700936 else
937 ret = add_pages(nid, align_start >> PAGE_SHIFT,
Christoph Hellwig24e6d5a2017-12-29 08:53:53 +0100938 align_size >> PAGE_SHIFT, NULL, false);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700939 if (ret) {
940 mem_hotplug_done();
941 goto error_add_memory;
942 }
943 move_pfn_range_to_zone(&NODE_DATA(nid)->node_zones[ZONE_DEVICE],
944 align_start >> PAGE_SHIFT,
Christoph Hellwiga99583e2017-12-29 08:53:57 +0100945 align_size >> PAGE_SHIFT, NULL);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700946 mem_hotplug_done();
947
948 for (pfn = devmem->pfn_first; pfn < devmem->pfn_last; pfn++) {
949 struct page *page = pfn_to_page(pfn);
950
951 page->pgmap = &devmem->pagemap;
952 }
953 return 0;
954
955error_add_memory:
956 untrack_pfn(NULL, PHYS_PFN(align_start), align_size);
957error_radix:
958 hmm_devmem_radix_release(devmem->resource);
959error:
960 return ret;
961}
962
963static int hmm_devmem_match(struct device *dev, void *data, void *match_data)
964{
965 struct hmm_devmem *devmem = data;
966
967 return devmem->resource == match_data;
968}
969
970static void hmm_devmem_pages_remove(struct hmm_devmem *devmem)
971{
972 devres_release(devmem->device, &hmm_devmem_release,
973 &hmm_devmem_match, devmem->resource);
974}
975
976/*
977 * hmm_devmem_add() - hotplug ZONE_DEVICE memory for device memory
978 *
979 * @ops: memory event device driver callback (see struct hmm_devmem_ops)
980 * @device: device struct to bind the resource too
981 * @size: size in bytes of the device memory to add
982 * Returns: pointer to new hmm_devmem struct ERR_PTR otherwise
983 *
984 * This function first finds an empty range of physical address big enough to
985 * contain the new resource, and then hotplugs it as ZONE_DEVICE memory, which
986 * in turn allocates struct pages. It does not do anything beyond that; all
987 * events affecting the memory will go through the various callbacks provided
988 * by hmm_devmem_ops struct.
989 *
990 * Device driver should call this function during device initialization and
991 * is then responsible of memory management. HMM only provides helpers.
992 */
993struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops,
994 struct device *device,
995 unsigned long size)
996{
997 struct hmm_devmem *devmem;
998 resource_size_t addr;
999 int ret;
1000
1001 static_branch_enable(&device_private_key);
1002
1003 devmem = devres_alloc_node(&hmm_devmem_release, sizeof(*devmem),
1004 GFP_KERNEL, dev_to_node(device));
1005 if (!devmem)
1006 return ERR_PTR(-ENOMEM);
1007
1008 init_completion(&devmem->completion);
1009 devmem->pfn_first = -1UL;
1010 devmem->pfn_last = -1UL;
1011 devmem->resource = NULL;
1012 devmem->device = device;
1013 devmem->ops = ops;
1014
1015 ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release,
1016 0, GFP_KERNEL);
1017 if (ret)
1018 goto error_percpu_ref;
1019
1020 ret = devm_add_action(device, hmm_devmem_ref_exit, &devmem->ref);
1021 if (ret)
1022 goto error_devm_add_action;
1023
1024 size = ALIGN(size, PA_SECTION_SIZE);
1025 addr = min((unsigned long)iomem_resource.end,
1026 (1UL << MAX_PHYSMEM_BITS) - 1);
1027 addr = addr - size + 1UL;
1028
1029 /*
1030 * FIXME add a new helper to quickly walk resource tree and find free
1031 * range
1032 *
1033 * FIXME what about ioport_resource resource ?
1034 */
1035 for (; addr > size && addr >= iomem_resource.start; addr -= size) {
1036 ret = region_intersects(addr, size, 0, IORES_DESC_NONE);
1037 if (ret != REGION_DISJOINT)
1038 continue;
1039
1040 devmem->resource = devm_request_mem_region(device, addr, size,
1041 dev_name(device));
1042 if (!devmem->resource) {
1043 ret = -ENOMEM;
1044 goto error_no_resource;
1045 }
1046 break;
1047 }
1048 if (!devmem->resource) {
1049 ret = -ERANGE;
1050 goto error_no_resource;
1051 }
1052
1053 devmem->resource->desc = IORES_DESC_DEVICE_PRIVATE_MEMORY;
1054 devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
1055 devmem->pfn_last = devmem->pfn_first +
1056 (resource_size(devmem->resource) >> PAGE_SHIFT);
1057
1058 ret = hmm_devmem_pages_create(devmem);
1059 if (ret)
1060 goto error_pages;
1061
1062 devres_add(device, devmem);
1063
1064 ret = devm_add_action(device, hmm_devmem_ref_kill, &devmem->ref);
1065 if (ret) {
1066 hmm_devmem_remove(devmem);
1067 return ERR_PTR(ret);
1068 }
1069
1070 return devmem;
1071
1072error_pages:
1073 devm_release_mem_region(device, devmem->resource->start,
1074 resource_size(devmem->resource));
1075error_no_resource:
1076error_devm_add_action:
1077 hmm_devmem_ref_kill(&devmem->ref);
1078 hmm_devmem_ref_exit(&devmem->ref);
1079error_percpu_ref:
1080 devres_free(devmem);
1081 return ERR_PTR(ret);
1082}
1083EXPORT_SYMBOL(hmm_devmem_add);
1084
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001085struct hmm_devmem *hmm_devmem_add_resource(const struct hmm_devmem_ops *ops,
1086 struct device *device,
1087 struct resource *res)
1088{
1089 struct hmm_devmem *devmem;
1090 int ret;
1091
1092 if (res->desc != IORES_DESC_DEVICE_PUBLIC_MEMORY)
1093 return ERR_PTR(-EINVAL);
1094
1095 static_branch_enable(&device_private_key);
1096
1097 devmem = devres_alloc_node(&hmm_devmem_release, sizeof(*devmem),
1098 GFP_KERNEL, dev_to_node(device));
1099 if (!devmem)
1100 return ERR_PTR(-ENOMEM);
1101
1102 init_completion(&devmem->completion);
1103 devmem->pfn_first = -1UL;
1104 devmem->pfn_last = -1UL;
1105 devmem->resource = res;
1106 devmem->device = device;
1107 devmem->ops = ops;
1108
1109 ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release,
1110 0, GFP_KERNEL);
1111 if (ret)
1112 goto error_percpu_ref;
1113
1114 ret = devm_add_action(device, hmm_devmem_ref_exit, &devmem->ref);
1115 if (ret)
1116 goto error_devm_add_action;
1117
1118
1119 devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
1120 devmem->pfn_last = devmem->pfn_first +
1121 (resource_size(devmem->resource) >> PAGE_SHIFT);
1122
1123 ret = hmm_devmem_pages_create(devmem);
1124 if (ret)
1125 goto error_devm_add_action;
1126
1127 devres_add(device, devmem);
1128
1129 ret = devm_add_action(device, hmm_devmem_ref_kill, &devmem->ref);
1130 if (ret) {
1131 hmm_devmem_remove(devmem);
1132 return ERR_PTR(ret);
1133 }
1134
1135 return devmem;
1136
1137error_devm_add_action:
1138 hmm_devmem_ref_kill(&devmem->ref);
1139 hmm_devmem_ref_exit(&devmem->ref);
1140error_percpu_ref:
1141 devres_free(devmem);
1142 return ERR_PTR(ret);
1143}
1144EXPORT_SYMBOL(hmm_devmem_add_resource);
1145
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001146/*
1147 * hmm_devmem_remove() - remove device memory (kill and free ZONE_DEVICE)
1148 *
1149 * @devmem: hmm_devmem struct use to track and manage the ZONE_DEVICE memory
1150 *
1151 * This will hot-unplug memory that was hotplugged by hmm_devmem_add on behalf
1152 * of the device driver. It will free struct page and remove the resource that
1153 * reserved the physical address range for this device memory.
1154 */
1155void hmm_devmem_remove(struct hmm_devmem *devmem)
1156{
1157 resource_size_t start, size;
1158 struct device *device;
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001159 bool cdm = false;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001160
1161 if (!devmem)
1162 return;
1163
1164 device = devmem->device;
1165 start = devmem->resource->start;
1166 size = resource_size(devmem->resource);
1167
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001168 cdm = devmem->resource->desc == IORES_DESC_DEVICE_PUBLIC_MEMORY;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001169 hmm_devmem_ref_kill(&devmem->ref);
1170 hmm_devmem_ref_exit(&devmem->ref);
1171 hmm_devmem_pages_remove(devmem);
1172
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001173 if (!cdm)
1174 devm_release_mem_region(device, start, size);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001175}
1176EXPORT_SYMBOL(hmm_devmem_remove);
Jérôme Glisse858b54d2017-09-08 16:12:02 -07001177
1178/*
1179 * A device driver that wants to handle multiple devices memory through a
1180 * single fake device can use hmm_device to do so. This is purely a helper
1181 * and it is not needed to make use of any HMM functionality.
1182 */
1183#define HMM_DEVICE_MAX 256
1184
1185static DECLARE_BITMAP(hmm_device_mask, HMM_DEVICE_MAX);
1186static DEFINE_SPINLOCK(hmm_device_lock);
1187static struct class *hmm_device_class;
1188static dev_t hmm_device_devt;
1189
1190static void hmm_device_release(struct device *device)
1191{
1192 struct hmm_device *hmm_device;
1193
1194 hmm_device = container_of(device, struct hmm_device, device);
1195 spin_lock(&hmm_device_lock);
1196 clear_bit(hmm_device->minor, hmm_device_mask);
1197 spin_unlock(&hmm_device_lock);
1198
1199 kfree(hmm_device);
1200}
1201
1202struct hmm_device *hmm_device_new(void *drvdata)
1203{
1204 struct hmm_device *hmm_device;
1205
1206 hmm_device = kzalloc(sizeof(*hmm_device), GFP_KERNEL);
1207 if (!hmm_device)
1208 return ERR_PTR(-ENOMEM);
1209
1210 spin_lock(&hmm_device_lock);
1211 hmm_device->minor = find_first_zero_bit(hmm_device_mask, HMM_DEVICE_MAX);
1212 if (hmm_device->minor >= HMM_DEVICE_MAX) {
1213 spin_unlock(&hmm_device_lock);
1214 kfree(hmm_device);
1215 return ERR_PTR(-EBUSY);
1216 }
1217 set_bit(hmm_device->minor, hmm_device_mask);
1218 spin_unlock(&hmm_device_lock);
1219
1220 dev_set_name(&hmm_device->device, "hmm_device%d", hmm_device->minor);
1221 hmm_device->device.devt = MKDEV(MAJOR(hmm_device_devt),
1222 hmm_device->minor);
1223 hmm_device->device.release = hmm_device_release;
1224 dev_set_drvdata(&hmm_device->device, drvdata);
1225 hmm_device->device.class = hmm_device_class;
1226 device_initialize(&hmm_device->device);
1227
1228 return hmm_device;
1229}
1230EXPORT_SYMBOL(hmm_device_new);
1231
1232void hmm_device_put(struct hmm_device *hmm_device)
1233{
1234 put_device(&hmm_device->device);
1235}
1236EXPORT_SYMBOL(hmm_device_put);
1237
1238static int __init hmm_init(void)
1239{
1240 int ret;
1241
1242 ret = alloc_chrdev_region(&hmm_device_devt, 0,
1243 HMM_DEVICE_MAX,
1244 "hmm_device");
1245 if (ret)
1246 return ret;
1247
1248 hmm_device_class = class_create(THIS_MODULE, "hmm_device");
1249 if (IS_ERR(hmm_device_class)) {
1250 unregister_chrdev_region(hmm_device_devt, HMM_DEVICE_MAX);
1251 return PTR_ERR(hmm_device_class);
1252 }
1253 return 0;
1254}
1255
1256device_initcall(hmm_init);
Jérôme Glissedf6ad692017-09-08 16:12:24 -07001257#endif /* CONFIG_DEVICE_PRIVATE || CONFIG_DEVICE_PUBLIC */