[PATCH 3/4] dma-mapping: add DMA_IOVA_FLAGS mask for the dma_iova_state size flags

From: Marek Szyprowski

Date: Fri Nov 27 2026 - 06:59:16 EST


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@xxxxxxxxxxxxxxx # v7.3+
Signed-off-by: Marek Szyprowski <m.szyprowski@xxxxxxxxxxx>
---
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