blk-mq: fix race condition in active queue accounting
PATCH 1/1 blk-mq: fix race condition in active queue accounting
From: Tian Lan <[email protected]>
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.
--- block/blk-mq-tag.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
block/blk-mq-tag.c +6 -4
@@ -42,13 +42,15 @@ void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx)if (blk_mq_is_shared_tags (hctx->flags)) {struct request_queue *q = hctx->queue; -if (test_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags)) +if (test_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags) || +test_and_set_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags)) {return ; -set_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags); + } }else { -if (test_bit (BLK_MQ_S_TAG_ACTIVE , &hctx->state)) +if (test_bit (BLK_MQ_S_TAG_ACTIVE , &hctx->state) || +test_and_set_bit (BLK_MQ_S_TAG_ACTIVE , &hctx->state)) {return ; -set_bit (BLK_MQ_S_TAG_ACTIVE , &hctx->state); + } } users =atomic_inc_return (&hctx->tags->active_queues);
Signature · 1 line
2.34.1
Quoted 9 lines · Tian Lan
From: Tian Lan <[email protected]>
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 ee78ec1077d3 ("blk-mq: blk_mq_tag_busy is no need to return a value")Signed-off-by Tian Lan <[email protected]>
Looks fine,
Quoted 17 lines · Tian Lan
From: Tian Lan <[email protected]>
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 ee78ec1077d3 ("blk-mq: blk_mq_tag_busy is no need to return a value")Signed-off-by Tian Lan <[email protected]>--- block/blk-mq-tag.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)block/blk-mq-tag.c +0
-0
block/blk-mq-tag.c void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx) · line 46
Quoted 6 lines
block/blk-mq-tag.c +2
-1@@ -42,13 +42,15 @@ void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx)if (blk_mq_is_shared_tags (hctx->flags)) {struct request_queue *q = hctx->queue; -if (test_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags)) +if (test_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags) || +test_and_set_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags)) {
This is weird. test_and_set_bit() returns the bit old value, so shouldn't this be:
if (test_and_set_bit(QUEUE_FLAG_HCTX_ACTIVE, &q->queue_flags)) return;
?
block/blk-mq-tag.c void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx) · line 53
Quoted 10 lines
block/blk-mq-tag.c +4
-3return ; -set_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags); + } }else { -if (test_bit (BLK_MQ_S_TAG_ACTIVE , &hctx->state)) +if (test_bit (BLK_MQ_S_TAG_ACTIVE , &hctx->state) || +test_and_set_bit (BLK_MQ_S_TAG_ACTIVE , &hctx->state)) {return ; -set_bit (BLK_MQ_S_TAG_ACTIVE , &hctx->state); + }
Same here. And given that this pattern is the same for the if and the else, this entire hunk can likely be simplified.
Quoted 3 lines
block/blk-mq-tag.c +0
-0} users =atomic_inc_return (&hctx->tags->active_queues);
Signature · 2 lines
Damien Le Moal Western Digital Research
Quoted 27 lines · Damien Le Moal
Quoted 17 lines · Tian Lan
From: Tian Lan <[email protected]>
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 ee78ec1077d3 ("blk-mq: blk_mq_tag_busy is no need to return a value")Signed-off-by Tian Lan <[email protected]>--- block/blk-mq-tag.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)block/blk-mq-tag.c +0
-0block/blk-mq-tag.c void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx) · line 46
Quoted 6 lines
block/blk-mq-tag.c +2
-1@@ -42,13 +42,15 @@ void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx)if (blk_mq_is_shared_tags (hctx->flags)) {struct request_queue *q = hctx->queue; -if (test_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags)) +if (test_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags) || +test_and_set_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags)) {This is weird.
test_and_set_bit()returns the bit old value, so shouldn't this be:
Quoted 4 lines
if (test_and_set_bit(QUEUE_FLAG_HCTX_ACTIVE, &q->queue_flags)) return;?
It is one micro optimization since test_and_set_bit is much heavier than test_bit, so test_and_set_bit() is just needed in the 1st time.
Thanks,
Ming
Quoted 28 lines · Ming Lei
Quoted 27 lines · Damien Le Moal
Quoted 17 lines · Tian Lan
From: Tian Lan <[email protected]>
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 ee78ec1077d3 ("blk-mq: blk_mq_tag_busy is no need to return a value")Signed-off-by Tian Lan <[email protected]>--- block/blk-mq-tag.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)block/blk-mq-tag.c +0
-0block/blk-mq-tag.c void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx) · line 46
Quoted 6 lines
block/blk-mq-tag.c +2
-1@@ -42,13 +42,15 @@ void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx)if (blk_mq_is_shared_tags (hctx->flags)) {struct request_queue *q = hctx->queue; -if (test_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags)) +if (test_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags) || +test_and_set_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags)) {This is weird.
test_and_set_bit()returns the bit old value, so shouldn't this be:
Quoted 7 lines
Quoted 4 lines
if (test_and_set_bit(QUEUE_FLAG_HCTX_ACTIVE, &q->queue_flags)) return;?
It is one micro optimization since
test_and_set_bitis much heavier thantest_bit, sotest_and_set_bit()is just needed in the 1st time.
But having the 2 calls test_bit + test_and_set_bit creates a race, doesn't it ? What if another cpu clears the bit between these 2 calls ?
At the very least, the patch should remove the curly braces around that if.
Quoted 4 lines
Thanks,
Ming
Signature · 2 lines
Damien Le Moal Western Digital Research
Jens Axboe
maintainerQuoted 28 lines · Ming Lei
Quoted 27 lines · Damien Le Moal
Quoted 17 lines · Tian Lan
From: Tian Lan <[email protected]>
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 ee78ec1077d3 ("blk-mq: blk_mq_tag_busy is no need to return a value")Signed-off-by Tian Lan <[email protected]>--- block/blk-mq-tag.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)block/blk-mq-tag.c +0
-0block/blk-mq-tag.c void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx) · line 46
Quoted 6 lines
block/blk-mq-tag.c +2
-1@@ -42,13 +42,15 @@ void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx)if (blk_mq_is_shared_tags (hctx->flags)) {struct request_queue *q = hctx->queue; -if (test_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags)) +if (test_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags) || +test_and_set_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags)) {This is weird.
test_and_set_bit()returns the bit old value, so shouldn't this be:
Quoted 7 lines
Quoted 4 lines
if (test_and_set_bit(QUEUE_FLAG_HCTX_ACTIVE, &q->queue_flags)) return;?
It is one micro optimization since
test_and_set_bitis much heavier thantest_bit, sotest_and_set_bit()is just needed in the 1st time.
It's an optimization, but it's certainly not a micro one. If the common case is always hitting that, then test_and_set_bit() will repeatedly dirty that cacheline. And obviously it's useless to do that if the bit is already set. This makes it a pretty nice optimization and definitely outside the realm of "micro optimization" as it can have quite a large impact. I used that in various spots in blk-mq, which I suspect is where the inspiration for this one came too.
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerQuoted 32 lines · Damien Le Moal
Quoted 31 lines · Ming Lei
Quoted 30 lines · Damien Le Moal
Quoted 17 lines · Tian Lan
From: Tian Lan <[email protected]>
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 ee78ec1077d3 ("blk-mq: blk_mq_tag_busy is no need to return a value")Signed-off-by Tian Lan <[email protected]>--- block/blk-mq-tag.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)block/blk-mq-tag.c +0
-0block/blk-mq-tag.c void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx) · line 46
Quoted 6 lines
block/blk-mq-tag.c +2
-1@@ -42,13 +42,15 @@ void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx)if (blk_mq_is_shared_tags (hctx->flags)) {struct request_queue *q = hctx->queue; -if (test_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags)) +if (test_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags) || +test_and_set_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags)) {This is weird.
test_and_set_bit()returns the bit old value, so shouldn't this be:if (test_and_set_bit(QUEUE_FLAG_HCTX_ACTIVE, &q->queue_flags)) return;
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
?
It is one micro optimization since
test_and_set_bitis much heavier thantest_bit, sotest_and_set_bit()is just needed in the 1st time.But having the 2 calls
test_bit+test_and_set_bitcreates a race, doesn't it ? What if another cpu clears the bit between these 2 calls ?
How so? If the bit is already set or you're racing with it being set or cleared, that race already exists before. This simply prevent unnecessary dirtying of that cacheline.
Signature · 1 line
Jens Axboe
PATCH 1/1 blk-mq: fix race condition in active queue accounting
In reply to Ming LeiFrom: Tian Lan <[email protected]>
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.
--- block/blk-mq-tag.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
block/blk-mq-tag.c +4 -4
@@ -42,13 +42,13 @@ void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx)if (blk_mq_is_shared_tags (hctx->flags)) {struct request_queue *q = hctx->queue; -if (test_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags)) +if (test_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags) || +test_and_set_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags))return ; -set_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags); }else { -if (test_bit (BLK_MQ_S_TAG_ACTIVE , &hctx->state)) +if (test_bit (BLK_MQ_S_TAG_ACTIVE , &hctx->state) || +test_and_set_bit (BLK_MQ_S_TAG_ACTIVE , &hctx->state))return ; -set_bit (BLK_MQ_S_TAG_ACTIVE , &hctx->state); } users =atomic_inc_return (&hctx->tags->active_queues);
Signature · 1 line
2.25.1
Quoted 32 lines · Damien Le Moal
Quoted 31 lines · Ming Lei
Quoted 30 lines · Damien Le Moal
Quoted 17 lines · Tian Lan
From: Tian Lan <[email protected]>
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 ee78ec1077d3 ("blk-mq: blk_mq_tag_busy is no need to return a value")Signed-off-by Tian Lan <[email protected]>--- block/blk-mq-tag.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)block/blk-mq-tag.c +0
-0block/blk-mq-tag.c void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx) · line 46
Quoted 6 lines
block/blk-mq-tag.c +2
-1@@ -42,13 +42,15 @@ void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx)if (blk_mq_is_shared_tags (hctx->flags)) {struct request_queue *q = hctx->queue; -if (test_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags)) +if (test_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags) || +test_and_set_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags)) {This is weird.
test_and_set_bit()returns the bit old value, so shouldn't this be:if (test_and_set_bit(QUEUE_FLAG_HCTX_ACTIVE, &q->queue_flags)) return;
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
?
It is one micro optimization since
test_and_set_bitis much heavier thantest_bit, sotest_and_set_bit()is just needed in the 1st time.But having the 2 calls
test_bit+test_and_set_bitcreates a race, doesn't it ? What if another cpu clears the bit between these 2 calls ?
If test_bit() returns 0, there isn't such race since both sides are atomic OP.
If test_bit() returns 1:
1) __blk_mq_tag_busy() vs. __blk_mq_tag_busy()
- both does nothing
2) __blk_mq_tag_busy() vs. __blk_mq_tag_idle()
- hctx_may_queue() is always following __blk_mq_tag_busy()
- hctx_may_queue() returns true in case that this flag is cleared
- current __blk_mq_tag_busy() does nothing, the following allocation works fine given hctx_may_queue() returns true
- new __blk_mq_tag_busy() will setup everything as fine
Thanks,
Ming
Quoted 48 lines · Ming Lei
Quoted 32 lines · Damien Le Moal
Quoted 31 lines · Ming Lei
Quoted 30 lines · Damien Le Moal
Quoted 17 lines · Tian Lan
From: Tian Lan <[email protected]>
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 ee78ec1077d3 ("blk-mq: blk_mq_tag_busy is no need to return a value")Signed-off-by Tian Lan <[email protected]>--- block/blk-mq-tag.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)block/blk-mq-tag.c +0
-0block/blk-mq-tag.c void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx) · line 46
Quoted 6 lines
block/blk-mq-tag.c +2
-1@@ -42,13 +42,15 @@ void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx)if (blk_mq_is_shared_tags (hctx->flags)) {struct request_queue *q = hctx->queue; -if (test_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags)) +if (test_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags) || +test_and_set_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags)) {This is weird.
test_and_set_bit()returns the bit old value, so shouldn't this be:if (test_and_set_bit(QUEUE_FLAG_HCTX_ACTIVE, &q->queue_flags)) return;Quoted 7 lines
Quoted 4 lines
Quoted 1 line
?
It is one micro optimization since
test_and_set_bitis much heavier thantest_bit, sotest_and_set_bit()is just needed in the 1st time.But having the 2 calls
test_bit+test_and_set_bitcreates a race, doesn't it ? What if another cpu clears the bit between these 2 calls ?If
test_bit()returns 0, there isn't such race since both sides are atomic OP.If
test_bit()returns 1:1)
__blk_mq_tag_busy()vs.__blk_mq_tag_busy()
- both does nothing
Quoted 6 lines
2)
__blk_mq_tag_busy()vs.__blk_mq_tag_idle()
-hctx_may_queue()is always following__blk_mq_tag_busy()
-hctx_may_queue()returns true in case that this flag is cleared
- current__blk_mq_tag_busy()does nothing, the following allocation works fine givenhctx_may_queue()returns true
- new__blk_mq_tag_busy()will setup everything as fine
OK. Thanks. It would be nice to update the function comment (which is not very clear due to grammar issues) to document this unusual use of the bit functions, including the fact that it is an optimization to avoid dirtying cache lines.
Quoted 5 lines
Thanks,
Ming
Signature · 2 lines
Damien Le Moal Western Digital Research
Quoted 18 lines · Tian Lan
From: Tian Lan <[email protected]>
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 ee78ec1077d3 ("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]>--- block/blk-mq-tag.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)block/blk-mq-tag.c +0
-0
block/blk-mq-tag.c void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx) · line 46
Quoted 6 lines
block/blk-mq-tag.c +2
-1@@ -42,13 +42,13 @@ void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx)if (blk_mq_is_shared_tags (hctx->flags)) {struct request_queue *q = hctx->queue; -if (test_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags)) +if (test_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags) || +test_and_set_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags))
How about add a small comment why we have the test_bit()? It seems to be asked every so often, like: https://lore.kernel.org/linux-block/[email protected]/#t
Quoted 11 lines
block/blk-mq-tag.c +2
-3return ; -set_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags); }else { -if (test_bit (BLK_MQ_S_TAG_ACTIVE , &hctx->state)) +if (test_bit (BLK_MQ_S_TAG_ACTIVE , &hctx->state) || +test_and_set_bit (BLK_MQ_S_TAG_ACTIVE , &hctx->state))return ; -set_bit (BLK_MQ_S_TAG_ACTIVE , &hctx->state); } users =atomic_inc_return (&hctx->tags->active_queues);
PATCH 1/1 blk-mq: fix race condition in active queue accounting
From: Tian Lan <[email protected]>
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.
--- block/blk-mq-tag.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
block/blk-mq-tag.c +8 -4
@@ -39,16 +39,20 @@ void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx) {unsigned int users; +/* +* calling test_bit() prior to test_and_set_bit() is intentional, +* it avoids dirtying the cacheline if the queue is already active. +*/ if (blk_mq_is_shared_tags (hctx->flags)) {struct request_queue *q = hctx->queue; -if (test_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags)) +if (test_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags) || +test_and_set_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags))return ; -set_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags); }else { -if (test_bit (BLK_MQ_S_TAG_ACTIVE , &hctx->state)) +if (test_bit (BLK_MQ_S_TAG_ACTIVE , &hctx->state) || +test_and_set_bit (BLK_MQ_S_TAG_ACTIVE , &hctx->state))return ; -set_bit (BLK_MQ_S_TAG_ACTIVE , &hctx->state); } users =atomic_inc_return (&hctx->tags->active_queues);
Signature · 1 line
2.25.1
Quoted 9 lines · Tian Lan
From: Tian Lan <[email protected]>
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 ee78ec1077d3 ("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]>
Quoted 33 lines
--- block/blk-mq-tag.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)block/blk-mq-tag.c +8
-4@@ -39,16 +39,20 @@ void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx) {unsigned int users; +/* +* calling test_bit() prior to test_and_set_bit() is intentional, +* it avoids dirtying the cacheline if the queue is already active. +*/ if (blk_mq_is_shared_tags (hctx->flags)) {struct request_queue *q = hctx->queue; -if (test_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags)) +if (test_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags) || +test_and_set_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags))return ; -set_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags); }else { -if (test_bit (BLK_MQ_S_TAG_ACTIVE , &hctx->state)) +if (test_bit (BLK_MQ_S_TAG_ACTIVE , &hctx->state) || +test_and_set_bit (BLK_MQ_S_TAG_ACTIVE , &hctx->state))return ; -set_bit (BLK_MQ_S_TAG_ACTIVE , &hctx->state); } users =atomic_inc_return (&hctx->tags->active_queues);
Signature · 2 lines
Damien Le Moal Western Digital Research
Quoted 9 lines · Tian Lan
From: Tian Lan <[email protected]>
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 ee78ec1077d3 ("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]>
Quoted 12 lines
--- block/blk-mq-tag.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)block/blk-mq-tag.c +0
-0@@ -39,16 +39,20 @@ void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx) {unsigned int users;
block/blk-mq-tag.c void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx) · line 50
Quoted 10 lines
block/blk-mq-tag.c +6
-1+/* +* calling test_bit() prior to test_and_set_bit() is intentional, +* it avoids dirtying the cacheline if the queue is already active. +*/ if (blk_mq_is_shared_tags (hctx->flags)) {struct request_queue *q = hctx->queue; -if (test_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags)) +if (test_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags) || +test_and_set_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags))
We could also add a bitops wrapper function for this. I don't know what name to use, maybe test_and_set_bit_fast_but_racy() - I'm half joking about the name
Quoted 11 lines
block/blk-mq-tag.c +2
-3return ; -set_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags); }else { -if (test_bit (BLK_MQ_S_TAG_ACTIVE , &hctx->state)) +if (test_bit (BLK_MQ_S_TAG_ACTIVE , &hctx->state) || +test_and_set_bit (BLK_MQ_S_TAG_ACTIVE , &hctx->state))return ; -set_bit (BLK_MQ_S_TAG_ACTIVE , &hctx->state); } users =atomic_inc_return (&hctx->tags->active_queues);
Quoted 5 lines · Tian Lan
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.
Applied, thanks!
[1/1] blk-mq: fix race condition in active queue accounting
(no commit info)
Best regards,
Signature · 1 line
Jens Axboe
Quoted 7 lines · John Garry
block/blk-mq-tag.c void __blk_mq_tag_busy(struct blk_mq_hw_ctx *hctx) · line 50
Quoted 2 lines · Tian Lan
block/blk-mq-tag.c +2
-0+if (test_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags) || +test_and_set_bit (QUEUE_FLAG_HCTX_ACTIVE , &q->queue_flags))We could also add a bitops wrapper function for this. I don't know what name to use, maybe
test_and_set_bit_fast_but_racy()- I'm half joking about the name
I don't think this pattern is any more racy than a solo test_and_set_bit(). Maybe "test_then_test_and_set_bit()"?