blk-mq: fix kobject lifetime issue
Subsystems (2)
cover blk-mq: fix kobject lifetime issue
Hi Jens,
The 1st patch fixes the kobject lifetime issue which is triggerd when DEBUG_KOBJECT_RELEASE is enabled.
The 2nd patch can be thought as one follow-up cleanup.
Ming Lei (2):
blk-mq: not embed .mq_kobj and ctx->kobj into queue instance
blk-mq: alloc q->queue_ctx as normal array
block/blk-mq-sysfs.c | 59 +++++++++++++++++++++++++++++++++++++++----------- block/blk-mq.c | 19 ++++++++++------ block/blk-mq.h | 4 ++-- include/linux/blkdev.h | 4 ++-- 4 files changed, 62 insertions(+), 24 deletions(-)
Signature · 1 line
2.9.5
PATCH 1/2 blk-mq: not embed .mq_kobj and ctx->kobj into queue instance
Even though .mq_kobj, ctx->kobj and q->kobj share same lifetime from block layer's view, actually they don't because userspace may grab one kobject anytime via sysfs, so each kobject's lifetime has to be independent, then the objects(mq_kobj, ctx) which hosts its own kobject have to be allocated dynamically.
This patch fixes kernel panic issue during booting when DEBUG_KOBJECT_RELEASE is enabled.
--- block/blk-mq-sysfs.c | 59 +++++++++++++++++++++++++++++++++++++++----------- block/blk-mq.c | 13 ++++++----- block/blk-mq.h | 4 ++-- include/linux/blkdev.h | 4 ++-- 4 files changed, 58 insertions(+), 22 deletions(-)
block/blk-mq-sysfs.c +46 -13
@@ -13,8 +13,22 @@#include "blk-mq.h" #include "blk-mq-tag.h" +struct blk_mq_kobj { +struct kobject kobj; +}; +static void blk_mq_sysfs_release (struct kobject *kobj) { + struct blk_mq_kobj *mq_kobj = container_of(kobj, struct blk_mq_kobj, + kobj); +kfree (mq_kobj); +} + +static void blk_mq_ctx_sysfs_release (struct kobject *kobj) +{ + struct blk_mq_ctx *ctx = container_of(kobj, struct blk_mq_ctx, kobj); + +kfree (ctx); }static void blk_mq_hw_sysfs_release (struct kobject *kobj)
@@ -213,7 +227,7 @@ static struct kobj_type blk_mq_ktype = {static struct kobj_type blk_mq_ctx_ktype = { .sysfs_ops = &blk_mq_sysfs_ops, .default_attrs = default_ctx_attrs, - .release = blk_mq_sysfs_release, + .release = blk_mq_ctx_sysfs_release, };static struct kobj_type blk_mq_hw_ktype = {
@@ -245,7 +259,7 @@ static int blk_mq_register_hctx(struct blk_mq_hw_ctx *hctx)if (!hctx->nr_ctx)return 0 ; - ret =kobject_add (&hctx->kobj, &q->mq_kobj,"%u" , hctx->queue_num); + ret =kobject_add (&hctx->kobj, q->mq_kobj,"%u" , hctx->queue_num);if (ret)return ret;
@@ -268,8 +282,8 @@ void blk_mq_unregister_dev(struct device *dev, struct request_queue *q)queue_for_each_hw_ctx (q, hctx, i)blk_mq_unregister_hctx (hctx); -kobject_uevent (&q->mq_kobj,KOBJ_REMOVE ); -kobject_del (&q->mq_kobj); +kobject_uevent (q->mq_kobj,KOBJ_REMOVE ); +kobject_del (q->mq_kobj);kobject_put (&dev->kobj); q->mq_sysfs_init_done = false;
@@ -286,23 +300,42 @@ void blk_mq_sysfs_deinit(struct request_queue *q)int cpu;for_each_possible_cpu (cpu) { - ctx =per_cpu_ptr (q->queue_ctx, cpu); + ctx = *per_cpu_ptr (q->queue_ctx, cpu);kobject_put (&ctx->kobj); } -kobject_put (&q->mq_kobj); +kobject_put (q->mq_kobj); } -void blk_mq_sysfs_init (struct request_queue *q) +int blk_mq_sysfs_init (struct request_queue *q) {struct blk_mq_ctx *ctx;int cpu; +struct blk_mq_kobj *mq_kobj; + + mq_kobj =kzalloc (sizeof (struct blk_mq_kobj ),GFP_KERNEL ); +if (!mq_kobj) +return -ENOMEM ; -kobject_init (&q->mq_kobj, &blk_mq_ktype); +kobject_init (&mq_kobj->kobj, &blk_mq_ktype);for_each_possible_cpu (cpu) { - ctx =per_cpu_ptr (q->queue_ctx, cpu); + ctx =kzalloc_node (sizeof (*ctx),GFP_KERNEL ,cpu_to_node (cpu)); +if (!ctx) + goto fail; + *per_cpu_ptr (q->queue_ctx, cpu) = ctx;kobject_init (&ctx->kobj, &blk_mq_ctx_ktype); } + q->mq_kobj = &mq_kobj->kobj; +return 0 ; + + fail: +for_each_possible_cpu (cpu) { + ctx = *per_cpu_ptr (q->queue_ctx, cpu); +if (ctx) +kobject_put (&ctx->kobj); + } +kobject_put (&mq_kobj->kobj); +return -ENOMEM ; }int __blk_mq_register_dev (struct device *dev,struct request_queue *q)
@@ -313,11 +346,11 @@ int __blk_mq_register_dev(struct device *dev, struct request_queue *q)WARN_ON_ONCE (!q->kobj.parent);lockdep_assert_held (&q->sysfs_lock); - ret =kobject_add (&q->mq_kobj,kobject_get (&dev->kobj),"%s" ,"mq" ); + ret =kobject_add (q->mq_kobj,kobject_get (&dev->kobj),"%s" ,"mq" );if (ret <0 ) goto out; -kobject_uevent (&q->mq_kobj,KOBJ_ADD ); +kobject_uevent (q->mq_kobj,KOBJ_ADD );queue_for_each_hw_ctx (q, hctx, i) { ret =blk_mq_register_hctx (hctx);
@@ -334,8 +367,8 @@ int __blk_mq_register_dev(struct device *dev, struct request_queue *q)while (--i >=0 )blk_mq_unregister_hctx (q->queue_hw_ctx[i]); -kobject_uevent (&q->mq_kobj,KOBJ_REMOVE ); -kobject_del (&q->mq_kobj); +kobject_uevent (q->mq_kobj,KOBJ_REMOVE ); +kobject_del (q->mq_kobj);kobject_put (&dev->kobj);return ret; }
block/blk-mq.c +8 -5
@@ -2299,7 +2299,7 @@ static void blk_mq_init_cpu_queues(struct request_queue *q,unsigned int i, j;for_each_possible_cpu (i) { -struct blk_mq_ctx *__ctx =per_cpu_ptr (q->queue_ctx, i); +struct blk_mq_ctx *__ctx = *per_cpu_ptr (q->queue_ctx, i);struct blk_mq_hw_ctx *hctx; __ctx->cpu = i;
@@ -2385,7 +2385,7 @@ static void blk_mq_map_swqueue(struct request_queue *q) set->map[0 ].mq_map[i] =0 ; } - ctx =per_cpu_ptr (q->queue_ctx, i); + ctx = *per_cpu_ptr (q->queue_ctx, i);for (j =0 ; j < set->nr_maps; j++) { hctx =blk_mq_map_queue_type (q, j, i);
@@ -2731,18 +2731,19 @@ struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,if (!q->poll_cb) goto err_exit; - q->queue_ctx = alloc_percpu(struct blk_mq_ctx); + q->queue_ctx = alloc_percpu(struct blk_mq_ctx *);if (!q->queue_ctx) goto err_exit;/* init q->mq_kobj and sw queues' kobjects */ -blk_mq_sysfs_init (q); +if (blk_mq_sysfs_init (q)) + goto err_percpu; q->nr_queues =nr_hw_queues (set); q->queue_hw_ctx =kcalloc_node (q->nr_queues,sizeof (*(q->queue_hw_ctx)),GFP_KERNEL , set->numa_node);if (!q->queue_hw_ctx) - goto err_percpu; + goto err_sys_init;blk_mq_realloc_hw_ctxs (set, q);if (!q->nr_hw_queues)
@@ -2794,6 +2795,8 @@ struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set, err_hctxs:kfree (q->queue_hw_ctx); +err_sys_init: +blk_mq_sysfs_deinit (q); err_percpu:free_percpu (q->queue_ctx); err_exit:
block/blk-mq.h +2 -2
@@ -108,7 +108,7 @@ static inline struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q,/* * sysfs helpers */ -extern void blk_mq_sysfs_init (struct request_queue *q); +extern int blk_mq_sysfs_init (struct request_queue *q);extern void blk_mq_sysfs_deinit (struct request_queue *q);extern int __blk_mq_register_dev (struct device *dev,struct request_queue *q);extern int blk_mq_sysfs_register (struct request_queue *q);
@@ -129,7 +129,7 @@ static inline enum mq_rq_state blk_mq_rq_state(struct request *rq)static inline struct blk_mq_ctx *__blk_mq_get_ctx (struct request_queue *q,unsigned int cpu) { -return per_cpu_ptr (q->queue_ctx, cpu); +return *per_cpu_ptr (q->queue_ctx, cpu); }/*
include/linux/blkdev.h +2 -2
@@ -407,7 +407,7 @@ struct request_queue {const struct blk_mq_ops *mq_ops;/* sw queues */ -struct blk_mq_ctx __percpu *queue_ctx; +struct blk_mq_ctx __percpu **queue_ctx;unsigned int nr_queues;unsigned int queue_depth;
@@ -456,7 +456,7 @@ struct request_queue {/* * mq queue kobject */ -struct kobject mq_kobj; +struct kobject *mq_kobj;#ifdef CONFIG_BLK_DEV_INTEGRITY struct blk_integrity integrity;
Signature · 1 line
2.9.5
PATCH 2/2 blk-mq: alloc q->queue_ctx as normal array
In reply to Ming LeiNow q->queue_ctx is just one read-mostly table for query the 'blk_mq_ctx' instance from one cpu index, it isn't necessary to allocate it as percpu variable. One simple array may be more efficient.
--- block/blk-mq-sysfs.c | 6 +++--- block/blk-mq.c | 12 +++++++----- block/blk-mq.h | 2 +- include/linux/blkdev.h | 2 +- 4 files changed, 12 insertions(+), 10 deletions(-)
block/blk-mq-sysfs.c +3 -3
@@ -300,7 +300,7 @@ void blk_mq_sysfs_deinit(struct request_queue *q)int cpu;for_each_possible_cpu (cpu) { - ctx = *per_cpu_ptr (q->queue_ctx, cpu); + ctx = q->queue_ctx[cpu];kobject_put (&ctx->kobj); }kobject_put (q->mq_kobj);
@@ -322,7 +322,7 @@ int blk_mq_sysfs_init(struct request_queue *q) ctx =kzalloc_node (sizeof (*ctx),GFP_KERNEL ,cpu_to_node (cpu));if (!ctx) goto fail; - *per_cpu_ptr (q->queue_ctx, cpu) = ctx; + q->queue_ctx[cpu] = ctx;kobject_init (&ctx->kobj, &blk_mq_ctx_ktype); } q->mq_kobj = &mq_kobj->kobj;
@@ -330,7 +330,7 @@ int blk_mq_sysfs_init(struct request_queue *q) fail:for_each_possible_cpu (cpu) { - ctx = *per_cpu_ptr (q->queue_ctx, cpu); + ctx = q->queue_ctx[cpu];if (ctx)kobject_put (&ctx->kobj); }
block/blk-mq.c +7 -5
@@ -2299,7 +2299,7 @@ static void blk_mq_init_cpu_queues(struct request_queue *q,unsigned int i, j;for_each_possible_cpu (i) { -struct blk_mq_ctx *__ctx = *per_cpu_ptr (q->queue_ctx, i); +struct blk_mq_ctx *__ctx = q->queue_ctx[i];struct blk_mq_hw_ctx *hctx; __ctx->cpu = i;
@@ -2385,7 +2385,7 @@ static void blk_mq_map_swqueue(struct request_queue *q) set->map[0 ].mq_map[i] =0 ; } - ctx = *per_cpu_ptr (q->queue_ctx, i); + ctx = q->queue_ctx[i];for (j =0 ; j < set->nr_maps; j++) { hctx =blk_mq_map_queue_type (q, j, i);
@@ -2541,7 +2541,7 @@ void blk_mq_release(struct request_queue *q)*/ blk_mq_sysfs_deinit (q); -free_percpu (q->queue_ctx); +kfree (q->queue_ctx); }struct request_queue *blk_mq_init_queue (struct blk_mq_tag_set *set)
@@ -2731,7 +2731,9 @@ struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,if (!q->poll_cb) goto err_exit; - q->queue_ctx = alloc_percpu(struct blk_mq_ctx *); + q->queue_ctx =kmalloc_array_node (nr_cpu_ids, +sizeof (struct blk_mq_ctx *), +GFP_KERNEL , set->numa_node);if (!q->queue_ctx) goto err_exit;
@@ -2798,7 +2800,7 @@ struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set, err_sys_init:blk_mq_sysfs_deinit (q); err_percpu: -free_percpu (q->queue_ctx); +kfree (q->queue_ctx); err_exit: q->mq_ops =NULL ;return ERR_PTR (-ENOMEM );
block/blk-mq.h +1 -1
@@ -129,7 +129,7 @@ static inline enum mq_rq_state blk_mq_rq_state(struct request *rq)static inline struct blk_mq_ctx *__blk_mq_get_ctx (struct request_queue *q,unsigned int cpu) { -return *per_cpu_ptr (q->queue_ctx, cpu); +return q->queue_ctx[cpu]; }/*
include/linux/blkdev.h +1 -1
@@ -407,7 +407,7 @@ struct request_queue {const struct blk_mq_ops *mq_ops;/* sw queues */ -struct blk_mq_ctx __percpu **queue_ctx; +struct blk_mq_ctx **queue_ctx;unsigned int nr_queues;unsigned int queue_depth;
Signature · 1 line
2.9.5
Re: [PATCH 1/2] blk-mq: not embed .mq_kobj and ctx->kobj into queue instance
In reply to Ming LeiOn 11/16/18 11:28 AM, Ming Lei wrote:
...
Quoted 12 lines
block/blk-mq-sysfs.c +9
-0+struct blk_mq_kobj { +struct kobject kobj; +}; +static void blk_mq_sysfs_release (struct kobject *kobj) { + struct blk_mq_kobj *mq_kobj = container_of(kobj, struct blk_mq_kobj, + kobj); +kfree (mq_kobj); +} +
...
Quoted 25 lines
block/blk-mq-sysfs.c +13
-3-void blk_mq_sysfs_init (struct request_queue *q) +int blk_mq_sysfs_init (struct request_queue *q) {struct blk_mq_ctx *ctx;int cpu; +struct blk_mq_kobj *mq_kobj; + + mq_kobj =kzalloc (sizeof (struct blk_mq_kobj ),GFP_KERNEL ); +if (!mq_kobj) +return -ENOMEM ; -kobject_init (&q->mq_kobj, &blk_mq_ktype); +kobject_init (&mq_kobj->kobj, &blk_mq_ktype);for_each_possible_cpu (cpu) { - ctx =per_cpu_ptr (q->queue_ctx, cpu); + ctx =kzalloc_node (sizeof (*ctx),GFP_KERNEL ,cpu_to_node (cpu)); +if (!ctx) + goto fail; + *per_cpu_ptr (q->queue_ctx, cpu) = ctx;kobject_init (&ctx->kobj, &blk_mq_ctx_ktype); } + q->mq_kobj = &mq_kobj->kobj; +return 0 ;
block/blk-mq-sysfs.c void blk_mq_sysfs_deinit(struct request_queue *q) · line 339
Quoted 10 lines
block/blk-mq-sysfs.c +9
-0+ + fail: +for_each_possible_cpu (cpu) { + ctx = *per_cpu_ptr (q->queue_ctx, cpu); +if (ctx) +kobject_put (&ctx->kobj); + } +kobject_put (&mq_kobj->kobj); +return -ENOMEM ; }
blk_mq_kobj looks meaningless, why do we need it, or do I miss something ? And maybe we should allocate ctx in blk_mq_init_allocated_queue.
Thanks
Jianchao
Sasha Levin
maintainerRe: [PATCH 1/2] blk-mq: not embed .mq_kobj and ctx->kobj into queue instance
In reply to Ming LeiQuoted 6 lines · Ming Lei
Even though .mq_kobj,
ctx->kobjandq->kobjshare same lifetime from block layer's view, actually they don't because userspace may grab one kobject anytime via sysfs, so each kobject's lifetime has to be independent, then theobjects(mq_kobj, ctx)which hosts its own kobject have to be allocated dynamically.
Quoted 8 lines
This patch fixes kernel panic issue during booting when
DEBUG_KOBJECT_RELEASEis enabled.Reported-by Guenter Roeck <[email protected]>Cc Guenter Roeck <[email protected]>Cc Greg Kroah-Hartman <[email protected]>Signed-off-by Ming Lei <[email protected]>
What does this patch depend on? It doesn't apply to Linus's tree nor to the block tree.
Also, could you please cc lkml with patches?
--
Thanks,
Sasha
Re: [PATCH 1/2] blk-mq: not embed .mq_kobj and ctx->kobj into queue instance
Quoted 16 lines · Sasha Levin
Quoted 6 lines · Ming Lei
Even though .mq_kobj,
ctx->kobjandq->kobjshare same lifetime from block layer's view, actually they don't because userspace may grab one kobject anytime via sysfs, so each kobject's lifetime has to be independent, then theobjects(mq_kobj, ctx)which hosts its own kobject have to be allocated dynamically.Quoted 8 lines
This patch fixes kernel panic issue during booting when
DEBUG_KOBJECT_RELEASEis enabled.Reported-by Guenter Roeck <[email protected]>Cc Guenter Roeck <[email protected]>Cc Greg Kroah-Hartman <[email protected]>Signed-off-by Ming Lei <[email protected]>
Quoted 4 lines
What does this patch depend on? It doesn't apply to Linus's tree nor to the block tree.
Also, could you please cc lkml with patches?
It depends on for-4.21/block.
thanks,
Ming
Re: [PATCH 1/2] blk-mq: not embed .mq_kobj and ctx->kobj into queue instance
In reply to jianchao.wangQuoted 47 lines · jianchao.wang
On 11/16/18 11:28 AM, Ming Lei wrote:
...Quoted 12 lines
block/blk-mq-sysfs.c +9
-0+struct blk_mq_kobj { +struct kobject kobj; +}; +static void blk_mq_sysfs_release (struct kobject *kobj) { + struct blk_mq_kobj *mq_kobj = container_of(kobj, struct blk_mq_kobj, + kobj); +kfree (mq_kobj); +} +...
Quoted 30 lines
block/blk-mq-sysfs.c +18
-3-void blk_mq_sysfs_init (struct request_queue *q) +int blk_mq_sysfs_init (struct request_queue *q) {struct blk_mq_ctx *ctx;int cpu; +struct blk_mq_kobj *mq_kobj; + + mq_kobj =kzalloc (sizeof (struct blk_mq_kobj ),GFP_KERNEL ); +if (!mq_kobj) +return -ENOMEM ; -kobject_init (&q->mq_kobj, &blk_mq_ktype); +kobject_init (&mq_kobj->kobj, &blk_mq_ktype);for_each_possible_cpu (cpu) { - ctx =per_cpu_ptr (q->queue_ctx, cpu); + ctx =kzalloc_node (sizeof (*ctx),GFP_KERNEL ,cpu_to_node (cpu)); +if (!ctx) + goto fail; + *per_cpu_ptr (q->queue_ctx, cpu) = ctx;kobject_init (&ctx->kobj, &blk_mq_ctx_ktype); } + q->mq_kobj = &mq_kobj->kobj; +return 0 ; + + fail: +for_each_possible_cpu (cpu) { + ctx = *per_cpu_ptr (q->queue_ctx, cpu); +if (ctx)
Quoted 8 lines
block/blk-mq-sysfs.c void blk_mq_sysfs_deinit(struct request_queue *q) · line 339
Quoted 5 lines
block/blk-mq-sysfs.c +4
-0+kobject_put (&ctx->kobj); + } +kobject_put (&mq_kobj->kobj); +return -ENOMEM ; }
blk_mq_kobjlooks meaningless, why do we need it, or do I miss something ?
Right, it should have been allocated directly.
Quoted 1 line
And maybe we should allocate ctx in
blk_mq_init_allocated_queue.
Looks either way is fine.
Thanks,
Ming
Sasha Levin
maintainerRe: [PATCH 1/2] blk-mq: not embed .mq_kobj and ctx->kobj into queue instance
In reply to Ming LeiQuoted 17 lines · Ming Lei
Quoted 16 lines · Sasha Levin
Quoted 14 lines · Ming Lei
Even though .mq_kobj,
ctx->kobjandq->kobjshare same lifetime from block layer's view, actually they don't because userspace may grab one kobject anytime via sysfs, so each kobject's lifetime has to be independent, then theobjects(mq_kobj, ctx)which hosts its own kobject have to be allocated dynamically.This patch fixes kernel panic issue during booting when
DEBUG_KOBJECT_RELEASEis enabled.Reported-by Guenter Roeck <[email protected]>Cc Guenter Roeck <[email protected]>Cc Greg Kroah-Hartman <[email protected]>Signed-off-by Ming Lei <[email protected]>
Quoted 6 lines
Quoted 4 lines
What does this patch depend on? It doesn't apply to Linus's tree nor to the block tree.
Also, could you please cc lkml with patches?
It depends on for-4.21/block.
And in particular on commit 392546aed22 ("blk-mq: separate number of hardware queues from nr_cpu_ids") which isn't tagged for stable nor looks like stable material, so this proposed patch doesn't backport cleanly to any of the stable trees.
--
Thanks,
Sasha