Skip to content

Commit 8870326

Browse files
hkamezawatorvalds
authored andcommitted
cgroup avoid permanent sleep at rmdir
After commit ec64f51 ("cgroup: fix frequent -EBUSY at rmdir"), cgroup's rmdir (especially against memcg) doesn't return -EBUSY by temporary ref counts. That commit expects all refs after pre_destroy() is temporary but...it wasn't. Then, rmdir can wait permanently. This patch tries to fix that and change followings. - set CGRP_WAIT_ON_RMDIR flag before pre_destroy(). - clear CGRP_WAIT_ON_RMDIR flag when the subsys finds racy case. if there are sleeping ones, wakes them up. - rmdir() sleeps only when CGRP_WAIT_ON_RMDIR flag is set. Tested-by: Daisuke Nishimura <nishimura@mxp.nes.nec.co.jp> Reported-by: Daisuke Nishimura <nishimura@mxp.nes.nec.co.jp> Reviewed-by: Paul Menage <menage@google.com> Acked-by: Balbir Sigh <balbir@linux.vnet.ibm.com> Signed-off-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent f0d8367 commit 8870326

3 files changed

Lines changed: 75 additions & 20 deletions

File tree

‎include/linux/cgroup.h‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,23 @@ int cgroup_task_count(const struct cgroup *cgrp);
362362
/* Return true if cgrp is a descendant of the task's cgroup */
363363
int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task);
364364

365+
/*
366+
* When the subsys has to access css and may add permanent refcnt to css,
367+
* it should take care of racy conditions with rmdir(). Following set of
368+
* functions, is for stop/restart rmdir if necessary.
369+
* Because these will call css_get/put, "css" should be alive css.
370+
*
371+
* cgroup_exclude_rmdir();
372+
* ...do some jobs which may access arbitrary empty cgroup
373+
* cgroup_release_and_wakeup_rmdir();
374+
*
375+
* When someone removes a cgroup while cgroup_exclude_rmdir() holds it,
376+
* it sleeps and cgroup_release_and_wakeup_rmdir() will wake him up.
377+
*/
378+
379+
void cgroup_exclude_rmdir(struct cgroup_subsys_state *css);
380+
void cgroup_release_and_wakeup_rmdir(struct cgroup_subsys_state *css);
381+
365382
/*
366383
* Control Group subsystem type.
367384
* See Documentation/cgroups/cgroups.txt for details

‎kernel/cgroup.c‎

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -735,16 +735,28 @@ static void cgroup_d_remove_dir(struct dentry *dentry)
735735
* reference to css->refcnt. In general, this refcnt is expected to goes down
736736
* to zero, soon.
737737
*
738-
* CGRP_WAIT_ON_RMDIR flag is modified under cgroup's inode->i_mutex;
738+
* CGRP_WAIT_ON_RMDIR flag is set under cgroup's inode->i_mutex;
739739
*/
740740
DECLARE_WAIT_QUEUE_HEAD(cgroup_rmdir_waitq);
741741

742-
static void cgroup_wakeup_rmdir_waiters(const struct cgroup *cgrp)
742+
static void cgroup_wakeup_rmdir_waiter(struct cgroup *cgrp)
743743
{
744-
if (unlikely(test_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags)))
744+
if (unlikely(test_and_clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags)))
745745
wake_up_all(&cgroup_rmdir_waitq);
746746
}
747747

748+
void cgroup_exclude_rmdir(struct cgroup_subsys_state *css)
749+
{
750+
css_get(css);
751+
}
752+
753+
void cgroup_release_and_wakeup_rmdir(struct cgroup_subsys_state *css)
754+
{
755+
cgroup_wakeup_rmdir_waiter(css->cgroup);
756+
css_put(css);
757+
}
758+
759+
748760
static int rebind_subsystems(struct cgroupfs_root *root,
749761
unsigned long final_bits)
750762
{
@@ -1359,7 +1371,7 @@ int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
13591371
* wake up rmdir() waiter. the rmdir should fail since the cgroup
13601372
* is no longer empty.
13611373
*/
1362-
cgroup_wakeup_rmdir_waiters(cgrp);
1374+
cgroup_wakeup_rmdir_waiter(cgrp);
13631375
return 0;
13641376
}
13651377

@@ -2743,34 +2755,43 @@ static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
27432755
}
27442756
mutex_unlock(&cgroup_mutex);
27452757

2758+
/*
2759+
* In general, subsystem has no css->refcnt after pre_destroy(). But
2760+
* in racy cases, subsystem may have to get css->refcnt after
2761+
* pre_destroy() and it makes rmdir return with -EBUSY. This sometimes
2762+
* make rmdir return -EBUSY too often. To avoid that, we use waitqueue
2763+
* for cgroup's rmdir. CGRP_WAIT_ON_RMDIR is for synchronizing rmdir
2764+
* and subsystem's reference count handling. Please see css_get/put
2765+
* and css_tryget() and cgroup_wakeup_rmdir_waiter() implementation.
2766+
*/
2767+
set_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
2768+
27462769
/*
27472770
* Call pre_destroy handlers of subsys. Notify subsystems
27482771
* that rmdir() request comes.
27492772
*/
27502773
ret = cgroup_call_pre_destroy(cgrp);
2751-
if (ret)
2774+
if (ret) {
2775+
clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
27522776
return ret;
2777+
}
27532778

27542779
mutex_lock(&cgroup_mutex);
27552780
parent = cgrp->parent;
27562781
if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) {
2782+
clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
27572783
mutex_unlock(&cgroup_mutex);
27582784
return -EBUSY;
27592785
}
2760-
/*
2761-
* css_put/get is provided for subsys to grab refcnt to css. In typical
2762-
* case, subsystem has no reference after pre_destroy(). But, under
2763-
* hierarchy management, some *temporal* refcnt can be hold.
2764-
* To avoid returning -EBUSY to a user, waitqueue is used. If subsys
2765-
* is really busy, it should return -EBUSY at pre_destroy(). wake_up
2766-
* is called when css_put() is called and refcnt goes down to 0.
2767-
*/
2768-
set_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
27692786
prepare_to_wait(&cgroup_rmdir_waitq, &wait, TASK_INTERRUPTIBLE);
2770-
27712787
if (!cgroup_clear_css_refs(cgrp)) {
27722788
mutex_unlock(&cgroup_mutex);
2773-
schedule();
2789+
/*
2790+
* Because someone may call cgroup_wakeup_rmdir_waiter() before
2791+
* prepare_to_wait(), we need to check this flag.
2792+
*/
2793+
if (test_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags))
2794+
schedule();
27742795
finish_wait(&cgroup_rmdir_waitq, &wait);
27752796
clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
27762797
if (signal_pending(current))
@@ -3342,7 +3363,7 @@ void __css_put(struct cgroup_subsys_state *css)
33423363
set_bit(CGRP_RELEASABLE, &cgrp->flags);
33433364
check_for_release(cgrp);
33443365
}
3345-
cgroup_wakeup_rmdir_waiters(cgrp);
3366+
cgroup_wakeup_rmdir_waiter(cgrp);
33463367
}
33473368
rcu_read_unlock();
33483369
}

‎mm/memcontrol.c‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,12 @@ static int mem_cgroup_move_account(struct page_cgroup *pc,
12071207
ret = 0;
12081208
out:
12091209
unlock_page_cgroup(pc);
1210+
/*
1211+
* We charges against "to" which may not have any tasks. Then, "to"
1212+
* can be under rmdir(). But in current implementation, caller of
1213+
* this function is just force_empty() and it's garanteed that
1214+
* "to" is never removed. So, we don't check rmdir status here.
1215+
*/
12101216
return ret;
12111217
}
12121218

@@ -1428,6 +1434,7 @@ __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
14281434
return;
14291435
if (!ptr)
14301436
return;
1437+
cgroup_exclude_rmdir(&ptr->css);
14311438
pc = lookup_page_cgroup(page);
14321439
mem_cgroup_lru_del_before_commit_swapcache(page);
14331440
__mem_cgroup_commit_charge(ptr, pc, ctype);
@@ -1457,8 +1464,12 @@ __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
14571464
}
14581465
rcu_read_unlock();
14591466
}
1460-
/* add this page(page_cgroup) to the LRU we want. */
1461-
1467+
/*
1468+
* At swapin, we may charge account against cgroup which has no tasks.
1469+
* So, rmdir()->pre_destroy() can be called while we do this charge.
1470+
* In that case, we need to call pre_destroy() again. check it here.
1471+
*/
1472+
cgroup_release_and_wakeup_rmdir(&ptr->css);
14621473
}
14631474

14641475
void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
@@ -1664,7 +1675,7 @@ void mem_cgroup_end_migration(struct mem_cgroup *mem,
16641675

16651676
if (!mem)
16661677
return;
1667-
1678+
cgroup_exclude_rmdir(&mem->css);
16681679
/* at migration success, oldpage->mapping is NULL. */
16691680
if (oldpage->mapping) {
16701681
target = oldpage;
@@ -1704,6 +1715,12 @@ void mem_cgroup_end_migration(struct mem_cgroup *mem,
17041715
*/
17051716
if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
17061717
mem_cgroup_uncharge_page(target);
1718+
/*
1719+
* At migration, we may charge account against cgroup which has no tasks
1720+
* So, rmdir()->pre_destroy() can be called while we do this charge.
1721+
* In that case, we need to call pre_destroy() again. check it here.
1722+
*/
1723+
cgroup_release_and_wakeup_rmdir(&mem->css);
17071724
}
17081725

17091726
/*

0 commit comments

Comments
 (0)