Skip to content

Commit

Permalink
treewide: add checks for the return value of memblock_alloc*()
Browse files Browse the repository at this point in the history
Add check for the return value of memblock_alloc*() functions and call
panic() in case of error.  The panic message repeats the one used by
panicing memblock allocators with adjustment of parameters to include
only relevant ones.

The replacement was mostly automated with semantic patches like the one
below with manual massaging of format strings.

  @@
  expression ptr, size, align;
  @@
  ptr = memblock_alloc(size, align);
  + if (!ptr)
  + 	panic("%s: Failed to allocate %lu bytes align=0x%lx\n", __func__, size, align);

[[email protected]: use '%pa' with 'phys_addr_t' type]
  Link: http://lkml.kernel.org/r/[email protected]
[[email protected]: fix format strings for panics after memblock_alloc]
  Link: http://lkml.kernel.org/r/[email protected]
[[email protected]: don't panic if the allocation in sparse_buffer_init fails]
  Link: http://lkml.kernel.org/r/20190131074018.GD28876@rapoport-lnx
[[email protected]: fix xtensa printk warning]
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Mike Rapoport <[email protected]>
Signed-off-by: Anders Roxell <[email protected]>
Reviewed-by: Guo Ren <[email protected]>		[c-sky]
Acked-by: Paul Burton <[email protected]>		[MIPS]
Acked-by: Heiko Carstens <[email protected]>	[s390]
Reviewed-by: Juergen Gross <[email protected]>		[Xen]
Reviewed-by: Geert Uytterhoeven <[email protected]>	[m68k]
Acked-by: Max Filippov <[email protected]>		[xtensa]
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: Greentime Hu <[email protected]>
Cc: Greg Kroah-Hartman <[email protected]>
Cc: Guan Xuetao <[email protected]>
Cc: Guo Ren <[email protected]>
Cc: Mark Salter <[email protected]>
Cc: Matt Turner <[email protected]>
Cc: Michael Ellerman <[email protected]>
Cc: Michal Simek <[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]>
  • Loading branch information
rppt authored and torvalds committed Mar 12, 2019
1 parent a0bf842 commit 8a7f97b
Show file tree
Hide file tree
Showing 74 changed files with 411 additions and 32 deletions.
3 changes: 3 additions & 0 deletions arch/alpha/kernel/core_cia.c
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,9 @@ cia_prepare_tbia_workaround(int window)

/* Use minimal 1K map. */
ppte = memblock_alloc(CIA_BROKEN_TBIA_SIZE, 32768);
if (!ppte)
panic("%s: Failed to allocate %u bytes align=0x%x\n",
__func__, CIA_BROKEN_TBIA_SIZE, 32768);
pte = (virt_to_phys(ppte) >> (PAGE_SHIFT - 1)) | 1;

for (i = 0; i < CIA_BROKEN_TBIA_SIZE / sizeof(unsigned long); ++i)
Expand Down
6 changes: 6 additions & 0 deletions arch/alpha/kernel/core_marvel.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ mk_resource_name(int pe, int port, char *str)

sprintf(tmp, "PCI %s PE %d PORT %d", str, pe, port);
name = memblock_alloc(strlen(tmp) + 1, SMP_CACHE_BYTES);
if (!name)
panic("%s: Failed to allocate %zu bytes\n", __func__,
strlen(tmp) + 1);
strcpy(name, tmp);

return name;
Expand Down Expand Up @@ -118,6 +121,9 @@ alloc_io7(unsigned int pe)
}

io7 = memblock_alloc(sizeof(*io7), SMP_CACHE_BYTES);
if (!io7)
panic("%s: Failed to allocate %zu bytes\n", __func__,
sizeof(*io7));
io7->pe = pe;
raw_spin_lock_init(&io7->irq_lock);

Expand Down
13 changes: 11 additions & 2 deletions arch/alpha/kernel/pci-noop.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ alloc_pci_controller(void)
struct pci_controller *hose;

hose = memblock_alloc(sizeof(*hose), SMP_CACHE_BYTES);
if (!hose)
panic("%s: Failed to allocate %zu bytes\n", __func__,
sizeof(*hose));

*hose_tail = hose;
hose_tail = &hose->next;
Expand All @@ -44,7 +47,13 @@ alloc_pci_controller(void)
struct resource * __init
alloc_resource(void)
{
return memblock_alloc(sizeof(struct resource), SMP_CACHE_BYTES);
void *ptr = memblock_alloc(sizeof(struct resource), SMP_CACHE_BYTES);

if (!ptr)
panic("%s: Failed to allocate %zu bytes\n", __func__,
sizeof(struct resource));

return ptr;
}

SYSCALL_DEFINE3(pciconfig_iobase, long, which, unsigned long, bus,
Expand All @@ -54,7 +63,7 @@ SYSCALL_DEFINE3(pciconfig_iobase, long, which, unsigned long, bus,

/* from hose or from bus.devfn */
if (which & IOBASE_FROM_HOSE) {
for (hose = hose_head; hose; hose = hose->next)
for (hose = hose_head; hose; hose = hose->next)
if (hose->index == bus)
break;
if (!hose)
Expand Down
11 changes: 10 additions & 1 deletion arch/alpha/kernel/pci.c
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,9 @@ alloc_pci_controller(void)
struct pci_controller *hose;

hose = memblock_alloc(sizeof(*hose), SMP_CACHE_BYTES);
if (!hose)
panic("%s: Failed to allocate %zu bytes\n", __func__,
sizeof(*hose));

*hose_tail = hose;
hose_tail = &hose->next;
Expand All @@ -403,7 +406,13 @@ alloc_pci_controller(void)
struct resource * __init
alloc_resource(void)
{
return memblock_alloc(sizeof(struct resource), SMP_CACHE_BYTES);
void *ptr = memblock_alloc(sizeof(struct resource), SMP_CACHE_BYTES);

if (!ptr)
panic("%s: Failed to allocate %zu bytes\n", __func__,
sizeof(struct resource));

return ptr;
}


Expand Down
12 changes: 12 additions & 0 deletions arch/alpha/kernel/pci_iommu.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ iommu_arena_new_node(int nid, struct pci_controller *hose, dma_addr_t base,
" falling back to system-wide allocation\n",
__func__, nid);
arena = memblock_alloc(sizeof(*arena), SMP_CACHE_BYTES);
if (!arena)
panic("%s: Failed to allocate %zu bytes\n", __func__,
sizeof(*arena));
}

arena->ptes = memblock_alloc_node(sizeof(*arena), align, nid);
Expand All @@ -88,12 +91,21 @@ iommu_arena_new_node(int nid, struct pci_controller *hose, dma_addr_t base,
" falling back to system-wide allocation\n",
__func__, nid);
arena->ptes = memblock_alloc(mem_size, align);
if (!arena->ptes)
panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
__func__, mem_size, align);
}

#else /* CONFIG_DISCONTIGMEM */

arena = memblock_alloc(sizeof(*arena), SMP_CACHE_BYTES);
if (!arena)
panic("%s: Failed to allocate %zu bytes\n", __func__,
sizeof(*arena));
arena->ptes = memblock_alloc(mem_size, align);
if (!arena->ptes)
panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
__func__, mem_size, align);

#endif /* CONFIG_DISCONTIGMEM */

Expand Down
4 changes: 4 additions & 0 deletions arch/arc/mm/highmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ static noinline pte_t * __init alloc_kmap_pgtable(unsigned long kvaddr)
pmd_k = pmd_offset(pud_k, kvaddr);

pte_k = (pte_t *)memblock_alloc_low(PAGE_SIZE, PAGE_SIZE);
if (!pte_k)
panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
__func__, PAGE_SIZE, PAGE_SIZE);

pmd_populate_kernel(&init_mm, pmd_k, pte_k);
return pte_k;
}
Expand Down
6 changes: 6 additions & 0 deletions arch/arm/kernel/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,9 @@ static void __init request_standard_resources(const struct machine_desc *mdesc)
boot_alias_start = phys_to_idmap(start);
if (arm_has_idmap_alias() && boot_alias_start != IDMAP_INVALID_ADDR) {
res = memblock_alloc(sizeof(*res), SMP_CACHE_BYTES);
if (!res)
panic("%s: Failed to allocate %zu bytes\n",
__func__, sizeof(*res));
res->name = "System RAM (boot alias)";
res->start = boot_alias_start;
res->end = phys_to_idmap(end);
Expand All @@ -875,6 +878,9 @@ static void __init request_standard_resources(const struct machine_desc *mdesc)
}

res = memblock_alloc(sizeof(*res), SMP_CACHE_BYTES);
if (!res)
panic("%s: Failed to allocate %zu bytes\n", __func__,
sizeof(*res));
res->name = "System RAM";
res->start = start;
res->end = end;
Expand Down
14 changes: 13 additions & 1 deletion arch/arm/mm/mmu.c
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,13 @@ EXPORT_SYMBOL(phys_mem_access_prot);

static void __init *early_alloc(unsigned long sz)
{
return memblock_alloc(sz, sz);
void *ptr = memblock_alloc(sz, sz);

if (!ptr)
panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
__func__, sz, sz);

return ptr;
}

static void *__init late_alloc(unsigned long sz)
Expand Down Expand Up @@ -994,6 +1000,9 @@ void __init iotable_init(struct map_desc *io_desc, int nr)
return;

svm = memblock_alloc(sizeof(*svm) * nr, __alignof__(*svm));
if (!svm)
panic("%s: Failed to allocate %zu bytes align=0x%zx\n",
__func__, sizeof(*svm) * nr, __alignof__(*svm));

for (md = io_desc; nr; md++, nr--) {
create_mapping(md);
Expand All @@ -1016,6 +1025,9 @@ void __init vm_reserve_area_early(unsigned long addr, unsigned long size,
struct static_vm *svm;

svm = memblock_alloc(sizeof(*svm), __alignof__(*svm));
if (!svm)
panic("%s: Failed to allocate %zu bytes align=0x%zx\n",
__func__, sizeof(*svm), __alignof__(*svm));

vm = &svm->vm;
vm->addr = (void *)addr;
Expand Down
8 changes: 5 additions & 3 deletions arch/arm64/kernel/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,18 @@ static void __init request_standard_resources(void)
struct memblock_region *region;
struct resource *res;
unsigned long i = 0;
size_t res_size;

kernel_code.start = __pa_symbol(_text);
kernel_code.end = __pa_symbol(__init_begin - 1);
kernel_data.start = __pa_symbol(_sdata);
kernel_data.end = __pa_symbol(_end - 1);

num_standard_resources = memblock.memory.cnt;
standard_resources = memblock_alloc_low(num_standard_resources *
sizeof(*standard_resources),
SMP_CACHE_BYTES);
res_size = num_standard_resources * sizeof(*standard_resources);
standard_resources = memblock_alloc_low(res_size, SMP_CACHE_BYTES);
if (!standard_resources)
panic("%s: Failed to allocate %zu bytes\n", __func__, res_size);

for_each_memblock(memory, region) {
res = &standard_resources[i++];
Expand Down
10 changes: 10 additions & 0 deletions arch/arm64/mm/kasan_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ static phys_addr_t __init kasan_alloc_zeroed_page(int node)
void *p = memblock_alloc_try_nid(PAGE_SIZE, PAGE_SIZE,
__pa(MAX_DMA_ADDRESS),
MEMBLOCK_ALLOC_KASAN, node);
if (!p)
panic("%s: Failed to allocate %lu bytes align=0x%lx nid=%d from=%llx\n",
__func__, PAGE_SIZE, PAGE_SIZE, node,
__pa(MAX_DMA_ADDRESS));

return __pa(p);
}

Expand All @@ -48,6 +53,11 @@ static phys_addr_t __init kasan_alloc_raw_page(int node)
void *p = memblock_alloc_try_nid_raw(PAGE_SIZE, PAGE_SIZE,
__pa(MAX_DMA_ADDRESS),
MEMBLOCK_ALLOC_KASAN, node);
if (!p)
panic("%s: Failed to allocate %lu bytes align=0x%lx nid=%d from=%llx\n",
__func__, PAGE_SIZE, PAGE_SIZE, node,
__pa(MAX_DMA_ADDRESS));

return __pa(p);
}

Expand Down
4 changes: 4 additions & 0 deletions arch/c6x/mm/dma-coherent.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ void __init coherent_mem_init(phys_addr_t start, u32 size)

dma_bitmap = memblock_alloc(BITS_TO_LONGS(dma_pages) * sizeof(long),
sizeof(long));
if (!dma_bitmap)
panic("%s: Failed to allocate %zu bytes align=0x%zx\n",
__func__, BITS_TO_LONGS(dma_pages) * sizeof(long),
sizeof(long));
}

static void c6x_dma_sync(struct device *dev, phys_addr_t paddr, size_t size,
Expand Down
3 changes: 3 additions & 0 deletions arch/c6x/mm/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ void __init paging_init(void)

empty_zero_page = (unsigned long) memblock_alloc(PAGE_SIZE,
PAGE_SIZE);
if (!empty_zero_page)
panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
__func__, PAGE_SIZE, PAGE_SIZE);

/*
* Set up user data space
Expand Down
5 changes: 5 additions & 0 deletions arch/csky/mm/highmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ static void __init fixrange_init(unsigned long start, unsigned long end,
for (; (k < PTRS_PER_PMD) && (vaddr != end); pmd++, k++) {
if (pmd_none(*pmd)) {
pte = (pte_t *) memblock_alloc_low(PAGE_SIZE, PAGE_SIZE);
if (!pte)
panic("%s: Failed to allocate %lu bytes align=%lx\n",
__func__, PAGE_SIZE,
PAGE_SIZE);

set_pmd(pmd, __pmd(__pa(pte)));
BUG_ON(pte != pte_offset_kernel(pmd, 0));
}
Expand Down
3 changes: 3 additions & 0 deletions arch/h8300/mm/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ void __init paging_init(void)
* to a couple of allocated pages.
*/
empty_zero_page = (unsigned long)memblock_alloc(PAGE_SIZE, PAGE_SIZE);
if (!empty_zero_page)
panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
__func__, PAGE_SIZE, PAGE_SIZE);

/*
* Set up SFC/DFC registers (user data space).
Expand Down
4 changes: 4 additions & 0 deletions arch/m68k/atari/stram.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ void __init atari_stram_reserve_pages(void *start_mem)
pr_debug("atari_stram pool: kernel in ST-RAM, using alloc_bootmem!\n");
stram_pool.start = (resource_size_t)memblock_alloc_low(pool_size,
PAGE_SIZE);
if (!stram_pool.start)
panic("%s: Failed to allocate %lu bytes align=%lx\n",
__func__, pool_size, PAGE_SIZE);

stram_pool.end = stram_pool.start + pool_size - 1;
request_resource(&iomem_resource, &stram_pool);
stram_virt_offset = 0;
Expand Down
3 changes: 3 additions & 0 deletions arch/m68k/mm/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ void __init paging_init(void)
high_memory = (void *) end_mem;

empty_zero_page = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
if (!empty_zero_page)
panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
__func__, PAGE_SIZE, PAGE_SIZE);

/*
* Set up SFC/DFC registers (user data space).
Expand Down
6 changes: 6 additions & 0 deletions arch/m68k/mm/mcfmmu.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,19 @@ void __init paging_init(void)
int i;

empty_zero_page = (void *) memblock_alloc(PAGE_SIZE, PAGE_SIZE);
if (!empty_zero_page)
panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
__func__, PAGE_SIZE, PAGE_SIZE);

pg_dir = swapper_pg_dir;
memset(swapper_pg_dir, 0, sizeof(swapper_pg_dir));

size = num_pages * sizeof(pte_t);
size = (size + PAGE_SIZE) & ~(PAGE_SIZE-1);
next_pgtable = (unsigned long) memblock_alloc(size, PAGE_SIZE);
if (!next_pgtable)
panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
__func__, size, PAGE_SIZE);

bootmem_end = (next_pgtable + size + PAGE_SIZE) & PAGE_MASK;
pg_dir += PAGE_OFFSET >> PGDIR_SHIFT;
Expand Down
9 changes: 9 additions & 0 deletions arch/m68k/mm/motorola.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ static pte_t * __init kernel_page_table(void)
pte_t *ptablep;

ptablep = (pte_t *)memblock_alloc_low(PAGE_SIZE, PAGE_SIZE);
if (!ptablep)
panic("%s: Failed to allocate %lu bytes align=%lx\n",
__func__, PAGE_SIZE, PAGE_SIZE);

clear_page(ptablep);
__flush_page_to_ram(ptablep);
Expand Down Expand Up @@ -96,6 +99,9 @@ static pmd_t * __init kernel_ptr_table(void)
if (((unsigned long)last_pgtable & ~PAGE_MASK) == 0) {
last_pgtable = (pmd_t *)memblock_alloc_low(PAGE_SIZE,
PAGE_SIZE);
if (!last_pgtable)
panic("%s: Failed to allocate %lu bytes align=%lx\n",
__func__, PAGE_SIZE, PAGE_SIZE);

clear_page(last_pgtable);
__flush_page_to_ram(last_pgtable);
Expand Down Expand Up @@ -278,6 +284,9 @@ void __init paging_init(void)
* to a couple of allocated pages
*/
empty_zero_page = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
if (!empty_zero_page)
panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
__func__, PAGE_SIZE, PAGE_SIZE);

/*
* Set up SFC/DFC registers
Expand Down
6 changes: 6 additions & 0 deletions arch/m68k/mm/sun3mmu.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ void __init paging_init(void)
unsigned long size;

empty_zero_page = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
if (!empty_zero_page)
panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
__func__, PAGE_SIZE, PAGE_SIZE);

address = PAGE_OFFSET;
pg_dir = swapper_pg_dir;
Expand All @@ -56,6 +59,9 @@ void __init paging_init(void)
size = (size + PAGE_SIZE) & ~(PAGE_SIZE-1);

next_pgtable = (unsigned long)memblock_alloc(size, PAGE_SIZE);
if (!next_pgtable)
panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
__func__, size, PAGE_SIZE);
bootmem_end = (next_pgtable + size + PAGE_SIZE) & PAGE_MASK;

/* Map whole memory from PAGE_OFFSET (0x0E000000) */
Expand Down
3 changes: 3 additions & 0 deletions arch/m68k/sun3/sun3dvma.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@ void __init dvma_init(void)

iommu_use = memblock_alloc(IOMMU_TOTAL_ENTRIES * sizeof(unsigned long),
SMP_CACHE_BYTES);
if (!iommu_use)
panic("%s: Failed to allocate %zu bytes\n", __func__,
IOMMU_TOTAL_ENTRIES * sizeof(unsigned long));

dvma_unmap_iommu(DVMA_START, DVMA_SIZE);

Expand Down
8 changes: 6 additions & 2 deletions arch/microblaze/mm/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,14 @@ void * __ref zalloc_maybe_bootmem(size_t size, gfp_t mask)
{
void *p;

if (mem_init_done)
if (mem_init_done) {
p = kzalloc(size, mask);
else
} else {
p = memblock_alloc(size, SMP_CACHE_BYTES);
if (!p)
panic("%s: Failed to allocate %zu bytes\n",
__func__, size);
}

return p;
}
Loading

0 comments on commit 8a7f97b

Please sign in to comment.