Skip to content

Commit 913ffd3

Browse files
committed
slab: handle kmalloc sheaves bootstrap
Enable sheaves for kmalloc caches. For other types than KMALLOC_NORMAL, we can simply allow them in calculate_sizes() as they are created later than KMALLOC_NORMAL caches and can allocate sheaves and barns from those. For KMALLOC_NORMAL caches we perform additional step after first creating them without sheaves. Then bootstrap_cache_sheaves() simply allocates and initializes barns and sheaves and finally sets s->sheaf_capacity to make them actually used. Afterwards the only caches left without sheaves (unless SLUB_TINY or debugging is enabled) are kmem_cache and kmem_cache_node. These are only used when creating or destroying other kmem_caches. Thus they are not performance critical and we can simply leave it that way. Reviewed-by: Harry Yoo <harry.yoo@oracle.com> Reviewed-by: Hao Li <hao.li@linux.dev> Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com> Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
1 parent f1427a1 commit 913ffd3

1 file changed

Lines changed: 84 additions & 4 deletions

File tree

mm/slub.c

Lines changed: 84 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2593,7 +2593,8 @@ static void *setup_object(struct kmem_cache *s, void *object)
25932593
return object;
25942594
}
25952595

2596-
static struct slab_sheaf *alloc_empty_sheaf(struct kmem_cache *s, gfp_t gfp)
2596+
static struct slab_sheaf *__alloc_empty_sheaf(struct kmem_cache *s, gfp_t gfp,
2597+
unsigned int capacity)
25972598
{
25982599
struct slab_sheaf *sheaf;
25992600
size_t sheaf_size;
@@ -2611,7 +2612,7 @@ static struct slab_sheaf *alloc_empty_sheaf(struct kmem_cache *s, gfp_t gfp)
26112612
if (s->flags & SLAB_KMALLOC)
26122613
gfp |= __GFP_NO_OBJ_EXT;
26132614

2614-
sheaf_size = struct_size(sheaf, objects, s->sheaf_capacity);
2615+
sheaf_size = struct_size(sheaf, objects, capacity);
26152616
sheaf = kzalloc(sheaf_size, gfp);
26162617

26172618
if (unlikely(!sheaf))
@@ -2624,6 +2625,12 @@ static struct slab_sheaf *alloc_empty_sheaf(struct kmem_cache *s, gfp_t gfp)
26242625
return sheaf;
26252626
}
26262627

2628+
static inline struct slab_sheaf *alloc_empty_sheaf(struct kmem_cache *s,
2629+
gfp_t gfp)
2630+
{
2631+
return __alloc_empty_sheaf(s, gfp, s->sheaf_capacity);
2632+
}
2633+
26272634
static void free_empty_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf)
26282635
{
26292636
kfree(sheaf);
@@ -8157,8 +8164,11 @@ static int calculate_sizes(struct kmem_cache_args *args, struct kmem_cache *s)
81578164
if (s->flags & SLAB_RECLAIM_ACCOUNT)
81588165
s->allocflags |= __GFP_RECLAIMABLE;
81598166

8160-
/* kmalloc caches need extra care to support sheaves */
8161-
if (!is_kmalloc_cache(s))
8167+
/*
8168+
* For KMALLOC_NORMAL caches we enable sheaves later by
8169+
* bootstrap_kmalloc_sheaves() to avoid recursion
8170+
*/
8171+
if (!is_kmalloc_normal(s))
81628172
s->sheaf_capacity = calculate_sheaf_capacity(s, args);
81638173

81648174
/*
@@ -8653,6 +8663,74 @@ static struct kmem_cache * __init bootstrap(struct kmem_cache *static_cache)
86538663
return s;
86548664
}
86558665

8666+
/*
8667+
* Finish the sheaves initialization done normally by init_percpu_sheaves() and
8668+
* init_kmem_cache_nodes(). For normal kmalloc caches we have to bootstrap it
8669+
* since sheaves and barns are allocated by kmalloc.
8670+
*/
8671+
static void __init bootstrap_cache_sheaves(struct kmem_cache *s)
8672+
{
8673+
struct kmem_cache_args empty_args = {};
8674+
unsigned int capacity;
8675+
bool failed = false;
8676+
int node, cpu;
8677+
8678+
capacity = calculate_sheaf_capacity(s, &empty_args);
8679+
8680+
/* capacity can be 0 due to debugging or SLUB_TINY */
8681+
if (!capacity)
8682+
return;
8683+
8684+
for_each_node_mask(node, slab_nodes) {
8685+
struct node_barn *barn;
8686+
8687+
barn = kmalloc_node(sizeof(*barn), GFP_KERNEL, node);
8688+
8689+
if (!barn) {
8690+
failed = true;
8691+
goto out;
8692+
}
8693+
8694+
barn_init(barn);
8695+
get_node(s, node)->barn = barn;
8696+
}
8697+
8698+
for_each_possible_cpu(cpu) {
8699+
struct slub_percpu_sheaves *pcs;
8700+
8701+
pcs = per_cpu_ptr(s->cpu_sheaves, cpu);
8702+
8703+
pcs->main = __alloc_empty_sheaf(s, GFP_KERNEL, capacity);
8704+
8705+
if (!pcs->main) {
8706+
failed = true;
8707+
break;
8708+
}
8709+
}
8710+
8711+
out:
8712+
/*
8713+
* It's still early in boot so treat this like same as a failure to
8714+
* create the kmalloc cache in the first place
8715+
*/
8716+
if (failed)
8717+
panic("Out of memory when creating kmem_cache %s\n", s->name);
8718+
8719+
s->sheaf_capacity = capacity;
8720+
}
8721+
8722+
static void __init bootstrap_kmalloc_sheaves(void)
8723+
{
8724+
enum kmalloc_cache_type type;
8725+
8726+
for (type = KMALLOC_NORMAL; type <= KMALLOC_RANDOM_END; type++) {
8727+
for (int idx = 0; idx < KMALLOC_SHIFT_HIGH + 1; idx++) {
8728+
if (kmalloc_caches[type][idx])
8729+
bootstrap_cache_sheaves(kmalloc_caches[type][idx]);
8730+
}
8731+
}
8732+
}
8733+
86568734
void __init kmem_cache_init(void)
86578735
{
86588736
static __initdata struct kmem_cache boot_kmem_cache,
@@ -8696,6 +8774,8 @@ void __init kmem_cache_init(void)
86968774
setup_kmalloc_cache_index_table();
86978775
create_kmalloc_caches();
86988776

8777+
bootstrap_kmalloc_sheaves();
8778+
86998779
/* Setup random freelists for each cache */
87008780
init_freelist_randomization();
87018781

0 commit comments

Comments
 (0)