Skip to contentSkip to conversation

treewide: remove unnecessary invalid range checks in memblock iteration loops

PATCH Superseded Sang-Heon Jeon 2 participants 11 messages Jun 29 – Jun 30

Subsystems (9)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover treewide: remove unnecessary invalid range checks in memblock iteration loops

The memblock API guarantees that for_each_mem_range() and for_each_mem_pfn_range() never return an invalid range, meaning start is always less than end.

Several memblock callers still have unnecessary invalid range checks in their loop bodies, so remove them.

Patches 1-6 cover for_each_mem_range() callers. memblock never stores a zero-size region, so the range it returns always has start < end. Some callers apply __va() or __phys_to_virt() before comparing, but these keep start < end too, so the check is unreachable.

Patches 7-8 cover for_each_mem_pfn_range() callers. __next_mem_pfn_range() skips any region that contains no whole page, so it only ever returns start_pfn < end_pfn and the check is unnecessary.

For reference, commit 36ca7f4be809 ("arm64: mm: Remove bogus stop condition from map_mem() loop") did a similar cleanup in arm64 map_mem().

All these checks are in different trees, so I split the change into one patch per arch/subsystem. The patches are independent and can be applied separately.

---
Changes from v1 [1]
- Add review-by, tested-by tags from v1
- Add missing simliar patches(5,6) to patch series
- Change base to rppt/for-next

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

Sang-Heon Jeon (8):
arm64: mm: remove unreachable invalid range check in
kasan_init_shadow()
LoongArch: remove unreachable invalid range check in kasan_init()
riscv: remove unreachable invalid range check in
create_linear_mapping_page_table()
riscv: remove unreachable invalid range check in kasan_init()
ARM: remove unreachable invalid range check in kasan_init() powerpc64/kasan: Remove unreachable invalid range check in
kasan_init_phys_region()
mm: remove unnecessary empty range check in
early_calculate_totalpages()
mm/hugetlb: remove unnecessary empty range check in
hugetlb_bootmem_set_nodes()

 arch/arm/mm/kasan_init.c               | 6 ------
 arch/arm64/mm/kasan_init.c             | 3 ---
 arch/loongarch/mm/kasan_init.c         | 3 ---
 arch/powerpc/mm/kasan/init_book3e_64.c | 3 ---
 arch/powerpc/mm/kasan/init_book3s_64.c | 3 ---
 arch/riscv/mm/init.c                   | 2 --
 arch/riscv/mm/kasan_init.c             | 3 ---
 mm/hugetlb.c                           | 3 +--
 mm/mm_init.c                           | 3 +--
 9 files changed, 2 insertions(+), 27 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/8 arm64: mm: remove unreachable invalid range check in kasan_init_shadow()

kasan_init_shadow() maps each memblock region with for_each_mem_range() and breaks the loop when start >= end. for_each_mem_range() never returns an invalid range, so start < end always.

Therefore the start >= end check is unreachable, so remove it.

No functional change.

---
 arch/arm64/mm/kasan_init.c | 3 ---
 1 file changed, 3 deletions(-)
arch/arm64/mm/kasan_init.c +0 -3
index 3fcad956fdf7..45fbdce684c8 100644
@@ -353,9 +353,6 @@ static void __init kasan_init_shadow(void)
 		void *start = (void *)__phys_to_virt(pa_start);
 		void *end = (void *)__phys_to_virt(pa_end);
 
-		if (start >= end)
-			break;
-
 		kasan_map_populate((unsigned long)kasan_mem_to_shadow(start),
 				   (unsigned long)kasan_mem_to_shadow(end),
 				   early_pfn_to_nid(virt_to_pfn(start)));
Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 2/8 LoongArch: remove unreachable invalid range check in kasan_init()

In reply to Sang-Heon Jeon

kasan_init() populates the linear mapping shadow with for_each_mem_range() and breaks the loop when start >= end. for_each_mem_range() never returns an invalid range, so start < end always.

Therefore the start >= end check is unreachable, so remove it.

No functional change.

---
 arch/loongarch/mm/kasan_init.c | 3 ---
 1 file changed, 3 deletions(-)
arch/loongarch/mm/kasan_init.c +0 -3
index 0fc02ca06457..92ca0ba86406 100644
@@ -305,9 +305,6 @@ void __init kasan_init(void)
 		void *start = (void *)phys_to_virt(pa_start);
 		void *end   = (void *)phys_to_virt(pa_end);
 
-		if (start >= end)
-			break;
-
 		kasan_map_populate((unsigned long)mem_to_shadow(start),
 			(unsigned long)mem_to_shadow(end), NUMA_NO_NODE);
 	}
Signature · 1 line
2.43.0
#3 lore raw

PATCH v2 3/8 riscv: remove unreachable invalid range check in create_linear_mapping_page_table()

In reply to Sang-Heon Jeon

create_linear_mapping_page_table() iterates memblock regions with for_each_mem_range() and breaks the loop when start >= end. for_each_mem_range() never returns an invalid range, so start < end always.

Therefore the start >= end check is unreachable, so remove it.

No functional change.

---
 arch/riscv/mm/init.c | 2 --
 1 file changed, 2 deletions(-)
arch/riscv/mm/init.c +0 -2
index 5b1b3c88b4d1..eb93c2ac05a6 100644
@@ -1229,8 +1229,6 @@ static void __init create_linear_mapping_page_table(void)
 
 	/* Map all memory banks in the linear mapping */
 	for_each_mem_range(i, &start, &end) {
-		if (start >= end)
-			break;
 		if (start <= __pa(PAGE_OFFSET) &&
 		    __pa(PAGE_OFFSET) < end)
 			start = __pa(PAGE_OFFSET);
Signature · 1 line
2.43.0
#4 lore raw

PATCH v2 4/8 riscv: remove unreachable invalid range check in kasan_init()

In reply to Sang-Heon Jeon

kasan_init() populates the linear mapping shadow with for_each_mem_range() and breaks the loop when start >= end. for_each_mem_range() never returns an invalid range, so start < end always.

Therefore the start >= end check is unreachable, so remove it.

No functional change.

---
 arch/riscv/mm/kasan_init.c | 3 ---
 1 file changed, 3 deletions(-)
arch/riscv/mm/kasan_init.c +0 -3
index c4a2a9e5586e..1f3aa9611187 100644
@@ -512,9 +512,6 @@ void __init kasan_init(void)
 		void *start = (void *)__va(p_start);
 		void *end = (void *)__va(p_end);
 
-		if (start >= end)
-			break;
-
 		kasan_populate(kasan_mem_to_shadow(start), kasan_mem_to_shadow(end));
 	}
 
Signature · 1 line
2.43.0
#5 lore raw

PATCH v2 5/8 ARM: remove unreachable invalid range check in kasan_init()

In reply to Sang-Heon Jeon

kasan_init() maps each memblock region with for_each_mem_range(), which guarantees pa_start < pa_end. Then it skips any region with pa_start >= arm_lowmem_limit, so pa_start < arm_lowmem_limit is guaranteed as well.

When pa_end <= arm_lowmem_limit, pa_start < pa_end means start < end, so the start >= end check is unreachable.

When pa_end > arm_lowmem_limit, end is clamped to __va(arm_lowmem_limit), and pa_start < arm_lowmem_limit means start < end, so the check is unreachable as well.

No functional change.

---
 arch/arm/mm/kasan_init.c | 6 ------
 1 file changed, 6 deletions(-)
arch/arm/mm/kasan_init.c +0 -6
index c6625e808bf8..1f7c74c5df9e 100644
@@ -262,12 +262,6 @@ void __init kasan_init(void)
 				&pa_start, &pa_end, &arm_lowmem_limit);
 			end = __va(arm_lowmem_limit);
 		}
-		if (start >= end) {
-			pr_info("Skipping invalid memory block %pa-%pa (virtual %p-%p)\n",
-				&pa_start, &pa_end, start, end);
-			continue;
-		}
-
 		create_mapping(start, end);
 	}
 
Signature · 1 line
2.43.0
#6 lore raw

PATCH v2 6/8 powerpc64/kasan: Remove unreachable invalid range check in kasan_init_phys_region()

In reply to Sang-Heon Jeon

kasan_init() maps each memblock region with for_each_mem_range() and passes it to kasan_init_phys_region(), which does nothing when start >= end. for_each_mem_range() never returns an invalid range, so start < end always.

Therefore the start >= end check is unreachable, so remove it.

No functional change.

---
 arch/powerpc/mm/kasan/init_book3e_64.c | 3 ---
 arch/powerpc/mm/kasan/init_book3s_64.c | 3 ---
 2 files changed, 6 deletions(-)
arch/powerpc/mm/kasan/init_book3e_64.c +0 -3
index 0d3a73d6d4b0..0ed372fb8b09 100644
@@ -68,9 +68,6 @@ static void __init kasan_init_phys_region(void *start, void *end)
 	unsigned long k_start, k_end, k_cur;
 	void *va;
 
-	if (start >= end)
-		return;
-
 	k_start = ALIGN_DOWN((unsigned long)kasan_mem_to_shadow(start), PAGE_SIZE);
 	k_end = ALIGN((unsigned long)kasan_mem_to_shadow(end), PAGE_SIZE);
 
arch/powerpc/mm/kasan/init_book3s_64.c +0 -3
index dcafa641804c..ccfbfb894637 100644
@@ -24,9 +24,6 @@ static void __init kasan_init_phys_region(void *start, void *end)
 	unsigned long k_start, k_end, k_cur;
 	void *va;
 
-	if (start >= end)
-		return;
-
 	k_start = ALIGN_DOWN((unsigned long)kasan_mem_to_shadow(start), PAGE_SIZE);
 	k_end = ALIGN((unsigned long)kasan_mem_to_shadow(end), PAGE_SIZE);
 
Signature · 1 line
2.43.0
#7 lore raw

PATCH v2 7/8 mm: remove unnecessary empty range check in early_calculate_totalpages()

In reply to Sang-Heon Jeon

early_calculate_totalpages() iterates the memory ranges with for_each_mem_pfn_range() and calls node_set_state(nid, N_MEMORY) only when end_pfn - start_pfn is non-zero. for_each_mem_pfn_range() never returns an empty range, so start_pfn < end_pfn always.

Therefore the check is unnecessary, 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 0f64909e8d20..17498a13eb99 100644
@@ -332,8 +332,7 @@ static unsigned long __init early_calculate_totalpages(void)
 		unsigned long pages = end_pfn - start_pfn;
 
 		totalpages += pages;
-		if (pages)
-			node_set_state(nid, N_MEMORY);
+		node_set_state(nid, N_MEMORY);
 	}
 	return totalpages;
 }
Signature · 1 line
2.43.0
#8 lore raw

PATCH v2 8/8 mm/hugetlb: remove unnecessary empty range check in hugetlb_bootmem_set_nodes()

In reply to Sang-Heon Jeon

hugetlb_bootmem_set_nodes() iterates the memory ranges with for_each_mem_pfn_range() and calls node_set(nid, hugetlb_bootmem_nodes) only when end_pfn > start_pfn. for_each_mem_pfn_range() never returns an empty range, so start_pfn < end_pfn always.

Therefore the check is unnecessary, so remove it.

No functional change.

---
 mm/hugetlb.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
mm/hugetlb.c +1 -2
index 571212b80835..d4d4c9df5f51 100644
@@ -4450,8 +4450,7 @@ void __init hugetlb_bootmem_set_nodes(void)
 		return;
 
 	for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) {
-		if (end_pfn > start_pfn)
-			node_set(nid, hugetlb_bootmem_nodes);
+		node_set(nid, hugetlb_bootmem_nodes);
 	}
 }
 
Signature · 1 line
2.43.0
maintainer
#9 lore raw

Re: [PATCH v2 8/8] mm/hugetlb: remove unnecessary empty range check in hugetlb_bootmem_set_nodes()

On Tue, 30 Jun 2026 01:37:36 +0900, Sang-Heon Jeon <[email protected]> wrote:

Hi,

mm/hugetlb.c void __init hugetlb_bootmem_set_nodes(void) · line 4453

Quoted 12 lines
mm/hugetlb.c +1 -2
index 571212b80835..d4d4c9df5f51 100644
@@ -4450,8 +4450,7 @@ void __init hugetlb_bootmem_set_nodes(void)
 		return;
 
 	for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) {
-		if (end_pfn > start_pfn)
-			node_set(nid, hugetlb_bootmem_nodes);
+		node_set(nid, hugetlb_bootmem_nodes);

Sashiko says:

Since the check was removed, are start_pfn and end_pfn now unused dead stores?

Looking at hugetlb_bootmem_set_nodes(), it appears these variables are populated by reference in the macro but never read in the loop body anymore. The underlying iterator function __next_mem_pfn_range() supports passing NULL for out_start_pfn and out_end_pfn.

Would it be better to remove the local variables entirely and pass NULL to the iterator instead?

and I agree :)

Signature · 2 lines
Sincerely yours,
Mike.
#10 lore raw

Re: [PATCH v2 8/8] mm/hugetlb: remove unnecessary empty range check in hugetlb_bootmem_set_nodes()

Quoted 28 lines · Mike Rapoport

On Tue, 30 Jun 2026 01:37:36 +0900, Sang-Heon Jeon <[email protected]> wrote:

Hi,

mm/hugetlb.c void __init hugetlb_bootmem_set_nodes(void) · line 4453

Quoted 12 lines
mm/hugetlb.c +1 -2
index 571212b80835..d4d4c9df5f51 100644
@@ -4450,8 +4450,7 @@ void __init hugetlb_bootmem_set_nodes(void)
              return;
 
      for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) {
-             if (end_pfn > start_pfn)
-                     node_set(nid, hugetlb_bootmem_nodes);
+             node_set(nid, hugetlb_bootmem_nodes);

Sashiko says:

Since the check was removed, are start_pfn and end_pfn now unused dead stores?

Looking at hugetlb_bootmem_set_nodes(), it appears these variables are populated by reference in the macro but never read in the loop body anymore. The underlying iterator function __next_mem_pfn_range() supports passing NULL for out_start_pfn and out_end_pfn.

Quoted 4 lines

Would it be better to remove the local variables entirely and pass NULL to the iterator instead?

and I agree :)

Thanks for pointing this out. I agree as well. I'll address it v3.

Quoted 4 lines

--
Sincerely yours,
Mike.

Best Regards,
Sang-Heon Jeon