block: fix lock dependency between percpu alloc lock and elevator lock
PATCH 1/1 block: fix lock dependency between percpu alloc lock and elevator lock
Recent lockdep reports [1] have indicated a potential deadlock arising from the dependency between the percpu allocator lock and the elevaor lock. This issue can be mitigated by ensuring that elevator/sched tags allocation and release occur outside the eleavtor lock. Moreover, we also don't require queue remains frozen while we allocate/release sched tags. So this patch addresses this problem by moving the allocation and de- allocation of elevator sched tags outside the elevator switch path that is protected by the ->freeze_lock and ->elevator_lock. Specifically, new elevator sched tags are now allocated before switching the elevator and outside the freeze section and elevator lock section. The old elevator's sched tags are then freed after the elevator lock is released and queue is unfrozen.
To support this, the elv_change_ctx structure is extended to hold relevant data needed for allocation and deferred release of sched tags during elevator switching. With these changes, all sched tag allocations and releases are performed outside both ->freeze_lock and ->elevator_lock, preventing the lock ordering issue when elv_iosched_store is triggered via sysfs.
[1] https://lore.kernel.org/all/[email protected]/
--- block/blk-mq-sched.c | 226 +++++++++++++++++++++++++++++-------------- block/blk-mq-sched.h | 33 ++++++- block/blk-mq.c | 2 +- block/blk.h | 2 +- block/elevator.c | 110 +++++++++++++-------- 5 files changed, 255 insertions(+), 118 deletions(-)
block/blk-mq-sched.c +152 -74
@@ -374,61 +374,51 @@ bool blk_mq_sched_try_insert_merge(struct request_queue *q, struct request *rq, }EXPORT_SYMBOL_GPL (blk_mq_sched_try_insert_merge); -static int blk_mq_sched_alloc_map_and_rqs (struct request_queue *q, -struct blk_mq_hw_ctx *hctx, -unsigned int hctx_idx) +static int blk_mq_init_sched_tags (struct request_queue *q, +struct blk_mq_hw_ctx *hctx, +unsigned int hctx_idx, +struct elv_data *elv) {if (blk_mq_is_shared_tags (q->tag_set->flags)) { hctx->sched_tags = q->sched_shared_tags;return 0 ; } +/* +* We allocated the sched tags before initiating the elevator switch, +* prior to acquiring ->elevator_lock, and stored them in the elevator +* context. So assign those pre-allocated tags now. +*/ + hctx->sched_tags = elv->tags[hctx_idx]; - hctx->sched_tags =blk_mq_alloc_map_and_rqs (q->tag_set, hctx_idx, - q->nr_requests); - -if (!hctx->sched_tags) -return -ENOMEM ;return 0 ; } -static void blk_mq_exit_sched_shared_tags (struct request_queue *queue) -{ -blk_mq_free_rq_map (queue->sched_shared_tags); - queue->sched_shared_tags =NULL ; -} - -/* called in queue's release handler, tagset has gone away */ -static void blk_mq_sched_tags_teardown (struct request_queue *q,unsigned int flags) +/* +* Called in queue's release handler, tagset has gone away. Sched tags +* are freed later when we finish switching the elevator and release +* the ->elevator_lock and ->frezze_lock. +*/ +static void blk_mq_sched_tags_release (struct request_queue *q,unsigned int flags) {struct blk_mq_hw_ctx *hctx;unsigned long i; -queue_for_each_hw_ctx (q, hctx, i) { -if (hctx->sched_tags) { -if (!blk_mq_is_shared_tags (flags)) -blk_mq_free_rq_map (hctx->sched_tags); - hctx->sched_tags =NULL ; - } - } +queue_for_each_hw_ctx (q, hctx, i) + hctx->sched_tags =NULL ;if (blk_mq_is_shared_tags (flags)) -blk_mq_exit_sched_shared_tags (q); + q->sched_shared_tags =NULL ; } - -static int blk_mq_init_sched_shared_tags (struct request_queue *queue) +static int blk_mq_init_sched_shared_tags (struct request_queue *queue, +struct elv_data *elv) { -struct blk_mq_tag_set *set = queue->tag_set; -/* -* Set initial depth at max so that we don't need to reallocate for -* updating nr_requests. +* We allocated the sched shared tags before initiating the elevator +* switch, prior to acquiring ->elevator_lock and ->freeze_lock, and +* stored them in the elevator context. So assign those pre-allocated +* tags now. */ - queue->sched_shared_tags =blk_mq_alloc_map_and_rqs (set, -BLK_MQ_NO_HCTX_IDX , -MAX_SCHED_RQ ); -if (!queue->sched_shared_tags) -return -ENOMEM ; - + queue->sched_shared_tags = elv->shared_tags;blk_mq_tag_update_sched_shared_tags (queue);return 0 ;
@@ -458,8 +448,108 @@ void blk_mq_sched_unreg_debugfs(struct request_queue *q)mutex_unlock (&q->debugfs_mutex); } +int blk_mq_alloc_sched_tags (struct request_queue *q,struct elv_change_ctx *ctx) +{ +unsigned long i; +struct elv_data *new_e = &ctx->new.elv; +struct elv_data *old_e = &ctx->old.elv; +struct blk_mq_tag_set *set = q->tag_set; +gfp_t gfp =GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY; + +if (strncmp (ctx->new.name,"none" ,4 )) { +/* +* Default to double of smaller one between hw queue_depth and +* 128, since we don't split into sync/async like the old code +* did. Additionally, this is a per-hw queue depth. +*/ + ctx->new.nr_requests = 2 * min_t(unsigned int, set->queue_depth, +BLKDEV_DEFAULT_RQ ); + +if (blk_mq_is_shared_tags (set->flags)) { + + new_e->shared_tags =blk_mq_alloc_map_and_rqs (set, +BLK_MQ_NO_HCTX_IDX , +MAX_SCHED_RQ ); +if (!new_e->shared_tags) +return -ENOMEM ; + +return 0 ; + } + + new_e->tags =kcalloc (new_e->nr_hw_queues, +sizeof (struct blk_mq_tags *), + gfp); +if (!new_e->tags) +return -ENOMEM ; + +for (i =0 ; i < new_e->nr_hw_queues; i++) { + new_e->tags[i] =blk_mq_alloc_map_and_rqs (set, i, + ctx->new.nr_requests); +if (!new_e->tags[i]) + goto out_free_new_e_tags; + } + } + +/* Allocate memory to hold old elevator sched tags. */ + old_e->tags =kcalloc (old_e->nr_hw_queues, +sizeof (struct blk_mq_tags *), + gfp); +if (!old_e->tags) + goto out_free_new_e_tags; + +return 0 ; + +out_free_new_e_tags: +__blk_mq_free_sched_tags (q->tag_set, new_e); +return -ENOMEM ; +} + +void __blk_mq_free_sched_tags (struct blk_mq_tag_set *set, +struct elv_data *elv) +{ +unsigned long i; + +if (elv->shared_tags) { +blk_mq_free_rqs (set, elv->shared_tags,BLK_MQ_NO_HCTX_IDX ); +blk_mq_free_rq_map (elv->shared_tags); + elv->shared_tags =NULL ; + } + +if (!elv->tags) +return ; + +for (i =0 ; i < elv->nr_hw_queues; i++) { +if (elv->tags[i]) { +blk_mq_free_rqs (set, elv->tags[i], i); +blk_mq_free_rq_map (elv->tags[i]); + } + } + +kfree (elv->tags); + elv->tags =NULL ; +} + +void blk_mq_free_sched_tags (struct request_queue *q, +struct elv_change_ctx *ctx) +{ +struct elv_data *new_e = &ctx->new.elv; +struct elv_data *old_e = &ctx->old.elv; + +/* Free old elevator sched tags. */ +__blk_mq_free_sched_tags (q->tag_set, old_e); + +/* Free new elevator sched tags if switching to new elevator failed. */ +if (!new_e->elevator &&strncmp (ctx->new.name,"none" ,4 )) +__blk_mq_free_sched_tags (q->tag_set, new_e); + +/* In any case, free new elevator **tags, which is no more needed. */ +kfree (new_e->tags); + new_e->tags =NULL ; +} +/* caller must have a reference to @e, will grab another one if successful */ -int blk_mq_init_sched (struct request_queue *q,struct elevator_type *e) +int blk_mq_init_sched (struct request_queue *q,struct elevator_type *e, +struct elv_change_ctx *ctx) {unsigned int flags = q->tag_set->flags;struct blk_mq_hw_ctx *hctx;
@@ -467,36 +557,28 @@ int blk_mq_init_sched(struct request_queue *q, struct elevator_type *e)unsigned long i;int ret; -/* -* Default to double of smaller one between hw queue_depth and 128, -* since we don't split into sync/async like the old code did. -* Additionally, this is a per-hw queue depth. -*/ - q->nr_requests = 2 * min_t(unsigned int, q->tag_set->queue_depth, -BLKDEV_DEFAULT_RQ ); + q->nr_requests = ctx->new.nr_requests; -if (blk_mq_is_shared_tags (flags)) { - ret =blk_mq_init_sched_shared_tags (q); -if (ret) -return ret; - } +if (blk_mq_is_shared_tags (flags)) +blk_mq_init_sched_shared_tags (q, &ctx->new.elv); -queue_for_each_hw_ctx (q, hctx, i) { - ret =blk_mq_sched_alloc_map_and_rqs (q, hctx, i); -if (ret) - goto err_free_map_and_rqs; - } +queue_for_each_hw_ctx (q, hctx, i) +blk_mq_init_sched_tags (q, hctx, i, &ctx->new.elv); ret = e->ops.init_sched (q, e);if (ret) - goto err_free_map_and_rqs; + goto out;queue_for_each_hw_ctx (q, hctx, i) {if (e->ops.init_hctx) { ret = e->ops.init_hctx (hctx, i);if (ret) { +/* +* For failed case, sched tags are released +* later when we finish switching elevator and +* release ->elevator_lock and ->freeze_lock. +*/ eq = q->elevator; -blk_mq_sched_free_rqs (q);blk_mq_exit_sched (q, eq);kobject_put (&eq->kobj);return ret;
@@ -505,33 +587,29 @@ int blk_mq_init_sched(struct request_queue *q, struct elevator_type *e) }return 0 ; -err_free_map_and_rqs: -blk_mq_sched_free_rqs (q); -blk_mq_sched_tags_teardown (q, flags); - +out: +/* +* Sched tags are released later when we finish switching elevator +* and release ->elevator_lock and ->freeze_lock. +*/ q->elevator =NULL ;return ret; }/* -* called in either blk_queue_cleanup or elevator_switch, tagset -* is required for freeing requests +* Copy elevator tags while exiting elevator. These tags are freed later +* when switching elevator is complete and we don't hold ->elevator_lock. */ -void blk_mq_sched_free_rqs (struct request_queue *q) +void blk_mq_sched_copy_tags (struct request_queue *q,struct elv_data *elv) {struct blk_mq_hw_ctx *hctx;unsigned long i; -if (blk_mq_is_shared_tags (q->tag_set->flags)) { -blk_mq_free_rqs (q->tag_set, q->sched_shared_tags, -BLK_MQ_NO_HCTX_IDX ); - }else { -queue_for_each_hw_ctx (q, hctx, i) { -if (hctx->sched_tags) -blk_mq_free_rqs (q->tag_set, - hctx->sched_tags, i); - } - } +if (blk_mq_is_shared_tags (q->tag_set->flags)) + elv->shared_tags = q->sched_shared_tags; +else +queue_for_each_hw_ctx (q, hctx, i) + elv->tags[i] = hctx->sched_tags; }void blk_mq_exit_sched (struct request_queue *q,struct elevator_queue *e)
@@ -550,7 +628,7 @@ void blk_mq_exit_sched(struct request_queue *q, struct elevator_queue *e)if (e->type->ops.exit_sched) e->type->ops.exit_sched (e); -blk_mq_sched_tags_teardown (q, flags); +blk_mq_sched_tags_release (q, flags);set_bit (ELEVATOR_FLAG_DYING , &q->elevator->flags); q->elevator =NULL ; }
block/blk-mq-sched.h +31 -2
@@ -7,6 +7,29 @@#define MAX_SCHED_RQ (16 * BLKDEV_DEFAULT_RQ) +struct elv_data { +struct elevator_queue *elevator; +struct blk_mq_tags *shared_tags; +struct blk_mq_tags **tags; +int nr_hw_queues; +}; + +/* Holding context data for changing elevator */ +struct elv_change_ctx { +/* for unregistering/freeing old elevator */ +struct { +struct elv_data elv; + } old; + +/* for registering/allocating new elevator */ +struct { +const char *name; +bool no_uevent; +unsigned long nr_requests; +struct elv_data elv; + } new; +}; +bool blk_mq_sched_try_merge (struct request_queue *q,struct bio *bio,unsigned int nr_segs,struct request **merged_request); bool blk_mq_sched_bio_merge(struct request_queue *q, struct bio *bio,
@@ -18,9 +41,15 @@ void __blk_mq_sched_restart(struct blk_mq_hw_ctx *hctx);void blk_mq_sched_dispatch_requests (struct blk_mq_hw_ctx *hctx); -int blk_mq_init_sched (struct request_queue *q,struct elevator_type *e); +int blk_mq_init_sched (struct request_queue *q,struct elevator_type *e, +struct elv_change_ctx *ctx); +int blk_mq_alloc_sched_tags (struct request_queue *q, +struct elv_change_ctx *ctx); +void __blk_mq_free_sched_tags (struct blk_mq_tag_set *set,struct elv_data *ctx); +void blk_mq_free_sched_tags (struct request_queue *q, +struct elv_change_ctx *ctx);void blk_mq_exit_sched (struct request_queue *q,struct elevator_queue *e); -void blk_mq_sched_free_rqs (struct request_queue *q); +void blk_mq_sched_copy_tags (struct request_queue *q,struct elv_data *ctx);static inline void blk_mq_sched_restart (struct blk_mq_hw_ctx *hctx) {
block/blk-mq.c +1 -1
@@ -5019,7 +5019,7 @@ static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set,/* elv_update_nr_hw_queues() unfreeze queue for us */ list_for_each_entry (q, &set->tag_list, tag_set_list) -elv_update_nr_hw_queues (q); +elv_update_nr_hw_queues (q, prev_nr_hw_queues); reregister:list_for_each_entry (q, &set->tag_list, tag_set_list) {
block/blk.h +1 -1
@@ -321,7 +321,7 @@ bool blk_bio_list_merge(struct request_queue *q, struct list_head *list,bool blk_insert_flush (struct request *rq); -void elv_update_nr_hw_queues (struct request_queue *q); +void elv_update_nr_hw_queues (struct request_queue *q,int prev_nr_hw_queues);void elevator_set_default (struct request_queue *q);void elevator_set_none (struct request_queue *q);
block/elevator.c +70 -40
238 lines not on this page · raw message
Signature · 1 line
2.49.0
Hi Nilay,
Quoted 13 lines · Nilay Shroff
Recent lockdep reports [1] have indicated a potential deadlock arising from the dependency between the percpu allocator lock and the elevaor lock. This issue can be mitigated by ensuring that elevator/sched tags allocation and release occur outside the eleavtor lock. Moreover, we also don't require queue remains frozen while we allocate/release sched tags. So this patch addresses this problem by moving the allocation and de- allocation of elevator sched tags outside the elevator switch path that is protected by the ->freeze_lock and ->elevator_lock. Specifically, new elevator sched tags are now allocated before switching the elevator and outside the freeze section and elevator lock section. The old elevator's sched tags are then freed after the elevator lock is released and queue is unfrozen.
Quoted 6 lines
To support this, the
elv_change_ctxstructure is extended to hold relevant data needed for allocation and deferred release of sched tags during elevator switching. With these changes, all sched tag allocations and releases are performed outside both ->freeze_lock and ->elevator_lock, preventing the lock ordering issue whenelv_iosched_storeis triggered via sysfs.
Not dig into this implementation, will look into later.
I guess it should work by extending elv_change_ctx.
However we have other elevator_queue lifetime issue, that is why ->elevator_lock is used almost everywhere.
Another solution is to move all sched_data into 'struct elevator_queue':
I feel it may be simpler in concept:
- sched data and elevator queue share same lifetime
- kobject_put(&eq->kobj) is already called without holding ->elevator_lock & queue isn't freezed
- replace unnecessary ->elevator_lock by blk_get_elevator()/blk_put_elevator()
But it needs some cleanup/refactor on scheduler interface.
What do you think about the above way?
Thanks,
Ming
Quoted 8 lines · Ming Lei
Not dig into this implementation, will look into later.
I guess it should work by extending
elv_change_ctx.However we have other
elevator_queuelifetime issue, that is why ->elevator_lock is used almost everywhere.Another solution is to move all
sched_datainto 'structelevator_queue':
With "sched_data" you primarily mean the sched tags here, right?
Anyway, getting scheduler owned items out of the request_queue and into the elevator data seems like the right thing to do, but it might indeed be very invasive.
Quoted 18 lines · Ming Lei
Hi Nilay,
On Tue, May 20, 2025 at 04:03:49PM +0530, Nilay Shroff wrote:
Not dig into this implementation, will look into later.
I guess it should work by extending
elv_change_ctx.However we have other
elevator_queuelifetime issue, that is why ->elevator_lock is used almost everywhere.Another solution is to move all
sched_datainto 'structelevator_queue':I feel it may be simpler in concept:
- sched data and elevator queue share same lifetime
Quoted 8 lines
-
kobject_put(&eq->kobj)is already called without holding ->elevator_lock & queue isn't freezed- replace unnecessary ->elevator_lock by blk_get_elevator()/blk_put_elevator()
But it needs some cleanup/refactor on scheduler interface.
What do you think about the above way?
IMO moving scheduler specific data/tags into 'struct elevator_queue' is a good idea. So instead of extending elv_change_ctx for storing/restoring sched_tags (which this propose patch implements), we may now store the sched_tags inside elevator_queue. And as you mentioned, the lifetime of elevator_queue and sched_tags are similar, we can release the sched_tags along with releasing the elevator kobject (which anyways happens outside of ->elevator_lock and ->freeze_lock). Also elevator queue alloc code can be moved out of init_sched before we acquire ->elevator_lock and ->freeze_lock. And, as Christoph mentioned in another email, the changes might be invasive but it seems doable.
I will send another patch with the above changes.
Thanks,
--Nilay