mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Cong Wang <xiyou.wangcong@gmail.com>
To: linux-kernel@vger.kernel.org, Paul Walmsley <pjw@kernel.org>,
	Palmer Dabbelt <palmer@dabbelt.com>,
	Albert Ou <aou@eecs.berkeley.edu>,
	Alexandre Ghiti <alex@ghiti.fr>,
	linux-riscv@lists.infradead.org
Cc: "Song Shuai" <songshuaishuai@tinylab.org>,
	"Björn Töpel" <bjorn@kernel.org>, "Han Gao" <gaohan@iscas.ac.cn>,
	"Jinjie Ruan" <ruanjinjie@huawei.com>,
	"Mike Rapoport" <rppt@kernel.org>,
	"Baoquan He" <baoquan.he@linux.dev>,
	"Guo Ren" <guoren@kernel.org>,
	kexec@lists.infradead.org, "Cong Wang" <cwang@multikernel.io>
Subject: [PATCH] riscv: kexec_file: Keep segments out of the new kernel's PMD padding
Date: Fri, 13 Nov 2026 19:49:00 -0800	[thread overview]
Message-ID: <20261114034901.107028-1-xiyou.wangcong@gmail.com> (raw)

From: Cong Wang <cwang@multikernel.io>

A 64-bit kernel with STRICT_KERNEL_RWX reserves itself up to the next
PMD boundary past _end in setup_bootmem(). load_extra_segments() only
keeps the other segments above kernel_start + kernel_len, which is the
unaligned _end of the new kernel, so they may be placed in that padding.

The segments are placed top-down, which normally keeps them far from
the kernel. A kdump kernel's segments are confined to the crashkernel
region though, and when it has little room to spare, the lowest of
them, the initrd and the device tree, end up in the padding. The kdump
kernel then finds its initrd in memory it reserved for itself, drops
it, and panics without a root file system:

  crashkernel reserved: 0x000000017be00000 - 0x000000017fe00000 (64 MB)
  Loaded kernel at 0x17be00000 bufsz=0x1c40600 memsz=0x1cb1000
  Loaded initrd at 0x17db58000
  ...
  Starting crashdump kernel...
  INITRD: 0x17db58000+0x022a4000 overlaps in-use memory region
  ...
  Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0)

The load itself succeeds, so this only shows once the system crashes.

The kernel being loaded may be any version or configuration that
reserves the padding, so the loader has to keep clear of it. Start the
other segments at the next PMD boundary. A crashkernel region too small
for them now fails the load with -EADDRNOTAVAIL, and one with enough
room boots the kdump kernel with its initrd.

This was found while testing multikernel, which loads a kernel with
kexec_file and hits the same overlap.

Fixes: 809a11eea8e8 ("riscv: kexec_file: Support loading Image binary file")
Signed-off-by: Cong Wang <cwang@multikernel.io>
---
 arch/riscv/kernel/machine_kexec_file.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/riscv/kernel/machine_kexec_file.c b/arch/riscv/kernel/machine_kexec_file.c
index 26cd2a8bd0cd..0d2d43a7dbfa 100644
--- a/arch/riscv/kernel/machine_kexec_file.c
+++ b/arch/riscv/kernel/machine_kexec_file.c
@@ -251,7 +251,8 @@ int load_extra_segments(struct kimage *image, unsigned long kernel_start,
 	char *modified_cmdline = NULL;
 
 	kbuf.image = image;
-	kbuf.buf_min = kernel_start + kernel_len;
+	/* The new kernel reserves itself up to the next PMD boundary */
+	kbuf.buf_min = ALIGN(kernel_start + kernel_len, PMD_SIZE);
 	kbuf.buf_max = PFN_PHYS(max_low_pfn);
 
 #ifdef CONFIG_CRASH_DUMP
-- 
2.43.0


                 reply	other threads:[~2026-11-14  3:49 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261114034901.107028-1-xiyou.wangcong@gmail.com \
    --to=xiyou.wangcong@gmail.com \
    --cc=alex@ghiti.fr \
    --cc=aou@eecs.berkeley.edu \
    --cc=baoquan.he@linux.dev \
    --cc=bjorn@kernel.org \
    --cc=cwang@multikernel.io \
    --cc=gaohan@iscas.ac.cn \
    --cc=guoren@kernel.org \
    --cc=kexec@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=palmer@dabbelt.com \
    --cc=pjw@kernel.org \
    --cc=rppt@kernel.org \
    --cc=ruanjinjie@huawei.com \
    --cc=songshuaishuai@tinylab.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®