Skip to contentSkip to conversation

mm/page_alloc: find_large_buddy() from start_pfn aligned order

PATCH In review Wei Yang 5 participants 6 messages Sep 2, 2025

Subsystems (2)
lore
Share & tools
lore

Wei Yang

author
#0 lore raw

PATCH v2 1/1 mm/page_alloc: find_large_buddy() from start_pfn aligned order

We iterate pfn from order 0 to MAX_PAGE_ORDER aligned to find large buddy. While if the order is less than start_pfn aligned order, we would get the same pfn and do the same check again.

Iterate from start_pfn aligned order to reduce duplicated work.

---
v2: add comment on assignment of order

---
 mm/page_alloc.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
mm/page_alloc.c +7 -1
index 07d79ae557f8..5d9ceca869e5 100644
@@ -2033,7 +2033,13 @@ static int move_freepages_block(struct zone *zone, struct page *page,
 /* Look for a buddy that straddles start_pfn */
 static unsigned long find_large_buddy(unsigned long start_pfn)
 {
-	int order = 0;
+	/*
+	 * If start_pfn is not an order-0 PageBuddy, next PageBuddy containing
+	 * start_pfn has minimal order of __ffs(start_pfn) + 1. Start checking
+	 * the order with __ffs(start_pfn). If start_pfn is order-0 PageBuddy,
+	 * the starting order does not matter.
+	 */
+	int order = start_pfn ? __ffs(start_pfn) : MAX_PAGE_ORDER;
 	struct page *page;
 	unsigned long pfn = start_pfn;
 
Signature · 1 line
2.34.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 6 lines · Wei Yang

We iterate pfn from order 0 to MAX_PAGE_ORDER aligned to find large buddy. While if the order is less than start_pfn aligned order, we would get the same pfn and do the same check again.

Iterate from start_pfn aligned order to reduce duplicated work.

Quoted 8 lines
Quoted 26 lines

---
v2: add comment on assignment of order

---
 mm/page_alloc.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
mm/page_alloc.c +7 -1
index 07d79ae557f8..5d9ceca869e5 100644
@@ -2033,7 +2033,13 @@ static int move_freepages_block(struct zone *zone, struct page *page,
 /* Look for a buddy that straddles start_pfn */
 static unsigned long find_large_buddy(unsigned long start_pfn)
 {
-	int order = 0;
+	/*
+	 * If start_pfn is not an order-0 PageBuddy, next PageBuddy containing
+	 * start_pfn has minimal order of __ffs(start_pfn) + 1. Start checking
+	 * the order with __ffs(start_pfn). If start_pfn is order-0 PageBuddy,
+	 * the starting order does not matter.
+	 */
+	int order = start_pfn ? __ffs(start_pfn) : MAX_PAGE_ORDER;
 	struct page *page;
 	unsigned long pfn = start_pfn;
 
#2 lore raw
In reply to Wei Yang
Quoted 27 lines · Wei Yang

We iterate pfn from order 0 to MAX_PAGE_ORDER aligned to find large buddy. While if the order is less than start_pfn aligned order, we would get the same pfn and do the same check again.

Iterate from start_pfn aligned order to reduce duplicated work.

---
v2: add comment on assignment of order

---
 mm/page_alloc.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
mm/page_alloc.c +0 -0
index 07d79ae557f8..5d9ceca869e5 100644
@@ -2033,7 +2033,13 @@ static int move_freepages_block(struct zone *zone, struct page *page,
 /* Look for a buddy that straddles start_pfn */

mm/page_alloc.c static int move_freepages_block(struct zone *zone, struct page *page, · line 2042

Quoted 10 lines
mm/page_alloc.c +7 -1
 static unsigned long find_large_buddy(unsigned long start_pfn)
 {
-	int order = 0;
+	/*
+	 * If start_pfn is not an order-0 PageBuddy, next PageBuddy containing
+	 * start_pfn has minimal order of __ffs(start_pfn) + 1. Start checking
+	 * the order with __ffs(start_pfn). If start_pfn is order-0 PageBuddy,
+	 * the starting order does not matter.
+	 */
+	int order = start_pfn ? __ffs(start_pfn) : MAX_PAGE_ORDER;

This should be __ffs(start_pfn) - 1, no?

If you have the lowest bit set in the pfn, you should check the order-1 buddy to the left first. But ffs(1) is already 1, which means the loop will check order-2 next.

reviewer
#3 lore raw
Quoted 41 lines · Johannes Weiner
Quoted 27 lines · Wei Yang

We iterate pfn from order 0 to MAX_PAGE_ORDER aligned to find large buddy. While if the order is less than start_pfn aligned order, we would get the same pfn and do the same check again.

Iterate from start_pfn aligned order to reduce duplicated work.

---
v2: add comment on assignment of order

---
 mm/page_alloc.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
mm/page_alloc.c +0 -0
index 07d79ae557f8..5d9ceca869e5 100644
@@ -2033,7 +2033,13 @@ static int move_freepages_block(struct zone *zone, struct page *page,
 /* Look for a buddy that straddles start_pfn */

mm/page_alloc.c static int move_freepages_block(struct zone *zone, struct page *page, · line 2042

Quoted 10 lines
mm/page_alloc.c +7 -1
 static unsigned long find_large_buddy(unsigned long start_pfn)
 {
-	int order = 0;
+	/*
+	 * If start_pfn is not an order-0 PageBuddy, next PageBuddy containing
+	 * start_pfn has minimal order of __ffs(start_pfn) + 1. Start checking
+	 * the order with __ffs(start_pfn). If start_pfn is order-0 PageBuddy,
+	 * the starting order does not matter.
+	 */
+	int order = start_pfn ? __ffs(start_pfn) : MAX_PAGE_ORDER;

This should be __ffs(start_pfn) - 1, no?

Quoted 3 lines

If you have the lowest bit set in the pfn, you should check the order-1 buddy to the left first. But ffs(1) is already 1, which means the loop will check order-2 next.

__ffs() seems different from usespace ffs() and is 0-index, so __ffs() is what you mean ffs() - 1.

Best Regards,
Yan, Zi

reviewer Acked-by
#4 lore raw
Quoted 43 lines · Zi Yan
Quoted 41 lines · Johannes Weiner
Quoted 27 lines · Wei Yang

We iterate pfn from order 0 to MAX_PAGE_ORDER aligned to find large buddy. While if the order is less than start_pfn aligned order, we would get the same pfn and do the same check again.

Iterate from start_pfn aligned order to reduce duplicated work.

---
v2: add comment on assignment of order

---
 mm/page_alloc.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
mm/page_alloc.c +0 -0
index 07d79ae557f8..5d9ceca869e5 100644
@@ -2033,7 +2033,13 @@ static int move_freepages_block(struct zone *zone, struct page *page,
 /* Look for a buddy that straddles start_pfn */

mm/page_alloc.c static int move_freepages_block(struct zone *zone, struct page *page, · line 2042

Quoted 10 lines
mm/page_alloc.c +7 -1
 static unsigned long find_large_buddy(unsigned long start_pfn)
 {
-	int order = 0;
+	/*
+	 * If start_pfn is not an order-0 PageBuddy, next PageBuddy containing
+	 * start_pfn has minimal order of __ffs(start_pfn) + 1. Start checking
+	 * the order with __ffs(start_pfn). If start_pfn is order-0 PageBuddy,
+	 * the starting order does not matter.
+	 */
+	int order = start_pfn ? __ffs(start_pfn) : MAX_PAGE_ORDER;

This should be __ffs(start_pfn) - 1, no?

Quoted 6 lines
Quoted 3 lines

If you have the lowest bit set in the pfn, you should check the order-1 buddy to the left first. But ffs(1) is already 1, which means the loop will check order-2 next.

__ffs() seems different from usespace ffs() and is 0-index, so __ffs() is what you mean ffs() - 1.

I'm going back to reading school.

Reviewed-by on v2 1/1
#5 lore raw
In reply to Wei Yang
Quoted 6 lines · Wei Yang

We iterate pfn from order 0 to MAX_PAGE_ORDER aligned to find large buddy. While if the order is less than start_pfn aligned order, we would get the same pfn and do the same check again.

Iterate from start_pfn aligned order to reduce duplicated work.

Quoted 8 lines