[PATCH v2] LoongArch: KVM: Use kvm_get_vcpu_by_id() instead of kvm_get_vcpu()

Song Gao posted 1 patch 1 month, 2 weeks ago
arch/loongarch/kvm/intc/eiointc.c | 6 +++++-
arch/loongarch/kvm/intc/ipi.c     | 2 +-
2 files changed, 6 insertions(+), 2 deletions(-)
[PATCH v2] LoongArch: KVM: Use kvm_get_vcpu_by_id() instead of kvm_get_vcpu()
Posted by Song Gao 1 month, 2 weeks ago
Since using kvm_get_vcpu() may fail to retrieve the vcpu context,
kvm_get_vcpu_by_id() should be used instead.

Fixes: 8e3054261bc3 ("LoongArch: KVM: Add IPI user mode read and write function")
Fixes: 3956a52bc05b ("LoongArch: KVM: Add EIOINTC read and write functions")
Reviewed-by: Yanteng Si <siyanteng@cqsoftware.com.cm>
Signed-off-by: Song Gao <gaosong@loongson.cn>
---
 arch/loongarch/kvm/intc/eiointc.c | 6 +++++-
 arch/loongarch/kvm/intc/ipi.c     | 2 +-
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/arch/loongarch/kvm/intc/eiointc.c b/arch/loongarch/kvm/intc/eiointc.c
index a3a12af9ecbf..b3829fb4f64e 100644
--- a/arch/loongarch/kvm/intc/eiointc.c
+++ b/arch/loongarch/kvm/intc/eiointc.c
@@ -45,7 +45,11 @@ static void eiointc_update_irq(struct loongarch_eiointc *s, int irq, int level)
 	}
 
 	cpu = s->sw_coremap[irq];
-	vcpu = kvm_get_vcpu(s->kvm, cpu);
+	vcpu = kvm_get_vcpu_by_id(s->kvm, cpu);
+	if (unlikely(vcpu == NULL)) {
+		kvm_err("%s: invalid target cpu: %d\n", __func__, cpu);
+		return;
+	}
 	if (level) {
 		/* if not enable return false */
 		if (!test_bit(irq, (unsigned long *)s->enable.reg_u32))
diff --git a/arch/loongarch/kvm/intc/ipi.c b/arch/loongarch/kvm/intc/ipi.c
index e658d5b37c04..0348a83a7ed7 100644
--- a/arch/loongarch/kvm/intc/ipi.c
+++ b/arch/loongarch/kvm/intc/ipi.c
@@ -298,7 +298,7 @@ static int kvm_ipi_regs_access(struct kvm_device *dev,
 	cpu = (attr->attr >> 16) & 0x3ff;
 	addr = attr->attr & 0xff;
 
-	vcpu = kvm_get_vcpu(dev->kvm, cpu);
+	vcpu = kvm_get_vcpu_by_id(dev->kvm, cpu);
 	if (unlikely(vcpu == NULL)) {
 		kvm_err("%s: invalid target cpu: %d\n", __func__, cpu);
 		return -EINVAL;
-- 
2.39.3
Re: [PATCH v2] LoongArch: KVM: Use kvm_get_vcpu_by_id() instead of kvm_get_vcpu()
Posted by Huacai Chen 1 month, 1 week ago
Applied, thanks.

Huacai

On Wed, Aug 20, 2025 at 11:45 AM Song Gao <gaosong@loongson.cn> wrote:
>
> Since using kvm_get_vcpu() may fail to retrieve the vcpu context,
> kvm_get_vcpu_by_id() should be used instead.
>
> Fixes: 8e3054261bc3 ("LoongArch: KVM: Add IPI user mode read and write function")
> Fixes: 3956a52bc05b ("LoongArch: KVM: Add EIOINTC read and write functions")
> Reviewed-by: Yanteng Si <siyanteng@cqsoftware.com.cm>
> Signed-off-by: Song Gao <gaosong@loongson.cn>
> ---
>  arch/loongarch/kvm/intc/eiointc.c | 6 +++++-
>  arch/loongarch/kvm/intc/ipi.c     | 2 +-
>  2 files changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/arch/loongarch/kvm/intc/eiointc.c b/arch/loongarch/kvm/intc/eiointc.c
> index a3a12af9ecbf..b3829fb4f64e 100644
> --- a/arch/loongarch/kvm/intc/eiointc.c
> +++ b/arch/loongarch/kvm/intc/eiointc.c
> @@ -45,7 +45,11 @@ static void eiointc_update_irq(struct loongarch_eiointc *s, int irq, int level)
>         }
>
>         cpu = s->sw_coremap[irq];
> -       vcpu = kvm_get_vcpu(s->kvm, cpu);
> +       vcpu = kvm_get_vcpu_by_id(s->kvm, cpu);
> +       if (unlikely(vcpu == NULL)) {
> +               kvm_err("%s: invalid target cpu: %d\n", __func__, cpu);
> +               return;
> +       }
>         if (level) {
>                 /* if not enable return false */
>                 if (!test_bit(irq, (unsigned long *)s->enable.reg_u32))
> diff --git a/arch/loongarch/kvm/intc/ipi.c b/arch/loongarch/kvm/intc/ipi.c
> index e658d5b37c04..0348a83a7ed7 100644
> --- a/arch/loongarch/kvm/intc/ipi.c
> +++ b/arch/loongarch/kvm/intc/ipi.c
> @@ -298,7 +298,7 @@ static int kvm_ipi_regs_access(struct kvm_device *dev,
>         cpu = (attr->attr >> 16) & 0x3ff;
>         addr = attr->attr & 0xff;
>
> -       vcpu = kvm_get_vcpu(dev->kvm, cpu);
> +       vcpu = kvm_get_vcpu_by_id(dev->kvm, cpu);
>         if (unlikely(vcpu == NULL)) {
>                 kvm_err("%s: invalid target cpu: %d\n", __func__, cpu);
>                 return -EINVAL;
> --
> 2.39.3
>