From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Tyler Smith <74526874+tylertms@users.noreply.github.com> Date: Tue, 3 Nov 2026 17:04:38 +0000 Subject: [PATCH] refactor(fs): keep mount allocation and ID ownership together --- fs/mount_cache.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++ fs/mount_cache.h | 7 ++++ fs/namespace.c | 91 ------------------------------------------------ 3 files changed, 97 insertions(+), 91 deletions(-) diff --git a/fs/mount_cache.c b/fs/mount_cache.c index 957bb3d313898110640580f547350258dfe78231..d101435e14babb80694b3f8329c27d5277fece8c 100644 --- a/fs/mount_cache.c +++ b/fs/mount_cache.c @@ -6,6 +6,7 @@ #include #include #include +#include #include "internal.h" #include "mount.h" @@ -22,6 +23,9 @@ unsigned int m_hash_shift __ro_after_init; unsigned int mp_hash_mask __ro_after_init; unsigned int mp_hash_shift __ro_after_init; +static DEFINE_XARRAY_FLAGS(mnt_id_xa, XA_FLAGS_ALLOC); +static u64 mnt_id_ctr = MNT_UNIQUE_ID_OFFSET; + static __initdata unsigned long mhash_entries; static int __init set_mhash_entries(char *str) { @@ -227,3 +231,89 @@ void attach_mnt(struct mount *mnt, struct mount *parent, mnt_set_mountpoint(parent, mp, mnt); make_visible(mnt); } + +static int mnt_alloc_id(struct mount *mnt) +{ + int res; + + xa_lock(&mnt_id_xa); + res = __xa_alloc(&mnt_id_xa, &mnt->mnt_id, mnt, xa_limit_31b, GFP_KERNEL); + if (!res) + mnt->mnt_id_unique = ++mnt_id_ctr; + xa_unlock(&mnt_id_xa); + return res; +} + +void mnt_free_id(struct mount *mnt) +{ + xa_erase(&mnt_id_xa, mnt->mnt_id); +} + +struct mount *alloc_vfsmnt(const char *name) +{ + struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL); + if (mnt) { + int err; + + err = mnt_alloc_id(mnt); + if (err) + goto out_free_cache; + + if (name) + mnt->mnt_devname = kstrdup_const(name, + GFP_KERNEL_ACCOUNT); + else + mnt->mnt_devname = "none"; + if (!mnt->mnt_devname) + goto out_free_id; + +#ifdef CONFIG_SMP + mnt->mnt_pcp = alloc_percpu(struct mnt_pcp); + if (!mnt->mnt_pcp) + goto out_free_devname; + + this_cpu_inc(mnt->mnt_pcp->mnt_gets); +#else + mnt->mnt_count = 1; + mnt->mnt_writers = 0; +#endif + + INIT_HLIST_NODE(&mnt->mnt_hash); + INIT_LIST_HEAD(&mnt->mnt_child); + INIT_LIST_HEAD(&mnt->mnt_mounts); + INIT_LIST_HEAD(&mnt->mnt_list); + INIT_LIST_HEAD(&mnt->mnt_expire); + INIT_LIST_HEAD(&mnt->mnt_share); + INIT_HLIST_HEAD(&mnt->mnt_slave_list); + INIT_HLIST_NODE(&mnt->mnt_slave); + INIT_HLIST_NODE(&mnt->mnt_mp_list); + INIT_HLIST_HEAD(&mnt->mnt_stuck_children); + INIT_HLIST_NODE(&mnt->mnt_ns_visible); +#ifdef CONFIG_FSNOTIFY + INIT_LIST_HEAD(&mnt->to_notify); +#endif + RB_CLEAR_NODE(&mnt->mnt_node); + mnt->mnt.mnt_idmap = &nop_mnt_idmap; + } + return mnt; + +#ifdef CONFIG_SMP +out_free_devname: + kfree_const(mnt->mnt_devname); +#endif +out_free_id: + mnt_free_id(mnt); +out_free_cache: + kmem_cache_free(mnt_cache, mnt); + return NULL; +} + +void free_vfsmnt(struct mount *mnt) +{ + mnt_idmap_put(mnt_idmap(&mnt->mnt)); + kfree_const(mnt->mnt_devname); +#ifdef CONFIG_SMP + free_percpu(mnt->mnt_pcp); +#endif + kmem_cache_free(mnt_cache, mnt); +} diff --git a/fs/mount_cache.h b/fs/mount_cache.h index 8c31a81dba9ef62452d3d42a96299da1436cfa2e..cf9f5bc37a078ee43a4b3dbdee5ac0ac315c7e62 100644 --- a/fs/mount_cache.h +++ b/fs/mount_cache.h @@ -14,6 +14,13 @@ extern struct hlist_head *mount_hashtable; extern struct hlist_head *mountpoint_hashtable; extern struct kmem_cache *mnt_cache; +/* Keep unique IDs separate from the legacy 32-bit IDs. */ +#define MNT_UNIQUE_ID_OFFSET (1ULL << 31) + +struct mount *alloc_vfsmnt(const char *name); +void mnt_free_id(struct mount *mnt); +void free_vfsmnt(struct mount *mnt); + void __init mnt_cache_init(void); struct pinned_mountpoint { diff --git a/fs/namespace.c b/fs/namespace.c index 4f560aa698dcade98a638804e93cc8eddf8831f9..21c76e8b26de7123a912ad0169019e9dc94a7b5a 100644 --- a/fs/namespace.c +++ b/fs/namespace.c @@ -52,13 +52,8 @@ static int __init initramfs_options_setup(char *str) __setup("initramfs_options=", initramfs_options_setup); static u64 event; -static DEFINE_XARRAY_FLAGS(mnt_id_xa, XA_FLAGS_ALLOC); static DEFINE_IDA(mnt_group_ida); -/* Don't allow confusion with old 32bit mount ID */ -#define MNT_UNIQUE_ID_OFFSET (1ULL << 31) -static u64 mnt_id_ctr = MNT_UNIQUE_ID_OFFSET; - static DECLARE_RWSEM(namespace_sem); static HLIST_HEAD(unmounted); /* protected by namespace_sem */ static LIST_HEAD(ex_mountpoints); /* protected by namespace_sem */ @@ -172,23 +167,6 @@ static inline void unlock_mount_hash(void) write_sequnlock(&mount_lock); } -static int mnt_alloc_id(struct mount *mnt) -{ - int res; - - xa_lock(&mnt_id_xa); - res = __xa_alloc(&mnt_id_xa, &mnt->mnt_id, mnt, xa_limit_31b, GFP_KERNEL); - if (!res) - mnt->mnt_id_unique = ++mnt_id_ctr; - xa_unlock(&mnt_id_xa); - return res; -} - -static void mnt_free_id(struct mount *mnt) -{ - xa_erase(&mnt_id_xa, mnt->mnt_id); -} - /* * Allocate a new peer group ID */ @@ -244,65 +222,6 @@ int mnt_get_count(struct mount *mnt) #endif } -static struct mount *alloc_vfsmnt(const char *name) -{ - struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL); - if (mnt) { - int err; - - err = mnt_alloc_id(mnt); - if (err) - goto out_free_cache; - - if (name) - mnt->mnt_devname = kstrdup_const(name, - GFP_KERNEL_ACCOUNT); - else - mnt->mnt_devname = "none"; - if (!mnt->mnt_devname) - goto out_free_id; - -#ifdef CONFIG_SMP - mnt->mnt_pcp = alloc_percpu(struct mnt_pcp); - if (!mnt->mnt_pcp) - goto out_free_devname; - - this_cpu_inc(mnt->mnt_pcp->mnt_gets); -#else - mnt->mnt_count = 1; - mnt->mnt_writers = 0; -#endif - - INIT_HLIST_NODE(&mnt->mnt_hash); - INIT_LIST_HEAD(&mnt->mnt_child); - INIT_LIST_HEAD(&mnt->mnt_mounts); - INIT_LIST_HEAD(&mnt->mnt_list); - INIT_LIST_HEAD(&mnt->mnt_expire); - INIT_LIST_HEAD(&mnt->mnt_share); - INIT_HLIST_HEAD(&mnt->mnt_slave_list); - INIT_HLIST_NODE(&mnt->mnt_slave); - INIT_HLIST_NODE(&mnt->mnt_mp_list); - INIT_HLIST_HEAD(&mnt->mnt_stuck_children); - INIT_HLIST_NODE(&mnt->mnt_ns_visible); -#ifdef CONFIG_FSNOTIFY - INIT_LIST_HEAD(&mnt->to_notify); -#endif - RB_CLEAR_NODE(&mnt->mnt_node); - mnt->mnt.mnt_idmap = &nop_mnt_idmap; - } - return mnt; - -#ifdef CONFIG_SMP -out_free_devname: - kfree_const(mnt->mnt_devname); -#endif -out_free_id: - mnt_free_id(mnt); -out_free_cache: - kmem_cache_free(mnt_cache, mnt); - return NULL; -} - /* * Most r/o checks on a fs are for operations that take * discrete amounts of time, like a write() or unlink(). @@ -687,16 +606,6 @@ int sb_prepare_remount_readonly(struct super_block *sb) return err; } -static void free_vfsmnt(struct mount *mnt) -{ - mnt_idmap_put(mnt_idmap(&mnt->mnt)); - kfree_const(mnt->mnt_devname); -#ifdef CONFIG_SMP - free_percpu(mnt->mnt_pcp); -#endif - kmem_cache_free(mnt_cache, mnt); -} - static void delayed_free_vfsmnt(struct rcu_head *head) { free_vfsmnt(container_of(head, struct mount, mnt_rcu));