Hi Wandun,
在 2026/12/3 17:48, Wandun Chen 写道: > On Fri, Nov 27, 2026 at 06:09:16PM +0800, Jinjie Ruan wrote: >> arm64, LoongArch and RISC-V load the kdump kernel's elf core header and >> initrd by hand, and arm64 and RISC-V also build the device tree for the >> kexec kernel and place it as a segment. Add helpers for them so that >> every architecture keeps its own placement policy, which the caller >> puts into the kexec_buf: >> >> - kexec_prepare_elf_core_header() builds the header once per image and >> returns the number of memory ranges, which x86 needs to size its >> crash hotplug reservation. >> >> - kexec_add_elf_core_header() places the prepared header as a segment. >> The caller provides the kexec_buf with its placement policy and the >> size to reserve: x86 and powerpc put their crash hotplug reservation >> there, the other architectures the header size. >> >> - kexec_add_initrd() places the initrd from the image. >> >> - of_kexec_add_fdt() builds the device tree for the kexec kernel, packs >> it and places it. >> >> Cc: Andrew Morton <[email protected]> >> Cc: Baoquan He <[email protected]> >> Cc: Mike Rapoport <[email protected]> >> Suggested-by: Mike Rapoport <[email protected]> >> Signed-off-by: Jinjie Ruan <[email protected]> >> --- >> drivers/of/kexec.c | 35 +++++++++++++++++++++++++ >> include/linux/kexec.h | 6 +++++ >> include/linux/of.h | 4 +++ >> kernel/kexec_file.c | 60 +++++++++++++++++++++++++++++++++++++++++++ >> 4 files changed, 105 insertions(+) >> >> diff --git a/drivers/of/kexec.c b/drivers/of/kexec.c >> index bb4e0d9bfd38..a6f07c33fc40 100644 >> --- a/drivers/of/kexec.c >> +++ b/drivers/of/kexec.c >> @@ -533,3 +533,38 @@ void *of_kexec_alloc_and_setup_fdt(const struct kimage >> *image, >> >> return fdt; >> } >> + >> +int of_kexec_add_fdt(struct kimage *image, struct kexec_buf *kbuf, >> + unsigned long initrd_load_addr, const char *cmdline, >> + void **fdt) >> +{ >> + void *new_fdt; >> + int ret; >> + >> + new_fdt = of_kexec_alloc_and_setup_fdt(image, initrd_load_addr, >> + image->initrd_buf_len, >> + cmdline, 0); >> + if (!new_fdt) { >> + pr_err("Preparing for new dtb failed\n"); >> + return -EINVAL; >> + } >> + >> + fdt_pack(new_fdt); >> + >> + kbuf->buffer = new_fdt; >> + kbuf->bufsz = kbuf->memsz = fdt_totalsize(new_fdt); >> + kbuf->mem = KEXEC_BUF_MEM_UNKNOWN; >> + >> + ret = kexec_add_buffer(kbuf); >> + if (ret) { >> + kvfree(new_fdt); >> + return ret; >> + } >> + >> + *fdt = new_fdt; >> + >> + kexec_dprintk("Loaded dtb at 0x%lx bufsz=0x%lx memsz=0x%lx\n", >> + kbuf->mem, kbuf->bufsz, kbuf->memsz); >> + >> + return 0; >> +} >> diff --git a/include/linux/kexec.h b/include/linux/kexec.h >> index bac11e940e74..491046a90ffa 100644 >> --- a/include/linux/kexec.h >> +++ b/include/linux/kexec.h >> @@ -258,6 +258,12 @@ int kexec_kernel_verify_pe_sig(const char *kernel, >> unsigned long kernel_len); >> >> extern int kexec_add_buffer(struct kexec_buf *kbuf); >> int kexec_locate_mem_hole(struct kexec_buf *kbuf); >> +int kexec_add_initrd(struct kimage *image, struct kexec_buf *kbuf); >> +#ifdef CONFIG_CRASH_DUMP >> +int kexec_prepare_elf_core_header(struct kimage *image, bool >> need_kernel_map, >> + unsigned long *nr_ranges); >> +int kexec_add_elf_core_header(struct kimage *image, struct kexec_buf *kbuf); >> +#endif >> >> #ifndef arch_kexec_apply_relocations_add >> /* >> diff --git a/include/linux/of.h b/include/linux/of.h >> index 44d83133a65a..5934097e7a21 100644 >> --- a/include/linux/of.h >> +++ b/include/linux/of.h >> @@ -480,10 +480,14 @@ int of_map_msi_id(const struct device_node *np, u32 id, >> phys_addr_t of_dma_get_max_cpu_address(struct device_node *np); >> >> struct kimage; >> +struct kexec_buf; >> void *of_kexec_alloc_and_setup_fdt(const struct kimage *image, >> unsigned long initrd_load_addr, >> unsigned long initrd_len, >> const char *cmdline, size_t extra_fdt_size); >> +int of_kexec_add_fdt(struct kimage *image, struct kexec_buf *kbuf, >> + unsigned long initrd_load_addr, const char *cmdline, >> + void **fdt); >> #else /* CONFIG_OF */ >> >> static inline void of_core_init(void) >> diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c >> index 1f128c6e6fa3..66f389346c2b 100644 >> --- a/kernel/kexec_file.c >> +++ b/kernel/kexec_file.c >> @@ -109,6 +109,66 @@ static void kexec_free_elf_headers(struct kimage *image) >> image->elf_headers_sz = 0; >> } >> >> +#ifdef CONFIG_CRASH_DUMP >> +int kexec_prepare_elf_core_header(struct kimage *image, bool >> need_kernel_map, >> + unsigned long *nr_ranges) >> +{ >> + int ret; >> + >> + /* >> + * The header does not depend on where the segments are placed, so >> + * the loaders' placement retries can reuse the same build. >> + */ >> + if (image->elf_headers) >> + return 0; > > The header is now built only at the first attempt and reused afterwards, > if new memory is hotplugged after that build, the reused header would miss > the newly added memory and the resulting vmcore would not cover it, is that > right? The header is built once per image when calling kexec_file_load(). The arm64/LoongArch retries only try another segment placement, they never change the memory map, so rebuilding the header on every retry repeats the same work. A hotplug that happens after the image is loaded is a separate path. On x86, RISC-V and Powerpc, crash_handle_hotplug_event() will update the elfcorehdr in place using crash_prepare_headers() directly, so the vmcore still covers the new memory. On arm64 and LoongArch that support does not exist yet and the header has always been a load-time snapshot; this change does not alter that. Best regards, Jinjie > > Thanks, > Wandun >> + >> + ret = crash_prepare_headers(need_kernel_map, &image->elf_headers, >> + &image->elf_headers_sz, nr_ranges); >> + if (ret) >> + pr_err("Preparing elf core header failed\n"); >> + >> + return ret; >> +} >> + >> +int kexec_add_elf_core_header(struct kimage *image, struct kexec_buf *kbuf) >> +{ >> + int ret; >> + >> + kbuf->buffer = image->elf_headers; >> + kbuf->bufsz = image->elf_headers_sz; >> + kbuf->mem = KEXEC_BUF_MEM_UNKNOWN; >> + >> + ret = kexec_add_buffer(kbuf); >> + if (ret) >> + return ret; >> + >> + image->elf_load_addr = kbuf->mem; >> + >> + kexec_dprintk("Loaded elf core header at 0x%lx bufsz=0x%lx >> memsz=0x%lx\n", >> + image->elf_load_addr, kbuf->bufsz, kbuf->memsz); >> + >> + return 0; >> +} >> +#endif /* CONFIG_CRASH_DUMP */ >> + >> +int kexec_add_initrd(struct kimage *image, struct kexec_buf *kbuf) >> +{ >> + int ret; >> + >> + kbuf->buffer = image->initrd_buf; >> + kbuf->bufsz = kbuf->memsz = image->initrd_buf_len; >> + kbuf->mem = KEXEC_BUF_MEM_UNKNOWN; >> + >> + ret = kexec_add_buffer(kbuf); >> + if (ret) >> + return ret; >> + >> + kexec_dprintk("Loaded initrd at 0x%lx bufsz=0x%lx memsz=0x%lx\n", >> + kbuf->mem, kbuf->bufsz, kbuf->memsz); >> + >> + return 0; >> +} >> + >> /* >> * Free up memory used by kernel, initrd, and command line. This is >> temporary >> * memory allocation which is not needed any more after these buffers have >> -- >> 2.34.1 >> >
