Skip to content

Commit ee18012

Browse files
Ming Leigregkh
authored andcommitted
block: avoid to reuse hctx not removed from cpuhp callback list
commit 85672ca upstream. 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 <oliver.sang@intel.com> Closes: https://lore.kernel.org/oe-lkp/202412172217.b906db7c-lkp@intel.com Tested-by: kernel test robot <oliver.sang@intel.com> Fixes: 22465bb ("blk-mq: move cpuhp callback registering out of q->sysfs_lock") Signed-off-by: Ming Lei <ming.lei@redhat.com> Link: https://lore.kernel.org/r/20241218101617.3275704-3-ming.lei@redhat.com Signed-off-by: Jens Axboe <axboe@kernel.dk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 937fcde commit ee18012

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
@@ -4278,6 +4278,15 @@ struct gendisk *blk_mq_alloc_disk_for_queue(struct request_queue *q,
42784278
}
42794279
EXPORT_SYMBOL(blk_mq_alloc_disk_for_queue);
42804280

4281+
/*
4282+
* Only hctx removed from cpuhp list can be reused
4283+
*/
4284+
static bool blk_mq_hctx_is_reusable(struct blk_mq_hw_ctx *hctx)
4285+
{
4286+
return hlist_unhashed(&hctx->cpuhp_online) &&
4287+
hlist_unhashed(&hctx->cpuhp_dead);
4288+
}
4289+
42814290
static struct blk_mq_hw_ctx *blk_mq_alloc_and_init_hctx(
42824291
struct blk_mq_tag_set *set, struct request_queue *q,
42834292
int hctx_idx, int node)
@@ -4287,7 +4296,7 @@ static struct blk_mq_hw_ctx *blk_mq_alloc_and_init_hctx(
42874296
/* reuse dead hctx first */
42884297
spin_lock(&q->unused_hctx_lock);
42894298
list_for_each_entry(tmp, &q->unused_hctx_list, hctx_list) {
4290-
if (tmp->numa_node == node) {
4299+
if (tmp->numa_node == node && blk_mq_hctx_is_reusable(tmp)) {
42914300
hctx = tmp;
42924301
break;
42934302
}

0 commit comments

Comments
 (0)