[4/4] blk-mq: improve readability of blk_mq_alloc_request()

Message ID 81fcbc046c9ea96cdfd8e20d1edc8e64c4d08153.1667314759.git.nickyc975@zju.edu.cn
State New
Headers
Series some random cleanups for blk-mq.c |

Commit Message

Jinlong Chen Nov. 1, 2022, 3:11 p.m. UTC
  Add a helper blk_mq_alloc_request_nocache() to alloc request without
cache. This makes blk_mq_alloc_request() more readable.

Signed-off-by: Jinlong Chen <nickyc975@zju.edu.cn>
---
 block/blk-mq.c | 47 +++++++++++++++++++++++++++++------------------
 1 file changed, 29 insertions(+), 18 deletions(-)
  

Comments

Christoph Hellwig Nov. 1, 2022, 5:37 p.m. UTC | #1
On Tue, Nov 01, 2022 at 11:11:37PM +0800, Jinlong Chen wrote:
> Add a helper blk_mq_alloc_request_nocache() to alloc request without
> cache. This makes blk_mq_alloc_request() more readable.
> 
> Signed-off-by: Jinlong Chen <nickyc975@zju.edu.cn>
> ---
>  block/blk-mq.c | 47 +++++++++++++++++++++++++++++------------------
>  1 file changed, 29 insertions(+), 18 deletions(-)
> 
> diff --git a/block/blk-mq.c b/block/blk-mq.c
> index 87a6348a0d0a..2fae111a42c8 100644
> --- a/block/blk-mq.c
> +++ b/block/blk-mq.c
> @@ -572,36 +572,47 @@ static struct request *blk_mq_alloc_cached_request(struct request_queue *q,
>  	return rq;
>  }
>  
> +static struct request *blk_mq_alloc_request_nocache(struct request_queue *q,
> +		blk_opf_t opf, blk_mq_req_flags_t flags)

The name is a bit odd, but I can't think off a better one.

> +	struct blk_mq_alloc_data data = {
>  			.q		= q,
>  			.flags		= flags,
>  			.cmd_flags	= opf,
>  			.nr_tags	= 1,
>  		};

And this now has superflous indenation.  Overall, while the separate
helper looks marginally nicer, I'm not really sure it is worth the
churn.
  
Chaitanya Kulkarni Nov. 2, 2022, 1:16 a.m. UTC | #2
>> +static struct request *blk_mq_alloc_request_nocache(struct request_queue *q,
>> +		blk_opf_t opf, blk_mq_req_flags_t flags)
> 
> The name is a bit odd, but I can't think off a better one.
> 
>> +	struct blk_mq_alloc_data data = {
>>   			.q		= q,
>>   			.flags		= flags,
>>   			.cmd_flags	= opf,
>>   			.nr_tags	= 1,
>>   		};
> 
> And this now has superflous indenation.  Overall, while the separate
> helper looks marginally nicer, I'm not really sure it is worth the
> churn.

existing code is fine here than adding this indentation which
is not worth a churn...

-ck
  
Jinlong Chen Nov. 2, 2022, 2:19 a.m. UTC | #3
> On Tue, Nov 01, 2022 at 11:11:37PM +0800, Jinlong Chen wrote:
> > Add a helper blk_mq_alloc_request_nocache() to alloc request without
> > cache. This makes blk_mq_alloc_request() more readable.
> > 
> > Signed-off-by: Jinlong Chen <nickyc975@zju.edu.cn>
> > ---
> >  block/blk-mq.c | 47 +++++++++++++++++++++++++++++------------------
> >  1 file changed, 29 insertions(+), 18 deletions(-)
> > 
> > diff --git a/block/blk-mq.c b/block/blk-mq.c
> > index 87a6348a0d0a..2fae111a42c8 100644
> > --- a/block/blk-mq.c
> > +++ b/block/blk-mq.c
> > @@ -572,36 +572,47 @@ static struct request *blk_mq_alloc_cached_request(struct request_queue *q,
> >  	return rq;
> >  }
> >  
> > +static struct request *blk_mq_alloc_request_nocache(struct request_queue *q,
> > +		blk_opf_t opf, blk_mq_req_flags_t flags)
> 
> The name is a bit odd, but I can't think off a better one.
> 
> > +	struct blk_mq_alloc_data data = {
> >  			.q		= q,
> >  			.flags		= flags,
> >  			.cmd_flags	= opf,
> >  			.nr_tags	= 1,
> >  		};
> 
> And this now has superflous indenation.  Overall, while the separate
> helper looks marginally nicer, I'm not really sure it is worth the
> churn.

I'll drop the patch if you think it is not worth the churn. But I
started doing this because of the following goto statement:

	rq = blk_mq_alloc_cached_request(q, opf, flags);
	if (!rq) {
		[...]
		ret = blk_queue_enter(q, flags);
		[...]
		rq = __blk_mq_alloc_requests(&data);
		if (!rq)
			goto out_queue_exit;
	}
	[...]
out_queue_exit:
	blk_queue_exit(q);
	return ERR_PTR(-EWOULDBLOCK);

Queue entering has been moved into the fallback path, left queue exiting
outside. Should I just eliminate the goto statement and move the error
handling into the fallback path too? Like:

	rq = blk_mq_alloc_cached_request(q, opf, flags);
	if (!rq) {
		[...]
		ret = blk_queue_enter(q, flags);
		[...]
		rq = __blk_mq_alloc_requests(&data);
		if (!rq) {
			blk_queue_exit(q);
			return ERR_PTR(-EWOULDBLOCK);
		}
	}

Thanks!
Jinlong Chen
  
Jens Axboe Nov. 2, 2022, 2:25 a.m. UTC | #4
On 11/1/22 8:19 PM, Jinlong Chen wrote:
>> On Tue, Nov 01, 2022 at 11:11:37PM +0800, Jinlong Chen wrote:
>>> Add a helper blk_mq_alloc_request_nocache() to alloc request without
>>> cache. This makes blk_mq_alloc_request() more readable.
>>>
>>> Signed-off-by: Jinlong Chen <nickyc975@zju.edu.cn>
>>> ---
>>>  block/blk-mq.c | 47 +++++++++++++++++++++++++++++------------------
>>>  1 file changed, 29 insertions(+), 18 deletions(-)
>>>
>>> diff --git a/block/blk-mq.c b/block/blk-mq.c
>>> index 87a6348a0d0a..2fae111a42c8 100644
>>> --- a/block/blk-mq.c
>>> +++ b/block/blk-mq.c
>>> @@ -572,36 +572,47 @@ static struct request *blk_mq_alloc_cached_request(struct request_queue *q,
>>>  	return rq;
>>>  }
>>>  
>>> +static struct request *blk_mq_alloc_request_nocache(struct request_queue *q,
>>> +		blk_opf_t opf, blk_mq_req_flags_t flags)
>>
>> The name is a bit odd, but I can't think off a better one.
>>
>>> +	struct blk_mq_alloc_data data = {
>>>  			.q		= q,
>>>  			.flags		= flags,
>>>  			.cmd_flags	= opf,
>>>  			.nr_tags	= 1,
>>>  		};
>>
>> And this now has superflous indenation.  Overall, while the separate
>> helper looks marginally nicer, I'm not really sure it is worth the
>> churn.
> 
> I'll drop the patch if you think it is not worth the churn. But I
> started doing this because of the following goto statement:

Please just drop it, I don't think it's an improvement.
  
Jinlong Chen Nov. 2, 2022, 2:39 a.m. UTC | #5
> -----原始邮件-----
> 发件人: "Jens Axboe" <axboe@kernel.dk>
> 发送时间: 2022-11-02 10:25:27 (星期三)
> 收件人: "Jinlong Chen" <nickyc975@zju.edu.cn>, "Christoph Hellwig" <hch@lst.de>
> 抄送: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org
> 主题: Re: [PATCH 4/4] blk-mq: improve readability of blk_mq_alloc_request()
> 
> On 11/1/22 8:19 PM, Jinlong Chen wrote:
> >> On Tue, Nov 01, 2022 at 11:11:37PM +0800, Jinlong Chen wrote:
> >>> Add a helper blk_mq_alloc_request_nocache() to alloc request without
> >>> cache. This makes blk_mq_alloc_request() more readable.
> >>>
> >>> Signed-off-by: Jinlong Chen <nickyc975@zju.edu.cn>
> >>> ---
> >>>  block/blk-mq.c | 47 +++++++++++++++++++++++++++++------------------
> >>>  1 file changed, 29 insertions(+), 18 deletions(-)
> >>>
> >>> diff --git a/block/blk-mq.c b/block/blk-mq.c
> >>> index 87a6348a0d0a..2fae111a42c8 100644
> >>> --- a/block/blk-mq.c
> >>> +++ b/block/blk-mq.c
> >>> @@ -572,36 +572,47 @@ static struct request *blk_mq_alloc_cached_request(struct request_queue *q,
> >>>  	return rq;
> >>>  }
> >>>  
> >>> +static struct request *blk_mq_alloc_request_nocache(struct request_queue *q,
> >>> +		blk_opf_t opf, blk_mq_req_flags_t flags)
> >>
> >> The name is a bit odd, but I can't think off a better one.
> >>
> >>> +	struct blk_mq_alloc_data data = {
> >>>  			.q		= q,
> >>>  			.flags		= flags,
> >>>  			.cmd_flags	= opf,
> >>>  			.nr_tags	= 1,
> >>>  		};
> >>
> >> And this now has superflous indenation.  Overall, while the separate
> >> helper looks marginally nicer, I'm not really sure it is worth the
> >> churn.
> > 
> > I'll drop the patch if you think it is not worth the churn. But I
> > started doing this because of the following goto statement:
> 
> Please just drop it, I don't think it's an improvement.

Ok, then I'll just resend patch 2 without the silly goto return NULL and
patch 3.

Thanks!
Jinlong Chen
  

Patch

diff --git a/block/blk-mq.c b/block/blk-mq.c
index 87a6348a0d0a..2fae111a42c8 100644
--- a/block/blk-mq.c
+++ b/block/blk-mq.c
@@ -572,36 +572,47 @@  static struct request *blk_mq_alloc_cached_request(struct request_queue *q,
 	return rq;
 }
 
-struct request *blk_mq_alloc_request(struct request_queue *q, blk_opf_t opf,
-		blk_mq_req_flags_t flags)
+static struct request *blk_mq_alloc_request_nocache(struct request_queue *q,
+		blk_opf_t opf, blk_mq_req_flags_t flags)
 {
-	struct request *rq;
-
-	rq = blk_mq_alloc_cached_request(q, opf, flags);
-	if (!rq) {
-		struct blk_mq_alloc_data data = {
+	struct blk_mq_alloc_data data = {
 			.q		= q,
 			.flags		= flags,
 			.cmd_flags	= opf,
 			.nr_tags	= 1,
 		};
-		int ret;
+	struct request *rq;
+	int ret;
 
-		ret = blk_queue_enter(q, flags);
-		if (ret)
-			return ERR_PTR(ret);
+	ret = blk_queue_enter(q, flags);
+	if (ret)
+		return ERR_PTR(ret);
 
-		rq = __blk_mq_alloc_requests(&data);
-		if (!rq)
-			goto out_queue_exit;
+	rq = __blk_mq_alloc_requests(&data);
+	if (!rq) {
+		rq = ERR_PTR(-EWOULDBLOCK);
+		blk_queue_exit(q);
 	}
+
+	return rq;
+}
+
+struct request *blk_mq_alloc_request(struct request_queue *q, blk_opf_t opf,
+		blk_mq_req_flags_t flags)
+{
+	struct request *rq;
+
+	rq = blk_mq_alloc_cached_request(q, opf, flags);
+	if (!rq) {
+		rq = blk_mq_alloc_request_nocache(q, opf, flags);
+		if (IS_ERR(rq))
+			return rq;
+	}
+
 	rq->__data_len = 0;
 	rq->__sector = (sector_t) -1;
 	rq->bio = rq->biotail = NULL;
-	return rq;
-out_queue_exit:
-	blk_queue_exit(q);
-	return ERR_PTR(-EWOULDBLOCK);
+	return rq;
 }
 EXPORT_SYMBOL(blk_mq_alloc_request);