Skip to content

Commit 2451656

Browse files
Ming Leiaxboe
authored andcommitted
blk-mq: fix NULL dereference on q->elevator in blk_mq_elv_switch_none
After grabbing q->sysfs_lock, q->elevator may become NULL because of elevator switch. Fix the NULL dereference on q->elevator by checking it with lock. Reported-by: Guangwu Zhang <guazhang@redhat.com> Signed-off-by: Ming Lei <ming.lei@redhat.com> Link: https://lore.kernel.org/r/20230616132354.415109-1-ming.lei@redhat.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 84bd06c commit 2451656

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

block/blk-mq.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4604,23 +4604,27 @@ static bool blk_mq_elv_switch_none(struct list_head *head,
46044604
{
46054605
struct blk_mq_qe_pair *qe;
46064606

4607-
if (!q->elevator)
4608-
return true;
4609-
46104607
qe = kmalloc(sizeof(*qe), GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY);
46114608
if (!qe)
46124609
return false;
46134610

46144611
/* q->elevator needs protection from ->sysfs_lock */
46154612
mutex_lock(&q->sysfs_lock);
46164613

4614+
/* the check has to be done with holding sysfs_lock */
4615+
if (!q->elevator) {
4616+
kfree(qe);
4617+
goto unlock;
4618+
}
4619+
46174620
INIT_LIST_HEAD(&qe->node);
46184621
qe->q = q;
46194622
qe->type = q->elevator->type;
46204623
/* keep a reference to the elevator module as we'll switch back */
46214624
__elevator_get(qe->type);
46224625
list_add(&qe->node, head);
46234626
elevator_disable(q);
4627+
unlock:
46244628
mutex_unlock(&q->sysfs_lock);
46254629

46264630
return true;

0 commit comments

Comments
 (0)