[PATCH v3 14/17] x86/crash: Simplify crash_load_segments() using crash_extra_elfcorehdr_size()

From: Jinjie Ruan

Date: Wed Aug 26 2026 - 05:39:14 EST


Replace the open-coded elfcorehdr reservation logic in
crash_load_segments() with the generic crash_extra_elfcorehdr_size()
helper. The helper centralizes all PN_XNUM and memory range checks,
reducing code duplication.

The elfcorehdr_index is handled separately in crash_handle_hotplug_event()
during the first hotplug event, so it does not need to be set in
crash_load_segments().

Cc: Thomas Gleixner <tglx@xxxxxxxxxx>
Cc: Ingo Molnar <mingo@xxxxxxxxxx>
Cc: Borislav Petkov <bp@xxxxxxxxx>
Cc: Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx>
Cc: "H. Peter Anvin" <hpa@xxxxxxxxx>
Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Cc: Baoquan He <baoquan.he@xxxxxxxxx>
Cc: "Mike Rapoport (Microsoft)" <rppt@xxxxxxxxxx>
Signed-off-by: Jinjie Ruan <ruanjinjie@xxxxxxxxxx>
---
arch/x86/kernel/crash.c | 32 +++++---------------------------
1 file changed, 5 insertions(+), 27 deletions(-)

diff --git a/arch/x86/kernel/crash.c b/arch/x86/kernel/crash.c
index afe2aea19f47..99eb8583d932 100644
--- a/arch/x86/kernel/crash.c
+++ b/arch/x86/kernel/crash.c
@@ -348,42 +348,20 @@ int crash_setup_memmap_entries(struct kimage *image, struct boot_params *params)

int crash_load_segments(struct kimage *image)
{
- int ret;
- unsigned long pnum = 0;
struct kexec_buf kbuf = { .image = image, .buf_min = 0,
.buf_max = ULONG_MAX, .top_down = false };
+ unsigned long nr_ranges = 0;
+ int ret;

/* Prepare elf headers and add a segment */
ret = crash_prepare_headers(IS_ENABLED(CONFIG_X86_64), &kbuf.buffer,
- &kbuf.bufsz, &pnum);
+ &kbuf.bufsz, &nr_ranges);
if (ret)
return ret;

image->elf_headers = kbuf.buffer;
- image->elf_headers_sz = kbuf.bufsz;
- kbuf.memsz = kbuf.bufsz;
-
-#ifdef CONFIG_CRASH_HOTPLUG
- /*
- * The elfcorehdr segment size accounts for VMCOREINFO, kernel_map,
- * maximum CPUs and maximum memory ranges.
- */
- if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG))
- pnum = 2 + num_possible_cpus() + CONFIG_CRASH_MAX_MEMORY_RANGES;
- else
- pnum += 2 + num_possible_cpus();
-
- if (pnum < (unsigned long)PN_XNUM) {
- kbuf.memsz = elf64_phdr_size(pnum);
-
- image->elfcorehdr_index = image->nr_segments;
-
- /* Mark as usable to crash kernel, else crash kernel fails on boot */
- image->elf_headers_sz = kbuf.memsz;
- } else {
- pr_err("number of Phdrs %lu exceeds max\n", pnum);
- }
-#endif
+ kbuf.memsz = kbuf.bufsz + crash_extra_elfcorehdr_size(nr_ranges);
+ image->elf_headers_sz = kbuf.memsz;

kbuf.buf_align = ELF_CORE_HEADER_ALIGN;
kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
--
2.34.1