From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=113.46.200.227 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796302637; b=AiOpV+XCUT+GcLK2OtgZsuqhdzW3W1i0p2poO34xhhBGhf2eSoGcXLbVmv6CVf/t5AcJ pULy5CeCi6TSXNouhazADN+zBJ988GFS0BtwwFKhOV6RpYCXOJrPcSiHR//UlbO32/ti8 Q2z2knpm6Wg6Q++5yMKmbzy5Z+WvbNMlhWxbeKPeuykuRI8tdzm4zdONoo+rjiqCZg0bD Bzve51/raE9qwcUk+8am15qJfQ2Bzr5mp7z4bfa9cP+rL6lB+IVLRB0OQDqkPL9vsWGEB oZCUunf0Yq/W7lMVwzj49Q+UrpPC6bvLCrywAbzXy7StbPehFT9+LlaQiWtBkcYtz3Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796302637; h=DMARC-Filter:dkim-signature:Received:Received:Received:Message-ID: Date:MIME-Version:User-Agent:Subject:To:CC:References:From: In-Reply-To:Content-Type:Content-Transfer-Encoding:X-Originating-IP: X-ClientProxiedBy; bh=Mq2HwViqxZhpirH+Z9r3+ImcGITytXUmPpW44inmdio=; b=pMJKJtLTTAuIwyN0oo4fimCL2xXLu4NdJr0ulAhLKcDhDbfPEtOPV70p7VzlAQikD8rv UhthlfesFz5idI3uqch/Asa+0UnCNpMpmQ/S2r3xcjPUOpCYJ+6/VKEWxUzT7/fhr618r tEmxzK9edHxwbiIZL5I9ImvjruRf+ND53J202ZaAg1RPbuDZqyPu4pYcHjpvV31UhWlK9 p1/44uX9015lAFebcIAmEJt51q0HRvd5bEpTP92naKkxwFb7yBx9F2J/w/gXa2MiugNW2 MUd8pVFsrCO4FbPoM1XH7Rt9N4B1OcPHv0XUtnjm696U2CqD+II0ByaT0Awn1DvRh8g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; dkim=pass header.d=huawei.com header.i=@huawei.com header.a=rsa-sha256 header.s=dkim header.b=uFcCcoIk; arc=none smtp.remote-ip=113.46.200.227 Received: from canpmsgout12.his.huawei.com (canpmsgout12.his.huawei.com [113.46.200.227]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 51D0B606A322; Thu, 3 Dec 2026 12:57:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 51D0B606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.a=rsa-sha256 header.s=dkim header.b=uFcCcoIk dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=Mq2HwViqxZhpirH+Z9r3+ImcGITytXUmPpW44inmdio=; b=uFcCcoIk8zvwEgGfKsj8JgMyD5Z717MREAIZpjXTH6KV8pArnmagZSAQvtRRl75bJ1it6MSoj m71s8q1OoV/aBdYFHqh/qN+tbfKGfv11jZgX+kc9DMunJiFROGURkyAk23+vRGunzxdhPnX3uy9 p9axxqviSk4tDazL5AhThUQ= Received: from mail.maildlp.com (unknown [172.19.163.200]) by canpmsgout12.his.huawei.com (SkyGuard) with ESMTPS id 4jgF4W4mHfznTbY; Thu, 3 Dec 2026 20:56:27 +0800 (CST) Received: from kwepemk200008.china.huawei.com (unknown [7.202.194.74]) by mail.maildlp.com (Postfix) with ESMTPS id DF3924055B; Thu, 3 Dec 2026 20:57:11 +0800 (CST) Received: from [10.67.110.68] (10.67.110.68) by kwepemk200008.china.huawei.com (7.202.194.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.45; Thu, 3 Dec 2026 20:57:09 +0800 Message-ID: Date: Thu, 3 Dec 2026 20:57:08 +0800 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v9 02/11] kexec: Add helpers to load the elf core header, initrd and FDT To: Wandun CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , References: <20261127100925.3019877-1-ruanjinjie@huawei.com> <20261127100925.3019877-3-ruanjinjie@huawei.com> <6qertvtuc6g7lx2f2r4pmvivbhw6m3orwlg5m6k3kr3kshzaqy@w33lihepgyhd> <0f6c7c68-ef84-4e7f-90f7-e267d8d34756@huawei.com> From: Jinjie Ruan In-Reply-To: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-ClientProxiedBy: kwepems200002.china.huawei.com (7.221.188.68) To kwepemk200008.china.huawei.com (7.202.194.74) 在 2026/12/3 20:07, Wandun 写道: > > > On 12/3/26 19:12, Jinjie Ruan wrote: >> >> 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 >>>> Cc: Baoquan He >>>> Cc: Mike Rapoport >>>> Suggested-by: Mike Rapoport >>>> Signed-off-by: Jinjie Ruan >>>> --- >>>> 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. > > Consider the case where load_other_segments() runs more than once: if > the first attempt fails, the elf header has already been built and > stored in image->elf_headers. If a memory hotplug happens at that point, > the next retry still reuses the old header. Before this patch, each retry > freed the previously built header and rebuilt it from scratch, so a later > retry would have picked up the new memory map. So there is a small behavior > change here, is that right? That is right in principle, but it is not a guarantee in either version. attempt 1: [build H]...[place: fail] : : hotplug A: the per-attempt rebuild would see the new : memory, the single build does not : attempt 2: [reuse H | rebuild H]...[place: ok] -> return ^ ^ hotplug B hotplug C A: only arm64 and LoongArch retry, and neither supports crash hotplug, so their elfcorehdr is a load-time snapshot. B: there is no rebuild point after the last build, both versions miss it. C: both versions miss it on the architectures without crash hotplug; on X86, RISC-V and Powerpc the next hotplug event rebuilds the elfcorehdr in place. Refreshing the header for hotplug that happens after it is built is what crash hotplug feature is for; the arm64 and LoongArch support for it is a separate effort (the ARM64 support will be submitted after the rework, refer to the following gitHub link). And the per-attempt rebuild never gave them that guarantee. The free-and-rebuild per attempt is what this series removes and Mike suggested in v7: the header now has a single build and is released once in kimage_file_post_load_cleanup(). And x86 has no retry loop and also always built it once. "Building the headers once and only placing them in the loop removes the leak instead of fixing it" v7: https://lore.kernel.org/all/179430504583.425130.15302016434972990362.b4-review@b4/ Link: https://github.com/ruanjinjie-eng/linux/commits/arm64_crash_hotplug/ Thanks, Jinjie > > > Thanks, > Wandun > >> >> 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 >>>> >>> >> >