Skip to content

Commit 5612ff3

Browse files
committed
nscommon: simplify initialization
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>
1 parent d5b27cb commit 5612ff3

10 files changed

Lines changed: 55 additions & 20 deletions

File tree

fs/namespace.c

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

41064106
if (anon)
4107-
new_ns->ns.inum = MNT_NS_ANON_INO;
4108-
ret = ns_common_init(&new_ns->ns, &mntns_operations, !anon);
4107+
ret = ns_common_init_inum(new_ns, &mntns_operations, MNT_NS_ANON_INO);
4108+
else
4109+
ret = ns_common_init(new_ns, &mntns_operations);
41094110
if (ret) {
41104111
kfree(new_ns);
41114112
dec_mnt_namespaces(ucounts);

include/linux/ns_common.h

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ struct time_namespace;
1616
struct user_namespace;
1717
struct uts_namespace;
1818

19+
extern struct cgroup_namespace init_cgroup_ns;
20+
extern struct ipc_namespace init_ipc_ns;
21+
extern struct mnt_namespace init_mnt_ns;
22+
extern struct net init_net;
23+
extern struct pid_namespace init_pid_ns;
24+
extern struct time_namespace init_time_ns;
25+
extern struct user_namespace init_user_ns;
26+
extern struct uts_namespace init_uts_ns;
27+
1928
struct ns_common {
2029
struct dentry *stashed;
2130
const struct proc_ns_operations *ops;
@@ -31,8 +40,7 @@ struct ns_common {
3140
};
3241
};
3342

34-
int ns_common_init(struct ns_common *ns, const struct proc_ns_operations *ops,
35-
bool alloc_inum);
43+
int __ns_common_init(struct ns_common *ns, const struct proc_ns_operations *ops, int inum);
3644

3745
#define to_ns_common(__ns) \
3846
_Generic((__ns), \
@@ -45,4 +53,31 @@ int ns_common_init(struct ns_common *ns, const struct proc_ns_operations *ops,
4553
struct user_namespace *: &(__ns)->ns, \
4654
struct uts_namespace *: &(__ns)->ns)
4755

56+
#define ns_init_inum(__ns) \
57+
_Generic((__ns), \
58+
struct cgroup_namespace *: CGROUP_NS_INIT_INO, \
59+
struct ipc_namespace *: IPC_NS_INIT_INO, \
60+
struct mnt_namespace *: MNT_NS_INIT_INO, \
61+
struct net *: NET_NS_INIT_INO, \
62+
struct pid_namespace *: PID_NS_INIT_INO, \
63+
struct time_namespace *: TIME_NS_INIT_INO, \
64+
struct user_namespace *: USER_NS_INIT_INO, \
65+
struct uts_namespace *: UTS_NS_INIT_INO)
66+
67+
#define ns_init_ns(__ns) \
68+
_Generic((__ns), \
69+
struct cgroup_namespace *: &init_cgroup_ns, \
70+
struct ipc_namespace *: &init_ipc_ns, \
71+
struct mnt_namespace *: &init_mnt_ns, \
72+
struct net *: &init_net, \
73+
struct pid_namespace *: &init_pid_ns, \
74+
struct time_namespace *: &init_time_ns, \
75+
struct user_namespace *: &init_user_ns, \
76+
struct uts_namespace *: &init_uts_ns)
77+
78+
#define ns_common_init(__ns, __ops) \
79+
__ns_common_init(to_ns_common(__ns), __ops, (((__ns) == ns_init_ns(__ns)) ? ns_init_inum(__ns) : 0))
80+
81+
#define ns_common_init_inum(__ns, __ops, __inum) __ns_common_init(to_ns_common(__ns), __ops, __inum)
82+
4883
#endif

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->ns, &ipcns_operations, true);
65+
err = ns_common_init(ns, &ipcns_operations);
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->ns, &cgroupns_operations, true);
30+
ret = ns_common_init(new_ns, &cgroupns_operations);
3131
if (ret)
3232
return ERR_PTR(ret);
3333
ns_tree_add(new_ns);

kernel/nscommon.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
// SPDX-License-Identifier: GPL-2.0-only
22

33
#include <linux/ns_common.h>
4+
#include <linux/proc_ns.h>
45

5-
int ns_common_init(struct ns_common *ns, const struct proc_ns_operations *ops,
6-
bool alloc_inum)
6+
int __ns_common_init(struct ns_common *ns, const struct proc_ns_operations *ops, int inum)
77
{
8-
if (alloc_inum && !ns->inum) {
9-
int ret;
10-
ret = proc_alloc_inum(&ns->inum);
11-
if (ret)
12-
return ret;
13-
}
148
refcount_set(&ns->count, 1);
159
ns->stashed = NULL;
1610
ns->ops = ops;
1711
ns->ns_id = 0;
1812
RB_CLEAR_NODE(&ns->ns_tree_node);
1913
INIT_LIST_HEAD(&ns->ns_list_node);
20-
return 0;
14+
15+
if (inum) {
16+
ns->inum = inum;
17+
return 0;
18+
}
19+
return proc_alloc_inum(&ns->inum);
2120
}

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->ns, &pidns_operations, true);
106+
err = ns_common_init(ns, &pidns_operations);
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->ns, &timens_operations, true);
100+
err = ns_common_init(ns, &timens_operations);
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->ns, &userns_operations, true);
129+
ret = ns_common_init(ns, &userns_operations);
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->ns, &utsns_operations, true);
53+
err = ns_common_init(ns, &utsns_operations);
5454
if (err)
5555
goto fail_free;
5656

net/core/net_namespace.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ static __net_init int preinit_net(struct net *net, struct user_namespace *user_n
409409
ns_ops = NULL;
410410
#endif
411411

412-
ret = ns_common_init(&net->ns, ns_ops, true);
412+
ret = ns_common_init(net, ns_ops);
413413
if (ret)
414414
return ret;
415415

0 commit comments

Comments
 (0)