[PATCH] Swapless page migration: modify core logic
[linux-2.6/sactl.git] / mm / migrate.c
blob8f91463eab4e2906018d816f2364f3270eb12131
1 /*
2 * Memory Migration functionality - linux/mm/migration.c
4 * Copyright (C) 2006 Silicon Graphics, Inc., Christoph Lameter
6 * Page migration was first developed in the context of the memory hotplug
7 * project. The main authors of the migration code are:
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>
15 #include <linux/migrate.h>
16 #include <linux/module.h>
17 #include <linux/swap.h>
18 #include <linux/swapops.h>
19 #include <linux/pagemap.h>
20 #include <linux/buffer_head.h>
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>
28 #include "internal.h"
30 /* The maximum number of pages to take off the LRU for migration */
31 #define MIGRATE_CHUNK_SIZE 256
33 #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
36 * Isolate one page from the LRU lists. If successful put it onto
37 * the indicated list with elevated page count.
39 * Result:
40 * -EBUSY: page not on LRU list
41 * 0: page removed from LRU list and added to the specified list.
43 int isolate_lru_page(struct page *page, struct list_head *pagelist)
45 int ret = -EBUSY;
47 if (PageLRU(page)) {
48 struct zone *zone = page_zone(page);
50 spin_lock_irq(&zone->lru_lock);
51 if (PageLRU(page)) {
52 ret = 0;
53 get_page(page);
54 ClearPageLRU(page);
55 if (PageActive(page))
56 del_page_from_active_list(zone, page);
57 else
58 del_page_from_inactive_list(zone, page);
59 list_add_tail(&page->lru, pagelist);
61 spin_unlock_irq(&zone->lru_lock);
63 return ret;
67 * migrate_prep() needs to be called after we have compiled the list of pages
68 * to be migrated using isolate_lru_page() but before we begin a series of calls
69 * to migrate_pages().
71 int migrate_prep(void)
74 * Clear the LRU lists so pages can be isolated.
75 * Note that pages may be moved off the LRU after we have
76 * drained them. Those pages will fail to migrate like other
77 * pages that may be busy.
79 lru_add_drain_all();
81 return 0;
84 static inline void move_to_lru(struct page *page)
86 list_del(&page->lru);
87 if (PageActive(page)) {
89 * lru_cache_add_active checks that
90 * the PG_active bit is off.
92 ClearPageActive(page);
93 lru_cache_add_active(page);
94 } else {
95 lru_cache_add(page);
97 put_page(page);
101 * Add isolated pages on the list back to the LRU.
103 * returns the number of pages put back.
105 int putback_lru_pages(struct list_head *l)
107 struct page *page;
108 struct page *page2;
109 int count = 0;
111 list_for_each_entry_safe(page, page2, l, lru) {
112 move_to_lru(page);
113 count++;
115 return count;
118 static inline int is_swap_pte(pte_t pte)
120 return !pte_none(pte) && !pte_present(pte) && !pte_file(pte);
124 * Restore a potential migration pte to a working pte entry
126 static void remove_migration_pte(struct vm_area_struct *vma, unsigned long addr,
127 struct page *old, struct page *new)
129 struct mm_struct *mm = vma->vm_mm;
130 swp_entry_t entry;
131 pgd_t *pgd;
132 pud_t *pud;
133 pmd_t *pmd;
134 pte_t *ptep, pte;
135 spinlock_t *ptl;
137 pgd = pgd_offset(mm, addr);
138 if (!pgd_present(*pgd))
139 return;
141 pud = pud_offset(pgd, addr);
142 if (!pud_present(*pud))
143 return;
145 pmd = pmd_offset(pud, addr);
146 if (!pmd_present(*pmd))
147 return;
149 ptep = pte_offset_map(pmd, addr);
151 if (!is_swap_pte(*ptep)) {
152 pte_unmap(ptep);
153 return;
156 ptl = pte_lockptr(mm, pmd);
157 spin_lock(ptl);
158 pte = *ptep;
159 if (!is_swap_pte(pte))
160 goto out;
162 entry = pte_to_swp_entry(pte);
164 if (!is_migration_entry(entry) || migration_entry_to_page(entry) != old)
165 goto out;
167 inc_mm_counter(mm, anon_rss);
168 get_page(new);
169 pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
170 if (is_write_migration_entry(entry))
171 pte = pte_mkwrite(pte);
172 set_pte_at(mm, addr, ptep, pte);
173 page_add_anon_rmap(new, vma, addr);
174 out:
175 pte_unmap_unlock(ptep, ptl);
179 * Get rid of all migration entries and replace them by
180 * references to the indicated page.
182 * Must hold mmap_sem lock on at least one of the vmas containing
183 * the page so that the anon_vma cannot vanish.
185 static void remove_migration_ptes(struct page *old, struct page *new)
187 struct anon_vma *anon_vma;
188 struct vm_area_struct *vma;
189 unsigned long mapping;
191 mapping = (unsigned long)new->mapping;
193 if (!mapping || (mapping & PAGE_MAPPING_ANON) == 0)
194 return;
197 * We hold the mmap_sem lock. So no need to call page_lock_anon_vma.
199 anon_vma = (struct anon_vma *) (mapping - PAGE_MAPPING_ANON);
200 spin_lock(&anon_vma->lock);
202 list_for_each_entry(vma, &anon_vma->head, anon_vma_node)
203 remove_migration_pte(vma, page_address_in_vma(new, vma),
204 old, new);
206 spin_unlock(&anon_vma->lock);
210 * Something used the pte of a page under migration. We need to
211 * get to the page and wait until migration is finished.
212 * When we return from this function the fault will be retried.
214 * This function is called from do_swap_page().
216 void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
217 unsigned long address)
219 pte_t *ptep, pte;
220 spinlock_t *ptl;
221 swp_entry_t entry;
222 struct page *page;
224 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
225 pte = *ptep;
226 if (!is_swap_pte(pte))
227 goto out;
229 entry = pte_to_swp_entry(pte);
230 if (!is_migration_entry(entry))
231 goto out;
233 page = migration_entry_to_page(entry);
235 get_page(page);
236 pte_unmap_unlock(ptep, ptl);
237 wait_on_page_locked(page);
238 put_page(page);
239 return;
240 out:
241 pte_unmap_unlock(ptep, ptl);
245 * Replace the page in the mapping.
247 * The number of remaining references must be:
248 * 1 for anonymous pages without a mapping
249 * 2 for pages with a mapping
250 * 3 for pages with a mapping and PagePrivate set.
252 static int migrate_page_move_mapping(struct address_space *mapping,
253 struct page *newpage, struct page *page)
255 struct page **radix_pointer;
257 if (!mapping) {
258 /* Anonymous page */
259 if (page_count(page) != 1)
260 return -EAGAIN;
261 return 0;
264 write_lock_irq(&mapping->tree_lock);
266 radix_pointer = (struct page **)radix_tree_lookup_slot(
267 &mapping->page_tree,
268 page_index(page));
270 if (page_count(page) != 2 + !!PagePrivate(page) ||
271 *radix_pointer != page) {
272 write_unlock_irq(&mapping->tree_lock);
273 return -EAGAIN;
277 * Now we know that no one else is looking at the page.
279 get_page(newpage);
280 #ifdef CONFIG_SWAP
281 if (PageSwapCache(page)) {
282 SetPageSwapCache(newpage);
283 set_page_private(newpage, page_private(page));
285 #endif
287 *radix_pointer = newpage;
288 __put_page(page);
289 write_unlock_irq(&mapping->tree_lock);
291 return 0;
295 * Copy the page to its new location
297 static void migrate_page_copy(struct page *newpage, struct page *page)
299 copy_highpage(newpage, page);
301 if (PageError(page))
302 SetPageError(newpage);
303 if (PageReferenced(page))
304 SetPageReferenced(newpage);
305 if (PageUptodate(page))
306 SetPageUptodate(newpage);
307 if (PageActive(page))
308 SetPageActive(newpage);
309 if (PageChecked(page))
310 SetPageChecked(newpage);
311 if (PageMappedToDisk(page))
312 SetPageMappedToDisk(newpage);
314 if (PageDirty(page)) {
315 clear_page_dirty_for_io(page);
316 set_page_dirty(newpage);
319 #ifdef CONFIG_SWAP
320 ClearPageSwapCache(page);
321 #endif
322 ClearPageActive(page);
323 ClearPagePrivate(page);
324 set_page_private(page, 0);
325 page->mapping = NULL;
328 * If any waiters have accumulated on the new page then
329 * wake them up.
331 if (PageWriteback(newpage))
332 end_page_writeback(newpage);
335 /************************************************************
336 * Migration functions
337 ***********************************************************/
339 /* Always fail migration. Used for mappings that are not movable */
340 int fail_migrate_page(struct address_space *mapping,
341 struct page *newpage, struct page *page)
343 return -EIO;
345 EXPORT_SYMBOL(fail_migrate_page);
348 * Common logic to directly migrate a single page suitable for
349 * pages that do not use PagePrivate.
351 * Pages are locked upon entry and exit.
353 int migrate_page(struct address_space *mapping,
354 struct page *newpage, struct page *page)
356 int rc;
358 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
360 rc = migrate_page_move_mapping(mapping, newpage, page);
362 if (rc)
363 return rc;
365 migrate_page_copy(newpage, page);
366 return 0;
368 EXPORT_SYMBOL(migrate_page);
371 * Migration function for pages with buffers. This function can only be used
372 * if the underlying filesystem guarantees that no other references to "page"
373 * exist.
375 int buffer_migrate_page(struct address_space *mapping,
376 struct page *newpage, struct page *page)
378 struct buffer_head *bh, *head;
379 int rc;
381 if (!page_has_buffers(page))
382 return migrate_page(mapping, newpage, page);
384 head = page_buffers(page);
386 rc = migrate_page_move_mapping(mapping, newpage, page);
388 if (rc)
389 return rc;
391 bh = head;
392 do {
393 get_bh(bh);
394 lock_buffer(bh);
395 bh = bh->b_this_page;
397 } while (bh != head);
399 ClearPagePrivate(page);
400 set_page_private(newpage, page_private(page));
401 set_page_private(page, 0);
402 put_page(page);
403 get_page(newpage);
405 bh = head;
406 do {
407 set_bh_page(bh, newpage, bh_offset(bh));
408 bh = bh->b_this_page;
410 } while (bh != head);
412 SetPagePrivate(newpage);
414 migrate_page_copy(newpage, page);
416 bh = head;
417 do {
418 unlock_buffer(bh);
419 put_bh(bh);
420 bh = bh->b_this_page;
422 } while (bh != head);
424 return 0;
426 EXPORT_SYMBOL(buffer_migrate_page);
428 static int fallback_migrate_page(struct address_space *mapping,
429 struct page *newpage, struct page *page)
432 * Default handling if a filesystem does not provide
433 * a migration function. We can only migrate clean
434 * pages so try to write out any dirty pages first.
436 if (PageDirty(page)) {
437 switch (pageout(page, mapping)) {
438 case PAGE_KEEP:
439 case PAGE_ACTIVATE:
440 return -EAGAIN;
442 case PAGE_SUCCESS:
443 /* Relock since we lost the lock */
444 lock_page(page);
445 /* Must retry since page state may have changed */
446 return -EAGAIN;
448 case PAGE_CLEAN:
449 ; /* try to migrate the page below */
454 * Buffers may be managed in a filesystem specific way.
455 * We must have no buffers or drop them.
457 if (page_has_buffers(page) &&
458 !try_to_release_page(page, GFP_KERNEL))
459 return -EAGAIN;
461 return migrate_page(mapping, newpage, page);
465 * migrate_pages
467 * Two lists are passed to this function. The first list
468 * contains the pages isolated from the LRU to be migrated.
469 * The second list contains new pages that the pages isolated
470 * can be moved to.
472 * The function returns after 10 attempts or if no pages
473 * are movable anymore because to has become empty
474 * or no retryable pages exist anymore.
476 * Return: Number of pages not migrated when "to" ran empty.
478 int migrate_pages(struct list_head *from, struct list_head *to,
479 struct list_head *moved, struct list_head *failed)
481 int retry;
482 int nr_failed = 0;
483 int pass = 0;
484 struct page *page;
485 struct page *page2;
486 int swapwrite = current->flags & PF_SWAPWRITE;
487 int rc;
489 if (!swapwrite)
490 current->flags |= PF_SWAPWRITE;
492 redo:
493 retry = 0;
495 list_for_each_entry_safe(page, page2, from, lru) {
496 struct page *newpage = NULL;
497 struct address_space *mapping;
499 cond_resched();
501 rc = 0;
502 if (page_count(page) == 1)
503 /* page was freed from under us. So we are done. */
504 goto next;
506 if (to && list_empty(to))
507 break;
510 * Skip locked pages during the first two passes to give the
511 * functions holding the lock time to release the page. Later we
512 * use lock_page() to have a higher chance of acquiring the
513 * lock.
515 rc = -EAGAIN;
516 if (pass > 2)
517 lock_page(page);
518 else
519 if (TestSetPageLocked(page))
520 goto next;
523 * Only wait on writeback if we have already done a pass where
524 * we we may have triggered writeouts for lots of pages.
526 if (pass > 0)
527 wait_on_page_writeback(page);
528 else
529 if (PageWriteback(page))
530 goto unlock_page;
533 * Establish migration ptes or remove ptes
535 rc = -EPERM;
536 if (try_to_unmap(page, 1) == SWAP_FAIL)
537 /* A vma has VM_LOCKED set -> permanent failure */
538 goto unlock_page;
540 rc = -EAGAIN;
541 if (page_mapped(page))
542 goto unlock_page;
544 newpage = lru_to_page(to);
545 lock_page(newpage);
546 /* Prepare mapping for the new page.*/
547 newpage->index = page->index;
548 newpage->mapping = page->mapping;
551 * Pages are properly locked and writeback is complete.
552 * Try to migrate the page.
554 mapping = page_mapping(page);
555 if (!mapping)
556 rc = migrate_page(mapping, newpage, page);
558 else if (mapping->a_ops->migratepage)
560 * Most pages have a mapping and most filesystems
561 * should provide a migration function. Anonymous
562 * pages are part of swap space which also has its
563 * own migration function. This is the most common
564 * path for page migration.
566 rc = mapping->a_ops->migratepage(mapping,
567 newpage, page);
568 else
569 rc = fallback_migrate_page(mapping, newpage, page);
571 if (!rc)
572 remove_migration_ptes(page, newpage);
574 unlock_page(newpage);
576 unlock_page:
577 if (rc)
578 remove_migration_ptes(page, page);
580 unlock_page(page);
582 next:
583 if (rc) {
584 if (newpage)
585 newpage->mapping = NULL;
587 if (rc == -EAGAIN)
588 retry++;
589 else {
590 /* Permanent failure */
591 list_move(&page->lru, failed);
592 nr_failed++;
594 } else {
595 if (newpage) {
596 /* Successful migration. Return page to LRU */
597 move_to_lru(newpage);
599 list_move(&page->lru, moved);
602 if (retry && pass++ < 10)
603 goto redo;
605 if (!swapwrite)
606 current->flags &= ~PF_SWAPWRITE;
608 return nr_failed + retry;
612 * Migrate the list 'pagelist' of pages to a certain destination.
614 * Specify destination with either non-NULL vma or dest_node >= 0
615 * Return the number of pages not migrated or error code
617 int migrate_pages_to(struct list_head *pagelist,
618 struct vm_area_struct *vma, int dest)
620 LIST_HEAD(newlist);
621 LIST_HEAD(moved);
622 LIST_HEAD(failed);
623 int err = 0;
624 unsigned long offset = 0;
625 int nr_pages;
626 struct page *page;
627 struct list_head *p;
629 redo:
630 nr_pages = 0;
631 list_for_each(p, pagelist) {
632 if (vma) {
634 * The address passed to alloc_page_vma is used to
635 * generate the proper interleave behavior. We fake
636 * the address here by an increasing offset in order
637 * to get the proper distribution of pages.
639 * No decision has been made as to which page
640 * a certain old page is moved to so we cannot
641 * specify the correct address.
643 page = alloc_page_vma(GFP_HIGHUSER, vma,
644 offset + vma->vm_start);
645 offset += PAGE_SIZE;
647 else
648 page = alloc_pages_node(dest, GFP_HIGHUSER, 0);
650 if (!page) {
651 err = -ENOMEM;
652 goto out;
654 list_add_tail(&page->lru, &newlist);
655 nr_pages++;
656 if (nr_pages > MIGRATE_CHUNK_SIZE)
657 break;
659 err = migrate_pages(pagelist, &newlist, &moved, &failed);
661 putback_lru_pages(&moved); /* Call release pages instead ?? */
663 if (err >= 0 && list_empty(&newlist) && !list_empty(pagelist))
664 goto redo;
665 out:
666 /* Return leftover allocated pages */
667 while (!list_empty(&newlist)) {
668 page = list_entry(newlist.next, struct page, lru);
669 list_del(&page->lru);
670 __free_page(page);
672 list_splice(&failed, pagelist);
673 if (err < 0)
674 return err;
676 /* Calculate number of leftover pages */
677 nr_pages = 0;
678 list_for_each(p, pagelist)
679 nr_pages++;
680 return nr_pages;