]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
blk-mq: fix null-ptr-deref in blk_mq_free_tags() from error path
authorYu Kuai <yukuai3@huawei.com>
Tue, 23 Sep 2025 07:01:01 +0000 (15:01 +0800)
committerJens Axboe <axboe@kernel.dk>
Tue, 23 Sep 2025 07:35:52 +0000 (01:35 -0600)
blk_mq_free_tags() can be called after blk_mq_init_tags(), while
tags->page_list is still not initialized, causing null-ptr-deref.

Fix this problem by initializing tags->page_list at blk_mq_init_tags(),
meanwhile, also free tags directly from error path because there is no
srcu barrier.

Fixes: ad0d05dbddc1 ("blk-mq: Defer freeing of tags page_list to SRCU callback")
Reported-by: syzbot+5c5d41e80248d610221f@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/all/68d1b079.a70a0220.1b52b.0000.GAE@google.com/
Signed-off-by: Yu Kuai <yukuai3@huawei.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
block/blk-mq-tag.c
block/blk-mq.c

index 271fa005c51e02fa4711e35cb00d2cd0814ac043..c7a4d4b9cc87b938315d40346daa46ad86d346c2 100644 (file)
@@ -566,6 +566,8 @@ struct blk_mq_tags *blk_mq_init_tags(unsigned int total_tags,
        tags->nr_tags = total_tags;
        tags->nr_reserved_tags = reserved_tags;
        spin_lock_init(&tags->lock);
+       INIT_LIST_HEAD(&tags->page_list);
+
        if (bt_alloc(&tags->bitmap_tags, depth, round_robin, node))
                goto out_free_tags;
        if (bt_alloc(&tags->breserved_tags, reserved_tags, round_robin, node))
@@ -603,6 +605,13 @@ void blk_mq_free_tags(struct blk_mq_tag_set *set, struct blk_mq_tags *tags)
 {
        sbitmap_queue_free(&tags->bitmap_tags);
        sbitmap_queue_free(&tags->breserved_tags);
+
+       /* if tags pages is not allocated yet, free tags directly */
+       if (list_empty(&tags->page_list)) {
+               kfree(tags);
+               return;
+       }
+
        call_srcu(&set->tags_srcu, &tags->rcu_head, blk_mq_free_tags_callback);
 }
 
index 4ccf11cadf8c7880965cd9db5e62760b38162c03..09f57941416159524eec14fdeed93f3132eb14b2 100644 (file)
@@ -3582,8 +3582,6 @@ static int blk_mq_alloc_rqs(struct blk_mq_tag_set *set,
        if (node == NUMA_NO_NODE)
                node = set->numa_node;
 
-       INIT_LIST_HEAD(&tags->page_list);
-
        /*
         * rq_size is the size of the request plus driver payload, rounded
         * to the cacheline size