Skip to contentSkip to conversation

blk-mq: fix NULL dereference on q->elevator in blk_mq_elv_switch_none

PATCH Applied Ming Lei 2 participants 2 messages Jun 16, 2023 Block layer

lore
Share & tools
lore

Ming Lei

author
#0 lore raw

PATCH 1/1 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.

---
 block/blk-mq.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
block/blk-mq.c +7 -3
index 850bfb844ed2..9516f65a50ea 100644
@@ -4608,9 +4608,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;
@@ -4618,6 +4615,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;
@@ -4625,6 +4628,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;
Signature · 1 line
2.40.1
maintainer Applied
#1 lore raw
Quoted 6 lines · Ming Lei

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.

Applied, thanks!

[1/1] blk-mq: fix NULL dereference on q->elevator in blk_mq_elv_switch_none

      commit: 245165658e1c9f95c0fecfe02b9b1ebd30a1198a

Best regards,

Signature · 1 line
Jens Axboe