# \[PATCH\] KVM: arm64: Don't zero the cycle count register when PMCR\_EL0.P is set

- Subsystems: ARM64 PORT (AARCH64 ARCHITECTURE), KERNEL VIRTUAL MACHINE FOR ARM64 (KVM/arm64), THE REST
- Maintainers: Catalin Marinas, Will Deacon, Marc Zyngier, Oliver Upton, Linus Torvalds
- Status: LANDED (2a71fabf6a1b · 2021-06-18 12:23)
- Landed in mainline as 2a71fabf6a1b on 2021-06-18.
- List: linux-arm-kernel
- 3 messages, 2 authors
- Page: https://ratatoskr.run/linux-arm-kernel/2021/06/8808682/t

## \[PATCH\] KVM: arm64: Don't zero the cycle count register when PMCR\_EL0.P is set

- From: Alexandru Elisei \<hidden\>
- Date: 2021-06-18 10:52:25 
- Message: https://ratatoskr.run/linux-arm-kernel/2021/06/8808682.md

```text
According to ARM DDI 0487G.a, page D13-3895, setting the PMCR_EL0.P bit to
1 has the following effect:

"Reset all event counters accessible in the current Exception level, not
including PMCCNTR_EL0, to zero."

Similar behaviour is described for AArch32 on page G8-7022. Make it so.

Fixes: c01d6a18023b ("KVM: arm64: pmu: Only handle supported event counters")
Signed-off-by: Alexandru Elisei <redacted>
---
Found by code inspection.

Entertained the idea of restricting the number of bits in
for_each_set_bit() to 31 since Linux (and the architecture, to some degree)
treats the cycle count register as the 32nd event counter. Settled on this
approach because I think it's clearer.

 arch/arm64/kvm/pmu-emul.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c
index fd167d4f4215..ecc0d19c8cc1 100644
--- a/arch/arm64/kvm/pmu-emul.c
+++ b/arch/arm64/kvm/pmu-emul.c
@@ -578,6 +578,7 @@ void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val)
 		kvm_pmu_set_counter_value(vcpu, ARMV8_PMU_CYCLE_IDX, 0);
 
 	if (val & ARMV8_PMU_PMCR_P) {
+		mask &= ~BIT(ARMV8_PMU_CYCLE_IDX);
 		for_each_set_bit(i, &mask, 32)
 			kvm_pmu_set_counter_value(vcpu, i, 0);
 	}
-- 
2.32.0


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
```

## Re: \[PATCH\] KVM: arm64: Don't zero the cycle count register when PMCR\_EL0.P is set

- From: Alexandru Elisei \<hidden\>
- Date: 2021-06-18 11:17:42 
- Message: https://ratatoskr.run/linux-arm-kernel/2021/06/8808683.md

```text
Argh, forgot to CC Eric.

On 6/18/21 11:51 AM, Alexandru Elisei wrote:
> According to ARM DDI 0487G.a, page D13-3895, setting the PMCR_EL0.P bit to
> 1 has the following effect:
>
> "Reset all event counters accessible in the current Exception level, not
> including PMCCNTR_EL0, to zero."
>
> Similar behaviour is described for AArch32 on page G8-7022. Make it so.
>
> Fixes: c01d6a18023b ("KVM: arm64: pmu: Only handle supported event counters")
> Signed-off-by: Alexandru Elisei <redacted>
> ---
> Found by code inspection.
>
> Entertained the idea of restricting the number of bits in
> for_each_set_bit() to 31 since Linux (and the architecture, to some degree)
> treats the cycle count register as the 32nd event counter. Settled on this
> approach because I think it's clearer.
>
>  arch/arm64/kvm/pmu-emul.c | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c
> index fd167d4f4215..ecc0d19c8cc1 100644
> --- a/arch/arm64/kvm/pmu-emul.c
> +++ b/arch/arm64/kvm/pmu-emul.c
> @@ -578,6 +578,7 @@ void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val)
>  		kvm_pmu_set_counter_value(vcpu, ARMV8_PMU_CYCLE_IDX, 0);
>  
>  	if (val & ARMV8_PMU_PMCR_P) {
> +		mask &= ~BIT(ARMV8_PMU_CYCLE_IDX);
>  		for_each_set_bit(i, &mask, 32)
>  			kvm_pmu_set_counter_value(vcpu, i, 0);
>  	}

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
```

## Re: \[PATCH\] KVM: arm64: Don't zero the cycle count register when PMCR\_EL0.P is set

- From: Marc Zyngier \<maz@kernel.org\>
- Date: 2021-06-18 13:24:51 
- Message: https://ratatoskr.run/linux-arm-kernel/2021/06/8808690.md

```text
On Fri, 18 Jun 2021 11:51:39 +0100, Alexandru Elisei wrote:
> According to ARM DDI 0487G.a, page D13-3895, setting the PMCR_EL0.P bit to
> 1 has the following effect:
> 
> "Reset all event counters accessible in the current Exception level, not
> including PMCCNTR_EL0, to zero."
> 
> Similar behaviour is described for AArch32 on page G8-7022. Make it so.

Applied to kvm-arm64/pmu-fixes, thanks!

[1/1] KVM: arm64: Don't zero the cycle count register when PMCR_EL0.P is set
      commit: 2a71fabf6a1bc9162a84e18d6ab991230ca4d588

Cheers,

	M.
-- 
Without deviation from the norm, progress is not possible.



_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
```
