]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
ns: add reference count helpers
authorChristian Brauner <brauner@kernel.org>
Thu, 18 Sep 2025 10:11:46 +0000 (12:11 +0200)
committerChristian Brauner <brauner@kernel.org>
Fri, 19 Sep 2025 14:22:36 +0000 (16:22 +0200)
Reviewed-by: Jan Kara <jack@suse.cz>
Signed-off-by: Christian Brauner <brauner@kernel.org>
include/linux/ns_common.h

index 19833ac547f9af4f493885e878574f176e344203..65e258e1fdc6dd4091da0c9473037eacc420950b 100644 (file)
@@ -43,16 +43,24 @@ struct ns_common {
 int __ns_common_init(struct ns_common *ns, const struct proc_ns_operations *ops, int inum);
 void __ns_common_free(struct ns_common *ns);
 
-#define to_ns_common(__ns)                              \
-       _Generic((__ns),                                \
-               struct cgroup_namespace *: &(__ns)->ns, \
-               struct ipc_namespace *:    &(__ns)->ns, \
-               struct mnt_namespace *:    &(__ns)->ns, \
-               struct net *:              &(__ns)->ns, \
-               struct pid_namespace *:    &(__ns)->ns, \
-               struct time_namespace *:   &(__ns)->ns, \
-               struct user_namespace *:   &(__ns)->ns, \
-               struct uts_namespace *:    &(__ns)->ns)
+#define to_ns_common(__ns)                                    \
+       _Generic((__ns),                                      \
+               struct cgroup_namespace *:       &(__ns)->ns, \
+               const struct cgroup_namespace *: &(__ns)->ns, \
+               struct ipc_namespace *:          &(__ns)->ns, \
+               const struct ipc_namespace *:    &(__ns)->ns, \
+               struct mnt_namespace *:          &(__ns)->ns, \
+               const struct mnt_namespace *:    &(__ns)->ns, \
+               struct net *:                    &(__ns)->ns, \
+               const struct net *:              &(__ns)->ns, \
+               struct pid_namespace *:          &(__ns)->ns, \
+               const struct pid_namespace *:    &(__ns)->ns, \
+               struct time_namespace *:         &(__ns)->ns, \
+               const struct time_namespace *:   &(__ns)->ns, \
+               struct user_namespace *:         &(__ns)->ns, \
+               const struct user_namespace *:   &(__ns)->ns, \
+               struct uts_namespace *:          &(__ns)->ns, \
+               const struct uts_namespace *:    &(__ns)->ns)
 
 #define ns_init_inum(__ns)                                     \
        _Generic((__ns),                                       \
@@ -83,4 +91,21 @@ void __ns_common_free(struct ns_common *ns);
 
 #define ns_common_free(__ns) __ns_common_free(to_ns_common((__ns)))
 
+static __always_inline __must_check bool __ns_ref_put(struct ns_common *ns)
+{
+       return refcount_dec_and_test(&ns->count);
+}
+
+static __always_inline __must_check bool __ns_ref_get(struct ns_common *ns)
+{
+       return refcount_inc_not_zero(&ns->count);
+}
+
+#define ns_ref_read(__ns) refcount_read(&to_ns_common((__ns))->count)
+#define ns_ref_inc(__ns) refcount_inc(&to_ns_common((__ns))->count)
+#define ns_ref_get(__ns) __ns_ref_get(to_ns_common((__ns)))
+#define ns_ref_put(__ns) __ns_ref_put(to_ns_common((__ns)))
+#define ns_ref_put_and_lock(__ns, __lock) \
+       refcount_dec_and_lock(&to_ns_common((__ns))->count, (__lock))
+
 #endif