KVM: RISC-V: refactor vector state reset
authorRadim Krčmář <rkrcmar@ventanamicro.com>
Thu, 3 Apr 2025 11:25:20 +0000 (13:25 +0200)
committerAnup Patel <anup@brainfault.org>
Wed, 21 May 2025 04:04:46 +0000 (09:34 +0530)
Do not depend on the reset structures.

vector.datap is a kernel memory pointer that needs to be preserved as it
is not a part of the guest vector data.

Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
Signed-off-by: Radim Krčmář <rkrcmar@ventanamicro.com>
Link: https://lore.kernel.org/r/20250403112522.1566629-4-rkrcmar@ventanamicro.com
Signed-off-by: Anup Patel <anup@brainfault.org>
arch/riscv/include/asm/kvm_vcpu_vector.h
arch/riscv/kvm/vcpu.c
arch/riscv/kvm/vcpu_vector.c

index 27f5bccdd8b02faf9e97cd225b89f3ee84f56065..57a798a4cb0d7d57de99462c7d12e5d5dc7f1306 100644 (file)
@@ -33,8 +33,7 @@ void kvm_riscv_vcpu_guest_vector_restore(struct kvm_cpu_context *cntx,
                                         unsigned long *isa);
 void kvm_riscv_vcpu_host_vector_save(struct kvm_cpu_context *cntx);
 void kvm_riscv_vcpu_host_vector_restore(struct kvm_cpu_context *cntx);
-int kvm_riscv_vcpu_alloc_vector_context(struct kvm_vcpu *vcpu,
-                                       struct kvm_cpu_context *cntx);
+int kvm_riscv_vcpu_alloc_vector_context(struct kvm_vcpu *vcpu);
 void kvm_riscv_vcpu_free_vector_context(struct kvm_vcpu *vcpu);
 #else
 
@@ -62,8 +61,7 @@ static inline void kvm_riscv_vcpu_host_vector_restore(struct kvm_cpu_context *cn
 {
 }
 
-static inline int kvm_riscv_vcpu_alloc_vector_context(struct kvm_vcpu *vcpu,
-                                                     struct kvm_cpu_context *cntx)
+static inline int kvm_riscv_vcpu_alloc_vector_context(struct kvm_vcpu *vcpu)
 {
        return 0;
 }
index 02635bac91f175989fa7a4430542483816d461f9..11b0df66034ffb18b09088c012b36143606436d5 100644 (file)
@@ -57,6 +57,7 @@ static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu)
        struct kvm_vcpu_csr *reset_csr = &vcpu->arch.guest_reset_csr;
        struct kvm_cpu_context *cntx = &vcpu->arch.guest_context;
        struct kvm_cpu_context *reset_cntx = &vcpu->arch.guest_reset_context;
+       void *vector_datap = cntx->vector.datap;
        bool loaded;
 
        /**
@@ -81,6 +82,8 @@ static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu)
 
        kvm_riscv_vcpu_fp_reset(vcpu);
 
+       /* Restore datap as it's not a part of the guest context. */
+       cntx->vector.datap = vector_datap;
        kvm_riscv_vcpu_vector_reset(vcpu);
 
        kvm_riscv_vcpu_timer_reset(vcpu);
@@ -145,7 +148,7 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
        cntx->hstatus |= HSTATUS_SPV;
        spin_unlock(&vcpu->arch.reset_cntx_lock);
 
-       if (kvm_riscv_vcpu_alloc_vector_context(vcpu, cntx))
+       if (kvm_riscv_vcpu_alloc_vector_context(vcpu))
                return -ENOMEM;
 
        /* By default, make CY, TM, and IR counters accessible in VU mode */
index d92d1348045c8cfc60ddd7b6d524f8db535e4619..a5f88cb717f3df3f5911815edf1185ba3d251432 100644 (file)
@@ -22,6 +22,9 @@ void kvm_riscv_vcpu_vector_reset(struct kvm_vcpu *vcpu)
        struct kvm_cpu_context *cntx = &vcpu->arch.guest_context;
 
        cntx->sstatus &= ~SR_VS;
+
+       cntx->vector.vlenb = riscv_v_vsize / 32;
+
        if (riscv_isa_extension_available(isa, v)) {
                cntx->sstatus |= SR_VS_INITIAL;
                WARN_ON(!cntx->vector.datap);
@@ -70,13 +73,11 @@ void kvm_riscv_vcpu_host_vector_restore(struct kvm_cpu_context *cntx)
                __kvm_riscv_vector_restore(cntx);
 }
 
-int kvm_riscv_vcpu_alloc_vector_context(struct kvm_vcpu *vcpu,
-                                       struct kvm_cpu_context *cntx)
+int kvm_riscv_vcpu_alloc_vector_context(struct kvm_vcpu *vcpu)
 {
-       cntx->vector.datap = kmalloc(riscv_v_vsize, GFP_KERNEL);
-       if (!cntx->vector.datap)
+       vcpu->arch.guest_context.vector.datap = kzalloc(riscv_v_vsize, GFP_KERNEL);
+       if (!vcpu->arch.guest_context.vector.datap)
                return -ENOMEM;
-       cntx->vector.vlenb = riscv_v_vsize / 32;
 
        vcpu->arch.host_context.vector.datap = kzalloc(riscv_v_vsize, GFP_KERNEL);
        if (!vcpu->arch.host_context.vector.datap)
@@ -87,7 +88,7 @@ int kvm_riscv_vcpu_alloc_vector_context(struct kvm_vcpu *vcpu,
 
 void kvm_riscv_vcpu_free_vector_context(struct kvm_vcpu *vcpu)
 {
-       kfree(vcpu->arch.guest_reset_context.vector.datap);
+       kfree(vcpu->arch.guest_context.vector.datap);
        kfree(vcpu->arch.host_context.vector.datap);
 }
 #endif