[PATCH RFC 08/16] dma-buf: Factor out the pin and fence wait around the exporter's map

From: Alistair Popple

Date: Tue Dec 01 2026 - 02:08:44 EST


dma_buf_map_sgt() pins the buffer for static importers of exporters with
a pin callback, calls the exporter, and waits for the buffer's fences on
behalf of static importers. A second way of mapping an attachment is
about to need the same steps around a different middle, so move them
into dma_buf_map_pin(), dma_buf_map_wait() and dma_buf_map_unpin(). No
functional change.

Signed-off-by: Alistair Popple <apopple@xxxxxxxxxx>
---
drivers/dma-buf/dma-buf.c | 69 +++++++++++++++++++++++++++++------------------
1 file changed, 43 insertions(+), 26 deletions(-)

diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c
index 70058509c397..799be8cad3ca 100644
--- a/drivers/dma-buf/dma-buf.c
+++ b/drivers/dma-buf/dma-buf.c
@@ -1068,6 +1068,40 @@ dma_buf_pin_on_map(struct dma_buf_attachment *attach)
!dma_buf_attachment_is_dynamic(attach);
}

+/* The pin around a static importer's mapping, for exporters that need one */
+static int dma_buf_map_pin(struct dma_buf_attachment *attach)
+{
+ int ret;
+
+ if (!dma_buf_pin_on_map(attach))
+ return 0;
+
+ ret = attach->dmabuf->ops->pin(attach);
+ /*
+ * Catch exporters making buffers inaccessible even when
+ * attachments preventing that exist.
+ */
+ WARN_ON_ONCE(ret == -EBUSY);
+ return ret;
+}
+
+static void dma_buf_map_unpin(struct dma_buf_attachment *attach)
+{
+ if (dma_buf_pin_on_map(attach))
+ attach->dmabuf->ops->unpin(attach);
+}
+
+/* Importers with static attachments don't wait for fences themselves */
+static long dma_buf_map_wait(struct dma_buf_attachment *attach)
+{
+ if (dma_buf_attachment_is_dynamic(attach))
+ return 0;
+
+ return dma_resv_wait_timeout(attach->dmabuf->resv,
+ DMA_RESV_USAGE_KERNEL, true,
+ MAX_SCHEDULE_TIMEOUT);
+}
+
/**
* DOC: locking convention
*
@@ -1326,16 +1360,9 @@ struct sg_table *dma_buf_map_sgt(struct dma_buf_attachment *attach,

dma_resv_assert_held(attach->dmabuf->resv);

- if (dma_buf_pin_on_map(attach)) {
- ret = attach->dmabuf->ops->pin(attach);
- /*
- * Catch exporters making buffers inaccessible even when
- * attachments preventing that exist.
- */
- WARN_ON_ONCE(ret == -EBUSY);
- if (ret)
- return ERR_PTR(ret);
- }
+ ret = dma_buf_map_pin(attach);
+ if (ret)
+ return ERR_PTR(ret);

sg_table = dma_buf_map_sg_table(attach, direction);
if (!sg_table)
@@ -1343,16 +1370,10 @@ struct sg_table *dma_buf_map_sgt(struct dma_buf_attachment *attach,
if (IS_ERR(sg_table))
goto error_unpin;

- /*
- * Importers with static attachments don't wait for fences.
- */
- if (!dma_buf_attachment_is_dynamic(attach)) {
- ret = dma_resv_wait_timeout(attach->dmabuf->resv,
- DMA_RESV_USAGE_KERNEL, true,
- MAX_SCHEDULE_TIMEOUT);
- if (ret < 0)
- goto error_unmap;
- }
+ ret = dma_buf_map_wait(attach);
+ if (ret < 0)
+ goto error_unmap;
+
ret = dma_buf_wrap_sg_table(&sg_table);
if (ret)
goto error_unmap;
@@ -1380,9 +1401,7 @@ struct sg_table *dma_buf_map_sgt(struct dma_buf_attachment *attach,
sg_table = ERR_PTR(ret);

error_unpin:
- if (dma_buf_pin_on_map(attach))
- attach->dmabuf->ops->unpin(attach);
-
+ dma_buf_map_unpin(attach);
return sg_table;
}
EXPORT_SYMBOL_NS_GPL(dma_buf_map_sgt, "DMA_BUF");
@@ -1438,9 +1457,7 @@ void dma_buf_unmap_sgt(struct dma_buf_attachment *attach,

dma_buf_unwrap_sg_table(&sg_table);
dma_buf_unmap_sg_table(attach, sg_table, direction);
-
- if (dma_buf_pin_on_map(attach))
- attach->dmabuf->ops->unpin(attach);
+ dma_buf_map_unpin(attach);
}
EXPORT_SYMBOL_NS_GPL(dma_buf_unmap_sgt, "DMA_BUF");


--
2.54.0