From: lishusen <lishusen2@h-partners.com> In scenarios such as sandboxes where QEMU is not used, the pv qspinlock feature cannot be enabled through libvirt or QEMU configurations. Add /sys/module/kvm/parameters/pv_qspinlock_force_enable to enable this feature in such scenarios. Signed-off-by: lishusen <lishusen2@h-partners.com> --- arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/kvm/arm.c | 6 ++++++ arch/arm64/kvm/hypercalls.c | 34 +++++++++++++++++++++---------- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index 07e8144d55f7..eee10254da3f 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -1265,6 +1265,7 @@ static inline bool kvm_arm_vcpu_is_preempted(struct kvm_vcpu_arch *vcpu_arch) #endif #ifdef CONFIG_PARAVIRT_SPINLOCKS +extern bool pv_qspinlock_force_enable; static inline void kvm_arm_pvspin_vcpu_init(struct kvm_vcpu_arch *vcpu_arch) { vcpu_arch->pv.pv_unhalted = false; diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 324553fea612..ebcc16204c4f 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -92,6 +92,12 @@ bool pv_preempted_enable = true; MODULE_PARM_DESC(pv_preempted_enable, "bool"); module_param_cb(pv_preempted_enable, &pv_preempted_enable_ops, &pv_preempted_enable, 0644); +#ifdef CONFIG_PARAVIRT_SPINLOCKS +bool pv_qspinlock_force_enable; +MODULE_PARM_DESC(pv_qspinlock_force_enable, "Force enable PV qspinlock"); +module_param(pv_qspinlock_force_enable, bool, 0644); +#endif + static int vcpu_req_reload_wfi_traps(const char *val, const struct kernel_param *kp); static const struct kernel_param_ops force_wfi_trap_ops = { diff --git a/arch/arm64/kvm/hypercalls.c b/arch/arm64/kvm/hypercalls.c index 847abfbf3813..744697755fae 100644 --- a/arch/arm64/kvm/hypercalls.c +++ b/arch/arm64/kvm/hypercalls.c @@ -101,6 +101,23 @@ static bool kvm_smccc_default_allowed(u32 func_id) } } +#ifdef CONFIG_PARAVIRT_SPINLOCKS +static bool kvm_pvspin_is_enabled(struct kvm_smccc_features *smccc_feat) +{ + if (pv_qspinlock_force_enable) + return true; + + /* + * NOPVSPINLOCK carries negative polarity: the bit is set by + * default (feature disabled) and userspace clears it to enable + * the PV queued spinlock. KICK_CPU is therefore allowed only + * while the bit is clear. + */ + return !test_bit(KVM_REG_ARM_VENDOR_HYP_BIT_NOPVSPINLOCK, + &smccc_feat->vendor_hyp_bmap); +} +#endif + static bool kvm_smccc_test_fw_bmap(struct kvm_vcpu *vcpu, u32 func_id) { struct kvm_smccc_features *smccc_feat = &vcpu->kvm->arch.smccc_feat; @@ -144,14 +161,7 @@ static bool kvm_smccc_test_fw_bmap(struct kvm_vcpu *vcpu, u32 func_id) #endif #ifdef CONFIG_PARAVIRT_SPINLOCKS case ARM_SMCCC_VENDOR_KICK_CPU: - /* - * NOPVSPINLOCK carries negative polarity: the bit is set by - * default (feature disabled) and userspace clears it to enable - * the PV queued spinlock. KICK_CPU is therefore allowed only - * while the bit is clear. - */ - return !test_bit(KVM_REG_ARM_VENDOR_HYP_BIT_NOPVSPINLOCK, - &smccc_feat->vendor_hyp_bmap); + return kvm_pvspin_is_enabled(smccc_feat); #endif default: return false; @@ -404,8 +414,7 @@ int kvm_smccc_call_handler(struct kvm_vcpu *vcpu) #endif /* CONFIG_PARAVIRT_SCHED */ #ifdef CONFIG_PARAVIRT_SPINLOCKS case ARM_SMCCC_VENDOR_KICK_CPU: - if (!test_bit(KVM_REG_ARM_VENDOR_HYP_BIT_NOPVSPINLOCK, - &smccc_feat->vendor_hyp_bmap)) + if (kvm_pvspin_is_enabled(smccc_feat)) val[0] = SMCCC_RET_SUCCESS; break; #endif /* CONFIG_PARAVIRT_SPINLOCKS */ @@ -437,7 +446,10 @@ int kvm_smccc_call_handler(struct kvm_vcpu *vcpu) #endif /* CONFIG_PARAVIRT_SCHED */ #ifdef CONFIG_PARAVIRT_SPINLOCKS case ARM_SMCCC_VENDOR_KICK_CPU: - val[0] = kvm_pvspin_kick_vcpu(vcpu); + if (kvm_pvspin_is_enabled(smccc_feat)) + val[0] = kvm_pvspin_kick_vcpu(vcpu); + else + val[0] = SMCCC_RET_NOT_SUPPORTED; break; #endif /* CONFIG_PARAVIRT_SPINLOCKS */ #ifdef CONFIG_ARM64_HISI_IPIV -- 2.33.0