Skip to content

Commit

Permalink
blk-mq: fix potential io hang by wrong 'wake_batch'
Browse files Browse the repository at this point in the history
In __blk_mq_tag_busy/idle(), updating 'active_queues' and calculating
'wake_batch' is not atomic:

t1:			t2:
_blk_mq_tag_busy	blk_mq_tag_busy
inc active_queues
// assume 1->2
			inc active_queues
			// 2 -> 3
			blk_mq_update_wake_batch
			// calculate based on 3
blk_mq_update_wake_batch
/* calculate based on 2, while active_queues is actually 3. */

Fix this problem by protecting them wih 'tags->lock', this is not a hot
path, so performance should not be concerned. And now that all writers
are inside the lock, switch 'actives_queues' from atomic to unsigned
int.

Fixes: 180dccb ("blk-mq: fix tag_get wait task can't be awakened")
Signed-off-by: Yu Kuai <yukuai3@huawei.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Link: https://lore.kernel.org/r/20230610023043.2559121-1-yukuai1@huaweicloud.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
  • Loading branch information
YuKuai-huawei authored and axboe committed Jun 12, 2023
1 parent 0733ad8 commit 4f1731d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion block/blk-mq-debugfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ static void blk_mq_debugfs_tags_show(struct seq_file *m,
seq_printf(m, "nr_tags=%u\n", tags->nr_tags);
seq_printf(m, "nr_reserved_tags=%u\n", tags->nr_reserved_tags);
seq_printf(m, "active_queues=%d\n",
atomic_read(&tags->active_queues));
READ_ONCE(tags->active_queues));

seq_puts(m, "\nbitmap_tags:\n");
sbitmap_queue_show(&tags->bitmap_tags, m);
Expand Down
15 changes: 10 additions & 5 deletions block/blk-mq-tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ static void blk_mq_update_wake_batch(struct blk_mq_tags *tags,
void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx)
{
unsigned int users;
struct blk_mq_tags *tags = hctx->tags;

if (blk_mq_is_shared_tags(hctx->flags)) {
struct request_queue *q = hctx->queue;
Expand All @@ -51,9 +52,11 @@ void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx)
set_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state);
}

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

blk_mq_update_wake_batch(hctx->tags, users);
spin_lock_irq(&tags->lock);
users = tags->active_queues + 1;
WRITE_ONCE(tags->active_queues, users);
blk_mq_update_wake_batch(tags, users);
spin_unlock_irq(&tags->lock);
}

/*
Expand Down Expand Up @@ -86,9 +89,11 @@ void __blk_mq_tag_idle(struct blk_mq_hw_ctx *hctx)
return;
}

users = atomic_dec_return(&tags->active_queues);

spin_lock_irq(&tags->lock);
users = tags->active_queues - 1;
WRITE_ONCE(tags->active_queues, users);
blk_mq_update_wake_batch(tags, users);
spin_unlock_irq(&tags->lock);

blk_mq_tag_wakeup_all(tags, false);
}
Expand Down
3 changes: 1 addition & 2 deletions block/blk-mq.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,7 @@ static inline bool hctx_may_queue(struct blk_mq_hw_ctx *hctx,
return true;
}

users = atomic_read(&hctx->tags->active_queues);

users = READ_ONCE(hctx->tags->active_queues);
if (!users)
return true;

Expand Down
3 changes: 1 addition & 2 deletions include/linux/blk-mq.h
Original file line number Diff line number Diff line change
Expand Up @@ -739,8 +739,7 @@ struct request *blk_mq_alloc_request_hctx(struct request_queue *q,
struct blk_mq_tags {
unsigned int nr_tags;
unsigned int nr_reserved_tags;

atomic_t active_queues;
unsigned int active_queues;

struct sbitmap_queue bitmap_tags;
struct sbitmap_queue breserved_tags;
Expand Down

0 comments on commit 4f1731d

Please sign in to comment.