Skip to content

Commit 8598351

Browse files
committed
mm/slab: move and refactor __kmem_cache_alias()
Move __kmem_cache_alias() to slab_common.c since it's called by __kmem_cache_create_args() and calls find_mergeable() that both are in this file. We can remove two slab.h declarations and make them static. Instead declare sysfs_slab_alias() from slub.c so that __kmem_cache_alias() can keep calling it. Add args parameter to __kmem_cache_alias() and find_mergeable() instead of align and ctor. With that we can also move the checks for usersize and sheaf_capacity there from __kmem_cache_create_args() and make the result more symmetric with slab_unmergeable(). No functional changes intended. Reviewed-by: Harry Yoo <harry.yoo@oracle.com> Reviewed-by: Suren Baghdasaryan <surenb@google.com> Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com> Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
1 parent b26e52c commit 8598351

3 files changed

Lines changed: 41 additions & 41 deletions

File tree

mm/slab.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,12 @@ struct kmem_cache {
281281
#define SLAB_SUPPORTS_SYSFS 1
282282
void sysfs_slab_unlink(struct kmem_cache *s);
283283
void sysfs_slab_release(struct kmem_cache *s);
284+
int sysfs_slab_alias(struct kmem_cache *s, const char *name);
284285
#else
285286
static inline void sysfs_slab_unlink(struct kmem_cache *s) { }
286287
static inline void sysfs_slab_release(struct kmem_cache *s) { }
288+
static inline int sysfs_slab_alias(struct kmem_cache *s, const char *name)
289+
{ return 0; }
287290
#endif
288291

289292
void *fixup_red_left(struct kmem_cache *s, void *p);
@@ -400,11 +403,6 @@ extern void create_boot_cache(struct kmem_cache *, const char *name,
400403
unsigned int useroffset, unsigned int usersize);
401404

402405
int slab_unmergeable(struct kmem_cache *s);
403-
struct kmem_cache *find_mergeable(unsigned size, unsigned align,
404-
slab_flags_t flags, const char *name, void (*ctor)(void *));
405-
struct kmem_cache *
406-
__kmem_cache_alias(const char *name, unsigned int size, unsigned int align,
407-
slab_flags_t flags, void (*ctor)(void *));
408406

409407
slab_flags_t kmem_cache_flags(slab_flags_t flags, const char *name);
410408

mm/slab_common.c

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,22 @@ int slab_unmergeable(struct kmem_cache *s)
174174
return 0;
175175
}
176176

177-
struct kmem_cache *find_mergeable(unsigned int size, unsigned int align,
178-
slab_flags_t flags, const char *name, void (*ctor)(void *))
177+
static struct kmem_cache *find_mergeable(unsigned int size, slab_flags_t flags,
178+
const char *name, struct kmem_cache_args *args)
179179
{
180180
struct kmem_cache *s;
181+
unsigned int align;
181182

182183
if (slab_nomerge)
183184
return NULL;
184185

185-
if (ctor)
186+
if (args->ctor)
187+
return NULL;
188+
189+
if (IS_ENABLED(CONFIG_HARDENED_USERCOPY) && args->usersize)
190+
return NULL;
191+
192+
if (args->sheaf_capacity)
186193
return NULL;
187194

188195
flags = kmem_cache_flags(flags, name);
@@ -191,7 +198,7 @@ struct kmem_cache *find_mergeable(unsigned int size, unsigned int align,
191198
return NULL;
192199

193200
size = ALIGN(size, sizeof(void *));
194-
align = calculate_alignment(flags, align, size);
201+
align = calculate_alignment(flags, args->align, size);
195202
size = ALIGN(size, align);
196203

197204
list_for_each_entry_reverse(s, &slab_caches, list) {
@@ -252,6 +259,31 @@ static struct kmem_cache *create_cache(const char *name,
252259
return ERR_PTR(err);
253260
}
254261

262+
static struct kmem_cache *
263+
__kmem_cache_alias(const char *name, unsigned int size, slab_flags_t flags,
264+
struct kmem_cache_args *args)
265+
{
266+
struct kmem_cache *s;
267+
268+
s = find_mergeable(size, flags, name, args);
269+
if (s) {
270+
if (sysfs_slab_alias(s, name))
271+
pr_err("SLUB: Unable to add cache alias %s to sysfs\n",
272+
name);
273+
274+
s->refcount++;
275+
276+
/*
277+
* Adjust the object sizes so that we clear
278+
* the complete object on kzalloc.
279+
*/
280+
s->object_size = max(s->object_size, size);
281+
s->inuse = max(s->inuse, ALIGN(size, sizeof(void *)));
282+
}
283+
284+
return s;
285+
}
286+
255287
/**
256288
* __kmem_cache_create_args - Create a kmem cache.
257289
* @name: A string which is used in /proc/slabinfo to identify this cache.
@@ -323,9 +355,7 @@ struct kmem_cache *__kmem_cache_create_args(const char *name,
323355
object_size - args->usersize < args->useroffset))
324356
args->usersize = args->useroffset = 0;
325357

326-
if (!args->usersize && !args->sheaf_capacity)
327-
s = __kmem_cache_alias(name, object_size, args->align, flags,
328-
args->ctor);
358+
s = __kmem_cache_alias(name, object_size, flags, args);
329359
if (s)
330360
goto out_unlock;
331361

mm/slub.c

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -350,11 +350,8 @@ enum track_item { TRACK_ALLOC, TRACK_FREE };
350350

351351
#ifdef SLAB_SUPPORTS_SYSFS
352352
static int sysfs_slab_add(struct kmem_cache *);
353-
static int sysfs_slab_alias(struct kmem_cache *, const char *);
354353
#else
355354
static inline int sysfs_slab_add(struct kmem_cache *s) { return 0; }
356-
static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p)
357-
{ return 0; }
358355
#endif
359356

360357
#if defined(CONFIG_DEBUG_FS) && defined(CONFIG_SLUB_DEBUG)
@@ -8580,31 +8577,6 @@ void __init kmem_cache_init_late(void)
85808577
WARN_ON(!flushwq);
85818578
}
85828579

8583-
struct kmem_cache *
8584-
__kmem_cache_alias(const char *name, unsigned int size, unsigned int align,
8585-
slab_flags_t flags, void (*ctor)(void *))
8586-
{
8587-
struct kmem_cache *s;
8588-
8589-
s = find_mergeable(size, align, flags, name, ctor);
8590-
if (s) {
8591-
if (sysfs_slab_alias(s, name))
8592-
pr_err("SLUB: Unable to add cache alias %s to sysfs\n",
8593-
name);
8594-
8595-
s->refcount++;
8596-
8597-
/*
8598-
* Adjust the object sizes so that we clear
8599-
* the complete object on kzalloc.
8600-
*/
8601-
s->object_size = max(s->object_size, size);
8602-
s->inuse = max(s->inuse, ALIGN(size, sizeof(void *)));
8603-
}
8604-
8605-
return s;
8606-
}
8607-
86088580
int do_kmem_cache_create(struct kmem_cache *s, const char *name,
86098581
unsigned int size, struct kmem_cache_args *args,
86108582
slab_flags_t flags)
@@ -9837,7 +9809,7 @@ struct saved_alias {
98379809

98389810
static struct saved_alias *alias_list;
98399811

9840-
static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
9812+
int sysfs_slab_alias(struct kmem_cache *s, const char *name)
98419813
{
98429814
struct saved_alias *al;
98439815

0 commit comments

Comments
 (0)