Skip to content

riscv: kexec: Make kexec/kdump robust under VS-mode

[email protected] kexec kvm kvm-riscv linux-riscv lkml 1 version New

Get series with b4
b4 am [email protected]

Versions newest first

  1. v3 Latest9 patches 0 repliesNew

Changes in v3 from the cover letter

- Add two new Fixes: patches at the start of the series:
#1: machine_kexec_cleanup() was empty, so the set_memory_x()
call in prepare() leaked an executable direct-map page back to the buddy allocator on kexec -u (W^X bypass). Fix: add set_memory_nx() in cleanup.
#2: machine_kexec_prepare() FDT search checked
memsz <= sizeof(fdt) but read sizeof(fdt) bytes from segment[i].buf, which can be smaller than memsz. Fix: check bufsz instead.
- Inline the .kexec.tramp.text section definition directly into vmlinux.lds.S instead of using a macro in image-vars.h.
- Rewrite map_tramp_page() to share a single set of lower-level page tables between the VA and PA mappings (5 BSS pages instead of 9), with a collision-safe walker that only populates entries still zero. Add Sv32 support.
- Link to v2:
https://lore.kernel.org/linux-riscv/[email protected]/
- Link to v1:
https://lore.kernel.org/linux-riscv/[email protected]/

Patches in v3 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/9riscv: kexec: Reset executable bit on the control code page in cleanuparch/riscv/kernel/machine_kexec.c+13 -00 replies
2/9riscv: kexec: Bound FDT search by source buffer size, not destinationarch/riscv/kernel/machine_kexec.c+1 -10 replies
3/9riscv: Add kexec trampoline text section to vmlinux.lds.Sarch/riscv/kernel/vmlinux.lds.S+10 -00 replies
4/9riscv: kexec: Place norelocate trampoline into .kexec.tramp.textarch/riscv/include/asm/kexec.h arch/riscv/kernel/kexec_relocate.S+5 -10 replies
5/9riscv: kexec: Build trampoline page tables for crash kernel entryarch/riscv/kernel/machine_kexec.c+87 -00 replies
6/9riscv: kexec: Switch to trampoline page table before norelocatearch/riscv/kernel/kexec_relocate.S arch/riscv/kernel/machine_kexec.c+63 -110 replies
7/9riscv: kexec: Always build the trampoline page tablearch/riscv/kernel/machine_kexec.c+10 -110 replies
8/9riscv: kexec: Add the relocate-trampoline wrapperarch/riscv/include/asm/kexec.h arch/riscv/kernel/kexec_relocate.S arch/riscv/kernel/machine_kexec.c+42 -00 replies
9/9riscv: kexec: Route normal kexec through the trampoline page tablearch/riscv/kernel/kexec_relocate.S arch/riscv/kernel/machine_kexec.c+25 -280 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v3 yet.

Read the v3 thread

Cover letter [PATCH v3 0/9] riscv: kexec: Make kexec/kdump robust under VS-mode

From: Fangyu Yu <[email protected]>

In a RISC-V kernel, both kexec and crashdump need to hand off execution to the next kernel after tearing down the current kernel address space. However, under virtualization the guest uses two-stage address translation, and pc does not jump to stvec after setting satp to zero, so the legacy single-step "csrw satp,0 + stvec redirect" sequence traps with "kvm run failed Operation not supported" and the VCPU dies.

This patch set introduces a dedicated kexec trampoline text section and builds a minimal trampoline page table for it. Both handoffs are then reworked into a two-pass trampoline:

1. First enter via the kernel VA, install the trampoline page table, and jump to the trampoline VA(=PA) of the entry stub;
2. Continue execution with PC already on a PA, drop SATP with csrw satp,0 (now safe because PC re-anchoring is moot), and jump directly to the target -- either the crash kernel entry (crash path) or the per-image control_code_buffer that runs the relocate body with SATP=0 throughout (normal path).

With this, both kexec and crashdump in RISC-V guests become robust against the two-stage translation.

Tested on QEMU virt under two configurations:

* HS-mode bare (QEMU TCG) -- regression check
- normal kexec: kexec -l/-e succeeds, second kernel boots and prints the userspace SECOND BOOT marker.
- crash kdump: panic triggers crash kernel boot, /proc/vmcore opens cleanly in crash(1) and shows the panic backtrace.

* VS-mode (L0 x86 + QEMU TCG -> L1 riscv64 + KVM -> L2) Before this series, both paths die with
kvm run failed Operation not supported
and an all-zero M-mode register dump on the SATP transition. After this series, both paths succeed end-to-end and the vmcore opens cleanly in crash.

---
Changes in v3 (Sashiko AI review):
- Add two new Fixes: patches at the start of the series:
#1: machine_kexec_cleanup() was empty, so the set_memory_x()
call in prepare() leaked an executable direct-map page back to the buddy allocator on kexec -u (W^X bypass). Fix: add set_memory_nx() in cleanup.
#2: machine_kexec_prepare() FDT search checked
memsz <= sizeof(fdt) but read sizeof(fdt) bytes from segment[i].buf, which can be smaller than memsz. Fix: check bufsz instead.
- Inline the .kexec.tramp.text section definition directly into vmlinux.lds.S instead of using a macro in image-vars.h.
- Rewrite map_tramp_page() to share a single set of lower-level page tables between the VA and PA mappings (5 BSS pages instead of 9), with a collision-safe walker that only populates entries still zero. Add Sv32 support.
- Link to v2:
https://lore.kernel.org/linux-riscv/[email protected]/
- Link to v1:
https://lore.kernel.org/linux-riscv/[email protected]/

Fangyu Yu (9):
riscv: kexec: Reset executable bit on the control code page in cleanup
riscv: kexec: Bound FDT search by source buffer size, not destination
riscv: Add kexec trampoline text section to vmlinux.lds.S
riscv: kexec: Place norelocate trampoline into .kexec.tramp.text
riscv: kexec: Build trampoline page tables for crash kernel entry
riscv: kexec: Switch to trampoline page table before norelocate
riscv: kexec: Always build the trampoline page table
riscv: kexec: Add the relocate-trampoline wrapper
riscv: kexec: Route normal kexec through the trampoline page table

 arch/riscv/include/asm/kexec.h     |   5 +
 arch/riscv/kernel/kexec_relocate.S |  92 +++++++++++-----
 arch/riscv/kernel/machine_kexec.c  | 171 +++++++++++++++++++++++++++--
 arch/riscv/kernel/vmlinux.lds.S    |  10 ++
 4 files changed, 241 insertions(+), 37 deletions(-)
Signature · 1 line
2.50.1