]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/commitdiff
cgroup: reduce dependency on cgroup_mutex
authorShakeel Butt <shakeelb@google.com>
Mon, 25 Oct 2021 06:19:14 +0000 (23:19 -0700)
committerStefan Bader <stefan.bader@canonical.com>
Mon, 14 Nov 2022 10:25:21 +0000 (11:25 +0100)
BugLink: https://bugs.launchpad.net/bugs/1995517
[ Upstream commit be288169712f3dea0bc6b50c00b3ab53d85f1435 ]

Currently cgroup_get_from_path() and cgroup_get_from_id() grab
cgroup_mutex before traversing the default hierarchy to find the
kernfs_node corresponding to the path/id and then extract the linked
cgroup. Since cgroup_mutex is still held, it is guaranteed that the
cgroup will be alive and the reference can be taken on it.

However similar guarantee can be provided without depending on the
cgroup_mutex and potentially reducing avenues of cgroup_mutex contentions.
The kernfs_node's priv pointer is RCU protected pointer and with just
rcu read lock we can grab the reference on the cgroup without
cgroup_mutex. So, remove cgroup_mutex from them.

Signed-off-by: Shakeel Butt <shakeelb@google.com>
Signed-off-by: Tejun Heo <tj@kernel.org>
Stable-dep-of: df02452f3df0 ("cgroup: cgroup_get_from_id() must check the looked-up kn is a directory")
Signed-off-by: Sasha Levin <sashal@kernel.org>
Signed-off-by: Kamal Mostafa <kamal@canonical.com>
Signed-off-by: Stefan Bader <stefan.bader@canonical.com>
kernel/cgroup/cgroup.c

index 75c3881af078414c23a04679990d1cf3fbb6aaf3..97282d6b5d18cbfed7378165ecec30136d4b8d81 100644 (file)
@@ -6021,17 +6021,20 @@ struct cgroup *cgroup_get_from_id(u64 id)
        struct kernfs_node *kn;
        struct cgroup *cgrp = NULL;
 
-       mutex_lock(&cgroup_mutex);
        kn = kernfs_find_and_get_node_by_id(cgrp_dfl_root.kf_root, id);
        if (!kn)
-               goto out_unlock;
+               goto out;
+
+       rcu_read_lock();
 
-       cgrp = kn->priv;
-       if (cgroup_is_dead(cgrp) || !cgroup_tryget(cgrp))
+       cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv);
+       if (cgrp && !cgroup_tryget(cgrp))
                cgrp = NULL;
+
+       rcu_read_unlock();
+
        kernfs_put(kn);
-out_unlock:
-       mutex_unlock(&cgroup_mutex);
+out:
        return cgrp;
 }
 EXPORT_SYMBOL_GPL(cgroup_get_from_id);
@@ -6585,30 +6588,34 @@ struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
  *
  * Find the cgroup at @path on the default hierarchy, increment its
  * reference count and return it.  Returns pointer to the found cgroup on
- * success, ERR_PTR(-ENOENT) if @path doesn't exist and ERR_PTR(-ENOTDIR)
- * if @path points to a non-directory.
+ * success, ERR_PTR(-ENOENT) if @path doesn't exist or if the cgroup has already
+ * been released and ERR_PTR(-ENOTDIR) if @path points to a non-directory.
  */
 struct cgroup *cgroup_get_from_path(const char *path)
 {
        struct kernfs_node *kn;
-       struct cgroup *cgrp;
-
-       mutex_lock(&cgroup_mutex);
+       struct cgroup *cgrp = ERR_PTR(-ENOENT);
 
        kn = kernfs_walk_and_get(cgrp_dfl_root.cgrp.kn, path);
-       if (kn) {
-               if (kernfs_type(kn) == KERNFS_DIR) {
-                       cgrp = kn->priv;
-                       cgroup_get_live(cgrp);
-               } else {
-                       cgrp = ERR_PTR(-ENOTDIR);
-               }
-               kernfs_put(kn);
-       } else {
-               cgrp = ERR_PTR(-ENOENT);
+       if (!kn)
+               goto out;
+
+       if (kernfs_type(kn) != KERNFS_DIR) {
+               cgrp = ERR_PTR(-ENOTDIR);
+               goto out_kernfs;
        }
 
-       mutex_unlock(&cgroup_mutex);
+       rcu_read_lock();
+
+       cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv);
+       if (!cgrp || !cgroup_tryget(cgrp))
+               cgrp = ERR_PTR(-ENOENT);
+
+       rcu_read_unlock();
+
+out_kernfs:
+       kernfs_put(kn);
+out:
        return cgrp;
 }
 EXPORT_SYMBOL_GPL(cgroup_get_from_path);