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