Move free pages between lists on steal
[powerpc.git] / mm / page_alloc.c
index d54ecf4..d575a3e 100644 (file)
@@ -158,6 +158,7 @@ int nr_node_ids __read_mostly = MAX_NUMNODES;
 EXPORT_SYMBOL(nr_node_ids);
 #endif
 
+#ifdef CONFIG_PAGE_GROUP_BY_MOBILITY
 static inline int get_pageblock_migratetype(struct page *page)
 {
        return get_pageblock_flags_group(page, PB_migrate, PB_migrate_end);
@@ -174,6 +175,22 @@ static inline int gfpflags_to_migratetype(gfp_t gfp_flags)
        return ((gfp_flags & __GFP_MOVABLE) != 0);
 }
 
+#else
+static inline int get_pageblock_migratetype(struct page *page)
+{
+       return MIGRATE_UNMOVABLE;
+}
+
+static void set_pageblock_migratetype(struct page *page, int migratetype)
+{
+}
+
+static inline int gfpflags_to_migratetype(gfp_t gfp_flags)
+{
+       return MIGRATE_UNMOVABLE;
+}
+#endif /* CONFIG_PAGE_GROUP_BY_MOBILITY */
+
 #ifdef CONFIG_DEBUG_VM
 static int page_outside_zone_boundaries(struct zone *zone, struct page *page)
 {
@@ -653,6 +670,7 @@ static int prep_new_page(struct page *page, int order, gfp_t gfp_flags)
        return 0;
 }
 
+#ifdef CONFIG_PAGE_GROUP_BY_MOBILITY
 /*
  * This array describes the order lists are fallen back to when
  * the free lists for the desirable migrate type are depleted
@@ -662,6 +680,72 @@ static int fallbacks[MIGRATE_TYPES][MIGRATE_TYPES-1] = {
        [MIGRATE_MOVABLE]   = { MIGRATE_UNMOVABLE },
 };
 
+/*
+ * Move the free pages in a range to the free lists of the requested type.
+ * Note that start_page and end_pages are not aligned in a MAX_ORDER_NR_PAGES
+ * boundary. If alignment is required, use move_freepages_block()
+ */
+int move_freepages(struct zone *zone,
+                       struct page *start_page, struct page *end_page,
+                       int migratetype)
+{
+       struct page *page;
+       unsigned long order;
+       int blocks_moved = 0;
+
+#ifndef CONFIG_HOLES_IN_ZONE
+       /*
+        * page_zone is not safe to call in this context when
+        * CONFIG_HOLES_IN_ZONE is set. This bug check is probably redundant
+        * anyway as we check zone boundaries in move_freepages_block().
+        * Remove at a later date when no bug reports exist related to
+        * CONFIG_PAGE_GROUP_BY_MOBILITY
+        */
+       BUG_ON(page_zone(start_page) != page_zone(end_page));
+#endif
+
+       for (page = start_page; page <= end_page;) {
+               if (!pfn_valid_within(page_to_pfn(page))) {
+                       page++;
+                       continue;
+               }
+
+               if (!PageBuddy(page)) {
+                       page++;
+                       continue;
+               }
+
+               order = page_order(page);
+               list_del(&page->lru);
+               list_add(&page->lru,
+                       &zone->free_area[order].free_list[migratetype]);
+               page += 1 << order;
+               blocks_moved++;
+       }
+
+       return blocks_moved;
+}
+
+int move_freepages_block(struct zone *zone, struct page *page, int migratetype)
+{
+       unsigned long start_pfn, end_pfn;
+       struct page *start_page, *end_page;
+
+       start_pfn = page_to_pfn(page);
+       start_pfn = start_pfn & ~(MAX_ORDER_NR_PAGES-1);
+       start_page = pfn_to_page(start_pfn);
+       end_page = start_page + MAX_ORDER_NR_PAGES - 1;
+       end_pfn = start_pfn + MAX_ORDER_NR_PAGES - 1;
+
+       /* Do not cross zone boundaries */
+       if (start_pfn < zone->zone_start_pfn)
+               start_page = page;
+       if (end_pfn >= zone->zone_start_pfn + zone->spanned_pages)
+               return 0;
+
+       return move_freepages(zone, start_page, end_page, migratetype);
+}
+
 /* Remove an element from the buddy allocator from the fallback list */
 static struct page *__rmqueue_fallback(struct zone *zone, int order,
                                                int start_migratetype)
@@ -686,11 +770,13 @@ static struct page *__rmqueue_fallback(struct zone *zone, int order,
                        area->nr_free--;
 
                        /*
-                        * If breaking a large block of pages, place the buddies
-                        * on the preferred allocation list
+                        * If breaking a large block of pages, move all free
+                        * pages to the preferred allocation list
                         */
-                       if (unlikely(current_order >= MAX_ORDER / 2))
+                       if (unlikely(current_order >= MAX_ORDER / 2)) {
                                migratetype = start_migratetype;
+                               move_freepages_block(zone, page, migratetype);
+                       }
 
                        /* Remove the page from the freelists */
                        list_del(&page->lru);
@@ -709,6 +795,13 @@ static struct page *__rmqueue_fallback(struct zone *zone, int order,
 
        return NULL;
 }
+#else
+static struct page *__rmqueue_fallback(struct zone *zone, int order,
+                                               int start_migratetype)
+{
+       return NULL;
+}
+#endif /* CONFIG_PAGE_GROUP_BY_MOBILITY */
 
 /* 
  * Do the hard work of removing an element from the buddy allocator.
@@ -760,7 +853,8 @@ static int rmqueue_bulk(struct zone *zone, unsigned int order,
                struct page *page = __rmqueue(zone, order, migratetype);
                if (unlikely(page == NULL))
                        break;
-               list_add_tail(&page->lru, list);
+               list_add(&page->lru, list);
+               set_page_private(page, migratetype);
        }
        spin_unlock(&zone->lock);
        return i;
@@ -850,7 +944,9 @@ void mark_free_pages(struct zone *zone)
        }
        spin_unlock_irqrestore(&zone->lock, flags);
 }
+#endif /* CONFIG_PM */
 
+#if defined(CONFIG_HIBERNATION) || defined(CONFIG_PAGE_GROUP_BY_MOBILITY)
 /*
  * Spill all of this CPU's per-cpu pages back into the buddy allocator.
  */
@@ -862,7 +958,28 @@ void drain_local_pages(void)
        __drain_pages(smp_processor_id());
        local_irq_restore(flags);       
 }
-#endif /* CONFIG_HIBERNATION */
+
+void smp_drain_local_pages(void *arg)
+{
+       drain_local_pages();
+}
+
+/*
+ * Spill all the per-cpu pages from all CPUs back into the buddy allocator
+ */
+void drain_all_local_pages(void)
+{
+       unsigned long flags;
+
+       local_irq_save(flags);
+       __drain_pages(smp_processor_id());
+       local_irq_restore(flags);
+
+       smp_call_function(smp_drain_local_pages, NULL, 0, 1);
+}
+#else
+void drain_all_local_pages(void) {}
+#endif /* CONFIG_HIBERNATION || CONFIG_PAGE_GROUP_BY_MOBILITY */
 
 /*
  * Free a 0-order page
@@ -887,6 +1004,7 @@ static void fastcall free_hot_cold_page(struct page *page, int cold)
        local_irq_save(flags);
        __count_vm_event(PGFREE);
        list_add(&page->lru, &pcp->list);
+       set_page_private(page, get_pageblock_migratetype(page));
        pcp->count++;
        if (pcp->count >= pcp->high) {
                free_pages_bulk(zone, pcp->batch, &pcp->list, 0);
@@ -951,7 +1069,23 @@ again:
                        if (unlikely(!pcp->count))
                                goto failed;
                }
+
+#ifdef CONFIG_PAGE_GROUP_BY_MOBILITY
+               /* Find a page of the appropriate migrate type */
+               list_for_each_entry(page, &pcp->list, lru)
+                       if (page_private(page) == migratetype)
+                               break;
+
+               /* Allocate more to the pcp list if necessary */
+               if (unlikely(&page->lru == &pcp->list)) {
+                       pcp->count += rmqueue_bulk(zone, 0,
+                                       pcp->batch, &pcp->list, migratetype);
+                       page = list_entry(pcp->list.next, struct page, lru);
+               }
+#else
                page = list_entry(pcp->list.next, struct page, lru);
+#endif /* CONFIG_PAGE_GROUP_BY_MOBILITY */
+
                list_del(&page->lru);
                pcp->count--;
        } else {
@@ -1437,6 +1571,9 @@ nofail_alloc:
 
        cond_resched();
 
+       if (order != 0)
+               drain_all_local_pages();
+
        if (likely(did_some_progress)) {
                page = get_page_from_freelist(gfp_mask, order,
                                                zonelist, alloc_flags);