Skip to contentSkip to conversation

kunit: fix minor error path mistakes

PATCH Superseded Wander Lairson Costa 4 participants 11 messages Apr 18, 2024 – Apr 19, 2024

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover kunit: fix minor error path mistakes

Hi,

These two patches fix some minor error path mistakes in the device module.

Changes
-------

v1->v2
* Add fixes tag
* Add imperative statement in the commit description v2->v3
* Add a goto exit label kunit_device_register_internal

Wander Lairson Costa (2):
kunit: unregister the device on error
kunit: avoid memory leak on device register error

 lib/kunit/device.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
Signature · 1 line
2.44.0
#1 lore raw

PATCH v3 1/2 kunit: unregister the device on error

kunit_init_device() should unregister the device on bus register error, but mistakenly it tries to unregister the bus.

Unregister the device instead of the bus.

Fixes d03c720e03bd ("kunit: Add APIs for managing devices")
---
 lib/kunit/device.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/kunit/device.c +1 -1
index abc603730b8e..25c81ed465fb 100644
@@ -51,7 +51,7 @@ int kunit_bus_init(void)
 
 	error = bus_register(&kunit_bus_type);
 	if (error)
-		bus_unregister(&kunit_bus_type);
+		root_device_unregister(kunit_bus_device);
 	return error;
 }
 
Signature · 1 line
2.44.0
Reviewed-by on v3 1/2
#2 lore raw

Re: [PATCH v3 1/2] kunit: unregister the device on error

Quoted 9 lines · Wander Lairson Costa

kunit_init_device() should unregister the device on bus register error, but mistakenly it tries to unregister the bus.

Unregister the device instead of the bus.

Fixes d03c720e03bd ("kunit: Add APIs for managing devices")

---

Nice catch!

Cheers,
-- David

Quoted 19 lines
 lib/kunit/device.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/kunit/device.c +1 -2
index abc603730b8e..25c81ed465fb 100644
@@ -51,7 +51,7 @@ int kunit_bus_init(void)
 
        error = bus_register(&kunit_bus_type);
        if (error)
-               bus_unregister(&kunit_bus_type);
+               root_device_unregister(kunit_bus_device);
        return error;
 }
 
--

2.44.0

#3 lore raw

Re: [PATCH v3 1/2] kunit: unregister the device on error

In reply to Wander Lairson Costa
Quoted 2 lines

kunit_init_device() should unregister the device on bus register error, but mistakenly it tries to unregister the bus.

Would the following description variant be more appropriate?

kunit_init_device() should unregister the device on bus registration error. But it mistakenly tries to unregister the bus.

Regards,
Markus

#4 lore raw

Re: [PATCH v3 1/2] kunit: unregister the device on error

Quoted 7 lines · Markus Elfring
Quoted 2 lines

kunit_init_device() should unregister the device on bus register error, but mistakenly it tries to unregister the bus.

Would the following description variant be more appropriate?

kunit_init_device() should unregister the device on bus registration error. But it mistakenly tries to unregister the bus.

Hi,

This is the semi-friendly patch-bot of Greg Kroah-Hartman.

Markus, you seem to have sent a nonsensical or otherwise pointless review comment to a patch submission on a Linux kernel developer mailing list. I strongly suggest that you not do this anymore. Please do not bother developers who are actively working to produce patches and features with comments that, in the end, are a waste of time.

Patch submitter, please ignore Markus's suggestion; you do not need to follow it at all. The person/bot/AI that sent it is being ignored by almost all Linux kernel maintainers for having a persistent pattern of behavior of producing distracting and pointless commentary, and inability to adapt to feedback. Please feel free to also ignore emails from them.

thanks,

greg k-h's patch email bot

#5 lore raw

PATCH v3 2/2 kunit: avoid memory leak on device register error

In reply to Wander Lairson Costa

If the device register fails, free the allocated memory before returning.

Fixes d03c720e03bd ("kunit: Add APIs for managing devices")
---
 lib/kunit/device.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
lib/kunit/device.c +6 -5
index 25c81ed465fb..bc2e2032e505 100644
@@ -119,10 +119,8 @@ static struct kunit_device *kunit_device_register_internal(struct kunit *test,
 	kunit_dev->owner = test;
 
 	err = dev_set_name(&kunit_dev->dev, "%s.%s", test->name, name);
-	if (err) {
-		kfree(kunit_dev);
-		return ERR_PTR(err);
-	}
+	if (err)
+		goto error;
 
 	kunit_dev->dev.release = kunit_device_release;
 	kunit_dev->dev.bus = &kunit_bus_type;
@@ -131,7 +129,7 @@ static struct kunit_device *kunit_device_register_internal(struct kunit *test,
 	err = device_register(&kunit_dev->dev);
 	if (err) {
 		put_device(&kunit_dev->dev);
-		return ERR_PTR(err);
+		goto error;
 	}
 
 	kunit_dev->dev.dma_mask = &kunit_dev->dev.coherent_dma_mask;
@@ -140,6 +138,9 @@ static struct kunit_device *kunit_device_register_internal(struct kunit *test,
 	kunit_add_action(test, device_unregister_wrapper, &kunit_dev->dev);
 
 	return kunit_dev;
+error:
+	kfree(kunit_dev);
+	return ERR_PTR(err);
 }
 
 /*
Signature · 1 line
2.44.0
#6 lore raw

Re: [PATCH v3 2/2] kunit: avoid memory leak on device register error

Quoted 7 lines · Wander Lairson Costa

If the device register fails, free the allocated memory before returning.

Fixes d03c720e03bd ("kunit: Add APIs for managing devices")

---

Thanks.

I'm not sure this is correct, though... Shouldn't put_device() free this for us?

The documentation for device_register() says to never free a device after device_register() has been called, even if it fails:
https://docs.kernel.org/driver-api/infrastructure.html#c.device_register

Or am I missing something?

Cheers,
-- David

Quoted 42 lines
 lib/kunit/device.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
lib/kunit/device.c +6 -6
index 25c81ed465fb..bc2e2032e505 100644
@@ -119,10 +119,8 @@ static struct kunit_device *kunit_device_register_internal(struct kunit *test,
        kunit_dev->owner = test;
 
        err = dev_set_name(&kunit_dev->dev, "%s.%s", test->name, name);
-       if (err) {
-               kfree(kunit_dev);
-               return ERR_PTR(err);
-       }
+       if (err)
+               goto error;
 
        kunit_dev->dev.release = kunit_device_release;
        kunit_dev->dev.bus = &kunit_bus_type;
@@ -131,7 +129,7 @@ static struct kunit_device *kunit_device_register_internal(struct kunit *test,
        err = device_register(&kunit_dev->dev);
        if (err) {
                put_device(&kunit_dev->dev);
-               return ERR_PTR(err);
+               goto error;
        }
 
        kunit_dev->dev.dma_mask = &kunit_dev->dev.coherent_dma_mask;
@@ -140,6 +138,9 @@ static struct kunit_device *kunit_device_register_internal(struct kunit *test,
        kunit_add_action(test, device_unregister_wrapper, &kunit_dev->dev);
 
        return kunit_dev;
+error:
+       kfree(kunit_dev);
+       return ERR_PTR(err);
 }
 
 /*
--

2.44.0

#7 lore raw

Re: [PATCH v3 2/2] kunit: avoid memory leak on device register error

Quoted 12 lines · David Gow
Quoted 7 lines · Wander Lairson Costa

If the device register fails, free the allocated memory before returning.

Fixes d03c720e03bd ("kunit: Add APIs for managing devices")

---

Thanks.

Quoted 8 lines

I'm not sure this is correct, though... Shouldn't put_device() free this for us?

The documentation for device_register() says to never free a device after device_register() has been called, even if it fails:
https://docs.kernel.org/driver-api/infrastructure.html#c.device_register

Or am I missing something?

I am not freeing the device object passed to device_register, but its parent structure.

As a side note, the behavior of device_register() seems counterintuitive and error-prone, IMO. If the function returns an error, it should ensure it leaks no resource and shouldn't require the caller to do any cleanup.

Quoted 46 lines

Cheers,
-- David

Quoted 42 lines
 lib/kunit/device.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
lib/kunit/device.c +6 -6
index 25c81ed465fb..bc2e2032e505 100644
@@ -119,10 +119,8 @@ static struct kunit_device *kunit_device_register_internal(struct kunit *test,
        kunit_dev->owner = test;
 
        err = dev_set_name(&kunit_dev->dev, "%s.%s", test->name, name);
-       if (err) {
-               kfree(kunit_dev);
-               return ERR_PTR(err);
-       }
+       if (err)
+               goto error;
 
        kunit_dev->dev.release = kunit_device_release;
        kunit_dev->dev.bus = &kunit_bus_type;
@@ -131,7 +129,7 @@ static struct kunit_device *kunit_device_register_internal(struct kunit *test,
        err = device_register(&kunit_dev->dev);
        if (err) {
                put_device(&kunit_dev->dev);
-               return ERR_PTR(err);
+               goto error;
        }
 
        kunit_dev->dev.dma_mask = &kunit_dev->dev.coherent_dma_mask;
@@ -140,6 +138,9 @@ static struct kunit_device *kunit_device_register_internal(struct kunit *test,
        kunit_add_action(test, device_unregister_wrapper, &kunit_dev->dev);
 
        return kunit_dev;
+error:
+       kfree(kunit_dev);
+       return ERR_PTR(err);
 }
 
 /*
--

2.44.0

#8 lore raw

Re: [PATCH v3 2/2] kunit: avoid memory leak on device register error

Quoted 4 lines · Wander Lairson Costa

As a side note, the behavior of device_register() seems counterintuitive and error-prone, IMO. If the function returns an error, it should ensure it leaks no resource and shouldn't require the caller to do any cleanup.

I too want a pony, but that's not the way the code works here, sorry. It's always been like this, and has always been a problem, but last time I looked, there was no way to really fix this. That's why we document it a lot to make sure people don't get the error paths wrong here. I know it's a pain :(

sorry,

greg k-h

#9 lore raw

Re: [PATCH v3 2/2] kunit: avoid memory leak on device register error

In reply to Wander Lairson Costa
Quoted 2 lines

If the device register fails, free the allocated memory before returning.

Can a description variant (like the following) be more appropriate?

Free the allocated memory (after a device registration failure) before returning.
Thus add a jump target so that a bit of exception handling can be better reused at the end of this function implementation.

Would you like to replace the word “register” by “registration” also in the summary phrase?

…

Quoted 1 line

+++ b/lib/kunit/device.c

…

lib/kunit/device.c static struct kunit_device *kunit_device_register_internal(struct kunit *test, · line 144

Quoted 8 lines
lib/kunit/device.c +3 -0
@@ -140,6 +138,9 @@ static struct kunit_device *kunit_device_register_internal(struct kunit *test,
 	kunit_add_action(test, device_unregister_wrapper, &kunit_dev->dev);
 
 	return kunit_dev;
+error:
+	kfree(kunit_dev);
+	return ERR_PTR(err);
 }

…

I find it nicer to use a label like free_device.

Regards,
Markus

#10 lore raw

Re: [PATCH v3 2/2] kunit: avoid memory leak on device register error

Quoted 5 lines · Markus Elfring
Quoted 2 lines

If the device register fails, free the allocated memory before returning.

Can a description variant (like the following) be more appropriate?

Quoted 9 lines

Free the allocated memory (after a device registration failure) before returning.
Thus add a jump target so that a bit of exception handling can be better reused at the end of this function implementation.

Would you like to replace the word “register” by “registration” also in the summary phrase?

Hi,

This is the semi-friendly patch-bot of Greg Kroah-Hartman.

Markus, you seem to have sent a nonsensical or otherwise pointless review comment to a patch submission on a Linux kernel developer mailing list. I strongly suggest that you not do this anymore. Please do not bother developers who are actively working to produce patches and features with comments that, in the end, are a waste of time.

Patch submitter, please ignore Markus's suggestion; you do not need to follow it at all. The person/bot/AI that sent it is being ignored by almost all Linux kernel maintainers for having a persistent pattern of behavior of producing distracting and pointless commentary, and inability to adapt to feedback. Please feel free to also ignore emails from them.

thanks,

greg k-h's patch email bot