[RFC PATCH v3 09/10] make thread component caches (fs_cachep, files_cachep, etc.) statically allocated

From: Al Viro

Date: Sat Jun 13 2026 - 01:10:13 EST


All of them are created in kernel/fork.c, might as well convert all
at once...

Signed-off-by: Al Viro <viro@xxxxxxxxxxxxxxxxxx>
---
include/linux/fdtable.h | 3 ++-
include/linux/fs_struct.h | 3 ++-
include/linux/signal.h | 3 ++-
kernel/fork.c | 37 +++++++++++++++++++++----------------
4 files changed, 27 insertions(+), 19 deletions(-)

diff --git a/include/linux/fdtable.h b/include/linux/fdtable.h
index c45306a9f007..f2d553f99c58 100644
--- a/include/linux/fdtable.h
+++ b/include/linux/fdtable.h
@@ -113,6 +113,7 @@ int iterate_fd(struct files_struct *, unsigned,
extern int close_fd(unsigned int fd);
extern struct file *file_close_fd(unsigned int fd);

-extern struct kmem_cache *files_cachep;
+extern struct kmem_cache_opaque files_cache;
+#define files_cachep to_kmem_cache(&files_cache)

#endif /* __LINUX_FDTABLE_H */
diff --git a/include/linux/fs_struct.h b/include/linux/fs_struct.h
index 0070764b790a..e8c9fac5b7b7 100644
--- a/include/linux/fs_struct.h
+++ b/include/linux/fs_struct.h
@@ -15,7 +15,8 @@ struct fs_struct {
struct path root, pwd;
} __randomize_layout;

-extern struct kmem_cache *fs_cachep;
+extern struct kmem_cache_opaque fs_struct_cache;
+#define fs_cachep to_kmem_cache(&fs_struct_cache)

extern void exit_fs(struct task_struct *);
extern void set_fs_root(struct fs_struct *, const struct path *);
diff --git a/include/linux/signal.h b/include/linux/signal.h
index f19816832f05..a0c7fee8b22a 100644
--- a/include/linux/signal.h
+++ b/include/linux/signal.h
@@ -323,7 +323,8 @@ static inline void disallow_signal(int sig)
kernel_sigaction(sig, SIG_IGN);
}

-extern struct kmem_cache *sighand_cachep;
+extern struct kmem_cache_opaque sighand_cache;
+#define sighand_cachep to_kmem_cache(&sighand_cache)

extern bool unhandled_signal(struct task_struct *tsk, int sig);

diff --git a/kernel/fork.c b/kernel/fork.c
index 8ac38beae360..618f19bb482a 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -111,6 +111,7 @@
#include <linux/unwind_deferred.h>
#include <linux/pgalloc.h>
#include <linux/uaccess.h>
+#include <linux/slab-static.h>

#include <asm/mmu_context.h>
#include <asm/cacheflush.h>
@@ -180,7 +181,8 @@ void __weak arch_release_task_struct(struct task_struct *tsk)
{
}

-static struct kmem_cache *task_struct_cachep;
+static struct kmem_cache_opaque task_struct_cache;
+#define task_struct_cachep to_kmem_cache(&task_struct_cache)

static inline struct task_struct *alloc_task_struct_node(int node)
{
@@ -425,7 +427,8 @@ static void free_thread_stack(struct task_struct *tsk)

#else /* !(THREAD_SIZE >= PAGE_SIZE) */

-static struct kmem_cache *thread_stack_cache;
+static struct kmem_cache_opaque __thread_stack_cache;
+#define thread_stack_cache to_kmem_cache(&__thread_stack_cache)

static void thread_stack_free_rcu(struct rcu_head *rh)
{
@@ -456,29 +459,31 @@ static void free_thread_stack(struct task_struct *tsk)

void thread_stack_cache_init(void)
{
- thread_stack_cache = kmem_cache_create_usercopy("thread_stack",
- THREAD_SIZE, THREAD_SIZE, 0, 0,
+ kmem_cache_setup_usercopy(thread_stack_cache, "thread_stack",
+ THREAD_SIZE, THREAD_SIZE,
+ SLAB_PANIC, 0,
THREAD_SIZE, NULL);
- BUG_ON(thread_stack_cache == NULL);
}

#endif /* THREAD_SIZE >= PAGE_SIZE */
#endif /* CONFIG_VMAP_STACK */

/* SLAB cache for signal_struct structures (tsk->signal) */
-static struct kmem_cache *signal_cachep;
+static struct kmem_cache_opaque signal_cache;
+#define signal_cachep to_kmem_cache(&signal_cache)

/* SLAB cache for sighand_struct structures (tsk->sighand) */
-struct kmem_cache *sighand_cachep;
+struct kmem_cache_opaque sighand_cache;

/* SLAB cache for files_struct structures (tsk->files) */
-struct kmem_cache *files_cachep;
+struct kmem_cache_opaque files_cache;

/* SLAB cache for fs_struct structures (tsk->fs) */
-struct kmem_cache *fs_cachep;
+struct kmem_cache_opaque fs_struct_cache;

/* SLAB cache for mm_struct structures (tsk->mm) */
-static struct kmem_cache *mm_cachep;
+static struct kmem_cache_opaque mm_cache;
+#define mm_cachep to_kmem_cache(&mm_cache)

static void account_kernel_stack(struct task_struct *tsk, int account)
{
@@ -859,7 +864,7 @@ void __init fork_init(void)

/* create a slab on which task_structs can be allocated */
task_struct_whitelist(&useroffset, &usersize);
- task_struct_cachep = kmem_cache_create_usercopy("task_struct",
+ kmem_cache_setup_usercopy(task_struct_cachep, "task_struct",
arch_task_struct_size, align,
SLAB_PANIC|SLAB_ACCOUNT,
useroffset, usersize, NULL);
@@ -3079,7 +3084,7 @@ void __init mm_cache_init(void)
*/
mm_size = sizeof(struct mm_struct) + cpumask_size() + mm_cid_size();

- mm_cachep = kmem_cache_create_usercopy("mm_struct",
+ kmem_cache_setup_usercopy(mm_cachep, "mm_struct",
mm_size, ARCH_MIN_MMSTRUCT_ALIGN,
SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT,
offsetof(struct mm_struct, saved_auxv),
@@ -3089,19 +3094,19 @@ void __init mm_cache_init(void)

void __init proc_caches_init(void)
{
- sighand_cachep = kmem_cache_create("sighand_cache",
+ kmem_cache_setup(sighand_cachep, "sighand_cache",
sizeof(struct sighand_struct), 0,
SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_TYPESAFE_BY_RCU|
SLAB_ACCOUNT, sighand_ctor);
- signal_cachep = kmem_cache_create("signal_cache",
+ kmem_cache_setup(signal_cachep, "signal_cache",
sizeof(struct signal_struct), 0,
SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT,
NULL);
- files_cachep = kmem_cache_create("files_cache",
+ kmem_cache_setup(files_cachep, "files_cache",
sizeof(struct files_struct), 0,
SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT,
NULL);
- fs_cachep = kmem_cache_create("fs_cache",
+ kmem_cache_setup(fs_cachep, "fs_cache",
sizeof(struct fs_struct), 0,
SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT,
NULL);
--
2.47.3