diff options
author | Vladimir Davydov <vdavydov@parallels.com> | 2014-04-07 18:39:27 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2014-04-07 19:36:12 -0400 |
commit | 051dd46050f2a9bdfff8cc067f8987069eae1743 (patch) | |
tree | 64ccd763fcdda9a1087063f154f0d96fc6ab573a /mm | |
parent | 794b1248be4e7e157f5535c3ee49168aa4643349 (diff) |
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>
Diffstat (limited to 'mm')
-rw-r--r-- | mm/memcontrol.c | 12 | ||||
-rw-r--r-- | mm/slab_common.c | 3 |
2 files changed, 8 insertions, 7 deletions
diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 451523c3bd4e..c22d8bf42d9a 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c | |||
@@ -3140,6 +3140,7 @@ int memcg_alloc_cache_params(struct mem_cgroup *memcg, struct kmem_cache *s, | |||
3140 | s->memcg_params->root_cache = root_cache; | 3140 | s->memcg_params->root_cache = root_cache; |
3141 | INIT_WORK(&s->memcg_params->destroy, | 3141 | INIT_WORK(&s->memcg_params->destroy, |
3142 | kmem_cache_destroy_work_func); | 3142 | kmem_cache_destroy_work_func); |
3143 | css_get(&memcg->css); | ||
3143 | } else | 3144 | } else |
3144 | s->memcg_params->is_root_cache = true; | 3145 | s->memcg_params->is_root_cache = true; |
3145 | 3146 | ||
@@ -3148,6 +3149,10 @@ int memcg_alloc_cache_params(struct mem_cgroup *memcg, struct kmem_cache *s, | |||
3148 | 3149 | ||
3149 | void memcg_free_cache_params(struct kmem_cache *s) | 3150 | void memcg_free_cache_params(struct kmem_cache *s) |
3150 | { | 3151 | { |
3152 | if (!s->memcg_params) | ||
3153 | return; | ||
3154 | if (!s->memcg_params->is_root_cache) | ||
3155 | css_put(&s->memcg_params->memcg->css); | ||
3151 | kfree(s->memcg_params); | 3156 | kfree(s->memcg_params); |
3152 | } | 3157 | } |
3153 | 3158 | ||
@@ -3170,9 +3175,6 @@ void memcg_register_cache(struct kmem_cache *s) | |||
3170 | memcg = s->memcg_params->memcg; | 3175 | memcg = s->memcg_params->memcg; |
3171 | id = memcg_cache_id(memcg); | 3176 | id = memcg_cache_id(memcg); |
3172 | 3177 | ||
3173 | css_get(&memcg->css); | ||
3174 | |||
3175 | |||
3176 | /* | 3178 | /* |
3177 | * Since readers won't lock (see cache_from_memcg_idx()), we need a | 3179 | * Since readers won't lock (see cache_from_memcg_idx()), we need a |
3178 | * barrier here to ensure nobody will see the kmem_cache partially | 3180 | * barrier here to ensure nobody will see the kmem_cache partially |
@@ -3221,10 +3223,8 @@ void memcg_unregister_cache(struct kmem_cache *s) | |||
3221 | * after removing it from the memcg_slab_caches list, otherwise we can | 3223 | * after removing it from the memcg_slab_caches list, otherwise we can |
3222 | * fail to convert memcg_params_to_cache() while traversing the list. | 3224 | * fail to convert memcg_params_to_cache() while traversing the list. |
3223 | */ | 3225 | */ |
3224 | VM_BUG_ON(!root->memcg_params->memcg_caches[id]); | 3226 | VM_BUG_ON(root->memcg_params->memcg_caches[id] != s); |
3225 | root->memcg_params->memcg_caches[id] = NULL; | 3227 | root->memcg_params->memcg_caches[id] = NULL; |
3226 | |||
3227 | css_put(&memcg->css); | ||
3228 | } | 3228 | } |
3229 | 3229 | ||
3230 | /* | 3230 | /* |
diff --git a/mm/slab_common.c b/mm/slab_common.c index ccc012f00126..0c2879ff414c 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c | |||
@@ -313,9 +313,9 @@ void kmem_cache_destroy(struct kmem_cache *s) | |||
313 | s->refcount--; | 313 | s->refcount--; |
314 | if (!s->refcount) { | 314 | if (!s->refcount) { |
315 | list_del(&s->list); | 315 | list_del(&s->list); |
316 | memcg_unregister_cache(s); | ||
316 | 317 | ||
317 | if (!__kmem_cache_shutdown(s)) { | 318 | if (!__kmem_cache_shutdown(s)) { |
318 | memcg_unregister_cache(s); | ||
319 | mutex_unlock(&slab_mutex); | 319 | mutex_unlock(&slab_mutex); |
320 | if (s->flags & SLAB_DESTROY_BY_RCU) | 320 | if (s->flags & SLAB_DESTROY_BY_RCU) |
321 | rcu_barrier(); | 321 | rcu_barrier(); |
@@ -325,6 +325,7 @@ void kmem_cache_destroy(struct kmem_cache *s) | |||
325 | kmem_cache_free(kmem_cache, s); | 325 | kmem_cache_free(kmem_cache, s); |
326 | } else { | 326 | } else { |
327 | list_add(&s->list, &slab_caches); | 327 | list_add(&s->list, &slab_caches); |
328 | memcg_register_cache(s); | ||
328 | mutex_unlock(&slab_mutex); | 329 | mutex_unlock(&slab_mutex); |
329 | printk(KERN_ERR "kmem_cache_destroy %s: Slab cache still has objects\n", | 330 | printk(KERN_ERR "kmem_cache_destroy %s: Slab cache still has objects\n", |
330 | s->name); | 331 | s->name); |