]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - mm/migrate.c
[PATCH] page migration: use allocator function for migrate_pages()
[thirdparty/kernel/stable.git] / mm / migrate.c
CommitLineData
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>
12 * Christoph Lameter <clameter@sgi.com>
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
CL
21#include <linux/mm_inline.h>
22#include <linux/pagevec.h>
23#include <linux/rmap.h>
24#include <linux/topology.h>
25#include <linux/cpu.h>
26#include <linux/cpuset.h>
04e62a29 27#include <linux/writeback.h>
b20a3503
CL
28
29#include "internal.h"
30
b20a3503
CL
31#define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
32
33/*
34 * Isolate one page from the LRU lists. If successful put it onto
35 * the indicated list with elevated page count.
36 *
37 * Result:
38 * -EBUSY: page not on LRU list
39 * 0: page removed from LRU list and added to the specified list.
40 */
41int isolate_lru_page(struct page *page, struct list_head *pagelist)
42{
43 int ret = -EBUSY;
44
45 if (PageLRU(page)) {
46 struct zone *zone = page_zone(page);
47
48 spin_lock_irq(&zone->lru_lock);
49 if (PageLRU(page)) {
50 ret = 0;
51 get_page(page);
52 ClearPageLRU(page);
53 if (PageActive(page))
54 del_page_from_active_list(zone, page);
55 else
56 del_page_from_inactive_list(zone, page);
57 list_add_tail(&page->lru, pagelist);
58 }
59 spin_unlock_irq(&zone->lru_lock);
60 }
61 return ret;
62}
63
64/*
65 * migrate_prep() needs to be called after we have compiled the list of pages
66 * to be migrated using isolate_lru_page() but before we begin a series of calls
67 * to migrate_pages().
68 */
69int migrate_prep(void)
70{
b20a3503
CL
71 /*
72 * Clear the LRU lists so pages can be isolated.
73 * Note that pages may be moved off the LRU after we have
74 * drained them. Those pages will fail to migrate like other
75 * pages that may be busy.
76 */
77 lru_add_drain_all();
78
79 return 0;
80}
81
82static inline void move_to_lru(struct page *page)
83{
b20a3503
CL
84 if (PageActive(page)) {
85 /*
86 * lru_cache_add_active checks that
87 * the PG_active bit is off.
88 */
89 ClearPageActive(page);
90 lru_cache_add_active(page);
91 } else {
92 lru_cache_add(page);
93 }
94 put_page(page);
95}
96
97/*
98 * Add isolated pages on the list back to the LRU.
99 *
100 * returns the number of pages put back.
101 */
102int putback_lru_pages(struct list_head *l)
103{
104 struct page *page;
105 struct page *page2;
106 int count = 0;
107
108 list_for_each_entry_safe(page, page2, l, lru) {
e24f0b8f 109 list_del(&page->lru);
b20a3503
CL
110 move_to_lru(page);
111 count++;
112 }
113 return count;
114}
115
0697212a
CL
116static inline int is_swap_pte(pte_t pte)
117{
118 return !pte_none(pte) && !pte_present(pte) && !pte_file(pte);
119}
120
121/*
122 * Restore a potential migration pte to a working pte entry
123 */
04e62a29 124static void remove_migration_pte(struct vm_area_struct *vma,
0697212a
CL
125 struct page *old, struct page *new)
126{
127 struct mm_struct *mm = vma->vm_mm;
128 swp_entry_t entry;
129 pgd_t *pgd;
130 pud_t *pud;
131 pmd_t *pmd;
132 pte_t *ptep, pte;
133 spinlock_t *ptl;
04e62a29
CL
134 unsigned long addr = page_address_in_vma(new, vma);
135
136 if (addr == -EFAULT)
137 return;
0697212a
CL
138
139 pgd = pgd_offset(mm, addr);
140 if (!pgd_present(*pgd))
141 return;
142
143 pud = pud_offset(pgd, addr);
144 if (!pud_present(*pud))
145 return;
146
147 pmd = pmd_offset(pud, addr);
148 if (!pmd_present(*pmd))
149 return;
150
151 ptep = pte_offset_map(pmd, addr);
152
153 if (!is_swap_pte(*ptep)) {
154 pte_unmap(ptep);
155 return;
156 }
157
158 ptl = pte_lockptr(mm, pmd);
159 spin_lock(ptl);
160 pte = *ptep;
161 if (!is_swap_pte(pte))
162 goto out;
163
164 entry = pte_to_swp_entry(pte);
165
166 if (!is_migration_entry(entry) || migration_entry_to_page(entry) != old)
167 goto out;
168
0697212a
CL
169 get_page(new);
170 pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
171 if (is_write_migration_entry(entry))
172 pte = pte_mkwrite(pte);
173 set_pte_at(mm, addr, ptep, pte);
04e62a29
CL
174
175 if (PageAnon(new))
176 page_add_anon_rmap(new, vma, addr);
177 else
178 page_add_file_rmap(new);
179
180 /* No need to invalidate - it was non-present before */
181 update_mmu_cache(vma, addr, pte);
182 lazy_mmu_prot_update(pte);
183
0697212a
CL
184out:
185 pte_unmap_unlock(ptep, ptl);
186}
187
188/*
04e62a29
CL
189 * Note that remove_file_migration_ptes will only work on regular mappings,
190 * Nonlinear mappings do not use migration entries.
191 */
192static void remove_file_migration_ptes(struct page *old, struct page *new)
193{
194 struct vm_area_struct *vma;
195 struct address_space *mapping = page_mapping(new);
196 struct prio_tree_iter iter;
197 pgoff_t pgoff = new->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
198
199 if (!mapping)
200 return;
201
202 spin_lock(&mapping->i_mmap_lock);
203
204 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff)
205 remove_migration_pte(vma, old, new);
206
207 spin_unlock(&mapping->i_mmap_lock);
208}
209
210/*
0697212a
CL
211 * Must hold mmap_sem lock on at least one of the vmas containing
212 * the page so that the anon_vma cannot vanish.
213 */
04e62a29 214static void remove_anon_migration_ptes(struct page *old, struct page *new)
0697212a
CL
215{
216 struct anon_vma *anon_vma;
217 struct vm_area_struct *vma;
218 unsigned long mapping;
219
220 mapping = (unsigned long)new->mapping;
221
222 if (!mapping || (mapping & PAGE_MAPPING_ANON) == 0)
223 return;
224
225 /*
226 * We hold the mmap_sem lock. So no need to call page_lock_anon_vma.
227 */
228 anon_vma = (struct anon_vma *) (mapping - PAGE_MAPPING_ANON);
229 spin_lock(&anon_vma->lock);
230
231 list_for_each_entry(vma, &anon_vma->head, anon_vma_node)
04e62a29 232 remove_migration_pte(vma, old, new);
0697212a
CL
233
234 spin_unlock(&anon_vma->lock);
235}
236
04e62a29
CL
237/*
238 * Get rid of all migration entries and replace them by
239 * references to the indicated page.
240 */
241static void remove_migration_ptes(struct page *old, struct page *new)
242{
243 if (PageAnon(new))
244 remove_anon_migration_ptes(old, new);
245 else
246 remove_file_migration_ptes(old, new);
247}
248
0697212a
CL
249/*
250 * Something used the pte of a page under migration. We need to
251 * get to the page and wait until migration is finished.
252 * When we return from this function the fault will be retried.
253 *
254 * This function is called from do_swap_page().
255 */
256void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
257 unsigned long address)
258{
259 pte_t *ptep, pte;
260 spinlock_t *ptl;
261 swp_entry_t entry;
262 struct page *page;
263
264 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
265 pte = *ptep;
266 if (!is_swap_pte(pte))
267 goto out;
268
269 entry = pte_to_swp_entry(pte);
270 if (!is_migration_entry(entry))
271 goto out;
272
273 page = migration_entry_to_page(entry);
274
275 get_page(page);
276 pte_unmap_unlock(ptep, ptl);
277 wait_on_page_locked(page);
278 put_page(page);
279 return;
280out:
281 pte_unmap_unlock(ptep, ptl);
282}
283
b20a3503 284/*
c3fcf8a5 285 * Replace the page in the mapping.
5b5c7120
CL
286 *
287 * The number of remaining references must be:
288 * 1 for anonymous pages without a mapping
289 * 2 for pages with a mapping
290 * 3 for pages with a mapping and PagePrivate set.
b20a3503 291 */
2d1db3b1
CL
292static int migrate_page_move_mapping(struct address_space *mapping,
293 struct page *newpage, struct page *page)
b20a3503 294{
b20a3503
CL
295 struct page **radix_pointer;
296
6c5240ae
CL
297 if (!mapping) {
298 /* Anonymous page */
299 if (page_count(page) != 1)
300 return -EAGAIN;
301 return 0;
302 }
303
b20a3503
CL
304 write_lock_irq(&mapping->tree_lock);
305
306 radix_pointer = (struct page **)radix_tree_lookup_slot(
307 &mapping->page_tree,
308 page_index(page));
309
6c5240ae 310 if (page_count(page) != 2 + !!PagePrivate(page) ||
b20a3503
CL
311 *radix_pointer != page) {
312 write_unlock_irq(&mapping->tree_lock);
e23ca00b 313 return -EAGAIN;
b20a3503
CL
314 }
315
316 /*
317 * Now we know that no one else is looking at the page.
b20a3503
CL
318 */
319 get_page(newpage);
6c5240ae 320#ifdef CONFIG_SWAP
b20a3503
CL
321 if (PageSwapCache(page)) {
322 SetPageSwapCache(newpage);
323 set_page_private(newpage, page_private(page));
324 }
6c5240ae 325#endif
b20a3503
CL
326
327 *radix_pointer = newpage;
328 __put_page(page);
329 write_unlock_irq(&mapping->tree_lock);
330
331 return 0;
332}
b20a3503
CL
333
334/*
335 * Copy the page to its new location
336 */
e7340f73 337static void migrate_page_copy(struct page *newpage, struct page *page)
b20a3503
CL
338{
339 copy_highpage(newpage, page);
340
341 if (PageError(page))
342 SetPageError(newpage);
343 if (PageReferenced(page))
344 SetPageReferenced(newpage);
345 if (PageUptodate(page))
346 SetPageUptodate(newpage);
347 if (PageActive(page))
348 SetPageActive(newpage);
349 if (PageChecked(page))
350 SetPageChecked(newpage);
351 if (PageMappedToDisk(page))
352 SetPageMappedToDisk(newpage);
353
354 if (PageDirty(page)) {
355 clear_page_dirty_for_io(page);
356 set_page_dirty(newpage);
357 }
358
6c5240ae 359#ifdef CONFIG_SWAP
b20a3503 360 ClearPageSwapCache(page);
6c5240ae 361#endif
b20a3503
CL
362 ClearPageActive(page);
363 ClearPagePrivate(page);
364 set_page_private(page, 0);
365 page->mapping = NULL;
366
367 /*
368 * If any waiters have accumulated on the new page then
369 * wake them up.
370 */
371 if (PageWriteback(newpage))
372 end_page_writeback(newpage);
373}
b20a3503 374
1d8b85cc
CL
375/************************************************************
376 * Migration functions
377 ***********************************************************/
378
379/* Always fail migration. Used for mappings that are not movable */
2d1db3b1
CL
380int fail_migrate_page(struct address_space *mapping,
381 struct page *newpage, struct page *page)
1d8b85cc
CL
382{
383 return -EIO;
384}
385EXPORT_SYMBOL(fail_migrate_page);
386
b20a3503
CL
387/*
388 * Common logic to directly migrate a single page suitable for
389 * pages that do not use PagePrivate.
390 *
391 * Pages are locked upon entry and exit.
392 */
2d1db3b1
CL
393int migrate_page(struct address_space *mapping,
394 struct page *newpage, struct page *page)
b20a3503
CL
395{
396 int rc;
397
398 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
399
2d1db3b1 400 rc = migrate_page_move_mapping(mapping, newpage, page);
b20a3503
CL
401
402 if (rc)
403 return rc;
404
405 migrate_page_copy(newpage, page);
b20a3503
CL
406 return 0;
407}
408EXPORT_SYMBOL(migrate_page);
409
1d8b85cc
CL
410/*
411 * Migration function for pages with buffers. This function can only be used
412 * if the underlying filesystem guarantees that no other references to "page"
413 * exist.
414 */
2d1db3b1
CL
415int buffer_migrate_page(struct address_space *mapping,
416 struct page *newpage, struct page *page)
1d8b85cc 417{
1d8b85cc
CL
418 struct buffer_head *bh, *head;
419 int rc;
420
1d8b85cc 421 if (!page_has_buffers(page))
2d1db3b1 422 return migrate_page(mapping, newpage, page);
1d8b85cc
CL
423
424 head = page_buffers(page);
425
2d1db3b1 426 rc = migrate_page_move_mapping(mapping, newpage, page);
1d8b85cc
CL
427
428 if (rc)
429 return rc;
430
431 bh = head;
432 do {
433 get_bh(bh);
434 lock_buffer(bh);
435 bh = bh->b_this_page;
436
437 } while (bh != head);
438
439 ClearPagePrivate(page);
440 set_page_private(newpage, page_private(page));
441 set_page_private(page, 0);
442 put_page(page);
443 get_page(newpage);
444
445 bh = head;
446 do {
447 set_bh_page(bh, newpage, bh_offset(bh));
448 bh = bh->b_this_page;
449
450 } while (bh != head);
451
452 SetPagePrivate(newpage);
453
454 migrate_page_copy(newpage, page);
455
456 bh = head;
457 do {
458 unlock_buffer(bh);
459 put_bh(bh);
460 bh = bh->b_this_page;
461
462 } while (bh != head);
463
464 return 0;
465}
466EXPORT_SYMBOL(buffer_migrate_page);
467
04e62a29
CL
468/*
469 * Writeback a page to clean the dirty state
470 */
471static int writeout(struct address_space *mapping, struct page *page)
8351a6e4 472{
04e62a29
CL
473 struct writeback_control wbc = {
474 .sync_mode = WB_SYNC_NONE,
475 .nr_to_write = 1,
476 .range_start = 0,
477 .range_end = LLONG_MAX,
478 .nonblocking = 1,
479 .for_reclaim = 1
480 };
481 int rc;
482
483 if (!mapping->a_ops->writepage)
484 /* No write method for the address space */
485 return -EINVAL;
486
487 if (!clear_page_dirty_for_io(page))
488 /* Someone else already triggered a write */
489 return -EAGAIN;
490
8351a6e4 491 /*
04e62a29
CL
492 * A dirty page may imply that the underlying filesystem has
493 * the page on some queue. So the page must be clean for
494 * migration. Writeout may mean we loose the lock and the
495 * page state is no longer what we checked for earlier.
496 * At this point we know that the migration attempt cannot
497 * be successful.
8351a6e4 498 */
04e62a29 499 remove_migration_ptes(page, page);
8351a6e4 500
04e62a29
CL
501 rc = mapping->a_ops->writepage(page, &wbc);
502 if (rc < 0)
503 /* I/O Error writing */
504 return -EIO;
8351a6e4 505
04e62a29
CL
506 if (rc != AOP_WRITEPAGE_ACTIVATE)
507 /* unlocked. Relock */
508 lock_page(page);
509
510 return -EAGAIN;
511}
512
513/*
514 * Default handling if a filesystem does not provide a migration function.
515 */
516static int fallback_migrate_page(struct address_space *mapping,
517 struct page *newpage, struct page *page)
518{
519 if (PageDirty(page))
520 return writeout(mapping, page);
8351a6e4
CL
521
522 /*
523 * Buffers may be managed in a filesystem specific way.
524 * We must have no buffers or drop them.
525 */
526 if (page_has_buffers(page) &&
527 !try_to_release_page(page, GFP_KERNEL))
528 return -EAGAIN;
529
530 return migrate_page(mapping, newpage, page);
531}
532
e24f0b8f
CL
533/*
534 * Move a page to a newly allocated page
535 * The page is locked and all ptes have been successfully removed.
536 *
537 * The new page will have replaced the old page if this function
538 * is successful.
539 */
540static int move_to_new_page(struct page *newpage, struct page *page)
541{
542 struct address_space *mapping;
543 int rc;
544
545 /*
546 * Block others from accessing the page when we get around to
547 * establishing additional references. We are the only one
548 * holding a reference to the new page at this point.
549 */
550 if (TestSetPageLocked(newpage))
551 BUG();
552
553 /* Prepare mapping for the new page.*/
554 newpage->index = page->index;
555 newpage->mapping = page->mapping;
556
557 mapping = page_mapping(page);
558 if (!mapping)
559 rc = migrate_page(mapping, newpage, page);
560 else if (mapping->a_ops->migratepage)
561 /*
562 * Most pages have a mapping and most filesystems
563 * should provide a migration function. Anonymous
564 * pages are part of swap space which also has its
565 * own migration function. This is the most common
566 * path for page migration.
567 */
568 rc = mapping->a_ops->migratepage(mapping,
569 newpage, page);
570 else
571 rc = fallback_migrate_page(mapping, newpage, page);
572
573 if (!rc)
574 remove_migration_ptes(page, newpage);
575 else
576 newpage->mapping = NULL;
577
578 unlock_page(newpage);
579
580 return rc;
581}
582
583/*
584 * Obtain the lock on page, remove all ptes and migrate the page
585 * to the newly allocated page in newpage.
586 */
95a402c3
CL
587static int unmap_and_move(new_page_t get_new_page, unsigned long private,
588 struct page *page, int force)
e24f0b8f
CL
589{
590 int rc = 0;
95a402c3
CL
591 struct page *newpage = get_new_page(page, private);
592
593 if (!newpage)
594 return -ENOMEM;
e24f0b8f
CL
595
596 if (page_count(page) == 1)
597 /* page was freed from under us. So we are done. */
95a402c3 598 goto move_newpage;
e24f0b8f
CL
599
600 rc = -EAGAIN;
601 if (TestSetPageLocked(page)) {
602 if (!force)
95a402c3 603 goto move_newpage;
e24f0b8f
CL
604 lock_page(page);
605 }
606
607 if (PageWriteback(page)) {
608 if (!force)
609 goto unlock;
610 wait_on_page_writeback(page);
611 }
612
613 /*
614 * Establish migration ptes or remove ptes
615 */
616 if (try_to_unmap(page, 1) != SWAP_FAIL) {
617 if (!page_mapped(page))
618 rc = move_to_new_page(newpage, page);
619 } else
620 /* A vma has VM_LOCKED set -> permanent failure */
621 rc = -EPERM;
622
623 if (rc)
624 remove_migration_ptes(page, page);
625unlock:
626 unlock_page(page);
95a402c3 627
e24f0b8f 628 if (rc != -EAGAIN) {
aaa994b3
CL
629 /*
630 * A page that has been migrated has all references
631 * removed and will be freed. A page that has not been
632 * migrated will have kepts its references and be
633 * restored.
634 */
635 list_del(&page->lru);
636 move_to_lru(page);
e24f0b8f 637 }
95a402c3
CL
638
639move_newpage:
640 /*
641 * Move the new page to the LRU. If migration was not successful
642 * then this will free the page.
643 */
644 move_to_lru(newpage);
e24f0b8f
CL
645 return rc;
646}
647
b20a3503
CL
648/*
649 * migrate_pages
650 *
95a402c3
CL
651 * The function takes one list of pages to migrate and a function
652 * that determines from the page to be migrated and the private data
653 * the target of the move and allocates the page.
b20a3503
CL
654 *
655 * The function returns after 10 attempts or if no pages
656 * are movable anymore because to has become empty
aaa994b3
CL
657 * or no retryable pages exist anymore. All pages will be
658 * retruned to the LRU or freed.
b20a3503 659 *
95a402c3 660 * Return: Number of pages not migrated or error code.
b20a3503 661 */
95a402c3
CL
662int migrate_pages(struct list_head *from,
663 new_page_t get_new_page, unsigned long private)
b20a3503 664{
e24f0b8f 665 int retry = 1;
b20a3503
CL
666 int nr_failed = 0;
667 int pass = 0;
668 struct page *page;
669 struct page *page2;
670 int swapwrite = current->flags & PF_SWAPWRITE;
671 int rc;
672
673 if (!swapwrite)
674 current->flags |= PF_SWAPWRITE;
675
e24f0b8f
CL
676 for(pass = 0; pass < 10 && retry; pass++) {
677 retry = 0;
b20a3503 678
e24f0b8f 679 list_for_each_entry_safe(page, page2, from, lru) {
e24f0b8f 680 cond_resched();
2d1db3b1 681
95a402c3
CL
682 rc = unmap_and_move(get_new_page, private,
683 page, pass > 2);
2d1db3b1 684
e24f0b8f 685 switch(rc) {
95a402c3
CL
686 case -ENOMEM:
687 goto out;
e24f0b8f 688 case -EAGAIN:
2d1db3b1 689 retry++;
e24f0b8f
CL
690 break;
691 case 0:
e24f0b8f
CL
692 break;
693 default:
2d1db3b1 694 /* Permanent failure */
2d1db3b1 695 nr_failed++;
e24f0b8f 696 break;
2d1db3b1 697 }
b20a3503
CL
698 }
699 }
95a402c3
CL
700 rc = 0;
701out:
b20a3503
CL
702 if (!swapwrite)
703 current->flags &= ~PF_SWAPWRITE;
704
aaa994b3 705 putback_lru_pages(from);
b20a3503 706
95a402c3
CL
707 if (rc)
708 return rc;
b20a3503 709
95a402c3 710 return nr_failed + retry;
b20a3503 711}
95a402c3 712