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/pagemap.h>
19 #include <linux/buffer_head.h>
20 #include <linux/mm_inline.h>
21 #include <linux/pagevec.h>
22 #include <linux/rmap.h>
23 #include <linux/topology.h>
24 #include <linux/cpu.h>
25 #include <linux/cpuset.h>
26 #include <linux/swapops.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.
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)
48 struct zone *zone = page_zone(page);
50 spin_lock_irq(&zone->lru_lock);
56 del_page_from_active_list(zone, page);
58 del_page_from_inactive_list(zone, page);
59 list_add_tail(&page->lru, pagelist);
61 spin_unlock_irq(&zone->lru_lock);
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
71 int migrate_prep(void)
73 /* Must have swap device for migration */
74 if (nr_swap_pages <= 0)
78 * Clear the LRU lists so pages can be isolated.
79 * Note that pages may be moved off the LRU after we have
80 * drained them. Those pages will fail to migrate like other
81 * pages that may be busy.
88 static inline void move_to_lru(struct page *page)
91 if (PageActive(page)) {
93 * lru_cache_add_active checks that
94 * the PG_active bit is off.
96 ClearPageActive(page);
97 lru_cache_add_active(page);
105 * Add isolated pages on the list back to the LRU.
107 * returns the number of pages put back.
109 int putback_lru_pages(struct list_head *l)
115 list_for_each_entry_safe(page, page2, l, lru) {
123 * swapout a single page
124 * page is locked upon entry, unlocked on exit
126 static int swap_page(struct page *page)
128 struct address_space *mapping = page_mapping(page);
130 if (page_mapped(page) && mapping)
131 if (try_to_unmap(page, 1) != SWAP_SUCCESS)
134 if (PageDirty(page)) {
135 /* Page is dirty, try to write it out here */
136 switch(pageout(page, mapping)) {
145 ; /* try to free the page below */
149 if (PagePrivate(page)) {
150 if (!try_to_release_page(page, GFP_KERNEL) ||
151 (!mapping && page_count(page) == 1))
155 if (remove_mapping(mapping, page)) {
169 * Replace the page in the mapping.
171 * The number of remaining references must be:
172 * 1 for anonymous pages without a mapping
173 * 2 for pages with a mapping
174 * 3 for pages with a mapping and PagePrivate set.
176 static int migrate_page_move_mapping(struct address_space *mapping,
177 struct page *newpage, struct page *page)
179 struct page **radix_pointer;
181 write_lock_irq(&mapping->tree_lock);
183 radix_pointer = (struct page **)radix_tree_lookup_slot(
187 if (!page_mapping(page) ||
188 page_count(page) != 2 + !!PagePrivate(page) ||
189 *radix_pointer != page) {
190 write_unlock_irq(&mapping->tree_lock);
195 * Now we know that no one else is looking at the page.
198 if (PageSwapCache(page)) {
199 SetPageSwapCache(newpage);
200 set_page_private(newpage, page_private(page));
203 *radix_pointer = newpage;
205 write_unlock_irq(&mapping->tree_lock);
211 * Copy the page to its new location
213 static void migrate_page_copy(struct page *newpage, struct page *page)
215 copy_highpage(newpage, page);
218 SetPageError(newpage);
219 if (PageReferenced(page))
220 SetPageReferenced(newpage);
221 if (PageUptodate(page))
222 SetPageUptodate(newpage);
223 if (PageActive(page))
224 SetPageActive(newpage);
225 if (PageChecked(page))
226 SetPageChecked(newpage);
227 if (PageMappedToDisk(page))
228 SetPageMappedToDisk(newpage);
230 if (PageDirty(page)) {
231 clear_page_dirty_for_io(page);
232 set_page_dirty(newpage);
235 ClearPageSwapCache(page);
236 ClearPageActive(page);
237 ClearPagePrivate(page);
238 set_page_private(page, 0);
239 page->mapping = NULL;
242 * If any waiters have accumulated on the new page then
245 if (PageWriteback(newpage))
246 end_page_writeback(newpage);
249 /************************************************************
250 * Migration functions
251 ***********************************************************/
253 /* Always fail migration. Used for mappings that are not movable */
254 int fail_migrate_page(struct address_space *mapping,
255 struct page *newpage, struct page *page)
259 EXPORT_SYMBOL(fail_migrate_page);
262 * Common logic to directly migrate a single page suitable for
263 * pages that do not use PagePrivate.
265 * Pages are locked upon entry and exit.
267 int migrate_page(struct address_space *mapping,
268 struct page *newpage, struct page *page)
272 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
274 rc = migrate_page_move_mapping(mapping, newpage, page);
279 migrate_page_copy(newpage, page);
282 * Remove auxiliary swap entries and replace
283 * them with real ptes.
285 * Note that a real pte entry will allow processes that are not
286 * waiting on the page lock to use the new page via the page tables
287 * before the new page is unlocked.
289 remove_from_swap(newpage);
292 EXPORT_SYMBOL(migrate_page);
295 * Migration function for pages with buffers. This function can only be used
296 * if the underlying filesystem guarantees that no other references to "page"
299 int buffer_migrate_page(struct address_space *mapping,
300 struct page *newpage, struct page *page)
302 struct buffer_head *bh, *head;
305 if (!page_has_buffers(page))
306 return migrate_page(mapping, newpage, page);
308 head = page_buffers(page);
310 rc = migrate_page_move_mapping(mapping, newpage, page);
319 bh = bh->b_this_page;
321 } while (bh != head);
323 ClearPagePrivate(page);
324 set_page_private(newpage, page_private(page));
325 set_page_private(page, 0);
331 set_bh_page(bh, newpage, bh_offset(bh));
332 bh = bh->b_this_page;
334 } while (bh != head);
336 SetPagePrivate(newpage);
338 migrate_page_copy(newpage, page);
344 bh = bh->b_this_page;
346 } while (bh != head);
350 EXPORT_SYMBOL(buffer_migrate_page);
355 * Two lists are passed to this function. The first list
356 * contains the pages isolated from the LRU to be migrated.
357 * The second list contains new pages that the pages isolated
358 * can be moved to. If the second list is NULL then all
359 * pages are swapped out.
361 * The function returns after 10 attempts or if no pages
362 * are movable anymore because to has become empty
363 * or no retryable pages exist anymore.
365 * Return: Number of pages not migrated when "to" ran empty.
367 int migrate_pages(struct list_head *from, struct list_head *to,
368 struct list_head *moved, struct list_head *failed)
375 int swapwrite = current->flags & PF_SWAPWRITE;
379 current->flags |= PF_SWAPWRITE;
384 list_for_each_entry_safe(page, page2, from, lru) {
385 struct page *newpage = NULL;
386 struct address_space *mapping;
391 if (page_count(page) == 1)
392 /* page was freed from under us. So we are done. */
395 if (to && list_empty(to))
399 * Skip locked pages during the first two passes to give the
400 * functions holding the lock time to release the page. Later we
401 * use lock_page() to have a higher chance of acquiring the
408 if (TestSetPageLocked(page))
412 * Only wait on writeback if we have already done a pass where
413 * we we may have triggered writeouts for lots of pages.
416 wait_on_page_writeback(page);
418 if (PageWriteback(page))
423 * Anonymous pages must have swap cache references otherwise
424 * the information contained in the page maps cannot be
427 if (PageAnon(page) && !PageSwapCache(page)) {
428 if (!add_to_swap(page, GFP_KERNEL)) {
435 rc = swap_page(page);
440 * Establish swap ptes for anonymous pages or destroy pte
443 * In order to reestablish file backed mappings the fault handlers
444 * will take the radix tree_lock which may then be used to stop
445 * processses from accessing this page until the new page is ready.
447 * A process accessing via a swap pte (an anonymous page) will take a
448 * page_lock on the old page which will block the process until the
449 * migration attempt is complete. At that time the PageSwapCache bit
450 * will be examined. If the page was migrated then the PageSwapCache
451 * bit will be clear and the operation to retrieve the page will be
452 * retried which will find the new page in the radix tree. Then a new
453 * direct mapping may be generated based on the radix tree contents.
455 * If the page was not migrated then the PageSwapCache bit
456 * is still set and the operation may continue.
459 if (try_to_unmap(page, 1) == SWAP_FAIL)
460 /* A vma has VM_LOCKED set -> permanent failure */
464 if (page_mapped(page))
467 newpage = lru_to_page(to);
469 /* Prepare mapping for the new page.*/
470 newpage->index = page->index;
471 newpage->mapping = page->mapping;
474 * Pages are properly locked and writeback is complete.
475 * Try to migrate the page.
477 mapping = page_mapping(page);
481 if (mapping->a_ops->migratepage) {
483 * Most pages have a mapping and most filesystems
484 * should provide a migration function. Anonymous
485 * pages are part of swap space which also has its
486 * own migration function. This is the most common
487 * path for page migration.
489 rc = mapping->a_ops->migratepage(mapping,
495 * Default handling if a filesystem does not provide
496 * a migration function. We can only migrate clean
497 * pages so try to write out any dirty pages first.
499 if (PageDirty(page)) {
500 switch (pageout(page, mapping)) {
506 unlock_page(newpage);
510 ; /* try to migrate the page below */
515 * Buffers are managed in a filesystem specific way.
516 * We must have no buffers or drop them.
518 if (!page_has_buffers(page) ||
519 try_to_release_page(page, GFP_KERNEL)) {
520 rc = migrate_page(mapping, newpage, page);
525 * On early passes with mapped pages simply
526 * retry. There may be a lock held for some
527 * buffers that may go away. Later
532 * Persistently unable to drop buffers..... As a
533 * measure of last resort we fall back to
536 unlock_page(newpage);
538 rc = swap_page(page);
543 unlock_page(newpage);
551 newpage->mapping = NULL;
556 /* Permanent failure */
557 list_move(&page->lru, failed);
562 /* Successful migration. Return page to LRU */
563 move_to_lru(newpage);
565 list_move(&page->lru, moved);
568 if (retry && pass++ < 10)
572 current->flags &= ~PF_SWAPWRITE;
574 return nr_failed + retry;
578 * Migrate the list 'pagelist' of pages to a certain destination.
580 * Specify destination with either non-NULL vma or dest_node >= 0
581 * Return the number of pages not migrated or error code
583 int migrate_pages_to(struct list_head *pagelist,
584 struct vm_area_struct *vma, int dest)
590 unsigned long offset = 0;
597 list_for_each(p, pagelist) {
600 * The address passed to alloc_page_vma is used to
601 * generate the proper interleave behavior. We fake
602 * the address here by an increasing offset in order
603 * to get the proper distribution of pages.
605 * No decision has been made as to which page
606 * a certain old page is moved to so we cannot
607 * specify the correct address.
609 page = alloc_page_vma(GFP_HIGHUSER, vma,
610 offset + vma->vm_start);
614 page = alloc_pages_node(dest, GFP_HIGHUSER, 0);
620 list_add_tail(&page->lru, &newlist);
622 if (nr_pages > MIGRATE_CHUNK_SIZE)
625 err = migrate_pages(pagelist, &newlist, &moved, &failed);
627 putback_lru_pages(&moved); /* Call release pages instead ?? */
629 if (err >= 0 && list_empty(&newlist) && !list_empty(pagelist))
632 /* Return leftover allocated pages */
633 while (!list_empty(&newlist)) {
634 page = list_entry(newlist.next, struct page, lru);
635 list_del(&page->lru);
638 list_splice(&failed, pagelist);
642 /* Calculate number of leftover pages */
644 list_for_each(p, pagelist)