Skip to content

Commit 4dcabec

Browse files
rgushchinhtejun
authored andcommitted
cgroup: protect cgroup->nr_(dying_)descendants by css_set_lock
The number of descendant cgroups and the number of dying descendant cgroups are currently synchronized using the cgroup_mutex. The number of descendant cgroups will be required by the cgroup v2 freezer, which will use it to determine if a cgroup is frozen (depending on total number of descendants and number of frozen descendants). It's not always acceptable to grab the cgroup_mutex, especially from quite hot paths (e.g. exit()). To avoid this, let's additionally synchronize these counters using the css_set_lock. So, it's safe to read these counters with either cgroup_mutex or css_set_lock locked, and for changing both locks should be acquired. Signed-off-by: Roman Gushchin <[email protected]> Signed-off-by: Tejun Heo <[email protected]> Cc: [email protected]
1 parent aade7f9 commit 4dcabec

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

include/linux/cgroup-defs.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,11 @@ struct cgroup {
349349
* Dying cgroups are cgroups which were deleted by a user,
350350
* but are still existing because someone else is holding a reference.
351351
* max_descendants is a maximum allowed number of descent cgroups.
352+
*
353+
* nr_descendants and nr_dying_descendants are protected
354+
* by cgroup_mutex and css_set_lock. It's fine to read them holding
355+
* any of cgroup_mutex and css_set_lock; for writing both locks
356+
* should be held.
352357
*/
353358
int nr_descendants;
354359
int nr_dying_descendants;

kernel/cgroup/cgroup.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4811,9 +4811,11 @@ static void css_release_work_fn(struct work_struct *work)
48114811
if (cgroup_on_dfl(cgrp))
48124812
cgroup_rstat_flush(cgrp);
48134813

4814+
spin_lock_irq(&css_set_lock);
48144815
for (tcgrp = cgroup_parent(cgrp); tcgrp;
48154816
tcgrp = cgroup_parent(tcgrp))
48164817
tcgrp->nr_dying_descendants--;
4818+
spin_unlock_irq(&css_set_lock);
48174819

48184820
cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
48194821
cgrp->id = -1;
@@ -5031,12 +5033,14 @@ static struct cgroup *cgroup_create(struct cgroup *parent)
50315033
if (ret)
50325034
goto out_psi_free;
50335035

5036+
spin_lock_irq(&css_set_lock);
50345037
for (tcgrp = cgrp; tcgrp; tcgrp = cgroup_parent(tcgrp)) {
50355038
cgrp->ancestor_ids[tcgrp->level] = tcgrp->id;
50365039

50375040
if (tcgrp != cgrp)
50385041
tcgrp->nr_descendants++;
50395042
}
5043+
spin_unlock_irq(&css_set_lock);
50405044

50415045
if (notify_on_release(parent))
50425046
set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
@@ -5321,10 +5325,12 @@ static int cgroup_destroy_locked(struct cgroup *cgrp)
53215325
if (parent && cgroup_is_threaded(cgrp))
53225326
parent->nr_threaded_children--;
53235327

5328+
spin_lock_irq(&css_set_lock);
53245329
for (tcgrp = cgroup_parent(cgrp); tcgrp; tcgrp = cgroup_parent(tcgrp)) {
53255330
tcgrp->nr_descendants--;
53265331
tcgrp->nr_dying_descendants++;
53275332
}
5333+
spin_unlock_irq(&css_set_lock);
53285334

53295335
cgroup1_check_for_release(parent);
53305336

0 commit comments

Comments
 (0)