Implement paravirtualized TLB flush for LoongArch KVM guests using the existing steal-time shared memory page. The mechanism uses the preempted byte in struct kvm_steal_time with an additional KVM_VCPU_FLUSH_TLB flag bit: - When a guest vCPU needs remote TLB flush but the target vCPU is preempted (not running), it atomically sets KVM_VCPU_FLUSH_TLB in the target's steal-time preempted byte instead of sending an IPI. - When the host re-enters the target vCPU (kvm_update_stolen_time()), it atomically reads and clears the preempted byte via amswap_db.w. If KVM_VCPU_FLUSH_TLB was set, the host drops the vCPU's VPID, which triggers a full TLB flush on the next VM entry via kvm_check_vpid(). - For non-preempted vCPUs, the guest falls back to normal IPI-based flush, avoiding unnecessary VM exits. This significantly reduces TLB flush overhead in multi-vCPU workloads where target vCPUs are often idle/preempted. Signed-off-by: Tao Cui --- arch/loongarch/include/asm/kvm_host.h | 1 + arch/loongarch/include/asm/kvm_para.h | 1 + arch/loongarch/include/uapi/asm/kvm.h | 1 + arch/loongarch/include/uapi/asm/kvm_para.h | 1 + arch/loongarch/kvm/trace.h | 15 ++++++++++ arch/loongarch/kvm/vcpu.c | 32 +++++++++++++++++++++- arch/loongarch/kvm/vm.c | 3 ++ 7 files changed, 53 insertions(+), 1 deletion(-) diff --git a/arch/loongarch/include/asm/kvm_host.h b/arch/loongarch/include/asm/kvm_host.h index 903cb67ca817..a379868fb147 100644 --- a/arch/loongarch/include/asm/kvm_host.h +++ b/arch/loongarch/include/asm/kvm_host.h @@ -170,6 +170,7 @@ enum emulation_result { #define LOONGARCH_PV_FEAT_MASK (BIT(KVM_FEATURE_IPI) | \ BIT(KVM_FEATURE_PREEMPT) | \ BIT(KVM_FEATURE_STEAL_TIME) | \ + BIT(KVM_FEATURE_PV_TLB_FLUSH) |\ BIT(KVM_FEATURE_USER_HCALL) | \ BIT(KVM_FEATURE_VIRT_EXTIOI)) diff --git a/arch/loongarch/include/asm/kvm_para.h b/arch/loongarch/include/asm/kvm_para.h index fb17ba0fa101..28e3fa3b4c0e 100644 --- a/arch/loongarch/include/asm/kvm_para.h +++ b/arch/loongarch/include/asm/kvm_para.h @@ -41,6 +41,7 @@ struct kvm_steal_time { __u8 pad[47]; }; #define KVM_VCPU_PREEMPTED (1 << 0) +#define KVM_VCPU_FLUSH_TLB (1 << 1) /* * Hypercall interface for KVM hypervisor diff --git a/arch/loongarch/include/uapi/asm/kvm.h b/arch/loongarch/include/uapi/asm/kvm.h index cd0b5c11ca9c..e4cd4bbf8914 100644 --- a/arch/loongarch/include/uapi/asm/kvm.h +++ b/arch/loongarch/include/uapi/asm/kvm.h @@ -106,6 +106,7 @@ struct kvm_fpu { #define KVM_LOONGARCH_VM_FEAT_PTW 8 #define KVM_LOONGARCH_VM_FEAT_MSGINT 9 #define KVM_LOONGARCH_VM_FEAT_PV_PREEMPT 10 +#define KVM_LOONGARCH_VM_FEAT_PV_TLB_FLUSH 11 /* Device Control API on vcpu fd */ #define KVM_LOONGARCH_VCPU_CPUCFG 0 diff --git a/arch/loongarch/include/uapi/asm/kvm_para.h b/arch/loongarch/include/uapi/asm/kvm_para.h index d28cbcadd276..8872839251cc 100644 --- a/arch/loongarch/include/uapi/asm/kvm_para.h +++ b/arch/loongarch/include/uapi/asm/kvm_para.h @@ -16,6 +16,7 @@ #define KVM_FEATURE_IPI 1 #define KVM_FEATURE_STEAL_TIME 2 #define KVM_FEATURE_PREEMPT 3 +#define KVM_FEATURE_PV_TLB_FLUSH 4 /* BIT 24 - 31 are features configurable by user space vmm */ #define KVM_FEATURE_VIRT_EXTIOI 24 #define KVM_FEATURE_USER_HCALL 25 diff --git a/arch/loongarch/kvm/trace.h b/arch/loongarch/kvm/trace.h index 3467ee22b704..8556954fa196 100644 --- a/arch/loongarch/kvm/trace.h +++ b/arch/loongarch/kvm/trace.h @@ -210,6 +210,21 @@ TRACE_EVENT(kvm_vpid_change, TP_printk("VPID: 0x%08lx", __entry->vpid) ); +TRACE_EVENT(kvm_pv_tlb_flush, + TP_PROTO(struct kvm_vcpu *vcpu, bool need_flush), + TP_ARGS(vcpu, need_flush), + TP_STRUCT__entry( + __field(unsigned int, vcpu_id) + __field(bool, need_flush) + ), + TP_fast_assign( + __entry->vcpu_id = vcpu->vcpu_id; + __entry->need_flush = need_flush; + ), + TP_printk("vcpu %u need_flush %u", __entry->vcpu_id, + __entry->need_flush) +); + #endif /* _TRACE_KVM_H */ #undef TRACE_INCLUDE_PATH diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c index d82782b90457..3b505a1c0a9f 100644 --- a/arch/loongarch/kvm/vcpu.c +++ b/arch/loongarch/kvm/vcpu.c @@ -173,7 +173,37 @@ static void kvm_update_stolen_time(struct kvm_vcpu *vcpu) } st = (struct kvm_steal_time __user *)ghc->hva; - if (kvm_guest_has_pv_feature(vcpu, KVM_FEATURE_PREEMPT)) { + if (kvm_guest_has_pv_feature(vcpu, KVM_FEATURE_PV_TLB_FLUSH)) { + u32 old = 0; + int err = 0; + + /* + * Atomically read and clear st->preempted. amswap_db.w + * provides full barrier semantics and avoids the race with + * guest-side try_cmpxchg(). Operates on the 32-bit aligned + * word containing preempted (pad bytes are always zero). + */ + asm volatile( + "1: amswap_db.w %1, %z3, %2 \n" + "2: \n" + _ASM_EXTABLE_UACCESS_ERR_ZERO(1b, 2b, %0, %1) + : "+r" (err), "+&r" (old), + "+ZB" (*(u32 *)&st->preempted) + : "Jr" (0) + : "memory"); + + if (err) + goto out; + + vcpu->arch.st.preempted = 0; + + if ((u8)old & KVM_VCPU_FLUSH_TLB) { + vcpu->arch.vpid = 0; /* Drop vpid to flush TLB */ + trace_kvm_pv_tlb_flush(vcpu, true); + } else { + trace_kvm_pv_tlb_flush(vcpu, false); + } + } else if (kvm_guest_has_pv_feature(vcpu, KVM_FEATURE_PREEMPT)) { unsafe_put_user(0, &st->preempted, out); vcpu->arch.st.preempted = 0; } diff --git a/arch/loongarch/kvm/vm.c b/arch/loongarch/kvm/vm.c index 8af874e0b36b..4be70e4f1279 100644 --- a/arch/loongarch/kvm/vm.c +++ b/arch/loongarch/kvm/vm.c @@ -54,8 +54,10 @@ static void kvm_vm_init_features(struct kvm *kvm) if (kvm_pvtime_supported()) { kvm->arch.pv_features |= BIT(KVM_FEATURE_PREEMPT); kvm->arch.pv_features |= BIT(KVM_FEATURE_STEAL_TIME); + kvm->arch.pv_features |= BIT(KVM_FEATURE_PV_TLB_FLUSH); kvm->arch.kvm_features |= BIT(KVM_LOONGARCH_VM_FEAT_PV_PREEMPT); kvm->arch.kvm_features |= BIT(KVM_LOONGARCH_VM_FEAT_PV_STEALTIME); + kvm->arch.kvm_features |= BIT(KVM_LOONGARCH_VM_FEAT_PV_TLB_FLUSH); } kvm->arch.pv_auto_features = kvm->arch.pv_features; } @@ -159,6 +161,7 @@ static int kvm_vm_feature_has_attr(struct kvm *kvm, struct kvm_device_attr *attr case KVM_LOONGARCH_VM_FEAT_PV_IPI: case KVM_LOONGARCH_VM_FEAT_PV_PREEMPT: case KVM_LOONGARCH_VM_FEAT_PV_STEALTIME: + case KVM_LOONGARCH_VM_FEAT_PV_TLB_FLUSH: if (kvm_vm_support(&kvm->arch, attr->attr)) return 0; return -ENXIO; -- 2.43.0