]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - mm/migrate.c
[PATCH] page migration cleanup: extract try_to_unmap from migration functions
[linux-2.6-omap-h63xx.git] / mm / migrate.c
index 1c25040693d2565b8f1a5affc6eb5bb2310cf15e..8095c607a494d11582500fed427bea090710443b 100644 (file)
@@ -119,15 +119,6 @@ int putback_lru_pages(struct list_head *l)
        return count;
 }
 
-/*
- * Non migratable page
- */
-int fail_migrate_page(struct page *newpage, struct page *page)
-{
-       return -EIO;
-}
-EXPORT_SYMBOL(fail_migrate_page);
-
 /*
  * swapout a single page
  * page is locked upon entry, unlocked on exit
@@ -175,50 +166,20 @@ retry:
 }
 
 /*
- * Remove references for a page and establish the new page with the correct
- * basic settings to be able to stop accesses to the page.
+ * Replace the page in the mapping.
+ *
+ * The number of remaining references must be:
+ * 1 for anonymous pages without a mapping
+ * 2 for pages with a mapping
+ * 3 for pages with a mapping and PagePrivate set.
  */
-int migrate_page_remove_references(struct page *newpage,
-                               struct page *page, int nr_refs)
+static int migrate_page_move_mapping(struct page *newpage,
+                               struct page *page)
 {
        struct address_space *mapping = page_mapping(page);
        struct page **radix_pointer;
 
-       /*
-        * Avoid doing any of the following work if the page count
-        * indicates that the page is in use or truncate has removed
-        * the page.
-        */
-       if (!mapping || page_mapcount(page) + nr_refs != page_count(page))
-               return -EAGAIN;
-
-       /*
-        * Establish swap ptes for anonymous pages or destroy pte
-        * maps for files.
-        *
-        * In order to reestablish file backed mappings the fault handlers
-        * will take the radix tree_lock which may then be used to stop
-        * processses from accessing this page until the new page is ready.
-        *
-        * A process accessing via a swap pte (an anonymous page) will take a
-        * page_lock on the old page which will block the process until the
-        * migration attempt is complete. At that time the PageSwapCache bit
-        * will be examined. If the page was migrated then the PageSwapCache
-        * bit will be clear and the operation to retrieve the page will be
-        * retried which will find the new page in the radix tree. Then a new
-        * direct mapping may be generated based on the radix tree contents.
-        *
-        * If the page was not migrated then the PageSwapCache bit
-        * is still set and the operation may continue.
-        */
-       if (try_to_unmap(page, 1) == SWAP_FAIL)
-               /* A vma has VM_LOCKED set -> permanent failure */
-               return -EPERM;
-
-       /*
-        * Give up if we were unable to remove all mappings.
-        */
-       if (page_mapcount(page))
+       if (!mapping)
                return -EAGAIN;
 
        write_lock_irq(&mapping->tree_lock);
@@ -227,7 +188,8 @@ int migrate_page_remove_references(struct page *newpage,
                                                &mapping->page_tree,
                                                page_index(page));
 
-       if (!page_mapping(page) || page_count(page) != nr_refs ||
+       if (!page_mapping(page) ||
+                       page_count(page) != 2 + !!PagePrivate(page) ||
                        *radix_pointer != page) {
                write_unlock_irq(&mapping->tree_lock);
                return -EAGAIN;
@@ -255,12 +217,11 @@ int migrate_page_remove_references(struct page *newpage,
 
        return 0;
 }
-EXPORT_SYMBOL(migrate_page_remove_references);
 
 /*
  * Copy the page to its new location
  */
-void migrate_page_copy(struct page *newpage, struct page *page)
+static void migrate_page_copy(struct page *newpage, struct page *page)
 {
        copy_highpage(newpage, page);
 
@@ -295,7 +256,17 @@ void migrate_page_copy(struct page *newpage, struct page *page)
        if (PageWriteback(newpage))
                end_page_writeback(newpage);
 }
-EXPORT_SYMBOL(migrate_page_copy);
+
+/************************************************************
+ *                    Migration functions
+ ***********************************************************/
+
+/* Always fail migration. Used for mappings that are not movable */
+int fail_migrate_page(struct page *newpage, struct page *page)
+{
+       return -EIO;
+}
+EXPORT_SYMBOL(fail_migrate_page);
 
 /*
  * Common logic to directly migrate a single page suitable for
@@ -309,7 +280,7 @@ int migrate_page(struct page *newpage, struct page *page)
 
        BUG_ON(PageWriteback(page));    /* Writeback must be complete */
 
-       rc = migrate_page_remove_references(newpage, page, 2);
+       rc = migrate_page_move_mapping(newpage, page);
 
        if (rc)
                return rc;
@@ -329,6 +300,67 @@ int migrate_page(struct page *newpage, struct page *page)
 }
 EXPORT_SYMBOL(migrate_page);
 
+/*
+ * Migration function for pages with buffers. This function can only be used
+ * if the underlying filesystem guarantees that no other references to "page"
+ * exist.
+ */
+int buffer_migrate_page(struct page *newpage, struct page *page)
+{
+       struct address_space *mapping = page->mapping;
+       struct buffer_head *bh, *head;
+       int rc;
+
+       if (!mapping)
+               return -EAGAIN;
+
+       if (!page_has_buffers(page))
+               return migrate_page(newpage, page);
+
+       head = page_buffers(page);
+
+       rc = migrate_page_move_mapping(newpage, page);
+
+       if (rc)
+               return rc;
+
+       bh = head;
+       do {
+               get_bh(bh);
+               lock_buffer(bh);
+               bh = bh->b_this_page;
+
+       } while (bh != head);
+
+       ClearPagePrivate(page);
+       set_page_private(newpage, page_private(page));
+       set_page_private(page, 0);
+       put_page(page);
+       get_page(newpage);
+
+       bh = head;
+       do {
+               set_bh_page(bh, newpage, bh_offset(bh));
+               bh = bh->b_this_page;
+
+       } while (bh != head);
+
+       SetPagePrivate(newpage);
+
+       migrate_page_copy(newpage, page);
+
+       bh = head;
+       do {
+               unlock_buffer(bh);
+               put_bh(bh);
+               bh = bh->b_this_page;
+
+       } while (bh != head);
+
+       return 0;
+}
+EXPORT_SYMBOL(buffer_migrate_page);
+
 /*
  * migrate_pages
  *
@@ -419,6 +451,33 @@ redo:
                newpage = lru_to_page(to);
                lock_page(newpage);
 
+               /*
+                * Establish swap ptes for anonymous pages or destroy pte
+                * maps for files.
+                *
+                * In order to reestablish file backed mappings the fault handlers
+                * will take the radix tree_lock which may then be used to stop
+                * processses from accessing this page until the new page is ready.
+                *
+                * A process accessing via a swap pte (an anonymous page) will take a
+                * page_lock on the old page which will block the process until the
+                * migration attempt is complete. At that time the PageSwapCache bit
+                * will be examined. If the page was migrated then the PageSwapCache
+                * bit will be clear and the operation to retrieve the page will be
+                * retried which will find the new page in the radix tree. Then a new
+                * direct mapping may be generated based on the radix tree contents.
+                *
+                * If the page was not migrated then the PageSwapCache bit
+                * is still set and the operation may continue.
+                */
+               rc = -EPERM;
+               if (try_to_unmap(page, 1) == SWAP_FAIL)
+                       /* A vma has VM_LOCKED set -> permanent failure */
+                       goto unlock_both;
+
+               rc = -EAGAIN;
+               if (page_mapped(page))
+                       goto unlock_both;
                /*
                 * Pages are properly locked and writeback is complete.
                 * Try to migrate the page.
@@ -439,17 +498,6 @@ redo:
                        goto unlock_both;
                 }
 
-               /* Make sure the dirty bit is up to date */
-               if (try_to_unmap(page, 1) == SWAP_FAIL) {
-                       rc = -EPERM;
-                       goto unlock_both;
-               }
-
-               if (page_mapcount(page)) {
-                       rc = -EAGAIN;
-                       goto unlock_both;
-               }
-
                /*
                 * Default handling if a filesystem does not provide
                 * a migration function. We can only migrate clean
@@ -528,67 +576,6 @@ next:
        return nr_failed + retry;
 }
 
-/*
- * Migration function for pages with buffers. This function can only be used
- * if the underlying filesystem guarantees that no other references to "page"
- * exist.
- */
-int buffer_migrate_page(struct page *newpage, struct page *page)
-{
-       struct address_space *mapping = page->mapping;
-       struct buffer_head *bh, *head;
-       int rc;
-
-       if (!mapping)
-               return -EAGAIN;
-
-       if (!page_has_buffers(page))
-               return migrate_page(newpage, page);
-
-       head = page_buffers(page);
-
-       rc = migrate_page_remove_references(newpage, page, 3);
-
-       if (rc)
-               return rc;
-
-       bh = head;
-       do {
-               get_bh(bh);
-               lock_buffer(bh);
-               bh = bh->b_this_page;
-
-       } while (bh != head);
-
-       ClearPagePrivate(page);
-       set_page_private(newpage, page_private(page));
-       set_page_private(page, 0);
-       put_page(page);
-       get_page(newpage);
-
-       bh = head;
-       do {
-               set_bh_page(bh, newpage, bh_offset(bh));
-               bh = bh->b_this_page;
-
-       } while (bh != head);
-
-       SetPagePrivate(newpage);
-
-       migrate_page_copy(newpage, page);
-
-       bh = head;
-       do {
-               unlock_buffer(bh);
-               put_bh(bh);
-               bh = bh->b_this_page;
-
-       } while (bh != head);
-
-       return 0;
-}
-EXPORT_SYMBOL(buffer_migrate_page);
-
 /*
  * Migrate the list 'pagelist' of pages to a certain destination.
  *