Skip to content

Commit 9f3d5ea

Browse files
rppttorvalds
authored andcommitted
memblock: implement for_each_reserved_mem_region() using __next_mem_region()
Iteration over memblock.reserved with for_each_reserved_mem_region() used __next_reserved_mem_region() that implemented a subset of __next_mem_region(). Use __for_each_mem_range() and, essentially, __next_mem_region() with appropriate parameters to reduce code duplication. While on it, rename for_each_reserved_mem_region() to for_each_reserved_mem_range() for consistency. Signed-off-by: Mike Rapoport <rppt@linux.ibm.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Acked-by: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com> [.clang-format] Cc: Andy Lutomirski <luto@kernel.org> Cc: Baoquan He <bhe@redhat.com> Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org> Cc: Borislav Petkov <bp@alien8.de> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: Christoph Hellwig <hch@lst.de> Cc: Daniel Axtens <dja@axtens.net> Cc: Dave Hansen <dave.hansen@linux.intel.com> Cc: Emil Renner Berthing <kernel@esmil.dk> Cc: Hari Bathini <hbathini@linux.ibm.com> Cc: Ingo Molnar <mingo@kernel.org> Cc: Ingo Molnar <mingo@redhat.com> Cc: Jonathan Cameron <Jonathan.Cameron@huawei.com> Cc: Marek Szyprowski <m.szyprowski@samsung.com> Cc: Max Filippov <jcmvbkbc@gmail.com> Cc: Michael Ellerman <mpe@ellerman.id.au> Cc: Michal Simek <monstr@monstr.eu> Cc: Palmer Dabbelt <palmer@dabbelt.com> Cc: Paul Mackerras <paulus@samba.org> Cc: Paul Walmsley <paul.walmsley@sifive.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Russell King <linux@armlinux.org.uk> Cc: Stafford Horne <shorne@gmail.com> Cc: Thomas Bogendoerfer <tsbogend@alpha.franken.de> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Will Deacon <will@kernel.org> Cc: Yoshinori Sato <ysato@users.sourceforge.jp> Link: https://lkml.kernel.org/r/20200818151634.14343-17-rppt@kernel.org Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 5bd0960 commit 9f3d5ea

5 files changed

Lines changed: 27 additions & 47 deletions

File tree

.clang-format

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ ForEachMacros:
273273
- 'for_each_registered_fb'
274274
- 'for_each_requested_gpio'
275275
- 'for_each_requested_gpio_in_range'
276-
- 'for_each_reserved_mem_region'
276+
- 'for_each_reserved_mem_range'
277277
- 'for_each_rtd_codec_dais'
278278
- 'for_each_rtd_codec_dais_rollback'
279279
- 'for_each_rtd_components'

arch/arm64/kernel/setup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ static int __init reserve_memblock_reserved_regions(void)
257257
if (!memblock_is_region_reserved(mem->start, mem_size))
258258
continue;
259259

260-
for_each_reserved_mem_region(j, &r_start, &r_end) {
260+
for_each_reserved_mem_range(j, &r_start, &r_end) {
261261
resource_size_t start, end;
262262

263263
start = max(PFN_PHYS(PFN_DOWN(r_start)), mem->start);

drivers/irqchip/irq-gic-v3-its.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2198,7 +2198,7 @@ static bool gic_check_reserved_range(phys_addr_t addr, unsigned long size)
21982198

21992199
addr_end = addr + size - 1;
22002200

2201-
for_each_reserved_mem_region(i, &start, &end) {
2201+
for_each_reserved_mem_range(i, &start, &end) {
22022202
if (addr >= start && addr_end <= end)
22032203
return true;
22042204
}

include/linux/memblock.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,6 @@ void __next_mem_range_rev(u64 *idx, int nid, enum memblock_flags flags,
132132
struct memblock_type *type_b, phys_addr_t *out_start,
133133
phys_addr_t *out_end, int *out_nid);
134134

135-
void __next_reserved_mem_region(u64 *idx, phys_addr_t *out_start,
136-
phys_addr_t *out_end);
137-
138135
void __memblock_free_late(phys_addr_t base, phys_addr_t size);
139136

140137
#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
@@ -224,18 +221,17 @@ static inline void __next_physmem_range(u64 *idx, struct memblock_type *type,
224221
MEMBLOCK_NONE, p_start, p_end, NULL)
225222

226223
/**
227-
* for_each_reserved_mem_region - iterate over all reserved memblock areas
224+
* for_each_reserved_mem_range - iterate over all reserved memblock areas
228225
* @i: u64 used as loop variable
229226
* @p_start: ptr to phys_addr_t for start address of the range, can be %NULL
230227
* @p_end: ptr to phys_addr_t for end address of the range, can be %NULL
231228
*
232229
* Walks over reserved areas of memblock. Available as soon as memblock
233230
* is initialized.
234231
*/
235-
#define for_each_reserved_mem_region(i, p_start, p_end) \
236-
for (i = 0UL, __next_reserved_mem_region(&i, p_start, p_end); \
237-
i != (u64)ULLONG_MAX; \
238-
__next_reserved_mem_region(&i, p_start, p_end))
232+
#define for_each_reserved_mem_range(i, p_start, p_end) \
233+
__for_each_mem_range(i, &memblock.reserved, NULL, NUMA_NO_NODE, \
234+
MEMBLOCK_NONE, p_start, p_end, NULL)
239235

240236
static inline bool memblock_is_hotpluggable(struct memblock_region *m)
241237
{

mm/memblock.c

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ struct memblock_type physmem = {
132132
};
133133
#endif
134134

135+
/*
136+
* keep a pointer to &memblock.memory in the text section to use it in
137+
* __next_mem_range() and its helpers.
138+
* For architectures that do not keep memblock data after init, this
139+
* pointer will be reset to NULL at memblock_discard()
140+
*/
141+
static __refdata struct memblock_type *memblock_memory = &memblock.memory;
142+
135143
#define for_each_memblock_type(i, memblock_type, rgn) \
136144
for (i = 0, rgn = &memblock_type->regions[0]; \
137145
i < memblock_type->cnt; \
@@ -402,6 +410,8 @@ void __init memblock_discard(void)
402410
memblock.memory.max);
403411
__memblock_free_late(addr, size);
404412
}
413+
414+
memblock_memory = NULL;
405415
}
406416
#endif
407417

@@ -952,42 +962,16 @@ int __init_memblock memblock_clear_nomap(phys_addr_t base, phys_addr_t size)
952962
return memblock_setclr_flag(base, size, 0, MEMBLOCK_NOMAP);
953963
}
954964

955-
/**
956-
* __next_reserved_mem_region - next function for for_each_reserved_region()
957-
* @idx: pointer to u64 loop variable
958-
* @out_start: ptr to phys_addr_t for start address of the region, can be %NULL
959-
* @out_end: ptr to phys_addr_t for end address of the region, can be %NULL
960-
*
961-
* Iterate over all reserved memory regions.
962-
*/
963-
void __init_memblock __next_reserved_mem_region(u64 *idx,
964-
phys_addr_t *out_start,
965-
phys_addr_t *out_end)
966-
{
967-
struct memblock_type *type = &memblock.reserved;
968-
969-
if (*idx < type->cnt) {
970-
struct memblock_region *r = &type->regions[*idx];
971-
phys_addr_t base = r->base;
972-
phys_addr_t size = r->size;
973-
974-
if (out_start)
975-
*out_start = base;
976-
if (out_end)
977-
*out_end = base + size - 1;
978-
979-
*idx += 1;
980-
return;
981-
}
982-
983-
/* signal end of iteration */
984-
*idx = ULLONG_MAX;
985-
}
986-
987-
static bool should_skip_region(struct memblock_region *m, int nid, int flags)
965+
static bool should_skip_region(struct memblock_type *type,
966+
struct memblock_region *m,
967+
int nid, int flags)
988968
{
989969
int m_nid = memblock_get_region_node(m);
990970

971+
/* we never skip regions when iterating memblock.reserved or physmem */
972+
if (type != memblock_memory)
973+
return false;
974+
991975
/* only memory regions are associated with nodes, check it */
992976
if (nid != NUMA_NO_NODE && nid != m_nid)
993977
return true;
@@ -1052,7 +1036,7 @@ void __next_mem_range(u64 *idx, int nid, enum memblock_flags flags,
10521036
phys_addr_t m_end = m->base + m->size;
10531037
int m_nid = memblock_get_region_node(m);
10541038

1055-
if (should_skip_region(m, nid, flags))
1039+
if (should_skip_region(type_a, m, nid, flags))
10561040
continue;
10571041

10581042
if (!type_b) {
@@ -1156,7 +1140,7 @@ void __init_memblock __next_mem_range_rev(u64 *idx, int nid,
11561140
phys_addr_t m_end = m->base + m->size;
11571141
int m_nid = memblock_get_region_node(m);
11581142

1159-
if (should_skip_region(m, nid, flags))
1143+
if (should_skip_region(type_a, m, nid, flags))
11601144
continue;
11611145

11621146
if (!type_b) {
@@ -1981,7 +1965,7 @@ static unsigned long __init free_low_memory_core_early(void)
19811965

19821966
memblock_clear_hotplug(0, -1);
19831967

1984-
for_each_reserved_mem_region(i, &start, &end)
1968+
for_each_reserved_mem_range(i, &start, &end)
19851969
reserve_bootmem_region(start, end);
19861970

19871971
/*

0 commit comments

Comments
 (0)