KVM: RISC-V: reset smstateen CSRs
Subsystems (3)
PATCH 1/1 KVM: RISC-V: reset smstateen CSRs
Hi, I'm sending this early to ventana-sw as we hit the issue in today's slack discussion. I only compile-tested it so far and it will take me a while to trigger a bug and verify the solution.
---8<--
The smstateen CSRs control which stateful features are enabled in VU-mode. SU-mode must properly context switch the state of all enabled features.
Reset the smstateen CSRs, because SU-mode might not know that it must context switch the state. Reset unconditionally as it is shorter and safer, and not that much slower.
--- arch/riscv/include/asm/kvm_host.h | 3 +++ arch/riscv/kvm/vcpu.c | 4 ++++ 2 files changed, 7 insertions(+)
arch/riscv/include/asm/kvm_host.h +3 -0
@@ -234,6 +234,9 @@ struct kvm_vcpu_arch {/* CPU CSR context upon Guest VCPU reset */ struct kvm_vcpu_csr guest_reset_csr; +/* CPU smstateen CSR context upon Guest VCPU reset */ +struct kvm_vcpu_smstateen_csr reset_smstateen_csr; +/* * VCPU interrupts *
arch/riscv/kvm/vcpu.c +4 -0
@@ -57,6 +57,8 @@ 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; +struct kvm_vcpu_smstateen_csr *smstateen_csr = &vcpu->arch.smstateen_csr; +struct kvm_vcpu_smstateen_csr *reset_smstateen_csr = &vcpu->arch.reset_smstateen_csr;bool loaded;/**
@@ -73,6 +75,8 @@ static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu)memcpy (csr, reset_csr,sizeof (*csr)); +memcpy (smstateen_csr, reset_smstateen_csr,sizeof (*smstateen_csr)); +spin_lock (&vcpu->arch.reset_cntx_lock);memcpy (cntx, reset_cntx,sizeof (*cntx));spin_unlock (&vcpu->arch.reset_cntx_lock);
Signature · 1 line
2.48.1
Quoted 10 lines · Radim Krčmář
Hi, I'm sending this early to ventana-sw as we hit the issue in today's slack discussion. I only compile-tested it so far and it will take me a while to trigger a bug and verify the solution.
---8<--
The smstateen CSRs control which stateful features are enabled in VU-mode. SU-mode must properly context switch the state of all enabled features.
Quoted 7 lines
Reset the smstateen CSRs, because SU-mode might not know that it must context switch the state. Reset unconditionally as it is shorter and safer, and not that much slower.
Fixes 81f0f314fec9 ("RISCV: KVM: Add sstateen0 context save/restore")Signed-off-by Radim Krčmář <[email protected]>
How about moving "struct kvm_vcpu_smstateen_csr smstateen" from "struct kvm_vcpu_arch" to "struct kvm_vcpu_csr". This way we will not need an extra "struct kvm_vcpu_smstateen_csr reset_smstateen_csr" in "struct kvm_vcpu_csr".
Regards,
Anup
Quoted 44 lines
--- arch/riscv/include/asm/kvm_host.h | 3 +++ arch/riscv/kvm/vcpu.c | 4 ++++ 2 files changed, 7 insertions(+)arch/riscv/include/asm/kvm_host.h +3
-0@@ -234,6 +234,9 @@ struct kvm_vcpu_arch {/* CPU CSR context upon Guest VCPU reset */ struct kvm_vcpu_csr guest_reset_csr; +/* CPU smstateen CSR context upon Guest VCPU reset */ +struct kvm_vcpu_smstateen_csr reset_smstateen_csr; +/* * VCPU interrupts * arch/riscv/kvm/vcpu.c +4
-1@@ -57,6 +57,8 @@ 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; +struct kvm_vcpu_smstateen_csr *smstateen_csr = &vcpu->arch.smstateen_csr; +struct kvm_vcpu_smstateen_csr *reset_smstateen_csr = &vcpu->arch.reset_smstateen_csr;bool loaded;/** @@ -73,6 +75,8 @@ static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu)memcpy (csr, reset_csr,sizeof (*csr)); +memcpy (smstateen_csr, reset_smstateen_csr,sizeof (*smstateen_csr)); +spin_lock (&vcpu->arch.reset_cntx_lock);memcpy (cntx, reset_cntx,sizeof (*cntx));spin_unlock (&vcpu->arch.reset_cntx_lock); --2.48.1
Quoted 16 lines · Anup Patel
Quoted 14 lines · Radim Krčmář
Hi, I'm sending this early to ventana-sw as we hit the issue in today's slack discussion. I only compile-tested it so far and it will take me a while to trigger a bug and verify the solution.
---8<--
The smstateen CSRs control which stateful features are enabled in VU-mode. SU-mode must properly context switch the state of all enabled features.Reset the smstateen CSRs, because SU-mode might not know that it must context switch the state. Reset unconditionally as it is shorter and safer, and not that much slower.
Quoted 8 lines
Quoted 3 lines
Fixes 81f0f314fec9 ("RISCV: KVM: Add sstateen0 context save/restore")Signed-off-by Radim Krčmář <[email protected]>How about moving "struct
kvm_vcpu_smstateen_csrsmstateen" from "structkvm_vcpu_arch" to "structkvm_vcpu_csr". This way we will not need an extra "structkvm_vcpu_smstateen_csrreset_smstateen_csr" in "structkvm_vcpu_csr".
Other than my comment, this looks good for upstreaming.
Thanks,
Anup
Quoted 48 lines
Regards,
AnupQuoted 44 lines
--- arch/riscv/include/asm/kvm_host.h | 3 +++ arch/riscv/kvm/vcpu.c | 4 ++++ 2 files changed, 7 insertions(+)arch/riscv/include/asm/kvm_host.h +3
-0@@ -234,6 +234,9 @@ struct kvm_vcpu_arch {/* CPU CSR context upon Guest VCPU reset */ struct kvm_vcpu_csr guest_reset_csr; +/* CPU smstateen CSR context upon Guest VCPU reset */ +struct kvm_vcpu_smstateen_csr reset_smstateen_csr; +/* * VCPU interrupts * arch/riscv/kvm/vcpu.c +4
-1@@ -57,6 +57,8 @@ 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; +struct kvm_vcpu_smstateen_csr *smstateen_csr = &vcpu->arch.smstateen_csr; +struct kvm_vcpu_smstateen_csr *reset_smstateen_csr = &vcpu->arch.reset_smstateen_csr;bool loaded;/** @@ -73,6 +75,8 @@ static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu)memcpy (csr, reset_csr,sizeof (*csr)); +memcpy (smstateen_csr, reset_smstateen_csr,sizeof (*smstateen_csr)); +spin_lock (&vcpu->arch.reset_cntx_lock);memcpy (cntx, reset_cntx,sizeof (*cntx));spin_unlock (&vcpu->arch.reset_cntx_lock); --2.48.1
2025-03-25T22:27:41+05:30, Anup Patel <[email protected]>:
Quoted 17 lines · Anup Patel
Quoted 16 lines · Radim Krčmář
Quoted 14 lines
Hi, I'm sending this early to ventana-sw as we hit the issue in today's slack discussion. I only compile-tested it so far and it will take me a while to trigger a bug and verify the solution.
---8<--
The smstateen CSRs control which stateful features are enabled in VU-mode. SU-mode must properly context switch the state of all enabled features.Reset the smstateen CSRs, because SU-mode might not know that it must context switch the state. Reset unconditionally as it is shorter and safer, and not that much slower.
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
Fixes 81f0f314fec9 ("RISCV: KVM: Add sstateen0 context save/restore")Signed-off-by Radim Krčmář <[email protected]>How about moving "struct
kvm_vcpu_smstateen_csrsmstateen" from "structkvm_vcpu_arch" to "structkvm_vcpu_csr". This way we will not need an extra "structkvm_vcpu_smstateen_csrreset_smstateen_csr" in "structkvm_vcpu_csr".
It is tricky, because kvm_riscv_vcpu_general_set_csr calculates the amount of registers accessible to userspace based the on size of kvm_vcpu_csr.
We'd have to make changes to logic before expanding kvm_vcpu_csr with kvm_vcpu_smstateen_csr. At that point, I think it be much easier to just put all csrs to kvm_vcpu_csr directly, which would also simplify future extensions.
Quoted 1 line
Other than my comment, this looks good for upstreaming.
I think the current version is more appropriate for stable, and we can implement your suggestion afterwards.
Thanks.
Quoted 37 lines · Radim Krčmář
2025-03-25T22:27:41+05:30, Anup Patel <[email protected]>:Quoted 17 lines · Anup Patel
Quoted 16 lines · Radim Krčmář
Quoted 14 lines
Hi, I'm sending this early to ventana-sw as we hit the issue in today's slack discussion. I only compile-tested it so far and it will take me a while to trigger a bug and verify the solution.
---8<--
The smstateen CSRs control which stateful features are enabled in VU-mode. SU-mode must properly context switch the state of all enabled features.Reset the smstateen CSRs, because SU-mode might not know that it must context switch the state. Reset unconditionally as it is shorter and safer, and not that much slower.
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
Fixes 81f0f314fec9 ("RISCV: KVM: Add sstateen0 context save/restore")Signed-off-by Radim Krčmář <[email protected]>How about moving "struct
kvm_vcpu_smstateen_csrsmstateen" from "structkvm_vcpu_arch" to "structkvm_vcpu_csr". This way we will not need an extra "structkvm_vcpu_smstateen_csrreset_smstateen_csr" in "structkvm_vcpu_csr".It is tricky, because
kvm_riscv_vcpu_general_set_csrcalculates the amount of registers accessible to userspace based the on size ofkvm_vcpu_csr.We'd have to make changes to logic before expanding
kvm_vcpu_csrwithkvm_vcpu_smstateen_csr. At that point, I think it be much easier to just put all csrs tokvm_vcpu_csrdirectly, which would also simplify future extensions.
Quoted 4 lines
Quoted 1 line
Other than my comment, this looks good for upstreaming.
I think the current version is more appropriate for stable, and we can implement your suggestion afterwards.
Sounds good.
Regards,
Anup