Skip to content

Commit b10d6bc

Browse files
rppttorvalds
authored andcommitted
arch, drivers: replace for_each_membock() with for_each_mem_range()
There are several occurrences of the following pattern: for_each_memblock(memory, reg) { start = __pfn_to_phys(memblock_region_memory_base_pfn(reg); end = __pfn_to_phys(memblock_region_memory_end_pfn(reg)); /* do something with start and end */ } Using for_each_mem_range() iterator is more appropriate in such cases and allows simpler and cleaner code. [[email protected]: fix arch/arm/mm/pmsa-v7.c build] [[email protected]: mips: fix cavium-octeon build caused by memblock refactoring] Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Mike Rapoport <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Cc: Andy Lutomirski <[email protected]> Cc: Baoquan He <[email protected]> Cc: Benjamin Herrenschmidt <[email protected]> Cc: Borislav Petkov <[email protected]> Cc: Catalin Marinas <[email protected]> Cc: Christoph Hellwig <[email protected]> Cc: Daniel Axtens <[email protected]> Cc: Dave Hansen <[email protected]> Cc: Emil Renner Berthing <[email protected]> Cc: Hari Bathini <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Jonathan Cameron <[email protected]> Cc: Marek Szyprowski <[email protected]> Cc: Max Filippov <[email protected]> Cc: Michael Ellerman <[email protected]> Cc: Michal Simek <[email protected]> Cc: Miguel Ojeda <[email protected]> Cc: Palmer Dabbelt <[email protected]> Cc: Paul Mackerras <[email protected]> Cc: Paul Walmsley <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Russell King <[email protected]> Cc: Stafford Horne <[email protected]> Cc: Thomas Bogendoerfer <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Will Deacon <[email protected]> Cc: Yoshinori Sato <[email protected]> Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Linus Torvalds <[email protected]>
1 parent c9118e6 commit b10d6bc

File tree

25 files changed

+195
-208
lines changed

25 files changed

+195
-208
lines changed

arch/arm/kernel/setup.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -843,19 +843,25 @@ early_param("mem", early_mem);
843843

844844
static void __init request_standard_resources(const struct machine_desc *mdesc)
845845
{
846-
struct memblock_region *region;
846+
phys_addr_t start, end, res_end;
847847
struct resource *res;
848+
u64 i;
848849

849850
kernel_code.start = virt_to_phys(_text);
850851
kernel_code.end = virt_to_phys(__init_begin - 1);
851852
kernel_data.start = virt_to_phys(_sdata);
852853
kernel_data.end = virt_to_phys(_end - 1);
853854

854-
for_each_memblock(memory, region) {
855-
phys_addr_t start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
856-
phys_addr_t end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
855+
for_each_mem_range(i, &start, &end) {
857856
unsigned long boot_alias_start;
858857

858+
/*
859+
* In memblock, end points to the first byte after the
860+
* range while in resourses, end points to the last byte in
861+
* the range.
862+
*/
863+
res_end = end - 1;
864+
859865
/*
860866
* Some systems have a special memory alias which is only
861867
* used for booting. We need to advertise this region to
@@ -869,7 +875,7 @@ static void __init request_standard_resources(const struct machine_desc *mdesc)
869875
__func__, sizeof(*res));
870876
res->name = "System RAM (boot alias)";
871877
res->start = boot_alias_start;
872-
res->end = phys_to_idmap(end);
878+
res->end = phys_to_idmap(res_end);
873879
res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
874880
request_resource(&iomem_resource, res);
875881
}
@@ -880,7 +886,7 @@ static void __init request_standard_resources(const struct machine_desc *mdesc)
880886
sizeof(*res));
881887
res->name = "System RAM";
882888
res->start = start;
883-
res->end = end;
889+
res->end = res_end;
884890
res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
885891

886892
request_resource(&iomem_resource, res);

arch/arm/mm/mmu.c

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,9 +1154,8 @@ phys_addr_t arm_lowmem_limit __initdata = 0;
11541154

11551155
void __init adjust_lowmem_bounds(void)
11561156
{
1157-
phys_addr_t memblock_limit = 0;
1158-
u64 vmalloc_limit;
1159-
struct memblock_region *reg;
1157+
phys_addr_t block_start, block_end, memblock_limit = 0;
1158+
u64 vmalloc_limit, i;
11601159
phys_addr_t lowmem_limit = 0;
11611160

11621161
/*
@@ -1172,26 +1171,18 @@ void __init adjust_lowmem_bounds(void)
11721171
* The first usable region must be PMD aligned. Mark its start
11731172
* as MEMBLOCK_NOMAP if it isn't
11741173
*/
1175-
for_each_memblock(memory, reg) {
1176-
if (!memblock_is_nomap(reg)) {
1177-
if (!IS_ALIGNED(reg->base, PMD_SIZE)) {
1178-
phys_addr_t len;
1174+
for_each_mem_range(i, &block_start, &block_end) {
1175+
if (!IS_ALIGNED(block_start, PMD_SIZE)) {
1176+
phys_addr_t len;
11791177

1180-
len = round_up(reg->base, PMD_SIZE) - reg->base;
1181-
memblock_mark_nomap(reg->base, len);
1182-
}
1183-
break;
1178+
len = round_up(block_start, PMD_SIZE) - block_start;
1179+
memblock_mark_nomap(block_start, len);
11841180
}
1181+
break;
11851182
}
11861183

1187-
for_each_memblock(memory, reg) {
1188-
phys_addr_t block_start = reg->base;
1189-
phys_addr_t block_end = reg->base + reg->size;
1190-
1191-
if (memblock_is_nomap(reg))
1192-
continue;
1193-
1194-
if (reg->base < vmalloc_limit) {
1184+
for_each_mem_range(i, &block_start, &block_end) {
1185+
if (block_start < vmalloc_limit) {
11951186
if (block_end > lowmem_limit)
11961187
/*
11971188
* Compare as u64 to ensure vmalloc_limit does
@@ -1440,19 +1431,15 @@ static void __init kmap_init(void)
14401431

14411432
static void __init map_lowmem(void)
14421433
{
1443-
struct memblock_region *reg;
14441434
phys_addr_t kernel_x_start = round_down(__pa(KERNEL_START), SECTION_SIZE);
14451435
phys_addr_t kernel_x_end = round_up(__pa(__init_end), SECTION_SIZE);
1436+
phys_addr_t start, end;
1437+
u64 i;
14461438

14471439
/* Map all the lowmem memory banks. */
1448-
for_each_memblock(memory, reg) {
1449-
phys_addr_t start = reg->base;
1450-
phys_addr_t end = start + reg->size;
1440+
for_each_mem_range(i, &start, &end) {
14511441
struct map_desc map;
14521442

1453-
if (memblock_is_nomap(reg))
1454-
continue;
1455-
14561443
if (end > arm_lowmem_limit)
14571444
end = arm_lowmem_limit;
14581445
if (start >= end)

arch/arm/mm/pmsa-v7.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,12 @@ static int __init allocate_region(phys_addr_t base, phys_addr_t size,
231231
void __init pmsav7_adjust_lowmem_bounds(void)
232232
{
233233
phys_addr_t specified_mem_size = 0, total_mem_size = 0;
234-
struct memblock_region *reg;
235-
bool first = true;
236234
phys_addr_t mem_start;
237235
phys_addr_t mem_end;
236+
phys_addr_t reg_start, reg_end;
238237
unsigned int mem_max_regions;
239-
int num, i;
238+
int num;
239+
u64 i;
240240

241241
/* Free-up PMSAv7_PROBE_REGION */
242242
mpu_min_region_order = __mpu_min_region_order();
@@ -262,29 +262,28 @@ void __init pmsav7_adjust_lowmem_bounds(void)
262262
mem_max_regions -= num;
263263
#endif
264264

265-
for_each_memblock(memory, reg) {
266-
if (first) {
265+
for_each_mem_range(i, &reg_start, &reg_end) {
266+
if (i == 0) {
267267
phys_addr_t phys_offset = PHYS_OFFSET;
268268

269269
/*
270270
* Initially only use memory continuous from
271271
* PHYS_OFFSET */
272-
if (reg->base != phys_offset)
272+
if (reg_start != phys_offset)
273273
panic("First memory bank must be contiguous from PHYS_OFFSET");
274274

275-
mem_start = reg->base;
276-
mem_end = reg->base + reg->size;
277-
specified_mem_size = reg->size;
278-
first = false;
275+
mem_start = reg_start;
276+
mem_end = reg_end;
277+
specified_mem_size = mem_end - mem_start;
279278
} else {
280279
/*
281280
* memblock auto merges contiguous blocks, remove
282281
* all blocks afterwards in one go (we can't remove
283282
* blocks separately while iterating)
284283
*/
285284
pr_notice("Ignoring RAM after %pa, memory at %pa ignored\n",
286-
&mem_end, &reg->base);
287-
memblock_remove(reg->base, 0 - reg->base);
285+
&mem_end, &reg_start);
286+
memblock_remove(reg_start, 0 - reg_start);
288287
break;
289288
}
290289
}

arch/arm/mm/pmsa-v8.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,29 +94,28 @@ static __init bool is_region_fixed(int number)
9494
void __init pmsav8_adjust_lowmem_bounds(void)
9595
{
9696
phys_addr_t mem_end;
97-
struct memblock_region *reg;
98-
bool first = true;
97+
phys_addr_t reg_start, reg_end;
98+
u64 i;
9999

100-
for_each_memblock(memory, reg) {
101-
if (first) {
100+
for_each_mem_range(i, &reg_start, &reg_end) {
101+
if (i == 0) {
102102
phys_addr_t phys_offset = PHYS_OFFSET;
103103

104104
/*
105105
* Initially only use memory continuous from
106106
* PHYS_OFFSET */
107-
if (reg->base != phys_offset)
107+
if (reg_start != phys_offset)
108108
panic("First memory bank must be contiguous from PHYS_OFFSET");
109-
mem_end = reg->base + reg->size;
110-
first = false;
109+
mem_end = reg_end;
111110
} else {
112111
/*
113112
* memblock auto merges contiguous blocks, remove
114113
* all blocks afterwards in one go (we can't remove
115114
* blocks separately while iterating)
116115
*/
117116
pr_notice("Ignoring RAM after %pa, memory at %pa ignored\n",
118-
&mem_end, &reg->base);
119-
memblock_remove(reg->base, 0 - reg->base);
117+
&mem_end, &reg_start);
118+
memblock_remove(reg_start, 0 - reg_start);
120119
break;
121120
}
122121
}

arch/arm/xen/mm.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@
2525

2626
unsigned long xen_get_swiotlb_free_pages(unsigned int order)
2727
{
28-
struct memblock_region *reg;
28+
phys_addr_t base;
2929
gfp_t flags = __GFP_NOWARN|__GFP_KSWAPD_RECLAIM;
30+
u64 i;
3031

31-
for_each_memblock(memory, reg) {
32-
if (reg->base < (phys_addr_t)0xffffffff) {
32+
for_each_mem_range(i, &base, NULL) {
33+
if (base < (phys_addr_t)0xffffffff) {
3334
if (IS_ENABLED(CONFIG_ZONE_DMA32))
3435
flags |= __GFP_DMA32;
3536
else

arch/arm64/mm/kasan_init.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@ void __init kasan_init(void)
212212
{
213213
u64 kimg_shadow_start, kimg_shadow_end;
214214
u64 mod_shadow_start, mod_shadow_end;
215-
struct memblock_region *reg;
216-
int i;
215+
phys_addr_t pa_start, pa_end;
216+
u64 i;
217217

218218
kimg_shadow_start = (u64)kasan_mem_to_shadow(_text) & PAGE_MASK;
219219
kimg_shadow_end = PAGE_ALIGN((u64)kasan_mem_to_shadow(_end));
@@ -246,9 +246,9 @@ void __init kasan_init(void)
246246
kasan_populate_early_shadow((void *)mod_shadow_end,
247247
(void *)kimg_shadow_start);
248248

249-
for_each_memblock(memory, reg) {
250-
void *start = (void *)__phys_to_virt(reg->base);
251-
void *end = (void *)__phys_to_virt(reg->base + reg->size);
249+
for_each_mem_range(i, &pa_start, &pa_end) {
250+
void *start = (void *)__phys_to_virt(pa_start);
251+
void *end = (void *)__phys_to_virt(pa_end);
252252

253253
if (start >= end)
254254
break;

arch/arm64/mm/mmu.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,9 @@ static void __init map_mem(pgd_t *pgdp)
473473
{
474474
phys_addr_t kernel_start = __pa_symbol(_text);
475475
phys_addr_t kernel_end = __pa_symbol(__init_begin);
476-
struct memblock_region *reg;
476+
phys_addr_t start, end;
477477
int flags = 0;
478+
u64 i;
478479

479480
if (rodata_full || debug_pagealloc_enabled())
480481
flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
@@ -493,15 +494,9 @@ static void __init map_mem(pgd_t *pgdp)
493494
#endif
494495

495496
/* map all the memory banks */
496-
for_each_memblock(memory, reg) {
497-
phys_addr_t start = reg->base;
498-
phys_addr_t end = start + reg->size;
499-
497+
for_each_mem_range(i, &start, &end) {
500498
if (start >= end)
501499
break;
502-
if (memblock_is_nomap(reg))
503-
continue;
504-
505500
/*
506501
* The linear map must allow allocation tags reading/writing
507502
* if MTE is present. Otherwise, it has the same attributes as

arch/c6x/kernel/setup.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,8 @@ notrace void __init machine_init(unsigned long dt_ptr)
287287

288288
void __init setup_arch(char **cmdline_p)
289289
{
290-
struct memblock_region *reg;
290+
phys_addr_t start, end;
291+
u64 i;
291292

292293
printk(KERN_INFO "Initializing kernel\n");
293294

@@ -351,9 +352,9 @@ void __init setup_arch(char **cmdline_p)
351352
disable_caching(ram_start, ram_end - 1);
352353

353354
/* Set caching of external RAM used by Linux */
354-
for_each_memblock(memory, reg)
355-
enable_caching(CACHE_REGION_START(reg->base),
356-
CACHE_REGION_START(reg->base + reg->size - 1));
355+
for_each_mem_range(i, &start, &end)
356+
enable_caching(CACHE_REGION_START(start),
357+
CACHE_REGION_START(end - 1));
357358

358359
#ifdef CONFIG_BLK_DEV_INITRD
359360
/*

arch/microblaze/mm/init.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,14 @@ static void __init paging_init(void)
109109
void __init setup_memory(void)
110110
{
111111
#ifndef CONFIG_MMU
112-
struct memblock_region *reg;
113112
u32 kernel_align_start, kernel_align_size;
113+
phys_addr_t start, end;
114+
u64 i;
114115

115116
/* Find main memory where is the kernel */
116-
for_each_memblock(memory, reg) {
117-
memory_start = (u32)reg->base;
118-
lowmem_size = reg->size;
117+
for_each_mem_range(i, &start, &end) {
118+
memory_start = start;
119+
lowmem_size = end - start;
119120
if ((memory_start <= (u32)_text) &&
120121
((u32)_text <= (memory_start + lowmem_size - 1))) {
121122
memory_size = lowmem_size;

arch/mips/cavium-octeon/dma-octeon.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,25 +190,25 @@ char *octeon_swiotlb;
190190

191191
void __init plat_swiotlb_setup(void)
192192
{
193-
struct memblock_region *mem;
193+
phys_addr_t start, end;
194194
phys_addr_t max_addr;
195195
phys_addr_t addr_size;
196196
size_t swiotlbsize;
197197
unsigned long swiotlb_nslabs;
198+
u64 i;
198199

199200
max_addr = 0;
200201
addr_size = 0;
201202

202-
for_each_memblock(memory, mem) {
203+
for_each_mem_range(i, &start, &end) {
203204
/* These addresses map low for PCI. */
204-
if (mem->base > 0x410000000ull && !OCTEON_IS_OCTEON2())
205+
if (start > 0x410000000ull && !OCTEON_IS_OCTEON2())
205206
continue;
206207

207-
addr_size += mem->size;
208-
209-
if (max_addr < mem->base + mem->size)
210-
max_addr = mem->base + mem->size;
208+
addr_size += (end - start);
211209

210+
if (max_addr < end)
211+
max_addr = end;
212212
}
213213

214214
swiotlbsize = PAGE_SIZE;

0 commit comments

Comments
 (0)