virt inclusion category: feature bugzilla: https://atomgit.com/openeuler/kernel/issues/10293 ---------------------------------------- Introduce `CONFIG_KVM_ARM_DIRECT_TLBI` and a new `direct_tlbi` module parameter exposed through `/sys/module/kvm/parameters/direct_tlbi`. Direct TLBI is supported only in VHE mode. The module parameter cannot be enabled in nVHE mode. Add `kvm_tlbi_direct_enabled()` to check whether direct TLBI can be used. This function checks all the conditions required for the direct TLBI path: - The `direct_tlbi` parameter is enabled. - KVM is running in VHE mode. - The VM is neither a Realm VM nor a pKVM VM. - The caller runs in task context with interrupts enabled Suggested-by: Yanan Wang <wangyanan55@huawei.com> Signed-off-by: Jinqian Yang <yangjinqian1@huawei.com> --- arch/arm64/include/asm/kvm_tlbi.h | 48 +++++++++++++++++++++++++++++++ arch/arm64/kvm/Kconfig | 23 +++++++++++++++ arch/arm64/kvm/arm.c | 26 ++++++++++++++++- 3 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/include/asm/kvm_tlbi.h diff --git a/arch/arm64/include/asm/kvm_tlbi.h b/arch/arm64/include/asm/kvm_tlbi.h new file mode 100644 index 000000000000..a72cc15cb635 --- /dev/null +++ b/arch/arm64/include/asm/kvm_tlbi.h @@ -0,0 +1,48 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __ARM64_KVM_TLBI_H__ +#define __ARM64_KVM_TLBI_H__ + +#include <linux/kvm_host.h> + +#ifdef CONFIG_KVM_ARM_DIRECT_TLBI + +extern bool kvm_direct_tlbi; + +#if defined(__KVM_NVHE_HYPERVISOR__) +/* + * The nVHE hypervisor has no request machinery: direct_tlbi refuses to + * enable there, so the stubs are never taken by the callers, which fall + * back to the broadcast. + */ +static __always_inline bool kvm_tlbi_direct_enabled(struct kvm *kvm) +{ + return false; +} + +#else + +#include <asm/kvm_emulate.h> +#include <asm/virt.h> +#include <linux/irqflags.h> +#include <linux/preempt.h> + +/* + * Routing decision for a stage-2 invalidation: use the directed + * (per-vCPU request) machinery rather than a broadcast. All the + * constraints live here so that the routing sites stay in sync: + * the knob plus VHE, no realm or pKVM VM, and the caller must be + * able to wait for the vCPUs (task context, IRQs on). + */ +static __always_inline bool kvm_tlbi_direct_enabled(struct kvm *kvm) +{ + return READ_ONCE(kvm_direct_tlbi) && + is_kernel_in_hyp_mode() && + !is_protected_kvm_enabled() && !kvm_is_realm(kvm) && + in_task() && !irqs_disabled(); +} + +#endif /* !__KVM_NVHE_HYPERVISOR__ */ + +#endif /* CONFIG_KVM_ARM_DIRECT_TLBI */ + +#endif /* __ARM64_KVM_TLBI_H__ */ diff --git a/arch/arm64/kvm/Kconfig b/arch/arm64/kvm/Kconfig index 2a71109ce082..e48b4a521a8b 100644 --- a/arch/arm64/kvm/Kconfig +++ b/arch/arm64/kvm/Kconfig @@ -157,4 +157,27 @@ config KVM_ARM_VTLBID If unsure, say N. +config KVM_ARM_DIRECT_TLBI + bool "Direct stage-2 TLB invalidation for KVM (EXPERIMENTAL)" + depends on KVM + default n + help + Replace KVM's broadcast stage-2 TLB invalidation with a + directed one: instead of broadcasting a TLBI over the whole + inner-shareable domain, KVM_REQ_DIRECT_TLBI requests are sent + to the vCPUs of the VM, each of which locally invalidates its + stage-2 context before re-entering the guest. vCPUs are also + tracked per-pCPU so that migration leaves no stale entry + behind. This is profitable for small VMs running on big + machines, e.g. a 2-vCPU VM on a 128-CPU system. + + The behaviour is controlled at runtime via the + /sys/module/kvm/parameters/direct_tlbi knob and can be + toggled without rebooting: each flush is self-contained. + + VHE-only: the local invalidation primitive runs directly at + EL2, and the knob refuses to enable the feature in nVHE mode. + + If unsure, say N. + endif # VIRTUALIZATION diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 761b3fe0075c..7581d45828e9 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -40,6 +40,7 @@ #include <asm/kvm_nested.h> #include <asm/kvm_pkvm.h> #include <asm/kvm_emulate.h> +#include <asm/kvm_tlbi.h> #include <asm/kvm_rme.h> #include <asm/sections.h> #include <asm/kvm_tmi.h> @@ -74,6 +75,30 @@ bool kvm_ncsnp_support; /* Capability of DVMBM */ bool kvm_dvmbm_support; +#ifdef CONFIG_KVM_ARM_DIRECT_TLBI +bool kvm_direct_tlbi; + +static int direct_tlbi_set(const char *val, const struct kernel_param *kp) +{ + int ret = param_set_bool(val, kp); + + if (!ret && kvm_direct_tlbi && !is_kernel_in_hyp_mode()) { + kvm_direct_tlbi = false; + kvm_err("direct_tlbi requires VHE\n"); + return -EINVAL; + } + + return ret; +} + +static const struct kernel_param_ops direct_tlbi_ops = { + .set = direct_tlbi_set, + .get = param_get_bool, +}; + +module_param_cb(direct_tlbi, &direct_tlbi_ops, &kvm_direct_tlbi, 0644); +#endif + static DEFINE_PER_CPU(unsigned char, kvm_hyp_initialized); bool is_kvm_arm_initialised(void) @@ -1189,7 +1214,6 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) kvm_call_hyp(__kvm_flush_cpu_context, mmu); *last_ran = vcpu->vcpu_id; } - vcpu->arch.last_ran_cpu = cpu; vcpu->cpu = cpu; -- 2.33.0