提交 15a27c36 编写于 作者: T Tejun Heo

cgroup: introduce cgroup_{save|propagate|restore}_control()

While controllers are being enabled and disabled in
cgroup_subtree_control_write(), the original subsystem masks are
stashed in local variables so that they can be restored if the
operation fails in the middle.

This patch adds dedicated fields to struct cgroup to be used instead
of the local variables and implements functions to stash the current
values, propagate the changes and restore them recursively.  Combined
with the previous changes, this makes subsystem management operations
fully recursive and modularlized.  This will be used to expand cgroup
core functionalities.

While at it, remove now unused @css_enable and @css_disable from
cgroup_subtree_control_write().
Signed-off-by: NTejun Heo <tj@kernel.org>
Acked-by: NZefan Li <lizefan@huawei.com>
上级 ce3f1d9d
...@@ -260,6 +260,8 @@ struct cgroup { ...@@ -260,6 +260,8 @@ struct cgroup {
*/ */
u16 subtree_control; u16 subtree_control;
u16 subtree_ss_mask; u16 subtree_ss_mask;
u16 old_subtree_control;
u16 old_subtree_ss_mask;
/* Private pointers for each registered subsystem */ /* Private pointers for each registered subsystem */
struct cgroup_subsys_state __rcu *subsys[CGROUP_SUBSYS_COUNT]; struct cgroup_subsys_state __rcu *subsys[CGROUP_SUBSYS_COUNT];
......
...@@ -3031,6 +3031,62 @@ static bool cgroup_drain_offline(struct cgroup *cgrp) ...@@ -3031,6 +3031,62 @@ static bool cgroup_drain_offline(struct cgroup *cgrp)
return false; return false;
} }
/**
* cgroup_save_control - save control masks of a subtree
* @cgrp: root of the target subtree
*
* Save ->subtree_control and ->subtree_ss_mask to the respective old_
* prefixed fields for @cgrp's subtree including @cgrp itself.
*/
static void cgroup_save_control(struct cgroup *cgrp)
{
struct cgroup *dsct;
struct cgroup_subsys_state *d_css;
cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
dsct->old_subtree_control = dsct->subtree_control;
dsct->old_subtree_ss_mask = dsct->subtree_ss_mask;
}
}
/**
* cgroup_propagate_control - refresh control masks of a subtree
* @cgrp: root of the target subtree
*
* For @cgrp and its subtree, ensure ->subtree_ss_mask matches
* ->subtree_control and propagate controller availability through the
* subtree so that descendants don't have unavailable controllers enabled.
*/
static void cgroup_propagate_control(struct cgroup *cgrp)
{
struct cgroup *dsct;
struct cgroup_subsys_state *d_css;
cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) {
dsct->subtree_control &= cgroup_control(dsct);
dsct->subtree_ss_mask = cgroup_calc_subtree_ss_mask(dsct,
dsct->subtree_control);
}
}
/**
* cgroup_restore_control - restore control masks of a subtree
* @cgrp: root of the target subtree
*
* Restore ->subtree_control and ->subtree_ss_mask from the respective old_
* prefixed fields for @cgrp's subtree including @cgrp itself.
*/
static void cgroup_restore_control(struct cgroup *cgrp)
{
struct cgroup *dsct;
struct cgroup_subsys_state *d_css;
cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) {
dsct->subtree_control = dsct->old_subtree_control;
dsct->subtree_ss_mask = dsct->old_subtree_ss_mask;
}
}
/** /**
* cgroup_apply_control_enable - enable or show csses according to control * cgroup_apply_control_enable - enable or show csses according to control
* @cgrp: root of the target subtree * @cgrp: root of the target subtree
...@@ -3119,7 +3175,6 @@ static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of, ...@@ -3119,7 +3175,6 @@ static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
loff_t off) loff_t off)
{ {
u16 enable = 0, disable = 0; u16 enable = 0, disable = 0;
u16 css_enable, css_disable, old_sc, new_sc, old_ss, new_ss;
struct cgroup *cgrp, *child; struct cgroup *cgrp, *child;
struct cgroup_subsys *ss; struct cgroup_subsys *ss;
char *tok; char *tok;
...@@ -3203,25 +3258,14 @@ static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of, ...@@ -3203,25 +3258,14 @@ static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
return restart_syscall(); return restart_syscall();
} }
/* /* save and update control masks and prepare csses */
* Update subsys masks and calculate what needs to be done. More cgroup_save_control(cgrp);
* subsystems than specified may need to be enabled or disabled
* depending on subsystem dependencies.
*/
old_sc = cgrp->subtree_control;
old_ss = cgrp->subtree_ss_mask;
new_sc = (old_sc | enable) & ~disable;
new_ss = cgroup_calc_subtree_ss_mask(cgrp, new_sc);
css_enable = ~old_ss & new_ss; cgrp->subtree_control |= enable;
css_disable = old_ss & ~new_ss; cgrp->subtree_control &= ~disable;
enable |= css_enable;
disable |= css_disable;
cgrp->subtree_control = new_sc; cgroup_propagate_control(cgrp);
cgrp->subtree_ss_mask = new_ss;
/* prepare csses */
ret = cgroup_apply_control_enable(cgrp); ret = cgroup_apply_control_enable(cgrp);
if (ret) if (ret)
goto err_undo_css; goto err_undo_css;
...@@ -3246,9 +3290,7 @@ static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of, ...@@ -3246,9 +3290,7 @@ static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of,
err_undo_css: err_undo_css:
/* restore masks and shoot down new csses */ /* restore masks and shoot down new csses */
cgrp->subtree_control = old_sc; cgroup_restore_control(cgrp);
cgrp->subtree_ss_mask = old_ss;
cgroup_apply_control_disable(cgrp); cgroup_apply_control_disable(cgrp);
goto out_unlock; goto out_unlock;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册