If bic_to_bfqd() returns NULL, bfq_exit_icq() calls _bfq_exit_icq() without holding bfqd->lock. _bfq_exit_icq() calls bfq_put_stable_ref() and bfq_exit_icq_bfqq(), both of which require bfqd->lock to be held and access bfqq->bfqd. bic_to_bfqd() cannot return NULL in practice: ioc_exit_icq() dereferences q->elevator before calling .exit_icq(), elevator_exit() calls ioc_clear_queue() before blk_mq_exit_sched(), and bfq_exit_queue() never clears elevator_data. Replace the unreachable else branch and its stale comment with WARN_ON_ONCE(!bfqd). This change prepares for folding _bfq_exit_icq() into bfq_exit_icq() and annotating bfq_exit_icq_bfqq() with __must_hold(&bfqd->lock). Signed-off-by: Bart Van Assche --- block/bfq-iosched.c | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/block/bfq-iosched.c b/block/bfq-iosched.c index b21ddba30df1..d9db66b0b650 100644 --- a/block/bfq-iosched.c +++ b/block/bfq-iosched.c @@ -5481,22 +5481,12 @@ static void bfq_exit_icq(struct io_cq *icq) struct bfq_data *bfqd = bic_to_bfqd(bic); unsigned long flags; - /* - * If bfqd and thus bfqd->num_actuators is not available any - * longer, then cycle over all possible per-actuator bfqqs in - * next loop. We rely on bic being zeroed on creation, and - * therefore on its unused per-actuator fields being NULL. - * - * bfqd is NULL if scheduler already exited, and in that case - * this is the last time these queues are accessed. - */ - if (bfqd) { - spin_lock_irqsave(&bfqd->lock, flags); - _bfq_exit_icq(bic, bfqd->num_actuators); - spin_unlock_irqrestore(&bfqd->lock, flags); - } else { - _bfq_exit_icq(bic, BFQ_MAX_ACTUATORS); - } + if (WARN_ON_ONCE(!bfqd)) + return; + + spin_lock_irqsave(&bfqd->lock, flags); + _bfq_exit_icq(bic, bfqd->num_actuators); + spin_unlock_irqrestore(&bfqd->lock, flags); } /*