]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
ns: simplify ns_common_init() further
authorChristian Brauner <brauner@kernel.org>
Mon, 22 Sep 2025 12:42:36 +0000 (14:42 +0200)
committerChristian Brauner <brauner@kernel.org>
Mon, 22 Sep 2025 12:47:10 +0000 (14:47 +0200)
Simply derive the ns operations from the namespace type.

Acked-by: Thomas Gleixner <tglx@linutronix.de>
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/pid_namespace.c
kernel/time/namespace.c
kernel/user_namespace.c
kernel/utsname.c
net/core/net_namespace.c

index 271cd6294c8a7388e16ad0ba1a7a973fa5ea7202..d65917ec5544fe382925d918be93da8565340281 100644 (file)
@@ -4104,9 +4104,9 @@ static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool a
        }
 
        if (anon)
-               ret = ns_common_init_inum(new_ns, &mntns_operations, MNT_NS_ANON_INO);
+               ret = ns_common_init_inum(new_ns, MNT_NS_ANON_INO);
        else
-               ret = ns_common_init(new_ns, &mntns_operations);
+               ret = ns_common_init(new_ns);
        if (ret) {
                kfree(new_ns);
                dec_mnt_namespaces(ucounts);
index aea8528d799a60562504c484ca3e0b872a3d949c..56492cd9ff8d43a837d55fd13bf2452f6f988777 100644 (file)
@@ -25,6 +25,17 @@ extern struct time_namespace init_time_ns;
 extern struct user_namespace init_user_ns;
 extern struct uts_namespace init_uts_ns;
 
+extern const struct proc_ns_operations netns_operations;
+extern const struct proc_ns_operations utsns_operations;
+extern const struct proc_ns_operations ipcns_operations;
+extern const struct proc_ns_operations pidns_operations;
+extern const struct proc_ns_operations pidns_for_children_operations;
+extern const struct proc_ns_operations userns_operations;
+extern const struct proc_ns_operations mntns_operations;
+extern const struct proc_ns_operations cgroupns_operations;
+extern const struct proc_ns_operations timens_operations;
+extern const struct proc_ns_operations timens_for_children_operations;
+
 struct ns_common {
        struct dentry *stashed;
        const struct proc_ns_operations *ops;
@@ -84,10 +95,21 @@ void __ns_common_free(struct ns_common *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)
+#define to_ns_operations(__ns)                                                                         \
+       _Generic((__ns),                                                                               \
+               struct cgroup_namespace *: (IS_ENABLED(CONFIG_CGROUPS) ? &cgroupns_operations : NULL), \
+               struct ipc_namespace *:    (IS_ENABLED(CONFIG_IPC_NS)  ? &ipcns_operations    : NULL), \
+               struct mnt_namespace *:    &mntns_operations,                                          \
+               struct net *:              (IS_ENABLED(CONFIG_NET_NS)  ? &netns_operations    : NULL), \
+               struct pid_namespace *:    (IS_ENABLED(CONFIG_PID_NS)  ? &pidns_operations    : NULL), \
+               struct time_namespace *:   (IS_ENABLED(CONFIG_TIME_NS) ? &timens_operations   : NULL), \
+               struct user_namespace *:   (IS_ENABLED(CONFIG_USER_NS) ? &userns_operations   : NULL), \
+               struct uts_namespace *:    (IS_ENABLED(CONFIG_UTS_NS)  ? &utsns_operations    : NULL))
+
+#define ns_common_init(__ns) \
+       __ns_common_init(to_ns_common(__ns), to_ns_operations(__ns), (((__ns) == ns_init_ns(__ns)) ? ns_init_inum(__ns) : 0))
+
+#define ns_common_init_inum(__ns, __inum) __ns_common_init(to_ns_common(__ns), to_ns_operations(__ns), __inum)
 
 #define ns_common_free(__ns) __ns_common_free(to_ns_common((__ns)))
 
index bd85d1c9d2c21394005df878ea5dc78294f2de8a..d89dfd718d2b1263c9dbe429acd437d003446c88 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, &ipcns_operations);
+       err = ns_common_init(ns);
        if (err)
                goto fail_free;
 
index 16ead7508371095199fd5fb659746b2bcefd7f3c..04c98338ac08d1ab787c11006d9a8b548c23856f 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, &cgroupns_operations);
+       ret = ns_common_init(new_ns);
        if (ret)
                return ERR_PTR(ret);
        ns_tree_add(new_ns);
index 162f5fb63d75a8b2922cb6b626d6415534cc1dc0..a262a3f19443212292d86eeb63f45c4a4ed2f962 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, &pidns_operations);
+       err = ns_common_init(ns);
        if (err)
                goto out_free_idr;
 
index 7aa4d6fedd49613d787be8679f4b62ef20b3cb4d..9f26e61be044f31560e68286ade3ec2580508370 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, &timens_operations);
+       err = ns_common_init(ns);
        if (err)
                goto fail_free_page;
 
index f9df45c46235253039c1d49cc281c59dc41ea51a..e1559e8a8a02c5abe47bbe2f7fd46ddf5db30e6e 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, &userns_operations);
+       ret = ns_common_init(ns);
        if (ret)
                goto fail_free;
 
index 95d733eb2c985b4d36f71f8f2bf8b57f370c1c66..00001592ad134b078b17a39e4e5a8f5a60217d0b 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, &utsns_operations);
+       err = ns_common_init(ns);
        if (err)
                goto fail_free;
 
index d5e3fd81916316be11d9547432091184df5fb5a6..bdea7d5fac56084921b1cca95b9254fa837df1f4 100644 (file)
@@ -400,16 +400,9 @@ static __net_init void preinit_net_sysctl(struct net *net)
 /* init code that must occur even if setup_net() is not called. */
 static __net_init int preinit_net(struct net *net, struct user_namespace *user_ns)
 {
-       const struct proc_ns_operations *ns_ops;
        int ret;
 
-#ifdef CONFIG_NET_NS
-       ns_ops = &netns_operations;
-#else
-       ns_ops = NULL;
-#endif
-
-       ret = ns_common_init(net, ns_ops);
+       ret = ns_common_init(net);
        if (ret)
                return ret;