Skip to contentSkip to conversation

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

PATCH Superseded Chaithanya Lagisetty 3 participants 5 messages Sep 22 – Oct 3

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

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.

Fixes c532de5a67a7 ("LoongArch: KVM: Add IPI device support")
Assisted-by Cursor:claude-opus-5
---
 arch/loongarch/kvm/intc/ipi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/loongarch/kvm/intc/ipi.c +1 -1
index 7b333a4a0430..672ec053a997 100644
@@ -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
maintainer Reviewed-by on v1 1/1
#1 lore raw
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 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
---
  arch/loongarch/kvm/intc/ipi.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
arch/loongarch/kvm/intc/ipi.c +0 -0
index 7b333a4a0430..672ec053a997 100644

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)
maintainer
#2 lore raw
In reply to Chaithanya Lagisetty

Hi, 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 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
---
 arch/loongarch/kvm/intc/ipi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/loongarch/kvm/intc/ipi.c +0 -0
index 7b333a4a0430..672ec053a997 100644

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

#3 lore raw

PATCH v2 1/1 LoongArch: KVM: Return the actual error code in kvm_ipi_create()

In reply to Chaithanya Lagisetty

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")
Assisted-by Cursor:claude-opus-5

---
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
index 7b333a4a0430..80faa770c0cd 100644
@@ -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
maintainer Applied
#4 lore raw

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 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")
Assisted-by Cursor:claude-opus-5

---
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
index 7b333a4a0430..80faa770c0cd 100644
@@ -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