From nobody Sat Oct 4 06:34:55 2025 Received: from out-180.mta1.migadu.com (out-180.mta1.migadu.com [95.215.58.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 457622C15A6 for ; Wed, 20 Aug 2025 05:57:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755669462; cv=none; b=Ijzec36JkuNbBOaeGunZV+E70ipQ2SD8NixQsjnxVOG2GerMLQrRyMtqRzzJdUHtISw0LM2bZDY6EaXa+E+7bR9yKY7+muEAxKotrlzwHi6Qm/IVU1tKrmOkhRYhTe08YSlZCb8GWpFP52jqB+VtNieck+uFUYVgklJA89B17HY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755669462; c=relaxed/simple; bh=jUmK03O6qt39sW1JXmuJ0moazBFyYhowvw8E+5VSZXY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Q2RnrbwIWGvqi+QAxCJD/LbgGD5OLWEsaG2DcduxQ0m+kiZG1sqyawyIVt+RQ07udZQnVwIXXzDw1/QHaNnkbdENB9AI75k4YQCYXUZt7znaeXtSCLZsbuI/MIg03rMrWCENnIaRNQuH7j+MufTwdsU7GU/fSWe9OC070FRGOAg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=HQ4MeK3j; arc=none smtp.client-ip=95.215.58.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="HQ4MeK3j" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1755669458; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BC7L55YtALYXWh7kK1DKLN5HZDHjt9lPYAHgNVecOSU=; b=HQ4MeK3jFv/WjRdqaVKNZXe5Qtn5cM1yAAOwNOYoa3cs7gn6A5n2JAl7Big2pAvy64FpPq pJMLE2Wo0xLrJScoX2oQNTDXoehwg67BMTl9KHFWYT8I3QQqHWj9NmMmnZ273y6WkQK2uQ 5cRKdeM2MbLw+un8+5YLEb7Bsh2HQJc= From: Youling Tang To: Huacai Chen Cc: WANG Xuerui , Baoquan He , Yao Zi , kexec@lists.infradead.org, loongarch@lists.linux.dev, linux-kernel@vger.kernel.org, youling.tang@linux.dev, Youling Tang Subject: [PATCH v2 4/5] LoongArch/kexec_file: Add crash dump support Date: Wed, 20 Aug 2025 13:56:59 +0800 Message-Id: <20250820055700.24344-5-youling.tang@linux.dev> In-Reply-To: <20250820055700.24344-1-youling.tang@linux.dev> References: <20250820055700.24344-1-youling.tang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" From: Youling Tang Enabling crash dump (kdump) includes: - Prepare contents of ELF header of a core dump file, /proc/vmcore, using crash_prepare_elf64_headers(). - Add "elfcorehdr=3Dsize@start" to cmdline. - Add the "mem=3Dsize@start" parameter to the command line and pass it to the capture kernel. Limit the runtime memory area of the captured kernel to avoid disrupting the production kernel's runtime state. Signed-off-by: Youling Tang --- arch/loongarch/kernel/machine_kexec_file.c | 117 +++++++++++++++++++++ 1 file changed, 117 insertions(+) diff --git a/arch/loongarch/kernel/machine_kexec_file.c b/arch/loongarch/ke= rnel/machine_kexec_file.c index dc6e37457559..1d47be06549e 100644 --- a/arch/loongarch/kernel/machine_kexec_file.c +++ b/arch/loongarch/kernel/machine_kexec_file.c @@ -35,6 +35,83 @@ int arch_kimage_file_post_load_cleanup(struct kimage *im= age) return kexec_image_post_load_cleanup_default(image); } =20 +#ifdef CONFIG_CRASH_DUMP + +static int prepare_elf_headers(void **addr, unsigned long *sz) +{ + struct crash_mem *cmem; + unsigned int nr_ranges; + int ret; + u64 i; + phys_addr_t start, end; + + nr_ranges =3D 2; /* for exclusion of crashkernel region */ + for_each_mem_range(i, &start, &end) + nr_ranges++; + + cmem =3D kmalloc(struct_size(cmem, ranges, nr_ranges), GFP_KERNEL); + if (!cmem) + return -ENOMEM; + + cmem->max_nr_ranges =3D nr_ranges; + cmem->nr_ranges =3D 0; + for_each_mem_range(i, &start, &end) { + cmem->ranges[cmem->nr_ranges].start =3D start; + cmem->ranges[cmem->nr_ranges].end =3D end - 1; + cmem->nr_ranges++; + } + + /* Exclude crashkernel region */ + ret =3D crash_exclude_mem_range(cmem, crashk_res.start, crashk_res.end); + if (ret) + goto out; + + if (crashk_low_res.end) { + ret =3D crash_exclude_mem_range(cmem, crashk_low_res.start, crashk_low_r= es.end); + if (ret) + goto out; + } + + ret =3D crash_prepare_elf64_headers(cmem, true, addr, sz); + +out: + kfree(cmem); + return ret; +} + +/* Adds the "elfcorehdr=3Dsize@start" command line parameter to command li= ne. */ +static void cmdline_add_elfcorehdr(struct kimage *image, unsigned long *cm= dline_tmplen, + char *modified_cmdline, unsigned long elfcorehdr_sz) +{ + int elfcorehdr_strlen =3D 0; + + elfcorehdr_strlen =3D sprintf(modified_cmdline + (*cmdline_tmplen), "elfc= orehdr=3D0x%lx@0x%lx ", + elfcorehdr_sz, image->elf_load_addr); + *cmdline_tmplen +=3D elfcorehdr_strlen; +} + +/* + * Adds the "mem=3Dsize@start" command line parameter to command line, ind= icating the + * memory region the new kernel can use to boot into. + */ +static void cmdline_add_mem(struct kimage *image, unsigned long *cmdline_t= mplen, + char *modified_cmdline) +{ + int mem_strlen =3D 0; + + mem_strlen =3D sprintf(modified_cmdline + (*cmdline_tmplen), "mem=3D0x%ll= x@0x%llx ", + crashk_res.end - crashk_res.start + 1, crashk_res.start); + *cmdline_tmplen +=3D mem_strlen; + + if (crashk_low_res.end) { + mem_strlen =3D sprintf(modified_cmdline + (*cmdline_tmplen), "mem=3D0x%l= lx@0x%llx ", + crashk_low_res.end - crashk_low_res.start + 1, crashk_low_res.start); + *cmdline_tmplen +=3D mem_strlen; + } +} + +#endif + /* Adds the "initrd=3Dstart,size" command line parameter to command line. = */ static void cmdline_add_initrd(struct kimage *image, unsigned long *cmdlin= e_tmplen, char *modified_cmdline, unsigned long initrd) @@ -76,6 +153,46 @@ int load_other_segments(struct kimage *image, /* Ensure it's nul terminated */ modified_cmdline[COMMAND_LINE_SIZE - 1] =3D '\0'; =20 +#ifdef CONFIG_CRASH_DUMP + /* load elf core header */ + void *headers; + unsigned long headers_sz; + + if (image->type =3D=3D KEXEC_TYPE_CRASH) { + ret =3D prepare_elf_headers(&headers, &headers_sz); + if (ret) { + pr_err("Preparing elf core header failed\n"); + goto out_err; + } + + kbuf.buffer =3D headers; + kbuf.bufsz =3D headers_sz; + kbuf.mem =3D KEXEC_BUF_MEM_UNKNOWN; + kbuf.memsz =3D headers_sz; + kbuf.buf_align =3D SZ_64K; /* largest supported page size */ + kbuf.buf_max =3D ULONG_MAX; + kbuf.top_down =3D true; + + ret =3D kexec_add_buffer(&kbuf); + if (ret) { + vfree(headers); + goto out_err; + } + image->elf_headers =3D headers; + image->elf_load_addr =3D kbuf.mem; + image->elf_headers_sz =3D headers_sz; + + kexec_dprintk("Loaded elf core header at 0x%lx bufsz=3D0x%lx memsz=3D0x%= lx\n", + image->elf_load_addr, kbuf.bufsz, kbuf.memsz); + + /* Add the elfcorehdr=3Dsize@start parameter to the command line */ + cmdline_add_elfcorehdr(image, &cmdline_tmplen, modified_cmdline, headers= _sz); + + /* Add the mem=3Dsize@start parameter to the command line */ + cmdline_add_mem(image, &cmdline_tmplen, modified_cmdline); + } +#endif + /* load initrd */ if (initrd) { kbuf.buffer =3D initrd; --=20 2.43.0