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