Re: [PATCH v3 4/4] LoongArch: KVM: Remove KVM_LARCH_LASX and KVM_LARCH_LSX
From: Huacai Chen
Date: Sat Jun 06 2026 - 05:50:52 EST
Hi, Bibo,
On Tue, Jun 2, 2026 at 4:55 PM Bibo Mao <maobibo@xxxxxxxxxxx> wrote:
>
>
>
> On 2026/6/2 下午3:43, Bibo Mao wrote:
> > In kvm_lose_fpu() FPU state will save in vcpu::arch::fpu, its FPU
> > state comes from vcpu->arch.aux_inuse. Instead existing API
> > vm_guest_has_xxx() can be used also, and bit KVM_LARCH_LASX and
> > KVM_LARCH_LSX in arch.aux_inuse are removed. It makes the logic
> > simpler than ever.
> >
> > Signed-off-by: Bibo Mao <maobibo@xxxxxxxxxxx>
> > ---
> > arch/loongarch/include/asm/kvm_host.h | 2 --
> > arch/loongarch/kvm/vcpu.c | 17 +++++++++--------
> > 2 files changed, 9 insertions(+), 10 deletions(-)
> >
> > diff --git a/arch/loongarch/include/asm/kvm_host.h b/arch/loongarch/include/asm/kvm_host.h
> > index bad16c5f963c..aa2000cfb58a 100644
> > --- a/arch/loongarch/include/asm/kvm_host.h
> > +++ b/arch/loongarch/include/asm/kvm_host.h
> > @@ -158,8 +158,6 @@ enum emulation_result {
> > };
> >
> > #define KVM_LARCH_FPU (0x1 << 0)
> > -#define KVM_LARCH_LSX (0x1 << 1)
> > -#define KVM_LARCH_LASX (0x1 << 2)
> > #define KVM_LARCH_LBT (0x1 << 3)
> > #define KVM_LARCH_PMU (0x1 << 4)
> > #define KVM_LARCH_SWCSR_LATEST (0x1 << 5)
> > diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c
> > index 53abf7aa61f8..523d4cf88343 100644
> > --- a/arch/loongarch/kvm/vcpu.c
> > +++ b/arch/loongarch/kvm/vcpu.c
> > @@ -1392,7 +1392,7 @@ int kvm_own_lsx(struct kvm_vcpu *vcpu)
> > kvm_restore_lsx(&vcpu->arch.fpu);
> >
> > trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_LSX);
> > - vcpu->arch.aux_inuse |= KVM_LARCH_LSX | KVM_LARCH_FPU;
> > + vcpu->arch.aux_inuse |= KVM_LARCH_FPU;
> >
> > return 0;
> > }
> > @@ -1408,7 +1408,7 @@ int kvm_own_lasx(struct kvm_vcpu *vcpu)
> > kvm_restore_lasx(&vcpu->arch.fpu);
> >
> > trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_LASX);
> > - vcpu->arch.aux_inuse |= KVM_LARCH_LASX | KVM_LARCH_LSX | KVM_LARCH_FPU;
> > + vcpu->arch.aux_inuse |= KVM_LARCH_FPU;
> >
> > return 0;
> > }
> > @@ -1417,31 +1417,32 @@ int kvm_own_lasx(struct kvm_vcpu *vcpu)
> > /* Save context and disable FPU */
> > void kvm_lose_fpu(struct kvm_vcpu *vcpu)
> > {
> > + if (!(vcpu->arch.aux_inuse & KVM_LARCH_FPU))
> > + return;
> here is problem with AI review
>
> https://sashiko.dev/#/patchset/20260602074316.1647373-1-maobibo@xxxxxxxxxxx?part=4
>
> kvm_lose_fpu() implies that kvm_lose_lbt(), there is problem returning
> directly without calling kvm_lose_lbt().
So we need V4? Maybe we can move the if condition after
preempt_disable() and then goto the callsite of kvm_lose_lbt().
Huacai
>
> Regards
> Bibo Mao
> > +
> > preempt_disable();
> >
> > kvm_check_fcsr_alive(vcpu);
> > - if (vcpu->arch.aux_inuse & KVM_LARCH_LASX) {
> > + if (kvm_guest_has_lasx(&vcpu->arch)) {
> > kvm_save_lasx(&vcpu->arch.fpu);
> > - vcpu->arch.aux_inuse &= ~(KVM_LARCH_LSX | KVM_LARCH_FPU | KVM_LARCH_LASX);
> > trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_LASX);
> >
> > /* Disable LASX & LSX & FPU */
> > clear_csr_euen(CSR_EUEN_FPEN | CSR_EUEN_LSXEN | CSR_EUEN_LASXEN);
> > - } else if (vcpu->arch.aux_inuse & KVM_LARCH_LSX) {
> > + } else if (kvm_guest_has_lsx(&vcpu->arch)) {
> > kvm_save_lsx(&vcpu->arch.fpu);
> > - vcpu->arch.aux_inuse &= ~(KVM_LARCH_LSX | KVM_LARCH_FPU);
> > trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_LSX);
> >
> > /* Disable LSX & FPU */
> > clear_csr_euen(CSR_EUEN_FPEN | CSR_EUEN_LSXEN);
> > - } else if (vcpu->arch.aux_inuse & KVM_LARCH_FPU) {
> > + } else {
> > kvm_save_fpu(&vcpu->arch.fpu);
> > - vcpu->arch.aux_inuse &= ~KVM_LARCH_FPU;
> > trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU);
> >
> > /* Disable FPU */
> > clear_csr_euen(CSR_EUEN_FPEN);
> > }
> > + vcpu->arch.aux_inuse &= ~KVM_LARCH_FPU;
> > kvm_lose_lbt(vcpu);
> >
> > preempt_enable();
> >
>
>