]>
Commit | Line | Data |
---|---|---|
76cbbead CH |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * Device Memory Migration functionality. | |
4 | * | |
5 | * Originally written by Jérôme Glisse. | |
6 | */ | |
7 | #include <linux/export.h> | |
8 | #include <linux/memremap.h> | |
9 | #include <linux/migrate.h> | |
10 | #include <linux/mm_inline.h> | |
11 | #include <linux/mmu_notifier.h> | |
12 | #include <linux/oom.h> | |
13 | #include <linux/pagewalk.h> | |
14 | #include <linux/rmap.h> | |
15 | #include <linux/swapops.h> | |
16 | #include <asm/tlbflush.h> | |
17 | #include "internal.h" | |
18 | ||
19 | static int migrate_vma_collect_skip(unsigned long start, | |
20 | unsigned long end, | |
21 | struct mm_walk *walk) | |
22 | { | |
23 | struct migrate_vma *migrate = walk->private; | |
24 | unsigned long addr; | |
25 | ||
26 | for (addr = start; addr < end; addr += PAGE_SIZE) { | |
27 | migrate->dst[migrate->npages] = 0; | |
28 | migrate->src[migrate->npages++] = 0; | |
29 | } | |
30 | ||
31 | return 0; | |
32 | } | |
33 | ||
34 | static int migrate_vma_collect_hole(unsigned long start, | |
35 | unsigned long end, | |
36 | __always_unused int depth, | |
37 | struct mm_walk *walk) | |
38 | { | |
39 | struct migrate_vma *migrate = walk->private; | |
40 | unsigned long addr; | |
41 | ||
42 | /* Only allow populating anonymous memory. */ | |
43 | if (!vma_is_anonymous(walk->vma)) | |
44 | return migrate_vma_collect_skip(start, end, walk); | |
45 | ||
46 | for (addr = start; addr < end; addr += PAGE_SIZE) { | |
47 | migrate->src[migrate->npages] = MIGRATE_PFN_MIGRATE; | |
48 | migrate->dst[migrate->npages] = 0; | |
49 | migrate->npages++; | |
50 | migrate->cpages++; | |
51 | } | |
52 | ||
53 | return 0; | |
54 | } | |
55 | ||
56 | static int migrate_vma_collect_pmd(pmd_t *pmdp, | |
57 | unsigned long start, | |
58 | unsigned long end, | |
59 | struct mm_walk *walk) | |
60 | { | |
61 | struct migrate_vma *migrate = walk->private; | |
62 | struct vm_area_struct *vma = walk->vma; | |
63 | struct mm_struct *mm = vma->vm_mm; | |
64 | unsigned long addr = start, unmapped = 0; | |
65 | spinlock_t *ptl; | |
66 | pte_t *ptep; | |
67 | ||
68 | again: | |
69 | if (pmd_none(*pmdp)) | |
70 | return migrate_vma_collect_hole(start, end, -1, walk); | |
71 | ||
72 | if (pmd_trans_huge(*pmdp)) { | |
73 | struct page *page; | |
74 | ||
75 | ptl = pmd_lock(mm, pmdp); | |
76 | if (unlikely(!pmd_trans_huge(*pmdp))) { | |
77 | spin_unlock(ptl); | |
78 | goto again; | |
79 | } | |
80 | ||
81 | page = pmd_page(*pmdp); | |
82 | if (is_huge_zero_page(page)) { | |
83 | spin_unlock(ptl); | |
84 | split_huge_pmd(vma, pmdp, addr); | |
85 | if (pmd_trans_unstable(pmdp)) | |
86 | return migrate_vma_collect_skip(start, end, | |
87 | walk); | |
88 | } else { | |
89 | int ret; | |
90 | ||
91 | get_page(page); | |
92 | spin_unlock(ptl); | |
93 | if (unlikely(!trylock_page(page))) | |
94 | return migrate_vma_collect_skip(start, end, | |
95 | walk); | |
96 | ret = split_huge_page(page); | |
97 | unlock_page(page); | |
98 | put_page(page); | |
99 | if (ret) | |
100 | return migrate_vma_collect_skip(start, end, | |
101 | walk); | |
102 | if (pmd_none(*pmdp)) | |
103 | return migrate_vma_collect_hole(start, end, -1, | |
104 | walk); | |
105 | } | |
106 | } | |
107 | ||
108 | if (unlikely(pmd_bad(*pmdp))) | |
109 | return migrate_vma_collect_skip(start, end, walk); | |
110 | ||
111 | ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl); | |
112 | arch_enter_lazy_mmu_mode(); | |
113 | ||
114 | for (; addr < end; addr += PAGE_SIZE, ptep++) { | |
115 | unsigned long mpfn = 0, pfn; | |
116 | struct page *page; | |
117 | swp_entry_t entry; | |
118 | pte_t pte; | |
119 | ||
120 | pte = *ptep; | |
121 | ||
122 | if (pte_none(pte)) { | |
123 | if (vma_is_anonymous(vma)) { | |
124 | mpfn = MIGRATE_PFN_MIGRATE; | |
125 | migrate->cpages++; | |
126 | } | |
127 | goto next; | |
128 | } | |
129 | ||
130 | if (!pte_present(pte)) { | |
131 | /* | |
132 | * Only care about unaddressable device page special | |
133 | * page table entry. Other special swap entries are not | |
134 | * migratable, and we ignore regular swapped page. | |
135 | */ | |
136 | entry = pte_to_swp_entry(pte); | |
137 | if (!is_device_private_entry(entry)) | |
138 | goto next; | |
139 | ||
140 | page = pfn_swap_entry_to_page(entry); | |
141 | if (!(migrate->flags & | |
142 | MIGRATE_VMA_SELECT_DEVICE_PRIVATE) || | |
143 | page->pgmap->owner != migrate->pgmap_owner) | |
144 | goto next; | |
145 | ||
146 | mpfn = migrate_pfn(page_to_pfn(page)) | | |
147 | MIGRATE_PFN_MIGRATE; | |
148 | if (is_writable_device_private_entry(entry)) | |
149 | mpfn |= MIGRATE_PFN_WRITE; | |
150 | } else { | |
151 | if (!(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM)) | |
152 | goto next; | |
153 | pfn = pte_pfn(pte); | |
154 | if (is_zero_pfn(pfn)) { | |
155 | mpfn = MIGRATE_PFN_MIGRATE; | |
156 | migrate->cpages++; | |
157 | goto next; | |
158 | } | |
159 | page = vm_normal_page(migrate->vma, addr, pte); | |
160 | mpfn = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE; | |
161 | mpfn |= pte_write(pte) ? MIGRATE_PFN_WRITE : 0; | |
162 | } | |
163 | ||
164 | /* FIXME support THP */ | |
165 | if (!page || !page->mapping || PageTransCompound(page)) { | |
166 | mpfn = 0; | |
167 | goto next; | |
168 | } | |
169 | ||
170 | /* | |
171 | * By getting a reference on the page we pin it and that blocks | |
172 | * any kind of migration. Side effect is that it "freezes" the | |
173 | * pte. | |
174 | * | |
175 | * We drop this reference after isolating the page from the lru | |
176 | * for non device page (device page are not on the lru and thus | |
177 | * can't be dropped from it). | |
178 | */ | |
179 | get_page(page); | |
180 | ||
181 | /* | |
182 | * Optimize for the common case where page is only mapped once | |
183 | * in one process. If we can lock the page, then we can safely | |
184 | * set up a special migration page table entry now. | |
185 | */ | |
186 | if (trylock_page(page)) { | |
187 | pte_t swp_pte; | |
188 | ||
189 | migrate->cpages++; | |
190 | ptep_get_and_clear(mm, addr, ptep); | |
191 | ||
192 | /* Setup special migration page table entry */ | |
193 | if (mpfn & MIGRATE_PFN_WRITE) | |
194 | entry = make_writable_migration_entry( | |
195 | page_to_pfn(page)); | |
196 | else | |
197 | entry = make_readable_migration_entry( | |
198 | page_to_pfn(page)); | |
199 | swp_pte = swp_entry_to_pte(entry); | |
200 | if (pte_present(pte)) { | |
201 | if (pte_soft_dirty(pte)) | |
202 | swp_pte = pte_swp_mksoft_dirty(swp_pte); | |
203 | if (pte_uffd_wp(pte)) | |
204 | swp_pte = pte_swp_mkuffd_wp(swp_pte); | |
205 | } else { | |
206 | if (pte_swp_soft_dirty(pte)) | |
207 | swp_pte = pte_swp_mksoft_dirty(swp_pte); | |
208 | if (pte_swp_uffd_wp(pte)) | |
209 | swp_pte = pte_swp_mkuffd_wp(swp_pte); | |
210 | } | |
211 | set_pte_at(mm, addr, ptep, swp_pte); | |
212 | ||
213 | /* | |
214 | * This is like regular unmap: we remove the rmap and | |
215 | * drop page refcount. Page won't be freed, as we took | |
216 | * a reference just above. | |
217 | */ | |
218 | page_remove_rmap(page, vma, false); | |
219 | put_page(page); | |
220 | ||
221 | if (pte_present(pte)) | |
222 | unmapped++; | |
223 | } else { | |
224 | put_page(page); | |
225 | mpfn = 0; | |
226 | } | |
227 | ||
228 | next: | |
229 | migrate->dst[migrate->npages] = 0; | |
230 | migrate->src[migrate->npages++] = mpfn; | |
231 | } | |
232 | arch_leave_lazy_mmu_mode(); | |
233 | pte_unmap_unlock(ptep - 1, ptl); | |
234 | ||
235 | /* Only flush the TLB if we actually modified any entries */ | |
236 | if (unmapped) | |
237 | flush_tlb_range(walk->vma, start, end); | |
238 | ||
239 | return 0; | |
240 | } | |
241 | ||
242 | static const struct mm_walk_ops migrate_vma_walk_ops = { | |
243 | .pmd_entry = migrate_vma_collect_pmd, | |
244 | .pte_hole = migrate_vma_collect_hole, | |
245 | }; | |
246 | ||
247 | /* | |
248 | * migrate_vma_collect() - collect pages over a range of virtual addresses | |
249 | * @migrate: migrate struct containing all migration information | |
250 | * | |
251 | * This will walk the CPU page table. For each virtual address backed by a | |
252 | * valid page, it updates the src array and takes a reference on the page, in | |
253 | * order to pin the page until we lock it and unmap it. | |
254 | */ | |
255 | static void migrate_vma_collect(struct migrate_vma *migrate) | |
256 | { | |
257 | struct mmu_notifier_range range; | |
258 | ||
259 | /* | |
260 | * Note that the pgmap_owner is passed to the mmu notifier callback so | |
261 | * that the registered device driver can skip invalidating device | |
262 | * private page mappings that won't be migrated. | |
263 | */ | |
264 | mmu_notifier_range_init_owner(&range, MMU_NOTIFY_MIGRATE, 0, | |
265 | migrate->vma, migrate->vma->vm_mm, migrate->start, migrate->end, | |
266 | migrate->pgmap_owner); | |
267 | mmu_notifier_invalidate_range_start(&range); | |
268 | ||
269 | walk_page_range(migrate->vma->vm_mm, migrate->start, migrate->end, | |
270 | &migrate_vma_walk_ops, migrate); | |
271 | ||
272 | mmu_notifier_invalidate_range_end(&range); | |
273 | migrate->end = migrate->start + (migrate->npages << PAGE_SHIFT); | |
274 | } | |
275 | ||
276 | /* | |
277 | * migrate_vma_check_page() - check if page is pinned or not | |
278 | * @page: struct page to check | |
279 | * | |
280 | * Pinned pages cannot be migrated. This is the same test as in | |
281 | * folio_migrate_mapping(), except that here we allow migration of a | |
282 | * ZONE_DEVICE page. | |
283 | */ | |
284 | static bool migrate_vma_check_page(struct page *page) | |
285 | { | |
286 | /* | |
287 | * One extra ref because caller holds an extra reference, either from | |
288 | * isolate_lru_page() for a regular page, or migrate_vma_collect() for | |
289 | * a device page. | |
290 | */ | |
291 | int extra = 1; | |
292 | ||
293 | /* | |
294 | * FIXME support THP (transparent huge page), it is bit more complex to | |
295 | * check them than regular pages, because they can be mapped with a pmd | |
296 | * or with a pte (split pte mapping). | |
297 | */ | |
298 | if (PageCompound(page)) | |
299 | return false; | |
300 | ||
301 | /* Page from ZONE_DEVICE have one extra reference */ | |
302 | if (is_zone_device_page(page)) | |
303 | extra++; | |
304 | ||
305 | /* For file back page */ | |
306 | if (page_mapping(page)) | |
307 | extra += 1 + page_has_private(page); | |
308 | ||
309 | if ((page_count(page) - extra) > page_mapcount(page)) | |
310 | return false; | |
311 | ||
312 | return true; | |
313 | } | |
314 | ||
315 | /* | |
316 | * migrate_vma_unmap() - replace page mapping with special migration pte entry | |
317 | * @migrate: migrate struct containing all migration information | |
318 | * | |
319 | * Isolate pages from the LRU and replace mappings (CPU page table pte) with a | |
320 | * special migration pte entry and check if it has been pinned. Pinned pages are | |
321 | * restored because we cannot migrate them. | |
322 | * | |
323 | * This is the last step before we call the device driver callback to allocate | |
324 | * destination memory and copy contents of original page over to new page. | |
325 | */ | |
326 | static void migrate_vma_unmap(struct migrate_vma *migrate) | |
327 | { | |
328 | const unsigned long npages = migrate->npages; | |
329 | unsigned long i, restore = 0; | |
330 | bool allow_drain = true; | |
331 | ||
332 | lru_add_drain(); | |
333 | ||
334 | for (i = 0; i < npages; i++) { | |
335 | struct page *page = migrate_pfn_to_page(migrate->src[i]); | |
336 | ||
337 | if (!page) | |
338 | continue; | |
339 | ||
340 | /* ZONE_DEVICE pages are not on LRU */ | |
341 | if (!is_zone_device_page(page)) { | |
342 | if (!PageLRU(page) && allow_drain) { | |
343 | /* Drain CPU's pagevec */ | |
344 | lru_add_drain_all(); | |
345 | allow_drain = false; | |
346 | } | |
347 | ||
348 | if (isolate_lru_page(page)) { | |
349 | migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; | |
350 | migrate->cpages--; | |
351 | restore++; | |
352 | continue; | |
353 | } | |
354 | ||
355 | /* Drop the reference we took in collect */ | |
356 | put_page(page); | |
357 | } | |
358 | ||
359 | if (page_mapped(page)) | |
360 | try_to_migrate(page, 0); | |
361 | ||
362 | if (page_mapped(page) || !migrate_vma_check_page(page)) { | |
363 | if (!is_zone_device_page(page)) { | |
364 | get_page(page); | |
365 | putback_lru_page(page); | |
366 | } | |
367 | ||
368 | migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; | |
369 | migrate->cpages--; | |
370 | restore++; | |
371 | continue; | |
372 | } | |
373 | } | |
374 | ||
375 | for (i = 0; i < npages && restore; i++) { | |
376 | struct page *page = migrate_pfn_to_page(migrate->src[i]); | |
377 | ||
378 | if (!page || (migrate->src[i] & MIGRATE_PFN_MIGRATE)) | |
379 | continue; | |
380 | ||
381 | remove_migration_ptes(page, page, false); | |
382 | ||
383 | migrate->src[i] = 0; | |
384 | unlock_page(page); | |
385 | put_page(page); | |
386 | restore--; | |
387 | } | |
388 | } | |
389 | ||
390 | /** | |
391 | * migrate_vma_setup() - prepare to migrate a range of memory | |
392 | * @args: contains the vma, start, and pfns arrays for the migration | |
393 | * | |
394 | * Returns: negative errno on failures, 0 when 0 or more pages were migrated | |
395 | * without an error. | |
396 | * | |
397 | * Prepare to migrate a range of memory virtual address range by collecting all | |
398 | * the pages backing each virtual address in the range, saving them inside the | |
399 | * src array. Then lock those pages and unmap them. Once the pages are locked | |
400 | * and unmapped, check whether each page is pinned or not. Pages that aren't | |
401 | * pinned have the MIGRATE_PFN_MIGRATE flag set (by this function) in the | |
402 | * corresponding src array entry. Then restores any pages that are pinned, by | |
403 | * remapping and unlocking those pages. | |
404 | * | |
405 | * The caller should then allocate destination memory and copy source memory to | |
406 | * it for all those entries (ie with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE | |
407 | * flag set). Once these are allocated and copied, the caller must update each | |
408 | * corresponding entry in the dst array with the pfn value of the destination | |
409 | * page and with MIGRATE_PFN_VALID. Destination pages must be locked via | |
410 | * lock_page(). | |
411 | * | |
412 | * Note that the caller does not have to migrate all the pages that are marked | |
413 | * with MIGRATE_PFN_MIGRATE flag in src array unless this is a migration from | |
414 | * device memory to system memory. If the caller cannot migrate a device page | |
415 | * back to system memory, then it must return VM_FAULT_SIGBUS, which has severe | |
416 | * consequences for the userspace process, so it must be avoided if at all | |
417 | * possible. | |
418 | * | |
419 | * For empty entries inside CPU page table (pte_none() or pmd_none() is true) we | |
420 | * do set MIGRATE_PFN_MIGRATE flag inside the corresponding source array thus | |
421 | * allowing the caller to allocate device memory for those unbacked virtual | |
422 | * addresses. For this the caller simply has to allocate device memory and | |
423 | * properly set the destination entry like for regular migration. Note that | |
424 | * this can still fail, and thus inside the device driver you must check if the | |
425 | * migration was successful for those entries after calling migrate_vma_pages(), | |
426 | * just like for regular migration. | |
427 | * | |
428 | * After that, the callers must call migrate_vma_pages() to go over each entry | |
429 | * in the src array that has the MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag | |
430 | * set. If the corresponding entry in dst array has MIGRATE_PFN_VALID flag set, | |
431 | * then migrate_vma_pages() to migrate struct page information from the source | |
432 | * struct page to the destination struct page. If it fails to migrate the | |
433 | * struct page information, then it clears the MIGRATE_PFN_MIGRATE flag in the | |
434 | * src array. | |
435 | * | |
436 | * At this point all successfully migrated pages have an entry in the src | |
437 | * array with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag set and the dst | |
438 | * array entry with MIGRATE_PFN_VALID flag set. | |
439 | * | |
440 | * Once migrate_vma_pages() returns the caller may inspect which pages were | |
441 | * successfully migrated, and which were not. Successfully migrated pages will | |
442 | * have the MIGRATE_PFN_MIGRATE flag set for their src array entry. | |
443 | * | |
444 | * It is safe to update device page table after migrate_vma_pages() because | |
445 | * both destination and source page are still locked, and the mmap_lock is held | |
446 | * in read mode (hence no one can unmap the range being migrated). | |
447 | * | |
448 | * Once the caller is done cleaning up things and updating its page table (if it | |
449 | * chose to do so, this is not an obligation) it finally calls | |
450 | * migrate_vma_finalize() to update the CPU page table to point to new pages | |
451 | * for successfully migrated pages or otherwise restore the CPU page table to | |
452 | * point to the original source pages. | |
453 | */ | |
454 | int migrate_vma_setup(struct migrate_vma *args) | |
455 | { | |
456 | long nr_pages = (args->end - args->start) >> PAGE_SHIFT; | |
457 | ||
458 | args->start &= PAGE_MASK; | |
459 | args->end &= PAGE_MASK; | |
460 | if (!args->vma || is_vm_hugetlb_page(args->vma) || | |
461 | (args->vma->vm_flags & VM_SPECIAL) || vma_is_dax(args->vma)) | |
462 | return -EINVAL; | |
463 | if (nr_pages <= 0) | |
464 | return -EINVAL; | |
465 | if (args->start < args->vma->vm_start || | |
466 | args->start >= args->vma->vm_end) | |
467 | return -EINVAL; | |
468 | if (args->end <= args->vma->vm_start || args->end > args->vma->vm_end) | |
469 | return -EINVAL; | |
470 | if (!args->src || !args->dst) | |
471 | return -EINVAL; | |
472 | ||
473 | memset(args->src, 0, sizeof(*args->src) * nr_pages); | |
474 | args->cpages = 0; | |
475 | args->npages = 0; | |
476 | ||
477 | migrate_vma_collect(args); | |
478 | ||
479 | if (args->cpages) | |
480 | migrate_vma_unmap(args); | |
481 | ||
482 | /* | |
483 | * At this point pages are locked and unmapped, and thus they have | |
484 | * stable content and can safely be copied to destination memory that | |
485 | * is allocated by the drivers. | |
486 | */ | |
487 | return 0; | |
488 | ||
489 | } | |
490 | EXPORT_SYMBOL(migrate_vma_setup); | |
491 | ||
492 | /* | |
493 | * This code closely matches the code in: | |
494 | * __handle_mm_fault() | |
495 | * handle_pte_fault() | |
496 | * do_anonymous_page() | |
497 | * to map in an anonymous zero page but the struct page will be a ZONE_DEVICE | |
498 | * private page. | |
499 | */ | |
500 | static void migrate_vma_insert_page(struct migrate_vma *migrate, | |
501 | unsigned long addr, | |
502 | struct page *page, | |
503 | unsigned long *src) | |
504 | { | |
505 | struct vm_area_struct *vma = migrate->vma; | |
506 | struct mm_struct *mm = vma->vm_mm; | |
507 | bool flush = false; | |
508 | spinlock_t *ptl; | |
509 | pte_t entry; | |
510 | pgd_t *pgdp; | |
511 | p4d_t *p4dp; | |
512 | pud_t *pudp; | |
513 | pmd_t *pmdp; | |
514 | pte_t *ptep; | |
515 | ||
516 | /* Only allow populating anonymous memory */ | |
517 | if (!vma_is_anonymous(vma)) | |
518 | goto abort; | |
519 | ||
520 | pgdp = pgd_offset(mm, addr); | |
521 | p4dp = p4d_alloc(mm, pgdp, addr); | |
522 | if (!p4dp) | |
523 | goto abort; | |
524 | pudp = pud_alloc(mm, p4dp, addr); | |
525 | if (!pudp) | |
526 | goto abort; | |
527 | pmdp = pmd_alloc(mm, pudp, addr); | |
528 | if (!pmdp) | |
529 | goto abort; | |
530 | ||
531 | if (pmd_trans_huge(*pmdp) || pmd_devmap(*pmdp)) | |
532 | goto abort; | |
533 | ||
534 | /* | |
535 | * Use pte_alloc() instead of pte_alloc_map(). We can't run | |
536 | * pte_offset_map() on pmds where a huge pmd might be created | |
537 | * from a different thread. | |
538 | * | |
539 | * pte_alloc_map() is safe to use under mmap_write_lock(mm) or when | |
540 | * parallel threads are excluded by other means. | |
541 | * | |
542 | * Here we only have mmap_read_lock(mm). | |
543 | */ | |
544 | if (pte_alloc(mm, pmdp)) | |
545 | goto abort; | |
546 | ||
547 | /* See the comment in pte_alloc_one_map() */ | |
548 | if (unlikely(pmd_trans_unstable(pmdp))) | |
549 | goto abort; | |
550 | ||
551 | if (unlikely(anon_vma_prepare(vma))) | |
552 | goto abort; | |
553 | if (mem_cgroup_charge(page_folio(page), vma->vm_mm, GFP_KERNEL)) | |
554 | goto abort; | |
555 | ||
556 | /* | |
557 | * The memory barrier inside __SetPageUptodate makes sure that | |
558 | * preceding stores to the page contents become visible before | |
559 | * the set_pte_at() write. | |
560 | */ | |
561 | __SetPageUptodate(page); | |
562 | ||
563 | if (is_device_private_page(page)) { | |
564 | swp_entry_t swp_entry; | |
565 | ||
566 | if (vma->vm_flags & VM_WRITE) | |
567 | swp_entry = make_writable_device_private_entry( | |
568 | page_to_pfn(page)); | |
569 | else | |
570 | swp_entry = make_readable_device_private_entry( | |
571 | page_to_pfn(page)); | |
572 | entry = swp_entry_to_pte(swp_entry); | |
573 | } else { | |
574 | /* | |
575 | * For now we only support migrating to un-addressable device | |
576 | * memory. | |
577 | */ | |
578 | if (is_zone_device_page(page)) { | |
579 | pr_warn_once("Unsupported ZONE_DEVICE page type.\n"); | |
580 | goto abort; | |
581 | } | |
582 | entry = mk_pte(page, vma->vm_page_prot); | |
583 | if (vma->vm_flags & VM_WRITE) | |
584 | entry = pte_mkwrite(pte_mkdirty(entry)); | |
585 | } | |
586 | ||
587 | ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl); | |
588 | ||
589 | if (check_stable_address_space(mm)) | |
590 | goto unlock_abort; | |
591 | ||
592 | if (pte_present(*ptep)) { | |
593 | unsigned long pfn = pte_pfn(*ptep); | |
594 | ||
595 | if (!is_zero_pfn(pfn)) | |
596 | goto unlock_abort; | |
597 | flush = true; | |
598 | } else if (!pte_none(*ptep)) | |
599 | goto unlock_abort; | |
600 | ||
601 | /* | |
602 | * Check for userfaultfd but do not deliver the fault. Instead, | |
603 | * just back off. | |
604 | */ | |
605 | if (userfaultfd_missing(vma)) | |
606 | goto unlock_abort; | |
607 | ||
608 | inc_mm_counter(mm, MM_ANONPAGES); | |
609 | page_add_new_anon_rmap(page, vma, addr, false); | |
610 | if (!is_zone_device_page(page)) | |
611 | lru_cache_add_inactive_or_unevictable(page, vma); | |
612 | get_page(page); | |
613 | ||
614 | if (flush) { | |
615 | flush_cache_page(vma, addr, pte_pfn(*ptep)); | |
616 | ptep_clear_flush_notify(vma, addr, ptep); | |
617 | set_pte_at_notify(mm, addr, ptep, entry); | |
618 | update_mmu_cache(vma, addr, ptep); | |
619 | } else { | |
620 | /* No need to invalidate - it was non-present before */ | |
621 | set_pte_at(mm, addr, ptep, entry); | |
622 | update_mmu_cache(vma, addr, ptep); | |
623 | } | |
624 | ||
625 | pte_unmap_unlock(ptep, ptl); | |
626 | *src = MIGRATE_PFN_MIGRATE; | |
627 | return; | |
628 | ||
629 | unlock_abort: | |
630 | pte_unmap_unlock(ptep, ptl); | |
631 | abort: | |
632 | *src &= ~MIGRATE_PFN_MIGRATE; | |
633 | } | |
634 | ||
635 | /** | |
636 | * migrate_vma_pages() - migrate meta-data from src page to dst page | |
637 | * @migrate: migrate struct containing all migration information | |
638 | * | |
639 | * This migrates struct page meta-data from source struct page to destination | |
640 | * struct page. This effectively finishes the migration from source page to the | |
641 | * destination page. | |
642 | */ | |
643 | void migrate_vma_pages(struct migrate_vma *migrate) | |
644 | { | |
645 | const unsigned long npages = migrate->npages; | |
646 | const unsigned long start = migrate->start; | |
647 | struct mmu_notifier_range range; | |
648 | unsigned long addr, i; | |
649 | bool notified = false; | |
650 | ||
651 | for (i = 0, addr = start; i < npages; addr += PAGE_SIZE, i++) { | |
652 | struct page *newpage = migrate_pfn_to_page(migrate->dst[i]); | |
653 | struct page *page = migrate_pfn_to_page(migrate->src[i]); | |
654 | struct address_space *mapping; | |
655 | int r; | |
656 | ||
657 | if (!newpage) { | |
658 | migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; | |
659 | continue; | |
660 | } | |
661 | ||
662 | if (!page) { | |
663 | if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE)) | |
664 | continue; | |
665 | if (!notified) { | |
666 | notified = true; | |
667 | ||
668 | mmu_notifier_range_init_owner(&range, | |
669 | MMU_NOTIFY_MIGRATE, 0, migrate->vma, | |
670 | migrate->vma->vm_mm, addr, migrate->end, | |
671 | migrate->pgmap_owner); | |
672 | mmu_notifier_invalidate_range_start(&range); | |
673 | } | |
674 | migrate_vma_insert_page(migrate, addr, newpage, | |
675 | &migrate->src[i]); | |
676 | continue; | |
677 | } | |
678 | ||
679 | mapping = page_mapping(page); | |
680 | ||
681 | if (is_device_private_page(newpage)) { | |
682 | /* | |
683 | * For now only support private anonymous when migrating | |
684 | * to un-addressable device memory. | |
685 | */ | |
686 | if (mapping) { | |
687 | migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; | |
688 | continue; | |
689 | } | |
690 | } else if (is_zone_device_page(newpage)) { | |
691 | /* | |
692 | * Other types of ZONE_DEVICE page are not supported. | |
693 | */ | |
694 | migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; | |
695 | continue; | |
696 | } | |
697 | ||
698 | r = migrate_page(mapping, newpage, page, MIGRATE_SYNC_NO_COPY); | |
699 | if (r != MIGRATEPAGE_SUCCESS) | |
700 | migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; | |
701 | } | |
702 | ||
703 | /* | |
704 | * No need to double call mmu_notifier->invalidate_range() callback as | |
705 | * the above ptep_clear_flush_notify() inside migrate_vma_insert_page() | |
706 | * did already call it. | |
707 | */ | |
708 | if (notified) | |
709 | mmu_notifier_invalidate_range_only_end(&range); | |
710 | } | |
711 | EXPORT_SYMBOL(migrate_vma_pages); | |
712 | ||
713 | /** | |
714 | * migrate_vma_finalize() - restore CPU page table entry | |
715 | * @migrate: migrate struct containing all migration information | |
716 | * | |
717 | * This replaces the special migration pte entry with either a mapping to the | |
718 | * new page if migration was successful for that page, or to the original page | |
719 | * otherwise. | |
720 | * | |
721 | * This also unlocks the pages and puts them back on the lru, or drops the extra | |
722 | * refcount, for device pages. | |
723 | */ | |
724 | void migrate_vma_finalize(struct migrate_vma *migrate) | |
725 | { | |
726 | const unsigned long npages = migrate->npages; | |
727 | unsigned long i; | |
728 | ||
729 | for (i = 0; i < npages; i++) { | |
730 | struct page *newpage = migrate_pfn_to_page(migrate->dst[i]); | |
731 | struct page *page = migrate_pfn_to_page(migrate->src[i]); | |
732 | ||
733 | if (!page) { | |
734 | if (newpage) { | |
735 | unlock_page(newpage); | |
736 | put_page(newpage); | |
737 | } | |
738 | continue; | |
739 | } | |
740 | ||
741 | if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE) || !newpage) { | |
742 | if (newpage) { | |
743 | unlock_page(newpage); | |
744 | put_page(newpage); | |
745 | } | |
746 | newpage = page; | |
747 | } | |
748 | ||
749 | remove_migration_ptes(page, newpage, false); | |
750 | unlock_page(page); | |
751 | ||
752 | if (is_zone_device_page(page)) | |
753 | put_page(page); | |
754 | else | |
755 | putback_lru_page(page); | |
756 | ||
757 | if (newpage != page) { | |
758 | unlock_page(newpage); | |
759 | if (is_zone_device_page(newpage)) | |
760 | put_page(newpage); | |
761 | else | |
762 | putback_lru_page(newpage); | |
763 | } | |
764 | } | |
765 | } | |
766 | EXPORT_SYMBOL(migrate_vma_finalize); |