virt inclusion category: feature bugzilla: https://atomgit.com/openeuler/kernel/issues/10293 ---------------------------------------- Introduce `KVM_REQ_DIRECT_TLBI`, and add a pending-range ledger to track Stage-2 TLB invalidation requests. Each vCPU maintains its own list of pending invalidation ranges. When a new range is added, it is merged with existing ranges where possible. If the ranges cannot be merged, the pending requests are replaced with a full VMID flush. Each vCPU processes its pending invalidation ranges before entering the guest. Suggested-by: Yanan Wang <wangyanan55@huawei.com> Signed-off-by: Jinqian Yang <yangjinqian1@huawei.com> --- arch/arm64/include/asm/kvm_host.h | 19 ++++++ arch/arm64/include/asm/kvm_tlbi.h | 108 ++++++++++++++++++++++++++++++ arch/arm64/kvm/arm.c | 20 ++++++ 3 files changed, 147 insertions(+) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index 88666fc07c82..5cd8e1b7dc2d 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -72,6 +72,10 @@ #define KVM_REQ_RELOAD_WFI_TRAPS KVM_ARCH_REQ(9) #define KVM_REQ_RELOAD_TIMER_EARLY_INJECT KVM_ARCH_REQ(10) #define KVM_REQ_RELOAD_VTLBID KVM_ARCH_REQ(11) +#ifdef CONFIG_KVM_ARM_DIRECT_TLBI +#define KVM_REQ_DIRECT_TLBI \ + KVM_ARCH_REQ_FLAGS(12, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) +#endif #define KVM_DIRTY_LOG_MANUAL_CAPS (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \ KVM_DIRTY_LOG_INITIALLY_SET) @@ -279,6 +283,13 @@ struct kvm_arch { struct mutex config_lock; #ifdef CONFIG_KVM_ARM_DIRECT_TLBI + /* + * Sticky once a directed flush has been issued: the migration + * gate keeps flushing on every cross-pCPU load, so the flag is + * never cleared. + */ + bool tlbi_directed; + /* * Sticky once a pass-through (VFIO) device is attached: on * systems sharing stage-2 with the SMMU its TLB relies on DVM @@ -286,6 +297,7 @@ struct kvm_arch { */ bool has_vfio_devices; #endif + /* * If we encounter a data abort without valid instruction syndrome * information, report this to user space. User space can (and @@ -781,6 +793,13 @@ struct kvm_vcpu_arch { /* The last pCPU this vCPU was loaded on */ KABI_EXTEND(int last_ran_cpu) + +#ifdef CONFIG_KVM_ARM_DIRECT_TLBI + spinlock_t tlbi_pending_lock; + u64 tlbi_pending_start; + u64 tlbi_pending_size; + bool tlbi_pending; +#endif }; /* diff --git a/arch/arm64/include/asm/kvm_tlbi.h b/arch/arm64/include/asm/kvm_tlbi.h index 0dbf3091af58..372f1b4be59a 100644 --- a/arch/arm64/include/asm/kvm_tlbi.h +++ b/arch/arm64/include/asm/kvm_tlbi.h @@ -19,6 +19,15 @@ static __always_inline bool kvm_tlbi_direct_enabled(struct kvm *kvm) return false; } +static __always_inline void kvm_tlbi_request_flush_range(struct kvm *kvm, + u64 start, u64 size) +{ +} + +static __always_inline void kvm_tlbi_request_flush(struct kvm *kvm) +{ + kvm_tlbi_request_flush_range(kvm, 0, 0); +} #else #include <asm/kvm_emulate.h> @@ -44,6 +53,105 @@ static __always_inline bool kvm_tlbi_direct_enabled(struct kvm *kvm) in_task() && !irqs_disabled(); } +/* + * Merge a pending invalidation into every vCPU's per-vCPU pending + * range, consumed on guest entry. Ranges that can't be merged + * escalate to a whole-VMID flush (size 0). + */ +static __always_inline void kvm_tlbi_mark_pending(struct kvm *kvm, + u64 start, u64 size) +{ + struct kvm_vcpu *vcpu; + unsigned long i; + + kvm_for_each_vcpu(i, vcpu, kvm) { + unsigned long flags; + u64 pend_end, new_end; + + spin_lock_irqsave(&vcpu->arch.tlbi_pending_lock, flags); + if (!vcpu->arch.tlbi_pending) { + vcpu->arch.tlbi_pending = true; + vcpu->arch.tlbi_pending_start = start; + vcpu->arch.tlbi_pending_size = size; + } else if (!size) { + vcpu->arch.tlbi_pending_size = 0; + } else if (vcpu->arch.tlbi_pending_size) { + pend_end = vcpu->arch.tlbi_pending_start + + vcpu->arch.tlbi_pending_size; + new_end = start + size; + + if (start <= pend_end && + new_end >= vcpu->arch.tlbi_pending_start) { + vcpu->arch.tlbi_pending_start = + min(vcpu->arch.tlbi_pending_start, start); + vcpu->arch.tlbi_pending_size = + max(pend_end, new_end) - + vcpu->arch.tlbi_pending_start; + } else { + vcpu->arch.tlbi_pending_size = 0; + } + } + spin_unlock_irqrestore(&vcpu->arch.tlbi_pending_lock, flags); + } +} + +/* Extract and clear the pending invalidation of this vCPU. */ +static __always_inline void kvm_tlbi_take_pending(struct kvm_vcpu *vcpu, + u64 *start, u64 *size) +{ + unsigned long flags; + + spin_lock_irqsave(&vcpu->arch.tlbi_pending_lock, flags); + if (vcpu->arch.tlbi_pending) { + *start = vcpu->arch.tlbi_pending_start; + *size = vcpu->arch.tlbi_pending_size; + vcpu->arch.tlbi_pending = false; + vcpu->arch.tlbi_pending_size = 0; + } else { + *start = 0; + *size = 0; + } + spin_unlock_irqrestore(&vcpu->arch.tlbi_pending_lock, flags); +} + +static __always_inline void kvm_tlbi_request_flush_range(struct kvm *kvm, + u64 start, u64 size) +{ + /* + * Publish the page-table updates that triggered this flush + * before the requests go out: the consuming vCPUs' local TLBI + * must observe them, which the arm64 TLB maintenance sequence + * requires of the writer (DSB ISHST before the invalidation). + */ + dsb(ishst); + smp_store_release(&kvm->arch.tlbi_directed, true); + kvm_tlbi_mark_pending(kvm, start, size); + kvm_make_all_cpus_request(kvm, KVM_REQ_DIRECT_TLBI); +} + +static __always_inline void kvm_tlbi_request_flush(struct kvm *kvm) +{ + kvm_tlbi_request_flush_range(kvm, 0, 0); +} + +void __kvm_tlb_flush_vmid_nsh(struct kvm_s2_mmu *mmu); +void __kvm_tlb_flush_vmid_range_nsh(struct kvm_s2_mmu *mmu, + phys_addr_t start, unsigned long pages); + +static __always_inline void kvm_tlbi_flush_vmid_nsh(struct kvm_s2_mmu *mmu) +{ + __kvm_tlb_flush_vmid_nsh(mmu); + isb(); +} + +static __always_inline void kvm_tlbi_flush_vmid_range_nsh(struct kvm_s2_mmu *mmu, + phys_addr_t start, + unsigned long size) +{ + __kvm_tlb_flush_vmid_range_nsh(mmu, start, size >> PAGE_SHIFT); + isb(); +} + #endif /* !__KVM_NVHE_HYPERVISOR__ */ #endif /* CONFIG_KVM_ARM_DIRECT_TLBI */ diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 7581d45828e9..d0bb33a2b043 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -853,6 +853,10 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) vcpu->arch.last_ran_cpu = -1; +#ifdef CONFIG_KVM_ARM_DIRECT_TLBI + spin_lock_init(&vcpu->arch.tlbi_pending_lock); +#endif + err = kvm_vgic_vcpu_init(vcpu); if (err) return err; @@ -1620,6 +1624,22 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu) if (kvm_check_request(KVM_REQ_VM_DEAD, vcpu)) return -EIO; +#ifdef CONFIG_KVM_ARM_DIRECT_TLBI + if (kvm_check_request(KVM_REQ_DIRECT_TLBI, vcpu)) { + u64 start, size; + + kvm_tlbi_take_pending(vcpu, &start, &size); + + preempt_disable(); + if (size) + kvm_tlbi_flush_vmid_range_nsh(vcpu->arch.hw_mmu, + start, size); + else + kvm_tlbi_flush_vmid_nsh(vcpu->arch.hw_mmu); + preempt_enable(); + } +#endif + if (kvm_check_request(KVM_REQ_SLEEP, vcpu)) kvm_vcpu_sleep(vcpu); -- 2.33.0