Skip to contentSkip to conversation

mm/page_alloc: Consolidate unlikely handling in page_expected_state

PATCH Superseded Ye Liu 2 participants 3 messages Mar 21, 2025

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Ye Liu

author
#0 lore raw

PATCH 1/1 mm/page_alloc: Consolidate unlikely handling in page_expected_state

From: Ye Liu <[email protected]>

This patch consolidates the handling of unlikely conditions in the page_expected_state function, reducing code duplication and improving readability.

Previously, the check_new_page_bad function contained logic to handle __PG_HWPOISON flags, which was called from check_new_page. This patch moves the handling of __PG_HWPOISON flags into the page_expected_state function and removes the check_new_page_bad function. The check_new_page function now directly calls bad_page if the page has unexpected flags.

This change simplifies the code by reducing the number of functions and centralizing the unlikely condition handling in one place.

---
 mm/page_alloc.c | 26 ++++++++++----------------
 1 file changed, 10 insertions(+), 16 deletions(-)
mm/page_alloc.c +10 -16
index 2842da893eea..d8d04ac1d709 100644
@@ -892,6 +892,13 @@ static inline bool page_expected_state(struct page *page,
 	if (unlikely(atomic_read(&page->_mapcount) != -1))
 		return false;
 
+	if (unlikely(PageHWPoison(page))) {
+		/* Don't complain about hwpoisoned pages */
+		if (PageBuddy(page))
+			__ClearPageBuddy(page);
+		return false;
+	}
+
 	if (unlikely((unsigned long)page->mapping |
 			page_ref_count(page) |
 #ifdef CONFIG_MEMCG
@@ -1586,29 +1593,16 @@ static __always_inline void page_del_and_expand(struct zone *zone,
 	account_freepages(zone, -nr_pages, migratetype);
 }
 
-static void check_new_page_bad(struct page *page)
-{
-	if (unlikely(PageHWPoison(page))) {
-		/* Don't complain about hwpoisoned pages */
-		if (PageBuddy(page))
-			__ClearPageBuddy(page);
-		return;
-	}
-
-	bad_page(page,
-		 page_bad_reason(page, PAGE_FLAGS_CHECK_AT_PREP));
-}
-
 /*
  * This page is about to be returned from the page allocator
  */
 static bool check_new_page(struct page *page)
 {
-	if (likely(page_expected_state(page,
-				PAGE_FLAGS_CHECK_AT_PREP|__PG_HWPOISON)))
+	if (likely(page_expected_state(page, PAGE_FLAGS_CHECK_AT_PREP)))
 		return false;
 
-	check_new_page_bad(page);
+	bad_page(page,
+		 page_bad_reason(page, PAGE_FLAGS_CHECK_AT_PREP));
 	return true;
 }
 
Signature · 1 line
2.25.1
#1 lore raw
Quoted 25 lines · Ye Liu

From: Ye Liu <[email protected]>

This patch consolidates the handling of unlikely conditions in the page_expected_state function, reducing code duplication and improving readability.

Previously, the check_new_page_bad function contained logic to handle __PG_HWPOISON flags, which was called from check_new_page. This patch moves the handling of __PG_HWPOISON flags into the page_expected_state function and removes the check_new_page_bad function. The check_new_page function now directly calls bad_page if the page has unexpected flags.

This change simplifies the code by reducing the number of functions and centralizing the unlikely condition handling in one place.

---
 mm/page_alloc.c | 26 ++++++++++----------------
 1 file changed, 10 insertions(+), 16 deletions(-)
mm/page_alloc.c +0 -0
index 2842da893eea..d8d04ac1d709 100644

mm/page_alloc.c static inline bool page_expected_state(struct page *page, · line 899

Quoted 8 lines
mm/page_alloc.c +5 -0
@@ -892,6 +892,13 @@ static inline bool page_expected_state(struct page *page,
 	if (unlikely(atomic_read(&page->_mapcount) != -1))
 		return false;
 
+	if (unlikely(PageHWPoison(page))) {
+		/* Don't complain about hwpoisoned pages */
+		if (PageBuddy(page))
+			__ClearPageBuddy(page);
+		return false;

Should this be return 'true' instead ?

Let's consider a scenario where PageHWPoison(page) is true.

Previously bad_page() was not getting called as check_new_page_bad() will return earlier before reaching bad_page().

But now with the proposed change here page_expected_state() returns false and hence bad_page() still gets called later on in check_new_page().

There is a change in behaviour - or am I missing something here ?

Quoted 38 lines
mm/page_alloc.c +5 -16
+	}
+
 	if (unlikely((unsigned long)page->mapping |
 			page_ref_count(page) |
 #ifdef CONFIG_MEMCG
@@ -1586,29 +1593,16 @@ static __always_inline void page_del_and_expand(struct zone *zone,
 	account_freepages(zone, -nr_pages, migratetype);
 }
 
-static void check_new_page_bad(struct page *page)
-{
-	if (unlikely(PageHWPoison(page))) {
-		/* Don't complain about hwpoisoned pages */
-		if (PageBuddy(page))
-			__ClearPageBuddy(page);
-		return;
-	}
-
-	bad_page(page,
-		 page_bad_reason(page, PAGE_FLAGS_CHECK_AT_PREP));
-}
-
 /*
  * This page is about to be returned from the page allocator
  */
 static bool check_new_page(struct page *page)
 {
-	if (likely(page_expected_state(page,
-				PAGE_FLAGS_CHECK_AT_PREP|__PG_HWPOISON)))
+	if (likely(page_expected_state(page, PAGE_FLAGS_CHECK_AT_PREP)))
 		return false;
 
-	check_new_page_bad(page);
+	bad_page(page,
+		 page_bad_reason(page, PAGE_FLAGS_CHECK_AT_PREP));
 	return true;
 }
 
author
#2 lore raw
Quoted 39 lines · Anshuman Khandual
Quoted 25 lines · Ye Liu

From: Ye Liu <[email protected]>

This patch consolidates the handling of unlikely conditions in the page_expected_state function, reducing code duplication and improving readability.

Previously, the check_new_page_bad function contained logic to handle __PG_HWPOISON flags, which was called from check_new_page. This patch moves the handling of __PG_HWPOISON flags into the page_expected_state function and removes the check_new_page_bad function. The check_new_page function now directly calls bad_page if the page has unexpected flags.

This change simplifies the code by reducing the number of functions and centralizing the unlikely condition handling in one place.

---
 mm/page_alloc.c | 26 ++++++++++----------------
 1 file changed, 10 insertions(+), 16 deletions(-)
mm/page_alloc.c +0 -0
index 2842da893eea..d8d04ac1d709 100644

mm/page_alloc.c static inline bool page_expected_state(struct page *page, · line 899

Quoted 8 lines
mm/page_alloc.c +5 -0
@@ -892,6 +892,13 @@ static inline bool page_expected_state(struct page *page,
 	if (unlikely(atomic_read(&page->_mapcount) != -1))
 		return false;
 
+	if (unlikely(PageHWPoison(page))) {
+		/* Don't complain about hwpoisoned pages */
+		if (PageBuddy(page))
+			__ClearPageBuddy(page);
+		return false;

Should this be return 'true' instead ?

Let's consider a scenario where PageHWPoison(page) is true.

Quoted 7 lines

Previously bad_page() was not getting called as check_new_page_bad() will return earlier before reaching bad_page().

But now with the proposed change here page_expected_state() returns false and hence bad_page() still gets called later on in check_new_page().

There is a change in behaviour - or am I missing something here ?

Thank you for the suggestion. You're right, it makes sense to return true in this case. I'll update the patch accordingly. Appreciate your feedback!

Quoted 39 lines
Quoted 39 lines
mm/page_alloc.c +5 -16
+	}
+
 	if (unlikely((unsigned long)page->mapping |
 			page_ref_count(page) |
 #ifdef CONFIG_MEMCG
@@ -1586,29 +1593,16 @@ static __always_inline void page_del_and_expand(struct zone *zone,
 	account_freepages(zone, -nr_pages, migratetype);
 }
 
-static void check_new_page_bad(struct page *page)
-{
-	if (unlikely(PageHWPoison(page))) {
-		/* Don't complain about hwpoisoned pages */
-		if (PageBuddy(page))
-			__ClearPageBuddy(page);
-		return;
-	}
-
-	bad_page(page,
-		 page_bad_reason(page, PAGE_FLAGS_CHECK_AT_PREP));
-}
-
 /*
  * This page is about to be returned from the page allocator
  */
 static bool check_new_page(struct page *page)
 {
-	if (likely(page_expected_state(page,
-				PAGE_FLAGS_CHECK_AT_PREP|__PG_HWPOISON)))
+	if (likely(page_expected_state(page, PAGE_FLAGS_CHECK_AT_PREP)))
 		return false;
 
-	check_new_page_bad(page);
+	bad_page(page,
+		 page_bad_reason(page, PAGE_FLAGS_CHECK_AT_PREP));
 	return true;
 }