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> |
df6ad698 | 41 | #include <linux/pfn_t.h> |
a5430dda | 42 | #include <linux/memremap.h> |
8315ada7 | 43 | #include <linux/userfaultfd_k.h> |
bf6bddf1 | 44 | #include <linux/balloon_compaction.h> |
33c3fc71 | 45 | #include <linux/page_idle.h> |
d435edca | 46 | #include <linux/page_owner.h> |
6e84f315 | 47 | #include <linux/sched/mm.h> |
197e7e52 | 48 | #include <linux/ptrace.h> |
34290e2c | 49 | #include <linux/oom.h> |
884a6e5d | 50 | #include <linux/memory.h> |
ac16ec83 | 51 | #include <linux/random.h> |
c574bbe9 | 52 | #include <linux/sched/sysctl.h> |
467b171a | 53 | #include <linux/memory-tiers.h> |
b20a3503 | 54 | |
0d1836c3 MN |
55 | #include <asm/tlbflush.h> |
56 | ||
7b2a2d4a MG |
57 | #include <trace/events/migrate.h> |
58 | ||
b20a3503 CL |
59 | #include "internal.h" |
60 | ||
9e5bcd61 | 61 | int isolate_movable_page(struct page *page, isolate_mode_t mode) |
bda807d4 | 62 | { |
68f2736a | 63 | const struct movable_operations *mops; |
bda807d4 MK |
64 | |
65 | /* | |
66 | * Avoid burning cycles with pages that are yet under __free_pages(), | |
67 | * or just got freed under us. | |
68 | * | |
69 | * In case we 'win' a race for a movable page being freed under us and | |
70 | * raise its refcount preventing __free_pages() from doing its job | |
71 | * the put_page() at the end of this block will take care of | |
72 | * release this page, thus avoiding a nasty leakage. | |
73 | */ | |
74 | if (unlikely(!get_page_unless_zero(page))) | |
75 | goto out; | |
76 | ||
8b881763 VB |
77 | if (unlikely(PageSlab(page))) |
78 | goto out_putpage; | |
79 | /* Pairs with smp_wmb() in slab freeing, e.g. SLUB's __free_slab() */ | |
80 | smp_rmb(); | |
bda807d4 | 81 | /* |
8b881763 VB |
82 | * Check movable flag before taking the page lock because |
83 | * we use non-atomic bitops on newly allocated page flags so | |
84 | * unconditionally grabbing the lock ruins page's owner side. | |
bda807d4 MK |
85 | */ |
86 | if (unlikely(!__PageMovable(page))) | |
87 | goto out_putpage; | |
8b881763 VB |
88 | /* Pairs with smp_wmb() in slab allocation, e.g. SLUB's alloc_slab_page() */ |
89 | smp_rmb(); | |
90 | if (unlikely(PageSlab(page))) | |
91 | goto out_putpage; | |
92 | ||
bda807d4 MK |
93 | /* |
94 | * As movable pages are not isolated from LRU lists, concurrent | |
95 | * compaction threads can race against page migration functions | |
96 | * as well as race against the releasing a page. | |
97 | * | |
98 | * In order to avoid having an already isolated movable page | |
99 | * being (wrongly) re-isolated while it is under migration, | |
100 | * or to avoid attempting to isolate pages being released, | |
101 | * lets be sure we have the page lock | |
102 | * before proceeding with the movable page isolation steps. | |
103 | */ | |
104 | if (unlikely(!trylock_page(page))) | |
105 | goto out_putpage; | |
106 | ||
107 | if (!PageMovable(page) || PageIsolated(page)) | |
108 | goto out_no_isolated; | |
109 | ||
68f2736a MWO |
110 | mops = page_movable_ops(page); |
111 | VM_BUG_ON_PAGE(!mops, page); | |
bda807d4 | 112 | |
68f2736a | 113 | if (!mops->isolate_page(page, mode)) |
bda807d4 MK |
114 | goto out_no_isolated; |
115 | ||
116 | /* Driver shouldn't use PG_isolated bit of page->flags */ | |
117 | WARN_ON_ONCE(PageIsolated(page)); | |
356ea386 | 118 | SetPageIsolated(page); |
bda807d4 MK |
119 | unlock_page(page); |
120 | ||
9e5bcd61 | 121 | return 0; |
bda807d4 MK |
122 | |
123 | out_no_isolated: | |
124 | unlock_page(page); | |
125 | out_putpage: | |
126 | put_page(page); | |
127 | out: | |
9e5bcd61 | 128 | return -EBUSY; |
bda807d4 MK |
129 | } |
130 | ||
606a6f71 | 131 | static void putback_movable_page(struct page *page) |
bda807d4 | 132 | { |
68f2736a | 133 | const struct movable_operations *mops = page_movable_ops(page); |
bda807d4 | 134 | |
68f2736a | 135 | mops->putback_page(page); |
356ea386 | 136 | ClearPageIsolated(page); |
bda807d4 MK |
137 | } |
138 | ||
5733c7d1 RA |
139 | /* |
140 | * Put previously isolated pages back onto the appropriate lists | |
141 | * from where they were once taken off for compaction/migration. | |
142 | * | |
59c82b70 JK |
143 | * This function shall be used whenever the isolated pageset has been |
144 | * built from lru, balloon, hugetlbfs page. See isolate_migratepages_range() | |
7ce82f4c | 145 | * and isolate_hugetlb(). |
5733c7d1 RA |
146 | */ |
147 | void putback_movable_pages(struct list_head *l) | |
148 | { | |
149 | struct page *page; | |
150 | struct page *page2; | |
151 | ||
b20a3503 | 152 | list_for_each_entry_safe(page, page2, l, lru) { |
31caf665 NH |
153 | if (unlikely(PageHuge(page))) { |
154 | putback_active_hugepage(page); | |
155 | continue; | |
156 | } | |
e24f0b8f | 157 | list_del(&page->lru); |
bda807d4 MK |
158 | /* |
159 | * We isolated non-lru movable page so here we can use | |
160 | * __PageMovable because LRU page's mapping cannot have | |
161 | * PAGE_MAPPING_MOVABLE. | |
162 | */ | |
b1123ea6 | 163 | if (unlikely(__PageMovable(page))) { |
bda807d4 MK |
164 | VM_BUG_ON_PAGE(!PageIsolated(page), page); |
165 | lock_page(page); | |
166 | if (PageMovable(page)) | |
167 | putback_movable_page(page); | |
168 | else | |
356ea386 | 169 | ClearPageIsolated(page); |
bda807d4 MK |
170 | unlock_page(page); |
171 | put_page(page); | |
172 | } else { | |
e8db67eb | 173 | mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + |
6c357848 | 174 | page_is_file_lru(page), -thp_nr_pages(page)); |
fc280fe8 | 175 | putback_lru_page(page); |
bda807d4 | 176 | } |
b20a3503 | 177 | } |
b20a3503 CL |
178 | } |
179 | ||
0697212a CL |
180 | /* |
181 | * Restore a potential migration pte to a working pte entry | |
182 | */ | |
2f031c6f MWO |
183 | static bool remove_migration_pte(struct folio *folio, |
184 | struct vm_area_struct *vma, unsigned long addr, void *old) | |
0697212a | 185 | { |
4eecb8b9 | 186 | DEFINE_FOLIO_VMA_WALK(pvmw, old, vma, addr, PVMW_SYNC | PVMW_MIGRATION); |
0697212a | 187 | |
3fe87967 | 188 | while (page_vma_mapped_walk(&pvmw)) { |
6c287605 | 189 | rmap_t rmap_flags = RMAP_NONE; |
4eecb8b9 MWO |
190 | pte_t pte; |
191 | swp_entry_t entry; | |
192 | struct page *new; | |
193 | unsigned long idx = 0; | |
194 | ||
195 | /* pgoff is invalid for ksm pages, but they are never large */ | |
196 | if (folio_test_large(folio) && !folio_test_hugetlb(folio)) | |
197 | idx = linear_page_index(vma, pvmw.address) - pvmw.pgoff; | |
198 | new = folio_page(folio, idx); | |
0697212a | 199 | |
616b8371 ZY |
200 | #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION |
201 | /* PMD-mapped THP migration entry */ | |
202 | if (!pvmw.pte) { | |
4eecb8b9 MWO |
203 | VM_BUG_ON_FOLIO(folio_test_hugetlb(folio) || |
204 | !folio_test_pmd_mappable(folio), folio); | |
616b8371 ZY |
205 | remove_migration_pmd(&pvmw, new); |
206 | continue; | |
207 | } | |
208 | #endif | |
209 | ||
4eecb8b9 | 210 | folio_get(folio); |
2e346877 | 211 | pte = mk_pte(new, READ_ONCE(vma->vm_page_prot)); |
3fe87967 KS |
212 | if (pte_swp_soft_dirty(*pvmw.pte)) |
213 | pte = pte_mksoft_dirty(pte); | |
0697212a | 214 | |
3fe87967 KS |
215 | /* |
216 | * Recheck VMA as permissions can change since migration started | |
217 | */ | |
218 | entry = pte_to_swp_entry(*pvmw.pte); | |
2e346877 PX |
219 | if (!is_migration_entry_young(entry)) |
220 | pte = pte_mkold(pte); | |
221 | if (folio_test_dirty(folio) && is_migration_entry_dirty(entry)) | |
222 | pte = pte_mkdirty(pte); | |
4dd845b5 | 223 | if (is_writable_migration_entry(entry)) |
3fe87967 | 224 | pte = maybe_mkwrite(pte, vma); |
f45ec5ff PX |
225 | else if (pte_swp_uffd_wp(*pvmw.pte)) |
226 | pte = pte_mkuffd_wp(pte); | |
d3cb8bf6 | 227 | |
6c287605 DH |
228 | if (folio_test_anon(folio) && !is_readable_migration_entry(entry)) |
229 | rmap_flags |= RMAP_EXCLUSIVE; | |
230 | ||
6128763f | 231 | if (unlikely(is_device_private_page(new))) { |
4dd845b5 AP |
232 | if (pte_write(pte)) |
233 | entry = make_writable_device_private_entry( | |
234 | page_to_pfn(new)); | |
235 | else | |
236 | entry = make_readable_device_private_entry( | |
237 | page_to_pfn(new)); | |
6128763f | 238 | pte = swp_entry_to_pte(entry); |
3d321bf8 RC |
239 | if (pte_swp_soft_dirty(*pvmw.pte)) |
240 | pte = pte_swp_mksoft_dirty(pte); | |
6128763f RC |
241 | if (pte_swp_uffd_wp(*pvmw.pte)) |
242 | pte = pte_swp_mkuffd_wp(pte); | |
d2b2c6dd | 243 | } |
a5430dda | 244 | |
3ef8fd7f | 245 | #ifdef CONFIG_HUGETLB_PAGE |
4eecb8b9 | 246 | if (folio_test_hugetlb(folio)) { |
79c1c594 CL |
247 | unsigned int shift = huge_page_shift(hstate_vma(vma)); |
248 | ||
3fe87967 | 249 | pte = pte_mkhuge(pte); |
79c1c594 | 250 | pte = arch_make_huge_pte(pte, shift, vma->vm_flags); |
4eecb8b9 | 251 | if (folio_test_anon(folio)) |
28c5209d | 252 | hugepage_add_anon_rmap(new, vma, pvmw.address, |
6c287605 | 253 | rmap_flags); |
3fe87967 | 254 | else |
fb3d824d | 255 | page_dup_file_rmap(new, true); |
1eba86c0 | 256 | set_huge_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte); |
383321ab AK |
257 | } else |
258 | #endif | |
259 | { | |
4eecb8b9 | 260 | if (folio_test_anon(folio)) |
f1e2db12 | 261 | page_add_anon_rmap(new, vma, pvmw.address, |
6c287605 | 262 | rmap_flags); |
383321ab | 263 | else |
cea86fe2 | 264 | page_add_file_rmap(new, vma, false); |
1eba86c0 | 265 | set_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte); |
383321ab | 266 | } |
b7435507 | 267 | if (vma->vm_flags & VM_LOCKED) |
adb11e78 | 268 | mlock_page_drain_local(); |
e125fe40 | 269 | |
4cc79b33 AK |
270 | trace_remove_migration_pte(pvmw.address, pte_val(pte), |
271 | compound_order(new)); | |
272 | ||
3fe87967 KS |
273 | /* No need to invalidate - it was non-present before */ |
274 | update_mmu_cache(vma, pvmw.address, pvmw.pte); | |
275 | } | |
51afb12b | 276 | |
e4b82222 | 277 | return true; |
0697212a CL |
278 | } |
279 | ||
04e62a29 CL |
280 | /* |
281 | * Get rid of all migration entries and replace them by | |
282 | * references to the indicated page. | |
283 | */ | |
4eecb8b9 | 284 | void remove_migration_ptes(struct folio *src, struct folio *dst, bool locked) |
04e62a29 | 285 | { |
051ac83a JK |
286 | struct rmap_walk_control rwc = { |
287 | .rmap_one = remove_migration_pte, | |
4eecb8b9 | 288 | .arg = src, |
051ac83a JK |
289 | }; |
290 | ||
e388466d | 291 | if (locked) |
2f031c6f | 292 | rmap_walk_locked(dst, &rwc); |
e388466d | 293 | else |
2f031c6f | 294 | rmap_walk(dst, &rwc); |
04e62a29 CL |
295 | } |
296 | ||
0697212a CL |
297 | /* |
298 | * Something used the pte of a page under migration. We need to | |
299 | * get to the page and wait until migration is finished. | |
300 | * When we return from this function the fault will be retried. | |
0697212a | 301 | */ |
e66f17ff | 302 | void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep, |
30dad309 | 303 | spinlock_t *ptl) |
0697212a | 304 | { |
30dad309 | 305 | pte_t pte; |
0697212a | 306 | swp_entry_t entry; |
0697212a | 307 | |
30dad309 | 308 | spin_lock(ptl); |
0697212a CL |
309 | pte = *ptep; |
310 | if (!is_swap_pte(pte)) | |
311 | goto out; | |
312 | ||
313 | entry = pte_to_swp_entry(pte); | |
314 | if (!is_migration_entry(entry)) | |
315 | goto out; | |
316 | ||
ffa65753 | 317 | migration_entry_wait_on_locked(entry, ptep, ptl); |
0697212a CL |
318 | return; |
319 | out: | |
320 | pte_unmap_unlock(ptep, ptl); | |
321 | } | |
322 | ||
30dad309 NH |
323 | void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd, |
324 | unsigned long address) | |
325 | { | |
326 | spinlock_t *ptl = pte_lockptr(mm, pmd); | |
327 | pte_t *ptep = pte_offset_map(pmd, address); | |
328 | __migration_entry_wait(mm, ptep, ptl); | |
329 | } | |
330 | ||
ad1ac596 ML |
331 | #ifdef CONFIG_HUGETLB_PAGE |
332 | void __migration_entry_wait_huge(pte_t *ptep, spinlock_t *ptl) | |
30dad309 | 333 | { |
ad1ac596 ML |
334 | pte_t pte; |
335 | ||
336 | spin_lock(ptl); | |
337 | pte = huge_ptep_get(ptep); | |
338 | ||
339 | if (unlikely(!is_hugetlb_entry_migration(pte))) | |
340 | spin_unlock(ptl); | |
341 | else | |
342 | migration_entry_wait_on_locked(pte_to_swp_entry(pte), NULL, ptl); | |
30dad309 NH |
343 | } |
344 | ||
ad1ac596 ML |
345 | void migration_entry_wait_huge(struct vm_area_struct *vma, pte_t *pte) |
346 | { | |
347 | spinlock_t *ptl = huge_pte_lockptr(hstate_vma(vma), vma->vm_mm, pte); | |
348 | ||
349 | __migration_entry_wait_huge(pte, ptl); | |
350 | } | |
351 | #endif | |
352 | ||
616b8371 ZY |
353 | #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION |
354 | void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd) | |
355 | { | |
356 | spinlock_t *ptl; | |
616b8371 ZY |
357 | |
358 | ptl = pmd_lock(mm, pmd); | |
359 | if (!is_pmd_migration_entry(*pmd)) | |
360 | goto unlock; | |
ffa65753 | 361 | migration_entry_wait_on_locked(pmd_to_swp_entry(*pmd), NULL, ptl); |
616b8371 ZY |
362 | return; |
363 | unlock: | |
364 | spin_unlock(ptl); | |
365 | } | |
366 | #endif | |
367 | ||
108ca835 MWO |
368 | static int folio_expected_refs(struct address_space *mapping, |
369 | struct folio *folio) | |
0b3901b3 | 370 | { |
108ca835 MWO |
371 | int refs = 1; |
372 | if (!mapping) | |
373 | return refs; | |
0b3901b3 | 374 | |
108ca835 MWO |
375 | refs += folio_nr_pages(folio); |
376 | if (folio_test_private(folio)) | |
377 | refs++; | |
378 | ||
379 | return refs; | |
0b3901b3 JK |
380 | } |
381 | ||
b20a3503 | 382 | /* |
c3fcf8a5 | 383 | * Replace the page in the mapping. |
5b5c7120 CL |
384 | * |
385 | * The number of remaining references must be: | |
386 | * 1 for anonymous pages without a mapping | |
387 | * 2 for pages with a mapping | |
266cf658 | 388 | * 3 for pages with a mapping and PagePrivate/PagePrivate2 set. |
b20a3503 | 389 | */ |
3417013e MWO |
390 | int folio_migrate_mapping(struct address_space *mapping, |
391 | struct folio *newfolio, struct folio *folio, int extra_count) | |
b20a3503 | 392 | { |
3417013e | 393 | XA_STATE(xas, &mapping->i_pages, folio_index(folio)); |
42cb14b1 HD |
394 | struct zone *oldzone, *newzone; |
395 | int dirty; | |
108ca835 | 396 | int expected_count = folio_expected_refs(mapping, folio) + extra_count; |
3417013e | 397 | long nr = folio_nr_pages(folio); |
8763cb45 | 398 | |
6c5240ae | 399 | if (!mapping) { |
0e8c7d0f | 400 | /* Anonymous page without mapping */ |
3417013e | 401 | if (folio_ref_count(folio) != expected_count) |
6c5240ae | 402 | return -EAGAIN; |
cf4b769a HD |
403 | |
404 | /* No turning back from here */ | |
3417013e MWO |
405 | newfolio->index = folio->index; |
406 | newfolio->mapping = folio->mapping; | |
407 | if (folio_test_swapbacked(folio)) | |
408 | __folio_set_swapbacked(newfolio); | |
cf4b769a | 409 | |
78bd5209 | 410 | return MIGRATEPAGE_SUCCESS; |
6c5240ae CL |
411 | } |
412 | ||
3417013e MWO |
413 | oldzone = folio_zone(folio); |
414 | newzone = folio_zone(newfolio); | |
42cb14b1 | 415 | |
89eb946a | 416 | xas_lock_irq(&xas); |
3417013e | 417 | if (!folio_ref_freeze(folio, expected_count)) { |
89eb946a | 418 | xas_unlock_irq(&xas); |
e286781d NP |
419 | return -EAGAIN; |
420 | } | |
421 | ||
b20a3503 | 422 | /* |
3417013e | 423 | * Now we know that no one else is looking at the folio: |
cf4b769a | 424 | * no turning back from here. |
b20a3503 | 425 | */ |
3417013e MWO |
426 | newfolio->index = folio->index; |
427 | newfolio->mapping = folio->mapping; | |
428 | folio_ref_add(newfolio, nr); /* add cache reference */ | |
429 | if (folio_test_swapbacked(folio)) { | |
430 | __folio_set_swapbacked(newfolio); | |
431 | if (folio_test_swapcache(folio)) { | |
432 | folio_set_swapcache(newfolio); | |
433 | newfolio->private = folio_get_private(folio); | |
6326fec1 NP |
434 | } |
435 | } else { | |
3417013e | 436 | VM_BUG_ON_FOLIO(folio_test_swapcache(folio), folio); |
b20a3503 CL |
437 | } |
438 | ||
42cb14b1 | 439 | /* Move dirty while page refs frozen and newpage not yet exposed */ |
3417013e | 440 | dirty = folio_test_dirty(folio); |
42cb14b1 | 441 | if (dirty) { |
3417013e MWO |
442 | folio_clear_dirty(folio); |
443 | folio_set_dirty(newfolio); | |
42cb14b1 HD |
444 | } |
445 | ||
3417013e | 446 | xas_store(&xas, newfolio); |
7cf9c2c7 NP |
447 | |
448 | /* | |
937a94c9 JG |
449 | * Drop cache reference from old page by unfreezing |
450 | * to one less reference. | |
7cf9c2c7 NP |
451 | * We know this isn't the last reference. |
452 | */ | |
3417013e | 453 | folio_ref_unfreeze(folio, expected_count - nr); |
7cf9c2c7 | 454 | |
89eb946a | 455 | xas_unlock(&xas); |
42cb14b1 HD |
456 | /* Leave irq disabled to prevent preemption while updating stats */ |
457 | ||
0e8c7d0f CL |
458 | /* |
459 | * If moved to a different zone then also account | |
460 | * the page for that zone. Other VM counters will be | |
461 | * taken care of when we establish references to the | |
462 | * new page and drop references to the old page. | |
463 | * | |
464 | * Note that anonymous pages are accounted for | |
4b9d0fab | 465 | * via NR_FILE_PAGES and NR_ANON_MAPPED if they |
0e8c7d0f CL |
466 | * are mapped to swap space. |
467 | */ | |
42cb14b1 | 468 | if (newzone != oldzone) { |
0d1c2072 JW |
469 | struct lruvec *old_lruvec, *new_lruvec; |
470 | struct mem_cgroup *memcg; | |
471 | ||
3417013e | 472 | memcg = folio_memcg(folio); |
0d1c2072 JW |
473 | old_lruvec = mem_cgroup_lruvec(memcg, oldzone->zone_pgdat); |
474 | new_lruvec = mem_cgroup_lruvec(memcg, newzone->zone_pgdat); | |
475 | ||
5c447d27 SB |
476 | __mod_lruvec_state(old_lruvec, NR_FILE_PAGES, -nr); |
477 | __mod_lruvec_state(new_lruvec, NR_FILE_PAGES, nr); | |
3417013e | 478 | if (folio_test_swapbacked(folio) && !folio_test_swapcache(folio)) { |
5c447d27 SB |
479 | __mod_lruvec_state(old_lruvec, NR_SHMEM, -nr); |
480 | __mod_lruvec_state(new_lruvec, NR_SHMEM, nr); | |
42cb14b1 | 481 | } |
b6038942 | 482 | #ifdef CONFIG_SWAP |
3417013e | 483 | if (folio_test_swapcache(folio)) { |
b6038942 SB |
484 | __mod_lruvec_state(old_lruvec, NR_SWAPCACHE, -nr); |
485 | __mod_lruvec_state(new_lruvec, NR_SWAPCACHE, nr); | |
486 | } | |
487 | #endif | |
f56753ac | 488 | if (dirty && mapping_can_writeback(mapping)) { |
5c447d27 SB |
489 | __mod_lruvec_state(old_lruvec, NR_FILE_DIRTY, -nr); |
490 | __mod_zone_page_state(oldzone, NR_ZONE_WRITE_PENDING, -nr); | |
491 | __mod_lruvec_state(new_lruvec, NR_FILE_DIRTY, nr); | |
492 | __mod_zone_page_state(newzone, NR_ZONE_WRITE_PENDING, nr); | |
42cb14b1 | 493 | } |
4b02108a | 494 | } |
42cb14b1 | 495 | local_irq_enable(); |
b20a3503 | 496 | |
78bd5209 | 497 | return MIGRATEPAGE_SUCCESS; |
b20a3503 | 498 | } |
3417013e | 499 | EXPORT_SYMBOL(folio_migrate_mapping); |
b20a3503 | 500 | |
290408d4 NH |
501 | /* |
502 | * The expected number of remaining references is the same as that | |
3417013e | 503 | * of folio_migrate_mapping(). |
290408d4 NH |
504 | */ |
505 | int migrate_huge_page_move_mapping(struct address_space *mapping, | |
b890ec2a | 506 | struct folio *dst, struct folio *src) |
290408d4 | 507 | { |
b890ec2a | 508 | XA_STATE(xas, &mapping->i_pages, folio_index(src)); |
290408d4 | 509 | int expected_count; |
290408d4 | 510 | |
89eb946a | 511 | xas_lock_irq(&xas); |
b890ec2a MWO |
512 | expected_count = 2 + folio_has_private(src); |
513 | if (!folio_ref_freeze(src, expected_count)) { | |
89eb946a | 514 | xas_unlock_irq(&xas); |
290408d4 NH |
515 | return -EAGAIN; |
516 | } | |
517 | ||
b890ec2a MWO |
518 | dst->index = src->index; |
519 | dst->mapping = src->mapping; | |
6a93ca8f | 520 | |
b890ec2a | 521 | folio_get(dst); |
290408d4 | 522 | |
b890ec2a | 523 | xas_store(&xas, dst); |
290408d4 | 524 | |
b890ec2a | 525 | folio_ref_unfreeze(src, expected_count - 1); |
290408d4 | 526 | |
89eb946a | 527 | xas_unlock_irq(&xas); |
6a93ca8f | 528 | |
78bd5209 | 529 | return MIGRATEPAGE_SUCCESS; |
290408d4 NH |
530 | } |
531 | ||
b20a3503 | 532 | /* |
19138349 | 533 | * Copy the flags and some other ancillary information |
b20a3503 | 534 | */ |
19138349 | 535 | void folio_migrate_flags(struct folio *newfolio, struct folio *folio) |
b20a3503 | 536 | { |
7851a45c RR |
537 | int cpupid; |
538 | ||
19138349 MWO |
539 | if (folio_test_error(folio)) |
540 | folio_set_error(newfolio); | |
541 | if (folio_test_referenced(folio)) | |
542 | folio_set_referenced(newfolio); | |
543 | if (folio_test_uptodate(folio)) | |
544 | folio_mark_uptodate(newfolio); | |
545 | if (folio_test_clear_active(folio)) { | |
546 | VM_BUG_ON_FOLIO(folio_test_unevictable(folio), folio); | |
547 | folio_set_active(newfolio); | |
548 | } else if (folio_test_clear_unevictable(folio)) | |
549 | folio_set_unevictable(newfolio); | |
550 | if (folio_test_workingset(folio)) | |
551 | folio_set_workingset(newfolio); | |
552 | if (folio_test_checked(folio)) | |
553 | folio_set_checked(newfolio); | |
6c287605 DH |
554 | /* |
555 | * PG_anon_exclusive (-> PG_mappedtodisk) is always migrated via | |
556 | * migration entries. We can still have PG_anon_exclusive set on an | |
557 | * effectively unmapped and unreferenced first sub-pages of an | |
558 | * anonymous THP: we can simply copy it here via PG_mappedtodisk. | |
559 | */ | |
19138349 MWO |
560 | if (folio_test_mappedtodisk(folio)) |
561 | folio_set_mappedtodisk(newfolio); | |
b20a3503 | 562 | |
3417013e | 563 | /* Move dirty on pages not done by folio_migrate_mapping() */ |
19138349 MWO |
564 | if (folio_test_dirty(folio)) |
565 | folio_set_dirty(newfolio); | |
b20a3503 | 566 | |
19138349 MWO |
567 | if (folio_test_young(folio)) |
568 | folio_set_young(newfolio); | |
569 | if (folio_test_idle(folio)) | |
570 | folio_set_idle(newfolio); | |
33c3fc71 | 571 | |
7851a45c RR |
572 | /* |
573 | * Copy NUMA information to the new page, to prevent over-eager | |
574 | * future migrations of this same page. | |
575 | */ | |
19138349 | 576 | cpupid = page_cpupid_xchg_last(&folio->page, -1); |
33024536 HY |
577 | /* |
578 | * For memory tiering mode, when migrate between slow and fast | |
579 | * memory node, reset cpupid, because that is used to record | |
580 | * page access time in slow memory node. | |
581 | */ | |
582 | if (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING) { | |
583 | bool f_toptier = node_is_toptier(page_to_nid(&folio->page)); | |
584 | bool t_toptier = node_is_toptier(page_to_nid(&newfolio->page)); | |
585 | ||
586 | if (f_toptier != t_toptier) | |
587 | cpupid = -1; | |
588 | } | |
19138349 | 589 | page_cpupid_xchg_last(&newfolio->page, cpupid); |
7851a45c | 590 | |
19138349 | 591 | folio_migrate_ksm(newfolio, folio); |
c8d6553b HD |
592 | /* |
593 | * Please do not reorder this without considering how mm/ksm.c's | |
594 | * get_ksm_page() depends upon ksm_migrate_page() and PageSwapCache(). | |
595 | */ | |
19138349 MWO |
596 | if (folio_test_swapcache(folio)) |
597 | folio_clear_swapcache(folio); | |
598 | folio_clear_private(folio); | |
ad2fa371 MS |
599 | |
600 | /* page->private contains hugetlb specific flags */ | |
19138349 MWO |
601 | if (!folio_test_hugetlb(folio)) |
602 | folio->private = NULL; | |
b20a3503 CL |
603 | |
604 | /* | |
605 | * If any waiters have accumulated on the new page then | |
606 | * wake them up. | |
607 | */ | |
19138349 MWO |
608 | if (folio_test_writeback(newfolio)) |
609 | folio_end_writeback(newfolio); | |
d435edca | 610 | |
6aeff241 YS |
611 | /* |
612 | * PG_readahead shares the same bit with PG_reclaim. The above | |
613 | * end_page_writeback() may clear PG_readahead mistakenly, so set the | |
614 | * bit after that. | |
615 | */ | |
19138349 MWO |
616 | if (folio_test_readahead(folio)) |
617 | folio_set_readahead(newfolio); | |
6aeff241 | 618 | |
19138349 | 619 | folio_copy_owner(newfolio, folio); |
74485cf2 | 620 | |
19138349 | 621 | if (!folio_test_hugetlb(folio)) |
d21bba2b | 622 | mem_cgroup_migrate(folio, newfolio); |
b20a3503 | 623 | } |
19138349 | 624 | EXPORT_SYMBOL(folio_migrate_flags); |
2916ecc0 | 625 | |
715cbfd6 | 626 | void folio_migrate_copy(struct folio *newfolio, struct folio *folio) |
2916ecc0 | 627 | { |
715cbfd6 MWO |
628 | folio_copy(newfolio, folio); |
629 | folio_migrate_flags(newfolio, folio); | |
2916ecc0 | 630 | } |
715cbfd6 | 631 | EXPORT_SYMBOL(folio_migrate_copy); |
b20a3503 | 632 | |
1d8b85cc CL |
633 | /************************************************************ |
634 | * Migration functions | |
635 | ***********************************************************/ | |
636 | ||
16ce101d AP |
637 | int migrate_folio_extra(struct address_space *mapping, struct folio *dst, |
638 | struct folio *src, enum migrate_mode mode, int extra_count) | |
639 | { | |
640 | int rc; | |
641 | ||
642 | BUG_ON(folio_test_writeback(src)); /* Writeback must be complete */ | |
643 | ||
644 | rc = folio_migrate_mapping(mapping, dst, src, extra_count); | |
645 | ||
646 | if (rc != MIGRATEPAGE_SUCCESS) | |
647 | return rc; | |
648 | ||
649 | if (mode != MIGRATE_SYNC_NO_COPY) | |
650 | folio_migrate_copy(dst, src); | |
651 | else | |
652 | folio_migrate_flags(dst, src); | |
653 | return MIGRATEPAGE_SUCCESS; | |
654 | } | |
655 | ||
54184650 MWO |
656 | /** |
657 | * migrate_folio() - Simple folio migration. | |
658 | * @mapping: The address_space containing the folio. | |
659 | * @dst: The folio to migrate the data to. | |
660 | * @src: The folio containing the current data. | |
661 | * @mode: How to migrate the page. | |
662 | * | |
663 | * Common logic to directly migrate a single LRU folio suitable for | |
664 | * folios that do not use PagePrivate/PagePrivate2. | |
b20a3503 | 665 | * |
54184650 | 666 | * Folios are locked upon entry and exit. |
b20a3503 | 667 | */ |
54184650 MWO |
668 | int migrate_folio(struct address_space *mapping, struct folio *dst, |
669 | struct folio *src, enum migrate_mode mode) | |
b20a3503 | 670 | { |
16ce101d | 671 | return migrate_folio_extra(mapping, dst, src, mode, 0); |
b20a3503 | 672 | } |
54184650 | 673 | EXPORT_SYMBOL(migrate_folio); |
b20a3503 | 674 | |
9361401e | 675 | #ifdef CONFIG_BLOCK |
84ade7c1 JK |
676 | /* Returns true if all buffers are successfully locked */ |
677 | static bool buffer_migrate_lock_buffers(struct buffer_head *head, | |
678 | enum migrate_mode mode) | |
679 | { | |
680 | struct buffer_head *bh = head; | |
681 | ||
682 | /* Simple case, sync compaction */ | |
683 | if (mode != MIGRATE_ASYNC) { | |
684 | do { | |
84ade7c1 JK |
685 | lock_buffer(bh); |
686 | bh = bh->b_this_page; | |
687 | ||
688 | } while (bh != head); | |
689 | ||
690 | return true; | |
691 | } | |
692 | ||
693 | /* async case, we cannot block on lock_buffer so use trylock_buffer */ | |
694 | do { | |
84ade7c1 JK |
695 | if (!trylock_buffer(bh)) { |
696 | /* | |
697 | * We failed to lock the buffer and cannot stall in | |
698 | * async migration. Release the taken locks | |
699 | */ | |
700 | struct buffer_head *failed_bh = bh; | |
84ade7c1 JK |
701 | bh = head; |
702 | while (bh != failed_bh) { | |
703 | unlock_buffer(bh); | |
84ade7c1 JK |
704 | bh = bh->b_this_page; |
705 | } | |
706 | return false; | |
707 | } | |
708 | ||
709 | bh = bh->b_this_page; | |
710 | } while (bh != head); | |
711 | return true; | |
712 | } | |
713 | ||
67235182 MWO |
714 | static int __buffer_migrate_folio(struct address_space *mapping, |
715 | struct folio *dst, struct folio *src, enum migrate_mode mode, | |
89cb0888 | 716 | bool check_refs) |
1d8b85cc | 717 | { |
1d8b85cc CL |
718 | struct buffer_head *bh, *head; |
719 | int rc; | |
cc4f11e6 | 720 | int expected_count; |
1d8b85cc | 721 | |
67235182 MWO |
722 | head = folio_buffers(src); |
723 | if (!head) | |
54184650 | 724 | return migrate_folio(mapping, dst, src, mode); |
1d8b85cc | 725 | |
cc4f11e6 | 726 | /* Check whether page does not have extra refs before we do more work */ |
108ca835 | 727 | expected_count = folio_expected_refs(mapping, src); |
67235182 | 728 | if (folio_ref_count(src) != expected_count) |
cc4f11e6 | 729 | return -EAGAIN; |
1d8b85cc | 730 | |
cc4f11e6 JK |
731 | if (!buffer_migrate_lock_buffers(head, mode)) |
732 | return -EAGAIN; | |
1d8b85cc | 733 | |
89cb0888 JK |
734 | if (check_refs) { |
735 | bool busy; | |
736 | bool invalidated = false; | |
737 | ||
738 | recheck_buffers: | |
739 | busy = false; | |
740 | spin_lock(&mapping->private_lock); | |
741 | bh = head; | |
742 | do { | |
743 | if (atomic_read(&bh->b_count)) { | |
744 | busy = true; | |
745 | break; | |
746 | } | |
747 | bh = bh->b_this_page; | |
748 | } while (bh != head); | |
89cb0888 JK |
749 | if (busy) { |
750 | if (invalidated) { | |
751 | rc = -EAGAIN; | |
752 | goto unlock_buffers; | |
753 | } | |
ebdf4de5 | 754 | spin_unlock(&mapping->private_lock); |
89cb0888 JK |
755 | invalidate_bh_lrus(); |
756 | invalidated = true; | |
757 | goto recheck_buffers; | |
758 | } | |
759 | } | |
760 | ||
67235182 | 761 | rc = folio_migrate_mapping(mapping, dst, src, 0); |
78bd5209 | 762 | if (rc != MIGRATEPAGE_SUCCESS) |
cc4f11e6 | 763 | goto unlock_buffers; |
1d8b85cc | 764 | |
67235182 | 765 | folio_attach_private(dst, folio_detach_private(src)); |
1d8b85cc CL |
766 | |
767 | bh = head; | |
768 | do { | |
67235182 | 769 | set_bh_page(bh, &dst->page, bh_offset(bh)); |
1d8b85cc | 770 | bh = bh->b_this_page; |
1d8b85cc CL |
771 | } while (bh != head); |
772 | ||
2916ecc0 | 773 | if (mode != MIGRATE_SYNC_NO_COPY) |
67235182 | 774 | folio_migrate_copy(dst, src); |
2916ecc0 | 775 | else |
67235182 | 776 | folio_migrate_flags(dst, src); |
1d8b85cc | 777 | |
cc4f11e6 JK |
778 | rc = MIGRATEPAGE_SUCCESS; |
779 | unlock_buffers: | |
ebdf4de5 JK |
780 | if (check_refs) |
781 | spin_unlock(&mapping->private_lock); | |
1d8b85cc CL |
782 | bh = head; |
783 | do { | |
784 | unlock_buffer(bh); | |
1d8b85cc | 785 | bh = bh->b_this_page; |
1d8b85cc CL |
786 | } while (bh != head); |
787 | ||
cc4f11e6 | 788 | return rc; |
1d8b85cc | 789 | } |
89cb0888 | 790 | |
67235182 MWO |
791 | /** |
792 | * buffer_migrate_folio() - Migration function for folios with buffers. | |
793 | * @mapping: The address space containing @src. | |
794 | * @dst: The folio to migrate to. | |
795 | * @src: The folio to migrate from. | |
796 | * @mode: How to migrate the folio. | |
797 | * | |
798 | * This function can only be used if the underlying filesystem guarantees | |
799 | * that no other references to @src exist. For example attached buffer | |
800 | * heads are accessed only under the folio lock. If your filesystem cannot | |
801 | * provide this guarantee, buffer_migrate_folio_norefs() may be more | |
802 | * appropriate. | |
803 | * | |
804 | * Return: 0 on success or a negative errno on failure. | |
89cb0888 | 805 | */ |
67235182 MWO |
806 | int buffer_migrate_folio(struct address_space *mapping, |
807 | struct folio *dst, struct folio *src, enum migrate_mode mode) | |
89cb0888 | 808 | { |
67235182 | 809 | return __buffer_migrate_folio(mapping, dst, src, mode, false); |
89cb0888 | 810 | } |
67235182 MWO |
811 | EXPORT_SYMBOL(buffer_migrate_folio); |
812 | ||
813 | /** | |
814 | * buffer_migrate_folio_norefs() - Migration function for folios with buffers. | |
815 | * @mapping: The address space containing @src. | |
816 | * @dst: The folio to migrate to. | |
817 | * @src: The folio to migrate from. | |
818 | * @mode: How to migrate the folio. | |
819 | * | |
820 | * Like buffer_migrate_folio() except that this variant is more careful | |
821 | * and checks that there are also no buffer head references. This function | |
822 | * is the right one for mappings where buffer heads are directly looked | |
823 | * up and referenced (such as block device mappings). | |
824 | * | |
825 | * Return: 0 on success or a negative errno on failure. | |
89cb0888 | 826 | */ |
67235182 MWO |
827 | int buffer_migrate_folio_norefs(struct address_space *mapping, |
828 | struct folio *dst, struct folio *src, enum migrate_mode mode) | |
89cb0888 | 829 | { |
67235182 | 830 | return __buffer_migrate_folio(mapping, dst, src, mode, true); |
89cb0888 | 831 | } |
e26355e2 | 832 | EXPORT_SYMBOL_GPL(buffer_migrate_folio_norefs); |
9361401e | 833 | #endif |
1d8b85cc | 834 | |
2ec810d5 MWO |
835 | int filemap_migrate_folio(struct address_space *mapping, |
836 | struct folio *dst, struct folio *src, enum migrate_mode mode) | |
837 | { | |
838 | int ret; | |
839 | ||
840 | ret = folio_migrate_mapping(mapping, dst, src, 0); | |
841 | if (ret != MIGRATEPAGE_SUCCESS) | |
842 | return ret; | |
843 | ||
844 | if (folio_get_private(src)) | |
845 | folio_attach_private(dst, folio_detach_private(src)); | |
846 | ||
847 | if (mode != MIGRATE_SYNC_NO_COPY) | |
848 | folio_migrate_copy(dst, src); | |
849 | else | |
850 | folio_migrate_flags(dst, src); | |
851 | return MIGRATEPAGE_SUCCESS; | |
852 | } | |
853 | EXPORT_SYMBOL_GPL(filemap_migrate_folio); | |
854 | ||
04e62a29 | 855 | /* |
2be7fa10 | 856 | * Writeback a folio to clean the dirty state |
04e62a29 | 857 | */ |
2be7fa10 | 858 | static int writeout(struct address_space *mapping, struct folio *folio) |
8351a6e4 | 859 | { |
04e62a29 CL |
860 | struct writeback_control wbc = { |
861 | .sync_mode = WB_SYNC_NONE, | |
862 | .nr_to_write = 1, | |
863 | .range_start = 0, | |
864 | .range_end = LLONG_MAX, | |
04e62a29 CL |
865 | .for_reclaim = 1 |
866 | }; | |
867 | int rc; | |
868 | ||
869 | if (!mapping->a_ops->writepage) | |
870 | /* No write method for the address space */ | |
871 | return -EINVAL; | |
872 | ||
2be7fa10 | 873 | if (!folio_clear_dirty_for_io(folio)) |
04e62a29 CL |
874 | /* Someone else already triggered a write */ |
875 | return -EAGAIN; | |
876 | ||
8351a6e4 | 877 | /* |
2be7fa10 MWO |
878 | * A dirty folio may imply that the underlying filesystem has |
879 | * the folio on some queue. So the folio must be clean for | |
880 | * migration. Writeout may mean we lose the lock and the | |
881 | * folio state is no longer what we checked for earlier. | |
04e62a29 CL |
882 | * At this point we know that the migration attempt cannot |
883 | * be successful. | |
8351a6e4 | 884 | */ |
4eecb8b9 | 885 | remove_migration_ptes(folio, folio, false); |
8351a6e4 | 886 | |
2be7fa10 | 887 | rc = mapping->a_ops->writepage(&folio->page, &wbc); |
8351a6e4 | 888 | |
04e62a29 CL |
889 | if (rc != AOP_WRITEPAGE_ACTIVATE) |
890 | /* unlocked. Relock */ | |
2be7fa10 | 891 | folio_lock(folio); |
04e62a29 | 892 | |
bda8550d | 893 | return (rc < 0) ? -EIO : -EAGAIN; |
04e62a29 CL |
894 | } |
895 | ||
896 | /* | |
897 | * Default handling if a filesystem does not provide a migration function. | |
898 | */ | |
8faa8ef5 MWO |
899 | static int fallback_migrate_folio(struct address_space *mapping, |
900 | struct folio *dst, struct folio *src, enum migrate_mode mode) | |
04e62a29 | 901 | { |
8faa8ef5 MWO |
902 | if (folio_test_dirty(src)) { |
903 | /* Only writeback folios in full synchronous migration */ | |
2916ecc0 JG |
904 | switch (mode) { |
905 | case MIGRATE_SYNC: | |
906 | case MIGRATE_SYNC_NO_COPY: | |
907 | break; | |
908 | default: | |
b969c4ab | 909 | return -EBUSY; |
2916ecc0 | 910 | } |
2be7fa10 | 911 | return writeout(mapping, src); |
b969c4ab | 912 | } |
8351a6e4 CL |
913 | |
914 | /* | |
915 | * Buffers may be managed in a filesystem specific way. | |
916 | * We must have no buffers or drop them. | |
917 | */ | |
8faa8ef5 MWO |
918 | if (folio_test_private(src) && |
919 | !filemap_release_folio(src, GFP_KERNEL)) | |
806031bb | 920 | return mode == MIGRATE_SYNC ? -EAGAIN : -EBUSY; |
8351a6e4 | 921 | |
54184650 | 922 | return migrate_folio(mapping, dst, src, mode); |
8351a6e4 CL |
923 | } |
924 | ||
e24f0b8f CL |
925 | /* |
926 | * Move a page to a newly allocated page | |
927 | * The page is locked and all ptes have been successfully removed. | |
928 | * | |
929 | * The new page will have replaced the old page if this function | |
930 | * is successful. | |
894bc310 LS |
931 | * |
932 | * Return value: | |
933 | * < 0 - error code | |
78bd5209 | 934 | * MIGRATEPAGE_SUCCESS - success |
e24f0b8f | 935 | */ |
e7e3ffeb | 936 | static int move_to_new_folio(struct folio *dst, struct folio *src, |
5c3f9a67 | 937 | enum migrate_mode mode) |
e24f0b8f | 938 | { |
bda807d4 | 939 | int rc = -EAGAIN; |
e7e3ffeb | 940 | bool is_lru = !__PageMovable(&src->page); |
e24f0b8f | 941 | |
e7e3ffeb MWO |
942 | VM_BUG_ON_FOLIO(!folio_test_locked(src), src); |
943 | VM_BUG_ON_FOLIO(!folio_test_locked(dst), dst); | |
e24f0b8f | 944 | |
bda807d4 | 945 | if (likely(is_lru)) { |
68f2736a MWO |
946 | struct address_space *mapping = folio_mapping(src); |
947 | ||
bda807d4 | 948 | if (!mapping) |
54184650 | 949 | rc = migrate_folio(mapping, dst, src, mode); |
5490da4f | 950 | else if (mapping->a_ops->migrate_folio) |
bda807d4 | 951 | /* |
5490da4f MWO |
952 | * Most folios have a mapping and most filesystems |
953 | * provide a migrate_folio callback. Anonymous folios | |
bda807d4 | 954 | * are part of swap space which also has its own |
5490da4f | 955 | * migrate_folio callback. This is the most common path |
bda807d4 MK |
956 | * for page migration. |
957 | */ | |
5490da4f MWO |
958 | rc = mapping->a_ops->migrate_folio(mapping, dst, src, |
959 | mode); | |
bda807d4 | 960 | else |
8faa8ef5 | 961 | rc = fallback_migrate_folio(mapping, dst, src, mode); |
bda807d4 | 962 | } else { |
68f2736a MWO |
963 | const struct movable_operations *mops; |
964 | ||
e24f0b8f | 965 | /* |
bda807d4 MK |
966 | * In case of non-lru page, it could be released after |
967 | * isolation step. In that case, we shouldn't try migration. | |
e24f0b8f | 968 | */ |
e7e3ffeb MWO |
969 | VM_BUG_ON_FOLIO(!folio_test_isolated(src), src); |
970 | if (!folio_test_movable(src)) { | |
bda807d4 | 971 | rc = MIGRATEPAGE_SUCCESS; |
e7e3ffeb | 972 | folio_clear_isolated(src); |
bda807d4 MK |
973 | goto out; |
974 | } | |
975 | ||
68f2736a MWO |
976 | mops = page_movable_ops(&src->page); |
977 | rc = mops->migrate_page(&dst->page, &src->page, mode); | |
bda807d4 | 978 | WARN_ON_ONCE(rc == MIGRATEPAGE_SUCCESS && |
e7e3ffeb | 979 | !folio_test_isolated(src)); |
bda807d4 | 980 | } |
e24f0b8f | 981 | |
5c3f9a67 | 982 | /* |
e7e3ffeb MWO |
983 | * When successful, old pagecache src->mapping must be cleared before |
984 | * src is freed; but stats require that PageAnon be left as PageAnon. | |
5c3f9a67 HD |
985 | */ |
986 | if (rc == MIGRATEPAGE_SUCCESS) { | |
e7e3ffeb MWO |
987 | if (__PageMovable(&src->page)) { |
988 | VM_BUG_ON_FOLIO(!folio_test_isolated(src), src); | |
bda807d4 MK |
989 | |
990 | /* | |
991 | * We clear PG_movable under page_lock so any compactor | |
992 | * cannot try to migrate this page. | |
993 | */ | |
e7e3ffeb | 994 | folio_clear_isolated(src); |
bda807d4 MK |
995 | } |
996 | ||
997 | /* | |
e7e3ffeb | 998 | * Anonymous and movable src->mapping will be cleared by |
bda807d4 MK |
999 | * free_pages_prepare so don't reset it here for keeping |
1000 | * the type to work PageAnon, for example. | |
1001 | */ | |
e7e3ffeb MWO |
1002 | if (!folio_mapping_flags(src)) |
1003 | src->mapping = NULL; | |
d2b2c6dd | 1004 | |
e7e3ffeb MWO |
1005 | if (likely(!folio_is_zone_device(dst))) |
1006 | flush_dcache_folio(dst); | |
3fe2011f | 1007 | } |
bda807d4 | 1008 | out: |
e24f0b8f CL |
1009 | return rc; |
1010 | } | |
1011 | ||
682a71a1 | 1012 | static int __unmap_and_move(struct folio *src, struct folio *dst, |
9c620e2b | 1013 | int force, enum migrate_mode mode) |
e24f0b8f | 1014 | { |
0dabec93 | 1015 | int rc = -EAGAIN; |
213ecb31 | 1016 | bool page_was_mapped = false; |
3f6c8272 | 1017 | struct anon_vma *anon_vma = NULL; |
682a71a1 | 1018 | bool is_lru = !__PageMovable(&src->page); |
95a402c3 | 1019 | |
682a71a1 | 1020 | if (!folio_trylock(src)) { |
a6bc32b8 | 1021 | if (!force || mode == MIGRATE_ASYNC) |
0dabec93 | 1022 | goto out; |
3e7d3449 MG |
1023 | |
1024 | /* | |
1025 | * It's not safe for direct compaction to call lock_page. | |
1026 | * For example, during page readahead pages are added locked | |
1027 | * to the LRU. Later, when the IO completes the pages are | |
1028 | * marked uptodate and unlocked. However, the queueing | |
1029 | * could be merging multiple pages for one bio (e.g. | |
d4388340 | 1030 | * mpage_readahead). If an allocation happens for the |
3e7d3449 MG |
1031 | * second or third page, the process can end up locking |
1032 | * the same page twice and deadlocking. Rather than | |
1033 | * trying to be clever about what pages can be locked, | |
1034 | * avoid the use of lock_page for direct compaction | |
1035 | * altogether. | |
1036 | */ | |
1037 | if (current->flags & PF_MEMALLOC) | |
0dabec93 | 1038 | goto out; |
3e7d3449 | 1039 | |
682a71a1 | 1040 | folio_lock(src); |
e24f0b8f CL |
1041 | } |
1042 | ||
682a71a1 | 1043 | if (folio_test_writeback(src)) { |
11bc82d6 | 1044 | /* |
fed5b64a | 1045 | * Only in the case of a full synchronous migration is it |
a6bc32b8 MG |
1046 | * necessary to wait for PageWriteback. In the async case, |
1047 | * the retry loop is too short and in the sync-light case, | |
1048 | * the overhead of stalling is too much | |
11bc82d6 | 1049 | */ |
2916ecc0 JG |
1050 | switch (mode) { |
1051 | case MIGRATE_SYNC: | |
1052 | case MIGRATE_SYNC_NO_COPY: | |
1053 | break; | |
1054 | default: | |
11bc82d6 | 1055 | rc = -EBUSY; |
0a31bc97 | 1056 | goto out_unlock; |
11bc82d6 AA |
1057 | } |
1058 | if (!force) | |
0a31bc97 | 1059 | goto out_unlock; |
682a71a1 | 1060 | folio_wait_writeback(src); |
e24f0b8f | 1061 | } |
03f15c86 | 1062 | |
e24f0b8f | 1063 | /* |
682a71a1 MWO |
1064 | * By try_to_migrate(), src->mapcount goes down to 0 here. In this case, |
1065 | * we cannot notice that anon_vma is freed while we migrate a page. | |
1ce82b69 | 1066 | * This get_anon_vma() delays freeing anon_vma pointer until the end |
dc386d4d | 1067 | * of migration. File cache pages are no problem because of page_lock() |
989f89c5 KH |
1068 | * File Caches may use write_page() or lock_page() in migration, then, |
1069 | * just care Anon page here. | |
03f15c86 | 1070 | * |
29eea9b5 | 1071 | * Only folio_get_anon_vma() understands the subtleties of |
03f15c86 HD |
1072 | * getting a hold on an anon_vma from outside one of its mms. |
1073 | * But if we cannot get anon_vma, then we won't need it anyway, | |
1074 | * because that implies that the anon page is no longer mapped | |
1075 | * (and cannot be remapped so long as we hold the page lock). | |
dc386d4d | 1076 | */ |
682a71a1 | 1077 | if (folio_test_anon(src) && !folio_test_ksm(src)) |
29eea9b5 | 1078 | anon_vma = folio_get_anon_vma(src); |
62e1c553 | 1079 | |
7db7671f HD |
1080 | /* |
1081 | * Block others from accessing the new page when we get around to | |
1082 | * establishing additional references. We are usually the only one | |
682a71a1 MWO |
1083 | * holding a reference to dst at this point. We used to have a BUG |
1084 | * here if folio_trylock(dst) fails, but would like to allow for | |
1085 | * cases where there might be a race with the previous use of dst. | |
7db7671f HD |
1086 | * This is much like races on refcount of oldpage: just don't BUG(). |
1087 | */ | |
682a71a1 | 1088 | if (unlikely(!folio_trylock(dst))) |
7db7671f HD |
1089 | goto out_unlock; |
1090 | ||
bda807d4 | 1091 | if (unlikely(!is_lru)) { |
682a71a1 | 1092 | rc = move_to_new_folio(dst, src, mode); |
bda807d4 MK |
1093 | goto out_unlock_both; |
1094 | } | |
1095 | ||
dc386d4d | 1096 | /* |
62e1c553 SL |
1097 | * Corner case handling: |
1098 | * 1. When a new swap-cache page is read into, it is added to the LRU | |
1099 | * and treated as swapcache but it has no rmap yet. | |
682a71a1 | 1100 | * Calling try_to_unmap() against a src->mapping==NULL page will |
62e1c553 | 1101 | * trigger a BUG. So handle it here. |
d12b8951 | 1102 | * 2. An orphaned page (see truncate_cleanup_page) might have |
62e1c553 SL |
1103 | * fs-private metadata. The page can be picked up due to memory |
1104 | * offlining. Everywhere else except page reclaim, the page is | |
1105 | * invisible to the vm, so the page can not be migrated. So try to | |
1106 | * free the metadata, so the page can be freed. | |
e24f0b8f | 1107 | */ |
682a71a1 MWO |
1108 | if (!src->mapping) { |
1109 | if (folio_test_private(src)) { | |
1110 | try_to_free_buffers(src); | |
7db7671f | 1111 | goto out_unlock_both; |
62e1c553 | 1112 | } |
682a71a1 | 1113 | } else if (folio_mapped(src)) { |
7db7671f | 1114 | /* Establish migration ptes */ |
682a71a1 MWO |
1115 | VM_BUG_ON_FOLIO(folio_test_anon(src) && |
1116 | !folio_test_ksm(src) && !anon_vma, src); | |
1117 | try_to_migrate(src, 0); | |
213ecb31 | 1118 | page_was_mapped = true; |
2ebba6b7 | 1119 | } |
dc386d4d | 1120 | |
682a71a1 MWO |
1121 | if (!folio_mapped(src)) |
1122 | rc = move_to_new_folio(dst, src, mode); | |
e24f0b8f | 1123 | |
c3096e67 | 1124 | /* |
682a71a1 | 1125 | * When successful, push dst to LRU immediately: so that if it |
c3096e67 | 1126 | * turns out to be an mlocked page, remove_migration_ptes() will |
682a71a1 | 1127 | * automatically build up the correct dst->mlock_count for it. |
c3096e67 HD |
1128 | * |
1129 | * We would like to do something similar for the old page, when | |
1130 | * unsuccessful, and other cases when a page has been temporarily | |
1131 | * isolated from the unevictable LRU: but this case is the easiest. | |
1132 | */ | |
1133 | if (rc == MIGRATEPAGE_SUCCESS) { | |
682a71a1 | 1134 | folio_add_lru(dst); |
c3096e67 HD |
1135 | if (page_was_mapped) |
1136 | lru_add_drain(); | |
1137 | } | |
1138 | ||
5c3f9a67 | 1139 | if (page_was_mapped) |
682a71a1 MWO |
1140 | remove_migration_ptes(src, |
1141 | rc == MIGRATEPAGE_SUCCESS ? dst : src, false); | |
3f6c8272 | 1142 | |
7db7671f | 1143 | out_unlock_both: |
682a71a1 | 1144 | folio_unlock(dst); |
7db7671f | 1145 | out_unlock: |
3f6c8272 | 1146 | /* Drop an anon_vma reference if we took one */ |
76545066 | 1147 | if (anon_vma) |
9e60109f | 1148 | put_anon_vma(anon_vma); |
682a71a1 | 1149 | folio_unlock(src); |
0dabec93 | 1150 | out: |
c6c919eb | 1151 | /* |
682a71a1 | 1152 | * If migration is successful, decrease refcount of dst, |
c6c919eb | 1153 | * which will not free the page because new page owner increased |
c3096e67 | 1154 | * refcounter. |
c6c919eb | 1155 | */ |
c3096e67 | 1156 | if (rc == MIGRATEPAGE_SUCCESS) |
682a71a1 | 1157 | folio_put(dst); |
c6c919eb | 1158 | |
0dabec93 MK |
1159 | return rc; |
1160 | } | |
95a402c3 | 1161 | |
0dabec93 | 1162 | /* |
49f51859 HY |
1163 | * Obtain the lock on folio, remove all ptes and migrate the folio |
1164 | * to the newly allocated folio in dst. | |
0dabec93 | 1165 | */ |
6ec4476a | 1166 | static int unmap_and_move(new_page_t get_new_page, |
ef2a5153 | 1167 | free_page_t put_new_page, |
49f51859 | 1168 | unsigned long private, struct folio *src, |
add05cec | 1169 | int force, enum migrate_mode mode, |
dd4ae78a YS |
1170 | enum migrate_reason reason, |
1171 | struct list_head *ret) | |
0dabec93 | 1172 | { |
49f51859 | 1173 | struct folio *dst; |
2def7424 | 1174 | int rc = MIGRATEPAGE_SUCCESS; |
74d4a579 | 1175 | struct page *newpage = NULL; |
0dabec93 | 1176 | |
49f51859 | 1177 | if (!thp_migration_supported() && folio_test_transhuge(src)) |
d532e2e5 | 1178 | return -ENOSYS; |
94723aaf | 1179 | |
49f51859 HY |
1180 | if (folio_ref_count(src) == 1) { |
1181 | /* Folio was freed from under us. So we are done. */ | |
1182 | folio_clear_active(src); | |
1183 | folio_clear_unevictable(src); | |
160088b3 | 1184 | /* free_pages_prepare() will clear PG_isolated. */ |
0dabec93 MK |
1185 | goto out; |
1186 | } | |
1187 | ||
49f51859 | 1188 | newpage = get_new_page(&src->page, private); |
74d4a579 YS |
1189 | if (!newpage) |
1190 | return -ENOMEM; | |
682a71a1 | 1191 | dst = page_folio(newpage); |
74d4a579 | 1192 | |
4c74b65f | 1193 | dst->private = NULL; |
682a71a1 | 1194 | rc = __unmap_and_move(src, dst, force, mode); |
c6c919eb | 1195 | if (rc == MIGRATEPAGE_SUCCESS) |
49f51859 | 1196 | set_page_owner_migrate_reason(&dst->page, reason); |
bf6bddf1 | 1197 | |
0dabec93 | 1198 | out: |
e24f0b8f | 1199 | if (rc != -EAGAIN) { |
0dabec93 | 1200 | /* |
49f51859 HY |
1201 | * A folio that has been migrated has all references |
1202 | * removed and will be freed. A folio that has not been | |
c23a0c99 | 1203 | * migrated will have kept its references and be restored. |
0dabec93 | 1204 | */ |
49f51859 | 1205 | list_del(&src->lru); |
dd4ae78a | 1206 | } |
6afcf8ef | 1207 | |
dd4ae78a YS |
1208 | /* |
1209 | * If migration is successful, releases reference grabbed during | |
49f51859 | 1210 | * isolation. Otherwise, restore the folio to right list unless |
dd4ae78a YS |
1211 | * we want to retry. |
1212 | */ | |
1213 | if (rc == MIGRATEPAGE_SUCCESS) { | |
6afcf8ef | 1214 | /* |
49f51859 | 1215 | * Compaction can migrate also non-LRU folios which are |
6afcf8ef | 1216 | * not accounted to NR_ISOLATED_*. They can be recognized |
49f51859 | 1217 | * as __folio_test_movable |
6afcf8ef | 1218 | */ |
49f51859 HY |
1219 | if (likely(!__folio_test_movable(src))) |
1220 | mod_node_page_state(folio_pgdat(src), NR_ISOLATED_ANON + | |
1221 | folio_is_file_lru(src), -folio_nr_pages(src)); | |
c6c919eb | 1222 | |
79f5f8fa | 1223 | if (reason != MR_MEMORY_FAILURE) |
d7e69488 | 1224 | /* |
49f51859 | 1225 | * We release the folio in page_handle_poison. |
d7e69488 | 1226 | */ |
49f51859 | 1227 | folio_put(src); |
c6c919eb | 1228 | } else { |
dd4ae78a | 1229 | if (rc != -EAGAIN) |
49f51859 | 1230 | list_add_tail(&src->lru, ret); |
bda807d4 | 1231 | |
c6c919eb | 1232 | if (put_new_page) |
49f51859 | 1233 | put_new_page(&dst->page, private); |
c6c919eb | 1234 | else |
49f51859 | 1235 | folio_put(dst); |
e24f0b8f | 1236 | } |
68711a74 | 1237 | |
e24f0b8f CL |
1238 | return rc; |
1239 | } | |
1240 | ||
290408d4 NH |
1241 | /* |
1242 | * Counterpart of unmap_and_move_page() for hugepage migration. | |
1243 | * | |
1244 | * This function doesn't wait the completion of hugepage I/O | |
1245 | * because there is no race between I/O and migration for hugepage. | |
1246 | * Note that currently hugepage I/O occurs only in direct I/O | |
1247 | * where no lock is held and PG_writeback is irrelevant, | |
1248 | * and writeback status of all subpages are counted in the reference | |
1249 | * count of the head page (i.e. if all subpages of a 2MB hugepage are | |
1250 | * under direct I/O, the reference of the head page is 512 and a bit more.) | |
1251 | * This means that when we try to migrate hugepage whose subpages are | |
1252 | * doing direct I/O, some references remain after try_to_unmap() and | |
1253 | * hugepage migration fails without data corruption. | |
1254 | * | |
1255 | * There is also no race when direct I/O is issued on the page under migration, | |
1256 | * because then pte is replaced with migration swap entry and direct I/O code | |
1257 | * will wait in the page fault for migration to complete. | |
1258 | */ | |
1259 | static int unmap_and_move_huge_page(new_page_t get_new_page, | |
68711a74 DR |
1260 | free_page_t put_new_page, unsigned long private, |
1261 | struct page *hpage, int force, | |
dd4ae78a YS |
1262 | enum migrate_mode mode, int reason, |
1263 | struct list_head *ret) | |
290408d4 | 1264 | { |
4eecb8b9 | 1265 | struct folio *dst, *src = page_folio(hpage); |
2def7424 | 1266 | int rc = -EAGAIN; |
2ebba6b7 | 1267 | int page_was_mapped = 0; |
32665f2b | 1268 | struct page *new_hpage; |
290408d4 | 1269 | struct anon_vma *anon_vma = NULL; |
c0d0381a | 1270 | struct address_space *mapping = NULL; |
290408d4 | 1271 | |
83467efb | 1272 | /* |
7ed2c31d | 1273 | * Migratability of hugepages depends on architectures and their size. |
83467efb NH |
1274 | * This check is necessary because some callers of hugepage migration |
1275 | * like soft offline and memory hotremove don't walk through page | |
1276 | * tables or check whether the hugepage is pmd-based or not before | |
1277 | * kicking migration. | |
1278 | */ | |
577be05c | 1279 | if (!hugepage_migration_supported(page_hstate(hpage))) |
83467efb NH |
1280 | return -ENOSYS; |
1281 | ||
c33db292 | 1282 | if (folio_ref_count(src) == 1) { |
71a64f61 MS |
1283 | /* page was freed from under us. So we are done. */ |
1284 | putback_active_hugepage(hpage); | |
1285 | return MIGRATEPAGE_SUCCESS; | |
1286 | } | |
1287 | ||
666feb21 | 1288 | new_hpage = get_new_page(hpage, private); |
290408d4 NH |
1289 | if (!new_hpage) |
1290 | return -ENOMEM; | |
4eecb8b9 | 1291 | dst = page_folio(new_hpage); |
290408d4 | 1292 | |
c33db292 | 1293 | if (!folio_trylock(src)) { |
2916ecc0 | 1294 | if (!force) |
290408d4 | 1295 | goto out; |
2916ecc0 JG |
1296 | switch (mode) { |
1297 | case MIGRATE_SYNC: | |
1298 | case MIGRATE_SYNC_NO_COPY: | |
1299 | break; | |
1300 | default: | |
1301 | goto out; | |
1302 | } | |
c33db292 | 1303 | folio_lock(src); |
290408d4 NH |
1304 | } |
1305 | ||
cb6acd01 MK |
1306 | /* |
1307 | * Check for pages which are in the process of being freed. Without | |
c33db292 | 1308 | * folio_mapping() set, hugetlbfs specific move page routine will not |
cb6acd01 MK |
1309 | * be called and we could leak usage counts for subpools. |
1310 | */ | |
345c62d1 | 1311 | if (hugetlb_folio_subpool(src) && !folio_mapping(src)) { |
cb6acd01 MK |
1312 | rc = -EBUSY; |
1313 | goto out_unlock; | |
1314 | } | |
1315 | ||
c33db292 | 1316 | if (folio_test_anon(src)) |
29eea9b5 | 1317 | anon_vma = folio_get_anon_vma(src); |
290408d4 | 1318 | |
c33db292 | 1319 | if (unlikely(!folio_trylock(dst))) |
7db7671f HD |
1320 | goto put_anon; |
1321 | ||
c33db292 | 1322 | if (folio_mapped(src)) { |
a98a2f0c | 1323 | enum ttu_flags ttu = 0; |
336bf30e | 1324 | |
c33db292 | 1325 | if (!folio_test_anon(src)) { |
336bf30e MK |
1326 | /* |
1327 | * In shared mappings, try_to_unmap could potentially | |
1328 | * call huge_pmd_unshare. Because of this, take | |
1329 | * semaphore in write mode here and set TTU_RMAP_LOCKED | |
1330 | * to let lower levels know we have taken the lock. | |
1331 | */ | |
1332 | mapping = hugetlb_page_mapping_lock_write(hpage); | |
1333 | if (unlikely(!mapping)) | |
1334 | goto unlock_put_anon; | |
1335 | ||
5202978b | 1336 | ttu = TTU_RMAP_LOCKED; |
336bf30e | 1337 | } |
c0d0381a | 1338 | |
4b8554c5 | 1339 | try_to_migrate(src, ttu); |
2ebba6b7 | 1340 | page_was_mapped = 1; |
336bf30e | 1341 | |
5202978b | 1342 | if (ttu & TTU_RMAP_LOCKED) |
336bf30e | 1343 | i_mmap_unlock_write(mapping); |
2ebba6b7 | 1344 | } |
290408d4 | 1345 | |
c33db292 | 1346 | if (!folio_mapped(src)) |
e7e3ffeb | 1347 | rc = move_to_new_folio(dst, src, mode); |
290408d4 | 1348 | |
336bf30e | 1349 | if (page_was_mapped) |
4eecb8b9 MWO |
1350 | remove_migration_ptes(src, |
1351 | rc == MIGRATEPAGE_SUCCESS ? dst : src, false); | |
290408d4 | 1352 | |
c0d0381a | 1353 | unlock_put_anon: |
c33db292 | 1354 | folio_unlock(dst); |
7db7671f HD |
1355 | |
1356 | put_anon: | |
fd4a4663 | 1357 | if (anon_vma) |
9e60109f | 1358 | put_anon_vma(anon_vma); |
8e6ac7fa | 1359 | |
2def7424 | 1360 | if (rc == MIGRATEPAGE_SUCCESS) { |
345c62d1 | 1361 | move_hugetlb_state(src, dst, reason); |
2def7424 HD |
1362 | put_new_page = NULL; |
1363 | } | |
8e6ac7fa | 1364 | |
cb6acd01 | 1365 | out_unlock: |
c33db292 | 1366 | folio_unlock(src); |
09761333 | 1367 | out: |
dd4ae78a | 1368 | if (rc == MIGRATEPAGE_SUCCESS) |
b8ec1cee | 1369 | putback_active_hugepage(hpage); |
a04840c6 | 1370 | else if (rc != -EAGAIN) |
c33db292 | 1371 | list_move_tail(&src->lru, ret); |
68711a74 DR |
1372 | |
1373 | /* | |
1374 | * If migration was not successful and there's a freeing callback, use | |
1375 | * it. Otherwise, put_page() will drop the reference grabbed during | |
1376 | * isolation. | |
1377 | */ | |
2def7424 | 1378 | if (put_new_page) |
68711a74 DR |
1379 | put_new_page(new_hpage, private); |
1380 | else | |
3aaa76e1 | 1381 | putback_active_hugepage(new_hpage); |
68711a74 | 1382 | |
290408d4 NH |
1383 | return rc; |
1384 | } | |
1385 | ||
eaec4e63 | 1386 | static inline int try_split_folio(struct folio *folio, struct list_head *split_folios) |
d532e2e5 | 1387 | { |
9c62ff00 | 1388 | int rc; |
d532e2e5 | 1389 | |
eaec4e63 HY |
1390 | folio_lock(folio); |
1391 | rc = split_folio_to_list(folio, split_folios); | |
1392 | folio_unlock(folio); | |
e6fa8a79 | 1393 | if (!rc) |
eaec4e63 | 1394 | list_move_tail(&folio->lru, split_folios); |
d532e2e5 YS |
1395 | |
1396 | return rc; | |
1397 | } | |
1398 | ||
b20a3503 | 1399 | /* |
eaec4e63 | 1400 | * migrate_pages - migrate the folios specified in a list, to the free folios |
c73e5c9c | 1401 | * supplied as the target for the page migration |
b20a3503 | 1402 | * |
eaec4e63 HY |
1403 | * @from: The list of folios to be migrated. |
1404 | * @get_new_page: The function used to allocate free folios to be used | |
1405 | * as the target of the folio migration. | |
1406 | * @put_new_page: The function used to free target folios if migration | |
68711a74 | 1407 | * fails, or NULL if no special handling is necessary. |
c73e5c9c SB |
1408 | * @private: Private data to be passed on to get_new_page() |
1409 | * @mode: The migration mode that specifies the constraints for | |
eaec4e63 HY |
1410 | * folio migration, if any. |
1411 | * @reason: The reason for folio migration. | |
1412 | * @ret_succeeded: Set to the number of folios migrated successfully if | |
5ac95884 | 1413 | * the caller passes a non-NULL pointer. |
b20a3503 | 1414 | * |
eaec4e63 HY |
1415 | * The function returns after 10 attempts or if no folios are movable any more |
1416 | * because the list has become empty or no retryable folios exist any more. | |
1417 | * It is caller's responsibility to call putback_movable_pages() to return folios | |
dd4ae78a | 1418 | * to the LRU or free list only if ret != 0. |
b20a3503 | 1419 | * |
eaec4e63 HY |
1420 | * Returns the number of {normal folio, large folio, hugetlb} that were not |
1421 | * migrated, or an error code. The number of large folio splits will be | |
1422 | * considered as the number of non-migrated large folio, no matter how many | |
1423 | * split folios of the large folio are migrated successfully. | |
b20a3503 | 1424 | */ |
9c620e2b | 1425 | int migrate_pages(struct list_head *from, new_page_t get_new_page, |
68711a74 | 1426 | free_page_t put_new_page, unsigned long private, |
5ac95884 | 1427 | enum migrate_mode mode, int reason, unsigned int *ret_succeeded) |
b20a3503 | 1428 | { |
e24f0b8f | 1429 | int retry = 1; |
eaec4e63 | 1430 | int large_retry = 1; |
1a5bae25 | 1431 | int thp_retry = 1; |
b20a3503 | 1432 | int nr_failed = 0; |
b5bade97 | 1433 | int nr_failed_pages = 0; |
077309bc | 1434 | int nr_retry_pages = 0; |
5647bc29 | 1435 | int nr_succeeded = 0; |
1a5bae25 | 1436 | int nr_thp_succeeded = 0; |
eaec4e63 | 1437 | int nr_large_failed = 0; |
1a5bae25 AK |
1438 | int nr_thp_failed = 0; |
1439 | int nr_thp_split = 0; | |
b20a3503 | 1440 | int pass = 0; |
eaec4e63 | 1441 | bool is_large = false; |
1a5bae25 | 1442 | bool is_thp = false; |
eaec4e63 HY |
1443 | struct folio *folio, *folio2; |
1444 | int rc, nr_pages; | |
1445 | LIST_HEAD(ret_folios); | |
1446 | LIST_HEAD(split_folios); | |
b0b515bf | 1447 | bool nosplit = (reason == MR_NUMA_MISPLACED); |
eaec4e63 | 1448 | bool no_split_folio_counting = false; |
b20a3503 | 1449 | |
7bc1aec5 LM |
1450 | trace_mm_migrate_pages_start(mode, reason); |
1451 | ||
eaec4e63 HY |
1452 | split_folio_migration: |
1453 | for (pass = 0; pass < 10 && (retry || large_retry); pass++) { | |
e24f0b8f | 1454 | retry = 0; |
eaec4e63 | 1455 | large_retry = 0; |
1a5bae25 | 1456 | thp_retry = 0; |
077309bc | 1457 | nr_retry_pages = 0; |
b20a3503 | 1458 | |
eaec4e63 | 1459 | list_for_each_entry_safe(folio, folio2, from, lru) { |
1a5bae25 | 1460 | /* |
eaec4e63 HY |
1461 | * Large folio statistics is based on the source large |
1462 | * folio. Capture required information that might get | |
1463 | * lost during migration. | |
1a5bae25 | 1464 | */ |
eaec4e63 HY |
1465 | is_large = folio_test_large(folio) && !folio_test_hugetlb(folio); |
1466 | is_thp = is_large && folio_test_pmd_mappable(folio); | |
1467 | nr_pages = folio_nr_pages(folio); | |
e24f0b8f | 1468 | cond_resched(); |
2d1db3b1 | 1469 | |
eaec4e63 | 1470 | if (folio_test_hugetlb(folio)) |
31caf665 | 1471 | rc = unmap_and_move_huge_page(get_new_page, |
eaec4e63 HY |
1472 | put_new_page, private, |
1473 | &folio->page, pass > 2, mode, | |
1474 | reason, | |
1475 | &ret_folios); | |
31caf665 | 1476 | else |
68711a74 | 1477 | rc = unmap_and_move(get_new_page, put_new_page, |
eaec4e63 HY |
1478 | private, folio, pass > 2, mode, |
1479 | reason, &ret_folios); | |
dd4ae78a YS |
1480 | /* |
1481 | * The rules are: | |
eaec4e63 HY |
1482 | * Success: non hugetlb folio will be freed, hugetlb |
1483 | * folio will be put back | |
dd4ae78a YS |
1484 | * -EAGAIN: stay on the from list |
1485 | * -ENOMEM: stay on the from list | |
577be05c | 1486 | * -ENOSYS: stay on the from list |
eaec4e63 | 1487 | * Other errno: put on ret_folios list then splice to |
dd4ae78a YS |
1488 | * from list |
1489 | */ | |
e24f0b8f | 1490 | switch(rc) { |
d532e2e5 | 1491 | /* |
eaec4e63 HY |
1492 | * Large folio migration might be unsupported or |
1493 | * the allocation could've failed so we should retry | |
1494 | * on the same folio with the large folio split | |
1495 | * to normal folios. | |
d532e2e5 | 1496 | * |
eaec4e63 | 1497 | * Split folios are put in split_folios, and |
e6fa8a79 HY |
1498 | * we will migrate them after the rest of the |
1499 | * list is processed. | |
d532e2e5 YS |
1500 | */ |
1501 | case -ENOSYS: | |
eaec4e63 HY |
1502 | /* Large folio migration is unsupported */ |
1503 | if (is_large) { | |
1504 | nr_large_failed++; | |
1505 | nr_thp_failed += is_thp; | |
1506 | if (!try_split_folio(folio, &split_folios)) { | |
1507 | nr_thp_split += is_thp; | |
e6fa8a79 | 1508 | break; |
d532e2e5 | 1509 | } |
d532e2e5 | 1510 | /* Hugetlb migration is unsupported */ |
eaec4e63 | 1511 | } else if (!no_split_folio_counting) { |
b5bade97 | 1512 | nr_failed++; |
f430893b ML |
1513 | } |
1514 | ||
eaec4e63 HY |
1515 | nr_failed_pages += nr_pages; |
1516 | list_move_tail(&folio->lru, &ret_folios); | |
d532e2e5 | 1517 | break; |
95a402c3 | 1518 | case -ENOMEM: |
94723aaf | 1519 | /* |
d532e2e5 | 1520 | * When memory is low, don't bother to try to migrate |
eaec4e63 | 1521 | * other folios, just exit. |
94723aaf | 1522 | */ |
eaec4e63 HY |
1523 | if (is_large) { |
1524 | nr_large_failed++; | |
1525 | nr_thp_failed += is_thp; | |
1526 | /* Large folio NUMA faulting doesn't split to retry. */ | |
fd4a7ac3 | 1527 | if (!nosplit) { |
eaec4e63 | 1528 | int ret = try_split_folio(folio, &split_folios); |
fd4a7ac3 BW |
1529 | |
1530 | if (!ret) { | |
eaec4e63 | 1531 | nr_thp_split += is_thp; |
fd4a7ac3 BW |
1532 | break; |
1533 | } else if (reason == MR_LONGTERM_PIN && | |
1534 | ret == -EAGAIN) { | |
1535 | /* | |
eaec4e63 HY |
1536 | * Try again to split large folio to |
1537 | * mitigate the failure of longterm pinning. | |
fd4a7ac3 | 1538 | */ |
eaec4e63 HY |
1539 | large_retry++; |
1540 | thp_retry += is_thp; | |
1541 | nr_retry_pages += nr_pages; | |
fd4a7ac3 BW |
1542 | break; |
1543 | } | |
94723aaf | 1544 | } |
eaec4e63 | 1545 | } else if (!no_split_folio_counting) { |
f430893b | 1546 | nr_failed++; |
1a5bae25 | 1547 | } |
b5bade97 | 1548 | |
eaec4e63 | 1549 | nr_failed_pages += nr_pages + nr_retry_pages; |
69a041ff | 1550 | /* |
eaec4e63 HY |
1551 | * There might be some split folios of fail-to-migrate large |
1552 | * folios left in split_folios list. Move them back to migration | |
69a041ff | 1553 | * list so that they could be put back to the right list by |
eaec4e63 | 1554 | * the caller otherwise the folio refcnt will be leaked. |
69a041ff | 1555 | */ |
eaec4e63 | 1556 | list_splice_init(&split_folios, from); |
fbed53b4 | 1557 | /* nr_failed isn't updated for not used */ |
eaec4e63 | 1558 | nr_large_failed += large_retry; |
69a041ff | 1559 | nr_thp_failed += thp_retry; |
95a402c3 | 1560 | goto out; |
e24f0b8f | 1561 | case -EAGAIN: |
eaec4e63 HY |
1562 | if (is_large) { |
1563 | large_retry++; | |
1564 | thp_retry += is_thp; | |
1565 | } else if (!no_split_folio_counting) { | |
f430893b | 1566 | retry++; |
eaec4e63 HY |
1567 | } |
1568 | nr_retry_pages += nr_pages; | |
e24f0b8f | 1569 | break; |
78bd5209 | 1570 | case MIGRATEPAGE_SUCCESS: |
eaec4e63 HY |
1571 | nr_succeeded += nr_pages; |
1572 | nr_thp_succeeded += is_thp; | |
e24f0b8f CL |
1573 | break; |
1574 | default: | |
354a3363 | 1575 | /* |
d532e2e5 | 1576 | * Permanent failure (-EBUSY, etc.): |
eaec4e63 HY |
1577 | * unlike -EAGAIN case, the failed folio is |
1578 | * removed from migration folio list and not | |
354a3363 NH |
1579 | * retried in the next outer loop. |
1580 | */ | |
eaec4e63 HY |
1581 | if (is_large) { |
1582 | nr_large_failed++; | |
1583 | nr_thp_failed += is_thp; | |
1584 | } else if (!no_split_folio_counting) { | |
b5bade97 | 1585 | nr_failed++; |
eaec4e63 | 1586 | } |
f430893b | 1587 | |
eaec4e63 | 1588 | nr_failed_pages += nr_pages; |
e24f0b8f | 1589 | break; |
2d1db3b1 | 1590 | } |
b20a3503 CL |
1591 | } |
1592 | } | |
7047b5a4 | 1593 | nr_failed += retry; |
eaec4e63 | 1594 | nr_large_failed += large_retry; |
1a5bae25 | 1595 | nr_thp_failed += thp_retry; |
077309bc | 1596 | nr_failed_pages += nr_retry_pages; |
b5bade97 | 1597 | /* |
eaec4e63 HY |
1598 | * Try to migrate split folios of fail-to-migrate large folios, no |
1599 | * nr_failed counting in this round, since all split folios of a | |
1600 | * large folio is counted as 1 failure in the first round. | |
b5bade97 | 1601 | */ |
eaec4e63 | 1602 | if (!list_empty(&split_folios)) { |
b5bade97 | 1603 | /* |
eaec4e63 | 1604 | * Move non-migrated folios (after 10 retries) to ret_folios |
b5bade97 BW |
1605 | * to avoid migrating them again. |
1606 | */ | |
eaec4e63 HY |
1607 | list_splice_init(from, &ret_folios); |
1608 | list_splice_init(&split_folios, from); | |
1609 | no_split_folio_counting = true; | |
b5bade97 | 1610 | retry = 1; |
eaec4e63 | 1611 | goto split_folio_migration; |
b5bade97 BW |
1612 | } |
1613 | ||
eaec4e63 | 1614 | rc = nr_failed + nr_large_failed; |
95a402c3 | 1615 | out: |
dd4ae78a | 1616 | /* |
eaec4e63 | 1617 | * Put the permanent failure folio back to migration list, they |
dd4ae78a YS |
1618 | * will be put back to the right list by the caller. |
1619 | */ | |
eaec4e63 | 1620 | list_splice(&ret_folios, from); |
dd4ae78a | 1621 | |
03e5f82e | 1622 | /* |
eaec4e63 HY |
1623 | * Return 0 in case all split folios of fail-to-migrate large folios |
1624 | * are migrated successfully. | |
03e5f82e BW |
1625 | */ |
1626 | if (list_empty(from)) | |
1627 | rc = 0; | |
1628 | ||
1a5bae25 | 1629 | count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded); |
b5bade97 | 1630 | count_vm_events(PGMIGRATE_FAIL, nr_failed_pages); |
1a5bae25 AK |
1631 | count_vm_events(THP_MIGRATION_SUCCESS, nr_thp_succeeded); |
1632 | count_vm_events(THP_MIGRATION_FAIL, nr_thp_failed); | |
1633 | count_vm_events(THP_MIGRATION_SPLIT, nr_thp_split); | |
b5bade97 | 1634 | trace_mm_migrate_pages(nr_succeeded, nr_failed_pages, nr_thp_succeeded, |
1a5bae25 | 1635 | nr_thp_failed, nr_thp_split, mode, reason); |
7b2a2d4a | 1636 | |
5ac95884 YS |
1637 | if (ret_succeeded) |
1638 | *ret_succeeded = nr_succeeded; | |
1639 | ||
78bd5209 | 1640 | return rc; |
b20a3503 | 1641 | } |
95a402c3 | 1642 | |
19fc7bed | 1643 | struct page *alloc_migration_target(struct page *page, unsigned long private) |
b4b38223 | 1644 | { |
ffe06786 | 1645 | struct folio *folio = page_folio(page); |
19fc7bed JK |
1646 | struct migration_target_control *mtc; |
1647 | gfp_t gfp_mask; | |
b4b38223 | 1648 | unsigned int order = 0; |
ffe06786 | 1649 | struct folio *new_folio = NULL; |
19fc7bed JK |
1650 | int nid; |
1651 | int zidx; | |
1652 | ||
1653 | mtc = (struct migration_target_control *)private; | |
1654 | gfp_mask = mtc->gfp_mask; | |
1655 | nid = mtc->nid; | |
1656 | if (nid == NUMA_NO_NODE) | |
ffe06786 | 1657 | nid = folio_nid(folio); |
b4b38223 | 1658 | |
ffe06786 | 1659 | if (folio_test_hugetlb(folio)) { |
e51da3a9 | 1660 | struct hstate *h = folio_hstate(folio); |
d92bbc27 | 1661 | |
19fc7bed JK |
1662 | gfp_mask = htlb_modify_alloc_mask(h, gfp_mask); |
1663 | return alloc_huge_page_nodemask(h, nid, mtc->nmask, gfp_mask); | |
d92bbc27 | 1664 | } |
b4b38223 | 1665 | |
ffe06786 | 1666 | if (folio_test_large(folio)) { |
9933a0c8 JK |
1667 | /* |
1668 | * clear __GFP_RECLAIM to make the migration callback | |
1669 | * consistent with regular THP allocations. | |
1670 | */ | |
1671 | gfp_mask &= ~__GFP_RECLAIM; | |
b4b38223 | 1672 | gfp_mask |= GFP_TRANSHUGE; |
ffe06786 | 1673 | order = folio_order(folio); |
b4b38223 | 1674 | } |
ffe06786 | 1675 | zidx = zone_idx(folio_zone(folio)); |
19fc7bed | 1676 | if (is_highmem_idx(zidx) || zidx == ZONE_MOVABLE) |
b4b38223 JK |
1677 | gfp_mask |= __GFP_HIGHMEM; |
1678 | ||
ffe06786 | 1679 | new_folio = __folio_alloc(gfp_mask, order, nid, mtc->nmask); |
b4b38223 | 1680 | |
ffe06786 | 1681 | return &new_folio->page; |
b4b38223 JK |
1682 | } |
1683 | ||
742755a1 | 1684 | #ifdef CONFIG_NUMA |
742755a1 | 1685 | |
a49bd4d7 | 1686 | static int store_status(int __user *status, int start, int value, int nr) |
742755a1 | 1687 | { |
a49bd4d7 MH |
1688 | while (nr-- > 0) { |
1689 | if (put_user(value, status + start)) | |
1690 | return -EFAULT; | |
1691 | start++; | |
1692 | } | |
1693 | ||
1694 | return 0; | |
1695 | } | |
1696 | ||
1697 | static int do_move_pages_to_node(struct mm_struct *mm, | |
1698 | struct list_head *pagelist, int node) | |
1699 | { | |
1700 | int err; | |
a0976311 JK |
1701 | struct migration_target_control mtc = { |
1702 | .nid = node, | |
1703 | .gfp_mask = GFP_HIGHUSER_MOVABLE | __GFP_THISNODE, | |
1704 | }; | |
a49bd4d7 | 1705 | |
a0976311 | 1706 | err = migrate_pages(pagelist, alloc_migration_target, NULL, |
5ac95884 | 1707 | (unsigned long)&mtc, MIGRATE_SYNC, MR_SYSCALL, NULL); |
a49bd4d7 MH |
1708 | if (err) |
1709 | putback_movable_pages(pagelist); | |
1710 | return err; | |
742755a1 CL |
1711 | } |
1712 | ||
1713 | /* | |
a49bd4d7 MH |
1714 | * Resolves the given address to a struct page, isolates it from the LRU and |
1715 | * puts it to the given pagelist. | |
e0153fc2 YS |
1716 | * Returns: |
1717 | * errno - if the page cannot be found/isolated | |
1718 | * 0 - when it doesn't have to be migrated because it is already on the | |
1719 | * target node | |
1720 | * 1 - when it has been queued | |
742755a1 | 1721 | */ |
a49bd4d7 MH |
1722 | static int add_page_for_migration(struct mm_struct *mm, unsigned long addr, |
1723 | int node, struct list_head *pagelist, bool migrate_all) | |
742755a1 | 1724 | { |
a49bd4d7 MH |
1725 | struct vm_area_struct *vma; |
1726 | struct page *page; | |
742755a1 | 1727 | int err; |
742755a1 | 1728 | |
d8ed45c5 | 1729 | mmap_read_lock(mm); |
a49bd4d7 | 1730 | err = -EFAULT; |
cb1c37b1 ML |
1731 | vma = vma_lookup(mm, addr); |
1732 | if (!vma || !vma_migratable(vma)) | |
a49bd4d7 | 1733 | goto out; |
742755a1 | 1734 | |
a49bd4d7 | 1735 | /* FOLL_DUMP to ignore special (like zero) pages */ |
87d2762e | 1736 | page = follow_page(vma, addr, FOLL_GET | FOLL_DUMP); |
89f5b7da | 1737 | |
a49bd4d7 MH |
1738 | err = PTR_ERR(page); |
1739 | if (IS_ERR(page)) | |
1740 | goto out; | |
89f5b7da | 1741 | |
a49bd4d7 | 1742 | err = -ENOENT; |
f7091ed6 | 1743 | if (!page) |
a49bd4d7 | 1744 | goto out; |
742755a1 | 1745 | |
f7091ed6 HW |
1746 | if (is_zone_device_page(page)) |
1747 | goto out_putpage; | |
1748 | ||
a49bd4d7 MH |
1749 | err = 0; |
1750 | if (page_to_nid(page) == node) | |
1751 | goto out_putpage; | |
742755a1 | 1752 | |
a49bd4d7 MH |
1753 | err = -EACCES; |
1754 | if (page_mapcount(page) > 1 && !migrate_all) | |
1755 | goto out_putpage; | |
742755a1 | 1756 | |
a49bd4d7 MH |
1757 | if (PageHuge(page)) { |
1758 | if (PageHead(page)) { | |
7ce82f4c ML |
1759 | err = isolate_hugetlb(page, pagelist); |
1760 | if (!err) | |
1761 | err = 1; | |
e632a938 | 1762 | } |
a49bd4d7 MH |
1763 | } else { |
1764 | struct page *head; | |
e632a938 | 1765 | |
e8db67eb NH |
1766 | head = compound_head(page); |
1767 | err = isolate_lru_page(head); | |
cf608ac1 | 1768 | if (err) |
a49bd4d7 | 1769 | goto out_putpage; |
742755a1 | 1770 | |
e0153fc2 | 1771 | err = 1; |
a49bd4d7 MH |
1772 | list_add_tail(&head->lru, pagelist); |
1773 | mod_node_page_state(page_pgdat(head), | |
9de4f22a | 1774 | NR_ISOLATED_ANON + page_is_file_lru(head), |
6c357848 | 1775 | thp_nr_pages(head)); |
a49bd4d7 MH |
1776 | } |
1777 | out_putpage: | |
1778 | /* | |
1779 | * Either remove the duplicate refcount from | |
1780 | * isolate_lru_page() or drop the page ref if it was | |
1781 | * not isolated. | |
1782 | */ | |
1783 | put_page(page); | |
1784 | out: | |
d8ed45c5 | 1785 | mmap_read_unlock(mm); |
742755a1 CL |
1786 | return err; |
1787 | } | |
1788 | ||
7ca8783a WY |
1789 | static int move_pages_and_store_status(struct mm_struct *mm, int node, |
1790 | struct list_head *pagelist, int __user *status, | |
1791 | int start, int i, unsigned long nr_pages) | |
1792 | { | |
1793 | int err; | |
1794 | ||
5d7ae891 WY |
1795 | if (list_empty(pagelist)) |
1796 | return 0; | |
1797 | ||
7ca8783a WY |
1798 | err = do_move_pages_to_node(mm, pagelist, node); |
1799 | if (err) { | |
1800 | /* | |
1801 | * Positive err means the number of failed | |
1802 | * pages to migrate. Since we are going to | |
1803 | * abort and return the number of non-migrated | |
ab9dd4f8 | 1804 | * pages, so need to include the rest of the |
7ca8783a WY |
1805 | * nr_pages that have not been attempted as |
1806 | * well. | |
1807 | */ | |
1808 | if (err > 0) | |
a7504ed1 | 1809 | err += nr_pages - i; |
7ca8783a WY |
1810 | return err; |
1811 | } | |
1812 | return store_status(status, start, node, i - start); | |
1813 | } | |
1814 | ||
5e9a0f02 BG |
1815 | /* |
1816 | * Migrate an array of page address onto an array of nodes and fill | |
1817 | * the corresponding array of status. | |
1818 | */ | |
3268c63e | 1819 | static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes, |
5e9a0f02 BG |
1820 | unsigned long nr_pages, |
1821 | const void __user * __user *pages, | |
1822 | const int __user *nodes, | |
1823 | int __user *status, int flags) | |
1824 | { | |
a49bd4d7 MH |
1825 | int current_node = NUMA_NO_NODE; |
1826 | LIST_HEAD(pagelist); | |
1827 | int start, i; | |
1828 | int err = 0, err1; | |
35282a2d | 1829 | |
361a2a22 | 1830 | lru_cache_disable(); |
35282a2d | 1831 | |
a49bd4d7 MH |
1832 | for (i = start = 0; i < nr_pages; i++) { |
1833 | const void __user *p; | |
1834 | unsigned long addr; | |
1835 | int node; | |
3140a227 | 1836 | |
a49bd4d7 MH |
1837 | err = -EFAULT; |
1838 | if (get_user(p, pages + i)) | |
1839 | goto out_flush; | |
1840 | if (get_user(node, nodes + i)) | |
1841 | goto out_flush; | |
057d3389 | 1842 | addr = (unsigned long)untagged_addr(p); |
a49bd4d7 MH |
1843 | |
1844 | err = -ENODEV; | |
1845 | if (node < 0 || node >= MAX_NUMNODES) | |
1846 | goto out_flush; | |
1847 | if (!node_state(node, N_MEMORY)) | |
1848 | goto out_flush; | |
5e9a0f02 | 1849 | |
a49bd4d7 MH |
1850 | err = -EACCES; |
1851 | if (!node_isset(node, task_nodes)) | |
1852 | goto out_flush; | |
1853 | ||
1854 | if (current_node == NUMA_NO_NODE) { | |
1855 | current_node = node; | |
1856 | start = i; | |
1857 | } else if (node != current_node) { | |
7ca8783a WY |
1858 | err = move_pages_and_store_status(mm, current_node, |
1859 | &pagelist, status, start, i, nr_pages); | |
a49bd4d7 MH |
1860 | if (err) |
1861 | goto out; | |
1862 | start = i; | |
1863 | current_node = node; | |
3140a227 BG |
1864 | } |
1865 | ||
a49bd4d7 MH |
1866 | /* |
1867 | * Errors in the page lookup or isolation are not fatal and we simply | |
1868 | * report them via status | |
1869 | */ | |
1870 | err = add_page_for_migration(mm, addr, current_node, | |
1871 | &pagelist, flags & MPOL_MF_MOVE_ALL); | |
e0153fc2 | 1872 | |
d08221a0 | 1873 | if (err > 0) { |
e0153fc2 YS |
1874 | /* The page is successfully queued for migration */ |
1875 | continue; | |
1876 | } | |
3140a227 | 1877 | |
65462462 JH |
1878 | /* |
1879 | * The move_pages() man page does not have an -EEXIST choice, so | |
1880 | * use -EFAULT instead. | |
1881 | */ | |
1882 | if (err == -EEXIST) | |
1883 | err = -EFAULT; | |
1884 | ||
d08221a0 WY |
1885 | /* |
1886 | * If the page is already on the target node (!err), store the | |
1887 | * node, otherwise, store the err. | |
1888 | */ | |
1889 | err = store_status(status, i, err ? : current_node, 1); | |
a49bd4d7 MH |
1890 | if (err) |
1891 | goto out_flush; | |
5e9a0f02 | 1892 | |
7ca8783a WY |
1893 | err = move_pages_and_store_status(mm, current_node, &pagelist, |
1894 | status, start, i, nr_pages); | |
a7504ed1 HY |
1895 | if (err) { |
1896 | /* We have accounted for page i */ | |
1897 | if (err > 0) | |
1898 | err--; | |
4afdacec | 1899 | goto out; |
a7504ed1 | 1900 | } |
a49bd4d7 | 1901 | current_node = NUMA_NO_NODE; |
3140a227 | 1902 | } |
a49bd4d7 MH |
1903 | out_flush: |
1904 | /* Make sure we do not overwrite the existing error */ | |
7ca8783a WY |
1905 | err1 = move_pages_and_store_status(mm, current_node, &pagelist, |
1906 | status, start, i, nr_pages); | |
dfe9aa23 | 1907 | if (err >= 0) |
a49bd4d7 | 1908 | err = err1; |
5e9a0f02 | 1909 | out: |
361a2a22 | 1910 | lru_cache_enable(); |
5e9a0f02 BG |
1911 | return err; |
1912 | } | |
1913 | ||
742755a1 | 1914 | /* |
2f007e74 | 1915 | * Determine the nodes of an array of pages and store it in an array of status. |
742755a1 | 1916 | */ |
80bba129 BG |
1917 | static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages, |
1918 | const void __user **pages, int *status) | |
742755a1 | 1919 | { |
2f007e74 | 1920 | unsigned long i; |
2f007e74 | 1921 | |
d8ed45c5 | 1922 | mmap_read_lock(mm); |
742755a1 | 1923 | |
2f007e74 | 1924 | for (i = 0; i < nr_pages; i++) { |
80bba129 | 1925 | unsigned long addr = (unsigned long)(*pages); |
742755a1 CL |
1926 | struct vm_area_struct *vma; |
1927 | struct page *page; | |
c095adbc | 1928 | int err = -EFAULT; |
2f007e74 | 1929 | |
059b8b48 LH |
1930 | vma = vma_lookup(mm, addr); |
1931 | if (!vma) | |
742755a1 CL |
1932 | goto set_status; |
1933 | ||
d899844e | 1934 | /* FOLL_DUMP to ignore special (like zero) pages */ |
16fd6b31 | 1935 | page = follow_page(vma, addr, FOLL_GET | FOLL_DUMP); |
89f5b7da LT |
1936 | |
1937 | err = PTR_ERR(page); | |
1938 | if (IS_ERR(page)) | |
1939 | goto set_status; | |
1940 | ||
f7091ed6 HW |
1941 | err = -ENOENT; |
1942 | if (!page) | |
1943 | goto set_status; | |
1944 | ||
1945 | if (!is_zone_device_page(page)) | |
4cd61484 | 1946 | err = page_to_nid(page); |
f7091ed6 | 1947 | |
16fd6b31 | 1948 | put_page(page); |
742755a1 | 1949 | set_status: |
80bba129 BG |
1950 | *status = err; |
1951 | ||
1952 | pages++; | |
1953 | status++; | |
1954 | } | |
1955 | ||
d8ed45c5 | 1956 | mmap_read_unlock(mm); |
80bba129 BG |
1957 | } |
1958 | ||
5b1b561b AB |
1959 | static int get_compat_pages_array(const void __user *chunk_pages[], |
1960 | const void __user * __user *pages, | |
1961 | unsigned long chunk_nr) | |
1962 | { | |
1963 | compat_uptr_t __user *pages32 = (compat_uptr_t __user *)pages; | |
1964 | compat_uptr_t p; | |
1965 | int i; | |
1966 | ||
1967 | for (i = 0; i < chunk_nr; i++) { | |
1968 | if (get_user(p, pages32 + i)) | |
1969 | return -EFAULT; | |
1970 | chunk_pages[i] = compat_ptr(p); | |
1971 | } | |
1972 | ||
1973 | return 0; | |
1974 | } | |
1975 | ||
80bba129 BG |
1976 | /* |
1977 | * Determine the nodes of a user array of pages and store it in | |
1978 | * a user array of status. | |
1979 | */ | |
1980 | static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages, | |
1981 | const void __user * __user *pages, | |
1982 | int __user *status) | |
1983 | { | |
3eefb826 | 1984 | #define DO_PAGES_STAT_CHUNK_NR 16UL |
80bba129 BG |
1985 | const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR]; |
1986 | int chunk_status[DO_PAGES_STAT_CHUNK_NR]; | |
80bba129 | 1987 | |
87b8d1ad | 1988 | while (nr_pages) { |
3eefb826 | 1989 | unsigned long chunk_nr = min(nr_pages, DO_PAGES_STAT_CHUNK_NR); |
87b8d1ad | 1990 | |
5b1b561b AB |
1991 | if (in_compat_syscall()) { |
1992 | if (get_compat_pages_array(chunk_pages, pages, | |
1993 | chunk_nr)) | |
1994 | break; | |
1995 | } else { | |
1996 | if (copy_from_user(chunk_pages, pages, | |
1997 | chunk_nr * sizeof(*chunk_pages))) | |
1998 | break; | |
1999 | } | |
80bba129 BG |
2000 | |
2001 | do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status); | |
2002 | ||
87b8d1ad PA |
2003 | if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status))) |
2004 | break; | |
742755a1 | 2005 | |
87b8d1ad PA |
2006 | pages += chunk_nr; |
2007 | status += chunk_nr; | |
2008 | nr_pages -= chunk_nr; | |
2009 | } | |
2010 | return nr_pages ? -EFAULT : 0; | |
742755a1 CL |
2011 | } |
2012 | ||
4dc200ce | 2013 | static struct mm_struct *find_mm_struct(pid_t pid, nodemask_t *mem_nodes) |
742755a1 | 2014 | { |
742755a1 | 2015 | struct task_struct *task; |
742755a1 | 2016 | struct mm_struct *mm; |
742755a1 | 2017 | |
4dc200ce ML |
2018 | /* |
2019 | * There is no need to check if current process has the right to modify | |
2020 | * the specified process when they are same. | |
2021 | */ | |
2022 | if (!pid) { | |
2023 | mmget(current->mm); | |
2024 | *mem_nodes = cpuset_mems_allowed(current); | |
2025 | return current->mm; | |
2026 | } | |
742755a1 CL |
2027 | |
2028 | /* Find the mm_struct */ | |
a879bf58 | 2029 | rcu_read_lock(); |
4dc200ce | 2030 | task = find_task_by_vpid(pid); |
742755a1 | 2031 | if (!task) { |
a879bf58 | 2032 | rcu_read_unlock(); |
4dc200ce | 2033 | return ERR_PTR(-ESRCH); |
742755a1 | 2034 | } |
3268c63e | 2035 | get_task_struct(task); |
742755a1 CL |
2036 | |
2037 | /* | |
2038 | * Check if this process has the right to modify the specified | |
197e7e52 | 2039 | * process. Use the regular "ptrace_may_access()" checks. |
742755a1 | 2040 | */ |
197e7e52 | 2041 | if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) { |
c69e8d9c | 2042 | rcu_read_unlock(); |
4dc200ce | 2043 | mm = ERR_PTR(-EPERM); |
5e9a0f02 | 2044 | goto out; |
742755a1 | 2045 | } |
c69e8d9c | 2046 | rcu_read_unlock(); |
742755a1 | 2047 | |
4dc200ce ML |
2048 | mm = ERR_PTR(security_task_movememory(task)); |
2049 | if (IS_ERR(mm)) | |
5e9a0f02 | 2050 | goto out; |
4dc200ce | 2051 | *mem_nodes = cpuset_mems_allowed(task); |
3268c63e | 2052 | mm = get_task_mm(task); |
4dc200ce | 2053 | out: |
3268c63e | 2054 | put_task_struct(task); |
6e8b09ea | 2055 | if (!mm) |
4dc200ce ML |
2056 | mm = ERR_PTR(-EINVAL); |
2057 | return mm; | |
2058 | } | |
2059 | ||
2060 | /* | |
2061 | * Move a list of pages in the address space of the currently executing | |
2062 | * process. | |
2063 | */ | |
2064 | static int kernel_move_pages(pid_t pid, unsigned long nr_pages, | |
2065 | const void __user * __user *pages, | |
2066 | const int __user *nodes, | |
2067 | int __user *status, int flags) | |
2068 | { | |
2069 | struct mm_struct *mm; | |
2070 | int err; | |
2071 | nodemask_t task_nodes; | |
2072 | ||
2073 | /* Check flags */ | |
2074 | if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL)) | |
6e8b09ea SL |
2075 | return -EINVAL; |
2076 | ||
4dc200ce ML |
2077 | if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE)) |
2078 | return -EPERM; | |
2079 | ||
2080 | mm = find_mm_struct(pid, &task_nodes); | |
2081 | if (IS_ERR(mm)) | |
2082 | return PTR_ERR(mm); | |
2083 | ||
6e8b09ea SL |
2084 | if (nodes) |
2085 | err = do_pages_move(mm, task_nodes, nr_pages, pages, | |
2086 | nodes, status, flags); | |
2087 | else | |
2088 | err = do_pages_stat(mm, nr_pages, pages, status); | |
742755a1 | 2089 | |
742755a1 CL |
2090 | mmput(mm); |
2091 | return err; | |
2092 | } | |
742755a1 | 2093 | |
7addf443 DB |
2094 | SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages, |
2095 | const void __user * __user *, pages, | |
2096 | const int __user *, nodes, | |
2097 | int __user *, status, int, flags) | |
2098 | { | |
2099 | return kernel_move_pages(pid, nr_pages, pages, nodes, status, flags); | |
2100 | } | |
2101 | ||
7039e1db PZ |
2102 | #ifdef CONFIG_NUMA_BALANCING |
2103 | /* | |
2104 | * Returns true if this is a safe migration target node for misplaced NUMA | |
bc53008e | 2105 | * pages. Currently it only checks the watermarks which is crude. |
7039e1db PZ |
2106 | */ |
2107 | static bool migrate_balanced_pgdat(struct pglist_data *pgdat, | |
3abef4e6 | 2108 | unsigned long nr_migrate_pages) |
7039e1db PZ |
2109 | { |
2110 | int z; | |
599d0c95 | 2111 | |
7039e1db PZ |
2112 | for (z = pgdat->nr_zones - 1; z >= 0; z--) { |
2113 | struct zone *zone = pgdat->node_zones + z; | |
2114 | ||
bc53008e | 2115 | if (!managed_zone(zone)) |
7039e1db PZ |
2116 | continue; |
2117 | ||
7039e1db PZ |
2118 | /* Avoid waking kswapd by allocating pages_to_migrate pages. */ |
2119 | if (!zone_watermark_ok(zone, 0, | |
2120 | high_wmark_pages(zone) + | |
2121 | nr_migrate_pages, | |
bfe9d006 | 2122 | ZONE_MOVABLE, 0)) |
7039e1db PZ |
2123 | continue; |
2124 | return true; | |
2125 | } | |
2126 | return false; | |
2127 | } | |
2128 | ||
2129 | static struct page *alloc_misplaced_dst_page(struct page *page, | |
666feb21 | 2130 | unsigned long data) |
7039e1db PZ |
2131 | { |
2132 | int nid = (int) data; | |
c185e494 MWO |
2133 | int order = compound_order(page); |
2134 | gfp_t gfp = __GFP_THISNODE; | |
2135 | struct folio *new; | |
2136 | ||
2137 | if (order > 0) | |
2138 | gfp |= GFP_TRANSHUGE_LIGHT; | |
2139 | else { | |
2140 | gfp |= GFP_HIGHUSER_MOVABLE | __GFP_NOMEMALLOC | __GFP_NORETRY | | |
2141 | __GFP_NOWARN; | |
2142 | gfp &= ~__GFP_RECLAIM; | |
2143 | } | |
2144 | new = __folio_alloc_node(gfp, order, nid); | |
c5b5a3dd | 2145 | |
c185e494 | 2146 | return &new->page; |
c5b5a3dd YS |
2147 | } |
2148 | ||
1c30e017 | 2149 | static int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page) |
b32967ff | 2150 | { |
2b9b624f | 2151 | int nr_pages = thp_nr_pages(page); |
c574bbe9 | 2152 | int order = compound_order(page); |
a8f60772 | 2153 | |
c574bbe9 | 2154 | VM_BUG_ON_PAGE(order && !PageTransHuge(page), page); |
3abef4e6 | 2155 | |
662aeea7 YS |
2156 | /* Do not migrate THP mapped by multiple processes */ |
2157 | if (PageTransHuge(page) && total_mapcount(page) > 1) | |
2158 | return 0; | |
2159 | ||
7039e1db | 2160 | /* Avoid migrating to a node that is nearly full */ |
c574bbe9 HY |
2161 | if (!migrate_balanced_pgdat(pgdat, nr_pages)) { |
2162 | int z; | |
2163 | ||
2164 | if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING)) | |
2165 | return 0; | |
2166 | for (z = pgdat->nr_zones - 1; z >= 0; z--) { | |
bc53008e | 2167 | if (managed_zone(pgdat->node_zones + z)) |
c574bbe9 HY |
2168 | break; |
2169 | } | |
2170 | wakeup_kswapd(pgdat->node_zones + z, 0, order, ZONE_MOVABLE); | |
340ef390 | 2171 | return 0; |
c574bbe9 | 2172 | } |
7039e1db | 2173 | |
340ef390 HD |
2174 | if (isolate_lru_page(page)) |
2175 | return 0; | |
7039e1db | 2176 | |
b75454e1 | 2177 | mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + page_is_file_lru(page), |
2b9b624f | 2178 | nr_pages); |
340ef390 | 2179 | |
149c33e1 | 2180 | /* |
340ef390 HD |
2181 | * Isolating the page has taken another reference, so the |
2182 | * caller's reference can be safely dropped without the page | |
2183 | * disappearing underneath us during migration. | |
149c33e1 MG |
2184 | */ |
2185 | put_page(page); | |
340ef390 | 2186 | return 1; |
b32967ff MG |
2187 | } |
2188 | ||
2189 | /* | |
2190 | * Attempt to migrate a misplaced page to the specified destination | |
2191 | * node. Caller is expected to have an elevated reference count on | |
2192 | * the page that will be dropped by this function before returning. | |
2193 | */ | |
1bc115d8 MG |
2194 | int migrate_misplaced_page(struct page *page, struct vm_area_struct *vma, |
2195 | int node) | |
b32967ff MG |
2196 | { |
2197 | pg_data_t *pgdat = NODE_DATA(node); | |
340ef390 | 2198 | int isolated; |
b32967ff | 2199 | int nr_remaining; |
e39bb6be | 2200 | unsigned int nr_succeeded; |
b32967ff | 2201 | LIST_HEAD(migratepages); |
b5916c02 | 2202 | int nr_pages = thp_nr_pages(page); |
c5b5a3dd | 2203 | |
b32967ff | 2204 | /* |
1bc115d8 MG |
2205 | * Don't migrate file pages that are mapped in multiple processes |
2206 | * with execute permissions as they are probably shared libraries. | |
b32967ff | 2207 | */ |
7ee820ee ML |
2208 | if (page_mapcount(page) != 1 && page_is_file_lru(page) && |
2209 | (vma->vm_flags & VM_EXEC)) | |
b32967ff | 2210 | goto out; |
b32967ff | 2211 | |
09a913a7 MG |
2212 | /* |
2213 | * Also do not migrate dirty pages as not all filesystems can move | |
2214 | * dirty pages in MIGRATE_ASYNC mode which is a waste of cycles. | |
2215 | */ | |
9de4f22a | 2216 | if (page_is_file_lru(page) && PageDirty(page)) |
09a913a7 MG |
2217 | goto out; |
2218 | ||
b32967ff MG |
2219 | isolated = numamigrate_isolate_page(pgdat, page); |
2220 | if (!isolated) | |
2221 | goto out; | |
2222 | ||
2223 | list_add(&page->lru, &migratepages); | |
c185e494 MWO |
2224 | nr_remaining = migrate_pages(&migratepages, alloc_misplaced_dst_page, |
2225 | NULL, node, MIGRATE_ASYNC, | |
2226 | MR_NUMA_MISPLACED, &nr_succeeded); | |
b32967ff | 2227 | if (nr_remaining) { |
59c82b70 JK |
2228 | if (!list_empty(&migratepages)) { |
2229 | list_del(&page->lru); | |
c5fc5c3a YS |
2230 | mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + |
2231 | page_is_file_lru(page), -nr_pages); | |
59c82b70 JK |
2232 | putback_lru_page(page); |
2233 | } | |
b32967ff | 2234 | isolated = 0; |
e39bb6be HY |
2235 | } |
2236 | if (nr_succeeded) { | |
2237 | count_vm_numa_events(NUMA_PAGE_MIGRATE, nr_succeeded); | |
2238 | if (!node_is_toptier(page_to_nid(page)) && node_is_toptier(node)) | |
2239 | mod_node_page_state(pgdat, PGPROMOTE_SUCCESS, | |
2240 | nr_succeeded); | |
2241 | } | |
7039e1db | 2242 | BUG_ON(!list_empty(&migratepages)); |
7039e1db | 2243 | return isolated; |
340ef390 HD |
2244 | |
2245 | out: | |
2246 | put_page(page); | |
2247 | return 0; | |
7039e1db | 2248 | } |
220018d3 | 2249 | #endif /* CONFIG_NUMA_BALANCING */ |
91952440 | 2250 | #endif /* CONFIG_NUMA */ |