From: Leon Chen <leonchen.oss@139.com>
To: guazhang@redhat.com, ming.lei@redhat.com, axboe@kernel.dk,
stable@vger.kernel.org
Subject: [PATCH 6.1.y] blk-mq: fix NULL dereference on q->elevator in blk_mq_elv_switch_none
Date: Mon, 27 Apr 2026 16:20:57 +0800 [thread overview]
Message-ID: <20260427082057.9619-1-leonchen.oss@139.com> (raw)
From: Ming Lei <ming.lei@redhat.com>
[ 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: Leon Chen <leonchen.oss@139.com>
---
block/blk-mq.c | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)
diff --git a/block/blk-mq.c b/block/blk-mq.c
index f480b6ddba5e..8a9d9e3db166 100644
--- a/block/blk-mq.c
+++ b/block/blk-mq.c
@@ -4732,9 +4732,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;
@@ -4742,6 +4739,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;
@@ -4756,6 +4759,7 @@ static bool blk_mq_elv_switch_none(struct list_head *head,
*/
__module_get(qe->type->elevator_owner);
elevator_switch(q, NULL);
+unlock:
mutex_unlock(&q->sysfs_lock);
return true;
--
2.35.3
reply other threads:[~2026-04-27 8:21 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260427082057.9619-1-leonchen.oss@139.com \
--to=leonchen.oss@139.com \
--cc=axboe@kernel.dk \
--cc=guazhang@redhat.com \
--cc=ming.lei@redhat.com \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox