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