xref: /linux/include/linux/ns_common.h (revision 08471914d6bb24493b75bb03701548133d617878)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_NS_COMMON_H
3 #define _LINUX_NS_COMMON_H
4 
5 #include <linux/ns/ns_common_types.h>
6 #include <linux/refcount.h>
7 #include <linux/vfsdebug.h>
8 #include <uapi/linux/sched.h>
9 #include <uapi/linux/nsfs.h>
10 
11 bool is_current_namespace(struct ns_common *ns);
12 int __ns_common_init(struct ns_common *ns, u32 ns_type, const struct proc_ns_operations *ops,
13 		     int inum, u64 ns_id);
14 void __ns_common_free(struct ns_common *ns);
15 struct ns_common *__must_check ns_owner(struct ns_common *ns);
16 
17 static __always_inline bool is_ns_init_inum(const struct ns_common *ns)
18 {
19 	VFS_WARN_ON_ONCE(ns->inum == 0);
20 	return unlikely(in_range(ns->inum, MNT_NS_INIT_INO,
21 				 IPC_NS_INIT_INO - MNT_NS_INIT_INO + 1));
22 }
23 
24 static __always_inline bool is_ns_init_id(const struct ns_common *ns)
25 {
26 	VFS_WARN_ON_ONCE(ns->ns_id == 0);
27 	return ns->ns_id <= NS_LAST_INIT_ID;
28 }
29 
30 #define NS_COMMON_INIT(nsname)										\
31 {													\
32 	.ns_type			= ns_common_type(&nsname),					\
33 	.ns_id				= ns_init_id(&nsname),						\
34 	.inum				= ns_init_inum(&nsname),					\
35 	.ops				= to_ns_operations(&nsname),					\
36 	.stashed			= NULL,								\
37 	.__ns_ref			= REFCOUNT_INIT(1),						\
38 	.__ns_ref_active		= ATOMIC_INIT(1),						\
39 	.ns_unified_node.ns_list_entry	= LIST_HEAD_INIT(nsname.ns.ns_unified_node.ns_list_entry),	\
40 	.ns_tree_node.ns_list_entry	= LIST_HEAD_INIT(nsname.ns.ns_tree_node.ns_list_entry),		\
41 	.ns_owner_node.ns_list_entry	= LIST_HEAD_INIT(nsname.ns.ns_owner_node.ns_list_entry),	\
42 	.ns_owner_root.ns_list_head	= LIST_HEAD_INIT(nsname.ns.ns_owner_root.ns_list_head),		\
43 }
44 
45 #define ns_common_init(__ns)						\
46 	__ns_common_init(to_ns_common(__ns),				\
47 			 ns_common_type(__ns),				\
48 			 to_ns_operations(__ns),			\
49 			 (((__ns) == ns_init_ns(__ns)) ? ns_init_inum(__ns) : 0), \
50 			 (((__ns) == ns_init_ns(__ns)) ? ns_init_id(__ns) : 0))
51 
52 #define ns_common_init_inum(__ns, __inum)        \
53 	__ns_common_init(to_ns_common(__ns),     \
54 			 ns_common_type(__ns),   \
55 			 to_ns_operations(__ns), \
56 			 __inum, 0)
57 
58 #define ns_common_free(__ns) __ns_common_free(to_ns_common((__ns)))
59 
60 bool may_see_all_namespaces(void);
61 
62 static __always_inline __must_check int __ns_ref_active_read(const struct ns_common *ns)
63 {
64 	return atomic_read(&ns->__ns_ref_active);
65 }
66 
67 static __always_inline __must_check int __ns_ref_read(const struct ns_common *ns)
68 {
69 	return refcount_read(&ns->__ns_ref);
70 }
71 
72 static __always_inline __must_check bool __ns_ref_put(struct ns_common *ns)
73 {
74 	if (is_ns_init_id(ns)) {
75 		VFS_WARN_ON_ONCE(__ns_ref_read(ns) != 1);
76 		VFS_WARN_ON_ONCE(__ns_ref_active_read(ns) != 1);
77 		return false;
78 	}
79 	if (refcount_dec_and_test(&ns->__ns_ref)) {
80 		VFS_WARN_ON_ONCE(__ns_ref_active_read(ns));
81 		return true;
82 	}
83 	return false;
84 }
85 
86 static __always_inline __must_check bool __ns_ref_get(struct ns_common *ns)
87 {
88 	if (is_ns_init_id(ns)) {
89 		VFS_WARN_ON_ONCE(__ns_ref_read(ns) != 1);
90 		VFS_WARN_ON_ONCE(__ns_ref_active_read(ns) != 1);
91 		return true;
92 	}
93 	if (refcount_inc_not_zero(&ns->__ns_ref))
94 		return true;
95 	VFS_WARN_ON_ONCE(__ns_ref_active_read(ns));
96 	return false;
97 }
98 
99 static __always_inline void __ns_ref_inc(struct ns_common *ns)
100 {
101 	if (is_ns_init_id(ns)) {
102 		VFS_WARN_ON_ONCE(__ns_ref_read(ns) != 1);
103 		VFS_WARN_ON_ONCE(__ns_ref_active_read(ns) != 1);
104 		return;
105 	}
106 	refcount_inc(&ns->__ns_ref);
107 }
108 
109 static __always_inline __must_check bool __ns_ref_dec_and_lock(struct ns_common *ns,
110 							       spinlock_t *ns_lock)
111 {
112 	if (is_ns_init_id(ns)) {
113 		VFS_WARN_ON_ONCE(__ns_ref_read(ns) != 1);
114 		VFS_WARN_ON_ONCE(__ns_ref_active_read(ns) != 1);
115 		return false;
116 	}
117 	return refcount_dec_and_lock(&ns->__ns_ref, ns_lock);
118 }
119 
120 #define ns_ref_read(__ns) __ns_ref_read(to_ns_common((__ns)))
121 #define ns_ref_inc(__ns) \
122 	do { if (__ns) __ns_ref_inc(to_ns_common((__ns))); } while (0)
123 #define ns_ref_get(__ns) \
124 	((__ns) ? __ns_ref_get(to_ns_common((__ns))) : false)
125 #define ns_ref_put(__ns) \
126 	((__ns) ? __ns_ref_put(to_ns_common((__ns))) : false)
127 #define ns_ref_put_and_lock(__ns, __ns_lock) \
128 	((__ns) ? __ns_ref_dec_and_lock(to_ns_common((__ns)), __ns_lock) : false)
129 
130 #define ns_ref_active_read(__ns) \
131 	((__ns) ? __ns_ref_active_read(to_ns_common(__ns)) : 0)
132 
133 void __ns_ref_active_put(struct ns_common *ns);
134 
135 #define ns_ref_active_put(__ns) \
136 	do { if (__ns) __ns_ref_active_put(to_ns_common(__ns)); } while (0)
137 
138 static __always_inline struct ns_common *__must_check ns_get_unless_inactive(struct ns_common *ns)
139 {
140 	if (!__ns_ref_active_read(ns)) {
141 		VFS_WARN_ON_ONCE(is_ns_init_id(ns));
142 		return NULL;
143 	}
144 	if (!__ns_ref_get(ns))
145 		return NULL;
146 	return ns;
147 }
148 
149 void __ns_ref_active_get(struct ns_common *ns);
150 
151 #define ns_ref_active_get(__ns) \
152 	do { if (__ns) __ns_ref_active_get(to_ns_common(__ns)); } while (0)
153 
154 #endif
155