[RFC] memcg: fix default behaviour of non-overridden memcg.swappiness

From: Ivan Teterevkov
Date: Thu Mar 19 2020 - 13:39:23 EST


This patch tries to resolve uncertainty around the memcg.swappiness when
it's not overridden by the user: shall there be the latest vm_swappiness
or the value captured at the moment when the cgroup was created?

I'm sitting on the fence with regards to this patch because cgroup v1 is
considered legacy nowadays and the semantics of "swappiness" is already
overwhelmed. However, the patch might be considered as a "fix" because
looking at the documentation [1] one might have the impression that it's
the latest /proc/sys/vm/swappiness value that should be found in the
memcg.swappiness unless it's overridden or inherited from a cgroup where
it was overridden when the given cgroup was created.

Also, shall this magic -1 be exposed to the user? I think it's a "no",
but what if the user wants to un-override the memcg.swappiness...

What do you reckon?

[1] https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v1/memory.html#swappiness

-------------------------------- %< --------------------------------

This patch makes the memcg with the non-overridden swappiness
use the latest value found in /proc/sys/vm/swappiness instead
of one captured at the time when the memcg was created.

Signed-off-by: Ivan Teterevkov <ivan.teterevkov@xxxxxxxxxxx>
---
Documentation/admin-guide/cgroup-v1/memory.rst | 7 +++++--
include/linux/memcontrol.h | 15 +++++++++++++++
include/linux/swap.h | 4 ++++
mm/memcontrol.c | 4 +++-
4 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/Documentation/admin-guide/cgroup-v1/memory.rst b/Documentation/admin-guide/cgroup-v1/memory.rst
index 0ae4f564c2d6..ccb4046c0aa3 100644
--- a/Documentation/admin-guide/cgroup-v1/memory.rst
+++ b/Documentation/admin-guide/cgroup-v1/memory.rst
@@ -610,8 +610,11 @@ Note:
5.3 swappiness
--------------

-Overrides /proc/sys/vm/swappiness for the particular group. The tunable
-in the root cgroup corresponds to the global swappiness setting.
+Overrides /proc/sys/vm/swappiness for the particular cgroup. The overridden
+memory.swappiness in the non-root cgroup is inherited by new child cgroups.
+The tunable in the root cgroup corresponds to the global swappiness setting;
+changes made there are also applied to the non-overridden memory.swappiness
+of the non-root cgroups.

Please note that unlike during the global reclaim, limit reclaim
enforces that 0 swappiness really prevents from any swapping even if
diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
index a7a0a1a5c8d5..b5d69648be88 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -240,7 +240,22 @@ struct mem_cgroup {
bool oom_lock;
int under_oom;

+ /*
+ * Overrides the global vm_swappiness, unless there's a special case:
+ *
+ * - The swappiness in the root cgroup always corresponds to the global
+ * vm_swappiness and the value below is ignored.
+ *
+ * - The default value -1 means the cgroup uses the global
+ * vm_swappiness.
+ *
+ * - The value 0 prevents any swapping in the cgroup.
+ *
+ * Otherwise, any integer from 1 to 100 overrides the vm_swappiness
+ * and is inherited by new child cgroups.
+ */
int swappiness;
+
/* OOM-Killer disable */
int oom_kill_disable;

diff --git a/include/linux/swap.h b/include/linux/swap.h
index 1e99f7ac1d7e..d4c65ebcae61 100644
--- a/include/linux/swap.h
+++ b/include/linux/swap.h
@@ -636,6 +636,10 @@ static inline int mem_cgroup_swappiness(struct mem_cgroup *memcg)
if (mem_cgroup_disabled() || mem_cgroup_is_root(memcg))
return vm_swappiness;

+ /* Not overridden? */
+ if (memcg->swappiness == -1)
+ return vm_swappiness;
+
return memcg->swappiness;
}
#else
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 2058b8da18db..a95a7df46442 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -4980,8 +4980,10 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
memcg->high = PAGE_COUNTER_MAX;
memcg->soft_limit = PAGE_COUNTER_MAX;
if (parent) {
- memcg->swappiness = mem_cgroup_swappiness(parent);
+ memcg->swappiness = parent->swappiness;
memcg->oom_kill_disable = parent->oom_kill_disable;
+ } else {
+ memcg->swappiness = -1;
}
if (parent && parent->use_hierarchy) {
memcg->use_hierarchy = true;
--
2.25.0