[PATCH v3 2/4] LoongArch: KVM: Move host CSR_EENTRY save and restore in context switch

From: Bibo Mao

Date: Sun Mar 22 2026 - 22:58:01 EST


CSR register LOONGARCH_CSR_EENTRY is shared between host cpu and guest
vCPU, KVM need save and restore LOONGARCH_CSR_EENTRY register. Here move
CSR register LOONGARCH_CSR_EENTRY saving in context switch function,
rather than VM enter entry.

At most time VM enter/exit is much more frequent than vCPU thread context
switch.

Signed-off-by: Bibo Mao <maobibo@xxxxxxxxxxx>
---
arch/loongarch/kvm/vcpu.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c
index 028eb3d5a33b..e82b2c84ce17 100644
--- a/arch/loongarch/kvm/vcpu.c
+++ b/arch/loongarch/kvm/vcpu.c
@@ -319,7 +319,6 @@ static int kvm_pre_enter_guest(struct kvm_vcpu *vcpu)
* and it may also clear KVM_REQ_TLB_FLUSH_GPA pending bit
*/
kvm_late_check_requests(vcpu);
- vcpu->arch.host_eentry = csr_read64(LOONGARCH_CSR_EENTRY);
/* Clear KVM_LARCH_SWCSR_LATEST as CSR will change when enter guest */
vcpu->arch.aux_inuse &= ~KVM_LARCH_SWCSR_LATEST;

@@ -1624,9 +1623,11 @@ static int _kvm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
* If not, any old guest state from this vCPU will have been clobbered.
*/
context = per_cpu_ptr(vcpu->kvm->arch.vmcs, cpu);
- if (migrated || (context->last_vcpu != vcpu))
+ if (migrated || (context->last_vcpu != vcpu)) {
vcpu->arch.aux_inuse &= ~KVM_LARCH_HWCSR_USABLE;
- context->last_vcpu = vcpu;
+ context->last_vcpu = vcpu;
+ vcpu->arch.host_eentry = csr_read64(LOONGARCH_CSR_EENTRY);
+ }

/* Restore timer state regardless */
kvm_restore_timer(vcpu);
--
2.39.3