diff options
author | Ming Lei <ming.lei@redhat.com> | 2023-06-16 21:23:54 +0800 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2023-07-27 08:56:45 +0200 |
commit | 3e977386521b71471e66ec2ba82efdfcc456adf2 (patch) | |
tree | 85d857159e7531f8406c3b011beb7218f7571104 /block | |
parent | 4cc664e59bf2553771e4c9e90f758f7434cfdc22 (diff) | |
download | linux-stable-3e977386521b71471e66ec2ba82efdfcc456adf2.tar.gz linux-stable-3e977386521b71471e66ec2ba82efdfcc456adf2.tar.bz2 linux-stable-3e977386521b71471e66ec2ba82efdfcc456adf2.zip |
blk-mq: fix NULL dereference on q->elevator in blk_mq_elv_switch_none
[ Upstream commit 245165658e1c9f95c0fecfe02b9b1ebd30a1198a ]
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>
Signed-off-by: Sasha Levin <sashal@kernel.org>
Diffstat (limited to 'block')
-rw-r--r-- | block/blk-mq.c | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/block/blk-mq.c b/block/blk-mq.c index b9f454613989..73ed8ccb09ce 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -4617,9 +4617,6 @@ static bool blk_mq_elv_switch_none(struct list_head *head, { struct blk_mq_qe_pair *qe; - if (!q->elevator) - return true; - qe = kmalloc(sizeof(*qe), GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY); if (!qe) return false; @@ -4627,6 +4624,12 @@ static bool blk_mq_elv_switch_none(struct list_head *head, /* q->elevator needs protection from ->sysfs_lock */ mutex_lock(&q->sysfs_lock); + /* the check has to be done with holding sysfs_lock */ + if (!q->elevator) { + kfree(qe); + goto unlock; + } + INIT_LIST_HEAD(&qe->node); qe->q = q; qe->type = q->elevator->type; @@ -4634,6 +4637,7 @@ static bool blk_mq_elv_switch_none(struct list_head *head, __elevator_get(qe->type); list_add(&qe->node, head); elevator_disable(q); +unlock: mutex_unlock(&q->sysfs_lock); return true; |