[SRU][J][PATCH 2/3] cgroup: Make cgroup_get_from_id() prettier
Philip Cox
philip.cox at canonical.com
Tue Feb 11 14:58:40 UTC 2025
From: Tejun Heo <tj at kernel.org>
BugLink: https://bugs.launchpad.net/bugs/2089318
After merging 836ac87d ("cgroup: fix cgroup_get_from_id") into for-6.1, its
combination with two commits in for-6.1 - 4534dee9 ("cgroup: cgroup: Honor
caller's cgroup NS when resolving cgroup id") and fa7e439c ("cgroup:
Homogenize cgroup_get_from_id() return value") - makes the gotos in the
error handling path too ugly while not adding anything of value.
All that the gotos are saving is one extra kernfs_put() call. Let's remove
the gotos and perform error returns directly.
Signed-off-by: Tejun Heo <tj at kernel.org>
Cc: Ming Lei <ming.lei at redhat.com>
Cc: Michal Koutný <mkoutny at suse.com>
(backported from commit 7e1eb5437d3c3fdb61d45378579aab383cafc694)
[context changes in cgroup_get_from_id()]
Signed-off-by: Philip Cox <philip.cox at canonical.com>
---
kernel/cgroup/cgroup.c | 17 +++++++++--------
1 file changed, 9 insertions(+), 8 deletions(-)
diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index 27d503fd17fe5..bc528b8635a5f 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -6038,14 +6038,16 @@ void cgroup_path_from_kernfs_id(u64 id, char *buf, size_t buflen)
struct cgroup *cgroup_get_from_id(u64 id)
{
struct kernfs_node *kn;
- struct cgroup *cgrp = NULL, *root_cgrp;
+ struct cgroup *cgrp, *root_cgrp;
kn = kernfs_find_and_get_node_by_id(cgrp_dfl_root.kf_root, id);
if (!kn)
- goto out;
+ return ERR_PTR(-ENOENT);
- if (kernfs_type(kn) != KERNFS_DIR)
- goto put;
+ if (kernfs_type(kn) != KERNFS_DIR) {
+ kernfs_put(kn);
+ return ERR_PTR(-ENOENT);
+ }
rcu_read_lock();
@@ -6054,20 +6056,19 @@ struct cgroup *cgroup_get_from_id(u64 id)
cgrp = NULL;
rcu_read_unlock();
-put:
kernfs_put(kn);
if (!cgrp)
- goto out;
+ return ERR_PTR(-ENOENT);
spin_lock_irq(&css_set_lock);
root_cgrp = current_cgns_cgroup_from_root(&cgrp_dfl_root);
spin_unlock_irq(&css_set_lock);
if (!cgroup_is_descendant(cgrp, root_cgrp)) {
cgroup_put(cgrp);
- cgrp = NULL;
+ return ERR_PTR(-ENOENT);
}
-out:
+
return cgrp;
}
EXPORT_SYMBOL_GPL(cgroup_get_from_id);
--
2.43.0
More information about the kernel-team
mailing list