Merge tag 'backport/v3.14.24-ltsi-rc1/irq-renesas-intc-irqpin-to-v3.18-rc1' into...
[platform/adaptation/renesas_rcar/renesas_kernel.git] / mm / migrate.c
index 9194375..3acac4a 100644 (file)
@@ -72,28 +72,12 @@ int migrate_prep_local(void)
 }
 
 /*
- * Add isolated pages on the list back to the LRU under page lock
- * to avoid leaking evictable pages back onto unevictable list.
- */
-void putback_lru_pages(struct list_head *l)
-{
-       struct page *page;
-       struct page *page2;
-
-       list_for_each_entry_safe(page, page2, l, lru) {
-               list_del(&page->lru);
-               dec_zone_page_state(page, NR_ISOLATED_ANON +
-                               page_is_file_cache(page));
-                       putback_lru_page(page);
-       }
-}
-
-/*
  * Put previously isolated pages back onto the appropriate lists
  * from where they were once taken off for compaction/migration.
  *
- * This function shall be used instead of putback_lru_pages(),
- * whenever the isolated pageset has been built by isolate_migratepages_range()
+ * This function shall be used whenever the isolated pageset has been
+ * built from lru, balloon, hugetlbfs page. See isolate_migratepages_range()
+ * and isolate_huge_page().
  */
 void putback_movable_pages(struct list_head *l)
 {
@@ -164,8 +148,11 @@ static int remove_migration_pte(struct page *new, struct vm_area_struct *vma,
        pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
        if (pte_swp_soft_dirty(*ptep))
                pte = pte_mksoft_dirty(pte);
+
+       /* Recheck VMA as permissions can change since migration started  */
        if (is_write_migration_entry(entry))
-               pte = pte_mkwrite(pte);
+               pte = maybe_mkwrite(pte, vma);
+
 #ifdef CONFIG_HUGETLB_PAGE
        if (PageHuge(new)) {
                pte = pte_mkhuge(pte);
@@ -194,12 +181,49 @@ out:
 }
 
 /*
+ * Congratulations to trinity for discovering this bug.
+ * mm/fremap.c's remap_file_pages() accepts any range within a single vma to
+ * convert that vma to VM_NONLINEAR; and generic_file_remap_pages() will then
+ * replace the specified range by file ptes throughout (maybe populated after).
+ * If page migration finds a page within that range, while it's still located
+ * by vma_interval_tree rather than lost to i_mmap_nonlinear list, no problem:
+ * zap_pte() clears the temporary migration entry before mmap_sem is dropped.
+ * But if the migrating page is in a part of the vma outside the range to be
+ * remapped, then it will not be cleared, and remove_migration_ptes() needs to
+ * deal with it.  Fortunately, this part of the vma is of course still linear,
+ * so we just need to use linear location on the nonlinear list.
+ */
+static int remove_linear_migration_ptes_from_nonlinear(struct page *page,
+               struct address_space *mapping, void *arg)
+{
+       struct vm_area_struct *vma;
+       /* hugetlbfs does not support remap_pages, so no huge pgoff worries */
+       pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
+       unsigned long addr;
+
+       list_for_each_entry(vma,
+               &mapping->i_mmap_nonlinear, shared.nonlinear) {
+
+               addr = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
+               if (addr >= vma->vm_start && addr < vma->vm_end)
+                       remove_migration_pte(page, vma, addr, arg);
+       }
+       return SWAP_AGAIN;
+}
+
+/*
  * Get rid of all migration entries and replace them by
  * references to the indicated page.
  */
 static void remove_migration_ptes(struct page *old, struct page *new)
 {
-       rmap_walk(new, remove_migration_pte, old);
+       struct rmap_walk_control rwc = {
+               .rmap_one = remove_migration_pte,
+               .arg = old,
+               .file_nonlinear = remove_linear_migration_ptes_from_nonlinear,
+       };
+
+       rmap_walk(new, &rwc);
 }
 
 /*
@@ -510,7 +534,7 @@ void migrate_page_copy(struct page *newpage, struct page *page)
        if (PageUptodate(page))
                SetPageUptodate(newpage);
        if (TestClearPageActive(page)) {
-               VM_BUG_ON(PageUnevictable(page));
+               VM_BUG_ON_PAGE(PageUnevictable(page), page);
                SetPageActive(newpage);
        } else if (TestClearPageUnevictable(page))
                SetPageUnevictable(newpage);
@@ -563,14 +587,6 @@ void migrate_page_copy(struct page *newpage, struct page *page)
  *                    Migration functions
  ***********************************************************/
 
-/* Always fail migration. Used for mappings that are not movable */
-int fail_migrate_page(struct address_space *mapping,
-                       struct page *newpage, struct page *page)
-{
-       return -EIO;
-}
-EXPORT_SYMBOL(fail_migrate_page);
-
 /*
  * Common logic to directly migrate a single page suitable for
  * pages that do not use PagePrivate/PagePrivate2.
@@ -890,7 +906,7 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
         * free the metadata, so the page can be freed.
         */
        if (!page->mapping) {
-               VM_BUG_ON(PageAnon(page));
+               VM_BUG_ON_PAGE(PageAnon(page), page);
                if (page_has_private(page)) {
                        try_to_free_buffers(page);
                        goto uncharge;
@@ -925,8 +941,9 @@ out:
  * Obtain the lock on page, remove all ptes and migrate the page
  * to the newly allocated page in newpage.
  */
-static int unmap_and_move(new_page_t get_new_page, unsigned long private,
-                       struct page *page, int force, enum migrate_mode mode)
+static int unmap_and_move(new_page_t get_new_page, free_page_t put_new_page,
+                       unsigned long private, struct page *page, int force,
+                       enum migrate_mode mode)
 {
        int rc = 0;
        int *result = NULL;
@@ -970,11 +987,18 @@ out:
                                page_is_file_cache(page));
                putback_lru_page(page);
        }
+
        /*
-        * Move the new page to the LRU. If migration was not successful
-        * then this will free the page.
+        * If migration was not successful and there's a freeing callback, use
+        * it.  Otherwise, putback_lru_page() will drop the reference grabbed
+        * during isolation.
         */
-       putback_lru_page(newpage);
+       if (rc != MIGRATEPAGE_SUCCESS && put_new_page) {
+               ClearPageSwapBacked(newpage);
+               put_new_page(newpage, private);
+       } else
+               putback_lru_page(newpage);
+
        if (result) {
                if (rc)
                        *result = rc;
@@ -1003,12 +1027,13 @@ out:
  * will wait in the page fault for migration to complete.
  */
 static int unmap_and_move_huge_page(new_page_t get_new_page,
-                               unsigned long private, struct page *hpage,
-                               int force, enum migrate_mode mode)
+                               free_page_t put_new_page, unsigned long private,
+                               struct page *hpage, int force,
+                               enum migrate_mode mode)
 {
        int rc = 0;
        int *result = NULL;
-       struct page *new_hpage = get_new_page(hpage, private, &result);
+       struct page *new_hpage;
        struct anon_vma *anon_vma = NULL;
 
        /*
@@ -1018,9 +1043,12 @@ static int unmap_and_move_huge_page(new_page_t get_new_page,
         * tables or check whether the hugepage is pmd-based or not before
         * kicking migration.
         */
-       if (!hugepage_migration_support(page_hstate(hpage)))
+       if (!hugepage_migration_support(page_hstate(hpage))) {
+               putback_active_hugepage(hpage);
                return -ENOSYS;
+       }
 
+       new_hpage = get_new_page(hpage, private, &result);
        if (!new_hpage)
                return -ENOMEM;
 
@@ -1040,20 +1068,30 @@ static int unmap_and_move_huge_page(new_page_t get_new_page,
        if (!page_mapped(hpage))
                rc = move_to_new_page(new_hpage, hpage, 1, mode);
 
-       if (rc)
+       if (rc != MIGRATEPAGE_SUCCESS)
                remove_migration_ptes(hpage, hpage);
 
        if (anon_vma)
                put_anon_vma(anon_vma);
 
-       if (!rc)
+       if (rc == MIGRATEPAGE_SUCCESS)
                hugetlb_cgroup_migrate(hpage, new_hpage);
 
        unlock_page(hpage);
 out:
        if (rc != -EAGAIN)
                putback_active_hugepage(hpage);
-       put_page(new_hpage);
+
+       /*
+        * If migration was not successful and there's a freeing callback, use
+        * it.  Otherwise, put_page() will drop the reference grabbed during
+        * isolation.
+        */
+       if (rc != MIGRATEPAGE_SUCCESS && put_new_page)
+               put_new_page(new_hpage, private);
+       else
+               put_page(new_hpage);
+
        if (result) {
                if (rc)
                        *result = rc;
@@ -1070,6 +1108,8 @@ out:
  * @from:              The list of pages to be migrated.
  * @get_new_page:      The function used to allocate free pages to be used
  *                     as the target of the page migration.
+ * @put_new_page:      The function used to free target pages if migration
+ *                     fails, or NULL if no special handling is necessary.
  * @private:           Private data to be passed on to get_new_page()
  * @mode:              The migration mode that specifies the constraints for
  *                     page migration, if any.
@@ -1083,7 +1123,8 @@ out:
  * Returns the number of pages that were not migrated, or an error code.
  */
 int migrate_pages(struct list_head *from, new_page_t get_new_page,
-               unsigned long private, enum migrate_mode mode, int reason)
+               free_page_t put_new_page, unsigned long private,
+               enum migrate_mode mode, int reason)
 {
        int retry = 1;
        int nr_failed = 0;
@@ -1105,10 +1146,11 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,
 
                        if (PageHuge(page))
                                rc = unmap_and_move_huge_page(get_new_page,
-                                               private, page, pass > 2, mode);
+                                               put_new_page, private, page,
+                                               pass > 2, mode);
                        else
-                               rc = unmap_and_move(get_new_page, private,
-                                               page, pass > 2, mode);
+                               rc = unmap_and_move(get_new_page, put_new_page,
+                                               private, page, pass > 2, mode);
 
                        switch(rc) {
                        case -ENOMEM:
@@ -1120,7 +1162,12 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,
                                nr_succeeded++;
                                break;
                        default:
-                               /* Permanent failure */
+                               /*
+                                * Permanent failure (-EBUSY, -ENOSYS, etc.):
+                                * unlike -EAGAIN case, the failed page is
+                                * removed from migration page list and not
+                                * retried in the next outer loop.
+                                */
                                nr_failed++;
                                break;
                        }
@@ -1169,7 +1216,7 @@ static struct page *new_page_node(struct page *p, unsigned long private,
                                        pm->node);
        else
                return alloc_pages_exact_node(pm->node,
-                               GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
+                               GFP_HIGHUSER_MOVABLE | __GFP_THISNODE, 0);
 }
 
 /*
@@ -1252,7 +1299,7 @@ set_status:
 
        err = 0;
        if (!list_empty(&pagelist)) {
-               err = migrate_pages(&pagelist, new_page_node,
+               err = migrate_pages(&pagelist, new_page_node, NULL,
                                (unsigned long)pm, MIGRATE_SYNC, MR_SYSCALL);
                if (err)
                        putback_movable_pages(&pagelist);
@@ -1555,12 +1602,10 @@ static struct page *alloc_misplaced_dst_page(struct page *page,
        struct page *newpage;
 
        newpage = alloc_pages_exact_node(nid,
-                                        (GFP_HIGHUSER_MOVABLE | GFP_THISNODE |
-                                         __GFP_NOMEMALLOC | __GFP_NORETRY |
-                                         __GFP_NOWARN) &
+                                        (GFP_HIGHUSER_MOVABLE |
+                                         __GFP_THISNODE | __GFP_NOMEMALLOC |
+                                         __GFP_NORETRY | __GFP_NOWARN) &
                                         ~GFP_IOFS, 0);
-       if (newpage)
-               page_cpupid_xchg_last(newpage, page_cpupid_last(page));
 
        return newpage;
 }
@@ -1594,35 +1639,42 @@ bool migrate_ratelimited(int node)
 }
 
 /* Returns true if the node is migrate rate-limited after the update */
-bool numamigrate_update_ratelimit(pg_data_t *pgdat, unsigned long nr_pages)
+static bool numamigrate_update_ratelimit(pg_data_t *pgdat,
+                                       unsigned long nr_pages)
 {
-       bool rate_limited = false;
-
        /*
         * Rate-limit the amount of data that is being migrated to a node.
         * Optimal placement is no good if the memory bus is saturated and
         * all the time is being spent migrating!
         */
-       spin_lock(&pgdat->numabalancing_migrate_lock);
        if (time_after(jiffies, pgdat->numabalancing_migrate_next_window)) {
+               spin_lock(&pgdat->numabalancing_migrate_lock);
                pgdat->numabalancing_migrate_nr_pages = 0;
                pgdat->numabalancing_migrate_next_window = jiffies +
                        msecs_to_jiffies(migrate_interval_millisecs);
+               spin_unlock(&pgdat->numabalancing_migrate_lock);
        }
-       if (pgdat->numabalancing_migrate_nr_pages > ratelimit_pages)
-               rate_limited = true;
-       else
-               pgdat->numabalancing_migrate_nr_pages += nr_pages;
-       spin_unlock(&pgdat->numabalancing_migrate_lock);
-       
-       return rate_limited;
+       if (pgdat->numabalancing_migrate_nr_pages > ratelimit_pages) {
+               trace_mm_numa_migrate_ratelimit(current, pgdat->node_id,
+                                                               nr_pages);
+               return true;
+       }
+
+       /*
+        * This is an unlocked non-atomic update so errors are possible.
+        * The consequences are failing to migrate when we potentiall should
+        * have which is not severe enough to warrant locking. If it is ever
+        * a problem, it can be converted to a per-cpu counter.
+        */
+       pgdat->numabalancing_migrate_nr_pages += nr_pages;
+       return false;
 }
 
-int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page)
+static int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page)
 {
        int page_lru;
 
-       VM_BUG_ON(compound_order(page) && !PageTransHuge(page));
+       VM_BUG_ON_PAGE(compound_order(page) && !PageTransHuge(page), page);
 
        /* Avoid migrating to a node that is nearly full */
        if (!migrate_balanced_pgdat(pgdat, 1UL << compound_order(page)))
@@ -1703,9 +1755,15 @@ int migrate_misplaced_page(struct page *page, struct vm_area_struct *vma,
 
        list_add(&page->lru, &migratepages);
        nr_remaining = migrate_pages(&migratepages, alloc_misplaced_dst_page,
-                                    node, MIGRATE_ASYNC, MR_NUMA_MISPLACED);
+                                    NULL, node, MIGRATE_ASYNC,
+                                    MR_NUMA_MISPLACED);
        if (nr_remaining) {
-               putback_lru_pages(&migratepages);
+               if (!list_empty(&migratepages)) {
+                       list_del(&page->lru);
+                       dec_zone_page_state(page, NR_ISOLATED_ANON +
+                                       page_is_file_cache(page));
+                       putback_lru_page(page);
+               }
                isolated = 0;
        } else
                count_vm_numa_event(NUMA_PAGE_MIGRATE);
@@ -1748,12 +1806,11 @@ int migrate_misplaced_transhuge_page(struct mm_struct *mm,
                goto out_dropref;
 
        new_page = alloc_pages_node(node,
-               (GFP_TRANSHUGE | GFP_THISNODE) & ~__GFP_WAIT, HPAGE_PMD_ORDER);
+               (GFP_TRANSHUGE | __GFP_THISNODE) & ~__GFP_WAIT,
+               HPAGE_PMD_ORDER);
        if (!new_page)
                goto out_fail;
 
-       page_cpupid_xchg_last(new_page, page_cpupid_last(page));
-
        isolated = numamigrate_isolate_page(pgdat, page);
        if (!isolated) {
                put_page(new_page);