cgroup hierarchy controls and stats
Subsystems (2)
cover cgroup hierarchy controls and stats
Creating cgroup hierearchies of an unreasonable size can affect system performance. A user might want to limit the size of the cgroup hierarchy.
This patchset implements an ability to control and monitor cgroup hierarchy size.
Patch 1 implements tracking of live and dying descendant cgroups
on each cgroup level.
Patch 2 adds cgroup.max.descendants and cgroup.max.depth interfaces
to set up hierarchy limits.
Patch 3 adds cgroup.stat interface with simple hierarchy stats. Patch 4 is a trivial cleanup.
Roman Gushchin (4):
cgroup: keep track of number of descent cgroups
cgroup: implement hierarchy limits
cgroup: add cgroup.stat interface with basic hierarchy stats
cgroup: re-use the parent pointer in cgroup_destroy_locked()
Documentation/cgroup-v2.txt | 32 +++++++++ include/linux/cgroup-defs.h | 13 ++++ kernel/cgroup/cgroup.c | 163 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 205 insertions(+), 3 deletions(-)
--
2.13.3
RFC 1/4 cgroup: keep track of number of descent cgroups
Keep track of the number of online and dying descent cgroups.
This data will be used later to add an ability to control cgroup hierarchy (limit the depth and the number of descent cgroups) and display hierarchy stats.
--- include/linux/cgroup-defs.h | 8 ++++++++ kernel/cgroup/cgroup.c | 19 +++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-)
include/linux/cgroup-defs.h +8 -0
@@ -274,6 +274,14 @@ struct cgroup {int level;/* +* Keep track of total numbers of visible and dying descent cgroups. +* Dying cgroups are cgroups which were deleted by a user, +* but are still existing because someone else is holding a reference. +*/ +int nr_descendants; +int nr_dying_descendants; + +/* * Each non-empty css_set associated with this cgroup contributes * one to nr_populated_csets. The counter is zero iff this cgroup * doesn't have any tasks.
kernel/cgroup/cgroup.c +17 -2
@@ -4408,9 +4408,15 @@ static void css_release_work_fn(struct work_struct *work)if (ss->css_released) ss->css_released (css); }else { +struct cgroup *tcgrp; +/* cgroup release path */ trace_cgroup_release (cgrp); +for (tcgrp =cgroup_parent (cgrp); tcgrp; + tcgrp =cgroup_parent (tcgrp)) + tcgrp->nr_dying_descendants--; +cgroup_idr_remove (&cgrp->root->cgroup_idr, cgrp->id); cgrp->id =-1 ;
@@ -4609,9 +4615,13 @@ static struct cgroup *cgroup_create(struct cgroup *parent) cgrp->root = root; cgrp->level = level; -for (tcgrp = cgrp; tcgrp; tcgrp =cgroup_parent (tcgrp)) +for (tcgrp = cgrp; tcgrp; tcgrp =cgroup_parent (tcgrp)) { cgrp->ancestor_ids[tcgrp->level] = tcgrp->id; +if (tcgrp != cgrp) + tcgrp->nr_descendants++; + } +if (notify_on_release (parent))set_bit (CGRP_NOTIFY_ON_RELEASE , &cgrp->flags);
@@ -4817,7 +4827,7 @@ static void kill_css(struct cgroup_subsys_state *css)static int cgroup_destroy_locked (struct cgroup *cgrp) __releases(&cgroup_mutex) __acquires(&cgroup_mutex) { -struct cgroup *parent =cgroup_parent (cgrp); +struct cgroup *tcgrp, *parent =cgroup_parent (cgrp);struct cgroup_subsys_state *css;struct cgrp_cset_link *link;int ssid;
@@ -4865,6 +4875,11 @@ static int cgroup_destroy_locked(struct cgroup *cgrp)if (parent &&cgroup_is_threaded (cgrp)) parent->nr_threaded_children--; +for (tcgrp =cgroup_parent (cgrp); tcgrp; tcgrp =cgroup_parent (tcgrp)) { + tcgrp->nr_descendants--; + tcgrp->nr_dying_descendants++; + } +cgroup1_check_for_release (cgroup_parent (cgrp));/* put the base reference */
Signature · 1 line
2.13.3
RFC 2/4 cgroup: implement hierarchy limits
In reply to Roman GushchinCreating cgroup hierearchies of unreasonable size can affect overall system performance. A user might want to limit the size of cgroup hierarchy. This is especially important if a user is delegating some cgroup sub-tree.
To address this issue, introduce an ability to control the size of cgroup hierarchy.
The cgroup.max.descendants control file allows to set the maximum allowed number of descendant cgroups.
The cgroup.max.depth file controls the maximum depth of the cgroup tree. Both are single value r/w files, with "max" default value.
The control files exist on each hierarchy level (including root). When a new cgroup is created, we check the total descendants and depth limits on each level, and if none of them are exceeded, a new cgroup is created.
Only alive cgroups are counted, removed (dying) cgroups are ignored.
--- Documentation/cgroup-v2.txt | 14 +++++ include/linux/cgroup-defs.h | 5 ++ kernel/cgroup/cgroup.c | 126 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 145 insertions(+)
Documentation/cgroup-v2.txt +14 -0
@@ -854,6 +854,20 @@ All cgroup core files are prefixed with "cgroup." 1 if the cgroup or its descendants contains any live processes; otherwise, 0. + cgroup.max.descendants + A read-write single value files. The default is "max". + + Maximum allowed number of descent cgroups. + If the actual number of descendants is equal or larger, + an attempt to create a new cgroup in the hierarchy will fail. + + cgroup.max.depth + A read-write single value files. The default is "max". + + Maximum allowed descent depth below the current cgroup. + If the actual descent depth is equal or larger, + an attempt to create a new child cgroup will fail. + Controllers ===========
include/linux/cgroup-defs.h +5 -0
@@ -273,13 +273,18 @@ struct cgroup {*/ int level; +/* Maximum allowed descent tree depth */ +int max_depth; +/* * Keep track of total numbers of visible and dying descent cgroups. * Dying cgroups are cgroups which were deleted by a user, * but are still existing because someone else is holding a reference. +* max_descendants is a maximum allowed number of descent cgroups. */ int nr_descendants;int nr_dying_descendants; +int max_descendants;/* * Each non-empty css_set associated with this cgroup contributes
kernel/cgroup/cgroup.c +126 -0
156 lines not on this page · raw message
Signature · 1 line
2.13.3
Tejun Heo
maintainerRe: [RFC 2/4] cgroup: implement hierarchy limits
Hello, Roman.
Generally looks good to me. One minor nit.
kernel/cgroup/cgroup.c static ssize_t cgroup_type_write(struct kernfs_open_file *of, char *buf, · line 3238
Quoted 12 lines · Roman Gushchin
kernel/cgroup/cgroup.c +12
-0+static ssize_t cgroup_max_descendants_write (struct kernfs_open_file *of, +char *buf,size_t nbytes,loff_t off) +{ +struct cgroup *cgrp; +int descendants; +ssize_t ret; + + buf =strstrip (buf); +if (!strcmp (buf,"max" )) { + descendants =INT_MAX ; + }else { + ret =kstrtouint (buf,0 , &descendants);
^^^^^^^^^^^
shouldn't this be kstrtoint?
Thanks.
Signature · 1 line
tejun
Re: [RFC 2/4] cgroup: implement hierarchy limits
Quoted 5 lines · Tejun Heo
Hello, Roman.
Generally looks good to me. One minor nit.
On Wed, Aug 02, 2017 at 05:55:30PM +0100, Roman Gushchin wrote:
Quoted 14 lines
Review
Quoted 12 lines
(quoted diff) +12
-0+static ssize_t cgroup_max_descendants_write(struct kernfs_open_file *of, + char *buf, size_t nbytes, loff_t off) +{ + struct cgroup *cgrp; + int descendants; + ssize_t ret; + + buf = strstrip(buf); + if (!strcmp(buf, "max")) { + descendants = INT_MAX; + } else { + ret = kstrtouint(buf, 0, &descendants);^^^^^^^^^^^
shouldn't this be kstrtoint?
Hi, Tejun!
Of course, it should.
Please, find an updated version below.
Thank you!
Roman
--
From c6492640f7a70b88e4b573c6f04081e3c82ce8fa Mon Sep 17 00:00:00 2001
From: Roman Gushchin <[email protected]>
Date: Fri, 28 Jul 2017 18:28:44 +0100
Subject: [PATCH 2/4] cgroup: implement hierarchy limits
Creating cgroup hierearchies of unreasonable size can affect overall system performance. A user might want to limit the size of cgroup hierarchy. This is especially important if a user is delegating some cgroup sub-tree.
To address this issue, introduce an ability to control the size of cgroup hierarchy.
The cgroup.max.descendants control file allows to set the maximum allowed number of descendant cgroups.
The cgroup.max.depth file controls the maximum depth of the cgroup tree. Both are single value r/w files, with "max" default value.
The control files exist on each hierarchy level (including root). When a new cgroup is created, we check the total descendants and depth limits on each level, and if none of them are exceeded, a new cgroup is created.
Only alive cgroups are counted, removed (dying) cgroups are ignored.
--- Documentation/cgroup-v2.txt | 14 +++++ include/linux/cgroup-defs.h | 5 ++ kernel/cgroup/cgroup.c | 126 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 145 insertions(+)
Documentation/cgroup-v2.txt +14 -0
@@ -854,6 +854,20 @@ All cgroup core files are prefixed with "cgroup." 1 if the cgroup or its descendants contains any live processes; otherwise, 0. + cgroup.max.descendants + A read-write single value files. The default is "max". + + Maximum allowed number of descent cgroups. + If the actual number of descendants is equal or larger, + an attempt to create a new cgroup in the hierarchy will fail. + + cgroup.max.depth + A read-write single value files. The default is "max". + + Maximum allowed descent depth below the current cgroup. + If the actual descent depth is equal or larger, + an attempt to create a new child cgroup will fail. + Controllers ===========
include/linux/cgroup-defs.h +5 -0
@@ -273,13 +273,18 @@ struct cgroup {*/ int level; +/* Maximum allowed descent tree depth */ +int max_depth; +/* * Keep track of total numbers of visible and dying descent cgroups. * Dying cgroups are cgroups which were deleted by a user, * but are still existing because someone else is holding a reference. +* max_descendants is a maximum allowed number of descent cgroups. */ int nr_descendants;int nr_dying_descendants; +int max_descendants;/* * Each non-empty css_set associated with this cgroup contributes
kernel/cgroup/cgroup.c +126 -0
156 lines not on this page · raw message
Signature · 1 line
2.13.3
RFC 3/4 cgroup: add cgroup.stat interface with basic hierarchy stats
In reply to Roman GushchinA cgroup can consume resources even after being deleted by a user. For example, writing back dirty pages should be accounted and limited, despite the corresponding cgroup might contain no processes and being deleted by a user.
In the current implementation a cgroup can remain in such "dying" state for an undefined amount of time. For instance, if a memory cgroup contains a pge, mlocked by a process belonging to an other cgroup.
Although the lifecycle of a dying cgroup is out of user's control, it's important to have some insight of what's going on under the hood.
In particular, it's handy to have a counter which will allow to detect css leaks.
To solve this problem, add a cgroup.stat interface to the base cgroup control files with the following metrics:
nr_descendants total number of visible descendant cgroupsnr_dying_descendants total number of dying descendant cgroups
--- Documentation/cgroup-v2.txt | 18 ++++++++++++++++++ kernel/cgroup/cgroup.c | 16 ++++++++++++++++ 2 files changed, 34 insertions(+)
Documentation/cgroup-v2.txt +18 -0
@@ -868,6 +868,24 @@ All cgroup core files are prefixed with "cgroup." If the actual descent depth is equal or larger, an attempt to create a new child cgroup will fail. + cgroup.stat + A read-only flat-keyed file with the following entries: + + nr_descendants + Total number of visible descendant cgroups. + + nr_dying_descendants + Total number of dying descendant cgroups. A cgroup becomes + dying after being deleted by a user. The cgroup will remain + in dying state for some time undefined time (which can depend + on system load) before being completely destroyed. + + A process can't enter a dying cgroup under any circumstances, + a dying cgroup can't revive. + + A dying cgroup can consume system resources not exceeding + limits, which were active at the moment of cgroup deletion. + Controllers ===========
kernel/cgroup/cgroup.c +16 -0
@@ -3304,6 +3304,18 @@ static int cgroup_events_show(struct seq_file *seq, void *v)return 0 ; } +static int cgroup_stats_show (struct seq_file *seq,void *v) +{ +struct cgroup *cgroup =seq_css (seq)->cgroup; + +seq_printf (seq,"nr_descendants %d\n" , + cgroup->nr_descendants); +seq_printf (seq,"nr_dying_descendants %d\n" , + cgroup->nr_dying_descendants); + +return 0 ; +} +static int cgroup_file_open (struct kernfs_open_file *of) {struct cftype *cft = of->kn->priv;
@@ -4407,6 +4419,10 @@ static struct cftype cgroup_base_files[] = { .seq_show = cgroup_max_depth_show, .write = cgroup_max_depth_write, }, + { + .name = "cgroup.stat", + .seq_show = cgroup_stats_show, + }, { }/* terminate */ };
Signature · 1 line
2.13.3
RFC 4/4 cgroup: re-use the parent pointer in cgroup_destroy_locked()
In reply to Roman GushchinAs we already have a pointer to the parent cgroup in cgroup_destroy_locked(), we don't need to calculate it again to pass as an argument for cgroup1_check_for_release().
--- kernel/cgroup/cgroup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/cgroup/cgroup.c +1 -1
@@ -5022,7 +5022,7 @@ static int cgroup_destroy_locked(struct cgroup *cgrp) tcgrp->nr_dying_descendants++; } -cgroup1_check_for_release (cgroup_parent (cgrp)); +cgroup1_check_for_release (parent);/* put the base reference */ percpu_ref_kill (&cgrp->self.refcnt);
Signature · 1 line
2.13.3
Tejun Heo
maintainerRe: [RFC 4/4] cgroup: re-use the parent pointer in cgroup_destroy_locked()
Quoted 3 lines · Roman Gushchin
As we already have a pointer to the parent cgroup in
cgroup_destroy_locked(), we don't need to calculate it again to pass as an argument forcgroup1_check_for_release().
Except for the minor nit earlier, everything in the series looks good to me.
Thanks.
Signature · 1 line
tejun
A message that is not in the archive (sent off-list or not yet received): [email protected]
Hello,
Quoted 7 lines · Roman Gushchin
Creating cgroup hierearchies of an unreasonable size can affect system performance. A user might want to limit the size of the cgroup hierarchy.
This patchset implements an ability to control and monitor cgroup hierarchy size.
Quoted 6 lines
Patch 1 implements tracking of live and dying descendant cgroups
on each cgroup level.
Patch 2 adds cgroup.max.descendants and cgroup.max.depth interfaces
to set up hierarchy limits.
Patch 3 adds cgroup.stat interface with simple hierarchy stats. Patch 4 is a trivial cleanup.
Applied 1-4 to cgroup/for-4.14. We *might* have further discussions on the specifics of the interface but in terms of feature and internal implementation, I don't think there's anything which can be controversial.
Thanks.
Signature · 1 line
tejun