Some optimizations about freezer
v2 cover Some optimizations about freezer
We optimized the freezer to reduce redundant loops, and we add helper to make code concise.
We tested the following subtree:
0
/ | \ \
A B C 1
/ | \ \
A B C 2
.....
n
/ | \
A B CWe tested is by following steps:
1. freeze 0
2. unfreeze 0
3. freeze 0
4. freeze 1
We measured the elapsed time(ns).
n=10
freeze 0 unfreeze 0 freeze 0 freeze 1
BEFORE 106179390 94016050 110423650 95063770
AFTER 96473608 91054188 94936398 93198510
n=50
freeze 0 unfreeze 0 freeze 0 freeze 1
BEFORE 109506660 105643800 105970220 96948940
AFTER 105244651 97357482 97517358 88466266
n=100
freeze 0 unfreeze 0 freeze 0 freeze 1
BEFORE 127944210 122049330 120988900 101232850
AFTER 117298106 107034146 105696895 91977833
As shown above, after optimizations, we can save elapsed time. By freezing 0 and subsequently freezing 1, the elapsed time is consistent, indicating that our optimizations are highly effective.
---
v2:
- open code inside the loop of cgroup_freeze instead of inline function.
- add helper to make code concise.
- remove selftest script(There are hierarchy test in test_freeze.c, I think that is enough for this series).
Chen Ridong (3):
cgroup/freezer: Reduce redundant traversal for cgroup_freeze cgroup/freezer: Add cgroup CGRP_FROZEN flag update helper cgroup/freezer: Reduce redundant propagation for
cgroup_propagate_frozen
include/linux/cgroup-defs.h | 6 +- kernel/cgroup/freezer.c | 110 ++++++++++++++++++------------------ 2 files changed, 59 insertions(+), 57 deletions(-)
Signature · 1 line
2.34.1
PATCH v2 1/3 cgroup/freezer: Reduce redundant traversal for cgroup_freeze
Whether a cgroup is frozen is determined solely by whether it is set to to be frozen and whether its parent is frozen. Currently, when is cgroup is frozen or unfrozen, it iterates through the entire subtree to freeze or unfreeze its descentdants. However, this is unesessary for a cgroup that does not change its effective frozen status. This path aims to skip the subtree if its parent does not have a change in effective freeze.
For an example, subtree like, a-b-c-d-e-f-g, when a is frozen, the entire tree is frozen. If we freeze b and c again, it is unesessary to iterate d, e, f and g. So does that If we unfreeze b/c.
--- include/linux/cgroup-defs.h | 2 +- kernel/cgroup/freezer.c | 30 ++++++++++++++---------------- 2 files changed, 15 insertions(+), 17 deletions(-)
include/linux/cgroup-defs.h +1 -1
@@ -397,7 +397,7 @@ struct cgroup_freezer_state {bool freeze;/* Should the cgroup actually be frozen? */ -int e_freeze; +bool e_freeze;/* Fields below are protected by css_set_lock */
kernel/cgroup/freezer.c +14 -16
@@ -260,8 +260,10 @@ void cgroup_freezer_migrate_task(struct task_struct *task,void cgroup_freeze (struct cgroup *cgrp,bool freeze) {struct cgroup_subsys_state *css; +struct cgroup *parent;struct cgroup *dsct;bool applied = false; +bool old_e;lockdep_assert_held (&cgroup_mutex);
@@ -282,22 +284,18 @@ void cgroup_freeze(struct cgroup *cgrp, bool freeze)if (cgroup_is_dead (dsct))continue ; -if (freeze) { - dsct->freezer.e_freeze++; -/* -* Already frozen because of ancestor's settings? -*/ -if (dsct->freezer.e_freeze >1 ) -continue ; - }else { - dsct->freezer.e_freeze--; -/* -* Still frozen because of ancestor's settings? -*/ -if (dsct->freezer.e_freeze >0 ) -continue ; - -WARN_ON_ONCE (dsct->freezer.e_freeze <0 ); +/* +* e_freeze is affected by parent's e_freeze and dst's freeze. +* If old e_freeze eq new e_freeze, no change, its children +* will not be affected. So do nothing and skip the subtree +*/ + old_e = dsct->freezer.e_freeze; + parent =cgroup_parent (dsct); + dsct->freezer.e_freeze = (dsct->freezer.freeze || + parent->freezer.e_freeze); +if (dsct->freezer.e_freeze == old_e) { + css =css_rightmost_descendant (css); +continue ; }/*
Signature · 1 line
2.34.1
PATCH v2 2/3 cgroup/freezer: Add cgroup CGRP_FROZEN flag update helper
In reply to Chen RidongAdd help to update cgroup CGRP_FROZEN flag. Both cgroup_propagate_frozen and cgroup_update_frozen functions update CGRP_FROZEN flag, this makes code concise.
--- kernel/cgroup/freezer.c | 67 ++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 35 deletions(-)
kernel/cgroup/freezer.c +32 -35
@@ -8,6 +8,28 @@#include <trace/events/cgroup.h> +/* +* Update CGRP_FROZEN of cgroup.flag +* Return true if flags is updated; false if flags has no change +*/ +static bool cgroup_update_frozen_flag (struct cgroup *cgrp,bool frozen) +{ +lockdep_assert_held (&css_set_lock); + +/* Already there? */ +if (test_bit (CGRP_FROZEN , &cgrp->flags) == frozen) +return false; + +if (frozen) +set_bit (CGRP_FROZEN , &cgrp->flags); +else +clear_bit (CGRP_FROZEN , &cgrp->flags); + +cgroup_file_notify (&cgrp->events_file); +TRACE_CGROUP_PATH (notify_frozen, cgrp, frozen); +return true; +} +/* * Propagate the cgroup frozen state upwards by the cgroup tree. */
@@ -24,24 +46,16 @@ static void cgroup_propagate_frozen(struct cgroup *cgrp, bool frozen)while ((cgrp =cgroup_parent (cgrp))) {if (frozen) { cgrp->freezer.nr_frozen_descendants += desc; -if (!test_bit (CGRP_FROZEN , &cgrp->flags) && -test_bit (CGRP_FREEZE , &cgrp->flags) && - cgrp->freezer.nr_frozen_descendants == - cgrp->nr_descendants) { -set_bit (CGRP_FROZEN , &cgrp->flags); -cgroup_file_notify (&cgrp->events_file); -TRACE_CGROUP_PATH (notify_frozen, cgrp,1 ); - desc++; - } +if (!test_bit (CGRP_FREEZE , &cgrp->flags) || + (cgrp->freezer.nr_frozen_descendants != + cgrp->nr_descendants)) +continue ; }else { cgrp->freezer.nr_frozen_descendants -= desc; -if (test_bit (CGRP_FROZEN , &cgrp->flags)) { -clear_bit (CGRP_FROZEN , &cgrp->flags); -cgroup_file_notify (&cgrp->events_file); -TRACE_CGROUP_PATH (notify_frozen, cgrp,0 ); - desc++; - } } + +if (cgroup_update_frozen_flag (cgrp, frozen)) + desc++; } }
@@ -53,8 +67,6 @@ void cgroup_update_frozen(struct cgroup *cgrp) {bool frozen; -lockdep_assert_held (&css_set_lock); -/* * If the cgroup has to be frozen (CGRP_FREEZE bit set), * and all tasks are frozen and/or stopped, let's consider
@@ -63,24 +75,9 @@ void cgroup_update_frozen(struct cgroup *cgrp) frozen =test_bit (CGRP_FREEZE , &cgrp->flags) && cgrp->freezer.nr_frozen_tasks ==__cgroup_task_count (cgrp); -if (frozen) { -/* Already there? */ -if (test_bit (CGRP_FROZEN , &cgrp->flags)) -return ; - -set_bit (CGRP_FROZEN , &cgrp->flags); - }else { -/* Already there? */ -if (!test_bit (CGRP_FROZEN , &cgrp->flags)) -return ; - -clear_bit (CGRP_FROZEN , &cgrp->flags); - } -cgroup_file_notify (&cgrp->events_file); -TRACE_CGROUP_PATH (notify_frozen, cgrp, frozen); - -/* Update the state of ancestor cgroups. */ -cgroup_propagate_frozen (cgrp, frozen); +/* If flags is updated, update the state of ancestor cgroups. */ +if (cgroup_update_frozen_flag (cgrp, frozen)) +cgroup_propagate_frozen (cgrp, frozen); }/*
Signature · 1 line
2.34.1
PATCH v2 3/3 cgroup/freezer: Reduce redundant propagation for cgroup_propagate_frozen
In reply to Chen RidongWhen 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 eIf 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.
--- include/linux/cgroup-defs.h | 4 +++- kernel/cgroup/freezer.c | 29 +++++++++++++++++------------ 2 files changed, 20 insertions(+), 13 deletions(-)
include/linux/cgroup-defs.h +3 -1
@@ -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;/*
kernel/cgroup/freezer.c +17 -12
@@ -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; 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); }/*
Signature · 1 line
2.34.1
kernel test robot
build botRe: [PATCH v2 -next 3/3] cgroup/freezer: Reduce redundant propagation for cgroup_propagate_frozen
kernel test robot noticed the following build warnings:
Hi Chen,
kernel test robot noticed the following build warnings:
[auto build test WARNING on next-20240911]
url: https://github.com/intel-lab-lkp/linux/commits/Chen-Ridong/cgroup-freezer-Reduce-redundant-traversal-for-cgroup_freeze/20240912-093141 base: next-20240911 patch link: https://lore.kernel.org/r/20240912012037.1324165-4-chenridong%40huawei.com patch subject: [PATCH v2 -next 3/3] cgroup/freezer: Reduce redundant propagation for cgroup_propagate_frozen config: i386-defconfig (https://download.01.org/0day-ci/archive/20240915/[email protected]/config) compiler: clang version 18.1.8 (https://github.com/llvm/llvm-project 3b5b5c1ec4a3095ab096dd780e84d7ab81f3d7ff) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240915/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> kernel/cgroup/freezer.c:46:12: warning: explicitly assigning value of variable of type 'struct cgroup *' to itself [-Wself-assign]
46 | for (cgrp = cgrp; cgrp; cgrp = cgroup_parent(cgrp)) {
| ~~~~ ^ ~~~~1 warning generated.
vim +46 kernel/cgroup/freezer.c
32
33 /*
34 * Propagate the cgroup frozen state upwards by the cgroup tree.
35 */
36 static void cgroup_propagate_frozen(struct cgroup *cgrp, bool frozen)
37 {
38 int deta;
39 struct cgroup *parent;
40 /*
41 * If the new state is frozen, some freezing ancestor cgroups may change
42 * their state too, depending on if all their descendants are frozen.
43 *
44 * Otherwise, all ancestor cgroups are forced into the non-frozen state.
45 */
> 46 for (cgrp = cgrp; cgrp; cgrp = cgroup_parent(cgrp)) {
47 if (frozen) {
48 /* If freezer is not set, or cgrp has descendants
49 * that are not frozen, cgrp can't be frozen
50 */
51 if (!test_bit(CGRP_FREEZE, &cgrp->flags) ||
52 (cgrp->freezer.nr_frozen_descendants !=
53 cgrp->nr_descendants))
54 break;
55 deta = cgrp->freezer.nr_frozen_descendants + 1;
56 } else {
57 deta = -(cgrp->freezer.nr_frozen_descendants + 1);
58 }
59
60 /* No change, stop propagate */
61 if (!cgroup_update_frozen_flag(cgrp, frozen))
62 break;
63
64 parent = cgroup_parent(cgrp);
65 parent->freezer.nr_frozen_descendants += deta;
66 }
67 }
68 Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki