Thread (3 messages) 3 messages, 2 authors, 2024-12-21

Re: [PATCH] mm/memblock: Add memblock_alloc_or_panic interface

From: Weikang Guo <hidden>
Date: 2024-12-21 09:59:54
Also in: linux-acpi, linux-alpha, linux-clk, linux-devicetree, linux-m68k, linux-mips, linux-mm, linux-omap, linux-pm, linux-riscv, linux-s390, linux-sh, linux-um, lkml, loongarch, sparclinux, xen-devel

Andrew Morton [off-list ref] wrote on Saturday, 21
December 2024 07:06:
On Fri, 20 Dec 2024 17:26:38 +0800 Guo Weikang [off-list ref] wrote:
quoted
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.
Seems nice.
quoted
...
--- a/include/linux/memblock.h
+++ b/include/linux/memblock.h
@@ -417,6 +417,19 @@ static __always_inline void *memblock_alloc(phys_addr_t size, phys_addr_t align)
                                    MEMBLOCK_ALLOC_ACCESSIBLE, NUMA_NO_NODE);
 }

+static __always_inline void *memblock_alloc_or_panic(phys_addr_t size, phys_addr_t align)
We lost the printing of the function name, but it's easy to retain with
something like

#define memblock_alloc_or_panic(size, align)    \
                __memblock_alloc_or_panic(size, align, __func__)
You're absolutely right, this was an oversight on my part. I’ll make
sure to update it with the correct function name.
quoted
+{
+     void *addr = memblock_alloc(size, align);
+
+     if (unlikely(!addr))
+#ifdef CONFIG_PHYS_ADDR_T_64BIT
+             panic("%s: Failed to allocate %llu bytes\n", __func__, size);
Won't this always print "memblock_alloc_or_panic: Failed ..."?  Not
very useful.
As mentioned above.
quoted
+#else
+             panic("%s: Failed to allocate %u bytes\n", __func__, size);
+#endif
We can avoid the ifdef with printk's "%pap"?
I appreciate you pointing this out. I wasn’t aware of this approach,
but it’s a great idea. It definitely simplifies things, and I’ve
learned something new in the process. I'll incorporate this into the
code.
quoted
+     return addr;
+}
  
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help