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