[PATCH RFC 03/26] dma-buf: Add dma_buf_mapping_attach()

Jason Gunthorpe
2026-02-18T00:12:05Z

Introduce a new attach function that accepts the list of importer
supported mapping types from the caller. Turn dma_buf_dynamic_attach() and
dma_buf_attach() into simple wrappers calling this new function with a
compatibility mapping type list that only includes sgt.

dma_buf_mapping_attach() checks if the exporter is mapping aware and calls
its ops->match_mapping() function to pick up the exporter match list and
call dma_buf_match_mapping().

If unaware it will use dma_buf_sgt_exp_compat_match as a compatibility
matchlist that uses the unaware exporter's dma_buf_ops callbacks.

The resulting match is stored in attach->map_type.

In effect attach->map_type is always available and always makes sense
regardless of what combination of aware/unaware importer/exporter is used.

For compatibility with unaware drivers copy the sgt matching data into the
attach->dev and peer2peer.

If the exporter sets exporter_requires_p2p then only the following are
allowed:
- dma_buf_dynamic_attach() with importer_ops->allow_peer2peer = true
- dma_buf_mapping_attach() with a
DMA_BUF_IMAPPING_SGT(xx, exporter_requires_p2p=true)

Other combinations are blocked.

Exporters that want to behave differently based on the importer's
capability can declare exporter_requires_p2p=false and check
attach->map_type.sgt_data.importer_accepts_p2p. Or they can declare two
SGT exporters with different map/unmap functions.

Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
drivers/dma-buf/dma-buf.c | 91 +++++++++++++++++++++++++++++++--------
include/linux/dma-buf.h | 14 ++++++
2 files changed, 87 insertions(+), 18 deletions(-)

diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c
index edaa9e4ee4aed0..6e89fcfdad3015 100644
--- a/drivers/dma-buf/dma-buf.c
+++ b/drivers/dma-buf/dma-buf.c
@@ -14,6 +14,7 @@
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/dma-buf.h>
+#include <linux/dma-buf-mapping.h>
#include <linux/dma-fence.h>
#include <linux/dma-fence-unwrap.h>
#include <linux/anon_inodes.h>
@@ -689,11 +690,19 @@ struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info)
int ret;

if (WARN_ON(!exp_info->priv || !exp_info->ops
- || !exp_info->ops->map_dma_buf
- || !exp_info->ops->unmap_dma_buf
|| !exp_info->ops->release))
return ERR_PTR(-EINVAL);

+ if (exp_info->ops->match_mapping) {
+ if (WARN_ON(exp_info->ops->map_dma_buf ||
+ exp_info->ops->unmap_dma_buf))
+ return ERR_PTR(-EINVAL);
+ } else {
+ 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->pin != !exp_info->ops->unpin))
return ERR_PTR(-EINVAL);

@@ -916,9 +925,10 @@ dma_buf_pin_on_map(struct dma_buf_attachment *attach)
*/

/**
- * dma_buf_dynamic_attach - Add the device to dma_buf's attachments list
+ * dma_buf_mapping_attach - Add the device to dma_buf's attachments list
* @dmabuf: [in] buffer to attach device to.
- * @dev: [in] device to be attached.
+ * @importer_matches: [in] mapping types supported by the importer
+ * @match_len: [in] length of @importer_matches
* @importer_ops: [in] importer operations for the attachment
* @importer_priv: [in] importer private pointer for the attachment
*
@@ -934,31 +944,46 @@ dma_buf_pin_on_map(struct dma_buf_attachment *attach)
* error code wrapped into a pointer on failure.
*
* Note that this can fail if the backing storage of @dmabuf is in a place not
- * accessible to @dev, and cannot be moved to a more suitable place. This is
- * indicated with the error code -EBUSY.
+ * accessible to any importers, and cannot be moved to a more suitable place.
+ * This is indicated with the error code -EBUSY.
*/
-struct dma_buf_attachment *
-dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
- const struct dma_buf_attach_ops *importer_ops,
- void *importer_priv)
+struct dma_buf_attachment *dma_buf_mapping_attach(
+ struct dma_buf *dmabuf, struct dma_buf_mapping_match *importer_matches,
+ size_t match_len, const struct dma_buf_attach_ops *importer_ops,
+ void *importer_priv)
{
+ struct dma_buf_match_args match_args = {
+ .dmabuf = dmabuf,
+ .imp_matches = importer_matches,
+ .imp_len = match_len,
+ };
struct dma_buf_attachment *attach;
int ret;

- if (WARN_ON(!dmabuf || !dev))
+ if (WARN_ON(!dmabuf))
return ERR_PTR(-EINVAL);

if (WARN_ON(importer_ops && !importer_ops->move_notify))
return ERR_PTR(-EINVAL);

+
attach = kzalloc(sizeof(*attach), GFP_KERNEL);
if (!attach)
return ERR_PTR(-ENOMEM);

- attach->dev = dev;
+ match_args.attach = attach;
+ if (dmabuf->ops->match_mapping) {
+ ret = dmabuf->ops->match_mapping(&match_args);
+ if (ret)
+ goto err_attach;
+ } else {
+ ret = dma_buf_match_mapping(&match_args,
+ &dma_buf_sgt_exp_compat_match, 1);
+ if (ret)
+ goto err_attach;
+ }
+
attach->dmabuf = dmabuf;
- if (importer_ops)
- attach->peer2peer = importer_ops->allow_peer2peer;
attach->importer_ops = importer_ops;
attach->importer_priv = importer_priv;

@@ -977,23 +1002,53 @@ dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
kfree(attach);
return ERR_PTR(ret);
}
-EXPORT_SYMBOL_NS_GPL(dma_buf_dynamic_attach, "DMA_BUF");
+EXPORT_SYMBOL_NS_GPL(dma_buf_mapping_attach, "DMA_BUF");

/**
- * dma_buf_attach - Wrapper for dma_buf_dynamic_attach
+ * dma_buf_attach - Wrapper for dma_buf_mapping_attach
* @dmabuf: [in] buffer to attach device to.
* @dev: [in] device to be attached.
*
- * Wrapper to call dma_buf_dynamic_attach() for drivers which still use a static
+ * Wrapper to call dma_buf_mapping_attach() for drivers which still use a static
* mapping.
*/
struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
struct device *dev)
{
- return dma_buf_dynamic_attach(dmabuf, dev, NULL, NULL);
+ struct dma_buf_mapping_match sgt_match[] = {
+ DMA_BUF_IMAPPING_SGT(dev, DMA_SGT_NO_P2P),
+ };
+
+ return dma_buf_mapping_attach(dmabuf, sgt_match, ARRAY_SIZE(sgt_match),
+ NULL, NULL);
}
EXPORT_SYMBOL_NS_GPL(dma_buf_attach, "DMA_BUF");

+/**
+ * dma_buf_dynamic_attach - Add the device to dma_buf's attachments list
+ * @dmabuf: [in] buffer to attach device to.
+ * @dev: [in] device to be attached.
+ * @importer_ops: [in] importer operations for the attachment
+ * @importer_priv: [in] importer private pointer for the attachment
+ *
+ * Wrapper to call dma_buf_mapping_attach() for drivers which only support SGT.
+ */
+struct dma_buf_attachment *
+dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
+ const struct dma_buf_attach_ops *importer_ops,
+ void *importer_priv)
+{
+ struct dma_buf_mapping_match sgt_match[] = {
+ DMA_BUF_IMAPPING_SGT(dev, importer_ops->allow_peer2peer ?
+ DMA_SGT_IMPORTER_ACCEPTS_P2P :
+ DMA_SGT_NO_P2P),
+ };
+
+ return dma_buf_mapping_attach(dmabuf, sgt_match, ARRAY_SIZE(sgt_match),
+ importer_ops, importer_priv);
+}
+EXPORT_SYMBOL_NS_GPL(dma_buf_dynamic_attach, "DMA_BUF");
+
/**
* dma_buf_detach - Remove the given attachment from dmabuf's attachments list
* @dmabuf: [in] buffer to detach from.
diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h
index 3bcd1d6d150188..14d556bb022862 100644
--- a/include/linux/dma-buf.h
+++ b/include/linux/dma-buf.h
@@ -29,6 +29,7 @@ struct dma_buf;
struct dma_buf_attachment;
struct dma_buf_mapping_type;
struct dma_buf_mapping_exp_ops;
+struct dma_buf_match_args;

/*
@@ -308,6 +309,14 @@ struct dma_buf_ops {

int (*vmap)(struct dma_buf *dmabuf, struct iosys_map *map);
void (*vunmap)(struct dma_buf *dmabuf, struct iosys_map *map);
+
+ /**
+ * @match_mapping:
+ *
+ * Called during attach. Allows the exporter to build its own exporter
+ * struct dma_buf_mapping_match[] and call dma_buf_match_mapping().
+ */
+ int (*match_mapping)(struct dma_buf_match_args *args);
};

/**
@@ -619,6 +628,11 @@ struct dma_buf_attachment *
dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
const struct dma_buf_attach_ops *importer_ops,
void *importer_priv);
+struct dma_buf_attachment *dma_buf_mapping_attach(
+ struct dma_buf *dmabuf, struct dma_buf_mapping_match *importer_matches,
+ size_t match_len, const struct dma_buf_attach_ops *importer_ops,
+ void *importer_priv);
+
void dma_buf_detach(struct dma_buf *dmabuf,
struct dma_buf_attachment *attach);
int dma_buf_pin(struct dma_buf_attachment *attach);
--
2.43.0