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

From: Liu Shixin
Date: Mon Oct 31 2022 - 08:59:49 EST


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.

Signed-off-by: Liu Shixin <liushixin2@xxxxxxxxxx>
---
mm/slab_common.c | 34 ++++++++++++++--------------------
1 file changed, 14 insertions(+), 20 deletions(-)

diff --git a/mm/slab_common.c b/mm/slab_common.c
index 33b1886b06eb..e5f430a17d95 100644
--- a/mm/slab_common.c
+++ b/mm/slab_common.c
@@ -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);
--
2.25.1