linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/4] KVM: arm64: PMU: Fix PMUver related handling of vPMU support
@ 2023-08-19  4:39 Reiji Watanabe
  2023-08-19  4:39 ` [PATCH v3 1/4] KVM: arm64: PMU: Disallow vPMU on non-uniform PMUVer Reiji Watanabe
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Reiji Watanabe @ 2023-08-19  4:39 UTC (permalink / raw)
  To: Marc Zyngier, Oliver Upton, kvmarm
  Cc: kvm, linux-arm-kernel, James Morse, Alexandru Elisei, Zenghui Yu,
	Suzuki K Poulose, Jing Zhang, Raghavendra Rao Anata,
	Reiji Watanabe

This series fixes a couple of PMUver related handling of
vPMU support.

On systems where the PMUVer is not uniform across all PEs,
KVM currently does not advertise PMUv3 to the guest,
even if userspace successfully runs KVM_ARM_VCPU_INIT with
KVM_ARM_VCPU_PMU_V3.

The patch-1 will address this inconsistent behavior by
disallowing userspace from configuring vPMU,
as such systems would be extremely uncommon and unlikely
to even use KVM (according to Marc [1]).

The patch-2 will fix improper use of the host's PMUver to
determine a valid range of PMU events for the guest (the
guest's PMUver should be used instead).

The patch-3 and patch-4 will try to hide the STALL_SLOT*
events unconditionally per Oliver's suggestion [2].
Presently, KVM hides the STALL_SLOT event depending on the
host PMU version, instead of the guest's PMU version, which
doesn't seem to be accurate, as it appears that older PMU than
PMUv3p4 could implement the event according to the Arm ARM.
Exposing the STALL_SLOT event without PMMIR_EL1 (supported
from PMUv3p4) for the guest won't be very useful though.
The patch-3 stops advertising the event for guest unconditionally,
rather than fixing or keeping the inaccurate checking to advertise
the event for the case, where it is not very useful.
The patch-4 stops advertising the STALL_SLOT_{FRONT,BACK}END
events to the guest, similar to the STALL_SLOT event, as when any
of these three events are implemented, all three of them should
be implemented, according to the Arm ARM.

This series is based on 6.5-rc6.

v3:
 - Fixed to not insert the host PMU instance in the list of valid
   PMUs [Oliver]

v2: https://lore.kernel.org/all/20230728181907.1759513-1-reijiw@google.com/
 - Combined a two separate v1 series
 - Disallow STALL_SLOT* event unconditionally [Oliver]

v1: https://lore.kernel.org/all/20230610061520.3026530-1-reijiw@google.com/
    https://lore.kernel.org/all/20230610194510.4146549-1-reijiw@google.com/

[1] https://lore.kernel.org/all/874jnqp73o.wl-maz@kernel.org/
[2] https://lore.kernel.org/all/ZIm1kdFBfXYMdfbV@linux.dev/

Reiji Watanabe (4):
  KVM: arm64: PMU: Disallow vPMU on non-uniform PMUVer
  KVM: arm64: PMU: Avoid inappropriate use of host's PMUVer
  KVM: arm64: PMU: Don't advertise the STALL_SLOT event
  KVM: arm64: PMU: Don't advertise STALL_SLOT_{FRONTEND,BACKEND}

 arch/arm64/kvm/pmu-emul.c | 37 ++++++++++++++++++++++++++-----------
 1 file changed, 26 insertions(+), 11 deletions(-)


base-commit: 2ccdd1b13c591d306f0401d98dedc4bdcd02b421
-- 
2.42.0.rc1.204.g551eb34607-goog


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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v3 1/4] KVM: arm64: PMU: Disallow vPMU on non-uniform PMUVer
  2023-08-19  4:39 [PATCH v3 0/4] KVM: arm64: PMU: Fix PMUver related handling of vPMU support Reiji Watanabe
@ 2023-08-19  4:39 ` Reiji Watanabe
  2023-08-19  4:39 ` [PATCH v3 2/4] KVM: arm64: PMU: Avoid inappropriate use of host's PMUVer Reiji Watanabe
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Reiji Watanabe @ 2023-08-19  4:39 UTC (permalink / raw)
  To: Marc Zyngier, Oliver Upton, kvmarm
  Cc: kvm, linux-arm-kernel, James Morse, Alexandru Elisei, Zenghui Yu,
	Suzuki K Poulose, Jing Zhang, Raghavendra Rao Anata,
	Reiji Watanabe

Disallow userspace from configuring vPMU for guests on systems
where the PMUVer is not uniform across all PEs.
KVM has not been advertising PMUv3 to the guests with vPMU on
such systems anyway, and such systems would be extremely
uncommon and unlikely to even use KVM.

Signed-off-by: Reiji Watanabe <reijiw@google.com>
---
 arch/arm64/kvm/pmu-emul.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c
index 560650972478..689bbd88fd69 100644
--- a/arch/arm64/kvm/pmu-emul.c
+++ b/arch/arm64/kvm/pmu-emul.c
@@ -14,6 +14,7 @@
 #include <asm/kvm_emulate.h>
 #include <kvm/arm_pmu.h>
 #include <kvm/arm_vgic.h>
+#include <asm/arm_pmuv3.h>
 
 #define PERF_ATTR_CFG1_COUNTER_64BIT	BIT(0)
 
@@ -672,8 +673,11 @@ void kvm_host_pmu_init(struct arm_pmu *pmu)
 {
 	struct arm_pmu_entry *entry;
 
-	if (pmu->pmuver == ID_AA64DFR0_EL1_PMUVer_NI ||
-	    pmu->pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF)
+	/*
+	 * Check the sanitised PMU version for the system, as KVM does not
+	 * support implementations where PMUv3 exists on a subset of CPUs.
+	 */
+	if (!pmuv3_implemented(kvm_arm_pmu_get_pmuver_limit()))
 		return;
 
 	mutex_lock(&arm_pmus_lock);
-- 
2.42.0.rc1.204.g551eb34607-goog


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

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v3 2/4] KVM: arm64: PMU: Avoid inappropriate use of host's PMUVer
  2023-08-19  4:39 [PATCH v3 0/4] KVM: arm64: PMU: Fix PMUver related handling of vPMU support Reiji Watanabe
  2023-08-19  4:39 ` [PATCH v3 1/4] KVM: arm64: PMU: Disallow vPMU on non-uniform PMUVer Reiji Watanabe
@ 2023-08-19  4:39 ` Reiji Watanabe
  2023-08-19  4:39 ` [PATCH v3 3/4] KVM: arm64: PMU: Don't advertise the STALL_SLOT event Reiji Watanabe
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Reiji Watanabe @ 2023-08-19  4:39 UTC (permalink / raw)
  To: Marc Zyngier, Oliver Upton, kvmarm
  Cc: kvm, linux-arm-kernel, James Morse, Alexandru Elisei, Zenghui Yu,
	Suzuki K Poulose, Jing Zhang, Raghavendra Rao Anata,
	Reiji Watanabe

Avoid using the PMUVer of the host's PMU hardware to determine
the PMU event mask, except in one case, as the value of host's
PMUVer may differ from the value of ID_AA64DFR0_EL1.PMUVer for
the guest.

The exception case is when using the PMUVer to determine the
valid range of events for KVM_ARM_VCPU_PMU_V3_FILTER, as it has
been allowing userspace to specify events that are valid for
the PMU hardware, regardless of the value of the guest's
ID_AA64DFR0_EL1.PMUVer.  KVM will use a valid range of events
based on the value of the guest's ID_AA64DFR0_EL1.PMUVer,
in order to effectively filter events that the guest attempts
to program though.

Signed-off-by: Reiji Watanabe <reijiw@google.com>
---
 arch/arm64/kvm/pmu-emul.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c
index 689bbd88fd69..eaeb8fea7971 100644
--- a/arch/arm64/kvm/pmu-emul.c
+++ b/arch/arm64/kvm/pmu-emul.c
@@ -36,12 +36,8 @@ static struct kvm_pmc *kvm_vcpu_idx_to_pmc(struct kvm_vcpu *vcpu, int cnt_idx)
 	return &vcpu->arch.pmu.pmc[cnt_idx];
 }
 
-static u32 kvm_pmu_event_mask(struct kvm *kvm)
+static u32 __kvm_pmu_event_mask(unsigned int pmuver)
 {
-	unsigned int pmuver;
-
-	pmuver = kvm->arch.arm_pmu->pmuver;
-
 	switch (pmuver) {
 	case ID_AA64DFR0_EL1_PMUVer_IMP:
 		return GENMASK(9, 0);
@@ -56,6 +52,14 @@ static u32 kvm_pmu_event_mask(struct kvm *kvm)
 	}
 }
 
+static u32 kvm_pmu_event_mask(struct kvm *kvm)
+{
+	u64 dfr0 = IDREG(kvm, SYS_ID_AA64DFR0_EL1);
+	u8 pmuver = SYS_FIELD_GET(ID_AA64DFR0_EL1, PMUVer, dfr0);
+
+	return __kvm_pmu_event_mask(pmuver);
+}
+
 /**
  * kvm_pmc_is_64bit - determine if counter is 64bit
  * @pmc: counter context
@@ -954,11 +958,17 @@ int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
 		return 0;
 	}
 	case KVM_ARM_VCPU_PMU_V3_FILTER: {
+		u8 pmuver = kvm_arm_pmu_get_pmuver_limit();
 		struct kvm_pmu_event_filter __user *uaddr;
 		struct kvm_pmu_event_filter filter;
 		int nr_events;
 
-		nr_events = kvm_pmu_event_mask(kvm) + 1;
+		/*
+		 * Allow userspace to specify an event filter for the entire
+		 * event range supported by PMUVer of the hardware, rather
+		 * than the guest's PMUVer for KVM backward compatibility.
+		 */
+		nr_events = __kvm_pmu_event_mask(pmuver) + 1;
 
 		uaddr = (struct kvm_pmu_event_filter __user *)(long)attr->addr;
 
-- 
2.42.0.rc1.204.g551eb34607-goog


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

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v3 3/4] KVM: arm64: PMU: Don't advertise the STALL_SLOT event
  2023-08-19  4:39 [PATCH v3 0/4] KVM: arm64: PMU: Fix PMUver related handling of vPMU support Reiji Watanabe
  2023-08-19  4:39 ` [PATCH v3 1/4] KVM: arm64: PMU: Disallow vPMU on non-uniform PMUVer Reiji Watanabe
  2023-08-19  4:39 ` [PATCH v3 2/4] KVM: arm64: PMU: Avoid inappropriate use of host's PMUVer Reiji Watanabe
@ 2023-08-19  4:39 ` Reiji Watanabe
  2023-08-19  4:39 ` [PATCH v3 4/4] KVM: arm64: PMU: Don't advertise STALL_SLOT_{FRONTEND,BACKEND} Reiji Watanabe
  2023-08-20  8:44 ` [PATCH v3 0/4] KVM: arm64: PMU: Fix PMUver related handling of vPMU support Marc Zyngier
  4 siblings, 0 replies; 6+ messages in thread
From: Reiji Watanabe @ 2023-08-19  4:39 UTC (permalink / raw)
  To: Marc Zyngier, Oliver Upton, kvmarm
  Cc: kvm, linux-arm-kernel, James Morse, Alexandru Elisei, Zenghui Yu,
	Suzuki K Poulose, Jing Zhang, Raghavendra Rao Anata,
	Reiji Watanabe

Currently, KVM hides the STALL_SLOT event for guests if the
host PMU version is PMUv3p4 or newer, as PMMIR_EL1 is handled
as RAZ for the guests. But, this should be based on the guests'
PMU version (instead of the host PMU version), as an older PMU
that doesn't support PMMIR_EL1 could support the STALL_SLOT
event, according to the Arm ARM. Exposing the STALL_SLOT event
without PMMIR_EL1 won't be very useful anyway though.

Stop advertising the STALL_SLOT event for guests unconditionally,
rather than fixing or keeping the inaccurate checking to
advertise the event for the case, where it is not very useful.

Suggested-by: Oliver Upton <oliver.upton@linux.dev>
Signed-off-by: Reiji Watanabe <reijiw@google.com>
---
 arch/arm64/kvm/pmu-emul.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c
index eaeb8fea7971..b9633deff32a 100644
--- a/arch/arm64/kvm/pmu-emul.c
+++ b/arch/arm64/kvm/pmu-emul.c
@@ -761,8 +761,7 @@ u64 kvm_pmu_get_pmceid(struct kvm_vcpu *vcpu, bool pmceid1)
 		 * Don't advertise STALL_SLOT, as PMMIR_EL0 is handled
 		 * as RAZ
 		 */
-		if (vcpu->kvm->arch.arm_pmu->pmuver >= ID_AA64DFR0_EL1_PMUVer_V3P4)
-			val &= ~BIT_ULL(ARMV8_PMUV3_PERFCTR_STALL_SLOT - 32);
+		val &= ~BIT_ULL(ARMV8_PMUV3_PERFCTR_STALL_SLOT - 32);
 		base = 32;
 	}
 
-- 
2.42.0.rc1.204.g551eb34607-goog


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

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v3 4/4] KVM: arm64: PMU: Don't advertise STALL_SLOT_{FRONTEND,BACKEND}
  2023-08-19  4:39 [PATCH v3 0/4] KVM: arm64: PMU: Fix PMUver related handling of vPMU support Reiji Watanabe
                   ` (2 preceding siblings ...)
  2023-08-19  4:39 ` [PATCH v3 3/4] KVM: arm64: PMU: Don't advertise the STALL_SLOT event Reiji Watanabe
@ 2023-08-19  4:39 ` Reiji Watanabe
  2023-08-20  8:44 ` [PATCH v3 0/4] KVM: arm64: PMU: Fix PMUver related handling of vPMU support Marc Zyngier
  4 siblings, 0 replies; 6+ messages in thread
From: Reiji Watanabe @ 2023-08-19  4:39 UTC (permalink / raw)
  To: Marc Zyngier, Oliver Upton, kvmarm
  Cc: kvm, linux-arm-kernel, James Morse, Alexandru Elisei, Zenghui Yu,
	Suzuki K Poulose, Jing Zhang, Raghavendra Rao Anata,
	Reiji Watanabe

Don't advertise STALL_SLOT_{FRONT,BACK}END events to the guest,
similar to STALL_SLOT event, as when any of these three events
are implemented, all three of them should be implemented,
according to the Arm ARM.

Suggested-by: Oliver Upton <oliver.upton@linux.dev>
Signed-off-by: Reiji Watanabe <reijiw@google.com>
---
 arch/arm64/kvm/pmu-emul.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c
index b9633deff32a..6b066e04dc5d 100644
--- a/arch/arm64/kvm/pmu-emul.c
+++ b/arch/arm64/kvm/pmu-emul.c
@@ -758,10 +758,12 @@ u64 kvm_pmu_get_pmceid(struct kvm_vcpu *vcpu, bool pmceid1)
 	} else {
 		val = read_sysreg(pmceid1_el0);
 		/*
-		 * Don't advertise STALL_SLOT, as PMMIR_EL0 is handled
+		 * Don't advertise STALL_SLOT*, as PMMIR_EL0 is handled
 		 * as RAZ
 		 */
-		val &= ~BIT_ULL(ARMV8_PMUV3_PERFCTR_STALL_SLOT - 32);
+		val &= ~(BIT_ULL(ARMV8_PMUV3_PERFCTR_STALL_SLOT - 32) |
+			 BIT_ULL(ARMV8_PMUV3_PERFCTR_STALL_SLOT_FRONTEND - 32) |
+			 BIT_ULL(ARMV8_PMUV3_PERFCTR_STALL_SLOT_BACKEND - 32));
 		base = 32;
 	}
 
-- 
2.42.0.rc1.204.g551eb34607-goog


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

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v3 0/4] KVM: arm64: PMU: Fix PMUver related handling of vPMU support
  2023-08-19  4:39 [PATCH v3 0/4] KVM: arm64: PMU: Fix PMUver related handling of vPMU support Reiji Watanabe
                   ` (3 preceding siblings ...)
  2023-08-19  4:39 ` [PATCH v3 4/4] KVM: arm64: PMU: Don't advertise STALL_SLOT_{FRONTEND,BACKEND} Reiji Watanabe
@ 2023-08-20  8:44 ` Marc Zyngier
  4 siblings, 0 replies; 6+ messages in thread
From: Marc Zyngier @ 2023-08-20  8:44 UTC (permalink / raw)
  To: kvmarm, Reiji Watanabe, Oliver Upton
  Cc: James Morse, Jing Zhang, Raghavendra Rao Anata, kvm,
	linux-arm-kernel, Zenghui Yu, Alexandru Elisei, Suzuki K Poulose

On Fri, 18 Aug 2023 21:39:43 -0700, Reiji Watanabe wrote:
> This series fixes a couple of PMUver related handling of
> vPMU support.
> 
> On systems where the PMUVer is not uniform across all PEs,
> KVM currently does not advertise PMUv3 to the guest,
> even if userspace successfully runs KVM_ARM_VCPU_INIT with
> KVM_ARM_VCPU_PMU_V3.
> 
> [...]

Applied to next, thanks!

[1/4] KVM: arm64: PMU: Disallow vPMU on non-uniform PMUVer
      commit: ec3eb9ed6081bea8ebf603ff545dba127071b928
[2/4] KVM: arm64: PMU: Avoid inappropriate use of host's PMUVer
      commit: 335ca49ff31f145c0f08540614062197a334e064
[3/4] KVM: arm64: PMU: Don't advertise the STALL_SLOT event
      commit: 8c694f557fd80ca9815ddb1cf5de10d8bf168110
[4/4] KVM: arm64: PMU: Don't advertise STALL_SLOT_{FRONTEND,BACKEND}
      commit: 64b81000b60b70f10a5834023fe100902d9f7a57

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-08-20  8:45 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-19  4:39 [PATCH v3 0/4] KVM: arm64: PMU: Fix PMUver related handling of vPMU support Reiji Watanabe
2023-08-19  4:39 ` [PATCH v3 1/4] KVM: arm64: PMU: Disallow vPMU on non-uniform PMUVer Reiji Watanabe
2023-08-19  4:39 ` [PATCH v3 2/4] KVM: arm64: PMU: Avoid inappropriate use of host's PMUVer Reiji Watanabe
2023-08-19  4:39 ` [PATCH v3 3/4] KVM: arm64: PMU: Don't advertise the STALL_SLOT event Reiji Watanabe
2023-08-19  4:39 ` [PATCH v3 4/4] KVM: arm64: PMU: Don't advertise STALL_SLOT_{FRONTEND,BACKEND} Reiji Watanabe
2023-08-20  8:44 ` [PATCH v3 0/4] KVM: arm64: PMU: Fix PMUver related handling of vPMU support Marc Zyngier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).