When a cgroup is frozen/unfrozen, it will always propagate bottom up. However it is unnecessary to propagate to the top every time. This patch aims to reduce redundant propagation for cgroup_propagate_frozen. For example, subtree like: a | b / | \ c d e If c is frozen, and d and e are not frozen now, it doesn't have to propagate to a; Only when c, d and e are all frozen, b and a could be set to frozen. Therefore, if nr_frozen_descendants is not equal to nr_descendants, just stop propagate. If a descendant is frozen, the parent's nr_frozen_descendants add child->nr_descendants + 1. This can reduce redundant propagation. Additionally, cgroup_propagate_frozen is not only used to update the ancestor state but also to update itself. This approach can make the code clearer and significantly simplify cgroup_update_frozen. Signed-off-by: Chen Ridong <chenridong@xxxxxxxxxx> --- include/linux/cgroup-defs.h | 4 +++- kernel/cgroup/freezer.c | 29 +++++++++++++++++------------ 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/include/linux/cgroup-defs.h b/include/linux/cgroup-defs.h index dd1ecab99eeb..41e4e5a7ae55 100644 --- a/include/linux/cgroup-defs.h +++ b/include/linux/cgroup-defs.h @@ -401,7 +401,9 @@ struct cgroup_freezer_state { /* Fields below are protected by css_set_lock */ - /* Number of frozen descendant cgroups */ + /* Aggregating frozen descendant cgroups, only when all + * descendants of a child are frozen will the count increase. + */ int nr_frozen_descendants; /* diff --git a/kernel/cgroup/freezer.c b/kernel/cgroup/freezer.c index bf1690a167dd..4ee33198d6fb 100644 --- a/kernel/cgroup/freezer.c +++ b/kernel/cgroup/freezer.c @@ -35,27 +35,34 @@ static bool cgroup_update_frozen_flag(struct cgroup *cgrp, bool frozen) */ static void cgroup_propagate_frozen(struct cgroup *cgrp, bool frozen) { - int desc = 1; - + int deta; + struct cgroup *parent; /* * If the new state is frozen, some freezing ancestor cgroups may change * their state too, depending on if all their descendants are frozen. * * Otherwise, all ancestor cgroups are forced into the non-frozen state. */ - while ((cgrp = cgroup_parent(cgrp))) { + for (; cgrp; cgrp = cgroup_parent(cgrp)) { if (frozen) { - cgrp->freezer.nr_frozen_descendants += desc; + /* If freezer is not set, or cgrp has descendants + * that are not frozen, cgrp can't be frozen + */ if (!test_bit(CGRP_FREEZE, &cgrp->flags) || (cgrp->freezer.nr_frozen_descendants != - cgrp->nr_descendants)) - continue; + cgrp->nr_descendants)) + break; + deta = cgrp->freezer.nr_frozen_descendants + 1; } else { - cgrp->freezer.nr_frozen_descendants -= desc; + deta = -(cgrp->freezer.nr_frozen_descendants + 1); } - if (cgroup_update_frozen_flag(cgrp, frozen)) - desc++; + /* No change, stop propagate */ + if (!cgroup_update_frozen_flag(cgrp, frozen)) + break; + + parent = cgroup_parent(cgrp); + parent->freezer.nr_frozen_descendants += deta; } } @@ -75,9 +82,7 @@ void cgroup_update_frozen(struct cgroup *cgrp) frozen = test_bit(CGRP_FREEZE, &cgrp->flags) && cgrp->freezer.nr_frozen_tasks == __cgroup_task_count(cgrp); - /* If flags is updated, update the state of ancestor cgroups. */ - if (cgroup_update_frozen_flag(cgrp, frozen)) - cgroup_propagate_frozen(cgrp, frozen); + cgroup_propagate_frozen(cgrp, frozen); } /* -- 2.34.1