Skip to contentSkip to conversation

treewide: remove unnecessary invalid range checks in memblock iteration loops

PATCH Applied Sang-Heon Jeon 5 participants 19 messages Jun 21 – Jun 29

Subsystems (7)

v1 of 3 Applied Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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-4 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 5-6 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.

Sang-Heon Jeon (6):
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()
mm: remove unnecessary empty range check in
early_calculate_totalpages()
mm/hugetlb: remove unnecessary empty range check in
hugetlb_bootmem_set_nodes()

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

PATCH 1/6 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
maintainer Reviewed-by on v1 1/6
#2 lore raw

Re: [PATCH 1/6] arm64: mm: remove unreachable invalid range check in kasan_init_shadow()

Quoted 4 lines · Sang-Heon Jeon

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.

Quoted 9 lines

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(-)
Quoted 16 lines
arch/arm64/mm/kasan_init.c +0 -4
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)));
--

2.43.0

#3 lore raw

PATCH 2/6 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
maintainer Reviewed-by on v1 2/6
#4 lore raw

Re: [PATCH 2/6] LoongArch: remove unreachable invalid range check in kasan_init()

Quoted 4 lines · 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.

Quoted 9 lines

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(-)
#5 lore raw

PATCH 3/6 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 · 7 lines
2.43.0


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Reviewed-by Tested-by on v1 3/6
#6 lore raw

Re: [PATCH 3/6] riscv: remove unreachable invalid range check in create_linear_mapping_page_table()

Quoted 9 lines · 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.

Quoted 26 lines
---
 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 · 7 lines
2.43.0


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#7 lore raw

PATCH 4/6 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 · 7 lines
2.43.0


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Reviewed-by Tested-by on v1 4/6
#8 lore raw

Re: [PATCH 4/6] riscv: remove unreachable invalid range check in kasan_init()

Quoted 9 lines · 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.

Quoted 27 lines
---
 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 · 7 lines
2.43.0


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

maintainer Reviewed-by on v1 4/6
#9 lore raw

Re: [PATCH 4/6] riscv: remove unreachable invalid range check in kasan_init()

In reply to Sang-Heon Jeon
Quoted 4 lines · 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.

Quoted 9 lines

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(-)

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#10 lore raw

PATCH 5/6 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
maintainer Reviewed-by on v1 5/6
#11 lore raw

Re: [PATCH 5/6] mm: remove unnecessary empty range check in early_calculate_totalpages()

Quoted 8 lines · 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.

[...]

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

PATCH 6/6 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
reviewer Reviewed-by on v1 6/6
#13 lore raw

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

Quoted 19 lines · 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 +0 -0
index 571212b80835..d4d4c9df5f51 100644

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

Quoted 8 lines
mm/hugetlb.c +1 -2
@@ -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);
 	}
 }

We have

	if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
		continue;

...

*out_start_pfn = PFN_UP(r->base);
*out_end_pfn = PFN_DOWN(r->base + r->size);

So we should always have *out_start_pfn < *out_end_pfn

Signature · 3 lines
Cheers,

David
maintainer Acked-by on the v1 series
#14 lore raw
In reply to Sang-Heon Jeon
Quoted 10 lines · Sang-Heon Jeon

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.

[...]

Signature · 2 lines
Sincerely yours,
Mike.
maintainer Applied
#15 lore raw
In reply to Sang-Heon Jeon
Quoted 7 lines · Sang-Heon Jeon

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.

Quoted 11 lines

Sang-Heon Jeon (6):
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()
mm: remove unnecessary empty range check in
early_calculate_totalpages()
mm/hugetlb: remove unnecessary empty range check in
hugetlb_bootmem_set_nodes()

I queued this for inclusion into memblock tree.

Quoted 11 lines
 arch/arm64/mm/kasan_init.c     | 3 ---
 arch/loongarch/mm/kasan_init.c | 3 ---
 arch/riscv/mm/init.c           | 2 --
 arch/riscv/mm/kasan_init.c     | 3 ---
 mm/hugetlb.c                   | 3 +--
 mm/mm_init.c                   | 3 +--
 6 files changed, 2 insertions(+), 15 deletions(-)
Signature · 1 line
2.43.0
Signature · 2 lines
Sincerely yours,
Mike.
#16 lore raw
Quoted 21 lines · Mike Rapoport
Quoted 7 lines · Sang-Heon Jeon

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.

Quoted 11 lines

Sang-Heon Jeon (6):
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()
mm: remove unnecessary empty range check in
early_calculate_totalpages()
mm/hugetlb: remove unnecessary empty range check in
hugetlb_bootmem_set_nodes()

Quoted 1 line

I queued this for inclusion into memblock tree.

Thank you, Mike.

Could you please review and queue this patch [1] as well? It does the same kind of clean up, I just missed it at the time.

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

Quoted 8 lines
Quoted 8 lines
 arch/arm64/mm/kasan_init.c     | 3 ---
 arch/loongarch/mm/kasan_init.c | 3 ---
 arch/riscv/mm/init.c           | 2 --
 arch/riscv/mm/kasan_init.c     | 3 ---
 mm/hugetlb.c                   | 3 +--
 mm/mm_init.c                   | 3 +--
 6 files changed, 2 insertions(+), 15 deletions(-)
Quoted 7 lines
Quoted 3 lines

--
2.43.0

--
Sincerely yours,
Mike.

Best Regards,
Sang-Heon Jeon

maintainer
#17 lore raw
Quoted 22 lines · Sang-Heon Jeon
Quoted 21 lines · Mike Rapoport
Quoted 18 lines · Sang-Heon Jeon

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.

Sang-Heon Jeon (6):
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()
mm: remove unnecessary empty range check in
early_calculate_totalpages()
mm/hugetlb: remove unnecessary empty range check in
hugetlb_bootmem_set_nodes()

Quoted 6 lines
Quoted 1 line

I queued this for inclusion into memblock tree.

Thank you, Mike.

Could you please review and queue this patch [1] as well? It does the same kind of clean up, I just missed it at the time.

Can you please resend them all as a single set?

Quoted 20 lines

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

Quoted 8 lines
Quoted 8 lines
 arch/arm64/mm/kasan_init.c     | 3 ---
 arch/loongarch/mm/kasan_init.c | 3 ---
 arch/riscv/mm/init.c           | 2 --
 arch/riscv/mm/kasan_init.c     | 3 ---
 mm/hugetlb.c                   | 3 +--
 mm/mm_init.c                   | 3 +--
 6 files changed, 2 insertions(+), 15 deletions(-)
Quoted 7 lines
Quoted 3 lines

--
2.43.0

--
Sincerely yours,
Mike.

Best Regards,
Sang-Heon Jeon

Signature · 2 lines
Sincerely yours,
Mike.
#18 lore raw
Quoted 24 lines · Mike Rapoport
Quoted 22 lines · Sang-Heon Jeon
Quoted 21 lines · Mike Rapoport
Quoted 18 lines · Sang-Heon Jeon

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.

Sang-Heon Jeon (6):
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()
mm: remove unnecessary empty range check in
early_calculate_totalpages()
mm/hugetlb: remove unnecessary empty range check in
hugetlb_bootmem_set_nodes()

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

I queued this for inclusion into memblock tree.

Thank you, Mike.

Could you please review and queue this patch [1] as well? It does the same kind of clean up, I just missed it at the time.

Can you please resend them all as a single set?

Thanks for considering, Mike.
I've sent v2 patch series [1] that includes the missing patches.

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

Quoted 18 lines
Quoted 18 lines

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

Quoted 15 lines
Quoted 11 lines
 arch/arm64/mm/kasan_init.c     | 3 ---
 arch/loongarch/mm/kasan_init.c | 3 ---
 arch/riscv/mm/init.c           | 2 --
 arch/riscv/mm/kasan_init.c     | 3 ---
 mm/hugetlb.c                   | 3 +--
 mm/mm_init.c                   | 3 +--
 6 files changed, 2 insertions(+), 15 deletions(-)

--
2.43.0

--
Sincerely yours,
Mike.

Quoted 6 lines
Quoted 2 lines

Best Regards,
Sang-Heon Jeon

--
Sincerely yours,
Mike.

Best Regards,
Sang-Heon Jeon