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