* [PATCH 0/4] iommu/dma: restore flush queue use in dma_iova_destroy() [not found] <CGME20261127115858eucas1p1ac63e4ce0f221453e8666ab9723a2ae0@eucas1p1.samsung.com> @ 2026-11-27 11:58 ` Marek Szyprowski 2026-11-27 11:58 ` [PATCH 1/4] media: venus: use dma_iova_size() to check the reserved IOVA size Marek Szyprowski ` (3 more replies) 0 siblings, 4 replies; 11+ messages in thread From: Marek Szyprowski @ 2026-11-27 11:58 UTC (permalink / raw) To: iommu, linux-media, linux-arm-msm, kvm, linux-kernel Cc: Marek Szyprowski, Robin Murphy, Joerg Roedel, Will Deacon, Leon Romanovsky, Jason Gunthorpe, Sibi Sankar, Honglei Huang, prakash.gupta, Alex Williamson, Vikash Garodia, Dikshita Agarwal, Abhinav Kumar, Bryan O'Donoghue, Mauro Carvalho Chehab, Hans Verkuil Hi, Commit 1acce29926d9 ("iommu/dma: simplify dma_iova_destroy() and drop the free_iova helper") made dma_iova_destroy() always invalidate the IOTLB synchronously and free the IOVA directly, bypassing the flush queue on DMA-FQ domains. As Sibi reported [1], this makes iommu.strict=0 ineffective for NVMe I/O (blk_rq_dma_map()) and costs about 4x of sequential throughput on SMMUv3 without RANGE_INV. This series is an alternative to the revert proposed in [1]. Instead of restoring the free_iova flag, dma_iova_try_alloc() decides once whether the IOVA space is going to be released through the flush queue and records that in a new DMA_IOVA_USE_FQ flag in dma_iova_state::__size, next to DMA_IOVA_USE_SWIOTLB, so struct dma_iova_state does not grow. dma_iova_destroy() then unmaps with a gather queued according to that flag and retires it together with the IOVA free, exactly like __iommu_dma_unmap(): either by iommu_iotlb_sync() or by handing the IOVA (and any released page table pages) over to the flush queue. The gather is never left dangling and the IOVA is never returned to the allocator before a pending queued invalidation, as pointed out by Jason. dma_iova_unlink() stays strict, as the unlinked range remains owned by the caller and may be linked again right away. The whole reservation is still freed, keeping the size fix of the original cleanup. Storing a flag in the upper bits of __size requires that nobody reads __size directly, so patches 1-2 convert the venus and iris IOVA reservation code to dma_iova_size() and patch 3 adds a DMA_IOVA_FLAGS mask used by dma_iova_size(), dma_iova_try_alloc() and the vfio-pci dma-buf length check. Patch 4 is the actual fix. The series has been developped with an aid of LLM, build tested on arm64 and x86_64. Sibi, it would be great if you could rerun your fio test on top of it. [1] https://lore.kernel.org/all/20261125135120.1126571-1-sibi.sankar@oss.qualcomm.com/ Best regards Marek Szyprowski, PhD Samsung R&D Institute Poland Patch summary: Marek Szyprowski (4): media: venus: use dma_iova_size() to check the reserved IOVA size media: iris: use dma_iova_size() to check the reserved IOVA size dma-mapping: add DMA_IOVA_FLAGS mask for the dma_iova_state size flags iommu/dma: restore flush queue use in dma_iova_destroy() drivers/iommu/dma-iommu.c | 111 +++++++++++++----- drivers/media/platform/qcom/iris/iris_probe.c | 3 +- drivers/media/platform/qcom/venus/core.c | 3 +- drivers/vfio/pci/vfio_pci_dmabuf.c | 2 +- include/linux/dma-mapping.h | 14 ++- 5 files changed, 98 insertions(+), 35 deletions(-) -- 2.43.0 ^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH 1/4] media: venus: use dma_iova_size() to check the reserved IOVA size 2026-11-27 11:58 ` [PATCH 0/4] iommu/dma: restore flush queue use in dma_iova_destroy() Marek Szyprowski @ 2026-11-27 11:58 ` Marek Szyprowski 2026-11-27 12:41 ` Vikash Garodia 2026-11-27 14:29 ` Jason Gunthorpe 2026-11-27 11:58 ` [PATCH 2/4] media: iris: " Marek Szyprowski ` (2 subsequent siblings) 3 siblings, 2 replies; 11+ messages in thread From: Marek Szyprowski @ 2026-11-27 11:58 UTC (permalink / raw) To: iommu, linux-media, linux-arm-msm, kvm, linux-kernel Cc: Marek Szyprowski, Robin Murphy, Joerg Roedel, Will Deacon, Leon Romanovsky, Jason Gunthorpe, Sibi Sankar, Honglei Huang, prakash.gupta, Alex Williamson, Vikash Garodia, Dikshita Agarwal, Abhinav Kumar, Bryan O'Donoghue, Mauro Carvalho Chehab, Hans Verkuil, stable The IOVA reservation code compares dma_iova_state::__size directly with the requested chunk size. __size is internal to the DMA IOVA API and may carry flags in its upper bits, so use the dma_iova_size() accessor instead. Fixes: 953439251b59 ("media: venus: Fix iova allocation from restrict region") Cc: stable@vger.kernel.org # v7.4+ Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com> --- drivers/media/platform/qcom/venus/core.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/media/platform/qcom/venus/core.c b/drivers/media/platform/qcom/venus/core.c index 7087af32060f..cec5db2bf755 100644 --- a/drivers/media/platform/qcom/venus/core.c +++ b/drivers/media/platform/qcom/venus/core.c @@ -408,7 +408,8 @@ static int venus_reserve_iova_region(struct device *dev, struct dma_iova_state * if (!dma_iova_try_alloc(dev, &state[count], 0, chunk)) goto err_free_iova; - if (state[count].addr != end - chunk || state[count].__size != chunk) + if (state[count].addr != end - chunk || + dma_iova_size(&state[count]) != chunk) goto err_free_iova; rem -= chunk; -- 2.43.0 ^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH 1/4] media: venus: use dma_iova_size() to check the reserved IOVA size 2026-11-27 11:58 ` [PATCH 1/4] media: venus: use dma_iova_size() to check the reserved IOVA size Marek Szyprowski @ 2026-11-27 12:41 ` Vikash Garodia 2026-11-27 14:29 ` Jason Gunthorpe 1 sibling, 0 replies; 11+ messages in thread From: Vikash Garodia @ 2026-11-27 12:41 UTC (permalink / raw) To: Marek Szyprowski, iommu, linux-media, linux-arm-msm, kvm, linux-kernel Cc: Robin Murphy, Joerg Roedel, Will Deacon, Leon Romanovsky, Jason Gunthorpe, Sibi Sankar, Honglei Huang, prakash.gupta, Alex Williamson, Dikshita Agarwal, Abhinav Kumar, Bryan O'Donoghue, Mauro Carvalho Chehab, Hans Verkuil, stable On 11/27/2026 5:28 PM, Marek Szyprowski wrote: > The IOVA reservation code compares dma_iova_state::__size directly with > the requested chunk size. __size is internal to the DMA IOVA API and may > carry flags in its upper bits, so use the dma_iova_size() accessor > instead. > > Fixes: 953439251b59 ("media: venus: Fix iova allocation from restrict region") > Cc:stable@vger.kernel.org # v7.4+ > Signed-off-by: Marek Szyprowski<m.szyprowski@samsung.com> > --- > drivers/media/platform/qcom/venus/core.c | 3 ++- > 1 file changed, 2 insertions(+), 1 deletion(-) Reviewed-by: Vikash Garodia <vikash.garodia@oss.qualcomm.com> ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH 1/4] media: venus: use dma_iova_size() to check the reserved IOVA size 2026-11-27 11:58 ` [PATCH 1/4] media: venus: use dma_iova_size() to check the reserved IOVA size Marek Szyprowski 2026-11-27 12:41 ` Vikash Garodia @ 2026-11-27 14:29 ` Jason Gunthorpe 1 sibling, 0 replies; 11+ messages in thread From: Jason Gunthorpe @ 2026-11-27 14:29 UTC (permalink / raw) To: Marek Szyprowski Cc: iommu, linux-media, linux-arm-msm, kvm, linux-kernel, Robin Murphy, Joerg Roedel, Will Deacon, Leon Romanovsky, Sibi Sankar, Honglei Huang, prakash.gupta, Alex Williamson, Vikash Garodia, Dikshita Agarwal, Abhinav Kumar, Bryan O'Donoghue, Mauro Carvalho Chehab, Hans Verkuil, stable On Fri, Nov 27, 2026 at 12:58:45PM +0100, Marek Szyprowski wrote: > The IOVA reservation code compares dma_iova_state::__size directly with > the requested chunk size. __size is internal to the DMA IOVA API and may > carry flags in its upper bits, so use the dma_iova_size() accessor > instead. WTF is this even doing? Fix this by reserving the 0-600MB range, below the boundary the non-pixel stream cannot address, using dma_iova_try_alloc() so the IOMMU-DMA core never hands that range out to a real DMA mapping. This reserves only IOVA space, it does not allocate any physical memory. That isn't what this API is for, and it isn't how it even works! It is pure luck that the first call to it happens to take out the lower address space. We even had a long discussion about this driver and this need in another thread and this is the hack job you guys merged? And it couldn't even use the right APIs??! Jason ^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH 2/4] media: iris: use dma_iova_size() to check the reserved IOVA size 2026-11-27 11:58 ` [PATCH 0/4] iommu/dma: restore flush queue use in dma_iova_destroy() Marek Szyprowski 2026-11-27 11:58 ` [PATCH 1/4] media: venus: use dma_iova_size() to check the reserved IOVA size Marek Szyprowski @ 2026-11-27 11:58 ` Marek Szyprowski 2026-11-27 12:41 ` Vikash Garodia 2026-11-27 11:58 ` [PATCH 3/4] dma-mapping: add DMA_IOVA_FLAGS mask for the dma_iova_state size flags Marek Szyprowski 2026-11-27 11:58 ` [PATCH 4/4] iommu/dma: restore flush queue use in dma_iova_destroy() Marek Szyprowski 3 siblings, 1 reply; 11+ messages in thread From: Marek Szyprowski @ 2026-11-27 11:58 UTC (permalink / raw) To: iommu, linux-media, linux-arm-msm, kvm, linux-kernel Cc: Marek Szyprowski, Robin Murphy, Joerg Roedel, Will Deacon, Leon Romanovsky, Jason Gunthorpe, Sibi Sankar, Honglei Huang, prakash.gupta, Alex Williamson, Vikash Garodia, Dikshita Agarwal, Abhinav Kumar, Bryan O'Donoghue, Mauro Carvalho Chehab, Hans Verkuil, stable The IOVA reservation code compares dma_iova_state::__size directly with the requested chunk size. __size is internal to the DMA IOVA API and may carry flags in its upper bits, so use the dma_iova_size() accessor instead. Fixes: 200473c198b2 ("media: iris: Fix iova allocation from restrict region") Cc: stable@vger.kernel.org # v7.4+ Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com> --- drivers/media/platform/qcom/iris/iris_probe.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/media/platform/qcom/iris/iris_probe.c b/drivers/media/platform/qcom/iris/iris_probe.c index 41dec90f3e2b..0af913e40604 100644 --- a/drivers/media/platform/qcom/iris/iris_probe.c +++ b/drivers/media/platform/qcom/iris/iris_probe.c @@ -181,7 +181,8 @@ static int iris_reserve_iova_region(struct device *dev, struct dma_iova_state ** if (!dma_iova_try_alloc(dev, &state[count], 0, chunk)) goto err_free_iova; - if (state[count].addr != end - chunk || state[count].__size != chunk) + if (state[count].addr != end - chunk || + dma_iova_size(&state[count]) != chunk) goto err_free_iova; rem -= chunk; -- 2.43.0 ^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH 2/4] media: iris: use dma_iova_size() to check the reserved IOVA size 2026-11-27 11:58 ` [PATCH 2/4] media: iris: " Marek Szyprowski @ 2026-11-27 12:41 ` Vikash Garodia 0 siblings, 0 replies; 11+ messages in thread From: Vikash Garodia @ 2026-11-27 12:41 UTC (permalink / raw) To: Marek Szyprowski, iommu, linux-media, linux-arm-msm, kvm, linux-kernel Cc: Robin Murphy, Joerg Roedel, Will Deacon, Leon Romanovsky, Jason Gunthorpe, Sibi Sankar, Honglei Huang, prakash.gupta, Alex Williamson, Dikshita Agarwal, Abhinav Kumar, Bryan O'Donoghue, Mauro Carvalho Chehab, Hans Verkuil, stable On 11/27/2026 5:28 PM, Marek Szyprowski wrote: > The IOVA reservation code compares dma_iova_state::__size directly with > the requested chunk size. __size is internal to the DMA IOVA API and may > carry flags in its upper bits, so use the dma_iova_size() accessor > instead. > > Fixes: 200473c198b2 ("media: iris: Fix iova allocation from restrict region") > Cc:stable@vger.kernel.org # v7.4+ > Signed-off-by: Marek Szyprowski<m.szyprowski@samsung.com> > --- > drivers/media/platform/qcom/iris/iris_probe.c | 3 ++- > 1 file changed, 2 insertions(+), 1 deletion(-) Reviewed-by: Vikash Garodia <vikash.garodia@oss.qualcomm.com> ^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH 3/4] dma-mapping: add DMA_IOVA_FLAGS mask for the dma_iova_state size flags 2026-11-27 11:58 ` [PATCH 0/4] iommu/dma: restore flush queue use in dma_iova_destroy() Marek Szyprowski 2026-11-27 11:58 ` [PATCH 1/4] media: venus: use dma_iova_size() to check the reserved IOVA size Marek Szyprowski 2026-11-27 11:58 ` [PATCH 2/4] media: iris: " Marek Szyprowski @ 2026-11-27 11:58 ` Marek Szyprowski 2026-11-27 14:37 ` Jason Gunthorpe 2026-11-27 11:58 ` [PATCH 4/4] iommu/dma: restore flush queue use in dma_iova_destroy() Marek Szyprowski 3 siblings, 1 reply; 11+ messages in thread From: Marek Szyprowski @ 2026-11-27 11:58 UTC (permalink / raw) To: iommu, linux-media, linux-arm-msm, kvm, linux-kernel Cc: Marek Szyprowski, Robin Murphy, Joerg Roedel, Will Deacon, Leon Romanovsky, Jason Gunthorpe, Sibi Sankar, Honglei Huang, prakash.gupta, Alex Williamson, Vikash Garodia, Dikshita Agarwal, Abhinav Kumar, Bryan O'Donoghue, Mauro Carvalho Chehab, Hans Verkuil, stable dma_iova_state::__size uses its top bit to record that SWIOTLB was used for one of the linked ranges. Users that mask or validate sizes refer to DMA_IOVA_USE_SWIOTLB directly, so adding another flag would require updating all of them. Add DMA_IOVA_FLAGS covering all flags kept in __size and use it in dma_iova_size(), in the size check of dma_iova_try_alloc() and in the vfio-pci dma-buf length validation. No functional change. Cc: stable@vger.kernel.org # v7.3+ Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com> --- drivers/iommu/dma-iommu.c | 8 ++++---- drivers/vfio/pci/vfio_pci_dmabuf.c | 2 +- include/linux/dma-mapping.h | 8 +++++++- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index 78742aeba932..99eb382a1359 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -1838,11 +1838,11 @@ bool dma_iova_try_alloc(struct device *dev, struct dma_iova_state *state, return false; /* - * DMA_IOVA_USE_SWIOTLB is flag which is set by dma-iommu - * internals, make sure that caller didn't set it and/or - * didn't use this interface to map SIZE_MAX. + * DMA_IOVA_FLAGS are set by dma-iommu internals, make sure that + * caller didn't set them and/or didn't use this interface to map + * SIZE_MAX. */ - if (WARN_ON_ONCE((u64)size & DMA_IOVA_USE_SWIOTLB)) + if (WARN_ON_ONCE((u64)size & DMA_IOVA_FLAGS)) return false; addr = iommu_dma_alloc_iova(domain, diff --git a/drivers/vfio/pci/vfio_pci_dmabuf.c b/drivers/vfio/pci/vfio_pci_dmabuf.c index c16f460c01d6..8bda610dd988 100644 --- a/drivers/vfio/pci/vfio_pci_dmabuf.c +++ b/drivers/vfio/pci/vfio_pci_dmabuf.c @@ -211,7 +211,7 @@ static int validate_dmabuf_input(struct vfio_device_feature_dma_buf *dma_buf, * dma_iova_try_alloc() will WARN on if userspace proposes a size that * is too big, eg with lots of ranges. */ - if ((u64)(length) & DMA_IOVA_USE_SWIOTLB) + if ((u64)(length) & DMA_IOVA_FLAGS) return -EINVAL; *lengthp = length; diff --git a/include/linux/dma-mapping.h b/include/linux/dma-mapping.h index efde0e5a58dc..0ebf3090ea0f 100644 --- a/include/linux/dma-mapping.h +++ b/include/linux/dma-mapping.h @@ -135,10 +135,16 @@ struct dma_iova_state { */ #define DMA_IOVA_USE_SWIOTLB (1ULL << 63) +/* + * All flags stored in the upper bits of dma_iova_state::__size. Sizes passed + * to dma_iova_try_alloc() must not overlap with them. + */ +#define DMA_IOVA_FLAGS DMA_IOVA_USE_SWIOTLB + static inline size_t dma_iova_size(struct dma_iova_state *state) { /* Casting is needed for 32-bits systems */ - return (size_t)(state->__size & ~DMA_IOVA_USE_SWIOTLB); + return (size_t)(state->__size & ~DMA_IOVA_FLAGS); } #ifdef CONFIG_DMA_API_DEBUG -- 2.43.0 ^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH 3/4] dma-mapping: add DMA_IOVA_FLAGS mask for the dma_iova_state size flags 2026-11-27 11:58 ` [PATCH 3/4] dma-mapping: add DMA_IOVA_FLAGS mask for the dma_iova_state size flags Marek Szyprowski @ 2026-11-27 14:37 ` Jason Gunthorpe 0 siblings, 0 replies; 11+ messages in thread From: Jason Gunthorpe @ 2026-11-27 14:37 UTC (permalink / raw) To: Marek Szyprowski Cc: iommu, linux-media, linux-arm-msm, kvm, linux-kernel, Robin Murphy, Joerg Roedel, Will Deacon, Leon Romanovsky, Sibi Sankar, Honglei Huang, prakash.gupta, Alex Williamson, Vikash Garodia, Dikshita Agarwal, Abhinav Kumar, Bryan O'Donoghue, Mauro Carvalho Chehab, Hans Verkuil, stable On Fri, Nov 27, 2026 at 12:58:47PM +0100, Marek Szyprowski wrote: > dma_iova_state::__size uses its top bit to record that SWIOTLB was used > for one of the linked ranges. Users that mask or validate sizes refer to > DMA_IOVA_USE_SWIOTLB directly, so adding another flag would require > updating all of them. > > Add DMA_IOVA_FLAGS covering all flags kept in __size and use it in > dma_iova_size(), in the size check of dma_iova_try_alloc() and in the > vfio-pci dma-buf length validation. No functional change. > > Cc: stable@vger.kernel.org # v7.3+ > Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com> > --- > drivers/iommu/dma-iommu.c | 8 ++++---- > drivers/vfio/pci/vfio_pci_dmabuf.c | 2 +- > include/linux/dma-mapping.h | 8 +++++++- > 3 files changed, 12 insertions(+), 6 deletions(-) Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Jason ^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH 4/4] iommu/dma: restore flush queue use in dma_iova_destroy() 2026-11-27 11:58 ` [PATCH 0/4] iommu/dma: restore flush queue use in dma_iova_destroy() Marek Szyprowski ` (2 preceding siblings ...) 2026-11-27 11:58 ` [PATCH 3/4] dma-mapping: add DMA_IOVA_FLAGS mask for the dma_iova_state size flags Marek Szyprowski @ 2026-11-27 11:58 ` Marek Szyprowski 2026-11-27 14:37 ` Jason Gunthorpe 3 siblings, 1 reply; 11+ messages in thread From: Marek Szyprowski @ 2026-11-27 11:58 UTC (permalink / raw) To: iommu, linux-media, linux-arm-msm, kvm, linux-kernel Cc: Marek Szyprowski, Robin Murphy, Joerg Roedel, Will Deacon, Leon Romanovsky, Jason Gunthorpe, Sibi Sankar, Honglei Huang, prakash.gupta, Alex Williamson, Vikash Garodia, Dikshita Agarwal, Abhinav Kumar, Bryan O'Donoghue, Mauro Carvalho Chehab, Hans Verkuil, stable Commit 1acce29926d9 ("iommu/dma: simplify dma_iova_destroy() and drop the free_iova helper") turned dma_iova_destroy() into dma_iova_unlink() followed by dma_iova_free(). Before it, the destroy path marked its IOTLB gather as queued for DMA-FQ domains, skipped iommu_iotlb_sync() and handed the IOVA over to the flush queue, exactly like __iommu_dma_unmap(). After it: - dma_iova_unlink() never queues its gather and always calls iommu_iotlb_sync() after iommu_unmap_fast(), - dma_iova_free() passes a NULL gather to iommu_dma_free_iova(), so the IOVA goes straight back to the allocator via free_iova_fast(). DMA-FQ domains therefore get strict invalidation on this path. This includes blk_rq_dma_map(), which NVMe uses for all data transfers, so iommu.strict=0 has no effect on NVMe I/O. On a Qualcomm Glymur CRD (SMMUv3 without RANGE_INV, 4K pages, NVMe on PCIe Gen5 x4) sequential fio throughput drops from 10.6/11.9 GiB/s (write/read) to the strict mode level of ~2.7 GiB/s. Instead of reverting, decide once in dma_iova_try_alloc() whether the IOVA space is going to be released through the flush queue and record it in a new DMA_IOVA_USE_FQ flag in dma_iova_state::__size, next to DMA_IOVA_USE_SWIOTLB, so struct dma_iova_state does not grow. dma_iova_destroy() then unmaps with a gather queued according to that flag, skipping the sync for flush queue domains, and passes the same gather to the IOVA free, so the IOVA space (and any page table pages released by the unmap) goes through queue_iova() again. dma_iova_free() honours the flag as well. The whole reservation is still freed, keeping the size fix of the reverted commit. A gather must always be retired, either by iommu_iotlb_sync() or by handing it to the flush queue together with the IOVA, and the IOVA must never be returned to the allocator while a queued invalidation for it is still pending. To keep that obvious, the internal unlink helper only unmaps into a gather supplied by its caller, and each caller retires it right away: dma_iova_destroy() syncs or queues it together with the IOVA free, exactly like __iommu_dma_unmap(), while dma_iova_unlink() always uses a never queued gather and syncs it, as the unlinked range remains owned by the caller and may be linked again immediately. Fixes: 1acce29926d9 ("iommu/dma: simplify dma_iova_destroy() and drop the free_iova helper") Reported-by: Sibi Sankar <sibi.sankar@oss.qualcomm.com> Closes: https://lore.kernel.org/all/20261125135120.1126571-1-sibi.sankar@oss.qualcomm.com/ Suggested-by: Leon Romanovsky <leon@kernel.org> Cc: stable@vger.kernel.org # v7.3+ Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com> --- drivers/iommu/dma-iommu.c | 103 ++++++++++++++++++++++++++---------- include/linux/dma-mapping.h | 8 ++- 2 files changed, 83 insertions(+), 28 deletions(-) diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index 99eb382a1359..0e329ecd7dfa 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -1853,10 +1853,31 @@ bool dma_iova_try_alloc(struct device *dev, struct dma_iova_state *state, state->addr = addr + iova_off; state->__size = size; + /* + * Decide once, for the whole life cycle of @state, whether the IOVA + * space is going to be released through the flush queue. A domain + * can only move from strict to lazy mode, so a stale "not queued" + * value is always safe. + */ + if (READ_ONCE(cookie->fq_domain)) + state->__size |= DMA_IOVA_USE_FQ; return true; } EXPORT_SYMBOL_GPL(dma_iova_try_alloc); +static void __dma_iova_free(struct device *dev, struct dma_iova_state *state, + struct iommu_iotlb_gather *gather) +{ + struct iommu_domain *domain = iommu_get_dma_domain(dev); + struct iommu_dma_cookie *cookie = domain->iova_cookie; + struct iova_domain *iovad = &cookie->iovad; + size_t iova_start_pad = iova_offset(iovad, state->addr); + size_t size = dma_iova_size(state); + + iommu_dma_free_iova(domain, state->addr - iova_start_pad, + iova_align(iovad, size + iova_start_pad), gather); +} + /** * dma_iova_free - Free an IOVA space * @dev: Device to free the IOVA space for @@ -1871,14 +1892,11 @@ EXPORT_SYMBOL_GPL(dma_iova_try_alloc); */ void dma_iova_free(struct device *dev, struct dma_iova_state *state) { - struct iommu_domain *domain = iommu_get_dma_domain(dev); - struct iommu_dma_cookie *cookie = domain->iova_cookie; - struct iova_domain *iovad = &cookie->iovad; - size_t iova_start_pad = iova_offset(iovad, state->addr); - size_t size = dma_iova_size(state); + struct iommu_iotlb_gather iotlb_gather; - iommu_dma_free_iova(domain, state->addr - iova_start_pad, - iova_align(iovad, size + iova_start_pad), NULL); + iommu_iotlb_gather_init(&iotlb_gather); + iotlb_gather.queued = state->__size & DMA_IOVA_USE_FQ; + __dma_iova_free(dev, state, &iotlb_gather); } EXPORT_SYMBOL_GPL(dma_iova_free); @@ -2097,27 +2115,20 @@ static void iommu_dma_iova_unlink_range_slow(struct device *dev, arch_sync_dma_flush(); } -/** - * dma_iova_unlink - Unlink a range of IOVA space - * @dev: DMA device - * @state: IOVA state - * @offset: offset into the IOVA state to unlink - * @size: size of the buffer - * @dir: DMA direction - * @attrs: attributes of mapping properties - * - * Unlink a range of IOVA space for the given IOVA state. +/* + * Unmap a range of IOVA space into @gather. The caller owns @gather and must + * retire it, either by iommu_iotlb_sync() or, for a queued gather, by handing + * it over to the flush queue together with the IOVA via iommu_dma_free_iova(). */ -void dma_iova_unlink(struct device *dev, struct dma_iova_state *state, +static void __dma_iova_unlink(struct device *dev, struct dma_iova_state *state, size_t offset, size_t size, enum dma_data_direction dir, - unsigned long attrs) + unsigned long attrs, struct iommu_iotlb_gather *gather) { struct iommu_domain *domain = iommu_get_dma_domain(dev); struct iommu_dma_cookie *cookie = domain->iova_cookie; struct iova_domain *iovad = &cookie->iovad; dma_addr_t addr = state->addr + offset; size_t iova_start_pad = iova_offset(iovad, addr); - struct iommu_iotlb_gather iotlb_gather; size_t unmapped; if ((state->__size & DMA_IOVA_USE_SWIOTLB) || @@ -2125,14 +2136,38 @@ void dma_iova_unlink(struct device *dev, struct dma_iova_state *state, !(attrs & (DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_MMIO)))) iommu_dma_iova_unlink_range_slow(dev, addr, size, dir, attrs); - iommu_iotlb_gather_init(&iotlb_gather); - size = iova_align(iovad, size + iova_start_pad); addr -= iova_start_pad; - unmapped = iommu_unmap_fast(domain, addr, size, &iotlb_gather); + unmapped = iommu_unmap_fast(domain, addr, size, gather); WARN_ON(unmapped != size); +} - iommu_iotlb_sync(domain, &iotlb_gather); +/** + * dma_iova_unlink - Unlink a range of IOVA space + * @dev: DMA device + * @state: IOVA state + * @offset: offset into the IOVA state to unlink + * @size: size of the buffer + * @dir: DMA direction + * @attrs: attributes of mapping properties + * + * Unlink a range of IOVA space for the given IOVA state. + */ +void dma_iova_unlink(struct device *dev, struct dma_iova_state *state, + size_t offset, size_t size, enum dma_data_direction dir, + unsigned long attrs) +{ + struct iommu_iotlb_gather iotlb_gather; + + /* + * The IOVA range stays owned by the caller and may be linked again + * right away, so the IOTLB has to be invalidated synchronously even + * for flush queue domains. The gather is therefore never queued and + * is retired right here. + */ + iommu_iotlb_gather_init(&iotlb_gather); + __dma_iova_unlink(dev, state, offset, size, dir, attrs, &iotlb_gather); + iommu_iotlb_sync(iommu_get_dma_domain(dev), &iotlb_gather); } EXPORT_SYMBOL_GPL(dma_iova_unlink); @@ -2152,14 +2187,28 @@ void dma_iova_destroy(struct device *dev, struct dma_iova_state *state, size_t mapped_len, enum dma_data_direction dir, unsigned long attrs) { - if (mapped_len) - dma_iova_unlink(dev, state, 0, mapped_len, dir, attrs); + struct iommu_iotlb_gather iotlb_gather; + + iommu_iotlb_gather_init(&iotlb_gather); + iotlb_gather.queued = state->__size & DMA_IOVA_USE_FQ; /* * We can be here if the first call to dma_iova_link() failed and * there is nothing to unlink, so let's be more clear. */ - dma_iova_free(dev, state); + if (mapped_len) + __dma_iova_unlink(dev, state, 0, mapped_len, dir, attrs, + &iotlb_gather); + + /* + * Retire the gather together with the IOVA, like __iommu_dma_unmap() + * does: either invalidate the IOTLB now, or hand the IOVA space and + * any page table pages released by the unmap over to the flush queue, + * so the IOVA cannot be reused before the deferred invalidation. + */ + if (!iotlb_gather.queued) + iommu_iotlb_sync(iommu_get_dma_domain(dev), &iotlb_gather); + __dma_iova_free(dev, state, &iotlb_gather); } EXPORT_SYMBOL_GPL(dma_iova_destroy); diff --git a/include/linux/dma-mapping.h b/include/linux/dma-mapping.h index 0ebf3090ea0f..7524c14f1262 100644 --- a/include/linux/dma-mapping.h +++ b/include/linux/dma-mapping.h @@ -135,11 +135,17 @@ struct dma_iova_state { */ #define DMA_IOVA_USE_SWIOTLB (1ULL << 63) +/* + * Set when the IOVA space is going to be released through the IOMMU flush + * queue. Internal to dma-iommu. + */ +#define DMA_IOVA_USE_FQ (1ULL << 62) + /* * All flags stored in the upper bits of dma_iova_state::__size. Sizes passed * to dma_iova_try_alloc() must not overlap with them. */ -#define DMA_IOVA_FLAGS DMA_IOVA_USE_SWIOTLB +#define DMA_IOVA_FLAGS (DMA_IOVA_USE_SWIOTLB | DMA_IOVA_USE_FQ) static inline size_t dma_iova_size(struct dma_iova_state *state) { -- 2.43.0 ^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH 4/4] iommu/dma: restore flush queue use in dma_iova_destroy() 2026-11-27 11:58 ` [PATCH 4/4] iommu/dma: restore flush queue use in dma_iova_destroy() Marek Szyprowski @ 2026-11-27 14:37 ` Jason Gunthorpe 2026-11-27 14:45 ` Marek Szyprowski 0 siblings, 1 reply; 11+ messages in thread From: Jason Gunthorpe @ 2026-11-27 14:37 UTC (permalink / raw) To: Marek Szyprowski Cc: iommu, linux-media, linux-arm-msm, kvm, linux-kernel, Robin Murphy, Joerg Roedel, Will Deacon, Leon Romanovsky, Sibi Sankar, Honglei Huang, prakash.gupta, Alex Williamson, Vikash Garodia, Dikshita Agarwal, Abhinav Kumar, Bryan O'Donoghue, Mauro Carvalho Chehab, Hans Verkuil, stable On Fri, Nov 27, 2026 at 12:58:48PM +0100, Marek Szyprowski wrote: > Commit 1acce29926d9 ("iommu/dma: simplify dma_iova_destroy() and drop > the free_iova helper") turned dma_iova_destroy() into dma_iova_unlink() > followed by dma_iova_free(). Before it, the destroy path marked its > IOTLB gather as queued for DMA-FQ domains, skipped iommu_iotlb_sync() > and handed the IOVA over to the flush queue, exactly like > __iommu_dma_unmap(). After it: > > - dma_iova_unlink() never queues its gather and always calls > iommu_iotlb_sync() after iommu_unmap_fast(), > - dma_iova_free() passes a NULL gather to iommu_dma_free_iova(), so > the IOVA goes straight back to the allocator via free_iova_fast(). > > DMA-FQ domains therefore get strict invalidation on this path. This > includes blk_rq_dma_map(), which NVMe uses for all data transfers, so > iommu.strict=0 has no effect on NVMe I/O. On a Qualcomm Glymur CRD > (SMMUv3 without RANGE_INV, 4K pages, NVMe on PCIe Gen5 x4) sequential > fio throughput drops from 10.6/11.9 GiB/s (write/read) to the strict > mode level of ~2.7 GiB/s. > > Instead of reverting, decide once in dma_iova_try_alloc() whether the > IOVA space is going to be released through the flush queue and record > it in a new DMA_IOVA_USE_FQ flag in dma_iova_state::__size, next to > DMA_IOVA_USE_SWIOTLB, so struct dma_iova_state does not grow. > dma_iova_destroy() then unmaps with a gather queued according to that > flag, skipping the sync for flush queue domains, and passes the same > gather to the IOVA free, so the IOVA space (and any page table pages > released by the unmap) goes through queue_iova() again. dma_iova_free() > honours the flag as well. The whole reservation is still freed, keeping > the size fix of the reverted commit. > > A gather must always be retired, either by iommu_iotlb_sync() or by > handing it to the flush queue together with the IOVA, and the IOVA must > never be returned to the allocator while a queued invalidation for it is > still pending. To keep that obvious, the internal unlink helper only > unmaps into a gather supplied by its caller, and each caller retires it > right away: dma_iova_destroy() syncs or queues it together with the IOVA > free, exactly like __iommu_dma_unmap(), while dma_iova_unlink() always > uses a never queued gather and syncs it, as the unlinked range remains > owned by the caller and may be linked again immediately. > > Fixes: 1acce29926d9 ("iommu/dma: simplify dma_iova_destroy() and drop the free_iova helper") > Reported-by: Sibi Sankar <sibi.sankar@oss.qualcomm.com> > Closes: https://lore.kernel.org/all/20261125135120.1126571-1-sibi.sankar@oss.qualcomm.com/ > Suggested-by: Leon Romanovsky <leon@kernel.org> > Cc: stable@vger.kernel.org # v7.3+ > Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com> > --- > drivers/iommu/dma-iommu.c | 103 ++++++++++++++++++++++++++---------- > include/linux/dma-mapping.h | 8 ++- > 2 files changed, 83 insertions(+), 28 deletions(-) Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Jason ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH 4/4] iommu/dma: restore flush queue use in dma_iova_destroy() 2026-11-27 14:37 ` Jason Gunthorpe @ 2026-11-27 14:45 ` Marek Szyprowski 0 siblings, 0 replies; 11+ messages in thread From: Marek Szyprowski @ 2026-11-27 14:45 UTC (permalink / raw) To: Jason Gunthorpe Cc: iommu, linux-media, linux-arm-msm, kvm, linux-kernel, Robin Murphy, Joerg Roedel, Will Deacon, Leon Romanovsky, Sibi Sankar, Honglei Huang, prakash.gupta, Alex Williamson, Vikash Garodia, Dikshita Agarwal, Abhinav Kumar, Bryan O'Donoghue, Mauro Carvalho Chehab, Hans Verkuil, stable On 27.11.2026 15:37, Jason Gunthorpe wrote: > On Fri, Nov 27, 2026 at 12:58:48PM +0100, Marek Szyprowski wrote: >> Commit 1acce29926d9 ("iommu/dma: simplify dma_iova_destroy() and drop >> the free_iova helper") turned dma_iova_destroy() into dma_iova_unlink() >> followed by dma_iova_free(). Before it, the destroy path marked its >> IOTLB gather as queued for DMA-FQ domains, skipped iommu_iotlb_sync() >> and handed the IOVA over to the flush queue, exactly like >> __iommu_dma_unmap(). After it: >> >> - dma_iova_unlink() never queues its gather and always calls >> iommu_iotlb_sync() after iommu_unmap_fast(), >> - dma_iova_free() passes a NULL gather to iommu_dma_free_iova(), so >> the IOVA goes straight back to the allocator via free_iova_fast(). >> >> DMA-FQ domains therefore get strict invalidation on this path. This >> includes blk_rq_dma_map(), which NVMe uses for all data transfers, so >> iommu.strict=0 has no effect on NVMe I/O. On a Qualcomm Glymur CRD >> (SMMUv3 without RANGE_INV, 4K pages, NVMe on PCIe Gen5 x4) sequential >> fio throughput drops from 10.6/11.9 GiB/s (write/read) to the strict >> mode level of ~2.7 GiB/s. >> >> Instead of reverting, decide once in dma_iova_try_alloc() whether the >> IOVA space is going to be released through the flush queue and record >> it in a new DMA_IOVA_USE_FQ flag in dma_iova_state::__size, next to >> DMA_IOVA_USE_SWIOTLB, so struct dma_iova_state does not grow. >> dma_iova_destroy() then unmaps with a gather queued according to that >> flag, skipping the sync for flush queue domains, and passes the same >> gather to the IOVA free, so the IOVA space (and any page table pages >> released by the unmap) goes through queue_iova() again. dma_iova_free() >> honours the flag as well. The whole reservation is still freed, keeping >> the size fix of the reverted commit. >> >> A gather must always be retired, either by iommu_iotlb_sync() or by >> handing it to the flush queue together with the IOVA, and the IOVA must >> never be returned to the allocator while a queued invalidation for it is >> still pending. To keep that obvious, the internal unlink helper only >> unmaps into a gather supplied by its caller, and each caller retires it >> right away: dma_iova_destroy() syncs or queues it together with the IOVA >> free, exactly like __iommu_dma_unmap(), while dma_iova_unlink() always >> uses a never queued gather and syncs it, as the unlinked range remains >> owned by the caller and may be linked again immediately. >> >> Fixes: 1acce29926d9 ("iommu/dma: simplify dma_iova_destroy() and drop the free_iova helper") >> Reported-by: Sibi Sankar <sibi.sankar@oss.qualcomm.com> >> Closes: https://lore.kernel.org/all/20261125135120.1126571-1-sibi.sankar@oss.qualcomm.com/ >> Suggested-by: Leon Romanovsky <leon@kernel.org> >> Cc: stable@vger.kernel.org # v7.3+ >> Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com> >> --- >> drivers/iommu/dma-iommu.c | 103 ++++++++++++++++++++++++++---------- >> include/linux/dma-mapping.h | 8 ++- >> 2 files changed, 83 insertions(+), 28 deletions(-) > Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Thanks! What about the issue pointed by sashiko.dev? My LLM said that this will fix it: diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index 0e329ecd7dfa..106da975bf0f 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -1892,11 +1892,12 @@ static void __dma_iova_free(struct device *dev, struct dma_iova_state *state, */ void dma_iova_free(struct device *dev, struct dma_iova_state *state) { - struct iommu_iotlb_gather iotlb_gather; - - iommu_iotlb_gather_init(&iotlb_gather); - iotlb_gather.queued = state->__size & DMA_IOVA_USE_FQ; - __dma_iova_free(dev, state, &iotlb_gather); + /* + * Nothing is linked any more and every dma_iova_unlink() has already + * invalidated the IOTLB synchronously, so there is no deferred + * invalidation to wait for and the IOVA can be reused right away. + */ + __dma_iova_free(dev, state, NULL); } EXPORT_SYMBOL_GPL(dma_iova_free); @@ -2190,12 +2191,13 @@ void dma_iova_destroy(struct device *dev, struct dma_iova_state *state, struct iommu_iotlb_gather iotlb_gather; iommu_iotlb_gather_init(&iotlb_gather); - iotlb_gather.queued = state->__size & DMA_IOVA_USE_FQ; /* * We can be here if the first call to dma_iova_link() failed and - * there is nothing to unlink, so let's be more clear. + * there is nothing to unlink, so let's be more clear. Only queue the + * gather if there is an unmap whose invalidation can be deferred. */ + iotlb_gather.queued = mapped_len && (state->__size & DMA_IOVA_USE_FQ); if (mapped_len) __dma_iova_unlink(dev, state, 0, mapped_len, dir, attrs, &iotlb_gather); Best regards -- Marek Szyprowski, PhD Samsung R&D Institute Poland ^ permalink raw reply related [flat|nested] 11+ messages in thread
end of thread, other threads:[~2026-11-27 14:45 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <CGME20261127115858eucas1p1ac63e4ce0f221453e8666ab9723a2ae0@eucas1p1.samsung.com>
2026-11-27 11:58 ` [PATCH 0/4] iommu/dma: restore flush queue use in dma_iova_destroy() Marek Szyprowski
2026-11-27 11:58 ` [PATCH 1/4] media: venus: use dma_iova_size() to check the reserved IOVA size Marek Szyprowski
2026-11-27 12:41 ` Vikash Garodia
2026-11-27 14:29 ` Jason Gunthorpe
2026-11-27 11:58 ` [PATCH 2/4] media: iris: " Marek Szyprowski
2026-11-27 12:41 ` Vikash Garodia
2026-11-27 11:58 ` [PATCH 3/4] dma-mapping: add DMA_IOVA_FLAGS mask for the dma_iova_state size flags Marek Szyprowski
2026-11-27 14:37 ` Jason Gunthorpe
2026-11-27 11:58 ` [PATCH 4/4] iommu/dma: restore flush queue use in dma_iova_destroy() Marek Szyprowski
2026-11-27 14:37 ` Jason Gunthorpe
2026-11-27 14:45 ` Marek Szyprowski
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox