Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 2 | /* |
Hugh Dickins | 14e0f9b | 2015-11-05 18:49:43 -0800 | [diff] [blame] | 3 | * Memory Migration functionality - linux/mm/migrate.c |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 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> |
Christoph Lameter | cde5353 | 2008-07-04 09:59:22 -0700 | [diff] [blame] | 13 | * Christoph Lameter |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <linux/migrate.h> |
Paul Gortmaker | b95f1b31 | 2011-10-16 02:01:52 -0400 | [diff] [blame] | 17 | #include <linux/export.h> |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 18 | #include <linux/swap.h> |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 19 | #include <linux/swapops.h> |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 20 | #include <linux/pagemap.h> |
Christoph Lameter | e23ca00 | 2006-04-10 22:52:57 -0700 | [diff] [blame] | 21 | #include <linux/buffer_head.h> |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 22 | #include <linux/mm_inline.h> |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 23 | #include <linux/nsproxy.h> |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 24 | #include <linux/pagevec.h> |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 25 | #include <linux/ksm.h> |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 26 | #include <linux/rmap.h> |
| 27 | #include <linux/topology.h> |
| 28 | #include <linux/cpu.h> |
| 29 | #include <linux/cpuset.h> |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 30 | #include <linux/writeback.h> |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 31 | #include <linux/mempolicy.h> |
| 32 | #include <linux/vmalloc.h> |
David Quigley | 86c3a76 | 2006-06-23 02:04:02 -0700 | [diff] [blame] | 33 | #include <linux/security.h> |
Hugh Dickins | 42cb14b | 2015-11-05 18:50:05 -0800 | [diff] [blame] | 34 | #include <linux/backing-dev.h> |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 35 | #include <linux/compaction.h> |
Adrian Bunk | 4f5ca26 | 2008-07-23 21:27:02 -0700 | [diff] [blame] | 36 | #include <linux/syscalls.h> |
Dominik Brodowski | 7addf44 | 2018-03-17 16:08:03 +0100 | [diff] [blame] | 37 | #include <linux/compat.h> |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 38 | #include <linux/hugetlb.h> |
Aneesh Kumar K.V | 8e6ac7f | 2012-07-31 16:42:27 -0700 | [diff] [blame] | 39 | #include <linux/hugetlb_cgroup.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 40 | #include <linux/gfp.h> |
Christoph Hellwig | a520110 | 2019-08-28 16:19:53 +0200 | [diff] [blame] | 41 | #include <linux/pagewalk.h> |
Jérôme Glisse | df6ad69 | 2017-09-08 16:12:24 -0700 | [diff] [blame] | 42 | #include <linux/pfn_t.h> |
Jérôme Glisse | a5430dd | 2017-09-08 16:12:17 -0700 | [diff] [blame] | 43 | #include <linux/memremap.h> |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 44 | #include <linux/userfaultfd_k.h> |
Rafael Aquini | bf6bddf1 | 2012-12-11 16:02:42 -0800 | [diff] [blame] | 45 | #include <linux/balloon_compaction.h> |
Mel Gorman | f714f4f | 2013-12-18 17:08:33 -0800 | [diff] [blame] | 46 | #include <linux/mmu_notifier.h> |
Vladimir Davydov | 33c3fc7 | 2015-09-09 15:35:45 -0700 | [diff] [blame] | 47 | #include <linux/page_idle.h> |
Vlastimil Babka | d435edc | 2016-03-15 14:56:15 -0700 | [diff] [blame] | 48 | #include <linux/page_owner.h> |
Ingo Molnar | 6e84f31 | 2017-02-08 18:51:29 +0100 | [diff] [blame] | 49 | #include <linux/sched/mm.h> |
Linus Torvalds | 197e7e5 | 2017-08-20 13:26:27 -0700 | [diff] [blame] | 50 | #include <linux/ptrace.h> |
Ralph Campbell | 34290e2 | 2020-01-30 22:14:44 -0800 | [diff] [blame] | 51 | #include <linux/oom.h> |
Dave Hansen | 884a6e5 | 2021-09-02 14:59:09 -0700 | [diff] [blame] | 52 | #include <linux/memory.h> |
Tao Zeng | 54f845e | 2022-01-26 16:04:08 +0800 | [diff] [blame] | 53 | #ifdef CONFIG_AMLOGIC_CMA |
| 54 | #include <linux/amlogic/aml_cma.h> |
| 55 | #include <linux/delay.h> |
| 56 | #endif |
Jianxiong Pan | 07622f0 | 2022-09-22 17:25:35 +0800 | [diff] [blame] | 57 | #ifdef CONFIG_AMLOGIC_PIN_LOCKED_FILE |
| 58 | #include <linux/amlogic/pin_file.h> |
| 59 | #endif |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 60 | |
Michal Nazarewicz | 0d1836c | 2010-12-21 17:24:26 -0800 | [diff] [blame] | 61 | #include <asm/tlbflush.h> |
| 62 | |
Mel Gorman | 7b2a2d4 | 2012-10-19 14:07:31 +0100 | [diff] [blame] | 63 | #define CREATE_TRACE_POINTS |
| 64 | #include <trace/events/migrate.h> |
| 65 | |
qinglin.li | a1a623f | 2024-06-21 14:36:34 +0800 | [diff] [blame] | 66 | #undef CREATE_TRACE_POINTS |
| 67 | #include <trace/hooks/mm.h> |
| 68 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 69 | #include "internal.h" |
| 70 | |
Yisheng Xie | 9e5bcd6 | 2017-02-24 14:57:29 -0800 | [diff] [blame] | 71 | int isolate_movable_page(struct page *page, isolate_mode_t mode) |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 72 | { |
| 73 | struct address_space *mapping; |
| 74 | |
| 75 | /* |
| 76 | * Avoid burning cycles with pages that are yet under __free_pages(), |
| 77 | * or just got freed under us. |
| 78 | * |
| 79 | * In case we 'win' a race for a movable page being freed under us and |
| 80 | * raise its refcount preventing __free_pages() from doing its job |
| 81 | * the put_page() at the end of this block will take care of |
| 82 | * release this page, thus avoiding a nasty leakage. |
| 83 | */ |
| 84 | if (unlikely(!get_page_unless_zero(page))) |
| 85 | goto out; |
| 86 | |
| 87 | /* |
| 88 | * Check PageMovable before holding a PG_lock because page's owner |
| 89 | * assumes anybody doesn't touch PG_lock of newly allocated page |
Wei Yang | 8bb4e7a | 2019-03-05 15:46:22 -0800 | [diff] [blame] | 90 | * so unconditionally grabbing the lock ruins page's owner side. |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 91 | */ |
| 92 | if (unlikely(!__PageMovable(page))) |
| 93 | goto out_putpage; |
| 94 | /* |
| 95 | * As movable pages are not isolated from LRU lists, concurrent |
| 96 | * compaction threads can race against page migration functions |
| 97 | * as well as race against the releasing a page. |
| 98 | * |
| 99 | * In order to avoid having an already isolated movable page |
| 100 | * being (wrongly) re-isolated while it is under migration, |
| 101 | * or to avoid attempting to isolate pages being released, |
| 102 | * lets be sure we have the page lock |
| 103 | * before proceeding with the movable page isolation steps. |
| 104 | */ |
| 105 | if (unlikely(!trylock_page(page))) |
| 106 | goto out_putpage; |
| 107 | |
| 108 | if (!PageMovable(page) || PageIsolated(page)) |
| 109 | goto out_no_isolated; |
| 110 | |
| 111 | mapping = page_mapping(page); |
| 112 | VM_BUG_ON_PAGE(!mapping, page); |
| 113 | |
| 114 | if (!mapping->a_ops->isolate_page(page, mode)) |
| 115 | goto out_no_isolated; |
| 116 | |
| 117 | /* Driver shouldn't use PG_isolated bit of page->flags */ |
| 118 | WARN_ON_ONCE(PageIsolated(page)); |
andrew.yang | 0d8a836 | 2022-03-15 16:58:34 +1100 | [diff] [blame] | 119 | SetPageIsolated(page); |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 120 | unlock_page(page); |
| 121 | |
Yisheng Xie | 9e5bcd6 | 2017-02-24 14:57:29 -0800 | [diff] [blame] | 122 | return 0; |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 123 | |
| 124 | out_no_isolated: |
| 125 | unlock_page(page); |
| 126 | out_putpage: |
| 127 | put_page(page); |
| 128 | out: |
Yisheng Xie | 9e5bcd6 | 2017-02-24 14:57:29 -0800 | [diff] [blame] | 129 | return -EBUSY; |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Miaohe Lin | 606a6f7 | 2021-05-04 18:37:04 -0700 | [diff] [blame] | 132 | static void putback_movable_page(struct page *page) |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 133 | { |
| 134 | struct address_space *mapping; |
| 135 | |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 136 | mapping = page_mapping(page); |
| 137 | mapping->a_ops->putback_page(page); |
andrew.yang | 0d8a836 | 2022-03-15 16:58:34 +1100 | [diff] [blame] | 138 | ClearPageIsolated(page); |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 141 | /* |
Rafael Aquini | 5733c7d | 2012-12-11 16:02:47 -0800 | [diff] [blame] | 142 | * Put previously isolated pages back onto the appropriate lists |
| 143 | * from where they were once taken off for compaction/migration. |
| 144 | * |
Joonsoo Kim | 59c82b7 | 2014-01-21 15:51:17 -0800 | [diff] [blame] | 145 | * This function shall be used whenever the isolated pageset has been |
| 146 | * built from lru, balloon, hugetlbfs page. See isolate_migratepages_range() |
Miaohe Lin | 072e741 | 2022-05-30 19:30:15 +0800 | [diff] [blame] | 147 | * and isolate_hugetlb(). |
Rafael Aquini | 5733c7d | 2012-12-11 16:02:47 -0800 | [diff] [blame] | 148 | */ |
| 149 | void putback_movable_pages(struct list_head *l) |
| 150 | { |
| 151 | struct page *page; |
| 152 | struct page *page2; |
| 153 | |
| 154 | list_for_each_entry_safe(page, page2, l, lru) { |
Naoya Horiguchi | 31caf66 | 2013-09-11 14:21:59 -0700 | [diff] [blame] | 155 | if (unlikely(PageHuge(page))) { |
| 156 | putback_active_hugepage(page); |
| 157 | continue; |
| 158 | } |
Rafael Aquini | 5733c7d | 2012-12-11 16:02:47 -0800 | [diff] [blame] | 159 | list_del(&page->lru); |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 160 | /* |
| 161 | * We isolated non-lru movable page so here we can use |
| 162 | * __PageMovable because LRU page's mapping cannot have |
| 163 | * PAGE_MAPPING_MOVABLE. |
| 164 | */ |
Minchan Kim | b1123ea6 | 2016-07-26 15:23:09 -0700 | [diff] [blame] | 165 | if (unlikely(__PageMovable(page))) { |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 166 | VM_BUG_ON_PAGE(!PageIsolated(page), page); |
| 167 | lock_page(page); |
| 168 | if (PageMovable(page)) |
| 169 | putback_movable_page(page); |
| 170 | else |
andrew.yang | 0d8a836 | 2022-03-15 16:58:34 +1100 | [diff] [blame] | 171 | ClearPageIsolated(page); |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 172 | unlock_page(page); |
| 173 | put_page(page); |
| 174 | } else { |
Naoya Horiguchi | e8db67e | 2017-09-08 16:11:12 -0700 | [diff] [blame] | 175 | mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + |
Matthew Wilcox (Oracle) | 6c35784 | 2020-08-14 17:30:37 -0700 | [diff] [blame] | 176 | page_is_file_lru(page), -thp_nr_pages(page)); |
Rabin Vincent | fc280fe | 2017-04-20 14:37:46 -0700 | [diff] [blame] | 177 | putback_lru_page(page); |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 178 | } |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 179 | } |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 180 | } |
Charan Teja Reddy | f47b852 | 2021-02-16 13:59:45 +0530 | [diff] [blame] | 181 | EXPORT_SYMBOL_GPL(putback_movable_pages); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 182 | |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 183 | /* |
| 184 | * Restore a potential migration pte to a working pte entry |
| 185 | */ |
Minchan Kim | e4b8222 | 2017-05-03 14:54:27 -0700 | [diff] [blame] | 186 | static bool remove_migration_pte(struct page *page, struct vm_area_struct *vma, |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 187 | unsigned long addr, void *old) |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 188 | { |
Kirill A. Shutemov | 3fe8796 | 2017-02-24 14:58:16 -0800 | [diff] [blame] | 189 | struct page_vma_mapped_walk pvmw = { |
| 190 | .page = old, |
| 191 | .vma = vma, |
| 192 | .address = addr, |
| 193 | .flags = PVMW_SYNC | PVMW_MIGRATION, |
| 194 | }; |
| 195 | struct page *new; |
| 196 | pte_t pte; |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 197 | swp_entry_t entry; |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 198 | |
Kirill A. Shutemov | 3fe8796 | 2017-02-24 14:58:16 -0800 | [diff] [blame] | 199 | VM_BUG_ON_PAGE(PageTail(page), page); |
| 200 | while (page_vma_mapped_walk(&pvmw)) { |
Naoya Horiguchi | 4b0ece6 | 2017-03-31 15:11:44 -0700 | [diff] [blame] | 201 | if (PageKsm(page)) |
| 202 | new = page; |
| 203 | else |
| 204 | new = page - pvmw.page->index + |
| 205 | linear_page_index(vma, pvmw.address); |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 206 | |
Zi Yan | 616b837 | 2017-09-08 16:10:57 -0700 | [diff] [blame] | 207 | #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION |
| 208 | /* PMD-mapped THP migration entry */ |
| 209 | if (!pvmw.pte) { |
| 210 | VM_BUG_ON_PAGE(PageHuge(page) || !PageTransCompound(page), page); |
| 211 | remove_migration_pmd(&pvmw, new); |
| 212 | continue; |
| 213 | } |
| 214 | #endif |
| 215 | |
Kirill A. Shutemov | 3fe8796 | 2017-02-24 14:58:16 -0800 | [diff] [blame] | 216 | get_page(new); |
| 217 | pte = pte_mkold(mk_pte(new, READ_ONCE(vma->vm_page_prot))); |
| 218 | if (pte_swp_soft_dirty(*pvmw.pte)) |
| 219 | pte = pte_mksoft_dirty(pte); |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 220 | |
Hugh Dickins | 486cf46 | 2011-10-19 12:50:35 -0700 | [diff] [blame] | 221 | /* |
Kirill A. Shutemov | 3fe8796 | 2017-02-24 14:58:16 -0800 | [diff] [blame] | 222 | * Recheck VMA as permissions can change since migration started |
Hugh Dickins | 486cf46 | 2011-10-19 12:50:35 -0700 | [diff] [blame] | 223 | */ |
Kirill A. Shutemov | 3fe8796 | 2017-02-24 14:58:16 -0800 | [diff] [blame] | 224 | entry = pte_to_swp_entry(*pvmw.pte); |
Alistair Popple | 4dd845b | 2021-06-30 18:54:09 -0700 | [diff] [blame] | 225 | if (is_writable_migration_entry(entry)) |
Kirill A. Shutemov | 3fe8796 | 2017-02-24 14:58:16 -0800 | [diff] [blame] | 226 | pte = maybe_mkwrite(pte, vma); |
Peter Xu | f45ec5f | 2020-04-06 20:06:01 -0700 | [diff] [blame] | 227 | else if (pte_swp_uffd_wp(*pvmw.pte)) |
| 228 | pte = pte_mkuffd_wp(pte); |
Mel Gorman | d3cb8bf | 2014-10-02 19:47:41 +0100 | [diff] [blame] | 229 | |
Ralph Campbell | 6128763 | 2020-09-04 16:36:04 -0700 | [diff] [blame] | 230 | if (unlikely(is_device_private_page(new))) { |
Alistair Popple | 4dd845b | 2021-06-30 18:54:09 -0700 | [diff] [blame] | 231 | if (pte_write(pte)) |
| 232 | entry = make_writable_device_private_entry( |
| 233 | page_to_pfn(new)); |
| 234 | else |
| 235 | entry = make_readable_device_private_entry( |
| 236 | page_to_pfn(new)); |
Ralph Campbell | 6128763 | 2020-09-04 16:36:04 -0700 | [diff] [blame] | 237 | pte = swp_entry_to_pte(entry); |
Ralph Campbell | 3d321bf8 | 2020-09-04 16:36:07 -0700 | [diff] [blame] | 238 | if (pte_swp_soft_dirty(*pvmw.pte)) |
| 239 | pte = pte_swp_mksoft_dirty(pte); |
Ralph Campbell | 6128763 | 2020-09-04 16:36:04 -0700 | [diff] [blame] | 240 | if (pte_swp_uffd_wp(*pvmw.pte)) |
| 241 | pte = pte_swp_mkuffd_wp(pte); |
Lars Persson | d2b2c6dd | 2019-03-28 20:44:28 -0700 | [diff] [blame] | 242 | } |
Jérôme Glisse | a5430dd | 2017-09-08 16:12:17 -0700 | [diff] [blame] | 243 | |
Andi Kleen | 3ef8fd7 | 2010-10-11 16:03:21 +0200 | [diff] [blame] | 244 | #ifdef CONFIG_HUGETLB_PAGE |
Kirill A. Shutemov | 3fe8796 | 2017-02-24 14:58:16 -0800 | [diff] [blame] | 245 | if (PageHuge(new)) { |
Christophe Leroy | 79c1c59 | 2021-06-30 18:48:00 -0700 | [diff] [blame] | 246 | unsigned int shift = huge_page_shift(hstate_vma(vma)); |
| 247 | |
Kirill A. Shutemov | 3fe8796 | 2017-02-24 14:58:16 -0800 | [diff] [blame] | 248 | pte = pte_mkhuge(pte); |
Christophe Leroy | 79c1c59 | 2021-06-30 18:48:00 -0700 | [diff] [blame] | 249 | pte = arch_make_huge_pte(pte, shift, vma->vm_flags); |
Aneesh Kumar K.V | 383321a | 2017-07-06 15:38:41 -0700 | [diff] [blame] | 250 | set_huge_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte); |
Kirill A. Shutemov | 3fe8796 | 2017-02-24 14:58:16 -0800 | [diff] [blame] | 251 | if (PageAnon(new)) |
| 252 | hugepage_add_anon_rmap(new, vma, pvmw.address); |
| 253 | else |
| 254 | page_dup_rmap(new, true); |
Aneesh Kumar K.V | 383321a | 2017-07-06 15:38:41 -0700 | [diff] [blame] | 255 | } else |
| 256 | #endif |
| 257 | { |
| 258 | set_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte); |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 259 | |
Aneesh Kumar K.V | 383321a | 2017-07-06 15:38:41 -0700 | [diff] [blame] | 260 | if (PageAnon(new)) |
| 261 | page_add_anon_rmap(new, vma, pvmw.address, false); |
| 262 | else |
| 263 | page_add_file_rmap(new, false); |
| 264 | } |
Kirill A. Shutemov | 3fe8796 | 2017-02-24 14:58:16 -0800 | [diff] [blame] | 265 | if (vma->vm_flags & VM_LOCKED && !PageTransCompound(new)) |
| 266 | mlock_vma_page(new); |
Hugh Dickins | 51afb12 | 2015-11-05 18:49:37 -0800 | [diff] [blame] | 267 | |
Kirill A. Shutemov | e125fe4 | 2018-10-05 15:51:41 -0700 | [diff] [blame] | 268 | if (PageTransHuge(page) && PageMlocked(page)) |
| 269 | clear_page_mlock(page); |
| 270 | |
Kirill A. Shutemov | 3fe8796 | 2017-02-24 14:58:16 -0800 | [diff] [blame] | 271 | /* No need to invalidate - it was non-present before */ |
| 272 | update_mmu_cache(vma, pvmw.address, pvmw.pte); |
| 273 | } |
| 274 | |
Minchan Kim | e4b8222 | 2017-05-03 14:54:27 -0700 | [diff] [blame] | 275 | return true; |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /* |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 279 | * Get rid of all migration entries and replace them by |
| 280 | * references to the indicated page. |
| 281 | */ |
Kirill A. Shutemov | e388466 | 2016-03-17 14:20:07 -0700 | [diff] [blame] | 282 | void remove_migration_ptes(struct page *old, struct page *new, bool locked) |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 283 | { |
Joonsoo Kim | 051ac83 | 2014-01-21 15:49:48 -0800 | [diff] [blame] | 284 | struct rmap_walk_control rwc = { |
| 285 | .rmap_one = remove_migration_pte, |
| 286 | .arg = old, |
| 287 | }; |
| 288 | |
Kirill A. Shutemov | e388466 | 2016-03-17 14:20:07 -0700 | [diff] [blame] | 289 | if (locked) |
| 290 | rmap_walk_locked(new, &rwc); |
| 291 | else |
| 292 | rmap_walk(new, &rwc); |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | /* |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 296 | * Something used the pte of a page under migration. We need to |
| 297 | * get to the page and wait until migration is finished. |
| 298 | * When we return from this function the fault will be retried. |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 299 | */ |
Naoya Horiguchi | e66f17f | 2015-02-11 15:25:22 -0800 | [diff] [blame] | 300 | void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep, |
Naoya Horiguchi | 30dad30 | 2013-06-12 14:05:04 -0700 | [diff] [blame] | 301 | spinlock_t *ptl) |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 302 | { |
Naoya Horiguchi | 30dad30 | 2013-06-12 14:05:04 -0700 | [diff] [blame] | 303 | pte_t pte; |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 304 | swp_entry_t entry; |
| 305 | struct page *page; |
Tao Zeng | 54f845e | 2022-01-26 16:04:08 +0800 | [diff] [blame] | 306 | #ifdef CONFIG_AMLOGIC_CMA |
| 307 | bool need_wait = 0; |
| 308 | #endif |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 309 | |
Naoya Horiguchi | 30dad30 | 2013-06-12 14:05:04 -0700 | [diff] [blame] | 310 | spin_lock(ptl); |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 311 | pte = *ptep; |
| 312 | if (!is_swap_pte(pte)) |
| 313 | goto out; |
| 314 | |
| 315 | entry = pte_to_swp_entry(pte); |
| 316 | if (!is_migration_entry(entry)) |
| 317 | goto out; |
| 318 | |
Alistair Popple | af5cdaf | 2021-06-30 18:54:06 -0700 | [diff] [blame] | 319 | page = pfn_swap_entry_to_page(entry); |
Tao Zeng | 54f845e | 2022-01-26 16:04:08 +0800 | [diff] [blame] | 320 | #ifdef CONFIG_AMLOGIC_CMA |
| 321 | /* This page is under cma allocating, do not increase it ref */ |
| 322 | if (in_cma_allocating(page)) { |
| 323 | pr_debug("%s, Page:%lx, flags:%lx, m:%d, c:%d, map:%p\n", |
| 324 | __func__, page_to_pfn(page), page->flags, |
| 325 | page_mapcount(page), page_count(page), |
| 326 | page->mapping); |
| 327 | need_wait = 1; |
| 328 | goto out; |
| 329 | } |
| 330 | #endif |
Xu Yu | ffc90cb | 2021-06-15 18:23:42 -0700 | [diff] [blame] | 331 | page = compound_head(page); |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 332 | |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 333 | /* |
Matthew Wilcox | 89eb946 | 2017-12-04 04:35:16 -0500 | [diff] [blame] | 334 | * Once page cache replacement of page migration started, page_count |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 335 | * is zero; but we must not call put_and_wait_on_page_locked() without |
| 336 | * a ref. Use get_page_unless_zero(), and just fault again if it fails. |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 337 | */ |
| 338 | if (!get_page_unless_zero(page)) |
| 339 | goto out; |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 340 | pte_unmap_unlock(ptep, ptl); |
Matthew Wilcox (Oracle) | 4805462 | 2021-02-24 12:02:02 -0800 | [diff] [blame] | 341 | put_and_wait_on_page_locked(page, TASK_UNINTERRUPTIBLE); |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 342 | return; |
| 343 | out: |
| 344 | pte_unmap_unlock(ptep, ptl); |
Tao Zeng | 54f845e | 2022-01-26 16:04:08 +0800 | [diff] [blame] | 345 | #ifdef CONFIG_AMLOGIC_CMA |
| 346 | if (need_wait) |
| 347 | usleep_range(1000, 1100); |
| 348 | #endif |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Naoya Horiguchi | 30dad30 | 2013-06-12 14:05:04 -0700 | [diff] [blame] | 351 | void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd, |
| 352 | unsigned long address) |
| 353 | { |
| 354 | spinlock_t *ptl = pte_lockptr(mm, pmd); |
| 355 | pte_t *ptep = pte_offset_map(pmd, address); |
| 356 | __migration_entry_wait(mm, ptep, ptl); |
| 357 | } |
| 358 | |
Kirill A. Shutemov | cb900f4 | 2013-11-14 14:31:02 -0800 | [diff] [blame] | 359 | void migration_entry_wait_huge(struct vm_area_struct *vma, |
| 360 | struct mm_struct *mm, pte_t *pte) |
Naoya Horiguchi | 30dad30 | 2013-06-12 14:05:04 -0700 | [diff] [blame] | 361 | { |
Kirill A. Shutemov | cb900f4 | 2013-11-14 14:31:02 -0800 | [diff] [blame] | 362 | spinlock_t *ptl = huge_pte_lockptr(hstate_vma(vma), mm, pte); |
Naoya Horiguchi | 30dad30 | 2013-06-12 14:05:04 -0700 | [diff] [blame] | 363 | __migration_entry_wait(mm, pte, ptl); |
| 364 | } |
| 365 | |
Zi Yan | 616b837 | 2017-09-08 16:10:57 -0700 | [diff] [blame] | 366 | #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION |
| 367 | void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd) |
| 368 | { |
| 369 | spinlock_t *ptl; |
| 370 | struct page *page; |
| 371 | |
| 372 | ptl = pmd_lock(mm, pmd); |
| 373 | if (!is_pmd_migration_entry(*pmd)) |
| 374 | goto unlock; |
Alistair Popple | af5cdaf | 2021-06-30 18:54:06 -0700 | [diff] [blame] | 375 | page = pfn_swap_entry_to_page(pmd_to_swp_entry(*pmd)); |
Zi Yan | 616b837 | 2017-09-08 16:10:57 -0700 | [diff] [blame] | 376 | if (!get_page_unless_zero(page)) |
| 377 | goto unlock; |
| 378 | spin_unlock(ptl); |
Matthew Wilcox (Oracle) | 4805462 | 2021-02-24 12:02:02 -0800 | [diff] [blame] | 379 | put_and_wait_on_page_locked(page, TASK_UNINTERRUPTIBLE); |
Zi Yan | 616b837 | 2017-09-08 16:10:57 -0700 | [diff] [blame] | 380 | return; |
| 381 | unlock: |
| 382 | spin_unlock(ptl); |
| 383 | } |
| 384 | #endif |
| 385 | |
Jan Kara | f900482 | 2019-03-05 15:48:46 -0800 | [diff] [blame] | 386 | static int expected_page_refs(struct address_space *mapping, struct page *page) |
Jan Kara | 0b3901b | 2018-12-28 00:39:01 -0800 | [diff] [blame] | 387 | { |
| 388 | int expected_count = 1; |
| 389 | |
| 390 | /* |
Ralph Campbell | f1f4f3a | 2020-10-13 16:58:42 -0700 | [diff] [blame] | 391 | * Device private pages have an extra refcount as they are |
Jan Kara | 0b3901b | 2018-12-28 00:39:01 -0800 | [diff] [blame] | 392 | * ZONE_DEVICE pages. |
| 393 | */ |
| 394 | expected_count += is_device_private_page(page); |
Jan Kara | f900482 | 2019-03-05 15:48:46 -0800 | [diff] [blame] | 395 | if (mapping) |
Matthew Wilcox (Oracle) | 6c35784 | 2020-08-14 17:30:37 -0700 | [diff] [blame] | 396 | expected_count += thp_nr_pages(page) + page_has_private(page); |
Jan Kara | 0b3901b | 2018-12-28 00:39:01 -0800 | [diff] [blame] | 397 | |
| 398 | return expected_count; |
| 399 | } |
| 400 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 401 | /* |
Christoph Lameter | c3fcf8a | 2006-06-23 02:03:32 -0700 | [diff] [blame] | 402 | * Replace the page in the mapping. |
Christoph Lameter | 5b5c712 | 2006-06-23 02:03:29 -0700 | [diff] [blame] | 403 | * |
| 404 | * The number of remaining references must be: |
| 405 | * 1 for anonymous pages without a mapping |
| 406 | * 2 for pages with a mapping |
David Howells | 266cf65 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 407 | * 3 for pages with a mapping and PagePrivate/PagePrivate2 set. |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 408 | */ |
Gu Zheng | 36bc08c | 2013-07-16 17:56:16 +0800 | [diff] [blame] | 409 | int migrate_page_move_mapping(struct address_space *mapping, |
Keith Busch | 3710969 | 2019-07-18 15:58:46 -0700 | [diff] [blame] | 410 | struct page *newpage, struct page *page, int extra_count) |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 411 | { |
Matthew Wilcox | 89eb946 | 2017-12-04 04:35:16 -0500 | [diff] [blame] | 412 | XA_STATE(xas, &mapping->i_pages, page_index(page)); |
Hugh Dickins | 42cb14b | 2015-11-05 18:50:05 -0800 | [diff] [blame] | 413 | struct zone *oldzone, *newzone; |
| 414 | int dirty; |
Jan Kara | f900482 | 2019-03-05 15:48:46 -0800 | [diff] [blame] | 415 | int expected_count = expected_page_refs(mapping, page) + extra_count; |
Shakeel Butt | 5c447d2 | 2021-01-23 21:01:15 -0800 | [diff] [blame] | 416 | int nr = thp_nr_pages(page); |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 417 | |
Christoph Lameter | 6c5240a | 2006-06-23 02:03:37 -0700 | [diff] [blame] | 418 | if (!mapping) { |
Christoph Lameter | 0e8c7d0 | 2007-04-23 14:41:09 -0700 | [diff] [blame] | 419 | /* Anonymous page without mapping */ |
Tao Zeng | 54f845e | 2022-01-26 16:04:08 +0800 | [diff] [blame] | 420 | #ifdef CONFIG_AMLOGIC_CMA |
| 421 | if (page_count(page) != expected_count) |
| 422 | cma_debug(2, page, " anon page cnt miss match, e:%d\n", |
| 423 | expected_count); |
| 424 | #endif |
Benjamin LaHaise | 8e321fe | 2013-12-21 17:56:08 -0500 | [diff] [blame] | 425 | if (page_count(page) != expected_count) |
Christoph Lameter | 6c5240a | 2006-06-23 02:03:37 -0700 | [diff] [blame] | 426 | return -EAGAIN; |
Hugh Dickins | cf4b769 | 2015-11-05 18:50:02 -0800 | [diff] [blame] | 427 | |
| 428 | /* No turning back from here */ |
Hugh Dickins | cf4b769 | 2015-11-05 18:50:02 -0800 | [diff] [blame] | 429 | newpage->index = page->index; |
| 430 | newpage->mapping = page->mapping; |
| 431 | if (PageSwapBacked(page)) |
Hugh Dickins | fa9949d | 2016-05-19 17:12:41 -0700 | [diff] [blame] | 432 | __SetPageSwapBacked(newpage); |
Hugh Dickins | cf4b769 | 2015-11-05 18:50:02 -0800 | [diff] [blame] | 433 | |
Rafael Aquini | 78bd520 | 2012-12-11 16:02:31 -0800 | [diff] [blame] | 434 | return MIGRATEPAGE_SUCCESS; |
Christoph Lameter | 6c5240a | 2006-06-23 02:03:37 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Hugh Dickins | 42cb14b | 2015-11-05 18:50:05 -0800 | [diff] [blame] | 437 | oldzone = page_zone(page); |
| 438 | newzone = page_zone(newpage); |
| 439 | |
Matthew Wilcox | 89eb946 | 2017-12-04 04:35:16 -0500 | [diff] [blame] | 440 | xas_lock_irq(&xas); |
Matthew Wilcox | 89eb946 | 2017-12-04 04:35:16 -0500 | [diff] [blame] | 441 | if (page_count(page) != expected_count || xas_load(&xas) != page) { |
| 442 | xas_unlock_irq(&xas); |
Tao Zeng | 54f845e | 2022-01-26 16:04:08 +0800 | [diff] [blame] | 443 | #ifdef CONFIG_AMLOGIC_CMA |
| 444 | cma_debug(2, page, " anon page cnt miss match, e:%d, p:%d\n", |
| 445 | expected_count, page_has_private(page)); |
| 446 | #endif |
Christoph Lameter | e23ca00 | 2006-04-10 22:52:57 -0700 | [diff] [blame] | 447 | return -EAGAIN; |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 448 | } |
| 449 | |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 450 | if (!page_ref_freeze(page, expected_count)) { |
Matthew Wilcox | 89eb946 | 2017-12-04 04:35:16 -0500 | [diff] [blame] | 451 | xas_unlock_irq(&xas); |
Tao Zeng | 54f845e | 2022-01-26 16:04:08 +0800 | [diff] [blame] | 452 | #ifdef CONFIG_AMLOGIC_CMA |
| 453 | cma_debug(2, page, " page free fail, e:%d, p:%d\n", |
| 454 | expected_count, page_has_private(page)); |
| 455 | #endif |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 456 | return -EAGAIN; |
| 457 | } |
| 458 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 459 | /* |
Hugh Dickins | cf4b769 | 2015-11-05 18:50:02 -0800 | [diff] [blame] | 460 | * Now we know that no one else is looking at the page: |
| 461 | * no turning back from here. |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 462 | */ |
Hugh Dickins | cf4b769 | 2015-11-05 18:50:02 -0800 | [diff] [blame] | 463 | newpage->index = page->index; |
| 464 | newpage->mapping = page->mapping; |
Shakeel Butt | 5c447d2 | 2021-01-23 21:01:15 -0800 | [diff] [blame] | 465 | page_ref_add(newpage, nr); /* add cache reference */ |
Nicholas Piggin | 6326fec | 2016-12-25 13:00:29 +1000 | [diff] [blame] | 466 | if (PageSwapBacked(page)) { |
| 467 | __SetPageSwapBacked(newpage); |
| 468 | if (PageSwapCache(page)) { |
| 469 | SetPageSwapCache(newpage); |
| 470 | set_page_private(newpage, page_private(page)); |
| 471 | } |
| 472 | } else { |
| 473 | VM_BUG_ON_PAGE(PageSwapCache(page), page); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 474 | } |
| 475 | |
Hugh Dickins | 42cb14b | 2015-11-05 18:50:05 -0800 | [diff] [blame] | 476 | /* Move dirty while page refs frozen and newpage not yet exposed */ |
| 477 | dirty = PageDirty(page); |
| 478 | if (dirty) { |
| 479 | ClearPageDirty(page); |
| 480 | SetPageDirty(newpage); |
| 481 | } |
| 482 | |
Matthew Wilcox | 89eb946 | 2017-12-04 04:35:16 -0500 | [diff] [blame] | 483 | xas_store(&xas, newpage); |
Naoya Horiguchi | e71769a | 2018-04-20 14:55:45 -0700 | [diff] [blame] | 484 | if (PageTransHuge(page)) { |
| 485 | int i; |
Naoya Horiguchi | e71769a | 2018-04-20 14:55:45 -0700 | [diff] [blame] | 486 | |
Shakeel Butt | 5c447d2 | 2021-01-23 21:01:15 -0800 | [diff] [blame] | 487 | for (i = 1; i < nr; i++) { |
Matthew Wilcox | 89eb946 | 2017-12-04 04:35:16 -0500 | [diff] [blame] | 488 | xas_next(&xas); |
Matthew Wilcox (Oracle) | 4101196 | 2019-09-23 15:34:52 -0700 | [diff] [blame] | 489 | xas_store(&xas, newpage); |
Naoya Horiguchi | e71769a | 2018-04-20 14:55:45 -0700 | [diff] [blame] | 490 | } |
Naoya Horiguchi | e71769a | 2018-04-20 14:55:45 -0700 | [diff] [blame] | 491 | } |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 492 | |
| 493 | /* |
Jacobo Giralt | 937a94c | 2012-01-10 15:07:11 -0800 | [diff] [blame] | 494 | * Drop cache reference from old page by unfreezing |
| 495 | * to one less reference. |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 496 | * We know this isn't the last reference. |
| 497 | */ |
Shakeel Butt | 5c447d2 | 2021-01-23 21:01:15 -0800 | [diff] [blame] | 498 | page_ref_unfreeze(page, expected_count - nr); |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 499 | |
Matthew Wilcox | 89eb946 | 2017-12-04 04:35:16 -0500 | [diff] [blame] | 500 | xas_unlock(&xas); |
Hugh Dickins | 42cb14b | 2015-11-05 18:50:05 -0800 | [diff] [blame] | 501 | /* Leave irq disabled to prevent preemption while updating stats */ |
| 502 | |
Christoph Lameter | 0e8c7d0 | 2007-04-23 14:41:09 -0700 | [diff] [blame] | 503 | /* |
| 504 | * If moved to a different zone then also account |
| 505 | * the page for that zone. Other VM counters will be |
| 506 | * taken care of when we establish references to the |
| 507 | * new page and drop references to the old page. |
| 508 | * |
| 509 | * Note that anonymous pages are accounted for |
Mel Gorman | 4b9d0fa | 2016-07-28 15:46:17 -0700 | [diff] [blame] | 510 | * via NR_FILE_PAGES and NR_ANON_MAPPED if they |
Christoph Lameter | 0e8c7d0 | 2007-04-23 14:41:09 -0700 | [diff] [blame] | 511 | * are mapped to swap space. |
| 512 | */ |
Hugh Dickins | 42cb14b | 2015-11-05 18:50:05 -0800 | [diff] [blame] | 513 | if (newzone != oldzone) { |
Johannes Weiner | 0d1c207 | 2020-06-03 16:01:54 -0700 | [diff] [blame] | 514 | struct lruvec *old_lruvec, *new_lruvec; |
| 515 | struct mem_cgroup *memcg; |
| 516 | |
| 517 | memcg = page_memcg(page); |
| 518 | old_lruvec = mem_cgroup_lruvec(memcg, oldzone->zone_pgdat); |
| 519 | new_lruvec = mem_cgroup_lruvec(memcg, newzone->zone_pgdat); |
| 520 | |
Shakeel Butt | 5c447d2 | 2021-01-23 21:01:15 -0800 | [diff] [blame] | 521 | __mod_lruvec_state(old_lruvec, NR_FILE_PAGES, -nr); |
| 522 | __mod_lruvec_state(new_lruvec, NR_FILE_PAGES, nr); |
Hugh Dickins | 42cb14b | 2015-11-05 18:50:05 -0800 | [diff] [blame] | 523 | if (PageSwapBacked(page) && !PageSwapCache(page)) { |
Shakeel Butt | 5c447d2 | 2021-01-23 21:01:15 -0800 | [diff] [blame] | 524 | __mod_lruvec_state(old_lruvec, NR_SHMEM, -nr); |
| 525 | __mod_lruvec_state(new_lruvec, NR_SHMEM, nr); |
Hugh Dickins | 42cb14b | 2015-11-05 18:50:05 -0800 | [diff] [blame] | 526 | } |
Shakeel Butt | b603894 | 2021-02-24 12:03:55 -0800 | [diff] [blame] | 527 | #ifdef CONFIG_SWAP |
| 528 | if (PageSwapCache(page)) { |
| 529 | __mod_lruvec_state(old_lruvec, NR_SWAPCACHE, -nr); |
| 530 | __mod_lruvec_state(new_lruvec, NR_SWAPCACHE, nr); |
| 531 | } |
| 532 | #endif |
Christoph Hellwig | f56753a | 2020-09-24 08:51:40 +0200 | [diff] [blame] | 533 | if (dirty && mapping_can_writeback(mapping)) { |
Shakeel Butt | 5c447d2 | 2021-01-23 21:01:15 -0800 | [diff] [blame] | 534 | __mod_lruvec_state(old_lruvec, NR_FILE_DIRTY, -nr); |
| 535 | __mod_zone_page_state(oldzone, NR_ZONE_WRITE_PENDING, -nr); |
| 536 | __mod_lruvec_state(new_lruvec, NR_FILE_DIRTY, nr); |
| 537 | __mod_zone_page_state(newzone, NR_ZONE_WRITE_PENDING, nr); |
Hugh Dickins | 42cb14b | 2015-11-05 18:50:05 -0800 | [diff] [blame] | 538 | } |
KOSAKI Motohiro | 4b02108 | 2009-09-21 17:01:33 -0700 | [diff] [blame] | 539 | } |
Hugh Dickins | 42cb14b | 2015-11-05 18:50:05 -0800 | [diff] [blame] | 540 | local_irq_enable(); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 541 | |
Rafael Aquini | 78bd520 | 2012-12-11 16:02:31 -0800 | [diff] [blame] | 542 | return MIGRATEPAGE_SUCCESS; |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 543 | } |
Richard Weinberger | 1118dce | 2016-06-16 23:26:14 +0200 | [diff] [blame] | 544 | EXPORT_SYMBOL(migrate_page_move_mapping); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 545 | |
| 546 | /* |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 547 | * The expected number of remaining references is the same as that |
| 548 | * of migrate_page_move_mapping(). |
| 549 | */ |
| 550 | int migrate_huge_page_move_mapping(struct address_space *mapping, |
| 551 | struct page *newpage, struct page *page) |
| 552 | { |
Matthew Wilcox | 89eb946 | 2017-12-04 04:35:16 -0500 | [diff] [blame] | 553 | XA_STATE(xas, &mapping->i_pages, page_index(page)); |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 554 | int expected_count; |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 555 | |
Matthew Wilcox | 89eb946 | 2017-12-04 04:35:16 -0500 | [diff] [blame] | 556 | xas_lock_irq(&xas); |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 557 | expected_count = 2 + page_has_private(page); |
Matthew Wilcox | 89eb946 | 2017-12-04 04:35:16 -0500 | [diff] [blame] | 558 | if (page_count(page) != expected_count || xas_load(&xas) != page) { |
| 559 | xas_unlock_irq(&xas); |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 560 | return -EAGAIN; |
| 561 | } |
| 562 | |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 563 | if (!page_ref_freeze(page, expected_count)) { |
Matthew Wilcox | 89eb946 | 2017-12-04 04:35:16 -0500 | [diff] [blame] | 564 | xas_unlock_irq(&xas); |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 565 | return -EAGAIN; |
| 566 | } |
| 567 | |
Hugh Dickins | cf4b769 | 2015-11-05 18:50:02 -0800 | [diff] [blame] | 568 | newpage->index = page->index; |
| 569 | newpage->mapping = page->mapping; |
Johannes Weiner | 6a93ca8 | 2016-03-15 14:57:19 -0700 | [diff] [blame] | 570 | |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 571 | get_page(newpage); |
| 572 | |
Matthew Wilcox | 89eb946 | 2017-12-04 04:35:16 -0500 | [diff] [blame] | 573 | xas_store(&xas, newpage); |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 574 | |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 575 | page_ref_unfreeze(page, expected_count - 1); |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 576 | |
Matthew Wilcox | 89eb946 | 2017-12-04 04:35:16 -0500 | [diff] [blame] | 577 | xas_unlock_irq(&xas); |
Johannes Weiner | 6a93ca8 | 2016-03-15 14:57:19 -0700 | [diff] [blame] | 578 | |
Rafael Aquini | 78bd520 | 2012-12-11 16:02:31 -0800 | [diff] [blame] | 579 | return MIGRATEPAGE_SUCCESS; |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | /* |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 583 | * Copy the page to its new location |
| 584 | */ |
Jérôme Glisse | 2916ecc | 2017-09-08 16:12:06 -0700 | [diff] [blame] | 585 | void migrate_page_states(struct page *newpage, struct page *page) |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 586 | { |
Rik van Riel | 7851a45 | 2013-10-07 11:29:23 +0100 | [diff] [blame] | 587 | int cpupid; |
| 588 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 589 | if (PageError(page)) |
| 590 | SetPageError(newpage); |
| 591 | if (PageReferenced(page)) |
| 592 | SetPageReferenced(newpage); |
| 593 | if (PageUptodate(page)) |
| 594 | SetPageUptodate(newpage); |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 595 | if (TestClearPageActive(page)) { |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 596 | VM_BUG_ON_PAGE(PageUnevictable(page), page); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 597 | SetPageActive(newpage); |
Lee Schermerhorn | 418b27e | 2009-12-14 17:59:54 -0800 | [diff] [blame] | 598 | } else if (TestClearPageUnevictable(page)) |
| 599 | SetPageUnevictable(newpage); |
Johannes Weiner | 1899ad1 | 2018-10-26 15:06:04 -0700 | [diff] [blame] | 600 | if (PageWorkingset(page)) |
| 601 | SetPageWorkingset(newpage); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 602 | if (PageChecked(page)) |
| 603 | SetPageChecked(newpage); |
| 604 | if (PageMappedToDisk(page)) |
| 605 | SetPageMappedToDisk(newpage); |
| 606 | |
qinglin.li | a1a623f | 2024-06-21 14:36:34 +0800 | [diff] [blame] | 607 | trace_android_vh_look_around_migrate_page(page, newpage); |
| 608 | |
Hugh Dickins | 42cb14b | 2015-11-05 18:50:05 -0800 | [diff] [blame] | 609 | /* Move dirty on pages not done by migrate_page_move_mapping() */ |
| 610 | if (PageDirty(page)) |
| 611 | SetPageDirty(newpage); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 612 | |
Vladimir Davydov | 33c3fc7 | 2015-09-09 15:35:45 -0700 | [diff] [blame] | 613 | if (page_is_young(page)) |
| 614 | set_page_young(newpage); |
| 615 | if (page_is_idle(page)) |
| 616 | set_page_idle(newpage); |
| 617 | |
Rik van Riel | 7851a45 | 2013-10-07 11:29:23 +0100 | [diff] [blame] | 618 | /* |
| 619 | * Copy NUMA information to the new page, to prevent over-eager |
| 620 | * future migrations of this same page. |
| 621 | */ |
| 622 | cpupid = page_cpupid_xchg_last(page, -1); |
| 623 | page_cpupid_xchg_last(newpage, cpupid); |
| 624 | |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 625 | ksm_migrate_page(newpage, page); |
Hugh Dickins | c8d6553 | 2013-02-22 16:35:10 -0800 | [diff] [blame] | 626 | /* |
| 627 | * Please do not reorder this without considering how mm/ksm.c's |
| 628 | * get_ksm_page() depends upon ksm_migrate_page() and PageSwapCache(). |
| 629 | */ |
Naoya Horiguchi | b3b3a99 | 2015-04-15 16:13:15 -0700 | [diff] [blame] | 630 | if (PageSwapCache(page)) |
| 631 | ClearPageSwapCache(page); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 632 | ClearPagePrivate(page); |
Muchun Song | ad2fa37 | 2021-06-30 18:47:21 -0700 | [diff] [blame] | 633 | |
| 634 | /* page->private contains hugetlb specific flags */ |
| 635 | if (!PageHuge(page)) |
| 636 | set_page_private(page, 0); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 637 | |
| 638 | /* |
| 639 | * If any waiters have accumulated on the new page then |
| 640 | * wake them up. |
| 641 | */ |
| 642 | if (PageWriteback(newpage)) |
| 643 | end_page_writeback(newpage); |
Vlastimil Babka | d435edc | 2016-03-15 14:56:15 -0700 | [diff] [blame] | 644 | |
Yang Shi | 6aeff24 | 2020-04-06 20:04:21 -0700 | [diff] [blame] | 645 | /* |
| 646 | * PG_readahead shares the same bit with PG_reclaim. The above |
| 647 | * end_page_writeback() may clear PG_readahead mistakenly, so set the |
| 648 | * bit after that. |
| 649 | */ |
| 650 | if (PageReadahead(page)) |
| 651 | SetPageReadahead(newpage); |
| 652 | |
Vlastimil Babka | d435edc | 2016-03-15 14:56:15 -0700 | [diff] [blame] | 653 | copy_page_owner(page, newpage); |
Johannes Weiner | 74485cf | 2016-03-15 14:57:54 -0700 | [diff] [blame] | 654 | |
Hugh Dickins | a333e3e | 2020-09-18 21:20:06 -0700 | [diff] [blame] | 655 | if (!PageHuge(page)) |
| 656 | mem_cgroup_migrate(page, newpage); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 657 | } |
Jérôme Glisse | 2916ecc | 2017-09-08 16:12:06 -0700 | [diff] [blame] | 658 | EXPORT_SYMBOL(migrate_page_states); |
| 659 | |
| 660 | void migrate_page_copy(struct page *newpage, struct page *page) |
| 661 | { |
| 662 | if (PageHuge(page) || PageTransHuge(page)) |
| 663 | copy_huge_page(newpage, page); |
| 664 | else |
| 665 | copy_highpage(newpage, page); |
| 666 | |
| 667 | migrate_page_states(newpage, page); |
| 668 | } |
Richard Weinberger | 1118dce | 2016-06-16 23:26:14 +0200 | [diff] [blame] | 669 | EXPORT_SYMBOL(migrate_page_copy); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 670 | |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 671 | /************************************************************ |
| 672 | * Migration functions |
| 673 | ***********************************************************/ |
| 674 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 675 | /* |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 676 | * Common logic to directly migrate a single LRU page suitable for |
David Howells | 266cf65 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 677 | * pages that do not use PagePrivate/PagePrivate2. |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 678 | * |
| 679 | * Pages are locked upon entry and exit. |
| 680 | */ |
Christoph Lameter | 2d1db3b | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 681 | int migrate_page(struct address_space *mapping, |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 682 | struct page *newpage, struct page *page, |
| 683 | enum migrate_mode mode) |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 684 | { |
| 685 | int rc; |
| 686 | |
| 687 | BUG_ON(PageWriteback(page)); /* Writeback must be complete */ |
| 688 | |
Keith Busch | 3710969 | 2019-07-18 15:58:46 -0700 | [diff] [blame] | 689 | rc = migrate_page_move_mapping(mapping, newpage, page, 0); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 690 | |
Rafael Aquini | 78bd520 | 2012-12-11 16:02:31 -0800 | [diff] [blame] | 691 | if (rc != MIGRATEPAGE_SUCCESS) |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 692 | return rc; |
| 693 | |
Jérôme Glisse | 2916ecc | 2017-09-08 16:12:06 -0700 | [diff] [blame] | 694 | if (mode != MIGRATE_SYNC_NO_COPY) |
| 695 | migrate_page_copy(newpage, page); |
| 696 | else |
| 697 | migrate_page_states(newpage, page); |
Rafael Aquini | 78bd520 | 2012-12-11 16:02:31 -0800 | [diff] [blame] | 698 | return MIGRATEPAGE_SUCCESS; |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 699 | } |
| 700 | EXPORT_SYMBOL(migrate_page); |
| 701 | |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 702 | #ifdef CONFIG_BLOCK |
Jan Kara | 84ade7c | 2018-12-28 00:39:09 -0800 | [diff] [blame] | 703 | /* Returns true if all buffers are successfully locked */ |
| 704 | static bool buffer_migrate_lock_buffers(struct buffer_head *head, |
| 705 | enum migrate_mode mode) |
| 706 | { |
| 707 | struct buffer_head *bh = head; |
| 708 | |
| 709 | /* Simple case, sync compaction */ |
| 710 | if (mode != MIGRATE_ASYNC) { |
| 711 | do { |
Jan Kara | 84ade7c | 2018-12-28 00:39:09 -0800 | [diff] [blame] | 712 | lock_buffer(bh); |
| 713 | bh = bh->b_this_page; |
| 714 | |
| 715 | } while (bh != head); |
| 716 | |
| 717 | return true; |
| 718 | } |
| 719 | |
| 720 | /* async case, we cannot block on lock_buffer so use trylock_buffer */ |
| 721 | do { |
Jan Kara | 84ade7c | 2018-12-28 00:39:09 -0800 | [diff] [blame] | 722 | if (!trylock_buffer(bh)) { |
| 723 | /* |
| 724 | * We failed to lock the buffer and cannot stall in |
| 725 | * async migration. Release the taken locks |
| 726 | */ |
| 727 | struct buffer_head *failed_bh = bh; |
Jan Kara | 84ade7c | 2018-12-28 00:39:09 -0800 | [diff] [blame] | 728 | bh = head; |
| 729 | while (bh != failed_bh) { |
| 730 | unlock_buffer(bh); |
Jan Kara | 84ade7c | 2018-12-28 00:39:09 -0800 | [diff] [blame] | 731 | bh = bh->b_this_page; |
| 732 | } |
| 733 | return false; |
| 734 | } |
| 735 | |
| 736 | bh = bh->b_this_page; |
| 737 | } while (bh != head); |
| 738 | return true; |
| 739 | } |
| 740 | |
Jan Kara | 89cb088 | 2018-12-28 00:39:12 -0800 | [diff] [blame] | 741 | static int __buffer_migrate_page(struct address_space *mapping, |
| 742 | struct page *newpage, struct page *page, enum migrate_mode mode, |
| 743 | bool check_refs) |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 744 | { |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 745 | struct buffer_head *bh, *head; |
| 746 | int rc; |
Jan Kara | cc4f11e | 2018-12-28 00:39:05 -0800 | [diff] [blame] | 747 | int expected_count; |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 748 | |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 749 | if (!page_has_buffers(page)) |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 750 | return migrate_page(mapping, newpage, page, mode); |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 751 | |
Jan Kara | cc4f11e | 2018-12-28 00:39:05 -0800 | [diff] [blame] | 752 | /* Check whether page does not have extra refs before we do more work */ |
Jan Kara | f900482 | 2019-03-05 15:48:46 -0800 | [diff] [blame] | 753 | expected_count = expected_page_refs(mapping, page); |
Jan Kara | cc4f11e | 2018-12-28 00:39:05 -0800 | [diff] [blame] | 754 | if (page_count(page) != expected_count) |
| 755 | return -EAGAIN; |
| 756 | |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 757 | head = page_buffers(page); |
Jan Kara | cc4f11e | 2018-12-28 00:39:05 -0800 | [diff] [blame] | 758 | if (!buffer_migrate_lock_buffers(head, mode)) |
| 759 | return -EAGAIN; |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 760 | |
Jan Kara | 89cb088 | 2018-12-28 00:39:12 -0800 | [diff] [blame] | 761 | if (check_refs) { |
| 762 | bool busy; |
| 763 | bool invalidated = false; |
| 764 | |
| 765 | recheck_buffers: |
| 766 | busy = false; |
| 767 | spin_lock(&mapping->private_lock); |
| 768 | bh = head; |
| 769 | do { |
| 770 | if (atomic_read(&bh->b_count)) { |
| 771 | busy = true; |
| 772 | break; |
| 773 | } |
| 774 | bh = bh->b_this_page; |
| 775 | } while (bh != head); |
Jan Kara | 89cb088 | 2018-12-28 00:39:12 -0800 | [diff] [blame] | 776 | if (busy) { |
| 777 | if (invalidated) { |
| 778 | rc = -EAGAIN; |
| 779 | goto unlock_buffers; |
| 780 | } |
Jan Kara | ebdf4de | 2019-08-02 21:48:47 -0700 | [diff] [blame] | 781 | spin_unlock(&mapping->private_lock); |
Jan Kara | 89cb088 | 2018-12-28 00:39:12 -0800 | [diff] [blame] | 782 | invalidate_bh_lrus(); |
| 783 | invalidated = true; |
| 784 | goto recheck_buffers; |
| 785 | } |
| 786 | } |
| 787 | |
Keith Busch | 3710969 | 2019-07-18 15:58:46 -0700 | [diff] [blame] | 788 | rc = migrate_page_move_mapping(mapping, newpage, page, 0); |
Rafael Aquini | 78bd520 | 2012-12-11 16:02:31 -0800 | [diff] [blame] | 789 | if (rc != MIGRATEPAGE_SUCCESS) |
Jan Kara | cc4f11e | 2018-12-28 00:39:05 -0800 | [diff] [blame] | 790 | goto unlock_buffers; |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 791 | |
Guoqing Jiang | cd0f371 | 2020-06-01 21:48:06 -0700 | [diff] [blame] | 792 | attach_page_private(newpage, detach_page_private(page)); |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 793 | |
| 794 | bh = head; |
| 795 | do { |
| 796 | set_bh_page(bh, newpage, bh_offset(bh)); |
| 797 | bh = bh->b_this_page; |
| 798 | |
| 799 | } while (bh != head); |
| 800 | |
Jérôme Glisse | 2916ecc | 2017-09-08 16:12:06 -0700 | [diff] [blame] | 801 | if (mode != MIGRATE_SYNC_NO_COPY) |
| 802 | migrate_page_copy(newpage, page); |
| 803 | else |
| 804 | migrate_page_states(newpage, page); |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 805 | |
Jan Kara | cc4f11e | 2018-12-28 00:39:05 -0800 | [diff] [blame] | 806 | rc = MIGRATEPAGE_SUCCESS; |
| 807 | unlock_buffers: |
Jan Kara | ebdf4de | 2019-08-02 21:48:47 -0700 | [diff] [blame] | 808 | if (check_refs) |
| 809 | spin_unlock(&mapping->private_lock); |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 810 | bh = head; |
| 811 | do { |
| 812 | unlock_buffer(bh); |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 813 | bh = bh->b_this_page; |
| 814 | |
| 815 | } while (bh != head); |
| 816 | |
Jan Kara | cc4f11e | 2018-12-28 00:39:05 -0800 | [diff] [blame] | 817 | return rc; |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 818 | } |
Jan Kara | 89cb088 | 2018-12-28 00:39:12 -0800 | [diff] [blame] | 819 | |
| 820 | /* |
| 821 | * Migration function for pages with buffers. This function can only be used |
| 822 | * if the underlying filesystem guarantees that no other references to "page" |
| 823 | * exist. For example attached buffer heads are accessed only under page lock. |
| 824 | */ |
| 825 | int buffer_migrate_page(struct address_space *mapping, |
| 826 | struct page *newpage, struct page *page, enum migrate_mode mode) |
| 827 | { |
| 828 | return __buffer_migrate_page(mapping, newpage, page, mode, false); |
| 829 | } |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 830 | EXPORT_SYMBOL(buffer_migrate_page); |
Jan Kara | 89cb088 | 2018-12-28 00:39:12 -0800 | [diff] [blame] | 831 | |
| 832 | /* |
| 833 | * Same as above except that this variant is more careful and checks that there |
| 834 | * are also no buffer head references. This function is the right one for |
| 835 | * mappings where buffer heads are directly looked up and referenced (such as |
| 836 | * block device mappings). |
| 837 | */ |
| 838 | int buffer_migrate_page_norefs(struct address_space *mapping, |
| 839 | struct page *newpage, struct page *page, enum migrate_mode mode) |
| 840 | { |
| 841 | return __buffer_migrate_page(mapping, newpage, page, mode, true); |
| 842 | } |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 843 | #endif |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 844 | |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 845 | /* |
| 846 | * Writeback a page to clean the dirty state |
| 847 | */ |
| 848 | static int writeout(struct address_space *mapping, struct page *page) |
| 849 | { |
| 850 | struct writeback_control wbc = { |
| 851 | .sync_mode = WB_SYNC_NONE, |
| 852 | .nr_to_write = 1, |
| 853 | .range_start = 0, |
| 854 | .range_end = LLONG_MAX, |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 855 | .for_reclaim = 1 |
| 856 | }; |
| 857 | int rc; |
| 858 | |
| 859 | if (!mapping->a_ops->writepage) |
| 860 | /* No write method for the address space */ |
| 861 | return -EINVAL; |
| 862 | |
| 863 | if (!clear_page_dirty_for_io(page)) |
| 864 | /* Someone else already triggered a write */ |
| 865 | return -EAGAIN; |
| 866 | |
| 867 | /* |
| 868 | * A dirty page may imply that the underlying filesystem has |
| 869 | * the page on some queue. So the page must be clean for |
| 870 | * migration. Writeout may mean we loose the lock and the |
| 871 | * page state is no longer what we checked for earlier. |
| 872 | * At this point we know that the migration attempt cannot |
| 873 | * be successful. |
| 874 | */ |
Kirill A. Shutemov | e388466 | 2016-03-17 14:20:07 -0700 | [diff] [blame] | 875 | remove_migration_ptes(page, page, false); |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 876 | |
| 877 | rc = mapping->a_ops->writepage(page, &wbc); |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 878 | |
| 879 | if (rc != AOP_WRITEPAGE_ACTIVATE) |
| 880 | /* unlocked. Relock */ |
| 881 | lock_page(page); |
| 882 | |
Hugh Dickins | bda8550 | 2008-11-19 15:36:36 -0800 | [diff] [blame] | 883 | return (rc < 0) ? -EIO : -EAGAIN; |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | /* |
| 887 | * Default handling if a filesystem does not provide a migration function. |
| 888 | */ |
Christoph Lameter | 8351a6e | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 889 | static int fallback_migrate_page(struct address_space *mapping, |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 890 | struct page *newpage, struct page *page, enum migrate_mode mode) |
Christoph Lameter | 8351a6e | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 891 | { |
Mel Gorman | b969c4ab | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 892 | if (PageDirty(page)) { |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 893 | /* Only writeback pages in full synchronous migration */ |
Jérôme Glisse | 2916ecc | 2017-09-08 16:12:06 -0700 | [diff] [blame] | 894 | switch (mode) { |
| 895 | case MIGRATE_SYNC: |
| 896 | case MIGRATE_SYNC_NO_COPY: |
| 897 | break; |
| 898 | default: |
Mel Gorman | b969c4ab | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 899 | return -EBUSY; |
Jérôme Glisse | 2916ecc | 2017-09-08 16:12:06 -0700 | [diff] [blame] | 900 | } |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 901 | return writeout(mapping, page); |
Mel Gorman | b969c4ab | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 902 | } |
Christoph Lameter | 8351a6e | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 903 | |
| 904 | /* |
| 905 | * Buffers may be managed in a filesystem specific way. |
| 906 | * We must have no buffers or drop them. |
| 907 | */ |
David Howells | 266cf65 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 908 | if (page_has_private(page) && |
Christoph Lameter | 8351a6e | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 909 | !try_to_release_page(page, GFP_KERNEL)) |
Mel Gorman | 806031b | 2019-03-05 15:44:43 -0800 | [diff] [blame] | 910 | return mode == MIGRATE_SYNC ? -EAGAIN : -EBUSY; |
Christoph Lameter | 8351a6e | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 911 | |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 912 | return migrate_page(mapping, newpage, page, mode); |
Christoph Lameter | 8351a6e | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 913 | } |
| 914 | |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 915 | /* |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 916 | * Move a page to a newly allocated page |
| 917 | * The page is locked and all ptes have been successfully removed. |
| 918 | * |
| 919 | * The new page will have replaced the old page if this function |
| 920 | * is successful. |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 921 | * |
| 922 | * Return value: |
| 923 | * < 0 - error code |
Rafael Aquini | 78bd520 | 2012-12-11 16:02:31 -0800 | [diff] [blame] | 924 | * MIGRATEPAGE_SUCCESS - success |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 925 | */ |
Mel Gorman | 3fe2011 | 2010-05-24 14:32:20 -0700 | [diff] [blame] | 926 | static int move_to_new_page(struct page *newpage, struct page *page, |
Hugh Dickins | 5c3f9a6 | 2015-11-05 18:49:53 -0800 | [diff] [blame] | 927 | enum migrate_mode mode) |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 928 | { |
| 929 | struct address_space *mapping; |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 930 | int rc = -EAGAIN; |
| 931 | bool is_lru = !__PageMovable(page); |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 932 | |
Hugh Dickins | 7db7671 | 2015-11-05 18:49:49 -0800 | [diff] [blame] | 933 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
| 934 | VM_BUG_ON_PAGE(!PageLocked(newpage), newpage); |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 935 | |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 936 | mapping = page_mapping(page); |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 937 | |
| 938 | if (likely(is_lru)) { |
| 939 | if (!mapping) |
| 940 | rc = migrate_page(mapping, newpage, page, mode); |
| 941 | else if (mapping->a_ops->migratepage) |
| 942 | /* |
| 943 | * Most pages have a mapping and most filesystems |
| 944 | * provide a migratepage callback. Anonymous pages |
| 945 | * are part of swap space which also has its own |
| 946 | * migratepage callback. This is the most common path |
| 947 | * for page migration. |
| 948 | */ |
| 949 | rc = mapping->a_ops->migratepage(mapping, newpage, |
| 950 | page, mode); |
| 951 | else |
| 952 | rc = fallback_migrate_page(mapping, newpage, |
| 953 | page, mode); |
| 954 | } else { |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 955 | /* |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 956 | * In case of non-lru page, it could be released after |
| 957 | * isolation step. In that case, we shouldn't try migration. |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 958 | */ |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 959 | VM_BUG_ON_PAGE(!PageIsolated(page), page); |
| 960 | if (!PageMovable(page)) { |
| 961 | rc = MIGRATEPAGE_SUCCESS; |
andrew.yang | 0d8a836 | 2022-03-15 16:58:34 +1100 | [diff] [blame] | 962 | ClearPageIsolated(page); |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 963 | goto out; |
| 964 | } |
| 965 | |
| 966 | rc = mapping->a_ops->migratepage(mapping, newpage, |
| 967 | page, mode); |
| 968 | WARN_ON_ONCE(rc == MIGRATEPAGE_SUCCESS && |
| 969 | !PageIsolated(page)); |
| 970 | } |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 971 | |
Hugh Dickins | 5c3f9a6 | 2015-11-05 18:49:53 -0800 | [diff] [blame] | 972 | /* |
| 973 | * When successful, old pagecache page->mapping must be cleared before |
| 974 | * page is freed; but stats require that PageAnon be left as PageAnon. |
| 975 | */ |
| 976 | if (rc == MIGRATEPAGE_SUCCESS) { |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 977 | if (__PageMovable(page)) { |
| 978 | VM_BUG_ON_PAGE(!PageIsolated(page), page); |
| 979 | |
| 980 | /* |
| 981 | * We clear PG_movable under page_lock so any compactor |
| 982 | * cannot try to migrate this page. |
| 983 | */ |
andrew.yang | 0d8a836 | 2022-03-15 16:58:34 +1100 | [diff] [blame] | 984 | ClearPageIsolated(page); |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | /* |
Ralph Campbell | c23a0c9 | 2020-01-30 22:14:41 -0800 | [diff] [blame] | 988 | * Anonymous and movable page->mapping will be cleared by |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 989 | * free_pages_prepare so don't reset it here for keeping |
| 990 | * the type to work PageAnon, for example. |
| 991 | */ |
| 992 | if (!PageMappingFlags(page)) |
Hugh Dickins | 5c3f9a6 | 2015-11-05 18:49:53 -0800 | [diff] [blame] | 993 | page->mapping = NULL; |
Lars Persson | d2b2c6dd | 2019-03-28 20:44:28 -0700 | [diff] [blame] | 994 | |
Muchun Song | 97a9f80 | 2022-03-22 14:41:56 -0700 | [diff] [blame] | 995 | if (likely(!is_zone_device_page(newpage))) { |
| 996 | int i, nr = compound_nr(newpage); |
Lars Persson | d2b2c6dd | 2019-03-28 20:44:28 -0700 | [diff] [blame] | 997 | |
Muchun Song | 97a9f80 | 2022-03-22 14:41:56 -0700 | [diff] [blame] | 998 | for (i = 0; i < nr; i++) |
| 999 | flush_dcache_page(newpage + i); |
| 1000 | } |
Mel Gorman | 3fe2011 | 2010-05-24 14:32:20 -0700 | [diff] [blame] | 1001 | } |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 1002 | out: |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1003 | return rc; |
| 1004 | } |
| 1005 | |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1006 | static int __unmap_and_move(struct page *page, struct page *newpage, |
Hugh Dickins | 9c620e2 | 2013-02-22 16:35:14 -0800 | [diff] [blame] | 1007 | int force, enum migrate_mode mode) |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1008 | { |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1009 | int rc = -EAGAIN; |
Baolin Wang | 213ecb3 | 2021-09-08 15:18:06 -0700 | [diff] [blame] | 1010 | bool page_was_mapped = false; |
Mel Gorman | 3f6c827 | 2010-05-24 14:32:17 -0700 | [diff] [blame] | 1011 | struct anon_vma *anon_vma = NULL; |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 1012 | bool is_lru = !__PageMovable(page); |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 1013 | |
Nick Piggin | 529ae9a | 2008-08-02 12:01:03 +0200 | [diff] [blame] | 1014 | if (!trylock_page(page)) { |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 1015 | if (!force || mode == MIGRATE_ASYNC) |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1016 | goto out; |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 1017 | |
| 1018 | /* |
| 1019 | * It's not safe for direct compaction to call lock_page. |
| 1020 | * For example, during page readahead pages are added locked |
| 1021 | * to the LRU. Later, when the IO completes the pages are |
| 1022 | * marked uptodate and unlocked. However, the queueing |
| 1023 | * could be merging multiple pages for one bio (e.g. |
Matthew Wilcox (Oracle) | d438834 | 2020-06-01 21:47:02 -0700 | [diff] [blame] | 1024 | * mpage_readahead). If an allocation happens for the |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 1025 | * second or third page, the process can end up locking |
| 1026 | * the same page twice and deadlocking. Rather than |
| 1027 | * trying to be clever about what pages can be locked, |
| 1028 | * avoid the use of lock_page for direct compaction |
| 1029 | * altogether. |
| 1030 | */ |
| 1031 | if (current->flags & PF_MEMALLOC) |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1032 | goto out; |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 1033 | |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1034 | lock_page(page); |
| 1035 | } |
| 1036 | |
| 1037 | if (PageWriteback(page)) { |
Andrea Arcangeli | 11bc82d | 2011-03-22 16:33:11 -0700 | [diff] [blame] | 1038 | /* |
Jianguo Wu | fed5b64 | 2013-04-29 15:07:58 -0700 | [diff] [blame] | 1039 | * Only in the case of a full synchronous migration is it |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 1040 | * necessary to wait for PageWriteback. In the async case, |
| 1041 | * the retry loop is too short and in the sync-light case, |
| 1042 | * the overhead of stalling is too much |
Andrea Arcangeli | 11bc82d | 2011-03-22 16:33:11 -0700 | [diff] [blame] | 1043 | */ |
Jérôme Glisse | 2916ecc | 2017-09-08 16:12:06 -0700 | [diff] [blame] | 1044 | switch (mode) { |
| 1045 | case MIGRATE_SYNC: |
| 1046 | case MIGRATE_SYNC_NO_COPY: |
| 1047 | break; |
| 1048 | default: |
Andrea Arcangeli | 11bc82d | 2011-03-22 16:33:11 -0700 | [diff] [blame] | 1049 | rc = -EBUSY; |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 1050 | goto out_unlock; |
Andrea Arcangeli | 11bc82d | 2011-03-22 16:33:11 -0700 | [diff] [blame] | 1051 | } |
| 1052 | if (!force) |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 1053 | goto out_unlock; |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1054 | wait_on_page_writeback(page); |
| 1055 | } |
Hugh Dickins | 03f15c8 | 2015-11-05 18:49:56 -0800 | [diff] [blame] | 1056 | |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1057 | /* |
Baolin Wang | 68a9843 | 2021-09-08 15:18:03 -0700 | [diff] [blame] | 1058 | * By try_to_migrate(), page->mapcount goes down to 0 here. In this case, |
KAMEZAWA Hiroyuki | dc386d4 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 1059 | * we cannot notice that anon_vma is freed while we migrates a page. |
Hugh Dickins | 1ce82b6 | 2011-01-13 15:47:30 -0800 | [diff] [blame] | 1060 | * This get_anon_vma() delays freeing anon_vma pointer until the end |
KAMEZAWA Hiroyuki | dc386d4 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 1061 | * of migration. File cache pages are no problem because of page_lock() |
KAMEZAWA Hiroyuki | 989f89c | 2007-08-30 23:56:21 -0700 | [diff] [blame] | 1062 | * File Caches may use write_page() or lock_page() in migration, then, |
| 1063 | * just care Anon page here. |
Hugh Dickins | 03f15c8 | 2015-11-05 18:49:56 -0800 | [diff] [blame] | 1064 | * |
| 1065 | * Only page_get_anon_vma() understands the subtleties of |
| 1066 | * getting a hold on an anon_vma from outside one of its mms. |
| 1067 | * But if we cannot get anon_vma, then we won't need it anyway, |
| 1068 | * because that implies that the anon page is no longer mapped |
| 1069 | * (and cannot be remapped so long as we hold the page lock). |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1070 | */ |
Hugh Dickins | 03f15c8 | 2015-11-05 18:49:56 -0800 | [diff] [blame] | 1071 | if (PageAnon(page) && !PageKsm(page)) |
Peter Zijlstra | 746b18d | 2011-05-24 17:12:10 -0700 | [diff] [blame] | 1072 | anon_vma = page_get_anon_vma(page); |
Shaohua Li | 62e1c55 | 2008-02-04 22:29:33 -0800 | [diff] [blame] | 1073 | |
Hugh Dickins | 7db7671 | 2015-11-05 18:49:49 -0800 | [diff] [blame] | 1074 | /* |
| 1075 | * Block others from accessing the new page when we get around to |
| 1076 | * establishing additional references. We are usually the only one |
| 1077 | * holding a reference to newpage at this point. We used to have a BUG |
| 1078 | * here if trylock_page(newpage) fails, but would like to allow for |
| 1079 | * cases where there might be a race with the previous use of newpage. |
| 1080 | * This is much like races on refcount of oldpage: just don't BUG(). |
| 1081 | */ |
| 1082 | if (unlikely(!trylock_page(newpage))) |
| 1083 | goto out_unlock; |
| 1084 | |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 1085 | if (unlikely(!is_lru)) { |
| 1086 | rc = move_to_new_page(newpage, page, mode); |
| 1087 | goto out_unlock_both; |
| 1088 | } |
| 1089 | |
KAMEZAWA Hiroyuki | dc386d4 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 1090 | /* |
Shaohua Li | 62e1c55 | 2008-02-04 22:29:33 -0800 | [diff] [blame] | 1091 | * Corner case handling: |
| 1092 | * 1. When a new swap-cache page is read into, it is added to the LRU |
| 1093 | * and treated as swapcache but it has no rmap yet. |
| 1094 | * Calling try_to_unmap() against a page->mapping==NULL page will |
| 1095 | * trigger a BUG. So handle it here. |
Yang Shi | d12b895 | 2020-12-14 19:13:02 -0800 | [diff] [blame] | 1096 | * 2. An orphaned page (see truncate_cleanup_page) might have |
Shaohua Li | 62e1c55 | 2008-02-04 22:29:33 -0800 | [diff] [blame] | 1097 | * fs-private metadata. The page can be picked up due to memory |
| 1098 | * offlining. Everywhere else except page reclaim, the page is |
| 1099 | * invisible to the vm, so the page can not be migrated. So try to |
| 1100 | * free the metadata, so the page can be freed. |
KAMEZAWA Hiroyuki | dc386d4 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 1101 | */ |
Shaohua Li | 62e1c55 | 2008-02-04 22:29:33 -0800 | [diff] [blame] | 1102 | if (!page->mapping) { |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 1103 | VM_BUG_ON_PAGE(PageAnon(page), page); |
Hugh Dickins | 1ce82b6 | 2011-01-13 15:47:30 -0800 | [diff] [blame] | 1104 | if (page_has_private(page)) { |
Shaohua Li | 62e1c55 | 2008-02-04 22:29:33 -0800 | [diff] [blame] | 1105 | try_to_free_buffers(page); |
Hugh Dickins | 7db7671 | 2015-11-05 18:49:49 -0800 | [diff] [blame] | 1106 | goto out_unlock_both; |
Shaohua Li | 62e1c55 | 2008-02-04 22:29:33 -0800 | [diff] [blame] | 1107 | } |
Hugh Dickins | 7db7671 | 2015-11-05 18:49:49 -0800 | [diff] [blame] | 1108 | } else if (page_mapped(page)) { |
| 1109 | /* Establish migration ptes */ |
Hugh Dickins | 03f15c8 | 2015-11-05 18:49:56 -0800 | [diff] [blame] | 1110 | VM_BUG_ON_PAGE(PageAnon(page) && !PageKsm(page) && !anon_vma, |
| 1111 | page); |
Alistair Popple | a98a2f0 | 2021-06-30 18:54:16 -0700 | [diff] [blame] | 1112 | try_to_migrate(page, 0); |
Baolin Wang | 213ecb3 | 2021-09-08 15:18:06 -0700 | [diff] [blame] | 1113 | page_was_mapped = true; |
Hugh Dickins | 2ebba6b | 2014-12-12 16:56:19 -0800 | [diff] [blame] | 1114 | } |
KAMEZAWA Hiroyuki | dc386d4 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 1115 | |
Christoph Lameter | e6a1530 | 2006-06-25 05:46:49 -0700 | [diff] [blame] | 1116 | if (!page_mapped(page)) |
Hugh Dickins | 5c3f9a6 | 2015-11-05 18:49:53 -0800 | [diff] [blame] | 1117 | rc = move_to_new_page(newpage, page, mode); |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1118 | |
Hugh Dickins | 5c3f9a6 | 2015-11-05 18:49:53 -0800 | [diff] [blame] | 1119 | if (page_was_mapped) |
| 1120 | remove_migration_ptes(page, |
Kirill A. Shutemov | e388466 | 2016-03-17 14:20:07 -0700 | [diff] [blame] | 1121 | rc == MIGRATEPAGE_SUCCESS ? newpage : page, false); |
Mel Gorman | 3f6c827 | 2010-05-24 14:32:17 -0700 | [diff] [blame] | 1122 | |
Hugh Dickins | 7db7671 | 2015-11-05 18:49:49 -0800 | [diff] [blame] | 1123 | out_unlock_both: |
| 1124 | unlock_page(newpage); |
| 1125 | out_unlock: |
Mel Gorman | 3f6c827 | 2010-05-24 14:32:17 -0700 | [diff] [blame] | 1126 | /* Drop an anon_vma reference if we took one */ |
Rik van Riel | 7654506 | 2010-08-09 17:18:41 -0700 | [diff] [blame] | 1127 | if (anon_vma) |
Peter Zijlstra | 9e60109 | 2011-03-22 16:32:46 -0700 | [diff] [blame] | 1128 | put_anon_vma(anon_vma); |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1129 | unlock_page(page); |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1130 | out: |
Minchan Kim | c6c919e | 2016-07-26 15:23:02 -0700 | [diff] [blame] | 1131 | /* |
| 1132 | * If migration is successful, decrease refcount of the newpage |
| 1133 | * which will not free the page because new page owner increased |
| 1134 | * refcounter. As well, if it is LRU page, add the page to LRU |
David Hildenbrand | e0a352f | 2019-02-01 14:21:19 -0800 | [diff] [blame] | 1135 | * list in here. Use the old state of the isolated source page to |
| 1136 | * determine if we migrated a LRU page. newpage was already unlocked |
| 1137 | * and possibly modified by its owner - don't rely on the page |
| 1138 | * state. |
Minchan Kim | c6c919e | 2016-07-26 15:23:02 -0700 | [diff] [blame] | 1139 | */ |
| 1140 | if (rc == MIGRATEPAGE_SUCCESS) { |
David Hildenbrand | e0a352f | 2019-02-01 14:21:19 -0800 | [diff] [blame] | 1141 | if (unlikely(!is_lru)) |
Minchan Kim | c6c919e | 2016-07-26 15:23:02 -0700 | [diff] [blame] | 1142 | put_page(newpage); |
| 1143 | else |
| 1144 | putback_lru_page(newpage); |
| 1145 | } |
| 1146 | |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1147 | return rc; |
| 1148 | } |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 1149 | |
Dave Hansen | 79c28a4 | 2021-09-02 14:59:06 -0700 | [diff] [blame] | 1150 | |
| 1151 | /* |
| 1152 | * node_demotion[] example: |
| 1153 | * |
| 1154 | * Consider a system with two sockets. Each socket has |
| 1155 | * three classes of memory attached: fast, medium and slow. |
| 1156 | * Each memory class is placed in its own NUMA node. The |
| 1157 | * CPUs are placed in the node with the "fast" memory. The |
| 1158 | * 6 NUMA nodes (0-5) might be split among the sockets like |
| 1159 | * this: |
| 1160 | * |
| 1161 | * Socket A: 0, 1, 2 |
| 1162 | * Socket B: 3, 4, 5 |
| 1163 | * |
| 1164 | * When Node 0 fills up, its memory should be migrated to |
| 1165 | * Node 1. When Node 1 fills up, it should be migrated to |
| 1166 | * Node 2. The migration path start on the nodes with the |
| 1167 | * processors (since allocations default to this node) and |
| 1168 | * fast memory, progress through medium and end with the |
| 1169 | * slow memory: |
| 1170 | * |
| 1171 | * 0 -> 1 -> 2 -> stop |
| 1172 | * 3 -> 4 -> 5 -> stop |
| 1173 | * |
| 1174 | * This is represented in the node_demotion[] like this: |
| 1175 | * |
| 1176 | * { 1, // Node 0 migrates to 1 |
| 1177 | * 2, // Node 1 migrates to 2 |
| 1178 | * -1, // Node 2 does not migrate |
| 1179 | * 4, // Node 3 migrates to 4 |
| 1180 | * 5, // Node 4 migrates to 5 |
| 1181 | * -1} // Node 5 does not migrate |
| 1182 | */ |
| 1183 | |
| 1184 | /* |
| 1185 | * Writes to this array occur without locking. Cycles are |
| 1186 | * not allowed: Node X demotes to Y which demotes to X... |
| 1187 | * |
| 1188 | * If multiple reads are performed, a single rcu_read_lock() |
| 1189 | * must be held over all reads to ensure that no cycles are |
| 1190 | * observed. |
| 1191 | */ |
| 1192 | static int node_demotion[MAX_NUMNODES] __read_mostly = |
| 1193 | {[0 ... MAX_NUMNODES - 1] = NUMA_NO_NODE}; |
| 1194 | |
| 1195 | /** |
| 1196 | * next_demotion_node() - Get the next node in the demotion path |
| 1197 | * @node: The starting node to lookup the next node |
| 1198 | * |
Randy Dunlap | c9bd7d1 | 2021-09-02 15:00:36 -0700 | [diff] [blame] | 1199 | * Return: node id for next memory node in the demotion path hierarchy |
Dave Hansen | 79c28a4 | 2021-09-02 14:59:06 -0700 | [diff] [blame] | 1200 | * from @node; NUMA_NO_NODE if @node is terminal. This does not keep |
| 1201 | * @node online or guarantee that it *continues* to be the next demotion |
| 1202 | * target. |
| 1203 | */ |
| 1204 | int next_demotion_node(int node) |
| 1205 | { |
| 1206 | int target; |
| 1207 | |
| 1208 | /* |
| 1209 | * node_demotion[] is updated without excluding this |
| 1210 | * function from running. RCU doesn't provide any |
| 1211 | * compiler barriers, so the READ_ONCE() is required |
| 1212 | * to avoid compiler reordering or read merging. |
| 1213 | * |
| 1214 | * Make sure to use RCU over entire code blocks if |
| 1215 | * node_demotion[] reads need to be consistent. |
| 1216 | */ |
| 1217 | rcu_read_lock(); |
| 1218 | target = READ_ONCE(node_demotion[node]); |
| 1219 | rcu_read_unlock(); |
| 1220 | |
| 1221 | return target; |
| 1222 | } |
| 1223 | |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1224 | /* |
| 1225 | * Obtain the lock on page, remove all ptes and migrate the page |
| 1226 | * to the newly allocated page in newpage. |
| 1227 | */ |
Linus Torvalds | 6ec4476 | 2020-07-08 10:48:35 -0700 | [diff] [blame] | 1228 | static int unmap_and_move(new_page_t get_new_page, |
Geert Uytterhoeven | ef2a515 | 2015-04-14 15:44:22 -0700 | [diff] [blame] | 1229 | free_page_t put_new_page, |
| 1230 | unsigned long private, struct page *page, |
Naoya Horiguchi | add05ce | 2015-06-24 16:56:50 -0700 | [diff] [blame] | 1231 | int force, enum migrate_mode mode, |
Yang Shi | dd4ae78 | 2020-12-14 19:13:06 -0800 | [diff] [blame] | 1232 | enum migrate_reason reason, |
| 1233 | struct list_head *ret) |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1234 | { |
Hugh Dickins | 2def742 | 2015-11-05 18:49:46 -0800 | [diff] [blame] | 1235 | int rc = MIGRATEPAGE_SUCCESS; |
Yang Shi | 74d4a57 | 2019-11-30 17:57:12 -0800 | [diff] [blame] | 1236 | struct page *newpage = NULL; |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1237 | |
Michal Hocko | 94723aa | 2018-04-10 16:30:07 -0700 | [diff] [blame] | 1238 | if (!thp_migration_supported() && PageTransHuge(page)) |
Yang Shi | d532e2e | 2020-12-14 19:13:16 -0800 | [diff] [blame] | 1239 | return -ENOSYS; |
Michal Hocko | 94723aa | 2018-04-10 16:30:07 -0700 | [diff] [blame] | 1240 | |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1241 | if (page_count(page) == 1) { |
| 1242 | /* page was freed from under us. So we are done. */ |
Minchan Kim | c6c919e | 2016-07-26 15:23:02 -0700 | [diff] [blame] | 1243 | ClearPageActive(page); |
| 1244 | ClearPageUnevictable(page); |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 1245 | if (unlikely(__PageMovable(page))) { |
| 1246 | lock_page(page); |
| 1247 | if (!PageMovable(page)) |
andrew.yang | 0d8a836 | 2022-03-15 16:58:34 +1100 | [diff] [blame] | 1248 | ClearPageIsolated(page); |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 1249 | unlock_page(page); |
| 1250 | } |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1251 | goto out; |
| 1252 | } |
| 1253 | |
Yang Shi | 74d4a57 | 2019-11-30 17:57:12 -0800 | [diff] [blame] | 1254 | newpage = get_new_page(page, private); |
| 1255 | if (!newpage) |
| 1256 | return -ENOMEM; |
| 1257 | |
Hugh Dickins | 9c620e2 | 2013-02-22 16:35:14 -0800 | [diff] [blame] | 1258 | rc = __unmap_and_move(page, newpage, force, mode); |
Minchan Kim | c6c919e | 2016-07-26 15:23:02 -0700 | [diff] [blame] | 1259 | if (rc == MIGRATEPAGE_SUCCESS) |
Vlastimil Babka | 7cd12b4 | 2016-03-15 14:56:18 -0700 | [diff] [blame] | 1260 | set_page_owner_migrate_reason(newpage, reason); |
Rafael Aquini | bf6bddf1 | 2012-12-11 16:02:42 -0800 | [diff] [blame] | 1261 | |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1262 | out: |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1263 | if (rc != -EAGAIN) { |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1264 | /* |
| 1265 | * A page that has been migrated has all references |
| 1266 | * removed and will be freed. A page that has not been |
Ralph Campbell | c23a0c9 | 2020-01-30 22:14:41 -0800 | [diff] [blame] | 1267 | * migrated will have kept its references and be restored. |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1268 | */ |
| 1269 | list_del(&page->lru); |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1270 | } |
David Rientjes | 68711a7 | 2014-06-04 16:08:25 -0700 | [diff] [blame] | 1271 | |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 1272 | /* |
Minchan Kim | c6c919e | 2016-07-26 15:23:02 -0700 | [diff] [blame] | 1273 | * If migration is successful, releases reference grabbed during |
| 1274 | * isolation. Otherwise, restore the page to right list unless |
| 1275 | * we want to retry. |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 1276 | */ |
Minchan Kim | c6c919e | 2016-07-26 15:23:02 -0700 | [diff] [blame] | 1277 | if (rc == MIGRATEPAGE_SUCCESS) { |
Yang Shi | dd4ae78 | 2020-12-14 19:13:06 -0800 | [diff] [blame] | 1278 | /* |
| 1279 | * Compaction can migrate also non-LRU pages which are |
| 1280 | * not accounted to NR_ISOLATED_*. They can be recognized |
| 1281 | * as __PageMovable |
| 1282 | */ |
| 1283 | if (likely(!__PageMovable(page))) |
| 1284 | mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + |
| 1285 | page_is_file_lru(page), -thp_nr_pages(page)); |
| 1286 | |
Oscar Salvador | 79f5f8f | 2020-10-15 20:07:09 -0700 | [diff] [blame] | 1287 | if (reason != MR_MEMORY_FAILURE) |
Minchan Kim | c6c919e | 2016-07-26 15:23:02 -0700 | [diff] [blame] | 1288 | /* |
Oscar Salvador | 79f5f8f | 2020-10-15 20:07:09 -0700 | [diff] [blame] | 1289 | * We release the page in page_handle_poison. |
Minchan Kim | c6c919e | 2016-07-26 15:23:02 -0700 | [diff] [blame] | 1290 | */ |
Oscar Salvador | 79f5f8f | 2020-10-15 20:07:09 -0700 | [diff] [blame] | 1291 | put_page(page); |
Minchan Kim | c6c919e | 2016-07-26 15:23:02 -0700 | [diff] [blame] | 1292 | } else { |
Yang Shi | dd4ae78 | 2020-12-14 19:13:06 -0800 | [diff] [blame] | 1293 | if (rc != -EAGAIN) |
| 1294 | list_add_tail(&page->lru, ret); |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 1295 | |
Minchan Kim | c6c919e | 2016-07-26 15:23:02 -0700 | [diff] [blame] | 1296 | if (put_new_page) |
| 1297 | put_new_page(newpage, private); |
| 1298 | else |
| 1299 | put_page(newpage); |
| 1300 | } |
Jianxiong Pan | 07622f0 | 2022-09-22 17:25:35 +0800 | [diff] [blame] | 1301 | #ifdef CONFIG_AMLOGIC_PIN_LOCKED_FILE |
| 1302 | if (reason == MR_CONTIG_RANGE && rc == MIGRATEPAGE_SUCCESS) |
| 1303 | aml_clear_pin_locked_file(page); |
| 1304 | #endif |
David Rientjes | 68711a7 | 2014-06-04 16:08:25 -0700 | [diff] [blame] | 1305 | |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1306 | return rc; |
| 1307 | } |
| 1308 | |
| 1309 | /* |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1310 | * Counterpart of unmap_and_move_page() for hugepage migration. |
| 1311 | * |
| 1312 | * This function doesn't wait the completion of hugepage I/O |
| 1313 | * because there is no race between I/O and migration for hugepage. |
| 1314 | * Note that currently hugepage I/O occurs only in direct I/O |
| 1315 | * where no lock is held and PG_writeback is irrelevant, |
| 1316 | * and writeback status of all subpages are counted in the reference |
| 1317 | * count of the head page (i.e. if all subpages of a 2MB hugepage are |
| 1318 | * under direct I/O, the reference of the head page is 512 and a bit more.) |
| 1319 | * This means that when we try to migrate hugepage whose subpages are |
| 1320 | * doing direct I/O, some references remain after try_to_unmap() and |
| 1321 | * hugepage migration fails without data corruption. |
| 1322 | * |
| 1323 | * There is also no race when direct I/O is issued on the page under migration, |
| 1324 | * because then pte is replaced with migration swap entry and direct I/O code |
| 1325 | * will wait in the page fault for migration to complete. |
| 1326 | */ |
| 1327 | static int unmap_and_move_huge_page(new_page_t get_new_page, |
David Rientjes | 68711a7 | 2014-06-04 16:08:25 -0700 | [diff] [blame] | 1328 | free_page_t put_new_page, unsigned long private, |
| 1329 | struct page *hpage, int force, |
Yang Shi | dd4ae78 | 2020-12-14 19:13:06 -0800 | [diff] [blame] | 1330 | enum migrate_mode mode, int reason, |
| 1331 | struct list_head *ret) |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1332 | { |
Hugh Dickins | 2def742 | 2015-11-05 18:49:46 -0800 | [diff] [blame] | 1333 | int rc = -EAGAIN; |
Hugh Dickins | 2ebba6b | 2014-12-12 16:56:19 -0800 | [diff] [blame] | 1334 | int page_was_mapped = 0; |
Joonsoo Kim | 32665f2 | 2014-01-21 15:51:15 -0800 | [diff] [blame] | 1335 | struct page *new_hpage; |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1336 | struct anon_vma *anon_vma = NULL; |
Mike Kravetz | c0d0381 | 2020-04-01 21:11:05 -0700 | [diff] [blame] | 1337 | struct address_space *mapping = NULL; |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1338 | |
Naoya Horiguchi | 83467ef | 2013-09-11 14:22:11 -0700 | [diff] [blame] | 1339 | /* |
Anshuman Khandual | 7ed2c31 | 2019-03-05 15:43:44 -0800 | [diff] [blame] | 1340 | * Migratability of hugepages depends on architectures and their size. |
Naoya Horiguchi | 83467ef | 2013-09-11 14:22:11 -0700 | [diff] [blame] | 1341 | * This check is necessary because some callers of hugepage migration |
| 1342 | * like soft offline and memory hotremove don't walk through page |
| 1343 | * tables or check whether the hugepage is pmd-based or not before |
| 1344 | * kicking migration. |
| 1345 | */ |
Naoya Horiguchi | 100873d | 2014-06-04 16:10:56 -0700 | [diff] [blame] | 1346 | if (!hugepage_migration_supported(page_hstate(hpage))) { |
Yang Shi | dd4ae78 | 2020-12-14 19:13:06 -0800 | [diff] [blame] | 1347 | list_move_tail(&hpage->lru, ret); |
Naoya Horiguchi | 83467ef | 2013-09-11 14:22:11 -0700 | [diff] [blame] | 1348 | return -ENOSYS; |
Joonsoo Kim | 32665f2 | 2014-01-21 15:51:15 -0800 | [diff] [blame] | 1349 | } |
Naoya Horiguchi | 83467ef | 2013-09-11 14:22:11 -0700 | [diff] [blame] | 1350 | |
Muchun Song | 71a64f6 | 2021-02-04 18:32:17 -0800 | [diff] [blame] | 1351 | if (page_count(hpage) == 1) { |
| 1352 | /* page was freed from under us. So we are done. */ |
| 1353 | putback_active_hugepage(hpage); |
| 1354 | return MIGRATEPAGE_SUCCESS; |
| 1355 | } |
| 1356 | |
Michal Hocko | 666feb2 | 2018-04-10 16:30:03 -0700 | [diff] [blame] | 1357 | new_hpage = get_new_page(hpage, private); |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1358 | if (!new_hpage) |
| 1359 | return -ENOMEM; |
| 1360 | |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1361 | if (!trylock_page(hpage)) { |
Jérôme Glisse | 2916ecc | 2017-09-08 16:12:06 -0700 | [diff] [blame] | 1362 | if (!force) |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1363 | goto out; |
Jérôme Glisse | 2916ecc | 2017-09-08 16:12:06 -0700 | [diff] [blame] | 1364 | switch (mode) { |
| 1365 | case MIGRATE_SYNC: |
| 1366 | case MIGRATE_SYNC_NO_COPY: |
| 1367 | break; |
| 1368 | default: |
| 1369 | goto out; |
| 1370 | } |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1371 | lock_page(hpage); |
| 1372 | } |
| 1373 | |
Mike Kravetz | cb6acd0 | 2019-02-28 16:22:02 -0800 | [diff] [blame] | 1374 | /* |
| 1375 | * Check for pages which are in the process of being freed. Without |
| 1376 | * page_mapping() set, hugetlbfs specific move page routine will not |
| 1377 | * be called and we could leak usage counts for subpools. |
| 1378 | */ |
Muchun Song | 6acfb5b | 2021-06-30 18:51:29 -0700 | [diff] [blame] | 1379 | if (hugetlb_page_subpool(hpage) && !page_mapping(hpage)) { |
Mike Kravetz | cb6acd0 | 2019-02-28 16:22:02 -0800 | [diff] [blame] | 1380 | rc = -EBUSY; |
| 1381 | goto out_unlock; |
| 1382 | } |
| 1383 | |
Peter Zijlstra | 746b18d | 2011-05-24 17:12:10 -0700 | [diff] [blame] | 1384 | if (PageAnon(hpage)) |
| 1385 | anon_vma = page_get_anon_vma(hpage); |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1386 | |
Hugh Dickins | 7db7671 | 2015-11-05 18:49:49 -0800 | [diff] [blame] | 1387 | if (unlikely(!trylock_page(new_hpage))) |
| 1388 | goto put_anon; |
| 1389 | |
Hugh Dickins | 2ebba6b | 2014-12-12 16:56:19 -0800 | [diff] [blame] | 1390 | if (page_mapped(hpage)) { |
Mike Kravetz | 336bf30 | 2020-11-13 22:52:16 -0800 | [diff] [blame] | 1391 | bool mapping_locked = false; |
Alistair Popple | a98a2f0 | 2021-06-30 18:54:16 -0700 | [diff] [blame] | 1392 | enum ttu_flags ttu = 0; |
Mike Kravetz | c0d0381 | 2020-04-01 21:11:05 -0700 | [diff] [blame] | 1393 | |
Mike Kravetz | 336bf30 | 2020-11-13 22:52:16 -0800 | [diff] [blame] | 1394 | if (!PageAnon(hpage)) { |
| 1395 | /* |
| 1396 | * In shared mappings, try_to_unmap could potentially |
| 1397 | * call huge_pmd_unshare. Because of this, take |
| 1398 | * semaphore in write mode here and set TTU_RMAP_LOCKED |
| 1399 | * to let lower levels know we have taken the lock. |
| 1400 | */ |
| 1401 | mapping = hugetlb_page_mapping_lock_write(hpage); |
| 1402 | if (unlikely(!mapping)) |
| 1403 | goto unlock_put_anon; |
| 1404 | |
| 1405 | mapping_locked = true; |
| 1406 | ttu |= TTU_RMAP_LOCKED; |
| 1407 | } |
| 1408 | |
Alistair Popple | a98a2f0 | 2021-06-30 18:54:16 -0700 | [diff] [blame] | 1409 | try_to_migrate(hpage, ttu); |
Hugh Dickins | 2ebba6b | 2014-12-12 16:56:19 -0800 | [diff] [blame] | 1410 | page_was_mapped = 1; |
Mike Kravetz | 336bf30 | 2020-11-13 22:52:16 -0800 | [diff] [blame] | 1411 | |
| 1412 | if (mapping_locked) |
| 1413 | i_mmap_unlock_write(mapping); |
Hugh Dickins | 2ebba6b | 2014-12-12 16:56:19 -0800 | [diff] [blame] | 1414 | } |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1415 | |
| 1416 | if (!page_mapped(hpage)) |
Hugh Dickins | 5c3f9a6 | 2015-11-05 18:49:53 -0800 | [diff] [blame] | 1417 | rc = move_to_new_page(new_hpage, hpage, mode); |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1418 | |
Mike Kravetz | 336bf30 | 2020-11-13 22:52:16 -0800 | [diff] [blame] | 1419 | if (page_was_mapped) |
Hugh Dickins | 5c3f9a6 | 2015-11-05 18:49:53 -0800 | [diff] [blame] | 1420 | remove_migration_ptes(hpage, |
Mike Kravetz | 336bf30 | 2020-11-13 22:52:16 -0800 | [diff] [blame] | 1421 | rc == MIGRATEPAGE_SUCCESS ? new_hpage : hpage, false); |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1422 | |
Mike Kravetz | c0d0381 | 2020-04-01 21:11:05 -0700 | [diff] [blame] | 1423 | unlock_put_anon: |
Hugh Dickins | 7db7671 | 2015-11-05 18:49:49 -0800 | [diff] [blame] | 1424 | unlock_page(new_hpage); |
| 1425 | |
| 1426 | put_anon: |
Hugh Dickins | fd4a466 | 2011-01-13 15:47:31 -0800 | [diff] [blame] | 1427 | if (anon_vma) |
Peter Zijlstra | 9e60109 | 2011-03-22 16:32:46 -0700 | [diff] [blame] | 1428 | put_anon_vma(anon_vma); |
Aneesh Kumar K.V | 8e6ac7f | 2012-07-31 16:42:27 -0700 | [diff] [blame] | 1429 | |
Hugh Dickins | 2def742 | 2015-11-05 18:49:46 -0800 | [diff] [blame] | 1430 | if (rc == MIGRATEPAGE_SUCCESS) { |
Michal Hocko | ab5ac90 | 2018-01-31 16:20:48 -0800 | [diff] [blame] | 1431 | move_hugetlb_state(hpage, new_hpage, reason); |
Hugh Dickins | 2def742 | 2015-11-05 18:49:46 -0800 | [diff] [blame] | 1432 | put_new_page = NULL; |
| 1433 | } |
Aneesh Kumar K.V | 8e6ac7f | 2012-07-31 16:42:27 -0700 | [diff] [blame] | 1434 | |
Mike Kravetz | cb6acd0 | 2019-02-28 16:22:02 -0800 | [diff] [blame] | 1435 | out_unlock: |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1436 | unlock_page(hpage); |
Hillf Danton | 0976133 | 2011-12-08 14:34:20 -0800 | [diff] [blame] | 1437 | out: |
Yang Shi | dd4ae78 | 2020-12-14 19:13:06 -0800 | [diff] [blame] | 1438 | if (rc == MIGRATEPAGE_SUCCESS) |
Naoya Horiguchi | b8ec1ce | 2013-09-11 14:22:01 -0700 | [diff] [blame] | 1439 | putback_active_hugepage(hpage); |
Miaohe Lin | a04840c | 2021-05-04 18:37:07 -0700 | [diff] [blame] | 1440 | else if (rc != -EAGAIN) |
Yang Shi | dd4ae78 | 2020-12-14 19:13:06 -0800 | [diff] [blame] | 1441 | list_move_tail(&hpage->lru, ret); |
David Rientjes | 68711a7 | 2014-06-04 16:08:25 -0700 | [diff] [blame] | 1442 | |
| 1443 | /* |
| 1444 | * If migration was not successful and there's a freeing callback, use |
| 1445 | * it. Otherwise, put_page() will drop the reference grabbed during |
| 1446 | * isolation. |
| 1447 | */ |
Hugh Dickins | 2def742 | 2015-11-05 18:49:46 -0800 | [diff] [blame] | 1448 | if (put_new_page) |
David Rientjes | 68711a7 | 2014-06-04 16:08:25 -0700 | [diff] [blame] | 1449 | put_new_page(new_hpage, private); |
| 1450 | else |
Naoya Horiguchi | 3aaa76e | 2015-09-22 14:59:14 -0700 | [diff] [blame] | 1451 | putback_active_hugepage(new_hpage); |
David Rientjes | 68711a7 | 2014-06-04 16:08:25 -0700 | [diff] [blame] | 1452 | |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1453 | return rc; |
| 1454 | } |
| 1455 | |
Yang Shi | d532e2e | 2020-12-14 19:13:16 -0800 | [diff] [blame] | 1456 | static inline int try_split_thp(struct page *page, struct page **page2, |
| 1457 | struct list_head *from) |
| 1458 | { |
| 1459 | int rc = 0; |
| 1460 | |
| 1461 | lock_page(page); |
| 1462 | rc = split_huge_page_to_list(page, from); |
| 1463 | unlock_page(page); |
| 1464 | if (!rc) |
| 1465 | list_safe_reset_next(page, *page2, lru); |
| 1466 | |
| 1467 | return rc; |
| 1468 | } |
| 1469 | |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1470 | /* |
Srivatsa S. Bhat | c73e5c9 | 2013-04-29 15:08:16 -0700 | [diff] [blame] | 1471 | * migrate_pages - migrate the pages specified in a list, to the free pages |
| 1472 | * supplied as the target for the page migration |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1473 | * |
Srivatsa S. Bhat | c73e5c9 | 2013-04-29 15:08:16 -0700 | [diff] [blame] | 1474 | * @from: The list of pages to be migrated. |
| 1475 | * @get_new_page: The function used to allocate free pages to be used |
| 1476 | * as the target of the page migration. |
David Rientjes | 68711a7 | 2014-06-04 16:08:25 -0700 | [diff] [blame] | 1477 | * @put_new_page: The function used to free target pages if migration |
| 1478 | * fails, or NULL if no special handling is necessary. |
Srivatsa S. Bhat | c73e5c9 | 2013-04-29 15:08:16 -0700 | [diff] [blame] | 1479 | * @private: Private data to be passed on to get_new_page() |
| 1480 | * @mode: The migration mode that specifies the constraints for |
| 1481 | * page migration, if any. |
| 1482 | * @reason: The reason for page migration. |
Yang Shi | 5ac9588 | 2021-09-02 14:59:13 -0700 | [diff] [blame] | 1483 | * @ret_succeeded: Set to the number of pages migrated successfully if |
| 1484 | * the caller passes a non-NULL pointer. |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1485 | * |
Srivatsa S. Bhat | c73e5c9 | 2013-04-29 15:08:16 -0700 | [diff] [blame] | 1486 | * The function returns after 10 attempts or if no pages are movable any more |
| 1487 | * because the list has become empty or no retryable pages exist any more. |
Yang Shi | dd4ae78 | 2020-12-14 19:13:06 -0800 | [diff] [blame] | 1488 | * It is caller's responsibility to call putback_movable_pages() to return pages |
| 1489 | * to the LRU or free list only if ret != 0. |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1490 | * |
Srivatsa S. Bhat | c73e5c9 | 2013-04-29 15:08:16 -0700 | [diff] [blame] | 1491 | * Returns the number of pages that were not migrated, or an error code. |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1492 | */ |
Hugh Dickins | 9c620e2 | 2013-02-22 16:35:14 -0800 | [diff] [blame] | 1493 | int migrate_pages(struct list_head *from, new_page_t get_new_page, |
David Rientjes | 68711a7 | 2014-06-04 16:08:25 -0700 | [diff] [blame] | 1494 | free_page_t put_new_page, unsigned long private, |
Yang Shi | 5ac9588 | 2021-09-02 14:59:13 -0700 | [diff] [blame] | 1495 | enum migrate_mode mode, int reason, unsigned int *ret_succeeded) |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1496 | { |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1497 | int retry = 1; |
Anshuman Khandual | 1a5bae2 | 2020-08-11 18:31:51 -0700 | [diff] [blame] | 1498 | int thp_retry = 1; |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1499 | int nr_failed = 0; |
Mel Gorman | 5647bc2 | 2012-10-19 10:46:20 +0100 | [diff] [blame] | 1500 | int nr_succeeded = 0; |
Anshuman Khandual | 1a5bae2 | 2020-08-11 18:31:51 -0700 | [diff] [blame] | 1501 | int nr_thp_succeeded = 0; |
| 1502 | int nr_thp_failed = 0; |
| 1503 | int nr_thp_split = 0; |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1504 | int pass = 0; |
Anshuman Khandual | 1a5bae2 | 2020-08-11 18:31:51 -0700 | [diff] [blame] | 1505 | bool is_thp = false; |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1506 | struct page *page; |
| 1507 | struct page *page2; |
| 1508 | int swapwrite = current->flags & PF_SWAPWRITE; |
Anshuman Khandual | 1a5bae2 | 2020-08-11 18:31:51 -0700 | [diff] [blame] | 1509 | int rc, nr_subpages; |
Yang Shi | dd4ae78 | 2020-12-14 19:13:06 -0800 | [diff] [blame] | 1510 | LIST_HEAD(ret_pages); |
Yang Shi | b0b515b | 2021-06-30 18:51:48 -0700 | [diff] [blame] | 1511 | bool nosplit = (reason == MR_NUMA_MISPLACED); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1512 | |
Liam Mark | 7bc1aec | 2021-05-04 18:37:25 -0700 | [diff] [blame] | 1513 | trace_mm_migrate_pages_start(mode, reason); |
| 1514 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1515 | if (!swapwrite) |
| 1516 | current->flags |= PF_SWAPWRITE; |
| 1517 | |
Anshuman Khandual | 1a5bae2 | 2020-08-11 18:31:51 -0700 | [diff] [blame] | 1518 | for (pass = 0; pass < 10 && (retry || thp_retry); pass++) { |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1519 | retry = 0; |
Anshuman Khandual | 1a5bae2 | 2020-08-11 18:31:51 -0700 | [diff] [blame] | 1520 | thp_retry = 0; |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1521 | |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1522 | list_for_each_entry_safe(page, page2, from, lru) { |
Michal Hocko | 94723aa | 2018-04-10 16:30:07 -0700 | [diff] [blame] | 1523 | retry: |
Anshuman Khandual | 1a5bae2 | 2020-08-11 18:31:51 -0700 | [diff] [blame] | 1524 | /* |
| 1525 | * THP statistics is based on the source huge page. |
| 1526 | * Capture required information that might get lost |
| 1527 | * during migration. |
| 1528 | */ |
Zi Yan | 6c5c7b9 | 2020-09-25 21:19:14 -0700 | [diff] [blame] | 1529 | is_thp = PageTransHuge(page) && !PageHuge(page); |
Matthew Wilcox (Oracle) | 6c35784 | 2020-08-14 17:30:37 -0700 | [diff] [blame] | 1530 | nr_subpages = thp_nr_pages(page); |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1531 | cond_resched(); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1532 | |
Naoya Horiguchi | 31caf66 | 2013-09-11 14:21:59 -0700 | [diff] [blame] | 1533 | if (PageHuge(page)) |
| 1534 | rc = unmap_and_move_huge_page(get_new_page, |
David Rientjes | 68711a7 | 2014-06-04 16:08:25 -0700 | [diff] [blame] | 1535 | put_new_page, private, page, |
Yang Shi | dd4ae78 | 2020-12-14 19:13:06 -0800 | [diff] [blame] | 1536 | pass > 2, mode, reason, |
| 1537 | &ret_pages); |
Naoya Horiguchi | 31caf66 | 2013-09-11 14:21:59 -0700 | [diff] [blame] | 1538 | else |
David Rientjes | 68711a7 | 2014-06-04 16:08:25 -0700 | [diff] [blame] | 1539 | rc = unmap_and_move(get_new_page, put_new_page, |
Naoya Horiguchi | add05ce | 2015-06-24 16:56:50 -0700 | [diff] [blame] | 1540 | private, page, pass > 2, mode, |
Yang Shi | dd4ae78 | 2020-12-14 19:13:06 -0800 | [diff] [blame] | 1541 | reason, &ret_pages); |
| 1542 | /* |
| 1543 | * The rules are: |
| 1544 | * Success: non hugetlb page will be freed, hugetlb |
| 1545 | * page will be put back |
| 1546 | * -EAGAIN: stay on the from list |
| 1547 | * -ENOMEM: stay on the from list |
| 1548 | * Other errno: put on ret_pages list then splice to |
| 1549 | * from list |
| 1550 | */ |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1551 | switch(rc) { |
Yang Shi | d532e2e | 2020-12-14 19:13:16 -0800 | [diff] [blame] | 1552 | /* |
| 1553 | * THP migration might be unsupported or the |
| 1554 | * allocation could've failed so we should |
| 1555 | * retry on the same page with the THP split |
| 1556 | * to base pages. |
| 1557 | * |
| 1558 | * Head page is retried immediately and tail |
| 1559 | * pages are added to the tail of the list so |
| 1560 | * we encounter them after the rest of the list |
| 1561 | * is processed. |
| 1562 | */ |
| 1563 | case -ENOSYS: |
| 1564 | /* THP migration is unsupported */ |
| 1565 | if (is_thp) { |
| 1566 | if (!try_split_thp(page, &page2, from)) { |
| 1567 | nr_thp_split++; |
| 1568 | goto retry; |
| 1569 | } |
| 1570 | |
| 1571 | nr_thp_failed++; |
| 1572 | nr_failed += nr_subpages; |
| 1573 | break; |
| 1574 | } |
| 1575 | |
| 1576 | /* Hugetlb migration is unsupported */ |
| 1577 | nr_failed++; |
Tao Zeng | 54f845e | 2022-01-26 16:04:08 +0800 | [diff] [blame] | 1578 | #ifdef CONFIG_AMLOGIC_CMA |
| 1579 | cma_debug(2, page, " NO SYS\n"); |
| 1580 | #endif |
Yang Shi | d532e2e | 2020-12-14 19:13:16 -0800 | [diff] [blame] | 1581 | break; |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 1582 | case -ENOMEM: |
Michal Hocko | 94723aa | 2018-04-10 16:30:07 -0700 | [diff] [blame] | 1583 | /* |
Yang Shi | d532e2e | 2020-12-14 19:13:16 -0800 | [diff] [blame] | 1584 | * When memory is low, don't bother to try to migrate |
| 1585 | * other pages, just exit. |
Yang Shi | b0b515b | 2021-06-30 18:51:48 -0700 | [diff] [blame] | 1586 | * THP NUMA faulting doesn't split THP to retry. |
Michal Hocko | 94723aa | 2018-04-10 16:30:07 -0700 | [diff] [blame] | 1587 | */ |
Yang Shi | b0b515b | 2021-06-30 18:51:48 -0700 | [diff] [blame] | 1588 | if (is_thp && !nosplit) { |
Yang Shi | d532e2e | 2020-12-14 19:13:16 -0800 | [diff] [blame] | 1589 | if (!try_split_thp(page, &page2, from)) { |
Anshuman Khandual | 1a5bae2 | 2020-08-11 18:31:51 -0700 | [diff] [blame] | 1590 | nr_thp_split++; |
Michal Hocko | 94723aa | 2018-04-10 16:30:07 -0700 | [diff] [blame] | 1591 | goto retry; |
| 1592 | } |
Zi Yan | 6c5c7b9 | 2020-09-25 21:19:14 -0700 | [diff] [blame] | 1593 | |
Anshuman Khandual | 1a5bae2 | 2020-08-11 18:31:51 -0700 | [diff] [blame] | 1594 | nr_thp_failed++; |
| 1595 | nr_failed += nr_subpages; |
| 1596 | goto out; |
| 1597 | } |
David Rientjes | dfef2ef | 2016-05-20 16:59:05 -0700 | [diff] [blame] | 1598 | nr_failed++; |
Tao Zeng | 54f845e | 2022-01-26 16:04:08 +0800 | [diff] [blame] | 1599 | #ifdef CONFIG_AMLOGIC_CMA |
| 1600 | cma_debug(2, page, " NO MEM\n"); |
| 1601 | #endif |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 1602 | goto out; |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1603 | case -EAGAIN: |
Anshuman Khandual | 1a5bae2 | 2020-08-11 18:31:51 -0700 | [diff] [blame] | 1604 | if (is_thp) { |
| 1605 | thp_retry++; |
| 1606 | break; |
| 1607 | } |
Christoph Lameter | 2d1db3b | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 1608 | retry++; |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1609 | break; |
Rafael Aquini | 78bd520 | 2012-12-11 16:02:31 -0800 | [diff] [blame] | 1610 | case MIGRATEPAGE_SUCCESS: |
Anshuman Khandual | 1a5bae2 | 2020-08-11 18:31:51 -0700 | [diff] [blame] | 1611 | if (is_thp) { |
| 1612 | nr_thp_succeeded++; |
| 1613 | nr_succeeded += nr_subpages; |
| 1614 | break; |
| 1615 | } |
Mel Gorman | 5647bc2 | 2012-10-19 10:46:20 +0100 | [diff] [blame] | 1616 | nr_succeeded++; |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1617 | break; |
| 1618 | default: |
Naoya Horiguchi | 354a336 | 2014-01-21 15:51:14 -0800 | [diff] [blame] | 1619 | /* |
Yang Shi | d532e2e | 2020-12-14 19:13:16 -0800 | [diff] [blame] | 1620 | * Permanent failure (-EBUSY, etc.): |
Naoya Horiguchi | 354a336 | 2014-01-21 15:51:14 -0800 | [diff] [blame] | 1621 | * unlike -EAGAIN case, the failed page is |
| 1622 | * removed from migration page list and not |
| 1623 | * retried in the next outer loop. |
| 1624 | */ |
Anshuman Khandual | 1a5bae2 | 2020-08-11 18:31:51 -0700 | [diff] [blame] | 1625 | if (is_thp) { |
| 1626 | nr_thp_failed++; |
| 1627 | nr_failed += nr_subpages; |
| 1628 | break; |
| 1629 | } |
Christoph Lameter | 2d1db3b | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 1630 | nr_failed++; |
Tao Zeng | 54f845e | 2022-01-26 16:04:08 +0800 | [diff] [blame] | 1631 | #ifdef CONFIG_AMLOGIC_CMA |
| 1632 | cma_debug(2, page, " failed:%d\n", rc); |
| 1633 | #endif |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1634 | break; |
Christoph Lameter | 2d1db3b | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 1635 | } |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1636 | } |
| 1637 | } |
Anshuman Khandual | 1a5bae2 | 2020-08-11 18:31:51 -0700 | [diff] [blame] | 1638 | nr_failed += retry + thp_retry; |
| 1639 | nr_thp_failed += thp_retry; |
Vlastimil Babka | f2f81fb | 2015-11-05 18:47:03 -0800 | [diff] [blame] | 1640 | rc = nr_failed; |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 1641 | out: |
Yang Shi | dd4ae78 | 2020-12-14 19:13:06 -0800 | [diff] [blame] | 1642 | /* |
| 1643 | * Put the permanent failure page back to migration list, they |
| 1644 | * will be put back to the right list by the caller. |
| 1645 | */ |
| 1646 | list_splice(&ret_pages, from); |
| 1647 | |
Anshuman Khandual | 1a5bae2 | 2020-08-11 18:31:51 -0700 | [diff] [blame] | 1648 | count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded); |
| 1649 | count_vm_events(PGMIGRATE_FAIL, nr_failed); |
| 1650 | count_vm_events(THP_MIGRATION_SUCCESS, nr_thp_succeeded); |
| 1651 | count_vm_events(THP_MIGRATION_FAIL, nr_thp_failed); |
| 1652 | count_vm_events(THP_MIGRATION_SPLIT, nr_thp_split); |
| 1653 | trace_mm_migrate_pages(nr_succeeded, nr_failed, nr_thp_succeeded, |
| 1654 | nr_thp_failed, nr_thp_split, mode, reason); |
Mel Gorman | 7b2a2d4 | 2012-10-19 14:07:31 +0100 | [diff] [blame] | 1655 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1656 | if (!swapwrite) |
| 1657 | current->flags &= ~PF_SWAPWRITE; |
| 1658 | |
Yang Shi | 5ac9588 | 2021-09-02 14:59:13 -0700 | [diff] [blame] | 1659 | if (ret_succeeded) |
| 1660 | *ret_succeeded = nr_succeeded; |
| 1661 | |
Rafael Aquini | 78bd520 | 2012-12-11 16:02:31 -0800 | [diff] [blame] | 1662 | return rc; |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1663 | } |
Charan Teja Reddy | f47b852 | 2021-02-16 13:59:45 +0530 | [diff] [blame] | 1664 | EXPORT_SYMBOL_GPL(migrate_pages); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1665 | |
Joonsoo Kim | 19fc7be | 2020-08-11 18:37:25 -0700 | [diff] [blame] | 1666 | struct page *alloc_migration_target(struct page *page, unsigned long private) |
Joonsoo Kim | b4b3822 | 2020-08-11 18:37:14 -0700 | [diff] [blame] | 1667 | { |
Joonsoo Kim | 19fc7be | 2020-08-11 18:37:25 -0700 | [diff] [blame] | 1668 | struct migration_target_control *mtc; |
| 1669 | gfp_t gfp_mask; |
Joonsoo Kim | b4b3822 | 2020-08-11 18:37:14 -0700 | [diff] [blame] | 1670 | unsigned int order = 0; |
| 1671 | struct page *new_page = NULL; |
Joonsoo Kim | 19fc7be | 2020-08-11 18:37:25 -0700 | [diff] [blame] | 1672 | int nid; |
| 1673 | int zidx; |
| 1674 | |
| 1675 | mtc = (struct migration_target_control *)private; |
| 1676 | gfp_mask = mtc->gfp_mask; |
| 1677 | nid = mtc->nid; |
| 1678 | if (nid == NUMA_NO_NODE) |
| 1679 | nid = page_to_nid(page); |
Joonsoo Kim | b4b3822 | 2020-08-11 18:37:14 -0700 | [diff] [blame] | 1680 | |
Joonsoo Kim | d92bbc2 | 2020-08-11 18:37:17 -0700 | [diff] [blame] | 1681 | if (PageHuge(page)) { |
| 1682 | struct hstate *h = page_hstate(compound_head(page)); |
| 1683 | |
Joonsoo Kim | 19fc7be | 2020-08-11 18:37:25 -0700 | [diff] [blame] | 1684 | gfp_mask = htlb_modify_alloc_mask(h, gfp_mask); |
| 1685 | return alloc_huge_page_nodemask(h, nid, mtc->nmask, gfp_mask); |
Joonsoo Kim | d92bbc2 | 2020-08-11 18:37:17 -0700 | [diff] [blame] | 1686 | } |
Joonsoo Kim | b4b3822 | 2020-08-11 18:37:14 -0700 | [diff] [blame] | 1687 | |
| 1688 | if (PageTransHuge(page)) { |
Joonsoo Kim | 9933a0c | 2020-08-11 18:37:20 -0700 | [diff] [blame] | 1689 | /* |
| 1690 | * clear __GFP_RECLAIM to make the migration callback |
| 1691 | * consistent with regular THP allocations. |
| 1692 | */ |
| 1693 | gfp_mask &= ~__GFP_RECLAIM; |
Joonsoo Kim | b4b3822 | 2020-08-11 18:37:14 -0700 | [diff] [blame] | 1694 | gfp_mask |= GFP_TRANSHUGE; |
| 1695 | order = HPAGE_PMD_ORDER; |
| 1696 | } |
Joonsoo Kim | 19fc7be | 2020-08-11 18:37:25 -0700 | [diff] [blame] | 1697 | zidx = zone_idx(page_zone(page)); |
| 1698 | if (is_highmem_idx(zidx) || zidx == ZONE_MOVABLE) |
Joonsoo Kim | b4b3822 | 2020-08-11 18:37:14 -0700 | [diff] [blame] | 1699 | gfp_mask |= __GFP_HIGHMEM; |
| 1700 | |
Matthew Wilcox (Oracle) | 84172f4 | 2021-04-29 23:01:15 -0700 | [diff] [blame] | 1701 | new_page = __alloc_pages(gfp_mask, order, nid, mtc->nmask); |
Joonsoo Kim | b4b3822 | 2020-08-11 18:37:14 -0700 | [diff] [blame] | 1702 | |
| 1703 | if (new_page && PageTransHuge(new_page)) |
| 1704 | prep_transhuge_page(new_page); |
| 1705 | |
| 1706 | return new_page; |
| 1707 | } |
| 1708 | |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1709 | #ifdef CONFIG_NUMA |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1710 | |
Michal Hocko | a49bd4d | 2018-04-10 16:29:59 -0700 | [diff] [blame] | 1711 | static int store_status(int __user *status, int start, int value, int nr) |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1712 | { |
Michal Hocko | a49bd4d | 2018-04-10 16:29:59 -0700 | [diff] [blame] | 1713 | while (nr-- > 0) { |
| 1714 | if (put_user(value, status + start)) |
| 1715 | return -EFAULT; |
| 1716 | start++; |
| 1717 | } |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1718 | |
Michal Hocko | a49bd4d | 2018-04-10 16:29:59 -0700 | [diff] [blame] | 1719 | return 0; |
| 1720 | } |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1721 | |
Michal Hocko | a49bd4d | 2018-04-10 16:29:59 -0700 | [diff] [blame] | 1722 | static int do_move_pages_to_node(struct mm_struct *mm, |
| 1723 | struct list_head *pagelist, int node) |
| 1724 | { |
| 1725 | int err; |
Joonsoo Kim | a097631 | 2020-08-11 18:37:28 -0700 | [diff] [blame] | 1726 | struct migration_target_control mtc = { |
| 1727 | .nid = node, |
| 1728 | .gfp_mask = GFP_HIGHUSER_MOVABLE | __GFP_THISNODE, |
| 1729 | }; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1730 | |
Joonsoo Kim | a097631 | 2020-08-11 18:37:28 -0700 | [diff] [blame] | 1731 | err = migrate_pages(pagelist, alloc_migration_target, NULL, |
Yang Shi | 5ac9588 | 2021-09-02 14:59:13 -0700 | [diff] [blame] | 1732 | (unsigned long)&mtc, MIGRATE_SYNC, MR_SYSCALL, NULL); |
Michal Hocko | a49bd4d | 2018-04-10 16:29:59 -0700 | [diff] [blame] | 1733 | if (err) |
| 1734 | putback_movable_pages(pagelist); |
| 1735 | return err; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1736 | } |
| 1737 | |
| 1738 | /* |
Michal Hocko | a49bd4d | 2018-04-10 16:29:59 -0700 | [diff] [blame] | 1739 | * Resolves the given address to a struct page, isolates it from the LRU and |
| 1740 | * puts it to the given pagelist. |
Yang Shi | e0153fc | 2020-01-04 12:59:46 -0800 | [diff] [blame] | 1741 | * Returns: |
| 1742 | * errno - if the page cannot be found/isolated |
| 1743 | * 0 - when it doesn't have to be migrated because it is already on the |
| 1744 | * target node |
| 1745 | * 1 - when it has been queued |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1746 | */ |
Michal Hocko | a49bd4d | 2018-04-10 16:29:59 -0700 | [diff] [blame] | 1747 | static int add_page_for_migration(struct mm_struct *mm, unsigned long addr, |
| 1748 | int node, struct list_head *pagelist, bool migrate_all) |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1749 | { |
Michal Hocko | a49bd4d | 2018-04-10 16:29:59 -0700 | [diff] [blame] | 1750 | struct vm_area_struct *vma; |
| 1751 | struct page *page; |
| 1752 | unsigned int follflags; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1753 | int err; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1754 | |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1755 | mmap_read_lock(mm); |
Michal Hocko | a49bd4d | 2018-04-10 16:29:59 -0700 | [diff] [blame] | 1756 | err = -EFAULT; |
| 1757 | vma = find_vma(mm, addr); |
| 1758 | if (!vma || addr < vma->vm_start || !vma_migratable(vma)) |
| 1759 | goto out; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1760 | |
Michal Hocko | a49bd4d | 2018-04-10 16:29:59 -0700 | [diff] [blame] | 1761 | /* FOLL_DUMP to ignore special (like zero) pages */ |
| 1762 | follflags = FOLL_GET | FOLL_DUMP; |
Michal Hocko | a49bd4d | 2018-04-10 16:29:59 -0700 | [diff] [blame] | 1763 | page = follow_page(vma, addr, follflags); |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1764 | |
Michal Hocko | a49bd4d | 2018-04-10 16:29:59 -0700 | [diff] [blame] | 1765 | err = PTR_ERR(page); |
| 1766 | if (IS_ERR(page)) |
| 1767 | goto out; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1768 | |
Michal Hocko | a49bd4d | 2018-04-10 16:29:59 -0700 | [diff] [blame] | 1769 | err = -ENOENT; |
| 1770 | if (!page) |
| 1771 | goto out; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1772 | |
Brice Goglin | e78bbfa | 2008-10-18 20:27:15 -0700 | [diff] [blame] | 1773 | err = 0; |
Michal Hocko | a49bd4d | 2018-04-10 16:29:59 -0700 | [diff] [blame] | 1774 | if (page_to_nid(page) == node) |
| 1775 | goto out_putpage; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1776 | |
Michal Hocko | a49bd4d | 2018-04-10 16:29:59 -0700 | [diff] [blame] | 1777 | err = -EACCES; |
| 1778 | if (page_mapcount(page) > 1 && !migrate_all) |
| 1779 | goto out_putpage; |
| 1780 | |
| 1781 | if (PageHuge(page)) { |
| 1782 | if (PageHead(page)) { |
Miaohe Lin | 072e741 | 2022-05-30 19:30:15 +0800 | [diff] [blame] | 1783 | err = isolate_hugetlb(page, pagelist); |
| 1784 | if (!err) |
| 1785 | err = 1; |
Michal Hocko | a49bd4d | 2018-04-10 16:29:59 -0700 | [diff] [blame] | 1786 | } |
| 1787 | } else { |
| 1788 | struct page *head; |
| 1789 | |
| 1790 | head = compound_head(page); |
| 1791 | err = isolate_lru_page(head); |
| 1792 | if (err) |
| 1793 | goto out_putpage; |
| 1794 | |
Yang Shi | e0153fc | 2020-01-04 12:59:46 -0800 | [diff] [blame] | 1795 | err = 1; |
Michal Hocko | a49bd4d | 2018-04-10 16:29:59 -0700 | [diff] [blame] | 1796 | list_add_tail(&head->lru, pagelist); |
| 1797 | mod_node_page_state(page_pgdat(head), |
Huang Ying | 9de4f22 | 2020-04-06 20:04:41 -0700 | [diff] [blame] | 1798 | NR_ISOLATED_ANON + page_is_file_lru(head), |
Matthew Wilcox (Oracle) | 6c35784 | 2020-08-14 17:30:37 -0700 | [diff] [blame] | 1799 | thp_nr_pages(head)); |
Michal Hocko | a49bd4d | 2018-04-10 16:29:59 -0700 | [diff] [blame] | 1800 | } |
| 1801 | out_putpage: |
| 1802 | /* |
| 1803 | * Either remove the duplicate refcount from |
| 1804 | * isolate_lru_page() or drop the page ref if it was |
| 1805 | * not isolated. |
| 1806 | */ |
| 1807 | put_page(page); |
| 1808 | out: |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1809 | mmap_read_unlock(mm); |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1810 | return err; |
| 1811 | } |
| 1812 | |
Wei Yang | 7ca8783 | 2020-04-06 20:04:12 -0700 | [diff] [blame] | 1813 | static int move_pages_and_store_status(struct mm_struct *mm, int node, |
| 1814 | struct list_head *pagelist, int __user *status, |
| 1815 | int start, int i, unsigned long nr_pages) |
| 1816 | { |
| 1817 | int err; |
| 1818 | |
Wei Yang | 5d7ae89 | 2020-04-06 20:04:15 -0700 | [diff] [blame] | 1819 | if (list_empty(pagelist)) |
| 1820 | return 0; |
| 1821 | |
Wei Yang | 7ca8783 | 2020-04-06 20:04:12 -0700 | [diff] [blame] | 1822 | err = do_move_pages_to_node(mm, pagelist, node); |
| 1823 | if (err) { |
| 1824 | /* |
| 1825 | * Positive err means the number of failed |
| 1826 | * pages to migrate. Since we are going to |
| 1827 | * abort and return the number of non-migrated |
Long Li | ab9dd4f | 2020-12-14 19:12:52 -0800 | [diff] [blame] | 1828 | * pages, so need to include the rest of the |
Wei Yang | 7ca8783 | 2020-04-06 20:04:12 -0700 | [diff] [blame] | 1829 | * nr_pages that have not been attempted as |
| 1830 | * well. |
| 1831 | */ |
| 1832 | if (err > 0) |
| 1833 | err += nr_pages - i - 1; |
| 1834 | return err; |
| 1835 | } |
| 1836 | return store_status(status, start, node, i - start); |
| 1837 | } |
| 1838 | |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1839 | /* |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1840 | * Migrate an array of page address onto an array of nodes and fill |
| 1841 | * the corresponding array of status. |
| 1842 | */ |
Christoph Lameter | 3268c63 | 2012-03-21 16:34:06 -0700 | [diff] [blame] | 1843 | static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes, |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1844 | unsigned long nr_pages, |
| 1845 | const void __user * __user *pages, |
| 1846 | const int __user *nodes, |
| 1847 | int __user *status, int flags) |
| 1848 | { |
Gregory Price | 556b68d | 2023-10-03 10:48:56 -0400 | [diff] [blame] | 1849 | compat_uptr_t __user *compat_pages = (void __user *)pages; |
Michal Hocko | a49bd4d | 2018-04-10 16:29:59 -0700 | [diff] [blame] | 1850 | int current_node = NUMA_NO_NODE; |
| 1851 | LIST_HEAD(pagelist); |
| 1852 | int start, i; |
| 1853 | int err = 0, err1; |
Brice Goglin | 35282a2 | 2009-06-16 15:32:43 -0700 | [diff] [blame] | 1854 | |
Minchan Kim | 361a2a2 | 2021-05-04 18:36:57 -0700 | [diff] [blame] | 1855 | lru_cache_disable(); |
Brice Goglin | 35282a2 | 2009-06-16 15:32:43 -0700 | [diff] [blame] | 1856 | |
Michal Hocko | a49bd4d | 2018-04-10 16:29:59 -0700 | [diff] [blame] | 1857 | for (i = start = 0; i < nr_pages; i++) { |
| 1858 | const void __user *p; |
| 1859 | unsigned long addr; |
| 1860 | int node; |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1861 | |
Michal Hocko | a49bd4d | 2018-04-10 16:29:59 -0700 | [diff] [blame] | 1862 | err = -EFAULT; |
Gregory Price | 556b68d | 2023-10-03 10:48:56 -0400 | [diff] [blame] | 1863 | if (in_compat_syscall()) { |
| 1864 | compat_uptr_t cp; |
| 1865 | |
| 1866 | if (get_user(cp, compat_pages + i)) |
| 1867 | goto out_flush; |
| 1868 | |
| 1869 | p = compat_ptr(cp); |
| 1870 | } else { |
| 1871 | if (get_user(p, pages + i)) |
| 1872 | goto out_flush; |
| 1873 | } |
Michal Hocko | a49bd4d | 2018-04-10 16:29:59 -0700 | [diff] [blame] | 1874 | if (get_user(node, nodes + i)) |
| 1875 | goto out_flush; |
Andrey Konovalov | 057d3389 | 2019-09-25 16:48:30 -0700 | [diff] [blame] | 1876 | addr = (unsigned long)untagged_addr(p); |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1877 | |
Michal Hocko | a49bd4d | 2018-04-10 16:29:59 -0700 | [diff] [blame] | 1878 | err = -ENODEV; |
| 1879 | if (node < 0 || node >= MAX_NUMNODES) |
| 1880 | goto out_flush; |
| 1881 | if (!node_state(node, N_MEMORY)) |
| 1882 | goto out_flush; |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1883 | |
Michal Hocko | a49bd4d | 2018-04-10 16:29:59 -0700 | [diff] [blame] | 1884 | err = -EACCES; |
| 1885 | if (!node_isset(node, task_nodes)) |
| 1886 | goto out_flush; |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1887 | |
Michal Hocko | a49bd4d | 2018-04-10 16:29:59 -0700 | [diff] [blame] | 1888 | if (current_node == NUMA_NO_NODE) { |
| 1889 | current_node = node; |
| 1890 | start = i; |
| 1891 | } else if (node != current_node) { |
Wei Yang | 7ca8783 | 2020-04-06 20:04:12 -0700 | [diff] [blame] | 1892 | err = move_pages_and_store_status(mm, current_node, |
| 1893 | &pagelist, status, start, i, nr_pages); |
Michal Hocko | a49bd4d | 2018-04-10 16:29:59 -0700 | [diff] [blame] | 1894 | if (err) |
| 1895 | goto out; |
| 1896 | start = i; |
| 1897 | current_node = node; |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1898 | } |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1899 | |
Michal Hocko | a49bd4d | 2018-04-10 16:29:59 -0700 | [diff] [blame] | 1900 | /* |
| 1901 | * Errors in the page lookup or isolation are not fatal and we simply |
| 1902 | * report them via status |
| 1903 | */ |
| 1904 | err = add_page_for_migration(mm, addr, current_node, |
| 1905 | &pagelist, flags & MPOL_MF_MOVE_ALL); |
Yang Shi | e0153fc | 2020-01-04 12:59:46 -0800 | [diff] [blame] | 1906 | |
Wei Yang | d08221a | 2020-04-06 20:04:18 -0700 | [diff] [blame] | 1907 | if (err > 0) { |
Yang Shi | e0153fc | 2020-01-04 12:59:46 -0800 | [diff] [blame] | 1908 | /* The page is successfully queued for migration */ |
| 1909 | continue; |
| 1910 | } |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1911 | |
Wei Yang | d08221a | 2020-04-06 20:04:18 -0700 | [diff] [blame] | 1912 | /* |
| 1913 | * If the page is already on the target node (!err), store the |
| 1914 | * node, otherwise, store the err. |
| 1915 | */ |
| 1916 | err = store_status(status, i, err ? : current_node, 1); |
Michal Hocko | a49bd4d | 2018-04-10 16:29:59 -0700 | [diff] [blame] | 1917 | if (err) |
| 1918 | goto out_flush; |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1919 | |
Wei Yang | 7ca8783 | 2020-04-06 20:04:12 -0700 | [diff] [blame] | 1920 | err = move_pages_and_store_status(mm, current_node, &pagelist, |
| 1921 | status, start, i, nr_pages); |
Wei Yang | 4afdace | 2020-04-06 20:04:09 -0700 | [diff] [blame] | 1922 | if (err) |
| 1923 | goto out; |
Michal Hocko | a49bd4d | 2018-04-10 16:29:59 -0700 | [diff] [blame] | 1924 | current_node = NUMA_NO_NODE; |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1925 | } |
Michal Hocko | a49bd4d | 2018-04-10 16:29:59 -0700 | [diff] [blame] | 1926 | out_flush: |
| 1927 | /* Make sure we do not overwrite the existing error */ |
Wei Yang | 7ca8783 | 2020-04-06 20:04:12 -0700 | [diff] [blame] | 1928 | err1 = move_pages_and_store_status(mm, current_node, &pagelist, |
| 1929 | status, start, i, nr_pages); |
Wei Yang | dfe9aa2 | 2020-01-30 22:11:14 -0800 | [diff] [blame] | 1930 | if (err >= 0) |
Michal Hocko | a49bd4d | 2018-04-10 16:29:59 -0700 | [diff] [blame] | 1931 | err = err1; |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1932 | out: |
Minchan Kim | 361a2a2 | 2021-05-04 18:36:57 -0700 | [diff] [blame] | 1933 | lru_cache_enable(); |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1934 | return err; |
| 1935 | } |
| 1936 | |
| 1937 | /* |
Brice Goglin | 2f007e7 | 2008-10-18 20:27:16 -0700 | [diff] [blame] | 1938 | * Determine the nodes of an array of pages and store it in an array of status. |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1939 | */ |
Brice Goglin | 80bba12 | 2008-12-09 13:14:23 -0800 | [diff] [blame] | 1940 | static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages, |
| 1941 | const void __user **pages, int *status) |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1942 | { |
Brice Goglin | 2f007e7 | 2008-10-18 20:27:16 -0700 | [diff] [blame] | 1943 | unsigned long i; |
Brice Goglin | 2f007e7 | 2008-10-18 20:27:16 -0700 | [diff] [blame] | 1944 | |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1945 | mmap_read_lock(mm); |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1946 | |
Brice Goglin | 2f007e7 | 2008-10-18 20:27:16 -0700 | [diff] [blame] | 1947 | for (i = 0; i < nr_pages; i++) { |
Brice Goglin | 80bba12 | 2008-12-09 13:14:23 -0800 | [diff] [blame] | 1948 | unsigned long addr = (unsigned long)(*pages); |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1949 | struct vm_area_struct *vma; |
| 1950 | struct page *page; |
KOSAKI Motohiro | c095adb | 2008-12-16 16:06:43 +0900 | [diff] [blame] | 1951 | int err = -EFAULT; |
Brice Goglin | 2f007e7 | 2008-10-18 20:27:16 -0700 | [diff] [blame] | 1952 | |
Liam Howlett | 059b8b4 | 2021-06-28 19:39:44 -0700 | [diff] [blame] | 1953 | vma = vma_lookup(mm, addr); |
| 1954 | if (!vma) |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1955 | goto set_status; |
| 1956 | |
Kirill A. Shutemov | d899844 | 2015-09-04 15:47:53 -0700 | [diff] [blame] | 1957 | /* FOLL_DUMP to ignore special (like zero) pages */ |
| 1958 | page = follow_page(vma, addr, FOLL_DUMP); |
Linus Torvalds | 89f5b7d | 2008-06-20 11:18:25 -0700 | [diff] [blame] | 1959 | |
| 1960 | err = PTR_ERR(page); |
| 1961 | if (IS_ERR(page)) |
| 1962 | goto set_status; |
| 1963 | |
Kirill A. Shutemov | d899844 | 2015-09-04 15:47:53 -0700 | [diff] [blame] | 1964 | err = page ? page_to_nid(page) : -ENOENT; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1965 | set_status: |
Brice Goglin | 80bba12 | 2008-12-09 13:14:23 -0800 | [diff] [blame] | 1966 | *status = err; |
| 1967 | |
| 1968 | pages++; |
| 1969 | status++; |
| 1970 | } |
| 1971 | |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1972 | mmap_read_unlock(mm); |
Brice Goglin | 80bba12 | 2008-12-09 13:14:23 -0800 | [diff] [blame] | 1973 | } |
| 1974 | |
Arnd Bergmann | 5b1b561 | 2021-09-08 15:18:17 -0700 | [diff] [blame] | 1975 | static int get_compat_pages_array(const void __user *chunk_pages[], |
| 1976 | const void __user * __user *pages, |
| 1977 | unsigned long chunk_nr) |
| 1978 | { |
| 1979 | compat_uptr_t __user *pages32 = (compat_uptr_t __user *)pages; |
| 1980 | compat_uptr_t p; |
| 1981 | int i; |
| 1982 | |
| 1983 | for (i = 0; i < chunk_nr; i++) { |
| 1984 | if (get_user(p, pages32 + i)) |
| 1985 | return -EFAULT; |
| 1986 | chunk_pages[i] = compat_ptr(p); |
| 1987 | } |
| 1988 | |
| 1989 | return 0; |
| 1990 | } |
| 1991 | |
Brice Goglin | 80bba12 | 2008-12-09 13:14:23 -0800 | [diff] [blame] | 1992 | /* |
| 1993 | * Determine the nodes of a user array of pages and store it in |
| 1994 | * a user array of status. |
| 1995 | */ |
| 1996 | static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages, |
| 1997 | const void __user * __user *pages, |
| 1998 | int __user *status) |
| 1999 | { |
| 2000 | #define DO_PAGES_STAT_CHUNK_NR 16 |
| 2001 | const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR]; |
| 2002 | int chunk_status[DO_PAGES_STAT_CHUNK_NR]; |
Brice Goglin | 80bba12 | 2008-12-09 13:14:23 -0800 | [diff] [blame] | 2003 | |
H. Peter Anvin | 87b8d1a | 2010-02-18 16:13:40 -0800 | [diff] [blame] | 2004 | while (nr_pages) { |
| 2005 | unsigned long chunk_nr; |
Brice Goglin | 80bba12 | 2008-12-09 13:14:23 -0800 | [diff] [blame] | 2006 | |
H. Peter Anvin | 87b8d1a | 2010-02-18 16:13:40 -0800 | [diff] [blame] | 2007 | chunk_nr = nr_pages; |
| 2008 | if (chunk_nr > DO_PAGES_STAT_CHUNK_NR) |
| 2009 | chunk_nr = DO_PAGES_STAT_CHUNK_NR; |
| 2010 | |
Arnd Bergmann | 5b1b561 | 2021-09-08 15:18:17 -0700 | [diff] [blame] | 2011 | if (in_compat_syscall()) { |
| 2012 | if (get_compat_pages_array(chunk_pages, pages, |
| 2013 | chunk_nr)) |
| 2014 | break; |
| 2015 | } else { |
| 2016 | if (copy_from_user(chunk_pages, pages, |
| 2017 | chunk_nr * sizeof(*chunk_pages))) |
| 2018 | break; |
| 2019 | } |
Brice Goglin | 80bba12 | 2008-12-09 13:14:23 -0800 | [diff] [blame] | 2020 | |
| 2021 | do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status); |
| 2022 | |
H. Peter Anvin | 87b8d1a | 2010-02-18 16:13:40 -0800 | [diff] [blame] | 2023 | if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status))) |
| 2024 | break; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 2025 | |
H. Peter Anvin | 87b8d1a | 2010-02-18 16:13:40 -0800 | [diff] [blame] | 2026 | pages += chunk_nr; |
| 2027 | status += chunk_nr; |
| 2028 | nr_pages -= chunk_nr; |
| 2029 | } |
| 2030 | return nr_pages ? -EFAULT : 0; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 2031 | } |
| 2032 | |
Miaohe Lin | 4dc200c | 2020-10-17 16:14:03 -0700 | [diff] [blame] | 2033 | static struct mm_struct *find_mm_struct(pid_t pid, nodemask_t *mem_nodes) |
| 2034 | { |
| 2035 | struct task_struct *task; |
| 2036 | struct mm_struct *mm; |
| 2037 | |
| 2038 | /* |
| 2039 | * There is no need to check if current process has the right to modify |
| 2040 | * the specified process when they are same. |
| 2041 | */ |
| 2042 | if (!pid) { |
| 2043 | mmget(current->mm); |
| 2044 | *mem_nodes = cpuset_mems_allowed(current); |
| 2045 | return current->mm; |
| 2046 | } |
| 2047 | |
| 2048 | /* Find the mm_struct */ |
| 2049 | rcu_read_lock(); |
| 2050 | task = find_task_by_vpid(pid); |
| 2051 | if (!task) { |
| 2052 | rcu_read_unlock(); |
| 2053 | return ERR_PTR(-ESRCH); |
| 2054 | } |
| 2055 | get_task_struct(task); |
| 2056 | |
| 2057 | /* |
| 2058 | * Check if this process has the right to modify the specified |
| 2059 | * process. Use the regular "ptrace_may_access()" checks. |
| 2060 | */ |
| 2061 | if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) { |
| 2062 | rcu_read_unlock(); |
| 2063 | mm = ERR_PTR(-EPERM); |
| 2064 | goto out; |
| 2065 | } |
| 2066 | rcu_read_unlock(); |
| 2067 | |
| 2068 | mm = ERR_PTR(security_task_movememory(task)); |
| 2069 | if (IS_ERR(mm)) |
| 2070 | goto out; |
| 2071 | *mem_nodes = cpuset_mems_allowed(task); |
| 2072 | mm = get_task_mm(task); |
| 2073 | out: |
| 2074 | put_task_struct(task); |
| 2075 | if (!mm) |
| 2076 | mm = ERR_PTR(-EINVAL); |
| 2077 | return mm; |
| 2078 | } |
| 2079 | |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 2080 | /* |
| 2081 | * Move a list of pages in the address space of the currently executing |
| 2082 | * process. |
| 2083 | */ |
Dominik Brodowski | 7addf44 | 2018-03-17 16:08:03 +0100 | [diff] [blame] | 2084 | static int kernel_move_pages(pid_t pid, unsigned long nr_pages, |
| 2085 | const void __user * __user *pages, |
| 2086 | const int __user *nodes, |
| 2087 | int __user *status, int flags) |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 2088 | { |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 2089 | struct mm_struct *mm; |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 2090 | int err; |
Christoph Lameter | 3268c63 | 2012-03-21 16:34:06 -0700 | [diff] [blame] | 2091 | nodemask_t task_nodes; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 2092 | |
| 2093 | /* Check flags */ |
| 2094 | if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL)) |
| 2095 | return -EINVAL; |
| 2096 | |
| 2097 | if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE)) |
| 2098 | return -EPERM; |
| 2099 | |
Miaohe Lin | 4dc200c | 2020-10-17 16:14:03 -0700 | [diff] [blame] | 2100 | mm = find_mm_struct(pid, &task_nodes); |
| 2101 | if (IS_ERR(mm)) |
| 2102 | return PTR_ERR(mm); |
Sasha Levin | 6e8b09e | 2012-04-25 16:01:53 -0700 | [diff] [blame] | 2103 | |
| 2104 | if (nodes) |
| 2105 | err = do_pages_move(mm, task_nodes, nr_pages, pages, |
| 2106 | nodes, status, flags); |
| 2107 | else |
| 2108 | err = do_pages_stat(mm, nr_pages, pages, status); |
Christoph Lameter | 3268c63 | 2012-03-21 16:34:06 -0700 | [diff] [blame] | 2109 | |
| 2110 | mmput(mm); |
| 2111 | return err; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 2112 | } |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 2113 | |
Dominik Brodowski | 7addf44 | 2018-03-17 16:08:03 +0100 | [diff] [blame] | 2114 | SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages, |
| 2115 | const void __user * __user *, pages, |
| 2116 | const int __user *, nodes, |
| 2117 | int __user *, status, int, flags) |
| 2118 | { |
| 2119 | return kernel_move_pages(pid, nr_pages, pages, nodes, status, flags); |
| 2120 | } |
| 2121 | |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 2122 | #ifdef CONFIG_NUMA_BALANCING |
| 2123 | /* |
| 2124 | * Returns true if this is a safe migration target node for misplaced NUMA |
| 2125 | * pages. Currently it only checks the watermarks which crude |
| 2126 | */ |
| 2127 | static bool migrate_balanced_pgdat(struct pglist_data *pgdat, |
Mel Gorman | 3abef4e | 2013-02-22 16:34:27 -0800 | [diff] [blame] | 2128 | unsigned long nr_migrate_pages) |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 2129 | { |
| 2130 | int z; |
Mel Gorman | 599d0c9 | 2016-07-28 15:45:31 -0700 | [diff] [blame] | 2131 | |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 2132 | for (z = pgdat->nr_zones - 1; z >= 0; z--) { |
| 2133 | struct zone *zone = pgdat->node_zones + z; |
| 2134 | |
| 2135 | if (!populated_zone(zone)) |
| 2136 | continue; |
| 2137 | |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 2138 | /* Avoid waking kswapd by allocating pages_to_migrate pages. */ |
| 2139 | if (!zone_watermark_ok(zone, 0, |
| 2140 | high_wmark_pages(zone) + |
| 2141 | nr_migrate_pages, |
Huang Ying | bfe9d00 | 2019-11-30 17:57:28 -0800 | [diff] [blame] | 2142 | ZONE_MOVABLE, 0)) |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 2143 | continue; |
| 2144 | return true; |
| 2145 | } |
| 2146 | return false; |
| 2147 | } |
| 2148 | |
| 2149 | static struct page *alloc_misplaced_dst_page(struct page *page, |
Michal Hocko | 666feb2 | 2018-04-10 16:30:03 -0700 | [diff] [blame] | 2150 | unsigned long data) |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 2151 | { |
| 2152 | int nid = (int) data; |
| 2153 | struct page *newpage; |
| 2154 | |
Vlastimil Babka | 96db800 | 2015-09-08 15:03:50 -0700 | [diff] [blame] | 2155 | newpage = __alloc_pages_node(nid, |
Johannes Weiner | e97ca8e5 | 2014-03-10 15:49:43 -0700 | [diff] [blame] | 2156 | (GFP_HIGHUSER_MOVABLE | |
| 2157 | __GFP_THISNODE | __GFP_NOMEMALLOC | |
| 2158 | __GFP_NORETRY | __GFP_NOWARN) & |
Mel Gorman | 8479eba | 2016-02-26 15:19:31 -0800 | [diff] [blame] | 2159 | ~__GFP_RECLAIM, 0); |
Hillf Danton | bac0382 | 2012-11-27 14:46:24 +0000 | [diff] [blame] | 2160 | |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 2161 | return newpage; |
| 2162 | } |
| 2163 | |
Yang Shi | c5b5a3d | 2021-06-30 18:51:42 -0700 | [diff] [blame] | 2164 | static struct page *alloc_misplaced_dst_page_thp(struct page *page, |
| 2165 | unsigned long data) |
| 2166 | { |
| 2167 | int nid = (int) data; |
| 2168 | struct page *newpage; |
| 2169 | |
| 2170 | newpage = alloc_pages_node(nid, (GFP_TRANSHUGE_LIGHT | __GFP_THISNODE), |
| 2171 | HPAGE_PMD_ORDER); |
| 2172 | if (!newpage) |
| 2173 | goto out; |
| 2174 | |
| 2175 | prep_transhuge_page(newpage); |
| 2176 | |
| 2177 | out: |
| 2178 | return newpage; |
| 2179 | } |
| 2180 | |
Mel Gorman | 1c30e01 | 2014-01-21 15:50:58 -0800 | [diff] [blame] | 2181 | static int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page) |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2182 | { |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 2183 | int page_lru; |
Baolin Wang | 2b9b624 | 2021-09-08 15:18:01 -0700 | [diff] [blame] | 2184 | int nr_pages = thp_nr_pages(page); |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2185 | |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 2186 | VM_BUG_ON_PAGE(compound_order(page) && !PageTransHuge(page), page); |
Mel Gorman | 3abef4e | 2013-02-22 16:34:27 -0800 | [diff] [blame] | 2187 | |
Yang Shi | 662aeea | 2021-06-30 18:51:51 -0700 | [diff] [blame] | 2188 | /* Do not migrate THP mapped by multiple processes */ |
| 2189 | if (PageTransHuge(page) && total_mapcount(page) > 1) |
| 2190 | return 0; |
| 2191 | |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2192 | /* Avoid migrating to a node that is nearly full */ |
Baolin Wang | 2b9b624 | 2021-09-08 15:18:01 -0700 | [diff] [blame] | 2193 | if (!migrate_balanced_pgdat(pgdat, nr_pages)) |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 2194 | return 0; |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2195 | |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 2196 | if (isolate_lru_page(page)) |
| 2197 | return 0; |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2198 | |
Huang Ying | 9de4f22 | 2020-04-06 20:04:41 -0700 | [diff] [blame] | 2199 | page_lru = page_is_file_lru(page); |
Mel Gorman | 599d0c9 | 2016-07-28 15:45:31 -0700 | [diff] [blame] | 2200 | mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + page_lru, |
Baolin Wang | 2b9b624 | 2021-09-08 15:18:01 -0700 | [diff] [blame] | 2201 | nr_pages); |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 2202 | |
| 2203 | /* |
| 2204 | * Isolating the page has taken another reference, so the |
| 2205 | * caller's reference can be safely dropped without the page |
| 2206 | * disappearing underneath us during migration. |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2207 | */ |
| 2208 | put_page(page); |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 2209 | return 1; |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2210 | } |
| 2211 | |
Mel Gorman | a8f6077 | 2012-11-14 21:41:46 +0000 | [diff] [blame] | 2212 | /* |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 2213 | * Attempt to migrate a misplaced page to the specified destination |
| 2214 | * node. Caller is expected to have an elevated reference count on |
| 2215 | * the page that will be dropped by this function before returning. |
| 2216 | */ |
Mel Gorman | 1bc115d | 2013-10-07 11:29:05 +0100 | [diff] [blame] | 2217 | int migrate_misplaced_page(struct page *page, struct vm_area_struct *vma, |
| 2218 | int node) |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 2219 | { |
Mel Gorman | a8f6077 | 2012-11-14 21:41:46 +0000 | [diff] [blame] | 2220 | pg_data_t *pgdat = NODE_DATA(node); |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 2221 | int isolated; |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2222 | int nr_remaining; |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 2223 | LIST_HEAD(migratepages); |
Yang Shi | c5b5a3d | 2021-06-30 18:51:42 -0700 | [diff] [blame] | 2224 | new_page_t *new; |
| 2225 | bool compound; |
Aneesh Kumar K.V | b5916c0 | 2021-07-29 14:53:47 -0700 | [diff] [blame] | 2226 | int nr_pages = thp_nr_pages(page); |
Yang Shi | c5b5a3d | 2021-06-30 18:51:42 -0700 | [diff] [blame] | 2227 | |
| 2228 | /* |
| 2229 | * PTE mapped THP or HugeTLB page can't reach here so the page could |
| 2230 | * be either base page or THP. And it must be head page if it is |
| 2231 | * THP. |
| 2232 | */ |
| 2233 | compound = PageTransHuge(page); |
| 2234 | |
| 2235 | if (compound) |
| 2236 | new = alloc_misplaced_dst_page_thp; |
| 2237 | else |
| 2238 | new = alloc_misplaced_dst_page; |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 2239 | |
| 2240 | /* |
Mel Gorman | 1bc115d | 2013-10-07 11:29:05 +0100 | [diff] [blame] | 2241 | * Don't migrate file pages that are mapped in multiple processes |
| 2242 | * with execute permissions as they are probably shared libraries. |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 2243 | */ |
Miaohe Lin | 7ee820e | 2021-05-04 18:37:16 -0700 | [diff] [blame] | 2244 | if (page_mapcount(page) != 1 && page_is_file_lru(page) && |
| 2245 | (vma->vm_flags & VM_EXEC)) |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 2246 | goto out; |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 2247 | |
Mel Gorman | a8f6077 | 2012-11-14 21:41:46 +0000 | [diff] [blame] | 2248 | /* |
Mel Gorman | 09a913a | 2018-04-10 16:29:20 -0700 | [diff] [blame] | 2249 | * Also do not migrate dirty pages as not all filesystems can move |
| 2250 | * dirty pages in MIGRATE_ASYNC mode which is a waste of cycles. |
| 2251 | */ |
Huang Ying | 9de4f22 | 2020-04-06 20:04:41 -0700 | [diff] [blame] | 2252 | if (page_is_file_lru(page) && PageDirty(page)) |
Mel Gorman | 09a913a | 2018-04-10 16:29:20 -0700 | [diff] [blame] | 2253 | goto out; |
| 2254 | |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2255 | isolated = numamigrate_isolate_page(pgdat, page); |
| 2256 | if (!isolated) |
| 2257 | goto out; |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 2258 | |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2259 | list_add(&page->lru, &migratepages); |
Yang Shi | c5b5a3d | 2021-06-30 18:51:42 -0700 | [diff] [blame] | 2260 | nr_remaining = migrate_pages(&migratepages, *new, NULL, node, |
Yang Shi | 5ac9588 | 2021-09-02 14:59:13 -0700 | [diff] [blame] | 2261 | MIGRATE_ASYNC, MR_NUMA_MISPLACED, NULL); |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2262 | if (nr_remaining) { |
Joonsoo Kim | 59c82b7 | 2014-01-21 15:51:17 -0800 | [diff] [blame] | 2263 | if (!list_empty(&migratepages)) { |
| 2264 | list_del(&page->lru); |
Yang Shi | c5fc5c3 | 2021-06-30 18:51:45 -0700 | [diff] [blame] | 2265 | mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + |
| 2266 | page_is_file_lru(page), -nr_pages); |
Joonsoo Kim | 59c82b7 | 2014-01-21 15:51:17 -0800 | [diff] [blame] | 2267 | putback_lru_page(page); |
| 2268 | } |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2269 | isolated = 0; |
| 2270 | } else |
Yang Shi | c5fc5c3 | 2021-06-30 18:51:45 -0700 | [diff] [blame] | 2271 | count_vm_numa_events(NUMA_PAGE_MIGRATE, nr_pages); |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 2272 | BUG_ON(!list_empty(&migratepages)); |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 2273 | return isolated; |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 2274 | |
| 2275 | out: |
| 2276 | put_page(page); |
| 2277 | return 0; |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 2278 | } |
Mel Gorman | 220018d | 2012-12-05 09:32:56 +0000 | [diff] [blame] | 2279 | #endif /* CONFIG_NUMA_BALANCING */ |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 2280 | #endif /* CONFIG_NUMA */ |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2281 | |
Christoph Hellwig | 9b2ed9c | 2019-08-14 09:59:28 +0200 | [diff] [blame] | 2282 | #ifdef CONFIG_DEVICE_PRIVATE |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 2283 | static int migrate_vma_collect_skip(unsigned long start, |
| 2284 | unsigned long end, |
| 2285 | struct mm_walk *walk) |
| 2286 | { |
| 2287 | struct migrate_vma *migrate = walk->private; |
| 2288 | unsigned long addr; |
| 2289 | |
Ralph Campbell | 872ea70 | 2020-01-30 22:14:38 -0800 | [diff] [blame] | 2290 | for (addr = start; addr < end; addr += PAGE_SIZE) { |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2291 | migrate->dst[migrate->npages] = 0; |
| 2292 | migrate->src[migrate->npages++] = 0; |
| 2293 | } |
| 2294 | |
| 2295 | return 0; |
| 2296 | } |
| 2297 | |
Miaohe Lin | 843e1be | 2021-05-04 18:37:13 -0700 | [diff] [blame] | 2298 | static int migrate_vma_collect_hole(unsigned long start, |
| 2299 | unsigned long end, |
| 2300 | __always_unused int depth, |
| 2301 | struct mm_walk *walk) |
| 2302 | { |
| 2303 | struct migrate_vma *migrate = walk->private; |
| 2304 | unsigned long addr; |
| 2305 | |
| 2306 | /* Only allow populating anonymous memory. */ |
| 2307 | if (!vma_is_anonymous(walk->vma)) |
| 2308 | return migrate_vma_collect_skip(start, end, walk); |
| 2309 | |
| 2310 | for (addr = start; addr < end; addr += PAGE_SIZE) { |
| 2311 | migrate->src[migrate->npages] = MIGRATE_PFN_MIGRATE; |
| 2312 | migrate->dst[migrate->npages] = 0; |
| 2313 | migrate->npages++; |
| 2314 | migrate->cpages++; |
| 2315 | } |
| 2316 | |
| 2317 | return 0; |
| 2318 | } |
| 2319 | |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2320 | static int migrate_vma_collect_pmd(pmd_t *pmdp, |
| 2321 | unsigned long start, |
| 2322 | unsigned long end, |
| 2323 | struct mm_walk *walk) |
| 2324 | { |
| 2325 | struct migrate_vma *migrate = walk->private; |
| 2326 | struct vm_area_struct *vma = walk->vma; |
| 2327 | struct mm_struct *mm = vma->vm_mm; |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame] | 2328 | unsigned long addr = start, unmapped = 0; |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2329 | spinlock_t *ptl; |
| 2330 | pte_t *ptep; |
| 2331 | |
| 2332 | again: |
| 2333 | if (pmd_none(*pmdp)) |
Steven Price | b7a16c7 | 2020-02-03 17:36:03 -0800 | [diff] [blame] | 2334 | return migrate_vma_collect_hole(start, end, -1, walk); |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2335 | |
| 2336 | if (pmd_trans_huge(*pmdp)) { |
| 2337 | struct page *page; |
| 2338 | |
| 2339 | ptl = pmd_lock(mm, pmdp); |
| 2340 | if (unlikely(!pmd_trans_huge(*pmdp))) { |
| 2341 | spin_unlock(ptl); |
| 2342 | goto again; |
| 2343 | } |
| 2344 | |
| 2345 | page = pmd_page(*pmdp); |
| 2346 | if (is_huge_zero_page(page)) { |
| 2347 | spin_unlock(ptl); |
| 2348 | split_huge_pmd(vma, pmdp, addr); |
| 2349 | if (pmd_trans_unstable(pmdp)) |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 2350 | return migrate_vma_collect_skip(start, end, |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2351 | walk); |
| 2352 | } else { |
| 2353 | int ret; |
| 2354 | |
| 2355 | get_page(page); |
| 2356 | spin_unlock(ptl); |
| 2357 | if (unlikely(!trylock_page(page))) |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 2358 | return migrate_vma_collect_skip(start, end, |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2359 | walk); |
| 2360 | ret = split_huge_page(page); |
| 2361 | unlock_page(page); |
| 2362 | put_page(page); |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 2363 | if (ret) |
| 2364 | return migrate_vma_collect_skip(start, end, |
| 2365 | walk); |
| 2366 | if (pmd_none(*pmdp)) |
Steven Price | b7a16c7 | 2020-02-03 17:36:03 -0800 | [diff] [blame] | 2367 | return migrate_vma_collect_hole(start, end, -1, |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2368 | walk); |
| 2369 | } |
| 2370 | } |
| 2371 | |
| 2372 | if (unlikely(pmd_bad(*pmdp))) |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 2373 | return migrate_vma_collect_skip(start, end, walk); |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2374 | |
| 2375 | ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl); |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame] | 2376 | arch_enter_lazy_mmu_mode(); |
| 2377 | |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2378 | for (; addr < end; addr += PAGE_SIZE, ptep++) { |
Christoph Hellwig | 800bb1c | 2020-03-16 20:32:14 +0100 | [diff] [blame] | 2379 | unsigned long mpfn = 0, pfn; |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2380 | struct page *page; |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame] | 2381 | swp_entry_t entry; |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2382 | pte_t pte; |
| 2383 | |
| 2384 | pte = *ptep; |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2385 | |
Jérôme Glisse | a5430dd | 2017-09-08 16:12:17 -0700 | [diff] [blame] | 2386 | if (pte_none(pte)) { |
Ralph Campbell | 0744f28 | 2020-08-11 18:31:41 -0700 | [diff] [blame] | 2387 | if (vma_is_anonymous(vma)) { |
| 2388 | mpfn = MIGRATE_PFN_MIGRATE; |
| 2389 | migrate->cpages++; |
| 2390 | } |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2391 | goto next; |
| 2392 | } |
| 2393 | |
Jérôme Glisse | a5430dd | 2017-09-08 16:12:17 -0700 | [diff] [blame] | 2394 | if (!pte_present(pte)) { |
Jérôme Glisse | a5430dd | 2017-09-08 16:12:17 -0700 | [diff] [blame] | 2395 | /* |
| 2396 | * Only care about unaddressable device page special |
| 2397 | * page table entry. Other special swap entries are not |
| 2398 | * migratable, and we ignore regular swapped page. |
| 2399 | */ |
| 2400 | entry = pte_to_swp_entry(pte); |
| 2401 | if (!is_device_private_entry(entry)) |
| 2402 | goto next; |
| 2403 | |
Alistair Popple | af5cdaf | 2021-06-30 18:54:06 -0700 | [diff] [blame] | 2404 | page = pfn_swap_entry_to_page(entry); |
Ralph Campbell | 5143192 | 2020-07-23 15:30:00 -0700 | [diff] [blame] | 2405 | if (!(migrate->flags & |
| 2406 | MIGRATE_VMA_SELECT_DEVICE_PRIVATE) || |
| 2407 | page->pgmap->owner != migrate->pgmap_owner) |
Christoph Hellwig | 800bb1c | 2020-03-16 20:32:14 +0100 | [diff] [blame] | 2408 | goto next; |
| 2409 | |
Christoph Hellwig | 06d462b | 2019-08-14 09:59:27 +0200 | [diff] [blame] | 2410 | mpfn = migrate_pfn(page_to_pfn(page)) | |
| 2411 | MIGRATE_PFN_MIGRATE; |
Alistair Popple | 4dd845b | 2021-06-30 18:54:09 -0700 | [diff] [blame] | 2412 | if (is_writable_device_private_entry(entry)) |
Jérôme Glisse | a5430dd | 2017-09-08 16:12:17 -0700 | [diff] [blame] | 2413 | mpfn |= MIGRATE_PFN_WRITE; |
| 2414 | } else { |
Ralph Campbell | 5143192 | 2020-07-23 15:30:00 -0700 | [diff] [blame] | 2415 | if (!(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM)) |
Christoph Hellwig | 800bb1c | 2020-03-16 20:32:14 +0100 | [diff] [blame] | 2416 | goto next; |
Pingfan Liu | 276f756 | 2019-09-23 15:37:38 -0700 | [diff] [blame] | 2417 | pfn = pte_pfn(pte); |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 2418 | if (is_zero_pfn(pfn)) { |
| 2419 | mpfn = MIGRATE_PFN_MIGRATE; |
| 2420 | migrate->cpages++; |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 2421 | goto next; |
| 2422 | } |
Christoph Hellwig | 25b2995 | 2019-06-13 22:50:49 +0200 | [diff] [blame] | 2423 | page = vm_normal_page(migrate->vma, addr, pte); |
Jérôme Glisse | a5430dd | 2017-09-08 16:12:17 -0700 | [diff] [blame] | 2424 | mpfn = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE; |
| 2425 | mpfn |= pte_write(pte) ? MIGRATE_PFN_WRITE : 0; |
| 2426 | } |
| 2427 | |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2428 | /* FIXME support THP */ |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2429 | if (!page || !page->mapping || PageTransCompound(page)) { |
Pingfan Liu | 276f756 | 2019-09-23 15:37:38 -0700 | [diff] [blame] | 2430 | mpfn = 0; |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2431 | goto next; |
| 2432 | } |
| 2433 | |
| 2434 | /* |
| 2435 | * By getting a reference on the page we pin it and that blocks |
| 2436 | * any kind of migration. Side effect is that it "freezes" the |
| 2437 | * pte. |
| 2438 | * |
| 2439 | * We drop this reference after isolating the page from the lru |
| 2440 | * for non device page (device page are not on the lru and thus |
| 2441 | * can't be dropped from it). |
| 2442 | */ |
| 2443 | get_page(page); |
| 2444 | migrate->cpages++; |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2445 | |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame] | 2446 | /* |
| 2447 | * Optimize for the common case where page is only mapped once |
| 2448 | * in one process. If we can lock the page, then we can safely |
| 2449 | * set up a special migration page table entry now. |
| 2450 | */ |
| 2451 | if (trylock_page(page)) { |
| 2452 | pte_t swp_pte; |
| 2453 | |
| 2454 | mpfn |= MIGRATE_PFN_LOCKED; |
| 2455 | ptep_get_and_clear(mm, addr, ptep); |
| 2456 | |
| 2457 | /* Setup special migration page table entry */ |
Alistair Popple | 4dd845b | 2021-06-30 18:54:09 -0700 | [diff] [blame] | 2458 | if (mpfn & MIGRATE_PFN_WRITE) |
| 2459 | entry = make_writable_migration_entry( |
| 2460 | page_to_pfn(page)); |
| 2461 | else |
| 2462 | entry = make_readable_migration_entry( |
| 2463 | page_to_pfn(page)); |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame] | 2464 | swp_pte = swp_entry_to_pte(entry); |
Alistair Popple | ad7df76 | 2020-09-04 16:36:01 -0700 | [diff] [blame] | 2465 | if (pte_present(pte)) { |
| 2466 | if (pte_soft_dirty(pte)) |
| 2467 | swp_pte = pte_swp_mksoft_dirty(swp_pte); |
| 2468 | if (pte_uffd_wp(pte)) |
| 2469 | swp_pte = pte_swp_mkuffd_wp(swp_pte); |
| 2470 | } else { |
| 2471 | if (pte_swp_soft_dirty(pte)) |
| 2472 | swp_pte = pte_swp_mksoft_dirty(swp_pte); |
| 2473 | if (pte_swp_uffd_wp(pte)) |
| 2474 | swp_pte = pte_swp_mkuffd_wp(swp_pte); |
| 2475 | } |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame] | 2476 | set_pte_at(mm, addr, ptep, swp_pte); |
| 2477 | |
| 2478 | /* |
| 2479 | * This is like regular unmap: we remove the rmap and |
| 2480 | * drop page refcount. Page won't be freed, as we took |
| 2481 | * a reference just above. |
| 2482 | */ |
| 2483 | page_remove_rmap(page, false); |
| 2484 | put_page(page); |
Jérôme Glisse | a5430dd | 2017-09-08 16:12:17 -0700 | [diff] [blame] | 2485 | |
| 2486 | if (pte_present(pte)) |
| 2487 | unmapped++; |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame] | 2488 | } |
| 2489 | |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2490 | next: |
Jérôme Glisse | a5430dd | 2017-09-08 16:12:17 -0700 | [diff] [blame] | 2491 | migrate->dst[migrate->npages] = 0; |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2492 | migrate->src[migrate->npages++] = mpfn; |
| 2493 | } |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2494 | |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame] | 2495 | /* Only flush the TLB if we actually modified any entries */ |
| 2496 | if (unmapped) |
| 2497 | flush_tlb_range(walk->vma, start, end); |
| 2498 | |
Alistair Popple | 1299c11 | 2022-09-02 10:35:51 +1000 | [diff] [blame] | 2499 | arch_leave_lazy_mmu_mode(); |
| 2500 | pte_unmap_unlock(ptep - 1, ptl); |
| 2501 | |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2502 | return 0; |
| 2503 | } |
| 2504 | |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 2505 | static const struct mm_walk_ops migrate_vma_walk_ops = { |
| 2506 | .pmd_entry = migrate_vma_collect_pmd, |
| 2507 | .pte_hole = migrate_vma_collect_hole, |
| 2508 | }; |
| 2509 | |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2510 | /* |
| 2511 | * migrate_vma_collect() - collect pages over a range of virtual addresses |
| 2512 | * @migrate: migrate struct containing all migration information |
| 2513 | * |
| 2514 | * This will walk the CPU page table. For each virtual address backed by a |
| 2515 | * valid page, it updates the src array and takes a reference on the page, in |
| 2516 | * order to pin the page until we lock it and unmap it. |
| 2517 | */ |
| 2518 | static void migrate_vma_collect(struct migrate_vma *migrate) |
| 2519 | { |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 2520 | struct mmu_notifier_range range; |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2521 | |
Ralph Campbell | 998427b | 2020-07-23 15:30:01 -0700 | [diff] [blame] | 2522 | /* |
| 2523 | * Note that the pgmap_owner is passed to the mmu notifier callback so |
| 2524 | * that the registered device driver can skip invalidating device |
| 2525 | * private page mappings that won't be migrated. |
| 2526 | */ |
Alistair Popple | 6b49bf6 | 2021-06-30 18:54:19 -0700 | [diff] [blame] | 2527 | mmu_notifier_range_init_owner(&range, MMU_NOTIFY_MIGRATE, 0, |
| 2528 | migrate->vma, migrate->vma->vm_mm, migrate->start, migrate->end, |
Ralph Campbell | c1a06df | 2020-08-06 23:17:09 -0700 | [diff] [blame] | 2529 | migrate->pgmap_owner); |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 2530 | mmu_notifier_invalidate_range_start(&range); |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2531 | |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 2532 | walk_page_range(migrate->vma->vm_mm, migrate->start, migrate->end, |
| 2533 | &migrate_vma_walk_ops, migrate); |
| 2534 | |
| 2535 | mmu_notifier_invalidate_range_end(&range); |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2536 | migrate->end = migrate->start + (migrate->npages << PAGE_SHIFT); |
| 2537 | } |
| 2538 | |
| 2539 | /* |
| 2540 | * migrate_vma_check_page() - check if page is pinned or not |
| 2541 | * @page: struct page to check |
| 2542 | * |
| 2543 | * Pinned pages cannot be migrated. This is the same test as in |
| 2544 | * migrate_page_move_mapping(), except that here we allow migration of a |
| 2545 | * ZONE_DEVICE page. |
| 2546 | */ |
| 2547 | static bool migrate_vma_check_page(struct page *page) |
| 2548 | { |
| 2549 | /* |
| 2550 | * One extra ref because caller holds an extra reference, either from |
| 2551 | * isolate_lru_page() for a regular page, or migrate_vma_collect() for |
| 2552 | * a device page. |
| 2553 | */ |
| 2554 | int extra = 1; |
| 2555 | |
| 2556 | /* |
| 2557 | * FIXME support THP (transparent huge page), it is bit more complex to |
| 2558 | * check them than regular pages, because they can be mapped with a pmd |
| 2559 | * or with a pte (split pte mapping). |
| 2560 | */ |
| 2561 | if (PageCompound(page)) |
| 2562 | return false; |
| 2563 | |
Jérôme Glisse | a5430dd | 2017-09-08 16:12:17 -0700 | [diff] [blame] | 2564 | /* Page from ZONE_DEVICE have one extra reference */ |
| 2565 | if (is_zone_device_page(page)) { |
| 2566 | /* |
| 2567 | * Private page can never be pin as they have no valid pte and |
| 2568 | * GUP will fail for those. Yet if there is a pending migration |
| 2569 | * a thread might try to wait on the pte migration entry and |
| 2570 | * will bump the page reference count. Sadly there is no way to |
| 2571 | * differentiate a regular pin from migration wait. Hence to |
| 2572 | * avoid 2 racing thread trying to migrate back to CPU to enter |
Haitao Shi | 8958b24 | 2020-12-15 20:47:26 -0800 | [diff] [blame] | 2573 | * infinite loop (one stopping migration because the other is |
Jérôme Glisse | a5430dd | 2017-09-08 16:12:17 -0700 | [diff] [blame] | 2574 | * waiting on pte migration entry). We always return true here. |
| 2575 | * |
| 2576 | * FIXME proper solution is to rework migration_entry_wait() so |
| 2577 | * it does not need to take a reference on page. |
| 2578 | */ |
Christoph Hellwig | 25b2995 | 2019-06-13 22:50:49 +0200 | [diff] [blame] | 2579 | return is_device_private_page(page); |
Jérôme Glisse | a5430dd | 2017-09-08 16:12:17 -0700 | [diff] [blame] | 2580 | } |
| 2581 | |
Jérôme Glisse | df6ad69 | 2017-09-08 16:12:24 -0700 | [diff] [blame] | 2582 | /* For file back page */ |
| 2583 | if (page_mapping(page)) |
| 2584 | extra += 1 + page_has_private(page); |
| 2585 | |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2586 | if ((page_count(page) - extra) > page_mapcount(page)) |
| 2587 | return false; |
| 2588 | |
| 2589 | return true; |
| 2590 | } |
| 2591 | |
| 2592 | /* |
| 2593 | * migrate_vma_prepare() - lock pages and isolate them from the lru |
| 2594 | * @migrate: migrate struct containing all migration information |
| 2595 | * |
| 2596 | * This locks pages that have been collected by migrate_vma_collect(). Once each |
| 2597 | * page is locked it is isolated from the lru (for non-device pages). Finally, |
| 2598 | * the ref taken by migrate_vma_collect() is dropped, as locked pages cannot be |
| 2599 | * migrated by concurrent kernel threads. |
| 2600 | */ |
| 2601 | static void migrate_vma_prepare(struct migrate_vma *migrate) |
| 2602 | { |
| 2603 | const unsigned long npages = migrate->npages; |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame] | 2604 | const unsigned long start = migrate->start; |
| 2605 | unsigned long addr, i, restore = 0; |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2606 | bool allow_drain = true; |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2607 | |
| 2608 | lru_add_drain(); |
| 2609 | |
| 2610 | for (i = 0; (i < npages) && migrate->cpages; i++) { |
| 2611 | struct page *page = migrate_pfn_to_page(migrate->src[i]); |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame] | 2612 | bool remap = true; |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2613 | |
| 2614 | if (!page) |
| 2615 | continue; |
| 2616 | |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame] | 2617 | if (!(migrate->src[i] & MIGRATE_PFN_LOCKED)) { |
| 2618 | /* |
| 2619 | * Because we are migrating several pages there can be |
| 2620 | * a deadlock between 2 concurrent migration where each |
| 2621 | * are waiting on each other page lock. |
| 2622 | * |
| 2623 | * Make migrate_vma() a best effort thing and backoff |
| 2624 | * for any page we can not lock right away. |
| 2625 | */ |
| 2626 | if (!trylock_page(page)) { |
| 2627 | migrate->src[i] = 0; |
| 2628 | migrate->cpages--; |
| 2629 | put_page(page); |
| 2630 | continue; |
| 2631 | } |
| 2632 | remap = false; |
| 2633 | migrate->src[i] |= MIGRATE_PFN_LOCKED; |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2634 | } |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2635 | |
Jérôme Glisse | a5430dd | 2017-09-08 16:12:17 -0700 | [diff] [blame] | 2636 | /* ZONE_DEVICE pages are not on LRU */ |
| 2637 | if (!is_zone_device_page(page)) { |
| 2638 | if (!PageLRU(page) && allow_drain) { |
| 2639 | /* Drain CPU's pagevec */ |
| 2640 | lru_add_drain_all(); |
| 2641 | allow_drain = false; |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame] | 2642 | } |
Jérôme Glisse | a5430dd | 2017-09-08 16:12:17 -0700 | [diff] [blame] | 2643 | |
| 2644 | if (isolate_lru_page(page)) { |
| 2645 | if (remap) { |
| 2646 | migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; |
| 2647 | migrate->cpages--; |
| 2648 | restore++; |
| 2649 | } else { |
| 2650 | migrate->src[i] = 0; |
| 2651 | unlock_page(page); |
| 2652 | migrate->cpages--; |
| 2653 | put_page(page); |
| 2654 | } |
| 2655 | continue; |
| 2656 | } |
| 2657 | |
| 2658 | /* Drop the reference we took in collect */ |
| 2659 | put_page(page); |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2660 | } |
| 2661 | |
| 2662 | if (!migrate_vma_check_page(page)) { |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame] | 2663 | if (remap) { |
| 2664 | migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; |
| 2665 | migrate->cpages--; |
| 2666 | restore++; |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2667 | |
Jérôme Glisse | a5430dd | 2017-09-08 16:12:17 -0700 | [diff] [blame] | 2668 | if (!is_zone_device_page(page)) { |
| 2669 | get_page(page); |
| 2670 | putback_lru_page(page); |
| 2671 | } |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame] | 2672 | } else { |
| 2673 | migrate->src[i] = 0; |
| 2674 | unlock_page(page); |
| 2675 | migrate->cpages--; |
| 2676 | |
Jérôme Glisse | a5430dd | 2017-09-08 16:12:17 -0700 | [diff] [blame] | 2677 | if (!is_zone_device_page(page)) |
| 2678 | putback_lru_page(page); |
| 2679 | else |
| 2680 | put_page(page); |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame] | 2681 | } |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2682 | } |
| 2683 | } |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame] | 2684 | |
| 2685 | for (i = 0, addr = start; i < npages && restore; i++, addr += PAGE_SIZE) { |
| 2686 | struct page *page = migrate_pfn_to_page(migrate->src[i]); |
| 2687 | |
| 2688 | if (!page || (migrate->src[i] & MIGRATE_PFN_MIGRATE)) |
| 2689 | continue; |
| 2690 | |
| 2691 | remove_migration_pte(page, migrate->vma, addr, page); |
| 2692 | |
| 2693 | migrate->src[i] = 0; |
| 2694 | unlock_page(page); |
| 2695 | put_page(page); |
| 2696 | restore--; |
| 2697 | } |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2698 | } |
| 2699 | |
| 2700 | /* |
| 2701 | * migrate_vma_unmap() - replace page mapping with special migration pte entry |
| 2702 | * @migrate: migrate struct containing all migration information |
| 2703 | * |
| 2704 | * Replace page mapping (CPU page table pte) with a special migration pte entry |
| 2705 | * and check again if it has been pinned. Pinned pages are restored because we |
| 2706 | * cannot migrate them. |
| 2707 | * |
| 2708 | * This is the last step before we call the device driver callback to allocate |
| 2709 | * destination memory and copy contents of original page over to new page. |
| 2710 | */ |
| 2711 | static void migrate_vma_unmap(struct migrate_vma *migrate) |
| 2712 | { |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2713 | const unsigned long npages = migrate->npages; |
| 2714 | const unsigned long start = migrate->start; |
| 2715 | unsigned long addr, i, restore = 0; |
| 2716 | |
| 2717 | for (i = 0; i < npages; i++) { |
| 2718 | struct page *page = migrate_pfn_to_page(migrate->src[i]); |
| 2719 | |
| 2720 | if (!page || !(migrate->src[i] & MIGRATE_PFN_MIGRATE)) |
| 2721 | continue; |
| 2722 | |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame] | 2723 | if (page_mapped(page)) { |
Alistair Popple | a98a2f0 | 2021-06-30 18:54:16 -0700 | [diff] [blame] | 2724 | try_to_migrate(page, 0); |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame] | 2725 | if (page_mapped(page)) |
| 2726 | goto restore; |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2727 | } |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame] | 2728 | |
| 2729 | if (migrate_vma_check_page(page)) |
| 2730 | continue; |
| 2731 | |
| 2732 | restore: |
| 2733 | migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; |
| 2734 | migrate->cpages--; |
| 2735 | restore++; |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2736 | } |
| 2737 | |
| 2738 | for (addr = start, i = 0; i < npages && restore; addr += PAGE_SIZE, i++) { |
| 2739 | struct page *page = migrate_pfn_to_page(migrate->src[i]); |
| 2740 | |
| 2741 | if (!page || (migrate->src[i] & MIGRATE_PFN_MIGRATE)) |
| 2742 | continue; |
| 2743 | |
| 2744 | remove_migration_ptes(page, page, false); |
| 2745 | |
| 2746 | migrate->src[i] = 0; |
| 2747 | unlock_page(page); |
| 2748 | restore--; |
| 2749 | |
Jérôme Glisse | a5430dd | 2017-09-08 16:12:17 -0700 | [diff] [blame] | 2750 | if (is_zone_device_page(page)) |
| 2751 | put_page(page); |
| 2752 | else |
| 2753 | putback_lru_page(page); |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2754 | } |
| 2755 | } |
| 2756 | |
Christoph Hellwig | a7d1f22 | 2019-08-14 09:59:19 +0200 | [diff] [blame] | 2757 | /** |
| 2758 | * migrate_vma_setup() - prepare to migrate a range of memory |
Randy Dunlap | eaf444d | 2020-08-11 18:33:08 -0700 | [diff] [blame] | 2759 | * @args: contains the vma, start, and pfns arrays for the migration |
Christoph Hellwig | a7d1f22 | 2019-08-14 09:59:19 +0200 | [diff] [blame] | 2760 | * |
| 2761 | * Returns: negative errno on failures, 0 when 0 or more pages were migrated |
| 2762 | * without an error. |
| 2763 | * |
| 2764 | * Prepare to migrate a range of memory virtual address range by collecting all |
| 2765 | * the pages backing each virtual address in the range, saving them inside the |
| 2766 | * src array. Then lock those pages and unmap them. Once the pages are locked |
| 2767 | * and unmapped, check whether each page is pinned or not. Pages that aren't |
| 2768 | * pinned have the MIGRATE_PFN_MIGRATE flag set (by this function) in the |
| 2769 | * corresponding src array entry. Then restores any pages that are pinned, by |
| 2770 | * remapping and unlocking those pages. |
| 2771 | * |
| 2772 | * The caller should then allocate destination memory and copy source memory to |
| 2773 | * it for all those entries (ie with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE |
| 2774 | * flag set). Once these are allocated and copied, the caller must update each |
| 2775 | * corresponding entry in the dst array with the pfn value of the destination |
| 2776 | * page and with the MIGRATE_PFN_VALID and MIGRATE_PFN_LOCKED flags set |
| 2777 | * (destination pages must have their struct pages locked, via lock_page()). |
| 2778 | * |
| 2779 | * Note that the caller does not have to migrate all the pages that are marked |
| 2780 | * with MIGRATE_PFN_MIGRATE flag in src array unless this is a migration from |
| 2781 | * device memory to system memory. If the caller cannot migrate a device page |
| 2782 | * back to system memory, then it must return VM_FAULT_SIGBUS, which has severe |
| 2783 | * consequences for the userspace process, so it must be avoided if at all |
| 2784 | * possible. |
| 2785 | * |
| 2786 | * For empty entries inside CPU page table (pte_none() or pmd_none() is true) we |
| 2787 | * do set MIGRATE_PFN_MIGRATE flag inside the corresponding source array thus |
Ingo Molnar | f0953a1 | 2021-05-06 18:06:47 -0700 | [diff] [blame] | 2788 | * allowing the caller to allocate device memory for those unbacked virtual |
| 2789 | * addresses. For this the caller simply has to allocate device memory and |
Christoph Hellwig | a7d1f22 | 2019-08-14 09:59:19 +0200 | [diff] [blame] | 2790 | * properly set the destination entry like for regular migration. Note that |
Ingo Molnar | f0953a1 | 2021-05-06 18:06:47 -0700 | [diff] [blame] | 2791 | * this can still fail, and thus inside the device driver you must check if the |
| 2792 | * migration was successful for those entries after calling migrate_vma_pages(), |
Christoph Hellwig | a7d1f22 | 2019-08-14 09:59:19 +0200 | [diff] [blame] | 2793 | * just like for regular migration. |
| 2794 | * |
| 2795 | * After that, the callers must call migrate_vma_pages() to go over each entry |
| 2796 | * in the src array that has the MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag |
| 2797 | * set. If the corresponding entry in dst array has MIGRATE_PFN_VALID flag set, |
| 2798 | * then migrate_vma_pages() to migrate struct page information from the source |
| 2799 | * struct page to the destination struct page. If it fails to migrate the |
| 2800 | * struct page information, then it clears the MIGRATE_PFN_MIGRATE flag in the |
| 2801 | * src array. |
| 2802 | * |
| 2803 | * At this point all successfully migrated pages have an entry in the src |
| 2804 | * array with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag set and the dst |
| 2805 | * array entry with MIGRATE_PFN_VALID flag set. |
| 2806 | * |
| 2807 | * Once migrate_vma_pages() returns the caller may inspect which pages were |
| 2808 | * successfully migrated, and which were not. Successfully migrated pages will |
| 2809 | * have the MIGRATE_PFN_MIGRATE flag set for their src array entry. |
| 2810 | * |
| 2811 | * It is safe to update device page table after migrate_vma_pages() because |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 2812 | * both destination and source page are still locked, and the mmap_lock is held |
Christoph Hellwig | a7d1f22 | 2019-08-14 09:59:19 +0200 | [diff] [blame] | 2813 | * in read mode (hence no one can unmap the range being migrated). |
| 2814 | * |
| 2815 | * Once the caller is done cleaning up things and updating its page table (if it |
| 2816 | * chose to do so, this is not an obligation) it finally calls |
| 2817 | * migrate_vma_finalize() to update the CPU page table to point to new pages |
| 2818 | * for successfully migrated pages or otherwise restore the CPU page table to |
| 2819 | * point to the original source pages. |
| 2820 | */ |
| 2821 | int migrate_vma_setup(struct migrate_vma *args) |
| 2822 | { |
| 2823 | long nr_pages = (args->end - args->start) >> PAGE_SHIFT; |
| 2824 | |
| 2825 | args->start &= PAGE_MASK; |
| 2826 | args->end &= PAGE_MASK; |
| 2827 | if (!args->vma || is_vm_hugetlb_page(args->vma) || |
| 2828 | (args->vma->vm_flags & VM_SPECIAL) || vma_is_dax(args->vma)) |
| 2829 | return -EINVAL; |
| 2830 | if (nr_pages <= 0) |
| 2831 | return -EINVAL; |
| 2832 | if (args->start < args->vma->vm_start || |
| 2833 | args->start >= args->vma->vm_end) |
| 2834 | return -EINVAL; |
| 2835 | if (args->end <= args->vma->vm_start || args->end > args->vma->vm_end) |
| 2836 | return -EINVAL; |
| 2837 | if (!args->src || !args->dst) |
| 2838 | return -EINVAL; |
| 2839 | |
| 2840 | memset(args->src, 0, sizeof(*args->src) * nr_pages); |
| 2841 | args->cpages = 0; |
| 2842 | args->npages = 0; |
| 2843 | |
| 2844 | migrate_vma_collect(args); |
| 2845 | |
| 2846 | if (args->cpages) |
| 2847 | migrate_vma_prepare(args); |
| 2848 | if (args->cpages) |
| 2849 | migrate_vma_unmap(args); |
| 2850 | |
| 2851 | /* |
| 2852 | * At this point pages are locked and unmapped, and thus they have |
| 2853 | * stable content and can safely be copied to destination memory that |
| 2854 | * is allocated by the drivers. |
| 2855 | */ |
| 2856 | return 0; |
| 2857 | |
| 2858 | } |
| 2859 | EXPORT_SYMBOL(migrate_vma_setup); |
| 2860 | |
Ralph Campbell | 34290e2 | 2020-01-30 22:14:44 -0800 | [diff] [blame] | 2861 | /* |
| 2862 | * This code closely matches the code in: |
| 2863 | * __handle_mm_fault() |
| 2864 | * handle_pte_fault() |
| 2865 | * do_anonymous_page() |
| 2866 | * to map in an anonymous zero page but the struct page will be a ZONE_DEVICE |
| 2867 | * private page. |
| 2868 | */ |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 2869 | static void migrate_vma_insert_page(struct migrate_vma *migrate, |
| 2870 | unsigned long addr, |
| 2871 | struct page *page, |
Stephen Zhang | d85c6db | 2020-12-14 19:13:20 -0800 | [diff] [blame] | 2872 | unsigned long *src) |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 2873 | { |
| 2874 | struct vm_area_struct *vma = migrate->vma; |
| 2875 | struct mm_struct *mm = vma->vm_mm; |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 2876 | bool flush = false; |
| 2877 | spinlock_t *ptl; |
| 2878 | pte_t entry; |
| 2879 | pgd_t *pgdp; |
| 2880 | p4d_t *p4dp; |
| 2881 | pud_t *pudp; |
| 2882 | pmd_t *pmdp; |
| 2883 | pte_t *ptep; |
| 2884 | |
| 2885 | /* Only allow populating anonymous memory */ |
| 2886 | if (!vma_is_anonymous(vma)) |
| 2887 | goto abort; |
| 2888 | |
| 2889 | pgdp = pgd_offset(mm, addr); |
| 2890 | p4dp = p4d_alloc(mm, pgdp, addr); |
| 2891 | if (!p4dp) |
| 2892 | goto abort; |
| 2893 | pudp = pud_alloc(mm, p4dp, addr); |
| 2894 | if (!pudp) |
| 2895 | goto abort; |
| 2896 | pmdp = pmd_alloc(mm, pudp, addr); |
| 2897 | if (!pmdp) |
| 2898 | goto abort; |
| 2899 | |
| 2900 | if (pmd_trans_huge(*pmdp) || pmd_devmap(*pmdp)) |
| 2901 | goto abort; |
| 2902 | |
| 2903 | /* |
| 2904 | * Use pte_alloc() instead of pte_alloc_map(). We can't run |
| 2905 | * pte_offset_map() on pmds where a huge pmd might be created |
| 2906 | * from a different thread. |
| 2907 | * |
Michel Lespinasse | 3e4e28c | 2020-06-08 21:33:51 -0700 | [diff] [blame] | 2908 | * pte_alloc_map() is safe to use under mmap_write_lock(mm) or when |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 2909 | * parallel threads are excluded by other means. |
| 2910 | * |
Michel Lespinasse | 3e4e28c | 2020-06-08 21:33:51 -0700 | [diff] [blame] | 2911 | * Here we only have mmap_read_lock(mm). |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 2912 | */ |
Joel Fernandes (Google) | 4cf5892 | 2019-01-03 15:28:34 -0800 | [diff] [blame] | 2913 | if (pte_alloc(mm, pmdp)) |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 2914 | goto abort; |
| 2915 | |
| 2916 | /* See the comment in pte_alloc_one_map() */ |
| 2917 | if (unlikely(pmd_trans_unstable(pmdp))) |
| 2918 | goto abort; |
| 2919 | |
| 2920 | if (unlikely(anon_vma_prepare(vma))) |
| 2921 | goto abort; |
Johannes Weiner | d9eb1ea | 2020-06-03 16:02:24 -0700 | [diff] [blame] | 2922 | if (mem_cgroup_charge(page, vma->vm_mm, GFP_KERNEL)) |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 2923 | goto abort; |
| 2924 | |
| 2925 | /* |
| 2926 | * The memory barrier inside __SetPageUptodate makes sure that |
| 2927 | * preceding stores to the page contents become visible before |
| 2928 | * the set_pte_at() write. |
| 2929 | */ |
| 2930 | __SetPageUptodate(page); |
| 2931 | |
Jérôme Glisse | df6ad69 | 2017-09-08 16:12:24 -0700 | [diff] [blame] | 2932 | if (is_zone_device_page(page)) { |
| 2933 | if (is_device_private_page(page)) { |
| 2934 | swp_entry_t swp_entry; |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 2935 | |
Alistair Popple | 4dd845b | 2021-06-30 18:54:09 -0700 | [diff] [blame] | 2936 | if (vma->vm_flags & VM_WRITE) |
| 2937 | swp_entry = make_writable_device_private_entry( |
| 2938 | page_to_pfn(page)); |
| 2939 | else |
| 2940 | swp_entry = make_readable_device_private_entry( |
| 2941 | page_to_pfn(page)); |
Jérôme Glisse | df6ad69 | 2017-09-08 16:12:24 -0700 | [diff] [blame] | 2942 | entry = swp_entry_to_pte(swp_entry); |
Miaohe Lin | 34f5e9b | 2021-05-04 18:37:10 -0700 | [diff] [blame] | 2943 | } else { |
| 2944 | /* |
| 2945 | * For now we only support migrating to un-addressable |
| 2946 | * device memory. |
| 2947 | */ |
| 2948 | pr_warn_once("Unsupported ZONE_DEVICE page type.\n"); |
| 2949 | goto abort; |
Jérôme Glisse | df6ad69 | 2017-09-08 16:12:24 -0700 | [diff] [blame] | 2950 | } |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 2951 | } else { |
| 2952 | entry = mk_pte(page, vma->vm_page_prot); |
| 2953 | if (vma->vm_flags & VM_WRITE) |
| 2954 | entry = pte_mkwrite(pte_mkdirty(entry)); |
| 2955 | } |
| 2956 | |
| 2957 | ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl); |
| 2958 | |
Ralph Campbell | 34290e2 | 2020-01-30 22:14:44 -0800 | [diff] [blame] | 2959 | if (check_stable_address_space(mm)) |
| 2960 | goto unlock_abort; |
| 2961 | |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 2962 | if (pte_present(*ptep)) { |
| 2963 | unsigned long pfn = pte_pfn(*ptep); |
| 2964 | |
Ralph Campbell | c23a0c9 | 2020-01-30 22:14:41 -0800 | [diff] [blame] | 2965 | if (!is_zero_pfn(pfn)) |
| 2966 | goto unlock_abort; |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 2967 | flush = true; |
Ralph Campbell | c23a0c9 | 2020-01-30 22:14:41 -0800 | [diff] [blame] | 2968 | } else if (!pte_none(*ptep)) |
| 2969 | goto unlock_abort; |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 2970 | |
| 2971 | /* |
Ralph Campbell | c23a0c9 | 2020-01-30 22:14:41 -0800 | [diff] [blame] | 2972 | * Check for userfaultfd but do not deliver the fault. Instead, |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 2973 | * just back off. |
| 2974 | */ |
Ralph Campbell | c23a0c9 | 2020-01-30 22:14:41 -0800 | [diff] [blame] | 2975 | if (userfaultfd_missing(vma)) |
| 2976 | goto unlock_abort; |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 2977 | |
| 2978 | inc_mm_counter(mm, MM_ANONPAGES); |
Johannes Weiner | be5d0a7 | 2020-06-03 16:01:57 -0700 | [diff] [blame] | 2979 | page_add_new_anon_rmap(page, vma, addr, false); |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 2980 | if (!is_zone_device_page(page)) |
Joonsoo Kim | b518154 | 2020-08-11 18:30:40 -0700 | [diff] [blame] | 2981 | lru_cache_add_inactive_or_unevictable(page, vma); |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 2982 | get_page(page); |
| 2983 | |
| 2984 | if (flush) { |
| 2985 | flush_cache_page(vma, addr, pte_pfn(*ptep)); |
| 2986 | ptep_clear_flush_notify(vma, addr, ptep); |
| 2987 | set_pte_at_notify(mm, addr, ptep, entry); |
| 2988 | update_mmu_cache(vma, addr, ptep); |
| 2989 | } else { |
| 2990 | /* No need to invalidate - it was non-present before */ |
| 2991 | set_pte_at(mm, addr, ptep, entry); |
| 2992 | update_mmu_cache(vma, addr, ptep); |
| 2993 | } |
| 2994 | |
| 2995 | pte_unmap_unlock(ptep, ptl); |
| 2996 | *src = MIGRATE_PFN_MIGRATE; |
| 2997 | return; |
| 2998 | |
Ralph Campbell | c23a0c9 | 2020-01-30 22:14:41 -0800 | [diff] [blame] | 2999 | unlock_abort: |
| 3000 | pte_unmap_unlock(ptep, ptl); |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 3001 | abort: |
| 3002 | *src &= ~MIGRATE_PFN_MIGRATE; |
| 3003 | } |
| 3004 | |
Christoph Hellwig | a7d1f22 | 2019-08-14 09:59:19 +0200 | [diff] [blame] | 3005 | /** |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 3006 | * migrate_vma_pages() - migrate meta-data from src page to dst page |
| 3007 | * @migrate: migrate struct containing all migration information |
| 3008 | * |
| 3009 | * This migrates struct page meta-data from source struct page to destination |
| 3010 | * struct page. This effectively finishes the migration from source page to the |
| 3011 | * destination page. |
| 3012 | */ |
Christoph Hellwig | a7d1f22 | 2019-08-14 09:59:19 +0200 | [diff] [blame] | 3013 | void migrate_vma_pages(struct migrate_vma *migrate) |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 3014 | { |
| 3015 | const unsigned long npages = migrate->npages; |
| 3016 | const unsigned long start = migrate->start; |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 3017 | struct mmu_notifier_range range; |
| 3018 | unsigned long addr, i; |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 3019 | bool notified = false; |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 3020 | |
| 3021 | for (i = 0, addr = start; i < npages; addr += PAGE_SIZE, i++) { |
| 3022 | struct page *newpage = migrate_pfn_to_page(migrate->dst[i]); |
| 3023 | struct page *page = migrate_pfn_to_page(migrate->src[i]); |
| 3024 | struct address_space *mapping; |
| 3025 | int r; |
| 3026 | |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 3027 | if (!newpage) { |
| 3028 | migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 3029 | continue; |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 3030 | } |
| 3031 | |
| 3032 | if (!page) { |
Ralph Campbell | c23a0c9 | 2020-01-30 22:14:41 -0800 | [diff] [blame] | 3033 | if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE)) |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 3034 | continue; |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 3035 | if (!notified) { |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 3036 | notified = true; |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 3037 | |
Alistair Popple | 6b49bf6 | 2021-06-30 18:54:19 -0700 | [diff] [blame] | 3038 | mmu_notifier_range_init_owner(&range, |
| 3039 | MMU_NOTIFY_MIGRATE, 0, migrate->vma, |
| 3040 | migrate->vma->vm_mm, addr, migrate->end, |
Ralph Campbell | 5e5dda8 | 2020-12-14 19:12:55 -0800 | [diff] [blame] | 3041 | migrate->pgmap_owner); |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 3042 | mmu_notifier_invalidate_range_start(&range); |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 3043 | } |
| 3044 | migrate_vma_insert_page(migrate, addr, newpage, |
Stephen Zhang | d85c6db | 2020-12-14 19:13:20 -0800 | [diff] [blame] | 3045 | &migrate->src[i]); |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 3046 | continue; |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 3047 | } |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 3048 | |
| 3049 | mapping = page_mapping(page); |
| 3050 | |
Jérôme Glisse | a5430dd | 2017-09-08 16:12:17 -0700 | [diff] [blame] | 3051 | if (is_zone_device_page(newpage)) { |
| 3052 | if (is_device_private_page(newpage)) { |
| 3053 | /* |
| 3054 | * For now only support private anonymous when |
| 3055 | * migrating to un-addressable device memory. |
| 3056 | */ |
| 3057 | if (mapping) { |
| 3058 | migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; |
| 3059 | continue; |
| 3060 | } |
Christoph Hellwig | 25b2995 | 2019-06-13 22:50:49 +0200 | [diff] [blame] | 3061 | } else { |
Jérôme Glisse | a5430dd | 2017-09-08 16:12:17 -0700 | [diff] [blame] | 3062 | /* |
| 3063 | * Other types of ZONE_DEVICE page are not |
| 3064 | * supported. |
| 3065 | */ |
| 3066 | migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; |
| 3067 | continue; |
| 3068 | } |
| 3069 | } |
| 3070 | |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 3071 | r = migrate_page(mapping, newpage, page, MIGRATE_SYNC_NO_COPY); |
| 3072 | if (r != MIGRATEPAGE_SUCCESS) |
| 3073 | migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; |
| 3074 | } |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 3075 | |
Jérôme Glisse | 4645b9f | 2017-11-15 17:34:11 -0800 | [diff] [blame] | 3076 | /* |
| 3077 | * No need to double call mmu_notifier->invalidate_range() callback as |
| 3078 | * the above ptep_clear_flush_notify() inside migrate_vma_insert_page() |
| 3079 | * did already call it. |
| 3080 | */ |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 3081 | if (notified) |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 3082 | mmu_notifier_invalidate_range_only_end(&range); |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 3083 | } |
Christoph Hellwig | a7d1f22 | 2019-08-14 09:59:19 +0200 | [diff] [blame] | 3084 | EXPORT_SYMBOL(migrate_vma_pages); |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 3085 | |
Christoph Hellwig | a7d1f22 | 2019-08-14 09:59:19 +0200 | [diff] [blame] | 3086 | /** |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 3087 | * migrate_vma_finalize() - restore CPU page table entry |
| 3088 | * @migrate: migrate struct containing all migration information |
| 3089 | * |
| 3090 | * This replaces the special migration pte entry with either a mapping to the |
| 3091 | * new page if migration was successful for that page, or to the original page |
| 3092 | * otherwise. |
| 3093 | * |
| 3094 | * This also unlocks the pages and puts them back on the lru, or drops the extra |
| 3095 | * refcount, for device pages. |
| 3096 | */ |
Christoph Hellwig | a7d1f22 | 2019-08-14 09:59:19 +0200 | [diff] [blame] | 3097 | void migrate_vma_finalize(struct migrate_vma *migrate) |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 3098 | { |
| 3099 | const unsigned long npages = migrate->npages; |
| 3100 | unsigned long i; |
| 3101 | |
| 3102 | for (i = 0; i < npages; i++) { |
| 3103 | struct page *newpage = migrate_pfn_to_page(migrate->dst[i]); |
| 3104 | struct page *page = migrate_pfn_to_page(migrate->src[i]); |
| 3105 | |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 3106 | if (!page) { |
| 3107 | if (newpage) { |
| 3108 | unlock_page(newpage); |
| 3109 | put_page(newpage); |
| 3110 | } |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 3111 | continue; |
Jérôme Glisse | 8315ada | 2017-09-08 16:12:21 -0700 | [diff] [blame] | 3112 | } |
| 3113 | |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 3114 | if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE) || !newpage) { |
| 3115 | if (newpage) { |
| 3116 | unlock_page(newpage); |
| 3117 | put_page(newpage); |
| 3118 | } |
| 3119 | newpage = page; |
| 3120 | } |
| 3121 | |
| 3122 | remove_migration_ptes(page, newpage, false); |
| 3123 | unlock_page(page); |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 3124 | |
Jérôme Glisse | a5430dd | 2017-09-08 16:12:17 -0700 | [diff] [blame] | 3125 | if (is_zone_device_page(page)) |
| 3126 | put_page(page); |
| 3127 | else |
| 3128 | putback_lru_page(page); |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 3129 | |
| 3130 | if (newpage != page) { |
| 3131 | unlock_page(newpage); |
Jérôme Glisse | a5430dd | 2017-09-08 16:12:17 -0700 | [diff] [blame] | 3132 | if (is_zone_device_page(newpage)) |
| 3133 | put_page(newpage); |
| 3134 | else |
| 3135 | putback_lru_page(newpage); |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 3136 | } |
| 3137 | } |
| 3138 | } |
Christoph Hellwig | a7d1f22 | 2019-08-14 09:59:19 +0200 | [diff] [blame] | 3139 | EXPORT_SYMBOL(migrate_vma_finalize); |
Christoph Hellwig | 9b2ed9c | 2019-08-14 09:59:28 +0200 | [diff] [blame] | 3140 | #endif /* CONFIG_DEVICE_PRIVATE */ |
Dave Hansen | 79c28a4 | 2021-09-02 14:59:06 -0700 | [diff] [blame] | 3141 | |
Dave Hansen | 76af6a0 | 2021-10-18 15:15:32 -0700 | [diff] [blame] | 3142 | #if defined(CONFIG_HOTPLUG_CPU) |
Dave Hansen | 79c28a4 | 2021-09-02 14:59:06 -0700 | [diff] [blame] | 3143 | /* Disable reclaim-based migration. */ |
| 3144 | static void __disable_all_migrate_targets(void) |
| 3145 | { |
| 3146 | int node; |
| 3147 | |
| 3148 | for_each_online_node(node) |
| 3149 | node_demotion[node] = NUMA_NO_NODE; |
| 3150 | } |
| 3151 | |
| 3152 | static void disable_all_migrate_targets(void) |
| 3153 | { |
| 3154 | __disable_all_migrate_targets(); |
| 3155 | |
| 3156 | /* |
| 3157 | * Ensure that the "disable" is visible across the system. |
| 3158 | * Readers will see either a combination of before+disable |
| 3159 | * state or disable+after. They will never see before and |
| 3160 | * after state together. |
| 3161 | * |
| 3162 | * The before+after state together might have cycles and |
| 3163 | * could cause readers to do things like loop until this |
| 3164 | * function finishes. This ensures they can only see a |
| 3165 | * single "bad" read and would, for instance, only loop |
| 3166 | * once. |
| 3167 | */ |
| 3168 | synchronize_rcu(); |
| 3169 | } |
| 3170 | |
| 3171 | /* |
| 3172 | * Find an automatic demotion target for 'node'. |
| 3173 | * Failing here is OK. It might just indicate |
| 3174 | * being at the end of a chain. |
| 3175 | */ |
| 3176 | static int establish_migrate_target(int node, nodemask_t *used) |
| 3177 | { |
| 3178 | int migration_target; |
| 3179 | |
| 3180 | /* |
| 3181 | * Can not set a migration target on a |
| 3182 | * node with it already set. |
| 3183 | * |
| 3184 | * No need for READ_ONCE() here since this |
| 3185 | * in the write path for node_demotion[]. |
| 3186 | * This should be the only thread writing. |
| 3187 | */ |
| 3188 | if (node_demotion[node] != NUMA_NO_NODE) |
| 3189 | return NUMA_NO_NODE; |
| 3190 | |
| 3191 | migration_target = find_next_best_node(node, used); |
| 3192 | if (migration_target == NUMA_NO_NODE) |
| 3193 | return NUMA_NO_NODE; |
| 3194 | |
| 3195 | node_demotion[node] = migration_target; |
| 3196 | |
| 3197 | return migration_target; |
| 3198 | } |
| 3199 | |
| 3200 | /* |
| 3201 | * When memory fills up on a node, memory contents can be |
| 3202 | * automatically migrated to another node instead of |
| 3203 | * discarded at reclaim. |
| 3204 | * |
| 3205 | * Establish a "migration path" which will start at nodes |
| 3206 | * with CPUs and will follow the priorities used to build the |
| 3207 | * page allocator zonelists. |
| 3208 | * |
| 3209 | * The difference here is that cycles must be avoided. If |
| 3210 | * node0 migrates to node1, then neither node1, nor anything |
| 3211 | * node1 migrates to can migrate to node0. |
| 3212 | * |
| 3213 | * This function can run simultaneously with readers of |
| 3214 | * node_demotion[]. However, it can not run simultaneously |
| 3215 | * with itself. Exclusion is provided by memory hotplug events |
| 3216 | * being single-threaded. |
| 3217 | */ |
| 3218 | static void __set_migration_target_nodes(void) |
| 3219 | { |
| 3220 | nodemask_t next_pass = NODE_MASK_NONE; |
| 3221 | nodemask_t this_pass = NODE_MASK_NONE; |
| 3222 | nodemask_t used_targets = NODE_MASK_NONE; |
| 3223 | int node; |
| 3224 | |
| 3225 | /* |
| 3226 | * Avoid any oddities like cycles that could occur |
| 3227 | * from changes in the topology. This will leave |
| 3228 | * a momentary gap when migration is disabled. |
| 3229 | */ |
| 3230 | disable_all_migrate_targets(); |
| 3231 | |
| 3232 | /* |
| 3233 | * Allocations go close to CPUs, first. Assume that |
| 3234 | * the migration path starts at the nodes with CPUs. |
| 3235 | */ |
| 3236 | next_pass = node_states[N_CPU]; |
| 3237 | again: |
| 3238 | this_pass = next_pass; |
| 3239 | next_pass = NODE_MASK_NONE; |
| 3240 | /* |
| 3241 | * To avoid cycles in the migration "graph", ensure |
| 3242 | * that migration sources are not future targets by |
| 3243 | * setting them in 'used_targets'. Do this only |
| 3244 | * once per pass so that multiple source nodes can |
| 3245 | * share a target node. |
| 3246 | * |
| 3247 | * 'used_targets' will become unavailable in future |
| 3248 | * passes. This limits some opportunities for |
| 3249 | * multiple source nodes to share a destination. |
| 3250 | */ |
| 3251 | nodes_or(used_targets, used_targets, this_pass); |
| 3252 | for_each_node_mask(node, this_pass) { |
| 3253 | int target_node = establish_migrate_target(node, &used_targets); |
| 3254 | |
| 3255 | if (target_node == NUMA_NO_NODE) |
| 3256 | continue; |
| 3257 | |
| 3258 | /* |
| 3259 | * Visit targets from this pass in the next pass. |
| 3260 | * Eventually, every node will have been part of |
| 3261 | * a pass, and will become set in 'used_targets'. |
| 3262 | */ |
| 3263 | node_set(target_node, next_pass); |
| 3264 | } |
| 3265 | /* |
| 3266 | * 'next_pass' contains nodes which became migration |
| 3267 | * targets in this pass. Make additional passes until |
| 3268 | * no more migrations targets are available. |
| 3269 | */ |
| 3270 | if (!nodes_empty(next_pass)) |
| 3271 | goto again; |
| 3272 | } |
| 3273 | |
| 3274 | /* |
| 3275 | * For callers that do not hold get_online_mems() already. |
| 3276 | */ |
Dave Hansen | 79c28a4 | 2021-09-02 14:59:06 -0700 | [diff] [blame] | 3277 | static void set_migration_target_nodes(void) |
| 3278 | { |
| 3279 | get_online_mems(); |
| 3280 | __set_migration_target_nodes(); |
| 3281 | put_online_mems(); |
| 3282 | } |
Dave Hansen | 884a6e5 | 2021-09-02 14:59:09 -0700 | [diff] [blame] | 3283 | |
| 3284 | /* |
Dave Hansen | 884a6e5 | 2021-09-02 14:59:09 -0700 | [diff] [blame] | 3285 | * This leaves migrate-on-reclaim transiently disabled between |
| 3286 | * the MEM_GOING_OFFLINE and MEM_OFFLINE events. This runs |
| 3287 | * whether reclaim-based migration is enabled or not, which |
| 3288 | * ensures that the user can turn reclaim-based migration at |
| 3289 | * any time without needing to recalculate migration targets. |
| 3290 | * |
| 3291 | * These callbacks already hold get_online_mems(). That is why |
| 3292 | * __set_migration_target_nodes() can be used as opposed to |
| 3293 | * set_migration_target_nodes(). |
| 3294 | */ |
| 3295 | static int __meminit migrate_on_reclaim_callback(struct notifier_block *self, |
Dave Hansen | 295be91 | 2021-10-18 15:15:29 -0700 | [diff] [blame] | 3296 | unsigned long action, void *_arg) |
Dave Hansen | 884a6e5 | 2021-09-02 14:59:09 -0700 | [diff] [blame] | 3297 | { |
Dave Hansen | 295be91 | 2021-10-18 15:15:29 -0700 | [diff] [blame] | 3298 | struct memory_notify *arg = _arg; |
| 3299 | |
| 3300 | /* |
| 3301 | * Only update the node migration order when a node is |
| 3302 | * changing status, like online->offline. This avoids |
| 3303 | * the overhead of synchronize_rcu() in most cases. |
| 3304 | */ |
| 3305 | if (arg->status_change_nid < 0) |
| 3306 | return notifier_from_errno(0); |
| 3307 | |
Dave Hansen | 884a6e5 | 2021-09-02 14:59:09 -0700 | [diff] [blame] | 3308 | switch (action) { |
| 3309 | case MEM_GOING_OFFLINE: |
| 3310 | /* |
| 3311 | * Make sure there are not transient states where |
| 3312 | * an offline node is a migration target. This |
| 3313 | * will leave migration disabled until the offline |
| 3314 | * completes and the MEM_OFFLINE case below runs. |
| 3315 | */ |
| 3316 | disable_all_migrate_targets(); |
| 3317 | break; |
| 3318 | case MEM_OFFLINE: |
| 3319 | case MEM_ONLINE: |
| 3320 | /* |
| 3321 | * Recalculate the target nodes once the node |
| 3322 | * reaches its final state (online or offline). |
| 3323 | */ |
| 3324 | __set_migration_target_nodes(); |
| 3325 | break; |
| 3326 | case MEM_CANCEL_OFFLINE: |
| 3327 | /* |
| 3328 | * MEM_GOING_OFFLINE disabled all the migration |
| 3329 | * targets. Reenable them. |
| 3330 | */ |
| 3331 | __set_migration_target_nodes(); |
| 3332 | break; |
| 3333 | case MEM_GOING_ONLINE: |
| 3334 | case MEM_CANCEL_ONLINE: |
| 3335 | break; |
| 3336 | } |
| 3337 | |
| 3338 | return notifier_from_errno(0); |
| 3339 | } |
| 3340 | |
Dave Hansen | 76af6a0 | 2021-10-18 15:15:32 -0700 | [diff] [blame] | 3341 | /* |
| 3342 | * React to hotplug events that might affect the migration targets |
| 3343 | * like events that online or offline NUMA nodes. |
| 3344 | * |
| 3345 | * The ordering is also currently dependent on which nodes have |
| 3346 | * CPUs. That means we need CPU on/offline notification too. |
| 3347 | */ |
| 3348 | static int migration_online_cpu(unsigned int cpu) |
| 3349 | { |
| 3350 | set_migration_target_nodes(); |
| 3351 | return 0; |
| 3352 | } |
| 3353 | |
| 3354 | static int migration_offline_cpu(unsigned int cpu) |
| 3355 | { |
| 3356 | set_migration_target_nodes(); |
| 3357 | return 0; |
| 3358 | } |
| 3359 | |
Dave Hansen | 884a6e5 | 2021-09-02 14:59:09 -0700 | [diff] [blame] | 3360 | static int __init migrate_on_reclaim_init(void) |
| 3361 | { |
| 3362 | int ret; |
| 3363 | |
Huang Ying | a6a0251 | 2021-10-18 15:15:35 -0700 | [diff] [blame] | 3364 | ret = cpuhp_setup_state_nocalls(CPUHP_MM_DEMOTION_DEAD, "mm/demotion:offline", |
| 3365 | NULL, migration_offline_cpu); |
Dave Hansen | 884a6e5 | 2021-09-02 14:59:09 -0700 | [diff] [blame] | 3366 | /* |
| 3367 | * In the unlikely case that this fails, the automatic |
| 3368 | * migration targets may become suboptimal for nodes |
| 3369 | * where N_CPU changes. With such a small impact in a |
| 3370 | * rare case, do not bother trying to do anything special. |
| 3371 | */ |
| 3372 | WARN_ON(ret < 0); |
Huang Ying | a6a0251 | 2021-10-18 15:15:35 -0700 | [diff] [blame] | 3373 | ret = cpuhp_setup_state(CPUHP_AP_MM_DEMOTION_ONLINE, "mm/demotion:online", |
| 3374 | migration_online_cpu, NULL); |
| 3375 | WARN_ON(ret < 0); |
Dave Hansen | 884a6e5 | 2021-09-02 14:59:09 -0700 | [diff] [blame] | 3376 | |
| 3377 | hotplug_memory_notifier(migrate_on_reclaim_callback, 100); |
| 3378 | return 0; |
| 3379 | } |
| 3380 | late_initcall(migrate_on_reclaim_init); |
Dave Hansen | 76af6a0 | 2021-10-18 15:15:32 -0700 | [diff] [blame] | 3381 | #endif /* CONFIG_HOTPLUG_CPU */ |