Skip to contentSkip to conversation

mm/pageblock: improve readability of some pageblock handling

PATCH Superseded Wei Yang 3 participants 9 messages Aug 26, 2025

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Wei Yang

author
#0 lore raw

cover mm/pageblock: improve readability of some pageblock handling

During code reading, found two possible points to improve the readability of pageblock handling.

Patch 1: isolate bit is standalone and there are dedicated helpers. Instead of check the bit directly, we could use the helper to do it.

Patch 2: First we could use PB_migratetype_bits to define MIGRATETYPE_MASK. Second use MIGRATETYPE_MASK to define MIGRATETYPE_AND_ISO_MASK instead of duplicate the definition.

Wei Yang (2):
mm/page_alloc: use xxx_pageblock_isolate() for better reading mm/pageblock-flags: simplify MIGRATETYPE_MASK definition

 include/linux/pageblock-flags.h | 5 ++---
 mm/page_alloc.c                 | 7 +++----
 2 files changed, 5 insertions(+), 7 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/2 mm/page_alloc: use xxx_pageblock_isolate() for better reading

Since commit e904bce2d9d4 ("mm/page_isolation: make page isolation a standalone bit"), it provides dedicated helper to handle isolation.

Change to use these helpers to be better reading.

No functional change intended.

Cc Zi Yan <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc David Hildenbrand <[email protected]>
---
 mm/page_alloc.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
mm/page_alloc.c +3 -4
index baead29b3e67..e534f31a6b39 100644
@@ -538,8 +538,7 @@ static void set_pageblock_migratetype(struct page *page,
 			"Use set_pageblock_isolate() for pageblock isolation");
 		return;
 	}
-	VM_WARN_ONCE(get_pfnblock_bit(page, page_to_pfn(page),
-				      PB_migrate_isolate),
+	VM_WARN_ONCE(get_pageblock_isolate(page),
 		     "Use clear_pageblock_isolate() to unisolate pageblock");
 	/* MIGRATETYPE_AND_ISO_MASK clears PB_migrate_isolate if it is set */
 #endif
@@ -2058,9 +2057,9 @@ static unsigned long find_large_buddy(unsigned long start_pfn)
 static inline void toggle_pageblock_isolate(struct page *page, bool isolate)
 {
 	if (isolate)
-		set_pfnblock_bit(page, page_to_pfn(page), PB_migrate_isolate);
+		set_pageblock_isolate(page);
 	else
-		clear_pfnblock_bit(page, page_to_pfn(page), PB_migrate_isolate);
+		clear_pageblock_isolate(page);
 }
 
 /**
Signature · 1 line
2.34.1
reviewer
#2 lore raw

Re: [PATCH 1/2] mm/page_alloc: use xxx_pageblock_isolate() for better reading

Quoted 7 lines · Wei Yang

Since commit e904bce2d9d4 ("mm/page_isolation: make page isolation a standalone bit"), it provides dedicated helper to handle isolation.

Change to use these helpers to be better reading.

No functional change intended.

Quoted 8 lines
Cc Zi Yan <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc David Hildenbrand <[email protected]>
---
 mm/page_alloc.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

LGTM. Reviewed-by: Zi Yan <[email protected]>

--
Best Regards,
Yan, Zi

Acked-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] mm/page_alloc: use xxx_pageblock_isolate() for better reading

In reply to Wei Yang
Quoted 5 lines · Wei Yang

Since commit e904bce2d9d4 ("mm/page_isolation: make page isolation a standalone bit"), it provides dedicated helper to handle isolation.

Change to use these helpers to be better reading.

Quoted 7 lines

No functional change intended.

Cc Zi Yan <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc David Hildenbrand <[email protected]>

---

Signature · 3 lines
Cheers

David / dhildenb
author
#4 lore raw

PATCH 2/2 mm/pageblock-flags: simplify MIGRATETYPE_MASK definition

In reply to Wei Yang

MIGRATETYPE_MASK is defined to be the mask of possible migratetype.

Define it with PB_migratetype_bits directly would be more clear.

Also, MIGRATETYPE_AND_ISO_MASK is MIGRATETYPE_MASK add isolation bit. Use MIGRATETYPE_MASK in the definition of MIGRATETYPE_AND_ISO_MASK looks cleaner.

No functional change intended.

Cc Zi Yan <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc David Hildenbrand <[email protected]>
---
 include/linux/pageblock-flags.h | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
include/linux/pageblock-flags.h +2 -3
index 6a44be0f39f4..1489c062a5a7 100644
@@ -37,11 +37,10 @@ enum pageblock_bits {
 
 #define NR_PAGEBLOCK_BITS (roundup_pow_of_two(__NR_PAGEBLOCK_BITS))
 
-#define MIGRATETYPE_MASK ((1UL << (PB_migrate_end + 1)) - 1)
+#define MIGRATETYPE_MASK (BIT(PB_migratetype_bits) - 1)
 
 #ifdef CONFIG_MEMORY_ISOLATION
-#define MIGRATETYPE_AND_ISO_MASK \
-	(((1UL << (PB_migrate_end + 1)) - 1) | BIT(PB_migrate_isolate))
+#define MIGRATETYPE_AND_ISO_MASK (MIGRATETYPE_MASK | BIT(PB_migrate_isolate))
 #else
 #define MIGRATETYPE_AND_ISO_MASK MIGRATETYPE_MASK
 #endif
Signature · 1 line
2.34.1
#5 lore raw

Re: [PATCH 2/2] mm/pageblock-flags: simplify MIGRATETYPE_MASK definition

Quoted 26 lines · Wei Yang

MIGRATETYPE_MASK is defined to be the mask of possible migratetype.

Define it with PB_migratetype_bits directly would be more clear.

Also, MIGRATETYPE_AND_ISO_MASK is MIGRATETYPE_MASK add isolation bit. Use MIGRATETYPE_MASK in the definition of MIGRATETYPE_AND_ISO_MASK looks cleaner.

No functional change intended.

Cc Zi Yan <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc David Hildenbrand <[email protected]>
---
  include/linux/pageblock-flags.h | 5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)
include/linux/pageblock-flags.h +0 -0
index 6a44be0f39f4..1489c062a5a7 100644
@@ -37,11 +37,10 @@ enum pageblock_bits {
 
  #define NR_PAGEBLOCK_BITS (roundup_pow_of_two(__NR_PAGEBLOCK_BITS))
 

include/linux/pageblock-flags.h enum pageblock_bits { · line 46

Quoted 10 lines
include/linux/pageblock-flags.h +2 -3
-#define MIGRATETYPE_MASK ((1UL << (PB_migrate_end + 1)) - 1)
+#define MIGRATETYPE_MASK (BIT(PB_migratetype_bits) - 1)
 
  #ifdef CONFIG_MEMORY_ISOLATION
-#define MIGRATETYPE_AND_ISO_MASK \
-	(((1UL << (PB_migrate_end + 1)) - 1) | BIT(PB_migrate_isolate))
+#define MIGRATETYPE_AND_ISO_MASK (MIGRATETYPE_MASK | BIT(PB_migrate_isolate))
  #else
  #define MIGRATETYPE_AND_ISO_MASK MIGRATETYPE_MASK
  #endif

Hm, I wonder if we can get rid of PB_migratetype_bits

include/linux/pageblock-flags.h +3 -4
index 6a44be0f39f45..70c988dbdddc8 100644
@@ -13,12 +13,11 @@
 
 #include <linux/types.h>
 
-#define PB_migratetype_bits 3
 /* Bit indices that affect a whole block of pages */
 enum pageblock_bits {
-       PB_migrate,
-       PB_migrate_end = PB_migrate + PB_migratetype_bits - 1,
-                       /* 3 bits required for migrate types */
+       PB_migrate_0,
+       PB_migrate_1,
+       PB_migrate_2,
        PB_compact_skip,/* If set the block is skipped by compaction */
 
 #ifdef CONFIG_MEMORY_ISOLATION

And then just work with the bits. No magical computations.

Signature · 3 lines
Cheers

David / dhildenb
author
#6 lore raw

Re: [PATCH 2/2] mm/pageblock-flags: simplify MIGRATETYPE_MASK definition

Quoted 60 lines · David Hildenbrand
Quoted 23 lines · Wei Yang

MIGRATETYPE_MASK is defined to be the mask of possible migratetype.

Define it with PB_migratetype_bits directly would be more clear.

Also, MIGRATETYPE_AND_ISO_MASK is MIGRATETYPE_MASK add isolation bit. Use MIGRATETYPE_MASK in the definition of MIGRATETYPE_AND_ISO_MASK looks cleaner.

No functional change intended.

Cc Zi Yan <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc David Hildenbrand <[email protected]>
---
  include/linux/pageblock-flags.h | 5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)
include/linux/pageblock-flags.h +0 -0
index 6a44be0f39f4..1489c062a5a7 100644

include/linux/pageblock-flags.h enum pageblock_bits { · line 43

Quoted 10 lines
include/linux/pageblock-flags.h +2 -3
@@ -37,11 +37,10 @@ enum pageblock_bits {
  #define NR_PAGEBLOCK_BITS (roundup_pow_of_two(__NR_PAGEBLOCK_BITS))
-#define MIGRATETYPE_MASK ((1UL << (PB_migrate_end + 1)) - 1)
+#define MIGRATETYPE_MASK (BIT(PB_migratetype_bits) - 1)
  #ifdef CONFIG_MEMORY_ISOLATION
-#define MIGRATETYPE_AND_ISO_MASK \
-	(((1UL << (PB_migrate_end + 1)) - 1) | BIT(PB_migrate_isolate))
+#define MIGRATETYPE_AND_ISO_MASK (MIGRATETYPE_MASK | BIT(PB_migrate_isolate))
  #else
  #define MIGRATETYPE_AND_ISO_MASK MIGRATETYPE_MASK
  #endif

Hm, I wonder if we can get rid of PB_migratetype_bits

include/linux/pageblock-flags.h +3 -4
index 6a44be0f39f45..70c988dbdddc8 100644
@@ -13,12 +13,11 @@
 
 #include <linux/types.h>
 
-#define PB_migratetype_bits 3
 /* Bit indices that affect a whole block of pages */
 enum pageblock_bits {
-       PB_migrate,
-       PB_migrate_end = PB_migrate + PB_migratetype_bits - 1,
-                       /* 3 bits required for migrate types */
+       PB_migrate_0,
+       PB_migrate_1,
+       PB_migrate_2,
       PB_compact_skip,/* If set the block is skipped by compaction */
 
 #ifdef CONFIG_MEMORY_ISOLATION
Quoted 1 line

And then just work with the bits. No magical computations.

Something like this?

include/linux/pageblock-flags.h +5 -7
index 6a44be0f39f4..a09b79a1fb7d 100644
@@ -13,12 +13,11 @@
 
 #include <linux/types.h>
 
-#define PB_migratetype_bits 3
 /* Bit indices that affect a whole block of pages */
 enum pageblock_bits {
-	PB_migrate,
-	PB_migrate_end = PB_migrate + PB_migratetype_bits - 1,
-			/* 3 bits required for migrate types */
+	PB_migrate_0,
+	PB_migrate_1,
+	PB_migrate_2,
 	PB_compact_skip,/* If set the block is skipped by compaction */
 
 #ifdef CONFIG_MEMORY_ISOLATION
@@ -37,11 +36,10 @@ enum pageblock_bits {
 
 #define NR_PAGEBLOCK_BITS (roundup_pow_of_two(__NR_PAGEBLOCK_BITS))
 
-#define MIGRATETYPE_MASK ((1UL << (PB_migrate_end + 1)) - 1)
+#define MIGRATETYPE_MASK (BIT(PB_migrate_2 + 1) - 1)
 
 #ifdef CONFIG_MEMORY_ISOLATION
-#define MIGRATETYPE_AND_ISO_MASK \
-	(((1UL << (PB_migrate_end + 1)) - 1) | BIT(PB_migrate_isolate))
+#define MIGRATETYPE_AND_ISO_MASK (MIGRATETYPE_MASK | BIT(PB_migrate_isolate))
 #else
 #define MIGRATETYPE_AND_ISO_MASK MIGRATETYPE_MASK
 #endif
mm/page_alloc.c +1 -1
index e534f31a6b39..7a67250ccfd5 100644
@@ -370,7 +370,7 @@ get_pfnblock_bitmap_bitidx(const struct page *page, unsigned long pfn,
 #else
 	BUILD_BUG_ON(NR_PAGEBLOCK_BITS != 4);
 #endif
-	BUILD_BUG_ON(__MIGRATE_TYPE_END >= (1 << PB_migratetype_bits));
+	BUILD_BUG_ON(__MIGRATE_TYPE_END >= BIT(PB_migrate_2 + 1));
 	VM_BUG_ON_PAGE(!zone_spans_pfn(page_zone(page), pfn), page);
 
 	bitmap = get_pageblock_bitmap(page, pfn);
Signature · 2 lines
Wei Yang
Help you, Help me
#7 lore raw

Re: [PATCH 2/2] mm/pageblock-flags: simplify MIGRATETYPE_MASK definition

Quoted 86 lines · Wei Yang
Quoted 60 lines · David Hildenbrand
Quoted 23 lines · Wei Yang

MIGRATETYPE_MASK is defined to be the mask of possible migratetype.

Define it with PB_migratetype_bits directly would be more clear.

Also, MIGRATETYPE_AND_ISO_MASK is MIGRATETYPE_MASK add isolation bit. Use MIGRATETYPE_MASK in the definition of MIGRATETYPE_AND_ISO_MASK looks cleaner.

No functional change intended.

Cc Zi Yan <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc David Hildenbrand <[email protected]>
---
   include/linux/pageblock-flags.h | 5 ++---
   1 file changed, 2 insertions(+), 3 deletions(-)
include/linux/pageblock-flags.h +0 -0
index 6a44be0f39f4..1489c062a5a7 100644

include/linux/pageblock-flags.h enum pageblock_bits { · line 43

Quoted 10 lines
include/linux/pageblock-flags.h +2 -3
@@ -37,11 +37,10 @@ enum pageblock_bits {
   #define NR_PAGEBLOCK_BITS (roundup_pow_of_two(__NR_PAGEBLOCK_BITS))
-#define MIGRATETYPE_MASK ((1UL << (PB_migrate_end + 1)) - 1)
+#define MIGRATETYPE_MASK (BIT(PB_migratetype_bits) - 1)
   #ifdef CONFIG_MEMORY_ISOLATION
-#define MIGRATETYPE_AND_ISO_MASK \
-	(((1UL << (PB_migrate_end + 1)) - 1) | BIT(PB_migrate_isolate))
+#define MIGRATETYPE_AND_ISO_MASK (MIGRATETYPE_MASK | BIT(PB_migrate_isolate))
   #else
   #define MIGRATETYPE_AND_ISO_MASK MIGRATETYPE_MASK
   #endif

Hm, I wonder if we can get rid of PB_migratetype_bits

include/linux/pageblock-flags.h +3 -4
index 6a44be0f39f45..70c988dbdddc8 100644
@@ -13,12 +13,11 @@
 
 #include <linux/types.h>
 
-#define PB_migratetype_bits 3
 /* Bit indices that affect a whole block of pages */
 enum pageblock_bits {
-       PB_migrate,
-       PB_migrate_end = PB_migrate + PB_migratetype_bits - 1,
-                       /* 3 bits required for migrate types */
+       PB_migrate_0,
+       PB_migrate_1,
+       PB_migrate_2,
        PB_compact_skip,/* If set the block is skipped by compaction */
 
 #ifdef CONFIG_MEMORY_ISOLATION
Quoted 1 line

And then just work with the bits. No magical computations.

Something like this?

include/linux/pageblock-flags.h +3 -4
index 6a44be0f39f4..a09b79a1fb7d 100644
@@ -13,12 +13,11 @@
 
  #include <linux/types.h>
 
-#define PB_migratetype_bits 3
  /* Bit indices that affect a whole block of pages */
  enum pageblock_bits {
-	PB_migrate,
-	PB_migrate_end = PB_migrate + PB_migratetype_bits - 1,
-			/* 3 bits required for migrate types */
+	PB_migrate_0,
+	PB_migrate_1,
+	PB_migrate_2,
  	PB_compact_skip,/* If set the block is skipped by compaction */
 
  #ifdef CONFIG_MEMORY_ISOLATION

include/linux/pageblock-flags.h enum pageblock_bits { · line 39

Quoted 5 lines
include/linux/pageblock-flags.h +1 -1
@@ -37,11 +36,10 @@ enum pageblock_bits {
 
  #define NR_PAGEBLOCK_BITS (roundup_pow_of_two(__NR_PAGEBLOCK_BITS))
 
-#define MIGRATETYPE_MASK ((1UL << (PB_migrate_end + 1)) - 1)
+#define MIGRATETYPE_MASK (BIT(PB_migrate_2 + 1) - 1)

I'd just do PB_migrate_0|PB_migrate_1|PB_migrate_2 and have code that is easier to parse.

Signature · 3 lines
Cheers

David / dhildenb
reviewer
#8 lore raw

Re: [PATCH 2/2] mm/pageblock-flags: simplify MIGRATETYPE_MASK definition

In reply to Wei Yang
Quoted 23 lines · Wei Yang

MIGRATETYPE_MASK is defined to be the mask of possible migratetype.

Define it with PB_migratetype_bits directly would be more clear.

Also, MIGRATETYPE_AND_ISO_MASK is MIGRATETYPE_MASK add isolation bit. Use MIGRATETYPE_MASK in the definition of MIGRATETYPE_AND_ISO_MASK looks cleaner.

No functional change intended.

Cc Zi Yan <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc David Hildenbrand <[email protected]>
---
 include/linux/pageblock-flags.h | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
include/linux/pageblock-flags.h +0 -0
index 6a44be0f39f4..1489c062a5a7 100644

include/linux/pageblock-flags.h enum pageblock_bits { · line 40

Quoted 5 lines
include/linux/pageblock-flags.h +1 -1
@@ -37,11 +37,10 @@ enum pageblock_bits {
 
 #define NR_PAGEBLOCK_BITS (roundup_pow_of_two(__NR_PAGEBLOCK_BITS))
 
-#define MIGRATETYPE_MASK ((1UL << (PB_migrate_end + 1)) - 1)
+#define MIGRATETYPE_MASK (BIT(PB_migratetype_bits) - 1)

I am not sure about this one, even if PB_migrate_end + 1 == PB_migratetype_bits. PB_migrate_end represents a bit index, where PB_migratetype_bits means the number of the bits.

David's suggestion in another email might be better.

include/linux/pageblock-flags.h enum pageblock_bits { · line 46

Quoted 8 lines
include/linux/pageblock-flags.h +1 -2
 #ifdef CONFIG_MEMORY_ISOLATION
-#define MIGRATETYPE_AND_ISO_MASK \
-	(((1UL << (PB_migrate_end + 1)) - 1) | BIT(PB_migrate_isolate))
+#define MIGRATETYPE_AND_ISO_MASK (MIGRATETYPE_MASK | BIT(PB_migrate_isolate))
 #else
 #define MIGRATETYPE_AND_ISO_MASK MIGRATETYPE_MASK
 #endif

This one looks good to me.

--
Best Regards,
Yan, Zi