diff mbox series

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

Message ID 20221104033945.1120044-2-yangyingliang@huawei.com (mailing list archive)
State Accepted
Commit 5822e8cc84ee37338ab0bdc3124f6eec04dc232d
Headers show
Series MIPS: fix possible memory leak while module exiting | expand

Commit Message

Yang Yingliang Nov. 4, 2022, 3:39 a.m. UTC
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")
Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
---
 arch/mips/kernel/vpe-mt.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Thomas Bogendoerfer Nov. 22, 2022, 11:21 a.m. UTC | #1
On Fri, Nov 04, 2022 at 11:39:44AM +0800, Yang Yingliang wrote:
> 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")
> Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
> ---
>  arch/mips/kernel/vpe-mt.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/mips/kernel/vpe-mt.c b/arch/mips/kernel/vpe-mt.c
> index bad6b0891b2b..84a82b551ec3 100644
> --- a/arch/mips/kernel/vpe-mt.c
> +++ b/arch/mips/kernel/vpe-mt.c
> @@ -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.
Yang Yingliang Nov. 22, 2022, 11:41 a.m. UTC | #2
Hi,

On 2022/11/22 19:21, Thomas Bogendoerfer wrote:
> On Fri, Nov 04, 2022 at 11:39:44AM +0800, Yang Yingliang wrote:
>> 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")
>> Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
>> ---
>>   arch/mips/kernel/vpe-mt.c | 4 ++--
>>   1 file changed, 2 insertions(+), 2 deletions(-)
>>
>> diff --git a/arch/mips/kernel/vpe-mt.c b/arch/mips/kernel/vpe-mt.c
>> index bad6b0891b2b..84a82b551ec3 100644
>> --- a/arch/mips/kernel/vpe-mt.c
>> +++ b/arch/mips/kernel/vpe-mt.c
>> @@ -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)
{
...

         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
>
> Thomas.
>
Thomas Bogendoerfer Nov. 22, 2022, 12:07 p.m. UTC | #3
On Tue, Nov 22, 2022 at 07:41:26PM +0800, Yang Yingliang wrote:
> Hi,
> 
> On 2022/11/22 19:21, Thomas Bogendoerfer wrote:
> > On Fri, Nov 04, 2022 at 11:39:44AM +0800, Yang Yingliang wrote:
> > > 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")
> > > Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
> > > ---
> > >   arch/mips/kernel/vpe-mt.c | 4 ++--
> > >   1 file changed, 2 insertions(+), 2 deletions(-)
> > > 
> > > diff --git a/arch/mips/kernel/vpe-mt.c b/arch/mips/kernel/vpe-mt.c
> > > index bad6b0891b2b..84a82b551ec3 100644
> > > --- a/arch/mips/kernel/vpe-mt.c
> > > +++ b/arch/mips/kernel/vpe-mt.c
> > > @@ -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)
> {
> ...
> 
>         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.
diff mbox series

Patch

diff --git a/arch/mips/kernel/vpe-mt.c b/arch/mips/kernel/vpe-mt.c
index bad6b0891b2b..84a82b551ec3 100644
--- a/arch/mips/kernel/vpe-mt.c
+++ b/arch/mips/kernel/vpe-mt.c
@@ -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);