]> git.baikalelectronics.ru Git - kernel.git/commitdiff
blk-mq: protect q->elevator by ->sysfs_lock in blk_mq_elv_switch_none
authorMing Lei <ming.lei@redhat.com>
Thu, 16 Jun 2022 01:43:59 +0000 (09:43 +0800)
committerJens Axboe <axboe@kernel.dk>
Thu, 16 Jun 2022 20:45:15 +0000 (14:45 -0600)
elevator can be tore down by sysfs switch interface or disk release, so
hold ->sysfs_lock before referring to q->elevator, then potential
use-after-free can be avoided.

Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
Link: https://lore.kernel.org/r/20220616014401.817001-2-ming.lei@redhat.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
block/blk-mq.c

index 26a7f802d7eecbfe713f4bceaaeeec69c188eb10..c13d03b2e17c6a78151eb4160536985808004475 100644 (file)
@@ -4440,12 +4440,14 @@ static bool blk_mq_elv_switch_none(struct list_head *head,
        if (!qe)
                return false;
 
+       /* q->elevator needs protection from ->sysfs_lock */
+       mutex_lock(&q->sysfs_lock);
+
        INIT_LIST_HEAD(&qe->node);
        qe->q = q;
        qe->type = q->elevator->type;
        list_add(&qe->node, head);
 
-       mutex_lock(&q->sysfs_lock);
        /*
         * After elevator_switch_mq, the previous elevator_queue will be
         * released by elevator_release. The reference of the io scheduler