Skip to contentSkip to conversation

Refactor __kmem_cache_create() and fix memory leak

PATCH Superseded Liu Shixin 3 participants 8 messages Oct 31, 2022 – Nov 9, 2022

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Liu Shixin

author
#0 lore raw

v2 cover Refactor __kmem_cache_create() and fix memory leak

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 <[email protected]>.

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                | 52 ++++++++++------------------------------
 3 files changed, 48 insertions(+), 59 deletions(-)
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v2 1/3 mm/slab_common: Move cache_name to create_cache()

The string cache_name and its kmem_cache have same life cycle. The latter is allocated in create_cache() so move cache_name to create_cache() too for better error handing.

---
 mm/slab_common.c | 34 ++++++++++++++--------------------
 1 file changed, 14 insertions(+), 20 deletions(-)
mm/slab_common.c +14 -20
index 33b1886b06eb..e5f430a17d95 100644
@@ -209,17 +209,21 @@ static struct kmem_cache *create_cache(const char *name,
 		struct kmem_cache *root_cache)
 {
 	struct kmem_cache *s;
-	int err;
+	const char *cache_name;
+	int err = -ENOMEM;
 
 	if (WARN_ON(useroffset + usersize > object_size))
 		useroffset = usersize = 0;
 
-	err = -ENOMEM;
 	s = kmem_cache_zalloc(kmem_cache, GFP_KERNEL);
 	if (!s)
-		goto out;
+		return ERR_PTR(err);
 
-	s->name = name;
+	cache_name = kstrdup_const(name, GFP_KERNEL);
+	if (!cache_name)
+		goto out_free_cache;
+
+	s->name = cache_name;
 	s->size = s->object_size = object_size;
 	s->align = align;
 	s->ctor = ctor;
@@ -228,18 +232,17 @@ static struct kmem_cache *create_cache(const char *name,
 
 	err = __kmem_cache_create(s, flags);
 	if (err)
-		goto out_free_cache;
+		goto out_free_name;
 
 	s->refcount = 1;
 	list_add(&s->list, &slab_caches);
-out:
-	if (err)
-		return ERR_PTR(err);
 	return s;
 
+out_free_name:
+	kfree_const(s->name);
 out_free_cache:
 	kmem_cache_free(kmem_cache, s);
-	goto out;
+	return ERR_PTR(err);
 }
 
 /**
@@ -278,7 +281,6 @@ kmem_cache_create_usercopy(const char *name,
 		  void (*ctor)(void *))
 {
 	struct kmem_cache *s = NULL;
-	const char *cache_name;
 	int err;
 
 #ifdef CONFIG_SLUB_DEBUG
@@ -326,19 +328,11 @@ kmem_cache_create_usercopy(const char *name,
 	if (s)
 		goto out_unlock;
 
-	cache_name = kstrdup_const(name, GFP_KERNEL);
-	if (!cache_name) {
-		err = -ENOMEM;
-		goto out_unlock;
-	}
-
-	s = create_cache(cache_name, size,
+	s = create_cache(name, size,
 			 calculate_alignment(flags, align, size),
 			 flags, useroffset, usersize, ctor, NULL);
-	if (IS_ERR(s)) {
+	if (IS_ERR(s))
 		err = PTR_ERR(s);
-		kfree_const(cache_name);
-	}
 
 out_unlock:
 	mutex_unlock(&slab_mutex);
Signature · 1 line
2.25.1
author
#2 lore raw

PATCH v2 2/3 mm/slub: Refactor __kmem_cache_create()

In reply to Liu Shixin

Separate sysfs_slab_add() and debugfs_slab_add() from __kmem_cache_create() can help to fix a memory leak about kobject. After this patch, we can fix the memory leak naturally by calling kobject_put() to free kobject and associated kmem_cache when sysfs_slab_add() failed.

Besides, after that, we can easy to provide sysfs and debugfs support for other allocators too.

---
 include/linux/slub_def.h | 11 ++++++++++
 mm/slab_common.c         | 12 +++++++++++
 mm/slub.c                | 44 +++++++---------------------------------
 3 files changed, 30 insertions(+), 37 deletions(-)
include/linux/slub_def.h +11 -0
index f9c68a9dac04..26d56c4c74d1 100644
@@ -144,9 +144,14 @@ struct kmem_cache {
 
 #ifdef CONFIG_SYSFS
 #define SLAB_SUPPORTS_SYSFS
+int sysfs_slab_add(struct kmem_cache *);
 void sysfs_slab_unlink(struct kmem_cache *);
 void sysfs_slab_release(struct kmem_cache *);
 #else
+static inline int sysfs_slab_add(struct kmem_cache *s)
+{
+	return 0;
+}
 static inline void sysfs_slab_unlink(struct kmem_cache *s)
 {
 }
@@ -155,6 +160,12 @@ static inline void sysfs_slab_release(struct kmem_cache *s)
 }
 #endif
 
+#if defined(CONFIG_DEBUG_FS) && defined(CONFIG_SLUB_DEBUG)
+void debugfs_slab_add(struct kmem_cache *);
+#else
+static inline void debugfs_slab_add(struct kmem_cache *s) { }
+#endif
+
 void *fixup_red_left(struct kmem_cache *s, void *p);
 
 static inline void *nearest_obj(struct kmem_cache *cache, const struct slab *slab,
mm/slab_common.c +12 -0
index e5f430a17d95..55e2cf064dfe 100644
@@ -234,6 +234,18 @@ static struct kmem_cache *create_cache(const char *name,
 	if (err)
 		goto out_free_name;
 
+#ifdef SLAB_SUPPORTS_SYSFS
+	/* Mutex is not taken during early boot */
+	if (slab_state >= FULL) {
+		err = sysfs_slab_add(s);
+		if (err) {
+			slab_kmem_cache_release(s);
+			return ERR_PTR(err);
+		}
+		debugfs_slab_add(s);
+	}
+#endif
+
 	s->refcount = 1;
 	list_add(&s->list, &slab_caches);
 	return s;
mm/slub.c +7 -37

89 lines not on this page · raw message

Signature · 1 line
2.25.1
author
#3 lore raw

PATCH v2 3/3 mm/slub: Fix memory leak of kobj->name in sysfs_slab_add()

In reply to Liu Shixin

There is a memory leak of kobj->name in sysfs_slab_add():

unreferenced object 0xffff88817e446440 (size 32):
comm "insmod", pid 4085, jiffies 4296564501 (age 126.272s) hex dump (first 32 bytes):

     75 62 69 66 73 5f 69 6e 6f 64 65 5f 73 6c 61 62  ubifs_inode_slab
     00 65 44 7e 81 88 ff ff 00 00 00 00 00 00 00 00  .eD~............
   backtrace:
     [<000000005b30fbbd>] __kmalloc_node_track_caller+0x4e/0x150
     [<000000002f70da0c>] kstrdup_const+0x4b/0x80
     [<00000000c6712c61>] kobject_set_name_vargs+0x2f/0xb0
     [<00000000b151218e>] kobject_init_and_add+0xb0/0x120
     [<00000000e56a4cf5>] sysfs_slab_add+0x17d/0x220
     [<000000009326fd57>] __kmem_cache_create+0x406/0x590
     [<00000000dde33cff>] kmem_cache_create_usercopy+0x1fc/0x300
     [<00000000fe90cedb>] kmem_cache_create+0x12/0x20
     [<000000007a6531c8>] 0xffffffffa02d802d
     [<000000000e3b13c7>] do_one_initcall+0x87/0x2a0
     [<00000000995ecdcf>] do_init_module+0xdf/0x320
     [<000000008821941f>] load_module+0x2f98/0x3330
     [<00000000ef51efa4>] __do_sys_finit_module+0x113/0x1b0
     [<000000009339fbce>] do_syscall_64+0x35/0x80
     [<000000006b7f2033>] entry_SYSCALL_64_after_hwframe+0x46/0xb0

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.

kobject_put() is more appropriate for error handling after kobject_init(). And we can use this function to solve this problem.

Fixes 80da026a8e5d ("mm/slub: fix slab double-free in case of duplicate sysfs filename")
---
 mm/slab_common.c | 4 +---
 mm/slub.c        | 8 ++++++--
 2 files changed, 7 insertions(+), 5 deletions(-)
mm/slab_common.c +1 -3
index 55e2cf064dfe..9337724b5c76 100644
@@ -238,10 +238,8 @@ static struct kmem_cache *create_cache(const char *name,
 	/* Mutex is not taken during early boot */
 	if (slab_state >= FULL) {
 		err = sysfs_slab_add(s);
-		if (err) {
-			slab_kmem_cache_release(s);
+		if (err)
 			return ERR_PTR(err);
-		}
 		debugfs_slab_add(s);
 	}
 #endif
mm/slub.c +6 -2
index a1ad759753ce..f8883bc642b8 100644
@@ -5911,14 +5911,16 @@ int sysfs_slab_add(struct kmem_cache *s)
 		 * for the symlinks.
 		 */
 		name = create_unique_id(s);
-		if (IS_ERR(name))
+		if (IS_ERR(name)) {
+			slab_kmem_cache_release(s);
 			return PTR_ERR(name);
+		}
 	}
 
 	s->kobj.kset = kset;
 	err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, "%s", name);
 	if (err)
-		goto out;
+		goto out_put_kobj;
 
 	err = sysfs_create_group(&s->kobj, &slab_attr_group);
 	if (err)
@@ -5934,6 +5936,8 @@ int sysfs_slab_add(struct kmem_cache *s)
 	return err;
 out_del_kobj:
 	kobject_del(&s->kobj);
+out_put_kobj:
+	kobject_put(&s->kobj);
 	goto out;
 }
 
Signature · 1 line
2.25.1
#4 lore raw
In reply to Liu Shixin
Quoted 3 lines · Liu Shixin

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():

Thank you for reporting this! Indeed it seems tried to fix double free but introduced a leak.

Quoted 4 lines

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 what to do if a cache is created early and later sysfs_slab_add() failed? (Which is unlikely on normal condition)

With this series it introduces use-after-free if sysfs_slab_add() in slab_sysfs_init() failed. Should we just call BUG() or something like that?

Quoted 23 lines

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 <[email protected]>.

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                | 52 ++++++++++------------------------------
 3 files changed, 48 insertions(+), 59 deletions(-)
Signature · 1 line
2.25.1
Signature · 2 lines
Thanks,
Hyeonggon
author
#5 lore raw
Quoted 10 lines · Hyeonggon Yoo
Quoted 3 lines · Liu Shixin

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():

Thank you for reporting this! Indeed it seems tried to fix double free but introduced a leak.

Quoted 3 lines

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

Quoted 6 lines
Quoted 1 line

We should use kobject_put() to free kobject.

But what to do if a cache is created early and later sysfs_slab_add() failed? (Which is unlikely on normal condition)

With this series it introduces use-after-free if sysfs_slab_add() in slab_sysfs_init() failed. Should we just call BUG() or something like that?

Thanks for your discovery, what I missed. I prefer to panic directly, just as create_boot_cache() does. Of couse, if you want the system to continue booting, I think it's possible to distinguish them by slab_state.

Looking forward to your advice.
Thanks,

Quoted 24 lines
Quoted 24 lines

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 <[email protected]>.

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                | 52 ++++++++++------------------------------
 3 files changed, 48 insertions(+), 59 deletions(-)
Signature · 1 line
2.25.1
#6 lore raw
Quoted 11 lines · Liu Shixin
Quoted 10 lines · Hyeonggon Yoo
Quoted 3 lines · Liu Shixin

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():

Thank you for reporting this! Indeed it seems tried to fix double free but introduced a leak.

Quoted 3 lines

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

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

We should use kobject_put() to free kobject.

But what to do if a cache is created early and later sysfs_slab_add() failed? (Which is unlikely on normal condition)

With this series it introduces use-after-free if sysfs_slab_add() in slab_sysfs_init() failed. Should we just call BUG() or something like that?

Thanks for your discovery, what I missed.

You're welcome.

Quoted 1 line

I prefer to panic directly, just as create_boot_cache() does.

IMHO that should be nothing serious. but let's hear maintainers' opinion.

Quoted 2 lines

Of couse, if you want the system to continue booting, I think it's possible to distinguish them by slab_state.

I'm afraid to make it more complex :(

Quoted 27 lines

Looking forward to your advice.
Thanks,

Quoted 24 lines
Quoted 24 lines

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 <[email protected]>.

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                | 52 ++++++++++------------------------------
 3 files changed, 48 insertions(+), 59 deletions(-)
Signature · 1 line
2.25.1
Signature · 2 lines
Thanks,
Hyeonggon
#7 lore raw
Quoted 23 lines · Hyeonggon Yoo
Quoted 11 lines · Liu Shixin
Quoted 10 lines · Hyeonggon Yoo
Quoted 3 lines · Liu Shixin

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():

Thank you for reporting this! Indeed it seems tried to fix double free but introduced a leak.

Quoted 3 lines

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

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

We should use kobject_put() to free kobject.

But what to do if a cache is created early and later sysfs_slab_add() failed? (Which is unlikely on normal condition)

With this series it introduces use-after-free if sysfs_slab_add() in slab_sysfs_init() failed. Should we just call BUG() or something like that?

Thanks for your discovery, what I missed.

You're welcome.

Quoted 8 lines
Quoted 1 line

I prefer to panic directly, just as create_boot_cache() does.

IMHO that should be nothing serious. but let's hear maintainers' opinion.

Quoted 2 lines

Of couse, if you want the system to continue booting, I think it's possible to distinguish them by slab_state.

I'm afraid to make it more complex :(

+Cc Rasmus who did a recent patch in this area. Thread starts here:
https://lore.kernel.org/all/[email protected]/

As for me, I don't think we should be addung new BUG() or panic() in general, and especially not for a failing sysfs add. AFAICS create_boot_cache() might panic, but not because of sysfs, as that's delayed until slab_sysfs_init() and we don't panic anymore in the latter.

So yeah, it could work to tell sysfs_slab_add() whether it should not do the kobject_put() as it's a boot cache. The slab_state should work.

Quoted 29 lines
Quoted 28 lines

Looking forward to your advice.
Thanks,

Quoted 24 lines
Quoted 24 lines

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 <[email protected]>.

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                | 52 ++++++++++------------------------------
 3 files changed, 48 insertions(+), 59 deletions(-)
Signature · 1 line
2.25.1