treewide: remove unnecessary invalid range checks in memblock iteration loops
Subsystems (9)
v3 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
Changes from v2 [2]
- Remove unused local variable and braces
[1] https://lore.kernel.org/all/[email protected]/
[2] 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 | 7 ++----- mm/mm_init.c | 3 +-- 9 files changed, 3 insertions(+), 30 deletions(-)
Signature · 1 line
2.43.0
PATCH v3 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
@@ -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
PATCH v3 2/8 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
PATCH v3 3/8 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 · 1 line
2.43.0
PATCH v3 4/8 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 · 1 line
2.43.0
PATCH v3 5/8 ARM: remove unreachable invalid range check in kasan_init()
In reply to Sang-Heon Jeonkasan_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
@@ -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
PATCH v3 6/8 powerpc64/kasan: Remove unreachable invalid range check in kasan_init_phys_region()
In reply to Sang-Heon Jeonkasan_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
@@ -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
@@ -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
PATCH v3 7/8 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
PATCH v3 8/8 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. start_pfn and end_pfn are no longer used, so remove the local variables.
No functional change.
--- mm/hugetlb.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
mm/hugetlb.c +2 -5
@@ -4444,15 +4444,12 @@ hugetlb_early_param("default_hugepagesz", default_hugepagesz_setup);void __init hugetlb_bootmem_set_nodes (void ) {int i, nid; -unsigned long start_pfn, end_pfn;if (!nodes_empty (hugetlb_bootmem_nodes))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); - } +for_each_mem_pfn_range (i,MAX_NUMNODES ,NULL ,NULL , &nid) +node_set (nid, hugetlb_bootmem_nodes); }void __init hugetlb_bootmem_alloc (void )
Signature · 1 line
2.43.0
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.
[...]
Applied to range-checks branch of memblock.git tree, thanks!
[1/8] arm64: mm: remove unreachable invalid range check in kasan_init_shadow()
commit: f6e6c57f2e100813be6d5882060fef81cb6f32de
[2/8] LoongArch: remove unreachable invalid range check in kasan_init()
commit: 68d3b7d58237b8794d9343210d4c90b8381cfa96
[3/8] riscv: remove unreachable invalid range check in create_linear_mapping_page_table()
commit: 99e60ebe108817fd48791f2cb81f1e672ad8285b
[4/8] riscv: remove unreachable invalid range check in kasan_init()
commit: 8008f6995d0c0cf22e7b4d60e95fe14085c10f35
[5/8] ARM: remove unreachable invalid range check in kasan_init()
commit: 3db1a9ccf9290ea3fe01aadf3bd743ef7032b24a
[6/8] powerpc64/kasan: Remove unreachable invalid range check in kasan_init_phys_region()
commit: 21e95a4b6f34770571eeeaa6adb535efba1b7281
[7/8] mm: remove unnecessary empty range check in early_calculate_totalpages()
commit: c6af91e48594191f62ad3c1fcc8269b1eb539ef8
[8/8] mm/hugetlb: remove unnecessary empty range check in hugetlb_bootmem_set_nodes()
commit: 2db44dc8bdd3cfc933a3193724f4d38b03e35b80tree: https://git.kernel.org/pub/scm/linux/kernel/git/rppt/memblock
branch: range-checks
--
Sincerely yours,
Mike.