[PATCH OLK-6.6 v2 0/8] KVM: arm64: Directed stage-2 TLB invalidation
This series replaces KVM's broadcast stage-2 TLB invalidation with a directed one: instead of a TLBI over the whole inner-shareable domain , KVM kicks only the vCPUs of the VM via KVM_REQ_TLB_FLUSH and lets each of them locally invalidate its own stage-2 context before re-entering the guest. Changes since v1: - replace the generic KVM_REQ_TLB_FLUSH with a dedicated arm64 KVM_REQ_DIRECT_TLBI (WAIT|NO_WAKEUP): the arch hooks issue it themselves, and the routing decision is consolidated in kvm_tlbi_direct_enabled(), taken once at the call site; - keep every new line under CONFIG_KVM_ARM_DIRECT_TLBI (VHE-only, knob renamed to kvm_direct_tlbi), restore the TLBID/DVMBM paths to their baseline form, and publish page-table updates with a DSB ISHST on the request side; - exclude realm VMs and pinned VMIDs, and broadcast once on the first VFIO device assignment to close the routing-check race; - restructure the series: primitives, last-pCPU tracking (KABI_EXTEND), switch and predicate, pass-through exclusion, request machinery, migration gate, then the two enablement sites (the remote-flush hooks, kvm_tlb_flush_vmid_range()), so every commit builds on its own. Jinqian Yang (8): KVM: arm64: Add non-shareable VMID TLB invalidation primitive KVM: arm64: Track the last pCPU each vCPU ran on KVM: arm64: Add the direct-TLBI switch and routing predicate KVM: arm64: Exclude VMs with pass-through devices from directed flushes KVM: arm64: Add the directed request machinery KVM: arm64: Flush on migration behind the directed gate KVM: arm64: Route remote TLB flushes through the request machinery KVM: arm64: Route the shared range flush through the request machinery arch/arm64/include/asm/kvm_host.h | 37 +++++++ arch/arm64/include/asm/kvm_tlbi.h | 159 ++++++++++++++++++++++++++++++ arch/arm64/kvm/Kconfig | 23 +++++ arch/arm64/kvm/arm.c | 56 +++++++++++ arch/arm64/kvm/hyp/pgtable.c | 9 ++ arch/arm64/kvm/hyp/vhe/tlb.c | 52 ++++++++++ arch/arm64/kvm/mmu.c | 47 +++++++++ 7 files changed, 383 insertions(+) create mode 100644 arch/arm64/include/asm/kvm_tlbi.h -- 2.33.0
virt inclusion category: feature bugzilla: https://atomgit.com/openeuler/kernel/issues/10293 ---------------------------------------- Add __kvm_tlb_flush_vmid_nsh() and its range-based companion, which locally invalidate this VM's stage-2 context on the calling pCPU instead of broadcasting over the inner-shareable domain. Without FEAT_TLBIRANGE, or beyond what a single range instruction can cover, the range variant falls back to the whole-VMID local flush. VHE-only. No functional change. Suggested-by: Yanan Wang <wangyanan55@huawei.com> Signed-off-by: Jinqian Yang <yangjinqian1@huawei.com> --- arch/arm64/kvm/hyp/vhe/tlb.c | 52 ++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/arch/arm64/kvm/hyp/vhe/tlb.c b/arch/arm64/kvm/hyp/vhe/tlb.c index 39f0965b4a50..12849fed7628 100644 --- a/arch/arm64/kvm/hyp/vhe/tlb.c +++ b/arch/arm64/kvm/hyp/vhe/tlb.c @@ -225,6 +225,58 @@ void __kvm_tlb_flush_vmid(struct kvm_s2_mmu *mmu) __tlb_switch_to_host(&cxt); } +#ifdef CONFIG_KVM_ARM_DIRECT_TLBI +void __kvm_tlb_flush_vmid_nsh(struct kvm_s2_mmu *mmu) +{ + struct tlb_inv_context cxt; + + dsb(nshst); + + /* Switch to requested VMID */ + __tlb_switch_to_guest(mmu, &cxt); + + __tlbi(vmalls12e1); + dsb(nsh); + isb(); + + __tlb_switch_to_host(&cxt); +} + +void __kvm_tlb_flush_vmid_range_nsh(struct kvm_s2_mmu *mmu, + phys_addr_t start, unsigned long pages) +{ + struct tlb_inv_context cxt; + unsigned long stride; + + /* + * Without FEAT_TLBIRANGE, or beyond what a single range + * instruction can cover, fall back to a whole-VMID local + * flush. + */ + if (!system_supports_tlb_range() || pages > MAX_TLBI_RANGE_PAGES) { + __kvm_tlb_flush_vmid_nsh(mmu); + return; + } + + stride = PAGE_SIZE; + start = round_down(start, stride); + + dsb(nshst); + + /* Switch to requested VMID */ + __tlb_switch_to_guest(mmu, &cxt); + + __flush_s2_tlb_range_op(ipas2e1, start, pages, stride, 0); + + dsb(nsh); + __tlbi(vmalle1); + dsb(nsh); + isb(); + + __tlb_switch_to_host(&cxt); +} +#endif /* CONFIG_KVM_ARM_DIRECT_TLBI */ + void __kvm_flush_cpu_context(struct kvm_s2_mmu *mmu) { struct tlb_inv_context cxt; -- 2.33.0
virt inclusion category: feature bugzilla: https://atomgit.com/openeuler/kernel/issues/10293 ---------------------------------------- Add vcpu->arch.last_ran_cpu, initialised to -1 at vCPU creation and updated to the current pCPU on every load and put. Suggested-by: Yanan Wang <wangyanan55@huawei.com> Signed-off-by: Jinqian Yang <yangjinqian1@huawei.com> --- arch/arm64/include/asm/kvm_host.h | 3 +++ arch/arm64/kvm/arm.c | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index 7e69a920eac2..efd05a1c172d 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -770,6 +770,9 @@ struct kvm_vcpu_arch { u64 prod_el2; } hdbss) #endif + + /* The last pCPU this vCPU was loaded on */ + KABI_EXTEND(int last_ran_cpu) }; /* diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 56a2daf882d7..761b3fe0075c 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -826,6 +826,8 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) vcpu->arch.hw_mmu = &vcpu->kvm->arch.mmu; + vcpu->arch.last_ran_cpu = -1; + err = kvm_vgic_vcpu_init(vcpu); if (err) return err; @@ -1188,6 +1190,8 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) *last_ran = vcpu->vcpu_id; } + vcpu->arch.last_ran_cpu = cpu; + vcpu->cpu = cpu; kvm_vgic_load(vcpu); @@ -1244,6 +1248,8 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) kvm_timer_vcpu_put(vcpu); kvm_vgic_put(vcpu); + vcpu->arch.last_ran_cpu = vcpu->cpu; + vcpu->cpu = -1; if (vcpu_is_rec(vcpu)) -- 2.33.0
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
virt inclusion category: feature bugzilla: https://atomgit.com/openeuler/kernel/issues/10293 ---------------------------------------- When the SMMU shares stage-2 page tables with KVM, it relies on broadcast TLBI to invalidate its TLB through DVM snooping. A local-only TLBI does not invalidate the SMMU's TLB, which could allow device DMA to access pages that have already been freed. To prevent this, implement `kvm_arch_start_assignment()` and `kvm_arch_has_assigned_device()` for arm64, under `CONFIG_KVM_ARM_DIRECT_TLBI`. When the first VFIO device is assigned to a VM, set a per-VM flag to permanently disable direct TLBI for that VM. This ensures that subsequent TLB invalidations use the broadcast path. VMs with a pinned VMID for the UMMU nested stage-2 are also excluded from direct TLBI, using the existing pinned VMID reference count. Suggested-by: Yanan Wang <wangyanan55@huawei.com> Signed-off-by: Jinqian Yang <yangjinqian1@huawei.com> --- arch/arm64/include/asm/kvm_host.h | 15 ++++++++++++++ arch/arm64/include/asm/kvm_tlbi.h | 7 +++++-- arch/arm64/kvm/mmu.c | 33 +++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index efd05a1c172d..88666fc07c82 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -278,6 +278,14 @@ struct kvm_arch { /* Protects VM-scoped configuration data */ struct mutex config_lock; +#ifdef CONFIG_KVM_ARM_DIRECT_TLBI + /* + * Sticky once a pass-through (VFIO) device is attached: on + * systems sharing stage-2 with the SMMU its TLB relies on DVM + * snooping of the broadcast TLBI, so such VMs stay broadcast. + */ + 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 @@ -1447,6 +1455,13 @@ struct kvm_memory_slot; void kvm_arch_finish_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot); +#ifdef CONFIG_KVM_ARM_DIRECT_TLBI +#define __KVM_HAVE_ARCH_ASSIGNED_DEVICE +void kvm_arch_start_assignment(struct kvm *kvm); +void kvm_arch_end_assignment(struct kvm *kvm); +bool kvm_arch_has_assigned_device(struct kvm *kvm); +#endif + static inline bool kvm_vm_is_protected(struct kvm *kvm) { return false; diff --git a/arch/arm64/include/asm/kvm_tlbi.h b/arch/arm64/include/asm/kvm_tlbi.h index a72cc15cb635..0dbf3091af58 100644 --- a/arch/arm64/include/asm/kvm_tlbi.h +++ b/arch/arm64/include/asm/kvm_tlbi.h @@ -30,14 +30,17 @@ static __always_inline bool kvm_tlbi_direct_enabled(struct kvm *kvm) * 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). + * the knob plus VHE, no realm or pKVM VM, no + * pass-through device, no pinned VMID, 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) && + !kvm_arch_has_assigned_device(kvm) && + !refcount_read(&kvm->arch.pinned) && in_task() && !irqs_disabled(); } diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 92aa66d4470c..07b923f42270 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -165,6 +165,39 @@ static bool memslot_is_logging(struct kvm_memory_slot *memslot) return memslot->dirty_bitmap && !(memslot->flags & KVM_MEM_READONLY); } +#ifdef CONFIG_KVM_ARM_DIRECT_TLBI +/* + * The SMMU sharing this VM's stage-2 page tables invalidates its TLB + * via the DVM snooping of the broadcast TLBI: a directed (local-only) + * flush would leave stale entries and let device DMA hit freed pages. + * The flag is sticky: the broadcast path is always safe. + */ +void kvm_arch_start_assignment(struct kvm *kvm) +{ + if (READ_ONCE(kvm->arch.has_vfio_devices)) + return; + + WRITE_ONCE(kvm->arch.has_vfio_devices, true); + + /* + * Close the window with a concurrent directed flush that + * already passed the routing check: broadcast once so the + * SMMU cannot keep stale entries from before the switch. + */ + kvm_call_hyp(__kvm_tlb_flush_vmid, &kvm->arch.mmu); +} + +void kvm_arch_end_assignment(struct kvm *kvm) +{ + /* Sticky: nothing to do, see above. */ +} + +bool kvm_arch_has_assigned_device(struct kvm *kvm) +{ + return READ_ONCE(kvm->arch.has_vfio_devices); +} +#endif /* CONFIG_KVM_ARM_DIRECT_TLBI */ + /** * kvm_arch_flush_remote_tlbs() - flush all VM TLB entries for v7/8 * @kvm: pointer to kvm structure. -- 2.33.0
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
virt inclusion category: feature bugzilla: https://atomgit.com/openeuler/kernel/issues/10293 ---------------------------------------- When Direct TLBI is enabled and a vCPU migrates to another pCPU, stale TLB entries may remain on the old pCPU. Therefore, a local TLBI must be performed on the new pCPU after migration to prevent stale TLB entries from affecting execution. Suggested-by: Yanan Wang <wangyanan55@huawei.com> Signed-off-by: Jinqian Yang <yangjinqian1@huawei.com> --- arch/arm64/kvm/arm.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index d0bb33a2b043..98ff9bb60256 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -1218,6 +1218,12 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) kvm_call_hyp(__kvm_flush_cpu_context, mmu); *last_ran = vcpu->vcpu_id; } + +#ifdef CONFIG_KVM_ARM_DIRECT_TLBI + if (smp_load_acquire(&vcpu->kvm->arch.tlbi_directed) && + !vcpu_is_rec(vcpu) && vcpu->arch.last_ran_cpu != cpu) + kvm_tlbi_flush_vmid_nsh(mmu); +#endif vcpu->arch.last_ran_cpu = cpu; vcpu->cpu = cpu; -- 2.33.0
virt inclusion category: feature bugzilla: https://atomgit.com/openeuler/kernel/issues/10293 ---------------------------------------- Enable the directed TLBI path in the two remote TLB flush functions: `kvm_arch_flush_remote_tlbs()` and `kvm_arch_flush_remote_tlbs_range()`. When `kvm_tlbi_direct_enabled()` returns true, these functions send `KVM_REQ_DIRECT_TLBI` requests to the affected vCPUs instead of using the traditional broadcast TLBI path. Each vCPU processes the request before entering the guest and performs a local TLBI on its Stage-2 translation context. Suggested-by: Yanan Wang <wangyanan55@huawei.com> Signed-off-by: Jinqian Yang <yangjinqian1@huawei.com> --- arch/arm64/kvm/mmu.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 07b923f42270..5a9af931e6dc 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -19,6 +19,7 @@ #include <asm/kvm_ras.h> #include <asm/kvm_asm.h> #include <asm/kvm_emulate.h> +#include <asm/kvm_tlbi.h> #include <asm/virt.h> #ifdef CONFIG_HISI_VIRTCCA_CODA #include <asm/kvm_tmi.h> @@ -206,6 +207,12 @@ bool kvm_arch_has_assigned_device(struct kvm *kvm) */ int kvm_arch_flush_remote_tlbs(struct kvm *kvm) { +#ifdef CONFIG_KVM_ARM_DIRECT_TLBI + if (kvm_tlbi_direct_enabled(kvm)) { + kvm_tlbi_request_flush(kvm); + return 0; + } +#endif kvm_call_hyp(__kvm_tlb_flush_vmid, &kvm->arch.mmu); return 0; } @@ -213,6 +220,13 @@ int kvm_arch_flush_remote_tlbs(struct kvm *kvm) int kvm_arch_flush_remote_tlbs_range(struct kvm *kvm, gfn_t gfn, u64 nr_pages) { +#ifdef CONFIG_KVM_ARM_DIRECT_TLBI + if (kvm_tlbi_direct_enabled(kvm)) { + kvm_tlbi_request_flush_range(kvm, gfn << PAGE_SHIFT, + nr_pages << PAGE_SHIFT); + return 0; + } +#endif kvm_tlb_flush_vmid_range(&kvm->arch.mmu, gfn << PAGE_SHIFT, nr_pages << PAGE_SHIFT); return 0; -- 2.33.0
virt inclusion category: feature bugzilla: https://atomgit.com/openeuler/kernel/issues/10293 ---------------------------------------- Route `kvm_tlb_flush_vmid_range()` through the directed TLBI mechanism. When `kvm_tlbi_direct_enabled()` returns true, the invalidation range is sent to each vCPU as a pending TLBI request. Each vCPU performs the local TLBI when it next enters the guest. Suggested-by: Yanan Wang <wangyanan55@huawei.com> Signed-off-by: Jinqian Yang <yangjinqian1@huawei.com> --- arch/arm64/kvm/hyp/pgtable.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c index 4b6adcf5a206..4a93a1dbc682 100644 --- a/arch/arm64/kvm/hyp/pgtable.c +++ b/arch/arm64/kvm/hyp/pgtable.c @@ -10,8 +10,10 @@ #include <linux/bitfield.h> #include <asm/cacheflush.h> #include <asm/cpufeature.h> +#include <asm/kvm_mmu.h> #include <asm/kvm_pgtable.h> #include <asm/kvm_pvm.h> +#include <asm/kvm_tlbi.h> #include <asm/memory.h> #include <asm/stage2_pgtable.h> @@ -705,6 +707,13 @@ void kvm_tlb_flush_vmid_range(struct kvm_s2_mmu *mmu, { unsigned long pages = size >> PAGE_SHIFT; +#ifdef CONFIG_KVM_ARM_DIRECT_TLBI + if (kvm_tlbi_direct_enabled(kvm_s2_mmu_to_kvm(mmu))) { + kvm_tlbi_request_flush_range(kvm_s2_mmu_to_kvm(mmu), addr, size); + return; + } +#endif + if (!system_supports_tlb_range() || pages > MAX_TLBI_RANGE_PAGES) { kvm_call_hyp(__kvm_tlb_flush_vmid, mmu); return; -- 2.33.0
反馈: 您发送到kernel@openeuler.org的补丁/补丁集,已成功转换为PR! PR链接地址: https://atomgit.com/openeuler/kernel/merge_requests/31255 邮件列表地址:https://mailweb.openeuler.org/archives/list/kernel@openeuler.org/message/UAM... FeedBack: The patch(es) which you have sent to kernel@openeuler.org mailing list has been converted to a pull request successfully! Pull request link: https://atomgit.com/openeuler/kernel/merge_requests/31255 Mailing list address: https://mailweb.openeuler.org/archives/list/kernel@openeuler.org/message/UAM...
participants (2)
-
Jinqian Yang -
patchwork bot