Skip to content

Commit d38a2b7

Browse files
Muchun Songtorvalds
authored andcommitted
mm: memcg/slab: fix memory leak at non-root kmem_cache destroy
If the kmem_cache refcount is greater than one, we should not mark the root kmem_cache as dying. If we mark the root kmem_cache dying incorrectly, the non-root kmem_cache can never be destroyed. It resulted in memory leak when memcg was destroyed. We can use the following steps to reproduce. 1) Use kmem_cache_create() to create a new kmem_cache named A. 2) Coincidentally, the kmem_cache A is an alias for kmem_cache B, so the refcount of B is just increased. 3) Use kmem_cache_destroy() to destroy the kmem_cache A, just decrease the B's refcount but mark the B as dying. 4) Create a new memory cgroup and alloc memory from the kmem_cache B. It leads to create a non-root kmem_cache for allocating memory. 5) When destroy the memory cgroup created in the step 4), the non-root kmem_cache can never be destroyed. If we repeat steps 4) and 5), this will cause a lot of memory leak. So only when refcount reach zero, we mark the root kmem_cache as dying. Fixes: 92ee383 ("mm: fix race between kmem_cache destroy, create and deactivate") Signed-off-by: Muchun Song <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Reviewed-by: Shakeel Butt <[email protected]> Acked-by: Roman Gushchin <[email protected]> Cc: Vlastimil Babka <[email protected]> Cc: Christoph Lameter <[email protected]> Cc: Pekka Enberg <[email protected]> Cc: David Rientjes <[email protected]> Cc: Joonsoo Kim <[email protected]> Cc: Shakeel Butt <[email protected]> Cc: <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Linus Torvalds <[email protected]>
1 parent 8d22a93 commit d38a2b7

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

mm/slab_common.c

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,14 @@ int slab_unmergeable(struct kmem_cache *s)
326326
if (s->refcount < 0)
327327
return 1;
328328

329+
#ifdef CONFIG_MEMCG_KMEM
330+
/*
331+
* Skip the dying kmem_cache.
332+
*/
333+
if (s->memcg_params.dying)
334+
return 1;
335+
#endif
336+
329337
return 0;
330338
}
331339

@@ -886,12 +894,15 @@ static int shutdown_memcg_caches(struct kmem_cache *s)
886894
return 0;
887895
}
888896

889-
static void flush_memcg_workqueue(struct kmem_cache *s)
897+
static void memcg_set_kmem_cache_dying(struct kmem_cache *s)
890898
{
891899
spin_lock_irq(&memcg_kmem_wq_lock);
892900
s->memcg_params.dying = true;
893901
spin_unlock_irq(&memcg_kmem_wq_lock);
902+
}
894903

904+
static void flush_memcg_workqueue(struct kmem_cache *s)
905+
{
895906
/*
896907
* SLAB and SLUB deactivate the kmem_caches through call_rcu. Make
897908
* sure all registered rcu callbacks have been invoked.
@@ -923,10 +934,6 @@ static inline int shutdown_memcg_caches(struct kmem_cache *s)
923934
{
924935
return 0;
925936
}
926-
927-
static inline void flush_memcg_workqueue(struct kmem_cache *s)
928-
{
929-
}
930937
#endif /* CONFIG_MEMCG_KMEM */
931938

932939
void slab_kmem_cache_release(struct kmem_cache *s)
@@ -944,8 +951,6 @@ void kmem_cache_destroy(struct kmem_cache *s)
944951
if (unlikely(!s))
945952
return;
946953

947-
flush_memcg_workqueue(s);
948-
949954
get_online_cpus();
950955
get_online_mems();
951956

@@ -955,6 +960,22 @@ void kmem_cache_destroy(struct kmem_cache *s)
955960
if (s->refcount)
956961
goto out_unlock;
957962

963+
#ifdef CONFIG_MEMCG_KMEM
964+
memcg_set_kmem_cache_dying(s);
965+
966+
mutex_unlock(&slab_mutex);
967+
968+
put_online_mems();
969+
put_online_cpus();
970+
971+
flush_memcg_workqueue(s);
972+
973+
get_online_cpus();
974+
get_online_mems();
975+
976+
mutex_lock(&slab_mutex);
977+
#endif
978+
958979
err = shutdown_memcg_caches(s);
959980
if (!err)
960981
err = shutdown_cache(s);

0 commit comments

Comments
 (0)