Skip to contentSkip to conversation

KVM: arm64: PMU: Preserve vPMC registers properly on migration

PATCH Superseded Reiji Watanabe 2 participants 9 messages Mar 2, 2023 – Mar 13, 2023

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover KVM: arm64: PMU: Preserve vPMC registers properly on migration

The series fixes two problems in preserving vPMU counter (vPMC) registers (PMCCNTR_EL0/PMEVCNTR<n>_EL0) during migration.

One of the problems is that KVM may not return the current values of the vPMC registers for KVM_GET_ONE_REG.

The other one might cause KVM to reset the vPMC registers on the first KVM_RUN on the destination. This is because userspace might save PMCR_EL0 with PMCR_EL0.{C,P} bits set on the source, and restore it on the destination.

See patch-1 and patch-2 for details on these issues respectively.

The series is based on v6.2.

Reiji Watanabe (2):
KVM: arm64: PMU: Fix GET_ONE_REG for vPMC regs to return the current
value
KVM: arm64: PMU: Don't save PMCR_EL0.{C,P} for the vCPU

 arch/arm64/kvm/pmu-emul.c |  4 +++-
 arch/arm64/kvm/sys_regs.c | 21 +++++++++++++++++++--
 2 files changed, 22 insertions(+), 3 deletions(-)

base-commit: c9c3395d5e3dcc6daee66c6908354d47bf98cb0c

Signature · 1 line
2.39.2.722.g9855ee24e9-goog
#1 lore raw

PATCH 1/2 KVM: arm64: PMU: Fix GET_ONE_REG for vPMC regs to return the current value

Have KVM_GET_ONE_REG for vPMU counter (vPMC) registers (PMCCNTR_EL0 and PMEVCNTR<n>_EL0) return the sum of the register value in the sysreg file and the current perf event counter value.

Values of vPMC registers are saved in sysreg files on certain occasions. These saved values don't represent the current values of the vPMC registers if the perf events for the vPMCs count events after the save. The current values of those registers are the sum of the sysreg file value and the current perf event counter value. But, when userspace reads those registers (using KVM_GET_ONE_REG), KVM returns the sysreg file value to userspace (not the sum value).

Fix this to return the sum value for KVM_GET_ONE_REG.

---
 arch/arm64/kvm/sys_regs.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
arch/arm64/kvm/sys_regs.c +19 -2
index c6cbfe6b854b..c48c053d6146 100644
@@ -765,6 +765,22 @@ static bool pmu_counter_idx_valid(struct kvm_vcpu *vcpu, u64 idx)
 	return true;
 }
 
+static int get_pmu_evcntr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
+			  u64 *val)
+{
+	u64 idx;
+
+	if (r->CRn == 9 && r->CRm == 13 && r->Op2 == 0)
+		/* PMCCNTR_EL0 */
+		idx = ARMV8_PMU_CYCLE_IDX;
+	else
+		/* PMEVCNTRn_EL0 */
+		idx = ((r->CRm & 3) << 3) | (r->Op2 & 7);
+
+	*val = kvm_pmu_get_counter_value(vcpu, idx);
+	return 0;
+}
+
 static bool access_pmu_evcntr(struct kvm_vcpu *vcpu,
 			      struct sys_reg_params *p,
 			      const struct sys_reg_desc *r)
@@ -981,7 +997,7 @@ static bool access_pmuserenr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
 /* Macro to expand the PMEVCNTRn_EL0 register */
 #define PMU_PMEVCNTR_EL0(n)						\
 	{ PMU_SYS_REG(SYS_PMEVCNTRn_EL0(n)),				\
-	  .reset = reset_pmevcntr,					\
+	  .reset = reset_pmevcntr, .get_user = get_pmu_evcntr,		\
 	  .access = access_pmu_evcntr, .reg = (PMEVCNTR0_EL0 + n), }
 
 /* Macro to expand the PMEVTYPERn_EL0 register */
@@ -1745,7 +1761,8 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 	{ PMU_SYS_REG(SYS_PMCEID1_EL0),
 	  .access = access_pmceid, .reset = NULL },
 	{ PMU_SYS_REG(SYS_PMCCNTR_EL0),
-	  .access = access_pmu_evcntr, .reset = reset_unknown, .reg = PMCCNTR_EL0 },
+	  .access = access_pmu_evcntr, .reset = reset_unknown,
+	  .reg = PMCCNTR_EL0, .get_user = get_pmu_evcntr},
 	{ PMU_SYS_REG(SYS_PMXEVTYPER_EL0),
 	  .access = access_pmu_evtyper, .reset = NULL },
 	{ PMU_SYS_REG(SYS_PMXEVCNTR_EL0),
Signature · 1 line
2.39.2.722.g9855ee24e9-goog
maintainer Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] KVM: arm64: PMU: Fix GET_ONE_REG for vPMC regs to return the current value

Quoted 32 lines · Reiji Watanabe

Have KVM_GET_ONE_REG for vPMU counter (vPMC) registers (PMCCNTR_EL0 and PMEVCNTR<n>_EL0) return the sum of the register value in the sysreg file and the current perf event counter value.

Values of vPMC registers are saved in sysreg files on certain occasions. These saved values don't represent the current values of the vPMC registers if the perf events for the vPMCs count events after the save. The current values of those registers are the sum of the sysreg file value and the current perf event counter value. But, when userspace reads those registers (using KVM_GET_ONE_REG), KVM returns the sysreg file value to userspace (not the sum value).

Fix this to return the sum value for KVM_GET_ONE_REG.

---
 arch/arm64/kvm/sys_regs.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
arch/arm64/kvm/sys_regs.c +4 -0
index c6cbfe6b854b..c48c053d6146 100644
@@ -765,6 +765,22 @@ static bool pmu_counter_idx_valid(struct kvm_vcpu *vcpu, u64 idx)
 	return true;
 }
 
+static int get_pmu_evcntr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
+			  u64 *val)
+{
+	u64 idx;

arch/arm64/kvm/sys_regs.c static bool pmu_counter_idx_valid(struct kvm_vcpu *vcpu, u64 idx) · line 781

Quoted 10 lines
arch/arm64/kvm/sys_regs.c +10 -0
+
+	if (r->CRn == 9 && r->CRm == 13 && r->Op2 == 0)
+		/* PMCCNTR_EL0 */
+		idx = ARMV8_PMU_CYCLE_IDX;
+	else
+		/* PMEVCNTRn_EL0 */
+		idx = ((r->CRm & 3) << 3) | (r->Op2 & 7);
+
+	*val = kvm_pmu_get_counter_value(vcpu, idx);
+	return 0;

It is a bit odd not to return an error when no PMU present, but this is already filtered out by the top-level accessors.

Quoted 15 lines
arch/arm64/kvm/sys_regs.c +3 -1
+}
+
 static bool access_pmu_evcntr(struct kvm_vcpu *vcpu,
 			      struct sys_reg_params *p,
 			      const struct sys_reg_desc *r)
@@ -981,7 +997,7 @@ static bool access_pmuserenr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
 /* Macro to expand the PMEVCNTRn_EL0 register */
 #define PMU_PMEVCNTR_EL0(n)						\
 	{ PMU_SYS_REG(SYS_PMEVCNTRn_EL0(n)),				\
-	  .reset = reset_pmevcntr,					\
+	  .reset = reset_pmevcntr, .get_user = get_pmu_evcntr,		\
 	  .access = access_pmu_evcntr, .reg = (PMEVCNTR0_EL0 + n), }
 
 /* Macro to expand the PMEVTYPERn_EL0 register */

arch/arm64/kvm/sys_regs.c static const struct sys_reg_desc sys_reg_descs[] = { · line 1768

Quoted 9 lines
arch/arm64/kvm/sys_regs.c +2 -1
@@ -1745,7 +1761,8 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 	{ PMU_SYS_REG(SYS_PMCEID1_EL0),
 	  .access = access_pmceid, .reset = NULL },
 	{ PMU_SYS_REG(SYS_PMCCNTR_EL0),
-	  .access = access_pmu_evcntr, .reset = reset_unknown, .reg = PMCCNTR_EL0 },
+	  .access = access_pmu_evcntr, .reset = reset_unknown,
+	  .reg = PMCCNTR_EL0, .get_user = get_pmu_evcntr},
 	{ PMU_SYS_REG(SYS_PMXEVTYPER_EL0),
 	  .access = access_pmu_evtyper, .reset = NULL },
 	{ PMU_SYS_REG(SYS_PMXEVCNTR_EL0),
	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#3 lore raw

Re: [PATCH 1/2] KVM: arm64: PMU: Fix GET_ONE_REG for vPMC regs to return the current value

Hi Marc,

Quoted 40 lines · Marc Zyngier
Quoted 37 lines · Reiji Watanabe

Have KVM_GET_ONE_REG for vPMU counter (vPMC) registers (PMCCNTR_EL0 and PMEVCNTR<n>_EL0) return the sum of the register value in the sysreg file and the current perf event counter value.

Values of vPMC registers are saved in sysreg files on certain occasions. These saved values don't represent the current values of the vPMC registers if the perf events for the vPMCs count events after the save. The current values of those registers are the sum of the sysreg file value and the current perf event counter value. But, when userspace reads those registers (using KVM_GET_ONE_REG), KVM returns the sysreg file value to userspace (not the sum value).

Fix this to return the sum value for KVM_GET_ONE_REG.

---
 arch/arm64/kvm/sys_regs.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
arch/arm64/kvm/sys_regs.c +9 -0
index c6cbfe6b854b..c48c053d6146 100644
@@ -765,6 +765,22 @@ static bool pmu_counter_idx_valid(struct kvm_vcpu *vcpu, u64 idx)
      return true;
 }
 
+static int get_pmu_evcntr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
+                       u64 *val)
+{
+     u64 idx;
+
+     if (r->CRn == 9 && r->CRm == 13 && r->Op2 == 0)
+             /* PMCCNTR_EL0 */
+             idx = ARMV8_PMU_CYCLE_IDX;
+     else
Quoted 8 lines

arch/arm64/kvm/sys_regs.c static bool pmu_counter_idx_valid(struct kvm_vcpu *vcpu, u64 idx) · line 781

Quoted 5 lines
arch/arm64/kvm/sys_regs.c +5 -0
+             /* PMEVCNTRn_EL0 */
+             idx = ((r->CRm & 3) << 3) | (r->Op2 & 7);
+
+     *val = kvm_pmu_get_counter_value(vcpu, idx);
+     return 0;

It is a bit odd not to return an error when no PMU present, but this is already filtered out by the top-level accessors.

Yes, exactly.

Quoted 14 lines
Quoted 14 lines
arch/arm64/kvm/sys_regs.c +3 -1
+}
+
 static bool access_pmu_evcntr(struct kvm_vcpu *vcpu,
                            struct sys_reg_params *p,
                            const struct sys_reg_desc *r)
@@ -981,7 +997,7 @@ static bool access_pmuserenr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
 /* Macro to expand the PMEVCNTRn_EL0 register */
 #define PMU_PMEVCNTR_EL0(n)                                          \
      { PMU_SYS_REG(SYS_PMEVCNTRn_EL0(n)),                            \
-       .reset = reset_pmevcntr,                                      \
+       .reset = reset_pmevcntr, .get_user = get_pmu_evcntr,          \
        .access = access_pmu_evcntr, .reg = (PMEVCNTR0_EL0 + n), }
 
Quoted 13 lines

arch/arm64/kvm/sys_regs.c static const struct sys_reg_desc sys_reg_descs[] = { · line 1768

Quoted 11 lines
arch/arm64/kvm/sys_regs.c +2 -1
 /* Macro to expand the PMEVTYPERn_EL0 register */
@@ -1745,7 +1761,8 @@ static const struct sys_reg_desc sys_reg_descs[] = {
      { PMU_SYS_REG(SYS_PMCEID1_EL0),
        .access = access_pmceid, .reset = NULL },
      { PMU_SYS_REG(SYS_PMCCNTR_EL0),
-       .access = access_pmu_evcntr, .reset = reset_unknown, .reg = PMCCNTR_EL0 },
+       .access = access_pmu_evcntr, .reset = reset_unknown,
+       .reg = PMCCNTR_EL0, .get_user = get_pmu_evcntr},
      { PMU_SYS_REG(SYS_PMXEVTYPER_EL0),
        .access = access_pmu_evtyper, .reset = NULL },
      { PMU_SYS_REG(SYS_PMXEVCNTR_EL0),

Thank you!
Reiji

Quoted 5 lines
        M.

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

#4 lore raw

PATCH 2/2 KVM: arm64: PMU: Don't save PMCR_EL0.{C,P} for the vCPU

In reply to Reiji Watanabe

Presently, when a guest writes 1 to PMCR_EL0.{C,P}, which is WO/RAZ, KVM saves the register value, including these bits.
When userspace reads the register using KVM_GET_ONE_REG, KVM returns the saved register value as it is (the saved value might have these bits set). This could result in userspace setting these bits on the destination during migration. Consequently, KVM may end up resetting the vPMU counter registers (PMCCNTR_EL0 and/or PMEVCNTR<n>_EL0) to zero on the first KVM_RUN after migration.

Fix this by not saving those bits when a guest writes 1 to those bits.

---
 arch/arm64/kvm/pmu-emul.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/arm64/kvm/pmu-emul.c +3 -1
index 24908400e190..a5a0a9811ddb 100644
@@ -538,7 +538,9 @@ void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val)
 	if (!kvm_pmu_is_3p5(vcpu))
 		val &= ~ARMV8_PMU_PMCR_LP;
 
-	__vcpu_sys_reg(vcpu, PMCR_EL0) = val;
+	/* The reset bits don't indicate any state, and shouldn't be saved. */
+	__vcpu_sys_reg(vcpu, PMCR_EL0) =
+				val & ~(ARMV8_PMU_PMCR_C | ARMV8_PMU_PMCR_P);
 
 	if (val & ARMV8_PMU_PMCR_E) {
 		kvm_pmu_enable_counter_mask(vcpu,
Signature · 1 line
2.39.2.722.g9855ee24e9-goog
maintainer Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] KVM: arm64: PMU: Don't save PMCR_EL0.{C,P} for the vCPU

Quoted 22 lines · Reiji Watanabe

Presently, when a guest writes 1 to PMCR_EL0.{C,P}, which is WO/RAZ, KVM saves the register value, including these bits.
When userspace reads the register using KVM_GET_ONE_REG, KVM returns the saved register value as it is (the saved value might have these bits set). This could result in userspace setting these bits on the destination during migration. Consequently, KVM may end up resetting the vPMU counter registers (PMCCNTR_EL0 and/or PMEVCNTR<n>_EL0) to zero on the first KVM_RUN after migration.

Fix this by not saving those bits when a guest writes 1 to those bits.

---
 arch/arm64/kvm/pmu-emul.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/arm64/kvm/pmu-emul.c +0 -0
index 24908400e190..a5a0a9811ddb 100644

arch/arm64/kvm/pmu-emul.c void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val) · line 543

Quoted 7 lines
arch/arm64/kvm/pmu-emul.c +3 -1
@@ -538,7 +538,9 @@ void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val)
 	if (!kvm_pmu_is_3p5(vcpu))
 		val &= ~ARMV8_PMU_PMCR_LP;
 
-	__vcpu_sys_reg(vcpu, PMCR_EL0) = val;
+	/* The reset bits don't indicate any state, and shouldn't be saved. */
+	__vcpu_sys_reg(vcpu, PMCR_EL0) =
+				val & ~(ARMV8_PMU_PMCR_C | ARMV8_PMU_PMCR_P);

nit: assignment on a single line, please.

With that,

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#6 lore raw

Re: [PATCH 2/2] KVM: arm64: PMU: Don't save PMCR_EL0.{C,P} for the vCPU

Hi Marc,

Quoted 28 lines · Marc Zyngier
Quoted 25 lines · Reiji Watanabe

Presently, when a guest writes 1 to PMCR_EL0.{C,P}, which is WO/RAZ, KVM saves the register value, including these bits.
When userspace reads the register using KVM_GET_ONE_REG, KVM returns the saved register value as it is (the saved value might have these bits set). This could result in userspace setting these bits on the destination during migration. Consequently, KVM may end up resetting the vPMU counter registers (PMCCNTR_EL0 and/or PMEVCNTR<n>_EL0) to zero on the first KVM_RUN after migration.

Fix this by not saving those bits when a guest writes 1 to those bits.

---
 arch/arm64/kvm/pmu-emul.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/arm64/kvm/pmu-emul.c +0 -0
index 24908400e190..a5a0a9811ddb 100644
@@ -538,7 +538,9 @@ void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val)
      if (!kvm_pmu_is_3p5(vcpu))
              val &= ~ARMV8_PMU_PMCR_LP;
 
Quoted 6 lines

arch/arm64/kvm/pmu-emul.c void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val) · line 543

Quoted 4 lines
arch/arm64/kvm/pmu-emul.c +3 -1
-     __vcpu_sys_reg(vcpu, PMCR_EL0) = val;
+     /* The reset bits don't indicate any state, and shouldn't be saved. */
+     __vcpu_sys_reg(vcpu, PMCR_EL0) =
+                             val & ~(ARMV8_PMU_PMCR_C | ARMV8_PMU_PMCR_P);

nit: assignment on a single line, please.

Yes, I fixed it in v2!

Quoted 4 lines

With that,

Thank you!
Reiji

maintainer
#7 lore raw
In reply to Reiji Watanabe
Quoted 11 lines · Reiji Watanabe

The series fixes two problems in preserving vPMU counter (vPMC) registers (PMCCNTR_EL0/PMEVCNTR<n>_EL0) during migration.

One of the problems is that KVM may not return the current values of the vPMC registers for KVM_GET_ONE_REG.

The other one might cause KVM to reset the vPMC registers on the first KVM_RUN on the destination. This is because userspace might save PMCR_EL0 with PMCR_EL0.{C,P} bits set on the source, and restore it on the destination.

This looks good to me. Can you please add the relevant Fixes: tags and a Cc: to stable? With that, that'd be a candidate for -rc3, I think.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#8 lore raw

Hi Marc,

Quoted 10 lines · Marc Zyngier
Quoted 7 lines · Reiji Watanabe

The series fixes two problems in preserving vPMU counter (vPMC) registers (PMCCNTR_EL0/PMEVCNTR<n>_EL0) during migration.

One of the problems is that KVM may not return the current values of the vPMC registers for KVM_GET_ONE_REG.

Quoted 7 lines
Quoted 4 lines

The other one might cause KVM to reset the vPMC registers on the first KVM_RUN on the destination. This is because userspace might save PMCR_EL0 with PMCR_EL0.{C,P} bits set on the source, and restore it on the destination.

This looks good to me. Can you please add the relevant Fixes: tags and a Cc: to stable? With that, that'd be a candidate for -rc3, I think.

Thank you for the review!

I posted v2, which addressed the comments above, and the comment for the patch-2 (remove the line break).

Thank you,
Reiji

Quoted 7 lines

Thanks,

        M.

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