]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - mm/migrate.c
[PATCH] Swapless page migration: modify core logic
[karo-tx-linux.git] / mm / migrate.c
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>
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
28 #include "internal.h"
29
30 /* The maximum number of pages to take off the LRU for migration */
31 #define MIGRATE_CHUNK_SIZE 256
32
33 #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
34
35 /*
36  * Isolate one page from the LRU lists. If successful put it onto
37  * the indicated list with elevated page count.
38  *
39  * Result:
40  *  -EBUSY: page not on LRU list
41  *  0: page removed from LRU list and added to the specified list.
42  */
43 int isolate_lru_page(struct page *page, struct list_head *pagelist)
44 {
45         int ret = -EBUSY;
46
47         if (PageLRU(page)) {
48                 struct zone *zone = page_zone(page);
49
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);
60                 }
61                 spin_unlock_irq(&zone->lru_lock);
62         }
63         return ret;
64 }
65
66 /*
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().
70  */
71 int migrate_prep(void)
72 {
73         /*
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.
78          */
79         lru_add_drain_all();
80
81         return 0;
82 }
83
84 static inline void move_to_lru(struct page *page)
85 {
86         list_del(&page->lru);
87         if (PageActive(page)) {
88                 /*
89                  * lru_cache_add_active checks that
90                  * the PG_active bit is off.
91                  */
92                 ClearPageActive(page);
93                 lru_cache_add_active(page);
94         } else {
95                 lru_cache_add(page);
96         }
97         put_page(page);
98 }
99
100 /*
101  * Add isolated pages on the list back to the LRU.
102  *
103  * returns the number of pages put back.
104  */
105 int putback_lru_pages(struct list_head *l)
106 {
107         struct page *page;
108         struct page *page2;
109         int count = 0;
110
111         list_for_each_entry_safe(page, page2, l, lru) {
112                 move_to_lru(page);
113                 count++;
114         }
115         return count;
116 }
117
118 static inline int is_swap_pte(pte_t pte)
119 {
120         return !pte_none(pte) && !pte_present(pte) && !pte_file(pte);
121 }
122
123 /*
124  * Restore a potential migration pte to a working pte entry
125  */
126 static void remove_migration_pte(struct vm_area_struct *vma, unsigned long addr,
127                 struct page *old, struct page *new)
128 {
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;
136
137         pgd = pgd_offset(mm, addr);
138         if (!pgd_present(*pgd))
139                 return;
140
141         pud = pud_offset(pgd, addr);
142         if (!pud_present(*pud))
143                 return;
144
145         pmd = pmd_offset(pud, addr);
146         if (!pmd_present(*pmd))
147                 return;
148
149         ptep = pte_offset_map(pmd, addr);
150
151         if (!is_swap_pte(*ptep)) {
152                 pte_unmap(ptep);
153                 return;
154         }
155
156         ptl = pte_lockptr(mm, pmd);
157         spin_lock(ptl);
158         pte = *ptep;
159         if (!is_swap_pte(pte))
160                 goto out;
161
162         entry = pte_to_swp_entry(pte);
163
164         if (!is_migration_entry(entry) || migration_entry_to_page(entry) != old)
165                 goto out;
166
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);
176 }
177
178 /*
179  * Get rid of all migration entries and replace them by
180  * references to the indicated page.
181  *
182  * Must hold mmap_sem lock on at least one of the vmas containing
183  * the page so that the anon_vma cannot vanish.
184  */
185 static void remove_migration_ptes(struct page *old, struct page *new)
186 {
187         struct anon_vma *anon_vma;
188         struct vm_area_struct *vma;
189         unsigned long mapping;
190
191         mapping = (unsigned long)new->mapping;
192
193         if (!mapping || (mapping & PAGE_MAPPING_ANON) == 0)
194                 return;
195
196         /*
197          * We hold the mmap_sem lock. So no need to call page_lock_anon_vma.
198          */
199         anon_vma = (struct anon_vma *) (mapping - PAGE_MAPPING_ANON);
200         spin_lock(&anon_vma->lock);
201
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);
205
206         spin_unlock(&anon_vma->lock);
207 }
208
209 /*
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.
213  *
214  * This function is called from do_swap_page().
215  */
216 void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
217                                 unsigned long address)
218 {
219         pte_t *ptep, pte;
220         spinlock_t *ptl;
221         swp_entry_t entry;
222         struct page *page;
223
224         ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
225         pte = *ptep;
226         if (!is_swap_pte(pte))
227                 goto out;
228
229         entry = pte_to_swp_entry(pte);
230         if (!is_migration_entry(entry))
231                 goto out;
232
233         page = migration_entry_to_page(entry);
234
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);
242 }
243
244 /*
245  * Replace the page in the mapping.
246  *
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.
251  */
252 static int migrate_page_move_mapping(struct address_space *mapping,
253                 struct page *newpage, struct page *page)
254 {
255         struct page **radix_pointer;
256
257         if (!mapping) {
258                 /* Anonymous page */
259                 if (page_count(page) != 1)
260                         return -EAGAIN;
261                 return 0;
262         }
263
264         write_lock_irq(&mapping->tree_lock);
265
266         radix_pointer = (struct page **)radix_tree_lookup_slot(
267                                                 &mapping->page_tree,
268                                                 page_index(page));
269
270         if (page_count(page) != 2 + !!PagePrivate(page) ||
271                         *radix_pointer != page) {
272                 write_unlock_irq(&mapping->tree_lock);
273                 return -EAGAIN;
274         }
275
276         /*
277          * Now we know that no one else is looking at the page.
278          */
279         get_page(newpage);
280 #ifdef CONFIG_SWAP
281         if (PageSwapCache(page)) {
282                 SetPageSwapCache(newpage);
283                 set_page_private(newpage, page_private(page));
284         }
285 #endif
286
287         *radix_pointer = newpage;
288         __put_page(page);
289         write_unlock_irq(&mapping->tree_lock);
290
291         return 0;
292 }
293
294 /*
295  * Copy the page to its new location
296  */
297 static void migrate_page_copy(struct page *newpage, struct page *page)
298 {
299         copy_highpage(newpage, page);
300
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);
313
314         if (PageDirty(page)) {
315                 clear_page_dirty_for_io(page);
316                 set_page_dirty(newpage);
317         }
318
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;
326
327         /*
328          * If any waiters have accumulated on the new page then
329          * wake them up.
330          */
331         if (PageWriteback(newpage))
332                 end_page_writeback(newpage);
333 }
334
335 /************************************************************
336  *                    Migration functions
337  ***********************************************************/
338
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)
342 {
343         return -EIO;
344 }
345 EXPORT_SYMBOL(fail_migrate_page);
346
347 /*
348  * Common logic to directly migrate a single page suitable for
349  * pages that do not use PagePrivate.
350  *
351  * Pages are locked upon entry and exit.
352  */
353 int migrate_page(struct address_space *mapping,
354                 struct page *newpage, struct page *page)
355 {
356         int rc;
357
358         BUG_ON(PageWriteback(page));    /* Writeback must be complete */
359
360         rc = migrate_page_move_mapping(mapping, newpage, page);
361
362         if (rc)
363                 return rc;
364
365         migrate_page_copy(newpage, page);
366         return 0;
367 }
368 EXPORT_SYMBOL(migrate_page);
369
370 /*
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.
374  */
375 int buffer_migrate_page(struct address_space *mapping,
376                 struct page *newpage, struct page *page)
377 {
378         struct buffer_head *bh, *head;
379         int rc;
380
381         if (!page_has_buffers(page))
382                 return migrate_page(mapping, newpage, page);
383
384         head = page_buffers(page);
385
386         rc = migrate_page_move_mapping(mapping, newpage, page);
387
388         if (rc)
389                 return rc;
390
391         bh = head;
392         do {
393                 get_bh(bh);
394                 lock_buffer(bh);
395                 bh = bh->b_this_page;
396
397         } while (bh != head);
398
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);
404
405         bh = head;
406         do {
407                 set_bh_page(bh, newpage, bh_offset(bh));
408                 bh = bh->b_this_page;
409
410         } while (bh != head);
411
412         SetPagePrivate(newpage);
413
414         migrate_page_copy(newpage, page);
415
416         bh = head;
417         do {
418                 unlock_buffer(bh);
419                 put_bh(bh);
420                 bh = bh->b_this_page;
421
422         } while (bh != head);
423
424         return 0;
425 }
426 EXPORT_SYMBOL(buffer_migrate_page);
427
428 static int fallback_migrate_page(struct address_space *mapping,
429         struct page *newpage, struct page *page)
430 {
431         /*
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.
435          */
436         if (PageDirty(page)) {
437                 switch (pageout(page, mapping)) {
438                 case PAGE_KEEP:
439                 case PAGE_ACTIVATE:
440                         return -EAGAIN;
441
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;
447
448                 case PAGE_CLEAN:
449                         ; /* try to migrate the page below */
450                 }
451         }
452
453         /*
454          * Buffers may be managed in a filesystem specific way.
455          * We must have no buffers or drop them.
456          */
457         if (page_has_buffers(page) &&
458             !try_to_release_page(page, GFP_KERNEL))
459                 return -EAGAIN;
460
461         return migrate_page(mapping, newpage, page);
462 }
463
464 /*
465  * migrate_pages
466  *
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.
471  *
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.
475  *
476  * Return: Number of pages not migrated when "to" ran empty.
477  */
478 int migrate_pages(struct list_head *from, struct list_head *to,
479                   struct list_head *moved, struct list_head *failed)
480 {
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;
488
489         if (!swapwrite)
490                 current->flags |= PF_SWAPWRITE;
491
492 redo:
493         retry = 0;
494
495         list_for_each_entry_safe(page, page2, from, lru) {
496                 struct page *newpage = NULL;
497                 struct address_space *mapping;
498
499                 cond_resched();
500
501                 rc = 0;
502                 if (page_count(page) == 1)
503                         /* page was freed from under us. So we are done. */
504                         goto next;
505
506                 if (to && list_empty(to))
507                         break;
508
509                 /*
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.
514                  */
515                 rc = -EAGAIN;
516                 if (pass > 2)
517                         lock_page(page);
518                 else
519                         if (TestSetPageLocked(page))
520                                 goto next;
521
522                 /*
523                  * Only wait on writeback if we have already done a pass where
524                  * we we may have triggered writeouts for lots of pages.
525                  */
526                 if (pass > 0)
527                         wait_on_page_writeback(page);
528                 else
529                         if (PageWriteback(page))
530                                 goto unlock_page;
531
532                 /*
533                  * Establish migration ptes or remove ptes
534                  */
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;
539
540                 rc = -EAGAIN;
541                 if (page_mapped(page))
542                         goto unlock_page;
543
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;
549
550                 /*
551                  * Pages are properly locked and writeback is complete.
552                  * Try to migrate the page.
553                  */
554                 mapping = page_mapping(page);
555                 if (!mapping)
556                         rc = migrate_page(mapping, newpage, page);
557
558                 else if (mapping->a_ops->migratepage)
559                         /*
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.
565                          */
566                         rc = mapping->a_ops->migratepage(mapping,
567                                                         newpage, page);
568                 else
569                         rc = fallback_migrate_page(mapping, newpage, page);
570
571                 if (!rc)
572                         remove_migration_ptes(page, newpage);
573
574                 unlock_page(newpage);
575
576 unlock_page:
577                 if (rc)
578                         remove_migration_ptes(page, page);
579
580                 unlock_page(page);
581
582 next:
583                 if (rc) {
584                         if (newpage)
585                                 newpage->mapping = NULL;
586
587                         if (rc == -EAGAIN)
588                                 retry++;
589                         else {
590                                 /* Permanent failure */
591                                 list_move(&page->lru, failed);
592                                 nr_failed++;
593                         }
594                 } else {
595                         if (newpage) {
596                                 /* Successful migration. Return page to LRU */
597                                 move_to_lru(newpage);
598                         }
599                         list_move(&page->lru, moved);
600                 }
601         }
602         if (retry && pass++ < 10)
603                 goto redo;
604
605         if (!swapwrite)
606                 current->flags &= ~PF_SWAPWRITE;
607
608         return nr_failed + retry;
609 }
610
611 /*
612  * Migrate the list 'pagelist' of pages to a certain destination.
613  *
614  * Specify destination with either non-NULL vma or dest_node >= 0
615  * Return the number of pages not migrated or error code
616  */
617 int migrate_pages_to(struct list_head *pagelist,
618                         struct vm_area_struct *vma, int dest)
619 {
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;
628
629 redo:
630         nr_pages = 0;
631         list_for_each(p, pagelist) {
632                 if (vma) {
633                         /*
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.
638                          *
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.
642                          */
643                         page = alloc_page_vma(GFP_HIGHUSER, vma,
644                                         offset + vma->vm_start);
645                         offset += PAGE_SIZE;
646                 }
647                 else
648                         page = alloc_pages_node(dest, GFP_HIGHUSER, 0);
649
650                 if (!page) {
651                         err = -ENOMEM;
652                         goto out;
653                 }
654                 list_add_tail(&page->lru, &newlist);
655                 nr_pages++;
656                 if (nr_pages > MIGRATE_CHUNK_SIZE)
657                         break;
658         }
659         err = migrate_pages(pagelist, &newlist, &moved, &failed);
660
661         putback_lru_pages(&moved);      /* Call release pages instead ?? */
662
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);
671         }
672         list_splice(&failed, pagelist);
673         if (err < 0)
674                 return err;
675
676         /* Calculate number of leftover pages */
677         nr_pages = 0;
678         list_for_each(p, pagelist)
679                 nr_pages++;
680         return nr_pages;
681 }