Commit c6f23979 authored by Guo Weikang's avatar Guo Weikang Committed by Andrew Morton
Browse files

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.

[guoweikang.kernel@gmail.com: arch/s390: save_area_alloc default failure behavior changed to panic]
  Link: https://lkml.kernel.org/r/20250109033136.2845676-1-guoweikang.kernel@gmail.com
  Link: https://lore.kernel.org/lkml/Z2fknmnNtiZbCc7x@kernel.org/
Link: https://lkml.kernel.org/r/20250102072528.650926-1-guoweikang.kernel@gmail.com


Signed-off-by: default avatarGuo Weikang <guoweikang.kernel@gmail.com>
Acked-by: Geert Uytterhoeven <geert@linux-m68k.org>	[m68k]
Reviewed-by: Alexander Gordeev <agordeev@linux.ibm.com>	[s390]
Acked-by: default avatarMike Rapoport (Microsoft) <rppt@kernel.org>
Cc: Alexander Gordeev <agordeev@linux.ibm.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent f8d4a6ca
Loading
Loading
Loading
Loading
+1 −4
Original line number Diff line number Diff line
@@ -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)
+2 −8
Original line number Diff line number Diff line
@@ -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;
@@ -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);

+2 −11
Original line number Diff line number Diff line
@@ -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;
@@ -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);
}


+2 −8
Original line number Diff line number Diff line
@@ -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;
+2 −8
Original line number Diff line number Diff line
@@ -880,10 +880,7 @@ 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);
@@ -891,10 +888,7 @@ static void __init request_standard_resources(const struct machine_desc *mdesc)
			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;
Loading