mm, memcg: consider subtrees in memory.events
authorChris Down <chris@chrisdown.name>
Sat, 1 Jun 2019 05:30:22 +0000 (22:30 -0700)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sun, 9 Jun 2019 07:16:13 +0000 (09:16 +0200)
commit 9852ae3fe5293264f01c49f2571ef7688f7823ce upstream.

memory.stat and other files already consider subtrees in their output, and
we should too in order to not present an inconsistent interface.

The current situation is fairly confusing, because people interacting with
cgroups expect hierarchical behaviour in the vein of memory.stat,
cgroup.events, and other files.  For example, this causes confusion when
debugging reclaim events under low, as currently these always read "0" at
non-leaf memcg nodes, which frequently causes people to misdiagnose breach
behaviour.  The same confusion applies to other counters in this file when
debugging issues.

Aggregation is done at write time instead of at read-time since these
counters aren't hot (unlike memory.stat which is per-page, so it does it
at read time), and it makes sense to bundle this with the file
notifications.

After this patch, events are propagated up the hierarchy:

    [root@ktst ~]# cat /sys/fs/cgroup/system.slice/memory.events
    low 0
    high 0
    max 0
    oom 0
    oom_kill 0
    [root@ktst ~]# systemd-run -p MemoryMax=1 true
    Running as unit: run-r251162a189fb4562b9dabfdc9b0422f5.service
    [root@ktst ~]# cat /sys/fs/cgroup/system.slice/memory.events
    low 0
    high 0
    max 7
    oom 1
    oom_kill 1

As this is a change in behaviour, this can be reverted to the old
behaviour by mounting with the `memory_localevents' flag set.  However, we
use the new behaviour by default as there's a lack of evidence that there
are any current users of memory.events that would find this change
undesirable.

akpm: this is a behaviour change, so Cc:stable.  THis is so that
forthcoming distros which use cgroup v2 are more likely to pick up the
revised behaviour.

Link: http://lkml.kernel.org/r/20190208224419.GA24772@chrisdown.name
Signed-off-by: Chris Down <chris@chrisdown.name>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Reviewed-by: Shakeel Butt <shakeelb@google.com>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Tejun Heo <tj@kernel.org>
Cc: Roman Gushchin <guro@fb.com>
Cc: Dennis Zhou <dennis@kernel.org>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Documentation/admin-guide/cgroup-v2.rst
include/linux/cgroup-defs.h
include/linux/memcontrol.h
kernel/cgroup/cgroup.c

index 20f92c16ffbf2c6e89ae090719d2e7225383985f..26b3a3e7daf6a7a0149050de5b11f7a78a53167f 100644 (file)
@@ -177,6 +177,15 @@ cgroup v2 currently supports the following mount options.
        ignored on non-init namespace mounts.  Please refer to the
        Delegation section for details.
 
+  memory_localevents
+
+        Only populate memory.events with data for the current cgroup,
+        and not any subtrees. This is legacy behaviour, the default
+        behaviour without this option is to include subtree counts.
+        This option is system wide and can only be set on mount or
+        modified through remount from the init namespace. The mount
+        option is ignored on non-init namespace mounts.
+
 
 Organizing Processes and Threads
 --------------------------------
index 7d57890cec671885d4bae9a15a59d240e9a36867..96398d8d4fd8de431ccdbd81ed9e0403e63a8e15 100644 (file)
@@ -83,6 +83,11 @@ enum {
         * Enable cpuset controller in v1 cgroup to use v2 behavior.
         */
        CGRP_ROOT_CPUSET_V2_MODE = (1 << 4),
+
+       /*
+        * Enable legacy local memory.events.
+        */
+       CGRP_ROOT_MEMORY_LOCAL_EVENTS = (1 << 5),
 };
 
 /* cftype->flags */
index dbb6118370c1e3c6df88b7da5695aea57dcd08ad..28525c7a8ab00dcb889cf4ee8f8691ed6e38a2b1 100644 (file)
@@ -777,8 +777,14 @@ static inline void count_memcg_event_mm(struct mm_struct *mm,
 static inline void memcg_memory_event(struct mem_cgroup *memcg,
                                      enum memcg_memory_event event)
 {
-       atomic_long_inc(&memcg->memory_events[event]);
-       cgroup_file_notify(&memcg->events_file);
+       do {
+               atomic_long_inc(&memcg->memory_events[event]);
+               cgroup_file_notify(&memcg->events_file);
+
+               if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS)
+                       break;
+       } while ((memcg = parent_mem_cgroup(memcg)) &&
+                !mem_cgroup_is_root(memcg));
 }
 
 static inline void memcg_memory_event_mm(struct mm_struct *mm,
index 9fcf6338ea5f99a083cd636d8c0a5d5f4b583dcf..4e2d82eea2cde875025aebd5abff2884594c5453 100644 (file)
@@ -1775,11 +1775,13 @@ int cgroup_show_path(struct seq_file *sf, struct kernfs_node *kf_node,
 
 enum cgroup2_param {
        Opt_nsdelegate,
+       Opt_memory_localevents,
        nr__cgroup2_params
 };
 
 static const struct fs_parameter_spec cgroup2_param_specs[] = {
-       fsparam_flag  ("nsdelegate",            Opt_nsdelegate),
+       fsparam_flag("nsdelegate",              Opt_nsdelegate),
+       fsparam_flag("memory_localevents",      Opt_memory_localevents),
        {}
 };
 
@@ -1802,6 +1804,9 @@ static int cgroup2_parse_param(struct fs_context *fc, struct fs_parameter *param
        case Opt_nsdelegate:
                ctx->flags |= CGRP_ROOT_NS_DELEGATE;
                return 0;
+       case Opt_memory_localevents:
+               ctx->flags |= CGRP_ROOT_MEMORY_LOCAL_EVENTS;
+               return 0;
        }
        return -EINVAL;
 }
@@ -1813,6 +1818,11 @@ static void apply_cgroup_root_flags(unsigned int root_flags)
                        cgrp_dfl_root.flags |= CGRP_ROOT_NS_DELEGATE;
                else
                        cgrp_dfl_root.flags &= ~CGRP_ROOT_NS_DELEGATE;
+
+               if (root_flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS)
+                       cgrp_dfl_root.flags |= CGRP_ROOT_MEMORY_LOCAL_EVENTS;
+               else
+                       cgrp_dfl_root.flags &= ~CGRP_ROOT_MEMORY_LOCAL_EVENTS;
        }
 }
 
@@ -1820,6 +1830,8 @@ static int cgroup_show_options(struct seq_file *seq, struct kernfs_root *kf_root
 {
        if (cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE)
                seq_puts(seq, ",nsdelegate");
+       if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS)
+               seq_puts(seq, ",memory_localevents");
        return 0;
 }
 
@@ -6122,7 +6134,7 @@ static struct kobj_attribute cgroup_delegate_attr = __ATTR_RO(delegate);
 static ssize_t features_show(struct kobject *kobj, struct kobj_attribute *attr,
                             char *buf)
 {
-       return snprintf(buf, PAGE_SIZE, "nsdelegate\n");
+       return snprintf(buf, PAGE_SIZE, "nsdelegate\nmemory_localevents\n");
 }
 static struct kobj_attribute cgroup_features_attr = __ATTR_RO(features);