[PATCH v3 04/24] dma-buf: Negotiate the mapping type in dma_buf_dynamic_attach()

From: Alistair Popple

Date: Fri Nov 20 2026 - 00:57:50 EST


Mapping types need to be negotiated based on what importer and
exporter can accept.

Add an optional dma_buf_attach_ops->supported_mapping_types table that
lists types supported by an importer in most preferred order. The entries
carry no device so the table can be static and shared by all of the
importer's attachments.

Instead the device passed to dma_buf_dynamic_attach() is passed to the
mapping types through the match callback arguments. Importers that do
not provide a table continue to support the SGT type.

dma_buf_dynamic_attach() checks if the exporter is mapping aware and
calls its ops->match_mapping() function to get the exporter match list
and call dma_buf_match_mapping(), otherwise it falls back to using the
SGT compatibility type. The resulting set of possible matches is then
stored in attach->matches the exporter's priority order.

The actual match which will be used is deferred until map time as future
attachments could change what's possible. Exporters that need to behave
differently based on the importer's capability can declare two exporter
entries with different map/unmap functions. This will be used in a future
series to negotiate PCIe P2P mappings.

Co-developed-by: Jason Gunthorpe <jgg@xxxxxxxxxx>
Signed-off-by: Jason Gunthorpe <jgg@xxxxxxxxxx>
Signed-off-by: Alistair Popple <apopple@xxxxxxxxxx>

---

Changes for v3:

- Move P2P negotiation to a later series.
---
drivers/dma-buf/dma-buf.c | 54 ++++++++++++++++++++++++++++++++++++++++++++---
include/linux/dma-buf.h | 40 +++++++++++++++++++++++++++++++++++
2 files changed, 91 insertions(+), 3 deletions(-)

diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c
index 0dd8d0999a92..4aee479c4a7e 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>
@@ -714,11 +715,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);

@@ -1027,12 +1036,24 @@ dma_buf_pin_on_map(struct dma_buf_attachment *attach)
* 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.
+ *
+ * The mapping types the importer supports come from
+ * &dma_buf_attach_ops.supported_mapping_types. Importers without it only
+ * support the SGT type. This records the types supported by both importer and
+ * exporter in &dma_buf_attachment.matches. Later attachments do not change
+ * the set but may affect what type the exporter selects when the attachment
+ * is mapped or pinned.
*/
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 default_matches[1];
+ struct dma_buf_match_args match_args = {
+ .dmabuf = dmabuf,
+ .dev = dev,
+ };
struct dma_buf_attachment *attach;
int ret;

@@ -1043,13 +1064,40 @@ dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
if (!attach)
return ERR_PTR(-ENOMEM);

- attach->dev = dev;
attach->dmabuf = dmabuf;
if (importer_ops)
attach->peer2peer = importer_ops->allow_peer2peer;
attach->importer_ops = importer_ops;
attach->importer_priv = importer_priv;

+ /*
+ * The exporter's match_mapping() may look at the importer side of the
+ * attachment, so initialise it before the match.
+ */
+ match_args.attach = attach;
+ if (importer_ops && importer_ops->supported_mapping_types) {
+ if (WARN_ON(!importer_ops->nr_supported_mapping_types)) {
+ ret = -EINVAL;
+ goto err_attach;
+ }
+ match_args.imp_matches = importer_ops->supported_mapping_types;
+ match_args.imp_len = importer_ops->nr_supported_mapping_types;
+ } else {
+ default_matches[0] = DMA_BUF_IMAPPING_SGT();
+ match_args.imp_matches = default_matches;
+ match_args.imp_len = ARRAY_SIZE(default_matches);
+ }
+
+ if (dmabuf->ops->match_mapping) {
+ ret = dmabuf->ops->match_mapping(&match_args);
+ if (ret)
+ goto err_attach;
+ } else {
+ ret = dma_buf_sgt_compat_match_mapping(&match_args);
+ if (ret)
+ goto err_attach;
+ }
+
if (dmabuf->ops->attach) {
ret = dmabuf->ops->attach(dmabuf, attach);
if (ret)
diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h
index 6b423e22174a..cfbb800aa12d 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;

/*
* Upper bound on the number of mapping types an attachment can record as
@@ -303,6 +304,24 @@ 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 without the dma_resv lock held. Allows the exporter to
+ * build its own struct dma_buf_mapping_match[] and call dma_buf_match_mapping()
+ * to record mutually acceptable types in &dma_buf_attachment.matches in exporter
+ * order of preference.
+ *
+ * The match set is fixed for the lifetime of the attachment and so every
+ * input determining match compatibility must hold for the lifetime of the
+ * attachment.
+ *
+ * It must not depend on the set of attachments or on current buffer
+ * placement as matches are static and buffer placement can change at
+ * any time until the final location is resolved during map/pin.
+ */
+ int (*match_mapping)(struct dma_buf_match_args *args);
};

/**
@@ -486,6 +505,27 @@ struct dma_buf_attach_ops {
* required behavior.
*/
void (*invalidate_mappings)(struct dma_buf_attachment *attach);
+
+ /**
+ * @supported_mapping_types: [optional] mapping types the importer
+ * can use
+ *
+ * Array of @nr_supported_mapping_types DMA_BUF_IMAPPING_*() entries
+ * the importer can use in priority order. Entries are static so can be
+ * shared by all of the importer's attachments. Any required dynamic
+ * information, such as the importing device, is passed to each type's
+ * callbacks by dma_buf_dynamic_attach() for recording in the
+ * negotiated entry.
+ *
+ * If not provided the importer only supports the SGT mapping type.
+ */
+ const struct dma_buf_mapping_match *supported_mapping_types;
+
+ /**
+ * @nr_supported_mapping_types: number of entries in
+ * @supported_mapping_types
+ */
+ unsigned int nr_supported_mapping_types;
};

/**

--
2.54.0