memcg: fix possible use-after-free in memcg_kmem_get_cache
Subsystems (3)
PATCH 1/1 memcg: fix possible use-after-free in memcg_kmem_get_cache
Suppose task @t that belongs to a memory cgroup @memcg is going to allocate an object from a kmem cache @c. The copy of @c corresponding to @memcg, @mc, is empty. Then if kmem_cache_alloc races with the memory cgroup destruction we can access the memory cgroup's copy of the cache after it was destroyed:
CPU0 CPU1
---- ----
[ current=@t
@mc->memcg_params->nr_pages=0 ]
kmem_cache_alloc(@c):
call memcg_kmem_get_cache(@c);
proceed to allocation from @mc:
alloc a page for @mc:
...
move @t from @memcg
destroy @memcg:
mem_cgroup_css_offline(@memcg):
memcg_unregister_all_caches(@memcg):
kmem_cache_destroy(@mc)
add page to @mc
We could fix this issue by taking a reference to a per-memcg cache, but that would require adding a per-cpu reference counter to per-memcg caches, which would look cumbersome.
Instead, let's take a reference to a memory cgroup, which already has a per-cpu reference counter, in the beginning of kmem_cache_alloc to be dropped in the end, and move per memcg caches destruction from css offline to css free. As a side effect, per-memcg caches will be destroyed not one by one, but all at once when the last page accounted to the memory cgroup is freed. This doesn't sound as a high price for code readability though.
Note, this patch does add some overhead to the kmem_cache_alloc hot path, but it is pretty negligible - it's just a function call plus a per cpu counter decrement, which is comparable to what we already have in memcg_kmem_get_cache. Besides, it's only relevant if there are memory cgroups with kmem accounting enabled. I don't think we can find a way to handle this race w/o it, because alloc_page called from kmem_cache_alloc may sleep so we can't flush all pending kmallocs w/o reference counting.
--- include/linux/memcontrol.h | 14 ++++++++++-- include/linux/slab.h | 2 -- mm/memcontrol.c | 51 ++++++++++++++------------------------------ mm/slab.c | 2 ++ mm/slub.c | 1 + 5 files changed, 31 insertions(+), 39 deletions(-)
include/linux/memcontrol.h +12 -2
@@ -400,8 +400,8 @@ int memcg_cache_id(struct mem_cgroup *memcg);void memcg_update_array_size (int num_groups); -struct kmem_cache * -__memcg_kmem_get_cache (struct kmem_cache *cachep); +struct kmem_cache *__memcg_kmem_get_cache (struct kmem_cache *cachep); +void __memcg_kmem_put_cache (struct kmem_cache *cachep);int __memcg_charge_slab (struct kmem_cache *cachep,gfp_t gfp,int order);void __memcg_uncharge_slab (struct kmem_cache *cachep,int order);
@@ -494,6 +494,12 @@ memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)return __memcg_kmem_get_cache (cachep); } + +static __always_inline void memcg_kmem_put_cache(struct kmem_cache *cachep) +{ +if (memcg_kmem_enabled ()) +__memcg_kmem_put_cache (cachep); +}#else #define for_each_memcg_cache_index (_idx) \ for (; NULL; )
@@ -528,6 +534,10 @@ memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp) {return cachep; } + +static inline void memcg_kmem_put_cache (struct kmem_cache *cachep) +{ +}#endif /* CONFIG_MEMCG_KMEM */ #endif /* _LINUX_MEMCONTROL_H */
include/linux/slab.h +0 -2
@@ -493,7 +493,6 @@ static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node)* @memcg: pointer to the memcg this cache belongs to * @list: list_head for the list of all caches in this memcg * @root_cache: pointer to the global, root cache, this cache was derived from -* @nr_pages: number of pages that belongs to this cache. */ struct memcg_cache_params {bool is_root_cache;
@@ -506,7 +505,6 @@ struct memcg_cache_params {struct mem_cgroup *memcg;struct list_head list;struct kmem_cache *root_cache; -atomic_t nr_pages; }; }; };
mm/memcontrol.c +16 -35
127 lines not on this page · raw message
mm/slab.c +2 -0
@@ -3182,6 +3182,7 @@ slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,memset (ptr,0 , cachep->object_size); } +memcg_kmem_put_cache (cachep);return ptr; }
@@ -3247,6 +3248,7 @@ slab_alloc(struct kmem_cache *cachep, gfp_t flags, unsigned long caller)memset (objp,0 , cachep->object_size); } +memcg_kmem_put_cache (cachep);return objp; }
mm/slub.c +1 -0
@@ -2450,6 +2450,7 @@ redo:slab_post_alloc_hook (s, gfpflags, object); +memcg_kmem_put_cache (s);return object; }
Signature · 1 line
1.7.10.4
mm/slub.c redo: · line 2455
Quoted 11 lines · Vladimir Davydov
mm/slub.c +1
-0@@ -2450,6 +2450,7 @@ redo:slab_post_alloc_hook (s, gfpflags, object); +memcg_kmem_put_cache (s);return object; }
The function should be added to slab_post_alloc().
Also move the memcg_kmem_get_cache() into slab_pre_alloc_hook().
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 10 lines · Christoph Lameter
Quoted 8 lines · Vladimir Davydov
mm/slub.c +0
-0@@ -2450,6 +2450,7 @@ redo:slab_post_alloc_hook (s, gfpflags, object);
Quoted 7 lines
mm/slub.c redo: · line 2455
Quoted 3 lines
mm/slub.c +1
-0+memcg_kmem_put_cache (s);return object; }The function should be added to
slab_post_alloc().Also move the
memcg_kmem_get_cache()intoslab_pre_alloc_hook().
Sounds reasonable, thanks. The updated patch is below.
---
From: Vladimir Davydov <[email protected]>
Subject: [PATCH -mm v2] memcg: fix possible use-after-free in
memcg_kmem_get_cache
Suppose task @t that belongs to a memory cgroup @memcg is going to allocate an object from a kmem cache @c. The copy of @c corresponding to @memcg, @mc, is empty. Then if kmem_cache_alloc races with the memory cgroup destruction we can access the memory cgroup's copy of the cache after it was destroyed:
CPU0 CPU1
---- ----
[ current=@t
@mc->memcg_params->nr_pages=0 ]
kmem_cache_alloc(@c):
call memcg_kmem_get_cache(@c);
proceed to allocation from @mc:
alloc a page for @mc:
...
move @t from @memcg
destroy @memcg:
mem_cgroup_css_offline(@memcg):
memcg_unregister_all_caches(@memcg):
kmem_cache_destroy(@mc)
add page to @mc
We could fix this issue by taking a reference to a per-memcg cache, but that would require adding a per-cpu reference counter to per-memcg caches, which would look cumbersome.
Instead, let's take a reference to a memory cgroup, which already has a per-cpu reference counter, in the beginning of kmem_cache_alloc to be dropped in the end, and move per memcg caches destruction from css offline to css free. As a side effect, per-memcg caches will be destroyed not one by one, but all at once when the last page accounted to the memory cgroup is freed. This doesn't sound as a high price for code readability though.
Note, this patch does add some overhead to the kmem_cache_alloc hot path, but it is pretty negligible - it's just a function call plus a per cpu counter decrement, which is comparable to what we already have in memcg_kmem_get_cache. Besides, it's only relevant if there are memory cgroups with kmem accounting enabled. I don't think we can find a way to handle this race w/o it, because alloc_page called from kmem_cache_alloc may sleep so we can't flush all pending kmallocs w/o reference counting.
---
Changes in v2:
- move memcg_kmem_{get,put}_cache to slab_{pre,post}_alloc_hook (Christoph)
include/linux/memcontrol.h | 14 ++++++++++-- include/linux/slab.h | 2 -- mm/memcontrol.c | 51 ++++++++++++++------------------------------ mm/slab.c | 2 ++ mm/slub.c | 14 +++++++----- 5 files changed, 39 insertions(+), 44 deletions(-)
include/linux/memcontrol.h +12 -2
@@ -400,8 +400,8 @@ int memcg_cache_id(struct mem_cgroup *memcg);void memcg_update_array_size (int num_groups); -struct kmem_cache * -__memcg_kmem_get_cache (struct kmem_cache *cachep); +struct kmem_cache *__memcg_kmem_get_cache (struct kmem_cache *cachep); +void __memcg_kmem_put_cache (struct kmem_cache *cachep);int __memcg_charge_slab (struct kmem_cache *cachep,gfp_t gfp,int order);void __memcg_uncharge_slab (struct kmem_cache *cachep,int order);
@@ -494,6 +494,12 @@ memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)return __memcg_kmem_get_cache (cachep); } + +static __always_inline void memcg_kmem_put_cache(struct kmem_cache *cachep) +{ +if (memcg_kmem_enabled ()) +__memcg_kmem_put_cache (cachep); +}#else #define for_each_memcg_cache_index (_idx) \ for (; NULL; )
@@ -528,6 +534,10 @@ memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp) {return cachep; } + +static inline void memcg_kmem_put_cache (struct kmem_cache *cachep) +{ +}#endif /* CONFIG_MEMCG_KMEM */ #endif /* _LINUX_MEMCONTROL_H */
include/linux/slab.h +0 -2
@@ -493,7 +493,6 @@ static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node)* @memcg: pointer to the memcg this cache belongs to * @list: list_head for the list of all caches in this memcg * @root_cache: pointer to the global, root cache, this cache was derived from -* @nr_pages: number of pages that belongs to this cache. */ struct memcg_cache_params {bool is_root_cache;
@@ -506,7 +505,6 @@ struct memcg_cache_params {struct mem_cgroup *memcg;struct list_head list;struct kmem_cache *root_cache; -atomic_t nr_pages; }; }; };
mm/memcontrol.c +16 -35
127 lines not on this page · raw message
mm/slab.c +2 -0
@@ -3182,6 +3182,7 @@ slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,memset (ptr,0 , cachep->object_size); } +memcg_kmem_put_cache (cachep);return ptr; }
@@ -3247,6 +3248,7 @@ slab_alloc(struct kmem_cache *cachep, gfp_t flags, unsigned long caller)memset (objp,0 , cachep->object_size); } +memcg_kmem_put_cache (cachep);return objp; }
mm/slub.c +9 -5
@@ -1233,13 +1233,17 @@ static inline void kfree_hook(const void *x)kmemleak_free (x); } -static inline int slab_pre_alloc_hook (struct kmem_cache *s,gfp_t flags) +static inline struct kmem_cache *slab_pre_alloc_hook (struct kmem_cache *s, +gfp_t flags) { flags &= gfp_allowed_mask;lockdep_trace_alloc (flags);might_sleep_if (flags & __GFP_WAIT); -return should_failslab (s->object_size, flags, s->flags); +if (should_failslab (s->object_size, flags, s->flags)) +return NULL ; + +return memcg_kmem_get_cache (s, flags); } static inline void slab_post_alloc_hook(struct kmem_cache *s,
@@ -1248,6 +1252,7 @@ static inline void slab_post_alloc_hook(struct kmem_cache *s, flags &= gfp_allowed_mask;kmemcheck_slab_alloc (s, flags, object,slab_ksize (s));kmemleak_alloc_recursive (object, s->object_size,1 , s->flags, flags); +memcg_kmem_put_cache (s); }static inline void slab_free_hook (struct kmem_cache *s,void *x)
@@ -2383,10 +2388,9 @@ static __always_inline void *slab_alloc_node(struct kmem_cache *s,struct page *page;unsigned long tid; -if (slab_pre_alloc_hook (s, gfpflags)) + s =slab_pre_alloc_hook (s, gfpflags); +if (!s)return NULL ; - - s =memcg_kmem_get_cache (s, gfpflags); redo:/* * Must read kmem_cache cpu data via this cpu ptr. Preemption is
Signature · 1 line
1.7.10.4
Christoph Lameter
Acked-byQuoted 1 line · Vladimir Davydov
Sounds reasonable, thanks. The updated patch is below.
Ok. SLAB also needs to have a similar hook scheme than SLUB at some point.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>