Skip to content

Commit c6f2397

Browse files
guoweikangakpm00
authored andcommitted
mm/memblock: add memblock_alloc_or_panic interface
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]>
1 parent f8d4a6c commit c6f2397

File tree

65 files changed

+144
-431
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+144
-431
lines changed

arch/alpha/kernel/core_cia.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,7 @@ cia_prepare_tbia_workaround(int window)
331331
long i;
332332

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

340337
for (i = 0; i < CIA_BROKEN_TBIA_SIZE / sizeof(unsigned long); ++i)

arch/alpha/kernel/core_marvel.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,7 @@ mk_resource_name(int pe, int port, char *str)
8181
char *name;
8282

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

9087
return name;
@@ -119,10 +116,7 @@ alloc_io7(unsigned int pe)
119116
return NULL;
120117
}
121118

122-
io7 = memblock_alloc(sizeof(*io7), SMP_CACHE_BYTES);
123-
if (!io7)
124-
panic("%s: Failed to allocate %zu bytes\n", __func__,
125-
sizeof(*io7));
119+
io7 = memblock_alloc_or_panic(sizeof(*io7), SMP_CACHE_BYTES);
126120
io7->pe = pe;
127121
raw_spin_lock_init(&io7->irq_lock);
128122

arch/alpha/kernel/pci.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,7 @@ alloc_pci_controller(void)
391391
{
392392
struct pci_controller *hose;
393393

394-
hose = memblock_alloc(sizeof(*hose), SMP_CACHE_BYTES);
395-
if (!hose)
396-
panic("%s: Failed to allocate %zu bytes\n", __func__,
397-
sizeof(*hose));
394+
hose = memblock_alloc_or_panic(sizeof(*hose), SMP_CACHE_BYTES);
398395

399396
*hose_tail = hose;
400397
hose_tail = &hose->next;
@@ -405,13 +402,7 @@ alloc_pci_controller(void)
405402
struct resource * __init
406403
alloc_resource(void)
407404
{
408-
void *ptr = memblock_alloc(sizeof(struct resource), SMP_CACHE_BYTES);
409-
410-
if (!ptr)
411-
panic("%s: Failed to allocate %zu bytes\n", __func__,
412-
sizeof(struct resource));
413-
414-
return ptr;
405+
return memblock_alloc_or_panic(sizeof(struct resource), SMP_CACHE_BYTES);
415406
}
416407

417408

arch/alpha/kernel/pci_iommu.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,8 @@ iommu_arena_new_node(int nid, struct pci_controller *hose, dma_addr_t base,
7171
if (align < mem_size)
7272
align = mem_size;
7373

74-
arena = memblock_alloc(sizeof(*arena), SMP_CACHE_BYTES);
75-
if (!arena)
76-
panic("%s: Failed to allocate %zu bytes\n", __func__,
77-
sizeof(*arena));
78-
arena->ptes = memblock_alloc(mem_size, align);
79-
if (!arena->ptes)
80-
panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
81-
__func__, mem_size, align);
74+
arena = memblock_alloc_or_panic(sizeof(*arena), SMP_CACHE_BYTES);
75+
arena->ptes = memblock_alloc_or_panic(mem_size, align);
8276

8377
spin_lock_init(&arena->lock);
8478
arena->hose = hose;

arch/arm/kernel/setup.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -880,21 +880,15 @@ static void __init request_standard_resources(const struct machine_desc *mdesc)
880880
*/
881881
boot_alias_start = phys_to_idmap(start);
882882
if (arm_has_idmap_alias() && boot_alias_start != IDMAP_INVALID_ADDR) {
883-
res = memblock_alloc(sizeof(*res), SMP_CACHE_BYTES);
884-
if (!res)
885-
panic("%s: Failed to allocate %zu bytes\n",
886-
__func__, sizeof(*res));
883+
res = memblock_alloc_or_panic(sizeof(*res), SMP_CACHE_BYTES);
887884
res->name = "System RAM (boot alias)";
888885
res->start = boot_alias_start;
889886
res->end = phys_to_idmap(res_end);
890887
res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
891888
request_resource(&iomem_resource, res);
892889
}
893890

894-
res = memblock_alloc(sizeof(*res), SMP_CACHE_BYTES);
895-
if (!res)
896-
panic("%s: Failed to allocate %zu bytes\n", __func__,
897-
sizeof(*res));
891+
res = memblock_alloc_or_panic(sizeof(*res), SMP_CACHE_BYTES);
898892
res->name = "System RAM";
899893
res->start = start;
900894
res->end = res_end;

arch/arm/mm/mmu.c

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -726,13 +726,8 @@ EXPORT_SYMBOL(phys_mem_access_prot);
726726

727727
static void __init *early_alloc(unsigned long sz)
728728
{
729-
void *ptr = memblock_alloc(sz, sz);
729+
return memblock_alloc_or_panic(sz, sz);
730730

731-
if (!ptr)
732-
panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
733-
__func__, sz, sz);
734-
735-
return ptr;
736731
}
737732

738733
static void *__init late_alloc(unsigned long sz)
@@ -1027,10 +1022,7 @@ void __init iotable_init(struct map_desc *io_desc, int nr)
10271022
if (!nr)
10281023
return;
10291024

1030-
svm = memblock_alloc(sizeof(*svm) * nr, __alignof__(*svm));
1031-
if (!svm)
1032-
panic("%s: Failed to allocate %zu bytes align=0x%zx\n",
1033-
__func__, sizeof(*svm) * nr, __alignof__(*svm));
1025+
svm = memblock_alloc_or_panic(sizeof(*svm) * nr, __alignof__(*svm));
10341026

10351027
for (md = io_desc; nr; md++, nr--) {
10361028
create_mapping(md);
@@ -1052,10 +1044,7 @@ void __init vm_reserve_area_early(unsigned long addr, unsigned long size,
10521044
struct vm_struct *vm;
10531045
struct static_vm *svm;
10541046

1055-
svm = memblock_alloc(sizeof(*svm), __alignof__(*svm));
1056-
if (!svm)
1057-
panic("%s: Failed to allocate %zu bytes align=0x%zx\n",
1058-
__func__, sizeof(*svm), __alignof__(*svm));
1047+
svm = memblock_alloc_or_panic(sizeof(*svm), __alignof__(*svm));
10591048

10601049
vm = &svm->vm;
10611050
vm->addr = (void *)addr;

arch/arm/mm/nommu.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,7 @@ void __init paging_init(const struct machine_desc *mdesc)
162162
mpu_setup();
163163

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

170167
bootmem_init();
171168

arch/arm64/kernel/setup.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,7 @@ static void __init request_standard_resources(void)
223223

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

230228
for_each_mem_region(region) {
231229
res = &standard_resources[i++];

arch/loongarch/kernel/setup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ static void __init resource_init(void)
431431

432432
num_standard_resources = memblock.memory.cnt;
433433
res_size = num_standard_resources * sizeof(*standard_resources);
434-
standard_resources = memblock_alloc(res_size, SMP_CACHE_BYTES);
434+
standard_resources = memblock_alloc_or_panic(res_size, SMP_CACHE_BYTES);
435435

436436
for_each_mem_region(region) {
437437
res = &standard_resources[i++];

arch/loongarch/mm/init.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,7 @@ pte_t * __init populate_kernel_pte(unsigned long addr)
174174
pmd_t *pmd;
175175

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

186184
pud = pud_offset(p4d, addr);
187185
if (pud_none(pudp_get(pud))) {
188-
pmd = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
189-
if (!pmd)
190-
panic("%s: Failed to allocate memory\n", __func__);
186+
pmd = memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);
191187
pud_populate(&init_mm, pud, pmd);
192188
#ifndef __PAGETABLE_PMD_FOLDED
193189
pmd_init(pmd);
@@ -198,10 +194,7 @@ pte_t * __init populate_kernel_pte(unsigned long addr)
198194
if (!pmd_present(pmdp_get(pmd))) {
199195
pte_t *pte;
200196

201-
pte = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
202-
if (!pte)
203-
panic("%s: Failed to allocate memory\n", __func__);
204-
197+
pte = memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);
205198
pmd_populate_kernel(&init_mm, pmd, pte);
206199
kernel_pte_init(pte);
207200
}

arch/m68k/mm/init.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,7 @@ void __init paging_init(void)
6868

6969
high_memory = (void *) end_mem;
7070

71-
empty_zero_page = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
72-
if (!empty_zero_page)
73-
panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
74-
__func__, PAGE_SIZE, PAGE_SIZE);
71+
empty_zero_page = memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);
7572
max_zone_pfn[ZONE_DMA] = end_mem >> PAGE_SHIFT;
7673
free_area_init(max_zone_pfn);
7774
}

arch/m68k/mm/mcfmmu.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,14 @@ void __init paging_init(void)
4242
unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0 };
4343
int i;
4444

45-
empty_zero_page = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
46-
if (!empty_zero_page)
47-
panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
48-
__func__, PAGE_SIZE, PAGE_SIZE);
45+
empty_zero_page = memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);
4946

5047
pg_dir = swapper_pg_dir;
5148
memset(swapper_pg_dir, 0, sizeof(swapper_pg_dir));
5249

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

6054
pg_dir += PAGE_OFFSET >> PGDIR_SHIFT;
6155

arch/m68k/mm/motorola.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -500,10 +500,7 @@ void __init paging_init(void)
500500
* initialize the bad page table and bad page to point
501501
* to a couple of allocated pages
502502
*/
503-
empty_zero_page = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
504-
if (!empty_zero_page)
505-
panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
506-
__func__, PAGE_SIZE, PAGE_SIZE);
503+
empty_zero_page = memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);
507504

508505
/*
509506
* Set up SFC/DFC registers

arch/m68k/mm/sun3mmu.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,7 @@ void __init paging_init(void)
4444
unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0, };
4545
unsigned long size;
4646

47-
empty_zero_page = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
48-
if (!empty_zero_page)
49-
panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
50-
__func__, PAGE_SIZE, PAGE_SIZE);
47+
empty_zero_page = memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);
5148

5249
address = PAGE_OFFSET;
5350
pg_dir = swapper_pg_dir;
@@ -57,10 +54,7 @@ void __init paging_init(void)
5754
size = num_pages * sizeof(pte_t);
5855
size = (size + PAGE_SIZE) & ~(PAGE_SIZE-1);
5956

60-
next_pgtable = (unsigned long)memblock_alloc(size, PAGE_SIZE);
61-
if (!next_pgtable)
62-
panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
63-
__func__, size, PAGE_SIZE);
57+
next_pgtable = (unsigned long)memblock_alloc_or_panic(size, PAGE_SIZE);
6458
bootmem_end = (next_pgtable + size + PAGE_SIZE) & PAGE_MASK;
6559

6660
/* Map whole memory from PAGE_OFFSET (0x0E000000) */

arch/m68k/sun3/sun3dvma.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,8 @@ void __init dvma_init(void)
252252

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

255-
iommu_use = memblock_alloc(IOMMU_TOTAL_ENTRIES * sizeof(unsigned long),
255+
iommu_use = memblock_alloc_or_panic(IOMMU_TOTAL_ENTRIES * sizeof(unsigned long),
256256
SMP_CACHE_BYTES);
257-
if (!iommu_use)
258-
panic("%s: Failed to allocate %zu bytes\n", __func__,
259-
IOMMU_TOTAL_ENTRIES * sizeof(unsigned long));
260-
261257
dvma_unmap_iommu(DVMA_START, DVMA_SIZE);
262258

263259
sun3_dvma_init();

arch/mips/kernel/setup.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -704,10 +704,7 @@ static void __init resource_init(void)
704704
for_each_mem_range(i, &start, &end) {
705705
struct resource *res;
706706

707-
res = memblock_alloc(sizeof(struct resource), SMP_CACHE_BYTES);
708-
if (!res)
709-
panic("%s: Failed to allocate %zu bytes\n", __func__,
710-
sizeof(struct resource));
707+
res = memblock_alloc_or_panic(sizeof(struct resource), SMP_CACHE_BYTES);
711708

712709
res->start = start;
713710
/*

arch/openrisc/mm/ioremap.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ pte_t __ref *pte_alloc_one_kernel(struct mm_struct *mm)
3838
if (likely(mem_init_done)) {
3939
pte = (pte_t *)get_zeroed_page(GFP_KERNEL);
4040
} else {
41-
pte = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
42-
if (!pte)
43-
panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
44-
__func__, PAGE_SIZE, PAGE_SIZE);
41+
pte = memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);
4542
}
4643

4744
return pte;

arch/parisc/mm/init.c

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -377,20 +377,16 @@ static void __ref map_pages(unsigned long start_vaddr,
377377

378378
#if CONFIG_PGTABLE_LEVELS == 3
379379
if (pud_none(*pud)) {
380-
pmd = memblock_alloc(PAGE_SIZE << PMD_TABLE_ORDER,
380+
pmd = memblock_alloc_or_panic(PAGE_SIZE << PMD_TABLE_ORDER,
381381
PAGE_SIZE << PMD_TABLE_ORDER);
382-
if (!pmd)
383-
panic("pmd allocation failed.\n");
384382
pud_populate(NULL, pud, pmd);
385383
}
386384
#endif
387385

388386
pmd = pmd_offset(pud, vaddr);
389387
for (tmp1 = start_pmd; tmp1 < PTRS_PER_PMD; tmp1++, pmd++) {
390388
if (pmd_none(*pmd)) {
391-
pg_table = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
392-
if (!pg_table)
393-
panic("page table allocation failed\n");
389+
pg_table = memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);
394390
pmd_populate_kernel(NULL, pmd, pg_table);
395391
}
396392

@@ -648,9 +644,7 @@ static void __init pagetable_init(void)
648644
}
649645
#endif
650646

651-
empty_zero_page = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
652-
if (!empty_zero_page)
653-
panic("zero page allocation failed.\n");
647+
empty_zero_page = memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);
654648

655649
}
656650

@@ -687,19 +681,15 @@ static void __init fixmap_init(void)
687681

688682
#if CONFIG_PGTABLE_LEVELS == 3
689683
if (pud_none(*pud)) {
690-
pmd = memblock_alloc(PAGE_SIZE << PMD_TABLE_ORDER,
684+
pmd = memblock_alloc_or_panic(PAGE_SIZE << PMD_TABLE_ORDER,
691685
PAGE_SIZE << PMD_TABLE_ORDER);
692-
if (!pmd)
693-
panic("fixmap: pmd allocation failed.\n");
694686
pud_populate(NULL, pud, pmd);
695687
}
696688
#endif
697689

698690
pmd = pmd_offset(pud, addr);
699691
do {
700-
pte_t *pte = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
701-
if (!pte)
702-
panic("fixmap: pte allocation failed.\n");
692+
pte_t *pte = memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);
703693

704694
pmd_populate_kernel(&init_mm, pmd, pte);
705695

arch/powerpc/kernel/dt_cpu_ftrs.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,12 +1087,10 @@ static int __init dt_cpu_ftrs_scan_callback(unsigned long node, const char
10871087
/* Count and allocate space for cpu features */
10881088
of_scan_flat_dt_subnodes(node, count_cpufeatures_subnodes,
10891089
&nr_dt_cpu_features);
1090-
dt_cpu_features = memblock_alloc(sizeof(struct dt_cpu_feature) * nr_dt_cpu_features, PAGE_SIZE);
1091-
if (!dt_cpu_features)
1092-
panic("%s: Failed to allocate %zu bytes align=0x%lx\n",
1093-
__func__,
1094-
sizeof(struct dt_cpu_feature) * nr_dt_cpu_features,
1095-
PAGE_SIZE);
1090+
dt_cpu_features =
1091+
memblock_alloc_or_panic(
1092+
sizeof(struct dt_cpu_feature) * nr_dt_cpu_features,
1093+
PAGE_SIZE);
10961094

10971095
cpufeatures_setup_start(isa);
10981096

0 commit comments

Comments
 (0)