blob: b8affe0bf4eb5ae8a0a19e984cc90dc23b1cc90b [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,
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700307 uint64_t *pfn)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700308{
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 int hmm_pfns_bad(unsigned long addr,
328 unsigned long end,
329 struct mm_walk *walk)
330{
Jérôme Glissec7195472018-04-10 16:28:27 -0700331 struct hmm_vma_walk *hmm_vma_walk = walk->private;
332 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700333 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700334 unsigned long i;
335
336 i = (addr - range->start) >> PAGE_SHIFT;
337 for (; addr < end; addr += PAGE_SIZE, i++)
338 pfns[i] = HMM_PFN_ERROR;
339
340 return 0;
341}
342
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700343static void hmm_pfns_clear(uint64_t *pfns,
Jérôme Glisse74eee182017-09-08 16:11:35 -0700344 unsigned long addr,
345 unsigned long end)
346{
347 for (; addr < end; addr += PAGE_SIZE, pfns++)
348 *pfns = 0;
349}
350
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700351/*
352 * hmm_vma_walk_hole() - handle a range lacking valid pmd or pte(s)
353 * @start: range virtual start address (inclusive)
354 * @end: range virtual end address (exclusive)
355 * @walk: mm_walk structure
356 * Returns: 0 on success, -EAGAIN after page fault, or page fault error
357 *
358 * This function will be called whenever pmd_none() or pte_none() returns true,
359 * or whenever there is no page directory covering the virtual address range.
360 */
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700361static int hmm_vma_walk_hole(unsigned long addr,
362 unsigned long end,
363 struct mm_walk *walk)
364{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700365 struct hmm_vma_walk *hmm_vma_walk = walk->private;
366 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700367 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700368 unsigned long i;
369
Jérôme Glisse74eee182017-09-08 16:11:35 -0700370 hmm_vma_walk->last = addr;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700371 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700372 for (; addr < end; addr += PAGE_SIZE, i++) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700373 pfns[i] = 0;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700374 if (hmm_vma_walk->fault) {
375 int ret;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700376
Jérôme Glisse74eee182017-09-08 16:11:35 -0700377 ret = hmm_vma_do_fault(walk, addr, &pfns[i]);
378 if (ret != -EAGAIN)
379 return ret;
380 }
381 }
382
383 return hmm_vma_walk->fault ? -EAGAIN : 0;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700384}
385
386static int hmm_vma_walk_pmd(pmd_t *pmdp,
387 unsigned long start,
388 unsigned long end,
389 struct mm_walk *walk)
390{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700391 struct hmm_vma_walk *hmm_vma_walk = walk->private;
392 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700393 struct vm_area_struct *vma = walk->vma;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700394 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700395 unsigned long addr = start, i;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700396 bool write_fault;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700397 pte_t *ptep;
398
399 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700400 write_fault = hmm_vma_walk->fault & hmm_vma_walk->write;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700401
402again:
403 if (pmd_none(*pmdp))
404 return hmm_vma_walk_hole(start, end, walk);
405
406 if (pmd_huge(*pmdp) && vma->vm_flags & VM_HUGETLB)
407 return hmm_pfns_bad(start, end, walk);
408
409 if (pmd_devmap(*pmdp) || pmd_trans_huge(*pmdp)) {
410 unsigned long pfn;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700411 uint64_t flag = 0;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700412 pmd_t pmd;
413
414 /*
415 * No need to take pmd_lock here, even if some other threads
416 * is splitting the huge pmd we will get that event through
417 * mmu_notifier callback.
418 *
419 * So just read pmd value and check again its a transparent
420 * huge or device mapping one and compute corresponding pfn
421 * values.
422 */
423 pmd = pmd_read_atomic(pmdp);
424 barrier();
425 if (!pmd_devmap(pmd) && !pmd_trans_huge(pmd))
426 goto again;
427 if (pmd_protnone(pmd))
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700428 return hmm_vma_walk_hole(start, end, walk);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700429
Linus Torvaldsf6f37322017-12-15 18:53:22 -0800430 if (write_fault && !pmd_write(pmd))
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700431 return hmm_vma_walk_hole(start, end, walk);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700432
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700433 pfn = pmd_pfn(pmd) + pte_index(addr);
Linus Torvaldsf6f37322017-12-15 18:53:22 -0800434 flag |= pmd_write(pmd) ? HMM_PFN_WRITE : 0;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700435 for (; addr < end; addr += PAGE_SIZE, i++, pfn++)
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700436 pfns[i] = hmm_pfn_from_pfn(pfn) | flag;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700437 return 0;
438 }
439
440 if (pmd_bad(*pmdp))
441 return hmm_pfns_bad(start, end, walk);
442
443 ptep = pte_offset_map(pmdp, addr);
444 for (; addr < end; addr += PAGE_SIZE, ptep++, i++) {
445 pte_t pte = *ptep;
446
447 pfns[i] = 0;
448
Jérôme Glisse74eee182017-09-08 16:11:35 -0700449 if (pte_none(pte)) {
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700450 pfns[i] = 0;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700451 if (hmm_vma_walk->fault)
452 goto fault;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700453 continue;
454 }
455
Jérôme Glisse74eee182017-09-08 16:11:35 -0700456 if (!pte_present(pte)) {
Ralph Campbell8d63e4c2018-01-31 16:20:30 -0800457 swp_entry_t entry = pte_to_swp_entry(pte);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700458
459 if (!non_swap_entry(entry)) {
460 if (hmm_vma_walk->fault)
461 goto fault;
462 continue;
463 }
464
Jérôme Glisse74eee182017-09-08 16:11:35 -0700465 /*
466 * This is a special swap entry, ignore migration, use
467 * device and report anything else as error.
468 */
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700469 if (is_device_private_entry(entry)) {
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700470 pfns[i] = hmm_pfn_from_pfn(swp_offset(entry));
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700471 if (is_write_device_private_entry(entry)) {
472 pfns[i] |= HMM_PFN_WRITE;
473 } else if (write_fault)
474 goto fault;
475 pfns[i] |= HMM_PFN_DEVICE_UNADDRESSABLE;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700476 } else if (is_migration_entry(entry)) {
Jérôme Glisse74eee182017-09-08 16:11:35 -0700477 if (hmm_vma_walk->fault) {
478 pte_unmap(ptep);
479 hmm_vma_walk->last = addr;
480 migration_entry_wait(vma->vm_mm,
481 pmdp, addr);
482 return -EAGAIN;
483 }
484 continue;
485 } else {
486 /* Report error for everything else */
487 pfns[i] = HMM_PFN_ERROR;
488 }
489 continue;
490 }
491
Linus Torvaldsf6f37322017-12-15 18:53:22 -0800492 if (write_fault && !pte_write(pte))
Jérôme Glisse74eee182017-09-08 16:11:35 -0700493 goto fault;
494
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700495 pfns[i] = hmm_pfn_from_pfn(pte_pfn(pte));
Linus Torvaldsf6f37322017-12-15 18:53:22 -0800496 pfns[i] |= pte_write(pte) ? HMM_PFN_WRITE : 0;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700497 continue;
498
499fault:
500 pte_unmap(ptep);
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700501 /* Fault any virtual address we were asked to fault */
502 return hmm_vma_walk_hole(start, end, walk);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700503 }
504 pte_unmap(ptep - 1);
505
506 return 0;
507}
508
Jérôme Glisse855ce7d2018-04-10 16:28:42 -0700509static void hmm_pfns_special(struct hmm_range *range)
510{
511 unsigned long addr = range->start, i = 0;
512
513 for (; addr < range->end; addr += PAGE_SIZE, i++)
514 range->pfns[i] = HMM_PFN_SPECIAL;
515}
516
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700517/*
518 * hmm_vma_get_pfns() - snapshot CPU page table for a range of virtual addresses
Jérôme Glisse08232a42018-04-10 16:28:30 -0700519 * @range: range being snapshotted
Jérôme Glisse86586a42018-04-10 16:28:34 -0700520 * Returns: -EINVAL if invalid argument, -ENOMEM out of memory, -EPERM invalid
521 * vma permission, 0 success
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700522 *
523 * This snapshots the CPU page table for a range of virtual addresses. Snapshot
524 * validity is tracked by range struct. See hmm_vma_range_done() for further
525 * information.
526 *
527 * The range struct is initialized here. It tracks the CPU page table, but only
528 * if the function returns success (0), in which case the caller must then call
529 * hmm_vma_range_done() to stop CPU page table update tracking on this range.
530 *
531 * NOT CALLING hmm_vma_range_done() IF FUNCTION RETURNS 0 WILL LEAD TO SERIOUS
532 * MEMORY CORRUPTION ! YOU HAVE BEEN WARNED !
533 */
Jérôme Glisse08232a42018-04-10 16:28:30 -0700534int hmm_vma_get_pfns(struct hmm_range *range)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700535{
Jérôme Glisse08232a42018-04-10 16:28:30 -0700536 struct vm_area_struct *vma = range->vma;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700537 struct hmm_vma_walk hmm_vma_walk;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700538 struct mm_walk mm_walk;
539 struct hmm *hmm;
540
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700541 /* Sanity check, this really should not happen ! */
Jérôme Glisse08232a42018-04-10 16:28:30 -0700542 if (range->start < vma->vm_start || range->start >= vma->vm_end)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700543 return -EINVAL;
Jérôme Glisse08232a42018-04-10 16:28:30 -0700544 if (range->end < vma->vm_start || range->end > vma->vm_end)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700545 return -EINVAL;
546
547 hmm = hmm_register(vma->vm_mm);
548 if (!hmm)
549 return -ENOMEM;
550 /* Caller must have registered a mirror, via hmm_mirror_register() ! */
551 if (!hmm->mmu_notifier.ops)
552 return -EINVAL;
553
Jérôme Glisse855ce7d2018-04-10 16:28:42 -0700554 /* FIXME support hugetlb fs */
555 if (is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_SPECIAL)) {
556 hmm_pfns_special(range);
557 return -EINVAL;
558 }
559
Jérôme Glisse86586a42018-04-10 16:28:34 -0700560 if (!(vma->vm_flags & VM_READ)) {
561 /*
562 * If vma do not allow read access, then assume that it does
563 * not allow write access, either. Architecture that allow
564 * write without read access are not supported by HMM, because
565 * operations such has atomic access would not work.
566 */
567 hmm_pfns_clear(range->pfns, range->start, range->end);
568 return -EPERM;
569 }
570
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700571 /* Initialize range to track CPU page table update */
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700572 spin_lock(&hmm->lock);
573 range->valid = true;
574 list_add_rcu(&range->list, &hmm->ranges);
575 spin_unlock(&hmm->lock);
576
Jérôme Glisse74eee182017-09-08 16:11:35 -0700577 hmm_vma_walk.fault = false;
578 hmm_vma_walk.range = range;
579 mm_walk.private = &hmm_vma_walk;
580
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700581 mm_walk.vma = vma;
582 mm_walk.mm = vma->vm_mm;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700583 mm_walk.pte_entry = NULL;
584 mm_walk.test_walk = NULL;
585 mm_walk.hugetlb_entry = NULL;
586 mm_walk.pmd_entry = hmm_vma_walk_pmd;
587 mm_walk.pte_hole = hmm_vma_walk_hole;
588
Jérôme Glisse08232a42018-04-10 16:28:30 -0700589 walk_page_range(range->start, range->end, &mm_walk);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700590 return 0;
591}
592EXPORT_SYMBOL(hmm_vma_get_pfns);
593
594/*
595 * hmm_vma_range_done() - stop tracking change to CPU page table over a range
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700596 * @range: range being tracked
597 * Returns: false if range data has been invalidated, true otherwise
598 *
599 * Range struct is used to track updates to the CPU page table after a call to
600 * either hmm_vma_get_pfns() or hmm_vma_fault(). Once the device driver is done
601 * using the data, or wants to lock updates to the data it got from those
602 * functions, it must call the hmm_vma_range_done() function, which will then
603 * stop tracking CPU page table updates.
604 *
605 * Note that device driver must still implement general CPU page table update
606 * tracking either by using hmm_mirror (see hmm_mirror_register()) or by using
607 * the mmu_notifier API directly.
608 *
609 * CPU page table update tracking done through hmm_range is only temporary and
610 * to be used while trying to duplicate CPU page table contents for a range of
611 * virtual addresses.
612 *
613 * There are two ways to use this :
614 * again:
Jérôme Glisse08232a42018-04-10 16:28:30 -0700615 * hmm_vma_get_pfns(range); or hmm_vma_fault(...);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700616 * trans = device_build_page_table_update_transaction(pfns);
617 * device_page_table_lock();
Jérôme Glisse08232a42018-04-10 16:28:30 -0700618 * if (!hmm_vma_range_done(range)) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700619 * device_page_table_unlock();
620 * goto again;
621 * }
622 * device_commit_transaction(trans);
623 * device_page_table_unlock();
624 *
625 * Or:
Jérôme Glisse08232a42018-04-10 16:28:30 -0700626 * hmm_vma_get_pfns(range); or hmm_vma_fault(...);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700627 * device_page_table_lock();
Jérôme Glisse08232a42018-04-10 16:28:30 -0700628 * hmm_vma_range_done(range);
629 * device_update_page_table(range->pfns);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700630 * device_page_table_unlock();
631 */
Jérôme Glisse08232a42018-04-10 16:28:30 -0700632bool hmm_vma_range_done(struct hmm_range *range)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700633{
634 unsigned long npages = (range->end - range->start) >> PAGE_SHIFT;
635 struct hmm *hmm;
636
637 if (range->end <= range->start) {
638 BUG();
639 return false;
640 }
641
Jérôme Glisse08232a42018-04-10 16:28:30 -0700642 hmm = hmm_register(range->vma->vm_mm);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700643 if (!hmm) {
644 memset(range->pfns, 0, sizeof(*range->pfns) * npages);
645 return false;
646 }
647
648 spin_lock(&hmm->lock);
649 list_del_rcu(&range->list);
650 spin_unlock(&hmm->lock);
651
652 return range->valid;
653}
654EXPORT_SYMBOL(hmm_vma_range_done);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700655
656/*
657 * hmm_vma_fault() - try to fault some address in a virtual address range
Jérôme Glisse08232a42018-04-10 16:28:30 -0700658 * @range: range being faulted
Jérôme Glisse74eee182017-09-08 16:11:35 -0700659 * @write: is it a write fault
660 * @block: allow blocking on fault (if true it sleeps and do not drop mmap_sem)
661 * Returns: 0 success, error otherwise (-EAGAIN means mmap_sem have been drop)
662 *
663 * This is similar to a regular CPU page fault except that it will not trigger
664 * any memory migration if the memory being faulted is not accessible by CPUs.
665 *
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700666 * On error, for one virtual address in the range, the function will mark the
667 * corresponding HMM pfn entry with an error flag.
Jérôme Glisse74eee182017-09-08 16:11:35 -0700668 *
669 * Expected use pattern:
670 * retry:
671 * down_read(&mm->mmap_sem);
672 * // Find vma and address device wants to fault, initialize hmm_pfn_t
673 * // array accordingly
Jérôme Glisse08232a42018-04-10 16:28:30 -0700674 * ret = hmm_vma_fault(range, write, block);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700675 * switch (ret) {
676 * case -EAGAIN:
Jérôme Glisse08232a42018-04-10 16:28:30 -0700677 * hmm_vma_range_done(range);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700678 * // You might want to rate limit or yield to play nicely, you may
679 * // also commit any valid pfn in the array assuming that you are
680 * // getting true from hmm_vma_range_monitor_end()
681 * goto retry;
682 * case 0:
683 * break;
Jérôme Glisse86586a42018-04-10 16:28:34 -0700684 * case -ENOMEM:
685 * case -EINVAL:
686 * case -EPERM:
Jérôme Glisse74eee182017-09-08 16:11:35 -0700687 * default:
688 * // Handle error !
689 * up_read(&mm->mmap_sem)
690 * return;
691 * }
692 * // Take device driver lock that serialize device page table update
693 * driver_lock_device_page_table_update();
Jérôme Glisse08232a42018-04-10 16:28:30 -0700694 * hmm_vma_range_done(range);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700695 * // Commit pfns we got from hmm_vma_fault()
696 * driver_unlock_device_page_table_update();
697 * up_read(&mm->mmap_sem)
698 *
699 * YOU MUST CALL hmm_vma_range_done() AFTER THIS FUNCTION RETURN SUCCESS (0)
700 * BEFORE FREEING THE range struct OR YOU WILL HAVE SERIOUS MEMORY CORRUPTION !
701 *
702 * YOU HAVE BEEN WARNED !
703 */
Jérôme Glisse08232a42018-04-10 16:28:30 -0700704int hmm_vma_fault(struct hmm_range *range, bool write, bool block)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700705{
Jérôme Glisse08232a42018-04-10 16:28:30 -0700706 struct vm_area_struct *vma = range->vma;
707 unsigned long start = range->start;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700708 struct hmm_vma_walk hmm_vma_walk;
709 struct mm_walk mm_walk;
710 struct hmm *hmm;
711 int ret;
712
713 /* Sanity check, this really should not happen ! */
Jérôme Glisse08232a42018-04-10 16:28:30 -0700714 if (range->start < vma->vm_start || range->start >= vma->vm_end)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700715 return -EINVAL;
Jérôme Glisse08232a42018-04-10 16:28:30 -0700716 if (range->end < vma->vm_start || range->end > vma->vm_end)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700717 return -EINVAL;
718
719 hmm = hmm_register(vma->vm_mm);
720 if (!hmm) {
Jérôme Glisse08232a42018-04-10 16:28:30 -0700721 hmm_pfns_clear(range->pfns, range->start, range->end);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700722 return -ENOMEM;
723 }
724 /* Caller must have registered a mirror using hmm_mirror_register() */
725 if (!hmm->mmu_notifier.ops)
726 return -EINVAL;
727
Jérôme Glisse855ce7d2018-04-10 16:28:42 -0700728 /* FIXME support hugetlb fs */
729 if (is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_SPECIAL)) {
730 hmm_pfns_special(range);
731 return -EINVAL;
732 }
733
Jérôme Glisse86586a42018-04-10 16:28:34 -0700734 if (!(vma->vm_flags & VM_READ)) {
735 /*
736 * If vma do not allow read access, then assume that it does
737 * not allow write access, either. Architecture that allow
738 * write without read access are not supported by HMM, because
739 * operations such has atomic access would not work.
740 */
741 hmm_pfns_clear(range->pfns, range->start, range->end);
742 return -EPERM;
743 }
Jérôme Glisse74eee182017-09-08 16:11:35 -0700744
Jérôme Glisse86586a42018-04-10 16:28:34 -0700745 /* Initialize range to track CPU page table update */
746 spin_lock(&hmm->lock);
747 range->valid = true;
748 list_add_rcu(&range->list, &hmm->ranges);
749 spin_unlock(&hmm->lock);
750
Jérôme Glisse74eee182017-09-08 16:11:35 -0700751 hmm_vma_walk.fault = true;
752 hmm_vma_walk.write = write;
753 hmm_vma_walk.block = block;
754 hmm_vma_walk.range = range;
755 mm_walk.private = &hmm_vma_walk;
756 hmm_vma_walk.last = range->start;
757
758 mm_walk.vma = vma;
759 mm_walk.mm = vma->vm_mm;
760 mm_walk.pte_entry = NULL;
761 mm_walk.test_walk = NULL;
762 mm_walk.hugetlb_entry = NULL;
763 mm_walk.pmd_entry = hmm_vma_walk_pmd;
764 mm_walk.pte_hole = hmm_vma_walk_hole;
765
766 do {
Jérôme Glisse08232a42018-04-10 16:28:30 -0700767 ret = walk_page_range(start, range->end, &mm_walk);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700768 start = hmm_vma_walk.last;
769 } while (ret == -EAGAIN);
770
771 if (ret) {
772 unsigned long i;
773
774 i = (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
Jérôme Glisse08232a42018-04-10 16:28:30 -0700775 hmm_pfns_clear(&range->pfns[i], hmm_vma_walk.last, range->end);
776 hmm_vma_range_done(range);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700777 }
778 return ret;
779}
780EXPORT_SYMBOL(hmm_vma_fault);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700781#endif /* IS_ENABLED(CONFIG_HMM_MIRROR) */
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700782
783
Jérôme Glissedf6ad692017-09-08 16:12:24 -0700784#if IS_ENABLED(CONFIG_DEVICE_PRIVATE) || IS_ENABLED(CONFIG_DEVICE_PUBLIC)
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700785struct page *hmm_vma_alloc_locked_page(struct vm_area_struct *vma,
786 unsigned long addr)
787{
788 struct page *page;
789
790 page = alloc_page_vma(GFP_HIGHUSER, vma, addr);
791 if (!page)
792 return NULL;
793 lock_page(page);
794 return page;
795}
796EXPORT_SYMBOL(hmm_vma_alloc_locked_page);
797
798
799static void hmm_devmem_ref_release(struct percpu_ref *ref)
800{
801 struct hmm_devmem *devmem;
802
803 devmem = container_of(ref, struct hmm_devmem, ref);
804 complete(&devmem->completion);
805}
806
807static void hmm_devmem_ref_exit(void *data)
808{
809 struct percpu_ref *ref = data;
810 struct hmm_devmem *devmem;
811
812 devmem = container_of(ref, struct hmm_devmem, ref);
813 percpu_ref_exit(ref);
814 devm_remove_action(devmem->device, &hmm_devmem_ref_exit, data);
815}
816
817static void hmm_devmem_ref_kill(void *data)
818{
819 struct percpu_ref *ref = data;
820 struct hmm_devmem *devmem;
821
822 devmem = container_of(ref, struct hmm_devmem, ref);
823 percpu_ref_kill(ref);
824 wait_for_completion(&devmem->completion);
825 devm_remove_action(devmem->device, &hmm_devmem_ref_kill, data);
826}
827
828static int hmm_devmem_fault(struct vm_area_struct *vma,
829 unsigned long addr,
830 const struct page *page,
831 unsigned int flags,
832 pmd_t *pmdp)
833{
834 struct hmm_devmem *devmem = page->pgmap->data;
835
836 return devmem->ops->fault(devmem, vma, addr, page, flags, pmdp);
837}
838
839static void hmm_devmem_free(struct page *page, void *data)
840{
841 struct hmm_devmem *devmem = data;
842
843 devmem->ops->free(devmem, page);
844}
845
846static DEFINE_MUTEX(hmm_devmem_lock);
847static RADIX_TREE(hmm_devmem_radix, GFP_KERNEL);
848
849static void hmm_devmem_radix_release(struct resource *resource)
850{
Colin Ian Kingfec11bc2017-11-15 17:38:52 -0800851 resource_size_t key, align_start, align_size;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700852
853 align_start = resource->start & ~(PA_SECTION_SIZE - 1);
854 align_size = ALIGN(resource_size(resource), PA_SECTION_SIZE);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700855
856 mutex_lock(&hmm_devmem_lock);
857 for (key = resource->start;
858 key <= resource->end;
859 key += PA_SECTION_SIZE)
860 radix_tree_delete(&hmm_devmem_radix, key >> PA_SECTION_SHIFT);
861 mutex_unlock(&hmm_devmem_lock);
862}
863
864static void hmm_devmem_release(struct device *dev, void *data)
865{
866 struct hmm_devmem *devmem = data;
867 struct resource *resource = devmem->resource;
868 unsigned long start_pfn, npages;
869 struct zone *zone;
870 struct page *page;
871
872 if (percpu_ref_tryget_live(&devmem->ref)) {
873 dev_WARN(dev, "%s: page mapping is still live!\n", __func__);
874 percpu_ref_put(&devmem->ref);
875 }
876
877 /* pages are dead and unused, undo the arch mapping */
878 start_pfn = (resource->start & ~(PA_SECTION_SIZE - 1)) >> PAGE_SHIFT;
879 npages = ALIGN(resource_size(resource), PA_SECTION_SIZE) >> PAGE_SHIFT;
880
881 page = pfn_to_page(start_pfn);
882 zone = page_zone(page);
883
884 mem_hotplug_begin();
Jérôme Glissed3df0a42017-09-08 16:12:28 -0700885 if (resource->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY)
Christoph Hellwigda024512017-12-29 08:53:55 +0100886 __remove_pages(zone, start_pfn, npages, NULL);
Jérôme Glissed3df0a42017-09-08 16:12:28 -0700887 else
888 arch_remove_memory(start_pfn << PAGE_SHIFT,
Christoph Hellwigda024512017-12-29 08:53:55 +0100889 npages << PAGE_SHIFT, NULL);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700890 mem_hotplug_done();
891
892 hmm_devmem_radix_release(resource);
893}
894
895static struct hmm_devmem *hmm_devmem_find(resource_size_t phys)
896{
897 WARN_ON_ONCE(!rcu_read_lock_held());
898
899 return radix_tree_lookup(&hmm_devmem_radix, phys >> PA_SECTION_SHIFT);
900}
901
902static int hmm_devmem_pages_create(struct hmm_devmem *devmem)
903{
904 resource_size_t key, align_start, align_size, align_end;
905 struct device *device = devmem->device;
906 int ret, nid, is_ram;
907 unsigned long pfn;
908
909 align_start = devmem->resource->start & ~(PA_SECTION_SIZE - 1);
910 align_size = ALIGN(devmem->resource->start +
911 resource_size(devmem->resource),
912 PA_SECTION_SIZE) - align_start;
913
914 is_ram = region_intersects(align_start, align_size,
915 IORESOURCE_SYSTEM_RAM,
916 IORES_DESC_NONE);
917 if (is_ram == REGION_MIXED) {
918 WARN_ONCE(1, "%s attempted on mixed region %pr\n",
919 __func__, devmem->resource);
920 return -ENXIO;
921 }
922 if (is_ram == REGION_INTERSECTS)
923 return -ENXIO;
924
Jérôme Glissed3df0a42017-09-08 16:12:28 -0700925 if (devmem->resource->desc == IORES_DESC_DEVICE_PUBLIC_MEMORY)
926 devmem->pagemap.type = MEMORY_DEVICE_PUBLIC;
927 else
928 devmem->pagemap.type = MEMORY_DEVICE_PRIVATE;
929
Logan Gunthorpee7744aa2017-12-29 08:54:04 +0100930 devmem->pagemap.res = *devmem->resource;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700931 devmem->pagemap.page_fault = hmm_devmem_fault;
932 devmem->pagemap.page_free = hmm_devmem_free;
933 devmem->pagemap.dev = devmem->device;
934 devmem->pagemap.ref = &devmem->ref;
935 devmem->pagemap.data = devmem;
936
937 mutex_lock(&hmm_devmem_lock);
938 align_end = align_start + align_size - 1;
939 for (key = align_start; key <= align_end; key += PA_SECTION_SIZE) {
940 struct hmm_devmem *dup;
941
942 rcu_read_lock();
943 dup = hmm_devmem_find(key);
944 rcu_read_unlock();
945 if (dup) {
946 dev_err(device, "%s: collides with mapping for %s\n",
947 __func__, dev_name(dup->device));
948 mutex_unlock(&hmm_devmem_lock);
949 ret = -EBUSY;
950 goto error;
951 }
952 ret = radix_tree_insert(&hmm_devmem_radix,
953 key >> PA_SECTION_SHIFT,
954 devmem);
955 if (ret) {
956 dev_err(device, "%s: failed: %d\n", __func__, ret);
957 mutex_unlock(&hmm_devmem_lock);
958 goto error_radix;
959 }
960 }
961 mutex_unlock(&hmm_devmem_lock);
962
963 nid = dev_to_node(device);
964 if (nid < 0)
965 nid = numa_mem_id();
966
967 mem_hotplug_begin();
968 /*
969 * For device private memory we call add_pages() as we only need to
970 * allocate and initialize struct page for the device memory. More-
971 * over the device memory is un-accessible thus we do not want to
972 * create a linear mapping for the memory like arch_add_memory()
973 * would do.
Jérôme Glissed3df0a42017-09-08 16:12:28 -0700974 *
975 * For device public memory, which is accesible by the CPU, we do
976 * want the linear mapping and thus use arch_add_memory().
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700977 */
Jérôme Glissed3df0a42017-09-08 16:12:28 -0700978 if (devmem->pagemap.type == MEMORY_DEVICE_PUBLIC)
Christoph Hellwig24e6d5a2017-12-29 08:53:53 +0100979 ret = arch_add_memory(nid, align_start, align_size, NULL,
980 false);
Jérôme Glissed3df0a42017-09-08 16:12:28 -0700981 else
982 ret = add_pages(nid, align_start >> PAGE_SHIFT,
Christoph Hellwig24e6d5a2017-12-29 08:53:53 +0100983 align_size >> PAGE_SHIFT, NULL, false);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700984 if (ret) {
985 mem_hotplug_done();
986 goto error_add_memory;
987 }
988 move_pfn_range_to_zone(&NODE_DATA(nid)->node_zones[ZONE_DEVICE],
989 align_start >> PAGE_SHIFT,
Christoph Hellwiga99583e2017-12-29 08:53:57 +0100990 align_size >> PAGE_SHIFT, NULL);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700991 mem_hotplug_done();
992
993 for (pfn = devmem->pfn_first; pfn < devmem->pfn_last; pfn++) {
994 struct page *page = pfn_to_page(pfn);
995
996 page->pgmap = &devmem->pagemap;
997 }
998 return 0;
999
1000error_add_memory:
1001 untrack_pfn(NULL, PHYS_PFN(align_start), align_size);
1002error_radix:
1003 hmm_devmem_radix_release(devmem->resource);
1004error:
1005 return ret;
1006}
1007
1008static int hmm_devmem_match(struct device *dev, void *data, void *match_data)
1009{
1010 struct hmm_devmem *devmem = data;
1011
1012 return devmem->resource == match_data;
1013}
1014
1015static void hmm_devmem_pages_remove(struct hmm_devmem *devmem)
1016{
1017 devres_release(devmem->device, &hmm_devmem_release,
1018 &hmm_devmem_match, devmem->resource);
1019}
1020
1021/*
1022 * hmm_devmem_add() - hotplug ZONE_DEVICE memory for device memory
1023 *
1024 * @ops: memory event device driver callback (see struct hmm_devmem_ops)
1025 * @device: device struct to bind the resource too
1026 * @size: size in bytes of the device memory to add
1027 * Returns: pointer to new hmm_devmem struct ERR_PTR otherwise
1028 *
1029 * This function first finds an empty range of physical address big enough to
1030 * contain the new resource, and then hotplugs it as ZONE_DEVICE memory, which
1031 * in turn allocates struct pages. It does not do anything beyond that; all
1032 * events affecting the memory will go through the various callbacks provided
1033 * by hmm_devmem_ops struct.
1034 *
1035 * Device driver should call this function during device initialization and
1036 * is then responsible of memory management. HMM only provides helpers.
1037 */
1038struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops,
1039 struct device *device,
1040 unsigned long size)
1041{
1042 struct hmm_devmem *devmem;
1043 resource_size_t addr;
1044 int ret;
1045
1046 static_branch_enable(&device_private_key);
1047
1048 devmem = devres_alloc_node(&hmm_devmem_release, sizeof(*devmem),
1049 GFP_KERNEL, dev_to_node(device));
1050 if (!devmem)
1051 return ERR_PTR(-ENOMEM);
1052
1053 init_completion(&devmem->completion);
1054 devmem->pfn_first = -1UL;
1055 devmem->pfn_last = -1UL;
1056 devmem->resource = NULL;
1057 devmem->device = device;
1058 devmem->ops = ops;
1059
1060 ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release,
1061 0, GFP_KERNEL);
1062 if (ret)
1063 goto error_percpu_ref;
1064
1065 ret = devm_add_action(device, hmm_devmem_ref_exit, &devmem->ref);
1066 if (ret)
1067 goto error_devm_add_action;
1068
1069 size = ALIGN(size, PA_SECTION_SIZE);
1070 addr = min((unsigned long)iomem_resource.end,
1071 (1UL << MAX_PHYSMEM_BITS) - 1);
1072 addr = addr - size + 1UL;
1073
1074 /*
1075 * FIXME add a new helper to quickly walk resource tree and find free
1076 * range
1077 *
1078 * FIXME what about ioport_resource resource ?
1079 */
1080 for (; addr > size && addr >= iomem_resource.start; addr -= size) {
1081 ret = region_intersects(addr, size, 0, IORES_DESC_NONE);
1082 if (ret != REGION_DISJOINT)
1083 continue;
1084
1085 devmem->resource = devm_request_mem_region(device, addr, size,
1086 dev_name(device));
1087 if (!devmem->resource) {
1088 ret = -ENOMEM;
1089 goto error_no_resource;
1090 }
1091 break;
1092 }
1093 if (!devmem->resource) {
1094 ret = -ERANGE;
1095 goto error_no_resource;
1096 }
1097
1098 devmem->resource->desc = IORES_DESC_DEVICE_PRIVATE_MEMORY;
1099 devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
1100 devmem->pfn_last = devmem->pfn_first +
1101 (resource_size(devmem->resource) >> PAGE_SHIFT);
1102
1103 ret = hmm_devmem_pages_create(devmem);
1104 if (ret)
1105 goto error_pages;
1106
1107 devres_add(device, devmem);
1108
1109 ret = devm_add_action(device, hmm_devmem_ref_kill, &devmem->ref);
1110 if (ret) {
1111 hmm_devmem_remove(devmem);
1112 return ERR_PTR(ret);
1113 }
1114
1115 return devmem;
1116
1117error_pages:
1118 devm_release_mem_region(device, devmem->resource->start,
1119 resource_size(devmem->resource));
1120error_no_resource:
1121error_devm_add_action:
1122 hmm_devmem_ref_kill(&devmem->ref);
1123 hmm_devmem_ref_exit(&devmem->ref);
1124error_percpu_ref:
1125 devres_free(devmem);
1126 return ERR_PTR(ret);
1127}
1128EXPORT_SYMBOL(hmm_devmem_add);
1129
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001130struct hmm_devmem *hmm_devmem_add_resource(const struct hmm_devmem_ops *ops,
1131 struct device *device,
1132 struct resource *res)
1133{
1134 struct hmm_devmem *devmem;
1135 int ret;
1136
1137 if (res->desc != IORES_DESC_DEVICE_PUBLIC_MEMORY)
1138 return ERR_PTR(-EINVAL);
1139
1140 static_branch_enable(&device_private_key);
1141
1142 devmem = devres_alloc_node(&hmm_devmem_release, sizeof(*devmem),
1143 GFP_KERNEL, dev_to_node(device));
1144 if (!devmem)
1145 return ERR_PTR(-ENOMEM);
1146
1147 init_completion(&devmem->completion);
1148 devmem->pfn_first = -1UL;
1149 devmem->pfn_last = -1UL;
1150 devmem->resource = res;
1151 devmem->device = device;
1152 devmem->ops = ops;
1153
1154 ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release,
1155 0, GFP_KERNEL);
1156 if (ret)
1157 goto error_percpu_ref;
1158
1159 ret = devm_add_action(device, hmm_devmem_ref_exit, &devmem->ref);
1160 if (ret)
1161 goto error_devm_add_action;
1162
1163
1164 devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
1165 devmem->pfn_last = devmem->pfn_first +
1166 (resource_size(devmem->resource) >> PAGE_SHIFT);
1167
1168 ret = hmm_devmem_pages_create(devmem);
1169 if (ret)
1170 goto error_devm_add_action;
1171
1172 devres_add(device, devmem);
1173
1174 ret = devm_add_action(device, hmm_devmem_ref_kill, &devmem->ref);
1175 if (ret) {
1176 hmm_devmem_remove(devmem);
1177 return ERR_PTR(ret);
1178 }
1179
1180 return devmem;
1181
1182error_devm_add_action:
1183 hmm_devmem_ref_kill(&devmem->ref);
1184 hmm_devmem_ref_exit(&devmem->ref);
1185error_percpu_ref:
1186 devres_free(devmem);
1187 return ERR_PTR(ret);
1188}
1189EXPORT_SYMBOL(hmm_devmem_add_resource);
1190
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001191/*
1192 * hmm_devmem_remove() - remove device memory (kill and free ZONE_DEVICE)
1193 *
1194 * @devmem: hmm_devmem struct use to track and manage the ZONE_DEVICE memory
1195 *
1196 * This will hot-unplug memory that was hotplugged by hmm_devmem_add on behalf
1197 * of the device driver. It will free struct page and remove the resource that
1198 * reserved the physical address range for this device memory.
1199 */
1200void hmm_devmem_remove(struct hmm_devmem *devmem)
1201{
1202 resource_size_t start, size;
1203 struct device *device;
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001204 bool cdm = false;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001205
1206 if (!devmem)
1207 return;
1208
1209 device = devmem->device;
1210 start = devmem->resource->start;
1211 size = resource_size(devmem->resource);
1212
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001213 cdm = devmem->resource->desc == IORES_DESC_DEVICE_PUBLIC_MEMORY;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001214 hmm_devmem_ref_kill(&devmem->ref);
1215 hmm_devmem_ref_exit(&devmem->ref);
1216 hmm_devmem_pages_remove(devmem);
1217
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001218 if (!cdm)
1219 devm_release_mem_region(device, start, size);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001220}
1221EXPORT_SYMBOL(hmm_devmem_remove);
Jérôme Glisse858b54d2017-09-08 16:12:02 -07001222
1223/*
1224 * A device driver that wants to handle multiple devices memory through a
1225 * single fake device can use hmm_device to do so. This is purely a helper
1226 * and it is not needed to make use of any HMM functionality.
1227 */
1228#define HMM_DEVICE_MAX 256
1229
1230static DECLARE_BITMAP(hmm_device_mask, HMM_DEVICE_MAX);
1231static DEFINE_SPINLOCK(hmm_device_lock);
1232static struct class *hmm_device_class;
1233static dev_t hmm_device_devt;
1234
1235static void hmm_device_release(struct device *device)
1236{
1237 struct hmm_device *hmm_device;
1238
1239 hmm_device = container_of(device, struct hmm_device, device);
1240 spin_lock(&hmm_device_lock);
1241 clear_bit(hmm_device->minor, hmm_device_mask);
1242 spin_unlock(&hmm_device_lock);
1243
1244 kfree(hmm_device);
1245}
1246
1247struct hmm_device *hmm_device_new(void *drvdata)
1248{
1249 struct hmm_device *hmm_device;
1250
1251 hmm_device = kzalloc(sizeof(*hmm_device), GFP_KERNEL);
1252 if (!hmm_device)
1253 return ERR_PTR(-ENOMEM);
1254
1255 spin_lock(&hmm_device_lock);
1256 hmm_device->minor = find_first_zero_bit(hmm_device_mask, HMM_DEVICE_MAX);
1257 if (hmm_device->minor >= HMM_DEVICE_MAX) {
1258 spin_unlock(&hmm_device_lock);
1259 kfree(hmm_device);
1260 return ERR_PTR(-EBUSY);
1261 }
1262 set_bit(hmm_device->minor, hmm_device_mask);
1263 spin_unlock(&hmm_device_lock);
1264
1265 dev_set_name(&hmm_device->device, "hmm_device%d", hmm_device->minor);
1266 hmm_device->device.devt = MKDEV(MAJOR(hmm_device_devt),
1267 hmm_device->minor);
1268 hmm_device->device.release = hmm_device_release;
1269 dev_set_drvdata(&hmm_device->device, drvdata);
1270 hmm_device->device.class = hmm_device_class;
1271 device_initialize(&hmm_device->device);
1272
1273 return hmm_device;
1274}
1275EXPORT_SYMBOL(hmm_device_new);
1276
1277void hmm_device_put(struct hmm_device *hmm_device)
1278{
1279 put_device(&hmm_device->device);
1280}
1281EXPORT_SYMBOL(hmm_device_put);
1282
1283static int __init hmm_init(void)
1284{
1285 int ret;
1286
1287 ret = alloc_chrdev_region(&hmm_device_devt, 0,
1288 HMM_DEVICE_MAX,
1289 "hmm_device");
1290 if (ret)
1291 return ret;
1292
1293 hmm_device_class = class_create(THIS_MODULE, "hmm_device");
1294 if (IS_ERR(hmm_device_class)) {
1295 unregister_chrdev_region(hmm_device_devt, HMM_DEVICE_MAX);
1296 return PTR_ERR(hmm_device_class);
1297 }
1298 return 0;
1299}
1300
1301device_initcall(hmm_init);
Jérôme Glissedf6ad692017-09-08 16:12:24 -07001302#endif /* CONFIG_DEVICE_PRIVATE || CONFIG_DEVICE_PUBLIC */