Re: [RFC] [PATCH] memcg: cleanup memory thresholds

From: Kirill A. Shutemov
Date: Mon May 17 2010 - 09:24:33 EST


Any comments?

On Sun, May 9, 2010 at 3:11 AM, Kirill A. Shutemov <kirill@xxxxxxxxxxxxx> wrote:
> Introduce struct mem_cgroup_thresholds. It helps to reduce number of
> checks of thresholds type (memory or mem+swap).
>
> Signed-off-by: Kirill A. Shutemov <kirill@xxxxxxxxxxxxx>
> ---
> Âmm/memcontrol.c | Â151 ++++++++++++++++++++++++-------------------------------
> Â1 files changed, 66 insertions(+), 85 deletions(-)
>
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index a6d2a4c..a6c6268 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -158,6 +158,18 @@ struct mem_cgroup_threshold_ary {
> Â Â Â Â/* Array of thresholds */
> Â Â Â Âstruct mem_cgroup_threshold entries[0];
> Â};
> +
> +struct mem_cgroup_thresholds {
> + Â Â Â /* Primary thresholds array */
> + Â Â Â struct mem_cgroup_threshold_ary *primary;
> + Â Â Â /*
> + Â Â Â Â* Spare threshold array.
> + Â Â Â Â* It needed to make mem_cgroup_unregister_event() "never fail".
> + Â Â Â Â* It must be able to store at least primary->size - 1 entires.
> + Â Â Â Â*/
> + Â Â Â struct mem_cgroup_threshold_ary *spare;
> +};
> +
> Â/* for OOM */
> Âstruct mem_cgroup_eventfd_list {
> Â Â Â Âstruct list_head list;
> @@ -224,20 +236,10 @@ struct mem_cgroup {
> Â Â Â Âstruct mutex thresholds_lock;
>
> Â Â Â Â/* thresholds for memory usage. RCU-protected */
> - Â Â Â struct mem_cgroup_threshold_ary *thresholds;
> -
> - Â Â Â /*
> - Â Â Â Â* Preallocated buffer to be used in mem_cgroup_unregister_event()
> - Â Â Â Â* to make it "never fail".
> - Â Â Â Â* It must be able to store at least thresholds->size - 1 entries.
> - Â Â Â Â*/
> - Â Â Â struct mem_cgroup_threshold_ary *__thresholds;
> + Â Â Â struct mem_cgroup_thresholds thresholds;
>
> Â Â Â Â/* thresholds for mem+swap usage. RCU-protected */
> - Â Â Â struct mem_cgroup_threshold_ary *memsw_thresholds;
> -
> - Â Â Â /* the same as __thresholds, but for memsw_thresholds */
> - Â Â Â struct mem_cgroup_threshold_ary *__memsw_thresholds;
> + Â Â Â struct mem_cgroup_thresholds memsw_thresholds;
>
> Â Â Â Â/* For oom notifier event fd */
> Â Â Â Âstruct list_head oom_notify;
> @@ -3438,9 +3440,9 @@ static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
>
> Â Â Â Ârcu_read_lock();
> Â Â Â Âif (!swap)
> - Â Â Â Â Â Â Â t = rcu_dereference(memcg->thresholds);
> + Â Â Â Â Â Â Â t = rcu_dereference(memcg->thresholds.primary);
> Â Â Â Âelse
> - Â Â Â Â Â Â Â t = rcu_dereference(memcg->memsw_thresholds);
> + Â Â Â Â Â Â Â t = rcu_dereference(memcg->memsw_thresholds.primary);
>
> Â Â Â Âif (!t)
> Â Â Â Â Â Â Â Âgoto unlock;
> @@ -3514,91 +3516,78 @@ static int mem_cgroup_usage_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;
> + Â Â Â struct mem_cgroup_thresholds *thresholds;
> + Â Â Â struct mem_cgroup_threshold_ary *new;
> Â Â Â Âint type = MEMFILE_TYPE(cft->private);
> Â Â Â Âu64 threshold, usage;
> - Â Â Â int size;
> - Â Â Â int i, ret;
> + Â Â Â int i, size, ret;
>
> Â Â Â Âret = res_counter_memparse_write_strategy(args, &threshold);
> Â Â Â Âif (ret)
> Â Â Â Â Â Â Â Âreturn ret;
>
> Â Â Â Âmutex_lock(&memcg->thresholds_lock);
> +
> Â Â Â Âif (type == _MEM)
> - Â Â Â Â Â Â Â thresholds = memcg->thresholds;
> + Â Â Â Â Â Â Â thresholds = &memcg->thresholds;
> Â Â Â Âelse if (type == _MEMSWAP)
> - Â Â Â Â Â Â Â thresholds = memcg->memsw_thresholds;
> + Â Â Â Â Â Â Â thresholds = &memcg->memsw_thresholds;
> Â Â Â Âelse
> Â Â Â Â Â Â Â ÂBUG();
>
> Â Â Â Âusage = mem_cgroup_usage(memcg, type == _MEMSWAP);
>
> Â Â Â Â/* Check if a threshold crossed before adding a new one */
> - Â Â Â if (thresholds)
> + Â Â Â if (thresholds->primary)
> Â Â Â Â Â Â Â Â__mem_cgroup_threshold(memcg, type == _MEMSWAP);
>
> - Â Â Â if (thresholds)
> - Â Â Â Â Â Â Â size = thresholds->size + 1;
> - Â Â Â else
> - Â Â Â Â Â Â Â size = 1;
> + Â Â Â size = thresholds->primary ? thresholds->primary->size + 1 : 1;
>
> Â Â Â Â/* Allocate memory for new array of thresholds */
> - Â Â Â thresholds_new = kmalloc(sizeof(*thresholds_new) +
> - Â Â Â Â Â Â Â Â Â Â Â size * sizeof(struct mem_cgroup_threshold),
> + Â Â Â new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
> Â Â Â Â Â Â Â Â Â Â Â ÂGFP_KERNEL);
> - Â Â Â if (!thresholds_new) {
> + Â Â Â if (!new) {
> Â Â Â Â Â Â Â Âret = -ENOMEM;
> Â Â Â Â Â Â Â Âgoto unlock;
> Â Â Â Â}
> - Â Â Â thresholds_new->size = size;
> + Â Â Â new->size = size;
>
> Â Â Â Â/* Copy thresholds (if any) to new array */
> - Â Â Â if (thresholds)
> - Â Â Â Â Â Â Â memcpy(thresholds_new->entries, thresholds->entries,
> - Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â thresholds->size *
> + Â Â Â if (thresholds->primary) {
> + Â Â Â Â Â Â Â memcpy(new->entries, thresholds->primary->entries, (size - 1) *
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âsizeof(struct mem_cgroup_threshold));
> + Â Â Â }
> +
> Â Â Â Â/* Add new threshold */
> - Â Â Â thresholds_new->entries[size - 1].eventfd = eventfd;
> - Â Â Â thresholds_new->entries[size - 1].threshold = threshold;
> + Â Â Â new->entries[size - 1].eventfd = eventfd;
> + Â Â Â new->entries[size - 1].threshold = threshold;
>
> Â Â Â Â/* Sort thresholds. Registering of new threshold isn't time-critical */
> - Â Â Â sort(thresholds_new->entries, size,
> - Â Â Â Â Â Â Â Â Â Â Â sizeof(struct mem_cgroup_threshold),
> + Â Â Â sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
> Â Â Â Â Â Â Â Â Â Â Â Âcompare_thresholds, NULL);
>
> Â Â Â Â/* Find current threshold */
> - Â Â Â thresholds_new->current_threshold = -1;
> + Â Â Â new->current_threshold = -1;
> Â Â Â Âfor (i = 0; i < size; i++) {
> - Â Â Â Â Â Â Â if (thresholds_new->entries[i].threshold < usage) {
> + Â Â Â Â Â Â Â if (new->entries[i].threshold < usage) {
> Â Â Â Â Â Â Â Â Â Â Â Â/*
> - Â Â Â Â Â Â Â Â Â Â Â Â* thresholds_new->current_threshold will not be used
> - Â Â Â Â Â Â Â Â Â Â Â Â* until rcu_assign_pointer(), so it's safe to increment
> + Â Â Â Â Â Â Â Â Â Â Â Â* new->current_threshold will not be used until
> + Â Â Â Â Â Â Â Â Â Â Â Â* rcu_assign_pointer(), so it's safe to increment
> Â Â Â Â Â Â Â Â Â Â Â Â * it here.
> Â Â Â Â Â Â Â Â Â Â Â Â */
> - Â Â Â Â Â Â Â Â Â Â Â ++thresholds_new->current_threshold;
> + Â Â Â Â Â Â Â Â Â Â Â ++new->current_threshold;
> Â Â Â Â Â Â Â Â}
> Â Â Â Â}
>
> - Â Â Â if (type == _MEM)
> - Â Â Â Â Â Â Â rcu_assign_pointer(memcg->thresholds, thresholds_new);
> - Â Â Â else
> - Â Â Â Â Â Â Â rcu_assign_pointer(memcg->memsw_thresholds, thresholds_new);
> + Â Â Â /* Free old spare buffer and save old primary buffer as spare */
> + Â Â Â kfree(thresholds->spare);
> + Â Â Â thresholds->spare = thresholds->primary;
> +
> + Â Â Â rcu_assign_pointer(thresholds->primary, new);
>
> Â Â Â Â/* To be sure that nobody uses thresholds */
> Â Â Â Âsynchronize_rcu();
>
> - Â Â Â /*
> - Â Â Â Â* Free old preallocated buffer and use thresholds as new
> - Â Â Â Â* preallocated buffer.
> - Â Â Â Â*/
> - Â Â Â if (type == _MEM) {
> - Â Â Â Â Â Â Â kfree(memcg->__thresholds);
> - Â Â Â Â Â Â Â memcg->__thresholds = thresholds;
> - Â Â Â } else {
> - Â Â Â Â Â Â Â kfree(memcg->__memsw_thresholds);
> - Â Â Â Â Â Â Â memcg->__memsw_thresholds = thresholds;
> - Â Â Â }
> Âunlock:
> Â Â Â Âmutex_unlock(&memcg->thresholds_lock);
>
> @@ -3609,17 +3598,17 @@ static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
> Â Â Â Âstruct cftype *cft, struct eventfd_ctx *eventfd)
> Â{
> Â Â Â Âstruct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
> - Â Â Â struct mem_cgroup_threshold_ary *thresholds, *thresholds_new;
> + Â Â Â struct mem_cgroup_thresholds *thresholds;
> + Â Â Â struct mem_cgroup_threshold_ary *new;
> Â Â Â Âint type = MEMFILE_TYPE(cft->private);
> Â Â Â Âu64 usage;
> - Â Â Â int size = 0;
> - Â Â Â int i, j;
> + Â Â Â int i, j, size;
>
> Â Â Â Âmutex_lock(&memcg->thresholds_lock);
> Â Â Â Âif (type == _MEM)
> - Â Â Â Â Â Â Â thresholds = memcg->thresholds;
> + Â Â Â Â Â Â Â thresholds = &memcg->thresholds;
> Â Â Â Âelse if (type == _MEMSWAP)
> - Â Â Â Â Â Â Â thresholds = memcg->memsw_thresholds;
> + Â Â Â Â Â Â Â thresholds = &memcg->memsw_thresholds;
> Â Â Â Âelse
> Â Â Â Â Â Â Â ÂBUG();
>
> @@ -3635,53 +3624,45 @@ static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
> Â Â Â Â__mem_cgroup_threshold(memcg, type == _MEMSWAP);
>
> Â Â Â Â/* Calculate new number of threshold */
> - Â Â Â for (i = 0; i < thresholds->size; i++) {
> - Â Â Â Â Â Â Â if (thresholds->entries[i].eventfd != eventfd)
> + Â Â Â size = 0;
> + Â Â Â for (i = 0; i < thresholds->primary->size; i++) {
> + Â Â Â Â Â Â Â if (thresholds->primary->entries[i].eventfd != eventfd)
> Â Â Â Â Â Â Â Â Â Â Â Âsize++;
> Â Â Â Â}
>
> - Â Â Â /* Use preallocated buffer for new array of thresholds */
> - Â Â Â if (type == _MEM)
> - Â Â Â Â Â Â Â thresholds_new = memcg->__thresholds;
> - Â Â Â else
> - Â Â Â Â Â Â Â thresholds_new = memcg->__memsw_thresholds;
> + Â Â Â new = thresholds->spare;
>
> Â Â Â Â/* Set thresholds array to NULL if we don't have thresholds */
> Â Â Â Âif (!size) {
> - Â Â Â Â Â Â Â kfree(thresholds_new);
> - Â Â Â Â Â Â Â thresholds_new = NULL;
> + Â Â Â Â Â Â Â kfree(new);
> + Â Â Â Â Â Â Â new = NULL;
> Â Â Â Â Â Â Â Âgoto swap_buffers;
> Â Â Â Â}
>
> - Â Â Â thresholds_new->size = size;
> + Â Â Â new->size = size;
>
> Â Â Â Â/* Copy thresholds and find current threshold */
> - Â Â Â thresholds_new->current_threshold = -1;
> - Â Â Â for (i = 0, j = 0; i < thresholds->size; i++) {
> - Â Â Â Â Â Â Â if (thresholds->entries[i].eventfd == eventfd)
> + Â Â Â new->current_threshold = -1;
> + Â Â Â for (i = 0, j = 0; i < thresholds->primary->size; i++) {
> + Â Â Â Â Â Â Â if (thresholds->primary->entries[i].eventfd == eventfd)
> Â Â Â Â Â Â Â Â Â Â Â Âcontinue;
>
> - Â Â Â Â Â Â Â thresholds_new->entries[j] = thresholds->entries[i];
> - Â Â Â Â Â Â Â if (thresholds_new->entries[j].threshold < usage) {
> + Â Â Â Â Â Â Â new->entries[j] = thresholds->primary->entries[i];
> + Â Â Â Â Â Â Â if (new->entries[j].threshold < usage) {
> Â Â Â Â Â Â Â Â Â Â Â Â/*
> - Â Â Â Â Â Â Â Â Â Â Â Â* thresholds_new->current_threshold will not be used
> + Â Â Â Â Â Â Â Â Â Â Â Â* new->current_threshold will not be used
> Â Â Â Â Â Â Â Â Â Â Â Â * until rcu_assign_pointer(), so it's safe to increment
> Â Â Â Â Â Â Â Â Â Â Â Â * it here.
> Â Â Â Â Â Â Â Â Â Â Â Â */
> - Â Â Â Â Â Â Â Â Â Â Â ++thresholds_new->current_threshold;
> + Â Â Â Â Â Â Â Â Â Â Â ++new->current_threshold;
> Â Â Â Â Â Â Â Â}
> Â Â Â Â Â Â Â Âj++;
> Â Â Â Â}
>
> Âswap_buffers:
> - Â Â Â /* Swap thresholds array and preallocated buffer */
> - Â Â Â if (type == _MEM) {
> - Â Â Â Â Â Â Â memcg->__thresholds = thresholds;
> - Â Â Â Â Â Â Â rcu_assign_pointer(memcg->thresholds, thresholds_new);
> - Â Â Â } else {
> - Â Â Â Â Â Â Â memcg->__memsw_thresholds = thresholds;
> - Â Â Â Â Â Â Â rcu_assign_pointer(memcg->memsw_thresholds, thresholds_new);
> - Â Â Â }
> + Â Â Â /* Swap primary and spare array */
> + Â Â Â thresholds->spare = thresholds->primary;
> + Â Â Â rcu_assign_pointer(thresholds->primary, new);
>
> Â Â Â Â/* To be sure that nobody uses thresholds */
> Â Â Â Âsynchronize_rcu();
> --
> 1.7.0.4
>
>
N‹§²æìr¸›yúèšØb²X¬¶ÇvØ^–)Þ{.nÇ+‰·¥Š{±‘êçzX§¶›¡Ü}©ž²ÆzÚ&j:+v‰¨¾«‘êçzZ+€Ê+zf£¢·hšˆ§~†­†Ûiÿûàz¹®w¥¢¸?™¨è­Ú&¢)ßf”ù^jÇy§m…á@A«a¶Úÿ 0¶ìh®å’i