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> |
| 16 | #include <linux/module.h> |
| 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> |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 33 | #include <linux/memcontrol.h> |
Adrian Bunk | 4f5ca26 | 2008-07-23 21:27:02 -0700 | [diff] [blame] | 34 | #include <linux/syscalls.h> |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 35 | |
| 36 | #include "internal.h" |
| 37 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 38 | #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru)) |
| 39 | |
| 40 | /* |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 41 | * migrate_prep() needs to be called before we start compiling a list of pages |
| 42 | * to be migrated using isolate_lru_page(). |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 43 | */ |
| 44 | int migrate_prep(void) |
| 45 | { |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 46 | /* |
| 47 | * Clear the LRU lists so pages can be isolated. |
| 48 | * Note that pages may be moved off the LRU after we have |
| 49 | * drained them. Those pages will fail to migrate like other |
| 50 | * pages that may be busy. |
| 51 | */ |
| 52 | lru_add_drain_all(); |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 57 | /* |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 58 | * Add isolated pages on the list back to the LRU under page lock |
| 59 | * to avoid leaking evictable pages back onto unevictable list. |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 60 | * |
| 61 | * returns the number of pages put back. |
| 62 | */ |
| 63 | int putback_lru_pages(struct list_head *l) |
| 64 | { |
| 65 | struct page *page; |
| 66 | struct page *page2; |
| 67 | int count = 0; |
| 68 | |
| 69 | list_for_each_entry_safe(page, page2, l, lru) { |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 70 | list_del(&page->lru); |
KOSAKI Motohiro | a731286 | 2009-09-21 17:01:37 -0700 | [diff] [blame] | 71 | dec_zone_page_state(page, NR_ISOLATED_ANON + |
Johannes Weiner | 6c0b135 | 2009-09-21 17:02:59 -0700 | [diff] [blame] | 72 | page_is_file_cache(page)); |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 73 | putback_lru_page(page); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 74 | count++; |
| 75 | } |
| 76 | return count; |
| 77 | } |
| 78 | |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 79 | /* |
| 80 | * Restore a potential migration pte to a working pte entry |
| 81 | */ |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 82 | static int remove_migration_pte(struct page *new, struct vm_area_struct *vma, |
| 83 | unsigned long addr, void *old) |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 84 | { |
| 85 | struct mm_struct *mm = vma->vm_mm; |
| 86 | swp_entry_t entry; |
| 87 | pgd_t *pgd; |
| 88 | pud_t *pud; |
| 89 | pmd_t *pmd; |
| 90 | pte_t *ptep, pte; |
| 91 | spinlock_t *ptl; |
| 92 | |
| 93 | pgd = pgd_offset(mm, addr); |
| 94 | if (!pgd_present(*pgd)) |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 95 | goto out; |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 96 | |
| 97 | pud = pud_offset(pgd, addr); |
| 98 | if (!pud_present(*pud)) |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 99 | goto out; |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 100 | |
| 101 | pmd = pmd_offset(pud, addr); |
| 102 | if (!pmd_present(*pmd)) |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 103 | goto out; |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 104 | |
| 105 | ptep = pte_offset_map(pmd, addr); |
| 106 | |
| 107 | if (!is_swap_pte(*ptep)) { |
| 108 | pte_unmap(ptep); |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 109 | goto out; |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | ptl = pte_lockptr(mm, pmd); |
| 113 | spin_lock(ptl); |
| 114 | pte = *ptep; |
| 115 | if (!is_swap_pte(pte)) |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 116 | goto unlock; |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 117 | |
| 118 | entry = pte_to_swp_entry(pte); |
| 119 | |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 120 | if (!is_migration_entry(entry) || |
| 121 | migration_entry_to_page(entry) != old) |
| 122 | goto unlock; |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 123 | |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 124 | get_page(new); |
| 125 | pte = pte_mkold(mk_pte(new, vma->vm_page_prot)); |
| 126 | if (is_write_migration_entry(entry)) |
| 127 | pte = pte_mkwrite(pte); |
KAMEZAWA Hiroyuki | 97ee052 | 2007-10-16 01:25:43 -0700 | [diff] [blame] | 128 | flush_cache_page(vma, addr, pte_pfn(pte)); |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 129 | set_pte_at(mm, addr, ptep, pte); |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 130 | |
| 131 | if (PageAnon(new)) |
| 132 | page_add_anon_rmap(new, vma, addr); |
| 133 | else |
| 134 | page_add_file_rmap(new); |
| 135 | |
| 136 | /* No need to invalidate - it was non-present before */ |
Russell King | 4b3073e | 2009-12-18 16:40:18 +0000 | [diff] [blame] | 137 | update_mmu_cache(vma, addr, ptep); |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 138 | unlock: |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 139 | pte_unmap_unlock(ptep, ptl); |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 140 | out: |
| 141 | return SWAP_AGAIN; |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /* |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 145 | * Get rid of all migration entries and replace them by |
| 146 | * references to the indicated page. |
| 147 | */ |
| 148 | static void remove_migration_ptes(struct page *old, struct page *new) |
| 149 | { |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 150 | rmap_walk(new, remove_migration_pte, old); |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | /* |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 154 | * Something used the pte of a page under migration. We need to |
| 155 | * get to the page and wait until migration is finished. |
| 156 | * When we return from this function the fault will be retried. |
| 157 | * |
| 158 | * This function is called from do_swap_page(). |
| 159 | */ |
| 160 | void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd, |
| 161 | unsigned long address) |
| 162 | { |
| 163 | pte_t *ptep, pte; |
| 164 | spinlock_t *ptl; |
| 165 | swp_entry_t entry; |
| 166 | struct page *page; |
| 167 | |
| 168 | ptep = pte_offset_map_lock(mm, pmd, address, &ptl); |
| 169 | pte = *ptep; |
| 170 | if (!is_swap_pte(pte)) |
| 171 | goto out; |
| 172 | |
| 173 | entry = pte_to_swp_entry(pte); |
| 174 | if (!is_migration_entry(entry)) |
| 175 | goto out; |
| 176 | |
| 177 | page = migration_entry_to_page(entry); |
| 178 | |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 179 | /* |
| 180 | * Once radix-tree replacement of page migration started, page_count |
| 181 | * *must* be zero. And, we don't want to call wait_on_page_locked() |
| 182 | * against a page without get_page(). |
| 183 | * So, we use get_page_unless_zero(), here. Even failed, page fault |
| 184 | * will occur again. |
| 185 | */ |
| 186 | if (!get_page_unless_zero(page)) |
| 187 | goto out; |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 188 | pte_unmap_unlock(ptep, ptl); |
| 189 | wait_on_page_locked(page); |
| 190 | put_page(page); |
| 191 | return; |
| 192 | out: |
| 193 | pte_unmap_unlock(ptep, ptl); |
| 194 | } |
| 195 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 196 | /* |
Christoph Lameter | c3fcf8a | 2006-06-23 02:03:32 -0700 | [diff] [blame] | 197 | * Replace the page in the mapping. |
Christoph Lameter | 5b5c712 | 2006-06-23 02:03:29 -0700 | [diff] [blame] | 198 | * |
| 199 | * The number of remaining references must be: |
| 200 | * 1 for anonymous pages without a mapping |
| 201 | * 2 for pages with a mapping |
David Howells | 266cf65 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 202 | * 3 for pages with a mapping and PagePrivate/PagePrivate2 set. |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 203 | */ |
Christoph Lameter | 2d1db3b | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 204 | static int migrate_page_move_mapping(struct address_space *mapping, |
| 205 | struct page *newpage, struct page *page) |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 206 | { |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 207 | int expected_count; |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 208 | void **pslot; |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 209 | |
Christoph Lameter | 6c5240a | 2006-06-23 02:03:37 -0700 | [diff] [blame] | 210 | if (!mapping) { |
Christoph Lameter | 0e8c7d0 | 2007-04-23 14:41:09 -0700 | [diff] [blame] | 211 | /* Anonymous page without mapping */ |
Christoph Lameter | 6c5240a | 2006-06-23 02:03:37 -0700 | [diff] [blame] | 212 | if (page_count(page) != 1) |
| 213 | return -EAGAIN; |
| 214 | return 0; |
| 215 | } |
| 216 | |
Nick Piggin | 19fd623 | 2008-07-25 19:45:32 -0700 | [diff] [blame] | 217 | spin_lock_irq(&mapping->tree_lock); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 218 | |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 219 | pslot = radix_tree_lookup_slot(&mapping->page_tree, |
| 220 | page_index(page)); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 221 | |
Johannes Weiner | edcf474 | 2009-09-21 17:02:59 -0700 | [diff] [blame] | 222 | expected_count = 2 + page_has_private(page); |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 223 | if (page_count(page) != expected_count || |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 224 | (struct page *)radix_tree_deref_slot(pslot) != page) { |
Nick Piggin | 19fd623 | 2008-07-25 19:45:32 -0700 | [diff] [blame] | 225 | spin_unlock_irq(&mapping->tree_lock); |
Christoph Lameter | e23ca00 | 2006-04-10 22:52:57 -0700 | [diff] [blame] | 226 | return -EAGAIN; |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 229 | if (!page_freeze_refs(page, expected_count)) { |
Nick Piggin | 19fd623 | 2008-07-25 19:45:32 -0700 | [diff] [blame] | 230 | spin_unlock_irq(&mapping->tree_lock); |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 231 | return -EAGAIN; |
| 232 | } |
| 233 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 234 | /* |
| 235 | * Now we know that no one else is looking at the page. |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 236 | */ |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 237 | get_page(newpage); /* add cache reference */ |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 238 | if (PageSwapCache(page)) { |
| 239 | SetPageSwapCache(newpage); |
| 240 | set_page_private(newpage, page_private(page)); |
| 241 | } |
| 242 | |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 243 | radix_tree_replace_slot(pslot, newpage); |
| 244 | |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 245 | page_unfreeze_refs(page, expected_count); |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 246 | /* |
| 247 | * Drop cache reference from old page. |
| 248 | * We know this isn't the last reference. |
| 249 | */ |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 250 | __put_page(page); |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 251 | |
Christoph Lameter | 0e8c7d0 | 2007-04-23 14:41:09 -0700 | [diff] [blame] | 252 | /* |
| 253 | * If moved to a different zone then also account |
| 254 | * the page for that zone. Other VM counters will be |
| 255 | * taken care of when we establish references to the |
| 256 | * new page and drop references to the old page. |
| 257 | * |
| 258 | * Note that anonymous pages are accounted for |
| 259 | * via NR_FILE_PAGES and NR_ANON_PAGES if they |
| 260 | * are mapped to swap space. |
| 261 | */ |
| 262 | __dec_zone_page_state(page, NR_FILE_PAGES); |
| 263 | __inc_zone_page_state(newpage, NR_FILE_PAGES); |
KOSAKI Motohiro | 4b02108 | 2009-09-21 17:01:33 -0700 | [diff] [blame] | 264 | if (PageSwapBacked(page)) { |
| 265 | __dec_zone_page_state(page, NR_SHMEM); |
| 266 | __inc_zone_page_state(newpage, NR_SHMEM); |
| 267 | } |
Nick Piggin | 19fd623 | 2008-07-25 19:45:32 -0700 | [diff] [blame] | 268 | spin_unlock_irq(&mapping->tree_lock); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 269 | |
| 270 | return 0; |
| 271 | } |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 272 | |
| 273 | /* |
| 274 | * Copy the page to its new location |
| 275 | */ |
Christoph Lameter | e7340f7 | 2006-06-23 02:03:29 -0700 | [diff] [blame] | 276 | static void migrate_page_copy(struct page *newpage, struct page *page) |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 277 | { |
KAMEZAWA Hiroyuki | b7abea9 | 2008-10-18 20:28:09 -0700 | [diff] [blame] | 278 | int anon; |
| 279 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 280 | copy_highpage(newpage, page); |
| 281 | |
| 282 | if (PageError(page)) |
| 283 | SetPageError(newpage); |
| 284 | if (PageReferenced(page)) |
| 285 | SetPageReferenced(newpage); |
| 286 | if (PageUptodate(page)) |
| 287 | SetPageUptodate(newpage); |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 288 | if (TestClearPageActive(page)) { |
| 289 | VM_BUG_ON(PageUnevictable(page)); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 290 | SetPageActive(newpage); |
Lee Schermerhorn | 418b27e | 2009-12-14 17:59:54 -0800 | [diff] [blame] | 291 | } else if (TestClearPageUnevictable(page)) |
| 292 | SetPageUnevictable(newpage); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 293 | if (PageChecked(page)) |
| 294 | SetPageChecked(newpage); |
| 295 | if (PageMappedToDisk(page)) |
| 296 | SetPageMappedToDisk(newpage); |
| 297 | |
| 298 | if (PageDirty(page)) { |
| 299 | clear_page_dirty_for_io(page); |
Nick Piggin | 3a902c5 | 2008-04-30 00:55:16 -0700 | [diff] [blame] | 300 | /* |
| 301 | * Want to mark the page and the radix tree as dirty, and |
| 302 | * redo the accounting that clear_page_dirty_for_io undid, |
| 303 | * but we can't use set_page_dirty because that function |
| 304 | * is actually a signal that all of the page has become dirty. |
| 305 | * Wheras only part of our page may be dirty. |
| 306 | */ |
| 307 | __set_page_dirty_nobuffers(newpage); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 308 | } |
| 309 | |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 310 | mlock_migrate_page(newpage, page); |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 311 | ksm_migrate_page(newpage, page); |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 312 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 313 | ClearPageSwapCache(page); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 314 | ClearPagePrivate(page); |
| 315 | set_page_private(page, 0); |
KAMEZAWA Hiroyuki | b7abea9 | 2008-10-18 20:28:09 -0700 | [diff] [blame] | 316 | /* page->mapping contains a flag for PageAnon() */ |
| 317 | anon = PageAnon(page); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 318 | page->mapping = NULL; |
| 319 | |
| 320 | /* |
| 321 | * If any waiters have accumulated on the new page then |
| 322 | * wake them up. |
| 323 | */ |
| 324 | if (PageWriteback(newpage)) |
| 325 | end_page_writeback(newpage); |
| 326 | } |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 327 | |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 328 | /************************************************************ |
| 329 | * Migration functions |
| 330 | ***********************************************************/ |
| 331 | |
| 332 | /* Always fail migration. Used for mappings that are not movable */ |
Christoph Lameter | 2d1db3b | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 333 | int fail_migrate_page(struct address_space *mapping, |
| 334 | struct page *newpage, struct page *page) |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 335 | { |
| 336 | return -EIO; |
| 337 | } |
| 338 | EXPORT_SYMBOL(fail_migrate_page); |
| 339 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 340 | /* |
| 341 | * Common logic to directly migrate a single page suitable for |
David Howells | 266cf65 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 342 | * pages that do not use PagePrivate/PagePrivate2. |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 343 | * |
| 344 | * Pages are locked upon entry and exit. |
| 345 | */ |
Christoph Lameter | 2d1db3b | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 346 | int migrate_page(struct address_space *mapping, |
| 347 | struct page *newpage, struct page *page) |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 348 | { |
| 349 | int rc; |
| 350 | |
| 351 | BUG_ON(PageWriteback(page)); /* Writeback must be complete */ |
| 352 | |
Christoph Lameter | 2d1db3b | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 353 | rc = migrate_page_move_mapping(mapping, newpage, page); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 354 | |
| 355 | if (rc) |
| 356 | return rc; |
| 357 | |
| 358 | migrate_page_copy(newpage, page); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 359 | return 0; |
| 360 | } |
| 361 | EXPORT_SYMBOL(migrate_page); |
| 362 | |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 363 | #ifdef CONFIG_BLOCK |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 364 | /* |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 365 | * Migration function for pages with buffers. This function can only be used |
| 366 | * if the underlying filesystem guarantees that no other references to "page" |
| 367 | * exist. |
| 368 | */ |
Christoph Lameter | 2d1db3b | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 369 | int buffer_migrate_page(struct address_space *mapping, |
| 370 | struct page *newpage, struct page *page) |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 371 | { |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 372 | struct buffer_head *bh, *head; |
| 373 | int rc; |
| 374 | |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 375 | if (!page_has_buffers(page)) |
Christoph Lameter | 2d1db3b | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 376 | return migrate_page(mapping, newpage, page); |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 377 | |
| 378 | head = page_buffers(page); |
| 379 | |
Christoph Lameter | 2d1db3b | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 380 | rc = migrate_page_move_mapping(mapping, newpage, page); |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 381 | |
| 382 | if (rc) |
| 383 | return rc; |
| 384 | |
| 385 | bh = head; |
| 386 | do { |
| 387 | get_bh(bh); |
| 388 | lock_buffer(bh); |
| 389 | bh = bh->b_this_page; |
| 390 | |
| 391 | } while (bh != head); |
| 392 | |
| 393 | ClearPagePrivate(page); |
| 394 | set_page_private(newpage, page_private(page)); |
| 395 | set_page_private(page, 0); |
| 396 | put_page(page); |
| 397 | get_page(newpage); |
| 398 | |
| 399 | bh = head; |
| 400 | do { |
| 401 | set_bh_page(bh, newpage, bh_offset(bh)); |
| 402 | bh = bh->b_this_page; |
| 403 | |
| 404 | } while (bh != head); |
| 405 | |
| 406 | SetPagePrivate(newpage); |
| 407 | |
| 408 | migrate_page_copy(newpage, page); |
| 409 | |
| 410 | bh = head; |
| 411 | do { |
| 412 | unlock_buffer(bh); |
| 413 | put_bh(bh); |
| 414 | bh = bh->b_this_page; |
| 415 | |
| 416 | } while (bh != head); |
| 417 | |
| 418 | return 0; |
| 419 | } |
| 420 | EXPORT_SYMBOL(buffer_migrate_page); |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 421 | #endif |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 422 | |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 423 | /* |
| 424 | * Writeback a page to clean the dirty state |
| 425 | */ |
| 426 | static int writeout(struct address_space *mapping, struct page *page) |
| 427 | { |
| 428 | struct writeback_control wbc = { |
| 429 | .sync_mode = WB_SYNC_NONE, |
| 430 | .nr_to_write = 1, |
| 431 | .range_start = 0, |
| 432 | .range_end = LLONG_MAX, |
| 433 | .nonblocking = 1, |
| 434 | .for_reclaim = 1 |
| 435 | }; |
| 436 | int rc; |
| 437 | |
| 438 | if (!mapping->a_ops->writepage) |
| 439 | /* No write method for the address space */ |
| 440 | return -EINVAL; |
| 441 | |
| 442 | if (!clear_page_dirty_for_io(page)) |
| 443 | /* Someone else already triggered a write */ |
| 444 | return -EAGAIN; |
| 445 | |
| 446 | /* |
| 447 | * A dirty page may imply that the underlying filesystem has |
| 448 | * the page on some queue. So the page must be clean for |
| 449 | * migration. Writeout may mean we loose the lock and the |
| 450 | * page state is no longer what we checked for earlier. |
| 451 | * At this point we know that the migration attempt cannot |
| 452 | * be successful. |
| 453 | */ |
| 454 | remove_migration_ptes(page, page); |
| 455 | |
| 456 | rc = mapping->a_ops->writepage(page, &wbc); |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 457 | |
| 458 | if (rc != AOP_WRITEPAGE_ACTIVATE) |
| 459 | /* unlocked. Relock */ |
| 460 | lock_page(page); |
| 461 | |
Hugh Dickins | bda8550 | 2008-11-19 15:36:36 -0800 | [diff] [blame] | 462 | return (rc < 0) ? -EIO : -EAGAIN; |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | /* |
| 466 | * Default handling if a filesystem does not provide a migration function. |
| 467 | */ |
Christoph Lameter | 8351a6e | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 468 | static int fallback_migrate_page(struct address_space *mapping, |
| 469 | struct page *newpage, struct page *page) |
| 470 | { |
Christoph Lameter | 04e62a2 | 2006-06-23 02:03:38 -0700 | [diff] [blame] | 471 | if (PageDirty(page)) |
| 472 | return writeout(mapping, page); |
Christoph Lameter | 8351a6e | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 473 | |
| 474 | /* |
| 475 | * Buffers may be managed in a filesystem specific way. |
| 476 | * We must have no buffers or drop them. |
| 477 | */ |
David Howells | 266cf65 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 478 | if (page_has_private(page) && |
Christoph Lameter | 8351a6e | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 479 | !try_to_release_page(page, GFP_KERNEL)) |
| 480 | return -EAGAIN; |
| 481 | |
| 482 | return migrate_page(mapping, newpage, page); |
| 483 | } |
| 484 | |
Christoph Lameter | 1d8b85c | 2006-06-23 02:03:28 -0700 | [diff] [blame] | 485 | /* |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 486 | * Move a page to a newly allocated page |
| 487 | * The page is locked and all ptes have been successfully removed. |
| 488 | * |
| 489 | * The new page will have replaced the old page if this function |
| 490 | * is successful. |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 491 | * |
| 492 | * Return value: |
| 493 | * < 0 - error code |
| 494 | * == 0 - success |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 495 | */ |
| 496 | static int move_to_new_page(struct page *newpage, struct page *page) |
| 497 | { |
| 498 | struct address_space *mapping; |
| 499 | int rc; |
| 500 | |
| 501 | /* |
| 502 | * Block others from accessing the page when we get around to |
| 503 | * establishing additional references. We are the only one |
| 504 | * holding a reference to the new page at this point. |
| 505 | */ |
Nick Piggin | 529ae9a | 2008-08-02 12:01:03 +0200 | [diff] [blame] | 506 | if (!trylock_page(newpage)) |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 507 | BUG(); |
| 508 | |
| 509 | /* Prepare mapping for the new page.*/ |
| 510 | newpage->index = page->index; |
| 511 | newpage->mapping = page->mapping; |
Rik van Riel | b2e1853 | 2008-10-18 20:26:30 -0700 | [diff] [blame] | 512 | if (PageSwapBacked(page)) |
| 513 | SetPageSwapBacked(newpage); |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 514 | |
| 515 | mapping = page_mapping(page); |
| 516 | if (!mapping) |
| 517 | rc = migrate_page(mapping, newpage, page); |
| 518 | else if (mapping->a_ops->migratepage) |
| 519 | /* |
| 520 | * Most pages have a mapping and most filesystems |
| 521 | * should provide a migration function. Anonymous |
| 522 | * pages are part of swap space which also has its |
| 523 | * own migration function. This is the most common |
| 524 | * path for page migration. |
| 525 | */ |
| 526 | rc = mapping->a_ops->migratepage(mapping, |
| 527 | newpage, page); |
| 528 | else |
| 529 | rc = fallback_migrate_page(mapping, newpage, page); |
| 530 | |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 531 | if (!rc) |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 532 | remove_migration_ptes(page, newpage); |
Hugh Dickins | e9995ef | 2009-12-14 17:59:31 -0800 | [diff] [blame] | 533 | else |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 534 | newpage->mapping = NULL; |
| 535 | |
| 536 | unlock_page(newpage); |
| 537 | |
| 538 | return rc; |
| 539 | } |
| 540 | |
| 541 | /* |
| 542 | * Obtain the lock on page, remove all ptes and migrate the page |
| 543 | * to the newly allocated page in newpage. |
| 544 | */ |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 545 | static int unmap_and_move(new_page_t get_new_page, unsigned long private, |
Hugh Dickins | 62b61f6 | 2009-12-14 17:59:33 -0800 | [diff] [blame] | 546 | struct page *page, int force, int offlining) |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 547 | { |
| 548 | int rc = 0; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 549 | int *result = NULL; |
| 550 | struct page *newpage = get_new_page(page, private, &result); |
KAMEZAWA Hiroyuki | 989f89c | 2007-08-30 23:56:21 -0700 | [diff] [blame] | 551 | int rcu_locked = 0; |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 552 | int charge = 0; |
KAMEZAWA Hiroyuki | e00e431 | 2009-11-11 14:26:26 -0800 | [diff] [blame] | 553 | struct mem_cgroup *mem = NULL; |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 554 | |
| 555 | if (!newpage) |
| 556 | return -ENOMEM; |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 557 | |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 558 | if (page_count(page) == 1) { |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 559 | /* page was freed from under us. So we are done. */ |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 560 | goto move_newpage; |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 561 | } |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 562 | |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 563 | /* prepare cgroup just returns 0 or -ENOMEM */ |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 564 | rc = -EAGAIN; |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 565 | |
Nick Piggin | 529ae9a | 2008-08-02 12:01:03 +0200 | [diff] [blame] | 566 | if (!trylock_page(page)) { |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 567 | if (!force) |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 568 | goto move_newpage; |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 569 | lock_page(page); |
| 570 | } |
| 571 | |
Hugh Dickins | 62b61f6 | 2009-12-14 17:59:33 -0800 | [diff] [blame] | 572 | /* |
| 573 | * Only memory hotplug's offline_pages() caller has locked out KSM, |
| 574 | * and can safely migrate a KSM page. The other cases have skipped |
| 575 | * PageKsm along with PageReserved - but it is only now when we have |
| 576 | * the page lock that we can be certain it will not go KSM beneath us |
| 577 | * (KSM will not upgrade a page from PageAnon to PageKsm when it sees |
| 578 | * its pagecount raised, but only here do we take the page lock which |
| 579 | * serializes that). |
| 580 | */ |
| 581 | if (PageKsm(page) && !offlining) { |
| 582 | rc = -EBUSY; |
| 583 | goto unlock; |
| 584 | } |
| 585 | |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 586 | /* charge against new page */ |
| 587 | charge = mem_cgroup_prepare_migration(page, &mem); |
| 588 | if (charge == -ENOMEM) { |
| 589 | rc = -ENOMEM; |
| 590 | goto unlock; |
| 591 | } |
| 592 | BUG_ON(charge); |
| 593 | |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 594 | if (PageWriteback(page)) { |
| 595 | if (!force) |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 596 | goto uncharge; |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 597 | wait_on_page_writeback(page); |
| 598 | } |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 599 | /* |
KAMEZAWA Hiroyuki | dc386d4 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 600 | * By try_to_unmap(), page->mapcount goes down to 0 here. In this case, |
| 601 | * we cannot notice that anon_vma is freed while we migrates a page. |
| 602 | * This rcu_read_lock() delays freeing anon_vma pointer until the end |
| 603 | * of migration. File cache pages are no problem because of page_lock() |
KAMEZAWA Hiroyuki | 989f89c | 2007-08-30 23:56:21 -0700 | [diff] [blame] | 604 | * File Caches may use write_page() or lock_page() in migration, then, |
| 605 | * just care Anon page here. |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 606 | */ |
KAMEZAWA Hiroyuki | 989f89c | 2007-08-30 23:56:21 -0700 | [diff] [blame] | 607 | if (PageAnon(page)) { |
| 608 | rcu_read_lock(); |
| 609 | rcu_locked = 1; |
| 610 | } |
Shaohua Li | 62e1c55 | 2008-02-04 22:29:33 -0800 | [diff] [blame] | 611 | |
KAMEZAWA Hiroyuki | dc386d4 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 612 | /* |
Shaohua Li | 62e1c55 | 2008-02-04 22:29:33 -0800 | [diff] [blame] | 613 | * Corner case handling: |
| 614 | * 1. When a new swap-cache page is read into, it is added to the LRU |
| 615 | * and treated as swapcache but it has no rmap yet. |
| 616 | * Calling try_to_unmap() against a page->mapping==NULL page will |
| 617 | * trigger a BUG. So handle it here. |
| 618 | * 2. An orphaned page (see truncate_complete_page) might have |
| 619 | * fs-private metadata. The page can be picked up due to memory |
| 620 | * offlining. Everywhere else except page reclaim, the page is |
| 621 | * invisible to the vm, so the page can not be migrated. So try to |
| 622 | * free the metadata, so the page can be freed. |
KAMEZAWA Hiroyuki | dc386d4 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 623 | */ |
Shaohua Li | 62e1c55 | 2008-02-04 22:29:33 -0800 | [diff] [blame] | 624 | if (!page->mapping) { |
David Howells | 266cf65 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 625 | if (!PageAnon(page) && page_has_private(page)) { |
Shaohua Li | 62e1c55 | 2008-02-04 22:29:33 -0800 | [diff] [blame] | 626 | /* |
| 627 | * Go direct to try_to_free_buffers() here because |
| 628 | * a) that's what try_to_release_page() would do anyway |
| 629 | * b) we may be under rcu_read_lock() here, so we can't |
| 630 | * use GFP_KERNEL which is what try_to_release_page() |
| 631 | * needs to be effective. |
| 632 | */ |
| 633 | try_to_free_buffers(page); |
Shaohua Li | abfc348 | 2009-09-21 17:01:19 -0700 | [diff] [blame] | 634 | goto rcu_unlock; |
Shaohua Li | 62e1c55 | 2008-02-04 22:29:33 -0800 | [diff] [blame] | 635 | } |
Shaohua Li | abfc348 | 2009-09-21 17:01:19 -0700 | [diff] [blame] | 636 | goto skip_unmap; |
Shaohua Li | 62e1c55 | 2008-02-04 22:29:33 -0800 | [diff] [blame] | 637 | } |
| 638 | |
KAMEZAWA Hiroyuki | dc386d4 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 639 | /* Establish migration ptes or remove ptes */ |
Andi Kleen | 14fa31b | 2009-09-16 11:50:10 +0200 | [diff] [blame] | 640 | try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS); |
KAMEZAWA Hiroyuki | dc386d4 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 641 | |
Shaohua Li | abfc348 | 2009-09-21 17:01:19 -0700 | [diff] [blame] | 642 | skip_unmap: |
Christoph Lameter | e6a1530 | 2006-06-25 05:46:49 -0700 | [diff] [blame] | 643 | if (!page_mapped(page)) |
| 644 | rc = move_to_new_page(newpage, page); |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 645 | |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 646 | if (rc) |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 647 | remove_migration_ptes(page, page); |
KAMEZAWA Hiroyuki | dc386d4 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 648 | rcu_unlock: |
KAMEZAWA Hiroyuki | 989f89c | 2007-08-30 23:56:21 -0700 | [diff] [blame] | 649 | if (rcu_locked) |
| 650 | rcu_read_unlock(); |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 651 | uncharge: |
| 652 | if (!charge) |
| 653 | mem_cgroup_end_migration(mem, page, newpage); |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 654 | unlock: |
| 655 | unlock_page(page); |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 656 | |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 657 | if (rc != -EAGAIN) { |
Christoph Lameter | aaa994b | 2006-06-23 02:03:52 -0700 | [diff] [blame] | 658 | /* |
| 659 | * A page that has been migrated has all references |
| 660 | * removed and will be freed. A page that has not been |
| 661 | * migrated will have kepts its references and be |
| 662 | * restored. |
| 663 | */ |
| 664 | list_del(&page->lru); |
KOSAKI Motohiro | a731286 | 2009-09-21 17:01:37 -0700 | [diff] [blame] | 665 | dec_zone_page_state(page, NR_ISOLATED_ANON + |
Johannes Weiner | 6c0b135 | 2009-09-21 17:02:59 -0700 | [diff] [blame] | 666 | page_is_file_cache(page)); |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 667 | putback_lru_page(page); |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 668 | } |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 669 | |
| 670 | move_newpage: |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 671 | |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 672 | /* |
| 673 | * Move the new page to the LRU. If migration was not successful |
| 674 | * then this will free the page. |
| 675 | */ |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 676 | putback_lru_page(newpage); |
| 677 | |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 678 | if (result) { |
| 679 | if (rc) |
| 680 | *result = rc; |
| 681 | else |
| 682 | *result = page_to_nid(newpage); |
| 683 | } |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 684 | return rc; |
| 685 | } |
| 686 | |
| 687 | /* |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 688 | * migrate_pages |
| 689 | * |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 690 | * The function takes one list of pages to migrate and a function |
| 691 | * that determines from the page to be migrated and the private data |
| 692 | * the target of the move and allocates the page. |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 693 | * |
| 694 | * The function returns after 10 attempts or if no pages |
| 695 | * are movable anymore because to has become empty |
Christoph Lameter | aaa994b | 2006-06-23 02:03:52 -0700 | [diff] [blame] | 696 | * or no retryable pages exist anymore. All pages will be |
Gabriel Craciunescu | e9534b3 | 2007-10-20 02:13:26 +0200 | [diff] [blame] | 697 | * returned to the LRU or freed. |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 698 | * |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 699 | * Return: Number of pages not migrated or error code. |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 700 | */ |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 701 | int migrate_pages(struct list_head *from, |
Hugh Dickins | 62b61f6 | 2009-12-14 17:59:33 -0800 | [diff] [blame] | 702 | new_page_t get_new_page, unsigned long private, int offlining) |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 703 | { |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 704 | int retry = 1; |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 705 | int nr_failed = 0; |
| 706 | int pass = 0; |
| 707 | struct page *page; |
| 708 | struct page *page2; |
| 709 | int swapwrite = current->flags & PF_SWAPWRITE; |
| 710 | int rc; |
| 711 | |
| 712 | if (!swapwrite) |
| 713 | current->flags |= PF_SWAPWRITE; |
| 714 | |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 715 | for(pass = 0; pass < 10 && retry; pass++) { |
| 716 | retry = 0; |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 717 | |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 718 | list_for_each_entry_safe(page, page2, from, lru) { |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 719 | cond_resched(); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 720 | |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 721 | rc = unmap_and_move(get_new_page, private, |
Hugh Dickins | 62b61f6 | 2009-12-14 17:59:33 -0800 | [diff] [blame] | 722 | page, pass > 2, offlining); |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 723 | |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 724 | switch(rc) { |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 725 | case -ENOMEM: |
| 726 | goto out; |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 727 | case -EAGAIN: |
Christoph Lameter | 2d1db3b | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 728 | retry++; |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 729 | break; |
| 730 | case 0: |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 731 | break; |
| 732 | default: |
Christoph Lameter | 2d1db3b | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 733 | /* Permanent failure */ |
Christoph Lameter | 2d1db3b | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 734 | nr_failed++; |
Christoph Lameter | e24f0b8 | 2006-06-23 02:03:51 -0700 | [diff] [blame] | 735 | break; |
Christoph Lameter | 2d1db3b | 2006-06-23 02:03:33 -0700 | [diff] [blame] | 736 | } |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 737 | } |
| 738 | } |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 739 | rc = 0; |
| 740 | out: |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 741 | if (!swapwrite) |
| 742 | current->flags &= ~PF_SWAPWRITE; |
| 743 | |
Christoph Lameter | aaa994b | 2006-06-23 02:03:52 -0700 | [diff] [blame] | 744 | putback_lru_pages(from); |
Christoph Lameter | 95a402c | 2006-06-23 02:03:53 -0700 | [diff] [blame] | 745 | |
| 746 | if (rc) |
| 747 | return rc; |
| 748 | |
Christoph Lameter | b20a350 | 2006-03-22 00:09:12 -0800 | [diff] [blame] | 749 | return nr_failed + retry; |
| 750 | } |
| 751 | |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 752 | #ifdef CONFIG_NUMA |
| 753 | /* |
| 754 | * Move a list of individual pages |
| 755 | */ |
| 756 | struct page_to_node { |
| 757 | unsigned long addr; |
| 758 | struct page *page; |
| 759 | int node; |
| 760 | int status; |
| 761 | }; |
| 762 | |
| 763 | static struct page *new_page_node(struct page *p, unsigned long private, |
| 764 | int **result) |
| 765 | { |
| 766 | struct page_to_node *pm = (struct page_to_node *)private; |
| 767 | |
| 768 | while (pm->node != MAX_NUMNODES && pm->page != p) |
| 769 | pm++; |
| 770 | |
| 771 | if (pm->node == MAX_NUMNODES) |
| 772 | return NULL; |
| 773 | |
| 774 | *result = &pm->status; |
| 775 | |
Mel Gorman | 6484eb3 | 2009-06-16 15:31:54 -0700 | [diff] [blame] | 776 | return alloc_pages_exact_node(pm->node, |
Mel Gorman | 769848c | 2007-07-17 04:03:05 -0700 | [diff] [blame] | 777 | GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0); |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | /* |
| 781 | * Move a set of pages as indicated in the pm array. The addr |
| 782 | * field must be set to the virtual address of the page to be moved |
| 783 | * and the node number must contain a valid target node. |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 784 | * The pm array ends with node = MAX_NUMNODES. |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 785 | */ |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 786 | static int do_move_page_to_node_array(struct mm_struct *mm, |
| 787 | struct page_to_node *pm, |
| 788 | int migrate_all) |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 789 | { |
| 790 | int err; |
| 791 | struct page_to_node *pp; |
| 792 | LIST_HEAD(pagelist); |
| 793 | |
| 794 | down_read(&mm->mmap_sem); |
| 795 | |
| 796 | /* |
| 797 | * Build a list of pages to migrate |
| 798 | */ |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 799 | for (pp = pm; pp->node != MAX_NUMNODES; pp++) { |
| 800 | struct vm_area_struct *vma; |
| 801 | struct page *page; |
| 802 | |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 803 | err = -EFAULT; |
| 804 | vma = find_vma(mm, pp->addr); |
Christoph Lameter | 0dc952d | 2007-03-05 00:30:33 -0800 | [diff] [blame] | 805 | if (!vma || !vma_migratable(vma)) |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 806 | goto set_status; |
| 807 | |
| 808 | page = follow_page(vma, pp->addr, FOLL_GET); |
Linus Torvalds | 89f5b7d | 2008-06-20 11:18:25 -0700 | [diff] [blame] | 809 | |
| 810 | err = PTR_ERR(page); |
| 811 | if (IS_ERR(page)) |
| 812 | goto set_status; |
| 813 | |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 814 | err = -ENOENT; |
| 815 | if (!page) |
| 816 | goto set_status; |
| 817 | |
Hugh Dickins | 62b61f6 | 2009-12-14 17:59:33 -0800 | [diff] [blame] | 818 | /* Use PageReserved to check for zero page */ |
| 819 | if (PageReserved(page) || PageKsm(page)) |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 820 | goto put_and_set; |
| 821 | |
| 822 | pp->page = page; |
| 823 | err = page_to_nid(page); |
| 824 | |
| 825 | if (err == pp->node) |
| 826 | /* |
| 827 | * Node already in the right place |
| 828 | */ |
| 829 | goto put_and_set; |
| 830 | |
| 831 | err = -EACCES; |
| 832 | if (page_mapcount(page) > 1 && |
| 833 | !migrate_all) |
| 834 | goto put_and_set; |
| 835 | |
Nick Piggin | 62695a8 | 2008-10-18 20:26:09 -0700 | [diff] [blame] | 836 | err = isolate_lru_page(page); |
KOSAKI Motohiro | 6d9c285 | 2009-12-14 17:58:11 -0800 | [diff] [blame] | 837 | if (!err) { |
Nick Piggin | 62695a8 | 2008-10-18 20:26:09 -0700 | [diff] [blame] | 838 | list_add_tail(&page->lru, &pagelist); |
KOSAKI Motohiro | 6d9c285 | 2009-12-14 17:58:11 -0800 | [diff] [blame] | 839 | inc_zone_page_state(page, NR_ISOLATED_ANON + |
| 840 | page_is_file_cache(page)); |
| 841 | } |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 842 | put_and_set: |
| 843 | /* |
| 844 | * Either remove the duplicate refcount from |
| 845 | * isolate_lru_page() or drop the page ref if it was |
| 846 | * not isolated. |
| 847 | */ |
| 848 | put_page(page); |
| 849 | set_status: |
| 850 | pp->status = err; |
| 851 | } |
| 852 | |
Brice Goglin | e78bbfa | 2008-10-18 20:27:15 -0700 | [diff] [blame] | 853 | err = 0; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 854 | if (!list_empty(&pagelist)) |
| 855 | err = migrate_pages(&pagelist, new_page_node, |
Hugh Dickins | 62b61f6 | 2009-12-14 17:59:33 -0800 | [diff] [blame] | 856 | (unsigned long)pm, 0); |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 857 | |
| 858 | up_read(&mm->mmap_sem); |
| 859 | return err; |
| 860 | } |
| 861 | |
| 862 | /* |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 863 | * Migrate an array of page address onto an array of nodes and fill |
| 864 | * the corresponding array of status. |
| 865 | */ |
| 866 | static int do_pages_move(struct mm_struct *mm, struct task_struct *task, |
| 867 | unsigned long nr_pages, |
| 868 | const void __user * __user *pages, |
| 869 | const int __user *nodes, |
| 870 | int __user *status, int flags) |
| 871 | { |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 872 | struct page_to_node *pm; |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 873 | nodemask_t task_nodes; |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 874 | unsigned long chunk_nr_pages; |
| 875 | unsigned long chunk_start; |
| 876 | int err; |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 877 | |
| 878 | task_nodes = cpuset_mems_allowed(task); |
| 879 | |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 880 | err = -ENOMEM; |
| 881 | pm = (struct page_to_node *)__get_free_page(GFP_KERNEL); |
| 882 | if (!pm) |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 883 | goto out; |
Brice Goglin | 35282a2 | 2009-06-16 15:32:43 -0700 | [diff] [blame] | 884 | |
| 885 | migrate_prep(); |
| 886 | |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 887 | /* |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 888 | * Store a chunk of page_to_node array in a page, |
| 889 | * but keep the last one as a marker |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 890 | */ |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 891 | chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1; |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 892 | |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 893 | for (chunk_start = 0; |
| 894 | chunk_start < nr_pages; |
| 895 | chunk_start += chunk_nr_pages) { |
| 896 | int j; |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 897 | |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 898 | if (chunk_start + chunk_nr_pages > nr_pages) |
| 899 | chunk_nr_pages = nr_pages - chunk_start; |
| 900 | |
| 901 | /* fill the chunk pm with addrs and nodes from user-space */ |
| 902 | for (j = 0; j < chunk_nr_pages; j++) { |
| 903 | const void __user *p; |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 904 | int node; |
| 905 | |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 906 | err = -EFAULT; |
| 907 | if (get_user(p, pages + j + chunk_start)) |
| 908 | goto out_pm; |
| 909 | pm[j].addr = (unsigned long) p; |
| 910 | |
| 911 | if (get_user(node, nodes + j + chunk_start)) |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 912 | goto out_pm; |
| 913 | |
| 914 | err = -ENODEV; |
Linus Torvalds | 6f5a55f | 2010-02-05 16:16:50 -0800 | [diff] [blame] | 915 | if (node < 0 || node >= MAX_NUMNODES) |
| 916 | goto out_pm; |
| 917 | |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 918 | if (!node_state(node, N_HIGH_MEMORY)) |
| 919 | goto out_pm; |
| 920 | |
| 921 | err = -EACCES; |
| 922 | if (!node_isset(node, task_nodes)) |
| 923 | goto out_pm; |
| 924 | |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 925 | pm[j].node = node; |
| 926 | } |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 927 | |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 928 | /* End marker for this chunk */ |
| 929 | pm[chunk_nr_pages].node = MAX_NUMNODES; |
| 930 | |
| 931 | /* Migrate this chunk */ |
| 932 | err = do_move_page_to_node_array(mm, pm, |
| 933 | flags & MPOL_MF_MOVE_ALL); |
| 934 | if (err < 0) |
| 935 | goto out_pm; |
| 936 | |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 937 | /* Return status information */ |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 938 | for (j = 0; j < chunk_nr_pages; j++) |
| 939 | if (put_user(pm[j].status, status + j + chunk_start)) { |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 940 | err = -EFAULT; |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 941 | goto out_pm; |
| 942 | } |
| 943 | } |
| 944 | err = 0; |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 945 | |
| 946 | out_pm: |
Brice Goglin | 3140a22 | 2009-01-06 14:38:57 -0800 | [diff] [blame] | 947 | free_page((unsigned long)pm); |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 948 | out: |
| 949 | return err; |
| 950 | } |
| 951 | |
| 952 | /* |
Brice Goglin | 2f007e7 | 2008-10-18 20:27:16 -0700 | [diff] [blame] | 953 | * 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] | 954 | */ |
Brice Goglin | 80bba12 | 2008-12-09 13:14:23 -0800 | [diff] [blame] | 955 | static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages, |
| 956 | const void __user **pages, int *status) |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 957 | { |
Brice Goglin | 2f007e7 | 2008-10-18 20:27:16 -0700 | [diff] [blame] | 958 | unsigned long i; |
Brice Goglin | 2f007e7 | 2008-10-18 20:27:16 -0700 | [diff] [blame] | 959 | |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 960 | down_read(&mm->mmap_sem); |
| 961 | |
Brice Goglin | 2f007e7 | 2008-10-18 20:27:16 -0700 | [diff] [blame] | 962 | for (i = 0; i < nr_pages; i++) { |
Brice Goglin | 80bba12 | 2008-12-09 13:14:23 -0800 | [diff] [blame] | 963 | unsigned long addr = (unsigned long)(*pages); |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 964 | struct vm_area_struct *vma; |
| 965 | struct page *page; |
KOSAKI Motohiro | c095adb | 2008-12-16 16:06:43 +0900 | [diff] [blame] | 966 | int err = -EFAULT; |
Brice Goglin | 2f007e7 | 2008-10-18 20:27:16 -0700 | [diff] [blame] | 967 | |
| 968 | vma = find_vma(mm, addr); |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 969 | if (!vma) |
| 970 | goto set_status; |
| 971 | |
Brice Goglin | 2f007e7 | 2008-10-18 20:27:16 -0700 | [diff] [blame] | 972 | page = follow_page(vma, addr, 0); |
Linus Torvalds | 89f5b7d | 2008-06-20 11:18:25 -0700 | [diff] [blame] | 973 | |
| 974 | err = PTR_ERR(page); |
| 975 | if (IS_ERR(page)) |
| 976 | goto set_status; |
| 977 | |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 978 | err = -ENOENT; |
| 979 | /* Use PageReserved to check for zero page */ |
Hugh Dickins | 62b61f6 | 2009-12-14 17:59:33 -0800 | [diff] [blame] | 980 | if (!page || PageReserved(page) || PageKsm(page)) |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 981 | goto set_status; |
| 982 | |
| 983 | err = page_to_nid(page); |
| 984 | set_status: |
Brice Goglin | 80bba12 | 2008-12-09 13:14:23 -0800 | [diff] [blame] | 985 | *status = err; |
| 986 | |
| 987 | pages++; |
| 988 | status++; |
| 989 | } |
| 990 | |
| 991 | up_read(&mm->mmap_sem); |
| 992 | } |
| 993 | |
| 994 | /* |
| 995 | * Determine the nodes of a user array of pages and store it in |
| 996 | * a user array of status. |
| 997 | */ |
| 998 | static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages, |
| 999 | const void __user * __user *pages, |
| 1000 | int __user *status) |
| 1001 | { |
| 1002 | #define DO_PAGES_STAT_CHUNK_NR 16 |
| 1003 | const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR]; |
| 1004 | int chunk_status[DO_PAGES_STAT_CHUNK_NR]; |
Brice Goglin | 80bba12 | 2008-12-09 13:14:23 -0800 | [diff] [blame] | 1005 | |
H. Peter Anvin | 87b8d1a | 2010-02-18 16:13:40 -0800 | [diff] [blame] | 1006 | while (nr_pages) { |
| 1007 | unsigned long chunk_nr; |
Brice Goglin | 80bba12 | 2008-12-09 13:14:23 -0800 | [diff] [blame] | 1008 | |
H. Peter Anvin | 87b8d1a | 2010-02-18 16:13:40 -0800 | [diff] [blame] | 1009 | chunk_nr = nr_pages; |
| 1010 | if (chunk_nr > DO_PAGES_STAT_CHUNK_NR) |
| 1011 | chunk_nr = DO_PAGES_STAT_CHUNK_NR; |
| 1012 | |
| 1013 | if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages))) |
| 1014 | break; |
Brice Goglin | 80bba12 | 2008-12-09 13:14:23 -0800 | [diff] [blame] | 1015 | |
| 1016 | do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status); |
| 1017 | |
H. Peter Anvin | 87b8d1a | 2010-02-18 16:13:40 -0800 | [diff] [blame] | 1018 | if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status))) |
| 1019 | break; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1020 | |
H. Peter Anvin | 87b8d1a | 2010-02-18 16:13:40 -0800 | [diff] [blame] | 1021 | pages += chunk_nr; |
| 1022 | status += chunk_nr; |
| 1023 | nr_pages -= chunk_nr; |
| 1024 | } |
| 1025 | return nr_pages ? -EFAULT : 0; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | /* |
| 1029 | * Move a list of pages in the address space of the currently executing |
| 1030 | * process. |
| 1031 | */ |
Heiko Carstens | 938bb9f | 2009-01-14 14:14:30 +0100 | [diff] [blame] | 1032 | SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages, |
| 1033 | const void __user * __user *, pages, |
| 1034 | const int __user *, nodes, |
| 1035 | int __user *, status, int, flags) |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1036 | { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1037 | const struct cred *cred = current_cred(), *tcred; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1038 | struct task_struct *task; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1039 | struct mm_struct *mm; |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1040 | int err; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1041 | |
| 1042 | /* Check flags */ |
| 1043 | if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL)) |
| 1044 | return -EINVAL; |
| 1045 | |
| 1046 | if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE)) |
| 1047 | return -EPERM; |
| 1048 | |
| 1049 | /* Find the mm_struct */ |
| 1050 | read_lock(&tasklist_lock); |
Pavel Emelyanov | 228ebcb | 2007-10-18 23:40:16 -0700 | [diff] [blame] | 1051 | task = pid ? find_task_by_vpid(pid) : current; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1052 | if (!task) { |
| 1053 | read_unlock(&tasklist_lock); |
| 1054 | return -ESRCH; |
| 1055 | } |
| 1056 | mm = get_task_mm(task); |
| 1057 | read_unlock(&tasklist_lock); |
| 1058 | |
| 1059 | if (!mm) |
| 1060 | return -EINVAL; |
| 1061 | |
| 1062 | /* |
| 1063 | * Check if this process has the right to modify the specified |
| 1064 | * process. The right exists if the process has administrative |
| 1065 | * capabilities, superuser privileges or the same |
| 1066 | * userid as the target process. |
| 1067 | */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1068 | rcu_read_lock(); |
| 1069 | tcred = __task_cred(task); |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 1070 | if (cred->euid != tcred->suid && cred->euid != tcred->uid && |
| 1071 | cred->uid != tcred->suid && cred->uid != tcred->uid && |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1072 | !capable(CAP_SYS_NICE)) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1073 | rcu_read_unlock(); |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1074 | err = -EPERM; |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1075 | goto out; |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1076 | } |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1077 | rcu_read_unlock(); |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1078 | |
David Quigley | 86c3a76 | 2006-06-23 02:04:02 -0700 | [diff] [blame] | 1079 | err = security_task_movememory(task); |
| 1080 | if (err) |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1081 | goto out; |
David Quigley | 86c3a76 | 2006-06-23 02:04:02 -0700 | [diff] [blame] | 1082 | |
Brice Goglin | 5e9a0f0 | 2008-10-18 20:27:17 -0700 | [diff] [blame] | 1083 | if (nodes) { |
| 1084 | err = do_pages_move(mm, task, nr_pages, pages, nodes, status, |
| 1085 | flags); |
| 1086 | } else { |
Brice Goglin | 2f007e7 | 2008-10-18 20:27:16 -0700 | [diff] [blame] | 1087 | err = do_pages_stat(mm, nr_pages, pages, status); |
Brice Goglin | 2f007e7 | 2008-10-18 20:27:16 -0700 | [diff] [blame] | 1088 | } |
David Quigley | 86c3a76 | 2006-06-23 02:04:02 -0700 | [diff] [blame] | 1089 | |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1090 | out: |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1091 | mmput(mm); |
| 1092 | return err; |
| 1093 | } |
Christoph Lameter | 742755a | 2006-06-23 02:03:55 -0700 | [diff] [blame] | 1094 | |
Christoph Lameter | 7b2259b | 2006-06-25 05:46:48 -0700 | [diff] [blame] | 1095 | /* |
| 1096 | * Call migration functions in the vma_ops that may prepare |
| 1097 | * memory in a vm for migration. migration functions may perform |
| 1098 | * the migration for vmas that do not have an underlying page struct. |
| 1099 | */ |
| 1100 | int migrate_vmas(struct mm_struct *mm, const nodemask_t *to, |
| 1101 | const nodemask_t *from, unsigned long flags) |
| 1102 | { |
| 1103 | struct vm_area_struct *vma; |
| 1104 | int err = 0; |
| 1105 | |
Daisuke Nishimura | 1001c9f | 2009-02-11 13:04:18 -0800 | [diff] [blame] | 1106 | for (vma = mm->mmap; vma && !err; vma = vma->vm_next) { |
Christoph Lameter | 7b2259b | 2006-06-25 05:46:48 -0700 | [diff] [blame] | 1107 | if (vma->vm_ops && vma->vm_ops->migrate) { |
| 1108 | err = vma->vm_ops->migrate(vma, to, from, flags); |
| 1109 | if (err) |
| 1110 | break; |
| 1111 | } |
| 1112 | } |
| 1113 | return err; |
| 1114 | } |
Gerald Schaefer | 83d1674 | 2008-07-23 21:28:22 -0700 | [diff] [blame] | 1115 | #endif |