Skip to content

Commit 3e94d54

Browse files
tilan7663axboe
authored andcommitted
blk-mq: fix race condition in active queue accounting
If multiple CPUs are sharing the same hardware queue, it can cause leak in the active queue counter tracking when __blk_mq_tag_busy() is executed simultaneously. Fixes: ee78ec1 ("blk-mq: blk_mq_tag_busy is no need to return a value") Signed-off-by: Tian Lan <[email protected]> Reviewed-by: Ming Lei <[email protected]> Reviewed-by: Damien Le Moal <[email protected]> Reviewed-by: John Garry <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 8a2b20a commit 3e94d54

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

block/blk-mq-tag.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,20 @@ void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx)
3939
{
4040
unsigned int users;
4141

42+
/*
43+
* calling test_bit() prior to test_and_set_bit() is intentional,
44+
* it avoids dirtying the cacheline if the queue is already active.
45+
*/
4246
if (blk_mq_is_shared_tags(hctx->flags)) {
4347
struct request_queue *q = hctx->queue;
4448

45-
if (test_bit(QUEUE_FLAG_HCTX_ACTIVE, &q->queue_flags))
49+
if (test_bit(QUEUE_FLAG_HCTX_ACTIVE, &q->queue_flags) ||
50+
test_and_set_bit(QUEUE_FLAG_HCTX_ACTIVE, &q->queue_flags))
4651
return;
47-
set_bit(QUEUE_FLAG_HCTX_ACTIVE, &q->queue_flags);
4852
} else {
49-
if (test_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
53+
if (test_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state) ||
54+
test_and_set_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
5055
return;
51-
set_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state);
5256
}
5357

5458
users = atomic_inc_return(&hctx->tags->active_queues);

0 commit comments

Comments
 (0)