Skip to contentSkip to conversation

Support writable CPU ID registers from userspace

PATCH Superseded Jing Zhang 3 participants 19 messages Feb 28, 2023 – Mar 14, 2023

Subsystems (3)

v3 of 11 Superseded Latest is v11 →

lore
Share & tools
lore

Jing Zhang

author
#0 lore raw

v3 cover Support writable CPU ID registers from userspace

This patchset refactors/adds code to support writable per guest CPU ID feature registers. Part of the code/ideas are from
https://lore.kernel.org/all/[email protected] . No functional change is intended in this patchset. With the new CPU ID feature registers infrastructure, only writtings of ID_AA64PFR0_EL1.[CSV2|CSV3], ID_AA64DFR0_EL1.PMUVer and ID_DFR0_ELF.PerfMon are allowed as KVM does before.

Writable (Configurable) per guest CPU ID feature registers are useful for creating/migrating guest on ARM CPUs with different kinds of features.

---

* v2 -> v3
- Rebased to 96a4627dbbd4 (kvmarm/next)
Merge tag ' https://github.com/oupton/linux tags/kvmarm-6.3' from into kvmarm-master/next
- Add id registere emulation entry point function emulate_id_reg
- Fix consistency for ID_AA64DFR0_EL1.PMUVer and ID_DFR0_EL1.PerfMon
- Improve the checking for id register table by ensuring that every entry has the correct id register encoding.
- Addressed other comments from Reiji and Marc.

* v1 -> v2
- Rebase to 7121a2e1d107 (kvmarm/next) Merge branch kvm-arm64/nv-prefix into kvmarm/next
- Address writing issue for PMUVer

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

---

Jing Zhang (5):
KVM: arm64: Move CPU ID feature registers emulation into a separate
file
KVM: arm64: Use per guest ID register for ID_AA64PFR0_EL1.[CSV2|CSV3]
KVM: arm64: Use per guest ID register for ID_AA64DFR0_EL1.PMUVer
KVM: arm64: Introduce ID register specific descriptor
KVM: arm64: Refactor writings for PMUVer/CSV2/CSV3

Reiji Watanabe (1):
KVM: arm64: Save ID registers' sanitized value per guest

 arch/arm64/include/asm/cpufeature.h |  25 +
 arch/arm64/include/asm/kvm_host.h   |  26 +-
 arch/arm64/kernel/cpufeature.c      |  26 +-
 arch/arm64/kvm/Makefile             |   2 +-
 arch/arm64/kvm/arm.c                |  24 +-
 arch/arm64/kvm/hyp/nvhe/sys_regs.c  |   7 +-
 arch/arm64/kvm/id_regs.c            | 801 ++++++++++++++++++++++++++++
 arch/arm64/kvm/sys_regs.c           | 469 +---------------
 arch/arm64/kvm/sys_regs.h           |  43 ++
 include/kvm/arm_pmu.h               |   6 +-
 10 files changed, 927 insertions(+), 502 deletions(-)
 create mode 100644 arch/arm64/kvm/id_regs.c

base-commit: 96a4627dbbd48144a65af936b321701c70876026

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

PATCH v3 1/6 KVM: arm64: Move CPU ID feature registers emulation into a separate file

Create a new file id_regs.c for CPU ID feature registers emulation code, which are moved from sys_regs.c and tweak sys_regs code accordingly.

No functional change intended.

---
 arch/arm64/kvm/Makefile   |   2 +-
 arch/arm64/kvm/id_regs.c  | 506 ++++++++++++++++++++++++++++++++++++++
 arch/arm64/kvm/sys_regs.c | 464 ++--------------------------------
 arch/arm64/kvm/sys_regs.h |  41 +++
 4 files changed, 575 insertions(+), 438 deletions(-)
 create mode 100644 arch/arm64/kvm/id_regs.c
arch/arm64/kvm/Makefile +1 -1
index c0c050e53157..a6a315fcd81e 100644
@@ -13,7 +13,7 @@ obj-$(CONFIG_KVM) += hyp/
 kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \
 	 inject_fault.o va_layout.o handle_exit.o \
 	 guest.o debug.o reset.o sys_regs.o stacktrace.o \
-	 vgic-sys-reg-v3.o fpsimd.o pkvm.o \
+	 vgic-sys-reg-v3.o fpsimd.o pkvm.o id_regs.o \
 	 arch_timer.o trng.o vmid.o emulate-nested.o nested.o \
 	 vgic/vgic.o vgic/vgic-init.o \
 	 vgic/vgic-irqfd.o vgic/vgic-v2.o \
arch/arm64/kvm/id_regs.c new file +506 -0

506 lines not on this page · raw message

arch/arm64/kvm/sys_regs.c +27 -437

554 lines not on this page · raw message

arch/arm64/kvm/sys_regs.h +41 -0

53 lines not on this page · raw message

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

PATCH v3 2/6 KVM: arm64: Save ID registers' sanitized value per guest

In reply to Jing Zhang

From: Reiji Watanabe <[email protected]>

Introduce id_regs[] in kvm_arch as a storage of guest's ID registers, and save ID registers' sanitized value in the array at KVM_CREATE_VM. Use the saved ones when ID registers are read by the guest or userspace (via KVM_GET_ONE_REG).

No functional change intended.

---
 arch/arm64/include/asm/kvm_host.h | 12 +++++++++
 arch/arm64/kvm/arm.c              |  1 +
 arch/arm64/kvm/id_regs.c          | 44 ++++++++++++++++++++++++-------
 arch/arm64/kvm/sys_regs.c         |  2 +-
 arch/arm64/kvm/sys_regs.h         |  1 +
 5 files changed, 50 insertions(+), 10 deletions(-)
arch/arm64/include/asm/kvm_host.h +12 -0
index a1892a8f6032..5c1cec4efa37 100644
@@ -245,6 +245,16 @@ struct kvm_arch {
 	 * the associated pKVM instance in the hypervisor.
 	 */
 	struct kvm_protected_vm pkvm;
+
+	/*
+	 * Save ID registers for the guest in id_regs[].
+	 * (Op0, Op1, CRn, CRm, Op2) of the ID registers to be saved in it
+	 * is (3, 0, 0, crm, op2), where 1<=crm<8, 0<=op2<8.
+	 */
+#define KVM_ARM_ID_REG_NUM	56
+#define IDREG_IDX(id)		(((sys_reg_CRm(id) - 1) << 3) | sys_reg_Op2(id))
+#define IDREG(kvm, id)		kvm->arch.id_regs[IDREG_IDX(id)]
+	u64 id_regs[KVM_ARM_ID_REG_NUM];
 };
 
 struct kvm_vcpu_fault_info {
@@ -1005,6 +1015,8 @@ int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu,
 long kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
 				struct kvm_arm_copy_mte_tags *copy_tags);
 
+void kvm_arm_set_default_id_regs(struct kvm *kvm);
+
 /* Guest/host FPSIMD coordination helpers */
 int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu);
 void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu);
arch/arm64/kvm/arm.c +1 -0
index 3bd732eaf087..4579c878ab30 100644
@@ -153,6 +153,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 
 	set_default_spectre(kvm);
 	kvm_arm_init_hypercalls(kvm);
+	kvm_arm_set_default_id_regs(kvm);
 
 	/*
 	 * Initialise the default PMUver before there is a chance to
arch/arm64/kvm/id_regs.c +35 -9

60 lines not on this page · raw message

arch/arm64/kvm/sys_regs.c +1 -1
index 22b60474fcab..568ebc0fb15c 100644
@@ -354,7 +354,7 @@ static bool trap_loregion(struct kvm_vcpu *vcpu,
 			  struct sys_reg_params *p,
 			  const struct sys_reg_desc *r)
 {
-	u64 val = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1);
+	u64 val = kvm_arm_read_id_reg_with_encoding(vcpu, SYS_ID_AA64MMFR1_EL1);
 	u32 sr = reg_to_encoding(r);
 
 	if (!(val & (0xfUL << ID_AA64MMFR1_EL1_LO_SHIFT))) {
arch/arm64/kvm/sys_regs.h +1 -0
index ad41305348f7..9231d89889c7 100644
@@ -261,6 +261,7 @@ int kvm_arm_get_id_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
 int kvm_arm_set_id_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
 bool kvm_arm_check_idreg_table(void);
 int kvm_arm_walk_id_regs(struct kvm_vcpu *vcpu, u64 __user *uind);
+u64 kvm_arm_read_id_reg_with_encoding(const struct kvm_vcpu *vcpu, u32 id);
 
 #define AA32(_x)	.aarch32_map = AA32_##_x
 #define Op0(_x) 	.Op0 = _x
Signature · 1 line
2.39.2.722.g9855ee24e9-goog
#3 lore raw

Re: [PATCH v3 2/6] KVM: arm64: Save ID registers' sanitized value per guest

Hi Jing,

Quoted 27 lines · Jing Zhang

From: Reiji Watanabe <[email protected]>

Introduce id_regs[] in kvm_arch as a storage of guest's ID registers, and save ID registers' sanitized value in the array at KVM_CREATE_VM. Use the saved ones when ID registers are read by the guest or userspace (via KVM_GET_ONE_REG).

No functional change intended.

---
 arch/arm64/include/asm/kvm_host.h | 12 +++++++++
 arch/arm64/kvm/arm.c              |  1 +
 arch/arm64/kvm/id_regs.c          | 44 ++++++++++++++++++++++++-------
 arch/arm64/kvm/sys_regs.c         |  2 +-
 arch/arm64/kvm/sys_regs.h         |  1 +
 5 files changed, 50 insertions(+), 10 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -0
index a1892a8f6032..5c1cec4efa37 100644
@@ -245,6 +245,16 @@ struct kvm_arch {
 	 * the associated pKVM instance in the hypervisor.
 	 */

arch/arm64/include/asm/kvm_host.h struct kvm_arch { · line 256

Quoted 10 lines
arch/arm64/include/asm/kvm_host.h +9 -0
 	struct kvm_protected_vm pkvm;
+
+	/*
+	 * Save ID registers for the guest in id_regs[].
+	 * (Op0, Op1, CRn, CRm, Op2) of the ID registers to be saved in it
+	 * is (3, 0, 0, crm, op2), where 1<=crm<8, 0<=op2<8.
+	 */
+#define KVM_ARM_ID_REG_NUM	56
+#define IDREG_IDX(id)		(((sys_reg_CRm(id) - 1) << 3) | sys_reg_Op2(id))
+#define IDREG(kvm, id)		kvm->arch.id_regs[IDREG_IDX(id)]

I feel like the IDREG(...) macro just obfuscates what is otherwise a simple array access.

arch/arm64/include/asm/kvm_host.h struct kvm_arch {

Quoted 6 lines
arch/arm64/include/asm/kvm_host.h +6 -0
+static u64 read_id_reg(const struct kvm_vcpu *vcpu, struct sys_reg_desc const *r)
+{
+	if (sysreg_visible_as_raz(vcpu, r))
+		return 0;
+
+	return kvm_arm_read_id_reg_with_encoding(vcpu, reg_to_encoding(r));

nit: you could probably drop the '_with_encoding' suffix, as I don't believe there are any other flavors of accessors.

Quoted 15 lines
arch/arm64/include/asm/kvm_host.h +8 -0
+}
+
 /* cpufeature ID register access trap handlers */
 
 static bool access_id_reg(struct kvm_vcpu *vcpu,
@@ -504,3 +505,28 @@ int kvm_arm_walk_id_regs(struct kvm_vcpu *vcpu, u64 __user *uind)
 	}
 	return total;
 }
+
+/*
+ * Set the guest's ID registers that are defined in id_reg_descs[]
+ * with ID_SANITISED() to the host's sanitized value.
+ */
+void kvm_arm_set_default_id_regs(struct kvm *kvm)

arch/arm64/include/asm/kvm_host.h int kvm_arm_walk_id_regs(struct kvm_vcpu *vcpu, u64 __user *uind) · line 523

Quoted 10 lines
arch/arm64/include/asm/kvm_host.h +10 -0
+{
+	int i;
+	u32 id;
+	u64 val;
+
+	for (i = 0; i < ARRAY_SIZE(id_reg_descs); i++) {
+		id = reg_to_encoding(&id_reg_descs[i]);
+		if (WARN_ON_ONCE(!is_id_reg(id)))
+			/* Shouldn't happen */
+			continue;

Could you instead wire in a check to kvm_sys_reg_table_init() or do something similar to it? Benefit of going that route is we outright refuse to run KVM with such an egregious bug.

arch/arm64/include/asm/kvm_host.h int kvm_arm_walk_id_regs(struct kvm_vcpu *vcpu, u64 __user *uind) · line 527

Quoted 4 lines
arch/arm64/include/asm/kvm_host.h +4 -0
+
+		if (id_reg_descs[i].visibility == raz_visibility)
+			/* Hidden or reserved ID register */
+			continue;

This sort of check works only for registers known to be RAZ at compile time, but not for others that depend on runtime configuration. Is it possible to reset the ID register values on the first call to KVM_ARM_VCPU_INIT?

The set of configured features is available at that point so you can actually handle things like SVE and 32-bit ID registers correctly.

Signature · 2 lines
Thanks,
Oliver
author
#4 lore raw

Re: [PATCH v3 2/6] KVM: arm64: Save ID registers' sanitized value per guest

Hi Oliver,

Quoted 36 lines · Oliver Upton

Hi Jing,

Quoted 32 lines · Jing Zhang

From: Reiji Watanabe <[email protected]>

Introduce id_regs[] in kvm_arch as a storage of guest's ID registers, and save ID registers' sanitized value in the array at KVM_CREATE_VM. Use the saved ones when ID registers are read by the guest or userspace (via KVM_GET_ONE_REG).

No functional change intended.

---
 arch/arm64/include/asm/kvm_host.h | 12 +++++++++
 arch/arm64/kvm/arm.c              |  1 +
 arch/arm64/kvm/id_regs.c          | 44 ++++++++++++++++++++++++-------
 arch/arm64/kvm/sys_regs.c         |  2 +-
 arch/arm64/kvm/sys_regs.h         |  1 +
 5 files changed, 50 insertions(+), 10 deletions(-)
arch/arm64/include/asm/kvm_host.h +4 -0
index a1892a8f6032..5c1cec4efa37 100644
@@ -245,6 +245,16 @@ struct kvm_arch {
       * the associated pKVM instance in the hypervisor.
       */
      struct kvm_protected_vm pkvm;
+
+     /*
+      * Save ID registers for the guest in id_regs[].
+      * (Op0, Op1, CRn, CRm, Op2) of the ID registers to be saved in it
Quoted 9 lines

arch/arm64/include/asm/kvm_host.h struct kvm_arch { · line 256

Quoted 5 lines
arch/arm64/include/asm/kvm_host.h +5 -0
+      * is (3, 0, 0, crm, op2), where 1<=crm<8, 0<=op2<8.
+      */
+#define KVM_ARM_ID_REG_NUM   56
+#define IDREG_IDX(id)                (((sys_reg_CRm(id) - 1) << 3) | sys_reg_Op2(id))
+#define IDREG(kvm, id)               kvm->arch.id_regs[IDREG_IDX(id)]

I feel like the IDREG(...) macro just obfuscates what is otherwise a simple array access.

Sure, will use array access.

Quoted 10 lines

arch/arm64/include/asm/kvm_host.h struct kvm_arch {

Quoted 6 lines
arch/arm64/include/asm/kvm_host.h +6 -0
+static u64 read_id_reg(const struct kvm_vcpu *vcpu, struct sys_reg_desc const *r)
+{
+     if (sysreg_visible_as_raz(vcpu, r))
+             return 0;
+
+     return kvm_arm_read_id_reg_with_encoding(vcpu, reg_to_encoding(r));

nit: you could probably drop the '_with_encoding' suffix, as I don't believe there are any other flavors of accessors.

Yes, will do.

Quoted 21 lines
Quoted 21 lines
arch/arm64/include/asm/kvm_host.h +14 -0
+}
+
 /* cpufeature ID register access trap handlers */
 
 static bool access_id_reg(struct kvm_vcpu *vcpu,
@@ -504,3 +505,28 @@ int kvm_arm_walk_id_regs(struct kvm_vcpu *vcpu, u64 __user *uind)
      }
      return total;
 }
+
+/*
+ * Set the guest's ID registers that are defined in id_reg_descs[]
+ * with ID_SANITISED() to the host's sanitized value.
+ */
+void kvm_arm_set_default_id_regs(struct kvm *kvm)
+{
+     int i;
+     u32 id;
+     u64 val;
+
+     for (i = 0; i < ARRAY_SIZE(id_reg_descs); i++) {
Quoted 9 lines

arch/arm64/include/asm/kvm_host.h int kvm_arm_walk_id_regs(struct kvm_vcpu *vcpu, u64 __user *uind) · line 523

Quoted 4 lines
arch/arm64/include/asm/kvm_host.h +4 -0
+             id = reg_to_encoding(&id_reg_descs[i]);
+             if (WARN_ON_ONCE(!is_id_reg(id)))
+                     /* Shouldn't happen */
+                     continue;

Could you instead wire in a check to kvm_sys_reg_table_init() or do something similar to it? Benefit of going that route is we outright refuse to run KVM with such an egregious bug.

The check is already added in the first commit, which is kvm_arm_check_idreg_table() and improved progressively in later commits.

Quoted 5 lines

arch/arm64/include/asm/kvm_host.h int kvm_arm_walk_id_regs(struct kvm_vcpu *vcpu, u64 __user *uind) · line 527

Quoted 4 lines
arch/arm64/include/asm/kvm_host.h +4 -0
+
+             if (id_reg_descs[i].visibility == raz_visibility)
+                     /* Hidden or reserved ID register */
+                     continue;
Quoted 8 lines

This sort of check works only for registers known to be RAZ at compile time, but not for others that depend on runtime configuration. Is it possible to reset the ID register values on the first call to KVM_ARM_VCPU_INIT?

The set of configured features is available at that point so you can actually handle things like SVE and 32-bit ID registers correctly.

This function actually will be replaced with another init function as you suggested in a later commit (6/6), which would be called in kvm_arch_init_vm.

Quoted 3 lines

--
Thanks,
Oliver

author
#5 lore raw

PATCH v3 3/6 KVM: arm64: Use per guest ID register for ID_AA64PFR0_EL1.[CSV2|CSV3]

In reply to Jing Zhang

With per guest ID registers, ID_AA64PFR0_EL1.[CSV2|CSV3] settings from userspace can be stored in its corresponding ID register.

No functional change intended.

---
 arch/arm64/include/asm/kvm_host.h  |  3 +--
 arch/arm64/kvm/arm.c               | 19 +------------------
 arch/arm64/kvm/hyp/nvhe/sys_regs.c |  7 +++----
 arch/arm64/kvm/id_regs.c           | 30 ++++++++++++++++++++++--------
 4 files changed, 27 insertions(+), 32 deletions(-)
arch/arm64/include/asm/kvm_host.h +1 -2
index 5c1cec4efa37..f64347eb77c2 100644
@@ -230,8 +230,6 @@ struct kvm_arch {
 
 	cpumask_var_t supported_cpus;
 
-	u8 pfr0_csv2;
-	u8 pfr0_csv3;
 	struct {
 		u8 imp:4;
 		u8 unimp:4;
@@ -254,6 +252,7 @@ struct kvm_arch {
 #define KVM_ARM_ID_REG_NUM	56
 #define IDREG_IDX(id)		(((sys_reg_CRm(id) - 1) << 3) | sys_reg_Op2(id))
 #define IDREG(kvm, id)		kvm->arch.id_regs[IDREG_IDX(id)]
+#define IDREG_RD(kvm, rd)	IDREG(kvm, reg_to_encoding(rd))
 	u64 id_regs[KVM_ARM_ID_REG_NUM];
 };
 
arch/arm64/kvm/arm.c +1 -18
index 4579c878ab30..c78d68d011cb 100644
@@ -104,22 +104,6 @@ static int kvm_arm_default_max_vcpus(void)
 	return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
 }
 
-static void set_default_spectre(struct kvm *kvm)
-{
-	/*
-	 * The default is to expose CSV2 == 1 if the HW isn't affected.
-	 * Although this is a per-CPU feature, we make it global because
-	 * asymmetric systems are just a nuisance.
-	 *
-	 * Userspace can override this as long as it doesn't promise
-	 * the impossible.
-	 */
-	if (arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED)
-		kvm->arch.pfr0_csv2 = 1;
-	if (arm64_get_meltdown_state() == SPECTRE_UNAFFECTED)
-		kvm->arch.pfr0_csv3 = 1;
-}
-
 /**
  * kvm_arch_init_vm - initializes a VM data structure
  * @kvm:	pointer to the KVM struct
@@ -151,9 +135,8 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 	/* The maximum number of VCPUs is limited by the host's GIC model */
 	kvm->max_vcpus = kvm_arm_default_max_vcpus();
 
-	set_default_spectre(kvm);
-	kvm_arm_init_hypercalls(kvm);
 	kvm_arm_set_default_id_regs(kvm);
+	kvm_arm_init_hypercalls(kvm);
 
 	/*
 	 * Initialise the default PMUver before there is a chance to
arch/arm64/kvm/hyp/nvhe/sys_regs.c +3 -4
index 08d2b004f4b7..ff486e78b32b 100644
@@ -93,10 +93,9 @@ static u64 get_pvm_id_aa64pfr0(const struct kvm_vcpu *vcpu)
 		PVM_ID_AA64PFR0_RESTRICT_UNSIGNED);
 
 	/* Spectre and Meltdown mitigation in KVM */
-	set_mask |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2),
-			       (u64)kvm->arch.pfr0_csv2);
-	set_mask |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3),
-			       (u64)kvm->arch.pfr0_csv3);
+	set_mask |= IDREG(kvm, SYS_ID_AA64PFR0_EL1) &
+		(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2) |
+			ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3));
 
 	return (id_aa64pfr0_el1_sys_val & allow_mask) | set_mask;
 }
arch/arm64/kvm/id_regs.c +22 -8
index 811f01361d12..36859e4caf02 100644
@@ -61,12 +61,6 @@ u64 kvm_arm_read_id_reg_with_encoding(const struct kvm_vcpu *vcpu, u32 id)
 		if (!vcpu_has_sve(vcpu))
 			val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_SVE);
 		val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_AMU);
-		val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2);
-		val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2),
-				  (u64)vcpu->kvm->arch.pfr0_csv2);
-		val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3);
-		val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3),
-				  (u64)vcpu->kvm->arch.pfr0_csv3);
 		if (kvm_vgic_global_state.type == VGIC_V3) {
 			val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_GIC);
 			val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_GIC), 1);
@@ -201,6 +195,7 @@ static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu,
 			       u64 val)
 {
 	u8 csv2, csv3;
+	u64 sval = val;
 
 	/*
 	 * Allow AA64PFR0_EL1.CSV2 to be set from userspace as long as
@@ -225,8 +220,7 @@ static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu,
 	if (val)
 		return -EINVAL;
 
-	vcpu->kvm->arch.pfr0_csv2 = csv2;
-	vcpu->kvm->arch.pfr0_csv3 = csv3;
+	IDREG_RD(vcpu->kvm, rd) = sval;
 
 	return 0;
 }
@@ -529,4 +523,24 @@ void kvm_arm_set_default_id_regs(struct kvm *kvm)
 		val = read_sanitised_ftr_reg(id);
 		IDREG(kvm, id) = val;
 	}
+	/*
+	 * The default is to expose CSV2 == 1 if the HW isn't affected.
+	 * Although this is a per-CPU feature, we make it global because
+	 * asymmetric systems are just a nuisance.
+	 *
+	 * Userspace can override this as long as it doesn't promise
+	 * the impossible.
+	 */
+	val = IDREG(kvm, SYS_ID_AA64PFR0_EL1);
+
+	if (arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED) {
+		val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2);
+		val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2), 1);
+	}
+	if (arm64_get_meltdown_state() == SPECTRE_UNAFFECTED) {
+		val &= ~ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3);
+		val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3), 1);
+	}
+
+	IDREG(kvm, SYS_ID_AA64PFR0_EL1) = val;
 }
Signature · 1 line
2.39.2.722.g9855ee24e9-goog
author
#6 lore raw

PATCH v3 4/6 KVM: arm64: Use per guest ID register for ID_AA64DFR0_EL1.PMUVer

In reply to Jing Zhang

With per guest ID registers, PMUver settings from userspace can be stored in its corresponding ID register.

No functional change intended.

---
 arch/arm64/include/asm/kvm_host.h | 11 ++++---
 arch/arm64/kvm/arm.c              |  6 ----
 arch/arm64/kvm/id_regs.c          | 52 ++++++++++++++++++++++++-------
 include/kvm/arm_pmu.h             |  6 ++--
 4 files changed, 51 insertions(+), 24 deletions(-)
arch/arm64/include/asm/kvm_host.h +6 -5
index f64347eb77c2..effb61a9a855 100644
@@ -218,6 +218,12 @@ struct kvm_arch {
 #define KVM_ARCH_FLAG_EL1_32BIT				4
 	/* PSCI SYSTEM_SUSPEND enabled for the guest */
 #define KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED		5
+	/*
+	 * AA64DFR0_EL1.PMUver was set as ID_AA64DFR0_EL1_PMUVer_IMP_DEF
+	 * or DFR0_EL1.PerfMon was set as ID_DFR0_EL1_PerfMon_IMPDEF from
+	 * userspace for VCPUs without PMU.
+	 */
+#define KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU		6
 
 	unsigned long flags;
 
@@ -230,11 +236,6 @@ struct kvm_arch {
 
 	cpumask_var_t supported_cpus;
 
-	struct {
-		u8 imp:4;
-		u8 unimp:4;
-	} dfr0_pmuver;
-
 	/* Hypercall features firmware registers' descriptor */
 	struct kvm_smccc_features smccc_feat;
 
arch/arm64/kvm/arm.c +0 -6
index c78d68d011cb..fb2de2cb98cb 100644
@@ -138,12 +138,6 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 	kvm_arm_set_default_id_regs(kvm);
 	kvm_arm_init_hypercalls(kvm);
 
-	/*
-	 * Initialise the default PMUver before there is a chance to
-	 * create an actual PMU.
-	 */
-	kvm->arch.dfr0_pmuver.imp = kvm_arm_pmu_get_pmuver_limit();
-
 	return 0;
 
 err_free_cpumask:
arch/arm64/kvm/id_regs.c +41 -11

74 lines not on this page · raw message

include/kvm/arm_pmu.h +4 -2
index 628775334d5e..eef67b7d9751 100644
@@ -92,8 +92,10 @@ void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu);
 /*
  * Evaluates as true when emulating PMUv3p5, and false otherwise.
  */
-#define kvm_pmu_is_3p5(vcpu)						\
-	(vcpu->kvm->arch.dfr0_pmuver.imp >= ID_AA64DFR0_EL1_PMUVer_V3P5)
+#define kvm_pmu_is_3p5(vcpu)									\
+	(kvm_vcpu_has_pmu(vcpu) &&								\
+	 FIELD_GET(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer),					\
+		 IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1)) >= ID_AA64DFR0_EL1_PMUVer_V3P5)
 
 u8 kvm_arm_pmu_get_pmuver_limit(void);
 
Signature · 1 line
2.39.2.722.g9855ee24e9-goog
#7 lore raw

Re: [PATCH v3 4/6] KVM: arm64: Use per guest ID register for ID_AA64DFR0_EL1.PMUVer

Hi Jing,

Quoted 86 lines · Jing Zhang

With per guest ID registers, PMUver settings from userspace can be stored in its corresponding ID register.

No functional change intended.

---
 arch/arm64/include/asm/kvm_host.h | 11 ++++---
 arch/arm64/kvm/arm.c              |  6 ----
 arch/arm64/kvm/id_regs.c          | 52 ++++++++++++++++++++++++-------
 include/kvm/arm_pmu.h             |  6 ++--
 4 files changed, 51 insertions(+), 24 deletions(-)
arch/arm64/include/asm/kvm_host.h +6 -5
index f64347eb77c2..effb61a9a855 100644
@@ -218,6 +218,12 @@ struct kvm_arch {
 #define KVM_ARCH_FLAG_EL1_32BIT                                4
        /* PSCI SYSTEM_SUSPEND enabled for the guest */
 #define KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED           5
+       /*
+        * AA64DFR0_EL1.PMUver was set as ID_AA64DFR0_EL1_PMUVer_IMP_DEF
+        * or DFR0_EL1.PerfMon was set as ID_DFR0_EL1_PerfMon_IMPDEF from
+        * userspace for VCPUs without PMU.
+        */
+#define KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU             6
 
        unsigned long flags;
 
@@ -230,11 +236,6 @@ struct kvm_arch {
 
        cpumask_var_t supported_cpus;
 
-       struct {
-               u8 imp:4;
-               u8 unimp:4;
-       } dfr0_pmuver;
-
        /* Hypercall features firmware registers' descriptor */
        struct kvm_smccc_features smccc_feat;
 
arch/arm64/kvm/arm.c +0 -6
index c78d68d011cb..fb2de2cb98cb 100644
@@ -138,12 +138,6 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
        kvm_arm_set_default_id_regs(kvm);
        kvm_arm_init_hypercalls(kvm);
 
-       /*
-        * Initialise the default PMUver before there is a chance to
-        * create an actual PMU.
-        */
-       kvm->arch.dfr0_pmuver.imp = kvm_arm_pmu_get_pmuver_limit();
-
        return 0;
 
 err_free_cpumask:
arch/arm64/kvm/id_regs.c +6 -5
index 36859e4caf02..21ec8fc10d79 100644
@@ -21,9 +21,12 @@
 static u8 vcpu_pmuver(const struct kvm_vcpu *vcpu)
 {
        if (kvm_vcpu_has_pmu(vcpu))
-               return vcpu->kvm->arch.dfr0_pmuver.imp;
-
-       return vcpu->kvm->arch.dfr0_pmuver.unimp;
+               return FIELD_GET(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer),
+                               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1));
+       else if (test_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags))
+               return ID_AA64DFR0_EL1_PMUVer_IMP_DEF;
+       else
+               return 0;
 }
 
 static u8 perfmon_to_pmuver(u8 perfmon)
@@ -256,10 +259,19 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,
        if (val)
                return -EINVAL;
 
-       if (valid_pmu)
-               vcpu->kvm->arch.dfr0_pmuver.imp = pmuver;

arch/arm64/kvm/id_regs.c static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, · line 269

Quoted 10 lines
arch/arm64/kvm/id_regs.c +8 -2
-       else
-               vcpu->kvm->arch.dfr0_pmuver.unimp = pmuver;
+       if (valid_pmu) {
+               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
+               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) |=
+                       FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer), pmuver);
+
+               IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon);
+               IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) |=
+                       FIELD_PREP(ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon), pmuver);

The pmuver must be converted to perfmon for ID_DFR0_EL1.

Also, I think those registers should be updated atomically, although PMUver specified by userspace will be normally the same for all vCPUs with PMUv3 configured (I have the same comment for set_id_dfr0_el1()).

Quoted 10 lines
arch/arm64/kvm/id_regs.c +5 -0
+       } else if (pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF) {
+               set_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+       } else {
+               clear_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+       }
 
        return 0;
 }
@@ -296,10 +308,19 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
        if (val)

arch/arm64/kvm/id_regs.c static int set_id_dfr0_el1(struct kvm_vcpu *vcpu, · line 314

Quoted 10 lines
arch/arm64/kvm/id_regs.c +4 -4
                return -EINVAL;
 
-       if (valid_pmu)
-               vcpu->kvm->arch.dfr0_pmuver.imp = perfmon_to_pmuver(perfmon);
-       else
-               vcpu->kvm->arch.dfr0_pmuver.unimp = perfmon_to_pmuver(perfmon);
+       if (valid_pmu) {
+               IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon);
+               IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) |= FIELD_PREP(
+                       ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon), perfmon_to_pmuver(perfmon));

The perfmon value should be set for ID_DFR0_EL1 (not pmuver).

Quoted 31 lines
arch/arm64/kvm/id_regs.c +18 -0
+
+               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
+               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) |= FIELD_PREP(
+                       ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer), perfmon_to_pmuver(perfmon));
+       } else if (perfmon == ID_DFR0_EL1_PerfMon_IMPDEF) {
+               set_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+       } else {
+               clear_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+       }
 
        return 0;
 }
@@ -543,4 +564,13 @@ void kvm_arm_set_default_id_regs(struct kvm *kvm)
        }
 
        IDREG(kvm, SYS_ID_AA64PFR0_EL1) = val;
+
+       /*
+        * Initialise the default PMUver before there is a chance to
+        * create an actual PMU.
+        */
+       IDREG(kvm, SYS_ID_AA64DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
+       IDREG(kvm, SYS_ID_AA64DFR0_EL1) |=
+               FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer),
+                          kvm_arm_pmu_get_pmuver_limit());
 }
include/kvm/arm_pmu.h +0 -0
index 628775334d5e..eef67b7d9751 100644

include/kvm/arm_pmu.h void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu); · line 96

Quoted 7 lines
include/kvm/arm_pmu.h +2 -2
@@ -92,8 +92,10 @@ void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu);
 /*
  * Evaluates as true when emulating PMUv3p5, and false otherwise.
  */
-#define kvm_pmu_is_3p5(vcpu)                                           \
-       (vcpu->kvm->arch.dfr0_pmuver.imp >= ID_AA64DFR0_EL1_PMUVer_V3P5)
+#define kvm_pmu_is_3p5(vcpu)                                                                   \
+       (kvm_vcpu_has_pmu(vcpu) &&                                                              \

What is the reason for adding this kvm_vcpu_has_pmu() checking ? I don't think this patch's changes necessitated this.

Quoted 9 lines
include/kvm/arm_pmu.h +2 -1
+        FIELD_GET(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer),                                  \
+                IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1)) >= ID_AA64DFR0_EL1_PMUVer_V3P5)
 
 u8 kvm_arm_pmu_get_pmuver_limit(void);
 
--

2.39.2.722.g9855ee24e9-goog

Thank you,
Reiji

author
#8 lore raw

Re: [PATCH v3 4/6] KVM: arm64: Use per guest ID register for ID_AA64DFR0_EL1.PMUVer

Hi Reiji,

Quoted 88 lines · Reiji Watanabe

Hi Jing,

Quoted 84 lines · Jing Zhang

With per guest ID registers, PMUver settings from userspace can be stored in its corresponding ID register.

No functional change intended.

---
 arch/arm64/include/asm/kvm_host.h | 11 ++++---
 arch/arm64/kvm/arm.c              |  6 ----
 arch/arm64/kvm/id_regs.c          | 52 ++++++++++++++++++++++++-------
 include/kvm/arm_pmu.h             |  6 ++--
 4 files changed, 51 insertions(+), 24 deletions(-)
arch/arm64/include/asm/kvm_host.h +6 -5
index f64347eb77c2..effb61a9a855 100644
@@ -218,6 +218,12 @@ struct kvm_arch {
 #define KVM_ARCH_FLAG_EL1_32BIT                                4
        /* PSCI SYSTEM_SUSPEND enabled for the guest */
 #define KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED           5
+       /*
+        * AA64DFR0_EL1.PMUver was set as ID_AA64DFR0_EL1_PMUVer_IMP_DEF
+        * or DFR0_EL1.PerfMon was set as ID_DFR0_EL1_PerfMon_IMPDEF from
+        * userspace for VCPUs without PMU.
+        */
+#define KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU             6
 
        unsigned long flags;
 
@@ -230,11 +236,6 @@ struct kvm_arch {
 
        cpumask_var_t supported_cpus;
 
-       struct {
-               u8 imp:4;
-               u8 unimp:4;
-       } dfr0_pmuver;
-
        /* Hypercall features firmware registers' descriptor */
        struct kvm_smccc_features smccc_feat;
 
arch/arm64/kvm/arm.c +0 -6
index c78d68d011cb..fb2de2cb98cb 100644
@@ -138,12 +138,6 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
        kvm_arm_set_default_id_regs(kvm);
        kvm_arm_init_hypercalls(kvm);
 
-       /*
-        * Initialise the default PMUver before there is a chance to
-        * create an actual PMU.
-        */
-       kvm->arch.dfr0_pmuver.imp = kvm_arm_pmu_get_pmuver_limit();
-
        return 0;
 
 err_free_cpumask:
arch/arm64/kvm/id_regs.c +6 -3
index 36859e4caf02..21ec8fc10d79 100644
@@ -21,9 +21,12 @@
 static u8 vcpu_pmuver(const struct kvm_vcpu *vcpu)
 {
        if (kvm_vcpu_has_pmu(vcpu))
-               return vcpu->kvm->arch.dfr0_pmuver.imp;
-
-       return vcpu->kvm->arch.dfr0_pmuver.unimp;
+               return FIELD_GET(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer),
+                               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1));
+       else if (test_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags))
+               return ID_AA64DFR0_EL1_PMUVer_IMP_DEF;
+       else
+               return 0;
 }
 
 static u8 perfmon_to_pmuver(u8 perfmon)
@@ -256,10 +259,19 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,
        if (val)
                return -EINVAL;
 
Quoted 14 lines

arch/arm64/kvm/id_regs.c static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, · line 269

Quoted 12 lines
arch/arm64/kvm/id_regs.c +8 -4
-       if (valid_pmu)
-               vcpu->kvm->arch.dfr0_pmuver.imp = pmuver;
-       else
-               vcpu->kvm->arch.dfr0_pmuver.unimp = pmuver;
+       if (valid_pmu) {
+               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
+               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) |=
+                       FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer), pmuver);
+
+               IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon);
+               IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) |=
+                       FIELD_PREP(ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon), pmuver);

The pmuver must be converted to perfmon for ID_DFR0_EL1.

Yes, wil fix it.

Quoted 5 lines

Also, I think those registers should be updated atomically, although PMUver specified by userspace will be normally the same for all vCPUs with PMUv3 configured (I have the same comment for set_id_dfr0_el1()).

I think there is no race condition here. No corrupted data would be set in the field, right?

Quoted 13 lines
Quoted 13 lines
arch/arm64/kvm/id_regs.c +5 -0
+       } else if (pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF) {
+               set_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+       } else {
+               clear_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+       }
 
        return 0;
 }
@@ -296,10 +308,19 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
        if (val)
                return -EINVAL;
 
Quoted 11 lines

arch/arm64/kvm/id_regs.c static int set_id_dfr0_el1(struct kvm_vcpu *vcpu, · line 314

Quoted 8 lines
arch/arm64/kvm/id_regs.c +4 -4
-       if (valid_pmu)
-               vcpu->kvm->arch.dfr0_pmuver.imp = perfmon_to_pmuver(perfmon);
-       else
-               vcpu->kvm->arch.dfr0_pmuver.unimp = perfmon_to_pmuver(perfmon);
+       if (valid_pmu) {
+               IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon);
+               IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) |= FIELD_PREP(
+                       ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon), perfmon_to_pmuver(perfmon));

The perfmon value should be set for ID_DFR0_EL1 (not pmuver).

Sure, will fix it.

Quoted 33 lines
Quoted 33 lines
arch/arm64/kvm/id_regs.c +18 -0
+
+               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
+               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) |= FIELD_PREP(
+                       ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer), perfmon_to_pmuver(perfmon));
+       } else if (perfmon == ID_DFR0_EL1_PerfMon_IMPDEF) {
+               set_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+       } else {
+               clear_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+       }
 
        return 0;
 }
@@ -543,4 +564,13 @@ void kvm_arm_set_default_id_regs(struct kvm *kvm)
        }
 
        IDREG(kvm, SYS_ID_AA64PFR0_EL1) = val;
+
+       /*
+        * Initialise the default PMUver before there is a chance to
+        * create an actual PMU.
+        */
+       IDREG(kvm, SYS_ID_AA64DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
+       IDREG(kvm, SYS_ID_AA64DFR0_EL1) |=
+               FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer),
+                          kvm_arm_pmu_get_pmuver_limit());
 }
include/kvm/arm_pmu.h +0 -0
index 628775334d5e..eef67b7d9751 100644
@@ -92,8 +92,10 @@ void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu);
 /*
  * Evaluates as true when emulating PMUv3p5, and false otherwise.
Quoted 8 lines

include/kvm/arm_pmu.h void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu); · line 96

Quoted 5 lines
include/kvm/arm_pmu.h +2 -2
  */
-#define kvm_pmu_is_3p5(vcpu)                                           \
-       (vcpu->kvm->arch.dfr0_pmuver.imp >= ID_AA64DFR0_EL1_PMUVer_V3P5)
+#define kvm_pmu_is_3p5(vcpu)                                                                   \
+       (kvm_vcpu_has_pmu(vcpu) &&                                                              \

What is the reason for adding this kvm_vcpu_has_pmu() checking ? I don't think this patch's changes necessitated this.

For the same VM, is it possible that some VCPUs would have PMU, but some may not have?
That's why the kvm_vcpu_has_pmu is added here.

Quoted 6 lines
Quoted 6 lines
include/kvm/arm_pmu.h +2 -0
+        FIELD_GET(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer),                                  \
+                IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1)) >= ID_AA64DFR0_EL1_PMUVer_V3P5)
 
 u8 kvm_arm_pmu_get_pmuver_limit(void);
 
Quoted 7 lines
Quoted 4 lines

--
2.39.2.722.g9855ee24e9-goog

Thank you,
Reiji

Thanks,
Jing

#9 lore raw

Re: [PATCH v3 4/6] KVM: arm64: Use per guest ID register for ID_AA64DFR0_EL1.PMUVer

Hi Jing,

Quoted 106 lines · Jing Zhang

Hi Reiji,

Quoted 88 lines · Reiji Watanabe

Hi Jing,

Quoted 84 lines · Jing Zhang

With per guest ID registers, PMUver settings from userspace can be stored in its corresponding ID register.

No functional change intended.

---
 arch/arm64/include/asm/kvm_host.h | 11 ++++---
 arch/arm64/kvm/arm.c              |  6 ----
 arch/arm64/kvm/id_regs.c          | 52 ++++++++++++++++++++++++-------
 include/kvm/arm_pmu.h             |  6 ++--
 4 files changed, 51 insertions(+), 24 deletions(-)
arch/arm64/include/asm/kvm_host.h +6 -5
index f64347eb77c2..effb61a9a855 100644
@@ -218,6 +218,12 @@ struct kvm_arch {
 #define KVM_ARCH_FLAG_EL1_32BIT                                4
        /* PSCI SYSTEM_SUSPEND enabled for the guest */
 #define KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED           5
+       /*
+        * AA64DFR0_EL1.PMUver was set as ID_AA64DFR0_EL1_PMUVer_IMP_DEF
+        * or DFR0_EL1.PerfMon was set as ID_DFR0_EL1_PerfMon_IMPDEF from
+        * userspace for VCPUs without PMU.
+        */
+#define KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU             6
 
        unsigned long flags;
 
@@ -230,11 +236,6 @@ struct kvm_arch {
 
        cpumask_var_t supported_cpus;
 
-       struct {
-               u8 imp:4;
-               u8 unimp:4;
-       } dfr0_pmuver;
-
        /* Hypercall features firmware registers' descriptor */
        struct kvm_smccc_features smccc_feat;
 
arch/arm64/kvm/arm.c +0 -6
index c78d68d011cb..fb2de2cb98cb 100644
@@ -138,12 +138,6 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
        kvm_arm_set_default_id_regs(kvm);
        kvm_arm_init_hypercalls(kvm);
 
-       /*
-        * Initialise the default PMUver before there is a chance to
-        * create an actual PMU.
-        */
-       kvm->arch.dfr0_pmuver.imp = kvm_arm_pmu_get_pmuver_limit();
-
        return 0;
 
 err_free_cpumask:
arch/arm64/kvm/id_regs.c +6 -3
index 36859e4caf02..21ec8fc10d79 100644
@@ -21,9 +21,12 @@
 static u8 vcpu_pmuver(const struct kvm_vcpu *vcpu)
 {
        if (kvm_vcpu_has_pmu(vcpu))
-               return vcpu->kvm->arch.dfr0_pmuver.imp;
-
-       return vcpu->kvm->arch.dfr0_pmuver.unimp;
+               return FIELD_GET(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer),
+                               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1));
+       else if (test_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags))
+               return ID_AA64DFR0_EL1_PMUVer_IMP_DEF;
+       else
+               return 0;
 }
 
 static u8 perfmon_to_pmuver(u8 perfmon)
@@ -256,10 +259,19 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,
        if (val)
                return -EINVAL;
 
Quoted 14 lines

arch/arm64/kvm/id_regs.c static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, · line 269

Quoted 12 lines
arch/arm64/kvm/id_regs.c +8 -4
-       if (valid_pmu)
-               vcpu->kvm->arch.dfr0_pmuver.imp = pmuver;
-       else
-               vcpu->kvm->arch.dfr0_pmuver.unimp = pmuver;
+       if (valid_pmu) {
+               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
+               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) |=
+                       FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer), pmuver);
+
+               IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon);
+               IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) |=
+                       FIELD_PREP(ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon), pmuver);

The pmuver must be converted to perfmon for ID_DFR0_EL1.

Quoted 8 lines

Yes, wil fix it.

Quoted 5 lines

Also, I think those registers should be updated atomically, although PMUver specified by userspace will be normally the same for all vCPUs with PMUv3 configured (I have the same comment for set_id_dfr0_el1()).

I think there is no race condition here. No corrupted data would be set in the field, right?

If userspace tries to set inconsistent values of PMUver/Perfmon for vCPUs with vPMU configured at the same time, PMUver and Perfmon won't be consistent even with this KVM code.
It won't be sane userspace though :)

Quoted 66 lines
Quoted 13 lines
Quoted 13 lines
arch/arm64/kvm/id_regs.c +5 -0
+       } else if (pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF) {
+               set_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+       } else {
+               clear_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+       }
 
        return 0;
 }
@@ -296,10 +308,19 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
        if (val)
                return -EINVAL;
 
Quoted 11 lines

arch/arm64/kvm/id_regs.c static int set_id_dfr0_el1(struct kvm_vcpu *vcpu, · line 314

Quoted 8 lines
arch/arm64/kvm/id_regs.c +4 -4
-       if (valid_pmu)
-               vcpu->kvm->arch.dfr0_pmuver.imp = perfmon_to_pmuver(perfmon);
-       else
-               vcpu->kvm->arch.dfr0_pmuver.unimp = perfmon_to_pmuver(perfmon);
+       if (valid_pmu) {
+               IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon);
+               IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) |= FIELD_PREP(
+                       ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon), perfmon_to_pmuver(perfmon));

The perfmon value should be set for ID_DFR0_EL1 (not pmuver).

Sure, will fix it.

Quoted 33 lines
Quoted 33 lines
arch/arm64/kvm/id_regs.c +18 -0
+
+               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
+               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) |= FIELD_PREP(
+                       ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer), perfmon_to_pmuver(perfmon));
+       } else if (perfmon == ID_DFR0_EL1_PerfMon_IMPDEF) {
+               set_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+       } else {
+               clear_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+       }
 
        return 0;
 }
@@ -543,4 +564,13 @@ void kvm_arm_set_default_id_regs(struct kvm *kvm)
        }
 
        IDREG(kvm, SYS_ID_AA64PFR0_EL1) = val;
+
+       /*
+        * Initialise the default PMUver before there is a chance to
+        * create an actual PMU.
+        */
+       IDREG(kvm, SYS_ID_AA64DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
+       IDREG(kvm, SYS_ID_AA64DFR0_EL1) |=
+               FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer),
+                          kvm_arm_pmu_get_pmuver_limit());
 }
include/kvm/arm_pmu.h +0 -0
index 628775334d5e..eef67b7d9751 100644
@@ -92,8 +92,10 @@ void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu);
 /*
  * Evaluates as true when emulating PMUv3p5, and false otherwise.
Quoted 8 lines

include/kvm/arm_pmu.h void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu); · line 96

Quoted 5 lines
include/kvm/arm_pmu.h +2 -2
  */
-#define kvm_pmu_is_3p5(vcpu)                                           \
-       (vcpu->kvm->arch.dfr0_pmuver.imp >= ID_AA64DFR0_EL1_PMUVer_V3P5)
+#define kvm_pmu_is_3p5(vcpu)                                                                   \
+       (kvm_vcpu_has_pmu(vcpu) &&                                                              \

What is the reason for adding this kvm_vcpu_has_pmu() checking ? I don't think this patch's changes necessitated this.

Quoted 3 lines

For the same VM, is it possible that some VCPUs would have PMU, but some may not have?
That's why the kvm_vcpu_has_pmu is added here.

Yes, it's possible. But, it doesn't appear that this patch or any patches in the series adds a code that newly uses the macro. I believe this macro is always used for the vCPUs with vPMU configured currently.
Did you find a case where this is used for vCPUs with no vPMU ?

If this change tries to address an existing issue, I think it would be nicer to fix this in a separate patch. Or it would be helpful if you could add an explanation in the commit log at least.

Thank you,
Reiji

author
#10 lore raw

Re: [PATCH v3 4/6] KVM: arm64: Use per guest ID register for ID_AA64DFR0_EL1.PMUVer

Hi Reiji,

Quoted 116 lines · Reiji Watanabe

Hi Jing,

Quoted 112 lines · Jing Zhang

Hi Reiji,

Quoted 88 lines · Reiji Watanabe

Hi Jing,

Quoted 84 lines · Jing Zhang

With per guest ID registers, PMUver settings from userspace can be stored in its corresponding ID register.

No functional change intended.

---
 arch/arm64/include/asm/kvm_host.h | 11 ++++---
 arch/arm64/kvm/arm.c              |  6 ----
 arch/arm64/kvm/id_regs.c          | 52 ++++++++++++++++++++++++-------
 include/kvm/arm_pmu.h             |  6 ++--
 4 files changed, 51 insertions(+), 24 deletions(-)
arch/arm64/include/asm/kvm_host.h +6 -5
index f64347eb77c2..effb61a9a855 100644
@@ -218,6 +218,12 @@ struct kvm_arch {
 #define KVM_ARCH_FLAG_EL1_32BIT                                4
        /* PSCI SYSTEM_SUSPEND enabled for the guest */
 #define KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED           5
+       /*
+        * AA64DFR0_EL1.PMUver was set as ID_AA64DFR0_EL1_PMUVer_IMP_DEF
+        * or DFR0_EL1.PerfMon was set as ID_DFR0_EL1_PerfMon_IMPDEF from
+        * userspace for VCPUs without PMU.
+        */
+#define KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU             6
 
        unsigned long flags;
 
@@ -230,11 +236,6 @@ struct kvm_arch {
 
        cpumask_var_t supported_cpus;
 
-       struct {
-               u8 imp:4;
-               u8 unimp:4;
-       } dfr0_pmuver;
-
        /* Hypercall features firmware registers' descriptor */
        struct kvm_smccc_features smccc_feat;
 
arch/arm64/kvm/arm.c +0 -6
index c78d68d011cb..fb2de2cb98cb 100644
@@ -138,12 +138,6 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
        kvm_arm_set_default_id_regs(kvm);
        kvm_arm_init_hypercalls(kvm);
 
-       /*
-        * Initialise the default PMUver before there is a chance to
-        * create an actual PMU.
-        */
-       kvm->arch.dfr0_pmuver.imp = kvm_arm_pmu_get_pmuver_limit();
-
        return 0;
 
 err_free_cpumask:
arch/arm64/kvm/id_regs.c +6 -3
index 36859e4caf02..21ec8fc10d79 100644
@@ -21,9 +21,12 @@
 static u8 vcpu_pmuver(const struct kvm_vcpu *vcpu)
 {
        if (kvm_vcpu_has_pmu(vcpu))
-               return vcpu->kvm->arch.dfr0_pmuver.imp;
-
-       return vcpu->kvm->arch.dfr0_pmuver.unimp;
+               return FIELD_GET(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer),
+                               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1));
+       else if (test_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags))
+               return ID_AA64DFR0_EL1_PMUVer_IMP_DEF;
+       else
+               return 0;
 }
 
 static u8 perfmon_to_pmuver(u8 perfmon)
@@ -256,10 +259,19 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,
        if (val)
                return -EINVAL;
 
Quoted 14 lines

arch/arm64/kvm/id_regs.c static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, · line 269

Quoted 12 lines
arch/arm64/kvm/id_regs.c +8 -4
-       if (valid_pmu)
-               vcpu->kvm->arch.dfr0_pmuver.imp = pmuver;
-       else
-               vcpu->kvm->arch.dfr0_pmuver.unimp = pmuver;
+       if (valid_pmu) {
+               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
+               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) |=
+                       FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer), pmuver);
+
+               IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon);
+               IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) |=
+                       FIELD_PREP(ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon), pmuver);

The pmuver must be converted to perfmon for ID_DFR0_EL1.

Yes, wil fix it.

Quoted 5 lines

Also, I think those registers should be updated atomically, although PMUver specified by userspace will be normally the same for all vCPUs with PMUv3 configured (I have the same comment for set_id_dfr0_el1()).

Quoted 8 lines
Quoted 2 lines

I think there is no race condition here. No corrupted data would be set in the field, right?

If userspace tries to set inconsistent values of PMUver/Perfmon for vCPUs with vPMU configured at the same time, PMUver and Perfmon won't be consistent even with this KVM code.
It won't be sane userspace though :)

I am still not convinced. I don't believe a VM would set AArch64 and AArch32 ID registers at the same time. Anyway, let's see if there are any ideas from others before adding the lockings.

Quoted 76 lines
Quoted 66 lines
Quoted 13 lines
Quoted 13 lines
arch/arm64/kvm/id_regs.c +5 -0
+       } else if (pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF) {
+               set_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+       } else {
+               clear_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+       }
 
        return 0;
 }
@@ -296,10 +308,19 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
        if (val)
                return -EINVAL;
 
Quoted 11 lines

arch/arm64/kvm/id_regs.c static int set_id_dfr0_el1(struct kvm_vcpu *vcpu, · line 314

Quoted 8 lines
arch/arm64/kvm/id_regs.c +4 -4
-       if (valid_pmu)
-               vcpu->kvm->arch.dfr0_pmuver.imp = perfmon_to_pmuver(perfmon);
-       else
-               vcpu->kvm->arch.dfr0_pmuver.unimp = perfmon_to_pmuver(perfmon);
+       if (valid_pmu) {
+               IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon);
+               IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) |= FIELD_PREP(
+                       ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon), perfmon_to_pmuver(perfmon));

The perfmon value should be set for ID_DFR0_EL1 (not pmuver).

Sure, will fix it.

Quoted 33 lines
Quoted 33 lines
arch/arm64/kvm/id_regs.c +18 -0
+
+               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
+               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) |= FIELD_PREP(
+                       ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer), perfmon_to_pmuver(perfmon));
+       } else if (perfmon == ID_DFR0_EL1_PerfMon_IMPDEF) {
+               set_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+       } else {
+               clear_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+       }
 
        return 0;
 }
@@ -543,4 +564,13 @@ void kvm_arm_set_default_id_regs(struct kvm *kvm)
        }
 
        IDREG(kvm, SYS_ID_AA64PFR0_EL1) = val;
+
+       /*
+        * Initialise the default PMUver before there is a chance to
+        * create an actual PMU.
+        */
+       IDREG(kvm, SYS_ID_AA64DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
+       IDREG(kvm, SYS_ID_AA64DFR0_EL1) |=
+               FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer),
+                          kvm_arm_pmu_get_pmuver_limit());
 }
include/kvm/arm_pmu.h +0 -0
index 628775334d5e..eef67b7d9751 100644
@@ -92,8 +92,10 @@ void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu);
 /*
  * Evaluates as true when emulating PMUv3p5, and false otherwise.
Quoted 8 lines

include/kvm/arm_pmu.h void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu); · line 96

Quoted 5 lines
include/kvm/arm_pmu.h +2 -2
  */
-#define kvm_pmu_is_3p5(vcpu)                                           \
-       (vcpu->kvm->arch.dfr0_pmuver.imp >= ID_AA64DFR0_EL1_PMUVer_V3P5)
+#define kvm_pmu_is_3p5(vcpu)                                                                   \
+       (kvm_vcpu_has_pmu(vcpu) &&                                                              \

What is the reason for adding this kvm_vcpu_has_pmu() checking ? I don't think this patch's changes necessitated this.

Quoted 3 lines

For the same VM, is it possible that some VCPUs would have PMU, but some may not have?
That's why the kvm_vcpu_has_pmu is added here.

Yes, it's possible. But, it doesn't appear that this patch or any patches in the series adds a code that newly uses the macro. I believe this macro is always used for the vCPUs with vPMU configured currently.
Did you find a case where this is used for vCPUs with no vPMU ?

Quoted 3 lines

If this change tries to address an existing issue, I think it would be nicer to fix this in a separate patch. Or it would be helpful if you could add an explanation in the commit log at least.

I don't think we should assume the potential users for the macro. Only adding kvm_vcpu_has_pmu() in the macro can have the same semantics as the original macro.
The original macro would return false if it is used by a vCPU without vPMU. I think we should keep it as the same.

Quoted 3 lines

Thank you,
Reiji

Thanks,
Jing

#11 lore raw

Re: [PATCH v3 4/6] KVM: arm64: Use per guest ID register for ID_AA64DFR0_EL1.PMUVer

Quoted 123 lines · Jing Zhang

Hi Reiji,

Quoted 119 lines · Reiji Watanabe

Hi Jing,

Quoted 114 lines · Jing Zhang

Hi Reiji,

Quoted 88 lines · Reiji Watanabe

Hi Jing,

Quoted 84 lines · Jing Zhang

With per guest ID registers, PMUver settings from userspace can be stored in its corresponding ID register.

No functional change intended.

---
 arch/arm64/include/asm/kvm_host.h | 11 ++++---
 arch/arm64/kvm/arm.c              |  6 ----
 arch/arm64/kvm/id_regs.c          | 52 ++++++++++++++++++++++++-------
 include/kvm/arm_pmu.h             |  6 ++--
 4 files changed, 51 insertions(+), 24 deletions(-)
arch/arm64/include/asm/kvm_host.h +6 -5
index f64347eb77c2..effb61a9a855 100644
@@ -218,6 +218,12 @@ struct kvm_arch {
 #define KVM_ARCH_FLAG_EL1_32BIT                                4
        /* PSCI SYSTEM_SUSPEND enabled for the guest */
 #define KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED           5
+       /*
+        * AA64DFR0_EL1.PMUver was set as ID_AA64DFR0_EL1_PMUVer_IMP_DEF
+        * or DFR0_EL1.PerfMon was set as ID_DFR0_EL1_PerfMon_IMPDEF from
+        * userspace for VCPUs without PMU.
+        */
+#define KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU             6
 
        unsigned long flags;
 
@@ -230,11 +236,6 @@ struct kvm_arch {
 
        cpumask_var_t supported_cpus;
 
-       struct {
-               u8 imp:4;
-               u8 unimp:4;
-       } dfr0_pmuver;
-
        /* Hypercall features firmware registers' descriptor */
        struct kvm_smccc_features smccc_feat;
 
arch/arm64/kvm/arm.c +0 -6
index c78d68d011cb..fb2de2cb98cb 100644
@@ -138,12 +138,6 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
        kvm_arm_set_default_id_regs(kvm);
        kvm_arm_init_hypercalls(kvm);
 
-       /*
-        * Initialise the default PMUver before there is a chance to
-        * create an actual PMU.
-        */
-       kvm->arch.dfr0_pmuver.imp = kvm_arm_pmu_get_pmuver_limit();
-
        return 0;
 
 err_free_cpumask:
arch/arm64/kvm/id_regs.c +6 -3
index 36859e4caf02..21ec8fc10d79 100644
@@ -21,9 +21,12 @@
 static u8 vcpu_pmuver(const struct kvm_vcpu *vcpu)
 {
        if (kvm_vcpu_has_pmu(vcpu))
-               return vcpu->kvm->arch.dfr0_pmuver.imp;
-
-       return vcpu->kvm->arch.dfr0_pmuver.unimp;
+               return FIELD_GET(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer),
+                               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1));
+       else if (test_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags))
+               return ID_AA64DFR0_EL1_PMUVer_IMP_DEF;
+       else
+               return 0;
 }
 
 static u8 perfmon_to_pmuver(u8 perfmon)
@@ -256,10 +259,19 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,
        if (val)
                return -EINVAL;
 
Quoted 14 lines

arch/arm64/kvm/id_regs.c static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, · line 269

Quoted 12 lines
arch/arm64/kvm/id_regs.c +8 -4
-       if (valid_pmu)
-               vcpu->kvm->arch.dfr0_pmuver.imp = pmuver;
-       else
-               vcpu->kvm->arch.dfr0_pmuver.unimp = pmuver;
+       if (valid_pmu) {
+               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
+               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) |=
+                       FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer), pmuver);
+
+               IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon);
+               IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) |=
+                       FIELD_PREP(ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon), pmuver);

The pmuver must be converted to perfmon for ID_DFR0_EL1.

Yes, wil fix it.

Quoted 5 lines

Also, I think those registers should be updated atomically, although PMUver specified by userspace will be normally the same for all vCPUs with PMUv3 configured (I have the same comment for set_id_dfr0_el1()).

I think there is no race condition here. No corrupted data would be set in the field, right?

Quoted 7 lines
Quoted 5 lines

If userspace tries to set inconsistent values of PMUver/Perfmon for vCPUs with vPMU configured at the same time, PMUver and Perfmon won't be consistent even with this KVM code.
It won't be sane userspace though :)

I am still not convinced. I don't believe a VM would set AArch64 and AArch32 ID registers at the same time.

Difference threads will set (restore) those registers for different vCPUs in parallel, although those data are shared per VM. (e.g. kvm_arm_set_fw_reg_bmap() addresses the similar case)

Quoted 78 lines

Anyway, let's see if there are
any ideas from others before adding the lockings.

Quoted 76 lines
Quoted 66 lines
Quoted 13 lines
Quoted 13 lines
arch/arm64/kvm/id_regs.c +5 -0
+       } else if (pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF) {
+               set_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+       } else {
+               clear_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+       }
 
        return 0;
 }
@@ -296,10 +308,19 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
        if (val)
                return -EINVAL;
 
Quoted 11 lines

arch/arm64/kvm/id_regs.c static int set_id_dfr0_el1(struct kvm_vcpu *vcpu, · line 314

Quoted 8 lines
arch/arm64/kvm/id_regs.c +4 -4
-       if (valid_pmu)
-               vcpu->kvm->arch.dfr0_pmuver.imp = perfmon_to_pmuver(perfmon);
-       else
-               vcpu->kvm->arch.dfr0_pmuver.unimp = perfmon_to_pmuver(perfmon);
+       if (valid_pmu) {
+               IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon);
+               IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) |= FIELD_PREP(
+                       ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon), perfmon_to_pmuver(perfmon));

The perfmon value should be set for ID_DFR0_EL1 (not pmuver).

Sure, will fix it.

Quoted 33 lines
Quoted 33 lines
arch/arm64/kvm/id_regs.c +18 -0
+
+               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
+               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) |= FIELD_PREP(
+                       ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer), perfmon_to_pmuver(perfmon));
+       } else if (perfmon == ID_DFR0_EL1_PerfMon_IMPDEF) {
+               set_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+       } else {
+               clear_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+       }
 
        return 0;
 }
@@ -543,4 +564,13 @@ void kvm_arm_set_default_id_regs(struct kvm *kvm)
        }
 
        IDREG(kvm, SYS_ID_AA64PFR0_EL1) = val;
+
+       /*
+        * Initialise the default PMUver before there is a chance to
+        * create an actual PMU.
+        */
+       IDREG(kvm, SYS_ID_AA64DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
+       IDREG(kvm, SYS_ID_AA64DFR0_EL1) |=
+               FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer),
+                          kvm_arm_pmu_get_pmuver_limit());
 }
include/kvm/arm_pmu.h +0 -0
index 628775334d5e..eef67b7d9751 100644
@@ -92,8 +92,10 @@ void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu);
 /*
  * Evaluates as true when emulating PMUv3p5, and false otherwise.
Quoted 8 lines

include/kvm/arm_pmu.h void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu); · line 96

Quoted 5 lines
include/kvm/arm_pmu.h +2 -2
  */
-#define kvm_pmu_is_3p5(vcpu)                                           \
-       (vcpu->kvm->arch.dfr0_pmuver.imp >= ID_AA64DFR0_EL1_PMUVer_V3P5)
+#define kvm_pmu_is_3p5(vcpu)                                                                   \
+       (kvm_vcpu_has_pmu(vcpu) &&                                                              \

What is the reason for adding this kvm_vcpu_has_pmu() checking ? I don't think this patch's changes necessitated this.

Quoted 3 lines

For the same VM, is it possible that some VCPUs would have PMU, but some may not have?
That's why the kvm_vcpu_has_pmu is added here.

Yes, it's possible. But, it doesn't appear that this patch or any patches in the series adds a code that newly uses the macro. I believe this macro is always used for the vCPUs with vPMU configured currently.
Did you find a case where this is used for vCPUs with no vPMU ?

Quoted 8 lines
Quoted 3 lines

If this change tries to address an existing issue, I think it would be nicer to fix this in a separate patch. Or it would be helpful if you could add an explanation in the commit log at least.

I don't think we should assume the potential users for the macro. Only adding kvm_vcpu_has_pmu() in the macro can have the same semantics as the original macro.
The original macro would return false if it is used by a vCPU without vPMU. I think we should keep it as the same.

The original macro always uses dfr0_pmuver.imp, which is the PMU version for vCPUs with PMU configured. So, if the macro is used for vCPUs with no PMU configured, it might return true (it depends on the value of dfr0_pmuver.imp).
Or am I missing something ??

Thank you,
Reiji

#12 lore raw

Re: [PATCH v3 4/6] KVM: arm64: Use per guest ID register for ID_AA64DFR0_EL1.PMUVer

Hi Jing,

Quoted 113 lines
Quoted 113 lines
Quoted 113 lines
Quoted 99 lines
Quoted 85 lines · Jing Zhang
Quoted 84 lines

With per guest ID registers, PMUver settings from userspace can be stored in its corresponding ID register.

No functional change intended.

---
 arch/arm64/include/asm/kvm_host.h | 11 ++++---
 arch/arm64/kvm/arm.c              |  6 ----
 arch/arm64/kvm/id_regs.c          | 52 ++++++++++++++++++++++++-------
 include/kvm/arm_pmu.h             |  6 ++--
 4 files changed, 51 insertions(+), 24 deletions(-)
arch/arm64/include/asm/kvm_host.h +6 -5
index f64347eb77c2..effb61a9a855 100644
@@ -218,6 +218,12 @@ struct kvm_arch {
 #define KVM_ARCH_FLAG_EL1_32BIT                                4
        /* PSCI SYSTEM_SUSPEND enabled for the guest */
 #define KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED           5
+       /*
+        * AA64DFR0_EL1.PMUver was set as ID_AA64DFR0_EL1_PMUVer_IMP_DEF
+        * or DFR0_EL1.PerfMon was set as ID_DFR0_EL1_PerfMon_IMPDEF from
+        * userspace for VCPUs without PMU.
+        */
+#define KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU             6
 
        unsigned long flags;
 
@@ -230,11 +236,6 @@ struct kvm_arch {
 
        cpumask_var_t supported_cpus;
 
-       struct {
-               u8 imp:4;
-               u8 unimp:4;
-       } dfr0_pmuver;
-
        /* Hypercall features firmware registers' descriptor */
        struct kvm_smccc_features smccc_feat;
 
arch/arm64/kvm/arm.c +0 -6
index c78d68d011cb..fb2de2cb98cb 100644
@@ -138,12 +138,6 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
        kvm_arm_set_default_id_regs(kvm);
        kvm_arm_init_hypercalls(kvm);
 
-       /*
-        * Initialise the default PMUver before there is a chance to
-        * create an actual PMU.
-        */
-       kvm->arch.dfr0_pmuver.imp = kvm_arm_pmu_get_pmuver_limit();
-
        return 0;
 
 err_free_cpumask:
arch/arm64/kvm/id_regs.c +6 -3
index 36859e4caf02..21ec8fc10d79 100644
@@ -21,9 +21,12 @@
 static u8 vcpu_pmuver(const struct kvm_vcpu *vcpu)
 {
        if (kvm_vcpu_has_pmu(vcpu))
-               return vcpu->kvm->arch.dfr0_pmuver.imp;
-
-       return vcpu->kvm->arch.dfr0_pmuver.unimp;
+               return FIELD_GET(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer),
+                               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1));
+       else if (test_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags))
+               return ID_AA64DFR0_EL1_PMUVer_IMP_DEF;
+       else
+               return 0;
 }
 
 static u8 perfmon_to_pmuver(u8 perfmon)
@@ -256,10 +259,19 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,
        if (val)
                return -EINVAL;
 
Quoted 14 lines

arch/arm64/kvm/id_regs.c static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, · line 269

Quoted 12 lines
arch/arm64/kvm/id_regs.c +8 -4
-       if (valid_pmu)
-               vcpu->kvm->arch.dfr0_pmuver.imp = pmuver;
-       else
-               vcpu->kvm->arch.dfr0_pmuver.unimp = pmuver;
+       if (valid_pmu) {
+               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
+               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) |=
+                       FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer), pmuver);
+
+               IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon);
+               IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) |=
+                       FIELD_PREP(ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon), pmuver);

The pmuver must be converted to perfmon for ID_DFR0_EL1.

Quoted 8 lines

Yes, wil fix it.

Quoted 5 lines

Also, I think those registers should be updated atomically, although PMUver specified by userspace will be normally the same for all vCPUs with PMUv3 configured (I have the same comment for set_id_dfr0_el1()).

I think there is no race condition here. No corrupted data would be set in the field, right?

If userspace tries to set inconsistent values of PMUver/Perfmon for vCPUs with vPMU configured at the same time, PMUver and Perfmon won't be consistent even with this KVM code.
It won't be sane userspace though :)

Quoted 6 lines
Quoted 2 lines

I am still not convinced. I don't believe a VM would set AArch64 and AArch32 ID registers at the same time.

Difference threads will set (restore) those registers for different vCPUs in parallel, although those data are shared per VM. (e.g. kvm_arm_set_fw_reg_bmap() addresses the similar case)

I'm sorry, this example is misleading (my memory was corrupted)... This is just an example of the code that gets a per VM lock in KVM_SET_ONE_REG (It gets the lock to prevent from modifying the registers after the first KVM_RUN for the guest).

Thank you,
Reiji

Quoted 96 lines
Quoted 79 lines

Anyway, let's see if there are
any ideas from others before adding the lockings.

Quoted 76 lines
Quoted 66 lines
Quoted 13 lines
Quoted 13 lines
arch/arm64/kvm/id_regs.c +5 -0
+       } else if (pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF) {
+               set_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+       } else {
+               clear_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+       }
 
        return 0;
 }
@@ -296,10 +308,19 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
        if (val)
                return -EINVAL;
 
Quoted 11 lines

arch/arm64/kvm/id_regs.c static int set_id_dfr0_el1(struct kvm_vcpu *vcpu, · line 314

Quoted 8 lines
arch/arm64/kvm/id_regs.c +4 -4
-       if (valid_pmu)
-               vcpu->kvm->arch.dfr0_pmuver.imp = perfmon_to_pmuver(perfmon);
-       else
-               vcpu->kvm->arch.dfr0_pmuver.unimp = perfmon_to_pmuver(perfmon);
+       if (valid_pmu) {
+               IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon);
+               IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) |= FIELD_PREP(
+                       ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon), perfmon_to_pmuver(perfmon));

The perfmon value should be set for ID_DFR0_EL1 (not pmuver).

Sure, will fix it.

Quoted 33 lines
Quoted 33 lines
arch/arm64/kvm/id_regs.c +18 -0
+
+               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
+               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) |= FIELD_PREP(
+                       ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer), perfmon_to_pmuver(perfmon));
+       } else if (perfmon == ID_DFR0_EL1_PerfMon_IMPDEF) {
+               set_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+       } else {
+               clear_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+       }
 
        return 0;
 }
@@ -543,4 +564,13 @@ void kvm_arm_set_default_id_regs(struct kvm *kvm)
        }
 
        IDREG(kvm, SYS_ID_AA64PFR0_EL1) = val;
+
+       /*
+        * Initialise the default PMUver before there is a chance to
+        * create an actual PMU.
+        */
+       IDREG(kvm, SYS_ID_AA64DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
+       IDREG(kvm, SYS_ID_AA64DFR0_EL1) |=
+               FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer),
+                          kvm_arm_pmu_get_pmuver_limit());
 }
include/kvm/arm_pmu.h +0 -0
index 628775334d5e..eef67b7d9751 100644
@@ -92,8 +92,10 @@ void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu);
 /*
  * Evaluates as true when emulating PMUv3p5, and false otherwise.
Quoted 8 lines

include/kvm/arm_pmu.h void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu); · line 96

Quoted 5 lines
include/kvm/arm_pmu.h +2 -2
  */
-#define kvm_pmu_is_3p5(vcpu)                                           \
-       (vcpu->kvm->arch.dfr0_pmuver.imp >= ID_AA64DFR0_EL1_PMUVer_V3P5)
+#define kvm_pmu_is_3p5(vcpu)                                                                   \
+       (kvm_vcpu_has_pmu(vcpu) &&                                                              \

What is the reason for adding this kvm_vcpu_has_pmu() checking ? I don't think this patch's changes necessitated this.

Quoted 3 lines

For the same VM, is it possible that some VCPUs would have PMU, but some may not have?
That's why the kvm_vcpu_has_pmu is added here.

Yes, it's possible. But, it doesn't appear that this patch or any patches in the series adds a code that newly uses the macro. I believe this macro is always used for the vCPUs with vPMU configured currently.
Did you find a case where this is used for vCPUs with no vPMU ?

Quoted 8 lines
Quoted 3 lines

If this change tries to address an existing issue, I think it would be nicer to fix this in a separate patch. Or it would be helpful if you could add an explanation in the commit log at least.

I don't think we should assume the potential users for the macro. Only adding kvm_vcpu_has_pmu() in the macro can have the same semantics as the original macro.
The original macro would return false if it is used by a vCPU without vPMU. I think we should keep it as the same.

The original macro always uses dfr0_pmuver.imp, which is the PMU version for vCPUs with PMU configured. So, if the macro is used for vCPUs with no PMU configured, it might return true (it depends on the value of dfr0_pmuver.imp).
Or am I missing something ??

Thank you,
Reiji

author
#13 lore raw

Re: [PATCH v3 4/6] KVM: arm64: Use per guest ID register for ID_AA64DFR0_EL1.PMUVer

Hi Reiji,

Quoted 119 lines · Reiji Watanabe

Hi Jing,

Quoted 116 lines
Quoted 115 lines
Quoted 105 lines
Quoted 105 lines
Quoted 85 lines · Jing Zhang
Quoted 84 lines

With per guest ID registers, PMUver settings from userspace can be stored in its corresponding ID register.

No functional change intended.

---
 arch/arm64/include/asm/kvm_host.h | 11 ++++---
 arch/arm64/kvm/arm.c              |  6 ----
 arch/arm64/kvm/id_regs.c          | 52 ++++++++++++++++++++++++-------
 include/kvm/arm_pmu.h             |  6 ++--
 4 files changed, 51 insertions(+), 24 deletions(-)
arch/arm64/include/asm/kvm_host.h +6 -5
index f64347eb77c2..effb61a9a855 100644
@@ -218,6 +218,12 @@ struct kvm_arch {
 #define KVM_ARCH_FLAG_EL1_32BIT                                4
        /* PSCI SYSTEM_SUSPEND enabled for the guest */
 #define KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED           5
+       /*
+        * AA64DFR0_EL1.PMUver was set as ID_AA64DFR0_EL1_PMUVer_IMP_DEF
+        * or DFR0_EL1.PerfMon was set as ID_DFR0_EL1_PerfMon_IMPDEF from
+        * userspace for VCPUs without PMU.
+        */
+#define KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU             6
 
        unsigned long flags;
 
@@ -230,11 +236,6 @@ struct kvm_arch {
 
        cpumask_var_t supported_cpus;
 
-       struct {
-               u8 imp:4;
-               u8 unimp:4;
-       } dfr0_pmuver;
-
        /* Hypercall features firmware registers' descriptor */
        struct kvm_smccc_features smccc_feat;
 
arch/arm64/kvm/arm.c +0 -6
index c78d68d011cb..fb2de2cb98cb 100644
@@ -138,12 +138,6 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
        kvm_arm_set_default_id_regs(kvm);
        kvm_arm_init_hypercalls(kvm);
 
-       /*
-        * Initialise the default PMUver before there is a chance to
-        * create an actual PMU.
-        */
-       kvm->arch.dfr0_pmuver.imp = kvm_arm_pmu_get_pmuver_limit();
-
        return 0;
 
 err_free_cpumask:
arch/arm64/kvm/id_regs.c +6 -3
index 36859e4caf02..21ec8fc10d79 100644
@@ -21,9 +21,12 @@
 static u8 vcpu_pmuver(const struct kvm_vcpu *vcpu)
 {
        if (kvm_vcpu_has_pmu(vcpu))
-               return vcpu->kvm->arch.dfr0_pmuver.imp;
-
-       return vcpu->kvm->arch.dfr0_pmuver.unimp;
+               return FIELD_GET(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer),
+                               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1));
+       else if (test_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags))
+               return ID_AA64DFR0_EL1_PMUVer_IMP_DEF;
+       else
+               return 0;
 }
 
 static u8 perfmon_to_pmuver(u8 perfmon)
@@ -256,10 +259,19 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,
        if (val)
                return -EINVAL;
 
Quoted 14 lines

arch/arm64/kvm/id_regs.c static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, · line 269

Quoted 12 lines
arch/arm64/kvm/id_regs.c +8 -4
-       if (valid_pmu)
-               vcpu->kvm->arch.dfr0_pmuver.imp = pmuver;
-       else
-               vcpu->kvm->arch.dfr0_pmuver.unimp = pmuver;
+       if (valid_pmu) {
+               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
+               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) |=
+                       FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer), pmuver);
+
+               IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon);
+               IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) |=
+                       FIELD_PREP(ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon), pmuver);

The pmuver must be converted to perfmon for ID_DFR0_EL1.

Yes, wil fix it.

Quoted 5 lines

Also, I think those registers should be updated atomically, although PMUver specified by userspace will be normally the same for all vCPUs with PMUv3 configured (I have the same comment for set_id_dfr0_el1()).

Quoted 8 lines
Quoted 2 lines

I think there is no race condition here. No corrupted data would be set in the field, right?

If userspace tries to set inconsistent values of PMUver/Perfmon for vCPUs with vPMU configured at the same time, PMUver and Perfmon won't be consistent even with this KVM code.
It won't be sane userspace though :)

I am still not convinced. I don't believe a VM would set AArch64 and AArch32 ID registers at the same time.

Quoted 8 lines
Quoted 3 lines

Difference threads will set (restore) those registers for different vCPUs in parallel, although those data are shared per VM. (e.g. kvm_arm_set_fw_reg_bmap() addresses the similar case)

I'm sorry, this example is misleading (my memory was corrupted)... This is just an example of the code that gets a per VM lock in KVM_SET_ONE_REG (It gets the lock to prevent from modifying the registers after the first KVM_RUN for the guest).

Thanks for the details, I'll use the per VM lock to guarantee the atomicity.

Quoted 101 lines

Thank you,
Reiji

Quoted 96 lines
Quoted 79 lines

Anyway, let's see if there are
any ideas from others before adding the lockings.

Quoted 76 lines
Quoted 66 lines
Quoted 13 lines
Quoted 13 lines
arch/arm64/kvm/id_regs.c +5 -0
+       } else if (pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF) {
+               set_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+       } else {
+               clear_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+       }
 
        return 0;
 }
@@ -296,10 +308,19 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
        if (val)
                return -EINVAL;
 
Quoted 11 lines

arch/arm64/kvm/id_regs.c static int set_id_dfr0_el1(struct kvm_vcpu *vcpu, · line 314

Quoted 8 lines
arch/arm64/kvm/id_regs.c +4 -4
-       if (valid_pmu)
-               vcpu->kvm->arch.dfr0_pmuver.imp = perfmon_to_pmuver(perfmon);
-       else
-               vcpu->kvm->arch.dfr0_pmuver.unimp = perfmon_to_pmuver(perfmon);
+       if (valid_pmu) {
+               IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon);
+               IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) |= FIELD_PREP(
+                       ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon), perfmon_to_pmuver(perfmon));

The perfmon value should be set for ID_DFR0_EL1 (not pmuver).

Sure, will fix it.

Quoted 33 lines
Quoted 33 lines
arch/arm64/kvm/id_regs.c +18 -0
+
+               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
+               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) |= FIELD_PREP(
+                       ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer), perfmon_to_pmuver(perfmon));
+       } else if (perfmon == ID_DFR0_EL1_PerfMon_IMPDEF) {
+               set_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+       } else {
+               clear_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+       }
 
        return 0;
 }
@@ -543,4 +564,13 @@ void kvm_arm_set_default_id_regs(struct kvm *kvm)
        }
 
        IDREG(kvm, SYS_ID_AA64PFR0_EL1) = val;
+
+       /*
+        * Initialise the default PMUver before there is a chance to
+        * create an actual PMU.
+        */
+       IDREG(kvm, SYS_ID_AA64DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
+       IDREG(kvm, SYS_ID_AA64DFR0_EL1) |=
+               FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer),
+                          kvm_arm_pmu_get_pmuver_limit());
 }
include/kvm/arm_pmu.h +0 -0
index 628775334d5e..eef67b7d9751 100644
@@ -92,8 +92,10 @@ void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu);
 /*
  * Evaluates as true when emulating PMUv3p5, and false otherwise.
Quoted 8 lines

include/kvm/arm_pmu.h void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu); · line 96

Quoted 5 lines
include/kvm/arm_pmu.h +2 -2
  */
-#define kvm_pmu_is_3p5(vcpu)                                           \
-       (vcpu->kvm->arch.dfr0_pmuver.imp >= ID_AA64DFR0_EL1_PMUVer_V3P5)
+#define kvm_pmu_is_3p5(vcpu)                                                                   \
+       (kvm_vcpu_has_pmu(vcpu) &&                                                              \

What is the reason for adding this kvm_vcpu_has_pmu() checking ? I don't think this patch's changes necessitated this.

Quoted 3 lines

For the same VM, is it possible that some VCPUs would have PMU, but some may not have?
That's why the kvm_vcpu_has_pmu is added here.

Yes, it's possible. But, it doesn't appear that this patch or any patches in the series adds a code that newly uses the macro. I believe this macro is always used for the vCPUs with vPMU configured currently.
Did you find a case where this is used for vCPUs with no vPMU ?

Quoted 8 lines
Quoted 3 lines

If this change tries to address an existing issue, I think it would be nicer to fix this in a separate patch. Or it would be helpful if you could add an explanation in the commit log at least.

I don't think we should assume the potential users for the macro. Only adding kvm_vcpu_has_pmu() in the macro can have the same semantics as the original macro.
The original macro would return false if it is used by a vCPU without vPMU. I think we should keep it as the same.

The original macro always uses dfr0_pmuver.imp, which is the PMU version for vCPUs with PMU configured. So, if the macro is used for vCPUs with no PMU configured, it might return true (it depends on the value of dfr0_pmuver.imp).
Or am I missing something ??

Thank you,
Reiji

Thanks,
Jing

author
#14 lore raw

Re: [PATCH v3 4/6] KVM: arm64: Use per guest ID register for ID_AA64DFR0_EL1.PMUVer

In reply to Reiji Watanabe

Hi Reiji,

Quoted 224 lines · Reiji Watanabe
Quoted 123 lines · Jing Zhang

Hi Reiji,

Quoted 119 lines · Reiji Watanabe

Hi Jing,

Quoted 114 lines · Jing Zhang

Hi Reiji,

Quoted 88 lines · Reiji Watanabe

Hi Jing,

Quoted 84 lines · Jing Zhang

With per guest ID registers, PMUver settings from userspace can be stored in its corresponding ID register.

No functional change intended.

---
 arch/arm64/include/asm/kvm_host.h | 11 ++++---
 arch/arm64/kvm/arm.c              |  6 ----
 arch/arm64/kvm/id_regs.c          | 52 ++++++++++++++++++++++++-------
 include/kvm/arm_pmu.h             |  6 ++--
 4 files changed, 51 insertions(+), 24 deletions(-)
arch/arm64/include/asm/kvm_host.h +6 -5
index f64347eb77c2..effb61a9a855 100644
@@ -218,6 +218,12 @@ struct kvm_arch {
 #define KVM_ARCH_FLAG_EL1_32BIT                                4
        /* PSCI SYSTEM_SUSPEND enabled for the guest */
 #define KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED           5
+       /*
+        * AA64DFR0_EL1.PMUver was set as ID_AA64DFR0_EL1_PMUVer_IMP_DEF
+        * or DFR0_EL1.PerfMon was set as ID_DFR0_EL1_PerfMon_IMPDEF from
+        * userspace for VCPUs without PMU.
+        */
+#define KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU             6
 
        unsigned long flags;
 
@@ -230,11 +236,6 @@ struct kvm_arch {
 
        cpumask_var_t supported_cpus;
 
-       struct {
-               u8 imp:4;
-               u8 unimp:4;
-       } dfr0_pmuver;
-
        /* Hypercall features firmware registers' descriptor */
        struct kvm_smccc_features smccc_feat;
 
arch/arm64/kvm/arm.c +0 -6
index c78d68d011cb..fb2de2cb98cb 100644
@@ -138,12 +138,6 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
        kvm_arm_set_default_id_regs(kvm);
        kvm_arm_init_hypercalls(kvm);
 
-       /*
-        * Initialise the default PMUver before there is a chance to
-        * create an actual PMU.
-        */
-       kvm->arch.dfr0_pmuver.imp = kvm_arm_pmu_get_pmuver_limit();
-
        return 0;
 
 err_free_cpumask:
arch/arm64/kvm/id_regs.c +6 -3
index 36859e4caf02..21ec8fc10d79 100644
@@ -21,9 +21,12 @@
 static u8 vcpu_pmuver(const struct kvm_vcpu *vcpu)
 {
        if (kvm_vcpu_has_pmu(vcpu))
-               return vcpu->kvm->arch.dfr0_pmuver.imp;
-
-       return vcpu->kvm->arch.dfr0_pmuver.unimp;
+               return FIELD_GET(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer),
+                               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1));
+       else if (test_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags))
+               return ID_AA64DFR0_EL1_PMUVer_IMP_DEF;
+       else
+               return 0;
 }
 
 static u8 perfmon_to_pmuver(u8 perfmon)
@@ -256,10 +259,19 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,
        if (val)
                return -EINVAL;
 
Quoted 14 lines

arch/arm64/kvm/id_regs.c static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, · line 269

Quoted 12 lines
arch/arm64/kvm/id_regs.c +8 -4
-       if (valid_pmu)
-               vcpu->kvm->arch.dfr0_pmuver.imp = pmuver;
-       else
-               vcpu->kvm->arch.dfr0_pmuver.unimp = pmuver;
+       if (valid_pmu) {
+               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
+               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) |=
+                       FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer), pmuver);
+
+               IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon);
+               IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) |=
+                       FIELD_PREP(ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon), pmuver);

The pmuver must be converted to perfmon for ID_DFR0_EL1.

Yes, wil fix it.

Quoted 5 lines

Also, I think those registers should be updated atomically, although PMUver specified by userspace will be normally the same for all vCPUs with PMUv3 configured (I have the same comment for set_id_dfr0_el1()).

I think there is no race condition here. No corrupted data would be set in the field, right?

Quoted 7 lines
Quoted 5 lines

If userspace tries to set inconsistent values of PMUver/Perfmon for vCPUs with vPMU configured at the same time, PMUver and Perfmon won't be consistent even with this KVM code.
It won't be sane userspace though :)

I am still not convinced. I don't believe a VM would set AArch64 and AArch32 ID registers at the same time.

Difference threads will set (restore) those registers for different vCPUs in parallel, although those data are shared per VM. (e.g. kvm_arm_set_fw_reg_bmap() addresses the similar case)

Quoted 78 lines

Anyway, let's see if there are
any ideas from others before adding the lockings.

Quoted 76 lines
Quoted 66 lines
Quoted 13 lines
Quoted 13 lines
arch/arm64/kvm/id_regs.c +5 -0
+       } else if (pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF) {
+               set_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+       } else {
+               clear_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+       }
 
        return 0;
 }
@@ -296,10 +308,19 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
        if (val)
                return -EINVAL;
 
Quoted 11 lines

arch/arm64/kvm/id_regs.c static int set_id_dfr0_el1(struct kvm_vcpu *vcpu, · line 314

Quoted 8 lines
arch/arm64/kvm/id_regs.c +4 -4
-       if (valid_pmu)
-               vcpu->kvm->arch.dfr0_pmuver.imp = perfmon_to_pmuver(perfmon);
-       else
-               vcpu->kvm->arch.dfr0_pmuver.unimp = perfmon_to_pmuver(perfmon);
+       if (valid_pmu) {
+               IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon);
+               IDREG(vcpu->kvm, SYS_ID_DFR0_EL1) |= FIELD_PREP(
+                       ARM64_FEATURE_MASK(ID_DFR0_EL1_PerfMon), perfmon_to_pmuver(perfmon));

The perfmon value should be set for ID_DFR0_EL1 (not pmuver).

Sure, will fix it.

Quoted 33 lines
Quoted 33 lines
arch/arm64/kvm/id_regs.c +18 -0
+
+               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
+               IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1) |= FIELD_PREP(
+                       ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer), perfmon_to_pmuver(perfmon));
+       } else if (perfmon == ID_DFR0_EL1_PerfMon_IMPDEF) {
+               set_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+       } else {
+               clear_bit(KVM_ARCH_FLAG_VCPU_HAS_IMP_DEF_PMU, &vcpu->kvm->arch.flags);
+       }
 
        return 0;
 }
@@ -543,4 +564,13 @@ void kvm_arm_set_default_id_regs(struct kvm *kvm)
        }
 
        IDREG(kvm, SYS_ID_AA64PFR0_EL1) = val;
+
+       /*
+        * Initialise the default PMUver before there is a chance to
+        * create an actual PMU.
+        */
+       IDREG(kvm, SYS_ID_AA64DFR0_EL1) &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
+       IDREG(kvm, SYS_ID_AA64DFR0_EL1) |=
+               FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer),
+                          kvm_arm_pmu_get_pmuver_limit());
 }
include/kvm/arm_pmu.h +0 -0
index 628775334d5e..eef67b7d9751 100644
@@ -92,8 +92,10 @@ void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu);
 /*
  * Evaluates as true when emulating PMUv3p5, and false otherwise.
Quoted 8 lines

include/kvm/arm_pmu.h void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu); · line 96

Quoted 5 lines
include/kvm/arm_pmu.h +2 -2
  */
-#define kvm_pmu_is_3p5(vcpu)                                           \
-       (vcpu->kvm->arch.dfr0_pmuver.imp >= ID_AA64DFR0_EL1_PMUVer_V3P5)
+#define kvm_pmu_is_3p5(vcpu)                                                                   \
+       (kvm_vcpu_has_pmu(vcpu) &&                                                              \

What is the reason for adding this kvm_vcpu_has_pmu() checking ? I don't think this patch's changes necessitated this.

Quoted 3 lines

For the same VM, is it possible that some VCPUs would have PMU, but some may not have?
That's why the kvm_vcpu_has_pmu is added here.

Yes, it's possible. But, it doesn't appear that this patch or any patches in the series adds a code that newly uses the macro. I believe this macro is always used for the vCPUs with vPMU configured currently.
Did you find a case where this is used for vCPUs with no vPMU ?

Quoted 8 lines
Quoted 3 lines

If this change tries to address an existing issue, I think it would be nicer to fix this in a separate patch. Or it would be helpful if you could add an explanation in the commit log at least.

I don't think we should assume the potential users for the macro. Only adding kvm_vcpu_has_pmu() in the macro can have the same semantics as the original macro.
The original macro would return false if it is used by a vCPU without vPMU. I think we should keep it as the same.

Quoted 5 lines

The original macro always uses dfr0_pmuver.imp, which is the PMU version for vCPUs with PMU configured. So, if the macro is used for vCPUs with no PMU configured, it might return true (it depends on the value of dfr0_pmuver.imp).
Or am I missing something ??

After double checking the code, I think you are right. The kvm_vcpu_has_pmu() is not necessary.

Quoted 3 lines

Thank you,
Reiji

Thanks,
Jing

author
#15 lore raw

PATCH v3 5/6 KVM: arm64: Introduce ID register specific descriptor

In reply to Jing Zhang

Introduce an ID feature register specific descriptor to include ID register specific fields and callbacks besides its corresponding general system register descriptor.
New fields for ID register descriptor would be added later when it is necessary to support a writable ID register.

No functional change intended.

---
 arch/arm64/kvm/id_regs.c  | 184 ++++++++++++++++++++++++++++----------
 arch/arm64/kvm/sys_regs.c |   2 +-
 arch/arm64/kvm/sys_regs.h |   1 +
 3 files changed, 138 insertions(+), 49 deletions(-)
arch/arm64/kvm/id_regs.c +136 -48
index 21ec8fc10d79..fc0dcd557cbb 100644
@@ -18,6 +18,10 @@
 
 #include "sys_regs.h"
 
+struct id_reg_desc {
+	const struct sys_reg_desc	reg_desc;
+};
+
 static u8 vcpu_pmuver(const struct kvm_vcpu *vcpu)
 {
 	if (kvm_vcpu_has_pmu(vcpu))
@@ -326,21 +330,25 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
 }
 
 /* sys_reg_desc initialiser for known cpufeature ID registers */
-#define ID_SANITISED(name) {			\
-	SYS_DESC(SYS_##name),			\
-	.access	= access_id_reg,		\
-	.get_user = get_id_reg,			\
-	.set_user = set_id_reg,			\
-	.visibility = id_visibility,		\
+#define ID_SANITISED(name) {				\
+	.reg_desc = {					\
+		SYS_DESC(SYS_##name),			\
+		.access	= access_id_reg,		\
+		.get_user = get_id_reg,			\
+		.set_user = set_id_reg,			\
+		.visibility = id_visibility,		\
+	},						\
 }
 
 /* sys_reg_desc initialiser for known cpufeature ID registers */
-#define AA32_ID_SANITISED(name) {		\
-	SYS_DESC(SYS_##name),			\
-	.access	= access_id_reg,		\
-	.get_user = get_id_reg,			\
-	.set_user = set_id_reg,			\
-	.visibility = aa32_id_visibility,	\
+#define AA32_ID_SANITISED(name) {			\
+	.reg_desc = {					\
+		SYS_DESC(SYS_##name),			\
+		.access	= access_id_reg,		\
+		.get_user = get_id_reg,			\
+		.set_user = set_id_reg,			\
+		.visibility = aa32_id_visibility,	\
+	},						\
 }
 
 /*
@@ -348,12 +356,14 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
  * register with encoding Op0=3, Op1=0, CRn=0, CRm=crm, Op2=op2
  * (1 <= crm < 8, 0 <= Op2 < 8).
  */
-#define ID_UNALLOCATED(crm, op2) {			\
-	Op0(3), Op1(0), CRn(0), CRm(crm), Op2(op2),	\
-	.access = access_id_reg,			\
-	.get_user = get_id_reg,				\
-	.set_user = set_id_reg,				\
-	.visibility = raz_visibility			\
+#define ID_UNALLOCATED(crm, op2) {				\
+	.reg_desc = {						\
+		Op0(3), Op1(0), CRn(0), CRm(crm), Op2(op2),	\
+		.access = access_id_reg,			\
+		.get_user = get_id_reg,				\
+		.set_user = set_id_reg,				\
+		.visibility = raz_visibility			\
+	},							\
 }
 
 /*
@@ -361,15 +371,17 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
  * For now, these are exposed just like unallocated ID regs: they appear
  * RAZ for the guest.
  */
-#define ID_HIDDEN(name) {			\
-	SYS_DESC(SYS_##name),			\
-	.access = access_id_reg,		\
-	.get_user = get_id_reg,			\
-	.set_user = set_id_reg,			\
-	.visibility = raz_visibility,		\
+#define ID_HIDDEN(name) {				\
+	.reg_desc = {					\
+		SYS_DESC(SYS_##name),			\
+		.access = access_id_reg,		\
+		.get_user = get_id_reg,			\
+		.set_user = set_id_reg,			\
+		.visibility = raz_visibility,		\
+	},						\
 }
 
-static const struct sys_reg_desc id_reg_descs[] = {
+static const struct id_reg_desc id_reg_descs[KVM_ARM_ID_REG_NUM] = {
 	/*
 	 * ID regs: all ID_SANITISED() entries here must have corresponding
 	 * entries in arm64_ftr_regs[].
@@ -379,9 +391,13 @@ static const struct sys_reg_desc id_reg_descs[] = {
 	/* CRm=1 */
 	AA32_ID_SANITISED(ID_PFR0_EL1),
 	AA32_ID_SANITISED(ID_PFR1_EL1),
-	{ SYS_DESC(SYS_ID_DFR0_EL1), .access = access_id_reg,
-	  .get_user = get_id_reg, .set_user = set_id_dfr0_el1,
-	  .visibility = aa32_id_visibility, },
+	{ .reg_desc = {
+		SYS_DESC(SYS_ID_DFR0_EL1),
+		.access = access_id_reg,
+		.get_user = get_id_reg,
+		.set_user = set_id_dfr0_el1,
+		.visibility = aa32_id_visibility, },
+	},
 	ID_HIDDEN(ID_AFR0_EL1),
 	AA32_ID_SANITISED(ID_MMFR0_EL1),
 	AA32_ID_SANITISED(ID_MMFR1_EL1),
@@ -410,8 +426,12 @@ static const struct sys_reg_desc id_reg_descs[] = {
 
 	/* AArch64 ID registers */
 	/* CRm=4 */
-	{ SYS_DESC(SYS_ID_AA64PFR0_EL1), .access = access_id_reg,
-	  .get_user = get_id_reg, .set_user = set_id_aa64pfr0_el1, },
+	{ .reg_desc = {
+		SYS_DESC(SYS_ID_AA64PFR0_EL1),
+		.access = access_id_reg,
+		.get_user = get_id_reg,
+		.set_user = set_id_aa64pfr0_el1, },
+	},
 	ID_SANITISED(ID_AA64PFR1_EL1),
 	ID_UNALLOCATED(4, 2),
 	ID_UNALLOCATED(4, 3),
@@ -421,8 +441,12 @@ static const struct sys_reg_desc id_reg_descs[] = {
 	ID_UNALLOCATED(4, 7),
 
 	/* CRm=5 */
-	{ SYS_DESC(SYS_ID_AA64DFR0_EL1), .access = access_id_reg,
-	  .get_user = get_id_reg, .set_user = set_id_aa64dfr0_el1, },
+	{ .reg_desc = {
+		SYS_DESC(SYS_ID_AA64DFR0_EL1),
+		.access = access_id_reg,
+		.get_user = get_id_reg,
+		.set_user = set_id_aa64dfr0_el1, },
+	},
 	ID_SANITISED(ID_AA64DFR1_EL1),
 	ID_UNALLOCATED(5, 2),
 	ID_UNALLOCATED(5, 3),
@@ -461,12 +485,12 @@ static const struct sys_reg_desc id_reg_descs[] = {
  */
 int emulate_id_reg(struct kvm_vcpu *vcpu, struct sys_reg_params *params)
 {
-	const struct sys_reg_desc *r;
+	u32 id;
 
-	r = find_reg(params, id_reg_descs, ARRAY_SIZE(id_reg_descs));
+	id = reg_to_encoding(params);
 
-	if (likely(r)) {
-		perform_access(vcpu, params, r);
+	if (likely(is_id_reg(id))) {
+		perform_access(vcpu, params, &id_reg_descs[IDREG_IDX(id)].reg_desc);
 	} else {
 		print_sys_reg_msg(params,
 				  "Unsupported guest id_reg access at: %lx [%08lx]\n",
@@ -483,38 +507,102 @@ void kvm_arm_reset_id_regs(struct kvm_vcpu *vcpu)
 	unsigned long i;
 
 	for (i = 0; i < ARRAY_SIZE(id_reg_descs); i++)
-		if (id_reg_descs[i].reset)
-			id_reg_descs[i].reset(vcpu, &id_reg_descs[i]);
+		if (id_reg_descs[i].reg_desc.reset)
+			id_reg_descs[i].reg_desc.reset(vcpu, &id_reg_descs[i].reg_desc);
 }
 
 int kvm_arm_get_id_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 {
-	return kvm_sys_reg_get_user(vcpu, reg,
-				    id_reg_descs, ARRAY_SIZE(id_reg_descs));
+	u64 __user *uaddr = (u64 __user *)(unsigned long)reg->addr;
+	const struct sys_reg_desc *r;
+	struct sys_reg_params params;
+	u64 val;
+	int ret;
+	u32 id;
+
+	if (!index_to_params(reg->id, &params))
+		return -ENOENT;
+	id = reg_to_encoding(&params);
+
+	if (!is_id_reg(id))
+		return -ENOENT;
+
+	r = &id_reg_descs[IDREG_IDX(id)].reg_desc;
+	if (r->get_user) {
+		ret = (r->get_user)(vcpu, r, &val);
+	} else {
+		ret = 0;
+		val = IDREG(vcpu->kvm, id);
+	}
+
+	if (!ret)
+		ret = put_user(val, uaddr);
+
+	return ret;
 }
 
 int kvm_arm_set_id_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 {
-	return kvm_sys_reg_set_user(vcpu, reg,
-				    id_reg_descs, ARRAY_SIZE(id_reg_descs));
+	u64 __user *uaddr = (u64 __user *)(unsigned long)reg->addr;
+	const struct sys_reg_desc *r;
+	struct sys_reg_params params;
+	u64 val;
+	int ret;
+	u32 id;
+
+	if (!index_to_params(reg->id, &params))
+		return -ENOENT;
+	id = reg_to_encoding(&params);
+
+	if (!is_id_reg(id))
+		return -ENOENT;
+
+	if (get_user(val, uaddr))
+		return -EFAULT;
+
+	r = &id_reg_descs[IDREG_IDX(id)].reg_desc;
+
+	if (sysreg_user_write_ignore(vcpu, r))
+		return 0;
+
+	if (r->set_user) {
+		ret = (r->set_user)(vcpu, r, val);
+	} else {
+		WARN_ONCE(1, "ID register set_user callback is NULL\n");
+		ret = 0;
+	}
+
+	return ret;
 }
 
 bool kvm_arm_check_idreg_table(void)
 {
-	return check_sysreg_table(id_reg_descs, ARRAY_SIZE(id_reg_descs), false);
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(id_reg_descs); i++) {
+		const struct sys_reg_desc *r = &id_reg_descs[i].reg_desc;
+
+		if (IDREG_IDX(reg_to_encoding(r)) != i) {
+			kvm_err("id_reg table %pS entry %d not set correctly\n",
+				&id_reg_descs[i].reg_desc, i);
+			return false;
+		}
+	}
+
+	return true;
 }
 
 int kvm_arm_walk_id_regs(struct kvm_vcpu *vcpu, u64 __user *uind)
 {
-	const struct sys_reg_desc *i2, *end2;
+	const struct id_reg_desc *i2, *end2;
 	unsigned int total = 0;
 	int err;
 
 	i2 = id_reg_descs;
 	end2 = id_reg_descs + ARRAY_SIZE(id_reg_descs);
 
-	while (i2 != end2) {
-		err = walk_one_sys_reg(vcpu, i2++, &uind, &total);
+	for (; i2 != end2; i2++) {
+		err = walk_one_sys_reg(vcpu, &(i2->reg_desc), &uind, &total);
 		if (err)
 			return err;
 	}
@@ -532,12 +620,12 @@ void kvm_arm_set_default_id_regs(struct kvm *kvm)
 	u64 val;
 
 	for (i = 0; i < ARRAY_SIZE(id_reg_descs); i++) {
-		id = reg_to_encoding(&id_reg_descs[i]);
+		id = reg_to_encoding(&id_reg_descs[i].reg_desc);
 		if (WARN_ON_ONCE(!is_id_reg(id)))
 			/* Shouldn't happen */
 			continue;
 
-		if (id_reg_descs[i].visibility == raz_visibility)
+		if (id_reg_descs[i].reg_desc.visibility == raz_visibility)
 			/* Hidden or reserved ID register */
 			continue;
 
arch/arm64/kvm/sys_regs.c +1 -1
index 568ebc0fb15c..7b63d9038639 100644
@@ -2519,7 +2519,7 @@ int kvm_handle_sys_reg(struct kvm_vcpu *vcpu)
  * Userspace API
  *****************************************************************************/
 
-static bool index_to_params(u64 id, struct sys_reg_params *params)
+bool index_to_params(u64 id, struct sys_reg_params *params)
 {
 	switch (id & KVM_REG_SIZE_MASK) {
 	case KVM_REG_SIZE_U64:
arch/arm64/kvm/sys_regs.h +1 -0
index 9231d89889c7..094a7f19d93f 100644
@@ -239,6 +239,7 @@ static inline bool is_id_reg(u32 id)
 
 void perform_access(struct kvm_vcpu *vcpu, struct sys_reg_params *params,
 		    const struct sys_reg_desc *r);
+bool index_to_params(u64 id, struct sys_reg_params *params);
 const struct sys_reg_desc *get_reg_by_id(u64 id,
 					 const struct sys_reg_desc table[],
 					 unsigned int num);
Signature · 1 line
2.39.2.722.g9855ee24e9-goog
#16 lore raw

Re: [PATCH v3 5/6] KVM: arm64: Introduce ID register specific descriptor

Hi Jing,

Quoted 258 lines · Jing Zhang

Introduce an ID feature register specific descriptor to include ID register specific fields and callbacks besides its corresponding general system register descriptor.
New fields for ID register descriptor would be added later when it is necessary to support a writable ID register.

No functional change intended.

---
 arch/arm64/kvm/id_regs.c  | 184 ++++++++++++++++++++++++++++----------
 arch/arm64/kvm/sys_regs.c |   2 +-
 arch/arm64/kvm/sys_regs.h |   1 +
 3 files changed, 138 insertions(+), 49 deletions(-)
arch/arm64/kvm/id_regs.c +118 -42
index 21ec8fc10d79..fc0dcd557cbb 100644
@@ -18,6 +18,10 @@
 
 #include "sys_regs.h"
 
+struct id_reg_desc {
+       const struct sys_reg_desc       reg_desc;
+};
+
 static u8 vcpu_pmuver(const struct kvm_vcpu *vcpu)
 {
        if (kvm_vcpu_has_pmu(vcpu))
@@ -326,21 +330,25 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
 }
 
 /* sys_reg_desc initialiser for known cpufeature ID registers */
-#define ID_SANITISED(name) {                   \
-       SYS_DESC(SYS_##name),                   \
-       .access = access_id_reg,                \
-       .get_user = get_id_reg,                 \
-       .set_user = set_id_reg,                 \
-       .visibility = id_visibility,            \
+#define ID_SANITISED(name) {                           \
+       .reg_desc = {                                   \
+               SYS_DESC(SYS_##name),                   \
+               .access = access_id_reg,                \
+               .get_user = get_id_reg,                 \
+               .set_user = set_id_reg,                 \
+               .visibility = id_visibility,            \
+       },                                              \
 }
 
 /* sys_reg_desc initialiser for known cpufeature ID registers */
-#define AA32_ID_SANITISED(name) {              \
-       SYS_DESC(SYS_##name),                   \
-       .access = access_id_reg,                \
-       .get_user = get_id_reg,                 \
-       .set_user = set_id_reg,                 \
-       .visibility = aa32_id_visibility,       \
+#define AA32_ID_SANITISED(name) {                      \
+       .reg_desc = {                                   \
+               SYS_DESC(SYS_##name),                   \
+               .access = access_id_reg,                \
+               .get_user = get_id_reg,                 \
+               .set_user = set_id_reg,                 \
+               .visibility = aa32_id_visibility,       \
+       },                                              \
 }
 
 /*
@@ -348,12 +356,14 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
  * register with encoding Op0=3, Op1=0, CRn=0, CRm=crm, Op2=op2
  * (1 <= crm < 8, 0 <= Op2 < 8).
  */
-#define ID_UNALLOCATED(crm, op2) {                     \
-       Op0(3), Op1(0), CRn(0), CRm(crm), Op2(op2),     \
-       .access = access_id_reg,                        \
-       .get_user = get_id_reg,                         \
-       .set_user = set_id_reg,                         \
-       .visibility = raz_visibility                    \
+#define ID_UNALLOCATED(crm, op2) {                             \
+       .reg_desc = {                                           \
+               Op0(3), Op1(0), CRn(0), CRm(crm), Op2(op2),     \
+               .access = access_id_reg,                        \
+               .get_user = get_id_reg,                         \
+               .set_user = set_id_reg,                         \
+               .visibility = raz_visibility                    \
+       },                                                      \
 }
 
 /*
@@ -361,15 +371,17 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
  * For now, these are exposed just like unallocated ID regs: they appear
  * RAZ for the guest.
  */
-#define ID_HIDDEN(name) {                      \
-       SYS_DESC(SYS_##name),                   \
-       .access = access_id_reg,                \
-       .get_user = get_id_reg,                 \
-       .set_user = set_id_reg,                 \
-       .visibility = raz_visibility,           \
+#define ID_HIDDEN(name) {                              \
+       .reg_desc = {                                   \
+               SYS_DESC(SYS_##name),                   \
+               .access = access_id_reg,                \
+               .get_user = get_id_reg,                 \
+               .set_user = set_id_reg,                 \
+               .visibility = raz_visibility,           \
+       },                                              \
 }
 
-static const struct sys_reg_desc id_reg_descs[] = {
+static const struct id_reg_desc id_reg_descs[KVM_ARM_ID_REG_NUM] = {
        /*
         * ID regs: all ID_SANITISED() entries here must have corresponding
         * entries in arm64_ftr_regs[].
@@ -379,9 +391,13 @@ static const struct sys_reg_desc id_reg_descs[] = {
        /* CRm=1 */
        AA32_ID_SANITISED(ID_PFR0_EL1),
        AA32_ID_SANITISED(ID_PFR1_EL1),
-       { SYS_DESC(SYS_ID_DFR0_EL1), .access = access_id_reg,
-         .get_user = get_id_reg, .set_user = set_id_dfr0_el1,
-         .visibility = aa32_id_visibility, },
+       { .reg_desc = {
+               SYS_DESC(SYS_ID_DFR0_EL1),
+               .access = access_id_reg,
+               .get_user = get_id_reg,
+               .set_user = set_id_dfr0_el1,
+               .visibility = aa32_id_visibility, },
+       },
        ID_HIDDEN(ID_AFR0_EL1),
        AA32_ID_SANITISED(ID_MMFR0_EL1),
        AA32_ID_SANITISED(ID_MMFR1_EL1),
@@ -410,8 +426,12 @@ static const struct sys_reg_desc id_reg_descs[] = {
 
        /* AArch64 ID registers */
        /* CRm=4 */
-       { SYS_DESC(SYS_ID_AA64PFR0_EL1), .access = access_id_reg,
-         .get_user = get_id_reg, .set_user = set_id_aa64pfr0_el1, },
+       { .reg_desc = {
+               SYS_DESC(SYS_ID_AA64PFR0_EL1),
+               .access = access_id_reg,
+               .get_user = get_id_reg,
+               .set_user = set_id_aa64pfr0_el1, },
+       },
        ID_SANITISED(ID_AA64PFR1_EL1),
        ID_UNALLOCATED(4, 2),
        ID_UNALLOCATED(4, 3),
@@ -421,8 +441,12 @@ static const struct sys_reg_desc id_reg_descs[] = {
        ID_UNALLOCATED(4, 7),
 
        /* CRm=5 */
-       { SYS_DESC(SYS_ID_AA64DFR0_EL1), .access = access_id_reg,
-         .get_user = get_id_reg, .set_user = set_id_aa64dfr0_el1, },
+       { .reg_desc = {
+               SYS_DESC(SYS_ID_AA64DFR0_EL1),
+               .access = access_id_reg,
+               .get_user = get_id_reg,
+               .set_user = set_id_aa64dfr0_el1, },
+       },
        ID_SANITISED(ID_AA64DFR1_EL1),
        ID_UNALLOCATED(5, 2),
        ID_UNALLOCATED(5, 3),
@@ -461,12 +485,12 @@ static const struct sys_reg_desc id_reg_descs[] = {
  */
 int emulate_id_reg(struct kvm_vcpu *vcpu, struct sys_reg_params *params)
 {
-       const struct sys_reg_desc *r;
+       u32 id;
 
-       r = find_reg(params, id_reg_descs, ARRAY_SIZE(id_reg_descs));
+       id = reg_to_encoding(params);
 
-       if (likely(r)) {
-               perform_access(vcpu, params, r);
+       if (likely(is_id_reg(id))) {
+               perform_access(vcpu, params, &id_reg_descs[IDREG_IDX(id)].reg_desc);
        } else {
                print_sys_reg_msg(params,
                                  "Unsupported guest id_reg access at: %lx [%08lx]\n",
@@ -483,38 +507,102 @@ void kvm_arm_reset_id_regs(struct kvm_vcpu *vcpu)
        unsigned long i;
 
        for (i = 0; i < ARRAY_SIZE(id_reg_descs); i++)
-               if (id_reg_descs[i].reset)
-                       id_reg_descs[i].reset(vcpu, &id_reg_descs[i]);
+               if (id_reg_descs[i].reg_desc.reset)
+                       id_reg_descs[i].reg_desc.reset(vcpu, &id_reg_descs[i].reg_desc);
 }
 
 int kvm_arm_get_id_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 {
-       return kvm_sys_reg_get_user(vcpu, reg,
-                                   id_reg_descs, ARRAY_SIZE(id_reg_descs));
+       u64 __user *uaddr = (u64 __user *)(unsigned long)reg->addr;
+       const struct sys_reg_desc *r;
+       struct sys_reg_params params;
+       u64 val;
+       int ret;
+       u32 id;
+
+       if (!index_to_params(reg->id, &params))
+               return -ENOENT;
+       id = reg_to_encoding(&params);
+
+       if (!is_id_reg(id))
+               return -ENOENT;
+
+       r = &id_reg_descs[IDREG_IDX(id)].reg_desc;
+       if (r->get_user) {
+               ret = (r->get_user)(vcpu, r, &val);
+       } else {
+               ret = 0;
+               val = IDREG(vcpu->kvm, id);
+       }
+
+       if (!ret)
+               ret = put_user(val, uaddr);
+
+       return ret;
 }
 
 int kvm_arm_set_id_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 {
-       return kvm_sys_reg_set_user(vcpu, reg,
-                                   id_reg_descs, ARRAY_SIZE(id_reg_descs));
+       u64 __user *uaddr = (u64 __user *)(unsigned long)reg->addr;
+       const struct sys_reg_desc *r;
+       struct sys_reg_params params;
+       u64 val;
+       int ret;
+       u32 id;
+
+       if (!index_to_params(reg->id, &params))
+               return -ENOENT;
+       id = reg_to_encoding(&params);
+
+       if (!is_id_reg(id))
+               return -ENOENT;
+
+       if (get_user(val, uaddr))
+               return -EFAULT;
+
+       r = &id_reg_descs[IDREG_IDX(id)].reg_desc;
+
+       if (sysreg_user_write_ignore(vcpu, r))
+               return 0;
+
+       if (r->set_user) {
+               ret = (r->set_user)(vcpu, r, val);
+       } else {
+               WARN_ONCE(1, "ID register set_user callback is NULL\n");
+               ret = 0;
+       }
+
+       return ret;
 }

arch/arm64/kvm/id_regs.c void kvm_arm_reset_id_regs(struct kvm_vcpu *vcpu) · line 585

Quoted 10 lines
arch/arm64/kvm/id_regs.c +6 -1
 
 bool kvm_arm_check_idreg_table(void)
 {
-       return check_sysreg_table(id_reg_descs, ARRAY_SIZE(id_reg_descs), false);
+       unsigned int i;
+
+       for (i = 0; i < ARRAY_SIZE(id_reg_descs); i++) {
+               const struct sys_reg_desc *r = &id_reg_descs[i].reg_desc;
+
+               if (IDREG_IDX(reg_to_encoding(r)) != i) {

As I mentioned for the previous version of the patch, can we also check if this is an ID register ?

Quoted 27 lines
arch/arm64/kvm/id_regs.c +10 -3
+                       kvm_err("id_reg table %pS entry %d not set correctly\n",
+                               &id_reg_descs[i].reg_desc, i);
+                       return false;
+               }
+       }
+
+       return true;
 }
 
 int kvm_arm_walk_id_regs(struct kvm_vcpu *vcpu, u64 __user *uind)
 {
-       const struct sys_reg_desc *i2, *end2;
+       const struct id_reg_desc *i2, *end2;
        unsigned int total = 0;
        int err;
 
        i2 = id_reg_descs;
        end2 = id_reg_descs + ARRAY_SIZE(id_reg_descs);
 
-       while (i2 != end2) {
-               err = walk_one_sys_reg(vcpu, i2++, &uind, &total);
+       for (; i2 != end2; i2++) {
+               err = walk_one_sys_reg(vcpu, &(i2->reg_desc), &uind, &total);
                if (err)
                        return err;
        }

arch/arm64/kvm/id_regs.c void kvm_arm_set_default_id_regs(struct kvm *kvm) · line 624

Quoted 6 lines
arch/arm64/kvm/id_regs.c +1 -1
@@ -532,12 +620,12 @@ void kvm_arm_set_default_id_regs(struct kvm *kvm)
        u64 val;
 
        for (i = 0; i < ARRAY_SIZE(id_reg_descs); i++) {
-               id = reg_to_encoding(&id_reg_descs[i]);
+               id = reg_to_encoding(&id_reg_descs[i].reg_desc);
                if (WARN_ON_ONCE(!is_id_reg(id)))

If kvm_arm_check_idreg_table() checks all entries in the table are an ID register, we can remove this checking from here.

Thank you,
Reiji

Quoted 36 lines
arch/arm64/kvm/id_regs.c +1 -1
                        /* Shouldn't happen */
                        continue;
 
-               if (id_reg_descs[i].visibility == raz_visibility)
+               if (id_reg_descs[i].reg_desc.visibility == raz_visibility)
                        /* Hidden or reserved ID register */
                        continue;
 
arch/arm64/kvm/sys_regs.c +1 -1
index 568ebc0fb15c..7b63d9038639 100644
@@ -2519,7 +2519,7 @@ int kvm_handle_sys_reg(struct kvm_vcpu *vcpu)
  * Userspace API
  *****************************************************************************/
 
-static bool index_to_params(u64 id, struct sys_reg_params *params)
+bool index_to_params(u64 id, struct sys_reg_params *params)
 {
        switch (id & KVM_REG_SIZE_MASK) {
        case KVM_REG_SIZE_U64:
arch/arm64/kvm/sys_regs.h +1 -1
index 9231d89889c7..094a7f19d93f 100644
@@ -239,6 +239,7 @@ static inline bool is_id_reg(u32 id)
 
 void perform_access(struct kvm_vcpu *vcpu, struct sys_reg_params *params,
                    const struct sys_reg_desc *r);
+bool index_to_params(u64 id, struct sys_reg_params *params);
 const struct sys_reg_desc *get_reg_by_id(u64 id,
                                         const struct sys_reg_desc table[],
                                         unsigned int num);
--

2.39.2.722.g9855ee24e9-goog

author
#17 lore raw

Re: [PATCH v3 5/6] KVM: arm64: Introduce ID register specific descriptor

Hi Reiji,

Quoted 263 lines · Reiji Watanabe

Hi Jing,

Quoted 259 lines · Jing Zhang

Introduce an ID feature register specific descriptor to include ID register specific fields and callbacks besides its corresponding general system register descriptor.
New fields for ID register descriptor would be added later when it is necessary to support a writable ID register.

No functional change intended.

---
 arch/arm64/kvm/id_regs.c  | 184 ++++++++++++++++++++++++++++----------
 arch/arm64/kvm/sys_regs.c |   2 +-
 arch/arm64/kvm/sys_regs.h |   1 +
 3 files changed, 138 insertions(+), 49 deletions(-)
arch/arm64/kvm/id_regs.c +118 -42
index 21ec8fc10d79..fc0dcd557cbb 100644
@@ -18,6 +18,10 @@
 
 #include "sys_regs.h"
 
+struct id_reg_desc {
+       const struct sys_reg_desc       reg_desc;
+};
+
 static u8 vcpu_pmuver(const struct kvm_vcpu *vcpu)
 {
        if (kvm_vcpu_has_pmu(vcpu))
@@ -326,21 +330,25 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
 }
 
 /* sys_reg_desc initialiser for known cpufeature ID registers */
-#define ID_SANITISED(name) {                   \
-       SYS_DESC(SYS_##name),                   \
-       .access = access_id_reg,                \
-       .get_user = get_id_reg,                 \
-       .set_user = set_id_reg,                 \
-       .visibility = id_visibility,            \
+#define ID_SANITISED(name) {                           \
+       .reg_desc = {                                   \
+               SYS_DESC(SYS_##name),                   \
+               .access = access_id_reg,                \
+               .get_user = get_id_reg,                 \
+               .set_user = set_id_reg,                 \
+               .visibility = id_visibility,            \
+       },                                              \
 }
 
 /* sys_reg_desc initialiser for known cpufeature ID registers */
-#define AA32_ID_SANITISED(name) {              \
-       SYS_DESC(SYS_##name),                   \
-       .access = access_id_reg,                \
-       .get_user = get_id_reg,                 \
-       .set_user = set_id_reg,                 \
-       .visibility = aa32_id_visibility,       \
+#define AA32_ID_SANITISED(name) {                      \
+       .reg_desc = {                                   \
+               SYS_DESC(SYS_##name),                   \
+               .access = access_id_reg,                \
+               .get_user = get_id_reg,                 \
+               .set_user = set_id_reg,                 \
+               .visibility = aa32_id_visibility,       \
+       },                                              \
 }
 
 /*
@@ -348,12 +356,14 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
  * register with encoding Op0=3, Op1=0, CRn=0, CRm=crm, Op2=op2
  * (1 <= crm < 8, 0 <= Op2 < 8).
  */
-#define ID_UNALLOCATED(crm, op2) {                     \
-       Op0(3), Op1(0), CRn(0), CRm(crm), Op2(op2),     \
-       .access = access_id_reg,                        \
-       .get_user = get_id_reg,                         \
-       .set_user = set_id_reg,                         \
-       .visibility = raz_visibility                    \
+#define ID_UNALLOCATED(crm, op2) {                             \
+       .reg_desc = {                                           \
+               Op0(3), Op1(0), CRn(0), CRm(crm), Op2(op2),     \
+               .access = access_id_reg,                        \
+               .get_user = get_id_reg,                         \
+               .set_user = set_id_reg,                         \
+               .visibility = raz_visibility                    \
+       },                                                      \
 }
 
 /*
@@ -361,15 +371,17 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
  * For now, these are exposed just like unallocated ID regs: they appear
  * RAZ for the guest.
  */
-#define ID_HIDDEN(name) {                      \
-       SYS_DESC(SYS_##name),                   \
-       .access = access_id_reg,                \
-       .get_user = get_id_reg,                 \
-       .set_user = set_id_reg,                 \
-       .visibility = raz_visibility,           \
+#define ID_HIDDEN(name) {                              \
+       .reg_desc = {                                   \
+               SYS_DESC(SYS_##name),                   \
+               .access = access_id_reg,                \
+               .get_user = get_id_reg,                 \
+               .set_user = set_id_reg,                 \
+               .visibility = raz_visibility,           \
+       },                                              \
 }
 
-static const struct sys_reg_desc id_reg_descs[] = {
+static const struct id_reg_desc id_reg_descs[KVM_ARM_ID_REG_NUM] = {
        /*
         * ID regs: all ID_SANITISED() entries here must have corresponding
         * entries in arm64_ftr_regs[].
@@ -379,9 +391,13 @@ static const struct sys_reg_desc id_reg_descs[] = {
        /* CRm=1 */
        AA32_ID_SANITISED(ID_PFR0_EL1),
        AA32_ID_SANITISED(ID_PFR1_EL1),
-       { SYS_DESC(SYS_ID_DFR0_EL1), .access = access_id_reg,
-         .get_user = get_id_reg, .set_user = set_id_dfr0_el1,
-         .visibility = aa32_id_visibility, },
+       { .reg_desc = {
+               SYS_DESC(SYS_ID_DFR0_EL1),
+               .access = access_id_reg,
+               .get_user = get_id_reg,
+               .set_user = set_id_dfr0_el1,
+               .visibility = aa32_id_visibility, },
+       },
        ID_HIDDEN(ID_AFR0_EL1),
        AA32_ID_SANITISED(ID_MMFR0_EL1),
        AA32_ID_SANITISED(ID_MMFR1_EL1),
@@ -410,8 +426,12 @@ static const struct sys_reg_desc id_reg_descs[] = {
 
        /* AArch64 ID registers */
        /* CRm=4 */
-       { SYS_DESC(SYS_ID_AA64PFR0_EL1), .access = access_id_reg,
-         .get_user = get_id_reg, .set_user = set_id_aa64pfr0_el1, },
+       { .reg_desc = {
+               SYS_DESC(SYS_ID_AA64PFR0_EL1),
+               .access = access_id_reg,
+               .get_user = get_id_reg,
+               .set_user = set_id_aa64pfr0_el1, },
+       },
        ID_SANITISED(ID_AA64PFR1_EL1),
        ID_UNALLOCATED(4, 2),
        ID_UNALLOCATED(4, 3),
@@ -421,8 +441,12 @@ static const struct sys_reg_desc id_reg_descs[] = {
        ID_UNALLOCATED(4, 7),
 
        /* CRm=5 */
-       { SYS_DESC(SYS_ID_AA64DFR0_EL1), .access = access_id_reg,
-         .get_user = get_id_reg, .set_user = set_id_aa64dfr0_el1, },
+       { .reg_desc = {
+               SYS_DESC(SYS_ID_AA64DFR0_EL1),
+               .access = access_id_reg,
+               .get_user = get_id_reg,
+               .set_user = set_id_aa64dfr0_el1, },
+       },
        ID_SANITISED(ID_AA64DFR1_EL1),
        ID_UNALLOCATED(5, 2),
        ID_UNALLOCATED(5, 3),
@@ -461,12 +485,12 @@ static const struct sys_reg_desc id_reg_descs[] = {
  */
 int emulate_id_reg(struct kvm_vcpu *vcpu, struct sys_reg_params *params)
 {
-       const struct sys_reg_desc *r;
+       u32 id;
 
-       r = find_reg(params, id_reg_descs, ARRAY_SIZE(id_reg_descs));
+       id = reg_to_encoding(params);
 
-       if (likely(r)) {
-               perform_access(vcpu, params, r);
+       if (likely(is_id_reg(id))) {
+               perform_access(vcpu, params, &id_reg_descs[IDREG_IDX(id)].reg_desc);
        } else {
                print_sys_reg_msg(params,
                                  "Unsupported guest id_reg access at: %lx [%08lx]\n",
@@ -483,38 +507,102 @@ void kvm_arm_reset_id_regs(struct kvm_vcpu *vcpu)
        unsigned long i;
 
        for (i = 0; i < ARRAY_SIZE(id_reg_descs); i++)
-               if (id_reg_descs[i].reset)
-                       id_reg_descs[i].reset(vcpu, &id_reg_descs[i]);
+               if (id_reg_descs[i].reg_desc.reset)
+                       id_reg_descs[i].reg_desc.reset(vcpu, &id_reg_descs[i].reg_desc);
 }
 
 int kvm_arm_get_id_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 {
-       return kvm_sys_reg_get_user(vcpu, reg,
-                                   id_reg_descs, ARRAY_SIZE(id_reg_descs));
+       u64 __user *uaddr = (u64 __user *)(unsigned long)reg->addr;
+       const struct sys_reg_desc *r;
+       struct sys_reg_params params;
+       u64 val;
+       int ret;
+       u32 id;
+
+       if (!index_to_params(reg->id, &params))
+               return -ENOENT;
+       id = reg_to_encoding(&params);
+
+       if (!is_id_reg(id))
+               return -ENOENT;
+
+       r = &id_reg_descs[IDREG_IDX(id)].reg_desc;
+       if (r->get_user) {
+               ret = (r->get_user)(vcpu, r, &val);
+       } else {
+               ret = 0;
+               val = IDREG(vcpu->kvm, id);
+       }
+
+       if (!ret)
+               ret = put_user(val, uaddr);
+
+       return ret;
 }
 
 int kvm_arm_set_id_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 {
-       return kvm_sys_reg_set_user(vcpu, reg,
-                                   id_reg_descs, ARRAY_SIZE(id_reg_descs));
+       u64 __user *uaddr = (u64 __user *)(unsigned long)reg->addr;
+       const struct sys_reg_desc *r;
+       struct sys_reg_params params;
+       u64 val;
+       int ret;
+       u32 id;
+
+       if (!index_to_params(reg->id, &params))
+               return -ENOENT;
+       id = reg_to_encoding(&params);
+
+       if (!is_id_reg(id))
+               return -ENOENT;
+
+       if (get_user(val, uaddr))
+               return -EFAULT;
+
+       r = &id_reg_descs[IDREG_IDX(id)].reg_desc;
+
+       if (sysreg_user_write_ignore(vcpu, r))
+               return 0;
+
+       if (r->set_user) {
+               ret = (r->set_user)(vcpu, r, val);
+       } else {
+               WARN_ONCE(1, "ID register set_user callback is NULL\n");
+               ret = 0;
+       }
+
+       return ret;
 }
 
Quoted 12 lines

arch/arm64/kvm/id_regs.c void kvm_arm_reset_id_regs(struct kvm_vcpu *vcpu) · line 585

Quoted 9 lines
arch/arm64/kvm/id_regs.c +6 -1
 bool kvm_arm_check_idreg_table(void)
 {
-       return check_sysreg_table(id_reg_descs, ARRAY_SIZE(id_reg_descs), false);
+       unsigned int i;
+
+       for (i = 0; i < ARRAY_SIZE(id_reg_descs); i++) {
+               const struct sys_reg_desc *r = &id_reg_descs[i].reg_desc;
+
+               if (IDREG_IDX(reg_to_encoding(r)) != i) {

As I mentioned for the previous version of the patch, can we also check if this is an ID register ?

Sure, will use is_id_reg() to do the checking.

Quoted 30 lines
Quoted 30 lines
arch/arm64/kvm/id_regs.c +10 -3
+                       kvm_err("id_reg table %pS entry %d not set correctly\n",
+                               &id_reg_descs[i].reg_desc, i);
+                       return false;
+               }
+       }
+
+       return true;
 }
 
 int kvm_arm_walk_id_regs(struct kvm_vcpu *vcpu, u64 __user *uind)
 {
-       const struct sys_reg_desc *i2, *end2;
+       const struct id_reg_desc *i2, *end2;
        unsigned int total = 0;
        int err;
 
        i2 = id_reg_descs;
        end2 = id_reg_descs + ARRAY_SIZE(id_reg_descs);
 
-       while (i2 != end2) {
-               err = walk_one_sys_reg(vcpu, i2++, &uind, &total);
+       for (; i2 != end2; i2++) {
+               err = walk_one_sys_reg(vcpu, &(i2->reg_desc), &uind, &total);
                if (err)
                        return err;
        }
@@ -532,12 +620,12 @@ void kvm_arm_set_default_id_regs(struct kvm *kvm)
        u64 val;
 
Quoted 7 lines

arch/arm64/kvm/id_regs.c void kvm_arm_set_default_id_regs(struct kvm *kvm) · line 624

Quoted 4 lines
arch/arm64/kvm/id_regs.c +1 -1
        for (i = 0; i < ARRAY_SIZE(id_reg_descs); i++) {
-               id = reg_to_encoding(&id_reg_descs[i]);
+               id = reg_to_encoding(&id_reg_descs[i].reg_desc);
                if (WARN_ON_ONCE(!is_id_reg(id)))

If kvm_arm_check_idreg_table() checks all entries in the table are an ID register, we can remove this checking from here.

Agreed.

Quoted 32 lines

Thank you,
Reiji

Quoted 27 lines
arch/arm64/kvm/id_regs.c +1 -1
                        /* Shouldn't happen */
                        continue;
 
-               if (id_reg_descs[i].visibility == raz_visibility)
+               if (id_reg_descs[i].reg_desc.visibility == raz_visibility)
                        /* Hidden or reserved ID register */
                        continue;
 
arch/arm64/kvm/sys_regs.c +1 -1
index 568ebc0fb15c..7b63d9038639 100644
@@ -2519,7 +2519,7 @@ int kvm_handle_sys_reg(struct kvm_vcpu *vcpu)
  * Userspace API
  *****************************************************************************/
 
-static bool index_to_params(u64 id, struct sys_reg_params *params)
+bool index_to_params(u64 id, struct sys_reg_params *params)
 {
        switch (id & KVM_REG_SIZE_MASK) {
        case KVM_REG_SIZE_U64:
arch/arm64/kvm/sys_regs.h +0 -0
index 9231d89889c7..094a7f19d93f 100644
@@ -239,6 +239,7 @@ static inline bool is_id_reg(u32 id)
 
Quoted 9 lines
Quoted 9 lines
arch/arm64/kvm/sys_regs.h +1 -1
 void perform_access(struct kvm_vcpu *vcpu, struct sys_reg_params *params,
                    const struct sys_reg_desc *r);
+bool index_to_params(u64 id, struct sys_reg_params *params);
 const struct sys_reg_desc *get_reg_by_id(u64 id,
                                         const struct sys_reg_desc table[],
                                         unsigned int num);
--

2.39.2.722.g9855ee24e9-goog

Thanks,
Jing

author
#18 lore raw

PATCH v3 6/6 KVM: arm64: Refactor writings for PMUVer/CSV2/CSV3

In reply to Jing Zhang

Save KVM sanitised ID register value in ID descriptor (kvm_sys_val). Add an init callback for every ID register to setup kvm_sys_val. All per VCPU sanitizations are still handled on the fly during ID register read and write from userspace.
An arm64_ftr_bits array is used to indicate writable feature fields.

Refactor writings for ID_AA64PFR0_EL1.[CSV2|CSV3], ID_AA64DFR0_EL1.PMUVer and ID_DFR0_ELF.PerfMon based on utilities introduced by ID register descriptor.

No functional change intended.

---
 arch/arm64/include/asm/cpufeature.h |  25 +++
 arch/arm64/include/asm/kvm_host.h   |   2 +-
 arch/arm64/kernel/cpufeature.c      |  26 +--
 arch/arm64/kvm/arm.c                |   2 +-
 arch/arm64/kvm/id_regs.c            | 325 ++++++++++++++++++++--------
 arch/arm64/kvm/sys_regs.c           |   3 +-
 arch/arm64/kvm/sys_regs.h           |   2 +-
 7 files changed, 261 insertions(+), 124 deletions(-)
arch/arm64/include/asm/cpufeature.h +25 -0
index fc2c739f48f1..493ec530eefc 100644
@@ -64,6 +64,30 @@ struct arm64_ftr_bits {
 	s64		safe_val; /* safe value for FTR_EXACT features */
 };
 
+#define __ARM64_FTR_BITS(SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
+	{						\
+		.sign = SIGNED,				\
+		.visible = VISIBLE,			\
+		.strict = STRICT,			\
+		.type = TYPE,				\
+		.shift = SHIFT,				\
+		.width = WIDTH,				\
+		.safe_val = SAFE_VAL,			\
+	}
+
+/* Define a feature with unsigned values */
+#define ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
+	__ARM64_FTR_BITS(FTR_UNSIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
+
+/* Define a feature with a signed value */
+#define S_ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
+	__ARM64_FTR_BITS(FTR_SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
+
+#define ARM64_FTR_END					\
+	{						\
+		.width = 0,				\
+	}
+
 /*
  * Describe the early feature override to the core override code:
  *
@@ -911,6 +935,7 @@ static inline unsigned int get_vmid_bits(u64 mmfr1)
 	return 8;
 }
 
+s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new, s64 cur);
 struct arm64_ftr_reg *get_arm64_ftr_reg(u32 sys_id);
 
 extern struct arm64_ftr_override id_aa64mmfr1_override;
arch/arm64/include/asm/kvm_host.h +1 -1
index effb61a9a855..4795f81b31b4 100644
@@ -1015,7 +1015,7 @@ int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu,
 long kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
 				struct kvm_arm_copy_mte_tags *copy_tags);
 
-void kvm_arm_set_default_id_regs(struct kvm *kvm);
+void kvm_arm_init_id_regs(struct kvm *kvm);
 
 /* Guest/host FPSIMD coordination helpers */
 int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu);
arch/arm64/kernel/cpufeature.c +1 -25
index 23bd2a926b74..e18848ee4b98 100644
@@ -139,30 +139,6 @@ void dump_cpu_features(void)
 	pr_emerg("0x%*pb\n", ARM64_NCAPS, &cpu_hwcaps);
 }
 
-#define __ARM64_FTR_BITS(SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
-	{						\
-		.sign = SIGNED,				\
-		.visible = VISIBLE,			\
-		.strict = STRICT,			\
-		.type = TYPE,				\
-		.shift = SHIFT,				\
-		.width = WIDTH,				\
-		.safe_val = SAFE_VAL,			\
-	}
-
-/* Define a feature with unsigned values */
-#define ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
-	__ARM64_FTR_BITS(FTR_UNSIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
-
-/* Define a feature with a signed value */
-#define S_ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
-	__ARM64_FTR_BITS(FTR_SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
-
-#define ARM64_FTR_END					\
-	{						\
-		.width = 0,				\
-	}
-
 static void cpu_enable_cnp(struct arm64_cpu_capabilities const *cap);
 
 static bool __system_matches_cap(unsigned int n);
@@ -790,7 +766,7 @@ static u64 arm64_ftr_set_value(const struct arm64_ftr_bits *ftrp, s64 reg,
 	return reg;
 }
 
-static s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new,
+s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new,
 				s64 cur)
 {
 	s64 ret = 0;
arch/arm64/kvm/arm.c +1 -1
index fb2de2cb98cb..e539d9ca9d01 100644
@@ -135,7 +135,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 	/* The maximum number of VCPUs is limited by the host's GIC model */
 	kvm->max_vcpus = kvm_arm_default_max_vcpus();
 
-	kvm_arm_set_default_id_regs(kvm);
+	kvm_arm_init_id_regs(kvm);
 	kvm_arm_init_hypercalls(kvm);
 
 	return 0;
arch/arm64/kvm/id_regs.c +231 -94

486 lines not on this page · raw message

arch/arm64/kvm/sys_regs.c +1 -2
index 7b63d9038639..e09730a6cba4 100644
@@ -2912,14 +2912,13 @@ int __init kvm_sys_reg_table_init(void)
 	unsigned int i;
 
 	/* Make sure tables are unique and in order. */
-	valid &= kvm_arm_check_idreg_table();
 	valid &= check_sysreg_table(sys_reg_descs, ARRAY_SIZE(sys_reg_descs), false);
 	valid &= check_sysreg_table(cp14_regs, ARRAY_SIZE(cp14_regs), true);
 	valid &= check_sysreg_table(cp14_64_regs, ARRAY_SIZE(cp14_64_regs), true);
 	valid &= check_sysreg_table(cp15_regs, ARRAY_SIZE(cp15_regs), true);
 	valid &= check_sysreg_table(cp15_64_regs, ARRAY_SIZE(cp15_64_regs), true);
 	valid &= check_sysreg_table(invariant_sys_regs, ARRAY_SIZE(invariant_sys_regs), false);
-
+	valid &= kvm_arm_idreg_table_init();
 	if (!valid)
 		return -EINVAL;
 
arch/arm64/kvm/sys_regs.h +1 -1
index 094a7f19d93f..379e42d70caf 100644
@@ -260,7 +260,7 @@ int emulate_id_reg(struct kvm_vcpu *vcpu, struct sys_reg_params *params);
 void kvm_arm_reset_id_regs(struct kvm_vcpu *vcpu);
 int kvm_arm_get_id_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
 int kvm_arm_set_id_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
-bool kvm_arm_check_idreg_table(void);
+bool kvm_arm_idreg_table_init(void);
 int kvm_arm_walk_id_regs(struct kvm_vcpu *vcpu, u64 __user *uind);
 u64 kvm_arm_read_id_reg_with_encoding(const struct kvm_vcpu *vcpu, u32 id);
 
Signature · 1 line
2.39.2.722.g9855ee24e9-goog