mbox series

[v6,0/4] Add MMC software queue support

Message ID cover.1573456283.git.baolin.wang@linaro.org
Headers show
Series Add MMC software queue support | expand

Message

(Exiting) Baolin Wang Nov. 11, 2019, 7:33 a.m. UTC
Hi All,

Now the MMC read/write stack will always wait for previous request is
completed by mmc_blk_rw_wait(), before sending a new request to hardware,
or queue a work to complete request, that will bring context switching
overhead, especially for high I/O per second rates, to affect the IO
performance.

Thus this patch set will introduce the MMC software command queue support
based on command queue engine's interfaces, and set the queue depth as 32
to allow more requests can be be prepared, merged and inserted into IO
scheduler, but we only allow 2 requests in flight, that is enough to let
the irq handler always trigger the next request without a context switch,
as well as avoiding a long latency.

Moreover we can expand the MMC software queue interface to support
MMC packed request or packed command instead of adding new interfaces,
according to previosus discussion.

Below are some comparison data with fio tool. The fio command I used
is like below with changing the '--rw' parameter and enabling the direct
IO flag to measure the actual hardware transfer speed in 4K block size.

./fio --filename=/dev/mmcblk0p30 --direct=1 --iodepth=20 --rw=read --bs=4K --size=1G --group_reporting --numjobs=20 --name=test_read

My eMMC card working at HS400 Enhanced strobe mode:
[    2.229856] mmc0: new HS400 Enhanced strobe MMC card at address 0001
[    2.237566] mmcblk0: mmc0:0001 HBG4a2 29.1 GiB 
[    2.242621] mmcblk0boot0: mmc0:0001 HBG4a2 partition 1 4.00 MiB
[    2.249110] mmcblk0boot1: mmc0:0001 HBG4a2 partition 2 4.00 MiB
[    2.255307] mmcblk0rpmb: mmc0:0001 HBG4a2 partition 3 4.00 MiB, chardev (248:0)

1. Without MMC software queue
I tested 5 times for each case and output a average speed.

1) Sequential read:
Speed: 59.4MiB/s, 63.4MiB/s, 57.5MiB/s, 57.2MiB/s, 60.8MiB/s
Average speed: 59.66MiB/s

2) Random read:
Speed: 26.9MiB/s, 26.9MiB/s, 27.1MiB/s, 27.1MiB/s, 27.2MiB/s
Average speed: 27.04MiB/s

3) Sequential write:
Speed: 71.6MiB/s, 72.5MiB/s, 72.2MiB/s, 64.6MiB/s, 67.5MiB/s
Average speed: 69.68MiB/s

4) Random write:
Speed: 36.3MiB/s, 35.4MiB/s, 38.6MiB/s, 34MiB/s, 35.5MiB/s
Average speed: 35.96MiB/s

2. With MMC software queue
I tested 5 times for each case and output a average speed.

1) Sequential read:
Speed: 59.2MiB/s, 60.4MiB/s, 63.6MiB/s, 60.3MiB/s, 59.9MiB/s
Average speed: 60.68MiB/s

2) Random read:
Speed: 31.3MiB/s, 31.4MiB/s, 31.5MiB/s, 31.3MiB/s, 31.3MiB/s
Average speed: 31.36MiB/s

3) Sequential write:
Speed: 71MiB/s, 71.8MiB/s, 72.3MiB/s, 72.2MiB/s, 71MiB/s
Average speed: 71.66MiB/s

4) Random write:
Speed: 68.9MiB/s, 68.7MiB/s, 68.8MiB/s, 68.6MiB/s, 68.8MiB/s
Average speed: 68.76MiB/s

Form above data, we can see the MMC software queue can help to improve some
performance obviously for random read and write, though no obvious improvement
for sequential read and write.

Any comments are welcome. Thanks a lot.

Hi Ulf,

This patch set was pending for a while, and I've tested it several times and
have not found any recessions. Hope this patch set can be merged into v5.5
if no objection from you, since I still have some patches introducing the
packed request depend on the mmc software queue as we talked before.
Thanks a lot.

Changes from v5:
 - Modify the condition of defering to complete request suggested by Adrian.

Changes from v4:
 - Add a seperate patch to introduce a variable to defer to complete
 data requests for some host drivers, when using host software queue.

Changes from v3:
 - Use host software queue instead of sqhci.
 - Fix random config building issue.
 - Change queue depth to 32, but still only allow 2 requests in flight.
 - Update the testing data.

Changes from v2:
 - Remove reference to 'struct cqhci_host' and 'struct cqhci_slot',
 instead adding 'struct sqhci_host', which is only used by software queue.

Changes from v1:
 - Add request_done ops for sdhci_ops.
 - Replace virtual command queue with software queue for functions and
 variables.
 - Rename the software queue file and add sqhci.h header file.

Baolin Wang (4):
  mmc: Add MMC host software queue support
  mmc: host: sdhci: Add request_done ops for struct sdhci_ops
  mmc: host: sdhci-sprd: Add software queue support
  mmc: host: sdhci: Add a variable to defer to complete requests if
    needed

 drivers/mmc/core/block.c      |   61 ++++++++
 drivers/mmc/core/mmc.c        |   13 +-
 drivers/mmc/core/queue.c      |   33 +++-
 drivers/mmc/host/Kconfig      |    8 +
 drivers/mmc/host/Makefile     |    1 +
 drivers/mmc/host/mmc_hsq.c    |  344 +++++++++++++++++++++++++++++++++++++++++
 drivers/mmc/host/mmc_hsq.h    |   30 ++++
 drivers/mmc/host/sdhci-sprd.c |   26 ++++
 drivers/mmc/host/sdhci.c      |   14 +-
 drivers/mmc/host/sdhci.h      |    3 +
 include/linux/mmc/host.h      |    3 +
 11 files changed, 523 insertions(+), 13 deletions(-)
 create mode 100644 drivers/mmc/host/mmc_hsq.c
 create mode 100644 drivers/mmc/host/mmc_hsq.h

-- 
1.7.9.5

Comments

Arnd Bergmann Nov. 11, 2019, 9:28 a.m. UTC | #1
On Mon, Nov 11, 2019 at 8:35 AM Baolin Wang <baolin.wang@linaro.org> wrote:
>

> Hi All,

>

> Now the MMC read/write stack will always wait for previous request is

> completed by mmc_blk_rw_wait(), before sending a new request to hardware,

> or queue a work to complete request, that will bring context switching

> overhead, especially for high I/O per second rates, to affect the IO

> performance.


Hi Baolin,

I had a chance to discuss your changes and what other improvements
can be done to the way mmc-blk works with Hannes Reinecke during the ELC
conference. He had some good suggestions. Adding him and the linux-block
mailing list to Cc to make sure I'm correctly representing this.

- For the queue_depth of a non-queuing block device, you indeed need to
  leave it at e.g. 32 or 64 rather than 1 or 2, as you do now (I was wrong
  here originally, but just confirmed that). The queue depth is just used to
  ensure there is room for reordering and merging, as you also noticed.

- Removing all the context switches and workqueues from the data submission
  path is also the right idea. As you found, there is still a workqueue inside
  of blk_mq that is used because it may get called from atomic context but
  the submission may get blocked in __mmc_claim_host(). This really
  needs to be changed as well, but not in the way I originally suggested:
  As Hannes suggested, the host interrrupt handler should always use
  request_threaded_irq() to have its own process context, and then pass a
  flag to blk_mq to say that we never need another workqueue there.

- With that change in place calling a blocking __mmc_claim_host() is
  still a problem, so there should still be a nonblocking mmc_try_claim_host()
  for the submission path, leading to a BLK_STS_DEV_RESOURCE (?)
  return code from mmc_mq_queue_rq(). Basically mmc_mq_queue_rq()
  should always return right away, either after having queued the next I/O
  or with an error, but not waiting for the device in any way.

- For the packed requests, there is apparently a very simple way to implement
  that without a software queue: mmc_mq_queue_rq() is allowed to look at
  and dequeue all requests that are currently part of the request_queue,
  so it should take out as many as it wants to submit at once and send
  them all down to the driver together, avoiding the need for any further
  round-trips to blk_mq or maintaining a queue in mmc.

- The DMA management (bounce buffer, map, unmap) that is currently
  done in mmc_blk_mq_issue_rq() should ideally be done in the
  init_request()/exit_request()  (?) callbacks from mmc_mq_ops so this
  can be done asynchronously, out of the critical timing path for the
  submission. With this, there won't be any need for a software queue.

Hannes,

Let me know if I misunderstood any of the above, or if I missed any
additional points.

       Arnd

> Thus this patch set will introduce the MMC software command queue support

> based on command queue engine's interfaces, and set the queue depth as 32

> to allow more requests can be be prepared, merged and inserted into IO

> scheduler, but we only allow 2 requests in flight, that is enough to let

> the irq handler always trigger the next request without a context switch,

> as well as avoiding a long latency.

>

> Moreover we can expand the MMC software queue interface to support

> MMC packed request or packed command instead of adding new interfaces,

> according to previosus discussion.

>

> Below are some comparison data with fio tool. The fio command I used

> is like below with changing the '--rw' parameter and enabling the direct

> IO flag to measure the actual hardware transfer speed in 4K block size.

>

> ./fio --filename=/dev/mmcblk0p30 --direct=1 --iodepth=20 --rw=read --bs=4K --size=1G --group_reporting --numjobs=20 --name=test_read

>

> My eMMC card working at HS400 Enhanced strobe mode:

> [    2.229856] mmc0: new HS400 Enhanced strobe MMC card at address 0001

> [    2.237566] mmcblk0: mmc0:0001 HBG4a2 29.1 GiB

> [    2.242621] mmcblk0boot0: mmc0:0001 HBG4a2 partition 1 4.00 MiB

> [    2.249110] mmcblk0boot1: mmc0:0001 HBG4a2 partition 2 4.00 MiB

> [    2.255307] mmcblk0rpmb: mmc0:0001 HBG4a2 partition 3 4.00 MiB, chardev (248:0)

>

> 1. Without MMC software queue

> I tested 5 times for each case and output a average speed.

>

> 1) Sequential read:

> Speed: 59.4MiB/s, 63.4MiB/s, 57.5MiB/s, 57.2MiB/s, 60.8MiB/s

> Average speed: 59.66MiB/s

>

> 2) Random read:

> Speed: 26.9MiB/s, 26.9MiB/s, 27.1MiB/s, 27.1MiB/s, 27.2MiB/s

> Average speed: 27.04MiB/s

>

> 3) Sequential write:

> Speed: 71.6MiB/s, 72.5MiB/s, 72.2MiB/s, 64.6MiB/s, 67.5MiB/s

> Average speed: 69.68MiB/s

>

> 4) Random write:

> Speed: 36.3MiB/s, 35.4MiB/s, 38.6MiB/s, 34MiB/s, 35.5MiB/s

> Average speed: 35.96MiB/s

>

> 2. With MMC software queue

> I tested 5 times for each case and output a average speed.

>

> 1) Sequential read:

> Speed: 59.2MiB/s, 60.4MiB/s, 63.6MiB/s, 60.3MiB/s, 59.9MiB/s

> Average speed: 60.68MiB/s

>

> 2) Random read:

> Speed: 31.3MiB/s, 31.4MiB/s, 31.5MiB/s, 31.3MiB/s, 31.3MiB/s

> Average speed: 31.36MiB/s

>

> 3) Sequential write:

> Speed: 71MiB/s, 71.8MiB/s, 72.3MiB/s, 72.2MiB/s, 71MiB/s

> Average speed: 71.66MiB/s

>

> 4) Random write:

> Speed: 68.9MiB/s, 68.7MiB/s, 68.8MiB/s, 68.6MiB/s, 68.8MiB/s

> Average speed: 68.76MiB/s

>

> Form above data, we can see the MMC software queue can help to improve some

> performance obviously for random read and write, though no obvious improvement

> for sequential read and write.

>

> Any comments are welcome. Thanks a lot.

>

> Hi Ulf,

>

> This patch set was pending for a while, and I've tested it several times and

> have not found any recessions. Hope this patch set can be merged into v5.5

> if no objection from you, since I still have some patches introducing the

> packed request depend on the mmc software queue as we talked before.

> Thanks a lot.

>

> Changes from v5:

>  - Modify the condition of defering to complete request suggested by Adrian.

>

> Changes from v4:

>  - Add a seperate patch to introduce a variable to defer to complete

>  data requests for some host drivers, when using host software queue.

>

> Changes from v3:

>  - Use host software queue instead of sqhci.

>  - Fix random config building issue.

>  - Change queue depth to 32, but still only allow 2 requests in flight.

>  - Update the testing data.

>

> Changes from v2:

>  - Remove reference to 'struct cqhci_host' and 'struct cqhci_slot',

>  instead adding 'struct sqhci_host', which is only used by software queue.

>

> Changes from v1:

>  - Add request_done ops for sdhci_ops.

>  - Replace virtual command queue with software queue for functions and

>  variables.

>  - Rename the software queue file and add sqhci.h header file.

>

> Baolin Wang (4):

>   mmc: Add MMC host software queue support

>   mmc: host: sdhci: Add request_done ops for struct sdhci_ops

>   mmc: host: sdhci-sprd: Add software queue support

>   mmc: host: sdhci: Add a variable to defer to complete requests if

>     needed

>

>  drivers/mmc/core/block.c      |   61 ++++++++

>  drivers/mmc/core/mmc.c        |   13 +-

>  drivers/mmc/core/queue.c      |   33 +++-

>  drivers/mmc/host/Kconfig      |    8 +

>  drivers/mmc/host/Makefile     |    1 +

>  drivers/mmc/host/mmc_hsq.c    |  344 +++++++++++++++++++++++++++++++++++++++++

>  drivers/mmc/host/mmc_hsq.h    |   30 ++++

>  drivers/mmc/host/sdhci-sprd.c |   26 ++++

>  drivers/mmc/host/sdhci.c      |   14 +-

>  drivers/mmc/host/sdhci.h      |    3 +

>  include/linux/mmc/host.h      |    3 +

>  11 files changed, 523 insertions(+), 13 deletions(-)

>  create mode 100644 drivers/mmc/host/mmc_hsq.c

>  create mode 100644 drivers/mmc/host/mmc_hsq.h

>

> --

> 1.7.9.5

>
(Exiting) Baolin Wang Nov. 11, 2019, 12:58 p.m. UTC | #2
Hi Arnd,

On Mon, 11 Nov 2019 at 17:28, Arnd Bergmann <arnd@arndb.de> wrote:
>

> On Mon, Nov 11, 2019 at 8:35 AM Baolin Wang <baolin.wang@linaro.org> wrote:

> >

> > Hi All,

> >

> > Now the MMC read/write stack will always wait for previous request is

> > completed by mmc_blk_rw_wait(), before sending a new request to hardware,

> > or queue a work to complete request, that will bring context switching

> > overhead, especially for high I/O per second rates, to affect the IO

> > performance.

>

> Hi Baolin,

>

> I had a chance to discuss your changes and what other improvements

> can be done to the way mmc-blk works with Hannes Reinecke during the ELC

> conference. He had some good suggestions. Adding him and the linux-block

> mailing list to Cc to make sure I'm correctly representing this.


Great, thanks for your input.

>

> - For the queue_depth of a non-queuing block device, you indeed need to

>   leave it at e.g. 32 or 64 rather than 1 or 2, as you do now (I was wrong

>   here originally, but just confirmed that). The queue depth is just used to

>   ensure there is room for reordering and merging, as you also noticed.


Right.

>

> - Removing all the context switches and workqueues from the data submission

>   path is also the right idea. As you found, there is still a workqueue inside

>   of blk_mq that is used because it may get called from atomic context but

>   the submission may get blocked in __mmc_claim_host(). This really

>   needs to be changed as well, but not in the way I originally suggested:

>   As Hannes suggested, the host interrrupt handler should always use

>   request_threaded_irq() to have its own process context, and then pass a

>   flag to blk_mq to say that we never need another workqueue there.


So you mean we should complete the request in the host driver irq
thread context, then issue another request in this context by calling
blk_mq_run_hw_queues()?

>

> - With that change in place calling a blocking __mmc_claim_host() is

>   still a problem, so there should still be a nonblocking mmc_try_claim_host()

>   for the submission path, leading to a BLK_STS_DEV_RESOURCE (?)

>   return code from mmc_mq_queue_rq(). Basically mmc_mq_queue_rq()

>   should always return right away, either after having queued the next I/O

>   or with an error, but not waiting for the device in any way.


Actually not only the mmc_claim_host() will block the MMC request
processing, in this routine, the mmc_blk_part_switch() and
mmc_retune() can also block the request processing. Moreover the part
switching and tuning should be sync operations, and we can not move
them to a work or a thread.

>

> - For the packed requests, there is apparently a very simple way to implement

>   that without a software queue: mmc_mq_queue_rq() is allowed to look at

>   and dequeue all requests that are currently part of the request_queue,

>   so it should take out as many as it wants to submit at once and send

>   them all down to the driver together, avoiding the need for any further

>   round-trips to blk_mq or maintaining a queue in mmc.


You mean we can dispatch a request directly from
elevator->type->ops.dispatch_request()?  but we still need some helper
functions to check if these requests can be packed (the package
condition), and need to invent new APIs to start a packed request (or
using cqe interfaces, which means we still need to implement some cqe
callbacks).

>

> - The DMA management (bounce buffer, map, unmap) that is currently

>   done in mmc_blk_mq_issue_rq() should ideally be done in the

>   init_request()/exit_request()  (?) callbacks from mmc_mq_ops so this

>   can be done asynchronously, out of the critical timing path for the

>   submission. With this, there won't be any need for a software queue.


This is not true, now the blk-mq will allocate some static request
objects (usually the static requests number should be the same with
the hardware queue depth) saved in struct blk_mq_tags. So the
init_request() is used to initialize the static requests when
allocating them, and call exit_request to free the static requests
when freeing the 'struct blk_mq_tags', such as the queue is dead. So
we can not move the DMA management into the init_request/exit_request.

>

> Hannes,

>

> Let me know if I misunderstood any of the above, or if I missed any

> additional points.

>

>        Arnd

>

> > Thus this patch set will introduce the MMC software command queue support

> > based on command queue engine's interfaces, and set the queue depth as 32

> > to allow more requests can be be prepared, merged and inserted into IO

> > scheduler, but we only allow 2 requests in flight, that is enough to let

> > the irq handler always trigger the next request without a context switch,

> > as well as avoiding a long latency.

> >

> > Moreover we can expand the MMC software queue interface to support

> > MMC packed request or packed command instead of adding new interfaces,

> > according to previosus discussion.

> >

> > Below are some comparison data with fio tool. The fio command I used

> > is like below with changing the '--rw' parameter and enabling the direct

> > IO flag to measure the actual hardware transfer speed in 4K block size.

> >

> > ./fio --filename=/dev/mmcblk0p30 --direct=1 --iodepth=20 --rw=read --bs=4K --size=1G --group_reporting --numjobs=20 --name=test_read

> >

> > My eMMC card working at HS400 Enhanced strobe mode:

> > [    2.229856] mmc0: new HS400 Enhanced strobe MMC card at address 0001

> > [    2.237566] mmcblk0: mmc0:0001 HBG4a2 29.1 GiB

> > [    2.242621] mmcblk0boot0: mmc0:0001 HBG4a2 partition 1 4.00 MiB

> > [    2.249110] mmcblk0boot1: mmc0:0001 HBG4a2 partition 2 4.00 MiB

> > [    2.255307] mmcblk0rpmb: mmc0:0001 HBG4a2 partition 3 4.00 MiB, chardev (248:0)

> >

> > 1. Without MMC software queue

> > I tested 5 times for each case and output a average speed.

> >

> > 1) Sequential read:

> > Speed: 59.4MiB/s, 63.4MiB/s, 57.5MiB/s, 57.2MiB/s, 60.8MiB/s

> > Average speed: 59.66MiB/s

> >

> > 2) Random read:

> > Speed: 26.9MiB/s, 26.9MiB/s, 27.1MiB/s, 27.1MiB/s, 27.2MiB/s

> > Average speed: 27.04MiB/s

> >

> > 3) Sequential write:

> > Speed: 71.6MiB/s, 72.5MiB/s, 72.2MiB/s, 64.6MiB/s, 67.5MiB/s

> > Average speed: 69.68MiB/s

> >

> > 4) Random write:

> > Speed: 36.3MiB/s, 35.4MiB/s, 38.6MiB/s, 34MiB/s, 35.5MiB/s

> > Average speed: 35.96MiB/s

> >

> > 2. With MMC software queue

> > I tested 5 times for each case and output a average speed.

> >

> > 1) Sequential read:

> > Speed: 59.2MiB/s, 60.4MiB/s, 63.6MiB/s, 60.3MiB/s, 59.9MiB/s

> > Average speed: 60.68MiB/s

> >

> > 2) Random read:

> > Speed: 31.3MiB/s, 31.4MiB/s, 31.5MiB/s, 31.3MiB/s, 31.3MiB/s

> > Average speed: 31.36MiB/s

> >

> > 3) Sequential write:

> > Speed: 71MiB/s, 71.8MiB/s, 72.3MiB/s, 72.2MiB/s, 71MiB/s

> > Average speed: 71.66MiB/s

> >

> > 4) Random write:

> > Speed: 68.9MiB/s, 68.7MiB/s, 68.8MiB/s, 68.6MiB/s, 68.8MiB/s

> > Average speed: 68.76MiB/s

> >

> > Form above data, we can see the MMC software queue can help to improve some

> > performance obviously for random read and write, though no obvious improvement

> > for sequential read and write.

> >

> > Any comments are welcome. Thanks a lot.

> >

> > Hi Ulf,

> >

> > This patch set was pending for a while, and I've tested it several times and

> > have not found any recessions. Hope this patch set can be merged into v5.5

> > if no objection from you, since I still have some patches introducing the

> > packed request depend on the mmc software queue as we talked before.

> > Thanks a lot.

> >

> > Changes from v5:

> >  - Modify the condition of defering to complete request suggested by Adrian.

> >

> > Changes from v4:

> >  - Add a seperate patch to introduce a variable to defer to complete

> >  data requests for some host drivers, when using host software queue.

> >

> > Changes from v3:

> >  - Use host software queue instead of sqhci.

> >  - Fix random config building issue.

> >  - Change queue depth to 32, but still only allow 2 requests in flight.

> >  - Update the testing data.

> >

> > Changes from v2:

> >  - Remove reference to 'struct cqhci_host' and 'struct cqhci_slot',

> >  instead adding 'struct sqhci_host', which is only used by software queue.

> >

> > Changes from v1:

> >  - Add request_done ops for sdhci_ops.

> >  - Replace virtual command queue with software queue for functions and

> >  variables.

> >  - Rename the software queue file and add sqhci.h header file.

> >

> > Baolin Wang (4):

> >   mmc: Add MMC host software queue support

> >   mmc: host: sdhci: Add request_done ops for struct sdhci_ops

> >   mmc: host: sdhci-sprd: Add software queue support

> >   mmc: host: sdhci: Add a variable to defer to complete requests if

> >     needed

> >

> >  drivers/mmc/core/block.c      |   61 ++++++++

> >  drivers/mmc/core/mmc.c        |   13 +-

> >  drivers/mmc/core/queue.c      |   33 +++-

> >  drivers/mmc/host/Kconfig      |    8 +

> >  drivers/mmc/host/Makefile     |    1 +

> >  drivers/mmc/host/mmc_hsq.c    |  344 +++++++++++++++++++++++++++++++++++++++++

> >  drivers/mmc/host/mmc_hsq.h    |   30 ++++

> >  drivers/mmc/host/sdhci-sprd.c |   26 ++++

> >  drivers/mmc/host/sdhci.c      |   14 +-

> >  drivers/mmc/host/sdhci.h      |    3 +

> >  include/linux/mmc/host.h      |    3 +

> >  11 files changed, 523 insertions(+), 13 deletions(-)

> >  create mode 100644 drivers/mmc/host/mmc_hsq.c

> >  create mode 100644 drivers/mmc/host/mmc_hsq.h

> >

> > --

> > 1.7.9.5

> >




-- 
Baolin Wang
Best Regards
Baolin Wang Nov. 12, 2019, 8:48 a.m. UTC | #3
On Tue, Nov 12, 2019 at 12:59 AM Arnd Bergmann <arnd@arndb.de> wrote:
>

> On Mon, Nov 11, 2019 at 1:58 PM Baolin Wang <baolin.wang@linaro.org> wrote:

> > On Mon, 11 Nov 2019 at 17:28, Arnd Bergmann <arnd@arndb.de> wrote:

> > > On Mon, Nov 11, 2019 at 8:35 AM Baolin Wang <baolin.wang@linaro.org> wrote:

> > > - Removing all the context switches and workqueues from the data submission

> > >   path is also the right idea. As you found, there is still a workqueue inside

> > >   of blk_mq that is used because it may get called from atomic context but

> > >   the submission may get blocked in __mmc_claim_host(). This really

> > >   needs to be changed as well, but not in the way I originally suggested:

> > >   As Hannes suggested, the host interrrupt handler should always use

> > >   request_threaded_irq() to have its own process context, and then pass a

> > >   flag to blk_mq to say that we never need another workqueue there.

> >

> > So you mean we should complete the request in the host driver irq

> > thread context, then issue another request in this context by calling

> > blk_mq_run_hw_queues()?

>

> Yes. I assumed there was already code that would always run

> blk_mq_run_hw_queue() at I/O completion, but I can't find where

> that happens today.


OK. Now we will complete a request in block softirq, which means the
irq thread of host driver should call blk_mq_complete_request() to
complete this request (triggering the block softirq) and call
blk_mq_run_hw_queues() to dispatch another request in this context.

>

> As I understand, the main difference to today is that

> __blk_mq_delay_run_hw_queue() can call into __blk_mq_run_hw_queue

> directly rather than using the delayed work queue once we

> can skip the BLK_MQ_F_BLOCKING check.


Right. Need to improve this as you suggested.

>

> > > - With that change in place calling a blocking __mmc_claim_host() is

> > >   still a problem, so there should still be a nonblocking mmc_try_claim_host()

> > >   for the submission path, leading to a BLK_STS_DEV_RESOURCE (?)

> > >   return code from mmc_mq_queue_rq(). Basically mmc_mq_queue_rq()

> > >   should always return right away, either after having queued the next I/O

> > >   or with an error, but not waiting for the device in any way.

> >

> > Actually not only the mmc_claim_host() will block the MMC request

> > processing, in this routine, the mmc_blk_part_switch() and

> > mmc_retune() can also block the request processing. Moreover the part

> > switching and tuning should be sync operations, and we can not move

> > them to a work or a thread.

>

> Ok, I see.

>

> Those would also cause requests to be sent to the device or the host

> controller, right? Maybe we can treat them as "a non-IO request


Right.

> has successfully been queued to the device" events, returning

> busy from the mmc_mq_queue_rq() function and then running

> the queue again when they complete?


Yes, seems reasonable to me.

>

> > > - For the packed requests, there is apparently a very simple way to implement

> > >   that without a software queue: mmc_mq_queue_rq() is allowed to look at

> > >   and dequeue all requests that are currently part of the request_queue,

> > >   so it should take out as many as it wants to submit at once and send

> > >   them all down to the driver together, avoiding the need for any further

> > >   round-trips to blk_mq or maintaining a queue in mmc.

> >

> > You mean we can dispatch a request directly from

> > elevator->type->ops.dispatch_request()?  but we still need some helper

> > functions to check if these requests can be packed (the package

> > condition), and need to invent new APIs to start a packed request (or

> > using cqe interfaces, which means we still need to implement some cqe

> > callbacks).

>

> I don't know how the dispatch_request() function fits in there,

> what Hannes told me is that in ->queue_rq() you can always

> look at the following requests that are already queued up

> and take the next ones off the list. Looking at bd->last

> tells you if there are additional requests. If there are, you can

> look at the next one from blk_mq_hw_ctx (not sure how, but

> should not be hard to find)

>

> I also see that there is a commit_rqs() callback that may

> go along with queue_rq(), implementing that one could make

> this easier as well.


Yes, we can use queue_rq()/commit_rqs() and bd->last (now bd->last may
can not work well, see [1]), but like we talked before, for packed
request, we still need some new interfaces (for example, a interface
used to start a packed request, and a interface used to complete a
packed request), but at last we got a consensus that we should re-use
the CQE interfaces instead of new invention.

[1] https://lore.kernel.org/patchwork/patch/1102897/

>

> > > - The DMA management (bounce buffer, map, unmap) that is currently

> > >   done in mmc_blk_mq_issue_rq() should ideally be done in the

> > >   init_request()/exit_request()  (?) callbacks from mmc_mq_ops so this

> > >   can be done asynchronously, out of the critical timing path for the

> > >   submission. With this, there won't be any need for a software queue.

> >

> > This is not true, now the blk-mq will allocate some static request

> > objects (usually the static requests number should be the same with

> > the hardware queue depth) saved in struct blk_mq_tags. So the

> > init_request() is used to initialize the static requests when

> > allocating them, and call exit_request to free the static requests

> > when freeing the 'struct blk_mq_tags', such as the queue is dead. So

> > we can not move the DMA management into the init_request/exit_request.

>

> Ok, I must have misremembered which callback that is then, but I guess

> there is some other place to do it.


I checked the 'struct blk_mq_ops', and I did not find a ops can be
used to do DMA management. And I also checked UFS driver, it also did
the DMA mapping in the queue_rq() (scsi_queue_rq() --->
ufshcd_queuecommand() ---> ufshcd_map_sg()). Maybe I missed something?

Moreover like I said above, for the packed request, we still need
implement something (like the software queue) based on the CQE
interfaces to help to handle packed requests.
(Exiting) Baolin Wang Nov. 18, 2019, 10:04 a.m. UTC | #4
Hi Arnd,

On Tue, 12 Nov 2019 at 16:48, Baolin Wang <baolin.wang7@gmail.com> wrote:
>

> On Tue, Nov 12, 2019 at 12:59 AM Arnd Bergmann <arnd@arndb.de> wrote:

> >

> > On Mon, Nov 11, 2019 at 1:58 PM Baolin Wang <baolin.wang@linaro.org> wrote:

> > > On Mon, 11 Nov 2019 at 17:28, Arnd Bergmann <arnd@arndb.de> wrote:

> > > > On Mon, Nov 11, 2019 at 8:35 AM Baolin Wang <baolin.wang@linaro.org> wrote:

> > > > - Removing all the context switches and workqueues from the data submission

> > > >   path is also the right idea. As you found, there is still a workqueue inside

> > > >   of blk_mq that is used because it may get called from atomic context but

> > > >   the submission may get blocked in __mmc_claim_host(). This really

> > > >   needs to be changed as well, but not in the way I originally suggested:

> > > >   As Hannes suggested, the host interrrupt handler should always use

> > > >   request_threaded_irq() to have its own process context, and then pass a

> > > >   flag to blk_mq to say that we never need another workqueue there.

> > >

> > > So you mean we should complete the request in the host driver irq

> > > thread context, then issue another request in this context by calling

> > > blk_mq_run_hw_queues()?

> >

> > Yes. I assumed there was already code that would always run

> > blk_mq_run_hw_queue() at I/O completion, but I can't find where

> > that happens today.

>

> OK. Now we will complete a request in block softirq, which means the

> irq thread of host driver should call blk_mq_complete_request() to

> complete this request (triggering the block softirq) and call

> blk_mq_run_hw_queues() to dispatch another request in this context.

>

> >

> > As I understand, the main difference to today is that

> > __blk_mq_delay_run_hw_queue() can call into __blk_mq_run_hw_queue

> > directly rather than using the delayed work queue once we

> > can skip the BLK_MQ_F_BLOCKING check.

>

> Right. Need to improve this as you suggested.

>

> >

> > > > - With that change in place calling a blocking __mmc_claim_host() is

> > > >   still a problem, so there should still be a nonblocking mmc_try_claim_host()

> > > >   for the submission path, leading to a BLK_STS_DEV_RESOURCE (?)

> > > >   return code from mmc_mq_queue_rq(). Basically mmc_mq_queue_rq()

> > > >   should always return right away, either after having queued the next I/O

> > > >   or with an error, but not waiting for the device in any way.

> > >

> > > Actually not only the mmc_claim_host() will block the MMC request

> > > processing, in this routine, the mmc_blk_part_switch() and

> > > mmc_retune() can also block the request processing. Moreover the part

> > > switching and tuning should be sync operations, and we can not move

> > > them to a work or a thread.

> >

> > Ok, I see.

> >

> > Those would also cause requests to be sent to the device or the host

> > controller, right? Maybe we can treat them as "a non-IO request

>

> Right.

>

> > has successfully been queued to the device" events, returning

> > busy from the mmc_mq_queue_rq() function and then running

> > the queue again when they complete?

>

> Yes, seems reasonable to me.

>

> >

> > > > - For the packed requests, there is apparently a very simple way to implement

> > > >   that without a software queue: mmc_mq_queue_rq() is allowed to look at

> > > >   and dequeue all requests that are currently part of the request_queue,

> > > >   so it should take out as many as it wants to submit at once and send

> > > >   them all down to the driver together, avoiding the need for any further

> > > >   round-trips to blk_mq or maintaining a queue in mmc.

> > >

> > > You mean we can dispatch a request directly from

> > > elevator->type->ops.dispatch_request()?  but we still need some helper

> > > functions to check if these requests can be packed (the package

> > > condition), and need to invent new APIs to start a packed request (or

> > > using cqe interfaces, which means we still need to implement some cqe

> > > callbacks).

> >

> > I don't know how the dispatch_request() function fits in there,

> > what Hannes told me is that in ->queue_rq() you can always

> > look at the following requests that are already queued up

> > and take the next ones off the list. Looking at bd->last

> > tells you if there are additional requests. If there are, you can

> > look at the next one from blk_mq_hw_ctx (not sure how, but

> > should not be hard to find)

> >

> > I also see that there is a commit_rqs() callback that may

> > go along with queue_rq(), implementing that one could make

> > this easier as well.

>

> Yes, we can use queue_rq()/commit_rqs() and bd->last (now bd->last may

> can not work well, see [1]), but like we talked before, for packed

> request, we still need some new interfaces (for example, a interface

> used to start a packed request, and a interface used to complete a

> packed request), but at last we got a consensus that we should re-use

> the CQE interfaces instead of new invention.

>

> [1] https://lore.kernel.org/patchwork/patch/1102897/

>

> >

> > > > - The DMA management (bounce buffer, map, unmap) that is currently

> > > >   done in mmc_blk_mq_issue_rq() should ideally be done in the

> > > >   init_request()/exit_request()  (?) callbacks from mmc_mq_ops so this

> > > >   can be done asynchronously, out of the critical timing path for the

> > > >   submission. With this, there won't be any need for a software queue.

> > >

> > > This is not true, now the blk-mq will allocate some static request

> > > objects (usually the static requests number should be the same with

> > > the hardware queue depth) saved in struct blk_mq_tags. So the

> > > init_request() is used to initialize the static requests when

> > > allocating them, and call exit_request to free the static requests

> > > when freeing the 'struct blk_mq_tags', such as the queue is dead. So

> > > we can not move the DMA management into the init_request/exit_request.

> >

> > Ok, I must have misremembered which callback that is then, but I guess

> > there is some other place to do it.

>

> I checked the 'struct blk_mq_ops', and I did not find a ops can be

> used to do DMA management. And I also checked UFS driver, it also did

> the DMA mapping in the queue_rq() (scsi_queue_rq() --->

> ufshcd_queuecommand() ---> ufshcd_map_sg()). Maybe I missed something?

>

> Moreover like I said above, for the packed request, we still need

> implement something (like the software queue) based on the CQE

> interfaces to help to handle packed requests.


After some investigation and offline discussion with you, I still have
some concerns about your suggestion.

1) Now blk-mq have not supplied some ops to prepare a request, which is
used to do some DMA management asynchronously. But yes, we can
introduce new ops for blk-mq. But there are still some remaining
preparation in mmc_mq_queue_rq(), like mmc part switch. For software
queue, we can prepare a request totally after issuing one.

2) I wonder if it is appropriate that using the irq threaded context
to dispatch next request, actually we will still introduce a context
switch here. Now we will complete a request in the hard irq handler
and kick the softirq to do time-consuming operations, like DMA
unmapping , and will start next request in the hard irq handler
without context switch. Moreover if we remove the BLK_MQ_F_BLOCKING in
future like you suggested, then we can remove all context switch. And
I think we can dispatch next request in the softirq context (actually
the CQE already did).

3) For packed request support, I did not see an example that block
driver can dispatch a request from the IO scheduler in queue_rq() and
no APIs supported from blk-mq. And we do not know where can dispatch a
request in queue_rq(), from IO scheduler? from ctx? or from
hctx->dispatch list? and if this request can not be passed to host
now, how to do it? Seems lots of complicated things.

Moreover, we still need some interfaces for the packed request
handling, from previous discussion, we still need something like MMC
software queue based on  the CQE to help to handle the packed request.

So I think I still need to introduce the MMC software queue, on the one
hand is that it can really improve the performance from fio data and
avoid a long latency, on the other hand we can expand it to support
packed request easily in future. Thanks.

(Anyway I will still post the V7 to address Adrian's comments and to
see if we can get a consensus there).

-- 
Baolin Wang
Best Regards
Arnd Bergmann Nov. 22, 2019, 9:50 a.m. UTC | #5
(adding Paolo as well, maybe he has some more insights)

On Mon, Nov 18, 2019 at 11:04 AM (Exiting) Baolin Wang
<baolin.wang@linaro.org> wrote:
> On Tue, 12 Nov 2019 at 16:48, Baolin Wang <baolin.wang7@gmail.com> wrote:

> > On Tue, Nov 12, 2019 at 12:59 AM Arnd Bergmann <arnd@arndb.de> wrote:

> > > On Mon, Nov 11, 2019 at 1:58 PM Baolin Wang <baolin.wang@linaro.org> wrote:

> > > > > - With that change in place calling a blocking __mmc_claim_host() is

> > > > >   still a problem, so there should still be a nonblocking mmc_try_claim_host()

> > > > >   for the submission path, leading to a BLK_STS_DEV_RESOURCE (?)

> > > > >   return code from mmc_mq_queue_rq(). Basically mmc_mq_queue_rq()

> > > > >   should always return right away, either after having queued the next I/O

> > > > >   or with an error, but not waiting for the device in any way.

> > > >

> > > > Actually not only the mmc_claim_host() will block the MMC request

> > > > processing, in this routine, the mmc_blk_part_switch() and

> > > > mmc_retune() can also block the request processing. Moreover the part

> > > > switching and tuning should be sync operations, and we can not move

> > > > them to a work or a thread.

> > >

> > > Ok, I see.

> > >

> > > Those would also cause requests to be sent to the device or the host

> > > controller, right? Maybe we can treat them as "a non-IO request

> >

> > Right.

> >

> > > has successfully been queued to the device" events, returning

> > > busy from the mmc_mq_queue_rq() function and then running

> > > the queue again when they complete?

> >

> > Yes, seems reasonable to me.

> >

> > >

> > > > > - For the packed requests, there is apparently a very simple way to implement

> > > > >   that without a software queue: mmc_mq_queue_rq() is allowed to look at

> > > > >   and dequeue all requests that are currently part of the request_queue,

> > > > >   so it should take out as many as it wants to submit at once and send

> > > > >   them all down to the driver together, avoiding the need for any further

> > > > >   round-trips to blk_mq or maintaining a queue in mmc.

> > > >

> > > > You mean we can dispatch a request directly from

> > > > elevator->type->ops.dispatch_request()?  but we still need some helper

> > > > functions to check if these requests can be packed (the package

> > > > condition), and need to invent new APIs to start a packed request (or

> > > > using cqe interfaces, which means we still need to implement some cqe

> > > > callbacks).

> > >

> > > I don't know how the dispatch_request() function fits in there,

> > > what Hannes told me is that in ->queue_rq() you can always

> > > look at the following requests that are already queued up

> > > and take the next ones off the list. Looking at bd->last

> > > tells you if there are additional requests. If there are, you can

> > > look at the next one from blk_mq_hw_ctx (not sure how, but

> > > should not be hard to find)

> > >

> > > I also see that there is a commit_rqs() callback that may

> > > go along with queue_rq(), implementing that one could make

> > > this easier as well.

> >

> > Yes, we can use queue_rq()/commit_rqs() and bd->last (now bd->last may

> > can not work well, see [1]), but like we talked before, for packed

> > request, we still need some new interfaces (for example, a interface

> > used to start a packed request, and a interface used to complete a

> > packed request), but at last we got a consensus that we should re-use

> > the CQE interfaces instead of new invention.

> >

> > [1] https://lore.kernel.org/patchwork/patch/1102897/

> >

> > >

> > > > > - The DMA management (bounce buffer, map, unmap) that is currently

> > > > >   done in mmc_blk_mq_issue_rq() should ideally be done in the

> > > > >   init_request()/exit_request()  (?) callbacks from mmc_mq_ops so this

> > > > >   can be done asynchronously, out of the critical timing path for the

> > > > >   submission. With this, there won't be any need for a software queue.

> > > >

> > > > This is not true, now the blk-mq will allocate some static request

> > > > objects (usually the static requests number should be the same with

> > > > the hardware queue depth) saved in struct blk_mq_tags. So the

> > > > init_request() is used to initialize the static requests when

> > > > allocating them, and call exit_request to free the static requests

> > > > when freeing the 'struct blk_mq_tags', such as the queue is dead. So

> > > > we can not move the DMA management into the init_request/exit_request.

> > >

> > > Ok, I must have misremembered which callback that is then, but I guess

> > > there is some other place to do it.

> >

> > I checked the 'struct blk_mq_ops', and I did not find a ops can be

> > used to do DMA management. And I also checked UFS driver, it also did

> > the DMA mapping in the queue_rq() (scsi_queue_rq() --->

> > ufshcd_queuecommand() ---> ufshcd_map_sg()). Maybe I missed something?

> >

> > Moreover like I said above, for the packed request, we still need

> > implement something (like the software queue) based on the CQE

> > interfaces to help to handle packed requests.

>

> After some investigation and offline discussion with you, I still have

> some concerns about your suggestion.

>

> 1) Now blk-mq have not supplied some ops to prepare a request, which is

> used to do some DMA management asynchronously. But yes, we can

> introduce new ops for blk-mq. But there are still some remaining

> preparation in mmc_mq_queue_rq(), like mmc part switch. For software

> queue, we can prepare a request totally after issuing one.


I suppose to make the submission non-blocking, all operations that
currently block in the submission path may have to be changed first.

For the case of a partition switch (same for retune), I suppose
something like this can be done:

- in queue_rq() check whether a partition switch is needed. If not,
  submit the current rq
- if a partition switch is needed, submit the partition switch cmd
  instead, and return busy status
- when the completion arrives for the partition switch, call back into
  blk_mq to have it call queue_rq again.

Or possibly even (this might not be possible without signifcant
restructuring):

- when preparing a request that would require a partition switch,
  insert another meta-request to switch the partition ahead of it.

I do realize that this is a significant departure from how it was done
in the past, but it seems cleaner that way to me.

> 2) I wonder if it is appropriate that using the irq threaded context

> to dispatch next request, actually we will still introduce a context

> switch here. Now we will complete a request in the hard irq handler

> and kick the softirq to do time-consuming operations, like DMA

> unmapping , and will start next request in the hard irq handler

> without context switch. Moreover if we remove the BLK_MQ_F_BLOCKING in

> future like you suggested, then we can remove all context switch. And

> I think we can dispatch next request in the softirq context (actually

> the CQE already did).


I hope Hannes (or someone else) can comment here, as I don't
know exactly what his objection to kicking off the next cmd in the
hardirq was.

I think generally, deferring all slow operations to an irqthread
rather than a softirq is a good idea, but I share your concern that
this can introduce an unnecessary latency between the the
the IRQ is signaled and the time the following cmd is sent to the
hardware.

Doing everything in a single (irqthread) context is clearly simpler,
so this would need to be measured carefully to avoid unnecessary
complexity, but I think don't see anything stopping us from having
the fast-path where the low-level driver first checks for any possible
error conditions in hardirq context and the fires off a prepared cmd
right away whenever it can before triggering the irqthread that does
everything else. I think this has to be a per-driver optimization, so
the common case would just have an irqthread.

> 3) For packed request support, I did not see an example that block

> driver can dispatch a request from the IO scheduler in queue_rq() and

> no APIs supported from blk-mq. And we do not know where can dispatch a

> request in queue_rq(), from IO scheduler? from ctx? or from

> hctx->dispatch list? and if this request can not be passed to host

> now, how to do it? Seems lots of complicated things.


The only way I can see is the ->last flag, so if blk_mq submits multiple
requests in a row to queue_rq() with this flag cleared and  calls
->commit_rqs() after the last one. This seems to be what the scsi
disk driver and the nvme driver rely on, and we should be able to use
it the same way for packed cmds, by checking each time in queue_rq()
whether requests can/should be combined and reporting busy otherwise
(after preparing a combined mmc cmd).
blk_mq will then call commit_rqs, which should do the actual submission
to the hardware driver.

Now as you point out, the *big* problem with this is that we never
get multiple requests together in practice, i.e. the last flag is almost
always set, and any optimization around it has no effect.

This is where I'm a bit lost in the code as well, but it seems that
this is part of the current bfq design that only sends one request down
the driver stack at a time, and this would have to change first before
we can rely on this for packing requests.

Paolo, can you comment on why this is currently done, or if it can
be changed? It seems to me that sending multiple requests at
once would also have a significant benefit on the per-request overhead
on NVMe devices with with bfq.

> Moreover, we still need some interfaces for the packed request

> handling, from previous discussion, we still need something like MMC

> software queue based on  the CQE to help to handle the packed request.

>

> So I think I still need to introduce the MMC software queue, on the one

> hand is that it can really improve the performance from fio data and

> avoid a long latency, on the other hand we can expand it to support

> packed request easily in future. Thanks.

>

> (Anyway I will still post the V7 to address Adrian's comments and to

> see if we can get a consensus there).



     Arnd
Arnd Bergmann Nov. 22, 2019, 1:49 p.m. UTC | #6
On Fri, Nov 22, 2019 at 2:20 PM Linus Walleij <linus.walleij@linaro.org> wrote:
>

> On Fri, Nov 22, 2019 at 10:50 AM Arnd Bergmann <arnd@arndb.de> wrote:

>

> > I suppose to make the submission non-blocking, all operations that

> > currently block in the submission path may have to be changed first.

> >

> > For the case of a partition switch (same for retune), I suppose

> > something like this can be done:

> >

> > - in queue_rq() check whether a partition switch is needed. If not,

> >   submit the current rq

> > - if a partition switch is needed, submit the partition switch cmd

> >   instead, and return busy status

> > - when the completion arrives for the partition switch, call back into

> >   blk_mq to have it call queue_rq again.

> >

> > Or possibly even (this might not be possible without signifcant

> > restructuring):

> >

> > - when preparing a request that would require a partition switch,

> >   insert another meta-request to switch the partition ahead of it.

> >

> > I do realize that this is a significant departure from how it was done

> > in the past, but it seems cleaner that way to me.

>

> This partition business really need a proper overhaul.

>

> I outlined the work elsewhere but the problem is that the

> eMMC "partitions" such as boot partitions and the usecase-defined

> "general" partition (notice SD cards do not have this problem)

> are badly integrated with the Linux partition manager.


I think that's a totally orthogonal problem though: we may
well be able to come up with a different way of representing
the extra partitions to user space or as separate block devices,
but in the end, this looks exactly the same to mm
 ->queue_rq() callback. If we have send a cmd to one partition
and want to send the next cmd to another partition, we first
have to send the partition switch cmd.

      Arnd
Hannes Reinecke Nov. 26, 2019, 11:17 a.m. UTC | #7
On 11/22/19 10:50 AM, Arnd Bergmann wrote:
> (adding Paolo as well, maybe he has some more insights)

> 

> On Mon, Nov 18, 2019 at 11:04 AM (Exiting) Baolin Wang

> <baolin.wang@linaro.org> wrote:

>> On Tue, 12 Nov 2019 at 16:48, Baolin Wang <baolin.wang7@gmail.com> wrote:

>>> On Tue, Nov 12, 2019 at 12:59 AM Arnd Bergmann <arnd@arndb.de> wrote:

>>>> On Mon, Nov 11, 2019 at 1:58 PM Baolin Wang <baolin.wang@linaro.org> wrote:

>>>>>> - With that change in place calling a blocking __mmc_claim_host() is

>>>>>>   still a problem, so there should still be a nonblocking mmc_try_claim_host()

>>>>>>   for the submission path, leading to a BLK_STS_DEV_RESOURCE (?)

>>>>>>   return code from mmc_mq_queue_rq(). Basically mmc_mq_queue_rq()

>>>>>>   should always return right away, either after having queued the next I/O

>>>>>>   or with an error, but not waiting for the device in any way.

>>>>>

>>>>> Actually not only the mmc_claim_host() will block the MMC request

>>>>> processing, in this routine, the mmc_blk_part_switch() and

>>>>> mmc_retune() can also block the request processing. Moreover the part

>>>>> switching and tuning should be sync operations, and we can not move

>>>>> them to a work or a thread.

>>>>

>>>> Ok, I see.

>>>>

>>>> Those would also cause requests to be sent to the device or the host

>>>> controller, right? Maybe we can treat them as "a non-IO request

>>>

>>> Right.

>>>

>>>> has successfully been queued to the device" events, returning

>>>> busy from the mmc_mq_queue_rq() function and then running

>>>> the queue again when they complete?

>>>

>>> Yes, seems reasonable to me.

>>>

>>>>

>>>>>> - For the packed requests, there is apparently a very simple way to implement

>>>>>>   that without a software queue: mmc_mq_queue_rq() is allowed to look at

>>>>>>   and dequeue all requests that are currently part of the request_queue,

>>>>>>   so it should take out as many as it wants to submit at once and send

>>>>>>   them all down to the driver together, avoiding the need for any further

>>>>>>   round-trips to blk_mq or maintaining a queue in mmc.

>>>>>

>>>>> You mean we can dispatch a request directly from

>>>>> elevator->type->ops.dispatch_request()?  but we still need some helper

>>>>> functions to check if these requests can be packed (the package

>>>>> condition), and need to invent new APIs to start a packed request (or

>>>>> using cqe interfaces, which means we still need to implement some cqe

>>>>> callbacks).

>>>>

>>>> I don't know how the dispatch_request() function fits in there,

>>>> what Hannes told me is that in ->queue_rq() you can always

>>>> look at the following requests that are already queued up

>>>> and take the next ones off the list. Looking at bd->last

>>>> tells you if there are additional requests. If there are, you can

>>>> look at the next one from blk_mq_hw_ctx (not sure how, but

>>>> should not be hard to find)

>>>>

>>>> I also see that there is a commit_rqs() callback that may

>>>> go along with queue_rq(), implementing that one could make

>>>> this easier as well.

>>>

>>> Yes, we can use queue_rq()/commit_rqs() and bd->last (now bd->last may

>>> can not work well, see [1]), but like we talked before, for packed

>>> request, we still need some new interfaces (for example, a interface

>>> used to start a packed request, and a interface used to complete a

>>> packed request), but at last we got a consensus that we should re-use

>>> the CQE interfaces instead of new invention.

>>>

>>> [1] https://lore.kernel.org/patchwork/patch/1102897/

>>>

>>>>

>>>>>> - The DMA management (bounce buffer, map, unmap) that is currently

>>>>>>   done in mmc_blk_mq_issue_rq() should ideally be done in the

>>>>>>   init_request()/exit_request()  (?) callbacks from mmc_mq_ops so this

>>>>>>   can be done asynchronously, out of the critical timing path for the

>>>>>>   submission. With this, there won't be any need for a software queue.

>>>>>

>>>>> This is not true, now the blk-mq will allocate some static request

>>>>> objects (usually the static requests number should be the same with

>>>>> the hardware queue depth) saved in struct blk_mq_tags. So the

>>>>> init_request() is used to initialize the static requests when

>>>>> allocating them, and call exit_request to free the static requests

>>>>> when freeing the 'struct blk_mq_tags', such as the queue is dead. So

>>>>> we can not move the DMA management into the init_request/exit_request.

>>>>

>>>> Ok, I must have misremembered which callback that is then, but I guess

>>>> there is some other place to do it.

>>>

>>> I checked the 'struct blk_mq_ops', and I did not find a ops can be

>>> used to do DMA management. And I also checked UFS driver, it also did

>>> the DMA mapping in the queue_rq() (scsi_queue_rq() --->

>>> ufshcd_queuecommand() ---> ufshcd_map_sg()). Maybe I missed something?

>>>

>>> Moreover like I said above, for the packed request, we still need

>>> implement something (like the software queue) based on the CQE

>>> interfaces to help to handle packed requests.

>>

>> After some investigation and offline discussion with you, I still have

>> some concerns about your suggestion.

>>

>> 1) Now blk-mq have not supplied some ops to prepare a request, which is

>> used to do some DMA management asynchronously. But yes, we can

>> introduce new ops for blk-mq. But there are still some remaining

>> preparation in mmc_mq_queue_rq(), like mmc part switch. For software

>> queue, we can prepare a request totally after issuing one.

> 

> I suppose to make the submission non-blocking, all operations that

> currently block in the submission path may have to be changed first.

> 

> For the case of a partition switch (same for retune), I suppose

> something like this can be done:

> 

> - in queue_rq() check whether a partition switch is needed. If not,

>   submit the current rq

> - if a partition switch is needed, submit the partition switch cmd

>   instead, and return busy status

> - when the completion arrives for the partition switch, call back into

>   blk_mq to have it call queue_rq again.

> 

> Or possibly even (this might not be possible without signifcant

> restructuring):

> 

> - when preparing a request that would require a partition switch,

>   insert another meta-request to switch the partition ahead of it.

> 

> I do realize that this is a significant departure from how it was done

> in the past, but it seems cleaner that way to me.

> 

I would be treating the partition issue separate from the queued/batched
submission.

Aligning with the 'traditional' linux way for partition handling is
definitely the way to go IMO; otherwise you'll end up having to worry
about resource allocation between distinct queues (like you have to do
now), and will be having a hard time trying to map it properly to the
underlying hardware abstraction in blk-mq.

For starters I would keep a partition marker in the driver instance, and
calculate the parition for each incoming request. If the partition is
different you'll have to insert a partition switch request before
submitting the actual one.

To do this efficiently it would be good to know if:
a) How invasive is the partition switch? Would it be feasible to eg add
a partition switch for every command? This might sound daft now, but if
we get request batching going it might not the _that_ expensive after all...
b) Can the request switch command batched together with the normal
command? IE is is possible to have them both send in one go?
If so it would make life _so_ much easier; we could submit both command
at the same time, and won't have to worry about handling internal
completions ...

>> 2) I wonder if it is appropriate that using the irq threaded context

>> to dispatch next request, actually we will still introduce a context

>> switch here. Now we will complete a request in the hard irq handler

>> and kick the softirq to do time-consuming operations, like DMA

>> unmapping , and will start next request in the hard irq handler

>> without context switch. Moreover if we remove the BLK_MQ_F_BLOCKING in

>> future like you suggested, then we can remove all context switch. And

>> I think we can dispatch next request in the softirq context (actually

>> the CQE already did).

> 

> I hope Hannes (or someone else) can comment here, as I don't

> know exactly what his objection to kicking off the next cmd in the

> hardirq was.

> 

The point being that you'll have to have a context switch anyway
(between hardirq and softirq), and you'll be able to better handle
recovery as the hardirq handler is pretty generic and the won't be any
chance of that one becoming stuck.
And, of course, modern software design :-)

> I think generally, deferring all slow operations to an irqthread

> rather than a softirq is a good idea, but I share your concern that

> this can introduce an unnecessary latency between the the

> the IRQ is signaled and the time the following cmd is sent to the

> hardware.

> > Doing everything in a single (irqthread) context is clearly simpler,

> so this would need to be measured carefully to avoid unnecessary

> complexity, but I think don't see anything stopping us from having

> the fast-path where the low-level driver first checks for any possible

> error conditions in hardirq context and the fires off a prepared cmd

> right away whenever it can before triggering the irqthread that does

> everything else. I think this has to be a per-driver optimization, so

> the common case would just have an irqthread.

> 

>> 3) For packed request support, I did not see an example that block

>> driver can dispatch a request from the IO scheduler in queue_rq() and

>> no APIs supported from blk-mq. And we do not know where can dispatch a

>> request in queue_rq(), from IO scheduler? from ctx? or from

>> hctx->dispatch list? and if this request can not be passed to host

>> now, how to do it? Seems lots of complicated things.

> 

> The only way I can see is the ->last flag, so if blk_mq submits multiple

> requests in a row to queue_rq() with this flag cleared and  calls

> ->commit_rqs() after the last one. This seems to be what the scsi

> disk driver and the nvme driver rely on, and we should be able to use

> it the same way for packed cmds, by checking each time in queue_rq()

> whether requests can/should be combined and reporting busy otherwise

> (after preparing a combined mmc cmd).

> blk_mq will then call commit_rqs, which should do the actual submission

> to the hardware driver.

> 

The ->last flag really depends on the submission thread, eg if something
in the upper layers is using on-stack plugging.
In my experience this is done only in some specific use-cases resp.
filesystems, so this is not something you can rely on to make any decisions.

> Now as you point out, the *big* problem with this is that we never

> get multiple requests together in practice, i.e. the last flag is almost

> always set, and any optimization around it has no effect.

> 

See above. I don't think the using the ->last flag is the way to go here.
What you really need to do here is to inject some 'pushback' into the
block layer so that is has a _chance_ of assembling more requests.

But the actual design really needs to take hardware features into account.

As mentioned above, initially I would concentrate on getting the
partitioning working with a single request queue; once that is done we
can look at request batching proper.

And for that you could have a look at the S/390 DASD driver (right,
Arndt?), which has a very similar concept.

Cheers,

Hannes
-- 
Dr. Hannes Reinecke		      Teamlead Storage & Networking
hare@suse.de			                  +49 911 74053 688
SUSE Software Solutions Germany GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), GF: Felix Imendörffer
Arnd Bergmann Nov. 27, 2019, 12:01 p.m. UTC | #8
On Wed, Nov 27, 2019 at 10:00 AM Christoph Hellwig <hch@infradead.org> wrote:
>

> On Tue, Nov 26, 2019 at 12:17:15PM +0100, Hannes Reinecke wrote:

>  If requests are batched enough we could just drain

> and switch every time an other partition access comes in.  Especially

> so if people only use partitions for boot partitions and other rarely

> used areas.


We only support a single user partition plus up to two boot partitions that
are accessed rarely, I don't think there is any reason to optimize switching
between them.

The only change that I think we need here is to change the partition switch
from something that is done synchronously during ->queue_rq() to
something that fits better into normal scheme of sending a cmd to
the device, returning BLK_STS_RESOURCE from ->queue_rq.
Possibly this could even be turned into a standard struct request that is
added between two normal requests for different partitions at some
point, if this simplifies the logic (I suspect it won't, but it may be worth
a try).

      Arnd