[PATCH] LoongArch: KVM: Return the actual error code in kvm_ipi_create()

Chaithanya Lagisetty posted 1 patch 2 days, 4 hours ago
There is a newer version of this series
arch/loongarch/kvm/intc/ipi.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] LoongArch: KVM: Return the actual error code in kvm_ipi_create()
Posted by Chaithanya Lagisetty 2 days, 4 hours ago
kvm_ipi_create() registers the IPI device on the IOCSR bus and jumps to
the error label when that fails. The error path then discards the error
code that kvm_io_bus_register_dev() reported and returns -EFAULT
instead:

	ret = kvm_io_bus_register_dev(kvm, KVM_IOCSR_BUS, IOCSR_IPI_BASE,
				      IOCSR_IPI_SIZE, device);
	mutex_unlock(&kvm->slots_lock);
	if (ret < 0) {
		kvm_pr_unimpl("%s: Initialize IOCSR dev failed, ret = %d\n",
			      __func__, ret);
		goto err;
	}
	...
err:
	kfree(s);
	return -EFAULT;

kvm_io_bus_register_dev() fails with -ENOMEM or -ENOSPC, so userspace
creating a KVM_DEV_TYPE_LOONGARCH_IPI device is told that it passed a
bad address when the real problem is that the host is out of memory or
out of IOCSR bus slots. -EFAULT is reserved for faulting user addresses,
and the remaining -EFAULT returns in this file are get_user() failures,
where it is correct.

Return ret instead. It is guaranteed to hold a valid negative errno
because the ret < 0 branch above is the only path that reaches the error
label. This also matches kvm_eiointc_create(), which already propagates
the kvm_io_bus_register_dev() error code unchanged.

Only compile-tested; the fix is a straight propagation of an error code
that the adjacent kvm_pr_unimpl() already logs.

Fixes: c532de5a67a7 ("LoongArch: KVM: Add IPI device support")
Assisted-by: Cursor:claude-opus-5
Signed-off-by: Chaithanya Lagisetty <nagachaithanya9911@gmail.com>
---
 arch/loongarch/kvm/intc/ipi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/loongarch/kvm/intc/ipi.c b/arch/loongarch/kvm/intc/ipi.c
index 7b333a4a0430..672ec053a997 100644
--- a/arch/loongarch/kvm/intc/ipi.c
+++ b/arch/loongarch/kvm/intc/ipi.c
@@ -431,7 +431,7 @@ static int kvm_ipi_create(struct kvm_device *dev, u32 type)
 
 err:
 	kfree(s);
-	return -EFAULT;
+	return ret;
 }
 
 static void kvm_ipi_destroy(struct kvm_device *dev)
-- 
2.43.0
Re: [PATCH] LoongArch: KVM: Return the actual error code in kvm_ipi_create()
Posted by Huacai Chen 1 day, 10 hours ago
Hi, Chaithanya

On Tue, Sep 22, 2026 at 6:39 PM Chaithanya Lagisetty
<nagachaithanya9911@gmail.com> wrote:
>
> kvm_ipi_create() registers the IPI device on the IOCSR bus and jumps to
> the error label when that fails. The error path then discards the error
> code that kvm_io_bus_register_dev() reported and returns -EFAULT
> instead:
>
>         ret = kvm_io_bus_register_dev(kvm, KVM_IOCSR_BUS, IOCSR_IPI_BASE,
>                                       IOCSR_IPI_SIZE, device);
>         mutex_unlock(&kvm->slots_lock);
>         if (ret < 0) {
>                 kvm_pr_unimpl("%s: Initialize IOCSR dev failed, ret = %d\n",
>                               __func__, ret);
>                 goto err;
>         }
>         ...
> err:
>         kfree(s);
>         return -EFAULT;
>
> kvm_io_bus_register_dev() fails with -ENOMEM or -ENOSPC, so userspace
> creating a KVM_DEV_TYPE_LOONGARCH_IPI device is told that it passed a
> bad address when the real problem is that the host is out of memory or
> out of IOCSR bus slots. -EFAULT is reserved for faulting user addresses,
> and the remaining -EFAULT returns in this file are get_user() failures,
> where it is correct.
>
> Return ret instead. It is guaranteed to hold a valid negative errno
> because the ret < 0 branch above is the only path that reaches the error
> label. This also matches kvm_eiointc_create(), which already propagates
> the kvm_io_bus_register_dev() error code unchanged.
>
> Only compile-tested; the fix is a straight propagation of an error code
> that the adjacent kvm_pr_unimpl() already logs.
>
> Fixes: c532de5a67a7 ("LoongArch: KVM: Add IPI device support")
> Assisted-by: Cursor:claude-opus-5
> Signed-off-by: Chaithanya Lagisetty <nagachaithanya9911@gmail.com>
> ---
>  arch/loongarch/kvm/intc/ipi.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/arch/loongarch/kvm/intc/ipi.c b/arch/loongarch/kvm/intc/ipi.c
> index 7b333a4a0430..672ec053a997 100644
> --- a/arch/loongarch/kvm/intc/ipi.c
> +++ b/arch/loongarch/kvm/intc/ipi.c
> @@ -431,7 +431,7 @@ static int kvm_ipi_create(struct kvm_device *dev, u32 type)
>
>  err:
>         kfree(s);
> -       return -EFAULT;
> +       return ret;
Why not remove the "err" label and return at the "goto" statement?

Huacai

>  }
>
>  static void kvm_ipi_destroy(struct kvm_device *dev)
> --
> 2.43.0
>
Re: [PATCH] LoongArch: KVM: Return the actual error code in kvm_ipi_create()
Posted by Bibo Mao 1 day, 11 hours ago

On 2026/9/22 下午6:39, Chaithanya Lagisetty wrote:
> kvm_ipi_create() registers the IPI device on the IOCSR bus and jumps to
> the error label when that fails. The error path then discards the error
> code that kvm_io_bus_register_dev() reported and returns -EFAULT
> instead:
> 
> 	ret = kvm_io_bus_register_dev(kvm, KVM_IOCSR_BUS, IOCSR_IPI_BASE,
> 				      IOCSR_IPI_SIZE, device);
> 	mutex_unlock(&kvm->slots_lock);
> 	if (ret < 0) {
> 		kvm_pr_unimpl("%s: Initialize IOCSR dev failed, ret = %d\n",
> 			      __func__, ret);
> 		goto err;
> 	}
> 	...
> err:
> 	kfree(s);
> 	return -EFAULT;
> 
> kvm_io_bus_register_dev() fails with -ENOMEM or -ENOSPC, so userspace
> creating a KVM_DEV_TYPE_LOONGARCH_IPI device is told that it passed a
> bad address when the real problem is that the host is out of memory or
> out of IOCSR bus slots. -EFAULT is reserved for faulting user addresses,
> and the remaining -EFAULT returns in this file are get_user() failures,
> where it is correct.
> 
> Return ret instead. It is guaranteed to hold a valid negative errno
> because the ret < 0 branch above is the only path that reaches the error
> label. This also matches kvm_eiointc_create(), which already propagates
> the kvm_io_bus_register_dev() error code unchanged.
> 
> Only compile-tested; the fix is a straight propagation of an error code
> that the adjacent kvm_pr_unimpl() already logs.
> 
> Fixes: c532de5a67a7 ("LoongArch: KVM: Add IPI device support")
> Assisted-by: Cursor:claude-opus-5
> Signed-off-by: Chaithanya Lagisetty <nagachaithanya9911@gmail.com>
> ---
>   arch/loongarch/kvm/intc/ipi.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/arch/loongarch/kvm/intc/ipi.c b/arch/loongarch/kvm/intc/ipi.c
> index 7b333a4a0430..672ec053a997 100644
> --- a/arch/loongarch/kvm/intc/ipi.c
> +++ b/arch/loongarch/kvm/intc/ipi.c
> @@ -431,7 +431,7 @@ static int kvm_ipi_create(struct kvm_device *dev, u32 type)
>   
>   err:
>   	kfree(s);
> -	return -EFAULT;
> +	return ret;
>   }
>   
>   static void kvm_ipi_destroy(struct kvm_device *dev)
> 
Reviewed-by: Bibo Mao <maobibo@loongson.cn>

[PATCH v2] LoongArch: KVM: Return the actual error code in kvm_ipi_create()
Posted by Chaithanya Lagisetty 8 hours ago
kvm_ipi_create() registers the IPI device on the IOCSR bus. The error
path currently discards the error code returned by
kvm_io_bus_register_dev() and returns -EFAULT instead:

	ret = kvm_io_bus_register_dev(kvm, KVM_IOCSR_BUS, IOCSR_IPI_BASE,
				      IOCSR_IPI_SIZE, device);
	mutex_unlock(&kvm->slots_lock);
	if (ret < 0) {
		kvm_pr_unimpl("%s: Initialize IOCSR dev failed, ret = %d\n",
			      __func__, ret);
		goto err;
	}
	...
err:
	kfree(s);
	return -EFAULT;

kvm_io_bus_register_dev() fails with -ENOMEM or -ENOSPC, so userspace
creating a KVM_DEV_TYPE_LOONGARCH_IPI device is told that it passed a
bad address when the real problem is that the host is out of memory or
out of IOCSR bus slots. -EFAULT is reserved for faulting user addresses,
and the remaining -EFAULT returns in this file are get_user() failures,
where it is correct.

Free the IPI state and propagate the error directly after cleanup. The
error label had a single user, so handling the failure in place removes
the label as well. This matches kvm_eiointc_create(), which already frees
its state and propagates the kvm_io_bus_register_dev() error code
unchanged.

Only compile-tested.

Fixes: c532de5a67a7 ("LoongArch: KVM: Add IPI device support")
Suggested-by: Huacai Chen <chenhuacai@kernel.org>
Assisted-by: Cursor:claude-opus-5
Signed-off-by: Chaithanya Lagisetty <nagachaithanya9911@gmail.com>
---
v2:
 - Handle the failure in place and drop the now unused err label instead
   of only correcting the returned value (Huacai Chen).
 - Reworded the commit message accordingly.
 - Dropped Bibo Mao's Reviewed-by from v1, as the error path is
   restructured in this version rather than only returning ret.

Link to v1:
https://lore.kernel.org/all/20260922103931.215091-1-nagachaithanya9911@gmail.com/

 arch/loongarch/kvm/intc/ipi.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/arch/loongarch/kvm/intc/ipi.c b/arch/loongarch/kvm/intc/ipi.c
index 7b333a4a0430..80faa770c0cd 100644
--- a/arch/loongarch/kvm/intc/ipi.c
+++ b/arch/loongarch/kvm/intc/ipi.c
@@ -423,15 +423,12 @@ static int kvm_ipi_create(struct kvm_device *dev, u32 type)
 	mutex_unlock(&kvm->slots_lock);
 	if (ret < 0) {
 		kvm_pr_unimpl("%s: Initialize IOCSR dev failed, ret = %d\n", __func__, ret);
-		goto err;
+		kfree(s);
+		return ret;
 	}
 
 	kvm->arch.ipi = s;
 	return 0;
-
-err:
-	kfree(s);
-	return -EFAULT;
 }
 
 static void kvm_ipi_destroy(struct kvm_device *dev)
-- 
2.43.0