Skip to contentSkip to conversation

KVM: arm64: pmu: Fix cycle counter truncation on counter stop

PATCH In review Marc Zyngier 2 participants 6 messages Oct 3, 2019 – Oct 4, 2019

Subsystems (3)
lore
Share & tools
lore

Marc Zyngier

author maintainer
#0 lore raw

PATCH 1/1 KVM: arm64: pmu: Fix cycle counter truncation on counter stop

When a counter is disabled, its value is sampled before the event is being disabled, and the value written back in the shadow register.

In that process, the value gets truncated to 32bit, which is adequate for any counter but the cycle counter, which can be configured to hold a 64bit value. This obviously results in a corrupted counter, and things like "perf record -e cycles" not working at all when run in a guest...

Make the truncation conditional on the counter not being 64bit.

Fixes 80f393a23be6 ("KVM: arm/arm64: Support chained PMU counters")
Cc Andrew Murray <[email protected]>
---
 virt/kvm/arm/pmu.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
virt/kvm/arm/pmu.c +3 -1
index 362a01886bab..d716aef2bae9 100644
@@ -206,9 +206,11 @@ static void kvm_pmu_stop_counter(struct kvm_vcpu *vcpu, struct kvm_pmc *pmc)
 		__vcpu_sys_reg(vcpu, reg) = lower_32_bits(counter);
 		__vcpu_sys_reg(vcpu, reg + 1) = upper_32_bits(counter);
 	} else {
+		if (!kvm_pmu_idx_is_64bit(vcpu, pmc->idx))
+			counter = lower_32_bits(counter);
 		reg = (pmc->idx == ARMV8_PMU_CYCLE_IDX)
 		       ? PMCCNTR_EL0 : PMEVCNTR0_EL0 + pmc->idx;
-		__vcpu_sys_reg(vcpu, reg) = lower_32_bits(counter);
+		__vcpu_sys_reg(vcpu, reg) = counter;
 	}
 
 	kvm_pmu_release_perf_event(pmc);
Signature · 6 lines
2.20.1

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Marc Zyngier

When a counter is disabled, its value is sampled before the event is being disabled, and the value written back in the shadow register.

In that process, the value gets truncated to 32bit, which is adequate

Doh, that shouldn't have happened.

Quoted 20 lines

for any counter but the cycle counter, which can be configured to hold a 64bit value. This obviously results in a corrupted counter, and things like "perf record -e cycles" not working at all when run in a guest...

Make the truncation conditional on the counter not being 64bit.

Fixes 80f393a23be6 ("KVM: arm/arm64: Support chained PMU counters")
Cc Andrew Murray <[email protected]>
---
 virt/kvm/arm/pmu.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
virt/kvm/arm/pmu.c +0 -0
index 362a01886bab..d716aef2bae9 100644

virt/kvm/arm/pmu.c static void kvm_pmu_stop_counter(struct kvm_vcpu *vcpu, struct kvm_pmc *pmc) · line 213

Quoted 9 lines
virt/kvm/arm/pmu.c +3 -1
@@ -206,9 +206,11 @@ static void kvm_pmu_stop_counter(struct kvm_vcpu *vcpu, struct kvm_pmc *pmc)
 		__vcpu_sys_reg(vcpu, reg) = lower_32_bits(counter);
 		__vcpu_sys_reg(vcpu, reg + 1) = upper_32_bits(counter);
 	} else {
+		if (!kvm_pmu_idx_is_64bit(vcpu, pmc->idx))
+			counter = lower_32_bits(counter);
 		reg = (pmc->idx == ARMV8_PMU_CYCLE_IDX)
 		       ? PMCCNTR_EL0 : PMEVCNTR0_EL0 + pmc->idx;
-		__vcpu_sys_reg(vcpu, reg) = lower_32_bits(counter);
+		__vcpu_sys_reg(vcpu, reg) = counter;

The other uses of lower_32_bits look OK to me.

As a side note, I'm not convinced that the implementation (or perhaps the use of) kvm_pmu_idx_is_64bit is correct:

static bool kvm_pmu_idx_is_64bit(struct kvm_vcpu *vcpu, u64 select_idx)

{
        return (select_idx == ARMV8_PMU_CYCLE_IDX &&
                __vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_LC);
}

We shouldn't truncate the value of a cycle counter to 32 bits just because _PMCR_LC is unset. We should only be interested in _PMCR_LC when setting the sample_period.

If you agree this is wrong, I'll spin a change.

Though unsetting _PMCR_LC is deprecated so I can't imagine this causes any issue.

Thanks,

Andrew Murray

Quoted 6 lines
virt/kvm/arm/pmu.c +0 -0
 	}
 
 	kvm_pmu_release_perf_event(pmc);
Signature · 1 line
2.20.1
author maintainer
#2 lore raw
Quoted 46 lines · On Fri, 4 Oct 2019 09:55:55 +0100 Andrew Murray <[email protected]> wrote:
Quoted 4 lines · Marc Zyngier

When a counter is disabled, its value is sampled before the event is being disabled, and the value written back in the shadow register.

In that process, the value gets truncated to 32bit, which is adequate

Doh, that shouldn't have happened.

Quoted 20 lines

for any counter but the cycle counter, which can be configured to hold a 64bit value. This obviously results in a corrupted counter, and things like "perf record -e cycles" not working at all when run in a guest...

Make the truncation conditional on the counter not being 64bit.

Fixes 80f393a23be6 ("KVM: arm/arm64: Support chained PMU counters")
Cc Andrew Murray <[email protected]>
---
 virt/kvm/arm/pmu.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
virt/kvm/arm/pmu.c +0 -0
index 362a01886bab..d716aef2bae9 100644

virt/kvm/arm/pmu.c static void kvm_pmu_stop_counter(struct kvm_vcpu *vcpu, struct kvm_pmc *pmc) · line 213

Quoted 9 lines
virt/kvm/arm/pmu.c +3 -1
@@ -206,9 +206,11 @@ static void kvm_pmu_stop_counter(struct kvm_vcpu *vcpu, struct kvm_pmc *pmc)
 		__vcpu_sys_reg(vcpu, reg) = lower_32_bits(counter);
 		__vcpu_sys_reg(vcpu, reg + 1) = upper_32_bits(counter);
 	} else {
+		if (!kvm_pmu_idx_is_64bit(vcpu, pmc->idx))
+			counter = lower_32_bits(counter);
 		reg = (pmc->idx == ARMV8_PMU_CYCLE_IDX)
 		       ? PMCCNTR_EL0 : PMEVCNTR0_EL0 + pmc->idx;
-		__vcpu_sys_reg(vcpu, reg) = lower_32_bits(counter);
+		__vcpu_sys_reg(vcpu, reg) = counter;  

The other uses of lower_32_bits look OK to me.

As a side note, I'm not convinced that the implementation (or perhaps the use of) kvm_pmu_idx_is_64bit is correct:

static bool kvm_pmu_idx_is_64bit(struct kvm_vcpu *vcpu, u64 select_idx)

Quoted 8 lines
{
        return (select_idx == ARMV8_PMU_CYCLE_IDX &&
                __vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_LC);
}

We shouldn't truncate the value of a cycle counter to 32 bits just because _PMCR_LC is unset. We should only be interested in _PMCR_LC when setting the sample_period.

That's a good point. The ARMv8 ARM says:

"Long cycle counter enable. Determines when unsigned overflow is recorded by the cycle counter overflow bit."

which doesn't say anything about the counter being truncated one way or another.

Quoted 1 line

If you agree this is wrong, I'll spin a change.

I still think kvm_pmu_idx_is_64bit() correct, and would be easily extended to supporting the ARMv8.5-PMU extension. However, it'd be better to just detect the cycle counter in the current patch rather than relying on the above helper:

virt/kvm/arm/pmu.c +1 -1
index d716aef2bae9..90a90d8f7280 100644
@@ -206,7 +206,7 @@ static void kvm_pmu_stop_counter(struct kvm_vcpu *vcpu, struct kvm_pmc *pmc)
 		__vcpu_sys_reg(vcpu, reg) = lower_32_bits(counter);
 		__vcpu_sys_reg(vcpu, reg + 1) = upper_32_bits(counter);
 	} else {
-		if (!kvm_pmu_idx_is_64bit(vcpu, pmc->idx))
+		if (pmc->idx != ARMV8_PMU_CYCLE_IDX)
 			counter = lower_32_bits(counter);
 		reg = (pmc->idx == ARMV8_PMU_CYCLE_IDX)
 		       ? PMCCNTR_EL0 : PMEVCNTR0_EL0 + pmc->idx;

As for revamping the rest of the code, that's 5.5 material.

Quoted 2 lines

Though unsetting _PMCR_LC is deprecated so I can't imagine this causes any issue.

Deprecated, yes. Disallowed, no. We'll have to support this as long as we have 32bit capable stuff in the wild. But we could at least start with correctly emulating the setting of the LC bit, see below.

Thanks,

	M.

From c421c17ae1e9c90db4b73bd25485580833321f4b Mon Sep 17 00:00:00 2001
From: Marc Zyngier <[email protected]>
Date: Fri, 4 Oct 2019 11:03:09 +0100
Subject: [PATCH] arm64: KVM: Handle PMCR_EL0.LC as RES1 on pure AArch64
systems

Of PMCR_EL0.LC, the ARMv8 ARM says:

"In an AArch64 only implementation, this field is RES 1."

So be it.

---
 arch/arm64/kvm/sys_regs.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/arm64/kvm/sys_regs.c +4 -0
index 2071260a275b..46822afc57e0 100644
@@ -632,6 +632,8 @@ static void reset_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
 	 */
 	val = ((pmcr & ~ARMV8_PMU_PMCR_MASK)
 	       | (ARMV8_PMU_PMCR_MASK & 0xdecafbad)) & (~ARMV8_PMU_PMCR_E);
+	if (!system_supports_32bit_el0())
+		val |= ARMV8_PMU_PMCR_LC;
 	__vcpu_sys_reg(vcpu, r->reg) = val;
 }
 
@@ -682,6 +684,8 @@ static bool access_pmcr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
 		val = __vcpu_sys_reg(vcpu, PMCR_EL0);
 		val &= ~ARMV8_PMU_PMCR_MASK;
 		val |= p->regval & ARMV8_PMU_PMCR_MASK;
+		if (!system_supports_32bit_el0())
+			val |= ARMV8_PMU_PMCR_LC;
 		__vcpu_sys_reg(vcpu, PMCR_EL0) = val;
 		kvm_pmu_handle_pmcr(vcpu, val);
 		kvm_vcpu_pmu_restore_guest(vcpu);
Signature · 5 lines
2.20.1


-- 
Jazz is not dead. It just smells funny...
Reviewed-by
#3 lore raw
Quoted 66 lines · Marc Zyngier
Quoted 46 lines · On Fri, 4 Oct 2019 09:55:55 +0100 Andrew Murray <[email protected]> wrote:
Quoted 4 lines · Marc Zyngier

When a counter is disabled, its value is sampled before the event is being disabled, and the value written back in the shadow register.

In that process, the value gets truncated to 32bit, which is adequate

Doh, that shouldn't have happened.

Quoted 20 lines

for any counter but the cycle counter, which can be configured to hold a 64bit value. This obviously results in a corrupted counter, and things like "perf record -e cycles" not working at all when run in a guest...

Make the truncation conditional on the counter not being 64bit.

Fixes 80f393a23be6 ("KVM: arm/arm64: Support chained PMU counters")
Cc Andrew Murray <[email protected]>
---
 virt/kvm/arm/pmu.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
virt/kvm/arm/pmu.c +0 -0
index 362a01886bab..d716aef2bae9 100644

virt/kvm/arm/pmu.c static void kvm_pmu_stop_counter(struct kvm_vcpu *vcpu, struct kvm_pmc *pmc) · line 213

Quoted 9 lines
virt/kvm/arm/pmu.c +3 -1
@@ -206,9 +206,11 @@ static void kvm_pmu_stop_counter(struct kvm_vcpu *vcpu, struct kvm_pmc *pmc)
 		__vcpu_sys_reg(vcpu, reg) = lower_32_bits(counter);
 		__vcpu_sys_reg(vcpu, reg + 1) = upper_32_bits(counter);
 	} else {
+		if (!kvm_pmu_idx_is_64bit(vcpu, pmc->idx))
+			counter = lower_32_bits(counter);
 		reg = (pmc->idx == ARMV8_PMU_CYCLE_IDX)
 		       ? PMCCNTR_EL0 : PMEVCNTR0_EL0 + pmc->idx;
-		__vcpu_sys_reg(vcpu, reg) = lower_32_bits(counter);
+		__vcpu_sys_reg(vcpu, reg) = counter;  

The other uses of lower_32_bits look OK to me.

As a side note, I'm not convinced that the implementation (or perhaps the use of) kvm_pmu_idx_is_64bit is correct:

static bool kvm_pmu_idx_is_64bit(struct kvm_vcpu *vcpu, u64 select_idx)

Quoted 8 lines
{
        return (select_idx == ARMV8_PMU_CYCLE_IDX &&
                __vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_LC);
}

We shouldn't truncate the value of a cycle counter to 32 bits just because _PMCR_LC is unset. We should only be interested in _PMCR_LC when setting the sample_period.

That's a good point. The ARMv8 ARM says:

"Long cycle counter enable. Determines when unsigned overflow is recorded by the cycle counter overflow bit."

which doesn't say anything about the counter being truncated one way or another.

Quoted 6 lines
Quoted 1 line

If you agree this is wrong, I'll spin a change.

I still think kvm_pmu_idx_is_64bit() correct, and would be easily extended to supporting the ARMv8.5-PMU extension. However, it'd be better to just detect the cycle counter in the current patch rather than relying on the above helper:

I guess at present kvm_pmu_idx_is_64bit has the meaning "does the counter have a 64 bit overflow". (And we check for the CYCLE_IDX because at present thats the only thing that *can* have a 64bit overflow.)

Quoted 7 lines
virt/kvm/arm/pmu.c +0 -0
index d716aef2bae9..90a90d8f7280 100644

virt/kvm/arm/pmu.c static void kvm_pmu_stop_counter(struct kvm_vcpu *vcpu, struct kvm_pmc *pmc) · line 212

Quoted 8 lines
virt/kvm/arm/pmu.c +1 -1
@@ -206,7 +206,7 @@ static void kvm_pmu_stop_counter(struct kvm_vcpu *vcpu, struct kvm_pmc *pmc)
 		__vcpu_sys_reg(vcpu, reg) = lower_32_bits(counter);
 		__vcpu_sys_reg(vcpu, reg + 1) = upper_32_bits(counter);
 	} else {
-		if (!kvm_pmu_idx_is_64bit(vcpu, pmc->idx))
+		if (pmc->idx != ARMV8_PMU_CYCLE_IDX)
 			counter = lower_32_bits(counter);
 		reg = (pmc->idx == ARMV8_PMU_CYCLE_IDX)
 		       ? PMCCNTR_EL0 : PMEVCNTR0_EL0 + pmc->idx;

That looks fine to me.

Quoted 2 lines

As for revamping the rest of the code, that's 5.5 material.

The only other change required would be as follows:

virt/kvm/arm/pmu.c +1 -1
index 362a01886bab..2435119b8524 100644
@@ -147,7 +147,7 @@ u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx)
            kvm_pmu_idx_is_high_counter(select_idx))
                counter = upper_32_bits(counter);
 
-       else if (!kvm_pmu_idx_is_64bit(vcpu, select_idx))
+       else if (select_idx != ARMV8_PMU_CYCLE_IDX)
                counter = lower_32_bits(counter);
 
        return counter;
Quoted 43 lines
Quoted 3 lines

Though unsetting _PMCR_LC is deprecated so I can't imagine this causes any issue.

Deprecated, yes. Disallowed, no. We'll have to support this as long as we have 32bit capable stuff in the wild. But we could at least start with correctly emulating the setting of the LC bit, see below.

Thanks,

	M.

From c421c17ae1e9c90db4b73bd25485580833321f4b Mon Sep 17 00:00:00 2001
From: Marc Zyngier <[email protected]>
Date: Fri, 4 Oct 2019 11:03:09 +0100
Subject: [PATCH] arm64: KVM: Handle PMCR_EL0.LC as RES1 on pure AArch64
systems

Of PMCR_EL0.LC, the ARMv8 ARM says:

"In an AArch64 only implementation, this field is RES 1."

So be it.

---
 arch/arm64/kvm/sys_regs.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/arm64/kvm/sys_regs.c +2 -0
index 2071260a275b..46822afc57e0 100644
@@ -632,6 +632,8 @@ static void reset_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
 	 */
 	val = ((pmcr & ~ARMV8_PMU_PMCR_MASK)
 	       | (ARMV8_PMU_PMCR_MASK & 0xdecafbad)) & (~ARMV8_PMU_PMCR_E);
+	if (!system_supports_32bit_el0())
+		val |= ARMV8_PMU_PMCR_LC;
 	__vcpu_sys_reg(vcpu, r->reg) = val;
 }
 

arch/arm64/kvm/sys_regs.c static bool access_pmcr(struct kvm_vcpu *vcpu, struct sys_reg_params *p, · line 691

Quoted 8 lines
arch/arm64/kvm/sys_regs.c +2 -0
@@ -682,6 +684,8 @@ static bool access_pmcr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
 		val = __vcpu_sys_reg(vcpu, PMCR_EL0);
 		val &= ~ARMV8_PMU_PMCR_MASK;
 		val |= p->regval & ARMV8_PMU_PMCR_MASK;
+		if (!system_supports_32bit_el0())
+			val |= ARMV8_PMU_PMCR_LC;
 		__vcpu_sys_reg(vcpu, PMCR_EL0) = val;
 		kvm_pmu_handle_pmcr(vcpu, val);
 		kvm_vcpu_pmu_restore_guest(vcpu);

This looks good to me.

Quoted 6 lines
Signature · 5 lines
2.20.1


-- 
Jazz is not dead. It just smells funny...
author maintainer
#4 lore raw
Quoted 70 lines · Andrew Murray
Quoted 68 lines · Marc Zyngier
Quoted 46 lines · On Fri, 4 Oct 2019 09:55:55 +0100 Andrew Murray <[email protected]> wrote:
Quoted 4 lines · Marc Zyngier

When a counter is disabled, its value is sampled before the event is being disabled, and the value written back in the shadow register.

In that process, the value gets truncated to 32bit, which is adequate

Doh, that shouldn't have happened.

Quoted 20 lines

for any counter but the cycle counter, which can be configured to hold a 64bit value. This obviously results in a corrupted counter, and things like "perf record -e cycles" not working at all when run in a guest...

Make the truncation conditional on the counter not being 64bit.

Fixes 80f393a23be6 ("KVM: arm/arm64: Support chained PMU counters")
Cc Andrew Murray <[email protected]>
---
 virt/kvm/arm/pmu.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
virt/kvm/arm/pmu.c +0 -0
index 362a01886bab..d716aef2bae9 100644

virt/kvm/arm/pmu.c static void kvm_pmu_stop_counter(struct kvm_vcpu *vcpu, struct kvm_pmc *pmc) · line 213

Quoted 9 lines
virt/kvm/arm/pmu.c +3 -1
@@ -206,9 +206,11 @@ static void kvm_pmu_stop_counter(struct kvm_vcpu *vcpu, struct kvm_pmc *pmc)
 		__vcpu_sys_reg(vcpu, reg) = lower_32_bits(counter);
 		__vcpu_sys_reg(vcpu, reg + 1) = upper_32_bits(counter);
 	} else {
+		if (!kvm_pmu_idx_is_64bit(vcpu, pmc->idx))
+			counter = lower_32_bits(counter);
 		reg = (pmc->idx == ARMV8_PMU_CYCLE_IDX)
 		       ? PMCCNTR_EL0 : PMEVCNTR0_EL0 + pmc->idx;
-		__vcpu_sys_reg(vcpu, reg) = lower_32_bits(counter);
+		__vcpu_sys_reg(vcpu, reg) = counter;  

The other uses of lower_32_bits look OK to me.

As a side note, I'm not convinced that the implementation (or perhaps the use of) kvm_pmu_idx_is_64bit is correct:

static bool kvm_pmu_idx_is_64bit(struct kvm_vcpu *vcpu, u64 select_idx)

Quoted 8 lines
{
        return (select_idx == ARMV8_PMU_CYCLE_IDX &&
                __vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_LC);
}

We shouldn't truncate the value of a cycle counter to 32 bits just because _PMCR_LC is unset. We should only be interested in _PMCR_LC when setting the sample_period.

That's a good point. The ARMv8 ARM says:

"Long cycle counter enable. Determines when unsigned overflow is recorded by the cycle counter overflow bit."

which doesn't say anything about the counter being truncated one way or another.

Quoted 1 line

If you agree this is wrong, I'll spin a change.

Quoted 8 lines
Quoted 4 lines

I still think kvm_pmu_idx_is_64bit() correct, and would be easily extended to supporting the ARMv8.5-PMU extension. However, it'd be better to just detect the cycle counter in the current patch rather than relying on the above helper:

I guess at present kvm_pmu_idx_is_64bit has the meaning "does the counter have a 64 bit overflow". (And we check for the CYCLE_IDX because at present thats the only thing that *can* have a 64bit overflow.)

Exactly. The function is badly named, but hey, we'll live with it until we refactor this further.

Quoted 8 lines
Quoted 8 lines
virt/kvm/arm/pmu.c +0 -0
index d716aef2bae9..90a90d8f7280 100644
@@ -206,7 +206,7 @@ static void kvm_pmu_stop_counter(struct kvm_vcpu *vcpu, struct kvm_pmc *pmc)
 		__vcpu_sys_reg(vcpu, reg) = lower_32_bits(counter);
 		__vcpu_sys_reg(vcpu, reg + 1) = upper_32_bits(counter);
 	} else {
Quoted 8 lines

virt/kvm/arm/pmu.c static void kvm_pmu_stop_counter(struct kvm_vcpu *vcpu, struct kvm_pmc *pmc) · line 212

Quoted 6 lines
virt/kvm/arm/pmu.c +1 -1
-		if (!kvm_pmu_idx_is_64bit(vcpu, pmc->idx))
+		if (pmc->idx != ARMV8_PMU_CYCLE_IDX)
 			counter = lower_32_bits(counter);
 		reg = (pmc->idx == ARMV8_PMU_CYCLE_IDX)
 		       ? PMCCNTR_EL0 : PMEVCNTR0_EL0 + pmc->idx;

That looks fine to me.

I've now revamped that code further, as having an if() and a conditional expression that check the same this is a bit... meh. The result is more invasive, but far more readable [1].

Quoted 9 lines
Quoted 1 line

As for revamping the rest of the code, that's 5.5 material.

The only other change required would be as follows:

virt/kvm/arm/pmu.c +0 -0
index 362a01886bab..2435119b8524 100644

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

Quoted 8 lines
virt/kvm/arm/pmu.c +1 -1
@@ -147,7 +147,7 @@ u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx)
            kvm_pmu_idx_is_high_counter(select_idx))
                counter = upper_32_bits(counter);
 
-       else if (!kvm_pmu_idx_is_64bit(vcpu, select_idx))
+       else if (select_idx != ARMV8_PMU_CYCLE_IDX)
                counter = lower_32_bits(counter);
 
        return counter;

Yeah, I wondered about that one. I've folded that in the patch.

Quoted 53 lines
Quoted 42 lines
Quoted 2 lines

Though unsetting _PMCR_LC is deprecated so I can't imagine this causes any issue.

Deprecated, yes. Disallowed, no. We'll have to support this as long as we have 32bit capable stuff in the wild. But we could at least start with correctly emulating the setting of the LC bit, see below.

Thanks,

	M.

From c421c17ae1e9c90db4b73bd25485580833321f4b Mon Sep 17 00:00:00 2001
From: Marc Zyngier <[email protected]>
Date: Fri, 4 Oct 2019 11:03:09 +0100
Subject: [PATCH] arm64: KVM: Handle PMCR_EL0.LC as RES1 on pure AArch64
systems

Of PMCR_EL0.LC, the ARMv8 ARM says:

"In an AArch64 only implementation, this field is RES 1."

So be it.

---
 arch/arm64/kvm/sys_regs.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/arm64/kvm/sys_regs.c +2 -0
index 2071260a275b..46822afc57e0 100644
@@ -632,6 +632,8 @@ static void reset_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
 	 */
 	val = ((pmcr & ~ARMV8_PMU_PMCR_MASK)
 	       | (ARMV8_PMU_PMCR_MASK & 0xdecafbad)) & (~ARMV8_PMU_PMCR_E);
+	if (!system_supports_32bit_el0())
+		val |= ARMV8_PMU_PMCR_LC;
 	__vcpu_sys_reg(vcpu, r->reg) = val;
 }
 

arch/arm64/kvm/sys_regs.c static bool access_pmcr(struct kvm_vcpu *vcpu, struct sys_reg_params *p, · line 691

Quoted 8 lines
arch/arm64/kvm/sys_regs.c +2 -0
@@ -682,6 +684,8 @@ static bool access_pmcr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
 		val = __vcpu_sys_reg(vcpu, PMCR_EL0);
 		val &= ~ARMV8_PMU_PMCR_MASK;
 		val |= p->regval & ARMV8_PMU_PMCR_MASK;
+		if (!system_supports_32bit_el0())
+			val |= ARMV8_PMU_PMCR_LC;
 		__vcpu_sys_reg(vcpu, PMCR_EL0) = val;
 		kvm_pmu_handle_pmcr(vcpu, val);
 		kvm_vcpu_pmu_restore_guest(vcpu);

This looks good to me.

Quoted 1 line

Thanks,

	M.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/kvmarm/kvmarm.git/commit/?h=next&id=b9195ff4accaa46ad5ed95435a3a69fdb7506ceb

Signature · 1 line
Jazz is not dead, it just smells funny.
#5 lore raw
Quoted 101 lines · Marc Zyngier
Quoted 70 lines · Andrew Murray
Quoted 68 lines · Marc Zyngier
Quoted 46 lines · On Fri, 4 Oct 2019 09:55:55 +0100 Andrew Murray <[email protected]> wrote:
Quoted 4 lines · Marc Zyngier

When a counter is disabled, its value is sampled before the event is being disabled, and the value written back in the shadow register.

In that process, the value gets truncated to 32bit, which is adequate

Doh, that shouldn't have happened.

Quoted 20 lines

for any counter but the cycle counter, which can be configured to hold a 64bit value. This obviously results in a corrupted counter, and things like "perf record -e cycles" not working at all when run in a guest...

Make the truncation conditional on the counter not being 64bit.

Fixes 80f393a23be6 ("KVM: arm/arm64: Support chained PMU counters")
Cc Andrew Murray <[email protected]>
---
 virt/kvm/arm/pmu.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
virt/kvm/arm/pmu.c +0 -0
index 362a01886bab..d716aef2bae9 100644

virt/kvm/arm/pmu.c static void kvm_pmu_stop_counter(struct kvm_vcpu *vcpu, struct kvm_pmc *pmc) · line 213

Quoted 9 lines
virt/kvm/arm/pmu.c +3 -1
@@ -206,9 +206,11 @@ static void kvm_pmu_stop_counter(struct kvm_vcpu *vcpu, struct kvm_pmc *pmc)
 		__vcpu_sys_reg(vcpu, reg) = lower_32_bits(counter);
 		__vcpu_sys_reg(vcpu, reg + 1) = upper_32_bits(counter);
 	} else {
+		if (!kvm_pmu_idx_is_64bit(vcpu, pmc->idx))
+			counter = lower_32_bits(counter);
 		reg = (pmc->idx == ARMV8_PMU_CYCLE_IDX)
 		       ? PMCCNTR_EL0 : PMEVCNTR0_EL0 + pmc->idx;
-		__vcpu_sys_reg(vcpu, reg) = lower_32_bits(counter);
+		__vcpu_sys_reg(vcpu, reg) = counter;  

The other uses of lower_32_bits look OK to me.

As a side note, I'm not convinced that the implementation (or perhaps the use of) kvm_pmu_idx_is_64bit is correct:

static bool kvm_pmu_idx_is_64bit(struct kvm_vcpu *vcpu, u64 select_idx)

Quoted 8 lines
{
        return (select_idx == ARMV8_PMU_CYCLE_IDX &&
                __vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_LC);
}

We shouldn't truncate the value of a cycle counter to 32 bits just because _PMCR_LC is unset. We should only be interested in _PMCR_LC when setting the sample_period.

That's a good point. The ARMv8 ARM says:

"Long cycle counter enable. Determines when unsigned overflow is recorded by the cycle counter overflow bit."

which doesn't say anything about the counter being truncated one way or another.

Quoted 1 line

If you agree this is wrong, I'll spin a change.

Quoted 8 lines
Quoted 4 lines

I still think kvm_pmu_idx_is_64bit() correct, and would be easily extended to supporting the ARMv8.5-PMU extension. However, it'd be better to just detect the cycle counter in the current patch rather than relying on the above helper:

I guess at present kvm_pmu_idx_is_64bit has the meaning "does the counter have a 64 bit overflow". (And we check for the CYCLE_IDX because at present thats the only thing that *can* have a 64bit overflow.)

Exactly. The function is badly named, but hey, we'll live with it until we refactor this further.

Quoted 8 lines
Quoted 8 lines
virt/kvm/arm/pmu.c +0 -0
index d716aef2bae9..90a90d8f7280 100644
@@ -206,7 +206,7 @@ static void kvm_pmu_stop_counter(struct kvm_vcpu *vcpu, struct kvm_pmc *pmc)
 		__vcpu_sys_reg(vcpu, reg) = lower_32_bits(counter);
 		__vcpu_sys_reg(vcpu, reg + 1) = upper_32_bits(counter);
 	} else {
Quoted 8 lines

virt/kvm/arm/pmu.c static void kvm_pmu_stop_counter(struct kvm_vcpu *vcpu, struct kvm_pmc *pmc) · line 212

Quoted 6 lines
virt/kvm/arm/pmu.c +1 -1
-		if (!kvm_pmu_idx_is_64bit(vcpu, pmc->idx))
+		if (pmc->idx != ARMV8_PMU_CYCLE_IDX)
 			counter = lower_32_bits(counter);
 		reg = (pmc->idx == ARMV8_PMU_CYCLE_IDX)
 		       ? PMCCNTR_EL0 : PMEVCNTR0_EL0 + pmc->idx;

That looks fine to me.

Quoted 3 lines

I've now revamped that code further, as having an if() and a conditional expression that check the same this is a bit... meh. The result is more invasive, but far more readable [1].

That looks OK to be (b9195ff).

Thanks,

Andrew Murray

Quoted 84 lines
Quoted 10 lines
Quoted 2 lines

As for revamping the rest of the code, that's 5.5 material.

The only other change required would be as follows:

virt/kvm/arm/pmu.c +0 -0
index 362a01886bab..2435119b8524 100644

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

Quoted 8 lines
virt/kvm/arm/pmu.c +1 -1
@@ -147,7 +147,7 @@ u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx)
            kvm_pmu_idx_is_high_counter(select_idx))
                counter = upper_32_bits(counter);
 
-       else if (!kvm_pmu_idx_is_64bit(vcpu, select_idx))
+       else if (select_idx != ARMV8_PMU_CYCLE_IDX)
                counter = lower_32_bits(counter);
 
        return counter;

Yeah, I wondered about that one. I've folded that in the patch.

Quoted 53 lines
Quoted 42 lines
Quoted 2 lines

Though unsetting _PMCR_LC is deprecated so I can't imagine this causes any issue.

Deprecated, yes. Disallowed, no. We'll have to support this as long as we have 32bit capable stuff in the wild. But we could at least start with correctly emulating the setting of the LC bit, see below.

Thanks,

	M.

From c421c17ae1e9c90db4b73bd25485580833321f4b Mon Sep 17 00:00:00 2001
From: Marc Zyngier <[email protected]>
Date: Fri, 4 Oct 2019 11:03:09 +0100
Subject: [PATCH] arm64: KVM: Handle PMCR_EL0.LC as RES1 on pure AArch64
systems

Of PMCR_EL0.LC, the ARMv8 ARM says:

"In an AArch64 only implementation, this field is RES 1."

So be it.

---
 arch/arm64/kvm/sys_regs.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/arm64/kvm/sys_regs.c +2 -0
index 2071260a275b..46822afc57e0 100644
@@ -632,6 +632,8 @@ static void reset_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
 	 */
 	val = ((pmcr & ~ARMV8_PMU_PMCR_MASK)
 	       | (ARMV8_PMU_PMCR_MASK & 0xdecafbad)) & (~ARMV8_PMU_PMCR_E);
+	if (!system_supports_32bit_el0())
+		val |= ARMV8_PMU_PMCR_LC;
 	__vcpu_sys_reg(vcpu, r->reg) = val;
 }
 

arch/arm64/kvm/sys_regs.c static bool access_pmcr(struct kvm_vcpu *vcpu, struct sys_reg_params *p, · line 691

Quoted 8 lines
arch/arm64/kvm/sys_regs.c +2 -0
@@ -682,6 +684,8 @@ static bool access_pmcr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
 		val = __vcpu_sys_reg(vcpu, PMCR_EL0);
 		val &= ~ARMV8_PMU_PMCR_MASK;
 		val |= p->regval & ARMV8_PMU_PMCR_MASK;
+		if (!system_supports_32bit_el0())
+			val |= ARMV8_PMU_PMCR_LC;
 		__vcpu_sys_reg(vcpu, PMCR_EL0) = val;
 		kvm_pmu_handle_pmcr(vcpu, val);
 		kvm_vcpu_pmu_restore_guest(vcpu);

This looks good to me.

Quoted 1 line

Thanks,

	M.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/kvmarm/kvmarm.git/commit/?h=next&id=b9195ff4accaa46ad5ed95435a3a69fdb7506ceb

Signature · 1 line
Jazz is not dead, it just smells funny.