Skip to content

Commit d9f89b4

Browse files
Andrew JonesMarc Zyngier
authored andcommitted
KVM: arm/arm64: PMU: Fix overflow interrupt injection
kvm_pmu_overflow_set() is called from perf's interrupt handler, making the call of kvm_vgic_inject_irq() from it introduced with "KVM: arm/arm64: PMU: remove request-less vcpu kick" a really bad idea, as it's quite easy to try and retake a lock that the interrupted context is already holding. The fix is to use a vcpu kick, leaving the interrupt injection to kvm_pmu_sync_hwstate(), like it was doing before the refactoring. We don't just revert, though, because before the kick was request-less, leaving the vcpu exposed to the request-less vcpu kick race, and also because the kick was used unnecessarily from register access handlers. Reviewed-by: Christoffer Dall <cdall@linaro.org> Signed-off-by: Andrew Jones <drjones@redhat.com> Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
1 parent 79962a5 commit d9f89b4

3 files changed

Lines changed: 16 additions & 31 deletions

File tree

‎arch/arm64/kvm/sys_regs.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ static bool access_pmovs(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
764764
if (p->is_write) {
765765
if (r->CRm & 0x2)
766766
/* accessing PMOVSSET_EL0 */
767-
kvm_pmu_overflow_set(vcpu, p->regval & mask);
767+
vcpu_sys_reg(vcpu, PMOVSSET_EL0) |= (p->regval & mask);
768768
else
769769
/* accessing PMOVSCLR_EL0 */
770770
vcpu_sys_reg(vcpu, PMOVSSET_EL0) &= ~(p->regval & mask);

‎include/kvm/arm_pmu.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ void kvm_pmu_vcpu_reset(struct kvm_vcpu *vcpu);
4848
void kvm_pmu_vcpu_destroy(struct kvm_vcpu *vcpu);
4949
void kvm_pmu_disable_counter(struct kvm_vcpu *vcpu, u64 val);
5050
void kvm_pmu_enable_counter(struct kvm_vcpu *vcpu, u64 val);
51-
void kvm_pmu_overflow_set(struct kvm_vcpu *vcpu, u64 val);
5251
void kvm_pmu_flush_hwstate(struct kvm_vcpu *vcpu);
5352
void kvm_pmu_sync_hwstate(struct kvm_vcpu *vcpu);
5453
bool kvm_pmu_should_notify_user(struct kvm_vcpu *vcpu);
@@ -86,7 +85,6 @@ static inline void kvm_pmu_vcpu_reset(struct kvm_vcpu *vcpu) {}
8685
static inline void kvm_pmu_vcpu_destroy(struct kvm_vcpu *vcpu) {}
8786
static inline void kvm_pmu_disable_counter(struct kvm_vcpu *vcpu, u64 val) {}
8887
static inline void kvm_pmu_enable_counter(struct kvm_vcpu *vcpu, u64 val) {}
89-
static inline void kvm_pmu_overflow_set(struct kvm_vcpu *vcpu, u64 val) {}
9088
static inline void kvm_pmu_flush_hwstate(struct kvm_vcpu *vcpu) {}
9189
static inline void kvm_pmu_sync_hwstate(struct kvm_vcpu *vcpu) {}
9290
static inline bool kvm_pmu_should_notify_user(struct kvm_vcpu *vcpu)

‎virt/kvm/arm/pmu.c‎

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -203,45 +203,27 @@ static u64 kvm_pmu_overflow_status(struct kvm_vcpu *vcpu)
203203
return reg;
204204
}
205205

206-
static void kvm_pmu_check_overflow(struct kvm_vcpu *vcpu)
206+
static void kvm_pmu_update_state(struct kvm_vcpu *vcpu)
207207
{
208208
struct kvm_pmu *pmu = &vcpu->arch.pmu;
209-
bool overflow = !!kvm_pmu_overflow_status(vcpu);
209+
bool overflow;
210+
211+
if (!kvm_arm_pmu_v3_ready(vcpu))
212+
return;
210213

214+
overflow = !!kvm_pmu_overflow_status(vcpu);
211215
if (pmu->irq_level == overflow)
212216
return;
213217

214218
pmu->irq_level = overflow;
215219

216220
if (likely(irqchip_in_kernel(vcpu->kvm))) {
217221
int ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id,
218-
pmu->irq_num, overflow,
219-
&vcpu->arch.pmu);
222+
pmu->irq_num, overflow, pmu);
220223
WARN_ON(ret);
221224
}
222225
}
223226

224-
/**
225-
* kvm_pmu_overflow_set - set PMU overflow interrupt
226-
* @vcpu: The vcpu pointer
227-
* @val: the value guest writes to PMOVSSET register
228-
*/
229-
void kvm_pmu_overflow_set(struct kvm_vcpu *vcpu, u64 val)
230-
{
231-
if (val == 0)
232-
return;
233-
234-
vcpu_sys_reg(vcpu, PMOVSSET_EL0) |= val;
235-
kvm_pmu_check_overflow(vcpu);
236-
}
237-
238-
static void kvm_pmu_update_state(struct kvm_vcpu *vcpu)
239-
{
240-
if (!kvm_arm_pmu_v3_ready(vcpu))
241-
return;
242-
kvm_pmu_check_overflow(vcpu);
243-
}
244-
245227
bool kvm_pmu_should_notify_user(struct kvm_vcpu *vcpu)
246228
{
247229
struct kvm_pmu *pmu = &vcpu->arch.pmu;
@@ -303,7 +285,7 @@ static inline struct kvm_vcpu *kvm_pmc_to_vcpu(struct kvm_pmc *pmc)
303285
}
304286

305287
/**
306-
* When perf event overflows, call kvm_pmu_overflow_set to set overflow status.
288+
* When the perf event overflows, set the overflow status and inform the vcpu.
307289
*/
308290
static void kvm_pmu_perf_overflow(struct perf_event *perf_event,
309291
struct perf_sample_data *data,
@@ -313,7 +295,12 @@ static void kvm_pmu_perf_overflow(struct perf_event *perf_event,
313295
struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
314296
int idx = pmc->idx;
315297

316-
kvm_pmu_overflow_set(vcpu, BIT(idx));
298+
vcpu_sys_reg(vcpu, PMOVSSET_EL0) |= BIT(idx);
299+
300+
if (kvm_pmu_overflow_status(vcpu)) {
301+
kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
302+
kvm_vcpu_kick(vcpu);
303+
}
317304
}
318305

319306
/**
@@ -341,7 +328,7 @@ void kvm_pmu_software_increment(struct kvm_vcpu *vcpu, u64 val)
341328
reg = lower_32_bits(reg);
342329
vcpu_sys_reg(vcpu, PMEVCNTR0_EL0 + i) = reg;
343330
if (!reg)
344-
kvm_pmu_overflow_set(vcpu, BIT(i));
331+
vcpu_sys_reg(vcpu, PMOVSSET_EL0) |= BIT(i);
345332
}
346333
}
347334
}

0 commit comments

Comments
 (0)