mm: memcg: clean up mm_match_cgroup() signature
[platform/adaptation/renesas_rcar/renesas_kernel.git] / mm / compaction.c
index 832c418..2c4ce17 100644 (file)
@@ -50,6 +50,106 @@ static inline bool migrate_async_suitable(int migratetype)
        return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
 }
 
+#ifdef CONFIG_COMPACTION
+/* Returns true if the pageblock should be scanned for pages to isolate. */
+static inline bool isolation_suitable(struct compact_control *cc,
+                                       struct page *page)
+{
+       if (cc->ignore_skip_hint)
+               return true;
+
+       return !get_pageblock_skip(page);
+}
+
+/*
+ * This function is called to clear all cached information on pageblocks that
+ * should be skipped for page isolation when the migrate and free page scanner
+ * meet.
+ */
+static void __reset_isolation_suitable(struct zone *zone)
+{
+       unsigned long start_pfn = zone->zone_start_pfn;
+       unsigned long end_pfn = zone->zone_start_pfn + zone->spanned_pages;
+       unsigned long pfn;
+
+       zone->compact_cached_migrate_pfn = start_pfn;
+       zone->compact_cached_free_pfn = end_pfn;
+       zone->compact_blockskip_flush = false;
+
+       /* Walk the zone and mark every pageblock as suitable for isolation */
+       for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
+               struct page *page;
+
+               cond_resched();
+
+               if (!pfn_valid(pfn))
+                       continue;
+
+               page = pfn_to_page(pfn);
+               if (zone != page_zone(page))
+                       continue;
+
+               clear_pageblock_skip(page);
+       }
+}
+
+void reset_isolation_suitable(pg_data_t *pgdat)
+{
+       int zoneid;
+
+       for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
+               struct zone *zone = &pgdat->node_zones[zoneid];
+               if (!populated_zone(zone))
+                       continue;
+
+               /* Only flush if a full compaction finished recently */
+               if (zone->compact_blockskip_flush)
+                       __reset_isolation_suitable(zone);
+       }
+}
+
+/*
+ * If no pages were isolated then mark this pageblock to be skipped in the
+ * future. The information is later cleared by __reset_isolation_suitable().
+ */
+static void update_pageblock_skip(struct compact_control *cc,
+                       struct page *page, unsigned long nr_isolated,
+                       bool migrate_scanner)
+{
+       struct zone *zone = cc->zone;
+       if (!page)
+               return;
+
+       if (!nr_isolated) {
+               unsigned long pfn = page_to_pfn(page);
+               set_pageblock_skip(page);
+
+               /* Update where compaction should restart */
+               if (migrate_scanner) {
+                       if (!cc->finished_update_migrate &&
+                           pfn > zone->compact_cached_migrate_pfn)
+                               zone->compact_cached_migrate_pfn = pfn;
+               } else {
+                       if (!cc->finished_update_free &&
+                           pfn < zone->compact_cached_free_pfn)
+                               zone->compact_cached_free_pfn = pfn;
+               }
+       }
+}
+#else
+static inline bool isolation_suitable(struct compact_control *cc,
+                                       struct page *page)
+{
+       return true;
+}
+
+static void update_pageblock_skip(struct compact_control *cc,
+                       struct page *page, unsigned long nr_isolated,
+                       bool migrate_scanner)
+{
+}
+#endif /* CONFIG_COMPACTION */
+
 static inline bool should_release_lock(spinlock_t *lock)
 {
        return need_resched() || spin_is_contended(lock);
@@ -93,6 +193,27 @@ static inline bool compact_trylock_irqsave(spinlock_t *lock,
        return compact_checklock_irqsave(lock, flags, false, cc);
 }
 
+/* Returns true if the page is within a block suitable for migration to */
+static bool suitable_migration_target(struct page *page)
+{
+       int migratetype = get_pageblock_migratetype(page);
+
+       /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
+       if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
+               return false;
+
+       /* If the page is a large free page, then allow migration */
+       if (PageBuddy(page) && page_order(page) >= pageblock_order)
+               return true;
+
+       /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
+       if (migrate_async_suitable(migratetype))
+               return true;
+
+       /* Otherwise skip the block */
+       return false;
+}
+
 static void compact_capture_page(struct compact_control *cc)
 {
        unsigned long flags;
@@ -153,38 +274,58 @@ static void compact_capture_page(struct compact_control *cc)
  * pages inside of the pageblock (even though it may still end up isolating
  * some pages).
  */
-static unsigned long isolate_freepages_block(unsigned long blockpfn,
+static unsigned long isolate_freepages_block(struct compact_control *cc,
+                               unsigned long blockpfn,
                                unsigned long end_pfn,
                                struct list_head *freelist,
                                bool strict)
 {
        int nr_scanned = 0, total_isolated = 0;
-       struct page *cursor;
+       struct page *cursor, *valid_page = NULL;
+       unsigned long nr_strict_required = end_pfn - blockpfn;
+       unsigned long flags;
+       bool locked = false;
 
        cursor = pfn_to_page(blockpfn);
 
-       /* Isolate free pages. This assumes the block is valid */
+       /* Isolate free pages. */
        for (; blockpfn < end_pfn; blockpfn++, cursor++) {
                int isolated, i;
                struct page *page = cursor;
 
-               if (!pfn_valid_within(blockpfn)) {
-                       if (strict)
-                               return 0;
-                       continue;
-               }
                nr_scanned++;
+               if (!pfn_valid_within(blockpfn))
+                       continue;
+               if (!valid_page)
+                       valid_page = page;
+               if (!PageBuddy(page))
+                       continue;
+
+               /*
+                * The zone lock must be held to isolate freepages.
+                * Unfortunately this is a very coarse lock and can be
+                * heavily contended if there are parallel allocations
+                * or parallel compactions. For async compaction do not
+                * spin on the lock and we acquire the lock as late as
+                * possible.
+                */
+               locked = compact_checklock_irqsave(&cc->zone->lock, &flags,
+                                                               locked, cc);
+               if (!locked)
+                       break;
+
+               /* Recheck this is a suitable migration target under lock */
+               if (!strict && !suitable_migration_target(page))
+                       break;
 
-               if (!PageBuddy(page)) {
-                       if (strict)
-                               return 0;
+               /* Recheck this is a buddy page under lock */
+               if (!PageBuddy(page))
                        continue;
-               }
 
                /* Found a free page, break it into order-0 pages */
                isolated = split_free_page(page);
                if (!isolated && strict)
-                       return 0;
+                       break;
                total_isolated += isolated;
                for (i = 0; i < isolated; i++) {
                        list_add(&page->lru, freelist);
@@ -199,6 +340,22 @@ static unsigned long isolate_freepages_block(unsigned long blockpfn,
        }
 
        trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
+
+       /*
+        * If strict isolation is requested by CMA then check that all the
+        * pages requested were isolated. If there were any failures, 0 is
+        * returned and CMA will fail.
+        */
+       if (strict && nr_strict_required != total_isolated)
+               total_isolated = 0;
+
+       if (locked)
+               spin_unlock_irqrestore(&cc->zone->lock, flags);
+
+       /* Update the pageblock-skip if the whole pageblock was scanned */
+       if (blockpfn == end_pfn)
+               update_pageblock_skip(cc, valid_page, total_isolated, false);
+
        return total_isolated;
 }
 
@@ -216,17 +373,14 @@ static unsigned long isolate_freepages_block(unsigned long blockpfn,
  * a free page).
  */
 unsigned long
-isolate_freepages_range(unsigned long start_pfn, unsigned long end_pfn)
+isolate_freepages_range(struct compact_control *cc,
+                       unsigned long start_pfn, unsigned long end_pfn)
 {
-       unsigned long isolated, pfn, block_end_pfn, flags;
-       struct zone *zone = NULL;
+       unsigned long isolated, pfn, block_end_pfn;
        LIST_HEAD(freelist);
 
-       if (pfn_valid(start_pfn))
-               zone = page_zone(pfn_to_page(start_pfn));
-
        for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) {
-               if (!pfn_valid(pfn) || zone != page_zone(pfn_to_page(pfn)))
+               if (!pfn_valid(pfn) || cc->zone != page_zone(pfn_to_page(pfn)))
                        break;
 
                /*
@@ -236,10 +390,8 @@ isolate_freepages_range(unsigned long start_pfn, unsigned long end_pfn)
                block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
                block_end_pfn = min(block_end_pfn, end_pfn);
 
-               spin_lock_irqsave(&zone->lock, flags);
-               isolated = isolate_freepages_block(pfn, block_end_pfn,
+               isolated = isolate_freepages_block(cc, pfn, block_end_pfn,
                                                   &freelist, true);
-               spin_unlock_irqrestore(&zone->lock, flags);
 
                /*
                 * In strict mode, isolate_freepages_block() returns 0 if
@@ -309,6 +461,7 @@ static bool too_many_isolated(struct zone *zone)
  * @cc:                Compaction control structure.
  * @low_pfn:   The first PFN of the range.
  * @end_pfn:   The one-past-the-last PFN of the range.
+ * @unevictable: true if it allows to isolate unevictable pages
  *
  * Isolate all pages that can be migrated from the range specified by
  * [low_pfn, end_pfn).  Returns zero if there is a fatal signal
@@ -324,7 +477,7 @@ static bool too_many_isolated(struct zone *zone)
  */
 unsigned long
 isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
-                          unsigned long low_pfn, unsigned long end_pfn)
+               unsigned long low_pfn, unsigned long end_pfn, bool unevictable)
 {
        unsigned long last_pageblock_nr = 0, pageblock_nr;
        unsigned long nr_scanned = 0, nr_isolated = 0;
@@ -333,6 +486,7 @@ isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
        struct lruvec *lruvec;
        unsigned long flags;
        bool locked = false;
+       struct page *page = NULL, *valid_page = NULL;
 
        /*
         * Ensure that there are not too many pages isolated from the LRU
@@ -353,8 +507,6 @@ isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
        /* Time to isolate some pages for migration */
        cond_resched();
        for (; low_pfn < end_pfn; low_pfn++) {
-               struct page *page;
-
                /* give a chance to irqs before checking need_resched() */
                if (locked && !((low_pfn+1) % SWAP_CLUSTER_MAX)) {
                        if (should_release_lock(&zone->lru_lock)) {
@@ -390,6 +542,14 @@ isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
                if (page_zone(page) != zone)
                        continue;
 
+               if (!valid_page)
+                       valid_page = page;
+
+               /* If isolation recently failed, do not retry */
+               pageblock_nr = low_pfn >> pageblock_order;
+               if (!isolation_suitable(cc, page))
+                       goto next_pageblock;
+
                /* Skip if free */
                if (PageBuddy(page))
                        continue;
@@ -399,9 +559,9 @@ isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
                 * migration is optimistic to see if the minimum amount of work
                 * satisfies the allocation
                 */
-               pageblock_nr = low_pfn >> pageblock_order;
                if (!cc->sync && last_pageblock_nr != pageblock_nr &&
                    !migrate_async_suitable(get_pageblock_migratetype(page))) {
+                       cc->finished_update_migrate = true;
                        goto next_pageblock;
                }
 
@@ -443,6 +603,9 @@ isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
                if (!cc->sync)
                        mode |= ISOLATE_ASYNC_MIGRATE;
 
+               if (unevictable)
+                       mode |= ISOLATE_UNEVICTABLE;
+
                lruvec = mem_cgroup_page_lruvec(page, zone);
 
                /* Try isolate the page */
@@ -452,6 +615,7 @@ isolate_migratepages_range(struct zone *zone, struct compact_control *cc,
                VM_BUG_ON(PageTransCompound(page));
 
                /* Successfully isolated */
+               cc->finished_update_migrate = true;
                del_page_from_lru_list(page, lruvec, page_lru(page));
                list_add(&page->lru, migratelist);
                cc->nr_migratepages++;
@@ -476,6 +640,10 @@ next_pageblock:
        if (locked)
                spin_unlock_irqrestore(&zone->lru_lock, flags);
 
+       /* Update the pageblock-skip if the whole pageblock was scanned */
+       if (low_pfn == end_pfn)
+               update_pageblock_skip(cc, valid_page, nr_isolated, true);
+
        trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
 
        return low_pfn;
@@ -483,43 +651,6 @@ next_pageblock:
 
 #endif /* CONFIG_COMPACTION || CONFIG_CMA */
 #ifdef CONFIG_COMPACTION
-
-/* Returns true if the page is within a block suitable for migration to */
-static bool suitable_migration_target(struct page *page)
-{
-
-       int migratetype = get_pageblock_migratetype(page);
-
-       /* Don't interfere with memory hot-remove or the min_free_kbytes blocks */
-       if (migratetype == MIGRATE_ISOLATE || migratetype == MIGRATE_RESERVE)
-               return false;
-
-       /* If the page is a large free page, then allow migration */
-       if (PageBuddy(page) && page_order(page) >= pageblock_order)
-               return true;
-
-       /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
-       if (migrate_async_suitable(migratetype))
-               return true;
-
-       /* Otherwise skip the block */
-       return false;
-}
-
-/*
- * Returns the start pfn of the last page block in a zone.  This is the starting
- * point for full compaction of a zone.  Compaction searches for free pages from
- * the end of each zone, while isolate_freepages_block scans forward inside each
- * page block.
- */
-static unsigned long start_free_pfn(struct zone *zone)
-{
-       unsigned long free_pfn;
-       free_pfn = zone->zone_start_pfn + zone->spanned_pages;
-       free_pfn &= ~(pageblock_nr_pages-1);
-       return free_pfn;
-}
-
 /*
  * Based on information in the current compact_control, find blocks
  * suitable for isolating free pages from and then isolate them.
@@ -529,7 +660,6 @@ static void isolate_freepages(struct zone *zone,
 {
        struct page *page;
        unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
-       unsigned long flags;
        int nr_freepages = cc->nr_freepages;
        struct list_head *freelist = &cc->freepages;
 
@@ -577,30 +707,16 @@ static void isolate_freepages(struct zone *zone,
                if (!suitable_migration_target(page))
                        continue;
 
-               /*
-                * Found a block suitable for isolating free pages from. Now
-                * we disabled interrupts, double check things are ok and
-                * isolate the pages. This is to minimise the time IRQs
-                * are disabled
-                */
-               isolated = 0;
+               /* If isolation recently failed, do not retry */
+               if (!isolation_suitable(cc, page))
+                       continue;
 
-               /*
-                * The zone lock must be held to isolate freepages. This
-                * unfortunately this is a very coarse lock and can be
-                * heavily contended if there are parallel allocations
-                * or parallel compactions. For async compaction do not
-                * spin on the lock
-                */
-               if (!compact_trylock_irqsave(&zone->lock, &flags, cc))
-                       break;
-               if (suitable_migration_target(page)) {
-                       end_pfn = min(pfn + pageblock_nr_pages, zone_end_pfn);
-                       isolated = isolate_freepages_block(pfn, end_pfn,
-                                                          freelist, false);
-                       nr_freepages += isolated;
-               }
-               spin_unlock_irqrestore(&zone->lock, flags);
+               /* Found a block suitable for isolating free pages from */
+               isolated = 0;
+               end_pfn = min(pfn + pageblock_nr_pages, zone_end_pfn);
+               isolated = isolate_freepages_block(cc, pfn, end_pfn,
+                                                  freelist, false);
+               nr_freepages += isolated;
 
                /*
                 * Record the highest PFN we isolated pages from. When next
@@ -608,17 +724,8 @@ static void isolate_freepages(struct zone *zone,
                 * page migration may have returned some pages to the allocator
                 */
                if (isolated) {
+                       cc->finished_update_free = true;
                        high_pfn = max(high_pfn, pfn);
-
-                       /*
-                        * If the free scanner has wrapped, update
-                        * compact_cached_free_pfn to point to the highest
-                        * pageblock with free pages. This reduces excessive
-                        * scanning of full pageblocks near the end of the
-                        * zone
-                        */
-                       if (cc->order > 0 && cc->wrapped)
-                               zone->compact_cached_free_pfn = high_pfn;
                }
        }
 
@@ -627,11 +734,6 @@ static void isolate_freepages(struct zone *zone,
 
        cc->free_pfn = high_pfn;
        cc->nr_freepages = nr_freepages;
-
-       /* If compact_cached_free_pfn is reset then set it now */
-       if (cc->order > 0 && !cc->wrapped &&
-                       zone->compact_cached_free_pfn == start_free_pfn(zone))
-               zone->compact_cached_free_pfn = high_pfn;
 }
 
 /*
@@ -709,7 +811,7 @@ static isolate_migrate_t isolate_migratepages(struct zone *zone,
        }
 
        /* Perform the isolation */
-       low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn);
+       low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn, false);
        if (!low_pfn || cc->contended)
                return ISOLATE_ABORT;
 
@@ -726,27 +828,19 @@ static int compact_finished(struct zone *zone,
        if (fatal_signal_pending(current))
                return COMPACT_PARTIAL;
 
-       /*
-        * A full (order == -1) compaction run starts at the beginning and
-        * end of a zone; it completes when the migrate and free scanner meet.
-        * A partial (order > 0) compaction can start with the free scanner
-        * at a random point in the zone, and may have to restart.
-        */
+       /* Compaction run completes if the migrate and free scanner meet */
        if (cc->free_pfn <= cc->migrate_pfn) {
-               if (cc->order > 0 && !cc->wrapped) {
-                       /* We started partway through; restart at the end. */
-                       unsigned long free_pfn = start_free_pfn(zone);
-                       zone->compact_cached_free_pfn = free_pfn;
-                       cc->free_pfn = free_pfn;
-                       cc->wrapped = 1;
-                       return COMPACT_CONTINUE;
-               }
-               return COMPACT_COMPLETE;
-       }
+               /*
+                * Mark that the PG_migrate_skip information should be cleared
+                * by kswapd when it goes to sleep. kswapd does not set the
+                * flag itself as the decision to be clear should be directly
+                * based on an allocation request.
+                */
+               if (!current_is_kswapd())
+                       zone->compact_blockskip_flush = true;
 
-       /* We wrapped around and ended up where we started. */
-       if (cc->wrapped && cc->free_pfn <= cc->start_free_pfn)
                return COMPACT_COMPLETE;
+       }
 
        /*
         * order == -1 is expected when compacting via
@@ -837,6 +931,8 @@ unsigned long compaction_suitable(struct zone *zone, int order)
 static int compact_zone(struct zone *zone, struct compact_control *cc)
 {
        int ret;
+       unsigned long start_pfn = zone->zone_start_pfn;
+       unsigned long end_pfn = zone->zone_start_pfn + zone->spanned_pages;
 
        ret = compaction_suitable(zone, cc->order);
        switch (ret) {
@@ -849,18 +945,30 @@ static int compact_zone(struct zone *zone, struct compact_control *cc)
                ;
        }
 
-       /* Setup to move all movable pages to the end of the zone */
-       cc->migrate_pfn = zone->zone_start_pfn;
-
-       if (cc->order > 0) {
-               /* Incremental compaction. Start where the last one stopped. */
-               cc->free_pfn = zone->compact_cached_free_pfn;
-               cc->start_free_pfn = cc->free_pfn;
-       } else {
-               /* Order == -1 starts at the end of the zone. */
-               cc->free_pfn = start_free_pfn(zone);
+       /*
+        * Setup to move all movable pages to the end of the zone. Used cached
+        * information on where the scanners should start but check that it
+        * is initialised by ensuring the values are within zone boundaries.
+        */
+       cc->migrate_pfn = zone->compact_cached_migrate_pfn;
+       cc->free_pfn = zone->compact_cached_free_pfn;
+       if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) {
+               cc->free_pfn = end_pfn & ~(pageblock_nr_pages-1);
+               zone->compact_cached_free_pfn = cc->free_pfn;
+       }
+       if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) {
+               cc->migrate_pfn = start_pfn;
+               zone->compact_cached_migrate_pfn = cc->migrate_pfn;
        }
 
+       /*
+        * Clear pageblock skip if there were failures recently and compaction
+        * is about to be retried after being deferred. kswapd does not do
+        * this reset as it'll reset the cached information when going to sleep.
+        */
+       if (compaction_restarting(zone, cc->order) && !current_is_kswapd())
+               __reset_isolation_suitable(zone);
+
        migrate_prep_local();
 
        while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {