]>
git.ipfire.org Git - thirdparty/kernel/stable.git/blob - mm/migrate.c
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>
27 #include <linux/writeback.h>
31 #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
34 * Isolate one page from the LRU lists. If successful put it onto
35 * the indicated list with elevated page count.
38 * -EBUSY: page not on LRU list
39 * 0: page removed from LRU list and added to the specified list.
41 int isolate_lru_page(struct page
*page
, struct list_head
*pagelist
)
46 struct zone
*zone
= page_zone(page
);
48 spin_lock_irq(&zone
->lru_lock
);
54 del_page_from_active_list(zone
, page
);
56 del_page_from_inactive_list(zone
, page
);
57 list_add_tail(&page
->lru
, pagelist
);
59 spin_unlock_irq(&zone
->lru_lock
);
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
69 int migrate_prep(void)
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.
82 static inline void move_to_lru(struct page
*page
)
84 if (PageActive(page
)) {
86 * lru_cache_add_active checks that
87 * the PG_active bit is off.
89 ClearPageActive(page
);
90 lru_cache_add_active(page
);
98 * Add isolated pages on the list back to the LRU.
100 * returns the number of pages put back.
102 int putback_lru_pages(struct list_head
*l
)
108 list_for_each_entry_safe(page
, page2
, l
, lru
) {
109 list_del(&page
->lru
);
116 static inline int is_swap_pte(pte_t pte
)
118 return !pte_none(pte
) && !pte_present(pte
) && !pte_file(pte
);
122 * Restore a potential migration pte to a working pte entry
124 static void remove_migration_pte(struct vm_area_struct
*vma
,
125 struct page
*old
, struct page
*new)
127 struct mm_struct
*mm
= vma
->vm_mm
;
134 unsigned long addr
= page_address_in_vma(new, vma
);
139 pgd
= pgd_offset(mm
, addr
);
140 if (!pgd_present(*pgd
))
143 pud
= pud_offset(pgd
, addr
);
144 if (!pud_present(*pud
))
147 pmd
= pmd_offset(pud
, addr
);
148 if (!pmd_present(*pmd
))
151 ptep
= pte_offset_map(pmd
, addr
);
153 if (!is_swap_pte(*ptep
)) {
158 ptl
= pte_lockptr(mm
, pmd
);
161 if (!is_swap_pte(pte
))
164 entry
= pte_to_swp_entry(pte
);
166 if (!is_migration_entry(entry
) || migration_entry_to_page(entry
) != old
)
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
);
176 page_add_anon_rmap(new, vma
, addr
);
178 page_add_file_rmap(new);
180 /* No need to invalidate - it was non-present before */
181 update_mmu_cache(vma
, addr
, pte
);
182 lazy_mmu_prot_update(pte
);
185 pte_unmap_unlock(ptep
, ptl
);
189 * Note that remove_file_migration_ptes will only work on regular mappings,
190 * Nonlinear mappings do not use migration entries.
192 static void remove_file_migration_ptes(struct page
*old
, struct page
*new)
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
);
202 spin_lock(&mapping
->i_mmap_lock
);
204 vma_prio_tree_foreach(vma
, &iter
, &mapping
->i_mmap
, pgoff
, pgoff
)
205 remove_migration_pte(vma
, old
, new);
207 spin_unlock(&mapping
->i_mmap_lock
);
211 * Must hold mmap_sem lock on at least one of the vmas containing
212 * the page so that the anon_vma cannot vanish.
214 static void remove_anon_migration_ptes(struct page
*old
, struct page
*new)
216 struct anon_vma
*anon_vma
;
217 struct vm_area_struct
*vma
;
218 unsigned long mapping
;
220 mapping
= (unsigned long)new->mapping
;
222 if (!mapping
|| (mapping
& PAGE_MAPPING_ANON
) == 0)
226 * We hold the mmap_sem lock. So no need to call page_lock_anon_vma.
228 anon_vma
= (struct anon_vma
*) (mapping
- PAGE_MAPPING_ANON
);
229 spin_lock(&anon_vma
->lock
);
231 list_for_each_entry(vma
, &anon_vma
->head
, anon_vma_node
)
232 remove_migration_pte(vma
, old
, new);
234 spin_unlock(&anon_vma
->lock
);
238 * Get rid of all migration entries and replace them by
239 * references to the indicated page.
241 static void remove_migration_ptes(struct page
*old
, struct page
*new)
244 remove_anon_migration_ptes(old
, new);
246 remove_file_migration_ptes(old
, new);
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.
254 * This function is called from do_swap_page().
256 void migration_entry_wait(struct mm_struct
*mm
, pmd_t
*pmd
,
257 unsigned long address
)
264 ptep
= pte_offset_map_lock(mm
, pmd
, address
, &ptl
);
266 if (!is_swap_pte(pte
))
269 entry
= pte_to_swp_entry(pte
);
270 if (!is_migration_entry(entry
))
273 page
= migration_entry_to_page(entry
);
276 pte_unmap_unlock(ptep
, ptl
);
277 wait_on_page_locked(page
);
281 pte_unmap_unlock(ptep
, ptl
);
285 * Replace the page in the mapping.
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.
292 static int migrate_page_move_mapping(struct address_space
*mapping
,
293 struct page
*newpage
, struct page
*page
)
295 struct page
**radix_pointer
;
299 if (page_count(page
) != 1)
304 write_lock_irq(&mapping
->tree_lock
);
306 radix_pointer
= (struct page
**)radix_tree_lookup_slot(
310 if (page_count(page
) != 2 + !!PagePrivate(page
) ||
311 *radix_pointer
!= page
) {
312 write_unlock_irq(&mapping
->tree_lock
);
317 * Now we know that no one else is looking at the page.
321 if (PageSwapCache(page
)) {
322 SetPageSwapCache(newpage
);
323 set_page_private(newpage
, page_private(page
));
327 *radix_pointer
= newpage
;
329 write_unlock_irq(&mapping
->tree_lock
);
335 * Copy the page to its new location
337 static void migrate_page_copy(struct page
*newpage
, struct page
*page
)
339 copy_highpage(newpage
, 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
);
354 if (PageDirty(page
)) {
355 clear_page_dirty_for_io(page
);
356 set_page_dirty(newpage
);
360 ClearPageSwapCache(page
);
362 ClearPageActive(page
);
363 ClearPagePrivate(page
);
364 set_page_private(page
, 0);
365 page
->mapping
= NULL
;
368 * If any waiters have accumulated on the new page then
371 if (PageWriteback(newpage
))
372 end_page_writeback(newpage
);
375 /************************************************************
376 * Migration functions
377 ***********************************************************/
379 /* Always fail migration. Used for mappings that are not movable */
380 int fail_migrate_page(struct address_space
*mapping
,
381 struct page
*newpage
, struct page
*page
)
385 EXPORT_SYMBOL(fail_migrate_page
);
388 * Common logic to directly migrate a single page suitable for
389 * pages that do not use PagePrivate.
391 * Pages are locked upon entry and exit.
393 int migrate_page(struct address_space
*mapping
,
394 struct page
*newpage
, struct page
*page
)
398 BUG_ON(PageWriteback(page
)); /* Writeback must be complete */
400 rc
= migrate_page_move_mapping(mapping
, newpage
, page
);
405 migrate_page_copy(newpage
, page
);
408 EXPORT_SYMBOL(migrate_page
);
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"
415 int buffer_migrate_page(struct address_space
*mapping
,
416 struct page
*newpage
, struct page
*page
)
418 struct buffer_head
*bh
, *head
;
421 if (!page_has_buffers(page
))
422 return migrate_page(mapping
, newpage
, page
);
424 head
= page_buffers(page
);
426 rc
= migrate_page_move_mapping(mapping
, newpage
, page
);
435 bh
= bh
->b_this_page
;
437 } while (bh
!= head
);
439 ClearPagePrivate(page
);
440 set_page_private(newpage
, page_private(page
));
441 set_page_private(page
, 0);
447 set_bh_page(bh
, newpage
, bh_offset(bh
));
448 bh
= bh
->b_this_page
;
450 } while (bh
!= head
);
452 SetPagePrivate(newpage
);
454 migrate_page_copy(newpage
, page
);
460 bh
= bh
->b_this_page
;
462 } while (bh
!= head
);
466 EXPORT_SYMBOL(buffer_migrate_page
);
469 * Writeback a page to clean the dirty state
471 static int writeout(struct address_space
*mapping
, struct page
*page
)
473 struct writeback_control wbc
= {
474 .sync_mode
= WB_SYNC_NONE
,
477 .range_end
= LLONG_MAX
,
483 if (!mapping
->a_ops
->writepage
)
484 /* No write method for the address space */
487 if (!clear_page_dirty_for_io(page
))
488 /* Someone else already triggered a write */
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
499 remove_migration_ptes(page
, page
);
501 rc
= mapping
->a_ops
->writepage(page
, &wbc
);
503 /* I/O Error writing */
506 if (rc
!= AOP_WRITEPAGE_ACTIVATE
)
507 /* unlocked. Relock */
514 * Default handling if a filesystem does not provide a migration function.
516 static int fallback_migrate_page(struct address_space
*mapping
,
517 struct page
*newpage
, struct page
*page
)
520 return writeout(mapping
, page
);
523 * Buffers may be managed in a filesystem specific way.
524 * We must have no buffers or drop them.
526 if (page_has_buffers(page
) &&
527 !try_to_release_page(page
, GFP_KERNEL
))
530 return migrate_page(mapping
, newpage
, page
);
534 * Move a page to a newly allocated page
535 * The page is locked and all ptes have been successfully removed.
537 * The new page will have replaced the old page if this function
540 static int move_to_new_page(struct page
*newpage
, struct page
*page
)
542 struct address_space
*mapping
;
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.
550 if (TestSetPageLocked(newpage
))
553 /* Prepare mapping for the new page.*/
554 newpage
->index
= page
->index
;
555 newpage
->mapping
= page
->mapping
;
557 mapping
= page_mapping(page
);
559 rc
= migrate_page(mapping
, newpage
, page
);
560 else if (mapping
->a_ops
->migratepage
)
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.
568 rc
= mapping
->a_ops
->migratepage(mapping
,
571 rc
= fallback_migrate_page(mapping
, newpage
, page
);
574 remove_migration_ptes(page
, newpage
);
576 newpage
->mapping
= NULL
;
578 unlock_page(newpage
);
584 * Obtain the lock on page, remove all ptes and migrate the page
585 * to the newly allocated page in newpage.
587 static int unmap_and_move(new_page_t get_new_page
, unsigned long private,
588 struct page
*page
, int force
)
591 struct page
*newpage
= get_new_page(page
, private);
596 if (page_count(page
) == 1)
597 /* page was freed from under us. So we are done. */
601 if (TestSetPageLocked(page
)) {
607 if (PageWriteback(page
)) {
610 wait_on_page_writeback(page
);
614 * Establish migration ptes or remove ptes
616 if (try_to_unmap(page
, 1) != SWAP_FAIL
) {
617 if (!page_mapped(page
))
618 rc
= move_to_new_page(newpage
, page
);
620 /* A vma has VM_LOCKED set -> permanent failure */
624 remove_migration_ptes(page
, page
);
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
635 list_del(&page
->lru
);
641 * Move the new page to the LRU. If migration was not successful
642 * then this will free the page.
644 move_to_lru(newpage
);
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.
655 * The function returns after 10 attempts or if no pages
656 * are movable anymore because to has become empty
657 * or no retryable pages exist anymore. All pages will be
658 * retruned to the LRU or freed.
660 * Return: Number of pages not migrated or error code.
662 int migrate_pages(struct list_head
*from
,
663 new_page_t get_new_page
, unsigned long private)
670 int swapwrite
= current
->flags
& PF_SWAPWRITE
;
674 current
->flags
|= PF_SWAPWRITE
;
676 for(pass
= 0; pass
< 10 && retry
; pass
++) {
679 list_for_each_entry_safe(page
, page2
, from
, lru
) {
682 rc
= unmap_and_move(get_new_page
, private,
694 /* Permanent failure */
703 current
->flags
&= ~PF_SWAPWRITE
;
705 putback_lru_pages(from
);
710 return nr_failed
+ retry
;