LoongArch: KVM: Return the actual error code in kvm_ipi_create()
Subsystems (3)
PATCH 1/1 LoongArch: KVM: Return the actual error code in kvm_ipi_create()
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.
--- arch/loongarch/kvm/intc/ipi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/loongarch/kvm/intc/ipi.c +1 -1
@@ -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)
Signature · 1 line
2.43.0
Quoted 46 lines · Chaithanya Lagisetty
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 thatkvm_io_bus_register_dev()reported and returns-EFAULTinstead: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-ENOMEMor-ENOSPC, so userspace creating aKVM_DEV_TYPE_LOONGARCH_IPIdevice 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.-EFAULTis reserved for faulting user addresses, and the remaining-EFAULTreturns in this file areget_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 thekvm_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-5Signed-off-by Chaithanya Lagisetty <[email protected]>--- arch/loongarch/kvm/intc/ipi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/loongarch/kvm/intc/ipi.c +0
-0
arch/loongarch/kvm/intc/ipi.c static int kvm_ipi_create(struct kvm_device *dev, u32 type) · line 437
Quoted 8 lines
arch/loongarch/kvm/intc/ipi.c +1
-1@@ -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)
Huacai Chen
maintainerHi, Chaithanya
Quoted 46 lines · Chaithanya Lagisetty
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 thatkvm_io_bus_register_dev()reported and returns-EFAULTinstead: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-ENOMEMor-ENOSPC, so userspace creating aKVM_DEV_TYPE_LOONGARCH_IPIdevice 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.-EFAULTis reserved for faulting user addresses, and the remaining-EFAULTreturns in this file areget_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 thekvm_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-5Signed-off-by Chaithanya Lagisetty <[email protected]>--- arch/loongarch/kvm/intc/ipi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/loongarch/kvm/intc/ipi.c +0
-0
arch/loongarch/kvm/intc/ipi.c static int kvm_ipi_create(struct kvm_device *dev, u32 type) · line 434
Quoted 5 lines
arch/loongarch/kvm/intc/ipi.c +1
-1@@ -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
Quoted 6 lines
arch/loongarch/kvm/intc/ipi.c +0
-1}static void kvm_ipi_destroy (struct kvm_device *dev) --2.43.0
PATCH v2 1/1 LoongArch: KVM: Return the actual error code in kvm_ipi_create()
In reply to Chaithanya Lagisettykvm_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.
---
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/[email protected]/
arch/loongarch/kvm/intc/ipi.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
arch/loongarch/kvm/intc/ipi.c +2 -5
@@ -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)
Signature · 1 line
2.43.0
Applied, thanks.
Huacai
Quoted 76 lines · Chaithanya Lagisetty
kvm_ipi_create()registers the IPI device on the IOCSR bus. The error path currently discards the error code returned bykvm_io_bus_register_dev()and returns-EFAULTinstead: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-ENOMEMor-ENOSPC, so userspace creating aKVM_DEV_TYPE_LOONGARCH_IPIdevice 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.-EFAULTis reserved for faulting user addresses, and the remaining-EFAULTreturns in this file areget_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 thekvm_io_bus_register_dev()error code unchanged.Only compile-tested.
Fixes c532de5a67a7 ("LoongArch: KVM: Add IPI device support")Suggested-by Huacai Chen <[email protected]>Assisted-by Cursor:claude-opus-5Signed-off-by Chaithanya Lagisetty <[email protected]>---
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/[email protected]/arch/loongarch/kvm/intc/ipi.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)arch/loongarch/kvm/intc/ipi.c +2
-6@@ -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