* [PATCH 6.1.y] blk-mq: fix NULL dereference on q->elevator in blk_mq_elv_switch_none
@ 2026-04-27 8:20 Leon Chen
0 siblings, 0 replies; only message in thread
From: Leon Chen @ 2026-04-27 8:20 UTC (permalink / raw)
To: guazhang, ming.lei, axboe, stable
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
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2026-04-27 8:21 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-27 8:20 [PATCH 6.1.y] blk-mq: fix NULL dereference on q->elevator in blk_mq_elv_switch_none Leon Chen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox