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