xref: /linux/include/linux/ns/ns_common_types.h (revision 546b928da0427b0d6c663cbb992bd7bfa9ac7971)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_NS_COMMON_TYPES_H
3 #define _LINUX_NS_COMMON_TYPES_H
4 
5 #include <linux/atomic.h>
6 #include <linux/ns/nstree_types.h>
7 #include <linux/rbtree.h>
8 #include <linux/refcount.h>
9 #include <linux/types.h>
10 #include <uapi/linux/sched.h>
11 
12 struct cgroup_namespace;
13 struct dentry;
14 struct ipc_namespace;
15 struct mnt_namespace;
16 struct net;
17 struct pid_namespace;
18 struct proc_ns_operations;
19 struct time_namespace;
20 struct user_namespace;
21 struct uts_namespace;
22 
23 extern struct cgroup_namespace init_cgroup_ns;
24 extern struct ipc_namespace init_ipc_ns;
25 extern struct mnt_namespace init_mnt_ns;
26 extern struct net init_net;
27 extern struct pid_namespace init_pid_ns;
28 extern struct time_namespace init_time_ns;
29 extern struct user_namespace init_user_ns;
30 extern struct uts_namespace init_uts_ns;
31 
32 extern const struct proc_ns_operations cgroupns_operations;
33 extern const struct proc_ns_operations ipcns_operations;
34 extern const struct proc_ns_operations mntns_operations;
35 extern const struct proc_ns_operations netns_operations;
36 extern const struct proc_ns_operations pidns_operations;
37 extern const struct proc_ns_operations pidns_for_children_operations;
38 extern const struct proc_ns_operations timens_operations;
39 extern const struct proc_ns_operations timens_for_children_operations;
40 extern const struct proc_ns_operations userns_operations;
41 extern const struct proc_ns_operations utsns_operations;
42 
43 /*
44  * Namespace lifetimes are managed via a two-tier reference counting model:
45  *
46  * (1) __ns_ref (refcount_t): Main reference count tracking memory
47  *     lifetime. Controls when the namespace structure itself is freed.
48  *     It also pins the namespace on the namespace trees whereas (2)
49  *     only regulates their visibility to userspace.
50  *
51  * (2) __ns_ref_active (atomic_t): Reference count tracking active users.
52  *     Controls visibility of the namespace in the namespace trees.
53  *     Any live task that uses the namespace (via nsproxy or cred) holds
54  *     an active reference. Any open file descriptor or bind-mount of
55  *     the namespace holds an active reference. Once all tasks have
56  *     called exited their namespaces and all file descriptors and
57  *     bind-mounts have been released the active reference count drops
58  *     to zero and the namespace becomes inactive. IOW, the namespace
59  *     cannot be listed or opened via file handles anymore.
60  *
61  *     Note that it is valid to transition from active to inactive and
62  *     back from inactive to active e.g., when resurrecting an inactive
63  *     namespace tree via the SIOCGSKNS ioctl().
64  *
65  * Relationship and lifecycle states:
66  *
67  * - Active (__ns_ref_active > 0):
68  *   Namespace is actively used and visible to userspace. The namespace
69  *   can be reopened via /proc/<pid>/ns/<ns_type>, via namespace file
70  *   handles, or discovered via listns().
71  *
72  * - Inactive (__ns_ref_active == 0, __ns_ref > 0):
73  *   No tasks are actively using the namespace and it isn't pinned by
74  *   any bind-mounts or open file descriptors anymore. But the namespace
75  *   is still kept alive by internal references. For example, the user
76  *   namespace could be pinned by an open file through file->f_cred
77  *   references when one of the now defunct tasks had opened a file and
78  *   handed the file descriptor off to another process via a UNIX
79  *   sockets. Such references keep the namespace structure alive through
80  *   __ns_ref but will not hold an active reference.
81  *
82  * - Destroyed (__ns_ref == 0):
83  *   No references remain. The namespace is removed from the tree and freed.
84  *
85  * State transitions:
86  *
87  * Active -> Inactive:
88  *   When the last task using the namespace exits it drops its active
89  *   references to all namespaces. However, user and pid namespaces
90  *   remain accessible until the task has been reaped.
91  *
92  * Inactive -> Active:
93  *   An inactive namespace tree might be resurrected due to e.g., the
94  *   SIOCGSKNS ioctl() on a socket.
95  *
96  * Inactive -> Destroyed:
97  *   When __ns_ref drops to zero the namespace is removed from the
98  *   namespaces trees and the memory is freed (after RCU grace period).
99  *
100  * Initial namespaces:
101  *   Boot-time namespaces (init_net, init_pid_ns, etc.) start with
102  *   __ns_ref_active = 1 and remain active forever.
103  *
104  * @ns_type: type of namespace (e.g., CLONE_NEWNET)
105  * @stashed: cached dentry to be used by the vfs
106  * @ops: namespace operations
107  * @inum: namespace inode number (quickly recycled for non-initial namespaces)
108  * @__ns_ref: main reference count (do not use directly)
109  * @ns_tree: namespace tree nodes and active reference count
110  */
111 struct ns_common {
112 	struct {
113 		refcount_t __ns_ref; /* do not use directly */
114 	} ____cacheline_aligned_in_smp;
115 	u32 ns_type;
116 	struct dentry *stashed;
117 	const struct proc_ns_operations *ops;
118 	unsigned int inum;
119 	struct ns_tree;
120 	struct rcu_head ns_rcu;
121 };
122 
123 #define to_ns_common(__ns)                                    \
124 	_Generic((__ns),                                      \
125 		struct cgroup_namespace *:       &(__ns)->ns, \
126 		const struct cgroup_namespace *: &(__ns)->ns, \
127 		struct ipc_namespace *:          &(__ns)->ns, \
128 		const struct ipc_namespace *:    &(__ns)->ns, \
129 		struct mnt_namespace *:          &(__ns)->ns, \
130 		const struct mnt_namespace *:    &(__ns)->ns, \
131 		struct net *:                    &(__ns)->ns, \
132 		const struct net *:              &(__ns)->ns, \
133 		struct pid_namespace *:          &(__ns)->ns, \
134 		const struct pid_namespace *:    &(__ns)->ns, \
135 		struct time_namespace *:         &(__ns)->ns, \
136 		const struct time_namespace *:   &(__ns)->ns, \
137 		struct user_namespace *:         &(__ns)->ns, \
138 		const struct user_namespace *:   &(__ns)->ns, \
139 		struct uts_namespace *:          &(__ns)->ns, \
140 		const struct uts_namespace *:    &(__ns)->ns)
141 
142 #define ns_init_inum(__ns)                                     \
143 	_Generic((__ns),                                       \
144 		struct cgroup_namespace *: CGROUP_NS_INIT_INO, \
145 		struct ipc_namespace *:    IPC_NS_INIT_INO,    \
146 		struct mnt_namespace *:    MNT_NS_INIT_INO,    \
147 		struct net *:              NET_NS_INIT_INO,    \
148 		struct pid_namespace *:    PID_NS_INIT_INO,    \
149 		struct time_namespace *:   TIME_NS_INIT_INO,   \
150 		struct user_namespace *:   USER_NS_INIT_INO,   \
151 		struct uts_namespace *:    UTS_NS_INIT_INO)
152 
153 #define ns_init_ns(__ns)                                    \
154 	_Generic((__ns),                                    \
155 		struct cgroup_namespace *: &init_cgroup_ns, \
156 		struct ipc_namespace *:    &init_ipc_ns,    \
157 		struct mnt_namespace *:    &init_mnt_ns,     \
158 		struct net *:              &init_net,       \
159 		struct pid_namespace *:    &init_pid_ns,    \
160 		struct time_namespace *:   &init_time_ns,   \
161 		struct user_namespace *:   &init_user_ns,   \
162 		struct uts_namespace *:    &init_uts_ns)
163 
164 #define ns_init_id(__ns)						\
165 	_Generic((__ns),						\
166 		struct cgroup_namespace *:	CGROUP_NS_INIT_ID,	\
167 		struct ipc_namespace *:		IPC_NS_INIT_ID,		\
168 		struct mnt_namespace *:		MNT_NS_INIT_ID,		\
169 		struct net *:			NET_NS_INIT_ID,		\
170 		struct pid_namespace *:		PID_NS_INIT_ID,		\
171 		struct time_namespace *:	TIME_NS_INIT_ID,	\
172 		struct user_namespace *:	USER_NS_INIT_ID,	\
173 		struct uts_namespace *:		UTS_NS_INIT_ID)
174 
175 #define to_ns_operations(__ns)                                                                         \
176 	_Generic((__ns),                                                                               \
177 		struct cgroup_namespace *: (IS_ENABLED(CONFIG_CGROUPS) ? &cgroupns_operations : NULL), \
178 		struct ipc_namespace *:    (IS_ENABLED(CONFIG_IPC_NS)  ? &ipcns_operations    : NULL), \
179 		struct mnt_namespace *:    &mntns_operations,                                          \
180 		struct net *:              (IS_ENABLED(CONFIG_NET_NS)  ? &netns_operations    : NULL), \
181 		struct pid_namespace *:    (IS_ENABLED(CONFIG_PID_NS)  ? &pidns_operations    : NULL), \
182 		struct time_namespace *:   (IS_ENABLED(CONFIG_TIME_NS) ? &timens_operations   : NULL), \
183 		struct user_namespace *:   (IS_ENABLED(CONFIG_USER_NS) ? &userns_operations   : NULL), \
184 		struct uts_namespace *:    (IS_ENABLED(CONFIG_UTS_NS)  ? &utsns_operations    : NULL))
185 
186 /*
187  * FOR_EACH_NS_TYPE - Canonical list of namespace types
188  *
189  * Enumerates all (struct type, CLONE_NEW* flag) pairs.  This is the
190  * single source of truth used to derive ns_common_type() and
191  * CLONE_NS_ALL.  When adding a new namespace type, add a single entry
192  * here; all consumers update automatically.
193  *
194  * @X: Callback macro taking (struct_name, clone_flag) as arguments.
195  */
196 #define FOR_EACH_NS_TYPE(X)                  \
197 	X(cgroup_namespace, CLONE_NEWCGROUP) \
198 	X(ipc_namespace, CLONE_NEWIPC)       \
199 	X(mnt_namespace, CLONE_NEWNS)        \
200 	X(net, CLONE_NEWNET)                 \
201 	X(pid_namespace, CLONE_NEWPID)       \
202 	X(time_namespace, CLONE_NEWTIME)     \
203 	X(user_namespace, CLONE_NEWUSER)     \
204 	X(uts_namespace, CLONE_NEWUTS)
205 
206 /* Bitmask of all known CLONE_NEW* flags. */
207 #define _NS_TYPE_FLAG_OR(struct_name, flag) | (flag)
208 #define CLONE_NS_ALL                        (0 FOR_EACH_NS_TYPE(_NS_TYPE_FLAG_OR))
209 
210 /*
211  * ns_common_type - Map a namespace struct pointer to its CLONE_NEW* flag
212  *
213  * Uses a leading-comma pattern so the FOR_EACH_NS_TYPE expansion
214  * produces ", struct foo *: FLAG" entries without a trailing comma.
215  */
216 #define _NS_TYPE_ASSOC(struct_name, flag) , struct struct_name *: (flag)
217 
218 #define ns_common_type(__ns) _Generic((__ns)FOR_EACH_NS_TYPE(_NS_TYPE_ASSOC))
219 
220 #endif /* _LINUX_NS_COMMON_TYPES_H */
221