提交 d24a3b0b 编写于 作者: T Tejun Heo 提交者: openeuler-sync-bot

cgroup: Elide write-locking threadgroup_rwsem when updating csses on an empty subtree

stable inclusion
from stable-v5.10.143
commit bfbacc2ef7b5b9261e795e361ee233e7d36efae6
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/I60G94
CVE: NA

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=bfbacc2ef7b5b9261e795e361ee233e7d36efae6

-------------------------------

[ Upstream commit 671c11f0 ]

cgroup_update_dfl_csses() write-lock the threadgroup_rwsem as updating the
csses can trigger process migrations. However, if the subtree doesn't
contain any tasks, there aren't gonna be any cgroup migrations. This
condition can be trivially detected by testing whether
mgctx.preloaded_src_csets is empty. Elide write-locking threadgroup_rwsem if
the subtree is empty.

After this optimization, the usage pattern of creating a cgroup, enabling
the necessary controllers, and then seeding it with CLONE_INTO_CGROUP and
then removing the cgroup after it becomes empty doesn't need to write-lock
threadgroup_rwsem at all.
Signed-off-by: NTejun Heo <tj@kernel.org>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Michal Koutný <mkoutny@suse.com>
Signed-off-by: NSasha Levin <sashal@kernel.org>
Signed-off-by: NCai Xinchen <caixinchen1@huawei.com>
Reviewed-by: NXiu Jianfeng <xiujianfeng@huawei.com>
Reviewed-by: NWang Weiyang <wangweiyang2@huawei.com>
Signed-off-by: NJialin Zhang <zhangjialin11@huawei.com>
(cherry picked from commit 00e6868b)
上级 c5a3f428
...@@ -2954,12 +2954,11 @@ static int cgroup_update_dfl_csses(struct cgroup *cgrp) ...@@ -2954,12 +2954,11 @@ static int cgroup_update_dfl_csses(struct cgroup *cgrp)
struct cgroup_subsys_state *d_css; struct cgroup_subsys_state *d_css;
struct cgroup *dsct; struct cgroup *dsct;
struct css_set *src_cset; struct css_set *src_cset;
bool has_tasks;
int ret; int ret;
lockdep_assert_held(&cgroup_mutex); lockdep_assert_held(&cgroup_mutex);
percpu_down_write(&cgroup_threadgroup_rwsem);
/* look up all csses currently attached to @cgrp's subtree */ /* look up all csses currently attached to @cgrp's subtree */
spin_lock_irq(&css_set_lock); spin_lock_irq(&css_set_lock);
cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
...@@ -2970,6 +2969,16 @@ static int cgroup_update_dfl_csses(struct cgroup *cgrp) ...@@ -2970,6 +2969,16 @@ static int cgroup_update_dfl_csses(struct cgroup *cgrp)
} }
spin_unlock_irq(&css_set_lock); spin_unlock_irq(&css_set_lock);
/*
* We need to write-lock threadgroup_rwsem while migrating tasks.
* However, if there are no source csets for @cgrp, changing its
* controllers isn't gonna produce any task migrations and the
* write-locking can be skipped safely.
*/
has_tasks = !list_empty(&mgctx.preloaded_src_csets);
if (has_tasks)
percpu_down_write(&cgroup_threadgroup_rwsem);
/* NULL dst indicates self on default hierarchy */ /* NULL dst indicates self on default hierarchy */
ret = cgroup_migrate_prepare_dst(&mgctx); ret = cgroup_migrate_prepare_dst(&mgctx);
if (ret) if (ret)
...@@ -2988,6 +2997,7 @@ static int cgroup_update_dfl_csses(struct cgroup *cgrp) ...@@ -2988,6 +2997,7 @@ static int cgroup_update_dfl_csses(struct cgroup *cgrp)
ret = cgroup_migrate_execute(&mgctx); ret = cgroup_migrate_execute(&mgctx);
out_finish: out_finish:
cgroup_migrate_finish(&mgctx); cgroup_migrate_finish(&mgctx);
if (has_tasks)
percpu_up_write(&cgroup_threadgroup_rwsem); percpu_up_write(&cgroup_threadgroup_rwsem);
return ret; return ret;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册