Re: [PATCH v7 3/4] RISC-V: KVM: Detect and expose supported HGATP G-stage modes

From: Radim Krčmář

Date: Thu Apr 02 2026 - 14:25:02 EST


2026-04-02T21:23:02+08:00, <fangyu.yu@xxxxxxxxxxxxxxxxx>:
> From: Fangyu Yu <fangyu.yu@xxxxxxxxxxxxxxxxx>
>
> Extend kvm_riscv_gstage_mode_detect() to record HGATP.MODE values in a
> bitmask. Keep tracking the maximum supported G-stage page table level
> for existing internal users.
>
> Also provide lightweight helpers to retrieve the supported-mode bitmask
> and validate a requested HGATP.MODE against it.
>
> Signed-off-by: Fangyu Yu <fangyu.yu@xxxxxxxxxxxxxxxxx>
> Reviewed-by: Andrew Jones <andrew.jones@xxxxxxxxxxxxxxxx>
> Reviewed-by: Guo Ren <guoren@xxxxxxxxxx>
> ---
> diff --git a/arch/riscv/include/asm/kvm_gstage.h b/arch/riscv/include/asm/kvm_gstage.h
> @@ -102,4 +103,14 @@ static inline void kvm_riscv_gstage_init(struct kvm_gstage *gstage, struct kvm *
> +static inline bool kvm_riscv_hgatp_mode_is_valid(unsigned long mode)
> +{
> + return kvm_riscv_gstage_supported_mode_mask & BIT(mode);

Shifting by more than the bit width is undefined behavior in C.
RV64 effectively translates BIT(mode) to 1UL << (mode & 0x3f), so this
could allow values larger than the mask.

Thanks.