[PATCH RESEND v3 0/7] hw/nvme: lift IOV_MAX limit in DMA path

Daniel Gomez posted 7 patches 3 weeks, 6 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261026-align-nvme-mdts-with-linux-v3-0-a33bd7c32df9@samsung.com
Maintainers: Keith Busch <kbusch@kernel.org>, Klaus Jensen <its@irrelevant.dk>, Jesper Devantier <foss@defmacro.it>, Paolo Bonzini <pbonzini@redhat.com>, Peter Xu <peterx@redhat.com>, "Philippe Mathieu-Daudé" <philmd@oss.qualcomm.com>
hw/nvme/ctrl.c       | 25 +++++++++++++++++--------
system/dma-helpers.c | 19 ++++++++++++++++---
2 files changed, 33 insertions(+), 11 deletions(-)
[PATCH RESEND v3 0/7] hw/nvme: lift IOV_MAX limit in DMA path
Posted by Daniel Gomez 3 weeks, 6 days ago
Raise the QEMU NVMe controller's MDTS beyond the 2 MiB cap.

Commit 53493c1f83 ("hw/nvme: cap MDTS value for internal limitation")
needed the 2 MiB cap because dma_blk_io() submitted the full sglist
in one preadv()/pwritev() call, which the host kernel rejects when the
iovec count exceeds IOV_MAX. This series moves the IOV_MAX bound down to
dma_blk_cb(), where we batch in IOV_MAX chunks when necessary.

dma_blk_cb() now breaks its accumulation at IOV_MAX and submits the
next chunk via the existing re-entry path. With that the DMA-path
nvme_map_addr() guard and the MDTS cap are removed. The cap stays only
for CMB/PMR, whose transfers go through a different path and are still
bounded by IOV_MAX.

This series builds cleanly under ubsan with large mdts and passes the
following fio tests on a 4k LBA QEMU NVMe device:

fio \
    --name=mdts-stress \
    --filename=/mnt/mdts/stress.fio \
    --rw=randwrite \
    --bs=8M \
    --ioengine=psync \
    --direct=0 \
    --numjobs=8 \
    --time_based \
    --runtime=600 \
    --fsync=8 \
    --end_fsync=1 \
    --group_reporting \
    --refill_buffers \
    --norandommap \
    --offset_align=32k

fio --name=demo --filename=/dev/nvme0n1 \
    --rw=write --bs=8M --size=8M --direct=1 \
    --iomem_align=4 --verify=crc32c --do_verify=0

fio --name=demo --filename=/dev/nvme0n1 \
    --rw=write --bs=8M --size=8M --direct=1 \
    --verify=crc32c --verify_only

---
Changes in v3:
- Apply review/ack trailers from Keith and Jesper
- Drop reproducer in commit message from patch 1 as unrelated
- Apply nvme_mdts_max_iovs() helper from Jesper
- Use params local instead of n->params in CMB/PMR check.
- Update commit message from last patch
- Link to v2: https://lore.kernel.org/qemu-devel/20260819-align-nvme-mdts-with-linux-v2-0-351ac2dfed64@samsung.com

Changes in v2:
- Update cover letter.
- Rebase on top of nvme.next branch, tree:
https://gitlab.com/birkelund/qemu
- Keep the 2 MiB mdts cap for CMB/PMR configurations (Klaus)
- Fix the `mem` leak reported by Keith
- Drop NVME_MDTS_MAX, ie. unlimited mdts as long as Linux host supports
it.
Allowing larger mdts, also meant we needed to:
- Clamp the mdts and zasl shifts to stay within uint64_t
- Cap qemu_iovec_init() to IOV_MAX when the sg list is large
- Keep IO chunks aligned when chunking at IOV_MAX
- Fix the wrong bytes being discarded in the discard path
- Link to v1: https://lore.kernel.org/qemu-devel/20260529-align-nvme-mdts-with-linux-v1-0-221d4d21ab43@samsung.com

To: qemu-devel@nongnu.org
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Peter Xu <peterx@redhat.com>
Cc: Philippe Mathieu-Daudé <philmd@mailo.com>
Cc: Keith Busch <kbusch@kernel.org>
Cc: Klaus Jensen <its@irrelevant.dk>
Cc: Klaus Jensen <k.jensen@samsung.com>
Cc: Jesper Devantier <foss@defmacro.it>
Cc: qemu-block@nongnu.org
Cc: Daniel Gomez <da.gomez@kernel.org>
Cc: Daniel Gomez <da.gomez@samsung.com>
Cc: GOST <gost.dev@samsung.com>
Signed-off-by: Daniel Gomez <da.gomez@samsung.com>

---
Daniel Gomez (7):
      dma-helpers: fix unaligned discard_back
      dma-helpers: ensure IOV_MAX chunks end aligned
      dma-helpers: cap iovec allocation at IOV_MAX
      dma-helpers: chunk dma_blk_cb at IOV_MAX
      hw/nvme: clamp mdts and zasl shifts
      hw/nvme: drop DMA-path IOV_MAX guard
      hw/nvme: cap mdts for CMB/PMR-only

 hw/nvme/ctrl.c       | 25 +++++++++++++++++--------
 system/dma-helpers.c | 19 ++++++++++++++++---
 2 files changed, 33 insertions(+), 11 deletions(-)
---
base-commit: 7a34f7b8794b29cd1bd4dfa45f7d8e8daba7cff5
change-id: 20260528-align-nvme-mdts-with-linux-67d618f6730b

Best regards,
-- 
Daniel Gomez <da.gomez@samsung.com>


Re: [PATCH RESEND v3 0/7] hw/nvme: lift IOV_MAX limit in DMA path
Posted by Klaus Jensen 3 weeks, 6 days ago
On Oct 26 11:11, Daniel Gomez wrote:
> Raise the QEMU NVMe controller's MDTS beyond the 2 MiB cap.
> 
> Commit 53493c1f83 ("hw/nvme: cap MDTS value for internal limitation")
> needed the 2 MiB cap because dma_blk_io() submitted the full sglist
> in one preadv()/pwritev() call, which the host kernel rejects when the
> iovec count exceeds IOV_MAX. This series moves the IOV_MAX bound down to
> dma_blk_cb(), where we batch in IOV_MAX chunks when necessary.
> 
> dma_blk_cb() now breaks its accumulation at IOV_MAX and submits the
> next chunk via the existing re-entry path. With that the DMA-path
> nvme_map_addr() guard and the MDTS cap are removed. The cap stays only
> for CMB/PMR, whose transfers go through a different path and are still
> bounded by IOV_MAX.
> 
> This series builds cleanly under ubsan with large mdts and passes the
> following fio tests on a 4k LBA QEMU NVMe device:
> 
> fio \
>     --name=mdts-stress \
>     --filename=/mnt/mdts/stress.fio \
>     --rw=randwrite \
>     --bs=8M \
>     --ioengine=psync \
>     --direct=0 \
>     --numjobs=8 \
>     --time_based \
>     --runtime=600 \
>     --fsync=8 \
>     --end_fsync=1 \
>     --group_reporting \
>     --refill_buffers \
>     --norandommap \
>     --offset_align=32k
> 
> fio --name=demo --filename=/dev/nvme0n1 \
>     --rw=write --bs=8M --size=8M --direct=1 \
>     --iomem_align=4 --verify=crc32c --do_verify=0
> 
> fio --name=demo --filename=/dev/nvme0n1 \
>     --rw=write --bs=8M --size=8M --direct=1 \
>     --verify=crc32c --verify_only
> 
> ---
> Changes in v3:
> - Apply review/ack trailers from Keith and Jesper
> - Drop reproducer in commit message from patch 1 as unrelated
> - Apply nvme_mdts_max_iovs() helper from Jesper
> - Use params local instead of n->params in CMB/PMR check.
> - Update commit message from last patch
> - Link to v2: https://lore.kernel.org/qemu-devel/20260819-align-nvme-mdts-with-linux-v2-0-351ac2dfed64@samsung.com
> 
> Changes in v2:
> - Update cover letter.
> - Rebase on top of nvme.next branch, tree:
> https://gitlab.com/birkelund/qemu
> - Keep the 2 MiB mdts cap for CMB/PMR configurations (Klaus)
> - Fix the `mem` leak reported by Keith
> - Drop NVME_MDTS_MAX, ie. unlimited mdts as long as Linux host supports
> it.
> Allowing larger mdts, also meant we needed to:
> - Clamp the mdts and zasl shifts to stay within uint64_t
> - Cap qemu_iovec_init() to IOV_MAX when the sg list is large
> - Keep IO chunks aligned when chunking at IOV_MAX
> - Fix the wrong bytes being discarded in the discard path
> - Link to v1: https://lore.kernel.org/qemu-devel/20260529-align-nvme-mdts-with-linux-v1-0-221d4d21ab43@samsung.com
> 
> To: qemu-devel@nongnu.org
> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Cc: Peter Xu <peterx@redhat.com>
> Cc: Philippe Mathieu-Daudé <philmd@mailo.com>
> Cc: Keith Busch <kbusch@kernel.org>
> Cc: Klaus Jensen <its@irrelevant.dk>
> Cc: Klaus Jensen <k.jensen@samsung.com>
> Cc: Jesper Devantier <foss@defmacro.it>
> Cc: qemu-block@nongnu.org
> Cc: Daniel Gomez <da.gomez@kernel.org>
> Cc: Daniel Gomez <da.gomez@samsung.com>
> Cc: GOST <gost.dev@samsung.com>
> Signed-off-by: Daniel Gomez <da.gomez@samsung.com>
> 
> ---
> Daniel Gomez (7):
>       dma-helpers: fix unaligned discard_back
>       dma-helpers: ensure IOV_MAX chunks end aligned
>       dma-helpers: cap iovec allocation at IOV_MAX
>       dma-helpers: chunk dma_blk_cb at IOV_MAX
>       hw/nvme: clamp mdts and zasl shifts
>       hw/nvme: drop DMA-path IOV_MAX guard
>       hw/nvme: cap mdts for CMB/PMR-only
> 
>  hw/nvme/ctrl.c       | 25 +++++++++++++++++--------
>  system/dma-helpers.c | 19 ++++++++++++++++---
>  2 files changed, 33 insertions(+), 11 deletions(-)
> ---
> base-commit: 7a34f7b8794b29cd1bd4dfa45f7d8e8daba7cff5
> change-id: 20260528-align-nvme-mdts-with-linux-67d618f6730b
> 
> Best regards,
> -- 
> Daniel Gomez <da.gomez@samsung.com>
> 
> 

I've been holding out on this because I'd really like an Acked-by from a
Memory API maintainer (system/dma-helpers.c).
Re: [PATCH RESEND v3 0/7] hw/nvme: lift IOV_MAX limit in DMA path
Posted by Daniel Gomez 3 weeks, 6 days ago
On 26/10/2026 13.03, Klaus Jensen wrote:
> On Oct 26 11:11, Daniel Gomez wrote:
> 
> I've been holding out on this because I'd really like an Acked-by from a
> Memory API maintainer (system/dma-helpers.c).

Paolo, Peter, Philippe,

Can you take a look? FYI, patches have already been reviewed by Keith
and Jesper early last month.
Re: [PATCH RESEND v3 0/7] hw/nvme: lift IOV_MAX limit in DMA path
Posted by Peter Xu 3 weeks, 6 days ago
On Mon, Oct 26, 2026 at 01:39:58PM +0100, Daniel Gomez wrote:
> On 26/10/2026 13.03, Klaus Jensen wrote:
> > On Oct 26 11:11, Daniel Gomez wrote:
> > 
> > I've been holding out on this because I'd really like an Acked-by from a
> > Memory API maintainer (system/dma-helpers.c).
> 
> Paolo, Peter, Philippe,
> 
> Can you take a look? FYI, patches have already been reviewed by Keith
> and Jesper early last month.

Sorry for being late, and sorry in advance if I won't follow up this email
as fast.. due to other things occupied.

Personally I have some uneasy feeling on some changes. It involves
DMAAIOCB, which is not part of what I familiar with QEMU at least before I
read more code.. so please bare with me if I will say something naive.

It seems to me patch 1/2 tries to fix an issue specific with
qemu_iovec_discard_back(), which I would raise a few questions unanswered
myself:

  - the existing qemu_iovec_discard_back() was introduced back in 2014 in
    commit 58f423fbd5, with a mention of fixing
    /x86_64/ide/bmdma/short_prdt.  Then if patch 1 fixed it up due to some
    regression, first question I have is why it wasn't spot by unit test.
    Maybe we missed some test?  Or others?

  - qemu_iovec_discard_back() alone seems risky on its own, which is the
    fundation of this whole series: it directly drop some ranges that we
    mapped, which can be bounce buffer.  Now, we only fetch bounce buffer
    from the raw HVA pointer, which seems fine if we only drop partial and
    end of some bounce buffer.  But could we drop the whole buffer (causing
    bounce buffer leak)?  It just seems slightly tricky, after all, the iov
    is the only thing dma_blk_unmap() later relies on releasing these
    buffers.  So if we want to fix this path, I wonder if we can do it even
    safer (doesn't mean these patches are bad, so really pure question).

  - for patch 2, it seems to me it's only fixing the case when discard
    worked in the middle not at the end.  Then IMHO it's a bad idea
    splitting two logics: can we somehow put this together with the discard
    logic?  Would that be cleaner and easier maintainable?

I added Stefan and Kevin, as this is very block relevant.

Thanks,

-- 
Peter Xu
Re: [PATCH RESEND v3 0/7] hw/nvme: lift IOV_MAX limit in DMA path
Posted by Peter Xu 3 weeks, 6 days ago
Cc Stefan & Kevin, for real.

On Mon, Oct 26, 2026 at 01:57:42PM -0400, Peter Xu wrote:
> On Mon, Oct 26, 2026 at 01:39:58PM +0100, Daniel Gomez wrote:
> > On 26/10/2026 13.03, Klaus Jensen wrote:
> > > On Oct 26 11:11, Daniel Gomez wrote:
> > > 
> > > I've been holding out on this because I'd really like an Acked-by from a
> > > Memory API maintainer (system/dma-helpers.c).
> > 
> > Paolo, Peter, Philippe,
> > 
> > Can you take a look? FYI, patches have already been reviewed by Keith
> > and Jesper early last month.
> 
> Sorry for being late, and sorry in advance if I won't follow up this email
> as fast.. due to other things occupied.
> 
> Personally I have some uneasy feeling on some changes. It involves
> DMAAIOCB, which is not part of what I familiar with QEMU at least before I
> read more code.. so please bare with me if I will say something naive.
> 
> It seems to me patch 1/2 tries to fix an issue specific with
> qemu_iovec_discard_back(), which I would raise a few questions unanswered
> myself:
> 
>   - the existing qemu_iovec_discard_back() was introduced back in 2014 in
>     commit 58f423fbd5, with a mention of fixing
>     /x86_64/ide/bmdma/short_prdt.  Then if patch 1 fixed it up due to some
>     regression, first question I have is why it wasn't spot by unit test.
>     Maybe we missed some test?  Or others?
> 
>   - qemu_iovec_discard_back() alone seems risky on its own, which is the
>     fundation of this whole series: it directly drop some ranges that we
>     mapped, which can be bounce buffer.  Now, we only fetch bounce buffer
>     from the raw HVA pointer, which seems fine if we only drop partial and
>     end of some bounce buffer.  But could we drop the whole buffer (causing
>     bounce buffer leak)?  It just seems slightly tricky, after all, the iov
>     is the only thing dma_blk_unmap() later relies on releasing these
>     buffers.  So if we want to fix this path, I wonder if we can do it even
>     safer (doesn't mean these patches are bad, so really pure question).
> 
>   - for patch 2, it seems to me it's only fixing the case when discard
>     worked in the middle not at the end.  Then IMHO it's a bad idea
>     splitting two logics: can we somehow put this together with the discard
>     logic?  Would that be cleaner and easier maintainable?
> 
> I added Stefan and Kevin, as this is very block relevant.
> 
> Thanks,
> 
> -- 
> Peter Xu

-- 
Peter Xu
Re: [PATCH RESEND v3 0/7] hw/nvme: lift IOV_MAX limit in DMA path
Posted by Daniel Gomez 3 weeks, 6 days ago
On 26/10/2026 18.59, Peter Xu wrote:
> Cc Stefan & Kevin, for real.
> 
> On Mon, Oct 26, 2026 at 01:57:42PM -0400, Peter Xu wrote:
>> On Mon, Oct 26, 2026 at 01:39:58PM +0100, Daniel Gomez wrote:
>>> On 26/10/2026 13.03, Klaus Jensen wrote:
>>>> On Oct 26 11:11, Daniel Gomez wrote:
>>   - the existing qemu_iovec_discard_back() was introduced back in 2014 in
>>     commit 58f423fbd5, with a mention of fixing
>>     /x86_64/ide/bmdma/short_prdt.  Then if patch 1 fixed it up due to some
>>     regression, first question I have is why it wasn't spot by unit test.
>>     Maybe we missed some test?  Or others?

I can't seem to find the test sending unaligned requests? discard_back is
only triggered if the caller (the prdt various test) sends unaligned
requests.

For clarity, the patch only restores the behavior back:

The regression (99868af3d0):

@@ -160,8 +161,9 @@ static void dma_blk_cb(void *opaque, int ret)
         return;
     }

-    if (dbs->iov.size & ~BDRV_SECTOR_MASK) {
-        qemu_iovec_discard_back(&dbs->iov, dbs->iov.size & ~BDRV_SECTOR_MASK);
+    if (!QEMU_IS_ALIGNED(dbs->iov.size, dbs->align)) {
+        qemu_iovec_discard_back(&dbs->iov,
+                                QEMU_ALIGN_DOWN(dbs->iov.size, dbs->align));

Fix:

@@ -174,8 +174,7 @@ static void dma_blk_cb(void *opaque, int ret)
     }

     if (!QEMU_IS_ALIGNED(dbs->iov.size, dbs->align)) {
-        qemu_iovec_discard_back(&dbs->iov,
-                                QEMU_ALIGN_DOWN(dbs->iov.size, dbs->align));
+        qemu_iovec_discard_back(&dbs->iov, dbs->iov.size % dbs->align);
     }

>>   - qemu_iovec_discard_back() alone seems risky on its own, which is the
>>     fundation of this whole series: 

Not really. But this is what triggered patch 2: to ensure requests are 
always aligned. This means, qemu_iovec_discard_back() is never in the path
for large IO.

>>   - for patch 2, it seems to me it's only fixing the case when discard
>>     worked in the middle not at the end.  

We don't trigger the discard path. Patch 2, ensures that by sending always
aligned requests.