blob: c968e49f7a0c527258a85b8c0259467e3b2924de [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 IS_ENABLED(CONFIG_HMM_MIRROR)
Jérôme Glissec0b12402017-09-08 16:11:27 -070039static const struct mmu_notifier_ops hmm_mmu_notifier_ops;
40
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070041/*
42 * struct hmm - HMM per mm struct
43 *
44 * @mm: mm struct this HMM struct is bound to
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070045 * @lock: lock protecting ranges list
Jérôme Glissec0b12402017-09-08 16:11:27 -070046 * @sequence: we track updates to the CPU page table with a sequence number
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070047 * @ranges: list of range being snapshotted
Jérôme Glissec0b12402017-09-08 16:11:27 -070048 * @mirrors: list of mirrors for this mm
49 * @mmu_notifier: mmu notifier to track updates to CPU page table
50 * @mirrors_sem: read/write semaphore protecting the mirrors list
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070051 */
52struct hmm {
53 struct mm_struct *mm;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070054 spinlock_t lock;
Jérôme Glissec0b12402017-09-08 16:11:27 -070055 atomic_t sequence;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070056 struct list_head ranges;
Jérôme Glissec0b12402017-09-08 16:11:27 -070057 struct list_head mirrors;
58 struct mmu_notifier mmu_notifier;
59 struct rw_semaphore mirrors_sem;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070060};
61
62/*
63 * hmm_register - register HMM against an mm (HMM internal)
64 *
65 * @mm: mm struct to attach to
66 *
67 * This is not intended to be used directly by device drivers. It allocates an
68 * HMM struct if mm does not have one, and initializes it.
69 */
70static struct hmm *hmm_register(struct mm_struct *mm)
71{
Jérôme Glissec0b12402017-09-08 16:11:27 -070072 struct hmm *hmm = READ_ONCE(mm->hmm);
73 bool cleanup = false;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -070074
75 /*
76 * The hmm struct can only be freed once the mm_struct goes away,
77 * hence we should always have pre-allocated an new hmm struct
78 * above.
79 */
Jérôme Glissec0b12402017-09-08 16:11:27 -070080 if (hmm)
81 return hmm;
82
83 hmm = kmalloc(sizeof(*hmm), GFP_KERNEL);
84 if (!hmm)
85 return NULL;
86 INIT_LIST_HEAD(&hmm->mirrors);
87 init_rwsem(&hmm->mirrors_sem);
88 atomic_set(&hmm->sequence, 0);
89 hmm->mmu_notifier.ops = NULL;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -070090 INIT_LIST_HEAD(&hmm->ranges);
91 spin_lock_init(&hmm->lock);
Jérôme Glissec0b12402017-09-08 16:11:27 -070092 hmm->mm = mm;
93
94 /*
95 * We should only get here if hold the mmap_sem in write mode ie on
96 * registration of first mirror through hmm_mirror_register()
97 */
98 hmm->mmu_notifier.ops = &hmm_mmu_notifier_ops;
99 if (__mmu_notifier_register(&hmm->mmu_notifier, mm)) {
100 kfree(hmm);
101 return NULL;
102 }
103
104 spin_lock(&mm->page_table_lock);
105 if (!mm->hmm)
106 mm->hmm = hmm;
107 else
108 cleanup = true;
109 spin_unlock(&mm->page_table_lock);
110
111 if (cleanup) {
112 mmu_notifier_unregister(&hmm->mmu_notifier, mm);
113 kfree(hmm);
114 }
115
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700116 return mm->hmm;
117}
118
119void hmm_mm_destroy(struct mm_struct *mm)
120{
121 kfree(mm->hmm);
122}
Jérôme Glissec0b12402017-09-08 16:11:27 -0700123
Jérôme Glissec0b12402017-09-08 16:11:27 -0700124static void hmm_invalidate_range(struct hmm *hmm,
125 enum hmm_update_type action,
126 unsigned long start,
127 unsigned long end)
128{
129 struct hmm_mirror *mirror;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700130 struct hmm_range *range;
131
132 spin_lock(&hmm->lock);
133 list_for_each_entry(range, &hmm->ranges, list) {
134 unsigned long addr, idx, npages;
135
136 if (end < range->start || start >= range->end)
137 continue;
138
139 range->valid = false;
140 addr = max(start, range->start);
141 idx = (addr - range->start) >> PAGE_SHIFT;
142 npages = (min(range->end, end) - addr) >> PAGE_SHIFT;
143 memset(&range->pfns[idx], 0, sizeof(*range->pfns) * npages);
144 }
145 spin_unlock(&hmm->lock);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700146
147 down_read(&hmm->mirrors_sem);
148 list_for_each_entry(mirror, &hmm->mirrors, list)
149 mirror->ops->sync_cpu_device_pagetables(mirror, action,
150 start, end);
151 up_read(&hmm->mirrors_sem);
152}
153
Ralph Campbelle1401512018-04-10 16:28:19 -0700154static void hmm_release(struct mmu_notifier *mn, struct mm_struct *mm)
155{
156 struct hmm_mirror *mirror;
157 struct hmm *hmm = mm->hmm;
158
159 down_write(&hmm->mirrors_sem);
160 mirror = list_first_entry_or_null(&hmm->mirrors, struct hmm_mirror,
161 list);
162 while (mirror) {
163 list_del_init(&mirror->list);
164 if (mirror->ops->release) {
165 /*
166 * Drop mirrors_sem so callback can wait on any pending
167 * work that might itself trigger mmu_notifier callback
168 * and thus would deadlock with us.
169 */
170 up_write(&hmm->mirrors_sem);
171 mirror->ops->release(mirror);
172 down_write(&hmm->mirrors_sem);
173 }
174 mirror = list_first_entry_or_null(&hmm->mirrors,
175 struct hmm_mirror, list);
176 }
177 up_write(&hmm->mirrors_sem);
178}
179
Michal Hocko93065ac2018-08-21 21:52:33 -0700180static int hmm_invalidate_range_start(struct mmu_notifier *mn,
Jérôme Glissec0b12402017-09-08 16:11:27 -0700181 struct mm_struct *mm,
182 unsigned long start,
Michal Hocko93065ac2018-08-21 21:52:33 -0700183 unsigned long end,
184 bool blockable)
Jérôme Glissec0b12402017-09-08 16:11:27 -0700185{
186 struct hmm *hmm = mm->hmm;
187
188 VM_BUG_ON(!hmm);
189
190 atomic_inc(&hmm->sequence);
Michal Hocko93065ac2018-08-21 21:52:33 -0700191
192 return 0;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700193}
194
195static void hmm_invalidate_range_end(struct mmu_notifier *mn,
196 struct mm_struct *mm,
197 unsigned long start,
198 unsigned long end)
199{
200 struct hmm *hmm = mm->hmm;
201
202 VM_BUG_ON(!hmm);
203
204 hmm_invalidate_range(mm->hmm, HMM_UPDATE_INVALIDATE, start, end);
205}
206
207static const struct mmu_notifier_ops hmm_mmu_notifier_ops = {
Ralph Campbelle1401512018-04-10 16:28:19 -0700208 .release = hmm_release,
Jérôme Glissec0b12402017-09-08 16:11:27 -0700209 .invalidate_range_start = hmm_invalidate_range_start,
210 .invalidate_range_end = hmm_invalidate_range_end,
211};
212
213/*
214 * hmm_mirror_register() - register a mirror against an mm
215 *
216 * @mirror: new mirror struct to register
217 * @mm: mm to register against
218 *
219 * To start mirroring a process address space, the device driver must register
220 * an HMM mirror struct.
221 *
222 * THE mm->mmap_sem MUST BE HELD IN WRITE MODE !
223 */
224int hmm_mirror_register(struct hmm_mirror *mirror, struct mm_struct *mm)
225{
226 /* Sanity check */
227 if (!mm || !mirror || !mirror->ops)
228 return -EINVAL;
229
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700230again:
Jérôme Glissec0b12402017-09-08 16:11:27 -0700231 mirror->hmm = hmm_register(mm);
232 if (!mirror->hmm)
233 return -ENOMEM;
234
235 down_write(&mirror->hmm->mirrors_sem);
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700236 if (mirror->hmm->mm == NULL) {
237 /*
238 * A racing hmm_mirror_unregister() is about to destroy the hmm
239 * struct. Try again to allocate a new one.
240 */
241 up_write(&mirror->hmm->mirrors_sem);
242 mirror->hmm = NULL;
243 goto again;
244 } else {
245 list_add(&mirror->list, &mirror->hmm->mirrors);
246 up_write(&mirror->hmm->mirrors_sem);
247 }
Jérôme Glissec0b12402017-09-08 16:11:27 -0700248
249 return 0;
250}
251EXPORT_SYMBOL(hmm_mirror_register);
252
253/*
254 * hmm_mirror_unregister() - unregister a mirror
255 *
256 * @mirror: new mirror struct to register
257 *
258 * Stop mirroring a process address space, and cleanup.
259 */
260void hmm_mirror_unregister(struct hmm_mirror *mirror)
261{
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700262 bool should_unregister = false;
263 struct mm_struct *mm;
264 struct hmm *hmm;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700265
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700266 if (mirror->hmm == NULL)
267 return;
268
269 hmm = mirror->hmm;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700270 down_write(&hmm->mirrors_sem);
Ralph Campbelle1401512018-04-10 16:28:19 -0700271 list_del_init(&mirror->list);
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700272 should_unregister = list_empty(&hmm->mirrors);
273 mirror->hmm = NULL;
274 mm = hmm->mm;
275 hmm->mm = NULL;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700276 up_write(&hmm->mirrors_sem);
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700277
278 if (!should_unregister || mm == NULL)
279 return;
280
281 spin_lock(&mm->page_table_lock);
282 if (mm->hmm == hmm)
283 mm->hmm = NULL;
284 spin_unlock(&mm->page_table_lock);
285
286 mmu_notifier_unregister_no_release(&hmm->mmu_notifier, mm);
287 kfree(hmm);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700288}
289EXPORT_SYMBOL(hmm_mirror_unregister);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700290
Jérôme Glisse74eee182017-09-08 16:11:35 -0700291struct hmm_vma_walk {
292 struct hmm_range *range;
293 unsigned long last;
294 bool fault;
295 bool block;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700296};
297
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700298static int hmm_vma_do_fault(struct mm_walk *walk, unsigned long addr,
299 bool write_fault, uint64_t *pfn)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700300{
301 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_REMOTE;
302 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700303 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700304 struct vm_area_struct *vma = walk->vma;
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700305 vm_fault_t ret;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700306
307 flags |= hmm_vma_walk->block ? 0 : FAULT_FLAG_ALLOW_RETRY;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700308 flags |= write_fault ? FAULT_FLAG_WRITE : 0;
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700309 ret = handle_mm_fault(vma, addr, flags);
310 if (ret & VM_FAULT_RETRY)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700311 return -EBUSY;
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700312 if (ret & VM_FAULT_ERROR) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700313 *pfn = range->values[HMM_PFN_ERROR];
Jérôme Glisse74eee182017-09-08 16:11:35 -0700314 return -EFAULT;
315 }
316
317 return -EAGAIN;
318}
319
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700320static int hmm_pfns_bad(unsigned long addr,
321 unsigned long end,
322 struct mm_walk *walk)
323{
Jérôme Glissec7195472018-04-10 16:28:27 -0700324 struct hmm_vma_walk *hmm_vma_walk = walk->private;
325 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700326 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700327 unsigned long i;
328
329 i = (addr - range->start) >> PAGE_SHIFT;
330 for (; addr < end; addr += PAGE_SIZE, i++)
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700331 pfns[i] = range->values[HMM_PFN_ERROR];
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700332
333 return 0;
334}
335
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700336/*
337 * hmm_vma_walk_hole() - handle a range lacking valid pmd or pte(s)
338 * @start: range virtual start address (inclusive)
339 * @end: range virtual end address (exclusive)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700340 * @fault: should we fault or not ?
341 * @write_fault: write fault ?
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700342 * @walk: mm_walk structure
343 * Returns: 0 on success, -EAGAIN after page fault, or page fault error
344 *
345 * This function will be called whenever pmd_none() or pte_none() returns true,
346 * or whenever there is no page directory covering the virtual address range.
347 */
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700348static int hmm_vma_walk_hole_(unsigned long addr, unsigned long end,
349 bool fault, bool write_fault,
350 struct mm_walk *walk)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700351{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700352 struct hmm_vma_walk *hmm_vma_walk = walk->private;
353 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700354 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700355 unsigned long i;
356
Jérôme Glisse74eee182017-09-08 16:11:35 -0700357 hmm_vma_walk->last = addr;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700358 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700359 for (; addr < end; addr += PAGE_SIZE, i++) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700360 pfns[i] = range->values[HMM_PFN_NONE];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700361 if (fault || write_fault) {
Jérôme Glisse74eee182017-09-08 16:11:35 -0700362 int ret;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700363
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700364 ret = hmm_vma_do_fault(walk, addr, write_fault,
365 &pfns[i]);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700366 if (ret != -EAGAIN)
367 return ret;
368 }
369 }
370
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700371 return (fault || write_fault) ? -EAGAIN : 0;
372}
373
374static inline void hmm_pte_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
375 uint64_t pfns, uint64_t cpu_flags,
376 bool *fault, bool *write_fault)
377{
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700378 struct hmm_range *range = hmm_vma_walk->range;
379
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700380 *fault = *write_fault = false;
381 if (!hmm_vma_walk->fault)
382 return;
383
384 /* We aren't ask to do anything ... */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700385 if (!(pfns & range->flags[HMM_PFN_VALID]))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700386 return;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700387 /* If this is device memory than only fault if explicitly requested */
388 if ((cpu_flags & range->flags[HMM_PFN_DEVICE_PRIVATE])) {
389 /* Do we fault on device memory ? */
390 if (pfns & range->flags[HMM_PFN_DEVICE_PRIVATE]) {
391 *write_fault = pfns & range->flags[HMM_PFN_WRITE];
392 *fault = true;
393 }
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700394 return;
395 }
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700396
397 /* If CPU page table is not valid then we need to fault */
398 *fault = !(cpu_flags & range->flags[HMM_PFN_VALID]);
399 /* Need to write fault ? */
400 if ((pfns & range->flags[HMM_PFN_WRITE]) &&
401 !(cpu_flags & range->flags[HMM_PFN_WRITE])) {
402 *write_fault = true;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700403 *fault = true;
404 }
405}
406
407static void hmm_range_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
408 const uint64_t *pfns, unsigned long npages,
409 uint64_t cpu_flags, bool *fault,
410 bool *write_fault)
411{
412 unsigned long i;
413
414 if (!hmm_vma_walk->fault) {
415 *fault = *write_fault = false;
416 return;
417 }
418
419 for (i = 0; i < npages; ++i) {
420 hmm_pte_need_fault(hmm_vma_walk, pfns[i], cpu_flags,
421 fault, write_fault);
422 if ((*fault) || (*write_fault))
423 return;
424 }
425}
426
427static int hmm_vma_walk_hole(unsigned long addr, unsigned long end,
428 struct mm_walk *walk)
429{
430 struct hmm_vma_walk *hmm_vma_walk = walk->private;
431 struct hmm_range *range = hmm_vma_walk->range;
432 bool fault, write_fault;
433 unsigned long i, npages;
434 uint64_t *pfns;
435
436 i = (addr - range->start) >> PAGE_SHIFT;
437 npages = (end - addr) >> PAGE_SHIFT;
438 pfns = &range->pfns[i];
439 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
440 0, &fault, &write_fault);
441 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
442}
443
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700444static inline uint64_t pmd_to_hmm_pfn_flags(struct hmm_range *range, pmd_t pmd)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700445{
446 if (pmd_protnone(pmd))
447 return 0;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700448 return pmd_write(pmd) ? range->flags[HMM_PFN_VALID] |
449 range->flags[HMM_PFN_WRITE] :
450 range->flags[HMM_PFN_VALID];
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700451}
452
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700453static int hmm_vma_handle_pmd(struct mm_walk *walk,
454 unsigned long addr,
455 unsigned long end,
456 uint64_t *pfns,
457 pmd_t pmd)
458{
459 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700460 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700461 unsigned long pfn, npages, i;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700462 bool fault, write_fault;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700463 uint64_t cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700464
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700465 npages = (end - addr) >> PAGE_SHIFT;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700466 cpu_flags = pmd_to_hmm_pfn_flags(range, pmd);
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700467 hmm_range_need_fault(hmm_vma_walk, pfns, npages, cpu_flags,
468 &fault, &write_fault);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700469
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700470 if (pmd_protnone(pmd) || fault || write_fault)
471 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700472
473 pfn = pmd_pfn(pmd) + pte_index(addr);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700474 for (i = 0; addr < end; addr += PAGE_SIZE, i++, pfn++)
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700475 pfns[i] = hmm_pfn_from_pfn(range, pfn) | cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700476 hmm_vma_walk->last = end;
477 return 0;
478}
479
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700480static inline uint64_t pte_to_hmm_pfn_flags(struct hmm_range *range, pte_t pte)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700481{
482 if (pte_none(pte) || !pte_present(pte))
483 return 0;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700484 return pte_write(pte) ? range->flags[HMM_PFN_VALID] |
485 range->flags[HMM_PFN_WRITE] :
486 range->flags[HMM_PFN_VALID];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700487}
488
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700489static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,
490 unsigned long end, pmd_t *pmdp, pte_t *ptep,
491 uint64_t *pfn)
492{
493 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700494 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700495 struct vm_area_struct *vma = walk->vma;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700496 bool fault, write_fault;
497 uint64_t cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700498 pte_t pte = *ptep;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700499 uint64_t orig_pfn = *pfn;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700500
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700501 *pfn = range->values[HMM_PFN_NONE];
502 cpu_flags = pte_to_hmm_pfn_flags(range, pte);
503 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700504 &fault, &write_fault);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700505
506 if (pte_none(pte)) {
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700507 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700508 goto fault;
509 return 0;
510 }
511
512 if (!pte_present(pte)) {
513 swp_entry_t entry = pte_to_swp_entry(pte);
514
515 if (!non_swap_entry(entry)) {
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700516 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700517 goto fault;
518 return 0;
519 }
520
521 /*
522 * This is a special swap entry, ignore migration, use
523 * device and report anything else as error.
524 */
525 if (is_device_private_entry(entry)) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700526 cpu_flags = range->flags[HMM_PFN_VALID] |
527 range->flags[HMM_PFN_DEVICE_PRIVATE];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700528 cpu_flags |= is_write_device_private_entry(entry) ?
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700529 range->flags[HMM_PFN_WRITE] : 0;
530 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
531 &fault, &write_fault);
532 if (fault || write_fault)
533 goto fault;
534 *pfn = hmm_pfn_from_pfn(range, swp_offset(entry));
535 *pfn |= cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700536 return 0;
537 }
538
539 if (is_migration_entry(entry)) {
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700540 if (fault || write_fault) {
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700541 pte_unmap(ptep);
542 hmm_vma_walk->last = addr;
543 migration_entry_wait(vma->vm_mm,
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700544 pmdp, addr);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700545 return -EAGAIN;
546 }
547 return 0;
548 }
549
550 /* Report error for everything else */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700551 *pfn = range->values[HMM_PFN_ERROR];
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700552 return -EFAULT;
553 }
554
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700555 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700556 goto fault;
557
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700558 *pfn = hmm_pfn_from_pfn(range, pte_pfn(pte)) | cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700559 return 0;
560
561fault:
562 pte_unmap(ptep);
563 /* Fault any virtual address we were asked to fault */
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700564 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700565}
566
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700567static int hmm_vma_walk_pmd(pmd_t *pmdp,
568 unsigned long start,
569 unsigned long end,
570 struct mm_walk *walk)
571{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700572 struct hmm_vma_walk *hmm_vma_walk = walk->private;
573 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700574 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700575 unsigned long addr = start, i;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700576 pte_t *ptep;
577
578 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700579
580again:
581 if (pmd_none(*pmdp))
582 return hmm_vma_walk_hole(start, end, walk);
583
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700584 if (pmd_huge(*pmdp) && (range->vma->vm_flags & VM_HUGETLB))
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700585 return hmm_pfns_bad(start, end, walk);
586
587 if (pmd_devmap(*pmdp) || pmd_trans_huge(*pmdp)) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700588 pmd_t pmd;
589
590 /*
591 * No need to take pmd_lock here, even if some other threads
592 * is splitting the huge pmd we will get that event through
593 * mmu_notifier callback.
594 *
595 * So just read pmd value and check again its a transparent
596 * huge or device mapping one and compute corresponding pfn
597 * values.
598 */
599 pmd = pmd_read_atomic(pmdp);
600 barrier();
601 if (!pmd_devmap(pmd) && !pmd_trans_huge(pmd))
602 goto again;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700603
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700604 return hmm_vma_handle_pmd(walk, addr, end, &pfns[i], pmd);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700605 }
606
607 if (pmd_bad(*pmdp))
608 return hmm_pfns_bad(start, end, walk);
609
610 ptep = pte_offset_map(pmdp, addr);
611 for (; addr < end; addr += PAGE_SIZE, ptep++, i++) {
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700612 int r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700613
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700614 r = hmm_vma_handle_pte(walk, addr, end, pmdp, ptep, &pfns[i]);
615 if (r) {
616 /* hmm_vma_handle_pte() did unmap pte directory */
617 hmm_vma_walk->last = addr;
618 return r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700619 }
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700620 }
621 pte_unmap(ptep - 1);
622
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700623 hmm_vma_walk->last = addr;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700624 return 0;
625}
626
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700627static void hmm_pfns_clear(struct hmm_range *range,
628 uint64_t *pfns,
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700629 unsigned long addr,
630 unsigned long end)
631{
632 for (; addr < end; addr += PAGE_SIZE, pfns++)
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700633 *pfns = range->values[HMM_PFN_NONE];
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700634}
635
Jérôme Glisse855ce7d2018-04-10 16:28:42 -0700636static void hmm_pfns_special(struct hmm_range *range)
637{
638 unsigned long addr = range->start, i = 0;
639
640 for (; addr < range->end; addr += PAGE_SIZE, i++)
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700641 range->pfns[i] = range->values[HMM_PFN_SPECIAL];
Jérôme Glisse855ce7d2018-04-10 16:28:42 -0700642}
643
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700644/*
645 * hmm_vma_get_pfns() - snapshot CPU page table for a range of virtual addresses
Jérôme Glisse08232a42018-04-10 16:28:30 -0700646 * @range: range being snapshotted
Jérôme Glisse86586a42018-04-10 16:28:34 -0700647 * Returns: -EINVAL if invalid argument, -ENOMEM out of memory, -EPERM invalid
648 * vma permission, 0 success
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700649 *
650 * This snapshots the CPU page table for a range of virtual addresses. Snapshot
651 * validity is tracked by range struct. See hmm_vma_range_done() for further
652 * information.
653 *
654 * The range struct is initialized here. It tracks the CPU page table, but only
655 * if the function returns success (0), in which case the caller must then call
656 * hmm_vma_range_done() to stop CPU page table update tracking on this range.
657 *
658 * NOT CALLING hmm_vma_range_done() IF FUNCTION RETURNS 0 WILL LEAD TO SERIOUS
659 * MEMORY CORRUPTION ! YOU HAVE BEEN WARNED !
660 */
Jérôme Glisse08232a42018-04-10 16:28:30 -0700661int hmm_vma_get_pfns(struct hmm_range *range)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700662{
Jérôme Glisse08232a42018-04-10 16:28:30 -0700663 struct vm_area_struct *vma = range->vma;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700664 struct hmm_vma_walk hmm_vma_walk;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700665 struct mm_walk mm_walk;
666 struct hmm *hmm;
667
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700668 /* Sanity check, this really should not happen ! */
Jérôme Glisse08232a42018-04-10 16:28:30 -0700669 if (range->start < vma->vm_start || range->start >= vma->vm_end)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700670 return -EINVAL;
Jérôme Glisse08232a42018-04-10 16:28:30 -0700671 if (range->end < vma->vm_start || range->end > vma->vm_end)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700672 return -EINVAL;
673
674 hmm = hmm_register(vma->vm_mm);
675 if (!hmm)
676 return -ENOMEM;
677 /* Caller must have registered a mirror, via hmm_mirror_register() ! */
678 if (!hmm->mmu_notifier.ops)
679 return -EINVAL;
680
Jérôme Glisse855ce7d2018-04-10 16:28:42 -0700681 /* FIXME support hugetlb fs */
Dave Jiange1fb4a02018-08-17 15:43:40 -0700682 if (is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_SPECIAL) ||
683 vma_is_dax(vma)) {
Jérôme Glisse855ce7d2018-04-10 16:28:42 -0700684 hmm_pfns_special(range);
685 return -EINVAL;
686 }
687
Jérôme Glisse86586a42018-04-10 16:28:34 -0700688 if (!(vma->vm_flags & VM_READ)) {
689 /*
690 * If vma do not allow read access, then assume that it does
691 * not allow write access, either. Architecture that allow
692 * write without read access are not supported by HMM, because
693 * operations such has atomic access would not work.
694 */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700695 hmm_pfns_clear(range, range->pfns, range->start, range->end);
Jérôme Glisse86586a42018-04-10 16:28:34 -0700696 return -EPERM;
697 }
698
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700699 /* Initialize range to track CPU page table update */
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700700 spin_lock(&hmm->lock);
701 range->valid = true;
702 list_add_rcu(&range->list, &hmm->ranges);
703 spin_unlock(&hmm->lock);
704
Jérôme Glisse74eee182017-09-08 16:11:35 -0700705 hmm_vma_walk.fault = false;
706 hmm_vma_walk.range = range;
707 mm_walk.private = &hmm_vma_walk;
708
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700709 mm_walk.vma = vma;
710 mm_walk.mm = vma->vm_mm;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700711 mm_walk.pte_entry = NULL;
712 mm_walk.test_walk = NULL;
713 mm_walk.hugetlb_entry = NULL;
714 mm_walk.pmd_entry = hmm_vma_walk_pmd;
715 mm_walk.pte_hole = hmm_vma_walk_hole;
716
Jérôme Glisse08232a42018-04-10 16:28:30 -0700717 walk_page_range(range->start, range->end, &mm_walk);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700718 return 0;
719}
720EXPORT_SYMBOL(hmm_vma_get_pfns);
721
722/*
723 * hmm_vma_range_done() - stop tracking change to CPU page table over a range
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700724 * @range: range being tracked
725 * Returns: false if range data has been invalidated, true otherwise
726 *
727 * Range struct is used to track updates to the CPU page table after a call to
728 * either hmm_vma_get_pfns() or hmm_vma_fault(). Once the device driver is done
729 * using the data, or wants to lock updates to the data it got from those
730 * functions, it must call the hmm_vma_range_done() function, which will then
731 * stop tracking CPU page table updates.
732 *
733 * Note that device driver must still implement general CPU page table update
734 * tracking either by using hmm_mirror (see hmm_mirror_register()) or by using
735 * the mmu_notifier API directly.
736 *
737 * CPU page table update tracking done through hmm_range is only temporary and
738 * to be used while trying to duplicate CPU page table contents for a range of
739 * virtual addresses.
740 *
741 * There are two ways to use this :
742 * again:
Jérôme Glisse08232a42018-04-10 16:28:30 -0700743 * hmm_vma_get_pfns(range); or hmm_vma_fault(...);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700744 * trans = device_build_page_table_update_transaction(pfns);
745 * device_page_table_lock();
Jérôme Glisse08232a42018-04-10 16:28:30 -0700746 * if (!hmm_vma_range_done(range)) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700747 * device_page_table_unlock();
748 * goto again;
749 * }
750 * device_commit_transaction(trans);
751 * device_page_table_unlock();
752 *
753 * Or:
Jérôme Glisse08232a42018-04-10 16:28:30 -0700754 * hmm_vma_get_pfns(range); or hmm_vma_fault(...);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700755 * device_page_table_lock();
Jérôme Glisse08232a42018-04-10 16:28:30 -0700756 * hmm_vma_range_done(range);
757 * device_update_page_table(range->pfns);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700758 * device_page_table_unlock();
759 */
Jérôme Glisse08232a42018-04-10 16:28:30 -0700760bool hmm_vma_range_done(struct hmm_range *range)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700761{
762 unsigned long npages = (range->end - range->start) >> PAGE_SHIFT;
763 struct hmm *hmm;
764
765 if (range->end <= range->start) {
766 BUG();
767 return false;
768 }
769
Jérôme Glisse08232a42018-04-10 16:28:30 -0700770 hmm = hmm_register(range->vma->vm_mm);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700771 if (!hmm) {
772 memset(range->pfns, 0, sizeof(*range->pfns) * npages);
773 return false;
774 }
775
776 spin_lock(&hmm->lock);
777 list_del_rcu(&range->list);
778 spin_unlock(&hmm->lock);
779
780 return range->valid;
781}
782EXPORT_SYMBOL(hmm_vma_range_done);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700783
784/*
785 * hmm_vma_fault() - try to fault some address in a virtual address range
Jérôme Glisse08232a42018-04-10 16:28:30 -0700786 * @range: range being faulted
Jérôme Glisse74eee182017-09-08 16:11:35 -0700787 * @block: allow blocking on fault (if true it sleeps and do not drop mmap_sem)
788 * Returns: 0 success, error otherwise (-EAGAIN means mmap_sem have been drop)
789 *
790 * This is similar to a regular CPU page fault except that it will not trigger
791 * any memory migration if the memory being faulted is not accessible by CPUs.
792 *
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700793 * On error, for one virtual address in the range, the function will mark the
794 * corresponding HMM pfn entry with an error flag.
Jérôme Glisse74eee182017-09-08 16:11:35 -0700795 *
796 * Expected use pattern:
797 * retry:
798 * down_read(&mm->mmap_sem);
799 * // Find vma and address device wants to fault, initialize hmm_pfn_t
800 * // array accordingly
Jérôme Glisse08232a42018-04-10 16:28:30 -0700801 * ret = hmm_vma_fault(range, write, block);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700802 * switch (ret) {
803 * case -EAGAIN:
Jérôme Glisse08232a42018-04-10 16:28:30 -0700804 * hmm_vma_range_done(range);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700805 * // You might want to rate limit or yield to play nicely, you may
806 * // also commit any valid pfn in the array assuming that you are
807 * // getting true from hmm_vma_range_monitor_end()
808 * goto retry;
809 * case 0:
810 * break;
Jérôme Glisse86586a42018-04-10 16:28:34 -0700811 * case -ENOMEM:
812 * case -EINVAL:
813 * case -EPERM:
Jérôme Glisse74eee182017-09-08 16:11:35 -0700814 * default:
815 * // Handle error !
816 * up_read(&mm->mmap_sem)
817 * return;
818 * }
819 * // Take device driver lock that serialize device page table update
820 * driver_lock_device_page_table_update();
Jérôme Glisse08232a42018-04-10 16:28:30 -0700821 * hmm_vma_range_done(range);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700822 * // Commit pfns we got from hmm_vma_fault()
823 * driver_unlock_device_page_table_update();
824 * up_read(&mm->mmap_sem)
825 *
826 * YOU MUST CALL hmm_vma_range_done() AFTER THIS FUNCTION RETURN SUCCESS (0)
827 * BEFORE FREEING THE range struct OR YOU WILL HAVE SERIOUS MEMORY CORRUPTION !
828 *
829 * YOU HAVE BEEN WARNED !
830 */
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700831int hmm_vma_fault(struct hmm_range *range, bool block)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700832{
Jérôme Glisse08232a42018-04-10 16:28:30 -0700833 struct vm_area_struct *vma = range->vma;
834 unsigned long start = range->start;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700835 struct hmm_vma_walk hmm_vma_walk;
836 struct mm_walk mm_walk;
837 struct hmm *hmm;
838 int ret;
839
840 /* Sanity check, this really should not happen ! */
Jérôme Glisse08232a42018-04-10 16:28:30 -0700841 if (range->start < vma->vm_start || range->start >= vma->vm_end)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700842 return -EINVAL;
Jérôme Glisse08232a42018-04-10 16:28:30 -0700843 if (range->end < vma->vm_start || range->end > vma->vm_end)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700844 return -EINVAL;
845
846 hmm = hmm_register(vma->vm_mm);
847 if (!hmm) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700848 hmm_pfns_clear(range, range->pfns, range->start, range->end);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700849 return -ENOMEM;
850 }
851 /* Caller must have registered a mirror using hmm_mirror_register() */
852 if (!hmm->mmu_notifier.ops)
853 return -EINVAL;
854
Jérôme Glisse855ce7d2018-04-10 16:28:42 -0700855 /* FIXME support hugetlb fs */
Dave Jiange1fb4a02018-08-17 15:43:40 -0700856 if (is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_SPECIAL) ||
857 vma_is_dax(vma)) {
Jérôme Glisse855ce7d2018-04-10 16:28:42 -0700858 hmm_pfns_special(range);
859 return -EINVAL;
860 }
861
Jérôme Glisse86586a42018-04-10 16:28:34 -0700862 if (!(vma->vm_flags & VM_READ)) {
863 /*
864 * If vma do not allow read access, then assume that it does
865 * not allow write access, either. Architecture that allow
866 * write without read access are not supported by HMM, because
867 * operations such has atomic access would not work.
868 */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700869 hmm_pfns_clear(range, range->pfns, range->start, range->end);
Jérôme Glisse86586a42018-04-10 16:28:34 -0700870 return -EPERM;
871 }
Jérôme Glisse74eee182017-09-08 16:11:35 -0700872
Jérôme Glisse86586a42018-04-10 16:28:34 -0700873 /* Initialize range to track CPU page table update */
874 spin_lock(&hmm->lock);
875 range->valid = true;
876 list_add_rcu(&range->list, &hmm->ranges);
877 spin_unlock(&hmm->lock);
878
Jérôme Glisse74eee182017-09-08 16:11:35 -0700879 hmm_vma_walk.fault = true;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700880 hmm_vma_walk.block = block;
881 hmm_vma_walk.range = range;
882 mm_walk.private = &hmm_vma_walk;
883 hmm_vma_walk.last = range->start;
884
885 mm_walk.vma = vma;
886 mm_walk.mm = vma->vm_mm;
887 mm_walk.pte_entry = NULL;
888 mm_walk.test_walk = NULL;
889 mm_walk.hugetlb_entry = NULL;
890 mm_walk.pmd_entry = hmm_vma_walk_pmd;
891 mm_walk.pte_hole = hmm_vma_walk_hole;
892
893 do {
Jérôme Glisse08232a42018-04-10 16:28:30 -0700894 ret = walk_page_range(start, range->end, &mm_walk);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700895 start = hmm_vma_walk.last;
896 } while (ret == -EAGAIN);
897
898 if (ret) {
899 unsigned long i;
900
901 i = (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700902 hmm_pfns_clear(range, &range->pfns[i], hmm_vma_walk.last,
903 range->end);
Jérôme Glisse08232a42018-04-10 16:28:30 -0700904 hmm_vma_range_done(range);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700905 }
906 return ret;
907}
908EXPORT_SYMBOL(hmm_vma_fault);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700909#endif /* IS_ENABLED(CONFIG_HMM_MIRROR) */
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700910
911
Jérôme Glissedf6ad692017-09-08 16:12:24 -0700912#if IS_ENABLED(CONFIG_DEVICE_PRIVATE) || IS_ENABLED(CONFIG_DEVICE_PUBLIC)
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700913struct page *hmm_vma_alloc_locked_page(struct vm_area_struct *vma,
914 unsigned long addr)
915{
916 struct page *page;
917
918 page = alloc_page_vma(GFP_HIGHUSER, vma, addr);
919 if (!page)
920 return NULL;
921 lock_page(page);
922 return page;
923}
924EXPORT_SYMBOL(hmm_vma_alloc_locked_page);
925
926
927static void hmm_devmem_ref_release(struct percpu_ref *ref)
928{
929 struct hmm_devmem *devmem;
930
931 devmem = container_of(ref, struct hmm_devmem, ref);
932 complete(&devmem->completion);
933}
934
935static void hmm_devmem_ref_exit(void *data)
936{
937 struct percpu_ref *ref = data;
938 struct hmm_devmem *devmem;
939
940 devmem = container_of(ref, struct hmm_devmem, ref);
941 percpu_ref_exit(ref);
942 devm_remove_action(devmem->device, &hmm_devmem_ref_exit, data);
943}
944
945static void hmm_devmem_ref_kill(void *data)
946{
947 struct percpu_ref *ref = data;
948 struct hmm_devmem *devmem;
949
950 devmem = container_of(ref, struct hmm_devmem, ref);
951 percpu_ref_kill(ref);
952 wait_for_completion(&devmem->completion);
953 devm_remove_action(devmem->device, &hmm_devmem_ref_kill, data);
954}
955
956static int hmm_devmem_fault(struct vm_area_struct *vma,
957 unsigned long addr,
958 const struct page *page,
959 unsigned int flags,
960 pmd_t *pmdp)
961{
962 struct hmm_devmem *devmem = page->pgmap->data;
963
964 return devmem->ops->fault(devmem, vma, addr, page, flags, pmdp);
965}
966
967static void hmm_devmem_free(struct page *page, void *data)
968{
969 struct hmm_devmem *devmem = data;
970
Dan Williams2fa147b2018-07-13 21:50:01 -0700971 page->mapping = NULL;
972
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700973 devmem->ops->free(devmem, page);
974}
975
976static DEFINE_MUTEX(hmm_devmem_lock);
977static RADIX_TREE(hmm_devmem_radix, GFP_KERNEL);
978
979static void hmm_devmem_radix_release(struct resource *resource)
980{
Colin Ian King1e926412018-08-17 15:50:07 -0700981 resource_size_t key;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700982
983 mutex_lock(&hmm_devmem_lock);
984 for (key = resource->start;
985 key <= resource->end;
986 key += PA_SECTION_SIZE)
987 radix_tree_delete(&hmm_devmem_radix, key >> PA_SECTION_SHIFT);
988 mutex_unlock(&hmm_devmem_lock);
989}
990
991static void hmm_devmem_release(struct device *dev, void *data)
992{
993 struct hmm_devmem *devmem = data;
994 struct resource *resource = devmem->resource;
995 unsigned long start_pfn, npages;
996 struct zone *zone;
997 struct page *page;
998
999 if (percpu_ref_tryget_live(&devmem->ref)) {
1000 dev_WARN(dev, "%s: page mapping is still live!\n", __func__);
1001 percpu_ref_put(&devmem->ref);
1002 }
1003
1004 /* pages are dead and unused, undo the arch mapping */
1005 start_pfn = (resource->start & ~(PA_SECTION_SIZE - 1)) >> PAGE_SHIFT;
1006 npages = ALIGN(resource_size(resource), PA_SECTION_SIZE) >> PAGE_SHIFT;
1007
1008 page = pfn_to_page(start_pfn);
1009 zone = page_zone(page);
1010
1011 mem_hotplug_begin();
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001012 if (resource->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY)
Christoph Hellwigda024512017-12-29 08:53:55 +01001013 __remove_pages(zone, start_pfn, npages, NULL);
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001014 else
1015 arch_remove_memory(start_pfn << PAGE_SHIFT,
Christoph Hellwigda024512017-12-29 08:53:55 +01001016 npages << PAGE_SHIFT, NULL);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001017 mem_hotplug_done();
1018
1019 hmm_devmem_radix_release(resource);
1020}
1021
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001022static int hmm_devmem_pages_create(struct hmm_devmem *devmem)
1023{
1024 resource_size_t key, align_start, align_size, align_end;
1025 struct device *device = devmem->device;
1026 int ret, nid, is_ram;
1027 unsigned long pfn;
1028
1029 align_start = devmem->resource->start & ~(PA_SECTION_SIZE - 1);
1030 align_size = ALIGN(devmem->resource->start +
1031 resource_size(devmem->resource),
1032 PA_SECTION_SIZE) - align_start;
1033
1034 is_ram = region_intersects(align_start, align_size,
1035 IORESOURCE_SYSTEM_RAM,
1036 IORES_DESC_NONE);
1037 if (is_ram == REGION_MIXED) {
1038 WARN_ONCE(1, "%s attempted on mixed region %pr\n",
1039 __func__, devmem->resource);
1040 return -ENXIO;
1041 }
1042 if (is_ram == REGION_INTERSECTS)
1043 return -ENXIO;
1044
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001045 if (devmem->resource->desc == IORES_DESC_DEVICE_PUBLIC_MEMORY)
1046 devmem->pagemap.type = MEMORY_DEVICE_PUBLIC;
1047 else
1048 devmem->pagemap.type = MEMORY_DEVICE_PRIVATE;
1049
Logan Gunthorpee7744aa2017-12-29 08:54:04 +01001050 devmem->pagemap.res = *devmem->resource;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001051 devmem->pagemap.page_fault = hmm_devmem_fault;
1052 devmem->pagemap.page_free = hmm_devmem_free;
1053 devmem->pagemap.dev = devmem->device;
1054 devmem->pagemap.ref = &devmem->ref;
1055 devmem->pagemap.data = devmem;
1056
1057 mutex_lock(&hmm_devmem_lock);
1058 align_end = align_start + align_size - 1;
1059 for (key = align_start; key <= align_end; key += PA_SECTION_SIZE) {
1060 struct hmm_devmem *dup;
1061
Tejun Heo18be4602018-04-10 16:29:09 -07001062 dup = radix_tree_lookup(&hmm_devmem_radix,
1063 key >> PA_SECTION_SHIFT);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001064 if (dup) {
1065 dev_err(device, "%s: collides with mapping for %s\n",
1066 __func__, dev_name(dup->device));
1067 mutex_unlock(&hmm_devmem_lock);
1068 ret = -EBUSY;
1069 goto error;
1070 }
1071 ret = radix_tree_insert(&hmm_devmem_radix,
1072 key >> PA_SECTION_SHIFT,
1073 devmem);
1074 if (ret) {
1075 dev_err(device, "%s: failed: %d\n", __func__, ret);
1076 mutex_unlock(&hmm_devmem_lock);
1077 goto error_radix;
1078 }
1079 }
1080 mutex_unlock(&hmm_devmem_lock);
1081
1082 nid = dev_to_node(device);
1083 if (nid < 0)
1084 nid = numa_mem_id();
1085
1086 mem_hotplug_begin();
1087 /*
1088 * For device private memory we call add_pages() as we only need to
1089 * allocate and initialize struct page for the device memory. More-
1090 * over the device memory is un-accessible thus we do not want to
1091 * create a linear mapping for the memory like arch_add_memory()
1092 * would do.
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001093 *
1094 * For device public memory, which is accesible by the CPU, we do
1095 * want the linear mapping and thus use arch_add_memory().
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001096 */
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001097 if (devmem->pagemap.type == MEMORY_DEVICE_PUBLIC)
Christoph Hellwig24e6d5a2017-12-29 08:53:53 +01001098 ret = arch_add_memory(nid, align_start, align_size, NULL,
1099 false);
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001100 else
1101 ret = add_pages(nid, align_start >> PAGE_SHIFT,
Christoph Hellwig24e6d5a2017-12-29 08:53:53 +01001102 align_size >> PAGE_SHIFT, NULL, false);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001103 if (ret) {
1104 mem_hotplug_done();
1105 goto error_add_memory;
1106 }
1107 move_pfn_range_to_zone(&NODE_DATA(nid)->node_zones[ZONE_DEVICE],
1108 align_start >> PAGE_SHIFT,
Christoph Hellwiga99583e2017-12-29 08:53:57 +01001109 align_size >> PAGE_SHIFT, NULL);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001110 mem_hotplug_done();
1111
1112 for (pfn = devmem->pfn_first; pfn < devmem->pfn_last; pfn++) {
1113 struct page *page = pfn_to_page(pfn);
1114
1115 page->pgmap = &devmem->pagemap;
1116 }
1117 return 0;
1118
1119error_add_memory:
1120 untrack_pfn(NULL, PHYS_PFN(align_start), align_size);
1121error_radix:
1122 hmm_devmem_radix_release(devmem->resource);
1123error:
1124 return ret;
1125}
1126
1127static int hmm_devmem_match(struct device *dev, void *data, void *match_data)
1128{
1129 struct hmm_devmem *devmem = data;
1130
1131 return devmem->resource == match_data;
1132}
1133
1134static void hmm_devmem_pages_remove(struct hmm_devmem *devmem)
1135{
1136 devres_release(devmem->device, &hmm_devmem_release,
1137 &hmm_devmem_match, devmem->resource);
1138}
1139
1140/*
1141 * hmm_devmem_add() - hotplug ZONE_DEVICE memory for device memory
1142 *
1143 * @ops: memory event device driver callback (see struct hmm_devmem_ops)
1144 * @device: device struct to bind the resource too
1145 * @size: size in bytes of the device memory to add
1146 * Returns: pointer to new hmm_devmem struct ERR_PTR otherwise
1147 *
1148 * This function first finds an empty range of physical address big enough to
1149 * contain the new resource, and then hotplugs it as ZONE_DEVICE memory, which
1150 * in turn allocates struct pages. It does not do anything beyond that; all
1151 * events affecting the memory will go through the various callbacks provided
1152 * by hmm_devmem_ops struct.
1153 *
1154 * Device driver should call this function during device initialization and
1155 * is then responsible of memory management. HMM only provides helpers.
1156 */
1157struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops,
1158 struct device *device,
1159 unsigned long size)
1160{
1161 struct hmm_devmem *devmem;
1162 resource_size_t addr;
1163 int ret;
1164
Dan Williamse76384882018-05-16 11:46:08 -07001165 dev_pagemap_get_ops();
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001166
1167 devmem = devres_alloc_node(&hmm_devmem_release, sizeof(*devmem),
1168 GFP_KERNEL, dev_to_node(device));
1169 if (!devmem)
1170 return ERR_PTR(-ENOMEM);
1171
1172 init_completion(&devmem->completion);
1173 devmem->pfn_first = -1UL;
1174 devmem->pfn_last = -1UL;
1175 devmem->resource = NULL;
1176 devmem->device = device;
1177 devmem->ops = ops;
1178
1179 ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release,
1180 0, GFP_KERNEL);
1181 if (ret)
1182 goto error_percpu_ref;
1183
1184 ret = devm_add_action(device, hmm_devmem_ref_exit, &devmem->ref);
1185 if (ret)
1186 goto error_devm_add_action;
1187
1188 size = ALIGN(size, PA_SECTION_SIZE);
1189 addr = min((unsigned long)iomem_resource.end,
1190 (1UL << MAX_PHYSMEM_BITS) - 1);
1191 addr = addr - size + 1UL;
1192
1193 /*
1194 * FIXME add a new helper to quickly walk resource tree and find free
1195 * range
1196 *
1197 * FIXME what about ioport_resource resource ?
1198 */
1199 for (; addr > size && addr >= iomem_resource.start; addr -= size) {
1200 ret = region_intersects(addr, size, 0, IORES_DESC_NONE);
1201 if (ret != REGION_DISJOINT)
1202 continue;
1203
1204 devmem->resource = devm_request_mem_region(device, addr, size,
1205 dev_name(device));
1206 if (!devmem->resource) {
1207 ret = -ENOMEM;
1208 goto error_no_resource;
1209 }
1210 break;
1211 }
1212 if (!devmem->resource) {
1213 ret = -ERANGE;
1214 goto error_no_resource;
1215 }
1216
1217 devmem->resource->desc = IORES_DESC_DEVICE_PRIVATE_MEMORY;
1218 devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
1219 devmem->pfn_last = devmem->pfn_first +
1220 (resource_size(devmem->resource) >> PAGE_SHIFT);
1221
1222 ret = hmm_devmem_pages_create(devmem);
1223 if (ret)
1224 goto error_pages;
1225
1226 devres_add(device, devmem);
1227
1228 ret = devm_add_action(device, hmm_devmem_ref_kill, &devmem->ref);
1229 if (ret) {
1230 hmm_devmem_remove(devmem);
1231 return ERR_PTR(ret);
1232 }
1233
1234 return devmem;
1235
1236error_pages:
1237 devm_release_mem_region(device, devmem->resource->start,
1238 resource_size(devmem->resource));
1239error_no_resource:
1240error_devm_add_action:
1241 hmm_devmem_ref_kill(&devmem->ref);
1242 hmm_devmem_ref_exit(&devmem->ref);
1243error_percpu_ref:
1244 devres_free(devmem);
1245 return ERR_PTR(ret);
1246}
1247EXPORT_SYMBOL(hmm_devmem_add);
1248
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001249struct hmm_devmem *hmm_devmem_add_resource(const struct hmm_devmem_ops *ops,
1250 struct device *device,
1251 struct resource *res)
1252{
1253 struct hmm_devmem *devmem;
1254 int ret;
1255
1256 if (res->desc != IORES_DESC_DEVICE_PUBLIC_MEMORY)
1257 return ERR_PTR(-EINVAL);
1258
Dan Williamse76384882018-05-16 11:46:08 -07001259 dev_pagemap_get_ops();
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001260
1261 devmem = devres_alloc_node(&hmm_devmem_release, sizeof(*devmem),
1262 GFP_KERNEL, dev_to_node(device));
1263 if (!devmem)
1264 return ERR_PTR(-ENOMEM);
1265
1266 init_completion(&devmem->completion);
1267 devmem->pfn_first = -1UL;
1268 devmem->pfn_last = -1UL;
1269 devmem->resource = res;
1270 devmem->device = device;
1271 devmem->ops = ops;
1272
1273 ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release,
1274 0, GFP_KERNEL);
1275 if (ret)
1276 goto error_percpu_ref;
1277
1278 ret = devm_add_action(device, hmm_devmem_ref_exit, &devmem->ref);
1279 if (ret)
1280 goto error_devm_add_action;
1281
1282
1283 devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
1284 devmem->pfn_last = devmem->pfn_first +
1285 (resource_size(devmem->resource) >> PAGE_SHIFT);
1286
1287 ret = hmm_devmem_pages_create(devmem);
1288 if (ret)
1289 goto error_devm_add_action;
1290
1291 devres_add(device, devmem);
1292
1293 ret = devm_add_action(device, hmm_devmem_ref_kill, &devmem->ref);
1294 if (ret) {
1295 hmm_devmem_remove(devmem);
1296 return ERR_PTR(ret);
1297 }
1298
1299 return devmem;
1300
1301error_devm_add_action:
1302 hmm_devmem_ref_kill(&devmem->ref);
1303 hmm_devmem_ref_exit(&devmem->ref);
1304error_percpu_ref:
1305 devres_free(devmem);
1306 return ERR_PTR(ret);
1307}
1308EXPORT_SYMBOL(hmm_devmem_add_resource);
1309
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001310/*
1311 * hmm_devmem_remove() - remove device memory (kill and free ZONE_DEVICE)
1312 *
1313 * @devmem: hmm_devmem struct use to track and manage the ZONE_DEVICE memory
1314 *
1315 * This will hot-unplug memory that was hotplugged by hmm_devmem_add on behalf
1316 * of the device driver. It will free struct page and remove the resource that
1317 * reserved the physical address range for this device memory.
1318 */
1319void hmm_devmem_remove(struct hmm_devmem *devmem)
1320{
1321 resource_size_t start, size;
1322 struct device *device;
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001323 bool cdm = false;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001324
1325 if (!devmem)
1326 return;
1327
1328 device = devmem->device;
1329 start = devmem->resource->start;
1330 size = resource_size(devmem->resource);
1331
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001332 cdm = devmem->resource->desc == IORES_DESC_DEVICE_PUBLIC_MEMORY;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001333 hmm_devmem_ref_kill(&devmem->ref);
1334 hmm_devmem_ref_exit(&devmem->ref);
1335 hmm_devmem_pages_remove(devmem);
1336
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001337 if (!cdm)
1338 devm_release_mem_region(device, start, size);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001339}
1340EXPORT_SYMBOL(hmm_devmem_remove);
Jérôme Glisse858b54d2017-09-08 16:12:02 -07001341
1342/*
1343 * A device driver that wants to handle multiple devices memory through a
1344 * single fake device can use hmm_device to do so. This is purely a helper
1345 * and it is not needed to make use of any HMM functionality.
1346 */
1347#define HMM_DEVICE_MAX 256
1348
1349static DECLARE_BITMAP(hmm_device_mask, HMM_DEVICE_MAX);
1350static DEFINE_SPINLOCK(hmm_device_lock);
1351static struct class *hmm_device_class;
1352static dev_t hmm_device_devt;
1353
1354static void hmm_device_release(struct device *device)
1355{
1356 struct hmm_device *hmm_device;
1357
1358 hmm_device = container_of(device, struct hmm_device, device);
1359 spin_lock(&hmm_device_lock);
1360 clear_bit(hmm_device->minor, hmm_device_mask);
1361 spin_unlock(&hmm_device_lock);
1362
1363 kfree(hmm_device);
1364}
1365
1366struct hmm_device *hmm_device_new(void *drvdata)
1367{
1368 struct hmm_device *hmm_device;
1369
1370 hmm_device = kzalloc(sizeof(*hmm_device), GFP_KERNEL);
1371 if (!hmm_device)
1372 return ERR_PTR(-ENOMEM);
1373
1374 spin_lock(&hmm_device_lock);
1375 hmm_device->minor = find_first_zero_bit(hmm_device_mask, HMM_DEVICE_MAX);
1376 if (hmm_device->minor >= HMM_DEVICE_MAX) {
1377 spin_unlock(&hmm_device_lock);
1378 kfree(hmm_device);
1379 return ERR_PTR(-EBUSY);
1380 }
1381 set_bit(hmm_device->minor, hmm_device_mask);
1382 spin_unlock(&hmm_device_lock);
1383
1384 dev_set_name(&hmm_device->device, "hmm_device%d", hmm_device->minor);
1385 hmm_device->device.devt = MKDEV(MAJOR(hmm_device_devt),
1386 hmm_device->minor);
1387 hmm_device->device.release = hmm_device_release;
1388 dev_set_drvdata(&hmm_device->device, drvdata);
1389 hmm_device->device.class = hmm_device_class;
1390 device_initialize(&hmm_device->device);
1391
1392 return hmm_device;
1393}
1394EXPORT_SYMBOL(hmm_device_new);
1395
1396void hmm_device_put(struct hmm_device *hmm_device)
1397{
1398 put_device(&hmm_device->device);
1399}
1400EXPORT_SYMBOL(hmm_device_put);
1401
1402static int __init hmm_init(void)
1403{
1404 int ret;
1405
1406 ret = alloc_chrdev_region(&hmm_device_devt, 0,
1407 HMM_DEVICE_MAX,
1408 "hmm_device");
1409 if (ret)
1410 return ret;
1411
1412 hmm_device_class = class_create(THIS_MODULE, "hmm_device");
1413 if (IS_ERR(hmm_device_class)) {
1414 unregister_chrdev_region(hmm_device_devt, HMM_DEVICE_MAX);
1415 return PTR_ERR(hmm_device_class);
1416 }
1417 return 0;
1418}
1419
1420device_initcall(hmm_init);
Jérôme Glissedf6ad692017-09-08 16:12:24 -07001421#endif /* CONFIG_DEVICE_PRIVATE || CONFIG_DEVICE_PUBLIC */