]>
Commit | Line | Data |
---|---|---|
133ff0ea JG |
1 | /* |
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 | * | |
f813f219 | 14 | * Authors: Jérôme Glisse <[email protected]> |
133ff0ea JG |
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> | |
858b54da | 22 | #include <linux/init.h> |
da4c3c73 JG |
23 | #include <linux/rmap.h> |
24 | #include <linux/swap.h> | |
133ff0ea JG |
25 | #include <linux/slab.h> |
26 | #include <linux/sched.h> | |
4ef589dc JG |
27 | #include <linux/mmzone.h> |
28 | #include <linux/pagemap.h> | |
da4c3c73 JG |
29 | #include <linux/swapops.h> |
30 | #include <linux/hugetlb.h> | |
4ef589dc | 31 | #include <linux/memremap.h> |
7b2d55d2 | 32 | #include <linux/jump_label.h> |
c0b12405 | 33 | #include <linux/mmu_notifier.h> |
4ef589dc JG |
34 | #include <linux/memory_hotplug.h> |
35 | ||
36 | #define PA_SECTION_SIZE (1UL << PA_SECTION_SHIFT) | |
133ff0ea | 37 | |
6b368cd4 | 38 | #if IS_ENABLED(CONFIG_HMM_MIRROR) |
c0b12405 JG |
39 | static const struct mmu_notifier_ops hmm_mmu_notifier_ops; |
40 | ||
133ff0ea JG |
41 | /* |
42 | * struct hmm - HMM per mm struct | |
43 | * | |
44 | * @mm: mm struct this HMM struct is bound to | |
da4c3c73 | 45 | * @lock: lock protecting ranges list |
da4c3c73 | 46 | * @ranges: list of range being snapshotted |
c0b12405 JG |
47 | * @mirrors: list of mirrors for this mm |
48 | * @mmu_notifier: mmu notifier to track updates to CPU page table | |
49 | * @mirrors_sem: read/write semaphore protecting the mirrors list | |
133ff0ea JG |
50 | */ |
51 | struct hmm { | |
52 | struct mm_struct *mm; | |
704f3f2c | 53 | struct kref kref; |
da4c3c73 | 54 | spinlock_t lock; |
da4c3c73 | 55 | struct list_head ranges; |
c0b12405 JG |
56 | struct list_head mirrors; |
57 | struct mmu_notifier mmu_notifier; | |
58 | struct rw_semaphore mirrors_sem; | |
133ff0ea JG |
59 | }; |
60 | ||
704f3f2c JG |
61 | static inline struct hmm *mm_get_hmm(struct mm_struct *mm) |
62 | { | |
63 | struct hmm *hmm = READ_ONCE(mm->hmm); | |
64 | ||
65 | if (hmm && kref_get_unless_zero(&hmm->kref)) | |
66 | return hmm; | |
67 | ||
68 | return NULL; | |
69 | } | |
70 | ||
71 | /** | |
72 | * hmm_get_or_create - register HMM against an mm (HMM internal) | |
133ff0ea JG |
73 | * |
74 | * @mm: mm struct to attach to | |
704f3f2c JG |
75 | * Returns: returns an HMM object, either by referencing the existing |
76 | * (per-process) object, or by creating a new one. | |
133ff0ea | 77 | * |
704f3f2c JG |
78 | * This is not intended to be used directly by device drivers. If mm already |
79 | * has an HMM struct then it get a reference on it and returns it. Otherwise | |
80 | * it allocates an HMM struct, initializes it, associate it with the mm and | |
81 | * returns it. | |
133ff0ea | 82 | */ |
704f3f2c | 83 | static struct hmm *hmm_get_or_create(struct mm_struct *mm) |
133ff0ea | 84 | { |
704f3f2c | 85 | struct hmm *hmm = mm_get_hmm(mm); |
c0b12405 | 86 | bool cleanup = false; |
133ff0ea | 87 | |
c0b12405 JG |
88 | if (hmm) |
89 | return hmm; | |
90 | ||
91 | hmm = kmalloc(sizeof(*hmm), GFP_KERNEL); | |
92 | if (!hmm) | |
93 | return NULL; | |
94 | INIT_LIST_HEAD(&hmm->mirrors); | |
95 | init_rwsem(&hmm->mirrors_sem); | |
c0b12405 | 96 | hmm->mmu_notifier.ops = NULL; |
da4c3c73 JG |
97 | INIT_LIST_HEAD(&hmm->ranges); |
98 | spin_lock_init(&hmm->lock); | |
704f3f2c | 99 | kref_init(&hmm->kref); |
c0b12405 JG |
100 | hmm->mm = mm; |
101 | ||
c0b12405 JG |
102 | spin_lock(&mm->page_table_lock); |
103 | if (!mm->hmm) | |
104 | mm->hmm = hmm; | |
105 | else | |
106 | cleanup = true; | |
107 | spin_unlock(&mm->page_table_lock); | |
108 | ||
86a2d598 RC |
109 | if (cleanup) |
110 | goto error; | |
111 | ||
112 | /* | |
113 | * We should only get here if hold the mmap_sem in write mode ie on | |
114 | * registration of first mirror through hmm_mirror_register() | |
115 | */ | |
116 | hmm->mmu_notifier.ops = &hmm_mmu_notifier_ops; | |
117 | if (__mmu_notifier_register(&hmm->mmu_notifier, mm)) | |
118 | goto error_mm; | |
c0b12405 | 119 | |
704f3f2c | 120 | return hmm; |
86a2d598 RC |
121 | |
122 | error_mm: | |
123 | spin_lock(&mm->page_table_lock); | |
124 | if (mm->hmm == hmm) | |
125 | mm->hmm = NULL; | |
126 | spin_unlock(&mm->page_table_lock); | |
127 | error: | |
128 | kfree(hmm); | |
129 | return NULL; | |
133ff0ea JG |
130 | } |
131 | ||
704f3f2c JG |
132 | static void hmm_free(struct kref *kref) |
133 | { | |
134 | struct hmm *hmm = container_of(kref, struct hmm, kref); | |
135 | struct mm_struct *mm = hmm->mm; | |
136 | ||
137 | mmu_notifier_unregister_no_release(&hmm->mmu_notifier, mm); | |
138 | ||
139 | spin_lock(&mm->page_table_lock); | |
140 | if (mm->hmm == hmm) | |
141 | mm->hmm = NULL; | |
142 | spin_unlock(&mm->page_table_lock); | |
143 | ||
144 | kfree(hmm); | |
145 | } | |
146 | ||
147 | static inline void hmm_put(struct hmm *hmm) | |
148 | { | |
149 | kref_put(&hmm->kref, hmm_free); | |
150 | } | |
151 | ||
133ff0ea JG |
152 | void hmm_mm_destroy(struct mm_struct *mm) |
153 | { | |
704f3f2c JG |
154 | struct hmm *hmm; |
155 | ||
156 | spin_lock(&mm->page_table_lock); | |
157 | hmm = mm_get_hmm(mm); | |
158 | mm->hmm = NULL; | |
159 | if (hmm) { | |
160 | hmm->mm = NULL; | |
161 | spin_unlock(&mm->page_table_lock); | |
162 | hmm_put(hmm); | |
163 | return; | |
164 | } | |
165 | ||
166 | spin_unlock(&mm->page_table_lock); | |
133ff0ea | 167 | } |
c0b12405 | 168 | |
ec131b2d | 169 | static int hmm_invalidate_range(struct hmm *hmm, bool device, |
44532d4c | 170 | const struct hmm_update *update) |
c0b12405 JG |
171 | { |
172 | struct hmm_mirror *mirror; | |
da4c3c73 JG |
173 | struct hmm_range *range; |
174 | ||
175 | spin_lock(&hmm->lock); | |
176 | list_for_each_entry(range, &hmm->ranges, list) { | |
44532d4c | 177 | if (update->end < range->start || update->start >= range->end) |
da4c3c73 JG |
178 | continue; |
179 | ||
180 | range->valid = false; | |
da4c3c73 JG |
181 | } |
182 | spin_unlock(&hmm->lock); | |
c0b12405 | 183 | |
ec131b2d JG |
184 | if (!device) |
185 | return 0; | |
186 | ||
c0b12405 | 187 | down_read(&hmm->mirrors_sem); |
44532d4c JG |
188 | list_for_each_entry(mirror, &hmm->mirrors, list) { |
189 | int ret; | |
190 | ||
191 | ret = mirror->ops->sync_cpu_device_pagetables(mirror, update); | |
192 | if (!update->blockable && ret == -EAGAIN) { | |
193 | up_read(&hmm->mirrors_sem); | |
194 | return -EAGAIN; | |
195 | } | |
196 | } | |
c0b12405 | 197 | up_read(&hmm->mirrors_sem); |
44532d4c JG |
198 | |
199 | return 0; | |
c0b12405 JG |
200 | } |
201 | ||
e1401513 RC |
202 | static void hmm_release(struct mmu_notifier *mn, struct mm_struct *mm) |
203 | { | |
204 | struct hmm_mirror *mirror; | |
704f3f2c | 205 | struct hmm *hmm = mm_get_hmm(mm); |
e1401513 RC |
206 | |
207 | down_write(&hmm->mirrors_sem); | |
208 | mirror = list_first_entry_or_null(&hmm->mirrors, struct hmm_mirror, | |
209 | list); | |
210 | while (mirror) { | |
211 | list_del_init(&mirror->list); | |
212 | if (mirror->ops->release) { | |
213 | /* | |
214 | * Drop mirrors_sem so callback can wait on any pending | |
215 | * work that might itself trigger mmu_notifier callback | |
216 | * and thus would deadlock with us. | |
217 | */ | |
218 | up_write(&hmm->mirrors_sem); | |
219 | mirror->ops->release(mirror); | |
220 | down_write(&hmm->mirrors_sem); | |
221 | } | |
222 | mirror = list_first_entry_or_null(&hmm->mirrors, | |
223 | struct hmm_mirror, list); | |
224 | } | |
225 | up_write(&hmm->mirrors_sem); | |
704f3f2c JG |
226 | |
227 | hmm_put(hmm); | |
e1401513 RC |
228 | } |
229 | ||
93065ac7 | 230 | static int hmm_invalidate_range_start(struct mmu_notifier *mn, |
5d6527a7 | 231 | const struct mmu_notifier_range *range) |
c0b12405 | 232 | { |
704f3f2c | 233 | struct hmm *hmm = mm_get_hmm(range->mm); |
ec131b2d | 234 | struct hmm_update update; |
704f3f2c | 235 | int ret; |
c0b12405 JG |
236 | |
237 | VM_BUG_ON(!hmm); | |
238 | ||
5d6527a7 JG |
239 | update.start = range->start; |
240 | update.end = range->end; | |
ec131b2d | 241 | update.event = HMM_UPDATE_INVALIDATE; |
5d6527a7 | 242 | update.blockable = range->blockable; |
704f3f2c JG |
243 | ret = hmm_invalidate_range(hmm, true, &update); |
244 | hmm_put(hmm); | |
245 | return ret; | |
c0b12405 JG |
246 | } |
247 | ||
248 | static void hmm_invalidate_range_end(struct mmu_notifier *mn, | |
5d6527a7 | 249 | const struct mmu_notifier_range *range) |
c0b12405 | 250 | { |
704f3f2c | 251 | struct hmm *hmm = mm_get_hmm(range->mm); |
44532d4c | 252 | struct hmm_update update; |
c0b12405 JG |
253 | |
254 | VM_BUG_ON(!hmm); | |
255 | ||
5d6527a7 JG |
256 | update.start = range->start; |
257 | update.end = range->end; | |
44532d4c JG |
258 | update.event = HMM_UPDATE_INVALIDATE; |
259 | update.blockable = true; | |
ec131b2d | 260 | hmm_invalidate_range(hmm, false, &update); |
704f3f2c | 261 | hmm_put(hmm); |
c0b12405 JG |
262 | } |
263 | ||
264 | static const struct mmu_notifier_ops hmm_mmu_notifier_ops = { | |
e1401513 | 265 | .release = hmm_release, |
c0b12405 JG |
266 | .invalidate_range_start = hmm_invalidate_range_start, |
267 | .invalidate_range_end = hmm_invalidate_range_end, | |
268 | }; | |
269 | ||
270 | /* | |
271 | * hmm_mirror_register() - register a mirror against an mm | |
272 | * | |
273 | * @mirror: new mirror struct to register | |
274 | * @mm: mm to register against | |
275 | * | |
276 | * To start mirroring a process address space, the device driver must register | |
277 | * an HMM mirror struct. | |
278 | * | |
279 | * THE mm->mmap_sem MUST BE HELD IN WRITE MODE ! | |
280 | */ | |
281 | int hmm_mirror_register(struct hmm_mirror *mirror, struct mm_struct *mm) | |
282 | { | |
283 | /* Sanity check */ | |
284 | if (!mm || !mirror || !mirror->ops) | |
285 | return -EINVAL; | |
286 | ||
704f3f2c | 287 | mirror->hmm = hmm_get_or_create(mm); |
c0b12405 JG |
288 | if (!mirror->hmm) |
289 | return -ENOMEM; | |
290 | ||
291 | down_write(&mirror->hmm->mirrors_sem); | |
704f3f2c JG |
292 | list_add(&mirror->list, &mirror->hmm->mirrors); |
293 | up_write(&mirror->hmm->mirrors_sem); | |
c0b12405 JG |
294 | |
295 | return 0; | |
296 | } | |
297 | EXPORT_SYMBOL(hmm_mirror_register); | |
298 | ||
299 | /* | |
300 | * hmm_mirror_unregister() - unregister a mirror | |
301 | * | |
302 | * @mirror: new mirror struct to register | |
303 | * | |
304 | * Stop mirroring a process address space, and cleanup. | |
305 | */ | |
306 | void hmm_mirror_unregister(struct hmm_mirror *mirror) | |
307 | { | |
704f3f2c | 308 | struct hmm *hmm = READ_ONCE(mirror->hmm); |
c01cbba2 | 309 | |
704f3f2c | 310 | if (hmm == NULL) |
c01cbba2 | 311 | return; |
c0b12405 JG |
312 | |
313 | down_write(&hmm->mirrors_sem); | |
e1401513 | 314 | list_del_init(&mirror->list); |
704f3f2c | 315 | /* To protect us against double unregister ... */ |
c01cbba2 | 316 | mirror->hmm = NULL; |
c0b12405 | 317 | up_write(&hmm->mirrors_sem); |
c01cbba2 | 318 | |
704f3f2c | 319 | hmm_put(hmm); |
c0b12405 JG |
320 | } |
321 | EXPORT_SYMBOL(hmm_mirror_unregister); | |
da4c3c73 | 322 | |
74eee180 JG |
323 | struct hmm_vma_walk { |
324 | struct hmm_range *range; | |
325 | unsigned long last; | |
326 | bool fault; | |
327 | bool block; | |
74eee180 JG |
328 | }; |
329 | ||
2aee09d8 JG |
330 | static int hmm_vma_do_fault(struct mm_walk *walk, unsigned long addr, |
331 | bool write_fault, uint64_t *pfn) | |
74eee180 JG |
332 | { |
333 | unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_REMOTE; | |
334 | struct hmm_vma_walk *hmm_vma_walk = walk->private; | |
f88a1e90 | 335 | struct hmm_range *range = hmm_vma_walk->range; |
74eee180 | 336 | struct vm_area_struct *vma = walk->vma; |
50a7ca3c | 337 | vm_fault_t ret; |
74eee180 JG |
338 | |
339 | flags |= hmm_vma_walk->block ? 0 : FAULT_FLAG_ALLOW_RETRY; | |
2aee09d8 | 340 | flags |= write_fault ? FAULT_FLAG_WRITE : 0; |
50a7ca3c SJ |
341 | ret = handle_mm_fault(vma, addr, flags); |
342 | if (ret & VM_FAULT_RETRY) | |
73231612 | 343 | return -EAGAIN; |
50a7ca3c | 344 | if (ret & VM_FAULT_ERROR) { |
f88a1e90 | 345 | *pfn = range->values[HMM_PFN_ERROR]; |
74eee180 JG |
346 | return -EFAULT; |
347 | } | |
348 | ||
73231612 | 349 | return -EBUSY; |
74eee180 JG |
350 | } |
351 | ||
da4c3c73 JG |
352 | static int hmm_pfns_bad(unsigned long addr, |
353 | unsigned long end, | |
354 | struct mm_walk *walk) | |
355 | { | |
c719547f JG |
356 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
357 | struct hmm_range *range = hmm_vma_walk->range; | |
ff05c0c6 | 358 | uint64_t *pfns = range->pfns; |
da4c3c73 JG |
359 | unsigned long i; |
360 | ||
361 | i = (addr - range->start) >> PAGE_SHIFT; | |
362 | for (; addr < end; addr += PAGE_SIZE, i++) | |
f88a1e90 | 363 | pfns[i] = range->values[HMM_PFN_ERROR]; |
da4c3c73 JG |
364 | |
365 | return 0; | |
366 | } | |
367 | ||
5504ed29 JG |
368 | /* |
369 | * hmm_vma_walk_hole() - handle a range lacking valid pmd or pte(s) | |
370 | * @start: range virtual start address (inclusive) | |
371 | * @end: range virtual end address (exclusive) | |
2aee09d8 JG |
372 | * @fault: should we fault or not ? |
373 | * @write_fault: write fault ? | |
5504ed29 | 374 | * @walk: mm_walk structure |
73231612 | 375 | * Returns: 0 on success, -EBUSY after page fault, or page fault error |
5504ed29 JG |
376 | * |
377 | * This function will be called whenever pmd_none() or pte_none() returns true, | |
378 | * or whenever there is no page directory covering the virtual address range. | |
379 | */ | |
2aee09d8 JG |
380 | static int hmm_vma_walk_hole_(unsigned long addr, unsigned long end, |
381 | bool fault, bool write_fault, | |
382 | struct mm_walk *walk) | |
da4c3c73 | 383 | { |
74eee180 JG |
384 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
385 | struct hmm_range *range = hmm_vma_walk->range; | |
ff05c0c6 | 386 | uint64_t *pfns = range->pfns; |
da4c3c73 JG |
387 | unsigned long i; |
388 | ||
74eee180 | 389 | hmm_vma_walk->last = addr; |
da4c3c73 | 390 | i = (addr - range->start) >> PAGE_SHIFT; |
74eee180 | 391 | for (; addr < end; addr += PAGE_SIZE, i++) { |
f88a1e90 | 392 | pfns[i] = range->values[HMM_PFN_NONE]; |
2aee09d8 | 393 | if (fault || write_fault) { |
74eee180 | 394 | int ret; |
da4c3c73 | 395 | |
2aee09d8 JG |
396 | ret = hmm_vma_do_fault(walk, addr, write_fault, |
397 | &pfns[i]); | |
73231612 | 398 | if (ret != -EBUSY) |
74eee180 JG |
399 | return ret; |
400 | } | |
401 | } | |
402 | ||
73231612 | 403 | return (fault || write_fault) ? -EBUSY : 0; |
2aee09d8 JG |
404 | } |
405 | ||
406 | static inline void hmm_pte_need_fault(const struct hmm_vma_walk *hmm_vma_walk, | |
407 | uint64_t pfns, uint64_t cpu_flags, | |
408 | bool *fault, bool *write_fault) | |
409 | { | |
f88a1e90 JG |
410 | struct hmm_range *range = hmm_vma_walk->range; |
411 | ||
2aee09d8 JG |
412 | *fault = *write_fault = false; |
413 | if (!hmm_vma_walk->fault) | |
414 | return; | |
415 | ||
416 | /* We aren't ask to do anything ... */ | |
f88a1e90 | 417 | if (!(pfns & range->flags[HMM_PFN_VALID])) |
2aee09d8 | 418 | return; |
f88a1e90 JG |
419 | /* If this is device memory than only fault if explicitly requested */ |
420 | if ((cpu_flags & range->flags[HMM_PFN_DEVICE_PRIVATE])) { | |
421 | /* Do we fault on device memory ? */ | |
422 | if (pfns & range->flags[HMM_PFN_DEVICE_PRIVATE]) { | |
423 | *write_fault = pfns & range->flags[HMM_PFN_WRITE]; | |
424 | *fault = true; | |
425 | } | |
2aee09d8 JG |
426 | return; |
427 | } | |
f88a1e90 JG |
428 | |
429 | /* If CPU page table is not valid then we need to fault */ | |
430 | *fault = !(cpu_flags & range->flags[HMM_PFN_VALID]); | |
431 | /* Need to write fault ? */ | |
432 | if ((pfns & range->flags[HMM_PFN_WRITE]) && | |
433 | !(cpu_flags & range->flags[HMM_PFN_WRITE])) { | |
434 | *write_fault = true; | |
2aee09d8 JG |
435 | *fault = true; |
436 | } | |
437 | } | |
438 | ||
439 | static void hmm_range_need_fault(const struct hmm_vma_walk *hmm_vma_walk, | |
440 | const uint64_t *pfns, unsigned long npages, | |
441 | uint64_t cpu_flags, bool *fault, | |
442 | bool *write_fault) | |
443 | { | |
444 | unsigned long i; | |
445 | ||
446 | if (!hmm_vma_walk->fault) { | |
447 | *fault = *write_fault = false; | |
448 | return; | |
449 | } | |
450 | ||
451 | for (i = 0; i < npages; ++i) { | |
452 | hmm_pte_need_fault(hmm_vma_walk, pfns[i], cpu_flags, | |
453 | fault, write_fault); | |
454 | if ((*fault) || (*write_fault)) | |
455 | return; | |
456 | } | |
457 | } | |
458 | ||
459 | static int hmm_vma_walk_hole(unsigned long addr, unsigned long end, | |
460 | struct mm_walk *walk) | |
461 | { | |
462 | struct hmm_vma_walk *hmm_vma_walk = walk->private; | |
463 | struct hmm_range *range = hmm_vma_walk->range; | |
464 | bool fault, write_fault; | |
465 | unsigned long i, npages; | |
466 | uint64_t *pfns; | |
467 | ||
468 | i = (addr - range->start) >> PAGE_SHIFT; | |
469 | npages = (end - addr) >> PAGE_SHIFT; | |
470 | pfns = &range->pfns[i]; | |
471 | hmm_range_need_fault(hmm_vma_walk, pfns, npages, | |
472 | 0, &fault, &write_fault); | |
473 | return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk); | |
474 | } | |
475 | ||
f88a1e90 | 476 | static inline uint64_t pmd_to_hmm_pfn_flags(struct hmm_range *range, pmd_t pmd) |
2aee09d8 JG |
477 | { |
478 | if (pmd_protnone(pmd)) | |
479 | return 0; | |
f88a1e90 JG |
480 | return pmd_write(pmd) ? range->flags[HMM_PFN_VALID] | |
481 | range->flags[HMM_PFN_WRITE] : | |
482 | range->flags[HMM_PFN_VALID]; | |
da4c3c73 JG |
483 | } |
484 | ||
53f5c3f4 JG |
485 | static int hmm_vma_handle_pmd(struct mm_walk *walk, |
486 | unsigned long addr, | |
487 | unsigned long end, | |
488 | uint64_t *pfns, | |
489 | pmd_t pmd) | |
490 | { | |
491 | struct hmm_vma_walk *hmm_vma_walk = walk->private; | |
f88a1e90 | 492 | struct hmm_range *range = hmm_vma_walk->range; |
2aee09d8 | 493 | unsigned long pfn, npages, i; |
2aee09d8 | 494 | bool fault, write_fault; |
f88a1e90 | 495 | uint64_t cpu_flags; |
53f5c3f4 | 496 | |
2aee09d8 | 497 | npages = (end - addr) >> PAGE_SHIFT; |
f88a1e90 | 498 | cpu_flags = pmd_to_hmm_pfn_flags(range, pmd); |
2aee09d8 JG |
499 | hmm_range_need_fault(hmm_vma_walk, pfns, npages, cpu_flags, |
500 | &fault, &write_fault); | |
53f5c3f4 | 501 | |
2aee09d8 JG |
502 | if (pmd_protnone(pmd) || fault || write_fault) |
503 | return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk); | |
53f5c3f4 JG |
504 | |
505 | pfn = pmd_pfn(pmd) + pte_index(addr); | |
53f5c3f4 | 506 | for (i = 0; addr < end; addr += PAGE_SIZE, i++, pfn++) |
f88a1e90 | 507 | pfns[i] = hmm_pfn_from_pfn(range, pfn) | cpu_flags; |
53f5c3f4 JG |
508 | hmm_vma_walk->last = end; |
509 | return 0; | |
510 | } | |
511 | ||
f88a1e90 | 512 | static inline uint64_t pte_to_hmm_pfn_flags(struct hmm_range *range, pte_t pte) |
2aee09d8 JG |
513 | { |
514 | if (pte_none(pte) || !pte_present(pte)) | |
515 | return 0; | |
f88a1e90 JG |
516 | return pte_write(pte) ? range->flags[HMM_PFN_VALID] | |
517 | range->flags[HMM_PFN_WRITE] : | |
518 | range->flags[HMM_PFN_VALID]; | |
2aee09d8 JG |
519 | } |
520 | ||
53f5c3f4 JG |
521 | static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr, |
522 | unsigned long end, pmd_t *pmdp, pte_t *ptep, | |
523 | uint64_t *pfn) | |
524 | { | |
525 | struct hmm_vma_walk *hmm_vma_walk = walk->private; | |
f88a1e90 | 526 | struct hmm_range *range = hmm_vma_walk->range; |
53f5c3f4 | 527 | struct vm_area_struct *vma = walk->vma; |
2aee09d8 JG |
528 | bool fault, write_fault; |
529 | uint64_t cpu_flags; | |
53f5c3f4 | 530 | pte_t pte = *ptep; |
f88a1e90 | 531 | uint64_t orig_pfn = *pfn; |
53f5c3f4 | 532 | |
f88a1e90 | 533 | *pfn = range->values[HMM_PFN_NONE]; |
73231612 | 534 | fault = write_fault = false; |
53f5c3f4 JG |
535 | |
536 | if (pte_none(pte)) { | |
73231612 JG |
537 | hmm_pte_need_fault(hmm_vma_walk, orig_pfn, 0, |
538 | &fault, &write_fault); | |
2aee09d8 | 539 | if (fault || write_fault) |
53f5c3f4 JG |
540 | goto fault; |
541 | return 0; | |
542 | } | |
543 | ||
544 | if (!pte_present(pte)) { | |
545 | swp_entry_t entry = pte_to_swp_entry(pte); | |
546 | ||
547 | if (!non_swap_entry(entry)) { | |
2aee09d8 | 548 | if (fault || write_fault) |
53f5c3f4 JG |
549 | goto fault; |
550 | return 0; | |
551 | } | |
552 | ||
553 | /* | |
554 | * This is a special swap entry, ignore migration, use | |
555 | * device and report anything else as error. | |
556 | */ | |
557 | if (is_device_private_entry(entry)) { | |
f88a1e90 JG |
558 | cpu_flags = range->flags[HMM_PFN_VALID] | |
559 | range->flags[HMM_PFN_DEVICE_PRIVATE]; | |
2aee09d8 | 560 | cpu_flags |= is_write_device_private_entry(entry) ? |
f88a1e90 JG |
561 | range->flags[HMM_PFN_WRITE] : 0; |
562 | hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags, | |
563 | &fault, &write_fault); | |
564 | if (fault || write_fault) | |
565 | goto fault; | |
566 | *pfn = hmm_pfn_from_pfn(range, swp_offset(entry)); | |
567 | *pfn |= cpu_flags; | |
53f5c3f4 JG |
568 | return 0; |
569 | } | |
570 | ||
571 | if (is_migration_entry(entry)) { | |
2aee09d8 | 572 | if (fault || write_fault) { |
53f5c3f4 JG |
573 | pte_unmap(ptep); |
574 | hmm_vma_walk->last = addr; | |
575 | migration_entry_wait(vma->vm_mm, | |
2aee09d8 | 576 | pmdp, addr); |
73231612 | 577 | return -EBUSY; |
53f5c3f4 JG |
578 | } |
579 | return 0; | |
580 | } | |
581 | ||
582 | /* Report error for everything else */ | |
f88a1e90 | 583 | *pfn = range->values[HMM_PFN_ERROR]; |
53f5c3f4 | 584 | return -EFAULT; |
73231612 JG |
585 | } else { |
586 | cpu_flags = pte_to_hmm_pfn_flags(range, pte); | |
587 | hmm_pte_need_fault(hmm_vma_walk, orig_pfn, cpu_flags, | |
588 | &fault, &write_fault); | |
53f5c3f4 JG |
589 | } |
590 | ||
2aee09d8 | 591 | if (fault || write_fault) |
53f5c3f4 JG |
592 | goto fault; |
593 | ||
f88a1e90 | 594 | *pfn = hmm_pfn_from_pfn(range, pte_pfn(pte)) | cpu_flags; |
53f5c3f4 JG |
595 | return 0; |
596 | ||
597 | fault: | |
598 | pte_unmap(ptep); | |
599 | /* Fault any virtual address we were asked to fault */ | |
2aee09d8 | 600 | return hmm_vma_walk_hole_(addr, end, fault, write_fault, walk); |
53f5c3f4 JG |
601 | } |
602 | ||
da4c3c73 JG |
603 | static int hmm_vma_walk_pmd(pmd_t *pmdp, |
604 | unsigned long start, | |
605 | unsigned long end, | |
606 | struct mm_walk *walk) | |
607 | { | |
74eee180 JG |
608 | struct hmm_vma_walk *hmm_vma_walk = walk->private; |
609 | struct hmm_range *range = hmm_vma_walk->range; | |
d08faca0 | 610 | struct vm_area_struct *vma = walk->vma; |
ff05c0c6 | 611 | uint64_t *pfns = range->pfns; |
da4c3c73 | 612 | unsigned long addr = start, i; |
da4c3c73 | 613 | pte_t *ptep; |
d08faca0 | 614 | pmd_t pmd; |
da4c3c73 | 615 | |
da4c3c73 JG |
616 | |
617 | again: | |
d08faca0 JG |
618 | pmd = READ_ONCE(*pmdp); |
619 | if (pmd_none(pmd)) | |
da4c3c73 JG |
620 | return hmm_vma_walk_hole(start, end, walk); |
621 | ||
d08faca0 | 622 | if (pmd_huge(pmd) && (range->vma->vm_flags & VM_HUGETLB)) |
da4c3c73 JG |
623 | return hmm_pfns_bad(start, end, walk); |
624 | ||
d08faca0 JG |
625 | if (thp_migration_supported() && is_pmd_migration_entry(pmd)) { |
626 | bool fault, write_fault; | |
627 | unsigned long npages; | |
628 | uint64_t *pfns; | |
629 | ||
630 | i = (addr - range->start) >> PAGE_SHIFT; | |
631 | npages = (end - addr) >> PAGE_SHIFT; | |
632 | pfns = &range->pfns[i]; | |
633 | ||
634 | hmm_range_need_fault(hmm_vma_walk, pfns, npages, | |
635 | 0, &fault, &write_fault); | |
636 | if (fault || write_fault) { | |
637 | hmm_vma_walk->last = addr; | |
638 | pmd_migration_entry_wait(vma->vm_mm, pmdp); | |
73231612 | 639 | return -EBUSY; |
d08faca0 JG |
640 | } |
641 | return 0; | |
642 | } else if (!pmd_present(pmd)) | |
643 | return hmm_pfns_bad(start, end, walk); | |
da4c3c73 | 644 | |
d08faca0 | 645 | if (pmd_devmap(pmd) || pmd_trans_huge(pmd)) { |
da4c3c73 JG |
646 | /* |
647 | * No need to take pmd_lock here, even if some other threads | |
648 | * is splitting the huge pmd we will get that event through | |
649 | * mmu_notifier callback. | |
650 | * | |
651 | * So just read pmd value and check again its a transparent | |
652 | * huge or device mapping one and compute corresponding pfn | |
653 | * values. | |
654 | */ | |
655 | pmd = pmd_read_atomic(pmdp); | |
656 | barrier(); | |
657 | if (!pmd_devmap(pmd) && !pmd_trans_huge(pmd)) | |
658 | goto again; | |
74eee180 | 659 | |
d08faca0 | 660 | i = (addr - range->start) >> PAGE_SHIFT; |
53f5c3f4 | 661 | return hmm_vma_handle_pmd(walk, addr, end, &pfns[i], pmd); |
da4c3c73 JG |
662 | } |
663 | ||
d08faca0 JG |
664 | /* |
665 | * We have handled all the valid case above ie either none, migration, | |
666 | * huge or transparent huge. At this point either it is a valid pmd | |
667 | * entry pointing to pte directory or it is a bad pmd that will not | |
668 | * recover. | |
669 | */ | |
670 | if (pmd_bad(pmd)) | |
da4c3c73 JG |
671 | return hmm_pfns_bad(start, end, walk); |
672 | ||
673 | ptep = pte_offset_map(pmdp, addr); | |
d08faca0 | 674 | i = (addr - range->start) >> PAGE_SHIFT; |
da4c3c73 | 675 | for (; addr < end; addr += PAGE_SIZE, ptep++, i++) { |
53f5c3f4 | 676 | int r; |
74eee180 | 677 | |
53f5c3f4 JG |
678 | r = hmm_vma_handle_pte(walk, addr, end, pmdp, ptep, &pfns[i]); |
679 | if (r) { | |
680 | /* hmm_vma_handle_pte() did unmap pte directory */ | |
681 | hmm_vma_walk->last = addr; | |
682 | return r; | |
74eee180 | 683 | } |
da4c3c73 JG |
684 | } |
685 | pte_unmap(ptep - 1); | |
686 | ||
53f5c3f4 | 687 | hmm_vma_walk->last = addr; |
da4c3c73 JG |
688 | return 0; |
689 | } | |
690 | ||
f88a1e90 JG |
691 | static void hmm_pfns_clear(struct hmm_range *range, |
692 | uint64_t *pfns, | |
33cd47dc JG |
693 | unsigned long addr, |
694 | unsigned long end) | |
695 | { | |
696 | for (; addr < end; addr += PAGE_SIZE, pfns++) | |
f88a1e90 | 697 | *pfns = range->values[HMM_PFN_NONE]; |
33cd47dc JG |
698 | } |
699 | ||
855ce7d2 JG |
700 | static void hmm_pfns_special(struct hmm_range *range) |
701 | { | |
702 | unsigned long addr = range->start, i = 0; | |
703 | ||
704 | for (; addr < range->end; addr += PAGE_SIZE, i++) | |
f88a1e90 | 705 | range->pfns[i] = range->values[HMM_PFN_SPECIAL]; |
855ce7d2 JG |
706 | } |
707 | ||
da4c3c73 | 708 | /* |
25f23a0c JG |
709 | * hmm_range_snapshot() - snapshot CPU page table for a range |
710 | * @range: range | |
711 | * Returns: number of valid pages in range->pfns[] (from range start | |
712 | * address). This may be zero. If the return value is negative, | |
713 | * then one of the following values may be returned: | |
714 | * | |
715 | * -EINVAL invalid arguments or mm or virtual address are in an | |
716 | * invalid vma (ie either hugetlbfs or device file vma). | |
717 | * -EPERM For example, asking for write, when the range is | |
718 | * read-only | |
719 | * -EAGAIN Caller needs to retry | |
720 | * -EFAULT Either no valid vma exists for this range, or it is | |
721 | * illegal to access the range | |
da4c3c73 JG |
722 | * |
723 | * This snapshots the CPU page table for a range of virtual addresses. Snapshot | |
724 | * validity is tracked by range struct. See hmm_vma_range_done() for further | |
725 | * information. | |
da4c3c73 | 726 | */ |
25f23a0c | 727 | long hmm_range_snapshot(struct hmm_range *range) |
da4c3c73 | 728 | { |
08232a45 | 729 | struct vm_area_struct *vma = range->vma; |
74eee180 | 730 | struct hmm_vma_walk hmm_vma_walk; |
da4c3c73 JG |
731 | struct mm_walk mm_walk; |
732 | struct hmm *hmm; | |
733 | ||
704f3f2c JG |
734 | range->hmm = NULL; |
735 | ||
da4c3c73 | 736 | /* Sanity check, this really should not happen ! */ |
08232a45 | 737 | if (range->start < vma->vm_start || range->start >= vma->vm_end) |
da4c3c73 | 738 | return -EINVAL; |
08232a45 | 739 | if (range->end < vma->vm_start || range->end > vma->vm_end) |
da4c3c73 JG |
740 | return -EINVAL; |
741 | ||
704f3f2c | 742 | hmm = hmm_get_or_create(vma->vm_mm); |
da4c3c73 JG |
743 | if (!hmm) |
744 | return -ENOMEM; | |
704f3f2c JG |
745 | |
746 | /* Check if hmm_mm_destroy() was call. */ | |
747 | if (hmm->mm == NULL) { | |
748 | hmm_put(hmm); | |
da4c3c73 | 749 | return -EINVAL; |
704f3f2c | 750 | } |
da4c3c73 | 751 | |
855ce7d2 | 752 | /* FIXME support hugetlb fs */ |
e1fb4a08 DJ |
753 | if (is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_SPECIAL) || |
754 | vma_is_dax(vma)) { | |
855ce7d2 | 755 | hmm_pfns_special(range); |
704f3f2c | 756 | hmm_put(hmm); |
855ce7d2 JG |
757 | return -EINVAL; |
758 | } | |
759 | ||
86586a41 JG |
760 | if (!(vma->vm_flags & VM_READ)) { |
761 | /* | |
762 | * If vma do not allow read access, then assume that it does | |
763 | * not allow write access, either. Architecture that allow | |
764 | * write without read access are not supported by HMM, because | |
765 | * operations such has atomic access would not work. | |
766 | */ | |
f88a1e90 | 767 | hmm_pfns_clear(range, range->pfns, range->start, range->end); |
704f3f2c | 768 | hmm_put(hmm); |
86586a41 JG |
769 | return -EPERM; |
770 | } | |
771 | ||
da4c3c73 | 772 | /* Initialize range to track CPU page table update */ |
da4c3c73 JG |
773 | spin_lock(&hmm->lock); |
774 | range->valid = true; | |
775 | list_add_rcu(&range->list, &hmm->ranges); | |
776 | spin_unlock(&hmm->lock); | |
777 | ||
74eee180 JG |
778 | hmm_vma_walk.fault = false; |
779 | hmm_vma_walk.range = range; | |
780 | mm_walk.private = &hmm_vma_walk; | |
25f23a0c | 781 | hmm_vma_walk.last = range->start; |
74eee180 | 782 | |
da4c3c73 JG |
783 | mm_walk.vma = vma; |
784 | mm_walk.mm = vma->vm_mm; | |
da4c3c73 JG |
785 | mm_walk.pte_entry = NULL; |
786 | mm_walk.test_walk = NULL; | |
787 | mm_walk.hugetlb_entry = NULL; | |
788 | mm_walk.pmd_entry = hmm_vma_walk_pmd; | |
789 | mm_walk.pte_hole = hmm_vma_walk_hole; | |
790 | ||
08232a45 | 791 | walk_page_range(range->start, range->end, &mm_walk); |
704f3f2c JG |
792 | /* |
793 | * Transfer hmm reference to the range struct it will be drop inside | |
794 | * the hmm_vma_range_done() function (which _must_ be call if this | |
795 | * function return 0). | |
796 | */ | |
797 | range->hmm = hmm; | |
25f23a0c | 798 | return (hmm_vma_walk.last - range->start) >> PAGE_SHIFT; |
da4c3c73 | 799 | } |
25f23a0c | 800 | EXPORT_SYMBOL(hmm_range_snapshot); |
da4c3c73 JG |
801 | |
802 | /* | |
803 | * hmm_vma_range_done() - stop tracking change to CPU page table over a range | |
da4c3c73 JG |
804 | * @range: range being tracked |
805 | * Returns: false if range data has been invalidated, true otherwise | |
806 | * | |
807 | * Range struct is used to track updates to the CPU page table after a call to | |
808 | * either hmm_vma_get_pfns() or hmm_vma_fault(). Once the device driver is done | |
809 | * using the data, or wants to lock updates to the data it got from those | |
810 | * functions, it must call the hmm_vma_range_done() function, which will then | |
811 | * stop tracking CPU page table updates. | |
812 | * | |
813 | * Note that device driver must still implement general CPU page table update | |
814 | * tracking either by using hmm_mirror (see hmm_mirror_register()) or by using | |
815 | * the mmu_notifier API directly. | |
816 | * | |
817 | * CPU page table update tracking done through hmm_range is only temporary and | |
818 | * to be used while trying to duplicate CPU page table contents for a range of | |
819 | * virtual addresses. | |
820 | * | |
821 | * There are two ways to use this : | |
822 | * again: | |
08232a45 | 823 | * hmm_vma_get_pfns(range); or hmm_vma_fault(...); |
da4c3c73 JG |
824 | * trans = device_build_page_table_update_transaction(pfns); |
825 | * device_page_table_lock(); | |
08232a45 | 826 | * if (!hmm_vma_range_done(range)) { |
da4c3c73 JG |
827 | * device_page_table_unlock(); |
828 | * goto again; | |
829 | * } | |
830 | * device_commit_transaction(trans); | |
831 | * device_page_table_unlock(); | |
832 | * | |
833 | * Or: | |
08232a45 | 834 | * hmm_vma_get_pfns(range); or hmm_vma_fault(...); |
da4c3c73 | 835 | * device_page_table_lock(); |
08232a45 JG |
836 | * hmm_vma_range_done(range); |
837 | * device_update_page_table(range->pfns); | |
da4c3c73 JG |
838 | * device_page_table_unlock(); |
839 | */ | |
08232a45 | 840 | bool hmm_vma_range_done(struct hmm_range *range) |
da4c3c73 | 841 | { |
704f3f2c | 842 | bool ret = false; |
da4c3c73 | 843 | |
704f3f2c JG |
844 | /* Sanity check this really should not happen. */ |
845 | if (range->hmm == NULL || range->end <= range->start) { | |
da4c3c73 JG |
846 | BUG(); |
847 | return false; | |
848 | } | |
849 | ||
704f3f2c | 850 | spin_lock(&range->hmm->lock); |
da4c3c73 | 851 | list_del_rcu(&range->list); |
704f3f2c JG |
852 | ret = range->valid; |
853 | spin_unlock(&range->hmm->lock); | |
da4c3c73 | 854 | |
704f3f2c JG |
855 | /* Is the mm still alive ? */ |
856 | if (range->hmm->mm == NULL) | |
857 | ret = false; | |
858 | ||
859 | /* Drop reference taken by hmm_vma_fault() or hmm_vma_get_pfns() */ | |
860 | hmm_put(range->hmm); | |
861 | range->hmm = NULL; | |
862 | return ret; | |
da4c3c73 JG |
863 | } |
864 | EXPORT_SYMBOL(hmm_vma_range_done); | |
74eee180 JG |
865 | |
866 | /* | |
73231612 | 867 | * hmm_range_fault() - try to fault some address in a virtual address range |
08232a45 | 868 | * @range: range being faulted |
74eee180 | 869 | * @block: allow blocking on fault (if true it sleeps and do not drop mmap_sem) |
73231612 JG |
870 | * Returns: number of valid pages in range->pfns[] (from range start |
871 | * address). This may be zero. If the return value is negative, | |
872 | * then one of the following values may be returned: | |
873 | * | |
874 | * -EINVAL invalid arguments or mm or virtual address are in an | |
875 | * invalid vma (ie either hugetlbfs or device file vma). | |
876 | * -ENOMEM: Out of memory. | |
877 | * -EPERM: Invalid permission (for instance asking for write and | |
878 | * range is read only). | |
879 | * -EAGAIN: If you need to retry and mmap_sem was drop. This can only | |
880 | * happens if block argument is false. | |
881 | * -EBUSY: If the the range is being invalidated and you should wait | |
882 | * for invalidation to finish. | |
883 | * -EFAULT: Invalid (ie either no valid vma or it is illegal to access | |
884 | * that range), number of valid pages in range->pfns[] (from | |
885 | * range start address). | |
74eee180 JG |
886 | * |
887 | * This is similar to a regular CPU page fault except that it will not trigger | |
73231612 JG |
888 | * any memory migration if the memory being faulted is not accessible by CPUs |
889 | * and caller does not ask for migration. | |
74eee180 | 890 | * |
ff05c0c6 JG |
891 | * On error, for one virtual address in the range, the function will mark the |
892 | * corresponding HMM pfn entry with an error flag. | |
74eee180 | 893 | */ |
73231612 | 894 | long hmm_range_fault(struct hmm_range *range, bool block) |
74eee180 | 895 | { |
08232a45 JG |
896 | struct vm_area_struct *vma = range->vma; |
897 | unsigned long start = range->start; | |
74eee180 JG |
898 | struct hmm_vma_walk hmm_vma_walk; |
899 | struct mm_walk mm_walk; | |
900 | struct hmm *hmm; | |
901 | int ret; | |
902 | ||
704f3f2c JG |
903 | range->hmm = NULL; |
904 | ||
74eee180 | 905 | /* Sanity check, this really should not happen ! */ |
08232a45 | 906 | if (range->start < vma->vm_start || range->start >= vma->vm_end) |
74eee180 | 907 | return -EINVAL; |
08232a45 | 908 | if (range->end < vma->vm_start || range->end > vma->vm_end) |
74eee180 JG |
909 | return -EINVAL; |
910 | ||
704f3f2c | 911 | hmm = hmm_get_or_create(vma->vm_mm); |
74eee180 | 912 | if (!hmm) { |
f88a1e90 | 913 | hmm_pfns_clear(range, range->pfns, range->start, range->end); |
74eee180 JG |
914 | return -ENOMEM; |
915 | } | |
704f3f2c JG |
916 | |
917 | /* Check if hmm_mm_destroy() was call. */ | |
918 | if (hmm->mm == NULL) { | |
919 | hmm_put(hmm); | |
74eee180 | 920 | return -EINVAL; |
704f3f2c | 921 | } |
74eee180 | 922 | |
855ce7d2 | 923 | /* FIXME support hugetlb fs */ |
e1fb4a08 DJ |
924 | if (is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_SPECIAL) || |
925 | vma_is_dax(vma)) { | |
855ce7d2 | 926 | hmm_pfns_special(range); |
704f3f2c | 927 | hmm_put(hmm); |
855ce7d2 JG |
928 | return -EINVAL; |
929 | } | |
930 | ||
86586a41 JG |
931 | if (!(vma->vm_flags & VM_READ)) { |
932 | /* | |
933 | * If vma do not allow read access, then assume that it does | |
934 | * not allow write access, either. Architecture that allow | |
935 | * write without read access are not supported by HMM, because | |
936 | * operations such has atomic access would not work. | |
937 | */ | |
f88a1e90 | 938 | hmm_pfns_clear(range, range->pfns, range->start, range->end); |
704f3f2c | 939 | hmm_put(hmm); |
86586a41 JG |
940 | return -EPERM; |
941 | } | |
74eee180 | 942 | |
86586a41 JG |
943 | /* Initialize range to track CPU page table update */ |
944 | spin_lock(&hmm->lock); | |
945 | range->valid = true; | |
946 | list_add_rcu(&range->list, &hmm->ranges); | |
947 | spin_unlock(&hmm->lock); | |
948 | ||
74eee180 | 949 | hmm_vma_walk.fault = true; |
74eee180 JG |
950 | hmm_vma_walk.block = block; |
951 | hmm_vma_walk.range = range; | |
952 | mm_walk.private = &hmm_vma_walk; | |
953 | hmm_vma_walk.last = range->start; | |
954 | ||
955 | mm_walk.vma = vma; | |
956 | mm_walk.mm = vma->vm_mm; | |
957 | mm_walk.pte_entry = NULL; | |
958 | mm_walk.test_walk = NULL; | |
959 | mm_walk.hugetlb_entry = NULL; | |
960 | mm_walk.pmd_entry = hmm_vma_walk_pmd; | |
961 | mm_walk.pte_hole = hmm_vma_walk_hole; | |
962 | ||
963 | do { | |
08232a45 | 964 | ret = walk_page_range(start, range->end, &mm_walk); |
74eee180 | 965 | start = hmm_vma_walk.last; |
73231612 JG |
966 | /* Keep trying while the range is valid. */ |
967 | } while (ret == -EBUSY && range->valid); | |
74eee180 JG |
968 | |
969 | if (ret) { | |
970 | unsigned long i; | |
971 | ||
972 | i = (hmm_vma_walk.last - range->start) >> PAGE_SHIFT; | |
f88a1e90 JG |
973 | hmm_pfns_clear(range, &range->pfns[i], hmm_vma_walk.last, |
974 | range->end); | |
08232a45 | 975 | hmm_vma_range_done(range); |
704f3f2c | 976 | hmm_put(hmm); |
73231612 | 977 | return ret; |
704f3f2c JG |
978 | } else { |
979 | /* | |
980 | * Transfer hmm reference to the range struct it will be drop | |
981 | * inside the hmm_vma_range_done() function (which _must_ be | |
982 | * call if this function return 0). | |
983 | */ | |
984 | range->hmm = hmm; | |
74eee180 | 985 | } |
704f3f2c | 986 | |
73231612 | 987 | return (hmm_vma_walk.last - range->start) >> PAGE_SHIFT; |
74eee180 | 988 | } |
73231612 | 989 | EXPORT_SYMBOL(hmm_range_fault); |
c0b12405 | 990 | #endif /* IS_ENABLED(CONFIG_HMM_MIRROR) */ |
4ef589dc JG |
991 | |
992 | ||
df6ad698 | 993 | #if IS_ENABLED(CONFIG_DEVICE_PRIVATE) || IS_ENABLED(CONFIG_DEVICE_PUBLIC) |
4ef589dc JG |
994 | struct page *hmm_vma_alloc_locked_page(struct vm_area_struct *vma, |
995 | unsigned long addr) | |
996 | { | |
997 | struct page *page; | |
998 | ||
999 | page = alloc_page_vma(GFP_HIGHUSER, vma, addr); | |
1000 | if (!page) | |
1001 | return NULL; | |
1002 | lock_page(page); | |
1003 | return page; | |
1004 | } | |
1005 | EXPORT_SYMBOL(hmm_vma_alloc_locked_page); | |
1006 | ||
1007 | ||
1008 | static void hmm_devmem_ref_release(struct percpu_ref *ref) | |
1009 | { | |
1010 | struct hmm_devmem *devmem; | |
1011 | ||
1012 | devmem = container_of(ref, struct hmm_devmem, ref); | |
1013 | complete(&devmem->completion); | |
1014 | } | |
1015 | ||
1016 | static void hmm_devmem_ref_exit(void *data) | |
1017 | { | |
1018 | struct percpu_ref *ref = data; | |
1019 | struct hmm_devmem *devmem; | |
1020 | ||
1021 | devmem = container_of(ref, struct hmm_devmem, ref); | |
bbecd94e | 1022 | wait_for_completion(&devmem->completion); |
4ef589dc | 1023 | percpu_ref_exit(ref); |
4ef589dc JG |
1024 | } |
1025 | ||
bbecd94e | 1026 | static void hmm_devmem_ref_kill(struct percpu_ref *ref) |
4ef589dc | 1027 | { |
4ef589dc | 1028 | percpu_ref_kill(ref); |
4ef589dc JG |
1029 | } |
1030 | ||
b57e622e | 1031 | static vm_fault_t hmm_devmem_fault(struct vm_area_struct *vma, |
4ef589dc JG |
1032 | unsigned long addr, |
1033 | const struct page *page, | |
1034 | unsigned int flags, | |
1035 | pmd_t *pmdp) | |
1036 | { | |
1037 | struct hmm_devmem *devmem = page->pgmap->data; | |
1038 | ||
1039 | return devmem->ops->fault(devmem, vma, addr, page, flags, pmdp); | |
1040 | } | |
1041 | ||
1042 | static void hmm_devmem_free(struct page *page, void *data) | |
1043 | { | |
1044 | struct hmm_devmem *devmem = data; | |
1045 | ||
2fa147bd DW |
1046 | page->mapping = NULL; |
1047 | ||
4ef589dc JG |
1048 | devmem->ops->free(devmem, page); |
1049 | } | |
1050 | ||
4ef589dc JG |
1051 | /* |
1052 | * hmm_devmem_add() - hotplug ZONE_DEVICE memory for device memory | |
1053 | * | |
1054 | * @ops: memory event device driver callback (see struct hmm_devmem_ops) | |
1055 | * @device: device struct to bind the resource too | |
1056 | * @size: size in bytes of the device memory to add | |
1057 | * Returns: pointer to new hmm_devmem struct ERR_PTR otherwise | |
1058 | * | |
1059 | * This function first finds an empty range of physical address big enough to | |
1060 | * contain the new resource, and then hotplugs it as ZONE_DEVICE memory, which | |
1061 | * in turn allocates struct pages. It does not do anything beyond that; all | |
1062 | * events affecting the memory will go through the various callbacks provided | |
1063 | * by hmm_devmem_ops struct. | |
1064 | * | |
1065 | * Device driver should call this function during device initialization and | |
1066 | * is then responsible of memory management. HMM only provides helpers. | |
1067 | */ | |
1068 | struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops, | |
1069 | struct device *device, | |
1070 | unsigned long size) | |
1071 | { | |
1072 | struct hmm_devmem *devmem; | |
1073 | resource_size_t addr; | |
bbecd94e | 1074 | void *result; |
4ef589dc JG |
1075 | int ret; |
1076 | ||
e7638488 | 1077 | dev_pagemap_get_ops(); |
4ef589dc | 1078 | |
58ef15b7 | 1079 | devmem = devm_kzalloc(device, sizeof(*devmem), GFP_KERNEL); |
4ef589dc JG |
1080 | if (!devmem) |
1081 | return ERR_PTR(-ENOMEM); | |
1082 | ||
1083 | init_completion(&devmem->completion); | |
1084 | devmem->pfn_first = -1UL; | |
1085 | devmem->pfn_last = -1UL; | |
1086 | devmem->resource = NULL; | |
1087 | devmem->device = device; | |
1088 | devmem->ops = ops; | |
1089 | ||
1090 | ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release, | |
1091 | 0, GFP_KERNEL); | |
1092 | if (ret) | |
58ef15b7 | 1093 | return ERR_PTR(ret); |
4ef589dc | 1094 | |
58ef15b7 | 1095 | ret = devm_add_action_or_reset(device, hmm_devmem_ref_exit, &devmem->ref); |
4ef589dc | 1096 | if (ret) |
58ef15b7 | 1097 | return ERR_PTR(ret); |
4ef589dc JG |
1098 | |
1099 | size = ALIGN(size, PA_SECTION_SIZE); | |
1100 | addr = min((unsigned long)iomem_resource.end, | |
1101 | (1UL << MAX_PHYSMEM_BITS) - 1); | |
1102 | addr = addr - size + 1UL; | |
1103 | ||
1104 | /* | |
1105 | * FIXME add a new helper to quickly walk resource tree and find free | |
1106 | * range | |
1107 | * | |
1108 | * FIXME what about ioport_resource resource ? | |
1109 | */ | |
1110 | for (; addr > size && addr >= iomem_resource.start; addr -= size) { | |
1111 | ret = region_intersects(addr, size, 0, IORES_DESC_NONE); | |
1112 | if (ret != REGION_DISJOINT) | |
1113 | continue; | |
1114 | ||
1115 | devmem->resource = devm_request_mem_region(device, addr, size, | |
1116 | dev_name(device)); | |
58ef15b7 DW |
1117 | if (!devmem->resource) |
1118 | return ERR_PTR(-ENOMEM); | |
4ef589dc JG |
1119 | break; |
1120 | } | |
58ef15b7 DW |
1121 | if (!devmem->resource) |
1122 | return ERR_PTR(-ERANGE); | |
4ef589dc JG |
1123 | |
1124 | devmem->resource->desc = IORES_DESC_DEVICE_PRIVATE_MEMORY; | |
1125 | devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT; | |
1126 | devmem->pfn_last = devmem->pfn_first + | |
1127 | (resource_size(devmem->resource) >> PAGE_SHIFT); | |
063a7d1d | 1128 | devmem->page_fault = hmm_devmem_fault; |
4ef589dc | 1129 | |
bbecd94e DW |
1130 | devmem->pagemap.type = MEMORY_DEVICE_PRIVATE; |
1131 | devmem->pagemap.res = *devmem->resource; | |
bbecd94e DW |
1132 | devmem->pagemap.page_free = hmm_devmem_free; |
1133 | devmem->pagemap.altmap_valid = false; | |
1134 | devmem->pagemap.ref = &devmem->ref; | |
1135 | devmem->pagemap.data = devmem; | |
1136 | devmem->pagemap.kill = hmm_devmem_ref_kill; | |
4ef589dc | 1137 | |
bbecd94e DW |
1138 | result = devm_memremap_pages(devmem->device, &devmem->pagemap); |
1139 | if (IS_ERR(result)) | |
1140 | return result; | |
4ef589dc | 1141 | return devmem; |
4ef589dc | 1142 | } |
02917e9f | 1143 | EXPORT_SYMBOL_GPL(hmm_devmem_add); |
4ef589dc | 1144 | |
d3df0a42 JG |
1145 | struct hmm_devmem *hmm_devmem_add_resource(const struct hmm_devmem_ops *ops, |
1146 | struct device *device, | |
1147 | struct resource *res) | |
1148 | { | |
1149 | struct hmm_devmem *devmem; | |
bbecd94e | 1150 | void *result; |
d3df0a42 JG |
1151 | int ret; |
1152 | ||
1153 | if (res->desc != IORES_DESC_DEVICE_PUBLIC_MEMORY) | |
1154 | return ERR_PTR(-EINVAL); | |
1155 | ||
e7638488 | 1156 | dev_pagemap_get_ops(); |
d3df0a42 | 1157 | |
58ef15b7 | 1158 | devmem = devm_kzalloc(device, sizeof(*devmem), GFP_KERNEL); |
d3df0a42 JG |
1159 | if (!devmem) |
1160 | return ERR_PTR(-ENOMEM); | |
1161 | ||
1162 | init_completion(&devmem->completion); | |
1163 | devmem->pfn_first = -1UL; | |
1164 | devmem->pfn_last = -1UL; | |
1165 | devmem->resource = res; | |
1166 | devmem->device = device; | |
1167 | devmem->ops = ops; | |
1168 | ||
1169 | ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release, | |
1170 | 0, GFP_KERNEL); | |
1171 | if (ret) | |
58ef15b7 | 1172 | return ERR_PTR(ret); |
d3df0a42 | 1173 | |
58ef15b7 DW |
1174 | ret = devm_add_action_or_reset(device, hmm_devmem_ref_exit, |
1175 | &devmem->ref); | |
d3df0a42 | 1176 | if (ret) |
58ef15b7 | 1177 | return ERR_PTR(ret); |
d3df0a42 JG |
1178 | |
1179 | devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT; | |
1180 | devmem->pfn_last = devmem->pfn_first + | |
1181 | (resource_size(devmem->resource) >> PAGE_SHIFT); | |
063a7d1d | 1182 | devmem->page_fault = hmm_devmem_fault; |
d3df0a42 | 1183 | |
bbecd94e DW |
1184 | devmem->pagemap.type = MEMORY_DEVICE_PUBLIC; |
1185 | devmem->pagemap.res = *devmem->resource; | |
bbecd94e DW |
1186 | devmem->pagemap.page_free = hmm_devmem_free; |
1187 | devmem->pagemap.altmap_valid = false; | |
1188 | devmem->pagemap.ref = &devmem->ref; | |
1189 | devmem->pagemap.data = devmem; | |
1190 | devmem->pagemap.kill = hmm_devmem_ref_kill; | |
d3df0a42 | 1191 | |
bbecd94e DW |
1192 | result = devm_memremap_pages(devmem->device, &devmem->pagemap); |
1193 | if (IS_ERR(result)) | |
1194 | return result; | |
d3df0a42 | 1195 | return devmem; |
d3df0a42 | 1196 | } |
02917e9f | 1197 | EXPORT_SYMBOL_GPL(hmm_devmem_add_resource); |
d3df0a42 | 1198 | |
858b54da JG |
1199 | /* |
1200 | * A device driver that wants to handle multiple devices memory through a | |
1201 | * single fake device can use hmm_device to do so. This is purely a helper | |
1202 | * and it is not needed to make use of any HMM functionality. | |
1203 | */ | |
1204 | #define HMM_DEVICE_MAX 256 | |
1205 | ||
1206 | static DECLARE_BITMAP(hmm_device_mask, HMM_DEVICE_MAX); | |
1207 | static DEFINE_SPINLOCK(hmm_device_lock); | |
1208 | static struct class *hmm_device_class; | |
1209 | static dev_t hmm_device_devt; | |
1210 | ||
1211 | static void hmm_device_release(struct device *device) | |
1212 | { | |
1213 | struct hmm_device *hmm_device; | |
1214 | ||
1215 | hmm_device = container_of(device, struct hmm_device, device); | |
1216 | spin_lock(&hmm_device_lock); | |
1217 | clear_bit(hmm_device->minor, hmm_device_mask); | |
1218 | spin_unlock(&hmm_device_lock); | |
1219 | ||
1220 | kfree(hmm_device); | |
1221 | } | |
1222 | ||
1223 | struct hmm_device *hmm_device_new(void *drvdata) | |
1224 | { | |
1225 | struct hmm_device *hmm_device; | |
1226 | ||
1227 | hmm_device = kzalloc(sizeof(*hmm_device), GFP_KERNEL); | |
1228 | if (!hmm_device) | |
1229 | return ERR_PTR(-ENOMEM); | |
1230 | ||
1231 | spin_lock(&hmm_device_lock); | |
1232 | hmm_device->minor = find_first_zero_bit(hmm_device_mask, HMM_DEVICE_MAX); | |
1233 | if (hmm_device->minor >= HMM_DEVICE_MAX) { | |
1234 | spin_unlock(&hmm_device_lock); | |
1235 | kfree(hmm_device); | |
1236 | return ERR_PTR(-EBUSY); | |
1237 | } | |
1238 | set_bit(hmm_device->minor, hmm_device_mask); | |
1239 | spin_unlock(&hmm_device_lock); | |
1240 | ||
1241 | dev_set_name(&hmm_device->device, "hmm_device%d", hmm_device->minor); | |
1242 | hmm_device->device.devt = MKDEV(MAJOR(hmm_device_devt), | |
1243 | hmm_device->minor); | |
1244 | hmm_device->device.release = hmm_device_release; | |
1245 | dev_set_drvdata(&hmm_device->device, drvdata); | |
1246 | hmm_device->device.class = hmm_device_class; | |
1247 | device_initialize(&hmm_device->device); | |
1248 | ||
1249 | return hmm_device; | |
1250 | } | |
1251 | EXPORT_SYMBOL(hmm_device_new); | |
1252 | ||
1253 | void hmm_device_put(struct hmm_device *hmm_device) | |
1254 | { | |
1255 | put_device(&hmm_device->device); | |
1256 | } | |
1257 | EXPORT_SYMBOL(hmm_device_put); | |
1258 | ||
1259 | static int __init hmm_init(void) | |
1260 | { | |
1261 | int ret; | |
1262 | ||
1263 | ret = alloc_chrdev_region(&hmm_device_devt, 0, | |
1264 | HMM_DEVICE_MAX, | |
1265 | "hmm_device"); | |
1266 | if (ret) | |
1267 | return ret; | |
1268 | ||
1269 | hmm_device_class = class_create(THIS_MODULE, "hmm_device"); | |
1270 | if (IS_ERR(hmm_device_class)) { | |
1271 | unregister_chrdev_region(hmm_device_devt, HMM_DEVICE_MAX); | |
1272 | return PTR_ERR(hmm_device_class); | |
1273 | } | |
1274 | return 0; | |
1275 | } | |
1276 | ||
1277 | device_initcall(hmm_init); | |
df6ad698 | 1278 | #endif /* CONFIG_DEVICE_PRIVATE || CONFIG_DEVICE_PUBLIC */ |