Skip to contentSkip to conversation

Reclaim page capture v2

RFC PATCH In review Andy Whitcroft 5 participants 24 messages Sep 3, 2008 – Sep 5, 2008

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover Reclaim page capture v2

For sometime we have been looking at mechanisms for improving the availability of larger allocations under load. One of the options we have explored is the capturing of pages freed under direct reclaim in order to increase the chances of free pages coelescing before they are subject to reallocation by racing allocators.

Following this email is a patch stack implementing page capture during direct reclaim. It consits of four patches. The first two simply pull out existing code into helpers for reuse. The third makes buddy's use of struct page explicit. The fourth contains the meat of the changes, and its leader contains a much fuller description of the feature.

This update represents a rebase to -mm and incorporates feedback from KOSAKI Motohiro. It also incorporates an accounting fix which was preventing some captures.

I have done a lot of comparitive testing with and without this patch set and in broad brush I am seeing improvements in hugepage allocations (worst case size) success on all of my test systems. These tests consist of placing a constant stream of high order allocations on the system, at varying rates. The results for these various runs are then averaged to give an overall improvement.

Absolute Effective
x86-64 2.48% 4.58%
powerpc 5.55% 25.22%

x86-64 has a relatively small huge page size and so is always much more effective at allocating huge pages. Even there we get a measurable improvement. On powerpc the huge pages are much larger and much harder to recover. Here we see a full 25% increase in page recovery.

It should be noted that these are worst case testing, and very agressive taking every possible page in the system.

Against: 2.6.27-rc1-mm1

Comments?

-apw

Changes since V1:
- Incorporates review feedback from KOSAKI Motohiro,
- fixes up accounting when checking watermarks for captured pages,
- rebase 2.6.27-rc1-mm1,
- Incorporates review feedback from Mel.

Andy Whitcroft (4):
pull out the page pre-release and sanity check logic for reuse pull out zone cpuset and watermark checks for reuse
buddy: explicitly identify buddy field use in struct page capture pages freed during direct reclaim for allocation by the
reclaimer

 include/linux/mm_types.h   |    4 +
 include/linux/page-flags.h |    6 +
 mm/internal.h              |    8 ++-
 mm/page_alloc.c            |  255 ++++++++++++++++++++++++++++++++++++++------
 mm/vmscan.c                |  115 ++++++++++++++++----
 5 files changed, 332 insertions(+), 56 deletions(-)

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#1 lore raw

PATCH 1/4 pull out the page pre-release and sanity check logic for reuse

When we are about to release a page we perform a number of actions on that page. We clear down any anonymous mappings, confirm that the page is safe to release, check for freeing locks, before mapping the page should that be required. Pull this processing out into a helper function for reuse in a later patch.

Note that we do not convert the similar cleardown in free_hot_cold_page() as the optimiser is unable to squash the loops during the inline.

---
 mm/page_alloc.c |   43 ++++++++++++++++++++++++++++++-------------
 1 files changed, 30 insertions(+), 13 deletions(-)
mm/page_alloc.c +30 -13
index f52fcf1..b2a2c2b 100644
@@ -489,6 +489,35 @@ static inline int free_pages_check(struct page *page)
 }
 
 /*
+ * Prepare this page for release to the buddy.  Sanity check the page.
+ * Returns 1 if the page is safe to free.
+ */
+static inline int free_page_prepare(struct page *page, int order)
+{
+	int i;
+	int reserved = 0;
+
+	if (PageAnon(page))
+		page->mapping = NULL;
+
+	for (i = 0 ; i < (1 << order) ; ++i)
+		reserved += free_pages_check(page + i);
+	if (reserved)
+		return 0;
+
+	if (!PageHighMem(page)) {
+		debug_check_no_locks_freed(page_address(page),
+							PAGE_SIZE << order);
+		debug_check_no_obj_freed(page_address(page),
+					   PAGE_SIZE << order);
+	}
+	arch_free_page(page, order);
+	kernel_map_pages(page, 1 << order, 0);
+
+	return 1;
+}
+
+/*
  * Frees a list of pages. 
  * Assumes all pages on list are in same zone, and of same order.
  * count is the number of pages to free.
@@ -529,22 +558,10 @@ static void free_one_page(struct zone *zone, struct page *page, int order)
 static void __free_pages_ok(struct page *page, unsigned int order)
 {
 	unsigned long flags;
-	int i;
-	int reserved = 0;
 
-	for (i = 0 ; i < (1 << order) ; ++i)
-		reserved += free_pages_check(page + i);
-	if (reserved)
+	if (!free_page_prepare(page, order))
 		return;
 
-	if (!PageHighMem(page)) {
-		debug_check_no_locks_freed(page_address(page),PAGE_SIZE<<order);
-		debug_check_no_obj_freed(page_address(page),
-					   PAGE_SIZE << order);
-	}
-	arch_free_page(page, order);
-	kernel_map_pages(page, 1 << order, 0);
-
 	local_irq_save(flags);
 	__count_vm_events(PGFREE, 1 << order);
 	free_one_page(page_zone(page), page, order);
Signature · 7 lines
1.6.0.rc1.258.g80295

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#4 lore raw

PATCH 2/4 pull out zone cpuset and watermark checks for reuse

In reply to Andy Whitcroft

When allocating we need to confirm that the zone we are about to allocate from is acceptable to the CPUSET we are in, and that it does not violate the zone watermarks. Pull these checks out so we can reuse them in a later patch.

---
 mm/page_alloc.c |   62 ++++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 43 insertions(+), 19 deletions(-)
mm/page_alloc.c +43 -19
index b2a2c2b..2c3874e 100644
@@ -1274,6 +1274,44 @@ int zone_watermark_ok(struct zone *z, int order, unsigned long mark,
 	return 1;
 }
 
+/*
+ * Return 1 if this zone is an acceptable source given the cpuset
+ * constraints.
+ */
+static inline int zone_cpuset_permits(struct zone *zone,
+					int alloc_flags, gfp_t gfp_mask)
+{
+	if ((alloc_flags & ALLOC_CPUSET) &&
+	    !cpuset_zone_allowed_softwall(zone, gfp_mask))
+		return 0;
+	return 1;
+}
+
+/*
+ * Return 1 if this zone is within the watermarks specified by the
+ * allocation flags.
+ */
+static inline int zone_watermark_permits(struct zone *zone, int order,
+			int classzone_idx, int alloc_flags, gfp_t gfp_mask)
+{
+	if (!(alloc_flags & ALLOC_NO_WATERMARKS)) {
+		unsigned long mark;
+		if (alloc_flags & ALLOC_WMARK_MIN)
+			mark = zone->pages_min;
+		else if (alloc_flags & ALLOC_WMARK_LOW)
+			mark = zone->pages_low;
+		else
+			mark = zone->pages_high;
+		if (!zone_watermark_ok(zone, order, mark,
+			    classzone_idx, alloc_flags)) {
+			if (!zone_reclaim_mode ||
+					!zone_reclaim(zone, gfp_mask, order))
+				return 0;
+		}
+	}
+	return 1;
+}
+
 #ifdef CONFIG_NUMA
 /*
  * zlc_setup - Setup for "zonelist cache".  Uses cached zone data to
@@ -1427,25 +1465,11 @@ zonelist_scan:
 		if (NUMA_BUILD && zlc_active &&
 			!zlc_zone_worth_trying(zonelist, z, allowednodes))
 				continue;
-		if ((alloc_flags & ALLOC_CPUSET) &&
-			!cpuset_zone_allowed_softwall(zone, gfp_mask))
-				goto try_next_zone;
-
-		if (!(alloc_flags & ALLOC_NO_WATERMARKS)) {
-			unsigned long mark;
-			if (alloc_flags & ALLOC_WMARK_MIN)
-				mark = zone->pages_min;
-			else if (alloc_flags & ALLOC_WMARK_LOW)
-				mark = zone->pages_low;
-			else
-				mark = zone->pages_high;
-			if (!zone_watermark_ok(zone, order, mark,
-				    classzone_idx, alloc_flags)) {
-				if (!zone_reclaim_mode ||
-				    !zone_reclaim(zone, gfp_mask, order))
-					goto this_zone_full;
-			}
-		}
+		if (!zone_cpuset_permits(zone, alloc_flags, gfp_mask))
+			goto try_next_zone;
+		if (!zone_watermark_permits(zone, order, classzone_idx,
+							alloc_flags, gfp_mask))
+			goto this_zone_full;
 
 		page = buffered_rmqueue(preferred_zone, zone, order, gfp_mask);
 		if (page)
Signature · 7 lines
1.6.0.rc1.258.g80295

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#7 lore raw

PATCH 3/4 buddy: explicitly identify buddy field use in struct page

In reply to Andy Whitcroft

Explicitly define the struct page fields which buddy uses when it owns pages. Defines a new anonymous struct to allow additional fields to be defined in a later patch.

---
 include/linux/mm_types.h |    3 +++
 mm/internal.h            |    2 +-
 mm/page_alloc.c          |    4 ++--
 3 files changed, 6 insertions(+), 3 deletions(-)
include/linux/mm_types.h +3 -0
index 995c588..906d8e0 100644
@@ -70,6 +70,9 @@ struct page {
 #endif
 	    struct kmem_cache *slab;	/* SLUB: Pointer to slab */
 	    struct page *first_page;	/* Compound tail pages */
+	    struct {
+		unsigned long buddy_order;     /* buddy: free page order */
+	    };
 	};
 	union {
 		pgoff_t index;		/* Our offset within mapping. */
mm/internal.h +1 -1
index c0e4859..fcedcd0 100644
@@ -58,7 +58,7 @@ extern void __free_pages_bootmem(struct page *page, unsigned int order);
 static inline unsigned long page_order(struct page *page)
 {
 	VM_BUG_ON(!PageBuddy(page));
-	return page_private(page);
+	return page->buddy_order;
 }
 
 extern int mlock_vma_pages_range(struct vm_area_struct *vma,
mm/page_alloc.c +2 -2
index 2c3874e..db0dbd6 100644
@@ -331,7 +331,7 @@ static inline void prep_zero_page(struct page *page, int order, gfp_t gfp_flags)
 
 static inline void set_page_order(struct page *page, int order)
 {
-	set_page_private(page, order);
+	page->buddy_order = order;
 	__SetPageBuddy(page);
 #ifdef CONFIG_PAGE_OWNER
 		page->order = -1;
@@ -341,7 +341,7 @@ static inline void set_page_order(struct page *page, int order)
 static inline void rmv_page_order(struct page *page)
 {
 	__ClearPageBuddy(page);
-	set_page_private(page, 0);
+	page->buddy_order = 0;
 }
 
 /*
Signature · 7 lines
1.6.0.rc1.258.g80295

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#11 lore raw

PATCH 4/4 capture pages freed during direct reclaim for allocation by the reclaimer

In reply to Andy Whitcroft

When a process enters direct reclaim it will expend effort identifying and releasing pages in the hope of obtaining a page. However as these pages are released asynchronously there is every possibility that the pages will have been consumed by other allocators before the reclaimer gets a look in. This is particularly problematic where the reclaimer is attempting to allocate a higher order page. It is highly likely that a parallel allocation will consume lower order constituent pages as we release them preventing them coelescing into the higher order page the reclaimer desires.

This patch set attempts to address this for allocations above ALLOC_COSTLY_ORDER by temporarily collecting the pages we are releasing onto a local free list. Instead of freeing them to the main buddy lists, pages are collected and coelesced on this per direct reclaimer free list. Pages which are freed by other processes are also considered, where they coelesce with a page already under capture they will be moved to the capture list. When pressure has been applied to a zone we then consult the capture list and if there is an appropriatly sized page available it is taken immediatly and the remainder returned to the free pool. Capture is only enabled when the reclaimer's allocation order exceeds ALLOC_COSTLY_ORDER as free pages below this order should naturally occur in large numbers following regular reclaim.

Thanks go to Mel Gorman for numerous discussions during the development of this patch and for his repeated reviews.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

Reviewed-by on v1 3/4
#8 lore raw

Re: [PATCH 3/4] buddy: explicitly identify buddy field use in struct page

In reply to Andy Whitcroft
Quoted 3 lines · Andy Whitcroft

Explicitly define the struct page fields which buddy uses when it owns pages. Defines a new anonymous struct to allow additional fields to be defined in a later patch.

Good. I have a similar patch floating around.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#13 lore raw

PATCH 4/4 capture pages freed during direct reclaim for allocation by the reclaimer

In reply to Andy Whitcroft

[Doh, as pointed out by Christoph the patch was missing from this one...]

When a process enters direct reclaim it will expend effort identifying and releasing pages in the hope of obtaining a page. However as these pages are released asynchronously there is every possibility that the pages will have been consumed by other allocators before the reclaimer gets a look in. This is particularly problematic where the reclaimer is attempting to allocate a higher order page. It is highly likely that a parallel allocation will consume lower order constituent pages as we release them preventing them coelescing into the higher order page the reclaimer desires.

This patch set attempts to address this for allocations above ALLOC_COSTLY_ORDER by temporarily collecting the pages we are releasing onto a local free list. Instead of freeing them to the main buddy lists, pages are collected and coelesced on this per direct reclaimer free list. Pages which are freed by other processes are also considered, where they coelesce with a page already under capture they will be moved to the capture list. When pressure has been applied to a zone we then consult the capture list and if there is an appropriatly sized page available it is taken immediatly and the remainder returned to the free pool. Capture is only enabled when the reclaimer's allocation order exceeds ALLOC_COSTLY_ORDER as free pages below this order should naturally occur in large numbers following regular reclaim.

Thanks go to Mel Gorman for numerous discussions during the development of this patch and for his repeated reviews.

---
 include/linux/mm_types.h   |    1 +
 include/linux/page-flags.h |    6 ++
 mm/internal.h              |    6 ++
 mm/page_alloc.c            |  154 +++++++++++++++++++++++++++++++++++++++++++-
 mm/vmscan.c                |  115 +++++++++++++++++++++++++++------
 5 files changed, 261 insertions(+), 21 deletions(-)
include/linux/mm_types.h +1 -0
index 906d8e0..cd2b549 100644
@@ -72,6 +72,7 @@ struct page {
 	    struct page *first_page;	/* Compound tail pages */
 	    struct {
 		unsigned long buddy_order;     /* buddy: free page order */
+		struct list_head *buddy_free;  /* buddy: free list pointer */
 	    };
 	};
 	union {
include/linux/page-flags.h +6 -0
index c0ac9e0..b0d8bc7 100644
@@ -117,6 +117,9 @@ enum pageflags {
 	/* SLUB */
 	PG_slub_frozen = PG_active,
 	PG_slub_debug = PG_error,
+
+	/* BUDDY overlays. */
+	PG_buddy_capture = PG_owner_priv_1,
 };
 
 #ifndef __GENERATING_BOUNDS_H
@@ -208,6 +211,9 @@ __PAGEFLAG(SlubDebug, slub_debug)
  */
 TESTPAGEFLAG(Writeback, writeback) TESTSCFLAG(Writeback, writeback)
 __PAGEFLAG(Buddy, buddy)
+PAGEFLAG(BuddyCapture, buddy_capture)	/* A buddy page, but reserved. */
+	__SETPAGEFLAG(BuddyCapture, buddy_capture)
+	__CLEARPAGEFLAG(BuddyCapture, buddy_capture)
 PAGEFLAG(MappedToDisk, mappedtodisk)
 
 /* PG_readahead is only used for file reads; PG_reclaim is only for writes */
mm/internal.h +6 -0
index fcedcd0..f266a35 100644
@@ -251,4 +251,10 @@ int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
 		     unsigned long start, int len, int flags,
 		     struct page **pages, struct vm_area_struct **vmas);
 
+extern struct page *capture_alloc_or_return(struct zone *, struct zone *,
+					struct list_head *, int, int, gfp_t);
+void capture_one_page(struct list_head *, struct zone *, struct page *, int);
+unsigned long try_to_free_pages_capture(struct page **, struct zonelist *,
+					nodemask_t *, int, gfp_t, int);
+
 #endif
mm/page_alloc.c +153 -1

194 lines not on this page · raw message

mm/vmscan.c +95 -20

302 lines not on this page · raw message

Signature · 7 lines
1.6.0.rc1.258.g80295

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#14 lore raw

Re: [PATCH 4/4] capture pages freed during direct reclaim for allocation by the reclaimer

include/linux/page-flags.h __PAGEFLAG(SlubDebug, slub_debug) · line 216

Quoted 9 lines · Andy Whitcroft

#ifndef __GENERATING_BOUNDS_H

include/linux/page-flags.h +3 -0
@@ -208,6 +211,9 @@ __PAGEFLAG(SlubDebug, slub_debug)
  */
 TESTPAGEFLAG(Writeback, writeback) TESTSCFLAG(Writeback, writeback)
 __PAGEFLAG(Buddy, buddy)
+PAGEFLAG(BuddyCapture, buddy_capture)	/* A buddy page, but reserved. */
+	__SETPAGEFLAG(BuddyCapture, buddy_capture)
+	__CLEARPAGEFLAG(BuddyCapture, buddy_capture)

Doesnt __PAGEFLAG do what you want without having to explicitly specify __SET/__CLEAR?

How does page allocator fastpath behavior fare with this pathch?

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

Reviewed-by on v1 2/4
#5 lore raw

Re: [PATCH 2/4] pull out zone cpuset and watermark checks for reuse

In reply to Andy Whitcroft
Quoted 6 lines · On Wed, 3 Sep 2008 19:44:10 +0100 Andy Whitcroft <[email protected]> wrote:

When allocating we need to confirm that the zone we are about to allocate from is acceptable to the CPUSET we are in, and that it does not violate the zone watermarks. Pull these checks out so we can reuse them in a later patch.

Signature · 1 line
All rights reversed.
Reviewed-by on v1 3/4
#9 lore raw

Re: [PATCH 3/4] buddy: explicitly identify buddy field use in struct page

In reply to Andy Whitcroft
Quoted 5 lines · On Wed, 3 Sep 2008 19:44:11 +0100 Andy Whitcroft <[email protected]> wrote:

Explicitly define the struct page fields which buddy uses when it owns pages. Defines a new anonymous struct to allow additional fields to be defined in a later patch.

Signature · 1 line
All rights reversed.
#15 lore raw

Re: [PATCH 4/4] capture pages freed during direct reclaim for allocation by the reclaimer

In reply to Christoph Lameter
Quoted 14 lines · Christoph Lameter

include/linux/page-flags.h __PAGEFLAG(SlubDebug, slub_debug) · line 216

Quoted 9 lines · Andy Whitcroft

#ifndef __GENERATING_BOUNDS_H

include/linux/page-flags.h +3 -0
@@ -208,6 +211,9 @@ __PAGEFLAG(SlubDebug, slub_debug)
  */
 TESTPAGEFLAG(Writeback, writeback) TESTSCFLAG(Writeback, writeback)
 __PAGEFLAG(Buddy, buddy)
+PAGEFLAG(BuddyCapture, buddy_capture)	/* A buddy page, but reserved. */
+	__SETPAGEFLAG(BuddyCapture, buddy_capture)
+	__CLEARPAGEFLAG(BuddyCapture, buddy_capture)

Doesnt __PAGEFLAG do what you want without having to explicitly specify __SET/__CLEAR?

PAGEFLAG() __PAGEFLAG()

does TESTPAGEFLAG() double.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

Acked-by on v1 4/4
#19 lore raw

Re: [PATCH 4/4] capture pages freed during direct reclaim for allocation by the reclaimer

In reply to Andy Whitcroft
Quoted 25 lines · Andy Whitcroft

[Doh, as pointed out by Christoph the patch was missing from this one...]

When a process enters direct reclaim it will expend effort identifying and releasing pages in the hope of obtaining a page. However as these pages are released asynchronously there is every possibility that the pages will have been consumed by other allocators before the reclaimer gets a look in. This is particularly problematic where the reclaimer is attempting to allocate a higher order page. It is highly likely that a parallel allocation will consume lower order constituent pages as we release them preventing them coelescing into the higher order page the reclaimer desires.

This patch set attempts to address this for allocations above ALLOC_COSTLY_ORDER by temporarily collecting the pages we are releasing onto a local free list. Instead of freeing them to the main buddy lists, pages are collected and coelesced on this per direct reclaimer free list. Pages which are freed by other processes are also considered, where they coelesce with a page already under capture they will be moved to the capture list. When pressure has been applied to a zone we then consult the capture list and if there is an appropriatly sized page available it is taken immediatly and the remainder returned to the free pool. Capture is only enabled when the reclaimer's allocation order exceeds ALLOC_COSTLY_ORDER as free pages below this order should naturally occur in large numbers following regular reclaim.

Quoted 2 lines

Thanks go to Mel Gorman for numerous discussions during the development of this patch and for his repeated reviews.

Whole series looks good, a few comments below.

Quoted 4 lines

---

mm/page_alloc.c out: · line 4909

Quoted 10 lines
mm/page_alloc.c +7 -0
@@ -4815,6 +4900,73 @@ out:
 	spin_unlock_irqrestore(&zone->lock, flags);
 }
 
+#define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
+
+/*
+ * Run through the accumulated list of captured pages and the first
+ * which is big enough to satisfy the original allocation.  Free
+ * the remainder of that page and all other pages.
+ */

That sentence looks incomplete, did you intend to write something along the lines of:

Run through the accumulated list of captures pages and /take/ the first which is big enough to satisfy the original allocation. Free the remaining pages.

?

Quoted 35 lines
mm/page_alloc.c +35 -0
+struct page *capture_alloc_or_return(struct zone *zone,
+		struct zone *preferred_zone, struct list_head *capture_list,
+		int order, int alloc_flags, gfp_t gfp_mask)
+{
+	struct page *capture_page = 0;
+	unsigned long flags;
+	int classzone_idx = zone_idx(preferred_zone);
+
+	spin_lock_irqsave(&zone->lock, flags);
+
+	while (!list_empty(capture_list)) {
+		struct page *page;
+		int pg_order;
+
+		page = lru_to_page(capture_list);
+		list_del(&page->lru);
+		pg_order = page_order(page);
+
+		/*
+		 * Clear out our buddy size and list information before
+		 * releasing or allocating the page.
+		 */
+		rmv_page_order(page);
+		page->buddy_free = 0;
+		ClearPageBuddyCapture(page);
+
+		if (!capture_page && pg_order >= order) {
+			__carve_off(page, pg_order, order);
+			capture_page = page;
+		} else
+			__free_one_page(page, zone, pg_order);
+	}
+
+	/*
+	 * Ensure that this capture would not violate the watermarks.

mm/page_alloc.c out: · line 4954

Quoted 10 lines
mm/page_alloc.c +10 -0
+	 * Subtle, we actually already have the page outside the watermarks
+	 * so check if we can allocate an order 0 page.
+	 */
+	if (capture_page &&
+	    (!zone_cpuset_permits(zone, alloc_flags, gfp_mask) ||
+	     !zone_watermark_permits(zone, 0, classzone_idx,
+					     alloc_flags, gfp_mask))) {
+		__free_one_page(capture_page, zone, order);
+		capture_page = NULL;
+	}

This makes me a little sad - we got a high order page and give it away again...

Can we start another round of direct reclaim with a lower order to try and increase the watermarks while we hold on to this large order page?

Quoted 7 lines
mm/page_alloc.c +7 -0
+	if (capture_page)
+		__count_zone_vm_events(PGALLOC, zone, 1 << order);
+
+	zone_clear_flag(zone, ZONE_ALL_UNRECLAIMABLE);
+	zone->pages_scanned = 0;
+
+	spin_unlock_irqrestore(&zone->lock, flags);

mm/page_alloc.c out: · line 4972

Quoted 10 lines
mm/page_alloc.c +7 -0
+
+	if (capture_page)
+		prep_new_page(capture_page, order, gfp_mask);
+
+	return capture_page;
+}
+
 #ifdef CONFIG_MEMORY_HOTREMOVE
 /*
  * All pages in the range must be isolated before calling this.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#21 lore raw

Re: [PATCH 4/4] capture pages freed during direct reclaim for allocation by the reclaimer

In reply to Andy Whitcroft
Quoted 10 lines

When a process enters direct reclaim it will expend effort identifying and releasing pages in the hope of obtaining a page. However as these pages are released asynchronously there is every possibility that the pages will have been consumed by other allocators before the reclaimer gets a look in. This is particularly problematic where the reclaimer is attempting to allocate a higher order page. It is highly likely that a parallel allocation will consume lower order constituent pages as we release them preventing them coelescing into the higher order page the reclaimer desires.

Quoted 12 lines

This patch set attempts to address this for allocations above ALLOC_COSTLY_ORDER by temporarily collecting the pages we are releasing onto a local free list. Instead of freeing them to the main buddy lists, pages are collected and coelesced on this per direct reclaimer free list. Pages which are freed by other processes are also considered, where they coelesce with a page already under capture they will be moved to the capture list. When pressure has been applied to a zone we then consult the capture list and if there is an appropriatly sized page available it is taken immediatly and the remainder returned to the free pool. Capture is only enabled when the reclaimer's allocation order exceeds ALLOC_COSTLY_ORDER as free pages below this order should naturally occur in large numbers following regular reclaim.

Hi Andy,

I like almost part of your patch.
(at least, I can ack patch 1/4 - 3/4)

So, I worry about OOM risk.
Can you remember desired page size to capture list (or any other location)? if possible, __capture_on_page can avoid to capture unnecessary pages.

So, if __capture_on_page() can make desired size page by buddy merging, it can free other pages on capture_list.

In worst case, shrink_zone() is called by very much process at the same time. Then, if each process doesn't back few pages, very many pages doesn't be backed.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#17 lore raw

Re: [PATCH 4/4] capture pages freed during direct reclaim for allocation by the reclaimer

In reply to Christoph Lameter

Hi Cristoph

Quoted 1 line

How does page allocator fastpath behavior fare with this pathch?

Don't worry it because

1. shrink_zone() isn't fastpath because any reclaim isn't fastpath.
2. buddy combining on __free_one_page() isn't fastpath because any buddy combining isn't fastpath. (*)

(*)
all modern allocator have delayed buddy combining mecanism because buddy combining increase cache miss.
(please imazine address X+1 is freed when address X is cold.
combining cause next alloc get address X, then caller see cold page)

at least, allocator's fastpath should avoid its combining IMHO.

Unfortunately the linux buddy's one is limited because zone->pcp only cache order-0 page.

Then, higher order pages's free always use slow path now. but it isn't his patch failure.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#18 lore raw

Re: [PATCH 4/4] capture pages freed during direct reclaim for allocation by the reclaimer

In reply to Christoph Lameter
Quoted 14 lines · Christoph Lameter

include/linux/page-flags.h __PAGEFLAG(SlubDebug, slub_debug) · line 216

Quoted 9 lines · Andy Whitcroft

#ifndef __GENERATING_BOUNDS_H

include/linux/page-flags.h +3 -0
@@ -208,6 +211,9 @@ __PAGEFLAG(SlubDebug, slub_debug)
  */
 TESTPAGEFLAG(Writeback, writeback) TESTSCFLAG(Writeback, writeback)
 __PAGEFLAG(Buddy, buddy)
+PAGEFLAG(BuddyCapture, buddy_capture)	/* A buddy page, but reserved. */
+	__SETPAGEFLAG(BuddyCapture, buddy_capture)
+	__CLEARPAGEFLAG(BuddyCapture, buddy_capture)

Doesnt __PAGEFLAG do what you want without having to explicitly specify __SET/__CLEAR?

I think I end up with one extra test that I don't need, but its probabally much clearer.

Quoted 1 line

How does page allocator fastpath behavior fare with this pathch?

The fastpath should be unaffected on the allocation side. On the free side there is an additional check for merging with a buddy under capture as we merge buddies in __free_one_page.

-apw

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#20 lore raw

Re: [PATCH 4/4] capture pages freed during direct reclaim for allocation by the reclaimer

In reply to Peter Zijlstra
Quoted 51 lines · Peter Zijlstra
Quoted 25 lines · Andy Whitcroft

[Doh, as pointed out by Christoph the patch was missing from this one...]

When a process enters direct reclaim it will expend effort identifying and releasing pages in the hope of obtaining a page. However as these pages are released asynchronously there is every possibility that the pages will have been consumed by other allocators before the reclaimer gets a look in. This is particularly problematic where the reclaimer is attempting to allocate a higher order page. It is highly likely that a parallel allocation will consume lower order constituent pages as we release them preventing them coelescing into the higher order page the reclaimer desires.

This patch set attempts to address this for allocations above ALLOC_COSTLY_ORDER by temporarily collecting the pages we are releasing onto a local free list. Instead of freeing them to the main buddy lists, pages are collected and coelesced on this per direct reclaimer free list. Pages which are freed by other processes are also considered, where they coelesce with a page already under capture they will be moved to the capture list. When pressure has been applied to a zone we then consult the capture list and if there is an appropriatly sized page available it is taken immediatly and the remainder returned to the free pool. Capture is only enabled when the reclaimer's allocation order exceeds ALLOC_COSTLY_ORDER as free pages below this order should naturally occur in large numbers following regular reclaim.

Quoted 2 lines

Thanks go to Mel Gorman for numerous discussions during the development of this patch and for his repeated reviews.

Whole series looks good, a few comments below.

Quoted 4 lines

---

mm/page_alloc.c out: · line 4909

Quoted 10 lines
mm/page_alloc.c +7 -0
@@ -4815,6 +4900,73 @@ out:
 	spin_unlock_irqrestore(&zone->lock, flags);
 }
 
+#define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
+
+/*
+ * Run through the accumulated list of captured pages and the first
+ * which is big enough to satisfy the original allocation.  Free
+ * the remainder of that page and all other pages.
+ */

That sentence looks incomplete, did you intend to write something along the lines of:

Quoted 5 lines

Run through the accumulated list of captures pages and /take/ the first which is big enough to satisfy the original allocation. Free the remaining pages.

?

Yeah that is more like it. Updated.

Quoted 49 lines
Quoted 35 lines
mm/page_alloc.c +35 -0
+struct page *capture_alloc_or_return(struct zone *zone,
+		struct zone *preferred_zone, struct list_head *capture_list,
+		int order, int alloc_flags, gfp_t gfp_mask)
+{
+	struct page *capture_page = 0;
+	unsigned long flags;
+	int classzone_idx = zone_idx(preferred_zone);
+
+	spin_lock_irqsave(&zone->lock, flags);
+
+	while (!list_empty(capture_list)) {
+		struct page *page;
+		int pg_order;
+
+		page = lru_to_page(capture_list);
+		list_del(&page->lru);
+		pg_order = page_order(page);
+
+		/*
+		 * Clear out our buddy size and list information before
+		 * releasing or allocating the page.
+		 */
+		rmv_page_order(page);
+		page->buddy_free = 0;
+		ClearPageBuddyCapture(page);
+
+		if (!capture_page && pg_order >= order) {
+			__carve_off(page, pg_order, order);
+			capture_page = page;
+		} else
+			__free_one_page(page, zone, pg_order);
+	}
+
+	/*
+	 * Ensure that this capture would not violate the watermarks.

mm/page_alloc.c out: · line 4954

Quoted 10 lines
mm/page_alloc.c +10 -0
+	 * Subtle, we actually already have the page outside the watermarks
+	 * so check if we can allocate an order 0 page.
+	 */
+	if (capture_page &&
+	    (!zone_cpuset_permits(zone, alloc_flags, gfp_mask) ||
+	     !zone_watermark_permits(zone, 0, classzone_idx,
+					     alloc_flags, gfp_mask))) {
+		__free_one_page(capture_page, zone, order);
+		capture_page = NULL;
+	}

This makes me a little sad - we got a high order page and give it away again...

Quoted 2 lines

Can we start another round of direct reclaim with a lower order to try and increase the watermarks while we hold on to this large order page?

Well in theory we have already pushed a load of other pages back, the ones we discarded during the capture selection. This actually triggers very rarely in real use, without it we would occasionally OOM but it was rare. Looking at some stats collected when running our tests I have yet to see it trigger. So its probabally not worth any additional effort there.

Quoted 9 lines
Quoted 9 lines
mm/page_alloc.c +9 -0
+	if (capture_page)
+		__count_zone_vm_events(PGALLOC, zone, 1 << order);
+
+	zone_clear_flag(zone, ZONE_ALL_UNRECLAIMABLE);
+	zone->pages_scanned = 0;
+
+	spin_unlock_irqrestore(&zone->lock, flags);
+
+	if (capture_page)
Quoted 8 lines

mm/page_alloc.c out: · line 4972

Quoted 8 lines
mm/page_alloc.c +5 -0
+		prep_new_page(capture_page, order, gfp_mask);
+
+	return capture_page;
+}
+
 #ifdef CONFIG_MEMORY_HOTREMOVE
 /*
  * All pages in the range must be isolated before calling this.

-apw

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#16 lore raw

Re: [PATCH 4/4] capture pages freed during direct reclaim for allocation by the reclaimer

In reply to Peter Zijlstra
Quoted 15 lines · Peter Zijlstra
Quoted 13 lines · Christoph Lameter

include/linux/page-flags.h __PAGEFLAG(SlubDebug, slub_debug) · line 216

Quoted 9 lines · Andy Whitcroft

#ifndef __GENERATING_BOUNDS_H

include/linux/page-flags.h +3 -0
@@ -208,6 +211,9 @@ __PAGEFLAG(SlubDebug, slub_debug)
  */
 TESTPAGEFLAG(Writeback, writeback) TESTSCFLAG(Writeback, writeback)
 __PAGEFLAG(Buddy, buddy)
+PAGEFLAG(BuddyCapture, buddy_capture)	/* A buddy page, but reserved. */
+	__SETPAGEFLAG(BuddyCapture, buddy_capture)
+	__CLEARPAGEFLAG(BuddyCapture, buddy_capture)

Doesnt __PAGEFLAG do what you want without having to explicitly specify __SET/__CLEAR?

Quoted 4 lines

PAGEFLAG() __PAGEFLAG()

does TESTPAGEFLAG() double.

Usually one either wants the atomic versions or the non atomic versions. This usage seems to be mainly non atomic plus one use of ClearPageBuddy() in capture_or_return() (Which raises some questions about how the bit modifications are serialized. Is there concurrency during free?)

So

__PAGEFLAG(BuddyCapture, buddy_capture)
CLEARPAGEFLAG(BuddyCapture, buddy_capture)

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#22 lore raw

Re: [PATCH 4/4] capture pages freed during direct reclaim for allocation by the reclaimer

In reply to KOSAKI Motohiro
Quoted 33 lines · KOSAKI Motohiro
Quoted 10 lines

When a process enters direct reclaim it will expend effort identifying and releasing pages in the hope of obtaining a page. However as these pages are released asynchronously there is every possibility that the pages will have been consumed by other allocators before the reclaimer gets a look in. This is particularly problematic where the reclaimer is attempting to allocate a higher order page. It is highly likely that a parallel allocation will consume lower order constituent pages as we release them preventing them coelescing into the higher order page the reclaimer desires.

Quoted 12 lines

This patch set attempts to address this for allocations above ALLOC_COSTLY_ORDER by temporarily collecting the pages we are releasing onto a local free list. Instead of freeing them to the main buddy lists, pages are collected and coelesced on this per direct reclaimer free list. Pages which are freed by other processes are also considered, where they coelesce with a page already under capture they will be moved to the capture list. When pressure has been applied to a zone we then consult the capture list and if there is an appropriatly sized page available it is taken immediatly and the remainder returned to the free pool. Capture is only enabled when the reclaimer's allocation order exceeds ALLOC_COSTLY_ORDER as free pages below this order should naturally occur in large numbers following regular reclaim.

Hi Andy,

I like almost part of your patch.
(at least, I can ack patch 1/4 - 3/4)

So, I worry about OOM risk.
Can you remember desired page size to capture list (or any other location)? if possible, __capture_on_page can avoid to capture unnecessary pages.

Quoted 5 lines

So, if __capture_on_page() can make desired size page by buddy merging, it can free other pages on capture_list.

In worst case, shrink_zone() is called by very much process at the same time. Then, if each process doesn't back few pages, very many pages doesn't be backed.

The testing we have done pushes the system pretty damn hard, about as hard as you can. Without the zone watermark checks in capture we would periodically lose a test to an OOM. Since adding that I have never seen an OOM, so I am confident we are safe. That said, clearly some wider testing in -mm would be very desirable to confirm that this does not tickle OOM for some unexpected workload.

I think the idea of trying to short-circuit capture once it has a page of the requisit order or greater is eminently sensible. I suspect we are going to have trouble getting the information to the right place, but it is clearly worth investigating. It feels like a logical step on top of this, so I would propose to do it as a patch on top of this set.

Thanks for your feedback.

-apw

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

Acked-by on v1 1/4
#3 lore raw

Re: [PATCH 1/4] pull out the page pre-release and sanity check logic for reuse

In reply to Andy Whitcroft
Quoted 10 lines

When we are about to release a page we perform a number of actions on that page. We clear down any anonymous mappings, confirm that the page is safe to release, check for freeing locks, before mapping the page should that be required. Pull this processing out into a helper function for reuse in a later patch.

Note that we do not convert the similar cleardown in free_hot_cold_page() as the optimiser is unable to squash the loops during the inline.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

Acked-by on v1 2/4
#6 lore raw

Re: [PATCH 2/4] pull out zone cpuset and watermark checks for reuse

In reply to Andy Whitcroft
Quoted 6 lines

When allocating we need to confirm that the zone we are about to allocate from is acceptable to the CPUSET we are in, and that it does not violate the zone watermarks. Pull these checks out so we can reuse them in a later patch.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

Acked-by on v1 3/4
#10 lore raw

Re: [PATCH 3/4] buddy: explicitly identify buddy field use in struct page

In reply to Andy Whitcroft
Quoted 5 lines

Explicitly define the struct page fields which buddy uses when it owns pages. Defines a new anonymous struct to allow additional fields to be defined in a later patch.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#23 lore raw

Re: [PATCH 4/4] capture pages freed during direct reclaim for allocation by the reclaimer

In reply to Andy Whitcroft
Quoted 22 lines
Quoted 9 lines

Hi Andy,

I like almost part of your patch.
(at least, I can ack patch 1/4 - 3/4)

So, I worry about OOM risk.
Can you remember desired page size to capture list (or any other location)? if possible, __capture_on_page can avoid to capture unnecessary pages.

Quoted 5 lines

So, if __capture_on_page() can make desired size page by buddy merging, it can free other pages on capture_list.

In worst case, shrink_zone() is called by very much process at the same time. Then, if each process doesn't back few pages, very many pages doesn't be backed.

The testing we have done pushes the system pretty damn hard, about as hard as you can. Without the zone watermark checks in capture we would periodically lose a test to an OOM. Since adding that I have never seen an OOM, so I am confident we are safe. That said, clearly some wider testing in -mm would be very desirable to confirm that this does not tickle OOM for some unexpected workload.

Quoted 7 lines

I think the idea of trying to short-circuit capture once it has a page of the requisit order or greater is eminently sensible. I suspect we are going to have trouble getting the information to the right place, but it is clearly worth investigating. It feels like a logical step on top of this, so I would propose to do it as a patch on top of this set.

Thanks for your feedback.

Ok. makes sense.
Thanks for good patch.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>