[PATCH 1/5] slab: Introduce kmem_buckets_destroy()

From: Kees Cook
Date: Fri Aug 09 2024 - 03:33:23 EST


Modular use of kmem_buckets_create() means that kmem_buckets will need
to be removed as well. Introduce kmem_buckets_destroy(), matching
kmem_cache_destroy().

Signed-off-by: Kees Cook <kees@xxxxxxxxxx>
---
Cc: Vlastimil Babka <vbabka@xxxxxxx>
Cc: Christoph Lameter <cl@xxxxxxxxx>
Cc: Pekka Enberg <penberg@xxxxxxxxxx>
Cc: David Rientjes <rientjes@xxxxxxxxxx>
Cc: Joonsoo Kim <iamjoonsoo.kim@xxxxxxx>
Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Cc: Roman Gushchin <roman.gushchin@xxxxxxxxx>
Cc: Hyeonggon Yoo <42.hyeyoo@xxxxxxxxx>
Cc: linux-mm@xxxxxxxxx
---
include/linux/slab.h | 1 +
mm/slab_common.c | 17 ++++++++++++++---
2 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/include/linux/slab.h b/include/linux/slab.h
index eb2bf4629157..86cb61a0102c 100644
--- a/include/linux/slab.h
+++ b/include/linux/slab.h
@@ -552,6 +552,7 @@ void kmem_cache_free(struct kmem_cache *s, void *objp);
kmem_buckets *kmem_buckets_create(const char *name, slab_flags_t flags,
unsigned int useroffset, unsigned int usersize,
void (*ctor)(void *));
+void kmem_buckets_destroy(kmem_buckets *b);

/*
* Bulk allocation and freeing operations. These are accelerated in an
diff --git a/mm/slab_common.c b/mm/slab_common.c
index 40b582a014b8..fc698cba0ebe 100644
--- a/mm/slab_common.c
+++ b/mm/slab_common.c
@@ -392,6 +392,19 @@ kmem_cache_create(const char *name, unsigned int size, unsigned int align,
}
EXPORT_SYMBOL(kmem_cache_create);

+void kmem_buckets_destroy(kmem_buckets *b)
+{
+ int idx;
+
+ if (!b)
+ return;
+
+ for (idx = 0; idx < ARRAY_SIZE(kmalloc_caches[KMALLOC_NORMAL]); idx++)
+ kmem_cache_destroy((*b)[idx]);
+ kfree(b);
+}
+EXPORT_SYMBOL(kmem_buckets_destroy);
+
static struct kmem_cache *kmem_buckets_cache __ro_after_init;

/**
@@ -476,9 +489,7 @@ kmem_buckets *kmem_buckets_create(const char *name, slab_flags_t flags,
return b;

fail:
- for (idx = 0; idx < ARRAY_SIZE(kmalloc_caches[KMALLOC_NORMAL]); idx++)
- kmem_cache_destroy((*b)[idx]);
- kfree(b);
+ kmem_buckets_destroy(b);

return NULL;
}
--
2.34.1