Skip to contentSkip to conversation

MIPS: fix possible memory leak while module exiting

PATCH In review Yang Yingliang 2 participants 7 messages Nov 4, 2022 – Nov 22, 2022 MIPS

lore
Share & tools
lore
#0 lore raw

v2 cover MIPS: fix possible memory leak while module exiting

This patchset fixes two device name leaks while module exiting in normal or error path.

v1 -> v2:
Add fix tag in patch #1.

Yang Yingliang (2):
MIPS: vpe-mt: fix possible memory leak while module exiting
MIPS: vpe-cmp: fix possible memory leak while module exiting

 arch/mips/kernel/vpe-cmp.c | 4 ++--
 arch/mips/kernel/vpe-mt.c  | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v2 1/2 MIPS: vpe-mt: fix possible memory leak while module exiting

Afer commit 1fa5ae857bb1 ("driver core: get rid of struct device's bus_id string array"), the name of device is allocated dynamically, it need be freed when module exiting, call put_device() to give up reference, so that it can be freed in kobject_cleanup() when the refcount hit to 0. The vpe_device is static, so remove kfree() from vpe_device_release().

Fixes 1fa5ae857bb1 ("driver core: get rid of struct device's bus_id string array")
---
 arch/mips/kernel/vpe-mt.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/mips/kernel/vpe-mt.c +2 -2
index bad6b0891b2b..84a82b551ec3 100644
@@ -313,7 +313,6 @@ ATTRIBUTE_GROUPS(vpe);
 
 static void vpe_device_release(struct device *cd)
 {
-	kfree(cd);
 }
 
 static struct class vpe_class = {
@@ -497,6 +496,7 @@ int __init vpe_module_init(void)
 	device_del(&vpe_device);
 
 out_class:
+	put_device(&vpe_device);
 	class_unregister(&vpe_class);
 
 out_chrdev:
@@ -509,7 +509,7 @@ void __exit vpe_module_exit(void)
 {
 	struct vpe *v, *n;
 
-	device_del(&vpe_device);
+	device_unregister(&vpe_device);
 	class_unregister(&vpe_class);
 	unregister_chrdev(major, VPE_MODULE_NAME);
 
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH v2 1/2] MIPS: vpe-mt: fix possible memory leak while module exiting

Quoted 18 lines · Yang Yingliang

Afer commit 1fa5ae857bb1 ("driver core: get rid of struct device's bus_id string array"), the name of device is allocated dynamically, it need be freed when module exiting, call put_device() to give up reference, so that it can be freed in kobject_cleanup() when the refcount hit to 0. The vpe_device is static, so remove kfree() from vpe_device_release().

Fixes 1fa5ae857bb1 ("driver core: get rid of struct device's bus_id string array")
---
 arch/mips/kernel/vpe-mt.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/mips/kernel/vpe-mt.c +0 -0
index bad6b0891b2b..84a82b551ec3 100644

arch/mips/kernel/vpe-mt.c ATTRIBUTE_GROUPS(vpe); · line 316

Quoted 5 lines
arch/mips/kernel/vpe-mt.c +0 -1
@@ -313,7 +313,6 @@ ATTRIBUTE_GROUPS(vpe);
 
 static void vpe_device_release(struct device *cd)
 {
-	kfree(cd);
 }

as this is empty now, we can IMHO remove the function completly. Same for the other patch in this series.

Thomas.

Signature · 2 lines
Crap can work. Given enough thrust pigs will fly, but it's not necessarily a
good idea.                                                [ RFC1925, 2.3 ]
#3 lore raw

Re: [PATCH v2 1/2] MIPS: vpe-mt: fix possible memory leak while module exiting

Hi,

Quoted 20 lines · Thomas Bogendoerfer
Quoted 19 lines · Yang Yingliang

Afer commit 1fa5ae857bb1 ("driver core: get rid of struct device's bus_id string array"), the name of device is allocated dynamically, it need be freed when module exiting, call put_device() to give up reference, so that it can be freed in kobject_cleanup() when the refcount hit to 0. The vpe_device is static, so remove kfree() from vpe_device_release().

Fixes 1fa5ae857bb1 ("driver core: get rid of struct device's bus_id string array")
---
  arch/mips/kernel/vpe-mt.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
arch/mips/kernel/vpe-mt.c +0 -0
index bad6b0891b2b..84a82b551ec3 100644
@@ -313,7 +313,6 @@ ATTRIBUTE_GROUPS(vpe);
 
Quoted 6 lines

arch/mips/kernel/vpe-mt.c ATTRIBUTE_GROUPS(vpe); · line 316

Quoted 4 lines
arch/mips/kernel/vpe-mt.c +0 -1
  static void vpe_device_release(struct device *cd)
  {
-	kfree(cd);
  }

as this is empty now, we can IMHO remove the function completly. Same for the other patch in this series.

It can not be removed, or it will cause WARNING in device_release() when calling put_device()/device_unregister().

static void device_release(struct kobject *kobj)
{
...

        if (dev->release)
                dev->release(dev);
        else if (dev->type && dev->type->release)
                dev->type->release(dev);
        else if (dev->class && dev->class->dev_release)
                dev->class->dev_release(dev);
        else
                WARN(1, KERN_ERR "Device '%s' does not have a release() function, it is broken and must be fixed. See Documentation/core-api/kobject.rst.\n", dev_name(dev));  // WANING here
...
}

Thanks,
Yang

Quoted 3 lines

Thomas.

#4 lore raw

Re: [PATCH v2 1/2] MIPS: vpe-mt: fix possible memory leak while module exiting

Quoted 36 lines · Yang Yingliang

Hi,

Quoted 14 lines · Thomas Bogendoerfer
Quoted 13 lines · Yang Yingliang

Afer commit 1fa5ae857bb1 ("driver core: get rid of struct device's bus_id string array"), the name of device is allocated dynamically, it need be freed when module exiting, call put_device() to give up reference, so that it can be freed in kobject_cleanup() when the refcount hit to 0. The vpe_device is static, so remove kfree() from vpe_device_release().

Fixes 1fa5ae857bb1 ("driver core: get rid of struct device's bus_id string array")
---
  arch/mips/kernel/vpe-mt.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 11 lines

arch/mips/kernel/vpe-mt.c ATTRIBUTE_GROUPS(vpe); · line 315

Quoted 9 lines
arch/mips/kernel/vpe-mt.c +0 -1
index bad6b0891b2b..84a82b551ec3 100644
@@ -313,7 +313,6 @@ ATTRIBUTE_GROUPS(vpe);
  static void vpe_device_release(struct device *cd)
  {
-	kfree(cd);
  }

as this is empty now, we can IMHO remove the function completly. Same for the other patch in this series.

It can not be removed, or it will cause WARNING in device_release() when calling
put_device()/device_unregister().

static void device_release(struct kobject *kobj)
{
...

Quoted 11 lines

        if (dev->release)
                dev->release(dev);
        else if (dev->type && dev->type->release) dev->type->release(dev);
        else if (dev->class && dev->class->dev_release) dev->class->dev_release(dev);
        else
                WARN(1, KERN_ERR "Device '%s' does not have a release() function, it is broken and must be fixed. See Documentation/core-api/kobject.rst.\n",
                        dev_name(dev));  // WANING here

you are right, thank you for looking into that,

Thomas.

Signature · 2 lines
Crap can work. Given enough thrust pigs will fly, but it's not necessarily a
good idea.                                                [ RFC1925, 2.3 ]
#5 lore raw

PATCH v2 2/2 MIPS: vpe-cmp: fix possible memory leak while module exiting

In reply to Yang Yingliang

dev_set_name() allocates memory for name, it need be freed when module exiting, call put_device() to give up reference, so that it can be freed in kobject_cleanup() when the refcount hit to 0. The vpe_device is static, so remove kfree() from vpe_device_release().

Fixes 17a1d523aa58 ("MIPS: APRP: Add VPE loader support for CMP platforms.")
---
 arch/mips/kernel/vpe-cmp.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/mips/kernel/vpe-cmp.c +2 -2
index e673603e11e5..92140edb3ce3 100644
@@ -75,7 +75,6 @@ ATTRIBUTE_GROUPS(vpe);
 
 static void vpe_device_release(struct device *cd)
 {
-	kfree(cd);
 }
 
 static struct class vpe_class = {
@@ -157,6 +156,7 @@ int __init vpe_module_init(void)
 	device_del(&vpe_device);
 
 out_class:
+	put_device(&vpe_device);
 	class_unregister(&vpe_class);
 
 out_chrdev:
@@ -169,7 +169,7 @@ void __exit vpe_module_exit(void)
 {
 	struct vpe *v, *n;
 
-	device_del(&vpe_device);
+	device_unregister(&vpe_device);
 	class_unregister(&vpe_class);
 	unregister_chrdev(major, VPE_MODULE_NAME);
 
Signature · 1 line
2.25.1
#6 lore raw
In reply to Yang Yingliang
Quoted 6 lines · Yang Yingliang

This patchset fixes two device name leaks while module exiting in normal or error path.

v1 -> v2:
Add fix tag in patch #1.

Quoted 10 lines

Yang Yingliang (2):
MIPS: vpe-mt: fix possible memory leak while module exiting
MIPS: vpe-cmp: fix possible memory leak while module exiting

 arch/mips/kernel/vpe-cmp.c | 4 ++--
 arch/mips/kernel/vpe-mt.c  | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
Signature · 1 line
2.25.1

series applied to mips-next.

Thomas.

Signature · 2 lines
Crap can work. Given enough thrust pigs will fly, but it's not necessarily a
good idea.                                                [ RFC1925, 2.3 ]