diff --git a/include/net/sock.h b/include/net/sock.h
index 28bdf874da4a..bed8ce0ac48b 100644
--- a/include/net/sock.h
+++ b/include/net/sock.h
@@ -71,7 +71,7 @@
struct cgroup;
struct cgroup_subsys;
-#ifdef CONFIG_NET
+#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
int mem_cgroup_sockets_init(struct mem_cgroup *memcg, struct cgroup_subsys *ss);
void mem_cgroup_sockets_destroy(struct mem_cgroup *memcg);
#else
diff --git a/init/Kconfig b/init/Kconfig
index 826aacc7399b..9989601f7d6c 100644
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -1037,6 +1037,19 @@ config MEMCG_KMEM
are plenty of kmem available for reclaim. That makes this option
unusable in real life so DO NOT SELECT IT unless for development
purposes.
+config MEMCG_KMEM_ENABLED
+ bool "Memory Resource Controller Kernel Memory accounting enabled by default"
+ depends on MEMCG_KMEM
+ default n
+ help
+ Memory Resource Controller Kernel Memory accounting comes with its
+ price in a bigger memory consumption and runtime overhead. General
+ purpose distribution kernels which want to enable the feature but
+ keep it disabled by default and let the user enable it by
+ kmemaccount=1 boot command line parameter should have this option
+ unselected. For those who want to have the feature enabled by default
+ should select this option (if, for some reason, they need to disable
+ it then kmemaccount=0 does the trick).
config CGROUP_HUGETLB
bool "HugeTLB Resource Controller for Control Groups"
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index f1ab93daa1b7..c3a18a5fe86c 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -334,7 +334,6 @@ struct mem_cgroup {
#if defined(CONFIG_MEMCG_KMEM)
/* Index in the kmem_cache->memcg_params.memcg_caches array */
int kmemcg_id;
- bool kmem_acct_activated;
bool kmem_acct_active;
#endif
@@ -3282,92 +3281,13 @@ static u64 mem_cgroup_read_u64(struct cgroup_subsys_state *css,
}
#ifdef CONFIG_MEMCG_KMEM
-static int memcg_activate_kmem(struct mem_cgroup *memcg,
- unsigned long nr_pages)
-{
- int err = 0;
- int memcg_id;
-
- BUG_ON(memcg->kmemcg_id >= 0);
- BUG_ON(memcg->kmem_acct_activated);
- BUG_ON(memcg->kmem_acct_active);
-
- /*
- * For simplicity, we won't allow this to be disabled. It also can't
- * be changed if the cgroup has children already, or if tasks had
- * already joined.
- *
- * If tasks join before we set the limit, a person looking at
- * kmem.usage_in_bytes will have no way to determine when it took
- * place, which makes the value quite meaningless.
- *
- * After it first became limited, changes in the value of the limit are
- * of course permitted.
- */
- mutex_lock(&memcg_create_mutex);
- if (cgroup_has_tasks(memcg->css.cgroup) ||
- (memcg->use_hierarchy && memcg_has_children(memcg)))
- err = -EBUSY;
- mutex_unlock(&memcg_create_mutex);
- if (err)
- goto out;
-
- memcg_id = memcg_alloc_cache_id();
- if (memcg_id < 0) {
- err = memcg_id;
- goto out;
- }
-
- /*
- * We couldn't have accounted to this cgroup, because it hasn't got
- * activated yet, so this should succeed.
- */
- err = page_counter_limit(&memcg->kmem, nr_pages);
- VM_BUG_ON(err);
-
- static_key_slow_inc(&memcg_kmem_enabled_key);
- /*
- * A memory cgroup is considered kmem-active as soon as it gets
- * kmemcg_id. Setting the id after enabling static branching will
- * guarantee no one starts accounting before all call sites are
- * patched.
- */
- memcg->kmemcg_id = memcg_id;
- memcg->kmem_acct_activated = true;
- memcg->kmem_acct_active = true;
-out:
- return err;
-}
-
static int memcg_update_kmem_limit(struct mem_cgroup *memcg,
unsigned long limit)
{
int ret;
mutex_lock(&memcg_limit_mutex);
- if (!memcg_kmem_is_active(memcg))
- ret = memcg_activate_kmem(memcg, limit);
- else
- ret = page_counter_limit(&memcg->kmem, limit);
- mutex_unlock(&memcg_limit_mutex);
- return ret;
-}
-
-static int memcg_propagate_kmem(struct mem_cgroup *memcg)
-{
- int ret = 0;
- struct mem_cgroup *parent = parent_mem_cgroup(memcg);
-
- if (!parent)
- return 0;
-
- mutex_lock(&memcg_limit_mutex);
- /*
- * If the parent cgroup is not kmem-active now, it cannot be activated
- * after this point, because it has at least one child already.
- */
- if (memcg_kmem_is_active(parent))
- ret = memcg_activate_kmem(memcg, PAGE_COUNTER_MAX);
+ ret = page_counter_limit(&memcg->kmem, limit);
mutex_unlock(&memcg_limit_mutex);
return ret;
}@@ -4003,15 +3923,34 @@ static int mem_cgroup_oom_control_write(struct cgroup_subsys_state *css,
}
#ifdef CONFIG_MEMCG_KMEM
-static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
+static int do_kmem_account = IS_ENABLED(CONFIG_MEMCG_KMEM_ENABLED);
+
+static int __init enable_kmem_account(char *s)
{
- int ret;
+ if (!strcmp(s, "1"))
+ do_kmem_account = 1;
+ else if (!strcmp(s, "0"))
+ do_kmem_account = 0;
+ return 1;
+}
+__setup("kmemaccount=", enable_kmem_account);
- ret = memcg_propagate_kmem(memcg);
- if (ret)
- return ret;
+static int memcg_init_kmem(struct mem_cgroup *memcg)
+{
+ int idx;
+
+ if (!do_kmem_account || mem_cgroup_is_root(memcg))
+ return 0;
+
+ idx = memcg_alloc_cache_id();
+ if (idx < 0)
+ return idx;
- return mem_cgroup_sockets_init(memcg, ss);
+ memcg->kmemcg_id = idx;
+ memcg->kmem_acct_active = true;
+
+ static_key_slow_inc(&memcg_kmem_enabled_key);
+ return 0;
}
static void memcg_deactivate_kmem(struct mem_cgroup *memcg)@@ -4062,12 +4001,12 @@ static void memcg_deactivate_kmem(struct mem_cgroup *memcg)
static void memcg_destroy_kmem(struct mem_cgroup *memcg)
{
- if (memcg->kmem_acct_activated) {
- memcg_destroy_kmem_caches(memcg);
- static_key_slow_dec(&memcg_kmem_enabled_key);
- WARN_ON(page_counter_read(&memcg->kmem));
- }
- mem_cgroup_sockets_destroy(memcg);
+ if (!do_kmem_account || mem_cgroup_is_root(memcg))
+ return;
+
+ memcg_destroy_kmem_caches(memcg);
+ static_key_slow_dec(&memcg_kmem_enabled_key);
+ WARN_ON(page_counter_read(&memcg->kmem));
}
#else
static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)@@ -4381,7 +4320,11 @@ static struct cftype mem_cgroup_legacy_files[] = {
.seq_show = memcg_numa_stat_show,
},
#endif
+ { }, /* terminate */
+};
+
#ifdef CONFIG_MEMCG_KMEM
+static struct cftype mem_cgroup_kmem_files[] = {
{
.name = "kmem.limit_in_bytes",
.private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),@@ -4414,10 +4357,19 @@ static struct cftype mem_cgroup_legacy_files[] = {
.seq_show = memcg_slab_show,
},
#endif
-#endif
{ }, /* terminate */
};
+static int __init mem_cgroup_kmem_init(void)
+{
+ if (!mem_cgroup_disabled() && do_kmem_account)
+ WARN_ON(cgroup_add_legacy_cftypes(&memory_cgrp_subsys,
+ mem_cgroup_kmem_files));
+ return 0;
+}
+subsys_initcall(mem_cgroup_kmem_init);
+#endif /* CONFIG_MEMCG_KMEM */
+
static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
{
struct mem_cgroup_per_node *pn;@@ -4601,10 +4553,16 @@ mem_cgroup_css_online(struct cgroup_subsys_state *css)
}
mutex_unlock(&memcg_create_mutex);
- ret = memcg_init_kmem(memcg, &memory_cgrp_subsys);
+ ret = mem_cgroup_sockets_init(memcg, &memory_cgrp_subsys);
if (ret)
return ret;
+ ret = memcg_init_kmem(memcg);
+ if (ret) {
+ mem_cgroup_sockets_destroy(memcg);
+ return ret;
+ }
+
/*
* Make sure the memcg is initialized: mem_cgroup_iter()
* orders reading memcg->initialized against its callers@@ -4642,6 +4600,7 @@ static void mem_cgroup_css_free(struct cgroup_subsys_state *css)
struct mem_cgroup *memcg = mem_cgroup_from_css(css);
memcg_destroy_kmem(memcg);
+ mem_cgroup_sockets_destroy(memcg);
__mem_cgroup_free(memcg);
}
--
1.7.10.4