From: Vladimir Davydov <vdavydov@parallels.com>
To: akpm@linux-foundation.org
Cc: mhocko@suse.cz, rientjes@google.com, penberg@kernel.org,
cl@linux.com, glommer@gmail.com, linux-mm@kvack.org,
linux-kernel@vger.kernel.org, devel@openvz.org
Subject: [PATCH 7/8] memcg, slab: unregister cache from memcg before starting to destroy it
Date: Sun, 2 Feb 2014 20:33:52 +0400 [thread overview]
Message-ID: <544f00fe8624c89f510a2b3d585f1a8d80606f7b.1391356789.git.vdavydov@parallels.com> (raw)
In-Reply-To: <cover.1391356789.git.vdavydov@parallels.com>
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>
---
mm/memcontrol.c | 12 ++++++------
mm/slab_common.c | 3 ++-
2 files changed, 8 insertions(+), 7 deletions(-)
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index d69c427e106b..69e8726aae4f 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -3224,6 +3224,7 @@ int memcg_alloc_cache_params(struct kmem_cache *s,
s->memcg_params->root_cache = root_cache;
INIT_WORK(&s->memcg_params->destroy,
kmem_cache_destroy_work_func);
+ css_get(&memcg->css);
} else
s->memcg_params->is_root_cache = true;
@@ -3232,6 +3233,10 @@ int memcg_alloc_cache_params(struct kmem_cache *s,
void memcg_free_cache_params(struct kmem_cache *s)
{
+ if (!s->memcg_params)
+ return;
+ if (!s->memcg_params->is_root_cache)
+ css_put(&s->memcg_params->memcg->css);
kfree(s->memcg_params);
}
@@ -3254,9 +3259,6 @@ void memcg_register_cache(struct kmem_cache *s)
memcg = s->memcg_params->memcg;
id = memcg_cache_id(memcg);
- css_get(&memcg->css);
-
-
/*
* Since readers won't lock (see cache_from_memcg_idx()), we need a
* barrier here to ensure nobody will see the kmem_cache partially
@@ -3305,10 +3307,8 @@ void memcg_unregister_cache(struct kmem_cache *s)
* after removing it from the memcg_slab_caches list, otherwise we can
* fail to convert memcg_params_to_cache() while traversing the list.
*/
- VM_BUG_ON(!root->memcg_params->memcg_caches[id]);
+ VM_BUG_ON(root->memcg_params->memcg_caches[id] != s);
root->memcg_params->memcg_caches[id] = NULL;
-
- css_put(&memcg->css);
}
/*
diff --git a/mm/slab_common.c b/mm/slab_common.c
index ade86bcddab9..ea1075e65271 100644
--- a/mm/slab_common.c
+++ b/mm/slab_common.c
@@ -312,9 +312,9 @@ void kmem_cache_destroy(struct kmem_cache *s)
s->refcount--;
if (!s->refcount) {
list_del(&s->list);
+ memcg_unregister_cache(s);
if (!__kmem_cache_shutdown(s)) {
- memcg_unregister_cache(s);
mutex_unlock(&slab_mutex);
if (s->flags & SLAB_DESTROY_BY_RCU)
rcu_barrier();
@@ -324,6 +324,7 @@ void kmem_cache_destroy(struct kmem_cache *s)
kmem_cache_free(kmem_cache, s);
} else {
list_add(&s->list, &slab_caches);
+ memcg_register_cache(s);
mutex_unlock(&slab_mutex);
printk(KERN_ERR "kmem_cache_destroy %s: Slab cache still has objects\n",
s->name);
--
1.7.10.4
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
next prev parent reply other threads:[~2014-02-02 16:34 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-02-02 16:33 [PATCH 0/8] memcg-vs-slab related fixes, improvements, cleanups Vladimir Davydov
2014-02-02 16:33 ` [PATCH 1/8] memcg: export kmemcg cache id via cgroup fs Vladimir Davydov
2014-02-03 6:21 ` David Rientjes
2014-02-03 6:57 ` Vladimir Davydov
2014-02-03 7:19 ` Vladimir Davydov
2014-02-03 10:05 ` Glauber Costa
2014-02-03 13:01 ` Vladimir Davydov
2014-02-03 11:04 ` David Rientjes
2014-02-03 13:00 ` Vladimir Davydov
2014-02-04 14:44 ` Michal Hocko
2014-02-04 14:40 ` Michal Hocko
2014-02-04 14:49 ` Vladimir Davydov
2014-02-02 16:33 ` [PATCH 2/8] memcg, slab: remove cgroup name from memcg cache names Vladimir Davydov
2014-02-04 14:45 ` Michal Hocko
2014-02-04 15:11 ` Vladimir Davydov
2014-02-04 15:13 ` Michal Hocko
2014-02-02 16:33 ` [PATCH 3/8] memcg, slab: never try to merge memcg caches Vladimir Davydov
2014-02-04 14:52 ` Michal Hocko
2014-02-04 14:59 ` Vladimir Davydov
2014-02-04 15:11 ` Michal Hocko
2014-02-04 15:27 ` Vladimir Davydov
2014-02-04 15:43 ` Glauber Costa
2014-02-04 16:04 ` Vladimir Davydov
2014-02-04 16:10 ` Glauber Costa
2014-02-06 14:07 ` Michal Hocko
2014-02-06 14:15 ` Vladimir Davydov
2014-02-06 15:29 ` Michal Hocko
2014-02-06 15:39 ` Vladimir Davydov
2014-02-02 16:33 ` [PATCH 4/8] memcg, slab: separate memcg vs root cache creation paths Vladimir Davydov
2014-02-02 16:33 ` [PATCH 5/8] slub: adjust memcg caches when creating cache alias Vladimir Davydov
2014-02-02 16:33 ` [PATCH 6/8] slub: rework sysfs layout for memcg caches Vladimir Davydov
2014-02-02 16:33 ` Vladimir Davydov [this message]
2014-02-02 16:33 ` [PATCH 8/8] memcg, slab: do not destroy children caches if parent has aliases Vladimir Davydov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=544f00fe8624c89f510a2b3d585f1a8d80606f7b.1391356789.git.vdavydov@parallels.com \
--to=vdavydov@parallels.com \
--cc=akpm@linux-foundation.org \
--cc=cl@linux.com \
--cc=devel@openvz.org \
--cc=glommer@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@suse.cz \
--cc=penberg@kernel.org \
--cc=rientjes@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox