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