KVM: arm64: Cleanup + fix to vCPU reset, feature flags
cover KVM: arm64: Cleanup + fix to vCPU reset, feature flags
The way we do vCPU feature flag checks is a bit of a scattered mess between the KVM_ARM_VCPU_INIT ioctl handler and kvm_reset_vcpu(). Let's move all the feature flag checks up into the ioctl() handler to eliminate failure paths from kvm_reset_vcpu(), as other usage of this function no not handle returned errors.
Nobody screamed about the VM-wide feature flag change, so its also a good time to rip out the vestiges of the vCPU-scoped bitmap.
I also spotted a bug with the NV feature flag where we allow it regardless of system support.
Oliver Upton (8):
KVM: arm64: Add generic check for system-supported vCPU features
KVM: arm64: Hoist PMUv3 check into KVM_ARM_VCPU_INIT ioctl handler
KVM: arm64: Hoist SVE check into KVM_ARM_VCPU_INIT ioctl handler
KVM: arm64: Hoist PAuth checks into KVM_ARM_VCPU_INIT ioctl
KVM: arm64: Prevent NV feature flag on systems w/o nested virt
KVM: arm64: Hoist NV+SVE check into KVM_ARM_VCPU_INIT ioctl handler
KVM: arm64: Remove unused return value from kvm_reset_vcpu()
KVM: arm64: Get rid of vCPU-scoped feature bitmap
arch/arm64/include/asm/kvm_emulate.h | 13 +++--- arch/arm64/include/asm/kvm_host.h | 5 +-- arch/arm64/include/asm/kvm_nested.h | 3 +- arch/arm64/kvm/arch_timer.c | 4 +- arch/arm64/kvm/arm.c | 62 +++++++++++++++++++++------- arch/arm64/kvm/hypercalls.c | 2 +- arch/arm64/kvm/reset.c | 56 +++++-------------------- include/kvm/arm_arch_timer.h | 2 +- include/kvm/arm_pmu.h | 2 +- include/kvm/arm_psci.h | 2 +- 10 files changed, 72 insertions(+), 79 deletions(-)
base-commit: ce9ecca0238b140b88f43859b211c9fdfd8e5b70
Signature · 1 line
2.42.0.515.g380fc7ccd1-goog
PATCH 1/8 KVM: arm64: Add generic check for system-supported vCPU features
To date KVM has relied on kvm_reset_vcpu() failing when the vCPU feature flags are unsupported by the system. This is a bit messy since kvm_reset_vcpu() is called at runtime outside of the KVM_ARM_VCPU_INIT ioctl when it is expected to succeed. Further complicating the matter is that kvm_reset_vcpu() must tolerate be idemptotent to the config_lock, as it isn't consistently called with the lock held.
Prepare to move feature compatibility checks out of kvm_reset_vcpu() with a 'generic' check that compares the user-provided flags with a computed maximum feature set for the system.
--- arch/arm64/kvm/arm.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)
arch/arm64/kvm/arm.c +13 -3
@@ -1190,6 +1190,16 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,return -EINVAL ; } +static unsigned long system_supported_vcpu_features (void ) +{ +unsigned long features =KVM_VCPU_VALID_FEATURES ; + +if (!cpus_have_const_cap (ARM64_HAS_32BIT_EL1 )) +clear_bit (KVM_ARM_VCPU_EL1_32BIT , &features); + +return features; +} +static int kvm_vcpu_init_check_features (struct kvm_vcpu *vcpu,const struct kvm_vcpu_init *init) {
@@ -1204,12 +1214,12 @@ static int kvm_vcpu_init_check_features(struct kvm_vcpu *vcpu,return -ENOENT ; } +if (features & ~system_supported_vcpu_features ()) +return -EINVAL ; +if (!test_bit (KVM_ARM_VCPU_EL1_32BIT , &features))return 0 ; -if (!cpus_have_const_cap (ARM64_HAS_32BIT_EL1 )) -return -EINVAL ; -/* MTE is incompatible with AArch32 */ if (kvm_has_mte (vcpu->kvm))return -EINVAL ;
Signature · 1 line
2.42.0.515.g380fc7ccd1-goog
Marc Zyngier
maintainerRe: [PATCH 1/8] KVM: arm64: Add generic check for system-supported vCPU features
Quoted 22 lines · Oliver Upton
To date KVM has relied on
kvm_reset_vcpu()failing when the vCPU feature flags are unsupported by the system. This is a bit messy sincekvm_reset_vcpu()is called at runtime outside of theKVM_ARM_VCPU_INITioctl when it is expected to succeed. Further complicating the matter is thatkvm_reset_vcpu()must tolerate be idemptotent to theconfig_lock, as it isn't consistently called with the lock held.Prepare to move feature compatibility checks out of
kvm_reset_vcpu()with a 'generic' check that compares the user-provided flags with a computed maximum feature set for the system.Signed-off-by Oliver Upton <[email protected]>--- arch/arm64/kvm/arm.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)arch/arm64/kvm/arm.c +0
-0
arch/arm64/kvm/arm.c int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level, · line 1197
Quoted 8 lines
arch/arm64/kvm/arm.c +5
-0@@ -1190,6 +1190,16 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,return -EINVAL ; } +static unsigned long system_supported_vcpu_features (void ) +{ +unsigned long features =KVM_VCPU_VALID_FEATURES ; + +if (!cpus_have_const_cap (ARM64_HAS_32BIT_EL1 ))
I think we can now convert this to a cpus_have_final_cap(), as Mark is getting rid of the helper altogether, see [1].
Thanks,
M.
[1] https://lore.kernel.org/r/[email protected]
Signature · 1 line
Without deviation from the norm, progress is not possible.
Re: [PATCH 1/8] KVM: arm64: Add generic check for system-supported vCPU features
In reply to Oliver UptonQuoted 7 lines · Oliver Upton
To date KVM has relied on
kvm_reset_vcpu()failing when the vCPU feature flags are unsupported by the system. This is a bit messy sincekvm_reset_vcpu()is called at runtime outside of theKVM_ARM_VCPU_INITioctl when it is expected to succeed. Further complicating the matter is thatkvm_reset_vcpu()must tolerate be idemptotent to theconfig_lock, as it isn't consistently called with the lock held.
Quoted 8 lines
Prepare to move feature compatibility checks out of
kvm_reset_vcpu()with a 'generic' check that compares the user-provided flags with a computed maximum feature set for the system.Signed-off-by Oliver Upton <[email protected]>--- arch/arm64/kvm/arm.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)
PATCH 2/8 KVM: arm64: Hoist PMUv3 check into KVM_ARM_VCPU_INIT ioctl handler
In reply to Oliver UptonTest that the system supports PMUv3 before ever getting to kvm_reset_vcpu().
--- arch/arm64/kvm/arm.c | 3 +++ arch/arm64/kvm/reset.c | 5 ----- 2 files changed, 3 insertions(+), 5 deletions(-)
arch/arm64/kvm/arm.c +3 -0
@@ -1197,6 +1197,9 @@ static unsigned long system_supported_vcpu_features(void)if (!cpus_have_const_cap (ARM64_HAS_32BIT_EL1 ))clear_bit (KVM_ARM_VCPU_EL1_32BIT , &features); +if (!kvm_arm_support_pmu_v3 ()) +clear_bit (KVM_ARM_VCPU_PMU_V3 , &features); +return features; }
arch/arm64/kvm/reset.c +0 -5
@@ -255,11 +255,6 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu)else pstate =VCPU_RESET_PSTATE_EL1 ; -if (kvm_vcpu_has_pmu (vcpu) && !kvm_arm_support_pmu_v3 ()) { - ret = -EINVAL ; - goto out; - } -/* Reset core registers */ memset (vcpu_gp_regs (vcpu),0 ,sizeof (*vcpu_gp_regs (vcpu)));memset (&vcpu->arch.ctxt.fp_regs,0 ,sizeof (vcpu->arch.ctxt.fp_regs));
Signature · 1 line
2.42.0.515.g380fc7ccd1-goog
Re: [PATCH 2/8] KVM: arm64: Hoist PMUv3 check into KVM_ARM_VCPU_INIT ioctl handler
Quoted 8 lines · Oliver Upton
Test that the system supports PMUv3 before ever getting to
kvm_reset_vcpu().Signed-off-by Oliver Upton <[email protected]>--- arch/arm64/kvm/arm.c | 3 +++ arch/arm64/kvm/reset.c | 5 ----- 2 files changed, 3 insertions(+), 5 deletions(-)
PATCH 3/8 KVM: arm64: Hoist SVE check into KVM_ARM_VCPU_INIT ioctl handler
In reply to Oliver UptonTest that the system supports SVE before ever getting to kvm_reset_vcpu().
--- arch/arm64/kvm/arm.c | 3 +++ arch/arm64/kvm/reset.c | 14 +++----------- 2 files changed, 6 insertions(+), 11 deletions(-)
arch/arm64/kvm/arm.c +3 -0
@@ -1200,6 +1200,9 @@ static unsigned long system_supported_vcpu_features(void)if (!kvm_arm_support_pmu_v3 ())clear_bit (KVM_ARM_VCPU_PMU_V3 , &features); +if (!system_supports_sve ()) +clear_bit (KVM_ARM_VCPU_SVE , &features); +return features; }
arch/arm64/kvm/reset.c +3 -11
@@ -73,11 +73,8 @@ int __init kvm_arm_init_sve(void)return 0 ; } -static int kvm_vcpu_enable_sve (struct kvm_vcpu *vcpu) +static void kvm_vcpu_enable_sve (struct kvm_vcpu *vcpu) { -if (!system_supports_sve ()) -return -EINVAL ; - vcpu->arch.sve_max_vl = kvm_sve_max_vl;/*
@@ -86,8 +83,6 @@ static int kvm_vcpu_enable_sve(struct kvm_vcpu *vcpu)* kvm_arm_vcpu_finalize(), which freezes the configuration. */ vcpu_set_flag (vcpu,GUEST_HAS_SVE ); - -return 0 ; }/*
@@ -231,11 +226,8 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu) }if (!kvm_arm_vcpu_sve_finalized (vcpu)) { -if (test_bit (KVM_ARM_VCPU_SVE , vcpu->arch.features)) { - ret =kvm_vcpu_enable_sve (vcpu); -if (ret) - goto out; - } +if (test_bit (KVM_ARM_VCPU_SVE , vcpu->arch.features)) +kvm_vcpu_enable_sve (vcpu); }else {kvm_vcpu_reset_sve (vcpu); }
Signature · 1 line
2.42.0.515.g380fc7ccd1-goog
PATCH 4/8 KVM: arm64: Hoist PAuth checks into KVM_ARM_VCPU_INIT ioctl
In reply to Oliver UptonTest for feature support in the ioctl handler rather than kvm_reset_vcpu(). Continue to uphold our all-or-nothing policy with address and generic pointer authentication.
--- arch/arm64/kvm/arm.c | 13 +++++++++++++ arch/arm64/kvm/reset.c | 21 +++------------------ 2 files changed, 16 insertions(+), 18 deletions(-)
arch/arm64/kvm/arm.c +13 -0
@@ -1203,6 +1203,11 @@ static unsigned long system_supported_vcpu_features(void)if (!system_supports_sve ())clear_bit (KVM_ARM_VCPU_SVE , &features); +if (!system_has_full_ptr_auth ()) { +clear_bit (KVM_ARM_VCPU_PTRAUTH_ADDRESS , &features); +clear_bit (KVM_ARM_VCPU_PTRAUTH_GENERIC , &features); + } +return features; }
@@ -1223,6 +1228,14 @@ static int kvm_vcpu_init_check_features(struct kvm_vcpu *vcpu,if (features & ~system_supported_vcpu_features ())return -EINVAL ; +/* +* For now make sure that both address/generic pointer authentication +* features are requested by the userspace together. +*/ +if (test_bit (KVM_ARM_VCPU_PTRAUTH_ADDRESS , &features) != +test_bit (KVM_ARM_VCPU_PTRAUTH_GENERIC , &features)) +return -EINVAL ; +if (!test_bit (KVM_ARM_VCPU_EL1_32BIT , &features))return 0 ;
arch/arm64/kvm/reset.c +3 -18
@@ -165,20 +165,9 @@ static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu)memset (vcpu->arch.sve_state,0 ,vcpu_sve_state_size (vcpu)); } -static int kvm_vcpu_enable_ptrauth (struct kvm_vcpu *vcpu) +static void kvm_vcpu_enable_ptrauth (struct kvm_vcpu *vcpu) { -/* -* For now make sure that both address/generic pointer authentication -* features are requested by the userspace together and the system -* supports these capabilities. -*/ -if (!test_bit (KVM_ARM_VCPU_PTRAUTH_ADDRESS , vcpu->arch.features) || - !test_bit (KVM_ARM_VCPU_PTRAUTH_GENERIC , vcpu->arch.features) || - !system_has_full_ptr_auth ()) -return -EINVAL ; -vcpu_set_flag (vcpu,GUEST_HAS_PTRAUTH ); -return 0 ; }/**
@@ -233,12 +222,8 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu) }if (test_bit (KVM_ARM_VCPU_PTRAUTH_ADDRESS , vcpu->arch.features) || -test_bit (KVM_ARM_VCPU_PTRAUTH_GENERIC , vcpu->arch.features)) { -if (kvm_vcpu_enable_ptrauth (vcpu)) { - ret = -EINVAL ; - goto out; - } - } +test_bit (KVM_ARM_VCPU_PTRAUTH_GENERIC , vcpu->arch.features)) +kvm_vcpu_enable_ptrauth (vcpu);if (vcpu_el1_is_32bit (vcpu)) pstate =VCPU_RESET_PSTATE_SVC ;
Signature · 1 line
2.42.0.515.g380fc7ccd1-goog
PATCH 5/8 KVM: arm64: Prevent NV feature flag on systems w/o nested virt
In reply to Oliver UptonIt would appear that userspace can select the NV feature flag regardless of whether the system actually supports the feature. Obviously a nested guest isn't getting far in this situation; let's reject the flag instead.
--- arch/arm64/kvm/arm.c | 3 +++ 1 file changed, 3 insertions(+)
arch/arm64/kvm/arm.c +3 -0
@@ -1208,6 +1208,9 @@ static unsigned long system_supported_vcpu_features(void)clear_bit (KVM_ARM_VCPU_PTRAUTH_GENERIC , &features); } +if (!cpus_have_const_cap (ARM64_HAS_NESTED_VIRT )) +clear_bit (KVM_ARM_VCPU_HAS_EL2 , &features); +return features; }
Signature · 1 line
2.42.0.515.g380fc7ccd1-goog
Marc Zyngier
maintainerRe: [PATCH 5/8] KVM: arm64: Prevent NV feature flag on systems w/o nested virt
Quoted 5 lines · Oliver Upton
It would appear that userspace can select the NV feature flag regardless of whether the system actually supports the feature. Obviously a nested guest isn't getting far in this situation; let's reject the flag instead.
The current code is definitely odd. We rely on vcpu_has_nv() to return false, meaning that we go all the way and initialise it as an EL1-only guest. Duh. Well-behaved userspace would check the KVM_CAP_ARM_EL2 capability, which isn't upstream yet... :-(
Thanks for fixing this. I'll review the series as a whole.
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
PATCH 6/8 KVM: arm64: Hoist NV+SVE check into KVM_ARM_VCPU_INIT ioctl handler
In reply to Oliver UptonMove the feature check out of kvm_reset_vcpu() so we can make the function succeed uncondtitionally.
--- arch/arm64/kvm/arm.c | 5 +++++ arch/arm64/kvm/reset.c | 8 +------- 2 files changed, 6 insertions(+), 7 deletions(-)
arch/arm64/kvm/arm.c +5 -0
@@ -1239,6 +1239,11 @@ static int kvm_vcpu_init_check_features(struct kvm_vcpu *vcpu, test_bit(KVM_ARM_VCPU_PTRAUTH_GENERIC, &features))return -EINVAL ; +/* Disallow NV+SVE for the time being */ +if (test_bit (KVM_ARM_VCPU_HAS_EL2 , &features) && +test_bit (KVM_ARM_VCPU_SVE , &features)) +return -EINVAL ; +if (!test_bit (KVM_ARM_VCPU_EL1_32BIT , &features))return 0 ;
arch/arm64/kvm/reset.c +1 -7
@@ -208,12 +208,6 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu)if (loaded)kvm_arch_vcpu_put (vcpu); -/* Disallow NV+SVE for the time being */ -if (vcpu_has_nv (vcpu) &&vcpu_has_feature (vcpu,KVM_ARM_VCPU_SVE )) { - ret = -EINVAL ; - goto out; - } -if (!kvm_arm_vcpu_sve_finalized (vcpu)) {if (test_bit (KVM_ARM_VCPU_SVE , vcpu->arch.features))kvm_vcpu_enable_sve (vcpu);
@@ -267,7 +261,7 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu)/* Reset timer */ ret =kvm_timer_vcpu_reset (vcpu); -out: +if (loaded)kvm_arch_vcpu_load (vcpu,smp_processor_id ());preempt_enable ();
Signature · 1 line
2.42.0.515.g380fc7ccd1-goog
PATCH 7/8 KVM: arm64: Remove unused return value from kvm_reset_vcpu()
In reply to Oliver UptonGet rid of the return value for kvm_reset_vcpu() as there are no longer any cases where it returns a nonzero value.
--- arch/arm64/include/asm/kvm_host.h | 2 +- arch/arm64/kvm/arch_timer.c | 4 +--- arch/arm64/kvm/arm.c | 10 ++++------ arch/arm64/kvm/reset.c | 6 ++---- include/kvm/arm_arch_timer.h | 2 +- 5 files changed, 9 insertions(+), 15 deletions(-)
arch/arm64/include/asm/kvm_host.h +1 -1
@@ -78,7 +78,7 @@ extern unsigned int __ro_after_init kvm_sve_max_vl;int __init kvm_arm_init_sve (void ); u32 __attribute_const__ kvm_target_cpu(void); -int kvm_reset_vcpu (struct kvm_vcpu *vcpu); +void kvm_reset_vcpu (struct kvm_vcpu *vcpu);void kvm_arm_vcpu_destroy (struct kvm_vcpu *vcpu);struct kvm_hyp_memcache {
arch/arm64/kvm/arch_timer.c +1 -3
@@ -943,7 +943,7 @@ void kvm_timer_sync_user(struct kvm_vcpu *vcpu)unmask_vtimer_irq_user (vcpu); } -int kvm_timer_vcpu_reset (struct kvm_vcpu *vcpu) +void kvm_timer_vcpu_reset (struct kvm_vcpu *vcpu) {struct arch_timer_cpu *timer =vcpu_timer (vcpu);struct timer_map map;
@@ -987,8 +987,6 @@ int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu)soft_timer_cancel (&map.emul_vtimer->hrtimer);if (map.emul_ptimer)soft_timer_cancel (&map.emul_ptimer->hrtimer); - -return 0 ; }static void timer_context_init (struct kvm_vcpu *vcpu,int timerid)
arch/arm64/kvm/arm.c +4 -6
@@ -1282,15 +1282,12 @@ static int __kvm_vcpu_set_target(struct kvm_vcpu *vcpu,bitmap_copy (vcpu->arch.features, &features,KVM_VCPU_MAX_FEATURES );/* Now we know what it is, we can reset it. */ - ret =kvm_reset_vcpu (vcpu); -if (ret) { -bitmap_zero (vcpu->arch.features,KVM_VCPU_MAX_FEATURES ); - goto out_unlock; - } +kvm_reset_vcpu (vcpu);bitmap_copy (kvm->arch.vcpu_features, &features,KVM_VCPU_MAX_FEATURES );set_bit (KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED , &kvm->arch.flags);vcpu_set_flag (vcpu,VCPU_INITIALIZED ); + ret =0 ; out_unlock:mutex_unlock (&kvm->arch.config_lock);return ret;
@@ -1315,7 +1312,8 @@ static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,if (kvm_vcpu_init_changed (vcpu, init))return -EINVAL ; -return kvm_reset_vcpu (vcpu); +kvm_reset_vcpu (vcpu); +return 0 ; } static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
arch/arm64/kvm/reset.c +2 -4
@@ -188,10 +188,9 @@ static void kvm_vcpu_enable_ptrauth(struct kvm_vcpu *vcpu)* disable preemption around the vcpu reset as we would otherwise race with * preempt notifiers which also call put/load. */ -int kvm_reset_vcpu (struct kvm_vcpu *vcpu) +void kvm_reset_vcpu (struct kvm_vcpu *vcpu) {struct vcpu_reset_state reset_state; -int ret;bool loaded;u32 pstate;
@@ -260,12 +259,11 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu) }/* Reset timer */ - ret =kvm_timer_vcpu_reset (vcpu); +kvm_timer_vcpu_reset (vcpu);if (loaded)kvm_arch_vcpu_load (vcpu,smp_processor_id ());preempt_enable (); -return ret; }u32 get_kvm_ipa_limit (void )
include/kvm/arm_arch_timer.h +1 -1
@@ -94,7 +94,7 @@ struct arch_timer_cpu {int __init kvm_timer_hyp_init (bool has_gic);int kvm_timer_enable (struct kvm_vcpu *vcpu); -int kvm_timer_vcpu_reset (struct kvm_vcpu *vcpu); +void kvm_timer_vcpu_reset (struct kvm_vcpu *vcpu);void kvm_timer_vcpu_init (struct kvm_vcpu *vcpu);void kvm_timer_sync_user (struct kvm_vcpu *vcpu);bool kvm_timer_should_notify_user (struct kvm_vcpu *vcpu);
Signature · 1 line
2.42.0.515.g380fc7ccd1-goog
PATCH 8/8 KVM: arm64: Get rid of vCPU-scoped feature bitmap
In reply to Oliver UptonThe vCPU-scoped feature bitmap was left in place a couple of releases ago in case the change to VM-scoped vCPU features broke anyone. Nobody has complained and the interop between VM and vCPU bitmaps is pretty gross. Throw it out.
--- arch/arm64/include/asm/kvm_emulate.h | 13 ++++++------- arch/arm64/include/asm/kvm_host.h | 3 --- arch/arm64/include/asm/kvm_nested.h | 3 ++- arch/arm64/kvm/arm.c | 9 ++++----- arch/arm64/kvm/hypercalls.c | 2 +- arch/arm64/kvm/reset.c | 6 +++--- include/kvm/arm_pmu.h | 2 +- include/kvm/arm_psci.h | 2 +- 8 files changed, 18 insertions(+), 22 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +6 -7
@@ -54,6 +54,11 @@ void kvm_emulate_nested_eret(struct kvm_vcpu *vcpu);int kvm_inject_nested_sync (struct kvm_vcpu *vcpu,u64 esr_el2);int kvm_inject_nested_irq (struct kvm_vcpu *vcpu); +static inline bool vcpu_has_feature (const struct kvm_vcpu *vcpu,int feature) +{ +return test_bit (feature, vcpu->kvm->arch.vcpu_features); +} +#if defined(__KVM_VHE_HYPERVISOR__) || defined(__KVM_NVHE_HYPERVISOR__) static __always_inline bool vcpu_el1_is_32bit(struct kvm_vcpu *vcpu) {
@@ -62,7 +67,7 @@ static __always_inline bool vcpu_el1_is_32bit(struct kvm_vcpu *vcpu)#else static __always_inline bool vcpu_el1_is_32bit(struct kvm_vcpu *vcpu) { -return test_bit (KVM_ARM_VCPU_EL1_32BIT , vcpu->arch.features); +return vcpu_has_feature (vcpu,KVM_ARM_VCPU_EL1_32BIT ); }#endif
@@ -565,12 +570,6 @@ static __always_inline void kvm_incr_pc(struct kvm_vcpu *vcpu)vcpu_set_flag ((v), e); \ }while (0 ) - -static inline bool vcpu_has_feature(struct kvm_vcpu *vcpu, int feature) -{ -return test_bit (feature, vcpu->arch.features); -} - static __always_inline void kvm_write_cptr_el2(u64 val) {if (has_vhe () ||has_hvhe ())
arch/arm64/include/asm/kvm_host.h +0 -3
@@ -574,9 +574,6 @@ struct kvm_vcpu_arch {/* Cache some mmu pages needed inside spinlock regions */ struct kvm_mmu_memory_cache mmu_page_cache; -/* feature flags */ -DECLARE_BITMAP (features,KVM_VCPU_MAX_FEATURES ); -/* Virtual SError ESR to restore when HCR_EL2.VSE is set */ u64 vsesr_el2;
arch/arm64/include/asm/kvm_nested.h +2 -1
@@ -2,13 +2,14 @@#ifndef __ARM64_KVM_NESTED_H#define __ARM64_KVM_NESTED_H +#include <asm/kvm_emulate.h> #include <linux/kvm_host.h> static inline bool vcpu_has_nv (const struct kvm_vcpu *vcpu) {return (!__is_defined (__KVM_NVHE_HYPERVISOR__) &&cpus_have_final_cap (ARM64_HAS_NESTED_VIRT ) && -test_bit (KVM_ARM_VCPU_HAS_EL2 , vcpu->arch.features)); +vcpu_has_feature (vcpu,KVM_ARM_VCPU_HAS_EL2 )); }extern bool __check_nv_sr_forward (struct kvm_vcpu *vcpu);
arch/arm64/kvm/arm.c +4 -5
@@ -367,7 +367,6 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)/* Force users to call KVM_ARM_VCPU_INIT */ vcpu_clear_flag (vcpu,VCPU_INITIALIZED ); -bitmap_zero (vcpu->arch.features,KVM_VCPU_MAX_FEATURES ); vcpu->arch.mmu_page_cache.gfp_zero = __GFP_ZERO;
@@ -1263,7 +1262,8 @@ static bool kvm_vcpu_init_changed(struct kvm_vcpu *vcpu, {unsigned long features = init->features[0 ]; -return !bitmap_equal (vcpu->arch.features, &features,KVM_VCPU_MAX_FEATURES ); +return !bitmap_equal (vcpu->kvm->arch.vcpu_features, &features, +KVM_VCPU_MAX_FEATURES ); } static int __kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
@@ -1276,15 +1276,14 @@ static int __kvm_vcpu_set_target(struct kvm_vcpu *vcpu,mutex_lock (&kvm->arch.config_lock);if (test_bit (KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED , &kvm->arch.flags) && - !bitmap_equal (kvm->arch.vcpu_features, &features,KVM_VCPU_MAX_FEATURES )) +kvm_vcpu_init_changed (vcpu, init)) goto out_unlock; -bitmap_copy (vcpu->arch.features, &features,KVM_VCPU_MAX_FEATURES ); +bitmap_copy (kvm->arch.vcpu_features, &features,KVM_VCPU_MAX_FEATURES );/* Now we know what it is, we can reset it. */ kvm_reset_vcpu (vcpu); -bitmap_copy (kvm->arch.vcpu_features, &features,KVM_VCPU_MAX_FEATURES );set_bit (KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED , &kvm->arch.flags);vcpu_set_flag (vcpu,VCPU_INITIALIZED ); ret =0 ;
arch/arm64/kvm/hypercalls.c +1 -1
@@ -554,7 +554,7 @@ int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) {bool wants_02; - wants_02 =test_bit (KVM_ARM_VCPU_PSCI_0_2 , vcpu->arch.features); + wants_02 =vcpu_has_feature (vcpu,KVM_ARM_VCPU_PSCI_0_2 );switch (val) {case KVM_ARM_PSCI_0_1 :
arch/arm64/kvm/reset.c +3 -3
@@ -208,14 +208,14 @@ void kvm_reset_vcpu(struct kvm_vcpu *vcpu)kvm_arch_vcpu_put (vcpu);if (!kvm_arm_vcpu_sve_finalized (vcpu)) { -if (test_bit (KVM_ARM_VCPU_SVE , vcpu->arch.features)) +if (vcpu_has_feature (vcpu,KVM_ARM_VCPU_SVE ))kvm_vcpu_enable_sve (vcpu); }else {kvm_vcpu_reset_sve (vcpu); } -if (test_bit (KVM_ARM_VCPU_PTRAUTH_ADDRESS , vcpu->arch.features) || -test_bit (KVM_ARM_VCPU_PTRAUTH_GENERIC , vcpu->arch.features)) +if (vcpu_has_feature (vcpu,KVM_ARM_VCPU_PTRAUTH_ADDRESS ) || +vcpu_has_feature (vcpu,KVM_ARM_VCPU_PTRAUTH_GENERIC ))kvm_vcpu_enable_ptrauth (vcpu);if (vcpu_el1_is_32bit (vcpu))
include/kvm/arm_pmu.h +1 -1
@@ -77,7 +77,7 @@ void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu);void kvm_vcpu_pmu_resync_el0 (void );#define kvm_vcpu_has_pmu (vcpu) \ - (test_bit(KVM_ARM_VCPU_PMU_V3, (vcpu)->arch.features)) + (vcpu_has_feature(vcpu, KVM_ARM_VCPU_PMU_V3))/* * Updates the vcpu's view of the pmu events for this cpu.
include/kvm/arm_psci.h +1 -1
@@ -26,7 +26,7 @@ static inline int kvm_psci_version(struct kvm_vcpu *vcpu)* revisions. It is thus safe to return the latest, unless * userspace has instructed us otherwise. */ -if (test_bit (KVM_ARM_VCPU_PSCI_0_2 , vcpu->arch.features)) { +if (vcpu_has_feature (vcpu,KVM_ARM_VCPU_PSCI_0_2 )) {if (vcpu->kvm->arch.psci_version)return vcpu->kvm->arch.psci_version;
Signature · 1 line
2.42.0.515.g380fc7ccd1-goog
Quoted 7 lines · Oliver Upton
The way we do vCPU feature flag checks is a bit of a scattered mess between the
KVM_ARM_VCPU_INITioctl handler andkvm_reset_vcpu(). Let's move all the feature flag checks up into theioctl()handler to eliminate failure paths fromkvm_reset_vcpu(), as other usage of this function no not handle returned errors.
Quoted 5 lines
Nobody screamed about the VM-wide feature flag change, so its also a good time to rip out the vestiges of the vCPU-scoped bitmap.
I also spotted a bug with the NV feature flag where we allow it regardless of system support.
Thanks for going the extra mile refactoring this stuff. Apart from the const/final nit, this looks good to me. It'd be good to have this in -next shortly.
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Oliver Upton
AppliedQuoted 10 lines · Oliver Upton
The way we do vCPU feature flag checks is a bit of a scattered mess between the
KVM_ARM_VCPU_INITioctl handler andkvm_reset_vcpu(). Let's move all the feature flag checks up into theioctl()handler to eliminate failure paths fromkvm_reset_vcpu(), as other usage of this function no not handle returned errors.Nobody screamed about the VM-wide feature flag change, so its also a good time to rip out the vestiges of the vCPU-scoped bitmap.
[...]
Applied to kvmarm/next, with the change to use cpus_have_final_cap() per Marc's suggestion.
[1/8] KVM: arm64: Add generic check for system-supported vCPU features
https://git.kernel.org/kvmarm/kvmarm/c/ef150908b6bd [2/8] KVM: arm64: Hoist PMUv3 check into KVM_ARM_VCPU_INIT ioctl handler https://git.kernel.org/kvmarm/kvmarm/c/9116db11feb5 [3/8] KVM: arm64: Hoist SVE check into KVM_ARM_VCPU_INIT ioctl handler https://git.kernel.org/kvmarm/kvmarm/c/be9c0c018389 [4/8] KVM: arm64: Hoist PAuth checks into KVM_ARM_VCPU_INIT ioctl https://git.kernel.org/kvmarm/kvmarm/c/baa28a53ddbe [5/8] KVM: arm64: Prevent NV feature flag on systems w/o nested virt https://git.kernel.org/kvmarm/kvmarm/c/12405b09926f [6/8] KVM: arm64: Hoist NV+SVE check into KVM_ARM_VCPU_INIT ioctl handler https://git.kernel.org/kvmarm/kvmarm/c/d99fb82fd35e [7/8] KVM: arm64: Remove unused return value from kvm_reset_vcpu() https://git.kernel.org/kvmarm/kvmarm/c/3d4b2a4cddd7 [8/8] KVM: arm64: Get rid of vCPU-scoped feature bitmap https://git.kernel.org/kvmarm/kvmarm/c/1de10b7d13a9
--
Best,
Oliver