Skip to content

Commit bca031e

Browse files
Zenghui YuMarc Zyngier
authored andcommitted
KVM: arm/arm64: Introduce kvm_pmu_vcpu_init() to setup PMU counter index
We use "pmc->idx" and the "chained" bitmap to determine if the pmc is chained, in kvm_pmu_pmc_is_chained(). But idx might be uninitialized (and random) when we doing this decision, through a KVM_ARM_VCPU_INIT ioctl -> kvm_pmu_vcpu_reset(). And the test_bit() against this random idx will potentially hit a KASAN BUG [1]. In general, idx is the static property of a PMU counter that is not expected to be modified across resets, as suggested by Julien. It looks more reasonable if we can setup the PMU counter idx for a vcpu in its creation time. Introduce a new function - kvm_pmu_vcpu_init() for this basic setup. Oh, and the KASAN BUG will get fixed this way. [1] https://www.spinics.net/lists/kvm-arm/msg36700.html Fixes: 80f393a ("KVM: arm/arm64: Support chained PMU counters") Suggested-by: Andrew Murray <andrew.murray@arm.com> Suggested-by: Julien Thierry <julien.thierry@arm.com> Acked-by: Julien Thierry <julien.thierry@arm.com> Signed-off-by: Zenghui Yu <yuzenghui@huawei.com> Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
1 parent 5f9e832 commit bca031e

3 files changed

Lines changed: 19 additions & 3 deletions

File tree

‎include/kvm/arm_pmu.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ struct kvm_pmu {
3434
u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx);
3535
void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu, u64 select_idx, u64 val);
3636
u64 kvm_pmu_valid_counter_mask(struct kvm_vcpu *vcpu);
37+
void kvm_pmu_vcpu_init(struct kvm_vcpu *vcpu);
3738
void kvm_pmu_vcpu_reset(struct kvm_vcpu *vcpu);
3839
void kvm_pmu_vcpu_destroy(struct kvm_vcpu *vcpu);
3940
void kvm_pmu_disable_counter_mask(struct kvm_vcpu *vcpu, u64 val);
@@ -71,6 +72,7 @@ static inline u64 kvm_pmu_valid_counter_mask(struct kvm_vcpu *vcpu)
7172
{
7273
return 0;
7374
}
75+
static inline void kvm_pmu_vcpu_init(struct kvm_vcpu *vcpu) {}
7476
static inline void kvm_pmu_vcpu_reset(struct kvm_vcpu *vcpu) {}
7577
static inline void kvm_pmu_vcpu_destroy(struct kvm_vcpu *vcpu) {}
7678
static inline void kvm_pmu_disable_counter_mask(struct kvm_vcpu *vcpu, u64 val) {}

‎virt/kvm/arm/arm.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,8 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
340340
/* Set up the timer */
341341
kvm_timer_vcpu_init(vcpu);
342342

343+
kvm_pmu_vcpu_init(vcpu);
344+
343345
kvm_arm_reset_debug_ptr(vcpu);
344346

345347
return kvm_vgic_vcpu_init(vcpu);

‎virt/kvm/arm/pmu.c‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,20 @@ static void kvm_pmu_stop_counter(struct kvm_vcpu *vcpu, struct kvm_pmc *pmc)
214214
kvm_pmu_release_perf_event(pmc);
215215
}
216216

217+
/**
218+
* kvm_pmu_vcpu_init - assign pmu counter idx for cpu
219+
* @vcpu: The vcpu pointer
220+
*
221+
*/
222+
void kvm_pmu_vcpu_init(struct kvm_vcpu *vcpu)
223+
{
224+
int i;
225+
struct kvm_pmu *pmu = &vcpu->arch.pmu;
226+
227+
for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++)
228+
pmu->pmc[i].idx = i;
229+
}
230+
217231
/**
218232
* kvm_pmu_vcpu_reset - reset pmu state for cpu
219233
* @vcpu: The vcpu pointer
@@ -224,10 +238,8 @@ void kvm_pmu_vcpu_reset(struct kvm_vcpu *vcpu)
224238
int i;
225239
struct kvm_pmu *pmu = &vcpu->arch.pmu;
226240

227-
for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++) {
241+
for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++)
228242
kvm_pmu_stop_counter(vcpu, &pmu->pmc[i]);
229-
pmu->pmc[i].idx = i;
230-
}
231243

232244
bitmap_zero(vcpu->arch.pmu.chained, ARMV8_PMU_MAX_COUNTER_PAIRS);
233245
}

0 commit comments

Comments
 (0)