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

Chaithanya Lagisetty posted 1 patch 5 hours ago
arch/loongarch/kvm/intc/ipi.c | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
[PATCH v2] LoongArch: KVM: Return the actual error code in kvm_ipi_create()
Posted by Chaithanya Lagisetty 5 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