Skip to contentSkip to conversation

kunit: fix minor error path mistakes

PATCH In review Wander Lairson Costa 4 participants 10 messages Apr 19, 2024 – Apr 23, 2024

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

v4 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 v3->v4
* Remove some changes requested by Marcus Elfring, as I was alerted he is a known troll.

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 v4 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
#3 lore raw

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

Quoted 4 lines
Quoted 4 lines

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.

…

Quoted 1 line

Would you ever like to distinguish hardware register errors from item registration failures according to further improved commit messages?

Regards,
Markus

#4 lore raw

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

Quoted 9 lines · Markus Elfring
Quoted 4 lines
Quoted 4 lines

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.

…

Quoted 1 line

Would you ever like to distinguish hardware register errors from item registration failures according to further improved commit messages?

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 v4 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 | 1 +
 1 file changed, 1 insertion(+)
lib/kunit/device.c +1 -0
index 25c81ed465fb..d8c09dcb3e79 100644
@@ -131,6 +131,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);
+		kfree(kunit_dev);
 		return ERR_PTR(err);
 	}
 
Signature · 1 line
2.44.0
#6 lore raw

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

Quoted 14 lines · 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 | 1 +
 1 file changed, 1 insertion(+)
lib/kunit/device.c +0 -0
index 25c81ed465fb..d8c09dcb3e79 100644

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

Quoted 4 lines
lib/kunit/device.c +1 -0
@@ -131,6 +131,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);
+		kfree(kunit_dev);

This still looks wrong, the release function for the device should free the memory here, not this kfree, as the reference count in the embedded 'struct device' handles the memory logic for the whole structure (if not, then something is REALLY wrong...)

You _do_ have a release function for the device, right? If not, you should be getting loud messages in the kernel log when releasing a device here.

thanks,

greg k-h

#7 lore raw

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

Quoted 26 lines · Greg Kroah-Hartman
Quoted 14 lines · 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 | 1 +
 1 file changed, 1 insertion(+)
lib/kunit/device.c +0 -0
index 25c81ed465fb..d8c09dcb3e79 100644

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

Quoted 4 lines
lib/kunit/device.c +1 -0
@@ -131,6 +131,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);
+             kfree(kunit_dev);

This still looks wrong, the release function for the device should free the memory here, not this kfree, as the reference count in the embedded 'struct device' handles the memory logic for the whole structure (if not, then something is REALLY wrong...)

Quoted 4 lines

You _do_ have a release function for the device, right? If not, you should be getting loud messages in the kernel log when releasing a device here.

Ok, I got it. Yes, there is a release function. So this patch is wrong, ignore it. Should I send a v5 with only the other patch?

Quoted 4 lines

thanks,

greg k-h

#8 lore raw

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

Quoted 29 lines · Wander Lairson Costa
Quoted 26 lines · Greg Kroah-Hartman
Quoted 14 lines · 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 | 1 +
 1 file changed, 1 insertion(+)
lib/kunit/device.c +0 -0
index 25c81ed465fb..d8c09dcb3e79 100644

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

Quoted 4 lines
lib/kunit/device.c +1 -0
@@ -131,6 +131,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);
+             kfree(kunit_dev);

This still looks wrong, the release function for the device should free the memory here, not this kfree, as the reference count in the embedded 'struct device' handles the memory logic for the whole structure (if not, then something is REALLY wrong...)

Quoted 8 lines
Quoted 4 lines

You _do_ have a release function for the device, right? If not, you should be getting loud messages in the kernel log when releasing a device here.

Ok, I got it. Yes, there is a release function. So this patch is wrong, ignore it. Should I send a v5 with only the other patch?

Thanks. Don't worry about sending a v5: just patch 1 of v4 is now in the kunit branch:
https://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest.git/commit/?h=kunit&id=63761ec5971ea47c1f2d7698f03e1c6ffb9fb22a

Cheers,
-- David

#9 lore raw
In reply to Wander Lairson Costa

…

Quoted 1 line

* Remove some changes requested by Marcus Elfring,

I became curious how affected software components can evolve further.

Quoted 1 line

as I was alerted he is a known troll.

I would appreciate if this interpretation will be reconsidered somehow.

Regards,
Markus