Linux Media Controller development
 help / color / mirror / Atom feed
From: Alistair Popple <apopple@nvidia.com>
To: "Christian König" <christian.koenig@amd.com>,
	"Sumit Semwal" <sumit.semwal@linaro.org>,
	"Thomas Hellström" <thomas.hellstrom@linux.intel.com>,
	"Simona Vetter" <simona.vetter@ffwll.ch>,
	"Matthew Brost" <matthew.brost@intel.com>,
	"Vivek Kasireddy" <vivek.kasireddy@intel.com>,
	"Dongwon Kim" <dongwon.kim@intel.com>,
	"Kevin Tian" <kevin.tian@intel.com>,
	"Xu Yilun" <yilun.xu@linux.intel.com>,
	"Karl Mehltretter" <kmehltretter@gmail.com>,
	"Baolu Lu" <baolu.lu@linux.intel.com>,
	"Felix Kuehling" <felix.kuehling@amd.com>,
	"Alex Deucher" <alexander.deucher@amd.com>,
	linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org,
	intel-xe@lists.freedesktop.org, amd-gfx@lists.freedesktop.org,
	linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org,
	linux-rdma@vger.kernel.org, iommu@lists.linux.dev,
	"Keith Busch" <kbusch@kernel.org>, "Jens Axboe" <axboe@kernel.dk>,
	"Christoph Hellwig" <hch@lst.de>,
	"Sagi Grimberg" <sagi@grimberg.me>,
	linux-nvme@lists.infradead.org,
	"David S. Miller" <davem@davemloft.net>,
	"Eric Dumazet" <edumazet@kernel.org>,
	"Jakub Kicinski" <kuba@kernel.org>,
	"Paolo Abeni" <pabeni@redhat.com>,
	"Simon Horman" <horms@kernel.org>,
	"Mina Almasry" <almasrymina@google.com>,
	netdev@vger.kernel.org, "Alexey Kardashevskiy" <aik@amd.com>,
	zhipingz@meta.com, treding@nvidia.com, jhubbard@nvidia.com,
	chaitanyak@nvidia.com, "Jason Gunthorpe" <jgg@ziepe.ca>,
	"Leon Romanovsky" <leon@kernel.org>
Cc: Alistair Popple <apopple@nvidia.com>
Subject: [PATCH v3 04/24] dma-buf: Negotiate the mapping type in dma_buf_dynamic_attach()
Date: Fri, 20 Nov 2026 16:55:42 +1100	[thread overview]
Message-ID: <20261120-dma_mapping_type_v4-v3-4-c0183a409bdf@nvidia.com> (raw)
In-Reply-To: <20261120-dma_mapping_type_v4-v3-0-c0183a409bdf@nvidia.com>

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@nvidia.com>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
Signed-off-by: Alistair Popple <apopple@nvidia.com>

---

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


  parent reply	other threads:[~2026-11-20  5:56 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20  5:55 [PATCH v3 00/24] DMA-buf mapping types Alistair Popple
2026-11-20  5:55 ` [PATCH v3 01/24] treewide: Rename dma_buf_(un)map_attachment() to dma_buf_(un)map_sgt() Alistair Popple
2026-11-20  5:55 ` [PATCH v3 02/24] dma-buf: Introduce DMA-buf mapping types Alistair Popple
2026-11-20  5:55 ` [PATCH v3 03/24] dma-buf: Add the SGT DMA mapping type Alistair Popple
2026-11-20  5:55 ` Alistair Popple [this message]
2026-11-20  5:55 ` [PATCH v3 05/24] dma-buf: Route SGT related actions through the attachment's " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 06/24] dma-buf: Allow single exporter drivers to avoid the match_mapping function Alistair Popple
2026-11-20  5:55 ` [PATCH v3 07/24] dma-buf: Convert exporters allocating a table per map to the SGT mapping type Alistair Popple
2026-11-20  9:02   ` Karol Wachowski
2026-11-20  5:55 ` [PATCH v3 08/24] iommufd/selftest: Convert the test exporter " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 09/24] dma-buf: Convert exporters keeping a table per attachment " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 10/24] drm: Convert drm_gem_map_dma_buf() and its users " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 11/24] drm/xe/dma-buf: Use " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 12/24] drm/amdgpu: " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 13/24] udmabuf: Convert to " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 14/24] drm/omapdrm: " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 15/24] accel/ivpu: Convert the userptr exporter " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 16/24] drm/vmwgfx: Use match_mapping instead of dummy calls Alistair Popple
2026-11-20  5:55 ` [PATCH v3 17/24] dma-buf: Convert dma_buf_phys_vec_to_sgt() and its users to the SGT mapping type Alistair Popple
2026-11-20  5:55 ` [PATCH v3 18/24] iio: buffer: convert to use " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 19/24] functionfs: " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 20/24] dma-buf: Remove unused SGT stuff from the common structures Alistair Popple
2026-11-20  5:55 ` [PATCH v3 21/24] dma-buf: Add kunit tests for mapping type Alistair Popple
2026-11-20  5:56 ` [PATCH v3 22/24] dma-buf: Let the exporter select mapping type at pin/map Alistair Popple
2026-11-20  5:56 ` [PATCH v3 23/24] dma-buf: Add dma_buf_mapping_map() returning a type-tagged mapping and implement it for sgt Alistair Popple
2026-11-20  5:56 ` [PATCH v3 24/24] dma-buf: Add kunit tests for mapping type selection Alistair Popple

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261120-dma_mapping_type_v4-v3-4-c0183a409bdf@nvidia.com \
    --to=apopple@nvidia.com \
    --cc=aik@amd.com \
    --cc=alexander.deucher@amd.com \
    --cc=almasrymina@google.com \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=axboe@kernel.dk \
    --cc=baolu.lu@linux.intel.com \
    --cc=chaitanyak@nvidia.com \
    --cc=christian.koenig@amd.com \
    --cc=davem@davemloft.net \
    --cc=dongwon.kim@intel.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=edumazet@kernel.org \
    --cc=felix.kuehling@amd.com \
    --cc=hch@lst.de \
    --cc=horms@kernel.org \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=iommu@lists.linux.dev \
    --cc=jgg@ziepe.ca \
    --cc=jhubbard@nvidia.com \
    --cc=kbusch@kernel.org \
    --cc=kevin.tian@intel.com \
    --cc=kmehltretter@gmail.com \
    --cc=kuba@kernel.org \
    --cc=leon@kernel.org \
    --cc=linaro-mm-sig@lists.linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-nvme@lists.infradead.org \
    --cc=linux-rdma@vger.kernel.org \
    --cc=matthew.brost@intel.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=sagi@grimberg.me \
    --cc=simona.vetter@ffwll.ch \
    --cc=sumit.semwal@linaro.org \
    --cc=thomas.hellstrom@linux.intel.com \
    --cc=treding@nvidia.com \
    --cc=vivek.kasireddy@intel.com \
    --cc=yilun.xu@linux.intel.com \
    --cc=zhipingz@meta.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox