diff options
author | Vladimir Davydov <vdavydov@parallels.com> | 2014-01-23 18:53:01 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2014-01-23 19:36:51 -0500 |
commit | 96403da244443d9842dbf290c2a02390b78a158e (patch) | |
tree | ca3ccfc550f9c3ef0cf2baa634e784a6a66b3f73 /mm | |
parent | 959c8963fc6c8c9b97e80c55ce77105247040e7d (diff) |
memcg: fix possible NULL deref while traversing memcg_slab_caches list
All caches of the same memory cgroup are linked in the memcg_slab_caches
list via kmem_cache::memcg_params::list. This list is traversed, for
example, when we read memory.kmem.slabinfo.
Since the list actually consists of memcg_cache_params objects, we have
to convert an element of the list to a kmem_cache object using
memcg_params_to_cache(), which obtains the pointer to the cache from the
memcg_params::memcg_caches array of the corresponding root cache. That
said the pointer to a kmem_cache in its parent's memcg_params must be
initialized before adding the cache to the list, and cleared only after
it has been unlinked. Currently it is vice-versa, which can result in a
NULL ptr dereference while traversing the memcg_slab_caches list. This
patch restores the correct order.
Signed-off-by: Vladimir Davydov <vdavydov@parallels.com>
Cc: Michal Hocko <mhocko@suse.cz>
Cc: Glauber Costa <glommer@gmail.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Balbir Singh <bsingharora@gmail.com>
Cc: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.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 | 25 |
1 files changed, 18 insertions, 7 deletions
diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 322d18dc17f0..014a4f1acf1c 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c | |||
@@ -3270,9 +3270,6 @@ void memcg_register_cache(struct kmem_cache *s) | |||
3270 | 3270 | ||
3271 | css_get(&memcg->css); | 3271 | css_get(&memcg->css); |
3272 | 3272 | ||
3273 | mutex_lock(&memcg->slab_caches_mutex); | ||
3274 | list_add(&s->memcg_params->list, &memcg->memcg_slab_caches); | ||
3275 | mutex_unlock(&memcg->slab_caches_mutex); | ||
3276 | 3273 | ||
3277 | /* | 3274 | /* |
3278 | * Since readers won't lock (see cache_from_memcg_idx()), we need a | 3275 | * Since readers won't lock (see cache_from_memcg_idx()), we need a |
@@ -3281,7 +3278,16 @@ void memcg_register_cache(struct kmem_cache *s) | |||
3281 | */ | 3278 | */ |
3282 | smp_wmb(); | 3279 | smp_wmb(); |
3283 | 3280 | ||
3281 | /* | ||
3282 | * Initialize the pointer to this cache in its parent's memcg_params | ||
3283 | * before adding it to the memcg_slab_caches list, otherwise we can | ||
3284 | * fail to convert memcg_params_to_cache() while traversing the list. | ||
3285 | */ | ||
3284 | root->memcg_params->memcg_caches[id] = s; | 3286 | root->memcg_params->memcg_caches[id] = s; |
3287 | |||
3288 | mutex_lock(&memcg->slab_caches_mutex); | ||
3289 | list_add(&s->memcg_params->list, &memcg->memcg_slab_caches); | ||
3290 | mutex_unlock(&memcg->slab_caches_mutex); | ||
3285 | } | 3291 | } |
3286 | 3292 | ||
3287 | void memcg_unregister_cache(struct kmem_cache *s) | 3293 | void memcg_unregister_cache(struct kmem_cache *s) |
@@ -3293,16 +3299,21 @@ void memcg_unregister_cache(struct kmem_cache *s) | |||
3293 | if (is_root_cache(s)) | 3299 | if (is_root_cache(s)) |
3294 | return; | 3300 | return; |
3295 | 3301 | ||
3296 | memcg = s->memcg_params->memcg; | ||
3297 | id = memcg_cache_id(memcg); | ||
3298 | |||
3299 | root = s->memcg_params->root_cache; | 3302 | root = s->memcg_params->root_cache; |
3300 | root->memcg_params->memcg_caches[id] = NULL; | 3303 | memcg = s->memcg_params->memcg; |
3304 | id = memcg_cache_id(memcg); | ||
3301 | 3305 | ||
3302 | mutex_lock(&memcg->slab_caches_mutex); | 3306 | mutex_lock(&memcg->slab_caches_mutex); |
3303 | list_del(&s->memcg_params->list); | 3307 | list_del(&s->memcg_params->list); |
3304 | mutex_unlock(&memcg->slab_caches_mutex); | 3308 | mutex_unlock(&memcg->slab_caches_mutex); |
3305 | 3309 | ||
3310 | /* | ||
3311 | * Clear the pointer to this cache in its parent's memcg_params only | ||
3312 | * after removing it from the memcg_slab_caches list, otherwise we can | ||
3313 | * fail to convert memcg_params_to_cache() while traversing the list. | ||
3314 | */ | ||
3315 | root->memcg_params->memcg_caches[id] = NULL; | ||
3316 | |||
3306 | css_put(&memcg->css); | 3317 | css_put(&memcg->css); |
3307 | } | 3318 | } |
3308 | 3319 | ||