Skip to content

Commit ec64f51

Browse files
hkamezawatorvalds
authored andcommitted
cgroup: fix frequent -EBUSY at rmdir
In following situation, with memory subsystem, /groupA use_hierarchy==1 /01 some tasks /02 some tasks /03 some tasks /04 empty When tasks under 01/02/03 hit limit on /groupA, hierarchical reclaim is triggered and the kernel walks tree under groupA. In this case, rmdir /groupA/04 fails with -EBUSY frequently because of temporal refcnt from the kernel. In general. cgroup can be rmdir'd if there are no children groups and no tasks. Frequent fails of rmdir() is not useful to users. (And the reason for -EBUSY is unknown to users.....in most cases) This patch tries to modify above behavior, by - retries if css_refcnt is got by someone. - add "return value" to pre_destroy() and allows subsystem to say "we're really busy!" Signed-off-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> Cc: Paul Menage <menage@google.com> Cc: Li Zefan <lizf@cn.fujitsu.com> Cc: Balbir Singh <balbir@in.ibm.com> Cc: Daisuke Nishimura <nishimura@mxp.nes.nec.co.jp> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 38460b4 commit ec64f51

4 files changed

Lines changed: 79 additions & 19 deletions

File tree

‎Documentation/cgroups/cgroups.txt‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,11 +476,13 @@ cgroup->parent is still valid. (Note - can also be called for a
476476
newly-created cgroup if an error occurs after this subsystem's
477477
create() method has been called for the new cgroup).
478478

479-
void pre_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp);
479+
int pre_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp);
480480

481481
Called before checking the reference count on each subsystem. This may
482482
be useful for subsystems which have some extra references even if
483-
there are not tasks in the cgroup.
483+
there are not tasks in the cgroup. If pre_destroy() returns error code,
484+
rmdir() will fail with it. From this behavior, pre_destroy() can be
485+
called multiple times against a cgroup.
484486

485487
int can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
486488
struct task_struct *task)

‎include/linux/cgroup.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ enum {
135135
CGRP_RELEASABLE,
136136
/* Control Group requires release notifications to userspace */
137137
CGRP_NOTIFY_ON_RELEASE,
138+
/*
139+
* A thread in rmdir() is wating for this cgroup.
140+
*/
141+
CGRP_WAIT_ON_RMDIR,
138142
};
139143

140144
struct cgroup {
@@ -360,7 +364,7 @@ int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task);
360364
struct cgroup_subsys {
361365
struct cgroup_subsys_state *(*create)(struct cgroup_subsys *ss,
362366
struct cgroup *cgrp);
363-
void (*pre_destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp);
367+
int (*pre_destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp);
364368
void (*destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp);
365369
int (*can_attach)(struct cgroup_subsys *ss,
366370
struct cgroup *cgrp, struct task_struct *tsk);

‎kernel/cgroup.c‎

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -622,13 +622,18 @@ static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb)
622622
* Call subsys's pre_destroy handler.
623623
* This is called before css refcnt check.
624624
*/
625-
static void cgroup_call_pre_destroy(struct cgroup *cgrp)
625+
static int cgroup_call_pre_destroy(struct cgroup *cgrp)
626626
{
627627
struct cgroup_subsys *ss;
628+
int ret = 0;
629+
628630
for_each_subsys(cgrp->root, ss)
629-
if (ss->pre_destroy)
630-
ss->pre_destroy(ss, cgrp);
631-
return;
631+
if (ss->pre_destroy) {
632+
ret = ss->pre_destroy(ss, cgrp);
633+
if (ret)
634+
break;
635+
}
636+
return ret;
632637
}
633638

634639
static void free_cgroup_rcu(struct rcu_head *obj)
@@ -722,6 +727,22 @@ static void cgroup_d_remove_dir(struct dentry *dentry)
722727
remove_dir(dentry);
723728
}
724729

730+
/*
731+
* A queue for waiters to do rmdir() cgroup. A tasks will sleep when
732+
* cgroup->count == 0 && list_empty(&cgroup->children) && subsys has some
733+
* reference to css->refcnt. In general, this refcnt is expected to goes down
734+
* to zero, soon.
735+
*
736+
* CGRP_WAIT_ON_RMDIR flag is modified under cgroup's inode->i_mutex;
737+
*/
738+
DECLARE_WAIT_QUEUE_HEAD(cgroup_rmdir_waitq);
739+
740+
static void cgroup_wakeup_rmdir_waiters(const struct cgroup *cgrp)
741+
{
742+
if (unlikely(test_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags)))
743+
wake_up_all(&cgroup_rmdir_waitq);
744+
}
745+
725746
static int rebind_subsystems(struct cgroupfs_root *root,
726747
unsigned long final_bits)
727748
{
@@ -1317,6 +1338,12 @@ int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
13171338
set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
13181339
synchronize_rcu();
13191340
put_css_set(cg);
1341+
1342+
/*
1343+
* wake up rmdir() waiter. the rmdir should fail since the cgroup
1344+
* is no longer empty.
1345+
*/
1346+
cgroup_wakeup_rmdir_waiters(cgrp);
13201347
return 0;
13211348
}
13221349

@@ -2608,9 +2635,11 @@ static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
26082635
struct cgroup *cgrp = dentry->d_fsdata;
26092636
struct dentry *d;
26102637
struct cgroup *parent;
2638+
DEFINE_WAIT(wait);
2639+
int ret;
26112640

26122641
/* the vfs holds both inode->i_mutex already */
2613-
2642+
again:
26142643
mutex_lock(&cgroup_mutex);
26152644
if (atomic_read(&cgrp->count) != 0) {
26162645
mutex_unlock(&cgroup_mutex);
@@ -2626,17 +2655,39 @@ static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
26262655
* Call pre_destroy handlers of subsys. Notify subsystems
26272656
* that rmdir() request comes.
26282657
*/
2629-
cgroup_call_pre_destroy(cgrp);
2658+
ret = cgroup_call_pre_destroy(cgrp);
2659+
if (ret)
2660+
return ret;
26302661

26312662
mutex_lock(&cgroup_mutex);
26322663
parent = cgrp->parent;
2633-
2634-
if (atomic_read(&cgrp->count)
2635-
|| !list_empty(&cgrp->children)
2636-
|| !cgroup_clear_css_refs(cgrp)) {
2664+
if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) {
26372665
mutex_unlock(&cgroup_mutex);
26382666
return -EBUSY;
26392667
}
2668+
/*
2669+
* css_put/get is provided for subsys to grab refcnt to css. In typical
2670+
* case, subsystem has no reference after pre_destroy(). But, under
2671+
* hierarchy management, some *temporal* refcnt can be hold.
2672+
* To avoid returning -EBUSY to a user, waitqueue is used. If subsys
2673+
* is really busy, it should return -EBUSY at pre_destroy(). wake_up
2674+
* is called when css_put() is called and refcnt goes down to 0.
2675+
*/
2676+
set_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
2677+
prepare_to_wait(&cgroup_rmdir_waitq, &wait, TASK_INTERRUPTIBLE);
2678+
2679+
if (!cgroup_clear_css_refs(cgrp)) {
2680+
mutex_unlock(&cgroup_mutex);
2681+
schedule();
2682+
finish_wait(&cgroup_rmdir_waitq, &wait);
2683+
clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
2684+
if (signal_pending(current))
2685+
return -EINTR;
2686+
goto again;
2687+
}
2688+
/* NO css_tryget() can success after here. */
2689+
finish_wait(&cgroup_rmdir_waitq, &wait);
2690+
clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
26402691

26412692
spin_lock(&release_list_lock);
26422693
set_bit(CGRP_REMOVED, &cgrp->flags);
@@ -3194,10 +3245,12 @@ void __css_put(struct cgroup_subsys_state *css)
31943245
{
31953246
struct cgroup *cgrp = css->cgroup;
31963247
rcu_read_lock();
3197-
if ((atomic_dec_return(&css->refcnt) == 1) &&
3198-
notify_on_release(cgrp)) {
3199-
set_bit(CGRP_RELEASABLE, &cgrp->flags);
3200-
check_for_release(cgrp);
3248+
if (atomic_dec_return(&css->refcnt) == 1) {
3249+
if (notify_on_release(cgrp)) {
3250+
set_bit(CGRP_RELEASABLE, &cgrp->flags);
3251+
check_for_release(cgrp);
3252+
}
3253+
cgroup_wakeup_rmdir_waiters(cgrp);
32013254
}
32023255
rcu_read_unlock();
32033256
}

‎mm/memcontrol.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2272,11 +2272,12 @@ mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
22722272
return ERR_PTR(-ENOMEM);
22732273
}
22742274

2275-
static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
2275+
static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
22762276
struct cgroup *cont)
22772277
{
22782278
struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2279-
mem_cgroup_force_empty(mem, false);
2279+
2280+
return mem_cgroup_force_empty(mem, false);
22802281
}
22812282

22822283
static void mem_cgroup_destroy(struct cgroup_subsys *ss,

0 commit comments

Comments
 (0)