ioc_cost_model_write() freezes the queue while holding rq_qos_mutex, which blkg_conf_open_bdev() acquired first. Every other path takes rq_qos_mutex only after the queue is frozen: ioc_qos_write() releases and re-acquires the mutex around blk_mq_freeze_queue(), and rq_qos_add() documents that its caller holds the mutex with the queue frozen. Waiting for in-flight IO while holding rq_qos_mutex breaks that rule, and on the first write to a queue without iocost the path even calls blk_iocost_init() -> rq_qos_add() -> blk_mq_freeze_queue() while nothing has frozen the queue yet. Mirror the release-freeze-reacquire sequence of ioc_qos_write(). The unfreeze moves to the close_bdev label so the error exits after the freeze don't leave the queue frozen, and it runs before blkg_conf_close_bdev() drops the bdev reference, while the queue is still pinned. Fixes: 2b2da2f6dc80 ("blk-iocost: prevent configuration update concurrent with io throttling") Cc: stable@vger.kernel.org Signed-off-by: Guixin Liu --- block/blk-iocost.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/block/blk-iocost.c b/block/blk-iocost.c index 1c1025157d8e..8e06b83691aa 100644 --- a/block/blk-iocost.c +++ b/block/blk-iocost.c @@ -3589,6 +3589,15 @@ static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input, ret = blkg_conf_open_bdev(&ctx); if (ret) return ret; + /* + * Nothing is protecting QoS state yet, so release q->rq_qos_mutex, + * which was first acquired in blkg_conf_open_bdev, and re-acquire it + * after freezing the queue to maintain the correct locking order. + */ + mutex_unlock(&ctx.bdev->bd_queue->rq_qos_mutex); + + memflags = blk_mq_freeze_queue(ctx.bdev->bd_queue); + mutex_lock(&ctx.bdev->bd_queue->rq_qos_mutex); body = ctx.body; q = bdev_get_queue(ctx.bdev); @@ -3605,7 +3614,6 @@ static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input, ioc = q_to_ioc(q); } - memflags = blk_mq_freeze_queue(q); blk_mq_quiesce_queue(q); spin_lock_irq(&ioc->lock); @@ -3663,9 +3671,9 @@ static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input, spin_unlock_irq(&ioc->lock); blk_mq_unquiesce_queue(q); - blk_mq_unfreeze_queue(q, memflags); close_bdev: + blk_mq_unfreeze_queue(q, memflags); blkg_conf_close_bdev(&ctx); return ret ?: nbytes; } -- 2.43.7