Skip to content

Commit 9ffecf0

Browse files
radimkrcmaravpatel
authored andcommitted
KVM: RISC-V: refactor vector state reset
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>
1 parent a29c19e commit 9ffecf0

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

arch/riscv/include/asm/kvm_vcpu_vector.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ void kvm_riscv_vcpu_guest_vector_restore(struct kvm_cpu_context *cntx,
3333
unsigned long *isa);
3434
void kvm_riscv_vcpu_host_vector_save(struct kvm_cpu_context *cntx);
3535
void kvm_riscv_vcpu_host_vector_restore(struct kvm_cpu_context *cntx);
36-
int kvm_riscv_vcpu_alloc_vector_context(struct kvm_vcpu *vcpu,
37-
struct kvm_cpu_context *cntx);
36+
int kvm_riscv_vcpu_alloc_vector_context(struct kvm_vcpu *vcpu);
3837
void kvm_riscv_vcpu_free_vector_context(struct kvm_vcpu *vcpu);
3938
#else
4039

@@ -62,8 +61,7 @@ static inline void kvm_riscv_vcpu_host_vector_restore(struct kvm_cpu_context *cn
6261
{
6362
}
6463

65-
static inline int kvm_riscv_vcpu_alloc_vector_context(struct kvm_vcpu *vcpu,
66-
struct kvm_cpu_context *cntx)
64+
static inline int kvm_riscv_vcpu_alloc_vector_context(struct kvm_vcpu *vcpu)
6765
{
6866
return 0;
6967
}

arch/riscv/kvm/vcpu.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu)
5757
struct kvm_vcpu_csr *reset_csr = &vcpu->arch.guest_reset_csr;
5858
struct kvm_cpu_context *cntx = &vcpu->arch.guest_context;
5959
struct kvm_cpu_context *reset_cntx = &vcpu->arch.guest_reset_context;
60+
void *vector_datap = cntx->vector.datap;
6061
bool loaded;
6162

6263
/**
@@ -81,6 +82,8 @@ static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu)
8182

8283
kvm_riscv_vcpu_fp_reset(vcpu);
8384

85+
/* Restore datap as it's not a part of the guest context. */
86+
cntx->vector.datap = vector_datap;
8487
kvm_riscv_vcpu_vector_reset(vcpu);
8588

8689
kvm_riscv_vcpu_timer_reset(vcpu);
@@ -145,7 +148,7 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
145148
cntx->hstatus |= HSTATUS_SPV;
146149
spin_unlock(&vcpu->arch.reset_cntx_lock);
147150

148-
if (kvm_riscv_vcpu_alloc_vector_context(vcpu, cntx))
151+
if (kvm_riscv_vcpu_alloc_vector_context(vcpu))
149152
return -ENOMEM;
150153

151154
/* By default, make CY, TM, and IR counters accessible in VU mode */

arch/riscv/kvm/vcpu_vector.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ void kvm_riscv_vcpu_vector_reset(struct kvm_vcpu *vcpu)
2222
struct kvm_cpu_context *cntx = &vcpu->arch.guest_context;
2323

2424
cntx->sstatus &= ~SR_VS;
25+
26+
cntx->vector.vlenb = riscv_v_vsize / 32;
27+
2528
if (riscv_isa_extension_available(isa, v)) {
2629
cntx->sstatus |= SR_VS_INITIAL;
2730
WARN_ON(!cntx->vector.datap);
@@ -70,13 +73,11 @@ void kvm_riscv_vcpu_host_vector_restore(struct kvm_cpu_context *cntx)
7073
__kvm_riscv_vector_restore(cntx);
7174
}
7275

73-
int kvm_riscv_vcpu_alloc_vector_context(struct kvm_vcpu *vcpu,
74-
struct kvm_cpu_context *cntx)
76+
int kvm_riscv_vcpu_alloc_vector_context(struct kvm_vcpu *vcpu)
7577
{
76-
cntx->vector.datap = kmalloc(riscv_v_vsize, GFP_KERNEL);
77-
if (!cntx->vector.datap)
78+
vcpu->arch.guest_context.vector.datap = kzalloc(riscv_v_vsize, GFP_KERNEL);
79+
if (!vcpu->arch.guest_context.vector.datap)
7880
return -ENOMEM;
79-
cntx->vector.vlenb = riscv_v_vsize / 32;
8081

8182
vcpu->arch.host_context.vector.datap = kzalloc(riscv_v_vsize, GFP_KERNEL);
8283
if (!vcpu->arch.host_context.vector.datap)
@@ -87,7 +88,7 @@ int kvm_riscv_vcpu_alloc_vector_context(struct kvm_vcpu *vcpu,
8788

8889
void kvm_riscv_vcpu_free_vector_context(struct kvm_vcpu *vcpu)
8990
{
90-
kfree(vcpu->arch.guest_reset_context.vector.datap);
91+
kfree(vcpu->arch.guest_context.vector.datap);
9192
kfree(vcpu->arch.host_context.vector.datap);
9293
}
9394
#endif

0 commit comments

Comments
 (0)