# \[PATCH V2\] blk-mq: don't grab rq's refcount in blk\_mq\_check\_expired()

- Subsystems: BLOCK LAYER, THE REST
- Maintainers: Jens Axboe, Linus Torvalds
- Status: LANDED (c797b40ccc34 · 2021-08-17 14:32)
- Landed in mainline as c797b40ccc34 on 2021-08-17.
- List: linux-block
- 5 messages, 4 authors
- Page: https://ratatoskr.run/linux-block/2021/08/6404533/t

## \[PATCH V2\] blk-mq: don't grab rq's refcount in blk\_mq\_check\_expired()

- From: Ming Lei \<hidden\>
- Date: 2021-08-11 15:52:54 
- Message: https://ratatoskr.run/linux-block/2021/08/6404533.md

```text
Inside blk_mq_queue_tag_busy_iter() we already grabbed request's
refcount before calling ->fn(), so needn't to grab it one more time
in blk_mq_check_expired().

Meantime remove extra request expire check in blk_mq_check_expired().

Cc: Keith Busch <kbusch@kernel.org>
Signed-off-by: Ming Lei <redacted>
---
V2:
	- remove extra request expire check as suggested by Keith
	- modify comment a bit

 block/blk-mq.c | 30 +++++-------------------------
 1 file changed, 5 insertions(+), 25 deletions(-)

diff --git a/block/blk-mq.c b/block/blk-mq.c
index d2725f94491d..b5237211ccb7 100644
--- a/block/blk-mq.c
+++ b/block/blk-mq.c
@@ -923,34 +923,14 @@ static bool blk_mq_check_expired(struct blk_mq_hw_ctx *hctx,
 	unsigned long *next = priv;
 
 	/*
-	 * Just do a quick check if it is expired before locking the request in
-	 * so we're not unnecessarilly synchronizing across CPUs.
-	 */
-	if (!blk_mq_req_expired(rq, next))
-		return true;
-
-	/*
-	 * We have reason to believe the request may be expired. Take a
-	 * reference on the request to lock this request lifetime into its
-	 * currently allocated context to prevent it from being reallocated in
-	 * the event the completion by-passes this timeout handler.
-	 *
-	 * If the reference was already released, then the driver beat the
-	 * timeout handler to posting a natural completion.
-	 */
-	if (!refcount_inc_not_zero(&rq->ref))
-		return true;
-
-	/*
-	 * The request is now locked and cannot be reallocated underneath the
-	 * timeout handler's processing. Re-verify this exact request is truly
-	 * expired; if it is not expired, then the request was completed and
-	 * reallocated as a new request.
+	 * blk_mq_queue_tag_busy_iter() has locked the request, so it cannot
+	 * be reallocated underneath the timeout handler's processing, then
+	 * the expire check is reliable. If the request is not expired, then
+	 * it was completed and reallocated as a new request after returning
+	 * from blk_mq_check_expired().
 	 */
 	if (blk_mq_req_expired(rq, next))
 		blk_mq_rq_timed_out(rq, reserved);
-
-	blk_mq_put_rq_ref(rq);
 	return true;
 }
 
-- 
2.31.1

```

## Re: \[PATCH V2\] blk-mq: don't grab rq's refcount in blk\_mq\_check\_expired()

- From: Christoph Hellwig \<hch@lst.de\>
- Date: 2021-08-12 08:55:38 
- Message: https://ratatoskr.run/linux-block/2021/08/6404606.md

```text
Looks good,

Reviewed-by: Christoph Hellwig <hch@lst.de>
```

## Re: \[PATCH V2\] blk-mq: don't grab rq's refcount in blk\_mq\_check\_expired()

- From: John Garry \<hidden\>
- Date: 2021-08-12 09:40:45 
- Message: https://ratatoskr.run/linux-block/2021/08/6404608.md

```text
On 11/08/2021 16:52, Ming Lei wrote:
> Inside blk_mq_queue_tag_busy_iter() we already grabbed request's
> refcount before calling ->fn(), so needn't to grab it one more time
> in blk_mq_check_expired().
>
> Meantime remove extra request expire check in blk_mq_check_expired().
>
> Cc: Keith Busch<kbusch@kernel.org>
> Signed-off-by: Ming Lei<redacted>

Reviewed-by: John Garry <redacted>
```

## Re: \[PATCH V2\] blk-mq: don't grab rq's refcount in blk\_mq\_check\_expired()

- From: Ming Lei \<hidden\>
- Date: 2021-08-17 01:04:15 
- Message: https://ratatoskr.run/linux-block/2021/08/6404746.md

```text
On Wed, Aug 11, 2021 at 11:52:02PM +0800, Ming Lei wrote:
> Inside blk_mq_queue_tag_busy_iter() we already grabbed request's
> refcount before calling ->fn(), so needn't to grab it one more time
> in blk_mq_check_expired().
> 
> Meantime remove extra request expire check in blk_mq_check_expired().
> 
> Cc: Keith Busch <kbusch@kernel.org>
> Signed-off-by: Ming Lei <redacted>
> ---
> V2:
> 	- remove extra request expire check as suggested by Keith
> 	- modify comment a bit
> 

Hello Jens,

Ping...

Thanks,
Ming

```

## Re: \[PATCH V2\] blk-mq: don't grab rq's refcount in blk\_mq\_check\_expired()

- From: Jens Axboe \<axboe@kernel.dk\>
- Date: 2021-08-17 14:35:15 
- Message: https://ratatoskr.run/linux-block/2021/08/6404760.md

```text
On 8/11/21 9:52 AM, Ming Lei wrote:
> Inside blk_mq_queue_tag_busy_iter() we already grabbed request's
> refcount before calling ->fn(), so needn't to grab it one more time
> in blk_mq_check_expired().
> 
> Meantime remove extra request expire check in blk_mq_check_expired().

Applied, thanks Ming.

-- 
Jens Axboe

```
