Re: [RFC][PATCH 1/2] memcg: oom notifier
From: KAMEZAWA Hiroyuki
Date: Mon Mar 08 2010 - 03:37:27 EST
On Mon, 8 Mar 2010 10:32:59 +0200
"Kirill A. Shutemov" <kirill@xxxxxxxxxxxxx> wrote:
> On Mon, Mar 8, 2010 at 9:25 AM, KAMEZAWA Hiroyuki
> <kamezawa.hiroyu@xxxxxxxxxxxxxx> wrote:
> > From: KAMEZAWA Hiroyuki <kamezawa.hiroyu@xxxxxxxxxxxxxx>
> >
> > Considering containers or other resource management softwares in userland,
> > event notification of OOM in memcg should be implemented.
> > Now, memcg has "threshold" notifier which uses eventfd, we can make
> > use of it for oom notification.
> >
> > This patch adds oom notification eventfd callback for memcg. The usage
> > is very similar to threshold notifier, but control file is
> > memory.oom_control and no arguments other than eventfd is required.
> >
> > Â Â Â Â% cgroup_event_notifier /cgroup/A/memory.oom_control dummy
> > Â Â Â Â(About cgroup_event_notifier, see Documentation/cgroup/)
>
> Nice idea!
>
> But I don't think that sharing mem_cgroup_(un)register_event()
> with thresholds is a good idea. There are too many
> "if (type != _OOM_TYPE)". Probably, it's cleaner to create separate
> register/unregister for oom events, since oom event is quite different
> from threshold. We, also, don't need RCU for oom events. It's not
> a critical path.
>
Ah, okay. I'll write independent functions. I just wanted to reuse existing
good codes :)
Thanks,
-Kame
> > TODO:
> > Â- add a knob to disable oom-kill under a memcg.
> > Â- add read/write function to oom_control
> >
> > Changelog: 20100304
> > Â- renewed implemnation.
> >
> > Signed-off-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@xxxxxxxxxxxxxx>
> > ---
> > ÂDocumentation/cgroups/memory.txt | Â 20 ++++-
> > Âmm/memcontrol.c         Â| Â155 ++++++++++++++++++++++++++++-----------
> > Â2 files changed, 131 insertions(+), 44 deletions(-)
> >
> > Index: mmotm-2.6.33-Mar5/mm/memcontrol.c
> > ===================================================================
> > --- mmotm-2.6.33-Mar5.orig/mm/memcontrol.c
> > +++ mmotm-2.6.33-Mar5/mm/memcontrol.c
> > @@ -159,6 +159,7 @@ struct mem_cgroup_threshold_ary {
> > Â};
> >
> > Âstatic void mem_cgroup_threshold(struct mem_cgroup *mem);
> > +static void mem_cgroup_oom_notify(struct mem_cgroup *mem);
> >
> > Â/*
> > Â* The memory controller data structure. The memory controller controls both
> > @@ -220,6 +221,9 @@ struct mem_cgroup {
> > Â Â Â Â/* thresholds for mem+swap usage. RCU-protected */
> > Â Â Â Âstruct mem_cgroup_threshold_ary *memsw_thresholds;
> >
> > + Â Â Â /* For oom notifier event fd */
> > + Â Â Â struct mem_cgroup_threshold_ary *oom_notify;
> > +
> > Â Â Â Â/*
> > Â Â Â Â * Should we move charges of a task when a task is moved into this
> > Â Â Â Â * mem_cgroup ? And what type of charges should we move ?
> > @@ -282,9 +286,12 @@ enum charge_type {
> > Â/* for encoding cft->private value on file */
> > Â#define _MEM Â Â Â Â Â Â Â Â Â (0)
> > Â#define _MEMSWAP Â Â Â Â Â Â Â (1)
> > +#define _OOM_TYPE Â Â Â Â Â Â Â(2)
> > Â#define MEMFILE_PRIVATE(x, val) Â Â Â Â(((x) << 16) | (val))
> > Â#define MEMFILE_TYPE(val) Â Â Â(((val) >> 16) & 0xffff)
> > Â#define MEMFILE_ATTR(val) Â Â Â((val) & 0xffff)
> > +/* Used for OOM nofiier */
> > +#define OOM_CONTROL Â Â Â Â Â Â(0)
> >
> > Â/*
> > Â* Reclaim flags for mem_cgroup_hierarchical_reclaim
> > @@ -1313,9 +1320,10 @@ bool mem_cgroup_handle_oom(struct mem_cg
> > Â Â Â Â Â Â Â Âprepare_to_wait(&memcg_oom_waitq, &wait, TASK_KILLABLE);
> > Â Â Â Âmutex_unlock(&memcg_oom_mutex);
> >
> > - Â Â Â if (locked)
> > + Â Â Â if (locked) {
> > + Â Â Â Â Â Â Â mem_cgroup_oom_notify(mem);
> > Â Â Â Â Â Â Â Âmem_cgroup_out_of_memory(mem, mask);
> > - Â Â Â else {
> > + Â Â Â } else {
> > Â Â Â Â Â Â Â Âschedule();
> > Â Â Â Â Â Â Â Âfinish_wait(&memcg_oom_waitq, &wait);
> > Â Â Â Â}
> > @@ -3363,33 +3371,65 @@ static int compare_thresholds(const void
> > Â Â Â Âreturn _a->threshold - _b->threshold;
> > Â}
> >
> > +static int mem_cgroup_oom_notify_cb(struct mem_cgroup *mem, void *data)
> > +{
> > + Â Â Â struct mem_cgroup_threshold_ary *x;
> > + Â Â Â int i;
> > +
> > + Â Â Â rcu_read_lock();
> > + Â Â Â x = rcu_dereference(mem->oom_notify);
> > + Â Â Â for (i = 0; x && i < x->size; i++)
> > + Â Â Â Â Â Â Â eventfd_signal(x->entries[i].eventfd, 1);
> > + Â Â Â rcu_read_unlock();
> > + Â Â Â return 0;
> > +}
> > +
> > +static void mem_cgroup_oom_notify(struct mem_cgroup *mem)
> > +{
> > + Â Â Â mem_cgroup_walk_tree(mem, NULL, mem_cgroup_oom_notify_cb);
> > +}
> > +
> > Âstatic int mem_cgroup_register_event(struct cgroup *cgrp, struct cftype *cft,
> > Â Â Â Â Â Â Â Âstruct eventfd_ctx *eventfd, const char *args)
> > Â{
> > Â Â Â Âstruct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
> > Â Â Â Âstruct mem_cgroup_threshold_ary *thresholds, *thresholds_new;
> > Â Â Â Âint type = MEMFILE_TYPE(cft->private);
> > - Â Â Â u64 threshold, usage;
> > + Â Â Â u64 threshold;
> > + Â Â Â u64 usage = 0;
> > Â Â Â Âint size;
> > Â Â Â Âint i, ret;
> >
> > - Â Â Â ret = res_counter_memparse_write_strategy(args, &threshold);
> > - Â Â Â if (ret)
> > - Â Â Â Â Â Â Â return ret;
> > + Â Â Â if (type != _OOM_TYPE) {
> > + Â Â Â Â Â Â Â ret = res_counter_memparse_write_strategy(args, &threshold);
> > + Â Â Â Â Â Â Â if (ret)
> > + Â Â Â Â Â Â Â Â Â Â Â return ret;
> > + Â Â Â } else if (mem_cgroup_is_root(memcg)) /* root cgroup ? */
> > + Â Â Â Â Â Â Â return -ENOTSUPP;
> >
> > Â Â Â Âmutex_lock(&memcg->thresholds_lock);
> > - Â Â Â if (type == _MEM)
> > + Â Â Â /* For waiting OOM notify, "-1" is passed */
> > +
> > + Â Â Â switch (type) {
> > + Â Â Â case _MEM:
> > Â Â Â Â Â Â Â Âthresholds = memcg->thresholds;
> > - Â Â Â else if (type == _MEMSWAP)
> > + Â Â Â Â Â Â Â break;
> > + Â Â Â case _MEMSWAP:
> > Â Â Â Â Â Â Â Âthresholds = memcg->memsw_thresholds;
> > - Â Â Â else
> > + Â Â Â Â Â Â Â break;
> > + Â Â Â case _OOM_TYPE:
> > + Â Â Â Â Â Â Â thresholds = memcg->oom_notify;
> > + Â Â Â Â Â Â Â break;
> > + Â Â Â default:
> > Â Â Â Â Â Â Â ÂBUG();
> > + Â Â Â }
> >
> > - Â Â Â usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
> > -
> > - Â Â Â /* Check if a threshold crossed before adding a new one */
> > - Â Â Â if (thresholds)
> > - Â Â Â Â Â Â Â __mem_cgroup_threshold(memcg, type == _MEMSWAP);
> > + Â Â Â if (type != _OOM_TYPE) {
> > + Â Â Â Â Â Â Â usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
> > + Â Â Â Â Â Â Â /* Check if a threshold crossed before adding a new one */
> > + Â Â Â Â Â Â Â if (thresholds)
> > + Â Â Â Â Â Â Â Â Â Â Â __mem_cgroup_threshold(memcg, type == _MEMSWAP);
> > + Â Â Â }
> >
> > Â Â Â Âif (thresholds)
> > Â Â Â Â Â Â Â Âsize = thresholds->size + 1;
> > @@ -3416,27 +3456,34 @@ static int mem_cgroup_register_event(str
> > Â Â Â Âthresholds_new->entries[size - 1].threshold = threshold;
> >
> > Â Â Â Â/* Sort thresholds. Registering of new threshold isn't time-critical */
> > - Â Â Â sort(thresholds_new->entries, size,
> > + Â Â Â if (type != _OOM_TYPE) {
> > + Â Â Â Â Â Â Â sort(thresholds_new->entries, size,
> > Â Â Â Â Â Â Â Â Â Â Â Âsizeof(struct mem_cgroup_threshold),
> > Â Â Â Â Â Â Â Â Â Â Â Âcompare_thresholds, NULL);
> > -
> > - Â Â Â /* Find current threshold */
> > - Â Â Â atomic_set(&thresholds_new->current_threshold, -1);
> > - Â Â Â for (i = 0; i < size; i++) {
> > - Â Â Â Â Â Â Â if (thresholds_new->entries[i].threshold < usage) {
> > - Â Â Â Â Â Â Â Â Â Â Â /*
> > - Â Â Â Â Â Â Â Â Â Â Â Â* thresholds_new->current_threshold will not be used
> > - Â Â Â Â Â Â Â Â Â Â Â Â* until rcu_assign_pointer(), so it's safe to increment
> > - Â Â Â Â Â Â Â Â Â Â Â Â* it here.
> > - Â Â Â Â Â Â Â Â Â Â Â Â*/
> > - Â Â Â Â Â Â Â Â Â Â Â atomic_inc(&thresholds_new->current_threshold);
> > + Â Â Â Â Â Â Â /* Find current threshold */
> > + Â Â Â Â Â Â Â atomic_set(&thresholds_new->current_threshold, -1);
> > + Â Â Â Â Â Â Â for (i = 0; i < size; i++) {
> > + Â Â Â Â Â Â Â Â Â Â Â if (thresholds_new->entries[i].threshold < usage) {
> > + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â /*
> > + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â* thresholds_new->current_threshold will not
> > + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â* be used until rcu_assign_pointer(), so it's
> > + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â* safe to increment it here.
> > + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â*/
> > + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â atomic_inc(&thresholds_new->current_threshold);
> > + Â Â Â Â Â Â Â Â Â Â Â }
> > Â Â Â Â Â Â Â Â}
> > Â Â Â Â}
> > -
> > - Â Â Â if (type == _MEM)
> > + Â Â Â switch (type) {
> > + Â Â Â case _MEM:
> > Â Â Â Â Â Â Â Ârcu_assign_pointer(memcg->thresholds, thresholds_new);
> > - Â Â Â else
> > + Â Â Â Â Â Â Â break;
> > + Â Â Â case _MEMSWAP:
> > Â Â Â Â Â Â Â Ârcu_assign_pointer(memcg->memsw_thresholds, thresholds_new);
> > + Â Â Â Â Â Â Â break;
> > + Â Â Â case _OOM_TYPE:
> > + Â Â Â Â Â Â Â rcu_assign_pointer(memcg->oom_notify, thresholds_new);
> > + Â Â Â Â Â Â Â break;
> > + Â Â Â }
> >
> > Â Â Â Â/* To be sure that nobody uses thresholds before freeing it */
> > Â Â Â Âsynchronize_rcu();
> > @@ -3454,17 +3501,25 @@ static int mem_cgroup_unregister_event(s
> > Â Â Â Âstruct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
> > Â Â Â Âstruct mem_cgroup_threshold_ary *thresholds, *thresholds_new;
> > Â Â Â Âint type = MEMFILE_TYPE(cft->private);
> > - Â Â Â u64 usage;
> > + Â Â Â u64 usage = 0;
> > Â Â Â Âint size = 0;
> > Â Â Â Âint i, j, ret;
> >
> > Â Â Â Âmutex_lock(&memcg->thresholds_lock);
> > - Â Â Â if (type == _MEM)
> > + Â Â Â /* check eventfd is for OOM check or not */
> > + Â Â Â switch (type) {
> > + Â Â Â case _MEM:
> > Â Â Â Â Â Â Â Âthresholds = memcg->thresholds;
> > - Â Â Â else if (type == _MEMSWAP)
> > + Â Â Â Â Â Â Â break;
> > + Â Â Â case _MEMSWAP:
> > Â Â Â Â Â Â Â Âthresholds = memcg->memsw_thresholds;
> > - Â Â Â else
> > + Â Â Â Â Â Â Â break;
> > + Â Â Â case _OOM_TYPE:
> > + Â Â Â Â Â Â Â thresholds = memcg->oom_notify;
> > + Â Â Â Â Â Â Â break;
> > + Â Â Â default:
> > Â Â Â Â Â Â Â ÂBUG();
> > + Â Â Â }
> >
> > Â Â Â Â/*
> > Â Â Â Â * Something went wrong if we trying to unregister a threshold
> > @@ -3472,11 +3527,11 @@ static int mem_cgroup_unregister_event(s
> > Â Â Â Â */
> > Â Â Â ÂBUG_ON(!thresholds);
> >
> > - Â Â Â usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
> > -
> > - Â Â Â /* Check if a threshold crossed before removing */
> > - Â Â Â __mem_cgroup_threshold(memcg, type == _MEMSWAP);
> > -
> > + Â Â Â if (type != _OOM_TYPE) {
> > + Â Â Â Â Â Â Â usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
> > + Â Â Â Â Â Â Â /* Check if a threshold crossed before removing */
> > + Â Â Â Â Â Â Â __mem_cgroup_threshold(memcg, type == _MEMSWAP);
> > + Â Â Â }
> > Â Â Â Â/* Calculate new number of threshold */
> > Â Â Â Âfor (i = 0; i < thresholds->size; i++) {
> > Â Â Â Â Â Â Â Âif (thresholds->entries[i].eventfd != eventfd)
> > @@ -3500,13 +3555,15 @@ static int mem_cgroup_unregister_event(s
> > Â Â Â Âthresholds_new->size = size;
> >
> > Â Â Â Â/* Copy thresholds and find current threshold */
> > - Â Â Â atomic_set(&thresholds_new->current_threshold, -1);
> > + Â Â Â if (type != _OOM_TYPE)
> > + Â Â Â Â Â Â Â atomic_set(&thresholds_new->current_threshold, -1);
> > Â Â Â Âfor (i = 0, j = 0; i < thresholds->size; i++) {
> > Â Â Â Â Â Â Â Âif (thresholds->entries[i].eventfd == eventfd)
> > Â Â Â Â Â Â Â Â Â Â Â Âcontinue;
> >
> > Â Â Â Â Â Â Â Âthresholds_new->entries[j] = thresholds->entries[i];
> > - Â Â Â Â Â Â Â if (thresholds_new->entries[j].threshold < usage) {
> > + Â Â Â Â Â Â Â if (type != _OOM_TYPE &&
> > + Â Â Â Â Â Â Â Â Â Â Â thresholds_new->entries[j].threshold < usage) {
> > Â Â Â Â Â Â Â Â Â Â Â Â/*
> > Â Â Â Â Â Â Â Â Â Â Â Â * thresholds_new->current_threshold will not be used
> > Â Â Â Â Â Â Â Â Â Â Â Â * until rcu_assign_pointer(), so it's safe to increment
> > @@ -3518,11 +3575,17 @@ static int mem_cgroup_unregister_event(s
> > Â Â Â Â}
> >
> > Âassign:
> > - Â Â Â if (type == _MEM)
> > + Â Â Â switch (type) {
> > + Â Â Â case _MEM:
> > Â Â Â Â Â Â Â Ârcu_assign_pointer(memcg->thresholds, thresholds_new);
> > - Â Â Â else
> > + Â Â Â Â Â Â Â break;
> > + Â Â Â case _MEMSWAP:
> > Â Â Â Â Â Â Â Ârcu_assign_pointer(memcg->memsw_thresholds, thresholds_new);
> > -
> > + Â Â Â Â Â Â Â break;
> > + Â Â Â case _OOM_TYPE:
> > + Â Â Â Â Â Â Â rcu_assign_pointer(memcg->oom_notify, thresholds_new);
> > + Â Â Â Â Â Â Â break;
> > + Â Â Â }
> > Â Â Â Â/* To be sure that nobody uses thresholds before freeing it */
> > Â Â Â Âsynchronize_rcu();
> >
> > @@ -3588,6 +3651,12 @@ static struct cftype mem_cgroup_files[]
> > Â Â Â Â Â Â Â Â.read_u64 = mem_cgroup_move_charge_read,
> > Â Â Â Â Â Â Â Â.write_u64 = mem_cgroup_move_charge_write,
> > Â Â Â Â},
> > + Â Â Â {
> > + Â Â Â Â Â Â Â .name = "oom_control",
> > + Â Â Â Â Â Â Â .register_event = mem_cgroup_register_event,
> > + Â Â Â Â Â Â Â .unregister_event = mem_cgroup_unregister_event,
> > + Â Â Â Â Â Â Â .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
> > + Â Â Â },
> > Â};
> >
> > Â#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
> > Index: mmotm-2.6.33-Mar5/Documentation/cgroups/memory.txt
> > ===================================================================
> > --- mmotm-2.6.33-Mar5.orig/Documentation/cgroups/memory.txt
> > +++ mmotm-2.6.33-Mar5/Documentation/cgroups/memory.txt
> > @@ -184,6 +184,9 @@ limits on the root cgroup.
> >
> > ÂNote2: When panic_on_oom is set to "2", the whole system will panic.
> >
> > +When oom event notifier is registered, event will be delivered.
> > +(See oom_control section)
> > +
> > Â2. Locking
> >
> > ÂThe memory controller uses the following hierarchy
> > @@ -486,7 +489,22 @@ threshold in any direction.
> >
> > ÂIt's applicable for root and non-root cgroup.
> >
> > -10. TODO
> > +10. OOM Control
> > +
> > +Memory controler implements oom notifier using cgroup notification
> > +API (See cgroups.txt). It allows to register multiple oom notification
> > +delivery and gets notification when oom happens.
> > +
> > +To register a notifier, application need:
> > + - create an eventfd using eventfd(2)
> > + - open memory.oom_control file
> > + - write string like "<event_fd> <memory.oom_control>" to cgroup.event_control
> > +
> > +Application will be notifier through eventfd when oom happens.
> > +OOM notification doesn't work for root cgroup.
> > +
> > +
> > +11. TODO
> >
> > Â1. Add support for accounting huge pages (as a separate controller)
> > Â2. Make per-cgroup scanner reclaim not-shared pages first
> >
> > --
> > 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/