Skip to content

Commit 797f037

Browse files
atishp04palmer-dabbelt
authored andcommitted
RISC-V: Do not allocate memblock while iterating reserved memblocks
Currently, resource tree allocates memory blocks while iterating on the list. It leads to following kernel warning because memblock allocation also invokes memory block reservation API. [ 0.000000] ------------[ cut here ]------------ [ 0.000000] WARNING: CPU: 0 PID: 0 at kernel/resource.c:795 __insert_resource+0x8e/0xd0 [ 0.000000] Modules linked in: [ 0.000000] CPU: 0 PID: 0 Comm: swapper Not tainted 5.10.0-00022-ge20097fb37e2-dirty #549 [ 0.000000] epc: c00125c ra : c001262c sp : c1c01f50 [ 0.000000] gp : c1d456e0 tp : c1c0a980 t0 : ffffcf20 [ 0.000000] t1 : 00000000 t2 : 00000000 s0 : c1c01f60 [ 0.000000] s1 : ffffcf00 a0 : ffffff00 a1 : c1c0c0c4 [ 0.000000] a2 : 80c12b15 a3 : 80402000 a4 : 80402000 [ 0.000000] a5 : c1c0c0c4 a6 : 80c12b15 a7 : f5faf600 [ 0.000000] s2 : c1c0c0c4 s3 : c1c0e000 s4 : c1009a80 [ 0.000000] s5 : c1c0c000 s6 : c1d48000 s7 : c1613b4c [ 0.000000] s8 : 00000fff s9 : 80000200 s10: c1613b40 [ 0.000000] s11: 00000000 t3 : c1d4a000 t4 : ffffffff This is also unnecessary as we can pre-compute the total memblocks required for each memory region and allocate it before the loop. It save precious boot time not going through memblock allocation code every time. Fixes: 00ab027 ("RISC-V: Add kernel image sections to the resource tree") Reviewed-by: Anup Patel <[email protected]> Tested-by: Geert Uytterhoeven <[email protected]> Signed-off-by: Atish Patra <[email protected]> Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent dec8227 commit 797f037

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

arch/riscv/kernel/setup.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ static void __init init_resources(void)
127127
{
128128
struct memblock_region *region = NULL;
129129
struct resource *res = NULL;
130-
int ret = 0;
130+
struct resource *mem_res = NULL;
131+
size_t mem_res_sz = 0;
132+
int ret = 0, i = 0;
131133

132134
code_res.start = __pa_symbol(_text);
133135
code_res.end = __pa_symbol(_etext) - 1;
@@ -145,16 +147,17 @@ static void __init init_resources(void)
145147
bss_res.end = __pa_symbol(__bss_stop) - 1;
146148
bss_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
147149

150+
mem_res_sz = (memblock.memory.cnt + memblock.reserved.cnt) * sizeof(*mem_res);
151+
mem_res = memblock_alloc(mem_res_sz, SMP_CACHE_BYTES);
152+
if (!mem_res)
153+
panic("%s: Failed to allocate %zu bytes\n", __func__, mem_res_sz);
148154
/*
149155
* Start by adding the reserved regions, if they overlap
150156
* with /memory regions, insert_resource later on will take
151157
* care of it.
152158
*/
153159
for_each_reserved_mem_region(region) {
154-
res = memblock_alloc(sizeof(struct resource), SMP_CACHE_BYTES);
155-
if (!res)
156-
panic("%s: Failed to allocate %zu bytes\n", __func__,
157-
sizeof(struct resource));
160+
res = &mem_res[i++];
158161

159162
res->name = "Reserved";
160163
res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
@@ -171,8 +174,10 @@ static void __init init_resources(void)
171174
* Ignore any other reserved regions within
172175
* system memory.
173176
*/
174-
if (memblock_is_memory(res->start))
177+
if (memblock_is_memory(res->start)) {
178+
memblock_free((phys_addr_t) res, sizeof(struct resource));
175179
continue;
180+
}
176181

177182
ret = add_resource(&iomem_resource, res);
178183
if (ret < 0)
@@ -181,10 +186,7 @@ static void __init init_resources(void)
181186

182187
/* Add /memory regions to the resource tree */
183188
for_each_mem_region(region) {
184-
res = memblock_alloc(sizeof(struct resource), SMP_CACHE_BYTES);
185-
if (!res)
186-
panic("%s: Failed to allocate %zu bytes\n", __func__,
187-
sizeof(struct resource));
189+
res = &mem_res[i++];
188190

189191
if (unlikely(memblock_is_nomap(region))) {
190192
res->name = "Reserved";
@@ -205,9 +207,9 @@ static void __init init_resources(void)
205207
return;
206208

207209
error:
208-
memblock_free((phys_addr_t) res, sizeof(struct resource));
209210
/* Better an empty resource tree than an inconsistent one */
210211
release_child_resources(&iomem_resource);
212+
memblock_free((phys_addr_t) mem_res, mem_res_sz);
211213
}
212214

213215

0 commit comments

Comments
 (0)