[PATCH] riscv: kvm: add null pointer check for vector datap

rom.wang posted 1 patch 2 weeks, 6 days ago
arch/riscv/kvm/vcpu_vector.c | 1 +
1 file changed, 1 insertion(+)
[PATCH] riscv: kvm: add null pointer check for vector datap
Posted by rom.wang 2 weeks, 6 days ago
From: Yufeng Wang <wangyufeng@kylinos.cn>

Add WARN_ON check before accessing cntx->vector.datap in
kvm_riscv_vcpu_vreg_addr() to detect potential null pointer
dereferences early, consistent with the pattern used in
kvm_riscv_vcpu_vector_reset().

This helps catch initialization issues where vector context
allocation may have failed.

Signed-off-by: Yufeng Wang <wangyufeng@kylinos.cn>
---
 arch/riscv/kvm/vcpu_vector.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/riscv/kvm/vcpu_vector.c b/arch/riscv/kvm/vcpu_vector.c
index 46fbf48f2503..e989cb9f1ac4 100644
--- a/arch/riscv/kvm/vcpu_vector.c
+++ b/arch/riscv/kvm/vcpu_vector.c
@@ -129,6 +129,7 @@ static int kvm_riscv_vcpu_vreg_addr(struct kvm_vcpu *vcpu,
 	} else if (reg_num <= KVM_REG_RISCV_VECTOR_REG(31)) {
 		if (reg_size != vlenb)
 			return -EINVAL;
+		WARN_ON(!cntx->vector.datap);
 		*reg_addr = cntx->vector.datap +
 			    (reg_num - KVM_REG_RISCV_VECTOR_REG(0)) * vlenb;
 	} else {
-- 
2.34.1
Re: [PATCH] riscv: kvm: add null pointer check for vector datap
Posted by Anup Patel 1 week ago
On Tue, Mar 17, 2026 at 5:18 PM rom.wang <r4o5m6e8o@163.com> wrote:
>
> From: Yufeng Wang <wangyufeng@kylinos.cn>
>
> Add WARN_ON check before accessing cntx->vector.datap in
> kvm_riscv_vcpu_vreg_addr() to detect potential null pointer
> dereferences early, consistent with the pattern used in
> kvm_riscv_vcpu_vector_reset().
>
> This helps catch initialization issues where vector context
> allocation may have failed.
>
> Signed-off-by: Yufeng Wang <wangyufeng@kylinos.cn>

LGTM.

Reviewed-by: Anup Patel <anup@brainfault.org>

Queued this patch for Linux-7.1

Thanks,
Anup


> ---
>  arch/riscv/kvm/vcpu_vector.c | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/arch/riscv/kvm/vcpu_vector.c b/arch/riscv/kvm/vcpu_vector.c
> index 46fbf48f2503..e989cb9f1ac4 100644
> --- a/arch/riscv/kvm/vcpu_vector.c
> +++ b/arch/riscv/kvm/vcpu_vector.c
> @@ -129,6 +129,7 @@ static int kvm_riscv_vcpu_vreg_addr(struct kvm_vcpu *vcpu,
>         } else if (reg_num <= KVM_REG_RISCV_VECTOR_REG(31)) {
>                 if (reg_size != vlenb)
>                         return -EINVAL;
> +               WARN_ON(!cntx->vector.datap);
>                 *reg_addr = cntx->vector.datap +
>                             (reg_num - KVM_REG_RISCV_VECTOR_REG(0)) * vlenb;
>         } else {
> --
> 2.34.1
>