Skip to content

Commit 051dd46

Browse files
Vladimir Davydovtorvalds
authored andcommitted
memcg, slab: unregister cache from memcg before starting to destroy it
Currently, memcg_unregister_cache(), which deletes the cache being destroyed from the memcg_slab_caches list, is called after __kmem_cache_shutdown() (see kmem_cache_destroy()), which starts to destroy the cache. As a result, one can access a partially destroyed cache while traversing a memcg_slab_caches list, which can have deadly consequences (for instance, cache_show() called for each cache on a memcg_slab_caches list from mem_cgroup_slabinfo_read() will dereference pointers to already freed data). To fix this, let's move memcg_unregister_cache() before the cache destruction process beginning, issuing memcg_register_cache() on failure. Signed-off-by: Vladimir Davydov <vdavydov@parallels.com> Cc: Michal Hocko <mhocko@suse.cz> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: David Rientjes <rientjes@google.com> Cc: Pekka Enberg <penberg@kernel.org> Cc: Glauber Costa <glommer@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 794b124 commit 051dd46

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

mm/memcontrol.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3140,6 +3140,7 @@ int memcg_alloc_cache_params(struct mem_cgroup *memcg, struct kmem_cache *s,
31403140
s->memcg_params->root_cache = root_cache;
31413141
INIT_WORK(&s->memcg_params->destroy,
31423142
kmem_cache_destroy_work_func);
3143+
css_get(&memcg->css);
31433144
} else
31443145
s->memcg_params->is_root_cache = true;
31453146

@@ -3148,6 +3149,10 @@ int memcg_alloc_cache_params(struct mem_cgroup *memcg, struct kmem_cache *s,
31483149

31493150
void memcg_free_cache_params(struct kmem_cache *s)
31503151
{
3152+
if (!s->memcg_params)
3153+
return;
3154+
if (!s->memcg_params->is_root_cache)
3155+
css_put(&s->memcg_params->memcg->css);
31513156
kfree(s->memcg_params);
31523157
}
31533158

@@ -3170,9 +3175,6 @@ void memcg_register_cache(struct kmem_cache *s)
31703175
memcg = s->memcg_params->memcg;
31713176
id = memcg_cache_id(memcg);
31723177

3173-
css_get(&memcg->css);
3174-
3175-
31763178
/*
31773179
* Since readers won't lock (see cache_from_memcg_idx()), we need a
31783180
* barrier here to ensure nobody will see the kmem_cache partially
@@ -3221,10 +3223,8 @@ void memcg_unregister_cache(struct kmem_cache *s)
32213223
* after removing it from the memcg_slab_caches list, otherwise we can
32223224
* fail to convert memcg_params_to_cache() while traversing the list.
32233225
*/
3224-
VM_BUG_ON(!root->memcg_params->memcg_caches[id]);
3226+
VM_BUG_ON(root->memcg_params->memcg_caches[id] != s);
32253227
root->memcg_params->memcg_caches[id] = NULL;
3226-
3227-
css_put(&memcg->css);
32283228
}
32293229

32303230
/*

mm/slab_common.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,9 @@ void kmem_cache_destroy(struct kmem_cache *s)
313313
s->refcount--;
314314
if (!s->refcount) {
315315
list_del(&s->list);
316+
memcg_unregister_cache(s);
316317

317318
if (!__kmem_cache_shutdown(s)) {
318-
memcg_unregister_cache(s);
319319
mutex_unlock(&slab_mutex);
320320
if (s->flags & SLAB_DESTROY_BY_RCU)
321321
rcu_barrier();
@@ -325,6 +325,7 @@ void kmem_cache_destroy(struct kmem_cache *s)
325325
kmem_cache_free(kmem_cache, s);
326326
} else {
327327
list_add(&s->list, &slab_caches);
328+
memcg_register_cache(s);
328329
mutex_unlock(&slab_mutex);
329330
printk(KERN_ERR "kmem_cache_destroy %s: Slab cache still has objects\n",
330331
s->name);

0 commit comments

Comments
 (0)