hw/nvme/ctrl.c | 10 ++++++++-- hw/nvme/trace-events | 1 + 2 files changed, 9 insertions(+), 2 deletions(-)
From: Klaus Jensen <k.jensen@samsung.com>
nvme_tx discards the MemTxResult returned by dma_buf_read/write and only
inspects the residual. dma_buf_rw already accounts failed transactions
against the residual, so a full-length transfer to or from unmapped
memory results in a zero residual and an apparently successful transfer.
Check the memory transaction result instead and complete the command
with a Data Transfer Error on error and add a new trace event that
actually describe the event correctly.
Cc: qemu-stable@nongnu.org
Fixes: f02b664aad8f ("hw/dma: Let dma_buf_read() / dma_buf_write() propagate MemTxResult")
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4700
Reported-by: Karl Mehltretter
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
---
The residual check is effectively dead code. Both nvme_map_prp and
nvme_map_sgl map exactly the requested length, so the residual is always
zero. Keep the check there instead of asserting it, keeping the fix
minimal.
---
hw/nvme/ctrl.c | 10 ++++++++--
hw/nvme/trace-events | 1 +
2 files changed, 9 insertions(+), 2 deletions(-)
diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c
index 71c24dc02b67..5e8788a257da 100644
--- a/hw/nvme/ctrl.c
+++ b/hw/nvme/ctrl.c
@@ -1380,11 +1380,17 @@ static uint16_t nvme_tx(NvmeCtrl *n, NvmeSg *sg, void *ptr, uint32_t len,
if (sg->flags & NVME_SG_DMA) {
const MemTxAttrs attrs = MEMTXATTRS_UNSPECIFIED;
dma_addr_t residual;
+ MemTxResult res;
if (dir == NVME_TX_DIRECTION_TO_DEVICE) {
- dma_buf_write(ptr, len, &residual, &sg->qsg, attrs);
+ res = dma_buf_write(ptr, len, &residual, &sg->qsg, attrs);
} else {
- dma_buf_read(ptr, len, &residual, &sg->qsg, attrs);
+ res = dma_buf_read(ptr, len, &residual, &sg->qsg, attrs);
+ }
+
+ if (unlikely(res != MEMTX_OK)) {
+ trace_pci_nvme_err_data_transfer(res);
+ return NVME_DATA_TRAS_ERROR;
}
if (unlikely(residual)) {
diff --git a/hw/nvme/trace-events b/hw/nvme/trace-events
index f97a6a11f360..7cc4d1975afb 100644
--- a/hw/nvme/trace-events
+++ b/hw/nvme/trace-events
@@ -139,6 +139,7 @@ pci_nvme_err_addr_write(uint64_t addr) "addr 0x%"PRIx64""
pci_nvme_err_cfs(void) "controller fatal status"
pci_nvme_err_aio(uint16_t cid, const char *errname, uint16_t status) "cid %"PRIu16" err '%s' status 0x%"PRIx16""
pci_nvme_err_copy_invalid_format(uint8_t format) "format 0x%"PRIx8""
+pci_nvme_err_data_transfer(int result) "result %d"
pci_nvme_err_invalid_sgld(uint16_t cid, uint8_t typ) "cid %"PRIu16" type 0x%"PRIx8""
pci_nvme_err_invalid_num_sgld(uint16_t cid, uint8_t typ) "cid %"PRIu16" type 0x%"PRIx8""
pci_nvme_err_invalid_sgl_excess_length(uint32_t residual) "residual %"PRIu32""
---
base-commit: 97c88c61d45dee6e1f9bc160a338048189846d28
change-id: 20261117-fix-4700-111c59d3a60f
Best regards,
--
Klaus Jensen <k.jensen@samsung.com>
On Tue, 17 Nov 2026 12:35:21 +0100, Klaus Jensen <its@irrelevant.dk> wrote:
> From: Klaus Jensen <k.jensen@samsung.com>
>
> nvme_tx discards the MemTxResult returned by dma_buf_read/write and only
> inspects the residual. dma_buf_rw already accounts failed transactions
> against the residual, so a full-length transfer to or from unmapped
> memory results in a zero residual and an apparently successful transfer.
>
> Check the memory transaction result instead and complete the command
> with a Data Transfer Error on error and add a new trace event that
> actually describe the event correctly.
>
> Cc: qemu-stable@nongnu.org
> Fixes: f02b664aad8f ("hw/dma: Let dma_buf_read() / dma_buf_write() propagate MemTxResult")
> Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4700
> Reported-by: Karl Mehltretter
> Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
> ---
> The residual check is effectively dead code. Both nvme_map_prp and
> nvme_map_sgl map exactly the requested length, so the residual is always
> zero. Keep the check there instead of asserting it, keeping the fix
> minimal.
> ---
> hw/nvme/ctrl.c | 10 ++++++++--
> hw/nvme/trace-events | 1 +
> 2 files changed, 9 insertions(+), 2 deletions(-)
>
> diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c
> index 71c24dc02b67..5e8788a257da 100644
> --- a/hw/nvme/ctrl.c
> +++ b/hw/nvme/ctrl.c
> @@ -1380,11 +1380,17 @@ static uint16_t nvme_tx(NvmeCtrl *n, NvmeSg *sg, void *ptr, uint32_t len,
> if (sg->flags & NVME_SG_DMA) {
> const MemTxAttrs attrs = MEMTXATTRS_UNSPECIFIED;
> dma_addr_t residual;
> + MemTxResult res;
>
> if (dir == NVME_TX_DIRECTION_TO_DEVICE) {
> - dma_buf_write(ptr, len, &residual, &sg->qsg, attrs);
> + res = dma_buf_write(ptr, len, &residual, &sg->qsg, attrs);
> } else {
> - dma_buf_read(ptr, len, &residual, &sg->qsg, attrs);
> + res = dma_buf_read(ptr, len, &residual, &sg->qsg, attrs);
> + }
> +
> + if (unlikely(res != MEMTX_OK)) {
> + trace_pci_nvme_err_data_transfer(res);
> + return NVME_DATA_TRAS_ERROR;
> }
>
> if (unlikely(residual)) {
> diff --git a/hw/nvme/trace-events b/hw/nvme/trace-events
> index f97a6a11f360..7cc4d1975afb 100644
> --- a/hw/nvme/trace-events
> +++ b/hw/nvme/trace-events
> @@ -139,6 +139,7 @@ pci_nvme_err_addr_write(uint64_t addr) "addr 0x%"PRIx64""
> pci_nvme_err_cfs(void) "controller fatal status"
> pci_nvme_err_aio(uint16_t cid, const char *errname, uint16_t status) "cid %"PRIu16" err '%s' status 0x%"PRIx16""
> pci_nvme_err_copy_invalid_format(uint8_t format) "format 0x%"PRIx8""
> +pci_nvme_err_data_transfer(int result) "result %d"
> pci_nvme_err_invalid_sgld(uint16_t cid, uint8_t typ) "cid %"PRIu16" type 0x%"PRIx8""
> pci_nvme_err_invalid_num_sgld(uint16_t cid, uint8_t typ) "cid %"PRIu16" type 0x%"PRIx8""
> pci_nvme_err_invalid_sgl_excess_length(uint32_t residual) "residual %"PRIu32""
>
> ---
> base-commit: 97c88c61d45dee6e1f9bc160a338048189846d28
> change-id: 20261117-fix-4700-111c59d3a60f
>
> Best regards,
> --
> Klaus Jensen <k.jensen@samsung.com>
>
>
>
Reviewed-by: Jesper Wendel Devantier <foss@defmacro.it>
© 2016 - 2026 Red Hat, Inc.