diff mbox series

[2/3] mm/memblock: Modify the default failure behavior of memblock_alloc_raw to panic

Message ID 20250103105158.1350689-2-guoweikang.kernel@gmail.com (mailing list archive)
State New
Headers show
Series [1/3] mm/memblock: Modify the default failure behavior of memblock_alloc to panic | expand

Commit Message

Weikang Guo Jan. 3, 2025, 10:51 a.m. UTC
Just like memblock_alloc, the default failure behavior of memblock_alloc_raw
is now modified to trigger a panic when allocation fails.

memblock_alloc_no_panic has been introduced to handle cases where panic
behavior is not desired.

Signed-off-by: Guo Weikang <guoweikang.kernel@gmail.com>
---
 arch/openrisc/mm/init.c                |  3 ---
 arch/powerpc/kernel/paca.c             |  4 ----
 arch/powerpc/kernel/prom.c             |  3 ---
 arch/powerpc/platforms/pseries/plpks.c |  2 +-
 include/linux/memblock.h               | 17 +++++++----------
 mm/memblock.c                          | 13 +++++++++++--
 6 files changed, 19 insertions(+), 23 deletions(-)
diff mbox series

Patch

diff --git a/arch/openrisc/mm/init.c b/arch/openrisc/mm/init.c
index d0cb1a0126f9..9e0047764f54 100644
--- a/arch/openrisc/mm/init.c
+++ b/arch/openrisc/mm/init.c
@@ -96,9 +96,6 @@  static void __init map_ram(void)
 
 			/* Alloc one page for holding PTE's... */
 			pte = memblock_alloc_raw(PAGE_SIZE, PAGE_SIZE);
-			if (!pte)
-				panic("%s: Failed to allocate page for PTEs\n",
-				      __func__);
 			set_pmd(pme, __pmd(_KERNPG_TABLE + __pa(pte)));
 
 			/* Fill the newly allocated page with PTE'S */
diff --git a/arch/powerpc/kernel/paca.c b/arch/powerpc/kernel/paca.c
index 7502066c3c53..9d15799e97d4 100644
--- a/arch/powerpc/kernel/paca.c
+++ b/arch/powerpc/kernel/paca.c
@@ -246,10 +246,6 @@  void __init allocate_paca_ptrs(void)
 
 	paca_ptrs_size = sizeof(struct paca_struct *) * nr_cpu_ids;
 	paca_ptrs = memblock_alloc_raw(paca_ptrs_size, SMP_CACHE_BYTES);
-	if (!paca_ptrs)
-		panic("Failed to allocate %d bytes for paca pointers\n",
-		      paca_ptrs_size);
-
 	memset(paca_ptrs, 0x88, paca_ptrs_size);
 }
 
diff --git a/arch/powerpc/kernel/prom.c b/arch/powerpc/kernel/prom.c
index e0059842a1c6..3aba66ddd2c8 100644
--- a/arch/powerpc/kernel/prom.c
+++ b/arch/powerpc/kernel/prom.c
@@ -128,9 +128,6 @@  static void __init move_device_tree(void)
 	    !memblock_is_memory(start + size - 1) ||
 	    overlaps_crashkernel(start, size) || overlaps_initrd(start, size)) {
 		p = memblock_alloc_raw(size, PAGE_SIZE);
-		if (!p)
-			panic("Failed to allocate %lu bytes to move device tree\n",
-			      size);
 		memcpy(p, initial_boot_params, size);
 		initial_boot_params = p;
 		DBG("Moved device tree to 0x%px\n", p);
diff --git a/arch/powerpc/platforms/pseries/plpks.c b/arch/powerpc/platforms/pseries/plpks.c
index b1667ed05f98..1bcbed41ce44 100644
--- a/arch/powerpc/platforms/pseries/plpks.c
+++ b/arch/powerpc/platforms/pseries/plpks.c
@@ -671,7 +671,7 @@  void __init plpks_early_init_devtree(void)
 		return;
 	}
 
-	ospassword = memblock_alloc_raw(len, SMP_CACHE_BYTES);
+	ospassword = memblock_alloc_raw_no_panic(len, SMP_CACHE_BYTES);
 	if (!ospassword) {
 		pr_err("Error allocating memory for password.\n");
 		goto out;
diff --git a/include/linux/memblock.h b/include/linux/memblock.h
index 6b21a3834225..b68c141ebc44 100644
--- a/include/linux/memblock.h
+++ b/include/linux/memblock.h
@@ -418,20 +418,17 @@  static __always_inline void *memblock_alloc(phys_addr_t size, phys_addr_t align)
 }
 
 void *__memblock_alloc_panic(phys_addr_t size, phys_addr_t align,
-				const char *func, bool should_panic);
+			     const char *func, bool should_panic, bool raw);
 
 #define memblock_alloc(size, align)    \
-	 __memblock_alloc_panic(size, align, __func__, true)
+	 __memblock_alloc_panic(size, align, __func__, true, false)
 #define memblock_alloc_no_panic(size, align)    \
-	 __memblock_alloc_panic(size, align, __func__, false)
+	 __memblock_alloc_panic(size, align, __func__, false, false)
 
-static inline void *memblock_alloc_raw(phys_addr_t size,
-					       phys_addr_t align)
-{
-	return memblock_alloc_try_nid_raw(size, align, MEMBLOCK_LOW_LIMIT,
-					  MEMBLOCK_ALLOC_ACCESSIBLE,
-					  NUMA_NO_NODE);
-}
+#define memblock_alloc_raw(size, align)    \
+	 __memblock_alloc_panic(size, align, __func__, true, true)
+#define memblock_alloc_raw_no_panic(size, align)    \
+	 __memblock_alloc_panic(size, align, __func__, false, true)
 
 static inline void *memblock_alloc_from(phys_addr_t size,
 						phys_addr_t align,
diff --git a/mm/memblock.c b/mm/memblock.c
index 901da45ecf8b..4974ae2ee5ec 100644
--- a/mm/memblock.c
+++ b/mm/memblock.c
@@ -1697,15 +1697,24 @@  void * __init memblock_alloc_try_nid(
  * @align: alignment of the region and block's size
  * @func: caller func name
  * @should_panic: whether failed panic
+ * @raw: whether zeroing mem
  *
  * In case of failure, it calls panic with the formatted message.
  * This function should not be used directly, please use the macro
  * memblock_alloc and memblock_alloc_no_panic.
+ * memblock_alloc_raw and memblock_alloc_raw_no_panic.
  */
 void *__init __memblock_alloc_panic(phys_addr_t size, phys_addr_t align,
-				    const char *func, bool should_panic)
+				    const char *func, bool should_panic,
+				    bool raw)
 {
-	void *addr = memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT,
+	void *addr;
+
+	if (unlikely(raw))
+		addr = memblock_alloc_try_nid_raw(size, align, MEMBLOCK_LOW_LIMIT,
+				      MEMBLOCK_ALLOC_ACCESSIBLE, NUMA_NO_NODE);
+	else
+		addr = memblock_alloc_try_nid(size, align, MEMBLOCK_LOW_LIMIT,
 				      MEMBLOCK_ALLOC_ACCESSIBLE, NUMA_NO_NODE);
 
 	if (unlikely(!addr && should_panic))