[PATCH 07/10] memcg: destroy memcg caches
This patch implements destruction of memcg caches. Right now, only caches where our reference counter is the last remaining are deleted. If there are any other reference counters around, we just leave the caches lying around until they go away. When that happen, a destruction function is called from the cache code. Caches are only destroyed in process context, so we queue them up for later processing in the general case. Signed-off-by: Glauber Costa CC: Christoph Lameter CC: Pekka Enberg CC: Michal Hocko CC: Kamezawa Hiroyuki CC: Johannes Weiner CC: Suleiman Souhlal --- include/linux/memcontrol.h |1 + include/linux/slab.h |3 ++ mm/memcontrol.c| 84 mm/slab.c |4 +++ mm/slab.h | 21 +++ mm/slub.c |7 +++- 6 files changed, 119 insertions(+), 1 deletion(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index bd1f34b..247019f 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -425,6 +425,7 @@ void memcg_register_cache(struct mem_cgroup *memcg, void memcg_release_cache(struct kmem_cache *cachep); struct kmem_cache * __memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp); +void mem_cgroup_destroy_cache(struct kmem_cache *cachep); #else static inline void memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s) diff --git a/include/linux/slab.h b/include/linux/slab.h index 249a0d3..b9310a4 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -186,6 +186,9 @@ struct mem_cgroup_cache_params { struct mem_cgroup *memcg; struct kmem_cache *parent; int id; + bool dead; + atomic_t nr_pages; + struct list_head destroyed_list; /* Used when deleting memcg cache */ }; #endif diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 2cc3acf..1231d86 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -599,6 +599,8 @@ void memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *cachep) { int id = -1; + INIT_LIST_HEAD(>memcg_params.destroyed_list); + if (!memcg) id = ida_simple_get(_types, 0, MAX_KMEM_CACHE_TYPES, GFP_KERNEL); @@ -768,6 +770,7 @@ static struct kmem_cache *memcg_create_kmem_cache(struct mem_cgroup *memcg, mem_cgroup_get(memcg); memcg->slabs[idx] = new_cachep; new_cachep->memcg_params.memcg = memcg; + atomic_set(_cachep->memcg_params.nr_pages , 0); out: mutex_unlock(_cache_mutex); return new_cachep; @@ -782,6 +785,55 @@ struct create_work { /* Use a single spinlock for destruction and creation, not a frequent op */ static DEFINE_SPINLOCK(cache_queue_lock); static LIST_HEAD(create_queue); +static LIST_HEAD(destroyed_caches); + +static void kmem_cache_destroy_work_func(struct work_struct *w) +{ + struct kmem_cache *cachep; + struct mem_cgroup_cache_params *p, *tmp; + unsigned long flags; + LIST_HEAD(del_unlocked); + + spin_lock_irqsave(_queue_lock, flags); + list_for_each_entry_safe(p, tmp, _caches, destroyed_list) { + cachep = container_of(p, struct kmem_cache, memcg_params); + list_move(>memcg_params.destroyed_list, _unlocked); + } + spin_unlock_irqrestore(_queue_lock, flags); + + list_for_each_entry_safe(p, tmp, _unlocked, destroyed_list) { + cachep = container_of(p, struct kmem_cache, memcg_params); + list_del(>memcg_params.destroyed_list); + if (!atomic_read(>memcg_params.nr_pages)) { + mem_cgroup_put(cachep->memcg_params.memcg); + kmem_cache_destroy(cachep); + } + } +} +static DECLARE_WORK(kmem_cache_destroy_work, kmem_cache_destroy_work_func); + +static void __mem_cgroup_destroy_cache(struct kmem_cache *cachep) +{ + BUG_ON(cachep->memcg_params.id != -1); + list_add(>memcg_params.destroyed_list, _caches); +} + +void mem_cgroup_destroy_cache(struct kmem_cache *cachep) +{ + unsigned long flags; + + if (!cachep->memcg_params.dead) + return; + /* +* We have to defer the actual destroying to a workqueue, because +* we might currently be in a context that cannot sleep. +*/ + spin_lock_irqsave(_queue_lock, flags); + __mem_cgroup_destroy_cache(cachep); + spin_unlock_irqrestore(_queue_lock, flags); + + schedule_work(_cache_destroy_work); +} /* * Flush the queue of kmem_caches to create, because we're creating a cgroup. @@ -803,6 +855,33 @@ void memcg_flush_cache_create_queue(void) spin_unlock_irqrestore(_queue_lock, flags); } +static void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg) +{ + struct kmem_cache *cachep; + unsigned long flags; + int
[PATCH 07/10] memcg: destroy memcg caches
This patch implements destruction of memcg caches. Right now, only caches where our reference counter is the last remaining are deleted. If there are any other reference counters around, we just leave the caches lying around until they go away. When that happen, a destruction function is called from the cache code. Caches are only destroyed in process context, so we queue them up for later processing in the general case. Signed-off-by: Glauber Costa glom...@parallels.com CC: Christoph Lameter c...@linux.com CC: Pekka Enberg penb...@cs.helsinki.fi CC: Michal Hocko mho...@suse.cz CC: Kamezawa Hiroyuki kamezawa.hir...@jp.fujitsu.com CC: Johannes Weiner han...@cmpxchg.org CC: Suleiman Souhlal sulei...@google.com --- include/linux/memcontrol.h |1 + include/linux/slab.h |3 ++ mm/memcontrol.c| 84 mm/slab.c |4 +++ mm/slab.h | 21 +++ mm/slub.c |7 +++- 6 files changed, 119 insertions(+), 1 deletion(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index bd1f34b..247019f 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -425,6 +425,7 @@ void memcg_register_cache(struct mem_cgroup *memcg, void memcg_release_cache(struct kmem_cache *cachep); struct kmem_cache * __memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp); +void mem_cgroup_destroy_cache(struct kmem_cache *cachep); #else static inline void memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s) diff --git a/include/linux/slab.h b/include/linux/slab.h index 249a0d3..b9310a4 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -186,6 +186,9 @@ struct mem_cgroup_cache_params { struct mem_cgroup *memcg; struct kmem_cache *parent; int id; + bool dead; + atomic_t nr_pages; + struct list_head destroyed_list; /* Used when deleting memcg cache */ }; #endif diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 2cc3acf..1231d86 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -599,6 +599,8 @@ void memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *cachep) { int id = -1; + INIT_LIST_HEAD(cachep-memcg_params.destroyed_list); + if (!memcg) id = ida_simple_get(cache_types, 0, MAX_KMEM_CACHE_TYPES, GFP_KERNEL); @@ -768,6 +770,7 @@ static struct kmem_cache *memcg_create_kmem_cache(struct mem_cgroup *memcg, mem_cgroup_get(memcg); memcg-slabs[idx] = new_cachep; new_cachep-memcg_params.memcg = memcg; + atomic_set(new_cachep-memcg_params.nr_pages , 0); out: mutex_unlock(memcg_cache_mutex); return new_cachep; @@ -782,6 +785,55 @@ struct create_work { /* Use a single spinlock for destruction and creation, not a frequent op */ static DEFINE_SPINLOCK(cache_queue_lock); static LIST_HEAD(create_queue); +static LIST_HEAD(destroyed_caches); + +static void kmem_cache_destroy_work_func(struct work_struct *w) +{ + struct kmem_cache *cachep; + struct mem_cgroup_cache_params *p, *tmp; + unsigned long flags; + LIST_HEAD(del_unlocked); + + spin_lock_irqsave(cache_queue_lock, flags); + list_for_each_entry_safe(p, tmp, destroyed_caches, destroyed_list) { + cachep = container_of(p, struct kmem_cache, memcg_params); + list_move(cachep-memcg_params.destroyed_list, del_unlocked); + } + spin_unlock_irqrestore(cache_queue_lock, flags); + + list_for_each_entry_safe(p, tmp, del_unlocked, destroyed_list) { + cachep = container_of(p, struct kmem_cache, memcg_params); + list_del(cachep-memcg_params.destroyed_list); + if (!atomic_read(cachep-memcg_params.nr_pages)) { + mem_cgroup_put(cachep-memcg_params.memcg); + kmem_cache_destroy(cachep); + } + } +} +static DECLARE_WORK(kmem_cache_destroy_work, kmem_cache_destroy_work_func); + +static void __mem_cgroup_destroy_cache(struct kmem_cache *cachep) +{ + BUG_ON(cachep-memcg_params.id != -1); + list_add(cachep-memcg_params.destroyed_list, destroyed_caches); +} + +void mem_cgroup_destroy_cache(struct kmem_cache *cachep) +{ + unsigned long flags; + + if (!cachep-memcg_params.dead) + return; + /* +* We have to defer the actual destroying to a workqueue, because +* we might currently be in a context that cannot sleep. +*/ + spin_lock_irqsave(cache_queue_lock, flags); + __mem_cgroup_destroy_cache(cachep); + spin_unlock_irqrestore(cache_queue_lock, flags); + + schedule_work(kmem_cache_destroy_work); +} /* * Flush the queue of kmem_caches to create, because we're creating a cgroup. @@ -803,6 +855,33 @@ void