blob: 57f0d2a4ff34c6c5200d104855042b10c8309d91 [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
Jérôme Glissec0b12402017-09-08 16:11:27 -070094 spin_lock(&mm->page_table_lock);
95 if (!mm->hmm)
96 mm->hmm = hmm;
97 else
98 cleanup = true;
99 spin_unlock(&mm->page_table_lock);
100
Ralph Campbelldaca89e2018-10-30 15:04:14 -0700101 if (cleanup)
102 goto error;
103
104 /*
105 * We should only get here if hold the mmap_sem in write mode ie on
106 * registration of first mirror through hmm_mirror_register()
107 */
108 hmm->mmu_notifier.ops = &hmm_mmu_notifier_ops;
109 if (__mmu_notifier_register(&hmm->mmu_notifier, mm))
110 goto error_mm;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700111
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700112 return mm->hmm;
Ralph Campbelldaca89e2018-10-30 15:04:14 -0700113
114error_mm:
115 spin_lock(&mm->page_table_lock);
116 if (mm->hmm == hmm)
117 mm->hmm = NULL;
118 spin_unlock(&mm->page_table_lock);
119error:
120 kfree(hmm);
121 return NULL;
Jérôme Glisse133ff0e2017-09-08 16:11:23 -0700122}
123
124void hmm_mm_destroy(struct mm_struct *mm)
125{
126 kfree(mm->hmm);
127}
Jérôme Glissec0b12402017-09-08 16:11:27 -0700128
Jérôme Glissec0b12402017-09-08 16:11:27 -0700129static void hmm_invalidate_range(struct hmm *hmm,
130 enum hmm_update_type action,
131 unsigned long start,
132 unsigned long end)
133{
134 struct hmm_mirror *mirror;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700135 struct hmm_range *range;
136
137 spin_lock(&hmm->lock);
138 list_for_each_entry(range, &hmm->ranges, list) {
139 unsigned long addr, idx, npages;
140
141 if (end < range->start || start >= range->end)
142 continue;
143
144 range->valid = false;
145 addr = max(start, range->start);
146 idx = (addr - range->start) >> PAGE_SHIFT;
147 npages = (min(range->end, end) - addr) >> PAGE_SHIFT;
148 memset(&range->pfns[idx], 0, sizeof(*range->pfns) * npages);
149 }
150 spin_unlock(&hmm->lock);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700151
152 down_read(&hmm->mirrors_sem);
153 list_for_each_entry(mirror, &hmm->mirrors, list)
154 mirror->ops->sync_cpu_device_pagetables(mirror, action,
155 start, end);
156 up_read(&hmm->mirrors_sem);
157}
158
Ralph Campbelle1401512018-04-10 16:28:19 -0700159static void hmm_release(struct mmu_notifier *mn, struct mm_struct *mm)
160{
161 struct hmm_mirror *mirror;
162 struct hmm *hmm = mm->hmm;
163
164 down_write(&hmm->mirrors_sem);
165 mirror = list_first_entry_or_null(&hmm->mirrors, struct hmm_mirror,
166 list);
167 while (mirror) {
168 list_del_init(&mirror->list);
169 if (mirror->ops->release) {
170 /*
171 * Drop mirrors_sem so callback can wait on any pending
172 * work that might itself trigger mmu_notifier callback
173 * and thus would deadlock with us.
174 */
175 up_write(&hmm->mirrors_sem);
176 mirror->ops->release(mirror);
177 down_write(&hmm->mirrors_sem);
178 }
179 mirror = list_first_entry_or_null(&hmm->mirrors,
180 struct hmm_mirror, list);
181 }
182 up_write(&hmm->mirrors_sem);
183}
184
Michal Hocko93065ac2018-08-21 21:52:33 -0700185static int hmm_invalidate_range_start(struct mmu_notifier *mn,
Jérôme Glissec0b12402017-09-08 16:11:27 -0700186 struct mm_struct *mm,
187 unsigned long start,
Michal Hocko93065ac2018-08-21 21:52:33 -0700188 unsigned long end,
189 bool blockable)
Jérôme Glissec0b12402017-09-08 16:11:27 -0700190{
191 struct hmm *hmm = mm->hmm;
192
193 VM_BUG_ON(!hmm);
194
195 atomic_inc(&hmm->sequence);
Michal Hocko93065ac2018-08-21 21:52:33 -0700196
197 return 0;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700198}
199
200static void hmm_invalidate_range_end(struct mmu_notifier *mn,
201 struct mm_struct *mm,
202 unsigned long start,
203 unsigned long end)
204{
205 struct hmm *hmm = mm->hmm;
206
207 VM_BUG_ON(!hmm);
208
209 hmm_invalidate_range(mm->hmm, HMM_UPDATE_INVALIDATE, start, end);
210}
211
212static const struct mmu_notifier_ops hmm_mmu_notifier_ops = {
Ralph Campbelle1401512018-04-10 16:28:19 -0700213 .release = hmm_release,
Jérôme Glissec0b12402017-09-08 16:11:27 -0700214 .invalidate_range_start = hmm_invalidate_range_start,
215 .invalidate_range_end = hmm_invalidate_range_end,
216};
217
218/*
219 * hmm_mirror_register() - register a mirror against an mm
220 *
221 * @mirror: new mirror struct to register
222 * @mm: mm to register against
223 *
224 * To start mirroring a process address space, the device driver must register
225 * an HMM mirror struct.
226 *
227 * THE mm->mmap_sem MUST BE HELD IN WRITE MODE !
228 */
229int hmm_mirror_register(struct hmm_mirror *mirror, struct mm_struct *mm)
230{
231 /* Sanity check */
232 if (!mm || !mirror || !mirror->ops)
233 return -EINVAL;
234
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700235again:
Jérôme Glissec0b12402017-09-08 16:11:27 -0700236 mirror->hmm = hmm_register(mm);
237 if (!mirror->hmm)
238 return -ENOMEM;
239
240 down_write(&mirror->hmm->mirrors_sem);
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700241 if (mirror->hmm->mm == NULL) {
242 /*
243 * A racing hmm_mirror_unregister() is about to destroy the hmm
244 * struct. Try again to allocate a new one.
245 */
246 up_write(&mirror->hmm->mirrors_sem);
247 mirror->hmm = NULL;
248 goto again;
249 } else {
250 list_add(&mirror->list, &mirror->hmm->mirrors);
251 up_write(&mirror->hmm->mirrors_sem);
252 }
Jérôme Glissec0b12402017-09-08 16:11:27 -0700253
254 return 0;
255}
256EXPORT_SYMBOL(hmm_mirror_register);
257
258/*
259 * hmm_mirror_unregister() - unregister a mirror
260 *
261 * @mirror: new mirror struct to register
262 *
263 * Stop mirroring a process address space, and cleanup.
264 */
265void hmm_mirror_unregister(struct hmm_mirror *mirror)
266{
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700267 bool should_unregister = false;
268 struct mm_struct *mm;
269 struct hmm *hmm;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700270
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700271 if (mirror->hmm == NULL)
272 return;
273
274 hmm = mirror->hmm;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700275 down_write(&hmm->mirrors_sem);
Ralph Campbelle1401512018-04-10 16:28:19 -0700276 list_del_init(&mirror->list);
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700277 should_unregister = list_empty(&hmm->mirrors);
278 mirror->hmm = NULL;
279 mm = hmm->mm;
280 hmm->mm = NULL;
Jérôme Glissec0b12402017-09-08 16:11:27 -0700281 up_write(&hmm->mirrors_sem);
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700282
283 if (!should_unregister || mm == NULL)
284 return;
285
Ralph Campbelldaca89e2018-10-30 15:04:14 -0700286 mmu_notifier_unregister_no_release(&hmm->mmu_notifier, mm);
287
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700288 spin_lock(&mm->page_table_lock);
289 if (mm->hmm == hmm)
290 mm->hmm = NULL;
291 spin_unlock(&mm->page_table_lock);
292
Jérôme Glissec01cbba2018-04-10 16:28:23 -0700293 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;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700302};
303
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700304static int hmm_vma_do_fault(struct mm_walk *walk, unsigned long addr,
305 bool write_fault, uint64_t *pfn)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700306{
307 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_REMOTE;
308 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700309 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700310 struct vm_area_struct *vma = walk->vma;
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700311 vm_fault_t ret;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700312
313 flags |= hmm_vma_walk->block ? 0 : FAULT_FLAG_ALLOW_RETRY;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700314 flags |= write_fault ? FAULT_FLAG_WRITE : 0;
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700315 ret = handle_mm_fault(vma, addr, flags);
316 if (ret & VM_FAULT_RETRY)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700317 return -EBUSY;
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700318 if (ret & VM_FAULT_ERROR) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700319 *pfn = range->values[HMM_PFN_ERROR];
Jérôme Glisse74eee182017-09-08 16:11:35 -0700320 return -EFAULT;
321 }
322
323 return -EAGAIN;
324}
325
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700326static int hmm_pfns_bad(unsigned long addr,
327 unsigned long end,
328 struct mm_walk *walk)
329{
Jérôme Glissec7195472018-04-10 16:28:27 -0700330 struct hmm_vma_walk *hmm_vma_walk = walk->private;
331 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700332 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700333 unsigned long i;
334
335 i = (addr - range->start) >> PAGE_SHIFT;
336 for (; addr < end; addr += PAGE_SIZE, i++)
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700337 pfns[i] = range->values[HMM_PFN_ERROR];
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700338
339 return 0;
340}
341
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700342/*
343 * hmm_vma_walk_hole() - handle a range lacking valid pmd or pte(s)
344 * @start: range virtual start address (inclusive)
345 * @end: range virtual end address (exclusive)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700346 * @fault: should we fault or not ?
347 * @write_fault: write fault ?
Jérôme Glisse5504ed22018-04-10 16:28:46 -0700348 * @walk: mm_walk structure
349 * Returns: 0 on success, -EAGAIN after page fault, or page fault error
350 *
351 * This function will be called whenever pmd_none() or pte_none() returns true,
352 * or whenever there is no page directory covering the virtual address range.
353 */
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700354static int hmm_vma_walk_hole_(unsigned long addr, unsigned long end,
355 bool fault, bool write_fault,
356 struct mm_walk *walk)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700357{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700358 struct hmm_vma_walk *hmm_vma_walk = walk->private;
359 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700360 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700361 unsigned long i;
362
Jérôme Glisse74eee182017-09-08 16:11:35 -0700363 hmm_vma_walk->last = addr;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700364 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700365 for (; addr < end; addr += PAGE_SIZE, i++) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700366 pfns[i] = range->values[HMM_PFN_NONE];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700367 if (fault || write_fault) {
Jérôme Glisse74eee182017-09-08 16:11:35 -0700368 int ret;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700369
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700370 ret = hmm_vma_do_fault(walk, addr, write_fault,
371 &pfns[i]);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700372 if (ret != -EAGAIN)
373 return ret;
374 }
375 }
376
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700377 return (fault || write_fault) ? -EAGAIN : 0;
378}
379
380static inline void hmm_pte_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
381 uint64_t pfns, uint64_t cpu_flags,
382 bool *fault, bool *write_fault)
383{
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700384 struct hmm_range *range = hmm_vma_walk->range;
385
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700386 *fault = *write_fault = false;
387 if (!hmm_vma_walk->fault)
388 return;
389
390 /* We aren't ask to do anything ... */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700391 if (!(pfns & range->flags[HMM_PFN_VALID]))
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700392 return;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700393 /* If this is device memory than only fault if explicitly requested */
394 if ((cpu_flags & range->flags[HMM_PFN_DEVICE_PRIVATE])) {
395 /* Do we fault on device memory ? */
396 if (pfns & range->flags[HMM_PFN_DEVICE_PRIVATE]) {
397 *write_fault = pfns & range->flags[HMM_PFN_WRITE];
398 *fault = true;
399 }
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700400 return;
401 }
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700402
403 /* If CPU page table is not valid then we need to fault */
404 *fault = !(cpu_flags & range->flags[HMM_PFN_VALID]);
405 /* Need to write fault ? */
406 if ((pfns & range->flags[HMM_PFN_WRITE]) &&
407 !(cpu_flags & range->flags[HMM_PFN_WRITE])) {
408 *write_fault = true;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700409 *fault = true;
410 }
411}
412
413static void hmm_range_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
414 const uint64_t *pfns, unsigned long npages,
415 uint64_t cpu_flags, bool *fault,
416 bool *write_fault)
417{
418 unsigned long i;
419
420 if (!hmm_vma_walk->fault) {
421 *fault = *write_fault = false;
422 return;
423 }
424
425 for (i = 0; i < npages; ++i) {
426 hmm_pte_need_fault(hmm_vma_walk, pfns[i], cpu_flags,
427 fault, write_fault);
428 if ((*fault) || (*write_fault))
429 return;
430 }
431}
432
433static int hmm_vma_walk_hole(unsigned long addr, unsigned long end,
434 struct mm_walk *walk)
435{
436 struct hmm_vma_walk *hmm_vma_walk = walk->private;
437 struct hmm_range *range = hmm_vma_walk->range;
438 bool fault, write_fault;
439 unsigned long i, npages;
440 uint64_t *pfns;
441
442 i = (addr - range->start) >> PAGE_SHIFT;
443 npages = (end - addr) >> PAGE_SHIFT;
444 pfns = &range->pfns[i];
445 hmm_range_need_fault(hmm_vma_walk, pfns, npages,
446 0, &fault, &write_fault);
447 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
448}
449
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700450static inline uint64_t pmd_to_hmm_pfn_flags(struct hmm_range *range, pmd_t pmd)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700451{
452 if (pmd_protnone(pmd))
453 return 0;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700454 return pmd_write(pmd) ? range->flags[HMM_PFN_VALID] |
455 range->flags[HMM_PFN_WRITE] :
456 range->flags[HMM_PFN_VALID];
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700457}
458
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700459static int hmm_vma_handle_pmd(struct mm_walk *walk,
460 unsigned long addr,
461 unsigned long end,
462 uint64_t *pfns,
463 pmd_t pmd)
464{
465 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700466 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700467 unsigned long pfn, npages, i;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700468 bool fault, write_fault;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700469 uint64_t cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700470
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700471 npages = (end - addr) >> PAGE_SHIFT;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700472 cpu_flags = pmd_to_hmm_pfn_flags(range, pmd);
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700473 hmm_range_need_fault(hmm_vma_walk, pfns, npages, cpu_flags,
474 &fault, &write_fault);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700475
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700476 if (pmd_protnone(pmd) || fault || write_fault)
477 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700478
479 pfn = pmd_pfn(pmd) + pte_index(addr);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700480 for (i = 0; addr < end; addr += PAGE_SIZE, i++, pfn++)
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700481 pfns[i] = hmm_pfn_from_pfn(range, pfn) | cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700482 hmm_vma_walk->last = end;
483 return 0;
484}
485
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700486static inline uint64_t pte_to_hmm_pfn_flags(struct hmm_range *range, pte_t pte)
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700487{
488 if (pte_none(pte) || !pte_present(pte))
489 return 0;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700490 return pte_write(pte) ? range->flags[HMM_PFN_VALID] |
491 range->flags[HMM_PFN_WRITE] :
492 range->flags[HMM_PFN_VALID];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700493}
494
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700495static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,
496 unsigned long end, pmd_t *pmdp, pte_t *ptep,
497 uint64_t *pfn)
498{
499 struct hmm_vma_walk *hmm_vma_walk = walk->private;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700500 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700501 struct vm_area_struct *vma = walk->vma;
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700502 bool fault, write_fault;
503 uint64_t cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700504 pte_t pte = *ptep;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700505 uint64_t orig_pfn = *pfn;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700506
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700507 *pfn = range->values[HMM_PFN_NONE];
508 cpu_flags = pte_to_hmm_pfn_flags(range, pte);
509 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700510 &fault, &write_fault);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700511
512 if (pte_none(pte)) {
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700513 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700514 goto fault;
515 return 0;
516 }
517
518 if (!pte_present(pte)) {
519 swp_entry_t entry = pte_to_swp_entry(pte);
520
521 if (!non_swap_entry(entry)) {
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700522 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700523 goto fault;
524 return 0;
525 }
526
527 /*
528 * This is a special swap entry, ignore migration, use
529 * device and report anything else as error.
530 */
531 if (is_device_private_entry(entry)) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700532 cpu_flags = range->flags[HMM_PFN_VALID] |
533 range->flags[HMM_PFN_DEVICE_PRIVATE];
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700534 cpu_flags |= is_write_device_private_entry(entry) ?
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700535 range->flags[HMM_PFN_WRITE] : 0;
536 hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags,
537 &fault, &write_fault);
538 if (fault || write_fault)
539 goto fault;
540 *pfn = hmm_pfn_from_pfn(range, swp_offset(entry));
541 *pfn |= cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700542 return 0;
543 }
544
545 if (is_migration_entry(entry)) {
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700546 if (fault || write_fault) {
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700547 pte_unmap(ptep);
548 hmm_vma_walk->last = addr;
549 migration_entry_wait(vma->vm_mm,
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700550 pmdp, addr);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700551 return -EAGAIN;
552 }
553 return 0;
554 }
555
556 /* Report error for everything else */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700557 *pfn = range->values[HMM_PFN_ERROR];
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700558 return -EFAULT;
559 }
560
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700561 if (fault || write_fault)
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700562 goto fault;
563
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700564 *pfn = hmm_pfn_from_pfn(range, pte_pfn(pte)) | cpu_flags;
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700565 return 0;
566
567fault:
568 pte_unmap(ptep);
569 /* Fault any virtual address we were asked to fault */
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700570 return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk);
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700571}
572
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700573static int hmm_vma_walk_pmd(pmd_t *pmdp,
574 unsigned long start,
575 unsigned long end,
576 struct mm_walk *walk)
577{
Jérôme Glisse74eee182017-09-08 16:11:35 -0700578 struct hmm_vma_walk *hmm_vma_walk = walk->private;
579 struct hmm_range *range = hmm_vma_walk->range;
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700580 uint64_t *pfns = range->pfns;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700581 unsigned long addr = start, i;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700582 pte_t *ptep;
583
584 i = (addr - range->start) >> PAGE_SHIFT;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700585
586again:
587 if (pmd_none(*pmdp))
588 return hmm_vma_walk_hole(start, end, walk);
589
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700590 if (pmd_huge(*pmdp) && (range->vma->vm_flags & VM_HUGETLB))
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700591 return hmm_pfns_bad(start, end, walk);
592
593 if (pmd_devmap(*pmdp) || pmd_trans_huge(*pmdp)) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700594 pmd_t pmd;
595
596 /*
597 * No need to take pmd_lock here, even if some other threads
598 * is splitting the huge pmd we will get that event through
599 * mmu_notifier callback.
600 *
601 * So just read pmd value and check again its a transparent
602 * huge or device mapping one and compute corresponding pfn
603 * values.
604 */
605 pmd = pmd_read_atomic(pmdp);
606 barrier();
607 if (!pmd_devmap(pmd) && !pmd_trans_huge(pmd))
608 goto again;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700609
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700610 return hmm_vma_handle_pmd(walk, addr, end, &pfns[i], pmd);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700611 }
612
613 if (pmd_bad(*pmdp))
614 return hmm_pfns_bad(start, end, walk);
615
616 ptep = pte_offset_map(pmdp, addr);
617 for (; addr < end; addr += PAGE_SIZE, ptep++, i++) {
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700618 int r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700619
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700620 r = hmm_vma_handle_pte(walk, addr, end, pmdp, ptep, &pfns[i]);
621 if (r) {
622 /* hmm_vma_handle_pte() did unmap pte directory */
623 hmm_vma_walk->last = addr;
624 return r;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700625 }
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700626 }
627 pte_unmap(ptep - 1);
628
Jérôme Glisse53f5c3f2018-04-10 16:28:59 -0700629 hmm_vma_walk->last = addr;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700630 return 0;
631}
632
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700633static void hmm_pfns_clear(struct hmm_range *range,
634 uint64_t *pfns,
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700635 unsigned long addr,
636 unsigned long end)
637{
638 for (; addr < end; addr += PAGE_SIZE, pfns++)
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700639 *pfns = range->values[HMM_PFN_NONE];
Jérôme Glisse33cd47d2018-04-10 16:28:54 -0700640}
641
Jérôme Glisse855ce7d2018-04-10 16:28:42 -0700642static void hmm_pfns_special(struct hmm_range *range)
643{
644 unsigned long addr = range->start, i = 0;
645
646 for (; addr < range->end; addr += PAGE_SIZE, i++)
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700647 range->pfns[i] = range->values[HMM_PFN_SPECIAL];
Jérôme Glisse855ce7d2018-04-10 16:28:42 -0700648}
649
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700650/*
651 * hmm_vma_get_pfns() - snapshot CPU page table for a range of virtual addresses
Jérôme Glisse08232a42018-04-10 16:28:30 -0700652 * @range: range being snapshotted
Jérôme Glisse86586a42018-04-10 16:28:34 -0700653 * Returns: -EINVAL if invalid argument, -ENOMEM out of memory, -EPERM invalid
654 * vma permission, 0 success
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700655 *
656 * This snapshots the CPU page table for a range of virtual addresses. Snapshot
657 * validity is tracked by range struct. See hmm_vma_range_done() for further
658 * information.
659 *
660 * The range struct is initialized here. It tracks the CPU page table, but only
661 * if the function returns success (0), in which case the caller must then call
662 * hmm_vma_range_done() to stop CPU page table update tracking on this range.
663 *
664 * NOT CALLING hmm_vma_range_done() IF FUNCTION RETURNS 0 WILL LEAD TO SERIOUS
665 * MEMORY CORRUPTION ! YOU HAVE BEEN WARNED !
666 */
Jérôme Glisse08232a42018-04-10 16:28:30 -0700667int hmm_vma_get_pfns(struct hmm_range *range)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700668{
Jérôme Glisse08232a42018-04-10 16:28:30 -0700669 struct vm_area_struct *vma = range->vma;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700670 struct hmm_vma_walk hmm_vma_walk;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700671 struct mm_walk mm_walk;
672 struct hmm *hmm;
673
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700674 /* Sanity check, this really should not happen ! */
Jérôme Glisse08232a42018-04-10 16:28:30 -0700675 if (range->start < vma->vm_start || range->start >= vma->vm_end)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700676 return -EINVAL;
Jérôme Glisse08232a42018-04-10 16:28:30 -0700677 if (range->end < vma->vm_start || range->end > vma->vm_end)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700678 return -EINVAL;
679
680 hmm = hmm_register(vma->vm_mm);
681 if (!hmm)
682 return -ENOMEM;
683 /* Caller must have registered a mirror, via hmm_mirror_register() ! */
684 if (!hmm->mmu_notifier.ops)
685 return -EINVAL;
686
Jérôme Glisse855ce7d2018-04-10 16:28:42 -0700687 /* FIXME support hugetlb fs */
Dave Jiange1fb4a02018-08-17 15:43:40 -0700688 if (is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_SPECIAL) ||
689 vma_is_dax(vma)) {
Jérôme Glisse855ce7d2018-04-10 16:28:42 -0700690 hmm_pfns_special(range);
691 return -EINVAL;
692 }
693
Jérôme Glisse86586a42018-04-10 16:28:34 -0700694 if (!(vma->vm_flags & VM_READ)) {
695 /*
696 * If vma do not allow read access, then assume that it does
697 * not allow write access, either. Architecture that allow
698 * write without read access are not supported by HMM, because
699 * operations such has atomic access would not work.
700 */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700701 hmm_pfns_clear(range, range->pfns, range->start, range->end);
Jérôme Glisse86586a42018-04-10 16:28:34 -0700702 return -EPERM;
703 }
704
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700705 /* Initialize range to track CPU page table update */
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700706 spin_lock(&hmm->lock);
707 range->valid = true;
708 list_add_rcu(&range->list, &hmm->ranges);
709 spin_unlock(&hmm->lock);
710
Jérôme Glisse74eee182017-09-08 16:11:35 -0700711 hmm_vma_walk.fault = false;
712 hmm_vma_walk.range = range;
713 mm_walk.private = &hmm_vma_walk;
714
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700715 mm_walk.vma = vma;
716 mm_walk.mm = vma->vm_mm;
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700717 mm_walk.pte_entry = NULL;
718 mm_walk.test_walk = NULL;
719 mm_walk.hugetlb_entry = NULL;
720 mm_walk.pmd_entry = hmm_vma_walk_pmd;
721 mm_walk.pte_hole = hmm_vma_walk_hole;
722
Jérôme Glisse08232a42018-04-10 16:28:30 -0700723 walk_page_range(range->start, range->end, &mm_walk);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700724 return 0;
725}
726EXPORT_SYMBOL(hmm_vma_get_pfns);
727
728/*
729 * hmm_vma_range_done() - stop tracking change to CPU page table over a range
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700730 * @range: range being tracked
731 * Returns: false if range data has been invalidated, true otherwise
732 *
733 * Range struct is used to track updates to the CPU page table after a call to
734 * either hmm_vma_get_pfns() or hmm_vma_fault(). Once the device driver is done
735 * using the data, or wants to lock updates to the data it got from those
736 * functions, it must call the hmm_vma_range_done() function, which will then
737 * stop tracking CPU page table updates.
738 *
739 * Note that device driver must still implement general CPU page table update
740 * tracking either by using hmm_mirror (see hmm_mirror_register()) or by using
741 * the mmu_notifier API directly.
742 *
743 * CPU page table update tracking done through hmm_range is only temporary and
744 * to be used while trying to duplicate CPU page table contents for a range of
745 * virtual addresses.
746 *
747 * There are two ways to use this :
748 * again:
Jérôme Glisse08232a42018-04-10 16:28:30 -0700749 * hmm_vma_get_pfns(range); or hmm_vma_fault(...);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700750 * trans = device_build_page_table_update_transaction(pfns);
751 * device_page_table_lock();
Jérôme Glisse08232a42018-04-10 16:28:30 -0700752 * if (!hmm_vma_range_done(range)) {
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700753 * device_page_table_unlock();
754 * goto again;
755 * }
756 * device_commit_transaction(trans);
757 * device_page_table_unlock();
758 *
759 * Or:
Jérôme Glisse08232a42018-04-10 16:28:30 -0700760 * hmm_vma_get_pfns(range); or hmm_vma_fault(...);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700761 * device_page_table_lock();
Jérôme Glisse08232a42018-04-10 16:28:30 -0700762 * hmm_vma_range_done(range);
763 * device_update_page_table(range->pfns);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700764 * device_page_table_unlock();
765 */
Jérôme Glisse08232a42018-04-10 16:28:30 -0700766bool hmm_vma_range_done(struct hmm_range *range)
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700767{
768 unsigned long npages = (range->end - range->start) >> PAGE_SHIFT;
769 struct hmm *hmm;
770
771 if (range->end <= range->start) {
772 BUG();
773 return false;
774 }
775
Jérôme Glisse08232a42018-04-10 16:28:30 -0700776 hmm = hmm_register(range->vma->vm_mm);
Jérôme Glisseda4c3c72017-09-08 16:11:31 -0700777 if (!hmm) {
778 memset(range->pfns, 0, sizeof(*range->pfns) * npages);
779 return false;
780 }
781
782 spin_lock(&hmm->lock);
783 list_del_rcu(&range->list);
784 spin_unlock(&hmm->lock);
785
786 return range->valid;
787}
788EXPORT_SYMBOL(hmm_vma_range_done);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700789
790/*
791 * hmm_vma_fault() - try to fault some address in a virtual address range
Jérôme Glisse08232a42018-04-10 16:28:30 -0700792 * @range: range being faulted
Jérôme Glisse74eee182017-09-08 16:11:35 -0700793 * @block: allow blocking on fault (if true it sleeps and do not drop mmap_sem)
794 * Returns: 0 success, error otherwise (-EAGAIN means mmap_sem have been drop)
795 *
796 * This is similar to a regular CPU page fault except that it will not trigger
797 * any memory migration if the memory being faulted is not accessible by CPUs.
798 *
Jérôme Glisseff05c0c2018-04-10 16:28:38 -0700799 * On error, for one virtual address in the range, the function will mark the
800 * corresponding HMM pfn entry with an error flag.
Jérôme Glisse74eee182017-09-08 16:11:35 -0700801 *
802 * Expected use pattern:
803 * retry:
804 * down_read(&mm->mmap_sem);
805 * // Find vma and address device wants to fault, initialize hmm_pfn_t
806 * // array accordingly
Jérôme Glisse08232a42018-04-10 16:28:30 -0700807 * ret = hmm_vma_fault(range, write, block);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700808 * switch (ret) {
809 * case -EAGAIN:
Jérôme Glisse08232a42018-04-10 16:28:30 -0700810 * hmm_vma_range_done(range);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700811 * // You might want to rate limit or yield to play nicely, you may
812 * // also commit any valid pfn in the array assuming that you are
813 * // getting true from hmm_vma_range_monitor_end()
814 * goto retry;
815 * case 0:
816 * break;
Jérôme Glisse86586a42018-04-10 16:28:34 -0700817 * case -ENOMEM:
818 * case -EINVAL:
819 * case -EPERM:
Jérôme Glisse74eee182017-09-08 16:11:35 -0700820 * default:
821 * // Handle error !
822 * up_read(&mm->mmap_sem)
823 * return;
824 * }
825 * // Take device driver lock that serialize device page table update
826 * driver_lock_device_page_table_update();
Jérôme Glisse08232a42018-04-10 16:28:30 -0700827 * hmm_vma_range_done(range);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700828 * // Commit pfns we got from hmm_vma_fault()
829 * driver_unlock_device_page_table_update();
830 * up_read(&mm->mmap_sem)
831 *
832 * YOU MUST CALL hmm_vma_range_done() AFTER THIS FUNCTION RETURN SUCCESS (0)
833 * BEFORE FREEING THE range struct OR YOU WILL HAVE SERIOUS MEMORY CORRUPTION !
834 *
835 * YOU HAVE BEEN WARNED !
836 */
Jérôme Glisse2aee09d2018-04-10 16:29:02 -0700837int hmm_vma_fault(struct hmm_range *range, bool block)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700838{
Jérôme Glisse08232a42018-04-10 16:28:30 -0700839 struct vm_area_struct *vma = range->vma;
840 unsigned long start = range->start;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700841 struct hmm_vma_walk hmm_vma_walk;
842 struct mm_walk mm_walk;
843 struct hmm *hmm;
844 int ret;
845
846 /* Sanity check, this really should not happen ! */
Jérôme Glisse08232a42018-04-10 16:28:30 -0700847 if (range->start < vma->vm_start || range->start >= vma->vm_end)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700848 return -EINVAL;
Jérôme Glisse08232a42018-04-10 16:28:30 -0700849 if (range->end < vma->vm_start || range->end > vma->vm_end)
Jérôme Glisse74eee182017-09-08 16:11:35 -0700850 return -EINVAL;
851
852 hmm = hmm_register(vma->vm_mm);
853 if (!hmm) {
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700854 hmm_pfns_clear(range, range->pfns, range->start, range->end);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700855 return -ENOMEM;
856 }
857 /* Caller must have registered a mirror using hmm_mirror_register() */
858 if (!hmm->mmu_notifier.ops)
859 return -EINVAL;
860
Jérôme Glisse855ce7d2018-04-10 16:28:42 -0700861 /* FIXME support hugetlb fs */
Dave Jiange1fb4a02018-08-17 15:43:40 -0700862 if (is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_SPECIAL) ||
863 vma_is_dax(vma)) {
Jérôme Glisse855ce7d2018-04-10 16:28:42 -0700864 hmm_pfns_special(range);
865 return -EINVAL;
866 }
867
Jérôme Glisse86586a42018-04-10 16:28:34 -0700868 if (!(vma->vm_flags & VM_READ)) {
869 /*
870 * If vma do not allow read access, then assume that it does
871 * not allow write access, either. Architecture that allow
872 * write without read access are not supported by HMM, because
873 * operations such has atomic access would not work.
874 */
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700875 hmm_pfns_clear(range, range->pfns, range->start, range->end);
Jérôme Glisse86586a42018-04-10 16:28:34 -0700876 return -EPERM;
877 }
Jérôme Glisse74eee182017-09-08 16:11:35 -0700878
Jérôme Glisse86586a42018-04-10 16:28:34 -0700879 /* Initialize range to track CPU page table update */
880 spin_lock(&hmm->lock);
881 range->valid = true;
882 list_add_rcu(&range->list, &hmm->ranges);
883 spin_unlock(&hmm->lock);
884
Jérôme Glisse74eee182017-09-08 16:11:35 -0700885 hmm_vma_walk.fault = true;
Jérôme Glisse74eee182017-09-08 16:11:35 -0700886 hmm_vma_walk.block = block;
887 hmm_vma_walk.range = range;
888 mm_walk.private = &hmm_vma_walk;
889 hmm_vma_walk.last = range->start;
890
891 mm_walk.vma = vma;
892 mm_walk.mm = vma->vm_mm;
893 mm_walk.pte_entry = NULL;
894 mm_walk.test_walk = NULL;
895 mm_walk.hugetlb_entry = NULL;
896 mm_walk.pmd_entry = hmm_vma_walk_pmd;
897 mm_walk.pte_hole = hmm_vma_walk_hole;
898
899 do {
Jérôme Glisse08232a42018-04-10 16:28:30 -0700900 ret = walk_page_range(start, range->end, &mm_walk);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700901 start = hmm_vma_walk.last;
902 } while (ret == -EAGAIN);
903
904 if (ret) {
905 unsigned long i;
906
907 i = (hmm_vma_walk.last - range->start) >> PAGE_SHIFT;
Jérôme Glissef88a1e92018-04-10 16:29:06 -0700908 hmm_pfns_clear(range, &range->pfns[i], hmm_vma_walk.last,
909 range->end);
Jérôme Glisse08232a42018-04-10 16:28:30 -0700910 hmm_vma_range_done(range);
Jérôme Glisse74eee182017-09-08 16:11:35 -0700911 }
912 return ret;
913}
914EXPORT_SYMBOL(hmm_vma_fault);
Jérôme Glissec0b12402017-09-08 16:11:27 -0700915#endif /* IS_ENABLED(CONFIG_HMM_MIRROR) */
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700916
917
Jérôme Glissedf6ad692017-09-08 16:12:24 -0700918#if IS_ENABLED(CONFIG_DEVICE_PRIVATE) || IS_ENABLED(CONFIG_DEVICE_PUBLIC)
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700919struct page *hmm_vma_alloc_locked_page(struct vm_area_struct *vma,
920 unsigned long addr)
921{
922 struct page *page;
923
924 page = alloc_page_vma(GFP_HIGHUSER, vma, addr);
925 if (!page)
926 return NULL;
927 lock_page(page);
928 return page;
929}
930EXPORT_SYMBOL(hmm_vma_alloc_locked_page);
931
932
933static void hmm_devmem_ref_release(struct percpu_ref *ref)
934{
935 struct hmm_devmem *devmem;
936
937 devmem = container_of(ref, struct hmm_devmem, ref);
938 complete(&devmem->completion);
939}
940
941static void hmm_devmem_ref_exit(void *data)
942{
943 struct percpu_ref *ref = data;
944 struct hmm_devmem *devmem;
945
946 devmem = container_of(ref, struct hmm_devmem, ref);
947 percpu_ref_exit(ref);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700948}
949
950static void hmm_devmem_ref_kill(void *data)
951{
952 struct percpu_ref *ref = data;
953 struct hmm_devmem *devmem;
954
955 devmem = container_of(ref, struct hmm_devmem, ref);
956 percpu_ref_kill(ref);
957 wait_for_completion(&devmem->completion);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700958}
959
960static int hmm_devmem_fault(struct vm_area_struct *vma,
961 unsigned long addr,
962 const struct page *page,
963 unsigned int flags,
964 pmd_t *pmdp)
965{
966 struct hmm_devmem *devmem = page->pgmap->data;
967
968 return devmem->ops->fault(devmem, vma, addr, page, flags, pmdp);
969}
970
971static void hmm_devmem_free(struct page *page, void *data)
972{
973 struct hmm_devmem *devmem = data;
974
Dan Williams2fa147b2018-07-13 21:50:01 -0700975 page->mapping = NULL;
976
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700977 devmem->ops->free(devmem, page);
978}
979
980static DEFINE_MUTEX(hmm_devmem_lock);
981static RADIX_TREE(hmm_devmem_radix, GFP_KERNEL);
982
983static void hmm_devmem_radix_release(struct resource *resource)
984{
Colin Ian King1e926412018-08-17 15:50:07 -0700985 resource_size_t key;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700986
987 mutex_lock(&hmm_devmem_lock);
988 for (key = resource->start;
989 key <= resource->end;
990 key += PA_SECTION_SIZE)
991 radix_tree_delete(&hmm_devmem_radix, key >> PA_SECTION_SHIFT);
992 mutex_unlock(&hmm_devmem_lock);
993}
994
Dan Williamse890a862018-12-28 00:35:07 -0800995static void hmm_devmem_release(void *data)
Jérôme Glisse4ef589d2017-09-08 16:11:58 -0700996{
997 struct hmm_devmem *devmem = data;
998 struct resource *resource = devmem->resource;
999 unsigned long start_pfn, npages;
1000 struct zone *zone;
1001 struct page *page;
1002
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001003 /* pages are dead and unused, undo the arch mapping */
1004 start_pfn = (resource->start & ~(PA_SECTION_SIZE - 1)) >> PAGE_SHIFT;
1005 npages = ALIGN(resource_size(resource), PA_SECTION_SIZE) >> PAGE_SHIFT;
1006
1007 page = pfn_to_page(start_pfn);
1008 zone = page_zone(page);
1009
1010 mem_hotplug_begin();
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001011 if (resource->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY)
Christoph Hellwigda024512017-12-29 08:53:55 +01001012 __remove_pages(zone, start_pfn, npages, NULL);
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001013 else
1014 arch_remove_memory(start_pfn << PAGE_SHIFT,
Christoph Hellwigda024512017-12-29 08:53:55 +01001015 npages << PAGE_SHIFT, NULL);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001016 mem_hotplug_done();
1017
1018 hmm_devmem_radix_release(resource);
1019}
1020
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001021static int hmm_devmem_pages_create(struct hmm_devmem *devmem)
1022{
1023 resource_size_t key, align_start, align_size, align_end;
1024 struct device *device = devmem->device;
1025 int ret, nid, is_ram;
1026 unsigned long pfn;
1027
1028 align_start = devmem->resource->start & ~(PA_SECTION_SIZE - 1);
1029 align_size = ALIGN(devmem->resource->start +
1030 resource_size(devmem->resource),
1031 PA_SECTION_SIZE) - align_start;
1032
1033 is_ram = region_intersects(align_start, align_size,
1034 IORESOURCE_SYSTEM_RAM,
1035 IORES_DESC_NONE);
1036 if (is_ram == REGION_MIXED) {
1037 WARN_ONCE(1, "%s attempted on mixed region %pr\n",
1038 __func__, devmem->resource);
1039 return -ENXIO;
1040 }
1041 if (is_ram == REGION_INTERSECTS)
1042 return -ENXIO;
1043
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001044 if (devmem->resource->desc == IORES_DESC_DEVICE_PUBLIC_MEMORY)
1045 devmem->pagemap.type = MEMORY_DEVICE_PUBLIC;
1046 else
1047 devmem->pagemap.type = MEMORY_DEVICE_PRIVATE;
1048
Logan Gunthorpee7744aa2017-12-29 08:54:04 +01001049 devmem->pagemap.res = *devmem->resource;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001050 devmem->pagemap.page_fault = hmm_devmem_fault;
1051 devmem->pagemap.page_free = hmm_devmem_free;
1052 devmem->pagemap.dev = devmem->device;
1053 devmem->pagemap.ref = &devmem->ref;
1054 devmem->pagemap.data = devmem;
1055
1056 mutex_lock(&hmm_devmem_lock);
1057 align_end = align_start + align_size - 1;
1058 for (key = align_start; key <= align_end; key += PA_SECTION_SIZE) {
1059 struct hmm_devmem *dup;
1060
Tejun Heo18be4602018-04-10 16:29:09 -07001061 dup = radix_tree_lookup(&hmm_devmem_radix,
1062 key >> PA_SECTION_SHIFT);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001063 if (dup) {
1064 dev_err(device, "%s: collides with mapping for %s\n",
1065 __func__, dev_name(dup->device));
1066 mutex_unlock(&hmm_devmem_lock);
1067 ret = -EBUSY;
1068 goto error;
1069 }
1070 ret = radix_tree_insert(&hmm_devmem_radix,
1071 key >> PA_SECTION_SHIFT,
1072 devmem);
1073 if (ret) {
1074 dev_err(device, "%s: failed: %d\n", __func__, ret);
1075 mutex_unlock(&hmm_devmem_lock);
1076 goto error_radix;
1077 }
1078 }
1079 mutex_unlock(&hmm_devmem_lock);
1080
1081 nid = dev_to_node(device);
1082 if (nid < 0)
1083 nid = numa_mem_id();
1084
1085 mem_hotplug_begin();
1086 /*
1087 * For device private memory we call add_pages() as we only need to
1088 * allocate and initialize struct page for the device memory. More-
1089 * over the device memory is un-accessible thus we do not want to
1090 * create a linear mapping for the memory like arch_add_memory()
1091 * would do.
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001092 *
1093 * For device public memory, which is accesible by the CPU, we do
1094 * want the linear mapping and thus use arch_add_memory().
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001095 */
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001096 if (devmem->pagemap.type == MEMORY_DEVICE_PUBLIC)
Christoph Hellwig24e6d5a2017-12-29 08:53:53 +01001097 ret = arch_add_memory(nid, align_start, align_size, NULL,
1098 false);
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001099 else
1100 ret = add_pages(nid, align_start >> PAGE_SHIFT,
Christoph Hellwig24e6d5a2017-12-29 08:53:53 +01001101 align_size >> PAGE_SHIFT, NULL, false);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001102 if (ret) {
1103 mem_hotplug_done();
1104 goto error_add_memory;
1105 }
1106 move_pfn_range_to_zone(&NODE_DATA(nid)->node_zones[ZONE_DEVICE],
1107 align_start >> PAGE_SHIFT,
Christoph Hellwiga99583e2017-12-29 08:53:57 +01001108 align_size >> PAGE_SHIFT, NULL);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001109 mem_hotplug_done();
1110
1111 for (pfn = devmem->pfn_first; pfn < devmem->pfn_last; pfn++) {
1112 struct page *page = pfn_to_page(pfn);
1113
1114 page->pgmap = &devmem->pagemap;
1115 }
1116 return 0;
1117
1118error_add_memory:
1119 untrack_pfn(NULL, PHYS_PFN(align_start), align_size);
1120error_radix:
1121 hmm_devmem_radix_release(devmem->resource);
1122error:
1123 return ret;
1124}
1125
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001126/*
1127 * hmm_devmem_add() - hotplug ZONE_DEVICE memory for device memory
1128 *
1129 * @ops: memory event device driver callback (see struct hmm_devmem_ops)
1130 * @device: device struct to bind the resource too
1131 * @size: size in bytes of the device memory to add
1132 * Returns: pointer to new hmm_devmem struct ERR_PTR otherwise
1133 *
1134 * This function first finds an empty range of physical address big enough to
1135 * contain the new resource, and then hotplugs it as ZONE_DEVICE memory, which
1136 * in turn allocates struct pages. It does not do anything beyond that; all
1137 * events affecting the memory will go through the various callbacks provided
1138 * by hmm_devmem_ops struct.
1139 *
1140 * Device driver should call this function during device initialization and
1141 * is then responsible of memory management. HMM only provides helpers.
1142 */
1143struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops,
1144 struct device *device,
1145 unsigned long size)
1146{
1147 struct hmm_devmem *devmem;
1148 resource_size_t addr;
1149 int ret;
1150
Dan Williamse76384882018-05-16 11:46:08 -07001151 dev_pagemap_get_ops();
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001152
Dan Williamse890a862018-12-28 00:35:07 -08001153 devmem = devm_kzalloc(device, sizeof(*devmem), GFP_KERNEL);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001154 if (!devmem)
1155 return ERR_PTR(-ENOMEM);
1156
1157 init_completion(&devmem->completion);
1158 devmem->pfn_first = -1UL;
1159 devmem->pfn_last = -1UL;
1160 devmem->resource = NULL;
1161 devmem->device = device;
1162 devmem->ops = ops;
1163
1164 ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release,
1165 0, GFP_KERNEL);
1166 if (ret)
Dan Williamse890a862018-12-28 00:35:07 -08001167 return ERR_PTR(ret);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001168
Dan Williamse890a862018-12-28 00:35:07 -08001169 ret = devm_add_action_or_reset(device, hmm_devmem_ref_exit, &devmem->ref);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001170 if (ret)
Dan Williamse890a862018-12-28 00:35:07 -08001171 return ERR_PTR(ret);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001172
1173 size = ALIGN(size, PA_SECTION_SIZE);
1174 addr = min((unsigned long)iomem_resource.end,
1175 (1UL << MAX_PHYSMEM_BITS) - 1);
1176 addr = addr - size + 1UL;
1177
1178 /*
1179 * FIXME add a new helper to quickly walk resource tree and find free
1180 * range
1181 *
1182 * FIXME what about ioport_resource resource ?
1183 */
1184 for (; addr > size && addr >= iomem_resource.start; addr -= size) {
1185 ret = region_intersects(addr, size, 0, IORES_DESC_NONE);
1186 if (ret != REGION_DISJOINT)
1187 continue;
1188
1189 devmem->resource = devm_request_mem_region(device, addr, size,
1190 dev_name(device));
Dan Williamse890a862018-12-28 00:35:07 -08001191 if (!devmem->resource)
1192 return ERR_PTR(-ENOMEM);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001193 break;
1194 }
Dan Williamse890a862018-12-28 00:35:07 -08001195 if (!devmem->resource)
1196 return ERR_PTR(-ERANGE);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001197
1198 devmem->resource->desc = IORES_DESC_DEVICE_PRIVATE_MEMORY;
1199 devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
1200 devmem->pfn_last = devmem->pfn_first +
1201 (resource_size(devmem->resource) >> PAGE_SHIFT);
1202
1203 ret = hmm_devmem_pages_create(devmem);
1204 if (ret)
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001205 return ERR_PTR(ret);
Dan Williamse890a862018-12-28 00:35:07 -08001206
1207 ret = devm_add_action_or_reset(device, hmm_devmem_release, devmem);
1208 if (ret)
1209 return ERR_PTR(ret);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001210
1211 return devmem;
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001212}
Dan Williams0f1a62e2018-12-28 00:35:15 -08001213EXPORT_SYMBOL_GPL(hmm_devmem_add);
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001214
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001215struct hmm_devmem *hmm_devmem_add_resource(const struct hmm_devmem_ops *ops,
1216 struct device *device,
1217 struct resource *res)
1218{
1219 struct hmm_devmem *devmem;
1220 int ret;
1221
1222 if (res->desc != IORES_DESC_DEVICE_PUBLIC_MEMORY)
1223 return ERR_PTR(-EINVAL);
1224
Dan Williamse76384882018-05-16 11:46:08 -07001225 dev_pagemap_get_ops();
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001226
Dan Williamse890a862018-12-28 00:35:07 -08001227 devmem = devm_kzalloc(device, sizeof(*devmem), GFP_KERNEL);
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001228 if (!devmem)
1229 return ERR_PTR(-ENOMEM);
1230
1231 init_completion(&devmem->completion);
1232 devmem->pfn_first = -1UL;
1233 devmem->pfn_last = -1UL;
1234 devmem->resource = res;
1235 devmem->device = device;
1236 devmem->ops = ops;
1237
1238 ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release,
1239 0, GFP_KERNEL);
1240 if (ret)
Dan Williamse890a862018-12-28 00:35:07 -08001241 return ERR_PTR(ret);
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001242
Dan Williamse890a862018-12-28 00:35:07 -08001243 ret = devm_add_action_or_reset(device, hmm_devmem_ref_exit,
1244 &devmem->ref);
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001245 if (ret)
Dan Williamse890a862018-12-28 00:35:07 -08001246 return ERR_PTR(ret);
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001247
1248 devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
1249 devmem->pfn_last = devmem->pfn_first +
1250 (resource_size(devmem->resource) >> PAGE_SHIFT);
1251
1252 ret = hmm_devmem_pages_create(devmem);
1253 if (ret)
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001254 return ERR_PTR(ret);
Dan Williamse890a862018-12-28 00:35:07 -08001255
1256 ret = devm_add_action_or_reset(device, hmm_devmem_release, devmem);
1257 if (ret)
1258 return ERR_PTR(ret);
1259
1260 ret = devm_add_action_or_reset(device, hmm_devmem_ref_kill,
1261 &devmem->ref);
1262 if (ret)
1263 return ERR_PTR(ret);
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001264
1265 return devmem;
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001266}
Dan Williams0f1a62e2018-12-28 00:35:15 -08001267EXPORT_SYMBOL_GPL(hmm_devmem_add_resource);
Jérôme Glissed3df0a42017-09-08 16:12:28 -07001268
Jérôme Glisse4ef589d2017-09-08 16:11:58 -07001269/*
Jérôme Glisse858b54d2017-09-08 16:12:02 -07001270 * A device driver that wants to handle multiple devices memory through a
1271 * single fake device can use hmm_device to do so. This is purely a helper
1272 * and it is not needed to make use of any HMM functionality.
1273 */
1274#define HMM_DEVICE_MAX 256
1275
1276static DECLARE_BITMAP(hmm_device_mask, HMM_DEVICE_MAX);
1277static DEFINE_SPINLOCK(hmm_device_lock);
1278static struct class *hmm_device_class;
1279static dev_t hmm_device_devt;
1280
1281static void hmm_device_release(struct device *device)
1282{
1283 struct hmm_device *hmm_device;
1284
1285 hmm_device = container_of(device, struct hmm_device, device);
1286 spin_lock(&hmm_device_lock);
1287 clear_bit(hmm_device->minor, hmm_device_mask);
1288 spin_unlock(&hmm_device_lock);
1289
1290 kfree(hmm_device);
1291}
1292
1293struct hmm_device *hmm_device_new(void *drvdata)
1294{
1295 struct hmm_device *hmm_device;
1296
1297 hmm_device = kzalloc(sizeof(*hmm_device), GFP_KERNEL);
1298 if (!hmm_device)
1299 return ERR_PTR(-ENOMEM);
1300
1301 spin_lock(&hmm_device_lock);
1302 hmm_device->minor = find_first_zero_bit(hmm_device_mask, HMM_DEVICE_MAX);
1303 if (hmm_device->minor >= HMM_DEVICE_MAX) {
1304 spin_unlock(&hmm_device_lock);
1305 kfree(hmm_device);
1306 return ERR_PTR(-EBUSY);
1307 }
1308 set_bit(hmm_device->minor, hmm_device_mask);
1309 spin_unlock(&hmm_device_lock);
1310
1311 dev_set_name(&hmm_device->device, "hmm_device%d", hmm_device->minor);
1312 hmm_device->device.devt = MKDEV(MAJOR(hmm_device_devt),
1313 hmm_device->minor);
1314 hmm_device->device.release = hmm_device_release;
1315 dev_set_drvdata(&hmm_device->device, drvdata);
1316 hmm_device->device.class = hmm_device_class;
1317 device_initialize(&hmm_device->device);
1318
1319 return hmm_device;
1320}
1321EXPORT_SYMBOL(hmm_device_new);
1322
1323void hmm_device_put(struct hmm_device *hmm_device)
1324{
1325 put_device(&hmm_device->device);
1326}
1327EXPORT_SYMBOL(hmm_device_put);
1328
1329static int __init hmm_init(void)
1330{
1331 int ret;
1332
1333 ret = alloc_chrdev_region(&hmm_device_devt, 0,
1334 HMM_DEVICE_MAX,
1335 "hmm_device");
1336 if (ret)
1337 return ret;
1338
1339 hmm_device_class = class_create(THIS_MODULE, "hmm_device");
1340 if (IS_ERR(hmm_device_class)) {
1341 unregister_chrdev_region(hmm_device_devt, HMM_DEVICE_MAX);
1342 return PTR_ERR(hmm_device_class);
1343 }
1344 return 0;
1345}
1346
1347device_initcall(hmm_init);
Jérôme Glissedf6ad692017-09-08 16:12:24 -07001348#endif /* CONFIG_DEVICE_PRIVATE || CONFIG_DEVICE_PUBLIC */