···11+/* SPDX-License-Identifier: GPL-2.0 */22+#ifndef _LINUX_NS_COMMON_TYPES_H33+#define _LINUX_NS_COMMON_TYPES_H44+55+#include <linux/atomic.h>66+#include <linux/rbtree.h>77+#include <linux/refcount.h>88+#include <linux/types.h>99+1010+struct cgroup_namespace;1111+struct dentry;1212+struct ipc_namespace;1313+struct mnt_namespace;1414+struct net;1515+struct pid_namespace;1616+struct proc_ns_operations;1717+struct time_namespace;1818+struct user_namespace;1919+struct uts_namespace;2020+2121+extern struct cgroup_namespace init_cgroup_ns;2222+extern struct ipc_namespace init_ipc_ns;2323+extern struct mnt_namespace init_mnt_ns;2424+extern struct net init_net;2525+extern struct pid_namespace init_pid_ns;2626+extern struct time_namespace init_time_ns;2727+extern struct user_namespace init_user_ns;2828+extern struct uts_namespace init_uts_ns;2929+3030+extern const struct proc_ns_operations cgroupns_operations;3131+extern const struct proc_ns_operations ipcns_operations;3232+extern const struct proc_ns_operations mntns_operations;3333+extern const struct proc_ns_operations netns_operations;3434+extern const struct proc_ns_operations pidns_operations;3535+extern const struct proc_ns_operations pidns_for_children_operations;3636+extern const struct proc_ns_operations timens_operations;3737+extern const struct proc_ns_operations timens_for_children_operations;3838+extern const struct proc_ns_operations userns_operations;3939+extern const struct proc_ns_operations utsns_operations;4040+4141+/*4242+ * Namespace lifetimes are managed via a two-tier reference counting model:4343+ *4444+ * (1) __ns_ref (refcount_t): Main reference count tracking memory4545+ * lifetime. Controls when the namespace structure itself is freed.4646+ * It also pins the namespace on the namespace trees whereas (2)4747+ * only regulates their visibility to userspace.4848+ *4949+ * (2) __ns_ref_active (atomic_t): Reference count tracking active users.5050+ * Controls visibility of the namespace in the namespace trees.5151+ * Any live task that uses the namespace (via nsproxy or cred) holds5252+ * an active reference. Any open file descriptor or bind-mount of5353+ * the namespace holds an active reference. Once all tasks have5454+ * called exited their namespaces and all file descriptors and5555+ * bind-mounts have been released the active reference count drops5656+ * to zero and the namespace becomes inactive. IOW, the namespace5757+ * cannot be listed or opened via file handles anymore.5858+ *5959+ * Note that it is valid to transition from active to inactive and6060+ * back from inactive to active e.g., when resurrecting an inactive6161+ * namespace tree via the SIOCGSKNS ioctl().6262+ *6363+ * Relationship and lifecycle states:6464+ *6565+ * - Active (__ns_ref_active > 0):6666+ * Namespace is actively used and visible to userspace. The namespace6767+ * can be reopened via /proc/<pid>/ns/<ns_type>, via namespace file6868+ * handles, or discovered via listns().6969+ *7070+ * - Inactive (__ns_ref_active == 0, __ns_ref > 0):7171+ * No tasks are actively using the namespace and it isn't pinned by7272+ * any bind-mounts or open file descriptors anymore. But the namespace7373+ * is still kept alive by internal references. For example, the user7474+ * namespace could be pinned by an open file through file->f_cred7575+ * references when one of the now defunct tasks had opened a file and7676+ * handed the file descriptor off to another process via a UNIX7777+ * sockets. Such references keep the namespace structure alive through7878+ * __ns_ref but will not hold an active reference.7979+ *8080+ * - Destroyed (__ns_ref == 0):8181+ * No references remain. The namespace is removed from the tree and freed.8282+ *8383+ * State transitions:8484+ *8585+ * Active -> Inactive:8686+ * When the last task using the namespace exits it drops its active8787+ * references to all namespaces. However, user and pid namespaces8888+ * remain accessible until the task has been reaped.8989+ *9090+ * Inactive -> Active:9191+ * An inactive namespace tree might be resurrected due to e.g., the9292+ * SIOCGSKNS ioctl() on a socket.9393+ *9494+ * Inactive -> Destroyed:9595+ * When __ns_ref drops to zero the namespace is removed from the9696+ * namespaces trees and the memory is freed (after RCU grace period).9797+ *9898+ * Initial namespaces:9999+ * Boot-time namespaces (init_net, init_pid_ns, etc.) start with100100+ * __ns_ref_active = 1 and remain active forever.101101+ */102102+struct ns_common {103103+ u32 ns_type;104104+ struct dentry *stashed;105105+ const struct proc_ns_operations *ops;106106+ unsigned int inum;107107+ refcount_t __ns_ref; /* do not use directly */108108+ union {109109+ struct {110110+ u64 ns_id;111111+ struct /* global namespace rbtree and list */ {112112+ struct rb_node ns_unified_tree_node;113113+ struct list_head ns_unified_list_node;114114+ };115115+ struct /* per type rbtree and list */ {116116+ struct rb_node ns_tree_node;117117+ struct list_head ns_list_node;118118+ };119119+ struct /* namespace ownership rbtree and list */ {120120+ struct rb_root ns_owner_tree; /* rbtree of namespaces owned by this namespace */121121+ struct list_head ns_owner; /* list of namespaces owned by this namespace */122122+ struct rb_node ns_owner_tree_node; /* node in the owner namespace's rbtree */123123+ struct list_head ns_owner_entry; /* node in the owner namespace's ns_owned list */124124+ };125125+ atomic_t __ns_ref_active; /* do not use directly */126126+ };127127+ struct rcu_head ns_rcu;128128+ };129129+};130130+131131+#define to_ns_common(__ns) \132132+ _Generic((__ns), \133133+ struct cgroup_namespace *: &(__ns)->ns, \134134+ const struct cgroup_namespace *: &(__ns)->ns, \135135+ struct ipc_namespace *: &(__ns)->ns, \136136+ const struct ipc_namespace *: &(__ns)->ns, \137137+ struct mnt_namespace *: &(__ns)->ns, \138138+ const struct mnt_namespace *: &(__ns)->ns, \139139+ struct net *: &(__ns)->ns, \140140+ const struct net *: &(__ns)->ns, \141141+ struct pid_namespace *: &(__ns)->ns, \142142+ const struct pid_namespace *: &(__ns)->ns, \143143+ struct time_namespace *: &(__ns)->ns, \144144+ const struct time_namespace *: &(__ns)->ns, \145145+ struct user_namespace *: &(__ns)->ns, \146146+ const struct user_namespace *: &(__ns)->ns, \147147+ struct uts_namespace *: &(__ns)->ns, \148148+ const struct uts_namespace *: &(__ns)->ns)149149+150150+#define ns_init_inum(__ns) \151151+ _Generic((__ns), \152152+ struct cgroup_namespace *: CGROUP_NS_INIT_INO, \153153+ struct ipc_namespace *: IPC_NS_INIT_INO, \154154+ struct mnt_namespace *: MNT_NS_INIT_INO, \155155+ struct net *: NET_NS_INIT_INO, \156156+ struct pid_namespace *: PID_NS_INIT_INO, \157157+ struct time_namespace *: TIME_NS_INIT_INO, \158158+ struct user_namespace *: USER_NS_INIT_INO, \159159+ struct uts_namespace *: UTS_NS_INIT_INO)160160+161161+#define ns_init_ns(__ns) \162162+ _Generic((__ns), \163163+ struct cgroup_namespace *: &init_cgroup_ns, \164164+ struct ipc_namespace *: &init_ipc_ns, \165165+ struct mnt_namespace *: &init_mnt_ns, \166166+ struct net *: &init_net, \167167+ struct pid_namespace *: &init_pid_ns, \168168+ struct time_namespace *: &init_time_ns, \169169+ struct user_namespace *: &init_user_ns, \170170+ struct uts_namespace *: &init_uts_ns)171171+172172+#define ns_init_id(__ns) \173173+ _Generic((__ns), \174174+ struct cgroup_namespace *: CGROUP_NS_INIT_ID, \175175+ struct ipc_namespace *: IPC_NS_INIT_ID, \176176+ struct mnt_namespace *: MNT_NS_INIT_ID, \177177+ struct net *: NET_NS_INIT_ID, \178178+ struct pid_namespace *: PID_NS_INIT_ID, \179179+ struct time_namespace *: TIME_NS_INIT_ID, \180180+ struct user_namespace *: USER_NS_INIT_ID, \181181+ struct uts_namespace *: UTS_NS_INIT_ID)182182+183183+#define to_ns_operations(__ns) \184184+ _Generic((__ns), \185185+ struct cgroup_namespace *: (IS_ENABLED(CONFIG_CGROUPS) ? &cgroupns_operations : NULL), \186186+ struct ipc_namespace *: (IS_ENABLED(CONFIG_IPC_NS) ? &ipcns_operations : NULL), \187187+ struct mnt_namespace *: &mntns_operations, \188188+ struct net *: (IS_ENABLED(CONFIG_NET_NS) ? &netns_operations : NULL), \189189+ struct pid_namespace *: (IS_ENABLED(CONFIG_PID_NS) ? &pidns_operations : NULL), \190190+ struct time_namespace *: (IS_ENABLED(CONFIG_TIME_NS) ? &timens_operations : NULL), \191191+ struct user_namespace *: (IS_ENABLED(CONFIG_USER_NS) ? &userns_operations : NULL), \192192+ struct uts_namespace *: (IS_ENABLED(CONFIG_UTS_NS) ? &utsns_operations : NULL))193193+194194+#define ns_common_type(__ns) \195195+ _Generic((__ns), \196196+ struct cgroup_namespace *: CLONE_NEWCGROUP, \197197+ struct ipc_namespace *: CLONE_NEWIPC, \198198+ struct mnt_namespace *: CLONE_NEWNS, \199199+ struct net *: CLONE_NEWNET, \200200+ struct pid_namespace *: CLONE_NEWPID, \201201+ struct time_namespace *: CLONE_NEWTIME, \202202+ struct user_namespace *: CLONE_NEWUSER, \203203+ struct uts_namespace *: CLONE_NEWUTS)204204+205205+#endif /* _LINUX_NS_COMMON_TYPES_H */
+1-195
include/linux/ns_common.h
···22#ifndef _LINUX_NS_COMMON_H33#define _LINUX_NS_COMMON_H4455+#include <linux/ns/ns_common_types.h>56#include <linux/refcount.h>66-#include <linux/rbtree.h>77#include <linux/vfsdebug.h>88#include <uapi/linux/sched.h>99#include <uapi/linux/nsfs.h>1010-1111-struct proc_ns_operations;1212-1313-struct cgroup_namespace;1414-struct ipc_namespace;1515-struct mnt_namespace;1616-struct net;1717-struct pid_namespace;1818-struct time_namespace;1919-struct user_namespace;2020-struct uts_namespace;2121-2222-extern struct cgroup_namespace init_cgroup_ns;2323-extern struct ipc_namespace init_ipc_ns;2424-extern struct mnt_namespace init_mnt_ns;2525-extern struct net init_net;2626-extern struct pid_namespace init_pid_ns;2727-extern struct time_namespace init_time_ns;2828-extern struct user_namespace init_user_ns;2929-extern struct uts_namespace init_uts_ns;3030-3131-extern const struct proc_ns_operations netns_operations;3232-extern const struct proc_ns_operations utsns_operations;3333-extern const struct proc_ns_operations ipcns_operations;3434-extern const struct proc_ns_operations pidns_operations;3535-extern const struct proc_ns_operations pidns_for_children_operations;3636-extern const struct proc_ns_operations userns_operations;3737-extern const struct proc_ns_operations mntns_operations;3838-extern const struct proc_ns_operations cgroupns_operations;3939-extern const struct proc_ns_operations timens_operations;4040-extern const struct proc_ns_operations timens_for_children_operations;4141-4242-/*4343- * Namespace lifetimes are managed via a two-tier reference counting model:4444- *4545- * (1) __ns_ref (refcount_t): Main reference count tracking memory4646- * lifetime. Controls when the namespace structure itself is freed.4747- * It also pins the namespace on the namespace trees whereas (2)4848- * only regulates their visibility to userspace.4949- *5050- * (2) __ns_ref_active (atomic_t): Reference count tracking active users.5151- * Controls visibility of the namespace in the namespace trees.5252- * Any live task that uses the namespace (via nsproxy or cred) holds5353- * an active reference. Any open file descriptor or bind-mount of5454- * the namespace holds an active reference. Once all tasks have5555- * called exited their namespaces and all file descriptors and5656- * bind-mounts have been released the active reference count drops5757- * to zero and the namespace becomes inactive. IOW, the namespace5858- * cannot be listed or opened via file handles anymore.5959- *6060- * Note that it is valid to transition from active to inactive and6161- * back from inactive to active e.g., when resurrecting an inactive6262- * namespace tree via the SIOCGSKNS ioctl().6363- *6464- * Relationship and lifecycle states:6565- *6666- * - Active (__ns_ref_active > 0):6767- * Namespace is actively used and visible to userspace. The namespace6868- * can be reopened via /proc/<pid>/ns/<ns_type>, via namespace file6969- * handles, or discovered via listns().7070- *7171- * - Inactive (__ns_ref_active == 0, __ns_ref > 0):7272- * No tasks are actively using the namespace and it isn't pinned by7373- * any bind-mounts or open file descriptors anymore. But the namespace7474- * is still kept alive by internal references. For example, the user7575- * namespace could be pinned by an open file through file->f_cred7676- * references when one of the now defunct tasks had opened a file and7777- * handed the file descriptor off to another process via a UNIX7878- * sockets. Such references keep the namespace structure alive through7979- * __ns_ref but will not hold an active reference.8080- *8181- * - Destroyed (__ns_ref == 0):8282- * No references remain. The namespace is removed from the tree and freed.8383- *8484- * State transitions:8585- *8686- * Active -> Inactive:8787- * When the last task using the namespace exits it drops its active8888- * references to all namespaces. However, user and pid namespaces8989- * remain accessible until the task has been reaped.9090- *9191- * Inactive -> Active:9292- * An inactive namespace tree might be resurrected due to e.g., the9393- * SIOCGSKNS ioctl() on a socket.9494- *9595- * Inactive -> Destroyed:9696- * When __ns_ref drops to zero the namespace is removed from the9797- * namespaces trees and the memory is freed (after RCU grace period).9898- *9999- * Initial namespaces:100100- * Boot-time namespaces (init_net, init_pid_ns, etc.) start with101101- * __ns_ref_active = 1 and remain active forever.102102- */103103-struct ns_common {104104- u32 ns_type;105105- struct dentry *stashed;106106- const struct proc_ns_operations *ops;107107- unsigned int inum;108108- refcount_t __ns_ref; /* do not use directly */109109- union {110110- struct {111111- u64 ns_id;112112- struct /* global namespace rbtree and list */ {113113- struct rb_node ns_unified_tree_node;114114- struct list_head ns_unified_list_node;115115- };116116- struct /* per type rbtree and list */ {117117- struct rb_node ns_tree_node;118118- struct list_head ns_list_node;119119- };120120- struct /* namespace ownership rbtree and list */ {121121- struct rb_root ns_owner_tree; /* rbtree of namespaces owned by this namespace */122122- struct list_head ns_owner; /* list of namespaces owned by this namespace */123123- struct rb_node ns_owner_tree_node; /* node in the owner namespace's rbtree */124124- struct list_head ns_owner_entry; /* node in the owner namespace's ns_owned list */125125- };126126- atomic_t __ns_ref_active; /* do not use directly */127127- };128128- struct rcu_head ns_rcu;129129- };130130-};1311013211bool is_current_namespace(struct ns_common *ns);13312int __ns_common_init(struct ns_common *ns, u32 ns_type, const struct proc_ns_operations *ops, int inum);···26147 return ns->ns_id <= NS_LAST_INIT_ID;27148}281492929-#define to_ns_common(__ns) \3030- _Generic((__ns), \3131- struct cgroup_namespace *: &(__ns)->ns, \3232- const struct cgroup_namespace *: &(__ns)->ns, \3333- struct ipc_namespace *: &(__ns)->ns, \3434- const struct ipc_namespace *: &(__ns)->ns, \3535- struct mnt_namespace *: &(__ns)->ns, \3636- const struct mnt_namespace *: &(__ns)->ns, \3737- struct net *: &(__ns)->ns, \3838- const struct net *: &(__ns)->ns, \3939- struct pid_namespace *: &(__ns)->ns, \4040- const struct pid_namespace *: &(__ns)->ns, \4141- struct time_namespace *: &(__ns)->ns, \4242- const struct time_namespace *: &(__ns)->ns, \4343- struct user_namespace *: &(__ns)->ns, \4444- const struct user_namespace *: &(__ns)->ns, \4545- struct uts_namespace *: &(__ns)->ns, \4646- const struct uts_namespace *: &(__ns)->ns)4747-4848-#define ns_init_inum(__ns) \4949- _Generic((__ns), \5050- struct cgroup_namespace *: CGROUP_NS_INIT_INO, \5151- struct ipc_namespace *: IPC_NS_INIT_INO, \5252- struct mnt_namespace *: MNT_NS_INIT_INO, \5353- struct net *: NET_NS_INIT_INO, \5454- struct pid_namespace *: PID_NS_INIT_INO, \5555- struct time_namespace *: TIME_NS_INIT_INO, \5656- struct user_namespace *: USER_NS_INIT_INO, \5757- struct uts_namespace *: UTS_NS_INIT_INO)5858-5959-#define ns_init_ns(__ns) \6060- _Generic((__ns), \6161- struct cgroup_namespace *: &init_cgroup_ns, \6262- struct ipc_namespace *: &init_ipc_ns, \6363- struct mnt_namespace *: &init_mnt_ns, \6464- struct net *: &init_net, \6565- struct pid_namespace *: &init_pid_ns, \6666- struct time_namespace *: &init_time_ns, \6767- struct user_namespace *: &init_user_ns, \6868- struct uts_namespace *: &init_uts_ns)6969-7070-#define ns_init_id(__ns) \7171- _Generic((__ns), \7272- struct cgroup_namespace *: CGROUP_NS_INIT_ID, \7373- struct ipc_namespace *: IPC_NS_INIT_ID, \7474- struct mnt_namespace *: MNT_NS_INIT_ID, \7575- struct net *: NET_NS_INIT_ID, \7676- struct pid_namespace *: PID_NS_INIT_ID, \7777- struct time_namespace *: TIME_NS_INIT_ID, \7878- struct user_namespace *: USER_NS_INIT_ID, \7979- struct uts_namespace *: UTS_NS_INIT_ID)8080-8181-#define to_ns_operations(__ns) \8282- _Generic((__ns), \8383- struct cgroup_namespace *: (IS_ENABLED(CONFIG_CGROUPS) ? &cgroupns_operations : NULL), \8484- struct ipc_namespace *: (IS_ENABLED(CONFIG_IPC_NS) ? &ipcns_operations : NULL), \8585- struct mnt_namespace *: &mntns_operations, \8686- struct net *: (IS_ENABLED(CONFIG_NET_NS) ? &netns_operations : NULL), \8787- struct pid_namespace *: (IS_ENABLED(CONFIG_PID_NS) ? &pidns_operations : NULL), \8888- struct time_namespace *: (IS_ENABLED(CONFIG_TIME_NS) ? &timens_operations : NULL), \8989- struct user_namespace *: (IS_ENABLED(CONFIG_USER_NS) ? &userns_operations : NULL), \9090- struct uts_namespace *: (IS_ENABLED(CONFIG_UTS_NS) ? &utsns_operations : NULL))9191-9292-#define ns_common_type(__ns) \9393- _Generic((__ns), \9494- struct cgroup_namespace *: CLONE_NEWCGROUP, \9595- struct ipc_namespace *: CLONE_NEWIPC, \9696- struct mnt_namespace *: CLONE_NEWNS, \9797- struct net *: CLONE_NEWNET, \9898- struct pid_namespace *: CLONE_NEWPID, \9999- struct time_namespace *: CLONE_NEWTIME, \100100- struct user_namespace *: CLONE_NEWUSER, \101101- struct uts_namespace *: CLONE_NEWUTS)102150103151#define NS_COMMON_INIT(nsname, refs) \104152{ \