Skip to content

Commit 0a9a25c

Browse files
Ming Leiaxboe
authored andcommitted
block: let blkcg_gq grab request queue's refcnt
In the whole lifetime of blkcg_gq instance, ->q will be referred, such as, ->pd_free_fn() is called in blkg_free, and throtl_pd_free() still may touch the request queue via &tg->service_queue.pending_timer which is handled by throtl_pending_timer_fn(), so it is reasonable to grab request queue's refcnt by blkcg_gq instance. Previously blkcg_exit_queue() is called from blk_release_queue, and it is hard to avoid the use-after-free. But recently commit 1059699 ("block: move blkcg initialization/destroy into disk allocation/release handler") is merged to for-5.18/block, it becomes simple to fix the issue by simply grabbing request queue's refcnt. Reported-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Ming Lei <ming.lei@redhat.com> Link: https://lore.kernel.org/r/20220318130144.1066064-3-ming.lei@redhat.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent ee37edd commit 0a9a25c

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

block/blk-cgroup.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ static void blkg_free(struct blkcg_gq *blkg)
8282
if (blkg->pd[i])
8383
blkcg_policy[i]->pd_free_fn(blkg->pd[i]);
8484

85+
if (blkg->q)
86+
blk_put_queue(blkg->q);
8587
free_percpu(blkg->iostat_cpu);
8688
percpu_ref_exit(&blkg->refcnt);
8789
kfree(blkg);
@@ -167,6 +169,9 @@ static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, struct request_queue *q,
167169
if (!blkg->iostat_cpu)
168170
goto err_free;
169171

172+
if (!blk_get_queue(q))
173+
goto err_free;
174+
170175
blkg->q = q;
171176
INIT_LIST_HEAD(&blkg->q_node);
172177
spin_lock_init(&blkg->async_bio_lock);

0 commit comments

Comments
 (0)