Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Memory Migration functionality - linux/mm/migration.c |
| 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 | e468c87 | 2016-07-15 15:08:19 -0400 | [diff] [blame] | 33 | #include <linux/backing-dev.h> |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 34 | #include <linux/memcontrol.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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 37 | #include <linux/gfp.h> |
Liam Mark | cc2d4bd | 2013-01-16 10:14:40 -0800 | [diff] [blame] | 38 | #include <trace/events/kmem.h> |
Linus Torvalds | 4c49736 | 2017-08-20 13:26:27 -0700 | [diff] [blame] | 39 | #include <linux/ptrace.h> |
Michal Nazarewicz | 0d1836c | 2010-12-21 17:24:26 -0800 | [diff] [blame] | 40 | #include <asm/tlbflush.h> |
| 41 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 42 | #include "internal.h" |
| 43 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 44 | /* |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 45 | * 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] | 46 | * to be migrated using isolate_lru_page(). If scheduling work on other CPUs is |
| 47 | * undesirable, use migrate_prep_local() |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 48 | */ |
| 49 | int migrate_prep(void) |
| 50 | { |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 51 | /* |
| 52 | * Clear the LRU lists so pages can be isolated. |
| 53 | * Note that pages may be moved off the LRU after we have |
| 54 | * drained them. Those pages will fail to migrate like other |
| 55 | * pages that may be busy. |
| 56 | */ |
| 57 | lru_add_drain_all(); |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 62 | /* Do the necessary work of migrate_prep but not if it involves other CPUs */ |
| 63 | int migrate_prep_local(void) |
| 64 | { |
| 65 | lru_add_drain(); |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 70 | /* |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 71 | * Add isolated pages on the list back to the LRU under page lock |
| 72 | * to avoid leaking evictable pages back onto unevictable list. |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 73 | */ |
Minchan Kim | e13861d | 2010-05-24 14:31:59 -0700 | [diff] [blame] | 74 | void putback_lru_pages(struct list_head *l) |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 75 | { |
| 76 | struct page *page; |
| 77 | struct page *page2; |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 78 | |
| 79 | list_for_each_entry_safe(page, page2, l, lru) { |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 80 | list_del(&page->lru); |
KOSAKI Motohiro | a731286 | 2009-09-21 17:01:37 -0700 | [diff] [blame] | 81 | dec_zone_page_state(page, NR_ISOLATED_ANON + |
Johannes Weiner | 6c0b135 | 2009-09-21 17:02:59 -0700 | [diff] [blame] | 82 | page_is_file_cache(page)); |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 83 | putback_lru_page(page); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 84 | } |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 87 | /* |
| 88 | * Restore a potential migration pte to a working pte entry |
| 89 | */ |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 90 | static int remove_migration_pte(struct page *new, struct vm_area_struct *vma, |
| 91 | unsigned long addr, void *old) |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 92 | { |
| 93 | struct mm_struct *mm = vma->vm_mm; |
| 94 | swp_entry_t entry; |
| 95 | pgd_t *pgd; |
| 96 | pud_t *pud; |
| 97 | pmd_t *pmd; |
| 98 | pte_t *ptep, pte; |
| 99 | spinlock_t *ptl; |
| 100 | |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 101 | if (unlikely(PageHuge(new))) { |
| 102 | ptep = huge_pte_offset(mm, addr); |
| 103 | if (!ptep) |
| 104 | goto out; |
| 105 | ptl = &mm->page_table_lock; |
| 106 | } else { |
| 107 | pgd = pgd_offset(mm, addr); |
| 108 | if (!pgd_present(*pgd)) |
| 109 | goto out; |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 110 | |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 111 | pud = pud_offset(pgd, addr); |
| 112 | if (!pud_present(*pud)) |
| 113 | goto out; |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 114 | |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 115 | pmd = pmd_offset(pud, addr); |
Andrea Arcangeli | 500d65d | 2011-01-13 15:46:55 -0800 | [diff] [blame] | 116 | if (pmd_trans_huge(*pmd)) |
| 117 | goto out; |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 118 | if (!pmd_present(*pmd)) |
| 119 | goto out; |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 120 | |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 121 | ptep = pte_offset_map(pmd, addr); |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 122 | |
Hugh Dickins | 486cf46 | 2011-10-19 12:50:35 -0700 | [diff] [blame] | 123 | /* |
| 124 | * Peek to check is_swap_pte() before taking ptlock? No, we |
| 125 | * can race mremap's move_ptes(), which skips anon_vma lock. |
| 126 | */ |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 127 | |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 128 | ptl = pte_lockptr(mm, pmd); |
| 129 | } |
| 130 | |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 131 | spin_lock(ptl); |
| 132 | pte = *ptep; |
| 133 | if (!is_swap_pte(pte)) |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 134 | goto unlock; |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 135 | |
| 136 | entry = pte_to_swp_entry(pte); |
| 137 | |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 138 | if (!is_migration_entry(entry) || |
| 139 | migration_entry_to_page(entry) != old) |
| 140 | goto unlock; |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 141 | |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 142 | get_page(new); |
| 143 | pte = pte_mkold(mk_pte(new, vma->vm_page_prot)); |
| 144 | if (is_write_migration_entry(entry)) |
| 145 | pte = pte_mkwrite(pte); |
Andi Kleen | 3ef8fd7 | 2010-10-11 16:03:21 +0200 | [diff] [blame] | 146 | #ifdef CONFIG_HUGETLB_PAGE |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 147 | if (PageHuge(new)) |
| 148 | pte = pte_mkhuge(pte); |
Andi Kleen | 3ef8fd7 | 2010-10-11 16:03:21 +0200 | [diff] [blame] | 149 | #endif |
Leonid Yegoshin | a0ca517 | 2013-05-24 15:55:18 -0700 | [diff] [blame] | 150 | flush_dcache_page(new); |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 151 | set_pte_at(mm, addr, ptep, pte); |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 152 | |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 153 | if (PageHuge(new)) { |
| 154 | if (PageAnon(new)) |
| 155 | hugepage_add_anon_rmap(new, vma, addr); |
| 156 | else |
| 157 | page_dup_rmap(new); |
| 158 | } else if (PageAnon(new)) |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 159 | page_add_anon_rmap(new, vma, addr); |
| 160 | else |
| 161 | page_add_file_rmap(new); |
| 162 | |
| 163 | /* No need to invalidate - it was non-present before */ |
Russell King | 4b3073e | 2009-12-18 16:40:18 +0000 | [diff] [blame] | 164 | update_mmu_cache(vma, addr, ptep); |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 165 | unlock: |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 166 | pte_unmap_unlock(ptep, ptl); |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 167 | out: |
| 168 | return SWAP_AGAIN; |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /* |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 172 | * Get rid of all migration entries and replace them by |
| 173 | * references to the indicated page. |
| 174 | */ |
| 175 | static void remove_migration_ptes(struct page *old, struct page *new) |
| 176 | { |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 177 | rmap_walk(new, remove_migration_pte, old); |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /* |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 181 | * Something used the pte of a page under migration. We need to |
| 182 | * get to the page and wait until migration is finished. |
| 183 | * When we return from this function the fault will be retried. |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 184 | */ |
| 185 | void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd, |
| 186 | unsigned long address) |
| 187 | { |
| 188 | pte_t *ptep, pte; |
| 189 | spinlock_t *ptl; |
| 190 | swp_entry_t entry; |
| 191 | struct page *page; |
| 192 | |
| 193 | ptep = pte_offset_map_lock(mm, pmd, address, &ptl); |
| 194 | pte = *ptep; |
| 195 | if (!is_swap_pte(pte)) |
| 196 | goto out; |
| 197 | |
| 198 | entry = pte_to_swp_entry(pte); |
| 199 | if (!is_migration_entry(entry)) |
| 200 | goto out; |
| 201 | |
| 202 | page = migration_entry_to_page(entry); |
| 203 | |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 204 | /* |
| 205 | * Once radix-tree replacement of page migration started, page_count |
| 206 | * *must* be zero. And, we don't want to call wait_on_page_locked() |
| 207 | * against a page without get_page(). |
| 208 | * So, we use get_page_unless_zero(), here. Even failed, page fault |
| 209 | * will occur again. |
| 210 | */ |
| 211 | if (!get_page_unless_zero(page)) |
| 212 | goto out; |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 213 | pte_unmap_unlock(ptep, ptl); |
| 214 | wait_on_page_locked(page); |
| 215 | put_page(page); |
| 216 | return; |
| 217 | out: |
| 218 | pte_unmap_unlock(ptep, ptl); |
| 219 | } |
| 220 | |
Mel Gorman | b969c4a | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 221 | #ifdef CONFIG_BLOCK |
| 222 | /* Returns true if all buffers are successfully locked */ |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 223 | static bool buffer_migrate_lock_buffers(struct buffer_head *head, |
| 224 | enum migrate_mode mode) |
Mel Gorman | b969c4a | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 225 | { |
| 226 | struct buffer_head *bh = head; |
| 227 | |
| 228 | /* Simple case, sync compaction */ |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 229 | if (mode != MIGRATE_ASYNC) { |
Mel Gorman | b969c4a | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 230 | do { |
| 231 | get_bh(bh); |
| 232 | lock_buffer(bh); |
| 233 | bh = bh->b_this_page; |
| 234 | |
| 235 | } while (bh != head); |
| 236 | |
| 237 | return true; |
| 238 | } |
| 239 | |
| 240 | /* async case, we cannot block on lock_buffer so use trylock_buffer */ |
| 241 | do { |
| 242 | get_bh(bh); |
| 243 | if (!trylock_buffer(bh)) { |
| 244 | /* |
| 245 | * We failed to lock the buffer and cannot stall in |
| 246 | * async migration. Release the taken locks |
| 247 | */ |
| 248 | struct buffer_head *failed_bh = bh; |
| 249 | put_bh(failed_bh); |
| 250 | bh = head; |
| 251 | while (bh != failed_bh) { |
| 252 | unlock_buffer(bh); |
| 253 | put_bh(bh); |
| 254 | bh = bh->b_this_page; |
| 255 | } |
| 256 | return false; |
| 257 | } |
| 258 | |
| 259 | bh = bh->b_this_page; |
| 260 | } while (bh != head); |
| 261 | return true; |
| 262 | } |
| 263 | #else |
| 264 | static inline bool buffer_migrate_lock_buffers(struct buffer_head *head, |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 265 | enum migrate_mode mode) |
Mel Gorman | b969c4a | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 266 | { |
| 267 | return true; |
| 268 | } |
| 269 | #endif /* CONFIG_BLOCK */ |
| 270 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 271 | /* |
Christoph Lameter | c3fcf8a | 2006-06-23 02:03:32 -0700 | [diff] [blame] | 272 | * Replace the page in the mapping. |
Christoph Lameter | 5b5c712 | 2006-06-23 02:03:29 -0700 | [diff] [blame] | 273 | * |
| 274 | * The number of remaining references must be: |
| 275 | * 1 for anonymous pages without a mapping |
| 276 | * 2 for pages with a mapping |
David Howells | 266cf65 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 277 | * 3 for pages with a mapping and PagePrivate/PagePrivate2 set. |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 278 | */ |
Christoph Lameter | 2d1db3b | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 279 | static int migrate_page_move_mapping(struct address_space *mapping, |
Mel Gorman | b969c4a | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 280 | struct page *newpage, struct page *page, |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 281 | struct buffer_head *head, enum migrate_mode mode) |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 282 | { |
Hugh Dickins | e468c87 | 2016-07-15 15:08:19 -0400 | [diff] [blame] | 283 | struct zone *oldzone, *newzone; |
| 284 | int dirty; |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 285 | int expected_count; |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 286 | void **pslot; |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 287 | |
Christoph Lameter | 6c5240a | 2006-06-23 02:03:37 -0700 | [diff] [blame] | 288 | if (!mapping) { |
Christoph Lameter | 0e8c7d0 | 2007-04-23 14:41:09 -0700 | [diff] [blame] | 289 | /* Anonymous page without mapping */ |
Christoph Lameter | 6c5240a | 2006-06-23 02:03:37 -0700 | [diff] [blame] | 290 | if (page_count(page) != 1) |
| 291 | return -EAGAIN; |
| 292 | return 0; |
| 293 | } |
| 294 | |
Hugh Dickins | e468c87 | 2016-07-15 15:08:19 -0400 | [diff] [blame] | 295 | oldzone = page_zone(page); |
| 296 | newzone = page_zone(newpage); |
| 297 | |
Nick Piggin | 19fd623 | 2008-07-25 19:45:32 -0700 | [diff] [blame] | 298 | spin_lock_irq(&mapping->tree_lock); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 299 | |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 300 | pslot = radix_tree_lookup_slot(&mapping->page_tree, |
| 301 | page_index(page)); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 302 | |
Johannes Weiner | edcf474 | 2009-09-21 17:02:59 -0700 | [diff] [blame] | 303 | expected_count = 2 + page_has_private(page); |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 304 | if (page_count(page) != expected_count || |
Mel Gorman | 29c1f67 | 2011-01-13 15:47:21 -0800 | [diff] [blame] | 305 | radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) { |
Nick Piggin | 19fd623 | 2008-07-25 19:45:32 -0700 | [diff] [blame] | 306 | spin_unlock_irq(&mapping->tree_lock); |
Christoph Lameter | e23ca00 | 2006-04-10 22:52:57 -0700 | [diff] [blame] | 307 | return -EAGAIN; |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 308 | } |
| 309 | |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 310 | if (!page_freeze_refs(page, expected_count)) { |
Nick Piggin | 19fd623 | 2008-07-25 19:45:32 -0700 | [diff] [blame] | 311 | spin_unlock_irq(&mapping->tree_lock); |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 312 | return -EAGAIN; |
| 313 | } |
| 314 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 315 | /* |
Mel Gorman | b969c4a | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 316 | * In the async migration case of moving a page with buffers, lock the |
| 317 | * buffers using trylock before the mapping is moved. If the mapping |
| 318 | * was moved, we later failed to lock the buffers and could not move |
| 319 | * the mapping back due to an elevated page count, we would have to |
| 320 | * block waiting on other references to be dropped. |
| 321 | */ |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 322 | if (mode == MIGRATE_ASYNC && head && |
| 323 | !buffer_migrate_lock_buffers(head, mode)) { |
Mel Gorman | b969c4a | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 324 | page_unfreeze_refs(page, expected_count); |
| 325 | spin_unlock_irq(&mapping->tree_lock); |
| 326 | return -EAGAIN; |
| 327 | } |
| 328 | |
| 329 | /* |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 330 | * Now we know that no one else is looking at the page. |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 331 | */ |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 332 | get_page(newpage); /* add cache reference */ |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 333 | if (PageSwapCache(page)) { |
| 334 | SetPageSwapCache(newpage); |
| 335 | set_page_private(newpage, page_private(page)); |
| 336 | } |
| 337 | |
Hugh Dickins | e468c87 | 2016-07-15 15:08:19 -0400 | [diff] [blame] | 338 | /* Move dirty while page refs frozen and newpage not yet exposed */ |
| 339 | dirty = PageDirty(page); |
| 340 | if (dirty) { |
| 341 | ClearPageDirty(page); |
| 342 | SetPageDirty(newpage); |
| 343 | } |
| 344 | |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 345 | radix_tree_replace_slot(pslot, newpage); |
| 346 | |
| 347 | /* |
Jacobo Giralt | 937a94c | 2012-01-10 15:07:11 -0800 | [diff] [blame] | 348 | * Drop cache reference from old page by unfreezing |
| 349 | * to one less reference. |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 350 | * We know this isn't the last reference. |
| 351 | */ |
Jacobo Giralt | 937a94c | 2012-01-10 15:07:11 -0800 | [diff] [blame] | 352 | page_unfreeze_refs(page, expected_count - 1); |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 353 | |
Hugh Dickins | e468c87 | 2016-07-15 15:08:19 -0400 | [diff] [blame] | 354 | spin_unlock(&mapping->tree_lock); |
| 355 | /* Leave irq disabled to prevent preemption while updating stats */ |
| 356 | |
Christoph Lameter | 0e8c7d0 | 2007-04-23 14:41:09 -0700 | [diff] [blame] | 357 | /* |
| 358 | * If moved to a different zone then also account |
| 359 | * the page for that zone. Other VM counters will be |
| 360 | * taken care of when we establish references to the |
| 361 | * new page and drop references to the old page. |
| 362 | * |
| 363 | * Note that anonymous pages are accounted for |
| 364 | * via NR_FILE_PAGES and NR_ANON_PAGES if they |
| 365 | * are mapped to swap space. |
| 366 | */ |
Hugh Dickins | e468c87 | 2016-07-15 15:08:19 -0400 | [diff] [blame] | 367 | if (newzone != oldzone) { |
| 368 | __dec_zone_state(oldzone, NR_FILE_PAGES); |
| 369 | __inc_zone_state(newzone, NR_FILE_PAGES); |
| 370 | if (PageSwapBacked(page) && !PageSwapCache(page)) { |
| 371 | __dec_zone_state(oldzone, NR_SHMEM); |
| 372 | __inc_zone_state(newzone, NR_SHMEM); |
| 373 | } |
| 374 | if (dirty && mapping_cap_account_dirty(mapping)) { |
| 375 | __dec_zone_state(oldzone, NR_FILE_DIRTY); |
| 376 | __inc_zone_state(newzone, NR_FILE_DIRTY); |
| 377 | } |
KOSAKI Motohiro | 4b02108 | 2009-09-21 17:01:33 -0700 | [diff] [blame] | 378 | } |
Hugh Dickins | e468c87 | 2016-07-15 15:08:19 -0400 | [diff] [blame] | 379 | local_irq_enable(); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 380 | |
| 381 | return 0; |
| 382 | } |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 383 | |
| 384 | /* |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 385 | * The expected number of remaining references is the same as that |
| 386 | * of migrate_page_move_mapping(). |
| 387 | */ |
| 388 | int migrate_huge_page_move_mapping(struct address_space *mapping, |
| 389 | struct page *newpage, struct page *page) |
| 390 | { |
| 391 | int expected_count; |
| 392 | void **pslot; |
| 393 | |
| 394 | if (!mapping) { |
| 395 | if (page_count(page) != 1) |
| 396 | return -EAGAIN; |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | spin_lock_irq(&mapping->tree_lock); |
| 401 | |
| 402 | pslot = radix_tree_lookup_slot(&mapping->page_tree, |
| 403 | page_index(page)); |
| 404 | |
| 405 | expected_count = 2 + page_has_private(page); |
| 406 | if (page_count(page) != expected_count || |
Mel Gorman | 29c1f67 | 2011-01-13 15:47:21 -0800 | [diff] [blame] | 407 | radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) { |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 408 | spin_unlock_irq(&mapping->tree_lock); |
| 409 | return -EAGAIN; |
| 410 | } |
| 411 | |
| 412 | if (!page_freeze_refs(page, expected_count)) { |
| 413 | spin_unlock_irq(&mapping->tree_lock); |
| 414 | return -EAGAIN; |
| 415 | } |
| 416 | |
| 417 | get_page(newpage); |
| 418 | |
| 419 | radix_tree_replace_slot(pslot, newpage); |
| 420 | |
Jacobo Giralt | 937a94c | 2012-01-10 15:07:11 -0800 | [diff] [blame] | 421 | page_unfreeze_refs(page, expected_count - 1); |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 422 | |
| 423 | spin_unlock_irq(&mapping->tree_lock); |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | /* |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 428 | * Copy the page to its new location |
| 429 | */ |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 430 | void migrate_page_copy(struct page *newpage, struct page *page) |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 431 | { |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 432 | if (PageHuge(page)) |
| 433 | copy_huge_page(newpage, page); |
| 434 | else |
| 435 | copy_highpage(newpage, page); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 436 | |
| 437 | if (PageError(page)) |
| 438 | SetPageError(newpage); |
| 439 | if (PageReferenced(page)) |
| 440 | SetPageReferenced(newpage); |
| 441 | if (PageUptodate(page)) |
| 442 | SetPageUptodate(newpage); |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 443 | if (TestClearPageActive(page)) { |
| 444 | VM_BUG_ON(PageUnevictable(page)); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 445 | SetPageActive(newpage); |
Lee Schermerhorn | 418b27e | 2009-12-14 17:59:54 -0800 | [diff] [blame] | 446 | } else if (TestClearPageUnevictable(page)) |
| 447 | SetPageUnevictable(newpage); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 448 | if (PageChecked(page)) |
| 449 | SetPageChecked(newpage); |
| 450 | if (PageMappedToDisk(page)) |
| 451 | SetPageMappedToDisk(newpage); |
| 452 | |
Hugh Dickins | e468c87 | 2016-07-15 15:08:19 -0400 | [diff] [blame] | 453 | /* Move dirty on pages not done by migrate_page_move_mapping() */ |
| 454 | if (PageDirty(page)) |
| 455 | SetPageDirty(newpage); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 456 | |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 457 | mlock_migrate_page(newpage, page); |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 458 | ksm_migrate_page(newpage, page); |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 459 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 460 | ClearPageSwapCache(page); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 461 | ClearPagePrivate(page); |
| 462 | set_page_private(page, 0); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 463 | |
| 464 | /* |
| 465 | * If any waiters have accumulated on the new page then |
| 466 | * wake them up. |
| 467 | */ |
| 468 | if (PageWriteback(newpage)) |
| 469 | end_page_writeback(newpage); |
| 470 | } |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 471 | |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 472 | /************************************************************ |
| 473 | * Migration functions |
| 474 | ***********************************************************/ |
| 475 | |
| 476 | /* Always fail migration. Used for mappings that are not movable */ |
Christoph Lameter | 2d1db3b | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 477 | int fail_migrate_page(struct address_space *mapping, |
| 478 | struct page *newpage, struct page *page) |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 479 | { |
| 480 | return -EIO; |
| 481 | } |
| 482 | EXPORT_SYMBOL(fail_migrate_page); |
| 483 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 484 | /* |
| 485 | * Common logic to directly migrate a single page suitable for |
David Howells | 266cf65 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 486 | * pages that do not use PagePrivate/PagePrivate2. |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 487 | * |
| 488 | * Pages are locked upon entry and exit. |
| 489 | */ |
Christoph Lameter | 2d1db3b | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 490 | int migrate_page(struct address_space *mapping, |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 491 | struct page *newpage, struct page *page, |
| 492 | enum migrate_mode mode) |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 493 | { |
| 494 | int rc; |
| 495 | |
| 496 | BUG_ON(PageWriteback(page)); /* Writeback must be complete */ |
| 497 | |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 498 | rc = migrate_page_move_mapping(mapping, newpage, page, NULL, mode); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 499 | |
| 500 | if (rc) |
| 501 | return rc; |
| 502 | |
| 503 | migrate_page_copy(newpage, page); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 504 | return 0; |
| 505 | } |
| 506 | EXPORT_SYMBOL(migrate_page); |
| 507 | |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 508 | #ifdef CONFIG_BLOCK |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 509 | /* |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 510 | * Migration function for pages with buffers. This function can only be used |
| 511 | * if the underlying filesystem guarantees that no other references to "page" |
| 512 | * exist. |
| 513 | */ |
Christoph Lameter | 2d1db3b | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 514 | int buffer_migrate_page(struct address_space *mapping, |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 515 | struct page *newpage, struct page *page, enum migrate_mode mode) |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 516 | { |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 517 | struct buffer_head *bh, *head; |
| 518 | int rc; |
| 519 | |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 520 | if (!page_has_buffers(page)) |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 521 | return migrate_page(mapping, newpage, page, mode); |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 522 | |
| 523 | head = page_buffers(page); |
| 524 | |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 525 | rc = migrate_page_move_mapping(mapping, newpage, page, head, mode); |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 526 | |
| 527 | if (rc) |
| 528 | return rc; |
| 529 | |
Mel Gorman | b969c4a | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 530 | /* |
| 531 | * In the async case, migrate_page_move_mapping locked the buffers |
| 532 | * with an IRQ-safe spinlock held. In the sync case, the buffers |
| 533 | * need to be locked now |
| 534 | */ |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 535 | if (mode != MIGRATE_ASYNC) |
| 536 | BUG_ON(!buffer_migrate_lock_buffers(head, mode)); |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 537 | |
| 538 | ClearPagePrivate(page); |
| 539 | set_page_private(newpage, page_private(page)); |
| 540 | set_page_private(page, 0); |
| 541 | put_page(page); |
| 542 | get_page(newpage); |
| 543 | |
| 544 | bh = head; |
| 545 | do { |
| 546 | set_bh_page(bh, newpage, bh_offset(bh)); |
| 547 | bh = bh->b_this_page; |
| 548 | |
| 549 | } while (bh != head); |
| 550 | |
| 551 | SetPagePrivate(newpage); |
| 552 | |
| 553 | migrate_page_copy(newpage, page); |
| 554 | |
| 555 | bh = head; |
| 556 | do { |
| 557 | unlock_buffer(bh); |
| 558 | put_bh(bh); |
| 559 | bh = bh->b_this_page; |
| 560 | |
| 561 | } while (bh != head); |
| 562 | |
| 563 | return 0; |
| 564 | } |
| 565 | EXPORT_SYMBOL(buffer_migrate_page); |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 566 | #endif |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 567 | |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 568 | /* |
| 569 | * Writeback a page to clean the dirty state |
| 570 | */ |
| 571 | static int writeout(struct address_space *mapping, struct page *page) |
| 572 | { |
| 573 | struct writeback_control wbc = { |
| 574 | .sync_mode = WB_SYNC_NONE, |
| 575 | .nr_to_write = 1, |
| 576 | .range_start = 0, |
| 577 | .range_end = LLONG_MAX, |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 578 | .for_reclaim = 1 |
| 579 | }; |
| 580 | int rc; |
| 581 | |
| 582 | if (!mapping->a_ops->writepage) |
| 583 | /* No write method for the address space */ |
| 584 | return -EINVAL; |
| 585 | |
| 586 | if (!clear_page_dirty_for_io(page)) |
| 587 | /* Someone else already triggered a write */ |
| 588 | return -EAGAIN; |
| 589 | |
| 590 | /* |
| 591 | * A dirty page may imply that the underlying filesystem has |
| 592 | * the page on some queue. So the page must be clean for |
| 593 | * migration. Writeout may mean we loose the lock and the |
| 594 | * page state is no longer what we checked for earlier. |
| 595 | * At this point we know that the migration attempt cannot |
| 596 | * be successful. |
| 597 | */ |
| 598 | remove_migration_ptes(page, page); |
| 599 | |
| 600 | rc = mapping->a_ops->writepage(page, &wbc); |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 601 | |
| 602 | if (rc != AOP_WRITEPAGE_ACTIVATE) |
| 603 | /* unlocked. Relock */ |
| 604 | lock_page(page); |
| 605 | |
Hugh Dickins | bda8550 | 2008-11-19 15:36:36 -0800 | [diff] [blame] | 606 | return (rc < 0) ? -EIO : -EAGAIN; |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | /* |
| 610 | * Default handling if a filesystem does not provide a migration function. |
| 611 | */ |
Christoph Lameter | 8351a6e | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 612 | static int fallback_migrate_page(struct address_space *mapping, |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 613 | struct page *newpage, struct page *page, enum migrate_mode mode) |
Christoph Lameter | 8351a6e | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 614 | { |
Mel Gorman | b969c4a | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 615 | if (PageDirty(page)) { |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 616 | /* Only writeback pages in full synchronous migration */ |
| 617 | if (mode != MIGRATE_SYNC) |
Mel Gorman | b969c4a | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 618 | return -EBUSY; |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 619 | return writeout(mapping, page); |
Mel Gorman | b969c4a | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 620 | } |
Christoph Lameter | 8351a6e | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 621 | |
| 622 | /* |
| 623 | * Buffers may be managed in a filesystem specific way. |
| 624 | * We must have no buffers or drop them. |
| 625 | */ |
David Howells | 266cf65 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 626 | if (page_has_private(page) && |
Christoph Lameter | 8351a6e | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 627 | !try_to_release_page(page, GFP_KERNEL)) |
| 628 | return -EAGAIN; |
| 629 | |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 630 | return migrate_page(mapping, newpage, page, mode); |
Christoph Lameter | 8351a6e | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 631 | } |
| 632 | |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 633 | /* |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 634 | * Move a page to a newly allocated page |
| 635 | * The page is locked and all ptes have been successfully removed. |
| 636 | * |
| 637 | * The new page will have replaced the old page if this function |
| 638 | * is successful. |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 639 | * |
| 640 | * Return value: |
| 641 | * < 0 - error code |
| 642 | * == 0 - success |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 643 | */ |
Mel Gorman | 3fe2011 | 2010-05-24 14:32:20 -0700 | [diff] [blame] | 644 | static int move_to_new_page(struct page *newpage, struct page *page, |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 645 | int remap_swapcache, enum migrate_mode mode) |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 646 | { |
| 647 | struct address_space *mapping; |
| 648 | int rc; |
| 649 | |
| 650 | /* |
| 651 | * Block others from accessing the page when we get around to |
| 652 | * establishing additional references. We are the only one |
| 653 | * holding a reference to the new page at this point. |
| 654 | */ |
Nick Piggin | 529ae9a | 2008-08-02 12:01:03 +0200 | [diff] [blame] | 655 | if (!trylock_page(newpage)) |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 656 | BUG(); |
| 657 | |
| 658 | /* Prepare mapping for the new page.*/ |
| 659 | newpage->index = page->index; |
| 660 | newpage->mapping = page->mapping; |
Rik van Riel | b2e1853 | 2008-10-18 20:26:30 -0700 | [diff] [blame] | 661 | if (PageSwapBacked(page)) |
| 662 | SetPageSwapBacked(newpage); |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 663 | |
| 664 | mapping = page_mapping(page); |
| 665 | if (!mapping) |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 666 | rc = migrate_page(mapping, newpage, page, mode); |
Mel Gorman | b969c4a | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 667 | else if (mapping->a_ops->migratepage) |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 668 | /* |
Mel Gorman | b969c4a | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 669 | * Most pages have a mapping and most filesystems provide a |
| 670 | * migratepage callback. Anonymous pages are part of swap |
| 671 | * space which also has its own migratepage callback. This |
| 672 | * is the most common path for page migration. |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 673 | */ |
Mel Gorman | b969c4a | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 674 | rc = mapping->a_ops->migratepage(mapping, |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 675 | newpage, page, mode); |
Mel Gorman | b969c4a | 2012-01-12 17:19:34 -0800 | [diff] [blame] | 676 | else |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 677 | rc = fallback_migrate_page(mapping, newpage, page, mode); |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 678 | |
Mel Gorman | 3fe2011 | 2010-05-24 14:32:20 -0700 | [diff] [blame] | 679 | if (rc) { |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 680 | newpage->mapping = NULL; |
Mel Gorman | 3fe2011 | 2010-05-24 14:32:20 -0700 | [diff] [blame] | 681 | } else { |
| 682 | if (remap_swapcache) |
| 683 | remove_migration_ptes(page, newpage); |
Konstantin Khlebnikov | 35512ec | 2012-02-03 15:37:13 -0800 | [diff] [blame] | 684 | page->mapping = NULL; |
Mel Gorman | 3fe2011 | 2010-05-24 14:32:20 -0700 | [diff] [blame] | 685 | } |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 686 | |
| 687 | unlock_page(newpage); |
| 688 | |
| 689 | return rc; |
| 690 | } |
| 691 | |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 692 | static int __unmap_and_move(struct page *page, struct page *newpage, |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 693 | int force, bool offlining, enum migrate_mode mode) |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 694 | { |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 695 | int rc = -EAGAIN; |
Mel Gorman | 3fe2011 | 2010-05-24 14:32:20 -0700 | [diff] [blame] | 696 | int remap_swapcache = 1; |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 697 | int charge = 0; |
KAMEZAWA Hiroyuki | 56039ef | 2011-03-23 16:42:19 -0700 | [diff] [blame] | 698 | struct mem_cgroup *mem; |
Mel Gorman | 3f6c827 | 2010-05-24 14:32:17 -0700 | [diff] [blame] | 699 | struct anon_vma *anon_vma = NULL; |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 700 | |
Nick Piggin | 529ae9a | 2008-08-02 12:01:03 +0200 | [diff] [blame] | 701 | if (!trylock_page(page)) { |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 702 | if (!force || mode == MIGRATE_ASYNC) |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 703 | goto out; |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 704 | |
| 705 | /* |
| 706 | * It's not safe for direct compaction to call lock_page. |
| 707 | * For example, during page readahead pages are added locked |
| 708 | * to the LRU. Later, when the IO completes the pages are |
| 709 | * marked uptodate and unlocked. However, the queueing |
| 710 | * could be merging multiple pages for one bio (e.g. |
| 711 | * mpage_readpages). If an allocation happens for the |
| 712 | * second or third page, the process can end up locking |
| 713 | * the same page twice and deadlocking. Rather than |
| 714 | * trying to be clever about what pages can be locked, |
| 715 | * avoid the use of lock_page for direct compaction |
| 716 | * altogether. |
| 717 | */ |
| 718 | if (current->flags & PF_MEMALLOC) |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 719 | goto out; |
Mel Gorman | 3e7d344 | 2011-01-13 15:45:56 -0800 | [diff] [blame] | 720 | |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 721 | lock_page(page); |
| 722 | } |
| 723 | |
Hugh Dickins | 62b61f6 | 2009-12-14 17:59:33 -0800 | [diff] [blame] | 724 | /* |
| 725 | * Only memory hotplug's offline_pages() caller has locked out KSM, |
| 726 | * and can safely migrate a KSM page. The other cases have skipped |
| 727 | * PageKsm along with PageReserved - but it is only now when we have |
| 728 | * the page lock that we can be certain it will not go KSM beneath us |
| 729 | * (KSM will not upgrade a page from PageAnon to PageKsm when it sees |
| 730 | * its pagecount raised, but only here do we take the page lock which |
| 731 | * serializes that). |
| 732 | */ |
| 733 | if (PageKsm(page) && !offlining) { |
| 734 | rc = -EBUSY; |
| 735 | goto unlock; |
| 736 | } |
| 737 | |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 738 | /* charge against new page */ |
Miklos Szeredi | ef6a3c6 | 2011-03-22 16:30:52 -0700 | [diff] [blame] | 739 | charge = mem_cgroup_prepare_migration(page, newpage, &mem, GFP_KERNEL); |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 740 | if (charge == -ENOMEM) { |
| 741 | rc = -ENOMEM; |
| 742 | goto unlock; |
| 743 | } |
| 744 | BUG_ON(charge); |
| 745 | |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 746 | if (PageWriteback(page)) { |
Andrea Arcangeli | 11bc82d | 2011-03-22 16:33:11 -0700 | [diff] [blame] | 747 | /* |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 748 | * Only in the case of a full syncronous migration is it |
| 749 | * necessary to wait for PageWriteback. In the async case, |
| 750 | * the retry loop is too short and in the sync-light case, |
| 751 | * the overhead of stalling is too much |
Andrea Arcangeli | 11bc82d | 2011-03-22 16:33:11 -0700 | [diff] [blame] | 752 | */ |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 753 | if (mode != MIGRATE_SYNC) { |
Andrea Arcangeli | 11bc82d | 2011-03-22 16:33:11 -0700 | [diff] [blame] | 754 | rc = -EBUSY; |
| 755 | goto uncharge; |
| 756 | } |
| 757 | if (!force) |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 758 | goto uncharge; |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 759 | wait_on_page_writeback(page); |
| 760 | } |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 761 | /* |
KAMEZAWA Hiroyuki | dc386d4 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 762 | * By try_to_unmap(), page->mapcount goes down to 0 here. In this case, |
| 763 | * 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] | 764 | * This get_anon_vma() delays freeing anon_vma pointer until the end |
KAMEZAWA Hiroyuki | dc386d4 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 765 | * of migration. File cache pages are no problem because of page_lock() |
KAMEZAWA Hiroyuki | 989f89c | 2007-08-30 23:56:21 -0700 | [diff] [blame] | 766 | * File Caches may use write_page() or lock_page() in migration, then, |
| 767 | * just care Anon page here. |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 768 | */ |
KAMEZAWA Hiroyuki | 989f89c | 2007-08-30 23:56:21 -0700 | [diff] [blame] | 769 | if (PageAnon(page)) { |
Hugh Dickins | 1ce82b6 | 2011-01-13 15:47:30 -0800 | [diff] [blame] | 770 | /* |
| 771 | * Only page_lock_anon_vma() understands the subtleties of |
| 772 | * getting a hold on an anon_vma from outside one of its mms. |
| 773 | */ |
Peter Zijlstra | 746b18d | 2011-05-24 17:12:10 -0700 | [diff] [blame] | 774 | anon_vma = page_get_anon_vma(page); |
Hugh Dickins | 1ce82b6 | 2011-01-13 15:47:30 -0800 | [diff] [blame] | 775 | if (anon_vma) { |
| 776 | /* |
Peter Zijlstra | 746b18d | 2011-05-24 17:12:10 -0700 | [diff] [blame] | 777 | * Anon page |
Hugh Dickins | 1ce82b6 | 2011-01-13 15:47:30 -0800 | [diff] [blame] | 778 | */ |
Hugh Dickins | 1ce82b6 | 2011-01-13 15:47:30 -0800 | [diff] [blame] | 779 | } else if (PageSwapCache(page)) { |
Mel Gorman | 3fe2011 | 2010-05-24 14:32:20 -0700 | [diff] [blame] | 780 | /* |
| 781 | * We cannot be sure that the anon_vma of an unmapped |
| 782 | * swapcache page is safe to use because we don't |
| 783 | * know in advance if the VMA that this page belonged |
| 784 | * to still exists. If the VMA and others sharing the |
| 785 | * data have been freed, then the anon_vma could |
| 786 | * already be invalid. |
| 787 | * |
| 788 | * To avoid this possibility, swapcache pages get |
| 789 | * migrated but are not remapped when migration |
| 790 | * completes |
| 791 | */ |
| 792 | remap_swapcache = 0; |
| 793 | } else { |
Hugh Dickins | 1ce82b6 | 2011-01-13 15:47:30 -0800 | [diff] [blame] | 794 | goto uncharge; |
Mel Gorman | 3fe2011 | 2010-05-24 14:32:20 -0700 | [diff] [blame] | 795 | } |
KAMEZAWA Hiroyuki | 989f89c | 2007-08-30 23:56:21 -0700 | [diff] [blame] | 796 | } |
Shaohua Li | 62e1c55 | 2008-02-04 22:29:33 -0800 | [diff] [blame] | 797 | |
KAMEZAWA Hiroyuki | dc386d4 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 798 | /* |
Shaohua Li | 62e1c55 | 2008-02-04 22:29:33 -0800 | [diff] [blame] | 799 | * Corner case handling: |
| 800 | * 1. When a new swap-cache page is read into, it is added to the LRU |
| 801 | * and treated as swapcache but it has no rmap yet. |
| 802 | * Calling try_to_unmap() against a page->mapping==NULL page will |
| 803 | * trigger a BUG. So handle it here. |
| 804 | * 2. An orphaned page (see truncate_complete_page) might have |
| 805 | * fs-private metadata. The page can be picked up due to memory |
| 806 | * offlining. Everywhere else except page reclaim, the page is |
| 807 | * invisible to the vm, so the page can not be migrated. So try to |
| 808 | * free the metadata, so the page can be freed. |
KAMEZAWA Hiroyuki | dc386d4 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 809 | */ |
Shaohua Li | 62e1c55 | 2008-02-04 22:29:33 -0800 | [diff] [blame] | 810 | if (!page->mapping) { |
Hugh Dickins | 1ce82b6 | 2011-01-13 15:47:30 -0800 | [diff] [blame] | 811 | VM_BUG_ON(PageAnon(page)); |
| 812 | if (page_has_private(page)) { |
Shaohua Li | 62e1c55 | 2008-02-04 22:29:33 -0800 | [diff] [blame] | 813 | try_to_free_buffers(page); |
Hugh Dickins | 1ce82b6 | 2011-01-13 15:47:30 -0800 | [diff] [blame] | 814 | goto uncharge; |
Shaohua Li | 62e1c55 | 2008-02-04 22:29:33 -0800 | [diff] [blame] | 815 | } |
Shaohua Li | abfc348 | 2009-09-21 17:01:19 -0700 | [diff] [blame] | 816 | goto skip_unmap; |
Shaohua Li | 62e1c55 | 2008-02-04 22:29:33 -0800 | [diff] [blame] | 817 | } |
| 818 | |
KAMEZAWA Hiroyuki | dc386d4 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 819 | /* Establish migration ptes or remove ptes */ |
Andi Kleen | 14fa31b | 2009-09-16 11:50:10 +0200 | [diff] [blame] | 820 | try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS); |
KAMEZAWA Hiroyuki | dc386d4 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 821 | |
Shaohua Li | abfc348 | 2009-09-21 17:01:19 -0700 | [diff] [blame] | 822 | skip_unmap: |
Christoph Lameter | e6a1530 | 2006-06-25 05:46:49 -0700 | [diff] [blame] | 823 | if (!page_mapped(page)) |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 824 | rc = move_to_new_page(newpage, page, remap_swapcache, mode); |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 825 | |
Mel Gorman | 3fe2011 | 2010-05-24 14:32:20 -0700 | [diff] [blame] | 826 | if (rc && remap_swapcache) |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 827 | remove_migration_ptes(page, page); |
Mel Gorman | 3f6c827 | 2010-05-24 14:32:17 -0700 | [diff] [blame] | 828 | |
| 829 | /* Drop an anon_vma reference if we took one */ |
Rik van Riel | 7654506 | 2010-08-09 17:18:41 -0700 | [diff] [blame] | 830 | if (anon_vma) |
Peter Zijlstra | 9e60109 | 2011-03-22 16:32:46 -0700 | [diff] [blame] | 831 | put_anon_vma(anon_vma); |
Mel Gorman | 3f6c827 | 2010-05-24 14:32:17 -0700 | [diff] [blame] | 832 | |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 833 | uncharge: |
| 834 | if (!charge) |
Daisuke Nishimura | 50de1dd | 2011-01-13 15:47:43 -0800 | [diff] [blame] | 835 | mem_cgroup_end_migration(mem, page, newpage, rc == 0); |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 836 | unlock: |
| 837 | unlock_page(page); |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 838 | out: |
| 839 | return rc; |
| 840 | } |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 841 | |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 842 | /* |
| 843 | * Obtain the lock on page, remove all ptes and migrate the page |
| 844 | * to the newly allocated page in newpage. |
| 845 | */ |
| 846 | static int unmap_and_move(new_page_t get_new_page, unsigned long private, |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 847 | struct page *page, int force, bool offlining, |
| 848 | enum migrate_mode mode) |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 849 | { |
| 850 | int rc = 0; |
| 851 | int *result = NULL; |
| 852 | struct page *newpage = get_new_page(page, private, &result); |
| 853 | |
| 854 | if (!newpage) |
| 855 | return -ENOMEM; |
| 856 | |
| 857 | if (page_count(page) == 1) { |
| 858 | /* page was freed from under us. So we are done. */ |
| 859 | goto out; |
| 860 | } |
| 861 | |
| 862 | if (unlikely(PageTransHuge(page))) |
| 863 | if (unlikely(split_huge_page(page))) |
| 864 | goto out; |
| 865 | |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 866 | rc = __unmap_and_move(page, newpage, force, offlining, mode); |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 867 | out: |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 868 | if (rc != -EAGAIN) { |
Minchan Kim | 0dabec9 | 2011-10-31 17:06:57 -0700 | [diff] [blame] | 869 | /* |
| 870 | * A page that has been migrated has all references |
| 871 | * removed and will be freed. A page that has not been |
| 872 | * migrated will have kepts its references and be |
| 873 | * restored. |
| 874 | */ |
| 875 | list_del(&page->lru); |
KOSAKI Motohiro | a731286 | 2009-09-21 17:01:37 -0700 | [diff] [blame] | 876 | dec_zone_page_state(page, NR_ISOLATED_ANON + |
Johannes Weiner | 6c0b135 | 2009-09-21 17:02:59 -0700 | [diff] [blame] | 877 | page_is_file_cache(page)); |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 878 | putback_lru_page(page); |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 879 | } |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 880 | /* |
| 881 | * Move the new page to the LRU. If migration was not successful |
| 882 | * then this will free the page. |
| 883 | */ |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 884 | putback_lru_page(newpage); |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 885 | if (result) { |
| 886 | if (rc) |
| 887 | *result = rc; |
| 888 | else |
| 889 | *result = page_to_nid(newpage); |
| 890 | } |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 891 | return rc; |
| 892 | } |
| 893 | |
| 894 | /* |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 895 | * Counterpart of unmap_and_move_page() for hugepage migration. |
| 896 | * |
| 897 | * This function doesn't wait the completion of hugepage I/O |
| 898 | * because there is no race between I/O and migration for hugepage. |
| 899 | * Note that currently hugepage I/O occurs only in direct I/O |
| 900 | * where no lock is held and PG_writeback is irrelevant, |
| 901 | * and writeback status of all subpages are counted in the reference |
| 902 | * count of the head page (i.e. if all subpages of a 2MB hugepage are |
| 903 | * under direct I/O, the reference of the head page is 512 and a bit more.) |
| 904 | * This means that when we try to migrate hugepage whose subpages are |
| 905 | * doing direct I/O, some references remain after try_to_unmap() and |
| 906 | * hugepage migration fails without data corruption. |
| 907 | * |
| 908 | * There is also no race when direct I/O is issued on the page under migration, |
| 909 | * because then pte is replaced with migration swap entry and direct I/O code |
| 910 | * will wait in the page fault for migration to complete. |
| 911 | */ |
| 912 | static int unmap_and_move_huge_page(new_page_t get_new_page, |
| 913 | unsigned long private, struct page *hpage, |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 914 | int force, bool offlining, |
| 915 | enum migrate_mode mode) |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 916 | { |
| 917 | int rc = 0; |
| 918 | int *result = NULL; |
| 919 | struct page *new_hpage = get_new_page(hpage, private, &result); |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 920 | struct anon_vma *anon_vma = NULL; |
| 921 | |
| 922 | if (!new_hpage) |
| 923 | return -ENOMEM; |
| 924 | |
| 925 | rc = -EAGAIN; |
| 926 | |
| 927 | if (!trylock_page(hpage)) { |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 928 | if (!force || mode != MIGRATE_SYNC) |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 929 | goto out; |
| 930 | lock_page(hpage); |
| 931 | } |
| 932 | |
Peter Zijlstra | 746b18d | 2011-05-24 17:12:10 -0700 | [diff] [blame] | 933 | if (PageAnon(hpage)) |
| 934 | anon_vma = page_get_anon_vma(hpage); |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 935 | |
| 936 | try_to_unmap(hpage, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS); |
| 937 | |
| 938 | if (!page_mapped(hpage)) |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 939 | rc = move_to_new_page(new_hpage, hpage, 1, mode); |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 940 | |
| 941 | if (rc) |
| 942 | remove_migration_ptes(hpage, hpage); |
| 943 | |
Hugh Dickins | fd4a466 | 2011-01-13 15:47:31 -0800 | [diff] [blame] | 944 | if (anon_vma) |
Peter Zijlstra | 9e60109 | 2011-03-22 16:32:46 -0700 | [diff] [blame] | 945 | put_anon_vma(anon_vma); |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 946 | unlock_page(hpage); |
| 947 | |
Hillf Danton | 0976133 | 2011-12-08 14:34:20 -0800 | [diff] [blame] | 948 | out: |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 949 | if (rc != -EAGAIN) { |
| 950 | list_del(&hpage->lru); |
| 951 | put_page(hpage); |
| 952 | } |
| 953 | |
| 954 | put_page(new_hpage); |
| 955 | |
| 956 | if (result) { |
| 957 | if (rc) |
| 958 | *result = rc; |
| 959 | else |
| 960 | *result = page_to_nid(new_hpage); |
| 961 | } |
| 962 | return rc; |
| 963 | } |
| 964 | |
| 965 | /* |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 966 | * migrate_pages |
| 967 | * |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 968 | * The function takes one list of pages to migrate and a function |
| 969 | * that determines from the page to be migrated and the private data |
| 970 | * the target of the move and allocates the page. |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 971 | * |
| 972 | * The function returns after 10 attempts or if no pages |
| 973 | * are movable anymore because to has become empty |
Minchan Kim | cf608ac | 2010-10-26 14:21:29 -0700 | [diff] [blame] | 974 | * or no retryable pages exist anymore. |
| 975 | * Caller should call putback_lru_pages to return pages to the LRU |
Minchan Kim | 28bd657 | 2011-01-25 15:07:26 -0800 | [diff] [blame] | 976 | * or free list only if ret != 0. |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 977 | * |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 978 | * Return: Number of pages not migrated or error code. |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 979 | */ |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 980 | int migrate_pages(struct list_head *from, |
Mel Gorman | 7f0f249 | 2011-01-13 15:45:58 -0800 | [diff] [blame] | 981 | new_page_t get_new_page, unsigned long private, bool offlining, |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 982 | enum migrate_mode mode) |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 983 | { |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 984 | int retry = 1; |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 985 | int nr_failed = 0; |
Mel Gorman | cd9bb9b | 2012-10-19 10:46:20 +0100 | [diff] [blame] | 986 | int nr_succeeded = 0; |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 987 | int pass = 0; |
| 988 | struct page *page; |
| 989 | struct page *page2; |
| 990 | int swapwrite = current->flags & PF_SWAPWRITE; |
| 991 | int rc; |
| 992 | |
Liam Mark | cc2d4bd | 2013-01-16 10:14:40 -0800 | [diff] [blame] | 993 | trace_migrate_pages_start(mode); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 994 | if (!swapwrite) |
| 995 | current->flags |= PF_SWAPWRITE; |
| 996 | |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 997 | for(pass = 0; pass < 10 && retry; pass++) { |
| 998 | retry = 0; |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 999 | |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1000 | list_for_each_entry_safe(page, page2, from, lru) { |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1001 | cond_resched(); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1002 | |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 1003 | rc = unmap_and_move(get_new_page, private, |
Mel Gorman | 77f1fe6 | 2011-01-13 15:45:57 -0800 | [diff] [blame] | 1004 | page, pass > 2, offlining, |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 1005 | mode); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1006 | |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1007 | switch(rc) { |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 1008 | case -ENOMEM: |
| 1009 | goto out; |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1010 | case -EAGAIN: |
Christoph Lameter | 2d1db3b | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 1011 | retry++; |
Liam Mark | cc2d4bd | 2013-01-16 10:14:40 -0800 | [diff] [blame] | 1012 | trace_migrate_retry(retry); |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1013 | break; |
| 1014 | case 0: |
Mel Gorman | cd9bb9b | 2012-10-19 10:46:20 +0100 | [diff] [blame] | 1015 | nr_succeeded++; |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1016 | break; |
| 1017 | default: |
Christoph Lameter | 2d1db3b | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 1018 | /* Permanent failure */ |
Christoph Lameter | 2d1db3b | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 1019 | nr_failed++; |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 1020 | break; |
Christoph Lameter | 2d1db3b | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 1021 | } |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1022 | } |
| 1023 | } |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 1024 | rc = 0; |
| 1025 | out: |
Mel Gorman | cd9bb9b | 2012-10-19 10:46:20 +0100 | [diff] [blame] | 1026 | if (nr_succeeded) |
| 1027 | count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded); |
| 1028 | if (nr_failed) |
| 1029 | count_vm_events(PGMIGRATE_FAIL, nr_failed); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1030 | if (!swapwrite) |
| 1031 | current->flags &= ~PF_SWAPWRITE; |
| 1032 | |
Liam Mark | cc2d4bd | 2013-01-16 10:14:40 -0800 | [diff] [blame] | 1033 | trace_migrate_pages_end(mode); |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 1034 | if (rc) |
| 1035 | return rc; |
| 1036 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 1037 | return nr_failed + retry; |
| 1038 | } |
| 1039 | |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1040 | int migrate_huge_pages(struct list_head *from, |
Mel Gorman | 7f0f249 | 2011-01-13 15:45:58 -0800 | [diff] [blame] | 1041 | new_page_t get_new_page, unsigned long private, bool offlining, |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 1042 | enum migrate_mode mode) |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1043 | { |
| 1044 | int retry = 1; |
| 1045 | int nr_failed = 0; |
| 1046 | int pass = 0; |
| 1047 | struct page *page; |
| 1048 | struct page *page2; |
| 1049 | int rc; |
| 1050 | |
| 1051 | for (pass = 0; pass < 10 && retry; pass++) { |
| 1052 | retry = 0; |
| 1053 | |
| 1054 | list_for_each_entry_safe(page, page2, from, lru) { |
| 1055 | cond_resched(); |
| 1056 | |
| 1057 | rc = unmap_and_move_huge_page(get_new_page, |
Mel Gorman | 77f1fe6 | 2011-01-13 15:45:57 -0800 | [diff] [blame] | 1058 | private, page, pass > 2, offlining, |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 1059 | mode); |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1060 | |
| 1061 | switch(rc) { |
| 1062 | case -ENOMEM: |
| 1063 | goto out; |
| 1064 | case -EAGAIN: |
| 1065 | retry++; |
| 1066 | break; |
| 1067 | case 0: |
| 1068 | break; |
| 1069 | default: |
| 1070 | /* Permanent failure */ |
| 1071 | nr_failed++; |
| 1072 | break; |
| 1073 | } |
| 1074 | } |
| 1075 | } |
| 1076 | rc = 0; |
| 1077 | out: |
Naoya Horiguchi | 290408d | 2010-09-08 10:19:35 +0900 | [diff] [blame] | 1078 | if (rc) |
| 1079 | return rc; |
| 1080 | |
| 1081 | return nr_failed + retry; |
| 1082 | } |
| 1083 | |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1084 | #ifdef CONFIG_NUMA |
| 1085 | /* |
| 1086 | * Move a list of individual pages |
| 1087 | */ |
| 1088 | struct page_to_node { |
| 1089 | unsigned long addr; |
| 1090 | struct page *page; |
| 1091 | int node; |
| 1092 | int status; |
| 1093 | }; |
| 1094 | |
| 1095 | static struct page *new_page_node(struct page *p, unsigned long private, |
| 1096 | int **result) |
| 1097 | { |
| 1098 | struct page_to_node *pm = (struct page_to_node *)private; |
| 1099 | |
| 1100 | while (pm->node != MAX_NUMNODES && pm->page != p) |
| 1101 | pm++; |
| 1102 | |
| 1103 | if (pm->node == MAX_NUMNODES) |
| 1104 | return NULL; |
| 1105 | |
| 1106 | *result = &pm->status; |
| 1107 | |
Mel Gorman | 6484eb3 | 2009-06-16 15:31:54 -0700 | [diff] [blame] | 1108 | return alloc_pages_exact_node(pm->node, |
Mel Gorman | 769848c | 2007-07-17 04:03:05 -0700 | [diff] [blame] | 1109 | GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0); |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | /* |
| 1113 | * Move a set of pages as indicated in the pm array. The addr |
| 1114 | * field must be set to the virtual address of the page to be moved |
| 1115 | * and the node number must contain a valid target node. |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1116 | * The pm array ends with node = MAX_NUMNODES. |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1117 | */ |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1118 | static int do_move_page_to_node_array(struct mm_struct *mm, |
| 1119 | struct page_to_node *pm, |
| 1120 | int migrate_all) |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1121 | { |
| 1122 | int err; |
| 1123 | struct page_to_node *pp; |
| 1124 | LIST_HEAD(pagelist); |
| 1125 | |
| 1126 | down_read(&mm->mmap_sem); |
| 1127 | |
| 1128 | /* |
| 1129 | * Build a list of pages to migrate |
| 1130 | */ |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1131 | for (pp = pm; pp->node != MAX_NUMNODES; pp++) { |
| 1132 | struct vm_area_struct *vma; |
| 1133 | struct page *page; |
| 1134 | |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1135 | err = -EFAULT; |
| 1136 | vma = find_vma(mm, pp->addr); |
Gleb Natapov | 70384dc | 2010-10-26 14:22:07 -0700 | [diff] [blame] | 1137 | if (!vma || pp->addr < vma->vm_start || !vma_migratable(vma)) |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1138 | goto set_status; |
| 1139 | |
Andrea Arcangeli | 500d65d | 2011-01-13 15:46:55 -0800 | [diff] [blame] | 1140 | page = follow_page(vma, pp->addr, FOLL_GET|FOLL_SPLIT); |
Linus Torvalds | 89f5b7d | 2008-06-20 11:18:25 -0700 | [diff] [blame] | 1141 | |
| 1142 | err = PTR_ERR(page); |
| 1143 | if (IS_ERR(page)) |
| 1144 | goto set_status; |
| 1145 | |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1146 | err = -ENOENT; |
| 1147 | if (!page) |
| 1148 | goto set_status; |
| 1149 | |
Hugh Dickins | 62b61f6 | 2009-12-14 17:59:33 -0800 | [diff] [blame] | 1150 | /* Use PageReserved to check for zero page */ |
| 1151 | if (PageReserved(page) || PageKsm(page)) |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1152 | goto put_and_set; |
| 1153 | |
| 1154 | pp->page = page; |
| 1155 | err = page_to_nid(page); |
| 1156 | |
| 1157 | if (err == pp->node) |
| 1158 | /* |
| 1159 | * Node already in the right place |
| 1160 | */ |
| 1161 | goto put_and_set; |
| 1162 | |
| 1163 | err = -EACCES; |
| 1164 | if (page_mapcount(page) > 1 && |
| 1165 | !migrate_all) |
| 1166 | goto put_and_set; |
| 1167 | |
Nick Piggin | 62695a8 | 2008-10-18 20:26:09 -0700 | [diff] [blame] | 1168 | err = isolate_lru_page(page); |
KOSAKI Motohiro | 6d9c285 | 2009-12-14 17:58:11 -0800 | [diff] [blame] | 1169 | if (!err) { |
Nick Piggin | 62695a8 | 2008-10-18 20:26:09 -0700 | [diff] [blame] | 1170 | list_add_tail(&page->lru, &pagelist); |
KOSAKI Motohiro | 6d9c285 | 2009-12-14 17:58:11 -0800 | [diff] [blame] | 1171 | inc_zone_page_state(page, NR_ISOLATED_ANON + |
| 1172 | page_is_file_cache(page)); |
| 1173 | } |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1174 | put_and_set: |
| 1175 | /* |
| 1176 | * Either remove the duplicate refcount from |
| 1177 | * isolate_lru_page() or drop the page ref if it was |
| 1178 | * not isolated. |
| 1179 | */ |
| 1180 | put_page(page); |
| 1181 | set_status: |
| 1182 | pp->status = err; |
| 1183 | } |
| 1184 | |
Brice Goglin | e78bbfa | 2008-10-18 20:27:15 -0700 | [diff] [blame] | 1185 | err = 0; |
Minchan Kim | cf608ac | 2010-10-26 14:21:29 -0700 | [diff] [blame] | 1186 | if (!list_empty(&pagelist)) { |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1187 | err = migrate_pages(&pagelist, new_page_node, |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 1188 | (unsigned long)pm, 0, MIGRATE_SYNC); |
Minchan Kim | cf608ac | 2010-10-26 14:21:29 -0700 | [diff] [blame] | 1189 | if (err) |
| 1190 | putback_lru_pages(&pagelist); |
| 1191 | } |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1192 | |
| 1193 | up_read(&mm->mmap_sem); |
| 1194 | return err; |
| 1195 | } |
| 1196 | |
| 1197 | /* |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1198 | * Migrate an array of page address onto an array of nodes and fill |
| 1199 | * the corresponding array of status. |
| 1200 | */ |
Christoph Lameter | 3268c63 | 2012-03-21 16:34:06 -0700 | [diff] [blame] | 1201 | 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] | 1202 | unsigned long nr_pages, |
| 1203 | const void __user * __user *pages, |
| 1204 | const int __user *nodes, |
| 1205 | int __user *status, int flags) |
| 1206 | { |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1207 | struct page_to_node *pm; |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1208 | unsigned long chunk_nr_pages; |
| 1209 | unsigned long chunk_start; |
| 1210 | int err; |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1211 | |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1212 | err = -ENOMEM; |
| 1213 | pm = (struct page_to_node *)__get_free_page(GFP_KERNEL); |
| 1214 | if (!pm) |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1215 | goto out; |
Brice Goglin | 35282a2 | 2009-06-16 15:32:43 -0700 | [diff] [blame] | 1216 | |
| 1217 | migrate_prep(); |
| 1218 | |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1219 | /* |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1220 | * Store a chunk of page_to_node array in a page, |
| 1221 | * but keep the last one as a marker |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1222 | */ |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1223 | chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1; |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1224 | |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1225 | for (chunk_start = 0; |
| 1226 | chunk_start < nr_pages; |
| 1227 | chunk_start += chunk_nr_pages) { |
| 1228 | int j; |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1229 | |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1230 | if (chunk_start + chunk_nr_pages > nr_pages) |
| 1231 | chunk_nr_pages = nr_pages - chunk_start; |
| 1232 | |
| 1233 | /* fill the chunk pm with addrs and nodes from user-space */ |
| 1234 | for (j = 0; j < chunk_nr_pages; j++) { |
| 1235 | const void __user *p; |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1236 | int node; |
| 1237 | |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1238 | err = -EFAULT; |
| 1239 | if (get_user(p, pages + j + chunk_start)) |
| 1240 | goto out_pm; |
| 1241 | pm[j].addr = (unsigned long) p; |
| 1242 | |
| 1243 | if (get_user(node, nodes + j + chunk_start)) |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1244 | goto out_pm; |
| 1245 | |
| 1246 | err = -ENODEV; |
Linus Torvalds | 6f5a55f | 2010-02-05 16:16:50 -0800 | [diff] [blame] | 1247 | if (node < 0 || node >= MAX_NUMNODES) |
| 1248 | goto out_pm; |
| 1249 | |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1250 | if (!node_state(node, N_HIGH_MEMORY)) |
| 1251 | goto out_pm; |
| 1252 | |
| 1253 | err = -EACCES; |
| 1254 | if (!node_isset(node, task_nodes)) |
| 1255 | goto out_pm; |
| 1256 | |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1257 | pm[j].node = node; |
| 1258 | } |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1259 | |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1260 | /* End marker for this chunk */ |
| 1261 | pm[chunk_nr_pages].node = MAX_NUMNODES; |
| 1262 | |
| 1263 | /* Migrate this chunk */ |
| 1264 | err = do_move_page_to_node_array(mm, pm, |
| 1265 | flags & MPOL_MF_MOVE_ALL); |
| 1266 | if (err < 0) |
| 1267 | goto out_pm; |
| 1268 | |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1269 | /* Return status information */ |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1270 | for (j = 0; j < chunk_nr_pages; j++) |
| 1271 | if (put_user(pm[j].status, status + j + chunk_start)) { |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1272 | err = -EFAULT; |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1273 | goto out_pm; |
| 1274 | } |
| 1275 | } |
| 1276 | err = 0; |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1277 | |
| 1278 | out_pm: |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 1279 | free_page((unsigned long)pm); |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1280 | out: |
| 1281 | return err; |
| 1282 | } |
| 1283 | |
| 1284 | /* |
Brice Goglin | 2f007e7 | 2008-10-18 20:27:16 -0700 | [diff] [blame] | 1285 | * 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] | 1286 | */ |
Brice Goglin | 80bba12 | 2008-12-09 13:14:23 -0800 | [diff] [blame] | 1287 | static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages, |
| 1288 | const void __user **pages, int *status) |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1289 | { |
Brice Goglin | 2f007e7 | 2008-10-18 20:27:16 -0700 | [diff] [blame] | 1290 | unsigned long i; |
Brice Goglin | 2f007e7 | 2008-10-18 20:27:16 -0700 | [diff] [blame] | 1291 | |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1292 | down_read(&mm->mmap_sem); |
| 1293 | |
Brice Goglin | 2f007e7 | 2008-10-18 20:27:16 -0700 | [diff] [blame] | 1294 | for (i = 0; i < nr_pages; i++) { |
Brice Goglin | 80bba12 | 2008-12-09 13:14:23 -0800 | [diff] [blame] | 1295 | unsigned long addr = (unsigned long)(*pages); |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1296 | struct vm_area_struct *vma; |
| 1297 | struct page *page; |
KOSAKI Motohiro | c095adb | 2008-12-16 16:06:43 +0900 | [diff] [blame] | 1298 | int err = -EFAULT; |
Brice Goglin | 2f007e7 | 2008-10-18 20:27:16 -0700 | [diff] [blame] | 1299 | |
| 1300 | vma = find_vma(mm, addr); |
Gleb Natapov | 70384dc | 2010-10-26 14:22:07 -0700 | [diff] [blame] | 1301 | if (!vma || addr < vma->vm_start) |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1302 | goto set_status; |
| 1303 | |
Brice Goglin | 2f007e7 | 2008-10-18 20:27:16 -0700 | [diff] [blame] | 1304 | page = follow_page(vma, addr, 0); |
Linus Torvalds | 89f5b7d | 2008-06-20 11:18:25 -0700 | [diff] [blame] | 1305 | |
| 1306 | err = PTR_ERR(page); |
| 1307 | if (IS_ERR(page)) |
| 1308 | goto set_status; |
| 1309 | |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1310 | err = -ENOENT; |
| 1311 | /* Use PageReserved to check for zero page */ |
Hugh Dickins | 62b61f6 | 2009-12-14 17:59:33 -0800 | [diff] [blame] | 1312 | if (!page || PageReserved(page) || PageKsm(page)) |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1313 | goto set_status; |
| 1314 | |
| 1315 | err = page_to_nid(page); |
| 1316 | set_status: |
Brice Goglin | 80bba12 | 2008-12-09 13:14:23 -0800 | [diff] [blame] | 1317 | *status = err; |
| 1318 | |
| 1319 | pages++; |
| 1320 | status++; |
| 1321 | } |
| 1322 | |
| 1323 | up_read(&mm->mmap_sem); |
| 1324 | } |
| 1325 | |
| 1326 | /* |
| 1327 | * Determine the nodes of a user array of pages and store it in |
| 1328 | * a user array of status. |
| 1329 | */ |
| 1330 | static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages, |
| 1331 | const void __user * __user *pages, |
| 1332 | int __user *status) |
| 1333 | { |
| 1334 | #define DO_PAGES_STAT_CHUNK_NR 16 |
| 1335 | const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR]; |
| 1336 | int chunk_status[DO_PAGES_STAT_CHUNK_NR]; |
Brice Goglin | 80bba12 | 2008-12-09 13:14:23 -0800 | [diff] [blame] | 1337 | |
H. Peter Anvin | 87b8d1a | 2010-02-18 16:13:40 -0800 | [diff] [blame] | 1338 | while (nr_pages) { |
| 1339 | unsigned long chunk_nr; |
Brice Goglin | 80bba12 | 2008-12-09 13:14:23 -0800 | [diff] [blame] | 1340 | |
H. Peter Anvin | 87b8d1a | 2010-02-18 16:13:40 -0800 | [diff] [blame] | 1341 | chunk_nr = nr_pages; |
| 1342 | if (chunk_nr > DO_PAGES_STAT_CHUNK_NR) |
| 1343 | chunk_nr = DO_PAGES_STAT_CHUNK_NR; |
| 1344 | |
| 1345 | if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages))) |
| 1346 | break; |
Brice Goglin | 80bba12 | 2008-12-09 13:14:23 -0800 | [diff] [blame] | 1347 | |
| 1348 | do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status); |
| 1349 | |
H. Peter Anvin | 87b8d1a | 2010-02-18 16:13:40 -0800 | [diff] [blame] | 1350 | if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status))) |
| 1351 | break; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1352 | |
H. Peter Anvin | 87b8d1a | 2010-02-18 16:13:40 -0800 | [diff] [blame] | 1353 | pages += chunk_nr; |
| 1354 | status += chunk_nr; |
| 1355 | nr_pages -= chunk_nr; |
| 1356 | } |
| 1357 | return nr_pages ? -EFAULT : 0; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | /* |
| 1361 | * Move a list of pages in the address space of the currently executing |
| 1362 | * process. |
| 1363 | */ |
Heiko Carstens | 938bb9f | 2009-01-14 14:14:30 +0100 | [diff] [blame] | 1364 | SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages, |
| 1365 | const void __user * __user *, pages, |
| 1366 | const int __user *, nodes, |
| 1367 | int __user *, status, int, flags) |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1368 | { |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1369 | struct task_struct *task; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1370 | struct mm_struct *mm; |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1371 | int err; |
Christoph Lameter | 3268c63 | 2012-03-21 16:34:06 -0700 | [diff] [blame] | 1372 | nodemask_t task_nodes; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1373 | |
| 1374 | /* Check flags */ |
| 1375 | if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL)) |
| 1376 | return -EINVAL; |
| 1377 | |
| 1378 | if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE)) |
| 1379 | return -EPERM; |
| 1380 | |
| 1381 | /* Find the mm_struct */ |
Greg Thelen | a879bf5 | 2011-02-25 14:44:13 -0800 | [diff] [blame] | 1382 | rcu_read_lock(); |
Pavel Emelyanov | 228ebcb | 2007-10-18 23:40:16 -0700 | [diff] [blame] | 1383 | task = pid ? find_task_by_vpid(pid) : current; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1384 | if (!task) { |
Greg Thelen | a879bf5 | 2011-02-25 14:44:13 -0800 | [diff] [blame] | 1385 | rcu_read_unlock(); |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1386 | return -ESRCH; |
| 1387 | } |
Christoph Lameter | 3268c63 | 2012-03-21 16:34:06 -0700 | [diff] [blame] | 1388 | get_task_struct(task); |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1389 | |
| 1390 | /* |
| 1391 | * Check if this process has the right to modify the specified |
Linus Torvalds | 4c49736 | 2017-08-20 13:26:27 -0700 | [diff] [blame] | 1392 | * process. Use the regular "ptrace_may_access()" checks. |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1393 | */ |
Linus Torvalds | 4c49736 | 2017-08-20 13:26:27 -0700 | [diff] [blame] | 1394 | if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1395 | rcu_read_unlock(); |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1396 | err = -EPERM; |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1397 | goto out; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1398 | } |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1399 | rcu_read_unlock(); |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1400 | |
David Quigley | 86c3a76 | 2006-06-23 02:04:02 -0700 | [diff] [blame] | 1401 | err = security_task_movememory(task); |
| 1402 | if (err) |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1403 | goto out; |
David Quigley | 86c3a76 | 2006-06-23 02:04:02 -0700 | [diff] [blame] | 1404 | |
Christoph Lameter | 3268c63 | 2012-03-21 16:34:06 -0700 | [diff] [blame] | 1405 | task_nodes = cpuset_mems_allowed(task); |
| 1406 | mm = get_task_mm(task); |
| 1407 | put_task_struct(task); |
| 1408 | |
Sasha Levin | 6e8b09e | 2012-04-25 16:01:53 -0700 | [diff] [blame] | 1409 | if (!mm) |
| 1410 | return -EINVAL; |
| 1411 | |
| 1412 | if (nodes) |
| 1413 | err = do_pages_move(mm, task_nodes, nr_pages, pages, |
| 1414 | nodes, status, flags); |
| 1415 | else |
| 1416 | err = do_pages_stat(mm, nr_pages, pages, status); |
Christoph Lameter | 3268c63 | 2012-03-21 16:34:06 -0700 | [diff] [blame] | 1417 | |
| 1418 | mmput(mm); |
| 1419 | return err; |
David Quigley | 86c3a76 | 2006-06-23 02:04:02 -0700 | [diff] [blame] | 1420 | |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1421 | out: |
Christoph Lameter | 3268c63 | 2012-03-21 16:34:06 -0700 | [diff] [blame] | 1422 | put_task_struct(task); |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1423 | return err; |
| 1424 | } |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1425 | |
Christoph Lameter | 7b2259b | 2006-06-25 05:46:48 -0700 | [diff] [blame] | 1426 | /* |
| 1427 | * Call migration functions in the vma_ops that may prepare |
| 1428 | * memory in a vm for migration. migration functions may perform |
| 1429 | * the migration for vmas that do not have an underlying page struct. |
| 1430 | */ |
| 1431 | int migrate_vmas(struct mm_struct *mm, const nodemask_t *to, |
| 1432 | const nodemask_t *from, unsigned long flags) |
| 1433 | { |
| 1434 | struct vm_area_struct *vma; |
| 1435 | int err = 0; |
| 1436 | |
Daisuke Nishimura | 1001c9f | 2009-02-11 13:04:18 -0800 | [diff] [blame] | 1437 | for (vma = mm->mmap; vma && !err; vma = vma->vm_next) { |
Christoph Lameter | 7b2259b | 2006-06-25 05:46:48 -0700 | [diff] [blame] | 1438 | if (vma->vm_ops && vma->vm_ops->migrate) { |
| 1439 | err = vma->vm_ops->migrate(vma, to, from, flags); |
| 1440 | if (err) |
| 1441 | break; |
| 1442 | } |
| 1443 | } |
| 1444 | return err; |
| 1445 | } |
Gerald Schaefer | 83d1674 | 2008-07-23 21:28:22 -0700 | [diff] [blame] | 1446 | #endif |