Skip to contentSkip to conversation

drivers: base: transport_class: fix possible memory leak

PATCH Superseded Yang Yingliang 2 participants 4 messages Nov 10, 2022 SCSI

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drivers: base: transport_class: fix possible memory leak

Current some drivers(like iscsi) call transport_register_device() failed, they don't call transport_destroy_device() to release the memory allocated in transport_setup_device(), because they don't know what was done, it should be internal thing to release the resource in register function. So fix this leak by calling destroy function inside register function.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 include/linux/transport_class.h | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
include/linux/transport_class.h +8 -1
index 63076fb835e3..f4835250bbfc 100644
@@ -70,8 +70,15 @@ void transport_destroy_device(struct device *);
 static inline int
 transport_register_device(struct device *dev)
 {
+	int ret;
+
 	transport_setup_device(dev);
-	return transport_add_device(dev);
+	ret = transport_add_device(dev);
+	if (ret) {
+		transport_destroy_device(dev);
+	}
+
+	return ret;
 }
 
 static inline void
Signature · 1 line
2.25.1
#1 lore raw
Quoted 19 lines · Yang Yingliang

Current some drivers(like iscsi) call transport_register_device() failed, they don't call transport_destroy_device() to release the memory allocated in transport_setup_device(), because they don't know what was done, it should be internal thing to release the resource in register function. So fix this leak by calling destroy function inside register function.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 include/linux/transport_class.h | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
include/linux/transport_class.h +0 -0
index 63076fb835e3..f4835250bbfc 100644
@@ -70,8 +70,15 @@ void transport_destroy_device(struct device *);
 static inline int

include/linux/transport_class.h void transport_destroy_device(struct device *); · line 79

Quoted 10 lines
include/linux/transport_class.h +6 -1
 transport_register_device(struct device *dev)
 {
+	int ret;
+
 	transport_setup_device(dev);
-	return transport_add_device(dev);
+	ret = transport_add_device(dev);
+	if (ret) {
+		transport_destroy_device(dev);
+	}

Please use scripts/checkpatch.pl on your patches before sending them out so you don't get grumpy maintainers asking you to use scripts/checkpatch.pl on your patches :)

thanks,

greg k-h

#2 lore raw

Hi Greg,

Quoted 23 lines · Greg KH
Quoted 22 lines · Yang Yingliang

Current some drivers(like iscsi) call transport_register_device() failed, they don't call transport_destroy_device() to release the memory allocated in transport_setup_device(), because they don't know what was done, it should be internal thing to release the resource in register function. So fix this leak by calling destroy function inside register function.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
  include/linux/transport_class.h | 9 ++++++++-
  1 file changed, 8 insertions(+), 1 deletion(-)
include/linux/transport_class.h +1 -0
index 63076fb835e3..f4835250bbfc 100644
@@ -70,8 +70,15 @@ void transport_destroy_device(struct device *);
  static inline int
  transport_register_device(struct device *dev)
  {
+	int ret;
Quoted 8 lines

include/linux/transport_class.h void transport_destroy_device(struct device *); · line 79

Quoted 7 lines
include/linux/transport_class.h +5 -1
+
  	transport_setup_device(dev);
-	return transport_add_device(dev);
+	ret = transport_add_device(dev);
+	if (ret) {
+		transport_destroy_device(dev);
+	}

Please use scripts/checkpatch.pl on your patches before sending them out

Sure, of course. :)

Quoted 2 lines

so you don't get grumpy maintainers asking you to use scripts/checkpatch.pl on your patches :)

I sent a fix patch to iscsi system earlier:
https://patchwork.kernel.org/project/linux-scsi/patch/[email protected]/

Mike give his point in the mail, so I send a new patch keep iscsi maintainers Cced.

Thanks,
Yang

Quoted 6 lines

thanks,

greg k-h

.

#3 lore raw
Quoted 34 lines · Yang Yingliang

Hi Greg,

Quoted 23 lines · Greg KH
Quoted 22 lines · Yang Yingliang

Current some drivers(like iscsi) call transport_register_device() failed, they don't call transport_destroy_device() to release the memory allocated in transport_setup_device(), because they don't know what was done, it should be internal thing to release the resource in register function. So fix this leak by calling destroy function inside register function.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
  include/linux/transport_class.h | 9 ++++++++-
  1 file changed, 8 insertions(+), 1 deletion(-)
include/linux/transport_class.h +1 -0
index 63076fb835e3..f4835250bbfc 100644
@@ -70,8 +70,15 @@ void transport_destroy_device(struct device *);
  static inline int
  transport_register_device(struct device *dev)
  {
+	int ret;
Quoted 8 lines

include/linux/transport_class.h void transport_destroy_device(struct device *); · line 79

Quoted 7 lines
include/linux/transport_class.h +5 -1
+
  	transport_setup_device(dev);
-	return transport_add_device(dev);
+	ret = transport_add_device(dev);
+	if (ret) {
+		transport_destroy_device(dev);
+	}

Please use scripts/checkpatch.pl on your patches before sending them out

Quoted 8 lines

Sure, of course. :)

Quoted 2 lines

so you don't get grumpy maintainers asking you to use scripts/checkpatch.pl on your patches :)

I sent a fix patch to iscsi system earlier:
https://patchwork.kernel.org/project/linux-scsi/patch/[email protected]/

Mike give his point in the mail, so I send a new patch keep iscsi maintainers Cced.

That's fine, but the code you wrote here should look different as it does not follow our coding style rules. That is the point I was trying to make.

thanks,

greg k-h