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