Skip to contentSkip to conversation

mm,slab,memcg: call memcg kmem put cache with same condition as get

PATCH In review Rik van Riel 2 participants 4 messages Jan 9, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm,slab,memcg: call memcg kmem put cache with same condition as get

There is an imbalance between when slab_pre_alloc_hook calls memcg_kmem_get_cache and when slab_post_alloc_hook calls memcg_kmem_put_cache.

This can cause a memcg kmem cache to be destroyed right as an object from that cache is being allocated, which is probably not good. It could lead to things like a memcg allocating new kmalloc slabs instead of using freed space in old ones, maybe memory leaks, and maybe oopses as a memcg kmalloc slab is getting destroyed on one CPU while another CPU is trying to do an allocation from that same memcg.

The obvious fix would be to use the same condition for calling memcg_kmem_put_cache that we also use to decide whether to call memcg_kmem_get_cache.

I am not sure how long this bug has been around, since the last changeset to touch that code - 452647784b2f ("mm: memcontrol: cleanup
kmem charge functions") - merely moved the bug from one location to
another. I am still tagging that changeset, because the fix should automatically apply that far back.

Fixes 452647784b2f ("mm: memcontrol: cleanup kmem charge functions")
Cc Alexey Dobriyan <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc Pekka Enberg <[email protected]>
Cc Andrew Morton <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joonsoo Kim <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Tejun Heo <[email protected]>
---
 mm/slab.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/slab.h +2 -1
index 4190c24ef0e9..ab3d95bef8a0 100644
@@ -444,7 +444,8 @@ static inline void slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags,
 		p[i] = kasan_slab_alloc(s, object, flags);
 	}
 
-	if (memcg_kmem_enabled())
+	if (memcg_kmem_enabled() &&
+	    ((flags & __GFP_ACCOUNT) || (s->flags & SLAB_ACCOUNT)))
 		memcg_kmem_put_cache(s);
 }
 
Signature · 1 line
2.17.1
#1 lore raw
Quoted 5 lines · Rik van Riel

There is an imbalance between when slab_pre_alloc_hook calls memcg_kmem_get_cache and when slab_post_alloc_hook calls memcg_kmem_put_cache.

Can you explain how there is an imbalance? If the returned kmem cache from memcg_kmem_get_cache() is the memcg kmem cache then the refcnt of memcg is elevated and the memcg_kmem_put_cache() will correctly decrement the refcnt of the memcg.

Quoted 40 lines

This can cause a memcg kmem cache to be destroyed right as an object from that cache is being allocated, which is probably not good. It could lead to things like a memcg allocating new kmalloc slabs instead of using freed space in old ones, maybe memory leaks, and maybe oopses as a memcg kmalloc slab is getting destroyed on one CPU while another CPU is trying to do an allocation from that same memcg.

The obvious fix would be to use the same condition for calling memcg_kmem_put_cache that we also use to decide whether to call memcg_kmem_get_cache.

I am not sure how long this bug has been around, since the last changeset to touch that code - 452647784b2f ("mm: memcontrol: cleanup
kmem charge functions") - merely moved the bug from one location to
another. I am still tagging that changeset, because the fix should automatically apply that far back.

Fixes 452647784b2f ("mm: memcontrol: cleanup kmem charge functions")
Cc Alexey Dobriyan <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc Pekka Enberg <[email protected]>
Cc Andrew Morton <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joonsoo Kim <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Tejun Heo <[email protected]>
---
 mm/slab.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/slab.h +0 -0
index 4190c24ef0e9..ab3d95bef8a0 100644

mm/slab.h static inline void slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags, · line 448

Quoted 6 lines
mm/slab.h +2 -1
@@ -444,7 +444,8 @@ static inline void slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags,
                p[i] = kasan_slab_alloc(s, object, flags);
        }
 
-       if (memcg_kmem_enabled())
+       if (memcg_kmem_enabled() &&
+           ((flags & __GFP_ACCOUNT) || (s->flags & SLAB_ACCOUNT)))

I don't think these extra checks are needed. They are safe but not needed.

Quoted 6 lines
mm/slab.h +0 -1
                memcg_kmem_put_cache(s);
 }
 
--

2.17.1

thanks,
Shakeel

#2 lore raw
Quoted 8 lines · Shakeel Butt
Quoted 5 lines · Rik van Riel

There is an imbalance between when slab_pre_alloc_hook calls memcg_kmem_get_cache and when slab_post_alloc_hook calls memcg_kmem_put_cache.

Quoted 7 lines

Can you explain how there is an imbalance? If the returned kmem cache from memcg_kmem_get_cache() is the memcg kmem cache then the refcnt of memcg is elevated and the memcg_kmem_put_cache() will correctly decrement the refcnt of the memcg.

Quoted 2 lines

This can cause a memcg kmem cache to be destroyed right as an object from that cache is being allocated,

Also please note that the memcg kmem caches are destroyed (if empty) on memcg offline. The css_tryget_online() within memcg_kmem_get_cache() will fail.

See kernel/cgroup/cgroup.c
* 2. When the percpu_ref is confirmed to be visible as killed on all CPUs

 *    and thus css_tryget_online() is guaranteed to fail, the css can be
 *    offlined by invoking offline_css().  After offlining, the base ref is
 *    put.  Implemented in css_killed_work_fn().
Quoted 57 lines
Quoted 39 lines

which is probably
not good. It could lead to things like a memcg allocating new kmalloc slabs instead of using freed space in old ones, maybe memory leaks, and maybe oopses as a memcg kmalloc slab is getting destroyed on one CPU while another CPU is trying to do an allocation from that same memcg.

The obvious fix would be to use the same condition for calling memcg_kmem_put_cache that we also use to decide whether to call memcg_kmem_get_cache.

I am not sure how long this bug has been around, since the last changeset to touch that code - 452647784b2f ("mm: memcontrol: cleanup
kmem charge functions") - merely moved the bug from one location to
another. I am still tagging that changeset, because the fix should automatically apply that far back.

Fixes 452647784b2f ("mm: memcontrol: cleanup kmem charge functions")
Cc Alexey Dobriyan <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc Pekka Enberg <[email protected]>
Cc Andrew Morton <[email protected]>
Cc David Rientjes <[email protected]>
Cc Joonsoo Kim <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Tejun Heo <[email protected]>
---
 mm/slab.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/slab.h +0 -0
index 4190c24ef0e9..ab3d95bef8a0 100644

mm/slab.h static inline void slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags, · line 448

Quoted 6 lines
mm/slab.h +2 -1
@@ -444,7 +444,8 @@ static inline void slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags,
                p[i] = kasan_slab_alloc(s, object, flags);
        }
 
-       if (memcg_kmem_enabled())
+       if (memcg_kmem_enabled() &&
+           ((flags & __GFP_ACCOUNT) || (s->flags & SLAB_ACCOUNT)))

I don't think these extra checks are needed. They are safe but not needed.

Quoted 6 lines
mm/slab.h +0 -1
                memcg_kmem_put_cache(s);
 }
 
--

2.17.1

thanks,
Shakeel

#3 lore raw
In reply to Shakeel Butt
Quoted 7 lines · Shakeel Butt
Quoted 5 lines · Rik van Riel

There is an imbalance between when slab_pre_alloc_hook calls memcg_kmem_get_cache and when slab_post_alloc_hook calls memcg_kmem_put_cache.

Quoted 5 lines

Can you explain how there is an imbalance? If the returned kmem cache from memcg_kmem_get_cache() is the memcg kmem cache then the refcnt of
memcg is elevated and the memcg_kmem_put_cache() will correctly decrement the refcnt of the memcg.

Indeed, you are right. Never mind this patch.

Back to square one on that bug.

Signature · 1 line
All Rights Reversed.