treewide: remove unnecessary invalid range checks in memblock iteration loops
Subsystems (7)
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
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
@@ -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
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 withfor_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.
Signed-off-by Sang-Heon Jeon <[email protected]>--- arch/arm64/mm/kasan_init.c | 3 --- 1 file changed, 3 deletions(-)
Quoted 16 lines
arch/arm64/mm/kasan_init.c +0
-4@@ -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
PATCH 2/6 LoongArch: remove unreachable invalid range check in kasan_init()
In reply to Sang-Heon Jeonkasan_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
@@ -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
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 withfor_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.
Signed-off-by Sang-Heon Jeon <[email protected]>--- arch/loongarch/mm/kasan_init.c | 3 --- 1 file changed, 3 deletions(-)
PATCH 3/6 riscv: remove unreachable invalid range check in create_linear_mapping_page_table()
In reply to Sang-Heon Jeoncreate_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
@@ -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
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 withfor_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.
Signed-off-by Sang-Heon Jeon <[email protected]>
Quoted 26 lines
--- arch/riscv/mm/init.c | 2 -- 1 file changed, 2 deletions(-)arch/riscv/mm/init.c +0
-2@@ -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
PATCH 4/6 riscv: remove unreachable invalid range check in kasan_init()
In reply to Sang-Heon Jeonkasan_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
@@ -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
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 withfor_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.
Signed-off-by Sang-Heon Jeon <[email protected]>
Quoted 27 lines
--- arch/riscv/mm/kasan_init.c | 3 --- 1 file changed, 3 deletions(-)arch/riscv/mm/kasan_init.c +0
-3@@ -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
Re: [PATCH 4/6] riscv: remove unreachable invalid range check in kasan_init()
In reply to Sang-Heon JeonQuoted 4 lines · Sang-Heon Jeon
kasan_init()populates the linear mapping shadow withfor_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.
Signed-off-by Sang-Heon Jeon <[email protected]>--- 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
PATCH 5/6 mm: remove unnecessary empty range check in early_calculate_totalpages()
In reply to Sang-Heon Jeonearly_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
@@ -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
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 withfor_each_mem_pfn_range()and callsnode_set_state(nid, N_MEMORY)only whenend_pfn-start_pfnis non-zero.for_each_mem_pfn_range()never returns an empty range, sostart_pfn<end_pfnalways.Therefore the check is unnecessary, so remove it.
[...]
Signature · 2 lines
Sincerely yours, Mike.
PATCH 6/6 mm/hugetlb: remove unnecessary empty range check in hugetlb_bootmem_set_nodes()
In reply to Sang-Heon Jeonhugetlb_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
@@ -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
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 withfor_each_mem_pfn_range()and callsnode_set(nid, hugetlb_bootmem_nodes)only whenend_pfn>start_pfn.for_each_mem_pfn_range()never returns an empty range, sostart_pfn<end_pfnalways.Therefore the check is unnecessary, so remove it.
No functional change.
Signed-off-by Sang-Heon Jeon <[email protected]>--- mm/hugetlb.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)mm/hugetlb.c +0
-0
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
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()andfor_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.
Quoted 7 lines · Sang-Heon Jeon
The memblock API guarantees that
for_each_mem_range()andfor_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 inkasan_init()
riscv: remove unreachable invalid range check in
create_linear_mapping_page_table()
riscv: remove unreachable invalid range check inkasan_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.
Quoted 21 lines · Mike Rapoport
Quoted 7 lines · Sang-Heon Jeon
The memblock API guarantees that
for_each_mem_range()andfor_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 inkasan_init()
riscv: remove unreachable invalid range check in
create_linear_mapping_page_table()
riscv: remove unreachable invalid range check inkasan_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
Mike Rapoport
maintainerQuoted 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()andfor_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 inkasan_init()
riscv: remove unreachable invalid range check in
create_linear_mapping_page_table()
riscv: remove unreachable invalid range check inkasan_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.
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()andfor_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 inkasan_init()
riscv: remove unreachable invalid range check in
create_linear_mapping_page_table()
riscv: remove unreachable invalid range check inkasan_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