This series fixes an issue with DMABUF support in the IIO subsystem where the wrong DMA device could be used for buffer mapping operations. This becomes critical on systems like Xilinx/AMD ZynqMP Ultrascale where memory can be mapped above the 32-bit address range.
Problem: -------- The current IIO DMABUF implementation assumes it can use the parent device of the IIO device for DMA operations. However, this device may not have the appropriate DMA mask configuration for accessing high memory addresses. On systems where memory is mapped above 32-bits, this leads to the use of bounce buffers through swiotlb, significantly impacting performance.
Solution: --------- This series introduces a new .get_dma_dev() callback in the buffer access functions that allows buffer implementations to specify the correct DMA device that should be used for DMABUF operations. The DMA buffer infrastructure implements this callback to return the device that actually owns the DMA channel, ensuring proper memory mapping without bounce buffers.
Changes: -------- 1. Add .get_dma_dev() callback to iio_buffer_access_funcs and update core DMABUF code to use it when available 2. Implement the callback in the DMA buffer infrastructure 3. Wire up the callback in the dmaengine buffer implementation
This ensures that DMABUF operations use the device with the correct DMA configuration, eliminating unnecessary bounce buffer usage and improving performance on high-memory systems.
(AI generated cover. I would not be this formal but I guess is not that bad :))
--- Changes in v2: - Dropped Fixes tags on the first two patches and Cc stable them instead (as prerequisites for the third patch). - Link to v1: https://lore.kernel.org/r/20251002-fix-iio-dmabuf-get-dma-device-v1-0-c1c994...
--- Nuno Sá (3): iio: buffer: support getting dma channel from the buffer iio: buffer-dma: support getting the DMA channel iio: buffer-dmaengine: enable .get_dma_dev()
drivers/iio/buffer/industrialio-buffer-dma.c | 6 +++++ drivers/iio/buffer/industrialio-buffer-dmaengine.c | 2 ++ drivers/iio/industrialio-buffer.c | 28 +++++++++++++++++----- include/linux/iio/buffer-dma.h | 1 + include/linux/iio/buffer_impl.h | 2 ++ 5 files changed, 33 insertions(+), 6 deletions(-) --- base-commit: b9700f87939f0f477e5c00db817f54ab8a97702b change-id: 20250930-fix-iio-dmabuf-get-dma-device-339ac70543db --
Thanks! - Nuno Sá
From: Nuno Sá nuno.sa@analog.com
Add a new buffer accessor .get_dma_dev() in order to get the struct device responsible for actually providing the dma channel. We cannot assume that we can use the parent of the IIO device for mapping the DMA buffer. This becomes important on systems (like the Xilinx/AMD zynqMP Ultrascale) where memory (or part of it) is mapped above the 32 bit range. On such systems and given that a device by default has a dma mask of 32 bits we would then need to rely on bounce buffers (to swiotlb) for mapping memory above the dma mask limit.
Cc: stable@vger.kernel.org Signed-off-by: Nuno Sá nuno.sa@analog.com --- drivers/iio/industrialio-buffer.c | 28 ++++++++++++++++++++++------ include/linux/iio/buffer_impl.h | 2 ++ 2 files changed, 24 insertions(+), 6 deletions(-)
diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c index f1448ae1b843fc577599fc1b9cf6d859bba226f1..279c7d716bf5d467d40b5c290789fcbd1f949660 100644 --- a/drivers/iio/industrialio-buffer.c +++ b/drivers/iio/industrialio-buffer.c @@ -1627,15 +1627,20 @@ static struct dma_buf_attachment * iio_buffer_find_attachment(struct iio_dev_buffer_pair *ib, struct dma_buf *dmabuf, bool nonblock) { - struct device *dev = ib->indio_dev->dev.parent; + struct device *dma_dev; struct iio_buffer *buffer = ib->buffer; struct dma_buf_attachment *attach = NULL; struct iio_dmabuf_priv *priv;
+ if (buffer->access->get_dma_dev) + dma_dev = buffer->access->get_dma_dev(buffer); + else + dma_dev = ib->indio_dev->dev.parent; + guard(mutex)(&buffer->dmabufs_mutex);
list_for_each_entry(priv, &buffer->dmabufs, entry) { - if (priv->attach->dev == dev + if (priv->attach->dev == dma_dev && priv->attach->dmabuf == dmabuf) { attach = priv->attach; break; @@ -1655,6 +1660,7 @@ static int iio_buffer_attach_dmabuf(struct iio_dev_buffer_pair *ib, struct iio_buffer *buffer = ib->buffer; struct dma_buf_attachment *attach; struct iio_dmabuf_priv *priv, *each; + struct device *dma_dev; struct dma_buf *dmabuf; int err, fd;
@@ -1679,7 +1685,12 @@ static int iio_buffer_attach_dmabuf(struct iio_dev_buffer_pair *ib, goto err_free_priv; }
- attach = dma_buf_attach(dmabuf, indio_dev->dev.parent); + if (buffer->access->get_dma_dev) + dma_dev = buffer->access->get_dma_dev(buffer); + else + dma_dev = indio_dev->dev.parent; + + attach = dma_buf_attach(dmabuf, dma_dev); if (IS_ERR(attach)) { err = PTR_ERR(attach); goto err_dmabuf_put; @@ -1719,7 +1730,7 @@ static int iio_buffer_attach_dmabuf(struct iio_dev_buffer_pair *ib, * combo. If we do, refuse to attach. */ list_for_each_entry(each, &buffer->dmabufs, entry) { - if (each->attach->dev == indio_dev->dev.parent + if (each->attach->dev == dma_dev && each->attach->dmabuf == dmabuf) { /* * We unlocked the reservation object, so going through @@ -1759,6 +1770,7 @@ static int iio_buffer_detach_dmabuf(struct iio_dev_buffer_pair *ib, struct iio_buffer *buffer = ib->buffer; struct iio_dev *indio_dev = ib->indio_dev; struct iio_dmabuf_priv *priv; + struct device *dma_dev; struct dma_buf *dmabuf; int dmabuf_fd, ret = -EPERM;
@@ -1769,11 +1781,15 @@ static int iio_buffer_detach_dmabuf(struct iio_dev_buffer_pair *ib, if (IS_ERR(dmabuf)) return PTR_ERR(dmabuf);
+ if (buffer->access->get_dma_dev) + dma_dev = buffer->access->get_dma_dev(buffer); + else + dma_dev = indio_dev->dev.parent; + guard(mutex)(&buffer->dmabufs_mutex);
list_for_each_entry(priv, &buffer->dmabufs, entry) { - if (priv->attach->dev == indio_dev->dev.parent - && priv->attach->dmabuf == dmabuf) { + if (priv->attach->dev == dma_dev && priv->attach->dmabuf == dmabuf) { list_del(&priv->entry);
/* Unref the reference from iio_buffer_attach_dmabuf() */ diff --git a/include/linux/iio/buffer_impl.h b/include/linux/iio/buffer_impl.h index 0daff9ff20ce49de67fa0f2ac6191882de2f4a67..c0b0e0992a85b2813a126c1a61f13f1ed0b498dd 100644 --- a/include/linux/iio/buffer_impl.h +++ b/include/linux/iio/buffer_impl.h @@ -51,6 +51,7 @@ struct sg_table; * @enqueue_dmabuf: called from userspace via ioctl to queue this DMABUF * object to this buffer. Requires a valid DMABUF fd, that * was previouly attached to this buffer. + * @get_dma_dev: called to get the DMA channel associated with this buffer. * @lock_queue: called when the core needs to lock the buffer queue; * it is used when enqueueing DMABUF objects. * @unlock_queue: used to unlock a previously locked buffer queue @@ -91,6 +92,7 @@ struct iio_buffer_access_funcs { struct iio_dma_buffer_block *block, struct dma_fence *fence, struct sg_table *sgt, size_t size, bool cyclic); + struct device * (*get_dma_dev)(struct iio_buffer *buffer); void (*lock_queue)(struct iio_buffer *buffer); void (*unlock_queue)(struct iio_buffer *buffer);
From: Nuno Sá nuno.sa@analog.com
Implement the .get_dma_dev() callback for DMA buffers by returning the device that owns the DMA channel. This allows the core DMABUF infrastructure to properly map DMA buffers using the correct device, avoiding the need for bounce buffers on systems where memory is mapped above the 32-bit range.
The function returns the DMA queue's device, which is the actual device responsible for DMA operations in buffer-dma implementations.
Cc: stable@vger.kernel.org Signed-off-by: Nuno Sá nuno.sa@analog.com --- drivers/iio/buffer/industrialio-buffer-dma.c | 6 ++++++ include/linux/iio/buffer-dma.h | 1 + 2 files changed, 7 insertions(+)
diff --git a/drivers/iio/buffer/industrialio-buffer-dma.c b/drivers/iio/buffer/industrialio-buffer-dma.c index ee294a775e8aa050aca85e422de4c267adee1a33..7a7a9d37339bc1c2ffec58687a42dde16b1412bb 100644 --- a/drivers/iio/buffer/industrialio-buffer-dma.c +++ b/drivers/iio/buffer/industrialio-buffer-dma.c @@ -786,6 +786,12 @@ int iio_dma_buffer_enqueue_dmabuf(struct iio_buffer *buffer, } EXPORT_SYMBOL_NS_GPL(iio_dma_buffer_enqueue_dmabuf, "IIO_DMA_BUFFER");
+struct device *iio_dma_buffer_get_dma_dev(struct iio_buffer *buffer) +{ + return iio_buffer_to_queue(buffer)->dev; +} +EXPORT_SYMBOL_NS_GPL(iio_dma_buffer_get_dma_dev, "IIO_DMA_BUFFER"); + void iio_dma_buffer_lock_queue(struct iio_buffer *buffer) { struct iio_dma_buffer_queue *queue = iio_buffer_to_queue(buffer); diff --git a/include/linux/iio/buffer-dma.h b/include/linux/iio/buffer-dma.h index 5eb66a3990021afb1821297e540656a65916daa7..4f33e6a39797d3ecfddc69c11d6d3985b9212920 100644 --- a/include/linux/iio/buffer-dma.h +++ b/include/linux/iio/buffer-dma.h @@ -174,5 +174,6 @@ int iio_dma_buffer_enqueue_dmabuf(struct iio_buffer *buffer, size_t size, bool cyclic); void iio_dma_buffer_lock_queue(struct iio_buffer *buffer); void iio_dma_buffer_unlock_queue(struct iio_buffer *buffer); +struct device *iio_dma_buffer_get_dma_dev(struct iio_buffer *buffer);
#endif
On 10/6/25 11:06 AM, Nuno Sá via B4 Relay wrote:
This series fixes an issue with DMABUF support in the IIO subsystem where the wrong DMA device could be used for buffer mapping operations. This becomes critical on systems like Xilinx/AMD ZynqMP Ultrascale where memory can be mapped above the 32-bit address range.
Problem:
The current IIO DMABUF implementation assumes it can use the parent device of the IIO device for DMA operations. However, this device may not have the appropriate DMA mask configuration for accessing high memory addresses. On systems where memory is mapped above 32-bits, this leads to the use of bounce buffers through swiotlb, significantly impacting performance.
Solution:
This series introduces a new .get_dma_dev() callback in the buffer access functions that allows buffer implementations to specify the correct DMA device that should be used for DMABUF operations. The DMA buffer infrastructure implements this callback to return the device that actually owns the DMA channel, ensuring proper memory mapping without bounce buffers.
Changes:
- Add .get_dma_dev() callback to iio_buffer_access_funcs and update core DMABUF code to use it when available
- Implement the callback in the DMA buffer infrastructure
- Wire up the callback in the dmaengine buffer implementation
This ensures that DMABUF operations use the device with the correct DMA configuration, eliminating unnecessary bounce buffer usage and improving performance on high-memory systems.
(AI generated cover. I would not be this formal but I guess is not that bad :))
Changes in v2:
- Dropped Fixes tags on the first two patches and Cc stable them instead (as prerequisites for the third patch).
- Link to v1: https://lore.kernel.org/r/20251002-fix-iio-dmabuf-get-dma-device-v1-0-c1c994...
Did you not care for my other suggestions in v1?
On Mon, 2025-10-06 at 11:18 -0500, David Lechner wrote:
On 10/6/25 11:06 AM, Nuno Sá via B4 Relay wrote:
This series fixes an issue with DMABUF support in the IIO subsystem where the wrong DMA device could be used for buffer mapping operations. This becomes critical on systems like Xilinx/AMD ZynqMP Ultrascale where memory can be mapped above the 32-bit address range.
Problem:
The current IIO DMABUF implementation assumes it can use the parent device of the IIO device for DMA operations. However, this device may not have the appropriate DMA mask configuration for accessing high memory addresses. On systems where memory is mapped above 32-bits, this leads to the use of bounce buffers through swiotlb, significantly impacting performance.
Solution:
This series introduces a new .get_dma_dev() callback in the buffer access functions that allows buffer implementations to specify the correct DMA device that should be used for DMABUF operations. The DMA buffer infrastructure implements this callback to return the device that actually owns the DMA channel, ensuring proper memory mapping without bounce buffers.
Changes:
- Add .get_dma_dev() callback to iio_buffer_access_funcs and update core
DMABUF code to use it when available 2. Implement the callback in the DMA buffer infrastructure 3. Wire up the callback in the dmaengine buffer implementation
This ensures that DMABUF operations use the device with the correct DMA configuration, eliminating unnecessary bounce buffer usage and improving performance on high-memory systems.
(AI generated cover. I would not be this formal but I guess is not that bad :))
Changes in v2:
- Dropped Fixes tags on the first two patches and Cc stable them instead
(as prerequisites for the third patch).
- Link to v1:
https://lore.kernel.org/r/20251002-fix-iio-dmabuf-get-dma-device-v1-0-c1c994...
Did you not care for my other suggestions in v1?
Completely missed them, sorry! I kind of stop reading in the stable stuff. I'm ok with the helper function. For the clarification I feel it's redundant. The field is called .get_dma_dev() and the description "called to get the DMA channel associated with this buffer" already implies is for DMA buffer. Same as ops like .enqueue_dmabuf().
- Nuno Sá
On 10/6/25 11:25 AM, Nuno Sá wrote:
On Mon, 2025-10-06 at 11:18 -0500, David Lechner wrote:
On 10/6/25 11:06 AM, Nuno Sá via B4 Relay wrote:
This series fixes an issue with DMABUF support in the IIO subsystem where the wrong DMA device could be used for buffer mapping operations. This becomes critical on systems like Xilinx/AMD ZynqMP Ultrascale where memory can be mapped above the 32-bit address range.
Problem:
The current IIO DMABUF implementation assumes it can use the parent device of the IIO device for DMA operations. However, this device may not have the appropriate DMA mask configuration for accessing high memory addresses. On systems where memory is mapped above 32-bits, this leads to the use of bounce buffers through swiotlb, significantly impacting performance.
Solution:
This series introduces a new .get_dma_dev() callback in the buffer access functions that allows buffer implementations to specify the correct DMA device that should be used for DMABUF operations. The DMA buffer infrastructure implements this callback to return the device that actually owns the DMA channel, ensuring proper memory mapping without bounce buffers.
Changes:
- Add .get_dma_dev() callback to iio_buffer_access_funcs and update core
DMABUF code to use it when available 2. Implement the callback in the DMA buffer infrastructure 3. Wire up the callback in the dmaengine buffer implementation
This ensures that DMABUF operations use the device with the correct DMA configuration, eliminating unnecessary bounce buffer usage and improving performance on high-memory systems.
(AI generated cover. I would not be this formal but I guess is not that bad :))
Changes in v2:
- Dropped Fixes tags on the first two patches and Cc stable them instead
(as prerequisites for the third patch).
- Link to v1:
https://lore.kernel.org/r/20251002-fix-iio-dmabuf-get-dma-device-v1-0-c1c994...
Did you not care for my other suggestions in v1?
Completely missed them, sorry! I kind of stop reading in the stable stuff. I'm ok with the helper function. For the clarification I feel it's redundant. The
I was thinking extra clarification could be helpful for someone new to the IIO subsystem. But it would be quite rare to add a new buffer implementation anyway. So probably not too many people would actually ever read it. :-)
field is called .get_dma_dev() and the description "called to get the DMA channel associated with this buffer" already implies is for DMA buffer. Same as ops like .enqueue_dmabuf().
- Nuno Sá
I don't feel too strongly about either change, so either way,
Reviewed-by: David Lechner dlechner@baylibre.com
On Mon, 2025-10-06 at 11:44 -0500, David Lechner wrote:
On 10/6/25 11:25 AM, Nuno Sá wrote:
On Mon, 2025-10-06 at 11:18 -0500, David Lechner wrote:
On 10/6/25 11:06 AM, Nuno Sá via B4 Relay wrote:
This series fixes an issue with DMABUF support in the IIO subsystem where the wrong DMA device could be used for buffer mapping operations. This becomes critical on systems like Xilinx/AMD ZynqMP Ultrascale where memory can be mapped above the 32-bit address range.
Problem:
The current IIO DMABUF implementation assumes it can use the parent device of the IIO device for DMA operations. However, this device may not have the appropriate DMA mask configuration for accessing high memory addresses. On systems where memory is mapped above 32-bits, this leads to the use of bounce buffers through swiotlb, significantly impacting performance.
Solution:
This series introduces a new .get_dma_dev() callback in the buffer access functions that allows buffer implementations to specify the correct DMA device that should be used for DMABUF operations. The DMA buffer infrastructure implements this callback to return the device that actually owns the DMA channel, ensuring proper memory mapping without bounce buffers.
Changes:
- Add .get_dma_dev() callback to iio_buffer_access_funcs and update
core DMABUF code to use it when available 2. Implement the callback in the DMA buffer infrastructure 3. Wire up the callback in the dmaengine buffer implementation
This ensures that DMABUF operations use the device with the correct DMA configuration, eliminating unnecessary bounce buffer usage and improving performance on high-memory systems.
(AI generated cover. I would not be this formal but I guess is not that bad :))
Changes in v2:
- Dropped Fixes tags on the first two patches and Cc stable them instead
(as prerequisites for the third patch).
- Link to v1:
https://lore.kernel.org/r/20251002-fix-iio-dmabuf-get-dma-device-v1-0-c1c994...
Did you not care for my other suggestions in v1?
Completely missed them, sorry! I kind of stop reading in the stable stuff. I'm ok with the helper function. For the clarification I feel it's redundant. The
I was thinking extra clarification could be helpful for someone new to the IIO subsystem. But it would be quite rare to add a new buffer implementation anyway. So probably not too many people would actually ever read it. :-)
I mean, it does not harm. If you want to add it, I'll hack it (as I feel it should be a separate patch also covering the other .ops related to DMA buffers).
field is called .get_dma_dev() and the description "called to get the DMA channel associated with this buffer" already implies is for DMA buffer. Same as ops like .enqueue_dmabuf().
- Nuno Sá
I don't feel too strongly about either change, so either way,
Reviewed-by: David Lechner dlechner@baylibre.com
Thx!
- Nuno Sá
On Tue, 07 Oct 2025 09:25:19 +0100 Nuno Sá noname.nuno@gmail.com wrote:
On Mon, 2025-10-06 at 11:44 -0500, David Lechner wrote:
On 10/6/25 11:25 AM, Nuno Sá wrote:
On Mon, 2025-10-06 at 11:18 -0500, David Lechner wrote:
On 10/6/25 11:06 AM, Nuno Sá via B4 Relay wrote:
This series fixes an issue with DMABUF support in the IIO subsystem where the wrong DMA device could be used for buffer mapping operations. This becomes critical on systems like Xilinx/AMD ZynqMP Ultrascale where memory can be mapped above the 32-bit address range.
Problem:
The current IIO DMABUF implementation assumes it can use the parent device of the IIO device for DMA operations. However, this device may not have the appropriate DMA mask configuration for accessing high memory addresses. On systems where memory is mapped above 32-bits, this leads to the use of bounce buffers through swiotlb, significantly impacting performance.
Solution:
This series introduces a new .get_dma_dev() callback in the buffer access functions that allows buffer implementations to specify the correct DMA device that should be used for DMABUF operations. The DMA buffer infrastructure implements this callback to return the device that actually owns the DMA channel, ensuring proper memory mapping without bounce buffers.
Changes:
- Add .get_dma_dev() callback to iio_buffer_access_funcs and update
core DMABUF code to use it when available 2. Implement the callback in the DMA buffer infrastructure 3. Wire up the callback in the dmaengine buffer implementation
This ensures that DMABUF operations use the device with the correct DMA configuration, eliminating unnecessary bounce buffer usage and improving performance on high-memory systems.
(AI generated cover. I would not be this formal but I guess is not that bad :))
Changes in v2:
- Dropped Fixes tags on the first two patches and Cc stable them instead
(as prerequisites for the third patch).
- Link to v1:
https://lore.kernel.org/r/20251002-fix-iio-dmabuf-get-dma-device-v1-0-c1c994...
Did you not care for my other suggestions in v1?
Completely missed them, sorry! I kind of stop reading in the stable stuff.
On that 'stable' stuff I'm very unclear on the logic for +CC a dependency to stable@vger.kernel.org
The +CC is IIUC the thing that gets a patch queued rather than the fixes tag and if you don't provide a fixes tag assumption is that it goes back as far as it can be applied. That concerns me as they could therefore get pushed back further than the thing dependent on them. Ah well. I guess they do some magic stuff on series though as the stable-rules.rst calls out the case you have here.
So applied as is but I marked the final patch for stable. Would have have been a weird situation if I decided not to do that for some reason.
Applied to the fixes-togreg branch of iio.git. That has a weird mid merge window base at the moment so I'll rebase on rc1 once available and push out at that point.
Jonathan
I'm ok with the helper function. For the clarification I feel it's redundant. The
I was thinking extra clarification could be helpful for someone new to the IIO subsystem. But it would be quite rare to add a new buffer implementation anyway. So probably not too many people would actually ever read it. :-)
I mean, it does not harm. If you want to add it, I'll hack it (as I feel it should be a separate patch also covering the other .ops related to DMA buffers).
field is called .get_dma_dev() and the description "called to get the DMA channel associated with this buffer" already implies is for DMA buffer. Same as ops like .enqueue_dmabuf().
- Nuno Sá
I don't feel too strongly about either change, so either way,
Reviewed-by: David Lechner dlechner@baylibre.com
Thx!
- Nuno Sá
linux-stable-mirror@lists.linaro.org