From: Vladimir Davydov <vdavydov@parallels.com>
To: akpm@linux-foundation.org
Cc: cl@linux.com, iamjoonsoo.kim@lge.com, rientjes@google.com,
penberg@kernel.org, hannes@cmpxchg.org, mhocko@suse.cz,
linux-kernel@vger.kernel.org, linux-mm@kvack.org
Subject: [PATCH -mm v2 6/8] memcg: wait for kfree's to finish before destroying cache
Date: Fri, 6 Jun 2014 17:22:43 +0400 [thread overview]
Message-ID: <07a2c0a9d898a68a7c8593c398d428aa16be9fd5.1402060096.git.vdavydov@parallels.com> (raw)
In-Reply-To: <cover.1402060096.git.vdavydov@parallels.com>
kmem_cache_free doesn't expect that the cache can be destroyed as soon
as the object is freed, e.g. SLUB's implementation may want to update
cache stats after putting the object to the free list.
Therefore we should wait for all kmem_cache_free's to finish before
proceeding to cache destruction. Since both SLAB and SLUB versions of
kmem_cache_free are non-preemptable, we wait for rcu-sched grace period
to elapse.
Signed-off-by: Vladimir Davydov <vdavydov@parallels.com>
---
include/linux/slab.h | 6 ++----
mm/memcontrol.c | 34 ++++++++++++++++++++++++++++++++--
2 files changed, 34 insertions(+), 6 deletions(-)
diff --git a/include/linux/slab.h b/include/linux/slab.h
index d99d5212b815..68b1feaba9d6 100644
--- a/include/linux/slab.h
+++ b/include/linux/slab.h
@@ -532,11 +532,9 @@ static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node)
*/
struct memcg_cache_params {
bool is_root_cache;
+ struct rcu_head rcu_head;
union {
- struct {
- struct rcu_head rcu_head;
- struct kmem_cache *memcg_caches[0];
- };
+ struct kmem_cache *memcg_caches[0];
struct {
struct mem_cgroup *memcg;
struct list_head list;
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index ed42fd1105a5..5b3543a9257e 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -3232,6 +3232,14 @@ static void memcg_unregister_cache_func(struct work_struct *work)
mutex_unlock(&memcg_slab_mutex);
}
+static void memcg_unregister_cache_rcu_func(struct rcu_head *rcu)
+{
+ struct memcg_cache_params *params =
+ container_of(rcu, struct memcg_cache_params, rcu_head);
+
+ schedule_work(¶ms->unregister_work);
+}
+
/*
* During the creation a new cache, we need to disable our accounting mechanism
* altogether. This is true even if we are not creating, but rather just
@@ -3287,6 +3295,7 @@ static void memcg_unregister_all_caches(struct mem_cgroup *memcg)
{
struct kmem_cache *cachep;
struct memcg_cache_params *params, *tmp;
+ LIST_HEAD(empty_caches);
if (!memcg_kmem_is_active(memcg))
return;
@@ -3297,7 +3306,26 @@ static void memcg_unregister_all_caches(struct mem_cgroup *memcg)
cachep->memcg_params->dead = true;
kmem_cache_shrink(cachep);
if (atomic_long_dec_and_test(&cachep->memcg_params->refcnt))
- memcg_unregister_cache(cachep);
+ list_move(&cachep->memcg_params->list, &empty_caches);
+ }
+
+ /*
+ * kmem_cache_free doesn't expect that the cache can be destroyed as
+ * soon as the object is freed, e.g. SLUB's implementation may want to
+ * update cache stats after putting the object to the free list.
+ *
+ * Therefore we should wait for all kmem_cache_free's to finish before
+ * proceeding to cache destruction. Since both SLAB and SLUB versions
+ * of kmem_cache_free are non-preemptable, we wait for rcu-sched grace
+ * period to elapse.
+ */
+ synchronize_sched();
+
+ while (!list_empty(&empty_caches)) {
+ params = list_first_entry(&empty_caches,
+ struct memcg_cache_params, list);
+ cachep = memcg_params_to_cache(params);
+ memcg_unregister_cache(cachep);
}
mutex_unlock(&memcg_slab_mutex);
}
@@ -3379,7 +3407,9 @@ void __memcg_uncharge_slab(struct kmem_cache *cachep, int order)
memcg_uncharge_kmem(cachep->memcg_params->memcg, PAGE_SIZE << order);
if (unlikely(atomic_long_dec_and_test(&cachep->memcg_params->refcnt)))
- schedule_work(&cachep->memcg_params->unregister_work);
+ /* see memcg_unregister_all_caches */
+ call_rcu_sched(&cachep->memcg_params->rcu_head,
+ memcg_unregister_cache_rcu_func);
}
/*
--
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-06-06 13:22 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-06-06 13:22 [PATCH -mm v2 0/8] memcg/slab: reintroduce dead cache self-destruction Vladimir Davydov
2014-06-06 13:22 ` [PATCH -mm v2 1/8] memcg: cleanup memcg_cache_params refcnt usage Vladimir Davydov
2014-06-06 13:22 ` [PATCH -mm v2 2/8] memcg: destroy kmem caches when last slab is freed Vladimir Davydov
2014-06-06 13:22 ` [PATCH -mm v2 3/8] memcg: mark caches that belong to offline memcgs as dead Vladimir Davydov
2014-06-10 7:48 ` Joonsoo Kim
2014-06-10 10:06 ` Vladimir Davydov
2014-06-06 13:22 ` [PATCH -mm v2 4/8] slub: don't fail kmem_cache_shrink if slab placement optimization fails Vladimir Davydov
2014-06-06 13:22 ` [PATCH -mm v2 5/8] slub: make slab_free non-preemptable Vladimir Davydov
2014-06-06 14:46 ` Christoph Lameter
2014-06-09 12:52 ` Vladimir Davydov
2014-06-09 13:52 ` Christoph Lameter
2014-06-12 6:58 ` Joonsoo Kim
2014-06-12 10:03 ` Vladimir Davydov
2014-06-06 13:22 ` Vladimir Davydov [this message]
2014-06-06 13:22 ` [PATCH -mm v2 7/8] slub: make dead memcg caches discard free slabs immediately Vladimir Davydov
2014-06-06 14:48 ` Christoph Lameter
2014-06-10 8:09 ` Joonsoo Kim
2014-06-10 10:09 ` Vladimir Davydov
2014-06-06 13:22 ` [PATCH -mm v2 8/8] slab: " Vladimir Davydov
2014-06-06 14:52 ` Christoph Lameter
2014-06-09 13:04 ` Vladimir Davydov
2014-06-10 7:43 ` Joonsoo Kim
2014-06-10 10:03 ` Vladimir Davydov
2014-06-10 14:26 ` Christoph Lameter
2014-06-10 15:18 ` Vladimir Davydov
2014-06-11 8:11 ` Joonsoo Kim
2014-06-11 21:24 ` Vladimir Davydov
2014-06-12 6:53 ` Joonsoo Kim
2014-06-12 10:02 ` Vladimir Davydov
2014-06-13 16:34 ` Christoph Lameter
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=07a2c0a9d898a68a7c8593c398d428aa16be9fd5.1402060096.git.vdavydov@parallels.com \
--to=vdavydov@parallels.com \
--cc=akpm@linux-foundation.org \
--cc=cl@linux.com \
--cc=hannes@cmpxchg.org \
--cc=iamjoonsoo.kim@lge.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