[PATCH v3 11/17] crash: Introduce crash_extra_elfcorehdr_size() helper
From: Jinjie Ruan
Date: Wed Aug 26 2026 - 05:51:16 EST
Extract the elfcorehdr extra space calculation from powerpc into a
generic helper crash_extra_elfcorehdr_size() for use by other
architectures.
The helper includes compile-time and runtime checks, it also checks
whether the total number of program headers (including fixed headers for
kernel_map, VMCOREINFO, and CPU notes) exceeds PN_XNUM, and provides a
stub when crash/memory hotplug is disabled.
Cc: Madhavan Srinivasan <maddy@xxxxxxxxxxxxx>
Cc: Michael Ellerman <mpe@xxxxxxxxxxxxxx>
Cc: Nicholas Piggin <npiggin@xxxxxxxxx>
Cc: "Christophe Leroy (CS GROUP)" <chleroy@xxxxxxxxxx>
Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Cc: Baoquan He <baoquan.he@xxxxxxxxx>
Cc: Mike Rapoport <rppt@xxxxxxxxxx>
Cc: Pasha Tatashin <pasha.tatashin@xxxxxxxxxx>
Cc: Pratyush Yadav <pratyush@xxxxxxxxxx>
Cc: Dave Young <ruirui.yang@xxxxxxxxx>
Cc: Sourabh Jain <sourabhjain@xxxxxxxxxxxxx>
Signed-off-by: Jinjie Ruan <ruanjinjie@xxxxxxxxxx>
---
arch/powerpc/kexec/file_load_64.c | 19 +------------------
include/linux/crash_core.h | 26 ++++++++++++++++++++++++++
2 files changed, 27 insertions(+), 18 deletions(-)
diff --git a/arch/powerpc/kexec/file_load_64.c b/arch/powerpc/kexec/file_load_64.c
index 6075b1c88511..2b0325e4a628 100644
--- a/arch/powerpc/kexec/file_load_64.c
+++ b/arch/powerpc/kexec/file_load_64.c
@@ -374,23 +374,6 @@ static int load_backup_segment(struct kimage *image, struct kexec_buf *kbuf)
return 0;
}
-static unsigned int kdump_extra_elfcorehdr_size(struct crash_mem *cmem)
-{
-#if defined(CONFIG_CRASH_HOTPLUG) && defined(CONFIG_MEMORY_HOTPLUG)
- unsigned int extra_sz = 0;
-
- if (CONFIG_CRASH_MAX_MEMORY_RANGES > (unsigned int)PN_XNUM)
- pr_warn("Number of Phdrs %u exceeds max\n", CONFIG_CRASH_MAX_MEMORY_RANGES);
- else if (cmem->nr_ranges >= CONFIG_CRASH_MAX_MEMORY_RANGES)
- pr_warn("Configured crash mem ranges may not be enough\n");
- else
- extra_sz = (CONFIG_CRASH_MAX_MEMORY_RANGES - cmem->nr_ranges) * sizeof(Elf64_Phdr);
-
- return extra_sz;
-#endif
- return 0;
-}
-
/**
* load_elfcorehdr_segment - Setup crash memory ranges and initialize elfcorehdr
* segment needed to load kdump kernel.
@@ -428,7 +411,7 @@ static int load_elfcorehdr_segment(struct kimage *image, struct kexec_buf *kbuf)
* Account for extra space required to accommodate additional memory
* ranges in elfcorehdr due to memory hotplug events.
*/
- kbuf->memsz = headers_sz + kdump_extra_elfcorehdr_size(cmem);
+ kbuf->memsz = headers_sz + crash_extra_elfcorehdr_size(cmem->nr_ranges);
kbuf->top_down = false;
ret = kexec_add_buffer(kbuf);
diff --git a/include/linux/crash_core.h b/include/linux/crash_core.h
index 6789ff0af39e..d5981008a812 100644
--- a/include/linux/crash_core.h
+++ b/include/linux/crash_core.h
@@ -106,4 +106,30 @@ ssize_t dm_crypt_keys_read(char *buf, size_t count, u64 *ppos);
static inline int crash_load_dm_crypt_keys(struct kimage *image) {return 0; }
#endif
+#if defined(CONFIG_CRASH_HOTPLUG) && defined(CONFIG_MEMORY_HOTPLUG)
+static inline unsigned int crash_extra_elfcorehdr_size(unsigned int nr_mem_ranges)
+{
+ unsigned int total_phdrs = 2 + num_possible_cpus() + CONFIG_CRASH_MAX_MEMORY_RANGES;
+
+ BUILD_BUG_ON(CONFIG_CRASH_MAX_MEMORY_RANGES > (unsigned int)PN_XNUM);
+
+ if (nr_mem_ranges >= CONFIG_CRASH_MAX_MEMORY_RANGES) {
+ pr_warn_once("Configured crash mem ranges may not be enough\n");
+ return 0;
+ }
+
+ if (total_phdrs >= (unsigned int)PN_XNUM) {
+ pr_warn_once("number of Phdrs %u exceeds max\n", total_phdrs);
+ return 0;
+ }
+
+ return (CONFIG_CRASH_MAX_MEMORY_RANGES - nr_mem_ranges) * sizeof(Elf64_Phdr);
+}
+#else
+static inline unsigned int crash_extra_elfcorehdr_size(unsigned int nr_mem_ranges)
+{
+ return 0;
+}
+#endif
+
#endif /* LINUX_CRASH_CORE_H */
--
2.34.1