Skip to contentSkip to conversation

mm/page_alloc: check the correct buddy if it is a starting block

PATCH Superseded Wei Yang 4 participants 6 messages Sep 4, 2025 – Sep 5, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Wei Yang

author
#0 lore raw

PATCH 1/1 mm/page_alloc: check the correct buddy if it is a starting block

find_large_buddy() search buddy based on start_pfn, which maybe different from page's pfn, e.g. when page is not pageblock aligned, because prep_move_freepages_block() always align start_pfn to pageblock.

This means when we found a starting block at start_pfn, it may check on the wrong page theoretically.

The good news is the page passed to __move_freepages_block_isolate() has only two possible cases:

* page is pageblock aligned
* page is __first_valid_page() of this block

So it is safe for the first case, and it won't get a buddy larger than pageblock for the second case.

To eliminate the ambiguity, unify the handling for starting/tail block.

Cc Johannes Weiner <[email protected]>
Cc Zi Yan <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Vlastimil Babka <[email protected]>

---
Tried memory online/offine, which looks good.

---
 mm/page_alloc.c | 17 ++++-------------
 1 file changed, 4 insertions(+), 13 deletions(-)
mm/page_alloc.c +4 -13
index 5183a646fa09..453cd361386c 100644
@@ -2093,6 +2093,7 @@ static bool __move_freepages_block_isolate(struct zone *zone,
 	unsigned long start_pfn, pfn;
 	int from_mt;
 	int to_mt;
+	struct page *buddy;
 
 	if (isolate == get_pageblock_isolate(page)) {
 		VM_WARN_ONCE(1, "%s a pageblock that is already in that state",
@@ -2107,10 +2108,10 @@ static bool __move_freepages_block_isolate(struct zone *zone,
 	if (pageblock_order == MAX_PAGE_ORDER)
 		goto move;
 
-	/* We're a tail block in a larger buddy */
 	pfn = find_large_buddy(start_pfn);
-	if (pfn != start_pfn) {
-		struct page *buddy = pfn_to_page(pfn);
+	buddy = pfn_to_page(pfn);
+	/* We're a part of a larger buddy */
+	if (PageBuddy(buddy) && buddy_order(buddy) > pageblock_order) {
 		int order = buddy_order(buddy);
 
 		del_page_from_free_list(buddy, zone, order,
@@ -2120,16 +2121,6 @@ static bool __move_freepages_block_isolate(struct zone *zone,
 		return true;
 	}
 
-	/* We're the starting block of a larger buddy */
-	if (PageBuddy(page) && buddy_order(page) > pageblock_order) {
-		int order = buddy_order(page);
-
-		del_page_from_free_list(page, zone, order,
-					get_pfnblock_migratetype(page, pfn));
-		toggle_pageblock_isolate(page, isolate);
-		split_large_buddy(zone, page, pfn, order, FPI_NONE);
-		return true;
-	}
 move:
 	/* Use MIGRATETYPE_MASK to get non-isolate migratetype */
 	if (isolate) {
Signature · 1 line
2.34.1
reviewer
#1 lore raw
Quoted 6 lines · Wei Yang

find_large_buddy() search buddy based on start_pfn, which maybe different from page's pfn, e.g. when page is not pageblock aligned, because prep_move_freepages_block() always align start_pfn to pageblock.

This means when we found a starting block at start_pfn, it may check on the wrong page theoretically.

and not split the free page as it is supposed to, causing a freelist migratetype mismatch.

Quoted 11 lines

The good news is the page passed to __move_freepages_block_isolate() has only two possible cases:

* page is pageblock aligned
* page is __first_valid_page() of this block

So it is safe for the first case, and it won't get a buddy larger than pageblock for the second case.

To eliminate the ambiguity, unify the handling for starting/tail block.

To fix the issue, check the returned pfn of find_large_buddy() to decide whether to split the free page:
1. if it is not a PageBuddy pfn, no split;
2. if it is a PageBuddy pfn but order <= pageblock_order, no split;
3. if it is a PageBuddy pfn with order > pageblock_order, start_pfn is either in the starting block or tail block, split the PageBuddy at pageblock_order level.

Otherwise, LGTM. Reviewed-by: Zi Yan <[email protected]>

Quoted 59 lines
Cc Johannes Weiner <[email protected]>
Cc Zi Yan <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Vlastimil Babka <[email protected]>

---
Tried memory online/offine, which looks good.

---
 mm/page_alloc.c | 17 ++++-------------
 1 file changed, 4 insertions(+), 13 deletions(-)
mm/page_alloc.c +4 -13
index 5183a646fa09..453cd361386c 100644
@@ -2093,6 +2093,7 @@ static bool __move_freepages_block_isolate(struct zone *zone,
 	unsigned long start_pfn, pfn;
 	int from_mt;
 	int to_mt;
+	struct page *buddy;
 
 	if (isolate == get_pageblock_isolate(page)) {
 		VM_WARN_ONCE(1, "%s a pageblock that is already in that state",
@@ -2107,10 +2108,10 @@ static bool __move_freepages_block_isolate(struct zone *zone,
 	if (pageblock_order == MAX_PAGE_ORDER)
 		goto move;
 
-	/* We're a tail block in a larger buddy */
 	pfn = find_large_buddy(start_pfn);
-	if (pfn != start_pfn) {
-		struct page *buddy = pfn_to_page(pfn);
+	buddy = pfn_to_page(pfn);
+	/* We're a part of a larger buddy */
+	if (PageBuddy(buddy) && buddy_order(buddy) > pageblock_order) {
 		int order = buddy_order(buddy);
 
 		del_page_from_free_list(buddy, zone, order,
@@ -2120,16 +2121,6 @@ static bool __move_freepages_block_isolate(struct zone *zone,
 		return true;
 	}
 
-	/* We're the starting block of a larger buddy */
-	if (PageBuddy(page) && buddy_order(page) > pageblock_order) {
-		int order = buddy_order(page);
-
-		del_page_from_free_list(page, zone, order,
-					get_pfnblock_migratetype(page, pfn));
-		toggle_pageblock_isolate(page, isolate);
-		split_large_buddy(zone, page, pfn, order, FPI_NONE);
-		return true;
-	}
 move:
 	/* Use MIGRATETYPE_MASK to get non-isolate migratetype */
 	if (isolate) {
Signature · 1 line
2.34.1

--
Best Regards,
Yan, Zi

author
#2 lore raw
Quoted 6 lines · Zi Yan
Quoted 4 lines · Wei Yang

find_large_buddy() search buddy based on start_pfn, which maybe different from page's pfn, e.g. when page is not pageblock aligned, because prep_move_freepages_block() always align start_pfn to pageblock.

Quoted 6 lines
Quoted 2 lines

This means when we found a starting block at start_pfn, it may check on the wrong page theoretically.

and not split the free page as it is supposed to, causing a freelist migratetype mismatch.

Thanks, this is important.

Quoted 21 lines
Quoted 11 lines

The good news is the page passed to __move_freepages_block_isolate() has only two possible cases:

* page is pageblock aligned
* page is __first_valid_page() of this block

So it is safe for the first case, and it won't get a buddy larger than pageblock for the second case.

To eliminate the ambiguity, unify the handling for starting/tail block.

To fix the issue, check the returned pfn of find_large_buddy() to decide whether to split the free page:
1. if it is not a PageBuddy pfn, no split;
2. if it is a PageBuddy pfn but order <= pageblock_order, no split;
3. if it is a PageBuddy pfn with order > pageblock_order, start_pfn is either in the starting block or tail block, split the PageBuddy at pageblock_order level.

Quoted 1 line

Otherwise, LGTM. Reviewed-by: Zi Yan <[email protected]>

Thanks.

@Andrew, if not bother, would you mind adjust the changelog?

Signature · 2 lines
Wei Yang
Help you, Help me
maintainer Reviewed-by Zi Yan
#3 lore raw
Quoted 21 lines · On Fri, 5 Sep 2025 03:11:30 +0000 Wei Yang <[email protected]> wrote:
Quoted 21 lines
Quoted 11 lines

The good news is the page passed to __move_freepages_block_isolate() has only two possible cases:

* page is pageblock aligned
* page is __first_valid_page() of this block

So it is safe for the first case, and it won't get a buddy larger than pageblock for the second case.

To eliminate the ambiguity, unify the handling for starting/tail block.

To fix the issue, check the returned pfn of find_large_buddy() to decide whether to split the free page:
1. if it is not a PageBuddy pfn, no split;
2. if it is a PageBuddy pfn but order <= pageblock_order, no split;
3. if it is a PageBuddy pfn with order > pageblock_order, start_pfn is either in the starting block or tail block, split the PageBuddy at pageblock_order level.

Quoted 5 lines
Quoted 1 line

Otherwise, LGTM. Reviewed-by: Zi Yan <[email protected]>

Thanks.

@Andrew, if not bother, would you mind adjust the changelog?

This?

From: Wei Yang <[email protected]>
Subject: mm/page_alloc: check the correct buddy if it is a starting block
Date: Thu, 4 Sep 2025 02:06:54 +0000

find_large_buddy() search buddy based on start_pfn, which may be different from page's pfn, e.g. when page is not pageblock aligned, because prep_move_freepages_block() always aligns start_pfn to pageblock.

This means when we found a starting block at start_pfn, it may check on the wrong page theoretically.

The good news is the page passed to __move_freepages_block_isolate() has only two possible cases:

* page is pageblock aligned
* page is __first_valid_page() of this block

So it is safe for the first case, and it won't get a buddy larger than pageblock for the second case.

To fix the issue, check the returned pfn of find_large_buddy() to decide whether to split the free page:

1. if it is not a PageBuddy pfn, no split;
2. if it is a PageBuddy pfn but order <= pageblock_order, no split;
3. if it is a PageBuddy pfn with order > pageblock_order, start_pfn is either in the starting block or tail block, split the PageBuddy at pageblock_order level.

#4 lore raw
In reply to Wei Yang
Quoted 44 lines · Wei Yang

find_large_buddy() search buddy based on start_pfn, which maybe different from page's pfn, e.g. when page is not pageblock aligned, because prep_move_freepages_block() always align start_pfn to pageblock.

This means when we found a starting block at start_pfn, it may check on the wrong page theoretically.

The good news is the page passed to __move_freepages_block_isolate() has only two possible cases:

* page is pageblock aligned
* page is __first_valid_page() of this block

So it is safe for the first case, and it won't get a buddy larger than pageblock for the second case.

To eliminate the ambiguity, unify the handling for starting/tail block.

Cc Johannes Weiner <[email protected]>
Cc Zi Yan <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Vlastimil Babka <[email protected]>

---
Tried memory online/offine, which looks good.

---
  mm/page_alloc.c | 17 ++++-------------
  1 file changed, 4 insertions(+), 13 deletions(-)
mm/page_alloc.c +1 -0
index 5183a646fa09..453cd361386c 100644
@@ -2093,6 +2093,7 @@ static bool __move_freepages_block_isolate(struct zone *zone,
  	unsigned long start_pfn, pfn;
  	int from_mt;
  	int to_mt;
+	struct page *buddy;
 
  	if (isolate == get_pageblock_isolate(page)) {
  		VM_WARN_ONCE(1, "%s a pageblock that is already in that state",

mm/page_alloc.c static bool __move_freepages_block_isolate(struct zone *zone, · line 2112

Quoted 8 lines
mm/page_alloc.c +1 -3
@@ -2107,10 +2108,10 @@ static bool __move_freepages_block_isolate(struct zone *zone,
  	if (pageblock_order == MAX_PAGE_ORDER)
  		goto move;
 
-	/* We're a tail block in a larger buddy */
  	pfn = find_large_buddy(start_pfn);
-	if (pfn != start_pfn) {
-		struct page *buddy = pfn_to_page(pfn);
+	buddy = pfn_to_page(pfn);

While at it, can you please rename "pfn" to "buddy_pfn" ?

mm/page_alloc.c static bool __move_freepages_block_isolate(struct zone *zone, · line 2115

Quoted 3 lines
mm/page_alloc.c +2 -0
+	/* We're a part of a larger buddy */
+	if (PageBuddy(buddy) && buddy_order(buddy) > pageblock_order) {
  		int order = buddy_order(buddy);

Yes, I think this should work.

Signature · 3 lines
Cheers

David / dhildenb
author
#5 lore raw
Quoted 45 lines · David Hildenbrand
Quoted 44 lines · Wei Yang

find_large_buddy() search buddy based on start_pfn, which maybe different from page's pfn, e.g. when page is not pageblock aligned, because prep_move_freepages_block() always align start_pfn to pageblock.

This means when we found a starting block at start_pfn, it may check on the wrong page theoretically.

The good news is the page passed to __move_freepages_block_isolate() has only two possible cases:

* page is pageblock aligned
* page is __first_valid_page() of this block

So it is safe for the first case, and it won't get a buddy larger than pageblock for the second case.

To eliminate the ambiguity, unify the handling for starting/tail block.

Cc Johannes Weiner <[email protected]>
Cc Zi Yan <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Vlastimil Babka <[email protected]>

---
Tried memory online/offine, which looks good.

---
  mm/page_alloc.c | 17 ++++-------------
  1 file changed, 4 insertions(+), 13 deletions(-)
mm/page_alloc.c +1 -0
index 5183a646fa09..453cd361386c 100644
@@ -2093,6 +2093,7 @@ static bool __move_freepages_block_isolate(struct zone *zone,
  	unsigned long start_pfn, pfn;
  	int from_mt;
  	int to_mt;
+	struct page *buddy;
  	if (isolate == get_pageblock_isolate(page)) {
  		VM_WARN_ONCE(1, "%s a pageblock that is already in that state",
@@ -2107,10 +2108,10 @@ static bool __move_freepages_block_isolate(struct zone *zone,
  	if (pageblock_order == MAX_PAGE_ORDER)
Quoted 9 lines

mm/page_alloc.c static bool __move_freepages_block_isolate(struct zone *zone, · line 2112

Quoted 6 lines
mm/page_alloc.c +1 -3
  		goto move;
-	/* We're a tail block in a larger buddy */
  	pfn = find_large_buddy(start_pfn);
-	if (pfn != start_pfn) {
-		struct page *buddy = pfn_to_page(pfn);
+	buddy = pfn_to_page(pfn);

While at it, can you please rename "pfn" to "buddy_pfn" ?

Ok.

Quoted 6 lines

mm/page_alloc.c static bool __move_freepages_block_isolate(struct zone *zone, · line 2115

Quoted 3 lines
mm/page_alloc.c +2 -0
+	/* We're a part of a larger buddy */
+	if (PageBuddy(buddy) && buddy_order(buddy) > pageblock_order) {
  		int order = buddy_order(buddy);

Yes, I think this should work.

@Andrew, looks I will send a v2 with updated changelog. Thanks

Signature · 2 lines
Wei Yang
Help you, Help me