[PATCH] RISC-V: KVM: Validate PMU counter info using available counters

Anup Patel anup at brainfault.org
Fri Nov 13 03:47:10 PST 2026


On Tue, Oct 27, 2026 at 11:56 AM Qingwei Hu <qingwei.hu at bytedance.com> wrote:
>
> COUNTER_GET_INFO validates the counter index using the pmc[] storage
> capacity rather than the number of counters advertised by NUM_COUNTERS.
> When fewer counters are available, a guest can query an index outside
> the advertised range and receive success.
>
> Validate the index using kvm_pmu_num_counters(), matching the counter
> initialization and read paths.
>
> Fixes: 8f0153ecd3bf ("RISC-V: KVM: Add skeleton support for perf")
> Signed-off-by: Qingwei Hu <qingwei.hu at bytedance.com>

LGTM.

Reviewed-by: Anup Patel <anup at brainfault.org>

Queued this patch as a fix for Linux-7.4-rcX

Thanks,
Anup

> ---
>  arch/riscv/kvm/vcpu_pmu.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/arch/riscv/kvm/vcpu_pmu.c b/arch/riscv/kvm/vcpu_pmu.c
> index e095d1ff439f..ea788619091a 100644
> --- a/arch/riscv/kvm/vcpu_pmu.c
> +++ b/arch/riscv/kvm/vcpu_pmu.c
> @@ -560,7 +560,7 @@ int kvm_riscv_vcpu_pmu_ctr_info(struct kvm_vcpu *vcpu, unsigned long cidx,
>  {
>         struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu);
>
> -       if (cidx >= RISCV_KVM_MAX_COUNTERS || cidx == 1) {
> +       if (cidx >= kvm_pmu_num_counters(kvpmu) || cidx == 1) {
>                 retdata->err_val = SBI_ERR_INVALID_PARAM;
>                 return 0;
>         }
> --
> 2.39.5
>



More information about the linux-riscv mailing list