Kernel KVM virtualization development
 help / color / mirror / Atom feed
* [PATCH v2] RISC-V: Clear HSTATUS.HU on CPU initialization
@ 2026-09-24 10:16 Yong-Xuan Wang
  2026-09-24 10:28 ` sashiko-bot
  2026-10-01  3:02 ` Paul Walmsley
  0 siblings, 2 replies; 3+ messages in thread
From: Yong-Xuan Wang @ 2026-09-24 10:16 UTC (permalink / raw)
  To: Anup Patel, Atish Patra, Paul Walmsley, Palmer Dabbelt, Albert Ou,
	Alexandre Ghiti
  Cc: greentime.hu, vincent.chen, zong.li, kvm, kvm-riscv, linux-riscv,
	linux-kernel, Yong-Xuan Wang, Samuel Holland

The RISC-V privileged specification does not mandate HSTATUS reset
values, leaving the HU bit potentially set after hardware reset.
When HU=1, hypervisor instructions (HLV/HLVX/HSV) can execute in
U-mode to access guest memory, which may cause unintended behavior
if not explicitly controlled.

Clear HSTATUS.HU during CPU initialization to ensure hypervisor
instructions are only available in HS-mode, preventing unexpected
guest memory access from U-mode code.

Signed-off-by: Yong-Xuan Wang <yongxuan.wang@sifive.com>
Reviewed-by: Samuel Holland <samuel.holland@sifive.com>
---
Changes in v2:
- Clear the hstatus.hu in the arch core setup instead of kvm core setup
(sashiko)
- Link to v1: https://patch.msgid.link/20260817-hstatus_hu-v1-1-28c53cdfb408@sifive.com
---
 arch/riscv/include/asm/cpufeature.h |  2 ++
 arch/riscv/kernel/cpufeature.c      | 12 ++++++++++++
 arch/riscv/kernel/setup.c           |  2 ++
 arch/riscv/kernel/smpboot.c         |  2 ++
 arch/riscv/kernel/suspend.c         |  2 ++
 5 files changed, 20 insertions(+)

diff --git a/arch/riscv/include/asm/cpufeature.h b/arch/riscv/include/asm/cpufeature.h
index 739fcc84bf7b..5efa72823475 100644
--- a/arch/riscv/include/asm/cpufeature.h
+++ b/arch/riscv/include/asm/cpufeature.h
@@ -40,6 +40,8 @@ extern u32 thead_vlenb_of;
 
 void __init riscv_user_isa_enable(void);
 
+void riscv_clear_hypervisor_csr(void);
+
 #define _RISCV_ISA_EXT_DATA(_name, _id, _subset_exts, _subset_exts_size, _validate) {	\
 	.name = #_name,									\
 	.property = #_name,								\
diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c
index f46aa5602d74..80767a016602 100644
--- a/arch/riscv/kernel/cpufeature.c
+++ b/arch/riscv/kernel/cpufeature.c
@@ -1183,6 +1183,18 @@ void __init riscv_user_isa_enable(void)
 		pr_warn("Zicbop disabled as it is unavailable on some harts\n");
 }
 
+void riscv_clear_hypervisor_csr(void)
+{
+	if (!riscv_has_extension_unlikely(RISCV_ISA_EXT_h))
+		return;
+
+	/*
+	 * Clear HSTATUS.HU to restrict hypervisor instructions to HS-mode.
+	 * This prevents user-mode from executing HLV/HSV instructions.
+	 */
+	csr_clear(CSR_HSTATUS, HSTATUS_HU);
+}
+
 #ifdef CONFIG_RISCV_ALTERNATIVE
 /*
  * Alternative patch sites consider 48 bits when determining when to patch
diff --git a/arch/riscv/kernel/setup.c b/arch/riscv/kernel/setup.c
index 52d1d2b8f338..bf42efd848f6 100644
--- a/arch/riscv/kernel/setup.c
+++ b/arch/riscv/kernel/setup.c
@@ -364,6 +364,8 @@ void __init setup_arch(char **cmdline_p)
 
 	if (!IS_ENABLED(CONFIG_RISCV_ISA_ZBB) || !riscv_isa_extension_available(NULL, ZBB))
 		static_branch_disable(&efficient_ffs_key);
+
+	riscv_clear_hypervisor_csr();
 }
 
 bool arch_cpu_is_hotpluggable(int cpu)
diff --git a/arch/riscv/kernel/smpboot.c b/arch/riscv/kernel/smpboot.c
index f6ef57930b50..a9de2dae804c 100644
--- a/arch/riscv/kernel/smpboot.c
+++ b/arch/riscv/kernel/smpboot.c
@@ -244,6 +244,8 @@ asmlinkage __visible void smp_callin(void)
 
 	numa_add_cpu(curr_cpuid);
 
+	riscv_clear_hypervisor_csr();
+
 	pr_debug("CPU%u: Booted secondary hartid %lu\n", curr_cpuid,
 		cpuid_to_hartid_map(curr_cpuid));
 
diff --git a/arch/riscv/kernel/suspend.c b/arch/riscv/kernel/suspend.c
index 3efbf7874f3b..db220966f782 100644
--- a/arch/riscv/kernel/suspend.c
+++ b/arch/riscv/kernel/suspend.c
@@ -43,6 +43,8 @@ void suspend_save_csrs(struct suspend_context *context)
 
 void suspend_restore_csrs(struct suspend_context *context)
 {
+	riscv_clear_hypervisor_csr();
+
 	csr_write(CSR_SCRATCH, 0);
 	if (riscv_has_extension_unlikely(RISCV_ISA_EXT_XLINUXENVCFG))
 		csr_write(CSR_ENVCFG, context->envcfg);

---
base-commit: b5060a4aa33d7d78a8c1837ab08ef0c81ea96650
change-id: 20260817-hstatus_hu-a1a88a27c7b1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] RISC-V: Clear HSTATUS.HU on CPU initialization
  2026-09-24 10:16 [PATCH v2] RISC-V: Clear HSTATUS.HU on CPU initialization Yong-Xuan Wang
@ 2026-09-24 10:28 ` sashiko-bot
  2026-10-01  3:02 ` Paul Walmsley
  1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-09-24 10:28 UTC (permalink / raw)
  To: Yong-Xuan Wang; +Cc: kvm

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] riscv: incomplete HSTATUS.HU mitigation on asymmetric MP due to global check
--

commit 34c26113f845622c7a6f9c472ce2342eaac66df7
Author: Yong-Xuan Wang <yongxuan.wang@sifive.com>

RISC-V: Clear HSTATUS.HU on CPU initialization

This commit clears the HSTATUS.HU bit during CPU initialization to restrict
hypervisor instructions to HS-mode. This is intended to prevent unexpected
guest memory access from U-mode code.

> diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c
> index f46aa5602d74d..80767a0166026 100644
> --- a/arch/riscv/kernel/cpufeature.c
> +++ b/arch/riscv/kernel/cpufeature.c

[ ... ]

> @@ -1183,6 +1183,18 @@ void __init riscv_user_isa_enable(void)
>  		pr_warn("Zicbop disabled as it is unavailable on some harts\n");
>  }
>  
> +void riscv_clear_hypervisor_csr(void)
> +{
> +	if (!riscv_has_extension_unlikely(RISCV_ISA_EXT_h))
> +		return;

[Severity: Critical]
Does this global check cause a regression on asymmetric multiprocessor setups?

If the 'H' extension is not present on all CPUs, the global
riscv_has_extension_unlikely() check returns false because it relies on the
riscv_isa bitmap representing the intersection of all CPUs' extensions.

This would cause riscv_clear_hypervisor_csr() to exit early without clearing
HSTATUS.HU on the specific CPUs that do possess the 'H' extension. This could
potentially allow unprivileged U-mode processes on those capable CPUs to
execute hypervisor instructions (HLV/HSV) and access host physical memory.

Would it be better to use riscv_cpu_has_extension_unlikely(smp_processor_id(),
RISCV_ISA_EXT_h) here to ensure the check evaluates correctly for each CPU?

> +
> +	/*
> +	 * Clear HSTATUS.HU to restrict hypervisor instructions to HS-mode.
> +	 * This prevents user-mode from executing HLV/HSV instructions.
> +	 */
> +	csr_clear(CSR_HSTATUS, HSTATUS_HU);
> +}
> +

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260924-hstatus_hu-v2-1-7e970f5f1d8d@sifive.com?part=1

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] RISC-V: Clear HSTATUS.HU on CPU initialization
  2026-09-24 10:16 [PATCH v2] RISC-V: Clear HSTATUS.HU on CPU initialization Yong-Xuan Wang
  2026-09-24 10:28 ` sashiko-bot
@ 2026-10-01  3:02 ` Paul Walmsley
  1 sibling, 0 replies; 3+ messages in thread
From: Paul Walmsley @ 2026-10-01  3:02 UTC (permalink / raw)
  To: Yong-Xuan Wang
  Cc: Anup Patel, Atish Patra, Paul Walmsley, Palmer Dabbelt, Albert Ou,
	Alexandre Ghiti, greentime.hu, vincent.chen, zong.li, kvm,
	kvm-riscv, linux-riscv, linux-kernel, Samuel Holland

On Thu, 24 Sep 2026, Yong-Xuan Wang wrote:

> The RISC-V privileged specification does not mandate HSTATUS reset
> values, leaving the HU bit potentially set after hardware reset.
> When HU=1, hypervisor instructions (HLV/HLVX/HSV) can execute in
> U-mode to access guest memory, which may cause unintended behavior
> if not explicitly controlled.
> 
> Clear HSTATUS.HU during CPU initialization to ensure hypervisor
> instructions are only available in HS-mode, preventing unexpected
> guest memory access from U-mode code.
> 
> Signed-off-by: Yong-Xuan Wang <yongxuan.wang@sifive.com>
> Reviewed-by: Samuel Holland <samuel.holland@sifive.com>

Thanks, queued for v7.3-rc.


- Paul

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-10-01  3:02 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-24 10:16 [PATCH v2] RISC-V: Clear HSTATUS.HU on CPU initialization Yong-Xuan Wang
2026-09-24 10:28 ` sashiko-bot
2026-10-01  3:02 ` Paul Walmsley

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox