Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1 | /* |
Hugh Dickins | 14e0f9b | 2015-11-05 18:49:43 -0800 | [diff] [blame] | 2 | * Memory Migration functionality - linux/mm/migrate.c |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2006 Silicon Graphics, Inc., Christoph Lameter |
| 5 | * |
| 6 | * Page migration was first developed in the context of the memory hotplug |
| 7 | * project. The main authors of the migration code are: |
| 8 | * |
| 9 | * IWAMOTO Toshihiro <iwamoto@valinux.co.jp> |
| 10 | * Hirokazu Takahashi <taka@valinux.co.jp> |
| 11 | * Dave Hansen <haveblue@us.ibm.com> |
Christoph Lameter | cde5353 | 2008-07-04 09:59:22 -0700 | [diff] [blame] | 12 | * Christoph Lameter |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <linux/migrate.h> |
Paul Gortmaker | b95f1b31 | 2011-10-16 02:01:52 -0400 | [diff] [blame] | 16 | #include <linux/export.h> |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 17 | #include <linux/swap.h> |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 18 | #include <linux/swapops.h> |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 19 | #include <linux/pagemap.h> |
Christoph Lameter | e23ca00 | 2006-04-10 22:52:57 -0700 | [diff] [blame] | 20 | #include <linux/buffer_head.h> |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 21 | #include <linux/mm_inline.h> |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 22 | #include <linux/nsproxy.h> |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 23 | #include <linux/pagevec.h> |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 24 | #include <linux/ksm.h> |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 25 | #include <linux/rmap.h> |
| 26 | #include <linux/topology.h> |
| 27 | #include <linux/cpu.h> |
| 28 | #include <linux/cpuset.h> |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 29 | #include <linux/writeback.h> |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 30 | #include <linux/mempolicy.h> |
| 31 | #include <linux/vmalloc.h> |
David Quigley | 86c3a76 | 2006-06-23 02:04:02 -0700 | [diff] [blame] | 32 | #include <linux/security.h> |
Hugh Dickins | 42cb14b | 2015-11-05 18:50:05 -0800 | [diff] [blame] | 33 | #include <linux/backing-dev.h> |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 34 | #include <linux/compaction.h> |
Adrian Bunk | 4f5ca26 | 2008-07-23 21:27:02 -0700 | [diff] [blame] | 35 | #include <linux/syscalls.h> |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 36 | #include <linux/hugetlb.h> |
Aneesh Kumar K.V | 8e6ac7f | 2012-07-31 16:42:27 -0700 | [diff] [blame] | 37 | #include <linux/hugetlb_cgroup.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 38 | #include <linux/gfp.h> |
Rafael Aquini | bf6bddf1 | 2012-12-11 16:02:42 -0800 | [diff] [blame] | 39 | #include <linux/balloon_compaction.h> |
Mel Gorman | f714f4f | 2013-12-18 17:08:33 -0800 | [diff] [blame] | 40 | #include <linux/mmu_notifier.h> |
Vladimir Davydov | 33c3fc7 | 2015-09-09 15:35:45 -0700 | [diff] [blame] | 41 | #include <linux/page_idle.h> |
Vlastimil Babka | d435edc | 2016-03-15 14:56:15 -0700 | [diff] [blame] | 42 | #include <linux/page_owner.h> |
Ingo Molnar | 6e84f31 | 2017-02-08 18:51:29 +0100 | [diff] [blame] | 43 | #include <linux/sched/mm.h> |
Linus Torvalds | 197e7e5 | 2017-08-20 13:26:27 -0700 | [diff] [blame] | 44 | #include <linux/ptrace.h> |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 45 | |
Michal Nazarewicz | 0d1836c | 2010-12-21 17:24:26 -0800 | [diff] [blame] | 46 | #include <asm/tlbflush.h> |
| 47 | |
Mel Gorman | 7b2a2d4 | 2012-10-19 14:07:31 +0100 | [diff] [blame] | 48 | #define CREATE_TRACE_POINTS |
| 49 | #include <trace/events/migrate.h> |
| 50 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 51 | #include "internal.h" |
| 52 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 53 | /* |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 54 | * migrate_prep() needs to be called before we start compiling a list of pages |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 55 | * to be migrated using isolate_lru_page(). If scheduling work on other CPUs is |
| 56 | * undesirable, use migrate_prep_local() |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 57 | */ |
| 58 | int migrate_prep(void) |
| 59 | { |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 60 | /* |
| 61 | * Clear the LRU lists so pages can be isolated. |
| 62 | * Note that pages may be moved off the LRU after we have |
| 63 | * drained them. Those pages will fail to migrate like other |
| 64 | * pages that may be busy. |
| 65 | */ |
| 66 | lru_add_drain_all(); |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 71 | /* Do the necessary work of migrate_prep but not if it involves other CPUs */ |
| 72 | int migrate_prep_local(void) |
| 73 | { |
| 74 | lru_add_drain(); |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
Yisheng Xie | 9e5bcd6 | 2017-02-24 14:57:29 -0800 | [diff] [blame] | 79 | int isolate_movable_page(struct page *page, isolate_mode_t mode) |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 80 | { |
| 81 | struct address_space *mapping; |
| 82 | |
| 83 | /* |
| 84 | * Avoid burning cycles with pages that are yet under __free_pages(), |
| 85 | * or just got freed under us. |
| 86 | * |
| 87 | * In case we 'win' a race for a movable page being freed under us and |
| 88 | * raise its refcount preventing __free_pages() from doing its job |
| 89 | * the put_page() at the end of this block will take care of |
| 90 | * release this page, thus avoiding a nasty leakage. |
| 91 | */ |
| 92 | if (unlikely(!get_page_unless_zero(page))) |
| 93 | goto out; |
| 94 | |
| 95 | /* |
| 96 | * Check PageMovable before holding a PG_lock because page's owner |
| 97 | * assumes anybody doesn't touch PG_lock of newly allocated page |
| 98 | * so unconditionally grapping the lock ruins page's owner side. |
| 99 | */ |
| 100 | if (unlikely(!__PageMovable(page))) |
| 101 | goto out_putpage; |
| 102 | /* |
| 103 | * As movable pages are not isolated from LRU lists, concurrent |
| 104 | * compaction threads can race against page migration functions |
| 105 | * as well as race against the releasing a page. |
| 106 | * |
| 107 | * In order to avoid having an already isolated movable page |
| 108 | * being (wrongly) re-isolated while it is under migration, |
| 109 | * or to avoid attempting to isolate pages being released, |
| 110 | * lets be sure we have the page lock |
| 111 | * before proceeding with the movable page isolation steps. |
| 112 | */ |
| 113 | if (unlikely(!trylock_page(page))) |
| 114 | goto out_putpage; |
| 115 | |
| 116 | if (!PageMovable(page) || PageIsolated(page)) |
| 117 | goto out_no_isolated; |
| 118 | |
| 119 | mapping = page_mapping(page); |
| 120 | VM_BUG_ON_PAGE(!mapping, page); |
| 121 | |
| 122 | if (!mapping->a_ops->isolate_page(page, mode)) |
| 123 | goto out_no_isolated; |
| 124 | |
| 125 | /* Driver shouldn't use PG_isolated bit of page->flags */ |
| 126 | WARN_ON_ONCE(PageIsolated(page)); |
| 127 | __SetPageIsolated(page); |
| 128 | unlock_page(page); |
| 129 | |
Yisheng Xie | 9e5bcd6 | 2017-02-24 14:57:29 -0800 | [diff] [blame] | 130 | return 0; |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 131 | |
| 132 | out_no_isolated: |
| 133 | unlock_page(page); |
| 134 | out_putpage: |
| 135 | put_page(page); |
| 136 | out: |
Yisheng Xie | 9e5bcd6 | 2017-02-24 14:57:29 -0800 | [diff] [blame] | 137 | return -EBUSY; |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /* It should be called on page which is PG_movable */ |
| 141 | void putback_movable_page(struct page *page) |
| 142 | { |
| 143 | struct address_space *mapping; |
| 144 | |
| 145 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
| 146 | VM_BUG_ON_PAGE(!PageMovable(page), page); |
| 147 | VM_BUG_ON_PAGE(!PageIsolated(page), page); |
| 148 | |
| 149 | mapping = page_mapping(page); |
| 150 | mapping->a_ops->putback_page(page); |
| 151 | __ClearPageIsolated(page); |
| 152 | } |
| 153 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 154 | /* |
Rafael Aquini | 5733c7d | 2012-12-11 16:02:47 -0800 | [diff] [blame] | 155 | * Put previously isolated pages back onto the appropriate lists |
| 156 | * from where they were once taken off for compaction/migration. |
| 157 | * |
Joonsoo Kim | 59c82b7 | 2014-01-21 15:51:17 -0800 | [diff] [blame] | 158 | * This function shall be used whenever the isolated pageset has been |
| 159 | * built from lru, balloon, hugetlbfs page. See isolate_migratepages_range() |
| 160 | * and isolate_huge_page(). |
Rafael Aquini | 5733c7d | 2012-12-11 16:02:47 -0800 | [diff] [blame] | 161 | */ |
| 162 | void putback_movable_pages(struct list_head *l) |
| 163 | { |
| 164 | struct page *page; |
| 165 | struct page *page2; |
| 166 | |
| 167 | list_for_each_entry_safe(page, page2, l, lru) { |
Naoya Horiguchi | 31caf66 | 2013-09-11 14:21:59 -0700 | [diff] [blame] | 168 | if (unlikely(PageHuge(page))) { |
| 169 | putback_active_hugepage(page); |
| 170 | continue; |
| 171 | } |
Rafael Aquini | 5733c7d | 2012-12-11 16:02:47 -0800 | [diff] [blame] | 172 | list_del(&page->lru); |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 173 | /* |
| 174 | * We isolated non-lru movable page so here we can use |
| 175 | * __PageMovable because LRU page's mapping cannot have |
| 176 | * PAGE_MAPPING_MOVABLE. |
| 177 | */ |
Minchan Kim | b1123ea6 | 2016-07-26 15:23:09 -0700 | [diff] [blame] | 178 | if (unlikely(__PageMovable(page))) { |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 179 | VM_BUG_ON_PAGE(!PageIsolated(page), page); |
| 180 | lock_page(page); |
| 181 | if (PageMovable(page)) |
| 182 | putback_movable_page(page); |
| 183 | else |
| 184 | __ClearPageIsolated(page); |
| 185 | unlock_page(page); |
| 186 | put_page(page); |
| 187 | } else { |
Naoya Horiguchi | e8db67e | 2017-09-08 16:11:12 -0700 | [diff] [blame] | 188 | mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + |
| 189 | page_is_file_cache(page), -hpage_nr_pages(page)); |
Rabin Vincent | fc280fe | 2017-04-20 14:37:46 -0700 | [diff] [blame] | 190 | putback_lru_page(page); |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 191 | } |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 192 | } |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 195 | /* |
| 196 | * Restore a potential migration pte to a working pte entry |
| 197 | */ |
Minchan Kim | e4b8222 | 2017-05-03 14:54:27 -0700 | [diff] [blame] | 198 | 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] | 199 | unsigned long addr, void *old) |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 200 | { |
Kirill A. Shutemov | 3fe8796 | 2017-02-24 14:58:16 -0800 | [diff] [blame] | 201 | struct page_vma_mapped_walk pvmw = { |
| 202 | .page = old, |
| 203 | .vma = vma, |
| 204 | .address = addr, |
| 205 | .flags = PVMW_SYNC | PVMW_MIGRATION, |
| 206 | }; |
| 207 | struct page *new; |
| 208 | pte_t pte; |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 209 | swp_entry_t entry; |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 210 | |
Kirill A. Shutemov | 3fe8796 | 2017-02-24 14:58:16 -0800 | [diff] [blame] | 211 | VM_BUG_ON_PAGE(PageTail(page), page); |
| 212 | while (page_vma_mapped_walk(&pvmw)) { |
Naoya Horiguchi | 4b0ece6 | 2017-03-31 15:11:44 -0700 | [diff] [blame] | 213 | if (PageKsm(page)) |
| 214 | new = page; |
| 215 | else |
| 216 | new = page - pvmw.page->index + |
| 217 | linear_page_index(vma, pvmw.address); |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 218 | |
Zi Yan | 616b837 | 2017-09-08 16:10:57 -0700 | [diff] [blame] | 219 | #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION |
| 220 | /* PMD-mapped THP migration entry */ |
| 221 | if (!pvmw.pte) { |
| 222 | VM_BUG_ON_PAGE(PageHuge(page) || !PageTransCompound(page), page); |
| 223 | remove_migration_pmd(&pvmw, new); |
| 224 | continue; |
| 225 | } |
| 226 | #endif |
| 227 | |
Kirill A. Shutemov | 3fe8796 | 2017-02-24 14:58:16 -0800 | [diff] [blame] | 228 | get_page(new); |
| 229 | pte = pte_mkold(mk_pte(new, READ_ONCE(vma->vm_page_prot))); |
| 230 | if (pte_swp_soft_dirty(*pvmw.pte)) |
| 231 | pte = pte_mksoft_dirty(pte); |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 232 | |
Hugh Dickins | 486cf46 | 2011-10-19 12:50:35 -0700 | [diff] [blame] | 233 | /* |
Kirill A. Shutemov | 3fe8796 | 2017-02-24 14:58:16 -0800 | [diff] [blame] | 234 | * Recheck VMA as permissions can change since migration started |
Hugh Dickins | 486cf46 | 2011-10-19 12:50:35 -0700 | [diff] [blame] | 235 | */ |
Kirill A. Shutemov | 3fe8796 | 2017-02-24 14:58:16 -0800 | [diff] [blame] | 236 | entry = pte_to_swp_entry(*pvmw.pte); |
| 237 | if (is_write_migration_entry(entry)) |
| 238 | pte = maybe_mkwrite(pte, vma); |
Mel Gorman | d3cb8bf | 2014-10-02 19:47:41 +0100 | [diff] [blame] | 239 | |
Aneesh Kumar K.V | 383321a | 2017-07-06 15:38:41 -0700 | [diff] [blame] | 240 | flush_dcache_page(new); |
Andi Kleen | 3ef8fd7 | 2010-10-11 16:03:21 +0200 | [diff] [blame] | 241 | #ifdef CONFIG_HUGETLB_PAGE |
Kirill A. Shutemov | 3fe8796 | 2017-02-24 14:58:16 -0800 | [diff] [blame] | 242 | if (PageHuge(new)) { |
| 243 | pte = pte_mkhuge(pte); |
| 244 | pte = arch_make_huge_pte(pte, vma, new, 0); |
Aneesh Kumar K.V | 383321a | 2017-07-06 15:38:41 -0700 | [diff] [blame] | 245 | 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] | 246 | if (PageAnon(new)) |
| 247 | hugepage_add_anon_rmap(new, vma, pvmw.address); |
| 248 | else |
| 249 | page_dup_rmap(new, true); |
Aneesh Kumar K.V | 383321a | 2017-07-06 15:38:41 -0700 | [diff] [blame] | 250 | } else |
| 251 | #endif |
| 252 | { |
| 253 | set_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte); |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 254 | |
Aneesh Kumar K.V | 383321a | 2017-07-06 15:38:41 -0700 | [diff] [blame] | 255 | if (PageAnon(new)) |
| 256 | page_add_anon_rmap(new, vma, pvmw.address, false); |
| 257 | else |
| 258 | page_add_file_rmap(new, false); |
| 259 | } |
Kirill A. Shutemov | 3fe8796 | 2017-02-24 14:58:16 -0800 | [diff] [blame] | 260 | if (vma->vm_flags & VM_LOCKED && !PageTransCompound(new)) |
| 261 | mlock_vma_page(new); |
Hugh Dickins | 51afb12 | 2015-11-05 18:49:37 -0800 | [diff] [blame] | 262 | |
Kirill A. Shutemov | 3fe8796 | 2017-02-24 14:58:16 -0800 | [diff] [blame] | 263 | /* No need to invalidate - it was non-present before */ |
| 264 | update_mmu_cache(vma, pvmw.address, pvmw.pte); |
| 265 | } |
| 266 | |
Minchan Kim | e4b8222 | 2017-05-03 14:54:27 -0700 | [diff] [blame] | 267 | return true; |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /* |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 271 | * Get rid of all migration entries and replace them by |
| 272 | * references to the indicated page. |
| 273 | */ |
Kirill A. Shutemov | e388466 | 2016-03-17 14:20:07 -0700 | [diff] [blame] | 274 | void remove_migration_ptes(struct page *old, struct page *new, bool locked) |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 275 | { |
Joonsoo Kim | 051ac83 | 2014-01-21 15:49:48 -0800 | [diff] [blame] | 276 | struct rmap_walk_control rwc = { |
| 277 | .rmap_one = remove_migration_pte, |
| 278 | .arg = old, |
| 279 | }; |
| 280 | |
Kirill A. Shutemov | e388466 | 2016-03-17 14:20:07 -0700 | [diff] [blame] | 281 | if (locked) |
| 282 | rmap_walk_locked(new, &rwc); |
| 283 | else |
| 284 | rmap_walk(new, &rwc); |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /* |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 288 | * Something used the pte of a page under migration. We need to |
| 289 | * get to the page and wait until migration is finished. |
| 290 | * When we return from this function the fault will be retried. |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 291 | */ |
Naoya Horiguchi | e66f17f | 2015-02-11 15:25:22 -0800 | [diff] [blame] | 292 | void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep, |
Naoya Horiguchi | 30dad30 | 2013-06-12 14:05:04 -0700 | [diff] [blame] | 293 | spinlock_t *ptl) |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 294 | { |
Naoya Horiguchi | 30dad30 | 2013-06-12 14:05:04 -0700 | [diff] [blame] | 295 | pte_t pte; |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 296 | swp_entry_t entry; |
| 297 | struct page *page; |
| 298 | |
Naoya Horiguchi | 30dad30 | 2013-06-12 14:05:04 -0700 | [diff] [blame] | 299 | spin_lock(ptl); |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 300 | pte = *ptep; |
| 301 | if (!is_swap_pte(pte)) |
| 302 | goto out; |
| 303 | |
| 304 | entry = pte_to_swp_entry(pte); |
| 305 | if (!is_migration_entry(entry)) |
| 306 | goto out; |
| 307 | |
| 308 | page = migration_entry_to_page(entry); |
| 309 | |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 310 | /* |
| 311 | * Once radix-tree replacement of page migration started, page_count |
| 312 | * *must* be zero. And, we don't want to call wait_on_page_locked() |
| 313 | * against a page without get_page(). |
| 314 | * So, we use get_page_unless_zero(), here. Even failed, page fault |
| 315 | * will occur again. |
| 316 | */ |
| 317 | if (!get_page_unless_zero(page)) |
| 318 | goto out; |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 319 | pte_unmap_unlock(ptep, ptl); |
| 320 | wait_on_page_locked(page); |
| 321 | put_page(page); |
| 322 | return; |
| 323 | out: |
| 324 | pte_unmap_unlock(ptep, ptl); |
| 325 | } |
| 326 | |
Naoya Horiguchi | 30dad30 | 2013-06-12 14:05:04 -0700 | [diff] [blame] | 327 | void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd, |
| 328 | unsigned long address) |
| 329 | { |
| 330 | spinlock_t *ptl = pte_lockptr(mm, pmd); |
| 331 | pte_t *ptep = pte_offset_map(pmd, address); |
| 332 | __migration_entry_wait(mm, ptep, ptl); |
| 333 | } |
| 334 | |
Kirill A. Shutemov | cb900f4 | 2013-11-14 14:31:02 -0800 | [diff] [blame] | 335 | void migration_entry_wait_huge(struct vm_area_struct *vma, |
| 336 | struct mm_struct *mm, pte_t *pte) |
Naoya Horiguchi | 30dad30 | 2013-06-12 14:05:04 -0700 | [diff] [blame] | 337 | { |
Kirill A. Shutemov | cb900f4 | 2013-11-14 14:31:02 -0800 | [diff] [blame] | 338 | spinlock_t *ptl = huge_pte_lockptr(hstate_vma(vma), mm, pte); |
Naoya Horiguchi | 30dad30 | 2013-06-12 14:05:04 -0700 | [diff] [blame] | 339 | __migration_entry_wait(mm, pte, ptl); |
| 340 | } |
| 341 | |
Zi Yan | 616b837 | 2017-09-08 16:10:57 -0700 | [diff] [blame] | 342 | #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION |
| 343 | void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd) |
| 344 | { |
| 345 | spinlock_t *ptl; |
| 346 | struct page *page; |
| 347 | |
| 348 | ptl = pmd_lock(mm, pmd); |
| 349 | if (!is_pmd_migration_entry(*pmd)) |
| 350 | goto unlock; |
| 351 | page = migration_entry_to_page(pmd_to_swp_entry(*pmd)); |
| 352 | if (!get_page_unless_zero(page)) |
| 353 | goto unlock; |
| 354 | spin_unlock(ptl); |
| 355 | wait_on_page_locked(page); |
| 356 | put_page(page); |
| 357 | return; |
| 358 | unlock: |
| 359 | spin_unlock(ptl); |
| 360 | } |
| 361 | #endif |
| 362 | |
Mel Gorman | b969c4a | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 363 | #ifdef CONFIG_BLOCK |
| 364 | /* Returns true if all buffers are successfully locked */ |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 365 | static bool buffer_migrate_lock_buffers(struct buffer_head *head, |
| 366 | enum migrate_mode mode) |
Mel Gorman | b969c4a | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 367 | { |
| 368 | struct buffer_head *bh = head; |
| 369 | |
| 370 | /* Simple case, sync compaction */ |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 371 | if (mode != MIGRATE_ASYNC) { |
Mel Gorman | b969c4a | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 372 | do { |
| 373 | get_bh(bh); |
| 374 | lock_buffer(bh); |
| 375 | bh = bh->b_this_page; |
| 376 | |
| 377 | } while (bh != head); |
| 378 | |
| 379 | return true; |
| 380 | } |
| 381 | |
| 382 | /* async case, we cannot block on lock_buffer so use trylock_buffer */ |
| 383 | do { |
| 384 | get_bh(bh); |
| 385 | if (!trylock_buffer(bh)) { |
| 386 | /* |
| 387 | * We failed to lock the buffer and cannot stall in |
| 388 | * async migration. Release the taken locks |
| 389 | */ |
| 390 | struct buffer_head *failed_bh = bh; |
| 391 | put_bh(failed_bh); |
| 392 | bh = head; |
| 393 | while (bh != failed_bh) { |
| 394 | unlock_buffer(bh); |
| 395 | put_bh(bh); |
| 396 | bh = bh->b_this_page; |
| 397 | } |
| 398 | return false; |
| 399 | } |
| 400 | |
| 401 | bh = bh->b_this_page; |
| 402 | } while (bh != head); |
| 403 | return true; |
| 404 | } |
| 405 | #else |
| 406 | static inline bool buffer_migrate_lock_buffers(struct buffer_head *head, |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 407 | enum migrate_mode mode) |
Mel Gorman | b969c4a | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 408 | { |
| 409 | return true; |
| 410 | } |
| 411 | #endif /* CONFIG_BLOCK */ |
| 412 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 413 | /* |
Christoph Lameter | c3fcf8a | 2006-06-23 02:03:32 -0700 | [diff] [blame] | 414 | * Replace the page in the mapping. |
Christoph Lameter | 5b5c712 | 2006-06-23 02:03:29 -0700 | [diff] [blame] | 415 | * |
| 416 | * The number of remaining references must be: |
| 417 | * 1 for anonymous pages without a mapping |
| 418 | * 2 for pages with a mapping |
David Howells | 266cf65 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 419 | * 3 for pages with a mapping and PagePrivate/PagePrivate2 set. |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 420 | */ |
Gu Zheng | 36bc08c | 2013-07-16 17:56:16 +0800 | [diff] [blame] | 421 | int migrate_page_move_mapping(struct address_space *mapping, |
Mel Gorman | b969c4a | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 422 | struct page *newpage, struct page *page, |
Benjamin LaHaise | 8e321fe | 2013-12-21 17:56:08 -0500 | [diff] [blame] | 423 | struct buffer_head *head, enum migrate_mode mode, |
| 424 | int extra_count) |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 425 | { |
Hugh Dickins | 42cb14b | 2015-11-05 18:50:05 -0800 | [diff] [blame] | 426 | struct zone *oldzone, *newzone; |
| 427 | int dirty; |
Benjamin LaHaise | 8e321fe | 2013-12-21 17:56:08 -0500 | [diff] [blame] | 428 | int expected_count = 1 + extra_count; |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 429 | void **pslot; |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 430 | |
Christoph Lameter | 6c5240a | 2006-06-23 02:03:37 -0700 | [diff] [blame] | 431 | if (!mapping) { |
Christoph Lameter | 0e8c7d0 | 2007-04-23 14:41:09 -0700 | [diff] [blame] | 432 | /* Anonymous page without mapping */ |
Benjamin LaHaise | 8e321fe | 2013-12-21 17:56:08 -0500 | [diff] [blame] | 433 | if (page_count(page) != expected_count) |
Christoph Lameter | 6c5240a | 2006-06-23 02:03:37 -0700 | [diff] [blame] | 434 | return -EAGAIN; |
Hugh Dickins | cf4b769 | 2015-11-05 18:50:02 -0800 | [diff] [blame] | 435 | |
| 436 | /* No turning back from here */ |
Hugh Dickins | cf4b769 | 2015-11-05 18:50:02 -0800 | [diff] [blame] | 437 | newpage->index = page->index; |
| 438 | newpage->mapping = page->mapping; |
| 439 | if (PageSwapBacked(page)) |
Hugh Dickins | fa9949d | 2016-05-19 17:12:41 -0700 | [diff] [blame] | 440 | __SetPageSwapBacked(newpage); |
Hugh Dickins | cf4b769 | 2015-11-05 18:50:02 -0800 | [diff] [blame] | 441 | |
Rafael Aquini | 78bd520 | 2012-12-11 16:02:31 -0800 | [diff] [blame] | 442 | return MIGRATEPAGE_SUCCESS; |
Christoph Lameter | 6c5240a | 2006-06-23 02:03:37 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Hugh Dickins | 42cb14b | 2015-11-05 18:50:05 -0800 | [diff] [blame] | 445 | oldzone = page_zone(page); |
| 446 | newzone = page_zone(newpage); |
| 447 | |
Nick Piggin | 19fd623 | 2008-07-25 19:45:32 -0700 | [diff] [blame] | 448 | spin_lock_irq(&mapping->tree_lock); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 449 | |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 450 | pslot = radix_tree_lookup_slot(&mapping->page_tree, |
| 451 | page_index(page)); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 452 | |
Benjamin LaHaise | 8e321fe | 2013-12-21 17:56:08 -0500 | [diff] [blame] | 453 | expected_count += 1 + page_has_private(page); |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 454 | if (page_count(page) != expected_count || |
Mel Gorman | 29c1f67 | 2011-01-13 15:47:21 -0800 | [diff] [blame] | 455 | radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) { |
Nick Piggin | 19fd623 | 2008-07-25 19:45:32 -0700 | [diff] [blame] | 456 | spin_unlock_irq(&mapping->tree_lock); |
Christoph Lameter | e23ca00 | 2006-04-10 22:52:57 -0700 | [diff] [blame] | 457 | return -EAGAIN; |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 458 | } |
| 459 | |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 460 | if (!page_ref_freeze(page, expected_count)) { |
Nick Piggin | 19fd623 | 2008-07-25 19:45:32 -0700 | [diff] [blame] | 461 | spin_unlock_irq(&mapping->tree_lock); |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 462 | return -EAGAIN; |
| 463 | } |
| 464 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 465 | /* |
Mel Gorman | b969c4a | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 466 | * In the async migration case of moving a page with buffers, lock the |
| 467 | * buffers using trylock before the mapping is moved. If the mapping |
| 468 | * was moved, we later failed to lock the buffers and could not move |
| 469 | * the mapping back due to an elevated page count, we would have to |
| 470 | * block waiting on other references to be dropped. |
| 471 | */ |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 472 | if (mode == MIGRATE_ASYNC && head && |
| 473 | !buffer_migrate_lock_buffers(head, mode)) { |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 474 | page_ref_unfreeze(page, expected_count); |
Mel Gorman | b969c4a | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 475 | spin_unlock_irq(&mapping->tree_lock); |
| 476 | return -EAGAIN; |
| 477 | } |
| 478 | |
| 479 | /* |
Hugh Dickins | cf4b769 | 2015-11-05 18:50:02 -0800 | [diff] [blame] | 480 | * Now we know that no one else is looking at the page: |
| 481 | * no turning back from here. |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 482 | */ |
Hugh Dickins | cf4b769 | 2015-11-05 18:50:02 -0800 | [diff] [blame] | 483 | newpage->index = page->index; |
| 484 | newpage->mapping = page->mapping; |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 485 | get_page(newpage); /* add cache reference */ |
Nicholas Piggin | 6326fec | 2016-12-25 13:00:29 +1000 | [diff] [blame] | 486 | if (PageSwapBacked(page)) { |
| 487 | __SetPageSwapBacked(newpage); |
| 488 | if (PageSwapCache(page)) { |
| 489 | SetPageSwapCache(newpage); |
| 490 | set_page_private(newpage, page_private(page)); |
| 491 | } |
| 492 | } else { |
| 493 | VM_BUG_ON_PAGE(PageSwapCache(page), page); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 494 | } |
| 495 | |
Hugh Dickins | 42cb14b | 2015-11-05 18:50:05 -0800 | [diff] [blame] | 496 | /* Move dirty while page refs frozen and newpage not yet exposed */ |
| 497 | dirty = PageDirty(page); |
| 498 | if (dirty) { |
| 499 | ClearPageDirty(page); |
| 500 | SetPageDirty(newpage); |
| 501 | } |
| 502 | |
Johannes Weiner | 6d75f36 | 2016-12-12 16:43:43 -0800 | [diff] [blame] | 503 | radix_tree_replace_slot(&mapping->page_tree, pslot, newpage); |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 504 | |
| 505 | /* |
Jacobo Giralt | 937a94c | 2012-01-10 15:07:11 -0800 | [diff] [blame] | 506 | * Drop cache reference from old page by unfreezing |
| 507 | * to one less reference. |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 508 | * We know this isn't the last reference. |
| 509 | */ |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 510 | page_ref_unfreeze(page, expected_count - 1); |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 511 | |
Hugh Dickins | 42cb14b | 2015-11-05 18:50:05 -0800 | [diff] [blame] | 512 | spin_unlock(&mapping->tree_lock); |
| 513 | /* Leave irq disabled to prevent preemption while updating stats */ |
| 514 | |
Christoph Lameter | 0e8c7d0 | 2007-04-23 14:41:09 -0700 | [diff] [blame] | 515 | /* |
| 516 | * If moved to a different zone then also account |
| 517 | * the page for that zone. Other VM counters will be |
| 518 | * taken care of when we establish references to the |
| 519 | * new page and drop references to the old page. |
| 520 | * |
| 521 | * Note that anonymous pages are accounted for |
Mel Gorman | 4b9d0fa | 2016-07-28 15:46:17 -0700 | [diff] [blame] | 522 | * via NR_FILE_PAGES and NR_ANON_MAPPED if they |
Christoph Lameter | 0e8c7d0 | 2007-04-23 14:41:09 -0700 | [diff] [blame] | 523 | * are mapped to swap space. |
| 524 | */ |
Hugh Dickins | 42cb14b | 2015-11-05 18:50:05 -0800 | [diff] [blame] | 525 | if (newzone != oldzone) { |
Mel Gorman | 11fb998 | 2016-07-28 15:46:20 -0700 | [diff] [blame] | 526 | __dec_node_state(oldzone->zone_pgdat, NR_FILE_PAGES); |
| 527 | __inc_node_state(newzone->zone_pgdat, NR_FILE_PAGES); |
Hugh Dickins | 42cb14b | 2015-11-05 18:50:05 -0800 | [diff] [blame] | 528 | if (PageSwapBacked(page) && !PageSwapCache(page)) { |
Mel Gorman | 11fb998 | 2016-07-28 15:46:20 -0700 | [diff] [blame] | 529 | __dec_node_state(oldzone->zone_pgdat, NR_SHMEM); |
| 530 | __inc_node_state(newzone->zone_pgdat, NR_SHMEM); |
Hugh Dickins | 42cb14b | 2015-11-05 18:50:05 -0800 | [diff] [blame] | 531 | } |
| 532 | if (dirty && mapping_cap_account_dirty(mapping)) { |
Mel Gorman | 11fb998 | 2016-07-28 15:46:20 -0700 | [diff] [blame] | 533 | __dec_node_state(oldzone->zone_pgdat, NR_FILE_DIRTY); |
Mel Gorman | 5a1c84b | 2016-07-28 15:47:31 -0700 | [diff] [blame] | 534 | __dec_zone_state(oldzone, NR_ZONE_WRITE_PENDING); |
Mel Gorman | 11fb998 | 2016-07-28 15:46:20 -0700 | [diff] [blame] | 535 | __inc_node_state(newzone->zone_pgdat, NR_FILE_DIRTY); |
Mel Gorman | 5a1c84b | 2016-07-28 15:47:31 -0700 | [diff] [blame] | 536 | __inc_zone_state(newzone, NR_ZONE_WRITE_PENDING); |
Hugh Dickins | 42cb14b | 2015-11-05 18:50:05 -0800 | [diff] [blame] | 537 | } |
KOSAKI Motohiro | 4b02108 | 2009-09-21 17:01:33 -0700 | [diff] [blame] | 538 | } |
Hugh Dickins | 42cb14b | 2015-11-05 18:50:05 -0800 | [diff] [blame] | 539 | local_irq_enable(); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 540 | |
Rafael Aquini | 78bd520 | 2012-12-11 16:02:31 -0800 | [diff] [blame] | 541 | return MIGRATEPAGE_SUCCESS; |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 542 | } |
Richard Weinberger | 1118dce | 2016-06-16 23:26:14 +0200 | [diff] [blame] | 543 | EXPORT_SYMBOL(migrate_page_move_mapping); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 544 | |
| 545 | /* |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 546 | * The expected number of remaining references is the same as that |
| 547 | * of migrate_page_move_mapping(). |
| 548 | */ |
| 549 | int migrate_huge_page_move_mapping(struct address_space *mapping, |
| 550 | struct page *newpage, struct page *page) |
| 551 | { |
| 552 | int expected_count; |
| 553 | void **pslot; |
| 554 | |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 555 | spin_lock_irq(&mapping->tree_lock); |
| 556 | |
| 557 | pslot = radix_tree_lookup_slot(&mapping->page_tree, |
| 558 | page_index(page)); |
| 559 | |
| 560 | expected_count = 2 + page_has_private(page); |
| 561 | if (page_count(page) != expected_count || |
Mel Gorman | 29c1f67 | 2011-01-13 15:47:21 -0800 | [diff] [blame] | 562 | radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) { |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 563 | spin_unlock_irq(&mapping->tree_lock); |
| 564 | return -EAGAIN; |
| 565 | } |
| 566 | |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 567 | if (!page_ref_freeze(page, expected_count)) { |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 568 | spin_unlock_irq(&mapping->tree_lock); |
| 569 | return -EAGAIN; |
| 570 | } |
| 571 | |
Hugh Dickins | cf4b769 | 2015-11-05 18:50:02 -0800 | [diff] [blame] | 572 | newpage->index = page->index; |
| 573 | newpage->mapping = page->mapping; |
Johannes Weiner | 6a93ca8 | 2016-03-15 14:57:19 -0700 | [diff] [blame] | 574 | |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 575 | get_page(newpage); |
| 576 | |
Johannes Weiner | 6d75f36 | 2016-12-12 16:43:43 -0800 | [diff] [blame] | 577 | radix_tree_replace_slot(&mapping->page_tree, pslot, newpage); |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 578 | |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 579 | page_ref_unfreeze(page, expected_count - 1); |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 580 | |
| 581 | spin_unlock_irq(&mapping->tree_lock); |
Johannes Weiner | 6a93ca8 | 2016-03-15 14:57:19 -0700 | [diff] [blame] | 582 | |
Rafael Aquini | 78bd520 | 2012-12-11 16:02:31 -0800 | [diff] [blame] | 583 | return MIGRATEPAGE_SUCCESS; |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | /* |
Dave Hansen | 30b0a10 | 2013-11-21 14:31:58 -0800 | [diff] [blame] | 587 | * Gigantic pages are so large that we do not guarantee that page++ pointer |
| 588 | * arithmetic will work across the entire page. We need something more |
| 589 | * specialized. |
| 590 | */ |
| 591 | static void __copy_gigantic_page(struct page *dst, struct page *src, |
| 592 | int nr_pages) |
| 593 | { |
| 594 | int i; |
| 595 | struct page *dst_base = dst; |
| 596 | struct page *src_base = src; |
| 597 | |
| 598 | for (i = 0; i < nr_pages; ) { |
| 599 | cond_resched(); |
| 600 | copy_highpage(dst, src); |
| 601 | |
| 602 | i++; |
| 603 | dst = mem_map_next(dst, dst_base, i); |
| 604 | src = mem_map_next(src, src_base, i); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | static void copy_huge_page(struct page *dst, struct page *src) |
| 609 | { |
| 610 | int i; |
| 611 | int nr_pages; |
| 612 | |
| 613 | if (PageHuge(src)) { |
| 614 | /* hugetlbfs page */ |
| 615 | struct hstate *h = page_hstate(src); |
| 616 | nr_pages = pages_per_huge_page(h); |
| 617 | |
| 618 | if (unlikely(nr_pages > MAX_ORDER_NR_PAGES)) { |
| 619 | __copy_gigantic_page(dst, src, nr_pages); |
| 620 | return; |
| 621 | } |
| 622 | } else { |
| 623 | /* thp page */ |
| 624 | BUG_ON(!PageTransHuge(src)); |
| 625 | nr_pages = hpage_nr_pages(src); |
| 626 | } |
| 627 | |
| 628 | for (i = 0; i < nr_pages; i++) { |
| 629 | cond_resched(); |
| 630 | copy_highpage(dst + i, src + i); |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | /* |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 635 | * Copy the page to its new location |
| 636 | */ |
Jérôme Glisse | 2916ecc | 2017-09-08 16:12:06 -0700 | [diff] [blame^] | 637 | void migrate_page_states(struct page *newpage, struct page *page) |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 638 | { |
Rik van Riel | 7851a45 | 2013-10-07 11:29:23 +0100 | [diff] [blame] | 639 | int cpupid; |
| 640 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 641 | if (PageError(page)) |
| 642 | SetPageError(newpage); |
| 643 | if (PageReferenced(page)) |
| 644 | SetPageReferenced(newpage); |
| 645 | if (PageUptodate(page)) |
| 646 | SetPageUptodate(newpage); |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 647 | if (TestClearPageActive(page)) { |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 648 | VM_BUG_ON_PAGE(PageUnevictable(page), page); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 649 | SetPageActive(newpage); |
Lee Schermerhorn | 418b27e | 2009-12-14 17:59:54 -0800 | [diff] [blame] | 650 | } else if (TestClearPageUnevictable(page)) |
| 651 | SetPageUnevictable(newpage); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 652 | if (PageChecked(page)) |
| 653 | SetPageChecked(newpage); |
| 654 | if (PageMappedToDisk(page)) |
| 655 | SetPageMappedToDisk(newpage); |
| 656 | |
Hugh Dickins | 42cb14b | 2015-11-05 18:50:05 -0800 | [diff] [blame] | 657 | /* Move dirty on pages not done by migrate_page_move_mapping() */ |
| 658 | if (PageDirty(page)) |
| 659 | SetPageDirty(newpage); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 660 | |
Vladimir Davydov | 33c3fc7 | 2015-09-09 15:35:45 -0700 | [diff] [blame] | 661 | if (page_is_young(page)) |
| 662 | set_page_young(newpage); |
| 663 | if (page_is_idle(page)) |
| 664 | set_page_idle(newpage); |
| 665 | |
Rik van Riel | 7851a45 | 2013-10-07 11:29:23 +0100 | [diff] [blame] | 666 | /* |
| 667 | * Copy NUMA information to the new page, to prevent over-eager |
| 668 | * future migrations of this same page. |
| 669 | */ |
| 670 | cpupid = page_cpupid_xchg_last(page, -1); |
| 671 | page_cpupid_xchg_last(newpage, cpupid); |
| 672 | |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 673 | ksm_migrate_page(newpage, page); |
Hugh Dickins | c8d6553 | 2013-02-22 16:35:10 -0800 | [diff] [blame] | 674 | /* |
| 675 | * Please do not reorder this without considering how mm/ksm.c's |
| 676 | * get_ksm_page() depends upon ksm_migrate_page() and PageSwapCache(). |
| 677 | */ |
Naoya Horiguchi | b3b3a99 | 2015-04-15 16:13:15 -0700 | [diff] [blame] | 678 | if (PageSwapCache(page)) |
| 679 | ClearPageSwapCache(page); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 680 | ClearPagePrivate(page); |
| 681 | set_page_private(page, 0); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 682 | |
| 683 | /* |
| 684 | * If any waiters have accumulated on the new page then |
| 685 | * wake them up. |
| 686 | */ |
| 687 | if (PageWriteback(newpage)) |
| 688 | end_page_writeback(newpage); |
Vlastimil Babka | d435edc | 2016-03-15 14:56:15 -0700 | [diff] [blame] | 689 | |
| 690 | copy_page_owner(page, newpage); |
Johannes Weiner | 74485cf | 2016-03-15 14:57:54 -0700 | [diff] [blame] | 691 | |
| 692 | mem_cgroup_migrate(page, newpage); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 693 | } |
Jérôme Glisse | 2916ecc | 2017-09-08 16:12:06 -0700 | [diff] [blame^] | 694 | EXPORT_SYMBOL(migrate_page_states); |
| 695 | |
| 696 | void migrate_page_copy(struct page *newpage, struct page *page) |
| 697 | { |
| 698 | if (PageHuge(page) || PageTransHuge(page)) |
| 699 | copy_huge_page(newpage, page); |
| 700 | else |
| 701 | copy_highpage(newpage, page); |
| 702 | |
| 703 | migrate_page_states(newpage, page); |
| 704 | } |
Richard Weinberger | 1118dce | 2016-06-16 23:26:14 +0200 | [diff] [blame] | 705 | EXPORT_SYMBOL(migrate_page_copy); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 706 | |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 707 | /************************************************************ |
| 708 | * Migration functions |
| 709 | ***********************************************************/ |
| 710 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 711 | /* |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 712 | * Common logic to directly migrate a single LRU page suitable for |
David Howells | 266cf65 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 713 | * pages that do not use PagePrivate/PagePrivate2. |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 714 | * |
| 715 | * Pages are locked upon entry and exit. |
| 716 | */ |
Christoph Lameter | 2d1db3b | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 717 | int migrate_page(struct address_space *mapping, |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 718 | struct page *newpage, struct page *page, |
| 719 | enum migrate_mode mode) |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 720 | { |
| 721 | int rc; |
| 722 | |
| 723 | BUG_ON(PageWriteback(page)); /* Writeback must be complete */ |
| 724 | |
Benjamin LaHaise | 8e321fe | 2013-12-21 17:56:08 -0500 | [diff] [blame] | 725 | rc = migrate_page_move_mapping(mapping, newpage, page, NULL, mode, 0); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 726 | |
Rafael Aquini | 78bd520 | 2012-12-11 16:02:31 -0800 | [diff] [blame] | 727 | if (rc != MIGRATEPAGE_SUCCESS) |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 728 | return rc; |
| 729 | |
Jérôme Glisse | 2916ecc | 2017-09-08 16:12:06 -0700 | [diff] [blame^] | 730 | if (mode != MIGRATE_SYNC_NO_COPY) |
| 731 | migrate_page_copy(newpage, page); |
| 732 | else |
| 733 | migrate_page_states(newpage, page); |
Rafael Aquini | 78bd520 | 2012-12-11 16:02:31 -0800 | [diff] [blame] | 734 | return MIGRATEPAGE_SUCCESS; |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 735 | } |
| 736 | EXPORT_SYMBOL(migrate_page); |
| 737 | |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 738 | #ifdef CONFIG_BLOCK |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 739 | /* |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 740 | * Migration function for pages with buffers. This function can only be used |
| 741 | * if the underlying filesystem guarantees that no other references to "page" |
| 742 | * exist. |
| 743 | */ |
Christoph Lameter | 2d1db3b | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 744 | int buffer_migrate_page(struct address_space *mapping, |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 745 | struct page *newpage, struct page *page, enum migrate_mode mode) |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 746 | { |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 747 | struct buffer_head *bh, *head; |
| 748 | int rc; |
| 749 | |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 750 | if (!page_has_buffers(page)) |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 751 | return migrate_page(mapping, newpage, page, mode); |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 752 | |
| 753 | head = page_buffers(page); |
| 754 | |
Benjamin LaHaise | 8e321fe | 2013-12-21 17:56:08 -0500 | [diff] [blame] | 755 | rc = migrate_page_move_mapping(mapping, newpage, page, head, mode, 0); |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 756 | |
Rafael Aquini | 78bd520 | 2012-12-11 16:02:31 -0800 | [diff] [blame] | 757 | if (rc != MIGRATEPAGE_SUCCESS) |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 758 | return rc; |
| 759 | |
Mel Gorman | b969c4a | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 760 | /* |
| 761 | * In the async case, migrate_page_move_mapping locked the buffers |
| 762 | * with an IRQ-safe spinlock held. In the sync case, the buffers |
| 763 | * need to be locked now |
| 764 | */ |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 765 | if (mode != MIGRATE_ASYNC) |
| 766 | BUG_ON(!buffer_migrate_lock_buffers(head, mode)); |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 767 | |
| 768 | ClearPagePrivate(page); |
| 769 | set_page_private(newpage, page_private(page)); |
| 770 | set_page_private(page, 0); |
| 771 | put_page(page); |
| 772 | get_page(newpage); |
| 773 | |
| 774 | bh = head; |
| 775 | do { |
| 776 | set_bh_page(bh, newpage, bh_offset(bh)); |
| 777 | bh = bh->b_this_page; |
| 778 | |
| 779 | } while (bh != head); |
| 780 | |
| 781 | SetPagePrivate(newpage); |
| 782 | |
Jérôme Glisse | 2916ecc | 2017-09-08 16:12:06 -0700 | [diff] [blame^] | 783 | if (mode != MIGRATE_SYNC_NO_COPY) |
| 784 | migrate_page_copy(newpage, page); |
| 785 | else |
| 786 | migrate_page_states(newpage, page); |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 787 | |
| 788 | bh = head; |
| 789 | do { |
| 790 | unlock_buffer(bh); |
Jérôme Glisse | 2916ecc | 2017-09-08 16:12:06 -0700 | [diff] [blame^] | 791 | put_bh(bh); |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 792 | bh = bh->b_this_page; |
| 793 | |
| 794 | } while (bh != head); |
| 795 | |
Rafael Aquini | 78bd520 | 2012-12-11 16:02:31 -0800 | [diff] [blame] | 796 | return MIGRATEPAGE_SUCCESS; |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 797 | } |
| 798 | EXPORT_SYMBOL(buffer_migrate_page); |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 799 | #endif |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 800 | |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 801 | /* |
| 802 | * Writeback a page to clean the dirty state |
| 803 | */ |
| 804 | static int writeout(struct address_space *mapping, struct page *page) |
| 805 | { |
| 806 | struct writeback_control wbc = { |
| 807 | .sync_mode = WB_SYNC_NONE, |
| 808 | .nr_to_write = 1, |
| 809 | .range_start = 0, |
| 810 | .range_end = LLONG_MAX, |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 811 | .for_reclaim = 1 |
| 812 | }; |
| 813 | int rc; |
| 814 | |
| 815 | if (!mapping->a_ops->writepage) |
| 816 | /* No write method for the address space */ |
| 817 | return -EINVAL; |
| 818 | |
| 819 | if (!clear_page_dirty_for_io(page)) |
| 820 | /* Someone else already triggered a write */ |
| 821 | return -EAGAIN; |
| 822 | |
| 823 | /* |
| 824 | * A dirty page may imply that the underlying filesystem has |
| 825 | * the page on some queue. So the page must be clean for |
| 826 | * migration. Writeout may mean we loose the lock and the |
| 827 | * page state is no longer what we checked for earlier. |
| 828 | * At this point we know that the migration attempt cannot |
| 829 | * be successful. |
| 830 | */ |
Kirill A. Shutemov | e388466 | 2016-03-17 14:20:07 -0700 | [diff] [blame] | 831 | remove_migration_ptes(page, page, false); |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 832 | |
| 833 | rc = mapping->a_ops->writepage(page, &wbc); |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 834 | |
| 835 | if (rc != AOP_WRITEPAGE_ACTIVATE) |
| 836 | /* unlocked. Relock */ |
| 837 | lock_page(page); |
| 838 | |
Hugh Dickins | bda8550 | 2008-11-19 15:36:36 -0800 | [diff] [blame] | 839 | return (rc < 0) ? -EIO : -EAGAIN; |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | /* |
| 843 | * Default handling if a filesystem does not provide a migration function. |
| 844 | */ |
Christoph Lameter | 8351a6e | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 845 | static int fallback_migrate_page(struct address_space *mapping, |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 846 | struct page *newpage, struct page *page, enum migrate_mode mode) |
Christoph Lameter | 8351a6e | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 847 | { |
Mel Gorman | b969c4a | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 848 | if (PageDirty(page)) { |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 849 | /* Only writeback pages in full synchronous migration */ |
Jérôme Glisse | 2916ecc | 2017-09-08 16:12:06 -0700 | [diff] [blame^] | 850 | switch (mode) { |
| 851 | case MIGRATE_SYNC: |
| 852 | case MIGRATE_SYNC_NO_COPY: |
| 853 | break; |
| 854 | default: |
Mel Gorman | b969c4a | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 855 | return -EBUSY; |
Jérôme Glisse | 2916ecc | 2017-09-08 16:12:06 -0700 | [diff] [blame^] | 856 | } |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 857 | return writeout(mapping, page); |
Mel Gorman | b969c4a | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 858 | } |
Christoph Lameter | 8351a6e | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 859 | |
| 860 | /* |
| 861 | * Buffers may be managed in a filesystem specific way. |
| 862 | * We must have no buffers or drop them. |
| 863 | */ |
David Howells | 266cf65 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 864 | if (page_has_private(page) && |
Christoph Lameter | 8351a6e | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 865 | !try_to_release_page(page, GFP_KERNEL)) |
| 866 | return -EAGAIN; |
| 867 | |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 868 | return migrate_page(mapping, newpage, page, mode); |
Christoph Lameter | 8351a6e | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 869 | } |
| 870 | |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 871 | /* |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 872 | * Move a page to a newly allocated page |
| 873 | * The page is locked and all ptes have been successfully removed. |
| 874 | * |
| 875 | * The new page will have replaced the old page if this function |
| 876 | * is successful. |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 877 | * |
| 878 | * Return value: |
| 879 | * < 0 - error code |
Rafael Aquini | 78bd520 | 2012-12-11 16:02:31 -0800 | [diff] [blame] | 880 | * MIGRATEPAGE_SUCCESS - success |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 881 | */ |
Mel Gorman | 3fe2011 | 2010-05-24 14:32:20 -0700 | [diff] [blame] | 882 | static int move_to_new_page(struct page *newpage, struct page *page, |
Hugh Dickins | 5c3f9a6 | 2015-11-05 18:49:53 -0800 | [diff] [blame] | 883 | enum migrate_mode mode) |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 884 | { |
| 885 | struct address_space *mapping; |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 886 | int rc = -EAGAIN; |
| 887 | bool is_lru = !__PageMovable(page); |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 888 | |
Hugh Dickins | 7db7671 | 2015-11-05 18:49:49 -0800 | [diff] [blame] | 889 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
| 890 | VM_BUG_ON_PAGE(!PageLocked(newpage), newpage); |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 891 | |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 892 | mapping = page_mapping(page); |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 893 | |
| 894 | if (likely(is_lru)) { |
| 895 | if (!mapping) |
| 896 | rc = migrate_page(mapping, newpage, page, mode); |
| 897 | else if (mapping->a_ops->migratepage) |
| 898 | /* |
| 899 | * Most pages have a mapping and most filesystems |
| 900 | * provide a migratepage callback. Anonymous pages |
| 901 | * are part of swap space which also has its own |
| 902 | * migratepage callback. This is the most common path |
| 903 | * for page migration. |
| 904 | */ |
| 905 | rc = mapping->a_ops->migratepage(mapping, newpage, |
| 906 | page, mode); |
| 907 | else |
| 908 | rc = fallback_migrate_page(mapping, newpage, |
| 909 | page, mode); |
| 910 | } else { |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 911 | /* |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 912 | * In case of non-lru page, it could be released after |
| 913 | * isolation step. In that case, we shouldn't try migration. |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 914 | */ |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 915 | VM_BUG_ON_PAGE(!PageIsolated(page), page); |
| 916 | if (!PageMovable(page)) { |
| 917 | rc = MIGRATEPAGE_SUCCESS; |
| 918 | __ClearPageIsolated(page); |
| 919 | goto out; |
| 920 | } |
| 921 | |
| 922 | rc = mapping->a_ops->migratepage(mapping, newpage, |
| 923 | page, mode); |
| 924 | WARN_ON_ONCE(rc == MIGRATEPAGE_SUCCESS && |
| 925 | !PageIsolated(page)); |
| 926 | } |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 927 | |
Hugh Dickins | 5c3f9a6 | 2015-11-05 18:49:53 -0800 | [diff] [blame] | 928 | /* |
| 929 | * When successful, old pagecache page->mapping must be cleared before |
| 930 | * page is freed; but stats require that PageAnon be left as PageAnon. |
| 931 | */ |
| 932 | if (rc == MIGRATEPAGE_SUCCESS) { |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 933 | if (__PageMovable(page)) { |
| 934 | VM_BUG_ON_PAGE(!PageIsolated(page), page); |
| 935 | |
| 936 | /* |
| 937 | * We clear PG_movable under page_lock so any compactor |
| 938 | * cannot try to migrate this page. |
| 939 | */ |
| 940 | __ClearPageIsolated(page); |
| 941 | } |
| 942 | |
| 943 | /* |
| 944 | * Anonymous and movable page->mapping will be cleard by |
| 945 | * free_pages_prepare so don't reset it here for keeping |
| 946 | * the type to work PageAnon, for example. |
| 947 | */ |
| 948 | if (!PageMappingFlags(page)) |
Hugh Dickins | 5c3f9a6 | 2015-11-05 18:49:53 -0800 | [diff] [blame] | 949 | page->mapping = NULL; |
Mel Gorman | 3fe2011 | 2010-05-24 14:32:20 -0700 | [diff] [blame] | 950 | } |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 951 | out: |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 952 | return rc; |
| 953 | } |
| 954 | |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 955 | static int __unmap_and_move(struct page *page, struct page *newpage, |
Hugh Dickins | 9c620e2 | 2013-02-22 16:35:14 -0800 | [diff] [blame] | 956 | int force, enum migrate_mode mode) |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 957 | { |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 958 | int rc = -EAGAIN; |
Hugh Dickins | 2ebba6b | 2014-12-12 16:56:19 -0800 | [diff] [blame] | 959 | int page_was_mapped = 0; |
Mel Gorman | 3f6c827 | 2010-05-24 14:32:17 -0700 | [diff] [blame] | 960 | struct anon_vma *anon_vma = NULL; |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 961 | bool is_lru = !__PageMovable(page); |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 962 | |
Nick Piggin | 529ae9a | 2008-08-02 12:01:03 +0200 | [diff] [blame] | 963 | if (!trylock_page(page)) { |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 964 | if (!force || mode == MIGRATE_ASYNC) |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 965 | goto out; |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 966 | |
| 967 | /* |
| 968 | * It's not safe for direct compaction to call lock_page. |
| 969 | * For example, during page readahead pages are added locked |
| 970 | * to the LRU. Later, when the IO completes the pages are |
| 971 | * marked uptodate and unlocked. However, the queueing |
| 972 | * could be merging multiple pages for one bio (e.g. |
| 973 | * mpage_readpages). If an allocation happens for the |
| 974 | * second or third page, the process can end up locking |
| 975 | * the same page twice and deadlocking. Rather than |
| 976 | * trying to be clever about what pages can be locked, |
| 977 | * avoid the use of lock_page for direct compaction |
| 978 | * altogether. |
| 979 | */ |
| 980 | if (current->flags & PF_MEMALLOC) |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 981 | goto out; |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 982 | |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 983 | lock_page(page); |
| 984 | } |
| 985 | |
| 986 | if (PageWriteback(page)) { |
Andrea Arcangeli | 11bc82d | 2011-03-22 16:33:11 -0700 | [diff] [blame] | 987 | /* |
Jianguo Wu | fed5b64 | 2013-04-29 15:07:58 -0700 | [diff] [blame] | 988 | * Only in the case of a full synchronous migration is it |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 989 | * necessary to wait for PageWriteback. In the async case, |
| 990 | * the retry loop is too short and in the sync-light case, |
| 991 | * the overhead of stalling is too much |
Andrea Arcangeli | 11bc82d | 2011-03-22 16:33:11 -0700 | [diff] [blame] | 992 | */ |
Jérôme Glisse | 2916ecc | 2017-09-08 16:12:06 -0700 | [diff] [blame^] | 993 | switch (mode) { |
| 994 | case MIGRATE_SYNC: |
| 995 | case MIGRATE_SYNC_NO_COPY: |
| 996 | break; |
| 997 | default: |
Andrea Arcangeli | 11bc82d | 2011-03-22 16:33:11 -0700 | [diff] [blame] | 998 | rc = -EBUSY; |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 999 | goto out_unlock; |
Andrea Arcangeli | 11bc82d | 2011-03-22 16:33:11 -0700 | [diff] [blame] | 1000 | } |
| 1001 | if (!force) |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 1002 | goto out_unlock; |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1003 | wait_on_page_writeback(page); |
| 1004 | } |
Hugh Dickins | 03f15c8 | 2015-11-05 18:49:56 -0800 | [diff] [blame] | 1005 | |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1006 | /* |
KAMEZAWA Hiroyuki | dc386d4 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 1007 | * By try_to_unmap(), page->mapcount goes down to 0 here. In this case, |
| 1008 | * 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] | 1009 | * This get_anon_vma() delays freeing anon_vma pointer until the end |
KAMEZAWA Hiroyuki | dc386d4 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 1010 | * of migration. File cache pages are no problem because of page_lock() |
KAMEZAWA Hiroyuki | 989f89c | 2007-08-30 23:56:21 -0700 | [diff] [blame] | 1011 | * File Caches may use write_page() or lock_page() in migration, then, |
| 1012 | * just care Anon page here. |
Hugh Dickins | 03f15c8 | 2015-11-05 18:49:56 -0800 | [diff] [blame] | 1013 | * |
| 1014 | * Only page_get_anon_vma() understands the subtleties of |
| 1015 | * getting a hold on an anon_vma from outside one of its mms. |
| 1016 | * But if we cannot get anon_vma, then we won't need it anyway, |
| 1017 | * because that implies that the anon page is no longer mapped |
| 1018 | * (and cannot be remapped so long as we hold the page lock). |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1019 | */ |
Hugh Dickins | 03f15c8 | 2015-11-05 18:49:56 -0800 | [diff] [blame] | 1020 | if (PageAnon(page) && !PageKsm(page)) |
Peter Zijlstra | 746b18d | 2011-05-24 17:12:10 -0700 | [diff] [blame] | 1021 | anon_vma = page_get_anon_vma(page); |
Shaohua Li | 62e1c55 | 2008-02-04 22:29:33 -0800 | [diff] [blame] | 1022 | |
Hugh Dickins | 7db7671 | 2015-11-05 18:49:49 -0800 | [diff] [blame] | 1023 | /* |
| 1024 | * Block others from accessing the new page when we get around to |
| 1025 | * establishing additional references. We are usually the only one |
| 1026 | * holding a reference to newpage at this point. We used to have a BUG |
| 1027 | * here if trylock_page(newpage) fails, but would like to allow for |
| 1028 | * cases where there might be a race with the previous use of newpage. |
| 1029 | * This is much like races on refcount of oldpage: just don't BUG(). |
| 1030 | */ |
| 1031 | if (unlikely(!trylock_page(newpage))) |
| 1032 | goto out_unlock; |
| 1033 | |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 1034 | if (unlikely(!is_lru)) { |
| 1035 | rc = move_to_new_page(newpage, page, mode); |
| 1036 | goto out_unlock_both; |
| 1037 | } |
| 1038 | |
KAMEZAWA Hiroyuki | dc386d4 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 1039 | /* |
Shaohua Li | 62e1c55 | 2008-02-04 22:29:33 -0800 | [diff] [blame] | 1040 | * Corner case handling: |
| 1041 | * 1. When a new swap-cache page is read into, it is added to the LRU |
| 1042 | * and treated as swapcache but it has no rmap yet. |
| 1043 | * Calling try_to_unmap() against a page->mapping==NULL page will |
| 1044 | * trigger a BUG. So handle it here. |
| 1045 | * 2. An orphaned page (see truncate_complete_page) might have |
| 1046 | * fs-private metadata. The page can be picked up due to memory |
| 1047 | * offlining. Everywhere else except page reclaim, the page is |
| 1048 | * invisible to the vm, so the page can not be migrated. So try to |
| 1049 | * free the metadata, so the page can be freed. |
KAMEZAWA Hiroyuki | dc386d4 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 1050 | */ |
Shaohua Li | 62e1c55 | 2008-02-04 22:29:33 -0800 | [diff] [blame] | 1051 | if (!page->mapping) { |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 1052 | VM_BUG_ON_PAGE(PageAnon(page), page); |
Hugh Dickins | 1ce82b6 | 2011-01-13 15:47:30 -0800 | [diff] [blame] | 1053 | if (page_has_private(page)) { |
Shaohua Li | 62e1c55 | 2008-02-04 22:29:33 -0800 | [diff] [blame] | 1054 | try_to_free_buffers(page); |
Hugh Dickins | 7db7671 | 2015-11-05 18:49:49 -0800 | [diff] [blame] | 1055 | goto out_unlock_both; |
Shaohua Li | 62e1c55 | 2008-02-04 22:29:33 -0800 | [diff] [blame] | 1056 | } |
Hugh Dickins | 7db7671 | 2015-11-05 18:49:49 -0800 | [diff] [blame] | 1057 | } else if (page_mapped(page)) { |
| 1058 | /* Establish migration ptes */ |
Hugh Dickins | 03f15c8 | 2015-11-05 18:49:56 -0800 | [diff] [blame] | 1059 | VM_BUG_ON_PAGE(PageAnon(page) && !PageKsm(page) && !anon_vma, |
| 1060 | page); |
Hugh Dickins | 2ebba6b | 2014-12-12 16:56:19 -0800 | [diff] [blame] | 1061 | try_to_unmap(page, |
Wanpeng Li | da1b13c | 2015-09-08 15:03:27 -0700 | [diff] [blame] | 1062 | TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS); |
Hugh Dickins | 2ebba6b | 2014-12-12 16:56:19 -0800 | [diff] [blame] | 1063 | page_was_mapped = 1; |
| 1064 | } |
KAMEZAWA Hiroyuki | dc386d4 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 1065 | |
Christoph Lameter | e6a1530 | 2006-06-25 05:46:49 -0700 | [diff] [blame] | 1066 | if (!page_mapped(page)) |
Hugh Dickins | 5c3f9a6 | 2015-11-05 18:49:53 -0800 | [diff] [blame] | 1067 | rc = move_to_new_page(newpage, page, mode); |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1068 | |
Hugh Dickins | 5c3f9a6 | 2015-11-05 18:49:53 -0800 | [diff] [blame] | 1069 | if (page_was_mapped) |
| 1070 | remove_migration_ptes(page, |
Kirill A. Shutemov | e388466 | 2016-03-17 14:20:07 -0700 | [diff] [blame] | 1071 | rc == MIGRATEPAGE_SUCCESS ? newpage : page, false); |
Mel Gorman | 3f6c827 | 2010-05-24 14:32:17 -0700 | [diff] [blame] | 1072 | |
Hugh Dickins | 7db7671 | 2015-11-05 18:49:49 -0800 | [diff] [blame] | 1073 | out_unlock_both: |
| 1074 | unlock_page(newpage); |
| 1075 | out_unlock: |
Mel Gorman | 3f6c827 | 2010-05-24 14:32:17 -0700 | [diff] [blame] | 1076 | /* Drop an anon_vma reference if we took one */ |
Rik van Riel | 7654506 | 2010-08-09 17:18:41 -0700 | [diff] [blame] | 1077 | if (anon_vma) |
Peter Zijlstra | 9e60109 | 2011-03-22 16:32:46 -0700 | [diff] [blame] | 1078 | put_anon_vma(anon_vma); |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1079 | unlock_page(page); |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1080 | out: |
Minchan Kim | c6c919e | 2016-07-26 15:23:02 -0700 | [diff] [blame] | 1081 | /* |
| 1082 | * If migration is successful, decrease refcount of the newpage |
| 1083 | * which will not free the page because new page owner increased |
| 1084 | * refcounter. As well, if it is LRU page, add the page to LRU |
| 1085 | * list in here. |
| 1086 | */ |
| 1087 | if (rc == MIGRATEPAGE_SUCCESS) { |
Minchan Kim | b1123ea6 | 2016-07-26 15:23:09 -0700 | [diff] [blame] | 1088 | if (unlikely(__PageMovable(newpage))) |
Minchan Kim | c6c919e | 2016-07-26 15:23:02 -0700 | [diff] [blame] | 1089 | put_page(newpage); |
| 1090 | else |
| 1091 | putback_lru_page(newpage); |
| 1092 | } |
| 1093 | |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1094 | return rc; |
| 1095 | } |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 1096 | |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1097 | /* |
Geert Uytterhoeven | ef2a515 | 2015-04-14 15:44:22 -0700 | [diff] [blame] | 1098 | * gcc 4.7 and 4.8 on arm get an ICEs when inlining unmap_and_move(). Work |
| 1099 | * around it. |
| 1100 | */ |
| 1101 | #if (GCC_VERSION >= 40700 && GCC_VERSION < 40900) && defined(CONFIG_ARM) |
| 1102 | #define ICE_noinline noinline |
| 1103 | #else |
| 1104 | #define ICE_noinline |
| 1105 | #endif |
| 1106 | |
| 1107 | /* |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1108 | * Obtain the lock on page, remove all ptes and migrate the page |
| 1109 | * to the newly allocated page in newpage. |
| 1110 | */ |
Geert Uytterhoeven | ef2a515 | 2015-04-14 15:44:22 -0700 | [diff] [blame] | 1111 | static ICE_noinline int unmap_and_move(new_page_t get_new_page, |
| 1112 | free_page_t put_new_page, |
| 1113 | unsigned long private, struct page *page, |
Naoya Horiguchi | add05ce | 2015-06-24 16:56:50 -0700 | [diff] [blame] | 1114 | int force, enum migrate_mode mode, |
| 1115 | enum migrate_reason reason) |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1116 | { |
Hugh Dickins | 2def742 | 2015-11-05 18:49:46 -0800 | [diff] [blame] | 1117 | int rc = MIGRATEPAGE_SUCCESS; |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1118 | int *result = NULL; |
Hugh Dickins | 2def742 | 2015-11-05 18:49:46 -0800 | [diff] [blame] | 1119 | struct page *newpage; |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1120 | |
Hugh Dickins | 2def742 | 2015-11-05 18:49:46 -0800 | [diff] [blame] | 1121 | newpage = get_new_page(page, private, &result); |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1122 | if (!newpage) |
| 1123 | return -ENOMEM; |
| 1124 | |
| 1125 | if (page_count(page) == 1) { |
| 1126 | /* page was freed from under us. So we are done. */ |
Minchan Kim | c6c919e | 2016-07-26 15:23:02 -0700 | [diff] [blame] | 1127 | ClearPageActive(page); |
| 1128 | ClearPageUnevictable(page); |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 1129 | if (unlikely(__PageMovable(page))) { |
| 1130 | lock_page(page); |
| 1131 | if (!PageMovable(page)) |
| 1132 | __ClearPageIsolated(page); |
| 1133 | unlock_page(page); |
| 1134 | } |
Minchan Kim | c6c919e | 2016-07-26 15:23:02 -0700 | [diff] [blame] | 1135 | if (put_new_page) |
| 1136 | put_new_page(newpage, private); |
| 1137 | else |
| 1138 | put_page(newpage); |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1139 | goto out; |
| 1140 | } |
| 1141 | |
Zi Yan | 616b837 | 2017-09-08 16:10:57 -0700 | [diff] [blame] | 1142 | if (unlikely(PageTransHuge(page) && !PageTransHuge(newpage))) { |
Kirill A. Shutemov | 4d2fa96 | 2016-01-15 16:54:00 -0800 | [diff] [blame] | 1143 | lock_page(page); |
| 1144 | rc = split_huge_page(page); |
| 1145 | unlock_page(page); |
| 1146 | if (rc) |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1147 | goto out; |
Kirill A. Shutemov | 4d2fa96 | 2016-01-15 16:54:00 -0800 | [diff] [blame] | 1148 | } |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1149 | |
Hugh Dickins | 9c620e2 | 2013-02-22 16:35:14 -0800 | [diff] [blame] | 1150 | rc = __unmap_and_move(page, newpage, force, mode); |
Minchan Kim | c6c919e | 2016-07-26 15:23:02 -0700 | [diff] [blame] | 1151 | if (rc == MIGRATEPAGE_SUCCESS) |
Vlastimil Babka | 7cd12b4 | 2016-03-15 14:56:18 -0700 | [diff] [blame] | 1152 | set_page_owner_migrate_reason(newpage, reason); |
Rafael Aquini | bf6bddf1 | 2012-12-11 16:02:42 -0800 | [diff] [blame] | 1153 | |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1154 | out: |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1155 | if (rc != -EAGAIN) { |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1156 | /* |
| 1157 | * A page that has been migrated has all references |
| 1158 | * removed and will be freed. A page that has not been |
| 1159 | * migrated will have kepts its references and be |
| 1160 | * restored. |
| 1161 | */ |
| 1162 | list_del(&page->lru); |
Ming Ling | 6afcf8e | 2016-12-12 16:42:26 -0800 | [diff] [blame] | 1163 | |
| 1164 | /* |
| 1165 | * Compaction can migrate also non-LRU pages which are |
| 1166 | * not accounted to NR_ISOLATED_*. They can be recognized |
| 1167 | * as __PageMovable |
| 1168 | */ |
| 1169 | if (likely(!__PageMovable(page))) |
Naoya Horiguchi | e8db67e | 2017-09-08 16:11:12 -0700 | [diff] [blame] | 1170 | mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + |
| 1171 | page_is_file_cache(page), -hpage_nr_pages(page)); |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1172 | } |
David Rientjes | 68711a7 | 2014-06-04 16:08:25 -0700 | [diff] [blame] | 1173 | |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 1174 | /* |
Minchan Kim | c6c919e | 2016-07-26 15:23:02 -0700 | [diff] [blame] | 1175 | * If migration is successful, releases reference grabbed during |
| 1176 | * isolation. Otherwise, restore the page to right list unless |
| 1177 | * we want to retry. |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 1178 | */ |
Minchan Kim | c6c919e | 2016-07-26 15:23:02 -0700 | [diff] [blame] | 1179 | if (rc == MIGRATEPAGE_SUCCESS) { |
| 1180 | put_page(page); |
| 1181 | if (reason == MR_MEMORY_FAILURE) { |
| 1182 | /* |
| 1183 | * Set PG_HWPoison on just freed page |
| 1184 | * intentionally. Although it's rather weird, |
| 1185 | * it's how HWPoison flag works at the moment. |
| 1186 | */ |
| 1187 | if (!test_set_page_hwpoison(page)) |
| 1188 | num_poisoned_pages_inc(); |
| 1189 | } |
| 1190 | } else { |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 1191 | if (rc != -EAGAIN) { |
| 1192 | if (likely(!__PageMovable(page))) { |
| 1193 | putback_lru_page(page); |
| 1194 | goto put_new; |
| 1195 | } |
| 1196 | |
| 1197 | lock_page(page); |
| 1198 | if (PageMovable(page)) |
| 1199 | putback_movable_page(page); |
| 1200 | else |
| 1201 | __ClearPageIsolated(page); |
| 1202 | unlock_page(page); |
| 1203 | put_page(page); |
| 1204 | } |
| 1205 | put_new: |
Minchan Kim | c6c919e | 2016-07-26 15:23:02 -0700 | [diff] [blame] | 1206 | if (put_new_page) |
| 1207 | put_new_page(newpage, private); |
| 1208 | else |
| 1209 | put_page(newpage); |
| 1210 | } |
David Rientjes | 68711a7 | 2014-06-04 16:08:25 -0700 | [diff] [blame] | 1211 | |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1212 | if (result) { |
| 1213 | if (rc) |
| 1214 | *result = rc; |
| 1215 | else |
| 1216 | *result = page_to_nid(newpage); |
| 1217 | } |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1218 | return rc; |
| 1219 | } |
| 1220 | |
| 1221 | /* |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1222 | * Counterpart of unmap_and_move_page() for hugepage migration. |
| 1223 | * |
| 1224 | * This function doesn't wait the completion of hugepage I/O |
| 1225 | * because there is no race between I/O and migration for hugepage. |
| 1226 | * Note that currently hugepage I/O occurs only in direct I/O |
| 1227 | * where no lock is held and PG_writeback is irrelevant, |
| 1228 | * and writeback status of all subpages are counted in the reference |
| 1229 | * count of the head page (i.e. if all subpages of a 2MB hugepage are |
| 1230 | * under direct I/O, the reference of the head page is 512 and a bit more.) |
| 1231 | * This means that when we try to migrate hugepage whose subpages are |
| 1232 | * doing direct I/O, some references remain after try_to_unmap() and |
| 1233 | * hugepage migration fails without data corruption. |
| 1234 | * |
| 1235 | * There is also no race when direct I/O is issued on the page under migration, |
| 1236 | * because then pte is replaced with migration swap entry and direct I/O code |
| 1237 | * will wait in the page fault for migration to complete. |
| 1238 | */ |
| 1239 | 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] | 1240 | free_page_t put_new_page, unsigned long private, |
| 1241 | struct page *hpage, int force, |
Vlastimil Babka | 7cd12b4 | 2016-03-15 14:56:18 -0700 | [diff] [blame] | 1242 | enum migrate_mode mode, int reason) |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1243 | { |
Hugh Dickins | 2def742 | 2015-11-05 18:49:46 -0800 | [diff] [blame] | 1244 | int rc = -EAGAIN; |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1245 | int *result = NULL; |
Hugh Dickins | 2ebba6b | 2014-12-12 16:56:19 -0800 | [diff] [blame] | 1246 | int page_was_mapped = 0; |
Joonsoo Kim | 32665f2 | 2014-01-21 15:51:15 -0800 | [diff] [blame] | 1247 | struct page *new_hpage; |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1248 | struct anon_vma *anon_vma = NULL; |
| 1249 | |
Naoya Horiguchi | 83467ef | 2013-09-11 14:22:11 -0700 | [diff] [blame] | 1250 | /* |
| 1251 | * Movability of hugepages depends on architectures and hugepage size. |
| 1252 | * This check is necessary because some callers of hugepage migration |
| 1253 | * like soft offline and memory hotremove don't walk through page |
| 1254 | * tables or check whether the hugepage is pmd-based or not before |
| 1255 | * kicking migration. |
| 1256 | */ |
Naoya Horiguchi | 100873d | 2014-06-04 16:10:56 -0700 | [diff] [blame] | 1257 | if (!hugepage_migration_supported(page_hstate(hpage))) { |
Joonsoo Kim | 32665f2 | 2014-01-21 15:51:15 -0800 | [diff] [blame] | 1258 | putback_active_hugepage(hpage); |
Naoya Horiguchi | 83467ef | 2013-09-11 14:22:11 -0700 | [diff] [blame] | 1259 | return -ENOSYS; |
Joonsoo Kim | 32665f2 | 2014-01-21 15:51:15 -0800 | [diff] [blame] | 1260 | } |
Naoya Horiguchi | 83467ef | 2013-09-11 14:22:11 -0700 | [diff] [blame] | 1261 | |
Joonsoo Kim | 32665f2 | 2014-01-21 15:51:15 -0800 | [diff] [blame] | 1262 | new_hpage = get_new_page(hpage, private, &result); |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1263 | if (!new_hpage) |
| 1264 | return -ENOMEM; |
| 1265 | |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1266 | if (!trylock_page(hpage)) { |
Jérôme Glisse | 2916ecc | 2017-09-08 16:12:06 -0700 | [diff] [blame^] | 1267 | if (!force) |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1268 | goto out; |
Jérôme Glisse | 2916ecc | 2017-09-08 16:12:06 -0700 | [diff] [blame^] | 1269 | switch (mode) { |
| 1270 | case MIGRATE_SYNC: |
| 1271 | case MIGRATE_SYNC_NO_COPY: |
| 1272 | break; |
| 1273 | default: |
| 1274 | goto out; |
| 1275 | } |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1276 | lock_page(hpage); |
| 1277 | } |
| 1278 | |
Peter Zijlstra | 746b18d | 2011-05-24 17:12:10 -0700 | [diff] [blame] | 1279 | if (PageAnon(hpage)) |
| 1280 | anon_vma = page_get_anon_vma(hpage); |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1281 | |
Hugh Dickins | 7db7671 | 2015-11-05 18:49:49 -0800 | [diff] [blame] | 1282 | if (unlikely(!trylock_page(new_hpage))) |
| 1283 | goto put_anon; |
| 1284 | |
Hugh Dickins | 2ebba6b | 2014-12-12 16:56:19 -0800 | [diff] [blame] | 1285 | if (page_mapped(hpage)) { |
| 1286 | try_to_unmap(hpage, |
| 1287 | TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS); |
| 1288 | page_was_mapped = 1; |
| 1289 | } |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1290 | |
| 1291 | if (!page_mapped(hpage)) |
Hugh Dickins | 5c3f9a6 | 2015-11-05 18:49:53 -0800 | [diff] [blame] | 1292 | rc = move_to_new_page(new_hpage, hpage, mode); |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1293 | |
Hugh Dickins | 5c3f9a6 | 2015-11-05 18:49:53 -0800 | [diff] [blame] | 1294 | if (page_was_mapped) |
| 1295 | remove_migration_ptes(hpage, |
Kirill A. Shutemov | e388466 | 2016-03-17 14:20:07 -0700 | [diff] [blame] | 1296 | rc == MIGRATEPAGE_SUCCESS ? new_hpage : hpage, false); |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1297 | |
Hugh Dickins | 7db7671 | 2015-11-05 18:49:49 -0800 | [diff] [blame] | 1298 | unlock_page(new_hpage); |
| 1299 | |
| 1300 | put_anon: |
Hugh Dickins | fd4a466 | 2011-01-13 15:47:31 -0800 | [diff] [blame] | 1301 | if (anon_vma) |
Peter Zijlstra | 9e60109 | 2011-03-22 16:32:46 -0700 | [diff] [blame] | 1302 | put_anon_vma(anon_vma); |
Aneesh Kumar K.V | 8e6ac7f | 2012-07-31 16:42:27 -0700 | [diff] [blame] | 1303 | |
Hugh Dickins | 2def742 | 2015-11-05 18:49:46 -0800 | [diff] [blame] | 1304 | if (rc == MIGRATEPAGE_SUCCESS) { |
Aneesh Kumar K.V | 8e6ac7f | 2012-07-31 16:42:27 -0700 | [diff] [blame] | 1305 | hugetlb_cgroup_migrate(hpage, new_hpage); |
Hugh Dickins | 2def742 | 2015-11-05 18:49:46 -0800 | [diff] [blame] | 1306 | put_new_page = NULL; |
Vlastimil Babka | 7cd12b4 | 2016-03-15 14:56:18 -0700 | [diff] [blame] | 1307 | set_page_owner_migrate_reason(new_hpage, reason); |
Hugh Dickins | 2def742 | 2015-11-05 18:49:46 -0800 | [diff] [blame] | 1308 | } |
Aneesh Kumar K.V | 8e6ac7f | 2012-07-31 16:42:27 -0700 | [diff] [blame] | 1309 | |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1310 | unlock_page(hpage); |
Hillf Danton | 0976133 | 2011-12-08 14:34:20 -0800 | [diff] [blame] | 1311 | out: |
Naoya Horiguchi | b8ec1ce | 2013-09-11 14:22:01 -0700 | [diff] [blame] | 1312 | if (rc != -EAGAIN) |
| 1313 | putback_active_hugepage(hpage); |
Anshuman Khandual | c3114a8 | 2017-07-10 15:47:41 -0700 | [diff] [blame] | 1314 | if (reason == MR_MEMORY_FAILURE && !test_set_page_hwpoison(hpage)) |
| 1315 | num_poisoned_pages_inc(); |
David Rientjes | 68711a7 | 2014-06-04 16:08:25 -0700 | [diff] [blame] | 1316 | |
| 1317 | /* |
| 1318 | * If migration was not successful and there's a freeing callback, use |
| 1319 | * it. Otherwise, put_page() will drop the reference grabbed during |
| 1320 | * isolation. |
| 1321 | */ |
Hugh Dickins | 2def742 | 2015-11-05 18:49:46 -0800 | [diff] [blame] | 1322 | if (put_new_page) |
David Rientjes | 68711a7 | 2014-06-04 16:08:25 -0700 | [diff] [blame] | 1323 | put_new_page(new_hpage, private); |
| 1324 | else |
Naoya Horiguchi | 3aaa76e | 2015-09-22 14:59:14 -0700 | [diff] [blame] | 1325 | putback_active_hugepage(new_hpage); |
David Rientjes | 68711a7 | 2014-06-04 16:08:25 -0700 | [diff] [blame] | 1326 | |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1327 | if (result) { |
| 1328 | if (rc) |
| 1329 | *result = rc; |
| 1330 | else |
| 1331 | *result = page_to_nid(new_hpage); |
| 1332 | } |
| 1333 | return rc; |
| 1334 | } |
| 1335 | |
| 1336 | /* |
Srivatsa S. Bhat | c73e5c9 | 2013-04-29 15:08:16 -0700 | [diff] [blame] | 1337 | * migrate_pages - migrate the pages specified in a list, to the free pages |
| 1338 | * supplied as the target for the page migration |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1339 | * |
Srivatsa S. Bhat | c73e5c9 | 2013-04-29 15:08:16 -0700 | [diff] [blame] | 1340 | * @from: The list of pages to be migrated. |
| 1341 | * @get_new_page: The function used to allocate free pages to be used |
| 1342 | * as the target of the page migration. |
David Rientjes | 68711a7 | 2014-06-04 16:08:25 -0700 | [diff] [blame] | 1343 | * @put_new_page: The function used to free target pages if migration |
| 1344 | * fails, or NULL if no special handling is necessary. |
Srivatsa S. Bhat | c73e5c9 | 2013-04-29 15:08:16 -0700 | [diff] [blame] | 1345 | * @private: Private data to be passed on to get_new_page() |
| 1346 | * @mode: The migration mode that specifies the constraints for |
| 1347 | * page migration, if any. |
| 1348 | * @reason: The reason for page migration. |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1349 | * |
Srivatsa S. Bhat | c73e5c9 | 2013-04-29 15:08:16 -0700 | [diff] [blame] | 1350 | * The function returns after 10 attempts or if no pages are movable any more |
| 1351 | * because the list has become empty or no retryable pages exist any more. |
Hugh Dickins | 14e0f9b | 2015-11-05 18:49:43 -0800 | [diff] [blame] | 1352 | * The caller should call putback_movable_pages() to return pages to the LRU |
Minchan Kim | 28bd657 | 2011-01-25 15:07:26 -0800 | [diff] [blame] | 1353 | * or free list only if ret != 0. |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1354 | * |
Srivatsa S. Bhat | c73e5c9 | 2013-04-29 15:08:16 -0700 | [diff] [blame] | 1355 | * 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] | 1356 | */ |
Hugh Dickins | 9c620e2 | 2013-02-22 16:35:14 -0800 | [diff] [blame] | 1357 | 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] | 1358 | free_page_t put_new_page, unsigned long private, |
| 1359 | enum migrate_mode mode, int reason) |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1360 | { |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1361 | int retry = 1; |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1362 | int nr_failed = 0; |
Mel Gorman | 5647bc2 | 2012-10-19 10:46:20 +0100 | [diff] [blame] | 1363 | int nr_succeeded = 0; |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1364 | int pass = 0; |
| 1365 | struct page *page; |
| 1366 | struct page *page2; |
| 1367 | int swapwrite = current->flags & PF_SWAPWRITE; |
| 1368 | int rc; |
| 1369 | |
| 1370 | if (!swapwrite) |
| 1371 | current->flags |= PF_SWAPWRITE; |
| 1372 | |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1373 | for(pass = 0; pass < 10 && retry; pass++) { |
| 1374 | retry = 0; |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1375 | |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1376 | list_for_each_entry_safe(page, page2, from, lru) { |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1377 | cond_resched(); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1378 | |
Naoya Horiguchi | 31caf66 | 2013-09-11 14:21:59 -0700 | [diff] [blame] | 1379 | if (PageHuge(page)) |
| 1380 | rc = unmap_and_move_huge_page(get_new_page, |
David Rientjes | 68711a7 | 2014-06-04 16:08:25 -0700 | [diff] [blame] | 1381 | put_new_page, private, page, |
Vlastimil Babka | 7cd12b4 | 2016-03-15 14:56:18 -0700 | [diff] [blame] | 1382 | pass > 2, mode, reason); |
Naoya Horiguchi | 31caf66 | 2013-09-11 14:21:59 -0700 | [diff] [blame] | 1383 | else |
David Rientjes | 68711a7 | 2014-06-04 16:08:25 -0700 | [diff] [blame] | 1384 | rc = unmap_and_move(get_new_page, put_new_page, |
Naoya Horiguchi | add05ce | 2015-06-24 16:56:50 -0700 | [diff] [blame] | 1385 | private, page, pass > 2, mode, |
| 1386 | reason); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1387 | |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1388 | switch(rc) { |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 1389 | case -ENOMEM: |
David Rientjes | dfef2ef | 2016-05-20 16:59:05 -0700 | [diff] [blame] | 1390 | nr_failed++; |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 1391 | goto out; |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1392 | case -EAGAIN: |
Christoph Lameter | 2d1db3b | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 1393 | retry++; |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1394 | break; |
Rafael Aquini | 78bd520 | 2012-12-11 16:02:31 -0800 | [diff] [blame] | 1395 | case MIGRATEPAGE_SUCCESS: |
Mel Gorman | 5647bc2 | 2012-10-19 10:46:20 +0100 | [diff] [blame] | 1396 | nr_succeeded++; |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1397 | break; |
| 1398 | default: |
Naoya Horiguchi | 354a336 | 2014-01-21 15:51:14 -0800 | [diff] [blame] | 1399 | /* |
| 1400 | * Permanent failure (-EBUSY, -ENOSYS, etc.): |
| 1401 | * unlike -EAGAIN case, the failed page is |
| 1402 | * removed from migration page list and not |
| 1403 | * retried in the next outer loop. |
| 1404 | */ |
Christoph Lameter | 2d1db3b | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 1405 | nr_failed++; |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1406 | break; |
Christoph Lameter | 2d1db3b | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 1407 | } |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1408 | } |
| 1409 | } |
Vlastimil Babka | f2f81fb | 2015-11-05 18:47:03 -0800 | [diff] [blame] | 1410 | nr_failed += retry; |
| 1411 | rc = nr_failed; |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 1412 | out: |
Mel Gorman | 5647bc2 | 2012-10-19 10:46:20 +0100 | [diff] [blame] | 1413 | if (nr_succeeded) |
| 1414 | count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded); |
| 1415 | if (nr_failed) |
| 1416 | count_vm_events(PGMIGRATE_FAIL, nr_failed); |
Mel Gorman | 7b2a2d4 | 2012-10-19 14:07:31 +0100 | [diff] [blame] | 1417 | trace_mm_migrate_pages(nr_succeeded, nr_failed, mode, reason); |
| 1418 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1419 | if (!swapwrite) |
| 1420 | current->flags &= ~PF_SWAPWRITE; |
| 1421 | |
Rafael Aquini | 78bd520 | 2012-12-11 16:02:31 -0800 | [diff] [blame] | 1422 | return rc; |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1423 | } |
| 1424 | |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1425 | #ifdef CONFIG_NUMA |
| 1426 | /* |
| 1427 | * Move a list of individual pages |
| 1428 | */ |
| 1429 | struct page_to_node { |
| 1430 | unsigned long addr; |
| 1431 | struct page *page; |
| 1432 | int node; |
| 1433 | int status; |
| 1434 | }; |
| 1435 | |
| 1436 | static struct page *new_page_node(struct page *p, unsigned long private, |
| 1437 | int **result) |
| 1438 | { |
| 1439 | struct page_to_node *pm = (struct page_to_node *)private; |
| 1440 | |
| 1441 | while (pm->node != MAX_NUMNODES && pm->page != p) |
| 1442 | pm++; |
| 1443 | |
| 1444 | if (pm->node == MAX_NUMNODES) |
| 1445 | return NULL; |
| 1446 | |
| 1447 | *result = &pm->status; |
| 1448 | |
Naoya Horiguchi | e632a93 | 2013-09-11 14:22:04 -0700 | [diff] [blame] | 1449 | if (PageHuge(p)) |
| 1450 | return alloc_huge_page_node(page_hstate(compound_head(p)), |
| 1451 | pm->node); |
Naoya Horiguchi | e8db67e | 2017-09-08 16:11:12 -0700 | [diff] [blame] | 1452 | else if (thp_migration_supported() && PageTransHuge(p)) { |
| 1453 | struct page *thp; |
| 1454 | |
| 1455 | thp = alloc_pages_node(pm->node, |
| 1456 | (GFP_TRANSHUGE | __GFP_THISNODE) & ~__GFP_RECLAIM, |
| 1457 | HPAGE_PMD_ORDER); |
| 1458 | if (!thp) |
| 1459 | return NULL; |
| 1460 | prep_transhuge_page(thp); |
| 1461 | return thp; |
| 1462 | } else |
Vlastimil Babka | 96db800 | 2015-09-08 15:03:50 -0700 | [diff] [blame] | 1463 | return __alloc_pages_node(pm->node, |
Johannes Weiner | e97ca8e5 | 2014-03-10 15:49:43 -0700 | [diff] [blame] | 1464 | GFP_HIGHUSER_MOVABLE | __GFP_THISNODE, 0); |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | /* |
| 1468 | * Move a set of pages as indicated in the pm array. The addr |
| 1469 | * field must be set to the virtual address of the page to be moved |
| 1470 | * and the node number must contain a valid target node. |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1471 | * The pm array ends with node = MAX_NUMNODES. |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1472 | */ |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1473 | static int do_move_page_to_node_array(struct mm_struct *mm, |
| 1474 | struct page_to_node *pm, |
| 1475 | int migrate_all) |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1476 | { |
| 1477 | int err; |
| 1478 | struct page_to_node *pp; |
| 1479 | LIST_HEAD(pagelist); |
| 1480 | |
| 1481 | down_read(&mm->mmap_sem); |
| 1482 | |
| 1483 | /* |
| 1484 | * Build a list of pages to migrate |
| 1485 | */ |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1486 | for (pp = pm; pp->node != MAX_NUMNODES; pp++) { |
| 1487 | struct vm_area_struct *vma; |
| 1488 | struct page *page; |
Naoya Horiguchi | e8db67e | 2017-09-08 16:11:12 -0700 | [diff] [blame] | 1489 | struct page *head; |
| 1490 | unsigned int follflags; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1491 | |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1492 | err = -EFAULT; |
| 1493 | vma = find_vma(mm, pp->addr); |
Gleb Natapov | 70384dc | 2010-10-26 14:22:07 -0700 | [diff] [blame] | 1494 | if (!vma || pp->addr < vma->vm_start || !vma_migratable(vma)) |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1495 | goto set_status; |
| 1496 | |
Kirill A. Shutemov | d899844 | 2015-09-04 15:47:53 -0700 | [diff] [blame] | 1497 | /* FOLL_DUMP to ignore special (like zero) pages */ |
Naoya Horiguchi | e8db67e | 2017-09-08 16:11:12 -0700 | [diff] [blame] | 1498 | follflags = FOLL_GET | FOLL_DUMP; |
| 1499 | if (!thp_migration_supported()) |
| 1500 | follflags |= FOLL_SPLIT; |
| 1501 | page = follow_page(vma, pp->addr, follflags); |
Linus Torvalds | 89f5b7d | 2008-06-20 11:18:25 -0700 | [diff] [blame] | 1502 | |
| 1503 | err = PTR_ERR(page); |
| 1504 | if (IS_ERR(page)) |
| 1505 | goto set_status; |
| 1506 | |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1507 | err = -ENOENT; |
| 1508 | if (!page) |
| 1509 | goto set_status; |
| 1510 | |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1511 | err = page_to_nid(page); |
| 1512 | |
| 1513 | if (err == pp->node) |
| 1514 | /* |
| 1515 | * Node already in the right place |
| 1516 | */ |
| 1517 | goto put_and_set; |
| 1518 | |
| 1519 | err = -EACCES; |
| 1520 | if (page_mapcount(page) > 1 && |
| 1521 | !migrate_all) |
| 1522 | goto put_and_set; |
| 1523 | |
Naoya Horiguchi | e632a93 | 2013-09-11 14:22:04 -0700 | [diff] [blame] | 1524 | if (PageHuge(page)) { |
Naoya Horiguchi | e8db67e | 2017-09-08 16:11:12 -0700 | [diff] [blame] | 1525 | if (PageHead(page)) { |
Naoya Horiguchi | e66f17f | 2015-02-11 15:25:22 -0800 | [diff] [blame] | 1526 | isolate_huge_page(page, &pagelist); |
Naoya Horiguchi | e8db67e | 2017-09-08 16:11:12 -0700 | [diff] [blame] | 1527 | err = 0; |
| 1528 | pp->page = page; |
| 1529 | } |
Naoya Horiguchi | e632a93 | 2013-09-11 14:22:04 -0700 | [diff] [blame] | 1530 | goto put_and_set; |
| 1531 | } |
| 1532 | |
Naoya Horiguchi | e8db67e | 2017-09-08 16:11:12 -0700 | [diff] [blame] | 1533 | pp->page = compound_head(page); |
| 1534 | head = compound_head(page); |
| 1535 | err = isolate_lru_page(head); |
KOSAKI Motohiro | 6d9c285 | 2009-12-14 17:58:11 -0800 | [diff] [blame] | 1536 | if (!err) { |
Naoya Horiguchi | e8db67e | 2017-09-08 16:11:12 -0700 | [diff] [blame] | 1537 | list_add_tail(&head->lru, &pagelist); |
| 1538 | mod_node_page_state(page_pgdat(head), |
| 1539 | NR_ISOLATED_ANON + page_is_file_cache(head), |
| 1540 | hpage_nr_pages(head)); |
KOSAKI Motohiro | 6d9c285 | 2009-12-14 17:58:11 -0800 | [diff] [blame] | 1541 | } |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1542 | put_and_set: |
| 1543 | /* |
| 1544 | * Either remove the duplicate refcount from |
| 1545 | * isolate_lru_page() or drop the page ref if it was |
| 1546 | * not isolated. |
| 1547 | */ |
| 1548 | put_page(page); |
| 1549 | set_status: |
| 1550 | pp->status = err; |
| 1551 | } |
| 1552 | |
Brice Goglin | e78bbfa | 2008-10-18 20:27:15 -0700 | [diff] [blame] | 1553 | err = 0; |
Minchan Kim | cf608ac | 2010-10-26 14:21:29 -0700 | [diff] [blame] | 1554 | if (!list_empty(&pagelist)) { |
David Rientjes | 68711a7 | 2014-06-04 16:08:25 -0700 | [diff] [blame] | 1555 | err = migrate_pages(&pagelist, new_page_node, NULL, |
Hugh Dickins | 9c620e2 | 2013-02-22 16:35:14 -0800 | [diff] [blame] | 1556 | (unsigned long)pm, MIGRATE_SYNC, MR_SYSCALL); |
Minchan Kim | cf608ac | 2010-10-26 14:21:29 -0700 | [diff] [blame] | 1557 | if (err) |
Naoya Horiguchi | e632a93 | 2013-09-11 14:22:04 -0700 | [diff] [blame] | 1558 | putback_movable_pages(&pagelist); |
Minchan Kim | cf608ac | 2010-10-26 14:21:29 -0700 | [diff] [blame] | 1559 | } |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1560 | |
| 1561 | up_read(&mm->mmap_sem); |
| 1562 | return err; |
| 1563 | } |
| 1564 | |
| 1565 | /* |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1566 | * Migrate an array of page address onto an array of nodes and fill |
| 1567 | * the corresponding array of status. |
| 1568 | */ |
Christoph Lameter | 3268c63 | 2012-03-21 16:34:06 -0700 | [diff] [blame] | 1569 | 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] | 1570 | unsigned long nr_pages, |
| 1571 | const void __user * __user *pages, |
| 1572 | const int __user *nodes, |
| 1573 | int __user *status, int flags) |
| 1574 | { |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1575 | struct page_to_node *pm; |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1576 | unsigned long chunk_nr_pages; |
| 1577 | unsigned long chunk_start; |
| 1578 | int err; |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1579 | |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1580 | err = -ENOMEM; |
| 1581 | pm = (struct page_to_node *)__get_free_page(GFP_KERNEL); |
| 1582 | if (!pm) |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1583 | goto out; |
Brice Goglin | 35282a2 | 2009-06-16 15:32:43 -0700 | [diff] [blame] | 1584 | |
| 1585 | migrate_prep(); |
| 1586 | |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1587 | /* |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1588 | * Store a chunk of page_to_node array in a page, |
| 1589 | * but keep the last one as a marker |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1590 | */ |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1591 | chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1; |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1592 | |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1593 | for (chunk_start = 0; |
| 1594 | chunk_start < nr_pages; |
| 1595 | chunk_start += chunk_nr_pages) { |
| 1596 | int j; |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1597 | |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1598 | if (chunk_start + chunk_nr_pages > nr_pages) |
| 1599 | chunk_nr_pages = nr_pages - chunk_start; |
| 1600 | |
| 1601 | /* fill the chunk pm with addrs and nodes from user-space */ |
| 1602 | for (j = 0; j < chunk_nr_pages; j++) { |
| 1603 | const void __user *p; |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1604 | int node; |
| 1605 | |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1606 | err = -EFAULT; |
| 1607 | if (get_user(p, pages + j + chunk_start)) |
| 1608 | goto out_pm; |
| 1609 | pm[j].addr = (unsigned long) p; |
| 1610 | |
| 1611 | if (get_user(node, nodes + j + chunk_start)) |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1612 | goto out_pm; |
| 1613 | |
| 1614 | err = -ENODEV; |
Linus Torvalds | 6f5a55f | 2010-02-05 16:16:50 -0800 | [diff] [blame] | 1615 | if (node < 0 || node >= MAX_NUMNODES) |
| 1616 | goto out_pm; |
| 1617 | |
Lai Jiangshan | 389162c | 2012-12-12 13:51:30 -0800 | [diff] [blame] | 1618 | if (!node_state(node, N_MEMORY)) |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1619 | goto out_pm; |
| 1620 | |
| 1621 | err = -EACCES; |
| 1622 | if (!node_isset(node, task_nodes)) |
| 1623 | goto out_pm; |
| 1624 | |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1625 | pm[j].node = node; |
| 1626 | } |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1627 | |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1628 | /* End marker for this chunk */ |
| 1629 | pm[chunk_nr_pages].node = MAX_NUMNODES; |
| 1630 | |
| 1631 | /* Migrate this chunk */ |
| 1632 | err = do_move_page_to_node_array(mm, pm, |
| 1633 | flags & MPOL_MF_MOVE_ALL); |
| 1634 | if (err < 0) |
| 1635 | goto out_pm; |
| 1636 | |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1637 | /* Return status information */ |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1638 | for (j = 0; j < chunk_nr_pages; j++) |
| 1639 | if (put_user(pm[j].status, status + j + chunk_start)) { |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1640 | err = -EFAULT; |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1641 | goto out_pm; |
| 1642 | } |
| 1643 | } |
| 1644 | err = 0; |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1645 | |
| 1646 | out_pm: |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1647 | free_page((unsigned long)pm); |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1648 | out: |
| 1649 | return err; |
| 1650 | } |
| 1651 | |
| 1652 | /* |
Brice Goglin | 2f007e7 | 2008-10-18 20:27:16 -0700 | [diff] [blame] | 1653 | * 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] | 1654 | */ |
Brice Goglin | 80bba12 | 2008-12-09 13:14:23 -0800 | [diff] [blame] | 1655 | static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages, |
| 1656 | const void __user **pages, int *status) |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1657 | { |
Brice Goglin | 2f007e7 | 2008-10-18 20:27:16 -0700 | [diff] [blame] | 1658 | unsigned long i; |
Brice Goglin | 2f007e7 | 2008-10-18 20:27:16 -0700 | [diff] [blame] | 1659 | |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1660 | down_read(&mm->mmap_sem); |
| 1661 | |
Brice Goglin | 2f007e7 | 2008-10-18 20:27:16 -0700 | [diff] [blame] | 1662 | for (i = 0; i < nr_pages; i++) { |
Brice Goglin | 80bba12 | 2008-12-09 13:14:23 -0800 | [diff] [blame] | 1663 | unsigned long addr = (unsigned long)(*pages); |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1664 | struct vm_area_struct *vma; |
| 1665 | struct page *page; |
KOSAKI Motohiro | c095adb | 2008-12-16 16:06:43 +0900 | [diff] [blame] | 1666 | int err = -EFAULT; |
Brice Goglin | 2f007e7 | 2008-10-18 20:27:16 -0700 | [diff] [blame] | 1667 | |
| 1668 | vma = find_vma(mm, addr); |
Gleb Natapov | 70384dc | 2010-10-26 14:22:07 -0700 | [diff] [blame] | 1669 | if (!vma || addr < vma->vm_start) |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1670 | goto set_status; |
| 1671 | |
Kirill A. Shutemov | d899844 | 2015-09-04 15:47:53 -0700 | [diff] [blame] | 1672 | /* FOLL_DUMP to ignore special (like zero) pages */ |
| 1673 | page = follow_page(vma, addr, FOLL_DUMP); |
Linus Torvalds | 89f5b7d | 2008-06-20 11:18:25 -0700 | [diff] [blame] | 1674 | |
| 1675 | err = PTR_ERR(page); |
| 1676 | if (IS_ERR(page)) |
| 1677 | goto set_status; |
| 1678 | |
Kirill A. Shutemov | d899844 | 2015-09-04 15:47:53 -0700 | [diff] [blame] | 1679 | err = page ? page_to_nid(page) : -ENOENT; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1680 | set_status: |
Brice Goglin | 80bba12 | 2008-12-09 13:14:23 -0800 | [diff] [blame] | 1681 | *status = err; |
| 1682 | |
| 1683 | pages++; |
| 1684 | status++; |
| 1685 | } |
| 1686 | |
| 1687 | up_read(&mm->mmap_sem); |
| 1688 | } |
| 1689 | |
| 1690 | /* |
| 1691 | * Determine the nodes of a user array of pages and store it in |
| 1692 | * a user array of status. |
| 1693 | */ |
| 1694 | static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages, |
| 1695 | const void __user * __user *pages, |
| 1696 | int __user *status) |
| 1697 | { |
| 1698 | #define DO_PAGES_STAT_CHUNK_NR 16 |
| 1699 | const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR]; |
| 1700 | int chunk_status[DO_PAGES_STAT_CHUNK_NR]; |
Brice Goglin | 80bba12 | 2008-12-09 13:14:23 -0800 | [diff] [blame] | 1701 | |
H. Peter Anvin | 87b8d1a | 2010-02-18 16:13:40 -0800 | [diff] [blame] | 1702 | while (nr_pages) { |
| 1703 | unsigned long chunk_nr; |
Brice Goglin | 80bba12 | 2008-12-09 13:14:23 -0800 | [diff] [blame] | 1704 | |
H. Peter Anvin | 87b8d1a | 2010-02-18 16:13:40 -0800 | [diff] [blame] | 1705 | chunk_nr = nr_pages; |
| 1706 | if (chunk_nr > DO_PAGES_STAT_CHUNK_NR) |
| 1707 | chunk_nr = DO_PAGES_STAT_CHUNK_NR; |
| 1708 | |
| 1709 | if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages))) |
| 1710 | break; |
Brice Goglin | 80bba12 | 2008-12-09 13:14:23 -0800 | [diff] [blame] | 1711 | |
| 1712 | do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status); |
| 1713 | |
H. Peter Anvin | 87b8d1a | 2010-02-18 16:13:40 -0800 | [diff] [blame] | 1714 | if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status))) |
| 1715 | break; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1716 | |
H. Peter Anvin | 87b8d1a | 2010-02-18 16:13:40 -0800 | [diff] [blame] | 1717 | pages += chunk_nr; |
| 1718 | status += chunk_nr; |
| 1719 | nr_pages -= chunk_nr; |
| 1720 | } |
| 1721 | return nr_pages ? -EFAULT : 0; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1722 | } |
| 1723 | |
| 1724 | /* |
| 1725 | * Move a list of pages in the address space of the currently executing |
| 1726 | * process. |
| 1727 | */ |
Heiko Carstens | 938bb9f | 2009-01-14 14:14:30 +0100 | [diff] [blame] | 1728 | SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages, |
| 1729 | const void __user * __user *, pages, |
| 1730 | const int __user *, nodes, |
| 1731 | int __user *, status, int, flags) |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1732 | { |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1733 | struct task_struct *task; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1734 | struct mm_struct *mm; |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1735 | int err; |
Christoph Lameter | 3268c63 | 2012-03-21 16:34:06 -0700 | [diff] [blame] | 1736 | nodemask_t task_nodes; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1737 | |
| 1738 | /* Check flags */ |
| 1739 | if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL)) |
| 1740 | return -EINVAL; |
| 1741 | |
| 1742 | if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE)) |
| 1743 | return -EPERM; |
| 1744 | |
| 1745 | /* Find the mm_struct */ |
Greg Thelen | a879bf5 | 2011-02-25 14:44:13 -0800 | [diff] [blame] | 1746 | rcu_read_lock(); |
Pavel Emelyanov | 228ebcb | 2007-10-18 23:40:16 -0700 | [diff] [blame] | 1747 | task = pid ? find_task_by_vpid(pid) : current; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1748 | if (!task) { |
Greg Thelen | a879bf5 | 2011-02-25 14:44:13 -0800 | [diff] [blame] | 1749 | rcu_read_unlock(); |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1750 | return -ESRCH; |
| 1751 | } |
Christoph Lameter | 3268c63 | 2012-03-21 16:34:06 -0700 | [diff] [blame] | 1752 | get_task_struct(task); |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1753 | |
| 1754 | /* |
| 1755 | * Check if this process has the right to modify the specified |
Linus Torvalds | 197e7e5 | 2017-08-20 13:26:27 -0700 | [diff] [blame] | 1756 | * process. Use the regular "ptrace_may_access()" checks. |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1757 | */ |
Linus Torvalds | 197e7e5 | 2017-08-20 13:26:27 -0700 | [diff] [blame] | 1758 | if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1759 | rcu_read_unlock(); |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1760 | err = -EPERM; |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1761 | goto out; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1762 | } |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1763 | rcu_read_unlock(); |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1764 | |
David Quigley | 86c3a76 | 2006-06-23 02:04:02 -0700 | [diff] [blame] | 1765 | err = security_task_movememory(task); |
| 1766 | if (err) |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1767 | goto out; |
David Quigley | 86c3a76 | 2006-06-23 02:04:02 -0700 | [diff] [blame] | 1768 | |
Christoph Lameter | 3268c63 | 2012-03-21 16:34:06 -0700 | [diff] [blame] | 1769 | task_nodes = cpuset_mems_allowed(task); |
| 1770 | mm = get_task_mm(task); |
| 1771 | put_task_struct(task); |
| 1772 | |
Sasha Levin | 6e8b09e | 2012-04-25 16:01:53 -0700 | [diff] [blame] | 1773 | if (!mm) |
| 1774 | return -EINVAL; |
| 1775 | |
| 1776 | if (nodes) |
| 1777 | err = do_pages_move(mm, task_nodes, nr_pages, pages, |
| 1778 | nodes, status, flags); |
| 1779 | else |
| 1780 | err = do_pages_stat(mm, nr_pages, pages, status); |
Christoph Lameter | 3268c63 | 2012-03-21 16:34:06 -0700 | [diff] [blame] | 1781 | |
| 1782 | mmput(mm); |
| 1783 | return err; |
David Quigley | 86c3a76 | 2006-06-23 02:04:02 -0700 | [diff] [blame] | 1784 | |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1785 | out: |
Christoph Lameter | 3268c63 | 2012-03-21 16:34:06 -0700 | [diff] [blame] | 1786 | put_task_struct(task); |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1787 | return err; |
| 1788 | } |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1789 | |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 1790 | #ifdef CONFIG_NUMA_BALANCING |
| 1791 | /* |
| 1792 | * Returns true if this is a safe migration target node for misplaced NUMA |
| 1793 | * pages. Currently it only checks the watermarks which crude |
| 1794 | */ |
| 1795 | static bool migrate_balanced_pgdat(struct pglist_data *pgdat, |
Mel Gorman | 3abef4e | 2013-02-22 16:34:27 -0800 | [diff] [blame] | 1796 | unsigned long nr_migrate_pages) |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 1797 | { |
| 1798 | int z; |
Mel Gorman | 599d0c9 | 2016-07-28 15:45:31 -0700 | [diff] [blame] | 1799 | |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 1800 | for (z = pgdat->nr_zones - 1; z >= 0; z--) { |
| 1801 | struct zone *zone = pgdat->node_zones + z; |
| 1802 | |
| 1803 | if (!populated_zone(zone)) |
| 1804 | continue; |
| 1805 | |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 1806 | /* Avoid waking kswapd by allocating pages_to_migrate pages. */ |
| 1807 | if (!zone_watermark_ok(zone, 0, |
| 1808 | high_wmark_pages(zone) + |
| 1809 | nr_migrate_pages, |
| 1810 | 0, 0)) |
| 1811 | continue; |
| 1812 | return true; |
| 1813 | } |
| 1814 | return false; |
| 1815 | } |
| 1816 | |
| 1817 | static struct page *alloc_misplaced_dst_page(struct page *page, |
| 1818 | unsigned long data, |
| 1819 | int **result) |
| 1820 | { |
| 1821 | int nid = (int) data; |
| 1822 | struct page *newpage; |
| 1823 | |
Vlastimil Babka | 96db800 | 2015-09-08 15:03:50 -0700 | [diff] [blame] | 1824 | newpage = __alloc_pages_node(nid, |
Johannes Weiner | e97ca8e5 | 2014-03-10 15:49:43 -0700 | [diff] [blame] | 1825 | (GFP_HIGHUSER_MOVABLE | |
| 1826 | __GFP_THISNODE | __GFP_NOMEMALLOC | |
| 1827 | __GFP_NORETRY | __GFP_NOWARN) & |
Mel Gorman | 8479eba | 2016-02-26 15:19:31 -0800 | [diff] [blame] | 1828 | ~__GFP_RECLAIM, 0); |
Hillf Danton | bac0382 | 2012-11-27 14:46:24 +0000 | [diff] [blame] | 1829 | |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 1830 | return newpage; |
| 1831 | } |
| 1832 | |
| 1833 | /* |
Mel Gorman | a8f6077 | 2012-11-14 21:41:46 +0000 | [diff] [blame] | 1834 | * page migration rate limiting control. |
| 1835 | * Do not migrate more than @pages_to_migrate in a @migrate_interval_millisecs |
| 1836 | * window of time. Default here says do not migrate more than 1280M per second. |
| 1837 | */ |
| 1838 | static unsigned int migrate_interval_millisecs __read_mostly = 100; |
| 1839 | static unsigned int ratelimit_pages __read_mostly = 128 << (20 - PAGE_SHIFT); |
| 1840 | |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1841 | /* Returns true if the node is migrate rate-limited after the update */ |
Mel Gorman | 1c30e01 | 2014-01-21 15:50:58 -0800 | [diff] [blame] | 1842 | static bool numamigrate_update_ratelimit(pg_data_t *pgdat, |
| 1843 | unsigned long nr_pages) |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1844 | { |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1845 | /* |
| 1846 | * Rate-limit the amount of data that is being migrated to a node. |
| 1847 | * Optimal placement is no good if the memory bus is saturated and |
| 1848 | * all the time is being spent migrating! |
| 1849 | */ |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1850 | if (time_after(jiffies, pgdat->numabalancing_migrate_next_window)) { |
Mel Gorman | 1c5e9c2 | 2014-01-21 15:50:59 -0800 | [diff] [blame] | 1851 | spin_lock(&pgdat->numabalancing_migrate_lock); |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1852 | pgdat->numabalancing_migrate_nr_pages = 0; |
| 1853 | pgdat->numabalancing_migrate_next_window = jiffies + |
| 1854 | msecs_to_jiffies(migrate_interval_millisecs); |
Mel Gorman | 1c5e9c2 | 2014-01-21 15:50:59 -0800 | [diff] [blame] | 1855 | spin_unlock(&pgdat->numabalancing_migrate_lock); |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1856 | } |
Mel Gorman | af1839d | 2014-01-21 15:51:01 -0800 | [diff] [blame] | 1857 | if (pgdat->numabalancing_migrate_nr_pages > ratelimit_pages) { |
| 1858 | trace_mm_numa_migrate_ratelimit(current, pgdat->node_id, |
| 1859 | nr_pages); |
Mel Gorman | 1c5e9c2 | 2014-01-21 15:50:59 -0800 | [diff] [blame] | 1860 | return true; |
Mel Gorman | af1839d | 2014-01-21 15:51:01 -0800 | [diff] [blame] | 1861 | } |
Mel Gorman | 1c5e9c2 | 2014-01-21 15:50:59 -0800 | [diff] [blame] | 1862 | |
| 1863 | /* |
| 1864 | * This is an unlocked non-atomic update so errors are possible. |
| 1865 | * The consequences are failing to migrate when we potentiall should |
| 1866 | * have which is not severe enough to warrant locking. If it is ever |
| 1867 | * a problem, it can be converted to a per-cpu counter. |
| 1868 | */ |
| 1869 | pgdat->numabalancing_migrate_nr_pages += nr_pages; |
| 1870 | return false; |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1871 | } |
| 1872 | |
Mel Gorman | 1c30e01 | 2014-01-21 15:50:58 -0800 | [diff] [blame] | 1873 | static int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page) |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1874 | { |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 1875 | int page_lru; |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1876 | |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 1877 | VM_BUG_ON_PAGE(compound_order(page) && !PageTransHuge(page), page); |
Mel Gorman | 3abef4e | 2013-02-22 16:34:27 -0800 | [diff] [blame] | 1878 | |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1879 | /* Avoid migrating to a node that is nearly full */ |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 1880 | if (!migrate_balanced_pgdat(pgdat, 1UL << compound_order(page))) |
| 1881 | return 0; |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1882 | |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 1883 | if (isolate_lru_page(page)) |
| 1884 | return 0; |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1885 | |
| 1886 | /* |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 1887 | * migrate_misplaced_transhuge_page() skips page migration's usual |
| 1888 | * check on page_count(), so we must do it here, now that the page |
| 1889 | * has been isolated: a GUP pin, or any other pin, prevents migration. |
| 1890 | * The expected page count is 3: 1 for page's mapcount and 1 for the |
| 1891 | * caller's pin and 1 for the reference taken by isolate_lru_page(). |
| 1892 | */ |
| 1893 | if (PageTransHuge(page) && page_count(page) != 3) { |
| 1894 | putback_lru_page(page); |
| 1895 | return 0; |
| 1896 | } |
| 1897 | |
| 1898 | page_lru = page_is_file_cache(page); |
Mel Gorman | 599d0c9 | 2016-07-28 15:45:31 -0700 | [diff] [blame] | 1899 | mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + page_lru, |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 1900 | hpage_nr_pages(page)); |
| 1901 | |
| 1902 | /* |
| 1903 | * Isolating the page has taken another reference, so the |
| 1904 | * caller's reference can be safely dropped without the page |
| 1905 | * disappearing underneath us during migration. |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1906 | */ |
| 1907 | put_page(page); |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 1908 | return 1; |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
Mel Gorman | de466bd | 2013-12-18 17:08:42 -0800 | [diff] [blame] | 1911 | bool pmd_trans_migrating(pmd_t pmd) |
| 1912 | { |
| 1913 | struct page *page = pmd_page(pmd); |
| 1914 | return PageLocked(page); |
| 1915 | } |
| 1916 | |
Mel Gorman | a8f6077 | 2012-11-14 21:41:46 +0000 | [diff] [blame] | 1917 | /* |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 1918 | * Attempt to migrate a misplaced page to the specified destination |
| 1919 | * node. Caller is expected to have an elevated reference count on |
| 1920 | * the page that will be dropped by this function before returning. |
| 1921 | */ |
Mel Gorman | 1bc115d | 2013-10-07 11:29:05 +0100 | [diff] [blame] | 1922 | int migrate_misplaced_page(struct page *page, struct vm_area_struct *vma, |
| 1923 | int node) |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 1924 | { |
Mel Gorman | a8f6077 | 2012-11-14 21:41:46 +0000 | [diff] [blame] | 1925 | pg_data_t *pgdat = NODE_DATA(node); |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 1926 | int isolated; |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1927 | int nr_remaining; |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 1928 | LIST_HEAD(migratepages); |
| 1929 | |
| 1930 | /* |
Mel Gorman | 1bc115d | 2013-10-07 11:29:05 +0100 | [diff] [blame] | 1931 | * Don't migrate file pages that are mapped in multiple processes |
| 1932 | * with execute permissions as they are probably shared libraries. |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 1933 | */ |
Mel Gorman | 1bc115d | 2013-10-07 11:29:05 +0100 | [diff] [blame] | 1934 | if (page_mapcount(page) != 1 && page_is_file_cache(page) && |
| 1935 | (vma->vm_flags & VM_EXEC)) |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 1936 | goto out; |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 1937 | |
Mel Gorman | a8f6077 | 2012-11-14 21:41:46 +0000 | [diff] [blame] | 1938 | /* |
| 1939 | * Rate-limit the amount of data that is being migrated to a node. |
| 1940 | * Optimal placement is no good if the memory bus is saturated and |
| 1941 | * all the time is being spent migrating! |
| 1942 | */ |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 1943 | if (numamigrate_update_ratelimit(pgdat, 1)) |
Mel Gorman | a8f6077 | 2012-11-14 21:41:46 +0000 | [diff] [blame] | 1944 | goto out; |
Mel Gorman | a8f6077 | 2012-11-14 21:41:46 +0000 | [diff] [blame] | 1945 | |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1946 | isolated = numamigrate_isolate_page(pgdat, page); |
| 1947 | if (!isolated) |
| 1948 | goto out; |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 1949 | |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1950 | list_add(&page->lru, &migratepages); |
Hugh Dickins | 9c620e2 | 2013-02-22 16:35:14 -0800 | [diff] [blame] | 1951 | nr_remaining = migrate_pages(&migratepages, alloc_misplaced_dst_page, |
David Rientjes | 68711a7 | 2014-06-04 16:08:25 -0700 | [diff] [blame] | 1952 | NULL, node, MIGRATE_ASYNC, |
| 1953 | MR_NUMA_MISPLACED); |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1954 | if (nr_remaining) { |
Joonsoo Kim | 59c82b7 | 2014-01-21 15:51:17 -0800 | [diff] [blame] | 1955 | if (!list_empty(&migratepages)) { |
| 1956 | list_del(&page->lru); |
Mel Gorman | 599d0c9 | 2016-07-28 15:45:31 -0700 | [diff] [blame] | 1957 | dec_node_page_state(page, NR_ISOLATED_ANON + |
Joonsoo Kim | 59c82b7 | 2014-01-21 15:51:17 -0800 | [diff] [blame] | 1958 | page_is_file_cache(page)); |
| 1959 | putback_lru_page(page); |
| 1960 | } |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1961 | isolated = 0; |
| 1962 | } else |
| 1963 | count_vm_numa_event(NUMA_PAGE_MIGRATE); |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 1964 | BUG_ON(!list_empty(&migratepages)); |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 1965 | return isolated; |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 1966 | |
| 1967 | out: |
| 1968 | put_page(page); |
| 1969 | return 0; |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 1970 | } |
Mel Gorman | 220018d | 2012-12-05 09:32:56 +0000 | [diff] [blame] | 1971 | #endif /* CONFIG_NUMA_BALANCING */ |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1972 | |
Mel Gorman | 220018d | 2012-12-05 09:32:56 +0000 | [diff] [blame] | 1973 | #if defined(CONFIG_NUMA_BALANCING) && defined(CONFIG_TRANSPARENT_HUGEPAGE) |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 1974 | /* |
| 1975 | * Migrates a THP to a given target node. page must be locked and is unlocked |
| 1976 | * before returning. |
| 1977 | */ |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1978 | int migrate_misplaced_transhuge_page(struct mm_struct *mm, |
| 1979 | struct vm_area_struct *vma, |
| 1980 | pmd_t *pmd, pmd_t entry, |
| 1981 | unsigned long address, |
| 1982 | struct page *page, int node) |
| 1983 | { |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 1984 | spinlock_t *ptl; |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1985 | pg_data_t *pgdat = NODE_DATA(node); |
| 1986 | int isolated = 0; |
| 1987 | struct page *new_page = NULL; |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1988 | int page_lru = page_is_file_cache(page); |
Mel Gorman | f714f4f | 2013-12-18 17:08:33 -0800 | [diff] [blame] | 1989 | unsigned long mmun_start = address & HPAGE_PMD_MASK; |
| 1990 | unsigned long mmun_end = mmun_start + HPAGE_PMD_SIZE; |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1991 | |
| 1992 | /* |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1993 | * Rate-limit the amount of data that is being migrated to a node. |
| 1994 | * Optimal placement is no good if the memory bus is saturated and |
| 1995 | * all the time is being spent migrating! |
| 1996 | */ |
Mel Gorman | d28d4335 | 2012-11-29 09:24:36 +0000 | [diff] [blame] | 1997 | if (numamigrate_update_ratelimit(pgdat, HPAGE_PMD_NR)) |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1998 | goto out_dropref; |
| 1999 | |
| 2000 | new_page = alloc_pages_node(node, |
Vlastimil Babka | 2516035 | 2016-07-28 15:49:25 -0700 | [diff] [blame] | 2001 | (GFP_TRANSHUGE_LIGHT | __GFP_THISNODE), |
Johannes Weiner | e97ca8e5 | 2014-03-10 15:49:43 -0700 | [diff] [blame] | 2002 | HPAGE_PMD_ORDER); |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 2003 | if (!new_page) |
| 2004 | goto out_fail; |
Kirill A. Shutemov | 9a98225 | 2016-01-15 16:54:17 -0800 | [diff] [blame] | 2005 | prep_transhuge_page(new_page); |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 2006 | |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2007 | isolated = numamigrate_isolate_page(pgdat, page); |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 2008 | if (!isolated) { |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2009 | put_page(new_page); |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 2010 | goto out_fail; |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2011 | } |
Mel Gorman | b0943d6 | 2013-12-18 17:08:46 -0800 | [diff] [blame] | 2012 | |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2013 | /* Prepare a page as a migration target */ |
Kirill A. Shutemov | 48c935a | 2016-01-15 16:51:24 -0800 | [diff] [blame] | 2014 | __SetPageLocked(new_page); |
Shaohua Li | d44d363 | 2017-05-03 14:52:26 -0700 | [diff] [blame] | 2015 | if (PageSwapBacked(page)) |
| 2016 | __SetPageSwapBacked(new_page); |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2017 | |
| 2018 | /* anon mapping, we can simply copy page->mapping to the new page: */ |
| 2019 | new_page->mapping = page->mapping; |
| 2020 | new_page->index = page->index; |
| 2021 | migrate_page_copy(new_page, page); |
| 2022 | WARN_ON(PageLRU(new_page)); |
| 2023 | |
| 2024 | /* Recheck the target PMD */ |
Mel Gorman | f714f4f | 2013-12-18 17:08:33 -0800 | [diff] [blame] | 2025 | mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 2026 | ptl = pmd_lock(mm, pmd); |
Will Deacon | f4e177d | 2017-07-10 15:48:31 -0700 | [diff] [blame] | 2027 | if (unlikely(!pmd_same(*pmd, entry) || !page_ref_freeze(page, 2))) { |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 2028 | spin_unlock(ptl); |
Mel Gorman | f714f4f | 2013-12-18 17:08:33 -0800 | [diff] [blame] | 2029 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2030 | |
| 2031 | /* Reverse changes made by migrate_page_copy() */ |
| 2032 | if (TestClearPageActive(new_page)) |
| 2033 | SetPageActive(page); |
| 2034 | if (TestClearPageUnevictable(new_page)) |
| 2035 | SetPageUnevictable(page); |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2036 | |
| 2037 | unlock_page(new_page); |
| 2038 | put_page(new_page); /* Free it */ |
| 2039 | |
Mel Gorman | a54a407 | 2013-10-07 11:28:46 +0100 | [diff] [blame] | 2040 | /* Retake the callers reference and putback on LRU */ |
| 2041 | get_page(page); |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2042 | putback_lru_page(page); |
Mel Gorman | 599d0c9 | 2016-07-28 15:45:31 -0700 | [diff] [blame] | 2043 | mod_node_page_state(page_pgdat(page), |
Mel Gorman | a54a407 | 2013-10-07 11:28:46 +0100 | [diff] [blame] | 2044 | NR_ISOLATED_ANON + page_lru, -HPAGE_PMD_NR); |
Mel Gorman | eb4489f6 | 2013-12-18 17:08:39 -0800 | [diff] [blame] | 2045 | |
| 2046 | goto out_unlock; |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2047 | } |
| 2048 | |
Kirill A. Shutemov | 1010245 | 2016-07-26 15:25:29 -0700 | [diff] [blame] | 2049 | entry = mk_huge_pmd(new_page, vma->vm_page_prot); |
Mel Gorman | 2b4847e | 2013-12-18 17:08:32 -0800 | [diff] [blame] | 2050 | entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2051 | |
Mel Gorman | 2b4847e | 2013-12-18 17:08:32 -0800 | [diff] [blame] | 2052 | /* |
| 2053 | * Clear the old entry under pagetable lock and establish the new PTE. |
| 2054 | * Any parallel GUP will either observe the old page blocking on the |
| 2055 | * page lock, block on the page table lock or observe the new page. |
| 2056 | * The SetPageUptodate on the new page and page_add_new_anon_rmap |
| 2057 | * guarantee the copy is visible before the pagetable update. |
| 2058 | */ |
Mel Gorman | f714f4f | 2013-12-18 17:08:33 -0800 | [diff] [blame] | 2059 | flush_cache_range(vma, mmun_start, mmun_end); |
Kirill A. Shutemov | d281ee6 | 2016-01-15 16:52:16 -0800 | [diff] [blame] | 2060 | page_add_anon_rmap(new_page, vma, mmun_start, true); |
Aneesh Kumar K.V | 8809aa2 | 2015-06-24 16:57:44 -0700 | [diff] [blame] | 2061 | pmdp_huge_clear_flush_notify(vma, mmun_start, pmd); |
Mel Gorman | f714f4f | 2013-12-18 17:08:33 -0800 | [diff] [blame] | 2062 | set_pmd_at(mm, mmun_start, pmd, entry); |
Stephen Rothwell | ce4a9cc | 2012-12-10 19:50:57 +1100 | [diff] [blame] | 2063 | update_mmu_cache_pmd(vma, address, &entry); |
Mel Gorman | 2b4847e | 2013-12-18 17:08:32 -0800 | [diff] [blame] | 2064 | |
Will Deacon | f4e177d | 2017-07-10 15:48:31 -0700 | [diff] [blame] | 2065 | page_ref_unfreeze(page, 2); |
Hugh Dickins | 51afb12 | 2015-11-05 18:49:37 -0800 | [diff] [blame] | 2066 | mlock_migrate_page(new_page, page); |
Kirill A. Shutemov | d281ee6 | 2016-01-15 16:52:16 -0800 | [diff] [blame] | 2067 | page_remove_rmap(page, true); |
Vlastimil Babka | 7cd12b4 | 2016-03-15 14:56:18 -0700 | [diff] [blame] | 2068 | set_page_owner_migrate_reason(new_page, MR_NUMA_MISPLACED); |
Mel Gorman | 2b4847e | 2013-12-18 17:08:32 -0800 | [diff] [blame] | 2069 | |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 2070 | spin_unlock(ptl); |
Mel Gorman | f714f4f | 2013-12-18 17:08:33 -0800 | [diff] [blame] | 2071 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2072 | |
Mel Gorman | 11de992 | 2014-06-04 16:07:41 -0700 | [diff] [blame] | 2073 | /* Take an "isolate" reference and put new page on the LRU. */ |
| 2074 | get_page(new_page); |
| 2075 | putback_lru_page(new_page); |
| 2076 | |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2077 | unlock_page(new_page); |
| 2078 | unlock_page(page); |
| 2079 | put_page(page); /* Drop the rmap reference */ |
| 2080 | put_page(page); /* Drop the LRU isolation reference */ |
| 2081 | |
| 2082 | count_vm_events(PGMIGRATE_SUCCESS, HPAGE_PMD_NR); |
| 2083 | count_vm_numa_events(NUMA_PAGE_MIGRATE, HPAGE_PMD_NR); |
| 2084 | |
Mel Gorman | 599d0c9 | 2016-07-28 15:45:31 -0700 | [diff] [blame] | 2085 | mod_node_page_state(page_pgdat(page), |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2086 | NR_ISOLATED_ANON + page_lru, |
| 2087 | -HPAGE_PMD_NR); |
| 2088 | return isolated; |
| 2089 | |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 2090 | out_fail: |
| 2091 | count_vm_events(PGMIGRATE_FAIL, HPAGE_PMD_NR); |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2092 | out_dropref: |
Mel Gorman | 2b4847e | 2013-12-18 17:08:32 -0800 | [diff] [blame] | 2093 | ptl = pmd_lock(mm, pmd); |
| 2094 | if (pmd_same(*pmd, entry)) { |
Mel Gorman | 4d94246 | 2015-02-12 14:58:28 -0800 | [diff] [blame] | 2095 | entry = pmd_modify(entry, vma->vm_page_prot); |
Mel Gorman | f714f4f | 2013-12-18 17:08:33 -0800 | [diff] [blame] | 2096 | set_pmd_at(mm, mmun_start, pmd, entry); |
Mel Gorman | 2b4847e | 2013-12-18 17:08:32 -0800 | [diff] [blame] | 2097 | update_mmu_cache_pmd(vma, address, &entry); |
| 2098 | } |
| 2099 | spin_unlock(ptl); |
Mel Gorman | a54a407 | 2013-10-07 11:28:46 +0100 | [diff] [blame] | 2100 | |
Mel Gorman | eb4489f6 | 2013-12-18 17:08:39 -0800 | [diff] [blame] | 2101 | out_unlock: |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 2102 | unlock_page(page); |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2103 | put_page(page); |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2104 | return 0; |
| 2105 | } |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 2106 | #endif /* CONFIG_NUMA_BALANCING */ |
| 2107 | |
| 2108 | #endif /* CONFIG_NUMA */ |