]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - mm/migrate.c
[PATCH] page migration cleanup: move fallback handling into special function
[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/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>
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         /* Must have swap device for migration */
74         if (nr_swap_pages <= 0)
75                 return -ENODEV;
76
77         /*
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.
82          */
83         lru_add_drain_all();
84
85         return 0;
86 }
87
88 static inline void move_to_lru(struct page *page)
89 {
90         list_del(&page->lru);
91         if (PageActive(page)) {
92                 /*
93                  * lru_cache_add_active checks that
94                  * the PG_active bit is off.
95                  */
96                 ClearPageActive(page);
97                 lru_cache_add_active(page);
98         } else {
99                 lru_cache_add(page);
100         }
101         put_page(page);
102 }
103
104 /*
105  * Add isolated pages on the list back to the LRU.
106  *
107  * returns the number of pages put back.
108  */
109 int putback_lru_pages(struct list_head *l)
110 {
111         struct page *page;
112         struct page *page2;
113         int count = 0;
114
115         list_for_each_entry_safe(page, page2, l, lru) {
116                 move_to_lru(page);
117                 count++;
118         }
119         return count;
120 }
121
122 /*
123  * swapout a single page
124  * page is locked upon entry, unlocked on exit
125  */
126 static int swap_page(struct page *page)
127 {
128         struct address_space *mapping = page_mapping(page);
129
130         if (page_mapped(page) && mapping)
131                 if (try_to_unmap(page, 1) != SWAP_SUCCESS)
132                         goto unlock_retry;
133
134         if (PageDirty(page)) {
135                 /* Page is dirty, try to write it out here */
136                 switch(pageout(page, mapping)) {
137                 case PAGE_KEEP:
138                 case PAGE_ACTIVATE:
139                         goto unlock_retry;
140
141                 case PAGE_SUCCESS:
142                         goto retry;
143
144                 case PAGE_CLEAN:
145                         ; /* try to free the page below */
146                 }
147         }
148
149         if (PagePrivate(page)) {
150                 if (!try_to_release_page(page, GFP_KERNEL) ||
151                     (!mapping && page_count(page) == 1))
152                         goto unlock_retry;
153         }
154
155         if (remove_mapping(mapping, page)) {
156                 /* Success */
157                 unlock_page(page);
158                 return 0;
159         }
160
161 unlock_retry:
162         unlock_page(page);
163
164 retry:
165         return -EAGAIN;
166 }
167
168 /*
169  * Replace the page in the mapping.
170  *
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.
175  */
176 static int migrate_page_move_mapping(struct address_space *mapping,
177                 struct page *newpage, struct page *page)
178 {
179         struct page **radix_pointer;
180
181         write_lock_irq(&mapping->tree_lock);
182
183         radix_pointer = (struct page **)radix_tree_lookup_slot(
184                                                 &mapping->page_tree,
185                                                 page_index(page));
186
187         if (!page_mapping(page) ||
188                         page_count(page) != 2 + !!PagePrivate(page) ||
189                         *radix_pointer != page) {
190                 write_unlock_irq(&mapping->tree_lock);
191                 return -EAGAIN;
192         }
193
194         /*
195          * Now we know that no one else is looking at the page.
196          */
197         get_page(newpage);
198         if (PageSwapCache(page)) {
199                 SetPageSwapCache(newpage);
200                 set_page_private(newpage, page_private(page));
201         }
202
203         *radix_pointer = newpage;
204         __put_page(page);
205         write_unlock_irq(&mapping->tree_lock);
206
207         return 0;
208 }
209
210 /*
211  * Copy the page to its new location
212  */
213 static void migrate_page_copy(struct page *newpage, struct page *page)
214 {
215         copy_highpage(newpage, page);
216
217         if (PageError(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);
229
230         if (PageDirty(page)) {
231                 clear_page_dirty_for_io(page);
232                 set_page_dirty(newpage);
233         }
234
235         ClearPageSwapCache(page);
236         ClearPageActive(page);
237         ClearPagePrivate(page);
238         set_page_private(page, 0);
239         page->mapping = NULL;
240
241         /*
242          * If any waiters have accumulated on the new page then
243          * wake them up.
244          */
245         if (PageWriteback(newpage))
246                 end_page_writeback(newpage);
247 }
248
249 /************************************************************
250  *                    Migration functions
251  ***********************************************************/
252
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)
256 {
257         return -EIO;
258 }
259 EXPORT_SYMBOL(fail_migrate_page);
260
261 /*
262  * Common logic to directly migrate a single page suitable for
263  * pages that do not use PagePrivate.
264  *
265  * Pages are locked upon entry and exit.
266  */
267 int migrate_page(struct address_space *mapping,
268                 struct page *newpage, struct page *page)
269 {
270         int rc;
271
272         BUG_ON(PageWriteback(page));    /* Writeback must be complete */
273
274         rc = migrate_page_move_mapping(mapping, newpage, page);
275
276         if (rc)
277                 return rc;
278
279         migrate_page_copy(newpage, page);
280
281         /*
282          * Remove auxiliary swap entries and replace
283          * them with real ptes.
284          *
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.
288          */
289         remove_from_swap(newpage);
290         return 0;
291 }
292 EXPORT_SYMBOL(migrate_page);
293
294 /*
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"
297  * exist.
298  */
299 int buffer_migrate_page(struct address_space *mapping,
300                 struct page *newpage, struct page *page)
301 {
302         struct buffer_head *bh, *head;
303         int rc;
304
305         if (!page_has_buffers(page))
306                 return migrate_page(mapping, newpage, page);
307
308         head = page_buffers(page);
309
310         rc = migrate_page_move_mapping(mapping, newpage, page);
311
312         if (rc)
313                 return rc;
314
315         bh = head;
316         do {
317                 get_bh(bh);
318                 lock_buffer(bh);
319                 bh = bh->b_this_page;
320
321         } while (bh != head);
322
323         ClearPagePrivate(page);
324         set_page_private(newpage, page_private(page));
325         set_page_private(page, 0);
326         put_page(page);
327         get_page(newpage);
328
329         bh = head;
330         do {
331                 set_bh_page(bh, newpage, bh_offset(bh));
332                 bh = bh->b_this_page;
333
334         } while (bh != head);
335
336         SetPagePrivate(newpage);
337
338         migrate_page_copy(newpage, page);
339
340         bh = head;
341         do {
342                 unlock_buffer(bh);
343                 put_bh(bh);
344                 bh = bh->b_this_page;
345
346         } while (bh != head);
347
348         return 0;
349 }
350 EXPORT_SYMBOL(buffer_migrate_page);
351
352 static int fallback_migrate_page(struct address_space *mapping,
353         struct page *newpage, struct page *page)
354 {
355         /*
356          * Default handling if a filesystem does not provide
357          * a migration function. We can only migrate clean
358          * pages so try to write out any dirty pages first.
359          */
360         if (PageDirty(page)) {
361                 switch (pageout(page, mapping)) {
362                 case PAGE_KEEP:
363                 case PAGE_ACTIVATE:
364                         return -EAGAIN;
365
366                 case PAGE_SUCCESS:
367                         /* Relock since we lost the lock */
368                         lock_page(page);
369                         /* Must retry since page state may have changed */
370                         return -EAGAIN;
371
372                 case PAGE_CLEAN:
373                         ; /* try to migrate the page below */
374                 }
375         }
376
377         /*
378          * Buffers may be managed in a filesystem specific way.
379          * We must have no buffers or drop them.
380          */
381         if (page_has_buffers(page) &&
382             !try_to_release_page(page, GFP_KERNEL))
383                 return -EAGAIN;
384
385         return migrate_page(mapping, newpage, page);
386 }
387
388 /*
389  * migrate_pages
390  *
391  * Two lists are passed to this function. The first list
392  * contains the pages isolated from the LRU to be migrated.
393  * The second list contains new pages that the pages isolated
394  * can be moved to. If the second list is NULL then all
395  * pages are swapped out.
396  *
397  * The function returns after 10 attempts or if no pages
398  * are movable anymore because to has become empty
399  * or no retryable pages exist anymore.
400  *
401  * Return: Number of pages not migrated when "to" ran empty.
402  */
403 int migrate_pages(struct list_head *from, struct list_head *to,
404                   struct list_head *moved, struct list_head *failed)
405 {
406         int retry;
407         int nr_failed = 0;
408         int pass = 0;
409         struct page *page;
410         struct page *page2;
411         int swapwrite = current->flags & PF_SWAPWRITE;
412         int rc;
413
414         if (!swapwrite)
415                 current->flags |= PF_SWAPWRITE;
416
417 redo:
418         retry = 0;
419
420         list_for_each_entry_safe(page, page2, from, lru) {
421                 struct page *newpage = NULL;
422                 struct address_space *mapping;
423
424                 cond_resched();
425
426                 rc = 0;
427                 if (page_count(page) == 1)
428                         /* page was freed from under us. So we are done. */
429                         goto next;
430
431                 if (to && list_empty(to))
432                         break;
433
434                 /*
435                  * Skip locked pages during the first two passes to give the
436                  * functions holding the lock time to release the page. Later we
437                  * use lock_page() to have a higher chance of acquiring the
438                  * lock.
439                  */
440                 rc = -EAGAIN;
441                 if (pass > 2)
442                         lock_page(page);
443                 else
444                         if (TestSetPageLocked(page))
445                                 goto next;
446
447                 /*
448                  * Only wait on writeback if we have already done a pass where
449                  * we we may have triggered writeouts for lots of pages.
450                  */
451                 if (pass > 0) {
452                         wait_on_page_writeback(page);
453                 } else {
454                         if (PageWriteback(page))
455                                 goto unlock_page;
456                 }
457
458                 /*
459                  * Anonymous pages must have swap cache references otherwise
460                  * the information contained in the page maps cannot be
461                  * preserved.
462                  */
463                 if (PageAnon(page) && !PageSwapCache(page)) {
464                         if (!add_to_swap(page, GFP_KERNEL)) {
465                                 rc = -ENOMEM;
466                                 goto unlock_page;
467                         }
468                 }
469
470                 if (!to) {
471                         rc = swap_page(page);
472                         goto next;
473                 }
474
475                 /*
476                  * Establish swap ptes for anonymous pages or destroy pte
477                  * maps for files.
478                  *
479                  * In order to reestablish file backed mappings the fault handlers
480                  * will take the radix tree_lock which may then be used to stop
481                  * processses from accessing this page until the new page is ready.
482                  *
483                  * A process accessing via a swap pte (an anonymous page) will take a
484                  * page_lock on the old page which will block the process until the
485                  * migration attempt is complete. At that time the PageSwapCache bit
486                  * will be examined. If the page was migrated then the PageSwapCache
487                  * bit will be clear and the operation to retrieve the page will be
488                  * retried which will find the new page in the radix tree. Then a new
489                  * direct mapping may be generated based on the radix tree contents.
490                  *
491                  * If the page was not migrated then the PageSwapCache bit
492                  * is still set and the operation may continue.
493                  */
494                 rc = -EPERM;
495                 if (try_to_unmap(page, 1) == SWAP_FAIL)
496                         /* A vma has VM_LOCKED set -> permanent failure */
497                         goto unlock_page;
498
499                 rc = -EAGAIN;
500                 if (page_mapped(page))
501                         goto unlock_page;
502
503                 newpage = lru_to_page(to);
504                 lock_page(newpage);
505                 /* Prepare mapping for the new page.*/
506                 newpage->index = page->index;
507                 newpage->mapping = page->mapping;
508
509                 /*
510                  * Pages are properly locked and writeback is complete.
511                  * Try to migrate the page.
512                  */
513                 mapping = page_mapping(page);
514                 if (!mapping)
515                         goto unlock_both;
516
517                 if (mapping->a_ops->migratepage)
518                         /*
519                          * Most pages have a mapping and most filesystems
520                          * should provide a migration function. Anonymous
521                          * pages are part of swap space which also has its
522                          * own migration function. This is the most common
523                          * path for page migration.
524                          */
525                         rc = mapping->a_ops->migratepage(mapping,
526                                                         newpage, page);
527                 else
528                         rc = fallback_migrate_page(mapping, newpage, page);
529
530 unlock_both:
531                 unlock_page(newpage);
532
533 unlock_page:
534                 unlock_page(page);
535
536 next:
537                 if (rc) {
538                         if (newpage)
539                                 newpage->mapping = NULL;
540
541                         if (rc == -EAGAIN)
542                                 retry++;
543                         else {
544                                 /* Permanent failure */
545                                 list_move(&page->lru, failed);
546                                 nr_failed++;
547                         }
548                 } else {
549                         if (newpage) {
550                                 /* Successful migration. Return page to LRU */
551                                 move_to_lru(newpage);
552                         }
553                         list_move(&page->lru, moved);
554                 }
555         }
556         if (retry && pass++ < 10)
557                 goto redo;
558
559         if (!swapwrite)
560                 current->flags &= ~PF_SWAPWRITE;
561
562         return nr_failed + retry;
563 }
564
565 /*
566  * Migrate the list 'pagelist' of pages to a certain destination.
567  *
568  * Specify destination with either non-NULL vma or dest_node >= 0
569  * Return the number of pages not migrated or error code
570  */
571 int migrate_pages_to(struct list_head *pagelist,
572                         struct vm_area_struct *vma, int dest)
573 {
574         LIST_HEAD(newlist);
575         LIST_HEAD(moved);
576         LIST_HEAD(failed);
577         int err = 0;
578         unsigned long offset = 0;
579         int nr_pages;
580         struct page *page;
581         struct list_head *p;
582
583 redo:
584         nr_pages = 0;
585         list_for_each(p, pagelist) {
586                 if (vma) {
587                         /*
588                          * The address passed to alloc_page_vma is used to
589                          * generate the proper interleave behavior. We fake
590                          * the address here by an increasing offset in order
591                          * to get the proper distribution of pages.
592                          *
593                          * No decision has been made as to which page
594                          * a certain old page is moved to so we cannot
595                          * specify the correct address.
596                          */
597                         page = alloc_page_vma(GFP_HIGHUSER, vma,
598                                         offset + vma->vm_start);
599                         offset += PAGE_SIZE;
600                 }
601                 else
602                         page = alloc_pages_node(dest, GFP_HIGHUSER, 0);
603
604                 if (!page) {
605                         err = -ENOMEM;
606                         goto out;
607                 }
608                 list_add_tail(&page->lru, &newlist);
609                 nr_pages++;
610                 if (nr_pages > MIGRATE_CHUNK_SIZE)
611                         break;
612         }
613         err = migrate_pages(pagelist, &newlist, &moved, &failed);
614
615         putback_lru_pages(&moved);      /* Call release pages instead ?? */
616
617         if (err >= 0 && list_empty(&newlist) && !list_empty(pagelist))
618                 goto redo;
619 out:
620         /* Return leftover allocated pages */
621         while (!list_empty(&newlist)) {
622                 page = list_entry(newlist.next, struct page, lru);
623                 list_del(&page->lru);
624                 __free_page(page);
625         }
626         list_splice(&failed, pagelist);
627         if (err < 0)
628                 return err;
629
630         /* Calculate number of leftover pages */
631         nr_pages = 0;
632         list_for_each(p, pagelist)
633                 nr_pages++;
634         return nr_pages;
635 }