Skip to content

Commit 4e21f22

Browse files
atishp04avpatel
authored andcommitted
RISC-V: KVM: Improve firmware counter read function
Rename the function to indicate that it is meant for firmware counter read. While at it, add a range sanity check for it as well. Reviewed-by: Andrew Jones <ajones@ventanamicro.com> Signed-off-by: Atish Patra <atishp@rivosinc.com> Reviewed-by: Anup Patel <anup@brainfault.org> Link: https://lore.kernel.org/r/20240420151741.962500-17-atishp@rivosinc.com Signed-off-by: Anup Patel <anup@brainfault.org>
1 parent 08fb07d commit 4e21f22

3 files changed

Lines changed: 8 additions & 3 deletions

File tree

arch/riscv/include/asm/kvm_vcpu_pmu.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vcpu, unsigned long ctr_ba
8989
unsigned long ctr_mask, unsigned long flags,
9090
unsigned long eidx, u64 evtdata,
9191
struct kvm_vcpu_sbi_return *retdata);
92-
int kvm_riscv_vcpu_pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx,
92+
int kvm_riscv_vcpu_pmu_fw_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx,
9393
struct kvm_vcpu_sbi_return *retdata);
9494
int kvm_riscv_vcpu_pmu_fw_ctr_read_hi(struct kvm_vcpu *vcpu, unsigned long cidx,
9595
struct kvm_vcpu_sbi_return *retdata);

arch/riscv/kvm/vcpu_pmu.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@ static int pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx,
235235
u64 enabled, running;
236236
int fevent_code;
237237

238+
if (cidx >= kvm_pmu_num_counters(kvpmu) || cidx == 1) {
239+
pr_warn("Invalid counter id [%ld] during read\n", cidx);
240+
return -EINVAL;
241+
}
242+
238243
pmc = &kvpmu->pmc[cidx];
239244

240245
if (pmc->cinfo.type == SBI_PMU_CTR_TYPE_FW) {
@@ -747,7 +752,7 @@ int kvm_riscv_vcpu_pmu_fw_ctr_read_hi(struct kvm_vcpu *vcpu, unsigned long cidx,
747752
return 0;
748753
}
749754

750-
int kvm_riscv_vcpu_pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx,
755+
int kvm_riscv_vcpu_pmu_fw_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx,
751756
struct kvm_vcpu_sbi_return *retdata)
752757
{
753758
int ret;

arch/riscv/kvm/vcpu_sbi_pmu.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static int kvm_sbi_ext_pmu_handler(struct kvm_vcpu *vcpu, struct kvm_run *run,
6262
ret = kvm_riscv_vcpu_pmu_ctr_stop(vcpu, cp->a0, cp->a1, cp->a2, retdata);
6363
break;
6464
case SBI_EXT_PMU_COUNTER_FW_READ:
65-
ret = kvm_riscv_vcpu_pmu_ctr_read(vcpu, cp->a0, retdata);
65+
ret = kvm_riscv_vcpu_pmu_fw_ctr_read(vcpu, cp->a0, retdata);
6666
break;
6767
case SBI_EXT_PMU_COUNTER_FW_READ_HI:
6868
if (IS_ENABLED(CONFIG_32BIT))

0 commit comments

Comments
 (0)