Skip to contentSkip to conversation

Minor namespace code simplication

PATCH Applied Joel Savitz 4 participants 6 messages May 8, 2025 – May 9, 2025 cgroup

lore
Share & tools
lore
#0 lore raw

v2 cover Minor namespace code simplication

The two patches are independent of each other. The first patch removes unnecssary NULL guards from free_nsproxy() and create_new_namespaces() in line with other usage of the put_*_ns() call sites. The second patch slightly reduces the size of the kernel when CONFIG_CGROUPS is not selected.

Joel Savitz (2):
kernel/nsproxy: remove unnecessary guards include/cgroup: separate {get,put}_cgroup_ns no-op case

Changes from v1:
- now removing the guards instead of adding them where missing since checking that all calls in the NULL case were already no-ops
- added second patch

 include/linux/cgroup.h | 26 ++++++++++++++------------
 kernel/nsproxy.c       | 30 ++++++++++--------------------
 2 files changed, 24 insertions(+), 32 deletions(-)
Signature · 1 line
2.45.2
#1 lore raw

PATCH v2 1/2 kernel/nsproxy: remove unnecessary guards

In free_nsproxy() and the error path of create_new_namesapces() the put_*_ns() calls are guarded by unnecessary NULL checks.

put_pid_ns(), put_ipc_ns(), put_uts_ns(), and put_time_ns() will never receive a NULL argument unless their namespace type is disabled, and in this case all four become no-ops at compile time anyway. put_mnt_ns() will never receive a null argument at any time.

This unguarded usage is in line with other call sites of put_*_ns().

---
 kernel/nsproxy.c | 30 ++++++++++--------------------
 1 file changed, 10 insertions(+), 20 deletions(-)
kernel/nsproxy.c +10 -20
index c9d97ed20122..5f31fdff8a38 100644
@@ -128,17 +128,13 @@ static struct nsproxy *create_new_namespaces(unsigned long flags,
 out_net:
 	put_cgroup_ns(new_nsp->cgroup_ns);
 out_cgroup:
-	if (new_nsp->pid_ns_for_children)
-		put_pid_ns(new_nsp->pid_ns_for_children);
+	put_pid_ns(new_nsp->pid_ns_for_children);
 out_pid:
-	if (new_nsp->ipc_ns)
-		put_ipc_ns(new_nsp->ipc_ns);
+	put_ipc_ns(new_nsp->ipc_ns);
 out_ipc:
-	if (new_nsp->uts_ns)
-		put_uts_ns(new_nsp->uts_ns);
+	put_uts_ns(new_nsp->uts_ns);
 out_uts:
-	if (new_nsp->mnt_ns)
-		put_mnt_ns(new_nsp->mnt_ns);
+	put_mnt_ns(new_nsp->mnt_ns);
 out_ns:
 	kmem_cache_free(nsproxy_cachep, new_nsp);
 	return ERR_PTR(err);
@@ -189,18 +185,12 @@ int copy_namespaces(unsigned long flags, struct task_struct *tsk)
 
 void free_nsproxy(struct nsproxy *ns)
 {
-	if (ns->mnt_ns)
-		put_mnt_ns(ns->mnt_ns);
-	if (ns->uts_ns)
-		put_uts_ns(ns->uts_ns);
-	if (ns->ipc_ns)
-		put_ipc_ns(ns->ipc_ns);
-	if (ns->pid_ns_for_children)
-		put_pid_ns(ns->pid_ns_for_children);
-	if (ns->time_ns)
-		put_time_ns(ns->time_ns);
-	if (ns->time_ns_for_children)
-		put_time_ns(ns->time_ns_for_children);
+	put_mnt_ns(ns->mnt_ns);
+	put_uts_ns(ns->uts_ns);
+	put_ipc_ns(ns->ipc_ns);
+	put_pid_ns(ns->pid_ns_for_children);
+	put_time_ns(ns->time_ns);
+	put_time_ns(ns->time_ns_for_children);
 	put_cgroup_ns(ns->cgroup_ns);
 	put_net(ns->net_ns);
 	kmem_cache_free(nsproxy_cachep, ns);
Signature · 1 line
2.45.2
#2 lore raw

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

In reply to Joel Savitz

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.

---
 include/linux/cgroup.h | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)
include/linux/cgroup.h +14 -12
index e7da3c3b098b..166d6de50dbf 100644
@@ -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
 
Signature · 1 line
2.45.2
maintainer Acked-by on v2 2/2
#3 lore raw

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

Quoted 11 lines · Joel Savitz

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.

---
 include/linux/cgroup.h | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)
maintainer Applied
#4 lore raw

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

In reply to Joel Savitz
Quoted 8 lines · Joel Savitz

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.

Applied to cgroup/for-6.16.

Thanks.

Signature · 1 line
tejun
#5 lore raw
In reply to Joel Savitz
Quoted 11 lines · Joel Savitz

The two patches are independent of each other. The first patch removes unnecssary NULL guards from free_nsproxy() and create_new_namespaces() in line with other usage of the put_*_ns() call sites. The second patch slightly reduces the size of the kernel when CONFIG_CGROUPS is not selected.

Joel Savitz (2):
kernel/nsproxy: remove unnecessary guards include/cgroup: separate {get,put}_cgroup_ns no-op case

[...]

Applied to the vfs-6.16.misc branch of the vfs/vfs.git tree. Patches in the vfs-6.16.misc branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs-6.16.misc

[1/2] kernel/nsproxy: remove unnecessary guards

      https://git.kernel.org/vfs/vfs/c/5caa2d89b7f1
[2/2] include/cgroup: separate {get,put}_cgroup_ns no-op case
      https://git.kernel.org/vfs/vfs/c/79fb8d8d93e4