Skip to contentSkip to conversation

KVM: arm64: PMU: Allow userspace to limit the number of PMCs on vCPU

PATCH Superseded Raghavendra Rao Ananta 4 participants 41 messages Aug 17, 2023 – Sep 18, 2023

Subsystems (5)
KVM Kselftest arm64 KVM arm64 +1 more subsystems

v5 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v5 cover KVM: arm64: PMU: Allow userspace to limit the number of PMCs on vCPU

Hello,

With permission from Reiji Watanabe <[email protected]>, the original author of the series, I'm posting the v5 with necessary alterations.

The goal of this series is to allow userspace to limit the number of PMU event counters on the vCPU. We need this to support migration across systems that implement different numbers of counters.

The number of PMU event counters is indicated in PMCR_EL0.N. For a vCPU with PMUv3 configured, its value will be the same as the current PE by default. Userspace can set PMCR_EL0.N for the vCPU to any value even with the current KVM using KVM_SET_ONE_REG. However, it is practically unsupported, as KVM resets PMCR_EL0.N to the host value on vCPU reset and some KVM code uses the host value to identify (un)implemented event counters on the vCPU.

This series will ensure that the PMCR_EL0.N value is preserved on vCPU reset and that KVM doesn't use the host value to identify (un)implemented event counters on the vCPU. This allows userspace to limit the number of the PMU event counters on the vCPU.

The series is based on v6.5-rc6.

Patch 1 adds a helper to set a PMU for the guest. This helper will make it easier for the following patches to add modify codes for that process.

Patch 2 makes the default PMU for the guest set on the first vCPU reset.

Patch 3 fixes reset_pmu_reg() to ensure that (RAZ) bits of PMCNTEN{SET,CLR}_EL0, PMINTEN{SET,CLR}_EL1, and PMOVS{SET,CLR}_EL1 corresponding to unimplemented event counters on the vCPU are reset to zero.

Patch 4 is a minor refactoring to use the default PMU register reset function for PMUSERENR_EL0 and PMCCFILTR_EL0.

Patch 5 simplifies the existing code that extracts PMCR_EL0.N by using FIELD_GET().

Patch 6 adds a helper to read vCPU's PMCR_EL0.

Patch 7 changes the code to use the guest's PMCR_EL0.N, instead of the PE's PMCR_EL0.N.

Patch 8 adds support userspace modifying PMCR_EL0.N.

Patch 9-12 adds a selftest to verify reading and writing PMU registers for implemented or unimplemented PMU event counters on the vCPU.

v5:
- Drop the patches (v4 3,4) related to PMU version fixes as it's now being handled in a separate series [1].
- Switch to config_lock, instead of kvm->lock, while configuring the guest PMU.
- Instead of continuing after a WARN_ON() for the return value of kvm_arm_set_vm_pmu() in kvm_arm_pmu_v3_set_pmu(), patch-1 now returns from the function immediately with the error code.
- Fix WARN_ON() logic in kvm_host_pmu_init() (patch v4 9/14).
- Instead of returning 0, return -ENODEV from the kvm_arm_set_vm_pmu() stub function.
- Do not define the PMEVN_CASE() and PMEVN_SWITCH() macros in the selftest code as they are now included in the imported arm_pmuv3.h header.
- Since the (initial) purpose of the selftest is to test the accessibility of the counter registers, remove the functional test at the end of test_access_pmc_regs(). It'll be added later in a separate series.
- Introduce additional helper functions (destroy_vpmu_vm(), PMC_ACC_TO_IDX()) in the selftest for ease of maintenance and debugging.

v4:
https://lore.kernel.org/all/[email protected]/
- Fix the selftest bug in patch 13 (Have test_access_pmc_regs() to specify pmc index for test_bitmap_pmu_regs() instead of bit-shifted value (Thank you Raghavendra for the reporting the issue!).

v3:
https://lore.kernel.org/all/[email protected]/
- Remove reset_pmu_reg(), and use reset_val() instead. [Marc]
- Fixed the initial value of PMCR_EL0.N on heterogeneous PMU systems. [Oliver]
- Fixed PMUVer issues on heterogeneous PMU systems.
- Fixed typos [Shaoqin]

v2:
https://lore.kernel.org/all/[email protected]/
- Added the sys_reg's set_user() handler for the PMCR_EL0 to disallow userspace to set PMCR_EL0.N for the vCPU to a value that is greater than the host value (and added a new test case for this behavior). [Oliver]
- Added to the commit log of the patch 2 that PMUSERENR_EL0 and PMCCFILTR_EL0 have UNKNOWN reset values.

v1:
https://lore.kernel.org/all/[email protected]/

Thank you.
Raghavendra

[1]:
https://lore.kernel.org/all/[email protected]/

Raghavendra Rao Ananta (1):
tools: Import arm_pmuv3.h

Reiji Watanabe (11):
KVM: arm64: PMU: Introduce a helper to set the guest's PMU
KVM: arm64: PMU: Set the default PMU for the guest on vCPU reset
KVM: arm64: PMU: Clear PM{C,I}NTEN{SET,CLR} and PMOVS{SET,CLR} on vCPU
reset
KVM: arm64: PMU: Don't define the sysreg reset() for
PM{USERENR,CCFILTR}_EL0
KVM: arm64: PMU: Simplify extracting PMCR_EL0.N
KVM: arm64: PMU: Add a helper to read a vCPU's PMCR_EL0
KVM: arm64: PMU: Set PMCR_EL0.N for vCPU based on the associated PMU
KVM: arm64: PMU: Allow userspace to limit PMCR_EL0.N for the guest
KVM: selftests: aarch64: Introduce vpmu_counter_access test
KVM: selftests: aarch64: vPMU register test for implemented counters
KVM: selftests: aarch64: vPMU register test for unimplemented counters

 arch/arm64/include/asm/kvm_host.h             |   6 +
 arch/arm64/kvm/arm.c                          |   3 +-
 arch/arm64/kvm/pmu-emul.c                     |  82 ++-
 arch/arm64/kvm/reset.c                        |  18 +-
 arch/arm64/kvm/sys_regs.c                     |  96 +--
 drivers/perf/arm_pmuv3.c                      |   3 +-
 include/kvm/arm_pmu.h                         |  12 +
 include/linux/perf/arm_pmuv3.h                |   2 +-
 tools/include/perf/arm_pmuv3.h                | 306 ++++++++++
 tools/testing/selftests/kvm/Makefile          |   1 +
 .../kvm/aarch64/vpmu_counter_access.c         | 568 ++++++++++++++++++
 .../selftests/kvm/include/aarch64/processor.h |   1 +
 12 files changed, 1028 insertions(+), 70 deletions(-)
 create mode 100644 tools/include/perf/arm_pmuv3.h
 create mode 100644 tools/testing/selftests/kvm/aarch64/vpmu_counter_access.c

base-commit: 2ccdd1b13c591d306f0401d98dedc4bdcd02b421

Signature · 1 line
2.41.0.694.ge786442a9b-goog
#1 lore raw

PATCH v5 1/12 KVM: arm64: PMU: Introduce a helper to set the guest's PMU

From: Reiji Watanabe <[email protected]>

Introduce a new helper function to set the guest's PMU (kvm->arch.arm_pmu), and use it when the guest's PMU needs to be set. This helper will make it easier for the following patches to modify the relevant code.

No functional change intended.

---
 arch/arm64/kvm/pmu-emul.c | 52 +++++++++++++++++++++++++++------------
 1 file changed, 36 insertions(+), 16 deletions(-)
arch/arm64/kvm/pmu-emul.c +36 -16
index 5606509724787..0ffd1efa90c07 100644
@@ -865,6 +865,32 @@ static bool pmu_irq_is_valid(struct kvm *kvm, int irq)
 	return true;
 }
 
+static int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
+{
+	lockdep_assert_held(&kvm->arch.config_lock);
+
+	if (!arm_pmu) {
+		/*
+		 * No PMU set, get the default one.
+		 *
+		 * The observant among you will notice that the supported_cpus
+		 * mask does not get updated for the default PMU even though it
+		 * is quite possible the selected instance supports only a
+		 * subset of cores in the system. This is intentional, and
+		 * upholds the preexisting behavior on heterogeneous systems
+		 * where vCPUs can be scheduled on any core but the guest
+		 * counters could stop working.
+		 */
+		arm_pmu = kvm_pmu_probe_armpmu();
+		if (!arm_pmu)
+			return -ENODEV;
+	}
+
+	kvm->arch.arm_pmu = arm_pmu;
+
+	return 0;
+}
+
 static int kvm_arm_pmu_v3_set_pmu(struct kvm_vcpu *vcpu, int pmu_id)
 {
 	struct kvm *kvm = vcpu->kvm;
@@ -884,9 +910,13 @@ static int kvm_arm_pmu_v3_set_pmu(struct kvm_vcpu *vcpu, int pmu_id)
 				break;
 			}
 
-			kvm->arch.arm_pmu = arm_pmu;
+			ret = kvm_arm_set_vm_pmu(kvm, arm_pmu);
+			if (ret) {
+				WARN_ON(ret);
+				break;
+			}
+
 			cpumask_copy(kvm->arch.supported_cpus, &arm_pmu->supported_cpus);
-			ret = 0;
 			break;
 		}
 	}
@@ -908,20 +938,10 @@ int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
 		return -EBUSY;
 
 	if (!kvm->arch.arm_pmu) {
-		/*
-		 * No PMU set, get the default one.
-		 *
-		 * The observant among you will notice that the supported_cpus
-		 * mask does not get updated for the default PMU even though it
-		 * is quite possible the selected instance supports only a
-		 * subset of cores in the system. This is intentional, and
-		 * upholds the preexisting behavior on heterogeneous systems
-		 * where vCPUs can be scheduled on any core but the guest
-		 * counters could stop working.
-		 */
-		kvm->arch.arm_pmu = kvm_pmu_probe_armpmu();
-		if (!kvm->arch.arm_pmu)
-			return -ENODEV;
+		int ret = kvm_arm_set_vm_pmu(kvm, NULL);
+
+		if (ret)
+			return ret;
 	}
 
 	switch (attr->attr) {
Signature · 1 line
2.41.0.694.ge786442a9b-goog
#2 lore raw

Re: [PATCH v5 01/12] KVM: arm64: PMU: Introduce a helper to set the guest's PMU

Hi Raghu,

Quoted 39 lines · Raghavendra Rao Ananta

From: Reiji Watanabe <[email protected]>

Introduce a new helper function to set the guest's PMU (kvm->arch.arm_pmu), and use it when the guest's PMU needs to be set. This helper will make it easier for the following patches to modify the relevant code.

No functional change intended.

---
 arch/arm64/kvm/pmu-emul.c | 52 +++++++++++++++++++++++++++------------
 1 file changed, 36 insertions(+), 16 deletions(-)
arch/arm64/kvm/pmu-emul.c +16 -0
index 5606509724787..0ffd1efa90c07 100644
@@ -865,6 +865,32 @@ static bool pmu_irq_is_valid(struct kvm *kvm, int irq)
 	return true;
 }
 
+static int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
+{
+	lockdep_assert_held(&kvm->arch.config_lock);
+
+	if (!arm_pmu) {
+		/*
+		 * No PMU set, get the default one.
+		 *
+		 * The observant among you will notice that the supported_cpus
+		 * mask does not get updated for the default PMU even though it
+		 * is quite possible the selected instance supports only a
+		 * subset of cores in the system. This is intentional, and
+		 * upholds the preexisting behavior on heterogeneous systems
+		 * where vCPUs can be scheduled on any core but the guest
+		 * counters could stop working.
+		 */

arch/arm64/kvm/pmu-emul.c static bool pmu_irq_is_valid(struct kvm *kvm, int irq) · line 893

Quoted 10 lines
arch/arm64/kvm/pmu-emul.c +10 -0
+		arm_pmu = kvm_pmu_probe_armpmu();
+		if (!arm_pmu)
+			return -ENODEV;
+	}
+
+	kvm->arch.arm_pmu = arm_pmu;
+
+	return 0;
+}
+

I'm not too big of a fan of adding the 'default' path to this helper. I'd prefer it if kvm_arm_set_vm_pmu() does all the necessary initialization for a valid pmu instance. You then avoid introducing unexpected error handling where it didn't exist before.

  static void kvm_arm_set_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
  {
  	lockdep_assert_held(&kvm->arch.config_lock);
	kvm->arch.arm_pmu = arm_pmu;

}

  /*
   * Blurb about default PMUs I'm too lazy to copy/paste
   */
  static int kvm_arm_set_default_pmu(struct kvm *kvm)
  {
  	struct arm_pmu *arm_pmu = kvm_pmu_probe_armpmu();
	if (!arm_pmu)
		return -ENODEV;

	kvm_arm_set_pmu(kvm, arm_pmu);
	return 0;

}

Signature · 2 lines
Thanks,
Oliver
#3 lore raw

Re: [PATCH v5 01/12] KVM: arm64: PMU: Introduce a helper to set the guest's PMU

Quoted 73 lines · Oliver Upton

Hi Raghu,

Quoted 39 lines · Raghavendra Rao Ananta

From: Reiji Watanabe <[email protected]>

Introduce a new helper function to set the guest's PMU (kvm->arch.arm_pmu), and use it when the guest's PMU needs to be set. This helper will make it easier for the following patches to modify the relevant code.

No functional change intended.

---
 arch/arm64/kvm/pmu-emul.c | 52 +++++++++++++++++++++++++++------------
 1 file changed, 36 insertions(+), 16 deletions(-)
arch/arm64/kvm/pmu-emul.c +16 -0
index 5606509724787..0ffd1efa90c07 100644
@@ -865,6 +865,32 @@ static bool pmu_irq_is_valid(struct kvm *kvm, int irq)
      return true;
 }
 
+static int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
+{
+     lockdep_assert_held(&kvm->arch.config_lock);
+
+     if (!arm_pmu) {
+             /*
+              * No PMU set, get the default one.
+              *
+              * The observant among you will notice that the supported_cpus
+              * mask does not get updated for the default PMU even though it
+              * is quite possible the selected instance supports only a
+              * subset of cores in the system. This is intentional, and
+              * upholds the preexisting behavior on heterogeneous systems
+              * where vCPUs can be scheduled on any core but the guest
+              * counters could stop working.
+              */

arch/arm64/kvm/pmu-emul.c static bool pmu_irq_is_valid(struct kvm *kvm, int irq) · line 893

Quoted 10 lines
arch/arm64/kvm/pmu-emul.c +10 -0
+             arm_pmu = kvm_pmu_probe_armpmu();
+             if (!arm_pmu)
+                     return -ENODEV;
+     }
+
+     kvm->arch.arm_pmu = arm_pmu;
+
+     return 0;
+}
+

I'm not too big of a fan of adding the 'default' path to this helper. I'd prefer it if kvm_arm_set_vm_pmu() does all the necessary initialization for a valid pmu instance. You then avoid introducing unexpected error handling where it didn't exist before.

  static void kvm_arm_set_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
  {
        lockdep_assert_held(&kvm->arch.config_lock);
        kvm->arch.arm_pmu = arm_pmu;

}

  /*
   * Blurb about default PMUs I'm too lazy to copy/paste
   */
  static int kvm_arm_set_default_pmu(struct kvm *kvm)
  {
        struct arm_pmu *arm_pmu = kvm_pmu_probe_armpmu();
Quoted 7 lines
        if (!arm_pmu)
                return -ENODEV;

        kvm_arm_set_pmu(kvm, arm_pmu);
        return 0;

}

Sounds good. We can adapt to your suggestion.

Thank you.
Raghavendra

Quoted 3 lines

--
Thanks,
Oliver

#4 lore raw

PATCH v5 2/12 KVM: arm64: PMU: Set the default PMU for the guest on vCPU reset

In reply to Raghavendra Rao Ananta

From: Reiji Watanabe <[email protected]>

The following patches will use the number of counters information from the arm_pmu and use this to set the PMCR.N for the guest during vCPU reset. However, since the guest is not associated with any arm_pmu until userspace configures the vPMU device attributes, and a reset can happen before this event, call kvm_arm_support_pmu_v3() just before doing the reset.

No functional change intended.

---
 arch/arm64/kvm/pmu-emul.c |  9 +--------
 arch/arm64/kvm/reset.c    | 18 +++++++++++++-----
 include/kvm/arm_pmu.h     |  6 ++++++
 3 files changed, 20 insertions(+), 13 deletions(-)
arch/arm64/kvm/pmu-emul.c +1 -8
index 0ffd1efa90c07..b87822024828a 100644
@@ -865,7 +865,7 @@ static bool pmu_irq_is_valid(struct kvm *kvm, int irq)
 	return true;
 }
 
-static int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
+int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
 {
 	lockdep_assert_held(&kvm->arch.config_lock);
 
@@ -937,13 +937,6 @@ int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
 	if (vcpu->arch.pmu.created)
 		return -EBUSY;
 
-	if (!kvm->arch.arm_pmu) {
-		int ret = kvm_arm_set_vm_pmu(kvm, NULL);
-
-		if (ret)
-			return ret;
-	}
-
 	switch (attr->attr) {
 	case KVM_ARM_VCPU_PMU_V3_IRQ: {
 		int __user *uaddr = (int __user *)(long)attr->addr;
arch/arm64/kvm/reset.c +13 -5
index bc8556b6f4590..4c20f1ccd0789 100644
@@ -206,6 +206,7 @@ static int kvm_vcpu_enable_ptrauth(struct kvm_vcpu *vcpu)
  */
 int kvm_reset_vcpu(struct kvm_vcpu *vcpu)
 {
+	struct kvm *kvm = vcpu->kvm;
 	struct vcpu_reset_state reset_state;
 	int ret;
 	bool loaded;
@@ -216,6 +217,18 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu)
 	vcpu->arch.reset_state.reset = false;
 	spin_unlock(&vcpu->arch.mp_state_lock);
 
+	/*
+	 * When the vCPU has a PMU, but no PMU is set for the guest
+	 * yet, set the default one.
+	 */
+	if (kvm_vcpu_has_pmu(vcpu) && unlikely(!kvm->arch.arm_pmu)) {
+		ret = -EINVAL;
+		if (kvm_arm_support_pmu_v3())
+			ret = kvm_arm_set_vm_pmu(kvm, NULL);
+		if (ret)
+			return ret;
+	}
+
 	/* Reset PMU outside of the non-preemptible section */
 	kvm_pmu_vcpu_reset(vcpu);
 
@@ -257,11 +270,6 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu)
 		} else {
 			pstate = VCPU_RESET_PSTATE_EL1;
 		}
-
-		if (kvm_vcpu_has_pmu(vcpu) && !kvm_arm_support_pmu_v3()) {
-			ret = -EINVAL;
-			goto out;
-		}
 		break;
 	}
 
include/kvm/arm_pmu.h +6 -0
index 847da6fc27139..66a2f8477641e 100644
@@ -100,6 +100,7 @@ void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu);
 })
 
 u8 kvm_arm_pmu_get_pmuver_limit(void);
+int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu);
 
 #else
 struct kvm_pmu {
@@ -172,6 +173,11 @@ static inline u8 kvm_arm_pmu_get_pmuver_limit(void)
 	return 0;
 }
 
+static inline int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
+{
+	return -ENODEV;
+}
+
 #endif
 
 #endif
Signature · 1 line
2.41.0.694.ge786442a9b-goog
#5 lore raw

Re: [PATCH v5 02/12] KVM: arm64: PMU: Set the default PMU for the guest on vCPU reset

kernel test robot noticed the following build warnings:

Hi Raghavendra,

kernel test robot noticed the following build warnings:

[auto build test WARNING on 2ccdd1b13c591d306f0401d98dedc4bdcd02b421]

url:    https://github.com/intel-lab-lkp/linux/commits/Raghavendra-Rao-Ananta/KVM-arm64-PMU-Introduce-a-helper-to-set-the-guest-s-PMU/20230817-083353
base:   2ccdd1b13c591d306f0401d98dedc4bdcd02b421
patch link:    https://lore.kernel.org/r/20230817003029.3073210-3-rananta%40google.com
patch subject: [PATCH v5 02/12] KVM: arm64: PMU: Set the default PMU for the guest on vCPU reset
config: arm64-randconfig-r032-20230817 (https://download.01.org/0day-ci/archive/20230817/[email protected]/config)
compiler: clang version 17.0.0 (https://github.com/llvm/llvm-project.git 4a5ac14ee968ff0ad5d2cc1ffa0299048db4c88a)
reproduce: (https://download.01.org/0day-ci/archive/20230817/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

In file included from arch/arm64/kernel/asm-offsets.c:16:
In file included from include/linux/kvm_host.h:45:
In file included from arch/arm64/include/asm/kvm_host.h:37:

>> include/kvm/arm_pmu.h:176:62: warning: declaration of 'struct arm_pmu' will not be visible outside of this function [-Wvisibility]

176 | static inline int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
| ^
1 warning generated.
--
In file included from arch/arm64/kernel/asm-offsets.c:16:
In file included from include/linux/kvm_host.h:45:
In file included from arch/arm64/include/asm/kvm_host.h:37:

>> include/kvm/arm_pmu.h:176:62: warning: declaration of 'struct arm_pmu' will not be visible outside of this function [-Wvisibility]

176 | static inline int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
| ^
1 warning generated.

vim +176 include/kvm/arm_pmu.h

175

 > 176	static inline int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)

177 {
178 return -ENODEV;
179 }
180

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#6 lore raw

Re: [PATCH v5 02/12] KVM: arm64: PMU: Set the default PMU for the guest on vCPU reset

In reply to Raghavendra Rao Ananta
kernel test robot noticed the following build warnings:

Hi Raghavendra,

kernel test robot noticed the following build warnings:

[auto build test WARNING on 2ccdd1b13c591d306f0401d98dedc4bdcd02b421]

url:    https://github.com/intel-lab-lkp/linux/commits/Raghavendra-Rao-Ananta/KVM-arm64-PMU-Introduce-a-helper-to-set-the-guest-s-PMU/20230817-083353
base:   2ccdd1b13c591d306f0401d98dedc4bdcd02b421
patch link:    https://lore.kernel.org/r/20230817003029.3073210-3-rananta%40google.com
patch subject: [PATCH v5 02/12] KVM: arm64: PMU: Set the default PMU for the guest on vCPU reset
config: arm64-randconfig-r024-20230817 (https://download.01.org/0day-ci/archive/20230817/[email protected]/config)
compiler: aarch64-linux-gcc (GCC) 12.3.0
reproduce: (https://download.01.org/0day-ci/archive/20230817/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

In file included from arch/arm64/include/asm/kvm_host.h:37,

                    from include/linux/kvm_host.h:45,
                    from arch/arm64/kernel/asm-offsets.c:16:
>> include/kvm/arm_pmu.h:176:62: warning: 'struct arm_pmu' declared inside parameter list will not be visible outside of this definition or declaration

176 | static inline int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
| ^~~~~~~
--
In file included from arch/arm64/include/asm/kvm_host.h:37,

                    from include/linux/kvm_host.h:45,
                    from arch/arm64/kernel/asm-offsets.c:16:
>> include/kvm/arm_pmu.h:176:62: warning: 'struct arm_pmu' declared inside parameter list will not be visible outside of this definition or declaration

176 | static inline int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
| ^~~~~~~

vim +176 include/kvm/arm_pmu.h

175

 > 176	static inline int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)

177 {
178 return -ENODEV;
179 }
180

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#7 lore raw

Re: [PATCH v5 02/12] KVM: arm64: PMU: Set the default PMU for the guest on vCPU reset

In reply to Raghavendra Rao Ananta
Quoted 10 lines · Raghavendra Rao Ananta

From: Reiji Watanabe <[email protected]>

The following patches will use the number of counters information from the arm_pmu and use this to set the PMCR.N for the guest during vCPU reset. However, since the guest is not associated with any arm_pmu until userspace configures the vPMU device attributes, and a reset can happen before this event, call kvm_arm_support_pmu_v3() just before doing the reset.

No functional change intended.

But there absolutely is a functional change here, and user visible at that. KVM_ARM_VCPU_INIT ioctls can now fail with -ENODEV, which is not part of the documented errors for the interface.

Quoted 53 lines
---
 arch/arm64/kvm/pmu-emul.c |  9 +--------
 arch/arm64/kvm/reset.c    | 18 +++++++++++++-----
 include/kvm/arm_pmu.h     |  6 ++++++
 3 files changed, 20 insertions(+), 13 deletions(-)
arch/arm64/kvm/pmu-emul.c +1 -8
index 0ffd1efa90c07..b87822024828a 100644
@@ -865,7 +865,7 @@ static bool pmu_irq_is_valid(struct kvm *kvm, int irq)
 	return true;
 }
 
-static int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
+int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
 {
 	lockdep_assert_held(&kvm->arch.config_lock);
 
@@ -937,13 +937,6 @@ int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
 	if (vcpu->arch.pmu.created)
 		return -EBUSY;
 
-	if (!kvm->arch.arm_pmu) {
-		int ret = kvm_arm_set_vm_pmu(kvm, NULL);
-
-		if (ret)
-			return ret;
-	}
-
 	switch (attr->attr) {
 	case KVM_ARM_VCPU_PMU_V3_IRQ: {
 		int __user *uaddr = (int __user *)(long)attr->addr;
arch/arm64/kvm/reset.c +3 -0
index bc8556b6f4590..4c20f1ccd0789 100644
@@ -206,6 +206,7 @@ static int kvm_vcpu_enable_ptrauth(struct kvm_vcpu *vcpu)
  */
 int kvm_reset_vcpu(struct kvm_vcpu *vcpu)
 {
+	struct kvm *kvm = vcpu->kvm;
 	struct vcpu_reset_state reset_state;
 	int ret;
 	bool loaded;
@@ -216,6 +217,18 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu)
 	vcpu->arch.reset_state.reset = false;
 	spin_unlock(&vcpu->arch.mp_state_lock);
 
+	/*
+	 * When the vCPU has a PMU, but no PMU is set for the guest

arch/arm64/kvm/reset.c int kvm_reset_vcpu(struct kvm_vcpu *vcpu) · line 231

Quoted 10 lines
arch/arm64/kvm/reset.c +10 -0
+	 * yet, set the default one.
+	 */
+	if (kvm_vcpu_has_pmu(vcpu) && unlikely(!kvm->arch.arm_pmu)) {
+		ret = -EINVAL;
+		if (kvm_arm_support_pmu_v3())
+			ret = kvm_arm_set_vm_pmu(kvm, NULL);
+		if (ret)
+			return ret;
+	}
+

On top of my prior suggestion w.r.t. the default PMU helper, I'd rather see this block look like:

	if (kvm_vcpu_has_pmu(vcpu)) {
		if (!kvm_arm_support_pmu_v3())
			return -EINVAL;
		/*
		 * When the vCPU has a PMU but no PMU is set for the
		 * guest yet, set the default one.
		 */
		if (unlikely(!kvm->arch.arm_pmu) && kvm_set_default_pmu(kvm))
			return -EINVAL;
	}

This would eliminate the possibility of returning ENODEV to userspace where we shouldn't.

Signature · 2 lines
Thanks,
Oliver
#8 lore raw

Re: [PATCH v5 02/12] KVM: arm64: PMU: Set the default PMU for the guest on vCPU reset

Quoted 95 lines · Oliver Upton
Quoted 10 lines · Raghavendra Rao Ananta

From: Reiji Watanabe <[email protected]>

The following patches will use the number of counters information from the arm_pmu and use this to set the PMCR.N for the guest during vCPU reset. However, since the guest is not associated with any arm_pmu until userspace configures the vPMU device attributes, and a reset can happen before this event, call kvm_arm_support_pmu_v3() just before doing the reset.

No functional change intended.

But there absolutely is a functional change here, and user visible at that. KVM_ARM_VCPU_INIT ioctls can now fail with -ENODEV, which is not part of the documented errors for the interface.

Quoted 53 lines
---
 arch/arm64/kvm/pmu-emul.c |  9 +--------
 arch/arm64/kvm/reset.c    | 18 +++++++++++++-----
 include/kvm/arm_pmu.h     |  6 ++++++
 3 files changed, 20 insertions(+), 13 deletions(-)
arch/arm64/kvm/pmu-emul.c +1 -8
index 0ffd1efa90c07..b87822024828a 100644
@@ -865,7 +865,7 @@ static bool pmu_irq_is_valid(struct kvm *kvm, int irq)
      return true;
 }
 
-static int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
+int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
 {
      lockdep_assert_held(&kvm->arch.config_lock);
 
@@ -937,13 +937,6 @@ int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
      if (vcpu->arch.pmu.created)
              return -EBUSY;
 
-     if (!kvm->arch.arm_pmu) {
-             int ret = kvm_arm_set_vm_pmu(kvm, NULL);
-
-             if (ret)
-                     return ret;
-     }
-
      switch (attr->attr) {
      case KVM_ARM_VCPU_PMU_V3_IRQ: {
              int __user *uaddr = (int __user *)(long)attr->addr;
arch/arm64/kvm/reset.c +3 -0
index bc8556b6f4590..4c20f1ccd0789 100644
@@ -206,6 +206,7 @@ static int kvm_vcpu_enable_ptrauth(struct kvm_vcpu *vcpu)
  */
 int kvm_reset_vcpu(struct kvm_vcpu *vcpu)
 {
+     struct kvm *kvm = vcpu->kvm;
      struct vcpu_reset_state reset_state;
      int ret;
      bool loaded;
@@ -216,6 +217,18 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu)
      vcpu->arch.reset_state.reset = false;
      spin_unlock(&vcpu->arch.mp_state_lock);
 
+     /*
+      * When the vCPU has a PMU, but no PMU is set for the guest

arch/arm64/kvm/reset.c int kvm_reset_vcpu(struct kvm_vcpu *vcpu) · line 231

Quoted 10 lines
arch/arm64/kvm/reset.c +10 -0
+      * yet, set the default one.
+      */
+     if (kvm_vcpu_has_pmu(vcpu) && unlikely(!kvm->arch.arm_pmu)) {
+             ret = -EINVAL;
+             if (kvm_arm_support_pmu_v3())
+                     ret = kvm_arm_set_vm_pmu(kvm, NULL);
+             if (ret)
+                     return ret;
+     }
+

On top of my prior suggestion w.r.t. the default PMU helper, I'd rather see this block look like:

        if (kvm_vcpu_has_pmu(vcpu)) {
                if (!kvm_arm_support_pmu_v3())
                        return -EINVAL;
                /*
                 * When the vCPU has a PMU but no PMU is set for the
                 * guest yet, set the default one.
                 */
                if (unlikely(!kvm->arch.arm_pmu) && kvm_set_default_pmu(kvm))
                        return -EINVAL;
        }
Quoted 3 lines

This would eliminate the possibility of returning ENODEV to userspace where we shouldn't.

I understand that we'll be breaking the API contract and userspace may have to adapt to this change, but is it not acceptable to document and return ENODEV, since ENODEV may offer more clarity to userspace as to why the ioctl failed? In general, do we never extend the APIs?

Thank you.
Raghavendra

Quoted 3 lines

--
Thanks,
Oliver

#9 lore raw

Re: [PATCH v5 02/12] KVM: arm64: PMU: Set the default PMU for the guest on vCPU reset

Quoted 1 line · Raghavendra Rao Ananta

On Fri, Sep 15, 2023 at 12:33 PM Oliver Upton <[email protected]> wrote:

[...]

Quoted 7 lines
Quoted 3 lines

This would eliminate the possibility of returning ENODEV to userspace where we shouldn't.

I understand that we'll be breaking the API contract and userspace may have to adapt to this change, but is it not acceptable to document and return ENODEV, since ENODEV may offer more clarity to userspace as to why the ioctl failed? In general, do we never extend the APIs?

Yes, we extend the existing interfaces all the time, but we almost always require user opt in for user-visible changes in behavior. Look at the way arm64_check_features() is handled -- we hide the 'detailed' error and return EINVAL due to UAPI.

--
Thanks,
Oliver

#10 lore raw

Re: [PATCH v5 02/12] KVM: arm64: PMU: Set the default PMU for the guest on vCPU reset

Quoted 14 lines · Oliver Upton
Quoted 1 line · Raghavendra Rao Ananta

On Fri, Sep 15, 2023 at 12:33 PM Oliver Upton <[email protected]> wrote:

[...]

Quoted 7 lines
Quoted 3 lines

This would eliminate the possibility of returning ENODEV to userspace where we shouldn't.

I understand that we'll be breaking the API contract and userspace may have to adapt to this change, but is it not acceptable to document and return ENODEV, since ENODEV may offer more clarity to userspace as to why the ioctl failed? In general, do we never extend the APIs?

Quoted 5 lines

Yes, we extend the existing interfaces all the time, but we almost always require user opt in for user-visible changes in behavior. Look at the way arm64_check_features() is handled -- we hide the 'detailed' error and return EINVAL due to UAPI.

Got it. Let's return EINVAL then. Thanks!

- Raghavendra

Quoted 3 lines

--
Thanks,
Oliver

#11 lore raw

PATCH v5 3/12 KVM: arm64: PMU: Clear PM{C,I}NTEN{SET,CLR} and PMOVS{SET,CLR} on vCPU reset

In reply to Raghavendra Rao Ananta

From: Reiji Watanabe <[email protected]>

On vCPU reset, PMCNTEN{SET,CLR}_EL0, PMINTEN{SET,CLR}_EL1, and PMOVS{SET,CLR}_EL1 for a vCPU are reset by reset_pmu_reg(). This function clears RAZ bits of those registers corresponding to unimplemented event counters on the vCPU, and sets bits corresponding to implemented event counters to a predefined pseudo UNKNOWN value (some bits are set to 1).

The function identifies (un)implemented event counters on the vCPU based on the PMCR_EL0.N value on the host. Using the host value for this would be problematic when KVM supports letting userspace set PMCR_EL0.N to a value different from the host value (some of the RAZ bits of those registers could end up being set to 1).

Fix this by clearing the registers so that it can ensure that all the RAZ bits are cleared even when the PMCR_EL0.N value for the vCPU is different from the host value. Use reset_val() to do this instead of fixing reset_pmu_reg(), and remove reset_pmu_reg(), as it is no longer used.

---
 arch/arm64/kvm/sys_regs.c | 21 +--------------------
 1 file changed, 1 insertion(+), 20 deletions(-)
arch/arm64/kvm/sys_regs.c +1 -20
index 2ca2973abe66f..9d3d44d165eed 100644
@@ -717,25 +717,6 @@ static unsigned int pmu_visibility(const struct kvm_vcpu *vcpu,
 	return REG_HIDDEN;
 }
 
-static u64 reset_pmu_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
-{
-	u64 n, mask = BIT(ARMV8_PMU_CYCLE_IDX);
-
-	/* No PMU available, any PMU reg may UNDEF... */
-	if (!kvm_arm_support_pmu_v3())
-		return 0;
-
-	n = read_sysreg(pmcr_el0) >> ARMV8_PMU_PMCR_N_SHIFT;
-	n &= ARMV8_PMU_PMCR_N_MASK;
-	if (n)
-		mask |= GENMASK(n - 1, 0);
-
-	reset_unknown(vcpu, r);
-	__vcpu_sys_reg(vcpu, r->reg) &= mask;
-
-	return __vcpu_sys_reg(vcpu, r->reg);
-}
-
 static u64 reset_pmevcntr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
 {
 	reset_unknown(vcpu, r);
@@ -1115,7 +1096,7 @@ static bool access_pmuserenr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
 	  trap_wcr, reset_wcr, 0, 0,  get_wcr, set_wcr }
 
 #define PMU_SYS_REG(name)						\
-	SYS_DESC(SYS_##name), .reset = reset_pmu_reg,			\
+	SYS_DESC(SYS_##name), .reset = reset_val,			\
 	.visibility = pmu_visibility
 
 /* Macro to expand the PMEVCNTRn_EL0 register */
Signature · 1 line
2.41.0.694.ge786442a9b-goog
#12 lore raw

PATCH v5 4/12 KVM: arm64: PMU: Don't define the sysreg reset() for PM{USERENR,CCFILTR}_EL0

In reply to Raghavendra Rao Ananta

From: Reiji Watanabe <[email protected]>

The default reset function for PMU registers (defined by PMU_SYS_REG) now simply clears a specified register. Use the default one for PMUSERENR_EL0 and PMCCFILTR_EL0, as KVM currently clears those registers on vCPU reset (NOTE: All non-RES0 fields of those registers have UNKNOWN reset values, and the same fields of their AArch32 registers have 0 reset values).

No functional change intended.

---
 arch/arm64/kvm/sys_regs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/kvm/sys_regs.c +2 -2
index 9d3d44d165eed..39e9248c935e7 100644
@@ -2178,7 +2178,7 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 	 * in 32bit mode. Here we choose to reset it as zero for consistency.
 	 */
 	{ PMU_SYS_REG(PMUSERENR_EL0), .access = access_pmuserenr,
-	  .reset = reset_val, .reg = PMUSERENR_EL0, .val = 0 },
+	  .reg = PMUSERENR_EL0, },
 	{ PMU_SYS_REG(PMOVSSET_EL0),
 	  .access = access_pmovs, .reg = PMOVSSET_EL0 },
 
@@ -2336,7 +2336,7 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 	 * in 32bit mode. Here we choose to reset it as zero for consistency.
 	 */
 	{ PMU_SYS_REG(PMCCFILTR_EL0), .access = access_pmu_evtyper,
-	  .reset = reset_val, .reg = PMCCFILTR_EL0, .val = 0 },
+	  .reg = PMCCFILTR_EL0, },
 
 	EL2_REG(VPIDR_EL2, access_rw, reset_unknown, 0),
 	EL2_REG(VMPIDR_EL2, access_rw, reset_unknown, 0),
Signature · 1 line
2.41.0.694.ge786442a9b-goog
#13 lore raw

PATCH v5 5/12 KVM: arm64: PMU: Simplify extracting PMCR_EL0.N

In reply to Raghavendra Rao Ananta

From: Reiji Watanabe <[email protected]>

Some code extracts PMCR_EL0.N using ARMV8_PMU_PMCR_N_SHIFT and ARMV8_PMU_PMCR_N_MASK. Define ARMV8_PMU_PMCR_N (0x1f << 11), and simplify those codes using FIELD_GET() and/or ARMV8_PMU_PMCR_N. The following patches will also use these macros to extract PMCR_EL0.N.

No functional change intended.

---
 arch/arm64/kvm/pmu-emul.c      | 3 +--
 arch/arm64/kvm/sys_regs.c      | 7 +++----
 drivers/perf/arm_pmuv3.c       | 3 +--
 include/linux/perf/arm_pmuv3.h | 2 +-
 4 files changed, 6 insertions(+), 9 deletions(-)
arch/arm64/kvm/pmu-emul.c +1 -2
index b87822024828a..f7b5fa16341ad 100644
@@ -245,9 +245,8 @@ void kvm_pmu_vcpu_destroy(struct kvm_vcpu *vcpu)
 
 u64 kvm_pmu_valid_counter_mask(struct kvm_vcpu *vcpu)
 {
-	u64 val = __vcpu_sys_reg(vcpu, PMCR_EL0) >> ARMV8_PMU_PMCR_N_SHIFT;
+	u64 val = FIELD_GET(ARMV8_PMU_PMCR_N, __vcpu_sys_reg(vcpu, PMCR_EL0));
 
-	val &= ARMV8_PMU_PMCR_N_MASK;
 	if (val == 0)
 		return BIT(ARMV8_PMU_CYCLE_IDX);
 	else
arch/arm64/kvm/sys_regs.c +3 -4
index 39e9248c935e7..30108f09e088b 100644
@@ -750,7 +750,7 @@ static u64 reset_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
 		return 0;
 
 	/* Only preserve PMCR_EL0.N, and reset the rest to 0 */
-	pmcr = read_sysreg(pmcr_el0) & (ARMV8_PMU_PMCR_N_MASK << ARMV8_PMU_PMCR_N_SHIFT);
+	pmcr = read_sysreg(pmcr_el0) & ARMV8_PMU_PMCR_N;
 	if (!kvm_supports_32bit_el0())
 		pmcr |= ARMV8_PMU_PMCR_LC;
 
@@ -858,10 +858,9 @@ static bool access_pmceid(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
 
 static bool pmu_counter_idx_valid(struct kvm_vcpu *vcpu, u64 idx)
 {
-	u64 pmcr, val;
+	u64 val;
 
-	pmcr = __vcpu_sys_reg(vcpu, PMCR_EL0);
-	val = (pmcr >> ARMV8_PMU_PMCR_N_SHIFT) & ARMV8_PMU_PMCR_N_MASK;
+	val = FIELD_GET(ARMV8_PMU_PMCR_N, __vcpu_sys_reg(vcpu, PMCR_EL0));
 	if (idx >= val && idx != ARMV8_PMU_CYCLE_IDX) {
 		kvm_inject_undefined(vcpu);
 		return false;
drivers/perf/arm_pmuv3.c +1 -2
index 08b3a1bf0ef62..7618b0adc0b8c 100644
@@ -1128,8 +1128,7 @@ static void __armv8pmu_probe_pmu(void *info)
 	probe->present = true;
 
 	/* Read the nb of CNTx counters supported from PMNC */
-	cpu_pmu->num_events = (armv8pmu_pmcr_read() >> ARMV8_PMU_PMCR_N_SHIFT)
-		& ARMV8_PMU_PMCR_N_MASK;
+	cpu_pmu->num_events = FIELD_GET(ARMV8_PMU_PMCR_N, armv8pmu_pmcr_read());
 
 	/* Add the CPU cycles counter */
 	cpu_pmu->num_events += 1;
include/linux/perf/arm_pmuv3.h +1 -1
index e3899bd77f5cc..ecbcf3f93560c 100644
@@ -216,7 +216,7 @@
 #define ARMV8_PMU_PMCR_LC	(1 << 6) /* Overflow on 64 bit cycle counter */
 #define ARMV8_PMU_PMCR_LP	(1 << 7) /* Long event counter enable */
 #define ARMV8_PMU_PMCR_N_SHIFT	11  /* Number of counters supported */
-#define ARMV8_PMU_PMCR_N_MASK	0x1f
+#define	ARMV8_PMU_PMCR_N	(0x1f << ARMV8_PMU_PMCR_N_SHIFT)
 #define ARMV8_PMU_PMCR_MASK	0xff    /* Mask for writable bits */
 
 /*
Signature · 1 line
2.41.0.694.ge786442a9b-goog
#14 lore raw

Re: [PATCH v5 05/12] KVM: arm64: PMU: Simplify extracting PMCR_EL0.N

kernel test robot noticed the following build errors:

Hi Raghavendra,

kernel test robot noticed the following build errors:

[auto build test ERROR on 2ccdd1b13c591d306f0401d98dedc4bdcd02b421]

url:    https://github.com/intel-lab-lkp/linux/commits/Raghavendra-Rao-Ananta/KVM-arm64-PMU-Introduce-a-helper-to-set-the-guest-s-PMU/20230817-083353
base:   2ccdd1b13c591d306f0401d98dedc4bdcd02b421
patch link:    https://lore.kernel.org/r/20230817003029.3073210-6-rananta%40google.com
patch subject: [PATCH v5 05/12] KVM: arm64: PMU: Simplify extracting PMCR_EL0.N
config: arm-randconfig-r046-20230817 (https://download.01.org/0day-ci/archive/20230817/[email protected]/config)
compiler: clang version 17.0.0 (https://github.com/llvm/llvm-project.git 4a5ac14ee968ff0ad5d2cc1ffa0299048db4c88a)
reproduce: (https://download.01.org/0day-ci/archive/20230817/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

| ^~~~~~
drivers/perf/arm_pmuv3.c:141:2: note: previous initialization is here

     141 |         PERF_CACHE_MAP_ALL_UNSUPPORTED,
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/perf/arm_pmu.h:45:31: note: expanded from macro 'PERF_CACHE_MAP_ALL_UNSUPPORTED'
      45 |                 [0 ... C(RESULT_MAX) - 1] = CACHE_OP_UNSUPPORTED,       \
         |                                             ^~~~~~~~~~~~~~~~~~~~
   include/linux/perf/arm_pmu.h:37:31: note: expanded from macro 'CACHE_OP_UNSUPPORTED'
      37 | #define CACHE_OP_UNSUPPORTED            0xFFFF
         |                                         ^~~~~~
   drivers/perf/arm_pmuv3.c:148:44: warning: initializer overrides prior initialization of this subobject [-Winitializer-overrides]
     148 |         [C(DTLB)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_RD,
         |                                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/perf/arm_pmuv3.h:133:44: note: expanded from macro 'ARMV8_IMPDEF_PERFCTR_L1D_TLB_RD'
     133 | #define ARMV8_IMPDEF_PERFCTR_L1D_TLB_RD                         0x004E
         |                                                                 ^~~~~~
   drivers/perf/arm_pmuv3.c:141:2: note: previous initialization is here
     141 |         PERF_CACHE_MAP_ALL_UNSUPPORTED,
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/perf/arm_pmu.h:45:31: note: expanded from macro 'PERF_CACHE_MAP_ALL_UNSUPPORTED'
      45 |                 [0 ... C(RESULT_MAX) - 1] = CACHE_OP_UNSUPPORTED,       \
         |                                             ^~~~~~~~~~~~~~~~~~~~
   include/linux/perf/arm_pmu.h:37:31: note: expanded from macro 'CACHE_OP_UNSUPPORTED'
      37 | #define CACHE_OP_UNSUPPORTED            0xFFFF
         |                                         ^~~~~~
   drivers/perf/arm_pmuv3.c:149:45: warning: initializer overrides prior initialization of this subobject [-Winitializer-overrides]
     149 |         [C(DTLB)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_WR,
         |                                                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/perf/arm_pmuv3.h:134:44: note: expanded from macro 'ARMV8_IMPDEF_PERFCTR_L1D_TLB_WR'
     134 | #define ARMV8_IMPDEF_PERFCTR_L1D_TLB_WR                         0x004F
         |                                                                 ^~~~~~
   drivers/perf/arm_pmuv3.c:141:2: note: previous initialization is here
     141 |         PERF_CACHE_MAP_ALL_UNSUPPORTED,
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/perf/arm_pmu.h:45:31: note: expanded from macro 'PERF_CACHE_MAP_ALL_UNSUPPORTED'
      45 |                 [0 ... C(RESULT_MAX) - 1] = CACHE_OP_UNSUPPORTED,       \
         |                                             ^~~~~~~~~~~~~~~~~~~~
   include/linux/perf/arm_pmu.h:37:31: note: expanded from macro 'CACHE_OP_UNSUPPORTED'
      37 | #define CACHE_OP_UNSUPPORTED            0xFFFF
         |                                         ^~~~~~
   drivers/perf/arm_pmuv3.c:150:42: warning: initializer overrides prior initialization of this subobject [-Winitializer-overrides]
     150 |         [C(DTLB)][C(OP_READ)][C(RESULT_MISS)]   = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_RD,
         |                                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/perf/arm_pmuv3.h:131:50: note: expanded from macro 'ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_RD'
     131 | #define ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_RD                  0x004C
         |                                                                 ^~~~~~
   drivers/perf/arm_pmuv3.c:141:2: note: previous initialization is here
     141 |         PERF_CACHE_MAP_ALL_UNSUPPORTED,
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/perf/arm_pmu.h:45:31: note: expanded from macro 'PERF_CACHE_MAP_ALL_UNSUPPORTED'
      45 |                 [0 ... C(RESULT_MAX) - 1] = CACHE_OP_UNSUPPORTED,       \
         |                                             ^~~~~~~~~~~~~~~~~~~~
   include/linux/perf/arm_pmu.h:37:31: note: expanded from macro 'CACHE_OP_UNSUPPORTED'
      37 | #define CACHE_OP_UNSUPPORTED            0xFFFF
         |                                         ^~~~~~
   drivers/perf/arm_pmuv3.c:151:43: warning: initializer overrides prior initialization of this subobject [-Winitializer-overrides]
     151 |         [C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)]  = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_WR,
         |                                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/perf/arm_pmuv3.h:132:50: note: expanded from macro 'ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_WR'
     132 | #define ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_WR                  0x004D
         |                                                                 ^~~~~~
   drivers/perf/arm_pmuv3.c:141:2: note: previous initialization is here
     141 |         PERF_CACHE_MAP_ALL_UNSUPPORTED,
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/perf/arm_pmu.h:45:31: note: expanded from macro 'PERF_CACHE_MAP_ALL_UNSUPPORTED'
      45 |                 [0 ... C(RESULT_MAX) - 1] = CACHE_OP_UNSUPPORTED,       \
         |                                             ^~~~~~~~~~~~~~~~~~~~
   include/linux/perf/arm_pmu.h:37:31: note: expanded from macro 'CACHE_OP_UNSUPPORTED'
      37 | #define CACHE_OP_UNSUPPORTED            0xFFFF
         |                                         ^~~~~~
   drivers/perf/arm_pmuv3.c:153:44: warning: initializer overrides prior initialization of this subobject [-Winitializer-overrides]
     153 |         [C(NODE)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_RD,
         |                                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/perf/arm_pmuv3.h:148:46: note: expanded from macro 'ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_RD'
     148 | #define ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_RD                      0x0060
         |                                                                 ^~~~~~
   drivers/perf/arm_pmuv3.c:141:2: note: previous initialization is here
     141 |         PERF_CACHE_MAP_ALL_UNSUPPORTED,
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/perf/arm_pmu.h:45:31: note: expanded from macro 'PERF_CACHE_MAP_ALL_UNSUPPORTED'
      45 |                 [0 ... C(RESULT_MAX) - 1] = CACHE_OP_UNSUPPORTED,       \
         |                                             ^~~~~~~~~~~~~~~~~~~~
   include/linux/perf/arm_pmu.h:37:31: note: expanded from macro 'CACHE_OP_UNSUPPORTED'
      37 | #define CACHE_OP_UNSUPPORTED            0xFFFF
         |                                         ^~~~~~
   drivers/perf/arm_pmuv3.c:154:45: warning: initializer overrides prior initialization of this subobject [-Winitializer-overrides]
     154 |         [C(NODE)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_WR,
         |                                                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/perf/arm_pmuv3.h:149:46: note: expanded from macro 'ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_WR'
     149 | #define ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_WR                      0x0061
         |                                                                 ^~~~~~
   drivers/perf/arm_pmuv3.c:141:2: note: previous initialization is here
     141 |         PERF_CACHE_MAP_ALL_UNSUPPORTED,
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/perf/arm_pmu.h:45:31: note: expanded from macro 'PERF_CACHE_MAP_ALL_UNSUPPORTED'
      45 |                 [0 ... C(RESULT_MAX) - 1] = CACHE_OP_UNSUPPORTED,       \
         |                                             ^~~~~~~~~~~~~~~~~~~~
   include/linux/perf/arm_pmu.h:37:31: note: expanded from macro 'CACHE_OP_UNSUPPORTED'
      37 | #define CACHE_OP_UNSUPPORTED            0xFFFF
         |                                         ^~~~~~
>> drivers/perf/arm_pmuv3.c:1131:24: error: call to undeclared function 'FIELD_GET'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
    1131 |         cpu_pmu->num_events = FIELD_GET(ARMV8_PMU_PMCR_N, armv8pmu_pmcr_read());
         |                               ^

55 warnings and 1 error generated.

vim +/FIELD_GET +1131 drivers/perf/arm_pmuv3.c

1114
1115 static void __armv8pmu_probe_pmu(void *info)
1116 {
1117 struct armv8pmu_probe_info *probe = info;
1118 struct arm_pmu *cpu_pmu = probe->pmu;
1119 u64 pmceid_raw[2];
1120 u32 pmceid[2];
1121 int pmuver;
1122
1123 pmuver = read_pmuver();
1124 if (!pmuv3_implemented(pmuver))
1125 return;
1126
1127 cpu_pmu->pmuver = pmuver;
1128 probe->present = true;
1129
1130 /* Read the nb of CNTx counters supported from PMNC */

> 1131		cpu_pmu->num_events = FIELD_GET(ARMV8_PMU_PMCR_N, armv8pmu_pmcr_read());

1132
1133 /* Add the CPU cycles counter */
1134 cpu_pmu->num_events += 1;
1135
1136 pmceid[0] = pmceid_raw[0] = read_pmceid0();
1137 pmceid[1] = pmceid_raw[1] = read_pmceid1();
1138
1139 bitmap_from_arr32(cpu_pmu->pmceid_bitmap,
1140 pmceid, ARMV8_PMUV3_MAX_COMMON_EVENTS);
1141
1142 pmceid[0] = pmceid_raw[0] >> 32;
1143 pmceid[1] = pmceid_raw[1] >> 32;
1144
1145 bitmap_from_arr32(cpu_pmu->pmceid_ext_bitmap,
1146 pmceid, ARMV8_PMUV3_MAX_COMMON_EVENTS);
1147
1148 /* store PMMIR register for sysfs */
1149 if (is_pmuv3p4(pmuver) && (pmceid_raw[1] & BIT(31)))
1150 cpu_pmu->reg_pmmir = read_pmmir();
1151 else
1152 cpu_pmu->reg_pmmir = 0;
1153 }
1154

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#15 lore raw

Re: [PATCH v5 05/12] KVM: arm64: PMU: Simplify extracting PMCR_EL0.N

In reply to Raghavendra Rao Ananta

+cc will, rutland

Hi Raghu,

Please make sure you cc the right folks for changes that poke multiple subsystems.

The diff looks OK, but I'm somewhat dubious of the need for this change in the context of what you're trying to accomplish for KVM. I'd prefer we either leave the existing definition/usage intact or rework *all* of the PMUv3 masks to be of the shifted variety.

Quoted 6 lines · Raghavendra Rao Ananta

From: Reiji Watanabe <[email protected]>

Some code extracts PMCR_EL0.N using ARMV8_PMU_PMCR_N_SHIFT and ARMV8_PMU_PMCR_N_MASK. Define ARMV8_PMU_PMCR_N (0x1f << 11), and simplify those codes using FIELD_GET() and/or ARMV8_PMU_PMCR_N. The following patches will also use these macros to extract PMCR_EL0.N.

Changelog is a bit wordy:

Define a shifted mask for accessing PMCR_EL0.N amenable to the use of bitfield accessors and convert the existing, open-coded mask shifts to the new definition.

Quoted 83 lines

No functional change intended.

---
 arch/arm64/kvm/pmu-emul.c      | 3 +--
 arch/arm64/kvm/sys_regs.c      | 7 +++----
 drivers/perf/arm_pmuv3.c       | 3 +--
 include/linux/perf/arm_pmuv3.h | 2 +-
 4 files changed, 6 insertions(+), 9 deletions(-)
arch/arm64/kvm/pmu-emul.c +1 -2
index b87822024828a..f7b5fa16341ad 100644
@@ -245,9 +245,8 @@ void kvm_pmu_vcpu_destroy(struct kvm_vcpu *vcpu)
 
 u64 kvm_pmu_valid_counter_mask(struct kvm_vcpu *vcpu)
 {
-	u64 val = __vcpu_sys_reg(vcpu, PMCR_EL0) >> ARMV8_PMU_PMCR_N_SHIFT;
+	u64 val = FIELD_GET(ARMV8_PMU_PMCR_N, __vcpu_sys_reg(vcpu, PMCR_EL0));
 
-	val &= ARMV8_PMU_PMCR_N_MASK;
 	if (val == 0)
 		return BIT(ARMV8_PMU_CYCLE_IDX);
 	else
arch/arm64/kvm/sys_regs.c +3 -4
index 39e9248c935e7..30108f09e088b 100644
@@ -750,7 +750,7 @@ static u64 reset_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
 		return 0;
 
 	/* Only preserve PMCR_EL0.N, and reset the rest to 0 */
-	pmcr = read_sysreg(pmcr_el0) & (ARMV8_PMU_PMCR_N_MASK << ARMV8_PMU_PMCR_N_SHIFT);
+	pmcr = read_sysreg(pmcr_el0) & ARMV8_PMU_PMCR_N;
 	if (!kvm_supports_32bit_el0())
 		pmcr |= ARMV8_PMU_PMCR_LC;
 
@@ -858,10 +858,9 @@ static bool access_pmceid(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
 
 static bool pmu_counter_idx_valid(struct kvm_vcpu *vcpu, u64 idx)
 {
-	u64 pmcr, val;
+	u64 val;
 
-	pmcr = __vcpu_sys_reg(vcpu, PMCR_EL0);
-	val = (pmcr >> ARMV8_PMU_PMCR_N_SHIFT) & ARMV8_PMU_PMCR_N_MASK;
+	val = FIELD_GET(ARMV8_PMU_PMCR_N, __vcpu_sys_reg(vcpu, PMCR_EL0));
 	if (idx >= val && idx != ARMV8_PMU_CYCLE_IDX) {
 		kvm_inject_undefined(vcpu);
 		return false;
drivers/perf/arm_pmuv3.c +1 -2
index 08b3a1bf0ef62..7618b0adc0b8c 100644
@@ -1128,8 +1128,7 @@ static void __armv8pmu_probe_pmu(void *info)
 	probe->present = true;
 
 	/* Read the nb of CNTx counters supported from PMNC */
-	cpu_pmu->num_events = (armv8pmu_pmcr_read() >> ARMV8_PMU_PMCR_N_SHIFT)
-		& ARMV8_PMU_PMCR_N_MASK;
+	cpu_pmu->num_events = FIELD_GET(ARMV8_PMU_PMCR_N, armv8pmu_pmcr_read());
 
 	/* Add the CPU cycles counter */
 	cpu_pmu->num_events += 1;
include/linux/perf/arm_pmuv3.h +1 -1
index e3899bd77f5cc..ecbcf3f93560c 100644
@@ -216,7 +216,7 @@
 #define ARMV8_PMU_PMCR_LC	(1 << 6) /* Overflow on 64 bit cycle counter */
 #define ARMV8_PMU_PMCR_LP	(1 << 7) /* Long event counter enable */
 #define ARMV8_PMU_PMCR_N_SHIFT	11  /* Number of counters supported */
-#define ARMV8_PMU_PMCR_N_MASK	0x1f
+#define	ARMV8_PMU_PMCR_N	(0x1f << ARMV8_PMU_PMCR_N_SHIFT)
 #define ARMV8_PMU_PMCR_MASK	0xff    /* Mask for writable bits */
 
 /*
Signature · 1 line
2.41.0.694.ge786442a9b-goog
Signature · 2 lines
Thanks,
Oliver
#16 lore raw

Re: [PATCH v5 05/12] KVM: arm64: PMU: Simplify extracting PMCR_EL0.N

Hi Oliver,

Quoted 5 lines · Oliver Upton

+cc will, rutland

Hi Raghu,

Quoted 8 lines

Please make sure you cc the right folks for changes that poke multiple subsystems.

The diff looks OK, but I'm somewhat dubious of the need for this change in the context of what you're trying to accomplish for KVM. I'd prefer we either leave the existing definition/usage intact or rework *all* of the PMUv3 masks to be of the shifted variety.

I believe the original intention was to make accessing the PMCR.N field simple. However, if you feel its redundant and is unnecessary for this series I'm happy to drop the patch.

Thank you.
Raghavendra

Quoted 101 lines · Raghavendra Rao Ananta
Quoted 6 lines

From: Reiji Watanabe <[email protected]>

Some code extracts PMCR_EL0.N using ARMV8_PMU_PMCR_N_SHIFT and ARMV8_PMU_PMCR_N_MASK. Define ARMV8_PMU_PMCR_N (0x1f << 11), and simplify those codes using FIELD_GET() and/or ARMV8_PMU_PMCR_N. The following patches will also use these macros to extract PMCR_EL0.N.

Changelog is a bit wordy:

Define a shifted mask for accessing PMCR_EL0.N amenable to the use of bitfield accessors and convert the existing, open-coded mask shifts to the new definition.

Quoted 80 lines

No functional change intended.

---
 arch/arm64/kvm/pmu-emul.c      | 3 +--
 arch/arm64/kvm/sys_regs.c      | 7 +++----
 drivers/perf/arm_pmuv3.c       | 3 +--
 include/linux/perf/arm_pmuv3.h | 2 +-
 4 files changed, 6 insertions(+), 9 deletions(-)
arch/arm64/kvm/pmu-emul.c +1 -2
index b87822024828a..f7b5fa16341ad 100644
@@ -245,9 +245,8 @@ void kvm_pmu_vcpu_destroy(struct kvm_vcpu *vcpu)
 
 u64 kvm_pmu_valid_counter_mask(struct kvm_vcpu *vcpu)
 {
-     u64 val = __vcpu_sys_reg(vcpu, PMCR_EL0) >> ARMV8_PMU_PMCR_N_SHIFT;
+     u64 val = FIELD_GET(ARMV8_PMU_PMCR_N, __vcpu_sys_reg(vcpu, PMCR_EL0));
 
-     val &= ARMV8_PMU_PMCR_N_MASK;
      if (val == 0)
              return BIT(ARMV8_PMU_CYCLE_IDX);
      else
arch/arm64/kvm/sys_regs.c +3 -4
index 39e9248c935e7..30108f09e088b 100644
@@ -750,7 +750,7 @@ static u64 reset_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
              return 0;
 
      /* Only preserve PMCR_EL0.N, and reset the rest to 0 */
-     pmcr = read_sysreg(pmcr_el0) & (ARMV8_PMU_PMCR_N_MASK << ARMV8_PMU_PMCR_N_SHIFT);
+     pmcr = read_sysreg(pmcr_el0) & ARMV8_PMU_PMCR_N;
      if (!kvm_supports_32bit_el0())
              pmcr |= ARMV8_PMU_PMCR_LC;
 
@@ -858,10 +858,9 @@ static bool access_pmceid(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
 
 static bool pmu_counter_idx_valid(struct kvm_vcpu *vcpu, u64 idx)
 {
-     u64 pmcr, val;
+     u64 val;
 
-     pmcr = __vcpu_sys_reg(vcpu, PMCR_EL0);
-     val = (pmcr >> ARMV8_PMU_PMCR_N_SHIFT) & ARMV8_PMU_PMCR_N_MASK;
+     val = FIELD_GET(ARMV8_PMU_PMCR_N, __vcpu_sys_reg(vcpu, PMCR_EL0));
      if (idx >= val && idx != ARMV8_PMU_CYCLE_IDX) {
              kvm_inject_undefined(vcpu);
              return false;
drivers/perf/arm_pmuv3.c +1 -2
index 08b3a1bf0ef62..7618b0adc0b8c 100644
@@ -1128,8 +1128,7 @@ static void __armv8pmu_probe_pmu(void *info)
      probe->present = true;
 
      /* Read the nb of CNTx counters supported from PMNC */
-     cpu_pmu->num_events = (armv8pmu_pmcr_read() >> ARMV8_PMU_PMCR_N_SHIFT)
-             & ARMV8_PMU_PMCR_N_MASK;
+     cpu_pmu->num_events = FIELD_GET(ARMV8_PMU_PMCR_N, armv8pmu_pmcr_read());
 
      /* Add the CPU cycles counter */
      cpu_pmu->num_events += 1;
include/linux/perf/arm_pmuv3.h +1 -2
index e3899bd77f5cc..ecbcf3f93560c 100644
@@ -216,7 +216,7 @@
 #define ARMV8_PMU_PMCR_LC    (1 << 6) /* Overflow on 64 bit cycle counter */
 #define ARMV8_PMU_PMCR_LP    (1 << 7) /* Long event counter enable */
 #define ARMV8_PMU_PMCR_N_SHIFT       11  /* Number of counters supported */
-#define ARMV8_PMU_PMCR_N_MASK        0x1f
+#define      ARMV8_PMU_PMCR_N        (0x1f << ARMV8_PMU_PMCR_N_SHIFT)
 #define ARMV8_PMU_PMCR_MASK  0xff    /* Mask for writable bits */
 
 /*
--
Quoted 3 lines

2.41.0.694.ge786442a9b-goog

--
Thanks,
Oliver

#17 lore raw

PATCH v5 6/12 KVM: arm64: PMU: Add a helper to read a vCPU's PMCR_EL0

In reply to Raghavendra Rao Ananta

From: Reiji Watanabe <[email protected]>

Add a helper to read a vCPU's PMCR_EL0, and use it when KVM reads a vCPU's PMCR_EL0.

The PMCR_EL0 value is tracked by a sysreg file per each vCPU. The following patches will make (only) PMCR_EL0.N track per guest. Having the new helper will be useful to combine the PMCR_EL0.N field (tracked per guest) and the other fields (tracked per vCPU) to provide the value of PMCR_EL0.

No functional change intended.

---
 arch/arm64/kvm/arm.c      |  3 +--
 arch/arm64/kvm/pmu-emul.c | 17 +++++++++++------
 arch/arm64/kvm/sys_regs.c |  6 +++---
 include/kvm/arm_pmu.h     |  6 ++++++
 4 files changed, 21 insertions(+), 11 deletions(-)
arch/arm64/kvm/arm.c +1 -2
index d1cb298a58a08..7bd438c181f76 100644
@@ -800,8 +800,7 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu)
 		}
 
 		if (kvm_check_request(KVM_REQ_RELOAD_PMU, vcpu))
-			kvm_pmu_handle_pmcr(vcpu,
-					    __vcpu_sys_reg(vcpu, PMCR_EL0));
+			kvm_pmu_handle_pmcr(vcpu, kvm_vcpu_read_pmcr(vcpu));
 
 		if (kvm_check_request(KVM_REQ_SUSPEND, vcpu))
 			return kvm_vcpu_suspend(vcpu);
arch/arm64/kvm/pmu-emul.c +11 -6
index f7b5fa16341ad..42b88b1a901f9 100644
@@ -67,7 +67,7 @@ static bool kvm_pmc_is_64bit(struct kvm_pmc *pmc)
 
 static bool kvm_pmc_has_64bit_overflow(struct kvm_pmc *pmc)
 {
-	u64 val = __vcpu_sys_reg(kvm_pmc_to_vcpu(pmc), PMCR_EL0);
+	u64 val = kvm_vcpu_read_pmcr(kvm_pmc_to_vcpu(pmc));
 
 	return (pmc->idx < ARMV8_PMU_CYCLE_IDX && (val & ARMV8_PMU_PMCR_LP)) ||
 	       (pmc->idx == ARMV8_PMU_CYCLE_IDX && (val & ARMV8_PMU_PMCR_LC));
@@ -245,7 +245,7 @@ void kvm_pmu_vcpu_destroy(struct kvm_vcpu *vcpu)
 
 u64 kvm_pmu_valid_counter_mask(struct kvm_vcpu *vcpu)
 {
-	u64 val = FIELD_GET(ARMV8_PMU_PMCR_N, __vcpu_sys_reg(vcpu, PMCR_EL0));
+	u64 val = FIELD_GET(ARMV8_PMU_PMCR_N, kvm_vcpu_read_pmcr(vcpu));
 
 	if (val == 0)
 		return BIT(ARMV8_PMU_CYCLE_IDX);
@@ -266,7 +266,7 @@ void kvm_pmu_enable_counter_mask(struct kvm_vcpu *vcpu, u64 val)
 	if (!kvm_vcpu_has_pmu(vcpu))
 		return;
 
-	if (!(__vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_E) || !val)
+	if (!(kvm_vcpu_read_pmcr(vcpu) & ARMV8_PMU_PMCR_E) || !val)
 		return;
 
 	for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++) {
@@ -318,7 +318,7 @@ static u64 kvm_pmu_overflow_status(struct kvm_vcpu *vcpu)
 {
 	u64 reg = 0;
 
-	if ((__vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_E)) {
+	if ((kvm_vcpu_read_pmcr(vcpu) & ARMV8_PMU_PMCR_E)) {
 		reg = __vcpu_sys_reg(vcpu, PMOVSSET_EL0);
 		reg &= __vcpu_sys_reg(vcpu, PMCNTENSET_EL0);
 		reg &= __vcpu_sys_reg(vcpu, PMINTENSET_EL1);
@@ -420,7 +420,7 @@ static void kvm_pmu_counter_increment(struct kvm_vcpu *vcpu,
 {
 	int i;
 
-	if (!(__vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_E))
+	if (!(kvm_vcpu_read_pmcr(vcpu) & ARMV8_PMU_PMCR_E))
 		return;
 
 	/* Weed out disabled counters */
@@ -563,7 +563,7 @@ void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val)
 static bool kvm_pmu_counter_is_enabled(struct kvm_pmc *pmc)
 {
 	struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
-	return (__vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_E) &&
+	return (kvm_vcpu_read_pmcr(vcpu) & ARMV8_PMU_PMCR_E) &&
 	       (__vcpu_sys_reg(vcpu, PMCNTENSET_EL0) & BIT(pmc->idx));
 }
 
@@ -1069,3 +1069,8 @@ u8 kvm_arm_pmu_get_pmuver_limit(void)
 					      ID_AA64DFR0_EL1_PMUVer_V3P5);
 	return FIELD_GET(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer), tmp);
 }
+
+u64 kvm_vcpu_read_pmcr(struct kvm_vcpu *vcpu)
+{
+	return __vcpu_sys_reg(vcpu, PMCR_EL0);
+}
arch/arm64/kvm/sys_regs.c +3 -3
index 30108f09e088b..cf4981e2c153b 100644
@@ -803,7 +803,7 @@ static bool access_pmcr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
 		 * Only update writeable bits of PMCR (continuing into
 		 * kvm_pmu_handle_pmcr() as well)
 		 */
-		val = __vcpu_sys_reg(vcpu, PMCR_EL0);
+		val = kvm_vcpu_read_pmcr(vcpu);
 		val &= ~ARMV8_PMU_PMCR_MASK;
 		val |= p->regval & ARMV8_PMU_PMCR_MASK;
 		if (!kvm_supports_32bit_el0())
@@ -811,7 +811,7 @@ static bool access_pmcr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
 		kvm_pmu_handle_pmcr(vcpu, val);
 	} else {
 		/* PMCR.P & PMCR.C are RAZ */
-		val = __vcpu_sys_reg(vcpu, PMCR_EL0)
+		val = kvm_vcpu_read_pmcr(vcpu)
 		      & ~(ARMV8_PMU_PMCR_P | ARMV8_PMU_PMCR_C);
 		p->regval = val;
 	}
@@ -860,7 +860,7 @@ static bool pmu_counter_idx_valid(struct kvm_vcpu *vcpu, u64 idx)
 {
 	u64 val;
 
-	val = FIELD_GET(ARMV8_PMU_PMCR_N, __vcpu_sys_reg(vcpu, PMCR_EL0));
+	val = FIELD_GET(ARMV8_PMU_PMCR_N, kvm_vcpu_read_pmcr(vcpu));
 	if (idx >= val && idx != ARMV8_PMU_CYCLE_IDX) {
 		kvm_inject_undefined(vcpu);
 		return false;
include/kvm/arm_pmu.h +6 -0
index 66a2f8477641e..99fe64c81ca8b 100644
@@ -102,6 +102,7 @@ void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu);
 u8 kvm_arm_pmu_get_pmuver_limit(void);
 int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu);
 
+u64 kvm_vcpu_read_pmcr(struct kvm_vcpu *vcpu);
 #else
 struct kvm_pmu {
 };
@@ -178,6 +179,11 @@ static inline int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
 	return -ENODEV;
 }
 
+static inline u64 kvm_vcpu_read_pmcr(struct kvm_vcpu *vcpu)
+{
+	return 0;
+}
+
 #endif
 
 #endif
Signature · 1 line
2.41.0.694.ge786442a9b-goog
#18 lore raw

PATCH v5 7/12 KVM: arm64: PMU: Set PMCR_EL0.N for vCPU based on the associated PMU

In reply to Raghavendra Rao Ananta

From: Reiji Watanabe <[email protected]>

The number of PMU event counters is indicated in PMCR_EL0.N. For a vCPU with PMUv3 configured, the value is set to the same value as the current PE on every vCPU reset. Unless the vCPU is pinned to PEs that has the PMU associated to the guest from the initial vCPU reset, the value might be different from the PMU's PMCR_EL0.N on heterogeneous PMU systems.

Fix this by setting the vCPU's PMCR_EL0.N to the PMU's PMCR_EL0.N value. Track the PMCR_EL0.N per guest, as only one PMU can be set for the guest (PMCR_EL0.N must be the same for all vCPUs of the guest), and it is convenient for updating the value.

KVM does not yet support userspace modifying PMCR_EL0.N. The following patch will add support for that.

---
 arch/arm64/include/asm/kvm_host.h |  3 +++
 arch/arm64/kvm/pmu-emul.c         | 14 +++++++++++++-
 arch/arm64/kvm/sys_regs.c         | 15 +++++++++------
 3 files changed, 25 insertions(+), 7 deletions(-)
arch/arm64/include/asm/kvm_host.h +3 -0
index d3dd05bbfe23f..0f2dbbe8f6a7e 100644
@@ -256,6 +256,9 @@ struct kvm_arch {
 
 	cpumask_var_t supported_cpus;
 
+	/* PMCR_EL0.N value for the guest */
+	u8 pmcr_n;
+
 	/* Hypercall features firmware registers' descriptor */
 	struct kvm_smccc_features smccc_feat;
 	struct maple_tree smccc_filter;
arch/arm64/kvm/pmu-emul.c +13 -1
index 42b88b1a901f9..ce7de6bbdc967 100644
@@ -681,6 +681,9 @@ void kvm_host_pmu_init(struct arm_pmu *pmu)
 	if (!entry)
 		goto out_unlock;
 
+	WARN_ON((pmu->num_events <= 0) ||
+		(pmu->num_events > ARMV8_PMU_MAX_COUNTERS));
+
 	entry->arm_pmu = pmu;
 	list_add_tail(&entry->entry, &arm_pmus);
 
@@ -887,6 +890,13 @@ int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
 
 	kvm->arch.arm_pmu = arm_pmu;
 
+	/*
+	 * Both the num_events and PMCR_EL0.N indicates the number of
+	 * PMU event counters, but the former includes the cycle counter
+	 * while the latter does not.
+	 */
+	kvm->arch.pmcr_n = arm_pmu->num_events - 1;
+
 	return 0;
 }
 
@@ -1072,5 +1082,7 @@ u8 kvm_arm_pmu_get_pmuver_limit(void)
 
 u64 kvm_vcpu_read_pmcr(struct kvm_vcpu *vcpu)
 {
-	return __vcpu_sys_reg(vcpu, PMCR_EL0);
+	u64 pmcr = __vcpu_sys_reg(vcpu, PMCR_EL0) & ~ARMV8_PMU_PMCR_N;
+
+	return pmcr | ((u64)vcpu->kvm->arch.pmcr_n << ARMV8_PMU_PMCR_N_SHIFT);
 }
arch/arm64/kvm/sys_regs.c +9 -6
index cf4981e2c153b..2075901356c5b 100644
@@ -745,12 +745,8 @@ static u64 reset_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
 {
 	u64 pmcr;
 
-	/* No PMU available, PMCR_EL0 may UNDEF... */
-	if (!kvm_arm_support_pmu_v3())
-		return 0;
-
 	/* Only preserve PMCR_EL0.N, and reset the rest to 0 */
-	pmcr = read_sysreg(pmcr_el0) & ARMV8_PMU_PMCR_N;
+	pmcr = kvm_vcpu_read_pmcr(vcpu) & ARMV8_PMU_PMCR_N;
 	if (!kvm_supports_32bit_el0())
 		pmcr |= ARMV8_PMU_PMCR_LC;
 
@@ -1083,6 +1079,13 @@ static bool access_pmuserenr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
 	return true;
 }
 
+static int get_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
+		    u64 *val)
+{
+	*val = kvm_vcpu_read_pmcr(vcpu);
+	return 0;
+}
+
 /* Silly macro to expand the DBG{BCR,BVR,WVR,WCR}n_EL1 registers in one go */
 #define DBG_BCR_BVR_WCR_WVR_EL1(n)					\
 	{ SYS_DESC(SYS_DBGBVRn_EL1(n)),					\
@@ -2145,7 +2148,7 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 	{ SYS_DESC(SYS_SVCR), undef_access },
 
 	{ PMU_SYS_REG(PMCR_EL0), .access = access_pmcr,
-	  .reset = reset_pmcr, .reg = PMCR_EL0 },
+	  .reset = reset_pmcr, .reg = PMCR_EL0, .get_user = get_pmcr },
 	{ PMU_SYS_REG(PMCNTENSET_EL0),
 	  .access = access_pmcnten, .reg = PMCNTENSET_EL0 },
 	{ PMU_SYS_REG(PMCNTENCLR_EL0),
Signature · 1 line
2.41.0.694.ge786442a9b-goog
#19 lore raw

PATCH v5 8/12 KVM: arm64: PMU: Allow userspace to limit PMCR_EL0.N for the guest

In reply to Raghavendra Rao Ananta

From: Reiji Watanabe <[email protected]>

KVM does not yet support userspace modifying PMCR_EL0.N (With the previous patch, KVM ignores what is written by upserspace). Add support userspace limiting PMCR_EL0.N.

Disallow userspace to set PMCR_EL0.N to a value that is greater than the host value (KVM_SET_ONE_REG will fail), as KVM doesn't support more event counters than the host HW implements. Although this is an ABI change, this change only affects userspace setting PMCR_EL0.N to a larger value than the host. As accesses to unadvertised event counters indices is CONSTRAINED UNPREDICTABLE behavior, and PMCR_EL0.N was reset to the host value on every vCPU reset before this series, I can't think of any use case where a user space would do that.

Also, ignore writes to read-only bits that are cleared on vCPU reset, and RES{0,1} bits (including writable bits that KVM doesn't support yet), as those bits shouldn't be modified (at least with the current KVM).

---
 arch/arm64/include/asm/kvm_host.h |  3 ++
 arch/arm64/kvm/pmu-emul.c         |  1 +
 arch/arm64/kvm/sys_regs.c         | 49 +++++++++++++++++++++++++++++--
 3 files changed, 51 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_host.h +3 -0
index 0f2dbbe8f6a7e..c15ec365283d1 100644
@@ -259,6 +259,9 @@ struct kvm_arch {
 	/* PMCR_EL0.N value for the guest */
 	u8 pmcr_n;
 
+	/* Limit value of PMCR_EL0.N for the guest */
+	u8 pmcr_n_limit;
+
 	/* Hypercall features firmware registers' descriptor */
 	struct kvm_smccc_features smccc_feat;
 	struct maple_tree smccc_filter;
arch/arm64/kvm/pmu-emul.c +1 -0
index ce7de6bbdc967..39ad56a71ad20 100644
@@ -896,6 +896,7 @@ int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
 	 * while the latter does not.
 	 */
 	kvm->arch.pmcr_n = arm_pmu->num_events - 1;
+	kvm->arch.pmcr_n_limit = arm_pmu->num_events - 1;
 
 	return 0;
 }
arch/arm64/kvm/sys_regs.c +47 -2

61 lines not on this page · raw message

Signature · 1 line
2.41.0.694.ge786442a9b-goog
#20 lore raw

Re: [PATCH v5 08/12] KVM: arm64: PMU: Allow userspace to limit PMCR_EL0.N for the guest

Hi Raghavendra,

Quoted 73 lines · Raghavendra Rao Ananta

From: Reiji Watanabe <[email protected]>

KVM does not yet support userspace modifying PMCR_EL0.N (With the previous patch, KVM ignores what is written by upserspace). Add support userspace limiting PMCR_EL0.N.

Disallow userspace to set PMCR_EL0.N to a value that is greater than the host value (KVM_SET_ONE_REG will fail), as KVM doesn't support more event counters than the host HW implements. Although this is an ABI change, this change only affects userspace setting PMCR_EL0.N to a larger value than the host. As accesses to unadvertised event counters indices is CONSTRAINED UNPREDICTABLE behavior, and PMCR_EL0.N was reset to the host value on every vCPU reset before this series, I can't think of any use case where a user space would do that.

Also, ignore writes to read-only bits that are cleared on vCPU reset, and RES{0,1} bits (including writable bits that KVM doesn't support yet), as those bits shouldn't be modified (at least with the current KVM).

---
  arch/arm64/include/asm/kvm_host.h |  3 ++
  arch/arm64/kvm/pmu-emul.c         |  1 +
  arch/arm64/kvm/sys_regs.c         | 49 +++++++++++++++++++++++++++++--
  3 files changed, 51 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_host.h +3 -0
index 0f2dbbe8f6a7e..c15ec365283d1 100644
@@ -259,6 +259,9 @@ struct kvm_arch {
  	/* PMCR_EL0.N value for the guest */
  	u8 pmcr_n;
 
+	/* Limit value of PMCR_EL0.N for the guest */
+	u8 pmcr_n_limit;
+
  	/* Hypercall features firmware registers' descriptor */
  	struct kvm_smccc_features smccc_feat;
  	struct maple_tree smccc_filter;
arch/arm64/kvm/pmu-emul.c +1 -0
index ce7de6bbdc967..39ad56a71ad20 100644
@@ -896,6 +896,7 @@ int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
  	 * while the latter does not.
  	 */
  	kvm->arch.pmcr_n = arm_pmu->num_events - 1;
+	kvm->arch.pmcr_n_limit = arm_pmu->num_events - 1;
 
  	return 0;
  }
arch/arm64/kvm/sys_regs.c +10 -0
index 2075901356c5b..c01d62afa7db4 100644
@@ -1086,6 +1086,51 @@ static int get_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
  	return 0;
  }
 
+static int set_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
+		    u64 val)
+{
+	struct kvm *kvm = vcpu->kvm;
+	u64 new_n, mutable_mask;
+	int ret = 0;
+
+	new_n = FIELD_GET(ARMV8_PMU_PMCR_N, val);
+
+	mutex_lock(&kvm->arch.config_lock);

arch/arm64/kvm/sys_regs.c static int get_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, · line 1108

Quoted 10 lines
arch/arm64/kvm/sys_regs.c +10 -0
+	if (unlikely(new_n != kvm->arch.pmcr_n)) {
+		/*
+		 * The vCPU can't have more counters than the PMU
+		 * hardware implements.
+		 */
+		if (new_n <= kvm->arch.pmcr_n_limit)
+			kvm->arch.pmcr_n = new_n;
+		else
+			ret = -EINVAL;
+	}

Since we have set the default value of pmcr_n, if we want to set a new pmcr_n, shouldn't it be a different value?

So how about change the checking to:

if (likely(new_n <= kvm->arch.pmcr_n_limit)

	kvm->arch.pmcr_n = new_n;
else
	ret = -EINVAL;

what do you think?

Quoted 29 lines
arch/arm64/kvm/sys_regs.c +25 -0
+	mutex_unlock(&kvm->arch.config_lock);
+	if (ret)
+		return ret;
+
+	/*
+	 * Ignore writes to RES0 bits, read only bits that are cleared on
+	 * vCPU reset, and writable bits that KVM doesn't support yet.
+	 * (i.e. only PMCR.N and bits [7:0] are mutable from userspace)
+	 * The LP bit is RES0 when FEAT_PMUv3p5 is not supported on the vCPU.
+	 * But, we leave the bit as it is here, as the vCPU's PMUver might
+	 * be changed later (NOTE: the bit will be cleared on first vCPU run
+	 * if necessary).
+	 */
+	mutable_mask = (ARMV8_PMU_PMCR_MASK | ARMV8_PMU_PMCR_N);
+	val &= mutable_mask;
+	val |= (__vcpu_sys_reg(vcpu, r->reg) & ~mutable_mask);
+
+	/* The LC bit is RES1 when AArch32 is not supported */
+	if (!kvm_supports_32bit_el0())
+		val |= ARMV8_PMU_PMCR_LC;
+
+	__vcpu_sys_reg(vcpu, r->reg) = val;
+	return 0;
+}
+
  /* Silly macro to expand the DBG{BCR,BVR,WVR,WCR}n_EL1 registers in one go */
  #define DBG_BCR_BVR_WCR_WVR_EL1(n)					\
  	{ SYS_DESC(SYS_DBGBVRn_EL1(n)),					\

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

Quoted 7 lines
arch/arm64/kvm/sys_regs.c +2 -2
@@ -2147,8 +2192,8 @@ static const struct sys_reg_desc sys_reg_descs[] = {
  	{ SYS_DESC(SYS_CTR_EL0), access_ctr },
  	{ SYS_DESC(SYS_SVCR), undef_access },
 
-	{ PMU_SYS_REG(PMCR_EL0), .access = access_pmcr,
-	  .reset = reset_pmcr, .reg = PMCR_EL0, .get_user = get_pmcr },
+	{ PMU_SYS_REG(PMCR_EL0), .access = access_pmcr, .reset = reset_pmcr,
+	  .reg = PMCR_EL0, .get_user = get_pmcr, .set_user = set_pmcr },

A little confusing, since the PMU_SYS_REG() defines the default visibility which is pmu_visibility can return REG_HIDDEN, the set_user to pmcr will be blocked, how can it being set?

Maybe I lose some details.

Thanks,
Shaoqin

Quoted 3 lines
arch/arm64/kvm/sys_regs.c +0 -0
  	{ PMU_SYS_REG(PMCNTENSET_EL0),
  	  .access = access_pmcnten, .reg = PMCNTENSET_EL0 },
  	{ PMU_SYS_REG(PMCNTENCLR_EL0),
Signature · 1 line
Shaoqin
#21 lore raw

Re: [PATCH v5 08/12] KVM: arm64: PMU: Allow userspace to limit PMCR_EL0.N for the guest

Hi Shaoqin,

Quoted 91 lines · Shaoqin Huang

Hi Raghavendra,

Quoted 73 lines · Raghavendra Rao Ananta

From: Reiji Watanabe <[email protected]>

KVM does not yet support userspace modifying PMCR_EL0.N (With the previous patch, KVM ignores what is written by upserspace). Add support userspace limiting PMCR_EL0.N.

Disallow userspace to set PMCR_EL0.N to a value that is greater than the host value (KVM_SET_ONE_REG will fail), as KVM doesn't support more event counters than the host HW implements. Although this is an ABI change, this change only affects userspace setting PMCR_EL0.N to a larger value than the host. As accesses to unadvertised event counters indices is CONSTRAINED UNPREDICTABLE behavior, and PMCR_EL0.N was reset to the host value on every vCPU reset before this series, I can't think of any use case where a user space would do that.

Also, ignore writes to read-only bits that are cleared on vCPU reset, and RES{0,1} bits (including writable bits that KVM doesn't support yet), as those bits shouldn't be modified (at least with the current KVM).

---
  arch/arm64/include/asm/kvm_host.h |  3 ++
  arch/arm64/kvm/pmu-emul.c         |  1 +
  arch/arm64/kvm/sys_regs.c         | 49 +++++++++++++++++++++++++++++--
  3 files changed, 51 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_host.h +3 -0
index 0f2dbbe8f6a7e..c15ec365283d1 100644
@@ -259,6 +259,9 @@ struct kvm_arch {
      /* PMCR_EL0.N value for the guest */
      u8 pmcr_n;
 
+     /* Limit value of PMCR_EL0.N for the guest */
+     u8 pmcr_n_limit;
+
      /* Hypercall features firmware registers' descriptor */
      struct kvm_smccc_features smccc_feat;
      struct maple_tree smccc_filter;
arch/arm64/kvm/pmu-emul.c +1 -0
index ce7de6bbdc967..39ad56a71ad20 100644
@@ -896,6 +896,7 @@ int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
       * while the latter does not.
       */
      kvm->arch.pmcr_n = arm_pmu->num_events - 1;
+     kvm->arch.pmcr_n_limit = arm_pmu->num_events - 1;
 
      return 0;
  }
arch/arm64/kvm/sys_regs.c +10 -0
index 2075901356c5b..c01d62afa7db4 100644
@@ -1086,6 +1086,51 @@ static int get_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
      return 0;
  }
 
+static int set_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
+                 u64 val)
+{
+     struct kvm *kvm = vcpu->kvm;
+     u64 new_n, mutable_mask;
+     int ret = 0;
+
+     new_n = FIELD_GET(ARMV8_PMU_PMCR_N, val);
+
+     mutex_lock(&kvm->arch.config_lock);

arch/arm64/kvm/sys_regs.c static int get_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, · line 1108

Quoted 10 lines
arch/arm64/kvm/sys_regs.c +10 -0
+     if (unlikely(new_n != kvm->arch.pmcr_n)) {
+             /*
+              * The vCPU can't have more counters than the PMU
+              * hardware implements.
+              */
+             if (new_n <= kvm->arch.pmcr_n_limit)
+                     kvm->arch.pmcr_n = new_n;
+             else
+                     ret = -EINVAL;
+     }

Since we have set the default value of pmcr_n, if we want to set a new pmcr_n, shouldn't it be a different value?

Quoted 9 lines

So how about change the checking to:

if (likely(new_n <= kvm->arch.pmcr_n_limit)

        kvm->arch.pmcr_n = new_n;
else
        ret = -EINVAL;

what do you think?

Sorry, I guess I didn't fully understand your suggestion. Are you saying that it's 'likely' that userspace would configure the correct value?

Quoted 41 lines
Quoted 29 lines
arch/arm64/kvm/sys_regs.c +25 -0
+     mutex_unlock(&kvm->arch.config_lock);
+     if (ret)
+             return ret;
+
+     /*
+      * Ignore writes to RES0 bits, read only bits that are cleared on
+      * vCPU reset, and writable bits that KVM doesn't support yet.
+      * (i.e. only PMCR.N and bits [7:0] are mutable from userspace)
+      * The LP bit is RES0 when FEAT_PMUv3p5 is not supported on the vCPU.
+      * But, we leave the bit as it is here, as the vCPU's PMUver might
+      * be changed later (NOTE: the bit will be cleared on first vCPU run
+      * if necessary).
+      */
+     mutable_mask = (ARMV8_PMU_PMCR_MASK | ARMV8_PMU_PMCR_N);
+     val &= mutable_mask;
+     val |= (__vcpu_sys_reg(vcpu, r->reg) & ~mutable_mask);
+
+     /* The LC bit is RES1 when AArch32 is not supported */
+     if (!kvm_supports_32bit_el0())
+             val |= ARMV8_PMU_PMCR_LC;
+
+     __vcpu_sys_reg(vcpu, r->reg) = val;
+     return 0;
+}
+
  /* Silly macro to expand the DBG{BCR,BVR,WVR,WCR}n_EL1 registers in one go */
  #define DBG_BCR_BVR_WCR_WVR_EL1(n)                                  \
      { SYS_DESC(SYS_DBGBVRn_EL1(n)),                                 \

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

Quoted 7 lines
arch/arm64/kvm/sys_regs.c +2 -2
@@ -2147,8 +2192,8 @@ static const struct sys_reg_desc sys_reg_descs[] = {
      { SYS_DESC(SYS_CTR_EL0), access_ctr },
      { SYS_DESC(SYS_SVCR), undef_access },
 
-     { PMU_SYS_REG(PMCR_EL0), .access = access_pmcr,
-       .reset = reset_pmcr, .reg = PMCR_EL0, .get_user = get_pmcr },
+     { PMU_SYS_REG(PMCR_EL0), .access = access_pmcr, .reset = reset_pmcr,
+       .reg = PMCR_EL0, .get_user = get_pmcr, .set_user = set_pmcr },

A little confusing, since the PMU_SYS_REG() defines the default visibility which is pmu_visibility can return REG_HIDDEN, the set_user to pmcr will be blocked, how can it being set?

Quoted 2 lines

Maybe I lose some details.

pmu_visibility() returns REG_HIDDEN only if userspace has not added support for PMUv3 via KVM_ARM_PREFERRED_TARGET ioctl. Else, it should return 0, and give access.

Thank you.
Raghavendra

Quoted 10 lines

Thanks,
Shaoqin

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

Quoted 3 lines
arch/arm64/kvm/sys_regs.c +0 -0
      { PMU_SYS_REG(PMCNTENSET_EL0),
        .access = access_pmcnten, .reg = PMCNTENSET_EL0 },
      { PMU_SYS_REG(PMCNTENCLR_EL0),

--
Shaoqin

#22 lore raw

Re: [PATCH v5 08/12] KVM: arm64: PMU: Allow userspace to limit PMCR_EL0.N for the guest

Hi Raghavendra,

Quoted 101 lines · Raghavendra Rao Ananta

Hi Shaoqin,

Quoted 98 lines · Shaoqin Huang

Hi Raghavendra,

Quoted 73 lines · Raghavendra Rao Ananta

From: Reiji Watanabe <[email protected]>

KVM does not yet support userspace modifying PMCR_EL0.N (With the previous patch, KVM ignores what is written by upserspace). Add support userspace limiting PMCR_EL0.N.

Disallow userspace to set PMCR_EL0.N to a value that is greater than the host value (KVM_SET_ONE_REG will fail), as KVM doesn't support more event counters than the host HW implements. Although this is an ABI change, this change only affects userspace setting PMCR_EL0.N to a larger value than the host. As accesses to unadvertised event counters indices is CONSTRAINED UNPREDICTABLE behavior, and PMCR_EL0.N was reset to the host value on every vCPU reset before this series, I can't think of any use case where a user space would do that.

Also, ignore writes to read-only bits that are cleared on vCPU reset, and RES{0,1} bits (including writable bits that KVM doesn't support yet), as those bits shouldn't be modified (at least with the current KVM).

---
   arch/arm64/include/asm/kvm_host.h |  3 ++
   arch/arm64/kvm/pmu-emul.c         |  1 +
   arch/arm64/kvm/sys_regs.c         | 49 +++++++++++++++++++++++++++++--
   3 files changed, 51 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_host.h +3 -0
index 0f2dbbe8f6a7e..c15ec365283d1 100644
@@ -259,6 +259,9 @@ struct kvm_arch {
       /* PMCR_EL0.N value for the guest */
       u8 pmcr_n;
 
+     /* Limit value of PMCR_EL0.N for the guest */
+     u8 pmcr_n_limit;
+
       /* Hypercall features firmware registers' descriptor */
       struct kvm_smccc_features smccc_feat;
       struct maple_tree smccc_filter;
arch/arm64/kvm/pmu-emul.c +1 -0
index ce7de6bbdc967..39ad56a71ad20 100644
@@ -896,6 +896,7 @@ int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
        * while the latter does not.
        */
       kvm->arch.pmcr_n = arm_pmu->num_events - 1;
+     kvm->arch.pmcr_n_limit = arm_pmu->num_events - 1;
 
       return 0;
   }
arch/arm64/kvm/sys_regs.c +10 -0
index 2075901356c5b..c01d62afa7db4 100644
@@ -1086,6 +1086,51 @@ static int get_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
       return 0;
   }
 
+static int set_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
+                 u64 val)
+{
+     struct kvm *kvm = vcpu->kvm;
+     u64 new_n, mutable_mask;
+     int ret = 0;
+
+     new_n = FIELD_GET(ARMV8_PMU_PMCR_N, val);
+
+     mutex_lock(&kvm->arch.config_lock);

arch/arm64/kvm/sys_regs.c static int get_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, · line 1108

Quoted 10 lines
arch/arm64/kvm/sys_regs.c +10 -0
+     if (unlikely(new_n != kvm->arch.pmcr_n)) {
+             /*
+              * The vCPU can't have more counters than the PMU
+              * hardware implements.
+              */
+             if (new_n <= kvm->arch.pmcr_n_limit)
+                     kvm->arch.pmcr_n = new_n;
+             else
+                     ret = -EINVAL;
+     }

Since we have set the default value of pmcr_n, if we want to set a new pmcr_n, shouldn't it be a different value?

So how about change the checking to:

if (likely(new_n <= kvm->arch.pmcr_n_limit)

         kvm->arch.pmcr_n = new_n;
else
         ret = -EINVAL;
Quoted 6 lines
Quoted 2 lines

what do you think?

Sorry, I guess I didn't fully understand your suggestion. Are you saying that it's 'likely' that userspace would configure the correct value?

It depends on how userspace use this api to limit the number of pmcr. I think what you mean in the code is that userspace need to set every vcpu's pmcr to the same value, so the unlikely here is right, only one vcpu can change the kvm->arch.pmcr.n, it saves the cpu cycles.

What suggest above might be wrong. Since I think when userspace want to limit the number of pmcr, it may just set the new_n on one vcpu, since the kvm->arch.pmcr_n is a VM-local value, every vcpu can see it, so it's likely the (new_n <= kvm->arch.pmcr_n_limit), it can decrease one checking statement.

Thanks,
Shaoqin

Quoted 41 lines
Quoted 41 lines
Quoted 29 lines
arch/arm64/kvm/sys_regs.c +25 -0
+     mutex_unlock(&kvm->arch.config_lock);
+     if (ret)
+             return ret;
+
+     /*
+      * Ignore writes to RES0 bits, read only bits that are cleared on
+      * vCPU reset, and writable bits that KVM doesn't support yet.
+      * (i.e. only PMCR.N and bits [7:0] are mutable from userspace)
+      * The LP bit is RES0 when FEAT_PMUv3p5 is not supported on the vCPU.
+      * But, we leave the bit as it is here, as the vCPU's PMUver might
+      * be changed later (NOTE: the bit will be cleared on first vCPU run
+      * if necessary).
+      */
+     mutable_mask = (ARMV8_PMU_PMCR_MASK | ARMV8_PMU_PMCR_N);
+     val &= mutable_mask;
+     val |= (__vcpu_sys_reg(vcpu, r->reg) & ~mutable_mask);
+
+     /* The LC bit is RES1 when AArch32 is not supported */
+     if (!kvm_supports_32bit_el0())
+             val |= ARMV8_PMU_PMCR_LC;
+
+     __vcpu_sys_reg(vcpu, r->reg) = val;
+     return 0;
+}
+
   /* Silly macro to expand the DBG{BCR,BVR,WVR,WCR}n_EL1 registers in one go */
   #define DBG_BCR_BVR_WCR_WVR_EL1(n)                                  \
       { SYS_DESC(SYS_DBGBVRn_EL1(n)),                                 \

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

Quoted 7 lines
arch/arm64/kvm/sys_regs.c +2 -2
@@ -2147,8 +2192,8 @@ static const struct sys_reg_desc sys_reg_descs[] = {
       { SYS_DESC(SYS_CTR_EL0), access_ctr },
       { SYS_DESC(SYS_SVCR), undef_access },
 
-     { PMU_SYS_REG(PMCR_EL0), .access = access_pmcr,
-       .reset = reset_pmcr, .reg = PMCR_EL0, .get_user = get_pmcr },
+     { PMU_SYS_REG(PMCR_EL0), .access = access_pmcr, .reset = reset_pmcr,
+       .reg = PMCR_EL0, .get_user = get_pmcr, .set_user = set_pmcr },

A little confusing, since the PMU_SYS_REG() defines the default visibility which is pmu_visibility can return REG_HIDDEN, the set_user to pmcr will be blocked, how can it being set?

Quoted 6 lines
Quoted 2 lines

Maybe I lose some details.

pmu_visibility() returns REG_HIDDEN only if userspace has not added support for PMUv3 via KVM_ARM_PREFERRED_TARGET ioctl. Else, it should return 0, and give access.

Got it. Thanks.

Quoted 14 lines

Thank you.
Raghavendra

Quoted 10 lines

Thanks,
Shaoqin

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

Quoted 3 lines
arch/arm64/kvm/sys_regs.c +0 -0
       { PMU_SYS_REG(PMCNTENSET_EL0),
         .access = access_pmcnten, .reg = PMCNTENSET_EL0 },
       { PMU_SYS_REG(PMCNTENCLR_EL0),

--
Shaoqin

Signature · 1 line
Shaoqin
#23 lore raw

Re: [PATCH v5 08/12] KVM: arm64: PMU: Allow userspace to limit PMCR_EL0.N for the guest

On Tue, Aug 22, 2023 at 11:26:23AM +0800, Shaoqin Huang wrote:

[...]

Quoted 42 lines
Quoted 31 lines
Quoted 31 lines
Quoted 10 lines
arch/arm64/kvm/sys_regs.c +10 -0
+static int set_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
+                 u64 val)
+{
+     struct kvm *kvm = vcpu->kvm;
+     u64 new_n, mutable_mask;
+     int ret = 0;
+
+     new_n = FIELD_GET(ARMV8_PMU_PMCR_N, val);
+
+     mutex_lock(&kvm->arch.config_lock);

arch/arm64/kvm/sys_regs.c static int get_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, · line 1108

Quoted 10 lines
arch/arm64/kvm/sys_regs.c +10 -0
+     if (unlikely(new_n != kvm->arch.pmcr_n)) {
+             /*
+              * The vCPU can't have more counters than the PMU
+              * hardware implements.
+              */
+             if (new_n <= kvm->arch.pmcr_n_limit)
+                     kvm->arch.pmcr_n = new_n;
+             else
+                     ret = -EINVAL;
+     }

Since we have set the default value of pmcr_n, if we want to set a new pmcr_n, shouldn't it be a different value?

So how about change the checking to:

if (likely(new_n <= kvm->arch.pmcr_n_limit)

         kvm->arch.pmcr_n = new_n;
else
         ret = -EINVAL;
Quoted 6 lines
Quoted 2 lines

what do you think?

Sorry, I guess I didn't fully understand your suggestion. Are you saying that it's 'likely' that userspace would configure the correct value?

It depends on how userspace use this api to limit the number of pmcr. I think what you mean in the code is that userspace need to set every vcpu's pmcr to the same value, so the unlikely here is right, only one vcpu can change the kvm->arch.pmcr.n, it saves the cpu cycles.

Quoted 5 lines

What suggest above might be wrong. Since I think when userspace want to limit the number of pmcr, it may just set the new_n on one vcpu, since the kvm->arch.pmcr_n is a VM-local value, every vcpu can see it, so it's likely the (new_n <= kvm->arch.pmcr_n_limit), it can decrease one checking statement.

How about we just do away with branch hints in the first place? This is _not_ a hot path.

Signature · 2 lines
Thanks,
Oliver
#24 lore raw

Re: [PATCH v5 08/12] KVM: arm64: PMU: Allow userspace to limit PMCR_EL0.N for the guest

Quoted 47 lines · Oliver Upton

On Tue, Aug 22, 2023 at 11:26:23AM +0800, Shaoqin Huang wrote:

[...]

Quoted 42 lines
Quoted 31 lines
Quoted 31 lines
Quoted 10 lines
arch/arm64/kvm/sys_regs.c +10 -0
+static int set_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
+                 u64 val)
+{
+     struct kvm *kvm = vcpu->kvm;
+     u64 new_n, mutable_mask;
+     int ret = 0;
+
+     new_n = FIELD_GET(ARMV8_PMU_PMCR_N, val);
+
+     mutex_lock(&kvm->arch.config_lock);

arch/arm64/kvm/sys_regs.c static int get_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, · line 1108

Quoted 10 lines
arch/arm64/kvm/sys_regs.c +10 -0
+     if (unlikely(new_n != kvm->arch.pmcr_n)) {
+             /*
+              * The vCPU can't have more counters than the PMU
+              * hardware implements.
+              */
+             if (new_n <= kvm->arch.pmcr_n_limit)
+                     kvm->arch.pmcr_n = new_n;
+             else
+                     ret = -EINVAL;
+     }

Since we have set the default value of pmcr_n, if we want to set a new pmcr_n, shouldn't it be a different value?

So how about change the checking to:

if (likely(new_n <= kvm->arch.pmcr_n_limit)

         kvm->arch.pmcr_n = new_n;
else
         ret = -EINVAL;
Quoted 6 lines
Quoted 2 lines

what do you think?

Sorry, I guess I didn't fully understand your suggestion. Are you saying that it's 'likely' that userspace would configure the correct value?

It depends on how userspace use this api to limit the number of pmcr. I think what you mean in the code is that userspace need to set every vcpu's pmcr to the same value, so the unlikely here is right, only one vcpu can change the kvm->arch.pmcr.n, it saves the cpu cycles.

Quoted 9 lines
Quoted 5 lines

What suggest above might be wrong. Since I think when userspace want to limit the number of pmcr, it may just set the new_n on one vcpu, since the kvm->arch.pmcr_n is a VM-local value, every vcpu can see it, so it's likely the (new_n <= kvm->arch.pmcr_n_limit), it can decrease one checking statement.

How about we just do away with branch hints in the first place? This is _not_ a hot path.

Sounds good to me.

Thank you.
Raghavendra

Quoted 3 lines

--
Thanks,
Oliver

#25 lore raw

Re: [PATCH v5 08/12] KVM: arm64: PMU: Allow userspace to limit PMCR_EL0.N for the guest

In reply to Raghavendra Rao Ananta

Hi Raghavendra,

Quoted 74 lines · Raghavendra Rao Ananta

From: Reiji Watanabe <[email protected]>

KVM does not yet support userspace modifying PMCR_EL0.N (With the previous patch, KVM ignores what is written by upserspace). Add support userspace limiting PMCR_EL0.N.

Disallow userspace to set PMCR_EL0.N to a value that is greater than the host value (KVM_SET_ONE_REG will fail), as KVM doesn't support more event counters than the host HW implements. Although this is an ABI change, this change only affects userspace setting PMCR_EL0.N to a larger value than the host. As accesses to unadvertised event counters indices is CONSTRAINED UNPREDICTABLE behavior, and PMCR_EL0.N was reset to the host value on every vCPU reset before this series, I can't think of any use case where a user space would do that.

Also, ignore writes to read-only bits that are cleared on vCPU reset, and RES{0,1} bits (including writable bits that KVM doesn't support yet), as those bits shouldn't be modified (at least with the current KVM).

---
  arch/arm64/include/asm/kvm_host.h |  3 ++
  arch/arm64/kvm/pmu-emul.c         |  1 +
  arch/arm64/kvm/sys_regs.c         | 49 +++++++++++++++++++++++++++++--
  3 files changed, 51 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_host.h +3 -0
index 0f2dbbe8f6a7e..c15ec365283d1 100644
@@ -259,6 +259,9 @@ struct kvm_arch {
  	/* PMCR_EL0.N value for the guest */
  	u8 pmcr_n;
 
+	/* Limit value of PMCR_EL0.N for the guest */
+	u8 pmcr_n_limit;
+
  	/* Hypercall features firmware registers' descriptor */
  	struct kvm_smccc_features smccc_feat;
  	struct maple_tree smccc_filter;
arch/arm64/kvm/pmu-emul.c +1 -0
index ce7de6bbdc967..39ad56a71ad20 100644
@@ -896,6 +896,7 @@ int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
  	 * while the latter does not.
  	 */
  	kvm->arch.pmcr_n = arm_pmu->num_events - 1;
+	kvm->arch.pmcr_n_limit = arm_pmu->num_events - 1;
 
  	return 0;
  }
arch/arm64/kvm/sys_regs.c +11 -0
index 2075901356c5b..c01d62afa7db4 100644
@@ -1086,6 +1086,51 @@ static int get_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
  	return 0;
  }
 
+static int set_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
+		    u64 val)
+{
+	struct kvm *kvm = vcpu->kvm;
+	u64 new_n, mutable_mask;
+	int ret = 0;
+
+	new_n = FIELD_GET(ARMV8_PMU_PMCR_N, val);
+
+	mutex_lock(&kvm->arch.config_lock);
+	if (unlikely(new_n != kvm->arch.pmcr_n)) {

arch/arm64/kvm/sys_regs.c static int get_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, · line 1109

Quoted 10 lines
arch/arm64/kvm/sys_regs.c +10 -0
+		/*
+		 * The vCPU can't have more counters than the PMU
+		 * hardware implements.
+		 */
+		if (new_n <= kvm->arch.pmcr_n_limit)
+			kvm->arch.pmcr_n = new_n;
+		else
+			ret = -EINVAL;
+	}
+	mutex_unlock(&kvm->arch.config_lock);

Another thing I am just wonder is that should we block any modification to the pmcr_n after vm start to run? Like add one more checking kvm_vm_has_ran_once() at the beginning of the set_pmcr() function.

Thanks,
Shaoqin

Quoted 38 lines
arch/arm64/kvm/sys_regs.c +26 -2
+	if (ret)
+		return ret;
+
+	/*
+	 * Ignore writes to RES0 bits, read only bits that are cleared on
+	 * vCPU reset, and writable bits that KVM doesn't support yet.
+	 * (i.e. only PMCR.N and bits [7:0] are mutable from userspace)
+	 * The LP bit is RES0 when FEAT_PMUv3p5 is not supported on the vCPU.
+	 * But, we leave the bit as it is here, as the vCPU's PMUver might
+	 * be changed later (NOTE: the bit will be cleared on first vCPU run
+	 * if necessary).
+	 */
+	mutable_mask = (ARMV8_PMU_PMCR_MASK | ARMV8_PMU_PMCR_N);
+	val &= mutable_mask;
+	val |= (__vcpu_sys_reg(vcpu, r->reg) & ~mutable_mask);
+
+	/* The LC bit is RES1 when AArch32 is not supported */
+	if (!kvm_supports_32bit_el0())
+		val |= ARMV8_PMU_PMCR_LC;
+
+	__vcpu_sys_reg(vcpu, r->reg) = val;
+	return 0;
+}
+
  /* Silly macro to expand the DBG{BCR,BVR,WVR,WCR}n_EL1 registers in one go */
  #define DBG_BCR_BVR_WCR_WVR_EL1(n)					\
  	{ SYS_DESC(SYS_DBGBVRn_EL1(n)),					\
@@ -2147,8 +2192,8 @@ static const struct sys_reg_desc sys_reg_descs[] = {
  	{ SYS_DESC(SYS_CTR_EL0), access_ctr },
  	{ SYS_DESC(SYS_SVCR), undef_access },
 
-	{ PMU_SYS_REG(PMCR_EL0), .access = access_pmcr,
-	  .reset = reset_pmcr, .reg = PMCR_EL0, .get_user = get_pmcr },
+	{ PMU_SYS_REG(PMCR_EL0), .access = access_pmcr, .reset = reset_pmcr,
+	  .reg = PMCR_EL0, .get_user = get_pmcr, .set_user = set_pmcr },
  	{ PMU_SYS_REG(PMCNTENSET_EL0),
  	  .access = access_pmcnten, .reg = PMCNTENSET_EL0 },
  	{ PMU_SYS_REG(PMCNTENCLR_EL0),
Signature · 1 line
Shaoqin
#26 lore raw

Re: [PATCH v5 08/12] KVM: arm64: PMU: Allow userspace to limit PMCR_EL0.N for the guest

Quoted 84 lines · Shaoqin Huang

Hi Raghavendra,

Quoted 80 lines · Raghavendra Rao Ananta

From: Reiji Watanabe <[email protected]>

KVM does not yet support userspace modifying PMCR_EL0.N (With the previous patch, KVM ignores what is written by upserspace). Add support userspace limiting PMCR_EL0.N.

Disallow userspace to set PMCR_EL0.N to a value that is greater than the host value (KVM_SET_ONE_REG will fail), as KVM doesn't support more event counters than the host HW implements. Although this is an ABI change, this change only affects userspace setting PMCR_EL0.N to a larger value than the host. As accesses to unadvertised event counters indices is CONSTRAINED UNPREDICTABLE behavior, and PMCR_EL0.N was reset to the host value on every vCPU reset before this series, I can't think of any use case where a user space would do that.

Also, ignore writes to read-only bits that are cleared on vCPU reset, and RES{0,1} bits (including writable bits that KVM doesn't support yet), as those bits shouldn't be modified (at least with the current KVM).

---
  arch/arm64/include/asm/kvm_host.h |  3 ++
  arch/arm64/kvm/pmu-emul.c         |  1 +
  arch/arm64/kvm/sys_regs.c         | 49 +++++++++++++++++++++++++++++--
  3 files changed, 51 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_host.h +3 -0
index 0f2dbbe8f6a7e..c15ec365283d1 100644
@@ -259,6 +259,9 @@ struct kvm_arch {
      /* PMCR_EL0.N value for the guest */
      u8 pmcr_n;
 
+     /* Limit value of PMCR_EL0.N for the guest */
+     u8 pmcr_n_limit;
+
      /* Hypercall features firmware registers' descriptor */
      struct kvm_smccc_features smccc_feat;
      struct maple_tree smccc_filter;
arch/arm64/kvm/pmu-emul.c +1 -0
index ce7de6bbdc967..39ad56a71ad20 100644
@@ -896,6 +896,7 @@ int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
       * while the latter does not.
       */
      kvm->arch.pmcr_n = arm_pmu->num_events - 1;
+     kvm->arch.pmcr_n_limit = arm_pmu->num_events - 1;
 
      return 0;
  }
arch/arm64/kvm/sys_regs.c +17 -0
index 2075901356c5b..c01d62afa7db4 100644
@@ -1086,6 +1086,51 @@ static int get_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
      return 0;
  }
 
+static int set_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
+                 u64 val)
+{
+     struct kvm *kvm = vcpu->kvm;
+     u64 new_n, mutable_mask;
+     int ret = 0;
+
+     new_n = FIELD_GET(ARMV8_PMU_PMCR_N, val);
+
+     mutex_lock(&kvm->arch.config_lock);
+     if (unlikely(new_n != kvm->arch.pmcr_n)) {
+             /*
+              * The vCPU can't have more counters than the PMU
+              * hardware implements.
+              */
+             if (new_n <= kvm->arch.pmcr_n_limit)
+                     kvm->arch.pmcr_n = new_n;
Quoted 9 lines

arch/arm64/kvm/sys_regs.c static int get_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, · line 1109

Quoted 4 lines
arch/arm64/kvm/sys_regs.c +4 -0
+             else
+                     ret = -EINVAL;
+     }
+     mutex_unlock(&kvm->arch.config_lock);

Another thing I am just wonder is that should we block any modification to the pmcr_n after vm start to run? Like add one more checking kvm_vm_has_ran_once() at the beginning of the set_pmcr() function.

Thanks for bringing it up. Reiji and I discussed about this. Checking for kvm_vm_has_ran_once() will be a good move, however, it will go against the ABI expectations of setting the PMCR. I'd like others to weigh in on this as well. What do you think?

Thank you.
Raghavendra

Quoted 45 lines

Thanks,
Shaoqin

Quoted 28 lines
arch/arm64/kvm/sys_regs.c +24 -0
+     if (ret)
+             return ret;
+
+     /*
+      * Ignore writes to RES0 bits, read only bits that are cleared on
+      * vCPU reset, and writable bits that KVM doesn't support yet.
+      * (i.e. only PMCR.N and bits [7:0] are mutable from userspace)
+      * The LP bit is RES0 when FEAT_PMUv3p5 is not supported on the vCPU.
+      * But, we leave the bit as it is here, as the vCPU's PMUver might
+      * be changed later (NOTE: the bit will be cleared on first vCPU run
+      * if necessary).
+      */
+     mutable_mask = (ARMV8_PMU_PMCR_MASK | ARMV8_PMU_PMCR_N);
+     val &= mutable_mask;
+     val |= (__vcpu_sys_reg(vcpu, r->reg) & ~mutable_mask);
+
+     /* The LC bit is RES1 when AArch32 is not supported */
+     if (!kvm_supports_32bit_el0())
+             val |= ARMV8_PMU_PMCR_LC;
+
+     __vcpu_sys_reg(vcpu, r->reg) = val;
+     return 0;
+}
+
  /* Silly macro to expand the DBG{BCR,BVR,WVR,WCR}n_EL1 registers in one go */
  #define DBG_BCR_BVR_WCR_WVR_EL1(n)                                  \
      { SYS_DESC(SYS_DBGBVRn_EL1(n)),                                 \

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

Quoted 10 lines
arch/arm64/kvm/sys_regs.c +2 -2
@@ -2147,8 +2192,8 @@ static const struct sys_reg_desc sys_reg_descs[] = {
      { SYS_DESC(SYS_CTR_EL0), access_ctr },
      { SYS_DESC(SYS_SVCR), undef_access },
 
-     { PMU_SYS_REG(PMCR_EL0), .access = access_pmcr,
-       .reset = reset_pmcr, .reg = PMCR_EL0, .get_user = get_pmcr },
+     { PMU_SYS_REG(PMCR_EL0), .access = access_pmcr, .reset = reset_pmcr,
+       .reg = PMCR_EL0, .get_user = get_pmcr, .set_user = set_pmcr },
      { PMU_SYS_REG(PMCNTENSET_EL0),
        .access = access_pmcnten, .reg = PMCNTENSET_EL0 },
      { PMU_SYS_REG(PMCNTENCLR_EL0),

--
Shaoqin

#27 lore raw

Re: [PATCH v5 08/12] KVM: arm64: PMU: Allow userspace to limit PMCR_EL0.N for the guest

Quoted 94 lines · Raghavendra Rao Ananta
Quoted 84 lines · Shaoqin Huang

Hi Raghavendra,

Quoted 80 lines · Raghavendra Rao Ananta

From: Reiji Watanabe <[email protected]>

KVM does not yet support userspace modifying PMCR_EL0.N (With the previous patch, KVM ignores what is written by upserspace). Add support userspace limiting PMCR_EL0.N.

Disallow userspace to set PMCR_EL0.N to a value that is greater than the host value (KVM_SET_ONE_REG will fail), as KVM doesn't support more event counters than the host HW implements. Although this is an ABI change, this change only affects userspace setting PMCR_EL0.N to a larger value than the host. As accesses to unadvertised event counters indices is CONSTRAINED UNPREDICTABLE behavior, and PMCR_EL0.N was reset to the host value on every vCPU reset before this series, I can't think of any use case where a user space would do that.

Also, ignore writes to read-only bits that are cleared on vCPU reset, and RES{0,1} bits (including writable bits that KVM doesn't support yet), as those bits shouldn't be modified (at least with the current KVM).

---
   arch/arm64/include/asm/kvm_host.h |  3 ++
   arch/arm64/kvm/pmu-emul.c         |  1 +
   arch/arm64/kvm/sys_regs.c         | 49 +++++++++++++++++++++++++++++--
   3 files changed, 51 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_host.h +3 -0
index 0f2dbbe8f6a7e..c15ec365283d1 100644
@@ -259,6 +259,9 @@ struct kvm_arch {
       /* PMCR_EL0.N value for the guest */
       u8 pmcr_n;
 
+     /* Limit value of PMCR_EL0.N for the guest */
+     u8 pmcr_n_limit;
+
       /* Hypercall features firmware registers' descriptor */
       struct kvm_smccc_features smccc_feat;
       struct maple_tree smccc_filter;
arch/arm64/kvm/pmu-emul.c +1 -0
index ce7de6bbdc967..39ad56a71ad20 100644
@@ -896,6 +896,7 @@ int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
        * while the latter does not.
        */
       kvm->arch.pmcr_n = arm_pmu->num_events - 1;
+     kvm->arch.pmcr_n_limit = arm_pmu->num_events - 1;
 
       return 0;
   }
arch/arm64/kvm/sys_regs.c +17 -0
index 2075901356c5b..c01d62afa7db4 100644
@@ -1086,6 +1086,51 @@ static int get_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
       return 0;
   }
 
+static int set_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
+                 u64 val)
+{
+     struct kvm *kvm = vcpu->kvm;
+     u64 new_n, mutable_mask;
+     int ret = 0;
+
+     new_n = FIELD_GET(ARMV8_PMU_PMCR_N, val);
+
+     mutex_lock(&kvm->arch.config_lock);
+     if (unlikely(new_n != kvm->arch.pmcr_n)) {
+             /*
+              * The vCPU can't have more counters than the PMU
+              * hardware implements.
+              */
+             if (new_n <= kvm->arch.pmcr_n_limit)
+                     kvm->arch.pmcr_n = new_n;
Quoted 9 lines

arch/arm64/kvm/sys_regs.c static int get_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, · line 1109

Quoted 4 lines
arch/arm64/kvm/sys_regs.c +4 -0
+             else
+                     ret = -EINVAL;
+     }
+     mutex_unlock(&kvm->arch.config_lock);

Another thing I am just wonder is that should we block any modification to the pmcr_n after vm start to run? Like add one more checking kvm_vm_has_ran_once() at the beginning of the set_pmcr() function.

Quoted 7 lines

Thanks for bringing it up. Reiji and I discussed about this. Checking for kvm_vm_has_ran_once() will be a good move, however, it will go against the ABI expectations of setting the PMCR. I'd like others to weigh in on this as well. What do you think?

Thank you.
Raghavendra

Before this change, kvm not allowed userspace to change the pmcr_n, but allowed to change the lower ARMV8_PMU_PMCR_MASK bits. With this change, we now allow to change the pmcr_n, we should not block the change to ARMV8_PMU_PMCR_MASK after vm start to run, but how about we just block the change to ARMV8_PMU_PMCR_N after vm start to run?

Thanks,
Shaoqin

Quoted 46 lines
Quoted 45 lines

Thanks,
Shaoqin

Quoted 28 lines
arch/arm64/kvm/sys_regs.c +24 -0
+     if (ret)
+             return ret;
+
+     /*
+      * Ignore writes to RES0 bits, read only bits that are cleared on
+      * vCPU reset, and writable bits that KVM doesn't support yet.
+      * (i.e. only PMCR.N and bits [7:0] are mutable from userspace)
+      * The LP bit is RES0 when FEAT_PMUv3p5 is not supported on the vCPU.
+      * But, we leave the bit as it is here, as the vCPU's PMUver might
+      * be changed later (NOTE: the bit will be cleared on first vCPU run
+      * if necessary).
+      */
+     mutable_mask = (ARMV8_PMU_PMCR_MASK | ARMV8_PMU_PMCR_N);
+     val &= mutable_mask;
+     val |= (__vcpu_sys_reg(vcpu, r->reg) & ~mutable_mask);
+
+     /* The LC bit is RES1 when AArch32 is not supported */
+     if (!kvm_supports_32bit_el0())
+             val |= ARMV8_PMU_PMCR_LC;
+
+     __vcpu_sys_reg(vcpu, r->reg) = val;
+     return 0;
+}
+
   /* Silly macro to expand the DBG{BCR,BVR,WVR,WCR}n_EL1 registers in one go */
   #define DBG_BCR_BVR_WCR_WVR_EL1(n)                                  \
       { SYS_DESC(SYS_DBGBVRn_EL1(n)),                                 \

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

Quoted 10 lines
arch/arm64/kvm/sys_regs.c +2 -2
@@ -2147,8 +2192,8 @@ static const struct sys_reg_desc sys_reg_descs[] = {
       { SYS_DESC(SYS_CTR_EL0), access_ctr },
       { SYS_DESC(SYS_SVCR), undef_access },
 
-     { PMU_SYS_REG(PMCR_EL0), .access = access_pmcr,
-       .reset = reset_pmcr, .reg = PMCR_EL0, .get_user = get_pmcr },
+     { PMU_SYS_REG(PMCR_EL0), .access = access_pmcr, .reset = reset_pmcr,
+       .reg = PMCR_EL0, .get_user = get_pmcr, .set_user = set_pmcr },
       { PMU_SYS_REG(PMCNTENSET_EL0),
         .access = access_pmcnten, .reg = PMCNTENSET_EL0 },
       { PMU_SYS_REG(PMCNTENCLR_EL0),

--
Shaoqin

Signature · 1 line
Shaoqin
#28 lore raw

Re: [PATCH v5 08/12] KVM: arm64: PMU: Allow userspace to limit PMCR_EL0.N for the guest

Quoted 103 lines · Shaoqin Huang
Quoted 99 lines · Raghavendra Rao Ananta
Quoted 84 lines · Shaoqin Huang

Hi Raghavendra,

Quoted 80 lines · Raghavendra Rao Ananta

From: Reiji Watanabe <[email protected]>

KVM does not yet support userspace modifying PMCR_EL0.N (With the previous patch, KVM ignores what is written by upserspace). Add support userspace limiting PMCR_EL0.N.

Disallow userspace to set PMCR_EL0.N to a value that is greater than the host value (KVM_SET_ONE_REG will fail), as KVM doesn't support more event counters than the host HW implements. Although this is an ABI change, this change only affects userspace setting PMCR_EL0.N to a larger value than the host. As accesses to unadvertised event counters indices is CONSTRAINED UNPREDICTABLE behavior, and PMCR_EL0.N was reset to the host value on every vCPU reset before this series, I can't think of any use case where a user space would do that.

Also, ignore writes to read-only bits that are cleared on vCPU reset, and RES{0,1} bits (including writable bits that KVM doesn't support yet), as those bits shouldn't be modified (at least with the current KVM).

---
   arch/arm64/include/asm/kvm_host.h |  3 ++
   arch/arm64/kvm/pmu-emul.c         |  1 +
   arch/arm64/kvm/sys_regs.c         | 49 +++++++++++++++++++++++++++++--
   3 files changed, 51 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_host.h +3 -0
index 0f2dbbe8f6a7e..c15ec365283d1 100644
@@ -259,6 +259,9 @@ struct kvm_arch {
       /* PMCR_EL0.N value for the guest */
       u8 pmcr_n;
 
+     /* Limit value of PMCR_EL0.N for the guest */
+     u8 pmcr_n_limit;
+
       /* Hypercall features firmware registers' descriptor */
       struct kvm_smccc_features smccc_feat;
       struct maple_tree smccc_filter;
arch/arm64/kvm/pmu-emul.c +1 -0
index ce7de6bbdc967..39ad56a71ad20 100644
@@ -896,6 +896,7 @@ int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
        * while the latter does not.
        */
       kvm->arch.pmcr_n = arm_pmu->num_events - 1;
+     kvm->arch.pmcr_n_limit = arm_pmu->num_events - 1;
 
       return 0;
   }
arch/arm64/kvm/sys_regs.c +17 -0
index 2075901356c5b..c01d62afa7db4 100644
@@ -1086,6 +1086,51 @@ static int get_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
       return 0;
   }
 
+static int set_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
+                 u64 val)
+{
+     struct kvm *kvm = vcpu->kvm;
+     u64 new_n, mutable_mask;
+     int ret = 0;
+
+     new_n = FIELD_GET(ARMV8_PMU_PMCR_N, val);
+
+     mutex_lock(&kvm->arch.config_lock);
+     if (unlikely(new_n != kvm->arch.pmcr_n)) {
+             /*
+              * The vCPU can't have more counters than the PMU
+              * hardware implements.
+              */
+             if (new_n <= kvm->arch.pmcr_n_limit)
+                     kvm->arch.pmcr_n = new_n;
Quoted 9 lines

arch/arm64/kvm/sys_regs.c static int get_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, · line 1109

Quoted 4 lines
arch/arm64/kvm/sys_regs.c +4 -0
+             else
+                     ret = -EINVAL;
+     }
+     mutex_unlock(&kvm->arch.config_lock);

Another thing I am just wonder is that should we block any modification to the pmcr_n after vm start to run? Like add one more checking kvm_vm_has_ran_once() at the beginning of the set_pmcr() function.

Thanks for bringing it up. Reiji and I discussed about this. Checking for kvm_vm_has_ran_once() will be a good move, however, it will go against the ABI expectations of setting the PMCR. I'd like others to weigh in on this as well. What do you think?

Quoted 9 lines
Quoted 2 lines

Thank you.
Raghavendra

Before this change, kvm not allowed userspace to change the pmcr_n, but allowed to change the lower ARMV8_PMU_PMCR_MASK bits. With this change, we now allow to change the pmcr_n, we should not block the change to ARMV8_PMU_PMCR_MASK after vm start to run, but how about we just block the change to ARMV8_PMU_PMCR_N after vm start to run?

I believe you are referring to the guest trap access part of it (access_pmcr()). This patch focuses on the userspace access of PMCR via the KVM_SET_ONE_REG ioctl. Before this patch, KVM did not control the writes to the reg and userspace was free to write to any bits at any time.

Thank you.
Raghavendra

Quoted 53 lines

Thanks,
Shaoqin

Quoted 34 lines
Quoted 34 lines

Thanks,
Shaoqin

Quoted 31 lines
arch/arm64/kvm/sys_regs.c +24 -0
+     if (ret)
+             return ret;
+
+     /*
+      * Ignore writes to RES0 bits, read only bits that are cleared on
+      * vCPU reset, and writable bits that KVM doesn't support yet.
+      * (i.e. only PMCR.N and bits [7:0] are mutable from userspace)
+      * The LP bit is RES0 when FEAT_PMUv3p5 is not supported on the vCPU.
+      * But, we leave the bit as it is here, as the vCPU's PMUver might
+      * be changed later (NOTE: the bit will be cleared on first vCPU run
+      * if necessary).
+      */
+     mutable_mask = (ARMV8_PMU_PMCR_MASK | ARMV8_PMU_PMCR_N);
+     val &= mutable_mask;
+     val |= (__vcpu_sys_reg(vcpu, r->reg) & ~mutable_mask);
+
+     /* The LC bit is RES1 when AArch32 is not supported */
+     if (!kvm_supports_32bit_el0())
+             val |= ARMV8_PMU_PMCR_LC;
+
+     __vcpu_sys_reg(vcpu, r->reg) = val;
+     return 0;
+}
+
   /* Silly macro to expand the DBG{BCR,BVR,WVR,WCR}n_EL1 registers in one go */
   #define DBG_BCR_BVR_WCR_WVR_EL1(n)                                  \
       { SYS_DESC(SYS_DBGBVRn_EL1(n)),                                 \
@@ -2147,8 +2192,8 @@ static const struct sys_reg_desc sys_reg_descs[] = {
       { SYS_DESC(SYS_CTR_EL0), access_ctr },
       { SYS_DESC(SYS_SVCR), undef_access },
 
Quoted 12 lines
Quoted 11 lines

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

Quoted 7 lines
arch/arm64/kvm/sys_regs.c +2 -2
-     { PMU_SYS_REG(PMCR_EL0), .access = access_pmcr,
-       .reset = reset_pmcr, .reg = PMCR_EL0, .get_user = get_pmcr },
+     { PMU_SYS_REG(PMCR_EL0), .access = access_pmcr, .reset = reset_pmcr,
+       .reg = PMCR_EL0, .get_user = get_pmcr, .set_user = set_pmcr },
       { PMU_SYS_REG(PMCNTENSET_EL0),
         .access = access_pmcnten, .reg = PMCNTENSET_EL0 },
       { PMU_SYS_REG(PMCNTENCLR_EL0),

--
Shaoqin

--
Shaoqin

#29 lore raw

Re: [PATCH v5 08/12] KVM: arm64: PMU: Allow userspace to limit PMCR_EL0.N for the guest

Quoted 113 lines · Raghavendra Rao Ananta
Quoted 103 lines · Shaoqin Huang
Quoted 99 lines · Raghavendra Rao Ananta
Quoted 84 lines · Shaoqin Huang

Hi Raghavendra,

Quoted 80 lines · Raghavendra Rao Ananta

From: Reiji Watanabe <[email protected]>

KVM does not yet support userspace modifying PMCR_EL0.N (With the previous patch, KVM ignores what is written by upserspace). Add support userspace limiting PMCR_EL0.N.

Disallow userspace to set PMCR_EL0.N to a value that is greater than the host value (KVM_SET_ONE_REG will fail), as KVM doesn't support more event counters than the host HW implements. Although this is an ABI change, this change only affects userspace setting PMCR_EL0.N to a larger value than the host. As accesses to unadvertised event counters indices is CONSTRAINED UNPREDICTABLE behavior, and PMCR_EL0.N was reset to the host value on every vCPU reset before this series, I can't think of any use case where a user space would do that.

Also, ignore writes to read-only bits that are cleared on vCPU reset, and RES{0,1} bits (including writable bits that KVM doesn't support yet), as those bits shouldn't be modified (at least with the current KVM).

---
    arch/arm64/include/asm/kvm_host.h |  3 ++
    arch/arm64/kvm/pmu-emul.c         |  1 +
    arch/arm64/kvm/sys_regs.c         | 49 +++++++++++++++++++++++++++++--
    3 files changed, 51 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_host.h +3 -0
index 0f2dbbe8f6a7e..c15ec365283d1 100644
@@ -259,6 +259,9 @@ struct kvm_arch {
        /* PMCR_EL0.N value for the guest */
        u8 pmcr_n;
 
+     /* Limit value of PMCR_EL0.N for the guest */
+     u8 pmcr_n_limit;
+
        /* Hypercall features firmware registers' descriptor */
        struct kvm_smccc_features smccc_feat;
        struct maple_tree smccc_filter;
arch/arm64/kvm/pmu-emul.c +1 -0
index ce7de6bbdc967..39ad56a71ad20 100644
@@ -896,6 +896,7 @@ int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
         * while the latter does not.
         */
        kvm->arch.pmcr_n = arm_pmu->num_events - 1;
+     kvm->arch.pmcr_n_limit = arm_pmu->num_events - 1;
 
        return 0;
    }
arch/arm64/kvm/sys_regs.c +17 -0
index 2075901356c5b..c01d62afa7db4 100644
@@ -1086,6 +1086,51 @@ static int get_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
        return 0;
    }
 
+static int set_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
+                 u64 val)
+{
+     struct kvm *kvm = vcpu->kvm;
+     u64 new_n, mutable_mask;
+     int ret = 0;
+
+     new_n = FIELD_GET(ARMV8_PMU_PMCR_N, val);
+
+     mutex_lock(&kvm->arch.config_lock);
+     if (unlikely(new_n != kvm->arch.pmcr_n)) {
+             /*
+              * The vCPU can't have more counters than the PMU
+              * hardware implements.
+              */
+             if (new_n <= kvm->arch.pmcr_n_limit)
+                     kvm->arch.pmcr_n = new_n;
Quoted 9 lines

arch/arm64/kvm/sys_regs.c static int get_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, · line 1109

Quoted 4 lines
arch/arm64/kvm/sys_regs.c +4 -0
+             else
+                     ret = -EINVAL;
+     }
+     mutex_unlock(&kvm->arch.config_lock);

Another thing I am just wonder is that should we block any modification to the pmcr_n after vm start to run? Like add one more checking kvm_vm_has_ran_once() at the beginning of the set_pmcr() function.

Thanks for bringing it up. Reiji and I discussed about this. Checking for kvm_vm_has_ran_once() will be a good move, however, it will go against the ABI expectations of setting the PMCR. I'd like others to weigh in on this as well. What do you think?

Quoted 9 lines
Quoted 2 lines

Thank you.
Raghavendra

Before this change, kvm not allowed userspace to change the pmcr_n, but allowed to change the lower ARMV8_PMU_PMCR_MASK bits. With this change, we now allow to change the pmcr_n, we should not block the change to ARMV8_PMU_PMCR_MASK after vm start to run, but how about we just block the change to ARMV8_PMU_PMCR_N after vm start to run?

Quoted 6 lines

I believe you are referring to the guest trap access part of it (access_pmcr()). This patch focuses on the userspace access of PMCR via the KVM_SET_ONE_REG ioctl. Before this patch, KVM did not control the writes to the reg and userspace was free to write to any bits at any time.

Oh yeah. Thanks for your explanation. My head sometimes broken down.

Thanks,
Shaoqin

Quoted 56 lines

Thank you.
Raghavendra

Quoted 53 lines

Thanks,
Shaoqin

Quoted 34 lines
Quoted 34 lines

Thanks,
Shaoqin

Quoted 31 lines
arch/arm64/kvm/sys_regs.c +24 -0
+     if (ret)
+             return ret;
+
+     /*
+      * Ignore writes to RES0 bits, read only bits that are cleared on
+      * vCPU reset, and writable bits that KVM doesn't support yet.
+      * (i.e. only PMCR.N and bits [7:0] are mutable from userspace)
+      * The LP bit is RES0 when FEAT_PMUv3p5 is not supported on the vCPU.
+      * But, we leave the bit as it is here, as the vCPU's PMUver might
+      * be changed later (NOTE: the bit will be cleared on first vCPU run
+      * if necessary).
+      */
+     mutable_mask = (ARMV8_PMU_PMCR_MASK | ARMV8_PMU_PMCR_N);
+     val &= mutable_mask;
+     val |= (__vcpu_sys_reg(vcpu, r->reg) & ~mutable_mask);
+
+     /* The LC bit is RES1 when AArch32 is not supported */
+     if (!kvm_supports_32bit_el0())
+             val |= ARMV8_PMU_PMCR_LC;
+
+     __vcpu_sys_reg(vcpu, r->reg) = val;
+     return 0;
+}
+
    /* Silly macro to expand the DBG{BCR,BVR,WVR,WCR}n_EL1 registers in one go */
    #define DBG_BCR_BVR_WCR_WVR_EL1(n)                                  \
        { SYS_DESC(SYS_DBGBVRn_EL1(n)),                                 \
@@ -2147,8 +2192,8 @@ static const struct sys_reg_desc sys_reg_descs[] = {
        { SYS_DESC(SYS_CTR_EL0), access_ctr },
        { SYS_DESC(SYS_SVCR), undef_access },
 
Quoted 12 lines
Quoted 11 lines

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

Quoted 7 lines
arch/arm64/kvm/sys_regs.c +2 -2
-     { PMU_SYS_REG(PMCR_EL0), .access = access_pmcr,
-       .reset = reset_pmcr, .reg = PMCR_EL0, .get_user = get_pmcr },
+     { PMU_SYS_REG(PMCR_EL0), .access = access_pmcr, .reset = reset_pmcr,
+       .reg = PMCR_EL0, .get_user = get_pmcr, .set_user = set_pmcr },
        { PMU_SYS_REG(PMCNTENSET_EL0),
          .access = access_pmcnten, .reg = PMCNTENSET_EL0 },
        { PMU_SYS_REG(PMCNTENCLR_EL0),

--
Shaoqin

--
Shaoqin

Signature · 1 line
Shaoqin
#30 lore raw

Re: [PATCH v5 08/12] KVM: arm64: PMU: Allow userspace to limit PMCR_EL0.N for the guest

In reply to Raghavendra Rao Ananta

Hi Raghu,

Quoted 4 lines · Raghavendra Rao Ananta

From: Reiji Watanabe <[email protected]>

KVM does not yet support userspace modifying PMCR_EL0.N (With the previous patch, KVM ignores what is written by upserspace).

typo: userspace

arch/arm64/kvm/pmu-emul.c int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu) · line 899

Quoted 9 lines
arch/arm64/kvm/pmu-emul.c +1 -0
index ce7de6bbdc967..39ad56a71ad20 100644
@@ -896,6 +896,7 @@ int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
 	 * while the latter does not.
 	 */
 	kvm->arch.pmcr_n = arm_pmu->num_events - 1;
+	kvm->arch.pmcr_n_limit = arm_pmu->num_events - 1;

Can't we just get at this through the arm_pmu instance rather than copying it into kvm_arch?

Quoted 20 lines
arch/arm64/kvm/pmu-emul.c +0 -0
 	return 0;
 }
arch/arm64/kvm/sys_regs.c +10 -0
index 2075901356c5b..c01d62afa7db4 100644
@@ -1086,6 +1086,51 @@ static int get_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
 	return 0;
 }
 
+static int set_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
+		    u64 val)
+{
+	struct kvm *kvm = vcpu->kvm;
+	u64 new_n, mutable_mask;
+	int ret = 0;
+
+	new_n = FIELD_GET(ARMV8_PMU_PMCR_N, val);
+
+	mutex_lock(&kvm->arch.config_lock);

arch/arm64/kvm/sys_regs.c static int get_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, · line 1108

Quoted 10 lines
arch/arm64/kvm/sys_regs.c +10 -0
+	if (unlikely(new_n != kvm->arch.pmcr_n)) {
+		/*
+		 * The vCPU can't have more counters than the PMU
+		 * hardware implements.
+		 */
+		if (new_n <= kvm->arch.pmcr_n_limit)
+			kvm->arch.pmcr_n = new_n;
+		else
+			ret = -EINVAL;
+	}

Hmm, I'm not so sure about returning an error here. ABI has it that userspace can write any value to PMCR_EL0 successfully. Can we just ignore writes that attempt to set PMCR_EL0.N to something higher than supported by hardware? Our general stance should be that system register fields responsible for feature identification are immutable after the VM has started.

Signature · 2 lines
Thanks,
Oliver
#31 lore raw

Re: [PATCH v5 08/12] KVM: arm64: PMU: Allow userspace to limit PMCR_EL0.N for the guest

Quoted 52 lines · Oliver Upton

Hi Raghu,

Quoted 4 lines · Raghavendra Rao Ananta

From: Reiji Watanabe <[email protected]>

KVM does not yet support userspace modifying PMCR_EL0.N (With the previous patch, KVM ignores what is written by upserspace).

typo: userspace

arch/arm64/kvm/pmu-emul.c int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu) · line 899

Quoted 9 lines
arch/arm64/kvm/pmu-emul.c +1 -0
index ce7de6bbdc967..39ad56a71ad20 100644
@@ -896,6 +896,7 @@ int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
 	 * while the latter does not.
 	 */
 	kvm->arch.pmcr_n = arm_pmu->num_events - 1;
+	kvm->arch.pmcr_n_limit = arm_pmu->num_events - 1;

Can't we just get at this through the arm_pmu instance rather than copying it into kvm_arch?

Quoted 29 lines
arch/arm64/kvm/pmu-emul.c +0 -0
 	return 0;
 }
arch/arm64/kvm/sys_regs.c +19 -0
index 2075901356c5b..c01d62afa7db4 100644
@@ -1086,6 +1086,51 @@ static int get_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
 	return 0;
 }
 
+static int set_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
+		    u64 val)
+{
+	struct kvm *kvm = vcpu->kvm;
+	u64 new_n, mutable_mask;
+	int ret = 0;
+
+	new_n = FIELD_GET(ARMV8_PMU_PMCR_N, val);
+
+	mutex_lock(&kvm->arch.config_lock);
+	if (unlikely(new_n != kvm->arch.pmcr_n)) {
+		/*
+		 * The vCPU can't have more counters than the PMU
+		 * hardware implements.
+		 */
+		if (new_n <= kvm->arch.pmcr_n_limit)
+			kvm->arch.pmcr_n = new_n;
+		else
+			ret = -EINVAL;
Quoted 8 lines

arch/arm64/kvm/sys_regs.c static int get_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, · line 1108

Quoted 1 line
arch/arm64/kvm/sys_regs.c +1 -0
+	}

Hmm, I'm not so sure about returning an error here. ABI has it that userspace can write any value to PMCR_EL0 successfully. Can we just ignore writes that attempt to set PMCR_EL0.N to something higher than supported by hardware? Our general stance should be that system register fields responsible for feature identification are immutable after the VM has started.

I hacked up my reply and dropped some context; this doesn't read right. Shaoqin made the point about preventing changes to PMCR_EL0.N after the VM has started and I firmly agree. The behavior should be:

- Writes to PMCR always succeed

- PMCR_EL0.N values greater than what's supported by hardware are ignored

- Changes to N after the VM has started are ignored.

Signature · 2 lines
Thanks,
Oliver
#32 lore raw

Re: [PATCH v5 08/12] KVM: arm64: PMU: Allow userspace to limit PMCR_EL0.N for the guest

Quoted 67 lines · Oliver Upton
Quoted 52 lines · Oliver Upton

Hi Raghu,

Quoted 4 lines · Raghavendra Rao Ananta

From: Reiji Watanabe <[email protected]>

KVM does not yet support userspace modifying PMCR_EL0.N (With the previous patch, KVM ignores what is written by upserspace).

typo: userspace

arch/arm64/kvm/pmu-emul.c int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu) · line 899

Quoted 9 lines
arch/arm64/kvm/pmu-emul.c +1 -0
index ce7de6bbdc967..39ad56a71ad20 100644
@@ -896,6 +896,7 @@ int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
     * while the latter does not.
     */
    kvm->arch.pmcr_n = arm_pmu->num_events - 1;
+   kvm->arch.pmcr_n_limit = arm_pmu->num_events - 1;

Can't we just get at this through the arm_pmu instance rather than copying it into kvm_arch?

Quoted 29 lines
arch/arm64/kvm/pmu-emul.c +0 -0
    return 0;
 }
arch/arm64/kvm/sys_regs.c +19 -0
index 2075901356c5b..c01d62afa7db4 100644
@@ -1086,6 +1086,51 @@ static int get_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
    return 0;
 }
 
+static int set_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
+               u64 val)
+{
+   struct kvm *kvm = vcpu->kvm;
+   u64 new_n, mutable_mask;
+   int ret = 0;
+
+   new_n = FIELD_GET(ARMV8_PMU_PMCR_N, val);
+
+   mutex_lock(&kvm->arch.config_lock);
+   if (unlikely(new_n != kvm->arch.pmcr_n)) {
+           /*
+            * The vCPU can't have more counters than the PMU
+            * hardware implements.
+            */
+           if (new_n <= kvm->arch.pmcr_n_limit)
+                   kvm->arch.pmcr_n = new_n;
+           else
+                   ret = -EINVAL;
Quoted 8 lines

arch/arm64/kvm/sys_regs.c static int get_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, · line 1108

Quoted 1 line
arch/arm64/kvm/sys_regs.c +1 -0
+   }

Hmm, I'm not so sure about returning an error here. ABI has it that userspace can write any value to PMCR_EL0 successfully. Can we just ignore writes that attempt to set PMCR_EL0.N to something higher than supported by hardware? Our general stance should be that system register fields responsible for feature identification are immutable after the VM has started.

I hacked up my reply and dropped some context; this doesn't read right. Shaoqin made the point about preventing changes to PMCR_EL0.N after the VM has started and I firmly agree. The behavior should be:

Quoted 7 lines

- Writes to PMCR always succeed

- PMCR_EL0.N values greater than what's supported by hardware are ignored

- Changes to N after the VM has started are ignored.

Reiji and I were wondering if we should proceed with this as this would change userspace expectation. BTW, when you said "ignored", does that mean we silently return to userspace with a success or with EBUSY (changing the expectations)?

Thank you.
Raghavendra

Quoted 3 lines

--
Thanks,
Oliver

#33 lore raw

Re: [PATCH v5 08/12] KVM: arm64: PMU: Allow userspace to limit PMCR_EL0.N for the guest

Hi Oliver,

Quoted 74 lines · Raghavendra Rao Ananta
Quoted 72 lines · Oliver Upton
Quoted 52 lines · Oliver Upton

Hi Raghu,

Quoted 4 lines · Raghavendra Rao Ananta

From: Reiji Watanabe <[email protected]>

KVM does not yet support userspace modifying PMCR_EL0.N (With the previous patch, KVM ignores what is written by upserspace).

typo: userspace

arch/arm64/kvm/pmu-emul.c int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu) · line 899

Quoted 9 lines
arch/arm64/kvm/pmu-emul.c +1 -0
index ce7de6bbdc967..39ad56a71ad20 100644
@@ -896,6 +896,7 @@ int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
     * while the latter does not.
     */
    kvm->arch.pmcr_n = arm_pmu->num_events - 1;
+   kvm->arch.pmcr_n_limit = arm_pmu->num_events - 1;

Can't we just get at this through the arm_pmu instance rather than copying it into kvm_arch?

Quoted 29 lines
arch/arm64/kvm/pmu-emul.c +0 -0
    return 0;
 }
arch/arm64/kvm/sys_regs.c +19 -0
index 2075901356c5b..c01d62afa7db4 100644
@@ -1086,6 +1086,51 @@ static int get_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
    return 0;
 }
 
+static int set_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
+               u64 val)
+{
+   struct kvm *kvm = vcpu->kvm;
+   u64 new_n, mutable_mask;
+   int ret = 0;
+
+   new_n = FIELD_GET(ARMV8_PMU_PMCR_N, val);
+
+   mutex_lock(&kvm->arch.config_lock);
+   if (unlikely(new_n != kvm->arch.pmcr_n)) {
+           /*
+            * The vCPU can't have more counters than the PMU
+            * hardware implements.
+            */
+           if (new_n <= kvm->arch.pmcr_n_limit)
+                   kvm->arch.pmcr_n = new_n;
+           else
+                   ret = -EINVAL;
Quoted 8 lines

arch/arm64/kvm/sys_regs.c static int get_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, · line 1108

Quoted 1 line
arch/arm64/kvm/sys_regs.c +1 -0
+   }

Hmm, I'm not so sure about returning an error here. ABI has it that userspace can write any value to PMCR_EL0 successfully. Can we just ignore writes that attempt to set PMCR_EL0.N to something higher than supported by hardware? Our general stance should be that system register fields responsible for feature identification are immutable after the VM has started.

I hacked up my reply and dropped some context; this doesn't read right. Shaoqin made the point about preventing changes to PMCR_EL0.N after the VM has started and I firmly agree. The behavior should be:

- Writes to PMCR always succeed

- PMCR_EL0.N values greater than what's supported by hardware are ignored

Quoted 7 lines
Quoted 2 lines

- Changes to N after the VM has started are ignored.

Reiji and I were wondering if we should proceed with this as this would change userspace expectation. BTW, when you said "ignored", does that mean we silently return to userspace with a success or with EBUSY (changing the expectations)?

Sorry, I just read your earlier comment (one before you detailed the behavior), from which I'm guessing "ignore" means simply disregard the change and return success to userspace. But wouldn't that cause issues in debugging?

Thank you.
Raghavendra

Quoted 5 lines

Thank you.
Raghavendra

Quoted 3 lines

--
Thanks,
Oliver

#34 lore raw

Re: [PATCH v5 08/12] KVM: arm64: PMU: Allow userspace to limit PMCR_EL0.N for the guest

In reply to Oliver Upton
Quoted 11 lines · Oliver Upton

Hi Raghu,

Quoted 4 lines · Raghavendra Rao Ananta

From: Reiji Watanabe <[email protected]>

KVM does not yet support userspace modifying PMCR_EL0.N (With the previous patch, KVM ignores what is written by upserspace).

typo: userspace

Noted.

Quoted 4 lines
Quoted 4 lines
arch/arm64/kvm/pmu-emul.c +0 -0
index ce7de6bbdc967..39ad56a71ad20 100644
Quoted 9 lines

arch/arm64/kvm/pmu-emul.c int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu) · line 899

Quoted 5 lines
arch/arm64/kvm/pmu-emul.c +1 -0
@@ -896,6 +896,7 @@ int kvm_arm_set_vm_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
       * while the latter does not.
       */
      kvm->arch.pmcr_n = arm_pmu->num_events - 1;
+     kvm->arch.pmcr_n_limit = arm_pmu->num_events - 1;

Can't we just get at this through the arm_pmu instance rather than copying it into kvm_arch?

Yeah, I suppose we can directly access it in set_pmcr().

Thank you.
Raghavendra

Quoted 41 lines
Quoted 20 lines
arch/arm64/kvm/pmu-emul.c +0 -0
      return 0;
 }
arch/arm64/kvm/sys_regs.c +10 -0
index 2075901356c5b..c01d62afa7db4 100644
@@ -1086,6 +1086,51 @@ static int get_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
      return 0;
 }
 
+static int set_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
+                 u64 val)
+{
+     struct kvm *kvm = vcpu->kvm;
+     u64 new_n, mutable_mask;
+     int ret = 0;
+
+     new_n = FIELD_GET(ARMV8_PMU_PMCR_N, val);
+
+     mutex_lock(&kvm->arch.config_lock);

arch/arm64/kvm/sys_regs.c static int get_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, · line 1108

Quoted 10 lines
arch/arm64/kvm/sys_regs.c +10 -0
+     if (unlikely(new_n != kvm->arch.pmcr_n)) {
+             /*
+              * The vCPU can't have more counters than the PMU
+              * hardware implements.
+              */
+             if (new_n <= kvm->arch.pmcr_n_limit)
+                     kvm->arch.pmcr_n = new_n;
+             else
+                     ret = -EINVAL;
+     }

Hmm, I'm not so sure about returning an error here. ABI has it that userspace can write any value to PMCR_EL0 successfully. Can we just ignore writes that attempt to set PMCR_EL0.N to something higher than supported by hardware? Our general stance should be that system register fields responsible for feature identification are immutable after the VM has started.

--
Thanks,
Oliver

#35 lore raw

PATCH v5 9/12 tools: Import arm_pmuv3.h

In reply to Raghavendra Rao Ananta

Import kernel's include/linux/perf/arm_pmuv3.h, with the definition of PMEVN_SWITCH() additionally including an assert() for the 'default' case. The following patches will use macros defined in this header.

---
 tools/include/perf/arm_pmuv3.h | 308 +++++++++++++++++++++++++++++++++
 1 file changed, 308 insertions(+)
 create mode 100644 tools/include/perf/arm_pmuv3.h
tools/include/perf/arm_pmuv3.h new file +308 -0
new file mode 100644
index 0000000000000..1567a772abe27
@@ -0,0 +1,308 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2012 ARM Ltd.
+ */
+
+#ifndef __PERF_ARM_PMUV3_H
+#define __PERF_ARM_PMUV3_H
+
+#include <assert.h>
+#include <asm/bug.h>
+
+#define ARMV8_PMU_MAX_COUNTERS	32
+#define ARMV8_PMU_COUNTER_MASK	(ARMV8_PMU_MAX_COUNTERS - 1)
+
+/*
+ * Common architectural and microarchitectural event numbers.
+ */
+#define ARMV8_PMUV3_PERFCTR_SW_INCR				0x0000
+#define ARMV8_PMUV3_PERFCTR_L1I_CACHE_REFILL			0x0001
+#define ARMV8_PMUV3_PERFCTR_L1I_TLB_REFILL			0x0002
+#define ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL			0x0003
+#define ARMV8_PMUV3_PERFCTR_L1D_CACHE				0x0004
+#define ARMV8_PMUV3_PERFCTR_L1D_TLB_REFILL			0x0005
+#define ARMV8_PMUV3_PERFCTR_LD_RETIRED				0x0006
+#define ARMV8_PMUV3_PERFCTR_ST_RETIRED				0x0007
+#define ARMV8_PMUV3_PERFCTR_INST_RETIRED			0x0008
+#define ARMV8_PMUV3_PERFCTR_EXC_TAKEN				0x0009
+#define ARMV8_PMUV3_PERFCTR_EXC_RETURN				0x000A
+#define ARMV8_PMUV3_PERFCTR_CID_WRITE_RETIRED			0x000B
+#define ARMV8_PMUV3_PERFCTR_PC_WRITE_RETIRED			0x000C
+#define ARMV8_PMUV3_PERFCTR_BR_IMMED_RETIRED			0x000D
+#define ARMV8_PMUV3_PERFCTR_BR_RETURN_RETIRED			0x000E
+#define ARMV8_PMUV3_PERFCTR_UNALIGNED_LDST_RETIRED		0x000F
+#define ARMV8_PMUV3_PERFCTR_BR_MIS_PRED				0x0010
+#define ARMV8_PMUV3_PERFCTR_CPU_CYCLES				0x0011
+#define ARMV8_PMUV3_PERFCTR_BR_PRED				0x0012
+#define ARMV8_PMUV3_PERFCTR_MEM_ACCESS				0x0013
+#define ARMV8_PMUV3_PERFCTR_L1I_CACHE				0x0014
+#define ARMV8_PMUV3_PERFCTR_L1D_CACHE_WB			0x0015
+#define ARMV8_PMUV3_PERFCTR_L2D_CACHE				0x0016
+#define ARMV8_PMUV3_PERFCTR_L2D_CACHE_REFILL			0x0017
+#define ARMV8_PMUV3_PERFCTR_L2D_CACHE_WB			0x0018
+#define ARMV8_PMUV3_PERFCTR_BUS_ACCESS				0x0019
+#define ARMV8_PMUV3_PERFCTR_MEMORY_ERROR			0x001A
+#define ARMV8_PMUV3_PERFCTR_INST_SPEC				0x001B
+#define ARMV8_PMUV3_PERFCTR_TTBR_WRITE_RETIRED			0x001C
+#define ARMV8_PMUV3_PERFCTR_BUS_CYCLES				0x001D
+#define ARMV8_PMUV3_PERFCTR_CHAIN				0x001E
+#define ARMV8_PMUV3_PERFCTR_L1D_CACHE_ALLOCATE			0x001F
+#define ARMV8_PMUV3_PERFCTR_L2D_CACHE_ALLOCATE			0x0020
+#define ARMV8_PMUV3_PERFCTR_BR_RETIRED				0x0021
+#define ARMV8_PMUV3_PERFCTR_BR_MIS_PRED_RETIRED			0x0022
+#define ARMV8_PMUV3_PERFCTR_STALL_FRONTEND			0x0023
+#define ARMV8_PMUV3_PERFCTR_STALL_BACKEND			0x0024
+#define ARMV8_PMUV3_PERFCTR_L1D_TLB				0x0025
+#define ARMV8_PMUV3_PERFCTR_L1I_TLB				0x0026
+#define ARMV8_PMUV3_PERFCTR_L2I_CACHE				0x0027
+#define ARMV8_PMUV3_PERFCTR_L2I_CACHE_REFILL			0x0028
+#define ARMV8_PMUV3_PERFCTR_L3D_CACHE_ALLOCATE			0x0029
+#define ARMV8_PMUV3_PERFCTR_L3D_CACHE_REFILL			0x002A
+#define ARMV8_PMUV3_PERFCTR_L3D_CACHE				0x002B
+#define ARMV8_PMUV3_PERFCTR_L3D_CACHE_WB			0x002C
+#define ARMV8_PMUV3_PERFCTR_L2D_TLB_REFILL			0x002D
+#define ARMV8_PMUV3_PERFCTR_L2I_TLB_REFILL			0x002E
+#define ARMV8_PMUV3_PERFCTR_L2D_TLB				0x002F
+#define ARMV8_PMUV3_PERFCTR_L2I_TLB				0x0030
+#define ARMV8_PMUV3_PERFCTR_REMOTE_ACCESS			0x0031
+#define ARMV8_PMUV3_PERFCTR_LL_CACHE				0x0032
+#define ARMV8_PMUV3_PERFCTR_LL_CACHE_MISS			0x0033
+#define ARMV8_PMUV3_PERFCTR_DTLB_WALK				0x0034
+#define ARMV8_PMUV3_PERFCTR_ITLB_WALK				0x0035
+#define ARMV8_PMUV3_PERFCTR_LL_CACHE_RD				0x0036
+#define ARMV8_PMUV3_PERFCTR_LL_CACHE_MISS_RD			0x0037
+#define ARMV8_PMUV3_PERFCTR_REMOTE_ACCESS_RD			0x0038
+#define ARMV8_PMUV3_PERFCTR_L1D_CACHE_LMISS_RD			0x0039
+#define ARMV8_PMUV3_PERFCTR_OP_RETIRED				0x003A
+#define ARMV8_PMUV3_PERFCTR_OP_SPEC				0x003B
+#define ARMV8_PMUV3_PERFCTR_STALL				0x003C
+#define ARMV8_PMUV3_PERFCTR_STALL_SLOT_BACKEND			0x003D
+#define ARMV8_PMUV3_PERFCTR_STALL_SLOT_FRONTEND			0x003E
+#define ARMV8_PMUV3_PERFCTR_STALL_SLOT				0x003F
+
+/* Statistical profiling extension microarchitectural events */
+#define ARMV8_SPE_PERFCTR_SAMPLE_POP				0x4000
+#define ARMV8_SPE_PERFCTR_SAMPLE_FEED				0x4001
+#define ARMV8_SPE_PERFCTR_SAMPLE_FILTRATE			0x4002
+#define ARMV8_SPE_PERFCTR_SAMPLE_COLLISION			0x4003
+
+/* AMUv1 architecture events */
+#define ARMV8_AMU_PERFCTR_CNT_CYCLES				0x4004
+#define ARMV8_AMU_PERFCTR_STALL_BACKEND_MEM			0x4005
+
+/* long-latency read miss events */
+#define ARMV8_PMUV3_PERFCTR_L1I_CACHE_LMISS			0x4006
+#define ARMV8_PMUV3_PERFCTR_L2D_CACHE_LMISS_RD			0x4009
+#define ARMV8_PMUV3_PERFCTR_L2I_CACHE_LMISS			0x400A
+#define ARMV8_PMUV3_PERFCTR_L3D_CACHE_LMISS_RD			0x400B
+
+/* Trace buffer events */
+#define ARMV8_PMUV3_PERFCTR_TRB_WRAP				0x400C
+#define ARMV8_PMUV3_PERFCTR_TRB_TRIG				0x400E
+
+/* Trace unit events */
+#define ARMV8_PMUV3_PERFCTR_TRCEXTOUT0				0x4010
+#define ARMV8_PMUV3_PERFCTR_TRCEXTOUT1				0x4011
+#define ARMV8_PMUV3_PERFCTR_TRCEXTOUT2				0x4012
+#define ARMV8_PMUV3_PERFCTR_TRCEXTOUT3				0x4013
+#define ARMV8_PMUV3_PERFCTR_CTI_TRIGOUT4			0x4018
+#define ARMV8_PMUV3_PERFCTR_CTI_TRIGOUT5			0x4019
+#define ARMV8_PMUV3_PERFCTR_CTI_TRIGOUT6			0x401A
+#define ARMV8_PMUV3_PERFCTR_CTI_TRIGOUT7			0x401B
+
+/* additional latency from alignment events */
+#define ARMV8_PMUV3_PERFCTR_LDST_ALIGN_LAT			0x4020
+#define ARMV8_PMUV3_PERFCTR_LD_ALIGN_LAT			0x4021
+#define ARMV8_PMUV3_PERFCTR_ST_ALIGN_LAT			0x4022
+
+/* Armv8.5 Memory Tagging Extension events */
+#define ARMV8_MTE_PERFCTR_MEM_ACCESS_CHECKED			0x4024
+#define ARMV8_MTE_PERFCTR_MEM_ACCESS_CHECKED_RD			0x4025
+#define ARMV8_MTE_PERFCTR_MEM_ACCESS_CHECKED_WR			0x4026
+
+/* ARMv8 recommended implementation defined event types */
+#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_RD			0x0040
+#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WR			0x0041
+#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_RD		0x0042
+#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_WR		0x0043
+#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_INNER		0x0044
+#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_OUTER		0x0045
+#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WB_VICTIM		0x0046
+#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WB_CLEAN			0x0047
+#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_INVAL			0x0048
+
+#define ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_RD			0x004C
+#define ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_WR			0x004D
+#define ARMV8_IMPDEF_PERFCTR_L1D_TLB_RD				0x004E
+#define ARMV8_IMPDEF_PERFCTR_L1D_TLB_WR				0x004F
+#define ARMV8_IMPDEF_PERFCTR_L2D_CACHE_RD			0x0050
+#define ARMV8_IMPDEF_PERFCTR_L2D_CACHE_WR			0x0051
+#define ARMV8_IMPDEF_PERFCTR_L2D_CACHE_REFILL_RD		0x0052
+#define ARMV8_IMPDEF_PERFCTR_L2D_CACHE_REFILL_WR		0x0053
+
+#define ARMV8_IMPDEF_PERFCTR_L2D_CACHE_WB_VICTIM		0x0056
+#define ARMV8_IMPDEF_PERFCTR_L2D_CACHE_WB_CLEAN			0x0057
+#define ARMV8_IMPDEF_PERFCTR_L2D_CACHE_INVAL			0x0058
+
+#define ARMV8_IMPDEF_PERFCTR_L2D_TLB_REFILL_RD			0x005C
+#define ARMV8_IMPDEF_PERFCTR_L2D_TLB_REFILL_WR			0x005D
+#define ARMV8_IMPDEF_PERFCTR_L2D_TLB_RD				0x005E
+#define ARMV8_IMPDEF_PERFCTR_L2D_TLB_WR				0x005F
+#define ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_RD			0x0060
+#define ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_WR			0x0061
+#define ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_SHARED			0x0062
+#define ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_NOT_SHARED		0x0063
+#define ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_NORMAL			0x0064
+#define ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_PERIPH			0x0065
+#define ARMV8_IMPDEF_PERFCTR_MEM_ACCESS_RD			0x0066
+#define ARMV8_IMPDEF_PERFCTR_MEM_ACCESS_WR			0x0067
+#define ARMV8_IMPDEF_PERFCTR_UNALIGNED_LD_SPEC			0x0068
+#define ARMV8_IMPDEF_PERFCTR_UNALIGNED_ST_SPEC			0x0069
+#define ARMV8_IMPDEF_PERFCTR_UNALIGNED_LDST_SPEC		0x006A
+
+#define ARMV8_IMPDEF_PERFCTR_LDREX_SPEC				0x006C
+#define ARMV8_IMPDEF_PERFCTR_STREX_PASS_SPEC			0x006D
+#define ARMV8_IMPDEF_PERFCTR_STREX_FAIL_SPEC			0x006E
+#define ARMV8_IMPDEF_PERFCTR_STREX_SPEC				0x006F
+#define ARMV8_IMPDEF_PERFCTR_LD_SPEC				0x0070
+#define ARMV8_IMPDEF_PERFCTR_ST_SPEC				0x0071
+#define ARMV8_IMPDEF_PERFCTR_LDST_SPEC				0x0072
+#define ARMV8_IMPDEF_PERFCTR_DP_SPEC				0x0073
+#define ARMV8_IMPDEF_PERFCTR_ASE_SPEC				0x0074
+#define ARMV8_IMPDEF_PERFCTR_VFP_SPEC				0x0075
+#define ARMV8_IMPDEF_PERFCTR_PC_WRITE_SPEC			0x0076
+#define ARMV8_IMPDEF_PERFCTR_CRYPTO_SPEC			0x0077
+#define ARMV8_IMPDEF_PERFCTR_BR_IMMED_SPEC			0x0078
+#define ARMV8_IMPDEF_PERFCTR_BR_RETURN_SPEC			0x0079
+#define ARMV8_IMPDEF_PERFCTR_BR_INDIRECT_SPEC			0x007A
+
+#define ARMV8_IMPDEF_PERFCTR_ISB_SPEC				0x007C
+#define ARMV8_IMPDEF_PERFCTR_DSB_SPEC				0x007D
+#define ARMV8_IMPDEF_PERFCTR_DMB_SPEC				0x007E
+
+#define ARMV8_IMPDEF_PERFCTR_EXC_UNDEF				0x0081
+#define ARMV8_IMPDEF_PERFCTR_EXC_SVC				0x0082
+#define ARMV8_IMPDEF_PERFCTR_EXC_PABORT				0x0083
+#define ARMV8_IMPDEF_PERFCTR_EXC_DABORT				0x0084
+
+#define ARMV8_IMPDEF_PERFCTR_EXC_IRQ				0x0086
+#define ARMV8_IMPDEF_PERFCTR_EXC_FIQ				0x0087
+#define ARMV8_IMPDEF_PERFCTR_EXC_SMC				0x0088
+
+#define ARMV8_IMPDEF_PERFCTR_EXC_HVC				0x008A
+#define ARMV8_IMPDEF_PERFCTR_EXC_TRAP_PABORT			0x008B
+#define ARMV8_IMPDEF_PERFCTR_EXC_TRAP_DABORT			0x008C
+#define ARMV8_IMPDEF_PERFCTR_EXC_TRAP_OTHER			0x008D
+#define ARMV8_IMPDEF_PERFCTR_EXC_TRAP_IRQ			0x008E
+#define ARMV8_IMPDEF_PERFCTR_EXC_TRAP_FIQ			0x008F
+#define ARMV8_IMPDEF_PERFCTR_RC_LD_SPEC				0x0090
+#define ARMV8_IMPDEF_PERFCTR_RC_ST_SPEC				0x0091
+
+#define ARMV8_IMPDEF_PERFCTR_L3D_CACHE_RD			0x00A0
+#define ARMV8_IMPDEF_PERFCTR_L3D_CACHE_WR			0x00A1
+#define ARMV8_IMPDEF_PERFCTR_L3D_CACHE_REFILL_RD		0x00A2
+#define ARMV8_IMPDEF_PERFCTR_L3D_CACHE_REFILL_WR		0x00A3
+
+#define ARMV8_IMPDEF_PERFCTR_L3D_CACHE_WB_VICTIM		0x00A6
+#define ARMV8_IMPDEF_PERFCTR_L3D_CACHE_WB_CLEAN			0x00A7
+#define ARMV8_IMPDEF_PERFCTR_L3D_CACHE_INVAL			0x00A8
+
+/*
+ * Per-CPU PMCR: config reg
+ */
+#define ARMV8_PMU_PMCR_E	(1 << 0) /* Enable all counters */
+#define ARMV8_PMU_PMCR_P	(1 << 1) /* Reset all counters */
+#define ARMV8_PMU_PMCR_C	(1 << 2) /* Cycle counter reset */
+#define ARMV8_PMU_PMCR_D	(1 << 3) /* CCNT counts every 64th cpu cycle */
+#define ARMV8_PMU_PMCR_X	(1 << 4) /* Export to ETM */
+#define ARMV8_PMU_PMCR_DP	(1 << 5) /* Disable CCNT if non-invasive debug*/
+#define ARMV8_PMU_PMCR_LC	(1 << 6) /* Overflow on 64 bit cycle counter */
+#define ARMV8_PMU_PMCR_LP	(1 << 7) /* Long event counter enable */
+#define ARMV8_PMU_PMCR_N_SHIFT	11  /* Number of counters supported */
+#define	ARMV8_PMU_PMCR_N	(0x1f << ARMV8_PMU_PMCR_N_SHIFT)
+#define ARMV8_PMU_PMCR_MASK	0xff    /* Mask for writable bits */
+
+/*
+ * PMOVSR: counters overflow flag status reg
+ */
+#define ARMV8_PMU_OVSR_MASK		0xffffffff	/* Mask for writable bits */
+#define ARMV8_PMU_OVERFLOWED_MASK	ARMV8_PMU_OVSR_MASK
+
+/*
+ * PMXEVTYPER: Event selection reg
+ */
+#define ARMV8_PMU_EVTYPE_MASK	0xc800ffff	/* Mask for writable bits */
+#define ARMV8_PMU_EVTYPE_EVENT	0xffff		/* Mask for EVENT bits */
+
+/*
+ * Event filters for PMUv3
+ */
+#define ARMV8_PMU_EXCLUDE_EL1	(1U << 31)
+#define ARMV8_PMU_EXCLUDE_EL0	(1U << 30)
+#define ARMV8_PMU_INCLUDE_EL2	(1U << 27)
+
+/*
+ * PMUSERENR: user enable reg
+ */
+#define ARMV8_PMU_USERENR_MASK	0xf		/* Mask for writable bits */
+#define ARMV8_PMU_USERENR_EN	(1 << 0) /* PMU regs can be accessed at EL0 */
+#define ARMV8_PMU_USERENR_SW	(1 << 1) /* PMSWINC can be written at EL0 */
+#define ARMV8_PMU_USERENR_CR	(1 << 2) /* Cycle counter can be read at EL0 */
+#define ARMV8_PMU_USERENR_ER	(1 << 3) /* Event counter can be read at EL0 */
+
+/* PMMIR_EL1.SLOTS mask */
+#define ARMV8_PMU_SLOTS_MASK	0xff
+
+#define ARMV8_PMU_BUS_SLOTS_SHIFT 8
+#define ARMV8_PMU_BUS_SLOTS_MASK 0xff
+#define ARMV8_PMU_BUS_WIDTH_SHIFT 16
+#define ARMV8_PMU_BUS_WIDTH_MASK 0xf
+
+/*
+ * This code is really good
+ */
+
+#define PMEVN_CASE(n, case_macro) \
+	case n: case_macro(n); break
+
+#define PMEVN_SWITCH(x, case_macro)				\
+	do {							\
+		switch (x) {					\
+		PMEVN_CASE(0,  case_macro);			\
+		PMEVN_CASE(1,  case_macro);			\
+		PMEVN_CASE(2,  case_macro);			\
+		PMEVN_CASE(3,  case_macro);			\
+		PMEVN_CASE(4,  case_macro);			\
+		PMEVN_CASE(5,  case_macro);			\
+		PMEVN_CASE(6,  case_macro);			\
+		PMEVN_CASE(7,  case_macro);			\
+		PMEVN_CASE(8,  case_macro);			\
+		PMEVN_CASE(9,  case_macro);			\
+		PMEVN_CASE(10, case_macro);			\
+		PMEVN_CASE(11, case_macro);			\
+		PMEVN_CASE(12, case_macro);			\
+		PMEVN_CASE(13, case_macro);			\
+		PMEVN_CASE(14, case_macro);			\
+		PMEVN_CASE(15, case_macro);			\
+		PMEVN_CASE(16, case_macro);			\
+		PMEVN_CASE(17, case_macro);			\
+		PMEVN_CASE(18, case_macro);			\
+		PMEVN_CASE(19, case_macro);			\
+		PMEVN_CASE(20, case_macro);			\
+		PMEVN_CASE(21, case_macro);			\
+		PMEVN_CASE(22, case_macro);			\
+		PMEVN_CASE(23, case_macro);			\
+		PMEVN_CASE(24, case_macro);			\
+		PMEVN_CASE(25, case_macro);			\
+		PMEVN_CASE(26, case_macro);			\
+		PMEVN_CASE(27, case_macro);			\
+		PMEVN_CASE(28, case_macro);			\
+		PMEVN_CASE(29, case_macro);			\
+		PMEVN_CASE(30, case_macro);			\
+		default:					\
+			WARN(1, "Invalid PMEV* index\n");	\
+			assert(0);				\
+		}						\
+	} while (0)
+
+#endif
Signature · 1 line
2.41.0.694.ge786442a9b-goog
#36 lore raw

PATCH v5 10/12 KVM: selftests: aarch64: Introduce vpmu_counter_access test

In reply to Raghavendra Rao Ananta

From: Reiji Watanabe <[email protected]>

Introduce vpmu_counter_access test for arm64 platforms. The test configures PMUv3 for a vCPU, sets PMCR_EL0.N for the vCPU, and check if the guest can consistently see the same number of the PMU event counters (PMCR_EL0.N) that userspace sets. This test case is done with each of the PMCR_EL0.N values from 0 to 31 (With the PMCR_EL0.N values greater than the host value, the test expects KVM_SET_ONE_REG for the PMCR_EL0 to fail).

---
 tools/testing/selftests/kvm/Makefile          |   1 +
 .../kvm/aarch64/vpmu_counter_access.c         | 235 ++++++++++++++++++
 2 files changed, 236 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/aarch64/vpmu_counter_access.c
tools/testing/selftests/kvm/Makefile +1 -0
index c692cc86e7da8..a1599e2b82e38 100644
@@ -148,6 +148,7 @@ TEST_GEN_PROGS_aarch64 += aarch64/smccc_filter
 TEST_GEN_PROGS_aarch64 += aarch64/vcpu_width_config
 TEST_GEN_PROGS_aarch64 += aarch64/vgic_init
 TEST_GEN_PROGS_aarch64 += aarch64/vgic_irq
+TEST_GEN_PROGS_aarch64 += aarch64/vpmu_counter_access
 TEST_GEN_PROGS_aarch64 += access_tracking_perf_test
 TEST_GEN_PROGS_aarch64 += demand_paging_test
 TEST_GEN_PROGS_aarch64 += dirty_log_test
tools/testing/selftests/kvm/aarch64/vpmu_counter_access.c new file +235 -0

235 lines not on this page · raw message

Signature · 1 line
2.41.0.694.ge786442a9b-goog
#37 lore raw

Re: [PATCH v5 10/12] KVM: selftests: aarch64: Introduce vpmu_counter_access test

Hi Raghu,

Quoted 52 lines · Raghavendra Rao Ananta

From: Reiji Watanabe <[email protected]>

Introduce vpmu_counter_access test for arm64 platforms. The test configures PMUv3 for a vCPU, sets PMCR_EL0.N for the vCPU, and check if the guest can consistently see the same number of the PMU event counters (PMCR_EL0.N) that userspace sets. This test case is done with each of the PMCR_EL0.N values from 0 to 31 (With the PMCR_EL0.N values greater than the host value, the test expects KVM_SET_ONE_REG for the PMCR_EL0 to fail).

---
 tools/testing/selftests/kvm/Makefile          |   1 +
 .../kvm/aarch64/vpmu_counter_access.c         | 235 ++++++++++++++++++
 2 files changed, 236 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/aarch64/vpmu_counter_access.c
tools/testing/selftests/kvm/Makefile +1 -0
index c692cc86e7da8..a1599e2b82e38 100644
@@ -148,6 +148,7 @@ TEST_GEN_PROGS_aarch64 += aarch64/smccc_filter
 TEST_GEN_PROGS_aarch64 += aarch64/vcpu_width_config
 TEST_GEN_PROGS_aarch64 += aarch64/vgic_init
 TEST_GEN_PROGS_aarch64 += aarch64/vgic_irq
+TEST_GEN_PROGS_aarch64 += aarch64/vpmu_counter_access
 TEST_GEN_PROGS_aarch64 += access_tracking_perf_test
 TEST_GEN_PROGS_aarch64 += demand_paging_test
 TEST_GEN_PROGS_aarch64 += dirty_log_test
tools/testing/selftests/kvm/aarch64/vpmu_counter_access.c new file +16 -0
new file mode 100644
index 0000000000000..d0afec07948ef
@@ -0,0 +1,235 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * vpmu_counter_access - Test vPMU event counter access
+ *
+ * Copyright (c) 2022 Google LLC.
+ *
+ * This test checks if the guest can see the same number of the PMU event
+ * counters (PMCR_EL0.N) that userspace sets.
+ * This test runs only when KVM_CAP_ARM_PMU_V3 is supported on the host.
+ */
+#include <kvm_util.h>
+#include <processor.h>
+#include <test_util.h>
+#include <vgic.h>
+#include <perf/arm_pmuv3.h>
+#include <linux/bitfield.h>

tools/testing/selftests/kvm/aarch64/vpmu_counter_access.c line 26

Quoted 10 lines
tools/testing/selftests/kvm/aarch64/vpmu_counter_access.c new file +10 -0
+
+/* The max number of the PMU event counters (excluding the cycle counter) */
+#define ARMV8_PMU_MAX_GENERAL_COUNTERS	(ARMV8_PMU_MAX_COUNTERS - 1)
+
+struct vpmu_vm {
+	struct kvm_vm *vm;
+	struct kvm_vcpu *vcpu;
+	int gic_fd;
+};
+

nit: this test is single threaded, so there will only ever be a single instance of a VM at a time. Dynamically allocating a backing structure doesn't add any value, IMO.

You can just get away with using globals.

Quoted 15 lines
tools/testing/selftests/kvm/aarch64/vpmu_counter_access.c new file +15 -0
+/*
+ * Create a guest with one vCPU, and attempt to set the PMCR_EL0.N for
+ * the vCPU to @pmcr_n, which is larger than the host value.
+ * The attempt should fail as @pmcr_n is too big to set for the vCPU.
+ */
+static void run_error_test(uint64_t pmcr_n)
+{
+	struct vpmu_vm *vpmu_vm;
+	struct kvm_vcpu *vcpu;
+	int ret;
+	uint64_t pmcr, pmcr_orig;
+
+	pr_debug("Error test with pmcr_n %lu (larger than the host)\n", pmcr_n);
+	vpmu_vm = create_vpmu_vm(guest_code);
+	vcpu = vpmu_vm->vcpu;

tools/testing/selftests/kvm/aarch64/vpmu_counter_access.c line 201

Quoted 10 lines
tools/testing/selftests/kvm/aarch64/vpmu_counter_access.c new file +10 -0
+
+	/* Update the PMCR_EL0.N with @pmcr_n */
+	vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_PMCR_EL0), &pmcr_orig);
+	pmcr = pmcr_orig & ~ARMV8_PMU_PMCR_N;
+	pmcr |= (pmcr_n << ARMV8_PMU_PMCR_N_SHIFT);
+
+	/* This should fail as @pmcr_n is too big to set for the vCPU */
+	ret = __vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(SYS_PMCR_EL0), pmcr);
+	TEST_ASSERT(ret, "Setting PMCR to 0x%lx (orig PMCR 0x%lx) didn't fail",
+		    pmcr, pmcr_orig);

The failure pattern for this should now be the write to PMCR_EL0.N had no effect.

Signature · 2 lines
Thanks,
Oliver
#38 lore raw

Re: [PATCH v5 10/12] KVM: selftests: aarch64: Introduce vpmu_counter_access test

Hi Oliver,

Quoted 71 lines · Oliver Upton

Hi Raghu,

Quoted 52 lines · Raghavendra Rao Ananta

From: Reiji Watanabe <[email protected]>

Introduce vpmu_counter_access test for arm64 platforms. The test configures PMUv3 for a vCPU, sets PMCR_EL0.N for the vCPU, and check if the guest can consistently see the same number of the PMU event counters (PMCR_EL0.N) that userspace sets. This test case is done with each of the PMCR_EL0.N values from 0 to 31 (With the PMCR_EL0.N values greater than the host value, the test expects KVM_SET_ONE_REG for the PMCR_EL0 to fail).

---
 tools/testing/selftests/kvm/Makefile          |   1 +
 .../kvm/aarch64/vpmu_counter_access.c         | 235 ++++++++++++++++++
 2 files changed, 236 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/aarch64/vpmu_counter_access.c
tools/testing/selftests/kvm/Makefile +1 -0
index c692cc86e7da8..a1599e2b82e38 100644
@@ -148,6 +148,7 @@ TEST_GEN_PROGS_aarch64 += aarch64/smccc_filter
 TEST_GEN_PROGS_aarch64 += aarch64/vcpu_width_config
 TEST_GEN_PROGS_aarch64 += aarch64/vgic_init
 TEST_GEN_PROGS_aarch64 += aarch64/vgic_irq
+TEST_GEN_PROGS_aarch64 += aarch64/vpmu_counter_access
 TEST_GEN_PROGS_aarch64 += access_tracking_perf_test
 TEST_GEN_PROGS_aarch64 += demand_paging_test
 TEST_GEN_PROGS_aarch64 += dirty_log_test
tools/testing/selftests/kvm/aarch64/vpmu_counter_access.c new file +16 -0
new file mode 100644
index 0000000000000..d0afec07948ef
@@ -0,0 +1,235 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * vpmu_counter_access - Test vPMU event counter access
+ *
+ * Copyright (c) 2022 Google LLC.
+ *
+ * This test checks if the guest can see the same number of the PMU event
+ * counters (PMCR_EL0.N) that userspace sets.
+ * This test runs only when KVM_CAP_ARM_PMU_V3 is supported on the host.
+ */
+#include <kvm_util.h>
+#include <processor.h>
+#include <test_util.h>
+#include <vgic.h>
+#include <perf/arm_pmuv3.h>
+#include <linux/bitfield.h>

tools/testing/selftests/kvm/aarch64/vpmu_counter_access.c line 26

Quoted 10 lines
tools/testing/selftests/kvm/aarch64/vpmu_counter_access.c new file +10 -0
+
+/* The max number of the PMU event counters (excluding the cycle counter) */
+#define ARMV8_PMU_MAX_GENERAL_COUNTERS       (ARMV8_PMU_MAX_COUNTERS - 1)
+
+struct vpmu_vm {
+     struct kvm_vm *vm;
+     struct kvm_vcpu *vcpu;
+     int gic_fd;
+};
+

nit: this test is single threaded, so there will only ever be a single instance of a VM at a time. Dynamically allocating a backing structure doesn't add any value, IMO.

Quoted 2 lines

You can just get away with using globals.

Probably. I can try to have a single global.

Quoted 20 lines
Quoted 20 lines
tools/testing/selftests/kvm/aarch64/vpmu_counter_access.c new file +20 -0
+/*
+ * Create a guest with one vCPU, and attempt to set the PMCR_EL0.N for
+ * the vCPU to @pmcr_n, which is larger than the host value.
+ * The attempt should fail as @pmcr_n is too big to set for the vCPU.
+ */
+static void run_error_test(uint64_t pmcr_n)
+{
+     struct vpmu_vm *vpmu_vm;
+     struct kvm_vcpu *vcpu;
+     int ret;
+     uint64_t pmcr, pmcr_orig;
+
+     pr_debug("Error test with pmcr_n %lu (larger than the host)\n", pmcr_n);
+     vpmu_vm = create_vpmu_vm(guest_code);
+     vcpu = vpmu_vm->vcpu;
+
+     /* Update the PMCR_EL0.N with @pmcr_n */
+     vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_PMCR_EL0), &pmcr_orig);
+     pmcr = pmcr_orig & ~ARMV8_PMU_PMCR_N;
+     pmcr |= (pmcr_n << ARMV8_PMU_PMCR_N_SHIFT);
Quoted 9 lines

tools/testing/selftests/kvm/aarch64/vpmu_counter_access.c line 201

Quoted 5 lines
tools/testing/selftests/kvm/aarch64/vpmu_counter_access.c new file +5 -0
+
+     /* This should fail as @pmcr_n is too big to set for the vCPU */
+     ret = __vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(SYS_PMCR_EL0), pmcr);
+     TEST_ASSERT(ret, "Setting PMCR to 0x%lx (orig PMCR 0x%lx) didn't fail",
+                 pmcr, pmcr_orig);

The failure pattern for this should now be the write to PMCR_EL0.N had no effect.

Right. I'll make the change.

Thank you.
Raghavendra

Quoted 3 lines

--
Thanks,
Oliver

#39 lore raw

PATCH v5 11/12 KVM: selftests: aarch64: vPMU register test for implemented counters

In reply to Raghavendra Rao Ananta

From: Reiji Watanabe <[email protected]>

Add a new test case to the vpmu_counter_access test to check if PMU registers or their bits for implemented counters on the vCPU are readable/writable as expected, and can be programmed to count events.

---
 .../kvm/aarch64/vpmu_counter_access.c         | 264 +++++++++++++++++-
 1 file changed, 261 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/aarch64/vpmu_counter_access.c +261 -3
index d0afec07948ef..3a2cf38bb415d 100644
@@ -5,7 +5,8 @@
  * Copyright (c) 2022 Google LLC.
  *
  * This test checks if the guest can see the same number of the PMU event
- * counters (PMCR_EL0.N) that userspace sets.
+ * counters (PMCR_EL0.N) that userspace sets, and if the guest can access
+ * those counters.
  * This test runs only when KVM_CAP_ARM_PMU_V3 is supported on the host.
  */
 #include <kvm_util.h>
@@ -24,6 +25,251 @@ struct vpmu_vm {
 	int gic_fd;
 };
 
+/* Read PMEVTCNTR<n>_EL0 through PMXEVCNTR_EL0 */
+static inline unsigned long read_sel_evcntr(int sel)
+{
+	write_sysreg(sel, pmselr_el0);
+	isb();
+	return read_sysreg(pmxevcntr_el0);
+}
+
+/* Write PMEVTCNTR<n>_EL0 through PMXEVCNTR_EL0 */
+static inline void write_sel_evcntr(int sel, unsigned long val)
+{
+	write_sysreg(sel, pmselr_el0);
+	isb();
+	write_sysreg(val, pmxevcntr_el0);
+	isb();
+}
+
+/* Read PMEVTYPER<n>_EL0 through PMXEVTYPER_EL0 */
+static inline unsigned long read_sel_evtyper(int sel)
+{
+	write_sysreg(sel, pmselr_el0);
+	isb();
+	return read_sysreg(pmxevtyper_el0);
+}
+
+/* Write PMEVTYPER<n>_EL0 through PMXEVTYPER_EL0 */
+static inline void write_sel_evtyper(int sel, unsigned long val)
+{
+	write_sysreg(sel, pmselr_el0);
+	isb();
+	write_sysreg(val, pmxevtyper_el0);
+	isb();
+}
+
+static inline void enable_counter(int idx)
+{
+	uint64_t v = read_sysreg(pmcntenset_el0);
+
+	write_sysreg(BIT(idx) | v, pmcntenset_el0);
+	isb();
+}
+
+static inline void disable_counter(int idx)
+{
+	uint64_t v = read_sysreg(pmcntenset_el0);
+
+	write_sysreg(BIT(idx) | v, pmcntenclr_el0);
+	isb();
+}
+
+static void pmu_disable_reset(void)
+{
+	uint64_t pmcr = read_sysreg(pmcr_el0);
+
+	/* Reset all counters, disabling them */
+	pmcr &= ~ARMV8_PMU_PMCR_E;
+	write_sysreg(pmcr | ARMV8_PMU_PMCR_P, pmcr_el0);
+	isb();
+}
+
+#define RETURN_READ_PMEVCNTRN(n) \
+	return read_sysreg(pmevcntr##n##_el0)
+static unsigned long read_pmevcntrn(int n)
+{
+	PMEVN_SWITCH(n, RETURN_READ_PMEVCNTRN);
+	return 0;
+}
+
+#define WRITE_PMEVCNTRN(n) \
+	write_sysreg(val, pmevcntr##n##_el0)
+static void write_pmevcntrn(int n, unsigned long val)
+{
+	PMEVN_SWITCH(n, WRITE_PMEVCNTRN);
+	isb();
+}
+
+#define READ_PMEVTYPERN(n) \
+	return read_sysreg(pmevtyper##n##_el0)
+static unsigned long read_pmevtypern(int n)
+{
+	PMEVN_SWITCH(n, READ_PMEVTYPERN);
+	return 0;
+}
+
+#define WRITE_PMEVTYPERN(n) \
+	write_sysreg(val, pmevtyper##n##_el0)
+static void write_pmevtypern(int n, unsigned long val)
+{
+	PMEVN_SWITCH(n, WRITE_PMEVTYPERN);
+	isb();
+}
+
+/*
+ * The pmc_accessor structure has pointers to PMEVT{CNTR,TYPER}<n>_EL0
+ * accessors that test cases will use. Each of the accessors will
+ * either directly reads/writes PMEVT{CNTR,TYPER}<n>_EL0
+ * (i.e. {read,write}_pmev{cnt,type}rn()), or reads/writes them through
+ * PMXEV{CNTR,TYPER}_EL0 (i.e. {read,write}_sel_ev{cnt,type}r()).
+ *
+ * This is used to test that combinations of those accessors provide
+ * the consistent behavior.
+ */
+struct pmc_accessor {
+	/* A function to be used to read PMEVTCNTR<n>_EL0 */
+	unsigned long	(*read_cntr)(int idx);
+	/* A function to be used to write PMEVTCNTR<n>_EL0 */
+	void		(*write_cntr)(int idx, unsigned long val);
+	/* A function to be used to read PMEVTYPER<n>_EL0 */
+	unsigned long	(*read_typer)(int idx);
+	/* A function to be used to write PMEVTYPER<n>_EL0 */
+	void		(*write_typer)(int idx, unsigned long val);
+};
+
+struct pmc_accessor pmc_accessors[] = {
+	/* test with all direct accesses */
+	{ read_pmevcntrn, write_pmevcntrn, read_pmevtypern, write_pmevtypern },
+	/* test with all indirect accesses */
+	{ read_sel_evcntr, write_sel_evcntr, read_sel_evtyper, write_sel_evtyper },
+	/* read with direct accesses, and write with indirect accesses */
+	{ read_pmevcntrn, write_sel_evcntr, read_pmevtypern, write_sel_evtyper },
+	/* read with indirect accesses, and write with direct accesses */
+	{ read_sel_evcntr, write_pmevcntrn, read_sel_evtyper, write_pmevtypern },
+};
+
+/*
+ * Convert a pointer of pmc_accessor to an index in pmc_accessors[],
+ * assuming that the pointer is one of the entries in pmc_accessors[].
+ */
+#define PMC_ACC_TO_IDX(acc)	(acc - &pmc_accessors[0])
+
+#define GUEST_ASSERT_BITMAP_REG(regname, mask, set_expected)		\
+{									\
+	uint64_t _tval = read_sysreg(regname);				\
+									\
+	if (set_expected)						\
+		GUEST_ASSERT_3((_tval & mask), _tval, mask, set_expected); \
+	else								   \
+		GUEST_ASSERT_3(!(_tval & mask), _tval, mask, set_expected);\
+}
+
+/*
+ * Check if @mask bits in {PMCNTEN,PMINTEN,PMOVS}{SET,CLR} registers
+ * are set or cleared as specified in @set_expected.
+ */
+static void check_bitmap_pmu_regs(uint64_t mask, bool set_expected)
+{
+	GUEST_ASSERT_BITMAP_REG(pmcntenset_el0, mask, set_expected);
+	GUEST_ASSERT_BITMAP_REG(pmcntenclr_el0, mask, set_expected);
+	GUEST_ASSERT_BITMAP_REG(pmintenset_el1, mask, set_expected);
+	GUEST_ASSERT_BITMAP_REG(pmintenclr_el1, mask, set_expected);
+	GUEST_ASSERT_BITMAP_REG(pmovsset_el0, mask, set_expected);
+	GUEST_ASSERT_BITMAP_REG(pmovsclr_el0, mask, set_expected);
+}
+
+/*
+ * Check if the bit in {PMCNTEN,PMINTEN,PMOVS}{SET,CLR} registers corresponding
+ * to the specified counter (@pmc_idx) can be read/written as expected.
+ * When @set_op is true, it tries to set the bit for the counter in
+ * those registers by writing the SET registers (the bit won't be set
+ * if the counter is not implemented though).
+ * Otherwise, it tries to clear the bits in the registers by writing
+ * the CLR registers.
+ * Then, it checks if the values indicated in the registers are as expected.
+ */
+static void test_bitmap_pmu_regs(int pmc_idx, bool set_op)
+{
+	uint64_t pmcr_n, test_bit = BIT(pmc_idx);
+	bool set_expected = false;
+
+	if (set_op) {
+		write_sysreg(test_bit, pmcntenset_el0);
+		write_sysreg(test_bit, pmintenset_el1);
+		write_sysreg(test_bit, pmovsset_el0);
+
+		/* The bit will be set only if the counter is implemented */
+		pmcr_n = FIELD_GET(ARMV8_PMU_PMCR_N, read_sysreg(pmcr_el0));
+		set_expected = (pmc_idx < pmcr_n) ? true : false;
+	} else {
+		write_sysreg(test_bit, pmcntenclr_el0);
+		write_sysreg(test_bit, pmintenclr_el1);
+		write_sysreg(test_bit, pmovsclr_el0);
+	}
+	check_bitmap_pmu_regs(test_bit, set_expected);
+}
+
+/*
+ * Tests for reading/writing registers for the (implemented) event counter
+ * specified by @pmc_idx.
+ */
+static void test_access_pmc_regs(struct pmc_accessor *acc, int pmc_idx)
+{
+	uint64_t write_data, read_data;
+
+	/* Disable all PMCs and reset all PMCs to zero. */
+	pmu_disable_reset();
+
+
+	/*
+	 * Tests for reading/writing {PMCNTEN,PMINTEN,PMOVS}{SET,CLR}_EL1.
+	 */
+
+	/* Make sure that the bit in those registers are set to 0 */
+	test_bitmap_pmu_regs(pmc_idx, false);
+	/* Test if setting the bit in those registers works */
+	test_bitmap_pmu_regs(pmc_idx, true);
+	/* Test if clearing the bit in those registers works */
+	test_bitmap_pmu_regs(pmc_idx, false);
+
+
+	/*
+	 * Tests for reading/writing the event type register.
+	 */
+
+	read_data = acc->read_typer(pmc_idx);
+	/*
+	 * Set the event type register to an arbitrary value just for testing
+	 * of reading/writing the register.
+	 * ArmARM says that for the event from 0x0000 to 0x003F,
+	 * the value indicated in the PMEVTYPER<n>_EL0.evtCount field is
+	 * the value written to the field even when the specified event
+	 * is not supported.
+	 */
+	write_data = (ARMV8_PMU_EXCLUDE_EL1 | ARMV8_PMUV3_PERFCTR_INST_RETIRED);
+	acc->write_typer(pmc_idx, write_data);
+	read_data = acc->read_typer(pmc_idx);
+	GUEST_ASSERT_4(read_data == write_data,
+		       pmc_idx, PMC_ACC_TO_IDX(acc), read_data, write_data);
+
+
+	/*
+	 * Tests for reading/writing the event count register.
+	 */
+
+	read_data = acc->read_cntr(pmc_idx);
+
+	/* The count value must be 0, as it is not used after the reset */
+	GUEST_ASSERT_3(read_data == 0, pmc_idx, acc, read_data);
+
+	write_data = read_data + pmc_idx + 0x12345;
+	acc->write_cntr(pmc_idx, write_data);
+	read_data = acc->read_cntr(pmc_idx);
+	GUEST_ASSERT_4(read_data == write_data,
+		       pmc_idx, PMC_ACC_TO_IDX(acc), read_data, write_data);
+}
+
 static void guest_sync_handler(struct ex_regs *regs)
 {
 	uint64_t esr, ec;
@@ -36,11 +282,14 @@ static void guest_sync_handler(struct ex_regs *regs)
 /*
  * The guest is configured with PMUv3 with @expected_pmcr_n number of
  * event counters.
- * Check if @expected_pmcr_n is consistent with PMCR_EL0.N.
+ * Check if @expected_pmcr_n is consistent with PMCR_EL0.N, and
+ * if reading/writing PMU registers for implemented counters can work
+ * as expected.
  */
 static void guest_code(uint64_t expected_pmcr_n)
 {
 	uint64_t pmcr, pmcr_n;
+	int i, pmc;
 
 	GUEST_ASSERT(expected_pmcr_n <= ARMV8_PMU_MAX_GENERAL_COUNTERS);
 
@@ -50,6 +299,15 @@ static void guest_code(uint64_t expected_pmcr_n)
 	/* Make sure that PMCR_EL0.N indicates the value userspace set */
 	GUEST_ASSERT_2(pmcr_n == expected_pmcr_n, pmcr_n, expected_pmcr_n);
 
+	/*
+	 * Tests for reading/writing PMU registers for implemented counters.
+	 * Use each combination of PMEVT{CNTR,TYPER}<n>_EL0 accessor functions.
+	 */
+	for (i = 0; i < ARRAY_SIZE(pmc_accessors); i++) {
+		for (pmc = 0; pmc < pmcr_n; pmc++)
+			test_access_pmc_regs(&pmc_accessors[i], pmc);
+	}
+
 	GUEST_DONE();
 }
 
@@ -121,7 +379,7 @@ static void run_vcpu(struct kvm_vcpu *vcpu, uint64_t pmcr_n)
 	vcpu_run(vcpu);
 	switch (get_ucall(vcpu, &uc)) {
 	case UCALL_ABORT:
-		REPORT_GUEST_ASSERT_2(uc, "values:%#lx %#lx");
+		REPORT_GUEST_ASSERT_4(uc, "values:%#lx %#lx %#lx %#lx");
 		break;
 	case UCALL_DONE:
 		break;
Signature · 1 line
2.41.0.694.ge786442a9b-goog
#40 lore raw

PATCH v5 12/12 KVM: selftests: aarch64: vPMU register test for unimplemented counters

In reply to Raghavendra Rao Ananta

From: Reiji Watanabe <[email protected]>

Add a new test case to the vpmu_counter_access test to check if PMU registers or their bits for unimplemented counters are not accessible or are RAZ, as expected.

---
 .../kvm/aarch64/vpmu_counter_access.c         | 93 +++++++++++++++++--
 .../selftests/kvm/include/aarch64/processor.h |  1 +
 2 files changed, 85 insertions(+), 9 deletions(-)
tools/testing/selftests/kvm/aarch64/vpmu_counter_access.c +84 -9
index 3a2cf38bb415d..61fd1420e3cc1 100644
@@ -5,8 +5,8 @@
  * Copyright (c) 2022 Google LLC.
  *
  * This test checks if the guest can see the same number of the PMU event
- * counters (PMCR_EL0.N) that userspace sets, and if the guest can access
- * those counters.
+ * counters (PMCR_EL0.N) that userspace sets, if the guest can access
+ * those counters, and if the guest cannot access any other counters.
  * This test runs only when KVM_CAP_ARM_PMU_V3 is supported on the host.
  */
 #include <kvm_util.h>
@@ -118,9 +118,9 @@ static void write_pmevtypern(int n, unsigned long val)
 }
 
 /*
- * The pmc_accessor structure has pointers to PMEVT{CNTR,TYPER}<n>_EL0
+ * The pmc_accessor structure has pointers to PMEV{CNTR,TYPER}<n>_EL0
  * accessors that test cases will use. Each of the accessors will
- * either directly reads/writes PMEVT{CNTR,TYPER}<n>_EL0
+ * either directly reads/writes PMEV{CNTR,TYPER}<n>_EL0
  * (i.e. {read,write}_pmev{cnt,type}rn()), or reads/writes them through
  * PMXEV{CNTR,TYPER}_EL0 (i.e. {read,write}_sel_ev{cnt,type}r()).
  *
@@ -270,25 +270,83 @@ static void test_access_pmc_regs(struct pmc_accessor *acc, int pmc_idx)
 		       pmc_idx, PMC_ACC_TO_IDX(acc), read_data, write_data);
 }
 
+#define INVALID_EC	(-1ul)
+uint64_t expected_ec = INVALID_EC;
+uint64_t op_end_addr;
+
 static void guest_sync_handler(struct ex_regs *regs)
 {
 	uint64_t esr, ec;
 
 	esr = read_sysreg(esr_el1);
 	ec = (esr >> ESR_EC_SHIFT) & ESR_EC_MASK;
-	GUEST_ASSERT_3(0, regs->pc, esr, ec);
+	GUEST_ASSERT_4(op_end_addr && (expected_ec == ec),
+		       regs->pc, esr, ec, expected_ec);
+
+	/* Will go back to op_end_addr after the handler exits */
+	regs->pc = op_end_addr;
+
+	/*
+	 * Clear op_end_addr, and setting expected_ec to INVALID_EC
+	 * as a sign that an exception has occurred.
+	 */
+	op_end_addr = 0;
+	expected_ec = INVALID_EC;
+}
+
+/*
+ * Run the given operation that should trigger an exception with the
+ * given exception class. The exception handler (guest_sync_handler)
+ * will reset op_end_addr to 0, and expected_ec to INVALID_EC, and
+ * will come back to the instruction at the @done_label.
+ * The @done_label must be a unique label in this test program.
+ */
+#define TEST_EXCEPTION(ec, ops, done_label)		\
+{							\
+	extern int done_label;				\
+							\
+	WRITE_ONCE(op_end_addr, (uint64_t)&done_label);	\
+	GUEST_ASSERT(ec != INVALID_EC);			\
+	WRITE_ONCE(expected_ec, ec);			\
+	dsb(ish);					\
+	ops;						\
+	asm volatile(#done_label":");			\
+	GUEST_ASSERT(!op_end_addr);			\
+	GUEST_ASSERT(expected_ec == INVALID_EC);	\
+}
+
+/*
+ * Tests for reading/writing registers for the unimplemented event counter
+ * specified by @pmc_idx (>= PMCR_EL0.N).
+ */
+static void test_access_invalid_pmc_regs(struct pmc_accessor *acc, int pmc_idx)
+{
+	/*
+	 * Reading/writing the event count/type registers should cause
+	 * an UNDEFINED exception.
+	 */
+	TEST_EXCEPTION(ESR_EC_UNKNOWN, acc->read_cntr(pmc_idx), inv_rd_cntr);
+	TEST_EXCEPTION(ESR_EC_UNKNOWN, acc->write_cntr(pmc_idx, 0), inv_wr_cntr);
+	TEST_EXCEPTION(ESR_EC_UNKNOWN, acc->read_typer(pmc_idx), inv_rd_typer);
+	TEST_EXCEPTION(ESR_EC_UNKNOWN, acc->write_typer(pmc_idx, 0), inv_wr_typer);
+	/*
+	 * The bit corresponding to the (unimplemented) counter in
+	 * {PMCNTEN,PMOVS}{SET,CLR}_EL1 registers should be RAZ.
+	 */
+	test_bitmap_pmu_regs(pmc_idx, 1);
+	test_bitmap_pmu_regs(pmc_idx, 0);
 }
 
 /*
  * The guest is configured with PMUv3 with @expected_pmcr_n number of
  * event counters.
  * Check if @expected_pmcr_n is consistent with PMCR_EL0.N, and
- * if reading/writing PMU registers for implemented counters can work
- * as expected.
+ * if reading/writing PMU registers for implemented or unimplemented
+ * counters can work as expected.
  */
 static void guest_code(uint64_t expected_pmcr_n)
 {
-	uint64_t pmcr, pmcr_n;
+	uint64_t pmcr, pmcr_n, unimp_mask;
 	int i, pmc;
 
 	GUEST_ASSERT(expected_pmcr_n <= ARMV8_PMU_MAX_GENERAL_COUNTERS);
@@ -299,15 +357,32 @@ static void guest_code(uint64_t expected_pmcr_n)
 	/* Make sure that PMCR_EL0.N indicates the value userspace set */
 	GUEST_ASSERT_2(pmcr_n == expected_pmcr_n, pmcr_n, expected_pmcr_n);
 
+	/*
+	 * Make sure that (RAZ) bits corresponding to unimplemented event
+	 * counters in {PMCNTEN,PMOVS}{SET,CLR}_EL1 registers are reset to zero.
+	 * (NOTE: bits for implemented event counters are reset to UNKNOWN)
+	 */
+	unimp_mask = GENMASK_ULL(ARMV8_PMU_MAX_GENERAL_COUNTERS - 1, pmcr_n);
+	check_bitmap_pmu_regs(unimp_mask, false);
+
 	/*
 	 * Tests for reading/writing PMU registers for implemented counters.
-	 * Use each combination of PMEVT{CNTR,TYPER}<n>_EL0 accessor functions.
+	 * Use each combination of PMEV{CNTR,TYPER}<n>_EL0 accessor functions.
 	 */
 	for (i = 0; i < ARRAY_SIZE(pmc_accessors); i++) {
 		for (pmc = 0; pmc < pmcr_n; pmc++)
 			test_access_pmc_regs(&pmc_accessors[i], pmc);
 	}
 
+	/*
+	 * Tests for reading/writing PMU registers for unimplemented counters.
+	 * Use each combination of PMEV{CNTR,TYPER}<n>_EL0 accessor functions.
+	 */
+	for (i = 0; i < ARRAY_SIZE(pmc_accessors); i++) {
+		for (pmc = pmcr_n; pmc < ARMV8_PMU_MAX_GENERAL_COUNTERS; pmc++)
+			test_access_invalid_pmc_regs(&pmc_accessors[i], pmc);
+	}
+
 	GUEST_DONE();
 }
 
tools/testing/selftests/kvm/include/aarch64/processor.h +1 -0
index cb537253a6b9c..c42d683102c7a 100644
@@ -104,6 +104,7 @@ enum {
 #define ESR_EC_SHIFT		26
 #define ESR_EC_MASK		(ESR_EC_NUM - 1)
 
+#define ESR_EC_UNKNOWN		0x0
 #define ESR_EC_SVC64		0x15
 #define ESR_EC_IABT		0x21
 #define ESR_EC_DABT		0x25
Signature · 1 line
2.41.0.694.ge786442a9b-goog