From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.227.130 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796299657; b=xwP15bATeJYbRvgMPNnfcOrL8KzAxXZBaZW8hhGQALeTtItwno6ZgzOJ3IG7pRB+Xa/D qAa/Uf5gq8HTe6TJGp87wWcL1w2xkUFZsbbhqN90h8goIMwYJHl+Ihcmxky3HQVSlLSmm xlC3rhVEvh+7pTynsj6Xzy0s1cfeLTIrpG49lzUDjH9HEQA4+annuwk9ep2yiNdCeuE7i 2uqk3VrDTghskMkGKX+nuY0fkdxlHKb04pJv6zyotUp9WxAZKcf2z+r58e4aDDmV5PDNV PPWhufTWuyBTQJs0PhydozSIHdYJObXQNOJ8AOy1qxFIRJCh8FZ/9Xlivc4YoFiLu3Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796299657; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Message-ID:Date:MIME-Version:User-Agent:Subject:To:Cc:References: Content-Language:From:In-Reply-To:Content-Type: Content-Transfer-Encoding; bh=Lg4NiCX96LuJcuWlm+E/MTfgjkITSFPl/CFP+c+M4UI=; b=1AslwIPbpTRVcA3dbFQV0242K/Qz4rQ5pcxcVc3wjTMyWPrGVw3MqPLWhDsqdxY5Q0qT cyR0IPiNAgbjQhOlv1GYRjkLQ60LIZUZr8eOzqAt/hzjsoavTxIsg4VvXFjDpy0HV2CUr w3AEUfUviIpUEd3p8omZ/Rq1JFnZSE0MHslJKYBUD5GP4YPSpPf2J/Ojp+dIkuNlRPIdm 1SiTdlHsi77iyh2l76Is2XAdSNt0Pf0FdMw7XCAfzf4FcRorpb0eBrhBSkmcSnQokn2Yf P7agQP/biKma7OEVpbQw4DCex+4md/L0+6tWuIM61mH7N3BNRgAZwT40xC5CQgaZvoA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=MGjAx7+L; arc=none smtp.remote-ip=74.125.227.130 Received: from mail-pj2-f2.google.com (mail-pj2-f2.google.com [74.125.227.130]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E9ACA606A335 for ; Thu, 3 Dec 2026 12:07:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E9ACA606A335 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=MGjAx7+L Received: by mail-pj2-f2.google.com with SMTP id d9443c01a7336-2efc8c9ba9fso47932655ad.0 for ; Thu, 03 Dec 2026 04:07:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796299655; x=1796904455; darn=vger.kernel.org; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:from:to:cc:subject:date:message-id:reply-to :content-type; bh=Lg4NiCX96LuJcuWlm+E/MTfgjkITSFPl/CFP+c+M4UI=; b=MGjAx7+LqW3IFa8VH0wSL2cNAczNgVZfEinNSk93JHU1wg0VwJamKwYuctMTz36P85 I96ZzwJhSVDuN8cZ6LrEN+HXhVRU9Q8EUWGuUO583p2CtcIoZehzEfd4ia8/9j1iGEgf I1/z73gpmwadelHXTMf8nIvS4sofvduGM+VDxoTFpsLZ4XZDQlWfkLm3FRuKAR3utXT8 BK1mxNlI1gzyDCcUmvyjL0zI6feXrIUGCYlk+wXmlks3WZIMk7u1DQ3sPPZiiE0Le81e RQk38mJcr6u6AdfZtrgwofSnJUY57lRDn/B2bT+e9G/hKWcBtglCjbMaoEe75CxffH6s ctbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796299655; x=1796904455; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=Lg4NiCX96LuJcuWlm+E/MTfgjkITSFPl/CFP+c+M4UI=; b=ApU8dIAnx8oYIaRm+ePckZvq+upcH//QG0sP/eJmYSPEfXDnFm3/iFV6RlYilRJrm4 EQckm0niX8zWG7GLRMSmKIkW17fQChPILZYwL3aGYVKhTtoEStbizbKSTZ3EOSIEePON je0nrr4IyjZwZ0XwOCqkl0Dnhxmq/ixHIlbzyx4Qkl9FEMllGoZfoFBsayfW9iGOEMj/ o60YCE8E+Uo2Zhgfwxgze9D+oMhu7EUyFFmJU5EFkSqUoiKbYBb1ihcIk3afr85UfV68 54uTD6VcngSaIoxCzOu0KkL4aIKPgPt4Rah5UiGZ5xa85FlG2TYnoLcINO2Tx0CUmYt4 xuzQ== X-Forwarded-Encrypted: i=1; AL+hErHa1Qpb0kh8+DFtTyXjp4LgKuyiLT/aPE7Fo84b6R0AIn4ZlKVqQ9h125EsmZXCILiE+hyjhCbPm+hL@vger.kernel.org X-Gm-Message-State: APuU4Z/cqjGmD65/t2niECQkPC311swPT1plnmPoGxX1F+lGKeRYjIsN GwX+Aq2INUTieba4TWzHADk0QiPbyo9yTrUZjgF4JNrbd274fikuEkmB X-Gm-Gg: AZFdTZce7jkuoobFuu4T69frhuUX4zg5HzpvpXCg1kw44GGyASZlXlHJiwByFFlQUGl 17DyUKspnBd9EqvSVEFvWk5Adsf7LHfuBOdUsaqIfICWKjBAffH2GSk85FqIHcUeL9MqF0Jf+rF Z0HBk4Pa6yOiMV0gYwQuZrs/eBvAI9GWq2GujkyEVN9zUhgPmNa3eHCULLDq0hBdQ4PsecKzc+t wJJfstDM3f/Phcmw0kuDkp8WCY/V8sKYtJhtmmaxbv/b18g3TSMS0QH9TI81X0l8zjaUASIe6uQ CGsGCwpI6/mbepuLlkLu3Zl/FwOaejBwIuGENKg77bF9CWiALwPRcdaUCsfAzJ+ey3A4ZWyvV8c LGhgXkLwGrHgRkABuBFGvjy5LwETFLb6r1+tD6WMtcSbGB5k4q1vTZNuo8jYZWSLuXQOtp/grEd ifC+XITv8YAzWGVyIPAfw9sTzOmmEAEhR7PyinSVBm+9D+1+edYb9+sfIH06zPOwajCt77HFdne pJOCFiDQFzP3SOSyrbFgLTBB8RZPDQjyrfLPmUgOILEYjPiOr7/6B5d3Id7VAhTkXtYY9NXg89X iOlLK9CKrVlAg2v8LwqUDL1vivQUI2XExpg= X-Received: by 2002:a17:902:f70f:b0:2df:90d4:7758 with SMTP id d9443c01a7336-2f03f118282mr386212085ad.23.1796299655195; Thu, 03 Dec 2026 04:07:35 -0800 (PST) Received: from [10.125.112.22] ([122.11.210.25]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2f14c8a7281sm70278855ad.61.2026.12.03.04.07.12 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 03 Dec 2026 04:07:34 -0800 (PST) Message-ID: Date: Thu, 3 Dec 2026 20:07:10 +0800 X-Mailing-List: devicetree@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: Jinjie Ruan Cc: catalin.marinas@arm.com, will@kernel.org, mark.rutland@arm.com, chenhuacai@kernel.org, kernel@xen0n.name, maddy@linux.ibm.com, mpe@ellerman.id.au, npiggin@gmail.com, chleroy@kernel.org, ritesh.list@gmail.com, sshegde@linux.ibm.com, pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, tglx@kernel.org, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, hpa@zytor.com, robh@kernel.org, saravanak@kernel.org, akpm@linux-foundation.org, baoquan.he@linux.dev, rppt@kernel.org, pasha.tatashin@soleen.com, pratyush@kernel.org, ben@cressey.dev, leitao@debian.org, mpilaniy@redhat.com, yeoreum.yun@arm.com, coiby.xu@gmail.com, Yeking@red54.com, prudo@redhat.com, bradley.morgan@linux.dev, sourabhjain@linux.ibm.com, blum@kernel.org, hbathini@linux.ibm.com, qirui.001@bytedance.com, hui.wang@canonical.com, sohammetha01@gmail.com, guoren@kernel.org, gaohan@iscas.ac.cn, ionut.nechita@windriver.com, vishal.l.verma@intel.com, seanjc@google.com, ardb@kernel.org, arnd@arndb.de, tzimmermann@suse.de, kas@kernel.org, rick.p.edgecombe@intel.com, mhklinux@outlook.com, piliu@redhat.com, shradhagupta@linux.microsoft.com, thuth@redhat.com, ebiggers@kernel.org, me@linux.beauty, mclapinski@google.com, graf@amazon.com, tangyouling@kylinos.cn, x86@kernel.org, linux-arm-kernel@lists.infradead.org, loongarch@lists.linux.dev, linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, kexec@lists.infradead.org References: <20261127100925.3019877-1-ruanjinjie@huawei.com> <20261127100925.3019877-3-ruanjinjie@huawei.com> <6qertvtuc6g7lx2f2r4pmvivbhw6m3orwlg5m6k3kr3kshzaqy@w33lihepgyhd> <0f6c7c68-ef84-4e7f-90f7-e267d8d34756@huawei.com> Content-Language: en-US From: Wandun In-Reply-To: <0f6c7c68-ef84-4e7f-90f7-e267d8d34756@huawei.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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? 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 >>> >> >