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 | |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 431 | /* |
| 432 | * ZONE_DEVICE pages have 1 refcount always held by their device |
| 433 | * |
| 434 | * Note that DAX memory will never reach that point as it does not have |
| 435 | * the MEMORY_DEVICE_ALLOW_MIGRATE flag set (see memory_hotplug.h). |
| 436 | */ |
| 437 | expected_count += is_zone_device_page(page); |
| 438 | |
Christoph Lameter | 6c5240a | 2006-06-23 02:03:37 -0700 | [diff] [blame] | 439 | if (!mapping) { |
Christoph Lameter | 0e8c7d0 | 2007-04-23 14:41:09 -0700 | [diff] [blame] | 440 | /* Anonymous page without mapping */ |
Benjamin LaHaise | 8e321fe | 2013-12-21 17:56:08 -0500 | [diff] [blame] | 441 | if (page_count(page) != expected_count) |
Christoph Lameter | 6c5240a | 2006-06-23 02:03:37 -0700 | [diff] [blame] | 442 | return -EAGAIN; |
Hugh Dickins | cf4b769 | 2015-11-05 18:50:02 -0800 | [diff] [blame] | 443 | |
| 444 | /* No turning back from here */ |
Hugh Dickins | cf4b769 | 2015-11-05 18:50:02 -0800 | [diff] [blame] | 445 | newpage->index = page->index; |
| 446 | newpage->mapping = page->mapping; |
| 447 | if (PageSwapBacked(page)) |
Hugh Dickins | fa9949d | 2016-05-19 17:12:41 -0700 | [diff] [blame] | 448 | __SetPageSwapBacked(newpage); |
Hugh Dickins | cf4b769 | 2015-11-05 18:50:02 -0800 | [diff] [blame] | 449 | |
Rafael Aquini | 78bd520 | 2012-12-11 16:02:31 -0800 | [diff] [blame] | 450 | return MIGRATEPAGE_SUCCESS; |
Christoph Lameter | 6c5240a | 2006-06-23 02:03:37 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Hugh Dickins | 42cb14b | 2015-11-05 18:50:05 -0800 | [diff] [blame] | 453 | oldzone = page_zone(page); |
| 454 | newzone = page_zone(newpage); |
| 455 | |
Nick Piggin | 19fd623 | 2008-07-25 19:45:32 -0700 | [diff] [blame] | 456 | spin_lock_irq(&mapping->tree_lock); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 457 | |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 458 | pslot = radix_tree_lookup_slot(&mapping->page_tree, |
| 459 | page_index(page)); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 460 | |
Benjamin LaHaise | 8e321fe | 2013-12-21 17:56:08 -0500 | [diff] [blame] | 461 | expected_count += 1 + page_has_private(page); |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 462 | if (page_count(page) != expected_count || |
Mel Gorman | 29c1f67 | 2011-01-13 15:47:21 -0800 | [diff] [blame] | 463 | radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) { |
Nick Piggin | 19fd623 | 2008-07-25 19:45:32 -0700 | [diff] [blame] | 464 | spin_unlock_irq(&mapping->tree_lock); |
Christoph Lameter | e23ca00 | 2006-04-10 22:52:57 -0700 | [diff] [blame] | 465 | return -EAGAIN; |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 466 | } |
| 467 | |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 468 | if (!page_ref_freeze(page, expected_count)) { |
Nick Piggin | 19fd623 | 2008-07-25 19:45:32 -0700 | [diff] [blame] | 469 | spin_unlock_irq(&mapping->tree_lock); |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 470 | return -EAGAIN; |
| 471 | } |
| 472 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 473 | /* |
Mel Gorman | b969c4a | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 474 | * In the async migration case of moving a page with buffers, lock the |
| 475 | * buffers using trylock before the mapping is moved. If the mapping |
| 476 | * was moved, we later failed to lock the buffers and could not move |
| 477 | * the mapping back due to an elevated page count, we would have to |
| 478 | * block waiting on other references to be dropped. |
| 479 | */ |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 480 | if (mode == MIGRATE_ASYNC && head && |
| 481 | !buffer_migrate_lock_buffers(head, mode)) { |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 482 | page_ref_unfreeze(page, expected_count); |
Mel Gorman | b969c4a | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 483 | spin_unlock_irq(&mapping->tree_lock); |
| 484 | return -EAGAIN; |
| 485 | } |
| 486 | |
| 487 | /* |
Hugh Dickins | cf4b769 | 2015-11-05 18:50:02 -0800 | [diff] [blame] | 488 | * Now we know that no one else is looking at the page: |
| 489 | * no turning back from here. |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 490 | */ |
Hugh Dickins | cf4b769 | 2015-11-05 18:50:02 -0800 | [diff] [blame] | 491 | newpage->index = page->index; |
| 492 | newpage->mapping = page->mapping; |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 493 | get_page(newpage); /* add cache reference */ |
Nicholas Piggin | 6326fec | 2016-12-25 13:00:29 +1000 | [diff] [blame] | 494 | if (PageSwapBacked(page)) { |
| 495 | __SetPageSwapBacked(newpage); |
| 496 | if (PageSwapCache(page)) { |
| 497 | SetPageSwapCache(newpage); |
| 498 | set_page_private(newpage, page_private(page)); |
| 499 | } |
| 500 | } else { |
| 501 | VM_BUG_ON_PAGE(PageSwapCache(page), page); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 502 | } |
| 503 | |
Hugh Dickins | 42cb14b | 2015-11-05 18:50:05 -0800 | [diff] [blame] | 504 | /* Move dirty while page refs frozen and newpage not yet exposed */ |
| 505 | dirty = PageDirty(page); |
| 506 | if (dirty) { |
| 507 | ClearPageDirty(page); |
| 508 | SetPageDirty(newpage); |
| 509 | } |
| 510 | |
Johannes Weiner | 6d75f36 | 2016-12-12 16:43:43 -0800 | [diff] [blame] | 511 | radix_tree_replace_slot(&mapping->page_tree, pslot, newpage); |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 512 | |
| 513 | /* |
Jacobo Giralt | 937a94c | 2012-01-10 15:07:11 -0800 | [diff] [blame] | 514 | * Drop cache reference from old page by unfreezing |
| 515 | * to one less reference. |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 516 | * We know this isn't the last reference. |
| 517 | */ |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 518 | page_ref_unfreeze(page, expected_count - 1); |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 519 | |
Hugh Dickins | 42cb14b | 2015-11-05 18:50:05 -0800 | [diff] [blame] | 520 | spin_unlock(&mapping->tree_lock); |
| 521 | /* Leave irq disabled to prevent preemption while updating stats */ |
| 522 | |
Christoph Lameter | 0e8c7d0 | 2007-04-23 14:41:09 -0700 | [diff] [blame] | 523 | /* |
| 524 | * If moved to a different zone then also account |
| 525 | * the page for that zone. Other VM counters will be |
| 526 | * taken care of when we establish references to the |
| 527 | * new page and drop references to the old page. |
| 528 | * |
| 529 | * Note that anonymous pages are accounted for |
Mel Gorman | 4b9d0fa | 2016-07-28 15:46:17 -0700 | [diff] [blame] | 530 | * via NR_FILE_PAGES and NR_ANON_MAPPED if they |
Christoph Lameter | 0e8c7d0 | 2007-04-23 14:41:09 -0700 | [diff] [blame] | 531 | * are mapped to swap space. |
| 532 | */ |
Hugh Dickins | 42cb14b | 2015-11-05 18:50:05 -0800 | [diff] [blame] | 533 | if (newzone != oldzone) { |
Mel Gorman | 11fb998 | 2016-07-28 15:46:20 -0700 | [diff] [blame] | 534 | __dec_node_state(oldzone->zone_pgdat, NR_FILE_PAGES); |
| 535 | __inc_node_state(newzone->zone_pgdat, NR_FILE_PAGES); |
Hugh Dickins | 42cb14b | 2015-11-05 18:50:05 -0800 | [diff] [blame] | 536 | if (PageSwapBacked(page) && !PageSwapCache(page)) { |
Mel Gorman | 11fb998 | 2016-07-28 15:46:20 -0700 | [diff] [blame] | 537 | __dec_node_state(oldzone->zone_pgdat, NR_SHMEM); |
| 538 | __inc_node_state(newzone->zone_pgdat, NR_SHMEM); |
Hugh Dickins | 42cb14b | 2015-11-05 18:50:05 -0800 | [diff] [blame] | 539 | } |
| 540 | if (dirty && mapping_cap_account_dirty(mapping)) { |
Mel Gorman | 11fb998 | 2016-07-28 15:46:20 -0700 | [diff] [blame] | 541 | __dec_node_state(oldzone->zone_pgdat, NR_FILE_DIRTY); |
Mel Gorman | 5a1c84b | 2016-07-28 15:47:31 -0700 | [diff] [blame] | 542 | __dec_zone_state(oldzone, NR_ZONE_WRITE_PENDING); |
Mel Gorman | 11fb998 | 2016-07-28 15:46:20 -0700 | [diff] [blame] | 543 | __inc_node_state(newzone->zone_pgdat, NR_FILE_DIRTY); |
Mel Gorman | 5a1c84b | 2016-07-28 15:47:31 -0700 | [diff] [blame] | 544 | __inc_zone_state(newzone, NR_ZONE_WRITE_PENDING); |
Hugh Dickins | 42cb14b | 2015-11-05 18:50:05 -0800 | [diff] [blame] | 545 | } |
KOSAKI Motohiro | 4b02108 | 2009-09-21 17:01:33 -0700 | [diff] [blame] | 546 | } |
Hugh Dickins | 42cb14b | 2015-11-05 18:50:05 -0800 | [diff] [blame] | 547 | local_irq_enable(); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 548 | |
Rafael Aquini | 78bd520 | 2012-12-11 16:02:31 -0800 | [diff] [blame] | 549 | return MIGRATEPAGE_SUCCESS; |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 550 | } |
Richard Weinberger | 1118dce | 2016-06-16 23:26:14 +0200 | [diff] [blame] | 551 | EXPORT_SYMBOL(migrate_page_move_mapping); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 552 | |
| 553 | /* |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 554 | * The expected number of remaining references is the same as that |
| 555 | * of migrate_page_move_mapping(). |
| 556 | */ |
| 557 | int migrate_huge_page_move_mapping(struct address_space *mapping, |
| 558 | struct page *newpage, struct page *page) |
| 559 | { |
| 560 | int expected_count; |
| 561 | void **pslot; |
| 562 | |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 563 | spin_lock_irq(&mapping->tree_lock); |
| 564 | |
| 565 | pslot = radix_tree_lookup_slot(&mapping->page_tree, |
| 566 | page_index(page)); |
| 567 | |
| 568 | expected_count = 2 + page_has_private(page); |
| 569 | if (page_count(page) != expected_count || |
Mel Gorman | 29c1f67 | 2011-01-13 15:47:21 -0800 | [diff] [blame] | 570 | radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) { |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 571 | spin_unlock_irq(&mapping->tree_lock); |
| 572 | return -EAGAIN; |
| 573 | } |
| 574 | |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 575 | if (!page_ref_freeze(page, expected_count)) { |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 576 | spin_unlock_irq(&mapping->tree_lock); |
| 577 | return -EAGAIN; |
| 578 | } |
| 579 | |
Hugh Dickins | cf4b769 | 2015-11-05 18:50:02 -0800 | [diff] [blame] | 580 | newpage->index = page->index; |
| 581 | newpage->mapping = page->mapping; |
Johannes Weiner | 6a93ca8 | 2016-03-15 14:57:19 -0700 | [diff] [blame] | 582 | |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 583 | get_page(newpage); |
| 584 | |
Johannes Weiner | 6d75f36 | 2016-12-12 16:43:43 -0800 | [diff] [blame] | 585 | radix_tree_replace_slot(&mapping->page_tree, pslot, newpage); |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 586 | |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 587 | page_ref_unfreeze(page, expected_count - 1); |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 588 | |
| 589 | spin_unlock_irq(&mapping->tree_lock); |
Johannes Weiner | 6a93ca8 | 2016-03-15 14:57:19 -0700 | [diff] [blame] | 590 | |
Rafael Aquini | 78bd520 | 2012-12-11 16:02:31 -0800 | [diff] [blame] | 591 | return MIGRATEPAGE_SUCCESS; |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | /* |
Dave Hansen | 30b0a10 | 2013-11-21 14:31:58 -0800 | [diff] [blame] | 595 | * Gigantic pages are so large that we do not guarantee that page++ pointer |
| 596 | * arithmetic will work across the entire page. We need something more |
| 597 | * specialized. |
| 598 | */ |
| 599 | static void __copy_gigantic_page(struct page *dst, struct page *src, |
| 600 | int nr_pages) |
| 601 | { |
| 602 | int i; |
| 603 | struct page *dst_base = dst; |
| 604 | struct page *src_base = src; |
| 605 | |
| 606 | for (i = 0; i < nr_pages; ) { |
| 607 | cond_resched(); |
| 608 | copy_highpage(dst, src); |
| 609 | |
| 610 | i++; |
| 611 | dst = mem_map_next(dst, dst_base, i); |
| 612 | src = mem_map_next(src, src_base, i); |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | static void copy_huge_page(struct page *dst, struct page *src) |
| 617 | { |
| 618 | int i; |
| 619 | int nr_pages; |
| 620 | |
| 621 | if (PageHuge(src)) { |
| 622 | /* hugetlbfs page */ |
| 623 | struct hstate *h = page_hstate(src); |
| 624 | nr_pages = pages_per_huge_page(h); |
| 625 | |
| 626 | if (unlikely(nr_pages > MAX_ORDER_NR_PAGES)) { |
| 627 | __copy_gigantic_page(dst, src, nr_pages); |
| 628 | return; |
| 629 | } |
| 630 | } else { |
| 631 | /* thp page */ |
| 632 | BUG_ON(!PageTransHuge(src)); |
| 633 | nr_pages = hpage_nr_pages(src); |
| 634 | } |
| 635 | |
| 636 | for (i = 0; i < nr_pages; i++) { |
| 637 | cond_resched(); |
| 638 | copy_highpage(dst + i, src + i); |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | /* |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 643 | * Copy the page to its new location |
| 644 | */ |
Jérôme Glisse | 2916ecc | 2017-09-08 16:12:06 -0700 | [diff] [blame] | 645 | void migrate_page_states(struct page *newpage, struct page *page) |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 646 | { |
Rik van Riel | 7851a45 | 2013-10-07 11:29:23 +0100 | [diff] [blame] | 647 | int cpupid; |
| 648 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 649 | if (PageError(page)) |
| 650 | SetPageError(newpage); |
| 651 | if (PageReferenced(page)) |
| 652 | SetPageReferenced(newpage); |
| 653 | if (PageUptodate(page)) |
| 654 | SetPageUptodate(newpage); |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 655 | if (TestClearPageActive(page)) { |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 656 | VM_BUG_ON_PAGE(PageUnevictable(page), page); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 657 | SetPageActive(newpage); |
Lee Schermerhorn | 418b27e | 2009-12-14 17:59:54 -0800 | [diff] [blame] | 658 | } else if (TestClearPageUnevictable(page)) |
| 659 | SetPageUnevictable(newpage); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 660 | if (PageChecked(page)) |
| 661 | SetPageChecked(newpage); |
| 662 | if (PageMappedToDisk(page)) |
| 663 | SetPageMappedToDisk(newpage); |
| 664 | |
Hugh Dickins | 42cb14b | 2015-11-05 18:50:05 -0800 | [diff] [blame] | 665 | /* Move dirty on pages not done by migrate_page_move_mapping() */ |
| 666 | if (PageDirty(page)) |
| 667 | SetPageDirty(newpage); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 668 | |
Vladimir Davydov | 33c3fc7 | 2015-09-09 15:35:45 -0700 | [diff] [blame] | 669 | if (page_is_young(page)) |
| 670 | set_page_young(newpage); |
| 671 | if (page_is_idle(page)) |
| 672 | set_page_idle(newpage); |
| 673 | |
Rik van Riel | 7851a45 | 2013-10-07 11:29:23 +0100 | [diff] [blame] | 674 | /* |
| 675 | * Copy NUMA information to the new page, to prevent over-eager |
| 676 | * future migrations of this same page. |
| 677 | */ |
| 678 | cpupid = page_cpupid_xchg_last(page, -1); |
| 679 | page_cpupid_xchg_last(newpage, cpupid); |
| 680 | |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 681 | ksm_migrate_page(newpage, page); |
Hugh Dickins | c8d6553 | 2013-02-22 16:35:10 -0800 | [diff] [blame] | 682 | /* |
| 683 | * Please do not reorder this without considering how mm/ksm.c's |
| 684 | * get_ksm_page() depends upon ksm_migrate_page() and PageSwapCache(). |
| 685 | */ |
Naoya Horiguchi | b3b3a99 | 2015-04-15 16:13:15 -0700 | [diff] [blame] | 686 | if (PageSwapCache(page)) |
| 687 | ClearPageSwapCache(page); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 688 | ClearPagePrivate(page); |
| 689 | set_page_private(page, 0); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 690 | |
| 691 | /* |
| 692 | * If any waiters have accumulated on the new page then |
| 693 | * wake them up. |
| 694 | */ |
| 695 | if (PageWriteback(newpage)) |
| 696 | end_page_writeback(newpage); |
Vlastimil Babka | d435edc | 2016-03-15 14:56:15 -0700 | [diff] [blame] | 697 | |
| 698 | copy_page_owner(page, newpage); |
Johannes Weiner | 74485cf | 2016-03-15 14:57:54 -0700 | [diff] [blame] | 699 | |
| 700 | mem_cgroup_migrate(page, newpage); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 701 | } |
Jérôme Glisse | 2916ecc | 2017-09-08 16:12:06 -0700 | [diff] [blame] | 702 | EXPORT_SYMBOL(migrate_page_states); |
| 703 | |
| 704 | void migrate_page_copy(struct page *newpage, struct page *page) |
| 705 | { |
| 706 | if (PageHuge(page) || PageTransHuge(page)) |
| 707 | copy_huge_page(newpage, page); |
| 708 | else |
| 709 | copy_highpage(newpage, page); |
| 710 | |
| 711 | migrate_page_states(newpage, page); |
| 712 | } |
Richard Weinberger | 1118dce | 2016-06-16 23:26:14 +0200 | [diff] [blame] | 713 | EXPORT_SYMBOL(migrate_page_copy); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 714 | |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 715 | /************************************************************ |
| 716 | * Migration functions |
| 717 | ***********************************************************/ |
| 718 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 719 | /* |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 720 | * Common logic to directly migrate a single LRU page suitable for |
David Howells | 266cf65 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 721 | * pages that do not use PagePrivate/PagePrivate2. |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 722 | * |
| 723 | * Pages are locked upon entry and exit. |
| 724 | */ |
Christoph Lameter | 2d1db3b | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 725 | int migrate_page(struct address_space *mapping, |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 726 | struct page *newpage, struct page *page, |
| 727 | enum migrate_mode mode) |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 728 | { |
| 729 | int rc; |
| 730 | |
| 731 | BUG_ON(PageWriteback(page)); /* Writeback must be complete */ |
| 732 | |
Benjamin LaHaise | 8e321fe | 2013-12-21 17:56:08 -0500 | [diff] [blame] | 733 | rc = migrate_page_move_mapping(mapping, newpage, page, NULL, mode, 0); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 734 | |
Rafael Aquini | 78bd520 | 2012-12-11 16:02:31 -0800 | [diff] [blame] | 735 | if (rc != MIGRATEPAGE_SUCCESS) |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 736 | return rc; |
| 737 | |
Jérôme Glisse | 2916ecc | 2017-09-08 16:12:06 -0700 | [diff] [blame] | 738 | if (mode != MIGRATE_SYNC_NO_COPY) |
| 739 | migrate_page_copy(newpage, page); |
| 740 | else |
| 741 | migrate_page_states(newpage, page); |
Rafael Aquini | 78bd520 | 2012-12-11 16:02:31 -0800 | [diff] [blame] | 742 | return MIGRATEPAGE_SUCCESS; |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 743 | } |
| 744 | EXPORT_SYMBOL(migrate_page); |
| 745 | |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 746 | #ifdef CONFIG_BLOCK |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 747 | /* |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 748 | * Migration function for pages with buffers. This function can only be used |
| 749 | * if the underlying filesystem guarantees that no other references to "page" |
| 750 | * exist. |
| 751 | */ |
Christoph Lameter | 2d1db3b | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 752 | int buffer_migrate_page(struct address_space *mapping, |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 753 | struct page *newpage, struct page *page, enum migrate_mode mode) |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 754 | { |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 755 | struct buffer_head *bh, *head; |
| 756 | int rc; |
| 757 | |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 758 | if (!page_has_buffers(page)) |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 759 | return migrate_page(mapping, newpage, page, mode); |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 760 | |
| 761 | head = page_buffers(page); |
| 762 | |
Benjamin LaHaise | 8e321fe | 2013-12-21 17:56:08 -0500 | [diff] [blame] | 763 | rc = migrate_page_move_mapping(mapping, newpage, page, head, mode, 0); |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 764 | |
Rafael Aquini | 78bd520 | 2012-12-11 16:02:31 -0800 | [diff] [blame] | 765 | if (rc != MIGRATEPAGE_SUCCESS) |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 766 | return rc; |
| 767 | |
Mel Gorman | b969c4a | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 768 | /* |
| 769 | * In the async case, migrate_page_move_mapping locked the buffers |
| 770 | * with an IRQ-safe spinlock held. In the sync case, the buffers |
| 771 | * need to be locked now |
| 772 | */ |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 773 | if (mode != MIGRATE_ASYNC) |
| 774 | BUG_ON(!buffer_migrate_lock_buffers(head, mode)); |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 775 | |
| 776 | ClearPagePrivate(page); |
| 777 | set_page_private(newpage, page_private(page)); |
| 778 | set_page_private(page, 0); |
| 779 | put_page(page); |
| 780 | get_page(newpage); |
| 781 | |
| 782 | bh = head; |
| 783 | do { |
| 784 | set_bh_page(bh, newpage, bh_offset(bh)); |
| 785 | bh = bh->b_this_page; |
| 786 | |
| 787 | } while (bh != head); |
| 788 | |
| 789 | SetPagePrivate(newpage); |
| 790 | |
Jérôme Glisse | 2916ecc | 2017-09-08 16:12:06 -0700 | [diff] [blame] | 791 | if (mode != MIGRATE_SYNC_NO_COPY) |
| 792 | migrate_page_copy(newpage, page); |
| 793 | else |
| 794 | migrate_page_states(newpage, page); |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 795 | |
| 796 | bh = head; |
| 797 | do { |
| 798 | unlock_buffer(bh); |
Jérôme Glisse | 2916ecc | 2017-09-08 16:12:06 -0700 | [diff] [blame] | 799 | put_bh(bh); |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 800 | bh = bh->b_this_page; |
| 801 | |
| 802 | } while (bh != head); |
| 803 | |
Rafael Aquini | 78bd520 | 2012-12-11 16:02:31 -0800 | [diff] [blame] | 804 | return MIGRATEPAGE_SUCCESS; |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 805 | } |
| 806 | EXPORT_SYMBOL(buffer_migrate_page); |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 807 | #endif |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 808 | |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 809 | /* |
| 810 | * Writeback a page to clean the dirty state |
| 811 | */ |
| 812 | static int writeout(struct address_space *mapping, struct page *page) |
| 813 | { |
| 814 | struct writeback_control wbc = { |
| 815 | .sync_mode = WB_SYNC_NONE, |
| 816 | .nr_to_write = 1, |
| 817 | .range_start = 0, |
| 818 | .range_end = LLONG_MAX, |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 819 | .for_reclaim = 1 |
| 820 | }; |
| 821 | int rc; |
| 822 | |
| 823 | if (!mapping->a_ops->writepage) |
| 824 | /* No write method for the address space */ |
| 825 | return -EINVAL; |
| 826 | |
| 827 | if (!clear_page_dirty_for_io(page)) |
| 828 | /* Someone else already triggered a write */ |
| 829 | return -EAGAIN; |
| 830 | |
| 831 | /* |
| 832 | * A dirty page may imply that the underlying filesystem has |
| 833 | * the page on some queue. So the page must be clean for |
| 834 | * migration. Writeout may mean we loose the lock and the |
| 835 | * page state is no longer what we checked for earlier. |
| 836 | * At this point we know that the migration attempt cannot |
| 837 | * be successful. |
| 838 | */ |
Kirill A. Shutemov | e388466 | 2016-03-17 14:20:07 -0700 | [diff] [blame] | 839 | remove_migration_ptes(page, page, false); |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 840 | |
| 841 | rc = mapping->a_ops->writepage(page, &wbc); |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 842 | |
| 843 | if (rc != AOP_WRITEPAGE_ACTIVATE) |
| 844 | /* unlocked. Relock */ |
| 845 | lock_page(page); |
| 846 | |
Hugh Dickins | bda8550 | 2008-11-19 15:36:36 -0800 | [diff] [blame] | 847 | return (rc < 0) ? -EIO : -EAGAIN; |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | /* |
| 851 | * Default handling if a filesystem does not provide a migration function. |
| 852 | */ |
Christoph Lameter | 8351a6e | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 853 | static int fallback_migrate_page(struct address_space *mapping, |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 854 | struct page *newpage, struct page *page, enum migrate_mode mode) |
Christoph Lameter | 8351a6e | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 855 | { |
Mel Gorman | b969c4a | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 856 | if (PageDirty(page)) { |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 857 | /* Only writeback pages in full synchronous migration */ |
Jérôme Glisse | 2916ecc | 2017-09-08 16:12:06 -0700 | [diff] [blame] | 858 | switch (mode) { |
| 859 | case MIGRATE_SYNC: |
| 860 | case MIGRATE_SYNC_NO_COPY: |
| 861 | break; |
| 862 | default: |
Mel Gorman | b969c4a | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 863 | return -EBUSY; |
Jérôme Glisse | 2916ecc | 2017-09-08 16:12:06 -0700 | [diff] [blame] | 864 | } |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 865 | return writeout(mapping, page); |
Mel Gorman | b969c4a | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 866 | } |
Christoph Lameter | 8351a6e | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 867 | |
| 868 | /* |
| 869 | * Buffers may be managed in a filesystem specific way. |
| 870 | * We must have no buffers or drop them. |
| 871 | */ |
David Howells | 266cf65 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 872 | if (page_has_private(page) && |
Christoph Lameter | 8351a6e | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 873 | !try_to_release_page(page, GFP_KERNEL)) |
| 874 | return -EAGAIN; |
| 875 | |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 876 | return migrate_page(mapping, newpage, page, mode); |
Christoph Lameter | 8351a6e | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 877 | } |
| 878 | |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 879 | /* |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 880 | * Move a page to a newly allocated page |
| 881 | * The page is locked and all ptes have been successfully removed. |
| 882 | * |
| 883 | * The new page will have replaced the old page if this function |
| 884 | * is successful. |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 885 | * |
| 886 | * Return value: |
| 887 | * < 0 - error code |
Rafael Aquini | 78bd520 | 2012-12-11 16:02:31 -0800 | [diff] [blame] | 888 | * MIGRATEPAGE_SUCCESS - success |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 889 | */ |
Mel Gorman | 3fe2011 | 2010-05-24 14:32:20 -0700 | [diff] [blame] | 890 | static int move_to_new_page(struct page *newpage, struct page *page, |
Hugh Dickins | 5c3f9a6 | 2015-11-05 18:49:53 -0800 | [diff] [blame] | 891 | enum migrate_mode mode) |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 892 | { |
| 893 | struct address_space *mapping; |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 894 | int rc = -EAGAIN; |
| 895 | bool is_lru = !__PageMovable(page); |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 896 | |
Hugh Dickins | 7db7671 | 2015-11-05 18:49:49 -0800 | [diff] [blame] | 897 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
| 898 | VM_BUG_ON_PAGE(!PageLocked(newpage), newpage); |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 899 | |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 900 | mapping = page_mapping(page); |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 901 | |
| 902 | if (likely(is_lru)) { |
| 903 | if (!mapping) |
| 904 | rc = migrate_page(mapping, newpage, page, mode); |
| 905 | else if (mapping->a_ops->migratepage) |
| 906 | /* |
| 907 | * Most pages have a mapping and most filesystems |
| 908 | * provide a migratepage callback. Anonymous pages |
| 909 | * are part of swap space which also has its own |
| 910 | * migratepage callback. This is the most common path |
| 911 | * for page migration. |
| 912 | */ |
| 913 | rc = mapping->a_ops->migratepage(mapping, newpage, |
| 914 | page, mode); |
| 915 | else |
| 916 | rc = fallback_migrate_page(mapping, newpage, |
| 917 | page, mode); |
| 918 | } else { |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 919 | /* |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 920 | * In case of non-lru page, it could be released after |
| 921 | * isolation step. In that case, we shouldn't try migration. |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 922 | */ |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 923 | VM_BUG_ON_PAGE(!PageIsolated(page), page); |
| 924 | if (!PageMovable(page)) { |
| 925 | rc = MIGRATEPAGE_SUCCESS; |
| 926 | __ClearPageIsolated(page); |
| 927 | goto out; |
| 928 | } |
| 929 | |
| 930 | rc = mapping->a_ops->migratepage(mapping, newpage, |
| 931 | page, mode); |
| 932 | WARN_ON_ONCE(rc == MIGRATEPAGE_SUCCESS && |
| 933 | !PageIsolated(page)); |
| 934 | } |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 935 | |
Hugh Dickins | 5c3f9a6 | 2015-11-05 18:49:53 -0800 | [diff] [blame] | 936 | /* |
| 937 | * When successful, old pagecache page->mapping must be cleared before |
| 938 | * page is freed; but stats require that PageAnon be left as PageAnon. |
| 939 | */ |
| 940 | if (rc == MIGRATEPAGE_SUCCESS) { |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 941 | if (__PageMovable(page)) { |
| 942 | VM_BUG_ON_PAGE(!PageIsolated(page), page); |
| 943 | |
| 944 | /* |
| 945 | * We clear PG_movable under page_lock so any compactor |
| 946 | * cannot try to migrate this page. |
| 947 | */ |
| 948 | __ClearPageIsolated(page); |
| 949 | } |
| 950 | |
| 951 | /* |
| 952 | * Anonymous and movable page->mapping will be cleard by |
| 953 | * free_pages_prepare so don't reset it here for keeping |
| 954 | * the type to work PageAnon, for example. |
| 955 | */ |
| 956 | if (!PageMappingFlags(page)) |
Hugh Dickins | 5c3f9a6 | 2015-11-05 18:49:53 -0800 | [diff] [blame] | 957 | page->mapping = NULL; |
Mel Gorman | 3fe2011 | 2010-05-24 14:32:20 -0700 | [diff] [blame] | 958 | } |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 959 | out: |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 960 | return rc; |
| 961 | } |
| 962 | |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 963 | static int __unmap_and_move(struct page *page, struct page *newpage, |
Hugh Dickins | 9c620e2 | 2013-02-22 16:35:14 -0800 | [diff] [blame] | 964 | int force, enum migrate_mode mode) |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 965 | { |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 966 | int rc = -EAGAIN; |
Hugh Dickins | 2ebba6b | 2014-12-12 16:56:19 -0800 | [diff] [blame] | 967 | int page_was_mapped = 0; |
Mel Gorman | 3f6c827 | 2010-05-24 14:32:17 -0700 | [diff] [blame] | 968 | struct anon_vma *anon_vma = NULL; |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 969 | bool is_lru = !__PageMovable(page); |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 970 | |
Nick Piggin | 529ae9a | 2008-08-02 12:01:03 +0200 | [diff] [blame] | 971 | if (!trylock_page(page)) { |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 972 | if (!force || mode == MIGRATE_ASYNC) |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 973 | goto out; |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 974 | |
| 975 | /* |
| 976 | * It's not safe for direct compaction to call lock_page. |
| 977 | * For example, during page readahead pages are added locked |
| 978 | * to the LRU. Later, when the IO completes the pages are |
| 979 | * marked uptodate and unlocked. However, the queueing |
| 980 | * could be merging multiple pages for one bio (e.g. |
| 981 | * mpage_readpages). If an allocation happens for the |
| 982 | * second or third page, the process can end up locking |
| 983 | * the same page twice and deadlocking. Rather than |
| 984 | * trying to be clever about what pages can be locked, |
| 985 | * avoid the use of lock_page for direct compaction |
| 986 | * altogether. |
| 987 | */ |
| 988 | if (current->flags & PF_MEMALLOC) |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 989 | goto out; |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 990 | |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 991 | lock_page(page); |
| 992 | } |
| 993 | |
| 994 | if (PageWriteback(page)) { |
Andrea Arcangeli | 11bc82d | 2011-03-22 16:33:11 -0700 | [diff] [blame] | 995 | /* |
Jianguo Wu | fed5b64 | 2013-04-29 15:07:58 -0700 | [diff] [blame] | 996 | * Only in the case of a full synchronous migration is it |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 997 | * necessary to wait for PageWriteback. In the async case, |
| 998 | * the retry loop is too short and in the sync-light case, |
| 999 | * the overhead of stalling is too much |
Andrea Arcangeli | 11bc82d | 2011-03-22 16:33:11 -0700 | [diff] [blame] | 1000 | */ |
Jérôme Glisse | 2916ecc | 2017-09-08 16:12:06 -0700 | [diff] [blame] | 1001 | switch (mode) { |
| 1002 | case MIGRATE_SYNC: |
| 1003 | case MIGRATE_SYNC_NO_COPY: |
| 1004 | break; |
| 1005 | default: |
Andrea Arcangeli | 11bc82d | 2011-03-22 16:33:11 -0700 | [diff] [blame] | 1006 | rc = -EBUSY; |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 1007 | goto out_unlock; |
Andrea Arcangeli | 11bc82d | 2011-03-22 16:33:11 -0700 | [diff] [blame] | 1008 | } |
| 1009 | if (!force) |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 1010 | goto out_unlock; |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1011 | wait_on_page_writeback(page); |
| 1012 | } |
Hugh Dickins | 03f15c8 | 2015-11-05 18:49:56 -0800 | [diff] [blame] | 1013 | |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1014 | /* |
KAMEZAWA Hiroyuki | dc386d4 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 1015 | * By try_to_unmap(), page->mapcount goes down to 0 here. In this case, |
| 1016 | * 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] | 1017 | * This get_anon_vma() delays freeing anon_vma pointer until the end |
KAMEZAWA Hiroyuki | dc386d4 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 1018 | * of migration. File cache pages are no problem because of page_lock() |
KAMEZAWA Hiroyuki | 989f89c | 2007-08-30 23:56:21 -0700 | [diff] [blame] | 1019 | * File Caches may use write_page() or lock_page() in migration, then, |
| 1020 | * just care Anon page here. |
Hugh Dickins | 03f15c8 | 2015-11-05 18:49:56 -0800 | [diff] [blame] | 1021 | * |
| 1022 | * Only page_get_anon_vma() understands the subtleties of |
| 1023 | * getting a hold on an anon_vma from outside one of its mms. |
| 1024 | * But if we cannot get anon_vma, then we won't need it anyway, |
| 1025 | * because that implies that the anon page is no longer mapped |
| 1026 | * (and cannot be remapped so long as we hold the page lock). |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1027 | */ |
Hugh Dickins | 03f15c8 | 2015-11-05 18:49:56 -0800 | [diff] [blame] | 1028 | if (PageAnon(page) && !PageKsm(page)) |
Peter Zijlstra | 746b18d | 2011-05-24 17:12:10 -0700 | [diff] [blame] | 1029 | anon_vma = page_get_anon_vma(page); |
Shaohua Li | 62e1c55 | 2008-02-04 22:29:33 -0800 | [diff] [blame] | 1030 | |
Hugh Dickins | 7db7671 | 2015-11-05 18:49:49 -0800 | [diff] [blame] | 1031 | /* |
| 1032 | * Block others from accessing the new page when we get around to |
| 1033 | * establishing additional references. We are usually the only one |
| 1034 | * holding a reference to newpage at this point. We used to have a BUG |
| 1035 | * here if trylock_page(newpage) fails, but would like to allow for |
| 1036 | * cases where there might be a race with the previous use of newpage. |
| 1037 | * This is much like races on refcount of oldpage: just don't BUG(). |
| 1038 | */ |
| 1039 | if (unlikely(!trylock_page(newpage))) |
| 1040 | goto out_unlock; |
| 1041 | |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 1042 | if (unlikely(!is_lru)) { |
| 1043 | rc = move_to_new_page(newpage, page, mode); |
| 1044 | goto out_unlock_both; |
| 1045 | } |
| 1046 | |
KAMEZAWA Hiroyuki | dc386d4 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 1047 | /* |
Shaohua Li | 62e1c55 | 2008-02-04 22:29:33 -0800 | [diff] [blame] | 1048 | * Corner case handling: |
| 1049 | * 1. When a new swap-cache page is read into, it is added to the LRU |
| 1050 | * and treated as swapcache but it has no rmap yet. |
| 1051 | * Calling try_to_unmap() against a page->mapping==NULL page will |
| 1052 | * trigger a BUG. So handle it here. |
| 1053 | * 2. An orphaned page (see truncate_complete_page) might have |
| 1054 | * fs-private metadata. The page can be picked up due to memory |
| 1055 | * offlining. Everywhere else except page reclaim, the page is |
| 1056 | * invisible to the vm, so the page can not be migrated. So try to |
| 1057 | * free the metadata, so the page can be freed. |
KAMEZAWA Hiroyuki | dc386d4 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 1058 | */ |
Shaohua Li | 62e1c55 | 2008-02-04 22:29:33 -0800 | [diff] [blame] | 1059 | if (!page->mapping) { |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 1060 | VM_BUG_ON_PAGE(PageAnon(page), page); |
Hugh Dickins | 1ce82b6 | 2011-01-13 15:47:30 -0800 | [diff] [blame] | 1061 | if (page_has_private(page)) { |
Shaohua Li | 62e1c55 | 2008-02-04 22:29:33 -0800 | [diff] [blame] | 1062 | try_to_free_buffers(page); |
Hugh Dickins | 7db7671 | 2015-11-05 18:49:49 -0800 | [diff] [blame] | 1063 | goto out_unlock_both; |
Shaohua Li | 62e1c55 | 2008-02-04 22:29:33 -0800 | [diff] [blame] | 1064 | } |
Hugh Dickins | 7db7671 | 2015-11-05 18:49:49 -0800 | [diff] [blame] | 1065 | } else if (page_mapped(page)) { |
| 1066 | /* Establish migration ptes */ |
Hugh Dickins | 03f15c8 | 2015-11-05 18:49:56 -0800 | [diff] [blame] | 1067 | VM_BUG_ON_PAGE(PageAnon(page) && !PageKsm(page) && !anon_vma, |
| 1068 | page); |
Hugh Dickins | 2ebba6b | 2014-12-12 16:56:19 -0800 | [diff] [blame] | 1069 | try_to_unmap(page, |
Wanpeng Li | da1b13c | 2015-09-08 15:03:27 -0700 | [diff] [blame] | 1070 | TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS); |
Hugh Dickins | 2ebba6b | 2014-12-12 16:56:19 -0800 | [diff] [blame] | 1071 | page_was_mapped = 1; |
| 1072 | } |
KAMEZAWA Hiroyuki | dc386d4 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 1073 | |
Christoph Lameter | e6a1530 | 2006-06-25 05:46:49 -0700 | [diff] [blame] | 1074 | if (!page_mapped(page)) |
Hugh Dickins | 5c3f9a6 | 2015-11-05 18:49:53 -0800 | [diff] [blame] | 1075 | rc = move_to_new_page(newpage, page, mode); |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1076 | |
Hugh Dickins | 5c3f9a6 | 2015-11-05 18:49:53 -0800 | [diff] [blame] | 1077 | if (page_was_mapped) |
| 1078 | remove_migration_ptes(page, |
Kirill A. Shutemov | e388466 | 2016-03-17 14:20:07 -0700 | [diff] [blame] | 1079 | rc == MIGRATEPAGE_SUCCESS ? newpage : page, false); |
Mel Gorman | 3f6c827 | 2010-05-24 14:32:17 -0700 | [diff] [blame] | 1080 | |
Hugh Dickins | 7db7671 | 2015-11-05 18:49:49 -0800 | [diff] [blame] | 1081 | out_unlock_both: |
| 1082 | unlock_page(newpage); |
| 1083 | out_unlock: |
Mel Gorman | 3f6c827 | 2010-05-24 14:32:17 -0700 | [diff] [blame] | 1084 | /* Drop an anon_vma reference if we took one */ |
Rik van Riel | 7654506 | 2010-08-09 17:18:41 -0700 | [diff] [blame] | 1085 | if (anon_vma) |
Peter Zijlstra | 9e60109 | 2011-03-22 16:32:46 -0700 | [diff] [blame] | 1086 | put_anon_vma(anon_vma); |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1087 | unlock_page(page); |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1088 | out: |
Minchan Kim | c6c919e | 2016-07-26 15:23:02 -0700 | [diff] [blame] | 1089 | /* |
| 1090 | * If migration is successful, decrease refcount of the newpage |
| 1091 | * which will not free the page because new page owner increased |
| 1092 | * refcounter. As well, if it is LRU page, add the page to LRU |
| 1093 | * list in here. |
| 1094 | */ |
| 1095 | if (rc == MIGRATEPAGE_SUCCESS) { |
Minchan Kim | b1123ea6 | 2016-07-26 15:23:09 -0700 | [diff] [blame] | 1096 | if (unlikely(__PageMovable(newpage))) |
Minchan Kim | c6c919e | 2016-07-26 15:23:02 -0700 | [diff] [blame] | 1097 | put_page(newpage); |
| 1098 | else |
| 1099 | putback_lru_page(newpage); |
| 1100 | } |
| 1101 | |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1102 | return rc; |
| 1103 | } |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 1104 | |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1105 | /* |
Geert Uytterhoeven | ef2a515 | 2015-04-14 15:44:22 -0700 | [diff] [blame] | 1106 | * gcc 4.7 and 4.8 on arm get an ICEs when inlining unmap_and_move(). Work |
| 1107 | * around it. |
| 1108 | */ |
| 1109 | #if (GCC_VERSION >= 40700 && GCC_VERSION < 40900) && defined(CONFIG_ARM) |
| 1110 | #define ICE_noinline noinline |
| 1111 | #else |
| 1112 | #define ICE_noinline |
| 1113 | #endif |
| 1114 | |
| 1115 | /* |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1116 | * Obtain the lock on page, remove all ptes and migrate the page |
| 1117 | * to the newly allocated page in newpage. |
| 1118 | */ |
Geert Uytterhoeven | ef2a515 | 2015-04-14 15:44:22 -0700 | [diff] [blame] | 1119 | static ICE_noinline int unmap_and_move(new_page_t get_new_page, |
| 1120 | free_page_t put_new_page, |
| 1121 | unsigned long private, struct page *page, |
Naoya Horiguchi | add05ce | 2015-06-24 16:56:50 -0700 | [diff] [blame] | 1122 | int force, enum migrate_mode mode, |
| 1123 | enum migrate_reason reason) |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1124 | { |
Hugh Dickins | 2def742 | 2015-11-05 18:49:46 -0800 | [diff] [blame] | 1125 | int rc = MIGRATEPAGE_SUCCESS; |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1126 | int *result = NULL; |
Hugh Dickins | 2def742 | 2015-11-05 18:49:46 -0800 | [diff] [blame] | 1127 | struct page *newpage; |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1128 | |
Hugh Dickins | 2def742 | 2015-11-05 18:49:46 -0800 | [diff] [blame] | 1129 | newpage = get_new_page(page, private, &result); |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1130 | if (!newpage) |
| 1131 | return -ENOMEM; |
| 1132 | |
| 1133 | if (page_count(page) == 1) { |
| 1134 | /* page was freed from under us. So we are done. */ |
Minchan Kim | c6c919e | 2016-07-26 15:23:02 -0700 | [diff] [blame] | 1135 | ClearPageActive(page); |
| 1136 | ClearPageUnevictable(page); |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 1137 | if (unlikely(__PageMovable(page))) { |
| 1138 | lock_page(page); |
| 1139 | if (!PageMovable(page)) |
| 1140 | __ClearPageIsolated(page); |
| 1141 | unlock_page(page); |
| 1142 | } |
Minchan Kim | c6c919e | 2016-07-26 15:23:02 -0700 | [diff] [blame] | 1143 | if (put_new_page) |
| 1144 | put_new_page(newpage, private); |
| 1145 | else |
| 1146 | put_page(newpage); |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1147 | goto out; |
| 1148 | } |
| 1149 | |
Zi Yan | 616b837 | 2017-09-08 16:10:57 -0700 | [diff] [blame] | 1150 | if (unlikely(PageTransHuge(page) && !PageTransHuge(newpage))) { |
Kirill A. Shutemov | 4d2fa96 | 2016-01-15 16:54:00 -0800 | [diff] [blame] | 1151 | lock_page(page); |
| 1152 | rc = split_huge_page(page); |
| 1153 | unlock_page(page); |
| 1154 | if (rc) |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1155 | goto out; |
Kirill A. Shutemov | 4d2fa96 | 2016-01-15 16:54:00 -0800 | [diff] [blame] | 1156 | } |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1157 | |
Hugh Dickins | 9c620e2 | 2013-02-22 16:35:14 -0800 | [diff] [blame] | 1158 | rc = __unmap_and_move(page, newpage, force, mode); |
Minchan Kim | c6c919e | 2016-07-26 15:23:02 -0700 | [diff] [blame] | 1159 | if (rc == MIGRATEPAGE_SUCCESS) |
Vlastimil Babka | 7cd12b4 | 2016-03-15 14:56:18 -0700 | [diff] [blame] | 1160 | set_page_owner_migrate_reason(newpage, reason); |
Rafael Aquini | bf6bddf1 | 2012-12-11 16:02:42 -0800 | [diff] [blame] | 1161 | |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1162 | out: |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1163 | if (rc != -EAGAIN) { |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 1164 | /* |
| 1165 | * A page that has been migrated has all references |
| 1166 | * removed and will be freed. A page that has not been |
| 1167 | * migrated will have kepts its references and be |
| 1168 | * restored. |
| 1169 | */ |
| 1170 | list_del(&page->lru); |
Ming Ling | 6afcf8e | 2016-12-12 16:42:26 -0800 | [diff] [blame] | 1171 | |
| 1172 | /* |
| 1173 | * Compaction can migrate also non-LRU pages which are |
| 1174 | * not accounted to NR_ISOLATED_*. They can be recognized |
| 1175 | * as __PageMovable |
| 1176 | */ |
| 1177 | if (likely(!__PageMovable(page))) |
Naoya Horiguchi | e8db67e | 2017-09-08 16:11:12 -0700 | [diff] [blame] | 1178 | mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + |
| 1179 | page_is_file_cache(page), -hpage_nr_pages(page)); |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1180 | } |
David Rientjes | 68711a7 | 2014-06-04 16:08:25 -0700 | [diff] [blame] | 1181 | |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 1182 | /* |
Minchan Kim | c6c919e | 2016-07-26 15:23:02 -0700 | [diff] [blame] | 1183 | * If migration is successful, releases reference grabbed during |
| 1184 | * isolation. Otherwise, restore the page to right list unless |
| 1185 | * we want to retry. |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 1186 | */ |
Minchan Kim | c6c919e | 2016-07-26 15:23:02 -0700 | [diff] [blame] | 1187 | if (rc == MIGRATEPAGE_SUCCESS) { |
| 1188 | put_page(page); |
| 1189 | if (reason == MR_MEMORY_FAILURE) { |
| 1190 | /* |
| 1191 | * Set PG_HWPoison on just freed page |
| 1192 | * intentionally. Although it's rather weird, |
| 1193 | * it's how HWPoison flag works at the moment. |
| 1194 | */ |
| 1195 | if (!test_set_page_hwpoison(page)) |
| 1196 | num_poisoned_pages_inc(); |
| 1197 | } |
| 1198 | } else { |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 1199 | if (rc != -EAGAIN) { |
| 1200 | if (likely(!__PageMovable(page))) { |
| 1201 | putback_lru_page(page); |
| 1202 | goto put_new; |
| 1203 | } |
| 1204 | |
| 1205 | lock_page(page); |
| 1206 | if (PageMovable(page)) |
| 1207 | putback_movable_page(page); |
| 1208 | else |
| 1209 | __ClearPageIsolated(page); |
| 1210 | unlock_page(page); |
| 1211 | put_page(page); |
| 1212 | } |
| 1213 | put_new: |
Minchan Kim | c6c919e | 2016-07-26 15:23:02 -0700 | [diff] [blame] | 1214 | if (put_new_page) |
| 1215 | put_new_page(newpage, private); |
| 1216 | else |
| 1217 | put_page(newpage); |
| 1218 | } |
David Rientjes | 68711a7 | 2014-06-04 16:08:25 -0700 | [diff] [blame] | 1219 | |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1220 | if (result) { |
| 1221 | if (rc) |
| 1222 | *result = rc; |
| 1223 | else |
| 1224 | *result = page_to_nid(newpage); |
| 1225 | } |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1226 | return rc; |
| 1227 | } |
| 1228 | |
| 1229 | /* |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1230 | * Counterpart of unmap_and_move_page() for hugepage migration. |
| 1231 | * |
| 1232 | * This function doesn't wait the completion of hugepage I/O |
| 1233 | * because there is no race between I/O and migration for hugepage. |
| 1234 | * Note that currently hugepage I/O occurs only in direct I/O |
| 1235 | * where no lock is held and PG_writeback is irrelevant, |
| 1236 | * and writeback status of all subpages are counted in the reference |
| 1237 | * count of the head page (i.e. if all subpages of a 2MB hugepage are |
| 1238 | * under direct I/O, the reference of the head page is 512 and a bit more.) |
| 1239 | * This means that when we try to migrate hugepage whose subpages are |
| 1240 | * doing direct I/O, some references remain after try_to_unmap() and |
| 1241 | * hugepage migration fails without data corruption. |
| 1242 | * |
| 1243 | * There is also no race when direct I/O is issued on the page under migration, |
| 1244 | * because then pte is replaced with migration swap entry and direct I/O code |
| 1245 | * will wait in the page fault for migration to complete. |
| 1246 | */ |
| 1247 | 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] | 1248 | free_page_t put_new_page, unsigned long private, |
| 1249 | struct page *hpage, int force, |
Vlastimil Babka | 7cd12b4 | 2016-03-15 14:56:18 -0700 | [diff] [blame] | 1250 | enum migrate_mode mode, int reason) |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1251 | { |
Hugh Dickins | 2def742 | 2015-11-05 18:49:46 -0800 | [diff] [blame] | 1252 | int rc = -EAGAIN; |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1253 | int *result = NULL; |
Hugh Dickins | 2ebba6b | 2014-12-12 16:56:19 -0800 | [diff] [blame] | 1254 | int page_was_mapped = 0; |
Joonsoo Kim | 32665f2 | 2014-01-21 15:51:15 -0800 | [diff] [blame] | 1255 | struct page *new_hpage; |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1256 | struct anon_vma *anon_vma = NULL; |
| 1257 | |
Naoya Horiguchi | 83467ef | 2013-09-11 14:22:11 -0700 | [diff] [blame] | 1258 | /* |
| 1259 | * Movability of hugepages depends on architectures and hugepage size. |
| 1260 | * This check is necessary because some callers of hugepage migration |
| 1261 | * like soft offline and memory hotremove don't walk through page |
| 1262 | * tables or check whether the hugepage is pmd-based or not before |
| 1263 | * kicking migration. |
| 1264 | */ |
Naoya Horiguchi | 100873d | 2014-06-04 16:10:56 -0700 | [diff] [blame] | 1265 | if (!hugepage_migration_supported(page_hstate(hpage))) { |
Joonsoo Kim | 32665f2 | 2014-01-21 15:51:15 -0800 | [diff] [blame] | 1266 | putback_active_hugepage(hpage); |
Naoya Horiguchi | 83467ef | 2013-09-11 14:22:11 -0700 | [diff] [blame] | 1267 | return -ENOSYS; |
Joonsoo Kim | 32665f2 | 2014-01-21 15:51:15 -0800 | [diff] [blame] | 1268 | } |
Naoya Horiguchi | 83467ef | 2013-09-11 14:22:11 -0700 | [diff] [blame] | 1269 | |
Joonsoo Kim | 32665f2 | 2014-01-21 15:51:15 -0800 | [diff] [blame] | 1270 | new_hpage = get_new_page(hpage, private, &result); |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1271 | if (!new_hpage) |
| 1272 | return -ENOMEM; |
| 1273 | |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1274 | if (!trylock_page(hpage)) { |
Jérôme Glisse | 2916ecc | 2017-09-08 16:12:06 -0700 | [diff] [blame] | 1275 | if (!force) |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1276 | goto out; |
Jérôme Glisse | 2916ecc | 2017-09-08 16:12:06 -0700 | [diff] [blame] | 1277 | switch (mode) { |
| 1278 | case MIGRATE_SYNC: |
| 1279 | case MIGRATE_SYNC_NO_COPY: |
| 1280 | break; |
| 1281 | default: |
| 1282 | goto out; |
| 1283 | } |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1284 | lock_page(hpage); |
| 1285 | } |
| 1286 | |
Peter Zijlstra | 746b18d | 2011-05-24 17:12:10 -0700 | [diff] [blame] | 1287 | if (PageAnon(hpage)) |
| 1288 | anon_vma = page_get_anon_vma(hpage); |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1289 | |
Hugh Dickins | 7db7671 | 2015-11-05 18:49:49 -0800 | [diff] [blame] | 1290 | if (unlikely(!trylock_page(new_hpage))) |
| 1291 | goto put_anon; |
| 1292 | |
Hugh Dickins | 2ebba6b | 2014-12-12 16:56:19 -0800 | [diff] [blame] | 1293 | if (page_mapped(hpage)) { |
| 1294 | try_to_unmap(hpage, |
| 1295 | TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS); |
| 1296 | page_was_mapped = 1; |
| 1297 | } |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1298 | |
| 1299 | if (!page_mapped(hpage)) |
Hugh Dickins | 5c3f9a6 | 2015-11-05 18:49:53 -0800 | [diff] [blame] | 1300 | rc = move_to_new_page(new_hpage, hpage, mode); |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1301 | |
Hugh Dickins | 5c3f9a6 | 2015-11-05 18:49:53 -0800 | [diff] [blame] | 1302 | if (page_was_mapped) |
| 1303 | remove_migration_ptes(hpage, |
Kirill A. Shutemov | e388466 | 2016-03-17 14:20:07 -0700 | [diff] [blame] | 1304 | rc == MIGRATEPAGE_SUCCESS ? new_hpage : hpage, false); |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1305 | |
Hugh Dickins | 7db7671 | 2015-11-05 18:49:49 -0800 | [diff] [blame] | 1306 | unlock_page(new_hpage); |
| 1307 | |
| 1308 | put_anon: |
Hugh Dickins | fd4a466 | 2011-01-13 15:47:31 -0800 | [diff] [blame] | 1309 | if (anon_vma) |
Peter Zijlstra | 9e60109 | 2011-03-22 16:32:46 -0700 | [diff] [blame] | 1310 | put_anon_vma(anon_vma); |
Aneesh Kumar K.V | 8e6ac7f | 2012-07-31 16:42:27 -0700 | [diff] [blame] | 1311 | |
Hugh Dickins | 2def742 | 2015-11-05 18:49:46 -0800 | [diff] [blame] | 1312 | if (rc == MIGRATEPAGE_SUCCESS) { |
Aneesh Kumar K.V | 8e6ac7f | 2012-07-31 16:42:27 -0700 | [diff] [blame] | 1313 | hugetlb_cgroup_migrate(hpage, new_hpage); |
Hugh Dickins | 2def742 | 2015-11-05 18:49:46 -0800 | [diff] [blame] | 1314 | put_new_page = NULL; |
Vlastimil Babka | 7cd12b4 | 2016-03-15 14:56:18 -0700 | [diff] [blame] | 1315 | set_page_owner_migrate_reason(new_hpage, reason); |
Hugh Dickins | 2def742 | 2015-11-05 18:49:46 -0800 | [diff] [blame] | 1316 | } |
Aneesh Kumar K.V | 8e6ac7f | 2012-07-31 16:42:27 -0700 | [diff] [blame] | 1317 | |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1318 | unlock_page(hpage); |
Hillf Danton | 0976133 | 2011-12-08 14:34:20 -0800 | [diff] [blame] | 1319 | out: |
Naoya Horiguchi | b8ec1ce | 2013-09-11 14:22:01 -0700 | [diff] [blame] | 1320 | if (rc != -EAGAIN) |
| 1321 | putback_active_hugepage(hpage); |
Anshuman Khandual | c3114a8 | 2017-07-10 15:47:41 -0700 | [diff] [blame] | 1322 | if (reason == MR_MEMORY_FAILURE && !test_set_page_hwpoison(hpage)) |
| 1323 | num_poisoned_pages_inc(); |
David Rientjes | 68711a7 | 2014-06-04 16:08:25 -0700 | [diff] [blame] | 1324 | |
| 1325 | /* |
| 1326 | * If migration was not successful and there's a freeing callback, use |
| 1327 | * it. Otherwise, put_page() will drop the reference grabbed during |
| 1328 | * isolation. |
| 1329 | */ |
Hugh Dickins | 2def742 | 2015-11-05 18:49:46 -0800 | [diff] [blame] | 1330 | if (put_new_page) |
David Rientjes | 68711a7 | 2014-06-04 16:08:25 -0700 | [diff] [blame] | 1331 | put_new_page(new_hpage, private); |
| 1332 | else |
Naoya Horiguchi | 3aaa76e | 2015-09-22 14:59:14 -0700 | [diff] [blame] | 1333 | putback_active_hugepage(new_hpage); |
David Rientjes | 68711a7 | 2014-06-04 16:08:25 -0700 | [diff] [blame] | 1334 | |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1335 | if (result) { |
| 1336 | if (rc) |
| 1337 | *result = rc; |
| 1338 | else |
| 1339 | *result = page_to_nid(new_hpage); |
| 1340 | } |
| 1341 | return rc; |
| 1342 | } |
| 1343 | |
| 1344 | /* |
Srivatsa S. Bhat | c73e5c9 | 2013-04-29 15:08:16 -0700 | [diff] [blame] | 1345 | * migrate_pages - migrate the pages specified in a list, to the free pages |
| 1346 | * supplied as the target for the page migration |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1347 | * |
Srivatsa S. Bhat | c73e5c9 | 2013-04-29 15:08:16 -0700 | [diff] [blame] | 1348 | * @from: The list of pages to be migrated. |
| 1349 | * @get_new_page: The function used to allocate free pages to be used |
| 1350 | * as the target of the page migration. |
David Rientjes | 68711a7 | 2014-06-04 16:08:25 -0700 | [diff] [blame] | 1351 | * @put_new_page: The function used to free target pages if migration |
| 1352 | * fails, or NULL if no special handling is necessary. |
Srivatsa S. Bhat | c73e5c9 | 2013-04-29 15:08:16 -0700 | [diff] [blame] | 1353 | * @private: Private data to be passed on to get_new_page() |
| 1354 | * @mode: The migration mode that specifies the constraints for |
| 1355 | * page migration, if any. |
| 1356 | * @reason: The reason for page migration. |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1357 | * |
Srivatsa S. Bhat | c73e5c9 | 2013-04-29 15:08:16 -0700 | [diff] [blame] | 1358 | * The function returns after 10 attempts or if no pages are movable any more |
| 1359 | * 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] | 1360 | * 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] | 1361 | * or free list only if ret != 0. |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1362 | * |
Srivatsa S. Bhat | c73e5c9 | 2013-04-29 15:08:16 -0700 | [diff] [blame] | 1363 | * 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] | 1364 | */ |
Hugh Dickins | 9c620e2 | 2013-02-22 16:35:14 -0800 | [diff] [blame] | 1365 | 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] | 1366 | free_page_t put_new_page, unsigned long private, |
| 1367 | enum migrate_mode mode, int reason) |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1368 | { |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1369 | int retry = 1; |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1370 | int nr_failed = 0; |
Mel Gorman | 5647bc2 | 2012-10-19 10:46:20 +0100 | [diff] [blame] | 1371 | int nr_succeeded = 0; |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1372 | int pass = 0; |
| 1373 | struct page *page; |
| 1374 | struct page *page2; |
| 1375 | int swapwrite = current->flags & PF_SWAPWRITE; |
| 1376 | int rc; |
| 1377 | |
| 1378 | if (!swapwrite) |
| 1379 | current->flags |= PF_SWAPWRITE; |
| 1380 | |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1381 | for(pass = 0; pass < 10 && retry; pass++) { |
| 1382 | retry = 0; |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1383 | |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1384 | list_for_each_entry_safe(page, page2, from, lru) { |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1385 | cond_resched(); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1386 | |
Naoya Horiguchi | 31caf66 | 2013-09-11 14:21:59 -0700 | [diff] [blame] | 1387 | if (PageHuge(page)) |
| 1388 | rc = unmap_and_move_huge_page(get_new_page, |
David Rientjes | 68711a7 | 2014-06-04 16:08:25 -0700 | [diff] [blame] | 1389 | put_new_page, private, page, |
Vlastimil Babka | 7cd12b4 | 2016-03-15 14:56:18 -0700 | [diff] [blame] | 1390 | pass > 2, mode, reason); |
Naoya Horiguchi | 31caf66 | 2013-09-11 14:21:59 -0700 | [diff] [blame] | 1391 | else |
David Rientjes | 68711a7 | 2014-06-04 16:08:25 -0700 | [diff] [blame] | 1392 | rc = unmap_and_move(get_new_page, put_new_page, |
Naoya Horiguchi | add05ce | 2015-06-24 16:56:50 -0700 | [diff] [blame] | 1393 | private, page, pass > 2, mode, |
| 1394 | reason); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1395 | |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1396 | switch(rc) { |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 1397 | case -ENOMEM: |
David Rientjes | dfef2ef | 2016-05-20 16:59:05 -0700 | [diff] [blame] | 1398 | nr_failed++; |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 1399 | goto out; |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1400 | case -EAGAIN: |
Christoph Lameter | 2d1db3b | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 1401 | retry++; |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1402 | break; |
Rafael Aquini | 78bd520 | 2012-12-11 16:02:31 -0800 | [diff] [blame] | 1403 | case MIGRATEPAGE_SUCCESS: |
Mel Gorman | 5647bc2 | 2012-10-19 10:46:20 +0100 | [diff] [blame] | 1404 | nr_succeeded++; |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1405 | break; |
| 1406 | default: |
Naoya Horiguchi | 354a336 | 2014-01-21 15:51:14 -0800 | [diff] [blame] | 1407 | /* |
| 1408 | * Permanent failure (-EBUSY, -ENOSYS, etc.): |
| 1409 | * unlike -EAGAIN case, the failed page is |
| 1410 | * removed from migration page list and not |
| 1411 | * retried in the next outer loop. |
| 1412 | */ |
Christoph Lameter | 2d1db3b | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 1413 | nr_failed++; |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1414 | break; |
Christoph Lameter | 2d1db3b | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 1415 | } |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1416 | } |
| 1417 | } |
Vlastimil Babka | f2f81fb | 2015-11-05 18:47:03 -0800 | [diff] [blame] | 1418 | nr_failed += retry; |
| 1419 | rc = nr_failed; |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 1420 | out: |
Mel Gorman | 5647bc2 | 2012-10-19 10:46:20 +0100 | [diff] [blame] | 1421 | if (nr_succeeded) |
| 1422 | count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded); |
| 1423 | if (nr_failed) |
| 1424 | count_vm_events(PGMIGRATE_FAIL, nr_failed); |
Mel Gorman | 7b2a2d4 | 2012-10-19 14:07:31 +0100 | [diff] [blame] | 1425 | trace_mm_migrate_pages(nr_succeeded, nr_failed, mode, reason); |
| 1426 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1427 | if (!swapwrite) |
| 1428 | current->flags &= ~PF_SWAPWRITE; |
| 1429 | |
Rafael Aquini | 78bd520 | 2012-12-11 16:02:31 -0800 | [diff] [blame] | 1430 | return rc; |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1431 | } |
| 1432 | |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1433 | #ifdef CONFIG_NUMA |
| 1434 | /* |
| 1435 | * Move a list of individual pages |
| 1436 | */ |
| 1437 | struct page_to_node { |
| 1438 | unsigned long addr; |
| 1439 | struct page *page; |
| 1440 | int node; |
| 1441 | int status; |
| 1442 | }; |
| 1443 | |
| 1444 | static struct page *new_page_node(struct page *p, unsigned long private, |
| 1445 | int **result) |
| 1446 | { |
| 1447 | struct page_to_node *pm = (struct page_to_node *)private; |
| 1448 | |
| 1449 | while (pm->node != MAX_NUMNODES && pm->page != p) |
| 1450 | pm++; |
| 1451 | |
| 1452 | if (pm->node == MAX_NUMNODES) |
| 1453 | return NULL; |
| 1454 | |
| 1455 | *result = &pm->status; |
| 1456 | |
Naoya Horiguchi | e632a93 | 2013-09-11 14:22:04 -0700 | [diff] [blame] | 1457 | if (PageHuge(p)) |
| 1458 | return alloc_huge_page_node(page_hstate(compound_head(p)), |
| 1459 | pm->node); |
Naoya Horiguchi | e8db67e | 2017-09-08 16:11:12 -0700 | [diff] [blame] | 1460 | else if (thp_migration_supported() && PageTransHuge(p)) { |
| 1461 | struct page *thp; |
| 1462 | |
| 1463 | thp = alloc_pages_node(pm->node, |
| 1464 | (GFP_TRANSHUGE | __GFP_THISNODE) & ~__GFP_RECLAIM, |
| 1465 | HPAGE_PMD_ORDER); |
| 1466 | if (!thp) |
| 1467 | return NULL; |
| 1468 | prep_transhuge_page(thp); |
| 1469 | return thp; |
| 1470 | } else |
Vlastimil Babka | 96db800 | 2015-09-08 15:03:50 -0700 | [diff] [blame] | 1471 | return __alloc_pages_node(pm->node, |
Johannes Weiner | e97ca8e5 | 2014-03-10 15:49:43 -0700 | [diff] [blame] | 1472 | GFP_HIGHUSER_MOVABLE | __GFP_THISNODE, 0); |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1473 | } |
| 1474 | |
| 1475 | /* |
| 1476 | * Move a set of pages as indicated in the pm array. The addr |
| 1477 | * field must be set to the virtual address of the page to be moved |
| 1478 | * and the node number must contain a valid target node. |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1479 | * The pm array ends with node = MAX_NUMNODES. |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1480 | */ |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1481 | static int do_move_page_to_node_array(struct mm_struct *mm, |
| 1482 | struct page_to_node *pm, |
| 1483 | int migrate_all) |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1484 | { |
| 1485 | int err; |
| 1486 | struct page_to_node *pp; |
| 1487 | LIST_HEAD(pagelist); |
| 1488 | |
| 1489 | down_read(&mm->mmap_sem); |
| 1490 | |
| 1491 | /* |
| 1492 | * Build a list of pages to migrate |
| 1493 | */ |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1494 | for (pp = pm; pp->node != MAX_NUMNODES; pp++) { |
| 1495 | struct vm_area_struct *vma; |
| 1496 | struct page *page; |
Naoya Horiguchi | e8db67e | 2017-09-08 16:11:12 -0700 | [diff] [blame] | 1497 | struct page *head; |
| 1498 | unsigned int follflags; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1499 | |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1500 | err = -EFAULT; |
| 1501 | vma = find_vma(mm, pp->addr); |
Gleb Natapov | 70384dc | 2010-10-26 14:22:07 -0700 | [diff] [blame] | 1502 | if (!vma || pp->addr < vma->vm_start || !vma_migratable(vma)) |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1503 | goto set_status; |
| 1504 | |
Kirill A. Shutemov | d899844 | 2015-09-04 15:47:53 -0700 | [diff] [blame] | 1505 | /* FOLL_DUMP to ignore special (like zero) pages */ |
Naoya Horiguchi | e8db67e | 2017-09-08 16:11:12 -0700 | [diff] [blame] | 1506 | follflags = FOLL_GET | FOLL_DUMP; |
| 1507 | if (!thp_migration_supported()) |
| 1508 | follflags |= FOLL_SPLIT; |
| 1509 | page = follow_page(vma, pp->addr, follflags); |
Linus Torvalds | 89f5b7d | 2008-06-20 11:18:25 -0700 | [diff] [blame] | 1510 | |
| 1511 | err = PTR_ERR(page); |
| 1512 | if (IS_ERR(page)) |
| 1513 | goto set_status; |
| 1514 | |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1515 | err = -ENOENT; |
| 1516 | if (!page) |
| 1517 | goto set_status; |
| 1518 | |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1519 | err = page_to_nid(page); |
| 1520 | |
| 1521 | if (err == pp->node) |
| 1522 | /* |
| 1523 | * Node already in the right place |
| 1524 | */ |
| 1525 | goto put_and_set; |
| 1526 | |
| 1527 | err = -EACCES; |
| 1528 | if (page_mapcount(page) > 1 && |
| 1529 | !migrate_all) |
| 1530 | goto put_and_set; |
| 1531 | |
Naoya Horiguchi | e632a93 | 2013-09-11 14:22:04 -0700 | [diff] [blame] | 1532 | if (PageHuge(page)) { |
Naoya Horiguchi | e8db67e | 2017-09-08 16:11:12 -0700 | [diff] [blame] | 1533 | if (PageHead(page)) { |
Naoya Horiguchi | e66f17f | 2015-02-11 15:25:22 -0800 | [diff] [blame] | 1534 | isolate_huge_page(page, &pagelist); |
Naoya Horiguchi | e8db67e | 2017-09-08 16:11:12 -0700 | [diff] [blame] | 1535 | err = 0; |
| 1536 | pp->page = page; |
| 1537 | } |
Naoya Horiguchi | e632a93 | 2013-09-11 14:22:04 -0700 | [diff] [blame] | 1538 | goto put_and_set; |
| 1539 | } |
| 1540 | |
Naoya Horiguchi | e8db67e | 2017-09-08 16:11:12 -0700 | [diff] [blame] | 1541 | pp->page = compound_head(page); |
| 1542 | head = compound_head(page); |
| 1543 | err = isolate_lru_page(head); |
KOSAKI Motohiro | 6d9c285 | 2009-12-14 17:58:11 -0800 | [diff] [blame] | 1544 | if (!err) { |
Naoya Horiguchi | e8db67e | 2017-09-08 16:11:12 -0700 | [diff] [blame] | 1545 | list_add_tail(&head->lru, &pagelist); |
| 1546 | mod_node_page_state(page_pgdat(head), |
| 1547 | NR_ISOLATED_ANON + page_is_file_cache(head), |
| 1548 | hpage_nr_pages(head)); |
KOSAKI Motohiro | 6d9c285 | 2009-12-14 17:58:11 -0800 | [diff] [blame] | 1549 | } |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1550 | put_and_set: |
| 1551 | /* |
| 1552 | * Either remove the duplicate refcount from |
| 1553 | * isolate_lru_page() or drop the page ref if it was |
| 1554 | * not isolated. |
| 1555 | */ |
| 1556 | put_page(page); |
| 1557 | set_status: |
| 1558 | pp->status = err; |
| 1559 | } |
| 1560 | |
Brice Goglin | e78bbfa | 2008-10-18 20:27:15 -0700 | [diff] [blame] | 1561 | err = 0; |
Minchan Kim | cf608ac | 2010-10-26 14:21:29 -0700 | [diff] [blame] | 1562 | if (!list_empty(&pagelist)) { |
David Rientjes | 68711a7 | 2014-06-04 16:08:25 -0700 | [diff] [blame] | 1563 | err = migrate_pages(&pagelist, new_page_node, NULL, |
Hugh Dickins | 9c620e2 | 2013-02-22 16:35:14 -0800 | [diff] [blame] | 1564 | (unsigned long)pm, MIGRATE_SYNC, MR_SYSCALL); |
Minchan Kim | cf608ac | 2010-10-26 14:21:29 -0700 | [diff] [blame] | 1565 | if (err) |
Naoya Horiguchi | e632a93 | 2013-09-11 14:22:04 -0700 | [diff] [blame] | 1566 | putback_movable_pages(&pagelist); |
Minchan Kim | cf608ac | 2010-10-26 14:21:29 -0700 | [diff] [blame] | 1567 | } |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1568 | |
| 1569 | up_read(&mm->mmap_sem); |
| 1570 | return err; |
| 1571 | } |
| 1572 | |
| 1573 | /* |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1574 | * Migrate an array of page address onto an array of nodes and fill |
| 1575 | * the corresponding array of status. |
| 1576 | */ |
Christoph Lameter | 3268c63 | 2012-03-21 16:34:06 -0700 | [diff] [blame] | 1577 | 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] | 1578 | unsigned long nr_pages, |
| 1579 | const void __user * __user *pages, |
| 1580 | const int __user *nodes, |
| 1581 | int __user *status, int flags) |
| 1582 | { |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1583 | struct page_to_node *pm; |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1584 | unsigned long chunk_nr_pages; |
| 1585 | unsigned long chunk_start; |
| 1586 | int err; |
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 | err = -ENOMEM; |
| 1589 | pm = (struct page_to_node *)__get_free_page(GFP_KERNEL); |
| 1590 | if (!pm) |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1591 | goto out; |
Brice Goglin | 35282a2 | 2009-06-16 15:32:43 -0700 | [diff] [blame] | 1592 | |
| 1593 | migrate_prep(); |
| 1594 | |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1595 | /* |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1596 | * Store a chunk of page_to_node array in a page, |
| 1597 | * but keep the last one as a marker |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1598 | */ |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1599 | chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1; |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1600 | |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1601 | for (chunk_start = 0; |
| 1602 | chunk_start < nr_pages; |
| 1603 | chunk_start += chunk_nr_pages) { |
| 1604 | int j; |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1605 | |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1606 | if (chunk_start + chunk_nr_pages > nr_pages) |
| 1607 | chunk_nr_pages = nr_pages - chunk_start; |
| 1608 | |
| 1609 | /* fill the chunk pm with addrs and nodes from user-space */ |
| 1610 | for (j = 0; j < chunk_nr_pages; j++) { |
| 1611 | const void __user *p; |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1612 | int node; |
| 1613 | |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1614 | err = -EFAULT; |
| 1615 | if (get_user(p, pages + j + chunk_start)) |
| 1616 | goto out_pm; |
| 1617 | pm[j].addr = (unsigned long) p; |
| 1618 | |
| 1619 | if (get_user(node, nodes + j + chunk_start)) |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1620 | goto out_pm; |
| 1621 | |
| 1622 | err = -ENODEV; |
Linus Torvalds | 6f5a55f | 2010-02-05 16:16:50 -0800 | [diff] [blame] | 1623 | if (node < 0 || node >= MAX_NUMNODES) |
| 1624 | goto out_pm; |
| 1625 | |
Lai Jiangshan | 389162c | 2012-12-12 13:51:30 -0800 | [diff] [blame] | 1626 | if (!node_state(node, N_MEMORY)) |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1627 | goto out_pm; |
| 1628 | |
| 1629 | err = -EACCES; |
| 1630 | if (!node_isset(node, task_nodes)) |
| 1631 | goto out_pm; |
| 1632 | |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1633 | pm[j].node = node; |
| 1634 | } |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1635 | |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1636 | /* End marker for this chunk */ |
| 1637 | pm[chunk_nr_pages].node = MAX_NUMNODES; |
| 1638 | |
| 1639 | /* Migrate this chunk */ |
| 1640 | err = do_move_page_to_node_array(mm, pm, |
| 1641 | flags & MPOL_MF_MOVE_ALL); |
| 1642 | if (err < 0) |
| 1643 | goto out_pm; |
| 1644 | |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1645 | /* Return status information */ |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1646 | for (j = 0; j < chunk_nr_pages; j++) |
| 1647 | if (put_user(pm[j].status, status + j + chunk_start)) { |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1648 | err = -EFAULT; |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1649 | goto out_pm; |
| 1650 | } |
| 1651 | } |
| 1652 | err = 0; |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1653 | |
| 1654 | out_pm: |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1655 | free_page((unsigned long)pm); |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1656 | out: |
| 1657 | return err; |
| 1658 | } |
| 1659 | |
| 1660 | /* |
Brice Goglin | 2f007e7 | 2008-10-18 20:27:16 -0700 | [diff] [blame] | 1661 | * 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] | 1662 | */ |
Brice Goglin | 80bba12 | 2008-12-09 13:14:23 -0800 | [diff] [blame] | 1663 | static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages, |
| 1664 | const void __user **pages, int *status) |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1665 | { |
Brice Goglin | 2f007e7 | 2008-10-18 20:27:16 -0700 | [diff] [blame] | 1666 | unsigned long i; |
Brice Goglin | 2f007e7 | 2008-10-18 20:27:16 -0700 | [diff] [blame] | 1667 | |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1668 | down_read(&mm->mmap_sem); |
| 1669 | |
Brice Goglin | 2f007e7 | 2008-10-18 20:27:16 -0700 | [diff] [blame] | 1670 | for (i = 0; i < nr_pages; i++) { |
Brice Goglin | 80bba12 | 2008-12-09 13:14:23 -0800 | [diff] [blame] | 1671 | unsigned long addr = (unsigned long)(*pages); |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1672 | struct vm_area_struct *vma; |
| 1673 | struct page *page; |
KOSAKI Motohiro | c095adb | 2008-12-16 16:06:43 +0900 | [diff] [blame] | 1674 | int err = -EFAULT; |
Brice Goglin | 2f007e7 | 2008-10-18 20:27:16 -0700 | [diff] [blame] | 1675 | |
| 1676 | vma = find_vma(mm, addr); |
Gleb Natapov | 70384dc | 2010-10-26 14:22:07 -0700 | [diff] [blame] | 1677 | if (!vma || addr < vma->vm_start) |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1678 | goto set_status; |
| 1679 | |
Kirill A. Shutemov | d899844 | 2015-09-04 15:47:53 -0700 | [diff] [blame] | 1680 | /* FOLL_DUMP to ignore special (like zero) pages */ |
| 1681 | page = follow_page(vma, addr, FOLL_DUMP); |
Linus Torvalds | 89f5b7d | 2008-06-20 11:18:25 -0700 | [diff] [blame] | 1682 | |
| 1683 | err = PTR_ERR(page); |
| 1684 | if (IS_ERR(page)) |
| 1685 | goto set_status; |
| 1686 | |
Kirill A. Shutemov | d899844 | 2015-09-04 15:47:53 -0700 | [diff] [blame] | 1687 | err = page ? page_to_nid(page) : -ENOENT; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1688 | set_status: |
Brice Goglin | 80bba12 | 2008-12-09 13:14:23 -0800 | [diff] [blame] | 1689 | *status = err; |
| 1690 | |
| 1691 | pages++; |
| 1692 | status++; |
| 1693 | } |
| 1694 | |
| 1695 | up_read(&mm->mmap_sem); |
| 1696 | } |
| 1697 | |
| 1698 | /* |
| 1699 | * Determine the nodes of a user array of pages and store it in |
| 1700 | * a user array of status. |
| 1701 | */ |
| 1702 | static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages, |
| 1703 | const void __user * __user *pages, |
| 1704 | int __user *status) |
| 1705 | { |
| 1706 | #define DO_PAGES_STAT_CHUNK_NR 16 |
| 1707 | const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR]; |
| 1708 | int chunk_status[DO_PAGES_STAT_CHUNK_NR]; |
Brice Goglin | 80bba12 | 2008-12-09 13:14:23 -0800 | [diff] [blame] | 1709 | |
H. Peter Anvin | 87b8d1a | 2010-02-18 16:13:40 -0800 | [diff] [blame] | 1710 | while (nr_pages) { |
| 1711 | unsigned long chunk_nr; |
Brice Goglin | 80bba12 | 2008-12-09 13:14:23 -0800 | [diff] [blame] | 1712 | |
H. Peter Anvin | 87b8d1a | 2010-02-18 16:13:40 -0800 | [diff] [blame] | 1713 | chunk_nr = nr_pages; |
| 1714 | if (chunk_nr > DO_PAGES_STAT_CHUNK_NR) |
| 1715 | chunk_nr = DO_PAGES_STAT_CHUNK_NR; |
| 1716 | |
| 1717 | if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages))) |
| 1718 | break; |
Brice Goglin | 80bba12 | 2008-12-09 13:14:23 -0800 | [diff] [blame] | 1719 | |
| 1720 | do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status); |
| 1721 | |
H. Peter Anvin | 87b8d1a | 2010-02-18 16:13:40 -0800 | [diff] [blame] | 1722 | if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status))) |
| 1723 | break; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1724 | |
H. Peter Anvin | 87b8d1a | 2010-02-18 16:13:40 -0800 | [diff] [blame] | 1725 | pages += chunk_nr; |
| 1726 | status += chunk_nr; |
| 1727 | nr_pages -= chunk_nr; |
| 1728 | } |
| 1729 | return nr_pages ? -EFAULT : 0; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1730 | } |
| 1731 | |
| 1732 | /* |
| 1733 | * Move a list of pages in the address space of the currently executing |
| 1734 | * process. |
| 1735 | */ |
Heiko Carstens | 938bb9f | 2009-01-14 14:14:30 +0100 | [diff] [blame] | 1736 | SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages, |
| 1737 | const void __user * __user *, pages, |
| 1738 | const int __user *, nodes, |
| 1739 | int __user *, status, int, flags) |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1740 | { |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1741 | struct task_struct *task; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1742 | struct mm_struct *mm; |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1743 | int err; |
Christoph Lameter | 3268c63 | 2012-03-21 16:34:06 -0700 | [diff] [blame] | 1744 | nodemask_t task_nodes; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1745 | |
| 1746 | /* Check flags */ |
| 1747 | if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL)) |
| 1748 | return -EINVAL; |
| 1749 | |
| 1750 | if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE)) |
| 1751 | return -EPERM; |
| 1752 | |
| 1753 | /* Find the mm_struct */ |
Greg Thelen | a879bf5 | 2011-02-25 14:44:13 -0800 | [diff] [blame] | 1754 | rcu_read_lock(); |
Pavel Emelyanov | 228ebcb | 2007-10-18 23:40:16 -0700 | [diff] [blame] | 1755 | task = pid ? find_task_by_vpid(pid) : current; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1756 | if (!task) { |
Greg Thelen | a879bf5 | 2011-02-25 14:44:13 -0800 | [diff] [blame] | 1757 | rcu_read_unlock(); |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1758 | return -ESRCH; |
| 1759 | } |
Christoph Lameter | 3268c63 | 2012-03-21 16:34:06 -0700 | [diff] [blame] | 1760 | get_task_struct(task); |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1761 | |
| 1762 | /* |
| 1763 | * Check if this process has the right to modify the specified |
Linus Torvalds | 197e7e5 | 2017-08-20 13:26:27 -0700 | [diff] [blame] | 1764 | * process. Use the regular "ptrace_may_access()" checks. |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1765 | */ |
Linus Torvalds | 197e7e5 | 2017-08-20 13:26:27 -0700 | [diff] [blame] | 1766 | if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1767 | rcu_read_unlock(); |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1768 | err = -EPERM; |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1769 | goto out; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1770 | } |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1771 | rcu_read_unlock(); |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1772 | |
David Quigley | 86c3a76 | 2006-06-23 02:04:02 -0700 | [diff] [blame] | 1773 | err = security_task_movememory(task); |
| 1774 | if (err) |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1775 | goto out; |
David Quigley | 86c3a76 | 2006-06-23 02:04:02 -0700 | [diff] [blame] | 1776 | |
Christoph Lameter | 3268c63 | 2012-03-21 16:34:06 -0700 | [diff] [blame] | 1777 | task_nodes = cpuset_mems_allowed(task); |
| 1778 | mm = get_task_mm(task); |
| 1779 | put_task_struct(task); |
| 1780 | |
Sasha Levin | 6e8b09e | 2012-04-25 16:01:53 -0700 | [diff] [blame] | 1781 | if (!mm) |
| 1782 | return -EINVAL; |
| 1783 | |
| 1784 | if (nodes) |
| 1785 | err = do_pages_move(mm, task_nodes, nr_pages, pages, |
| 1786 | nodes, status, flags); |
| 1787 | else |
| 1788 | err = do_pages_stat(mm, nr_pages, pages, status); |
Christoph Lameter | 3268c63 | 2012-03-21 16:34:06 -0700 | [diff] [blame] | 1789 | |
| 1790 | mmput(mm); |
| 1791 | return err; |
David Quigley | 86c3a76 | 2006-06-23 02:04:02 -0700 | [diff] [blame] | 1792 | |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1793 | out: |
Christoph Lameter | 3268c63 | 2012-03-21 16:34:06 -0700 | [diff] [blame] | 1794 | put_task_struct(task); |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1795 | return err; |
| 1796 | } |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1797 | |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 1798 | #ifdef CONFIG_NUMA_BALANCING |
| 1799 | /* |
| 1800 | * Returns true if this is a safe migration target node for misplaced NUMA |
| 1801 | * pages. Currently it only checks the watermarks which crude |
| 1802 | */ |
| 1803 | static bool migrate_balanced_pgdat(struct pglist_data *pgdat, |
Mel Gorman | 3abef4e | 2013-02-22 16:34:27 -0800 | [diff] [blame] | 1804 | unsigned long nr_migrate_pages) |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 1805 | { |
| 1806 | int z; |
Mel Gorman | 599d0c9 | 2016-07-28 15:45:31 -0700 | [diff] [blame] | 1807 | |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 1808 | for (z = pgdat->nr_zones - 1; z >= 0; z--) { |
| 1809 | struct zone *zone = pgdat->node_zones + z; |
| 1810 | |
| 1811 | if (!populated_zone(zone)) |
| 1812 | continue; |
| 1813 | |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 1814 | /* Avoid waking kswapd by allocating pages_to_migrate pages. */ |
| 1815 | if (!zone_watermark_ok(zone, 0, |
| 1816 | high_wmark_pages(zone) + |
| 1817 | nr_migrate_pages, |
| 1818 | 0, 0)) |
| 1819 | continue; |
| 1820 | return true; |
| 1821 | } |
| 1822 | return false; |
| 1823 | } |
| 1824 | |
| 1825 | static struct page *alloc_misplaced_dst_page(struct page *page, |
| 1826 | unsigned long data, |
| 1827 | int **result) |
| 1828 | { |
| 1829 | int nid = (int) data; |
| 1830 | struct page *newpage; |
| 1831 | |
Vlastimil Babka | 96db800 | 2015-09-08 15:03:50 -0700 | [diff] [blame] | 1832 | newpage = __alloc_pages_node(nid, |
Johannes Weiner | e97ca8e5 | 2014-03-10 15:49:43 -0700 | [diff] [blame] | 1833 | (GFP_HIGHUSER_MOVABLE | |
| 1834 | __GFP_THISNODE | __GFP_NOMEMALLOC | |
| 1835 | __GFP_NORETRY | __GFP_NOWARN) & |
Mel Gorman | 8479eba | 2016-02-26 15:19:31 -0800 | [diff] [blame] | 1836 | ~__GFP_RECLAIM, 0); |
Hillf Danton | bac0382 | 2012-11-27 14:46:24 +0000 | [diff] [blame] | 1837 | |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 1838 | return newpage; |
| 1839 | } |
| 1840 | |
| 1841 | /* |
Mel Gorman | a8f6077 | 2012-11-14 21:41:46 +0000 | [diff] [blame] | 1842 | * page migration rate limiting control. |
| 1843 | * Do not migrate more than @pages_to_migrate in a @migrate_interval_millisecs |
| 1844 | * window of time. Default here says do not migrate more than 1280M per second. |
| 1845 | */ |
| 1846 | static unsigned int migrate_interval_millisecs __read_mostly = 100; |
| 1847 | static unsigned int ratelimit_pages __read_mostly = 128 << (20 - PAGE_SHIFT); |
| 1848 | |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1849 | /* Returns true if the node is migrate rate-limited after the update */ |
Mel Gorman | 1c30e01 | 2014-01-21 15:50:58 -0800 | [diff] [blame] | 1850 | static bool numamigrate_update_ratelimit(pg_data_t *pgdat, |
| 1851 | unsigned long nr_pages) |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1852 | { |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1853 | /* |
| 1854 | * Rate-limit the amount of data that is being migrated to a node. |
| 1855 | * Optimal placement is no good if the memory bus is saturated and |
| 1856 | * all the time is being spent migrating! |
| 1857 | */ |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1858 | if (time_after(jiffies, pgdat->numabalancing_migrate_next_window)) { |
Mel Gorman | 1c5e9c2 | 2014-01-21 15:50:59 -0800 | [diff] [blame] | 1859 | spin_lock(&pgdat->numabalancing_migrate_lock); |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1860 | pgdat->numabalancing_migrate_nr_pages = 0; |
| 1861 | pgdat->numabalancing_migrate_next_window = jiffies + |
| 1862 | msecs_to_jiffies(migrate_interval_millisecs); |
Mel Gorman | 1c5e9c2 | 2014-01-21 15:50:59 -0800 | [diff] [blame] | 1863 | spin_unlock(&pgdat->numabalancing_migrate_lock); |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1864 | } |
Mel Gorman | af1839d | 2014-01-21 15:51:01 -0800 | [diff] [blame] | 1865 | if (pgdat->numabalancing_migrate_nr_pages > ratelimit_pages) { |
| 1866 | trace_mm_numa_migrate_ratelimit(current, pgdat->node_id, |
| 1867 | nr_pages); |
Mel Gorman | 1c5e9c2 | 2014-01-21 15:50:59 -0800 | [diff] [blame] | 1868 | return true; |
Mel Gorman | af1839d | 2014-01-21 15:51:01 -0800 | [diff] [blame] | 1869 | } |
Mel Gorman | 1c5e9c2 | 2014-01-21 15:50:59 -0800 | [diff] [blame] | 1870 | |
| 1871 | /* |
| 1872 | * This is an unlocked non-atomic update so errors are possible. |
| 1873 | * The consequences are failing to migrate when we potentiall should |
| 1874 | * have which is not severe enough to warrant locking. If it is ever |
| 1875 | * a problem, it can be converted to a per-cpu counter. |
| 1876 | */ |
| 1877 | pgdat->numabalancing_migrate_nr_pages += nr_pages; |
| 1878 | return false; |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1879 | } |
| 1880 | |
Mel Gorman | 1c30e01 | 2014-01-21 15:50:58 -0800 | [diff] [blame] | 1881 | static int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page) |
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 | int page_lru; |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1884 | |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 1885 | VM_BUG_ON_PAGE(compound_order(page) && !PageTransHuge(page), page); |
Mel Gorman | 3abef4e | 2013-02-22 16:34:27 -0800 | [diff] [blame] | 1886 | |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1887 | /* Avoid migrating to a node that is nearly full */ |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 1888 | if (!migrate_balanced_pgdat(pgdat, 1UL << compound_order(page))) |
| 1889 | return 0; |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1890 | |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 1891 | if (isolate_lru_page(page)) |
| 1892 | return 0; |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1893 | |
| 1894 | /* |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 1895 | * migrate_misplaced_transhuge_page() skips page migration's usual |
| 1896 | * check on page_count(), so we must do it here, now that the page |
| 1897 | * has been isolated: a GUP pin, or any other pin, prevents migration. |
| 1898 | * The expected page count is 3: 1 for page's mapcount and 1 for the |
| 1899 | * caller's pin and 1 for the reference taken by isolate_lru_page(). |
| 1900 | */ |
| 1901 | if (PageTransHuge(page) && page_count(page) != 3) { |
| 1902 | putback_lru_page(page); |
| 1903 | return 0; |
| 1904 | } |
| 1905 | |
| 1906 | page_lru = page_is_file_cache(page); |
Mel Gorman | 599d0c9 | 2016-07-28 15:45:31 -0700 | [diff] [blame] | 1907 | mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + page_lru, |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 1908 | hpage_nr_pages(page)); |
| 1909 | |
| 1910 | /* |
| 1911 | * Isolating the page has taken another reference, so the |
| 1912 | * caller's reference can be safely dropped without the page |
| 1913 | * disappearing underneath us during migration. |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1914 | */ |
| 1915 | put_page(page); |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 1916 | return 1; |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1917 | } |
| 1918 | |
Mel Gorman | de466bd | 2013-12-18 17:08:42 -0800 | [diff] [blame] | 1919 | bool pmd_trans_migrating(pmd_t pmd) |
| 1920 | { |
| 1921 | struct page *page = pmd_page(pmd); |
| 1922 | return PageLocked(page); |
| 1923 | } |
| 1924 | |
Mel Gorman | a8f6077 | 2012-11-14 21:41:46 +0000 | [diff] [blame] | 1925 | /* |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 1926 | * Attempt to migrate a misplaced page to the specified destination |
| 1927 | * node. Caller is expected to have an elevated reference count on |
| 1928 | * the page that will be dropped by this function before returning. |
| 1929 | */ |
Mel Gorman | 1bc115d | 2013-10-07 11:29:05 +0100 | [diff] [blame] | 1930 | int migrate_misplaced_page(struct page *page, struct vm_area_struct *vma, |
| 1931 | int node) |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 1932 | { |
Mel Gorman | a8f6077 | 2012-11-14 21:41:46 +0000 | [diff] [blame] | 1933 | pg_data_t *pgdat = NODE_DATA(node); |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 1934 | int isolated; |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1935 | int nr_remaining; |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 1936 | LIST_HEAD(migratepages); |
| 1937 | |
| 1938 | /* |
Mel Gorman | 1bc115d | 2013-10-07 11:29:05 +0100 | [diff] [blame] | 1939 | * Don't migrate file pages that are mapped in multiple processes |
| 1940 | * with execute permissions as they are probably shared libraries. |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 1941 | */ |
Mel Gorman | 1bc115d | 2013-10-07 11:29:05 +0100 | [diff] [blame] | 1942 | if (page_mapcount(page) != 1 && page_is_file_cache(page) && |
| 1943 | (vma->vm_flags & VM_EXEC)) |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 1944 | goto out; |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 1945 | |
Mel Gorman | a8f6077 | 2012-11-14 21:41:46 +0000 | [diff] [blame] | 1946 | /* |
| 1947 | * Rate-limit the amount of data that is being migrated to a node. |
| 1948 | * Optimal placement is no good if the memory bus is saturated and |
| 1949 | * all the time is being spent migrating! |
| 1950 | */ |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 1951 | if (numamigrate_update_ratelimit(pgdat, 1)) |
Mel Gorman | a8f6077 | 2012-11-14 21:41:46 +0000 | [diff] [blame] | 1952 | goto out; |
Mel Gorman | a8f6077 | 2012-11-14 21:41:46 +0000 | [diff] [blame] | 1953 | |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1954 | isolated = numamigrate_isolate_page(pgdat, page); |
| 1955 | if (!isolated) |
| 1956 | goto out; |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 1957 | |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1958 | list_add(&page->lru, &migratepages); |
Hugh Dickins | 9c620e2 | 2013-02-22 16:35:14 -0800 | [diff] [blame] | 1959 | nr_remaining = migrate_pages(&migratepages, alloc_misplaced_dst_page, |
David Rientjes | 68711a7 | 2014-06-04 16:08:25 -0700 | [diff] [blame] | 1960 | NULL, node, MIGRATE_ASYNC, |
| 1961 | MR_NUMA_MISPLACED); |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1962 | if (nr_remaining) { |
Joonsoo Kim | 59c82b7 | 2014-01-21 15:51:17 -0800 | [diff] [blame] | 1963 | if (!list_empty(&migratepages)) { |
| 1964 | list_del(&page->lru); |
Mel Gorman | 599d0c9 | 2016-07-28 15:45:31 -0700 | [diff] [blame] | 1965 | dec_node_page_state(page, NR_ISOLATED_ANON + |
Joonsoo Kim | 59c82b7 | 2014-01-21 15:51:17 -0800 | [diff] [blame] | 1966 | page_is_file_cache(page)); |
| 1967 | putback_lru_page(page); |
| 1968 | } |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1969 | isolated = 0; |
| 1970 | } else |
| 1971 | count_vm_numa_event(NUMA_PAGE_MIGRATE); |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 1972 | BUG_ON(!list_empty(&migratepages)); |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 1973 | return isolated; |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 1974 | |
| 1975 | out: |
| 1976 | put_page(page); |
| 1977 | return 0; |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 1978 | } |
Mel Gorman | 220018d | 2012-12-05 09:32:56 +0000 | [diff] [blame] | 1979 | #endif /* CONFIG_NUMA_BALANCING */ |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1980 | |
Mel Gorman | 220018d | 2012-12-05 09:32:56 +0000 | [diff] [blame] | 1981 | #if defined(CONFIG_NUMA_BALANCING) && defined(CONFIG_TRANSPARENT_HUGEPAGE) |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 1982 | /* |
| 1983 | * Migrates a THP to a given target node. page must be locked and is unlocked |
| 1984 | * before returning. |
| 1985 | */ |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1986 | int migrate_misplaced_transhuge_page(struct mm_struct *mm, |
| 1987 | struct vm_area_struct *vma, |
| 1988 | pmd_t *pmd, pmd_t entry, |
| 1989 | unsigned long address, |
| 1990 | struct page *page, int node) |
| 1991 | { |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 1992 | spinlock_t *ptl; |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1993 | pg_data_t *pgdat = NODE_DATA(node); |
| 1994 | int isolated = 0; |
| 1995 | struct page *new_page = NULL; |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1996 | int page_lru = page_is_file_cache(page); |
Mel Gorman | f714f4f | 2013-12-18 17:08:33 -0800 | [diff] [blame] | 1997 | unsigned long mmun_start = address & HPAGE_PMD_MASK; |
| 1998 | unsigned long mmun_end = mmun_start + HPAGE_PMD_SIZE; |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1999 | |
| 2000 | /* |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2001 | * Rate-limit the amount of data that is being migrated to a node. |
| 2002 | * Optimal placement is no good if the memory bus is saturated and |
| 2003 | * all the time is being spent migrating! |
| 2004 | */ |
Mel Gorman | d28d4335 | 2012-11-29 09:24:36 +0000 | [diff] [blame] | 2005 | if (numamigrate_update_ratelimit(pgdat, HPAGE_PMD_NR)) |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2006 | goto out_dropref; |
| 2007 | |
| 2008 | new_page = alloc_pages_node(node, |
Vlastimil Babka | 2516035 | 2016-07-28 15:49:25 -0700 | [diff] [blame] | 2009 | (GFP_TRANSHUGE_LIGHT | __GFP_THISNODE), |
Johannes Weiner | e97ca8e5 | 2014-03-10 15:49:43 -0700 | [diff] [blame] | 2010 | HPAGE_PMD_ORDER); |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 2011 | if (!new_page) |
| 2012 | goto out_fail; |
Kirill A. Shutemov | 9a98225 | 2016-01-15 16:54:17 -0800 | [diff] [blame] | 2013 | prep_transhuge_page(new_page); |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 2014 | |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2015 | isolated = numamigrate_isolate_page(pgdat, page); |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 2016 | if (!isolated) { |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2017 | put_page(new_page); |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 2018 | goto out_fail; |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2019 | } |
Mel Gorman | b0943d6 | 2013-12-18 17:08:46 -0800 | [diff] [blame] | 2020 | |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2021 | /* Prepare a page as a migration target */ |
Kirill A. Shutemov | 48c935a | 2016-01-15 16:51:24 -0800 | [diff] [blame] | 2022 | __SetPageLocked(new_page); |
Shaohua Li | d44d363 | 2017-05-03 14:52:26 -0700 | [diff] [blame] | 2023 | if (PageSwapBacked(page)) |
| 2024 | __SetPageSwapBacked(new_page); |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2025 | |
| 2026 | /* anon mapping, we can simply copy page->mapping to the new page: */ |
| 2027 | new_page->mapping = page->mapping; |
| 2028 | new_page->index = page->index; |
| 2029 | migrate_page_copy(new_page, page); |
| 2030 | WARN_ON(PageLRU(new_page)); |
| 2031 | |
| 2032 | /* Recheck the target PMD */ |
Mel Gorman | f714f4f | 2013-12-18 17:08:33 -0800 | [diff] [blame] | 2033 | mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 2034 | ptl = pmd_lock(mm, pmd); |
Will Deacon | f4e177d | 2017-07-10 15:48:31 -0700 | [diff] [blame] | 2035 | if (unlikely(!pmd_same(*pmd, entry) || !page_ref_freeze(page, 2))) { |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 2036 | spin_unlock(ptl); |
Mel Gorman | f714f4f | 2013-12-18 17:08:33 -0800 | [diff] [blame] | 2037 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2038 | |
| 2039 | /* Reverse changes made by migrate_page_copy() */ |
| 2040 | if (TestClearPageActive(new_page)) |
| 2041 | SetPageActive(page); |
| 2042 | if (TestClearPageUnevictable(new_page)) |
| 2043 | SetPageUnevictable(page); |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2044 | |
| 2045 | unlock_page(new_page); |
| 2046 | put_page(new_page); /* Free it */ |
| 2047 | |
Mel Gorman | a54a407 | 2013-10-07 11:28:46 +0100 | [diff] [blame] | 2048 | /* Retake the callers reference and putback on LRU */ |
| 2049 | get_page(page); |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2050 | putback_lru_page(page); |
Mel Gorman | 599d0c9 | 2016-07-28 15:45:31 -0700 | [diff] [blame] | 2051 | mod_node_page_state(page_pgdat(page), |
Mel Gorman | a54a407 | 2013-10-07 11:28:46 +0100 | [diff] [blame] | 2052 | NR_ISOLATED_ANON + page_lru, -HPAGE_PMD_NR); |
Mel Gorman | eb4489f6 | 2013-12-18 17:08:39 -0800 | [diff] [blame] | 2053 | |
| 2054 | goto out_unlock; |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2055 | } |
| 2056 | |
Kirill A. Shutemov | 1010245 | 2016-07-26 15:25:29 -0700 | [diff] [blame] | 2057 | entry = mk_huge_pmd(new_page, vma->vm_page_prot); |
Mel Gorman | 2b4847e | 2013-12-18 17:08:32 -0800 | [diff] [blame] | 2058 | entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2059 | |
Mel Gorman | 2b4847e | 2013-12-18 17:08:32 -0800 | [diff] [blame] | 2060 | /* |
| 2061 | * Clear the old entry under pagetable lock and establish the new PTE. |
| 2062 | * Any parallel GUP will either observe the old page blocking on the |
| 2063 | * page lock, block on the page table lock or observe the new page. |
| 2064 | * The SetPageUptodate on the new page and page_add_new_anon_rmap |
| 2065 | * guarantee the copy is visible before the pagetable update. |
| 2066 | */ |
Mel Gorman | f714f4f | 2013-12-18 17:08:33 -0800 | [diff] [blame] | 2067 | flush_cache_range(vma, mmun_start, mmun_end); |
Kirill A. Shutemov | d281ee6 | 2016-01-15 16:52:16 -0800 | [diff] [blame] | 2068 | page_add_anon_rmap(new_page, vma, mmun_start, true); |
Aneesh Kumar K.V | 8809aa2 | 2015-06-24 16:57:44 -0700 | [diff] [blame] | 2069 | pmdp_huge_clear_flush_notify(vma, mmun_start, pmd); |
Mel Gorman | f714f4f | 2013-12-18 17:08:33 -0800 | [diff] [blame] | 2070 | set_pmd_at(mm, mmun_start, pmd, entry); |
Stephen Rothwell | ce4a9cc | 2012-12-10 19:50:57 +1100 | [diff] [blame] | 2071 | update_mmu_cache_pmd(vma, address, &entry); |
Mel Gorman | 2b4847e | 2013-12-18 17:08:32 -0800 | [diff] [blame] | 2072 | |
Will Deacon | f4e177d | 2017-07-10 15:48:31 -0700 | [diff] [blame] | 2073 | page_ref_unfreeze(page, 2); |
Hugh Dickins | 51afb12 | 2015-11-05 18:49:37 -0800 | [diff] [blame] | 2074 | mlock_migrate_page(new_page, page); |
Kirill A. Shutemov | d281ee6 | 2016-01-15 16:52:16 -0800 | [diff] [blame] | 2075 | page_remove_rmap(page, true); |
Vlastimil Babka | 7cd12b4 | 2016-03-15 14:56:18 -0700 | [diff] [blame] | 2076 | set_page_owner_migrate_reason(new_page, MR_NUMA_MISPLACED); |
Mel Gorman | 2b4847e | 2013-12-18 17:08:32 -0800 | [diff] [blame] | 2077 | |
Kirill A. Shutemov | c4088eb | 2013-11-14 14:31:04 -0800 | [diff] [blame] | 2078 | spin_unlock(ptl); |
Mel Gorman | f714f4f | 2013-12-18 17:08:33 -0800 | [diff] [blame] | 2079 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2080 | |
Mel Gorman | 11de992 | 2014-06-04 16:07:41 -0700 | [diff] [blame] | 2081 | /* Take an "isolate" reference and put new page on the LRU. */ |
| 2082 | get_page(new_page); |
| 2083 | putback_lru_page(new_page); |
| 2084 | |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2085 | unlock_page(new_page); |
| 2086 | unlock_page(page); |
| 2087 | put_page(page); /* Drop the rmap reference */ |
| 2088 | put_page(page); /* Drop the LRU isolation reference */ |
| 2089 | |
| 2090 | count_vm_events(PGMIGRATE_SUCCESS, HPAGE_PMD_NR); |
| 2091 | count_vm_numa_events(NUMA_PAGE_MIGRATE, HPAGE_PMD_NR); |
| 2092 | |
Mel Gorman | 599d0c9 | 2016-07-28 15:45:31 -0700 | [diff] [blame] | 2093 | mod_node_page_state(page_pgdat(page), |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2094 | NR_ISOLATED_ANON + page_lru, |
| 2095 | -HPAGE_PMD_NR); |
| 2096 | return isolated; |
| 2097 | |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 2098 | out_fail: |
| 2099 | count_vm_events(PGMIGRATE_FAIL, HPAGE_PMD_NR); |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2100 | out_dropref: |
Mel Gorman | 2b4847e | 2013-12-18 17:08:32 -0800 | [diff] [blame] | 2101 | ptl = pmd_lock(mm, pmd); |
| 2102 | if (pmd_same(*pmd, entry)) { |
Mel Gorman | 4d94246 | 2015-02-12 14:58:28 -0800 | [diff] [blame] | 2103 | entry = pmd_modify(entry, vma->vm_page_prot); |
Mel Gorman | f714f4f | 2013-12-18 17:08:33 -0800 | [diff] [blame] | 2104 | set_pmd_at(mm, mmun_start, pmd, entry); |
Mel Gorman | 2b4847e | 2013-12-18 17:08:32 -0800 | [diff] [blame] | 2105 | update_mmu_cache_pmd(vma, address, &entry); |
| 2106 | } |
| 2107 | spin_unlock(ptl); |
Mel Gorman | a54a407 | 2013-10-07 11:28:46 +0100 | [diff] [blame] | 2108 | |
Mel Gorman | eb4489f6 | 2013-12-18 17:08:39 -0800 | [diff] [blame] | 2109 | out_unlock: |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 2110 | unlock_page(page); |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2111 | put_page(page); |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 2112 | return 0; |
| 2113 | } |
Peter Zijlstra | 7039e1d | 2012-10-25 14:16:34 +0200 | [diff] [blame] | 2114 | #endif /* CONFIG_NUMA_BALANCING */ |
| 2115 | |
| 2116 | #endif /* CONFIG_NUMA */ |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2117 | |
| 2118 | |
| 2119 | struct migrate_vma { |
| 2120 | struct vm_area_struct *vma; |
| 2121 | unsigned long *dst; |
| 2122 | unsigned long *src; |
| 2123 | unsigned long cpages; |
| 2124 | unsigned long npages; |
| 2125 | unsigned long start; |
| 2126 | unsigned long end; |
| 2127 | }; |
| 2128 | |
| 2129 | static int migrate_vma_collect_hole(unsigned long start, |
| 2130 | unsigned long end, |
| 2131 | struct mm_walk *walk) |
| 2132 | { |
| 2133 | struct migrate_vma *migrate = walk->private; |
| 2134 | unsigned long addr; |
| 2135 | |
| 2136 | for (addr = start & PAGE_MASK; addr < end; addr += PAGE_SIZE) { |
| 2137 | migrate->dst[migrate->npages] = 0; |
| 2138 | migrate->src[migrate->npages++] = 0; |
| 2139 | } |
| 2140 | |
| 2141 | return 0; |
| 2142 | } |
| 2143 | |
| 2144 | static int migrate_vma_collect_pmd(pmd_t *pmdp, |
| 2145 | unsigned long start, |
| 2146 | unsigned long end, |
| 2147 | struct mm_walk *walk) |
| 2148 | { |
| 2149 | struct migrate_vma *migrate = walk->private; |
| 2150 | struct vm_area_struct *vma = walk->vma; |
| 2151 | struct mm_struct *mm = vma->vm_mm; |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame^] | 2152 | unsigned long addr = start, unmapped = 0; |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2153 | spinlock_t *ptl; |
| 2154 | pte_t *ptep; |
| 2155 | |
| 2156 | again: |
| 2157 | if (pmd_none(*pmdp)) |
| 2158 | return migrate_vma_collect_hole(start, end, walk); |
| 2159 | |
| 2160 | if (pmd_trans_huge(*pmdp)) { |
| 2161 | struct page *page; |
| 2162 | |
| 2163 | ptl = pmd_lock(mm, pmdp); |
| 2164 | if (unlikely(!pmd_trans_huge(*pmdp))) { |
| 2165 | spin_unlock(ptl); |
| 2166 | goto again; |
| 2167 | } |
| 2168 | |
| 2169 | page = pmd_page(*pmdp); |
| 2170 | if (is_huge_zero_page(page)) { |
| 2171 | spin_unlock(ptl); |
| 2172 | split_huge_pmd(vma, pmdp, addr); |
| 2173 | if (pmd_trans_unstable(pmdp)) |
| 2174 | return migrate_vma_collect_hole(start, end, |
| 2175 | walk); |
| 2176 | } else { |
| 2177 | int ret; |
| 2178 | |
| 2179 | get_page(page); |
| 2180 | spin_unlock(ptl); |
| 2181 | if (unlikely(!trylock_page(page))) |
| 2182 | return migrate_vma_collect_hole(start, end, |
| 2183 | walk); |
| 2184 | ret = split_huge_page(page); |
| 2185 | unlock_page(page); |
| 2186 | put_page(page); |
| 2187 | if (ret || pmd_none(*pmdp)) |
| 2188 | return migrate_vma_collect_hole(start, end, |
| 2189 | walk); |
| 2190 | } |
| 2191 | } |
| 2192 | |
| 2193 | if (unlikely(pmd_bad(*pmdp))) |
| 2194 | return migrate_vma_collect_hole(start, end, walk); |
| 2195 | |
| 2196 | ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl); |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame^] | 2197 | arch_enter_lazy_mmu_mode(); |
| 2198 | |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2199 | for (; addr < end; addr += PAGE_SIZE, ptep++) { |
| 2200 | unsigned long mpfn, pfn; |
| 2201 | struct page *page; |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame^] | 2202 | swp_entry_t entry; |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2203 | pte_t pte; |
| 2204 | |
| 2205 | pte = *ptep; |
| 2206 | pfn = pte_pfn(pte); |
| 2207 | |
| 2208 | if (!pte_present(pte)) { |
| 2209 | mpfn = pfn = 0; |
| 2210 | goto next; |
| 2211 | } |
| 2212 | |
| 2213 | /* FIXME support THP */ |
| 2214 | page = vm_normal_page(migrate->vma, addr, pte); |
| 2215 | if (!page || !page->mapping || PageTransCompound(page)) { |
| 2216 | mpfn = pfn = 0; |
| 2217 | goto next; |
| 2218 | } |
| 2219 | |
| 2220 | /* |
| 2221 | * By getting a reference on the page we pin it and that blocks |
| 2222 | * any kind of migration. Side effect is that it "freezes" the |
| 2223 | * pte. |
| 2224 | * |
| 2225 | * We drop this reference after isolating the page from the lru |
| 2226 | * for non device page (device page are not on the lru and thus |
| 2227 | * can't be dropped from it). |
| 2228 | */ |
| 2229 | get_page(page); |
| 2230 | migrate->cpages++; |
| 2231 | mpfn = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE; |
| 2232 | mpfn |= pte_write(pte) ? MIGRATE_PFN_WRITE : 0; |
| 2233 | |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame^] | 2234 | /* |
| 2235 | * Optimize for the common case where page is only mapped once |
| 2236 | * in one process. If we can lock the page, then we can safely |
| 2237 | * set up a special migration page table entry now. |
| 2238 | */ |
| 2239 | if (trylock_page(page)) { |
| 2240 | pte_t swp_pte; |
| 2241 | |
| 2242 | mpfn |= MIGRATE_PFN_LOCKED; |
| 2243 | ptep_get_and_clear(mm, addr, ptep); |
| 2244 | |
| 2245 | /* Setup special migration page table entry */ |
| 2246 | entry = make_migration_entry(page, pte_write(pte)); |
| 2247 | swp_pte = swp_entry_to_pte(entry); |
| 2248 | if (pte_soft_dirty(pte)) |
| 2249 | swp_pte = pte_swp_mksoft_dirty(swp_pte); |
| 2250 | set_pte_at(mm, addr, ptep, swp_pte); |
| 2251 | |
| 2252 | /* |
| 2253 | * This is like regular unmap: we remove the rmap and |
| 2254 | * drop page refcount. Page won't be freed, as we took |
| 2255 | * a reference just above. |
| 2256 | */ |
| 2257 | page_remove_rmap(page, false); |
| 2258 | put_page(page); |
| 2259 | unmapped++; |
| 2260 | } |
| 2261 | |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2262 | next: |
| 2263 | migrate->src[migrate->npages++] = mpfn; |
| 2264 | } |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame^] | 2265 | arch_leave_lazy_mmu_mode(); |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2266 | pte_unmap_unlock(ptep - 1, ptl); |
| 2267 | |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame^] | 2268 | /* Only flush the TLB if we actually modified any entries */ |
| 2269 | if (unmapped) |
| 2270 | flush_tlb_range(walk->vma, start, end); |
| 2271 | |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2272 | return 0; |
| 2273 | } |
| 2274 | |
| 2275 | /* |
| 2276 | * migrate_vma_collect() - collect pages over a range of virtual addresses |
| 2277 | * @migrate: migrate struct containing all migration information |
| 2278 | * |
| 2279 | * This will walk the CPU page table. For each virtual address backed by a |
| 2280 | * valid page, it updates the src array and takes a reference on the page, in |
| 2281 | * order to pin the page until we lock it and unmap it. |
| 2282 | */ |
| 2283 | static void migrate_vma_collect(struct migrate_vma *migrate) |
| 2284 | { |
| 2285 | struct mm_walk mm_walk; |
| 2286 | |
| 2287 | mm_walk.pmd_entry = migrate_vma_collect_pmd; |
| 2288 | mm_walk.pte_entry = NULL; |
| 2289 | mm_walk.pte_hole = migrate_vma_collect_hole; |
| 2290 | mm_walk.hugetlb_entry = NULL; |
| 2291 | mm_walk.test_walk = NULL; |
| 2292 | mm_walk.vma = migrate->vma; |
| 2293 | mm_walk.mm = migrate->vma->vm_mm; |
| 2294 | mm_walk.private = migrate; |
| 2295 | |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame^] | 2296 | mmu_notifier_invalidate_range_start(mm_walk.mm, |
| 2297 | migrate->start, |
| 2298 | migrate->end); |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2299 | walk_page_range(migrate->start, migrate->end, &mm_walk); |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame^] | 2300 | mmu_notifier_invalidate_range_end(mm_walk.mm, |
| 2301 | migrate->start, |
| 2302 | migrate->end); |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2303 | |
| 2304 | migrate->end = migrate->start + (migrate->npages << PAGE_SHIFT); |
| 2305 | } |
| 2306 | |
| 2307 | /* |
| 2308 | * migrate_vma_check_page() - check if page is pinned or not |
| 2309 | * @page: struct page to check |
| 2310 | * |
| 2311 | * Pinned pages cannot be migrated. This is the same test as in |
| 2312 | * migrate_page_move_mapping(), except that here we allow migration of a |
| 2313 | * ZONE_DEVICE page. |
| 2314 | */ |
| 2315 | static bool migrate_vma_check_page(struct page *page) |
| 2316 | { |
| 2317 | /* |
| 2318 | * One extra ref because caller holds an extra reference, either from |
| 2319 | * isolate_lru_page() for a regular page, or migrate_vma_collect() for |
| 2320 | * a device page. |
| 2321 | */ |
| 2322 | int extra = 1; |
| 2323 | |
| 2324 | /* |
| 2325 | * FIXME support THP (transparent huge page), it is bit more complex to |
| 2326 | * check them than regular pages, because they can be mapped with a pmd |
| 2327 | * or with a pte (split pte mapping). |
| 2328 | */ |
| 2329 | if (PageCompound(page)) |
| 2330 | return false; |
| 2331 | |
| 2332 | if ((page_count(page) - extra) > page_mapcount(page)) |
| 2333 | return false; |
| 2334 | |
| 2335 | return true; |
| 2336 | } |
| 2337 | |
| 2338 | /* |
| 2339 | * migrate_vma_prepare() - lock pages and isolate them from the lru |
| 2340 | * @migrate: migrate struct containing all migration information |
| 2341 | * |
| 2342 | * This locks pages that have been collected by migrate_vma_collect(). Once each |
| 2343 | * page is locked it is isolated from the lru (for non-device pages). Finally, |
| 2344 | * the ref taken by migrate_vma_collect() is dropped, as locked pages cannot be |
| 2345 | * migrated by concurrent kernel threads. |
| 2346 | */ |
| 2347 | static void migrate_vma_prepare(struct migrate_vma *migrate) |
| 2348 | { |
| 2349 | const unsigned long npages = migrate->npages; |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame^] | 2350 | const unsigned long start = migrate->start; |
| 2351 | unsigned long addr, i, restore = 0; |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2352 | bool allow_drain = true; |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2353 | |
| 2354 | lru_add_drain(); |
| 2355 | |
| 2356 | for (i = 0; (i < npages) && migrate->cpages; i++) { |
| 2357 | struct page *page = migrate_pfn_to_page(migrate->src[i]); |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame^] | 2358 | bool remap = true; |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2359 | |
| 2360 | if (!page) |
| 2361 | continue; |
| 2362 | |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame^] | 2363 | if (!(migrate->src[i] & MIGRATE_PFN_LOCKED)) { |
| 2364 | /* |
| 2365 | * Because we are migrating several pages there can be |
| 2366 | * a deadlock between 2 concurrent migration where each |
| 2367 | * are waiting on each other page lock. |
| 2368 | * |
| 2369 | * Make migrate_vma() a best effort thing and backoff |
| 2370 | * for any page we can not lock right away. |
| 2371 | */ |
| 2372 | if (!trylock_page(page)) { |
| 2373 | migrate->src[i] = 0; |
| 2374 | migrate->cpages--; |
| 2375 | put_page(page); |
| 2376 | continue; |
| 2377 | } |
| 2378 | remap = false; |
| 2379 | migrate->src[i] |= MIGRATE_PFN_LOCKED; |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2380 | } |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2381 | |
| 2382 | if (!PageLRU(page) && allow_drain) { |
| 2383 | /* Drain CPU's pagevec */ |
| 2384 | lru_add_drain_all(); |
| 2385 | allow_drain = false; |
| 2386 | } |
| 2387 | |
| 2388 | if (isolate_lru_page(page)) { |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame^] | 2389 | if (remap) { |
| 2390 | migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; |
| 2391 | migrate->cpages--; |
| 2392 | restore++; |
| 2393 | } else { |
| 2394 | migrate->src[i] = 0; |
| 2395 | unlock_page(page); |
| 2396 | migrate->cpages--; |
| 2397 | put_page(page); |
| 2398 | } |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2399 | continue; |
| 2400 | } |
| 2401 | |
| 2402 | if (!migrate_vma_check_page(page)) { |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame^] | 2403 | if (remap) { |
| 2404 | migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; |
| 2405 | migrate->cpages--; |
| 2406 | restore++; |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2407 | |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame^] | 2408 | get_page(page); |
| 2409 | putback_lru_page(page); |
| 2410 | } else { |
| 2411 | migrate->src[i] = 0; |
| 2412 | unlock_page(page); |
| 2413 | migrate->cpages--; |
| 2414 | |
| 2415 | putback_lru_page(page); |
| 2416 | } |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2417 | } |
| 2418 | } |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame^] | 2419 | |
| 2420 | for (i = 0, addr = start; i < npages && restore; i++, addr += PAGE_SIZE) { |
| 2421 | struct page *page = migrate_pfn_to_page(migrate->src[i]); |
| 2422 | |
| 2423 | if (!page || (migrate->src[i] & MIGRATE_PFN_MIGRATE)) |
| 2424 | continue; |
| 2425 | |
| 2426 | remove_migration_pte(page, migrate->vma, addr, page); |
| 2427 | |
| 2428 | migrate->src[i] = 0; |
| 2429 | unlock_page(page); |
| 2430 | put_page(page); |
| 2431 | restore--; |
| 2432 | } |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2433 | } |
| 2434 | |
| 2435 | /* |
| 2436 | * migrate_vma_unmap() - replace page mapping with special migration pte entry |
| 2437 | * @migrate: migrate struct containing all migration information |
| 2438 | * |
| 2439 | * Replace page mapping (CPU page table pte) with a special migration pte entry |
| 2440 | * and check again if it has been pinned. Pinned pages are restored because we |
| 2441 | * cannot migrate them. |
| 2442 | * |
| 2443 | * This is the last step before we call the device driver callback to allocate |
| 2444 | * destination memory and copy contents of original page over to new page. |
| 2445 | */ |
| 2446 | static void migrate_vma_unmap(struct migrate_vma *migrate) |
| 2447 | { |
| 2448 | int flags = TTU_MIGRATION | TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS; |
| 2449 | const unsigned long npages = migrate->npages; |
| 2450 | const unsigned long start = migrate->start; |
| 2451 | unsigned long addr, i, restore = 0; |
| 2452 | |
| 2453 | for (i = 0; i < npages; i++) { |
| 2454 | struct page *page = migrate_pfn_to_page(migrate->src[i]); |
| 2455 | |
| 2456 | if (!page || !(migrate->src[i] & MIGRATE_PFN_MIGRATE)) |
| 2457 | continue; |
| 2458 | |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame^] | 2459 | if (page_mapped(page)) { |
| 2460 | try_to_unmap(page, flags); |
| 2461 | if (page_mapped(page)) |
| 2462 | goto restore; |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2463 | } |
Jérôme Glisse | 8c3328f | 2017-09-08 16:12:13 -0700 | [diff] [blame^] | 2464 | |
| 2465 | if (migrate_vma_check_page(page)) |
| 2466 | continue; |
| 2467 | |
| 2468 | restore: |
| 2469 | migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; |
| 2470 | migrate->cpages--; |
| 2471 | restore++; |
Jérôme Glisse | 8763cb4 | 2017-09-08 16:12:09 -0700 | [diff] [blame] | 2472 | } |
| 2473 | |
| 2474 | for (addr = start, i = 0; i < npages && restore; addr += PAGE_SIZE, i++) { |
| 2475 | struct page *page = migrate_pfn_to_page(migrate->src[i]); |
| 2476 | |
| 2477 | if (!page || (migrate->src[i] & MIGRATE_PFN_MIGRATE)) |
| 2478 | continue; |
| 2479 | |
| 2480 | remove_migration_ptes(page, page, false); |
| 2481 | |
| 2482 | migrate->src[i] = 0; |
| 2483 | unlock_page(page); |
| 2484 | restore--; |
| 2485 | |
| 2486 | putback_lru_page(page); |
| 2487 | } |
| 2488 | } |
| 2489 | |
| 2490 | /* |
| 2491 | * migrate_vma_pages() - migrate meta-data from src page to dst page |
| 2492 | * @migrate: migrate struct containing all migration information |
| 2493 | * |
| 2494 | * This migrates struct page meta-data from source struct page to destination |
| 2495 | * struct page. This effectively finishes the migration from source page to the |
| 2496 | * destination page. |
| 2497 | */ |
| 2498 | static void migrate_vma_pages(struct migrate_vma *migrate) |
| 2499 | { |
| 2500 | const unsigned long npages = migrate->npages; |
| 2501 | const unsigned long start = migrate->start; |
| 2502 | unsigned long addr, i; |
| 2503 | |
| 2504 | for (i = 0, addr = start; i < npages; addr += PAGE_SIZE, i++) { |
| 2505 | struct page *newpage = migrate_pfn_to_page(migrate->dst[i]); |
| 2506 | struct page *page = migrate_pfn_to_page(migrate->src[i]); |
| 2507 | struct address_space *mapping; |
| 2508 | int r; |
| 2509 | |
| 2510 | if (!page || !newpage) |
| 2511 | continue; |
| 2512 | if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE)) |
| 2513 | continue; |
| 2514 | |
| 2515 | mapping = page_mapping(page); |
| 2516 | |
| 2517 | r = migrate_page(mapping, newpage, page, MIGRATE_SYNC_NO_COPY); |
| 2518 | if (r != MIGRATEPAGE_SUCCESS) |
| 2519 | migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; |
| 2520 | } |
| 2521 | } |
| 2522 | |
| 2523 | /* |
| 2524 | * migrate_vma_finalize() - restore CPU page table entry |
| 2525 | * @migrate: migrate struct containing all migration information |
| 2526 | * |
| 2527 | * This replaces the special migration pte entry with either a mapping to the |
| 2528 | * new page if migration was successful for that page, or to the original page |
| 2529 | * otherwise. |
| 2530 | * |
| 2531 | * This also unlocks the pages and puts them back on the lru, or drops the extra |
| 2532 | * refcount, for device pages. |
| 2533 | */ |
| 2534 | static void migrate_vma_finalize(struct migrate_vma *migrate) |
| 2535 | { |
| 2536 | const unsigned long npages = migrate->npages; |
| 2537 | unsigned long i; |
| 2538 | |
| 2539 | for (i = 0; i < npages; i++) { |
| 2540 | struct page *newpage = migrate_pfn_to_page(migrate->dst[i]); |
| 2541 | struct page *page = migrate_pfn_to_page(migrate->src[i]); |
| 2542 | |
| 2543 | if (!page) |
| 2544 | continue; |
| 2545 | if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE) || !newpage) { |
| 2546 | if (newpage) { |
| 2547 | unlock_page(newpage); |
| 2548 | put_page(newpage); |
| 2549 | } |
| 2550 | newpage = page; |
| 2551 | } |
| 2552 | |
| 2553 | remove_migration_ptes(page, newpage, false); |
| 2554 | unlock_page(page); |
| 2555 | migrate->cpages--; |
| 2556 | |
| 2557 | putback_lru_page(page); |
| 2558 | |
| 2559 | if (newpage != page) { |
| 2560 | unlock_page(newpage); |
| 2561 | putback_lru_page(newpage); |
| 2562 | } |
| 2563 | } |
| 2564 | } |
| 2565 | |
| 2566 | /* |
| 2567 | * migrate_vma() - migrate a range of memory inside vma |
| 2568 | * |
| 2569 | * @ops: migration callback for allocating destination memory and copying |
| 2570 | * @vma: virtual memory area containing the range to be migrated |
| 2571 | * @start: start address of the range to migrate (inclusive) |
| 2572 | * @end: end address of the range to migrate (exclusive) |
| 2573 | * @src: array of hmm_pfn_t containing source pfns |
| 2574 | * @dst: array of hmm_pfn_t containing destination pfns |
| 2575 | * @private: pointer passed back to each of the callback |
| 2576 | * Returns: 0 on success, error code otherwise |
| 2577 | * |
| 2578 | * This function tries to migrate a range of memory virtual address range, using |
| 2579 | * callbacks to allocate and copy memory from source to destination. First it |
| 2580 | * collects all the pages backing each virtual address in the range, saving this |
| 2581 | * inside the src array. Then it locks those pages and unmaps them. Once the pages |
| 2582 | * are locked and unmapped, it checks whether each page is pinned or not. Pages |
| 2583 | * that aren't pinned have the MIGRATE_PFN_MIGRATE flag set (by this function) |
| 2584 | * in the corresponding src array entry. It then restores any pages that are |
| 2585 | * pinned, by remapping and unlocking those pages. |
| 2586 | * |
| 2587 | * At this point it calls the alloc_and_copy() callback. For documentation on |
| 2588 | * what is expected from that callback, see struct migrate_vma_ops comments in |
| 2589 | * include/linux/migrate.h |
| 2590 | * |
| 2591 | * After the alloc_and_copy() callback, this function goes over each entry in |
| 2592 | * the src array that has the MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag |
| 2593 | * set. If the corresponding entry in dst array has MIGRATE_PFN_VALID flag set, |
| 2594 | * then the function tries to migrate struct page information from the source |
| 2595 | * struct page to the destination struct page. If it fails to migrate the struct |
| 2596 | * page information, then it clears the MIGRATE_PFN_MIGRATE flag in the src |
| 2597 | * array. |
| 2598 | * |
| 2599 | * At this point all successfully migrated pages have an entry in the src |
| 2600 | * array with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag set and the dst |
| 2601 | * array entry with MIGRATE_PFN_VALID flag set. |
| 2602 | * |
| 2603 | * It then calls the finalize_and_map() callback. See comments for "struct |
| 2604 | * migrate_vma_ops", in include/linux/migrate.h for details about |
| 2605 | * finalize_and_map() behavior. |
| 2606 | * |
| 2607 | * After the finalize_and_map() callback, for successfully migrated pages, this |
| 2608 | * function updates the CPU page table to point to new pages, otherwise it |
| 2609 | * restores the CPU page table to point to the original source pages. |
| 2610 | * |
| 2611 | * Function returns 0 after the above steps, even if no pages were migrated |
| 2612 | * (The function only returns an error if any of the arguments are invalid.) |
| 2613 | * |
| 2614 | * Both src and dst array must be big enough for (end - start) >> PAGE_SHIFT |
| 2615 | * unsigned long entries. |
| 2616 | */ |
| 2617 | int migrate_vma(const struct migrate_vma_ops *ops, |
| 2618 | struct vm_area_struct *vma, |
| 2619 | unsigned long start, |
| 2620 | unsigned long end, |
| 2621 | unsigned long *src, |
| 2622 | unsigned long *dst, |
| 2623 | void *private) |
| 2624 | { |
| 2625 | struct migrate_vma migrate; |
| 2626 | |
| 2627 | /* Sanity check the arguments */ |
| 2628 | start &= PAGE_MASK; |
| 2629 | end &= PAGE_MASK; |
| 2630 | if (!vma || is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_SPECIAL)) |
| 2631 | return -EINVAL; |
| 2632 | if (start < vma->vm_start || start >= vma->vm_end) |
| 2633 | return -EINVAL; |
| 2634 | if (end <= vma->vm_start || end > vma->vm_end) |
| 2635 | return -EINVAL; |
| 2636 | if (!ops || !src || !dst || start >= end) |
| 2637 | return -EINVAL; |
| 2638 | |
| 2639 | memset(src, 0, sizeof(*src) * ((end - start) >> PAGE_SHIFT)); |
| 2640 | migrate.src = src; |
| 2641 | migrate.dst = dst; |
| 2642 | migrate.start = start; |
| 2643 | migrate.npages = 0; |
| 2644 | migrate.cpages = 0; |
| 2645 | migrate.end = end; |
| 2646 | migrate.vma = vma; |
| 2647 | |
| 2648 | /* Collect, and try to unmap source pages */ |
| 2649 | migrate_vma_collect(&migrate); |
| 2650 | if (!migrate.cpages) |
| 2651 | return 0; |
| 2652 | |
| 2653 | /* Lock and isolate page */ |
| 2654 | migrate_vma_prepare(&migrate); |
| 2655 | if (!migrate.cpages) |
| 2656 | return 0; |
| 2657 | |
| 2658 | /* Unmap pages */ |
| 2659 | migrate_vma_unmap(&migrate); |
| 2660 | if (!migrate.cpages) |
| 2661 | return 0; |
| 2662 | |
| 2663 | /* |
| 2664 | * At this point pages are locked and unmapped, and thus they have |
| 2665 | * stable content and can safely be copied to destination memory that |
| 2666 | * is allocated by the callback. |
| 2667 | * |
| 2668 | * Note that migration can fail in migrate_vma_struct_page() for each |
| 2669 | * individual page. |
| 2670 | */ |
| 2671 | ops->alloc_and_copy(vma, src, dst, start, end, private); |
| 2672 | |
| 2673 | /* This does the real migration of struct page */ |
| 2674 | migrate_vma_pages(&migrate); |
| 2675 | |
| 2676 | ops->finalize_and_map(vma, src, dst, start, end, private); |
| 2677 | |
| 2678 | /* Unlock and remap pages */ |
| 2679 | migrate_vma_finalize(&migrate); |
| 2680 | |
| 2681 | return 0; |
| 2682 | } |
| 2683 | EXPORT_SYMBOL(migrate_vma); |