Skip to contentSkip to conversation

mm/page_alloc: simplify page_is_buddy() for better code readability

PATCH Superseded [email protected] 2 participants 3 messages Mar 10, 2020

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 mm/page_alloc: simplify page_is_buddy() for better code readability

From: chenqiwu <[email protected]>

Simplify page_is_buddy() to reduce the redundant code for better code readability.

---
changes in v2:
- split the conditional statement into four separate tests for better code readability.

---
 mm/page_alloc.c | 32 +++++++++++++-------------------
 1 file changed, 13 insertions(+), 19 deletions(-)
mm/page_alloc.c +13 -19
index 3c4eb75..b221f6d 100644
@@ -794,29 +794,23 @@ static inline void set_page_order(struct page *page, unsigned int order)
 static inline int page_is_buddy(struct page *page, struct page *buddy,
 							unsigned int order)
 {
-	if (page_is_guard(buddy) && page_order(buddy) == order) {
-		if (page_zone_id(page) != page_zone_id(buddy))
-			return 0;
-
-		VM_BUG_ON_PAGE(page_count(buddy) != 0, buddy);
+	if (page_order(buddy) != order)
+		return 0;
 
-		return 1;
-	}
+	if (!page_is_guard(buddy) && !PageBuddy(buddy))
+		return 0;
 
-	if (PageBuddy(buddy) && page_order(buddy) == order) {
-		/*
-		 * zone check is done late to avoid uselessly
-		 * calculating zone/node ids for pages that could
-		 * never merge.
-		 */
-		if (page_zone_id(page) != page_zone_id(buddy))
-			return 0;
+	/*
+	 * zone check is done late to avoid uselessly
+	 * calculating zone/node ids for pages that could
+	 * never merge.
+	 */
+	if (page_zone_id(page) != page_zone_id(buddy))
+		return 0;
 
-		VM_BUG_ON_PAGE(page_count(buddy) != 0, buddy);
+	VM_BUG_ON_PAGE(page_count(buddy) != 0, buddy);
 
-		return 1;
-	}
-	return 0;
+	return 1;
 }
 
 #ifdef CONFIG_COMPACTION
Signature · 1 line
1.9.1
#1 lore raw
Quoted 6 lines · On 3/10/20 3:34 AM, [email protected] wrote:

From: chenqiwu <[email protected]>

Simplify page_is_buddy() to reduce the redundant code for better code readability.

I would personally keep testing the page type first, and even switch it so that PageBuddy is tested before page_is_guard (but it only matters on debugging kernels).

I can only speculate which will have better performance (if any), but it's more logical to test the type first. Both orders should be safe though as we have the zone lock.

author
#2 lore raw
Quoted 6 lines · Vlastimil Babka
Quoted 5 lines · On 3/10/20 3:34 AM, [email protected] wrote:

From: chenqiwu <[email protected]>

Simplify page_is_buddy() to reduce the redundant code for better code readability.

Quoted 8 lines
Quoted 1 line

I would personally keep testing the page type first, and even switch it so that PageBuddy is tested before page_is_guard (but it only matters on debugging kernels).

I can only speculate which will have better performance (if any), but it's more logical to test the type first. Both orders should be safe though as we have the zone lock.

Yes, I have ignored the iegality of the page must be checked first. Thanks for your review!