diff mbox series

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

Message ID 1583831729-14383-1-git-send-email-qiwuchen55@gmail.com (mailing list archive)
State New, archived
Headers show
Series [v3] mm/page_alloc: simplify page_is_buddy() for better code readability | expand

Commit Message

chenqiwu March 10, 2020, 9:15 a.m. UTC
From: chenqiwu <chenqiwu@xiaomi.com>

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

Signed-off-by: chenqiwu <chenqiwu@xiaomi.com>
---
changes in v3:
 - exchange the first two condition statements in v2 because the page type
   must be checked first.
---
 mm/page_alloc.c | 32 +++++++++++++-------------------
 1 file changed, 13 insertions(+), 19 deletions(-)

Comments

Vlastimil Babka March 10, 2020, 9:36 a.m. UTC | #1
On 3/10/20 10:15 AM, qiwuchen55@gmail.com wrote:
> From: chenqiwu <chenqiwu@xiaomi.com>
> 
> Simplify page_is_buddy() to reduce the redundant code for better code
> readability.
> 
> Signed-off-by: chenqiwu <chenqiwu@xiaomi.com>

Reviewed-by: Vlastimil Babka <vbabka@suse.cz>

Thanks.
Matthew Wilcox March 10, 2020, 12:48 p.m. UTC | #2
On Tue, Mar 10, 2020 at 05:15:29PM +0800, qiwuchen55@gmail.com wrote:
> Simplify page_is_buddy() to reduce the redundant code for better code
> readability.

If you're making these larger changes, then ...

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

Ideally this should return 'bool'.

> +	/*
> +	 * zone check is done late to avoid uselessly
> +	 * calculating zone/node ids for pages that could
> +	 * never merge.
> +	 */

This comment could be reflowed onto fewer lines.
diff mbox series

Patch

diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 3c4eb75..412f5d5 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -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_is_guard(buddy) && !PageBuddy(buddy))
+		return 0;
 
-		return 1;
-	}
+	if (page_order(buddy) != order)
+		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