mm: fix race between kmem_cache destroy, create and deactivate
Subsystems (4)
PATCH v2 1/1 mm: fix race between kmem_cache destroy, create and deactivate
The memcg kmem cache creation and deactivation (SLUB only) is asynchronous. If a root kmem cache is destroyed whose memcg cache is in the process of creation or deactivation, the kernel may crash.
Example of one such crash:
general protection fault: 0000 [#1] SMP PTI CPU: 1 PID: 1721 Comm: kworker/14:1 Not tainted 4.17.0-smp ... Workqueue: memcg_kmem_cache kmemcg_deactivate_workfn RIP: 0010:has_cpu_slab ... Call Trace: ? on_each_cpu_cond __kmem_cache_shrink kmemcg_cache_deact_after_rcu kmemcg_deactivate_workfn process_one_work worker_thread kthread ret_from_fork+0x35/0x40
This issue is due to the lack of real reference counting for the root kmem_caches. Currently kmem_cache does have a field named refcount which has been used for multiple purposes i.e. shared count, reference count and noshare flag. Due to its conflated nature, it can not be used for reference counting by other subsystems.
This patch decoupled the reference counting from shared count and noshare flag. The new field 'shared_count' represents the shared count and noshare flag while 'refcount' is converted into a real reference counter.
The reference counting is only implemented for root kmem_caches for simplicity. The reference of a root kmem_cache is elevated on sharing or while its memcg kmem_cache creation or deactivation request is in the fly and thus it is made sure that the root kmem_cache is not destroyed in the middle. As the reference of kmem_cache is elevated on sharing, the 'shared_count' does not need any locking protection as at worst it can be out-dated for a small window which is tolerable.
---
Changelog since v1:
- Added more documentation to the code
- Renamed fields to be more readable
include/linux/slab.h | 2 + include/linux/slab_def.h | 5 +- include/linux/slub_def.h | 3 +- mm/memcontrol.c | 7 +++ mm/slab.c | 4 +- mm/slab.h | 5 +- mm/slab_common.c | 122 ++++++++++++++++++++++++++++++++------- mm/slub.c | 14 +++-- 8 files changed, 130 insertions(+), 32 deletions(-)
include/linux/slab.h +2 -0
@@ -674,6 +674,8 @@ struct memcg_cache_params { };int memcg_update_all_caches (int num_memcgs); +bool kmem_cache_tryget (struct kmem_cache *s); +void kmem_cache_put (struct kmem_cache *s);/** * kmalloc_array - allocate memory for an array.
include/linux/slab_def.h +4 -1
@@ -41,7 +41,10 @@ struct kmem_cache {/* 4) cache creation/removal */ const char *name;struct list_head list; -int refcount; +/* Refcount for root kmem caches */ +refcount_t refcount; +/* Number of root kmem caches sharing this cache */ +int shared_count;int object_size;int align;
include/linux/slub_def.h +2 -1
@@ -97,7 +97,8 @@ struct kmem_cache {struct kmem_cache_order_objects max;struct kmem_cache_order_objects min;gfp_t allocflags;/* gfp flags to use on each alloc */ -int refcount;/* Refcount for slab cache destroy */ +refcount_t refcount;/* Refcount for root kmem cache */ +int shared_count;/* Number of kmem caches sharing this cache */ void (*ctor)(void *);unsigned int inuse;/* Offset to metadata */ unsigned int align;/* Alignment */
mm/memcontrol.c +7 -0
@@ -2185,6 +2185,7 @@ static void memcg_kmem_cache_create_func(struct work_struct *w)memcg_create_kmem_cache (memcg, cachep);css_put (&memcg->css); +kmem_cache_put (cachep);kfree (cw); }
@@ -2200,6 +2201,12 @@ static void __memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg,if (!cw)return ; +/* Make sure root kmem cache does not get destroyed in the middle */ +if (!kmem_cache_tryget (cachep)) { +kfree (cw); +return ; + } +css_get (&memcg->css); cw->memcg = memcg;
mm/slab.c +2 -2
@@ -1883,8 +1883,8 @@ __kmem_cache_alias(const char *name, unsigned int size, unsigned int align,struct kmem_cache *cachep; cachep =find_mergeable (size, align, flags, name, ctor); -if (cachep) { - cachep->refcount++; +if (cachep &&kmem_cache_tryget (cachep)) { + cachep->shared_count++;/* * Adjust the object sizes so that we clear
mm/slab.h +3 -2
@@ -25,7 +25,8 @@ struct kmem_cache {unsigned int useroffset;/* Usercopy region offset */ unsigned int usersize;/* Usercopy region size */ const char *name;/* Slab name for sysfs */ -int refcount;/* Use counter */ +refcount_t refcount;/* Refcount for root kmem cache */ +int shared_count;/* Number of kmem caches sharing this cache */ void (*ctor)(void *);/* Called on object slot creation */ struct list_head list;/* List of all slab caches on the system */ };
@@ -295,7 +296,7 @@ extern void slab_init_memcg_params(struct kmem_cache *);extern void memcg_link_cache (struct kmem_cache *s);extern void slab_deactivate_memcg_cache_rcu_sched (struct kmem_cache *s,void (*deact_fn)(struct kmem_cache *)); - +extern void kmem_cache_put_locked (struct kmem_cache *s);#else /* CONFIG_MEMCG && !CONFIG_SLOB */ /* If !memcg, all caches are root. */
mm/slab_common.c +102 -20
187 lines not on this page · raw message
mm/slub.c +8 -6
@@ -4270,8 +4270,8 @@ __kmem_cache_alias(const char *name, unsigned int size, unsigned int align,struct kmem_cache *s, *c; s =find_mergeable (size, align, flags, name, ctor); -if (s) { - s->refcount++; +if (s &&kmem_cache_tryget (s)) { + s->shared_count++;/* * Adjust the object sizes so that we clear
@@ -4286,7 +4286,8 @@ __kmem_cache_alias(const char *name, unsigned int size, unsigned int align, }if (sysfs_slab_alias (s, name)) { - s->refcount--; + s->shared_count--; +kmem_cache_put_locked (s); s =NULL ; } }
@@ -5009,7 +5010,8 @@ SLAB_ATTR_RO(ctor);static ssize_t aliases_show (struct kmem_cache *s,char *buf) { -return sprintf (buf,"%d\n" , s->refcount <0 ?0 : s->refcount -1 ); +return sprintf (buf,"%d\n" , + s->shared_count <0 ?0 : s->shared_count -1 ); }SLAB_ATTR_RO (aliases);
@@ -5162,7 +5164,7 @@ static ssize_t trace_store(struct kmem_cache *s, const char *buf,* as well as cause other issues like converting a mergeable * cache into an umergeable one. */ -if (s->refcount >1 ) +if (s->shared_count >1 )return -EINVAL ; s->flags &= ~SLAB_TRACE ;
@@ -5280,7 +5282,7 @@ static ssize_t failslab_show(struct kmem_cache *s, char *buf)static ssize_t failslab_store (struct kmem_cache *s,const char *buf,size_t length) { -if (s->refcount >1 ) +if (s->shared_count >1 )return -EINVAL ; s->flags &= ~SLAB_FAILSLAB ;
Signature · 1 line
2.17.0.441.gb46fe60e1d-goog
Quoted 32 lines · Shakeel Butt
The memcg kmem cache creation and deactivation (SLUB only) is asynchronous. If a root kmem cache is destroyed whose memcg cache is in the process of creation or deactivation, the kernel may crash.
Example of one such crash:
general protection fault: 0000 [#1] SMP PTI CPU: 1 PID: 1721 Comm: kworker/14:1 Not tainted 4.17.0-smp ... Workqueue: memcg_kmem_cache kmemcg_deactivate_workfn RIP: 0010:has_cpu_slab ... Call Trace: ? on_each_cpu_cond __kmem_cache_shrink kmemcg_cache_deact_after_rcu kmemcg_deactivate_workfn process_one_work worker_thread kthread ret_from_fork+0x35/0x40This issue is due to the lack of real reference counting for the root
kmem_caches. Currentlykmem_cachedoes have a field named refcount which has been used for multiple purposes i.e. shared count, reference count and noshare flag. Due to its conflated nature, it can not be used for reference counting by other subsystems.This patch decoupled the reference counting from shared count and noshare flag. The new field '
shared_count' represents the shared count and noshare flag while 'refcount' is converted into a real reference counter.
Quoted 7 lines
The reference counting is only implemented for root
kmem_cachesfor simplicity. The reference of a rootkmem_cacheis elevated on sharing or while its memcgkmem_cachecreation or deactivation request is in the fly and thus it is made sure that the rootkmem_cacheis not destroyed in the middle. As the reference ofkmem_cacheis elevated on sharing, the 'shared_count' does not need any locking protection as at worst it can be out-dated for a small window which is tolerable.
I wonder if we could fix this problem without introducing reference counting for kmem caches (which seems a bit of an overkill to me TBO), e.g. by flushing memcg_kmem_cache_wq before root cache destruction?
Quoted 41 lines · Vladimir Davydov
Quoted 32 lines · Shakeel Butt
The memcg kmem cache creation and deactivation (SLUB only) is asynchronous. If a root kmem cache is destroyed whose memcg cache is in the process of creation or deactivation, the kernel may crash.
Example of one such crash:
general protection fault: 0000 [#1] SMP PTI CPU: 1 PID: 1721 Comm: kworker/14:1 Not tainted 4.17.0-smp ... Workqueue: memcg_kmem_cache kmemcg_deactivate_workfn RIP: 0010:has_cpu_slab ... Call Trace: ? on_each_cpu_cond __kmem_cache_shrink kmemcg_cache_deact_after_rcu kmemcg_deactivate_workfn process_one_work worker_thread kthread ret_from_fork+0x35/0x40This issue is due to the lack of real reference counting for the root
kmem_caches. Currentlykmem_cachedoes have a field named refcount which has been used for multiple purposes i.e. shared count, reference count and noshare flag. Due to its conflated nature, it can not be used for reference counting by other subsystems.This patch decoupled the reference counting from shared count and noshare flag. The new field '
shared_count' represents the shared count and noshare flag while 'refcount' is converted into a real reference counter.Quoted 7 lines
The reference counting is only implemented for root
kmem_cachesfor simplicity. The reference of a rootkmem_cacheis elevated on sharing or while its memcgkmem_cachecreation or deactivation request is in the fly and thus it is made sure that the rootkmem_cacheis not destroyed in the middle. As the reference ofkmem_cacheis elevated on sharing, the 'shared_count' does not need any locking protection as at worst it can be out-dated for a small window which is tolerable.
Quoted 3 lines
I wonder if we could fix this problem without introducing reference counting for kmem caches (which seems a bit of an overkill to me TBO), e.g. by flushing
memcg_kmem_cache_wqbefore root cache destruction?
Thanks I will look into workqueue flushing.
Quoted 11 lines · Vladimir Davydov
Quoted 7 lines
The reference counting is only implemented for root
kmem_cachesfor simplicity. The reference of a rootkmem_cacheis elevated on sharing or while its memcgkmem_cachecreation or deactivation request is in the fly and thus it is made sure that the rootkmem_cacheis not destroyed in the middle. As the reference ofkmem_cacheis elevated on sharing, the 'shared_count' does not need any locking protection as at worst it can be out-dated for a small window which is tolerable.I wonder if we could fix this problem without introducing reference counting for kmem caches (which seems a bit of an overkill to me TBO), e.g. by flushing
memcg_kmem_cache_wqbefore root cache destruction?
Would prefer that too but the whole memcg handling is something of a mystery to me.