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