Skip to content

Commit 85672ca

Browse files
Ming Leiaxboe
Ming Lei
authored andcommitted
block: avoid to reuse hctx not removed from cpuhp callback list
If the 'hctx' isn't removed from cpuhp callback list, we can't reuse it, otherwise use-after-free may be triggered. Reported-by: kernel test robot <[email protected]> Closes: https://lore.kernel.org/oe-lkp/[email protected] Tested-by: kernel test robot <[email protected]> Fixes: 22465bb ("blk-mq: move cpuhp callback registering out of q->sysfs_lock") Signed-off-by: Ming Lei <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 224749b commit 85672ca

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

block/blk-mq.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4412,6 +4412,15 @@ struct gendisk *blk_mq_alloc_disk_for_queue(struct request_queue *q,
44124412
}
44134413
EXPORT_SYMBOL(blk_mq_alloc_disk_for_queue);
44144414

4415+
/*
4416+
* Only hctx removed from cpuhp list can be reused
4417+
*/
4418+
static bool blk_mq_hctx_is_reusable(struct blk_mq_hw_ctx *hctx)
4419+
{
4420+
return hlist_unhashed(&hctx->cpuhp_online) &&
4421+
hlist_unhashed(&hctx->cpuhp_dead);
4422+
}
4423+
44154424
static struct blk_mq_hw_ctx *blk_mq_alloc_and_init_hctx(
44164425
struct blk_mq_tag_set *set, struct request_queue *q,
44174426
int hctx_idx, int node)
@@ -4421,7 +4430,7 @@ static struct blk_mq_hw_ctx *blk_mq_alloc_and_init_hctx(
44214430
/* reuse dead hctx first */
44224431
spin_lock(&q->unused_hctx_lock);
44234432
list_for_each_entry(tmp, &q->unused_hctx_list, hctx_list) {
4424-
if (tmp->numa_node == node) {
4433+
if (tmp->numa_node == node && blk_mq_hctx_is_reusable(tmp)) {
44254434
hctx = tmp;
44264435
break;
44274436
}

0 commit comments

Comments
 (0)