mm: fix race between kmem_cache destroy, create and deactivate
Subsystems (4)
PATCH 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 reference counting for the root kmem_caches. There exist a refcount in kmem_cache but it is actually a count of aliases i.e. number of kmem_caches merged together.
This patch make alias count explicit and adds reference counting to the root kmem_caches. The reference of a root kmem cache is elevated on merge and while its memcg kmem_cache is in the process of creation or deactivation.
--- include/linux/slab.h | 2 + include/linux/slab_def.h | 3 +- include/linux/slub_def.h | 3 +- mm/memcontrol.c | 7 ++++ mm/slab.c | 4 +- mm/slab.h | 5 ++- mm/slab_common.c | 84 ++++++++++++++++++++++++++++++---------- mm/slub.c | 14 ++++--- 8 files changed, 90 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 +2 -1
@@ -41,7 +41,8 @@ struct kmem_cache {/* 4) cache creation/removal */ const char *name;struct list_head list; -int refcount; +refcount_t refcount; +int alias_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 slab cache destroy */ +int alias_count;/* Number of root kmem caches merged */ 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->alias_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;/* Use counter */ +int alias_count;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 +64 -20
149 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->alias_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->alias_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->alias_count <0 ?0 : s->alias_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->alias_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->alias_count >1 )return -EINVAL ; s->flags &= ~SLAB_FAILSLAB ;
Signature · 1 line
2.17.0.441.gb46fe60e1d-goog
Andrew Morton
maintainerQuoted 21 lines · On Mon, 21 May 2018 10:41:16 -0700 Shakeel Butt <[email protected]> wrote:
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
Quoted 9 lines
This issue is due to the lack of reference counting for the root
kmem_caches. There exist a refcount inkmem_cachebut it is actually a count of aliases i.e. number ofkmem_cachesmerged together.This patch make alias count explicit and adds reference counting to the root
kmem_caches. The reference of a root kmem cache is elevated on merge and while its memcgkmem_cacheis in the process of creation or deactivation.
The patch seems depressingly complex.
And a bit underdocumented...
Quoted 16 lines
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 +0
-0
include/linux/slab_def.h struct kmem_cache { · line 45
Quoted 6 lines
include/linux/slab_def.h +2
-1@@ -41,7 +41,8 @@ struct kmem_cache {/* 4) cache creation/removal */ const char *name;struct list_head list; -int refcount; +refcount_t refcount; +int alias_count;
The semantic meaning of these two? What locking protects alias_count?
Quoted 8 lines
include/linux/slab_def.h +0
-0int object_size;int align;include/linux/slub_def.h +0
-0
include/linux/slub_def.h struct kmem_cache { · line 101
Quoted 6 lines
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 slab cache destroy */ +int alias_count;/* Number of root kmem caches merged */
"merged" what with what in what manner?
Quoted 9 lines
include/linux/slub_def.h +0
-0void (*ctor)(void *);unsigned int inuse;/* Offset to metadata */ unsigned int align;/* Alignment */
mm/slab.h struct kmem_cache { · line 29
Quoted 6 lines
mm/slab.h +2
-1@@ -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;/* Use counter */ +int alias_count;
Semantic meaning/usage of alias_count? Locking for it?
Quoted 16 lines
mm/slab.h +10
-0void (*ctor)(void *);/* Called on object slot creation */ struct list_head list;/* List of all slab caches on the system */ }; +bool kmem_cache_tryget (struct kmem_cache *s) +{ +if (is_root_cache (s)) +return refcount_inc_not_zero (&s->refcount); +return false; +} + +void kmem_cache_put (struct kmem_cache *s) +{ + if (is_root_cache(s) &&
mm/slab.h struct kmem_cache {
Quoted 10 lines
mm/slab.h +10
-0+ refcount_dec_and_test(&s->refcount)) +__kmem_cache_destroy (s, true); +} + +void kmem_cache_put_locked (struct kmem_cache *s) +{ +if (is_root_cache (s) && +refcount_dec_and_test (&s->refcount)) +__kmem_cache_destroy (s, false); +}
Some covering documentation for the above would be useful. Why do they exist, why do they only operate on the root cache? etc.
Quoted 3 lines
...
Quoted 1 line · Andrew Morton
On Mon, 21 May 2018 10:41:16 -0700 Shakeel Butt <[email protected]>
Quoted 31 lines · wrote:
Quoted 21 lines
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/0x40Quoted 9 lines
This issue is due to the lack of reference counting for the root
kmem_caches. There exist a refcount inkmem_cachebut it is actually a count of aliases i.e. number ofkmem_cachesmerged together.This patch make alias count explicit and adds reference counting to the root
kmem_caches. The reference of a root kmem cache is elevated on merge and while its memcgkmem_cacheis in the process of creation or deactivation.
Quoted 3 lines
The patch seems depressingly complex.
And a bit underdocumented...
I will add more documentation to the code.
Quoted 9 lines
Quoted 9 lines
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);
Quoted 15 lines
include/linux/slab_def.h struct kmem_cache { · line 45
Quoted 13 lines
include/linux/slab.h +0
-0/** * kmalloc_array - allocate memory for an array. include/linux/slab_def.h +2
-1@@ -41,7 +41,8 @@ struct kmem_cache {/* 4) cache creation/removal */ const char *name;struct list_head list; -int refcount; +refcount_t refcount; +int alias_count;The semantic meaning of these two? What locking protects
alias_count?
SLAB and SLUB allow reusing existing root kmem caches. The alias_count of a kmem cache tells the number of times this kmem cache is reused (maybe shared_count or reused_count are better names). Basically if there were 5 root kmem cache creation request and suppose SLAB/SLUB decide to reuse the first kmem cache created for next 4 requests then this count will be 5 and all 5 will be pointing to the same kmem_cache object.
Before this patch, alias_count (previously named refcount) was modified only within slab_mutex but can be read outside. It was conflated into multiple things like shared count, reference count and unmergeable flag (if -ve). This patch decouples the reference counting from this field and there is no need to protect alias_count with locks.
Quoted 16 lines
Quoted 8 lines
include/linux/slab_def.h +0
-0int object_size;int align;include/linux/slub_def.h +0
-0include/linux/slub_def.h struct kmem_cache { · line 101
Quoted 6 lines
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 slab cache destroy */ +int alias_count;/* Number of root kmem caches merged */ "merged" what with what in what manner?
shared or reused might be better words here.
Quoted 6 lines
Quoted 6 lines
include/linux/slub_def.h +0
-0void (*ctor)(void *);unsigned int inuse;/* Offset to metadata */ unsigned int align;/* Alignment */
Quoted 11 lines
mm/slab.h struct kmem_cache { · line 29
Quoted 9 lines
mm/slab.h +2
-1@@ -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;/* Use counter */ +int alias_count;Semantic meaning/usage of
alias_count? Locking for it?
Will add in the next version.
Quoted 2 lines
mm/slab.h struct kmem_cache { · line 31
Quoted 2 lines
mm/slab.h +0
-0void (*ctor)(void *);/* Called on object slot creation */ struct list_head list;/* List of all slab caches on the system
*/
Quoted 19 lines
Quoted 19 lines
mm/slab.h +15
-0}; +bool kmem_cache_tryget (struct kmem_cache *s) +{ +if (is_root_cache (s)) +return refcount_inc_not_zero (&s->refcount); +return false; +} + +void kmem_cache_put (struct kmem_cache *s) +{ +if (is_root_cache (s) && +refcount_dec_and_test (&s->refcount)) +__kmem_cache_destroy (s, true); +} + +void kmem_cache_put_locked (struct kmem_cache *s)
Quoted 8 lines
mm/slab.h
Quoted 5 lines
mm/slab.h +5
-0+{ +if (is_root_cache (s) && +refcount_dec_and_test (&s->refcount)) +__kmem_cache_destroy (s, false); +}Some covering documentation for the above would be useful. Why do they exist, why do they only operate on the root cache? etc.
Ack.
Quoted 3 lines · Andrew Morton
The patch seems depressingly complex.
And a bit underdocumented...
Maybe separate out the bits that rename refcount to alias_count?
Quoted 4 lines
include/linux/slab_def.h struct kmem_cache { · line 45
Quoted 2 lines
include/linux/slab_def.h +2
-0+refcount_t refcount; +int alias_count;The semantic meaning of these two? What locking protects
alias_count?
slab_mutex
Quoted 4 lines
Quoted 4 lines
include/linux/slab_def.h +0
-0int object_size;int align;
Quoted 13 lines
include/linux/slub_def.h struct kmem_cache { · line 101
Quoted 11 lines
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 slab cache destroy */ +int alias_count;/* Number of root kmem caches merged */ "merged" what with what in what manner?
That is a basic SLUB feature.
kbuild test robot
build botThank you for the patch! Yet something to improve:
Hi Shakeel,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on mmotm/master]
[also build test ERROR on v4.17-rc6 next-20180517]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Shakeel-Butt/mm-fix-race-between-kmem_cache-destroy-create-and-deactivate/20180523-041715 base: git://git.cmpxchg.org/linux-mmotm.git master config: i386-randconfig-x009-201820 (attached as .config) compiler: gcc-7 (Debian 7.3.0-16) 7.3.0 reproduce: # save the attached .config to linux build tree make ARCH=i386
All errors (new ones prefixed by >>):
mm/slub.c: In function '__kmem_cache_alias':
>> mm/slub.c:4251:4: error: implicit declaration of function 'kmem_cache_put_locked'; did you mean 'kmem_cache_init_late'? [-Werror=implicit-function-declaration]
kmem_cache_put_locked(s);
^~~~~~~~~~~~~~~~~~~~~
kmem_cache_init_latecc1: some warnings being treated as errors
vim +4251 mm/slub.c
4226
4227 struct kmem_cache *
4228 __kmem_cache_alias(const char *name, unsigned int size, unsigned int align,
4229 slab_flags_t flags, void (*ctor)(void *))
4230 {
4231 struct kmem_cache *s, *c;
4232
4233 s = find_mergeable(size, align, flags, name, ctor);
4234 if (s && kmem_cache_tryget(s)) {
4235 s->alias_count++;
4236
4237 /*
4238 * Adjust the object sizes so that we clear
4239 * the complete object on kzalloc.
4240 */
4241 s->object_size = max(s->object_size, size);
4242 s->inuse = max(s->inuse, ALIGN(size, sizeof(void *)));
4243
4244 for_each_memcg_cache(c, s) {
4245 c->object_size = s->object_size;
4246 c->inuse = max(c->inuse, ALIGN(size, sizeof(void *)));
4247 }
4248
4249 if (sysfs_slab_alias(s, name)) {
4250 s->alias_count--;
> 4251 kmem_cache_put_locked(s);
4252 s = NULL;
4253 }
4254 }
4255
4256 return s;
4257 }
4258
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
Andrew Morton
maintainerQuoted 14 lines · On Wed, 23 May 2018 05:14:36 +0800 kbuild test robot <[email protected]> wrote:
Thank you for the patch! Yet something to improve:
[auto build test ERROR on mmotm/master]
[also build test ERROR on v4.17-rc6 next-20180517]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]url: https://github.com/0day-ci/linux/commits/Shakeel-Butt/mm-fix-race-between-kmem_cache-destroy-create-and-deactivate/20180523-041715 base: git://git.cmpxchg.org/linux-mmotm.git master config: i386-randconfig-x009-201820 (attached as .config) compiler: gcc-7 (Debian 7.3.0-16) 7.3.0 reproduce: # save the attached .config to linux build tree make ARCH=i386
Quoted 8 lines
All errors (new ones prefixed by >>):
mm/slub.c: In function '__kmem_cache_alias':Quoted 1 line
Quoted 1 line
mm/slub.c:4251:4: error: implicit declaration of function 'kmem_cache_put_locked'; did you mean 'kmem_cache_init_late'? [-Werror=implicit-function-declaration]kmem_cache_put_locked(s); ^~~~~~~~~~~~~~~~~~~~~ kmem_cache_init_latecc1: some warnings being treated as errors
Thanks.
mm/slab.h renamed from mm/slab.h~mm-fix-race-between-kmem_cache-destroy-create-and-deactivate-v2-fix +2 -1
@@ -204,6 +204,8 @@ ssize_t slabinfo_write(struct file *filevoid __kmem_cache_free_bulk (struct kmem_cache *,size_t ,void **);int __kmem_cache_alloc_bulk (struct kmem_cache *,gfp_t ,size_t ,void **); +extern void kmem_cache_put_locked (struct kmem_cache *s); +#if defined(CONFIG_MEMCG ) && !defined(CONFIG_SLOB )/* List of all root caches. */
@@ -296,7 +298,6 @@ extern void slab_init_memcg_params(strucextern 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. */
_
kbuild test robot
build botThank you for the patch! Yet something to improve:
Hi Shakeel,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on mmotm/master]
[also build test ERROR on v4.17-rc6 next-20180517]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Shakeel-Butt/mm-fix-race-between-kmem_cache-destroy-create-and-deactivate/20180523-041715 base: git://git.cmpxchg.org/linux-mmotm.git master config: i386-randconfig-a0-201820 (attached as .config) compiler: gcc-4.9 (Debian 4.9.4-2) 4.9.4 reproduce: # save the attached .config to linux build tree make ARCH=i386
All errors (new ones prefixed by >>):
mm/slub.c: In function '__kmem_cache_alias':
>> mm/slub.c:4251:4: error: implicit declaration of function 'kmem_cache_put_locked' [-Werror=implicit-function-declaration]
kmem_cache_put_locked(s);
^cc1: some warnings being treated as errors
vim +/kmem_cache_put_locked +4251 mm/slub.c
4226
4227 struct kmem_cache *
4228 __kmem_cache_alias(const char *name, unsigned int size, unsigned int align,
4229 slab_flags_t flags, void (*ctor)(void *))
4230 {
4231 struct kmem_cache *s, *c;
4232
4233 s = find_mergeable(size, align, flags, name, ctor);
4234 if (s && kmem_cache_tryget(s)) {
4235 s->alias_count++;
4236
4237 /*
4238 * Adjust the object sizes so that we clear
4239 * the complete object on kzalloc.
4240 */
4241 s->object_size = max(s->object_size, size);
4242 s->inuse = max(s->inuse, ALIGN(size, sizeof(void *)));
4243
4244 for_each_memcg_cache(c, s) {
4245 c->object_size = s->object_size;
4246 c->inuse = max(c->inuse, ALIGN(size, sizeof(void *)));
4247 }
4248
4249 if (sysfs_slab_alias(s, name)) {
4250 s->alias_count--;
> 4251 kmem_cache_put_locked(s);
4252 s = NULL;
4253 }
4254 }
4255
4256 return s;
4257 }
4258
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation