[PATCH v2 2/2] include/cgroup: separate {get,put}_cgroup_ns no-op case

Joel Savitz posted 2 patches 9 months ago
[PATCH v2 2/2] include/cgroup: separate {get,put}_cgroup_ns no-op case
Posted by Joel Savitz 9 months ago
When CONFIG_CGROUPS is not selected, {get,put}_cgroup_ns become no-ops
and therefore it is not necessary to compile in the code for changing
the reference count.

When CONFIG_CGROUP is selected, there is no valid case where
either of {get,put}_cgroup_ns() will be called with a NULL argument.

Signed-off-by: Joel Savitz <jsavitz@redhat.com>
---
 include/linux/cgroup.h | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)

diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h
index e7da3c3b098b..166d6de50dbf 100644
--- a/include/linux/cgroup.h
+++ b/include/linux/cgroup.h
@@ -785,6 +785,17 @@ struct cgroup_namespace *copy_cgroup_ns(unsigned long flags,
 int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen,
 		   struct cgroup_namespace *ns);
 
+static inline void get_cgroup_ns(struct cgroup_namespace *ns)
+{
+	refcount_inc(&ns->ns.count);
+}
+
+static inline void put_cgroup_ns(struct cgroup_namespace *ns)
+{
+	if (refcount_dec_and_test(&ns->ns.count))
+		free_cgroup_ns(ns);
+}
+
 #else /* !CONFIG_CGROUPS */
 
 static inline void free_cgroup_ns(struct cgroup_namespace *ns) { }
@@ -795,19 +806,10 @@ copy_cgroup_ns(unsigned long flags, struct user_namespace *user_ns,
 	return old_ns;
 }
 
-#endif /* !CONFIG_CGROUPS */
+static inline void get_cgroup_ns(struct cgroup_namespace *ns) { }
+static inline void put_cgroup_ns(struct cgroup_namespace *ns) { }
 
-static inline void get_cgroup_ns(struct cgroup_namespace *ns)
-{
-	if (ns)
-		refcount_inc(&ns->ns.count);
-}
-
-static inline void put_cgroup_ns(struct cgroup_namespace *ns)
-{
-	if (ns && refcount_dec_and_test(&ns->ns.count))
-		free_cgroup_ns(ns);
-}
+#endif /* !CONFIG_CGROUPS */
 
 #ifdef CONFIG_CGROUPS
 
-- 
2.45.2
Re: [PATCH v2 2/2] include/cgroup: separate {get,put}_cgroup_ns no-op case
Posted by Tejun Heo 9 months ago
On Thu, May 08, 2025 at 02:49:30PM -0400, Joel Savitz wrote:
> When CONFIG_CGROUPS is not selected, {get,put}_cgroup_ns become no-ops
> and therefore it is not necessary to compile in the code for changing
> the reference count.
> 
> When CONFIG_CGROUP is selected, there is no valid case where
> either of {get,put}_cgroup_ns() will be called with a NULL argument.
> 
> Signed-off-by: Joel Savitz <jsavitz@redhat.com>

Applied to cgroup/for-6.16.

Thanks.

-- 
tejun
Re: [PATCH v2 2/2] include/cgroup: separate {get,put}_cgroup_ns no-op case
Posted by Michal Koutný 9 months ago
On Thu, May 08, 2025 at 02:49:30PM -0400, Joel Savitz <jsavitz@redhat.com> wrote:
> When CONFIG_CGROUPS is not selected, {get,put}_cgroup_ns become no-ops
> and therefore it is not necessary to compile in the code for changing
> the reference count.
> 
> When CONFIG_CGROUP is selected, there is no valid case where
> either of {get,put}_cgroup_ns() will be called with a NULL argument.
> 
> Signed-off-by: Joel Savitz <jsavitz@redhat.com>
> ---
>  include/linux/cgroup.h | 26 ++++++++++++++------------
>  1 file changed, 14 insertions(+), 12 deletions(-)

Acked-by: Michal Koutný <mkoutny@suse.com>