Commit c03914b7 authored by Roman Gushchin's avatar Roman Gushchin Committed by Linus Torvalds

mm: memcg/slab: postpone kmem_cache memcg pointer initialization to memcg_link_cache()

Patch series "mm: reparent slab memory on cgroup removal", v7.

# Why do we need this?

We've noticed that the number of dying cgroups is steadily growing on most
of our hosts in production.  The following investigation revealed an issue
in the userspace memory reclaim code [1], accounting of kernel stacks [2],
and also the main reason: slab objects.

The underlying problem is quite simple: any page charged to a cgroup holds
a reference to it, so the cgroup can't be reclaimed unless all charged
pages are gone.  If a slab object is actively used by other cgroups, it
won't be reclaimed, and will prevent the origin cgroup from being
reclaimed.

Slab objects, and first of all vfs cache, is shared between cgroups, which
are using the same underlying fs, and what's even more important, it's
shared between multiple generations of the same workload.  So if something
is running periodically every time in a new cgroup (like how systemd
works), we do accumulate multiple dying cgroups.

Strictly speaking pagecache isn't different here, but there is a key
difference: we disable protection and apply some extra pressure on LRUs of
dying cgroups, and these LRUs contain all charged pages.  My experiments
show that with the disabled kernel memory accounting the number of dying
cgroups stabilizes at a relatively small number (~100, depends on memory
pressure and cgroup creation rate), and with kernel memory accounting it
grows pretty steadily up to several thousands.

Memory cgroups are quite complex and big objects (mostly due to percpu
stats), so it leads to noticeable memory losses.  Memory occupied by dying
cgroups is measured in hundreds of megabytes.  I've even seen a host with
more than 100Gb of memory wasted for dying cgroups.  It leads to a
degradation of performance with the uptime, and generally limits the usage
of cgroups.

My previous attempt [3] to fix the problem by applying extra pressure on
slab shrinker lists caused a regressions with xfs and ext4, and has been
reverted [4].  The following attempts to find the right balance [5, 6]
were not successful.

So instead of trying to find a maybe non-existing balance, let's do
reparent accounted slab caches to the parent cgroup on cgroup removal.

# Implementation approach

There is however a significant problem with reparenting of slab memory:
there is no list of charged pages.  Some of them are in shrinker lists,
but not all.  Introducing of a new list is really not an option.

But fortunately there is a way forward: every slab page has a stable
pointer to the corresponding kmem_cache.  So the idea is to reparent
kmem_caches instead of slab pages.

It's actually simpler and cheaper, but requires some underlying changes:
1) Make kmem_caches to hold a single reference to the memory cgroup,
   instead of a separate reference per every slab page.
2) Stop setting page->mem_cgroup pointer for memcg slab pages and use
   page->kmem_cache->memcg indirection instead. It's used only on
   slab page release, so performance overhead shouldn't be a big issue.
3) Introduce a refcounter for non-root slab caches. It's required to
   be able to destroy kmem_caches when they become empty and release
   the associated memory cgroup.

There is a bonus: currently we release all memcg kmem_caches all together
with the memory cgroup itself.  This patchset allows individual
kmem_caches to be released as soon as they become inactive and free.

Some additional implementation details are provided in corresponding
commit messages.

# Results

Below is the average number of dying cgroups on two groups of our
production hosts.  They do run some sort of web frontend workload, the
memory pressure is moderate.  As we can see, with the kernel memory
reparenting the number stabilizes in 60s range; however with the original
version it grows almost linearly and doesn't show any signs of plateauing.
The difference in slab and percpu usage between patched and unpatched
versions also grows linearly.  In 7 days it exceeded 200Mb.

day           0    1    2    3    4    5    6    7
original     56  362  628  752 1070 1250 1490 1560
patched      23   46   51   55   60   57   67   69
mem diff(Mb) 22   74  123  152  164  182  214  241

# Links

[1]: commit 68600f62 ("mm: don't miss the last page because of round-off error")
[2]: commit 9b6f7e16 ("mm: rework memcg kernel stack accounting")
[3]: commit 172b06c3 ("mm: slowly shrink slabs with a relatively small number of objects")
[4]: commit a9a238e8 ("Revert "mm: slowly shrink slabs with a relatively small number of objects")
[5]: https://lkml.org/lkml/2019/1/28/1865
[6]: https://marc.info/?l=linux-mm&m=155064763626437&w=2

This patch (of 10):

Initialize kmem_cache->memcg_params.memcg pointer in memcg_link_cache()
rather than in init_memcg_params().

Once kmem_cache will hold a reference to the memory cgroup, it will
simplify the refcounting.

For non-root kmem_caches memcg_link_cache() is always called before the
kmem_cache becomes visible to a user, so it's safe.

Link: http://lkml.kernel.org/r/20190611231813.3148843-2-guro@fb.comSigned-off-by: default avatarRoman Gushchin <guro@fb.com>
Reviewed-by: default avatarShakeel Butt <shakeelb@google.com>
Acked-by: default avatarVladimir Davydov <vdavydov.dev@gmail.com>
Acked-by: default avatarJohannes Weiner <hannes@cmpxchg.org>
Cc: Waiman Long <longman@redhat.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Christoph Lameter <cl@linux.com>
Cc: Pekka Enberg <penberg@kernel.org>
Cc: David Rientjes <rientjes@google.com>
Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
Cc: Andrei Vagin <avagin@gmail.com>
Cc: Qian Cai <cai@lca.pw>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent c8713d0b
...@@ -1239,7 +1239,7 @@ void __init kmem_cache_init(void) ...@@ -1239,7 +1239,7 @@ void __init kmem_cache_init(void)
nr_node_ids * sizeof(struct kmem_cache_node *), nr_node_ids * sizeof(struct kmem_cache_node *),
SLAB_HWCACHE_ALIGN, 0, 0); SLAB_HWCACHE_ALIGN, 0, 0);
list_add(&kmem_cache->list, &slab_caches); list_add(&kmem_cache->list, &slab_caches);
memcg_link_cache(kmem_cache); memcg_link_cache(kmem_cache, NULL);
slab_state = PARTIAL; slab_state = PARTIAL;
/* /*
......
...@@ -289,7 +289,7 @@ static __always_inline void memcg_uncharge_slab(struct page *page, int order, ...@@ -289,7 +289,7 @@ static __always_inline void memcg_uncharge_slab(struct page *page, int order,
} }
extern void slab_init_memcg_params(struct kmem_cache *); extern void slab_init_memcg_params(struct kmem_cache *);
extern void memcg_link_cache(struct kmem_cache *s); extern void memcg_link_cache(struct kmem_cache *s, struct mem_cgroup *memcg);
extern void slab_deactivate_memcg_cache_rcu_sched(struct kmem_cache *s, extern void slab_deactivate_memcg_cache_rcu_sched(struct kmem_cache *s,
void (*deact_fn)(struct kmem_cache *)); void (*deact_fn)(struct kmem_cache *));
...@@ -344,7 +344,8 @@ static inline void slab_init_memcg_params(struct kmem_cache *s) ...@@ -344,7 +344,8 @@ static inline void slab_init_memcg_params(struct kmem_cache *s)
{ {
} }
static inline void memcg_link_cache(struct kmem_cache *s) static inline void memcg_link_cache(struct kmem_cache *s,
struct mem_cgroup *memcg)
{ {
} }
......
...@@ -140,13 +140,12 @@ void slab_init_memcg_params(struct kmem_cache *s) ...@@ -140,13 +140,12 @@ void slab_init_memcg_params(struct kmem_cache *s)
} }
static int init_memcg_params(struct kmem_cache *s, static int init_memcg_params(struct kmem_cache *s,
struct mem_cgroup *memcg, struct kmem_cache *root_cache) struct kmem_cache *root_cache)
{ {
struct memcg_cache_array *arr; struct memcg_cache_array *arr;
if (root_cache) { if (root_cache) {
s->memcg_params.root_cache = root_cache; s->memcg_params.root_cache = root_cache;
s->memcg_params.memcg = memcg;
INIT_LIST_HEAD(&s->memcg_params.children_node); INIT_LIST_HEAD(&s->memcg_params.children_node);
INIT_LIST_HEAD(&s->memcg_params.kmem_caches_node); INIT_LIST_HEAD(&s->memcg_params.kmem_caches_node);
return 0; return 0;
...@@ -221,11 +220,12 @@ int memcg_update_all_caches(int num_memcgs) ...@@ -221,11 +220,12 @@ int memcg_update_all_caches(int num_memcgs)
return ret; return ret;
} }
void memcg_link_cache(struct kmem_cache *s) void memcg_link_cache(struct kmem_cache *s, struct mem_cgroup *memcg)
{ {
if (is_root_cache(s)) { if (is_root_cache(s)) {
list_add(&s->root_caches_node, &slab_root_caches); list_add(&s->root_caches_node, &slab_root_caches);
} else { } else {
s->memcg_params.memcg = memcg;
list_add(&s->memcg_params.children_node, list_add(&s->memcg_params.children_node,
&s->memcg_params.root_cache->memcg_params.children); &s->memcg_params.root_cache->memcg_params.children);
list_add(&s->memcg_params.kmem_caches_node, list_add(&s->memcg_params.kmem_caches_node,
...@@ -244,7 +244,7 @@ static void memcg_unlink_cache(struct kmem_cache *s) ...@@ -244,7 +244,7 @@ static void memcg_unlink_cache(struct kmem_cache *s)
} }
#else #else
static inline int init_memcg_params(struct kmem_cache *s, static inline int init_memcg_params(struct kmem_cache *s,
struct mem_cgroup *memcg, struct kmem_cache *root_cache) struct kmem_cache *root_cache)
{ {
return 0; return 0;
} }
...@@ -384,7 +384,7 @@ static struct kmem_cache *create_cache(const char *name, ...@@ -384,7 +384,7 @@ static struct kmem_cache *create_cache(const char *name,
s->useroffset = useroffset; s->useroffset = useroffset;
s->usersize = usersize; s->usersize = usersize;
err = init_memcg_params(s, memcg, root_cache); err = init_memcg_params(s, root_cache);
if (err) if (err)
goto out_free_cache; goto out_free_cache;
...@@ -394,7 +394,7 @@ static struct kmem_cache *create_cache(const char *name, ...@@ -394,7 +394,7 @@ static struct kmem_cache *create_cache(const char *name,
s->refcount = 1; s->refcount = 1;
list_add(&s->list, &slab_caches); list_add(&s->list, &slab_caches);
memcg_link_cache(s); memcg_link_cache(s, memcg);
out: out:
if (err) if (err)
return ERR_PTR(err); return ERR_PTR(err);
...@@ -997,7 +997,7 @@ struct kmem_cache *__init create_kmalloc_cache(const char *name, ...@@ -997,7 +997,7 @@ struct kmem_cache *__init create_kmalloc_cache(const char *name,
create_boot_cache(s, name, size, flags, useroffset, usersize); create_boot_cache(s, name, size, flags, useroffset, usersize);
list_add(&s->list, &slab_caches); list_add(&s->list, &slab_caches);
memcg_link_cache(s); memcg_link_cache(s, NULL);
s->refcount = 1; s->refcount = 1;
return s; return s;
} }
......
...@@ -4199,7 +4199,7 @@ static struct kmem_cache * __init bootstrap(struct kmem_cache *static_cache) ...@@ -4199,7 +4199,7 @@ static struct kmem_cache * __init bootstrap(struct kmem_cache *static_cache)
} }
slab_init_memcg_params(s); slab_init_memcg_params(s);
list_add(&s->list, &slab_caches); list_add(&s->list, &slab_caches);
memcg_link_cache(s); memcg_link_cache(s, NULL);
return s; return s;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment