Skip to content

Commit d7610cb

Browse files
committed
ns: simplify ns_common_init() further
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>
1 parent 8535bd3 commit d7610cb

9 files changed

Lines changed: 35 additions & 20 deletions

File tree

fs/namespace.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4104,9 +4104,9 @@ static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool a
41044104
}
41054105

41064106
if (anon)
4107-
ret = ns_common_init_inum(new_ns, &mntns_operations, MNT_NS_ANON_INO);
4107+
ret = ns_common_init_inum(new_ns, MNT_NS_ANON_INO);
41084108
else
4109-
ret = ns_common_init(new_ns, &mntns_operations);
4109+
ret = ns_common_init(new_ns);
41104110
if (ret) {
41114111
kfree(new_ns);
41124112
dec_mnt_namespaces(ucounts);

include/linux/ns_common.h

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ extern struct time_namespace init_time_ns;
2525
extern struct user_namespace init_user_ns;
2626
extern struct uts_namespace init_uts_ns;
2727

28+
extern const struct proc_ns_operations netns_operations;
29+
extern const struct proc_ns_operations utsns_operations;
30+
extern const struct proc_ns_operations ipcns_operations;
31+
extern const struct proc_ns_operations pidns_operations;
32+
extern const struct proc_ns_operations pidns_for_children_operations;
33+
extern const struct proc_ns_operations userns_operations;
34+
extern const struct proc_ns_operations mntns_operations;
35+
extern const struct proc_ns_operations cgroupns_operations;
36+
extern const struct proc_ns_operations timens_operations;
37+
extern const struct proc_ns_operations timens_for_children_operations;
38+
2839
struct ns_common {
2940
struct dentry *stashed;
3041
const struct proc_ns_operations *ops;
@@ -84,10 +95,21 @@ void __ns_common_free(struct ns_common *ns);
8495
struct user_namespace *: &init_user_ns, \
8596
struct uts_namespace *: &init_uts_ns)
8697

87-
#define ns_common_init(__ns, __ops) \
88-
__ns_common_init(to_ns_common(__ns), __ops, (((__ns) == ns_init_ns(__ns)) ? ns_init_inum(__ns) : 0))
89-
90-
#define ns_common_init_inum(__ns, __ops, __inum) __ns_common_init(to_ns_common(__ns), __ops, __inum)
98+
#define to_ns_operations(__ns) \
99+
_Generic((__ns), \
100+
struct cgroup_namespace *: (IS_ENABLED(CONFIG_CGROUPS) ? &cgroupns_operations : NULL), \
101+
struct ipc_namespace *: (IS_ENABLED(CONFIG_IPC_NS) ? &ipcns_operations : NULL), \
102+
struct mnt_namespace *: &mntns_operations, \
103+
struct net *: (IS_ENABLED(CONFIG_NET_NS) ? &netns_operations : NULL), \
104+
struct pid_namespace *: (IS_ENABLED(CONFIG_PID_NS) ? &pidns_operations : NULL), \
105+
struct time_namespace *: (IS_ENABLED(CONFIG_TIME_NS) ? &timens_operations : NULL), \
106+
struct user_namespace *: (IS_ENABLED(CONFIG_USER_NS) ? &userns_operations : NULL), \
107+
struct uts_namespace *: (IS_ENABLED(CONFIG_UTS_NS) ? &utsns_operations : NULL))
108+
109+
#define ns_common_init(__ns) \
110+
__ns_common_init(to_ns_common(__ns), to_ns_operations(__ns), (((__ns) == ns_init_ns(__ns)) ? ns_init_inum(__ns) : 0))
111+
112+
#define ns_common_init_inum(__ns, __inum) __ns_common_init(to_ns_common(__ns), to_ns_operations(__ns), __inum)
91113

92114
#define ns_common_free(__ns) __ns_common_free(to_ns_common((__ns)))
93115

ipc/namespace.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static struct ipc_namespace *create_ipc_ns(struct user_namespace *user_ns,
6262
if (ns == NULL)
6363
goto fail_dec;
6464

65-
err = ns_common_init(ns, &ipcns_operations);
65+
err = ns_common_init(ns);
6666
if (err)
6767
goto fail_free;
6868

kernel/cgroup/namespace.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static struct cgroup_namespace *alloc_cgroup_ns(void)
2727
new_ns = kzalloc(sizeof(struct cgroup_namespace), GFP_KERNEL_ACCOUNT);
2828
if (!new_ns)
2929
return ERR_PTR(-ENOMEM);
30-
ret = ns_common_init(new_ns, &cgroupns_operations);
30+
ret = ns_common_init(new_ns);
3131
if (ret)
3232
return ERR_PTR(ret);
3333
ns_tree_add(new_ns);

kernel/pid_namespace.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ static struct pid_namespace *create_pid_namespace(struct user_namespace *user_ns
103103
if (ns->pid_cachep == NULL)
104104
goto out_free_idr;
105105

106-
err = ns_common_init(ns, &pidns_operations);
106+
err = ns_common_init(ns);
107107
if (err)
108108
goto out_free_idr;
109109

kernel/time/namespace.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ static struct time_namespace *clone_time_ns(struct user_namespace *user_ns,
9797
if (!ns->vvar_page)
9898
goto fail_free;
9999

100-
err = ns_common_init(ns, &timens_operations);
100+
err = ns_common_init(ns);
101101
if (err)
102102
goto fail_free_page;
103103

kernel/user_namespace.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ int create_user_ns(struct cred *new)
126126

127127
ns->parent_could_setfcap = cap_raised(new->cap_effective, CAP_SETFCAP);
128128

129-
ret = ns_common_init(ns, &userns_operations);
129+
ret = ns_common_init(ns);
130130
if (ret)
131131
goto fail_free;
132132

kernel/utsname.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static struct uts_namespace *clone_uts_ns(struct user_namespace *user_ns,
5050
if (!ns)
5151
goto fail_dec;
5252

53-
err = ns_common_init(ns, &utsns_operations);
53+
err = ns_common_init(ns);
5454
if (err)
5555
goto fail_free;
5656

net/core/net_namespace.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -400,16 +400,9 @@ static __net_init void preinit_net_sysctl(struct net *net)
400400
/* init code that must occur even if setup_net() is not called. */
401401
static __net_init int preinit_net(struct net *net, struct user_namespace *user_ns)
402402
{
403-
const struct proc_ns_operations *ns_ops;
404403
int ret;
405404

406-
#ifdef CONFIG_NET_NS
407-
ns_ops = &netns_operations;
408-
#else
409-
ns_ops = NULL;
410-
#endif
411-
412-
ret = ns_common_init(net, ns_ops);
405+
ret = ns_common_init(net);
413406
if (ret)
414407
return ret;
415408

0 commit comments

Comments
 (0)