Skip to content

Commit d969328

Browse files
committed
Merge patch series "ns: minor tweaks"
Christian Brauner <brauner@kernel.org> says: * Add a missing include into the cgroup namespace header. * Simplify ns_common_init{_inum}() and derive the namespace operations from the namespace type. * Add debug asserts into ns_common_init{_inum}() to catch bugs. * patches from https://lore.kernel.org/20250922-work-namespace-ns_common-fixes-v1-0-3c26aeb30831@kernel.org: ns: add ns_debug() ns: simplify ns_common_init() further cgroup: add missing ns_common include Signed-off-by: Christian Brauner <brauner@kernel.org>
2 parents 7cf7303 + 5890f50 commit d969328

11 files changed

Lines changed: 90 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/cgroup_namespace.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#ifndef _LINUX_CGROUP_NAMESPACE_H
33
#define _LINUX_CGROUP_NAMESPACE_H
44

5+
#include <linux/ns_common.h>
6+
57
struct cgroup_namespace {
68
struct ns_common ns;
79
struct user_namespace *user_ns;

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/nscommon.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,55 @@
22

33
#include <linux/ns_common.h>
44
#include <linux/proc_ns.h>
5+
#include <linux/vfsdebug.h>
6+
7+
#ifdef CONFIG_DEBUG_VFS
8+
static void ns_debug(struct ns_common *ns, const struct proc_ns_operations *ops)
9+
{
10+
switch (ns->ops->type) {
11+
#ifdef CONFIG_CGROUPS
12+
case CLONE_NEWCGROUP:
13+
VFS_WARN_ON_ONCE(ops != &cgroupns_operations);
14+
break;
15+
#endif
16+
#ifdef CONFIG_IPC_NS
17+
case CLONE_NEWIPC:
18+
VFS_WARN_ON_ONCE(ops != &ipcns_operations);
19+
break;
20+
#endif
21+
case CLONE_NEWNS:
22+
VFS_WARN_ON_ONCE(ops != &mntns_operations);
23+
break;
24+
#ifdef CONFIG_NET_NS
25+
case CLONE_NEWNET:
26+
VFS_WARN_ON_ONCE(ops != &netns_operations);
27+
break;
28+
#endif
29+
#ifdef CONFIG_PID_NS
30+
case CLONE_NEWPID:
31+
VFS_WARN_ON_ONCE(ops != &pidns_operations);
32+
break;
33+
#endif
34+
#ifdef CONFIG_TIME_NS
35+
case CLONE_NEWTIME:
36+
VFS_WARN_ON_ONCE(ops != &timens_operations);
37+
break;
38+
#endif
39+
#ifdef CONFIG_USER_NS
40+
case CLONE_NEWUSER:
41+
VFS_WARN_ON_ONCE(ops != &userns_operations);
42+
break;
43+
#endif
44+
#ifdef CONFIG_UTS_NS
45+
case CLONE_NEWUTS:
46+
VFS_WARN_ON_ONCE(ops != &utsns_operations);
47+
break;
48+
#endif
49+
default:
50+
VFS_WARN_ON_ONCE(true);
51+
}
52+
}
53+
#endif
554

655
int __ns_common_init(struct ns_common *ns, const struct proc_ns_operations *ops, int inum)
756
{
@@ -12,6 +61,10 @@ int __ns_common_init(struct ns_common *ns, const struct proc_ns_operations *ops,
1261
RB_CLEAR_NODE(&ns->ns_tree_node);
1362
INIT_LIST_HEAD(&ns->ns_list_node);
1463

64+
#ifdef CONFIG_DEBUG_VFS
65+
ns_debug(ns, ops);
66+
#endif
67+
1568
if (inum) {
1669
ns->inum = inum;
1770
return 0;

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

0 commit comments

Comments
 (0)