From 69d3fd99024e7b2c139cace82e19db786a33d8c5 Mon Sep 17 00:00:00 2001 From: Bibo Mao Date: Mon, 13 Jan 2025 21:37:17 +0800 Subject: [PATCH] anolis: LoongArch: KVM: Clear LLBCTL if secondary mmu mapping is changed ANBZ: #31153 LLBCTL is a separated guest CSR register from host, host exception ERET instruction will clear the host LLBCTL CSR register, and guest exception will clear the guest LLBCTL CSR register. VCPU0 atomic64_fetch_add_unless VCPU1 atomic64_fetch_add_unless ll.d %[p], %[c] beq %[p], %[u], 1f Here secondary mmu mapping is changed, host hpa page is replaced with a new page. And VCPU1 will execute atomic instruction on the new page. ll.d %[p], %[c] beq %[p], %[u], 1f add.d %[rc], %[p], %[a] sc.d %[rc], %[c] add.d %[rc], %[p], %[a] sc.d %[rc], %[c] LLBCTL is set on VCPU0 and it represents the memory is not modified by other VCPUs, sc.d will modify the memory directly. So clear WCLLB of the guest LLBCTL register when mapping is the changed. Fixes: ("LoongArch:LSVZ: Clear LLBCTL if secondary mmu mapping is changed") Signed-off-by: Bibo Mao Signed-off-by: Huacai Chen Signed-off-by: Xianglai Li Signed-off-by: Juxin Gao --- arch/loongarch/kvm/main.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/arch/loongarch/kvm/main.c b/arch/loongarch/kvm/main.c index ef2bad612c00..3dddf3c8f222 100644 --- a/arch/loongarch/kvm/main.c +++ b/arch/loongarch/kvm/main.c @@ -246,20 +246,22 @@ void kvm_check_vpid(struct kvm_vcpu *vcpu) vcpu->cpu = cpu; kvm_clear_request(KVM_REQ_TLB_FLUSH_GPA, vcpu); /* - * LLBCTL_WCLLB is separated CSR register from host - * eret instruction in host mode clears host LLBCTL_WCLLB - * register, and clears guest register in guest mode + * LLBCTL is a separated guest CSR register from host, a general + * exception ERET instruction clears the host LLBCTL register in + * host mode, and clears the guest LLBCTL register in guest mode. + * ERET in tlb refill exception does not clear LLBCTL register. * - * When gpa --> hpa mapping is changed, guest does not know - * even if the content is changed with new address + * When secondary mmu mapping is changed, guest OS does not know + * even if the content is changed after mapping is changed. * - * Here clear guest LLBCTL_WCLLB register when mapping is - * changed, else if mapping is changed when guest is executing - * LL/SC pair, LL loads old address, SC store new address - * successfully since LLBCTL_WCLLB is on, even if memory - * with new address is changed with other VCPUs. + * Here clear WCLLB of the guest LLBCTL register when mapping is + * changed. Otherwise, if mmu mapping is changed while guest is + * executing LL/SC pair, LL loads with the old address and set + * the LLBCTL flag, SC checks the LLBCTL flag and will store the + * new address successfully since LLBCTL_WCLLB is on, even if + * memory with new address is changed on other VCPUs. */ - set_gcsr_llbctl(LOONGARCH_CSR_LLBCTL); + set_gcsr_llbctl(CSR_LLBCTL_WCLLB); } /* Restore GSTAT(0x50).vpid */ -- Gitee