riscv: Add supervisor pointer masking infrastructure
cover riscv: Add supervisor pointer masking infrastructure
RVA23 requires an Sspm execution environment to provide supervisor-mode pointer masking with at least PMLEN=0 and PMLEN=7. SBI 3.0 FWFT provides the standard per-hart interface for selecting that value.
This RFC adds the architecture-private lifecycle needed by a future in-kernel tagged-pointer consumer. It deliberately does not enable pointer masking at boot and adds no userspace ABI, DT binding, hwprobe key, HWCAP, or cpuinfo token.
The consumer API performs an all-online-hart PMLEN=7 transaction. It only publishes success after every online hart accepts the setting, rolls back the complete mask before publishing failure, and rejects an incompatible hart before it becomes online after successful enablement.
The lifecycle code also restores a known setting across CPU hotplug and hibernation, clears retained state at boot, and clears every hart at its final kexec handoff point so an older successor kernel cannot inherit PMLEN=7. Returning to normal execution with an unknown retained PMLEN is treated as fatal.
Patch 2 is an independent RISC-V kexec fix: KEXEC_FILE-only SMP builds need HOTPLUG_CPU for machine_shutdown() to offline secondary harts. It is placed before the Sspm lifecycle code because safe outgoing handoff depends on that guarantee.
There is no tagged-pointer consumer in this series. I am sending this as an RFC to get feedback on the API, lifecycle boundaries, failure policy, and whether the infrastructure should instead be carried with its first consumer.
Specifications:
Pointer Masking Extensions, version 1.0
https://docs.riscv.org/reference/isa/v20260120/priv/zpm.html
RISC-V SBI specification, FWFT extension
https://docs.riscv.org/reference/sbi/_attachments/riscv-sbi.pdf
Tested on Linux v7.2-rc4 with a RISC-V cross compiler:
- W=1 builds with CONFIG_RISCV_ISA_SSPM=y and disabled
- KEXEC_FILE=y, KEXEC=n SMP configuration selects HOTPLUG_CPU
- strict checkpatch (apart from the generic new-file MAINTAINERS warning)
- sequential git apply --check against v7.2-rc4
Runtime SBI 3.0 FWFT, CPU hotplug, hibernation, kexec, and crash-kexec tests have not yet been performed.
Xie Bo (3):
riscv: sbi: Add FWFT get helper
riscv: kexec: Select HOTPLUG_CPU for KEXEC_FILE
riscv: Add supervisor pointer masking control
arch/riscv/Kconfig | 23 ++++ arch/riscv/include/asm/sbi.h | 1 + arch/riscv/include/asm/sspm.h | 78 ++++++++++++ arch/riscv/kernel/Makefile | 1 + arch/riscv/kernel/cpu-hotplug.c | 3 +- arch/riscv/kernel/hibernate.c | 12 ++ arch/riscv/kernel/machine_kexec.c | 10 +- arch/riscv/kernel/sbi.c | 29 +++++ arch/riscv/kernel/setup.c | 2 + arch/riscv/kernel/smp.c | 2 + arch/riscv/kernel/smpboot.c | 11 +- arch/riscv/kernel/sspm.c | 202 ++++++++++++++++++++++++++++++ 12 files changed, 368 insertions(+), 6 deletions(-) create mode 100644 arch/riscv/include/asm/sspm.h create mode 100644 arch/riscv/kernel/sspm.c
base-commit: 1590cf0329716306e948a8fc29f1d3ee87d3989f
Signature · 1 line
2.17.1
RFC PATCH 1/3 riscv: sbi: Add FWFT get helper
The SBI FWFT extension supports querying a feature's value on the local hart, but the RISC-V SBI wrapper currently only exposes SET operations.
Add a generic GET helper which maps SBI errors to Linux errno values and only updates the caller's output on success.
--- arch/riscv/include/asm/sbi.h | 1 + arch/riscv/kernel/sbi.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+)
arch/riscv/include/asm/sbi.h +1 -0
@@ -607,6 +607,7 @@ int sbi_remote_hfence_vvma_asid(const struct cpumask *cpu_mask, unsigned long asid);long sbi_probe_extension (int ext); +int sbi_fwft_get (u32 feature,unsigned long *value);int sbi_fwft_set (u32 feature,unsigned long value,unsigned long flags);int sbi_fwft_set_cpumask (const cpumask_t *mask,u32 feature,unsigned long value,unsigned long flags);
arch/riscv/kernel/sbi.c +29 -0
@@ -318,6 +318,35 @@ static void cpu_sbi_fwft_set(void *arg)atomic_set (&req->error, ret); } +/** +* sbi_fwft_get() - Get a feature value on the local hart +* @feature: The feature ID to get +* @value: Where to store the feature value +* +* Return: 0 on success, appropriate Linux error code otherwise. +*/ +int sbi_fwft_get (u32 feature,unsigned long *value) +{ +struct sbiret ret; +int error; + +if (!sbi_fwft_supported) +return -EOPNOTSUPP ; + +if (!value) +return -EINVAL ; + + ret =sbi_ecall (SBI_EXT_FWFT ,SBI_EXT_FWFT_GET , + feature,0 ,0 ,0 ,0 ,0 ); + error =sbi_err_map_linux_errno (ret.error); +if (error) +return error; + + *value = ret.value; + +return 0 ; +} +/** * sbi_fwft_set() - Set a feature on the local hart * @feature: The feature ID to be set
Signature · 1 line
2.17.1
RFC PATCH 2/3 riscv: kexec: Select HOTPLUG_CPU for KEXEC_FILE
In reply to Xie BoRISC-V machine_shutdown() uses CPU hotplug to offline secondary harts before handing control to the next kernel. ARCH_SELECTS_KEXEC selects HOTPLUG_CPU for SMP, but the KEXEC_FILE-only configuration does not.
Select HOTPLUG_CPU from ARCH_SELECTS_KEXEC_FILE as well so both loading interfaces use the same shutdown guarantee.
--- arch/riscv/Kconfig | 1 + 1 file changed, 1 insertion(+)
arch/riscv/Kconfig +1 -0
@@ -1097,6 +1097,7 @@ config ARCH_SUPPORTS_KEXEC_FILE config ARCH_SELECTS_KEXEC_FILE def_bool y depends on KEXEC_FILE + select HOTPLUG_CPU if SMP select HAVE_IMA_KEXEC if IMA select KEXEC_ELF
Signature · 1 line
2.17.1
RFC PATCH 3/3 riscv: Add supervisor pointer masking control
In reply to Xie BoProvide internal supervisor pointer-masking lifecycle support through SBI FWFT independently of optional in-kernel consumer enablement. This lets Linux establish a known PMLEN state even when the consumer is disabled.
Allow enabled consumers to select PMLEN=7 transactionally. Gate enable until smp_cpus_done() and serialize runtime state changes against CPU hotplug.
Preserve PMLEN in the same-kernel hibernation header and configure the resume hart immediately before restoring the memory image. Fail-stop if the fresh kernel cannot establish or recover verified PMLEN0.
Reset retained state on successor boot and secondary hart entry. Before kexec, reset every hart at its final offline or handoff point so an older kernel cannot inherit PMLEN7. Refuse a crash handoff while a possibly tagged hart remains stale.
Keep consumer enablement fail-closed on firmware errors. Roll back an unsuccessful initial transaction, distinguish rollback failure as a permanent broken state, and reject harts whose required local state cannot be established.
--- arch/riscv/Kconfig | 22 ++++ arch/riscv/include/asm/sspm.h | 78 ++++++++++++ arch/riscv/kernel/Makefile | 1 + arch/riscv/kernel/cpu-hotplug.c | 3 +- arch/riscv/kernel/hibernate.c | 12 ++ arch/riscv/kernel/machine_kexec.c | 10 +- arch/riscv/kernel/setup.c | 2 + arch/riscv/kernel/smp.c | 2 + arch/riscv/kernel/smpboot.c | 11 +- arch/riscv/kernel/sspm.c | 202 ++++++++++++++++++++++++++++++ 10 files changed, 337 insertions(+), 6 deletions(-) create mode 100644 arch/riscv/include/asm/sspm.h create mode 100644 arch/riscv/kernel/sspm.c
arch/riscv/Kconfig +22 -0
@@ -572,6 +572,28 @@ config RISCV_ISA_SUPM If this option is disabled, userspace will be unable to use the prctl(PR_{SET,GET}_TAGGED_ADDR_CTRL) API. +config RISCV_SSPM + bool + depends on 64BIT && RISCV_SBI + default y + help + Provide internal supervisor pointer-masking lifecycle support for + hibernation and for resetting firmware state retained across kexec. + +config RISCV_ISA_SSPM + bool "Sspm extension for supervisor pointer masking" + depends on RISCV_SSPM + default y + help + Allow an in-kernel consumer to enable + supervisor-mode pointer masking with PMLEN=7 through SBI FWFT. + + Pointer masking remains disabled until a kernel consumer explicitly + requests it. Internal reset and hibernation support remains available + when this consumer option is disabled. + + If unsure, say Y. + config RISCV_ISA_SVNAPOT bool "Svnapot extension support for supervisor mode NAPOT pages" depends on 64BIT && MMU
arch/riscv/include/asm/sspm.h new file +78 -0
78 lines not on this page · raw message
arch/riscv/kernel/Makefile +1 -0
@@ -101,6 +101,7 @@ obj-$(CONFIG_DYNAMIC_FTRACE) += mcount-dyn.o obj-$(CONFIG_PERF_EVENTS) += perf_callchain.o obj-$(CONFIG_HAVE_PERF_REGS) += perf_regs.o obj-$(CONFIG_RISCV_SBI) += sbi.o sbi_ecall.o +obj-$(CONFIG_RISCV_SSPM) += sspm.o ifeq ($(CONFIG_RISCV_SBI), y) obj-$(CONFIG_SMP) += sbi-ipi.o obj-$(CONFIG_SMP) += cpu_ops_sbi.o
arch/riscv/kernel/cpu-hotplug.c +2 -1
@@ -15,6 +15,7 @@#include <asm/cpu_ops.h> #include <asm/numa.h> #include <asm/smp.h> +#include <asm/sspm.h> bool cpu_has_hotplug (unsigned int cpu) {
@@ -67,8 +68,8 @@ void arch_cpuhp_cleanup_dead_cpu(unsigned int cpu)void __noreturn arch_cpu_idle_dead (void ) {idle_task_exit (); -cpuhp_ap_report_dead (); +riscv_sspm_reset_local_or_panic ("offline hart" ); cpu_ops->cpu_stop ();/* It should never reach here */
arch/riscv/kernel/hibernate.c +12 -0
@@ -16,6 +16,7 @@#include <asm/sections.h> #include <asm/set_memory.h> #include <asm/smp.h> +#include <asm/sspm.h> #include <asm/suspend.h> #include <linux/cpu.h>
@@ -53,12 +54,14 @@ struct arch_hibernate_hdr_invariants {* @hartid: to make sure same boot_cpu executes the hibernate/restore code. * @saved_satp: original page table used by the hibernated image. * @restore_cpu_addr: the kernel's image address to restore the CPU context. +* @pmlen: supervisor pointer-mask length used by the hibernated image. */ static struct arch_hibernate_hdr {struct arch_hibernate_hdr_invariants invariants;unsigned long hartid;unsigned long saved_satp;unsigned long restore_cpu_addr; +unsigned long pmlen; } resume_hdr;static void arch_hdr_invariants (struct arch_hibernate_hdr_invariants *i)
@@ -101,6 +104,7 @@ int arch_hibernation_header_save(void *addr, unsigned int max_size) hdr->hartid =cpuid_to_hartid_map (sleep_cpu); hdr->saved_satp =csr_read (CSR_SATP ); hdr->restore_cpu_addr = (unsigned long )__hibernate_cpu_resume; + hdr->pmlen =riscv_sspm_hibernate_pmlen ();return 0 ; }
@@ -121,6 +125,10 @@ int arch_hibernation_header_restore(void *addr)pr_crit ("Hibernate image not generated by this kernel!\n" );return -EINVAL ; } +if (!riscv_sspm_hibernate_pmlen_valid (hdr->pmlen)) { +pr_crit ("Invalid PMLEN in hibernate image: %lu\n" , hdr->pmlen); +return -EINVAL ; + } sleep_cpu =riscv_hartid_to_cpuid (hdr->hartid);if (sleep_cpu <0 ) {
@@ -395,6 +403,10 @@ int swsusp_arch_resume(void)if (ret)return ret; + ret =riscv_sspm_hibernate_restore (resume_hdr.pmlen); +if (ret) +return ret; +hibernate_restore_image (resume_hdr.saved_satp, (PFN_DOWN (__pa (resume_pg_dir)) | satp_mode), resume_hdr.restore_cpu_addr);
arch/riscv/kernel/machine_kexec.c +8 -2
@@ -12,6 +12,7 @@#include <asm/page.h> /* For PAGE_MASK */ #include <linux/libfdt.h> /* For fdt_check_header() */ #include <asm/set_memory.h> /* For set_memory_x() */ +#include <asm/sspm.h> #include <linux/compiler.h> /* For unreachable() */ #include <linux/cpu.h> /* For cpu_down() */ #include <linux/reboot.h>
@@ -161,8 +162,12 @@ machine_kexec(struct kimage *image)riscv_kexec_method kexec_method =NULL ;#ifdef CONFIG_SMP -WARN (smp_crash_stop_failed (), -"Some CPUs may be stale, kdump will be unreliable.\n" ); +bool stop_failed =smp_crash_stop_failed (); + +WARN (stop_failed, +"Some CPUs may be stale, kdump will be unreliable.\n" ); +if (stop_failed &&riscv_sspm_may_be_active ()) +panic ("Sspm: cannot hand off with stale PMLEN state" );#endif if (image->type !=KEXEC_TYPE_CRASH )
@@ -179,6 +184,7 @@ machine_kexec(struct kimage *image)/* Jump to the relocation code */ pr_notice ("Bye...\n" ); +riscv_sspm_reset_local_or_panic ("kexec boot hart" );kexec_method (first_ind_entry, jump_addr, fdt_addr, this_hart_id, kernel_map.va_pa_offset);unreachable ();
arch/riscv/kernel/setup.c +2 -0
@@ -34,6 +34,7 @@#include <asm/set_memory.h> #include <asm/sections.h> #include <asm/sbi.h> +#include <asm/sspm.h> #include <asm/tlbflush.h> #include <asm/thread_info.h> #include <asm/kasan.h>
@@ -315,6 +316,7 @@ void __init setup_arch(char **cmdline_p)early_ioremap_setup ();sbi_init (); +riscv_sspm_boot_reset ();jump_label_init ();parse_early_param ();
arch/riscv/kernel/smp.c +2 -0
@@ -27,6 +27,7 @@#include <asm/tlbflush.h> #include <asm/cacheflush.h> #include <asm/cpu_ops.h> +#include <asm/sspm.h> enum ipi_message_type {IPI_RESCHEDULE ,
@@ -92,6 +93,7 @@ static atomic_t waiting_for_crash_ipi = ATOMIC_INIT(0);static inline void ipi_cpu_crash_stop (unsigned int cpu,struct pt_regs *regs) {crash_save_cpu (regs, cpu); +riscv_sspm_reset_local_or_panic ("crash secondary hart" );atomic_dec (&waiting_for_crash_ipi);
arch/riscv/kernel/smpboot.c +8 -3
@@ -34,6 +34,7 @@#include <asm/tlbflush.h> #include <asm/sections.h> #include <asm/smp.h> +#include <asm/sspm.h> #include <uapi/asm/hwcap.h> #include <asm/vector.h>
@@ -210,6 +211,7 @@ int __cpu_up(unsigned int cpu, struct task_struct *tidle)void __init smp_cpus_done (unsigned int max_cpus) { +riscv_sspm_smp_cpus_done (); }/*
@@ -229,14 +231,17 @@ asmlinkage __visible void smp_callin(void)return ; } -/* All kernel threads share the same mm context. */ -mmgrab (mm); - current->active_mm = mm; +if (riscv_sspm_prepare_cpu ()) +return ;#ifdef CONFIG_HOTPLUG_PARALLEL cpuhp_ap_sync_alive ();#endif +/* All kernel threads share the same mm context. */ +mmgrab (mm); + current->active_mm = mm; +store_cpu_topology (curr_cpuid);notify_cpu_starting (curr_cpuid);
arch/riscv/kernel/sspm.c new file +202 -0
202 lines not on this page · raw message
Signature · 1 line
2.17.1