Skip to contentSkip to conversation

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

PATCH Superseded [email protected] 5 participants 6 messages Mar 8, 2020 – Mar 10, 2020

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

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

---
 mm/page_alloc.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)
mm/page_alloc.c +3 -10
index 79e950d..c6eef38 100644
@@ -797,16 +797,8 @@ 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);
-
-		return 1;
-	}
-
-	if (PageBuddy(buddy) && page_order(buddy) == order) {
+	if ((page_is_guard(buddy) || PageBuddy(buddy))
+	     && page_order(buddy) == order) {
 		/*
 		 * zone check is done late to avoid uselessly
 		 * calculating zone/node ids for pages that could
@@ -819,6 +811,7 @@ static inline int page_is_buddy(struct page *page, struct page *buddy,
 
 		return 1;
 	}
+
 	return 0;
 }
 
Signature · 1 line
1.9.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 16 lines · On 03/08/20 at 04:10pm, [email protected] wrote:

From: chenqiwu <[email protected]>

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

---
 mm/page_alloc.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)
mm/page_alloc.c +0 -0
index 79e950d..c6eef38 100644
@@ -797,16 +797,8 @@ static inline void set_page_order(struct page *page, unsigned int order)
 static inline int page_is_buddy(struct page *page, struct page *buddy,

mm/page_alloc.c static inline void set_page_order(struct page *page, unsigned int order) · line 807

Quoted 10 lines
mm/page_alloc.c +0 -8
 							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);
-
-		return 1;
-	}

This duplicated removing Looks good, not sure if there's any performance impact or other considering.

FWIW,

Quoted 21 lines
mm/page_alloc.c +2 -2
-
-	if (PageBuddy(buddy) && page_order(buddy) == order) {
+	if ((page_is_guard(buddy) || PageBuddy(buddy))
+	     && page_order(buddy) == order) {
 
 
 		/*
mm/page_alloc.c +1 -0
 		 * zone check is done late to avoid uselessly
 		 * calculating zone/node ids for pages that could
@@ -819,6 +811,7 @@ static inline int page_is_buddy(struct page *page, struct page *buddy,
 
 		return 1;
 	}
+
 	return 0;
 }
 
Signature · 1 line
1.9.1
#3 lore raw
In reply to [email protected]
Quoted 24 lines · On Sun, Mar 8, 2020 at 12:10 AM <[email protected]> wrote:

From: chenqiwu <[email protected]>

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

---
 mm/page_alloc.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)
mm/page_alloc.c +0 -5
index 79e950d..c6eef38 100644
@@ -797,16 +797,8 @@ 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);

mm/page_alloc.c static inline void set_page_order(struct page *page, unsigned int order) · line 804

Quoted 10 lines
mm/page_alloc.c +2 -5
-
-               return 1;
-       }
-
-       if (PageBuddy(buddy) && page_order(buddy) == order) {
+       if ((page_is_guard(buddy) || PageBuddy(buddy))
+            && page_order(buddy) == order) {
                /*
                 * zone check is done late to avoid uselessly
                 * calculating zone/node ids for pages that could

Instead of keeping the if statement as is couldn't you flatten this out further by just returning 0 if !page_is_guard && !PageBuddy?

So something like:
if (!page_is_guard(buddy) && !PageBuddy(buddy))

        return0;

if (page_order(buddy) != order)

        return 0;

I feel like this would be more readable than sorting out the parenthesis for the conditional statement. Then you can also just get rid of the indenting and braces for the rest of the statement. With that it would more closely match the description above as well as you are going through and checking a - d as separate tests.

author
#4 lore raw
Quoted 43 lines · Alexander Duyck
Quoted 24 lines · On Sun, Mar 8, 2020 at 12:10 AM <[email protected]> wrote:

From: chenqiwu <[email protected]>

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

---
 mm/page_alloc.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)
mm/page_alloc.c +0 -5
index 79e950d..c6eef38 100644
@@ -797,16 +797,8 @@ 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);

mm/page_alloc.c static inline void set_page_order(struct page *page, unsigned int order) · line 804

Quoted 10 lines
mm/page_alloc.c +2 -5
-
-               return 1;
-       }
-
-       if (PageBuddy(buddy) && page_order(buddy) == order) {
+       if ((page_is_guard(buddy) || PageBuddy(buddy))
+            && page_order(buddy) == order) {
                /*
                 * zone check is done late to avoid uselessly
                 * calculating zone/node ids for pages that could

Instead of keeping the if statement as is couldn't you flatten this out further by just returning 0 if !page_is_guard && !PageBuddy?

So something like:
if (!page_is_guard(buddy) && !PageBuddy(buddy))

        return0;
Quoted 8 lines

if (page_order(buddy) != order)

        return 0;

I feel like this would be more readable than sorting out the parenthesis for the conditional statement. Then you can also just get rid of the indenting and braces for the rest of the statement. With that it would more closely match the description above as well as you are going through and checking a - d as separate tests.

I agree, for performance considering, I think the second conditional statement should be moved up. I will resend this as proper patch v2 for review.

Acked-by on v1 1/1
#5 lore raw
In reply to [email protected]
Quoted 35 lines

From: chenqiwu <[email protected]>

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

---
 mm/page_alloc.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)
mm/page_alloc.c +2 -10
index 79e950d..c6eef38 100644
@@ -797,16 +797,8 @@ 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);
-
-               return 1;
-       }
-
-       if (PageBuddy(buddy) && page_order(buddy) == order) {
+       if ((page_is_guard(buddy) || PageBuddy(buddy))
+            && page_order(buddy) == order) {
                /*
                 * zone check is done late to avoid uselessly
                 * calculating zone/node ids for pages that could

mm/page_alloc.c static inline int page_is_buddy(struct page *page, struct page *buddy, · line 816

Quoted 6 lines
mm/page_alloc.c +1 -0
@@ -819,6 +811,7 @@ static inline int page_is_buddy(struct page *page, struct page *buddy,
 
                return 1;
        }
+
        return 0;
 }

Looks good to me.

Quoted 5 lines
mm/page_alloc.c +0 -1
--

1.9.1