Skip to content

Commit c9118e6

Browse files
rppttorvalds
authored andcommitted
arch, mm: replace for_each_memblock() with for_each_mem_pfn_range()
There are several occurrences of the following pattern: for_each_memblock(memory, reg) { start_pfn = memblock_region_memory_base_pfn(reg); end_pfn = memblock_region_memory_end_pfn(reg); /* do something with start_pfn and end_pfn */ } Rather than iterate over all memblock.memory regions and each time query for their start and end PFNs, use for_each_mem_pfn_range() iterator to get simpler and clearer code. Signed-off-by: Mike Rapoport <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Reviewed-by: Baoquan He <[email protected]> Acked-by: Miguel Ojeda <[email protected]> [.clang-format] Cc: Andy Lutomirski <[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: 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 6e245ad commit c9118e6

File tree

9 files changed

+35
-51
lines changed

9 files changed

+35
-51
lines changed

arch/arm/mm/init.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -299,16 +299,14 @@ free_memmap(unsigned long start_pfn, unsigned long end_pfn)
299299
*/
300300
static void __init free_unused_memmap(void)
301301
{
302-
unsigned long start, prev_end = 0;
303-
struct memblock_region *reg;
302+
unsigned long start, end, prev_end = 0;
303+
int i;
304304

305305
/*
306306
* This relies on each bank being in address order.
307307
* The banks are sorted previously in bootmem_init().
308308
*/
309-
for_each_memblock(memory, reg) {
310-
start = memblock_region_memory_base_pfn(reg);
311-
309+
for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, NULL) {
312310
#ifdef CONFIG_SPARSEMEM
313311
/*
314312
* Take care not to free memmap entries that don't exist
@@ -336,8 +334,7 @@ static void __init free_unused_memmap(void)
336334
* memmap entries are valid from the bank end aligned to
337335
* MAX_ORDER_NR_PAGES.
338336
*/
339-
prev_end = ALIGN(memblock_region_memory_end_pfn(reg),
340-
MAX_ORDER_NR_PAGES);
337+
prev_end = ALIGN(end, MAX_ORDER_NR_PAGES);
341338
}
342339

343340
#ifdef CONFIG_SPARSEMEM

arch/arm64/mm/init.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -471,12 +471,10 @@ static inline void free_memmap(unsigned long start_pfn, unsigned long end_pfn)
471471
*/
472472
static void __init free_unused_memmap(void)
473473
{
474-
unsigned long start, prev_end = 0;
475-
struct memblock_region *reg;
476-
477-
for_each_memblock(memory, reg) {
478-
start = __phys_to_pfn(reg->base);
474+
unsigned long start, end, prev_end = 0;
475+
int i;
479476

477+
for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, NULL) {
480478
#ifdef CONFIG_SPARSEMEM
481479
/*
482480
* Take care not to free memmap entries that don't exist due
@@ -496,8 +494,7 @@ static void __init free_unused_memmap(void)
496494
* memmap entries are valid from the bank end aligned to
497495
* MAX_ORDER_NR_PAGES.
498496
*/
499-
prev_end = ALIGN(__phys_to_pfn(reg->base + reg->size),
500-
MAX_ORDER_NR_PAGES);
497+
prev_end = ALIGN(end, MAX_ORDER_NR_PAGES);
501498
}
502499

503500
#ifdef CONFIG_SPARSEMEM

arch/powerpc/kernel/fadump.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,14 +1242,15 @@ static void fadump_free_reserved_memory(unsigned long start_pfn,
12421242
*/
12431243
static void fadump_release_reserved_area(u64 start, u64 end)
12441244
{
1245-
u64 tstart, tend, spfn, epfn;
1246-
struct memblock_region *reg;
1245+
u64 tstart, tend, spfn, epfn, reg_spfn, reg_epfn, i;
12471246

12481247
spfn = PHYS_PFN(start);
12491248
epfn = PHYS_PFN(end);
1250-
for_each_memblock(memory, reg) {
1251-
tstart = max_t(u64, spfn, memblock_region_memory_base_pfn(reg));
1252-
tend = min_t(u64, epfn, memblock_region_memory_end_pfn(reg));
1249+
1250+
for_each_mem_pfn_range(i, MAX_NUMNODES, &reg_spfn, &reg_epfn, NULL) {
1251+
tstart = max_t(u64, spfn, reg_spfn);
1252+
tend = min_t(u64, epfn, reg_epfn);
1253+
12531254
if (tstart < tend) {
12541255
fadump_free_reserved_memory(tstart, tend);
12551256

arch/powerpc/mm/mem.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,16 @@ void __init initmem_init(void)
184184
/* mark pages that don't exist as nosave */
185185
static int __init mark_nonram_nosave(void)
186186
{
187-
struct memblock_region *reg, *prev = NULL;
187+
unsigned long spfn, epfn, prev = 0;
188+
int i;
188189

189-
for_each_memblock(memory, reg) {
190-
if (prev &&
191-
memblock_region_memory_end_pfn(prev) < memblock_region_memory_base_pfn(reg))
192-
register_nosave_region(memblock_region_memory_end_pfn(prev),
193-
memblock_region_memory_base_pfn(reg));
194-
prev = reg;
190+
for_each_mem_pfn_range(i, MAX_NUMNODES, &spfn, &epfn, NULL) {
191+
if (prev && prev < spfn)
192+
register_nosave_region(prev, spfn);
193+
194+
prev = epfn;
195195
}
196+
196197
return 0;
197198
}
198199
#else /* CONFIG_NEED_MULTIPLE_NODES */

arch/powerpc/mm/numa.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -804,17 +804,14 @@ static void __init setup_nonnuma(void)
804804
unsigned long total_ram = memblock_phys_mem_size();
805805
unsigned long start_pfn, end_pfn;
806806
unsigned int nid = 0;
807-
struct memblock_region *reg;
807+
int i;
808808

809809
printk(KERN_DEBUG "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
810810
top_of_ram, total_ram);
811811
printk(KERN_DEBUG "Memory hole size: %ldMB\n",
812812
(top_of_ram - total_ram) >> 20);
813813

814-
for_each_memblock(memory, reg) {
815-
start_pfn = memblock_region_memory_base_pfn(reg);
816-
end_pfn = memblock_region_memory_end_pfn(reg);
817-
814+
for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, NULL) {
818815
fake_numa_create_new_node(end_pfn, &nid);
819816
memblock_set_node(PFN_PHYS(start_pfn),
820817
PFN_PHYS(end_pfn - start_pfn),

arch/s390/mm/page-states.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,19 +183,17 @@ static void mark_kernel_pgd(void)
183183

184184
void __init cmma_init_nodat(void)
185185
{
186-
struct memblock_region *reg;
187186
struct page *page;
188187
unsigned long start, end, ix;
188+
int i;
189189

190190
if (cmma_flag < 2)
191191
return;
192192
/* Mark pages used in kernel page tables */
193193
mark_kernel_pgd();
194194

195195
/* Set all kernel pages not used for page tables to stable/no-dat */
196-
for_each_memblock(memory, reg) {
197-
start = memblock_region_memory_base_pfn(reg);
198-
end = memblock_region_memory_end_pfn(reg);
196+
for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, NULL) {
199197
page = pfn_to_page(start);
200198
for (ix = start; ix < end; ix++, page++) {
201199
if (__test_and_clear_bit(PG_arch_1, &page->flags))

arch/sh/mm/init.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,15 +226,12 @@ void __init allocate_pgdat(unsigned int nid)
226226

227227
static void __init do_init_bootmem(void)
228228
{
229-
struct memblock_region *reg;
229+
unsigned long start_pfn, end_pfn;
230+
int i;
230231

231232
/* Add active regions with valid PFNs. */
232-
for_each_memblock(memory, reg) {
233-
unsigned long start_pfn, end_pfn;
234-
start_pfn = memblock_region_memory_base_pfn(reg);
235-
end_pfn = memblock_region_memory_end_pfn(reg);
233+
for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, NULL)
236234
__add_active_range(0, start_pfn, end_pfn);
237-
}
238235

239236
/* All of system RAM sits in node 0 for the non-NUMA case */
240237
allocate_pgdat(0);

mm/memblock.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,12 +1663,10 @@ phys_addr_t __init_memblock memblock_reserved_size(void)
16631663
phys_addr_t __init memblock_mem_size(unsigned long limit_pfn)
16641664
{
16651665
unsigned long pages = 0;
1666-
struct memblock_region *r;
16671666
unsigned long start_pfn, end_pfn;
1667+
int i;
16681668

1669-
for_each_memblock(memory, r) {
1670-
start_pfn = memblock_region_memory_base_pfn(r);
1671-
end_pfn = memblock_region_memory_end_pfn(r);
1669+
for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, NULL) {
16721670
start_pfn = min_t(unsigned long, start_pfn, limit_pfn);
16731671
end_pfn = min_t(unsigned long, end_pfn, limit_pfn);
16741672
pages += end_pfn - start_pfn;

mm/sparse.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -291,13 +291,11 @@ static void __init memory_present(int nid, unsigned long start, unsigned long en
291291
*/
292292
static void __init memblocks_present(void)
293293
{
294-
struct memblock_region *reg;
294+
unsigned long start, end;
295+
int i, nid;
295296

296-
for_each_memblock(memory, reg) {
297-
memory_present(memblock_get_region_node(reg),
298-
memblock_region_memory_base_pfn(reg),
299-
memblock_region_memory_end_pfn(reg));
300-
}
297+
for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, &nid)
298+
memory_present(nid, start, end);
301299
}
302300

303301
/*

0 commit comments

Comments
 (0)