Re: [PATCH] memcg: cgroup fix rmdir hang

From: KAMEZAWA Hiroyuki
Date: Sun Jun 28 2009 - 19:58:03 EST


On Mon, 29 Jun 2009 08:50:26 +0900
KAMEZAWA Hiroyuki <kamezawa.hiroyu@xxxxxxxxxxxxxx> wrote:

> > Maybe we should name this wakeup_rmdir_waiter() to emphasise that fact
> > that there will only be one waiter (the thread doing the rmdir).
> >
> Hm, but, there is no guarantee that there will "an" waiter.
>
Ignore above comment....I forget inode->mutex is held.

Thanks,
-Kame

>
> > > - Â Â Â if (unlikely(test_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags)))
> > > - Â Â Â Â Â Â Â wake_up_all(&cgroup_rmdir_waitq);
> > > + Â Â Â wake_up_all(&cgroup_rmdir_waitq);
> > > Â}
> > >
> > > Âstatic int rebind_subsystems(struct cgroupfs_root *root,
> > > @@ -2696,33 +2695,40 @@ again:
> > > Â Â Â Âmutex_unlock(&cgroup_mutex);
> > >
> > > Â Â Â Â/*
> > > + Â Â Â Â* css_put/get is provided for subsys to grab refcnt to css. In typical
> > > + Â Â Â Â* case, subsystem has no reference after pre_destroy(). But, under
> > > + Â Â Â Â* hierarchy management, some *temporal* refcnt can be hold.
> >
> > This sentence needs improvement/clarification. (Yes, I know it's just
> > copied from later in the file, but it wasn't clear there either :-) )
> >
> ok, I'll modify here. How about this ?
> ==
> In general, subsystem has no css->refcnt after pre_destroy(). But in racy cases,
> subsystem may have to get css->refcnt after pre_destroy() and it makes rmdir
> return with -EBUSY. But we don't like frequent -EBUSY. To avoid that, we use
> waitqueue for cgroup's rmdir. CGROUP_WAIT_ON_RMDIR bit is for synchronizing
> rmdir waitqueue and subsystem behavior. Please see css_get()/put()/tryget()
> implementation, it allows subsystem to avoid unnecessary failure of rmdir().
> If css_put()/get()/tryget() is not enough, cgroup_wakeup_rmdir_waiter() can be
> used.
> ==
>
>
> >
> > > + Â Â Â Â* To avoid returning -EBUSY to a user, waitqueue is used. If subsys
> > > + Â Â Â Â* is really busy, it should return -EBUSY at pre_destroy(). wake_up
> > > + Â Â Â Â* is called when css_put() is called and refcnt goes down to 0.
> > > + Â Â Â Â* And this WAIT_ON_RMDIR flag is cleared when subsys detect a race
> > > + Â Â Â Â* condition under pre_destroy()->rmdir.
> >
> > What exactly do you mean by pre_destroy()->rmdir ?
> >
> Ah, between pre_destroy() and "cgroup is removed" state. I'll remove this.
> Maybe above comment is enough.
>
>
> > > If flag is cleared, we need
> > > + Â Â Â Â* to call pre_destroy(), again.
> > > + Â Â Â Â*/
> > > + Â Â Â set_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
> > > +
> > > + Â Â Â /*
> > > Â Â Â Â * Call pre_destroy handlers of subsys. Notify subsystems
> > > Â Â Â Â * that rmdir() request comes.
> > > Â Â Â Â */
> > > Â Â Â Âret = cgroup_call_pre_destroy(cgrp);
> > > - Â Â Â if (ret)
> > > + Â Â Â if (ret) {
> > > + Â Â Â Â Â Â Â clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
> > > Â Â Â Â Â Â Â Âreturn ret;
> > > + Â Â Â }
> > >
> > > Â Â Â Âmutex_lock(&cgroup_mutex);
> > > Â Â Â Âparent = cgrp->parent;
> > > Â Â Â Âif (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) {
> > > + Â Â Â Â Â Â Â clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
> > > Â Â Â Â Â Â Â Âmutex_unlock(&cgroup_mutex);
> > > Â Â Â Â Â Â Â Âreturn -EBUSY;
> > > Â Â Â Â}
> > > - Â Â Â /*
> > > - Â Â Â Â* css_put/get is provided for subsys to grab refcnt to css. In typical
> > > - Â Â Â Â* case, subsystem has no reference after pre_destroy(). But, under
> > > - Â Â Â Â* hierarchy management, some *temporal* refcnt can be hold.
> > > - Â Â Â Â* To avoid returning -EBUSY to a user, waitqueue is used. If subsys
> > > - Â Â Â Â* is really busy, it should return -EBUSY at pre_destroy(). wake_up
> > > - Â Â Â Â* is called when css_put() is called and refcnt goes down to 0.
> > > - Â Â Â Â*/
> > > - Â Â Â set_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
> > > Â Â Â Âprepare_to_wait(&cgroup_rmdir_waitq, &wait, TASK_INTERRUPTIBLE);
> > > -
> > > Â Â Â Âif (!cgroup_clear_css_refs(cgrp)) {
> > > Â Â Â Â Â Â Â Âmutex_unlock(&cgroup_mutex);
> > > - Â Â Â Â Â Â Â schedule();
> > > + Â Â Â Â Â Â Â if (test_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags))
> > > + Â Â Â Â Â Â Â Â Â Â Â schedule();
> >
> > The need for this test_bit() should be commented, I think - at first
> > sight it doesn't seem necessary since if we've already been woken up,
> > then the schedule() is logically a no-op anyway. We only need it
> > because we are worried about the case where someone calls
> > wakeup_rmdir_waiters() prior to the prepare_to_wait()
> >
> >
> yes. will add comments.
>
>
> > > ===================================================================
> > > --- mmotm-2.6.31-Jun25.orig/mm/memcontrol.c
> > > +++ mmotm-2.6.31-Jun25/mm/memcontrol.c
> > > @@ -1234,6 +1234,12 @@ static int mem_cgroup_move_account(struc
> > > Â Â Â Âret = 0;
> > > Âout:
> > > Â Â Â Âunlock_page_cgroup(pc);
> > > + Â Â Â /*
> > > + Â Â Â Â* We charges against "to" which may not have any tasks. Then, "to"
> > > + Â Â Â Â* can be under rmdir(). But in current implementation, caller of
> > > + Â Â Â Â* this function is just force_empty() and it's garanteed that
> > > + Â Â Â Â* "to" is never removed. So, we don't check rmdir status here.
> > > + Â Â Â Â*/
> > > Â Â Â Âreturn ret;
> > > Â}
> > >
> > > @@ -1455,6 +1461,7 @@ __mem_cgroup_commit_charge_swapin(struct
> > > Â Â Â Â Â Â Â Âreturn;
> > > Â Â Â Âif (!ptr)
> > > Â Â Â Â Â Â Â Âreturn;
> > > + Â Â Â css_get(&ptr->css);
> > > Â Â Â Âpc = lookup_page_cgroup(page);
> > > Â Â Â Âmem_cgroup_lru_del_before_commit_swapcache(page);
> > > Â Â Â Â__mem_cgroup_commit_charge(ptr, pc, ctype);
> > > @@ -1484,7 +1491,13 @@ __mem_cgroup_commit_charge_swapin(struct
> > > Â Â Â Â Â Â Â Â}
> > > Â Â Â Â Â Â Â Ârcu_read_unlock();
> > > Â Â Â Â}
> > > - Â Â Â /* add this page(page_cgroup) to the LRU we want. */
> > > + Â Â Â /*
> > > + Â Â Â Â* At swapin, we may charge account against cgroup which has no tasks.
> > > + Â Â Â Â* So, rmdir()->pre_destroy() can be called while we do this charge.
> > > + Â Â Â Â* In that case, we need to call pre_destroy() again. check it here.
> > > + Â Â Â Â*/
> > > + Â Â Â cgroup_wakeup_rmdir_waiters(ptr->css.cgroup);
> > > + Â Â Â css_put(&ptr->css);
> > >
> > > Â}
> > >
> > > @@ -1691,7 +1704,7 @@ void mem_cgroup_end_migration(struct mem
> > >
> > > Â Â Â Âif (!mem)
> > > Â Â Â Â Â Â Â Âreturn;
> > > -
> > > + Â Â Â css_get(&mem->css);
> > > Â Â Â Â/* at migration success, oldpage->mapping is NULL. */
> > > Â Â Â Âif (oldpage->mapping) {
> > > Â Â Â Â Â Â Â Âtarget = oldpage;
> > > @@ -1731,6 +1744,14 @@ void mem_cgroup_end_migration(struct mem
> > > Â Â Â Â */
> > > Â Â Â Âif (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
> > > Â Â Â Â Â Â Â Âmem_cgroup_uncharge_page(target);
> > > + Â Â Â /*
> > > + Â Â Â Â* At migration, we may charge account against cgroup which has no tasks
> > > + Â Â Â Â* So, rmdir()->pre_destroy() can be called while we do this charge.
> > > + Â Â Â Â* In that case, we need to call pre_destroy() again. check it here.
> > > + Â Â Â Â*/
> > > + Â Â Â cgroup_wakeup_rmdir_waiters(mem->css.cgroup);
> > > + Â Â Â css_put(&mem->css);
> >
> > Having to do an extra get/put purely in order to seems unfortunate -
> > is that purely to force the cgroup_clear_css_refs() to fail?
> >
> yes.
>
> > Maybe we could wrap the get and the wakeup/put inside functions named
> > "cgroup_exclude_rmdir()" "cgroup_release_rmdir()" so that the mm
> > cgroup code is more self-explanatory.
> >
> Ah, it sounds nice idea. I'll prepare that as [2/2] patch.
>
> Thanks,
> -Kame
>
>
> > Paul
> > --
> > To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> > the body of a message to majordomo@xxxxxxxxxxxxxxx
> > More majordomo info at http://vger.kernel.org/majordomo-info.html
> > Please read the FAQ at http://www.tux.org/lkml/
> >
>
> --
> To unsubscribe, send a message with 'unsubscribe linux-mm' in
> the body to majordomo@xxxxxxxxxx For more info on Linux MM,
> see: http://www.linux-mm.org/ .
> Don't email: <a href=mailto:"dont@xxxxxxxxx";> email@xxxxxxxxx </a>
>

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/