blob: 251a8d158257a39fd87c1aa26f6f13f758048715 [file] [log] [blame]
Christoph Lameterb20a3502006-03-22 00:09:12 -08001/*
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>
Christoph Lameter06972122006-06-23 02:03:35 -070018#include <linux/swapops.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080019#include <linux/pagemap.h>
Christoph Lametere23ca002006-04-10 22:52:57 -070020#include <linux/buffer_head.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080021#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>
Christoph Lameter04e62a22006-06-23 02:03:38 -070027#include <linux/writeback.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080028
29#include "internal.h"
30
Christoph Lameterb20a3502006-03-22 00:09:12 -080031#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{
Christoph Lameterb20a3502006-03-22 00:09:12 -080071 /*
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{
Christoph Lameterb20a3502006-03-22 00:09:12 -080084 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) {
Christoph Lametere24f0b82006-06-23 02:03:51 -0700109 list_del(&page->lru);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800110 move_to_lru(page);
111 count++;
112 }
113 return count;
114}
115
Christoph Lameter06972122006-06-23 02:03:35 -0700116static 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 */
Christoph Lameter04e62a22006-06-23 02:03:38 -0700124static void remove_migration_pte(struct vm_area_struct *vma,
Christoph Lameter06972122006-06-23 02:03:35 -0700125 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;
Christoph Lameter04e62a22006-06-23 02:03:38 -0700134 unsigned long addr = page_address_in_vma(new, vma);
135
136 if (addr == -EFAULT)
137 return;
Christoph Lameter06972122006-06-23 02:03:35 -0700138
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
Christoph Lameter06972122006-06-23 02:03:35 -0700169 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);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700174
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
Christoph Lameter06972122006-06-23 02:03:35 -0700184out:
185 pte_unmap_unlock(ptep, ptl);
186}
187
188/*
Christoph Lameter04e62a22006-06-23 02:03:38 -0700189 * 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/*
Christoph Lameter06972122006-06-23 02:03:35 -0700211 * Must hold mmap_sem lock on at least one of the vmas containing
212 * the page so that the anon_vma cannot vanish.
213 */
Christoph Lameter04e62a22006-06-23 02:03:38 -0700214static void remove_anon_migration_ptes(struct page *old, struct page *new)
Christoph Lameter06972122006-06-23 02:03:35 -0700215{
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)
Christoph Lameter04e62a22006-06-23 02:03:38 -0700232 remove_migration_pte(vma, old, new);
Christoph Lameter06972122006-06-23 02:03:35 -0700233
234 spin_unlock(&anon_vma->lock);
235}
236
237/*
Christoph Lameter04e62a22006-06-23 02:03:38 -0700238 * 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
249/*
Christoph Lameter06972122006-06-23 02:03:35 -0700250 * 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
Christoph Lameterb20a3502006-03-22 00:09:12 -0800284/*
Christoph Lameterc3fcf8a2006-06-23 02:03:32 -0700285 * Replace the page in the mapping.
Christoph Lameter5b5c7122006-06-23 02:03:29 -0700286 *
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.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800291 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700292static int migrate_page_move_mapping(struct address_space *mapping,
293 struct page *newpage, struct page *page)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800294{
Christoph Lameterb20a3502006-03-22 00:09:12 -0800295 struct page **radix_pointer;
296
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700297 if (!mapping) {
298 /* Anonymous page */
299 if (page_count(page) != 1)
300 return -EAGAIN;
301 return 0;
302 }
303
Christoph Lameterb20a3502006-03-22 00:09:12 -0800304 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
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700310 if (page_count(page) != 2 + !!PagePrivate(page) ||
Christoph Lameterb20a3502006-03-22 00:09:12 -0800311 *radix_pointer != page) {
312 write_unlock_irq(&mapping->tree_lock);
Christoph Lametere23ca002006-04-10 22:52:57 -0700313 return -EAGAIN;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800314 }
315
316 /*
317 * Now we know that no one else is looking at the page.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800318 */
319 get_page(newpage);
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700320#ifdef CONFIG_SWAP
Christoph Lameterb20a3502006-03-22 00:09:12 -0800321 if (PageSwapCache(page)) {
322 SetPageSwapCache(newpage);
323 set_page_private(newpage, page_private(page));
324 }
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700325#endif
Christoph Lameterb20a3502006-03-22 00:09:12 -0800326
327 *radix_pointer = newpage;
328 __put_page(page);
329 write_unlock_irq(&mapping->tree_lock);
330
331 return 0;
332}
Christoph Lameterb20a3502006-03-22 00:09:12 -0800333
334/*
335 * Copy the page to its new location
336 */
Christoph Lametere7340f72006-06-23 02:03:29 -0700337static void migrate_page_copy(struct page *newpage, struct page *page)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800338{
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
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700359#ifdef CONFIG_SWAP
Christoph Lameterb20a3502006-03-22 00:09:12 -0800360 ClearPageSwapCache(page);
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700361#endif
Christoph Lameterb20a3502006-03-22 00:09:12 -0800362 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}
Christoph Lameterb20a3502006-03-22 00:09:12 -0800374
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700375/************************************************************
376 * Migration functions
377 ***********************************************************/
378
379/* Always fail migration. Used for mappings that are not movable */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700380int fail_migrate_page(struct address_space *mapping,
381 struct page *newpage, struct page *page)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700382{
383 return -EIO;
384}
385EXPORT_SYMBOL(fail_migrate_page);
386
Christoph Lameterb20a3502006-03-22 00:09:12 -0800387/*
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 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700393int migrate_page(struct address_space *mapping,
394 struct page *newpage, struct page *page)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800395{
396 int rc;
397
398 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
399
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700400 rc = migrate_page_move_mapping(mapping, newpage, page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800401
402 if (rc)
403 return rc;
404
405 migrate_page_copy(newpage, page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800406 return 0;
407}
408EXPORT_SYMBOL(migrate_page);
409
410/*
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700411 * 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 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700415int buffer_migrate_page(struct address_space *mapping,
416 struct page *newpage, struct page *page)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700417{
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700418 struct buffer_head *bh, *head;
419 int rc;
420
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700421 if (!page_has_buffers(page))
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700422 return migrate_page(mapping, newpage, page);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700423
424 head = page_buffers(page);
425
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700426 rc = migrate_page_move_mapping(mapping, newpage, page);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700427
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
Christoph Lameter04e62a22006-06-23 02:03:38 -0700468/*
469 * Writeback a page to clean the dirty state
470 */
471static int writeout(struct address_space *mapping, struct page *page)
472{
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
491 /*
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.
498 */
499 remove_migration_ptes(page, page);
500
501 rc = mapping->a_ops->writepage(page, &wbc);
502 if (rc < 0)
503 /* I/O Error writing */
504 return -EIO;
505
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 */
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700516static int fallback_migrate_page(struct address_space *mapping,
517 struct page *newpage, struct page *page)
518{
Christoph Lameter04e62a22006-06-23 02:03:38 -0700519 if (PageDirty(page))
520 return writeout(mapping, page);
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700521
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
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700533/*
Christoph Lametere24f0b82006-06-23 02:03:51 -0700534 * 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 */
Christoph Lameter95a402c2006-06-23 02:03:53 -0700587static int unmap_and_move(new_page_t get_new_page, unsigned long private,
588 struct page *page, int force)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700589{
590 int rc = 0;
Christoph Lameter95a402c2006-06-23 02:03:53 -0700591 struct page *newpage = get_new_page(page, private);
592
593 if (!newpage)
594 return -ENOMEM;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700595
596 if (page_count(page) == 1)
597 /* page was freed from under us. So we are done. */
Christoph Lameter95a402c2006-06-23 02:03:53 -0700598 goto move_newpage;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700599
600 rc = -EAGAIN;
601 if (TestSetPageLocked(page)) {
602 if (!force)
Christoph Lameter95a402c2006-06-23 02:03:53 -0700603 goto move_newpage;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700604 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);
Christoph Lameter95a402c2006-06-23 02:03:53 -0700627
Christoph Lametere24f0b82006-06-23 02:03:51 -0700628 if (rc != -EAGAIN) {
Christoph Lameteraaa994b2006-06-23 02:03:52 -0700629 /*
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);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700637 }
Christoph Lameter95a402c2006-06-23 02:03:53 -0700638
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);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700645 return rc;
646}
647
648/*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800649 * migrate_pages
650 *
Christoph Lameter95a402c2006-06-23 02:03:53 -0700651 * 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.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800654 *
655 * The function returns after 10 attempts or if no pages
656 * are movable anymore because to has become empty
Christoph Lameteraaa994b2006-06-23 02:03:52 -0700657 * or no retryable pages exist anymore. All pages will be
658 * retruned to the LRU or freed.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800659 *
Christoph Lameter95a402c2006-06-23 02:03:53 -0700660 * Return: Number of pages not migrated or error code.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800661 */
Christoph Lameter95a402c2006-06-23 02:03:53 -0700662int migrate_pages(struct list_head *from,
663 new_page_t get_new_page, unsigned long private)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800664{
Christoph Lametere24f0b82006-06-23 02:03:51 -0700665 int retry = 1;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800666 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
Christoph Lametere24f0b82006-06-23 02:03:51 -0700676 for(pass = 0; pass < 10 && retry; pass++) {
677 retry = 0;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800678
Christoph Lametere24f0b82006-06-23 02:03:51 -0700679 list_for_each_entry_safe(page, page2, from, lru) {
Christoph Lametere24f0b82006-06-23 02:03:51 -0700680 cond_resched();
Christoph Lameterb20a3502006-03-22 00:09:12 -0800681
Christoph Lameter95a402c2006-06-23 02:03:53 -0700682 rc = unmap_and_move(get_new_page, private,
683 page, pass > 2);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800684
Christoph Lametere24f0b82006-06-23 02:03:51 -0700685 switch(rc) {
Christoph Lameter95a402c2006-06-23 02:03:53 -0700686 case -ENOMEM:
687 goto out;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700688 case -EAGAIN:
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700689 retry++;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700690 break;
691 case 0:
Christoph Lametere24f0b82006-06-23 02:03:51 -0700692 break;
693 default:
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700694 /* Permanent failure */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700695 nr_failed++;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700696 break;
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700697 }
Christoph Lameterb20a3502006-03-22 00:09:12 -0800698 }
699 }
Christoph Lameter95a402c2006-06-23 02:03:53 -0700700 rc = 0;
701out:
Christoph Lameterb20a3502006-03-22 00:09:12 -0800702 if (!swapwrite)
703 current->flags &= ~PF_SWAPWRITE;
704
Christoph Lameteraaa994b2006-06-23 02:03:52 -0700705 putback_lru_pages(from);
Christoph Lameter95a402c2006-06-23 02:03:53 -0700706
707 if (rc)
708 return rc;
709
Christoph Lameterb20a3502006-03-22 00:09:12 -0800710 return nr_failed + retry;
711}
712