blob: 24485f5ec498dc48fb40e8d4f23d689cde5da357
1 | /* |
2 | * Memory Migration functionality - linux/mm/migrate.c |
3 | * |
4 | * Copyright (C) 2006 Silicon Graphics, Inc., Christoph Lameter |
5 | * |
6 | * Page migration was first developed in the context of the memory hotplug |
7 | * project. The main authors of the migration code are: |
8 | * |
9 | * IWAMOTO Toshihiro <iwamoto@valinux.co.jp> |
10 | * Hirokazu Takahashi <taka@valinux.co.jp> |
11 | * Dave Hansen <haveblue@us.ibm.com> |
12 | * Christoph Lameter |
13 | */ |
14 | |
15 | #include <linux/migrate.h> |
16 | #include <linux/export.h> |
17 | #include <linux/swap.h> |
18 | #include <linux/swapops.h> |
19 | #include <linux/pagemap.h> |
20 | #include <linux/buffer_head.h> |
21 | #include <linux/mm_inline.h> |
22 | #include <linux/nsproxy.h> |
23 | #include <linux/pagevec.h> |
24 | #include <linux/ksm.h> |
25 | #include <linux/rmap.h> |
26 | #include <linux/topology.h> |
27 | #include <linux/cpu.h> |
28 | #include <linux/cpuset.h> |
29 | #include <linux/writeback.h> |
30 | #include <linux/mempolicy.h> |
31 | #include <linux/vmalloc.h> |
32 | #include <linux/security.h> |
33 | #include <linux/backing-dev.h> |
34 | #include <linux/compaction.h> |
35 | #include <linux/syscalls.h> |
36 | #include <linux/hugetlb.h> |
37 | #include <linux/hugetlb_cgroup.h> |
38 | #include <linux/gfp.h> |
39 | #include <linux/balloon_compaction.h> |
40 | #include <linux/mmu_notifier.h> |
41 | #include <linux/page_idle.h> |
42 | #include <linux/page_owner.h> |
43 | #include <linux/ptrace.h> |
44 | #ifdef CONFIG_AMLOGIC_CMA |
45 | #include <linux/delay.h> |
46 | #endif |
47 | |
48 | #include <asm/tlbflush.h> |
49 | |
50 | #define CREATE_TRACE_POINTS |
51 | #include <trace/events/migrate.h> |
52 | |
53 | #include "internal.h" |
54 | |
55 | /* |
56 | * migrate_prep() needs to be called before we start compiling a list of pages |
57 | * to be migrated using isolate_lru_page(). If scheduling work on other CPUs is |
58 | * undesirable, use migrate_prep_local() |
59 | */ |
60 | int migrate_prep(void) |
61 | { |
62 | /* |
63 | * Clear the LRU lists so pages can be isolated. |
64 | * Note that pages may be moved off the LRU after we have |
65 | * drained them. Those pages will fail to migrate like other |
66 | * pages that may be busy. |
67 | */ |
68 | lru_add_drain_all(); |
69 | |
70 | return 0; |
71 | } |
72 | |
73 | /* Do the necessary work of migrate_prep but not if it involves other CPUs */ |
74 | int migrate_prep_local(void) |
75 | { |
76 | lru_add_drain(); |
77 | |
78 | return 0; |
79 | } |
80 | |
81 | bool isolate_movable_page(struct page *page, isolate_mode_t mode) |
82 | { |
83 | struct address_space *mapping; |
84 | |
85 | /* |
86 | * Avoid burning cycles with pages that are yet under __free_pages(), |
87 | * or just got freed under us. |
88 | * |
89 | * In case we 'win' a race for a movable page being freed under us and |
90 | * raise its refcount preventing __free_pages() from doing its job |
91 | * the put_page() at the end of this block will take care of |
92 | * release this page, thus avoiding a nasty leakage. |
93 | */ |
94 | if (unlikely(!get_page_unless_zero(page))) |
95 | goto out; |
96 | |
97 | /* |
98 | * Check PageMovable before holding a PG_lock because page's owner |
99 | * assumes anybody doesn't touch PG_lock of newly allocated page |
100 | * so unconditionally grapping the lock ruins page's owner side. |
101 | */ |
102 | if (unlikely(!__PageMovable(page))) |
103 | goto out_putpage; |
104 | /* |
105 | * As movable pages are not isolated from LRU lists, concurrent |
106 | * compaction threads can race against page migration functions |
107 | * as well as race against the releasing a page. |
108 | * |
109 | * In order to avoid having an already isolated movable page |
110 | * being (wrongly) re-isolated while it is under migration, |
111 | * or to avoid attempting to isolate pages being released, |
112 | * lets be sure we have the page lock |
113 | * before proceeding with the movable page isolation steps. |
114 | */ |
115 | if (unlikely(!trylock_page(page))) |
116 | goto out_putpage; |
117 | |
118 | if (!PageMovable(page) || PageIsolated(page)) |
119 | goto out_no_isolated; |
120 | |
121 | mapping = page_mapping(page); |
122 | VM_BUG_ON_PAGE(!mapping, page); |
123 | |
124 | if (!mapping->a_ops->isolate_page(page, mode)) |
125 | goto out_no_isolated; |
126 | |
127 | /* Driver shouldn't use PG_isolated bit of page->flags */ |
128 | WARN_ON_ONCE(PageIsolated(page)); |
129 | __SetPageIsolated(page); |
130 | unlock_page(page); |
131 | |
132 | return true; |
133 | |
134 | out_no_isolated: |
135 | unlock_page(page); |
136 | out_putpage: |
137 | put_page(page); |
138 | out: |
139 | return false; |
140 | } |
141 | |
142 | /* It should be called on page which is PG_movable */ |
143 | void putback_movable_page(struct page *page) |
144 | { |
145 | struct address_space *mapping; |
146 | |
147 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
148 | VM_BUG_ON_PAGE(!PageMovable(page), page); |
149 | VM_BUG_ON_PAGE(!PageIsolated(page), page); |
150 | |
151 | mapping = page_mapping(page); |
152 | mapping->a_ops->putback_page(page); |
153 | __ClearPageIsolated(page); |
154 | } |
155 | |
156 | /* |
157 | * Put previously isolated pages back onto the appropriate lists |
158 | * from where they were once taken off for compaction/migration. |
159 | * |
160 | * This function shall be used whenever the isolated pageset has been |
161 | * built from lru, balloon, hugetlbfs page. See isolate_migratepages_range() |
162 | * and isolate_huge_page(). |
163 | */ |
164 | void putback_movable_pages(struct list_head *l) |
165 | { |
166 | struct page *page; |
167 | struct page *page2; |
168 | |
169 | list_for_each_entry_safe(page, page2, l, lru) { |
170 | #ifdef CONFIG_AMLOGIC_CMA |
171 | if (PageCmaAllocating(page)) /* migrate/reclaim failed */ |
172 | ClearPageCmaAllocating(page); |
173 | #endif |
174 | if (unlikely(PageHuge(page))) { |
175 | putback_active_hugepage(page); |
176 | continue; |
177 | } |
178 | list_del(&page->lru); |
179 | /* |
180 | * We isolated non-lru movable page so here we can use |
181 | * __PageMovable because LRU page's mapping cannot have |
182 | * PAGE_MAPPING_MOVABLE. |
183 | */ |
184 | if (unlikely(__PageMovable(page))) { |
185 | VM_BUG_ON_PAGE(!PageIsolated(page), page); |
186 | lock_page(page); |
187 | if (PageMovable(page)) |
188 | putback_movable_page(page); |
189 | else |
190 | __ClearPageIsolated(page); |
191 | unlock_page(page); |
192 | put_page(page); |
193 | } else { |
194 | dec_node_page_state(page, NR_ISOLATED_ANON + |
195 | page_is_file_cache(page)); |
196 | putback_lru_page(page); |
197 | } |
198 | } |
199 | } |
200 | |
201 | /* |
202 | * Restore a potential migration pte to a working pte entry |
203 | */ |
204 | static int remove_migration_pte(struct page *new, struct vm_area_struct *vma, |
205 | unsigned long addr, void *old) |
206 | { |
207 | struct mm_struct *mm = vma->vm_mm; |
208 | swp_entry_t entry; |
209 | pmd_t *pmd; |
210 | pte_t *ptep, pte; |
211 | spinlock_t *ptl; |
212 | |
213 | if (unlikely(PageHuge(new))) { |
214 | ptep = huge_pte_offset(mm, addr); |
215 | if (!ptep) |
216 | goto out; |
217 | ptl = huge_pte_lockptr(hstate_vma(vma), mm, ptep); |
218 | } else { |
219 | pmd = mm_find_pmd(mm, addr); |
220 | if (!pmd) |
221 | goto out; |
222 | |
223 | ptep = pte_offset_map(pmd, addr); |
224 | |
225 | /* |
226 | * Peek to check is_swap_pte() before taking ptlock? No, we |
227 | * can race mremap's move_ptes(), which skips anon_vma lock. |
228 | */ |
229 | |
230 | ptl = pte_lockptr(mm, pmd); |
231 | } |
232 | |
233 | spin_lock(ptl); |
234 | pte = *ptep; |
235 | if (!is_swap_pte(pte)) |
236 | goto unlock; |
237 | |
238 | entry = pte_to_swp_entry(pte); |
239 | |
240 | if (!is_migration_entry(entry) || |
241 | migration_entry_to_page(entry) != old) |
242 | goto unlock; |
243 | |
244 | get_page(new); |
245 | pte = pte_mkold(mk_pte(new, READ_ONCE(vma->vm_page_prot))); |
246 | if (pte_swp_soft_dirty(*ptep)) |
247 | pte = pte_mksoft_dirty(pte); |
248 | |
249 | /* Recheck VMA as permissions can change since migration started */ |
250 | if (is_write_migration_entry(entry)) |
251 | pte = maybe_mkwrite(pte, vma); |
252 | |
253 | #ifdef CONFIG_HUGETLB_PAGE |
254 | if (PageHuge(new)) { |
255 | pte = pte_mkhuge(pte); |
256 | pte = arch_make_huge_pte(pte, vma, new, 0); |
257 | } |
258 | #endif |
259 | flush_dcache_page(new); |
260 | set_pte_at(mm, addr, ptep, pte); |
261 | |
262 | if (PageHuge(new)) { |
263 | if (PageAnon(new)) |
264 | hugepage_add_anon_rmap(new, vma, addr); |
265 | else |
266 | page_dup_rmap(new, true); |
267 | } else if (PageAnon(new)) |
268 | page_add_anon_rmap(new, vma, addr, false); |
269 | else |
270 | page_add_file_rmap(new, false); |
271 | |
272 | if (vma->vm_flags & VM_LOCKED && !PageTransCompound(new)) |
273 | mlock_vma_page(new); |
274 | |
275 | /* No need to invalidate - it was non-present before */ |
276 | update_mmu_cache(vma, addr, ptep); |
277 | unlock: |
278 | pte_unmap_unlock(ptep, ptl); |
279 | out: |
280 | return SWAP_AGAIN; |
281 | } |
282 | |
283 | /* |
284 | * Get rid of all migration entries and replace them by |
285 | * references to the indicated page. |
286 | */ |
287 | void remove_migration_ptes(struct page *old, struct page *new, bool locked) |
288 | { |
289 | struct rmap_walk_control rwc = { |
290 | .rmap_one = remove_migration_pte, |
291 | .arg = old, |
292 | }; |
293 | |
294 | if (locked) |
295 | rmap_walk_locked(new, &rwc); |
296 | else |
297 | rmap_walk(new, &rwc); |
298 | } |
299 | |
300 | /* |
301 | * Something used the pte of a page under migration. We need to |
302 | * get to the page and wait until migration is finished. |
303 | * When we return from this function the fault will be retried. |
304 | */ |
305 | void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep, |
306 | spinlock_t *ptl) |
307 | { |
308 | pte_t pte; |
309 | swp_entry_t entry; |
310 | struct page *page; |
311 | #ifdef CONFIG_AMLOGIC_CMA |
312 | bool need_wait = 0; |
313 | #endif |
314 | |
315 | spin_lock(ptl); |
316 | pte = *ptep; |
317 | if (!is_swap_pte(pte)) |
318 | goto out; |
319 | |
320 | entry = pte_to_swp_entry(pte); |
321 | if (!is_migration_entry(entry)) |
322 | goto out; |
323 | |
324 | page = migration_entry_to_page(entry); |
325 | #ifdef CONFIG_AMLOGIC_CMA |
326 | /* This page is under cma allocating, do not increase it ref */ |
327 | if (PageCmaAllocating(page)) { |
328 | pr_debug("%s, Page:%lx, flags:%lx, m:%d, c:%d, map:%p\n", |
329 | __func__, page_to_pfn(page), page->flags, |
330 | page_mapcount(page), page_count(page), |
331 | page->mapping); |
332 | need_wait = 1; |
333 | goto out; |
334 | } |
335 | #endif |
336 | |
337 | /* |
338 | * Once radix-tree replacement of page migration started, page_count |
339 | * *must* be zero. And, we don't want to call wait_on_page_locked() |
340 | * against a page without get_page(). |
341 | * So, we use get_page_unless_zero(), here. Even failed, page fault |
342 | * will occur again. |
343 | */ |
344 | if (!get_page_unless_zero(page)) |
345 | goto out; |
346 | pte_unmap_unlock(ptep, ptl); |
347 | wait_on_page_locked(page); |
348 | put_page(page); |
349 | return; |
350 | out: |
351 | pte_unmap_unlock(ptep, ptl); |
352 | #ifdef CONFIG_AMLOGIC_CMA |
353 | if (need_wait) |
354 | usleep_range(1000, 1100); |
355 | #endif |
356 | } |
357 | |
358 | void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd, |
359 | unsigned long address) |
360 | { |
361 | spinlock_t *ptl = pte_lockptr(mm, pmd); |
362 | pte_t *ptep = pte_offset_map(pmd, address); |
363 | __migration_entry_wait(mm, ptep, ptl); |
364 | } |
365 | |
366 | void migration_entry_wait_huge(struct vm_area_struct *vma, |
367 | struct mm_struct *mm, pte_t *pte) |
368 | { |
369 | spinlock_t *ptl = huge_pte_lockptr(hstate_vma(vma), mm, pte); |
370 | __migration_entry_wait(mm, pte, ptl); |
371 | } |
372 | |
373 | #ifdef CONFIG_BLOCK |
374 | /* Returns true if all buffers are successfully locked */ |
375 | static bool buffer_migrate_lock_buffers(struct buffer_head *head, |
376 | enum migrate_mode mode) |
377 | { |
378 | struct buffer_head *bh = head; |
379 | |
380 | /* Simple case, sync compaction */ |
381 | if (mode != MIGRATE_ASYNC) { |
382 | do { |
383 | get_bh(bh); |
384 | lock_buffer(bh); |
385 | bh = bh->b_this_page; |
386 | |
387 | } while (bh != head); |
388 | |
389 | return true; |
390 | } |
391 | |
392 | /* async case, we cannot block on lock_buffer so use trylock_buffer */ |
393 | do { |
394 | get_bh(bh); |
395 | if (!trylock_buffer(bh)) { |
396 | /* |
397 | * We failed to lock the buffer and cannot stall in |
398 | * async migration. Release the taken locks |
399 | */ |
400 | struct buffer_head *failed_bh = bh; |
401 | put_bh(failed_bh); |
402 | bh = head; |
403 | while (bh != failed_bh) { |
404 | unlock_buffer(bh); |
405 | put_bh(bh); |
406 | bh = bh->b_this_page; |
407 | } |
408 | return false; |
409 | } |
410 | |
411 | bh = bh->b_this_page; |
412 | } while (bh != head); |
413 | return true; |
414 | } |
415 | #else |
416 | static inline bool buffer_migrate_lock_buffers(struct buffer_head *head, |
417 | enum migrate_mode mode) |
418 | { |
419 | return true; |
420 | } |
421 | #endif /* CONFIG_BLOCK */ |
422 | |
423 | /* |
424 | * Replace the page in the mapping. |
425 | * |
426 | * The number of remaining references must be: |
427 | * 1 for anonymous pages without a mapping |
428 | * 2 for pages with a mapping |
429 | * 3 for pages with a mapping and PagePrivate/PagePrivate2 set. |
430 | */ |
431 | int migrate_page_move_mapping(struct address_space *mapping, |
432 | struct page *newpage, struct page *page, |
433 | struct buffer_head *head, enum migrate_mode mode, |
434 | int extra_count) |
435 | { |
436 | struct zone *oldzone, *newzone; |
437 | int dirty; |
438 | int expected_count = 1 + extra_count; |
439 | void **pslot; |
440 | |
441 | if (!mapping) { |
442 | /* Anonymous page without mapping */ |
443 | #ifdef CONFIG_AMLOGIC_CMA |
444 | if (page_count(page) != expected_count) |
445 | cma_debug(2, page, " anon page cnt miss match, e:%d\n", |
446 | expected_count); |
447 | #endif |
448 | if (page_count(page) != expected_count) |
449 | return -EAGAIN; |
450 | |
451 | /* No turning back from here */ |
452 | newpage->index = page->index; |
453 | newpage->mapping = page->mapping; |
454 | if (PageSwapBacked(page)) |
455 | __SetPageSwapBacked(newpage); |
456 | |
457 | return MIGRATEPAGE_SUCCESS; |
458 | } |
459 | |
460 | oldzone = page_zone(page); |
461 | newzone = page_zone(newpage); |
462 | |
463 | spin_lock_irq(&mapping->tree_lock); |
464 | |
465 | pslot = radix_tree_lookup_slot(&mapping->page_tree, |
466 | page_index(page)); |
467 | |
468 | expected_count += 1 + page_has_private(page); |
469 | if (page_count(page) != expected_count || |
470 | radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) { |
471 | spin_unlock_irq(&mapping->tree_lock); |
472 | #ifdef CONFIG_AMLOGIC_CMA |
473 | cma_debug(2, page, " file page cnt miss match, e:%d, p:%d\n", |
474 | expected_count, page_has_private(page)); |
475 | #endif |
476 | return -EAGAIN; |
477 | } |
478 | |
479 | if (!page_ref_freeze(page, expected_count)) { |
480 | spin_unlock_irq(&mapping->tree_lock); |
481 | return -EAGAIN; |
482 | } |
483 | |
484 | /* |
485 | * In the async migration case of moving a page with buffers, lock the |
486 | * buffers using trylock before the mapping is moved. If the mapping |
487 | * was moved, we later failed to lock the buffers and could not move |
488 | * the mapping back due to an elevated page count, we would have to |
489 | * block waiting on other references to be dropped. |
490 | */ |
491 | if (mode == MIGRATE_ASYNC && head && |
492 | !buffer_migrate_lock_buffers(head, mode)) { |
493 | page_ref_unfreeze(page, expected_count); |
494 | spin_unlock_irq(&mapping->tree_lock); |
495 | return -EAGAIN; |
496 | } |
497 | |
498 | /* |
499 | * Now we know that no one else is looking at the page: |
500 | * no turning back from here. |
501 | */ |
502 | newpage->index = page->index; |
503 | newpage->mapping = page->mapping; |
504 | if (PageSwapBacked(page)) |
505 | __SetPageSwapBacked(newpage); |
506 | |
507 | get_page(newpage); /* add cache reference */ |
508 | if (PageSwapCache(page)) { |
509 | SetPageSwapCache(newpage); |
510 | set_page_private(newpage, page_private(page)); |
511 | } |
512 | |
513 | /* Move dirty while page refs frozen and newpage not yet exposed */ |
514 | dirty = PageDirty(page); |
515 | if (dirty) { |
516 | ClearPageDirty(page); |
517 | SetPageDirty(newpage); |
518 | } |
519 | |
520 | radix_tree_replace_slot(pslot, newpage); |
521 | |
522 | /* |
523 | * Drop cache reference from old page by unfreezing |
524 | * to one less reference. |
525 | * We know this isn't the last reference. |
526 | */ |
527 | page_ref_unfreeze(page, expected_count - 1); |
528 | |
529 | spin_unlock(&mapping->tree_lock); |
530 | /* Leave irq disabled to prevent preemption while updating stats */ |
531 | |
532 | /* |
533 | * If moved to a different zone then also account |
534 | * the page for that zone. Other VM counters will be |
535 | * taken care of when we establish references to the |
536 | * new page and drop references to the old page. |
537 | * |
538 | * Note that anonymous pages are accounted for |
539 | * via NR_FILE_PAGES and NR_ANON_MAPPED if they |
540 | * are mapped to swap space. |
541 | */ |
542 | if (newzone != oldzone) { |
543 | __dec_node_state(oldzone->zone_pgdat, NR_FILE_PAGES); |
544 | __inc_node_state(newzone->zone_pgdat, NR_FILE_PAGES); |
545 | if (PageSwapBacked(page) && !PageSwapCache(page)) { |
546 | __dec_node_state(oldzone->zone_pgdat, NR_SHMEM); |
547 | __inc_node_state(newzone->zone_pgdat, NR_SHMEM); |
548 | } |
549 | if (dirty && mapping_cap_account_dirty(mapping)) { |
550 | __dec_node_state(oldzone->zone_pgdat, NR_FILE_DIRTY); |
551 | __dec_zone_state(oldzone, NR_ZONE_WRITE_PENDING); |
552 | __inc_node_state(newzone->zone_pgdat, NR_FILE_DIRTY); |
553 | __inc_zone_state(newzone, NR_ZONE_WRITE_PENDING); |
554 | } |
555 | } |
556 | local_irq_enable(); |
557 | |
558 | return MIGRATEPAGE_SUCCESS; |
559 | } |
560 | EXPORT_SYMBOL(migrate_page_move_mapping); |
561 | |
562 | /* |
563 | * The expected number of remaining references is the same as that |
564 | * of migrate_page_move_mapping(). |
565 | */ |
566 | int migrate_huge_page_move_mapping(struct address_space *mapping, |
567 | struct page *newpage, struct page *page) |
568 | { |
569 | int expected_count; |
570 | void **pslot; |
571 | |
572 | spin_lock_irq(&mapping->tree_lock); |
573 | |
574 | pslot = radix_tree_lookup_slot(&mapping->page_tree, |
575 | page_index(page)); |
576 | |
577 | expected_count = 2 + page_has_private(page); |
578 | if (page_count(page) != expected_count || |
579 | radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) { |
580 | spin_unlock_irq(&mapping->tree_lock); |
581 | return -EAGAIN; |
582 | } |
583 | |
584 | if (!page_ref_freeze(page, expected_count)) { |
585 | spin_unlock_irq(&mapping->tree_lock); |
586 | return -EAGAIN; |
587 | } |
588 | |
589 | newpage->index = page->index; |
590 | newpage->mapping = page->mapping; |
591 | |
592 | get_page(newpage); |
593 | |
594 | radix_tree_replace_slot(pslot, newpage); |
595 | |
596 | page_ref_unfreeze(page, expected_count - 1); |
597 | |
598 | spin_unlock_irq(&mapping->tree_lock); |
599 | |
600 | return MIGRATEPAGE_SUCCESS; |
601 | } |
602 | |
603 | /* |
604 | * Gigantic pages are so large that we do not guarantee that page++ pointer |
605 | * arithmetic will work across the entire page. We need something more |
606 | * specialized. |
607 | */ |
608 | static void __copy_gigantic_page(struct page *dst, struct page *src, |
609 | int nr_pages) |
610 | { |
611 | int i; |
612 | struct page *dst_base = dst; |
613 | struct page *src_base = src; |
614 | |
615 | for (i = 0; i < nr_pages; ) { |
616 | cond_resched(); |
617 | copy_highpage(dst, src); |
618 | |
619 | i++; |
620 | dst = mem_map_next(dst, dst_base, i); |
621 | src = mem_map_next(src, src_base, i); |
622 | } |
623 | } |
624 | |
625 | static void copy_huge_page(struct page *dst, struct page *src) |
626 | { |
627 | int i; |
628 | int nr_pages; |
629 | |
630 | if (PageHuge(src)) { |
631 | /* hugetlbfs page */ |
632 | struct hstate *h = page_hstate(src); |
633 | nr_pages = pages_per_huge_page(h); |
634 | |
635 | if (unlikely(nr_pages > MAX_ORDER_NR_PAGES)) { |
636 | __copy_gigantic_page(dst, src, nr_pages); |
637 | return; |
638 | } |
639 | } else { |
640 | /* thp page */ |
641 | BUG_ON(!PageTransHuge(src)); |
642 | nr_pages = hpage_nr_pages(src); |
643 | } |
644 | |
645 | for (i = 0; i < nr_pages; i++) { |
646 | cond_resched(); |
647 | copy_highpage(dst + i, src + i); |
648 | } |
649 | } |
650 | |
651 | /* |
652 | * Copy the page to its new location |
653 | */ |
654 | void migrate_page_copy(struct page *newpage, struct page *page) |
655 | { |
656 | int cpupid; |
657 | |
658 | if (PageHuge(page) || PageTransHuge(page)) |
659 | copy_huge_page(newpage, page); |
660 | else |
661 | copy_highpage(newpage, page); |
662 | |
663 | if (PageError(page)) |
664 | SetPageError(newpage); |
665 | if (PageReferenced(page)) |
666 | SetPageReferenced(newpage); |
667 | if (PageUptodate(page)) |
668 | SetPageUptodate(newpage); |
669 | if (TestClearPageActive(page)) { |
670 | VM_BUG_ON_PAGE(PageUnevictable(page), page); |
671 | SetPageActive(newpage); |
672 | } else if (TestClearPageUnevictable(page)) |
673 | SetPageUnevictable(newpage); |
674 | if (PageWorkingset(page)) |
675 | SetPageWorkingset(newpage); |
676 | if (PageChecked(page)) |
677 | SetPageChecked(newpage); |
678 | if (PageMappedToDisk(page)) |
679 | SetPageMappedToDisk(newpage); |
680 | |
681 | /* Move dirty on pages not done by migrate_page_move_mapping() */ |
682 | if (PageDirty(page)) |
683 | SetPageDirty(newpage); |
684 | |
685 | if (page_is_young(page)) |
686 | set_page_young(newpage); |
687 | if (page_is_idle(page)) |
688 | set_page_idle(newpage); |
689 | |
690 | /* |
691 | * Copy NUMA information to the new page, to prevent over-eager |
692 | * future migrations of this same page. |
693 | */ |
694 | cpupid = page_cpupid_xchg_last(page, -1); |
695 | page_cpupid_xchg_last(newpage, cpupid); |
696 | |
697 | ksm_migrate_page(newpage, page); |
698 | /* |
699 | * Please do not reorder this without considering how mm/ksm.c's |
700 | * get_ksm_page() depends upon ksm_migrate_page() and PageSwapCache(). |
701 | */ |
702 | if (PageSwapCache(page)) |
703 | ClearPageSwapCache(page); |
704 | ClearPagePrivate(page); |
705 | set_page_private(page, 0); |
706 | |
707 | /* |
708 | * If any waiters have accumulated on the new page then |
709 | * wake them up. |
710 | */ |
711 | if (PageWriteback(newpage)) |
712 | end_page_writeback(newpage); |
713 | |
714 | copy_page_owner(page, newpage); |
715 | |
716 | mem_cgroup_migrate(page, newpage); |
717 | } |
718 | EXPORT_SYMBOL(migrate_page_copy); |
719 | |
720 | /************************************************************ |
721 | * Migration functions |
722 | ***********************************************************/ |
723 | |
724 | /* |
725 | * Common logic to directly migrate a single LRU page suitable for |
726 | * pages that do not use PagePrivate/PagePrivate2. |
727 | * |
728 | * Pages are locked upon entry and exit. |
729 | */ |
730 | int migrate_page(struct address_space *mapping, |
731 | struct page *newpage, struct page *page, |
732 | enum migrate_mode mode) |
733 | { |
734 | int rc; |
735 | |
736 | BUG_ON(PageWriteback(page)); /* Writeback must be complete */ |
737 | |
738 | rc = migrate_page_move_mapping(mapping, newpage, page, NULL, mode, 0); |
739 | |
740 | if (rc != MIGRATEPAGE_SUCCESS) |
741 | return rc; |
742 | |
743 | migrate_page_copy(newpage, page); |
744 | return MIGRATEPAGE_SUCCESS; |
745 | } |
746 | EXPORT_SYMBOL(migrate_page); |
747 | |
748 | #ifdef CONFIG_BLOCK |
749 | /* |
750 | * Migration function for pages with buffers. This function can only be used |
751 | * if the underlying filesystem guarantees that no other references to "page" |
752 | * exist. |
753 | */ |
754 | int buffer_migrate_page(struct address_space *mapping, |
755 | struct page *newpage, struct page *page, enum migrate_mode mode) |
756 | { |
757 | struct buffer_head *bh, *head; |
758 | int rc; |
759 | |
760 | if (!page_has_buffers(page)) |
761 | return migrate_page(mapping, newpage, page, mode); |
762 | |
763 | head = page_buffers(page); |
764 | |
765 | rc = migrate_page_move_mapping(mapping, newpage, page, head, mode, 0); |
766 | |
767 | if (rc != MIGRATEPAGE_SUCCESS) |
768 | return rc; |
769 | |
770 | /* |
771 | * In the async case, migrate_page_move_mapping locked the buffers |
772 | * with an IRQ-safe spinlock held. In the sync case, the buffers |
773 | * need to be locked now |
774 | */ |
775 | if (mode != MIGRATE_ASYNC) |
776 | BUG_ON(!buffer_migrate_lock_buffers(head, mode)); |
777 | |
778 | ClearPagePrivate(page); |
779 | set_page_private(newpage, page_private(page)); |
780 | set_page_private(page, 0); |
781 | put_page(page); |
782 | get_page(newpage); |
783 | |
784 | bh = head; |
785 | do { |
786 | set_bh_page(bh, newpage, bh_offset(bh)); |
787 | bh = bh->b_this_page; |
788 | |
789 | } while (bh != head); |
790 | |
791 | SetPagePrivate(newpage); |
792 | |
793 | migrate_page_copy(newpage, page); |
794 | |
795 | bh = head; |
796 | do { |
797 | unlock_buffer(bh); |
798 | put_bh(bh); |
799 | bh = bh->b_this_page; |
800 | |
801 | } while (bh != head); |
802 | |
803 | return MIGRATEPAGE_SUCCESS; |
804 | } |
805 | EXPORT_SYMBOL(buffer_migrate_page); |
806 | #endif |
807 | |
808 | /* |
809 | * Writeback a page to clean the dirty state |
810 | */ |
811 | static int writeout(struct address_space *mapping, struct page *page) |
812 | { |
813 | struct writeback_control wbc = { |
814 | .sync_mode = WB_SYNC_NONE, |
815 | .nr_to_write = 1, |
816 | .range_start = 0, |
817 | .range_end = LLONG_MAX, |
818 | .for_reclaim = 1 |
819 | }; |
820 | int rc; |
821 | |
822 | if (!mapping->a_ops->writepage) |
823 | /* No write method for the address space */ |
824 | return -EINVAL; |
825 | |
826 | if (!clear_page_dirty_for_io(page)) |
827 | /* Someone else already triggered a write */ |
828 | return -EAGAIN; |
829 | |
830 | /* |
831 | * A dirty page may imply that the underlying filesystem has |
832 | * the page on some queue. So the page must be clean for |
833 | * migration. Writeout may mean we loose the lock and the |
834 | * page state is no longer what we checked for earlier. |
835 | * At this point we know that the migration attempt cannot |
836 | * be successful. |
837 | */ |
838 | remove_migration_ptes(page, page, false); |
839 | |
840 | rc = mapping->a_ops->writepage(page, &wbc); |
841 | |
842 | if (rc != AOP_WRITEPAGE_ACTIVATE) |
843 | /* unlocked. Relock */ |
844 | lock_page(page); |
845 | |
846 | return (rc < 0) ? -EIO : -EAGAIN; |
847 | } |
848 | |
849 | /* |
850 | * Default handling if a filesystem does not provide a migration function. |
851 | */ |
852 | static int fallback_migrate_page(struct address_space *mapping, |
853 | struct page *newpage, struct page *page, enum migrate_mode mode) |
854 | { |
855 | if (PageDirty(page)) { |
856 | /* Only writeback pages in full synchronous migration */ |
857 | if (mode != MIGRATE_SYNC) |
858 | return -EBUSY; |
859 | return writeout(mapping, page); |
860 | } |
861 | |
862 | /* |
863 | * Buffers may be managed in a filesystem specific way. |
864 | * We must have no buffers or drop them. |
865 | */ |
866 | if (page_has_private(page) && |
867 | !try_to_release_page(page, GFP_KERNEL)) |
868 | return -EAGAIN; |
869 | |
870 | return migrate_page(mapping, newpage, page, mode); |
871 | } |
872 | |
873 | /* |
874 | * Move a page to a newly allocated page |
875 | * The page is locked and all ptes have been successfully removed. |
876 | * |
877 | * The new page will have replaced the old page if this function |
878 | * is successful. |
879 | * |
880 | * Return value: |
881 | * < 0 - error code |
882 | * MIGRATEPAGE_SUCCESS - success |
883 | */ |
884 | static int move_to_new_page(struct page *newpage, struct page *page, |
885 | enum migrate_mode mode) |
886 | { |
887 | struct address_space *mapping; |
888 | int rc = -EAGAIN; |
889 | bool is_lru = !__PageMovable(page); |
890 | |
891 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
892 | VM_BUG_ON_PAGE(!PageLocked(newpage), newpage); |
893 | |
894 | mapping = page_mapping(page); |
895 | |
896 | if (likely(is_lru)) { |
897 | if (!mapping) |
898 | rc = migrate_page(mapping, newpage, page, mode); |
899 | else if (mapping->a_ops->migratepage) |
900 | /* |
901 | * Most pages have a mapping and most filesystems |
902 | * provide a migratepage callback. Anonymous pages |
903 | * are part of swap space which also has its own |
904 | * migratepage callback. This is the most common path |
905 | * for page migration. |
906 | */ |
907 | rc = mapping->a_ops->migratepage(mapping, newpage, |
908 | page, mode); |
909 | else |
910 | rc = fallback_migrate_page(mapping, newpage, |
911 | page, mode); |
912 | } else { |
913 | /* |
914 | * In case of non-lru page, it could be released after |
915 | * isolation step. In that case, we shouldn't try migration. |
916 | */ |
917 | VM_BUG_ON_PAGE(!PageIsolated(page), page); |
918 | if (!PageMovable(page)) { |
919 | rc = MIGRATEPAGE_SUCCESS; |
920 | __ClearPageIsolated(page); |
921 | goto out; |
922 | } |
923 | |
924 | rc = mapping->a_ops->migratepage(mapping, newpage, |
925 | page, mode); |
926 | WARN_ON_ONCE(rc == MIGRATEPAGE_SUCCESS && |
927 | !PageIsolated(page)); |
928 | } |
929 | |
930 | /* |
931 | * When successful, old pagecache page->mapping must be cleared before |
932 | * page is freed; but stats require that PageAnon be left as PageAnon. |
933 | */ |
934 | if (rc == MIGRATEPAGE_SUCCESS) { |
935 | if (__PageMovable(page)) { |
936 | VM_BUG_ON_PAGE(!PageIsolated(page), page); |
937 | |
938 | /* |
939 | * We clear PG_movable under page_lock so any compactor |
940 | * cannot try to migrate this page. |
941 | */ |
942 | __ClearPageIsolated(page); |
943 | } |
944 | |
945 | /* |
946 | * Anonymous and movable page->mapping will be cleard by |
947 | * free_pages_prepare so don't reset it here for keeping |
948 | * the type to work PageAnon, for example. |
949 | */ |
950 | if (!PageMappingFlags(page)) |
951 | page->mapping = NULL; |
952 | } |
953 | out: |
954 | return rc; |
955 | } |
956 | |
957 | static int __unmap_and_move(struct page *page, struct page *newpage, |
958 | int force, enum migrate_mode mode) |
959 | { |
960 | int rc = -EAGAIN; |
961 | int page_was_mapped = 0; |
962 | struct anon_vma *anon_vma = NULL; |
963 | bool is_lru = !__PageMovable(page); |
964 | |
965 | if (!trylock_page(page)) { |
966 | if (!force || mode == MIGRATE_ASYNC) |
967 | goto out; |
968 | |
969 | /* |
970 | * It's not safe for direct compaction to call lock_page. |
971 | * For example, during page readahead pages are added locked |
972 | * to the LRU. Later, when the IO completes the pages are |
973 | * marked uptodate and unlocked. However, the queueing |
974 | * could be merging multiple pages for one bio (e.g. |
975 | * mpage_readpages). If an allocation happens for the |
976 | * second or third page, the process can end up locking |
977 | * the same page twice and deadlocking. Rather than |
978 | * trying to be clever about what pages can be locked, |
979 | * avoid the use of lock_page for direct compaction |
980 | * altogether. |
981 | */ |
982 | if (current->flags & PF_MEMALLOC) |
983 | goto out; |
984 | |
985 | lock_page(page); |
986 | } |
987 | |
988 | if (PageWriteback(page)) { |
989 | /* |
990 | * Only in the case of a full synchronous migration is it |
991 | * necessary to wait for PageWriteback. In the async case, |
992 | * the retry loop is too short and in the sync-light case, |
993 | * the overhead of stalling is too much |
994 | */ |
995 | if (mode != MIGRATE_SYNC) { |
996 | rc = -EBUSY; |
997 | goto out_unlock; |
998 | } |
999 | if (!force) |
1000 | goto out_unlock; |
1001 | wait_on_page_writeback(page); |
1002 | } |
1003 | |
1004 | /* |
1005 | * By try_to_unmap(), page->mapcount goes down to 0 here. In this case, |
1006 | * we cannot notice that anon_vma is freed while we migrates a page. |
1007 | * This get_anon_vma() delays freeing anon_vma pointer until the end |
1008 | * of migration. File cache pages are no problem because of page_lock() |
1009 | * File Caches may use write_page() or lock_page() in migration, then, |
1010 | * just care Anon page here. |
1011 | * |
1012 | * Only page_get_anon_vma() understands the subtleties of |
1013 | * getting a hold on an anon_vma from outside one of its mms. |
1014 | * But if we cannot get anon_vma, then we won't need it anyway, |
1015 | * because that implies that the anon page is no longer mapped |
1016 | * (and cannot be remapped so long as we hold the page lock). |
1017 | */ |
1018 | if (PageAnon(page) && !PageKsm(page)) |
1019 | anon_vma = page_get_anon_vma(page); |
1020 | |
1021 | /* |
1022 | * Block others from accessing the new page when we get around to |
1023 | * establishing additional references. We are usually the only one |
1024 | * holding a reference to newpage at this point. We used to have a BUG |
1025 | * here if trylock_page(newpage) fails, but would like to allow for |
1026 | * cases where there might be a race with the previous use of newpage. |
1027 | * This is much like races on refcount of oldpage: just don't BUG(). |
1028 | */ |
1029 | if (unlikely(!trylock_page(newpage))) |
1030 | goto out_unlock; |
1031 | |
1032 | if (unlikely(!is_lru)) { |
1033 | rc = move_to_new_page(newpage, page, mode); |
1034 | goto out_unlock_both; |
1035 | } |
1036 | |
1037 | /* |
1038 | * Corner case handling: |
1039 | * 1. When a new swap-cache page is read into, it is added to the LRU |
1040 | * and treated as swapcache but it has no rmap yet. |
1041 | * Calling try_to_unmap() against a page->mapping==NULL page will |
1042 | * trigger a BUG. So handle it here. |
1043 | * 2. An orphaned page (see truncate_complete_page) might have |
1044 | * fs-private metadata. The page can be picked up due to memory |
1045 | * offlining. Everywhere else except page reclaim, the page is |
1046 | * invisible to the vm, so the page can not be migrated. So try to |
1047 | * free the metadata, so the page can be freed. |
1048 | */ |
1049 | if (!page->mapping) { |
1050 | VM_BUG_ON_PAGE(PageAnon(page), page); |
1051 | if (page_has_private(page)) { |
1052 | try_to_free_buffers(page); |
1053 | goto out_unlock_both; |
1054 | } |
1055 | } else if (page_mapped(page)) { |
1056 | #ifdef CONFIG_AMLOGIC_CMA |
1057 | int ret; |
1058 | |
1059 | ret = try_to_unmap(page, |
1060 | TTU_MIGRATION | TTU_IGNORE_MLOCK | |
1061 | TTU_IGNORE_ACCESS); |
1062 | if (ret != SWAP_SUCCESS) |
1063 | cma_debug(2, page, " unmap failed:%d\n", ret); |
1064 | #else |
1065 | /* Establish migration ptes */ |
1066 | VM_BUG_ON_PAGE(PageAnon(page) && !PageKsm(page) && !anon_vma, |
1067 | page); |
1068 | try_to_unmap(page, |
1069 | TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS); |
1070 | #endif |
1071 | page_was_mapped = 1; |
1072 | } |
1073 | |
1074 | if (!page_mapped(page)) |
1075 | rc = move_to_new_page(newpage, page, mode); |
1076 | |
1077 | if (page_was_mapped) |
1078 | remove_migration_ptes(page, |
1079 | rc == MIGRATEPAGE_SUCCESS ? newpage : page, false); |
1080 | |
1081 | out_unlock_both: |
1082 | unlock_page(newpage); |
1083 | out_unlock: |
1084 | /* Drop an anon_vma reference if we took one */ |
1085 | if (anon_vma) |
1086 | put_anon_vma(anon_vma); |
1087 | unlock_page(page); |
1088 | out: |
1089 | /* |
1090 | * If migration is successful, decrease refcount of the newpage |
1091 | * which will not free the page because new page owner increased |
1092 | * refcounter. As well, if it is LRU page, add the page to LRU |
1093 | * list in here. Use the old state of the isolated source page to |
1094 | * determine if we migrated a LRU page. newpage was already unlocked |
1095 | * and possibly modified by its owner - don't rely on the page |
1096 | * state. |
1097 | */ |
1098 | if (rc == MIGRATEPAGE_SUCCESS) { |
1099 | if (unlikely(!is_lru)) |
1100 | put_page(newpage); |
1101 | else |
1102 | putback_lru_page(newpage); |
1103 | } |
1104 | |
1105 | return rc; |
1106 | } |
1107 | |
1108 | /* |
1109 | * gcc 4.7 and 4.8 on arm get an ICEs when inlining unmap_and_move(). Work |
1110 | * around it. |
1111 | */ |
1112 | #if (GCC_VERSION >= 40700 && GCC_VERSION < 40900) && defined(CONFIG_ARM) |
1113 | #define ICE_noinline noinline |
1114 | #else |
1115 | #define ICE_noinline |
1116 | #endif |
1117 | |
1118 | /* |
1119 | * Obtain the lock on page, remove all ptes and migrate the page |
1120 | * to the newly allocated page in newpage. |
1121 | */ |
1122 | static ICE_noinline int unmap_and_move(new_page_t get_new_page, |
1123 | free_page_t put_new_page, |
1124 | unsigned long private, struct page *page, |
1125 | int force, enum migrate_mode mode, |
1126 | enum migrate_reason reason) |
1127 | { |
1128 | int rc = MIGRATEPAGE_SUCCESS; |
1129 | int *result = NULL; |
1130 | struct page *newpage; |
1131 | |
1132 | newpage = get_new_page(page, private, &result); |
1133 | if (!newpage) |
1134 | return -ENOMEM; |
1135 | |
1136 | if (page_count(page) == 1) { |
1137 | /* page was freed from under us. So we are done. */ |
1138 | ClearPageActive(page); |
1139 | ClearPageUnevictable(page); |
1140 | if (unlikely(__PageMovable(page))) { |
1141 | lock_page(page); |
1142 | if (!PageMovable(page)) |
1143 | __ClearPageIsolated(page); |
1144 | unlock_page(page); |
1145 | } |
1146 | if (put_new_page) |
1147 | put_new_page(newpage, private); |
1148 | else |
1149 | put_page(newpage); |
1150 | goto out; |
1151 | } |
1152 | |
1153 | if (unlikely(PageTransHuge(page))) { |
1154 | lock_page(page); |
1155 | rc = split_huge_page(page); |
1156 | unlock_page(page); |
1157 | if (rc) |
1158 | goto out; |
1159 | } |
1160 | |
1161 | rc = __unmap_and_move(page, newpage, force, mode); |
1162 | if (rc == MIGRATEPAGE_SUCCESS) |
1163 | set_page_owner_migrate_reason(newpage, reason); |
1164 | |
1165 | out: |
1166 | if (rc != -EAGAIN) { |
1167 | /* |
1168 | * A page that has been migrated has all references |
1169 | * removed and will be freed. A page that has not been |
1170 | * migrated will have kepts its references and be |
1171 | * restored. |
1172 | */ |
1173 | list_del(&page->lru); |
1174 | |
1175 | /* |
1176 | * Compaction can migrate also non-LRU pages which are |
1177 | * not accounted to NR_ISOLATED_*. They can be recognized |
1178 | * as __PageMovable |
1179 | */ |
1180 | if (likely(!__PageMovable(page))) |
1181 | dec_node_page_state(page, NR_ISOLATED_ANON + |
1182 | page_is_file_cache(page)); |
1183 | } |
1184 | |
1185 | /* |
1186 | * If migration is successful, releases reference grabbed during |
1187 | * isolation. Otherwise, restore the page to right list unless |
1188 | * we want to retry. |
1189 | */ |
1190 | if (rc == MIGRATEPAGE_SUCCESS) { |
1191 | put_page(page); |
1192 | if (reason == MR_MEMORY_FAILURE) { |
1193 | /* |
1194 | * Set PG_HWPoison on just freed page |
1195 | * intentionally. Although it's rather weird, |
1196 | * it's how HWPoison flag works at the moment. |
1197 | */ |
1198 | if (!test_set_page_hwpoison(page)) |
1199 | num_poisoned_pages_inc(); |
1200 | } |
1201 | } else { |
1202 | if (rc != -EAGAIN) { |
1203 | if (likely(!__PageMovable(page))) { |
1204 | putback_lru_page(page); |
1205 | goto put_new; |
1206 | } |
1207 | |
1208 | lock_page(page); |
1209 | if (PageMovable(page)) |
1210 | putback_movable_page(page); |
1211 | else |
1212 | __ClearPageIsolated(page); |
1213 | unlock_page(page); |
1214 | put_page(page); |
1215 | } |
1216 | put_new: |
1217 | if (put_new_page) |
1218 | put_new_page(newpage, private); |
1219 | else |
1220 | put_page(newpage); |
1221 | } |
1222 | |
1223 | if (result) { |
1224 | if (rc) |
1225 | *result = rc; |
1226 | else |
1227 | *result = page_to_nid(newpage); |
1228 | } |
1229 | #ifdef CONFIG_AMLOGIC_CMA |
1230 | if (reason == MR_CMA && rc == MIGRATEPAGE_SUCCESS) |
1231 | ClearPageCmaAllocating(page); |
1232 | #endif |
1233 | return rc; |
1234 | } |
1235 | |
1236 | /* |
1237 | * Counterpart of unmap_and_move_page() for hugepage migration. |
1238 | * |
1239 | * This function doesn't wait the completion of hugepage I/O |
1240 | * because there is no race between I/O and migration for hugepage. |
1241 | * Note that currently hugepage I/O occurs only in direct I/O |
1242 | * where no lock is held and PG_writeback is irrelevant, |
1243 | * and writeback status of all subpages are counted in the reference |
1244 | * count of the head page (i.e. if all subpages of a 2MB hugepage are |
1245 | * under direct I/O, the reference of the head page is 512 and a bit more.) |
1246 | * This means that when we try to migrate hugepage whose subpages are |
1247 | * doing direct I/O, some references remain after try_to_unmap() and |
1248 | * hugepage migration fails without data corruption. |
1249 | * |
1250 | * There is also no race when direct I/O is issued on the page under migration, |
1251 | * because then pte is replaced with migration swap entry and direct I/O code |
1252 | * will wait in the page fault for migration to complete. |
1253 | */ |
1254 | static int unmap_and_move_huge_page(new_page_t get_new_page, |
1255 | free_page_t put_new_page, unsigned long private, |
1256 | struct page *hpage, int force, |
1257 | enum migrate_mode mode, int reason) |
1258 | { |
1259 | int rc = -EAGAIN; |
1260 | int *result = NULL; |
1261 | int page_was_mapped = 0; |
1262 | struct page *new_hpage; |
1263 | struct anon_vma *anon_vma = NULL; |
1264 | |
1265 | /* |
1266 | * Movability of hugepages depends on architectures and hugepage size. |
1267 | * This check is necessary because some callers of hugepage migration |
1268 | * like soft offline and memory hotremove don't walk through page |
1269 | * tables or check whether the hugepage is pmd-based or not before |
1270 | * kicking migration. |
1271 | */ |
1272 | if (!hugepage_migration_supported(page_hstate(hpage))) { |
1273 | putback_active_hugepage(hpage); |
1274 | return -ENOSYS; |
1275 | } |
1276 | |
1277 | new_hpage = get_new_page(hpage, private, &result); |
1278 | if (!new_hpage) |
1279 | return -ENOMEM; |
1280 | |
1281 | if (!trylock_page(hpage)) { |
1282 | if (!force || mode != MIGRATE_SYNC) |
1283 | goto out; |
1284 | lock_page(hpage); |
1285 | } |
1286 | |
1287 | /* |
1288 | * Check for pages which are in the process of being freed. Without |
1289 | * page_mapping() set, hugetlbfs specific move page routine will not |
1290 | * be called and we could leak usage counts for subpools. |
1291 | */ |
1292 | if (page_private(hpage) && !page_mapping(hpage)) { |
1293 | rc = -EBUSY; |
1294 | goto out_unlock; |
1295 | } |
1296 | |
1297 | if (PageAnon(hpage)) |
1298 | anon_vma = page_get_anon_vma(hpage); |
1299 | |
1300 | if (unlikely(!trylock_page(new_hpage))) |
1301 | goto put_anon; |
1302 | |
1303 | if (page_mapped(hpage)) { |
1304 | try_to_unmap(hpage, |
1305 | TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS); |
1306 | page_was_mapped = 1; |
1307 | } |
1308 | |
1309 | if (!page_mapped(hpage)) |
1310 | rc = move_to_new_page(new_hpage, hpage, mode); |
1311 | |
1312 | if (page_was_mapped) |
1313 | remove_migration_ptes(hpage, |
1314 | rc == MIGRATEPAGE_SUCCESS ? new_hpage : hpage, false); |
1315 | |
1316 | unlock_page(new_hpage); |
1317 | |
1318 | put_anon: |
1319 | if (anon_vma) |
1320 | put_anon_vma(anon_vma); |
1321 | |
1322 | if (rc == MIGRATEPAGE_SUCCESS) { |
1323 | hugetlb_cgroup_migrate(hpage, new_hpage); |
1324 | put_new_page = NULL; |
1325 | set_page_owner_migrate_reason(new_hpage, reason); |
1326 | } |
1327 | |
1328 | out_unlock: |
1329 | unlock_page(hpage); |
1330 | out: |
1331 | if (rc != -EAGAIN) |
1332 | putback_active_hugepage(hpage); |
1333 | |
1334 | /* |
1335 | * If migration was not successful and there's a freeing callback, use |
1336 | * it. Otherwise, put_page() will drop the reference grabbed during |
1337 | * isolation. |
1338 | */ |
1339 | if (put_new_page) |
1340 | put_new_page(new_hpage, private); |
1341 | else |
1342 | putback_active_hugepage(new_hpage); |
1343 | |
1344 | if (result) { |
1345 | if (rc) |
1346 | *result = rc; |
1347 | else |
1348 | *result = page_to_nid(new_hpage); |
1349 | } |
1350 | return rc; |
1351 | } |
1352 | |
1353 | /* |
1354 | * migrate_pages - migrate the pages specified in a list, to the free pages |
1355 | * supplied as the target for the page migration |
1356 | * |
1357 | * @from: The list of pages to be migrated. |
1358 | * @get_new_page: The function used to allocate free pages to be used |
1359 | * as the target of the page migration. |
1360 | * @put_new_page: The function used to free target pages if migration |
1361 | * fails, or NULL if no special handling is necessary. |
1362 | * @private: Private data to be passed on to get_new_page() |
1363 | * @mode: The migration mode that specifies the constraints for |
1364 | * page migration, if any. |
1365 | * @reason: The reason for page migration. |
1366 | * |
1367 | * The function returns after 10 attempts or if no pages are movable any more |
1368 | * because the list has become empty or no retryable pages exist any more. |
1369 | * The caller should call putback_movable_pages() to return pages to the LRU |
1370 | * or free list only if ret != 0. |
1371 | * |
1372 | * Returns the number of pages that were not migrated, or an error code. |
1373 | */ |
1374 | int migrate_pages(struct list_head *from, new_page_t get_new_page, |
1375 | free_page_t put_new_page, unsigned long private, |
1376 | enum migrate_mode mode, int reason) |
1377 | { |
1378 | int retry = 1; |
1379 | int nr_failed = 0; |
1380 | int nr_succeeded = 0; |
1381 | int pass = 0; |
1382 | struct page *page; |
1383 | struct page *page2; |
1384 | int swapwrite = current->flags & PF_SWAPWRITE; |
1385 | int rc; |
1386 | |
1387 | if (!swapwrite) |
1388 | current->flags |= PF_SWAPWRITE; |
1389 | |
1390 | for(pass = 0; pass < 10 && retry; pass++) { |
1391 | retry = 0; |
1392 | |
1393 | list_for_each_entry_safe(page, page2, from, lru) { |
1394 | cond_resched(); |
1395 | |
1396 | if (PageHuge(page)) |
1397 | rc = unmap_and_move_huge_page(get_new_page, |
1398 | put_new_page, private, page, |
1399 | pass > 2, mode, reason); |
1400 | else |
1401 | rc = unmap_and_move(get_new_page, put_new_page, |
1402 | private, page, pass > 2, mode, |
1403 | reason); |
1404 | |
1405 | switch(rc) { |
1406 | case -ENOMEM: |
1407 | nr_failed++; |
1408 | #ifdef CONFIG_AMLOGIC_CMA |
1409 | cma_debug(2, page, " NO MEM\n"); |
1410 | #endif |
1411 | goto out; |
1412 | case -EAGAIN: |
1413 | retry++; |
1414 | break; |
1415 | case MIGRATEPAGE_SUCCESS: |
1416 | nr_succeeded++; |
1417 | break; |
1418 | default: |
1419 | /* |
1420 | * Permanent failure (-EBUSY, -ENOSYS, etc.): |
1421 | * unlike -EAGAIN case, the failed page is |
1422 | * removed from migration page list and not |
1423 | * retried in the next outer loop. |
1424 | */ |
1425 | nr_failed++; |
1426 | #ifdef CONFIG_AMLOGIC_CMA |
1427 | cma_debug(2, page, " failed:%d\n", rc); |
1428 | #endif |
1429 | break; |
1430 | } |
1431 | } |
1432 | } |
1433 | nr_failed += retry; |
1434 | rc = nr_failed; |
1435 | out: |
1436 | if (nr_succeeded) |
1437 | count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded); |
1438 | if (nr_failed) |
1439 | count_vm_events(PGMIGRATE_FAIL, nr_failed); |
1440 | trace_mm_migrate_pages(nr_succeeded, nr_failed, mode, reason); |
1441 | |
1442 | if (!swapwrite) |
1443 | current->flags &= ~PF_SWAPWRITE; |
1444 | |
1445 | return rc; |
1446 | } |
1447 | |
1448 | #ifdef CONFIG_NUMA |
1449 | /* |
1450 | * Move a list of individual pages |
1451 | */ |
1452 | struct page_to_node { |
1453 | unsigned long addr; |
1454 | struct page *page; |
1455 | int node; |
1456 | int status; |
1457 | }; |
1458 | |
1459 | static struct page *new_page_node(struct page *p, unsigned long private, |
1460 | int **result) |
1461 | { |
1462 | struct page_to_node *pm = (struct page_to_node *)private; |
1463 | |
1464 | while (pm->node != MAX_NUMNODES && pm->page != p) |
1465 | pm++; |
1466 | |
1467 | if (pm->node == MAX_NUMNODES) |
1468 | return NULL; |
1469 | |
1470 | *result = &pm->status; |
1471 | |
1472 | if (PageHuge(p)) |
1473 | return alloc_huge_page_node(page_hstate(compound_head(p)), |
1474 | pm->node); |
1475 | else |
1476 | return __alloc_pages_node(pm->node, |
1477 | GFP_HIGHUSER_MOVABLE | __GFP_THISNODE, 0); |
1478 | } |
1479 | |
1480 | /* |
1481 | * Move a set of pages as indicated in the pm array. The addr |
1482 | * field must be set to the virtual address of the page to be moved |
1483 | * and the node number must contain a valid target node. |
1484 | * The pm array ends with node = MAX_NUMNODES. |
1485 | */ |
1486 | static int do_move_page_to_node_array(struct mm_struct *mm, |
1487 | struct page_to_node *pm, |
1488 | int migrate_all) |
1489 | { |
1490 | int err; |
1491 | struct page_to_node *pp; |
1492 | LIST_HEAD(pagelist); |
1493 | |
1494 | down_read(&mm->mmap_sem); |
1495 | |
1496 | /* |
1497 | * Build a list of pages to migrate |
1498 | */ |
1499 | for (pp = pm; pp->node != MAX_NUMNODES; pp++) { |
1500 | struct vm_area_struct *vma; |
1501 | struct page *page; |
1502 | |
1503 | err = -EFAULT; |
1504 | vma = find_vma(mm, pp->addr); |
1505 | if (!vma || pp->addr < vma->vm_start || !vma_migratable(vma)) |
1506 | goto set_status; |
1507 | |
1508 | /* FOLL_DUMP to ignore special (like zero) pages */ |
1509 | page = follow_page(vma, pp->addr, |
1510 | FOLL_GET | FOLL_SPLIT | FOLL_DUMP); |
1511 | |
1512 | err = PTR_ERR(page); |
1513 | if (IS_ERR(page)) |
1514 | goto set_status; |
1515 | |
1516 | err = -ENOENT; |
1517 | if (!page) |
1518 | goto set_status; |
1519 | |
1520 | pp->page = page; |
1521 | err = page_to_nid(page); |
1522 | |
1523 | if (err == pp->node) |
1524 | /* |
1525 | * Node already in the right place |
1526 | */ |
1527 | goto put_and_set; |
1528 | |
1529 | err = -EACCES; |
1530 | if (page_mapcount(page) > 1 && |
1531 | !migrate_all) |
1532 | goto put_and_set; |
1533 | |
1534 | if (PageHuge(page)) { |
1535 | if (PageHead(page)) |
1536 | isolate_huge_page(page, &pagelist); |
1537 | goto put_and_set; |
1538 | } |
1539 | |
1540 | err = isolate_lru_page(page); |
1541 | if (!err) { |
1542 | list_add_tail(&page->lru, &pagelist); |
1543 | inc_node_page_state(page, NR_ISOLATED_ANON + |
1544 | page_is_file_cache(page)); |
1545 | } |
1546 | put_and_set: |
1547 | /* |
1548 | * Either remove the duplicate refcount from |
1549 | * isolate_lru_page() or drop the page ref if it was |
1550 | * not isolated. |
1551 | */ |
1552 | put_page(page); |
1553 | set_status: |
1554 | pp->status = err; |
1555 | } |
1556 | |
1557 | err = 0; |
1558 | if (!list_empty(&pagelist)) { |
1559 | err = migrate_pages(&pagelist, new_page_node, NULL, |
1560 | (unsigned long)pm, MIGRATE_SYNC, MR_SYSCALL); |
1561 | if (err) |
1562 | putback_movable_pages(&pagelist); |
1563 | } |
1564 | |
1565 | up_read(&mm->mmap_sem); |
1566 | return err; |
1567 | } |
1568 | |
1569 | /* |
1570 | * Migrate an array of page address onto an array of nodes and fill |
1571 | * the corresponding array of status. |
1572 | */ |
1573 | static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes, |
1574 | unsigned long nr_pages, |
1575 | const void __user * __user *pages, |
1576 | const int __user *nodes, |
1577 | int __user *status, int flags) |
1578 | { |
1579 | struct page_to_node *pm; |
1580 | unsigned long chunk_nr_pages; |
1581 | unsigned long chunk_start; |
1582 | int err; |
1583 | |
1584 | err = -ENOMEM; |
1585 | pm = (struct page_to_node *)__get_free_page(GFP_KERNEL); |
1586 | if (!pm) |
1587 | goto out; |
1588 | |
1589 | migrate_prep(); |
1590 | |
1591 | /* |
1592 | * Store a chunk of page_to_node array in a page, |
1593 | * but keep the last one as a marker |
1594 | */ |
1595 | chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1; |
1596 | |
1597 | for (chunk_start = 0; |
1598 | chunk_start < nr_pages; |
1599 | chunk_start += chunk_nr_pages) { |
1600 | int j; |
1601 | |
1602 | if (chunk_start + chunk_nr_pages > nr_pages) |
1603 | chunk_nr_pages = nr_pages - chunk_start; |
1604 | |
1605 | /* fill the chunk pm with addrs and nodes from user-space */ |
1606 | for (j = 0; j < chunk_nr_pages; j++) { |
1607 | const void __user *p; |
1608 | int node; |
1609 | |
1610 | err = -EFAULT; |
1611 | if (get_user(p, pages + j + chunk_start)) |
1612 | goto out_pm; |
1613 | pm[j].addr = (unsigned long) p; |
1614 | |
1615 | if (get_user(node, nodes + j + chunk_start)) |
1616 | goto out_pm; |
1617 | |
1618 | err = -ENODEV; |
1619 | if (node < 0 || node >= MAX_NUMNODES) |
1620 | goto out_pm; |
1621 | |
1622 | if (!node_state(node, N_MEMORY)) |
1623 | goto out_pm; |
1624 | |
1625 | err = -EACCES; |
1626 | if (!node_isset(node, task_nodes)) |
1627 | goto out_pm; |
1628 | |
1629 | pm[j].node = node; |
1630 | } |
1631 | |
1632 | /* End marker for this chunk */ |
1633 | pm[chunk_nr_pages].node = MAX_NUMNODES; |
1634 | |
1635 | /* Migrate this chunk */ |
1636 | err = do_move_page_to_node_array(mm, pm, |
1637 | flags & MPOL_MF_MOVE_ALL); |
1638 | if (err < 0) |
1639 | goto out_pm; |
1640 | |
1641 | /* Return status information */ |
1642 | for (j = 0; j < chunk_nr_pages; j++) |
1643 | if (put_user(pm[j].status, status + j + chunk_start)) { |
1644 | err = -EFAULT; |
1645 | goto out_pm; |
1646 | } |
1647 | } |
1648 | err = 0; |
1649 | |
1650 | out_pm: |
1651 | free_page((unsigned long)pm); |
1652 | out: |
1653 | return err; |
1654 | } |
1655 | |
1656 | /* |
1657 | * Determine the nodes of an array of pages and store it in an array of status. |
1658 | */ |
1659 | static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages, |
1660 | const void __user **pages, int *status) |
1661 | { |
1662 | unsigned long i; |
1663 | |
1664 | down_read(&mm->mmap_sem); |
1665 | |
1666 | for (i = 0; i < nr_pages; i++) { |
1667 | unsigned long addr = (unsigned long)(*pages); |
1668 | struct vm_area_struct *vma; |
1669 | struct page *page; |
1670 | int err = -EFAULT; |
1671 | |
1672 | vma = find_vma(mm, addr); |
1673 | if (!vma || addr < vma->vm_start) |
1674 | goto set_status; |
1675 | |
1676 | /* FOLL_DUMP to ignore special (like zero) pages */ |
1677 | page = follow_page(vma, addr, FOLL_DUMP); |
1678 | |
1679 | err = PTR_ERR(page); |
1680 | if (IS_ERR(page)) |
1681 | goto set_status; |
1682 | |
1683 | err = page ? page_to_nid(page) : -ENOENT; |
1684 | set_status: |
1685 | *status = err; |
1686 | |
1687 | pages++; |
1688 | status++; |
1689 | } |
1690 | |
1691 | up_read(&mm->mmap_sem); |
1692 | } |
1693 | |
1694 | /* |
1695 | * Determine the nodes of a user array of pages and store it in |
1696 | * a user array of status. |
1697 | */ |
1698 | static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages, |
1699 | const void __user * __user *pages, |
1700 | int __user *status) |
1701 | { |
1702 | #define DO_PAGES_STAT_CHUNK_NR 16 |
1703 | const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR]; |
1704 | int chunk_status[DO_PAGES_STAT_CHUNK_NR]; |
1705 | |
1706 | while (nr_pages) { |
1707 | unsigned long chunk_nr; |
1708 | |
1709 | chunk_nr = nr_pages; |
1710 | if (chunk_nr > DO_PAGES_STAT_CHUNK_NR) |
1711 | chunk_nr = DO_PAGES_STAT_CHUNK_NR; |
1712 | |
1713 | if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages))) |
1714 | break; |
1715 | |
1716 | do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status); |
1717 | |
1718 | if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status))) |
1719 | break; |
1720 | |
1721 | pages += chunk_nr; |
1722 | status += chunk_nr; |
1723 | nr_pages -= chunk_nr; |
1724 | } |
1725 | return nr_pages ? -EFAULT : 0; |
1726 | } |
1727 | |
1728 | /* |
1729 | * Move a list of pages in the address space of the currently executing |
1730 | * process. |
1731 | */ |
1732 | SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages, |
1733 | const void __user * __user *, pages, |
1734 | const int __user *, nodes, |
1735 | int __user *, status, int, flags) |
1736 | { |
1737 | struct task_struct *task; |
1738 | struct mm_struct *mm; |
1739 | int err; |
1740 | nodemask_t task_nodes; |
1741 | |
1742 | /* Check flags */ |
1743 | if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL)) |
1744 | return -EINVAL; |
1745 | |
1746 | if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE)) |
1747 | return -EPERM; |
1748 | |
1749 | /* Find the mm_struct */ |
1750 | rcu_read_lock(); |
1751 | task = pid ? find_task_by_vpid(pid) : current; |
1752 | if (!task) { |
1753 | rcu_read_unlock(); |
1754 | return -ESRCH; |
1755 | } |
1756 | get_task_struct(task); |
1757 | |
1758 | /* |
1759 | * Check if this process has the right to modify the specified |
1760 | * process. Use the regular "ptrace_may_access()" checks. |
1761 | */ |
1762 | if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) { |
1763 | rcu_read_unlock(); |
1764 | err = -EPERM; |
1765 | goto out; |
1766 | } |
1767 | rcu_read_unlock(); |
1768 | |
1769 | err = security_task_movememory(task); |
1770 | if (err) |
1771 | goto out; |
1772 | |
1773 | task_nodes = cpuset_mems_allowed(task); |
1774 | mm = get_task_mm(task); |
1775 | put_task_struct(task); |
1776 | |
1777 | if (!mm) |
1778 | return -EINVAL; |
1779 | |
1780 | if (nodes) |
1781 | err = do_pages_move(mm, task_nodes, nr_pages, pages, |
1782 | nodes, status, flags); |
1783 | else |
1784 | err = do_pages_stat(mm, nr_pages, pages, status); |
1785 | |
1786 | mmput(mm); |
1787 | return err; |
1788 | |
1789 | out: |
1790 | put_task_struct(task); |
1791 | return err; |
1792 | } |
1793 | |
1794 | #ifdef CONFIG_NUMA_BALANCING |
1795 | /* |
1796 | * Returns true if this is a safe migration target node for misplaced NUMA |
1797 | * pages. Currently it only checks the watermarks which crude |
1798 | */ |
1799 | static bool migrate_balanced_pgdat(struct pglist_data *pgdat, |
1800 | unsigned long nr_migrate_pages) |
1801 | { |
1802 | int z; |
1803 | |
1804 | if (!pgdat_reclaimable(pgdat)) |
1805 | return false; |
1806 | |
1807 | for (z = pgdat->nr_zones - 1; z >= 0; z--) { |
1808 | struct zone *zone = pgdat->node_zones + z; |
1809 | |
1810 | if (!populated_zone(zone)) |
1811 | continue; |
1812 | |
1813 | /* Avoid waking kswapd by allocating pages_to_migrate pages. */ |
1814 | if (!zone_watermark_ok(zone, 0, |
1815 | high_wmark_pages(zone) + |
1816 | nr_migrate_pages, |
1817 | 0, 0)) |
1818 | continue; |
1819 | return true; |
1820 | } |
1821 | return false; |
1822 | } |
1823 | |
1824 | static struct page *alloc_misplaced_dst_page(struct page *page, |
1825 | unsigned long data, |
1826 | int **result) |
1827 | { |
1828 | int nid = (int) data; |
1829 | struct page *newpage; |
1830 | |
1831 | newpage = __alloc_pages_node(nid, |
1832 | (GFP_HIGHUSER_MOVABLE | |
1833 | __GFP_THISNODE | __GFP_NOMEMALLOC | |
1834 | __GFP_NORETRY | __GFP_NOWARN) & |
1835 | ~__GFP_RECLAIM, 0); |
1836 | |
1837 | return newpage; |
1838 | } |
1839 | |
1840 | /* |
1841 | * page migration rate limiting control. |
1842 | * Do not migrate more than @pages_to_migrate in a @migrate_interval_millisecs |
1843 | * window of time. Default here says do not migrate more than 1280M per second. |
1844 | */ |
1845 | static unsigned int migrate_interval_millisecs __read_mostly = 100; |
1846 | static unsigned int ratelimit_pages __read_mostly = 128 << (20 - PAGE_SHIFT); |
1847 | |
1848 | /* Returns true if the node is migrate rate-limited after the update */ |
1849 | static bool numamigrate_update_ratelimit(pg_data_t *pgdat, |
1850 | unsigned long nr_pages) |
1851 | { |
1852 | /* |
1853 | * Rate-limit the amount of data that is being migrated to a node. |
1854 | * Optimal placement is no good if the memory bus is saturated and |
1855 | * all the time is being spent migrating! |
1856 | */ |
1857 | if (time_after(jiffies, pgdat->numabalancing_migrate_next_window)) { |
1858 | spin_lock(&pgdat->numabalancing_migrate_lock); |
1859 | pgdat->numabalancing_migrate_nr_pages = 0; |
1860 | pgdat->numabalancing_migrate_next_window = jiffies + |
1861 | msecs_to_jiffies(migrate_interval_millisecs); |
1862 | spin_unlock(&pgdat->numabalancing_migrate_lock); |
1863 | } |
1864 | if (pgdat->numabalancing_migrate_nr_pages > ratelimit_pages) { |
1865 | trace_mm_numa_migrate_ratelimit(current, pgdat->node_id, |
1866 | nr_pages); |
1867 | return true; |
1868 | } |
1869 | |
1870 | /* |
1871 | * This is an unlocked non-atomic update so errors are possible. |
1872 | * The consequences are failing to migrate when we potentiall should |
1873 | * have which is not severe enough to warrant locking. If it is ever |
1874 | * a problem, it can be converted to a per-cpu counter. |
1875 | */ |
1876 | pgdat->numabalancing_migrate_nr_pages += nr_pages; |
1877 | return false; |
1878 | } |
1879 | |
1880 | static int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page) |
1881 | { |
1882 | int page_lru; |
1883 | |
1884 | VM_BUG_ON_PAGE(compound_order(page) && !PageTransHuge(page), page); |
1885 | |
1886 | /* Avoid migrating to a node that is nearly full */ |
1887 | if (!migrate_balanced_pgdat(pgdat, 1UL << compound_order(page))) |
1888 | return 0; |
1889 | |
1890 | if (isolate_lru_page(page)) |
1891 | return 0; |
1892 | |
1893 | /* |
1894 | * migrate_misplaced_transhuge_page() skips page migration's usual |
1895 | * check on page_count(), so we must do it here, now that the page |
1896 | * has been isolated: a GUP pin, or any other pin, prevents migration. |
1897 | * The expected page count is 3: 1 for page's mapcount and 1 for the |
1898 | * caller's pin and 1 for the reference taken by isolate_lru_page(). |
1899 | */ |
1900 | if (PageTransHuge(page) && page_count(page) != 3) { |
1901 | putback_lru_page(page); |
1902 | return 0; |
1903 | } |
1904 | |
1905 | page_lru = page_is_file_cache(page); |
1906 | mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + page_lru, |
1907 | hpage_nr_pages(page)); |
1908 | |
1909 | /* |
1910 | * Isolating the page has taken another reference, so the |
1911 | * caller's reference can be safely dropped without the page |
1912 | * disappearing underneath us during migration. |
1913 | */ |
1914 | put_page(page); |
1915 | return 1; |
1916 | } |
1917 | |
1918 | bool pmd_trans_migrating(pmd_t pmd) |
1919 | { |
1920 | struct page *page = pmd_page(pmd); |
1921 | return PageLocked(page); |
1922 | } |
1923 | |
1924 | /* |
1925 | * Attempt to migrate a misplaced page to the specified destination |
1926 | * node. Caller is expected to have an elevated reference count on |
1927 | * the page that will be dropped by this function before returning. |
1928 | */ |
1929 | int migrate_misplaced_page(struct page *page, struct vm_area_struct *vma, |
1930 | int node) |
1931 | { |
1932 | pg_data_t *pgdat = NODE_DATA(node); |
1933 | int isolated; |
1934 | int nr_remaining; |
1935 | LIST_HEAD(migratepages); |
1936 | |
1937 | /* |
1938 | * Don't migrate file pages that are mapped in multiple processes |
1939 | * with execute permissions as they are probably shared libraries. |
1940 | */ |
1941 | if (page_mapcount(page) != 1 && page_is_file_cache(page) && |
1942 | (vma->vm_flags & VM_EXEC)) |
1943 | goto out; |
1944 | |
1945 | /* |
1946 | * Rate-limit the amount of data that is being migrated to a node. |
1947 | * Optimal placement is no good if the memory bus is saturated and |
1948 | * all the time is being spent migrating! |
1949 | */ |
1950 | if (numamigrate_update_ratelimit(pgdat, 1)) |
1951 | goto out; |
1952 | |
1953 | isolated = numamigrate_isolate_page(pgdat, page); |
1954 | if (!isolated) |
1955 | goto out; |
1956 | |
1957 | list_add(&page->lru, &migratepages); |
1958 | nr_remaining = migrate_pages(&migratepages, alloc_misplaced_dst_page, |
1959 | NULL, node, MIGRATE_ASYNC, |
1960 | MR_NUMA_MISPLACED); |
1961 | if (nr_remaining) { |
1962 | if (!list_empty(&migratepages)) { |
1963 | list_del(&page->lru); |
1964 | dec_node_page_state(page, NR_ISOLATED_ANON + |
1965 | page_is_file_cache(page)); |
1966 | putback_lru_page(page); |
1967 | } |
1968 | isolated = 0; |
1969 | } else |
1970 | count_vm_numa_event(NUMA_PAGE_MIGRATE); |
1971 | BUG_ON(!list_empty(&migratepages)); |
1972 | return isolated; |
1973 | |
1974 | out: |
1975 | put_page(page); |
1976 | return 0; |
1977 | } |
1978 | #endif /* CONFIG_NUMA_BALANCING */ |
1979 | |
1980 | #if defined(CONFIG_NUMA_BALANCING) && defined(CONFIG_TRANSPARENT_HUGEPAGE) |
1981 | /* |
1982 | * Migrates a THP to a given target node. page must be locked and is unlocked |
1983 | * before returning. |
1984 | */ |
1985 | int migrate_misplaced_transhuge_page(struct mm_struct *mm, |
1986 | struct vm_area_struct *vma, |
1987 | pmd_t *pmd, pmd_t entry, |
1988 | unsigned long address, |
1989 | struct page *page, int node) |
1990 | { |
1991 | spinlock_t *ptl; |
1992 | pg_data_t *pgdat = NODE_DATA(node); |
1993 | int isolated = 0; |
1994 | struct page *new_page = NULL; |
1995 | int page_lru = page_is_file_cache(page); |
1996 | unsigned long mmun_start = address & HPAGE_PMD_MASK; |
1997 | unsigned long mmun_end = mmun_start + HPAGE_PMD_SIZE; |
1998 | pmd_t orig_entry; |
1999 | |
2000 | /* |
2001 | * Rate-limit the amount of data that is being migrated to a node. |
2002 | * Optimal placement is no good if the memory bus is saturated and |
2003 | * all the time is being spent migrating! |
2004 | */ |
2005 | if (numamigrate_update_ratelimit(pgdat, HPAGE_PMD_NR)) |
2006 | goto out_dropref; |
2007 | |
2008 | new_page = alloc_pages_node(node, |
2009 | (GFP_TRANSHUGE_LIGHT | __GFP_THISNODE), |
2010 | HPAGE_PMD_ORDER); |
2011 | if (!new_page) |
2012 | goto out_fail; |
2013 | prep_transhuge_page(new_page); |
2014 | |
2015 | isolated = numamigrate_isolate_page(pgdat, page); |
2016 | if (!isolated) { |
2017 | put_page(new_page); |
2018 | goto out_fail; |
2019 | } |
2020 | /* |
2021 | * We are not sure a pending tlb flush here is for a huge page |
2022 | * mapping or not. Hence use the tlb range variant |
2023 | */ |
2024 | if (mm_tlb_flush_pending(mm)) |
2025 | flush_tlb_range(vma, mmun_start, mmun_end); |
2026 | |
2027 | /* Prepare a page as a migration target */ |
2028 | __SetPageLocked(new_page); |
2029 | __SetPageSwapBacked(new_page); |
2030 | |
2031 | /* anon mapping, we can simply copy page->mapping to the new page: */ |
2032 | new_page->mapping = page->mapping; |
2033 | new_page->index = page->index; |
2034 | migrate_page_copy(new_page, page); |
2035 | WARN_ON(PageLRU(new_page)); |
2036 | |
2037 | /* Recheck the target PMD */ |
2038 | mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); |
2039 | ptl = pmd_lock(mm, pmd); |
2040 | if (unlikely(!pmd_same(*pmd, entry) || page_count(page) != 2)) { |
2041 | fail_putback: |
2042 | spin_unlock(ptl); |
2043 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); |
2044 | |
2045 | /* Reverse changes made by migrate_page_copy() */ |
2046 | if (TestClearPageActive(new_page)) |
2047 | SetPageActive(page); |
2048 | if (TestClearPageUnevictable(new_page)) |
2049 | SetPageUnevictable(page); |
2050 | |
2051 | unlock_page(new_page); |
2052 | put_page(new_page); /* Free it */ |
2053 | |
2054 | /* Retake the callers reference and putback on LRU */ |
2055 | get_page(page); |
2056 | putback_lru_page(page); |
2057 | mod_node_page_state(page_pgdat(page), |
2058 | NR_ISOLATED_ANON + page_lru, -HPAGE_PMD_NR); |
2059 | |
2060 | goto out_unlock; |
2061 | } |
2062 | |
2063 | orig_entry = *pmd; |
2064 | entry = mk_huge_pmd(new_page, vma->vm_page_prot); |
2065 | entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); |
2066 | |
2067 | /* |
2068 | * Clear the old entry under pagetable lock and establish the new PTE. |
2069 | * Any parallel GUP will either observe the old page blocking on the |
2070 | * page lock, block on the page table lock or observe the new page. |
2071 | * The SetPageUptodate on the new page and page_add_new_anon_rmap |
2072 | * guarantee the copy is visible before the pagetable update. |
2073 | */ |
2074 | flush_cache_range(vma, mmun_start, mmun_end); |
2075 | page_add_anon_rmap(new_page, vma, mmun_start, true); |
2076 | pmdp_huge_clear_flush_notify(vma, mmun_start, pmd); |
2077 | set_pmd_at(mm, mmun_start, pmd, entry); |
2078 | update_mmu_cache_pmd(vma, address, &entry); |
2079 | |
2080 | if (page_count(page) != 2) { |
2081 | set_pmd_at(mm, mmun_start, pmd, orig_entry); |
2082 | flush_pmd_tlb_range(vma, mmun_start, mmun_end); |
2083 | mmu_notifier_invalidate_range(mm, mmun_start, mmun_end); |
2084 | update_mmu_cache_pmd(vma, address, &entry); |
2085 | page_remove_rmap(new_page, true); |
2086 | goto fail_putback; |
2087 | } |
2088 | |
2089 | mlock_migrate_page(new_page, page); |
2090 | page_remove_rmap(page, true); |
2091 | set_page_owner_migrate_reason(new_page, MR_NUMA_MISPLACED); |
2092 | |
2093 | spin_unlock(ptl); |
2094 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); |
2095 | |
2096 | /* Take an "isolate" reference and put new page on the LRU. */ |
2097 | get_page(new_page); |
2098 | putback_lru_page(new_page); |
2099 | |
2100 | unlock_page(new_page); |
2101 | unlock_page(page); |
2102 | put_page(page); /* Drop the rmap reference */ |
2103 | put_page(page); /* Drop the LRU isolation reference */ |
2104 | |
2105 | count_vm_events(PGMIGRATE_SUCCESS, HPAGE_PMD_NR); |
2106 | count_vm_numa_events(NUMA_PAGE_MIGRATE, HPAGE_PMD_NR); |
2107 | |
2108 | mod_node_page_state(page_pgdat(page), |
2109 | NR_ISOLATED_ANON + page_lru, |
2110 | -HPAGE_PMD_NR); |
2111 | return isolated; |
2112 | |
2113 | out_fail: |
2114 | count_vm_events(PGMIGRATE_FAIL, HPAGE_PMD_NR); |
2115 | out_dropref: |
2116 | ptl = pmd_lock(mm, pmd); |
2117 | if (pmd_same(*pmd, entry)) { |
2118 | entry = pmd_modify(entry, vma->vm_page_prot); |
2119 | set_pmd_at(mm, mmun_start, pmd, entry); |
2120 | update_mmu_cache_pmd(vma, address, &entry); |
2121 | } |
2122 | spin_unlock(ptl); |
2123 | |
2124 | out_unlock: |
2125 | unlock_page(page); |
2126 | put_page(page); |
2127 | return 0; |
2128 | } |
2129 | #endif /* CONFIG_NUMA_BALANCING */ |
2130 | |
2131 | #endif /* CONFIG_NUMA */ |
2132 |