[Concept,01/15] lib: Add CONFIG_LIB_KMEM_CACHE for full kmem_cache support

Message ID 20251230234134.906477-2-sjg@u-boot.org
State New
Headers
Series ext4l: Infrastructure and fixes for write support (part K) |

Commit Message

Simon Glass Dec. 30, 2025, 11:41 p.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

Add a Kconfig option to control whether full kmem_cache_free() and
kmem_cache_destroy() implementations are provided in lib/linux_compat.c

Most boards do not need these functions, so they can use simple inline
stubs in slab.h. Subsystems like ext4 that require proper cache
management can select CONFIG_LIB_KMEM_CACHE.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---

 include/linux/slab.h | 27 ++++++++++++++++-----------
 lib/Kconfig          |  8 ++++++++
 lib/Makefile         |  1 +
 lib/kmem_cache.c     | 20 ++++++++++++++++++++
 4 files changed, 45 insertions(+), 11 deletions(-)
 create mode 100644 lib/kmem_cache.c
  

Patch

diff --git a/include/linux/slab.h b/include/linux/slab.h
index 2b374641534..caaaa3d9e16 100644
--- a/include/linux/slab.h
+++ b/include/linux/slab.h
@@ -84,24 +84,19 @@  static inline void *krealloc(const void *p, size_t new_size, gfp_t flags)
 
 void *kmemdup(const void *src, size_t len, gfp_t gfp);
 
-/* kmem_cache stubs */
+/* kmem_cache implementation / stubs */
 struct kmem_cache {
 	int sz;
 };
 
 struct kmem_cache *get_mem(int element_sz);
-#define kmem_cache_create(a, sz, c, d, e)	({ (void)(a); (void)(e); get_mem(sz); })
+#define kmem_cache_create(a, sz, c, d, e)	get_mem(sz)
 void *kmem_cache_alloc(struct kmem_cache *obj, gfp_t flag);
 
-static inline void *kmem_cache_zalloc(struct kmem_cache *obj, gfp_t flags)
-{
-	void *ret = kmem_cache_alloc(obj, flags);
-
-	if (ret)
-		memset(ret, 0, obj->sz);
-	return ret;
-}
-
+#if CONFIG_IS_ENABLED(LIB_KMEM_CACHE)
+void kmem_cache_free(struct kmem_cache *cachep, void *obj);
+void kmem_cache_destroy(struct kmem_cache *cachep);
+#else
 static inline void kmem_cache_free(struct kmem_cache *cachep, void *obj)
 {
 	free(obj);
@@ -111,5 +106,15 @@  static inline void kmem_cache_destroy(struct kmem_cache *cachep)
 {
 	free(cachep);
 }
+#endif
+
+static inline void *kmem_cache_zalloc(struct kmem_cache *obj, gfp_t flags)
+{
+	void *ret = kmem_cache_alloc(obj, flags);
+
+	if (ret)
+		memset(ret, 0, obj->sz);
+	return ret;
+}
 
 #endif /* _LINUX_SLAB_H */
diff --git a/lib/Kconfig b/lib/Kconfig
index 9c7eb27c392..5ddf8125766 100644
--- a/lib/Kconfig
+++ b/lib/Kconfig
@@ -396,6 +396,14 @@  config TPL_TINY_MEMSET
 config RBTREE
 	bool
 
+config LIB_KMEM_CACHE
+	bool "Enable full kmem_cache implementation"
+	help
+	  Provide a proper kmem_cache implementation in lib/linux_compat.c
+	  that tracks allocated objects. This is needed by subsystems like
+	  ext4 that require cache management. When disabled, simple inline
+	  stubs are used instead.
+
 config BITREVERSE
 	bool "Bit reverse library from Linux"
 
diff --git a/lib/Makefile b/lib/Makefile
index 15a43b2cc5e..01b967d46b7 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -135,6 +135,7 @@  obj-$(CONFIG_$(PHASE_)OF_LIBFDT) += fdtdec.o fdtdec_common.o fdt_print.o
 obj-y += hang.o
 obj-y += linux_compat.o
 obj-y += linux_string.o
+obj-$(CONFIG_$(PHASE_)LIB_KMEM_CACHE) += kmem_cache.o
 obj-$(CONFIG_$(PHASE_)LMB) += lmb.o
 obj-y += membuf.o
 obj-$(CONFIG_REGEX) += slre.o
diff --git a/lib/kmem_cache.c b/lib/kmem_cache.c
new file mode 100644
index 00000000000..bff9329aa53
--- /dev/null
+++ b/lib/kmem_cache.c
@@ -0,0 +1,20 @@ 
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * kmem_cache implementation for U-Boot
+ *
+ * Copyright 2025 Canonical Ltd
+ * Written by Simon Glass <simon.glass@canonical.com>
+ */
+
+#include <malloc.h>
+#include <linux/slab.h>
+
+void kmem_cache_free(struct kmem_cache *cachep, void *obj)
+{
+	free(obj);
+}
+
+void kmem_cache_destroy(struct kmem_cache *cachep)
+{
+	free(cachep);
+}