Skip to content

Commit f655f40

Browse files
rppttorvalds
authored andcommitted
mm/percpu: add checks for the return value of memblock_alloc*()
Add panic() calls if memblock_alloc() returns NULL. The panic() format duplicates the one used by memblock itself and in order to avoid explosion with long parameters list replace open coded allocation size calculations with a local variable. Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Mike Rapoport <[email protected]> Cc: Catalin Marinas <[email protected]> Cc: Christophe Leroy <[email protected]> Cc: Christoph Hellwig <[email protected]> Cc: "David S. Miller" <[email protected]> Cc: Dennis Zhou <[email protected]> Cc: Geert Uytterhoeven <[email protected]> Cc: Greentime Hu <[email protected]> Cc: Greg Kroah-Hartman <[email protected]> Cc: Guan Xuetao <[email protected]> Cc: Guo Ren <[email protected]> Cc: Guo Ren <[email protected]> [c-sky] Cc: Heiko Carstens <[email protected]> Cc: Juergen Gross <[email protected]> [Xen] Cc: Mark Salter <[email protected]> Cc: Matt Turner <[email protected]> Cc: Max Filippov <[email protected]> Cc: Michael Ellerman <[email protected]> Cc: Michal Simek <[email protected]> Cc: Paul Burton <[email protected]> Cc: Petr Mladek <[email protected]> Cc: Richard Weinberger <[email protected]> Cc: Rich Felker <[email protected]> Cc: Rob Herring <[email protected]> Cc: Rob Herring <[email protected]> Cc: Russell King <[email protected]> Cc: Stafford Horne <[email protected]> Cc: Tony Luck <[email protected]> Cc: Vineet Gupta <[email protected]> Cc: Yoshinori Sato <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent b1e1c86 commit f655f40

File tree

1 file changed

+56
-17
lines changed

1 file changed

+56
-17
lines changed

mm/percpu.c

+56-17
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,7 @@ static struct pcpu_chunk * __init pcpu_alloc_first_chunk(unsigned long tmp_addr,
10861086
struct pcpu_chunk *chunk;
10871087
unsigned long aligned_addr, lcm_align;
10881088
int start_offset, offset_bits, region_size, region_bits;
1089+
size_t alloc_size;
10891090

10901091
/* region calculations */
10911092
aligned_addr = tmp_addr & PAGE_MASK;
@@ -1101,9 +1102,12 @@ static struct pcpu_chunk * __init pcpu_alloc_first_chunk(unsigned long tmp_addr,
11011102
region_size = ALIGN(start_offset + map_size, lcm_align);
11021103

11031104
/* allocate chunk */
1104-
chunk = memblock_alloc(sizeof(struct pcpu_chunk) +
1105-
BITS_TO_LONGS(region_size >> PAGE_SHIFT),
1106-
SMP_CACHE_BYTES);
1105+
alloc_size = sizeof(struct pcpu_chunk) +
1106+
BITS_TO_LONGS(region_size >> PAGE_SHIFT);
1107+
chunk = memblock_alloc(alloc_size, SMP_CACHE_BYTES);
1108+
if (!chunk)
1109+
panic("%s: Failed to allocate %zu bytes\n", __func__,
1110+
alloc_size);
11071111

11081112
INIT_LIST_HEAD(&chunk->list);
11091113

@@ -1114,12 +1118,25 @@ static struct pcpu_chunk * __init pcpu_alloc_first_chunk(unsigned long tmp_addr,
11141118
chunk->nr_pages = region_size >> PAGE_SHIFT;
11151119
region_bits = pcpu_chunk_map_bits(chunk);
11161120

1117-
chunk->alloc_map = memblock_alloc(BITS_TO_LONGS(region_bits) * sizeof(chunk->alloc_map[0]),
1118-
SMP_CACHE_BYTES);
1119-
chunk->bound_map = memblock_alloc(BITS_TO_LONGS(region_bits + 1) * sizeof(chunk->bound_map[0]),
1120-
SMP_CACHE_BYTES);
1121-
chunk->md_blocks = memblock_alloc(pcpu_chunk_nr_blocks(chunk) * sizeof(chunk->md_blocks[0]),
1122-
SMP_CACHE_BYTES);
1121+
alloc_size = BITS_TO_LONGS(region_bits) * sizeof(chunk->alloc_map[0]);
1122+
chunk->alloc_map = memblock_alloc(alloc_size, SMP_CACHE_BYTES);
1123+
if (!chunk->alloc_map)
1124+
panic("%s: Failed to allocate %zu bytes\n", __func__,
1125+
alloc_size);
1126+
1127+
alloc_size =
1128+
BITS_TO_LONGS(region_bits + 1) * sizeof(chunk->bound_map[0]);
1129+
chunk->bound_map = memblock_alloc(alloc_size, SMP_CACHE_BYTES);
1130+
if (!chunk->bound_map)
1131+
panic("%s: Failed to allocate %zu bytes\n", __func__,
1132+
alloc_size);
1133+
1134+
alloc_size = pcpu_chunk_nr_blocks(chunk) * sizeof(chunk->md_blocks[0]);
1135+
chunk->md_blocks = memblock_alloc(alloc_size, SMP_CACHE_BYTES);
1136+
if (!chunk->md_blocks)
1137+
panic("%s: Failed to allocate %zu bytes\n", __func__,
1138+
alloc_size);
1139+
11231140
pcpu_init_md_blocks(chunk);
11241141

11251142
/* manage populated page bitmap */
@@ -2044,6 +2061,7 @@ int __init pcpu_setup_first_chunk(const struct pcpu_alloc_info *ai,
20442061
int group, unit, i;
20452062
int map_size;
20462063
unsigned long tmp_addr;
2064+
size_t alloc_size;
20472065

20482066
#define PCPU_SETUP_BUG_ON(cond) do { \
20492067
if (unlikely(cond)) { \
@@ -2075,14 +2093,29 @@ int __init pcpu_setup_first_chunk(const struct pcpu_alloc_info *ai,
20752093
PCPU_SETUP_BUG_ON(pcpu_verify_alloc_info(ai) < 0);
20762094

20772095
/* process group information and build config tables accordingly */
2078-
group_offsets = memblock_alloc(ai->nr_groups * sizeof(group_offsets[0]),
2079-
SMP_CACHE_BYTES);
2080-
group_sizes = memblock_alloc(ai->nr_groups * sizeof(group_sizes[0]),
2081-
SMP_CACHE_BYTES);
2082-
unit_map = memblock_alloc(nr_cpu_ids * sizeof(unit_map[0]),
2083-
SMP_CACHE_BYTES);
2084-
unit_off = memblock_alloc(nr_cpu_ids * sizeof(unit_off[0]),
2085-
SMP_CACHE_BYTES);
2096+
alloc_size = ai->nr_groups * sizeof(group_offsets[0]);
2097+
group_offsets = memblock_alloc(alloc_size, SMP_CACHE_BYTES);
2098+
if (!group_offsets)
2099+
panic("%s: Failed to allocate %zu bytes\n", __func__,
2100+
alloc_size);
2101+
2102+
alloc_size = ai->nr_groups * sizeof(group_sizes[0]);
2103+
group_sizes = memblock_alloc(alloc_size, SMP_CACHE_BYTES);
2104+
if (!group_sizes)
2105+
panic("%s: Failed to allocate %zu bytes\n", __func__,
2106+
alloc_size);
2107+
2108+
alloc_size = nr_cpu_ids * sizeof(unit_map[0]);
2109+
unit_map = memblock_alloc(alloc_size, SMP_CACHE_BYTES);
2110+
if (!unit_map)
2111+
panic("%s: Failed to allocate %zu bytes\n", __func__,
2112+
alloc_size);
2113+
2114+
alloc_size = nr_cpu_ids * sizeof(unit_off[0]);
2115+
unit_off = memblock_alloc(alloc_size, SMP_CACHE_BYTES);
2116+
if (!unit_off)
2117+
panic("%s: Failed to allocate %zu bytes\n", __func__,
2118+
alloc_size);
20862119

20872120
for (cpu = 0; cpu < nr_cpu_ids; cpu++)
20882121
unit_map[cpu] = UINT_MAX;
@@ -2148,6 +2181,9 @@ int __init pcpu_setup_first_chunk(const struct pcpu_alloc_info *ai,
21482181
pcpu_nr_slots = __pcpu_size_to_slot(pcpu_unit_size) + 2;
21492182
pcpu_slot = memblock_alloc(pcpu_nr_slots * sizeof(pcpu_slot[0]),
21502183
SMP_CACHE_BYTES);
2184+
if (!pcpu_slot)
2185+
panic("%s: Failed to allocate %zu bytes\n", __func__,
2186+
pcpu_nr_slots * sizeof(pcpu_slot[0]));
21512187
for (i = 0; i < pcpu_nr_slots; i++)
21522188
INIT_LIST_HEAD(&pcpu_slot[i]);
21532189

@@ -2602,6 +2638,9 @@ int __init pcpu_page_first_chunk(size_t reserved_size,
26022638
pages_size = PFN_ALIGN(unit_pages * num_possible_cpus() *
26032639
sizeof(pages[0]));
26042640
pages = memblock_alloc(pages_size, SMP_CACHE_BYTES);
2641+
if (!pages)
2642+
panic("%s: Failed to allocate %zu bytes\n", __func__,
2643+
pages_size);
26052644

26062645
/* allocate pages */
26072646
j = 0;

0 commit comments

Comments
 (0)