From nobody Sun Oct 5 03:35:12 2025 Received: from out-183.mta1.migadu.com (out-183.mta1.migadu.com [95.215.58.183]) (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 058C82DFA2A for ; Mon, 11 Aug 2025 09:28:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.183 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754904503; cv=none; b=fwrE7LWKBoX8Pu5v1fn4hv4c84zqjtI5mBunTTA7yZNxCApPC178gYDPh9FFozGJv8w7f0YcgSzPKmLdRMymnsbuV9LKR70nvTz+f7XZgoN0hqWhqC122OV1VtFsxGxW+sWzw2QZhDKv+WjPTQbZAuKTSFlif2uZptOMtEJZzyo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754904503; c=relaxed/simple; bh=TWmefRoC55W7AVicXj3J/cV4CdEcntC6RZQ0S5QOBSE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=lJO20VJleGMrhz2ozoi6G2w3ZSdES9Vq8eeOWXA7WECQ4BaoMzGnQ3U/STu6b6igU9E2Fjyzy/coVmAA58vEq/WDcdBdLMO3y2TBBvVgaQl+tvNSkUPoXOZhb5VtmoOWGmOhT+g6M2UzYQ5sy7amGZAImdxXcPf8+xKW2fRXTMQ= 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=l5eXt6ER; arc=none smtp.client-ip=95.215.58.183 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="l5eXt6ER" 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=1754904500; 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=S1fZPuz6xxyXns5V9c+B6pAeL+oN23MEikk16JZKk2w=; b=l5eXt6ERjCvzJ8rIOVvwNo2eM+PByJrlb98f/pBGcJB+DEWL0eNzEhSLrTUENgw/fAdPcW QC3tSk+dlAuXPU7LBzHcKZRN+IQklsx2sp0txteBTwTVrpj5apV3vayiheuAjFrr2oJXMR AidcjYtm3kXBxgm2xWBZ7UlRXbHxiGg= From: Youling Tang To: Huacai Chen Cc: WANG Xuerui , Baoquan He , kexec@lists.infradead.org, loongarch@lists.linux.dev, linux-kernel@vger.kernel.org, youling.tang@linux.dev, Youling Tang Subject: [PATCH 3/6] LoongArch/kexec_file: Add initrd loading Date: Mon, 11 Aug 2025 17:26:56 +0800 Message-Id: <20250811092659.14903-4-youling.tang@linux.dev> In-Reply-To: <20250811092659.14903-1-youling.tang@linux.dev> References: <20250811092659.14903-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 Add inird loading support and pass it to the second kernel via the cmdline 'initrd=3Dstart,size'. Signed-off-by: Youling Tang --- arch/loongarch/kernel/machine_kexec_file.c | 71 ++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/arch/loongarch/kernel/machine_kexec_file.c b/arch/loongarch/ke= rnel/machine_kexec_file.c index bc91ae0afa4c..e1240644f529 100644 --- a/arch/loongarch/kernel/machine_kexec_file.c +++ b/arch/loongarch/kernel/machine_kexec_file.c @@ -34,13 +34,84 @@ int arch_kimage_file_post_load_cleanup(struct kimage *i= mage) return kexec_image_post_load_cleanup_default(image); } =20 +/* 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) +{ + int initrd_strlen; + + initrd_strlen =3D sprintf(modified_cmdline + (*cmdline_tmplen), "initrd= =3D0x%lx,0x%lx ", + initrd, image->initrd_buf_len); + *cmdline_tmplen +=3D initrd_strlen; +} + +/* + * Tries to add the initrd to the image. If it is not possible to find + * valid locations, this function will undo changes to the image and retur= n non + * zero. + */ int load_other_segments(struct kimage *image, unsigned long kernel_load_addr, unsigned long kernel_size, char *initrd, unsigned long initrd_len, char *cmdline, unsigned long cmdline_len) { + struct kexec_buf kbuf; + unsigned long orig_segments =3D image->nr_segments; + char *modified_cmdline =3D NULL; + unsigned long cmdline_tmplen =3D 0; + unsigned long initrd_load_addr =3D 0; + int ret =3D 0; + + + kbuf.image =3D image; + /* not allocate anything below the kernel */ + kbuf.buf_min =3D kernel_load_addr + kernel_size; + + modified_cmdline =3D kzalloc(COMMAND_LINE_SIZE, GFP_KERNEL); + if (!modified_cmdline) + return -EINVAL; + + /* Ensure it's nul terminated */ + modified_cmdline[COMMAND_LINE_SIZE - 1] =3D '\0'; + + /* load initrd */ + if (initrd) { + kbuf.buffer =3D initrd; + kbuf.bufsz =3D initrd_len; + kbuf.mem =3D KEXEC_BUF_MEM_UNKNOWN; + kbuf.memsz =3D initrd_len; + kbuf.buf_align =3D 0; + /* within 1GB-aligned window of up to 32GB in size */ + kbuf.buf_max =3D round_down(kernel_load_addr, SZ_1G) + + (unsigned long)SZ_1G * 32; + kbuf.top_down =3D false; + + ret =3D kexec_add_buffer(&kbuf); + if (ret) + goto out_err; + initrd_load_addr =3D kbuf.mem; + + kexec_dprintk("Loaded initrd at 0x%lx bufsz=3D0x%lx memsz=3D0x%lx\n", + initrd_load_addr, kbuf.bufsz, kbuf.memsz); + + /* Add the initrd=3Dstart,size parameter to the command line */ + cmdline_add_initrd(image, &cmdline_tmplen, modified_cmdline, initrd_load= _addr); + } + + if (cmdline_len + cmdline_tmplen > COMMAND_LINE_SIZE) { + pr_err("Appending kdump cmdline exceeds cmdline size\n"); + ret =3D -EINVAL; + goto out_err; + } + memcpy(modified_cmdline + cmdline_tmplen, cmdline, cmdline_len); + cmdline =3D modified_cmdline; image->arch.cmdline_ptr =3D (unsigned long)cmdline; =20 return 0; + +out_err: + image->nr_segments =3D orig_segments; + kfree(modified_cmdline); + return ret; } --=20 2.34.1