Skip to contentSkip to conversation

mm: Fix kobject memleak in SLUB

PATCH In review Tobin C. Harding 4 participants 4 messages Apr 27, 2019 – Apr 30, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: Fix kobject memleak in SLUB

Currently error return from kobject_init_and_add() is not followed by a call to kobject_put(). This means there is a memory leak.

Add call to kobject_put() in error path of kobject_init_and_add().

---
 mm/slub.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
mm/slub.c +3 -1
index d30ede89f4a6..84a9d6c06c27 100644
@@ -5756,8 +5756,10 @@ static int sysfs_slab_add(struct kmem_cache *s)
 
 	s->kobj.kset = kset;
 	err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, "%s", name);
-	if (err)
+	if (err) {
+		kobject_put(&s->kobj);
 		goto out;
+	}
 
 	err = sysfs_create_group(&s->kobj, &slab_attr_group);
 	if (err)
Signature · 1 line
2.21.0
#3 lore raw
In reply to Tobin C. Harding
Quoted 29 lines · Tobin C. Harding

Currently error return from kobject_init_and_add() is not followed by a call to kobject_put(). This means there is a memory leak.

Add call to kobject_put() in error path of kobject_init_and_add().

---
 mm/slub.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
mm/slub.c +3 -1
index d30ede89f4a6..84a9d6c06c27 100644
@@ -5756,8 +5756,10 @@ static int sysfs_slab_add(struct kmem_cache *s)
 
 	s->kobj.kset = kset;
 	err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, "%s", name);
-	if (err)
+	if (err) {
+		kobject_put(&s->kobj);
 		goto out;
+	}
 
 	err = sysfs_create_group(&s->kobj, &slab_attr_group);
 	if (err)
Signature · 1 line
2.21.0

This patch is not _completely_ correct. Please do not consider for merge. There are a bunch of these on various LKML lists, once the confusion has cleared I'll re-spin v2.

thanks,
Tobin.