[PATCH] page migration cleanup: pass "mapping" to migration functions
[deliverable/linux.git] / mm / migrate.c
index be3f141e53a42a4aad9d4b5678f06a3f343154d2..f65e69d9452711a12e00897dd4e00733fbb863d0 100644 (file)
@@ -166,59 +166,26 @@ 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.
  */
-static int migrate_page_remove_references(struct page *newpage,
-                               struct page *page, int nr_refs)
+static int migrate_page_move_mapping(struct address_space *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))
-               return -EAGAIN;
-
        write_lock_irq(&mapping->tree_lock);
 
        radix_pointer = (struct page **)radix_tree_lookup_slot(
                                                &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;
@@ -226,15 +193,8 @@ static int migrate_page_remove_references(struct page *newpage,
 
        /*
         * Now we know that no one else is looking at the page.
-        *
-        * Certain minimal information about a page must be available
-        * in order for other subsystems to properly handle the page if they
-        * find it through the radix tree update before we are finished
-        * copying the page.
         */
        get_page(newpage);
-       newpage->index = page->index;
-       newpage->mapping = page->mapping;
        if (PageSwapCache(page)) {
                SetPageSwapCache(newpage);
                set_page_private(newpage, page_private(page));
@@ -291,7 +251,8 @@ static void migrate_page_copy(struct page *newpage, struct page *page)
  ***********************************************************/
 
 /* Always fail migration. Used for mappings that are not movable */
-int fail_migrate_page(struct page *newpage, struct page *page)
+int fail_migrate_page(struct address_space *mapping,
+                       struct page *newpage, struct page *page)
 {
        return -EIO;
 }
@@ -303,13 +264,14 @@ EXPORT_SYMBOL(fail_migrate_page);
  *
  * Pages are locked upon entry and exit.
  */
-int migrate_page(struct page *newpage, struct page *page)
+int migrate_page(struct address_space *mapping,
+               struct page *newpage, struct page *page)
 {
        int rc;
 
        BUG_ON(PageWriteback(page));    /* Writeback must be complete */
 
-       rc = migrate_page_remove_references(newpage, page, 2);
+       rc = migrate_page_move_mapping(mapping, newpage, page);
 
        if (rc)
                return rc;
@@ -334,21 +296,18 @@ EXPORT_SYMBOL(migrate_page);
  * if the underlying filesystem guarantees that no other references to "page"
  * exist.
  */
-int buffer_migrate_page(struct page *newpage, struct page *page)
+int buffer_migrate_page(struct address_space *mapping,
+               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);
+               return migrate_page(mapping, newpage, page);
 
        head = page_buffers(page);
 
-       rc = migrate_page_remove_references(newpage, page, 3);
+       rc = migrate_page_move_mapping(mapping, newpage, page);
 
        if (rc)
                return rc;
@@ -477,8 +436,39 @@ redo:
                        goto next;
                }
 
+               /*
+                * 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_page;
+
+               rc = -EAGAIN;
+               if (page_mapped(page))
+                       goto unlock_page;
+
                newpage = lru_to_page(to);
                lock_page(newpage);
+               /* Prepare mapping for the new page.*/
+               newpage->index = page->index;
+               newpage->mapping = page->mapping;
 
                /*
                 * Pages are properly locked and writeback is complete.
@@ -496,21 +486,11 @@ redo:
                         * own migration function. This is the most common
                         * path for page migration.
                         */
-                       rc = mapping->a_ops->migratepage(newpage, page);
+                       rc = mapping->a_ops->migratepage(mapping,
+                                                       newpage, page);
                        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
@@ -537,7 +517,7 @@ redo:
                 */
                if (!page_has_buffers(page) ||
                    try_to_release_page(page, GFP_KERNEL)) {
-                       rc = migrate_page(newpage, page);
+                       rc = migrate_page(mapping, newpage, page);
                        goto unlock_both;
                }
 
@@ -566,12 +546,17 @@ unlock_page:
                unlock_page(page);
 
 next:
-               if (rc == -EAGAIN) {
-                       retry++;
-               } else if (rc) {
-                       /* Permanent failure */
-                       list_move(&page->lru, failed);
-                       nr_failed++;
+               if (rc) {
+                       if (newpage)
+                               newpage->mapping = NULL;
+
+                       if (rc == -EAGAIN)
+                               retry++;
+                       else {
+                               /* Permanent failure */
+                               list_move(&page->lru, failed);
+                               nr_failed++;
+                       }
                } else {
                        if (newpage) {
                                /* Successful migration. Return page to LRU */
This page took 0.033203 seconds and 5 git commands to generate.