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