diff mbox series

[RFC,v2,1/7] block: Extand commit_rqs() to do batch processing

Message ID c8bd9e5ba815a3f1bc9dac0a4bc2fbadadbc0a43.1587888520.git.baolin.wang7@gmail.com (mailing list archive)
State New, archived
Headers show
Series Add MMC packed request support | expand

Commit Message

Baolin Wang April 26, 2020, 9:38 a.m. UTC
From: Ming Lei <ming.lei@redhat.com>

Now some SD/MMC host controllers can support packed command or packed request,
that means we can package several requests to host controller at one time
to improve performence.

But the blk-mq always takes one request from the scheduler and dispatch it to
the device, regardless of the driver or the scheduler, so there should only
ever be one request in the local list in blk_mq_dispatch_rq_list(), that means
the bd.last is always true and the driver can not use bd.last to decide if
there are requests are pending now in hardware queue to help to package
requests.

Thus this patch introduces a new 'BLK_MQ_F_FORCE_COMMIT_RQS' flag to call
.commit_rqs() to do batch processing if necessary.

Signed-off-by: Ming Lei <ming.lei@redhat.com>
Tested-by: Baolin Wang <baolin.wang7@gmail.com>
Signed-off-by: Baolin Wang <baolin.wang7@gmail.com>
---
 block/blk-mq-sched.c   | 29 ++++++++++++++++++++---------
 block/blk-mq.c         | 15 ++++++++++-----
 include/linux/blk-mq.h |  1 +
 3 files changed, 31 insertions(+), 14 deletions(-)

Comments

Christoph Hellwig April 27, 2020, 3:46 p.m. UTC | #1
extand in the subject really shpuld be 'extend'

On Sun, Apr 26, 2020 at 05:38:54PM +0800, Baolin Wang wrote:
> From: Ming Lei <ming.lei@redhat.com>
> 
> Now some SD/MMC host controllers can support packed command or packed request,
> that means we can package several requests to host controller at one time
> to improve performence.
> 
> But the blk-mq always takes one request from the scheduler and dispatch it to
> the device, regardless of the driver or the scheduler, so there should only
> ever be one request in the local list in blk_mq_dispatch_rq_list(), that means
> the bd.last is always true and the driver can not use bd.last to decide if
> there are requests are pending now in hardware queue to help to package
> requests.
> 
> Thus this patch introduces a new 'BLK_MQ_F_FORCE_COMMIT_RQS' flag to call
> .commit_rqs() to do batch processing if necessary.
> 
> Signed-off-by: Ming Lei <ming.lei@redhat.com>
> Tested-by: Baolin Wang <baolin.wang7@gmail.com>
> Signed-off-by: Baolin Wang <baolin.wang7@gmail.com>
> ---
>  block/blk-mq-sched.c   | 29 ++++++++++++++++++++---------
>  block/blk-mq.c         | 15 ++++++++++-----
>  include/linux/blk-mq.h |  1 +
>  3 files changed, 31 insertions(+), 14 deletions(-)
> 
> diff --git a/block/blk-mq-sched.c b/block/blk-mq-sched.c
> index 74cedea56034..3429a71a7364 100644
> --- a/block/blk-mq-sched.c
> +++ b/block/blk-mq-sched.c
> @@ -85,11 +85,12 @@ void blk_mq_sched_restart(struct blk_mq_hw_ctx *hctx)
>   * its queue by itself in its completion handler, so we don't need to
>   * restart queue if .get_budget() returns BLK_STS_NO_RESOURCE.
>   */
> -static void blk_mq_do_dispatch_sched(struct blk_mq_hw_ctx *hctx)
> +static bool blk_mq_do_dispatch_sched(struct blk_mq_hw_ctx *hctx)

This function already returns an int in the current for-5.8/block tree.

> +		if (!(hctx->flags & BLK_MQ_F_FORCE_COMMIT_RQS)) {
> +			if (list_empty(list)) {
> +				bd.last = true;
> +			} else {
> +				nxt = list_first_entry(list, struct request,
> +						       queuelist);
> +				bd.last = !blk_mq_get_driver_tag(nxt);
> +			}
> +		} else {
> +			bd.last = false;
>  		}

This seems a little odd in terms of code flow.  Why not:

		if (hctx->flags & BLK_MQ_F_FORCE_COMMIT_RQS) {
			bd.last = false;
		} else if (list_empty(list)) {
			bd.last = true;
		} else {
			nxt = list_first_entry(list, struct request, queuelist);
			bd.last = !blk_mq_get_driver_tag(nxt);
		}

> diff --git a/include/linux/blk-mq.h b/include/linux/blk-mq.h
> index f389d7c724bd..6a20f8e8eb85 100644
> --- a/include/linux/blk-mq.h
> +++ b/include/linux/blk-mq.h
> @@ -391,6 +391,7 @@ struct blk_mq_ops {
>  enum {
>  	BLK_MQ_F_SHOULD_MERGE	= 1 << 0,
>  	BLK_MQ_F_TAG_SHARED	= 1 << 1,
> +	BLK_MQ_F_FORCE_COMMIT_RQS = 1 << 3,

Maybe BLK_MQ_F_ALWAYS_COMMIT might be a better name?  Also this
flag (just like the existing ones..) could really use a comment
explaining it.
Baolin Wang April 28, 2020, 8:02 a.m. UTC | #2
On Mon, Apr 27, 2020 at 11:46 PM Christoph Hellwig <hch@infradead.org> wrote:
>
> extand in the subject really shpuld be 'extend'

Sorry for typo, and will fix in next version.

>
> On Sun, Apr 26, 2020 at 05:38:54PM +0800, Baolin Wang wrote:
> > From: Ming Lei <ming.lei@redhat.com>
> >
> > Now some SD/MMC host controllers can support packed command or packed request,
> > that means we can package several requests to host controller at one time
> > to improve performence.
> >
> > But the blk-mq always takes one request from the scheduler and dispatch it to
> > the device, regardless of the driver or the scheduler, so there should only
> > ever be one request in the local list in blk_mq_dispatch_rq_list(), that means
> > the bd.last is always true and the driver can not use bd.last to decide if
> > there are requests are pending now in hardware queue to help to package
> > requests.
> >
> > Thus this patch introduces a new 'BLK_MQ_F_FORCE_COMMIT_RQS' flag to call
> > .commit_rqs() to do batch processing if necessary.
> >
> > Signed-off-by: Ming Lei <ming.lei@redhat.com>
> > Tested-by: Baolin Wang <baolin.wang7@gmail.com>
> > Signed-off-by: Baolin Wang <baolin.wang7@gmail.com>
> > ---
> >  block/blk-mq-sched.c   | 29 ++++++++++++++++++++---------
> >  block/blk-mq.c         | 15 ++++++++++-----
> >  include/linux/blk-mq.h |  1 +
> >  3 files changed, 31 insertions(+), 14 deletions(-)
> >
> > diff --git a/block/blk-mq-sched.c b/block/blk-mq-sched.c
> > index 74cedea56034..3429a71a7364 100644
> > --- a/block/blk-mq-sched.c
> > +++ b/block/blk-mq-sched.c
> > @@ -85,11 +85,12 @@ void blk_mq_sched_restart(struct blk_mq_hw_ctx *hctx)
> >   * its queue by itself in its completion handler, so we don't need to
> >   * restart queue if .get_budget() returns BLK_STS_NO_RESOURCE.
> >   */
> > -static void blk_mq_do_dispatch_sched(struct blk_mq_hw_ctx *hctx)
> > +static bool blk_mq_do_dispatch_sched(struct blk_mq_hw_ctx *hctx)
>
> This function already returns an int in the current for-5.8/block tree.

Thanks for pointing this out, and seems I should re-modify the return
values of the functions.

>
> > +             if (!(hctx->flags & BLK_MQ_F_FORCE_COMMIT_RQS)) {
> > +                     if (list_empty(list)) {
> > +                             bd.last = true;
> > +                     } else {
> > +                             nxt = list_first_entry(list, struct request,
> > +                                                    queuelist);
> > +                             bd.last = !blk_mq_get_driver_tag(nxt);
> > +                     }
> > +             } else {
> > +                     bd.last = false;
> >               }
>
> This seems a little odd in terms of code flow.  Why not:
>
>                 if (hctx->flags & BLK_MQ_F_FORCE_COMMIT_RQS) {
>                         bd.last = false;
>                 } else if (list_empty(list)) {
>                         bd.last = true;
>                 } else {
>                         nxt = list_first_entry(list, struct request, queuelist);
>                         bd.last = !blk_mq_get_driver_tag(nxt);
>                 }

Yes, looks better.

> > diff --git a/include/linux/blk-mq.h b/include/linux/blk-mq.h
> > index f389d7c724bd..6a20f8e8eb85 100644
> > --- a/include/linux/blk-mq.h
> > +++ b/include/linux/blk-mq.h
> > @@ -391,6 +391,7 @@ struct blk_mq_ops {
> >  enum {
> >       BLK_MQ_F_SHOULD_MERGE   = 1 << 0,
> >       BLK_MQ_F_TAG_SHARED     = 1 << 1,
> > +     BLK_MQ_F_FORCE_COMMIT_RQS = 1 << 3,
>
> Maybe BLK_MQ_F_ALWAYS_COMMIT might be a better name?  Also this

Looks reasonable to me, and will do.

> flag (just like the existing ones..) could really use a comment
> explaining it.

OK, will add some comments. Thanks for your comments.
Sagi Grimberg May 8, 2020, 9:35 p.m. UTC | #3
>> diff --git a/include/linux/blk-mq.h b/include/linux/blk-mq.h
>> index f389d7c724bd..6a20f8e8eb85 100644
>> --- a/include/linux/blk-mq.h
>> +++ b/include/linux/blk-mq.h
>> @@ -391,6 +391,7 @@ struct blk_mq_ops {
>>   enum {
>>   	BLK_MQ_F_SHOULD_MERGE	= 1 << 0,
>>   	BLK_MQ_F_TAG_SHARED	= 1 << 1,
>> +	BLK_MQ_F_FORCE_COMMIT_RQS = 1 << 3,
> 
> Maybe BLK_MQ_F_ALWAYS_COMMIT might be a better name?  Also this
> flag (just like the existing ones..) could really use a comment
> explaining it.

Would it make sense to elevate this flag to a request_queue flag
(QUEUE_FLAG_ALWAYS_COMMIT)?

I'm thinking of a possibility that an I/O scheduler may be used
to activate this functionality rather than having the driver set
it necessarily...
Ming Lei May 8, 2020, 9:46 p.m. UTC | #4
On Fri, May 08, 2020 at 02:35:35PM -0700, Sagi Grimberg wrote:
> 
> > > diff --git a/include/linux/blk-mq.h b/include/linux/blk-mq.h
> > > index f389d7c724bd..6a20f8e8eb85 100644
> > > --- a/include/linux/blk-mq.h
> > > +++ b/include/linux/blk-mq.h
> > > @@ -391,6 +391,7 @@ struct blk_mq_ops {
> > >   enum {
> > >   	BLK_MQ_F_SHOULD_MERGE	= 1 << 0,
> > >   	BLK_MQ_F_TAG_SHARED	= 1 << 1,
> > > +	BLK_MQ_F_FORCE_COMMIT_RQS = 1 << 3,
> > 
> > Maybe BLK_MQ_F_ALWAYS_COMMIT might be a better name?  Also this
> > flag (just like the existing ones..) could really use a comment
> > explaining it.
> 
> Would it make sense to elevate this flag to a request_queue flag
> (QUEUE_FLAG_ALWAYS_COMMIT)?

request queue flag usually is writable, however this case just needs
one read-only flag, so I think it may be better to make it as
tagset/hctx flag.

> 
> I'm thinking of a possibility that an I/O scheduler may be used
> to activate this functionality rather than having the driver set
> it necessarily...

Could you explain a bit why I/O scheduler should activate this
functionality?

batching submission may be good for some drivers, and currently
we only do it in limited way. One reason is that there is extra
cost for full batching submission, such as this patch requires
one extra .commit_rqs() for each dispatch, and lock is often needed
in this callback.

IMO it can be a win for some slow driver or device, but may cause
a little performance drop for fast driver/device especially in workload
of not-batching submission.


Thanks, 
Ming
Sagi Grimberg May 8, 2020, 10:19 p.m. UTC | #5
Hey Ming,

>> Would it make sense to elevate this flag to a request_queue flag
>> (QUEUE_FLAG_ALWAYS_COMMIT)?
> 
> request queue flag usually is writable, however this case just needs
> one read-only flag, so I think it may be better to make it as
> tagset/hctx flag.

I actually intended it to be writable.

>> I'm thinking of a possibility that an I/O scheduler may be used
>> to activate this functionality rather than having the driver set
>> it necessarily...
> 
> Could you explain a bit why I/O scheduler should activate this
> functionality?

Sure, I've recently seen some academic work showing the benefits
of batching in tcp/ip based block drivers. The problem with the
approaches taken is that I/O scheduling is exercised deep down in the
driver, which is not the direction I'd like to go if we are want
to adopt some of the batching concepts.

I spent some (limited) time thinking about this, and it seems to
me that there is an opportunity to implement this as a dedicated
I/O scheduler, and tie it to driver specific LLD stack optimizations
(net-stack for example) relying on the commit_rq/bd->last hints.

When scanning the scheduler code, I noticed exactly the phenomenon that
this patchset is attempting to solve and Christoph referred me to it.
Now I'm thinking if we can extend this batching optimization for both
use-cases.

> batching submission may be good for some drivers, and currently
> we only do it in limited way. One reason is that there is extra
> cost for full batching submission, such as this patch requires
> one extra .commit_rqs() for each dispatch, and lock is often needed
> in this callback.

That is not necessarily the case at all.

> IMO it can be a win for some slow driver or device, but may cause
> a little performance drop for fast driver/device especially in workload
> of not-batching submission.

You're mostly correct. This is exactly why an I/O scheduler may be
applicable here IMO. Mostly because I/O schedulers tend to optimize for
something specific and always present tradeoffs. Users need to
understand what they are optimizing for.

Hence I'd say this functionality can definitely be available to an I/O
scheduler should one exist.
Ming Lei May 8, 2020, 11:22 p.m. UTC | #6
Hi Sagi,

On Fri, May 08, 2020 at 03:19:45PM -0700, Sagi Grimberg wrote:
> Hey Ming,
> 
> > > Would it make sense to elevate this flag to a request_queue flag
> > > (QUEUE_FLAG_ALWAYS_COMMIT)?
> > 
> > request queue flag usually is writable, however this case just needs
> > one read-only flag, so I think it may be better to make it as
> > tagset/hctx flag.
> 
> I actually intended it to be writable.
> 
> > > I'm thinking of a possibility that an I/O scheduler may be used
> > > to activate this functionality rather than having the driver set
> > > it necessarily...
> > 
> > Could you explain a bit why I/O scheduler should activate this
> > functionality?
> 
> Sure, I've recently seen some academic work showing the benefits
> of batching in tcp/ip based block drivers. The problem with the
> approaches taken is that I/O scheduling is exercised deep down in the
> driver, which is not the direction I'd like to go if we are want
> to adopt some of the batching concepts.
> 
> I spent some (limited) time thinking about this, and it seems to
> me that there is an opportunity to implement this as a dedicated
> I/O scheduler, and tie it to driver specific LLD stack optimizations
> (net-stack for example) relying on the commit_rq/bd->last hints.
> 
> When scanning the scheduler code, I noticed exactly the phenomenon that
> this patchset is attempting to solve and Christoph referred me to it.
> Now I'm thinking if we can extend this batching optimization for both
> use-cases.

Got it, thanks for the sharing.

> 
> > batching submission may be good for some drivers, and currently
> > we only do it in limited way. One reason is that there is extra
> > cost for full batching submission, such as this patch requires
> > one extra .commit_rqs() for each dispatch, and lock is often needed
> > in this callback.
> 
> That is not necessarily the case at all.

So far, all in-tree .commit_rqs() implementation requires lock.

> 
> > IMO it can be a win for some slow driver or device, but may cause
> > a little performance drop for fast driver/device especially in workload
> > of not-batching submission.
> 
> You're mostly correct. This is exactly why an I/O scheduler may be
> applicable here IMO. Mostly because I/O schedulers tend to optimize for
> something specific and always present tradeoffs. Users need to
> understand what they are optimizing for.
> 
> Hence I'd say this functionality can definitely be available to an I/O
> scheduler should one exist.
> 

I guess it is just that there can be multiple requests available from
scheduler queue. Actually it can be so for other non-nvme drivers in
case of none, such as SCSI.

Another way is to use one per-task list(such as plug list) to hold the
requests for dispatch, then every drivers may see real .last flag, so they
may get chance for optimizing batch queuing. I will think about the
idea further and see if it is really doable.


Thanks,
Ming
Baolin Wang May 9, 2020, 8:57 a.m. UTC | #7
On Sat, May 9, 2020 at 7:22 AM Ming Lei <ming.lei@redhat.com> wrote:
>
> Hi Sagi,
>
> On Fri, May 08, 2020 at 03:19:45PM -0700, Sagi Grimberg wrote:
> > Hey Ming,
> >
> > > > Would it make sense to elevate this flag to a request_queue flag
> > > > (QUEUE_FLAG_ALWAYS_COMMIT)?
> > >
> > > request queue flag usually is writable, however this case just needs
> > > one read-only flag, so I think it may be better to make it as
> > > tagset/hctx flag.
> >
> > I actually intended it to be writable.
> >
> > > > I'm thinking of a possibility that an I/O scheduler may be used
> > > > to activate this functionality rather than having the driver set
> > > > it necessarily...
> > >
> > > Could you explain a bit why I/O scheduler should activate this
> > > functionality?
> >
> > Sure, I've recently seen some academic work showing the benefits
> > of batching in tcp/ip based block drivers. The problem with the
> > approaches taken is that I/O scheduling is exercised deep down in the
> > driver, which is not the direction I'd like to go if we are want
> > to adopt some of the batching concepts.
> >
> > I spent some (limited) time thinking about this, and it seems to
> > me that there is an opportunity to implement this as a dedicated
> > I/O scheduler, and tie it to driver specific LLD stack optimizations
> > (net-stack for example) relying on the commit_rq/bd->last hints.
> >
> > When scanning the scheduler code, I noticed exactly the phenomenon that
> > this patchset is attempting to solve and Christoph referred me to it.
> > Now I'm thinking if we can extend this batching optimization for both
> > use-cases.
>
> Got it, thanks for the sharing.
>
> >
> > > batching submission may be good for some drivers, and currently
> > > we only do it in limited way. One reason is that there is extra
> > > cost for full batching submission, such as this patch requires
> > > one extra .commit_rqs() for each dispatch, and lock is often needed
> > > in this callback.
> >
> > That is not necessarily the case at all.
>
> So far, all in-tree .commit_rqs() implementation requires lock.
>
> >
> > > IMO it can be a win for some slow driver or device, but may cause
> > > a little performance drop for fast driver/device especially in workload
> > > of not-batching submission.
> >
> > You're mostly correct. This is exactly why an I/O scheduler may be
> > applicable here IMO. Mostly because I/O schedulers tend to optimize for
> > something specific and always present tradeoffs. Users need to
> > understand what they are optimizing for.
> >
> > Hence I'd say this functionality can definitely be available to an I/O
> > scheduler should one exist.
> >
>
> I guess it is just that there can be multiple requests available from
> scheduler queue. Actually it can be so for other non-nvme drivers in
> case of none, such as SCSI.
>
> Another way is to use one per-task list(such as plug list) to hold the
> requests for dispatch, then every drivers may see real .last flag, so they
> may get chance for optimizing batch queuing. I will think about the
> idea further and see if it is really doable.

How about my RFC v1 patch set[1], which allows dispatching more than
one request from the scheduler to support batch requests?

[1]
https://lore.kernel.org/patchwork/patch/1210034/
https://lore.kernel.org/patchwork/patch/1210035/
Ming Lei May 9, 2020, 9:43 a.m. UTC | #8
On Sat, May 09, 2020 at 04:57:48PM +0800, Baolin Wang wrote:
> On Sat, May 9, 2020 at 7:22 AM Ming Lei <ming.lei@redhat.com> wrote:
> >
> > Hi Sagi,
> >
> > On Fri, May 08, 2020 at 03:19:45PM -0700, Sagi Grimberg wrote:
> > > Hey Ming,
> > >
> > > > > Would it make sense to elevate this flag to a request_queue flag
> > > > > (QUEUE_FLAG_ALWAYS_COMMIT)?
> > > >
> > > > request queue flag usually is writable, however this case just needs
> > > > one read-only flag, so I think it may be better to make it as
> > > > tagset/hctx flag.
> > >
> > > I actually intended it to be writable.
> > >
> > > > > I'm thinking of a possibility that an I/O scheduler may be used
> > > > > to activate this functionality rather than having the driver set
> > > > > it necessarily...
> > > >
> > > > Could you explain a bit why I/O scheduler should activate this
> > > > functionality?
> > >
> > > Sure, I've recently seen some academic work showing the benefits
> > > of batching in tcp/ip based block drivers. The problem with the
> > > approaches taken is that I/O scheduling is exercised deep down in the
> > > driver, which is not the direction I'd like to go if we are want
> > > to adopt some of the batching concepts.
> > >
> > > I spent some (limited) time thinking about this, and it seems to
> > > me that there is an opportunity to implement this as a dedicated
> > > I/O scheduler, and tie it to driver specific LLD stack optimizations
> > > (net-stack for example) relying on the commit_rq/bd->last hints.
> > >
> > > When scanning the scheduler code, I noticed exactly the phenomenon that
> > > this patchset is attempting to solve and Christoph referred me to it.
> > > Now I'm thinking if we can extend this batching optimization for both
> > > use-cases.
> >
> > Got it, thanks for the sharing.
> >
> > >
> > > > batching submission may be good for some drivers, and currently
> > > > we only do it in limited way. One reason is that there is extra
> > > > cost for full batching submission, such as this patch requires
> > > > one extra .commit_rqs() for each dispatch, and lock is often needed
> > > > in this callback.
> > >
> > > That is not necessarily the case at all.
> >
> > So far, all in-tree .commit_rqs() implementation requires lock.
> >
> > >
> > > > IMO it can be a win for some slow driver or device, but may cause
> > > > a little performance drop for fast driver/device especially in workload
> > > > of not-batching submission.
> > >
> > > You're mostly correct. This is exactly why an I/O scheduler may be
> > > applicable here IMO. Mostly because I/O schedulers tend to optimize for
> > > something specific and always present tradeoffs. Users need to
> > > understand what they are optimizing for.
> > >
> > > Hence I'd say this functionality can definitely be available to an I/O
> > > scheduler should one exist.
> > >
> >
> > I guess it is just that there can be multiple requests available from
> > scheduler queue. Actually it can be so for other non-nvme drivers in
> > case of none, such as SCSI.
> >
> > Another way is to use one per-task list(such as plug list) to hold the
> > requests for dispatch, then every drivers may see real .last flag, so they
> > may get chance for optimizing batch queuing. I will think about the
> > idea further and see if it is really doable.
> 
> How about my RFC v1 patch set[1], which allows dispatching more than
> one request from the scheduler to support batch requests?
> 
> [1]
> https://lore.kernel.org/patchwork/patch/1210034/
> https://lore.kernel.org/patchwork/patch/1210035/

Basically, my idea is to dequeue request one by one, and for each
dequeued request:

- we try to get a budget and driver tag, if both succeed, add the
request to one per-task list which can be stored in stack variable,
then continue to dequeue more request

- if either budget or driver tag can't be allocated for this request,
marks the last request in the per-task list as .last, and send the
batching requests stored in the list to LLD

- when queueing batching requests to LLD, if one request isn't queued
to driver successfully, calling .commit_rqs() like before, meantime
adding the remained requests in the per-task list back to scheduler
queue or hctx->dispatch.

One issue is that this way might degrade sequential IO performance if
the LLD just tells queue busy to blk-mq via return value of .queue_rq(),
so I guess we still may need one flag, such as BLK_MQ_F_BATCHING_SUBMISSION.


thanks, 
Ming
Sagi Grimberg May 10, 2020, 7:44 a.m. UTC | #9
>>>> You're mostly correct. This is exactly why an I/O scheduler may be
>>>> applicable here IMO. Mostly because I/O schedulers tend to optimize for
>>>> something specific and always present tradeoffs. Users need to
>>>> understand what they are optimizing for.
>>>>
>>>> Hence I'd say this functionality can definitely be available to an I/O
>>>> scheduler should one exist.
>>>>
>>>
>>> I guess it is just that there can be multiple requests available from
>>> scheduler queue. Actually it can be so for other non-nvme drivers in
>>> case of none, such as SCSI.
>>>
>>> Another way is to use one per-task list(such as plug list) to hold the
>>> requests for dispatch, then every drivers may see real .last flag, so they
>>> may get chance for optimizing batch queuing. I will think about the
>>> idea further and see if it is really doable.
>>
>> How about my RFC v1 patch set[1], which allows dispatching more than
>> one request from the scheduler to support batch requests?
>>
>> [1]
>> https://lore.kernel.org/patchwork/patch/1210034/
>> https://lore.kernel.org/patchwork/patch/1210035/
> 
> Basically, my idea is to dequeue request one by one, and for each
> dequeued request:
> 
> - we try to get a budget and driver tag, if both succeed, add the
> request to one per-task list which can be stored in stack variable,
> then continue to dequeue more request
> 
> - if either budget or driver tag can't be allocated for this request,
> marks the last request in the per-task list as .last, and send the
> batching requests stored in the list to LLD
> 
> - when queueing batching requests to LLD, if one request isn't queued
> to driver successfully, calling .commit_rqs() like before, meantime
> adding the remained requests in the per-task list back to scheduler
> queue or hctx->dispatch.

Sounds good to me.

> One issue is that this way might degrade sequential IO performance if
> the LLD just tells queue busy to blk-mq via return value of .queue_rq(),
> so I guess we still may need one flag, such as BLK_MQ_F_BATCHING_SUBMISSION.

Why is that degrading sequential I/O performance? because the specific
device will do better without batching submissions? If so, the driver
is not obligated to respect the bd->last/.commit_rqs, so if that is the
case, it should just ignore it.
Ming Lei May 11, 2020, 1:29 a.m. UTC | #10
On Sun, May 10, 2020 at 12:44:53AM -0700, Sagi Grimberg wrote:
> 
> > > > > You're mostly correct. This is exactly why an I/O scheduler may be
> > > > > applicable here IMO. Mostly because I/O schedulers tend to optimize for
> > > > > something specific and always present tradeoffs. Users need to
> > > > > understand what they are optimizing for.
> > > > > 
> > > > > Hence I'd say this functionality can definitely be available to an I/O
> > > > > scheduler should one exist.
> > > > > 
> > > > 
> > > > I guess it is just that there can be multiple requests available from
> > > > scheduler queue. Actually it can be so for other non-nvme drivers in
> > > > case of none, such as SCSI.
> > > > 
> > > > Another way is to use one per-task list(such as plug list) to hold the
> > > > requests for dispatch, then every drivers may see real .last flag, so they
> > > > may get chance for optimizing batch queuing. I will think about the
> > > > idea further and see if it is really doable.
> > > 
> > > How about my RFC v1 patch set[1], which allows dispatching more than
> > > one request from the scheduler to support batch requests?
> > > 
> > > [1]
> > > https://lore.kernel.org/patchwork/patch/1210034/
> > > https://lore.kernel.org/patchwork/patch/1210035/
> > 
> > Basically, my idea is to dequeue request one by one, and for each
> > dequeued request:
> > 
> > - we try to get a budget and driver tag, if both succeed, add the
> > request to one per-task list which can be stored in stack variable,
> > then continue to dequeue more request
> > 
> > - if either budget or driver tag can't be allocated for this request,
> > marks the last request in the per-task list as .last, and send the
> > batching requests stored in the list to LLD
> > 
> > - when queueing batching requests to LLD, if one request isn't queued
> > to driver successfully, calling .commit_rqs() like before, meantime
> > adding the remained requests in the per-task list back to scheduler
> > queue or hctx->dispatch.
> 
> Sounds good to me.
> 
> > One issue is that this way might degrade sequential IO performance if
> > the LLD just tells queue busy to blk-mq via return value of .queue_rq(),
> > so I guess we still may need one flag, such as BLK_MQ_F_BATCHING_SUBMISSION.
> 
> Why is that degrading sequential I/O performance? because the specific

Some devices may only return BLK_STS_RESOURCE from .queue_rq(), then more
requests are dequeued from scheduler queue if we always queue batching IOs
to LLD, and chance of IO merge is reduced, so sequential IO performance will
be effected.

Such as some scsi device which doesn't use sdev->queue_depth for
throttling IOs.

For virtio-scsi or virtio-blk, we may stop queue for avoiding the
potential affect.

> device will do better without batching submissions? If so, the driver

It isn't related with batching submission, IMO.


Thanks,
Ming
Sagi Grimberg May 11, 2020, 9:23 a.m. UTC | #11
>>> Basically, my idea is to dequeue request one by one, and for each
>>> dequeued request:
>>>
>>> - we try to get a budget and driver tag, if both succeed, add the
>>> request to one per-task list which can be stored in stack variable,
>>> then continue to dequeue more request
>>>
>>> - if either budget or driver tag can't be allocated for this request,
>>> marks the last request in the per-task list as .last, and send the
>>> batching requests stored in the list to LLD
>>>
>>> - when queueing batching requests to LLD, if one request isn't queued
>>> to driver successfully, calling .commit_rqs() like before, meantime
>>> adding the remained requests in the per-task list back to scheduler
>>> queue or hctx->dispatch.
>>
>> Sounds good to me.
>>
>>> One issue is that this way might degrade sequential IO performance if
>>> the LLD just tells queue busy to blk-mq via return value of .queue_rq(),
>>> so I guess we still may need one flag, such as BLK_MQ_F_BATCHING_SUBMISSION.
>>
>> Why is that degrading sequential I/O performance? because the specific
> 
> Some devices may only return BLK_STS_RESOURCE from .queue_rq(), then more
> requests are dequeued from scheduler queue if we always queue batching IOs
> to LLD, and chance of IO merge is reduced, so sequential IO performance will
> be effected.
> 
> Such as some scsi device which doesn't use sdev->queue_depth for
> throttling IOs.
> 
> For virtio-scsi or virtio-blk, we may stop queue for avoiding the
> potential affect.

Do we have a way to characterize such devices? I'd assume that most
devices will benefit from the batching so maybe the flag needs to be
inverted? BLK_MQ_F_DONT_BATCHING_SUBMISSION?
Ming Lei May 11, 2020, 11:47 a.m. UTC | #12
On Mon, May 11, 2020 at 02:23:14AM -0700, Sagi Grimberg wrote:
> 
> > > > Basically, my idea is to dequeue request one by one, and for each
> > > > dequeued request:
> > > > 
> > > > - we try to get a budget and driver tag, if both succeed, add the
> > > > request to one per-task list which can be stored in stack variable,
> > > > then continue to dequeue more request
> > > > 
> > > > - if either budget or driver tag can't be allocated for this request,
> > > > marks the last request in the per-task list as .last, and send the
> > > > batching requests stored in the list to LLD
> > > > 
> > > > - when queueing batching requests to LLD, if one request isn't queued
> > > > to driver successfully, calling .commit_rqs() like before, meantime
> > > > adding the remained requests in the per-task list back to scheduler
> > > > queue or hctx->dispatch.
> > > 
> > > Sounds good to me.
> > > 
> > > > One issue is that this way might degrade sequential IO performance if
> > > > the LLD just tells queue busy to blk-mq via return value of .queue_rq(),
> > > > so I guess we still may need one flag, such as BLK_MQ_F_BATCHING_SUBMISSION.
> > > 
> > > Why is that degrading sequential I/O performance? because the specific
> > 
> > Some devices may only return BLK_STS_RESOURCE from .queue_rq(), then more
> > requests are dequeued from scheduler queue if we always queue batching IOs
> > to LLD, and chance of IO merge is reduced, so sequential IO performance will
> > be effected.
> > 
> > Such as some scsi device which doesn't use sdev->queue_depth for
> > throttling IOs.
> > 
> > For virtio-scsi or virtio-blk, we may stop queue for avoiding the
> > potential affect.
> 
> Do we have a way to characterize such devices? I'd assume that most

It may not be easy.

> devices will benefit from the batching so maybe the flag needs to be
> inverted? BLK_MQ_F_DONT_BATCHING_SUBMISSION?

Actually I'd rather to not add any flag, and we may use some algorithm
(maybe EWMA or other intelligent stuff, or use hctx->dispatch_busy directly)
to figure out one dynamic batching number which is used to dequeue requests
from scheduler or sw queue.

Then just one single dispatch io code path is enough.

Thanks, 
Ming
Sagi Grimberg May 12, 2020, 6:26 a.m. UTC | #13
>> devices will benefit from the batching so maybe the flag needs to be
>> inverted? BLK_MQ_F_DONT_BATCHING_SUBMISSION?
> 
> Actually I'd rather to not add any flag, and we may use some algorithm
> (maybe EWMA or other intelligent stuff, or use hctx->dispatch_busy directly)
> to figure out one dynamic batching number which is used to dequeue requests
> from scheduler or sw queue.
> 
> Then just one single dispatch io code path is enough.

Sounds good to me, do you plan to submit a patchset?
Ming Lei May 12, 2020, 7:55 a.m. UTC | #14
On Mon, May 11, 2020 at 11:26:07PM -0700, Sagi Grimberg wrote:
> 
> > > devices will benefit from the batching so maybe the flag needs to be
> > > inverted? BLK_MQ_F_DONT_BATCHING_SUBMISSION?
> > 
> > Actually I'd rather to not add any flag, and we may use some algorithm
> > (maybe EWMA or other intelligent stuff, or use hctx->dispatch_busy directly)
> > to figure out one dynamic batching number which is used to dequeue requests
> > from scheduler or sw queue.
> > 
> > Then just one single dispatch io code path is enough.
> 
> Sounds good to me, do you plan to submit a patchset?
> 

Yeah, I am working on that.


Thanks,
Ming
diff mbox series

Patch

diff --git a/block/blk-mq-sched.c b/block/blk-mq-sched.c
index 74cedea56034..3429a71a7364 100644
--- a/block/blk-mq-sched.c
+++ b/block/blk-mq-sched.c
@@ -85,11 +85,12 @@  void blk_mq_sched_restart(struct blk_mq_hw_ctx *hctx)
  * its queue by itself in its completion handler, so we don't need to
  * restart queue if .get_budget() returns BLK_STS_NO_RESOURCE.
  */
-static void blk_mq_do_dispatch_sched(struct blk_mq_hw_ctx *hctx)
+static bool blk_mq_do_dispatch_sched(struct blk_mq_hw_ctx *hctx)
 {
 	struct request_queue *q = hctx->queue;
 	struct elevator_queue *e = q->elevator;
 	LIST_HEAD(rq_list);
+	bool ret = false;
 
 	do {
 		struct request *rq;
@@ -112,7 +113,10 @@  static void blk_mq_do_dispatch_sched(struct blk_mq_hw_ctx *hctx)
 		 * in blk_mq_dispatch_rq_list().
 		 */
 		list_add(&rq->queuelist, &rq_list);
-	} while (blk_mq_dispatch_rq_list(q, &rq_list, true));
+		ret = blk_mq_dispatch_rq_list(q, &rq_list, true);
+	} while (ret);
+
+	return ret;
 }
 
 static struct blk_mq_ctx *blk_mq_next_ctx(struct blk_mq_hw_ctx *hctx,
@@ -131,11 +135,12 @@  static struct blk_mq_ctx *blk_mq_next_ctx(struct blk_mq_hw_ctx *hctx,
  * its queue by itself in its completion handler, so we don't need to
  * restart queue if .get_budget() returns BLK_STS_NO_RESOURCE.
  */
-static void blk_mq_do_dispatch_ctx(struct blk_mq_hw_ctx *hctx)
+static bool blk_mq_do_dispatch_ctx(struct blk_mq_hw_ctx *hctx)
 {
 	struct request_queue *q = hctx->queue;
 	LIST_HEAD(rq_list);
 	struct blk_mq_ctx *ctx = READ_ONCE(hctx->dispatch_from);
+	bool ret = false;
 
 	do {
 		struct request *rq;
@@ -161,10 +166,11 @@  static void blk_mq_do_dispatch_ctx(struct blk_mq_hw_ctx *hctx)
 
 		/* round robin for fair dispatch */
 		ctx = blk_mq_next_ctx(hctx, rq->mq_ctx);
-
-	} while (blk_mq_dispatch_rq_list(q, &rq_list, true));
+		ret = blk_mq_dispatch_rq_list(q, &rq_list, true);
+	} while (ret);
 
 	WRITE_ONCE(hctx->dispatch_from, ctx);
+	return ret;
 }
 
 void blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx *hctx)
@@ -173,6 +179,7 @@  void blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx *hctx)
 	struct elevator_queue *e = q->elevator;
 	const bool has_sched_dispatch = e && e->type->ops.dispatch_request;
 	LIST_HEAD(rq_list);
+	bool dispatch_ret;
 
 	/* RCU or SRCU read lock is needed before checking quiesced flag */
 	if (unlikely(blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q)))
@@ -206,21 +213,25 @@  void blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx *hctx)
 	 */
 	if (!list_empty(&rq_list)) {
 		blk_mq_sched_mark_restart_hctx(hctx);
-		if (blk_mq_dispatch_rq_list(q, &rq_list, false)) {
+		dispatch_ret = blk_mq_dispatch_rq_list(q, &rq_list, false);
+		if (dispatch_ret) {
 			if (has_sched_dispatch)
 				blk_mq_do_dispatch_sched(hctx);
 			else
 				blk_mq_do_dispatch_ctx(hctx);
 		}
 	} else if (has_sched_dispatch) {
-		blk_mq_do_dispatch_sched(hctx);
+		dispatch_ret = blk_mq_do_dispatch_sched(hctx);
 	} else if (hctx->dispatch_busy) {
 		/* dequeue request one by one from sw queue if queue is busy */
-		blk_mq_do_dispatch_ctx(hctx);
+		dispatch_ret = blk_mq_do_dispatch_ctx(hctx);
 	} else {
 		blk_mq_flush_busy_ctxs(hctx, &rq_list);
-		blk_mq_dispatch_rq_list(q, &rq_list, false);
+		dispatch_ret = blk_mq_dispatch_rq_list(q, &rq_list, false);
 	}
+
+	if (dispatch_ret && (hctx->flags & BLK_MQ_F_FORCE_COMMIT_RQS))
+		hctx->queue->mq_ops->commit_rqs(hctx);
 }
 
 bool blk_mq_sched_try_merge(struct request_queue *q, struct bio *bio,
diff --git a/block/blk-mq.c b/block/blk-mq.c
index 8e56884fd2e9..bde122feef01 100644
--- a/block/blk-mq.c
+++ b/block/blk-mq.c
@@ -1253,11 +1253,16 @@  bool blk_mq_dispatch_rq_list(struct request_queue *q, struct list_head *list,
 		 * Flag last if we have no more requests, or if we have more
 		 * but can't assign a driver tag to it.
 		 */
-		if (list_empty(list))
-			bd.last = true;
-		else {
-			nxt = list_first_entry(list, struct request, queuelist);
-			bd.last = !blk_mq_get_driver_tag(nxt);
+		if (!(hctx->flags & BLK_MQ_F_FORCE_COMMIT_RQS)) {
+			if (list_empty(list)) {
+				bd.last = true;
+			} else {
+				nxt = list_first_entry(list, struct request,
+						       queuelist);
+				bd.last = !blk_mq_get_driver_tag(nxt);
+			}
+		} else {
+			bd.last = false;
 		}
 
 		ret = q->mq_ops->queue_rq(hctx, &bd);
diff --git a/include/linux/blk-mq.h b/include/linux/blk-mq.h
index f389d7c724bd..6a20f8e8eb85 100644
--- a/include/linux/blk-mq.h
+++ b/include/linux/blk-mq.h
@@ -391,6 +391,7 @@  struct blk_mq_ops {
 enum {
 	BLK_MQ_F_SHOULD_MERGE	= 1 << 0,
 	BLK_MQ_F_TAG_SHARED	= 1 << 1,
+	BLK_MQ_F_FORCE_COMMIT_RQS = 1 << 3,
 	BLK_MQ_F_BLOCKING	= 1 << 5,
 	BLK_MQ_F_NO_SCHED	= 1 << 6,
 	BLK_MQ_F_ALLOC_POLICY_START_BIT = 8,