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