Skip to contentSkip to conversation

KVM: arm/arm64: add support for chained counters

PATCH Superseded Andrew Murray 3 participants 13 messages Jun 12, 2019 – Jun 17, 2019

Subsystems (2)

v9 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v9 cover KVM: arm/arm64: add support for chained 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.

Testing has been performed by hard-coding hwc->sample_period in __hw_perf_event_init (arm_pmu.c) to a small value, this results in regular overflows (for non sampling events). The following command was then used to measure chained and non-chained instruction cycles:

perf stat -e armv8_pmuv3/long=1,inst_retired/u \
          -e armv8_pmuv3/long=0,inst_retired/u dd if=/dev/zero bs=1M \
          count=10 | gzip > /dev/null

The reported values were identical (and for non-chained was in the same ballpark when running on a kernel without this patchset). Debug was added to verify that the guest received overflow interrupts for the chain counter.

The test was also repeated using the cycle counter (cycle:u).

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.

Changes since v8:

- Correctly calculate the sample_period for the cycle counter

- Drop "arm64: perf: extract chain helper into header" patch

Changes since v7:

- Remove pmc->bitmask

- Remove a couple of instances of using kvm_pmu_get_canonical_pmc when not needed

- Remove unused perf_event variable

Changes since v6:

- Drop kvm_pmu_{get,set}_perf_event

- Avoid duplicate work by using kvm_pmu_get_pair_counter_value inside kvm_pmu_stop_counter

- Use GENMASK for 64bit mask

Changes since v5:

- Use kvm_pmu_pmc_is_high_counter instead of open coding

- Rename kvm_pmu_event_is_chained to kvm_pmu_idx_has_chain_evtype

- Use kvm_pmu_get_canonical_pmc only where needed and reintroduce the kvm_pmu_{set, get}_perf_event functions

- Drop masking of counter in kvm_pmu_get_pair_counter_value

- Only initialise pmc once in kvm_pmu_create_perf_event and other minor changes.

Changes since v4:

- Track pairs of chained counters with a bitmap instead of using a struct kvm_pmc_pair.

- Rebase onto kvmarm/queue

Changes since v3:

- Simplify approach by not creating events lazily and by introducing a struct kvm_pmc_pair to represent the relationship between adjacent counters.

- Rebase onto v5.1-rc2

Changes since v2:

- Rebased onto v5.0-rc7

- Add check for cycle counter in correct patch

- Minor style, naming and comment changes

- Extract armv8pmu_evtype_is_chain from arch/arm64/kernel/perf_event.c into a common header that KVM can use

Changes since v1:

- Rename kvm_pmu_{enable,disable}_counter to reflect that they can operate on multiple counters at once and use these functions where possible

- Fix bugs with overflow handing, kvm_pmu_get_counter_value did not take into consideration the perf counter value overflowing the low counter

- Ensure PMCCFILTR_EL0 is used when operating on the cycle counter

- Rename kvm_pmu_reenable_enabled_{pair, single} and similar

- Always create perf event disabled to simplify logic elsewhere

- Move PMCNTENSET_EL0 test to kvm_pmu_enable_counter_mask

Andrew Murray (5):
KVM: arm/arm64: rename kvm_pmu_{enable/disable}_counter functions
KVM: arm/arm64: extract duplicated code to own function
KVM: arm/arm64: re-create event when setting counter value
KVM: arm/arm64: remove pmc->bitmask
KVM: arm/arm64: support chained PMU counters

 arch/arm64/kvm/sys_regs.c |   4 +-
 include/kvm/arm_pmu.h     |  11 +-
 virt/kvm/arm/pmu.c        | 340 ++++++++++++++++++++++++++++++--------
 3 files changed, 281 insertions(+), 74 deletions(-)
Signature · 6 lines
2.21.0

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#1 lore raw

PATCH v9 1/5 KVM: arm/arm64: rename kvm_pmu_{enable/disable}_counter functions

The kvm_pmu_{enable/disable}_counter functions can enabled/disable multiple counters at once as they operate on a bitmask. Let's make this clearer by renaming the function.

---
 arch/arm64/kvm/sys_regs.c |  4 ++--
 include/kvm/arm_pmu.h     |  8 ++++----
 virt/kvm/arm/pmu.c        | 12 ++++++------
 3 files changed, 12 insertions(+), 12 deletions(-)
arch/arm64/kvm/sys_regs.c +2 -2
index 9d02643bc601..8e98fb173ed3 100644
@@ -876,12 +876,12 @@ static bool access_pmcnten(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
 		if (r->Op2 & 0x1) {
 			/* accessing PMCNTENSET_EL0 */
 			__vcpu_sys_reg(vcpu, PMCNTENSET_EL0) |= val;
-			kvm_pmu_enable_counter(vcpu, val);
+			kvm_pmu_enable_counter_mask(vcpu, val);
 			kvm_vcpu_pmu_restore_guest(vcpu);
 		} else {
 			/* accessing PMCNTENCLR_EL0 */
 			__vcpu_sys_reg(vcpu, PMCNTENSET_EL0) &= ~val;
-			kvm_pmu_disable_counter(vcpu, val);
+			kvm_pmu_disable_counter_mask(vcpu, val);
 		}
 	} else {
 		p->regval = __vcpu_sys_reg(vcpu, PMCNTENSET_EL0) & mask;
include/kvm/arm_pmu.h +4 -4
index f87fe20fcb05..b73f31baca52 100644
@@ -46,8 +46,8 @@ void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu, u64 select_idx, u64 val);
 u64 kvm_pmu_valid_counter_mask(struct kvm_vcpu *vcpu);
 void kvm_pmu_vcpu_reset(struct kvm_vcpu *vcpu);
 void kvm_pmu_vcpu_destroy(struct kvm_vcpu *vcpu);
-void kvm_pmu_disable_counter(struct kvm_vcpu *vcpu, u64 val);
-void kvm_pmu_enable_counter(struct kvm_vcpu *vcpu, u64 val);
+void kvm_pmu_disable_counter_mask(struct kvm_vcpu *vcpu, u64 val);
+void kvm_pmu_enable_counter_mask(struct kvm_vcpu *vcpu, u64 val);
 void kvm_pmu_flush_hwstate(struct kvm_vcpu *vcpu);
 void kvm_pmu_sync_hwstate(struct kvm_vcpu *vcpu);
 bool kvm_pmu_should_notify_user(struct kvm_vcpu *vcpu);
@@ -83,8 +83,8 @@ static inline u64 kvm_pmu_valid_counter_mask(struct kvm_vcpu *vcpu)
 }
 static inline void kvm_pmu_vcpu_reset(struct kvm_vcpu *vcpu) {}
 static inline void kvm_pmu_vcpu_destroy(struct kvm_vcpu *vcpu) {}
-static inline void kvm_pmu_disable_counter(struct kvm_vcpu *vcpu, u64 val) {}
-static inline void kvm_pmu_enable_counter(struct kvm_vcpu *vcpu, u64 val) {}
+static inline void kvm_pmu_disable_counter_mask(struct kvm_vcpu *vcpu, u64 val) {}
+static inline void kvm_pmu_enable_counter_mask(struct kvm_vcpu *vcpu, u64 val) {}
 static inline void kvm_pmu_flush_hwstate(struct kvm_vcpu *vcpu) {}
 static inline void kvm_pmu_sync_hwstate(struct kvm_vcpu *vcpu) {}
 static inline bool kvm_pmu_should_notify_user(struct kvm_vcpu *vcpu)
virt/kvm/arm/pmu.c +6 -6
index 1c5b76c46e26..c5a722ad283f 100644
@@ -135,13 +135,13 @@ u64 kvm_pmu_valid_counter_mask(struct kvm_vcpu *vcpu)
 }
 
 /**
- * kvm_pmu_enable_counter - enable selected PMU counter
+ * kvm_pmu_enable_counter_mask - enable selected PMU counters
  * @vcpu: The vcpu pointer
  * @val: the value guest writes to PMCNTENSET register
  *
  * Call perf_event_enable to start counting the perf event
  */
-void kvm_pmu_enable_counter(struct kvm_vcpu *vcpu, u64 val)
+void kvm_pmu_enable_counter_mask(struct kvm_vcpu *vcpu, u64 val)
 {
 	int i;
 	struct kvm_pmu *pmu = &vcpu->arch.pmu;
@@ -164,13 +164,13 @@ void kvm_pmu_enable_counter(struct kvm_vcpu *vcpu, u64 val)
 }
 
 /**
- * kvm_pmu_disable_counter - disable selected PMU counter
+ * kvm_pmu_disable_counter_mask - disable selected PMU counters
  * @vcpu: The vcpu pointer
  * @val: the value guest writes to PMCNTENCLR register
  *
  * Call perf_event_disable to stop counting the perf event
  */
-void kvm_pmu_disable_counter(struct kvm_vcpu *vcpu, u64 val)
+void kvm_pmu_disable_counter_mask(struct kvm_vcpu *vcpu, u64 val)
 {
 	int i;
 	struct kvm_pmu *pmu = &vcpu->arch.pmu;
@@ -347,10 +347,10 @@ void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val)
 
 	mask = kvm_pmu_valid_counter_mask(vcpu);
 	if (val & ARMV8_PMU_PMCR_E) {
-		kvm_pmu_enable_counter(vcpu,
+		kvm_pmu_enable_counter_mask(vcpu,
 		       __vcpu_sys_reg(vcpu, PMCNTENSET_EL0) & mask);
 	} else {
-		kvm_pmu_disable_counter(vcpu, mask);
+		kvm_pmu_disable_counter_mask(vcpu, mask);
 	}
 
 	if (val & ARMV8_PMU_PMCR_C)
Signature · 6 lines
2.21.0

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#2 lore raw

PATCH v9 2/5 KVM: arm/arm64: extract duplicated code to own function

In reply to Andrew Murray

Let's reduce code duplication by extracting common code to its own function.

---
 virt/kvm/arm/pmu.c | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)
virt/kvm/arm/pmu.c +16 -12
index c5a722ad283f..6e7c179103a6 100644
@@ -64,6 +64,19 @@ void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu, u64 select_idx, u64 val)
 	__vcpu_sys_reg(vcpu, reg) += (s64)val - kvm_pmu_get_counter_value(vcpu, select_idx);
 }
 
+/**
+ * kvm_pmu_release_perf_event - remove the perf event
+ * @pmc: The PMU counter pointer
+ */
+static void kvm_pmu_release_perf_event(struct kvm_pmc *pmc)
+{
+	if (pmc->perf_event) {
+		perf_event_disable(pmc->perf_event);
+		perf_event_release_kernel(pmc->perf_event);
+		pmc->perf_event = NULL;
+	}
+}
+
 /**
  * kvm_pmu_stop_counter - stop PMU counter
  * @pmc: The PMU counter pointer
@@ -79,9 +92,7 @@ static void kvm_pmu_stop_counter(struct kvm_vcpu *vcpu, struct kvm_pmc *pmc)
 		reg = (pmc->idx == ARMV8_PMU_CYCLE_IDX)
 		       ? PMCCNTR_EL0 : PMEVCNTR0_EL0 + pmc->idx;
 		__vcpu_sys_reg(vcpu, reg) = counter;
-		perf_event_disable(pmc->perf_event);
-		perf_event_release_kernel(pmc->perf_event);
-		pmc->perf_event = NULL;
+		kvm_pmu_release_perf_event(pmc);
 	}
 }
 
@@ -112,15 +123,8 @@ void kvm_pmu_vcpu_destroy(struct kvm_vcpu *vcpu)
 	int i;
 	struct kvm_pmu *pmu = &vcpu->arch.pmu;
 
-	for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++) {
-		struct kvm_pmc *pmc = &pmu->pmc[i];
-
-		if (pmc->perf_event) {
-			perf_event_disable(pmc->perf_event);
-			perf_event_release_kernel(pmc->perf_event);
-			pmc->perf_event = NULL;
-		}
-	}
+	for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++)
+		kvm_pmu_release_perf_event(&pmu->pmc[i]);
 }
 
 u64 kvm_pmu_valid_counter_mask(struct kvm_vcpu *vcpu)
Signature · 6 lines
2.21.0

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#3 lore raw

PATCH v9 3/5 KVM: arm/arm64: re-create event when setting counter value

In reply to Andrew Murray

The perf event sample_period is currently set based upon the current counter value, when PMXEVTYPER is written to and the perf event is created. However the user may choose to write the type before the counter value in which case sample_period will be set incorrectly. Let's instead decouple event creation from PMXEVTYPER and (re)create the event in either suitation.

---
 virt/kvm/arm/pmu.c | 42 +++++++++++++++++++++++++++++++++---------
 1 file changed, 33 insertions(+), 9 deletions(-)
virt/kvm/arm/pmu.c +33 -9
index 6e7c179103a6..ae1e886d4a1a 100644
@@ -24,6 +24,7 @@
 #include <kvm/arm_pmu.h>
 #include <kvm/arm_vgic.h>
 
+static void kvm_pmu_create_perf_event(struct kvm_vcpu *vcpu, u64 select_idx);
 /**
  * kvm_pmu_get_counter_value - get PMU counter value
  * @vcpu: The vcpu pointer
@@ -62,6 +63,9 @@ void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu, u64 select_idx, u64 val)
 	reg = (select_idx == ARMV8_PMU_CYCLE_IDX)
 	      ? PMCCNTR_EL0 : PMEVCNTR0_EL0 + select_idx;
 	__vcpu_sys_reg(vcpu, reg) += (s64)val - kvm_pmu_get_counter_value(vcpu, select_idx);
+
+	/* Recreate the perf event to reflect the updated sample_period */
+	kvm_pmu_create_perf_event(vcpu, select_idx);
 }
 
 /**
@@ -378,23 +382,21 @@ static bool kvm_pmu_counter_is_enabled(struct kvm_vcpu *vcpu, u64 select_idx)
 }
 
 /**
- * kvm_pmu_set_counter_event_type - set selected counter to monitor some event
+ * kvm_pmu_create_perf_event - create a perf event for a counter
  * @vcpu: The vcpu pointer
- * @data: The data guest writes to PMXEVTYPER_EL0
  * @select_idx: The number of selected counter
- *
- * When OS accesses PMXEVTYPER_EL0, that means it wants to set a PMC to count an
- * event with given hardware event number. Here we call perf_event API to
- * emulate this action and create a kernel perf event for it.
  */
-void kvm_pmu_set_counter_event_type(struct kvm_vcpu *vcpu, u64 data,
-				    u64 select_idx)
+static void kvm_pmu_create_perf_event(struct kvm_vcpu *vcpu, u64 select_idx)
 {
 	struct kvm_pmu *pmu = &vcpu->arch.pmu;
 	struct kvm_pmc *pmc = &pmu->pmc[select_idx];
 	struct perf_event *event;
 	struct perf_event_attr attr;
-	u64 eventsel, counter;
+	u64 eventsel, counter, reg, data;
+
+	reg = (select_idx == ARMV8_PMU_CYCLE_IDX)
+	      ? PMCCFILTR_EL0 : PMEVTYPER0_EL0 + select_idx;
+	data = __vcpu_sys_reg(vcpu, reg);
 
 	kvm_pmu_stop_counter(vcpu, pmc);
 	eventsel = data & ARMV8_PMU_EVTYPE_EVENT;
@@ -431,6 +433,28 @@ void kvm_pmu_set_counter_event_type(struct kvm_vcpu *vcpu, u64 data,
 	pmc->perf_event = event;
 }
 
+/**
+ * kvm_pmu_set_counter_event_type - set selected counter to monitor some event
+ * @vcpu: The vcpu pointer
+ * @data: The data guest writes to PMXEVTYPER_EL0
+ * @select_idx: The number of selected counter
+ *
+ * When OS accesses PMXEVTYPER_EL0, that means it wants to set a PMC to count an
+ * event with given hardware event number. Here we call perf_event API to
+ * emulate this action and create a kernel perf event for it.
+ */
+void kvm_pmu_set_counter_event_type(struct kvm_vcpu *vcpu, u64 data,
+				    u64 select_idx)
+{
+	u64 reg, event_type = data & ARMV8_PMU_EVTYPE_MASK;
+
+	reg = (select_idx == ARMV8_PMU_CYCLE_IDX)
+	      ? PMCCFILTR_EL0 : PMEVTYPER0_EL0 + select_idx;
+
+	__vcpu_sys_reg(vcpu, reg) = event_type;
+	kvm_pmu_create_perf_event(vcpu, select_idx);
+}
+
 bool kvm_arm_support_pmu_v3(void)
 {
 	/*
Signature · 6 lines
2.21.0

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#4 lore raw

PATCH v9 4/5 KVM: arm/arm64: remove pmc->bitmask

In reply to Andrew Murray

We currently use pmc->bitmask to determine the width of the pmc - however it's superfluous as the pmc index already describes if the pmc is a cycle counter or event counter. The architecture clearly describes the widths of these counters.

Let's remove the bitmask to simplify the code.

---
 include/kvm/arm_pmu.h |  1 -
 virt/kvm/arm/pmu.c    | 19 +++++++++----------
 2 files changed, 9 insertions(+), 11 deletions(-)
include/kvm/arm_pmu.h +0 -1
index b73f31baca52..2f0e28dc5a9e 100644
@@ -28,7 +28,6 @@
 struct kvm_pmc {
 	u8 idx;	/* index into the pmu->pmc array */
 	struct perf_event *perf_event;
-	u64 bitmask;
 };
 
 struct kvm_pmu {
virt/kvm/arm/pmu.c +9 -10
index ae1e886d4a1a..88ce24ae0b45 100644
@@ -47,7 +47,10 @@ u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx)
 		counter += perf_event_read_value(pmc->perf_event, &enabled,
 						 &running);
 
-	return counter & pmc->bitmask;
+	if (select_idx != ARMV8_PMU_CYCLE_IDX)
+		counter = lower_32_bits(counter);
+
+	return counter;
 }
 
 /**
@@ -113,7 +116,6 @@ void kvm_pmu_vcpu_reset(struct kvm_vcpu *vcpu)
 	for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++) {
 		kvm_pmu_stop_counter(vcpu, &pmu->pmc[i]);
 		pmu->pmc[i].idx = i;
-		pmu->pmc[i].bitmask = 0xffffffffUL;
 	}
 }
 
@@ -348,8 +350,6 @@ void kvm_pmu_software_increment(struct kvm_vcpu *vcpu, u64 val)
  */
 void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val)
 {
-	struct kvm_pmu *pmu = &vcpu->arch.pmu;
-	struct kvm_pmc *pmc;
 	u64 mask;
 	int i;
 
@@ -368,11 +368,6 @@ void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val)
 		for (i = 0; i < ARMV8_PMU_CYCLE_IDX; i++)
 			kvm_pmu_set_counter_value(vcpu, i, 0);
 	}
-
-	if (val & ARMV8_PMU_PMCR_LC) {
-		pmc = &pmu->pmc[ARMV8_PMU_CYCLE_IDX];
-		pmc->bitmask = 0xffffffffffffffffUL;
-	}
 }
 
 static bool kvm_pmu_counter_is_enabled(struct kvm_vcpu *vcpu, u64 select_idx)
@@ -420,7 +415,11 @@ static void kvm_pmu_create_perf_event(struct kvm_vcpu *vcpu, u64 select_idx)
 
 	counter = kvm_pmu_get_counter_value(vcpu, select_idx);
 	/* The initial sample period (overflow count) of an event. */
-	attr.sample_period = (-counter) & pmc->bitmask;
+	if (pmc->idx == ARMV8_PMU_CYCLE_IDX &&
+	    __vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_LC)
+		attr.sample_period = (-counter) & GENMASK(63, 0);
+	else
+		attr.sample_period = (-counter) & GENMASK(31, 0);
 
 	event = perf_event_create_kernel_counter(&attr, -1, current,
 						 kvm_pmu_perf_overflow, pmc);
Signature · 6 lines
2.21.0

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#11 lore raw

PATCH v9 5/5 KVM: arm/arm64: support chained PMU counters

In reply to Andrew Murray

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.

---
 include/kvm/arm_pmu.h |   2 +
 virt/kvm/arm/pmu.c    | 255 +++++++++++++++++++++++++++++++++++-------
 2 files changed, 219 insertions(+), 38 deletions(-)
include/kvm/arm_pmu.h +2 -0
index 2f0e28dc5a9e..589f49ed8cf8 100644
@@ -22,6 +22,7 @@
 #include <asm/perf_event.h>
 
 #define ARMV8_PMU_CYCLE_IDX		(ARMV8_PMU_MAX_COUNTERS - 1)
+#define ARMV8_PMU_MAX_COUNTER_PAIRS	((ARMV8_PMU_MAX_COUNTERS + 1) >> 1)
 
 #ifdef CONFIG_KVM_ARM_PMU
 
@@ -33,6 +34,7 @@ struct kvm_pmc {
 struct kvm_pmu {
 	int irq_num;
 	struct kvm_pmc pmc[ARMV8_PMU_MAX_COUNTERS];
+	DECLARE_BITMAP(chained, ARMV8_PMU_MAX_COUNTER_PAIRS);
 	bool ready;
 	bool created;
 	bool irq_level;
virt/kvm/arm/pmu.c +217 -38

356 lines not on this page · raw message

Signature · 6 lines
2.21.0

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#5 lore raw

Re: [PATCH v9 4/5] KVM: arm/arm64: remove pmc->bitmask

In reply to Andrew Murray

Hi Andrew,

Quoted 30 lines · Andrew Murray

We currently use pmc->bitmask to determine the width of the pmc - however it's superfluous as the pmc index already describes if the pmc is a cycle counter or event counter. The architecture clearly describes the widths of these counters.

Let's remove the bitmask to simplify the code.

---
 include/kvm/arm_pmu.h |  1 -
 virt/kvm/arm/pmu.c    | 19 +++++++++----------
 2 files changed, 9 insertions(+), 11 deletions(-)
include/kvm/arm_pmu.h +0 -1
index b73f31baca52..2f0e28dc5a9e 100644
@@ -28,7 +28,6 @@
 struct kvm_pmc {
 	u8 idx;	/* index into the pmu->pmc array */
 	struct perf_event *perf_event;
-	u64 bitmask;
 };
 
 struct kvm_pmu {
virt/kvm/arm/pmu.c +0 -0
index ae1e886d4a1a..88ce24ae0b45 100644

virt/kvm/arm/pmu.c u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx) · line 51

Quoted 6 lines
virt/kvm/arm/pmu.c +2 -1
@@ -47,7 +47,10 @@ u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx)
 		counter += perf_event_read_value(pmc->perf_event, &enabled,
 						 &running);
 
-	return counter & pmc->bitmask;
+	if (select_idx != ARMV8_PMU_CYCLE_IDX)
+		counter = lower_32_bits(counter);

Shouldn't this depend on PMCR.LC as well? If PMCR.LC is clear we only want the lower 32bits of the cycle counter.

Cheers,

Signature · 5 lines
Julien Thierry
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#6 lore raw

Re: [PATCH v9 4/5] KVM: arm/arm64: remove pmc->bitmask

Quoted 36 lines · Julien Thierry

Hi Andrew,

Quoted 33 lines · Andrew Murray

We currently use pmc->bitmask to determine the width of the pmc - however it's superfluous as the pmc index already describes if the pmc is a cycle counter or event counter. The architecture clearly describes the widths of these counters.

Let's remove the bitmask to simplify the code.

---
 include/kvm/arm_pmu.h |  1 -
 virt/kvm/arm/pmu.c    | 19 +++++++++----------
 2 files changed, 9 insertions(+), 11 deletions(-)
include/kvm/arm_pmu.h +0 -1
index b73f31baca52..2f0e28dc5a9e 100644
@@ -28,7 +28,6 @@
 struct kvm_pmc {
 	u8 idx;	/* index into the pmu->pmc array */
 	struct perf_event *perf_event;
-	u64 bitmask;
 };
 
 struct kvm_pmu {
virt/kvm/arm/pmu.c +0 -0
index ae1e886d4a1a..88ce24ae0b45 100644
@@ -47,7 +47,10 @@ u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx)
 		counter += perf_event_read_value(pmc->perf_event, &enabled,
 						 &running);
 
Quoted 6 lines

virt/kvm/arm/pmu.c u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx) · line 51

Quoted 3 lines
virt/kvm/arm/pmu.c +2 -1
-	return counter & pmc->bitmask;
+	if (select_idx != ARMV8_PMU_CYCLE_IDX)
+		counter = lower_32_bits(counter);

Shouldn't this depend on PMCR.LC as well? If PMCR.LC is clear we only want the lower 32bits of the cycle counter.

Yes that's correct. The hunk should look like this:

- return counter & pmc->bitmask;
+ if (!(select_idx == ARMV8_PMU_CYCLE_IDX &&
+ __vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_LC))
+ counter = lower_32_bits(counter);
+
+ return counter;

Thanks for the review.

Andrew Murray

Quoted 5 lines

Cheers,

Signature · 1 line
Julien Thierry

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#7 lore raw

Re: [PATCH v9 4/5] KVM: arm/arm64: remove pmc->bitmask

Quoted 46 lines · Andrew Murray
Quoted 36 lines · Julien Thierry

Hi Andrew,

Quoted 33 lines · Andrew Murray

We currently use pmc->bitmask to determine the width of the pmc - however it's superfluous as the pmc index already describes if the pmc is a cycle counter or event counter. The architecture clearly describes the widths of these counters.

Let's remove the bitmask to simplify the code.

---
  include/kvm/arm_pmu.h |  1 -
  virt/kvm/arm/pmu.c    | 19 +++++++++----------
  2 files changed, 9 insertions(+), 11 deletions(-)
include/kvm/arm_pmu.h +0 -1
index b73f31baca52..2f0e28dc5a9e 100644
@@ -28,7 +28,6 @@
  struct kvm_pmc {
  	u8 idx;	/* index into the pmu->pmc array */
  	struct perf_event *perf_event;
-	u64 bitmask;
  };
 
  struct kvm_pmu {
virt/kvm/arm/pmu.c +0 -0
index ae1e886d4a1a..88ce24ae0b45 100644
@@ -47,7 +47,10 @@ u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx)
  		counter += perf_event_read_value(pmc->perf_event, &enabled,
  						 &running);
 
Quoted 6 lines

virt/kvm/arm/pmu.c u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx) · line 51

Quoted 3 lines
virt/kvm/arm/pmu.c +2 -1
-	return counter & pmc->bitmask;
+	if (select_idx != ARMV8_PMU_CYCLE_IDX)
+		counter = lower_32_bits(counter);

Shouldn't this depend on PMCR.LC as well? If PMCR.LC is clear we only want the lower 32bits of the cycle counter.

Yes that's correct. The hunk should look like this:

virt/kvm/arm/pmu.c u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx) · line 53

Quoted 6 lines
virt/kvm/arm/pmu.c +5 -1
-       return counter & pmc->bitmask;
+       if (!(select_idx == ARMV8_PMU_CYCLE_IDX &&
+             __vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_LC))
+               counter = lower_32_bits(counter);
+
+       return counter;

May be you could add a macro :

#define vcpu_pmu_counter_is_64bit(vcpu, idx) ?

Cheers
Suzuki
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

reviewer Reviewed-by on v9 5/5
#12 lore raw

Re: [PATCH v9 5/5] KVM: arm/arm64: support chained PMU counters

In reply to Andrew Murray
Quoted 6 lines · Andrew Murray

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.

Quoted 7 lines

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.

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#8 lore raw

Re: [PATCH v9 4/5] KVM: arm/arm64: remove pmc->bitmask

In reply to Suzuki K Poulose
Quoted 54 lines · Suzuki K Poulose
Quoted 44 lines · Andrew Murray
Quoted 32 lines · Julien Thierry

Hi Andrew,

Quoted 29 lines · Andrew Murray

We currently use pmc->bitmask to determine the width of the pmc - however it's superfluous as the pmc index already describes if the pmc is a cycle counter or event counter. The architecture clearly describes the widths of these counters.

Let's remove the bitmask to simplify the code.

---
  include/kvm/arm_pmu.h |  1 -
  virt/kvm/arm/pmu.c    | 19 +++++++++----------
  2 files changed, 9 insertions(+), 11 deletions(-)
include/kvm/arm_pmu.h +0 -1
index b73f31baca52..2f0e28dc5a9e 100644
@@ -28,7 +28,6 @@
  struct kvm_pmc {
  	u8 idx;	/* index into the pmu->pmc array */
  	struct perf_event *perf_event;
-	u64 bitmask;
  };
  struct kvm_pmu {
virt/kvm/arm/pmu.c +0 -0
index ae1e886d4a1a..88ce24ae0b45 100644
@@ -47,7 +47,10 @@ u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx)
Quoted 8 lines

virt/kvm/arm/pmu.c u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx) · line 51

Quoted 5 lines
virt/kvm/arm/pmu.c +2 -1
  		counter += perf_event_read_value(pmc->perf_event, &enabled,
  						 &running);
-	return counter & pmc->bitmask;
+	if (select_idx != ARMV8_PMU_CYCLE_IDX)
+		counter = lower_32_bits(counter);

Shouldn't this depend on PMCR.LC as well? If PMCR.LC is clear we only want the lower 32bits of the cycle counter.

Yes that's correct. The hunk should look like this:

virt/kvm/arm/pmu.c u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx) · line 53

Quoted 6 lines
virt/kvm/arm/pmu.c +5 -1
-       return counter & pmc->bitmask;
+       if (!(select_idx == ARMV8_PMU_CYCLE_IDX &&
+             __vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_LC))
+               counter = lower_32_bits(counter);
+
+       return counter;
Quoted 3 lines

May be you could add a macro :

#define vcpu_pmu_counter_is_64bit(vcpu, idx) ?

Yes I think a helper would be useful - though I'd prefer the name 'kvm_pmu_idx_is_long_cycle_counter'. This seems a little clearer as you could otherwise argue that a chained counter is also 64 bits.

Thanks,

Andrew Murray

Quoted 3 lines

Cheers
Suzuki

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#9 lore raw

Re: [PATCH v9 4/5] KVM: arm/arm64: remove pmc->bitmask

Quoted 28 lines · Andrew Murray
Quoted 27 lines · Suzuki K Poulose
Quoted 23 lines · Andrew Murray
Quoted 4 lines · Julien Thierry
Quoted 4 lines

index ae1e886d4a1a..88ce24ae0b45 100644

virt/kvm/arm/pmu.c +0 -0
@@ -47,7 +47,10 @@ u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx)
Quoted 8 lines

virt/kvm/arm/pmu.c u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx) · line 51

Quoted 5 lines
virt/kvm/arm/pmu.c +2 -1
   		counter += perf_event_read_value(pmc->perf_event, &enabled,
   						 &running);
-	return counter & pmc->bitmask;
+	if (select_idx != ARMV8_PMU_CYCLE_IDX)
+		counter = lower_32_bits(counter);

Shouldn't this depend on PMCR.LC as well? If PMCR.LC is clear we only want the lower 32bits of the cycle counter.

Yes that's correct. The hunk should look like this:

virt/kvm/arm/pmu.c +5 -1
-       return counter & pmc->bitmask;
+       if (!(select_idx == ARMV8_PMU_CYCLE_IDX &&
+             __vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_LC))
+               counter = lower_32_bits(counter);
+
+       return counter;
Quoted 7 lines
Quoted 3 lines

May be you could add a macro :

#define vcpu_pmu_counter_is_64bit(vcpu, idx) ?

Yes I think a helper would be useful - though I'd prefer the name 'kvm_pmu_idx_is_long_cycle_counter'. This seems a little clearer as you could otherwise argue that a chained counter is also 64 bits.

When you get to add 64bit PMU counter (v8.5), this would be handy. So having it de-coupled from the cycle counter may be a good idea. Anyways, I leave that to you.

Cheers
Suzuki
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#10 lore raw

Re: [PATCH v9 4/5] KVM: arm/arm64: remove pmc->bitmask

Quoted 35 lines · Suzuki K Poulose
Quoted 32 lines · Andrew Murray
Quoted 30 lines · Suzuki K Poulose
Quoted 17 lines · Andrew Murray
Quoted 4 lines · Julien Thierry
Quoted 4 lines

index ae1e886d4a1a..88ce24ae0b45 100644

virt/kvm/arm/pmu.c +0 -0
@@ -47,7 +47,10 @@ u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx)
Quoted 8 lines

virt/kvm/arm/pmu.c u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx) · line 51

Quoted 5 lines
virt/kvm/arm/pmu.c +2 -1
   		counter += perf_event_read_value(pmc->perf_event, &enabled,
   						 &running);
-	return counter & pmc->bitmask;
+	if (select_idx != ARMV8_PMU_CYCLE_IDX)
+		counter = lower_32_bits(counter);

Shouldn't this depend on PMCR.LC as well? If PMCR.LC is clear we only want the lower 32bits of the cycle counter.

Yes that's correct. The hunk should look like this:

virt/kvm/arm/pmu.c u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx) · line 53

Quoted 6 lines
virt/kvm/arm/pmu.c +5 -1
-       return counter & pmc->bitmask;
+       if (!(select_idx == ARMV8_PMU_CYCLE_IDX &&
+             __vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_LC))
+               counter = lower_32_bits(counter);
+
+       return counter;

May be you could add a macro :

#define vcpu_pmu_counter_is_64bit(vcpu, idx) ?

Quoted 7 lines
Quoted 3 lines

Yes I think a helper would be useful - though I'd prefer the name 'kvm_pmu_idx_is_long_cycle_counter'. This seems a little clearer as you could otherwise argue that a chained counter is also 64 bits.

When you get to add 64bit PMU counter (v8.5), this would be handy. So having it de-coupled from the cycle counter may be a good idea. Anyways, I leave that to you.

Yes that thought crossed my mind. I'll take your suggestion afterall.

Thanks,

Andrew Murray

Quoted 3 lines

Cheers
Suzuki

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm