blob: 812a66997627c5da3a9d441d4ed3bb13a288fa5c [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
536 * @vma: virtual memory area containing the virtual address range
537 * @range: used to track snapshot validity
538 * @start: range virtual start address (inclusive)
539 * @end: range virtual end address (exclusive)
540 * @entries: array of hmm_pfn_t: provided by the caller, filled in by function
541 * Returns: -EINVAL if invalid argument, -ENOMEM out of memory, 0 success
542 *
543 * This snapshots the CPU page table for a range of virtual addresses. Snapshot
544 * validity is tracked by range struct. See hmm_vma_range_done() for further
545 * information.
546 *
547 * The range struct is initialized here. It tracks the CPU page table, but only
548 * if the function returns success (0), in which case the caller must then call
549 * hmm_vma_range_done() to stop CPU page table update tracking on this range.
550 *
551 * NOT CALLING hmm_vma_range_done() IF FUNCTION RETURNS 0 WILL LEAD TO SERIOUS
552 * MEMORY CORRUPTION ! YOU HAVE BEEN WARNED !
553 */
554int hmm_vma_get_pfns(struct vm_area_struct *vma,
555 struct hmm_range *range,
556 unsigned long start,
557 unsigned long end,
558 hmm_pfn_t *pfns)
559{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700560 struct hmm_vma_walk hmm_vma_walk;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700561 struct mm_walk mm_walk;
562 struct hmm *hmm;
563
564 /* FIXME support hugetlb fs */
565 if (is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_SPECIAL)) {
566 hmm_pfns_special(pfns, start, end);
567 return -EINVAL;
568 }
569
570 /* Sanity check, this really should not happen ! */
571 if (start < vma->vm_start || start >= vma->vm_end)
572 return -EINVAL;
573 if (end < vma->vm_start || end > vma->vm_end)
574 return -EINVAL;
575
576 hmm = hmm_register(vma->vm_mm);
577 if (!hmm)
578 return -ENOMEM;
579 /* Caller must have registered a mirror, via hmm_mirror_register() ! */
580 if (!hmm->mmu_notifier.ops)
581 return -EINVAL;
582
583 /* Initialize range to track CPU page table update */
584 range->start = start;
585 range->pfns = pfns;
586 range->end = end;
587 spin_lock(&hmm->lock);
588 range->valid = true;
589 list_add_rcu(&range->list, &hmm->ranges);
590 spin_unlock(&hmm->lock);
591
Jérôme Glisse74eee182017-09-08 16:11:35 -0700592 hmm_vma_walk.fault = false;
593 hmm_vma_walk.range = range;
594 mm_walk.private = &hmm_vma_walk;
595
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700596 mm_walk.vma = vma;
597 mm_walk.mm = vma->vm_mm;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700598 mm_walk.pte_entry = NULL;
599 mm_walk.test_walk = NULL;
600 mm_walk.hugetlb_entry = NULL;
601 mm_walk.pmd_entry = hmm_vma_walk_pmd;
602 mm_walk.pte_hole = hmm_vma_walk_hole;
603
604 walk_page_range(start, end, &mm_walk);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700605 return 0;
606}
607EXPORT_SYMBOL(hmm_vma_get_pfns);
608
609/*
610 * hmm_vma_range_done() - stop tracking change to CPU page table over a range
611 * @vma: virtual memory area containing the virtual address range
612 * @range: range being tracked
613 * Returns: false if range data has been invalidated, true otherwise
614 *
615 * Range struct is used to track updates to the CPU page table after a call to
616 * either hmm_vma_get_pfns() or hmm_vma_fault(). Once the device driver is done
617 * using the data, or wants to lock updates to the data it got from those
618 * functions, it must call the hmm_vma_range_done() function, which will then
619 * stop tracking CPU page table updates.
620 *
621 * Note that device driver must still implement general CPU page table update
622 * tracking either by using hmm_mirror (see hmm_mirror_register()) or by using
623 * the mmu_notifier API directly.
624 *
625 * CPU page table update tracking done through hmm_range is only temporary and
626 * to be used while trying to duplicate CPU page table contents for a range of
627 * virtual addresses.
628 *
629 * There are two ways to use this :
630 * again:
Jérôme Glisse74eee182017-09-08 16:11:35 -0700631 * hmm_vma_get_pfns(vma, range, start, end, pfns); or hmm_vma_fault(...);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700632 * trans = device_build_page_table_update_transaction(pfns);
633 * device_page_table_lock();
634 * if (!hmm_vma_range_done(vma, range)) {
635 * device_page_table_unlock();
636 * goto again;
637 * }
638 * device_commit_transaction(trans);
639 * device_page_table_unlock();
640 *
641 * Or:
Jérôme Glisse74eee182017-09-08 16:11:35 -0700642 * hmm_vma_get_pfns(vma, range, start, end, pfns); or hmm_vma_fault(...);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700643 * device_page_table_lock();
644 * hmm_vma_range_done(vma, range);
645 * device_update_page_table(pfns);
646 * device_page_table_unlock();
647 */
648bool hmm_vma_range_done(struct vm_area_struct *vma, struct hmm_range *range)
649{
650 unsigned long npages = (range->end - range->start) >> PAGE_SHIFT;
651 struct hmm *hmm;
652
653 if (range->end <= range->start) {
654 BUG();
655 return false;
656 }
657
658 hmm = hmm_register(vma->vm_mm);
659 if (!hmm) {
660 memset(range->pfns, 0, sizeof(*range->pfns) * npages);
661 return false;
662 }
663
664 spin_lock(&hmm->lock);
665 list_del_rcu(&range->list);
666 spin_unlock(&hmm->lock);
667
668 return range->valid;
669}
670EXPORT_SYMBOL(hmm_vma_range_done);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700671
672/*
673 * hmm_vma_fault() - try to fault some address in a virtual address range
674 * @vma: virtual memory area containing the virtual address range
675 * @range: use to track pfns array content validity
676 * @start: fault range virtual start address (inclusive)
677 * @end: fault range virtual end address (exclusive)
678 * @pfns: array of hmm_pfn_t, only entry with fault flag set will be faulted
679 * @write: is it a write fault
680 * @block: allow blocking on fault (if true it sleeps and do not drop mmap_sem)
681 * Returns: 0 success, error otherwise (-EAGAIN means mmap_sem have been drop)
682 *
683 * This is similar to a regular CPU page fault except that it will not trigger
684 * any memory migration if the memory being faulted is not accessible by CPUs.
685 *
686 * On error, for one virtual address in the range, the function will set the
687 * hmm_pfn_t error flag for the corresponding pfn entry.
688 *
689 * Expected use pattern:
690 * retry:
691 * down_read(&mm->mmap_sem);
692 * // Find vma and address device wants to fault, initialize hmm_pfn_t
693 * // array accordingly
694 * ret = hmm_vma_fault(vma, start, end, pfns, allow_retry);
695 * switch (ret) {
696 * case -EAGAIN:
697 * hmm_vma_range_done(vma, range);
698 * // You might want to rate limit or yield to play nicely, you may
699 * // also commit any valid pfn in the array assuming that you are
700 * // getting true from hmm_vma_range_monitor_end()
701 * goto retry;
702 * case 0:
703 * break;
704 * default:
705 * // Handle error !
706 * up_read(&mm->mmap_sem)
707 * return;
708 * }
709 * // Take device driver lock that serialize device page table update
710 * driver_lock_device_page_table_update();
711 * hmm_vma_range_done(vma, range);
712 * // Commit pfns we got from hmm_vma_fault()
713 * driver_unlock_device_page_table_update();
714 * up_read(&mm->mmap_sem)
715 *
716 * YOU MUST CALL hmm_vma_range_done() AFTER THIS FUNCTION RETURN SUCCESS (0)
717 * BEFORE FREEING THE range struct OR YOU WILL HAVE SERIOUS MEMORY CORRUPTION !
718 *
719 * YOU HAVE BEEN WARNED !
720 */
721int hmm_vma_fault(struct vm_area_struct *vma,
722 struct hmm_range *range,
723 unsigned long start,
724 unsigned long end,
725 hmm_pfn_t *pfns,
726 bool write,
727 bool block)
728{
729 struct hmm_vma_walk hmm_vma_walk;
730 struct mm_walk mm_walk;
731 struct hmm *hmm;
732 int ret;
733
734 /* Sanity check, this really should not happen ! */
735 if (start < vma->vm_start || start >= vma->vm_end)
736 return -EINVAL;
737 if (end < vma->vm_start || end > vma->vm_end)
738 return -EINVAL;
739
740 hmm = hmm_register(vma->vm_mm);
741 if (!hmm) {
742 hmm_pfns_clear(pfns, start, end);
743 return -ENOMEM;
744 }
745 /* Caller must have registered a mirror using hmm_mirror_register() */
746 if (!hmm->mmu_notifier.ops)
747 return -EINVAL;
748
749 /* Initialize range to track CPU page table update */
750 range->start = start;
751 range->pfns = pfns;
752 range->end = end;
753 spin_lock(&hmm->lock);
754 range->valid = true;
755 list_add_rcu(&range->list, &hmm->ranges);
756 spin_unlock(&hmm->lock);
757
758 /* FIXME support hugetlb fs */
759 if (is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_SPECIAL)) {
760 hmm_pfns_special(pfns, start, end);
761 return 0;
762 }
763
764 hmm_vma_walk.fault = true;
765 hmm_vma_walk.write = write;
766 hmm_vma_walk.block = block;
767 hmm_vma_walk.range = range;
768 mm_walk.private = &hmm_vma_walk;
769 hmm_vma_walk.last = range->start;
770
771 mm_walk.vma = vma;
772 mm_walk.mm = vma->vm_mm;
773 mm_walk.pte_entry = NULL;
774 mm_walk.test_walk = NULL;
775 mm_walk.hugetlb_entry = NULL;
776 mm_walk.pmd_entry = hmm_vma_walk_pmd;
777 mm_walk.pte_hole = hmm_vma_walk_hole;
778
779 do {
780 ret = walk_page_range(start, end, &mm_walk);
781 start = hmm_vma_walk.last;
782 } while (ret == -EAGAIN);
783
784 if (ret) {
785 unsigned long i;
786
787 i = (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
788 hmm_pfns_clear(&pfns[i], hmm_vma_walk.last, end);
789 hmm_vma_range_done(vma, range);
790 }
791 return ret;
792}
793EXPORT_SYMBOL(hmm_vma_fault);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700794#endif /* IS_ENABLED(CONFIG_HMM_MIRROR) */
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700795
796
Jérôme Glissedf6ad692017-09-08 16:12:24 -0700797#if IS_ENABLED(CONFIG_DEVICE_PRIVATE) || IS_ENABLED(CONFIG_DEVICE_PUBLIC)
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700798struct page *hmm_vma_alloc_locked_page(struct vm_area_struct *vma,
799 unsigned long addr)
800{
801 struct page *page;
802
803 page = alloc_page_vma(GFP_HIGHUSER, vma, addr);
804 if (!page)
805 return NULL;
806 lock_page(page);
807 return page;
808}
809EXPORT_SYMBOL(hmm_vma_alloc_locked_page);
810
811
812static void hmm_devmem_ref_release(struct percpu_ref *ref)
813{
814 struct hmm_devmem *devmem;
815
816 devmem = container_of(ref, struct hmm_devmem, ref);
817 complete(&devmem->completion);
818}
819
820static void hmm_devmem_ref_exit(void *data)
821{
822 struct percpu_ref *ref = data;
823 struct hmm_devmem *devmem;
824
825 devmem = container_of(ref, struct hmm_devmem, ref);
826 percpu_ref_exit(ref);
827 devm_remove_action(devmem->device, &hmm_devmem_ref_exit, data);
828}
829
830static void hmm_devmem_ref_kill(void *data)
831{
832 struct percpu_ref *ref = data;
833 struct hmm_devmem *devmem;
834
835 devmem = container_of(ref, struct hmm_devmem, ref);
836 percpu_ref_kill(ref);
837 wait_for_completion(&devmem->completion);
838 devm_remove_action(devmem->device, &hmm_devmem_ref_kill, data);
839}
840
841static int hmm_devmem_fault(struct vm_area_struct *vma,
842 unsigned long addr,
843 const struct page *page,
844 unsigned int flags,
845 pmd_t *pmdp)
846{
847 struct hmm_devmem *devmem = page->pgmap->data;
848
849 return devmem->ops->fault(devmem, vma, addr, page, flags, pmdp);
850}
851
852static void hmm_devmem_free(struct page *page, void *data)
853{
854 struct hmm_devmem *devmem = data;
855
856 devmem->ops->free(devmem, page);
857}
858
859static DEFINE_MUTEX(hmm_devmem_lock);
860static RADIX_TREE(hmm_devmem_radix, GFP_KERNEL);
861
862static void hmm_devmem_radix_release(struct resource *resource)
863{
Colin Ian Kingfec11bc2017-11-15 17:38:52 -0800864 resource_size_t key, align_start, align_size;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700865
866 align_start = resource->start & ~(PA_SECTION_SIZE - 1);
867 align_size = ALIGN(resource_size(resource), PA_SECTION_SIZE);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700868
869 mutex_lock(&hmm_devmem_lock);
870 for (key = resource->start;
871 key <= resource->end;
872 key += PA_SECTION_SIZE)
873 radix_tree_delete(&hmm_devmem_radix, key >> PA_SECTION_SHIFT);
874 mutex_unlock(&hmm_devmem_lock);
875}
876
877static void hmm_devmem_release(struct device *dev, void *data)
878{
879 struct hmm_devmem *devmem = data;
880 struct resource *resource = devmem->resource;
881 unsigned long start_pfn, npages;
882 struct zone *zone;
883 struct page *page;
884
885 if (percpu_ref_tryget_live(&devmem->ref)) {
886 dev_WARN(dev, "%s: page mapping is still live!\n", __func__);
887 percpu_ref_put(&devmem->ref);
888 }
889
890 /* pages are dead and unused, undo the arch mapping */
891 start_pfn = (resource->start & ~(PA_SECTION_SIZE - 1)) >> PAGE_SHIFT;
892 npages = ALIGN(resource_size(resource), PA_SECTION_SIZE) >> PAGE_SHIFT;
893
894 page = pfn_to_page(start_pfn);
895 zone = page_zone(page);
896
897 mem_hotplug_begin();
Jérôme Glissed3df0a42017-09-08 16:12:28 -0700898 if (resource->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY)
Christoph Hellwigda024512017-12-29 08:53:55 +0100899 __remove_pages(zone, start_pfn, npages, NULL);
Jérôme Glissed3df0a42017-09-08 16:12:28 -0700900 else
901 arch_remove_memory(start_pfn << PAGE_SHIFT,
Christoph Hellwigda024512017-12-29 08:53:55 +0100902 npages << PAGE_SHIFT, NULL);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700903 mem_hotplug_done();
904
905 hmm_devmem_radix_release(resource);
906}
907
908static struct hmm_devmem *hmm_devmem_find(resource_size_t phys)
909{
910 WARN_ON_ONCE(!rcu_read_lock_held());
911
912 return radix_tree_lookup(&hmm_devmem_radix, phys >> PA_SECTION_SHIFT);
913}
914
915static int hmm_devmem_pages_create(struct hmm_devmem *devmem)
916{
917 resource_size_t key, align_start, align_size, align_end;
918 struct device *device = devmem->device;
919 int ret, nid, is_ram;
920 unsigned long pfn;
921
922 align_start = devmem->resource->start & ~(PA_SECTION_SIZE - 1);
923 align_size = ALIGN(devmem->resource->start +
924 resource_size(devmem->resource),
925 PA_SECTION_SIZE) - align_start;
926
927 is_ram = region_intersects(align_start, align_size,
928 IORESOURCE_SYSTEM_RAM,
929 IORES_DESC_NONE);
930 if (is_ram == REGION_MIXED) {
931 WARN_ONCE(1, "%s attempted on mixed region %pr\n",
932 __func__, devmem->resource);
933 return -ENXIO;
934 }
935 if (is_ram == REGION_INTERSECTS)
936 return -ENXIO;
937
Jérôme Glissed3df0a42017-09-08 16:12:28 -0700938 if (devmem->resource->desc == IORES_DESC_DEVICE_PUBLIC_MEMORY)
939 devmem->pagemap.type = MEMORY_DEVICE_PUBLIC;
940 else
941 devmem->pagemap.type = MEMORY_DEVICE_PRIVATE;
942
Logan Gunthorpee7744aa2017-12-29 08:54:04 +0100943 devmem->pagemap.res = *devmem->resource;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700944 devmem->pagemap.page_fault = hmm_devmem_fault;
945 devmem->pagemap.page_free = hmm_devmem_free;
946 devmem->pagemap.dev = devmem->device;
947 devmem->pagemap.ref = &devmem->ref;
948 devmem->pagemap.data = devmem;
949
950 mutex_lock(&hmm_devmem_lock);
951 align_end = align_start + align_size - 1;
952 for (key = align_start; key <= align_end; key += PA_SECTION_SIZE) {
953 struct hmm_devmem *dup;
954
955 rcu_read_lock();
956 dup = hmm_devmem_find(key);
957 rcu_read_unlock();
958 if (dup) {
959 dev_err(device, "%s: collides with mapping for %s\n",
960 __func__, dev_name(dup->device));
961 mutex_unlock(&hmm_devmem_lock);
962 ret = -EBUSY;
963 goto error;
964 }
965 ret = radix_tree_insert(&hmm_devmem_radix,
966 key >> PA_SECTION_SHIFT,
967 devmem);
968 if (ret) {
969 dev_err(device, "%s: failed: %d\n", __func__, ret);
970 mutex_unlock(&hmm_devmem_lock);
971 goto error_radix;
972 }
973 }
974 mutex_unlock(&hmm_devmem_lock);
975
976 nid = dev_to_node(device);
977 if (nid < 0)
978 nid = numa_mem_id();
979
980 mem_hotplug_begin();
981 /*
982 * For device private memory we call add_pages() as we only need to
983 * allocate and initialize struct page for the device memory. More-
984 * over the device memory is un-accessible thus we do not want to
985 * create a linear mapping for the memory like arch_add_memory()
986 * would do.
Jérôme Glissed3df0a42017-09-08 16:12:28 -0700987 *
988 * For device public memory, which is accesible by the CPU, we do
989 * want the linear mapping and thus use arch_add_memory().
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700990 */
Jérôme Glissed3df0a42017-09-08 16:12:28 -0700991 if (devmem->pagemap.type == MEMORY_DEVICE_PUBLIC)
Christoph Hellwig24e6d5a2017-12-29 08:53:53 +0100992 ret = arch_add_memory(nid, align_start, align_size, NULL,
993 false);
Jérôme Glissed3df0a42017-09-08 16:12:28 -0700994 else
995 ret = add_pages(nid, align_start >> PAGE_SHIFT,
Christoph Hellwig24e6d5a2017-12-29 08:53:53 +0100996 align_size >> PAGE_SHIFT, NULL, false);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700997 if (ret) {
998 mem_hotplug_done();
999 goto error_add_memory;
1000 }
1001 move_pfn_range_to_zone(&NODE_DATA(nid)->node_zones[ZONE_DEVICE],
1002 align_start >> PAGE_SHIFT,
Christoph Hellwiga99583e2017-12-29 08:53:57 +01001003 align_size >> PAGE_SHIFT, NULL);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001004 mem_hotplug_done();
1005
1006 for (pfn = devmem->pfn_first; pfn < devmem->pfn_last; pfn++) {
1007 struct page *page = pfn_to_page(pfn);
1008
1009 page->pgmap = &devmem->pagemap;
1010 }
1011 return 0;
1012
1013error_add_memory:
1014 untrack_pfn(NULL, PHYS_PFN(align_start), align_size);
1015error_radix:
1016 hmm_devmem_radix_release(devmem->resource);
1017error:
1018 return ret;
1019}
1020
1021static int hmm_devmem_match(struct device *dev, void *data, void *match_data)
1022{
1023 struct hmm_devmem *devmem = data;
1024
1025 return devmem->resource == match_data;
1026}
1027
1028static void hmm_devmem_pages_remove(struct hmm_devmem *devmem)
1029{
1030 devres_release(devmem->device, &hmm_devmem_release,
1031 &hmm_devmem_match, devmem->resource);
1032}
1033
1034/*
1035 * hmm_devmem_add() - hotplug ZONE_DEVICE memory for device memory
1036 *
1037 * @ops: memory event device driver callback (see struct hmm_devmem_ops)
1038 * @device: device struct to bind the resource too
1039 * @size: size in bytes of the device memory to add
1040 * Returns: pointer to new hmm_devmem struct ERR_PTR otherwise
1041 *
1042 * This function first finds an empty range of physical address big enough to
1043 * contain the new resource, and then hotplugs it as ZONE_DEVICE memory, which
1044 * in turn allocates struct pages. It does not do anything beyond that; all
1045 * events affecting the memory will go through the various callbacks provided
1046 * by hmm_devmem_ops struct.
1047 *
1048 * Device driver should call this function during device initialization and
1049 * is then responsible of memory management. HMM only provides helpers.
1050 */
1051struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops,
1052 struct device *device,
1053 unsigned long size)
1054{
1055 struct hmm_devmem *devmem;
1056 resource_size_t addr;
1057 int ret;
1058
1059 static_branch_enable(&device_private_key);
1060
1061 devmem = devres_alloc_node(&hmm_devmem_release, sizeof(*devmem),
1062 GFP_KERNEL, dev_to_node(device));
1063 if (!devmem)
1064 return ERR_PTR(-ENOMEM);
1065
1066 init_completion(&devmem->completion);
1067 devmem->pfn_first = -1UL;
1068 devmem->pfn_last = -1UL;
1069 devmem->resource = NULL;
1070 devmem->device = device;
1071 devmem->ops = ops;
1072
1073 ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release,
1074 0, GFP_KERNEL);
1075 if (ret)
1076 goto error_percpu_ref;
1077
1078 ret = devm_add_action(device, hmm_devmem_ref_exit, &devmem->ref);
1079 if (ret)
1080 goto error_devm_add_action;
1081
1082 size = ALIGN(size, PA_SECTION_SIZE);
1083 addr = min((unsigned long)iomem_resource.end,
1084 (1UL << MAX_PHYSMEM_BITS) - 1);
1085 addr = addr - size + 1UL;
1086
1087 /*
1088 * FIXME add a new helper to quickly walk resource tree and find free
1089 * range
1090 *
1091 * FIXME what about ioport_resource resource ?
1092 */
1093 for (; addr > size && addr >= iomem_resource.start; addr -= size) {
1094 ret = region_intersects(addr, size, 0, IORES_DESC_NONE);
1095 if (ret != REGION_DISJOINT)
1096 continue;
1097
1098 devmem->resource = devm_request_mem_region(device, addr, size,
1099 dev_name(device));
1100 if (!devmem->resource) {
1101 ret = -ENOMEM;
1102 goto error_no_resource;
1103 }
1104 break;
1105 }
1106 if (!devmem->resource) {
1107 ret = -ERANGE;
1108 goto error_no_resource;
1109 }
1110
1111 devmem->resource->desc = IORES_DESC_DEVICE_PRIVATE_MEMORY;
1112 devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
1113 devmem->pfn_last = devmem->pfn_first +
1114 (resource_size(devmem->resource) >> PAGE_SHIFT);
1115
1116 ret = hmm_devmem_pages_create(devmem);
1117 if (ret)
1118 goto error_pages;
1119
1120 devres_add(device, devmem);
1121
1122 ret = devm_add_action(device, hmm_devmem_ref_kill, &devmem->ref);
1123 if (ret) {
1124 hmm_devmem_remove(devmem);
1125 return ERR_PTR(ret);
1126 }
1127
1128 return devmem;
1129
1130error_pages:
1131 devm_release_mem_region(device, devmem->resource->start,
1132 resource_size(devmem->resource));
1133error_no_resource:
1134error_devm_add_action:
1135 hmm_devmem_ref_kill(&devmem->ref);
1136 hmm_devmem_ref_exit(&devmem->ref);
1137error_percpu_ref:
1138 devres_free(devmem);
1139 return ERR_PTR(ret);
1140}
1141EXPORT_SYMBOL(hmm_devmem_add);
1142
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001143struct hmm_devmem *hmm_devmem_add_resource(const struct hmm_devmem_ops *ops,
1144 struct device *device,
1145 struct resource *res)
1146{
1147 struct hmm_devmem *devmem;
1148 int ret;
1149
1150 if (res->desc != IORES_DESC_DEVICE_PUBLIC_MEMORY)
1151 return ERR_PTR(-EINVAL);
1152
1153 static_branch_enable(&device_private_key);
1154
1155 devmem = devres_alloc_node(&hmm_devmem_release, sizeof(*devmem),
1156 GFP_KERNEL, dev_to_node(device));
1157 if (!devmem)
1158 return ERR_PTR(-ENOMEM);
1159
1160 init_completion(&devmem->completion);
1161 devmem->pfn_first = -1UL;
1162 devmem->pfn_last = -1UL;
1163 devmem->resource = res;
1164 devmem->device = device;
1165 devmem->ops = ops;
1166
1167 ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release,
1168 0, GFP_KERNEL);
1169 if (ret)
1170 goto error_percpu_ref;
1171
1172 ret = devm_add_action(device, hmm_devmem_ref_exit, &devmem->ref);
1173 if (ret)
1174 goto error_devm_add_action;
1175
1176
1177 devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
1178 devmem->pfn_last = devmem->pfn_first +
1179 (resource_size(devmem->resource) >> PAGE_SHIFT);
1180
1181 ret = hmm_devmem_pages_create(devmem);
1182 if (ret)
1183 goto error_devm_add_action;
1184
1185 devres_add(device, devmem);
1186
1187 ret = devm_add_action(device, hmm_devmem_ref_kill, &devmem->ref);
1188 if (ret) {
1189 hmm_devmem_remove(devmem);
1190 return ERR_PTR(ret);
1191 }
1192
1193 return devmem;
1194
1195error_devm_add_action:
1196 hmm_devmem_ref_kill(&devmem->ref);
1197 hmm_devmem_ref_exit(&devmem->ref);
1198error_percpu_ref:
1199 devres_free(devmem);
1200 return ERR_PTR(ret);
1201}
1202EXPORT_SYMBOL(hmm_devmem_add_resource);
1203
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001204/*
1205 * hmm_devmem_remove() - remove device memory (kill and free ZONE_DEVICE)
1206 *
1207 * @devmem: hmm_devmem struct use to track and manage the ZONE_DEVICE memory
1208 *
1209 * This will hot-unplug memory that was hotplugged by hmm_devmem_add on behalf
1210 * of the device driver. It will free struct page and remove the resource that
1211 * reserved the physical address range for this device memory.
1212 */
1213void hmm_devmem_remove(struct hmm_devmem *devmem)
1214{
1215 resource_size_t start, size;
1216 struct device *device;
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001217 bool cdm = false;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001218
1219 if (!devmem)
1220 return;
1221
1222 device = devmem->device;
1223 start = devmem->resource->start;
1224 size = resource_size(devmem->resource);
1225
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001226 cdm = devmem->resource->desc == IORES_DESC_DEVICE_PUBLIC_MEMORY;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001227 hmm_devmem_ref_kill(&devmem->ref);
1228 hmm_devmem_ref_exit(&devmem->ref);
1229 hmm_devmem_pages_remove(devmem);
1230
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001231 if (!cdm)
1232 devm_release_mem_region(device, start, size);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001233}
1234EXPORT_SYMBOL(hmm_devmem_remove);
Jérôme Glisse858b54d2017-09-08 16:12:02 -07001235
1236/*
1237 * A device driver that wants to handle multiple devices memory through a
1238 * single fake device can use hmm_device to do so. This is purely a helper
1239 * and it is not needed to make use of any HMM functionality.
1240 */
1241#define HMM_DEVICE_MAX 256
1242
1243static DECLARE_BITMAP(hmm_device_mask, HMM_DEVICE_MAX);
1244static DEFINE_SPINLOCK(hmm_device_lock);
1245static struct class *hmm_device_class;
1246static dev_t hmm_device_devt;
1247
1248static void hmm_device_release(struct device *device)
1249{
1250 struct hmm_device *hmm_device;
1251
1252 hmm_device = container_of(device, struct hmm_device, device);
1253 spin_lock(&hmm_device_lock);
1254 clear_bit(hmm_device->minor, hmm_device_mask);
1255 spin_unlock(&hmm_device_lock);
1256
1257 kfree(hmm_device);
1258}
1259
1260struct hmm_device *hmm_device_new(void *drvdata)
1261{
1262 struct hmm_device *hmm_device;
1263
1264 hmm_device = kzalloc(sizeof(*hmm_device), GFP_KERNEL);
1265 if (!hmm_device)
1266 return ERR_PTR(-ENOMEM);
1267
1268 spin_lock(&hmm_device_lock);
1269 hmm_device->minor = find_first_zero_bit(hmm_device_mask, HMM_DEVICE_MAX);
1270 if (hmm_device->minor >= HMM_DEVICE_MAX) {
1271 spin_unlock(&hmm_device_lock);
1272 kfree(hmm_device);
1273 return ERR_PTR(-EBUSY);
1274 }
1275 set_bit(hmm_device->minor, hmm_device_mask);
1276 spin_unlock(&hmm_device_lock);
1277
1278 dev_set_name(&hmm_device->device, "hmm_device%d", hmm_device->minor);
1279 hmm_device->device.devt = MKDEV(MAJOR(hmm_device_devt),
1280 hmm_device->minor);
1281 hmm_device->device.release = hmm_device_release;
1282 dev_set_drvdata(&hmm_device->device, drvdata);
1283 hmm_device->device.class = hmm_device_class;
1284 device_initialize(&hmm_device->device);
1285
1286 return hmm_device;
1287}
1288EXPORT_SYMBOL(hmm_device_new);
1289
1290void hmm_device_put(struct hmm_device *hmm_device)
1291{
1292 put_device(&hmm_device->device);
1293}
1294EXPORT_SYMBOL(hmm_device_put);
1295
1296static int __init hmm_init(void)
1297{
1298 int ret;
1299
1300 ret = alloc_chrdev_region(&hmm_device_devt, 0,
1301 HMM_DEVICE_MAX,
1302 "hmm_device");
1303 if (ret)
1304 return ret;
1305
1306 hmm_device_class = class_create(THIS_MODULE, "hmm_device");
1307 if (IS_ERR(hmm_device_class)) {
1308 unregister_chrdev_region(hmm_device_devt, HMM_DEVICE_MAX);
1309 return PTR_ERR(hmm_device_class);
1310 }
1311 return 0;
1312}
1313
1314device_initcall(hmm_init);
Jérôme Glissedf6ad692017-09-08 16:12:24 -07001315#endif /* CONFIG_DEVICE_PRIVATE || CONFIG_DEVICE_PUBLIC */