Skip to content

Commit 455050d

Browse files
committed
cgroup: reorder the operations in cgroup_destroy_locked()
This patch reorders the operations in cgroup_destroy_locked() such that the userland visible parts happen before css offlining and removal from the ->sibling list. This will be used to make css use percpu refcnt. While at it, split out CGRP_DEAD related comment from the refcnt deactivation one and correct / clarify how different guarantees are met. While this patch changes the specific order of operations, it shouldn't cause any noticeable behavior difference. Signed-off-by: Tejun Heo <tj@kernel.org> Acked-by: Li Zefan <lizefan@huawei.com>
1 parent 6f3d828 commit 455050d

1 file changed

Lines changed: 35 additions & 26 deletions

File tree

‎kernel/cgroup.c‎

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4379,22 +4379,50 @@ static int cgroup_destroy_locked(struct cgroup *cgrp)
43794379

43804380
/*
43814381
* Block new css_tryget() by deactivating refcnt and mark @cgrp
4382-
* removed. This makes future css_tryget() and child creation
4383-
* attempts fail thus maintaining the removal conditions verified
4384-
* above.
4385-
*
4386-
* Note that CGRP_DEAD assertion is depended upon by
4387-
* cgroup_next_sibling() to resume iteration after dropping RCU
4388-
* read lock. See cgroup_next_sibling() for details.
4382+
* removed. This makes future css_tryget() attempts fail which we
4383+
* guarantee to ->css_offline() callbacks.
43894384
*/
43904385
for_each_subsys(cgrp->root, ss) {
43914386
struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
43924387

43934388
WARN_ON(atomic_read(&css->refcnt) < 0);
43944389
atomic_add(CSS_DEACT_BIAS, &css->refcnt);
43954390
}
4391+
4392+
/*
4393+
* Mark @cgrp dead. This prevents further task migration and child
4394+
* creation by disabling cgroup_lock_live_group(). Note that
4395+
* CGRP_DEAD assertion is depended upon by cgroup_next_sibling() to
4396+
* resume iteration after dropping RCU read lock. See
4397+
* cgroup_next_sibling() for details.
4398+
*/
43964399
set_bit(CGRP_DEAD, &cgrp->flags);
43974400

4401+
/* CGRP_DEAD is set, remove from ->release_list for the last time */
4402+
raw_spin_lock(&release_list_lock);
4403+
if (!list_empty(&cgrp->release_list))
4404+
list_del_init(&cgrp->release_list);
4405+
raw_spin_unlock(&release_list_lock);
4406+
4407+
/*
4408+
* Remove @cgrp directory. The removal puts the base ref but we
4409+
* aren't quite done with @cgrp yet, so hold onto it.
4410+
*/
4411+
dget(d);
4412+
cgroup_d_remove_dir(d);
4413+
4414+
/*
4415+
* Unregister events and notify userspace.
4416+
* Notify userspace about cgroup removing only after rmdir of cgroup
4417+
* directory to avoid race between userspace and kernelspace.
4418+
*/
4419+
spin_lock(&cgrp->event_list_lock);
4420+
list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4421+
list_del_init(&event->list);
4422+
schedule_work(&event->remove);
4423+
}
4424+
spin_unlock(&cgrp->event_list_lock);
4425+
43984426
/* tell subsystems to initate destruction */
43994427
for_each_subsys(cgrp->root, ss)
44004428
offline_css(ss, cgrp);
@@ -4409,34 +4437,15 @@ static int cgroup_destroy_locked(struct cgroup *cgrp)
44094437
for_each_subsys(cgrp->root, ss)
44104438
css_put(cgrp->subsys[ss->subsys_id]);
44114439

4412-
raw_spin_lock(&release_list_lock);
4413-
if (!list_empty(&cgrp->release_list))
4414-
list_del_init(&cgrp->release_list);
4415-
raw_spin_unlock(&release_list_lock);
4416-
44174440
/* delete this cgroup from parent->children */
44184441
list_del_rcu(&cgrp->sibling);
44194442
list_del_init(&cgrp->allcg_node);
44204443

4421-
dget(d);
4422-
cgroup_d_remove_dir(d);
44234444
dput(d);
44244445

44254446
set_bit(CGRP_RELEASABLE, &parent->flags);
44264447
check_for_release(parent);
44274448

4428-
/*
4429-
* Unregister events and notify userspace.
4430-
* Notify userspace about cgroup removing only after rmdir of cgroup
4431-
* directory to avoid race between userspace and kernelspace.
4432-
*/
4433-
spin_lock(&cgrp->event_list_lock);
4434-
list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4435-
list_del_init(&event->list);
4436-
schedule_work(&event->remove);
4437-
}
4438-
spin_unlock(&cgrp->event_list_lock);
4439-
44404449
return 0;
44414450
}
44424451

0 commit comments

Comments
 (0)