FAILED: patch "[PATCH] KVM: arm64: Check for SYSREGS_ON_CPU before accessing the CPU" failed to apply to 6.15-stable tree
Subsystems (3)
[email protected]
maintainerThe patch below does not apply to the 6.15-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.15.y
git checkout FETCH_HEAD
git cherry-pick -x c6e35dff58d348c1a9489e9b3b62b3721e62631d
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2025081248-omission-talisman-0619@gregkh' --subject-prefix 'PATCH 6.15.y' HEAD^..
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From c6e35dff58d348c1a9489e9b3b62b3721e62631d Mon Sep 17 00:00:00 2001
From: Marc Zyngier <[email protected]>
Date: Sun, 20 Jul 2025 11:22:29 +0100
Subject: [PATCH] KVM: arm64: Check for SYSREGS_ON_CPU before accessing the CPU
state
Mark Brown reports that since we commit to making exceptions visible without the vcpu being loaded, the external abort selftest fails.
Upon investigation, it turns out that the code that makes registers affected by an exception visible to the guest is completely broken on VHE, as we don't check whether the system registers are loaded on the CPU at this point. We managed to get away with this so far, but that's obviously as bad as it gets,
Add the required checksm and document the absolute need to check for the SYSREGS_ON_CPU flag before calling into any of the __vcpu_write_sys_reg_to_cpu()__vcpu_read_sys_reg_from_cpu() helpers.
arch/arm64/include/asm/kvm_host.h +4 -0
@@ -1169,6 +1169,8 @@ static inline bool __vcpu_read_sys_reg_from_cpu(int reg, u64 *val)* System registers listed in the switch are not saved on every * exit from the guest but are only saved on vcpu_put. * +* SYSREGS_ON_CPU *MUST* be checked before using this helper. +* * Note that MPIDR_EL1 for the guest is set by KVM via VMPIDR_EL2 but * should never be listed below, because the guest cannot modify its * own MPIDR_EL1 and MPIDR_EL1 is accessed for VCPU A from VCPU B's
@@ -1221,6 +1223,8 @@ static inline bool __vcpu_write_sys_reg_to_cpu(u64 val, int reg)* System registers listed in the switch are not restored on every * entry to the guest but are only restored on vcpu_load. * +* SYSREGS_ON_CPU *MUST* be checked before using this helper. +* * Note that MPIDR_EL1 for the guest is set by KVM via VMPIDR_EL2 but * should never be listed below, because the MPIDR should only be set * once, before running the VCPU, and never changed later.
arch/arm64/kvm/hyp/exception.c +4 -2
@@ -26,7 +26,8 @@ static inline u64 __vcpu_read_sys_reg(const struct kvm_vcpu *vcpu, int reg)if (unlikely (vcpu_has_nv (vcpu)))return vcpu_read_sys_reg (vcpu, reg); -else if (__vcpu_read_sys_reg_from_cpu (reg, &val)) +else if (vcpu_get_flag (vcpu,SYSREGS_ON_CPU ) && +__vcpu_read_sys_reg_from_cpu (reg, &val))return val;return __vcpu_sys_reg (vcpu, reg);
@@ -36,7 +37,8 @@ static inline void __vcpu_write_sys_reg(struct kvm_vcpu *vcpu, u64 val, int reg) {if (unlikely (vcpu_has_nv (vcpu)))vcpu_write_sys_reg (vcpu, val, reg); -else if (!__vcpu_write_sys_reg_to_cpu (val, reg)) +else if (!vcpu_get_flag (vcpu,SYSREGS_ON_CPU ) || + !__vcpu_write_sys_reg_to_cpu (val, reg))__vcpu_assign_sys_reg (vcpu, reg, val); }
Sasha Levin
maintainerPATCH 1/5 KVM: arm64: Repaint pmcr_n into nr_pmu_counters
From: Marc Zyngier <[email protected]>
[ Upstream commit f12b54d7c24388886277598236b3eeea5c68eec4 ]
The pmcr_n field obviously refers to PMCR_EL0.N, but is generally used as the number of counters seen by the guest. Rename it accordingly.
Stable-dep-of: c6e35dff58d3 ("KVM: arm64: Check for SYSREGS_ON_CPU before accessing the CPU state")
--- arch/arm64/include/asm/kvm_host.h | 4 ++-- arch/arm64/kvm/pmu-emul.c | 6 +++--- arch/arm64/kvm/sys_regs.c | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-)
arch/arm64/include/asm/kvm_host.h +2 -2
@@ -359,8 +359,8 @@ struct kvm_arch {cpumask_var_t supported_cpus; -/* PMCR_EL0.N value for the guest */ -u8 pmcr_n; +/* Maximum number of counters for the guest */ +u8 nr_pmu_counters;/* Iterator for idreg debugfs */ u8 idreg_debugfs_iter;
arch/arm64/kvm/pmu-emul.c +3 -3
@@ -280,7 +280,7 @@ static u64 kvm_pmu_hyp_counter_mask(struct kvm_vcpu *vcpu)return 0 ; hpmn =SYS_FIELD_GET (MDCR_EL2 ,HPMN ,__vcpu_sys_reg (vcpu,MDCR_EL2 )); - n = vcpu->kvm->arch.pmcr_n; + n = vcpu->kvm->arch.nr_pmu_counters;/* * Programming HPMN to a value greater than PMCR_EL0.N is
@@ -1032,7 +1032,7 @@ static void kvm_arm_set_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)lockdep_assert_held (&kvm->arch.config_lock); kvm->arch.arm_pmu = arm_pmu; - kvm->arch.pmcr_n =kvm_arm_pmu_get_max_counters (kvm); + kvm->arch.nr_pmu_counters =kvm_arm_pmu_get_max_counters (kvm); }/**
@@ -1261,7 +1261,7 @@ u64 kvm_vcpu_read_pmcr(struct kvm_vcpu *vcpu) {u64 pmcr =__vcpu_sys_reg (vcpu,PMCR_EL0 ); -return u64_replace_bits (pmcr, vcpu->kvm->arch.pmcr_n,ARMV8_PMU_PMCR_N ); +return u64_replace_bits (pmcr, vcpu->kvm->arch.nr_pmu_counters,ARMV8_PMU_PMCR_N ); }void kvm_pmu_nested_transition (struct kvm_vcpu *vcpu)
arch/arm64/kvm/sys_regs.c +2 -2
@@ -785,7 +785,7 @@ static unsigned int pmu_visibility(const struct kvm_vcpu *vcpu,static u64 reset_pmu_reg (struct kvm_vcpu *vcpu,const struct sys_reg_desc *r) {u64 mask =BIT (ARMV8_PMU_CYCLE_IDX ); -u8 n = vcpu->kvm->arch.pmcr_n; +u8 n = vcpu->kvm->arch.nr_pmu_counters;if (n) mask |=GENMASK (n -1 ,0 );
@@ -1217,7 +1217,7 @@ static int set_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,*/ if (!kvm_vm_has_ran_once (kvm) && new_n <=kvm_arm_pmu_get_max_counters (kvm)) - kvm->arch.pmcr_n = new_n; + kvm->arch.nr_pmu_counters = new_n;mutex_unlock (&kvm->arch.config_lock);
Signature · 1 line
2.39.5
Sasha Levin
maintainerPATCH 2/5 KVM: arm64: Fix MDCR_EL2.HPMN reset value
From: Marc Zyngier <[email protected]>
[ Upstream commit c8823e51b534d490ec27d372596eb35d2bb7193c ]
The MDCR_EL2 documentation indicates that the HPMN field has the following behaviour:
"On a Warm reset, this field resets to the expression NUM_PMU_COUNTERS."
However, it appears we reset it to zero, which is not very useful.
Add a reset helper for MDCR_EL2, and handle the case where userspace changes the target PMU, which may force us to change HPMN again.
Stable-dep-of: c6e35dff58d3 ("KVM: arm64: Check for SYSREGS_ON_CPU before accessing the CPU state")
--- arch/arm64/kvm/pmu-emul.c | 20 +++++++++++++++++++- arch/arm64/kvm/sys_regs.c | 8 +++++++- 2 files changed, 26 insertions(+), 2 deletions(-)
arch/arm64/kvm/pmu-emul.c +19 -1
@@ -1027,12 +1027,30 @@ u8 kvm_arm_pmu_get_max_counters(struct kvm *kvm)return bitmap_weight (arm_pmu->cntr_mask,ARMV8_PMU_MAX_GENERAL_COUNTERS ); } +static void kvm_arm_set_nr_counters (struct kvm *kvm,unsigned int nr) +{ + kvm->arch.nr_pmu_counters = nr; + +/* Reset MDCR_EL2.HPMN behind the vcpus' back... */ +if (test_bit (KVM_ARM_VCPU_HAS_EL2 , kvm->arch.vcpu_features)) { +struct kvm_vcpu *vcpu; +unsigned long i; + +kvm_for_each_vcpu (i, vcpu, kvm) { +u64 val =__vcpu_sys_reg (vcpu,MDCR_EL2 ); + val &= ~MDCR_EL2_HPMN ; + val |=FIELD_PREP (MDCR_EL2_HPMN , kvm->arch.nr_pmu_counters); +__vcpu_sys_reg (vcpu,MDCR_EL2 ) = val; + } + } +} +static void kvm_arm_set_pmu (struct kvm *kvm,struct arm_pmu *arm_pmu) {lockdep_assert_held (&kvm->arch.config_lock); kvm->arch.arm_pmu = arm_pmu; - kvm->arch.nr_pmu_counters =kvm_arm_pmu_get_max_counters (kvm); +kvm_arm_set_nr_counters (kvm,kvm_arm_pmu_get_max_counters (kvm)); }/**
arch/arm64/kvm/sys_regs.c +7 -1
@@ -2704,6 +2704,12 @@ static int set_imp_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, .set_user = set_imp_id_reg, \ .reset = reset_imp_id_reg, \ .val = mask, \ + } + +static u64 reset_mdcr (struct kvm_vcpu *vcpu,const struct sys_reg_desc *r) +{ +__vcpu_sys_reg (vcpu, r->reg) = vcpu->kvm->arch.nr_pmu_counters; +return vcpu->kvm->arch.nr_pmu_counters; }/*
@@ -3249,7 +3255,7 @@ static const struct sys_reg_desc sys_reg_descs[] = {EL2_REG (SCTLR_EL2 , access_rw, reset_val,SCTLR_EL2_RES1 ),EL2_REG (ACTLR_EL2 , access_rw, reset_val,0 ),EL2_REG_VNCR (HCR_EL2 , reset_hcr,0 ), -EL2_REG (MDCR_EL2 , access_mdcr, reset_val,0 ), +EL2_REG (MDCR_EL2 , access_mdcr, reset_mdcr,0 ),EL2_REG (CPTR_EL2 , access_rw, reset_val,CPTR_NVHE_EL2_RES1 ),EL2_REG_VNCR (HSTR_EL2 , reset_val,0 ), EL2_REG_VNCR(HFGRTR_EL2, reset_val, 0),
Signature · 1 line
2.39.5
Sasha Levin
maintainerPATCH 3/5 KVM: arm64: Handle out-of-bound write to MDCR_EL2.HPMN
In reply to Sasha LevinFrom: Marc Zyngier <[email protected]>
[ Upstream commit efff9dd2fee7a5969b5b2a04995e638c3ba15826 ]
We don't really pay attention to what gets written to MDCR_EL2.HPMN, and funky guests could play ugly games on us.
Restrict what gets written there, and limit the number of counters to what the PMU is allowed to have.
Stable-dep-of: c6e35dff58d3 ("KVM: arm64: Check for SYSREGS_ON_CPU before accessing the CPU state")
--- arch/arm64/kvm/sys_regs.c | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-)
arch/arm64/kvm/sys_regs.c +23 -6
@@ -2576,16 +2576,33 @@ static bool access_mdcr(struct kvm_vcpu *vcpu,struct sys_reg_params *p, const struct sys_reg_desc *r) { - u64 old = __vcpu_sys_reg(vcpu, MDCR_EL2); + u64 hpmn, val, old = __vcpu_sys_reg(vcpu, MDCR_EL2); -if (!access_rw (vcpu, p, r)) -return false; +if (!p->is_write) { + p->regval = old; +return true; + } + + val = p->regval; + hpmn =FIELD_GET (MDCR_EL2_HPMN , val); + +/* +* If HPMN is out of bounds, limit it to what we actually +* support. This matches the UNKNOWN definition of the field +* in that case, and keeps the emulation simple. Sort of. +*/ +if (hpmn > vcpu->kvm->arch.nr_pmu_counters) { + hpmn = vcpu->kvm->arch.nr_pmu_counters; +u64_replace_bits (val, hpmn,MDCR_EL2_HPMN ); + } + +__vcpu_sys_reg (vcpu,MDCR_EL2 ) = val;/* -* Request a reload of the PMU to enable/disable the counters affected -* by HPME. +* Request a reload of the PMU to enable/disable the counters +* affected by HPME. */ -if ((old ^__vcpu_sys_reg (vcpu,MDCR_EL2 )) &MDCR_EL2_HPME ) +if ((old ^ val) &MDCR_EL2_HPME )kvm_make_request (KVM_REQ_RELOAD_PMU , vcpu);return true;
Signature · 1 line
2.39.5
Sasha Levin
maintainerPATCH 4/5 KVM: arm64: Add assignment-specific sysreg accessor
In reply to Sasha LevinFrom: Marc Zyngier <[email protected]>
[ Upstream commit 6678791ee3da0b78c28fe7d77814097f53cbb8df ]
Assigning a value to a system register doesn't do what it is supposed to be doing if that register is one that has RESx bits.
The main problem is that we use __vcpu_sys_reg(), which can be used both as a lvalue and rvalue. When used as a lvalue, the bit masking occurs *before* the new value is assigned, meaning that we (1) do pointless work on the old cvalue, and (2) potentially assign an invalid value as we fail to apply the masks to it.
Fix this by providing a new __vcpu_assign_sys_reg() that does what it says on the tin, and sanitises the *new* value instead of the old one. This comes with a significant amount of churn.
Stable-dep-of: c6e35dff58d3 ("KVM: arm64: Check for SYSREGS_ON_CPU before accessing the CPU state")
--- arch/arm64/include/asm/kvm_host.h | 11 ++++++ arch/arm64/kvm/arch_timer.c | 16 ++++---- arch/arm64/kvm/hyp/exception.c | 4 +- arch/arm64/kvm/hyp/include/hyp/switch.h | 4 +- arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h | 6 +-- arch/arm64/kvm/hyp/nvhe/hyp-main.c | 4 +- arch/arm64/kvm/hyp/vhe/switch.c | 4 +- arch/arm64/kvm/hyp/vhe/sysreg-sr.c | 44 +++++++++++----------- arch/arm64/kvm/pmu-emul.c | 14 +++---- arch/arm64/kvm/sys_regs.c | 38 ++++++++++--------- arch/arm64/kvm/sys_regs.h | 4 +- arch/arm64/kvm/vgic/vgic-v3-nested.c | 10 ++--- 12 files changed, 86 insertions(+), 73 deletions(-)
arch/arm64/include/asm/kvm_host.h +11 -0
@@ -1050,6 +1050,17 @@ static inline u64 *___ctxt_sys_reg(const struct kvm_cpu_context *ctxt, int r)#define ctxt_sys_reg (c,r) (*__ctxt_sys_reg(c,r))u64 kvm_vcpu_apply_reg_masks (const struct kvm_vcpu *,enum vcpu_sysreg ,u64 ); + +#define __vcpu_assign_sys_reg (v, r, val) \ + do { \ + const struct kvm_cpu_context *ctxt = &(v)->arch.ctxt; \ + u64 __v = (val); \ + if (vcpu_has_nv((v)) && (r) >= __SANITISED_REG_START__) \ + __v = kvm_vcpu_apply_reg_masks((v), (r), __v); \ + \ + ctxt_sys_reg(ctxt, (r)) = __v; \ + } while (0) +#define __vcpu_sys_reg (v,r) \ (*({ \ const struct kvm_cpu_context *ctxt = &(v)->arch.ctxt; \
arch/arm64/kvm/arch_timer.c +8 -8
@@ -108,16 +108,16 @@ static void timer_set_ctl(struct arch_timer_context *ctxt, u32 ctl)switch (arch_timer_ctx_index (ctxt)) {case TIMER_VTIMER : -__vcpu_sys_reg (vcpu,CNTV_CTL_EL0 ) = ctl; +__vcpu_assign_sys_reg (vcpu,CNTV_CTL_EL0 , ctl);break ;case TIMER_PTIMER : -__vcpu_sys_reg (vcpu,CNTP_CTL_EL0 ) = ctl; +__vcpu_assign_sys_reg (vcpu,CNTP_CTL_EL0 , ctl);break ;case TIMER_HVTIMER : -__vcpu_sys_reg (vcpu,CNTHV_CTL_EL2 ) = ctl; +__vcpu_assign_sys_reg (vcpu,CNTHV_CTL_EL2 , ctl);break ;case TIMER_HPTIMER : -__vcpu_sys_reg (vcpu,CNTHP_CTL_EL2 ) = ctl; +__vcpu_assign_sys_reg (vcpu,CNTHP_CTL_EL2 , ctl);break ;default :WARN_ON (1 );
@@ -130,16 +130,16 @@ static void timer_set_cval(struct arch_timer_context *ctxt, u64 cval)switch (arch_timer_ctx_index (ctxt)) {case TIMER_VTIMER : -__vcpu_sys_reg (vcpu,CNTV_CVAL_EL0 ) = cval; +__vcpu_assign_sys_reg (vcpu,CNTV_CVAL_EL0 , cval);break ;case TIMER_PTIMER : -__vcpu_sys_reg (vcpu,CNTP_CVAL_EL0 ) = cval; +__vcpu_assign_sys_reg (vcpu,CNTP_CVAL_EL0 , cval);break ;case TIMER_HVTIMER : -__vcpu_sys_reg (vcpu,CNTHV_CVAL_EL2 ) = cval; +__vcpu_assign_sys_reg (vcpu,CNTHV_CVAL_EL2 , cval);break ;case TIMER_HPTIMER : -__vcpu_sys_reg (vcpu,CNTHP_CVAL_EL2 ) = cval; +__vcpu_assign_sys_reg (vcpu,CNTHP_CVAL_EL2 , cval);break ;default :WARN_ON (1 );
arch/arm64/kvm/hyp/exception.c +2 -2
@@ -37,7 +37,7 @@ static inline void __vcpu_write_sys_reg(struct kvm_vcpu *vcpu, u64 val, int reg)if (unlikely (vcpu_has_nv (vcpu)))vcpu_write_sys_reg (vcpu, val, reg);else if (!__vcpu_write_sys_reg_to_cpu (val, reg)) -__vcpu_sys_reg (vcpu, reg) = val; +__vcpu_assign_sys_reg (vcpu, reg, val); } static void __vcpu_write_spsr(struct kvm_vcpu *vcpu, unsigned long target_mode,
@@ -51,7 +51,7 @@ static void __vcpu_write_spsr(struct kvm_vcpu *vcpu, unsigned long target_mode, }else if (has_vhe ()) {write_sysreg_el1 (val,SYS_SPSR ); }else { -__vcpu_sys_reg (vcpu,SPSR_EL1 ) = val; +__vcpu_assign_sys_reg (vcpu,SPSR_EL1 , val); } }
arch/arm64/kvm/hyp/include/hyp/switch.h +2 -2
@@ -45,7 +45,7 @@ static inline void __fpsimd_save_fpexc32(struct kvm_vcpu *vcpu)if (!vcpu_el1_is_32bit (vcpu))return ; -__vcpu_sys_reg (vcpu,FPEXC32_EL2 ) =read_sysreg (fpexc32_el2); +__vcpu_assign_sys_reg (vcpu,FPEXC32_EL2 ,read_sysreg (fpexc32_el2)); }static inline void __activate_traps_fpsimd32 (struct kvm_vcpu *vcpu)
@@ -412,7 +412,7 @@ static inline void fpsimd_lazy_switch_to_host(struct kvm_vcpu *vcpu)*/ if (vcpu_has_sve (vcpu)) { zcr_el1 =read_sysreg_el1 (SYS_ZCR ); -__vcpu_sys_reg (vcpu,vcpu_sve_zcr_elx (vcpu)) = zcr_el1; +__vcpu_assign_sys_reg (vcpu,vcpu_sve_zcr_elx (vcpu), zcr_el1);/* * The guest's state is always saved using the guest's max VL.
arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h +3 -3
@@ -307,11 +307,11 @@ static inline void __sysreg32_save_state(struct kvm_vcpu *vcpu) vcpu->arch.ctxt.spsr_irq =read_sysreg (spsr_irq); vcpu->arch.ctxt.spsr_fiq =read_sysreg (spsr_fiq); -__vcpu_sys_reg (vcpu,DACR32_EL2 ) =read_sysreg (dacr32_el2); -__vcpu_sys_reg (vcpu,IFSR32_EL2 ) =read_sysreg (ifsr32_el2); +__vcpu_assign_sys_reg (vcpu,DACR32_EL2 ,read_sysreg (dacr32_el2)); +__vcpu_assign_sys_reg (vcpu,IFSR32_EL2 ,read_sysreg (ifsr32_el2));if (has_vhe () ||kvm_debug_regs_in_use (vcpu)) -__vcpu_sys_reg (vcpu,DBGVCR32_EL2 ) =read_sysreg (dbgvcr32_el2); +__vcpu_assign_sys_reg (vcpu,DBGVCR32_EL2 ,read_sysreg (dbgvcr32_el2)); }static inline void __sysreg32_restore_state (struct kvm_vcpu *vcpu)
arch/arm64/kvm/hyp/nvhe/hyp-main.c +2 -2
@@ -26,7 +26,7 @@ void __kvm_hyp_host_forward_smc(struct kvm_cpu_context *host_ctxt);static void __hyp_sve_save_guest (struct kvm_vcpu *vcpu) { -__vcpu_sys_reg (vcpu,ZCR_EL1 ) =read_sysreg_el1 (SYS_ZCR ); +__vcpu_assign_sys_reg (vcpu,ZCR_EL1 ,read_sysreg_el1 (SYS_ZCR ));/* * On saving/restoring guest sve state, always use the maximum VL for * the guest. The layout of the data when saving the sve state depends
@@ -79,7 +79,7 @@ static void fpsimd_sve_sync(struct kvm_vcpu *vcpu) has_fpmr =kvm_has_fpmr (kern_hyp_va (vcpu->kvm));if (has_fpmr) -__vcpu_sys_reg (vcpu,FPMR ) =read_sysreg_s (SYS_FPMR ); +__vcpu_assign_sys_reg (vcpu,FPMR ,read_sysreg_s (SYS_FPMR ));if (system_supports_sve ())__hyp_sve_restore_host ();
arch/arm64/kvm/hyp/vhe/switch.c +2 -2
@@ -198,9 +198,9 @@ static void __deactivate_traps(struct kvm_vcpu *vcpu)*/ val =read_sysreg_el0 (SYS_CNTP_CVAL );if (map.direct_ptimer ==vcpu_ptimer (vcpu)) -__vcpu_sys_reg (vcpu,CNTP_CVAL_EL0 ) = val; +__vcpu_assign_sys_reg (vcpu,CNTP_CVAL_EL0 , val);if (map.direct_ptimer ==vcpu_hptimer (vcpu)) -__vcpu_sys_reg (vcpu,CNTHP_CVAL_EL2 ) = val; +__vcpu_assign_sys_reg (vcpu,CNTHP_CVAL_EL2 , val); offset =read_sysreg_s (SYS_CNTPOFF_EL2 );
arch/arm64/kvm/hyp/vhe/sysreg-sr.c +22 -22
69 lines not on this page · raw message
arch/arm64/kvm/pmu-emul.c +7 -7
@@ -178,7 +178,7 @@ static void kvm_pmu_set_pmc_value(struct kvm_pmc *pmc, u64 val, bool force) val |=lower_32_bits (val); } -__vcpu_sys_reg (vcpu, reg) = val; +__vcpu_assign_sys_reg (vcpu, reg, val);/* Recreate the perf event to reflect the updated sample_period */ kvm_pmu_create_perf_event (pmc);
@@ -204,7 +204,7 @@ void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu, u64 select_idx, u64 val)void kvm_pmu_set_counter_value_user (struct kvm_vcpu *vcpu,u64 select_idx,u64 val) {kvm_pmu_release_perf_event (kvm_vcpu_idx_to_pmc (vcpu, select_idx)); -__vcpu_sys_reg (vcpu,counter_index_to_reg (select_idx)) = val; +__vcpu_assign_sys_reg (vcpu,counter_index_to_reg (select_idx), val);kvm_make_request (KVM_REQ_RELOAD_PMU , vcpu); }
@@ -239,7 +239,7 @@ static void kvm_pmu_stop_counter(struct kvm_pmc *pmc) reg =counter_index_to_reg (pmc->idx); -__vcpu_sys_reg (vcpu, reg) = val; +__vcpu_assign_sys_reg (vcpu, reg, val);kvm_pmu_release_perf_event (pmc); }
@@ -503,7 +503,7 @@ static void kvm_pmu_counter_increment(struct kvm_vcpu *vcpu, reg =__vcpu_sys_reg (vcpu,counter_index_to_reg (i)) +1 ;if (!kvm_pmc_is_64bit (pmc)) reg =lower_32_bits (reg); -__vcpu_sys_reg (vcpu,counter_index_to_reg (i)) = reg; +__vcpu_assign_sys_reg (vcpu,counter_index_to_reg (i), reg);/* No overflow? move on */ if (kvm_pmc_has_64bit_overflow (pmc) ? reg :lower_32_bits (reg))
@@ -602,7 +602,7 @@ void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val)kvm_make_request (KVM_REQ_RELOAD_PMU , vcpu);/* The reset bits don't indicate any state, and shouldn't be saved. */ -__vcpu_sys_reg (vcpu,PMCR_EL0 ) = val & ~(ARMV8_PMU_PMCR_C |ARMV8_PMU_PMCR_P ); +__vcpu_assign_sys_reg (vcpu,PMCR_EL0 , (val & ~(ARMV8_PMU_PMCR_C |ARMV8_PMU_PMCR_P )));if (val &ARMV8_PMU_PMCR_C )kvm_pmu_set_counter_value (vcpu,ARMV8_PMU_CYCLE_IDX ,0 );
@@ -781,7 +781,7 @@ void kvm_pmu_set_counter_event_type(struct kvm_vcpu *vcpu, u64 data,u64 reg; reg =counter_index_to_evtreg (pmc->idx); -__vcpu_sys_reg (vcpu, reg) = data &kvm_pmu_evtyper_mask (vcpu->kvm); +__vcpu_assign_sys_reg (vcpu, reg, (data &kvm_pmu_evtyper_mask (vcpu->kvm)));kvm_pmu_create_perf_event (pmc); }
@@ -1040,7 +1040,7 @@ static void kvm_arm_set_nr_counters(struct kvm *kvm, unsigned int nr)u64 val =__vcpu_sys_reg (vcpu,MDCR_EL2 ); val &= ~MDCR_EL2_HPMN ; val |=FIELD_PREP (MDCR_EL2_HPMN , kvm->arch.nr_pmu_counters); -__vcpu_sys_reg (vcpu,MDCR_EL2 ) = val; +__vcpu_assign_sys_reg (vcpu,MDCR_EL2 , val); } } }
arch/arm64/kvm/sys_regs.c +20 -18
@@ -228,7 +228,7 @@ void vcpu_write_sys_reg(struct kvm_vcpu *vcpu, u64 val, int reg)* to reverse-translate virtual EL2 system registers for a * non-VHE guest hypervisor. */ -__vcpu_sys_reg (vcpu, reg) = val; +__vcpu_assign_sys_reg (vcpu, reg, val);switch (reg) {case CNTHCTL_EL2 :
@@ -263,7 +263,7 @@ void vcpu_write_sys_reg(struct kvm_vcpu *vcpu, u64 val, int reg)return ; memory_write: -__vcpu_sys_reg (vcpu, reg) = val; +__vcpu_assign_sys_reg (vcpu, reg, val); }/* CSSELR values; used to index KVM_REG_ARM_DEMUX_ID_CCSIDR */
@@ -605,7 +605,7 @@ static int set_oslsr_el1(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,if ((val ^ rd->val) & ~OSLSR_EL1_OSLK )return -EINVAL ; -__vcpu_sys_reg (vcpu, rd->reg) = val; +__vcpu_assign_sys_reg (vcpu, rd->reg, val);return 0 ; }
@@ -835,7 +835,7 @@ static u64 reset_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)* The value of PMCR.N field is included when the * vCPU register is read via kvm_vcpu_read_pmcr(). */ -__vcpu_sys_reg (vcpu, r->reg) = pmcr; +__vcpu_assign_sys_reg (vcpu, r->reg, pmcr);return __vcpu_sys_reg (vcpu, r->reg); }
@@ -907,7 +907,7 @@ static bool access_pmselr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,return false;if (p->is_write) -__vcpu_sys_reg (vcpu,PMSELR_EL0 ) = p->regval; +__vcpu_assign_sys_reg (vcpu,PMSELR_EL0 , p->regval);else /* return PMSELR.SEL field */ p->regval =__vcpu_sys_reg (vcpu,PMSELR_EL0 )
@@ -1076,7 +1076,7 @@ static int set_pmreg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, u64 va {u64 mask =kvm_pmu_accessible_counter_mask (vcpu); -__vcpu_sys_reg (vcpu, r->reg) = val & mask; +__vcpu_assign_sys_reg (vcpu, r->reg, val & mask);kvm_make_request (KVM_REQ_RELOAD_PMU , vcpu);return 0 ;
@@ -1185,8 +1185,8 @@ static bool access_pmuserenr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,if (!vcpu_mode_priv (vcpu))return undef_access (vcpu, p, r); -__vcpu_sys_reg (vcpu,PMUSERENR_EL0 ) = - p->regval &ARMV8_PMU_USERENR_MASK ; +__vcpu_assign_sys_reg (vcpu,PMUSERENR_EL0 , + (p->regval &ARMV8_PMU_USERENR_MASK )); }else { p->regval =__vcpu_sys_reg (vcpu,PMUSERENR_EL0 ) &ARMV8_PMU_USERENR_MASK ;
@@ -1236,7 +1236,7 @@ static int set_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,if (!kvm_supports_32bit_el0 ()) val |=ARMV8_PMU_PMCR_LC ; -__vcpu_sys_reg (vcpu, r->reg) = val; +__vcpu_assign_sys_reg (vcpu, r->reg, val);kvm_make_request (KVM_REQ_RELOAD_PMU , vcpu);return 0 ;
@@ -2188,7 +2188,7 @@ static u64 reset_clidr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)if (kvm_has_mte (vcpu->kvm)) clidr |=2ULL <<CLIDR_TTYPE_SHIFT (loc); -__vcpu_sys_reg (vcpu, r->reg) = clidr; +__vcpu_assign_sys_reg (vcpu, r->reg, clidr);return __vcpu_sys_reg (vcpu, r->reg); }
@@ -2202,7 +2202,7 @@ static int set_clidr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,if ((val &CLIDR_EL1_RES0 ) || (!(ctr_el0 &CTR_EL0_IDC ) && idc))return -EINVAL ; -__vcpu_sys_reg (vcpu, rd->reg) = val; +__vcpu_assign_sys_reg (vcpu, rd->reg, val);return 0 ; }
@@ -2385,7 +2385,7 @@ static bool access_sp_el1(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r) {if (p->is_write) -__vcpu_sys_reg (vcpu,SP_EL1 ) = p->regval; +__vcpu_assign_sys_reg (vcpu,SP_EL1 , p->regval);else p->regval =__vcpu_sys_reg (vcpu,SP_EL1 );
@@ -2409,7 +2409,7 @@ static bool access_spsr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r) {if (p->is_write) -__vcpu_sys_reg (vcpu,SPSR_EL1 ) = p->regval; +__vcpu_assign_sys_reg (vcpu,SPSR_EL1 , p->regval);else p->regval =__vcpu_sys_reg (vcpu,SPSR_EL1 );
@@ -2421,7 +2421,7 @@ static bool access_cntkctl_el12(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r) {if (p->is_write) -__vcpu_sys_reg (vcpu,CNTKCTL_EL1 ) = p->regval; +__vcpu_assign_sys_reg (vcpu,CNTKCTL_EL1 , p->regval);else p->regval =__vcpu_sys_reg (vcpu,CNTKCTL_EL1 );
@@ -2435,7 +2435,9 @@ static u64 reset_hcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)if (!cpus_have_final_cap (ARM64_HAS_HCR_NV1 )) val |=HCR_E2H ; -return __vcpu_sys_reg (vcpu, r->reg) = val; +__vcpu_assign_sys_reg (vcpu, r->reg, val); + +return __vcpu_sys_reg (vcpu, r->reg); } static unsigned int __el2_visibility(const struct kvm_vcpu *vcpu,
@@ -2596,7 +2598,7 @@ static bool access_mdcr(struct kvm_vcpu *vcpu,u64_replace_bits (val, hpmn,MDCR_EL2_HPMN ); } -__vcpu_sys_reg (vcpu,MDCR_EL2 ) = val; +__vcpu_assign_sys_reg (vcpu,MDCR_EL2 , val);/* * Request a reload of the PMU to enable/disable the counters
@@ -2725,7 +2727,7 @@ static int set_imp_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,static u64 reset_mdcr (struct kvm_vcpu *vcpu,const struct sys_reg_desc *r) { -__vcpu_sys_reg (vcpu, r->reg) = vcpu->kvm->arch.nr_pmu_counters; +__vcpu_assign_sys_reg (vcpu, r->reg, vcpu->kvm->arch.nr_pmu_counters);return vcpu->kvm->arch.nr_pmu_counters; }
@@ -4966,7 +4968,7 @@ int kvm_sys_reg_set_user(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg,if (r->set_user) { ret = (r->set_user)(vcpu, r, val); }else { -__vcpu_sys_reg (vcpu, r->reg) = val; +__vcpu_assign_sys_reg (vcpu, r->reg, val); ret =0 ; }
arch/arm64/kvm/sys_regs.h +2 -2
@@ -137,7 +137,7 @@ static inline u64 reset_unknown(struct kvm_vcpu *vcpu, {BUG_ON (!r->reg);BUG_ON (r->reg >=NR_SYS_REGS ); -__vcpu_sys_reg (vcpu, r->reg) =0x1de7ec7edbadc0deULL ; +__vcpu_assign_sys_reg (vcpu, r->reg,0x1de7ec7edbadc0deULL );return __vcpu_sys_reg (vcpu, r->reg); }
@@ -145,7 +145,7 @@ static inline u64 reset_val(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r) {BUG_ON (!r->reg);BUG_ON (r->reg >=NR_SYS_REGS ); -__vcpu_sys_reg (vcpu, r->reg) = r->val; +__vcpu_assign_sys_reg (vcpu, r->reg, r->val);return __vcpu_sys_reg (vcpu, r->reg); }
arch/arm64/kvm/vgic/vgic-v3-nested.c +5 -5
@@ -359,12 +359,12 @@ void vgic_v3_put_nested(struct kvm_vcpu *vcpu) val =__vcpu_sys_reg (vcpu,ICH_HCR_EL2 ); val &= ~ICH_HCR_EL2_EOIcount_MASK; val |= (s_cpu_if->vgic_hcr & ICH_HCR_EL2_EOIcount_MASK); -__vcpu_sys_reg (vcpu,ICH_HCR_EL2 ) = val; -__vcpu_sys_reg (vcpu,ICH_VMCR_EL2 ) = s_cpu_if->vgic_vmcr; +__vcpu_assign_sys_reg (vcpu,ICH_HCR_EL2 , val); +__vcpu_assign_sys_reg (vcpu,ICH_VMCR_EL2 , s_cpu_if->vgic_vmcr);for (i =0 ; i <4 ; i++) { -__vcpu_sys_reg (vcpu,ICH_AP0RN (i)) = s_cpu_if->vgic_ap0r[i]; -__vcpu_sys_reg (vcpu,ICH_AP1RN (i)) = s_cpu_if->vgic_ap1r[i]; +__vcpu_assign_sys_reg (vcpu,ICH_AP0RN (i), s_cpu_if->vgic_ap0r[i]); +__vcpu_assign_sys_reg (vcpu,ICH_AP1RN (i), s_cpu_if->vgic_ap1r[i]); }for_each_set_bit (i, &shadow_if->lr_map, kvm_vgic_global_state.nr_lr) {
@@ -373,7 +373,7 @@ void vgic_v3_put_nested(struct kvm_vcpu *vcpu) val &= ~ICH_LR_STATE ; val |= s_cpu_if->vgic_lr[i] &ICH_LR_STATE ; -__vcpu_sys_reg (vcpu,ICH_LRN (i)) = val; +__vcpu_assign_sys_reg (vcpu,ICH_LRN (i), val); s_cpu_if->vgic_lr[i] =0 ; }
Signature · 1 line
2.39.5
Sasha Levin
maintainerPATCH 5/5 KVM: arm64: Check for SYSREGS_ON_CPU before accessing the CPU state
In reply to Sasha LevinFrom: Marc Zyngier <[email protected]>
[ Upstream commit c6e35dff58d348c1a9489e9b3b62b3721e62631d ]
Mark Brown reports that since we commit to making exceptions visible without the vcpu being loaded, the external abort selftest fails.
Upon investigation, it turns out that the code that makes registers affected by an exception visible to the guest is completely broken on VHE, as we don't check whether the system registers are loaded on the CPU at this point. We managed to get away with this so far, but that's obviously as bad as it gets,
Add the required checksm and document the absolute need to check for the SYSREGS_ON_CPU flag before calling into any of the __vcpu_write_sys_reg_to_cpu()__vcpu_read_sys_reg_from_cpu() helpers.
--- arch/arm64/include/asm/kvm_host.h | 4 ++++ arch/arm64/kvm/hyp/exception.c | 6 ++++-- 2 files changed, 8 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_host.h +4 -0
@@ -1081,6 +1081,8 @@ static inline bool __vcpu_read_sys_reg_from_cpu(int reg, u64 *val)* System registers listed in the switch are not saved on every * exit from the guest but are only saved on vcpu_put. * +* SYSREGS_ON_CPU *MUST* be checked before using this helper. +* * Note that MPIDR_EL1 for the guest is set by KVM via VMPIDR_EL2 but * should never be listed below, because the guest cannot modify its * own MPIDR_EL1 and MPIDR_EL1 is accessed for VCPU A from VCPU B's
@@ -1132,6 +1134,8 @@ static inline bool __vcpu_write_sys_reg_to_cpu(u64 val, int reg)* System registers listed in the switch are not restored on every * entry to the guest but are only restored on vcpu_load. * +* SYSREGS_ON_CPU *MUST* be checked before using this helper. +* * Note that MPIDR_EL1 for the guest is set by KVM via VMPIDR_EL2 but * should never be listed below, because the MPIDR should only be set * once, before running the VCPU, and never changed later.
arch/arm64/kvm/hyp/exception.c +4 -2
@@ -26,7 +26,8 @@ static inline u64 __vcpu_read_sys_reg(const struct kvm_vcpu *vcpu, int reg)if (unlikely (vcpu_has_nv (vcpu)))return vcpu_read_sys_reg (vcpu, reg); -else if (__vcpu_read_sys_reg_from_cpu (reg, &val)) +else if (vcpu_get_flag (vcpu,SYSREGS_ON_CPU ) && +__vcpu_read_sys_reg_from_cpu (reg, &val))return val;return __vcpu_sys_reg (vcpu, reg);
@@ -36,7 +37,8 @@ static inline void __vcpu_write_sys_reg(struct kvm_vcpu *vcpu, u64 val, int reg) {if (unlikely (vcpu_has_nv (vcpu)))vcpu_write_sys_reg (vcpu, val, reg); -else if (!__vcpu_write_sys_reg_to_cpu (val, reg)) +else if (!vcpu_get_flag (vcpu,SYSREGS_ON_CPU ) || + !__vcpu_write_sys_reg_to_cpu (val, reg))__vcpu_assign_sys_reg (vcpu, reg, val); }
Signature · 1 line
2.39.5
Re: [PATCH 6.15.y 1/5] KVM: arm64: Repaint pmcr_n into nr_pmu_counters
In reply to Sasha LevinHi Sasha,
Please drop this series, it affects code that is effectively dead prior to 6.16.
Thanks,
Oliver
Quoted 88 lines · Sasha Levin
From: Marc Zyngier <[email protected]>
[ Upstream commit
f12b54d7c24388886277598236b3eeea5c68eec4]The
pmcr_nfield obviously refers to PMCR_EL0.N, but is generally used as the number of counters seen by the guest. Rename it accordingly.Suggested-by Oliver upton <[email protected]>Signed-off-by Marc Zyngier <[email protected]>Stable-dep-of:
c6e35dff58d3("KVM: arm64: Check forSYSREGS_ON_CPUbefore accessing the CPU state")Signed-off-by Sasha Levin <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 4 ++-- arch/arm64/kvm/pmu-emul.c | 6 +++--- arch/arm64/kvm/sys_regs.c | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-)arch/arm64/include/asm/kvm_host.h +2
-2@@ -359,8 +359,8 @@ struct kvm_arch {cpumask_var_t supported_cpus; -/* PMCR_EL0.N value for the guest */ -u8 pmcr_n; +/* Maximum number of counters for the guest */ +u8 nr_pmu_counters;/* Iterator for idreg debugfs */ u8 idreg_debugfs_iter;arch/arm64/kvm/pmu-emul.c +3
-3@@ -280,7 +280,7 @@ static u64 kvm_pmu_hyp_counter_mask(struct kvm_vcpu *vcpu)return 0 ; hpmn =SYS_FIELD_GET (MDCR_EL2 ,HPMN ,__vcpu_sys_reg (vcpu,MDCR_EL2 )); - n = vcpu->kvm->arch.pmcr_n; + n = vcpu->kvm->arch.nr_pmu_counters;/* * Programming HPMN to a value greater than PMCR_EL0.N is @@ -1032,7 +1032,7 @@ static void kvm_arm_set_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)lockdep_assert_held (&kvm->arch.config_lock); kvm->arch.arm_pmu = arm_pmu; - kvm->arch.pmcr_n =kvm_arm_pmu_get_max_counters (kvm); + kvm->arch.nr_pmu_counters =kvm_arm_pmu_get_max_counters (kvm); }/** @@ -1261,7 +1261,7 @@ u64 kvm_vcpu_read_pmcr(struct kvm_vcpu *vcpu) {u64 pmcr =__vcpu_sys_reg (vcpu,PMCR_EL0 ); -return u64_replace_bits (pmcr, vcpu->kvm->arch.pmcr_n,ARMV8_PMU_PMCR_N ); +return u64_replace_bits (pmcr, vcpu->kvm->arch.nr_pmu_counters,ARMV8_PMU_PMCR_N ); }void kvm_pmu_nested_transition (struct kvm_vcpu *vcpu)arch/arm64/kvm/sys_regs.c +2
-2@@ -785,7 +785,7 @@ static unsigned int pmu_visibility(const struct kvm_vcpu *vcpu,static u64 reset_pmu_reg (struct kvm_vcpu *vcpu,const struct sys_reg_desc *r) {u64 mask =BIT (ARMV8_PMU_CYCLE_IDX ); -u8 n = vcpu->kvm->arch.pmcr_n; +u8 n = vcpu->kvm->arch.nr_pmu_counters;if (n) mask |=GENMASK (n -1 ,0 );@@ -1217,7 +1217,7 @@ static int set_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,*/ if (!kvm_vm_has_ran_once (kvm) && new_n <=kvm_arm_pmu_get_max_counters (kvm)) - kvm->arch.pmcr_n = new_n; + kvm->arch.nr_pmu_counters = new_n;mutex_unlock (&kvm->arch.config_lock);Signature · 1 line
2.39.5