]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
b20a3503 | 2 | /* |
14e0f9bc | 3 | * Memory Migration functionality - linux/mm/migrate.c |
b20a3503 CL |
4 | * |
5 | * Copyright (C) 2006 Silicon Graphics, Inc., Christoph Lameter | |
6 | * | |
7 | * Page migration was first developed in the context of the memory hotplug | |
8 | * project. The main authors of the migration code are: | |
9 | * | |
10 | * IWAMOTO Toshihiro <iwamoto@valinux.co.jp> | |
11 | * Hirokazu Takahashi <taka@valinux.co.jp> | |
12 | * Dave Hansen <haveblue@us.ibm.com> | |
cde53535 | 13 | * Christoph Lameter |
b20a3503 CL |
14 | */ |
15 | ||
16 | #include <linux/migrate.h> | |
b95f1b31 | 17 | #include <linux/export.h> |
b20a3503 | 18 | #include <linux/swap.h> |
0697212a | 19 | #include <linux/swapops.h> |
b20a3503 | 20 | #include <linux/pagemap.h> |
e23ca00b | 21 | #include <linux/buffer_head.h> |
b20a3503 | 22 | #include <linux/mm_inline.h> |
b488893a | 23 | #include <linux/nsproxy.h> |
b20a3503 | 24 | #include <linux/pagevec.h> |
e9995ef9 | 25 | #include <linux/ksm.h> |
b20a3503 CL |
26 | #include <linux/rmap.h> |
27 | #include <linux/topology.h> | |
28 | #include <linux/cpu.h> | |
29 | #include <linux/cpuset.h> | |
04e62a29 | 30 | #include <linux/writeback.h> |
742755a1 CL |
31 | #include <linux/mempolicy.h> |
32 | #include <linux/vmalloc.h> | |
86c3a764 | 33 | #include <linux/security.h> |
42cb14b1 | 34 | #include <linux/backing-dev.h> |
bda807d4 | 35 | #include <linux/compaction.h> |
4f5ca265 | 36 | #include <linux/syscalls.h> |
7addf443 | 37 | #include <linux/compat.h> |
290408d4 | 38 | #include <linux/hugetlb.h> |
8e6ac7fa | 39 | #include <linux/hugetlb_cgroup.h> |
5a0e3ad6 | 40 | #include <linux/gfp.h> |
a520110e | 41 | #include <linux/pagewalk.h> |
df6ad698 | 42 | #include <linux/pfn_t.h> |
a5430dda | 43 | #include <linux/memremap.h> |
8315ada7 | 44 | #include <linux/userfaultfd_k.h> |
bf6bddf1 | 45 | #include <linux/balloon_compaction.h> |
f714f4f2 | 46 | #include <linux/mmu_notifier.h> |
33c3fc71 | 47 | #include <linux/page_idle.h> |
d435edca | 48 | #include <linux/page_owner.h> |
6e84f315 | 49 | #include <linux/sched/mm.h> |
197e7e52 | 50 | #include <linux/ptrace.h> |
34290e2c | 51 | #include <linux/oom.h> |
b20a3503 | 52 | |
0d1836c3 MN |
53 | #include <asm/tlbflush.h> |
54 | ||
7b2a2d4a MG |
55 | #define CREATE_TRACE_POINTS |
56 | #include <trace/events/migrate.h> | |
57 | ||
b20a3503 CL |
58 | #include "internal.h" |
59 | ||
9e5bcd61 | 60 | int isolate_movable_page(struct page *page, isolate_mode_t mode) |
bda807d4 MK |
61 | { |
62 | struct address_space *mapping; | |
63 | ||
64 | /* | |
65 | * Avoid burning cycles with pages that are yet under __free_pages(), | |
66 | * or just got freed under us. | |
67 | * | |
68 | * In case we 'win' a race for a movable page being freed under us and | |
69 | * raise its refcount preventing __free_pages() from doing its job | |
70 | * the put_page() at the end of this block will take care of | |
71 | * release this page, thus avoiding a nasty leakage. | |
72 | */ | |
73 | if (unlikely(!get_page_unless_zero(page))) | |
74 | goto out; | |
75 | ||
76 | /* | |
77 | * Check PageMovable before holding a PG_lock because page's owner | |
78 | * assumes anybody doesn't touch PG_lock of newly allocated page | |
8bb4e7a2 | 79 | * so unconditionally grabbing the lock ruins page's owner side. |
bda807d4 MK |
80 | */ |
81 | if (unlikely(!__PageMovable(page))) | |
82 | goto out_putpage; | |
83 | /* | |
84 | * As movable pages are not isolated from LRU lists, concurrent | |
85 | * compaction threads can race against page migration functions | |
86 | * as well as race against the releasing a page. | |
87 | * | |
88 | * In order to avoid having an already isolated movable page | |
89 | * being (wrongly) re-isolated while it is under migration, | |
90 | * or to avoid attempting to isolate pages being released, | |
91 | * lets be sure we have the page lock | |
92 | * before proceeding with the movable page isolation steps. | |
93 | */ | |
94 | if (unlikely(!trylock_page(page))) | |
95 | goto out_putpage; | |
96 | ||
97 | if (!PageMovable(page) || PageIsolated(page)) | |
98 | goto out_no_isolated; | |
99 | ||
100 | mapping = page_mapping(page); | |
101 | VM_BUG_ON_PAGE(!mapping, page); | |
102 | ||
103 | if (!mapping->a_ops->isolate_page(page, mode)) | |
104 | goto out_no_isolated; | |
105 | ||
106 | /* Driver shouldn't use PG_isolated bit of page->flags */ | |
107 | WARN_ON_ONCE(PageIsolated(page)); | |
108 | __SetPageIsolated(page); | |
109 | unlock_page(page); | |
110 | ||
9e5bcd61 | 111 | return 0; |
bda807d4 MK |
112 | |
113 | out_no_isolated: | |
114 | unlock_page(page); | |
115 | out_putpage: | |
116 | put_page(page); | |
117 | out: | |
9e5bcd61 | 118 | return -EBUSY; |
bda807d4 MK |
119 | } |
120 | ||
606a6f71 | 121 | static void putback_movable_page(struct page *page) |
bda807d4 MK |
122 | { |
123 | struct address_space *mapping; | |
124 | ||
bda807d4 MK |
125 | mapping = page_mapping(page); |
126 | mapping->a_ops->putback_page(page); | |
127 | __ClearPageIsolated(page); | |
128 | } | |
129 | ||
5733c7d1 RA |
130 | /* |
131 | * Put previously isolated pages back onto the appropriate lists | |
132 | * from where they were once taken off for compaction/migration. | |
133 | * | |
59c82b70 JK |
134 | * This function shall be used whenever the isolated pageset has been |
135 | * built from lru, balloon, hugetlbfs page. See isolate_migratepages_range() | |
136 | * and isolate_huge_page(). | |
5733c7d1 RA |
137 | */ |
138 | void putback_movable_pages(struct list_head *l) | |
139 | { | |
140 | struct page *page; | |
141 | struct page *page2; | |
142 | ||
b20a3503 | 143 | list_for_each_entry_safe(page, page2, l, lru) { |
31caf665 NH |
144 | if (unlikely(PageHuge(page))) { |
145 | putback_active_hugepage(page); | |
146 | continue; | |
147 | } | |
e24f0b8f | 148 | list_del(&page->lru); |
bda807d4 MK |
149 | /* |
150 | * We isolated non-lru movable page so here we can use | |
151 | * __PageMovable because LRU page's mapping cannot have | |
152 | * PAGE_MAPPING_MOVABLE. | |
153 | */ | |
b1123ea6 | 154 | if (unlikely(__PageMovable(page))) { |
bda807d4 MK |
155 | VM_BUG_ON_PAGE(!PageIsolated(page), page); |
156 | lock_page(page); | |
157 | if (PageMovable(page)) | |
158 | putback_movable_page(page); | |
159 | else | |
160 | __ClearPageIsolated(page); | |
161 | unlock_page(page); | |
162 | put_page(page); | |
163 | } else { | |
e8db67eb | 164 | mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + |
6c357848 | 165 | page_is_file_lru(page), -thp_nr_pages(page)); |
fc280fe8 | 166 | putback_lru_page(page); |
bda807d4 | 167 | } |
b20a3503 | 168 | } |
b20a3503 CL |
169 | } |
170 | ||
0697212a CL |
171 | /* |
172 | * Restore a potential migration pte to a working pte entry | |
173 | */ | |
e4b82222 | 174 | static bool remove_migration_pte(struct page *page, struct vm_area_struct *vma, |
e9995ef9 | 175 | unsigned long addr, void *old) |
0697212a | 176 | { |
3fe87967 KS |
177 | struct page_vma_mapped_walk pvmw = { |
178 | .page = old, | |
179 | .vma = vma, | |
180 | .address = addr, | |
181 | .flags = PVMW_SYNC | PVMW_MIGRATION, | |
182 | }; | |
183 | struct page *new; | |
184 | pte_t pte; | |
0697212a | 185 | swp_entry_t entry; |
0697212a | 186 | |
3fe87967 KS |
187 | VM_BUG_ON_PAGE(PageTail(page), page); |
188 | while (page_vma_mapped_walk(&pvmw)) { | |
4b0ece6f NH |
189 | if (PageKsm(page)) |
190 | new = page; | |
191 | else | |
192 | new = page - pvmw.page->index + | |
193 | linear_page_index(vma, pvmw.address); | |
0697212a | 194 | |
616b8371 ZY |
195 | #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION |
196 | /* PMD-mapped THP migration entry */ | |
197 | if (!pvmw.pte) { | |
198 | VM_BUG_ON_PAGE(PageHuge(page) || !PageTransCompound(page), page); | |
199 | remove_migration_pmd(&pvmw, new); | |
200 | continue; | |
201 | } | |
202 | #endif | |
203 | ||
3fe87967 KS |
204 | get_page(new); |
205 | pte = pte_mkold(mk_pte(new, READ_ONCE(vma->vm_page_prot))); | |
206 | if (pte_swp_soft_dirty(*pvmw.pte)) | |
207 | pte = pte_mksoft_dirty(pte); | |
0697212a | 208 | |
3fe87967 KS |
209 | /* |
210 | * Recheck VMA as permissions can change since migration started | |
211 | */ | |
212 | entry = pte_to_swp_entry(*pvmw.pte); | |
4dd845b5 | 213 | if (is_writable_migration_entry(entry)) |
3fe87967 | 214 | pte = maybe_mkwrite(pte, vma); |
f45ec5ff PX |
215 | else if (pte_swp_uffd_wp(*pvmw.pte)) |
216 | pte = pte_mkuffd_wp(pte); | |
d3cb8bf6 | 217 | |
6128763f | 218 | if (unlikely(is_device_private_page(new))) { |
4dd845b5 AP |
219 | if (pte_write(pte)) |
220 | entry = make_writable_device_private_entry( | |
221 | page_to_pfn(new)); | |
222 | else | |
223 | entry = make_readable_device_private_entry( | |
224 | page_to_pfn(new)); | |
6128763f | 225 | pte = swp_entry_to_pte(entry); |
3d321bf8 RC |
226 | if (pte_swp_soft_dirty(*pvmw.pte)) |
227 | pte = pte_swp_mksoft_dirty(pte); | |
6128763f RC |
228 | if (pte_swp_uffd_wp(*pvmw.pte)) |
229 | pte = pte_swp_mkuffd_wp(pte); | |
d2b2c6dd | 230 | } |
a5430dda | 231 | |
3ef8fd7f | 232 | #ifdef CONFIG_HUGETLB_PAGE |
3fe87967 | 233 | if (PageHuge(new)) { |
79c1c594 CL |
234 | unsigned int shift = huge_page_shift(hstate_vma(vma)); |
235 | ||
3fe87967 | 236 | pte = pte_mkhuge(pte); |
79c1c594 | 237 | pte = arch_make_huge_pte(pte, shift, vma->vm_flags); |
383321ab | 238 | set_huge_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte); |
3fe87967 KS |
239 | if (PageAnon(new)) |
240 | hugepage_add_anon_rmap(new, vma, pvmw.address); | |
241 | else | |
242 | page_dup_rmap(new, true); | |
383321ab AK |
243 | } else |
244 | #endif | |
245 | { | |
246 | set_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte); | |
04e62a29 | 247 | |
383321ab AK |
248 | if (PageAnon(new)) |
249 | page_add_anon_rmap(new, vma, pvmw.address, false); | |
250 | else | |
251 | page_add_file_rmap(new, false); | |
252 | } | |
3fe87967 KS |
253 | if (vma->vm_flags & VM_LOCKED && !PageTransCompound(new)) |
254 | mlock_vma_page(new); | |
255 | ||
e125fe40 KS |
256 | if (PageTransHuge(page) && PageMlocked(page)) |
257 | clear_page_mlock(page); | |
258 | ||
3fe87967 KS |
259 | /* No need to invalidate - it was non-present before */ |
260 | update_mmu_cache(vma, pvmw.address, pvmw.pte); | |
261 | } | |
51afb12b | 262 | |
e4b82222 | 263 | return true; |
0697212a CL |
264 | } |
265 | ||
04e62a29 CL |
266 | /* |
267 | * Get rid of all migration entries and replace them by | |
268 | * references to the indicated page. | |
269 | */ | |
e388466d | 270 | void remove_migration_ptes(struct page *old, struct page *new, bool locked) |
04e62a29 | 271 | { |
051ac83a JK |
272 | struct rmap_walk_control rwc = { |
273 | .rmap_one = remove_migration_pte, | |
274 | .arg = old, | |
275 | }; | |
276 | ||
e388466d KS |
277 | if (locked) |
278 | rmap_walk_locked(new, &rwc); | |
279 | else | |
280 | rmap_walk(new, &rwc); | |
04e62a29 CL |
281 | } |
282 | ||
0697212a CL |
283 | /* |
284 | * Something used the pte of a page under migration. We need to | |
285 | * get to the page and wait until migration is finished. | |
286 | * When we return from this function the fault will be retried. | |
0697212a | 287 | */ |
e66f17ff | 288 | void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep, |
30dad309 | 289 | spinlock_t *ptl) |
0697212a | 290 | { |
30dad309 | 291 | pte_t pte; |
0697212a CL |
292 | swp_entry_t entry; |
293 | struct page *page; | |
294 | ||
30dad309 | 295 | spin_lock(ptl); |
0697212a CL |
296 | pte = *ptep; |
297 | if (!is_swap_pte(pte)) | |
298 | goto out; | |
299 | ||
300 | entry = pte_to_swp_entry(pte); | |
301 | if (!is_migration_entry(entry)) | |
302 | goto out; | |
303 | ||
af5cdaf8 | 304 | page = pfn_swap_entry_to_page(entry); |
ffc90cbb | 305 | page = compound_head(page); |
0697212a | 306 | |
e286781d | 307 | /* |
89eb946a | 308 | * Once page cache replacement of page migration started, page_count |
9a1ea439 HD |
309 | * is zero; but we must not call put_and_wait_on_page_locked() without |
310 | * a ref. Use get_page_unless_zero(), and just fault again if it fails. | |
e286781d NP |
311 | */ |
312 | if (!get_page_unless_zero(page)) | |
313 | goto out; | |
0697212a | 314 | pte_unmap_unlock(ptep, ptl); |
48054625 | 315 | put_and_wait_on_page_locked(page, TASK_UNINTERRUPTIBLE); |
0697212a CL |
316 | return; |
317 | out: | |
318 | pte_unmap_unlock(ptep, ptl); | |
319 | } | |
320 | ||
30dad309 NH |
321 | void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd, |
322 | unsigned long address) | |
323 | { | |
324 | spinlock_t *ptl = pte_lockptr(mm, pmd); | |
325 | pte_t *ptep = pte_offset_map(pmd, address); | |
326 | __migration_entry_wait(mm, ptep, ptl); | |
327 | } | |
328 | ||
cb900f41 KS |
329 | void migration_entry_wait_huge(struct vm_area_struct *vma, |
330 | struct mm_struct *mm, pte_t *pte) | |
30dad309 | 331 | { |
cb900f41 | 332 | spinlock_t *ptl = huge_pte_lockptr(hstate_vma(vma), mm, pte); |
30dad309 NH |
333 | __migration_entry_wait(mm, pte, ptl); |
334 | } | |
335 | ||
616b8371 ZY |
336 | #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION |
337 | void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd) | |
338 | { | |
339 | spinlock_t *ptl; | |
340 | struct page *page; | |
341 | ||
342 | ptl = pmd_lock(mm, pmd); | |
343 | if (!is_pmd_migration_entry(*pmd)) | |
344 | goto unlock; | |
af5cdaf8 | 345 | page = pfn_swap_entry_to_page(pmd_to_swp_entry(*pmd)); |
616b8371 ZY |
346 | if (!get_page_unless_zero(page)) |
347 | goto unlock; | |
348 | spin_unlock(ptl); | |
48054625 | 349 | put_and_wait_on_page_locked(page, TASK_UNINTERRUPTIBLE); |
616b8371 ZY |
350 | return; |
351 | unlock: | |
352 | spin_unlock(ptl); | |
353 | } | |
354 | #endif | |
355 | ||
f900482d | 356 | static int expected_page_refs(struct address_space *mapping, struct page *page) |
0b3901b3 JK |
357 | { |
358 | int expected_count = 1; | |
359 | ||
360 | /* | |
f1f4f3ab | 361 | * Device private pages have an extra refcount as they are |
0b3901b3 JK |
362 | * ZONE_DEVICE pages. |
363 | */ | |
364 | expected_count += is_device_private_page(page); | |
f900482d | 365 | if (mapping) |
6c357848 | 366 | expected_count += thp_nr_pages(page) + page_has_private(page); |
0b3901b3 JK |
367 | |
368 | return expected_count; | |
369 | } | |
370 | ||
b20a3503 | 371 | /* |
c3fcf8a5 | 372 | * Replace the page in the mapping. |
5b5c7120 CL |
373 | * |
374 | * The number of remaining references must be: | |
375 | * 1 for anonymous pages without a mapping | |
376 | * 2 for pages with a mapping | |
266cf658 | 377 | * 3 for pages with a mapping and PagePrivate/PagePrivate2 set. |
b20a3503 | 378 | */ |
36bc08cc | 379 | int migrate_page_move_mapping(struct address_space *mapping, |
37109694 | 380 | struct page *newpage, struct page *page, int extra_count) |
b20a3503 | 381 | { |
89eb946a | 382 | XA_STATE(xas, &mapping->i_pages, page_index(page)); |
42cb14b1 HD |
383 | struct zone *oldzone, *newzone; |
384 | int dirty; | |
f900482d | 385 | int expected_count = expected_page_refs(mapping, page) + extra_count; |
5c447d27 | 386 | int nr = thp_nr_pages(page); |
8763cb45 | 387 | |
6c5240ae | 388 | if (!mapping) { |
0e8c7d0f | 389 | /* Anonymous page without mapping */ |
8e321fef | 390 | if (page_count(page) != expected_count) |
6c5240ae | 391 | return -EAGAIN; |
cf4b769a HD |
392 | |
393 | /* No turning back from here */ | |
cf4b769a HD |
394 | newpage->index = page->index; |
395 | newpage->mapping = page->mapping; | |
396 | if (PageSwapBacked(page)) | |
fa9949da | 397 | __SetPageSwapBacked(newpage); |
cf4b769a | 398 | |
78bd5209 | 399 | return MIGRATEPAGE_SUCCESS; |
6c5240ae CL |
400 | } |
401 | ||
42cb14b1 HD |
402 | oldzone = page_zone(page); |
403 | newzone = page_zone(newpage); | |
404 | ||
89eb946a | 405 | xas_lock_irq(&xas); |
89eb946a MW |
406 | if (page_count(page) != expected_count || xas_load(&xas) != page) { |
407 | xas_unlock_irq(&xas); | |
e23ca00b | 408 | return -EAGAIN; |
b20a3503 CL |
409 | } |
410 | ||
fe896d18 | 411 | if (!page_ref_freeze(page, expected_count)) { |
89eb946a | 412 | xas_unlock_irq(&xas); |
e286781d NP |
413 | return -EAGAIN; |
414 | } | |
415 | ||
b20a3503 | 416 | /* |
cf4b769a HD |
417 | * Now we know that no one else is looking at the page: |
418 | * no turning back from here. | |
b20a3503 | 419 | */ |
cf4b769a HD |
420 | newpage->index = page->index; |
421 | newpage->mapping = page->mapping; | |
5c447d27 | 422 | page_ref_add(newpage, nr); /* add cache reference */ |
6326fec1 NP |
423 | if (PageSwapBacked(page)) { |
424 | __SetPageSwapBacked(newpage); | |
425 | if (PageSwapCache(page)) { | |
426 | SetPageSwapCache(newpage); | |
427 | set_page_private(newpage, page_private(page)); | |
428 | } | |
429 | } else { | |
430 | VM_BUG_ON_PAGE(PageSwapCache(page), page); | |
b20a3503 CL |
431 | } |
432 | ||
42cb14b1 HD |
433 | /* Move dirty while page refs frozen and newpage not yet exposed */ |
434 | dirty = PageDirty(page); | |
435 | if (dirty) { | |
436 | ClearPageDirty(page); | |
437 | SetPageDirty(newpage); | |
438 | } | |
439 | ||
89eb946a | 440 | xas_store(&xas, newpage); |
e71769ae NH |
441 | if (PageTransHuge(page)) { |
442 | int i; | |
e71769ae | 443 | |
5c447d27 | 444 | for (i = 1; i < nr; i++) { |
89eb946a | 445 | xas_next(&xas); |
4101196b | 446 | xas_store(&xas, newpage); |
e71769ae | 447 | } |
e71769ae | 448 | } |
7cf9c2c7 NP |
449 | |
450 | /* | |
937a94c9 JG |
451 | * Drop cache reference from old page by unfreezing |
452 | * to one less reference. | |
7cf9c2c7 NP |
453 | * We know this isn't the last reference. |
454 | */ | |
5c447d27 | 455 | page_ref_unfreeze(page, expected_count - nr); |
7cf9c2c7 | 456 | |
89eb946a | 457 | xas_unlock(&xas); |
42cb14b1 HD |
458 | /* Leave irq disabled to prevent preemption while updating stats */ |
459 | ||
0e8c7d0f CL |
460 | /* |
461 | * If moved to a different zone then also account | |
462 | * the page for that zone. Other VM counters will be | |
463 | * taken care of when we establish references to the | |
464 | * new page and drop references to the old page. | |
465 | * | |
466 | * Note that anonymous pages are accounted for | |
4b9d0fab | 467 | * via NR_FILE_PAGES and NR_ANON_MAPPED if they |
0e8c7d0f CL |
468 | * are mapped to swap space. |
469 | */ | |
42cb14b1 | 470 | if (newzone != oldzone) { |
0d1c2072 JW |
471 | struct lruvec *old_lruvec, *new_lruvec; |
472 | struct mem_cgroup *memcg; | |
473 | ||
474 | memcg = page_memcg(page); | |
475 | old_lruvec = mem_cgroup_lruvec(memcg, oldzone->zone_pgdat); | |
476 | new_lruvec = mem_cgroup_lruvec(memcg, newzone->zone_pgdat); | |
477 | ||
5c447d27 SB |
478 | __mod_lruvec_state(old_lruvec, NR_FILE_PAGES, -nr); |
479 | __mod_lruvec_state(new_lruvec, NR_FILE_PAGES, nr); | |
42cb14b1 | 480 | if (PageSwapBacked(page) && !PageSwapCache(page)) { |
5c447d27 SB |
481 | __mod_lruvec_state(old_lruvec, NR_SHMEM, -nr); |
482 | __mod_lruvec_state(new_lruvec, NR_SHMEM, nr); | |
42cb14b1 | 483 | } |
b6038942 SB |
484 | #ifdef CONFIG_SWAP |
485 | if (PageSwapCache(page)) { | |
486 | __mod_lruvec_state(old_lruvec, NR_SWAPCACHE, -nr); | |
487 | __mod_lruvec_state(new_lruvec, NR_SWAPCACHE, nr); | |
488 | } | |
489 | #endif | |
f56753ac | 490 | if (dirty && mapping_can_writeback(mapping)) { |
5c447d27 SB |
491 | __mod_lruvec_state(old_lruvec, NR_FILE_DIRTY, -nr); |
492 | __mod_zone_page_state(oldzone, NR_ZONE_WRITE_PENDING, -nr); | |
493 | __mod_lruvec_state(new_lruvec, NR_FILE_DIRTY, nr); | |
494 | __mod_zone_page_state(newzone, NR_ZONE_WRITE_PENDING, nr); | |
42cb14b1 | 495 | } |
4b02108a | 496 | } |
42cb14b1 | 497 | local_irq_enable(); |
b20a3503 | 498 | |
78bd5209 | 499 | return MIGRATEPAGE_SUCCESS; |
b20a3503 | 500 | } |
1118dce7 | 501 | EXPORT_SYMBOL(migrate_page_move_mapping); |
b20a3503 | 502 | |
290408d4 NH |
503 | /* |
504 | * The expected number of remaining references is the same as that | |
505 | * of migrate_page_move_mapping(). | |
506 | */ | |
507 | int migrate_huge_page_move_mapping(struct address_space *mapping, | |
508 | struct page *newpage, struct page *page) | |
509 | { | |
89eb946a | 510 | XA_STATE(xas, &mapping->i_pages, page_index(page)); |
290408d4 | 511 | int expected_count; |
290408d4 | 512 | |
89eb946a | 513 | xas_lock_irq(&xas); |
290408d4 | 514 | expected_count = 2 + page_has_private(page); |
89eb946a MW |
515 | if (page_count(page) != expected_count || xas_load(&xas) != page) { |
516 | xas_unlock_irq(&xas); | |
290408d4 NH |
517 | return -EAGAIN; |
518 | } | |
519 | ||
fe896d18 | 520 | if (!page_ref_freeze(page, expected_count)) { |
89eb946a | 521 | xas_unlock_irq(&xas); |
290408d4 NH |
522 | return -EAGAIN; |
523 | } | |
524 | ||
cf4b769a HD |
525 | newpage->index = page->index; |
526 | newpage->mapping = page->mapping; | |
6a93ca8f | 527 | |
290408d4 NH |
528 | get_page(newpage); |
529 | ||
89eb946a | 530 | xas_store(&xas, newpage); |
290408d4 | 531 | |
fe896d18 | 532 | page_ref_unfreeze(page, expected_count - 1); |
290408d4 | 533 | |
89eb946a | 534 | xas_unlock_irq(&xas); |
6a93ca8f | 535 | |
78bd5209 | 536 | return MIGRATEPAGE_SUCCESS; |
290408d4 NH |
537 | } |
538 | ||
30b0a105 DH |
539 | /* |
540 | * Gigantic pages are so large that we do not guarantee that page++ pointer | |
541 | * arithmetic will work across the entire page. We need something more | |
542 | * specialized. | |
543 | */ | |
544 | static void __copy_gigantic_page(struct page *dst, struct page *src, | |
545 | int nr_pages) | |
546 | { | |
547 | int i; | |
548 | struct page *dst_base = dst; | |
549 | struct page *src_base = src; | |
550 | ||
551 | for (i = 0; i < nr_pages; ) { | |
552 | cond_resched(); | |
553 | copy_highpage(dst, src); | |
554 | ||
555 | i++; | |
556 | dst = mem_map_next(dst, dst_base, i); | |
557 | src = mem_map_next(src, src_base, i); | |
558 | } | |
559 | } | |
560 | ||
8cc5fcbb | 561 | void copy_huge_page(struct page *dst, struct page *src) |
30b0a105 DH |
562 | { |
563 | int i; | |
564 | int nr_pages; | |
565 | ||
566 | if (PageHuge(src)) { | |
567 | /* hugetlbfs page */ | |
568 | struct hstate *h = page_hstate(src); | |
569 | nr_pages = pages_per_huge_page(h); | |
570 | ||
571 | if (unlikely(nr_pages > MAX_ORDER_NR_PAGES)) { | |
572 | __copy_gigantic_page(dst, src, nr_pages); | |
573 | return; | |
574 | } | |
575 | } else { | |
576 | /* thp page */ | |
577 | BUG_ON(!PageTransHuge(src)); | |
6c357848 | 578 | nr_pages = thp_nr_pages(src); |
30b0a105 DH |
579 | } |
580 | ||
581 | for (i = 0; i < nr_pages; i++) { | |
582 | cond_resched(); | |
583 | copy_highpage(dst + i, src + i); | |
584 | } | |
585 | } | |
586 | ||
b20a3503 CL |
587 | /* |
588 | * Copy the page to its new location | |
589 | */ | |
2916ecc0 | 590 | void migrate_page_states(struct page *newpage, struct page *page) |
b20a3503 | 591 | { |
7851a45c RR |
592 | int cpupid; |
593 | ||
b20a3503 CL |
594 | if (PageError(page)) |
595 | SetPageError(newpage); | |
596 | if (PageReferenced(page)) | |
597 | SetPageReferenced(newpage); | |
598 | if (PageUptodate(page)) | |
599 | SetPageUptodate(newpage); | |
894bc310 | 600 | if (TestClearPageActive(page)) { |
309381fe | 601 | VM_BUG_ON_PAGE(PageUnevictable(page), page); |
b20a3503 | 602 | SetPageActive(newpage); |
418b27ef LS |
603 | } else if (TestClearPageUnevictable(page)) |
604 | SetPageUnevictable(newpage); | |
1899ad18 JW |
605 | if (PageWorkingset(page)) |
606 | SetPageWorkingset(newpage); | |
b20a3503 CL |
607 | if (PageChecked(page)) |
608 | SetPageChecked(newpage); | |
609 | if (PageMappedToDisk(page)) | |
610 | SetPageMappedToDisk(newpage); | |
611 | ||
42cb14b1 HD |
612 | /* Move dirty on pages not done by migrate_page_move_mapping() */ |
613 | if (PageDirty(page)) | |
614 | SetPageDirty(newpage); | |
b20a3503 | 615 | |
33c3fc71 VD |
616 | if (page_is_young(page)) |
617 | set_page_young(newpage); | |
618 | if (page_is_idle(page)) | |
619 | set_page_idle(newpage); | |
620 | ||
7851a45c RR |
621 | /* |
622 | * Copy NUMA information to the new page, to prevent over-eager | |
623 | * future migrations of this same page. | |
624 | */ | |
625 | cpupid = page_cpupid_xchg_last(page, -1); | |
626 | page_cpupid_xchg_last(newpage, cpupid); | |
627 | ||
e9995ef9 | 628 | ksm_migrate_page(newpage, page); |
c8d6553b HD |
629 | /* |
630 | * Please do not reorder this without considering how mm/ksm.c's | |
631 | * get_ksm_page() depends upon ksm_migrate_page() and PageSwapCache(). | |
632 | */ | |
b3b3a99c NH |
633 | if (PageSwapCache(page)) |
634 | ClearPageSwapCache(page); | |
b20a3503 | 635 | ClearPagePrivate(page); |
ad2fa371 MS |
636 | |
637 | /* page->private contains hugetlb specific flags */ | |
638 | if (!PageHuge(page)) | |
639 | set_page_private(page, 0); | |
b20a3503 CL |
640 | |
641 | /* | |
642 | * If any waiters have accumulated on the new page then | |
643 | * wake them up. | |
644 | */ | |
645 | if (PageWriteback(newpage)) | |
646 | end_page_writeback(newpage); | |
d435edca | 647 | |
6aeff241 YS |
648 | /* |
649 | * PG_readahead shares the same bit with PG_reclaim. The above | |
650 | * end_page_writeback() may clear PG_readahead mistakenly, so set the | |
651 | * bit after that. | |
652 | */ | |
653 | if (PageReadahead(page)) | |
654 | SetPageReadahead(newpage); | |
655 | ||
d435edca | 656 | copy_page_owner(page, newpage); |
74485cf2 | 657 | |
a333e3e7 HD |
658 | if (!PageHuge(page)) |
659 | mem_cgroup_migrate(page, newpage); | |
b20a3503 | 660 | } |
2916ecc0 JG |
661 | EXPORT_SYMBOL(migrate_page_states); |
662 | ||
663 | void migrate_page_copy(struct page *newpage, struct page *page) | |
664 | { | |
665 | if (PageHuge(page) || PageTransHuge(page)) | |
666 | copy_huge_page(newpage, page); | |
667 | else | |
668 | copy_highpage(newpage, page); | |
669 | ||
670 | migrate_page_states(newpage, page); | |
671 | } | |
1118dce7 | 672 | EXPORT_SYMBOL(migrate_page_copy); |
b20a3503 | 673 | |
1d8b85cc CL |
674 | /************************************************************ |
675 | * Migration functions | |
676 | ***********************************************************/ | |
677 | ||
b20a3503 | 678 | /* |
bda807d4 | 679 | * Common logic to directly migrate a single LRU page suitable for |
266cf658 | 680 | * pages that do not use PagePrivate/PagePrivate2. |
b20a3503 CL |
681 | * |
682 | * Pages are locked upon entry and exit. | |
683 | */ | |
2d1db3b1 | 684 | int migrate_page(struct address_space *mapping, |
a6bc32b8 MG |
685 | struct page *newpage, struct page *page, |
686 | enum migrate_mode mode) | |
b20a3503 CL |
687 | { |
688 | int rc; | |
689 | ||
690 | BUG_ON(PageWriteback(page)); /* Writeback must be complete */ | |
691 | ||
37109694 | 692 | rc = migrate_page_move_mapping(mapping, newpage, page, 0); |
b20a3503 | 693 | |
78bd5209 | 694 | if (rc != MIGRATEPAGE_SUCCESS) |
b20a3503 CL |
695 | return rc; |
696 | ||
2916ecc0 JG |
697 | if (mode != MIGRATE_SYNC_NO_COPY) |
698 | migrate_page_copy(newpage, page); | |
699 | else | |
700 | migrate_page_states(newpage, page); | |
78bd5209 | 701 | return MIGRATEPAGE_SUCCESS; |
b20a3503 CL |
702 | } |
703 | EXPORT_SYMBOL(migrate_page); | |
704 | ||
9361401e | 705 | #ifdef CONFIG_BLOCK |
84ade7c1 JK |
706 | /* Returns true if all buffers are successfully locked */ |
707 | static bool buffer_migrate_lock_buffers(struct buffer_head *head, | |
708 | enum migrate_mode mode) | |
709 | { | |
710 | struct buffer_head *bh = head; | |
711 | ||
712 | /* Simple case, sync compaction */ | |
713 | if (mode != MIGRATE_ASYNC) { | |
714 | do { | |
84ade7c1 JK |
715 | lock_buffer(bh); |
716 | bh = bh->b_this_page; | |
717 | ||
718 | } while (bh != head); | |
719 | ||
720 | return true; | |
721 | } | |
722 | ||
723 | /* async case, we cannot block on lock_buffer so use trylock_buffer */ | |
724 | do { | |
84ade7c1 JK |
725 | if (!trylock_buffer(bh)) { |
726 | /* | |
727 | * We failed to lock the buffer and cannot stall in | |
728 | * async migration. Release the taken locks | |
729 | */ | |
730 | struct buffer_head *failed_bh = bh; | |
84ade7c1 JK |
731 | bh = head; |
732 | while (bh != failed_bh) { | |
733 | unlock_buffer(bh); | |
84ade7c1 JK |
734 | bh = bh->b_this_page; |
735 | } | |
736 | return false; | |
737 | } | |
738 | ||
739 | bh = bh->b_this_page; | |
740 | } while (bh != head); | |
741 | return true; | |
742 | } | |
743 | ||
89cb0888 JK |
744 | static int __buffer_migrate_page(struct address_space *mapping, |
745 | struct page *newpage, struct page *page, enum migrate_mode mode, | |
746 | bool check_refs) | |
1d8b85cc | 747 | { |
1d8b85cc CL |
748 | struct buffer_head *bh, *head; |
749 | int rc; | |
cc4f11e6 | 750 | int expected_count; |
1d8b85cc | 751 | |
1d8b85cc | 752 | if (!page_has_buffers(page)) |
a6bc32b8 | 753 | return migrate_page(mapping, newpage, page, mode); |
1d8b85cc | 754 | |
cc4f11e6 | 755 | /* Check whether page does not have extra refs before we do more work */ |
f900482d | 756 | expected_count = expected_page_refs(mapping, page); |
cc4f11e6 JK |
757 | if (page_count(page) != expected_count) |
758 | return -EAGAIN; | |
1d8b85cc | 759 | |
cc4f11e6 JK |
760 | head = page_buffers(page); |
761 | if (!buffer_migrate_lock_buffers(head, mode)) | |
762 | return -EAGAIN; | |
1d8b85cc | 763 | |
89cb0888 JK |
764 | if (check_refs) { |
765 | bool busy; | |
766 | bool invalidated = false; | |
767 | ||
768 | recheck_buffers: | |
769 | busy = false; | |
770 | spin_lock(&mapping->private_lock); | |
771 | bh = head; | |
772 | do { | |
773 | if (atomic_read(&bh->b_count)) { | |
774 | busy = true; | |
775 | break; | |
776 | } | |
777 | bh = bh->b_this_page; | |
778 | } while (bh != head); | |
89cb0888 JK |
779 | if (busy) { |
780 | if (invalidated) { | |
781 | rc = -EAGAIN; | |
782 | goto unlock_buffers; | |
783 | } | |
ebdf4de5 | 784 | spin_unlock(&mapping->private_lock); |
89cb0888 JK |
785 | invalidate_bh_lrus(); |
786 | invalidated = true; | |
787 | goto recheck_buffers; | |
788 | } | |
789 | } | |
790 | ||
37109694 | 791 | rc = migrate_page_move_mapping(mapping, newpage, page, 0); |
78bd5209 | 792 | if (rc != MIGRATEPAGE_SUCCESS) |
cc4f11e6 | 793 | goto unlock_buffers; |
1d8b85cc | 794 | |
cd0f3715 | 795 | attach_page_private(newpage, detach_page_private(page)); |
1d8b85cc CL |
796 | |
797 | bh = head; | |
798 | do { | |
799 | set_bh_page(bh, newpage, bh_offset(bh)); | |
800 | bh = bh->b_this_page; | |
801 | ||
802 | } while (bh != head); | |
803 | ||
2916ecc0 JG |
804 | if (mode != MIGRATE_SYNC_NO_COPY) |
805 | migrate_page_copy(newpage, page); | |
806 | else | |
807 | migrate_page_states(newpage, page); | |
1d8b85cc | 808 | |
cc4f11e6 JK |
809 | rc = MIGRATEPAGE_SUCCESS; |
810 | unlock_buffers: | |
ebdf4de5 JK |
811 | if (check_refs) |
812 | spin_unlock(&mapping->private_lock); | |
1d8b85cc CL |
813 | bh = head; |
814 | do { | |
815 | unlock_buffer(bh); | |
1d8b85cc CL |
816 | bh = bh->b_this_page; |
817 | ||
818 | } while (bh != head); | |
819 | ||
cc4f11e6 | 820 | return rc; |
1d8b85cc | 821 | } |
89cb0888 JK |
822 | |
823 | /* | |
824 | * Migration function for pages with buffers. This function can only be used | |
825 | * if the underlying filesystem guarantees that no other references to "page" | |
826 | * exist. For example attached buffer heads are accessed only under page lock. | |
827 | */ | |
828 | int buffer_migrate_page(struct address_space *mapping, | |
829 | struct page *newpage, struct page *page, enum migrate_mode mode) | |
830 | { | |
831 | return __buffer_migrate_page(mapping, newpage, page, mode, false); | |
832 | } | |
1d8b85cc | 833 | EXPORT_SYMBOL(buffer_migrate_page); |
89cb0888 JK |
834 | |
835 | /* | |
836 | * Same as above except that this variant is more careful and checks that there | |
837 | * are also no buffer head references. This function is the right one for | |
838 | * mappings where buffer heads are directly looked up and referenced (such as | |
839 | * block device mappings). | |
840 | */ | |
841 | int buffer_migrate_page_norefs(struct address_space *mapping, | |
842 | struct page *newpage, struct page *page, enum migrate_mode mode) | |
843 | { | |
844 | return __buffer_migrate_page(mapping, newpage, page, mode, true); | |
845 | } | |
9361401e | 846 | #endif |
1d8b85cc | 847 | |
04e62a29 CL |
848 | /* |
849 | * Writeback a page to clean the dirty state | |
850 | */ | |
851 | static int writeout(struct address_space *mapping, struct page *page) | |
8351a6e4 | 852 | { |
04e62a29 CL |
853 | struct writeback_control wbc = { |
854 | .sync_mode = WB_SYNC_NONE, | |
855 | .nr_to_write = 1, | |
856 | .range_start = 0, | |
857 | .range_end = LLONG_MAX, | |
04e62a29 CL |
858 | .for_reclaim = 1 |
859 | }; | |
860 | int rc; | |
861 | ||
862 | if (!mapping->a_ops->writepage) | |
863 | /* No write method for the address space */ | |
864 | return -EINVAL; | |
865 | ||
866 | if (!clear_page_dirty_for_io(page)) | |
867 | /* Someone else already triggered a write */ | |
868 | return -EAGAIN; | |
869 | ||
8351a6e4 | 870 | /* |
04e62a29 CL |
871 | * A dirty page may imply that the underlying filesystem has |
872 | * the page on some queue. So the page must be clean for | |
873 | * migration. Writeout may mean we loose the lock and the | |
874 | * page state is no longer what we checked for earlier. | |
875 | * At this point we know that the migration attempt cannot | |
876 | * be successful. | |
8351a6e4 | 877 | */ |
e388466d | 878 | remove_migration_ptes(page, page, false); |
8351a6e4 | 879 | |
04e62a29 | 880 | rc = mapping->a_ops->writepage(page, &wbc); |
8351a6e4 | 881 | |
04e62a29 CL |
882 | if (rc != AOP_WRITEPAGE_ACTIVATE) |
883 | /* unlocked. Relock */ | |
884 | lock_page(page); | |
885 | ||
bda8550d | 886 | return (rc < 0) ? -EIO : -EAGAIN; |
04e62a29 CL |
887 | } |
888 | ||
889 | /* | |
890 | * Default handling if a filesystem does not provide a migration function. | |
891 | */ | |
892 | static int fallback_migrate_page(struct address_space *mapping, | |
a6bc32b8 | 893 | struct page *newpage, struct page *page, enum migrate_mode mode) |
04e62a29 | 894 | { |
b969c4ab | 895 | if (PageDirty(page)) { |
a6bc32b8 | 896 | /* Only writeback pages in full synchronous migration */ |
2916ecc0 JG |
897 | switch (mode) { |
898 | case MIGRATE_SYNC: | |
899 | case MIGRATE_SYNC_NO_COPY: | |
900 | break; | |
901 | default: | |
b969c4ab | 902 | return -EBUSY; |
2916ecc0 | 903 | } |
04e62a29 | 904 | return writeout(mapping, page); |
b969c4ab | 905 | } |
8351a6e4 CL |
906 | |
907 | /* | |
908 | * Buffers may be managed in a filesystem specific way. | |
909 | * We must have no buffers or drop them. | |
910 | */ | |
266cf658 | 911 | if (page_has_private(page) && |
8351a6e4 | 912 | !try_to_release_page(page, GFP_KERNEL)) |
806031bb | 913 | return mode == MIGRATE_SYNC ? -EAGAIN : -EBUSY; |
8351a6e4 | 914 | |
a6bc32b8 | 915 | return migrate_page(mapping, newpage, page, mode); |
8351a6e4 CL |
916 | } |
917 | ||
e24f0b8f CL |
918 | /* |
919 | * Move a page to a newly allocated page | |
920 | * The page is locked and all ptes have been successfully removed. | |
921 | * | |
922 | * The new page will have replaced the old page if this function | |
923 | * is successful. | |
894bc310 LS |
924 | * |
925 | * Return value: | |
926 | * < 0 - error code | |
78bd5209 | 927 | * MIGRATEPAGE_SUCCESS - success |
e24f0b8f | 928 | */ |
3fe2011f | 929 | static int move_to_new_page(struct page *newpage, struct page *page, |
5c3f9a67 | 930 | enum migrate_mode mode) |
e24f0b8f CL |
931 | { |
932 | struct address_space *mapping; | |
bda807d4 MK |
933 | int rc = -EAGAIN; |
934 | bool is_lru = !__PageMovable(page); | |
e24f0b8f | 935 | |
7db7671f HD |
936 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
937 | VM_BUG_ON_PAGE(!PageLocked(newpage), newpage); | |
e24f0b8f | 938 | |
e24f0b8f | 939 | mapping = page_mapping(page); |
bda807d4 MK |
940 | |
941 | if (likely(is_lru)) { | |
942 | if (!mapping) | |
943 | rc = migrate_page(mapping, newpage, page, mode); | |
944 | else if (mapping->a_ops->migratepage) | |
945 | /* | |
946 | * Most pages have a mapping and most filesystems | |
947 | * provide a migratepage callback. Anonymous pages | |
948 | * are part of swap space which also has its own | |
949 | * migratepage callback. This is the most common path | |
950 | * for page migration. | |
951 | */ | |
952 | rc = mapping->a_ops->migratepage(mapping, newpage, | |
953 | page, mode); | |
954 | else | |
955 | rc = fallback_migrate_page(mapping, newpage, | |
956 | page, mode); | |
957 | } else { | |
e24f0b8f | 958 | /* |
bda807d4 MK |
959 | * In case of non-lru page, it could be released after |
960 | * isolation step. In that case, we shouldn't try migration. | |
e24f0b8f | 961 | */ |
bda807d4 MK |
962 | VM_BUG_ON_PAGE(!PageIsolated(page), page); |
963 | if (!PageMovable(page)) { | |
964 | rc = MIGRATEPAGE_SUCCESS; | |
965 | __ClearPageIsolated(page); | |
966 | goto out; | |
967 | } | |
968 | ||
969 | rc = mapping->a_ops->migratepage(mapping, newpage, | |
970 | page, mode); | |
971 | WARN_ON_ONCE(rc == MIGRATEPAGE_SUCCESS && | |
972 | !PageIsolated(page)); | |
973 | } | |
e24f0b8f | 974 | |
5c3f9a67 HD |
975 | /* |
976 | * When successful, old pagecache page->mapping must be cleared before | |
977 | * page is freed; but stats require that PageAnon be left as PageAnon. | |
978 | */ | |
979 | if (rc == MIGRATEPAGE_SUCCESS) { | |
bda807d4 MK |
980 | if (__PageMovable(page)) { |
981 | VM_BUG_ON_PAGE(!PageIsolated(page), page); | |
982 | ||
983 | /* | |
984 | * We clear PG_movable under page_lock so any compactor | |
985 | * cannot try to migrate this page. | |
986 | */ | |
987 | __ClearPageIsolated(page); | |
988 | } | |
989 | ||
990 | /* | |
c23a0c99 | 991 | * Anonymous and movable page->mapping will be cleared by |
bda807d4 MK |
992 | * free_pages_prepare so don't reset it here for keeping |
993 | * the type to work PageAnon, for example. | |
994 | */ | |
995 | if (!PageMappingFlags(page)) | |
5c3f9a67 | 996 | page->mapping = NULL; |
d2b2c6dd | 997 | |
25b2995a | 998 | if (likely(!is_zone_device_page(newpage))) |
d2b2c6dd LP |
999 | flush_dcache_page(newpage); |
1000 | ||
3fe2011f | 1001 | } |
bda807d4 | 1002 | out: |
e24f0b8f CL |
1003 | return rc; |
1004 | } | |
1005 | ||
0dabec93 | 1006 | static int __unmap_and_move(struct page *page, struct page *newpage, |
9c620e2b | 1007 | int force, enum migrate_mode mode) |
e24f0b8f | 1008 | { |
0dabec93 | 1009 | int rc = -EAGAIN; |
2ebba6b7 | 1010 | int page_was_mapped = 0; |
3f6c8272 | 1011 | struct anon_vma *anon_vma = NULL; |
bda807d4 | 1012 | bool is_lru = !__PageMovable(page); |
95a402c3 | 1013 | |
529ae9aa | 1014 | if (!trylock_page(page)) { |
a6bc32b8 | 1015 | if (!force || mode == MIGRATE_ASYNC) |
0dabec93 | 1016 | goto out; |
3e7d3449 MG |
1017 | |
1018 | /* | |
1019 | * It's not safe for direct compaction to call lock_page. | |
1020 | * For example, during page readahead pages are added locked | |
1021 | * to the LRU. Later, when the IO completes the pages are | |
1022 | * marked uptodate and unlocked. However, the queueing | |
1023 | * could be merging multiple pages for one bio (e.g. | |
d4388340 | 1024 | * mpage_readahead). If an allocation happens for the |
3e7d3449 MG |
1025 | * second or third page, the process can end up locking |
1026 | * the same page twice and deadlocking. Rather than | |
1027 | * trying to be clever about what pages can be locked, | |
1028 | * avoid the use of lock_page for direct compaction | |
1029 | * altogether. | |
1030 | */ | |
1031 | if (current->flags & PF_MEMALLOC) | |
0dabec93 | 1032 | goto out; |
3e7d3449 | 1033 | |
e24f0b8f CL |
1034 | lock_page(page); |
1035 | } | |
1036 | ||
1037 | if (PageWriteback(page)) { | |
11bc82d6 | 1038 | /* |
fed5b64a | 1039 | * Only in the case of a full synchronous migration is it |
a6bc32b8 MG |
1040 | * necessary to wait for PageWriteback. In the async case, |
1041 | * the retry loop is too short and in the sync-light case, | |
1042 | * the overhead of stalling is too much | |
11bc82d6 | 1043 | */ |
2916ecc0 JG |
1044 | switch (mode) { |
1045 | case MIGRATE_SYNC: | |
1046 | case MIGRATE_SYNC_NO_COPY: | |
1047 | break; | |
1048 | default: | |
11bc82d6 | 1049 | rc = -EBUSY; |
0a31bc97 | 1050 | goto out_unlock; |
11bc82d6 AA |
1051 | } |
1052 | if (!force) | |
0a31bc97 | 1053 | goto out_unlock; |
e24f0b8f CL |
1054 | wait_on_page_writeback(page); |
1055 | } | |
03f15c86 | 1056 | |
e24f0b8f | 1057 | /* |
dc386d4d KH |
1058 | * By try_to_unmap(), page->mapcount goes down to 0 here. In this case, |
1059 | * we cannot notice that anon_vma is freed while we migrates a page. | |
1ce82b69 | 1060 | * This get_anon_vma() delays freeing anon_vma pointer until the end |
dc386d4d | 1061 | * of migration. File cache pages are no problem because of page_lock() |
989f89c5 KH |
1062 | * File Caches may use write_page() or lock_page() in migration, then, |
1063 | * just care Anon page here. | |
03f15c86 HD |
1064 | * |
1065 | * Only page_get_anon_vma() understands the subtleties of | |
1066 | * getting a hold on an anon_vma from outside one of its mms. | |
1067 | * But if we cannot get anon_vma, then we won't need it anyway, | |
1068 | * because that implies that the anon page is no longer mapped | |
1069 | * (and cannot be remapped so long as we hold the page lock). | |
dc386d4d | 1070 | */ |
03f15c86 | 1071 | if (PageAnon(page) && !PageKsm(page)) |
746b18d4 | 1072 | anon_vma = page_get_anon_vma(page); |
62e1c553 | 1073 | |
7db7671f HD |
1074 | /* |
1075 | * Block others from accessing the new page when we get around to | |
1076 | * establishing additional references. We are usually the only one | |
1077 | * holding a reference to newpage at this point. We used to have a BUG | |
1078 | * here if trylock_page(newpage) fails, but would like to allow for | |
1079 | * cases where there might be a race with the previous use of newpage. | |
1080 | * This is much like races on refcount of oldpage: just don't BUG(). | |
1081 | */ | |
1082 | if (unlikely(!trylock_page(newpage))) | |
1083 | goto out_unlock; | |
1084 | ||
bda807d4 MK |
1085 | if (unlikely(!is_lru)) { |
1086 | rc = move_to_new_page(newpage, page, mode); | |
1087 | goto out_unlock_both; | |
1088 | } | |
1089 | ||
dc386d4d | 1090 | /* |
62e1c553 SL |
1091 | * Corner case handling: |
1092 | * 1. When a new swap-cache page is read into, it is added to the LRU | |
1093 | * and treated as swapcache but it has no rmap yet. | |
1094 | * Calling try_to_unmap() against a page->mapping==NULL page will | |
1095 | * trigger a BUG. So handle it here. | |
d12b8951 | 1096 | * 2. An orphaned page (see truncate_cleanup_page) might have |
62e1c553 SL |
1097 | * fs-private metadata. The page can be picked up due to memory |
1098 | * offlining. Everywhere else except page reclaim, the page is | |
1099 | * invisible to the vm, so the page can not be migrated. So try to | |
1100 | * free the metadata, so the page can be freed. | |
e24f0b8f | 1101 | */ |
62e1c553 | 1102 | if (!page->mapping) { |
309381fe | 1103 | VM_BUG_ON_PAGE(PageAnon(page), page); |
1ce82b69 | 1104 | if (page_has_private(page)) { |
62e1c553 | 1105 | try_to_free_buffers(page); |
7db7671f | 1106 | goto out_unlock_both; |
62e1c553 | 1107 | } |
7db7671f HD |
1108 | } else if (page_mapped(page)) { |
1109 | /* Establish migration ptes */ | |
03f15c86 HD |
1110 | VM_BUG_ON_PAGE(PageAnon(page) && !PageKsm(page) && !anon_vma, |
1111 | page); | |
a98a2f0c | 1112 | try_to_migrate(page, 0); |
2ebba6b7 HD |
1113 | page_was_mapped = 1; |
1114 | } | |
dc386d4d | 1115 | |
e6a1530d | 1116 | if (!page_mapped(page)) |
5c3f9a67 | 1117 | rc = move_to_new_page(newpage, page, mode); |
e24f0b8f | 1118 | |
5c3f9a67 HD |
1119 | if (page_was_mapped) |
1120 | remove_migration_ptes(page, | |
e388466d | 1121 | rc == MIGRATEPAGE_SUCCESS ? newpage : page, false); |
3f6c8272 | 1122 | |
7db7671f HD |
1123 | out_unlock_both: |
1124 | unlock_page(newpage); | |
1125 | out_unlock: | |
3f6c8272 | 1126 | /* Drop an anon_vma reference if we took one */ |
76545066 | 1127 | if (anon_vma) |
9e60109f | 1128 | put_anon_vma(anon_vma); |
e24f0b8f | 1129 | unlock_page(page); |
0dabec93 | 1130 | out: |
c6c919eb MK |
1131 | /* |
1132 | * If migration is successful, decrease refcount of the newpage | |
1133 | * which will not free the page because new page owner increased | |
1134 | * refcounter. As well, if it is LRU page, add the page to LRU | |
e0a352fa DH |
1135 | * list in here. Use the old state of the isolated source page to |
1136 | * determine if we migrated a LRU page. newpage was already unlocked | |
1137 | * and possibly modified by its owner - don't rely on the page | |
1138 | * state. | |
c6c919eb MK |
1139 | */ |
1140 | if (rc == MIGRATEPAGE_SUCCESS) { | |
e0a352fa | 1141 | if (unlikely(!is_lru)) |
c6c919eb MK |
1142 | put_page(newpage); |
1143 | else | |
1144 | putback_lru_page(newpage); | |
1145 | } | |
1146 | ||
0dabec93 MK |
1147 | return rc; |
1148 | } | |
95a402c3 | 1149 | |
0dabec93 MK |
1150 | /* |
1151 | * Obtain the lock on page, remove all ptes and migrate the page | |
1152 | * to the newly allocated page in newpage. | |
1153 | */ | |
6ec4476a | 1154 | static int unmap_and_move(new_page_t get_new_page, |
ef2a5153 GU |
1155 | free_page_t put_new_page, |
1156 | unsigned long private, struct page *page, | |
add05cec | 1157 | int force, enum migrate_mode mode, |
dd4ae78a YS |
1158 | enum migrate_reason reason, |
1159 | struct list_head *ret) | |
0dabec93 | 1160 | { |
2def7424 | 1161 | int rc = MIGRATEPAGE_SUCCESS; |
74d4a579 | 1162 | struct page *newpage = NULL; |
0dabec93 | 1163 | |
94723aaf | 1164 | if (!thp_migration_supported() && PageTransHuge(page)) |
d532e2e5 | 1165 | return -ENOSYS; |
94723aaf | 1166 | |
0dabec93 MK |
1167 | if (page_count(page) == 1) { |
1168 | /* page was freed from under us. So we are done. */ | |
c6c919eb MK |
1169 | ClearPageActive(page); |
1170 | ClearPageUnevictable(page); | |
bda807d4 MK |
1171 | if (unlikely(__PageMovable(page))) { |
1172 | lock_page(page); | |
1173 | if (!PageMovable(page)) | |
1174 | __ClearPageIsolated(page); | |
1175 | unlock_page(page); | |
1176 | } | |
0dabec93 MK |
1177 | goto out; |
1178 | } | |
1179 | ||
74d4a579 YS |
1180 | newpage = get_new_page(page, private); |
1181 | if (!newpage) | |
1182 | return -ENOMEM; | |
1183 | ||
9c620e2b | 1184 | rc = __unmap_and_move(page, newpage, force, mode); |
c6c919eb | 1185 | if (rc == MIGRATEPAGE_SUCCESS) |
7cd12b4a | 1186 | set_page_owner_migrate_reason(newpage, reason); |
bf6bddf1 | 1187 | |
0dabec93 | 1188 | out: |
e24f0b8f | 1189 | if (rc != -EAGAIN) { |
0dabec93 MK |
1190 | /* |
1191 | * A page that has been migrated has all references | |
1192 | * removed and will be freed. A page that has not been | |
c23a0c99 | 1193 | * migrated will have kept its references and be restored. |
0dabec93 MK |
1194 | */ |
1195 | list_del(&page->lru); | |
dd4ae78a | 1196 | } |
6afcf8ef | 1197 | |
dd4ae78a YS |
1198 | /* |
1199 | * If migration is successful, releases reference grabbed during | |
1200 | * isolation. Otherwise, restore the page to right list unless | |
1201 | * we want to retry. | |
1202 | */ | |
1203 | if (rc == MIGRATEPAGE_SUCCESS) { | |
6afcf8ef ML |
1204 | /* |
1205 | * Compaction can migrate also non-LRU pages which are | |
1206 | * not accounted to NR_ISOLATED_*. They can be recognized | |
1207 | * as __PageMovable | |
1208 | */ | |
1209 | if (likely(!__PageMovable(page))) | |
e8db67eb | 1210 | mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + |
6c357848 | 1211 | page_is_file_lru(page), -thp_nr_pages(page)); |
c6c919eb | 1212 | |
79f5f8fa | 1213 | if (reason != MR_MEMORY_FAILURE) |
d7e69488 | 1214 | /* |
79f5f8fa | 1215 | * We release the page in page_handle_poison. |
d7e69488 | 1216 | */ |
79f5f8fa | 1217 | put_page(page); |
c6c919eb | 1218 | } else { |
dd4ae78a YS |
1219 | if (rc != -EAGAIN) |
1220 | list_add_tail(&page->lru, ret); | |
bda807d4 | 1221 | |
c6c919eb MK |
1222 | if (put_new_page) |
1223 | put_new_page(newpage, private); | |
1224 | else | |
1225 | put_page(newpage); | |
e24f0b8f | 1226 | } |
68711a74 | 1227 | |
e24f0b8f CL |
1228 | return rc; |
1229 | } | |
1230 | ||
290408d4 NH |
1231 | /* |
1232 | * Counterpart of unmap_and_move_page() for hugepage migration. | |
1233 | * | |
1234 | * This function doesn't wait the completion of hugepage I/O | |
1235 | * because there is no race between I/O and migration for hugepage. | |
1236 | * Note that currently hugepage I/O occurs only in direct I/O | |
1237 | * where no lock is held and PG_writeback is irrelevant, | |
1238 | * and writeback status of all subpages are counted in the reference | |
1239 | * count of the head page (i.e. if all subpages of a 2MB hugepage are | |
1240 | * under direct I/O, the reference of the head page is 512 and a bit more.) | |
1241 | * This means that when we try to migrate hugepage whose subpages are | |
1242 | * doing direct I/O, some references remain after try_to_unmap() and | |
1243 | * hugepage migration fails without data corruption. | |
1244 | * | |
1245 | * There is also no race when direct I/O is issued on the page under migration, | |
1246 | * because then pte is replaced with migration swap entry and direct I/O code | |
1247 | * will wait in the page fault for migration to complete. | |
1248 | */ | |
1249 | static int unmap_and_move_huge_page(new_page_t get_new_page, | |
68711a74 DR |
1250 | free_page_t put_new_page, unsigned long private, |
1251 | struct page *hpage, int force, | |
dd4ae78a YS |
1252 | enum migrate_mode mode, int reason, |
1253 | struct list_head *ret) | |
290408d4 | 1254 | { |
2def7424 | 1255 | int rc = -EAGAIN; |
2ebba6b7 | 1256 | int page_was_mapped = 0; |
32665f2b | 1257 | struct page *new_hpage; |
290408d4 | 1258 | struct anon_vma *anon_vma = NULL; |
c0d0381a | 1259 | struct address_space *mapping = NULL; |
290408d4 | 1260 | |
83467efb | 1261 | /* |
7ed2c31d | 1262 | * Migratability of hugepages depends on architectures and their size. |
83467efb NH |
1263 | * This check is necessary because some callers of hugepage migration |
1264 | * like soft offline and memory hotremove don't walk through page | |
1265 | * tables or check whether the hugepage is pmd-based or not before | |
1266 | * kicking migration. | |
1267 | */ | |
100873d7 | 1268 | if (!hugepage_migration_supported(page_hstate(hpage))) { |
dd4ae78a | 1269 | list_move_tail(&hpage->lru, ret); |
83467efb | 1270 | return -ENOSYS; |
32665f2b | 1271 | } |
83467efb | 1272 | |
71a64f61 MS |
1273 | if (page_count(hpage) == 1) { |
1274 | /* page was freed from under us. So we are done. */ | |
1275 | putback_active_hugepage(hpage); | |
1276 | return MIGRATEPAGE_SUCCESS; | |
1277 | } | |
1278 | ||
666feb21 | 1279 | new_hpage = get_new_page(hpage, private); |
290408d4 NH |
1280 | if (!new_hpage) |
1281 | return -ENOMEM; | |
1282 | ||
290408d4 | 1283 | if (!trylock_page(hpage)) { |
2916ecc0 | 1284 | if (!force) |
290408d4 | 1285 | goto out; |
2916ecc0 JG |
1286 | switch (mode) { |
1287 | case MIGRATE_SYNC: | |
1288 | case MIGRATE_SYNC_NO_COPY: | |
1289 | break; | |
1290 | default: | |
1291 | goto out; | |
1292 | } | |
290408d4 NH |
1293 | lock_page(hpage); |
1294 | } | |
1295 | ||
cb6acd01 MK |
1296 | /* |
1297 | * Check for pages which are in the process of being freed. Without | |
1298 | * page_mapping() set, hugetlbfs specific move page routine will not | |
1299 | * be called and we could leak usage counts for subpools. | |
1300 | */ | |
6acfb5ba | 1301 | if (hugetlb_page_subpool(hpage) && !page_mapping(hpage)) { |
cb6acd01 MK |
1302 | rc = -EBUSY; |
1303 | goto out_unlock; | |
1304 | } | |
1305 | ||
746b18d4 PZ |
1306 | if (PageAnon(hpage)) |
1307 | anon_vma = page_get_anon_vma(hpage); | |
290408d4 | 1308 | |
7db7671f HD |
1309 | if (unlikely(!trylock_page(new_hpage))) |
1310 | goto put_anon; | |
1311 | ||
2ebba6b7 | 1312 | if (page_mapped(hpage)) { |
336bf30e | 1313 | bool mapping_locked = false; |
a98a2f0c | 1314 | enum ttu_flags ttu = 0; |
336bf30e MK |
1315 | |
1316 | if (!PageAnon(hpage)) { | |
1317 | /* | |
1318 | * In shared mappings, try_to_unmap could potentially | |
1319 | * call huge_pmd_unshare. Because of this, take | |
1320 | * semaphore in write mode here and set TTU_RMAP_LOCKED | |
1321 | * to let lower levels know we have taken the lock. | |
1322 | */ | |
1323 | mapping = hugetlb_page_mapping_lock_write(hpage); | |
1324 | if (unlikely(!mapping)) | |
1325 | goto unlock_put_anon; | |
1326 | ||
1327 | mapping_locked = true; | |
1328 | ttu |= TTU_RMAP_LOCKED; | |
1329 | } | |
c0d0381a | 1330 | |
a98a2f0c | 1331 | try_to_migrate(hpage, ttu); |
2ebba6b7 | 1332 | page_was_mapped = 1; |
336bf30e MK |
1333 | |
1334 | if (mapping_locked) | |
1335 | i_mmap_unlock_write(mapping); | |
2ebba6b7 | 1336 | } |
290408d4 NH |
1337 | |
1338 | if (!page_mapped(hpage)) | |
5c3f9a67 | 1339 | rc = move_to_new_page(new_hpage, hpage, mode); |
290408d4 | 1340 | |
336bf30e | 1341 | if (page_was_mapped) |
5c3f9a67 | 1342 | remove_migration_ptes(hpage, |
336bf30e | 1343 | rc == MIGRATEPAGE_SUCCESS ? new_hpage : hpage, false); |
290408d4 | 1344 | |
c0d0381a | 1345 | unlock_put_anon: |
7db7671f HD |
1346 | unlock_page(new_hpage); |
1347 | ||
1348 | put_anon: | |
fd4a4663 | 1349 | if (anon_vma) |
9e60109f | 1350 | put_anon_vma(anon_vma); |
8e6ac7fa | 1351 | |
2def7424 | 1352 | if (rc == MIGRATEPAGE_SUCCESS) { |
ab5ac90a | 1353 | move_hugetlb_state(hpage, new_hpage, reason); |
2def7424 HD |
1354 | put_new_page = NULL; |
1355 | } | |
8e6ac7fa | 1356 | |
cb6acd01 | 1357 | out_unlock: |
290408d4 | 1358 | unlock_page(hpage); |
09761333 | 1359 | out: |
dd4ae78a | 1360 | if (rc == MIGRATEPAGE_SUCCESS) |
b8ec1cee | 1361 | putback_active_hugepage(hpage); |
a04840c6 | 1362 | else if (rc != -EAGAIN) |
dd4ae78a | 1363 | list_move_tail(&hpage->lru, ret); |
68711a74 DR |
1364 | |
1365 | /* | |
1366 | * If migration was not successful and there's a freeing callback, use | |
1367 | * it. Otherwise, put_page() will drop the reference grabbed during | |
1368 | * isolation. | |
1369 | */ | |
2def7424 | 1370 | if (put_new_page) |
68711a74 DR |
1371 | put_new_page(new_hpage, private); |
1372 | else | |
3aaa76e1 | 1373 | putback_active_hugepage(new_hpage); |
68711a74 | 1374 | |
290408d4 NH |
1375 | return rc; |
1376 | } | |
1377 | ||
d532e2e5 YS |
1378 | static inline int try_split_thp(struct page *page, struct page **page2, |
1379 | struct list_head *from) | |
1380 | { | |
1381 | int rc = 0; | |
1382 | ||
1383 | lock_page(page); | |
1384 | rc = split_huge_page_to_list(page, from); | |
1385 | unlock_page(page); | |
1386 | if (!rc) | |
1387 | list_safe_reset_next(page, *page2, lru); | |
1388 | ||
1389 | return rc; | |
1390 | } | |
1391 | ||
b20a3503 | 1392 | /* |
c73e5c9c SB |
1393 | * migrate_pages - migrate the pages specified in a list, to the free pages |
1394 | * supplied as the target for the page migration | |
b20a3503 | 1395 | * |
c73e5c9c SB |
1396 | * @from: The list of pages to be migrated. |
1397 | * @get_new_page: The function used to allocate free pages to be used | |
1398 | * as the target of the page migration. | |
68711a74 DR |
1399 | * @put_new_page: The function used to free target pages if migration |
1400 | * fails, or NULL if no special handling is necessary. | |
c73e5c9c SB |
1401 | * @private: Private data to be passed on to get_new_page() |
1402 | * @mode: The migration mode that specifies the constraints for | |
1403 | * page migration, if any. | |
1404 | * @reason: The reason for page migration. | |
b20a3503 | 1405 | * |
c73e5c9c SB |
1406 | * The function returns after 10 attempts or if no pages are movable any more |
1407 | * because the list has become empty or no retryable pages exist any more. | |
dd4ae78a YS |
1408 | * It is caller's responsibility to call putback_movable_pages() to return pages |
1409 | * to the LRU or free list only if ret != 0. | |
b20a3503 | 1410 | * |
c73e5c9c | 1411 | * Returns the number of pages that were not migrated, or an error code. |
b20a3503 | 1412 | */ |
9c620e2b | 1413 | int migrate_pages(struct list_head *from, new_page_t get_new_page, |
68711a74 DR |
1414 | free_page_t put_new_page, unsigned long private, |
1415 | enum migrate_mode mode, int reason) | |
b20a3503 | 1416 | { |
e24f0b8f | 1417 | int retry = 1; |
1a5bae25 | 1418 | int thp_retry = 1; |
b20a3503 | 1419 | int nr_failed = 0; |
5647bc29 | 1420 | int nr_succeeded = 0; |
1a5bae25 AK |
1421 | int nr_thp_succeeded = 0; |
1422 | int nr_thp_failed = 0; | |
1423 | int nr_thp_split = 0; | |
b20a3503 | 1424 | int pass = 0; |
1a5bae25 | 1425 | bool is_thp = false; |
b20a3503 CL |
1426 | struct page *page; |
1427 | struct page *page2; | |
1428 | int swapwrite = current->flags & PF_SWAPWRITE; | |
1a5bae25 | 1429 | int rc, nr_subpages; |
dd4ae78a | 1430 | LIST_HEAD(ret_pages); |
b0b515bf | 1431 | bool nosplit = (reason == MR_NUMA_MISPLACED); |
b20a3503 | 1432 | |
7bc1aec5 LM |
1433 | trace_mm_migrate_pages_start(mode, reason); |
1434 | ||
b20a3503 CL |
1435 | if (!swapwrite) |
1436 | current->flags |= PF_SWAPWRITE; | |
1437 | ||
1a5bae25 | 1438 | for (pass = 0; pass < 10 && (retry || thp_retry); pass++) { |
e24f0b8f | 1439 | retry = 0; |
1a5bae25 | 1440 | thp_retry = 0; |
b20a3503 | 1441 | |
e24f0b8f | 1442 | list_for_each_entry_safe(page, page2, from, lru) { |
94723aaf | 1443 | retry: |
1a5bae25 AK |
1444 | /* |
1445 | * THP statistics is based on the source huge page. | |
1446 | * Capture required information that might get lost | |
1447 | * during migration. | |
1448 | */ | |
6c5c7b9f | 1449 | is_thp = PageTransHuge(page) && !PageHuge(page); |
6c357848 | 1450 | nr_subpages = thp_nr_pages(page); |
e24f0b8f | 1451 | cond_resched(); |
2d1db3b1 | 1452 | |
31caf665 NH |
1453 | if (PageHuge(page)) |
1454 | rc = unmap_and_move_huge_page(get_new_page, | |
68711a74 | 1455 | put_new_page, private, page, |
dd4ae78a YS |
1456 | pass > 2, mode, reason, |
1457 | &ret_pages); | |
31caf665 | 1458 | else |
68711a74 | 1459 | rc = unmap_and_move(get_new_page, put_new_page, |
add05cec | 1460 | private, page, pass > 2, mode, |
dd4ae78a YS |
1461 | reason, &ret_pages); |
1462 | /* | |
1463 | * The rules are: | |
1464 | * Success: non hugetlb page will be freed, hugetlb | |
1465 | * page will be put back | |
1466 | * -EAGAIN: stay on the from list | |
1467 | * -ENOMEM: stay on the from list | |
1468 | * Other errno: put on ret_pages list then splice to | |
1469 | * from list | |
1470 | */ | |
e24f0b8f | 1471 | switch(rc) { |
d532e2e5 YS |
1472 | /* |
1473 | * THP migration might be unsupported or the | |
1474 | * allocation could've failed so we should | |
1475 | * retry on the same page with the THP split | |
1476 | * to base pages. | |
1477 | * | |
1478 | * Head page is retried immediately and tail | |
1479 | * pages are added to the tail of the list so | |
1480 | * we encounter them after the rest of the list | |
1481 | * is processed. | |
1482 | */ | |
1483 | case -ENOSYS: | |
1484 | /* THP migration is unsupported */ | |
1485 | if (is_thp) { | |
1486 | if (!try_split_thp(page, &page2, from)) { | |
1487 | nr_thp_split++; | |
1488 | goto retry; | |
1489 | } | |
1490 | ||
1491 | nr_thp_failed++; | |
1492 | nr_failed += nr_subpages; | |
1493 | break; | |
1494 | } | |
1495 | ||
1496 | /* Hugetlb migration is unsupported */ | |
1497 | nr_failed++; | |
1498 | break; | |
95a402c3 | 1499 | case -ENOMEM: |
94723aaf | 1500 | /* |
d532e2e5 YS |
1501 | * When memory is low, don't bother to try to migrate |
1502 | * other pages, just exit. | |
b0b515bf | 1503 | * THP NUMA faulting doesn't split THP to retry. |
94723aaf | 1504 | */ |
b0b515bf | 1505 | if (is_thp && !nosplit) { |
d532e2e5 | 1506 | if (!try_split_thp(page, &page2, from)) { |
1a5bae25 | 1507 | nr_thp_split++; |
94723aaf MH |
1508 | goto retry; |
1509 | } | |
6c5c7b9f | 1510 | |
1a5bae25 AK |
1511 | nr_thp_failed++; |
1512 | nr_failed += nr_subpages; | |
1513 | goto out; | |
1514 | } | |
dfef2ef4 | 1515 | nr_failed++; |
95a402c3 | 1516 | goto out; |
e24f0b8f | 1517 | case -EAGAIN: |
1a5bae25 AK |
1518 | if (is_thp) { |
1519 | thp_retry++; | |
1520 | break; | |
1521 | } | |
2d1db3b1 | 1522 | retry++; |
e24f0b8f | 1523 | break; |
78bd5209 | 1524 | case MIGRATEPAGE_SUCCESS: |
1a5bae25 AK |
1525 | if (is_thp) { |
1526 | nr_thp_succeeded++; | |
1527 | nr_succeeded += nr_subpages; | |
1528 | break; | |
1529 | } | |
5647bc29 | 1530 | nr_succeeded++; |
e24f0b8f CL |
1531 | break; |
1532 | default: | |
354a3363 | 1533 | /* |
d532e2e5 | 1534 | * Permanent failure (-EBUSY, etc.): |
354a3363 NH |
1535 | * unlike -EAGAIN case, the failed page is |
1536 | * removed from migration page list and not | |
1537 | * retried in the next outer loop. | |
1538 | */ | |
1a5bae25 AK |
1539 | if (is_thp) { |
1540 | nr_thp_failed++; | |
1541 | nr_failed += nr_subpages; | |
1542 | break; | |
1543 | } | |
2d1db3b1 | 1544 | nr_failed++; |
e24f0b8f | 1545 | break; |
2d1db3b1 | 1546 | } |
b20a3503 CL |
1547 | } |
1548 | } | |
1a5bae25 AK |
1549 | nr_failed += retry + thp_retry; |
1550 | nr_thp_failed += thp_retry; | |
f2f81fb2 | 1551 | rc = nr_failed; |
95a402c3 | 1552 | out: |
dd4ae78a YS |
1553 | /* |
1554 | * Put the permanent failure page back to migration list, they | |
1555 | * will be put back to the right list by the caller. | |
1556 | */ | |
1557 | list_splice(&ret_pages, from); | |
1558 | ||
1a5bae25 AK |
1559 | count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded); |
1560 | count_vm_events(PGMIGRATE_FAIL, nr_failed); | |
1561 | count_vm_events(THP_MIGRATION_SUCCESS, nr_thp_succeeded); | |
1562 | count_vm_events(THP_MIGRATION_FAIL, nr_thp_failed); | |
1563 | count_vm_events(THP_MIGRATION_SPLIT, nr_thp_split); | |
1564 | trace_mm_migrate_pages(nr_succeeded, nr_failed, nr_thp_succeeded, | |
1565 | nr_thp_failed, nr_thp_split, mode, reason); | |
7b2a2d4a | 1566 | |
b20a3503 CL |
1567 | if (!swapwrite) |
1568 | current->flags &= ~PF_SWAPWRITE; | |
1569 | ||
78bd5209 | 1570 | return rc; |
b20a3503 | 1571 | } |
95a402c3 | 1572 | |
19fc7bed | 1573 | struct page *alloc_migration_target(struct page *page, unsigned long private) |
b4b38223 | 1574 | { |
19fc7bed JK |
1575 | struct migration_target_control *mtc; |
1576 | gfp_t gfp_mask; | |
b4b38223 JK |
1577 | unsigned int order = 0; |
1578 | struct page *new_page = NULL; | |
19fc7bed JK |
1579 | int nid; |
1580 | int zidx; | |
1581 | ||
1582 | mtc = (struct migration_target_control *)private; | |
1583 | gfp_mask = mtc->gfp_mask; | |
1584 | nid = mtc->nid; | |
1585 | if (nid == NUMA_NO_NODE) | |
1586 | nid = page_to_nid(page); | |
b4b38223 | 1587 | |
d92bbc27 JK |
1588 | if (PageHuge(page)) { |
1589 | struct hstate *h = page_hstate(compound_head(page)); | |
1590 | ||
19fc7bed JK |
1591 | gfp_mask = htlb_modify_alloc_mask(h, gfp_mask); |
1592 | return alloc_huge_page_nodemask(h, nid, mtc->nmask, gfp_mask); | |
d92bbc27 | 1593 | } |
b4b38223 JK |
1594 | |
1595 | if (PageTransHuge(page)) { | |
9933a0c8 JK |
1596 | /* |
1597 | * clear __GFP_RECLAIM to make the migration callback | |
1598 | * consistent with regular THP allocations. | |
1599 | */ | |
1600 | gfp_mask &= ~__GFP_RECLAIM; | |
b4b38223 JK |
1601 | gfp_mask |= GFP_TRANSHUGE; |
1602 | order = HPAGE_PMD_ORDER; | |
1603 | } | |
19fc7bed JK |
1604 | zidx = zone_idx(page_zone(page)); |
1605 | if (is_highmem_idx(zidx) || zidx == ZONE_MOVABLE) | |
b4b38223 JK |
1606 | gfp_mask |= __GFP_HIGHMEM; |
1607 | ||
84172f4b | 1608 | new_page = __alloc_pages(gfp_mask, order, nid, mtc->nmask); |
b4b38223 JK |
1609 | |
1610 | if (new_page && PageTransHuge(new_page)) | |
1611 | prep_transhuge_page(new_page); | |
1612 | ||
1613 | return new_page; | |
1614 | } | |
1615 | ||
742755a1 | 1616 | #ifdef CONFIG_NUMA |
742755a1 | 1617 | |
a49bd4d7 | 1618 | static int store_status(int __user *status, int start, int value, int nr) |
742755a1 | 1619 | { |
a49bd4d7 MH |
1620 | while (nr-- > 0) { |
1621 | if (put_user(value, status + start)) | |
1622 | return -EFAULT; | |
1623 | start++; | |
1624 | } | |
1625 | ||
1626 | return 0; | |
1627 | } | |
1628 | ||
1629 | static int do_move_pages_to_node(struct mm_struct *mm, | |
1630 | struct list_head *pagelist, int node) | |
1631 | { | |
1632 | int err; | |
a0976311 JK |
1633 | struct migration_target_control mtc = { |
1634 | .nid = node, | |
1635 | .gfp_mask = GFP_HIGHUSER_MOVABLE | __GFP_THISNODE, | |
1636 | }; | |
a49bd4d7 | 1637 | |
a0976311 JK |
1638 | err = migrate_pages(pagelist, alloc_migration_target, NULL, |
1639 | (unsigned long)&mtc, MIGRATE_SYNC, MR_SYSCALL); | |
a49bd4d7 MH |
1640 | if (err) |
1641 | putback_movable_pages(pagelist); | |
1642 | return err; | |
742755a1 CL |
1643 | } |
1644 | ||
1645 | /* | |
a49bd4d7 MH |
1646 | * Resolves the given address to a struct page, isolates it from the LRU and |
1647 | * puts it to the given pagelist. | |
e0153fc2 YS |
1648 | * Returns: |
1649 | * errno - if the page cannot be found/isolated | |
1650 | * 0 - when it doesn't have to be migrated because it is already on the | |
1651 | * target node | |
1652 | * 1 - when it has been queued | |
742755a1 | 1653 | */ |
a49bd4d7 MH |
1654 | static int add_page_for_migration(struct mm_struct *mm, unsigned long addr, |
1655 | int node, struct list_head *pagelist, bool migrate_all) | |
742755a1 | 1656 | { |
a49bd4d7 MH |
1657 | struct vm_area_struct *vma; |
1658 | struct page *page; | |
1659 | unsigned int follflags; | |
742755a1 | 1660 | int err; |
742755a1 | 1661 | |
d8ed45c5 | 1662 | mmap_read_lock(mm); |
a49bd4d7 MH |
1663 | err = -EFAULT; |
1664 | vma = find_vma(mm, addr); | |
1665 | if (!vma || addr < vma->vm_start || !vma_migratable(vma)) | |
1666 | goto out; | |
742755a1 | 1667 | |
a49bd4d7 MH |
1668 | /* FOLL_DUMP to ignore special (like zero) pages */ |
1669 | follflags = FOLL_GET | FOLL_DUMP; | |
a49bd4d7 | 1670 | page = follow_page(vma, addr, follflags); |
89f5b7da | 1671 | |
a49bd4d7 MH |
1672 | err = PTR_ERR(page); |
1673 | if (IS_ERR(page)) | |
1674 | goto out; | |
89f5b7da | 1675 | |
a49bd4d7 MH |
1676 | err = -ENOENT; |
1677 | if (!page) | |
1678 | goto out; | |
742755a1 | 1679 | |
a49bd4d7 MH |
1680 | err = 0; |
1681 | if (page_to_nid(page) == node) | |
1682 | goto out_putpage; | |
742755a1 | 1683 | |
a49bd4d7 MH |
1684 | err = -EACCES; |
1685 | if (page_mapcount(page) > 1 && !migrate_all) | |
1686 | goto out_putpage; | |
742755a1 | 1687 | |
a49bd4d7 MH |
1688 | if (PageHuge(page)) { |
1689 | if (PageHead(page)) { | |
1690 | isolate_huge_page(page, pagelist); | |
e0153fc2 | 1691 | err = 1; |
e632a938 | 1692 | } |
a49bd4d7 MH |
1693 | } else { |
1694 | struct page *head; | |
e632a938 | 1695 | |
e8db67eb NH |
1696 | head = compound_head(page); |
1697 | err = isolate_lru_page(head); | |
cf608ac1 | 1698 | if (err) |
a49bd4d7 | 1699 | goto out_putpage; |
742755a1 | 1700 | |
e0153fc2 | 1701 | err = 1; |
a49bd4d7 MH |
1702 | list_add_tail(&head->lru, pagelist); |
1703 | mod_node_page_state(page_pgdat(head), | |
9de4f22a | 1704 | NR_ISOLATED_ANON + page_is_file_lru(head), |
6c357848 | 1705 | thp_nr_pages(head)); |
a49bd4d7 MH |
1706 | } |
1707 | out_putpage: | |
1708 | /* | |
1709 | * Either remove the duplicate refcount from | |
1710 | * isolate_lru_page() or drop the page ref if it was | |
1711 | * not isolated. | |
1712 | */ | |
1713 | put_page(page); | |
1714 | out: | |
d8ed45c5 | 1715 | mmap_read_unlock(mm); |
742755a1 CL |
1716 | return err; |
1717 | } | |
1718 | ||
7ca8783a WY |
1719 | static int move_pages_and_store_status(struct mm_struct *mm, int node, |
1720 | struct list_head *pagelist, int __user *status, | |
1721 | int start, int i, unsigned long nr_pages) | |
1722 | { | |
1723 | int err; | |
1724 | ||
5d7ae891 WY |
1725 | if (list_empty(pagelist)) |
1726 | return 0; | |
1727 | ||
7ca8783a WY |
1728 | err = do_move_pages_to_node(mm, pagelist, node); |
1729 | if (err) { | |
1730 | /* | |
1731 | * Positive err means the number of failed | |
1732 | * pages to migrate. Since we are going to | |
1733 | * abort and return the number of non-migrated | |
ab9dd4f8 | 1734 | * pages, so need to include the rest of the |
7ca8783a WY |
1735 | * nr_pages that have not been attempted as |
1736 | * well. | |
1737 | */ | |
1738 | if (err > 0) | |
1739 | err += nr_pages - i - 1; | |
1740 | return err; | |
1741 | } | |
1742 | return store_status(status, start, node, i - start); | |
1743 | } | |
1744 | ||
5e9a0f02 BG |
1745 | /* |
1746 | * Migrate an array of page address onto an array of nodes and fill | |
1747 | * the corresponding array of status. | |
1748 | */ | |
3268c63e | 1749 | static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes, |
5e9a0f02 BG |
1750 | unsigned long nr_pages, |
1751 | const void __user * __user *pages, | |
1752 | const int __user *nodes, | |
1753 | int __user *status, int flags) | |
1754 | { | |
a49bd4d7 MH |
1755 | int current_node = NUMA_NO_NODE; |
1756 | LIST_HEAD(pagelist); | |
1757 | int start, i; | |
1758 | int err = 0, err1; | |
35282a2d | 1759 | |
361a2a22 | 1760 | lru_cache_disable(); |
35282a2d | 1761 | |
a49bd4d7 MH |
1762 | for (i = start = 0; i < nr_pages; i++) { |
1763 | const void __user *p; | |
1764 | unsigned long addr; | |
1765 | int node; | |
3140a227 | 1766 | |
a49bd4d7 MH |
1767 | err = -EFAULT; |
1768 | if (get_user(p, pages + i)) | |
1769 | goto out_flush; | |
1770 | if (get_user(node, nodes + i)) | |
1771 | goto out_flush; | |
057d3389 | 1772 | addr = (unsigned long)untagged_addr(p); |
a49bd4d7 MH |
1773 | |
1774 | err = -ENODEV; | |
1775 | if (node < 0 || node >= MAX_NUMNODES) | |
1776 | goto out_flush; | |
1777 | if (!node_state(node, N_MEMORY)) | |
1778 | goto out_flush; | |
5e9a0f02 | 1779 | |
a49bd4d7 MH |
1780 | err = -EACCES; |
1781 | if (!node_isset(node, task_nodes)) | |
1782 | goto out_flush; | |
1783 | ||
1784 | if (current_node == NUMA_NO_NODE) { | |
1785 | current_node = node; | |
1786 | start = i; | |
1787 | } else if (node != current_node) { | |
7ca8783a WY |
1788 | err = move_pages_and_store_status(mm, current_node, |
1789 | &pagelist, status, start, i, nr_pages); | |
a49bd4d7 MH |
1790 | if (err) |
1791 | goto out; | |
1792 | start = i; | |
1793 | current_node = node; | |
3140a227 BG |
1794 | } |
1795 | ||
a49bd4d7 MH |
1796 | /* |
1797 | * Errors in the page lookup or isolation are not fatal and we simply | |
1798 | * report them via status | |
1799 | */ | |
1800 | err = add_page_for_migration(mm, addr, current_node, | |
1801 | &pagelist, flags & MPOL_MF_MOVE_ALL); | |
e0153fc2 | 1802 | |
d08221a0 | 1803 | if (err > 0) { |
e0153fc2 YS |
1804 | /* The page is successfully queued for migration */ |
1805 | continue; | |
1806 | } | |
3140a227 | 1807 | |
d08221a0 WY |
1808 | /* |
1809 | * If the page is already on the target node (!err), store the | |
1810 | * node, otherwise, store the err. | |
1811 | */ | |
1812 | err = store_status(status, i, err ? : current_node, 1); | |
a49bd4d7 MH |
1813 | if (err) |
1814 | goto out_flush; | |
5e9a0f02 | 1815 | |
7ca8783a WY |
1816 | err = move_pages_and_store_status(mm, current_node, &pagelist, |
1817 | status, start, i, nr_pages); | |
4afdacec WY |
1818 | if (err) |
1819 | goto out; | |
a49bd4d7 | 1820 | current_node = NUMA_NO_NODE; |
3140a227 | 1821 | } |
a49bd4d7 MH |
1822 | out_flush: |
1823 | /* Make sure we do not overwrite the existing error */ | |
7ca8783a WY |
1824 | err1 = move_pages_and_store_status(mm, current_node, &pagelist, |
1825 | status, start, i, nr_pages); | |
dfe9aa23 | 1826 | if (err >= 0) |
a49bd4d7 | 1827 | err = err1; |
5e9a0f02 | 1828 | out: |
361a2a22 | 1829 | lru_cache_enable(); |
5e9a0f02 BG |
1830 | return err; |
1831 | } | |
1832 | ||
742755a1 | 1833 | /* |
2f007e74 | 1834 | * Determine the nodes of an array of pages and store it in an array of status. |
742755a1 | 1835 | */ |
80bba129 BG |
1836 | static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages, |
1837 | const void __user **pages, int *status) | |
742755a1 | 1838 | { |
2f007e74 | 1839 | unsigned long i; |
2f007e74 | 1840 | |
d8ed45c5 | 1841 | mmap_read_lock(mm); |
742755a1 | 1842 | |
2f007e74 | 1843 | for (i = 0; i < nr_pages; i++) { |
80bba129 | 1844 | unsigned long addr = (unsigned long)(*pages); |
742755a1 CL |
1845 | struct vm_area_struct *vma; |
1846 | struct page *page; | |
c095adbc | 1847 | int err = -EFAULT; |
2f007e74 | 1848 | |
059b8b48 LH |
1849 | vma = vma_lookup(mm, addr); |
1850 | if (!vma) | |
742755a1 CL |
1851 | goto set_status; |
1852 | ||
d899844e KS |
1853 | /* FOLL_DUMP to ignore special (like zero) pages */ |
1854 | page = follow_page(vma, addr, FOLL_DUMP); | |
89f5b7da LT |
1855 | |
1856 | err = PTR_ERR(page); | |
1857 | if (IS_ERR(page)) | |
1858 | goto set_status; | |
1859 | ||
d899844e | 1860 | err = page ? page_to_nid(page) : -ENOENT; |
742755a1 | 1861 | set_status: |
80bba129 BG |
1862 | *status = err; |
1863 | ||
1864 | pages++; | |
1865 | status++; | |
1866 | } | |
1867 | ||
d8ed45c5 | 1868 | mmap_read_unlock(mm); |
80bba129 BG |
1869 | } |
1870 | ||
1871 | /* | |
1872 | * Determine the nodes of a user array of pages and store it in | |
1873 | * a user array of status. | |
1874 | */ | |
1875 | static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages, | |
1876 | const void __user * __user *pages, | |
1877 | int __user *status) | |
1878 | { | |
1879 | #define DO_PAGES_STAT_CHUNK_NR 16 | |
1880 | const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR]; | |
1881 | int chunk_status[DO_PAGES_STAT_CHUNK_NR]; | |
80bba129 | 1882 | |
87b8d1ad PA |
1883 | while (nr_pages) { |
1884 | unsigned long chunk_nr; | |
80bba129 | 1885 | |
87b8d1ad PA |
1886 | chunk_nr = nr_pages; |
1887 | if (chunk_nr > DO_PAGES_STAT_CHUNK_NR) | |
1888 | chunk_nr = DO_PAGES_STAT_CHUNK_NR; | |
1889 | ||
1890 | if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages))) | |
1891 | break; | |
80bba129 BG |
1892 | |
1893 | do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status); | |
1894 | ||
87b8d1ad PA |
1895 | if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status))) |
1896 | break; | |
742755a1 | 1897 | |
87b8d1ad PA |
1898 | pages += chunk_nr; |
1899 | status += chunk_nr; | |
1900 | nr_pages -= chunk_nr; | |
1901 | } | |
1902 | return nr_pages ? -EFAULT : 0; | |
742755a1 CL |
1903 | } |
1904 | ||
4dc200ce | 1905 | static struct mm_struct *find_mm_struct(pid_t pid, nodemask_t *mem_nodes) |
742755a1 | 1906 | { |
742755a1 | 1907 | struct task_struct *task; |
742755a1 | 1908 | struct mm_struct *mm; |
742755a1 | 1909 | |
4dc200ce ML |
1910 | /* |
1911 | * There is no need to check if current process has the right to modify | |
1912 | * the specified process when they are same. | |
1913 | */ | |
1914 | if (!pid) { | |
1915 | mmget(current->mm); | |
1916 | *mem_nodes = cpuset_mems_allowed(current); | |
1917 | return current->mm; | |
1918 | } | |
742755a1 CL |
1919 | |
1920 | /* Find the mm_struct */ | |
a879bf58 | 1921 | rcu_read_lock(); |
4dc200ce | 1922 | task = find_task_by_vpid(pid); |
742755a1 | 1923 | if (!task) { |
a879bf58 | 1924 | rcu_read_unlock(); |
4dc200ce | 1925 | return ERR_PTR(-ESRCH); |
742755a1 | 1926 | } |
3268c63e | 1927 | get_task_struct(task); |
742755a1 CL |
1928 | |
1929 | /* | |
1930 | * Check if this process has the right to modify the specified | |
197e7e52 | 1931 | * process. Use the regular "ptrace_may_access()" checks. |
742755a1 | 1932 | */ |
197e7e52 | 1933 | if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) { |
c69e8d9c | 1934 | rcu_read_unlock(); |
4dc200ce | 1935 | mm = ERR_PTR(-EPERM); |
5e9a0f02 | 1936 | goto out; |
742755a1 | 1937 | } |
c69e8d9c | 1938 | rcu_read_unlock(); |
742755a1 | 1939 | |
4dc200ce ML |
1940 | mm = ERR_PTR(security_task_movememory(task)); |
1941 | if (IS_ERR(mm)) | |
5e9a0f02 | 1942 | goto out; |
4dc200ce | 1943 | *mem_nodes = cpuset_mems_allowed(task); |
3268c63e | 1944 | mm = get_task_mm(task); |
4dc200ce | 1945 | out: |
3268c63e | 1946 | put_task_struct(task); |
6e8b09ea | 1947 | if (!mm) |
4dc200ce ML |
1948 | mm = ERR_PTR(-EINVAL); |
1949 | return mm; | |
1950 | } | |
1951 | ||
1952 | /* | |
1953 | * Move a list of pages in the address space of the currently executing | |
1954 | * process. | |
1955 | */ | |
1956 | static int kernel_move_pages(pid_t pid, unsigned long nr_pages, | |
1957 | const void __user * __user *pages, | |
1958 | const int __user *nodes, | |
1959 | int __user *status, int flags) | |
1960 | { | |
1961 | struct mm_struct *mm; | |
1962 | int err; | |
1963 | nodemask_t task_nodes; | |
1964 | ||
1965 | /* Check flags */ | |
1966 | if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL)) | |
6e8b09ea SL |
1967 | return -EINVAL; |
1968 | ||
4dc200ce ML |
1969 | if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE)) |
1970 | return -EPERM; | |
1971 | ||
1972 | mm = find_mm_struct(pid, &task_nodes); | |
1973 | if (IS_ERR(mm)) | |
1974 | return PTR_ERR(mm); | |
1975 | ||
6e8b09ea SL |
1976 | if (nodes) |
1977 | err = do_pages_move(mm, task_nodes, nr_pages, pages, | |
1978 | nodes, status, flags); | |
1979 | else | |
1980 | err = do_pages_stat(mm, nr_pages, pages, status); | |
742755a1 | 1981 | |
742755a1 CL |
1982 | mmput(mm); |
1983 | return err; | |
1984 | } | |
742755a1 | 1985 | |
7addf443 DB |
1986 | SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages, |
1987 | const void __user * __user *, pages, | |
1988 | const int __user *, nodes, | |
1989 | int __user *, status, int, flags) | |
1990 | { | |
1991 | return kernel_move_pages(pid, nr_pages, pages, nodes, status, flags); | |
1992 | } | |
1993 | ||
1994 | #ifdef CONFIG_COMPAT | |
1995 | COMPAT_SYSCALL_DEFINE6(move_pages, pid_t, pid, compat_ulong_t, nr_pages, | |
1996 | compat_uptr_t __user *, pages32, | |
1997 | const int __user *, nodes, | |
1998 | int __user *, status, | |
1999 | int, flags) | |
2000 | { | |
2001 | const void __user * __user *pages; | |
2002 | int i; | |
2003 | ||
2004 | pages = compat_alloc_user_space(nr_pages * sizeof(void *)); | |
2005 | for (i = 0; i < nr_pages; i++) { | |
2006 | compat_uptr_t p; | |
2007 | ||
2008 | if (get_user(p, pages32 + i) || | |
2009 | put_user(compat_ptr(p), pages + i)) | |
2010 | return -EFAULT; | |
2011 | } | |
2012 | return kernel_move_pages(pid, nr_pages, pages, nodes, status, flags); | |
2013 | } | |
2014 | #endif /* CONFIG_COMPAT */ | |
2015 | ||
7039e1db PZ |
2016 | #ifdef CONFIG_NUMA_BALANCING |
2017 | /* | |
2018 | * Returns true if this is a safe migration target node for misplaced NUMA | |
2019 | * pages. Currently it only checks the watermarks which crude | |
2020 | */ | |
2021 | static bool migrate_balanced_pgdat(struct pglist_data *pgdat, | |
3abef4e6 | 2022 | unsigned long nr_migrate_pages) |
7039e1db PZ |
2023 | { |
2024 | int z; | |
599d0c95 | 2025 | |
7039e1db PZ |
2026 | for (z = pgdat->nr_zones - 1; z >= 0; z--) { |
2027 | struct zone *zone = pgdat->node_zones + z; | |
2028 | ||
2029 | if (!populated_zone(zone)) | |
2030 | continue; | |
2031 | ||
7039e1db PZ |
2032 | /* Avoid waking kswapd by allocating pages_to_migrate pages. */ |
2033 | if (!zone_watermark_ok(zone, 0, | |
2034 | high_wmark_pages(zone) + | |
2035 | nr_migrate_pages, | |
bfe9d006 | 2036 | ZONE_MOVABLE, 0)) |
7039e1db PZ |
2037 | continue; |
2038 | return true; | |
2039 | } | |
2040 | return false; | |
2041 | } | |
2042 | ||
2043 | static struct page *alloc_misplaced_dst_page(struct page *page, | |
666feb21 | 2044 | unsigned long data) |
7039e1db PZ |
2045 | { |
2046 | int nid = (int) data; | |
2047 | struct page *newpage; | |
2048 | ||
96db800f | 2049 | newpage = __alloc_pages_node(nid, |
e97ca8e5 JW |
2050 | (GFP_HIGHUSER_MOVABLE | |
2051 | __GFP_THISNODE | __GFP_NOMEMALLOC | | |
2052 | __GFP_NORETRY | __GFP_NOWARN) & | |
8479eba7 | 2053 | ~__GFP_RECLAIM, 0); |
bac0382c | 2054 | |
7039e1db PZ |
2055 | return newpage; |
2056 | } | |
2057 | ||
c5b5a3dd YS |
2058 | static struct page *alloc_misplaced_dst_page_thp(struct page *page, |
2059 | unsigned long data) | |
2060 | { | |
2061 | int nid = (int) data; | |
2062 | struct page *newpage; | |
2063 | ||
2064 | newpage = alloc_pages_node(nid, (GFP_TRANSHUGE_LIGHT | __GFP_THISNODE), | |
2065 | HPAGE_PMD_ORDER); | |
2066 | if (!newpage) | |
2067 | goto out; | |
2068 | ||
2069 | prep_transhuge_page(newpage); | |
2070 | ||
2071 | out: | |
2072 | return newpage; | |
2073 | } | |
2074 | ||
1c30e017 | 2075 | static int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page) |
b32967ff | 2076 | { |
340ef390 | 2077 | int page_lru; |
a8f60772 | 2078 | |
309381fe | 2079 | VM_BUG_ON_PAGE(compound_order(page) && !PageTransHuge(page), page); |
3abef4e6 | 2080 | |
662aeea7 YS |
2081 | /* Do not migrate THP mapped by multiple processes */ |
2082 | if (PageTransHuge(page) && total_mapcount(page) > 1) | |
2083 | return 0; | |
2084 | ||
7039e1db | 2085 | /* Avoid migrating to a node that is nearly full */ |
d8c6546b | 2086 | if (!migrate_balanced_pgdat(pgdat, compound_nr(page))) |
340ef390 | 2087 | return 0; |
7039e1db | 2088 | |
340ef390 HD |
2089 | if (isolate_lru_page(page)) |
2090 | return 0; | |
7039e1db | 2091 | |
9de4f22a | 2092 | page_lru = page_is_file_lru(page); |
599d0c95 | 2093 | mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + page_lru, |
6c357848 | 2094 | thp_nr_pages(page)); |
340ef390 | 2095 | |
149c33e1 | 2096 | /* |
340ef390 HD |
2097 | * Isolating the page has taken another reference, so the |
2098 | * caller's reference can be safely dropped without the page | |
2099 | * disappearing underneath us during migration. | |
149c33e1 MG |
2100 | */ |
2101 | put_page(page); | |
340ef390 | 2102 | return 1; |
b32967ff MG |
2103 | } |
2104 | ||
2105 | /* | |
2106 | * Attempt to migrate a misplaced page to the specified destination | |
2107 | * node. Caller is expected to have an elevated reference count on | |
2108 | * the page that will be dropped by this function before returning. | |
2109 | */ | |
1bc115d8 MG |
2110 | int migrate_misplaced_page(struct page *page, struct vm_area_struct *vma, |
2111 | int node) | |
b32967ff MG |
2112 | { |
2113 | pg_data_t *pgdat = NODE_DATA(node); | |
340ef390 | 2114 | int isolated; |
b32967ff MG |
2115 | int nr_remaining; |
2116 | LIST_HEAD(migratepages); | |
c5b5a3dd YS |
2117 | new_page_t *new; |
2118 | bool compound; | |
c5fc5c3a | 2119 | unsigned int nr_pages = thp_nr_pages(page); |
c5b5a3dd YS |
2120 | |
2121 | /* | |
2122 | * PTE mapped THP or HugeTLB page can't reach here so the page could | |
2123 | * be either base page or THP. And it must be head page if it is | |
2124 | * THP. | |
2125 | */ | |
2126 | compound = PageTransHuge(page); | |
2127 | ||
2128 | if (compound) | |
2129 | new = alloc_misplaced_dst_page_thp; | |
2130 | else | |
2131 | new = alloc_misplaced_dst_page; | |
b32967ff MG |
2132 | |
2133 | /* | |
1bc115d8 MG |
2134 | * Don't migrate file pages that are mapped in multiple processes |
2135 | * with execute permissions as they are probably shared libraries. | |
b32967ff | 2136 | */ |
7ee820ee ML |
2137 | if (page_mapcount(page) != 1 && page_is_file_lru(page) && |
2138 | (vma->vm_flags & VM_EXEC)) | |
b32967ff | 2139 | goto out; |
b32967ff | 2140 | |
09a913a7 MG |
2141 | /* |
2142 | * Also do not migrate dirty pages as not all filesystems can move | |
2143 | * dirty pages in MIGRATE_ASYNC mode which is a waste of cycles. | |
2144 | */ | |
9de4f22a | 2145 | if (page_is_file_lru(page) && PageDirty(page)) |
09a913a7 MG |
2146 | goto out; |
2147 | ||
b32967ff MG |
2148 | isolated = numamigrate_isolate_page(pgdat, page); |
2149 | if (!isolated) | |
2150 | goto out; | |
2151 | ||
2152 | list_add(&page->lru, &migratepages); | |
c5b5a3dd YS |
2153 | nr_remaining = migrate_pages(&migratepages, *new, NULL, node, |
2154 | MIGRATE_ASYNC, MR_NUMA_MISPLACED); | |
b32967ff | 2155 | if (nr_remaining) { |
59c82b70 JK |
2156 | if (!list_empty(&migratepages)) { |
2157 | list_del(&page->lru); | |
c5fc5c3a YS |
2158 | mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + |
2159 | page_is_file_lru(page), -nr_pages); | |
59c82b70 JK |
2160 | putback_lru_page(page); |
2161 | } | |
b32967ff MG |
2162 | isolated = 0; |
2163 | } else | |
c5fc5c3a | 2164 | count_vm_numa_events(NUMA_PAGE_MIGRATE, nr_pages); |
7039e1db | 2165 | BUG_ON(!list_empty(&migratepages)); |
7039e1db | 2166 | return isolated; |
340ef390 HD |
2167 | |
2168 | out: | |
2169 | put_page(page); | |
2170 | return 0; | |
7039e1db | 2171 | } |
220018d3 | 2172 | #endif /* CONFIG_NUMA_BALANCING */ |
7039e1db | 2173 | #endif /* CONFIG_NUMA */ |
8763cb45 | 2174 | |
9b2ed9cb | 2175 | #ifdef CONFIG_DEVICE_PRIVATE |
843e1be1 | 2176 | static int migrate_vma_collect_skip(unsigned long start, |
8763cb45 JG |
2177 | unsigned long end, |
2178 | struct mm_walk *walk) | |
2179 | { | |
2180 | struct migrate_vma *migrate = walk->private; | |
2181 | unsigned long addr; | |
2182 | ||
872ea707 | 2183 | for (addr = start; addr < end; addr += PAGE_SIZE) { |
8315ada7 | 2184 | migrate->dst[migrate->npages] = 0; |
843e1be1 | 2185 | migrate->src[migrate->npages++] = 0; |
8315ada7 JG |
2186 | } |
2187 | ||
2188 | return 0; | |
2189 | } | |
2190 | ||
843e1be1 | 2191 | static int migrate_vma_collect_hole(unsigned long start, |
8315ada7 | 2192 | unsigned long end, |
843e1be1 | 2193 | __always_unused int depth, |
8315ada7 JG |
2194 | struct mm_walk *walk) |
2195 | { | |
2196 | struct migrate_vma *migrate = walk->private; | |
2197 | unsigned long addr; | |
2198 | ||
843e1be1 ML |
2199 | /* Only allow populating anonymous memory. */ |
2200 | if (!vma_is_anonymous(walk->vma)) | |
2201 | return migrate_vma_collect_skip(start, end, walk); | |
2202 | ||
872ea707 | 2203 | for (addr = start; addr < end; addr += PAGE_SIZE) { |
843e1be1 | 2204 | migrate->src[migrate->npages] = MIGRATE_PFN_MIGRATE; |
8763cb45 | 2205 | migrate->dst[migrate->npages] = 0; |
843e1be1 ML |
2206 | migrate->npages++; |
2207 | migrate->cpages++; | |
8763cb45 JG |
2208 | } |
2209 | ||
2210 | return 0; | |
2211 | } | |
2212 | ||
2213 | static int migrate_vma_collect_pmd(pmd_t *pmdp, | |
2214 | unsigned long start, | |
2215 | unsigned long end, | |
2216 | struct mm_walk *walk) | |
2217 | { | |
2218 | struct migrate_vma *migrate = walk->private; | |
2219 | struct vm_area_struct *vma = walk->vma; | |
2220 | struct mm_struct *mm = vma->vm_mm; | |
8c3328f1 | 2221 | unsigned long addr = start, unmapped = 0; |
8763cb45 JG |
2222 | spinlock_t *ptl; |
2223 | pte_t *ptep; | |
2224 | ||
2225 | again: | |
2226 | if (pmd_none(*pmdp)) | |
b7a16c7a | 2227 | return migrate_vma_collect_hole(start, end, -1, walk); |
8763cb45 JG |
2228 | |
2229 | if (pmd_trans_huge(*pmdp)) { | |
2230 | struct page *page; | |
2231 | ||
2232 | ptl = pmd_lock(mm, pmdp); | |
2233 | if (unlikely(!pmd_trans_huge(*pmdp))) { | |
2234 | spin_unlock(ptl); | |
2235 | goto again; | |
2236 | } | |
2237 | ||
2238 | page = pmd_page(*pmdp); | |
2239 | if (is_huge_zero_page(page)) { | |
2240 | spin_unlock(ptl); | |
2241 | split_huge_pmd(vma, pmdp, addr); | |
2242 | if (pmd_trans_unstable(pmdp)) | |
8315ada7 | 2243 | return migrate_vma_collect_skip(start, end, |
8763cb45 JG |
2244 | walk); |
2245 | } else { | |
2246 | int ret; | |
2247 | ||
2248 | get_page(page); | |
2249 | spin_unlock(ptl); | |
2250 | if (unlikely(!trylock_page(page))) | |
8315ada7 | 2251 | return migrate_vma_collect_skip(start, end, |
8763cb45 JG |
2252 | walk); |
2253 | ret = split_huge_page(page); | |
2254 | unlock_page(page); | |
2255 | put_page(page); | |
8315ada7 JG |
2256 | if (ret) |
2257 | return migrate_vma_collect_skip(start, end, | |
2258 | walk); | |
2259 | if (pmd_none(*pmdp)) | |
b7a16c7a | 2260 | return migrate_vma_collect_hole(start, end, -1, |
8763cb45 JG |
2261 | walk); |
2262 | } | |
2263 | } | |
2264 | ||
2265 | if (unlikely(pmd_bad(*pmdp))) | |
8315ada7 | 2266 | return migrate_vma_collect_skip(start, end, walk); |
8763cb45 JG |
2267 | |
2268 | ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl); | |
8c3328f1 JG |
2269 | arch_enter_lazy_mmu_mode(); |
2270 | ||
8763cb45 | 2271 | for (; addr < end; addr += PAGE_SIZE, ptep++) { |
800bb1c8 | 2272 | unsigned long mpfn = 0, pfn; |
8763cb45 | 2273 | struct page *page; |
8c3328f1 | 2274 | swp_entry_t entry; |
8763cb45 JG |
2275 | pte_t pte; |
2276 | ||
2277 | pte = *ptep; | |
8763cb45 | 2278 | |
a5430dda | 2279 | if (pte_none(pte)) { |
0744f280 RC |
2280 | if (vma_is_anonymous(vma)) { |
2281 | mpfn = MIGRATE_PFN_MIGRATE; | |
2282 | migrate->cpages++; | |
2283 | } | |
8763cb45 JG |
2284 | goto next; |
2285 | } | |
2286 | ||
a5430dda | 2287 | if (!pte_present(pte)) { |
a5430dda JG |
2288 | /* |
2289 | * Only care about unaddressable device page special | |
2290 | * page table entry. Other special swap entries are not | |
2291 | * migratable, and we ignore regular swapped page. | |
2292 | */ | |
2293 | entry = pte_to_swp_entry(pte); | |
2294 | if (!is_device_private_entry(entry)) | |
2295 | goto next; | |
2296 | ||
af5cdaf8 | 2297 | page = pfn_swap_entry_to_page(entry); |
5143192c RC |
2298 | if (!(migrate->flags & |
2299 | MIGRATE_VMA_SELECT_DEVICE_PRIVATE) || | |
2300 | page->pgmap->owner != migrate->pgmap_owner) | |
800bb1c8 CH |
2301 | goto next; |
2302 | ||
06d462be CH |
2303 | mpfn = migrate_pfn(page_to_pfn(page)) | |
2304 | MIGRATE_PFN_MIGRATE; | |
4dd845b5 | 2305 | if (is_writable_device_private_entry(entry)) |
a5430dda JG |
2306 | mpfn |= MIGRATE_PFN_WRITE; |
2307 | } else { | |
5143192c | 2308 | if (!(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM)) |
800bb1c8 | 2309 | goto next; |
276f756d | 2310 | pfn = pte_pfn(pte); |
8315ada7 JG |
2311 | if (is_zero_pfn(pfn)) { |
2312 | mpfn = MIGRATE_PFN_MIGRATE; | |
2313 | migrate->cpages++; | |
8315ada7 JG |
2314 | goto next; |
2315 | } | |
25b2995a | 2316 | page = vm_normal_page(migrate->vma, addr, pte); |
a5430dda JG |
2317 | mpfn = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE; |
2318 | mpfn |= pte_write(pte) ? MIGRATE_PFN_WRITE : 0; | |
2319 | } | |
2320 | ||
8763cb45 | 2321 | /* FIXME support THP */ |
8763cb45 | 2322 | if (!page || !page->mapping || PageTransCompound(page)) { |
276f756d | 2323 | mpfn = 0; |
8763cb45 JG |
2324 | goto next; |
2325 | } | |
2326 | ||
2327 | /* | |
2328 | * By getting a reference on the page we pin it and that blocks | |
2329 | * any kind of migration. Side effect is that it "freezes" the | |
2330 | * pte. | |
2331 | * | |
2332 | * We drop this reference after isolating the page from the lru | |
2333 | * for non device page (device page are not on the lru and thus | |
2334 | * can't be dropped from it). | |
2335 | */ | |
2336 | get_page(page); | |
2337 | migrate->cpages++; | |
8763cb45 | 2338 | |
8c3328f1 JG |
2339 | /* |
2340 | * Optimize for the common case where page is only mapped once | |
2341 | * in one process. If we can lock the page, then we can safely | |
2342 | * set up a special migration page table entry now. | |
2343 | */ | |
2344 | if (trylock_page(page)) { | |
2345 | pte_t swp_pte; | |
2346 | ||
2347 | mpfn |= MIGRATE_PFN_LOCKED; | |
2348 | ptep_get_and_clear(mm, addr, ptep); | |
2349 | ||
2350 | /* Setup special migration page table entry */ | |
4dd845b5 AP |
2351 | if (mpfn & MIGRATE_PFN_WRITE) |
2352 | entry = make_writable_migration_entry( | |
2353 | page_to_pfn(page)); | |
2354 | else | |
2355 | entry = make_readable_migration_entry( | |
2356 | page_to_pfn(page)); | |
8c3328f1 | 2357 | swp_pte = swp_entry_to_pte(entry); |
ad7df764 AP |
2358 | if (pte_present(pte)) { |
2359 | if (pte_soft_dirty(pte)) | |
2360 | swp_pte = pte_swp_mksoft_dirty(swp_pte); | |
2361 | if (pte_uffd_wp(pte)) | |
2362 | swp_pte = pte_swp_mkuffd_wp(swp_pte); | |
2363 | } else { | |
2364 | if (pte_swp_soft_dirty(pte)) | |
2365 | swp_pte = pte_swp_mksoft_dirty(swp_pte); | |
2366 | if (pte_swp_uffd_wp(pte)) | |
2367 | swp_pte = pte_swp_mkuffd_wp(swp_pte); | |
2368 | } | |
8c3328f1 JG |
2369 | set_pte_at(mm, addr, ptep, swp_pte); |
2370 | ||
2371 | /* | |
2372 | * This is like regular unmap: we remove the rmap and | |
2373 | * drop page refcount. Page won't be freed, as we took | |
2374 | * a reference just above. | |
2375 | */ | |
2376 | page_remove_rmap(page, false); | |
2377 | put_page(page); | |
a5430dda JG |
2378 | |
2379 | if (pte_present(pte)) | |
2380 | unmapped++; | |
8c3328f1 JG |
2381 | } |
2382 | ||
8763cb45 | 2383 | next: |
a5430dda | 2384 | migrate->dst[migrate->npages] = 0; |
8763cb45 JG |
2385 | migrate->src[migrate->npages++] = mpfn; |
2386 | } | |
8c3328f1 | 2387 | arch_leave_lazy_mmu_mode(); |
8763cb45 JG |
2388 | pte_unmap_unlock(ptep - 1, ptl); |
2389 | ||
8c3328f1 JG |
2390 | /* Only flush the TLB if we actually modified any entries */ |
2391 | if (unmapped) | |
2392 | flush_tlb_range(walk->vma, start, end); | |
2393 | ||
8763cb45 JG |
2394 | return 0; |
2395 | } | |
2396 | ||
7b86ac33 CH |
2397 | static const struct mm_walk_ops migrate_vma_walk_ops = { |
2398 | .pmd_entry = migrate_vma_collect_pmd, | |
2399 | .pte_hole = migrate_vma_collect_hole, | |
2400 | }; | |
2401 | ||
8763cb45 JG |
2402 | /* |
2403 | * migrate_vma_collect() - collect pages over a range of virtual addresses | |
2404 | * @migrate: migrate struct containing all migration information | |
2405 | * | |
2406 | * This will walk the CPU page table. For each virtual address backed by a | |
2407 | * valid page, it updates the src array and takes a reference on the page, in | |
2408 | * order to pin the page until we lock it and unmap it. | |
2409 | */ | |
2410 | static void migrate_vma_collect(struct migrate_vma *migrate) | |
2411 | { | |
ac46d4f3 | 2412 | struct mmu_notifier_range range; |
8763cb45 | 2413 | |
998427b3 RC |
2414 | /* |
2415 | * Note that the pgmap_owner is passed to the mmu notifier callback so | |
2416 | * that the registered device driver can skip invalidating device | |
2417 | * private page mappings that won't be migrated. | |
2418 | */ | |
6b49bf6d AP |
2419 | mmu_notifier_range_init_owner(&range, MMU_NOTIFY_MIGRATE, 0, |
2420 | migrate->vma, migrate->vma->vm_mm, migrate->start, migrate->end, | |
c1a06df6 | 2421 | migrate->pgmap_owner); |
ac46d4f3 | 2422 | mmu_notifier_invalidate_range_start(&range); |
8763cb45 | 2423 | |
7b86ac33 CH |
2424 | walk_page_range(migrate->vma->vm_mm, migrate->start, migrate->end, |
2425 | &migrate_vma_walk_ops, migrate); | |
2426 | ||
2427 | mmu_notifier_invalidate_range_end(&range); | |
8763cb45 JG |
2428 | migrate->end = migrate->start + (migrate->npages << PAGE_SHIFT); |
2429 | } | |
2430 | ||
2431 | /* | |
2432 | * migrate_vma_check_page() - check if page is pinned or not | |
2433 | * @page: struct page to check | |
2434 | * | |
2435 | * Pinned pages cannot be migrated. This is the same test as in | |
2436 | * migrate_page_move_mapping(), except that here we allow migration of a | |
2437 | * ZONE_DEVICE page. | |
2438 | */ | |
2439 | static bool migrate_vma_check_page(struct page *page) | |
2440 | { | |
2441 | /* | |
2442 | * One extra ref because caller holds an extra reference, either from | |
2443 | * isolate_lru_page() for a regular page, or migrate_vma_collect() for | |
2444 | * a device page. | |
2445 | */ | |
2446 | int extra = 1; | |
2447 | ||
2448 | /* | |
2449 | * FIXME support THP (transparent huge page), it is bit more complex to | |
2450 | * check them than regular pages, because they can be mapped with a pmd | |
2451 | * or with a pte (split pte mapping). | |
2452 | */ | |
2453 | if (PageCompound(page)) | |
2454 | return false; | |
2455 | ||
a5430dda JG |
2456 | /* Page from ZONE_DEVICE have one extra reference */ |
2457 | if (is_zone_device_page(page)) { | |
2458 | /* | |
2459 | * Private page can never be pin as they have no valid pte and | |
2460 | * GUP will fail for those. Yet if there is a pending migration | |
2461 | * a thread might try to wait on the pte migration entry and | |
2462 | * will bump the page reference count. Sadly there is no way to | |
2463 | * differentiate a regular pin from migration wait. Hence to | |
2464 | * avoid 2 racing thread trying to migrate back to CPU to enter | |
8958b249 | 2465 | * infinite loop (one stopping migration because the other is |
a5430dda JG |
2466 | * waiting on pte migration entry). We always return true here. |
2467 | * | |
2468 | * FIXME proper solution is to rework migration_entry_wait() so | |
2469 | * it does not need to take a reference on page. | |
2470 | */ | |
25b2995a | 2471 | return is_device_private_page(page); |
a5430dda JG |
2472 | } |
2473 | ||
df6ad698 JG |
2474 | /* For file back page */ |
2475 | if (page_mapping(page)) | |
2476 | extra += 1 + page_has_private(page); | |
2477 | ||
8763cb45 JG |
2478 | if ((page_count(page) - extra) > page_mapcount(page)) |
2479 | return false; | |
2480 | ||
2481 | return true; | |
2482 | } | |
2483 | ||
2484 | /* | |
2485 | * migrate_vma_prepare() - lock pages and isolate them from the lru | |
2486 | * @migrate: migrate struct containing all migration information | |
2487 | * | |
2488 | * This locks pages that have been collected by migrate_vma_collect(). Once each | |
2489 | * page is locked it is isolated from the lru (for non-device pages). Finally, | |
2490 | * the ref taken by migrate_vma_collect() is dropped, as locked pages cannot be | |
2491 | * migrated by concurrent kernel threads. | |
2492 | */ | |
2493 | static void migrate_vma_prepare(struct migrate_vma *migrate) | |
2494 | { | |
2495 | const unsigned long npages = migrate->npages; | |
8c3328f1 JG |
2496 | const unsigned long start = migrate->start; |
2497 | unsigned long addr, i, restore = 0; | |
8763cb45 | 2498 | bool allow_drain = true; |
8763cb45 JG |
2499 | |
2500 | lru_add_drain(); | |
2501 | ||
2502 | for (i = 0; (i < npages) && migrate->cpages; i++) { | |
2503 | struct page *page = migrate_pfn_to_page(migrate->src[i]); | |
8c3328f1 | 2504 | bool remap = true; |
8763cb45 JG |
2505 | |
2506 | if (!page) | |
2507 | continue; | |
2508 | ||
8c3328f1 JG |
2509 | if (!(migrate->src[i] & MIGRATE_PFN_LOCKED)) { |
2510 | /* | |
2511 | * Because we are migrating several pages there can be | |
2512 | * a deadlock between 2 concurrent migration where each | |
2513 | * are waiting on each other page lock. | |
2514 | * | |
2515 | * Make migrate_vma() a best effort thing and backoff | |
2516 | * for any page we can not lock right away. | |
2517 | */ | |
2518 | if (!trylock_page(page)) { | |
2519 | migrate->src[i] = 0; | |
2520 | migrate->cpages--; | |
2521 | put_page(page); | |
2522 | continue; | |
2523 | } | |
2524 | remap = false; | |
2525 | migrate->src[i] |= MIGRATE_PFN_LOCKED; | |
8763cb45 | 2526 | } |
8763cb45 | 2527 | |
a5430dda JG |
2528 | /* ZONE_DEVICE pages are not on LRU */ |
2529 | if (!is_zone_device_page(page)) { | |
2530 | if (!PageLRU(page) && allow_drain) { | |
2531 | /* Drain CPU's pagevec */ | |
2532 | lru_add_drain_all(); | |
2533 | allow_drain = false; | |
2534 | } | |
8763cb45 | 2535 | |
a5430dda JG |
2536 | if (isolate_lru_page(page)) { |
2537 | if (remap) { | |
2538 | migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; | |
2539 | migrate->cpages--; | |
2540 | restore++; | |
2541 | } else { | |
2542 | migrate->src[i] = 0; | |
2543 | unlock_page(page); | |
2544 | migrate->cpages--; | |
2545 | put_page(page); | |
2546 | } | |
2547 | continue; | |
8c3328f1 | 2548 | } |
a5430dda JG |
2549 | |
2550 | /* Drop the reference we took in collect */ | |
2551 | put_page(page); | |
8763cb45 JG |
2552 | } |
2553 | ||
2554 | if (!migrate_vma_check_page(page)) { | |
8c3328f1 JG |
2555 | if (remap) { |
2556 | migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; | |
2557 | migrate->cpages--; | |
2558 | restore++; | |
8763cb45 | 2559 | |
a5430dda JG |
2560 | if (!is_zone_device_page(page)) { |
2561 | get_page(page); | |
2562 | putback_lru_page(page); | |
2563 | } | |
8c3328f1 JG |
2564 | } else { |
2565 | migrate->src[i] = 0; | |
2566 | unlock_page(page); | |
2567 | migrate->cpages--; | |
2568 | ||
a5430dda JG |
2569 | if (!is_zone_device_page(page)) |
2570 | putback_lru_page(page); | |
2571 | else | |
2572 | put_page(page); | |
8c3328f1 | 2573 | } |
8763cb45 JG |
2574 | } |
2575 | } | |
8c3328f1 JG |
2576 | |
2577 | for (i = 0, addr = start; i < npages && restore; i++, addr += PAGE_SIZE) { | |
2578 | struct page *page = migrate_pfn_to_page(migrate->src[i]); | |
2579 | ||
2580 | if (!page || (migrate->src[i] & MIGRATE_PFN_MIGRATE)) | |
2581 | continue; | |
2582 | ||
2583 | remove_migration_pte(page, migrate->vma, addr, page); | |
2584 | ||
2585 | migrate->src[i] = 0; | |
2586 | unlock_page(page); | |
2587 | put_page(page); | |
2588 | restore--; | |
2589 | } | |
8763cb45 JG |
2590 | } |
2591 | ||
2592 | /* | |
2593 | * migrate_vma_unmap() - replace page mapping with special migration pte entry | |
2594 | * @migrate: migrate struct containing all migration information | |
2595 | * | |
2596 | * Replace page mapping (CPU page table pte) with a special migration pte entry | |
2597 | * and check again if it has been pinned. Pinned pages are restored because we | |
2598 | * cannot migrate them. | |
2599 | * | |
2600 | * This is the last step before we call the device driver callback to allocate | |
2601 | * destination memory and copy contents of original page over to new page. | |
2602 | */ | |
2603 | static void migrate_vma_unmap(struct migrate_vma *migrate) | |
2604 | { | |
8763cb45 JG |
2605 | const unsigned long npages = migrate->npages; |
2606 | const unsigned long start = migrate->start; | |
2607 | unsigned long addr, i, restore = 0; | |
2608 | ||
2609 | for (i = 0; i < npages; i++) { | |
2610 | struct page *page = migrate_pfn_to_page(migrate->src[i]); | |
2611 | ||
2612 | if (!page || !(migrate->src[i] & MIGRATE_PFN_MIGRATE)) | |
2613 | continue; | |
2614 | ||
8c3328f1 | 2615 | if (page_mapped(page)) { |
a98a2f0c | 2616 | try_to_migrate(page, 0); |
8c3328f1 JG |
2617 | if (page_mapped(page)) |
2618 | goto restore; | |
8763cb45 | 2619 | } |
8c3328f1 JG |
2620 | |
2621 | if (migrate_vma_check_page(page)) | |
2622 | continue; | |
2623 | ||
2624 | restore: | |
2625 | migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; | |
2626 | migrate->cpages--; | |
2627 | restore++; | |
8763cb45 JG |
2628 | } |
2629 | ||
2630 | for (addr = start, i = 0; i < npages && restore; addr += PAGE_SIZE, i++) { | |
2631 | struct page *page = migrate_pfn_to_page(migrate->src[i]); | |
2632 | ||
2633 | if (!page || (migrate->src[i] & MIGRATE_PFN_MIGRATE)) | |
2634 | continue; | |
2635 | ||
2636 | remove_migration_ptes(page, page, false); | |
2637 | ||
2638 | migrate->src[i] = 0; | |
2639 | unlock_page(page); | |
2640 | restore--; | |
2641 | ||
a5430dda JG |
2642 | if (is_zone_device_page(page)) |
2643 | put_page(page); | |
2644 | else | |
2645 | putback_lru_page(page); | |
8763cb45 JG |
2646 | } |
2647 | } | |
2648 | ||
a7d1f22b CH |
2649 | /** |
2650 | * migrate_vma_setup() - prepare to migrate a range of memory | |
eaf444de | 2651 | * @args: contains the vma, start, and pfns arrays for the migration |
a7d1f22b CH |
2652 | * |
2653 | * Returns: negative errno on failures, 0 when 0 or more pages were migrated | |
2654 | * without an error. | |
2655 | * | |
2656 | * Prepare to migrate a range of memory virtual address range by collecting all | |
2657 | * the pages backing each virtual address in the range, saving them inside the | |
2658 | * src array. Then lock those pages and unmap them. Once the pages are locked | |
2659 | * and unmapped, check whether each page is pinned or not. Pages that aren't | |
2660 | * pinned have the MIGRATE_PFN_MIGRATE flag set (by this function) in the | |
2661 | * corresponding src array entry. Then restores any pages that are pinned, by | |
2662 | * remapping and unlocking those pages. | |
2663 | * | |
2664 | * The caller should then allocate destination memory and copy source memory to | |
2665 | * it for all those entries (ie with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE | |
2666 | * flag set). Once these are allocated and copied, the caller must update each | |
2667 | * corresponding entry in the dst array with the pfn value of the destination | |
2668 | * page and with the MIGRATE_PFN_VALID and MIGRATE_PFN_LOCKED flags set | |
2669 | * (destination pages must have their struct pages locked, via lock_page()). | |
2670 | * | |
2671 | * Note that the caller does not have to migrate all the pages that are marked | |
2672 | * with MIGRATE_PFN_MIGRATE flag in src array unless this is a migration from | |
2673 | * device memory to system memory. If the caller cannot migrate a device page | |
2674 | * back to system memory, then it must return VM_FAULT_SIGBUS, which has severe | |
2675 | * consequences for the userspace process, so it must be avoided if at all | |
2676 | * possible. | |
2677 | * | |
2678 | * For empty entries inside CPU page table (pte_none() or pmd_none() is true) we | |
2679 | * do set MIGRATE_PFN_MIGRATE flag inside the corresponding source array thus | |
f0953a1b IM |
2680 | * allowing the caller to allocate device memory for those unbacked virtual |
2681 | * addresses. For this the caller simply has to allocate device memory and | |
a7d1f22b | 2682 | * properly set the destination entry like for regular migration. Note that |
f0953a1b IM |
2683 | * this can still fail, and thus inside the device driver you must check if the |
2684 | * migration was successful for those entries after calling migrate_vma_pages(), | |
a7d1f22b CH |
2685 | * just like for regular migration. |
2686 | * | |
2687 | * After that, the callers must call migrate_vma_pages() to go over each entry | |
2688 | * in the src array that has the MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag | |
2689 | * set. If the corresponding entry in dst array has MIGRATE_PFN_VALID flag set, | |
2690 | * then migrate_vma_pages() to migrate struct page information from the source | |
2691 | * struct page to the destination struct page. If it fails to migrate the | |
2692 | * struct page information, then it clears the MIGRATE_PFN_MIGRATE flag in the | |
2693 | * src array. | |
2694 | * | |
2695 | * At this point all successfully migrated pages have an entry in the src | |
2696 | * array with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag set and the dst | |
2697 | * array entry with MIGRATE_PFN_VALID flag set. | |
2698 | * | |
2699 | * Once migrate_vma_pages() returns the caller may inspect which pages were | |
2700 | * successfully migrated, and which were not. Successfully migrated pages will | |
2701 | * have the MIGRATE_PFN_MIGRATE flag set for their src array entry. | |
2702 | * | |
2703 | * It is safe to update device page table after migrate_vma_pages() because | |
c1e8d7c6 | 2704 | * both destination and source page are still locked, and the mmap_lock is held |
a7d1f22b CH |
2705 | * in read mode (hence no one can unmap the range being migrated). |
2706 | * | |
2707 | * Once the caller is done cleaning up things and updating its page table (if it | |
2708 | * chose to do so, this is not an obligation) it finally calls | |
2709 | * migrate_vma_finalize() to update the CPU page table to point to new pages | |
2710 | * for successfully migrated pages or otherwise restore the CPU page table to | |
2711 | * point to the original source pages. | |
2712 | */ | |
2713 | int migrate_vma_setup(struct migrate_vma *args) | |
2714 | { | |
2715 | long nr_pages = (args->end - args->start) >> PAGE_SHIFT; | |
2716 | ||
2717 | args->start &= PAGE_MASK; | |
2718 | args->end &= PAGE_MASK; | |
2719 | if (!args->vma || is_vm_hugetlb_page(args->vma) || | |
2720 | (args->vma->vm_flags & VM_SPECIAL) || vma_is_dax(args->vma)) | |
2721 | return -EINVAL; | |
2722 | if (nr_pages <= 0) | |
2723 | return -EINVAL; | |
2724 | if (args->start < args->vma->vm_start || | |
2725 | args->start >= args->vma->vm_end) | |
2726 | return -EINVAL; | |
2727 | if (args->end <= args->vma->vm_start || args->end > args->vma->vm_end) | |
2728 | return -EINVAL; | |
2729 | if (!args->src || !args->dst) | |
2730 | return -EINVAL; | |
2731 | ||
2732 | memset(args->src, 0, sizeof(*args->src) * nr_pages); | |
2733 | args->cpages = 0; | |
2734 | args->npages = 0; | |
2735 | ||
2736 | migrate_vma_collect(args); | |
2737 | ||
2738 | if (args->cpages) | |
2739 | migrate_vma_prepare(args); | |
2740 | if (args->cpages) | |
2741 | migrate_vma_unmap(args); | |
2742 | ||
2743 | /* | |
2744 | * At this point pages are locked and unmapped, and thus they have | |
2745 | * stable content and can safely be copied to destination memory that | |
2746 | * is allocated by the drivers. | |
2747 | */ | |
2748 | return 0; | |
2749 | ||
2750 | } | |
2751 | EXPORT_SYMBOL(migrate_vma_setup); | |
2752 | ||
34290e2c RC |
2753 | /* |
2754 | * This code closely matches the code in: | |
2755 | * __handle_mm_fault() | |
2756 | * handle_pte_fault() | |
2757 | * do_anonymous_page() | |
2758 | * to map in an anonymous zero page but the struct page will be a ZONE_DEVICE | |
2759 | * private page. | |
2760 | */ | |
8315ada7 JG |
2761 | static void migrate_vma_insert_page(struct migrate_vma *migrate, |
2762 | unsigned long addr, | |
2763 | struct page *page, | |
d85c6db4 | 2764 | unsigned long *src) |
8315ada7 JG |
2765 | { |
2766 | struct vm_area_struct *vma = migrate->vma; | |
2767 | struct mm_struct *mm = vma->vm_mm; | |
8315ada7 JG |
2768 | bool flush = false; |
2769 | spinlock_t *ptl; | |
2770 | pte_t entry; | |
2771 | pgd_t *pgdp; | |
2772 | p4d_t *p4dp; | |
2773 | pud_t *pudp; | |
2774 | pmd_t *pmdp; | |
2775 | pte_t *ptep; | |
2776 | ||
2777 | /* Only allow populating anonymous memory */ | |
2778 | if (!vma_is_anonymous(vma)) | |
2779 | goto abort; | |
2780 | ||
2781 | pgdp = pgd_offset(mm, addr); | |
2782 | p4dp = p4d_alloc(mm, pgdp, addr); | |
2783 | if (!p4dp) | |
2784 | goto abort; | |
2785 | pudp = pud_alloc(mm, p4dp, addr); | |
2786 | if (!pudp) | |
2787 | goto abort; | |
2788 | pmdp = pmd_alloc(mm, pudp, addr); | |
2789 | if (!pmdp) | |
2790 | goto abort; | |
2791 | ||
2792 | if (pmd_trans_huge(*pmdp) || pmd_devmap(*pmdp)) | |
2793 | goto abort; | |
2794 | ||
2795 | /* | |
2796 | * Use pte_alloc() instead of pte_alloc_map(). We can't run | |
2797 | * pte_offset_map() on pmds where a huge pmd might be created | |
2798 | * from a different thread. | |
2799 | * | |
3e4e28c5 | 2800 | * pte_alloc_map() is safe to use under mmap_write_lock(mm) or when |
8315ada7 JG |
2801 | * parallel threads are excluded by other means. |
2802 | * | |
3e4e28c5 | 2803 | * Here we only have mmap_read_lock(mm). |
8315ada7 | 2804 | */ |
4cf58924 | 2805 | if (pte_alloc(mm, pmdp)) |
8315ada7 JG |
2806 | goto abort; |
2807 | ||
2808 | /* See the comment in pte_alloc_one_map() */ | |
2809 | if (unlikely(pmd_trans_unstable(pmdp))) | |
2810 | goto abort; | |
2811 | ||
2812 | if (unlikely(anon_vma_prepare(vma))) | |
2813 | goto abort; | |
d9eb1ea2 | 2814 | if (mem_cgroup_charge(page, vma->vm_mm, GFP_KERNEL)) |
8315ada7 JG |
2815 | goto abort; |
2816 | ||
2817 | /* | |
2818 | * The memory barrier inside __SetPageUptodate makes sure that | |
2819 | * preceding stores to the page contents become visible before | |
2820 | * the set_pte_at() write. | |
2821 | */ | |
2822 | __SetPageUptodate(page); | |
2823 | ||
df6ad698 JG |
2824 | if (is_zone_device_page(page)) { |
2825 | if (is_device_private_page(page)) { | |
2826 | swp_entry_t swp_entry; | |
2827 | ||
4dd845b5 AP |
2828 | if (vma->vm_flags & VM_WRITE) |
2829 | swp_entry = make_writable_device_private_entry( | |
2830 | page_to_pfn(page)); | |
2831 | else | |
2832 | swp_entry = make_readable_device_private_entry( | |
2833 | page_to_pfn(page)); | |
df6ad698 | 2834 | entry = swp_entry_to_pte(swp_entry); |
34f5e9b9 ML |
2835 | } else { |
2836 | /* | |
2837 | * For now we only support migrating to un-addressable | |
2838 | * device memory. | |
2839 | */ | |
2840 | pr_warn_once("Unsupported ZONE_DEVICE page type.\n"); | |
2841 | goto abort; | |
df6ad698 | 2842 | } |
8315ada7 JG |
2843 | } else { |
2844 | entry = mk_pte(page, vma->vm_page_prot); | |
2845 | if (vma->vm_flags & VM_WRITE) | |
2846 | entry = pte_mkwrite(pte_mkdirty(entry)); | |
2847 | } | |
2848 | ||
2849 | ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl); | |
2850 | ||
34290e2c RC |
2851 | if (check_stable_address_space(mm)) |
2852 | goto unlock_abort; | |
2853 | ||
8315ada7 JG |
2854 | if (pte_present(*ptep)) { |
2855 | unsigned long pfn = pte_pfn(*ptep); | |
2856 | ||
c23a0c99 RC |
2857 | if (!is_zero_pfn(pfn)) |
2858 | goto unlock_abort; | |
8315ada7 | 2859 | flush = true; |
c23a0c99 RC |
2860 | } else if (!pte_none(*ptep)) |
2861 | goto unlock_abort; | |
8315ada7 JG |
2862 | |
2863 | /* | |
c23a0c99 | 2864 | * Check for userfaultfd but do not deliver the fault. Instead, |
8315ada7 JG |
2865 | * just back off. |
2866 | */ | |
c23a0c99 RC |
2867 | if (userfaultfd_missing(vma)) |
2868 | goto unlock_abort; | |
8315ada7 JG |
2869 | |
2870 | inc_mm_counter(mm, MM_ANONPAGES); | |
be5d0a74 | 2871 | page_add_new_anon_rmap(page, vma, addr, false); |
8315ada7 | 2872 | if (!is_zone_device_page(page)) |
b518154e | 2873 | lru_cache_add_inactive_or_unevictable(page, vma); |
8315ada7 JG |
2874 | get_page(page); |
2875 | ||
2876 | if (flush) { | |
2877 | flush_cache_page(vma, addr, pte_pfn(*ptep)); | |
2878 | ptep_clear_flush_notify(vma, addr, ptep); | |
2879 | set_pte_at_notify(mm, addr, ptep, entry); | |
2880 | update_mmu_cache(vma, addr, ptep); | |
2881 | } else { | |
2882 | /* No need to invalidate - it was non-present before */ | |
2883 | set_pte_at(mm, addr, ptep, entry); | |
2884 | update_mmu_cache(vma, addr, ptep); | |
2885 | } | |
2886 | ||
2887 | pte_unmap_unlock(ptep, ptl); | |
2888 | *src = MIGRATE_PFN_MIGRATE; | |
2889 | return; | |
2890 | ||
c23a0c99 RC |
2891 | unlock_abort: |
2892 | pte_unmap_unlock(ptep, ptl); | |
8315ada7 JG |
2893 | abort: |
2894 | *src &= ~MIGRATE_PFN_MIGRATE; | |
2895 | } | |
2896 | ||
a7d1f22b | 2897 | /** |
8763cb45 JG |
2898 | * migrate_vma_pages() - migrate meta-data from src page to dst page |
2899 | * @migrate: migrate struct containing all migration information | |
2900 | * | |
2901 | * This migrates struct page meta-data from source struct page to destination | |
2902 | * struct page. This effectively finishes the migration from source page to the | |
2903 | * destination page. | |
2904 | */ | |
a7d1f22b | 2905 | void migrate_vma_pages(struct migrate_vma *migrate) |
8763cb45 JG |
2906 | { |
2907 | const unsigned long npages = migrate->npages; | |
2908 | const unsigned long start = migrate->start; | |
ac46d4f3 JG |
2909 | struct mmu_notifier_range range; |
2910 | unsigned long addr, i; | |
8315ada7 | 2911 | bool notified = false; |
8763cb45 JG |
2912 | |
2913 | for (i = 0, addr = start; i < npages; addr += PAGE_SIZE, i++) { | |
2914 | struct page *newpage = migrate_pfn_to_page(migrate->dst[i]); | |
2915 | struct page *page = migrate_pfn_to_page(migrate->src[i]); | |
2916 | struct address_space *mapping; | |
2917 | int r; | |
2918 | ||
8315ada7 JG |
2919 | if (!newpage) { |
2920 | migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; | |
8763cb45 | 2921 | continue; |
8315ada7 JG |
2922 | } |
2923 | ||
2924 | if (!page) { | |
c23a0c99 | 2925 | if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE)) |
8315ada7 | 2926 | continue; |
8315ada7 | 2927 | if (!notified) { |
8315ada7 | 2928 | notified = true; |
ac46d4f3 | 2929 | |
6b49bf6d AP |
2930 | mmu_notifier_range_init_owner(&range, |
2931 | MMU_NOTIFY_MIGRATE, 0, migrate->vma, | |
2932 | migrate->vma->vm_mm, addr, migrate->end, | |
5e5dda81 | 2933 | migrate->pgmap_owner); |
ac46d4f3 | 2934 | mmu_notifier_invalidate_range_start(&range); |
8315ada7 JG |
2935 | } |
2936 | migrate_vma_insert_page(migrate, addr, newpage, | |
d85c6db4 | 2937 | &migrate->src[i]); |
8763cb45 | 2938 | continue; |
8315ada7 | 2939 | } |
8763cb45 JG |
2940 | |
2941 | mapping = page_mapping(page); | |
2942 | ||
a5430dda JG |
2943 | if (is_zone_device_page(newpage)) { |
2944 | if (is_device_private_page(newpage)) { | |
2945 | /* | |
2946 | * For now only support private anonymous when | |
2947 | * migrating to un-addressable device memory. | |
2948 | */ | |
2949 | if (mapping) { | |
2950 | migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; | |
2951 | continue; | |
2952 | } | |
25b2995a | 2953 | } else { |
a5430dda JG |
2954 | /* |
2955 | * Other types of ZONE_DEVICE page are not | |
2956 | * supported. | |
2957 | */ | |
2958 | migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; | |
2959 | continue; | |
2960 | } | |
2961 | } | |
2962 | ||
8763cb45 JG |
2963 | r = migrate_page(mapping, newpage, page, MIGRATE_SYNC_NO_COPY); |
2964 | if (r != MIGRATEPAGE_SUCCESS) | |
2965 | migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; | |
2966 | } | |
8315ada7 | 2967 | |
4645b9fe JG |
2968 | /* |
2969 | * No need to double call mmu_notifier->invalidate_range() callback as | |
2970 | * the above ptep_clear_flush_notify() inside migrate_vma_insert_page() | |
2971 | * did already call it. | |
2972 | */ | |
8315ada7 | 2973 | if (notified) |
ac46d4f3 | 2974 | mmu_notifier_invalidate_range_only_end(&range); |
8763cb45 | 2975 | } |
a7d1f22b | 2976 | EXPORT_SYMBOL(migrate_vma_pages); |
8763cb45 | 2977 | |
a7d1f22b | 2978 | /** |
8763cb45 JG |
2979 | * migrate_vma_finalize() - restore CPU page table entry |
2980 | * @migrate: migrate struct containing all migration information | |
2981 | * | |
2982 | * This replaces the special migration pte entry with either a mapping to the | |
2983 | * new page if migration was successful for that page, or to the original page | |
2984 | * otherwise. | |
2985 | * | |
2986 | * This also unlocks the pages and puts them back on the lru, or drops the extra | |
2987 | * refcount, for device pages. | |
2988 | */ | |
a7d1f22b | 2989 | void migrate_vma_finalize(struct migrate_vma *migrate) |
8763cb45 JG |
2990 | { |
2991 | const unsigned long npages = migrate->npages; | |
2992 | unsigned long i; | |
2993 | ||
2994 | for (i = 0; i < npages; i++) { | |
2995 | struct page *newpage = migrate_pfn_to_page(migrate->dst[i]); | |
2996 | struct page *page = migrate_pfn_to_page(migrate->src[i]); | |
2997 | ||
8315ada7 JG |
2998 | if (!page) { |
2999 | if (newpage) { | |
3000 | unlock_page(newpage); | |
3001 | put_page(newpage); | |
3002 | } | |
8763cb45 | 3003 | continue; |
8315ada7 JG |
3004 | } |
3005 | ||
8763cb45 JG |
3006 | if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE) || !newpage) { |
3007 | if (newpage) { | |
3008 | unlock_page(newpage); | |
3009 | put_page(newpage); | |
3010 | } | |
3011 | newpage = page; | |
3012 | } | |
3013 | ||
3014 | remove_migration_ptes(page, newpage, false); | |
3015 | unlock_page(page); | |
8763cb45 | 3016 | |
a5430dda JG |
3017 | if (is_zone_device_page(page)) |
3018 | put_page(page); | |
3019 | else | |
3020 | putback_lru_page(page); | |
8763cb45 JG |
3021 | |
3022 | if (newpage != page) { | |
3023 | unlock_page(newpage); | |
a5430dda JG |
3024 | if (is_zone_device_page(newpage)) |
3025 | put_page(newpage); | |
3026 | else | |
3027 | putback_lru_page(newpage); | |
8763cb45 JG |
3028 | } |
3029 | } | |
3030 | } | |
a7d1f22b | 3031 | EXPORT_SYMBOL(migrate_vma_finalize); |
9b2ed9cb | 3032 | #endif /* CONFIG_DEVICE_PRIVATE */ |