Re: [PATCH v2 3/5] LoongArch: KVM: Add separate vmid feature support

From: Huacai Chen

Date: Thu Aug 06 2026 - 08:22:50 EST


Hi, Bibo,

On Thu, Aug 6, 2026 at 10:15 AM Bibo Mao <maobibo@xxxxxxxxxxx> wrote:
>
> Feature cpu_has_guestid is used to check whether separate vmid/vpid
> is supported or not. It is disabled by default, add this feature
> checking, there is no function change.
>
> Signed-off-by: Bibo Mao <maobibo@xxxxxxxxxxx>
> ---
> arch/loongarch/kvm/main.c | 14 +++++++++-----
> arch/loongarch/kvm/mmu.c | 6 ++++--
> arch/loongarch/kvm/vcpu.c | 3 ++-
> 3 files changed, 15 insertions(+), 8 deletions(-)
>
> diff --git a/arch/loongarch/kvm/main.c b/arch/loongarch/kvm/main.c
> index ac804e77e844..c36e62ab0d91 100644
> --- a/arch/loongarch/kvm/main.c
> +++ b/arch/loongarch/kvm/main.c
> @@ -218,7 +218,8 @@ static void kvm_update_vpid(struct kvm_vcpu *vcpu, int cpu)
> ++vpid; /* vpid 0 reserved for root */
>
> /* start new vpid cycle */
> - kvm_flush_tlb_all();
> + if (!cpu_has_guestid)
> + kvm_flush_tlb_all();
> }
>
> context->vpid_cache = vpid;
> @@ -282,10 +283,13 @@ static void __kvm_check_vmid(struct kvm_vcpu *vcpu)
> {
> unsigned long vmid;
>
> - vmid = vcpu->arch.vpid & vpid_mask;
> - if (vcpu->arch.hw_vmid != vmid) {
> - vcpu->arch.hw_vmid = vcpu->arch.vpid & vpid_mask;
> - kvm_clear_request(KVM_REQ_TLB_FLUSH_GPA, vcpu);
> + /* On some machines like 3A5000, vmid needs the same with vpid */
> + if (!cpu_has_guestid) {
> + vmid = vcpu->arch.vpid & vpid_mask;
> + if (vcpu->arch.hw_vmid != vmid) {
> + vcpu->arch.hw_vmid = vcpu->arch.vpid & vpid_mask;
> + kvm_clear_request(KVM_REQ_TLB_FLUSH_GPA, vcpu);
> + }
Merge Patch#3 and Patch#4, then __kvm_check_vmid() can handle both
cpu_has_guestid and !cpu_has_guestid cases together.


Huacai

> }
> }
>
> diff --git a/arch/loongarch/kvm/mmu.c b/arch/loongarch/kvm/mmu.c
> index e104897aa532..714640a2e614 100644
> --- a/arch/loongarch/kvm/mmu.c
> +++ b/arch/loongarch/kvm/mmu.c
> @@ -929,8 +929,10 @@ int kvm_handle_mm_fault(struct kvm_vcpu *vcpu, unsigned long gpa, bool write, in
> *
> * With SW PTW, invalid TLB is added in TLB refill exception.
> */
> - vcpu->arch.flush_gpa = gpa;
> - kvm_make_request(KVM_REQ_TLB_FLUSH_GPA, vcpu);
> + if (!cpu_has_guestid) {
> + vcpu->arch.flush_gpa = gpa;
> + kvm_make_request(KVM_REQ_TLB_FLUSH_GPA, vcpu);
> + }
> }
>
> return 0;
> diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c
> index 20c207d80e31..0b2355aec932 100644
> --- a/arch/loongarch/kvm/vcpu.c
> +++ b/arch/loongarch/kvm/vcpu.c
> @@ -210,7 +210,8 @@ static int kvm_check_requests(struct kvm_vcpu *vcpu)
> return RESUME_GUEST;
>
> if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
> - vcpu->arch.vpid = 0; /* Drop vpid for this vCPU */
> + if (!cpu_has_guestid)
> + vcpu->arch.vpid = 0; /* Drop vpid for this vCPU */
>
> if (kvm_dirty_ring_check_request(vcpu))
> return RESUME_HOST;
> --
> 2.39.3
>