Skip to contentSkip to conversation

mm/page_alloc: Some clarifications for migratetype fallback

PATCH Applied Brendan Jackman 2 participants 4 messages Feb 28, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v4 cover mm/page_alloc: Some clarifications for migratetype fallback

A couple of patches to try and make the code easier to follow.

This is based on an old mm-unstable as v3 is already merged there. Base commit info is attached below.

---
Changes in v4:
- Some more precision about "try to claim" vs "claim"
- More improvements to the commentary text.
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- Rebased onto mm-unstable to get on top of Johannes' cleanups:
https://lore.kernel.org/linux-mm/[email protected]/
- Fixed missing renames
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Tweaked some naming
- Added clarifications of should_claim_block()
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Brendan Jackman (2):
mm/page_alloc: Clarify terminology in migratetype fallback code mm/page_alloc: Clarify should_claim_block() commentary

 mm/compaction.c |   4 +--
 mm/internal.h   |   2 +-
 mm/page_alloc.c | 100 ++++++++++++++++++++++++++++++--------------------------
 3 files changed, 56 insertions(+), 50 deletions(-)

---
base-commit: 801d47bd96ce22acd43809bc09e004679f707c39
change-id: 20250214-clarify-steal-f244880441c1

Best regards,

Signature · 1 line
Brendan Jackman <[email protected]>
#1 lore raw

PATCH v4 1/2 mm/page_alloc: Clarify terminology in migratetype fallback code

This code is rather confusing because:

1. "Steal" is sometimes used to refer to the general concept of allocating from a from a block of a fallback migratetype (steal_suitable_fallback()) but sometimes it refers specifically to converting a whole block's migratetype (can_steal_fallback()).

2. can_steal_fallback() sounds as though it's answering the question "am I functionally permitted to allocate from that other type" but in fact it is encoding a heuristic preference.

3. The same piece of data has different names in different places:
can_steal vs whole_block. This reinforces point 2 because it looks like the different names reflect a shift in intent from "am I allowed to steal" to "do I want to steal", but no such shift exists.

Fix 1. by avoiding the term "steal" in ambiguous contexts. Start using the term "claim" to refer to the special case of stealing the entire block.

Fix 2. by using "should" instead of "can", and also rename its parameters and add some commentary to make it more explicit what they mean.

Fix 3. by adopting the new "claim" terminology universally for this set of variables.

---
 mm/compaction.c |  4 ++--
 mm/internal.h   |  2 +-
 mm/page_alloc.c | 72 ++++++++++++++++++++++++++++-----------------------------
 3 files changed, 39 insertions(+), 39 deletions(-)
mm/compaction.c +2 -2
index 0992106d4ea751f7f1f8ebf7c75cd433d676cbe0..550ce50218075509ccb5f9485fd84f5d1f3d23a7 100644
@@ -2333,7 +2333,7 @@ static enum compact_result __compact_finished(struct compact_control *cc)
 	ret = COMPACT_NO_SUITABLE_PAGE;
 	for (order = cc->order; order < NR_PAGE_ORDERS; order++) {
 		struct free_area *area = &cc->zone->free_area[order];
-		bool can_steal;
+		bool claim_block;
 
 		/* Job done if page is free of the right migratetype */
 		if (!free_area_empty(area, migratetype))
@@ -2350,7 +2350,7 @@ static enum compact_result __compact_finished(struct compact_control *cc)
 		 * other migratetype buddy lists.
 		 */
 		if (find_suitable_fallback(area, order, migratetype,
-						true, &can_steal) != -1)
+						true, &claim_block) != -1)
 			/*
 			 * Movable pages are OK in any pageblock. If we are
 			 * stealing for a non-movable allocation, make sure
mm/internal.h +1 -1
index b07550db2bfd1d152fa90f91b3687b0fa1a9f653..aa30282a774ae26349944a75da854ae6a3da2a98 100644
@@ -863,7 +863,7 @@ static inline void init_cma_pageblock(struct page *page)
 
 
 int find_suitable_fallback(struct free_area *area, unsigned int order,
-			int migratetype, bool only_stealable, bool *can_steal);
+			int migratetype, bool claim_only, bool *claim_block);
 
 static inline bool free_area_empty(struct free_area *area, int migratetype)
 {
mm/page_alloc.c +36 -36

154 lines not on this page · raw message

Signature · 1 line
2.48.1.711.g2feabab25a-goog
#2 lore raw

PATCH v4 2/2 mm/page_alloc: Clarify should_claim_block() commentary

In reply to Brendan Jackman

There's lots of text here but it's a little hard to follow, this is an attempt to break it up and align its structure more closely with the code.

Reword the top-level function comment to just explain what question the function answers from the point of view of the caller.

Break up the internal logic into different sections that can have their own commentary describing why that part of the rationale is present.

Note the page_group_by_mobility_disabled logic is not explained in the commentary, that is outside the scope of this patch...

---
 mm/page_alloc.c | 46 ++++++++++++++++++++++++++--------------------
 1 file changed, 26 insertions(+), 20 deletions(-)
mm/page_alloc.c +26 -20
index 441c9d9cc5f8edbae1f4169207f5de9f32586f34..17ba5d758aa539370947b6f894e5fce7de6c5c5e 100644
@@ -1941,16 +1941,9 @@ static inline bool boost_watermark(struct zone *zone)
 }
 
 /*
- * When we are falling back to another migratetype during allocation, try to
- * claim entire blocks to satisfy further allocations, instead of polluting
- * multiple pageblocks.
- *
- * If we are stealing a relatively large buddy page, it is likely there will be
- * more free pages in the pageblock, so try to claim the whole block. For
- * reclaimable and unmovable allocations, we try to claim the whole block
- * regardless of page size, as fragmentation caused by those allocations
- * polluting movable pageblocks is worse than movable allocations stealing from
- * unmovable and reclaimable pageblocks.
+ * When we are falling back to another migratetype during allocation, should we
+ * try to claim an entire block to satisfy further allocations, instead of
+ * polluting multiple pageblocks?
  */
 static bool should_try_claim_block(unsigned int order, int start_mt)
 {
@@ -1965,19 +1958,32 @@ static bool should_try_claim_block(unsigned int order, int start_mt)
 		return true;
 
 	/*
-	 * Movable pages won't cause permanent fragmentation, so when you alloc
-	 * small pages, you just need to temporarily steal unmovable or
-	 * reclaimable pages that are closest to the request size.  After a
-	 * while, memory compaction may occur to form large contiguous pages,
-	 * and the next movable allocation may not need to steal.  Unmovable and
-	 * reclaimable allocations need to actually claim the whole block.
+	 * Above a certain threshold, always try to claim, as it's likely there
+	 * will be more free pages in the pageblock.
 	 */
-	if (order >= pageblock_order / 2 ||
-		start_mt == MIGRATE_RECLAIMABLE ||
-		start_mt == MIGRATE_UNMOVABLE ||
-		page_group_by_mobility_disabled)
+	if (order >= pageblock_order / 2)
 		return true;
 
+	/*
+	 * Unmovable/reclaimable allocations would cause permanent
+	 * fragmentations if they fell back to allocating from a movable block
+	 * (polluting it), so we try to claim the whole block regardless of the
+	 * allocation size. Later movable allocations can always steal from this
+	 * block, which is less problematic.
+	 */
+	if (start_mt == MIGRATE_RECLAIMABLE || start_mt == MIGRATE_UNMOVABLE)
+		return true;
+
+	if (page_group_by_mobility_disabled)
+		return true;
+
+	/*
+	 * Movable pages won't cause permanent fragmentation, so when you alloc
+	 * small pages, we just need to temporarily steal unmovable or
+	 * reclaimable pages that are closest to the request size. After a
+	 * while, memory compaction may occur to form large contiguous pages,
+	 * and the next movable allocation may not need to steal.
+	 */
 	return false;
 }
 
Signature · 1 line
2.48.1.711.g2feabab25a-goog
Reviewed-by on v4 2/2
#3 lore raw

Re: [PATCH v4 2/2] mm/page_alloc: Clarify should_claim_block() commentary

Quoted 7 lines · Brendan Jackman

There's lots of text here but it's a little hard to follow, this is an attempt to break it up and align its structure more closely with the code.

Reword the top-level function comment to just explain what question the function answers from the point of view of the caller.

Quoted 7 lines

Break up the internal logic into different sections that can have their own commentary describing why that part of the rationale is present.

Note the page_group_by_mobility_disabled logic is not explained in the commentary, that is outside the scope of this patch...

Quoted 5 lines
mm/page_alloc.c +5 -0
+	/*
+	 * Unmovable/reclaimable allocations would cause permanent
+	 * fragmentations if they fell back to allocating from a movable block
+	 * (polluting it), so we try to claim the whole block regardless of the
+	 * allocation size. Later movable allocations can always steal from this

mm/page_alloc.c static bool should_try_claim_block(unsigned int order, int start_mt) · line 1981

Quoted 10 lines
mm/page_alloc.c +10 -0
+	 * block, which is less problematic.
+	 */
+	if (start_mt == MIGRATE_RECLAIMABLE || start_mt == MIGRATE_UNMOVABLE)
+		return true;
+
+	if (page_group_by_mobility_disabled)
+		return true;
+
+	/*
+	 * Movable pages won't cause permanent fragmentation, so when you alloc

s/you/we/ for consistency? I think Andrew can amend locally to avoid resend.

Thanks.

Quoted 9 lines
mm/page_alloc.c +5 -0
+	 * small pages, we just need to temporarily steal unmovable or
+	 * reclaimable pages that are closest to the request size. After a
+	 * while, memory compaction may occur to form large contiguous pages,
+	 * and the next movable allocation may not need to steal.
+	 */
 	return false;
 }