Skip to contentSkip to conversation

cma: cached pageblock type fixup

PATCH In review Bartlomiej Zolnierkiewicz 3 participants 7 messages May 23, 2012 – Jun 19, 2012

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

PATCH 1/1 cma: cached pageblock type fixup

From: Bartlomiej Zolnierkiewicz <[email protected]>
Subject: [PATCH] cma: cached pageblock type fixup

CMA pages added to per-cpu pages lists in free_hot_cold_page() have private field set to MIGRATE_CMA pageblock type . If this happes just before start_isolate_page_range() in alloc_contig_range() changes pageblock type of the page to MIGRATE_ISOLATE it may result in the cached pageblock type being stale in free_pcppages_bulk() (which may be triggered by drain_all_pages() in alloc_contig_range()), page being added to MIGRATE_CMA free list instead of MIGRATE_ISOLATE one in __free_one_page() and (if the page is reused just before test_pages_isolated() check) causing alloc_contig_range() failure.

Fix such situation by checking whether pageblock type of the page changed to MIGRATE_ISOLATE for MIGRATE_CMA type pages in free_pcppages_bulk() and if so fixup the pageblock type to MIGRATE_ISOLATE (so the page will be added to MIGRATE_ISOLATE free list in __free_one_page() and won't be used).

Similar situation can happen if rmqueue_bulk() sets cached pageblock of the page to MIGRATE_CMA and start_isolate_page_range() is called before buffered_rmqueue() completes (so the page may used by get_page_from_freelist() and cause test_pages_isolated() check failure in alloc_contig_range()). Fix it in buffered_rmqueue() by changing the pageblock type of the affected page if needed, freeing page back to buddy allocator and retrying the allocation.

Please note that even with this patch applied some page allocation vs alloc_contig_range() races are still possible and may result in rare test_pages_isolated() failures.

---
 mm/page_alloc.c |   38 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)
mm/page_alloc.c +36 -2
Index: b/mm/page_alloc.c
===================================================================
@@ -664,12 +664,24 @@
 			batch_free = to_free;
 
 		do {
+			int mt;
+
 			page = list_entry(list->prev, struct page, lru);
 			/* must delete as __free_one_page list manipulates */
 			list_del(&page->lru);
+
+			mt = page_private(page);
+			/*
+			 * cached MIGRATE_CMA pageblock type may have changed
+			 * during isolation
+			 */
+			if (is_migrate_cma(mt) &&
+			    get_pageblock_migratetype(page) == MIGRATE_ISOLATE)
+				mt = MIGRATE_ISOLATE;
+
 			/* MIGRATE_MOVABLE list may include MIGRATE_RESERVEs */
-			__free_one_page(page, zone, 0, page_private(page));
-			trace_mm_page_pcpu_drain(page, 0, page_private(page));
+			__free_one_page(page, zone, 0, mt);
+			trace_mm_page_pcpu_drain(page, 0, mt);
 		} while (--to_free && --batch_free && !list_empty(list));
 	}
 	__mod_zone_page_state(zone, NR_FREE_PAGES, count);
@@ -1440,6 +1452,7 @@
 	if (likely(order == 0)) {
 		struct per_cpu_pages *pcp;
 		struct list_head *list;
+		int mt;
 
 		local_irq_save(flags);
 		pcp = &this_cpu_ptr(zone->pageset)->pcp;
@@ -1459,6 +1472,27 @@
 
 		list_del(&page->lru);
 		pcp->count--;
+
+		spin_lock(&zone->lock);
+		mt = page_private(page);
+		/*
+		 * cached MIGRATE_CMA pageblock type may have changed
+		 * during isolation
+		 */
+		if ((is_migrate_cma(mt) &&
+		     get_pageblock_migratetype(page) == MIGRATE_ISOLATE) ||
+		    mt == MIGRATE_ISOLATE) {
+			mt = MIGRATE_ISOLATE;
+
+			zone->all_unreclaimable = 0;
+			zone->pages_scanned = 0;
+
+			__free_one_page(page, zone, 0, mt);
+			__mod_zone_page_state(zone, NR_FREE_PAGES, 1);
+			spin_unlock(&zone->lock);
+			goto again;
+		} else
+			spin_unlock(&zone->lock);
 	} else {
 		if (unlikely(gfp_flags & __GFP_NOFAIL)) {
 			/*

--
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/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#1 lore raw

Hello,

Quoted 28 lines · Bartlomiej Zolnierkiewicz

CMA pages added to per-cpu pages lists in free_hot_cold_page() have private field set to MIGRATE_CMA pageblock type . If this happes just before start_isolate_page_range() in alloc_contig_range() changes pageblock type of the page to MIGRATE_ISOLATE it may result in the cached pageblock type being stale in free_pcppages_bulk() (which may be triggered by drain_all_pages() in alloc_contig_range()), page being added to MIGRATE_CMA free list instead of MIGRATE_ISOLATE one in __free_one_page() and (if the page is reused just before test_pages_isolated() check) causing alloc_contig_range() failure.

Fix such situation by checking whether pageblock type of the page changed to MIGRATE_ISOLATE for MIGRATE_CMA type pages in free_pcppages_bulk() and if so fixup the pageblock type to MIGRATE_ISOLATE (so the page will be added to MIGRATE_ISOLATE free list in __free_one_page() and won't be used).

Similar situation can happen if rmqueue_bulk() sets cached pageblock of the page to MIGRATE_CMA and start_isolate_page_range() is called before buffered_rmqueue() completes (so the page may used by get_page_from_freelist() and cause test_pages_isolated() check failure in alloc_contig_range()). Fix it in buffered_rmqueue() by changing the pageblock type of the affected page if needed, freeing page back to buddy allocator and retrying the allocation.

Please note that even with this patch applied some page allocation vs alloc_contig_range() races are still possible and may result in rare test_pages_isolated() failures.

Quoted 5 lines

Mel, do you have any suggestions or comments how can we deal with this issue?

Quoted 61 lines
---
 mm/page_alloc.c |   38 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)

Index: b/mm/page_alloc.c
===================================================================

mm/page_alloc.c +29 -2
@@ -664,12 +664,24 @@
 			batch_free = to_free;
 
 		do {
+			int mt;
+
 			page = list_entry(list->prev, struct page, lru);
 			/* must delete as __free_one_page list manipulates */
 			list_del(&page->lru);
+
+			mt = page_private(page);
+			/*
+			 * cached MIGRATE_CMA pageblock type may have changed
+			 * during isolation
+			 */
+			if (is_migrate_cma(mt) &&
+			    get_pageblock_migratetype(page) == MIGRATE_ISOLATE)
+				mt = MIGRATE_ISOLATE;
+
 			/* MIGRATE_MOVABLE list may include MIGRATE_RESERVEs */
-			__free_one_page(page, zone, 0, page_private(page));
-			trace_mm_page_pcpu_drain(page, 0, page_private(page));
+			__free_one_page(page, zone, 0, mt);
+			trace_mm_page_pcpu_drain(page, 0, mt);
 		} while (--to_free && --batch_free && !list_empty(list));
 	}
 	__mod_zone_page_state(zone, NR_FREE_PAGES, count);
@@ -1440,6 +1452,7 @@
 	if (likely(order == 0)) {
 		struct per_cpu_pages *pcp;
 		struct list_head *list;
+		int mt;
 
 		local_irq_save(flags);
 		pcp = &this_cpu_ptr(zone->pageset)->pcp;
@@ -1459,6 +1472,27 @@
 
 		list_del(&page->lru);
 		pcp->count--;
+
+		spin_lock(&zone->lock);
+		mt = page_private(page);
+		/*
+		 * cached MIGRATE_CMA pageblock type may have changed
+		 * during isolation
+		 */
+		if ((is_migrate_cma(mt) &&
+		     get_pageblock_migratetype(page) == MIGRATE_ISOLATE) ||
+		    mt == MIGRATE_ISOLATE) {
+			mt = MIGRATE_ISOLATE;
+
+			zone->all_unreclaimable = 0;
+			zone->pages_scanned = 0;

mm/page_alloc.c line 1498

Quoted 10 lines
mm/page_alloc.c +7 -0
+
+			__free_one_page(page, zone, 0, mt);
+			__mod_zone_page_state(zone, NR_FREE_PAGES, 1);
+			spin_unlock(&zone->lock);
+			goto again;
+		} else
+			spin_unlock(&zone->lock);
 	} else {
 		if (unlikely(gfp_flags & __GFP_NOFAIL)) {
 			/*

Best regards

Signature · 10 lines
Marek Szyprowski
Samsung Poland R&D Center



--
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>
#2 lore raw
In reply to Bartlomiej Zolnierkiewicz
Quoted 9 lines · Bartlomiej Zolnierkiewicz

From: Bartlomiej Zolnierkiewicz <[email protected]>
Subject: [PATCH] cma: cached pageblock type fixup

CMA pages added to per-cpu pages lists in free_hot_cold_page() have private field set to MIGRATE_CMA pageblock type . If this happes just before start_isolate_page_range() in alloc_contig_range() changes pageblock type of the page to MIGRATE_ISOLATE it may result in the cached pageblock type being stale in free_pcppages_bulk() (which may be triggered by drain_all_pages() in alloc_contig_range()),

So what?

The pages get freed to the MIGRATE_CMA region. At worst they will be used for an allocation request that is compatible with being migrated by CMA. This will delay the allocation time of alloc_contig_range() but is hardly critical.

Your fix on the other hand adds another call to get_pageblock_type() to free_pcppages_bulk which is expensive. The change made to buffered_rmqueue() is horrific. It takes the per-cpu page allocation path and adds a spin lock to it which completely defeats the purpose of having the per-cpu allocation avoid taking locks. This will have a very heavy impact on performance, particularly on parallel workloads.

As the impact of the race should be marginal and the cost of the fix is so unbelivably high I'm nacking this patch. If this race is a problem then it should be handled in alloc_contig_range() not in the allocator fast paths.

Signature · 8 lines
Mel Gorman
SUSE Labs

--
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>
#3 lore raw
Quoted 4 lines · On Tuesday 19 June 2012 11:00:15 Mel Gorman wrote:
Quoted 3 lines · Bartlomiej Zolnierkiewicz

From: Bartlomiej Zolnierkiewicz <[email protected]>
Subject: [PATCH] cma: cached pageblock type fixup

Quoted 8 lines
Quoted 6 lines

CMA pages added to per-cpu pages lists in free_hot_cold_page() have private field set to MIGRATE_CMA pageblock type . If this happes just before start_isolate_page_range() in alloc_contig_range() changes pageblock type of the page to MIGRATE_ISOLATE it may result in the cached pageblock type being stale in free_pcppages_bulk() (which may be triggered by drain_all_pages() in alloc_contig_range()),

So what?

"page being added to MIGRATE_CMA free list instead of MIGRATE_ISOLATE one in __free_one_page() and (if the page is reused just before test_pages_isolated() check) causing alloc_contig_range() failure."

Quoted 4 lines

The pages get freed to the MIGRATE_CMA region. At worst they will be used for an allocation request that is compatible with being migrated by CMA. This will delay the allocation time of alloc_contig_range() but is hardly critical.

There is no waiting on MIGRATE_CMA pages in alloc_contig_range() to become free again. If the conversion to MIGRATE_ISOLATE fails then the whole alloc_contig_range() allocation fails on test_pages_isolated() check.

Quoted 10 lines

Your fix on the other hand adds another call to get_pageblock_type() to free_pcppages_bulk which is expensive. The change made to buffered_rmqueue() is horrific. It takes the per-cpu page allocation path and adds a spin lock to it which completely defeats the purpose of having the per-cpu allocation avoid taking locks. This will have a very heavy impact on performance, particularly on parallel workloads.

As the impact of the race should be marginal and the cost of the fix is so unbelivably high I'm nacking this patch. If this race is a problem then it should be handled in alloc_contig_range() not in the allocator fast paths.

Do you have any idea how this race can be handled in alloc_contig_range()?

Best regards,
--
Bartlomiej Zolnierkiewicz
Samsung Poland R&D Center

--
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
Quoted 12 lines · Bartlomiej Zolnierkiewicz
Quoted 11 lines · On Tuesday 19 June 2012 11:00:15 Mel Gorman wrote:
Quoted 9 lines · Bartlomiej Zolnierkiewicz

From: Bartlomiej Zolnierkiewicz <[email protected]>
Subject: [PATCH] cma: cached pageblock type fixup

CMA pages added to per-cpu pages lists in free_hot_cold_page() have private field set to MIGRATE_CMA pageblock type . If this happes just before start_isolate_page_range() in alloc_contig_range() changes pageblock type of the page to MIGRATE_ISOLATE it may result in the cached pageblock type being stale in free_pcppages_bulk() (which may be triggered by drain_all_pages() in alloc_contig_range()),

Quoted 6 lines
Quoted 1 line

So what?

"page being added to MIGRATE_CMA free list instead of MIGRATE_ISOLATE one in __free_one_page() and (if the page is reused just before test_pages_isolated() check) causing alloc_contig_range() failure."

And.... so what?

If it's on the wrong free list then it can still be isolated from that list if it has not been allocated. If it has been allocated then it must be a MIGRATE_CMA-compatible allocation so the page can be migrated and the operation retried.

Quoted 10 lines
Quoted 4 lines

The pages get freed to the MIGRATE_CMA region. At worst they will be used for an allocation request that is compatible with being migrated by CMA. This will delay the allocation time of alloc_contig_range() but is hardly critical.

There is no waiting on MIGRATE_CMA pages in alloc_contig_range() to become free again. If the conversion to MIGRATE_ISOLATE fails then the whole alloc_contig_range() allocation fails on test_pages_isolated() check.

Then retry the operation if alloc_contig_range() finds it raced.

Quoted 7 lines
Quoted 7 lines

Your fix on the other hand adds another call to get_pageblock_type() to free_pcppages_bulk which is expensive. The change made to buffered_rmqueue() is horrific. It takes the per-cpu page allocation path and adds a spin lock to it which completely defeats the purpose of having the per-cpu allocation avoid taking locks. This will have a very heavy impact on performance, particularly on parallel workloads.

Quoted 6 lines
Quoted 3 lines

As the impact of the race should be marginal and the cost of the fix is so unbelivably high I'm nacking this patch. If this race is a problem then it should be handled in alloc_contig_range() not in the allocator fast paths.

Do you have any idea how this race can be handled in alloc_contig_range()?

If the page is on the wrong free list, just isolate it or move it to the MIGRATE_ISOLATE free list at that point. If it has been allocated then migrate it and move the resulting free page to the MIGRATE_ISOLATE list.

Signature · 8 lines
Mel Gorman
SUSE Labs

--
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>
#5 lore raw
Quoted 32 lines · On Tuesday 19 June 2012 14:00:45 Mel Gorman wrote:
Quoted 12 lines · Bartlomiej Zolnierkiewicz
Quoted 11 lines · On Tuesday 19 June 2012 11:00:15 Mel Gorman wrote:
Quoted 9 lines · Bartlomiej Zolnierkiewicz

From: Bartlomiej Zolnierkiewicz <[email protected]>
Subject: [PATCH] cma: cached pageblock type fixup

CMA pages added to per-cpu pages lists in free_hot_cold_page() have private field set to MIGRATE_CMA pageblock type . If this happes just before start_isolate_page_range() in alloc_contig_range() changes pageblock type of the page to MIGRATE_ISOLATE it may result in the cached pageblock type being stale in free_pcppages_bulk() (which may be triggered by drain_all_pages() in alloc_contig_range()),

Quoted 6 lines
Quoted 1 line

So what?

"page being added to MIGRATE_CMA free list instead of MIGRATE_ISOLATE one in __free_one_page() and (if the page is reused just before test_pages_isolated() check) causing alloc_contig_range() failure."

And.... so what?

If it's on the wrong free list then it can still be isolated from that list if it has not been allocated. If it has been allocated then it must be a MIGRATE_CMA-compatible allocation so the page can be migrated and the operation retried.

Quoted 5 lines
Quoted 4 lines

The pages get freed to the MIGRATE_CMA region. At worst they will be used for an allocation request that is compatible with being migrated by CMA. This will delay the allocation time of alloc_contig_range() but is hardly critical.

Quoted 7 lines
Quoted 5 lines

There is no waiting on MIGRATE_CMA pages in alloc_contig_range() to become free again. If the conversion to MIGRATE_ISOLATE fails then the whole alloc_contig_range() allocation fails on test_pages_isolated() check.

Then retry the operation if alloc_contig_range() finds it raced.

Right, my other patch is trying to go in this direction:
http://marc.info/?l=linux-mm&m=133775796822644&w=2

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

Your fix on the other hand adds another call to get_pageblock_type() to free_pcppages_bulk which is expensive. The change made to buffered_rmqueue() is horrific. It takes the per-cpu page allocation path and adds a spin lock to it which completely defeats the purpose of having the per-cpu allocation avoid taking locks. This will have a very heavy impact on performance, particularly on parallel workloads.

As the impact of the race should be marginal and the cost of the fix is so unbelivably high I'm nacking this patch. If this race is a problem then it should be handled in alloc_contig_range() not in the allocator fast paths.

Quoted 6 lines
Quoted 2 lines

Do you have any idea how this race can be handled in alloc_contig_range()?

If the page is on the wrong free list, just isolate it or move it to the MIGRATE_ISOLATE free list at that point. If it has been allocated then migrate it and move the resulting free page to the MIGRATE_ISOLATE list.

Thanks, this makes sense but still leaves us with some page allocation vs alloc_contig_range() races (i.e. pages "in-flight" state being added/removed to/from pcp lists so not being on the freelists and not being allocated).

Best regards,
--
Bartlomiej Zolnierkiewicz
Samsung Poland R&D Center

--
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>

#6 lore raw
Quoted 8 lines · Bartlomiej Zolnierkiewicz
Quoted 3 lines

If the page is on the wrong free list, just isolate it or move it to the MIGRATE_ISOLATE free list at that point. If it has been allocated then migrate it and move the resulting free page to the MIGRATE_ISOLATE list.

Thanks, this makes sense but still leaves us with some page allocation vs alloc_contig_range() races (i.e. pages "in-flight" state being added/removed to/from pcp lists so not being on the freelists and not being allocated).

The races should not be forever or open-ended. Once the pageblock is marked ISOLATE it should only take one pass to either move it from MIGRATE_CMA to MIGRATE_ISOLATE free lists or to migrate the page and free it to the MIGRATE_ISOLATE.

I would be very surprised if this cannot be properly handled in alloc_contig_range() in a manner that does not wreck the page allocator fast paths.

Signature · 8 lines
Mel Gorman
SUSE Labs

--
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>