Skip to contentSkip to conversation

mm/mm_init: simplify zone_spanned_pages_in_node()

PATCH Superseded Sang-Heon Jeon 2 participants 7 messages Aug 6 – Aug 13

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover mm/mm_init: simplify zone_spanned_pages_in_node()

Hello,

This series simplifies zone_spanned_pages_in_node(), which adjusts *zone_start_pfn and *zone_end_pfn before and after calling adjust_zone_range_for_zone_movable(). The helper adjusts the same two values, which makes the code harder to follow.

Split into five patches for easier review. Squashing them into one patch when applying is also fine.

Patch 1 folds adjust_zone_range_for_zone_movable() into the caller.

Patch 2 removes the clamping to node_end_pfn, which has no effect.

Patch 3 moves the check and the clamping to node_start_pfn into the ZONE_MOVABLE branch, which is the only place they have an effect.

Patch 4 merges the two branches that adjust for zone_movable_pfn[nid].

Patch 5 extracts a local variable and reduces the indentation level.

No functional change.

---
Changes from v1 [1]
- Change base to memblock/for-next
- Add patch 4 to merge the two branches
- Update comment

[1] https://lore.kernel.org/all/[email protected]/
---

Sang-Heon Jeon (5):
mm/mm_init: fold adjust_zone_range_for_zone_movable() into its only
caller
mm/mm_init: remove redundant clamping in zone_spanned_pages_in_node() mm/mm_init: move the range check and clamping inside ZONE_MOVABLE
branch
mm/mm_init: use min() when adjusting for ZONE_MOVABLE mm/mm_init: clean up zone_spanned_pages_in_node()

 mm/mm_init.c | 79 ++++++++++++++++++++++++----------------------------
 1 file changed, 36 insertions(+), 43 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/5 mm/mm_init: fold adjust_zone_range_for_zone_movable() into its only caller

adjust_zone_range_for_zone_movable() adjusts zone_start_pfn and zone_end_pfn. It is only called from zone_spanned_pages_in_node(), which adjusts the same two values before and after the call. This makes the code harder to follow.

Fold the helper into the caller to make the code clearer.

No functional change.

---
 mm/mm_init.c | 64 ++++++++++++++++++++++------------------------------
 1 file changed, 27 insertions(+), 37 deletions(-)
mm/mm_init.c +27 -37
index eb53c327210b..30d3125951f0 100644
@@ -1124,41 +1124,6 @@ void __ref memmap_init_zone_device(struct zone *zone,
 }
 #endif
 
-/*
- * The zone ranges provided by the architecture do not include ZONE_MOVABLE
- * because it is sized independent of architecture. Unlike the other zones,
- * the starting point for ZONE_MOVABLE is not fixed. It may be different
- * in each node depending on the size of each node and how evenly kernelcore
- * is distributed. This helper function adjusts the zone ranges
- * provided by the architecture for a given node by using the end of the
- * highest usable zone for ZONE_MOVABLE. This preserves the assumption that
- * zones within a node are in order of monotonic increases memory addresses
- */
-static void __init adjust_zone_range_for_zone_movable(int nid,
-					unsigned long zone_type,
-					unsigned long node_end_pfn,
-					unsigned long *zone_start_pfn,
-					unsigned long *zone_end_pfn)
-{
-	/* Only adjust if ZONE_MOVABLE is on this node */
-	if (zone_movable_pfn[nid]) {
-		/* Size ZONE_MOVABLE */
-		if (zone_type == ZONE_MOVABLE) {
-			*zone_start_pfn = zone_movable_pfn[nid];
-			*zone_end_pfn = min(node_end_pfn,
-				arch_zone_highest_possible_pfn[movable_zone]);
-
-		/* Adjust for ZONE_MOVABLE starting within this range */
-		} else if (*zone_start_pfn < zone_movable_pfn[nid] &&
-			   *zone_end_pfn > zone_movable_pfn[nid]) {
-			*zone_end_pfn = zone_movable_pfn[nid];
-
-		/* Check if this whole range is within ZONE_MOVABLE */
-		} else if (*zone_start_pfn >= zone_movable_pfn[nid])
-			*zone_start_pfn = *zone_end_pfn;
-	}
-}
-
 /*
  * Return the number of holes in a range on a node. If nid is MAX_NUMNODES,
  * then all holes in the requested range will be accounted for.
@@ -1208,6 +1173,15 @@ static unsigned long __init zone_absent_pages_in_node(int nid,
 /*
  * Return the number of pages a zone spans in a node, including holes
  * present_pages = zone_spanned_pages_in_node() - zone_absent_pages_in_node()
+ *
+ * The zone ranges provided by the architecture do not include ZONE_MOVABLE
+ * because it is sized independent of architecture. Unlike the other zones,
+ * the starting point for ZONE_MOVABLE is not fixed. It may be different
+ * in each node depending on the size of each node and how evenly kernelcore
+ * is distributed. The zone ranges provided by the architecture are adjusted
+ * for a given node by using the end of the highest usable zone for
+ * ZONE_MOVABLE. This preserves the assumption that zones within a node are
+ * in order of monotonic increases memory addresses
  */
 static unsigned long __init zone_spanned_pages_in_node(int nid,
 					unsigned long zone_type,
@@ -1222,8 +1196,24 @@ static unsigned long __init zone_spanned_pages_in_node(int nid,
 	/* Get the start and end of the zone */
 	*zone_start_pfn = clamp(node_start_pfn, zone_low, zone_high);
 	*zone_end_pfn = clamp(node_end_pfn, zone_low, zone_high);
-	adjust_zone_range_for_zone_movable(nid, zone_type, node_end_pfn,
-					   zone_start_pfn, zone_end_pfn);
+
+	/* Only adjust if ZONE_MOVABLE is on this node */
+	if (zone_movable_pfn[nid]) {
+		/* Size ZONE_MOVABLE */
+		if (zone_type == ZONE_MOVABLE) {
+			*zone_start_pfn = zone_movable_pfn[nid];
+			*zone_end_pfn = min(node_end_pfn,
+				arch_zone_highest_possible_pfn[movable_zone]);
+
+		/* Adjust for ZONE_MOVABLE starting within this range */
+		} else if (*zone_start_pfn < zone_movable_pfn[nid] &&
+			   *zone_end_pfn > zone_movable_pfn[nid]) {
+			*zone_end_pfn = zone_movable_pfn[nid];
+
+		/* Check if this whole range is within ZONE_MOVABLE */
+		} else if (*zone_start_pfn >= zone_movable_pfn[nid])
+			*zone_start_pfn = *zone_end_pfn;
+	}
 
 	/* Check that this node has pages within the zone's required range */
 	if (*zone_end_pfn < node_start_pfn || *zone_start_pfn > node_end_pfn)
Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 2/5 mm/mm_init: remove redundant clamping in zone_spanned_pages_in_node()

In reply to Sang-Heon Jeon

Clamping *zone_end_pfn to node_end_pfn has no effect, because *zone_end_pfn <= node_end_pfn is guaranteed at that point.

1. If zone_movable_pfn[nid] != 0 && zone_type == ZONE_MOVABLE, *zone_end_pfn = min(node_end_pfn, ...) <= node_end_pfn.

2. Else If zone_movable_pfn[nid] != 0 && *zone_start_pfn < zone_movable_pfn[nid] < *zone_end_pfn, *zone_end_pfn = zone_movable_pfn[nid].

The condition requires the clamped *zone_start_pfn and *zone_end_pfn to differ, which cannot happen when node_end_pfn < zone_low.

So zone_movable_pfn[nid] < clamped *zone_end_pfn = min(node_end_pfn, zone_high) <= node_end_pfn.

3. Else *zone_end_pfn = clamp(node_end_pfn, zone_low, zone_high)

a. If node_end_pfn >= zone_low, *zone_end_pfn =
min(node_end_pfn, zone_high) <= node_end_pfn.

b. If node_end_pfn < zone_low, *zone_start_pfn = zone_low >
node_end_pfn, so the function returns 0 before the clamping is executed.

So remove it.

No functional change.

---
 mm/mm_init.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
mm/mm_init.c +1 -2
index 30d3125951f0..bdc15de0a62a 100644
@@ -1219,8 +1219,7 @@ static unsigned long __init zone_spanned_pages_in_node(int nid,
 	if (*zone_end_pfn < node_start_pfn || *zone_start_pfn > node_end_pfn)
 		return 0;
 
-	/* Move the zone boundaries inside the node if necessary */
-	*zone_end_pfn = min(*zone_end_pfn, node_end_pfn);
+	/* Move the zone start inside the node if necessary */
 	*zone_start_pfn = max(*zone_start_pfn, node_start_pfn);
 
 	/* Return the spanned pages */
Signature · 1 line
2.43.0
#3 lore raw

PATCH v2 3/5 mm/mm_init: move the range check and clamping inside ZONE_MOVABLE branch

In reply to Sang-Heon Jeon

Outside the ZONE_MOVABLE branch the check and the clamping have no effect, because every branch ends with one of the following.

- *zone_start_pfn == *zone_end_pfn, so the function returns 0 anyway.

- node_start_pfn <= *zone_start_pfn <= *zone_end_pfn <= node_end_pfn, so the check does not return 0 and max(*zone_start_pfn, node_start_pfn) is always *zone_start_pfn.

The branches set *zone_start_pfn and *zone_end_pfn as follows.

1. If zone_movable_pfn[nid] != 0 && zone_type == ZONE_MOVABLE, *zone_start_pfn = zone_movable_pfn[nid] and *zone_end_pfn = min(node_end_pfn, ...).

2. Else If zone_movable_pfn[nid] != 0 && *zone_start_pfn < zone_movable_pfn[nid] < *zone_end_pfn, *zone_end_pfn = zone_movable_pfn[nid].

The condition requires the clamped *zone_start_pfn and *zone_end_pfn to differ, which cannot happen when node_end_pfn < zone_low or node_start_pfn > zone_high.

So *zone_start_pfn = max(node_start_pfn, zone_low) >= node_start_pfn, and zone_movable_pfn[nid] < clamped *zone_end_pfn = min(node_end_pfn, zone_high) <= node_end_pfn.

So node_start_pfn <= *zone_start_pfn < *zone_end_pfn <= node_end_pfn.

3. Else If zone_movable_pfn[nid] != 0 && *zone_start_pfn >= zone_movable_pfn[nid], *zone_start_pfn = *zone_end_pfn.

4. Else *zone_start_pfn = clamp(node_start_pfn, zone_low, zone_high) and *zone_end_pfn = clamp(node_end_pfn, zone_low, zone_high)

a. If node_end_pfn < zone_low, *zone_start_pfn = *zone_end_pfn =
zone_low.

b. If node_start_pfn > zone_high, *zone_start_pfn = *zone_end_pfn =
zone_high.

c. If node_end_pfn >= zone_low && node_start_pfn <= zone_high,

      node_start_pfn <= *zone_start_pfn <= *zone_end_pfn <=
      node_end_pfn.

So move both inside the ZONE_MOVABLE branch.

No functional change.

---
 mm/mm_init.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
mm/mm_init.c +8 -7
index bdc15de0a62a..fa6b5977261f 100644
@@ -1205,6 +1205,14 @@ static unsigned long __init zone_spanned_pages_in_node(int nid,
 			*zone_end_pfn = min(node_end_pfn,
 				arch_zone_highest_possible_pfn[movable_zone]);
 
+			/* Check that this node has pages within the zone's required range */
+			if (*zone_end_pfn < node_start_pfn ||
+			    *zone_start_pfn > node_end_pfn)
+				return 0;
+
+			/* Move the zone start inside the node if necessary */
+			*zone_start_pfn = max(*zone_start_pfn, node_start_pfn);
+
 		/* Adjust for ZONE_MOVABLE starting within this range */
 		} else if (*zone_start_pfn < zone_movable_pfn[nid] &&
 			   *zone_end_pfn > zone_movable_pfn[nid]) {
@@ -1215,13 +1223,6 @@ static unsigned long __init zone_spanned_pages_in_node(int nid,
 			*zone_start_pfn = *zone_end_pfn;
 	}
 
-	/* Check that this node has pages within the zone's required range */
-	if (*zone_end_pfn < node_start_pfn || *zone_start_pfn > node_end_pfn)
-		return 0;
-
-	/* Move the zone start inside the node if necessary */
-	*zone_start_pfn = max(*zone_start_pfn, node_start_pfn);
-
 	/* Return the spanned pages */
 	return *zone_end_pfn - *zone_start_pfn;
 }
Signature · 1 line
2.43.0
#4 lore raw

PATCH v2 4/5 mm/mm_init: use min() when adjusting for ZONE_MOVABLE

In reply to Sang-Heon Jeon

Outside the ZONE_MOVABLE branch, the two branches set *zone_start_pfn and *zone_end_pfn as follows.

1. If *zone_start_pfn < zone_movable_pfn[nid], *zone_end_pfn ends up as min(*zone_end_pfn, zone_movable_pfn[nid]).

a. If *zone_end_pfn > zone_movable_pfn[nid], *zone_end_pfn =
zone_movable_pfn[nid].

b. If *zone_end_pfn <= zone_movable_pfn[nid], *zone_end_pfn is
unchanged.

2. Else *zone_start_pfn >= zone_movable_pfn[nid] and the whole range is within ZONE_MOVABLE, so *zone_start_pfn = *zone_end_pfn.

So use min() to simplify the conditions.

No functional change.

---
 mm/mm_init.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
mm/mm_init.c +6 -6
index fa6b5977261f..27f454edbebb 100644
@@ -1213,14 +1213,14 @@ static unsigned long __init zone_spanned_pages_in_node(int nid,
 			/* Move the zone start inside the node if necessary */
 			*zone_start_pfn = max(*zone_start_pfn, node_start_pfn);
 
-		/* Adjust for ZONE_MOVABLE starting within this range */
-		} else if (*zone_start_pfn < zone_movable_pfn[nid] &&
-			   *zone_end_pfn > zone_movable_pfn[nid]) {
-			*zone_end_pfn = zone_movable_pfn[nid];
+		/* This range starts below ZONE_MOVABLE */
+		} else if (*zone_start_pfn < zone_movable_pfn[nid]) {
+			*zone_end_pfn = min(*zone_end_pfn, zone_movable_pfn[nid]);
 
-		/* Check if this whole range is within ZONE_MOVABLE */
-		} else if (*zone_start_pfn >= zone_movable_pfn[nid])
+		/* This whole range is within ZONE_MOVABLE */
+		} else {
 			*zone_start_pfn = *zone_end_pfn;
+		}
 	}
 
 	/* Return the spanned pages */
Signature · 1 line
2.43.0
#5 lore raw

PATCH v2 5/5 mm/mm_init: clean up zone_spanned_pages_in_node()

In reply to Sang-Heon Jeon

Extract zone_movable_pfn[nid], which is used four times, into a local variable, and return early to reduce the indentation level.

No functional change.

---
 mm/mm_init.c | 43 +++++++++++++++++++++++--------------------
 1 file changed, 23 insertions(+), 20 deletions(-)
mm/mm_init.c +23 -20
index 27f454edbebb..ab445111a846 100644
@@ -1192,37 +1192,40 @@ static unsigned long __init zone_spanned_pages_in_node(int nid,
 {
 	unsigned long zone_low = arch_zone_lowest_possible_pfn[zone_type];
 	unsigned long zone_high = arch_zone_highest_possible_pfn[zone_type];
+	unsigned long movable_pfn = zone_movable_pfn[nid];
 
 	/* Get the start and end of the zone */
 	*zone_start_pfn = clamp(node_start_pfn, zone_low, zone_high);
 	*zone_end_pfn = clamp(node_end_pfn, zone_low, zone_high);
 
-	/* Only adjust if ZONE_MOVABLE is on this node */
-	if (zone_movable_pfn[nid]) {
-		/* Size ZONE_MOVABLE */
-		if (zone_type == ZONE_MOVABLE) {
-			*zone_start_pfn = zone_movable_pfn[nid];
-			*zone_end_pfn = min(node_end_pfn,
-				arch_zone_highest_possible_pfn[movable_zone]);
+	/* Nothing to adjust if ZONE_MOVABLE is not on this node */
+	if (!movable_pfn)
+		goto out;
 
-			/* Check that this node has pages within the zone's required range */
-			if (*zone_end_pfn < node_start_pfn ||
-			    *zone_start_pfn > node_end_pfn)
-				return 0;
+	/* Size ZONE_MOVABLE */
+	if (zone_type == ZONE_MOVABLE) {
+		*zone_start_pfn = movable_pfn;
+		*zone_end_pfn = min(node_end_pfn,
+			arch_zone_highest_possible_pfn[movable_zone]);
 
-			/* Move the zone start inside the node if necessary */
-			*zone_start_pfn = max(*zone_start_pfn, node_start_pfn);
+		/* Check that this node has pages within the zone's required range */
+		if (*zone_end_pfn < node_start_pfn ||
+		    *zone_start_pfn > node_end_pfn)
+			return 0;
 
-		/* This range starts below ZONE_MOVABLE */
-		} else if (*zone_start_pfn < zone_movable_pfn[nid]) {
-			*zone_end_pfn = min(*zone_end_pfn, zone_movable_pfn[nid]);
+		/* Move the zone start inside the node if necessary */
+		*zone_start_pfn = max(*zone_start_pfn, node_start_pfn);
 
-		/* This whole range is within ZONE_MOVABLE */
-		} else {
-			*zone_start_pfn = *zone_end_pfn;
-		}
+	/* This range starts below ZONE_MOVABLE */
+	} else if (*zone_start_pfn < movable_pfn) {
+		*zone_end_pfn = min(*zone_end_pfn, movable_pfn);
+
+	/* This whole range is within ZONE_MOVABLE */
+	} else {
+		*zone_start_pfn = *zone_end_pfn;
 	}
 
+out:
 	/* Return the spanned pages */
 	return *zone_end_pfn - *zone_start_pfn;
 }
Signature · 1 line
2.43.0
maintainer
#6 lore raw
In reply to Sang-Heon Jeon
Quoted 10 lines

Hello,

This series simplifies zone_spanned_pages_in_node(), which adjusts *zone_start_pfn and *zone_end_pfn before and after calling adjust_zone_range_for_zone_movable(). The helper adjusts the same two values, which makes the code harder to follow.

Split into five patches for easier review. Squashing them into one patch when applying is also fine.

Quoted 8 lines

Patch 1 folds adjust_zone_range_for_zone_movable() into the caller.

Patch 2 removes the clamping to node_end_pfn, which has no effect.

Patch 3 moves the check and the clamping to node_start_pfn into the ZONE_MOVABLE branch, which is the only place they have an effect.

Patch 4 merges the two branches that adjust for zone_movable_pfn[nid].

It would be easier to review if patches 2-4 would be squashed.

Quoted 1 line

Patch 5 extracts a local variable and reduces the indentation level.

This change can be folded into the first patch.

Signature · 2 lines
Sincerely yours,
Mike.