treewide: add checks for the return value of memblock_alloc*()
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); [anders.roxell@linaro.org: use '%pa' with 'phys_addr_t' type] Link: http://lkml.kernel.org/r/20190131161046.21886-1-anders.roxell@linaro.org [rppt@linux.ibm.com: fix format strings for panics after memblock_alloc] Link: http://lkml.kernel.org/r/1548950940-15145-1-git-send-email-rppt@linux.ibm.com [rppt@linux.ibm.com: don't panic if the allocation in sparse_buffer_init fails] Link: http://lkml.kernel.org/r/20190131074018.GD28876@rapoport-lnx [akpm@linux-foundation.org: fix xtensa printk warning] Link: http://lkml.kernel.org/r/1548057848-15136-20-git-send-email-rppt@linux.ibm.com Signed-off-by: Mike Rapoport <rppt@linux.ibm.com> Signed-off-by: Anders Roxell <anders.roxell@linaro.org> Reviewed-by: Guo Ren <ren_guo@c-sky.com> [c-sky] Acked-by: Paul Burton <paul.burton@mips.com> [MIPS] Acked-by: Heiko Carstens <heiko.carstens@de.ibm.com> [s390] Reviewed-by: Juergen Gross <jgross@suse.com> [Xen] Reviewed-by: Geert Uytterhoeven <geert@linux-m68k.org> [m68k] Acked-by: Max Filippov <jcmvbkbc@gmail.com> [xtensa] Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: Christophe Leroy <christophe.leroy@c-s.fr> Cc: Christoph Hellwig <hch@lst.de> Cc: "David S. Miller" <davem@davemloft.net> Cc: Dennis Zhou <dennis@kernel.org> Cc: Greentime Hu <green.hu@gmail.com> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Cc: Guan Xuetao <gxt@pku.edu.cn> Cc: Guo Ren <guoren@kernel.org> Cc: Mark Salter <msalter@redhat.com> Cc: Matt Turner <mattst88@gmail.com> Cc: Michael Ellerman <mpe@ellerman.id.au> Cc: Michal Simek <monstr@monstr.eu> Cc: Petr Mladek <pmladek@suse.com> Cc: Richard Weinberger <richard@nod.at> Cc: Rich Felker <dalias@libc.org> Cc: Rob Herring <robh+dt@kernel.org> Cc: Rob Herring <robh@kernel.org> Cc: Russell King <linux@armlinux.org.uk> Cc: Stafford Horne <shorne@gmail.com> Cc: Tony Luck <tony.luck@intel.com> Cc: Vineet Gupta <vgupta@synopsys.com> Cc: Yoshinori Sato <ysato@users.sourceforge.jp> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:

committed by
Linus Torvalds

parent
a0bf842e89
commit
8a7f97b902
@@ -1005,6 +1005,11 @@ static int __init dt_cpu_ftrs_scan_callback(unsigned long node, const char
|
||||
of_scan_flat_dt_subnodes(node, count_cpufeatures_subnodes,
|
||||
&nr_dt_cpu_features);
|
||||
dt_cpu_features = memblock_alloc(sizeof(struct dt_cpu_feature) * nr_dt_cpu_features, PAGE_SIZE);
|
||||
if (!dt_cpu_features)
|
||||
panic("%s: Failed to allocate %zu bytes align=0x%lx\n",
|
||||
__func__,
|
||||
sizeof(struct dt_cpu_feature) * nr_dt_cpu_features,
|
||||
PAGE_SIZE);
|
||||
|
||||
cpufeatures_setup_start(isa);
|
||||
|
||||
|
@@ -205,6 +205,9 @@ pci_create_OF_bus_map(void)
|
||||
|
||||
of_prop = memblock_alloc(sizeof(struct property) + 256,
|
||||
SMP_CACHE_BYTES);
|
||||
if (!of_prop)
|
||||
panic("%s: Failed to allocate %zu bytes\n", __func__,
|
||||
sizeof(struct property) + 256);
|
||||
dn = of_find_node_by_path("/");
|
||||
if (dn) {
|
||||
memset(of_prop, -1, sizeof(struct property) + 256);
|
||||
|
@@ -461,6 +461,9 @@ void __init smp_setup_cpu_maps(void)
|
||||
|
||||
cpu_to_phys_id = memblock_alloc(nr_cpu_ids * sizeof(u32),
|
||||
__alignof__(u32));
|
||||
if (!cpu_to_phys_id)
|
||||
panic("%s: Failed to allocate %zu bytes align=0x%zx\n",
|
||||
__func__, nr_cpu_ids * sizeof(u32), __alignof__(u32));
|
||||
|
||||
for_each_node_by_type(dn, "cpu") {
|
||||
const __be32 *intserv;
|
||||
|
@@ -905,6 +905,10 @@ static void __ref init_fallback_flush(void)
|
||||
l1d_flush_fallback_area = memblock_alloc_try_nid(l1d_size * 2,
|
||||
l1d_size, MEMBLOCK_LOW_LIMIT,
|
||||
limit, NUMA_NO_NODE);
|
||||
if (!l1d_flush_fallback_area)
|
||||
panic("%s: Failed to allocate %llu bytes align=0x%llx max_addr=%pa\n",
|
||||
__func__, l1d_size * 2, l1d_size, &limit);
|
||||
|
||||
|
||||
for_each_possible_cpu(cpu) {
|
||||
struct paca_struct *paca = paca_ptrs[cpu];
|
||||
|
@@ -15,6 +15,9 @@ void * __ref zalloc_maybe_bootmem(size_t size, gfp_t mask)
|
||||
p = kzalloc(size, mask);
|
||||
else {
|
||||
p = memblock_alloc(size, SMP_CACHE_BYTES);
|
||||
if (!p)
|
||||
panic("%s: Failed to allocate %zu bytes\n", __func__,
|
||||
size);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
@@ -915,6 +915,9 @@ static void __init htab_initialize(void)
|
||||
linear_map_hash_slots = memblock_alloc_try_nid(
|
||||
linear_map_hash_count, 1, MEMBLOCK_LOW_LIMIT,
|
||||
ppc64_rma_size, NUMA_NO_NODE);
|
||||
if (!linear_map_hash_slots)
|
||||
panic("%s: Failed to allocate %lu bytes max_addr=%pa\n",
|
||||
__func__, linear_map_hash_count, &ppc64_rma_size);
|
||||
}
|
||||
#endif /* CONFIG_DEBUG_PAGEALLOC */
|
||||
|
||||
|
@@ -461,10 +461,19 @@ void __init mmu_context_init(void)
|
||||
* Allocate the maps used by context management
|
||||
*/
|
||||
context_map = memblock_alloc(CTX_MAP_SIZE, SMP_CACHE_BYTES);
|
||||
if (!context_map)
|
||||
panic("%s: Failed to allocate %zu bytes\n", __func__,
|
||||
CTX_MAP_SIZE);
|
||||
context_mm = memblock_alloc(sizeof(void *) * (LAST_CONTEXT + 1),
|
||||
SMP_CACHE_BYTES);
|
||||
if (!context_mm)
|
||||
panic("%s: Failed to allocate %zu bytes\n", __func__,
|
||||
sizeof(void *) * (LAST_CONTEXT + 1));
|
||||
#ifdef CONFIG_SMP
|
||||
stale_map[boot_cpuid] = memblock_alloc(CTX_MAP_SIZE, SMP_CACHE_BYTES);
|
||||
if (!stale_map[boot_cpuid])
|
||||
panic("%s: Failed to allocate %zu bytes\n", __func__,
|
||||
CTX_MAP_SIZE);
|
||||
|
||||
cpuhp_setup_state_nocalls(CPUHP_POWERPC_MMU_CTX_PREPARE,
|
||||
"powerpc/mmu/ctx:prepare",
|
||||
|
@@ -57,8 +57,16 @@ void vmemmap_remove_mapping(unsigned long start,
|
||||
|
||||
static __ref void *early_alloc_pgtable(unsigned long size)
|
||||
{
|
||||
return memblock_alloc_try_nid(size, size, MEMBLOCK_LOW_LIMIT,
|
||||
__pa(MAX_DMA_ADDRESS), NUMA_NO_NODE);
|
||||
void *ptr;
|
||||
|
||||
ptr = memblock_alloc_try_nid(size, size, MEMBLOCK_LOW_LIMIT,
|
||||
__pa(MAX_DMA_ADDRESS), NUMA_NO_NODE);
|
||||
|
||||
if (!ptr)
|
||||
panic("%s: Failed to allocate %lu bytes align=0x%lx max_addr=%lx\n",
|
||||
__func__, size, size, __pa(MAX_DMA_ADDRESS));
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
/*
|
||||
|
@@ -197,6 +197,9 @@ void __init mmu_partition_table_init(void)
|
||||
BUILD_BUG_ON_MSG((PATB_SIZE_SHIFT > 36), "Partition table size too large.");
|
||||
/* Initialize the Partition Table with no entries */
|
||||
partition_tb = memblock_alloc(patb_size, patb_size);
|
||||
if (!partition_tb)
|
||||
panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
|
||||
__func__, patb_size, patb_size);
|
||||
|
||||
/*
|
||||
* update partition table control register,
|
||||
|
@@ -53,13 +53,20 @@ static __ref void *early_alloc_pgtable(unsigned long size, int nid,
|
||||
{
|
||||
phys_addr_t min_addr = MEMBLOCK_LOW_LIMIT;
|
||||
phys_addr_t max_addr = MEMBLOCK_ALLOC_ANYWHERE;
|
||||
void *ptr;
|
||||
|
||||
if (region_start)
|
||||
min_addr = region_start;
|
||||
if (region_end)
|
||||
max_addr = region_end;
|
||||
|
||||
return memblock_alloc_try_nid(size, size, min_addr, max_addr, nid);
|
||||
ptr = memblock_alloc_try_nid(size, size, min_addr, max_addr, nid);
|
||||
|
||||
if (!ptr)
|
||||
panic("%s: Failed to allocate %lu bytes align=0x%lx nid=%d from=%pa max_addr=%pa\n",
|
||||
__func__, size, size, nid, &min_addr, &max_addr);
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
static int early_map_kernel_page(unsigned long ea, unsigned long pa,
|
||||
|
@@ -340,6 +340,9 @@ void __init MMU_init_hw(void)
|
||||
*/
|
||||
if ( ppc_md.progress ) ppc_md.progress("hash:find piece", 0x322);
|
||||
Hash = memblock_alloc(Hash_size, Hash_size);
|
||||
if (!Hash)
|
||||
panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
|
||||
__func__, Hash_size, Hash_size);
|
||||
_SDR1 = __pa(Hash) | SDR1_LOW_BITS;
|
||||
|
||||
Hash_end = (struct hash_pte *) ((unsigned long)Hash + Hash_size);
|
||||
|
@@ -211,6 +211,9 @@ static int __init iob_init(struct device_node *dn)
|
||||
iob_l2_base = memblock_alloc_try_nid_raw(1UL << 21, 1UL << 21,
|
||||
MEMBLOCK_LOW_LIMIT, 0x80000000,
|
||||
NUMA_NO_NODE);
|
||||
if (!iob_l2_base)
|
||||
panic("%s: Failed to allocate %lu bytes align=0x%lx max_addr=%x\n",
|
||||
__func__, 1UL << 21, 1UL << 21, 0x80000000);
|
||||
|
||||
pr_info("IOBMAP L2 allocated at: %p\n", iob_l2_base);
|
||||
|
||||
|
@@ -519,6 +519,9 @@ static int __init core99_nvram_setup(struct device_node *dp, unsigned long addr)
|
||||
return -EINVAL;
|
||||
}
|
||||
nvram_image = memblock_alloc(NVRAM_SIZE, SMP_CACHE_BYTES);
|
||||
if (!nvram_image)
|
||||
panic("%s: Failed to allocate %u bytes\n", __func__,
|
||||
NVRAM_SIZE);
|
||||
nvram_data = ioremap(addr, NVRAM_SIZE*2);
|
||||
nvram_naddrs = 1; /* Make sure we get the correct case */
|
||||
|
||||
|
@@ -171,6 +171,9 @@ int __init early_init_dt_scan_recoverable_ranges(unsigned long node,
|
||||
* Allocate a buffer to hold the MC recoverable ranges.
|
||||
*/
|
||||
mc_recoverable_range = memblock_alloc(size, __alignof__(u64));
|
||||
if (!mc_recoverable_range)
|
||||
panic("%s: Failed to allocate %u bytes align=0x%lx\n",
|
||||
__func__, size, __alignof__(u64));
|
||||
|
||||
for (i = 0; i < mc_recoverable_range_len; i++) {
|
||||
mc_recoverable_range[i].start_addr =
|
||||
|
@@ -3657,6 +3657,9 @@ static void __init pnv_pci_init_ioda_phb(struct device_node *np,
|
||||
pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
|
||||
|
||||
phb = memblock_alloc(sizeof(*phb), SMP_CACHE_BYTES);
|
||||
if (!phb)
|
||||
panic("%s: Failed to allocate %zu bytes\n", __func__,
|
||||
sizeof(*phb));
|
||||
|
||||
/* Allocate PCI controller */
|
||||
phb->hose = hose = pcibios_alloc_controller(np);
|
||||
@@ -3703,6 +3706,9 @@ static void __init pnv_pci_init_ioda_phb(struct device_node *np,
|
||||
phb->diag_data_size = PNV_PCI_DIAG_BUF_SIZE;
|
||||
|
||||
phb->diag_data = memblock_alloc(phb->diag_data_size, SMP_CACHE_BYTES);
|
||||
if (!phb->diag_data)
|
||||
panic("%s: Failed to allocate %u bytes\n", __func__,
|
||||
phb->diag_data_size);
|
||||
|
||||
/* Parse 32-bit and IO ranges (if any) */
|
||||
pci_process_bridge_OF_ranges(hose, np, !hose->global_number);
|
||||
@@ -3762,6 +3768,8 @@ static void __init pnv_pci_init_ioda_phb(struct device_node *np,
|
||||
pemap_off = size;
|
||||
size += phb->ioda.total_pe_num * sizeof(struct pnv_ioda_pe);
|
||||
aux = memblock_alloc(size, SMP_CACHE_BYTES);
|
||||
if (!aux)
|
||||
panic("%s: Failed to allocate %lu bytes\n", __func__, size);
|
||||
phb->ioda.pe_alloc = aux;
|
||||
phb->ioda.m64_segmap = aux + m64map_off;
|
||||
phb->ioda.m32_segmap = aux + m32map_off;
|
||||
|
@@ -127,6 +127,9 @@ static void __init prealloc(struct ps3_prealloc *p)
|
||||
return;
|
||||
|
||||
p->address = memblock_alloc(p->size, p->align);
|
||||
if (!p->address)
|
||||
panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
|
||||
__func__, p->size, p->align);
|
||||
|
||||
printk(KERN_INFO "%s: %lu bytes at %p\n", p->name, p->size,
|
||||
p->address);
|
||||
|
@@ -129,6 +129,9 @@ int __ref msi_bitmap_alloc(struct msi_bitmap *bmp, unsigned int irq_count,
|
||||
bmp->bitmap = kzalloc(size, GFP_KERNEL);
|
||||
else {
|
||||
bmp->bitmap = memblock_alloc(size, SMP_CACHE_BYTES);
|
||||
if (!bmp->bitmap)
|
||||
panic("%s: Failed to allocate %u bytes\n", __func__,
|
||||
size);
|
||||
/* the bitmap won't be freed from memblock allocator */
|
||||
kmemleak_not_leak(bmp->bitmap);
|
||||
}
|
||||
|
Reference in New Issue
Block a user