Skip to content

Commit

Permalink
mm/memblock: add memblock_alloc_or_panic interface
Browse files Browse the repository at this point in the history
Before SLUB initialization, various subsystems used memblock_alloc to
allocate memory.  In most cases, when memory allocation fails, an
immediate panic is required.  To simplify this behavior and reduce
repetitive checks, introduce `memblock_alloc_or_panic`.  This function
ensures that memory allocation failures result in a panic automatically,
improving code readability and consistency across subsystems that require
this behavior.

[[email protected]: arch/s390: save_area_alloc default failure behavior changed to panic]
  Link: https://lkml.kernel.org/r/[email protected]
  Link: https://lore.kernel.org/lkml/[email protected]/
Link: https://lkml.kernel.org/r/[email protected]
Signed-off-by: Guo Weikang <[email protected]>
Acked-by: Geert Uytterhoeven <[email protected]>	[m68k]
Reviewed-by: Alexander Gordeev <[email protected]>	[s390]
Acked-by: Mike Rapoport (Microsoft) <[email protected]>
Cc: Alexander Gordeev <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
  • Loading branch information
guoweikang authored and akpm00 committed Jan 26, 2025
1 parent f8d4a6c commit c6f2397
Show file tree
Hide file tree
Showing 65 changed files with 144 additions and 431 deletions.
5 changes: 1 addition & 4 deletions arch/alpha/kernel/core_cia.c
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,7 @@ cia_prepare_tbia_workaround(int window)
long i;

/* 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);
ppte = memblock_alloc_or_panic(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
10 changes: 2 additions & 8 deletions arch/alpha/kernel/core_marvel.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ mk_resource_name(int pe, int port, char *str)
char *name;

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);
name = memblock_alloc_or_panic(strlen(tmp) + 1, SMP_CACHE_BYTES);
strcpy(name, tmp);

return name;
Expand Down Expand Up @@ -119,10 +116,7 @@ alloc_io7(unsigned int pe)
return NULL;
}

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

Expand Down
13 changes: 2 additions & 11 deletions arch/alpha/kernel/pci.c
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,7 @@ 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 = memblock_alloc_or_panic(sizeof(*hose), SMP_CACHE_BYTES);

*hose_tail = hose;
hose_tail = &hose->next;
Expand All @@ -405,13 +402,7 @@ alloc_pci_controller(void)
struct resource * __init
alloc_resource(void)
{
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;
return memblock_alloc_or_panic(sizeof(struct resource), SMP_CACHE_BYTES);
}


Expand Down
10 changes: 2 additions & 8 deletions arch/alpha/kernel/pci_iommu.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,8 @@ iommu_arena_new_node(int nid, struct pci_controller *hose, dma_addr_t base,
if (align < mem_size)
align = mem_size;

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);
arena = memblock_alloc_or_panic(sizeof(*arena), SMP_CACHE_BYTES);
arena->ptes = memblock_alloc_or_panic(mem_size, align);

spin_lock_init(&arena->lock);
arena->hose = hose;
Expand Down
10 changes: 2 additions & 8 deletions arch/arm/kernel/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -880,21 +880,15 @@ 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 = memblock_alloc_or_panic(sizeof(*res), SMP_CACHE_BYTES);
res->name = "System RAM (boot alias)";
res->start = boot_alias_start;
res->end = phys_to_idmap(res_end);
res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
request_resource(&iomem_resource, res);
}

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

static void __init *early_alloc(unsigned long sz)
{
void *ptr = memblock_alloc(sz, sz);
return memblock_alloc_or_panic(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 @@ -1027,10 +1022,7 @@ void __init iotable_init(struct map_desc *io_desc, int nr)
if (!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));
svm = memblock_alloc_or_panic(sizeof(*svm) * nr, __alignof__(*svm));

for (md = io_desc; nr; md++, nr--) {
create_mapping(md);
Expand All @@ -1052,10 +1044,7 @@ void __init vm_reserve_area_early(unsigned long addr, unsigned long size,
struct vm_struct *vm;
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));
svm = memblock_alloc_or_panic(sizeof(*svm), __alignof__(*svm));

vm = &svm->vm;
vm->addr = (void *)addr;
Expand Down
5 changes: 1 addition & 4 deletions arch/arm/mm/nommu.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,7 @@ void __init paging_init(const struct machine_desc *mdesc)
mpu_setup();

/* allocate the zero page. */
zero_page = (void *)memblock_alloc(PAGE_SIZE, PAGE_SIZE);
if (!zero_page)
panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
__func__, PAGE_SIZE, PAGE_SIZE);
zero_page = (void *)memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);

bootmem_init();

Expand Down
4 changes: 1 addition & 3 deletions arch/arm64/kernel/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,7 @@ static void __init request_standard_resources(void)

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

for_each_mem_region(region) {
res = &standard_resources[i++];
Expand Down
2 changes: 1 addition & 1 deletion arch/loongarch/kernel/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ static void __init resource_init(void)

num_standard_resources = memblock.memory.cnt;
res_size = num_standard_resources * sizeof(*standard_resources);
standard_resources = memblock_alloc(res_size, SMP_CACHE_BYTES);
standard_resources = memblock_alloc_or_panic(res_size, SMP_CACHE_BYTES);

for_each_mem_region(region) {
res = &standard_resources[i++];
Expand Down
13 changes: 3 additions & 10 deletions arch/loongarch/mm/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,7 @@ pte_t * __init populate_kernel_pte(unsigned long addr)
pmd_t *pmd;

if (p4d_none(p4dp_get(p4d))) {
pud = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
if (!pud)
panic("%s: Failed to allocate memory\n", __func__);
pud = memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);
p4d_populate(&init_mm, p4d, pud);
#ifndef __PAGETABLE_PUD_FOLDED
pud_init(pud);
Expand All @@ -185,9 +183,7 @@ pte_t * __init populate_kernel_pte(unsigned long addr)

pud = pud_offset(p4d, addr);
if (pud_none(pudp_get(pud))) {
pmd = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
if (!pmd)
panic("%s: Failed to allocate memory\n", __func__);
pmd = memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);
pud_populate(&init_mm, pud, pmd);
#ifndef __PAGETABLE_PMD_FOLDED
pmd_init(pmd);
Expand All @@ -198,10 +194,7 @@ pte_t * __init populate_kernel_pte(unsigned long addr)
if (!pmd_present(pmdp_get(pmd))) {
pte_t *pte;

pte = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
if (!pte)
panic("%s: Failed to allocate memory\n", __func__);

pte = memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);
pmd_populate_kernel(&init_mm, pmd, pte);
kernel_pte_init(pte);
}
Expand Down
5 changes: 1 addition & 4 deletions arch/m68k/mm/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ 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);
empty_zero_page = memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);
max_zone_pfn[ZONE_DMA] = end_mem >> PAGE_SHIFT;
free_area_init(max_zone_pfn);
}
Expand Down
10 changes: 2 additions & 8 deletions arch/m68k/mm/mcfmmu.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,14 @@ void __init paging_init(void)
unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0 };
int i;

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);
empty_zero_page = memblock_alloc_or_panic(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);
next_pgtable = (unsigned long) memblock_alloc_or_panic(size, PAGE_SIZE);

pg_dir += PAGE_OFFSET >> PGDIR_SHIFT;

Expand Down
5 changes: 1 addition & 4 deletions arch/m68k/mm/motorola.c
Original file line number Diff line number Diff line change
Expand Up @@ -500,10 +500,7 @@ void __init paging_init(void)
* initialize the bad page table and bad page to point
* 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);
empty_zero_page = memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);

/*
* Set up SFC/DFC registers
Expand Down
10 changes: 2 additions & 8 deletions arch/m68k/mm/sun3mmu.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ void __init paging_init(void)
unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0, };
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);
empty_zero_page = memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);

address = PAGE_OFFSET;
pg_dir = swapper_pg_dir;
Expand All @@ -57,10 +54,7 @@ void __init paging_init(void)
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);
next_pgtable = (unsigned long)memblock_alloc_or_panic(size, PAGE_SIZE);
bootmem_end = (next_pgtable + size + PAGE_SIZE) & PAGE_MASK;

/* Map whole memory from PAGE_OFFSET (0x0E000000) */
Expand Down
6 changes: 1 addition & 5 deletions arch/m68k/sun3/sun3dvma.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,8 @@ void __init dvma_init(void)

list_add(&(hole->list), &hole_list);

iommu_use = memblock_alloc(IOMMU_TOTAL_ENTRIES * sizeof(unsigned long),
iommu_use = memblock_alloc_or_panic(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);

sun3_dvma_init();
Expand Down
5 changes: 1 addition & 4 deletions arch/mips/kernel/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -704,10 +704,7 @@ static void __init resource_init(void)
for_each_mem_range(i, &start, &end) {
struct resource *res;

res = memblock_alloc(sizeof(struct resource), SMP_CACHE_BYTES);
if (!res)
panic("%s: Failed to allocate %zu bytes\n", __func__,
sizeof(struct resource));
res = memblock_alloc_or_panic(sizeof(struct resource), SMP_CACHE_BYTES);

res->start = start;
/*
Expand Down
5 changes: 1 addition & 4 deletions arch/openrisc/mm/ioremap.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ pte_t __ref *pte_alloc_one_kernel(struct mm_struct *mm)
if (likely(mem_init_done)) {
pte = (pte_t *)get_zeroed_page(GFP_KERNEL);
} else {
pte = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
if (!pte)
panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
__func__, PAGE_SIZE, PAGE_SIZE);
pte = memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);
}

return pte;
Expand Down
20 changes: 5 additions & 15 deletions arch/parisc/mm/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -377,20 +377,16 @@ static void __ref map_pages(unsigned long start_vaddr,

#if CONFIG_PGTABLE_LEVELS == 3
if (pud_none(*pud)) {
pmd = memblock_alloc(PAGE_SIZE << PMD_TABLE_ORDER,
pmd = memblock_alloc_or_panic(PAGE_SIZE << PMD_TABLE_ORDER,
PAGE_SIZE << PMD_TABLE_ORDER);
if (!pmd)
panic("pmd allocation failed.\n");
pud_populate(NULL, pud, pmd);
}
#endif

pmd = pmd_offset(pud, vaddr);
for (tmp1 = start_pmd; tmp1 < PTRS_PER_PMD; tmp1++, pmd++) {
if (pmd_none(*pmd)) {
pg_table = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
if (!pg_table)
panic("page table allocation failed\n");
pg_table = memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);
pmd_populate_kernel(NULL, pmd, pg_table);
}

Expand Down Expand Up @@ -648,9 +644,7 @@ static void __init pagetable_init(void)
}
#endif

empty_zero_page = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
if (!empty_zero_page)
panic("zero page allocation failed.\n");
empty_zero_page = memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);

}

Expand Down Expand Up @@ -687,19 +681,15 @@ static void __init fixmap_init(void)

#if CONFIG_PGTABLE_LEVELS == 3
if (pud_none(*pud)) {
pmd = memblock_alloc(PAGE_SIZE << PMD_TABLE_ORDER,
pmd = memblock_alloc_or_panic(PAGE_SIZE << PMD_TABLE_ORDER,
PAGE_SIZE << PMD_TABLE_ORDER);
if (!pmd)
panic("fixmap: pmd allocation failed.\n");
pud_populate(NULL, pud, pmd);
}
#endif

pmd = pmd_offset(pud, addr);
do {
pte_t *pte = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
if (!pte)
panic("fixmap: pte allocation failed.\n");
pte_t *pte = memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);

pmd_populate_kernel(&init_mm, pmd, pte);

Expand Down
10 changes: 4 additions & 6 deletions arch/powerpc/kernel/dt_cpu_ftrs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1087,12 +1087,10 @@ static int __init dt_cpu_ftrs_scan_callback(unsigned long node, const char
/* Count and allocate space for cpu features */
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);
dt_cpu_features =
memblock_alloc_or_panic(
sizeof(struct dt_cpu_feature) * nr_dt_cpu_features,
PAGE_SIZE);

cpufeatures_setup_start(isa);

Expand Down
Loading

0 comments on commit c6f2397

Please sign in to comment.