It's pointless to migrate pages within a pageblock if the entire pageblock will 
not become free for a thp allocation.

If we encounter a page that cannot be migrated and a direct compactor other 
than 
khugepaged is trying to allocate a hugepage for thp, then skip the entire 
pageblock and avoid migrating pages needlessly.

Signed-off-by: David Rientjes <rient...@google.com>
---
 mm/compaction.c | 41 ++++++++++++++++++++++++++++-------------
 1 file changed, 28 insertions(+), 13 deletions(-)

diff --git a/mm/compaction.c b/mm/compaction.c
--- a/mm/compaction.c
+++ b/mm/compaction.c
@@ -537,12 +537,12 @@ isolate_migratepages_range(struct zone *zone, struct 
compact_control *cc,
                if ((low_pfn & (MAX_ORDER_NR_PAGES - 1)) == 0) {
                        if (!pfn_valid(low_pfn)) {
                                low_pfn += MAX_ORDER_NR_PAGES - 1;
-                               continue;
+                               goto next;
                        }
                }
 
                if (!pfn_valid_within(low_pfn))
-                       continue;
+                       goto next;
                nr_scanned++;
 
                /*
@@ -553,7 +553,7 @@ isolate_migratepages_range(struct zone *zone, struct 
compact_control *cc,
                 */
                page = pfn_to_page(low_pfn);
                if (page_zone(page) != zone)
-                       continue;
+                       goto next;
 
                if (!valid_page)
                        valid_page = page;
@@ -599,7 +599,7 @@ isolate_migratepages_range(struct zone *zone, struct 
compact_control *cc,
                                        goto isolate_success;
                                }
                        }
-                       continue;
+                       goto next;
                }
 
                /*
@@ -616,7 +616,7 @@ isolate_migratepages_range(struct zone *zone, struct 
compact_control *cc,
                        if (!locked)
                                goto next_pageblock;
                        low_pfn += (1 << compound_order(page)) - 1;
-                       continue;
+                       goto next;
                }
 
                /*
@@ -626,7 +626,7 @@ isolate_migratepages_range(struct zone *zone, struct 
compact_control *cc,
                 */
                if (!page_mapping(page) &&
                    page_count(page) > page_mapcount(page))
-                       continue;
+                       goto next;
 
                /* Check if it is ok to still hold the lock */
                locked = compact_checklock_irqsave(&zone->lru_lock, &flags,
@@ -636,17 +636,17 @@ isolate_migratepages_range(struct zone *zone, struct 
compact_control *cc,
 
                /* Recheck PageLRU and PageTransHuge under lock */
                if (!PageLRU(page))
-                       continue;
+                       goto next;
                if (PageTransHuge(page)) {
                        low_pfn += (1 << compound_order(page)) - 1;
-                       continue;
+                       goto next;
                }
 
                lruvec = mem_cgroup_page_lruvec(page, zone);
 
                /* Try isolate the page */
                if (__isolate_lru_page(page, mode) != 0)
-                       continue;
+                       goto next;
 
                VM_BUG_ON_PAGE(PageTransCompound(page), page);
 
@@ -669,6 +669,24 @@ isolate_success:
 
 next_pageblock:
                low_pfn = ALIGN(low_pfn + 1, pageblock_nr_pages) - 1;
+
+next:
+               /*
+                * It is too expensive for compaction to migrate pages from a
+                * pageblock for thp page faults unless the entire pageblock
+                * will become free.
+                */
+               if ((cc->gfp_mask & __GFP_NO_KSWAPD) &&
+                   !(current->flags & PF_KTHREAD)) {
+                       if (locked) {
+                               spin_unlock_irqrestore(&zone->lru_lock, flags);
+                               locked = false;
+                       }
+                       putback_movable_pages(migratelist);
+                       cc->nr_migratepages = 0;
+                       nr_isolated = 0;
+                       low_pfn = ALIGN(low_pfn + 1, pageblock_nr_pages) - 1;
+               }
        }
 
        acct_isolated(zone, locked, cc);
@@ -880,7 +898,7 @@ static isolate_migrate_t isolate_migratepages(struct zone 
*zone,
 
        cc->migrate_pfn = low_pfn;
 
-       return ISOLATE_SUCCESS;
+       return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE;
 }
 
 static int compact_finished(struct zone *zone, struct compact_control *cc,
@@ -1055,9 +1073,6 @@ static int compact_zone(struct zone *zone, struct 
compact_control *cc)
                        ;
                }
 
-               if (!cc->nr_migratepages)
-                       continue;
-
                err = migrate_pages(&cc->migratepages, compaction_alloc,
                                compaction_free, (unsigned long)cc, cc->mode,
                                MR_COMPACTION);
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Reply via email to