Skip to contentSkip to conversation

memory-hotplug: handle page race between allocation and isolation

RFC In review Minchan Kim 2 participants 6 messages Jul 23, 2012 – Jul 27, 2012

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

cover memory-hotplug: handle page race between allocation and isolation

Memory hotplug has a subtle race problem so this patchset fixes the problem (Look at [3/3] for detail and please confirm the problem before review other patches in this series.)

[1/3] is just clean up and help for [2/3].
[2/3] keeps the migratetype information to freed page's index field
and [3/3] uses the information.
[3/3] fixes the race problem with [2/3]'s information.

After applying [2/3], migratetype argument in __free_one_page and free_one_page is redundant so we can remove it but I decide to not touch them because it increases code size about 50 byte.

Minchan Kim (3):
mm: use get_page_migratetype instead of page_private
mm: remain migratetype in freed page
memory-hotplug: bug fix race between isolation and allocation

 include/linux/mm.h  |   12 ++++++++++++
 mm/page_alloc.c     |   16 ++++++++++------
 mm/page_isolation.c |    7 +++++--
 3 files changed, 27 insertions(+), 8 deletions(-)
Signature · 7 lines
1.7.9.5

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

RFC 1/3 mm: use get_page_migratetype instead of page_private

page allocator uses set_page_private and page_private for handling migratetype when it frees page. Let's replace them with [set|get] _page_migratetype to make it more clear.

---
 include/linux/mm.h  |   10 ++++++++++
 mm/page_alloc.c     |   11 +++++++----
 mm/page_isolation.c |    2 +-
 3 files changed, 18 insertions(+), 5 deletions(-)
include/linux/mm.h +10 -0
index 5c76634..86d61d6 100644
@@ -249,6 +249,16 @@ struct inode;
 #define page_private(page)		((page)->private)
 #define set_page_private(page, v)	((page)->private = (v))
 
+static inline void set_page_migratetype(struct page *page, int migratetype)
+{
+	set_page_private(page, migratetype);
+}
+
+static inline int get_page_migratetype(struct page *page)
+{
+	return page_private(page);
+}
+
 /*
  * FIXME: take this include out, include page-flags.h in
  * files which need it (119 of them)
mm/page_alloc.c +7 -4
index 710d91c..103ba66 100644
@@ -671,8 +671,10 @@ static void free_pcppages_bulk(struct zone *zone, int count,
 			/* must delete as __free_one_page list manipulates */
 			list_del(&page->lru);
 			/* 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,
+				get_page_migratetype(page));
+			trace_mm_page_pcpu_drain(page, 0,
+				get_page_migratetype(page));
 		} while (--to_free && --batch_free && !list_empty(list));
 	}
 	__mod_zone_page_state(zone, NR_FREE_PAGES, count);
@@ -731,6 +733,7 @@ static void __free_pages_ok(struct page *page, unsigned int order)
 	__count_vm_events(PGFREE, 1 << order);
 	free_one_page(page_zone(page), page, order,
 					get_pageblock_migratetype(page));
+
 	local_irq_restore(flags);
 }
 
@@ -1134,7 +1137,7 @@ static int rmqueue_bulk(struct zone *zone, unsigned int order,
 			if (!is_migrate_cma(mt) && mt != MIGRATE_ISOLATE)
 				mt = migratetype;
 		}
-		set_page_private(page, mt);
+		set_page_migratetype(page, mt);
 		list = &page->lru;
 	}
 	__mod_zone_page_state(zone, NR_FREE_PAGES, -(i << order));
@@ -1301,7 +1304,7 @@ void free_hot_cold_page(struct page *page, int cold)
 		return;
 
 	migratetype = get_pageblock_migratetype(page);
-	set_page_private(page, migratetype);
+	set_page_migratetype(page, migratetype);
 	local_irq_save(flags);
 	if (unlikely(wasMlocked))
 		free_page_mlock(page);
mm/page_isolation.c +1 -1
index 64abb33..acf65a7 100644
@@ -199,7 +199,7 @@ __test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn)
 		if (PageBuddy(page))
 			pfn += 1 << page_order(page);
 		else if (page_count(page) == 0 &&
-				page_private(page) == MIGRATE_ISOLATE)
+				get_page_migratetype(page) == MIGRATE_ISOLATE)
 			pfn += 1;
 		else
 			break;
Signature · 7 lines
1.7.9.5

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

RFC 2/3 mm: remain migratetype in freed page

In reply to Minchan Kim

Page allocator doesn't keep migratetype information to page when the page is freed. This patch remains the information to freed page's index field which isn't used by free/alloc preparing so it shouldn't change any behavir except below one.

This patch adds a new call site in __free_pages_ok so it might be overhead a bit but it's for high order allocation.
So I believe damage isn't hurt.

---
 include/linux/mm.h |    6 ++++--
 mm/page_alloc.c    |    7 ++++---
 2 files changed, 8 insertions(+), 5 deletions(-)
include/linux/mm.h +4 -2
index 86d61d6..8fd32da 100644
@@ -251,12 +251,14 @@ struct inode;
 
 static inline void set_page_migratetype(struct page *page, int migratetype)
 {
-	set_page_private(page, migratetype);
+	VM_BUG_ON((unsigned int)migratetype >= MIGRATE_TYPES);
+	page->index = migratetype;
 }
 
 static inline int get_page_migratetype(struct page *page)
 {
-	return page_private(page);
+	VM_BUG_ON((unsigned int)page->index >= MIGRATE_TYPES);
+	return page->index;
 }
 
 /*
mm/page_alloc.c +4 -3
index 103ba66..32985dd 100644
@@ -723,6 +723,7 @@ static void __free_pages_ok(struct page *page, unsigned int order)
 {
 	unsigned long flags;
 	int wasMlocked = __TestClearPageMlocked(page);
+	int migratetype;
 
 	if (!free_pages_prepare(page, order))
 		return;
@@ -731,9 +732,9 @@ static void __free_pages_ok(struct page *page, unsigned int order)
 	if (unlikely(wasMlocked))
 		free_page_mlock(page);
 	__count_vm_events(PGFREE, 1 << order);
-	free_one_page(page_zone(page), page, order,
-					get_pageblock_migratetype(page));
-
+	migratetype = get_pageblock_migratetype(page);
+	set_page_migratetype(page, migratetype);
+	free_one_page(page_zone(page), page, order, migratetype);
 	local_irq_restore(flags);
 }
 
Signature · 7 lines
1.7.9.5

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

RFC 3/3 memory-hotplug: bug fix race between isolation and allocation

In reply to Minchan Kim

Like below, memory-hotplug makes race between page-isolation and page-allocation so it can hit BUG_ON in __offline_isolated_pages.

	CPU A					CPU B

start_isolate_page_range
set_migratetype_isolate
spin_lock_irqsave(zone->lock)

				free_hot_cold_page(Page A)
				/* without zone->lock */
				migratetype = get_pageblock_migratetype(Page A);
				/*
				 * Page could be moved into MIGRATE_MOVABLE
				 * of per_cpu_pages
				 */
				list_add_tail(&page->lru, &pcp->lists[migratetype]);

set_pageblock_isolate
move_freepages_block
drain_all_pages

				/* Page A could be in MIGRATE_MOVABLE of free_list. */

check_pages_isolated
__test_page_isolated_in_pageblock
/*
* We can't catch freed page which
* is free_list[MIGRATE_MOVABLE]
*/
if (PageBuddy(page A))

	pfn += 1 << page_order(page A);

				/* So, Page A could be allocated */

__offline_isolated_pages
/*
* BUG_ON hit or offline page
* which is used by someone
*/
BUG_ON(!PageBuddy(page A));

---
I found this problem during code review so please confirm it. Kame?

 mm/page_isolation.c |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
mm/page_isolation.c +4 -1
index acf65a7..4699d1f 100644
@@ -196,8 +196,11 @@ __test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn)
 			continue;
 		}
 		page = pfn_to_page(pfn);
-		if (PageBuddy(page))
+		if (PageBuddy(page)) {
+			if (get_page_migratetype(page) != MIGRATE_ISOLATE)
+				break;
 			pfn += 1 << page_order(page);
+		}
 		else if (page_count(page) == 0 &&
 				get_page_migratetype(page) == MIGRATE_ISOLATE)
 			pfn += 1;
Signature · 1 line
1.7.9.5
#4 lore raw

Re: [RESEND RFC 3/3] memory-hotplug: bug fix race between isolation and allocation

Quoted 35 lines · Minchan Kim

Like below, memory-hotplug makes race between page-isolation and page-allocation so it can hit BUG_ON in __offline_isolated_pages.

	CPU A					CPU B

start_isolate_page_range
set_migratetype_isolate
spin_lock_irqsave(zone->lock)

				free_hot_cold_page(Page A)
				/* without zone->lock */
				migratetype = get_pageblock_migratetype(Page A);
				/*
				 * Page could be moved into MIGRATE_MOVABLE
				 * of per_cpu_pages
				 */
				list_add_tail(&page->lru, &pcp->lists[migratetype]);

set_pageblock_isolate
move_freepages_block
drain_all_pages

				/* Page A could be in MIGRATE_MOVABLE of free_list. */

check_pages_isolated
__test_page_isolated_in_pageblock
/*
* We can't catch freed page which
* is free_list[MIGRATE_MOVABLE]
*/
if (PageBuddy(page A))

	pfn += 1 << page_order(page A);

				/* So, Page A could be allocated */
Quoted 8 lines

__offline_isolated_pages
/*
* BUG_ON hit or offline page
* which is used by someone
*/
BUG_ON(!PageBuddy(page A));

Ah, hm. Then, you say the page in MIGRATE_MOVABLE will not be isolated and may be used again.

Quoted 12 lines

---
I found this problem during code review so please confirm it. Kame?

  mm/page_isolation.c |    5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
mm/page_isolation.c +0 -0
index acf65a7..4699d1f 100644

mm/page_isolation.c __test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn) · line 201

Quoted 7 lines
mm/page_isolation.c +3 -1
@@ -196,8 +196,11 @@ __test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn)
  			continue;
  		}
  		page = pfn_to_page(pfn);
-		if (PageBuddy(page))
+		if (PageBuddy(page)) {
+			if (get_page_migratetype(page) != MIGRATE_ISOLATE)
+				break;

Doesn't this work enough ? The problem is MIGRATE_TYPE and list_head mis-match.

Thanks,
-Kame

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

Re: [RESEND RFC 3/3] memory-hotplug: bug fix race between isolation and allocation

Quoted 43 lines · Kamezawa Hiroyuki
Quoted 42 lines · Minchan Kim

Like below, memory-hotplug makes race between page-isolation and page-allocation so it can hit BUG_ON in __offline_isolated_pages.

	CPU A					CPU B

start_isolate_page_range
set_migratetype_isolate
spin_lock_irqsave(zone->lock)

				free_hot_cold_page(Page A)
				/* without zone->lock */
				migratetype = get_pageblock_migratetype(Page A);
				/*
				 * Page could be moved into MIGRATE_MOVABLE
				 * of per_cpu_pages
				 */
				list_add_tail(&page->lru, &pcp->lists[migratetype]);

set_pageblock_isolate
move_freepages_block
drain_all_pages

				/* Page A could be in MIGRATE_MOVABLE of free_list. */

check_pages_isolated
__test_page_isolated_in_pageblock
/*
* We can't catch freed page which
* is free_list[MIGRATE_MOVABLE]
*/
if (PageBuddy(page A))

	pfn += 1 << page_order(page A);

				/* So, Page A could be allocated */

__offline_isolated_pages
/*
* BUG_ON hit or offline page
* which is used by someone
*/
BUG_ON(!PageBuddy(page A));

Quoted 4 lines
Quoted 1 line

Ah, hm. Then, you say the page in MIGRATE_MOVABLE will not be isolated and may be used again.

Yes.

Quoted 9 lines
Quoted 9 lines

---
I found this problem during code review so please confirm it. Kame?

  mm/page_isolation.c |    5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
Quoted 14 lines

mm/page_isolation.c __test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn) · line 201

Quoted 12 lines
mm/page_isolation.c +3 -1
index acf65a7..4699d1f 100644
@@ -196,8 +196,11 @@ __test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn)
  			continue;
  		}
  		page = pfn_to_page(pfn);
-		if (PageBuddy(page))
+		if (PageBuddy(page)) {
+			if (get_page_migratetype(page) != MIGRATE_ISOLATE)
+				break;

Doesn't this work enough ? The problem is MIGRATE_TYPE and list_head mis-match.

I guess you are confused between get_page_migratetype and get_pageblock_migratetype. It's not get_pageblock_migratetype but get_page_migratetype which is introduced for detecting MIGRATE_TYPE and list_head mismatch in [1,2/3].

Quoted 6 lines

Thanks,
-Kame

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