Re: [PATCH 2/6] LoongArch: KVM: Add separate vCPU and VM id update function

From: Huacai Chen

Date: Sun Aug 02 2026 - 10:12:46 EST


Hi, Bibo,

On Mon, Jul 27, 2026 at 3:29 PM Bibo Mao <maobibo@xxxxxxxxxxx> wrote:
>
> There are two-level MMU on LoongArch KVM system, the first level (stage1)
> MMU is mapping for GVA --> GPA, the second level (stage2) is mapping
> for GPA --> HPA. Similar with ASID, there is different ID management
> to support multiple VMs and vCPUs. The stage1 MMU is called vCPUID, and
> the second MMU is called VMID, the corresponding ID management function
> is kvm_check_vcpuid() and kvm_check_vmid().
>
> Signed-off-by: Bibo Mao <maobibo@xxxxxxxxxxx>
> ---
> arch/loongarch/kvm/main.c | 21 ++++++++++++++++++---
> 1 file changed, 18 insertions(+), 3 deletions(-)
>
> diff --git a/arch/loongarch/kvm/main.c b/arch/loongarch/kvm/main.c
> index 6e3e8efa1dc2..c1e1d8424fa7 100644
> --- a/arch/loongarch/kvm/main.c
> +++ b/arch/loongarch/kvm/main.c
> @@ -223,10 +223,9 @@ static void kvm_update_vpid(struct kvm_vcpu *vcpu, int cpu)
>
> context->vpid_cache = vpid;
> vcpu->arch.vpid = vpid;
> - vcpu->arch.vmid = vcpu->arch.vpid & vpid_mask;
> }
>
> -void kvm_check_vpid(struct kvm_vcpu *vcpu)
> +static void kvm_check_vcpuid(struct kvm_vcpu *vcpu)
> {
> int cpu;
> bool migrated;
> @@ -254,7 +253,6 @@ void kvm_check_vpid(struct kvm_vcpu *vcpu)
> kvm_update_vpid(vcpu, cpu);
> trace_kvm_vpid_change(vcpu, vcpu->arch.vpid);
> vcpu->cpu = cpu;
> - kvm_clear_request(KVM_REQ_TLB_FLUSH_GPA, vcpu);
>
> /*
> * LLBCTL is a separated guest CSR register from host, a general
> @@ -280,6 +278,23 @@ void kvm_check_vpid(struct kvm_vcpu *vcpu)
> }
> }
>
> +static void kvm_check_vmid(struct kvm_vcpu *vcpu)
> +{
> + unsigned long vmid;
> +
> + vmid = vcpu->arch.vpid & vpid_mask;
> + if (vcpu->arch.vmid != vmid) {
> + vcpu->arch.vmid = vmid;
> + kvm_clear_request(KVM_REQ_TLB_FLUSH_GPA, vcpu);
> + }
> +}
> +
> +void kvm_check_vpid(struct kvm_vcpu *vcpu)
> +{
> + kvm_check_vcpuid(vcpu);
> + kvm_check_vmid(vcpu);
> +}
I think the previous naming is a little better:

+void kvm_check_vpid(struct kvm_vcpu *vcpu)
+{
+ __kvm_check_vpid(vcpu);
+ __kvm_check_vmid(vcpu);
+}


Huacai


> +
> void kvm_init_vmcs(struct kvm *kvm)
> {
> kvm->arch.vmcs = vmcs;
> --
> 2.39.3
>