]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
nscommon: simplify initialization
authorChristian Brauner <brauner@kernel.org>
Wed, 17 Sep 2025 10:28:07 +0000 (12:28 +0200)
committerChristian Brauner <brauner@kernel.org>
Fri, 19 Sep 2025 12:26:19 +0000 (14:26 +0200)
There's a lot of information that namespace implementers don't need to
know about at all. Encapsulate this all in the initialization helper.

Reviewed-by: Jan Kara <jack@suse.cz>
Signed-off-by: Christian Brauner <brauner@kernel.org>
fs/namespace.c
include/linux/ns_common.h
ipc/namespace.c
kernel/cgroup/namespace.c
kernel/nscommon.c
kernel/pid_namespace.c
kernel/time/namespace.c
kernel/user_namespace.c
kernel/utsname.c
net/core/net_namespace.c

index b2fcb901ad8c1e3f7b78055f1d4c858a71eb3de1..699b8c770c47dab626661f4c285074af60279f63 100644 (file)
@@ -4104,8 +4104,9 @@ static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool a
        }
 
        if (anon)
-               new_ns->ns.inum = MNT_NS_ANON_INO;
-       ret = ns_common_init(&new_ns->ns, &mntns_operations, !anon);
+               ret = ns_common_init_inum(new_ns, &mntns_operations, MNT_NS_ANON_INO);
+       else
+               ret = ns_common_init(new_ns, &mntns_operations);
        if (ret) {
                kfree(new_ns);
                dec_mnt_namespaces(ucounts);
index 78b17fe80b62d4a2466ba7ba260d47fe506bc087..05c7a7dd211bd37f61bd05ab9b639586ee91178a 100644 (file)
@@ -16,6 +16,15 @@ struct time_namespace;
 struct user_namespace;
 struct uts_namespace;
 
+extern struct cgroup_namespace init_cgroup_ns;
+extern struct ipc_namespace init_ipc_ns;
+extern struct mnt_namespace init_mnt_ns;
+extern struct net init_net;
+extern struct pid_namespace init_pid_ns;
+extern struct time_namespace init_time_ns;
+extern struct user_namespace init_user_ns;
+extern struct uts_namespace init_uts_ns;
+
 struct ns_common {
        struct dentry *stashed;
        const struct proc_ns_operations *ops;
@@ -31,8 +40,7 @@ struct ns_common {
        };
 };
 
-int ns_common_init(struct ns_common *ns, const struct proc_ns_operations *ops,
-                  bool alloc_inum);
+int __ns_common_init(struct ns_common *ns, const struct proc_ns_operations *ops, int inum);
 
 #define to_ns_common(__ns)                              \
        _Generic((__ns),                                \
@@ -45,4 +53,31 @@ int ns_common_init(struct ns_common *ns, const struct proc_ns_operations *ops,
                struct user_namespace *:   &(__ns)->ns, \
                struct uts_namespace *:    &(__ns)->ns)
 
+#define ns_init_inum(__ns)                                     \
+       _Generic((__ns),                                       \
+               struct cgroup_namespace *: CGROUP_NS_INIT_INO, \
+               struct ipc_namespace *:    IPC_NS_INIT_INO,    \
+               struct mnt_namespace *:    MNT_NS_INIT_INO,    \
+               struct net *:              NET_NS_INIT_INO,    \
+               struct pid_namespace *:    PID_NS_INIT_INO,    \
+               struct time_namespace *:   TIME_NS_INIT_INO,   \
+               struct user_namespace *:   USER_NS_INIT_INO,   \
+               struct uts_namespace *:    UTS_NS_INIT_INO)
+
+#define ns_init_ns(__ns)                                    \
+       _Generic((__ns),                                    \
+               struct cgroup_namespace *: &init_cgroup_ns, \
+               struct ipc_namespace *:    &init_ipc_ns,    \
+               struct mnt_namespace *:    &init_mnt_ns,     \
+               struct net *:              &init_net,       \
+               struct pid_namespace *:    &init_pid_ns,    \
+               struct time_namespace *:   &init_time_ns,   \
+               struct user_namespace *:   &init_user_ns,   \
+               struct uts_namespace *:    &init_uts_ns)
+
+#define ns_common_init(__ns, __ops) \
+       __ns_common_init(to_ns_common(__ns), __ops, (((__ns) == ns_init_ns(__ns)) ? ns_init_inum(__ns) : 0))
+
+#define ns_common_init_inum(__ns, __ops, __inum) __ns_common_init(to_ns_common(__ns), __ops, __inum)
+
 #endif
index 89588819956b2042b8626c5fbe908e861b0ff85a..0f8bbd18a47580c3a4a50f422ac8dd2fb81a0cbf 100644 (file)
@@ -62,7 +62,7 @@ static struct ipc_namespace *create_ipc_ns(struct user_namespace *user_ns,
        if (ns == NULL)
                goto fail_dec;
 
-       err = ns_common_init(&ns->ns, &ipcns_operations, true);
+       err = ns_common_init(ns, &ipcns_operations);
        if (err)
                goto fail_free;
 
index 5a327914b5654ebdf755843ee26d5a4c062a0d8e..d928c557e28b1a7c39dbee1046768a2cf35289f9 100644 (file)
@@ -27,7 +27,7 @@ static struct cgroup_namespace *alloc_cgroup_ns(void)
        new_ns = kzalloc(sizeof(struct cgroup_namespace), GFP_KERNEL_ACCOUNT);
        if (!new_ns)
                return ERR_PTR(-ENOMEM);
-       ret = ns_common_init(&new_ns->ns, &cgroupns_operations, true);
+       ret = ns_common_init(new_ns, &cgroupns_operations);
        if (ret)
                return ERR_PTR(ret);
        ns_tree_add(new_ns);
index e10fad8afe61b745e74c5d0b984a645ebecebae3..c3a90bb665ad671d08577b3808e1eb8d8c97b641 100644 (file)
@@ -1,21 +1,20 @@
 // SPDX-License-Identifier: GPL-2.0-only
 
 #include <linux/ns_common.h>
+#include <linux/proc_ns.h>
 
-int ns_common_init(struct ns_common *ns, const struct proc_ns_operations *ops,
-                  bool alloc_inum)
+int __ns_common_init(struct ns_common *ns, const struct proc_ns_operations *ops, int inum)
 {
-       if (alloc_inum && !ns->inum) {
-               int ret;
-               ret = proc_alloc_inum(&ns->inum);
-               if (ret)
-                       return ret;
-       }
        refcount_set(&ns->count, 1);
        ns->stashed = NULL;
        ns->ops = ops;
        ns->ns_id = 0;
        RB_CLEAR_NODE(&ns->ns_tree_node);
        INIT_LIST_HEAD(&ns->ns_list_node);
-       return 0;
+
+       if (inum) {
+               ns->inum = inum;
+               return 0;
+       }
+       return proc_alloc_inum(&ns->inum);
 }
index 9b327420309e8b5f25e62f77b9356dff92de132d..170757c265c299166c50728dbdf670af35e2eca3 100644 (file)
@@ -103,7 +103,7 @@ static struct pid_namespace *create_pid_namespace(struct user_namespace *user_ns
        if (ns->pid_cachep == NULL)
                goto out_free_idr;
 
-       err = ns_common_init(&ns->ns, &pidns_operations, true);
+       err = ns_common_init(ns, &pidns_operations);
        if (err)
                goto out_free_idr;
 
index 20b65f90549e20b739da6831d08ba40a87a8e992..ce8e952104a7e7bd81a63d30553e6ec09e117a99 100644 (file)
@@ -97,7 +97,7 @@ static struct time_namespace *clone_time_ns(struct user_namespace *user_ns,
        if (!ns->vvar_page)
                goto fail_free;
 
-       err = ns_common_init(&ns->ns, &timens_operations, true);
+       err = ns_common_init(ns, &timens_operations);
        if (err)
                goto fail_free_page;
 
index cfb0e28f2779c9bf6b49327dfa58165e02815686..db9f0463219c169a973aae221ca27851750a061a 100644 (file)
@@ -126,7 +126,7 @@ int create_user_ns(struct cred *new)
 
        ns->parent_could_setfcap = cap_raised(new->cap_effective, CAP_SETFCAP);
 
-       ret = ns_common_init(&ns->ns, &userns_operations, true);
+       ret = ns_common_init(ns, &userns_operations);
        if (ret)
                goto fail_free;
 
index a682830742d3442e542bb89c91af8e9962b98980..399888be66bd1f749a2fb7d68077cc8f878dbc18 100644 (file)
@@ -50,7 +50,7 @@ static struct uts_namespace *clone_uts_ns(struct user_namespace *user_ns,
        if (!ns)
                goto fail_dec;
 
-       err = ns_common_init(&ns->ns, &utsns_operations, true);
+       err = ns_common_init(ns, &utsns_operations);
        if (err)
                goto fail_free;
 
index 9df23681145470c92c8187da19b72172a924603f..e50897fba8cde163e086c07de29e76e42ff987ce 100644 (file)
@@ -409,7 +409,7 @@ static __net_init int preinit_net(struct net *net, struct user_namespace *user_n
        ns_ops = NULL;
 #endif
 
-       ret = ns_common_init(&net->ns, ns_ops, true);
+       ret = ns_common_init(net, ns_ops);
        if (ret)
                return ret;