riscv: kexec: Fix VCPU crash on kexec/kdump under KVM
Subsystems (5)
v2 cover riscv: kexec: Fix VCPU crash on kexec/kdump under KVM
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.
v1 (Link below) addressed the crash path only. v2 extends the same mechanism to the normal kexec path (kexec -l/-e) and reworks the trampoline as a shared piece of infrastructure between the two paths.
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 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 v2:
- Extend the trampoline mechanism to the normal kexec path; the legacy stvec trick in riscv_kexec_relocate (csrw satp,0 followed by jr s6 to land at PA) is replaced by a small wrapper riscv_kexec_relocate_entry in .kexec.tramp.text that performs the same two-step transition used by the crash path and then hands off to the PA of control_code_buffer.
- Page-align both ends of the .kexec.tramp.text section so no .rodata neighbour can leak into the identity-mapped executable trampoline page, and assert the section fits within one page.
- In machine_kexec() pin t3 = 0 and the call arguments (a0..a4) to their ABI registers via local register asm variables and perform the final jr inside the inline asm block, so the compiler can never insert a scratch use of t3 between the argument setup and the trampoline entry. The bare "asm volatile ("li t3, 0" ::: "t3")" placebo used in v1 is not actually a guarantee.
- Link to v1:
https://lore.kernel.org/linux-riscv/[email protected]/
Fangyu Yu (7):
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/image-vars.h | 14 ++++ arch/riscv/kernel/kexec_relocate.S | 97 +++++++++++++++++------ arch/riscv/kernel/machine_kexec.c | 123 +++++++++++++++++++++++++++-- arch/riscv/kernel/vmlinux.lds.S | 1 + 5 files changed, 209 insertions(+), 31 deletions(-)
Signature · 1 line
2.50.1
PATCH v2 1/7 riscv: Add kexec trampoline text section to vmlinux.lds.S
From: Fangyu Yu <[email protected]>
When CONFIG_KEXEC_CORE is enabled, add a dedicated .kexec.tramp.text area to the RISC-V kernel linker script.
This introduces a KEXEC_TRAMP_TEXT linker snippet in image-vars.h and uses it from vmlinux.lds.S to:
- align both the start and the end to PAGE_SIZE
- define __kexec_tramp_text_start/__kexec_tramp_text_end
- KEEP all .kexec.tramp.text* input sections
- ASSERT the trampoline text fits within one page
The end-of-section page alignment guarantees that the trampoline page, which is later identity-mapped as PAGE_KERNEL_EXEC, contains nothing but the trampoline code and padding (no shared neighbour data).
When kexec is disabled, KEXEC_TRAMP_TEXT expands to nothing.
--- arch/riscv/kernel/image-vars.h | 14 ++++++++++++++ arch/riscv/kernel/vmlinux.lds.S | 1 + 2 files changed, 15 insertions(+)
arch/riscv/kernel/image-vars.h +14 -0
@@ -34,4 +34,18 @@ __efistub_sysfb_primary_display = sysfb_primary_display;#endif +#ifdef CONFIG_KEXEC_CORE +#define KEXEC_TRAMP_TEXT \ + . = ALIGN(PAGE_SIZE); \ + __kexec_tramp_text_start = .; \ + KEEP(*(.kexec.tramp.text)) \ + KEEP(*(.kexec.tramp.text.*)) \ + __kexec_tramp_text_end = .; \ + ASSERT((__kexec_tramp_text_end - __kexec_tramp_text_start) <= PAGE_SIZE, \ + ".kexec.tramp.text exceeds 4K"); \ + . = ALIGN(PAGE_SIZE); +#else +#define KEXEC_TRAMP_TEXT /* nothing */ +#endif +#endif /* __RISCV_KERNEL_IMAGE_VARS_H */
arch/riscv/kernel/vmlinux.lds.S +1 -0
@@ -41,6 +41,7 @@ SECTIONS ENTRY_TEXT IRQENTRY_TEXT SOFTIRQENTRY_TEXT + KEXEC_TRAMP_TEXT _etext = .; }
Signature · 1 line
2.50.1
[email protected]
AI reviewRe: [PATCH v2 1/7] riscv: Add kexec trampoline text section to vmlinux.lds.S
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Low] The `KEXEC_TRAMP_TEXT` macro introduces section layout directives but is placed in `image-vars.h`…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Low] The KEXEC_TRAMP_TEXT macro introduces section layout directives but is placed in image-vars.h, a header strictly intended for linker script variable assignments.
- [Low] The linker ASSERT failure message statically hardcodes '4K', which is inaccurate for RISC-V kernels configured with larger page sizes.
- [Low] The .kexec.tramp.text section and its boundary symbols are omitted when CONFIG_XIP_KERNEL is enabled alongside CONFIG_KEXEC_CORE, causing a build failure. --
commit 2ab699179f3a5599c71d8911590e4b8328996c8a
Author: Fangyu Yu <[email protected]>
riscv: Add kexec trampoline text section to vmlinux.lds.S
This commit adds a dedicated .kexec.tramp.text area to the RISC-V linker script when CONFIG_KEXEC_CORE is enabled. It aligns the section to PAGE_SIZE and adds an assertion to ensure the trampoline fits within a single page.
Quoted 5 lines
arch/riscv/kernel/image-vars.h +0
-0
arch/riscv/kernel/image-vars.h __efistub_screen_info = screen_info; · line 43
Quoted 10 lines
arch/riscv/kernel/image-vars.h +7
-0@@ -34,4 +34,18 @@ __efistub_screen_info = screen_info;#endif +#ifdef CONFIG_KEXEC_CORE +#define KEXEC_TRAMP_TEXT \ + . = ALIGN(PAGE_SIZE); \ + __kexec_tramp_text_start = .; \ + KEEP(*(.kexec.tramp.text)) \ + KEEP(*(.kexec.tramp.text.*)) \ + __kexec_tramp_text_end = .; \
[Severity: Low]
Does placing a section layout macro like KEXEC_TRAMP_TEXT in image-vars.h violate the architectural purpose of this header?
The header comment indicates it is intended only for "Linker script variables to be set after section resolution", rather than layout directives containing KEEP and ALIGN.
arch/riscv/kernel/image-vars.h __efistub_sysfb_primary_display = sysfb_primary_display; · line 45
Quoted 2 lines
arch/riscv/kernel/image-vars.h +2
-0+ASSERT ((__kexec_tramp_text_end - __kexec_tramp_text_start) <=PAGE_SIZE , \ +".kexec.tramp.text exceeds 4K" ); \
[Severity: Low]
Should the error message avoid hardcoding "4K" here?
RISC-V supports larger page sizes like 16KB and 64KB, so if the assertion dynamically checks against PAGE_SIZE and fails on a larger page size, emitting "exceeds 4K" might be misleading.
Quoted 11 lines
arch/riscv/kernel/image-vars.h +5
-0+ . = ALIGN(PAGE_SIZE); +#else +#define KEXEC_TRAMP_TEXT /* nothing */ +#endif +#endif /* __RISCV_KERNEL_IMAGE_VARS_H */ arch/riscv/kernel/vmlinux.lds.S +0
-0
arch/riscv/kernel/vmlinux.lds.S SECTIONS · line 48
Quoted 4 lines
arch/riscv/kernel/vmlinux.lds.S +1
-0@@ -45,6 +45,7 @@ SECTIONS ENTRY_TEXT IRQENTRY_TEXT SOFTIRQENTRY_TEXT + KEXEC_TRAMP_TEXT
[Severity: Low]
Will this cause a build failure when CONFIG_XIP_KERNEL is enabled alongside CONFIG_KEXEC_CORE?
When CONFIG_XIP_KERNEL is active, vmlinux.lds.S delegates to vmlinux-xip.lds.S and skips including image-vars.h.
Because the KEXEC_TRAMP_TEXT macro is never evaluated, the symbols __kexec_tramp_text_start and __kexec_tramp_text_end would be undefined when arch/riscv/kernel/machine_kexec.c tries to reference them.
Quoted 2 lines
arch/riscv/kernel/vmlinux.lds.S +0
-0_etext = .; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v2 2/7 riscv: kexec: Place norelocate trampoline into .kexec.tramp.text
In reply to [email protected]From: Fangyu Yu <[email protected]>
Move riscv_kexec_norelocate out of the generic .text section and into a dedicated executable trampoline section, .kexec.tramp.text.
--- arch/riscv/include/asm/kexec.h | 4 ++++ arch/riscv/kernel/kexec_relocate.S | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-)
arch/riscv/include/asm/kexec.h +4 -0
@@ -75,4 +75,8 @@ int load_extra_segments(struct kimage *image, unsigned long kernel_start, unsigned long cmdline_len);#endif +#ifndef __ASSEMBLY__ +extern char __kexec_tramp_text_start[]; +#endif +#endif
arch/riscv/kernel/kexec_relocate.S +1 -1
@@ -147,7 +147,7 @@ riscv_kexec_relocate_end: /* Used for jumping to crashkernel */ -.section ".text" +.section ".kexec.tramp.text", "ax" SYM_CODE_START(riscv_kexec_norelocate) /* * s0: (const) Phys address to jump to
Signature · 1 line
2.50.1
PATCH v2 3/7 riscv: kexec: Build trampoline page tables for crash kernel entry
In reply to [email protected]From: Fangyu Yu <[email protected]>
Crash kexec uses riscv_kexec_norelocate as a trampoline to jump into the crashkernel. Add a small helper to build dedicated 4KB page tables that map the trampoline page as executable.
Two mappings are installed:
- VA(__kexec_tramp_text_start) -> PA(__kexec_tramp_text_start)
- PA(__kexec_tramp_text_start) -> PA(__kexec_tramp_text_start)
This allows the trampoline to run regardless of whether it is entered via its linked virtual address or its physical address.
--- arch/riscv/kernel/machine_kexec.c | 67 +++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+)
arch/riscv/kernel/machine_kexec.c +67 -0
@@ -18,6 +18,65 @@#include <linux/interrupt.h> #include <linux/irq.h> +static pgd_t kexec_tramp_pgd[PTRS_PER_PGD ]__aligned(PAGE_SIZE) ; +static p4d_t kexec_tramp_p4d[PTRS_PER_P4D ]__aligned(PAGE_SIZE) ; +static pud_t kexec_tramp_pud[PTRS_PER_PUD ]__aligned(PAGE_SIZE) ; +static pmd_t kexec_tramp_pmd[PTRS_PER_PMD ]__aligned(PAGE_SIZE) ; +static pte_t kexec_tramp_pte[PTRS_PER_PTE ]__aligned(PAGE_SIZE) ; +static p4d_t kexec_tramp_p4d2[PTRS_PER_P4D ]__aligned(PAGE_SIZE) ; +static pud_t kexec_tramp_pud2[PTRS_PER_PUD ]__aligned(PAGE_SIZE) ; +static pmd_t kexec_tramp_pmd2[PTRS_PER_PMD ]__aligned(PAGE_SIZE) ; +static pte_t kexec_tramp_pte2[PTRS_PER_PTE ]__aligned(PAGE_SIZE) ; + +static void map_tramp_page (p4d_t *p4ds,pud_t *puds,pmd_t *pmds,pte_t *ptes, +unsigned long va,unsigned long pa) +{ +pgd_t *pgd = (pgd_t *)kexec_tramp_pgd +pgd_index (va); +pmd_t *pmd; + +if (pgtable_l5_enabled) { +p4d_t *p4d; + +set_pgd (pgd,pfn_pgd (PFN_DOWN (__pa_symbol (p4ds)),PAGE_TABLE )); + p4d = (p4d_t *)p4ds +p4d_index (va); +if (pgtable_l4_enabled) +set_p4d (p4d,pfn_p4d (PFN_DOWN (__pa_symbol (puds)), +PAGE_TABLE )); +else +set_p4d (p4d,pfn_p4d (PFN_DOWN (__pa_symbol (pmds)), +PAGE_TABLE )); + }else { +set_pgd (pgd,pfn_pgd (PFN_DOWN (__pa_symbol (puds)),PAGE_TABLE )); + } + +if (pgtable_l4_enabled) { +pud_t *pud = (pud_t *)puds +pud_index (va); + +set_pud (pud,pfn_pud (PFN_DOWN (__pa_symbol (pmds)),PAGE_TABLE )); + pmd = (pmd_t *)pmds +pmd_index (va); + }else { + pmd = (pmd_t *)puds +pmd_index (va); + } +set_pmd (pmd,pfn_pmd (PFN_DOWN (__pa_symbol (ptes)),PAGE_TABLE )); + +set_pte ((pte_t *)ptes +pte_index (va), +pfn_pte (PFN_DOWN (pa),PAGE_KERNEL_EXEC )); +} + +static void riscv_kexec_build_tramp (unsigned long va,unsigned long pa) +{ +/* VA -> PA: map the trampoline page via its kernel VA. */ +map_tramp_page (kexec_tramp_p4d, kexec_tramp_pud, + kexec_tramp_pmd, kexec_tramp_pte, va, pa); + +/* +* PA -> PA: identity-map the same page so the second-pass code +* can keep executing after the kernel VA mapping is dropped. +*/ +map_tramp_page (kexec_tramp_p4d2, kexec_tramp_pud2, + kexec_tramp_pmd2, kexec_tramp_pte2, pa, pa); +} +/* * machine_kexec_prepare - Initialize kexec *
@@ -73,6 +132,14 @@ machine_kexec_prepare(struct kimage *image)/* Mark the control page executable */ set_memory_x ((unsigned long ) control_code_buffer,1 ); + }else { +/* +* Crash kexec uses riscv_kexec_norelocate as a trampoline. +* Pre-build the trampoline page tables here so the panic +* path only has to switch satp and jump. +*/ +riscv_kexec_build_tramp ((unsigned long )__kexec_tramp_text_start, +__pa_symbol (__kexec_tramp_text_start)); }return 0 ;
Signature · 1 line
2.50.1
[email protected]
AI reviewRe: [PATCH v2 3/7] riscv: kexec: Build trampoline page tables for crash kernel entry
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
New issues:
- [Critical] Hardware Page Table Layout Violation on Sv32 (32-bit RISC-V)
- [High] Page Table Overwrite / Collision in riscv_kexec_build_tramp()
Pre-existing issues:
- [High] Privilege Escalation / W^X Protection Bypass via Missing Cleanup --
commit 2f8fd9ee19b545626c02a8751eacf676c55f551a
Author: Fangyu Yu <[email protected]>
riscv: kexec: Build trampoline page tables for crash kernel entry
Crash kexec uses riscv_kexec_norelocate as a trampoline to jump into the crashkernel. Add a small helper to build dedicated 4KB page tables that map the trampoline page as executable.
Quoted 7 lines
arch/riscv/kernel/machine_kexec.c +0
-0@@ -18,6 +18,65 @@#include <linux/interrupt.h> #include <linux/irq.h>
[ ... ]
Quoted 7 lines
arch/riscv/kernel/machine_kexec.c +7
-0+static void map_tramp_page (p4d_t *p4ds,pud_t *puds,pmd_t *pmds,pte_t *ptes, +unsigned long va,unsigned long pa) +{ +pgd_t *pgd = (pgd_t *)kexec_tramp_pgd +pgd_index (va); +pmd_t *pmd; + +if (pgtable_l5_enabled) {
[ ... ]
Quoted 5 lines
arch/riscv/kernel/machine_kexec.c +5
-0+ }else { +set_pgd (pgd,pfn_pgd (PFN_DOWN (__pa_symbol (puds)),PAGE_TABLE )); + } + +if (pgtable_l4_enabled) {
[ ... ]
arch/riscv/kernel/machine_kexec.c line 64
Quoted 8 lines
arch/riscv/kernel/machine_kexec.c +8
-0+ }else { + pmd = (pmd_t *)puds +pmd_index (va); + } +set_pmd (pmd,pfn_pmd (PFN_DOWN (__pa_symbol (ptes)),PAGE_TABLE )); + +set_pte ((pte_t *)ptes +pte_index (va), +pfn_pte (PFN_DOWN (pa),PAGE_KERNEL_EXEC )); +}
[Severity: Critical]
Does this construct an invalid 3-level page table hierarchy on 32-bit RISC-V?
On Sv32, the hardware MMU strictly expects a 2-level hierarchy (PGD -> PTE). Because both pgtable_l5_enabled and pgtable_l4_enabled are false on 32-bit, this code will set the PGD entry to point to puds, set puds[0] to point to ptes, and finally populate ptes.
When the Sv32 hardware MMU walks this table, it expects the table pointed to by PGD to be the leaf PTE table. If it reads puds[0], it will find a non-leaf PAGE_TABLE entry (RWX=0), which the hardware considers invalid, resulting in a fatal page fault.
arch/riscv/kernel/machine_kexec.c line 78
Quoted 13 lines
arch/riscv/kernel/machine_kexec.c +13
-0+static void riscv_kexec_build_tramp (unsigned long va,unsigned long pa) +{ +/* VA -> PA: map the trampoline page via its kernel VA. */ +map_tramp_page (kexec_tramp_p4d, kexec_tramp_pud, + kexec_tramp_pmd, kexec_tramp_pte, va, pa); + +/* +* PA -> PA: identity-map the same page so the second-pass code +* can keep executing after the kernel VA mapping is dropped. +*/ +map_tramp_page (kexec_tramp_p4d2, kexec_tramp_pud2, + kexec_tramp_pmd2, kexec_tramp_pte2, pa, pa); +}
[Severity: High]
Can the second call to map_tramp_page() overwrite the PGD entry created by the first call?
Both calls use the shared kexec_tramp_pgd root table. If va and pa happen to share the same PGD index (for example, on Sv39 if pa >> 30 & 511 matches va >> 30 & 511), the second call will replace the pointer to va's lower-level tables with a pointer to the identity-map's lower-level tables.
Since the identity-map tables only contain valid entries for pa's indices, attempting to execute from va after switching satp would result in a fatal instruction page fault.
arch/riscv/kernel/machine_kexec.c machine_kexec_prepare(struct kimage *image) · line 134
Quoted 4 lines
arch/riscv/kernel/machine_kexec.c +0
-0@@ -73,6 +132,14 @@ machine_kexec_prepare(struct kimage *image)/* Mark the control page executable */ set_memory_x ((unsigned long ) control_code_buffer,1 );
[Severity: High]
This is a pre-existing issue, but does this leak an executable page back to the system allocator if the kexec load fails or is unloaded?
When preparing a non-crash kexec image, the control code page is marked executable using set_memory_x(). However, if the user unloads the kexec image via kexec -u, or if the load fails later, kimage_free() returns this page to the buddy allocator.
Since machine_kexec_cleanup() is empty, the permissions are never reverted, violating W^X protections and leaving a window for privilege escalation where allocated memory can be executable.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH v2 4/7 riscv: kexec: Switch to trampoline page table before norelocate
In reply to [email protected]From: Fangyu Yu <[email protected]>
Make riscv_kexec_norelocate a two-pass trampoline so it can drop the kernel page tables while still executing from a mapped address.
On the first entry, t3 is initialized to 0 by machine_kexec(). Loads the physical address of riscv_kexec_norelocate and the trampoline SATP value, switches to the trampoline page table, and jumps to the trampoline VA(=PA).
On the second entry, t3 contains the physical address of riscv_kexec_norelocate, so the PC comparison matches and execution continues under trampoline VA(=PA).
Since the trampoline page table is already active, replace the previous stvec-based handoff with a direct jump to the target entry (jr a2).
--- arch/riscv/kernel/kexec_relocate.S | 32 ++++++++++++++++++---- arch/riscv/kernel/machine_kexec.c | 44 +++++++++++++++++++++++++++--- 2 files changed, 67 insertions(+), 9 deletions(-)
arch/riscv/kernel/kexec_relocate.S +27 -5
@@ -147,13 +147,35 @@ riscv_kexec_relocate_end: /* Used for jumping to crashkernel */ +.extern kexec_tramp_satp +.extern riscv_kexec_norelocate_pa .section ".kexec.tramp.text", "ax" SYM_CODE_START(riscv_kexec_norelocate) + /* + * Two-pass entry: + * - 1st entry: t3 == 0 (initialized by machine_kexec()). + * + * - 2nd entry: t3 holds the physical address of + * riscv_kexec_norelocate, so auipc matches t3 and we fall through + * to label 1 to continue execution under trampoline VA(=PA). + */ + auipc t0, 0 + beq t0, t3, 1f + + la t0, riscv_kexec_norelocate_pa + REG_L t3, 0(t0) + la t0, kexec_tramp_satp + REG_L t1, 0(t0) + csrw CSR_SATP, t1 + sfence.vma x0, x0 + + jr t3 /* * s0: (const) Phys address to jump to * s1: (const) Phys address of the FDT image * s2: (const) The hartid of the current hart */ +1: mv s0, a1 mv s1, a2 mv s2, a3
@@ -199,13 +221,13 @@ SYM_CODE_START(riscv_kexec_norelocate) csrw CSR_SSCRATCH, zero /* - * Switch to physical addressing - * This will also trigger a jump to CSR_STVEC - * which in this case is the address of the new - * kernel. + * We are already executing from the trampoline VA with the trampoline + * page table installed, so there is no need to rely on the old flow + * of programming stvec and taking the implicit trap on SATP switch. + * Jump directly to the target entry instead. */ - csrw CSR_STVEC, a2 csrw CSR_SATP, zero + jr a2 SYM_CODE_END(riscv_kexec_norelocate)
arch/riscv/kernel/machine_kexec.c +40 -4
64 lines not on this page · raw message
Signature · 1 line
2.50.1
[email protected]
AI reviewRe: [PATCH v2 4/7] riscv: kexec: Switch to trampoline page table before norelocate
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] Missing `sfence.vma` after `csrw CSR_SATP, zero` causes a fatal instruction fetch page fault…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] Missing sfence.vma after csrw CSR_SATP, zero causes a fatal instruction fetch page fault, leading to a system hang during crash kexec. --
commit 05a54b3ecf6222d16e88b0edb1efa0fb1ed8df09
Author: Fangyu Yu <[email protected]>
riscv: kexec: Switch to trampoline page table before norelocate
This patch converts the riscv_kexec_norelocate function into a two-pass trampoline. This allows dropping the kernel page tables while still executing from a mapped address, and replaces the old stvec-based handoff with a direct jump.
Quoted 4 lines
arch/riscv/kernel/kexec_relocate.S +0
-0
[ ... ]
Quoted 6 lines
arch/riscv/kernel/kexec_relocate.S +0
-2@@ -199,13 +221,13 @@ SYM_CODE_START(riscv_kexec_norelocate) csrw CSR_SSCRATCH, zero /* - * Switch to physical addressing - * This will also trigger a jump to CSR_STVEC
arch/riscv/kernel/kexec_relocate.S SYM_CODE_START(riscv_kexec_norelocate) · line 230
Quoted 10 lines
arch/riscv/kernel/kexec_relocate.S +5
-3- * which in this case is the address of the new - * kernel. + * We are already executing from the trampoline VA with the trampoline + * page table installed, so there is no need to rely on the old flow + * of programming stvec and taking the implicit trap on SATP switch. + * Jump directly to the target entry instead. */ - csrw CSR_STVEC, a2 csrw CSR_SATP, zero + jr a2
[Severity: Critical]
Does this sequence require an sfence.vma before the jump?
According to the RISC-V Privileged Architecture Manual, writes to the satp register do not guarantee that subsequent instruction fetches use the new translation until an sfence.vma is executed.
Without it, the CPU might fetch the target address a2 using the active trampoline page table instead of physical addressing. Since the crash kernel isn't mapped in the trampoline page table, could this trigger an Instruction Page Fault?
Without the stvec setup that was removed, the CPU would vector to the standard kernel trap handler. Because satp is now 0, the trap handler would be fetched as a physical address, which could result in a nested Instruction Access Fault and a total system hang during crash recovery.
Quoted 3 lines
arch/riscv/kernel/kexec_relocate.S +0
-0SYM_CODE_END(riscv_kexec_norelocate)
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
PATCH v2 5/7 riscv: kexec: Always build the trampoline page table
In reply to [email protected]From: Fangyu Yu <[email protected]>
The trampoline page table and the kexec_tramp_satp value are currently built only on the crash path. A follow-up patch needs the same infrastructure for the normal kexec path.
Pull the trampoline build and the WRITE_ONCE() that publishes the SATP value out of the crash-only else branch in machine_kexec_prepare(). The crash path keeps recording its own riscv_kexec_norelocate_pa; the normal path keeps its existing control_code_buffer copy.
No functional change.
--- arch/riscv/kernel/machine_kexec.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-)
arch/riscv/kernel/machine_kexec.c +10 -11
@@ -119,6 +119,16 @@ machine_kexec_prepare(struct kimage *image)return -EINVAL ; } +/* +* Build the trampoline page table and capture its SATP value. +* The crash path consumes it today; the non-crash kexec path +* will use the same setup as well. +*/ +riscv_kexec_build_tramp ((unsigned long )__kexec_tramp_text_start, +__pa_symbol (__kexec_tramp_text_start)); +WRITE_ONCE (kexec_tramp_satp, +PFN_DOWN (__pa_symbol (kexec_tramp_pgd)) | satp_mode); +/* Copy the assembler code for relocation to the control page */ if (image->type !=KEXEC_TYPE_CRASH ) { control_code_buffer =page_address (image->control_code_page);
@@ -135,19 +145,8 @@ machine_kexec_prepare(struct kimage *image)/* Mark the control page executable */ set_memory_x ((unsigned long ) control_code_buffer,1 ); }else { -/* -* Crash kexec uses riscv_kexec_norelocate as a trampoline. -* Pre-build the trampoline page tables and capture the -* trampoline SATP value plus the physical address of -* riscv_kexec_norelocate so that the panic path only has -* to switch satp and jump. -*/ -riscv_kexec_build_tramp ((unsigned long )__kexec_tramp_text_start, -__pa_symbol (__kexec_tramp_text_start));WRITE_ONCE (riscv_kexec_norelocate_pa,__pa_symbol (&riscv_kexec_norelocate)); -WRITE_ONCE (kexec_tramp_satp, -PFN_DOWN (__pa_symbol (kexec_tramp_pgd)) | satp_mode); }return 0 ;
Signature · 1 line
2.50.1
[email protected]
AI reviewRe: [PATCH v2 5/7] riscv: kexec: Always build the trampoline page table
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] W^X memory protection bypass due to missing set_memory_nx() in machine_kexec_cleanup()
- [High] Out-of-bounds read in machine_kexec_prepare() during FDT search leading to kernel panic --
commit 12b9a2069db4f35db2b3ed4b36105e4aefe8ff09
Author: Fangyu Yu <[email protected]>
riscv: kexec: Always build the trampoline page table
This patch refactors the kexec preparation path to build the trampoline page table and capture its satp value unconditionally, rather than only doing so for crash kernels.
arch/riscv/kernel/machine_kexec.c machine_kexec_prepare(struct kimage *image) · line 120
Quoted 7 lines
arch/riscv/kernel/machine_kexec.c +0
-0@@ -119,6 +119,16 @@ machine_kexec_prepare(struct kimage *image)return -EINVAL ; }
[Severity: High]
This is a pre-existing issue in the code just above this hunk, but can the device tree search loop trigger an out-of-bounds read?
When iterating over kexec segments to locate the device tree, the code checks memsz but not bufsz:
if (image->segment[i].memsz <= sizeof(fdt))
continue;
if (image->file_mode)
memcpy(&fdt, image->segment[i].buf, sizeof(fdt));If a segment is provided where p_filesz is smaller than sizeof(fdt) but p_memsz is larger, memcpy() will read past the end of the kernel-allocated image buffer. If this buffer happens to be adjacent to an unmapped guard page, could this result in a fatal page fault?
Quoted 14 lines
arch/riscv/kernel/machine_kexec.c +10
-0+/* +* Build the trampoline page table and capture its SATP value. +* The crash path consumes it today; the non-crash kexec path +* will use the same setup as well. +*/ +riscv_kexec_build_tramp ((unsigned long )__kexec_tramp_text_start, +__pa_symbol (__kexec_tramp_text_start)); +WRITE_ONCE (kexec_tramp_satp, +PFN_DOWN (__pa_symbol (kexec_tramp_pgd)) | satp_mode); +/* Copy the assembler code for relocation to the control page */ if (image->type !=KEXEC_TYPE_CRASH ) { control_code_buffer =page_address (image->control_code_page);
[ ... ]
arch/riscv/kernel/machine_kexec.c machine_kexec_prepare(struct kimage *image) · line 146
Quoted 3 lines
arch/riscv/kernel/machine_kexec.c +0
-0@@ -135,19 +145,8 @@ machine_kexec_prepare(struct kimage *image)/* Mark the control page executable */ set_memory_x ((unsigned long ) control_code_buffer,1 );
[Severity: High]
This isn't a bug introduced by this patch, but if the kexec payload is subsequently unloaded (like via kexec -u) or fails later in the load process, kimage_free() will call the architecture-specific machine_kexec_cleanup().
Since machine_kexec_cleanup() on riscv is an empty stub, it doesn't call set_memory_nx() to reverse this permission change. The page is then freed back to the buddy allocator via kimage_free_pages() while still retaining its executable permission.
Does this leave executable pages in the generic kernel allocator, bypassing write-xor-execute protections?
Quoted 17 lines
arch/riscv/kernel/machine_kexec.c +0
-11}else { -/* -* Crash kexec uses riscv_kexec_norelocate as a trampoline. -* Pre-build the trampoline page tables and capture the -* trampoline SATP value plus the physical address of -* riscv_kexec_norelocate so that the panic path only has -* to switch satp and jump. -*/ -riscv_kexec_build_tramp ((unsigned long )__kexec_tramp_text_start, -__pa_symbol (__kexec_tramp_text_start));WRITE_ONCE (riscv_kexec_norelocate_pa,__pa_symbol (&riscv_kexec_norelocate)); -WRITE_ONCE (kexec_tramp_satp, -PFN_DOWN (__pa_symbol (kexec_tramp_pgd)) | satp_mode); }return 0 ;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
PATCH v2 6/7 riscv: kexec: Add the relocate-trampoline wrapper
In reply to [email protected]From: Fangyu Yu <[email protected]>
Add riscv_kexec_relocate_entry to .kexec.tramp.text and the two asm-visible globals (riscv_kexec_relocate_entry_pa and riscv_kexec_cc_buffer_pa) that the wrapper consumes.
The wrapper performs the same two-step transition used by the crash
path: switch to the trampoline pgd, jump to the PA of self, then drop the MMU with PC already on a PA. It finally jumps to the PA of control_code_buffer.
machine_kexec_prepare() publishes the wrapper PA and the control_code_buffer PA via WRITE_ONCE for non-crash images. Nothing routes to the wrapper yet; the switchover happens in the follow-up patch.
--- arch/riscv/include/asm/kexec.h | 1 + arch/riscv/kernel/kexec_relocate.S | 37 ++++++++++++++++++++++++++++++ arch/riscv/kernel/machine_kexec.c | 7 ++++++ 3 files changed, 45 insertions(+)
arch/riscv/include/asm/kexec.h +1 -0
@@ -53,6 +53,7 @@ typedef void (*riscv_kexec_method)(unsigned long first_ind_entry, unsigned long va_pa_off);extern riscv_kexec_method riscv_kexec_norelocate; +extern riscv_kexec_method riscv_kexec_relocate_entry;#ifdef CONFIG_KEXEC_FILE extern const struct kexec_file_ops elf_kexec_ops;
arch/riscv/kernel/kexec_relocate.S +37 -0
@@ -231,6 +231,43 @@ SYM_CODE_START(riscv_kexec_norelocate) SYM_CODE_END(riscv_kexec_norelocate) +.extern riscv_kexec_relocate_entry_pa +.extern riscv_kexec_cc_buffer_pa +.section ".kexec.tramp.text", "ax" +SYM_CODE_START(riscv_kexec_relocate_entry) + /* + * Two-pass entry, identical in shape to riscv_kexec_norelocate: + * - 1st entry: t3 == 0 (initialized by machine_kexec()). + * - 2nd entry: t3 == PA of riscv_kexec_relocate_entry, so auipc + * matches t3 and we fall through to label 1. + * Args a0..a4 are passed through unchanged to riscv_kexec_relocate. + */ + auipc t0, 0 + beq t0, t3, 1f + + la t0, riscv_kexec_relocate_entry_pa + REG_L t3, 0(t0) + la t0, kexec_tramp_satp + REG_L t1, 0(t0) + csrw CSR_SATP, t1 + sfence.vma x0, x0 + + jr t3 +1: + /* + * Now executing at the PA of this wrapper with the trampoline pgd + * installed (identity-mapped). Drop the MMU; PC stays valid because + * it is already a PA. + */ + csrw CSR_SATP, zero + sfence.vma x0, x0 + + /* Jump to the PA of control_code_buffer to run the relocate body. */ + la t0, riscv_kexec_cc_buffer_pa + REG_L t0, 0(t0) + jr t0 +SYM_CODE_END(riscv_kexec_relocate_entry) + .section ".rodata" SYM_DATA(riscv_kexec_relocate_size, .long riscv_kexec_relocate_end - riscv_kexec_relocate)
arch/riscv/kernel/machine_kexec.c +7 -0
@@ -20,6 +20,8 @@unsigned long kexec_tramp_satp;unsigned long riscv_kexec_norelocate_pa; +unsigned long riscv_kexec_relocate_entry_pa; +unsigned long riscv_kexec_cc_buffer_pa;static pgd_t kexec_tramp_pgd[PTRS_PER_PGD ]__aligned(PAGE_SIZE) ;static p4d_t kexec_tramp_p4d[PTRS_PER_P4D ]__aligned(PAGE_SIZE) ;static pud_t kexec_tramp_pud[PTRS_PER_PUD ]__aligned(PAGE_SIZE) ;
@@ -144,6 +146,11 @@ machine_kexec_prepare(struct kimage *image)/* Mark the control page executable */ set_memory_x ((unsigned long ) control_code_buffer,1 ); + +WRITE_ONCE (riscv_kexec_relocate_entry_pa, +__pa_symbol (&riscv_kexec_relocate_entry)); +WRITE_ONCE (riscv_kexec_cc_buffer_pa, +__pa (control_code_buffer)); }else {WRITE_ONCE (riscv_kexec_norelocate_pa,__pa_symbol (&riscv_kexec_norelocate));
Signature · 7 lines
2.50.1 -- kvm-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kvm-riscv
[email protected]
AI reviewRe: [PATCH v2 6/7] riscv: kexec: Add the relocate-trampoline wrapper
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
New issues:
- [High] Writing per-image physical addresses to global variables during the 'prepare' phase causes a Use-After-Free and state corruption if the kexec load fails.
- [High] The assembly wrapper incorrectly calculates physical addresses for .bss variables on CONFIG_XIP_KERNEL builds by assuming virtual offsets always match physical offsets.
Pre-existing issues:
- [High] The kexec control page is made executable but never marked non-executable when freed, and is left writable, violating W^X protections. --
commit 6032faa530dc53961ca595e035246adbe5c3514a
Author: Fangyu Yu <[email protected]>
riscv: kexec: Add the relocate-trampoline wrapper
This commit adds a new assembly wrapper for kexec on RISC-V that handles the two-step MMU teardown process. It also updates the preparation phase to publish the wrapper and control code buffer physical addresses to global variables.
Quoted 2 lines
arch/riscv/kernel/kexec_relocate.S +0
-0
[ ... ]
Quoted 1 line
arch/riscv/kernel/kexec_relocate.S +0
-0@@ -231,6 +231,43 @@ SYM_CODE_START(riscv_kexec_norelocate)
[ ... ]
arch/riscv/kernel/kexec_relocate.S SYM_CODE_START(riscv_kexec_norelocate) · line 266
Quoted 10 lines
arch/riscv/kernel/kexec_relocate.S +10
-0+ /* + * Now executing at the PA of this wrapper with the trampoline pgd + * installed (identity-mapped). Drop the MMU; PC stays valid because + * it is already a PA. + */ + csrw CSR_SATP, zero + sfence.vma x0, x0 + + /* Jump to the PA of control_code_buffer to run the relocate body. */ + la t0, riscv_kexec_cc_buffer_pa
[Severity: High]
Does this PC-relative load calculate the correct physical address on CONFIG_XIP_KERNEL builds?
On XIP kernels, .text executes from ROM and .data/.bss reside in RAM. Because this code executes with the MMU disabled, a PC-relative load like la will compute the distance relative to the ROM address.
Could this lead to computing an incorrect physical address and causing an unhandled fault?
Quoted 6 lines
arch/riscv/kernel/kexec_relocate.S +3
-0+ REG_L t0, 0(t0) + jr t0 +SYM_CODE_END(riscv_kexec_relocate_entry)arch/riscv/kernel/machine_kexec.c +0
-0
[ ... ]
arch/riscv/kernel/machine_kexec.c machine_kexec_prepare(struct kimage *image) · line 148
Quoted 4 lines
arch/riscv/kernel/machine_kexec.c +0
-0@@ -144,6 +146,11 @@ machine_kexec_prepare(struct kimage *image)/* Mark the control page executable */ set_memory_x ((unsigned long ) control_code_buffer,1 );
[Severity: High]
This is a pre-existing issue, but does this leak executable pages back into the general allocation pool?
If the kexec load fails, or if the image is later unloaded, kimage_free() will return this page to the allocator. Since arch_kexec_pre_free_pages() isn't implemented to clear _PAGE_EXEC, does this return an executable page to the standard page allocator?
arch/riscv/kernel/machine_kexec.c machine_kexec_prepare(struct kimage *image) · line 153
Quoted 5 lines
arch/riscv/kernel/machine_kexec.c +5
-0+ +WRITE_ONCE (riscv_kexec_relocate_entry_pa, +__pa_symbol (&riscv_kexec_relocate_entry)); +WRITE_ONCE (riscv_kexec_cc_buffer_pa, +__pa (control_code_buffer));
[Severity: High]
Can writing per-image addresses to global variables during the prepare phase cause a use-after-free if the load fails?
If do_kexec_load() fails after this point (for example, due to an invalid segment in kimage_load_segment()), the new image and its control page are freed by kimage_free().
If a previously loaded, valid kexec image was already in place, won't a subsequent kexec reboot read these corrupted global variables and jump to the freed page?
Quoted 3 lines
arch/riscv/kernel/machine_kexec.c +0
-0}else {WRITE_ONCE (riscv_kexec_norelocate_pa,__pa_symbol (&riscv_kexec_norelocate));
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
PATCH v2 7/7 riscv: kexec: Route normal kexec through the trampoline page table
In reply to [email protected]From: Fangyu Yu <[email protected]>
riscv_kexec_relocate (copied into control_code_buffer) uses an stvec trick to drop the MMU and land on the PA of the next loop label. Under VS-mode KVM cannot emulate this single-step transition and the VCPU dies with "kvm run failed Operation not supported".
Route normal kexec through riscv_kexec_relocate_entry, the trampoline wrapper added in the previous patch. It drops SATP with PC already on a PA, then hands off to control_code_buffer where the relocate body runs with SATP=0.
Drop the stvec trick from the relocate body and pass first_ind_entry as a physical address since the body now starts with SATP=0. The ".align 2" plus filler "nop" that ensured the PA of the loop top was 4-byte aligned -- required because the legacy stvec trick wrote that PA into stvec.BASE, whose low two bits are MODE and are discarded by the hardware -- is no longer load-bearing and is removed as well.
--- arch/riscv/kernel/kexec_relocate.S | 26 ++++++-------------------- arch/riscv/kernel/machine_kexec.c | 10 +++++++--- 2 files changed, 13 insertions(+), 23 deletions(-)
arch/riscv/kernel/kexec_relocate.S +6 -20
@@ -34,27 +34,13 @@ SYM_CODE_START(riscv_kexec_relocate) csrw CSR_SIP, zero /* - * When we switch SATP.MODE to "Bare" we'll only - * play with physical addresses. However the first time - * we try to jump somewhere, the offset on the jump - * will be relative to pc which will still be on VA. To - * deal with this we set stvec to the physical address at - * the start of the loop below so that we jump there in - * any case. + * The trampoline wrapper (riscv_kexec_relocate_entry) has already + * dropped the MMU and handed control to us at this PA copy of the + * relocate code. From here on the entire loop runs with SATP=0 and + * every address (s0, s5, source/dest pointers) is a physical one. */ - la s6, 1f - sub s6, s6, s4 - csrw CSR_STVEC, s6 - - /* - * With C-extension, here we get 42 Bytes and the next - * .align directive would pad zeros here up to 44 Bytes. - * So manually put a nop here to avoid zeros padding. - */ - nop /* Process entries in a loop */ -.align 2 1: REG_L t0, 0(s0) /* t0 = *image->entry */ addi s0, s0, RISCV_SZPTR /* image->entry++ */
@@ -70,8 +56,8 @@ SYM_CODE_START(riscv_kexec_relocate) andi t1, t0, 0x2 beqz t1, 2f andi s0, t0, ~0x2 - csrw CSR_SATP, zero - jr s6 + /* MMU is already off; the entry wrapper handled the transition. */ + j 1b 2: /* IND_DONE entry ? -> jump to done label */
arch/riscv/kernel/machine_kexec.c +7 -3
@@ -231,11 +231,15 @@ machine_kexec(struct kimage *image) {struct kimage_arch *internal = &image->arch;unsigned long jump_addr = (unsigned long ) image->start; -unsigned long first_ind_entry = (unsigned long ) &image->head; +/* +* The relocate body runs entirely with the MMU off (the wrapper +* drops SATP before jumping into control_code_buffer), so the very +* first entry must be a physical address. +*/ +unsigned long first_ind_entry =__pa (&image->head);unsigned long this_cpu_id =__smp_processor_id ();unsigned long this_hart_id =cpuid_to_hartid_map (this_cpu_id);unsigned long fdt_addr = internal->fdt_addr; -void *control_code_buffer =page_address (image->control_code_page);riscv_kexec_method kexec_method =NULL ;#ifdef CONFIG_SMP
@@ -244,7 +248,7 @@ machine_kexec(struct kimage *image)#endif if (image->type !=KEXEC_TYPE_CRASH ) - kexec_method = control_code_buffer; + kexec_method = (riscv_kexec_method) &riscv_kexec_relocate_entry;else kexec_method = (riscv_kexec_method) &riscv_kexec_norelocate;
Signature · 1 line
2.50.1
[email protected]
AI reviewRe: [PATCH v2 7/7] riscv: kexec: Route normal kexec through the trampoline page table
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] The control code page (image->control_code_page) is marked executable using set_memory_x() during machine_kexec_prepare(), but this permission is never reverted. Because machine_kexec_cleanup() is empty, if the kexec image is freed, the page is returned to the buddy allocator while still retaining executable permissions. --
commit 1b8b8dd4e5bc4de88d60ccac9a5bd712badb4dd3
Author: Fangyu Yu <[email protected]>
riscv: kexec: Route normal kexec through the trampoline page table
This patch modifies the normal kexec path on RISC-V to route through a trampoline wrapper that disables the MMU before jumping to the relocation code. This resolves an emulation issue with KVM in VS-mode where the previous single-step transition failed.
Quoted 4 lines
arch/riscv/kernel/machine_kexec.c +0
-0
[ ... ]
arch/riscv/kernel/machine_kexec.c machine_kexec(struct kimage *image) · line 238
Quoted 10 lines
arch/riscv/kernel/machine_kexec.c +5
-1@@ -231,11 +231,15 @@ machine_kexec(struct kimage *image) {struct kimage_arch *internal = &image->arch;unsigned long jump_addr = (unsigned long ) image->start; -unsigned long first_ind_entry = (unsigned long ) &image->head; +/* +* The relocate body runs entirely with the MMU off (the wrapper +* drops SATP before jumping into control_code_buffer), so the very +* first entry must be a physical address. +*/
[Severity: High]
This is a pre-existing issue, but since the relocate body now runs entirely with the MMU off, is the set_memory_x() call for the control code page in machine_kexec_prepare() still necessary?
Currently, image->control_code_page is marked executable during preparation, but these permissions are never reverted because machine_kexec_cleanup() is empty. If the kexec image is freed, the page is returned to the buddy allocator while retaining its executable status. This leads to a persistent W^X violation when the page is later reallocated for regular kernel data.
Since SATP=0 during the execution of this buffer, does it make sense to remove the set_memory_x() call entirely to prevent this leak of executable permissions?
Quoted 6 lines
arch/riscv/kernel/machine_kexec.c +1
-1+unsigned long first_ind_entry =__pa (&image->head);unsigned long this_cpu_id =__smp_processor_id ();unsigned long this_hart_id =cpuid_to_hartid_map (this_cpu_id);unsigned long fdt_addr = internal->fdt_addr; -void *control_code_buffer =page_address (image->control_code_page);riscv_kexec_method kexec_method =NULL ;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7