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