mbox series

[v3,0/3] Refactor __kmem_cache_create() and fix memory leak

Message ID 20221112112055.1111078-1-liushixin2@huawei.com (mailing list archive)
Headers show
Series Refactor __kmem_cache_create() and fix memory leak | expand

Message

Liu Shixin Nov. 12, 2022, 11:20 a.m. UTC
I found a memory leak of kobj->name in sysfs_slab_add() which is introduced
by 80da026a8e5d ("mm/slub: fix slab double-free in case of duplicate sysfs filename").
Following the rules stated in the comment for kobject_init_and_add():

 If this function returns an error, kobject_put() must be called to
 properly clean up the memory associated with the object.

We should use kobject_put() to free kobject.

But we can't simply add kobject_put() since it will free kmem_cache too.
If we use kobject_put(), we need to skip other release functions.

In this series, We refactor the code to separate sysfs_slab_add() and
debugfs_slab_add() from __kmem_cache_create(), and then use kobject_put()
to free kobject in sysfs_slab_add(). This can fix the memory leak of
kobject->name.

v1->v2: Fix build error reported by kernel test robot <lkp@intel.com>.
v2->v3: Don't free kmem_cache that create early.

Liu Shixin (3):
  mm/slab_common: Move cache_name to create_cache()
  mm/slub: Refactor __kmem_cache_create()
  mm/slub: Fix memory leak of kobj->name in sysfs_slab_add()

 include/linux/slub_def.h | 11 +++++++
 mm/slab_common.c         | 44 +++++++++++++++-------------
 mm/slub.c                | 63 +++++++++++++++-------------------------
 3 files changed, 58 insertions(+), 60 deletions(-)

Comments

Liu Shixin Nov. 12, 2022, 10:47 a.m. UTC | #1
Please ignore this set of patches. I need fix and will resend then.


thanks,

Liu Shixin

.

On 2022/11/12 19:20, Liu Shixin wrote:
> I found a memory leak of kobj->name in sysfs_slab_add() which is introduced
> by 80da026a8e5d ("mm/slub: fix slab double-free in case of duplicate sysfs filename").
> Following the rules stated in the comment for kobject_init_and_add():
>
>  If this function returns an error, kobject_put() must be called to
>  properly clean up the memory associated with the object.
>
> We should use kobject_put() to free kobject.
>
> But we can't simply add kobject_put() since it will free kmem_cache too.
> If we use kobject_put(), we need to skip other release functions.
>
> In this series, We refactor the code to separate sysfs_slab_add() and
> debugfs_slab_add() from __kmem_cache_create(), and then use kobject_put()
> to free kobject in sysfs_slab_add(). This can fix the memory leak of
> kobject->name.
>
> v1->v2: Fix build error reported by kernel test robot <lkp@intel.com>.
> v2->v3: Don't free kmem_cache that create early.
>
> Liu Shixin (3):
>   mm/slab_common: Move cache_name to create_cache()
>   mm/slub: Refactor __kmem_cache_create()
>   mm/slub: Fix memory leak of kobj->name in sysfs_slab_add()
>
>  include/linux/slub_def.h | 11 +++++++
>  mm/slab_common.c         | 44 +++++++++++++++-------------
>  mm/slub.c                | 63 +++++++++++++++-------------------------
>  3 files changed, 58 insertions(+), 60 deletions(-)
>