blob: fc5057d7aa058770c297e262b19825cc9a4e9340 [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
Ralph Campbelle1401512018-04-10 16:28:19 -0700163static 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
Jérôme Glissec0b12402017-09-08 16:11:27 -0700189static 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 = {
Ralph Campbelle1401512018-04-10 16:28:19 -0700214 .release = hmm_release,
Jérôme Glissec0b12402017-09-08 16:11:27 -0700215 .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
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700236again:
Jérôme Glissec0b12402017-09-08 16:11:27 -0700237 mirror->hmm = hmm_register(mm);
238 if (!mirror->hmm)
239 return -ENOMEM;
240
241 down_write(&mirror->hmm->mirrors_sem);
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700242 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 }
Jérôme Glissec0b12402017-09-08 16:11:27 -0700254
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{
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700268 bool should_unregister = false;
269 struct mm_struct *mm;
270 struct hmm *hmm;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700271
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700272 if (mirror->hmm == NULL)
273 return;
274
275 hmm = mirror->hmm;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700276 down_write(&hmm->mirrors_sem);
Ralph Campbelle1401512018-04-10 16:28:19 -0700277 list_del_init(&mirror->list);
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700278 should_unregister = list_empty(&hmm->mirrors);
279 mirror->hmm = NULL;
280 mm = hmm->mm;
281 hmm->mm = NULL;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700282 up_write(&hmm->mirrors_sem);
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700283
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);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700294}
295EXPORT_SYMBOL(hmm_mirror_unregister);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700296
Jérôme Glisse74eee182017-09-08 16:11:35 -0700297struct 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
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700327static 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{
Jérôme Glissec7195472018-04-10 16:28:27 -0700339 struct hmm_vma_walk *hmm_vma_walk = walk->private;
340 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700341 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
Jérôme Glisse74eee182017-09-08 16:11:35 -0700351static 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
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700359static int hmm_vma_walk_hole(unsigned long addr,
360 unsigned long end,
361 struct mm_walk *walk)
362{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700363 struct hmm_vma_walk *hmm_vma_walk = walk->private;
364 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700365 hmm_pfn_t *pfns = range->pfns;
366 unsigned long i;
367
Jérôme Glisse74eee182017-09-08 16:11:35 -0700368 hmm_vma_walk->last = addr;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700369 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700370 for (; addr < end; addr += PAGE_SIZE, i++) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700371 pfns[i] = HMM_PFN_EMPTY;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700372 if (hmm_vma_walk->fault) {
373 int ret;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700374
Jérôme Glisse74eee182017-09-08 16:11:35 -0700375 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;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700382}
383
384static int hmm_vma_walk_clear(unsigned long addr,
385 unsigned long end,
386 struct mm_walk *walk)
387{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700388 struct hmm_vma_walk *hmm_vma_walk = walk->private;
389 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700390 hmm_pfn_t *pfns = range->pfns;
391 unsigned long i;
392
Jérôme Glisse74eee182017-09-08 16:11:35 -0700393 hmm_vma_walk->last = addr;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700394 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700395 for (; addr < end; addr += PAGE_SIZE, i++) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700396 pfns[i] = 0;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700397 if (hmm_vma_walk->fault) {
398 int ret;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700399
Jérôme Glisse74eee182017-09-08 16:11:35 -0700400 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;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700407}
408
409static int hmm_vma_walk_pmd(pmd_t *pmdp,
410 unsigned long start,
411 unsigned long end,
412 struct mm_walk *walk)
413{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700414 struct hmm_vma_walk *hmm_vma_walk = walk->private;
415 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700416 struct vm_area_struct *vma = walk->vma;
417 hmm_pfn_t *pfns = range->pfns;
418 unsigned long addr = start, i;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700419 bool write_fault;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700420 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;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700425 write_fault = hmm_vma_walk->fault & hmm_vma_walk->write;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700426
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
Linus Torvaldsf6f37322017-12-15 18:53:22 -0800454 if (write_fault && !pmd_write(pmd))
Jérôme Glisse74eee182017-09-08 16:11:35 -0700455 return hmm_vma_walk_clear(start, end, walk);
456
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700457 pfn = pmd_pfn(pmd) + pte_index(addr);
Linus Torvaldsf6f37322017-12-15 18:53:22 -0800458 flag |= pmd_write(pmd) ? HMM_PFN_WRITE : 0;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700459 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
Jérôme Glisse74eee182017-09-08 16:11:35 -0700473 if (pte_none(pte)) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700474 pfns[i] = HMM_PFN_EMPTY;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700475 if (hmm_vma_walk->fault)
476 goto fault;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700477 continue;
478 }
479
Jérôme Glisse74eee182017-09-08 16:11:35 -0700480 if (!pte_present(pte)) {
Ralph Campbell8d63e4c2018-01-31 16:20:30 -0800481 swp_entry_t entry = pte_to_swp_entry(pte);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700482
483 if (!non_swap_entry(entry)) {
484 if (hmm_vma_walk->fault)
485 goto fault;
486 continue;
487 }
488
Jérôme Glisse74eee182017-09-08 16:11:35 -0700489 /*
490 * This is a special swap entry, ignore migration, use
491 * device and report anything else as error.
492 */
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700493 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)) {
Jérôme Glisse74eee182017-09-08 16:11:35 -0700502 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
Linus Torvaldsf6f37322017-12-15 18:53:22 -0800517 if (write_fault && !pte_write(pte))
Jérôme Glisse74eee182017-09-08 16:11:35 -0700518 goto fault;
519
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700520 pfns[i] = hmm_pfn_t_from_pfn(pte_pfn(pte)) | flag;
Linus Torvaldsf6f37322017-12-15 18:53:22 -0800521 pfns[i] |= pte_write(pte) ? HMM_PFN_WRITE : 0;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700522 continue;
523
524fault:
525 pte_unmap(ptep);
526 /* Fault all pages in range */
527 return hmm_vma_walk_clear(start, end, walk);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700528 }
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
Jérôme Glisse08232a42018-04-10 16:28:30 -0700536 * @range: range being snapshotted
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700537 * Returns: -EINVAL if invalid argument, -ENOMEM out of memory, 0 success
538 *
539 * This snapshots the CPU page table for a range of virtual addresses. Snapshot
540 * validity is tracked by range struct. See hmm_vma_range_done() for further
541 * information.
542 *
543 * The range struct is initialized here. It tracks the CPU page table, but only
544 * if the function returns success (0), in which case the caller must then call
545 * hmm_vma_range_done() to stop CPU page table update tracking on this range.
546 *
547 * NOT CALLING hmm_vma_range_done() IF FUNCTION RETURNS 0 WILL LEAD TO SERIOUS
548 * MEMORY CORRUPTION ! YOU HAVE BEEN WARNED !
549 */
Jérôme Glisse08232a42018-04-10 16:28:30 -0700550int hmm_vma_get_pfns(struct hmm_range *range)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700551{
Jérôme Glisse08232a42018-04-10 16:28:30 -0700552 struct vm_area_struct *vma = range->vma;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700553 struct hmm_vma_walk hmm_vma_walk;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700554 struct mm_walk mm_walk;
555 struct hmm *hmm;
556
557 /* FIXME support hugetlb fs */
558 if (is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_SPECIAL)) {
Jérôme Glisse08232a42018-04-10 16:28:30 -0700559 hmm_pfns_special(range->pfns, range->start, range->end);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700560 return -EINVAL;
561 }
562
563 /* Sanity check, this really should not happen ! */
Jérôme Glisse08232a42018-04-10 16:28:30 -0700564 if (range->start < vma->vm_start || range->start >= vma->vm_end)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700565 return -EINVAL;
Jérôme Glisse08232a42018-04-10 16:28:30 -0700566 if (range->end < vma->vm_start || range->end > vma->vm_end)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700567 return -EINVAL;
568
569 hmm = hmm_register(vma->vm_mm);
570 if (!hmm)
571 return -ENOMEM;
572 /* Caller must have registered a mirror, via hmm_mirror_register() ! */
573 if (!hmm->mmu_notifier.ops)
574 return -EINVAL;
575
576 /* Initialize range to track CPU page table update */
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700577 spin_lock(&hmm->lock);
578 range->valid = true;
579 list_add_rcu(&range->list, &hmm->ranges);
580 spin_unlock(&hmm->lock);
581
Jérôme Glisse74eee182017-09-08 16:11:35 -0700582 hmm_vma_walk.fault = false;
583 hmm_vma_walk.range = range;
584 mm_walk.private = &hmm_vma_walk;
585
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700586 mm_walk.vma = vma;
587 mm_walk.mm = vma->vm_mm;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700588 mm_walk.pte_entry = NULL;
589 mm_walk.test_walk = NULL;
590 mm_walk.hugetlb_entry = NULL;
591 mm_walk.pmd_entry = hmm_vma_walk_pmd;
592 mm_walk.pte_hole = hmm_vma_walk_hole;
593
Jérôme Glisse08232a42018-04-10 16:28:30 -0700594 walk_page_range(range->start, range->end, &mm_walk);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700595 return 0;
596}
597EXPORT_SYMBOL(hmm_vma_get_pfns);
598
599/*
600 * hmm_vma_range_done() - stop tracking change to CPU page table over a range
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700601 * @range: range being tracked
602 * Returns: false if range data has been invalidated, true otherwise
603 *
604 * Range struct is used to track updates to the CPU page table after a call to
605 * either hmm_vma_get_pfns() or hmm_vma_fault(). Once the device driver is done
606 * using the data, or wants to lock updates to the data it got from those
607 * functions, it must call the hmm_vma_range_done() function, which will then
608 * stop tracking CPU page table updates.
609 *
610 * Note that device driver must still implement general CPU page table update
611 * tracking either by using hmm_mirror (see hmm_mirror_register()) or by using
612 * the mmu_notifier API directly.
613 *
614 * CPU page table update tracking done through hmm_range is only temporary and
615 * to be used while trying to duplicate CPU page table contents for a range of
616 * virtual addresses.
617 *
618 * There are two ways to use this :
619 * again:
Jérôme Glisse08232a42018-04-10 16:28:30 -0700620 * hmm_vma_get_pfns(range); or hmm_vma_fault(...);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700621 * trans = device_build_page_table_update_transaction(pfns);
622 * device_page_table_lock();
Jérôme Glisse08232a42018-04-10 16:28:30 -0700623 * if (!hmm_vma_range_done(range)) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700624 * device_page_table_unlock();
625 * goto again;
626 * }
627 * device_commit_transaction(trans);
628 * device_page_table_unlock();
629 *
630 * Or:
Jérôme Glisse08232a42018-04-10 16:28:30 -0700631 * hmm_vma_get_pfns(range); or hmm_vma_fault(...);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700632 * device_page_table_lock();
Jérôme Glisse08232a42018-04-10 16:28:30 -0700633 * hmm_vma_range_done(range);
634 * device_update_page_table(range->pfns);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700635 * device_page_table_unlock();
636 */
Jérôme Glisse08232a42018-04-10 16:28:30 -0700637bool hmm_vma_range_done(struct hmm_range *range)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700638{
639 unsigned long npages = (range->end - range->start) >> PAGE_SHIFT;
640 struct hmm *hmm;
641
642 if (range->end <= range->start) {
643 BUG();
644 return false;
645 }
646
Jérôme Glisse08232a42018-04-10 16:28:30 -0700647 hmm = hmm_register(range->vma->vm_mm);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700648 if (!hmm) {
649 memset(range->pfns, 0, sizeof(*range->pfns) * npages);
650 return false;
651 }
652
653 spin_lock(&hmm->lock);
654 list_del_rcu(&range->list);
655 spin_unlock(&hmm->lock);
656
657 return range->valid;
658}
659EXPORT_SYMBOL(hmm_vma_range_done);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700660
661/*
662 * hmm_vma_fault() - try to fault some address in a virtual address range
Jérôme Glisse08232a42018-04-10 16:28:30 -0700663 * @range: range being faulted
Jérôme Glisse74eee182017-09-08 16:11:35 -0700664 * @write: is it a write fault
665 * @block: allow blocking on fault (if true it sleeps and do not drop mmap_sem)
666 * Returns: 0 success, error otherwise (-EAGAIN means mmap_sem have been drop)
667 *
668 * This is similar to a regular CPU page fault except that it will not trigger
669 * any memory migration if the memory being faulted is not accessible by CPUs.
670 *
671 * On error, for one virtual address in the range, the function will set the
672 * hmm_pfn_t error flag for the corresponding pfn entry.
673 *
674 * Expected use pattern:
675 * retry:
676 * down_read(&mm->mmap_sem);
677 * // Find vma and address device wants to fault, initialize hmm_pfn_t
678 * // array accordingly
Jérôme Glisse08232a42018-04-10 16:28:30 -0700679 * ret = hmm_vma_fault(range, write, block);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700680 * switch (ret) {
681 * case -EAGAIN:
Jérôme Glisse08232a42018-04-10 16:28:30 -0700682 * hmm_vma_range_done(range);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700683 * // You might want to rate limit or yield to play nicely, you may
684 * // also commit any valid pfn in the array assuming that you are
685 * // getting true from hmm_vma_range_monitor_end()
686 * goto retry;
687 * case 0:
688 * break;
689 * default:
690 * // Handle error !
691 * up_read(&mm->mmap_sem)
692 * return;
693 * }
694 * // Take device driver lock that serialize device page table update
695 * driver_lock_device_page_table_update();
Jérôme Glisse08232a42018-04-10 16:28:30 -0700696 * hmm_vma_range_done(range);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700697 * // Commit pfns we got from hmm_vma_fault()
698 * driver_unlock_device_page_table_update();
699 * up_read(&mm->mmap_sem)
700 *
701 * YOU MUST CALL hmm_vma_range_done() AFTER THIS FUNCTION RETURN SUCCESS (0)
702 * BEFORE FREEING THE range struct OR YOU WILL HAVE SERIOUS MEMORY CORRUPTION !
703 *
704 * YOU HAVE BEEN WARNED !
705 */
Jérôme Glisse08232a42018-04-10 16:28:30 -0700706int hmm_vma_fault(struct hmm_range *range, bool write, bool block)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700707{
Jérôme Glisse08232a42018-04-10 16:28:30 -0700708 struct vm_area_struct *vma = range->vma;
709 unsigned long start = range->start;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700710 struct hmm_vma_walk hmm_vma_walk;
711 struct mm_walk mm_walk;
712 struct hmm *hmm;
713 int ret;
714
715 /* Sanity check, this really should not happen ! */
Jérôme Glisse08232a42018-04-10 16:28:30 -0700716 if (range->start < vma->vm_start || range->start >= vma->vm_end)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700717 return -EINVAL;
Jérôme Glisse08232a42018-04-10 16:28:30 -0700718 if (range->end < vma->vm_start || range->end > vma->vm_end)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700719 return -EINVAL;
720
721 hmm = hmm_register(vma->vm_mm);
722 if (!hmm) {
Jérôme Glisse08232a42018-04-10 16:28:30 -0700723 hmm_pfns_clear(range->pfns, range->start, range->end);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700724 return -ENOMEM;
725 }
726 /* Caller must have registered a mirror using hmm_mirror_register() */
727 if (!hmm->mmu_notifier.ops)
728 return -EINVAL;
729
730 /* Initialize range to track CPU page table update */
Jérôme Glisse74eee182017-09-08 16:11:35 -0700731 spin_lock(&hmm->lock);
732 range->valid = true;
733 list_add_rcu(&range->list, &hmm->ranges);
734 spin_unlock(&hmm->lock);
735
736 /* FIXME support hugetlb fs */
737 if (is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_SPECIAL)) {
Jérôme Glisse08232a42018-04-10 16:28:30 -0700738 hmm_pfns_special(range->pfns, range->start, range->end);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700739 return 0;
740 }
741
742 hmm_vma_walk.fault = true;
743 hmm_vma_walk.write = write;
744 hmm_vma_walk.block = block;
745 hmm_vma_walk.range = range;
746 mm_walk.private = &hmm_vma_walk;
747 hmm_vma_walk.last = range->start;
748
749 mm_walk.vma = vma;
750 mm_walk.mm = vma->vm_mm;
751 mm_walk.pte_entry = NULL;
752 mm_walk.test_walk = NULL;
753 mm_walk.hugetlb_entry = NULL;
754 mm_walk.pmd_entry = hmm_vma_walk_pmd;
755 mm_walk.pte_hole = hmm_vma_walk_hole;
756
757 do {
Jérôme Glisse08232a42018-04-10 16:28:30 -0700758 ret = walk_page_range(start, range->end, &mm_walk);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700759 start = hmm_vma_walk.last;
760 } while (ret == -EAGAIN);
761
762 if (ret) {
763 unsigned long i;
764
765 i = (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
Jérôme Glisse08232a42018-04-10 16:28:30 -0700766 hmm_pfns_clear(&range->pfns[i], hmm_vma_walk.last, range->end);
767 hmm_vma_range_done(range);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700768 }
769 return ret;
770}
771EXPORT_SYMBOL(hmm_vma_fault);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700772#endif /* IS_ENABLED(CONFIG_HMM_MIRROR) */
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700773
774
Jérôme Glissedf6ad692017-09-08 16:12:24 -0700775#if IS_ENABLED(CONFIG_DEVICE_PRIVATE) || IS_ENABLED(CONFIG_DEVICE_PUBLIC)
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700776struct page *hmm_vma_alloc_locked_page(struct vm_area_struct *vma,
777 unsigned long addr)
778{
779 struct page *page;
780
781 page = alloc_page_vma(GFP_HIGHUSER, vma, addr);
782 if (!page)
783 return NULL;
784 lock_page(page);
785 return page;
786}
787EXPORT_SYMBOL(hmm_vma_alloc_locked_page);
788
789
790static void hmm_devmem_ref_release(struct percpu_ref *ref)
791{
792 struct hmm_devmem *devmem;
793
794 devmem = container_of(ref, struct hmm_devmem, ref);
795 complete(&devmem->completion);
796}
797
798static void hmm_devmem_ref_exit(void *data)
799{
800 struct percpu_ref *ref = data;
801 struct hmm_devmem *devmem;
802
803 devmem = container_of(ref, struct hmm_devmem, ref);
804 percpu_ref_exit(ref);
805 devm_remove_action(devmem->device, &hmm_devmem_ref_exit, data);
806}
807
808static void hmm_devmem_ref_kill(void *data)
809{
810 struct percpu_ref *ref = data;
811 struct hmm_devmem *devmem;
812
813 devmem = container_of(ref, struct hmm_devmem, ref);
814 percpu_ref_kill(ref);
815 wait_for_completion(&devmem->completion);
816 devm_remove_action(devmem->device, &hmm_devmem_ref_kill, data);
817}
818
819static int hmm_devmem_fault(struct vm_area_struct *vma,
820 unsigned long addr,
821 const struct page *page,
822 unsigned int flags,
823 pmd_t *pmdp)
824{
825 struct hmm_devmem *devmem = page->pgmap->data;
826
827 return devmem->ops->fault(devmem, vma, addr, page, flags, pmdp);
828}
829
830static void hmm_devmem_free(struct page *page, void *data)
831{
832 struct hmm_devmem *devmem = data;
833
834 devmem->ops->free(devmem, page);
835}
836
837static DEFINE_MUTEX(hmm_devmem_lock);
838static RADIX_TREE(hmm_devmem_radix, GFP_KERNEL);
839
840static void hmm_devmem_radix_release(struct resource *resource)
841{
Colin Ian Kingfec11bc2017-11-15 17:38:52 -0800842 resource_size_t key, align_start, align_size;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700843
844 align_start = resource->start & ~(PA_SECTION_SIZE - 1);
845 align_size = ALIGN(resource_size(resource), PA_SECTION_SIZE);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700846
847 mutex_lock(&hmm_devmem_lock);
848 for (key = resource->start;
849 key <= resource->end;
850 key += PA_SECTION_SIZE)
851 radix_tree_delete(&hmm_devmem_radix, key >> PA_SECTION_SHIFT);
852 mutex_unlock(&hmm_devmem_lock);
853}
854
855static void hmm_devmem_release(struct device *dev, void *data)
856{
857 struct hmm_devmem *devmem = data;
858 struct resource *resource = devmem->resource;
859 unsigned long start_pfn, npages;
860 struct zone *zone;
861 struct page *page;
862
863 if (percpu_ref_tryget_live(&devmem->ref)) {
864 dev_WARN(dev, "%s: page mapping is still live!\n", __func__);
865 percpu_ref_put(&devmem->ref);
866 }
867
868 /* pages are dead and unused, undo the arch mapping */
869 start_pfn = (resource->start & ~(PA_SECTION_SIZE - 1)) >> PAGE_SHIFT;
870 npages = ALIGN(resource_size(resource), PA_SECTION_SIZE) >> PAGE_SHIFT;
871
872 page = pfn_to_page(start_pfn);
873 zone = page_zone(page);
874
875 mem_hotplug_begin();
Jérôme Glissed3df0a42017-09-08 16:12:28 -0700876 if (resource->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY)
Christoph Hellwigda024512017-12-29 08:53:55 +0100877 __remove_pages(zone, start_pfn, npages, NULL);
Jérôme Glissed3df0a42017-09-08 16:12:28 -0700878 else
879 arch_remove_memory(start_pfn << PAGE_SHIFT,
Christoph Hellwigda024512017-12-29 08:53:55 +0100880 npages << PAGE_SHIFT, NULL);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700881 mem_hotplug_done();
882
883 hmm_devmem_radix_release(resource);
884}
885
886static struct hmm_devmem *hmm_devmem_find(resource_size_t phys)
887{
888 WARN_ON_ONCE(!rcu_read_lock_held());
889
890 return radix_tree_lookup(&hmm_devmem_radix, phys >> PA_SECTION_SHIFT);
891}
892
893static int hmm_devmem_pages_create(struct hmm_devmem *devmem)
894{
895 resource_size_t key, align_start, align_size, align_end;
896 struct device *device = devmem->device;
897 int ret, nid, is_ram;
898 unsigned long pfn;
899
900 align_start = devmem->resource->start & ~(PA_SECTION_SIZE - 1);
901 align_size = ALIGN(devmem->resource->start +
902 resource_size(devmem->resource),
903 PA_SECTION_SIZE) - align_start;
904
905 is_ram = region_intersects(align_start, align_size,
906 IORESOURCE_SYSTEM_RAM,
907 IORES_DESC_NONE);
908 if (is_ram == REGION_MIXED) {
909 WARN_ONCE(1, "%s attempted on mixed region %pr\n",
910 __func__, devmem->resource);
911 return -ENXIO;
912 }
913 if (is_ram == REGION_INTERSECTS)
914 return -ENXIO;
915
Jérôme Glissed3df0a42017-09-08 16:12:28 -0700916 if (devmem->resource->desc == IORES_DESC_DEVICE_PUBLIC_MEMORY)
917 devmem->pagemap.type = MEMORY_DEVICE_PUBLIC;
918 else
919 devmem->pagemap.type = MEMORY_DEVICE_PRIVATE;
920
Logan Gunthorpee7744aa2017-12-29 08:54:04 +0100921 devmem->pagemap.res = *devmem->resource;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700922 devmem->pagemap.page_fault = hmm_devmem_fault;
923 devmem->pagemap.page_free = hmm_devmem_free;
924 devmem->pagemap.dev = devmem->device;
925 devmem->pagemap.ref = &devmem->ref;
926 devmem->pagemap.data = devmem;
927
928 mutex_lock(&hmm_devmem_lock);
929 align_end = align_start + align_size - 1;
930 for (key = align_start; key <= align_end; key += PA_SECTION_SIZE) {
931 struct hmm_devmem *dup;
932
933 rcu_read_lock();
934 dup = hmm_devmem_find(key);
935 rcu_read_unlock();
936 if (dup) {
937 dev_err(device, "%s: collides with mapping for %s\n",
938 __func__, dev_name(dup->device));
939 mutex_unlock(&hmm_devmem_lock);
940 ret = -EBUSY;
941 goto error;
942 }
943 ret = radix_tree_insert(&hmm_devmem_radix,
944 key >> PA_SECTION_SHIFT,
945 devmem);
946 if (ret) {
947 dev_err(device, "%s: failed: %d\n", __func__, ret);
948 mutex_unlock(&hmm_devmem_lock);
949 goto error_radix;
950 }
951 }
952 mutex_unlock(&hmm_devmem_lock);
953
954 nid = dev_to_node(device);
955 if (nid < 0)
956 nid = numa_mem_id();
957
958 mem_hotplug_begin();
959 /*
960 * For device private memory we call add_pages() as we only need to
961 * allocate and initialize struct page for the device memory. More-
962 * over the device memory is un-accessible thus we do not want to
963 * create a linear mapping for the memory like arch_add_memory()
964 * would do.
Jérôme Glissed3df0a42017-09-08 16:12:28 -0700965 *
966 * For device public memory, which is accesible by the CPU, we do
967 * want the linear mapping and thus use arch_add_memory().
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700968 */
Jérôme Glissed3df0a42017-09-08 16:12:28 -0700969 if (devmem->pagemap.type == MEMORY_DEVICE_PUBLIC)
Christoph Hellwig24e6d5a2017-12-29 08:53:53 +0100970 ret = arch_add_memory(nid, align_start, align_size, NULL,
971 false);
Jérôme Glissed3df0a42017-09-08 16:12:28 -0700972 else
973 ret = add_pages(nid, align_start >> PAGE_SHIFT,
Christoph Hellwig24e6d5a2017-12-29 08:53:53 +0100974 align_size >> PAGE_SHIFT, NULL, false);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700975 if (ret) {
976 mem_hotplug_done();
977 goto error_add_memory;
978 }
979 move_pfn_range_to_zone(&NODE_DATA(nid)->node_zones[ZONE_DEVICE],
980 align_start >> PAGE_SHIFT,
Christoph Hellwiga99583e2017-12-29 08:53:57 +0100981 align_size >> PAGE_SHIFT, NULL);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700982 mem_hotplug_done();
983
984 for (pfn = devmem->pfn_first; pfn < devmem->pfn_last; pfn++) {
985 struct page *page = pfn_to_page(pfn);
986
987 page->pgmap = &devmem->pagemap;
988 }
989 return 0;
990
991error_add_memory:
992 untrack_pfn(NULL, PHYS_PFN(align_start), align_size);
993error_radix:
994 hmm_devmem_radix_release(devmem->resource);
995error:
996 return ret;
997}
998
999static int hmm_devmem_match(struct device *dev, void *data, void *match_data)
1000{
1001 struct hmm_devmem *devmem = data;
1002
1003 return devmem->resource == match_data;
1004}
1005
1006static void hmm_devmem_pages_remove(struct hmm_devmem *devmem)
1007{
1008 devres_release(devmem->device, &hmm_devmem_release,
1009 &hmm_devmem_match, devmem->resource);
1010}
1011
1012/*
1013 * hmm_devmem_add() - hotplug ZONE_DEVICE memory for device memory
1014 *
1015 * @ops: memory event device driver callback (see struct hmm_devmem_ops)
1016 * @device: device struct to bind the resource too
1017 * @size: size in bytes of the device memory to add
1018 * Returns: pointer to new hmm_devmem struct ERR_PTR otherwise
1019 *
1020 * This function first finds an empty range of physical address big enough to
1021 * contain the new resource, and then hotplugs it as ZONE_DEVICE memory, which
1022 * in turn allocates struct pages. It does not do anything beyond that; all
1023 * events affecting the memory will go through the various callbacks provided
1024 * by hmm_devmem_ops struct.
1025 *
1026 * Device driver should call this function during device initialization and
1027 * is then responsible of memory management. HMM only provides helpers.
1028 */
1029struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops,
1030 struct device *device,
1031 unsigned long size)
1032{
1033 struct hmm_devmem *devmem;
1034 resource_size_t addr;
1035 int ret;
1036
1037 static_branch_enable(&device_private_key);
1038
1039 devmem = devres_alloc_node(&hmm_devmem_release, sizeof(*devmem),
1040 GFP_KERNEL, dev_to_node(device));
1041 if (!devmem)
1042 return ERR_PTR(-ENOMEM);
1043
1044 init_completion(&devmem->completion);
1045 devmem->pfn_first = -1UL;
1046 devmem->pfn_last = -1UL;
1047 devmem->resource = NULL;
1048 devmem->device = device;
1049 devmem->ops = ops;
1050
1051 ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release,
1052 0, GFP_KERNEL);
1053 if (ret)
1054 goto error_percpu_ref;
1055
1056 ret = devm_add_action(device, hmm_devmem_ref_exit, &devmem->ref);
1057 if (ret)
1058 goto error_devm_add_action;
1059
1060 size = ALIGN(size, PA_SECTION_SIZE);
1061 addr = min((unsigned long)iomem_resource.end,
1062 (1UL << MAX_PHYSMEM_BITS) - 1);
1063 addr = addr - size + 1UL;
1064
1065 /*
1066 * FIXME add a new helper to quickly walk resource tree and find free
1067 * range
1068 *
1069 * FIXME what about ioport_resource resource ?
1070 */
1071 for (; addr > size && addr >= iomem_resource.start; addr -= size) {
1072 ret = region_intersects(addr, size, 0, IORES_DESC_NONE);
1073 if (ret != REGION_DISJOINT)
1074 continue;
1075
1076 devmem->resource = devm_request_mem_region(device, addr, size,
1077 dev_name(device));
1078 if (!devmem->resource) {
1079 ret = -ENOMEM;
1080 goto error_no_resource;
1081 }
1082 break;
1083 }
1084 if (!devmem->resource) {
1085 ret = -ERANGE;
1086 goto error_no_resource;
1087 }
1088
1089 devmem->resource->desc = IORES_DESC_DEVICE_PRIVATE_MEMORY;
1090 devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
1091 devmem->pfn_last = devmem->pfn_first +
1092 (resource_size(devmem->resource) >> PAGE_SHIFT);
1093
1094 ret = hmm_devmem_pages_create(devmem);
1095 if (ret)
1096 goto error_pages;
1097
1098 devres_add(device, devmem);
1099
1100 ret = devm_add_action(device, hmm_devmem_ref_kill, &devmem->ref);
1101 if (ret) {
1102 hmm_devmem_remove(devmem);
1103 return ERR_PTR(ret);
1104 }
1105
1106 return devmem;
1107
1108error_pages:
1109 devm_release_mem_region(device, devmem->resource->start,
1110 resource_size(devmem->resource));
1111error_no_resource:
1112error_devm_add_action:
1113 hmm_devmem_ref_kill(&devmem->ref);
1114 hmm_devmem_ref_exit(&devmem->ref);
1115error_percpu_ref:
1116 devres_free(devmem);
1117 return ERR_PTR(ret);
1118}
1119EXPORT_SYMBOL(hmm_devmem_add);
1120
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001121struct hmm_devmem *hmm_devmem_add_resource(const struct hmm_devmem_ops *ops,
1122 struct device *device,
1123 struct resource *res)
1124{
1125 struct hmm_devmem *devmem;
1126 int ret;
1127
1128 if (res->desc != IORES_DESC_DEVICE_PUBLIC_MEMORY)
1129 return ERR_PTR(-EINVAL);
1130
1131 static_branch_enable(&device_private_key);
1132
1133 devmem = devres_alloc_node(&hmm_devmem_release, sizeof(*devmem),
1134 GFP_KERNEL, dev_to_node(device));
1135 if (!devmem)
1136 return ERR_PTR(-ENOMEM);
1137
1138 init_completion(&devmem->completion);
1139 devmem->pfn_first = -1UL;
1140 devmem->pfn_last = -1UL;
1141 devmem->resource = res;
1142 devmem->device = device;
1143 devmem->ops = ops;
1144
1145 ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release,
1146 0, GFP_KERNEL);
1147 if (ret)
1148 goto error_percpu_ref;
1149
1150 ret = devm_add_action(device, hmm_devmem_ref_exit, &devmem->ref);
1151 if (ret)
1152 goto error_devm_add_action;
1153
1154
1155 devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
1156 devmem->pfn_last = devmem->pfn_first +
1157 (resource_size(devmem->resource) >> PAGE_SHIFT);
1158
1159 ret = hmm_devmem_pages_create(devmem);
1160 if (ret)
1161 goto error_devm_add_action;
1162
1163 devres_add(device, devmem);
1164
1165 ret = devm_add_action(device, hmm_devmem_ref_kill, &devmem->ref);
1166 if (ret) {
1167 hmm_devmem_remove(devmem);
1168 return ERR_PTR(ret);
1169 }
1170
1171 return devmem;
1172
1173error_devm_add_action:
1174 hmm_devmem_ref_kill(&devmem->ref);
1175 hmm_devmem_ref_exit(&devmem->ref);
1176error_percpu_ref:
1177 devres_free(devmem);
1178 return ERR_PTR(ret);
1179}
1180EXPORT_SYMBOL(hmm_devmem_add_resource);
1181
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001182/*
1183 * hmm_devmem_remove() - remove device memory (kill and free ZONE_DEVICE)
1184 *
1185 * @devmem: hmm_devmem struct use to track and manage the ZONE_DEVICE memory
1186 *
1187 * This will hot-unplug memory that was hotplugged by hmm_devmem_add on behalf
1188 * of the device driver. It will free struct page and remove the resource that
1189 * reserved the physical address range for this device memory.
1190 */
1191void hmm_devmem_remove(struct hmm_devmem *devmem)
1192{
1193 resource_size_t start, size;
1194 struct device *device;
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001195 bool cdm = false;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001196
1197 if (!devmem)
1198 return;
1199
1200 device = devmem->device;
1201 start = devmem->resource->start;
1202 size = resource_size(devmem->resource);
1203
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001204 cdm = devmem->resource->desc == IORES_DESC_DEVICE_PUBLIC_MEMORY;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001205 hmm_devmem_ref_kill(&devmem->ref);
1206 hmm_devmem_ref_exit(&devmem->ref);
1207 hmm_devmem_pages_remove(devmem);
1208
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001209 if (!cdm)
1210 devm_release_mem_region(device, start, size);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001211}
1212EXPORT_SYMBOL(hmm_devmem_remove);
Jérôme Glisse858b54d2017-09-08 16:12:02 -07001213
1214/*
1215 * A device driver that wants to handle multiple devices memory through a
1216 * single fake device can use hmm_device to do so. This is purely a helper
1217 * and it is not needed to make use of any HMM functionality.
1218 */
1219#define HMM_DEVICE_MAX 256
1220
1221static DECLARE_BITMAP(hmm_device_mask, HMM_DEVICE_MAX);
1222static DEFINE_SPINLOCK(hmm_device_lock);
1223static struct class *hmm_device_class;
1224static dev_t hmm_device_devt;
1225
1226static void hmm_device_release(struct device *device)
1227{
1228 struct hmm_device *hmm_device;
1229
1230 hmm_device = container_of(device, struct hmm_device, device);
1231 spin_lock(&hmm_device_lock);
1232 clear_bit(hmm_device->minor, hmm_device_mask);
1233 spin_unlock(&hmm_device_lock);
1234
1235 kfree(hmm_device);
1236}
1237
1238struct hmm_device *hmm_device_new(void *drvdata)
1239{
1240 struct hmm_device *hmm_device;
1241
1242 hmm_device = kzalloc(sizeof(*hmm_device), GFP_KERNEL);
1243 if (!hmm_device)
1244 return ERR_PTR(-ENOMEM);
1245
1246 spin_lock(&hmm_device_lock);
1247 hmm_device->minor = find_first_zero_bit(hmm_device_mask, HMM_DEVICE_MAX);
1248 if (hmm_device->minor >= HMM_DEVICE_MAX) {
1249 spin_unlock(&hmm_device_lock);
1250 kfree(hmm_device);
1251 return ERR_PTR(-EBUSY);
1252 }
1253 set_bit(hmm_device->minor, hmm_device_mask);
1254 spin_unlock(&hmm_device_lock);
1255
1256 dev_set_name(&hmm_device->device, "hmm_device%d", hmm_device->minor);
1257 hmm_device->device.devt = MKDEV(MAJOR(hmm_device_devt),
1258 hmm_device->minor);
1259 hmm_device->device.release = hmm_device_release;
1260 dev_set_drvdata(&hmm_device->device, drvdata);
1261 hmm_device->device.class = hmm_device_class;
1262 device_initialize(&hmm_device->device);
1263
1264 return hmm_device;
1265}
1266EXPORT_SYMBOL(hmm_device_new);
1267
1268void hmm_device_put(struct hmm_device *hmm_device)
1269{
1270 put_device(&hmm_device->device);
1271}
1272EXPORT_SYMBOL(hmm_device_put);
1273
1274static int __init hmm_init(void)
1275{
1276 int ret;
1277
1278 ret = alloc_chrdev_region(&hmm_device_devt, 0,
1279 HMM_DEVICE_MAX,
1280 "hmm_device");
1281 if (ret)
1282 return ret;
1283
1284 hmm_device_class = class_create(THIS_MODULE, "hmm_device");
1285 if (IS_ERR(hmm_device_class)) {
1286 unregister_chrdev_region(hmm_device_devt, HMM_DEVICE_MAX);
1287 return PTR_ERR(hmm_device_class);
1288 }
1289 return 0;
1290}
1291
1292device_initcall(hmm_init);
Jérôme Glissedf6ad692017-09-08 16:12:24 -07001293#endif /* CONFIG_DEVICE_PRIVATE || CONFIG_DEVICE_PUBLIC */