[PATCH v3 06/24] dma-buf: Allow single exporter drivers to avoid the match_mapping function
From: Alistair Popple
Date: Fri Nov 20 2026 - 00:57:24 EST
Introduce single_exporter_match into the dma_buf_ops so that single
exporter drivers can simply set it using a static initializer to use the
mapping type APIs. Provide a helper macro DMA_BUF_SIMPLE_SGT_EXP_MATCH()
that generates the initializer for simple drivers.
More complex exporters, such as those offering more than one entry or
that need to look at the importer when matching, implement the
match_mapping callback instead.
Co-developed-by: Jason Gunthorpe <jgg@xxxxxxxxxx>
Signed-off-by: Jason Gunthorpe <jgg@xxxxxxxxxx>
Signed-off-by: Alistair Popple <apopple@xxxxxxxxxx>
---
drivers/dma-buf/dma-buf.c | 14 ++++++++++++--
include/linux/dma-buf-mapping.h | 13 +++++++++++++
include/linux/dma-buf.h | 9 +++++++++
3 files changed, 34 insertions(+), 2 deletions(-)
diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c
index 76340d067f6c..6188cd11b004 100644
--- a/drivers/dma-buf/dma-buf.c
+++ b/drivers/dma-buf/dma-buf.c
@@ -718,10 +718,14 @@ struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info)
|| !exp_info->ops->release))
return ERR_PTR(-EINVAL);
- if (exp_info->ops->match_mapping) {
+ if (exp_info->ops->match_mapping ||
+ exp_info->ops->single_exporter_match) {
if (WARN_ON(exp_info->ops->map_dma_buf ||
exp_info->ops->unmap_dma_buf))
return ERR_PTR(-EINVAL);
+ if (WARN_ON(exp_info->ops->match_mapping &&
+ exp_info->ops->single_exporter_match))
+ return ERR_PTR(-EINVAL);
} else {
if (WARN_ON(!exp_info->ops->map_dma_buf ||
!exp_info->ops->unmap_dma_buf))
@@ -1094,7 +1098,13 @@ dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
if (ret)
goto err_attach;
} else {
- ret = dma_buf_sgt_compat_match_mapping(&match_args);
+ const struct dma_buf_mapping_match *exp_match =
+ dmabuf->ops->single_exporter_match;
+
+ if (exp_match)
+ ret = dma_buf_match_mapping(&match_args, exp_match, 1);
+ else
+ ret = dma_buf_sgt_compat_match_mapping(&match_args);
if (ret)
goto err_attach;
}
diff --git a/include/linux/dma-buf-mapping.h b/include/linux/dma-buf-mapping.h
index 8be03b4ba6cc..e442ed0ede71 100644
--- a/include/linux/dma-buf-mapping.h
+++ b/include/linux/dma-buf-mapping.h
@@ -244,4 +244,17 @@ dma_buf_sgt_mapping_dma_device(const struct dma_buf_sgt_mapping *mapping)
/* Match on behalf of an exporter that has no match_mapping() */
int dma_buf_sgt_compat_match_mapping(struct dma_buf_match_args *args);
+/*
+ * dma_buf_ops initializer helper for simple drivers that use a single
+ * SGT map/unmap operation.
+ */
+#define DMA_BUF_SIMPLE_SGT_EXP_MATCH(_map, _unmap) \
+ .single_exporter_match = &((const struct dma_buf_mapping_match){ \
+ .type = &dma_buf_mapping_sgt_type, \
+ .exp_ops = &((const struct dma_buf_mapping_sgt_exp_ops){ \
+ .map_dma_buf = _map, \
+ .unmap_dma_buf = _unmap, \
+ }).ops, \
+ })
+
#endif
diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h
index cfbb800aa12d..5c8f090a9b8a 100644
--- a/include/linux/dma-buf.h
+++ b/include/linux/dma-buf.h
@@ -322,6 +322,15 @@ struct dma_buf_ops {
* any time until the final location is resolved during map/pin.
*/
int (*match_mapping)(struct dma_buf_match_args *args);
+
+ /**
+ * @single_exporter_match:
+ *
+ * Should only be set by the DMA_BUF_SIMPLE_*_EXP_MATCH() helper macros.
+ * Exactly one of @match_mapping or @single_exporter_match must be
+ * provided.
+ */
+ const struct dma_buf_mapping_match *single_exporter_match;
};
/**
--
2.54.0