Skip to content

Commit 80f393a

Browse files
Andrew MurrayMarc Zyngier
authored andcommitted
KVM: arm/arm64: Support chained PMU counters
ARMv8 provides support for chained PMU counters, where an event type of 0x001E is set for odd-numbered counters, the event counter will increment by one for each overflow of the preceding even-numbered counter. Let's emulate this in KVM by creating a 64 bit perf counter when a user chains two emulated counters together. For chained events we only support generating an overflow interrupt on the high counter. We use the attributes of the low counter to determine the attributes of the perf event. Suggested-by: Marc Zyngier <marc.zyngier@arm.com> Signed-off-by: Andrew Murray <andrew.murray@arm.com> Reviewed-by: Julien Thierry <julien.thierry@arm.com> Reviewed-by: Suzuki K Poulose <suzuki.poulose@arm.com> Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
1 parent 218907c commit 80f393a

2 files changed

Lines changed: 217 additions & 37 deletions

File tree

‎include/kvm/arm_pmu.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <asm/perf_event.h>
1212

1313
#define ARMV8_PMU_CYCLE_IDX (ARMV8_PMU_MAX_COUNTERS - 1)
14+
#define ARMV8_PMU_MAX_COUNTER_PAIRS ((ARMV8_PMU_MAX_COUNTERS + 1) >> 1)
1415

1516
#ifdef CONFIG_KVM_ARM_PMU
1617

@@ -22,6 +23,7 @@ struct kvm_pmc {
2223
struct kvm_pmu {
2324
int irq_num;
2425
struct kvm_pmc pmc[ARMV8_PMU_MAX_COUNTERS];
26+
DECLARE_BITMAP(chained, ARMV8_PMU_MAX_COUNTER_PAIRS);
2527
bool ready;
2628
bool created;
2729
bool irq_level;

‎virt/kvm/arm/pmu.c‎

Lines changed: 215 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
static void kvm_pmu_create_perf_event(struct kvm_vcpu *vcpu, u64 select_idx);
1717

18+
#define PERF_ATTR_CFG1_KVM_PMU_CHAINED 0x1
19+
1820
/**
1921
* kvm_pmu_idx_is_64bit - determine if select_idx is a 64bit counter
2022
* @vcpu: The vcpu pointer
@@ -26,29 +28,126 @@ static bool kvm_pmu_idx_is_64bit(struct kvm_vcpu *vcpu, u64 select_idx)
2628
__vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_LC);
2729
}
2830

31+
static struct kvm_vcpu *kvm_pmc_to_vcpu(struct kvm_pmc *pmc)
32+
{
33+
struct kvm_pmu *pmu;
34+
struct kvm_vcpu_arch *vcpu_arch;
35+
36+
pmc -= pmc->idx;
37+
pmu = container_of(pmc, struct kvm_pmu, pmc[0]);
38+
vcpu_arch = container_of(pmu, struct kvm_vcpu_arch, pmu);
39+
return container_of(vcpu_arch, struct kvm_vcpu, arch);
40+
}
41+
2942
/**
30-
* kvm_pmu_get_counter_value - get PMU counter value
43+
* kvm_pmu_pmc_is_chained - determine if the pmc is chained
44+
* @pmc: The PMU counter pointer
45+
*/
46+
static bool kvm_pmu_pmc_is_chained(struct kvm_pmc *pmc)
47+
{
48+
struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
49+
50+
return test_bit(pmc->idx >> 1, vcpu->arch.pmu.chained);
51+
}
52+
53+
/**
54+
* kvm_pmu_idx_is_high_counter - determine if select_idx is a high/low counter
55+
* @select_idx: The counter index
56+
*/
57+
static bool kvm_pmu_idx_is_high_counter(u64 select_idx)
58+
{
59+
return select_idx & 0x1;
60+
}
61+
62+
/**
63+
* kvm_pmu_get_canonical_pmc - obtain the canonical pmc
64+
* @pmc: The PMU counter pointer
65+
*
66+
* When a pair of PMCs are chained together we use the low counter (canonical)
67+
* to hold the underlying perf event.
68+
*/
69+
static struct kvm_pmc *kvm_pmu_get_canonical_pmc(struct kvm_pmc *pmc)
70+
{
71+
if (kvm_pmu_pmc_is_chained(pmc) &&
72+
kvm_pmu_idx_is_high_counter(pmc->idx))
73+
return pmc - 1;
74+
75+
return pmc;
76+
}
77+
78+
/**
79+
* kvm_pmu_idx_has_chain_evtype - determine if the event type is chain
3180
* @vcpu: The vcpu pointer
3281
* @select_idx: The counter index
3382
*/
34-
u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx)
83+
static bool kvm_pmu_idx_has_chain_evtype(struct kvm_vcpu *vcpu, u64 select_idx)
3584
{
36-
u64 counter, reg, enabled, running;
37-
struct kvm_pmu *pmu = &vcpu->arch.pmu;
38-
struct kvm_pmc *pmc = &pmu->pmc[select_idx];
85+
u64 eventsel, reg;
3986

40-
reg = (select_idx == ARMV8_PMU_CYCLE_IDX)
41-
? PMCCNTR_EL0 : PMEVCNTR0_EL0 + select_idx;
42-
counter = __vcpu_sys_reg(vcpu, reg);
87+
select_idx |= 0x1;
88+
89+
if (select_idx == ARMV8_PMU_CYCLE_IDX)
90+
return false;
91+
92+
reg = PMEVTYPER0_EL0 + select_idx;
93+
eventsel = __vcpu_sys_reg(vcpu, reg) & ARMV8_PMU_EVTYPE_EVENT;
94+
95+
return eventsel == ARMV8_PMUV3_PERFCTR_CHAIN;
96+
}
97+
98+
/**
99+
* kvm_pmu_get_pair_counter_value - get PMU counter value
100+
* @vcpu: The vcpu pointer
101+
* @pmc: The PMU counter pointer
102+
*/
103+
static u64 kvm_pmu_get_pair_counter_value(struct kvm_vcpu *vcpu,
104+
struct kvm_pmc *pmc)
105+
{
106+
u64 counter, counter_high, reg, enabled, running;
43107

44-
/* The real counter value is equal to the value of counter register plus
108+
if (kvm_pmu_pmc_is_chained(pmc)) {
109+
pmc = kvm_pmu_get_canonical_pmc(pmc);
110+
reg = PMEVCNTR0_EL0 + pmc->idx;
111+
112+
counter = __vcpu_sys_reg(vcpu, reg);
113+
counter_high = __vcpu_sys_reg(vcpu, reg + 1);
114+
115+
counter = lower_32_bits(counter) | (counter_high << 32);
116+
} else {
117+
reg = (pmc->idx == ARMV8_PMU_CYCLE_IDX)
118+
? PMCCNTR_EL0 : PMEVCNTR0_EL0 + pmc->idx;
119+
counter = __vcpu_sys_reg(vcpu, reg);
120+
}
121+
122+
/*
123+
* The real counter value is equal to the value of counter register plus
45124
* the value perf event counts.
46125
*/
47126
if (pmc->perf_event)
48127
counter += perf_event_read_value(pmc->perf_event, &enabled,
49128
&running);
50129

51-
if (!kvm_pmu_idx_is_64bit(vcpu, select_idx))
130+
return counter;
131+
}
132+
133+
/**
134+
* kvm_pmu_get_counter_value - get PMU counter value
135+
* @vcpu: The vcpu pointer
136+
* @select_idx: The counter index
137+
*/
138+
u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx)
139+
{
140+
u64 counter;
141+
struct kvm_pmu *pmu = &vcpu->arch.pmu;
142+
struct kvm_pmc *pmc = &pmu->pmc[select_idx];
143+
144+
counter = kvm_pmu_get_pair_counter_value(vcpu, pmc);
145+
146+
if (kvm_pmu_pmc_is_chained(pmc) &&
147+
kvm_pmu_idx_is_high_counter(select_idx))
148+
counter = upper_32_bits(counter);
149+
150+
else if (!kvm_pmu_idx_is_64bit(vcpu, select_idx))
52151
counter = lower_32_bits(counter);
53152

54153
return counter;
@@ -78,6 +177,7 @@ void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu, u64 select_idx, u64 val)
78177
*/
79178
static void kvm_pmu_release_perf_event(struct kvm_pmc *pmc)
80179
{
180+
pmc = kvm_pmu_get_canonical_pmc(pmc);
81181
if (pmc->perf_event) {
82182
perf_event_disable(pmc->perf_event);
83183
perf_event_release_kernel(pmc->perf_event);
@@ -95,13 +195,23 @@ static void kvm_pmu_stop_counter(struct kvm_vcpu *vcpu, struct kvm_pmc *pmc)
95195
{
96196
u64 counter, reg;
97197

98-
if (pmc->perf_event) {
99-
counter = kvm_pmu_get_counter_value(vcpu, pmc->idx);
198+
pmc = kvm_pmu_get_canonical_pmc(pmc);
199+
if (!pmc->perf_event)
200+
return;
201+
202+
counter = kvm_pmu_get_pair_counter_value(vcpu, pmc);
203+
204+
if (kvm_pmu_pmc_is_chained(pmc)) {
205+
reg = PMEVCNTR0_EL0 + pmc->idx;
206+
__vcpu_sys_reg(vcpu, reg) = lower_32_bits(counter);
207+
__vcpu_sys_reg(vcpu, reg + 1) = upper_32_bits(counter);
208+
} else {
100209
reg = (pmc->idx == ARMV8_PMU_CYCLE_IDX)
101210
? PMCCNTR_EL0 : PMEVCNTR0_EL0 + pmc->idx;
102-
__vcpu_sys_reg(vcpu, reg) = counter;
103-
kvm_pmu_release_perf_event(pmc);
211+
__vcpu_sys_reg(vcpu, reg) = lower_32_bits(counter);
104212
}
213+
214+
kvm_pmu_release_perf_event(pmc);
105215
}
106216

107217
/**
@@ -118,6 +228,8 @@ void kvm_pmu_vcpu_reset(struct kvm_vcpu *vcpu)
118228
kvm_pmu_stop_counter(vcpu, &pmu->pmc[i]);
119229
pmu->pmc[i].idx = i;
120230
}
231+
232+
bitmap_zero(vcpu->arch.pmu.chained, ARMV8_PMU_MAX_COUNTER_PAIRS);
121233
}
122234

123235
/**
@@ -166,6 +278,18 @@ void kvm_pmu_enable_counter_mask(struct kvm_vcpu *vcpu, u64 val)
166278
continue;
167279

168280
pmc = &pmu->pmc[i];
281+
282+
/*
283+
* For high counters of chained events we must recreate the
284+
* perf event with the long (64bit) attribute set.
285+
*/
286+
if (kvm_pmu_pmc_is_chained(pmc) &&
287+
kvm_pmu_idx_is_high_counter(i)) {
288+
kvm_pmu_create_perf_event(vcpu, i);
289+
continue;
290+
}
291+
292+
/* At this point, pmc must be the canonical */
169293
if (pmc->perf_event) {
170294
perf_event_enable(pmc->perf_event);
171295
if (pmc->perf_event->state != PERF_EVENT_STATE_ACTIVE)
@@ -195,6 +319,18 @@ void kvm_pmu_disable_counter_mask(struct kvm_vcpu *vcpu, u64 val)
195319
continue;
196320

197321
pmc = &pmu->pmc[i];
322+
323+
/*
324+
* For high counters of chained events we must recreate the
325+
* perf event with the long (64bit) attribute unset.
326+
*/
327+
if (kvm_pmu_pmc_is_chained(pmc) &&
328+
kvm_pmu_idx_is_high_counter(i)) {
329+
kvm_pmu_create_perf_event(vcpu, i);
330+
continue;
331+
}
332+
333+
/* At this point, pmc must be the canonical */
198334
if (pmc->perf_event)
199335
perf_event_disable(pmc->perf_event);
200336
}
@@ -284,17 +420,6 @@ void kvm_pmu_sync_hwstate(struct kvm_vcpu *vcpu)
284420
kvm_pmu_update_state(vcpu);
285421
}
286422

287-
static inline struct kvm_vcpu *kvm_pmc_to_vcpu(struct kvm_pmc *pmc)
288-
{
289-
struct kvm_pmu *pmu;
290-
struct kvm_vcpu_arch *vcpu_arch;
291-
292-
pmc -= pmc->idx;
293-
pmu = container_of(pmc, struct kvm_pmu, pmc[0]);
294-
vcpu_arch = container_of(pmu, struct kvm_vcpu_arch, pmu);
295-
return container_of(vcpu_arch, struct kvm_vcpu, arch);
296-
}
297-
298423
/**
299424
* When the perf event overflows, set the overflow status and inform the vcpu.
300425
*/
@@ -385,44 +510,68 @@ static bool kvm_pmu_counter_is_enabled(struct kvm_vcpu *vcpu, u64 select_idx)
385510
static void kvm_pmu_create_perf_event(struct kvm_vcpu *vcpu, u64 select_idx)
386511
{
387512
struct kvm_pmu *pmu = &vcpu->arch.pmu;
388-
struct kvm_pmc *pmc = &pmu->pmc[select_idx];
513+
struct kvm_pmc *pmc;
389514
struct perf_event *event;
390515
struct perf_event_attr attr;
391516
u64 eventsel, counter, reg, data;
392517

393-
reg = (select_idx == ARMV8_PMU_CYCLE_IDX)
394-
? PMCCFILTR_EL0 : PMEVTYPER0_EL0 + select_idx;
518+
/*
519+
* For chained counters the event type and filtering attributes are
520+
* obtained from the low/even counter. We also use this counter to
521+
* determine if the event is enabled/disabled.
522+
*/
523+
pmc = kvm_pmu_get_canonical_pmc(&pmu->pmc[select_idx]);
524+
525+
reg = (pmc->idx == ARMV8_PMU_CYCLE_IDX)
526+
? PMCCFILTR_EL0 : PMEVTYPER0_EL0 + pmc->idx;
395527
data = __vcpu_sys_reg(vcpu, reg);
396528

397529
kvm_pmu_stop_counter(vcpu, pmc);
398530
eventsel = data & ARMV8_PMU_EVTYPE_EVENT;
399531

400532
/* Software increment event does't need to be backed by a perf event */
401533
if (eventsel == ARMV8_PMUV3_PERFCTR_SW_INCR &&
402-
select_idx != ARMV8_PMU_CYCLE_IDX)
534+
pmc->idx != ARMV8_PMU_CYCLE_IDX)
403535
return;
404536

405537
memset(&attr, 0, sizeof(struct perf_event_attr));
406538
attr.type = PERF_TYPE_RAW;
407539
attr.size = sizeof(attr);
408540
attr.pinned = 1;
409-
attr.disabled = !kvm_pmu_counter_is_enabled(vcpu, select_idx);
541+
attr.disabled = !kvm_pmu_counter_is_enabled(vcpu, pmc->idx);
410542
attr.exclude_user = data & ARMV8_PMU_EXCLUDE_EL0 ? 1 : 0;
411543
attr.exclude_kernel = data & ARMV8_PMU_EXCLUDE_EL1 ? 1 : 0;
412544
attr.exclude_hv = 1; /* Don't count EL2 events */
413545
attr.exclude_host = 1; /* Don't count host events */
414-
attr.config = (select_idx == ARMV8_PMU_CYCLE_IDX) ?
546+
attr.config = (pmc->idx == ARMV8_PMU_CYCLE_IDX) ?
415547
ARMV8_PMUV3_PERFCTR_CPU_CYCLES : eventsel;
416548

417-
counter = kvm_pmu_get_counter_value(vcpu, select_idx);
418-
/* The initial sample period (overflow count) of an event. */
419-
if (kvm_pmu_idx_is_64bit(vcpu, pmc->idx))
549+
counter = kvm_pmu_get_pair_counter_value(vcpu, pmc);
550+
551+
if (kvm_pmu_idx_has_chain_evtype(vcpu, pmc->idx)) {
552+
/**
553+
* The initial sample period (overflow count) of an event. For
554+
* chained counters we only support overflow interrupts on the
555+
* high counter.
556+
*/
420557
attr.sample_period = (-counter) & GENMASK(63, 0);
421-
else
422-
attr.sample_period = (-counter) & GENMASK(31, 0);
558+
event = perf_event_create_kernel_counter(&attr, -1, current,
559+
kvm_pmu_perf_overflow,
560+
pmc + 1);
561+
562+
if (kvm_pmu_counter_is_enabled(vcpu, pmc->idx + 1))
563+
attr.config1 |= PERF_ATTR_CFG1_KVM_PMU_CHAINED;
564+
} else {
565+
/* The initial sample period (overflow count) of an event. */
566+
if (kvm_pmu_idx_is_64bit(vcpu, pmc->idx))
567+
attr.sample_period = (-counter) & GENMASK(63, 0);
568+
else
569+
attr.sample_period = (-counter) & GENMASK(31, 0);
423570

424-
event = perf_event_create_kernel_counter(&attr, -1, current,
571+
event = perf_event_create_kernel_counter(&attr, -1, current,
425572
kvm_pmu_perf_overflow, pmc);
573+
}
574+
426575
if (IS_ERR(event)) {
427576
pr_err_once("kvm: pmu event creation failed %ld\n",
428577
PTR_ERR(event));
@@ -432,6 +581,33 @@ static void kvm_pmu_create_perf_event(struct kvm_vcpu *vcpu, u64 select_idx)
432581
pmc->perf_event = event;
433582
}
434583

584+
/**
585+
* kvm_pmu_update_pmc_chained - update chained bitmap
586+
* @vcpu: The vcpu pointer
587+
* @select_idx: The number of selected counter
588+
*
589+
* Update the chained bitmap based on the event type written in the
590+
* typer register.
591+
*/
592+
static void kvm_pmu_update_pmc_chained(struct kvm_vcpu *vcpu, u64 select_idx)
593+
{
594+
struct kvm_pmu *pmu = &vcpu->arch.pmu;
595+
struct kvm_pmc *pmc = &pmu->pmc[select_idx];
596+
597+
if (kvm_pmu_idx_has_chain_evtype(vcpu, pmc->idx)) {
598+
/*
599+
* During promotion from !chained to chained we must ensure
600+
* the adjacent counter is stopped and its event destroyed
601+
*/
602+
if (!kvm_pmu_pmc_is_chained(pmc))
603+
kvm_pmu_stop_counter(vcpu, pmc);
604+
605+
set_bit(pmc->idx >> 1, vcpu->arch.pmu.chained);
606+
} else {
607+
clear_bit(pmc->idx >> 1, vcpu->arch.pmu.chained);
608+
}
609+
}
610+
435611
/**
436612
* kvm_pmu_set_counter_event_type - set selected counter to monitor some event
437613
* @vcpu: The vcpu pointer
@@ -451,6 +627,8 @@ void kvm_pmu_set_counter_event_type(struct kvm_vcpu *vcpu, u64 data,
451627
? PMCCFILTR_EL0 : PMEVTYPER0_EL0 + select_idx;
452628

453629
__vcpu_sys_reg(vcpu, reg) = event_type;
630+
631+
kvm_pmu_update_pmc_chained(vcpu, select_idx);
454632
kvm_pmu_create_perf_event(vcpu, select_idx);
455633
}
456634

0 commit comments

Comments
 (0)